group_id
stringlengths
12
18
problem_description
stringlengths
85
3.02k
candidates
listlengths
3
20
aoj_1231_cpp
Problem H: Super Star During a voyage of the starship Hakodate-maru (see Problem A), researchers found strange synchronized movements of stars. Having heard these observations, Dr. Extreme proposed a theory of "super stars". Do not take this term as a description of actors or singers. It is a revolutionary theory in astronomy. According to this theory, stars we are observing are not independent objects, but only small portions of larger objects called super stars. A super star is filled with invisible (or transparent) material, and only a number of points inside or on its surface shine. These points are observed as stars by us. In order to verify this theory, Dr. Extreme wants to build motion equations of super stars and to compare the solutions of these equations with observed movements of stars. As the first step, he assumes that a super star is sphere-shaped, and has the smallest possible radius such that the sphere contains all given stars in or on it. This assumption makes it possible to estimate the volume of a super star, and thus its mass (the density of the invisible material is known). You are asked to help Dr. Extreme by writing a program which, given the locations of a number of stars, finds the smallest sphere containing all of them in or on it. In this computation, you should ignore the sizes of stars. In other words, a star should be regarded as a point. You may assume the universe is a Euclidean space. Input The input consists of multiple data sets. Each data set is given in the following format. n x 1 y 1 z 1 x 2 y 2 z 2 ... x n y n z n The first line of a data set contains an integer n , which is the number of points. It satisfies the condition 4 ≤ n ≤ 30. The locations of n points are given by three-dimensional orthogonal coordinates: ( x i , y i , z i ) ( i = 1,..., n ). Three coordinates of a point appear in a line, separated by a space character. Each value is given by a decimal fraction, and is between 0.0 and 100.0 (both ends inclusive). Points are at least 0.01 distant from each other. The end of the input is indicated by a line containing a zero. Output For each data set, the radius ofthe smallest sphere containing all given points should be printed, each in a separate line. The printed values should have 5 digits after the decimal point. They may not have an error greater than 0.00001. Sample Input 4 10.00000 10.00000 10.00000 20.00000 10.00000 10.00000 20.00000 20.00000 10.00000 10.00000 20.00000 10.00000 4 10.00000 10.00000 10.00000 10.00000 50.00000 50.00000 50.00000 10.00000 50.00000 50.00000 50.00000 10.00000 0 Output for the Sample Input 7.07107 34.64102
[ { "submission_id": "aoj_1231_10848858", "code_snippet": "//\n// main.cpp\n// poj 2069 Problem B : Super Star\n//\n// Created by nuu_tong on 2018/4/20.\n// Copyright © 2018年 apple. All rights reserved.\n//\n\n#include <iostream>\n#include <cmath>\n#include <cstdio>\nusing namespace std;\nstruct Point\n{\n double x,y,z;\n};\nPoint p[35];\ndouble dis[35];\nint N;\ndouble calDis(Point p1,Point p2)\n{\n return sqrt((p1.x-p2.x)*(p1.x-p2.x)+(p1.y-p2.y)*(p1.y-p2.y)+(p1.z-p2.z)*(p1.z-p2.z));\n}\nint main()\n{\n while(cin>>N&&N)\n {\n double ans=1e8;\n for(int i=0;i<N;i++)\n scanf(\"%lf%lf%lf\",&p[i].x,&p[i].y,&p[i].z);\n Point c;\n c.x=c.y=c.z=0;\n double step=100.0,len=0;\n int cur=0;\n while(step>1e-6)\n {\n for(int i=0;i<N;i++)//找出离当前位置最远的点\n {\n if(calDis(c,p[i])>calDis(c,p[cur]))\n cur=i;\n }\n len=calDis(c,p[cur]);//\n ans=min(ans,len);\n c.x+=(p[cur].x-c.x)/len*step;//逐步向最远点移动\n c.y+=(p[cur].y-c.y)/len*step;\n c.z+=(p[cur].z-c.z)/len*step;\n step*=0.9994;\n }\n printf(\"%.5lf\\n\",ans);\n }\n return 0;\n}", "accuracy": 1, "time_ms": 110, "memory_kb": 3380, "score_of_the_acc": -0.6124, "final_rank": 12 }, { "submission_id": "aoj_1231_9164989", "code_snippet": "#pragma region Macros\n\n#pragma GCC optimize(\"O3,unroll-loops\")\n#pragma GCC target(\"sse,sse2,sse3,ssse3,sse4,fma,abm,mmx,avx,avx2\")\n\n#include <bits/extc++.h>\n// #include <atcoder/all>\n// using namespace atcoder;\nusing namespace std;\nusing namespace __gnu_pbds;\n\n// #include <boost/multiprecision/cpp_dec_float.hpp>\n// #include <boost/multiprecision/cpp_int.hpp>\n// namespace mp = boost::multiprecision;\n// using Bint = mp::cpp_int;\n// using Bdouble = mp::number<mp::cpp_dec_float<256>>;\n// Bdouble Beps = 0.00000000000000000000000000000001; // 1e-32\n// const bool equals(Bdouble a, Bdouble b) { return mp::fabs(a - b) < Beps; }\n\n#define pb emplace_back\n#define int ll\n#define endl '\\n'\n// #define unordered_map<int, int> gp_hash_table<int, int, custom_hash> \n\n#define sqrt __builtin_sqrtl\n#define cbrt __builtin_cbrtl\n#define hypot __builtin_hypotl\n\n#define next asdnext\n#define prev asdprev\n\nusing ll = long long;\nusing ld = long double;\nconst ld PI = acosl(-1);\nconst int INF = 1 << 30;\nconst ll INFL = 1LL << 61;\n// const int MOD = 998244353;\nconst int MOD = 1000000007;\n\nconst ld EPS = 1e-10;\nconst bool equals(ld a, ld b) { return fabs((a) - (b)) < EPS; }\n\nconst vector<int> dx = {0, 1, 0, -1, 1, 1, -1, -1}; // → ↓ ← ↑ ↘ ↙ ↖ ↗\nconst vector<int> dy = {1, 0, -1, 0, 1, -1, -1, 1};\n\nstruct Edge {\n int from, to;\n double cost;\n Edge() : from(-1), to(-1), cost(-1) {}\n Edge(int to, double cost) : to(to), cost(cost) {}\n Edge(int from, int to, double cost) : from(from), to(to), cost(cost) {}\n bool operator ==(const Edge& e) {\n return this->from == e.from && this->to == e.to && this->cost == e.cost;\n }\n bool operator !=(const Edge& e) {\n return this->from != e.from or this->to != e.to or this->cost != e.cost;\n }\n};\n// struct Edge {\n// int from, to;\n// int cost;\n// Edge() : from(-1), to(-1), cost(-1) {}\n// Edge(int to, int cost) : to(to), cost(cost) {}\n// Edge(int from, int to, int cost) : from(from), to(to), cost(cost) {}\n// bool operator ==(const Edge& e) {\n// return this->from == e.from && this->to == e.to && this->cost == e.cost;\n// }\n// bool operator !=(const Edge& e) {\n// return this->from != e.from or this->to != e.to or this->cost != e.cost;\n// }\n// };\n\nchrono::system_clock::time_point start;\n__attribute__((constructor))\nvoid constructor() {\n ios::sync_with_stdio(false);\n cin.tie(nullptr);\n cout << fixed << setprecision(10);\n start = chrono::system_clock::now();\n}\n\nrandom_device seed_gen;\nmt19937_64 rng(seed_gen());\nuniform_int_distribution<int> dist_x(0, 1e9);\nstruct RNG {\n unsigned Int(unsigned l, unsigned r) {\n return dist_x(rng) % (r - l + 1) + l;\n }\n ld Double() {\n return ld(dist_x(rng)) / 1e9;\n }\n} rnd;\n\nusing i64 = ll;\n// using i64 = uint64_t;\n// bit演算, x==0の場合は例外処理した方がよさそう. 区間は [l, r)\ni64 lrmask(i64 l, i64 r) { return (1LL << r) - (1LL << l); }\ni64 sub_bit(i64 x, i64 l, i64 r) { i64 b = x & ((1LL << r) - (1LL << l)); return b >> l; } // r溢れ可\ni64 bit_width(i64 x) { return 64 - __builtin_clzll(x) + (x == 0); }\n\ni64 popcount(i64 x) { return __builtin_popcountll(x); }\ni64 popcount(i64 x, i64 l, i64 r) { return __builtin_popcountll(sub_bit(x, l, r)); }\ni64 unpopcount(i64 x) { return bit_width(x) - __builtin_popcountll(x); }\ni64 unpopcount(i64 x, i64 l, i64 r) { return r - l - __builtin_popcountll(sub_bit(x, l, r)); }\nbool is_pow2(i64 x) { return __builtin_popcountll(x) == 1; }\n\ni64 top_bit(i64 x) { return 63 - __builtin_clzll(x);} // 2^kの位 (x > 0)\ni64 bot_bit(i64 x) { return __builtin_ctz(x);} // 2^kの位 (x > 0)\n// i64 next_bit(i64 x, i64 k) { return 0; }\n// i64 prev_bit(i64 x, i64 k) { return 0; }\n// i64 kth_bit(i64 x, i64 k) { return 0; }\ni64 MSB(i64 x) { if (x == 0) return 0; return 1LL << (63 - __builtin_clzll(x)); } // mask\ni64 LSB(i64 x) { return (x & -x); } // mask\n\ni64 countl_zero(i64 x) { return __builtin_clzll(x); }\ni64 countl_one(i64 x) {\n i64 ret = 0, k = 63 - __builtin_clzll(x);\n while (k != -1 && (x & (1LL << k))) { k--; ret++; }\n return ret;\n}\ni64 countr_zero(i64 x) { return __builtin_ctzll(x); } // x==0のとき64が返ることに注意\ni64 countr_one(i64 x) { i64 ret = 0; while (x & 1) { x >>= 1; ret++; } return ret; }\n\ni64 floor_log2(i64 x) { if (x == 0) return 0; return 63 - __builtin_clzll(x); }\ni64 bit_floor(i64 x) { if (x == 0) return 0; return 1LL << (63 - __builtin_clzll(x)); } // MSBと同じ\ni64 ceil_log2(i64 x) { if (x == 0) return 0; return 64 - __builtin_clzll(x - 1); }\ni64 bit_ceil(i64 x) { if (x == 0) return 0; return 1LL << (64 - __builtin_clzll(x - 1)); }\n\ni64 rotl(i64 x, i64 k) { // 有効bit内でrotate. オーバーフロー注意\n i64 w = bit_width(x); k %= w;\n return ((x << k) | (x >> (w - k))) & ((1LL << w) - 1);\n}\n// i64 rotl(i64 x, i64 l, i64 m, i64 r) {}\ni64 rotr(i64 x, i64 k) {\n i64 w = bit_width(x); k %= w;\n return ((x >> k) | (x << (w - k))) & ((1LL << w) - 1);\n}\n// i64 rotr(i64 x, i64 l, i64 m, i64 r) {}\ni64 bit_reverse(i64 x) { // 有効bit内で左右反転\n i64 r = 0, w = bit_width(x);\n for (i64 i = 0; i < w; i++) r |= ((x >> i) & 1) << (w - i - 1);\n return r;\n}\n// i64 bit_reverse(i64 x, i64 l, i64 r) { return 0; }\n\nbool is_palindrome(i64 x) { return x == bit_reverse(x); }\nbool is_palindrome(i64 x, i64 l, i64 r) { i64 b = sub_bit(x, l, r); return b == bit_reverse(b); }\ni64 concat(i64 a, i64 b) { return (a << bit_width(b)) | b; } // オーバーフロー注意\ni64 erase(i64 x, i64 l, i64 r) { return x>>r<<l | x&(1LL<<l - 1); } // [l, r) をカット\n\ni64 hamming(i64 a, i64 b) { return __builtin_popcountll(a ^ b); }\ni64 hamming(i64 a, i64 b, i64 l, i64 r) { return __builtin_popcountll(sub_bit(a, l, r) ^ sub_bit(b, l, r)); }\ni64 compcount(i64 x) { return (__builtin_popcountll(x ^ (x >> 1)) + (x & 1)) / 2; }\ni64 compcount2(i64 x) { return compcount(x & (x >> 1)); } // 長さ2以上の連結成分の個数\ni64 adjacount(i64 x) { return __builtin_popcountll(x & (x >> 1)); } // 隣接する1のペアの個数\n\ni64 next_combination(i64 x) {\n i64 t = x | (x - 1); return (t + 1) | (((~t & -~t) - 1) >> (__builtin_ctz(x) + 1));\n}\n\n\n__int128_t POW(__int128_t x, int n) {\n __int128_t ret = 1;\n assert(n >= 0);\n if (x == 1 or n == 0) ret = 1;\n else if (x == -1 && n % 2 == 0) ret = 1; \n else if (x == -1) ret = -1; \n else if (n % 2 == 0) {\n assert(x < INFL);\n ret = POW(x * x, n / 2);\n } else {\n assert(x < INFL);\n ret = x * POW(x, n - 1);\n }\n return ret;\n}\nint per(int x, int y) { // x = qy + r (0 <= r < y) を満たすq\n assert(y != 0);\n if (x >= 0 && y > 0) return x / y;\n if (x >= 0 && y < 0) return x / y - (x % y < 0);\n if (x < 0 && y < 0) return x / y + (x % y < 0);\n return x / y - (x % y < 0); // (x < 0 && y > 0) \n}\nint mod(int x, int y) { // x = qy + r (0 <= r < y) を満たすr\n assert(y != 0);\n if (x >= 0) return x % y;\n __int128_t ret = x % y; // (x < 0)\n ret += (__int128_t)abs(y) * INFL;\n ret %= abs(y);\n return ret;\n}\nint floor(int x, int y) { // (ld)x / y 以下の最大の整数\n assert(y != 0);\n if (y < 0) x = -x, y = -y;\n return x >= 0 ? x / y : (x + 1) / y - 1;\n}\nint ceil(int x, int y) { // (ld)x / y 以上の最小の整数\n assert(y != 0);\n if (y < 0) x = -x, y = -y;\n return x > 0 ? (x - 1) / y + 1 : x / y;\n}\nint round(int x, int y) {\n assert(y != 0);\n return (x * 2 + y) / (y * 2);\n}\nint round(int x, int y, int k) { // (ld)(x/y)を10^kの位に関して四捨五入\n assert(y != 0); // TODO\n return INF;\n}\nint round2(int x, int y) { // 五捨五超入 // 未verify\n assert(y != 0);\n if (y < 0) y = -y, x = -x;\n int z = z / y;\n if ((z * 2 + 1) * y <= y * 2) z++;\n return z;\n}\n// int round(ld x, int k) { // xを10^kの位に関して四捨五入\n// }\n// int floor(ld x, int k) { // xを10^kの位に関してflooring\n// }\n// int ceil(ld x, int k) { // xを10^kの位に関してceiling\n// }\n// int kth(int x, int y, int k) { // x / yの10^kの位の桁\n// }\nint floor(ld x, ld y) { // 誤差対策TODO\n assert(!equals(y, 0));\n return floor(x / y);\n // floor(x) = ceil(x - 1) という話も\n}\nint ceil(ld x, ld y) { // 誤差対策TODO // ceil(p/q) = -floor(-(p/q))らしい\n assert(!equals(y, 0));\n return ceil(x / y);\n // ceil(x) = floor(x + 1)\n}\nint perl(ld x, ld y) { // x = qy + r (0 <= r < y, qは整数) を満たす q\n // 未verify. 誤差対策TODO. EPS外してもいいかも。\n assert(!equals(y, 0));\n if (x >= 0 && y > 0) return floor(x / y)+EPS;\n if (x >= 0 && y < 0) return -floor(x / fabs(y));\n if (x < 0 && y < 0) return floor(x / y) + (x - floor(x/y)*y < -EPS);\n return floor(x / y) - (x - floor(x/y)*y < -EPS); // (x < 0 && y > 0) \n}\nld modl(ld x, ld y) { // x = qy + r (0 <= r < y, qは整数) を満たす r\n // 未verify. 誤差対策TODO. -0.0が返りうる。\n assert(!equals(y, 0));\n if (x >= 0) return x - fabs(y)*fabs(per(x, y));\n return x - fabs(y)*floor(x, fabs(y));\n}\nint seisuu(ld x) { return (int)x; } // 整数部分. 誤差対策TODO\nint modf(ld x) {\n\tif (x < 0) return ceill(x);\n\telse return floorl(x);\n}\n// 正なら+EPS, 負なら-EPSしてから、文字列に直して小数点以下を捨てる?\nint seisuu(int x, int y) {\n assert(y != 0);\n return x / y;\n}\nint seisuu(ld x, ld y) { // 誤差対策TODO\n assert(!equals(y, 0));\n return (int)(x / y);\n}\n\ntemplate <class T> pair<T, T> max(const pair<T, T> &a, const pair<T, T> &b) {\n if (a.first > b.first or a.first == b.first && a.second > b.second) return a;\n return b;\n}\ntemplate <class T> pair<T, T> min(const pair<T, T> &a, const pair<T, T> &b) {\n if (a.first < b.first or a.first == b.first && a.second < b.second) return a;\n return b;\n}\n\ntemplate <class T> bool chmax(T &a, const T& b) {\n if (a < b) { a = b; return true; } return false;\n}\ntemplate <class T> bool chmin(T &a, const T& b) {\n if (a > b) { a = b; return true; } return false;\n}\ntemplate <class T> T mid(T a, T b, T c) { // 誤差対策TODO\n return a + b + c - max({a, b, c}) - min({a, b, c});\n}\ntemplate <class T> void Sort(T &a, T &b, bool rev = false) { \n if (rev == false) { // TODO テンプレート引数\n if (a > b) swap(a, b);\n } else {\n if (b > a) swap(b, a);\n }\n}\ntemplate <class T> void Sort(T &a, T &b, T &c, bool rev = false) {\n if (rev == false) { \n if (a > b) swap(a, b); if (a > c) swap(a, c); if (b > c) swap(b, c);\n } else {\n if (c > b) swap(c, b); if (c > a) swap(c, a); if (b > a) swap(b, a);\n }\n}\ntemplate <class T> void Sort(T &a, T &b, T &c, T &d, bool rev = false) {\n if (rev == false) { \n if (a > b) swap(a, b); if (a > c) swap(a, c); if (a > d) swap(a, d);\n if (b > c) swap(b, c); if (b > d) swap(b, d); if (c > d) swap(c, d);\n } else {\n if (d > c) swap(d, c); if (d > b) swap(d, b); if (d > a) swap(d, a);\n if (c > b) swap(c, b); if (c > a) swap(c, a); if (b > a) swap(b, a);\n }\n}\n\nstruct custom_hash {\n static uint64_t splitmix64(uint64_t x) {\n x += 0x9e3779b97f4a7c15;\n x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;\n x = (x ^ (x >> 27)) * 0x94d049bb133111eb;\n return x ^ (x >> 31);\n }\n\n size_t operator()(uint64_t x) const {\n static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count();\n return splitmix64(x + FIXED_RANDOM);\n }\n};\n\nclass Compress {\npublic:\n int sz = 0;\n // gp_hash_table<int, int, custom_hash> Z, UZ;\n unordered_map<int, int> Z; // 元の値 -> 圧縮した値\n unordered_map<int, int> UZ; // 圧縮した値 -> 元の値\n\n Compress(const vector<int> &V, int base = 0) {\n this->sz = base;\n set<int> s(V.begin(), V.end());\n\n for (int x : s) {\n this->Z[x] = this->sz;\n this->UZ[this->sz] = x;\n this->sz++;\n }\n }\n \n Compress(const vector<int> &V1, const vector<int> &V2, int base = 0) {\n this->sz = base;\n vector<int> V3 = V2;\n V3.insert(V3.end(), V1.begin(), V1.end());\n set<int> s(V3.begin(), V3.end());\n\n for (int x : s) {\n this->Z[x] = this->sz;\n this->UZ[this->sz] = x;\n this->sz++;\n }\n }\n\n Compress(const vector<int> &V1, const vector<int> &V2, const vector<int> &V3, int base = 0) {\n this->sz = base;\n vector<int> V4 = V1;\n V4.insert(V4.end(), V2.begin(), V2.end());\n V4.insert(V4.end(), V3.begin(), V3.end());\n set<int> s(V4.begin(), V4.end());\n\n for (int x : s) {\n this->Z[x] = this->sz;\n this->UZ[this->sz] = x;\n this->sz++;\n }\n }\n\n Compress(const vector<int> &V1, const vector<int> &V2,\n const vector<int> &V3, const vector<int> &V4, int base = 0) {\n this->sz = base;\n vector<int> V5 = V1;\n V5.insert(V5.end(), V2.begin(), V2.end());\n V5.insert(V5.end(), V3.begin(), V3.end());\n V5.insert(V5.end(), V4.begin(), V4.end());\n set<int> s(V5.begin(), V5.end());\n\n for (int x : s) {\n this->Z[x] = this->sz;\n this->UZ[this->sz] = x;\n this->sz++;\n }\n }\n\n vector<int> zip(const vector<int> &V) {\n vector<int> ret = V;\n for (int i = 0; i < (int)V.size(); i++) {\n ret[i] = Z[ret[i]];\n }\n return ret;\n }\n\n vector<int> unzip(const vector<int> &V) {\n vector<int> ret = V;\n for (int i = 0; i < (int)V.size(); i++) {\n ret[i] = UZ[ret[i]];\n }\n return ret;\n }\n\n int size() { return sz; }\n\n int encode(int x) { return Z[x]; }\n int decode(int x) {\n if (UZ.find(x) == UZ.end()) return -1; // xが元の配列に存在しないとき\n return UZ[x];\n }\n};\n\nclass UnionFind {\npublic:\n\tUnionFind() = default;\n UnionFind(int N) : par(N), sz(N, 1) {\n iota(par.begin(), par.end(), 0);\n }\n\tint root(int x) {\n\t\tif (par[x] == x) return x;\n\t\treturn (par[x] = root(par[x]));\n\t}\n\tbool unite(int x, int y) {\n\t\tint rx = root(x);\n\t\tint ry = root(y);\n if (rx == ry) return false;\n\t\tif (sz[rx] < sz[ry]) swap(rx, ry);\n\t\tsz[rx] += sz[ry];\n\t\tpar[ry] = rx;\n return true;\n\t}\n\tbool issame(int x, int y) { return (root(x) == root(y)); }\n\tint size(int x) { return sz[root(x)]; }\n vector<vector<int>> groups(int N) {\n vector<vector<int>> G(N);\n for (int x = 0; x < N; x++) {\n G[root(x)].push_back(x);\n }\n\t\tG.erase( remove_if(G.begin(), G.end(),\n [&](const vector<int>& V) { return V.empty(); }), G.end());\n return G;\n }\nprivate:\n\tvector<int> par, sz;\n};\n\ntemplate<typename T>\nstruct BIT {\n int N; // 要素数\n vector<T> bit[2]; // データの格納先\n BIT(int N_, int x = 0) {\n N = N_ + 1;\n bit[0].assign(N, 0); bit[1].assign(N, 0);\n if (x != 0) {\n for (int i = 0; i < N; i++) add(i, x);\n }\n }\n BIT(const vector<int> &A) {\n N = A.size() + 1;\n bit[0].assign(N, 0); bit[1].assign(N, 0);\n for (int i = 0; i < (int)A.size(); i++) add(i, A[i]);\n }\n void add_sub(int p, int i, T x) {\n while (i < N) { bit[p][i] += x; i += (i & -i); }\n }\n void add(int l, int r, T x) {\n add_sub(0, l + 1, -x * l); add_sub(0, r + 1, x * r);\n add_sub(1, l + 1, x); add_sub(1, r + 1, -x);\n }\n void add(int i, T x) { add(i, i + 1, x); }\n T sum_sub(int p, int i) {\n T ret = 0;\n while (i > 0) { ret += bit[p][i]; i -= (i & -i); }\n return ret;\n }\n T sum(int i) { return sum_sub(0, i) + sum_sub(1, i) * i; }\n T sum(int l, int r) { return sum(r) - sum(l); }\n T get(int i) { return sum(i, i + 1); }\n void set(int i, T x) { T s = get(i); add(i, -s + x); }\n};\n\ntemplate<int mod> class Modint {\npublic:\n int val = 0;\n Modint(int x = 0) { while (x < 0) x += mod; val = x % mod; }\n Modint(const Modint &r) { val = r.val; }\n\n Modint operator -() { return Modint(-val); } // 単項\n Modint operator +(const Modint &r) { return Modint(*this) += r; }\n Modint operator +(const int &q) { Modint r(q); return Modint(*this) += r; }\n Modint operator -(const Modint &r) { return Modint(*this) -= r; }\n Modint operator -(const int &q) { Modint r(q); return Modint(*this) -= r; }\n Modint operator *(const Modint &r) { return Modint(*this) *= r; }\n Modint operator *(const int &q) { Modint r(q); return Modint(*this) *= r; }\n Modint operator /(const Modint &r) { return Modint(*this) /= r; }\n Modint operator /(const int &q) { Modint r(q); return Modint(*this) /= r; }\n \n Modint& operator ++() { val++; if (val >= mod) val -= mod; return *this; } // 前置\n Modint operator ++(signed) { ++*this; return *this; } // 後置\n Modint& operator --() { val--; if (val < 0) val += mod; return *this; }\n Modint operator --(signed) { --*this; return *this; }\n Modint &operator +=(const Modint &r) { val += r.val; if (val >= mod) val -= mod; return *this; }\n Modint &operator +=(const int &q) { Modint r(q); val += r.val; if (val >= mod) val -= mod; return *this; }\n Modint &operator -=(const Modint &r) { if (val < r.val) val += mod; val -= r.val; return *this; }\n Modint &operator -=(const int &q) { Modint r(q); if (val < r.val) val += mod; val -= r.val; return *this; }\n Modint &operator *=(const Modint &r) { val = val * r.val % mod; return *this; }\n Modint &operator *=(const int &q) { Modint r(q); val = val * r.val % mod; return *this; }\n Modint &operator /=(const Modint &r) {\n int a = r.val, b = mod, u = 1, v = 0;\n while (b) {int t = a / b; a -= t * b; swap(a, b); u -= t * v; swap(u, v);}\n val = val * u % mod; if (val < 0) val += mod;\n return *this;\n }\n Modint &operator /=(const int &q) {\n Modint r(q); int a = r.val, b = mod, u = 1, v = 0;\n while (b) {int t = a / b; a -= t * b; swap(a, b); u -= t * v; swap(u, v);}\n val = val * u % mod; if (val < 0) val += mod;\n return *this;\n }\n bool operator ==(const Modint& r) { return this -> val == r.val; }\n bool operator <(const Modint& r) { return this -> val < r.val; }\n bool operator >(const Modint& r) { return this -> val > r.val; }\n bool operator !=(const Modint& r) { return this -> val != r.val; }\n};\n\nusing mint = Modint<MOD>;\n\nistream &operator >>(istream &is, mint& x) {\n int t; is >> t; x = t; return (is);\n}\nostream &operator <<(ostream &os, const mint& x) {\n return os << x.val;\n}\nmint modpow(const mint &x, int n) {\n if (n < 0) return (mint)1 / modpow(x, -n); // 未verify\n assert(n >= 0);\n if (n == 0) return 1;\n mint t = modpow(x, n / 2);\n t = t * t;\n if (n & 1) t = t * x;\n return t;\n}\n\nint modpow(__int128_t x, int n, int mod) {\n assert(n >= 0 && mod > 0); // TODO: n <= -1\n __int128_t ret = 1;\n while (n > 0) {\n if (n % 2 == 1) ret = ret * x % mod;\n x = x * x % mod;\n n /= 2;\n }\n return ret;\n}\nint modinv(__int128_t x, int mod) {\n assert(mod > 0);\n // assert(x > 0);\n if (x == 1 or x == 0) return 1;\n return mod - modinv(mod % x, mod) * (mod / x) % mod;\n}\n\nistream &operator >>(istream &is, __int128_t& x) {\n string S; is >> S;\n __int128_t ret = 0;\n int f = 1;\n if (S[0] == '-') f = -1; \n for (int i = 0; i < S.length(); i++)\n if ('0' <= S[i] && S[i] <= '9')\n ret = ret * 10 + S[i] - '0';\n x = ret * f;\n return (is);\n}\nostream &operator <<(ostream &os, __int128_t x) {\n ostream::sentry s(os);\n if (s) {\n __uint128_t tmp = x < 0 ? -x : x;\n char buffer[128]; char *d = end(buffer);\n do {\n --d; *d = \"0123456789\"[tmp % 10]; tmp /= 10;\n } while (tmp != 0);\n if (x < 0) { --d; *d = '-'; }\n int len = end(buffer) - d;\n if (os.rdbuf()->sputn(d, len) != len) os.setstate(ios_base::badbit);\n }\n return os;\n}\n\n__int128_t stoll(string &S) {\n __int128_t ret = 0; int f = 1;\n if (S[0] == '-') f = -1; \n for (int i = 0; i < S.length(); i++)\n if ('0' <= S[i] && S[i] <= '9') ret = ret * 10 + S[i] - '0';\n return ret * f;\n}\n__int128_t gcd(__int128_t a, __int128_t b) { return b ? gcd(b, a % b) : a; }\n__int128_t lcm(__int128_t a, __int128_t b) {\n return a / gcd(a, b) * b;\n // lcmが__int128_tに収まる必要あり\n}\n\nstring to_string(ld x, int k) { // xの小数第k位までをstring化する\n assert(k >= 0);\n stringstream ss;\n ss << setprecision(k + 2) << x;\n string s = ss.str();\n if (s.find('.') == string::npos) s += '.';\n int pos = s.find('.');\n for (int i = 0; k >= (int)s.size() - 1 - pos; i++) s += '0';\n s.pop_back();\n if (s.back() == '.') s.pop_back();\n return s;\n\n // stringstream ss; // 第k+1位を四捨五入して第k位まで返す\n // ss << setprecision(k + 1) << x;\n // string s = ss.str();\n // if (s.find('.') == string::npos) s += '.';\n // int pos = s.find('.');\n // for (int i = 0; k > (int)s.size() - 1 - pos; i++) s += '0';\n // if (s.back() == '.') s.pop_back();\n // return s;\n}\nstring to_string(__int128_t x) {\n string ret = \"\";\n if (x < 0) { ret += \"-\"; x *= -1; }\n while (x) { ret += (char)('0' + x % 10); x /= 10; }\n reverse(ret.begin(), ret.end());\n return ret;\n}\nstring to_string(char c) { string s = \"\"; s += c; return s; }\n\ntemplate<class T> size_t HashCombine(const size_t seed,const T &v) {\n return seed^(hash<T>()(v)+0x9e3779b9+(seed<<6)+(seed>>2));\n}\ntemplate<class T,class S> struct hash<pair<T,S>>{\n size_t operator()(const pair<T,S> &keyval) const noexcept {\n return HashCombine(hash<T>()(keyval.first), keyval.second);\n }\n};\ntemplate<class T> struct hash<vector<T>>{\n size_t operator()(const vector<T> &keyval) const noexcept {\n size_t s=0;\n for (auto&& v: keyval) s=HashCombine(s,v);\n return s;\n }\n};\ntemplate<int N> struct HashTupleCore{\n template<class Tuple> size_t operator()(const Tuple &keyval) const noexcept{\n size_t s=HashTupleCore<N-1>()(keyval);\n return HashCombine(s,get<N-1>(keyval));\n }\n};\ntemplate <> struct HashTupleCore<0>{\n template<class Tuple> size_t operator()(const Tuple &keyval) const noexcept{ return 0; }\n};\ntemplate<class... Args> struct hash<tuple<Args...>>{\n size_t operator()(const tuple<Args...> &keyval) const noexcept {\n return HashTupleCore<tuple_size<tuple<Args...>>::value>()(keyval);\n }\n};\n\nvector<mint> _fac, _finv, _inv;\nvoid COMinit(int N) {\n _fac.resize(N + 1); _finv.resize(N + 1); _inv.resize(N + 1);\n _fac[0] = _fac[1] = 1; _finv[0] = _finv[1] = 1; _inv[1] = 1;\n for (int i = 2; i <= N; i++) {\n _fac[i] = _fac[i-1] * mint(i);\n _inv[i] = -_inv[MOD % i] * mint(MOD / i);\n _finv[i] = _finv[i - 1] * _inv[i];\n }\n}\n\nmint FAC(int N) {\n if (N < 0) return 0; return _fac[N];\n}\nmint COM(int N, int K) {\n if (N < K) return 0; if (N < 0 or K < 0) return 0;\n return _fac[N] * _finv[K] * _finv[N - K];\n}\nmint PERM(int N, int K) {\n if (N < K) return 0; if (N < 0 or K < 0) return 0;\n return _fac[N] * _finv[N - K];\n}\nmint NHK(int N, int K) { // initのサイズに注意\n if (N == 0 && K == 0) return 1;\n return COM(N + K - 1, K);\n}\n\n#pragma endregion\n\nclass Point3d {\npublic:\n double x, y, z;\n\n Point3d(double x = 0, double y = 0, double z = 0) : x(x), y(y), z(z) {}\n\n Point3d operator+(const Point3d& a) {\n return Point3d(x + a.x, y + a.y, z + a.z);\n }\n Point3d operator-(const Point3d& a) {\n return Point3d(x - a.x, y - a.y, z - a.z);\n }\n Point3d operator*(const double& d) {\n return Point3d(x * d, y * d, z * d);\n }\n Point3d operator/(const double& d) {\n return Point3d(x / d, y / d, z / d);\n }\n\n bool operator<(const Point3d& p) const {\n if (!equals(x, p.x)) return x < p.x;\n if (!equals(y, p.y)) return y < p.y;\n if (!equals(z, p.z)) return z < p.z;\n return false;\n }\n\n bool operator==(const Point3d& p) const {\n return equals(x, p.x) && equals(y, p.y) && equals(z, p.z);\n }\n\n friend istream& operator>>(istream& is, Point3d& p) {\n\t\tis >> p.x >> p.y >> p.z;\n\t\treturn is;\n\t}\n\tfriend ostream& operator<<(ostream& os, Point3d& p) {\n\t\tos << p.x << \" \" << p.y << \" \" << p.z;\n\t\treturn os;\n\t}\n};\n\nint sign(double x) { return x < -EPS ? -1 : x > EPS; } // -1(負)/0/1(正)\n\nstruct Segment3d {\n Point3d p[2], d; // pは端点、dは向き\n Segment3d(Point3d p1 = Point3d(), Point3d p2 = Point3d()) {\n p[0] = p1, p[1] = p2;\n d = p[1] - p[0];\n }\n bool operator==(const Segment3d& S) const {\n return (p[0] == S.p[0] && p[1] == S.p[1]) || (p[0] == S.p[1] && p[1] == S.p[0]);\n }\n};\n\nusing Line3d = Segment3d;\nusing Vector3d = Point3d;\n\nbool is_parallel(Vector3d v1, Vector3d v2) {\n if (equals(v1.x*v2.y, v1.y*v2.x) && \n equals(v1.y*v2.z, v1.z*v2.y) &&\n equals(v1.z*v2.x, v1.x*v2.z)) return true;\n return false;\n}\nbool is_orthogonal(Vector3d v1, Vector3d v2) {\n if (equals(v1.x*v2.x + v1.y*v2.y + v1.z*v2.z, 0)) return true;\n return false;\n}\n\nbool is_parallel(Line3d &l, Line3d &m) {\n return is_parallel(l.p[1] - l.p[0], m.p[1] - m.p[0]);\n}\nbool is_orthogonal(Line3d &l, Line3d &m) {\n return is_orthogonal(l.p[1] - l.p[0], m.p[1] - m.p[0]);\n}\n\nostream& operator<<(ostream& os, const Point3d& p) {\n return os << \"(\" << p.x << \",\" << p.y << \",\" << p.z << \")\";\n}\n\nostream& operator<<(ostream& os, const Segment3d& S) {\n return os << \"(\" << S.p[0] << \",\" << S.p[1] << \")\";\n}\n\ndouble dot(const Point3d& a, const Point3d& b) {\n return a.x * b.x + a.y * b.y + a.z * b.z;\n}\n\nVector3d cross(const Point3d& a, const Point3d& b) {\n return Vector3d(a.y * b.z - a.z * b.y, a.z * b.x - a.x * b.z, a.x * b.y - a.y * b.x);\n}\n\ninline double norm(const Point3d& p) {\n return p.x * p.x + p.y * p.y + p.z * p.z;\n}\n\ninline double abs(const Point3d& p) {\n return sqrt(norm(p));\n}\n\ndouble rad_to_deg(double r) { return (r * 180.0 / PI); }\ndouble deg_to_rad(double d) { return (d * PI / 180.0); }\n\ndouble angle(Point3d a, Point3d b) {\n double costheta = dot(a, b) / norm(a) / norm(b);\n return acos(max<double>(-1.0, min<double>(1.0, costheta)));\n}\n\nPoint3d rotateccw90(Point3d a) { return Point3d(-a.y, a.x); }\nPoint3d rotatecw90(Point3d a) { return Point3d(a.y, -a.x); }\nPoint3d rotateccw(Point3d a, double t) { return Point3d(a.x * cos(t) - a.y * sin(t), a.x * sin(t) + a.y * cos(t)); }\nPoint3d rotatecw(Point3d a, double t) { return Point3d(a.x * cos(t) + a.y * sin(t), -a.x * sin(t) + a.y * cos(t)); }\n\ndouble distanceLP(Line3d L, Point3d p) {\n return abs(cross(L.p[1] - L.p[0], p - L.p[0])) / abs(L.p[1] - L.p[0]);\n}\n\ndouble dist(Point3d p1, Point3d p2) {\n return sqrt(norm(p1 - p2));\n}\n\nPoint3d project(Segment3d S, Point3d p) {\n Vector3d base = S.p[1] - S.p[0];\n double t = dot(p - S.p[0], base) / norm(base);\n return S.p[0] + base * t;\n}\n\nPoint3d reflect(Segment3d S, Point3d p) {\n return p + (project(S, p) - p) * 2.0;\n}\n\nbool on_line3d(Line3d L, Point3d p) {\n return equals(abs(cross(L.p[1] - p, L.p[0] - p)), 0);\n}\n\nbool on_segment3d(Segment3d S, Point3d p) {\n if (!on_line3d(S, p)) return false;\n double dist[3] = {abs(S.p[1] - S.p[0]), abs(p - S.p[0]), abs(p - S.p[1])};\n return on_line3d(S, p) && equals(dist[0], dist[1] + dist[2]);\n}\n\ndouble distanceSP(Segment3d S, Point3d p) {\n Point3d r = project(S, p);\n if (on_segment3d(S, r)) return abs(p - r);\n return min<double>(abs(S.p[0] - p), abs(S.p[1] - p));\n}\n\nstruct Sphere {\n Point3d c;\n double r;\n Sphere() {}\n Sphere(Point3d c, double r) : c(c), r(r) {}\n};\n\n// https://github.com/ShahjalalShohag/code-library/blob/main/Geometry/Geometry%203D.cpp\nSphere smallest_enclosing_sphere(vector<Point3d> p) {\n int n = p.size();\n Point3d c(0, 0, 0);\n for(int i = 0; i < n; i++) c = c + p[i];\n c = c / n;\n\n double ratio = 0.1;\n int pos = 0;\n int it = 100000;\n while (it--) {\n pos = 0;\n for (int i = 1; i < n; i++) {\n if(dot(c - p[i], c - p[i]) > dot(c - p[pos], c - p[pos])) pos = i;\n }\n c = c + (p[pos] - c) * ratio;\n ratio *= 0.998;\n }\n return Sphere(c, abs(c - p[pos]));\n}\n\nsigned main() {\n int N;\n while (cin >> N, N) {\n vector<Point3d> P(N);\n for (int i = 0; i < N; i++) cin >> P[i];\n\n Sphere S = smallest_enclosing_sphere(P);\n cout << S.r << endl;\n }\n}", "accuracy": 1, "time_ms": 180, "memory_kb": 3528, "score_of_the_acc": -0.6721, "final_rank": 13 }, { "submission_id": "aoj_1231_4953208", "code_snippet": "#include <iostream>\n#include <vector>\n#include <cmath>\n#include <numeric>\n#include <cfloat>\n#include <climits>\n#include <algorithm>\n#include <iomanip>\n#include <queue>\n#include <unordered_set>\n#include <unordered_map>\n#include <string>\n#include <tuple>\n#include <set>\n#include <stack>\n#include <array>\n#include <cassert>\nconstexpr double EPSILON = 1e-8;\nstruct Point {\n\tdouble x, y, z;\n\tdouble distance(const Point& other) const;\n};\nstruct Angle {\n\tdouble sin, cos;\n\tstatic Angle from_cos(const double cos) {\n\t\treturn Angle{ std::sqrt(1 - cos * cos), cos };\n\t}\n\tAngle to_under180() const {\n\t\treturn (sin >= 0) ? *this : Angle{ -sin, cos };\n\t}\n};\nstruct Vector {\n\tdouble dx, dy, dz;\n\tdouble length() const {\n\t\treturn std::sqrt(dx * dx + dy * dy + dz * dz);\n\t}\n\tdouble dot(const Vector& other) const {\n\t\treturn dx * other.dx + dy * other.dy + dz * other.dz;\n\t}\n\tVector cross(const Vector& other) const {\n\t\treturn Vector{\n\t\t\tdy * other.dz - dz * other.dy,\n\t\t\tdz * other.dx - dx * other.dz,\n\t\t\tdx * other.dy - dy * other.dx\n\t\t};\n\t}\n\tAngle angle_between(const Vector& other) const {\n\t\tconst auto len = length() * other.length();\n\t\treturn Angle{ cross(other).length() / len, dot(other) / len };\n\t}\n};\nVector operator*(const Vector& vec, const double scale) {\n\treturn Vector{ vec.dx * scale, vec.dy * scale, vec.dz * scale };\n}\nVector operator*(const double scale, const Vector& vec) {\n\treturn Vector{ vec.dx * scale, vec.dy * scale, vec.dz * scale };\n}\nVector operator+(const Vector& a, const Vector& b) {\n\treturn Vector{ a.dx + b.dx, a.dy + b.dy, a.dz + b.dz };\n}\nPoint operator+(const Vector& vec, const Point& p) {\n\treturn Point{ p.x + vec.dx, p.y + vec.dy, p.z + vec.dz };\n}\nPoint operator+(const Point& p, const Vector& vec) {\n\treturn vec + p;\n}\nVector operator-(const Point& a, const Point& b) {\n\treturn Vector{ a.x - b.x, a.y - b.y, a.z - b.z };\n}\nstruct Triangle {\n\tstd::array<Point, 3> vertex;\n\tAngle angle_a() const {\n\t\treturn (vertex[1] - vertex[0]).angle_between(vertex[2] - vertex[0]).to_under180();\n\t}\n\tAngle angle_b() const {\n\t\treturn (vertex[2] - vertex[1]).angle_between(vertex[0] - vertex[1]).to_under180();\n\t}\n\tAngle angle_c() const {\n\t\treturn (vertex[0] - vertex[2]).angle_between(vertex[1] - vertex[2]).to_under180();\n\t}\n\tdouble len_a() const {\n\t\treturn (vertex[2] - vertex[1]).length();\n\t}\n\tdouble len_b() const {\n\t\treturn (vertex[0] - vertex[2]).length();\n\t}\n\tdouble len_c() const {\n\t\treturn (vertex[1] - vertex[0]).length();\n\t}\n\tPoint circumcenter() const {\n\t\tconst auto A = angle_a();\n\t\tconst auto B = angle_b();\n\t\tconst auto C = angle_c();\n\t\tconst auto a = len_a();\n\t\tconst auto b = len_b();\n\t\tconst auto c = len_c();\n\t\tconst auto vec_a = vertex[0] - Point{ 0, 0 };\n\t\tconst auto vec_b = vertex[1] - Point{ 0, 0 };\n\t\tconst auto vec_c = vertex[2] - Point{ 0, 0 };\n\t\treturn Point{ 0, 0 } + (vec_a * a * A.cos + vec_b * b * B.cos + vec_c * c * C.cos) * (1 / (a * A.cos + b * B.cos + c * C.cos));\n\t}\n};\nstd::vector<double>& operator+=(std::vector<double>& dest, const std::vector<double>& add) {\n\tfor (auto i = 0; i < dest.size(); ++i) {\n\t\tdest[i] += add[i];\n\t}\n\treturn dest;\n}\nstd::vector<double>& operator-=(std::vector<double>& dest, const std::vector<double>& sub) {\n\tfor (auto i = 0; i < dest.size(); ++i) {\n\t\tdest[i] -= sub[i];\n\t}\n\treturn dest;\n}\nstd::vector<double>& operator*=(std::vector<double>& dest, const double scale) {\n\tfor (auto& a : dest) a *= scale;\n\treturn dest;\n}\nvoid remove(std::vector<double>& dest, const std::vector<double>& vec, const int key) {\n\tconst auto ratio = dest[key] / vec[key];\n\tfor (auto i = 0; i < dest.size(); ++i) {\n\t\tdest[i] -= vec[i] * ratio;\n\t}\n}\nbool solve(std::vector<std::vector<double>> &matrix) {\n\tfor (auto i = 0; i < matrix.size(); ++i) {\n\t\tint key{ -1 };\n\t\tfor (auto j = i; j < matrix.size(); ++j) {\n\t\t\tif (std::abs(matrix[j][i]) > DBL_EPSILON) {\n\t\t\t\tkey = j; break;\n\t\t\t}\n\t\t}\n\t\tif (key < 0) return false;\n\t\tif (key != i) matrix[i] += matrix[key];\n\t\tmatrix[i] *= 1 / matrix[i][i];\n\t\tfor (auto j = 0; j < matrix.size(); ++j) {\n\t\t\tif (i == j) continue;\n\t\t\tremove(matrix[j], matrix[i], i);\n\t\t}\n\t}\n\treturn true;\n}\nstd::vector<double> circle_equation(const Point p) {\n\treturn { -2 * p.x, -2 * p.y, -2 * p.z, -p.x * p.x - p.y * p.y - p.z * p.z };\n}\nstruct Circle {\n\tPoint center;\n\tdouble radius;\n\tbool contains(const Point& point) const {\n\t\treturn point.distance(center) < radius + EPSILON;\n\t}\n};\nbool is_on_same_plane(const Point &a, const Point &b, const Point &c, const Point &d) {\n\treturn std::abs((a - b).cross(c - b).dot(d - b)) < EPSILON;\n}\nbool is_on_same_line(const Point &a, const Point &b, const Point &c) {\n\treturn std::abs((a - b).cross(c - b).length()) < EPSILON;\n}\nCircle calc_circle(const Point a, const Point b) {\n\tconst Point center{ (a.x + b.x) / 2, (a.y + b.y) / 2, (a.z + b.z) / 2 };\n\tconst auto radius = center.distance(a);\n\t//assert(std::abs(radius - center.distance(b)) < EPSILON);\n\treturn Circle{ center, radius };\n}\nCircle calc_circle(const Point a, const Point b, const Point c) {\n\tif (is_on_same_line(a, b, c)) return calc_circle(a, b);\n\tconst Triangle triangle{ a, b, c };\n\tconst auto center = triangle.circumcenter();\n\tconst auto radius = center.distance(a);\n\t//assert(std::abs(radius - center.distance(b)) < EPSILON);\n\t//assert(std::abs(radius - center.distance(c)) < EPSILON);\n\treturn Circle{ center, radius };\n}\nCircle calc_circle(const Point a, const Point b, const Point c, const Point d) {\n\tif (is_on_same_plane(a, b, c, d)) { return calc_circle(a, b, c); }\n\tstd::vector<std::vector<double>> equation{ circle_equation(a), circle_equation(b), circle_equation(c) };\n\tequation[0] -= equation[1];\n\tequation[1] -= equation[2];\n\tequation[2] -= circle_equation(d);\n\tif (!solve(equation)) {\n\t\tthrow 0;\n\t}\n\tconst Point center{ equation[0].back(), equation[1].back(), equation[2].back() };\n\tconst auto radius = center.distance(a);\n\t//assert(std::abs(radius - center.distance(b)) < EPSILON);\n\t//assert(std::abs(radius - center.distance(c)) < EPSILON);\n\t//assert(std::abs(radius - center.distance(d)) < EPSILON);\n\treturn Circle{ center, radius };\n}\nCircle calc_circle(const std::vector<Point>& points) {\n\tswitch (points.size()) {\n\tcase 2: return calc_circle(points[0], points[1]);\n\tcase 3: return calc_circle(points[0], points[1], points[2]);\n\tcase 4: return calc_circle(points[0], points[1], points[2], points[3]);\n\tdefault: throw 0;\n\t}\n}\ndouble min_radius(const Point center, const std::vector<Point>& points) {\n\tdouble result = 0;\n\tfor (const auto &p : points) {\n\t\tresult = std::max(result, center.distance(p));\n\t}\n\treturn result;\n}\ndouble min_radius(const int current, const std::vector<Point>& points, std::vector<Point>& stack) {\n\tif (stack.size() == 4) {\n\t\treturn min_radius(calc_circle(stack).center, points);\n\t}\n\telse if (current == points.size()) {\n\t\treturn DBL_MAX;\n\t}\n\telse if (stack.size() >= 2) {\n\t\tdouble result = min_radius(calc_circle(stack).center, points);\n\t\tresult = std::min(result, min_radius(current + 1, points, stack));\n\t\tstack.push_back(points[current]);\n\t\tresult = std::min(result, min_radius(current + 1, points, stack));\n\t\tstack.pop_back();\n\t\treturn result;\n\t}\n\telse {\n\t\tdouble result = min_radius(current + 1, points, stack);\n\t\tstack.push_back(points[current]);\n\t\tresult = std::min(result, min_radius(current + 1, points, stack));\n\t\tstack.pop_back();\n\t\treturn result;\n\t}\n}\nint main() {\n\twhile (true) {\n\t\tint n; std::cin >> n;\n\t\tif (n == 0) break;\n\t\tstd::vector<Point> points(n);\n\t\tfor (auto& p : points) std::cin >> p.x >> p.y >> p.z;\n\t\tstd::vector<Point> stack;\n\t\tconst auto result = min_radius(0, points, stack);\n\t\tstd::cout << std::setprecision(5) << std::fixed << std::round(result * 100000) / 100000 << '\\n';\n\t}\n}\n\ndouble Point::distance(const Point& other) const\n{\n\treturn (*this - other).length();\n}", "accuracy": 1, "time_ms": 80, "memory_kb": 3396, "score_of_the_acc": -0.607, "final_rank": 11 }, { "submission_id": "aoj_1231_4761034", "code_snippet": "#include<iostream>\n#include<cmath>\n\nusing namespace std;\n\n#define S(X) ((X)*(X))\n\ndouble dsq(double x,double y,double z,double xx,double yy,double zz){\n return S(xx-x)+S(yy-y)+S(zz-z);\n}\n\nint main(){\n for(int n;cin>>n,n;){\n double x[30],y[30],z[30];\n for(int i=0;i<n;i++){\n cin>>x[i]>>y[i]>>z[i];\n }\n double cx=0,cy=0,cz=0;\n double f;\n for(double d=10;d>1e-9;d/=10){\n for(int i=0;i<1000;i++){\n\tint dx=0;\n\tfor(int j=0;j<n;j++){\n\t if(dsq(cx,cy,cz,x[dx],y[dx],z[dx])<dsq(cx,cy,cz,x[j],y[j],z[j])){\n\t dx=j;\n\t }\n\t}\n\tdouble bx,by,bz;\n\tbx=x[dx]-cx;\n\tby=y[dx]-cy;\n\tbz=z[dx]-cz;\n\tf=sqrt(S(bx)+S(by)+S(bz));\n\tcx+=bx*d/f;\n\tcy+=by*d/f;\n\tcz+=bz*d/f;\n }\n }\n cout.precision(5);\n cout<<fixed<<f<<endl;\n }\n return 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3220, "score_of_the_acc": -0.5402, "final_rank": 6 }, { "submission_id": "aoj_1231_4735073", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\n#define Fi first\n#define Se second\ntypedef long long ll;\ntypedef pair<int, int> pii;\ntypedef pair<ll, ll> pll;\n#define all(x) (x).begin(), (x).end()\n#define pb push_back\n#define szz(x) (int)(x).size()\n#define rep(i, n) for(int i=0;i<(n);i++)\ntypedef tuple<int, int, int> t3;\n\nusing T = double;\nint n;\nT x[110], y[110], z[110];\nT sq(T x) { return x * x; }\nT ssq(T a, T b, T c) { return sq(a) + sq(b) + sq(c); }\n\npair<int, T> far(T px, T py, T pz) {\n\tint r = -1;\n\tT f = -1;\n\tfor (int i = 1; i <= n; i++) {\n\t\tT dis = ssq(px - x[i], py - y[i], pz - z[i]);\n\t\tif (f < dis) f = dis, r = i;\n\t}\n\treturn { r, f };\n}\n\nint main() {\n\twhile (scanf(\"%d\", &n) == 1 && n) {\n\t\tfor (int i = 1; i <= n; i++) scanf(\"%lf%lf%lf\", x + i, y + i, z + i);\n\t\tauto g = [](T l, T r) {\n\t\t\treturn l + (r - l) * (T)0.5;\n\t\t};\n\t\tT ans = 1e9;\n\t\trep(a, 1) {\n\t\t\tT px = g(*min_element(x + 1, x + 1 + n), *max_element(x + 1, x + 1 + n));\n\t\t\tT py = g(*min_element(y + 1, y + 1 + n), *max_element(y + 1, y + 1 + n));\n\t\t\tT pz = g(*min_element(z + 1, z + 1 + n), *max_element(z + 1, z + 1 + n));\n\t\t\tT d = far(px, py, pz).Se * 0.1;\n\t\t\trep(t, 100000) {\n\t\t\t\tint i; T r;\n\t\t\t\ttie(i, r) = far(px, py, pz);\n\t\t\t\tT vx = x[i] - px, vy = y[i] - py, vz = z[i] - pz;\n\t\t\t\tT lp = d / sqrt(r);\n\t\t\t\tpx += vx * lp; py += vy * lp; pz += vz * lp;\n\t\t\t\td = d * 0.9995;\n\t\t\t}\n\t\t\tans = min(ans, sqrt(far(px, py, pz).Se));\n\t\t}\n\t\tprintf(\"%.5lf\\n\", ans);\n\t}\n\treturn 0;\n}", "accuracy": 1, "time_ms": 80, "memory_kb": 3180, "score_of_the_acc": -0.5521, "final_rank": 8 }, { "submission_id": "aoj_1231_4735070", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\n#define Fi first\n#define Se second\ntypedef long long ll;\ntypedef pair<int, int> pii;\ntypedef pair<ll, ll> pll;\n#define all(x) (x).begin(), (x).end()\n#define pb push_back\n#define szz(x) (int)(x).size()\n#define rep(i, n) for(int i=0;i<(n);i++)\ntypedef tuple<int, int, int> t3;\n\nusing T = long double;\nint n;\nT x[110], y[110], z[110];\nT sq(T x) { return x * x; }\nT ssq(T a, T b, T c) { return sq(a) + sq(b) + sq(c); }\n\npair<int, T> far(T px, T py, T pz) {\n\tint r = -1;\n\tT f = -1;\n\tfor (int i = 1; i <= n; i++) {\n\t\tT dis = ssq(px - x[i], py - y[i], pz - z[i]);\n\t\tif (f < dis) f = dis, r = i;\n\t}\n\treturn { r, f };\n}\n\nint main() {\n\twhile (scanf(\"%d\", &n) == 1 && n) {\n\t\tfor (int i = 1; i <= n; i++) scanf(\"%Lf%Lf%Lf\", x + i, y + i, z + i);\n\t\tauto g = [](T l, T r) {\n\t\t\treturn l + (r - l) * (T)0.5;\n\t\t};\n\t\tT ans = 1e9;\n\t\trep(a, 1) {\n\t\t\tT px = g(*min_element(x + 1, x + 1 + n), *max_element(x + 1, x + 1 + n));\n\t\t\tT py = g(*min_element(y + 1, y + 1 + n), *max_element(y + 1, y + 1 + n));\n\t\t\tT pz = g(*min_element(z + 1, z + 1 + n), *max_element(z + 1, z + 1 + n));\n\t\t\tT d = far(px, py, pz).Se * 0.1;\n\t\t\trep(t, 100000) {\n\t\t\t\tint i; T r;\n\t\t\t\ttie(i, r) = far(px, py, pz);\n\t\t\t\tT vx = x[i] - px, vy = y[i] - py, vz = z[i] - pz;\n\t\t\t\tT lp = d / sqrt(r);\n\t\t\t\tpx += vx * lp; py += vy * lp; pz += vz * lp;\n\t\t\t\td = d * 0.9995;\n\t\t\t}\n\t\t\tans = min(ans, sqrt(far(px, py, pz).Se));\n\t\t}\n\t\tprintf(\"%.5Lf\\n\", ans);\n\t}\n\treturn 0;\n}", "accuracy": 1, "time_ms": 120, "memory_kb": 3156, "score_of_the_acc": -0.5586, "final_rank": 9 }, { "submission_id": "aoj_1231_4735068", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\n#define Fi first\n#define Se second\ntypedef long long ll;\ntypedef pair<int, int> pii;\ntypedef pair<ll, ll> pll;\n#define all(x) (x).begin(), (x).end()\n#define pb push_back\n#define szz(x) (int)(x).size()\n#define rep(i, n) for(int i=0;i<(n);i++)\ntypedef tuple<int, int, int> t3;\n\nusing T = long double;\nint n;\nT x[110], y[110], z[110];\nT sq(T x) { return x * x; }\nT ssq(T a, T b, T c) { return sq(a) + sq(b) + sq(c); }\n\npair<int, T> far(T px, T py, T pz) {\n\tint r = -1;\n\tT f = -1;\n\tfor (int i = 1; i <= n; i++) {\n\t\tT dis = ssq(px - x[i], py - y[i], pz - z[i]);\n\t\tif (f < dis) f = dis, r = i;\n\t}\n\treturn { r, f };\n}\n\nint main() {\n\twhile (scanf(\"%d\", &n) == 1 && n) {\n\t\tfor (int i = 1; i <= n; i++) scanf(\"%Lf%Lf%Lf\", x + i, y + i, z + i);\n\t\tauto g = [](T l, T r) {\n\t\t\treturn l + (r - l) * ((T)rand() / RAND_MAX);\n\t\t};\n\t\tT ans = 1e9;\n\t\trep(a, 2) {\n\t\t\tT px = g(*min_element(x + 1, x + 1 + n), *max_element(x + 1, x + 1 + n));\n\t\t\tT py = g(*min_element(y + 1, y + 1 + n), *max_element(y + 1, y + 1 + n));\n\t\t\tT pz = g(*min_element(z + 1, z + 1 + n), *max_element(z + 1, z + 1 + n));\n\t\t\tT d = far(px, py, pz).Se * 0.1;\n\t\t\trep(t, 100000) {\n\t\t\t\tint i; T r;\n\t\t\t\ttie(i, r) = far(px, py, pz);\n\t\t\t\tT vx = x[i] - px, vy = y[i] - py, vz = z[i] - pz;\n\t\t\t\tT lp = d / sqrt(r);\n\t\t\t\tpx += vx * lp; py += vy * lp; pz += vz * lp;\n\t\t\t\td = d * 0.9995;\n\t\t\t}\n\t\t\tans = min(ans, sqrt(far(px, py, pz).Se));\n\t\t}\n\t\tprintf(\"%.5Lf\\n\", ans);\n\t}\n\treturn 0;\n}", "accuracy": 1, "time_ms": 220, "memory_kb": 3152, "score_of_the_acc": -0.5891, "final_rank": 10 }, { "submission_id": "aoj_1231_4735067", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\n#define Fi first\n#define Se second\ntypedef long long ll;\ntypedef pair<int, int> pii;\ntypedef pair<ll, ll> pll;\n#define all(x) (x).begin(), (x).end()\n#define pb push_back\n#define szz(x) (int)(x).size()\n#define rep(i, n) for(int i=0;i<(n);i++)\ntypedef tuple<int, int, int> t3;\n\nusing T = long double;\nint n;\nT x[110], y[110], z[110];\nT sq(T x) { return x * x; }\nT ssq(T a, T b, T c) { return sq(a) + sq(b) + sq(c); }\n\npair<int, T> far(T px, T py, T pz) {\n\tint r = -1;\n\tT f = -1;\n\tfor (int i = 1; i <= n; i++) {\n\t\tT dis = ssq(px - x[i], py - y[i], pz - z[i]);\n\t\tif (f < dis) f = dis, r = i;\n\t}\n\treturn { r, f };\n}\n\nint main() {\n\twhile (scanf(\"%d\", &n) == 1 && n) {\n\t\tfor (int i = 1; i <= n; i++) scanf(\"%Lf%Lf%Lf\", x + i, y + i, z + i);\n\t\tauto g = [](T l, T r) {\n\t\t\treturn l + (r - l) * ((T)rand() / RAND_MAX);\n\t\t};\n\t\tT ans = 1e9;\n\t\trep(a, 10) {\n\t\t\tT px = g(*min_element(x + 1, x + 1 + n), *max_element(x + 1, x + 1 + n));\n\t\t\tT py = g(*min_element(y + 1, y + 1 + n), *max_element(y + 1, y + 1 + n));\n\t\t\tT pz = g(*min_element(z + 1, z + 1 + n), *max_element(z + 1, z + 1 + n));\n\t\t\tT d = far(px, py, pz).Se * 0.1;\n\t\t\trep(t, 100000) {\n\t\t\t\tint i; T r;\n\t\t\t\ttie(i, r) = far(px, py, pz);\n\t\t\t\tT vx = x[i] - px, vy = y[i] - py, vz = z[i] - pz;\n\t\t\t\tT lp = d / sqrt(r);\n\t\t\t\tpx += vx * lp; py += vy * lp; pz += vz * lp;\n\t\t\t\td = d * 0.9995;\n\t\t\t}\n\t\t\tans = min(ans, sqrt(far(px, py, pz).Se));\n\t\t}\n\t\tprintf(\"%.5Lf\\n\", ans);\n\t}\n\treturn 0;\n}", "accuracy": 1, "time_ms": 1130, "memory_kb": 3236, "score_of_the_acc": -0.8976, "final_rank": 15 }, { "submission_id": "aoj_1231_3036599", "code_snippet": "#include <iostream>\n#include <iomanip>\n#include <vector>\n#include <algorithm>\n#include <cmath>\nusing namespace std;\nconst double EPS = 1e-6;\nconst double INF = 1e12;\n \nstruct Point{\n double x,y,z;\n Point(double x, double y, double z):x(x), y(y), z(z){}\n Point(){}\n Point &operator +=(const Point &a){ x+=a.x; y+=a.y; z+=a.z; return *this; }\n Point &operator -=(const Point &a){ x-=a.x; y-=a.y; z-=a.z; return *this; }\n Point &operator *=(const double &a){ x*=a; y*=a; z*=a; return *this; }\n Point operator +(const Point &a) const{ return Point(x+a.x, y+a.y, z+a.z); }\n Point operator -(const Point &a) const{ return Point(x-a.x, y-a.y, z-a.z); }\n Point operator *(const double &a) const{ return Point(a*x, a*y, a*z); }\n Point operator -() { return Point(-x, -y, -z); }\n};\n \ndouble dot(const Point &a, const Point &b){\n return a.x*b.x +a.y*b.y +a.z*b.z;\n}\nPoint cross(const Point &a, const Point &b){\n return Point(a.y*b.z -a.z*b.y, a.z*b.x -a.x*b.z, a.x*b.y -a.y*b.x);\n}\ndouble abs(const Point &a){\n return sqrt(dot(a, a));\n}\nPoint unit(const Point &a){\n return a *(1/abs(a));\n}\nbool operator ==(const Point &a, const Point &b){\n return abs(a-b) < EPS;\n}\n\nvector<double> solve_linear_equation(vector<vector<double> > a){\n int n = a.size();\n for(int i=0; i<n; i++){\n for(int j=i+1; j<n; j++){\n if(abs(a[j][i]) > abs(a[i][i])){\n swap(a[i], a[j]);\n }\n }\n for(int j=n; j>=i; j--){\n a[i][j] /= a[i][i];\n }\n for(int j=i+1; j<n; j++){\n for(int k=n; k>=i; k--){ \n a[j][k] -= a[i][k]*a[j][i];\n }\n }\n }\n vector<double> res(n);\n for(int i=n-1; i>=0; i--){\n res[i] = a[i][n];\n for(int j=n-1; j>i; j--){\n res[i] -= a[i][j]*res[j];\n }\n }\n return res;\n}\n\nint main(){\n while(1){\n int n;\n cin >> n;\n if(n==0) break;\n\n vector<Point> p(n);\n for(int i=0; i<n; i++){\n cin >> p[i].x >> p[i].y >> p[i].z;\n }\n\n vector<Point> cand;\n vector<double> r;\n Point c[4];\n for(int i=0; i<n; i++){\n c[0] = p[i];\n for(int j=i+1; j<n; j++){\n c[1] = p[j];\n for(int k=j+1; k<n; k++){\n c[2] = p[k];\n for(int l=k+1; l<n; l++){\n c[3] = p[l];\n //4点(連立方程式)\n vector<vector<double> > mat(3, vector<double>(4));\n for(int s=0; s<3; s++){\n mat[s][0] = 2*(c[s].x -c[s+1].x);\n mat[s][1] = 2*(c[s].y -c[s+1].y);\n mat[s][2] = 2*(c[s].z -c[s+1].z);\n mat[s][3] = (c[s].x *c[s].x -c[s+1].x *c[s+1].x)\n +(c[s].y *c[s].y -c[s+1].y *c[s+1].y)\n +(c[s].z *c[s].z -c[s+1].z *c[s+1].z);\n }\n vector<double> ret = solve_linear_equation(mat);\n double x = c[0].x -ret[0];\n double y = c[0].y -ret[1];\n double z = c[0].z -ret[2];\n cand.push_back(Point(ret[0], ret[1], ret[2]));\n r.push_back(sqrt(x*x +y*y +z*z));\n }\n //3点(外心のベクトル表示)\n double sin2[3];\n for(int i=0; i<3; i++){\n double A = abs(c[(i+1)%3] -c[(i+2)%3]);\n double B = abs(c[i] -c[(i+1)%3]);\n double C = abs(c[i] -c[(i+2)%3]);\n double cosa = (B*B +C*C -A*A)/(2*B*C);\n sin2[i] = sin(2*acos(cosa));\n }\n Point center = (c[0]*sin2[0] +c[1]*sin2[1] +c[2]*sin2[2])\n *(1/(sin2[0] +sin2[1] +sin2[2]));\n cand.push_back(center);\n r.push_back(abs(center -c[0]));\n }\n //2点(直径)\n cand.push_back((c[0]+c[1]) *0.5);\n r.push_back(abs(c[0]-c[1])/2);\n }\n }\n\n double ans = INF;\n for(int i=0; i<(int)cand.size(); i++){\n bool ok = true;\n for(int j=0; j<n; j++){\n if(abs(p[j] -cand[i]) > r[i] +EPS){\n ok = false;\n break;\n }\n }\n if(ok){\n ans = min(ans, r[i]);\n }\n }\n \n cout << fixed << setprecision(5);\n cout << ans << endl;\n }\n return 0;\n}", "accuracy": 1, "time_ms": 90, "memory_kb": 5028, "score_of_the_acc": -1.0252, "final_rank": 16 }, { "submission_id": "aoj_1231_2811407", "code_snippet": "#include<iostream>\n#include<cstring>\n#include<string>\n#include<cmath>\n#include<cstdio>\n#include<ctime>\n#include <algorithm>\nusing namespace std;\ntypedef long long ll;\nconst double PI = acos(-1.0);\nconst double eps = 1e-8;\nint n;\n/*\n机选取一个点作为初始解,然后不断向当前距离最远点靠近,这是一个不断调整的过程,\n将对应模拟淬火算法中不断向内能最低(半径最小)这一目标函数逼近,温度对应控制变量。\n*/\nstruct Point\n{\n\tdouble x,y,z;\n\tPoint(double x = 0, double y = 0, double z = 0)\n\t{\n\t\tx = x;\n\t\ty = y;\n\t\tz = z;\t\n\t}\n}p[1010];\ndouble dis(Point a, Point b)\n{\n\treturn sqrt((a.x - b.x)*(a.x - b.x) + (a.y - b.y)*(a.y - b.y)+(a.z-b.z)*(a.z - b.z) );\n}\n\nvoid solve()\n{\n\t/*\n\t不要用随机取点,取不到结果\n\t*/\n\tPoint temp = p[1];\n\tdouble ans = 9999999;\n\tdouble T = 100.0;\n\twhile( T > eps)//如果温度小于eps,则达到最优解\n\t{\n\t\tint k = 1;\n\t\tfor (int i = 1; i <= n; i++)\n\t\t{\n\t\t\tif (dis(temp, p[i]) > dis(temp, p[k]))\n\t\t\t\tk = i;//寻找距离temp最远的点\n\t\t}\n\t\tdouble dista = dis(temp, p[k]);\n\t\tans = min(ans, dista);//找出该点的最小距离\n\n\t\t\ttemp.x += (p[k].x - temp.x) / dista * T;\n\t\t\t//离temp最远的点与temp的x坐标差/两点最小距离*温度\n\t\t\ttemp.y += (p[k].y - temp.y) / dista * T;\n\t\t\ttemp.z += (p[k].z - temp.z) / dista * T;\n\t\tT *= 0.988;//降火的幅度\n\t}\n\tprintf(\"%.5f\\n\", ans);\n}\n\nint main()\n{\n\tstd::ios::sync_with_stdio(false);\n\twhile (cin>> n)\n\t{\n\t\t\n\t\tif (n == 0)break;\n\t\tmemset(p, 0, sizeof(p));\n\t\tfor (int i = 1; i <= n; i++)\n\t\t{\n\t\t\tcin >> p[i].x >> p[i].y>>p[i].z;\n\t\t}\n\t\tsolve();\n\t}\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3244, "score_of_the_acc": -0.5463, "final_rank": 7 }, { "submission_id": "aoj_1231_2804550", "code_snippet": "// Super Star Aizu - 1231 \n// status:\n// time:\n// main.cpp\n// Created by livin2 on 18/04/20.\n// Copyright © 2017年 livin2. All rights reserved.\n// Note:#\n//#include <cstdio>\n#include <iostream>\n#include <vector>\n#include <cmath>\n#include <algorithm>\n#include <iomanip>\n//#include <map>\n//#include <fstream>\nusing namespace std;\nconst int precision = 100000;\nconst double DZERO = 1e-9;\nconst double PI = acos(-1.0);\nconst int INF = 0x3f3f3f3f;\nstruct pos\n{\n\tdouble x, y,z;\n\tpos() {}\n\tpos(double x,double y,double z):x(x),y(y),z(z){}\n\tvoid moveRand(double step);\n};\ndouble len(const pos &a,const pos &b)\n{\n\treturn sqrt((a.x - b.x)*(a.x - b.x) + (a.y - b.y)*(a.y - b.y) + (a.z - b.z)*(a.z - b.z));\n}\ndouble Rand()\n{\n\treturn (double)(rand() % precision) / precision;\n}\n\nstruct SuperStar\n{\n\tint n;\n\t//double X, Y;\n\tdouble maxX = 100.0;\n\tdouble Dstep, ans;\n\tpos ansp;\n\tstatic const int pRandNum = 40;\n\tstatic const int stepRandNum = 20;\n\tconst double minDstep = 1e-6;\n\tconst double ratio = 0.98;\n\tvector<pos> P;\n\tvector<double> Rs;\n\tvector<pos> ansPs;\n\tSuperStar(int n);\n\tpos posRand();\n\tdouble getRadius(const pos &p);\n\tbool isInRange(const pos &p);\n\tvoid solve();\n};\nint main()\n{\n\tdouble x, y;int n;\n\twhile (cin>>n&&n)\n\t{\n\t\tSuperStar exp(n);\n\t\texp.solve();\n\t\tcout <<fixed<<setprecision(5)\n\t\t\t<<exp. ans << endl;\n\t}\n\treturn 0;\n}\n\nSuperStar::SuperStar(int n):n(n),Rs(pRandNum), ansPs(pRandNum)\n{\n\tdouble xx, yy,zz;\n\tfor (int i = 0; i < n; i++)\n\t{\n\t\tcin >> xx >> yy>>zz;\n\t\tP.push_back(pos(xx,yy,zz));\n\t}\n\tDstep = sqrt(2) * maxX / sqrt(n);//sqrt (2) *...\n}\nvoid pos::moveRand(double step)\n{\n\tdouble ang = Rand() * 2 * PI;\n\tz += step * sin(ang);\n\tdouble xy = step * cos(ang);\n\tang = Rand() * 2 * PI;\n\tx += xy * cos(ang);\n\ty += xy * sin(ang);\n\t\n}\npos SuperStar::posRand()\n{\n\tdouble x = maxX * Rand();\n\tdouble y = maxX * Rand();\n\tdouble z = maxX * Rand();\n\treturn pos(x,y,z);\n}\n\ndouble SuperStar::getRadius(const pos &p)\n{\n\tdouble res = 0;\n\tfor (int i = 0; i < P.size(); i++)\n\t\tres = max(res,len(P[i],p));\n\treturn res;\n}\n\nbool SuperStar::isInRange(const pos & p)\n{\n\tif (p.x > DZERO&&p.x - maxX <= DZERO && p.y > DZERO&&p.y - maxX <= DZERO && p.z > DZERO&&p.z - maxX <= DZERO)\n\t\treturn true;\n\treturn false;\n}\n\n\n\nvoid SuperStar::solve()\n{\n\t//init\n\tfor (int i = 0; i < pRandNum; i++)\n\t{\n\t\tansPs[i] = posRand();\n\t\tRs[i] = getRadius(ansPs[i]);\n\t}\n\t//SA\n\twhile (Dstep>minDstep)\n\t{\n\t\tfor (int i = 0; i < pRandNum; i++)\n\t\t{\n\t\t\tpos cur = ansPs[i];\n\t\t\tfor (int j = 0; j < stepRandNum; j++)\n\t\t\t{\n\t\t\t\tcur.moveRand(Dstep);\n\t\t\t\tif (isInRange(cur))\n\t\t\t\t{\n\t\t\t\t\tdouble rad = getRadius(cur);\n\t\t\t\t\tif (rad < Rs[i])\n\t\t\t\t\t{\n\t\t\t\t\t\tRs[i] = rad;\n\t\t\t\t\t\tansPs[i].x = cur.x;\n\t\t\t\t\t\tansPs[i].y = cur.y;\n\t\t\t\t\t\tansPs[i].z = cur.z;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\tDstep *= ratio;\n\t}\n\t//\n\tans = INF;\n\tansp = ansPs[0];\n\tfor (int i = 0; i < pRandNum; i++)\n\t{\n\t\tif (Rs[i] < ans)\n\t\t{\n\t\t\tans = Rs[i];\n\t\t\tansp = ansPs[i];\n\t\t}\n\t}\n}", "accuracy": 1, "time_ms": 2650, "memory_kb": 3544, "score_of_the_acc": -1.4554, "final_rank": 18 }, { "submission_id": "aoj_1231_2800311", "code_snippet": "#include <bits/stdc++.h>\n\nusing namespace std;\n\ntypedef long long ll;\nconst double EPS = 1e-15;\nconst double eps = 1e-15;\nconst int num = 15;\nconst double PI = acos(-1.0);\nconst double INF = 0x3f3f3f3f;\n\ndouble T = 100;\ndouble r = 0.99;\n\nstruct Point{\n double x,y,z;\n};\n\ndouble dist(Point A,Point B)\n{\n return sqrt((A.x-B.x)*(A.x-B.x)+(A.y-B.y)*(A.y-B.y)+(A.z-B.z)*(A.z-B.z));\n}\n\nPoint p[num],m[num];\nint n;\ndouble d[num];\ndouble ans;\n\ndouble MAX(Point t)\n{\n double res=0;\n for(int i=0;i<n;i++)\n res=max(res,dist(t,p[i]));\n ans=min(ans,res);\n return res;\n}\n\nint main()\n{\n srand(time(0));\n while(cin>>n)\n {\n if(n==0)\n break;\n for(int i=0;i<n;i++)\n cin>>p[i].x>>p[i].y>>p[i].z;\n double t=T;\n Point now,next;\n now.x=now.y=now.z=0;\n int pos;\n ans=1e100;\n while(t>EPS)\n {\n double dis=0;\n for(int i=0;i<n;i++)\n {\n if(dis<dist(now,p[i]))\n {\n pos=i;\n dis=dist(now,p[i]);\n }\n }\n next.x=now.x+(p[pos].x-now.x)/dis*t;\n next.y=now.y+(p[pos].y-now.y)/dis*t;\n next.z=now.z+(p[pos].z-now.z)/dis*t;\n double x=MAX(now)-MAX(next);\n if(x>0||exp(x/t)>(rand()%1000+1)/1000.0)\n now=next;\n t*=r;\n }\n printf(\"%.5f\\n\",ans);\n }\n return 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3744, "score_of_the_acc": -0.6734, "final_rank": 14 }, { "submission_id": "aoj_1231_2800154", "code_snippet": "#include <cmath>\n#include <cstdio>\n#include <cstring>\n#include <algorithm>\nusing namespace std;\ntypedef long long ll;\ninline void read(int &x){\n x=0;char ch;bool flag = false;\n while(ch=getchar(),ch<'!');if(ch == '-') ch=getchar(),flag = true;\n while(x=10*x+ch-'0',ch=getchar(),ch>'!');if(flag) x=-x;\n}\ninline int cat_max(const int &a,const int &b){return a>b ? a:b;}\ninline int cat_min(const int &a,const int &b){return a<b ? a:b;}\nconst int maxn = 45;\nconst double eps = 1e-15;\nconst double det = 0.99;\nstruct Point{\n double x,y,z;\n Point(const double &a=0,const double &b=0,const double &c=0){\n x=a;y=b;z=c;\n }\n};\ninline double dis(const Point &a,const Point &b){\n return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y)+(a.z-b.z)*(a.z-b.z));\n}\nint n;double ans = 1e100;\nPoint p[maxn];\ninline double f(const Point &x){\n double ret = 0;\n for(int i=1;i<=n;++i) ret = max(ret,dis(x,p[i]));\n if(ret < ans) ans = ret;\n return ret;\n}\ninline double ran(){\n return (rand() % 1000 + 1)/1000.0;\n}\nPoint nw;\nint main(){\n srand(2333);\n while(1){\n read(n);if(n == 0) break;\n nw.x = nw.y = nw.z = 0;\n ans = 1e100;\n for(int i=1;i<=n;++i){\n scanf(\"%lf%lf%lf\",&p[i].x,&p[i].y,&p[i].z);\n }\n double T = 100.0,x;\n double dist;int pos;\n while(T > eps){\n dist = 0.0;\n for(int i=1;i<=n;++i){\n if(dist < dis(nw,p[i])){\n pos = i;dist = dis(nw,p[i]);\n }\n }\n Point nx(\n nw.x+(p[pos].x-nw.x)/dist*T,\n nw.y+(p[pos].y-nw.y)/dist*T,\n nw.z+(p[pos].z-nw.z)/dist*T\n );\n x = f(nw) - f(nx);\n if(x > 0 || exp(x/T) > ran()) nw = nx;\n T *= det;\n }\n printf(\"%.5lf\\n\",ans);\n }\n getchar();getchar();\n return 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3120, "score_of_the_acc": -0.5148, "final_rank": 4 }, { "submission_id": "aoj_1231_2599272", "code_snippet": "#include <stdio.h>\n#include <cmath>\n#include <algorithm>\n#include <cfloat>\n#include <stack>\n#include <queue>\n#include <vector>\n#include <string>\n#include <iostream>\n#include <set>\n#include <map>\n#include <time.h>\ntypedef long long int ll;\ntypedef unsigned long long int ull;\n#define BIG_NUM 2000000000\n#define MOD 1000000007\n#define EPS 0.000000001\nusing namespace std;\n\nstruct Point{\n\tdouble x,y,z;\n};\n\nint N;\nPoint point[30];\n\ndouble calc_len(Point a,Point b){\n\treturn sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y)+(a.z-b.z)*(a.z-b.z));\n}\n\nvoid func(){\n\tfor(int i = 0; i < N; i++){\n\t\tscanf(\"%lf %lf %lf\",&point[i].x,&point[i].y,&point[i].z);\n\t}\n\n\tPoint tmp;\n\ttmp.x = 50.0,tmp.y = 50.0,tmp.z = 50.0;\n\tdouble ans = 10000.0,max_dist,move_dist = 10.0;\n\tint max_index;\n\n\tfor(int i = 0; i < 10000; i++){\n\t\tmax_dist = -1;\n\n\t\tfor(int k = 0; k < N; k++){\n\t\t\tif(max_dist < calc_len(tmp,point[k])){\n\t\t\t\tmax_dist = calc_len(tmp,point[k]);\n\t\t\t\tmax_index = k;\n\t\t\t}\n\t\t}\n\n\t\tif(ans > max_dist){\n\t\t\tans = max_dist;\n\t\t}\n\n\t\ttmp.x += (point[max_index].x-tmp.x)*move_dist;\n\t\ttmp.y += (point[max_index].y-tmp.y)*move_dist;\n\t\ttmp.z += (point[max_index].z-tmp.z)*move_dist;\n\n\t\tif(i%100 == 0)move_dist /= 2;\n\t}\n\tprintf(\"%.10lf\\n\",ans);\n}\n\n\nint main(){\n\twhile(true){\n\t\tscanf(\"%d\",&N);\n\t\tif(N == 0)break;\n\n\t\tfunc();\n\t}\n\treturn 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3184, "score_of_the_acc": -0.531, "final_rank": 5 }, { "submission_id": "aoj_1231_2183635", "code_snippet": "#include <string>\n#include <iomanip>\n#include <iostream>\n#include <algorithm>\nusing namespace std;\nint n; double x[35], y[35], z[35];\ndouble solve(double px, double py, double pz) {\n\tdouble ret = 0.0;\n\tfor (int i = 0; i < n; i++) ret = max(ret, (x[i] - px) * (x[i] - px) + (y[i] - py) * (y[i] - py) + (z[i] - pz) * (z[i] - pz));\n\treturn sqrt(ret);\n}\ndouble solve1(double px, double py) {\n\tdouble l = -1.0e+3, r = 1.0e+3;\n\tfor (int i = 0; i < 70; i++) {\n\t\tdouble m1 = (l * 2.0 + r) / 3.0;\n\t\tdouble m2 = (l + r * 2.0) / 3.0;\n\t\tif (solve(px, py, m1) < solve(px, py, m2)) r = m2;\n\t\telse l = m1;\n\t}\n\treturn solve(px, py, l);\n}\ndouble solve2(double px) {\n\tdouble l = -1.0e+3, r = 1.0e+3;\n\tfor (int i = 0; i < 70; i++) {\n\t\tdouble m1 = (l * 2.0 + r) / 3.0;\n\t\tdouble m2 = (l + r * 2.0) / 3.0;\n\t\tif (solve1(px, m1) < solve1(px, m2)) r = m2;\n\t\telse l = m1;\n\t}\n\treturn solve1(px, l);\n}\ndouble solve3() {\n\tdouble l = -1.0e+3, r = 1.0e+3;\n\tfor (int i = 0; i < 70; i++) {\n\t\tdouble m1 = (l * 2.0 + r) / 3.0;\n\t\tdouble m2 = (l + r * 2.0) / 3.0;\n\t\tif (solve2(m1) < solve2(m2)) r = m2;\n\t\telse l = m1;\n\t}\n\treturn solve2(l);\n}\nint main() {\n\twhile (cin >> n, n) {\n\t\tfor (int i = 0; i < n; i++) cin >> x[i] >> y[i] >> z[i];\n\t\tdouble ret = solve3();\n\t\tcout << fixed << setprecision(10) << ret << endl;\n\t}\n\treturn 0;\n}", "accuracy": 1, "time_ms": 1720, "memory_kb": 3268, "score_of_the_acc": -1.0918, "final_rank": 17 }, { "submission_id": "aoj_1231_2179647", "code_snippet": "#include<iostream>\n#include<vector>\n#include<algorithm>\nusing namespace std;\nint n; long double x[30], y[30], z[30];\nlong double norm(long double ax, long double ay, long double az) {\n\tlong double maxn = 0;\n\tfor (int i = 0; i < n; i++) {\n\t\tmaxn = max(maxn, (ax - x[i])*(ax - x[i]) + (ay - y[i])*(ay - y[i]) + (az - z[i])*(az - z[i]));\n\t}\n\treturn maxn;\n}\nlong double solve(int depth, long double L1, long double L2) {\n\tlong double L = -114.514l, R = 100.0l, c1, c2, maxn = 1e9;\n\tfor (int i = 0; i < 70; i++) {\n\t\tc1 = (L + L + R) / 3.0l;\n\t\tc2 = (L + R + R) / 3.0l;\n\t\tlong double I1, I2;\n\t\tif (depth == 0) { I1 = solve(depth + 1, c1, L2); I2 = solve(depth + 1, c2, L2); }\n\t\tif (depth == 1) { I1 = solve(depth + 1, L1, c1); I2 = solve(depth + 1, L1, c2); }\n\t\tif (depth == 2) { I1 = norm(L1, L2, c1); I2 = norm(L1, L2, c2); }\n\t\tif (I1 < I2)R = c2; else L = c1;\n\t\tmaxn = min(maxn, min(I1, I2));\n\t}\n\treturn maxn;\n}\nint main() {\n\twhile (true) {\n\t\tcin >> n; if (n == 0)break; for (int i = 0; i < n; i++)cin >> x[i] >> y[i] >> z[i];\n\t\tprintf(\"%.12Lf\\n\", sqrtl(solve(0, 0, 0)));\n\t}\n\treturn 0;\n}", "accuracy": 1, "time_ms": 3180, "memory_kb": 3136, "score_of_the_acc": -1.5188, "final_rank": 19 }, { "submission_id": "aoj_1231_1198719", "code_snippet": "#include<cstdio>\n#include<cstdlib>\n#include<cstring>\n#include<cmath>\n#include<iostream>\n#include<string>\n#include<vector>\n#include<map>\n#include<list>\n#include<queue>\n#include<deque>\n#include<algorithm>\n#include<numeric>\n#include<utility>\n#include<complex>\n#include<functional>\n \nusing namespace std;\n\n/* constant */\n\n#define MAX_N\t(30)\n#define MAX_CAN\t(10000)\n\n#define DELTA\t((double)1e-6)\n\n/* typedef */\n\ntypedef vector<int> vi;\ntypedef vector<long long> vl;\ntypedef vector<double> vd;\ntypedef pair<int,int> pii;\ntypedef pair<long,long> pll;\ntypedef long long ll;\n\nstruct can_t {\n double r, x, y, z;\n};\n\nstruct pt_t {\n double x, y, z;\n};\n\n/* classes */\n\nclass LessCan {\npublic:\n bool operator()(const can_t& lc, const can_t& rc) const {\n return lc.r < rc.r;\n }\n};\n\n/* global variables */\n\nstatic pt_t signs[] = {\n { 1, 1, 1}, { 1, 1, -1}, { 1, -1, 1}, { 1, -1, -1},\n {-1, 1, 1}, {-1, 1, -1}, {-1, -1, 1}, {-1, -1, -1}\n};\n\nstatic int n;\nstatic pt_t pts[MAX_N];\n\nstatic can_t candidate[MAX_CAN], new_candidate[MAX_CAN];\n\n/* subroutines */\n\ndouble max_r(double x, double y, double z) {\n double max_r2 = 0.0;\n\n for (int i = 0; i < n; i++) {\n pt_t& p = pts[i];\n double dx = x - p.x;\n double dy = y - p.y;\n double dz = z - p.z;\n double r2 = dx * dx + dy * dy + dz * dz;\n if (max_r2 < r2) max_r2 = r2;\n }\n\n return sqrt(max_r2);\n}\n\ndouble solve_by_partition() {\n candidate[0].x = 50.0;\n candidate[0].y = 50.0;\n candidate[0].z = 50.0;\n\n int count = 1;\n double size = 100.0;\n double min_sofar = 1e50;\n double root3 = sqrt(3.0);\n\n while (size > DELTA) {\n size /= 2;\n int k = 0;\n\n for (int i = 0; i < count; i++) {\n for (int d = 0; d < 8; d++) {\n can_t c = candidate[i];\n double sizeh = size / 2;\n c.x += signs[d].x * sizeh;\n c.y += signs[d].y * sizeh;\n c.z += signs[d].z * sizeh;\n c.r = max_r(c.x, c.y, c.z);\n if (min_sofar > c.r) min_sofar = c.r;\n if (c.r > min_sofar + sizeh * root3) continue;\n if (k >= MAX_CAN) break;\n\n new_candidate[k] = c;\n k++;\n }\n if (k >= MAX_CAN) break;\n }\n\n count = k;\n for (int i = 0; i < count; i++)\n candidate[i] = new_candidate[i];\n\n sort(candidate, candidate + count, LessCan());\n }\n\n return min_sofar;\n}\n\n/* main */\n\nint main() {\n for (;;) {\n cin >> n;\n if (n == 0) break;\n\n for (int i = 0; i < n; i++) {\n cin >> pts[i].x >> pts[i].y >> pts[i].z;\n }\n\n printf(\"%.5lf\\n\", solve_by_partition());\n }\n \n return 0;\n}", "accuracy": 1, "time_ms": 170, "memory_kb": 1888, "score_of_the_acc": -0.2519, "final_rank": 3 }, { "submission_id": "aoj_1231_1159493", "code_snippet": "#include <bits/stdc++.h>\n\nusing namespace std;\n\n#define MAX 30\n#define EPS 1e-9\n\nclass Point{\npublic:\n double x,y,z;\n Point(){}\n Point(double x,double y,double z) : x(x),y(y),z(z) {}\n};\n\ndouble getDistance(const Point &a,const Point &b){\n return sqrt(pow(a.x-b.x,2)+pow(a.y-b.y,2)+pow(a.z-b.z,2));\n}\n\nistream &operator >> (istream &is,Point &p){\n is >> p.x >> p.y >> p.z;\n return is;\n}\n\nint main(){\n int N;\n Point point[MAX];\n while(cin >> N, N){\n double x = 0, y = 0, z = 0;\n for(int i = 0 ; i < N ; i++){\n cin >> point[i];\n x += point[i].x;\n y += point[i].y;\n z += point[i].z;\n }\n x /= N; y /= N; z /= N;\n int pos = -1;\n double d = 0.8, ans = 0;\n Point p(x,y,z);\n while(d > EPS){\n for(int i = 0 ; i < 150 ; i++){\n ans = 0;\n for(int j = 0 ; j < N ; j++){\n double dist = getDistance(p,point[j]);\n if(ans < dist){\n ans = dist;\n pos = j;\n }\n }\n p.x += (point[pos].x-p.x)*d;\n p.y += (point[pos].y-p.y)*d;\n p.z += (point[pos].z-p.z)*d;\n }\n d /= 2;\n }\n printf(\"%.6f\\n\",ans);\n }\n return 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 1248, "score_of_the_acc": -0.0387, "final_rank": 2 }, { "submission_id": "aoj_1231_1131007", "code_snippet": "#include<stdio.h>\n#include<algorithm>\n#include<math.h>\nusing namespace std;\ndouble x[32];\ndouble y[32];\ndouble z[32];\nint main(){\n\tint a;\n\twhile(scanf(\"%d\",&a),a){\n\t\tfor(int i=0;i<a;i++)scanf(\"%lf%lf%lf\",x+i,y+i,z+i);\n\t\tdouble nx=50;\n\t\tdouble ny=50;\n\t\tdouble nz=50;\n\t\tdouble res=9999999;\n\t\tdouble mv=10;\n\t\tfor(int i=0;i<10000;i++){\n\t\t\tdouble r=0;\n\t\t\tint at=0;\n\t\t\tfor(int j=0;j<a;j++){\n\t\t\t\tif(r<sqrt((x[j]-nx)*(x[j]-nx)+(y[j]-ny)*(y[j]-ny)+(z[j]-nz)*(z[j]-nz))){\n\t\t\t\t\tat=j;\n\t\t\t\t\tr=sqrt((x[j]-nx)*(x[j]-nx)+(y[j]-ny)*(y[j]-ny)+(z[j]-nz)*(z[j]-nz));\n\t\t\t\t}\n\t\t\t}\n\t\t\tif(r<res){\n\t\t\t\tres=r;\n\t\t\t}\n\t\t\tnx+=(x[at]-nx)*mv;\n\t\t\tny+=(y[at]-ny)*mv;\n\t\t\tnz+=(z[at]-nz)*mv;\n\t\t\t\n\t\t\tif(i%100==0)mv/=2;\n\t\t}\n\t\tprintf(\"%.5f\\n\",res);\n\t}\n}", "accuracy": 1, "time_ms": 30, "memory_kb": 1096, "score_of_the_acc": -0.0063, "final_rank": 1 } ]
aoj_1246_cpp
Problem G: Concert Hall Scheduling You are appointed director of a famous concert hall, to save it from bankruptcy. The hall is very popular, and receives many requests to use its two fine rooms, but unfortunately the previous director was not very efficient, and it has been losing money for many years. The two rooms are of the same size and arrangement. Therefore, each applicant wishing to hold a concert asks for a room without specifying which. Each room can be used for only one concert per day. In order to make more money, you have decided to abandon the previous fixed price policy, and rather let applicants specify the price they are ready to pay. Each application shall specify a period [ i , j ] and an asking price w , where i and j are respectively the first and last days of the period (1 ≤ i ≤ j ≤ 365), and w is a positive integer in yen, indicating the amount the applicant is willing to pay to use a room for the whole period. You have received applications for the next year, and you should now choose the applications you will accept. Each application should be either accepted for its whole period or completely rejected. Each concert should use the same room during the whole applied period. Considering the dire economic situation of the concert hall, artistic quality is to be ignored, and you should just try to maximize the total income for the whole year by accepting the most profitable applications. Input The input has multiple data sets, each starting with a line consisting of a single integer n, the number of applications in the data set. Then, it is followed by n lines, each of which represents one application with a period [ i , j ] and an asking price w yen in the following format. i j w A line containing a single zero indicates the end of the input. The maximum number of applications in a data set is one thousand, and the maximum asking price is one million yen. Output For each data set, print a single line containing an integer, the maximum total income in yen for the data set. Sample Input 4 1 2 10 2 3 10 3 3 10 1 3 10 6 1 20 1000 3 25 10000 5 15 5000 22 300 5500 10 295 9000 7 7 6000 8 32 251 2261 123 281 1339 211 235 5641 162 217 7273 22 139 7851 194 198 9190 119 274 878 122 173 8640 0 Output for the Sample Input 30 25500 38595
[ { "submission_id": "aoj_1246_9706976", "code_snippet": "#include <bits/stdc++.h>\n\nusing namespace std;\n\n#define rep(i, a, b) for (int i = (int)(a); i < (int)(b); i++)\n#define rrep(i, a, b) for (int i = (int)(b)-1; i >= (int)(a); i--)\n#define ALL(v) (v).begin(), (v).end()\n#define UNIQUE(v) sort(ALL(v)), (v).erase(unique(ALL(v)), (v).end())\n#define SZ(v) (int)v.size()\n#define MIN(v) *min_element(ALL(v))\n#define MAX(v) *max_element(ALL(v))\n#define LB(v, x) int(lower_bound(ALL(v), (x)) - (v).begin())\n#define UB(v, x) int(upper_bound(ALL(v), (x)) - (v).begin())\n\nusing uint = unsigned int;\nusing ll = long long int;\nusing ull = unsigned long long;\nusing i128 = __int128_t;\nusing u128 = __uint128_t;\nconst int inf = 0x3fffffff;\nconst ll INF = 0x1fffffffffffffff;\n\ntemplate <typename T> inline bool chmax(T &a, T b) {\n if (a < b) {\n a = b;\n return 1;\n }\n return 0;\n}\ntemplate <typename T> inline bool chmin(T &a, T b) {\n if (a > b) {\n a = b;\n return 1;\n }\n return 0;\n}\ntemplate <typename T, typename U> T ceil(T x, U y) {\n assert(y != 0);\n if (y < 0)\n x = -x, y = -y;\n return (x > 0 ? (x + y - 1) / y : x / y);\n}\ntemplate <typename T, typename U> T floor(T x, U y) {\n assert(y != 0);\n if (y < 0)\n x = -x, y = -y;\n return (x > 0 ? x / y : (x - y + 1) / y);\n}\ntemplate <typename T> int popcnt(T x) {\n return __builtin_popcountll(x);\n}\ntemplate <typename T> int topbit(T x) {\n return (x == 0 ? -1 : 63 - __builtin_clzll(x));\n}\ntemplate <typename T> int lowbit(T x) {\n return (x == 0 ? -1 : __builtin_ctzll(x));\n}\n\ntemplate <class T, class U>\nostream &operator<<(ostream &os, const pair<T, U> &p) {\n os << \"P(\" << p.first << \", \" << p.second << \")\";\n return os;\n}\ntemplate <typename T> ostream &operator<<(ostream &os, const vector<T> &vec) {\n os << \"{\";\n for (int i = 0; i < vec.size(); i++) {\n os << vec[i] << (i + 1 == vec.size() ? \"\" : \", \");\n }\n os << \"}\";\n return os;\n}\ntemplate <typename T, typename U>\nostream &operator<<(ostream &os, const map<T, U> &map_var) {\n os << \"{\";\n for (auto itr = map_var.begin(); itr != map_var.end(); itr++) {\n os << \"(\" << itr->first << \", \" << itr->second << \")\";\n itr++;\n if (itr != map_var.end())\n os << \", \";\n itr--;\n }\n os << \"}\";\n return os;\n}\ntemplate <typename T> ostream &operator<<(ostream &os, const set<T> &set_var) {\n os << \"{\";\n for (auto itr = set_var.begin(); itr != set_var.end(); itr++) {\n os << *itr;\n ++itr;\n if (itr != set_var.end())\n os << \", \";\n itr--;\n }\n os << \"}\";\n return os;\n}\n#ifdef LOCAL\n#define show(...) _show(0, #__VA_ARGS__, __VA_ARGS__)\n#else\n#define show(...) true\n#endif\ntemplate <typename T> void _show(int i, T name) {\n cerr << '\\n';\n}\ntemplate <typename T1, typename T2, typename... T3>\nvoid _show(int i, const T1 &a, const T2 &b, const T3 &...c) {\n for (; a[i] != ',' && a[i] != '\\0'; i++)\n cerr << a[i];\n cerr << \":\" << b << \" \";\n _show(i + 1, a, c...);\n}\n\n/**\n * @brief template\n */\n\nint main() {\nwhile(1) {\n int K = 365;\n int N;\n cin >> N;\n if (N == 0) return 0;\n vector<int> L(N), R(N), C(N);\n rep(i,0,N) cin >> L[i] >> R[i] >> C[i], L[i]--;\n vector<int> ord(N);\n iota(ALL(ord),0);\n sort(ALL(ord),[&](int i, int j){return R[i]<R[j];});\n vector<vector<int>> DP(K+1,vector<int>(K+1,0));\n for (int i : ord) {\n vector<vector<int>> NewDP(K+1,vector<int>(K+1,0));\n rep(j,0,K+1) chmax(NewDP[j][R[i]],DP[j][L[i]]+C[i]);\n rep(j,0,K+1) chmax(NewDP[R[i]][j],DP[L[i]][j]+C[i]);\n rep(j,0,K+1) rep(k,0,K+1) chmax(DP[j][k],NewDP[j][k]);\n rep(j,0,K+1) {\n rep(k,0,K+1) {\n if (j+1<=K) chmax(DP[j+1][k],DP[j][k]);\n if (k+1<=K) chmax(DP[j][k+1],DP[j][k]);\n }\n }\n }\n cout << DP[K][K] << endl;\n}\n}", "accuracy": 1, "time_ms": 990, "memory_kb": 4148, "score_of_the_acc": -0.6903, "final_rank": 14 }, { "submission_id": "aoj_1246_9697470", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\n\nvoid solve(int N) {\n vector<pair<pair<ll,ll>,ll>> P(N);\n for(int i=0;i<N;i++)cin>>P[i].first.first>>P[i].first.second>>P[i].second;\n sort(P.begin(),P.end());\n vector<vector<ll>> DP(366,vector<ll>(366,0));\n for(int i=0;i<N;i++){\n auto NDP=DP;\n auto [L,R]=P[i].first;\n for(int n=0;n<=365;n++){\n for(int j=0;j<=365;j++){\n if(n<L){\n DP[R][j]=max(DP[R][j],NDP[n][j]+P[i].second);\n }\n if(j<L){\n DP[n][R]=max(DP[n][R],NDP[n][j]+P[i].second);\n }\n }\n }\n for(int n=0;n<=365;n++){\n for(int j=0;j<=365;j++){\n if(n!=365)DP[n+1][j]=max(DP[n+1][j],DP[n][j]);\n if(j!=365)DP[n][j+1]=max(DP[n][j+1],DP[n][j]);\n }\n }\n }\n cout<<DP[365][365]<<endl;\n}\n\nint main() {\n cin.tie(nullptr);\n ios::sync_with_stdio(false);\n\n int T;\n while (cin>>T,T!=0)solve(T);\n}", "accuracy": 1, "time_ms": 1880, "memory_kb": 5272, "score_of_the_acc": -1.4982, "final_rank": 20 }, { "submission_id": "aoj_1246_6771570", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\n#ifdef _RUTHEN\n#include \"debug.hpp\"\n#else\n#define show(...) true\n#endif\n\nusing ll = long long;\n#define rep(i, n) for (int i = 0; i < (n); i++)\ntemplate <class T> using V = vector<T>;\n\nvoid solve(int N) {\n V<tuple<int, int, int>> LRW(N);\n rep(i, N) {\n int l, r, w;\n cin >> l >> r >> w;\n l--;\n LRW[i] = {l, r, w};\n }\n sort(LRW.begin(), LRW.end());\n int M = 365;\n V<V<ll>> dp(M + 1, V<ll>(M + 1, 0));\n // 蟻本p221のグラフを2次元に拡張し、DAGの最長路を計算\n // ->これだと同じ区間を2回使ってしまう(例えばsample1で[1,2][3,3]を2回ずつ使って40など)\n // ->3次元に拡張\n // 同一の高さでは平面的な遷移(np[i1 + 1][i2] = max(np[i1 + 1][i2], np[i1][i2])など)\n // 高さが+1するところで区間を使った遷移をする(これによって同じ区間を使うことはなくなる)\n // このとき、解を復元することを考えると、区間は始点ソートしておけば、解として選ばれた任意の同一ホールでの区間について\n // 後日の区間がより前の日の区間より先に来ることはないので、最適解を求められる\n rep(i, N) {\n auto [l, r, w] = LRW[i];\n V<V<ll>> np = dp;\n rep(i1, M + 1) {\n rep(i2, M + 1) {\n if (i1 == l) {\n np[r][i2] = max(np[r][i2], dp[i1][i2] + w);\n }\n if (i2 == l) {\n np[i1][r] = max(np[i1][r], dp[i1][i2] + w);\n }\n if (i1 + 1 <= M) np[i1 + 1][i2] = max(np[i1 + 1][i2], np[i1][i2]);\n if (i2 + 1 <= M) np[i1][i2 + 1] = max(np[i1][i2 + 1], np[i1][i2]);\n }\n }\n dp = np;\n }\n cout << dp[M][M] << '\\n';\n return;\n}\n\nint main() {\n ios::sync_with_stdio(false);\n cin.tie(0);\n int N;\n while (cin >> N, N != 0) solve(N);\n return 0;\n}", "accuracy": 1, "time_ms": 1590, "memory_kb": 5156, "score_of_the_acc": -1.3083, "final_rank": 18 }, { "submission_id": "aoj_1246_6771562", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\n#ifdef _RUTHEN\n#include \"debug.hpp\"\n#else\n#define show(...) true\n#endif\n\nusing ll = long long;\n#define rep(i, n) for (int i = 0; i < (n); i++)\ntemplate <class T> using V = vector<T>;\n\nvoid solve(int N) {\n V<tuple<int, int, int>> LRW(N);\n rep(i, N) {\n int l, r, w;\n cin >> l >> r >> w;\n l--;\n LRW[i] = {l, r, w};\n }\n sort(LRW.begin(), LRW.end());\n show(LRW);\n int M = 365;\n V<V<ll>> dp(M + 1, V<ll>(M + 1, 0));\n rep(i, N) {\n auto [l, r, w] = LRW[i];\n V<V<ll>> np = dp;\n rep(i1, M + 1) {\n rep(i2, M + 1) {\n if (i1 == l) {\n np[r][i2] = max(np[r][i2], dp[i1][i2] + w);\n }\n if (i2 == l) {\n np[i1][r] = max(np[i1][r], dp[i1][i2] + w);\n }\n if (i1 + 1 <= M) np[i1 + 1][i2] = max(np[i1 + 1][i2], np[i1][i2]);\n if (i2 + 1 <= M) np[i1][i2 + 1] = max(np[i1][i2 + 1], np[i1][i2]);\n }\n }\n dp = np;\n }\n cout << dp[M][M] << '\\n';\n return;\n}\n\nint main() {\n ios::sync_with_stdio(false);\n cin.tie(0);\n int N;\n while (cin >> N, N != 0) solve(N);\n return 0;\n}", "accuracy": 1, "time_ms": 1610, "memory_kb": 5120, "score_of_the_acc": -1.3085, "final_rank": 19 }, { "submission_id": "aoj_1246_6771478", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\n#ifdef _RUTHEN\n#include \"debug.hpp\"\n#else\n#define show(...) true\n#endif\n\nusing ll = long long;\n#define rep(i, n) for (int i = 0; i < (n); i++)\ntemplate <class T> using V = vector<T>;\n\n\n#include <algorithm>\n#include <cassert>\n#include <limits>\n#include <queue>\n#include <vector>\n\n\n#include <algorithm>\n#include <utility>\n#include <vector>\n\nnamespace atcoder {\nnamespace internal {\n\ntemplate <class E> struct csr {\n std::vector<int> start;\n std::vector<E> elist;\n explicit csr(int n, const std::vector<std::pair<int, E>>& edges)\n : start(n + 1), elist(edges.size()) {\n for (auto e : edges) {\n start[e.first + 1]++;\n }\n for (int i = 1; i <= n; i++) {\n start[i] += start[i - 1];\n }\n auto counter = start;\n for (auto e : edges) {\n elist[counter[e.first]++] = e.second;\n }\n }\n};\n\n} // namespace internal\n\n} // namespace atcoder\n\n\n#include <vector>\n\nnamespace atcoder {\n\nnamespace internal {\n\ntemplate <class T> struct simple_queue {\n std::vector<T> payload;\n int pos = 0;\n void reserve(int n) { payload.reserve(n); }\n int size() const { return int(payload.size()) - pos; }\n bool empty() const { return pos == int(payload.size()); }\n void push(const T& t) { payload.push_back(t); }\n T& front() { return payload[pos]; }\n void clear() {\n payload.clear();\n pos = 0;\n }\n void pop() { pos++; }\n};\n\n} // namespace internal\n\n} // namespace atcoder\n\n\nnamespace atcoder {\n\ntemplate <class Cap, class Cost> struct mcf_graph {\n public:\n mcf_graph() {}\n explicit mcf_graph(int n) : _n(n) {}\n\n int add_edge(int from, int to, Cap cap, Cost cost) {\n assert(0 <= from && from < _n);\n assert(0 <= to && to < _n);\n assert(0 <= cap);\n assert(0 <= cost);\n int m = int(_edges.size());\n _edges.push_back({from, to, cap, 0, cost});\n return m;\n }\n\n struct edge {\n int from, to;\n Cap cap, flow;\n Cost cost;\n };\n\n edge get_edge(int i) {\n int m = int(_edges.size());\n assert(0 <= i && i < m);\n return _edges[i];\n }\n std::vector<edge> edges() { return _edges; }\n\n std::pair<Cap, Cost> flow(int s, int t) {\n return flow(s, t, std::numeric_limits<Cap>::max());\n }\n std::pair<Cap, Cost> flow(int s, int t, Cap flow_limit) {\n return slope(s, t, flow_limit).back();\n }\n std::vector<std::pair<Cap, Cost>> slope(int s, int t) {\n return slope(s, t, std::numeric_limits<Cap>::max());\n }\n std::vector<std::pair<Cap, Cost>> slope(int s, int t, Cap flow_limit) {\n assert(0 <= s && s < _n);\n assert(0 <= t && t < _n);\n assert(s != t);\n\n int m = int(_edges.size());\n std::vector<int> edge_idx(m);\n\n auto g = [&]() {\n std::vector<int> degree(_n), redge_idx(m);\n std::vector<std::pair<int, _edge>> elist;\n elist.reserve(2 * m);\n for (int i = 0; i < m; i++) {\n auto e = _edges[i];\n edge_idx[i] = degree[e.from]++;\n redge_idx[i] = degree[e.to]++;\n elist.push_back({e.from, {e.to, -1, e.cap - e.flow, e.cost}});\n elist.push_back({e.to, {e.from, -1, e.flow, -e.cost}});\n }\n auto _g = internal::csr<_edge>(_n, elist);\n for (int i = 0; i < m; i++) {\n auto e = _edges[i];\n edge_idx[i] += _g.start[e.from];\n redge_idx[i] += _g.start[e.to];\n _g.elist[edge_idx[i]].rev = redge_idx[i];\n _g.elist[redge_idx[i]].rev = edge_idx[i];\n }\n return _g;\n }();\n\n auto result = slope(g, s, t, flow_limit);\n\n for (int i = 0; i < m; i++) {\n auto e = g.elist[edge_idx[i]];\n _edges[i].flow = _edges[i].cap - e.cap;\n }\n\n return result;\n }\n\n private:\n int _n;\n std::vector<edge> _edges;\n\n struct _edge {\n int to, rev;\n Cap cap;\n Cost cost;\n };\n\n std::vector<std::pair<Cap, Cost>> slope(internal::csr<_edge>& g,\n int s,\n int t,\n Cap flow_limit) {\n\n std::vector<std::pair<Cost, Cost>> dual_dist(_n);\n std::vector<int> prev_e(_n);\n std::vector<bool> vis(_n);\n struct Q {\n Cost key;\n int to;\n bool operator<(Q r) const { return key > r.key; }\n };\n std::vector<int> que_min;\n std::vector<Q> que;\n auto dual_ref = [&]() {\n for (int i = 0; i < _n; i++) {\n dual_dist[i].second = std::numeric_limits<Cost>::max();\n }\n std::fill(vis.begin(), vis.end(), false);\n que_min.clear();\n que.clear();\n\n size_t heap_r = 0;\n\n dual_dist[s].second = 0;\n que_min.push_back(s);\n while (!que_min.empty() || !que.empty()) {\n int v;\n if (!que_min.empty()) {\n v = que_min.back();\n que_min.pop_back();\n } else {\n while (heap_r < que.size()) {\n heap_r++;\n std::push_heap(que.begin(), que.begin() + heap_r);\n }\n v = que.front().to;\n std::pop_heap(que.begin(), que.end());\n que.pop_back();\n heap_r--;\n }\n if (vis[v]) continue;\n vis[v] = true;\n if (v == t) break;\n Cost dual_v = dual_dist[v].first, dist_v = dual_dist[v].second;\n for (int i = g.start[v]; i < g.start[v + 1]; i++) {\n auto e = g.elist[i];\n if (!e.cap) continue;\n Cost cost = e.cost - dual_dist[e.to].first + dual_v;\n if (dual_dist[e.to].second - dist_v > cost) {\n Cost dist_to = dist_v + cost;\n dual_dist[e.to].second = dist_to;\n prev_e[e.to] = e.rev;\n if (dist_to == dist_v) {\n que_min.push_back(e.to);\n } else {\n que.push_back(Q{dist_to, e.to});\n }\n }\n }\n }\n if (!vis[t]) {\n return false;\n }\n\n for (int v = 0; v < _n; v++) {\n if (!vis[v]) continue;\n dual_dist[v].first -= dual_dist[t].second - dual_dist[v].second;\n }\n return true;\n };\n Cap flow = 0;\n Cost cost = 0, prev_cost_per_flow = -1;\n std::vector<std::pair<Cap, Cost>> result = {{Cap(0), Cost(0)}};\n while (flow < flow_limit) {\n if (!dual_ref()) break;\n Cap c = flow_limit - flow;\n for (int v = t; v != s; v = g.elist[prev_e[v]].to) {\n c = std::min(c, g.elist[g.elist[prev_e[v]].rev].cap);\n }\n for (int v = t; v != s; v = g.elist[prev_e[v]].to) {\n auto& e = g.elist[prev_e[v]];\n e.cap += c;\n g.elist[e.rev].cap -= c;\n }\n Cost d = -dual_dist[s].first;\n flow += c;\n cost += c * d;\n if (prev_cost_per_flow == d) {\n result.pop_back();\n }\n result.push_back({flow, cost});\n prev_cost_per_flow = d;\n }\n return result;\n }\n};\n\n} // namespace atcoder\n\nusing namespace atcoder;\n\nvoid solve(int N) {\n V<int> L(N), R(N), W(N);\n rep(i, N) {\n cin >> L[i] >> R[i] >> W[i];\n L[i]--;\n }\n int K = 2;\n int M = 366;\n mcf_graph<int, ll> G(M + 2);\n ll res = 0;\n int s = M, t = s + 1;\n G.add_edge(s, 0, K, 0);\n G.add_edge(M - 1, t, K, 0);\n rep(i, M - 1) G.add_edge(i, i + 1, K, 0);\n rep(i, N) {\n G.add_edge(R[i], L[i], 1, W[i]);\n G.add_edge(s, R[i], 1, 0);\n G.add_edge(L[i], t, 1, 0);\n res -= W[i];\n }\n res += G.flow(s, t, K + N).second;\n cout << -res << '\\n';\n return;\n}\n\nint main() {\n ios::sync_with_stdio(false);\n cin.tie(0);\n int N;\n while (cin >> N, N != 0) solve(N);\n return 0;\n}", "accuracy": 1, "time_ms": 20, "memory_kb": 3628, "score_of_the_acc": -0.0164, "final_rank": 1 }, { "submission_id": "aoj_1246_6771471", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\n#ifdef _RUTHEN\n#include \"debug.hpp\"\n#else\n#define show(...) true\n#endif\n\nusing ll = long long;\n#define rep(i, n) for (int i = 0; i < (n); i++)\ntemplate <class T> using V = vector<T>;\n\n\n#include <algorithm>\n#include <cassert>\n#include <limits>\n#include <queue>\n#include <vector>\n\n\n#include <algorithm>\n#include <utility>\n#include <vector>\n\nnamespace atcoder {\nnamespace internal {\n\ntemplate <class E> struct csr {\n std::vector<int> start;\n std::vector<E> elist;\n explicit csr(int n, const std::vector<std::pair<int, E>>& edges)\n : start(n + 1), elist(edges.size()) {\n for (auto e : edges) {\n start[e.first + 1]++;\n }\n for (int i = 1; i <= n; i++) {\n start[i] += start[i - 1];\n }\n auto counter = start;\n for (auto e : edges) {\n elist[counter[e.first]++] = e.second;\n }\n }\n};\n\n} // namespace internal\n\n} // namespace atcoder\n\n\n#include <vector>\n\nnamespace atcoder {\n\nnamespace internal {\n\ntemplate <class T> struct simple_queue {\n std::vector<T> payload;\n int pos = 0;\n void reserve(int n) { payload.reserve(n); }\n int size() const { return int(payload.size()) - pos; }\n bool empty() const { return pos == int(payload.size()); }\n void push(const T& t) { payload.push_back(t); }\n T& front() { return payload[pos]; }\n void clear() {\n payload.clear();\n pos = 0;\n }\n void pop() { pos++; }\n};\n\n} // namespace internal\n\n} // namespace atcoder\n\n\nnamespace atcoder {\n\ntemplate <class Cap, class Cost> struct mcf_graph {\n public:\n mcf_graph() {}\n explicit mcf_graph(int n) : _n(n) {}\n\n int add_edge(int from, int to, Cap cap, Cost cost) {\n assert(0 <= from && from < _n);\n assert(0 <= to && to < _n);\n assert(0 <= cap);\n assert(0 <= cost);\n int m = int(_edges.size());\n _edges.push_back({from, to, cap, 0, cost});\n return m;\n }\n\n struct edge {\n int from, to;\n Cap cap, flow;\n Cost cost;\n };\n\n edge get_edge(int i) {\n int m = int(_edges.size());\n assert(0 <= i && i < m);\n return _edges[i];\n }\n std::vector<edge> edges() { return _edges; }\n\n std::pair<Cap, Cost> flow(int s, int t) {\n return flow(s, t, std::numeric_limits<Cap>::max());\n }\n std::pair<Cap, Cost> flow(int s, int t, Cap flow_limit) {\n return slope(s, t, flow_limit).back();\n }\n std::vector<std::pair<Cap, Cost>> slope(int s, int t) {\n return slope(s, t, std::numeric_limits<Cap>::max());\n }\n std::vector<std::pair<Cap, Cost>> slope(int s, int t, Cap flow_limit) {\n assert(0 <= s && s < _n);\n assert(0 <= t && t < _n);\n assert(s != t);\n\n int m = int(_edges.size());\n std::vector<int> edge_idx(m);\n\n auto g = [&]() {\n std::vector<int> degree(_n), redge_idx(m);\n std::vector<std::pair<int, _edge>> elist;\n elist.reserve(2 * m);\n for (int i = 0; i < m; i++) {\n auto e = _edges[i];\n edge_idx[i] = degree[e.from]++;\n redge_idx[i] = degree[e.to]++;\n elist.push_back({e.from, {e.to, -1, e.cap - e.flow, e.cost}});\n elist.push_back({e.to, {e.from, -1, e.flow, -e.cost}});\n }\n auto _g = internal::csr<_edge>(_n, elist);\n for (int i = 0; i < m; i++) {\n auto e = _edges[i];\n edge_idx[i] += _g.start[e.from];\n redge_idx[i] += _g.start[e.to];\n _g.elist[edge_idx[i]].rev = redge_idx[i];\n _g.elist[redge_idx[i]].rev = edge_idx[i];\n }\n return _g;\n }();\n\n auto result = slope(g, s, t, flow_limit);\n\n for (int i = 0; i < m; i++) {\n auto e = g.elist[edge_idx[i]];\n _edges[i].flow = _edges[i].cap - e.cap;\n }\n\n return result;\n }\n\n private:\n int _n;\n std::vector<edge> _edges;\n\n struct _edge {\n int to, rev;\n Cap cap;\n Cost cost;\n };\n\n std::vector<std::pair<Cap, Cost>> slope(internal::csr<_edge>& g,\n int s,\n int t,\n Cap flow_limit) {\n\n std::vector<std::pair<Cost, Cost>> dual_dist(_n);\n std::vector<int> prev_e(_n);\n std::vector<bool> vis(_n);\n struct Q {\n Cost key;\n int to;\n bool operator<(Q r) const { return key > r.key; }\n };\n std::vector<int> que_min;\n std::vector<Q> que;\n auto dual_ref = [&]() {\n for (int i = 0; i < _n; i++) {\n dual_dist[i].second = std::numeric_limits<Cost>::max();\n }\n std::fill(vis.begin(), vis.end(), false);\n que_min.clear();\n que.clear();\n\n size_t heap_r = 0;\n\n dual_dist[s].second = 0;\n que_min.push_back(s);\n while (!que_min.empty() || !que.empty()) {\n int v;\n if (!que_min.empty()) {\n v = que_min.back();\n que_min.pop_back();\n } else {\n while (heap_r < que.size()) {\n heap_r++;\n std::push_heap(que.begin(), que.begin() + heap_r);\n }\n v = que.front().to;\n std::pop_heap(que.begin(), que.end());\n que.pop_back();\n heap_r--;\n }\n if (vis[v]) continue;\n vis[v] = true;\n if (v == t) break;\n Cost dual_v = dual_dist[v].first, dist_v = dual_dist[v].second;\n for (int i = g.start[v]; i < g.start[v + 1]; i++) {\n auto e = g.elist[i];\n if (!e.cap) continue;\n Cost cost = e.cost - dual_dist[e.to].first + dual_v;\n if (dual_dist[e.to].second - dist_v > cost) {\n Cost dist_to = dist_v + cost;\n dual_dist[e.to].second = dist_to;\n prev_e[e.to] = e.rev;\n if (dist_to == dist_v) {\n que_min.push_back(e.to);\n } else {\n que.push_back(Q{dist_to, e.to});\n }\n }\n }\n }\n if (!vis[t]) {\n return false;\n }\n\n for (int v = 0; v < _n; v++) {\n if (!vis[v]) continue;\n dual_dist[v].first -= dual_dist[t].second - dual_dist[v].second;\n }\n return true;\n };\n Cap flow = 0;\n Cost cost = 0, prev_cost_per_flow = -1;\n std::vector<std::pair<Cap, Cost>> result = {{Cap(0), Cost(0)}};\n while (flow < flow_limit) {\n if (!dual_ref()) break;\n Cap c = flow_limit - flow;\n for (int v = t; v != s; v = g.elist[prev_e[v]].to) {\n c = std::min(c, g.elist[g.elist[prev_e[v]].rev].cap);\n }\n for (int v = t; v != s; v = g.elist[prev_e[v]].to) {\n auto& e = g.elist[prev_e[v]];\n e.cap += c;\n g.elist[e.rev].cap -= c;\n }\n Cost d = -dual_dist[s].first;\n flow += c;\n cost += c * d;\n if (prev_cost_per_flow == d) {\n result.pop_back();\n }\n result.push_back({flow, cost});\n prev_cost_per_flow = d;\n }\n return result;\n }\n};\n\n} // namespace atcoder\n\nusing namespace atcoder;\n\nvoid solve(int N) {\n V<int> L(N), R(N), W(N);\n rep(i, N) {\n cin >> L[i] >> R[i] >> W[i];\n L[i]--;\n }\n int K = 2;\n int M = 366;\n mcf_graph<int, ll> G(M + 2);\n ll res = 0;\n int s = M, t = s + 1;\n G.add_edge(s, 0, 2, 0);\n G.add_edge(M - 1, t, 2, 0);\n rep(i, M - 1) G.add_edge(i, i + 1, 2, 0);\n rep(i, N) {\n G.add_edge(R[i], L[i], 1, W[i]);\n G.add_edge(s, R[i], 1, 0);\n G.add_edge(L[i], t, 1, 0);\n res -= W[i];\n }\n res += G.flow(s, t, K + N).second;\n cout << -res << '\\n';\n return;\n}\n\nint main() {\n ios::sync_with_stdio(false);\n cin.tie(0);\n int N;\n while (cin >> N, N != 0) solve(N);\n return 0;\n}", "accuracy": 1, "time_ms": 20, "memory_kb": 3696, "score_of_the_acc": -0.0363, "final_rank": 2 }, { "submission_id": "aoj_1246_6770880", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n// #pragma GCC target(\"arch=skylake-avx512\")\n// #include <atcoder/all>\n// using namespace atcoder;\n// #define NDEBUG\n\n#pragma region template\n// Define\nusing ll = long long;\nusing ull = unsigned long long;\nusing ld = long double;\ntemplate <class T> using pvector = vector<pair<T, T>>;\ntemplate <class T> using rpriority_queue = priority_queue<T, vector<T>, greater<T>>;\nconstexpr const ll dx[4] = {1, 0, -1, 0};\nconstexpr const ll dy[4] = {0, 1, 0, -1};\nconstexpr const ll MOD = 1e9 + 7;\nconstexpr const ll mod = 998244353;\nconstexpr const ll INF = 1LL << 60;\nconstexpr const ll inf = 1 << 30;\nconstexpr const char rt = '\\n';\nconstexpr const char sp = ' ';\n#define rt(i, n) (i == (ll) (n) -1 ? rt : sp)\n#define len(x) ((ll) (x).size())\n#define all(x) (x).begin(), (x).end()\n#define rall(x) (x).rbegin(), (x).rend()\n#define mp make_pair\n#define mt make_tuple\n#define pb push_back\n#define eb emplace_back\n#define ifn(x) if (not(x))\n#define elif else if\n#define elifn else ifn\n#define fi first\n#define se second\n#define uniq(x) (sort(all(x)), (x).erase(unique(all(x)), (x).end()))\n#define bis(x, y) ((ll) (lower_bound(all(x), y) - (x).begin()))\n\nusing graph = vector<vector<ll>>;\ntemplate <class T> using wgraph = vector<vector<pair<ll, T>>>;\nbool __DIRECTED__ = true;\nbool __ZERO_INDEXED__ = false;\nistream &operator>>(istream &is, graph &g) {\n ll a, b;\n is >> a >> b;\n if (__ZERO_INDEXED__ == false) a--, b--;\n g[a].pb(b);\n if (__DIRECTED__ == false) g[b].pb(a);\n return is;\n}\ntemplate <class T> istream &operator>>(istream &is, wgraph<T> &g) {\n ll a, b;\n T c;\n is >> a >> b >> c;\n if (__ZERO_INDEXED__ == false) a--, b--;\n g[a].pb({b, c});\n if (__DIRECTED__ == false) g[b].pb({a, c});\n return is;\n}\n\ntemplate <class T> bool chmax(T &a, const T &b) {\n if (a < b) {\n a = b;\n return 1;\n }\n return 0;\n}\ntemplate <class T> bool chmin(T &a, const T &b) {\n if (a > b) {\n a = b;\n return 1;\n }\n return 0;\n}\n\n// Debug\n#ifdef NDEBUG\n#define debug(...)\n#define dumpi(a, h, w)\n#define vdumpi(a, n)\n#define dump(a, h, w)\n#define vdump(a, n)\n#else\n#define debug(...) \\\n { \\\n cerr << __LINE__ << \": \" << #__VA_ARGS__ << \" = \"; \\\n for (auto &&__i : {__VA_ARGS__}) cerr << \"[\" << __i << \"] \"; \\\n cerr << rt; \\\n }\n\n#define dumpi(a, h, w) \\\n { \\\n cerr << __LINE__ << \": \" << #a << \" = [\" << rt; \\\n rep(__i, h) { \\\n if (__i) cerr << \",\\n\"; \\\n cerr << \"[\"; \\\n rep(__j, w) { \\\n if (__j) cerr << \", \"; \\\n if (abs(a[__i][__j]) >= INF / 2 and a[__i][__j] <= -INF / 2) cerr << '-'; \\\n if (abs(a[__i][__j]) >= INF / 2) cerr << \"∞\"; \\\n else \\\n cerr << a[__i][__j]; \\\n } \\\n cerr << \"]\"; \\\n } \\\n cerr << \"\\n]\" << rt; \\\n }\n\n#define vdumpi(a, n) \\\n { \\\n cerr << __LINE__ << \": \" << #a << \" = [\"; \\\n rep(__i, n) { \\\n if (__i) cerr << \", \"; \\\n if (abs(a[__i]) >= INF / 2 and a[__i] <= -INF / 2) cerr << '-'; \\\n if (abs(a[__i]) >= INF / 2) cerr << \"∞\"; \\\n else \\\n cerr << a[__i]; \\\n } \\\n cerr << \"]\" << rt; \\\n }\n\n#define dump(a, h, w) \\\n { \\\n cerr << __LINE__ << \": \" << #a << \" = [\" << rt; \\\n rep(__i, h) { \\\n if (__i) cerr << \",\\n\"; \\\n cerr << \"[\"; \\\n rep(__j, w) { \\\n if (__j) cerr << \", \"; \\\n cerr << a[__i][__j]; \\\n } \\\n cerr << \"]\"; \\\n } \\\n cerr << \"\\n]\" << rt; \\\n }\n\n#define vdump(a, n) \\\n { \\\n cerr << __LINE__ << \": \" << #a << \" = [\"; \\\n rep(__i, n) { \\\n if (__i) cerr << \", \"; \\\n cerr << a[__i]; \\\n } \\\n cerr << \"]\" << rt; \\\n }\n#endif\n\ntemplate <class S, class T> istream &operator>>(istream &is, pair<S, T> &p) {\n is >> p.first >> p.second;\n return is;\n}\ntemplate <class S, class T> ostream &operator<<(ostream &os, const pair<S, T> &p) {\n os << p.first << ' ' << p.second;\n return os;\n}\n\n// Loop\n#define inc(i, a, n) for (ll i = (a), _##i = (n); i <= _##i; ++i)\n#define dec(i, a, n) for (ll i = (a), _##i = (n); i >= _##i; --i)\n#define rep(i, n) for (ll i = 0, _##i = (n); i < _##i; ++i)\n#define each(i, a) for (auto &&i : a)\n\n// Stream\n#define fout(n) cout << fixed << setprecision(n)\nstruct io {\n io() { cin.tie(nullptr), ios::sync_with_stdio(false); }\n} io;\n\n// Speed\n#pragma GCC target(\"sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,tune=native\")\n#pragma GCC optimize(\"Ofast,unroll-loops\")\n\n// Math\ninline constexpr ll gcd(const ll a, const ll b) { return b ? gcd(b, a % b) : a; }\ninline constexpr ll lcm(const ll a, const ll b) { return a / gcd(a, b) * b; }\n\ninline constexpr ll modulo(const ll n, const ll m = MOD) {\n ll k = n % m;\n return k + m * (k < 0);\n}\ninline constexpr ll chmod(ll &n, const ll m = MOD) {\n n %= m;\n return n += m * (n < 0);\n}\ninline constexpr ll mpow(ll a, ll n, const ll m = MOD) {\n ll r = 1;\n rep(i, 64) {\n if (n & (1LL << i)) r *= a;\n chmod(r, m);\n a *= a;\n chmod(a, m);\n }\n return r;\n}\ninline ll inv(const ll n, const ll m = MOD) {\n ll a = n, b = m, x = 1, y = 0;\n while (b) {\n ll t = a / b;\n a -= t * b;\n swap(a, b);\n x -= t * y;\n swap(x, y);\n }\n return modulo(x, m);\n}\nunsigned long long binary_gcd(unsigned long long x, unsigned long long y) {\n if (!x | !y) return x | y;\n unsigned long long cx = __builtin_ctzll(x), cy = __builtin_ctzll(y);\n x >>= cx, y >>= cy;\n while (x ^ y) {\n if (x > y) {\n x = (x - y) >> __builtin_ctzll(x ^ y);\n } else {\n y = (y - x) >> __builtin_ctzll(x ^ y);\n }\n }\n return x << min(cx, cy);\n}\n\ninline long long binary_gcd(long long x, long long y) {\n return binary_gcd((unsigned long long) (abs(x)), (unsigned long long) (abs(y)));\n}\n\n#define codeforces \\\n ll testcases; \\\n cin >> testcases; \\\n rep(testcase, testcases)\n#define gcj(s) cout << s << testcase + 1 << \": \"\n\n#pragma endregion\n\n/**\n * @brief Primal Dual(最小費用流)\n * @docs docs/primal-dual.md\n */\ntemplate <typename flow_t, typename cost_t> struct PrimalDual {\n struct edge {\n int to;\n flow_t cap;\n cost_t cost;\n int rev;\n bool isrev;\n };\n\n vector<vector<edge>> graph;\n vector<cost_t> potential, min_cost;\n vector<int> prevv, preve;\n const cost_t INF;\n\n PrimalDual(int V) : graph(V), INF(numeric_limits<cost_t>::max()) {}\n\n void add_edge(int from, int to, flow_t cap, cost_t cost) {\n graph[from].emplace_back((edge){to, cap, cost, (int) graph[to].size(), false});\n graph[to].emplace_back((edge){from, 0, -cost, (int) graph[from].size() - 1, true});\n }\n\n cost_t min_cost_flow(int s, int t, flow_t f) {\n int V = (int) graph.size();\n cost_t ret = 0;\n using Pi = pair<cost_t, int>;\n priority_queue<Pi, vector<Pi>, greater<Pi>> que;\n potential.assign(V, 0);\n preve.assign(V, -1);\n prevv.assign(V, -1);\n\n while (f > 0) {\n min_cost.assign(V, INF);\n que.emplace(0, s);\n min_cost[s] = 0;\n while (!que.empty()) {\n Pi p = que.top();\n que.pop();\n if (min_cost[p.second] < p.first) continue;\n for (int i = 0; i < (int) graph[p.second].size(); i++) {\n edge &e = graph[p.second][i];\n cost_t nextCost =\n min_cost[p.second] + e.cost + potential[p.second] - potential[e.to];\n if (e.cap > 0 && min_cost[e.to] > nextCost) {\n min_cost[e.to] = nextCost;\n prevv[e.to] = p.second, preve[e.to] = i;\n que.emplace(min_cost[e.to], e.to);\n }\n }\n }\n if (min_cost[t] == INF) return -1;\n for (int v = 0; v < V; v++) potential[v] += min_cost[v];\n flow_t addflow = f;\n for (int v = t; v != s; v = prevv[v]) {\n addflow = min(addflow, graph[prevv[v]][preve[v]].cap);\n }\n f -= addflow;\n ret += addflow * potential[t];\n for (int v = t; v != s; v = prevv[v]) {\n edge &e = graph[prevv[v]][preve[v]];\n e.cap -= addflow;\n graph[v][e.rev].cap += addflow;\n }\n }\n return ret;\n }\n\n void output() {\n for (int i = 0; i < graph.size(); i++) {\n for (auto &e : graph[i]) {\n if (e.isrev) continue;\n auto &rev_e = graph[e.to][e.rev];\n cout << i << \"->\" << e.to << \" (flow: \" << rev_e.cap << \"/\" << rev_e.cap + e.cap\n << \")\" << endl;\n }\n }\n }\n};\n\nsigned main() {\n ll n;\n while (cin >> n, n) {\n vector<ll> x(n), y(n), z(n), a;\n rep(i, n) cin >> x[i] >> y[i] >> z[i], x[i]--, a.pb(x[i]), a.pb(y[i]), a.pb(z[i]);\n sort(all(a));\n a.erase(unique(all(a)), a.end());\n ll m = a.size();\n PrimalDual<ll, ll> pd(m);\n rep(i, m - 1) pd.add_edge(i, i + 1, 100000, 0);\n rep(i, n) {\n ll xi = lower_bound(all(a), x[i]) - a.begin();\n ll yi = lower_bound(all(a), y[i]) - a.begin();\n assert(xi < yi);\n pd.add_edge(xi, yi, 1, -z[i]);\n }\n ll ans = pd.min_cost_flow(0, m - 1, 2);\n cout << -ans << rt;\n }\n}", "accuracy": 1, "time_ms": 20, "memory_kb": 3752, "score_of_the_acc": -0.0528, "final_rank": 3 }, { "submission_id": "aoj_1246_6379081", "code_snippet": "#include<bits/stdc++.h>\n#define rep(i,a,...) for(int i = (a)*(strlen(#__VA_ARGS__)!=0);i<(int)(strlen(#__VA_ARGS__)?__VA_ARGS__:(a));++i)\n#define per(i,a,...) for(int i = (strlen(#__VA_ARGS__)?__VA_ARGS__:(a))-1;i>=(int)(strlen(#__VA_ARGS__)?(a):0);--i)\n#define foreach(i, n) for(auto &i:(n))\n#define all(x) (x).begin(), (x).end()\n#define bit(x) (1ll << (x))\n#define lambda(RES_TYPE, ...) (function<RES_TYPE(__VA_ARGS__)>)[&](__VA_ARGS__) -> RES_TYPE\n#define method(FUNC_NAME, RES_TYPE, ...) function<RES_TYPE(__VA_ARGS__)> FUNC_NAME = lambda(RES_TYPE, __VA_ARGS__)\nusing namespace std;\nusing ll = long long;\nusing pii = pair<int,int>;\nusing pll = pair<ll,ll>;\n//const ll MOD = (ll)1e9+7;\nconst ll MOD = 998244353;\nconst int INF = (ll)1e9+7;\nconst ll INFLL = (ll)1e18;\ntemplate<class t>\nusing vvector = vector<vector<t>>;\ntemplate<class t>\nusing vvvector = vector<vector<vector<t>>>;\ntemplate<class t>\nusing priority_queuer = priority_queue<t, vector<t>, greater<t>>;\ntemplate<class t, class u> bool chmax(t &a, u b){if(a<b){a=b;return true;}return false;}\ntemplate<class t, class u> bool chmin(t &a, u b){if(a>b){a=b;return true;}return false;}\n#ifdef DEBUG\n#define debug(x) cout<<\"LINE \"<<__LINE__<<\": \"<<#x<<\" = \"<<x<<endl;\n#else\n#define debug(x) (void)0\n#endif\n\nnamespace templates{\n ll modpow(ll x, ll b,ll mod=MOD){\n ll res = 1;\n while(b){\n if(b&1)res = res * x % mod;\n x = x * x % mod;\n b>>=1;\n }\n return res;\n }\n\n ll modinv(ll x){\n return modpow(x, MOD-2);\n }\n\n bool was_output = false;\n\n void print();\n template <class t> void print(const vector<t> &);\n template <class t, class u> void print(const pair<t, u> &);\n template <class t> void print(const t&);\n template <class Head, class... Tail> void print(const Head&, const Tail&...);\n\n template <class t> void println(const vector<vector<t>>&);\n template <class t> void println(const vector<t>&);\n template <class t> void println(const t&);\n template <class Head, class... Tail> void println(const Head&, const Tail&...);\n void println();\n void newline();\n\n void print(){\n return;\n }\n\n template <class t>\n void print(const vector<t>&x){\n for(const t&i:x)print(i);\n }\n template <class t, class u>\n void print(const pair<t,u>&p){\n print(p.first);\n print(p.second);\n }\n template <class t>\n void print(const t&x){\n if(was_output)cout<<\" \";\n cout<<x;\n was_output = true;\n }\n template <class Head, class... Tail>\n void print(const Head&head,const Tail&...tail){\n print(head);\n print(tail...);\n }\n\n template<class t>\n void println(const vector<vector<t>>&x){\n for(vector<t> i:x)println(i);\n }\n template<class t>\n void println(const vector<t>&x){\n for(const t&i:x)print(i);\n println();\n }\n template <class t>\n void println(const t&x){\n print(x);\n println();\n }\n void println(){\n if(was_output){\n cout << endl;\n was_output = false;\n }\n }\n template <class Head, class... Tail>\n void println(const Head&head,const Tail&...tail){\n print(head);\n println(tail...);\n }\n\n void newline(){\n was_output = true;\n println();\n }\n\n template<class t>\n istream& operator>>(istream&is, vector<t>&x){\n for(auto &i:x)is >> i;\n return is;\n }\n\n template<class t, class u>\n istream& operator>>(istream&is, pair<t, u>&x){\n is >> x.first >> x.second;\n return is;\n }\n\n template<class t>\n ostream& operator<<(ostream&os, vector<t> &v){\n os << \"{\";\n for(t &i:v){\n os << i << \", \";\n }\n os << \"}\";\n return os;\n }\n\n template<class t = long long>\n t in(){\n t res; cin >> res; return res;\n }\n\n template<class t>\n vector<t> sorted(vector<t> line,function<bool(t,t)> comp=[](t a,t b){return a<b;}){\n sort(line.begin(),line.end(),comp);\n return line;\n }\n\n template<class t>\n vector<t> reversed(vector<t> line){\n reverse(line.begin(),line.end());\n return line;\n }\n string reversed(string str){\n reverse(str.begin(),str.end());\n return str;\n }\n\n long long gcd(long long a,long long b){\n while(b){\n a %= b;\n swap(a,b);\n }\n return a;\n }\n\n long long lcm(long long a,long long b){\n return a / gcd(a,b) * b;\n }\n\n class output_initializer{\n public:\n output_initializer(){\n ios::sync_with_stdio(false);\n cin.tie(nullptr);\n cout << setprecision(20);\n }\n };output_initializer OUTPUT_INITIALIZER_INSTANCE = output_initializer();\n}\n\nusing namespace templates;\n\nclass bit2d{\npublic:\n int h;\n int w;\n vvector<int> table;\n bit2d(int h,int w,int init=0):h(h),w(w),table(h,vector<int>(w,init)){}\n\n void add(int y,int x,int v){\n for(int i=y+1;i<h;i+=(i&-i)){\n for(int j=x+1;j<w;j+=(j&-j)){\n table[i][j] = max(table[i][j],v);\n }\n }\n }\n int sum(int y,int x){\n int res = 0;\n for(int i=y;i>0;i-=(i&-i)){\n for(int j=x;j>0;j-=(j&-j)){\n res = max(res,table[i][j]);\n }\n }\n return res;\n }\n};\n\nll func(int n){\n bit2d bi(400,400);\n struct record{\n int from;\n int to;\n int cost;\n record(){}\n record(int from,int to,int cost):from(from),to(to),cost(cost){}\n };\n vector<record> records(n);\n foreach(i,records){\n i.from = in();\n i.to = in();\n i.cost = in();\n }\n sort(all(records),lambda(bool,record a,record b){return a.from < b.from;});\n foreach(rec,records){\n per(i,366){\n bi.add(rec.to,i,bi.sum(rec.from,i+1)+rec.cost);\n bi.add(i,rec.to,bi.sum(i+1,rec.from)+rec.cost);\n }\n }\n return bi.sum(366,366);\n}\n\nint main(){\n ios::sync_with_stdio(false);\n cin.tie(nullptr);\n while(true){\n int n = in();\n if(n==0)break;\n println(func(n));\n }\n return 0;\n}", "accuracy": 1, "time_ms": 60, "memory_kb": 3840, "score_of_the_acc": -0.1001, "final_rank": 4 }, { "submission_id": "aoj_1246_6355087", "code_snippet": "#include <cstdio>\n#include <vector>\n#include <queue>\n#include <set>\n#include <map>\n#include <algorithm>\nusing namespace std;\n\nint main() {\nint n;\nwhile (scanf(\"%d\", &n) == 1) {\n if (n == 0) break;\n int m = 365;\n vector<int> l(n), r(n), c(n);\n for (int i = 0; i < n; i++) {\n scanf(\"%d%d%d\", &l[i], &r[i], &c[i]);\n }\n vector<int> seq(n);\n for (int i = 0; i < n; i++) seq[i] = i;\n sort(seq.begin(), seq.end(), [&](auto u, auto v) {\n return r[u] < r[v];\n });\n vector<vector<int>> f(m + 1, vector<int> (m + 1, 0));\n // f[0][0] = 0;\n int ans = 0;\n for (int g = 0; g < n; g++) {\n int it = seq[g];\n vector<vector<int>> nf(m + 1, vector<int> (m + 1, 0));\n // printf(\"it = %d\\n\", it);\n for (int i = 0; i <= m; i++) {\n for (int j = 0; j <= m; j++) {\n if (i) f[i][j] = max(f[i][j], f[i - 1][j]);\n if (j) f[i][j] = max(f[i][j], f[i][j - 1]);\n nf[i][j] = f[i][j];\n if (i == r[it]) nf[i][j] = max(nf[i][j], f[l[it] - 1][j] + c[it]);\n if (j == r[it]) nf[i][j] = max(nf[i][j], f[i][l[it] - 1] + c[it]);\n // printf(\"f[%d][%d] = %d\\n\", i, j, nf[i][j]);\n ans = max(ans, nf[i][j]);\n }\n }\n swap(f, nf);\n }\n printf(\"%d\\n\", ans);\n}\n return 0; \n}\n/*\n4\n5 15 5000\n22 23 5500\n10 11 9000\n7 7 6000\n*/", "accuracy": 1, "time_ms": 1000, "memory_kb": 3572, "score_of_the_acc": -0.5269, "final_rank": 13 }, { "submission_id": "aoj_1246_6263995", "code_snippet": "#include<bits/stdc++.h>\n#define rep(i,a,...) for(int i = (a)*(strlen(#__VA_ARGS__)!=0);i<(int)(strlen(#__VA_ARGS__)?__VA_ARGS__:(a));++i)\n#define per(i,a,...) for(int i = (strlen(#__VA_ARGS__)?__VA_ARGS__:(a))-1;i>=(int)(strlen(#__VA_ARGS__)?(a):0);--i)\n#define foreach(i, n) for(auto &i:(n))\n#define all(x) (x).begin(), (x).end()\n#define bit(x) (1ll << (x))\n#define lambda(RES_TYPE, ...) (function<RES_TYPE(__VA_ARGS__)>)[&](__VA_ARGS__) -> RES_TYPE\n#define method(FUNC_NAME, RES_TYPE, ...) function<RES_TYPE(__VA_ARGS__)> FUNC_NAME = lambda(RES_TYPE, __VA_ARGS__)\nusing namespace std;\nusing ll = long long;\nusing pii = pair<int,int>;\nusing pll = pair<ll,ll>;\n//const ll MOD = (ll)1e9+7;\nconst ll MOD = 998244353;\nconst int INF = (ll)1e9+7;\nconst ll INFLL = (ll)1e18;\ntemplate<class t>\nusing vvector = vector<vector<t>>;\ntemplate<class t>\nusing vvvector = vector<vector<vector<t>>>;\ntemplate<class t>\nusing priority_queuer = priority_queue<t, vector<t>, greater<t>>;\ntemplate<class t, class u> bool chmax(t &a, u b){if(a<b){a=b;return true;}return false;}\ntemplate<class t, class u> bool chmin(t &a, u b){if(a>b){a=b;return true;}return false;}\n#ifdef DEBUG\n#define debug(x) cout<<\"LINE \"<<__LINE__<<\": \"<<#x<<\" = \"<<x<<endl;\n#else\n#define debug(x) (void)0\n#endif\n\nnamespace templates{\n ll modpow(ll x, ll b,ll mod=MOD){\n ll res = 1;\n while(b){\n if(b&1)res = res * x % mod;\n x = x * x % mod;\n b>>=1;\n }\n return res;\n }\n\n ll modinv(ll x){\n return modpow(x, MOD-2);\n }\n\n bool was_output = false;\n\n void print();\n template <class t> void print(const vector<t> &);\n template <class t, class u> void print(const pair<t, u> &);\n template <class t> void print(const t&);\n template <class Head, class... Tail> void print(const Head&, const Tail&...);\n\n template <class t> void println(const vector<vector<t>>&);\n template <class t> void println(const vector<t>&);\n template <class t> void println(const t&);\n template <class Head, class... Tail> void println(const Head&, const Tail&...);\n void println();\n void newline();\n\n void print(){\n return;\n }\n\n template <class t>\n void print(const vector<t>&x){\n for(const t&i:x)print(i);\n }\n template <class t, class u>\n void print(const pair<t,u>&p){\n print(p.first);\n print(p.second);\n }\n template <class t>\n void print(const t&x){\n if(was_output)cout<<\" \";\n cout<<x;\n was_output = true;\n }\n template <class Head, class... Tail>\n void print(const Head&head,const Tail&...tail){\n print(head);\n print(tail...);\n }\n\n template<class t>\n void println(const vector<vector<t>>&x){\n for(vector<t> i:x)println(i);\n }\n template<class t>\n void println(const vector<t>&x){\n for(const t&i:x)print(i);\n println();\n }\n template <class t>\n void println(const t&x){\n print(x);\n println();\n }\n void println(){\n if(was_output){\n cout << endl;\n was_output = false;\n }\n }\n template <class Head, class... Tail>\n void println(const Head&head,const Tail&...tail){\n print(head);\n println(tail...);\n }\n\n void newline(){\n was_output = true;\n println();\n }\n\n template<class t>\n istream& operator>>(istream&is, vector<t>&x){\n for(auto &i:x)is >> i;\n return is;\n }\n\n template<class t, class u>\n istream& operator>>(istream&is, pair<t, u>&x){\n is >> x.first >> x.second;\n return is;\n }\n\n template<class t>\n ostream& operator<<(ostream&os, vector<t> &v){\n os << \"{\";\n for(t &i:v){\n os << i << \", \";\n }\n os << \"}\";\n return os;\n }\n\n template<class t = long long>\n t in(){\n t res; cin >> res; return res;\n }\n\n template<class t>\n vector<t> sorted(vector<t> line,function<bool(t,t)> comp=[](t a,t b){return a<b;}){\n sort(line.begin(),line.end(),comp);\n return line;\n }\n\n template<class t>\n vector<t> reversed(vector<t> line){\n reverse(line.begin(),line.end());\n return line;\n }\n string reversed(string str){\n reverse(str.begin(),str.end());\n return str;\n }\n\n long long gcd(long long a,long long b){\n while(b){\n a %= b;\n swap(a,b);\n }\n return a;\n }\n\n long long lcm(long long a,long long b){\n return a / gcd(a,b) * b;\n }\n\n class output_initializer{\n public:\n output_initializer(){\n ios::sync_with_stdio(false);\n cin.tie(nullptr);\n cout << setprecision(20);\n }\n };output_initializer OUTPUT_INITIALIZER_INSTANCE = output_initializer();\n}\n\nusing namespace templates;\n\nclass binary_indexed_tree{\npublic:\n int n;\n vector<int> value;\n binary_indexed_tree(int n):n(n),value(n+1,0){};\n void add(int p,int v){\n for(int i=p+1;i<n;i+=i&(-i)){\n value[i] = max(value[i],v);\n }\n }\n int sum(int p){\n int res = 0;\n for(int i=p;i;i-=i&(-i)){\n res = max(res,value[i]);\n }\n return res;\n }\n};\n\nint func(int n){\n using T = tuple<int,int,int>;\n vector<T> line(n);\n foreach(i,line){\n int l = in();\n int r = in();\n int c = in();\n line.emplace_back(l-1,r-1,c);\n }\n sort(all(line),lambda(bool,T a,T b){\n return get<0>(a) < get<0>(b);\n });\n vector<binary_indexed_tree> dp1(400,binary_indexed_tree(400));\n vector<binary_indexed_tree> dp2(400,binary_indexed_tree(400));\n int res = 0;\n foreach(a,line){\n int l = get<0>(a);\n int r = get<1>(a);\n int c = get<2>(a);\n per(i,365){\n {\n int next = dp1[i].sum(l) + c;\n chmax(res,next);\n dp1[i].add(r,next);\n dp2[r].add(i,next);\n }\n {\n int next = dp2[i].sum(l) + c;\n chmax(res,next);\n dp2[i].add(r,next);\n dp1[r].add(i,next);\n }\n }\n }\n return res;\n}\n\nint main(){\n ios::sync_with_stdio(false);\n cin.tie(nullptr);\n while(true){\n int n = in();\n if(n==0)break;\n println(func(n));\n }\n return 0;\n}", "accuracy": 1, "time_ms": 50, "memory_kb": 4332, "score_of_the_acc": -0.2389, "final_rank": 7 }, { "submission_id": "aoj_1246_6204551", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nint n;\n\nvoid solve(){\n vector<vector<int>> lrc(n, vector<int>(3));\n int d = 0;\n for(int i = 0; i < n; i++){\n cin >> lrc[i][1] >> lrc[i][0] >> lrc[i][2];\n d = max(d, max(lrc[i][1], lrc[i][0]));\n }\n sort(lrc.begin(), lrc.end());\n d++;\n vector<vector<int>> dp(d, vector<int>(d, 0));\n int l, r, c;\n for(auto tmp: lrc){\n l = tmp[1];\n r = tmp[0];\n c = tmp[2];\n vector<int> col(d, 0);\n for(int i = 0; i < l; i++) for(int j = 0; j < d; j++){\n col[j] = max(col[j], dp[i][j] + c);\n }\n \n vector<int> row(d, 0);\n for(int j = 0; j < l; j++) for(int i = 0; i < d; i++){\n col[i] = max(col[i], dp[i][j] + c);\n }\n \n for(int j = 0; j < d; j++){\n dp[r][j] = max(dp[r][j], col[j]);\n dp[j][r] = max(dp[j][r], row[j]);\n }\n }\n \n int ans = 0;\n for(int i = 0; i < d; i++) for(int j = 0; j < d; j++) ans = max(ans, dp[i][j]);\n cout << ans << \"\\n\";\n \n}\n\nint main(void){\n while(1){\n cin >> n;\n if(n == 0) break;\n solve();\n }\n}", "accuracy": 1, "time_ms": 150, "memory_kb": 3928, "score_of_the_acc": -0.1742, "final_rank": 5 }, { "submission_id": "aoj_1246_5990022", "code_snippet": "// #pragma comment(linker, \"/stack:200000000\")\n\n#include <bits/stdc++.h>\n\n#include <limits>\n#include <type_traits>\n\nnamespace suisen {\n// ! utility\ntemplate <typename ...Types>\nusing constraints_t = std::enable_if_t<std::conjunction_v<Types...>, std::nullptr_t>;\ntemplate <bool cond_v, typename Then, typename OrElse>\nconstexpr decltype(auto) constexpr_if(Then&& then, OrElse&& or_else) {\n if constexpr (cond_v) {\n return std::forward<Then>(then);\n } else {\n return std::forward<OrElse>(or_else);\n }\n}\n\n// ! function\ntemplate <typename ReturnType, typename Callable, typename ...Args>\nusing is_same_as_invoke_result = std::is_same<std::invoke_result_t<Callable, Args...>, ReturnType>;\ntemplate <typename F, typename T>\nusing is_uni_op = is_same_as_invoke_result<T, F, T>;\ntemplate <typename F, typename T>\nusing is_bin_op = is_same_as_invoke_result<T, F, T, T>;\n\ntemplate <typename Comparator, typename T>\nusing is_comparator = std::is_same<std::invoke_result_t<Comparator, T, T>, bool>;\n\n// ! integral\ntemplate <typename T, typename = constraints_t<std::is_integral<T>>>\nconstexpr int bit_num = std::numeric_limits<std::make_unsigned_t<T>>::digits;\ntemplate <typename T, unsigned int n>\nstruct is_nbit { static constexpr bool value = bit_num<T> == n; };\ntemplate <typename T, unsigned int n>\nstatic constexpr bool is_nbit_v = is_nbit<T, n>::value;\n\n// ?\ntemplate <typename T>\nstruct safely_multipliable {};\ntemplate <>\nstruct safely_multipliable<int> { using type = long long; };\ntemplate <>\nstruct safely_multipliable<long long> { using type = __int128_t; };\ntemplate <>\nstruct safely_multipliable<float> { using type = float; };\ntemplate <>\nstruct safely_multipliable<double> { using type = double; };\ntemplate <>\nstruct safely_multipliable<long double> { using type = long double; };\ntemplate <typename T>\nusing safely_multipliable_t = typename safely_multipliable<T>::type;\n\n} // namespace suisen\n\n// ! type aliases\nusing i128 = __int128_t;\nusing u128 = __uint128_t;\nusing ll = long long;\nusing uint = unsigned int;\nusing ull = unsigned long long;\n\ntemplate <typename T> using vec = std::vector<T>;\ntemplate <typename T> using vec2 = vec<vec <T>>;\ntemplate <typename T> using vec3 = vec<vec2<T>>;\ntemplate <typename T> using vec4 = vec<vec3<T>>;\n\ntemplate <typename T>\nusing pq_greater = std::priority_queue<T, std::vector<T>, std::greater<T>>;\ntemplate <typename T, typename U>\nusing umap = std::unordered_map<T, U>;\n\n// ! macros (capital: internal macro)\n#define OVERLOAD2(_1,_2,name,...) name\n#define OVERLOAD3(_1,_2,_3,name,...) name\n#define OVERLOAD4(_1,_2,_3,_4,name,...) name\n\n#define REP4(i,l,r,s) for(std::remove_reference_t<std::remove_const_t<decltype(r)>>i=(l);i<(r);i+=(s))\n#define REP3(i,l,r) REP4(i,l,r,1)\n#define REP2(i,n) REP3(i,0,n)\n#define REPINF3(i,l,s) for(std::remove_reference_t<std::remove_const_t<decltype(l)>>i=(l);;i+=(s))\n#define REPINF2(i,l) REPINF3(i,l,1)\n#define REPINF1(i) REPINF2(i,0)\n#define RREP4(i,l,r,s) for(std::remove_reference_t<std::remove_const_t<decltype(r)>>i=(l)+fld((r)-(l)-1,s)*(s);i>=(l);i-=(s))\n#define RREP3(i,l,r) RREP4(i,l,r,1)\n#define RREP2(i,n) RREP3(i,0,n)\n\n#define rep(...) OVERLOAD4(__VA_ARGS__, REP4 , REP3 , REP2 )(__VA_ARGS__)\n#define rrep(...) OVERLOAD4(__VA_ARGS__, RREP4 , RREP3 , RREP2 )(__VA_ARGS__)\n#define repinf(...) OVERLOAD3(__VA_ARGS__, REPINF3, REPINF2, REPINF1)(__VA_ARGS__)\n\n#define CAT_I(a, b) a##b\n#define CAT(a, b) CAT_I(a, b)\n#define UNIQVAR(tag) CAT(tag, __LINE__)\n#define loop(n) for (std::remove_reference_t<std::remove_const_t<decltype(n)>> UNIQVAR(loop_variable) = n; UNIQVAR(loop_variable) --> 0;)\n\n#define all(iterable) (iterable).begin(), (iterable).end()\n#define input(type, ...) type __VA_ARGS__; read(__VA_ARGS__)\n\n// ! I/O utilities\n\n// pair\ntemplate <typename T, typename U>\nstd::ostream& operator<<(std::ostream& out, const std::pair<T, U> &a) {\n return out << a.first << ' ' << a.second;\n}\n// tuple\ntemplate <unsigned int N = 0, typename ...Args>\nstd::ostream& operator<<(std::ostream& out, const std::tuple<Args...> &a) {\n if constexpr (N >= std::tuple_size_v<std::tuple<Args...>>) {\n return out;\n } else {\n out << std::get<N>(a);\n if constexpr (N + 1 < std::tuple_size_v<std::tuple<Args...>>) {\n out << ' ';\n }\n return operator<<<N + 1>(out, a);\n }\n}\n// vector\ntemplate <typename T>\nstd::ostream& operator<<(std::ostream& out, const std::vector<T> &a) {\n for (auto it = a.begin(); it != a.end();) {\n out << *it;\n if (++it != a.end()) out << ' ';\n }\n return out;\n}\ninline void print() { std::cout << '\\n'; }\ntemplate <typename Head, typename... Tail>\ninline void print(const Head &head, const Tail &...tails) {\n std::cout << head;\n if (sizeof...(tails)) std::cout << ' ';\n print(tails...);\n}\ntemplate <typename Iterable>\nauto print_all(const Iterable& v, std::string sep = \" \", std::string end = \"\\n\") -> decltype(std::cout << *v.begin(), void()) {\n for (auto it = v.begin(); it != v.end();) {\n std::cout << *it;\n if (++it != v.end()) std::cout << sep;\n }\n std::cout << end;\n}\n\n// pair\ntemplate <typename T, typename U>\nstd::istream& operator>>(std::istream& in, std::pair<T, U> &a) {\n return in >> a.first >> a.second;\n}\n// tuple\ntemplate <unsigned int N = 0, typename ...Args>\nstd::istream& operator>>(std::istream& in, std::tuple<Args...> &a) {\n if constexpr (N >= std::tuple_size_v<std::tuple<Args...>>) {\n return in;\n } else {\n return operator>><N + 1>(in >> std::get<N>(a), a);\n }\n}\n// vector\ntemplate <typename T>\nstd::istream& operator>>(std::istream& in, std::vector<T> &a) {\n for (auto it = a.begin(); it != a.end(); ++it) in >> *it;\n return in;\n}\ntemplate <typename ...Args>\nvoid read(Args &...args) {\n ( std::cin >> ... >> args );\n}\n\n// ! integral utilities\n\n// Returns pow(-1, n)\ntemplate <typename T>\nconstexpr inline int pow_m1(T n) {\n return -(n & 1) | 1;\n}\n// Returns pow(-1, n)\ntemplate <>\nconstexpr inline int pow_m1<bool>(bool n) {\n return -int(n) | 1;\n}\n\n// Returns floor(x / y)\ntemplate <typename T>\nconstexpr inline T fld(const T x, const T y) {\n return (x ^ y) >= 0 ? x / y : (x - (y + pow_m1(y >= 0))) / y;\n}\ntemplate <typename T>\nconstexpr inline T cld(const T x, const T y) {\n return (x ^ y) <= 0 ? x / y : (x + (y + pow_m1(y >= 0))) / y;\n}\n\ntemplate <typename T, suisen::constraints_t<suisen::is_nbit<T, 32>> = nullptr>\nconstexpr inline int popcount(const T x) { return __builtin_popcount(x); }\ntemplate <typename T, suisen::constraints_t<suisen::is_nbit<T, 64>> = nullptr>\nconstexpr inline int popcount(const T x) { return __builtin_popcountll(x); }\ntemplate <typename T, suisen::constraints_t<suisen::is_nbit<T, 32>> = nullptr>\nconstexpr inline int count_lz(const T x) { return x ? __builtin_clz(x) : suisen::bit_num<T>; }\ntemplate <typename T, suisen::constraints_t<suisen::is_nbit<T, 64>> = nullptr>\nconstexpr inline int count_lz(const T x) { return x ? __builtin_clzll(x) : suisen::bit_num<T>; }\ntemplate <typename T, suisen::constraints_t<suisen::is_nbit<T, 32>> = nullptr>\nconstexpr inline int count_tz(const T x) { return x ? __builtin_ctz(x) : suisen::bit_num<T>; }\ntemplate <typename T, suisen::constraints_t<suisen::is_nbit<T, 64>> = nullptr>\nconstexpr inline int count_tz(const T x) { return x ? __builtin_ctzll(x) : suisen::bit_num<T>; }\ntemplate <typename T>\nconstexpr inline int floor_log2(const T x) { return suisen::bit_num<T> - 1 - count_lz(x); }\ntemplate <typename T>\nconstexpr inline int ceil_log2(const T x) { return floor_log2(x) + ((x & -x) != x); }\ntemplate <typename T>\nconstexpr inline int kth_bit(const T x, const unsigned int k) { return (x >> k) & 1; }\ntemplate <typename T>\nconstexpr inline int parity(const T x) { return popcount(x) & 1; }\n\nstruct all_subset {\n struct all_subset_iter {\n const int s; int t;\n constexpr all_subset_iter(int s) : s(s), t(s + 1) {}\n constexpr auto operator*() const { return t; }\n constexpr auto operator++() {}\n constexpr auto operator!=(std::nullptr_t) { return t ? (--t &= s, true) : false; }\n };\n int s;\n constexpr all_subset(int s) : s(s) {}\n constexpr auto begin() { return all_subset_iter(s); }\n constexpr auto end() { return nullptr; }\n};\n\n// ! container\n\ntemplate <typename T, typename Comparator, suisen::constraints_t<suisen::is_comparator<Comparator, T>> = nullptr>\nauto priqueue_comp(const Comparator comparator) {\n return std::priority_queue<T, std::vector<T>, Comparator>(comparator);\n}\n\ntemplate <typename Iterable>\nauto isize(const Iterable &iterable) -> decltype(int(iterable.size())) {\n return iterable.size();\n}\n\ntemplate <typename T, typename Gen, suisen::constraints_t<suisen::is_same_as_invoke_result<T, Gen, int>> = nullptr>\nauto generate_vector(int n, Gen generator) {\n std::vector<T> v(n);\n for (int i = 0; i < n; ++i) v[i] = generator(i);\n return v;\n}\n\ntemplate <typename T>\nvoid sort_unique_erase(std::vector<T> &a) {\n std::sort(a.begin(), a.end());\n a.erase(std::unique(a.begin(), a.end()), a.end());\n}\n\n// ! other utilities\n\n// x <- min(x, y). returns true iff `x` has chenged.\ntemplate <typename T>\ninline bool chmin(T &x, const T &y) {\n if (y >= x) return false;\n x = y;\n return true;\n}\n// x <- max(x, y). returns true iff `x` has chenged.\ntemplate <typename T>\ninline bool chmax(T &x, const T &y) {\n if (y <= x) return false;\n x = y;\n return true;\n}\n\nnamespace suisen {}\nusing namespace suisen;\nusing namespace std;\n\nstruct io_setup {\n io_setup(int precision = 20) {\n std::ios::sync_with_stdio(false);\n std::cin.tie(nullptr);\n std::cout << std::fixed << std::setprecision(precision);\n }\n} io_setup_ {};\n\n// ! code from here\n\nconstexpr int D = 366;\n\nvoid solve(const int n, vector<tuple<int, int, int>> &applicants) {\n vector dp(D, vector(D, 0LL));\n sort(all(applicants), [](auto &p1, auto &p2) {\n auto &[l1, r1, w1] = p1;\n auto &[l2, r2, w2] = p2;\n return r1 == r2 ? l1 < l2 : r1 < r2;\n });\n for (auto &[l, r, w] : applicants) {\n rep(i, l + 1) rep(j, D) {\n chmax(dp[r][j], dp[i][j] + w);\n }\n rep(i, D) rep(j, i) {\n chmax(dp[i][j], dp[j][i]);\n chmax(dp[j][i], dp[i][j]);\n }\n }\n long long ans = 0;\n for (auto &v : dp) chmax(ans, *max_element(all(v)));\n print(ans);\n}\n\nint main() {\n while (true) {\n input(int, n);\n if (n == 0) break;\n vector<tuple<int, int, int>> applicants(n);\n read(applicants);\n for (auto &[l, r, w] : applicants) --l;\n solve(n, applicants);\n }\n}", "accuracy": 1, "time_ms": 300, "memory_kb": 4076, "score_of_the_acc": -0.2983, "final_rank": 8 }, { "submission_id": "aoj_1246_5935821", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\n\nint main() {\n ios_base::sync_with_stdio(false);\n cin.tie(nullptr);\n cout << fixed << setprecision(15);\n\n while (true) {\n int N;\n cin >> N;\n if (N == 0) break;\n vector<vector<pair<int, int>>> apps(366);\n for (int t = 0; t < N; ++t) {\n int i, j, w;\n cin >> i >> j >> w;\n apps[j].push_back({i - 1, w});\n }\n vector<vector<int>> dp(366, vector<int>(366));\n for (int j = 1; j <= 365; ++j) {\n for (int p = 0; p < j; ++p) {\n dp[j][p] = dp[j-1][p];\n dp[p][j] = dp[p][j-1];\n }\n dp[j][j] = dp[j-1][j-1];\n for (auto [i, w] : apps[j]) {\n auto ndp = dp;\n for (int p = 0; p <= j; ++p) {\n ndp[j][p] = max(ndp[j][p], dp[i][p] + w);\n ndp[p][j] = max(ndp[p][j], dp[p][i] + w);\n }\n dp.swap(ndp);\n }\n }\n cout << dp[365][365] << \"\\n\";\n }\n}", "accuracy": 1, "time_ms": 220, "memory_kb": 3972, "score_of_the_acc": -0.2248, "final_rank": 6 }, { "submission_id": "aoj_1246_5934526", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\nint dp[2][366][366];\n\nint main() {\n while (true) {\n int n;\n cin >> n;\n if(n == 0) {\n return 0;\n }\n vector<array<int,3>>ar(n);\n for(int i = 0; i < n; i++) {\n cin >> ar[i][0] >> ar[i][1] >> ar[i][2];\n ar[i][0]--;\n }\n sort(ar.begin(),ar.end());\n for(int k = 0; k < n; k++) {\n for(int i = 0; i <= 365; i++) {\n for(int j = 0; j <= 365; j++) {\n dp[1][i][j] = max(dp[1][i][j],dp[0][i][j]);\n if(i <= ar[k][0]) {\n dp[1][ar[k][1]][j] = max(dp[1][ar[k][1]][j],dp[0][i][j]+ar[k][2]);\n }\n if(j <= ar[k][0]) {\n dp[1][i][ar[k][1]] = max(dp[1][i][ar[k][1]],dp[0][i][j]+ar[k][2]);\n }\n }\n }\n for(int i = 0; i <= 365; i++) {\n for(int j = 0; j <= 365; j++) {\n dp[0][i][j] = dp[1][i][j];\n dp[1][i][j] = 0;\n }\n }\n }\n int ans = 0;\n for(int k = 0; k <= 1; k++) {\n for(int i = 0; i <= 365; i++) {\n for(int j = 0; j <= 365; j++) {\n ans = max(ans,dp[k][i][j]);\n dp[k][i][j] = 0;\n }\n }\n }\n cout << ans << endl;\n }\n}", "accuracy": 1, "time_ms": 620, "memory_kb": 4252, "score_of_the_acc": -0.5219, "final_rank": 12 }, { "submission_id": "aoj_1246_5835646", "code_snippet": "#include <iostream>\n#include <vector>\n#include <algorithm>\n\n#define rep(i, a, n) for (int i = (int)(a); i < (int)(n); i++)\n#define rrep(i, a, n) for (int i = ((int)(n-1)); i >= (int)(a); i--)\n\nusing i64 = std::int64_t;\n\nconstexpr i64 mod = 1e9 + 7;\n\nconstexpr i64 LNF = 1e18;\n\nconstexpr int sz = 365;\n\nstruct Query {\n int i,j;\n i64 w;\n};\n\ntemplate <class T>\ninline bool chmin(T &a, T b) {\n if (a > b){\n a = b;\n return true;\n }\n return false;\n}\n \ntemplate <class T>\ninline bool chmax(T &a, T b) {\n if (a < b){\n a = b;\n return true;\n }\n return false;\n}\n\nint main() {\n int n;\n while(std::cin >> n, n != 0) {\n std::vector dp(sz+1, std::vector<i64>(sz+1, -1));\n dp[0][0] = 0;\n std::vector<Query> p(n);\n rep(i,0,n) {\n std::cin >> p[i].i >> p[i].j >> p[i].w;\n p[i].i--;\n }\n std::sort(p.begin(), p.end(), [](const Query &a, const Query &b) -> bool { return a.j == b.j ? a.w > b.w : a.j < b.j; });\n for(auto q: p) {\n int l = q.i;\n int r = q.j;\n i64 w = q.w;\n rrep(i,0,sz+1) {\n rrep(j,0,sz+1) {\n if(dp[i][j] == -1) continue;\n if(i <= l) {\n chmax(dp[r][j], dp[i][j] + w);\n }\n if(j <= l) {\n chmax(dp[i][r], dp[i][j] + w);\n }\n }\n }\n }\n i64 ans = 0;\n rep(i,0,sz+1) {\n rep(j,0,sz+1) {\n chmax(ans, dp[i][j]);\n }\n }\n std::cout << ans << '\\n';\n }\n}", "accuracy": 1, "time_ms": 330, "memory_kb": 4412, "score_of_the_acc": -0.4129, "final_rank": 10 }, { "submission_id": "aoj_1246_5804516", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\nusing P = pair<int,int>;\n\nint main() {\n \n ios::sync_with_stdio(false);\n cin.tie(0);\n\n int n;\n cin >> n;\n while (n != 0) {\n vector<int> l(n),r(n),w(n);\n for (int i=0;i<n;i++) {\n cin >> l[i] >> r[i] >> w[i];\n l[i]--;\n }\n vector<P> ps(n);\n for (int i=0;i<n;i++) {\n ps[i] = P(r[i],i);\n }\n sort(ps.begin(),ps.end());\n vector<vector<int>> dp(366,vector<int>(366,0));\n for (int j=0;j<n;j++) {\n int ind = ps[j].second;\n queue<P> q;\n for (int s=365*2;s>=0;s--) {\n for (int i=max(s-365,0);i<=min(s,365);i++) {\n // from dp[i][s-i]\n if (min(i,s-i) <= l[ind]) {\n q.push(P(i,s-i));\n }\n }\n }\n while (!q.empty()) {\n P np = q.front();\n q.pop();\n int ni = np.first;\n int nj = np.second;\n if (ni <= l[ind]) {\n dp[r[ind]][nj] = max(dp[r[ind]][nj],dp[ni][nj]+w[ind]);\n }\n if (nj <= l[ind]) {\n dp[ni][r[ind]] = max(dp[ni][r[ind]],dp[ni][nj]+w[ind]);\n }\n }\n }\n int ans = 0;\n for (int i=0;i<=365;i++) {\n for (int j=0;j<=365;j++) {\n ans = max(ans,dp[i][j]);\n //if (i <= 3 && j <= 3) cout << dp[i][j] << ' ';\n }\n //if (i <= 3)cout << endl;\n }\n cout << ans << endl;\n cin >> n;\n }\n}", "accuracy": 1, "time_ms": 1130, "memory_kb": 4712, "score_of_the_acc": -0.9309, "final_rank": 15 }, { "submission_id": "aoj_1246_5748037", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\nusing P = pair<int,int>;\n\nint main() {\n \n ios::sync_with_stdio(false);\n cin.tie(0);\n\n int n;\n cin >> n;\n while (n != 0) {\n vector<int> l(n),r(n),w(n);\n for (int i=0;i<n;i++) {\n cin >> l[i] >> r[i] >> w[i];\n l[i]--;\n }\n vector<P> ps(n);\n for (int i=0;i<n;i++) {\n ps[i] = P(r[i],i);\n }\n sort(ps.begin(),ps.end());\n vector<vector<int>> dp(366,vector<int>(366,0));\n for (int j=0;j<n;j++) {\n int ind = ps[j].second;\n queue<P> q;\n for (int s=365*2;s>=0;s--) {\n for (int i=max(s-365,0);i<=min(s,365);i++) {\n // from dp[i][s-i]\n if (min(i,s-i) <= l[ind]) {\n q.push(P(i,s-i));\n }\n }\n }\n while (!q.empty()) {\n P np = q.front();\n q.pop();\n int ni = np.first;\n int nj = np.second;\n if (ni <= l[ind]) {\n dp[r[ind]][nj] = max(dp[r[ind]][nj],dp[ni][nj]+w[ind]);\n }\n if (nj <= l[ind]) {\n dp[ni][r[ind]] = max(dp[ni][r[ind]],dp[ni][nj]+w[ind]);\n }\n }\n }\n int ans = 0;\n for (int i=0;i<=365;i++) {\n for (int j=0;j<=365;j++) {\n ans = max(ans,dp[i][j]);\n //if (i <= 3 && j <= 3) cout << dp[i][j] << ' ';\n }\n //if (i <= 3)cout << endl;\n }\n cout << ans << endl;\n cin >> n;\n }\n}", "accuracy": 1, "time_ms": 1130, "memory_kb": 4820, "score_of_the_acc": -0.9625, "final_rank": 16 }, { "submission_id": "aoj_1246_5734210", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\n#define ALL(x) begin(x),end(x)\n#define rep(i,n) for(int i=0;i<(n);i++)\n#define debug(v) cout<<#v<<\":\";for(auto x:v){cout<<x<<' ';}cout<<endl;\n#define mod 1000000007\nusing ll=long long;\nconst int INF=1000000000;\nconst ll LINF=1001002003004005006ll;\nint dx[]={1,0,-1,0},dy[]={0,1,0,-1};\n// ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}\ntemplate<class T>bool chmax(T &a,const T &b){if(a<b){a=b;return true;}return false;}\ntemplate<class T>bool chmin(T &a,const T &b){if(b<a){a=b;return true;}return false;}\n\nstruct IOSetup{\n IOSetup(){\n cin.tie(0);\n ios::sync_with_stdio(0);\n cout<<fixed<<setprecision(12);\n }\n} iosetup;\n\ntemplate<typename T>\nostream &operator<<(ostream &os,const vector<T>&v){\n for(int i=0;i<(int)v.size();i++) os<<v[i]<<(i+1==(int)v.size()?\"\":\" \");\n return os;\n}\ntemplate<typename T>\nistream &operator>>(istream &is,vector<T>&v){\n for(T &x:v)is>>x;\n return is;\n}\n\ntemplate<typename Monoid>\nstruct SegmentTree{\n using F=function<Monoid(Monoid,Monoid)>;\n\n private:\n int sz;\n vector<Monoid> seg;\n\n Monoid query(int a,int b,int k,int l,int r){\n if(a<=l and r<=b) return seg[k];\n if(b<=l or r<=a) return M0;\n Monoid L=query(a,b,2*k,l,(l+r)/2);\n Monoid R=query(a,b,2*k+1,(l+r)/2,r);\n return f(L,R);\n }\n template<typename C>\n int find_first(int a,const C &check,Monoid &acc,int k,int l,int r){\n if(k>=sz){\n acc=f(acc,seg[k]);\n if(check(acc)) return -1;\n else return k-sz;\n }\n int m=(l+r)/2;\n if(m<=a) return find_first(a,check,acc,2*k+1,m,r);\n if(a<=l and check(f(acc,seg[k]))){\n acc=f(acc,seg[k]);\n return -1;\n }\n int x=find_first(a,check,acc,2*k+0,l,m);\n if(x>=0) return x;\n return find_first(a,check,acc,2*k+1,m,r);\n }\n template<typename C>\n int find_last(int b,const C &check,Monoid &acc,int k,int l,int r){\n if(k>=sz){\n acc=f(acc,seg[k]);\n if(check(acc)) return -1;\n else return k-sz+1;//ここはfalse, +1した位置はtrue\n }\n int m=(l+r)/2;\n if(b<=m) return find_last(b,check,acc,2*k,l,m);\n if(r<=b and check(f(acc,seg[k]))){\n acc=f(acc,seg[k]);\n return -1;\n }\n int x=find_last(b,check,acc,2*k+1,m,r);\n if(x>=0) return x;\n return find_last(b,check,acc,2*k,l,m);\n }\n\n public:\n\n F f;\n Monoid M0;// モノイドの元\n SegmentTree(int n,F f,Monoid M0):f(f),M0(M0){\n sz=1;\n while(sz<n)sz<<=1;\n seg.assign(2*sz,M0);\n }\n void set(int k,Monoid x){\n seg[k+sz]=x;\n }\n void build(){\n for(int k=sz-1;k>0;k--) seg[k]=f(seg[2*k],seg[2*k+1]);\n }\n void update(int k,Monoid x){\n k+=sz;\n seg[k]=x;\n k>>=1;\n for(;k;k>>=1) seg[k]=f(seg[2*k],seg[2*k+1]);\n }\n Monoid query(int a,int b){\n return query(a,b,1,0,sz);\n }\n Monoid operator[](const int &k)const{\n return seg[k+sz];\n }\n\n // http://codeforces.com/contest/914/submission/107505449\n // max x, check(query(a, x)) = true \n template<typename C>\n int find_first(int a,const C &check){\n Monoid val=M0;\n return find_first(a,check,val,1,0,sz);\n }\n // http://codeforces.com/contest/914/submission/107505582\n // min x, check(query(x, b)) = true\n template<typename C>\n int find_last(int b,C &check){\n Monoid val=M0;\n return find_last(b,check,val,1,0,sz);\n }\n};\n\nint segfunc(int x,int y){\n return max(x,y);\n}\n\nint N;\n\nconst int D=400;\n\nvoid solve(){\n using T=tuple<int,int,int>;\n vector<T> v;// r, l, w;\n rep(i,N){\n int l,r,w;cin>>l>>r>>w;r++;\n v.emplace_back(r,l,w);\n }\n sort(ALL(v));\n\n vector<vector<int>> dp(D,vector<int>(D,0));\n\n vector<SegmentTree<int>>H,W;\n rep(i,D) H.push_back(SegmentTree<int>(D,segfunc,0)),W.push_back(SegmentTree<int>(D,segfunc,0));\n rep(i,D) H[i].build(),W[i].build();\n\n for(auto [r,l,w]:v){\n {\n for(int i=0;i<D;i++){\n int t=H[i].query(0,l+1)+w;\n if(H[i][r]<t) H[i].update(r,t);\n }\n }\n {\n for(int i=0;i<D;i++){\n int t=W[i].query(0,l+1)+w;\n if(W[i][r]<t) W[i].update(r,t);\n }\n }\n\n rep(i,D){\n {\n int hval=H[i].query(r,r+1);\n int wval=W[r].query(i,i+1);\n if(hval>wval){\n if(W[r][i]<hval) W[r].update(i,hval);\n }\n else{\n if(H[i][r]<wval) H[i].update(r,wval);\n }\n }\n {\n int hval=H[r].query(i,i+1);\n int wval=W[i].query(r,r+1);\n if(hval>wval){\n if(W[i][r]<hval) W[i].update(r,hval);\n }\n else{\n if(H[r][i]<wval) H[r].update(i,wval);\n }\n }\n\n }\n\n // cout<<l<<\" ~ \"<<r<<\" ) \"<<w<<endl;\n // rep(i,8){\n // rep(j,8){\n // cout<<max(H[i].query(j,j+1),W[j].query(i,i+1))<<\" \";\n // }\n // cout<<endl;\n // }\n }\n\n\n int mx=0;\n rep(i,D) chmax(mx,H[i].query(0,D));\n rep(i,D) chmax(mx,W[i].query(0,D));\n cout<<mx<<endl;\n}\n\nsigned main(){\n while(cin>>N,N) solve();\n return 0;\n}", "accuracy": 1, "time_ms": 340, "memory_kb": 6984, "score_of_the_acc": -1.172, "final_rank": 17 }, { "submission_id": "aoj_1246_5446058", "code_snippet": "#pragma GCC optimize(\"Ofast\")\n#include <bits/stdc++.h>\nusing namespace std;\ntypedef long long int ll;\ntypedef unsigned long long ull;\n\nmt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());\nll myRand(ll B) {\n return (ull)rng() % B;\n}\n\nconst int N=367;\n\nint main(){\n cin.tie(nullptr);\n ios::sync_with_stdio(false);\n int n;\n while(cin >> n,n){\n vector<vector<int>> dp(N,vector<int>(N));\n vector<pair<pair<int,int>,int>> v(n);\n for(int i=0;i<n;i++){\n cin >> v[i].first.first >> v[i].first.second >> v[i].second;\n }\n sort(v.begin(), v.end(),[&](auto i,auto j){\n return i.first.second<j.first.second;\n });\n for(int i=0;i<n;i++){\n vector<vector<int>> ndp=dp;\n for(int j=0;j<N;j++){\n for(int k=0;k<N;k++){\n if(j<=v[i].first.first){\n ndp[v[i].first.second+1][k]=max(ndp[v[i].first.second+1][k],dp[j][k]+v[i].second);\n }\n if(k<=v[i].first.first){\n ndp[j][v[i].first.second+1]=max(ndp[j][v[i].first.second+1],dp[j][k]+v[i].second);\n }\n }\n }\n swap(dp,ndp);\n }\n int res=0;\n for(int i=0;i<N;i++){\n for(int j=0;j<N;j++){\n res=max(res,dp[i][j]);\n }\n }\n cout << res << endl;\n }\n}", "accuracy": 1, "time_ms": 730, "memory_kb": 3956, "score_of_the_acc": -0.4943, "final_rank": 11 }, { "submission_id": "aoj_1246_5391384", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\n/**\n * @brief compress\n */\ntemplate <typename T> map<T, int> compress(vector<T>& v) {\n sort(v.begin(), v.end());\n v.erase(unique(v.begin(), v.end()), v.end());\n map<T, int> res;\n for (int i = 0; i < v.size(); i++) res[v[i]] = i;\n return res;\n}\n\n/**\n * @brief Segment Tree\n * @docs docs/datastructure/SegmentTree.md\n */\ntemplate <typename Monoid> struct SegmentTree {\n typedef function<Monoid(Monoid, Monoid)> F;\n int n;\n F f;\n Monoid id;\n vector<Monoid> dat;\n SegmentTree(int n_, F f, Monoid id) : f(f), id(id) { init(n_); }\n void init(int n_) {\n n = 1;\n while (n < n_) n <<= 1;\n dat.assign(n << 1, id);\n }\n void build(const vector<Monoid>& v) {\n for (int i = 0; i < (int)v.size(); i++) dat[i + n] = v[i];\n for (int i = n - 1; i; i--) dat[i] = f(dat[i << 1 | 0], dat[i << 1 | 1]);\n }\n void update(int k, Monoid x) {\n dat[k += n] = x;\n while (k >>= 1) dat[k] = f(dat[k << 1 | 0], dat[k << 1 | 1]);\n }\n Monoid query(int a, int b) {\n if (a >= b) return id;\n Monoid vl = id, vr = id;\n for (int l = a + n, r = b + n; l < r; l >>= 1, r >>= 1) {\n if (l & 1) vl = f(vl, dat[l++]);\n if (r & 1) vr = f(dat[--r], vr);\n }\n return f(vl, vr);\n }\n template <typename C> int find_subtree(int k, const C& check, Monoid& M, bool type) {\n while (k < n) {\n Monoid nxt = type ? f(dat[k << 1 | type], M) : f(M, dat[k << 1 | type]);\n if (check(nxt))\n k = k << 1 | type;\n else\n M = nxt, k = k << 1 | (type ^ 1);\n }\n return k - n;\n }\n // min i s.t. f(seg[a],seg[a+1],...,seg[i]) satisfy \"check\"\n template <typename C> int find_first(int a, const C& check) {\n Monoid L = id;\n if (a <= 0) {\n if (check(f(L, dat[1]))) return find_subtree(1, check, L, false);\n return -1;\n }\n int b = n;\n for (int l = a + n, r = b + n; l < r; l >>= 1, r >>= 1) {\n if (l & 1) {\n Monoid nxt = f(L, dat[l]);\n if (check(nxt)) return find_subtree(l, check, L, false);\n L = nxt;\n l++;\n }\n }\n return -1;\n }\n // max i s.t. f(seg[i],...,seg[b-2],seg[b-1]) satisfy \"check\"\n template <typename C> int find_last(int b, const C& check) {\n Monoid R = id;\n if (b >= n) {\n if (check(f(dat[1], R))) return find_subtree(1, check, R, true);\n return -1;\n }\n int a = n;\n for (int l = a, r = b + n; l < r; l >>= 1, r >>= 1) {\n if (r & 1) {\n Monoid nxt = f(dat[--r], R);\n if (check(nxt)) return find_subtree(r, check, R, true);\n R = nxt;\n }\n }\n return -1;\n }\n Monoid operator[](int i) { return dat[i + n]; }\n};\n\nconst int INF = 1e9;\n\nstruct event {\n int l, r, w;\n event(int l, int r, int w) : l(l), r(r), w(w) {}\n bool operator<(const event& rhs) const { return r < rhs.r; }\n};\n\nvoid solve(int n) {\n vector<int> l(n), r(n), w(n), comp;\n for (int i = 0; i < n; i++) {\n cin >> l[i] >> r[i] >> w[i];\n l[i]--;\n comp.emplace_back(l[i]);\n comp.emplace_back(r[i]);\n }\n\n map<int, int> mp = compress(comp);\n vector<event> es;\n for (int i = 0; i < n; i++) es.emplace_back(mp[l[i]], mp[r[i]], w[i]);\n sort(es.begin(), es.end());\n int sz = mp.size();\n vector<SegmentTree<int>> seg(sz, SegmentTree<int>(\n sz, [](int a, int b) { return max(a, b); }, -INF));\n seg[0].update(0, 0);\n\n for (int i = 0; i < n; i++) {\n int L = es[i].l, R = es[i].r;\n vector<pair<int, int>> update;\n for (int j = 0; j < sz; j++) {\n seg[j].update(R, max(seg[j][R], seg[j].query(0, L + 1) + es[i].w));\n update.emplace_back(j, R);\n }\n for (auto& p : update) {\n int x = p.first, y = p.second;\n seg[y].update(x, max(seg[y][x], seg[x][y]));\n }\n }\n\n int ans = -INF;\n for (int i = 0; i < sz; i++) {\n for (int j = 0; j < sz; j++) {\n ans = max(ans, seg[i][j]);\n }\n }\n\n cout << ans << '\\n';\n}\n\nint main() {\n cin.tie(0);\n ios::sync_with_stdio(false);\n int n;\n while (cin >> n, n) solve(n);\n return 0;\n}", "accuracy": 1, "time_ms": 40, "memory_kb": 4644, "score_of_the_acc": -0.3249, "final_rank": 9 } ]
aoj_1242_cpp
Problem C: Area of Polygons Yoko’s math homework today was to calculate areas of polygons in the xy -plane. Vertices are all aligned to grid points (i.e. they have integer coordinates). Your job is to help Yoko, not good either at math or at computer programming, get her home- work done. A polygon is given by listing the coordinates of its vertices. Your program should approximate its area by counting the number of unit squares (whose vertices are also grid points) intersecting the polygon. Precisely, a unit square “intersects the polygon” if and only if the in- tersection of the two has non-zero area. In the figure below, dashed horizontal and vertical lines are grid lines, and solid lines are edges of the polygon. Shaded unit squares are considered inter- secting the polygon. Your program should output 55 for this polygon (as you see, the number of shaded unit squares is 55). Figure 1: A polygon and unit squares intersecting it Input The input file describes polygons one after another, followed by a terminating line that only contains a single zero. A description of a polygon begins with a line containing a single integer, m (≥ 3), that gives the number of its vertices. It is followed by m lines, each containing two integers x and y , the coordinates of a vertex. The x and y are separated by a single space. The i -th of these m lines gives the coordinates of the i -th vertex ( i = 1, ... , m ). For each i = 1, ... , m - 1, the i -th vertex and the ( i + 1)-th vertex are connected by an edge. The m -th vertex and the first vertex are also connected by an edge (i.e., the curve is closed). Edges intersect only at vertices. No three edges share a single vertex (i.e., the curve is simple). The number of polygons is no more than 100. For each polygon, the number of vertices ( m ) is no more than 100. All coordinates x and y satisfy -2000 ≤ x ≤ 2000 and -2000 ≤ y ≤ 2000. Output The output should consist of as many lines as the number of polygons. The k -th output line should print an integer which is the area of the k -th polygon, approximated in the way described above. No other characters, including whitespaces, should be printed. Sample Input 4 5 -3 1 0 1 7 -7 -1 3 5 5 18 5 5 10 3 -5 -5 -5 -10 -18 -10 5 0 0 20 2 11 1 21 2 2 0 0 Output for the Sample Input 55 41 41 23
[ { "submission_id": "aoj_1242_10851351", "code_snippet": "#include<iostream>\n#include<cstdio>\n#include<algorithm>\n#include<cmath>\nusing namespace std;\nconst double eps=1e-8;\nstruct Point{double x,y;}p[1005];\nstruct seg{Point A,B;}s[1005];\nstruct SEG{int x,y;SEG(int _x=0,int _y=0){x=_x;y=_y;}}t[1005];\nint N,tt=0;\nstruct jgt{double x,y,mid;jgt(double _x=0,double _y=0,double _mid=0){x=_x;y=_y;mid=_mid;}}q[1005];\nbool operator < (const jgt &x,const jgt &y){return x.mid<y.mid;}\nbool operator < (const SEG &x,const SEG &y){return x.x<y.x;}\nvoid Makeseg(Point A,Point B)\n{ if(A.x>B.x)swap(A,B);\n tt++;s[tt].A=A;s[tt].B=B;\n}\ndouble Abs(double x){if(x<0)x=-x;return x;}\nvoid GetInter(double x)\n{ int i;\n Point A,B;\n for(i=1,tt=0;i<=N;i++)\n if(s[i].A.x<=x&&x<=s[i].B.x&&s[i].A.x<=x+1&&x+1<=s[i].B.x&&Abs(s[i].A.x-s[i].B.x)>eps)\n {A=s[i].A;B=s[i].B;\n\t\t q[++tt]=jgt(A.y+(B.y-A.y)/(B.x-A.x)*(x-A.x),A.y+(B.y-A.y)/(B.x-A.x)*(x+1-A.x),A.y+(B.y-A.y)/(B.x-A.x)*(x+0.5-A.x));\n\t\t }\n}\nint main()\n{ int k,i,ans,st,ed;\n\twhile(1)\n\t {scanf(\"%d\",&N);\n\t if(!N)break;\n\t ans=tt=0;\n\t for(i=1;i<=N;i++)scanf(\"%lf%lf\",&p[i].x,&p[i].y);\n\t for(i=1;i<N;i++)Makeseg(p[i],p[i+1]);\n\t Makeseg(p[N],p[1]);\n\t for(k=-2000;k<2000;k++)\n\t {GetInter(k);\n\t if(!tt)continue;\n\t sort(q+1,q+1+tt);\n\t for(i=1;i<=tt;i+=2)\n\t {t[(i+1)/2]=SEG(floor(min(q[i].x+eps,q[i].y+eps)),ceil(max(q[i+1].x-eps,q[i+1].y-eps)));}\n\t tt/=2;\n\t sort(t+1,t+1+tt);\n\t for(i=1,st=ed=-3005;i<=tt;i++)\n\t {if(t[i].x>ed){ans+=ed-st;st=t[i].x;ed=t[i].y;}\n\t else if(t[i].y>ed)ed=t[i].y;\n\t\t\t}\n\t\t ans+=ed-st;\n\t\t }\n\t printf(\"%d\\n\",ans);\n\t }\n\treturn 0;\n}", "accuracy": 1, "time_ms": 40, "memory_kb": 3352, "score_of_the_acc": -0.0186, "final_rank": 2 }, { "submission_id": "aoj_1242_10108617", "code_snippet": "#include <iostream>\n#include <vector>\n#include <algorithm>\nusing namespace std;\nusing i64 = long long;\n#define rep(i,n) for(i64 i=0; i<(i64)(n); i++)\n\n\ni64 solve(vector<pair<i64,i64>> A){\n i64 n = A.size();\n A.push_back(A[0]);\n i64 area = 0;\n rep(i,n){\n auto [u,v] = A[i];\n auto [x,y] = A[i+1];\n area += u*y - v*x;\n }\n if(area < 0) reverse(A.begin() + 1, A.end() - 1);\n i64 ans = 0;\n vector<vector<pair<i64,i64>>> que(4000);\n rep(i,n){\n auto [x0, y0] = A[i];\n auto [x1, y1] = A[i+1];\n if(y0 < y1){\n if(x0 <= x1){\n for(i64 y=y0+1; y<=y1; y++){\n i64 x = (x0 * (y1-y) + x1 * (y-y0) + (y1-y0-1)) / (y1-y0);\n que[y-1].push_back({x,1});\n }\n } else {\n for(i64 y=y0; y<y1; y++){\n i64 x = (x0 * (y1-y) + x1 * (y-y0) + (y1-y0-1)) / (y1-y0);\n que[y].push_back({x,1});\n }\n }\n } else {\n if(x0 <= x1){\n for(i64 y=y1+1; y<=y0; y++){\n i64 x = (x1 * (y0-y) + x0 * (y-y1)) / (y0-y1);\n que[y-1].push_back({x,-1});\n }\n } else {\n for(i64 y=y1; y<y0; y++){\n i64 x = (x1 * (y0-y) + x0 * (y-y1)) / (y0-y1);\n que[y].push_back({x,-1});\n }\n }\n }\n }\n for(auto& qu : que) if(!qu.empty()){\n sort(qu.begin(), qu.end());\n i64 f = 0;\n //for(auto [u,v] : qu){ cout << \"(\" << (u-2000) << \",\" << v << \")\"; } cout << endl;\n for(auto [u,v] : qu){\n if(!f) ans -= u;\n f += v;\n if(!f) ans += u;\n }\n }\n return ans;\n}\n\nvoid testcase(){\n i64 N; cin >> N;\n if(N == 0) exit(0);\n vector<pair<i64,i64>> A(N);\n for(auto& [u,v] : A){\n cin >> u >> v;\n u += 2000; v += 2000;\n }\n i64 ans = solve(move(A));\n cout << ans << \"\\n\";\n}\n\nint main(){\n ios::sync_with_stdio(false);\n cin.tie(nullptr);\n while(true) testcase();\n return 0;\n}", "accuracy": 1, "time_ms": 80, "memory_kb": 11244, "score_of_the_acc": -0.6751, "final_rank": 16 }, { "submission_id": "aoj_1242_9820272", "code_snippet": "/// jajaja\n#include <bits/stdc++.h>\nusing namespace std;\n#define x first\n#define y second\nusing Point = pair<int, int>;\n\nconst double EPS = 1e-5;\n\nint main() {\n int n;\n while (cin >> n && n > 0) {\n vector<Point> polygon(n);\n for (auto& p : polygon) cin >> p.x >> p.y;\n\n int area2 = 0;\n for (int i = n - 1, j = 0; j < n; i = j++) {\n const Point p = polygon[i];\n const Point q = polygon[j];\n area2 += p.x * q.y - p.y * q.x;\n }\n if (area2 < 0) reverse(polygon.begin(), polygon.end());\n\n unordered_map<int, vector<pair<int, int>>> events_by_y;\n\n for (int i = n - 1, j = 0; j < n; i = j++) {\n const Point p = polygon[i];\n const Point q = polygon[j];\n if (p.y == q.y) continue;\n\n const auto [min_y, max_y] = minmax(p.y, q.y);\n const double slope = (p.x - q.x) / (double)(p.y - q.y);\n\n for (int y = min_y; y < max_y; y++) {\n const double a = p.x + slope * (y - p.y);\n const double b = p.x + slope * (y + 1 - p.y);\n\n if (p.y > q.y) {\n const int x = floor(min(a, b) + EPS);\n events_by_y[y].emplace_back(x, 1);\n } else {\n const int x = ceil(max(a, b) - EPS);\n events_by_y[y].emplace_back(x, -1);\n }\n }\n }\n\n int ans = 0;\n\n for (auto& [_, events] : events_by_y) {\n sort(events.begin(), events.end());\n\n int count = 0;\n int prev = 0;\n\n for (const auto& [x, d] : events) {\n if (count == 0) prev = x;\n count += d;\n if (count == 0) ans += x - prev;\n }\n }\n\n cout << ans << endl;\n }\n}", "accuracy": 1, "time_ms": 70, "memory_kb": 7796, "score_of_the_acc": -0.3905, "final_rank": 13 }, { "submission_id": "aoj_1242_9734608", "code_snippet": "#include <bits/stdc++.h>\n\nusing namespace std;\n\n#define rep(i, a, b) for (int i = (int)(a); i < (int)(b); i++)\n#define rrep(i, a, b) for (int i = (int)(b)-1; i >= (int)(a); i--)\n#define ALL(v) (v).begin(), (v).end()\n#define UNIQUE(v) sort(ALL(v)), (v).erase(unique(ALL(v)), (v).end())\n#define SZ(v) (int)v.size()\n#define MIN(v) *min_element(ALL(v))\n#define MAX(v) *max_element(ALL(v))\n#define LB(v, x) int(lower_bound(ALL(v), (x)) - (v).begin())\n#define UB(v, x) int(upper_bound(ALL(v), (x)) - (v).begin())\n\nusing uint = unsigned int;\nusing ll = long long int;\nusing ull = unsigned long long;\nusing i128 = __int128_t;\nusing u128 = __uint128_t;\nconst int inf = 0x3fffffff;\nconst ll INF = 0x1fffffffffffffff;\n\ntemplate <typename T> inline bool chmax(T &a, T b) {\n if (a < b) {\n a = b;\n return 1;\n }\n return 0;\n}\ntemplate <typename T> inline bool chmin(T &a, T b) {\n if (a > b) {\n a = b;\n return 1;\n }\n return 0;\n}\ntemplate <typename T, typename U> T ceil(T x, U y) {\n assert(y != 0);\n if (y < 0)\n x = -x, y = -y;\n return (x > 0 ? (x + y - 1) / y : x / y);\n}\ntemplate <typename T, typename U> T floor(T x, U y) {\n assert(y != 0);\n if (y < 0)\n x = -x, y = -y;\n return (x > 0 ? x / y : (x - y + 1) / y);\n}\ntemplate <typename T> int popcnt(T x) {\n return __builtin_popcountll(x);\n}\ntemplate <typename T> int topbit(T x) {\n return (x == 0 ? -1 : 63 - __builtin_clzll(x));\n}\ntemplate <typename T> int lowbit(T x) {\n return (x == 0 ? -1 : __builtin_ctzll(x));\n}\n\ntemplate <class T, class U>\nostream &operator<<(ostream &os, const pair<T, U> &p) {\n os << \"P(\" << p.first << \", \" << p.second << \")\";\n return os;\n}\ntemplate <typename T> ostream &operator<<(ostream &os, const vector<T> &vec) {\n os << \"{\";\n for (int i = 0; i < vec.size(); i++) {\n os << vec[i] << (i + 1 == vec.size() ? \"\" : \", \");\n }\n os << \"}\";\n return os;\n}\ntemplate <typename T, typename U>\nostream &operator<<(ostream &os, const map<T, U> &map_var) {\n os << \"{\";\n for (auto itr = map_var.begin(); itr != map_var.end(); itr++) {\n os << \"(\" << itr->first << \", \" << itr->second << \")\";\n itr++;\n if (itr != map_var.end())\n os << \", \";\n itr--;\n }\n os << \"}\";\n return os;\n}\ntemplate <typename T> ostream &operator<<(ostream &os, const set<T> &set_var) {\n os << \"{\";\n for (auto itr = set_var.begin(); itr != set_var.end(); itr++) {\n os << *itr;\n ++itr;\n if (itr != set_var.end())\n os << \", \";\n itr--;\n }\n os << \"}\";\n return os;\n}\n#ifdef LOCAL\n#define show(...) _show(0, #__VA_ARGS__, __VA_ARGS__)\n#else\n#define show(...) true\n#endif\ntemplate <typename T> void _show(int i, T name) {\n cerr << '\\n';\n}\ntemplate <typename T1, typename T2, typename... T3>\nvoid _show(int i, const T1 &a, const T2 &b, const T3 &...c) {\n for (; a[i] != ',' && a[i] != '\\0'; i++)\n cerr << a[i];\n cerr << \":\" << b << \" \";\n _show(i + 1, a, c...);\n}\n\n/**\n * @brief template\n */\n\nint main() {\nwhile(1) {\n int MAXS = 4010;\n int N;\n cin >> N;\n if (N == 0) return 0;\n vector<int> A(N), B(N);\n rep(i,0,N) cin >> A[i] >> B[i], A[i] += 2000, B[i] += 2000;\n auto Calc = [&](int L, int R, double X) -> double {\n return (double)B[L]+((double)(B[R]-B[L])/(double)(A[R]-A[L]))*(X-(double)A[L]);\n };\n auto Calc2 = [&](int L, int R, int X) -> int {\n return B[L]+floor((B[R]-B[L])*(X-A[L]),A[R]-A[L]);\n };\n auto Calc3 = [&](int L, int R, int X) -> int {\n return B[L]+ceil((B[R]-B[L])*(X-A[L]),A[R]-A[L]);\n };\n int ANS = 0;\n rep(i,0,MAXS) {\n vector<pair<double,int>> V;\n rep(j,0,N) {\n int L = j, R = (j+1) % N;\n if (A[L] == A[R]) continue;\n if (A[L] > A[R]) swap(L,R);\n if (!(A[L] <= i && i < A[R])) continue;\n double X = (double)i+0.5;\n V.push_back({Calc(L,R,X),j});\n }\n if (V.empty()) continue;\n sort(ALL(V));\n vector<int> IMOS(MAXS,0);\n rep(j,0,V.size()) {\n int L = V[j].second, R = (L+1) % N;\n if (A[L] > A[R]) swap(L,R);\n if (j % 2 == 0) {\n int Y = min(Calc2(L,R,i),Calc2(L,R,i+1));\n IMOS[Y]++;\n }\n else {\n int Y = max(Calc3(L,R,i),Calc3(L,R,i+1));\n IMOS[Y]--;\n }\n }\n rep(j,0,MAXS-1) IMOS[j+1] += IMOS[j];\n rep(j,0,MAXS) if (IMOS[j] >= 1) ANS++;\n }\n cout << ANS << endl;\n}\n}", "accuracy": 1, "time_ms": 320, "memory_kb": 3372, "score_of_the_acc": -0.1024, "final_rank": 7 }, { "submission_id": "aoj_1242_9593999", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n#define x first\n#define y second\nusing Point = pair<int, int>;\n\nconst double EPS = 1e-5;\n\nint main() {\n int n;\n while (cin >> n && n > 0) {\n vector<Point> polygon(n);\n for (auto& p : polygon) cin >> p.x >> p.y;\n\n int area2 = 0;\n for (int i = n - 1, j = 0; j < n; i = j++) {\n const Point p = polygon[i];\n const Point q = polygon[j];\n area2 += p.x * q.y - p.y * q.x;\n }\n if (area2 < 0) reverse(polygon.begin(), polygon.end());\n\n unordered_map<int, vector<pair<int, int>>> events_by_y;\n\n for (int i = n - 1, j = 0; j < n; i = j++) {\n const Point p = polygon[i];\n const Point q = polygon[j];\n if (p.y == q.y) continue;\n\n const auto [min_y, max_y] = minmax(p.y, q.y);\n const double slope = (p.x - q.x) / (double)(p.y - q.y);\n\n for (int y = min_y; y < max_y; y++) {\n const double a = p.x + slope * (y - p.y);\n const double b = p.x + slope * (y + 1 - p.y);\n\n if (p.y > q.y) {\n const int x = floor(min(a, b) + EPS);\n events_by_y[y].emplace_back(x, 1);\n } else {\n const int x = ceil(max(a, b) - EPS);\n events_by_y[y].emplace_back(x, -1);\n }\n }\n }\n\n int ans = 0;\n\n for (auto& [_, events] : events_by_y) {\n sort(events.begin(), events.end());\n\n int count = 0;\n int prev = 0;\n\n for (const auto& [x, d] : events) {\n if (count == 0) prev = x;\n count += d;\n if (count == 0) {\n ans += x - prev;\n }\n }\n }\n\n cout << ans << endl;\n }\n}", "accuracy": 1, "time_ms": 80, "memory_kb": 7632, "score_of_the_acc": -0.38, "final_rank": 12 }, { "submission_id": "aoj_1242_9593993", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n#define x first\n#define y second\nusing Point = pair<int, int>;\n\ndouble fix_integer(const double x) { return fabs(round(x) - x) < 1e-6 ? round(x) : x; }\n\nint main() {\n int n;\n while (cin >> n && n > 0) {\n vector<Point> polygon(n);\n for (auto& p : polygon) {\n cin >> p.x >> p.y;\n /*p.x += 10;*/\n /*p.y += 10;*/\n /*p.x += 2000;*/\n /*p.y += 2000;*/\n }\n\n int area2 = 0;\n for (int i = n - 1, j = 0; j < n; i = j++) {\n const Point p = polygon[i];\n const Point q = polygon[j];\n area2 += p.x * q.y - p.y * q.x;\n }\n if (area2 < 0) reverse(polygon.begin(), polygon.end());\n\n unordered_map<int, vector<pair<int, int>>> events_by_y;\n\n for (int i = n - 1, j = 0; j < n; i = j++) {\n const Point p = polygon[i];\n const Point q = polygon[j];\n if (p.y == q.y) continue;\n\n const auto [min_y, max_y] = minmax(p.y, q.y);\n const double slope = (p.x - q.x) / (double)(p.y - q.y);\n\n for (int y = min_y; y < max_y; y++) {\n const double a = fix_integer(p.x + slope * (y - p.y));\n const double b = fix_integer(p.x + slope * (y + 1 - p.y));\n\n if (p.y > q.y) {\n const int x = floor(min(a, b));\n events_by_y[y].emplace_back(x, 1);\n } else {\n const int x = ceil(max(a, b));\n events_by_y[y].emplace_back(x, -1);\n }\n }\n }\n\n int ans = 0;\n\n for (auto& [_, events] : events_by_y) {\n /*const auto xs2 = y_events2[y];*/\n /*/*sort(xs1.begin(), xs1.end());*/\n /*/*sort(xs2.begin(), xs2.end());*/\n /*const int n = xs1.size();*/\n /*const int m = xs2.size();*/\n /*assert(n == m);*/\n /**/\n /*vector<pair<int, int>> events;*/\n /**/\n /*for (int i = 0; i < n; i++) {*/\n /* const auto [l1, l2] = xs1[i];*/\n /* const int enter = floor(min(l1, l2));*/\n /* events.emplace_back(enter, 1);*/\n /**/\n /* const auto [r1, r2] = xs2[i];*/\n /* const int exit = ceil(max(r1, r2));*/\n /* events.emplace_back(exit, -1);*/\n /*}*/\n\n sort(events.begin(), events.end());\n\n int count = 0;\n int prev = 0;\n\n for (const auto& [x, d] : events) {\n if (count == 0) prev = x;\n count += d;\n if (count == 0) {\n ans += x - prev;\n }\n }\n assert(count == 0);\n }\n\n cout << ans << endl;\n }\n}", "accuracy": 1, "time_ms": 100, "memory_kb": 7800, "score_of_the_acc": -0.3996, "final_rank": 14 }, { "submission_id": "aoj_1242_9593923", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n#define x first\n#define y second\nusing Point = pair<int, int>;\n\ndouble fix_integer(const double x) { return fabs(round(x) - x) < 1e-6 ? round(x) : x; }\n\nint main() {\n int n;\n while (cin >> n && n > 0) {\n /*cerr << \"------------------\" << endl;*/\n vector<Point> polygon(n);\n for (auto& p : polygon) {\n cin >> p.x >> p.y;\n p.x += 10;\n p.y += 10;\n /*p.x += 2000;*/\n /*p.y += 2000;*/\n }\n\n // todo: it shouldn't be necessary to make it clock-wise.\n int area = 0;\n for (int i = n - 1, j = 0; j < n; i = j++) {\n const Point p = polygon[i];\n const Point q = polygon[j];\n area += p.x * q.y - p.y * q.x;\n }\n if (area < 0) reverse(polygon.begin(), polygon.end());\n\n /*cerr << \"polygon(\";*/\n /*bool fuck = false;*/\n /*for (const Point& p : polygon) {*/\n /* if (fuck) cerr << \",\";*/\n /* cerr << \"(\" << p.x << \",\" << p.y << \")\";*/\n /* fuck = true;*/\n /*}*/\n /*cerr << \")\" << endl;*/\n\n unordered_map<int, vector<pair<double, double>>> y_events;\n unordered_map<int, vector<pair<double, double>>> y_events2;\n\n for (int i = 0; i < (int)polygon.size(); i++) {\n const Point p = polygon[i];\n const Point q = polygon[(i + 1) % polygon.size()];\n if (p.y == q.y) continue;\n\n const auto [min_y, max_y] = minmax(p.y, q.y);\n const double slope = (p.x - q.x) / (double)(p.y - q.y);\n\n for (int y = min_y; y < max_y; y++) {\n if (p.y > q.y) {\n y_events[y].emplace_back(fix_integer(p.x + slope * (y - p.y)),\n fix_integer(p.x + slope * (y + 1 - p.y)));\n } else {\n y_events2[y].emplace_back(fix_integer(p.x + slope * (y - p.y)),\n fix_integer(p.x + slope * (y + 1 - p.y)));\n }\n }\n }\n\n int ans = 0;\n\n for (auto& [y, xs1] : y_events) {\n const auto xs2 = y_events2[y];\n /*sort(xs1.begin(), xs1.end());*/\n /*sort(xs2.begin(), xs2.end());*/\n const int n = xs1.size();\n const int m = xs2.size();\n assert(n == m);\n\n vector<pair<int, int>> events;\n\n for (int i = 0; i < n; i++) {\n const auto [l1, l2] = xs1[i];\n const int enter = floor(min(l1, l2));\n events.emplace_back(enter, 1);\n\n const auto [r1, r2] = xs2[i];\n const int exit = ceil(max(r1, r2));\n events.emplace_back(exit, -1);\n }\n\n sort(events.begin(), events.end());\n\n int count = 0;\n int prev = 0;\n\n for (const auto& [x, d] : events) {\n if (count == 0) prev = x;\n /*cerr << y << \" \" << x << \" \" << d << endl;*/\n count += d;\n /*assert(count >= 0);*/\n if (count == 0) {\n /*fprintf(stderr, \"polygon((%d,%d),(%d,%d),(%d,%d),(%d,%d))\\n\", prev, y, x, y,\n * x,*/\n /* y + 1, prev, y + 1);*/\n ans += x - prev;\n }\n }\n /*assert(count == 0);*/\n }\n\n cout << ans << endl;\n }\n}", "accuracy": 1, "time_ms": 130, "memory_kb": 12288, "score_of_the_acc": -0.7751, "final_rank": 18 }, { "submission_id": "aoj_1242_8291226", "code_snippet": "#include <iostream>\n#include <vector>\n#include <algorithm>\nusing namespace std;\n\nstruct Point {\n\tlong double px, py;\n};\n\nlong double Intersect(Point p1, Point p2, long double ux) {\n\tif (p1.px > p2.px) swap(p1, p2);\n\tif (ux <= p1.px || p2.px <= ux) return -1e9;\n\tlong double perc = (ux - p1.px) / (p2.px - p1.px);\n\treturn p1.py + (p2.py - p1.py) * perc;\n}\n\nint N;\nPoint G[109];\n\nint overlap(vector<pair<int, int>> L1) {\n\tvector<int> Zahyou;\n\tfor (int i = 0; i < L1.size(); i++) Zahyou.push_back(L1[i].first);\n\tfor (int i = 0; i < L1.size(); i++) Zahyou.push_back(L1[i].second);\n\tsort(Zahyou.begin(), Zahyou.end());\n\tZahyou.erase(unique(Zahyou.begin(), Zahyou.end()), Zahyou.end());\n\n\t// Imos Method\n\tvector<int> sum(Zahyou.size() + 2, 0);\n\tfor (int i = 0; i < L1.size(); i++) {\n\t\tint pos1 = lower_bound(Zahyou.begin(), Zahyou.end(), L1[i].first) - Zahyou.begin();\n\t\tint pos2 = lower_bound(Zahyou.begin(), Zahyou.end(), L1[i].second) - Zahyou.begin();\n\t\tsum[pos1] += 1;\n\t\tsum[pos2] -= 1;\n\t}\n\tfor (int i = 1; i <= Zahyou.size(); i++) sum[i] += sum[i - 1];\n\n\t// Return\n\tint Return = 0;\n\tfor (int i = 0; i < Zahyou.size(); i++) {\n\t\tif (sum[i] == 0) continue;\n\t\tReturn += (Zahyou[i + 1] - Zahyou[i]);\n\t}\n\treturn Return;\n}\n\nint solve() {\n\tint Answer = 0;\n\tfor (int i = -2000; i <= 1999; i++) {\n\t\tvector<long double> P1, P2;\n\t\tfor (int j = 0; j < N; j++) {\n\t\t\tlong double r1 = Intersect(G[j], G[(j + 1) % N], 1.0L * i + 0.00001L);\n\t\t\tlong double r2 = Intersect(G[j], G[(j + 1) % N], 1.0L * i + 0.99999L);\n\t\t\tif (r1 >= -1e8) P1.push_back(r1);\n\t\t\tif (r2 >= -1e8) P2.push_back(r2);\n\t\t}\n\t\tif (P1.size() == 0 && P2.size() == 0) continue;\n\t\tsort(P1.begin(), P1.end());\n\t\tsort(P2.begin(), P2.end());\n\t\tvector<pair<int, int>> L1;\n\t\tfor (int j = 0; j < P1.size(); j += 2) {\n\t\t\tint cl = (int)(P1[j + 0] + 3000.0L);\n\t\t\tint cr = (int)(P1[j + 1] + 3000.0L + 0.999999999L);\n\t\t\tint dl = (int)(P2[j + 0] + 3000.0L);\n\t\t\tint dr = (int)(P2[j + 1] + 3000.0L + 0.999999999L);\n\t\t\tL1.push_back(make_pair(min(cl, dl), max(cr, dr)));\n\t\t}\n\t\tAnswer += overlap(L1);\n\t}\n\treturn Answer;\n}\n\nint main() {\n\twhile (true) {\n\t\tcin >> N; if (N == 0) break;\n\t\tfor (int i = 0; i < N; i++) cin >> G[i].px >> G[i].py;\n\t\tcout << solve() << endl;\n\t}\n\treturn 0;\n}", "accuracy": 1, "time_ms": 220, "memory_kb": 3328, "score_of_the_acc": -0.0694, "final_rank": 5 }, { "submission_id": "aoj_1242_7220996", "code_snippet": "//#define _GLIBCXX_DEBUG\n\n#include<bits/stdc++.h>\nusing namespace std;\n\n#define endl '\\n'\n#define lfs cout<<fixed<<setprecision(10)\n#define ALL(a) (a).begin(),(a).end()\n#define ALLR(a) (a).rbegin(),(a).rend()\n#define UNIQUE(a) (a).erase(unique((a).begin(),(a).end()),(a).end())\n#define spa << \" \" <<\n#define fi first\n#define se second\n#define MP make_pair\n#define MT make_tuple\n#define PB push_back\n#define EB emplace_back\n#define rep(i,n,m) for(ll i = (n); i < (ll)(m); i++)\n#define rrep(i,n,m) for(ll i = (ll)(m) - 1; i >= (ll)(n); i--)\nusing ll = long long;\nusing ld = long double;\nconst ll MOD1 = 1e9+7;\nconst ll MOD9 = 998244353;\nconst ll INF = 1e18;\nusing P = pair<ll, ll>;\ntemplate<typename T> using PQ = priority_queue<T>;\ntemplate<typename T> using QP = priority_queue<T,vector<T>,greater<T>>;\ntemplate<typename T1, typename T2>bool chmin(T1 &a,T2 b){if(a>b){a=b;return true;}else return false;}\ntemplate<typename T1, typename T2>bool chmax(T1 &a,T2 b){if(a<b){a=b;return true;}else return false;}\nll median(ll a,ll b, ll c){return a+b+c-max({a,b,c})-min({a,b,c});}\nvoid ans1(bool x){if(x) cout<<\"Yes\"<<endl;else cout<<\"No\"<<endl;}\nvoid ans2(bool x){if(x) cout<<\"YES\"<<endl;else cout<<\"NO\"<<endl;}\nvoid ans3(bool x){if(x) cout<<\"Yay!\"<<endl;else cout<<\":(\"<<endl;}\ntemplate<typename T1,typename T2>void ans(bool x,T1 y,T2 z){if(x)cout<<y<<endl;else cout<<z<<endl;}\ntemplate<typename T1,typename T2,typename T3>void anss(T1 x,T2 y,T3 z){ans(x!=y,x,z);};\ntemplate<typename T>void debug(const T &v,ll h,ll w,string sv=\" \"){for(ll i=0;i<h;i++){cout<<v[i][0];for(ll j=1;j<w;j++)cout<<sv<<v[i][j];cout<<endl;}};\ntemplate<typename T>void debug(const T &v,ll n,string sv=\" \"){if(n!=0)cout<<v[0];for(ll i=1;i<n;i++)cout<<sv<<v[i];cout<<endl;};\ntemplate<typename T>void debug(const vector<T>&v){debug(v,v.size());}\ntemplate<typename T>void debug(const vector<vector<T>>&v){for(auto &vv:v)debug(vv,vv.size());}\ntemplate<typename T>void debug(stack<T> st){while(!st.empty()){cout<<st.top()<<\" \";st.pop();}cout<<endl;}\ntemplate<typename T>void debug(queue<T> st){while(!st.empty()){cout<<st.front()<<\" \";st.pop();}cout<<endl;}\ntemplate<typename T>void debug(deque<T> st){while(!st.empty()){cout<<st.front()<<\" \";st.pop_front();}cout<<endl;}\ntemplate<typename T>void debug(PQ<T> st){while(!st.empty()){cout<<st.top()<<\" \";st.pop();}cout<<endl;}\ntemplate<typename T>void debug(QP<T> st){while(!st.empty()){cout<<st.top()<<\" \";st.pop();}cout<<endl;}\ntemplate<typename T>void debug(const set<T>&v){for(auto z:v)cout<<z<<\" \";cout<<endl;}\ntemplate<typename T>void debug(const multiset<T>&v){for(auto z:v)cout<<z<<\" \";cout<<endl;}\ntemplate<typename T,size_t size>void debug(const array<T, size> &a){for(auto z:a)cout<<z<<\" \";cout<<endl;}\ntemplate<typename T,typename V>void debug(const map<T,V>&v){for(auto z:v)cout<<\"[\"<<z.first<<\"]=\"<<z.second<<\",\";cout<<endl;}\ntemplate<typename T>vector<vector<T>>vec(ll x, ll y, T w){vector<vector<T>>v(x,vector<T>(y,w));return v;}\nll gcd(ll x,ll y){ll r;while(y!=0&&(r=x%y)!=0){x=y;y=r;}return y==0?x:y;}\n//vector<ll>dx={1,-1,0,0,1,1,-1,-1};vector<ll>dy={0,0,1,-1,1,-1,1,-1};\ntemplate<typename T>vector<T> make_v(size_t a,T b){return vector<T>(a,b);}\ntemplate<typename... Ts>auto make_v(size_t a,Ts... ts){return vector<decltype(make_v(ts...))>(a,make_v(ts...));}\ntemplate<typename T1, typename T2>ostream &operator<<(ostream &os, const pair<T1, T2>&p){return os << p.first << \" \" << p.second;}\ntemplate<typename T>ostream &operator<<(ostream &os, const vector<T> &v){for(auto &z:v)os << z << \" \";cout<<\"|\"; return os;}\ntemplate<typename T>void rearrange(vector<int>&ord, vector<T>&v){\n auto tmp = v;\n for(int i=0;i<tmp.size();i++)v[i] = tmp[ord[i]];\n}\ntemplate<typename Head, typename... Tail>void rearrange(vector<int>&ord,Head&& head, Tail&&... tail){\n rearrange(ord, head);\n rearrange(ord, tail...);\n}\ntemplate<typename T> vector<int> ascend(const vector<T>&v){\n vector<int>ord(v.size());iota(ord.begin(),ord.end(),0);\n sort(ord.begin(),ord.end(),[&](int i,int j){return v[i]<v[j];});\n return ord;\n}\ntemplate<typename T> vector<int> descend(const vector<T>&v){\n vector<int>ord(v.size());iota(ord.begin(),ord.end(),0);\n sort(ord.begin(),ord.end(),[&](int i,int j){return v[i]>v[j];});\n return ord;\n}\ntemplate<typename T> vector<T> inv_perm(const vector<T>&ord){\n vector<T>inv(ord.size());\n for(int i=0;i<ord.size();i++)inv[ord[i]] = i;\n return inv;\n}\nll FLOOR(ll n,ll div){assert(div>0);return n>=0?n/div:(n-div+1)/div;}\nll CEIL(ll n,ll div){assert(div>0);return n>=0?(n+div-1)/div:n/div;}\nll digitsum(ll n){ll ret=0;while(n){ret+=n%10;n/=10;}return ret;}\nll modulo(ll n,ll d){return (n%d+d)%d;};\ntemplate<typename T>T min(const vector<T>&v){return *min_element(v.begin(),v.end());}\ntemplate<typename T>T max(const vector<T>&v){return *max_element(v.begin(),v.end());}\ntemplate<typename T>T acc(const vector<T>&v){return accumulate(v.begin(),v.end(),T(0));};\ntemplate<typename T>T reverse(const T &v){return T(v.rbegin(),v.rend());};\n//mt19937 mt(chrono::steady_clock::now().time_since_epoch().count());\nint popcount(ll x){return __builtin_popcountll(x);};\nint poplow(ll x){return __builtin_ctzll(x);};\nint pophigh(ll x){return 63 - __builtin_clzll(x);};\ntemplate<typename T>T poll(queue<T> &q){auto ret=q.front();q.pop();return ret;};\ntemplate<typename T>T poll(priority_queue<T> &q){auto ret=q.top();q.pop();return ret;};\ntemplate<typename T>T poll(QP<T> &q){auto ret=q.top();q.pop();return ret;};\ntemplate<typename T>T poll(stack<T> &s){auto ret=s.top();s.pop();return ret;};\nll MULT(ll x,ll y){if(LLONG_MAX/x<=y)return LLONG_MAX;return x*y;}\nll POW2(ll x, ll k){ll ret=1,mul=x;while(k){if(mul==LLONG_MAX)return LLONG_MAX;if(k&1)ret=MULT(ret,mul);mul=MULT(mul,mul);k>>=1;}return ret;}\nll POW(ll x, ll k){ll ret=1;for(int i=0;i<k;i++){if(LLONG_MAX/x<=ret)return LLONG_MAX;ret*=x;}return ret;}\ntemplate< typename T = int >\nstruct edge {\n int to;\n T cost;\n int id;\n edge():id(-1){};\n edge(int to, T cost = 1, int id = -1):to(to), cost(cost), id(id){}\n operator int() const { return to; }\n};\n\ntemplate<typename T>\nusing Graph = vector<vector<edge<T>>>;\ntemplate<typename T>\nGraph<T>revgraph(const Graph<T> &g){\n Graph<T>ret(g.size());\n for(int i=0;i<g.size();i++){\n for(auto e:g[i]){\n int to = e.to;\n e.to = i;\n ret[to].push_back(e);\n }\n }\n return ret;\n}\ntemplate<typename T>\nGraph<T> readGraph(int n,int m,int indexed=1,bool directed=false,bool weighted=false){\n Graph<T> ret(n);\n for(int es = 0; es < m; es++){\n int u,v;\n T w=1;\n cin>>u>>v;u-=indexed,v-=indexed;\n if(weighted)cin>>w;\n ret[u].emplace_back(v,w,es);\n if(!directed)ret[v].emplace_back(u,w,es);\n }\n return ret;\n}\ntemplate<typename T>\nGraph<T> readParent(int n,int indexed=1,bool directed=true){\n Graph<T>ret(n);\n for(int i=1;i<n;i++){\n int p;cin>>p;\n p-=indexed;\n ret[p].emplace_back(i);\n if(!directed)ret[i].emplace_back(p);\n }\n return ret;\n}\n\ntemplate <typename F>\nclass\n#if defined(__has_cpp_attribute) && __has_cpp_attribute(nodiscard)\n [[nodiscard]]\n#endif // defined(__has_cpp_attribute) && __has_cpp_attribute(nodiscard)\nFixPoint final : private F\n{\npublic:\n template <typename G>\n explicit constexpr FixPoint(G&& g) noexcept\n : F{std::forward<G>(g)}\n {}\n\n template <typename... Args>\n constexpr decltype(auto)\n operator()(Args&&... args) const\n#if !defined(__GNUC__) || defined(__clang__) || __GNUC__ >= 9\n noexcept(noexcept(F::operator()(std::declval<FixPoint>(), std::declval<Args>()...)))\n#endif // !defined(__GNUC__) || defined(__clang__) || __GNUC__ >= 9\n {\n return F::operator()(*this, std::forward<Args>(args)...);\n }\n}; // class FixPoint\n\n\n#if defined(__cpp_deduction_guides)\ntemplate <typename F>\nFixPoint(F&&)\n-> FixPoint<std::decay_t<F>>;\n#endif // defined(__cpp_deduction_guides)\n\n\nnamespace\n{\n template <typename F>\n#if !defined(__has_cpp_attribute) || !__has_cpp_attribute(nodiscard)\n # if defined(__GNUC__) && (__GNUC__ > 3 || __GNUC__ == 3 && __GNUC_MINOR__ >= 4)\n __attribute__((warn_unused_result))\n# elif defined(_MSC_VER) && _MSC_VER >= 1700 && defined(_Check_return_)\n _Check_return_\n# endif // defined(__GNUC__) && (__GNUC__ > 3 || __GNUC__ == 3 && __GNUC_MINOR__ >= 4)\n#endif // !defined(__has_cpp_attribute) || !__has_cpp_attribute(nodiscard)\n inline constexpr decltype(auto)\n makeFixPoint(F&& f) noexcept\n {\n return FixPoint<std::decay_t<F>>{std::forward<std::decay_t<F>>(f)};\n }\n} // namespace\n\nusing Real = long double;\nusing Point = complex<Real>;\nconst Real EPS = 1e-10, PI = acos((Real)(-1.0));\n\ninline bool eq(Real a, Real b) { return fabs(b - a) < EPS; }\ninline bool eq(Point a, Point b) { return fabs(b - a) < EPS; }\n\nPoint operator*(const Point &p, const Real &d) {\n return Point(real(p) * d, imag(p) * d);\n}\n\nistream &operator>>(istream &is, Point &p) {\n Real a, b;\n is >> a >> b;\n p = Point(a, b);\n return is;\n}\n\nostream &operator<<(ostream &os, Point &p) {\n return os << fixed << setprecision(10) << p.real() << \" \" << p.imag();\n}\n\nPoint rotate(Real theta, const Point &p) {\n return Point(cos(theta) * p.real() - sin(theta) * p.imag(), sin(theta) * p.real() + cos(theta) * p.imag());\n}\n\nnamespace std {\n bool operator<(const Point& a, const Point& b) {\n return !eq(a.real(), b.real()) ? a.real() < b.real() : a.imag() < b.imag();\n }\n}\n\nstruct Line {\n Point a, b;\n Line() = default;\n Line(Point a, Point b): a(a), b(b) {}\n\n friend ostream &operator<<(ostream &os, Line &p) {\n return os << p.a << \" to \" << p.b;\n }\n\n friend istream &operator>>(istream &is, Line &a) {\n return is >> a.a >> a.b;\n }\n};\n\nstruct Circle {\n Point c;\n Real r;\n Circle() = default;\n Circle(Point c, Real r): c(c), r(r) {}\n\n friend ostream &operator<<(ostream &os, Circle &c) {\n return os << c.c << \": \" << c.r;\n }\n\n friend istream &operator>>(istream &is, Circle &c) {\n return is >> c.c >> c.r;\n }\n};\n\nReal cross(const Point &a, const Point &b) {\n return real(a) * imag(b) - real(b) * imag(a);\n}\n\nReal dot(const Point &a, const Point &b) {\n return real(a) * real(b) + imag(a) * imag(b);\n}\n\nstruct Segment : Line {\n Segment() = default;\n Segment(Point a, Point b) : Line(a, b) {}\n};\n\nint ccw(const Point &a, Point b, Point c) {\n b = b - a, c = c - a;\n if (cross(b, c) > EPS) return +1; // COUNTER_CLOCKWISE\n if (cross(b, c) < -EPS) return -1; // CLOCKWISE\n if (dot(b, c) < 0) return +2; // ONLINE_BACK\n if (norm(b) < norm(c)) return -2; // ONLINE_FRONT\n return 0; // ON_SEGMENT\n}\n\nbool intersect(const Segment &s, const Point &p) {\n return ccw(s.a, s.b, p) == 0;\n}\n\nbool intersect(const Segment &s, const Segment&t) {\n 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;\n}\n\nbool intersect(const Line &l, const Segment& s) {\n return cross(l.b - l.a, s.a - l.a) * cross(l.b - l.a, s.b - l.a) < EPS;\n}\n\nint intersect(const Circle &c, const Circle &d) {\n Real dis = fabs(c.c - d.c);\n\n if (dis > c.r + d.r + EPS) return 4;\n else if (eq(dis, c.r + d.r)) return 3;\n else if (eq(dis, abs(c.r - d.r))) return 1;\n else if (dis + min(c.r, d.r) < max(c.r, d.r) - EPS) return 0;\n else return 2;\n}\n\nbool parallel(const Line &a, const Line &b) {\n return abs(cross(a.b - a.a, b.b - b.a)) < EPS;\n}\n\nbool orthogonal(const Line &a, const Line &b) {\n return abs(dot(a.b - a.a, b.b - b.a)) < EPS;\n}\n\nPoint projection(const Line& s, const Point &p) {\n double t = dot(p - s.a, s.b - s.a) / norm(s.b - s. a);\n return s.a + (s.b - s.a) * t;\n}\n\nPoint projection(const Segment& l, const Point &p) {\n return projection(Line(l), p);\n}\n\nPoint reflection(const Line &l, const Point &p) {\n Point r = projection(l, p);\n return p + (r - p) * 2;\n}\n\nReal distance(const Line &l, const Point &p) {\n Point r = projection(l, p);\n return fabs(p - r);\n}\n\nReal distance(const Segment&s, const Point &p) {\n Point r = projection(s, p);\n if (intersect(s, r)) return abs(r - p);\n return min(abs(s.a - p), abs(s.b - p));\n}\n\nReal distance(const Segment& a, const Segment& b) {\n if (intersect(a, b)) return 0;\n return min({\n distance(a, b.a),\n distance(a, b.b),\n distance(b, a.a),\n distance(b, a.b)\n });\n}\n\nbool contains(const Circle &c, const Point &p) {\n if (fabs(c.c - p) < c.r + EPS) return true;\n return false;\n}\n\nPoint crosspoint(const Line &l, const Line &m) {\n Real A = cross(l.b - l.a, m.b - m.a);\n Real B = cross(l.b - l.a, l.b - m.a);\n if (eq(abs(A), 0.0) && eq(abs(B), 0.0)) return m.a;\n return m.a + (m.b - m.a) * B / A;\n}\n\nPoint crosspoint(const Segment& l, const Segment& m) {\n return crosspoint(Line(l), Line(m));\n}\n\nLine bisector_of_angle(const Point& a, const Point& b, const Point& c) {\n Real d_ab = fabs(a - b);\n Real d_bc = fabs(b - c);\n Point c_ = b + (c - b) * d_ab / d_bc;\n\n return Line(b, (a + c_) * 0.5);\n}\n\nLine bisector(const Point& a, const Point& b) {\n Point c = (a + b) * 0.5;\n Point d = c + rotate(PI / 2, b - c);\n\n return Line(c, d);\n}\n\nint intersect(const Circle &c, const Line &l) {\n Real d = distance(l, c.c);\n\n if (d > c.r + EPS) return 0;\n if (eq(d, c.r)) return 1;\n return 2;\n}\n\npair<Point, Point> crosspoint(const Circle& c, const Line& l) {\n Real d = distance(l, c.c);\n\n Point p = projection(l, c.c);\n if (intersect(c, l) == 1) return { p, p };\n\n Real d2 = sqrt(c.r * c.r - d * d);\n Point v = (l.a - l.b) * (1.0 / fabs(l.a - l.b));\n\n return { p + d2 * v, p - d2 * v };\n}\n\nvector<Point> crosspoint(const Circle &c, const Segment &s) {\n int isect = intersect(c, Line(s));\n if (isect == 0) return {};\n\n auto [p1, p2] = crosspoint(c, Line(s));\n\n vector<Point> res;\n if (ccw(s.a, s.b, p1) == 0) res.push_back(p1);\n if (ccw(s.a, s.b, p2) == 0) res.push_back(p2);\n\n if (res.size() == 2 && ccw(s.a, p1, p2) == 0) swap(res[0], res[1]);\n return res;\n}\n\npair<Point, Point> crosspoint(const Circle &c1, const Circle &c2) {\n Real d = fabs(c1.c - c2.c);\n Real x1 = (c1.r * c1.r + d * d - c2.r * c2.r) / 2 / d;\n Real y = sqrt(c1.r * c1.r - x1 * x1);\n\n Point base = (c2.c * x1 + c1.c * (d - x1)) * (1.0 / d);\n Point v = rotate(PI / 2, c1.c - base);\n v = v / fabs(v);\n\n return { base + v * y, base - v * y };\n}\n\npair<Point, Point> tangent(const Circle &c, const Point &p) {\n Real d = fabs(c.c - p);\n Real d2 = sqrt(d * d - c.r * c.r);\n Real a = acos(d2 / d);\n\n\n Point q = c.c - p;\n q = q / fabs(q);\n\n// cout << d << \" \" << d2 << \" \" << a << \" \" << q << endl;\n return {\n p + rotate(a, q) * d2,\n p + rotate(-a, q) * d2\n };\n}\n\nvector<Segment> common_tangent(Circle c1, Circle c2) {\n int isect = intersect(c1, c2);\n Real d = fabs(c1.c - c2.c);\n Point v = (c2.c - c1.c) / d;\n\n vector<Segment> res;\n\n if (isect == 0) {\n return {};\n } else if (isect == 1) {\n auto [p1, p2] = crosspoint(c1, Line(c1.c, c2.c));\n if (eq(fabs(p2 - c2.c), c2.r)) swap(p1, p2);\n\n Point v = p1 + rotate(PI/2, c2.c - p1);\n return { Segment(p1, v) };\n } else if (isect == 2) {\n } else if (isect == 3) {\n auto [p1, p2] = crosspoint(c1, c2);\n res.emplace_back(p1, rotate(PI / 2, c1.c - p1) + p1);\n } else {\n Real a = d * c1.r / (c1.r + c2.r);\n Point p = c1.c + v * a;\n\n Real d1 = sqrt(a * a - c1.r * c1.r), d2 = sqrt(norm(p - c2.c) - c2.r * c2.r);\n Real theta = acos(d1 / a);\n\n// cout << d << endl;\n// cout << a << \" \" << d1 << \" \" << d2 << \" \" << theta << \": \" << p << endl;\n\n Point e = (c1.c - p) * (1.0 / fabs(c1.c - p));\n Point e1 = rotate(theta, e), e2 = rotate(-theta, e);\n\n res.emplace_back(p + e1 * d1, p - e1 * d2);\n res.emplace_back(p + e2 * d1, p - e2 * d2);\n }\n\n if (eq(c1.r, c2.r)) {\n Point e = rotate(PI / 2, v);\n res.push_back(Segment(c1.c + e * c1.r, c2.c + e * c1.r));\n res.push_back(Segment(c1.c - e * c1.r, c2.c - e * c1.r));\n return res;\n }\n\n if (c1.r > c2.r) swap(c1, c2);\n\n v = c1.c - c2.c;\n Point p = v * (1.0 / (c2.r - c1.r)) * c2.r + c2.c;\n\n auto [q1, q2] = tangent(c1, p);\n res.emplace_back(q1, crosspoint(c2, Line(p, q1)).first);\n res.emplace_back(q2, crosspoint(c2, Line(p, q2)).first);\n\n return res;\n}\n\nusing Polygon = vector<Point>;\n\nistream& operator>>(istream& is, Polygon &p) {\n for (int i = 0; i < p.size(); i++) is >> p[i];\n return is;\n}\n\nostream& operator<<(ostream& os, Polygon &p) {\n for (int i = 0; i < p.size(); i++) os << p[i] << endl;\n return os;\n}\n\nReal area(const Polygon& p) {\n Real res = 0;\n for (int i = 0; i < p.size(); i++) {\n res += cross(p[i], p[(i + 1) % p.size()]);\n }\n return abs(res)/2;\n}\n\nReal arcarea(const Circle &c, const Point &p1_, const Point &p2_) {\n Point p1 = p1_ - c.c, p2 = p2_ - c.c;\n Real a2 = atan2(imag(p2), real(p2));\n Point p = rotate(-a2, p1);\n Real a1 = atan2(imag(p), real(p));\n// cout << p1 << \" \" << p2 << \" --- \" << a2 << \" \" << a1 << endl;\n return - a1 / 2 * c.r * c.r;\n}\n\nint contains(Polygon g,Point p){\n int n=g.size();\n bool x=false;\n for(int i=0;i<n;i++){\n Point a=g[i]-p,b=g[(i+1)%n]-p;\n if(fabs(cross(a,b)) < EPS and dot(a,b) < EPS) return 1;\n if(a.imag()>b.imag()) swap(a,b);\n if(a.imag() < EPS and EPS < b.imag() and cross(a,b) > EPS ) {\n// cout << a << \" --- \" << b << \": \" << cross(a, b) << endl;\n x = !x;\n }\n }\n return (x?2:0);\n}\n\nPolygon convex_hull(Polygon Q) {\n sort(ALL(Q));\n\n Polygon upper({Q[0]}), lower({Q[0]});\n\n rep(i, 1, Q.size()) {\n while (upper.size() >= 2) {\n int _ccw = ccw(upper[upper.size() - 2], upper[upper.size() - 1], Q[i]);\n if (_ccw == 1) {\n upper.pop_back();\n } else {\n break;\n }\n }\n if (upper.size() <= 1) {\n upper.push_back(Q[i]);\n } else if (upper.size() > 1) {\n int _ccw = ccw(upper[upper.size() - 2], upper[upper.size() - 1], Q[i]);\n if (_ccw == -1 || _ccw == -2) {\n upper.push_back(Q[i]);\n }\n }\n }\n rep(i, 1, Q.size()) {\n while (lower.size() >= 2) {\n int _ccw = ccw(lower[lower.size() - 2], lower[lower.size() - 1], Q[i]);\n if (_ccw == -1) {\n lower.pop_back();\n } else {\n break;\n }\n }\n\n if (lower.size() <= 1) {\n lower.push_back(Q[i]);\n } else if (lower.size() > 1) {\n int _ccw = ccw(lower[lower.size() - 2], lower[lower.size() - 1], Q[i]);\n if (_ccw == 1 || _ccw == -2) {\n lower.push_back(Q[i]);\n }\n }\n }\n\n Polygon ret;\n\n rep(i, 0, lower.size()) {\n ret.push_back(lower[i]);\n// cout << lower[i] << endl;\n }\n\n// cout << \" | \" << endl;\n\n rrep(i, 0, upper.size()) {\n if (!eq(lower[0], upper[i]) && !eq(lower.back(), upper[i]))\n ret.push_back(upper[i]);\n// cout << upper[i] << endl;\n }\n\n int start = 0;\n\n// cout << ret.size() << endl;\n rep(i, 0, ret.size()) {\n if (ret[i].imag() < ret[start].imag() || (eq(ret[i].imag(), ret[start].imag()) && ret[i].real() < ret[start].real())) {\n start = i;\n }\n }\n\n// cout << ret.size() << endl;\n// rep(i, 0, ret.size()) {\n// auto p = ret[(i + start) % ret.size()];\n// cout << (int)p.real() << \" \" << (int)p.imag() << endl;\n// }\n//\n return ret;\n}\n\nReal closest_pair(vector<Point> &pts, int l, int r) {\n int m = (l + r) / 2;\n if (r - l <= 1) {\n return INF;\n } else if (r - l == 2) {\n if (imag(pts[l]) > imag(pts[l + 1])) swap(pts[l], pts[l + 1]);\n return fabs(pts[l] - pts[l + 1]);\n }\n\n Real mid = pts[m].real();\n Real d = min(closest_pair(pts, l, m), closest_pair(pts, m, r));\n\n inplace_merge(pts.begin() + l, pts.begin() + m, pts.begin() + r, [](const Point &a, const Point& b) {\n return a.imag() < b.imag();\n });\n\n vector<Point> ret;\n\n for (int i = l; i < r; i++) {\n if (fabs(pts[i].real() - mid) >= d) continue;\n\n for (int j = ret.size() - 1; j >= 0; j--) {\n if (fabs(imag(ret[j]) - imag(pts[i])) >= d) break;\n chmin(d, fabs(ret[j] - pts[i]));\n }\n\n ret.emplace_back(pts[i]);\n }\n\n return d;\n}\n\nbool contains(const Polygon &a, const Polygon &b) {\n rep(i, 0, b.size()) {\n if (!contains(a, b[i])) return false;\n }\n return true;\n}\n\n\nReal distance(const Polygon &a, const Point &p) {\n if (contains(a, p)) return 0;\n Real res = INF;\n rep(i, 0, a.size()) {\n Segment as(a[i], a[(i + 1) % a.size()]);\n chmin(res, distance(as, p));\n }\n return res;\n}\n\nReal distance(const Polygon &a, const Polygon &b) {\n// if (contains(a, b) || contains(b, a)) return 0;\n Real res = INF;\n rep(i, 0, a.size()) chmin(res, distance(b, a[i]));\n rep(i, 0, b.size()) chmin(res, distance(a, b[i]));\n return res;\n}\n\nstruct Point3d {\n Real x, y, z;\n Point3d() {}\n Point3d(Real x, Real y, Real z): x(x), y(y), z(z) {}\n};\n\nReal distance(const Point3d &a, const Point3d &b) {\n return sqrt(pow(a.x - b.x, 2) + pow(a.y - b.y, 2) + pow(a.z - b.z, 2));\n}\n\nvoid solve(int N) {\n Polygon Q(N);\n cin >> Q;\n\n using T = tuple<int, int>;\n ll res = 0;\n rep(y, -2000, 2000) {\n Segment low(Point(-3000, y), Point(3000, y)), high(Point(-3000, y + 1), Point(3000, y + 1));\n\n vector<T> cr;\n rep(i, 0, N) {\n Segment e(Q[i], Q[(i + 1) % N]);\n if (!intersect(low, e) || !intersect(high, e)) continue;\n\n auto cl = crosspoint(low, e), ch = crosspoint(high, e);\n Real x1 = floor(min(cl.real(), ch.real())), x2 = ceil(max(cl.real(), real(ch)));\n cr.emplace_back(x1, x2);\n }\n sort(ALL(cr));\n\n int last = -3000;\n for (int j = 0; j < cr.size(); j += 2) {\n auto [l1, r1] = cr[j];\n auto [l2, r2] = cr[j + 1];\n\n chmax(l1, last);\n\n res += r2 - l1;\n\n chmax(last, r2);\n }\n }\n cout << res << endl;\n}\n\nint main() {\n cin.tie();\n ios::sync_with_stdio(false);\n\n cout << fixed << setprecision(10);\n int N, M;\n while (cin >> N&& N) {\n solve(N);\n }\n}", "accuracy": 1, "time_ms": 440, "memory_kb": 3476, "score_of_the_acc": -0.1461, "final_rank": 8 }, { "submission_id": "aoj_1242_6977900", "code_snippet": "#pragma GCC optimize(\"Ofast\")\n#include <iostream>\n#include <vector>\n#include <algorithm>\n#include <map>\n#include <queue>\n#include <cstdio>\n#include <ctime>\n#include <assert.h>\n#include <chrono>\n#include <random>\n#include <numeric>\n#include <set>\n#include <deque>\n#include <stack>\n#include <sstream>\n#include <utility>\n#include <cstring>\n#include <unordered_map>\n#include <unordered_set>\n#include <tuple>\n#include <array>\n#include <bitset>\nusing namespace std;\ntypedef long long int ll;\ntypedef unsigned long long ull;\n\nmt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());\nll myRand(ll B) {\n return (ull)rng() % B;\n}\ninline double time() {\n return static_cast<long double>(chrono::duration_cast<chrono::nanoseconds>(chrono::steady_clock::now().time_since_epoch()).count()) * 1e-9;\n}\n\nconst double eps=1e-7;\n\nstruct point{\n double x,y;\n point() {}\n point(double x,double y): x(x), y(y) {}\n point& operator-=(const point &p){\n x -= p.x; y -= p.y;\n return *this;\n }\n point& operator+=(const point &p){\n x += p.x; y += p.y;\n return *this;\n }\n point& operator*=(double d){\n x *= d; y *= d;\n return *this;\n }\n point& operator/=(double d){\n x /= d; y /= d;\n return *this;\n }\n const point operator+ (const point& p) const;\n const point operator- (const point& p) const;\n const point operator* (double d) const;\n const point operator/ (double d) const;\n};\nconst point point::operator+ (const point& p) const{\n point res(*this); return res += p;\n}\nconst point point::operator- (const point& p) const{\n point res(*this); return res -= p;\n}\nconst point point::operator* (double d) const{\n point res(*this); return res *= d;\n}\nconst point point::operator/ (double d) const{\n point res(*this); return res /= d;\n}\n\nstruct line{\n point A,B;\n line() {}\n line(point A, point B): A(A), B(B) {}\n};\n\ndouble dot(point a,point b){\n return a.x*b.x+a.y*b.y;\n}\n\ndouble cross(point a,point b){\n return a.x*b.y-a.y*b.x;\n}\n\nint ccw(point a,point b,point c){\n b={b.x-a.x,b.y-a.y};\n c={c.x-a.x,c.y-a.y};\n if(cross(b,c)>eps)return 1; // a,b,c反時計回り\n if(cross(b,c)<-eps)return -1; // a,b,c時計周り\n if(dot(b,c)<-eps)return 2; // c,a,b 一直線\n if((b.x*b.x+b.y*b.y)<(c.x*c.x+c.y*c.y))return -2; // a,b,c 一直線\n return 0; // a,c,b 一直線\n}\n\n// 線分交差判定\n// 端点含む場合は等号外す(AOJ-2742)\nbool intersect(point a,point b,point c,point d){\n if(ccw(a,b,c)*ccw(a,b,d)<=0 and ccw(c,d,a)*ccw(c,d,b)<=0)return 1;\n else return 0;\n}\n\n// 交点の座標を返す\npoint Crosspoint(point a,point b,point c,point d){\n point cd={d.x-c.x,d.y-c.y};\n point ca={a.x-c.x,a.y-c.y};\n point cb={b.x-c.x,b.y-c.y};\n double d1=abs(cd.x*ca.y-cd.y*ca.x),d2=abs(cd.x*cb.y-cd.y*cb.x);\n double t=d1/(d1+d2);\n return {a.x+(b.x-a.x)*t,a.y+(b.y-a.y)*t};\n}\n\nvector<pair<int,int>> v[4040];\n\nint solve(int n){\n vector<point> p(n);\n for(int i=0;i<n;i++){\n cin >> p[i].x >> p[i].y;\n p[i].x += 2000;\n p[i].y += 2000;\n }\n vector<line> e(n);\n for(int i=0;i<n;i++){\n e[i].A = p[i];\n e[i].B = p[(i+1)%n];\n if(e[i].A.y > e[i].B.y) swap(e[i].A,e[i].B);\n for(int j=e[i].A.y;j<e[i].B.y;j++){\n auto p1 = Crosspoint(e[i].A, e[i].B, point(-100,j), point(10000,j));\n auto p2 = Crosspoint(e[i].A, e[i].B, point(-100,j+1), point(10000,j+1));\n int l = min(p1.x,p2.x)+eps;\n int r = max(p1.x,p2.x)+1-eps;\n v[j].push_back({l,r});\n }\n }\n int res = 0;\n for(int y=0;y<4040;y++){\n sort(v[y].begin(), v[y].end());\n int n = v[y].size();\n int r = -1;\n for(int i=1;i<n;i+=2){\n res += v[y][i].second - max(r, v[y][i-1].first);\n r = v[y][i].second;\n }\n v[y].clear();\n }\n return res;\n}\n\nint main(){\n cin.tie(nullptr);\n ios::sync_with_stdio(false);\n\n int n;\n while(cin >> n,n){\n cout << solve(n) << \"\\n\";\n }\n}", "accuracy": 1, "time_ms": 30, "memory_kb": 7644, "score_of_the_acc": -0.3663, "final_rank": 11 }, { "submission_id": "aoj_1242_6599547", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\n#define rep(i, s, t) for (int i = (int)(s); i < (int)(t); ++i)\n#define revrep(i, t, s) for (int i = (int)(t)-1; i >= (int)(s); --i)\n#define all(x) begin(x), end(x)\ntemplate <typename T> bool chmax(T& a, const T& b) { return a < b ? (a = b, 1) : 0; }\ntemplate <typename T> bool chmin(T& a, const T& b) { return a > b ? (a = b, 1) : 0; }\n\nint main() {\n ios_base::sync_with_stdio(false);\n cin.tie(nullptr);\n cout << fixed << setprecision(15);\n\n const int M = 2000;\n\n while (true) {\n int n;\n cin >> n;\n if (n == 0) break;\n vector<pair<ll, ll>> pts(n);\n rep(i,0,n) {\n int x, y;\n cin >> x >> y;\n x += M;\n y += M;\n pts[i] = {x, y};\n }\n\n auto calc = [&](ll x1, ll y1, ll x2, ll y2, ll x, bool include) {\n ll add = include ? x2-x1-1 : 0;\n if (y1 < y2) {\n return y1 + ((y2-y1)*(x-x1)+add)/(x2-x1);\n } else {\n return y2 + ((y1-y2)*(x2-x)+add)/(x2-x1);\n }\n };\n\n ll ans0 = 0, ans1 = 0;\n rep(x,0,2*M) {\n vector<int> imos0(2*M+1), imos1(2*M+1);\n rep(i,0,n) {\n int x1, y1, x2, y2;\n tie(x1, y1) = pts[i];\n tie(x2, y2) = pts[(i+1)%n];\n if (x1 <= x && x < x2) {\n int ya = min(calc(x1, y1, x2, y2, x, false), calc(x1, y1, x2, y2, x+1, false));\n imos0[0] -= 1;\n imos0[ya] += 1;\n int yb = max(calc(x1, y1, x2, y2, x, true), calc(x1, y1, x2, y2, x+1, true));\n imos1[0] += 1;\n imos1[yb] -= 1;\n } else if (x2 <= x && x < x1) {\n int ya = max(calc(x2, y2, x1, y1, x, true), calc(x2, y2, x1, y1, x+1, true));\n imos0[0] += 1;\n imos0[ya] -= 1;\n int yb = min(calc(x2, y2, x1, y1, x, false), calc(x2, y2, x1, y1, x+1, false));\n imos1[0] -= 1;\n imos1[yb] += 1;\n\n }\n }\n rep(i,0,2*M) {\n imos0[i+1] += imos0[i];\n imos1[i+1] += imos1[i];\n }\n rep(i,0,2*M) {\n if (imos0[i] > 0) ++ans0;\n if (imos1[i] > 0) ++ans1;\n }\n }\n cout << max(ans0, ans1) << \"\\n\";\n }\n}", "accuracy": 1, "time_ms": 1400, "memory_kb": 3396, "score_of_the_acc": -0.421, "final_rank": 15 }, { "submission_id": "aoj_1242_6024840", "code_snippet": "#include<bits/stdc++.h> \nusing namespace std;\nusing ll=long long int;\n//using Int=__int128;\n#define ALL(A) A.begin(),A.end()\ntemplate<typename T1,typename T2> bool chmin(T1 &a,T2 b){if(a<=b)return 0; a=b; return 1;}\ntemplate<typename T1,typename T2> bool chmax(T1 &a,T2 b){if(a>=b)return 0; a=b; return 1;}\ntemplate<typename T> constexpr int bitUP(T x,int a){return (x>>a)&1;}\n//→ ↓ ← ↑ \nint dh[4]={0,1,0,-1}, dw[4]={1,0,-1,0};\n//右上から時計回り\n//int dh[8]={-1,0,1,1,1,0,-1,-1}, dw[8]={1,1,1,0,-1,-1,-1,0};\n//long double EPS = 1e-6;\n//long double PI = acos(-1);\n//const ll INF=(1LL<<62);\nconst int MAX=(1<<30);\nconstexpr ll MOD=1000000000+7;\n//constexpr ll MOD=998244353;\n\n\ninline void bin101(){\n ios::sync_with_stdio(false);\n cin.tie(0);\n cout << fixed << setprecision(20);\n}\n\nusing pii=pair<int,int>;\nusing pil=pair<int,ll>;\nusing pli=pair<ll,int>;\nusing pll=pair<ll,ll>;\nusing psi=pair<string,int>;\nusing pis=pair<int,string>;\nusing psl=pair<string,ll>;\nusing pls=pair<ll,string>;\nusing pss=pair<string,string>;\n\nusing Graph=vector<vector<int>>;\n\ntemplate<typename T >\nstruct edge {\n\tint to;\n\tT cost;\n\tedge()=default;\n\tedge(int to, T cost) : to(to), cost(cost) {}\n\n};\ntemplate<typename T>\nusing WeightGraph=vector<vector<edge<T>>>;\n\ntemplate<typename T>\nvoid CinGraph(int M,WeightGraph<T> &g,bool directed=false,bool index1=true){\n while(M--){\n int s,t;\n T cost;\n cin>>s>>t>>cost;\n if(index1) s--,t--;\n g[s].emplace_back(t,cost);\n if(not directed) g[t].emplace_back(s,cost);\n }\n}\n\nvoid CinGraph(int M,Graph &g,bool directed=false,bool index1=true){\n while(M--){\n int s,t;\n cin>>s>>t;\n if(index1) s--,t--;\n g[s].push_back(t);\n if(not directed) g[t].push_back(s);\n }\n}\n\n\n//0-indexed vector cin\ntemplate<typename T>\ninline istream &operator>>(istream &is,vector<T> &v) {\n for(size_t i=0;i<v.size();i++) is>>v[i];\n\treturn is;\n}\n \n//0-indexed vector cin\ntemplate<typename T>\ninline istream &operator>>(istream &is,vector<vector<T>> &v) {\n for(size_t i=0;i<v.size();i++){\n is>>v[i];\n }\n return is;\n}\n//vector cout\ntemplate<typename T>\ninline ostream &operator<<(ostream &os,const vector<T> &v) {\n for(size_t i=0;i<v.size();i++){\n if(i) os<<\" \";\n os<<v[i];\n }\n return os;\n}\n//vector<vector> cout\ntemplate<typename T>\ninline ostream &operator<<(ostream &os,const vector<vector<T>> &v) {\n for(size_t i=0;i<v.size();i++){\n os<<v[i];\n if(i+1!=v.size()) os<<\"\\n\";\n }\n return os;\n}\n\n//Graph out\ntemplate<typename T>\ninline ostream &operator<<(ostream &os,const Graph &g) {\n for(size_t i=0;i<g.size();i++){\n for(int to:g[i]){\n os<<i<<\"->\"<<to<<\" \";\n }\n os<<endl;\n }\n return os;\n}\n\n//WeightGraph out\ntemplate<typename T>\ninline ostream &operator<<(ostream &os,const WeightGraph<T> &g) {\n for(size_t i=0;i<g.size();i++){\n for(auto e:g[i]){\n os<<i<<\"->\"<<e.to<<\"(\"<<e.cost<<\") \";\n }\n os<<endl;\n }\n return os;\n}\n\n\n//要素数n 初期値x\ntemplate<typename T>\ninline vector<T> vmake(size_t n,T x){\n\treturn vector<T>(n,x);\n}\n\n//a,b,c,x data[a][b][c] 初期値x\ntemplate<typename... Args>\nauto vmake(size_t n,Args... args){\n\tauto v=vmake(args...);\n\treturn vector<decltype(v)>(n,move(v));\n}\ntemplate<typename V,typename T>\nvoid fill(V &v,const T value){\n v=value;\n}\n\ntemplate<typename V,typename T>\nvoid fill(vector<V> &vec,const T value){\n for(auto &v:vec) fill(v,value);\n}\n\n//pair cout\ntemplate<typename T, typename U>\ninline ostream &operator<<(ostream &os,const pair<T,U> &p) {\n\tos<<p.first<<\" \"<<p.second;\n\treturn os;\n}\n \n//pair cin\ntemplate<typename T, typename U>\ninline istream &operator>>(istream &is,pair<T,U> &p) {\n\tis>>p.first>>p.second;\n\treturn is;\n}\n \n//ソート\ntemplate<typename T>\ninline void vsort(vector<T> &v){\n sort(v.begin(),v.end());\n}\n \n//逆順ソート\ntemplate<typename T>\ninline void rvsort(vector<T> &v){\n\tsort(v.rbegin(),v.rend());\n}\n\n//1ビットの数を返す\ninline int popcount(int x){\n\treturn __builtin_popcount(x);\n}\n//1ビットの数を返す\ninline int popcount(ll x){\n\treturn __builtin_popcountll(x);\n}\ntemplate<typename T>\ninline void Compress(vector<T> &C){\n sort(C.begin(),C.end());\n C.erase(unique(C.begin(),C.end()),C.end());\n}\ntemplate<typename T>\ninline int lower_idx(const vector<T> &C,T value){\n return lower_bound(C.begin(),C.end(),value)-C.begin();\n}\ntemplate<typename T>\ninline int upper_idx(const vector<T> &C,T value){\n return upper_bound(C.begin(),C.end(),value)-C.begin();\n}\n//時計回りに90度回転\ntemplate<typename T>\ninline void rotate90(vector<vector<T>> &C){\n vector<vector<T>> D(C[0].size(),vector<T>(C.size()));\n for(int h=0;h<C.size();h++){\n for(int w=0;w<C[h].size();w++){\n D[w][C.size()-1-h]=C[h][w];\n }\n }\n C=D;\n}\n//補グラフを返す\n//i→iのような辺は加えない\nGraph ComplementGraph(const Graph &g){\n size_t sz=g.size();\n bool used[sz][sz];\n fill(used[0],used[sz],false);\n for(size_t i=0;i<sz;i++){\n for(int to:g[i]){\n used[i][to]=true;\n }\n }\n Graph ret(sz);\n for(size_t i=0;i<sz;i++){\n for(size_t j=0;j<sz;j++){\n if(used[i][j]) continue;\n if(i==j) continue;\n ret[i].push_back(j);\n }\n }\n return ret;\n}\n//グラフの分解 secondはある頂点がどこに対応するか id[i]={2,3}のとき,頂点iはret[2][3]に対応\n//無効グラフのみに対応\npair<vector<Graph>,vector<pair<int,int>>> GraphDecomposition(const Graph &g){\n vector<pair<int,int>> id(g.size(),pair<int,int>(-1,-1));\n vector<Graph> ret;\n vector<int> now;\n for(size_t i=0;i<g.size();i++){\n if(id[i].first!=-1) continue;\n id[i].first=ret.size();\n id[i].second=0;\n now.push_back(i);\n for(size_t j=0;j<now.size();j++){\n for(int to:g[now[j]]){\n if(id[to].first==-1){\n id[to].first=ret.size();\n id[to].second=now.size();\n now.push_back(to);\n }\n }\n }\n Graph r(now.size());\n for(size_t j=0;j<now.size();j++){\n r[j]=g[now[j]];\n for(int &to:r[j]){\n to=id[to].second;\n }\n }\n ret.push_back(r);\n now.clear();\n }\n return make_pair(ret,id);\n}\n//0indexを想定\nbool OutGrid(ll h,ll w,ll H,ll W){\n return (h>=H or w>=W or h<0 or w<0);\n}\n\nvoid NO(){\n cout<<\"NO\"<<\"\\n\";\n}\nvoid YES(){\n cout<<\"YES\"<<\"\\n\";\n}\nvoid No(){\n cout<<\"No\"<<\"\\n\";\n}\nvoid Yes(){\n cout<<\"Yes\"<<\"\\n\";\n}\nnamespace overflow{\n template<typename T>\n T max(){\n return numeric_limits<T>::max();\n }\n template<typename T>\n T ADD(T a,T b){\n T res;\n return __builtin_add_overflow(a,b,&res)?max<T>():res;\n }\n template<typename T>\n T MUL(T a,T b){\n T res;\n return __builtin_mul_overflow(a,b,&res)?max<T>():res;\n }\n};\nusing namespace overflow;\nstruct ModInt{\n using u64=uint_fast64_t;\n u64 a;\n constexpr ModInt() :a(0){}\n constexpr ModInt(ll x) :a((x>=0)?(x%MOD):(x%MOD+MOD) ) {}\n\n inline constexpr ModInt operator+(const ModInt rhs)const noexcept{\n return ModInt(*this)+=rhs;\n }\n inline constexpr ModInt operator-(const ModInt rhs)const noexcept{\n return ModInt(*this)-=rhs;\n }\n inline constexpr ModInt operator*(const ModInt rhs)const noexcept{\n return ModInt(*this)*=rhs;\n }\n inline constexpr ModInt operator/(const ModInt rhs)const noexcept{\n return ModInt(*this)/=rhs;\n }\n inline constexpr ModInt operator+(const ll rhs) const noexcept{\n return ModInt(*this)+=ModInt(rhs);\n }\n inline constexpr ModInt operator-(const ll rhs)const noexcept{\n return ModInt(*this)-=ModInt(rhs);\n }\n inline constexpr ModInt operator*(const ll rhs)const noexcept{\n return ModInt(*this)*=ModInt(rhs);\n }\n inline constexpr ModInt operator/(const ll rhs)const noexcept{\n return ModInt(*this)/=ModInt(rhs);\n }\n\n inline constexpr ModInt &operator+=(const ModInt rhs)noexcept{\n a+=rhs.a;\n if(a>=MOD) a-=MOD;\n return *this;\n }\n inline constexpr ModInt &operator-=(const ModInt rhs)noexcept{\n if(rhs.a>a) a+=MOD;\n a-=rhs.a;\n return *this;\n }\n inline constexpr ModInt &operator*=(const ModInt rhs)noexcept{\n a=(a*rhs.a)%MOD;\n return *this;\n }\n inline constexpr ModInt &operator/=(ModInt rhs)noexcept{\n a=(a*rhs.inverse().a)%MOD;\n return *this;\n }\n inline constexpr ModInt &operator+=(const ll rhs)noexcept{\n return *this+=ModInt(rhs);\n }\n inline constexpr ModInt &operator-=(const ll rhs)noexcept{\n return *this-=ModInt(rhs);\n }\n inline constexpr ModInt &operator*=(const ll rhs)noexcept{\n return *this*=ModInt(rhs);\n }\n inline constexpr ModInt &operator/=(const ll rhs)noexcept{\n return *this/=ModInt(rhs);\n }\n\n inline constexpr ModInt operator=(const ll x)noexcept{\n a=(x>=0)?(x%MOD):(x%MOD+MOD);\n return *this;\n }\n\n inline constexpr bool operator==(const ModInt p)const noexcept{\n return a==p.a;\n }\n\n inline constexpr bool operator!=(const ModInt p)const noexcept{\n return a!=p.a;\n }\n\n inline constexpr ModInt pow(ll N) const noexcept{\n ModInt ans(1LL),p(a);\n while(N>0){\n if(bitUP(N,0)){\n ans*=p;\n }\n p*=p;\n N>>=1;\n }\n return ans;\n }\n inline constexpr ModInt inverse() const noexcept{\n return pow(MOD-2);\n }\n\n};\ninline constexpr ModInt operator+(const ll &a,const ModInt &b)noexcept{\n return ModInt(a)+=b;\n}\ninline constexpr ModInt operator-(const ll &a,const ModInt &b)noexcept{\n return ModInt(a)-=b;\n}\ninline constexpr ModInt operator*(const ll &a,const ModInt &b)noexcept{\n return ModInt(a)*=b;\n}\ninline constexpr ModInt operator/(const ll &a,const ModInt &b)noexcept{\n return ModInt(a)/=b;\n}\n//cout\ninline ostream &operator<<(ostream &os,const ModInt &p) {\n return os<<p.a;\n}\n\n//cin\ninline istream &operator>>(istream &is,ModInt &p) {\n ll t;\n is>>t;\n p=t;\n return is;\n}\n\nstruct Binominal{\n vector<ModInt> fac,finv,inv; //fac[n]:n! finv:(n!)の逆元\n int sz;\n Binominal(int n=10) :sz(1){\n if(n<=0) n=10;\n init(n);\n }\n inline void init(int n){\n fac.resize(n+1,1);\n finv.resize(n+1,1);\n inv.resize(n+1,1);\n for(int i=sz+1;i<=n;i++){\n fac[i]=fac[i-1]*i;\n inv[i]=MOD-inv[MOD%i]*(MOD/i);\n finv[i]=finv[i-1]*inv[i];\n }\n sz=n;\n }\n //nCk(n,k<=N) をO(1)で求める\n inline ModInt com(int n,int k){\n if(n<k) return ModInt(0);\n if(n<0 || k<0) return ModInt(0);\n if(n>sz) init(n);\n return fac[n]*finv[k]*finv[n-k];\n }\n //nCk(k<=N) をO(k) で求める \n inline ModInt rcom(ll n,int k){\n if(n<0 || k<0 || n<k) return ModInt(0);\n if(k>sz) init(k);\n ModInt ret(1);\n for(int i=0;i<k;i++){\n ret*=n-i;\n }\n ret*=finv[k];\n return ret;\n }\n\n //重複組み合わせ n種類のものから重複を許してk個を選ぶ\n //〇がn個,|がk個\n inline ModInt h(int n,int k){\n return com(n+k-1,k);\n }\n};\nvector<int> Subset(int S,bool zero=false,bool full=false){\n vector<int> ret;\n int now=(S-1)&S;\n if(full and S){\n ret.push_back(S);\n }\n do{\n ret.push_back(now);\n now=(now-1)&S;\n }while(now!=0);\n if(zero){\n ret.push_back(0);\n }\n return ret;\n}\ntemplate<typename T>\nT SUM(const vector<T> &v,int s,int t){\n chmax(s,0);\n chmin(t,int(v.size())-1);\n if(s>t) return 0;\n if(s==0) return v[t];\n else return v[t]-v[s-1];\n}\ntemplate<typename T>\nvoid buildSUM(vector<T> &v){\n for(size_t i=1;i<v.size();i++){\n v[i]+=v[i-1];\n }\n return;\n}\n////////////////////////////\n// マクロや型\n////////////////////////////\n\nusing DD=long double;\n\nconst DD EPS=1e-9;\nconst DD INF=1e50;\nconst DD PI=acosl(-1.0);\n#define EQ(a,b) (abs( (a) - (b) )<EPS) //a==b\n#define LS(a,b) ( (a)+EPS<(b) ) //a<b\n#define GR(a,b) ( (a)>(b)+EPS ) //a>b\n#define LE(a,b) ( (a)<(b)+EPS ) //a<=b\n#define GE(a,b) ( (a)>(b)-EPS ) //a>=b\n\n#define X real()\n#define Y imag()\n\n\n//点\nusing Point=complex<DD>;\nistream &operator>>(istream &is, Point &p) {\n DD x,y;\n is>>x>>y;\n p=Point(x,y);\n return is;\n}\n\n//点a→点bの線分\n//aとbが等しい時、色々バグるから注意!\nstruct Segment{\n Point a,b;\n Segment()=default;\n Segment(Point a,Point b) :a(a),b(b){}\n Segment(DD ax,DD ay,DD bx,DD by):a(ax,ay),b(bx,by){}\n Segment(DD r,Point a) :a(a),b(a+polar((DD)1.0,r)){} \n};\nusing Line=Segment;\n\n//円\nstruct Circle{\n Point p;\n DD r;\n Circle()=default;\n Circle(Point p,DD r):p(p),r(r){}\n};\n\nusing Polygon=vector<Point>;\n\n////////////////////////////\n// 基本計算\n////////////////////////////\n\n//度→ラジアン\ninline DD torad(const DD deg){return deg*PI/180;}\n//ラジアン→度\ninline DD todeg(const DD rad){return rad*180/PI;}\n//内積 |a||b|cosθ\ninline DD dot(const Point &a,const Point &b){return (conj(a)*b).X;}\n//外積 |a||b|sinθ\ninline DD cross(const Point &a,const Point &b){return (conj(a)*b).Y;}\n//ベクトルpを反時計回りにtheta(ラジアン)回転\nPoint rotate(const Point &p,const DD theta){return p*Point(cos(theta),sin(theta));}\n\n//余弦定理 cos(角度abc(ラジアン))を返す\n//長さの対応に注意 ab:辺abの長さ\n//verify:https://codeforces.com/gym/102056/problem/F\nDD cosine_formula(const DD ab,const DD bc,const DD ca){\n return (ab*ab+bc*bc-ca*ca)/(2*ab*bc);\n}\n\ninline bool xy_sort(const Point &a,const Point &b){\n if(a.X+EPS<b.X) return true;\n if(EQ(a.X,b.X) && a.Y+EPS<b.Y) return true;\n return false;\n}\ninline bool yx_sort(const Point &a,const Point &b){\n if(a.Y+EPS<b.Y) return true;\n if(EQ(a.Y,b.Y) && a.X+EPS<b.X) return true;\n return false;\n}\nnamespace std{\n inline bool operator<(const Point &a,const Point &b)noexcept{\n return xy_sort(a,b);\n }\n}\n//DDの比較関数\n//map<DD,vector<pii>,DDcom>\nstruct DDcom{\n bool operator()(const DD a, const DD b) const noexcept {\n return a+EPS<b;\n }\n};\n\n\n////////////////////////////\n// 平行や直交\n////////////////////////////\n\nbool isOrthogonal(const Point &a,const Point &b){\n return EQ(dot(a,b),0.0);\n}\n//verify:http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_2_A\nbool isOrthogonal(const Segment &s,const Segment &t){\n return EQ(dot(s.a-s.b,t.a-t.b),0.0);\n}\nbool isParallel(const Point &a,const Point &b){\n return EQ(cross(a,b),0.0);\n}\n//verify:http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_2_A\nbool isParallel(const Segment &s,const Segment &t){\n return EQ(cross(s.a-s.b,t.a-t.b),0);\n}\n//線分a→bに対して点cがどの位置にあるか\n//反時計回り:1 時計回り:-1 直線上(a,b,c:-2 a,c,b:0 c,a,b:2)\n//verify:http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_1_C\n//線分a→bの端点にcがあるなら0と判定されるはず\nint ccw(const Point &a,Point b,Point c){\n if(EQ(abs(b-c),0) or EQ(abs(a-c),0)) return 0;\n b-=a;c-=a;\n if(cross(b,c)>EPS) return 1;\n if(cross(b,c)<-EPS) return -1;\n if(dot(b,c)<-EPS) return 2;\n if(LS(norm(b),norm(c))) return -2;\n return 0;\n}\n\n////////////////////////////\n// 射影と反射\n////////////////////////////\n\n//射影\n//直線lに点pから垂線を引いたときの交点\n//verify:http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_1_A\nPoint project(const Point &p,const Line &l){\n Point base=l.b-l.a;\n DD r=dot(p-l.a,base)/norm(base);\n return l.a+base*r;\n}\n//反射\n//直線lに対して点pと対称な点\n//verify:http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_1_B\nPoint reflect(const Point &p,const Line &l){\n return p+(project(p,l)-p)*(DD)2.0;\n}\n//反射\n//直線lに対して線分sと対称な線分(直線でも使える)\n//verify:https://onlinejudge.u-aizu.ac.jp/solutions/problem/1171/review/6009447/bin101/C++17\nSegment reflect(const Segment &s,const Line &l){\n return Segment(reflect(s.a,l) , reflect(s.b,l));\n}\n\n////////////////////////////\n// 点との距離\n////////////////////////////\n\n//点と直線の距離\nDD DistPL(const Point &p,const Line &l){\n return abs(cross(l.b-l.a,p-l.a))/abs(l.b-l.a);\n}\n//点と線分の距離\nDD DistPS(const Point &p,const Segment &s){\n if( dot(s.b-s.a,p-s.a)<0.0 ) return abs(p-s.a);\n if( dot(s.a-s.b,p-s.b)<0.0 ) return abs(p-s.b);\n return DistPL(p,s);\n}\n\n////////////////////////////\n// 線分と直線\n////////////////////////////\n\n//線分a-b,c-dは交差するか?\n//接する場合も交差すると判定\n//接する場合は交差しないとするなら<=を<に変換\nbool intersect(const Point &a,const Point &b,const Point &c,const Point &d){\n return(ccw(a,b,c)*ccw(a,b,d)<=0 && ccw(c,d,a)*ccw(c,d,b)<=0);\n}\n//線分s,tは交差するか?\n//接する場合も交差すると判定\n//verify:http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_2_B&lang=ja\n//接する場合は交差しないとするならintersectの<=を<に変換\nbool intersect(const Segment &s,const Segment &t){\n return intersect(s.a,s.b,t.a,t.b);\n}\n// 2直線の交点\n// 線分の場合はintersectをしてから使う\n//verify:http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_2_C&lang=ja\nPoint crossPoint(const Line &s,const Line &t){\n Point base=t.b-t.a;\n DD d1=cross(base,t.a-s.a);\n DD d2=cross(base,s.b-s.a);\n if(EQ(d1,0) and EQ(d2,0)) return s.a; //同じ直線\n if(EQ(d2,0)) assert(false); //交点がない\n return s.a+d1/d2*(s.b-s.a);\n}\n//線分と線分の距離\n//verify:http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_2_D&lang=ja\nDD Dist(const Segment &s,const Segment &t){\n if(intersect(s,t)) return 0.0;\n return min(min(DistPS(t.a,s),DistPS(t.b,s)),min(DistPS(s.a,t),DistPS(s.b,t)) );\n}\nbool issame(const Line &l,const Line &m){\n if (GR(abs(cross(m.a - l.a, l.b - l.a)),0) ) return false;\n if (GR(abs(cross(m.b - l.a, l.b - l.a)),0) ) return false;\n return true;\n}\n\n////////////////////////////\n// 円\n////////////////////////////\n//直線lと円Cの交点\n//l.aにより近い方がindexが小さい\n//veirfy:http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_7_D\nvector<Point> crossPointLC(const Line &l,const Circle &c){\n vector<Point> ret;\n if(GR(DistPL(c.p,l),c.r)) return ret; //交点がないとき、空ベクトルを返す\n Point pr=project(c.p,l);\n Point e=(l.b-l.a)/(abs(l.b-l.a));\n DD base=sqrt(c.r*c.r-norm(pr-c.p));\n ret.push_back(pr-e*base);\n ret.push_back(pr+e*base);\n if(EQ(DistPL(c.p,l),c.r)) ret.pop_back(); //接するとき\n return ret;\n}\n//線分sと円cの交点\n//交点がないときは空ベクトルを返す\n//線分の端が交わる場合も交点とみなす\n//s.aにより近い方がindexが小さい\nvector<Point> crossPointSC(const Segment &s,const Circle &c){\n vector<Point> ret;\n if(DistPS(c.p,s)>c.r+EPS) return ret;\n auto koho=crossPointLC(s,c);\n for(auto p:koho){\n if(ccw(s.a,s.b,p)==0 or EQ(abs(p-s.a),0) or EQ(abs(p-s.b),0)) ret.push_back(p);\n }\n return ret;\n}\n\n//円aと円bの共通接線の数\n//離れている:4 外接:3 交わる:2 内接:1 内包:0\n//verify:http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_7_A\nint intersect(const Circle &a,const Circle &b){\n DD d=abs(a.p-b.p);\n if(GR(d,a.r+b.r)) return 4;\n if(EQ(d,a.r+b.r)) return 3;\n if(EQ(d,abs(a.r-b.r))) return 1;\n if(LS(d,abs(a.r-b.r))) return 0;\n return 2;\n}\n\n//円aと円bの交点\n//交点がないときは空ベクトルを返す\n//事前にintersectをしたほうがいいかも\n//verify:http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_7_E\nvector<Point> crossPoint(const Circle &a,const Circle &b){\n vector<Point> ret;\n if(GR(abs(a.p-b.p),a.r+b.r)) return ret;\n DD d=abs(a.p-b.p);\n DD s=acosl((a.r*a.r+d*d-b.r*b.r)/(2*a.r*d)); //0~π\n DD t=arg(b.p-a.p);\n if(EQ(a.r+b.r,d)) ret.push_back(a.p+polar(a.r,t));\n else ret.push_back(a.p+polar(a.r,t+s)),ret.push_back(a.p+polar(a.r,t-s));\n return ret;\n}\n\n//点pから円cに接線を引いたときの接点\n//verify:http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_7_F&lang=ja\nvector<Point> TanLine(const Point &p,const Circle &c){\n vector<Point> ret;\n DD d=abs(p-c.p);\n if(LS(d,c.r)) return ret; //pがcの内部にある場合\n if(EQ(d,c.r)){\n ret.push_back(p);\n return ret; //円の線上にある場合\n } \n return crossPoint(c,Circle(p,sqrt(d*d-c.r*c.r)));\n}\n\n//円a,bの共通接線\n//https://www.slideshare.net/kujira16/ss-35861169\n//Line.aは円aと接線の交点\n//(接線と円bの交点)と(接線と円aの交点)が違う場合はLine.bは(接線と円bの交点)\n//一致する場合は円aの中心からLine.aに向かうベクトルを反時計回りに90度回転したものを\n//Line.aに足したもの\n//verify:http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_7_G\n//verify:http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=2201\nvector<Line> TanLine(const Circle &a,const Circle &b){\n DD d=abs(a.p-b.p);\n vector<Line> ret;\n //外接線\n if(intersect(a,b)>=2){\n if(EQ(a.r,b.r)){\n Point up=polar(a.r,arg(b.p-a.p)+PI/2);\n ret.emplace_back(a.p+up,b.p+up);\n ret.emplace_back(a.p-up,b.p-up);\n }else{\n Point q=(a.p*b.r-b.p*a.r)/(b.r-a.r);\n auto as=TanLine(q,a);\n auto bs=TanLine(q,b);\n assert(as.size()==2 and bs.size()==2);\n for(int i=0;i<2;i++){\n ret.emplace_back(as[i],bs[i]);\n }\n }\n }else if(intersect(a,b)==1){ //内接する場合\n Point dir;\n if(a.r>b.r) dir=polar(a.r,arg(b.p-a.p));\n else dir=polar(a.r,arg(a.p-b.p));\n ret.emplace_back(a.p+dir,a.p+dir+rotate(dir,PI/2));\n }\n //内接線\n if(GR(abs(a.p-b.p),a.r+b.r)){ //円が離れている場合\n Point q=a.p+(b.p-a.p)*a.r/(a.r+b.r);\n auto as=TanLine(q,a);\n auto bs=TanLine(q,b);\n assert(as.size()==2 and bs.size()==2);\n for(int i=0;i<2;i++){\n ret.emplace_back(as[i],bs[i]);\n }\n }else if(EQ(d,a.r+b.r)){ //円が接している場合\n Point dir=polar(a.r,arg(b.p-a.p));\n ret.emplace_back(a.p+dir,a.p+dir+rotate(dir,PI/2));\n }\n return ret;\n}\n//2つの円の共通部分の面積\n//参考: https://tjkendev.github.io/procon-library/python/geometry/circles_intersection_area.html\n//verify: https://judge.u-aizu.ac.jp/onlinejudge/review.jsp?rid=6005736#1\nDD Area(const Circle &c1,const Circle &c2){\n DD cd=abs(c1.p-c2.p);\n if(LE(c1.r+c2.r,cd)) return 0; //円が離れている\n\n if(LE(cd,abs(c1.r-c2.r))) //片方の円が他方を包んでいる\n return min(c1.r,c2.r)*min(c1.r,c2.r)*PI;\n\n DD theta1=acosl(cosine_formula(c1.r,cd,c2.r));\n DD theta2=acosl(cosine_formula(c2.r,cd,c1.r));\n\n return c1.r*c1.r*theta1+c2.r*c2.r*theta2-c1.r*cd*sin(theta1);\n}\n\n\n////////////////////////////\n// 三角形\n////////////////////////////\n\n//ヘロンの公式 三角形の面積を返す\n//三角形が成立するか(平行ではないか)を忘れずに\nDD Heron(const DD ad,const DD bd,const DD cd){\n DD s=(ad+bd+cd)/2;\n return sqrt(s*(s-ad)*(s-bd)*(s-cd));\n}\n//三角形の外接円\n//isParallel()を使って三角形が成立するか(平行ではないか)を忘れずに\n//verify:http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_7_C\nCircle CircumscribedCircle(const Point &a,const Point &b,const Point &c){\n Point bc=(b+c)/(DD)2.0;\n Line aa(bc,bc+rotate(c-b,PI/2));\n Point ab=(a+b)/(DD)2.0;\n Line cc(ab,ab+rotate(b-a,PI/2));\n Point p=crossPoint(aa,cc);\n return Circle(p,abs(a-p));\n}\n//三角形の内接円\n//isParallel()を使って三角形が成立するか(平行ではないか)を忘れずに\n//verify:http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_7_B\nCircle InCircle(const Point &a,const Point &b,const Point &c){\n Line aa(a,a+polar((DD)1.0,(arg(b-a)+arg(c-a))/(DD)2.0));\n Line bb(b,b+polar((DD)1.0,(arg(a-b)+arg(c-b))/(DD)2.0));\n Point p=crossPoint(aa,bb);\n return Circle(p,DistPL(p,Line(a,b)));\n}\n\n\n \n\nvoid solve(){\nwhile(true){\n int N; cin>>N;\n if(N==0) return;\n\n vector<Point> P(N);\n for(auto &p:P) cin>>p;\n vector<Segment> vs;\n for(int i=0;i<N;i++){\n vs.emplace_back(P[i],P[(i+1)%N]);\n }\n int ans=0;\n for(int y=-2000;y<2000;y++){\n vector<pair<DD,DD>> v;\n Segment ys1(Point(-2100,y+1),Point(2100,y+1));\n Segment ys2(Point(-2100,y),Point(2100,y));\n for(auto s:vs){\n if(intersect(s,ys1)==false or intersect(s,ys2)==false){\n continue;\n }\n DD x1=crossPoint(s,ys1).X;\n DD x2=crossPoint(s,ys2).X;\n if(x1>x2) swap(x1,x2);\n v.emplace_back(x1,x2);\n }\n vsort(v);\n int r=-2100;\n for(int i=0;i+1<v.size();i+=2){\n DD x1=v[i].first;\n DD x2=v[i+1].second;\n ans+=ceil(x2-EPS)-max<int>(r,floor(x1+EPS));\n chmax(r,ceil(x2-EPS));\n }\n }\n cout<<ans<<endl;\n}\n}\n\nint main(){\n bin101();\n int T=1;\n //cin>>T;\n while(T--) solve();\n}", "accuracy": 1, "time_ms": 3440, "memory_kb": 3540, "score_of_the_acc": -1.031, "final_rank": 19 }, { "submission_id": "aoj_1242_6005190", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\nusing ll = long long;\nusing ull = unsigned long long;\nusing PII = pair<ll, ll>;\n\n#define FOR(i, a, n) for (ll i = (ll)a; i < (ll)n; ++i)\n#define REP(i, n) FOR(i, 0, n)\n#define ALL(x) x.begin(), x.end()\n#define POPCOUNT(x) __builtin_popcount(x)\ntemplate <typename T> void chmin(T &a, const T &b) { a = min(a, b); }\ntemplate <typename T> void chmax(T &a, const T &b) { a = max(a, b); }\n\nconst ll INF = 1LL << 60;\n\nstruct FastIO {\n FastIO() {\n cin.tie(0);\n ios::sync_with_stdio(0);\n }\n} fastiofastio;\n\nconst ull MOD = 1e9 + 7;\n\n// BEGIN CUT\nll modpow(ll x, ll y, ll m) {\n ll a = 1, p = x;\n while (y > 0) {\n if (y % 2 == 0) {\n p = (p * p) % m;\n y /= 2;\n } else {\n a = (a * p) % m;\n y--;\n }\n }\n return a;\n}\n// END CUT\n\nll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }\nll lcm(ll a, ll b) { return a / gcd(a, b) * b; }\n\nint main() {\n int n;\n while (cin >> n, n) {\n vector<PII> poly;\n int min_y = 1 << 30, max_y = -(1 << 30);\n for (int i = 0; i < n; i++) {\n int x, y;\n cin >> x >> y;\n poly.emplace_back(x, y);\n chmin(min_y, y);\n chmax(max_y, y);\n }\n int ans = 0;\n for (int Y = min_y; Y < max_y; Y++) {\n vector<pair<double, double>> points;\n for (int i = 0; i < n; i++) {\n auto [x1, y1] = poly[i];\n auto [x2, y2] = poly[(i + 1) % n];\n if (y1 > y2) {\n swap(x1, x2);\n swap(y1, y2);\n }\n if (y1 == y2 || y2 < Y + 1 || y1 > Y)\n continue;\n double cx1 = x1 + 1.0 * (x2 - x1) * (Y - y1) / (y2 - y1);\n double cx2 = x1 + 1.0 * (x2 - x1) * (Y + 1 - y1) / (y2 - y1);\n if (cx1 > cx2)\n swap(cx1, cx2);\n points.emplace_back(cx1, cx2);\n }\n sort(ALL(points));\n int prev = -(1 << 30);\n for (int i = 0; i + 1 < points.size(); i += 2) {\n int l = max((int)floor(points[i].first), prev);\n int r = ceil(points[i + 1].second);\n ans += r - l;\n prev = r;\n }\n }\n cout << ans << '\\n';\n }\n}", "accuracy": 1, "time_ms": 50, "memory_kb": 3392, "score_of_the_acc": -0.0248, "final_rank": 3 }, { "submission_id": "aoj_1242_6002163", "code_snippet": "#pragma GCC ta g(\"avx2\")\n#pragma GCC optimize(\"Ofast\")\n#pragma GCC optimize(\"unroll-loops\")\n#include <bits/stdc++.h>\nusing namespace std;\n#define DEBUG\n#ifdef DEBUG\ntemplate <class T, class U>\nostream &operator<<(ostream &os, const pair<T, U> &p) {\n os << '(' << p.first << ',' << p.second << ')';\n return os;\n}\ntemplate <class T> ostream &operator<<(ostream &os, const vector<T> &v) {\n os << '{';\n for(int i = 0; i < (int)v.size(); i++) {\n if(i) { os << ','; }\n os << v[i];\n }\n os << '}';\n return os;\n}\nvoid debugg() { cerr << endl; }\ntemplate <class T, class... Args>\nvoid debugg(const T &x, const Args &... args) {\n cerr << \" \" << x;\n debugg(args...);\n}\n#define debug(...) \\\n cerr << __LINE__ << \" [\" << #__VA_ARGS__ << \"]: \", debugg(__VA_ARGS__)\n#define dump(x) cerr << __LINE__ << \" \" << #x << \" = \" << (x) << endl\n#else\n#define debug(...) (void(0))\n#define dump(x) (void(0))\n#endif\nusing namespace std;\ntypedef long long ll;\ntypedef vector<ll> vl;\ntypedef vector<vl> vvl;\ntypedef vector<char> vc;\ntypedef vector<string> vs;\ntypedef vector<bool> vb;\ntypedef vector<double> vd;\ntypedef pair<ll,ll> P;\ntypedef pair<int,int> pii;\ntypedef vector<P> vpl;\ntypedef tuple<ll,ll,ll> tapu;\n#define rep(i,n) for(int i=0; i<(n); i++)\n#define REP(i,a,b) for(int i=(a); i<(b); i++)\n#define all(x) (x).begin(), (x).end()\n#define rall(x) (x).rbegin(), (x).rend()\ntemplate<typename T1,typename T2> inline bool chmin(T1 &a,T2 b){\n\tif(a>b){\n\t\ta = b; return true;\n\t}\n\telse return false;\n}\ntemplate<typename T1,typename T2> inline bool chmax(T1 &a,T2 b){\n\tif(a<b){\n\t\ta = b; return true;\n\t}\n\telse return false;\n}\ntemplate<typename T> inline void print(T &a){\n int sz = a.size();\n for(auto itr = a.begin(); itr != a.end(); itr++){\n\t\tcout << *itr;\n sz--;\n if(sz) cout << \" \";\n\t}\n cout << \"\\n\";\n}\ntemplate<typename T1,typename T2> inline void print2(T1 a, T2 b){\n\tcout << a << \" \" << b << \"\\n\";\n}\ntemplate<typename T1,typename T2,typename T3> inline void print3(T1 a, T2 b, T3 c){\n\tcout << a << \" \" << b << \" \" << c << \"\\n\";\n}\nvoid mark() {cout << \"#\" << \"\\n\";}\nll pcount(ll x) {return __builtin_popcountll(x);}\n//#include <atcoder/maxflow>\n//using namespace atcoder;\nconst int inf = (1<<30)-1;\nconst ll linf = 1LL<<61;\nconst int MAX = 510000;\nint dy[8] = {0,1,0,-1,1,-1,-1,1};\nint dx[8] = {-1,0,1,0,1,-1,1,-1};\nconst double pi = acos(-1);\nconst double eps = 1e-9;\n//const int mod = 1e9 + 7;\nconst int mod = 998244353;\n\nusing tu = tuple<double,double,int>;\n\nint main(){\n\twhile(1){\n\t\tint n; cin >> n;\n\t\tif(!n) break;\n\t\tvl x(n),y(n);\n\t\trep(i,n) cin >> x[i] >> y[i], x[i] += 2000, y[i] += 2000;\n\t\tvector<vector<tu>> in(4040);\n\t\tvector<set<int>> out(4040);\n\t\trep(i,n){\n\t\t\tint now = i;\n\t\t\tint nxt = (i+1)%n;\n\t\t\tif(x[now] > x[nxt]) swap(now,nxt);\n\t\t\tif(x[now] == x[nxt]) continue;\n\t\t\tin[x[now]].emplace_back(y[now],(double)(y[nxt]-y[now])/(x[nxt]-x[now]),i);\n\t\t\tout[x[nxt]].insert(i);\n\t\t}\n\t\tset<tu> se;\n\t\tll ans = 0;\n\t\trep(i,4010){\n\t\t\tfor(auto p : in[i]){\n\t\t\t\tse.emplace(p);\n\t\t\t}\n\t\t\tvector<tu> nxt;\n\t\t\tfor(auto itr = se.begin(); itr != se.end();){\n\t\t\t\tif(out[i].count(get<2>(*itr))){\n\t\t\t\t\titr = se.erase(itr);\n\t\t\t\t}else{\n\t\t\t\t\titr++;\n\t\t\t\t}\n\t\t\t}\n\t\t\tassert((int)(se.size()) % 2 == 0);\n\t\t\tll pre = -1;\n\t\t\twhile(!se.empty()){\n\t\t\t\tauto t = *se.begin(); se.erase(se.begin());\n\t\t\t\tauto u = *se.begin(); se.erase(se.begin());\n\t\t\t\tdouble ue = max(get<0>(u), get<0>(u) + get<1>(u));\n\t\t\t\tdouble sita = min(get<0>(t), get<0>(t) + get<1>(t));\n\t\t\t\tif((ll)(floor(sita+eps) + eps) < pre){\n\t\t\t\t\tsita = pre;\n\t\t\t\t}\n\t\t\t\tans += (ll)round(ceil(ue-eps) - floor(sita+eps));\n\t\t\t\tpre = (ll)(ceil(ue-eps) + eps);\n\t\t\t\tget<0>(t) += get<1>(t);\n\t\t\t\tget<0>(u) += get<1>(u);\n\t\t\t\tnxt.emplace_back(t);\n\t\t\t\tnxt.emplace_back(u);\n\t\t\t}\n\t\t\tfor(auto p : nxt) se.emplace(p);\n\t\t}\n\t\tcout << ans << \"\\n\";\n\t}\n}", "accuracy": 1, "time_ms": 220, "memory_kb": 3428, "score_of_the_acc": -0.0776, "final_rank": 6 }, { "submission_id": "aoj_1242_5324829", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nconst double EPS = 0.0000001;\nstruct point{\n double x, y;\n point(){\n }\n point(double x, double y): x(x), y(y){\n }\n point operator +(point P){\n return point(x + P.x, y + P.y);\n }\n point operator -(point P){\n return point(x - P.x, y - P.y);\n }\n point operator *(double k){\n return point(x * k, y * k);\n }\n point operator /(double k){\n return point(x / k, y / k);\n }\n};\ndouble abs(point P){\n return sqrt(P.x * P.x + P.y * P.y);\n}\ndouble cross(point P, point Q){\n return P.x * Q.y - P.y * Q.x;\n}\nstruct line{\n point A, B;\n line(point A, point B): A(A), B(B){\n }\n};\npoint vec(line L){\n return L.B - L.A;\n}\npoint line_intersection(line L1, line L2){\n return L1.A + vec(L1) * cross(L2.A - L1.A, vec(L2)) / cross(vec(L1), vec(L2));\n}\nint main(){\n while (true){\n int m;\n cin >> m;\n if (m == 0){\n break;\n }\n vector<int> x(m + 1), y(m + 1);\n vector<point> P(m + 1);\n for (int i = 0; i < m; i++){\n cin >> x[i] >> y[i];\n x[i] += 2000;\n y[i] += 2000;\n P[i] = point(x[i], y[i]);\n }\n x[m] = x[0];\n y[m] = y[0];\n P[m] = P[0];\n vector<vector<pair<int, int>>> R(4000);\n for (int i = 0; i < m; i++){\n int mn = min(y[i], y[i + 1]);\n int mx = max(y[i], y[i + 1]);\n line L(P[i], P[i + 1]);\n for (int j = mn; j < mx; j++){\n line L1(point(0, j), point(5000, j));\n point Q1 = line_intersection(L, L1);\n line L2(point(0, j + 1), point(5000, j + 1));\n point Q2 = line_intersection(L, L2);\n int l = min(Q1.x, Q2.x) + EPS;\n int r = max(Q1.x, Q2.x) + 1 - EPS;\n R[j].push_back(make_pair(l, r));\n }\n }\n int ans = 0;\n for (int i = 0; i < 4000; i++){\n sort(R[i].begin(), R[i].end());\n int cnt = R[i].size();\n int r = -1;\n int tmp = ans;\n for (int j = 0; j < cnt; j++){\n if (j % 2 == 1){\n ans += R[i][j].second - r;\n } else {\n ans += R[i][j].second - max(R[i][j].first, r);\n }\n r = R[i][j].second;\n }\n }\n cout << ans << endl;\n }\n}", "accuracy": 1, "time_ms": 80, "memory_kb": 6940, "score_of_the_acc": -0.3235, "final_rank": 10 }, { "submission_id": "aoj_1242_5023769", "code_snippet": "#include <iostream>\n#include <vector>\n#include <algorithm>\n#include <cstring>\n#include <cmath>\n\nusing namespace std;\n\nstruct Point {\n int x, y;\n};\n\nint n;\nPoint v[100];\nvector<pair<int, int> > table1[4001];\nvector<pair<int, int> > table2[4001];\n\ndouble GetX(const Point& a, const Point& b, double y) {\n static const double epcilon = 0.000000000001;\n double t = (a.x - b.x) * (y - b.y) / (a.y - b.y) + b.x;\n if(ceil(t) - t < epcilon)\n t = ceil(t);\n if(t - floor(t) < epcilon)\n t = ceil(t);\n return t;\n}\n\nvoid Add(const Point& a, const Point& b) {\n double x1, x2;\n if(a.y < b.y) {\n for(int y = a.y; y < b.y; ++y) {\n x1 = GetX(a, b, y);\n x2 = GetX(a, b, y+1);\n if(a.x > b.x)\n\tswap(x1, x2);\n table1[y+2000].push_back(make_pair((int)floor(x1), 1));\n table2[y+2000].push_back(make_pair((int)ceil(x2), 1));\n }\n } else {\n for(int y = b.y; y < a.y; ++y) {\n x1 = GetX(a, b, y);\n x2 = GetX(a, b, y+1);\n if(a.x > b.x)\n\tswap(x1, x2);\n table1[y+2000].push_back(make_pair((int)ceil(x1), -1));\n table2[y+2000].push_back(make_pair((int)floor(x2), -1));\n }\n }\n}\n\nint CountT(vector<pair<int, int> >& t) {\n sort(t.begin(), t.end());\n int cnt = 0;\n int state = 0;\n int prev;\n for(vector<pair<int, int> >::iterator p = t.begin(); p != t.end(); ++p) {\n if(state)\n cnt += p->first - prev;\n state += p->second;\n prev = p->first;\n }\n return cnt;\n}\n\nint Count() {\n int c1 = 0;\n int c2 = 0;\n for(int i = 0; i < 4001; ++i) {\n c1 += CountT(table1[i]);\n c2 += CountT(table2[i]);\n }\n return max(c1, c2);\n}\n\nint main() {\n while(cin >> n && n) {\n for(int i = 0; i < 4001; ++i) {\n table1[i].clear();\n table2[i].clear();\n }\n for(int i = 0; i < n; ++i) {\n cin >> v[i].x >> v[i].y;\n if(i)\n\tAdd(v[i-1], v[i]);\n }\n Add(v[n-1], v[0]);\n cout << Count() << endl;\n }\n}", "accuracy": 1, "time_ms": 170, "memory_kb": 11280, "score_of_the_acc": -0.7045, "final_rank": 17 }, { "submission_id": "aoj_1242_5023051", "code_snippet": "#include<bits/stdc++.h>\ntypedef long long int ll;\ntypedef unsigned long long int ull;\n#define BIG_NUM 2000000000\n#define HUGE_NUM 99999999999999999\n#define MOD 1000000007\n#define EPS 0.000000001\nusing namespace std;\n\n\n#define NUM 105\n\nint N;\n\nstruct Point{\n\n\tvoid set(int arg_x,int arg_y){\n\t\tx = arg_x;\n\t\ty = arg_y;\n\t}\n\tint x,y;\n};\n\nstruct Edge{\n\tEdge(int arg_left,int arg_right){\n\t\tleft = arg_left;\n\t\tright = arg_right;\n\t}\n\tbool operator<(const struct Edge &arg) const{\n\t\tif(left != arg.left){\n\n\t\t\treturn left < arg.left;\n\n\t\t}else{\n\t\t\treturn right < arg.right;\n\t\t}\n\t}\n\tint left,right;\n};\n\n//http://www.ipsj.or.jp/07editj/promenade/4501.pdf\n\nPoint point[NUM];\n\nvoid compute_x(int i,int y,int *xl,int *xr){\n\n\tint den,num,w;\n\n\tden = point[i+1].y-point[i].y;\n\tnum = (y-point[i].y)*(point[i+1].x-point[i].x);\n\tw = point[i].x+num/den;\n\n\tif(num%den == 0){\n\t\t*xl = w;\n\t\t*xr = w;\n\t}\n\telse if((num%den)*den < 0){\n\t\t*xl = w-1;\n\t\t*xr = w;\n\t}else{\n\t\t*xl = w;\n\t\t*xr = w+1;\n\t}\n}\n\nint calc_S(int under_y){\n\n\tvector<Edge> V;\n\tint xla,xra,xlb,xrb;\n\n\t//under_y~under_y+1を走る辺を求める\n\tfor(int i = 0; i < N; i++){\n\t\tif((point[i].y <= under_y && point[i+1].y <= under_y) ||\n\t\t\t(point[i].y >= under_y+1 && point[i+1].y >= under_y+1))continue;\n\n\t\tcompute_x(i,under_y,&xla,&xra); //y == yとの交点\n\t\tcompute_x(i,under_y+1,&xlb,&xrb); //y == y+1との交点\n\n\t\tV.push_back(Edge(min(xla,xlb),max(xra,xrb)));\n\t}\n\n\tsort(V.begin(),V.end());\n\n\tint prev = -2000,ret = 0;\n\n\tfor(int i = 0; i < V.size(); i += 2){\n\t\tret += V[i+1].right-max(V[i].left,prev);\n\t\tprev = V[i+1].right;\n\t}\n\n\treturn ret;\n}\n\nvoid func(){\n\n\tint y_min = BIG_NUM,y_max = -BIG_NUM;\n\n\tfor(int i = 0; i < N; i++){\n\n\t\tscanf(\"%d %d\",&point[i].x,&point[i].y);\n\n\t\t//入力のついでにy座標の最大値と最小値を求める\n\t\ty_min = min(y_min,point[i].y);\n\t\ty_max = max(y_max,point[i].y);\n\t}\n\tpoint[N] = point[0];\n\n\tint ans = 0;\n\n\tfor(int under_y = y_min; under_y <= y_max-1; under_y++){\n\n\t\tans += calc_S(under_y);\n\t}\n\n\tprintf(\"%d\\n\",ans);\n}\n\nint main(){\n\n\twhile(true){\n\t\tscanf(\"%d\",&N);\n\t\tif(N == 0)break;\n\n\t\tfunc();\n\t}\n\n\treturn 0;\n}", "accuracy": 1, "time_ms": 80, "memory_kb": 3160, "score_of_the_acc": -0.0147, "final_rank": 1 }, { "submission_id": "aoj_1242_4965206", "code_snippet": "#include<iostream>\n#include<algorithm>\n#include<vector>\nusing namespace std;\nint N;\nint X[100],Y[100];\nvoid f(int sx,int sy,int gx,int gy,vector<pair<int,int> >&ed)\n{\n\tif(sy==gy)return;\n\tif(sx>gx)\n\t{\n\t\tf(gx,gy,sx,sy,ed);\n\t\treturn;\n\t}\n\tint dx=gx-sx,dy=gy-sy;\n\tfor(int i=0;i<dx;i++)\n\t{\n\t\tif(dy>0)\n\t\t{\n\t\t\tint A=i*dy/dx,B=((i+1)*dy-1)/dx;\n\t\t\tfor(;A<=B;A++)ed.push_back(make_pair(sx+i,sy+A));\n\t\t}\n\t\telse\n\t\t{\n\t\t\tint A=i*dy/dx-1,B=((i+1)*dy+1)/dx-1;\n\t\t\tfor(;A>=B;A--)ed.push_back(make_pair(sx+i,sy+A));\n\t\t}\n\t}\n}\nlong gcd(long a,long b){return b?gcd(b,a%b):a;}\nstruct rat{\n\tlong a,b;\n\trat(long a_,long b_){\n\t\tlong g=gcd(a_,b_);\n\t\ta=a_/g;\n\t\tb=b_/g;\n\t\tif(b<0)\n\t\t{\n\t\t\ta=-a;\n\t\t\tb=-b;\n\t\t}\n\t}\n\trat operator*(long x)const{\n\t\treturn rat(a*x,b);\n\t}\n\trat operator+(long x)const{\n\t\treturn rat(a+b*x,b);\n\t}\n\tbool operator<(const rat&r)const\n\t{\n\t\treturn a*r.b<b*r.a;\n\t}\n\tlong floor()const{return a/b;}\n\tlong ceil()const{return(a+b-1)/b;}\n};\nmain()\n{\n\twhile(cin>>N,N)\n\t{\n\t\tfor(int i=0;i<N;i++)\n\t\t{\n\t\t\tcin>>X[i]>>Y[i];\n\t\t\tX[i]+=2000;\n\t\t\tY[i]+=2000;\n\t\t}\n\t\tint ans=0;\n\t\tvector<pair<int,int> >ed;\n\t\tfor(int i=0;i<N;i++)\n\t\t{\n\t\t\tint j=(i+1)%N;\n\t\t\tif(X[i]==X[j])continue;\n\t\t\tf(X[i],Y[i],X[j],Y[j],ed);\n\t\t}\n\t\tfor(int x=0;x<4000;x++)\n\t\t{\n\t\t\tvector<pair<rat,rat> >y;\n\t\t\tfor(int i=0;i<N;i++)\n\t\t\t{\n\t\t\t\tint j=(i+1)%N;\n\t\t\t\tif(X[i]<=x&&x+1<=X[j])\n\t\t\t\t{\n\t\t\t\t\tint dx=X[j]-X[i];\n\t\t\t\t\tint dy=Y[j]-Y[i];\n\t\t\t\t\trat now(dy,dx);\n\t\t\t\t\ty.push_back(make_pair(\n\t\t\t\t\t\tnow*(x-X[i])+Y[i],\n\t\t\t\t\t\tnow*(x+1-X[i])+Y[i]\n\t\t\t\t\t));\n\t\t\t\t}\n\t\t\t\telse if(X[j]<=x&&x+1<=X[i])\n\t\t\t\t{\n\t\t\t\t\tint dx=X[i]-X[j];\n\t\t\t\t\tint dy=Y[i]-Y[j];\n\t\t\t\t\trat now(dy,dx);\n\t\t\t\t\ty.push_back(make_pair(\n\t\t\t\t\t\tnow*(x-X[j])+Y[j],\n\t\t\t\t\t\tnow*(x+1-X[j])+Y[j]\n\t\t\t\t\t));\n\t\t\t\t}\n\t\t\t}\n\t\t\tsort(y.begin(),y.end());\n\t\t\tif(y.size()%2==1)cout<<\"ERROR\"<<endl;\n\t\t\tfor(int i=0;i<y.size();i+=2)\n\t\t\t{\n\t\t\t\tlong L=max(y[i].first.ceil(),y[i].second.ceil());\n\t\t\t\tlong R=min(y[i+1].first.floor(),y[i+1].second.floor());\n\t\t\t\tif(R>L)ans+=R-L;\n\t\t\t}\n\t\t}\n\t\tsort(ed.begin(),ed.end());\n\t\ted.erase(unique(ed.begin(),ed.end()),ed.end());\n\t\tans+=ed.size();\n\t\tcout<<ans<<endl;\n\t}\n}", "accuracy": 1, "time_ms": 2040, "memory_kb": 15400, "score_of_the_acc": -1.5894, "final_rank": 20 }, { "submission_id": "aoj_1242_4961633", "code_snippet": "#include \"iostream\"\n#include \"climits\"\n#include \"list\"\n#include \"queue\"\n#include \"stack\"\n#include \"set\"\n#include \"functional\"\n#include \"algorithm\"\n#include \"string\"\n#include \"map\"\n#include \"unordered_map\"\n#include \"unordered_set\"\n#include \"iomanip\"\n#include \"cmath\"\n#include \"random\"\n#include \"bitset\"\n#include \"cstdio\"\n#include \"numeric\"\n#include \"cassert\"\n#include \"ctime\"\n\nusing namespace std;\n\nconstexpr long long int MOD = 1000000007;\n//constexpr int MOD = 1000000007;\n//constexpr int MOD = 998244353;\n//constexpr long long int MOD = 998244353;\nconstexpr double EPS = 1e-12;\n\n//int N, M, K, T, H, W, L, R;\nlong long int N, M, K, T, H, W, L, R;\n\nstruct Point {\n\tlong double x;\n\tlong double y;\n\tPoint(long double xx, long double yy) {\n\t\tx = xx, y = yy;\n\t}\n\tPoint() {\n\t}\n\tPoint operator + (const Point& c)const {\n\t\tPoint box;\n\t\tbox.x = x + c.x;\n\t\tbox.y = y + c.y;\n\t\treturn box;\n\t}\n\tPoint operator - (const Point& c)const {\n\t\tPoint box;\n\t\tbox.x = x - c.x;\n\t\tbox.y = y - c.y;\n\t\treturn box;\n\t}\n\tPoint operator * (const long double& b)const {\n\t\tPoint box;\n\t\tbox.x = x * b;\n\t\tbox.y = y * b;\n\t\treturn box;\n\t}\n\tbool operator <(const Point& c)const {\n\t\tif (y < c.y)return true;\n\t\tif (y > c.y)return false;\n\t\tif (x < c.x)return true;\n\t\treturn false;\n\t}\n\tbool operator >(const Point& c)const {\n\t\tif (y > c.y)return true;\n\t\tif (y < c.y)return false;\n\t\tif (x > c.x)return true;\n\t\treturn false;\n\t}\n\tlong double det(Point a) {\n\t\treturn x * a.y - y * a.x;\n\t}\n};\n\nstruct Line {\n\tlong double a, b, c;\n\tLine(const int aa, const int bb, const int cc) {\n\t\ta = aa, b = bb, c = cc;\n\t}\n\tLine(const Point s, const Point t) {\n\t\tif (abs(s.x - t.x) < EPS) {\n\t\t\ta = 1;\n\t\t\tb = 0;\n\t\t}\n\t\telse {\n\t\t\tb = 1;\n\t\t\ta = (t.y - s.y) / (s.x - t.x);\n\t\t}\n\t\tc = s.x*a + s.y*b;\n\t}\n};\n\nlong double dot(Point a, Point b) {\n\treturn a.x*b.x + a.y*b.y;\n}\n\nlong double cross(Point a, Point b) {\n\treturn a.x*b.y - b.x*a.y;\n}\n\nlong double Distance(Point a, Point b) {\n\treturn sqrt((a.x - b.x)*(a.x - b.x) + (a.y - b.y)*(a.y - b.y));\n}\n\nlong double Distance(Point a, pair<Point, Point>b) {\n\tLine l = Line(b.first, b.second);\n\tif (abs(l.a*a.x + l.b*a.y - l.c) < EPS) {\n\t\tif (abs(Distance(a, b.first) + Distance(a, b.second) - Distance(b.second, b.first)) <= sqrt(EPS))return 0;\n\t\treturn min(Distance(a, b.first), Distance(a, b.second));\n\t}\n\tlong double A = pow((b.first.x - b.second.x), 2) + pow((b.first.y - b.second.y), 2);\n\tlong double B = 2 * (a.x - b.first.x)*(b.first.x - b.second.x) + 2 * (a.y - b.first.y)*(b.first.y - b.second.y);\n\tlong double r = B / 2 / A;\n\tr = -r;\n\tif (r >= 0 && r <= 1) {\n\t\treturn Distance(a, Point(b.first.x + r * (b.second.x - b.first.x), b.first.y + r * (b.second.y - b.first.y)));\n\t}\n\telse {\n\t\treturn min(Distance(a, b.first), Distance(a, b.second));\n\t}\n}\n\nbool LineCross(Point a1, Point a2, Point b1, Point b2) {\n\treturn cross(a2 - a1, b1 - a1)*cross(a2 - a1, b2 - a1) < EPS&&cross(b2 - b1, a1 - b1)*cross(b2 - b1, a2 - b1) < EPS && !(abs((a1 - a2).x*(a1 - b1).y - (a1 - a2).y*(a1 - b1).x)<EPS && abs((a1 - a2).x*(a1 - b2).y - (a1 - a2).y*(a1 - b2).x)<EPS&&abs(Distance(a1, a2) - Distance(a1, b1) - Distance(a2, b1)) > EPS&&abs(Distance(a1, a2) - Distance(a2, b2) - Distance(a1, b2)) > EPS&&abs(Distance(b1, b2) - Distance(a1, b1) - Distance(a1, b2)) > EPS&&abs(Distance(b1, b2) - Distance(a2, b1) - Distance(a2, b2)) > EPS);\n}\n\nlong double Distance(pair<Point, Point>a, pair<Point, Point>b) {\n\tif (LineCross(a.first, a.second, b.first, b.second))return 0;\n\treturn min({ Distance(a.first,b),Distance(a.second,b),Distance(b.first,a),Distance(b.second,a) });\n}\n\nPoint LineCross(Line a, Line b) {\n\tPoint ret;\n\tret.x = (a.c*b.b - a.b*b.c) / (a.a*b.b - a.b*b.a);\n\tret.y = -(a.c*b.a - a.a*b.c) / (a.a*b.b - a.b*b.a);\n\treturn ret;\n}\n\nlong double TriangleArea(Point a, Point b, Point c) {\n\tlong double A, B, C;\n\tA = Distance(a, b);\n\tB = Distance(a, c);\n\tC = Distance(b, c);\n\tlong double S = (A + B + C) / 2;\n\treturn sqrt(S*(S - A)*(S - B)*(S - C));\n}\n\nbool IsPointLeft(pair<Point, Point>a, Point b) {\n\ta.second = a.second - a.first;\n\tb = b - a.first;\n\ta.first = a.first - a.first;\n\tlong double radline = atan2(a.second.y, a.second.x);\n\tlong double radpoint = atan2(b.y, b.x) - radline;\n\tif (sin(radpoint) >= -EPS)return true;\n\telse return false;\n}\n\nbool IsPointInPolygon(Point p, vector<Point>poly) {\n\tbool ret = true;\n\tfor (int i = 0; i < poly.size(); i++) {\n\t\tif (Distance(p, { poly[i],poly[(i + 1) % poly.size()] }) <= EPS) return true;\n\t\tret &= IsPointLeft({ poly[i],poly[(i + 1) % poly.size()] }, p);\n\t}\n\treturn ret;\n}\n\nlong double Area(vector<Point>p) {\n\tlong double ret = 0;\n\tfor (int i = 2; i < p.size(); i++) {\n\t\tret += TriangleArea(p[0], p[i - 1], p[i]);\n\t}\n\treturn ret;\n}\n\nvector<vector<Point>>DividePolygonByLine(vector<Point>p, pair<Point, Point>l) {\n\tvector<int>cr;\n\tvector<Point>cp;\n\tfor (int k = 0; k < p.size(); k++) {\n\t\tint nx = k + 1;\n\t\tnx %= p.size();\n\t\tif (LineCross(p[k], p[(k + 1) % p.size()], l.first, l.second)) {\n\t\t\tauto np = LineCross(Line(p[k], p[(k + 1) % p.size()]), Line(l.first, l.second));\n\t\t\tbool flag = true;\n\t\t\tfor (auto l : cp) {\n\t\t\t\tif (Distance(l, np) <= EPS)flag = false;\n\t\t\t}\n\t\t\tif (flag) {\n\t\t\t\tcr.push_back(k);\n\t\t\t\tcp.push_back(np);\n\t\t\t}\n\t\t}\n\t}\n\tvector<Point>w;\n\tvector<Point>x;\n\tif (cr.size() != 2)return vector<vector<Point>>(1, p);\n\tint cnt = cr[0];\n\tdo {\n\t\tif (w.empty() || Distance(w.back(), p[cnt]) > EPS)w.push_back(p[cnt]);\n\t\tif (cnt == cr[0]) {\n\t\t\tif (w.empty() || Distance(w.back(), cp[0]) > EPS)w.push_back(cp[0]);\n\t\t\tif (w.empty() || Distance(w.back(), cp[1]) > EPS)w.push_back(cp[1]);\n\t\t\tcnt = cr[1] + 1;\n\t\t\tcnt %= p.size();\n\t\t}\n\t\telse if (cnt == cr[1]) {\n\t\t\tif (w.empty() || Distance(w.back(), cp[1]) > EPS)w.push_back(cp[1]);\n\t\t\tif (w.empty() || Distance(w.back(), cp[0]) > EPS)w.push_back(cp[0]);\n\t\t\tcnt = cr[0] + 1;\n\t\t\tcnt %= p.size();\n\t\t}\n\t\telse {\n\t\t\tcnt++;\n\t\t\tcnt %= p.size();\n\t\t}\n\t} while (cnt != cr[0]);\n\tcnt = cr[1];\n\tdo {\n\t\tif (x.empty() || Distance(x.back(), p[cnt]) > EPS)x.push_back(p[cnt]);\n\t\tif (cnt == cr[0]) {\n\t\t\tif (x.empty() || Distance(x.back(), cp[0]) > EPS)x.push_back(cp[0]);\n\t\t\tif (x.empty() || Distance(x.back(), cp[1]) > EPS)x.push_back(cp[1]);\n\t\t\tcnt = cr[1] + 1;\n\t\t\tcnt %= p.size();\n\t\t}\n\t\telse if (cnt == cr[1]) {\n\t\t\tif (x.empty() || Distance(x.back(), cp[1]) > EPS)x.push_back(cp[1]);\n\t\t\tif (x.empty() || Distance(x.back(), cp[0]) > EPS)x.push_back(cp[0]);\n\t\t\tcnt = cr[0] + 1;\n\t\t\tcnt %= p.size();\n\t\t}\n\t\telse {\n\t\t\tcnt++;\n\t\t\tcnt %= p.size();\n\t\t}\n\t} while (cnt != cr[1]);\n\tvector<vector<Point>>ret;\n\tret.push_back(w);\n\tret.push_back(x);\n\treturn ret;\n}\n\nstruct Circle {\n\tlong double x, y, r;\n\tvoid Input() {\n\t\tcin >> x >> y >> r;\n\t}\n\tbool operator==(const Circle&c)const {\n\t\treturn x == c.x&&y == c.y&&r == c.r;\n\t}\n};\n\nvector<Point>CircleCross(Circle a, Circle b) {\n\tvector<Point>ret;\n\tPoint ap(a.x, a.y);\n\tPoint bp(b.x, b.y);\n\tPoint dp = bp - ap;\n\tauto dis = Distance(ap, bp);\n\tif (a == b)return ret;\n\tif (a.r + b.r < Distance(ap, bp))return ret;\n\tif (abs(a.r + b.r - dis) <= EPS) {\n\t\tret.push_back(ap + dp * (a.r / (a.r + b.r)));\n\t\treturn ret;\n\t}\n\tif (abs(abs(a.r - b.r) - dis) <= EPS) {\n\t\tret.push_back(ap + dp * (a.r / (a.r - b.r)));\n\t}\n\tlong double ad;\n\tad = (a.r*a.r - b.r*b.r + dis * dis) / 2 / dis;\n\tPoint cp = ap + dp * (ad / dis);\n\tlong double amari = sqrt(a.r*a.r - ad * ad);\n\tret.push_back(cp + Point(-dp.y, dp.x)*(amari / dis));\n\tret.push_back(cp - Point(-dp.y, dp.x)*(amari / dis));\n\treturn ret;\n}\n\nvector<pair<Point, Point>>Common_Tangent(Circle a, Circle b, long double inf) {\n\tlong double pi = acos(-1);\n\tPoint ap = Point(a.x, a.y);\n\tPoint bp = Point(b.x, b.y);\n\tPoint dp = bp - ap;\n\tvector<pair<Point, Point>>ret;\n\tlong double rad = atan2(dp.y, dp.x);\n\tlong double d = hypot(dp.y, dp.x);\n\tif (a.r + b.r <= d) {\n\t\tlong double newrad = asin((a.r + b.r) / d);\n\t\t{\n\t\t\tlong double fd = hypot(inf, a.r);\n\t\t\tlong double lrad = atan2(a.r, -inf);\n\t\t\tlong double rrad = atan2(a.r, inf);\n\t\t\tpair<Point, Point>l = { Point(fd*cos(lrad + rad - newrad),fd*sin(lrad + rad - newrad)),Point(fd*cos(rrad + rad - newrad),fd*sin(rrad + rad - newrad)) };\n\t\t\tret.push_back({ l.first + ap,l.second + ap });\n\t\t}\n\t\tnewrad *= -1;\n\t\t{\n\t\t\tlong double fd = hypot(inf, -a.r);\n\t\t\tlong double lrad = atan2(-a.r, -inf);\n\t\t\tlong double rrad = atan2(-a.r, inf);\n\t\t\tpair<Point, Point>l = { Point(fd*cos(lrad + rad - newrad),fd*sin(lrad + rad - newrad)),Point(fd*cos(rrad + rad - newrad),fd*sin(rrad + rad - newrad)) };\n\t\t\tret.push_back({ l.first + ap,l.second + ap });\n\t\t}\n\t}\n\tif (abs(a.r - b.r) <= d) {\n\t\tlong double newrad = asin(abs(a.r - b.r) / d);\n\t\t{\n\t\t\tlong double fd = hypot(inf, a.r);\n\t\t\tlong double lrad = atan2(a.r, -inf);\n\t\t\tlong double rrad = atan2(a.r, inf);\n\t\t\tpair<Point, Point>l = { Point(fd*cos(lrad + rad - newrad),fd*sin(lrad + rad - newrad)),Point(fd*cos(rrad + rad - newrad),fd*sin(rrad + rad - newrad)) };\n\t\t\tret.push_back({ l.first + ap,l.second + ap });\n\t\t}\n\t\tnewrad *= -1;\n\t\t{\n\t\t\tlong double fd = hypot(inf, -a.r);\n\t\t\tlong double lrad = atan2(-a.r, -inf);\n\t\t\tlong double rrad = atan2(-a.r, inf);\n\t\t\tpair<Point, Point>l = { Point(fd*cos(lrad + rad - newrad),fd*sin(lrad + rad - newrad)),Point(fd*cos(rrad + rad - newrad),fd*sin(rrad + rad - newrad)) };\n\t\t\tret.push_back({ l.first + ap,l.second + ap });\n\t\t}\n\t}\n\treturn ret;\n}\n\nstruct Convex_hull {\n\tvector<Point>node;\n\tvector<Point>ret;\n\tbool line;\n\tConvex_hull(bool l) {\n\t\tline = l;\n\t}\n\tvoid Add_node(Point n) {\n\t\tnode.push_back(n);\n\t}\n\tvector<Point>solve() {\n\t\tsort(node.begin(), node.end());\n\t\tint index = 0;\n\t\tint num = node.size();\n\t\tret.resize(num * 2);\n\t\tfor (int i = 0; i < num; i++) {\n\t\t\tif (line) {\n\t\t\t\twhile (index > 1 && (ret[index - 1] - ret[index - 2]).det(node[i] - ret[index - 1]) < -EPS) {\n\t\t\t\t\tindex--;\n\t\t\t\t}\n\t\t\t}\n\t\t\telse {\n\t\t\t\twhile (index > 1 && (ret[index - 1] - ret[index - 2]).det(node[i] - ret[index - 1]) < EPS) {\n\t\t\t\t\tindex--;\n\t\t\t\t}\n\t\t\t}\n\t\t\tret[index++] = node[i];\n\t\t}\n\t\tint box = index;\n\t\tfor (int i = num - 2; i >= 0; i--) {\n\t\t\tif (line) {\n\t\t\t\twhile (index > box && (ret[index - 1] - ret[index - 2]).det(node[i] - ret[index - 1]) < -EPS) {\n\t\t\t\t\tindex--;\n\t\t\t\t}\n\t\t\t}\n\t\t\telse {\n\t\t\t\twhile (index > box && (ret[index - 1] - ret[index - 2]).det(node[i] - ret[index - 1]) < EPS) {\n\t\t\t\t\tindex--;\n\t\t\t\t}\n\t\t\t}\n\t\t\tret[index++] = node[i];\n\t\t}\n\t\tret.resize(index - 1);\n\t\treturn ret;\n\t}\n};\n\nint main() {\n\tios::sync_with_stdio(false);\n\tcin.tie(0);\n\n\twhile (cin >> N, N) {\n\t\tvector<Point>p(N);\n\t\tfor (int i = 0; i < N; i++) {\n\t\t\tcin >> p[i].x >> p[i].y;\n\t\t\tp[i].x += 2000;\n\t\t\tp[i].y += 2000;\n\t\t}\n\t\tint ans = 0;\n\t\tfor (int i = 0; i < 4000; i++) {\n\t\t\tvector<pair<long double, long double>>vp;\n\t\t\tauto ab = Point(EPS+i, -1);\n\t\t\tauto at = Point(EPS+i, 4001);\n\t\t\tauto bb = Point(-EPS+i + 1, -1);\n\t\t\tauto bt = Point(-EPS+i + 1, 4001);\n\t\t\tfor (int j = 0; j < N; j++) {\n\t\t\t\tint nx = (j + 1) % N;\n\t\t\t\tif (p[j].x == p[nx].x)continue;\n\t\t\t\tif (LineCross(ab, at, p[j], p[nx]) && LineCross(bb, bt, p[j], p[nx])) {\n\t\t\t\t\tvp.push_back({ LineCross(Line(ab,at),Line(p[j],p[nx])).y,LineCross(Line(bb,bt),Line(p[j],p[nx])).y });\n\t\t\t\t}\n\t\t\t}\n\t\t\tsort(vp.begin(), vp.end());\n\t\t\tif (vp.size()) {\n\t\t\t\tvector<int>sum(4002);\n\t\t\t\tfor (int j = 1; j < vp.size(); j += 2) {\n\t\t\t\t\tsum[(int)(max(vp[j].first, vp[j].second) + 1 - EPS)]--;\n\t\t\t\t\tsum[(int)(min(vp[j - 1].first, vp[j - 1].second)) + EPS]++;\n\t\t\t\t}\n\t\t\t\tfor (int i = 1; i < 4000; i++)sum[i] += sum[i - 1];\n\t\t\t\tfor (int i = 0; i < 4000; i++) {\n\t\t\t\t\tif (sum[i])ans++;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\tcout << ans << endl;\n\t}\n}", "accuracy": 1, "time_ms": 840, "memory_kb": 3480, "score_of_the_acc": -0.2637, "final_rank": 9 }, { "submission_id": "aoj_1242_4950232", "code_snippet": "#define MOD_TYPE 1\n\n#pragma region Macros\n\n#include <bits/stdc++.h>\nusing namespace std;\n\n#if 0\n#include <boost/multiprecision/cpp_int.hpp>\n#include <boost/multiprecision/cpp_dec_float.hpp>\nusing Int = boost::multiprecision::cpp_int;\nusing lld = boost::multiprecision::cpp_dec_float_100;\n#endif\n#if 1\n#pragma GCC target(\"avx2\")\n#pragma GCC optimize(\"O3\")\n#pragma GCC optimize(\"unroll-loops\")\n#endif\nusing ll = long long int;\nusing ld = long double;\nusing pii = pair<int, int>;\nusing pll = pair<ll, ll>;\nusing pld = pair<ld, ld>;\ntemplate <typename Q_type>\nusing smaller_queue = priority_queue<Q_type, vector<Q_type>, greater<Q_type>>;\n\nconstexpr ll MOD = (MOD_TYPE == 1 ? (ll)(1e9 + 7) : 998244353);\n//constexpr ll MOD = ;\nconstexpr int INF = (int)1e9 + 10;\nconstexpr ll LINF = (ll)4e18;\nconstexpr double PI = acos(-1.0);\nconstexpr double EPS = 1e-9;\nconstexpr int Dx[] = {0, 0, -1, 1, -1, 1, -1, 1, 0};\nconstexpr int Dy[] = {1, -1, 0, 0, -1, -1, 1, 1, 0};\n\n#define REP(i, m, n) for (ll i = m; i < (ll)(n); ++i)\n#define rep(i, n) REP(i, 0, n)\n#define REPI(i, m, n) for (int i = m; i < (int)(n); ++i)\n#define repi(i, n) REPI(i, 0, n)\n#define MP make_pair\n#define MT make_tuple\n#define YES(n) cout << ((n) ? \"YES\" : \"NO\") << \"\\n\"\n#define Yes(n) cout << ((n) ? \"Yes\" : \"No\") << \"\\n\"\n#define possible(n) cout << ((n) ? \"possible\" : \"impossible\") << \"\\n\"\n#define Possible(n) cout << ((n) ? \"Possible\" : \"Impossible\") << \"\\n\"\n#define Yay(n) cout << ((n) ? \"Yay!\" : \":(\") << \"\\n\"\n#define all(v) v.begin(), v.end()\n#define NP(v) next_permutation(all(v))\n#define dbg(x) cerr << #x << \":\" << x << \"\\n\";\n\nstruct io_init\n{\n io_init()\n {\n cin.tie(0);\n ios::sync_with_stdio(false);\n cout << setprecision(30) << setiosflags(ios::fixed);\n };\n} io_init;\ntemplate <typename T>\ninline bool chmin(T &a, T b)\n{\n if (a > b)\n {\n a = b;\n return true;\n }\n return false;\n}\ntemplate <typename T>\ninline bool chmax(T &a, T b)\n{\n if (a < b)\n {\n a = b;\n return true;\n }\n return false;\n}\ninline ll CEIL(ll a, ll b)\n{\n return (a + b - 1) / b;\n}\ntemplate <typename A, size_t N, typename T>\ninline void Fill(A (&array)[N], const T &val)\n{\n fill((T *)array, (T *)(array + N), val);\n}\ntemplate <typename T, typename U>\nconstexpr istream &operator>>(istream &is, pair<T, U> &p) noexcept\n{\n is >> p.first >> p.second;\n return is;\n}\ntemplate <typename T, typename U>\nconstexpr ostream &operator<<(ostream &os, pair<T, U> &p) noexcept\n{\n os << p.first << \" \" << p.second;\n return os;\n}\n#pragma endregion\n\nusing Point = pair<double, double>;\nusing Segment = pair<Point, Point>;\n\nbool intersect(Segment &s, int x)\n{\n auto [p, q] = s;\n auto [x1, y1] = p;\n auto [x2, y2] = q;\n return x1 <= x and x + 1 <= x2;\n}\n\npair<double, double> cut(Segment &s, int x)\n{\n auto [p, q] = s;\n auto [x1, y1] = p;\n auto [x2, y2] = q;\n double r1 = (y1 * (x2 - x) + y2 * (x - x1)) / (x2 - x1);\n double r2 = (y1 * (x2 - (x + 1)) + y2 * ((x + 1) - x1)) / (x2 - x1);\n if (r1 > r2)\n swap(r1, r2);\n return {r1, r2};\n}\n\nvoid solve()\n{\n int n;\n cin >> n;\n if (!n)\n exit(0);\n vector<Point> p(n);\n rep(i, n) cin >> p[i];\n vector<Segment> S(n);\n rep(i, n)\n {\n S[i] = {p[i], p[(i + 1) % n]};\n if (S[i].first > S[i].second)\n swap(S[i].first, S[i].second);\n }\n int ans = 0;\n for (int x = -2005; x <= 2005; x++)\n {\n vector<pair<double, double>> v;\n rep(i, n)\n {\n if (intersect(S[i], x))\n v.push_back(cut(S[i], x));\n }\n if (v.empty())\n continue;\n sort(all(v));\n int D = -3000;\n for (int i = 0; i + 1 < v.size(); i += 2)\n {\n int L = floor(v[i].first), R = ceil(v[i + 1].second);\n chmax(L, D);\n ans += R - L;\n D = R;\n }\n }\n cout << ans << \"\\n\";\n}\n\nint main()\n{\n while (1)\n solve();\n}", "accuracy": 1, "time_ms": 40, "memory_kb": 3456, "score_of_the_acc": -0.0271, "final_rank": 4 } ]
aoj_1250_cpp
Problem C: Leaky Cryptography The ACM ICPC judges are very careful about not leaking their problems, and all communications are encrypted. However, one does sometimes make mistakes, like using too weak an encryption scheme. Here is an example of that. The encryption chosen was very simple: encrypt each chunk of the input by flipping some bits according to a shared key. To provide reasonable security, the size of both chunk and key is 32 bits. That is, suppose the input was a sequence of m 32-bit integers. N 1 N 2 N 3 ... N m After encoding with the key K it becomes the following sequence of m 32-bit integers. ( N 1 ∧ K ) ( N 2 ∧ K ) ( N 3 ∧ K ) ... ( N m ∧ K ) where ( a ∧ b ) is the bitwise exclusive or of a and b . Exclusive or is the logical operator which is 1 when only one of its operands is 1, and 0 otherwise. Here is its definition for 1-bit integers. 0 ⊕ 0 = 0 0 ⊕ 1 = 1 1 ⊕ 0 = 1 1 ⊕ 1 =0 As you can see, it is identical to addition modulo 2. For two 32-bit integers a and b , their bitwise exclusive or a ∧ b is defined as follows, using their binary representations, composed of 0's and 1's. a ∧ b = a 31 ... a 1 a 0 ∧ b 31 ... b 1 b 0 = c 31 ... c 1 c 0 where c i = a i ⊕ b i ( i = 0, 1, ... , 31). For instance, using binary notation, 11010110 ∧ 01010101 = 10100011, or using hexadecimal, d6 ∧ 55 = a3. Since this kind of encryption is notoriously weak to statistical attacks, the message has to be compressed in advance, so that it has no statistical regularity. We suppose that N 1 N 2 ... N m is already in compressed form. However, the trouble is that the compression algorithm itself introduces some form of regularity: after every 8 integers of compressed data, it inserts a checksum, the sum of these integers. That is, in the above input, N 9 = ∑ 8 i =1 N i = N 1 + ... + N 8 , where additions are modulo 2 32 . Luckily, you could intercept a communication between the judges. Maybe it contains a problem for the finals! As you are very clever, you have certainly seen that you can easily find the lowest bit of the key, denoted by K 0 . On the one hand, if K 0 = 1, then after encoding, the lowest bit of ∑ 8 i =1 N i ∧ K is unchanged, as K 0 is added an even number of times, but the lowest bit of N 9 ∧ K is changed, so they shall differ. On the other hand, if K 0 = 0, then after encoding, the lowest bit of ∑ 8 i =1 N i ∧ K shall still be identical to the lowest bit of N 9 ∧ K , as they do not change. For instance, if the lowest bits after encoding are 1 1 1 1 1 1 1 1 1 then K 0 must be 1, but if they are 1 1 1 1 1 1 1 0 1 then K 0 must be 0. So far, so good. Can you do better? You should find the key used for encoding. Input The input starts with a line containing only a positive integer S , indicating the number of datasets in the input. S is no more than 1000. It is followed by S datasets. Each dataset is composed of nine 32-bit integers corresponding to the first nine chunks of a communication. They are written in hexadecimal notati ...(truncated)
[ { "submission_id": "aoj_1250_4304282", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\nusing ll = long long int;\nusing ull = unsigned long long int;\nusing P = pair<ll, ll>;\n\nll decode(string s){\n ll res = 0;\n for(auto c : s){\n res <<= 4;\n if(isdigit(c)){\n res |= (c-'0');\n }else{\n res |= (c-'a'+10);\n }\n }\n return res;\n}\n\nstring encode(ll x){\n if(x == 0) return \"0\";\n string res = \"\";\n const ll base = (1<<4)-1;\n while(x > 0){\n ll b = x&base;\n if(b<10){\n res.push_back('0'+b);\n }else{\n res.push_back('a'+b-10);\n }\n x >>= 4;\n }\n reverse(res.begin(), res.end());\n return res;\n}\n\nll A[9], c[32]{};\n\nvector<P> build(int a, int b){\n vector<P> res;\n int n = b-a;\n for(ll S=0;S<1<<n;S++){\n ll sum = 0;\n for(int i=0;i<n;i++){\n if((S>>i)&1){\n sum += c[a+i];\n }\n }\n sum -= ((A[8]^(S<<a))-A[8]);\n res.push_back(P(sum, S<<a));\n }\n sort(res.begin(),res.end());\n return res;\n}\n\nint solve(){\n ll S = 0;\n string str;\n for(int i=0;i<9;i++){\n cin >> str;\n A[i] = decode(str);\n if(i<8) S+=A[i];\n }\n fill(c,c+32,0);\n for(int i=0;i<8;i++){\n for(int j=0;j<32;j++){\n if((A[i]>>j)&1){\n c[j] -= 1LL<<j;\n }else{\n c[j] += 1LL<<j;\n }\n }\n }\n auto v1 = build(0,16);\n auto v2 = build(16,32);\n ll ans = -1;\n for(auto p : v1){\n ll dS = p.first;\n for(ll i=0;i<8;i++){\n auto itr = lower_bound(v2.begin(), v2.end(), P(A[8]+(i<<32)-(S+dS),0));\n if(itr != v2.end() && S+dS+(*itr).first == A[8]+(i<<32)){\n ans = (*itr).second + p.second;\n break;\n }\n }\n if(ans>=0) break;\n }\n assert(ans>=0);\n cout << encode(ans) << endl;\n return 0;\n}\n\n\nint main(){\n int t; cin >> t; for(;t>0;t--) solve();\n return 0;\n}", "accuracy": 1, "time_ms": 1650, "memory_kb": 5924, "score_of_the_acc": -1.5299, "final_rank": 3 }, { "submission_id": "aoj_1250_3633218", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\n#define rep(i, n) for(int i = 0; i < (n); i++)\n#define repp(i, l, r) for(int i = (l); i < (r); i++)\n#define per(i, n) for(int i = ((n)-1); i >= 0; i--)\n#define perr(i, l, r) for(int i = ((r)-1); i >= (l); i--)\n#define all(x) (x).begin(),(x).end()\n#define MOD 1000000007\n#define IINF 1000000000\n#define LINF 1000000000000000000\n#define SP <<\" \"<<\n#define CYES cout<<\"Yes\"<<endl\n#define CNO cout<<\"No\"<<endl\n#define CFS cin.tie(0);ios::sync_with_stdio(false)\n\ntypedef long long LL;\ntypedef long double LD;\n\nLL htoi(string &s){\n LL ans=0;\n rep(i,s.length()){\n ans*=16;\n if('0'<=s[i]&&s[i]<='9') ans+=s[i]-'0';\n else ans+=s[i]-'a'+10;\n }\n return ans;\n}\n\nstring itoh(LL x){\n string ans=\"\";\n while(x){\n if(x%16<=9) ans.push_back('0'+x%16);\n else ans.push_back('a'+x%16-10);\n x/=16;\n }\n if(ans.size()==0) ans=\"0\";\n reverse(all(ans));\n return ans;\n}\n\nint main(){\n int q;\n cin >> q;\n rep(t,q){\n string s;\n vector<LL> p(9),q(9);\n rep(i,9){\n cin >> s;\n p[i]=htoi(s);\n q[i]=p[i]%65536;\n p[i]/=65536;\n }\n map<LL,vector<LL>> mp;\n rep(i,65536){\n LL sum=0;\n rep(j,8) sum+=(p[j]^i);\n sum%=65536;\n mp[(p[8]^i)-sum].push_back(i);\n }\n rep(i,65536){\n LL sum=0;\n rep(j,8) sum+=(q[j]^i);\n if(sum%65536==(q[8]^i)){\n auto itr=mp.find(sum/65536);\n if(itr!=mp.end()){\n vector<LL> &v = itr->second;\n for(LL x:v){\n sum=0;\n rep(j,8) sum+=((p[j]*65536+q[j])^(x*65536+i));\n sum%=65536LL*65536;\n if(sum==(p[8]*65536+q[8])^(x*65536+i)){\n cout << itoh(x*65536+i) << endl;\n goto next;\n }\n }\n }\n }\n }\n next:;\n }\n return 0;\n}", "accuracy": 1, "time_ms": 940, "memory_kb": 10264, "score_of_the_acc": -1.5617, "final_rank": 4 }, { "submission_id": "aoj_1250_1816311", "code_snippet": "#include<iostream>\n#include<string>\n#include<vector>\nusing namespace std;\nlong long L[9], K[9], c[32], power[33];\nchar T[17] = \"0123456789abcdef\";\nvector<int>vec;\nint main() {\n\tpower[0] = 1; for (long long i = 1; i <= 32; i++)power[i] = power[i - 1] * 2;\n\tint n; cin >> n;\n\tfor (int t = 0; t < n; t++) {\n\t\tvec.clear(); for (long long i = 0; i < 9; i++) { L[i] = 0; K[i] = 0; }\n\t\tfor (long long i = 0; i < 32; i++)c[i] = 0;\n\t\tfor (long long i = 0; i < 9; i++) {\n\t\t\tstring S; cin >> S; long long b[8];\n\t\t\twhile (S.size() < 8)S = '0' + S;\n\t\t\tfor (long long j = 0; j < 8; j++) {\n\t\t\t\tfor (long long k = 0; k < 16; k++) {\n\t\t\t\t\tif (T[k] == S[j])b[7 - j] = k;\n\t\t\t\t}\n\t\t\t}\n\t\t\tfor (long long j = 0; j < 4; j++)L[i] += power[j * 4] * b[j];\n\t\t\tfor (long long j = 0; j < 8; j++)K[i] += power[j * 4] * b[j];\n\t\t}\n\t\tfor (long long j = 0; j < 65536; j++) {\n\t\t\tlong long S = 0;\n\t\t\tfor (long long k = 0; k < 8; k++) {\n\t\t\t\tS += j^L[k];\n\t\t\t}\n\t\t\tif (S % 65536 == (L[8] ^ j))vec.push_back(j);\n\t\t}\n\t\tfor (long long i = 0; i < vec.size(); i++) {\n\t\t\tfor (long long j = 0; j < 65536; j++) {\n\t\t\t\tlong long W = j * 65536 + vec[i], S = 0;\n\t\t\t\tfor (long long k = 0; k < 8; k++) {\n\t\t\t\t\tS += W^K[k];\n\t\t\t\t}\n\t\t\t\tif (S%power[32] == (K[8] ^ W)) {\n\t\t\t\t\tlong long OK = 0;\n\t\t\t\t\tfor (long long k = 7; k >= 0; k--) {\n\t\t\t\t\t\tchar V = T[(W / power[k * 4]) % 16];\n\t\t\t\t\t\tif (V >= '1')OK = 1;\n\t\t\t\t\t\tif (OK == 1 || k == 0)cout << V;\n\t\t\t\t\t}\n\t\t\t\t\tcout << endl;\n\t\t\t\t\tgoto E;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\tE:;\n\t}\n\treturn 0;\n}", "accuracy": 1, "time_ms": 90, "memory_kb": 1204, "score_of_the_acc": -0.0557, "final_rank": 2 }, { "submission_id": "aoj_1250_484635", "code_snippet": "//19\n#include<cstdio>\n\nint main(){\n int n;\n scanf(\"%d\",&n);\n while(n--){\n unsigned b[2][9];\n for(int i=0;i<9;i++){\n unsigned bb;\n scanf(\"%x\",&bb);\n b[1][i]=bb>>16;\n b[0][i]=bb&(1<<16)-1;\n }\n unsigned c=0;\n unsigned r=0;\n for(int i=0;i<2;i++){\n for(unsigned j=0;j<1<<16;j++){\n\tunsigned s=c;\n\tfor(int k=0;k<8;k++){\n\t s+=b[i][k]^j;\n\t}\n\tif((s&(1<<16)-1)==(b[i][8]^j)){\n\t r+=j<<(16*i);\n\t c=s>>16;\n\t break;\n\t}\n }\n }\n printf(\"%x\\n\",r);\n }\n return 0;\n}", "accuracy": 1, "time_ms": 30, "memory_kb": 1032, "score_of_the_acc": 0, "final_rank": 1 } ]
aoj_1245_cpp
Problem F: Gap Let’s play a card game called Gap. You have 28 cards labeled with two-digit numbers. The first digit (from 1 to 4) represents the suit of the card, and the second digit (from 1 to 7) represents the value of the card. First, you shuffle the cards and lay them face up on the table in four rows of seven cards, leaving a space of one card at the extreme left of each row. The following shows an example of initial layout. Next, you remove all cards of value 1, and put them in the open space at the left end of the rows: “11” to the top row, “21” to the next, and so on. Now you have 28 cards and four spaces, called gaps, in four rows and eight columns. You start moving cards from this layout. At each move, you choose one of the four gaps and fill it with the successor of the left neighbor of the gap. The successor of a card is the next card in the same suit, when it exists. For instance the successor of “42” is “43”, and “27” has no successor. In the above layout, you can move “43” to the gap at the right of “42”, or “36” to the gap at the right of “35”. If you move “43”, a new gap is generated to the right of “16”. You cannot move any card to the right of a card of value 7, nor to the right of a gap. The goal of the game is, by choosing clever moves, to make four ascending sequences of the same suit, as follows. Your task is to find the minimum number of moves to reach the goal layout. Input The input starts with a line containing the number of initial layouts that follow. Each layout consists of five lines - a blank line and four lines which represent initial layouts of four rows. Each row has seven two-digit numbers which correspond to the cards. Output For each initial layout, produce a line with the minimum number of moves to reach the goal layout. Note that this number should not include the initial four moves of the cards of value 1. If there is no move sequence from the initial layout to the goal layout, produce “-1”. Sample Input 4 12 13 14 15 16 17 21 22 23 24 25 26 27 31 32 33 34 35 36 37 41 42 43 44 45 46 47 11 26 31 13 44 21 24 42 17 45 23 25 41 36 11 46 34 14 12 37 32 47 16 43 27 35 22 33 15 17 12 16 13 15 14 11 27 22 26 23 25 24 21 37 32 36 33 35 34 31 47 42 46 43 45 44 41 27 14 22 35 32 46 33 13 17 36 24 44 21 15 43 16 45 47 23 11 26 25 37 41 34 42 12 31 Output for the Sample Input 0 33 60 -1
[ { "submission_id": "aoj_1245_10853948", "code_snippet": "#include <iostream>\n#include <cstring>\n#include <climits>\n#include <queue>\n#include <unordered_set>\n \nusing namespace std;\n \nclass State\n{\npublic:\n /**\n * 表格数字\n */\n int table[4][8];\n /**\n * 移动回数\n */\n int turn;\n \n \n State(int card[4][7])\n {\n for (int i = 0; i < 4; i++)\n table[i][0] = (10 * (i + 1) + 1);// 11 21 31 41\n for (int i = 0; i < 4; i++)\n {\n memcpy(table[i] + 1, card[i], 7 * sizeof(int));\n }\n // 从这种布局开始游戏\n for (int i = 0; i < 4; i++)\n {\n for (int j = 1; j < 8; j++)\n {\n if (table[i][j] == 11 || table[i][j] == 21 || table[i][j] == 31 || table[i][j] == 41)\n table[i][j] = 0;\n }\n }\n turn = 0;\n }\n \n State(const State &t)\n {\n memcpy(table, t.table, sizeof(table));\n turn = t.turn;\n }\n \n /**\n * 可以填充空白\n * @param x\n * @param y\n * @return\n */\n bool can_fill_gap(int x, int y)\n {\n if (table[x][y] != 0)\n return false;\n else if (table[x][y - 1] == 0 || (table[x][y - 1]) % 10 == 7) // x7 has no successor\n return false;\n else\n return true;\n }\n \n /**\n * 填充空白\n * @param x\n * @param y\n */\n void fill_gap(int x, int y)\n {\n int s, sx, sy;\n sx = sy = -1;\n \n s = table[x][y - 1] + 1;\n \n for (int i = 0; i < 4; i++)\n {\n for (int j = 0; j < 8; j++)\n {\n if (table[i][j] == s)\n {\n sx = i;\n sy = j;\n }\n }\n }\n \n table[x][y] = table[sx][sy];\n table[sx][sy] = 0;\n \n turn++;\n }\n \n /**\n * 是否是游戏结束状态,即每行都是升序,最后一列全为0\n * @return\n */\n bool done()\n {\n for (int i = 0; i < 4; i++)\n {\n for (int j = 0; j < 7; j++)\n {\n if (table[i][j] != (10 * (i + 1) + (j + 1)))\n return false;\n }\n if (table[i][7] != 0)\n return false;\n }\n \n return true;\n }\n \n bool operator==(const State &s) const\n {\n int i, j;\n for (i = 0; i < 4; i++)\n {\n for (j = 1; j < 8; j++)\n {\n if (table[i][j] != s.table[i][j])\n return false;\n }\n }\n return true;\n }\n \n};\n \nstruct StateHash\n{\n size_t operator()(const State &s) const\n {\n size_t hash = 0;\n for (int i = 0; i < 4; i++)\n {\n for (int j = 1; j < 8; j++)\n {\n hash += s.table[i][j];\n hash <<= 1;\n }\n }\n \n return hash;\n }\n};\n \nint solve(int card[4][7])\n{\n queue<State> que;\n unordered_set<State, StateHash> visited;\n //准备BFS\n bool end = false;\n int ans = INT_MAX;\n State init(card);\n if (init.done())\n return 0;\n que.push(init);\n visited.insert(init);\n //BFS\n while (!que.empty() && !end)\n {\n State s = que.front();\n que.pop();\n for (int i = 0; i < 4; i++)\n {\n for (int j = 1; j < 8; j++)\n {\n if (s.can_fill_gap(i, j))\n {\n State temp(s);\n temp.fill_gap(i, j);\n // 结束\n if (temp.done())\n {\n end = true;\n ans = temp.turn;\n }// 未遍历\n else if (visited.find(temp) == visited.end())\n {\n que.push(temp);\n visited.insert(temp);\n }\n }\n }\n }\n }\n \n if (ans == INT_MAX)\n ans = -1;\n return ans;\n}\n \nint main()\n{\n int n;\n scanf(\"%d\", &n);\n while (n--)\n {\n int card[4][7];\n for (int i = 0; i < 4; ++i)\n {\n for (int j = 0; j < 7; ++j)\n {\n scanf(\"%d\", &card[i][j]);\n }\n }\n \n printf(\"%d\\n\", solve(card));\n }\n return 0;\n}", "accuracy": 1, "time_ms": 70, "memory_kb": 14380, "score_of_the_acc": -0.0788, "final_rank": 2 }, { "submission_id": "aoj_1245_4801817", "code_snippet": "//#include<bits/stdc++.h>\n#include<iostream>\n#include<stdio.h>\n#include<queue>\n#include<string>\n#include<cstring>\n#include<algorithm>\n#include<map>\n#include<unordered_set>\nusing namespace std;\n#define inf 0x3f3f3f3f\n\ntypedef long long ll;\ntypedef unsigned long long ULL;\ntypedef pair<int,int> P;\n\nclass State{\npublic:\n int arr[4][8];\n int times;\n\n State(int a[][7]){\n times=0;\n arr[0][0]=11;\n arr[1][0]=21;\n arr[2][0]=31;\n arr[3][0]=41;\n for(int i=0;i<4;i++){\n memcpy(arr[i]+1,a[i],sizeof(int)*7);\n }\n for(int i=0;i<4;i++){\n for(int j=1;j<=7;j++){\n if(arr[i][j]==11||arr[i][j]==21||arr[i][j]==31||arr[i][j]==41){\n arr[i][j]=0;\n }\n }\n }\n }\n\n bool operator == (const State& y)const{\n for(int i=0;i<4;i++){\n for(int j=0;j<8;j++){\n if(arr[i][j]!=y.arr[i][j]){\n return false;\n }\n }\n }\n return true;\n }\n\n State(const State& y){\n memcpy(arr,y.arr,sizeof(arr));\n times=y.times;\n }\n\n bool can_fill(int x,int y){\n if(arr[x][y]!=0){\n return false;\n }\n if(arr[x][y-1]==0||arr[x][y-1]%10==7){\n return false;\n }\n return true;\n }\n\n void fill_gap(int x,int y){\n int tx,ty;\n int target=arr[x][y-1]+1;\n for(int i=0;i<4;i++){\n for(int j=1;j<8;j++){\n if(arr[i][j]==target){\n tx=i;\n ty=j;\n break;\n }\n }\n }\n arr[x][y]=target;\n arr[tx][ty]=0;\n times++;\n }\n\n bool done(){\n for(int i=0;i<4;i++){\n for(int j=0;j<7;j++){\n if(arr[i][j]!=(i+1)*10+(j+1)){\n return false;\n }\n }\n if(arr[i][7]!=0){\n return false;\n }\n }\n return true;\n }\n\n};\n\nstruct State_Hash{\n size_t operator() (const State &x)const{\n size_t hash=0;\n for(int i=0;i<4;i++){\n for(int j=0;j<8;j++){\n hash+=x.arr[i][j];\n hash<<=1;\n }\n }\n return hash;\n }\n};\n\nint bfs(int a[4][7]){\n unordered_set<State,State_Hash> visit;\n State init(a);\n if(init.done()){\n return 0;\n }\n visit.insert(init);\n queue<State> Q;\n Q.push(init);\n while(!Q.empty()){\n State now=Q.front();\n Q.pop();\n for(int i=0;i<4;i++){\n for(int j=1;j<8;j++){\n if(now.can_fill(i,j)){\n State newS(now);\n newS.fill_gap(i,j);\n if(visit.find(newS)!=visit.end()){\n continue;\n }\n if(newS.done()){\n return newS.times;\n }\n Q.push(newS);\n visit.insert(newS);\n }\n }\n }\n }\n return -1;\n}\n\n\nint main()\n{\n int T;\n scanf(\"%d\",&T);\n while(T--){\n int a[4][7];\n for(int i=0;i<4;i++){\n for(int j=0;j<7;j++){\n scanf(\"%d\",&a[i][j]);\n }\n }\n printf(\"%d\\n\",bfs(a));\n }\n return 0;\n}", "accuracy": 1, "time_ms": 90, "memory_kb": 15232, "score_of_the_acc": -0.106, "final_rank": 4 }, { "submission_id": "aoj_1245_4001769", "code_snippet": "#include <iostream>\n#include <cstring>\n#include <climits>\n#include <queue>\n#include <unordered_set>\n \nusing namespace std;\n \nclass State\n{\npublic:\n /**\n * 表格数字\n */\n int table[4][8];\n /**\n * 移动回数\n */\n int turn;\n \n \n State(int card[4][7])\n {\n for (int i = 0; i < 4; i++)\n table[i][0] = (10 * (i + 1) + 1);// 11 21 31 41\n for (int i = 0; i < 4; i++)\n {\n memcpy(table[i] + 1, card[i], 7 * sizeof(int));\n }\n // 从这种布局开始游戏\n for (int i = 0; i < 4; i++)\n {\n for (int j = 1; j < 8; j++)\n {\n if (table[i][j] == 11 || table[i][j] == 21 || table[i][j] == 31 || table[i][j] == 41)\n table[i][j] = 0;\n }\n }\n turn = 0;\n }\n \n State(const State &t)\n {\n memcpy(table, t.table, sizeof(table));\n turn = t.turn;\n }\n \n /**\n * 可以填充空白\n * @param x\n * @param y\n * @return\n */\n bool can_fill_gap(int x, int y)\n {\n if (table[x][y] != 0)\n return false;\n else if (table[x][y - 1] == 0 || (table[x][y - 1]) % 10 == 7) // x7 has no successor\n return false;\n else\n return true;\n }\n \n /**\n * 填充空白\n * @param x\n * @param y\n */\n void fill_gap(int x, int y)\n {\n int s, sx, sy;\n sx = sy = -1;\n \n s = table[x][y - 1] + 1;\n \n for (int i = 0; i < 4; i++)\n {\n for (int j = 0; j < 8; j++)\n {\n if (table[i][j] == s)\n {\n sx = i;\n sy = j;\n }\n }\n }\n \n table[x][y] = table[sx][sy];\n table[sx][sy] = 0;\n \n turn++;\n }\n \n /**\n * 是否是游戏结束状态,即每行都是升序,最后一列全为0\n * @return\n */\n bool done()\n {\n for (int i = 0; i < 4; i++)\n {\n for (int j = 0; j < 7; j++)\n {\n if (table[i][j] != (10 * (i + 1) + (j + 1)))\n return false;\n }\n if (table[i][7] != 0)\n return false;\n }\n \n return true;\n }\n \n bool operator==(const State &s) const\n {\n int i, j;\n for (i = 0; i < 4; i++)\n {\n for (j = 1; j < 8; j++)\n {\n if (table[i][j] != s.table[i][j])\n return false;\n }\n }\n return true;\n }\n \n};\n \nstruct StateHash\n{\n size_t operator()(const State &s) const\n {\n size_t hash = 0;\n for (int i = 0; i < 4; i++)\n {\n for (int j = 1; j < 8; j++)\n {\n hash += s.table[i][j];\n hash <<= 1;\n }\n }\n \n return hash;\n }\n};\n \nint solve(int card[4][7])\n{\n queue<State> que;\n unordered_set<State, StateHash> visited;\n //准备BFS\n bool end = false;\n int ans = INT_MAX;\n State init(card);\n if (init.done())\n return 0;\n que.push(init);\n visited.insert(init);\n //BFS\n while (!que.empty() && !end)\n {\n State s = que.front();\n que.pop();\n for (int i = 0; i < 4; i++)\n {\n for (int j = 1; j < 8; j++)\n {\n if (s.can_fill_gap(i, j))\n {\n State temp(s);\n temp.fill_gap(i, j);\n // 结束\n if (temp.done())\n {\n end = true;\n ans = temp.turn;\n }// 未遍历\n else if (visited.find(temp) == visited.end())\n {\n que.push(temp);\n visited.insert(temp);\n }\n }\n }\n }\n }\n \n if (ans == INT_MAX)\n ans = -1;\n return ans;\n}\n \nint main()\n{\n int n;\n scanf(\"%d\", &n);\n while (n--)\n {\n int card[4][7];\n for (int i = 0; i < 4; ++i)\n {\n for (int j = 0; j < 7; ++j)\n {\n scanf(\"%d\", &card[i][j]);\n }\n }\n \n printf(\"%d\\n\", solve(card));\n }\n return 0;\n}", "accuracy": 1, "time_ms": 90, "memory_kb": 15196, "score_of_the_acc": -0.1051, "final_rank": 3 }, { "submission_id": "aoj_1245_2652704", "code_snippet": "#include <stdio.h>\n#include <cmath>\n#include <algorithm>\n#include <cfloat>\n#include <stack>\n#include <queue>\n#include <vector>\n#include <string>\n#include <iostream>\n#include <set>\n#include <map>\n#include <time.h>\ntypedef long long int ll;\ntypedef unsigned long long int ull;\n#define BIG_NUM 2000000000\n#define MOD 1000000007\n#define EPS 0.000000001\nusing namespace std;\n\nstruct Data{\n\tvoid set(int arg_row,int arg_col){\n\t\trow = arg_row;\n\t\tcol = arg_col;\n\t}\n\tint row,col;\n};\n\nstruct Info{\n\tint table[4][8],count;\n\tData sace_loc[4];\n\tData num_loc[48];\n};\n\nmap<string,bool> MAP;\nint ans_table[4][8];\n\nstring makeString(int table[4][8]){\n\n\tstring ret;\n\tfor(int row = 0; row < 4; row++){\n\t\tfor(int col = 0; col < 8; col++){\n\t\t\tret.append(to_string(table[row][col]));\n\t\t}\n\t}\n\treturn ret;\n}\n\nbool checkClear(int table[4][8]){\n\n\tfor(int row = 0; row < 4; row++){\n\t\tfor(int col = 0; col < 8; col++){\n\t\t\tif(table[row][col] != ans_table[row][col])return false;\n\t\t}\n\t}\n\n\treturn true;\n}\n\nvoid copyInfo(Info& to,Info from){\n\n\tfor(int row = 0; row < 4; row++){\n\t\tfor(int col = 0; col < 8; col++){\n\t\t\tto.table[row][col] = from.table[row][col];\n\t\t}\n\t}\n\n\tfor(int i = 0; i < 4; i++){\n\t\tto.sace_loc[i] = from.sace_loc[i];\n\t}\n\n\tfor(int i = 0; i < 48; i++)to.num_loc[i] = from.num_loc[i];\n}\n\n\nvoid func(){\n\tMAP.clear();\n\n\tInfo first;\n\tfirst.table[0][0] = 0;\n\tfirst.table[1][0] = 0;\n\tfirst.table[2][0] = 0;\n\tfirst.table[3][0] = 0;\n\n\tfor(int row = 0; row < 4; row++){\n\t\tfor(int col = 1; col <= 7; col++){\n\t\t\tscanf(\"%d\",&first.table[row][col]);\n\t\t\tswitch(first.table[row][col]){\n\t\t\tcase 11:\n\t\t\t\tfirst.table[0][0] = 11;\n\t\t\t\tfirst.table[row][col] = 0;\n\t\t\t\tfirst.sace_loc[0].set(row,col);\n\t\t\t\tbreak;\n\t\t\tcase 21:\n\t\t\t\tfirst.table[1][0] = 21;\n\t\t\t\tfirst.table[row][col] = 0;\n\t\t\t\tfirst.sace_loc[1].set(row,col);\n\t\t\t\tbreak;\n\t\t\tcase 31:\n\t\t\t\tfirst.table[2][0] = 31;\n\t\t\t\tfirst.table[row][col] = 0;\n\t\t\t\tfirst.sace_loc[2].set(row,col);\n\t\t\t\tbreak;\n\t\t\tcase 41:\n\t\t\t\tfirst.table[3][0] = 41;\n\t\t\t\tfirst.table[row][col] = 0;\n\t\t\t\tfirst.sace_loc[3].set(row,col);\n\t\t\t\tbreak;\n\t\t\tdefault:\n\t\t\t\tfirst.num_loc[first.table[row][col]].set(row,col);\n\t\t\t\tbreak;\n\t\t\t}\n\t\t}\n\t}\n\n\tfirst.count = 0;\n\tstring tmp = makeString(first.table);\n\tMAP[tmp] = true;\n\n\tqueue<Info> Q;\n\tQ.push(first);\n\n\tint left_num;\n\n\tData tmp_data;\n\n\twhile(!Q.empty()){\n\n\t\tif(checkClear(Q.front().table)){\n\t\t\tprintf(\"%d\\n\",Q.front().count);\n\t\t\treturn;\n\t\t}else{\n\n\t\t\tfor(int i = 0; i < 4; i++){\n\n\t\t\t\tleft_num = Q.front().table[Q.front().sace_loc[i].row][Q.front().sace_loc[i].col-1];\n\n\t\t\t\tif(left_num == 0 || left_num%10 == 7)continue;\n\n\t\t\t\ttmp_data = Q.front().num_loc[left_num+1];\n\n\t\t\t\tInfo next_info;\n\t\t\t\tcopyInfo(next_info,Q.front());\n\t\t\t\tnext_info.table[Q.front().sace_loc[i].row][Q.front().sace_loc[i].col] = left_num+1;\n\t\t\t\tnext_info.table[tmp_data.row][tmp_data.col] = 0;\n\t\t\t\tnext_info.num_loc[left_num+1].set(Q.front().sace_loc[i].row,Q.front().sace_loc[i].col);\n\n\t\t\t\ttmp = makeString(next_info.table);\n\n\t\t\t\tauto at = MAP.find(tmp);\n\t\t\t\tif(at != MAP.end())continue;\n\n\t\t\t\tMAP[tmp] = true;\n\t\t\t\tnext_info.sace_loc[i].set(tmp_data.row,tmp_data.col);\n\t\t\t\tnext_info.count = Q.front().count+1;\n\t\t\t\tQ.push(next_info);\n\t\t\t}\n\t\t\tQ.pop();\n\t\t}\n\t}\n\n\tprintf(\"-1\\n\");\n}\n\nint main(){\n\n\tfor(int col = 0; col <= 6; col++)ans_table[0][col] = 11+col;\n\tans_table[0][7] = 0;\n\tfor(int col = 0; col <= 6; col++)ans_table[1][col] = 21+col;\n\tans_table[1][7] = 0;\n\tfor(int col = 0; col <= 6; col++)ans_table[2][col] = 31+col;\n\tans_table[2][7] = 0;\n\tfor(int col = 0; col <= 6; col++)ans_table[3][col] = 41+col;\n\tans_table[3][7] = 0;\n\n\tint case_num;\n\tscanf(\"%d\",&case_num);\n\n\tfor(int i = 0; i < case_num; i++)func();\n\n}", "accuracy": 1, "time_ms": 2390, "memory_kb": 14780, "score_of_the_acc": -0.6353, "final_rank": 12 }, { "submission_id": "aoj_1245_2652703", "code_snippet": "#include <stdio.h>\n#include <cmath>\n#include <algorithm>\n#include <cfloat>\n#include <stack>\n#include <queue>\n#include <vector>\n#include <string>\n#include <iostream>\n#include <set>\n#include <map>\n#include <time.h>\ntypedef long long int ll;\ntypedef unsigned long long int ull;\n#define BIG_NUM 2000000000\n#define MOD 1000000007\n#define EPS 0.000000001\nusing namespace std;\n\nstruct Data{\n\tvoid set(int arg_row,int arg_col){\n\t\trow = arg_row;\n\t\tcol = arg_col;\n\t}\n\tint row,col;\n};\n\nstruct Info{\n\tint table[4][8],count;\n\tData sace_loc[4];\n\tData num_loc[48];\n};\n\nmap<string,bool> MAP;\nint ans_table[4][8];\n\nstring makeString(int table[4][8]){\n\n\tstring ret;\n\tfor(int row = 0; row < 4; row++){\n\t\tfor(int col = 0; col < 8; col++){\n\t\t\tret.append(to_string(table[row][col])).append(to_string('*'));\n\t\t}\n\t}\n\treturn ret;\n}\n\nbool checkClear(int table[4][8]){\n\n\tfor(int row = 0; row < 4; row++){\n\t\tfor(int col = 0; col < 8; col++){\n\t\t\tif(table[row][col] != ans_table[row][col])return false;\n\t\t}\n\t}\n\n\treturn true;\n}\n\nvoid copyInfo(Info& to,Info from){\n\n\tfor(int row = 0; row < 4; row++){\n\t\tfor(int col = 0; col < 8; col++){\n\t\t\tto.table[row][col] = from.table[row][col];\n\t\t}\n\t}\n\n\tfor(int i = 0; i < 4; i++){\n\t\tto.sace_loc[i] = from.sace_loc[i];\n\t}\n\n\tfor(int i = 0; i < 48; i++)to.num_loc[i] = from.num_loc[i];\n}\n\n\nvoid func(){\n\tMAP.clear();\n\n\tInfo first;\n\tfirst.table[0][0] = 0;\n\tfirst.table[1][0] = 0;\n\tfirst.table[2][0] = 0;\n\tfirst.table[3][0] = 0;\n\n\tfor(int row = 0; row < 4; row++){\n\t\tfor(int col = 1; col <= 7; col++){\n\t\t\tscanf(\"%d\",&first.table[row][col]);\n\t\t\tswitch(first.table[row][col]){\n\t\t\tcase 11:\n\t\t\t\tfirst.table[0][0] = 11;\n\t\t\t\tfirst.table[row][col] = 0;\n\t\t\t\tfirst.sace_loc[0].set(row,col);\n\t\t\t\tbreak;\n\t\t\tcase 21:\n\t\t\t\tfirst.table[1][0] = 21;\n\t\t\t\tfirst.table[row][col] = 0;\n\t\t\t\tfirst.sace_loc[1].set(row,col);\n\t\t\t\tbreak;\n\t\t\tcase 31:\n\t\t\t\tfirst.table[2][0] = 31;\n\t\t\t\tfirst.table[row][col] = 0;\n\t\t\t\tfirst.sace_loc[2].set(row,col);\n\t\t\t\tbreak;\n\t\t\tcase 41:\n\t\t\t\tfirst.table[3][0] = 41;\n\t\t\t\tfirst.table[row][col] = 0;\n\t\t\t\tfirst.sace_loc[3].set(row,col);\n\t\t\t\tbreak;\n\t\t\tdefault:\n\t\t\t\tfirst.num_loc[first.table[row][col]].set(row,col);\n\t\t\t\tbreak;\n\t\t\t}\n\t\t}\n\t}\n\n\tfirst.count = 0;\n\tstring tmp = makeString(first.table);\n\tMAP[tmp] = true;\n\n\tqueue<Info> Q;\n\tQ.push(first);\n\n\tint left_num;\n\n\tData tmp_data;\n\n\twhile(!Q.empty()){\n\n\t\tif(checkClear(Q.front().table)){\n\t\t\tprintf(\"%d\\n\",Q.front().count);\n\t\t\treturn;\n\t\t}else{\n\n\t\t\tfor(int i = 0; i < 4; i++){\n\n\t\t\t\tleft_num = Q.front().table[Q.front().sace_loc[i].row][Q.front().sace_loc[i].col-1];\n\n\t\t\t\tif(left_num == 0 || left_num%10 == 7)continue;\n\n\t\t\t\ttmp_data = Q.front().num_loc[left_num+1];\n\n\t\t\t\tInfo next_info;\n\t\t\t\tcopyInfo(next_info,Q.front());\n\t\t\t\tnext_info.table[Q.front().sace_loc[i].row][Q.front().sace_loc[i].col] = left_num+1;\n\t\t\t\tnext_info.table[tmp_data.row][tmp_data.col] = 0;\n\t\t\t\tnext_info.num_loc[left_num+1].set(Q.front().sace_loc[i].row,Q.front().sace_loc[i].col);\n\n\t\t\t\ttmp = makeString(next_info.table);\n\n\t\t\t\tauto at = MAP.find(tmp);\n\t\t\t\tif(at != MAP.end())continue;\n\n\t\t\t\tMAP[tmp] = true;\n\t\t\t\tnext_info.sace_loc[i].set(tmp_data.row,tmp_data.col);\n\t\t\t\tnext_info.count = Q.front().count+1;\n\t\t\t\tQ.push(next_info);\n\t\t\t}\n\t\t\tQ.pop();\n\t\t}\n\t}\n\n\tprintf(\"-1\\n\");\n}\n\nint main(){\n\n\tfor(int col = 0; col <= 6; col++)ans_table[0][col] = 11+col;\n\tans_table[0][7] = 0;\n\tfor(int col = 0; col <= 6; col++)ans_table[1][col] = 21+col;\n\tans_table[1][7] = 0;\n\tfor(int col = 0; col <= 6; col++)ans_table[2][col] = 31+col;\n\tans_table[2][7] = 0;\n\tfor(int col = 0; col <= 6; col++)ans_table[3][col] = 41+col;\n\tans_table[3][7] = 0;\n\n\tint case_num;\n\tscanf(\"%d\",&case_num);\n\n\tfor(int i = 0; i < case_num; i++)func();\n\n}", "accuracy": 1, "time_ms": 4320, "memory_kb": 19232, "score_of_the_acc": -1.207, "final_rank": 18 }, { "submission_id": "aoj_1245_2652684", "code_snippet": "#include <stdio.h>\n#include <cmath>\n#include <algorithm>\n#include <cfloat>\n#include <stack>\n#include <queue>\n#include <vector>\n#include <string>\n#include <iostream>\n#include <set>\n#include <map>\n#include <time.h>\ntypedef long long int ll;\ntypedef unsigned long long int ull;\n#define BIG_NUM 2000000000\n#define MOD 1000000007\n#define EPS 0.000000001\nusing namespace std;\n\nstruct Data{\n\tvoid set(int arg_row,int arg_col){\n\t\trow = arg_row;\n\t\tcol = arg_col;\n\t}\n\tint row,col;\n};\n\nstruct Info{\n\tint table[4][8],count;\n\tData sace_loc[4];\n};\n\nmap<string,bool> MAP;\nint ans_table[4][8];\n\nstring makeString(int table[4][8]){\n\n\tstring ret;\n\tfor(int row = 0; row < 4; row++){\n\t\tfor(int col = 0; col < 8; col++){\n\t\t\tret.append(to_string(table[row][col])).append(to_string('*'));\n\t\t}\n\t}\n\treturn ret;\n}\n\nbool checkClear(int table[4][8]){\n\n\tfor(int row = 0; row < 4; row++){\n\t\tfor(int col = 0; col < 8; col++){\n\t\t\tif(table[row][col] != ans_table[row][col])return false;\n\t\t}\n\t}\n\n\treturn true;\n}\n\nvoid copyInfo(Info& to,Info from){\n\n\tfor(int row = 0; row < 4; row++){\n\t\tfor(int col = 0; col < 8; col++){\n\t\t\tto.table[row][col] = from.table[row][col];\n\t\t}\n\t}\n\n\tfor(int i = 0; i < 4; i++){\n\t\tto.sace_loc[i] = from.sace_loc[i];\n\t}\n}\n\n\nvoid func(){\n\tMAP.clear();\n\n\tInfo first;\n\tfirst.table[0][0] = 0;\n\tfirst.table[1][0] = 0;\n\tfirst.table[2][0] = 0;\n\tfirst.table[3][0] = 0;\n\n\tfor(int row = 0; row < 4; row++){\n\t\tfor(int col = 1; col <= 7; col++){\n\t\t\tscanf(\"%d\",&first.table[row][col]);\n\t\t\tswitch(first.table[row][col]){\n\t\t\tcase 11:\n\t\t\t\tfirst.table[0][0] = 11;\n\t\t\t\tfirst.table[row][col] = 0;\n\t\t\t\tfirst.sace_loc[0].set(row,col);\n\t\t\t\tbreak;\n\t\t\tcase 21:\n\t\t\t\tfirst.table[1][0] = 21;\n\t\t\t\tfirst.table[row][col] = 0;\n\t\t\t\tfirst.sace_loc[1].set(row,col);\n\t\t\t\tbreak;\n\t\t\tcase 31:\n\t\t\t\tfirst.table[2][0] = 31;\n\t\t\t\tfirst.table[row][col] = 0;\n\t\t\t\tfirst.sace_loc[2].set(row,col);\n\t\t\t\tbreak;\n\t\t\tcase 41:\n\t\t\t\tfirst.table[3][0] = 41;\n\t\t\t\tfirst.table[row][col] = 0;\n\t\t\t\tfirst.sace_loc[3].set(row,col);\n\t\t\t\tbreak;\n\t\t\t}\n\t\t}\n\t}\n\n\tfirst.count = 0;\n\tstring tmp = makeString(first.table);\n\tMAP[tmp] = true;\n\n\tqueue<Info> Q;\n\tQ.push(first);\n\n\tint left_num,tmp_row,tmp_col;\n\n\twhile(!Q.empty()){\n\n\t\tif(checkClear(Q.front().table)){\n\t\t\tprintf(\"%d\\n\",Q.front().count);\n\t\t\treturn;\n\t\t}else{\n\n\t\t\tfor(int i = 0; i < 4; i++){\n\n\t\t\t\tleft_num = Q.front().table[Q.front().sace_loc[i].row][Q.front().sace_loc[i].col-1];\n\n\t\t\t\tif(left_num%7 == 0){\n\t\t\t\t\tswitch(left_num){\n\t\t\t\t\tcase 17:\n\t\t\t\t\t\tif(Q.front().sace_loc[i].row == 0 && Q.front().sace_loc[i].col == 7)continue;\n\t\t\t\t\t\tbreak;\n\t\t\t\t\tcase 27:\n\t\t\t\t\t\tif(Q.front().sace_loc[i].row == 1 && Q.front().sace_loc[i].col == 7)continue;\n\t\t\t\t\t\tbreak;\n\t\t\t\t\tcase 37:\n\t\t\t\t\t\tif(Q.front().sace_loc[i].row == 2 && Q.front().sace_loc[i].col == 7)continue;\n\t\t\t\t\t\tbreak;\n\t\t\t\t\tcase 47:\n\t\t\t\t\t\tif(Q.front().sace_loc[i].row == 3 && Q.front().sace_loc[i].col == 7)continue;\n\t\t\t\t\t\tbreak;\n\t\t\t\t\t}\n\t\t\t\t}\n\n\t\t\t\ttmp_row = -1;\n\t\t\t\tfor(int row = 0; row < 4; row++){\n\t\t\t\t\tfor(int col = 0; col < 8; col++){\n\t\t\t\t\t\tif(Q.front().table[row][col] == left_num+1){\n\t\t\t\t\t\t\ttmp_row = row;\n\t\t\t\t\t\t\ttmp_col = col;\n\t\t\t\t\t\t\tbreak;\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t\tif(tmp_row != -1)break;\n\t\t\t\t}\n\n\t\t\t\tif(tmp_row == -1)continue;\n\n\t\t\t\tInfo next_info;\n\t\t\t\tcopyInfo(next_info,Q.front());\n\t\t\t\tnext_info.table[Q.front().sace_loc[i].row][Q.front().sace_loc[i].col] = left_num+1;\n\t\t\t\tnext_info.table[tmp_row][tmp_col] = 0;\n\n\t\t\t\ttmp = makeString(next_info.table);\n\n\t\t\t\tauto at = MAP.find(tmp);\n\t\t\t\tif(at != MAP.end())continue;\n\n\t\t\t\tMAP[tmp] = true;\n\t\t\t\tnext_info.sace_loc[i].set(tmp_row,tmp_col);\n\t\t\t\tnext_info.count = Q.front().count+1;\n\t\t\t\tQ.push(next_info);\n\t\t\t}\n\t\t\tQ.pop();\n\t\t}\n\t}\n\n\tprintf(\"-1\\n\");\n}\n\nint main(){\n\n\tfor(int col = 0; col <= 6; col++)ans_table[0][col] = 11+col;\n\tans_table[0][7] = 0;\n\tfor(int col = 0; col <= 6; col++)ans_table[1][col] = 21+col;\n\tans_table[1][7] = 0;\n\tfor(int col = 0; col <= 6; col++)ans_table[2][col] = 31+col;\n\tans_table[2][7] = 0;\n\tfor(int col = 0; col <= 6; col++)ans_table[3][col] = 41+col;\n\tans_table[3][7] = 0;\n\n\tint case_num;\n\tscanf(\"%d\",&case_num);\n\n\tfor(int i = 0; i < case_num; i++)func();\n\n}", "accuracy": 1, "time_ms": 4270, "memory_kb": 19448, "score_of_the_acc": -1.201, "final_rank": 16 }, { "submission_id": "aoj_1245_2652678", "code_snippet": "#include <stdio.h>\n#include <cmath>\n#include <algorithm>\n#include <cfloat>\n#include <stack>\n#include <queue>\n#include <vector>\n#include <string>\n#include <iostream>\n#include <set>\n#include <map>\n#include <time.h>\ntypedef long long int ll;\ntypedef unsigned long long int ull;\n#define BIG_NUM 2000000000\n#define MOD 1000000007\n#define EPS 0.000000001\nusing namespace std;\n\nstruct Data{\n\tvoid set(int arg_row,int arg_col){\n\t\trow = arg_row;\n\t\tcol = arg_col;\n\t}\n\tint row,col;\n};\n\nstruct Info{\n\tint table[4][8],count;\n\tData sace_loc[4];\n};\n\nmap<string,bool> MAP;\nint ans_table[4][8];\n\nstring makeString(int table[4][8]){\n\n\tstring ret;\n\tfor(int row = 0; row < 4; row++){\n\t\tfor(int col = 0; col < 8; col++){\n\t\t\tret.append(to_string(table[row][col])).append(to_string('*'));\n\t\t}\n\t}\n\treturn ret;\n}\n\nbool checkClear(int table[4][8]){\n\n\tfor(int row = 0; row < 4; row++){\n\t\tfor(int col = 0; col < 8; col++){\n\t\t\tif(table[row][col] != ans_table[row][col])return false;\n\t\t}\n\t}\n\n\treturn true;\n}\n\nvoid copyInfo(Info& to,Info from){\n\n\tfor(int row = 0; row < 4; row++){\n\t\tfor(int col = 0; col < 8; col++){\n\t\t\tto.table[row][col] = from.table[row][col];\n\t\t}\n\t}\n\n\tfor(int i = 0; i < 4; i++){\n\t\tto.sace_loc[i] = from.sace_loc[i];\n\t}\n}\n\n\nvoid func(){\n\tMAP.clear();\n\n\tInfo first;\n\tfirst.table[0][0] = 0;\n\tfirst.table[1][0] = 0;\n\tfirst.table[2][0] = 0;\n\tfirst.table[3][0] = 0;\n\n\tfor(int row = 0; row < 4; row++){\n\t\tfor(int col = 1; col <= 7; col++){\n\t\t\tscanf(\"%d\",&first.table[row][col]);\n\t\t\tswitch(first.table[row][col]){\n\t\t\tcase 11:\n\t\t\t\tfirst.table[0][0] = 11;\n\t\t\t\tfirst.table[row][col] = 0;\n\t\t\t\tfirst.sace_loc[0].set(row,col);\n\t\t\t\tbreak;\n\t\t\tcase 21:\n\t\t\t\tfirst.table[1][0] = 21;\n\t\t\t\tfirst.table[row][col] = 0;\n\t\t\t\tfirst.sace_loc[1].set(row,col);\n\t\t\t\tbreak;\n\t\t\tcase 31:\n\t\t\t\tfirst.table[2][0] = 31;\n\t\t\t\tfirst.table[row][col] = 0;\n\t\t\t\tfirst.sace_loc[2].set(row,col);\n\t\t\t\tbreak;\n\t\t\tcase 41:\n\t\t\t\tfirst.table[3][0] = 41;\n\t\t\t\tfirst.table[row][col] = 0;\n\t\t\t\tfirst.sace_loc[3].set(row,col);\n\t\t\t\tbreak;\n\t\t\t}\n\t\t}\n\t}\n\n\tfirst.count = 0;\n\tstring tmp = makeString(first.table);\n\tMAP[tmp] = true;\n\n\tqueue<Info> Q;\n\tQ.push(first);\n\n\tint left_num,tmp_row,tmp_col;\n\n\twhile(!Q.empty()){\n\n\t\tif(checkClear(Q.front().table)){\n\t\t\tprintf(\"%d\\n\",Q.front().count);\n\t\t\treturn;\n\t\t}else{\n\n\t\t\tfor(int i = 0; i < 4; i++){\n\n\t\t\t\tleft_num = Q.front().table[Q.front().sace_loc[i].row][Q.front().sace_loc[i].col-1];\n\n\t\t\t\ttmp_row = -1;\n\t\t\t\tfor(int row = 0; row < 4; row++){\n\t\t\t\t\tfor(int col = 0; col < 8; col++){\n\t\t\t\t\t\tif(Q.front().table[row][col] == left_num+1){\n\t\t\t\t\t\t\ttmp_row = row;\n\t\t\t\t\t\t\ttmp_col = col;\n\t\t\t\t\t\t\tbreak;\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t\tif(tmp_row != -1)break;\n\t\t\t\t}\n\n\t\t\t\tif(tmp_row == -1)continue;\n\n\t\t\t\tInfo next_info;\n\t\t\t\tcopyInfo(next_info,Q.front());\n\t\t\t\tnext_info.table[Q.front().sace_loc[i].row][Q.front().sace_loc[i].col] = left_num+1;\n\t\t\t\tnext_info.table[tmp_row][tmp_col] = 0;\n\n\t\t\t\ttmp = makeString(next_info.table);\n\n\t\t\t\tauto at = MAP.find(tmp);\n\t\t\t\tif(at != MAP.end())continue;\n\n\t\t\t\tMAP[tmp] = true;\n\t\t\t\tnext_info.sace_loc[i].set(tmp_row,tmp_col);\n\t\t\t\tnext_info.count = Q.front().count+1;\n\t\t\t\tQ.push(next_info);\n\t\t\t}\n\t\t\tQ.pop();\n\t\t}\n\t}\n\n\tprintf(\"-1\\n\");\n}\n\nint main(){\n\n\tfor(int col = 0; col <= 6; col++)ans_table[0][col] = 11+col;\n\tans_table[0][7] = 0;\n\tfor(int col = 0; col <= 6; col++)ans_table[1][col] = 21+col;\n\tans_table[1][7] = 0;\n\tfor(int col = 0; col <= 6; col++)ans_table[2][col] = 31+col;\n\tans_table[2][7] = 0;\n\tfor(int col = 0; col <= 6; col++)ans_table[3][col] = 41+col;\n\tans_table[3][7] = 0;\n\n\tint case_num;\n\tscanf(\"%d\",&case_num);\n\n\tfor(int i = 0; i < case_num; i++)func();\n\n}", "accuracy": 1, "time_ms": 4290, "memory_kb": 19480, "score_of_the_acc": -1.2065, "final_rank": 17 }, { "submission_id": "aoj_1245_2273114", "code_snippet": "/*\n * POJ 2046: Gap\n * ?¢?????????????4?\\?1???7?????????????????¨4???8????????????????????°?°?1????????????????????¨?¬¬???????????¶???????¬???????????????????????°?????????§????±?????????????????????§??????????????¨????????\\???????????????????±???¨??¨????????????????°?????????\\??°???\n * ?±???????BFS\n * ?????????????????¶?????????????¬?????????¨set??????????????????????????¶?????????4????????????????????\\???????????°???????????????????????¨????°????????????¬?§????\n */\n\n#include <cstdio>\n#include <string>\n#include <cstring>\n#include <queue>\n#include <vector>\n#include <set>\n#include <unordered_set>\n\nusing namespace std;\n\n\nunordered_set<string> ss;\n\nstruct S {\n string s;\n int p[29];\n int b[4];\n int d;\n\n S() {}\n\n S(const string &t) : s(t), d(0) {\n int cnt = 0;\n for (int i = 0; i < 32; ++i) {\n if (s[i] == 30) {\n b[cnt++] = i;\n } else {\n p[s[i]] = i;\n }\n }\n }\n\n S(const S &st) : s(st.s), d(st.d) {\n memcpy(p, st.p, sizeof(p));\n memcpy(b, st.b, sizeof(b));\n }\n\n bool operator<(const S &st) const {\n return d > st.d;\n }\n};\n\npriority_queue<S> pq;\n\n\nint main() {\n string target(32, ' ');\n int cnt = 0;\n for (int i = 0; i < 4; ++i) {\n for (int j = 1; j <= 7; ++j) {\n target[cnt++] = char(7 * i + j);\n }\n target[cnt++] = char(30);\n }\n\n int T;\n scanf(\"%d\", &T);\n while (T--) {\n ss.clear();\n while (!pq.empty()) pq.pop();\n cnt = 0;\n string s(32, ' ');\n for (int i = 0; i < 4; ++i) {\n s[cnt++] = char(i * 7 + 1);\n for (int j = 0; j < 7; ++j) {\n int x;\n scanf(\"%d\", &x);\n if (x % 10 == 1) {\n s[cnt++] = char(30);\n } else {\n s[cnt++] = char((x / 10 - 1) * 7 + x % 10);\n }\n }\n }\n// for (int i = 0; i < 32; ++i) printf(\"%d \", s[i]);printf(\"\\n\");\n\n if (s == target) {\n printf(\"0\\n\");\n continue;\n }\n pq.push(S(s));\n int ans = -1;\n while (!pq.empty()) {\n S u(pq.top());\n pq.pop();\n for (int i = 0; i < 4; ++i) {\n int bp = u.b[i];\n char pre = u.s[bp - 1];\n if (pre == 30 || pre % 7 == 0) {\n continue;\n }\n int np = u.p[pre + 1];\n\n S v(u);\n v.s[bp] = char(pre + 1);\n v.s[np] = char(30);\n if (ss.find(v.s) == ss.end()) {\n ++v.d;\n if (v.s == target) {\n// for (int i = 0; i < 32; ++i) printf(\"%d \", u.s[i]);\n// printf(\"---d=%d\\n\", u.d);\n ans = v.d;\n goto L;\n }\n ss.insert(v.s);\n v.p[pre + 1] = bp;\n v.b[i] = np;\n pq.push(v);\n }\n }\n }\n L:\n printf(\"%d\\n\", ans);\n }\n return 0;\n}", "accuracy": 1, "time_ms": 130, "memory_kb": 11396, "score_of_the_acc": -0.0141, "final_rank": 1 }, { "submission_id": "aoj_1245_1987067", "code_snippet": "#include <vector>\n#include <list>\n#include <map>\n#include <set>\n#include <queue>\n#include <deque>\n#include <stack>\n#include <algorithm>\n#include <functional>\n#include <numeric>\n#include <utility>\n#include <sstream>\n#include <iostream>\n#include <iomanip>\n#include <cstdio>\n#include <cmath>\n#include <cstdlib>\n#include <cctype>\n#include <string>\n#include <cstring>\nusing namespace std;\n#define REP(i,n) for(int i = 0; i < (int)n; i++)\n#define FOR(i,a,b) for(int i = a; i < (int)b; i++)\n#define pb push_back\n#define mp make_pair\ntypedef vector<int> vi;\ntypedef vector<vi> vvi;\ntypedef pair<int, int> pi;\ntypedef long long ll;\ntypedef unsigned long long ull;\nconst int IINF = 1<<28;\nconst ll MOD = 1000000007;\nconst int dx[] = {1, 0, -1, 0}, dy[] = {0, 1, 0, -1};\n\nstruct P {\n\tvvi v;\n\tint cnt;\n\tvector<pi> space;\n};\n\nbool done(vvi a) {\n\tREP(i, 4) {\n\t\tREP(j, 7) {\n\t\t\tif(a[i][j] != (i+1)*10 + j+1)\n\t\t\t\treturn false;\n\t\t}\n\t}\n\treturn true;\n}\n\nint main() {\n\tios_base::sync_with_stdio(0);\n\tcin.tie(0);\n\n\n\tint n; cin >> n;\n\tREP(_, n) {\n\t\tset <vvi> s;\n\t\tqueue<P> q;\n\t\tP p;\n\t\tp.v.resize(4);\n\t\tREP(i, 4) {\n\t\t\tp.v[i].resize(8);\n\t\t\tFOR(j, 1, 8) {\n\t\t\t\tcin >> p.v[i][j];\n\t\t\t\tif(p.v[i][j] % 10 == 1) {\n\t\t\t\t\tp.v[i][j] = 0;\n\t\t\t\t\tp.space.pb(pi(i, j));\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\tp.v[0][0] = 11;\n\t\tp.v[1][0] = 21;\n\t\tp.v[2][0] = 31;\n\t\tp.v[3][0] = 41;\n\t\tp.cnt = 0;\n\t\tq.push(p);\n\t\tbool flg = false;\n\t\twhile(q.size()) {\n\t\t\tp = q.front(); q.pop();\n\t\t\tif(done(p.v)) {\n\t\t\t\tflg = true;\n\t\t\t\tbreak;\n\t\t\t}\n\t\t\tREP(i, 4) {\n\t\t\t\tP p2(p);\n\t\t\t\tint num = p2.v[p.space[i].first][p.space[i].second-1]+1;\n\t\t\t\tif(num % 10 == 8)\n\t\t\t\t\tcontinue;\n\t\t\t\tREP(j, 4) REP(k, 8) {\n\t\t\t\t\tif(p2.v[j][k] == num) {\n\t\t\t\t\t\tp2.v[p2.space[i].first][p2.space[i].second] = num;\n\t\t\t\t\t\tp2.v[j][k] = 0;\n\t\t\t\t\t\tp2.space[i] = pi(j, k);\n\t\t\t\t\t\tif(!s.count(p2.v)) {\n\t\t\t\t\t\t\tp2.cnt++;\n\t\t\t\t\t\t\tq.push(p2);\n\t\t\t\t\t\t\ts.insert(p2.v);\n\t\t\t\t\t\t}\n\t\t\t\t\t\tbreak;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\t/*\n\t\t\t\tREP(j, 4) {\n\t\t\t\t\tREP(k, 8)\n\t\t\t\t\t\tcout << p2.v[j][k] << ' ';\n\t\t\t\t\tcout << endl;\n\t\t\t\t}\n\t\t\t\t*/\n\t\t\t}\n\t\t}\n\t\tcout << (flg ? p.cnt : -1) << endl;\n\t}\n\treturn 0;\n}", "accuracy": 1, "time_ms": 930, "memory_kb": 25000, "score_of_the_acc": -0.5618, "final_rank": 9 }, { "submission_id": "aoj_1245_1907857", "code_snippet": "#include <bits/stdc++.h>\n\n#define _overload(_1,_2,_3,name,...) name\n#define _rep(i,n) _range(i,0,n)\n#define _range(i,a,b) for(int i=int(a);i<int(b);++i)\n#define rep(...) _overload(__VA_ARGS__,_range,_rep,)(__VA_ARGS__)\n\n#define _rrep(i,n) _rrange(i,n,0)\n#define _rrange(i,a,b) for(int i=int(a)-1;i>=int(b);--i)\n#define rrep(...) _overload(__VA_ARGS__,_rrange,_rrep,)(__VA_ARGS__)\n\n#define _all(arg) begin(arg),end(arg)\n#define uniq(arg) sort(_all(arg)),(arg).erase(unique(_all(arg)),end(arg))\n#define getidx(ary,key) lower_bound(_all(ary),key)-begin(ary)\n#define clr(a,b) memset((a),(b),sizeof(a))\n#define bit(n) (1LL<<(n))\n#define popcount(n) (__builtin_popcountll(n))\n\ntemplate<class T>bool chmax(T &a, const T &b) { return (a<b)?(a=b,1):0;}\ntemplate<class T>bool chmin(T &a, const T &b) { return (b<a)?(a=b,1):0;}\n\nusing namespace std;\n\n\nusing state=vector<vector<int>>;\nconst int inf=1<<29;\nconst int n=4,m=8;\n\t\n// inline int hstar(const state &board){\n// \tint ret=0;\n// \trep(i,n)rep(j,m-1) if(10*(i+1)+j+1!=board[i][j]) ret++;\n// \treturn ret;\n// }\n\ninline bool finish(const state &board){\n\trep(i,n)rep(j,m-1) if(10*(i+1)+j+1!=board[i][j]) return false;\n\treturn true;\n}\n\nint bfs(const state &board){\n\tmap<state,int> dist;\n\tusing elem=tuple<int,state>;\n\tpriority_queue<elem,vector<elem>,greater<elem>> q;\n\t\n\tdist[board]=0;\n\tq.push(make_tuple(0,board));\n\t\n\twhile(!q.empty()){\n\t\tint cost; state cboard;\n\t\ttie(cost,cboard)=q.top();q.pop();\n\t\tif(finish(cboard)) return dist[cboard];\n\n\t\trep(i,n)rep(j,m){\n\t\t\tif(cboard[i][j]==0){\n\t\t\t\tstate nboard=cboard;\n\t\t\t\tint left=cboard[i][j-1];\n\t\t\t\trep(a,n)rep(b,m) if(cboard[a][b]==left+1) swap(nboard[a][b],nboard[i][j]);\n\t\t\t\tif(dist.find(nboard)!=end(dist)) continue;\n\t\t\t\tint d=dist[nboard]=dist[cboard]+1;\n\t\t\t\tq.push(make_tuple(d,nboard));\n\t\t\t}\n\t\t}\n\t}\n\treturn -1;\n}\n\nint main(void){\n\tint t;\n\tcin >> t;\n\twhile(t--){\n\t\tstate board(4,vector<int>(8,0));\n\t\trep(i,n)rep(j,1,m) cin >> board[i][j]; \n\t\trep(i,n)rep(j,m) rep(k,4) if(board[i][j]==10*(k+1)+1) swap(board[i][j],board[k][0]);\n\t\tcout << bfs(board) << endl;\n\t}\n\treturn 0;\n}", "accuracy": 1, "time_ms": 1000, "memory_kb": 27672, "score_of_the_acc": -0.6488, "final_rank": 13 }, { "submission_id": "aoj_1245_1881357", "code_snippet": "#include <bits/stdc++.h>\n \nusing namespace std;\n \ntypedef vector<int> Vec;\ntypedef pair<Vec,int> P;\n \nbool check(Vec &v){\n for(int i = 0 ; i < 32 ; i++){\n\tif((i+1) % 8 == 0) continue;\n\tif((i/8+1)*10+i%8+1 != v[i]) return false;\n }\n return true;\n}\n \nint bfs(Vec &start,Vec &space){\n queue<P> Q;\n queue<Vec> S;\n Q.push(P(start,0));\n S.push(space);\n set<Vec> visited;\n visited.insert(start);\n \n while(!Q.empty()){\n\tP p = Q.front(); Q.pop();\n\tVec s = S.front(); S.pop();\n\tif(check(p.first)) return p.second;\n \n\tfor(int i = 0 ; i < 4 ; i++){\n\t if(p.first[s[i]-1] % 10 == 7) continue;\n\t for(int j = 0 ; j < 32 ; j++){\n\t\tif(p.first[s[i]-1]+1 == p.first[j]){\n\t\t swap(p.first[s[i]],p.first[j]);\n\t\t int tmp = s[i]; s[i] = j;\n\t\t if(!visited.count(p.first)){\n\t\t\tvisited.insert(p.first);\n\t\t\tQ.push(P(p.first,p.second+1));\n\t\t\tS.push(s);\n\t\t }\n\t\t s[i] = tmp;\n\t\t swap(p.first[s[i]],p.first[j]);\n\t\t}\n\t }\n\t}\n }\n return -1;\n}\n \nint main(){\n int Tc;\n cin >> Tc;\n while(Tc--){\n\tVec v(32),space;\n\tfor(int i = 0 ; i < 32 ; i++){\n\t if(i % 8 != 0){\n\t\tcin >> v[i];\n\t\tif(v[i]%10 == 1){\n\t\t v[i] = 0;\n\t\t space.push_back(i);\n\t\t}\n\t }else{\n\t\tv[i] = (i/8+1)*10+1;\n\t }\n\t}\n\tcout << bfs(v,space) << endl;\n }\n return 0;\n}", "accuracy": 1, "time_ms": 320, "memory_kb": 16812, "score_of_the_acc": -0.2019, "final_rank": 5 }, { "submission_id": "aoj_1245_1844767", "code_snippet": "#include <bits/stdc++.h>\n#define rep(i,n) for(int i = 0; i < n; i++)\nconst int INF = 100000000;\nconst double EPS = 1e-10;\nconst int MOD = 1000000007;\nusing namespace std;\n\nstruct info{\n\tvector<vector<int> > v;\n\tvector<pair<int,int> > b;\n};\n\ntypedef pair<int,info > P;\ntypedef pair<int,int> PP;\n\nint n;\nvector<vector<int> > v, end;\nmap<vector<vector<int> >, int> m;\nvector<PP> b;\n\nvoid solve(){\n\tv.clear(); end.clear(); m.clear(); b.clear();\n\tv.resize(4);\n\trep(i,4) v[i].resize(8);\n\tend.resize(4);\n\trep(i,4) end[i].resize(8);\n\trep(i,4) rep(j,8){\n\t\tv[i][j] = 0;\n\t\tend[i][j] = 0;\n\t}\n\trep(i,4) rep(j,7){\n\t\tend[i][j] = (i+1)*10+j+1;\n\t}\n\tv[0][0] = 11; v[1][0] = 21; v[2][0] = 31; v[3][0] = 41;\n\trep(i,4){\n\t\trep(j,7){\n\t\t\tcin >> v[i][j+1];\n\t\t\tif(v[i][j+1]%10 == 1){\n\t\t\t\tv[i][j+1] = 0;\n\t\t\t\tb.push_back(PP(i,j+1));\n\t\t\t}\n\t\t}\n\t}\n\tqueue<P> que;\n\tinfo tmp; tmp.v = v; tmp.b = b;\n\tque.push(P(0,tmp));\n\tm[v] = 1;\n\twhile(!que.empty()){\n\t\tP p = que.front();\n\t\tinfo q = p.second;\n\t\tque.pop();\n\t\tif(q.v == end){\n\t\t\tcout << p.first << endl;\n\t\t\treturn;\n\t\t}\n\t\trep(u,q.b.size()){\n\t\t\tint i = q.b[u].first, j = q.b[u].second;\n\t\t\tif(q.v[i][j-1]%10 != 7){\n\t\t\t\trep(k,4){\n\t\t\t\t\tbool br = false;\n\t\t\t\t\trep(l,8){\n\t\t\t\t\t\tif(q.v[k][l] == q.v[i][j-1]+1){\n\t\t\t\t\t\t\tq.v[i][j] = q.v[k][l];\n\t\t\t\t\t\t\tq.v[k][l] = 0;\n\t\t\t\t\t\t\tif(m[q.v]){\n\t\t\t\t\t\t\t\tq.v[k][l] = q.v[i][j];\n\t\t\t\t\t\t\t\tq.v[i][j] = 0;\n\t\t\t\t\t\t\t\tbr = true;\n\t\t\t\t\t\t\t\tbreak;\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\tm[q.v] = 1;\n\t\t\t\t\t\t\tq.b[u] = PP(k,l);\n\t\t\t\t\t\t\tque.push(P(p.first+1,q));\n\t\t\t\t\t\t\tq.b[u] = PP(i,j);\n\t\t\t\t\t\t\tq.v[k][l] = q.v[i][j];\n\t\t\t\t\t\t\tq.v[i][j] = 0;\n\t\t\t\t\t\t\tbr = true;\n\t\t\t\t\t\t\tbreak;\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t\tif(br) break;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n\tcout << \"-1\" << endl;\n}\n\nint main(){\n\tcin >> n;\n\trep(i,n) solve();\n}", "accuracy": 1, "time_ms": 860, "memory_kb": 25864, "score_of_the_acc": -0.5681, "final_rank": 10 }, { "submission_id": "aoj_1245_1810869", "code_snippet": "#include \"bits/stdc++.h\"\n#include<unordered_map>\n#include<unordered_set>\n#pragma warning(disable:4996)\nusing namespace std;\nusing ld = long double;\ntemplate<class T>\nusing Table = vector<vector<T>>;\nconst ld eps=1e-9;\n\n//// < \"D:\\D_Download\\Visual Studio 2015\\Projects\\programing_contest_c++\\Debug\\a.txt\"\nmap<vector<vector<int>>, int>mp;\n\nvector<vector<int>>goal;\n\nint getans(const vector<vector<int>>&field) {\n\tif (field == goal)return 0;\n\tauto it = mp.find(field);\n\tif (it != mp.end())return it->second;\n\telse {\n\t\tint ans = 1e5;\n\t\tvector<vector<int>>nfield(field);\n\t\tfor (int i = 0; i < 4; ++i) {\n\t\t\tfor (int j = 1; j < 8; ++j) {\n\t\t\t\tif (!field[i][j]) {\n\t\t\t\t\tint from = field[i][j - 1];\n\t\t\t\t\tfor (int k = 0; k < 4; ++k) {\n\t\t\t\t\t\tfor (int l = 0; l < 8; ++l) {\n\t\t\t\t\t\t\tif (nfield[k][l] == from + 1) {\n\t\t\t\t\t\t\t\tswap(nfield[i][j], nfield[k][l]);\n\t\t\t\t\t\t\t\tans = min(getans(nfield) + 1,ans);\n\t\t\t\t\t\t\t\tswap(nfield[i][j], nfield[k][l]);\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\treturn mp[field] = ans;\n\t}\n}\nint main() {\n\tint N; cin >> N;\n\twhile (N--) {\n\t\tmp.clear();\n\t\tvector<vector<int>>field(4, vector<int>(8));\n\t\tgoal = field;\n\t\tfor (int i = 0; i < 4; ++i) {\n\t\t\tfor (int j = 0; j < 7; ++j) {\n\t\t\t\tgoal[i][j] = 10 * (i + 1) + j + 1;\n\t\t\t}\n\t\t}\n\t\tfor (int i = 0; i < 4; ++i) {\n\t\t\tfield[i][0] = i * 10 + 11;\n\t\t\tfor (int j = 0; j < 7; ++j) {\n\t\t\t\tint a; cin >> a;\n\t\t\t\tif (a % 10 != 1) {\n\t\t\t\t\tfield[i][j + 1] = a;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\tint ans = getans(field);\n\t\tif (ans > 9999)ans = -1;\n\t\tcout << ans << endl;\n\n\t}\n\t\n\t\n\treturn 0;\n}", "accuracy": 1, "time_ms": 500, "memory_kb": 27660, "score_of_the_acc": -0.5308, "final_rank": 7 }, { "submission_id": "aoj_1245_1703959", "code_snippet": "#include <bits/stdc++.h>\n \n#define _overload(_1,_2,_3,name,...) name\n#define _rep(i,n) _range(i,0,n)\n#define _range(i,a,b) for(int i=(int)(a);i<(int)(b);++i)\n#define rep(...) _overload(__VA_ARGS__,_range,_rep,)(__VA_ARGS__)\n \n#define _rrep(i,n) _rrange(i,n,0)\n#define _rrange(i,a,b) for(int i=(int)(a)-1;i>=(int)(b);--i)\n#define rrep(...) _overload(__VA_ARGS__,_rrange,_rrep,)(__VA_ARGS__)\n \n#define _all(arg) begin(arg),end(arg)\n#define uniq(arg) sort(_all(arg)),(arg).erase(unique(_all(arg)),end(arg))\n#define getidx(ary,key) lower_bound(_all(ary),key)-begin(ary)\n#define clr(a,b) memset((a),(b),sizeof(a))\n#define bit(n) (1LL<<(n))\n \n// #define DEBUG\n \n#ifdef DEBUG\n #define dump(...) fprintf(stderr, __VA_ARGS__)\n#else\n #define dump(...)\n#endif\n \ntemplate<class T>bool chmax(T &a, const T &b) { return (a<b)?(a=b,1):0;}\ntemplate<class T>bool chmin(T &a, const T &b) { return (b<a)?(a=b,1):0;}\n \nusing namespace std;\nusing ll=long long;\nusing vi=vector<int>;\nusing vll=vector<ll>;\n \nconst double EPS = 1e-10;\nconst double PI = acos(-1.0);\nconst ll inf =1LL << 62;\nconst ll mod=1000000007LL;\nconst int dx[4]={1,0,-1,0};\nconst int dy[4]={0,1,0,-1};\n \n \nll extgcd(ll a,ll b,ll& x,ll& y){x=1,y=0;ll g=a;if(b!=0) g=extgcd(b,a%b,y,x),y-=a/b*x;return g;}\nll ADD(const ll &a, const ll &b,const ll &mod) { return (a+b)%mod;}\nll SUB(const ll &a, const ll &b,const ll &mod) { return (a-b+mod)%mod;}\nll MUL(const ll &a, const ll &b,const ll &mod) { return (1LL*a*b)%mod;}\nll DIV(const ll &a, const ll &b,const ll &mod) {ll x,y; extgcd(b,mod,x,y);return MUL(a,(x+mod)%mod,mod);}\n \nrandom_device rd;\nmt19937 mt(rd());\nuniform_int_distribution<int> dice(1,6);\nuniform_real_distribution<double> score(0.0,10.0);\n \nusing vvi = vector<vi>;\n\nint h = 4, w = 8;\n\nbool check(vvi & board){\n rep(y, h){\n rep(x, w - 1){\n if(board[y][x] != 10 * (y + 1) + (x + 1)) return false;\n }\n }\n\n return true;\n}\n\nint main(void){\n int T; cin >> T;\n\n while(T--){\n vvi board(h, vi(w));\n rep(y, h){\n rep(x, 1, w){\n cin >> board[y][x];\n if(board[y][x] % 10 == 1){\n board[y][x] = 0;\n }\n }\n }\n rep(y, h) board[y][0] = 10 * (y + 1) + 1;\n\n queue<pair<int, vvi>> q;\n map<vvi, bool> used;\n q.push(make_pair(0, board));\n\n int res = -1;\n while(q.size()){\n vvi cboard = q.front().second;\n int ccost = q.front().first;\n q.pop();\n\n if(used[cboard]) continue;\n used[cboard] = true;\n\n bool ch = false;\n rep(y, h){\n rep(x, 1, w){\n if(cboard[y][x] != 0 or cboard[y][x - 1] % 10 == 7) continue;\n\n [&]{\n rep(yy, h){\n rep(xx, w){\n if(cboard[yy][xx] == cboard[y][x - 1] + 1){\n swap(cboard[yy][xx], cboard[y][x]);\n if(not used[cboard]){\n q.push(make_pair(ccost + 1, cboard));\n ch = true;\n }\n swap(cboard[yy][xx], cboard[y][x]);\n }\n }\n }\n }();\n }\n }\n\n if(not ch and check(cboard)){\n res = ccost;\n break;\n }\n }\n\n cout << res << endl;\n }\n\n return 0;\n}", "accuracy": 1, "time_ms": 1060, "memory_kb": 27984, "score_of_the_acc": -0.6712, "final_rank": 14 }, { "submission_id": "aoj_1245_1694428", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\n\nstring goal[32]={\"11\",\"12\",\"13\",\"14\",\"15\",\"16\",\"17\",\"\",\"21\",\"22\",\"23\",\"24\",\"25\",\"26\",\"27\",\"\",\"31\",\"32\",\"33\",\"34\",\"35\",\"36\",\"37\",\"\",\"41\",\"42\",\"43\",\"44\",\"45\",\"46\",\"47\",\"\"};\n\nbool check(vector<string> s){\n for(int i=0;i<32;i++)if(s[i]!=goal[i])return false;\n return true;\n}\n\nint main()\n{\n int n;\n\n cin>>n;\n\n for(int k=0;k<n;k++){\n vector<string> f(32);\n for(int i=0;i<4;i++){\n for(int j=0;j<7;j++){\n\tcin>>f[i*8+j+1];\n }\n }\n for(int i=0;i<32;i++){\n if(f[i]==\"11\" || f[i]==\"21\" || f[i]==\"31\" || f[i]==\"41\")f[i]=\"\";\n }\n f[0]=\"11\";\n f[8]=\"21\";\n f[16]=\"31\";\n f[24]=\"41\";\n\n int ans=-1;\n map<vector<string>,int> m;\n queue<vector<string> > q;\n m[f]=0;\n q.push(f);\n while(!q.empty()){\n vector<string> u=q.front();\n q.pop();\n if(check(u)){\n\tans=m[u];\n\tbreak;\n }\n for(int i=0;i<32;i++){\n\tif(u[i]==\"\"){\n\t vector<string> t=u;\n\t if(t[i-1]==\"\" || t[i-1][1]=='7')continue;\n\t string s=t[i-1];\n\t s[1]++;\n\t for(int j=0;j<32;j++){\n\t if(t[j]==s){\n\t t[j]=\"\";\n\t break;\n\t }\n\t }\n\t t[i]=s;\n\t if(m.find(t)==m.end()){\n\t m[t]=m[u]+1;\n\t q.push(t);\n\t }\n\t}\n }\n }\n cout<<ans<<endl;\n }\n}", "accuracy": 1, "time_ms": 3680, "memory_kb": 35776, "score_of_the_acc": -1.4935, "final_rank": 20 }, { "submission_id": "aoj_1245_1674300", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\ntypedef vector<vector<int> > Mat;\n#define getMat(r, c) Mat(r, vector<int>(c));\n\nconst int INF = 1<<28;\nconst int R = 4;\nconst int C = 8;\n\nMat G = getMat(R, C);\nmap<Mat, int> mem;\n\nvoid show() {\n for (int i = 0; i < R; ++i) {\n for (int j = 0; j < C; ++j) {\n if (j) cout << \" \";\n if (G[i][j] == 0) cout << \"00\";\n else cout << G[i][j];\n }\n cout << endl;\n }\n}\n\nint diff() {\n int res = 0;\n for (int i = 0; i < R; ++i) {\n for (int j = 0; j < C-1; ++j) {\n if (G[i][j] != (i+1) * 10 + (j+1)) {\n ++res;\n }\n }\n }\n return res;\n}\n\nint rec() {\n if (mem.count(G)) return mem[G];\n if (diff() == 0) {\n return mem[G] = 0;\n }\n //show(); cout << endl;\n int res = INF;\n for (int i = 0; i < R; ++i) {\n for (int j = 0; j < C; ++j) {\n if (G[i][j] != 0) continue;\n if (G[i][j-1] == 0) continue;\n if (G[i][j-1] % 10 == 7) continue;\n int target = G[i][j-1] + 1;\n try {\n for (int i2 = 0; i2 < R; ++i2) {\n for (int j2 = 0; j2 < C; ++j2) {\n if (G[i2][j2] == target) {\n swap(G[i][j], G[i2][j2]);\n res = min(res, rec() + 1);\n swap(G[i][j], G[i2][j2]);\n throw 0;\n }\n }\n }\n } catch(...) {}\n }\n }\n return mem[G] = res;\n}\n\nint main() {\n int Tc;\n cin >> Tc;\n while (Tc--) {\n for (int i = 0; i < R; ++i) {\n G[i][0] = (i+1)*10 + 1;\n for (int j = 1; j < C; ++j) {\n cin >> G[i][j];\n if (G[i][j] % 10 == 1) G[i][j] = 0;\n }\n }\n mem.clear();\n int res = rec();\n if (res == INF) res = -1;\n cout << res << endl;\n }\n return 0;\n}", "accuracy": 1, "time_ms": 2050, "memory_kb": 25824, "score_of_the_acc": -0.8471, "final_rank": 15 }, { "submission_id": "aoj_1245_1659705", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n#define FOR(i,k,n) for(int i = (k); i < (n); i++)\n#define REP(i,n) FOR(i,0,n)\n#define ALL(a) a.begin(), a.end()\n#define MS(m,v) memset(m,v,sizeof(m))\n#define D10 fixed<<setprecision(10)\ntypedef vector<int> vi;\ntypedef vector<string> vs;\ntypedef pair<int, int> pii;\ntypedef long long ll;\ntypedef long double ld;\nconst int MOD = 1000000007;\nconst int INF = MOD + 1;\nconst ld EPS = 1e-10;\ntemplate<class T> T &chmin(T &a, const T &b) { return a = min(a, b); }\ntemplate<class T> T &chmax(T &a, const T &b) { return a = max(a, b); }\n\n/*--------------------template--------------------*/\n\nint main()\n{\n\tint n;\n\tcin >> n;\n\twhile (n--)\n\t{\n\t\tvector<vi> v(4, vi(8));\n\t\tauto goal = v;\n\t\tREP(i, 4)REP(j, 7) goal[i][j] = (i+1) * 10 + j + 1;\n\t\tREP(i, 4)FOR(j, 1, 8)\n\t\t{\n\t\t\tcin >> v[i][j];\n\t\t\tif (v[i][j] % 10 == 1)\n\t\t\t{\n\t\t\t\tv[v[i][j] / 10 - 1][0] = v[i][j];\n\t\t\t\tv[i][j] = 0;\n\t\t\t}\n\t\t}\n\t\tqueue<pair<vector<vi>, int>> que;\n\t\tset<vector<vi>> st;\n\t\tque.push(make_pair(v, 0));\n\t\tbool f = true;\n\t\twhile (que.size())\n\t\t{\n\t\t\tauto tmp = que.front().first;\n\t\t\tint cnt = que.front().second;\n\t\t\tque.pop();\n\t\t\tif (tmp == goal)\n\t\t\t{\n\t\t\t\tf = false;\n\t\t\t\tcout << cnt << endl;\n\t\t\t\tbreak;\n\t\t\t}\n\t\t\tREP(i, 4)FOR(j, 1, 8)\n\t\t\t{\n\t\t\t\tif (tmp[i][j] == 0 && tmp[i][j - 1] != 0 && tmp[i][j - 1] % 10 != 7)\n\t\t\t\t{\n\t\t\t\t\tauto nx = tmp;\n\t\t\t\t\tint t = tmp[i][j - 1] + 1;\n\t\t\t\t\tREP(ii, 4)REP(jj, 8)\n\t\t\t\t\t{\n\t\t\t\t\t\tif (tmp[ii][jj] == t)\n\t\t\t\t\t\t{\n\t\t\t\t\t\t\tnx[ii][jj] = 0;\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t\tnx[i][j] = t;\n\t\t\t\t\tif (st.count(nx)) continue;\n\t\t\t\t\telse\n\t\t\t\t\t{\n\t\t\t\t\t\tst.insert(nx);\n\t\t\t\t\t\tque.push(make_pair(nx, cnt + 1));\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\tif (f) puts(\"-1\");\n\t}\n\treturn 0;\n}", "accuracy": 1, "time_ms": 660, "memory_kb": 26364, "score_of_the_acc": -0.5343, "final_rank": 8 }, { "submission_id": "aoj_1245_1598322", "code_snippet": "#include <cstdio>\n#include <cstring>\n#include <map>\n#include <vector>\n#include <queue>\n#include <set>\n#include <algorithm>\nusing namespace std;\ntypedef vector<int> vec;\ntypedef vector<vec > mal;\nint ttt;\nmal g(4,vector<int>(8));\nmap<mal,int> dist;\nset<mal> checked;\n\nbool check(mal c){\n\tfor(int i=0;i<4;i++){\n\t\tfor(int j=0;j<7;j++){\n\t\t\tif(c[i][j]!=(i+1)*10+j+1)return false;\n\t\t}\n\t}\n\treturn true;\n}\n\nint bfs(mal s){\n\tchecked.clear();\n\tdist.clear();\n\tqueue<mal > que;\n\tque.push(s);\n\tchecked.insert(s);\n\twhile(que.size()){\n\t\tmal q=que.front();\n\t\tque.pop();\n\t\tif(check(q))return dist[q];\n\t\tfor(int i=0;i<4;i++){\n\t\t\tfor(int j=1;j<8;j++){\n\t\t\t\tif(q[i][j]!=0)continue;\n\t\t\t\tfor(int k=0;k<4;k++){\n\t\t\t\t\tfor(int l=1;l<8;l++){\n\t\t\t\t\t\tif(q[k][l]==0)continue;\n\t\t\t\t\t\tif(q[i][j-1]==q[k][l]-1){\n\t\t\t\t\t\t\tmal nq=q;\n\t\t\t\t\t\t\tnq[i][j]=q[k][l];\n\t\t\t\t\t\t\tnq[k][l]=0;\n\t\t\t\t\t\t\tif(checked.find(nq)==checked.end()){\n\t\t\t\t\t\t\t\tchecked.insert(nq);\n\t\t\t\t\t\t\t\tdist[nq]=dist[q]+1;\n\t\t\t\t\t\t\t\tque.push(nq);\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t}\t\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n\treturn -1;\n}\n\nint main(void){\n\tscanf(\"%d\",&ttt);\n\tfor(int test=0;test<ttt;test++){\n\t\tmal fie(4,vec(8));\n\t\tfor(int i=0;i<4;i++){\n\t\t\tfor(int j=1;j<8;j++){\n\t\t\t\tscanf(\"%d\",&fie[i][j]);\n\t\t\t\tif(fie[i][j]%10==1){\n\t\t\t\t\tfie[fie[i][j]/10-1][0]=fie[i][j];\n\t\t\t\t\tfie[i][j]=0;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\tprintf(\"%d\\n\",bfs(fie));\n\t}\n\treturn 0;\n}", "accuracy": 1, "time_ms": 1430, "memory_kb": 49248, "score_of_the_acc": -1.32, "final_rank": 19 }, { "submission_id": "aoj_1245_1484164", "code_snippet": "//include\n//------------------------------------------\n#include <vector>\n#include <list>\n#include <map>\n#include <set>\n#include <deque>\n#include <stack>\n#include <bitset>\n#include <algorithm>\n#include <functional>\n#include <numeric>\n#include <utility>\n#include <sstream>\n#include <iostream>\n#include <iomanip>\n#include <cstdio>\n#include <cmath>\n#include <cstdlib>\n#include <cctype>\n#include <string>\n#include <cstring>\n#include <ctime>\n#include <climits>\n#include <queue>\n\nusing namespace std;\n\n//typedef\n//------------------------------------------\ntypedef vector<int> VI;\ntypedef vector<VI> VVI;\ntypedef vector<string> VS;\ntypedef pair<int, int> PII;\ntypedef long long LL;\n\n//container util\n//------------------------------------------\n#define ALL(a) (a).begin(),(a).end()\n#define RALL(a) (a).rbegin(), (a).rend()\n#define PB push_back\n#define MP make_pair\n#define SZ(a) int((a).size())\n#define EACH(i,c) for(typeof((c).begin()) i=(c).begin(); i!=(c).end(); ++i)\n#define EXIST(s,e) ((s).find(e)!=(s).end())\n#define SORT(c) sort((c).begin(),(c).end())\n\n//repetition\n//------------------------------------------\n#define FOR(i,a,b) for(int i=(a);i<(b);++i)\n#define REP(i,n) FOR(i,0,n)\n\n//constant\n//--------------------------------------------\nconst double EPS = 1e-10;\nconst double PI = acos(-1.0);\n\nint idx(PII p){\n return p.first*10+p.second+11;\n}\n\nint main(){\n cin.tie(0);\n ios_base::sync_with_stdio(false);\n\n int N; cin >> N;\n REP(n,N){\n\tVI xs(32);\n\tREP(i,4){\n\t REP(j,7) cin >> xs[i*8+j+1];\n\t xs[i*8] = 0;\n\t}\n\n\tREP(i,32)\n\t if(xs[i]%10 == 1){\n\t\tswap(xs[i], xs[(xs[i]/10-1)*8]);\n\t }\n\n\tmap<VI,int> memo;\n\tqueue<VI> q;\n\tq.push(xs);\n\tmemo[xs] = 0;\n\n\tint ans = -1;\n\twhile(!q.empty()){\n\t xs = q.front(); q.pop();\n\t bool ok = true;\n\t REP(y,4) REP(x,7)\n\t\tif(xs[y*8+x] != y*10+x+11){\n\t\t ok = false;\n\t\t x = y = 100;\n\t\t}\n\t if(ok){\n\t\tans = memo[xs];\n\t\tbreak;\n\t }\n\n\t int d = memo[xs];\n\t REP(y,4) FOR(x,1,8){\n\t\tint idx = y*8+x;\n\t\tif(xs[idx] == 0 && xs[idx-1]%10 < 7){\n\t\t REP(ty,4) FOR(tx,1,8){\n\t\t\tint tidx = ty*8+tx;\n\t\t\tif(xs[idx-1]+1 == xs[tidx]){\n\t\t\t VI tmp = xs;\n\t\t\t swap(tmp[idx], tmp[tidx]);\n\n\t\t\t if(!memo.count(tmp)){\n\t\t\t\tq.emplace(tmp);\n\t\t\t\tmemo[tmp] = d + 1;\n\t\t\t }\n\t\t\t ty = tx = 100;\n\t\t\t}\n\t\t }\n\t\t}\n\t }\n\t}\n\tcout << ans << endl;\n }\n\n return 0;\n}", "accuracy": 1, "time_ms": 500, "memory_kb": 15984, "score_of_the_acc": -0.2224, "final_rank": 6 }, { "submission_id": "aoj_1245_1375756", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\ntypedef vector< int > vec;\ntypedef vector< vec > mat;\nmap < mat , int > d;\n\nint solve(mat A){\n queue< mat > Q;\n Q.push(A);\n \n\n \n d[A]=1;\n while(!Q.empty()){\n mat a=Q.front();Q.pop();\n \n bool f=true;\n \n for(int i=0;i<4;i++){\n for(int j=0;j<8;j++){\n if(a[i][j]!=0&&i*10+10+j+1!=a[i][j]){\n f=false;\n }\n }\n }\n if(f)return d[a]-1;\n\n for(int i=0;i<4;i++){\n for(int j=1;j<8;j++){\n if(a[i][j]!=0)continue;\n \n mat b=a;\n int c=a[i][j-1]+1;\n\n int h=-1,w;\n for(int k=0;k<4;k++){\n for(int l=0;l<8;l++){\n if(b[k][l]==c){\n h=k,w=l;\n }\n }\n }\n\n if(h==-1)continue;\n\n swap(b[h][w],b[i][j]);\n\n if(d[b]==0){\n d[b]=d[a]+1;\n Q.push(b);\n }\n }\n }\n\n }\n return -1;\n}\n\nint main(){\n int Tc;\n cin>>Tc;\n while(Tc--){\n d.clear();\n mat A(4, vec(8) );\n for(int i=0;i<4;i++){\n A[i][0]=i*10+10+1;\n for(int j=0;j<7;j++){\n cin>>A[i][j+1];\n if(A[i][j+1]%10==1)A[i][j+1]=0;\n }\n }\n cout<<solve(A)<<endl;\n }\n return 0;\n}", "accuracy": 1, "time_ms": 1070, "memory_kb": 25832, "score_of_the_acc": -0.6167, "final_rank": 11 } ]
aoj_1248_cpp
Problem A: The Balance Ms. Iyo Kiffa-Australis has a balance and only two kinds of weights to measure a dose of medicine. For example, to measure 200mg of aspirin using 300mg weights and 700mg weights, she can put one 700mg weight on the side of the medicine and three 300mg weights on the opposite side (Figure 1). Although she could put four 300mg weights on the medicine side and two 700mg weights on the other (Figure 2), she would not choose this solution because it is less convenient to use more weights. You are asked to help her by calculating how many weights are required. Figure 1: To measure 200mg of aspirin using three 300mg weights and one 700mg weight Figure 2: To measure 200mg of aspirin using four 300mg weights and two 700mg weights Input The input is a sequence of datasets. A dataset is a line containing three positive integers a , b , and d separated by a space. The following relations hold: a ≠ b , a ≤ 10000, b ≤ 10000, and d ≤ 50000. You may assume that it is possible to measure d mg using a combination of a mg and b mg weights. In other words, you need not consider “no solution” cases. The end of the input is indicated by a line containing three zeros separated by a space. It is not a dataset. Output The output should be composed of lines, each corresponding to an input dataset ( a , b , d ). An output line should contain two nonnegative integers x and y separated by a space. They should satisfy the following three conditions. You can measure d mg using x many a mg weights and y many b mg weights. The total number of weights ( x + y ) is the smallest among those pairs of nonnegative integers satisfying the previous condition. The total mass of weights ( ax + by ) is the smallest among those pairs of nonnegative integers satisfying the previous two conditions. No extra characters (e.g. extra spaces) should appear in the output. Sample Input 700 300 200 500 200 300 500 200 500 275 110 330 275 110 385 648 375 4002 3 1 10000 0 0 0 Output for the Sample Input 1 3 1 1 1 0 0 3 1 1 49 74 3333 1
[ { "submission_id": "aoj_1248_10852694", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\n\nll EX_GCD(ll a, ll b, ll& x, ll& y) {\n\tll d = a;\n\tif (b != 0) {\n\t\td = EX_GCD(b, a % b, y, x);\n\t\ty -= (a / b) * x;\n\t}\n\telse {\n\t\tx = 1;\n\t\ty = 0;\n\t}\n\treturn d;\n}\n\nint main()\n{\n\tll a, b, d;\n\twhile (cin >> a >> b >> d, a | b | d) {\n\t\tll cx, cy;\n\t\tll gcd = EX_GCD(a, b, cx, cy);\n\t\ta /= gcd; b /= gcd; d /= gcd;\n\t\tcx *= d, cy *= d;\n\t\tll x = cx, y = cy;\n\t\tfor (ll i = -100000; i <= 100000; i++) {\n\t\t\tif (abs(cx + b * i) + abs(cy - a * i) < abs(x) + abs(y) || (abs(cx + b * i) + abs(cy - a * i) == abs(x) + abs(y) && abs(cx + b * i) * a + abs(cy - a * i) * b < abs(x) * a + abs(y) * b)) {\n\t\t\t\tx = cx + b * i;\n\t\t\t\ty = cy - a * i;\n\t\t\t}\n\t\t}\n\t\tcout << abs(x) << ' ' << abs(y) << endl;\n\t}\n\treturn 0;\n}", "accuracy": 1, "time_ms": 30, "memory_kb": 3288, "score_of_the_acc": -0.0536, "final_rank": 5 }, { "submission_id": "aoj_1248_10189449", "code_snippet": "#include <cmath>\n#include <iostream>\n#include <algorithm>\nusing namespace std;\n\nconst int INF = 1012345678;\n\nint main() {\n\twhile (true) {\n\t\tint A, B, D;\n\t\tcin >> A >> B >> D;\n\t\tif (A == 0 && B == 0 && D == 0) {\n\t\t\tbreak;\n\t\t}\n\t\tint optx = INF, opty = INF;\n\t\tfor (int a = 0; a <= B + D; a++) {\n\t\t\tfor (int p = -D; p <= +D; p += 2 * D) {\n\t\t\t\tint z = a * A + p;\n\t\t\t\tif (abs(z) % B == 0) {\n\t\t\t\t\tint b = abs(z) / B;\n\t\t\t\t\tif (optx + opty > a + b || (optx + opty == a + b && A * optx + B * opty > A * a + B * b)) {\n\t\t\t\t\t\toptx = a;\n\t\t\t\t\t\topty = b;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\tcout << optx << ' ' << opty << endl;\n\t}\n\treturn 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3320, "score_of_the_acc": -0.0526, "final_rank": 4 }, { "submission_id": "aoj_1248_10022114", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\n#define all(a) a.begin(),a.end()\n#define reps(i, a, n) for (int i = (a); i < (int)(n); i++)\n#define rep(i, n) reps(i, 0, n)\n#define rreps(i, a, n) for (int i = (a); i > (int)(n); i--)\nconst long long mod = 1000000007;\nconst long long INF = 1e18;\nll myceil(ll a, ll b) {return (a+b-1)/b;}\n\nint a,b,d;\n\nvoid solve() {\n pair<int,int> ans = {1e9,1e9};\n ll weight = 0;\n for (int i = 0; i <= 1e5; i++) {\n if (abs(i*a-d)%b == 0) {\n if (ans.first + ans.second > i+abs(i*a-d)/b) {\n ans.first = i;\n ans.second = abs(i*a-d)/b;\n weight = d;\n }\n else if (ans.first + ans.second == i+abs(i*a-d)/b){\n if (weight > d) {\n ans.first = i;\n ans.second = abs(i*a-d)/b;\n weight = d;\n }\n }\n }\n\n if ((i*a+d)%b == 0) {\n if (ans.first + ans.second > i+(i*a+d)/b) {\n ans.first = i;\n ans.second = (i*a+d)/b;\n weight = i*a+d+i*a;\n }\n else if (ans.first + ans.second == i+(i*a+d)/b){\n if (weight > d) {\n ans.first = i;\n ans.second = (i*a+d)/b;\n weight = i*a+d+i*a;\n }\n }\n }\n }\n\n cout << ans.first << ' ' << ans.second << endl;\n return;\n\n}\n\n\nint main() {\n cin.tie(nullptr);\n ios_base::sync_with_stdio(false);\n\n cin >> a >> b >> d;\n while (a != 0) {\n solve();\n cin >> a >> b >> d;\n }\n}", "accuracy": 1, "time_ms": 30, "memory_kb": 3368, "score_of_the_acc": -0.0663, "final_rank": 6 }, { "submission_id": "aoj_1248_9590743", "code_snippet": "#include <bits/stdc++.h>\n\nusing namespace std;\n\n#define rep(i, a, b) for (int i = (int)(a); i < (int)(b); i++)\n#define rrep(i, a, b) for (int i = (int)(b)-1; i >= (int)(a); i--)\n#define ALL(v) (v).begin(), (v).end()\n#define UNIQUE(v) sort(ALL(v)), (v).erase(unique(ALL(v)), (v).end())\n#define SZ(v) (int)v.size()\n#define MIN(v) *min_element(ALL(v))\n#define MAX(v) *max_element(ALL(v))\n#define LB(v, x) int(lower_bound(ALL(v), (x)) - (v).begin())\n#define UB(v, x) int(upper_bound(ALL(v), (x)) - (v).begin())\n\nusing uint = unsigned int;\nusing ll = long long int;\nusing ull = unsigned long long;\nusing i128 = __int128_t;\nusing u128 = __uint128_t;\nconst int inf = 0x3fffffff;\nconst ll INF = 0x1fffffffffffffff;\n\ntemplate <typename T> inline bool chmax(T &a, T b) {\n if (a < b) {\n a = b;\n return 1;\n }\n return 0;\n}\ntemplate <typename T> inline bool chmin(T &a, T b) {\n if (a > b) {\n a = b;\n return 1;\n }\n return 0;\n}\ntemplate <typename T, typename U> T ceil(T x, U y) {\n assert(y != 0);\n if (y < 0)\n x = -x, y = -y;\n return (x > 0 ? (x + y - 1) / y : x / y);\n}\ntemplate <typename T, typename U> T floor(T x, U y) {\n assert(y != 0);\n if (y < 0)\n x = -x, y = -y;\n return (x > 0 ? x / y : (x - y + 1) / y);\n}\ntemplate <typename T> int popcnt(T x) {\n return __builtin_popcountll(x);\n}\ntemplate <typename T> int topbit(T x) {\n return (x == 0 ? -1 : 63 - __builtin_clzll(x));\n}\ntemplate <typename T> int lowbit(T x) {\n return (x == 0 ? -1 : __builtin_ctzll(x));\n}\n\ntemplate <class T, class U>\nostream &operator<<(ostream &os, const pair<T, U> &p) {\n os << \"P(\" << p.first << \", \" << p.second << \")\";\n return os;\n}\ntemplate <typename T> ostream &operator<<(ostream &os, const vector<T> &vec) {\n os << \"{\";\n for (int i = 0; i < vec.size(); i++) {\n os << vec[i] << (i + 1 == vec.size() ? \"\" : \", \");\n }\n os << \"}\";\n return os;\n}\ntemplate <typename T, typename U>\nostream &operator<<(ostream &os, const map<T, U> &map_var) {\n os << \"{\";\n for (auto itr = map_var.begin(); itr != map_var.end(); itr++) {\n os << \"(\" << itr->first << \", \" << itr->second << \")\";\n itr++;\n if (itr != map_var.end())\n os << \", \";\n itr--;\n }\n os << \"}\";\n return os;\n}\ntemplate <typename T> ostream &operator<<(ostream &os, const set<T> &set_var) {\n os << \"{\";\n for (auto itr = set_var.begin(); itr != set_var.end(); itr++) {\n os << *itr;\n ++itr;\n if (itr != set_var.end())\n os << \", \";\n itr--;\n }\n os << \"}\";\n return os;\n}\n#ifdef LOCAL\n#define show(...) _show(0, #__VA_ARGS__, __VA_ARGS__)\n#else\n#define show(...) true\n#endif\ntemplate <typename T> void _show(int i, T name) {\n cerr << '\\n';\n}\ntemplate <typename T1, typename T2, typename... T3>\nvoid _show(int i, const T1 &a, const T2 &b, const T3 &...c) {\n for (; a[i] != ',' && a[i] != '\\0'; i++)\n cerr << a[i];\n cerr << \":\" << b << \" \";\n _show(i + 1, a, c...);\n}\n\n#include <unistd.h>\n\nnamespace fastio {\nstatic constexpr uint32_t SZ = 1 << 17;\nchar ibuf[SZ];\nchar obuf[SZ];\nchar out[100];\n// pointer of ibuf, obuf\n\n\nuint32_t pil = 0, pir = 0, por = 0;\n\nstruct Pre {\n char num[10000][4];\n constexpr Pre() : num() {\n for (int i = 0; i < 10000; i++) {\n int n = i;\n for (int j = 3; j >= 0; j--) {\n num[i][j] = n % 10 | '0';\n n /= 10;\n }\n }\n }\n} constexpr pre;\n\ninline void load() {\n memmove(ibuf, ibuf + pil, pir - pil);\n pir = pir - pil + fread(ibuf + pir - pil, 1, SZ - pir + pil, stdin);\n pil = 0;\n if (pir < SZ)\n ibuf[pir++] = '\\n';\n}\n\ninline void flush() {\n fwrite(obuf, 1, por, stdout);\n por = 0;\n}\n\nvoid rd(char &c) {\n do {\n if (pil + 1 > pir)\n load();\n c = ibuf[pil++];\n } while (isspace(c));\n}\n\nvoid rd(string &x) {\n x.clear();\n char c;\n do {\n if (pil + 1 > pir)\n load();\n c = ibuf[pil++];\n } while (isspace(c));\n do {\n x += c;\n if (pil == pir)\n load();\n c = ibuf[pil++];\n } while (!isspace(c));\n}\n\ntemplate <typename T> void rd_real(T &x) {\n string s;\n rd(s);\n x = stod(s);\n}\n\ntemplate <typename T> void rd_integer(T &x) {\n if (pil + 100 > pir)\n load();\n char c;\n do\n c = ibuf[pil++];\n while (c < '-');\n bool minus = 0;\n if constexpr (is_signed<T>::value || is_same_v<T, i128>) {\n if (c == '-') {\n minus = 1, c = ibuf[pil++];\n }\n }\n x = 0;\n while ('0' <= c) {\n x = x * 10 + (c & 15), c = ibuf[pil++];\n }\n if constexpr (is_signed<T>::value || is_same_v<T, i128>) {\n if (minus)\n x = -x;\n }\n}\n\nvoid rd(int &x) {\n rd_integer(x);\n}\nvoid rd(ll &x) {\n rd_integer(x);\n}\nvoid rd(i128 &x) {\n rd_integer(x);\n}\nvoid rd(uint &x) {\n rd_integer(x);\n}\nvoid rd(ull &x) {\n rd_integer(x);\n}\nvoid rd(u128 &x) {\n rd_integer(x);\n}\nvoid rd(double &x) {\n rd_real(x);\n}\nvoid rd(long double &x) {\n rd_real(x);\n}\n\ntemplate <class T, class U> void rd(pair<T, U> &p) {\n return rd(p.first), rd(p.second);\n}\ntemplate <size_t N = 0, typename T> void rd_tuple(T &t) {\n if constexpr (N < std::tuple_size<T>::value) {\n auto &x = std::get<N>(t);\n rd(x);\n rd_tuple<N + 1>(t);\n }\n}\ntemplate <class... T> void rd(tuple<T...> &tpl) {\n rd_tuple(tpl);\n}\n\ntemplate <size_t N = 0, typename T> void rd(array<T, N> &x) {\n for (auto &d : x)\n rd(d);\n}\ntemplate <class T> void rd(vector<T> &x) {\n for (auto &d : x)\n rd(d);\n}\n\nvoid read() {}\ntemplate <class H, class... T> void read(H &h, T &...t) {\n rd(h), read(t...);\n}\n\nvoid wt(const char c) {\n if (por == SZ)\n flush();\n obuf[por++] = c;\n}\nvoid wt(const string s) {\n for (char c : s)\n wt(c);\n}\nvoid wt(const char *s) {\n size_t len = strlen(s);\n for (size_t i = 0; i < len; i++)\n wt(s[i]);\n}\n\ntemplate <typename T> void wt_integer(T x) {\n if (por > SZ - 100)\n flush();\n if (x < 0) {\n obuf[por++] = '-', x = -x;\n }\n int outi;\n for (outi = 96; x >= 10000; outi -= 4) {\n memcpy(out + outi, pre.num[x % 10000], 4);\n x /= 10000;\n }\n if (x >= 1000) {\n memcpy(obuf + por, pre.num[x], 4);\n por += 4;\n } else if (x >= 100) {\n memcpy(obuf + por, pre.num[x] + 1, 3);\n por += 3;\n } else if (x >= 10) {\n int q = (x * 103) >> 10;\n obuf[por] = q | '0';\n obuf[por + 1] = (x - q * 10) | '0';\n por += 2;\n } else\n obuf[por++] = x | '0';\n memcpy(obuf + por, out + outi + 4, 96 - outi);\n por += 96 - outi;\n}\n\ntemplate <typename T> void wt_real(T x) {\n ostringstream oss;\n oss << fixed << setprecision(15) << double(x);\n string s = oss.str();\n wt(s);\n}\n\nvoid wt(int x) {\n wt_integer(x);\n}\nvoid wt(ll x) {\n wt_integer(x);\n}\nvoid wt(i128 x) {\n wt_integer(x);\n}\nvoid wt(uint x) {\n wt_integer(x);\n}\nvoid wt(ull x) {\n wt_integer(x);\n}\nvoid wt(u128 x) {\n wt_integer(x);\n}\nvoid wt(double x) {\n wt_real(x);\n}\nvoid wt(long double x) {\n wt_real(x);\n}\n\ntemplate <class T, class U> void wt(const pair<T, U> val) {\n wt(val.first);\n wt(' ');\n wt(val.second);\n}\ntemplate <size_t N = 0, typename T> void wt_tuple(const T t) {\n if constexpr (N < std::tuple_size<T>::value) {\n if constexpr (N > 0) {\n wt(' ');\n }\n const auto x = std::get<N>(t);\n wt(x);\n wt_tuple<N + 1>(t);\n }\n}\ntemplate <class... T> void wt(tuple<T...> tpl) {\n wt_tuple(tpl);\n}\ntemplate <class T, size_t S> void wt(const array<T, S> val) {\n auto n = val.size();\n for (size_t i = 0; i < n; i++) {\n if (i)\n wt(' ');\n wt(val[i]);\n }\n}\ntemplate <class T> void wt(const vector<T> val) {\n auto n = val.size();\n for (size_t i = 0; i < n; i++) {\n if (i)\n wt(' ');\n wt(val[i]);\n }\n}\n\nvoid print() {\n wt('\\n');\n}\ntemplate <class Head, class... Tail> void print(Head &&head, Tail &&...tail) {\n wt(head);\n if (sizeof...(Tail))\n wt(' ');\n print(forward<Tail>(tail)...);\n}\nvoid __attribute__((destructor)) _d() {\n flush();\n}\n} // namespace fastio\n\n\nusing fastio::flush;\nusing fastio::print;\nusing fastio::read;\n\ninline void first(bool i = true) {\n print(i ? \"first\" : \"second\");\n}\ninline void Alice(bool i = true) {\n print(i ? \"Alice\" : \"Bob\");\n}\ninline void Takahashi(bool i = true) {\n print(i ? \"Takahashi\" : \"Aoki\");\n}\ninline void yes(bool i = true) {\n print(i ? \"yes\" : \"no\");\n}\ninline void Yes(bool i = true) {\n print(i ? \"Yes\" : \"No\");\n}\ninline void No() {\n print(\"No\");\n}\ninline void YES(bool i = true) {\n print(i ? \"YES\" : \"NO\");\n}\ninline void NO() {\n print(\"NO\");\n}\ninline void Yay(bool i = true) {\n print(i ? \"Yay!\" : \":(\");\n}\ninline void Possible(bool i = true) {\n print(i ? \"Possible\" : \"Impossible\");\n}\ninline void POSSIBLE(bool i = true) {\n print(i ? \"POSSIBLE\" : \"IMPOSSIBLE\");\n}\n\n/**\n * @brief Fast IO\n */\n\nint main() {\nwhile(1) {\n ll A, B, C;\n cin >> A >> B >> C;\n if (A == 0 && B == 0 && C == 0) return 0;\n ll X = inf, Y = inf;\n rep(i,0,1000000) {\n ll D = A*(ll)i;\n if ((D+C)%B==0) {\n ll NA = i, NB = (D+C)/B;\n if (NA+NB<X+Y) {\n X=NA,Y=NB;\n }\n else if (NA+NB==X+Y && NA*A+NB*B<X*A+Y*B) {\n X=NA,Y=NB;\n }\n }\n if (abs(D-C)%B==0) {\n ll NA = i, NB = abs(D-C)/B;\n if (NA+NB<X+Y) {\n X=NA,Y=NB;\n }\n else if (NA+NB==X+Y && NA*A+NB*B<X*A+Y*B) {\n X=NA,Y=NB;\n }\n }\n }\n cout << X << ' ' << Y << endl;\n}\n}", "accuracy": 1, "time_ms": 1160, "memory_kb": 3108, "score_of_the_acc": -0.3675, "final_rank": 14 }, { "submission_id": "aoj_1248_9280873", "code_snippet": "#include <algorithm>\n#include <iostream>\n#include <string>\n#include <vector>\n\n#define ll long long\n\nusing namespace std;\nint main() {\n while (1) {\n ll a, b, d, x, x1,x2,y, y1, y2, mink = 1 << 30, ming = 1 << 30;\n cin >> a >> b >> d;\n if (a == 0 && b == 0 && d == 0) {\n break;\n }\n\n for (ll i = 0; i < 25001; i++) {\n if (abs(d - a * i) % b == 0) {\n y1 = abs(d - a * i) / b;\n if (i + y1 <= mink && (a * i + b * y1) <= ming) {\n x = i;\n y = y1;\n mink = i + y1;\n ming = a * i + b * y1;\n }\n }\n if ((d + a * i) % b == 0) {\n y2 = (d + a * i) / b;\n if (i + y2 <= mink && (a * i + b * y2) <= ming) {\n x = i;\n y = y2;\n mink = i + y2;\n ming = a * i + b * y2;\n }\n }\n }\n\n for (ll i = 0; i < 25001; i++) {\n if (abs(d - b * i) % a == 0) {\n x1 = abs(d - b* i) / a;\n if (i + x1 <= mink && (a * x1 + b * i) <= ming) {\n x = x1;\n y = i;\n mink = i + x1;\n ming = a * x1 + b * i;\n }\n }\n if ((d + b * i) % a == 0) {\n x2 = (d + b * i) / a;\n if (i + x2 <= mink && (a * x2 + b * i) <= ming) {\n x = x2;\n y = i;\n mink = i + x2;\n ming = a * x2 + b * i;\n }\n }\n }\n cout << x << \" \" << y << endl;\n }\n}", "accuracy": 1, "time_ms": 70, "memory_kb": 3104, "score_of_the_acc": -0.0366, "final_rank": 3 }, { "submission_id": "aoj_1248_6784188", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\nusing vll = vector<ll>;\nusing vvll = vector<vll>;\nusing vvvll = vector<vvll>;\nusing vvvvll = vector<vvvll>;\nusing vb = vector<bool>;\nusing vvb = vector<vb>;\nusing vvvb = vector<vvb>;\nusing vd = vector<double>;\nusing vvd = vector<vd>;\nusing vvvd = vector<vvd>;\n#define all(A) A.begin(),A.end()\n#define ALL(A) A.begin(),A.end()\n#define rep(i, n) for (ll i = 0; i < (ll) (n); i++)\nusing pqr = priority_queue<pair<ll, ll>, vector<pair<ll, ll>>, greater<pair<ll, ll>>>;\n\ntemplate<class T>\nbool chmax(T& p, T q, bool C = 1) {\n if (C == 0 && p == q) {\n return 1;\n }\n if (p < q) {\n p = q;\n return 1;\n }\n else {\n return 0;\n }\n}\n\ntemplate<class T>\nbool chmin(T& p, T q, bool C = 1) {\n if (C == 0 && p == q) {\n return 1;\n }\n if (p > q) {\n p = q;\n return 1;\n }\n else {\n return 0;\n }\n}\n\nll gcd(ll(a), ll(b)) {\n if (b == 0)return a;\n ll c = a;\n while (a % b != 0) {\n c = a % b;\n a = b;\n b = c;\n }\n return b;\n}\n\nvector<ll> fact, factinv, inv;\nll mod = 1e9 + 7;\nvoid prenCkModp(ll n) {\n fact.resize(n + 5);\n factinv.resize(n + 5);\n inv.resize(n + 5);\n fact.at(0) = fact.at(1) = 1;\n factinv.at(0) = factinv.at(1) = 1;\n inv.at(1) = 1;\n for (ll i = 2; i < n + 5; i++) {\n fact.at(i) = (fact.at(i - 1) * i) % mod;\n inv.at(i) = mod - (inv.at(mod % i) * (mod / i)) % mod;\n factinv.at(i) = (factinv.at(i - 1) * inv.at(i)) % mod;\n }\n\n}\nll nCk(ll n, ll k) {\n if (k < 0)return 0;\n if (n < k) return 0;\n return fact.at(n) * (factinv.at(k) * factinv.at(n - k) % mod) % mod;\n}\nnamespace atcoder {\n\n namespace internal {\n\n // @param n `0 <= n`\n // @return minimum non-negative `x` s.t. `n <= 2**x`\n int ceil_pow2(int n) {\n int x = 0;\n while ((1U << x) < (unsigned int)(n)) x++;\n return x;\n }\n\n // @param n `1 <= n`\n // @return minimum non-negative `x` s.t. `(n & (1 << x)) != 0`\n int bsf(unsigned int n) {\n#ifdef _MSC_VER\n unsigned long index;\n _BitScanForward(&index, n);\n return index;\n#else\n return __builtin_ctz(n);\n#endif\n }\n\n } // namespace internal\n\n} // namespace atcoder\n\nnamespace atcoder {\n\n template <class S,\n S(*op)(S, S),\n S(*e)(),\n class F,\n S(*mapping)(F, S),\n F(*composition)(F, F),\n F(*id)()>\n struct lazy_segtree {\n public:\n lazy_segtree() : lazy_segtree(0) {}\n lazy_segtree(int n) : lazy_segtree(std::vector<S>(n, e())) {}\n lazy_segtree(const std::vector<S>& v) : _n(int(v.size())) {\n log = internal::ceil_pow2(_n);\n size = 1 << log;\n d = std::vector<S>(2 * size, e());\n lz = std::vector<F>(size, id());\n for (int i = 0; i < _n; i++) d[size + i] = v[i];\n for (int i = size - 1; i >= 1; i--) {\n update(i);\n }\n }\n\n void set(int p, S x) {\n //assertrt(0 <= p && p < _n);\n p += size;\n for (int i = log; i >= 1; i--) push(p >> i);\n d[p] = x;\n for (int i = 1; i <= log; i++) update(p >> i);\n }\n\n S get(int p) {\n //assertrt(0 <= p && p < _n);\n p += size;\n for (int i = log; i >= 1; i--) push(p >> i);\n return d[p];\n }\n\n S prod(int l, int r) {\n //assertrt(0 <= l && l <= r && r <= _n);\n if (l == r) return e();\n\n l += size;\n r += size;\n\n for (int i = log; i >= 1; i--) {\n if (((l >> i) << i) != l) push(l >> i);\n if (((r >> i) << i) != r) push(r >> i);\n }\n\n S sml = e(), smr = e();\n while (l < r) {\n if (l & 1) sml = op(sml, d[l++]);\n if (r & 1) smr = op(d[--r], smr);\n l >>= 1;\n r >>= 1;\n }\n\n return op(sml, smr);\n }\n\n S all_prod() { return d[1]; }\n\n void apply(int p, F f) {\n //assertrt(0 <= p && p < _n);\n p += size;\n for (int i = log; i >= 1; i--) push(p >> i);\n d[p] = mapping(f, d[p]);\n for (int i = 1; i <= log; i++) update(p >> i);\n }\n void apply(int l, int r, F f) {\n //assertrt(0 <= l && l <= r && r <= _n);\n if (l == r) return;\n\n l += size;\n r += size;\n\n for (int i = log; i >= 1; i--) {\n if (((l >> i) << i) != l) push(l >> i);\n if (((r >> i) << i) != r) push((r - 1) >> i);\n }\n\n {\n int l2 = l, r2 = r;\n while (l < r) {\n if (l & 1) all_apply(l++, f);\n if (r & 1) all_apply(--r, f);\n l >>= 1;\n r >>= 1;\n }\n l = l2;\n r = r2;\n }\n\n for (int i = 1; i <= log; i++) {\n if (((l >> i) << i) != l) update(l >> i);\n if (((r >> i) << i) != r) update((r - 1) >> i);\n }\n }\n\n template <bool (*g)(S)> int max_right(int l) {\n return max_right(l, [](S x) { return g(x); });\n }\n template <class G> int max_right(int l, G g) {\n //assertrt(0 <= l && l <= _n);\n //assertrt(g(e()));\n if (l == _n) return _n;\n l += size;\n for (int i = log; i >= 1; i--) push(l >> i);\n S sm = e();\n do {\n while (l % 2 == 0) l >>= 1;\n if (!g(op(sm, d[l]))) {\n while (l < size) {\n push(l);\n l = (2 * l);\n if (g(op(sm, d[l]))) {\n sm = op(sm, d[l]);\n l++;\n }\n }\n return l - size;\n }\n sm = op(sm, d[l]);\n l++;\n } while ((l & -l) != l);\n return _n;\n }\n\n template <bool (*g)(S)> int min_left(int r) {\n return min_left(r, [](S x) { return g(x); });\n }\n template <class G> int min_left(int r, G g) {\n //assertrt(0 <= r && r <= _n);\n //assertrt(g(e()));\n if (r == 0) return 0;\n r += size;\n for (int i = log; i >= 1; i--) push((r - 1) >> i);\n S sm = e();\n do {\n r--;\n while (r > 1 && (r % 2)) r >>= 1;\n if (!g(op(d[r], sm))) {\n while (r < size) {\n push(r);\n r = (2 * r + 1);\n if (g(op(d[r], sm))) {\n sm = op(d[r], sm);\n r--;\n }\n }\n return r + 1 - size;\n }\n sm = op(d[r], sm);\n } while ((r & -r) != r);\n return 0;\n }\n\n private:\n int _n, size, log;\n std::vector<S> d;\n std::vector<F> lz;\n\n void update(int k) { d[k] = op(d[2 * k], d[2 * k + 1]); }\n void all_apply(int k, F f) {\n d[k] = mapping(f, d[k]);\n if (k < size) lz[k] = composition(f, lz[k]);\n }\n void push(int k) {\n all_apply(2 * k, lz[k]);\n all_apply(2 * k + 1, lz[k]);\n lz[k] = id();\n }\n };\n\n} // \nusing namespace atcoder;\n\nstruct S {\n ll a;\n int size;\n};\n\nstruct F {\n ll a, b;\n};\n\nS op(S l, S r) { return S{ min(l.a,r.a), l.size + r.size }; }\n\nS e() { return S{ ll(1e18), 0 }; }\n\nS mapping(F l, S r) { return S{ r.a * l.a + r.size * l.b, r.size }; }\n\nF composition(F l, F r) { return F{ r.a * l.a, r.b * l.a + l.b }; }\n\nF id() { return F{ 1, 0 }; }\n\nint main() {\n cin.tie(nullptr);\n ios::sync_with_stdio(false);\n\n while (1) {\n ll A, B, D;\n cin >> A >> B >> D;\n if (A == 0)return 0;\n for (ll d = 1; d <= D*10000; d++) {\n ll M = 1e17;\n ll AN = 1e17, BN = 1e17;\n for (ll a = 0; a <= d; a++) {\n ll b = d - a;\n if (a * A + b * B == D) {\n if (chmin(M, D)) {\n AN = a, BN = b;\n }\n }\n if (abs(a * A - b * B) == D) {\n if (chmin(M, a * A + b * B)) {\n AN = a, BN = b;\n }\n }\n }\n if (M < 1e16) {\n cout << AN << \" \" << BN << endl;\n break;\n }\n }\n }\n\n\n\n}", "accuracy": 1, "time_ms": 650, "memory_kb": 3148, "score_of_the_acc": -0.2193, "final_rank": 13 }, { "submission_id": "aoj_1248_6730692", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\n\nint main(){\n ll a, b, d;\n while(cin >> a >> b >> d){\n if(a == 0 && b == 0 && d == 0)return 0;\n array<ll,2> minv = {1ll << 60, 1ll << 60}, ans;\n auto f = [&](ll x, ll v){\n if(v % b != 0)return;\n ll y = v / b;\n if(y < 0)return;\n if(x + y < minv[0]){\n minv[0] = x + y;\n minv[1] = a * x + b * y;\n ans[0] = x, ans[1] = y;\n }else if(x + y == minv[0] && a * x + b * y < minv[1]){\n minv[1] = a * x + b * y;\n ans[0] = x, ans[1] = y;\n }\n };\n for(ll x = 0; x <= 100 * d; x++){\n f(x, d - a * x);\n f(x, a * x - d);\n f(x, a * x + d);\n }\n cout << ans[0] << \" \" << ans[1] << '\\n';\n }\n}", "accuracy": 1, "time_ms": 3310, "memory_kb": 3132, "score_of_the_acc": -1.0228, "final_rank": 19 }, { "submission_id": "aoj_1248_6040492", "code_snippet": "#include <bits/stdc++.h>\n#define rep(i,n) for(int i = 0; i < (n); i++)\nusing namespace std;\ntypedef long long ll;\n\nlong long ext_gcd(long long a, long long b, long long &x, long long &y){\n\tif(b == 0){ x = 1; y = 0; return a;}\n\tlong long d = ext_gcd(b, a%b, y, x);\n\ty -= a / b * x;\n\treturn d;\n}\n\nint main(){\n\tcin.tie(0);\n\tios::sync_with_stdio(0);\n\t\n\t// ax + by = d\n\t// a(x + b) + b(y - a) = d\n\t// a(x - b) + b(y + a) = d\n\n\twhile(true) {\n\t\tll a,b,d; cin >> a >> b >> d; if(a == 0 && b == 0 && d == 0) break;\n\t\tll x,y,g = ext_gcd(a, b, x, y);\n\t\ta /= g; b /= g; d /= g;\n\t\tx *= d; y *= d;\n\n\t\tll ansX = abs(x), ansY = abs(y);\n\t\tint range = 1000000;\n\t\tfor(int i = -range; i <= +range; i++) {\n\t\t\tll nx = abs(x + b * i), ny = abs(y - a * i);\n\t\t\tif(nx + ny < ansX + ansY || nx + ny == ansX + ansY && nx * a + ny * b < ansX * a + ansY * b)\n\t\t\t\tansX = nx, ansY = ny;\n\t\t}\n\t\tcout << ansX << \" \" << ansY << endl;\n\t}\n}", "accuracy": 1, "time_ms": 210, "memory_kb": 3464, "score_of_the_acc": -0.1361, "final_rank": 11 }, { "submission_id": "aoj_1248_5881709", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\n\ntemplate <typename T>\nT extgcd(T a, T b, T& x, T& y) {\n if (b) {\n T d = extgcd(b, a % b, y, x);\n y -= a / b * x;\n return d;\n } else {\n x = 1;\n y = 0;\n return a;\n }\n}\n\nint main() {\n ll a, b, d;\n while (cin >> a >> b >> d, a) {\n ll x, y, g;\n g = extgcd(a, b, x, y);\n a /= g;\n b /= g;\n d /= g;\n ll Xbest = 1e18, Ybest = 1e18;\n for (int i = -1e5; i < 1e5; i++) {\n ll X = abs(d * x + b * i), Y = abs(d * y - a * i);\n if (X + Y < Xbest + Ybest || (X + Y == Xbest + Ybest && a * X + b * Y < a * Xbest + b * Ybest))\n Xbest = X, Ybest = Y;\n }\n cout << Xbest << \" \" << Ybest << endl;\n }\n}", "accuracy": 1, "time_ms": 20, "memory_kb": 3416, "score_of_the_acc": -0.0709, "final_rank": 8 }, { "submission_id": "aoj_1248_5826384", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\nint a, b, d;\n\nvoid solve();\n\nint main() {\n while (1) {\n cin >> a >> b >> d;\n if (!(a + b + d)) break;\n solve();\n }\n return 0;\n}\n\nvoid solve() {\n struct dat {\n int x, y, total;\n dat(int _x = 1e8, int _y = 1e8, int _t = 0) : x(_x), y(_y), total(_t) {}\n bool operator>(const dat& r) const {\n if (x + y != r.x + r.y) return x + y > r.x + r.y;\n return a * x + b * y > a * r.x + b * r.y;\n }\n };\n const int len = 200000;\n vector<dat> dp(len + 1);\n priority_queue<dat, vector<dat>, greater<dat>> pq;\n dp[0] = dat(0, 0, 0);\n pq.push(dp[0]);\n while (pq.size()) {\n auto [x, y, total] = pq.top();\n pq.pop();\n if (x != dp[total].x || y != dp[total].y) continue;\n for (int i = -1; i <= 1; ++i)\n for (int j = -1; j <= 1; ++j) {\n int tx = x + (i != 0), ty = y + (j != 0), to = total + i * a + j * b;\n if (to < 0 || to > len || !(dp[to] > dat(tx, ty, to))) continue;\n dp[to] = dat(tx, ty, to);\n pq.push(dp[to]);\n }\n }\n cout << dp[d].x << \" \" << dp[d].y << endl;\n}", "accuracy": 1, "time_ms": 1300, "memory_kb": 5448, "score_of_the_acc": -0.7809, "final_rank": 18 }, { "submission_id": "aoj_1248_5704398", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n#define db double\n#define ll long long\n#define Pir pair<ll, ll>\n#define fi first\n#define se second\n#define pb push_back\n#define m_p make_pair\n#define inf 0x3f3f3f3f\n#define INF 0x3f3f3f3f3f3f3f3f\n/*==========ACMer===========*/\nll a, b, c;\n\nint main()\n{\n while (scanf(\"%lld %lld %lld\", &a, &b, &c) && a + b + c)\n {\n ll ans = inf, sum = 0; Pir num;\n for (int i = 0; i <= 10 * c; i ++)\n {\n ll tmp = abs(c - i * a);\n if (tmp % b == 0)\n {\n int j = tmp / b;\n if (i + j < ans)\n {\n ans = i + j;\n sum = i * a + j * b;\n num = m_p(i, j);\n }\n else if (i + j == ans && i * a + j * b < sum)\n {\n sum = i * a + j * b;\n num = m_p(i, j);\n }\n }\n }\n\n for (int j = 0; j <= 10 * c; j ++)\n {\n ll tmp = abs(c - j * b);\n if (tmp % a == 0)\n {\n int i = tmp / a;\n if (i + j < ans)\n {\n ans = i + j;\n sum = i * a + j * b;\n num = m_p(i, j);\n }\n else if (i + j == ans && i * a + j * b < sum)\n {\n sum = i * a + j * b;\n num = m_p(i, j);\n }\n }\n\n }\n\n printf(\"%lld %lld\\n\", num.fi, num.se);\n }\n\n return 0;\n}", "accuracy": 1, "time_ms": 240, "memory_kb": 3412, "score_of_the_acc": -0.1369, "final_rank": 12 }, { "submission_id": "aoj_1248_5621403", "code_snippet": "#include <cmath>\n#include <deque>\n#include <algorithm>\n#include <iterator>\n#include <list>\n#include <tuple>\n#include <map>\n#include <unordered_map>\n#include <queue>\n#include <set>\n#include <unordered_set>\n#include <stack>\n#include <string>\n#include <vector>\n#include <fstream>\n#include <iostream>\n#include <functional>\n#include <numeric>\n#include <iomanip> \n#include <stdio.h>\n//eolibraries\n#define lnf 3999999999999999999\n#define inf 999999999\n#define fi first\n#define se second\n#define pb push_back\n#define ll long long\n#define ld long double\n#define all(c) (c).begin(),(c).end()\n#define sz(c) (ll)(c).size()\n#define make_unique(a) sort(all(a)),a.erase(unique(all(a)),a.end())\n#define pii pair <ll,ll>\n#define rep(i,n) for(ll i = 0 ; i < n ; i++) \n#define drep(i,n) for(ll i = n-1 ; i >= 0 ; i--)\n#define crep(i,x,n) for(ll i = x ; i < n ; i++)\n#define vi vector <ll> \n#define vec(...) vector<__VA_ARGS__>\n#define fcin ios_base::sync_with_stdio(false),cin.tie(0),cout.tie(0);\n//eodefine\nusing namespace std;\n\nconst ll max_n = 103002;\n\nint main(){\nfcin;\n\twhile(true){\n\t\tll a,b,d;\n\t\tcin>>a>>b>>d;\n\t\tif(!a and !b and !d) break;\n\t\tll ans=inf,ansx=0,ansy=0,p=0;\n\t\t\n\t\tfunction<void(ll,ll)> comp = [&](ll x,ll y){\n\t\t\tif(ans>x+y){\n\t\t\t\tans=x+y;\n\t\t\t\tp=a*x+b*y;\n\t\t\t\tansx=x,ansy=y;\n\t\t\t}else if(ans==x+y){\n\t\t\t\tif(a*x+b*y<p){\n\t\t\t\t\tp=a*x+b*y;\n\t\t\t\t\tansx=x,ansy=y;\n\t\t\t\t}\n\t\t\t}\n\t\t\treturn;\n\t\t};\n\n\t\trep(x,50000){\n\t\t\tif((d-x*a)%b==0){\n\t\t\t\tll y = (d-x*a)/b;\n\t\t\t\tif(x*a+y*b==d and y>=0){\n\t\t\t\t\tcomp(x,y);\n\t\t\t\t}\n\t\t\t}\n\t\t\tif((d+x*a)%b==0){\n\t\t\t\tll y = (d+x*a)/b;\n\t\t\t\tif(x*a+d==y*b and y>=0){\n\t\t\t\t\tcomp(x,y);\n\t\t\t\t}\n\t\t\t}\n\t\t\tif((x*a-d)%b==0){\n\t\t\t\tll y = (x*a-d)/b;\n\t\t\t\t// if(x==1)cout<<y<<\"\\n\";\n\t\t\t\tif(x*a-d==y*b and y>=0){\n\t\t\t\t\tcomp(x,y);\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\tcout<<ansx<<\" \"<<ansy<<\"\\n\";\n\t}\n/*\n*/\n return 0; \n}", "accuracy": 1, "time_ms": 80, "memory_kb": 3296, "score_of_the_acc": -0.07, "final_rank": 7 }, { "submission_id": "aoj_1248_5074528", "code_snippet": "#include<iostream>\n#include<iomanip>\n#include<cassert>\n#include<math.h>\n#include<complex>\n#include<algorithm>\n#include<utility>\n#include<queue>\n#include<stack>\n#include<string.h>\n#include<string>\n#include<set>\n#include<map>\n#include<unordered_map>\n#include<functional>\n#include<vector>\n#include<bitset>\nusing namespace std;\ntypedef long long ll;\ntypedef pair<ll,ll> L_L;\nconst ll INF=2e18;\nconst ll MOD=1e9+7;\n\nll gcd(ll a, ll b) {\n if (b == 0) return a;\n return gcd(b, a % b);\n}\n\nll extgcd(ll a, ll b, ll c, ll& x, ll& y) {\n if (b == 0) {\n if(c%a!=0){\n x=INF;\n y=INF;\n return INF;\n }\n x=c/a;\n y=0;\n return a;\n }\n ll x0,y0;\n ll g=extgcd(b,a%b,c,x0,y0);\n x = y0;\n y = x0-(a/b)*y0;\n return g;\n}\n\nll A,B,D;\nll Mass(L_L p){\n ll x=p.first;\n ll y=p.second;\n return A*abs(x)+B*abs(y);\n}\nbool GreaterA(L_L A,L_L B){\n ll aSum=abs(A.first)+abs(A.second);\n ll bSum=abs(B.first)+abs(B.second);\n if(aSum==bSum){\n return Mass(A) > Mass(B);\n }\n return aSum > bSum;\n}\nint main(){\n while(cin>>A>>B>>D,A!=0){\n ll g=gcd(gcd(A,B),D);\n A/=g; B/=g; D/=g;\n ll x,y;\n extgcd(A,B,D,x,y);\n L_L ans(x,y);\n for(ll i=-100000;i<=100000;i++){\n L_L now=L_L(x+B*i,y-A*i);\n if(GreaterA(ans,now)){\n ans=now;\n }\n }\n cout<<abs(ans.first)<<\" \"<<abs(ans.second)<<endl;\n }\n\n return 0;\n}", "accuracy": 1, "time_ms": 50, "memory_kb": 3080, "score_of_the_acc": -0.0267, "final_rank": 2 }, { "submission_id": "aoj_1248_4964543", "code_snippet": "// #include <atcoder/all>\n// using namespace atcoder;\n#include <bits/stdc++.h>\n#define rep(i, n) for (int i = 0; i < (int)n; ++i)\n#define rrep(i, n) for (int i = (int)n-1; i >= 0; --i)\nusing namespace std;\nusing ll = long long;\ntemplate<typename T>\ninline bool chmax(T& a, const T& b) {\n if (a < b){\n a = b;\n return true;\n }\n return false;\n}\ntemplate<typename T>\ninline bool chmin(T& a, const T& b) {\n if (b < a) {\n a = b;\n return true;\n }\n return false;\n}\n/**\n * @brief 多次元 vector の作成\n * @author えびちゃん\n */\nnamespace detail {\n template<typename T, int N>\n auto make_vec(vector<int>& sizes, T const& x) {\n if constexpr (N == 1) {\n return vector(sizes[0], x);\n } else {\n int size = sizes[N-1];\n sizes.pop_back();\n return vector(size, make_vec<T, N-1>(sizes, x));\n }\n }\n}\ntemplate<typename T, int N>\nauto make_vec(int const(&sizes)[N], T const& x = T()) {\n vector<int> s(N);\n for (int i = 0; i < N; ++i) s[i] = sizes[N-i-1];\n return detail::make_vec<T, N>(s, x);\n}\n__attribute__((constructor))\nvoid fast_io() {\n ios::sync_with_stdio(false);\n cin.tie(nullptr);\n}\n\n\n\nint solve() {\n ll a, b, c;\n cin >> a >> b >> c;\n if (a == 0) return 1;\n\n auto update = [&](ll& prex, ll& prey, ll newx, ll newy) {\n if (prex + prey > newx + newy) {\n prex = newx;\n prey = newy;\n } else if (prex + prey == newx + newy) {\n if (prex * a + prey * b > newx * a + newy * b) {\n prex = newx;\n prey = newy;\n }\n }\n };\n\n ll ansx = INT_MAX, ansy = INT_MAX;\n for (ll x = 0; x < 100000; ++x) {\n ll y;\n if (a * x - c >= 0 && (a * x - c) % b == 0) {\n y = (a * x - c) / b;\n update(ansx, ansy, x, y);\n }\n if ((a * x + c) % b == 0) {\n y = (a * x + c) / b;\n update(ansx, ansy, x, y);\n }\n if (c - a * x >= 0 && (c - a * x) % b == 0) {\n y = (c - a * x) / b;\n update(ansx, ansy, x, y);\n }\n }\n cout << ansx << ' ' << ansy << '\\n';\n\n return 0;\n}\n\nint main() {\n while (!solve());\n}", "accuracy": 1, "time_ms": 110, "memory_kb": 3388, "score_of_the_acc": -0.0937, "final_rank": 10 }, { "submission_id": "aoj_1248_4962965", "code_snippet": "#include <bits/stdc++.h>\n\nusing namespace std;\n\n#define REP(i, n) for (int i=0; i<(n); ++i)\n#define RREP(i, n) for (int i=(int)(n)-1; i>=0; --i)\n#define FOR(i, a, n) for (int i=(a); i<(n); ++i)\n#define RFOR(i, a, n) for (int i=(int)(n)-1; i>=(a); --i)\n\n#define SZ(x) ((int)(x).size())\n#define ALL(x) (x).begin(),(x).end()\n\n#define DUMP(x) cerr<<#x<<\" = \"<<(x)<<endl\n#define DEBUG(x) cerr<<#x<<\" = \"<<(x)<<\" (L\"<<__LINE__<<\")\"<<endl;\n\ntemplate<class T>\nostream &operator<<(ostream &os, const vector<T> &v) {\n os << \"[\";\n REP(i, SZ(v)) {\n if (i) os << \", \";\n os << v[i];\n }\n return os << \"]\";\n}\n\ntemplate<class T, class U>\nostream &operator<<(ostream &os, const pair<T, U> &p) {\n return os << \"(\" << p.first << \" \" << p.second << \")\";\n}\n\ntemplate<class T>\nbool chmax(T &a, const T &b) {\n if (a < b) {\n a = b;\n return true;\n }\n return false;\n}\n\ntemplate<class T>\nbool chmin(T &a, const T &b) {\n if (b < a) {\n a = b;\n return true;\n }\n return false;\n}\n\nusing ll = long long;\nusing ull = unsigned long long;\nusing ld = long double;\nusing P = pair<int, int>;\nusing vi = vector<int>;\nusing vll = vector<ll>;\nusing vvi = vector<vi>;\nusing vvll = vector<vll>;\n\nconst ll MOD = 1e9 + 7;\nconst int INF = INT_MAX / 2;\nconst ll LINF = LLONG_MAX / 2;\nconst ld eps = 1e-9;\n\nint main() {\n cin.tie(0);\n ios::sync_with_stdio(false);\n cout << fixed << setprecision(10);\n\n while(1) {\n ll a, b, d; cin >> a >> b >> d;\n if(a == 0 && b == 0 && d == 0) break;\n\n ll mi1 = LINF;\n ll mi2 = LINF;\n ll ansx = 0, ansy = 0;\n REP(x, 1000000) {\n // left\n ll v = abs(d - a * x);\n if(v % b == 0) {\n ll y = v / b;\n if(mi1 > x + y || (mi1 == x + y && mi2 > a * x + b * y)) {\n chmin(mi1, x + y);\n chmin(mi2, a * x + b * y);\n ansx = x;\n ansy = y;\n }\n }\n\n // right\n v = abs(d + a * x);\n if(v % b == 0) {\n ll y = v / b;\n if(mi1 > x + y || (mi1 == x + y && mi2 > a * x + b * y)) {\n chmin(mi1, x + y);\n chmin(mi2, a * x + b * y);\n ansx = x;\n ansy = y;\n }\n }\n }\n cout << ansx << \" \" << ansy << endl;\n\n }\n\n return 0;\n}", "accuracy": 1, "time_ms": 1230, "memory_kb": 3356, "score_of_the_acc": -0.428, "final_rank": 16 }, { "submission_id": "aoj_1248_4929588", "code_snippet": "#pragma GCC optimize(\"Ofast\")\n#include <iostream> // cout, endl, cin\n#include <string> // string, to_string, stoi\n#include <vector> // vector\n#include <algorithm> // min, max, swap, sort, reverse, lower_bound, upper_bound\n#include <utility> // pair, make_pair\n#include <tuple> // tuple, make_tuple\n#include <cstdint> // int64_t, int*_t\n#include <cstdio> // printf\n#include <map> // map\n#include <queue> // queue, priority_queue\n#include <set> // set\n#include <stack> // stack\n#include <deque> // deque\n#include <unordered_map> // unordered_map\n#include <unordered_set> // unordered_set\n#include <bitset> // bitset\n#include <cctype> // isupper, islower, isdigit, toupper, tolower\n#include <iomanip> // setprecision\n#include <complex> // complex\n#include <math.h> \n#include <functional>\n#include <cassert>\nusing namespace std;\nusing ll = long long;\nusing P = pair<int,int>;\nconstexpr ll INF = 1e18;\nconstexpr int inf = 1e9;\nconstexpr ll mod = 1000000007;\nconstexpr ll mod2 = 998244353;\nconst int dx[8] = {1, 0, -1, 0,1,1,-1,-1};\nconst int dy[8] = {0, 1, 0, -1,1,-1,1,-1};\ntemplate<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }\ntemplate<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }\n// ---------------------------------------------------------------------------\n\nbool solve(){\n ll A,B,D;\n cin >> A >> B >> D;\n if(A==0 && B==0 && D==0) return false;\n ll x,y;\n pair<ll,ll> now(INF,INF);\n for(ll i=0; i<=1000000; i++){\n if(abs(A*i-D)%B == 0){\n ll nowX = i,nowY = abs(A*i-D)/B;\n if(chmin(now,pair<ll,ll>(nowX+nowY,nowX*A+nowY*B))){\n x = nowX;\n y = nowY;\n }\n }\n if(abs(B*i-D)%A == 0){\n ll nowX = i,nowY = abs(B*i-D)/A;\n if(chmin(now,pair<ll,ll>(nowX+nowY,nowX*A+nowY*B))){\n y = nowX;\n x = nowY;\n }\n }\n }\n cout << x << \" \" << y << \"\\n\";\n return true; \n}\n\n\nint main(){\n ios::sync_with_stdio(false);\n cin.tie(nullptr);\n while(solve());\n return 0;\n}", "accuracy": 1, "time_ms": 1190, "memory_kb": 3292, "score_of_the_acc": -0.4058, "final_rank": 15 }, { "submission_id": "aoj_1248_4922330", "code_snippet": "// I SELL YOU...! \n#include<iostream>\n#include<vector>\n#include<algorithm>\n#include<functional>\n#include<queue>\n#include<chrono>\n#include<iomanip>\n#include<map>\n#include<set>\nusing namespace std;\nusing ll = long long;\nusing P = pair<ll,ll>;\nusing TP = tuple<ll,ll,ll,ll>;\nvoid init_io(){\n cin.tie(0);\n ios::sync_with_stdio(false);\n cout << fixed << setprecision(18);\n}\nconst ll INF = 1e15;\nvoid solve(){\n ll a,b,d;\n ll v0=INF,v1=INF;\n ll ans_a=-1,ans_b=-1;\n cin >> a >> b >> d;\n if((a|b|d)==0) return;\n for(ll i=0;i<1e5;i++){\n ll na,nb;\n ll ax,bx;\n ll t_v0,t_v1;\n\n ax = i;\n na = a * i;\n if((na+d)%b==0){\n bx = (na+d)/b;\n nb = na+d;\n t_v0 = ax+bx;\n t_v1 = na+nb;\n if(v0>t_v0||(v0==t_v0&&t_v1<v1)){\n ans_a = ax;\n ans_b = bx;\n v0 = t_v0;\n v1 = t_v1;\n }\n }\n bx = i;\n nb = b * i;\n if((nb+d)%a==0){\n ax = (nb+d)/a;\n na = nb+d;\n t_v0 = ax+bx;\n t_v1 = na+nb;\n if(v0>t_v0||(v0==t_v0&&t_v1<v1)){\n ans_a = ax;\n ans_b = bx;\n v0 = t_v0;\n v1 = t_v1;\n }\n }\n if(d-nb>=0&&(d-nb)%a==0){\n ax = (d-nb)/a;\n na = d-nb;\n t_v0 = ax+bx;\n t_v1 = na+nb;\n if(v0>t_v0||(v0==t_v0&&t_v1<v1)){\n ans_a = ax;\n ans_b = bx;\n v0 = t_v0;\n v1 = t_v1;\n }\n }\n }\n cout << ans_a <<\" \"<<ans_b << endl;\n solve();\n}\nsigned main(){\n init_io();\n solve();\n}", "accuracy": 1, "time_ms": 120, "memory_kb": 3264, "score_of_the_acc": -0.0771, "final_rank": 9 }, { "submission_id": "aoj_1248_4918492", "code_snippet": "#include <bits/stdc++.h>\n//#include <atcoder/all>\n//using namespace atcoder;\nconst double pi = 3.141592653589793238462643383279;\nusing namespace std;\n//typedef\n//------------------------------------------\ntypedef vector<int> VI;\ntypedef vector<VI> VVI;\ntypedef vector<string> VS;\ntypedef pair<int, int> PII;\ntypedef pair<long long, long long> PLL;\ntypedef pair<int, PII> TIII;\ntypedef long long LL;\ntypedef unsigned long long ULL;\ntypedef vector<LL> VLL;\ntypedef vector<VLL> VVLL;\n\n//container util\n//------------------------------------------\n#define ALL(a) (a).begin(), (a).end()\n#define RALL(a) (a).rbegin(), (a).rend()\n#define PB push_back\n#define MP make_pair\n#define SZ(a) int((a).size())\n#define SQ(a) ((a) * (a))\n#define EACH(i, c) for (typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)\n#define EXIST(s, e) ((s).find(e) != (s).end())\n#define SORT(c) sort((c).begin(), (c).end())\n\n//repetition\n//------------------------------------------\n#define FOR(i, s, n) for (int i = s; i < (int)n; ++i)\n#define REP(i, n) FOR(i, 0, n)\n#define MOD 1000000007\n\n#define rep(i, a, b) for (int i = a; i < (b); ++i)\n#define trav(a, x) for (auto &a : x)\n#define all(x) x.begin(), x.end()\n#define sz(x) (int)(x).size()\n\ntypedef long long ll;\ntypedef pair<int, int> pii;\ntypedef vector<int> vi;\n\n#define chmin(x, y) x = min(x, y)\n#define chmax(x, y) x = max(x, y)\nconst double EPS = 1e-11, PI = acos(-1);\n//ここから編集\n\nint main()\n{\n cin.tie(0);\n ios::sync_with_stdio(false);\n cout << fixed << setprecision(10);\n \n int A, B, D;\n while(cin >> A >> B >> D){\n if(A == 0 && B == 0 && D == 0) break;\n\n /* x+y が小さくなる */\n /* ax+byが小さくなる */\n\n map<int, pair<int, int>> dp;\n\n dp[D] = make_pair(0, 0);\n /* 0を目指す */\n\n priority_queue<pair<pair<int, int>, int>, vector<pair<pair<int, int>, int>>, greater<pair<pair<int, int>, int>>> q;\n q.push(make_pair(make_pair(0, 0), D));\n\n while(q.size()){\n auto p = q.top();\n q.pop();\n\n int xy = p.first.first;\n int ax = p.first.second;\n int v = p.second;\n if(v == 0) break;\n if(dp[v] < make_pair(xy, ax)) continue;\n\n int nv = v+A;\n if(nv <= 50000){\n if(dp.find(nv) == dp.end()){\n dp[nv] = make_pair(xy+1, ax+A);\n q.push(make_pair(dp[nv], nv));\n }else{\n if(dp[nv] > make_pair(xy+1, ax+A)){\n dp[nv] = make_pair(xy+1, ax+A);\n q.push(make_pair(dp[nv], nv));\n }\n }\n }\n nv = v-A;\n if(nv >= -50000){\n if(dp.find(nv) == dp.end()){\n dp[nv] = make_pair(xy+1, ax+A);\n q.push(make_pair(dp[nv], nv));\n }else{\n if(dp[nv] > make_pair(xy+1, ax+A)){\n dp[nv] = make_pair(xy+1, ax+A);\n q.push(make_pair(dp[nv], nv));\n }\n }\n }\n nv = v+B;\n if(nv <= 50000){\n if(dp.find(nv) == dp.end()){\n dp[nv] = make_pair(xy+1, ax+B);\n q.push(make_pair(dp[nv], nv));\n }else{\n if(dp[nv] > make_pair(xy+1, ax+B)){\n dp[nv] = make_pair(xy+1, ax+B);\n q.push(make_pair(dp[nv], nv));\n }\n }\n }\n nv = v-B;\n if(nv >= -50000){\n if(dp.find(nv) == dp.end()){\n dp[nv] = make_pair(xy+1, ax+B);\n q.push(make_pair(dp[nv], nv));\n }else{\n if(dp[nv] > make_pair(xy+1, ax+B)){\n dp[nv] = make_pair(xy+1, ax+B);\n q.push(make_pair(dp[nv], nv));\n }\n }\n }\n }\n int X = dp[0].first;\n int Y = dp[0].second;\n\n int ans1=-1, ans2=-1;\n for(int i=0; i<=X; i++){\n int x = i, y = X-i;\n if(x*A + y*B == Y){\n ans1 = x, ans2 = y;\n }\n }\n cout << ans1 << \" \" << ans2 << endl;\n }\n return 0;\n}", "accuracy": 1, "time_ms": 3180, "memory_kb": 9296, "score_of_the_acc": -1.9606, "final_rank": 20 }, { "submission_id": "aoj_1248_4917902", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\ntypedef long long lint;\n#define rep(i,n) for(lint (i)=0;(i)<(n);(i)++)\n#define repp(i,m,n) for(lint (i)=(m);(i)<(n);(i)++)\n#define repm(i,n) for(lint (i)=(n-1);(i)>=0;(i)--)\n#define INF (1ll<<60)\n#define all(x) (x).begin(),(x).end()\nconst lint MOD =1000000007;\nconst lint MAX = 1000000;\nusing Graph =vector<vector<lint>>;\ntypedef pair<lint,lint> P;\ntypedef map<lint,lint> M;\n#define chmax(x,y) x=max(x,y)\n#define chmin(x,y) x=min(x,y)\n\n \nlint fac[MAX], finv[MAX], inv[MAX];\n \nvoid COMinit() \n{\n fac[0] = fac[1] = 1;\n finv[0] = finv[1] = 1;\n inv[1] = 1;\n for (lint i = 2; i < MAX; i++)\n {\n fac[i] = fac[i - 1] * i % MOD;\n inv[i] = MOD - inv[MOD % i] * (MOD / i) % MOD;\n finv[i] = finv[i - 1] * inv[i] % MOD;\n }\n}\n \nlong long COM(lint n, lint k)\n{\n if (n < k)\n return 0;\n if (n < 0 || k < 0)\n return 0;\n return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD;\n}\n \nlint primary(lint num)\n{\n if (num < 2) return 0;\n else if (num == 2) return 1;\n else if (num % 2 == 0) return 0;\n \n double sqrtNum = sqrt(num);\n for (int i = 3; i <= sqrtNum; i += 2)\n {\n if (num % i == 0)\n {\n return 0;\n }\n }\n \n return 1;\n}\n long long modpow(long long a, long long n, long long mod) {\n long long res = 1;\n while (n > 0) {\n if (n & 1) res = res * a % mod;\n a = a * a % mod;\n n >>= 1;\n }\n return res;\n}\n lint lcm(lint a,lint b){\n return a/__gcd(a,b)*b;\n }\n lint gcd(lint a,lint b){\n return __gcd(a,b);\n } \n\n int main(){\n while(true){\n lint a,b,c;\n cin>>a>>b>>c;\n if(a==0&&b==0&&c==0)break;\n lint w=INF,cost=INF,ans1,ans2;\n rep(i,1000000){\n lint x=a*i;\n lint d=abs(c-x);\n if(d%b==0){\n lint cmp=i+d/b;\n if(cost>cmp){\n cost=cmp;\n w=i*a+d;\n ans1=i;\n ans2=d/b;\n }else if(cost==cmp&&(i*a+d)<w){\n cost=cmp;\n w=i*a+d;\n ans1=i;\n ans2=d/b;\n }\n }\n d=abs(c+x);\n if(d%b==0){\n lint cmp=i+d/b;\n if(cost>cmp){\n cost=cmp;\n w=i*a+d;\n ans1=i;\n ans2=d/b;\n }else if(cost==cmp&&(i*a+d)<w){\n cost=cmp;\n w=i*a+d;\n ans1=i;\n ans2=d/b;\n }\n }\n }\n cout<<ans1<<\" \"<<ans2<<endl;\n }\n \n \n }", "accuracy": 1, "time_ms": 1470, "memory_kb": 2988, "score_of_the_acc": -0.4424, "final_rank": 17 }, { "submission_id": "aoj_1248_4855723", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\ntypedef long long ll;\n\ntemplate<typename T> T extgcd(T a,T b,T &x,T &y){\n T d=a;\n if (b!=0){\n d=extgcd(b,a%b,y,x);\n y-=(a/b)*x;\n } else x=1,y=0;\n return d;\n}\n\nvoid solve(int a,int b,int d){\n int x,y;\n ll g=extgcd(a,b,x,y);\n a/=g; b/=g; d/=g; x*=d; y*=d;\n ll ax=abs(x),ay=abs(y),sum=ax+ay,mass=a*ax+b*ay;\n auto update=[&](ll x,ll y){\n if (x+y<sum){\n sum=x+y; mass=a*x+b*y;\n ax=x; ay=y;\n } else if (a*x+b*y<mass){\n mass=a*x+b*y;\n ax=x; ay=y;\n }\n };\n for (int i=-50000;i<=50000;++i) update(abs(x-b*i),abs(y+a*i));\n\n cout << ax << ' ' << ay << '\\n';\n}\n\nint main(){\n cin.tie(0);\n ios::sync_with_stdio(false);\n int a,b,d;\n while(cin >> a >> b >> d,a){\n solve(a,b,d);\n }\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3144, "score_of_the_acc": -0.0247, "final_rank": 1 } ]
aoj_1249_cpp
Problem B: Make a Sequence Your company’s next product will be a new game, which is a three-dimensional variant of the classic game “Tic-Tac-Toe”. Two players place balls in a three-dimensional space (board), and try to make a sequence of a certain length. People believe that it is fun to play the game, but they still cannot fix the values of some parameters of the game. For example, what size of the board makes the game most exciting? Parameters currently under discussion are the board size (we call it n in the following) and the length of the sequence ( m ). In order to determine these parameter values, you are requested to write a computer simulator of the game. You can see several snapshots of the game in Figures 1-3. These figures correspond to the three datasets given in the Sample Input. Figure 1: A game with n = m = 3 Here are the precise rules of the game. Two players, Black and White, play alternately. Black plays first. There are n × n vertical pegs. Each peg can accommodate up to n balls. A peg can be specified by its x - and y -coordinates (1 ≤ x , y ≤ n ). A ball on a peg can be specified by its z -coordinate (1 ≤ z ≤ n ). At the beginning of a game, there are no balls on any of the pegs. Figure 2: A game with n = m = 3 (White made a 3-sequence before Black) On his turn, a player chooses one of n × n pegs, and puts a ball of his color onto the peg. The ball follows the law of gravity. That is, the ball stays just above the top-most ball on the same peg or on the floor (if there are no balls on the peg). Speaking differently, a player can choose x - and y -coordinates of the ball, but he cannot choose its z -coordinate. The objective of the game is to make an m -sequence. If a player makes an m -sequence or longer of his color, he wins. An m -sequence is a row of m consecutive balls of the same color. For example, black balls in positions (5, 1, 2), (5, 2, 2) and (5, 3, 2) form a 3-sequence. A sequence can be horizontal, vertical, or diagonal. Precisely speaking, there are 13 possible directions to make a sequence, categorized as follows. Figure 3: A game with n = 4, m = 3 (Black made two 4-sequences) (a) One-dimensional axes. For example, (3, 1, 2), (4, 1, 2) and (5, 1, 2) is a 3-sequence. There are three directions in this category. (b) Two-dimensional diagonals. For example, (2, 3, 1), (3, 3, 2) and (4, 3, 3) is a 3-sequence. There are six directions in this category. (c) Three-dimensional diagonals. For example, (5, 1, 3), (4, 2, 4) and (3, 3, 5) is a 3- sequence. There are four directions in this category. Note that we do not distinguish between opposite directions. As the evaluation process of the game, people have been playing the game several times changing the parameter values. You are given the records of these games. It is your job to write a computer program which determines the winner of each recorded game. Since it is difficult for a human to find three-dimensional sequences ...(truncated)
[ { "submission_id": "aoj_1249_10896082", "code_snippet": "#include<bits/stdc++.h>\n// #include<atcoder/all>\n// #include<boost/multiprecision/cpp_int.hpp>\n\nusing namespace std;\n// using namespace atcoder;\n// using bint = boost::multiprecision::cpp_int;\nusing ll = long long;\nusing ull = unsigned long long;\nusing ld = long double;\nusing pii = pair<int,int>;\nusing pll = pair<ll,ll>;\nusing vi = vector<ll>;\nusing vvi = vector<vi>;\nusing vvvi = vector<vvi>;\nusing ve = vector<vector<int>>;\nusing vb = vector<bool>;\nusing vvb = vector<vb>;\n#define rep(i,s,n) for(ll i = (ll)s;i < (ll)n;i++)\n#define rrep(i,l,r) for(ll i = r-1;i >= l;i--)\n#define ALL(x) (x).begin(),(x).end()\n#define sz(c) ((ll)(c).size())\n#define LB(A,x) (int)(lower_bound(A.begin(),A.end(),x)-A.begin())\n#define UB(A,x) (int)(upper_bound(A.begin(),A.end(),x)-A.begin())\n// #define MOD 1000000007\n#define MOD 998244353\ntemplate<typename T>using min_priority_queue=priority_queue<T,vector<T>,greater<T>>;\ntemplate<typename T>ostream&operator<<(ostream&os,vector<T>&v){for(int i = 0;i < v.size();i++)os<<v[i]<<(i+1!=v.size()?\" \":\"\");return os;}\ntemplate<typename T>istream&operator>>(istream&is,vector<T>&v){for(T&in:v)is>>in;return is;}\ntemplate<typename T1,typename T2>ostream&operator<<(ostream&os,pair<T1,T2>&p){os<<p.first<<\" \"<<p.second;return os;}\ntemplate<typename T1,typename T2>istream&operator>>(istream&is,pair<T1,T2>&p){is>>p.first>>p.second;return is;}\ntemplate<typename T> inline bool chmax(T &a,T b){if(a < b){a = b;return true;}return false;}\ntemplate<typename T> inline bool chmin(T &a,T b){if(a > b){a = b;return true;}return false;}\nld dist(ld x1,ld y1,ld x2, ld y2){return sqrtl((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2));}\n\nint dx[] = {-1,-1,0,1,-1,-1,-1,0,0,1,1,1,0};\nint dy[] = {0,1,1,1,-1,0,1,-1,1,-1,0,1,0};\nint dz[] = {0,0,0,0,1,1,1,1,1,1,1,1,1};\n\nint main(){\n \n ios_base::sync_with_stdio(0), cin.tie(0);\n while(1){\n int n,m,p;cin >> n >> m >> p;\n if(!n)break;\n vvvi mem(n,vvi(n,vi(n,-1)));\n auto ch = [&](int cl){\n rep(i,0,n)rep(j,0,n)rep(k,0,n)rep(dir,0,13){\n bool is = true;\n rep(l,0,m){\n int ni = i + dx[dir]*l;\n int nj = j + dy[dir]*l;\n int nk = k + dz[dir]*l;\n if(ni < 0 || ni >= n || nj < 0 || nj >= n || nk < 0 || nk >= n){is = false;break;}\n if(mem[ni][nj][nk] != cl){is = false;break;}\n }\n if(is)return true;\n }\n return false;\n };\n vvi cl(n,vi(n));\n int win = -1,moves = -1;\n rep(_,0,p){\n int x,y;cin >> x >> y;\n x--;y--;\n mem[x][y][cl[x][y]++] = _ & 1;\n if(win == -1 && ch(_ & 1))win = _ & 1,moves = _+1;\n }\n if(win == -1)cout << \"Draw\\n\";\n else if(win == 0)cout << \"Black \" << moves << \"\\n\";\n else cout << \"White \" << moves << \"\\n\";\n }\n \n\n return 0;\n}", "accuracy": 1, "time_ms": 130, "memory_kb": 3364, "score_of_the_acc": -0.6603, "final_rank": 7 }, { "submission_id": "aoj_1249_10189552", "code_snippet": "#include <vector>\n#include <iostream>\n#include <algorithm>\nusing namespace std;\n\nint main() {\n\twhile (true) {\n\t\tint N, M, P;\n\t\tcin >> N >> M >> P;\n\t\tif (N == 0 && M == 0 && P == 0) {\n\t\t\tbreak;\n\t\t}\n\t\tvector<vector<vector<int> > > A(N, vector<vector<int> >(N, vector<int>(N, -1)));\n\t\tvector<vector<vector<int> > > C(N, vector<vector<int> >(N, vector<int>(N, -1)));\n\t\tvector<vector<int> > H(N, vector<int>(N, 0));\n\t\tfor (int i = 0; i < P; i++) {\n\t\t\tint x, y;\n\t\t\tcin >> x >> y;\n\t\t\tx--; y--;\n\t\t\tA[H[x][y]][x][y] = i;\n\t\t\tC[H[x][y]][x][y] = i % 2;\n\t\t\tH[x][y]++;\n\t\t}\n\t\tauto inside = [&](int x, int y, int z) -> bool {\n\t\t\treturn 0 <= x && x < N && 0 <= y && y < N && 0 <= z && z < N;\n\t\t};\n\t\tint ans = P;\n\t\tfor (int i = 0; i < N; i++) {\n\t\t\tfor (int j = 0; j < N; j++) {\n\t\t\t\tfor (int k = 0; k < N; k++) {\n\t\t\t\t\tfor (int dx = -1; dx <= +1; dx++) {\n\t\t\t\t\t\tfor (int dy = -1; dy <= +1; dy++) {\n\t\t\t\t\t\t\tfor (int dz = -1; dz <= +1; dz++) {\n\t\t\t\t\t\t\t\tif (dx | dy | dz) {\n\t\t\t\t\t\t\t\t\tbool f = true;\n\t\t\t\t\t\t\t\t\tvector<int> sa(M), sc(M);\n\t\t\t\t\t\t\t\t\tfor (int l = 0; l < M; l++) {\n\t\t\t\t\t\t\t\t\t\tint x = i + dx * l;\n\t\t\t\t\t\t\t\t\t\tint y = j + dy * l;\n\t\t\t\t\t\t\t\t\t\tint z = k + dz * l;\n\t\t\t\t\t\t\t\t\t\tif (!inside(x, y, z)) {\n\t\t\t\t\t\t\t\t\t\t\tf = false;\n\t\t\t\t\t\t\t\t\t\t\tbreak;\n\t\t\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t\t\t\tsa[l] = A[x][y][z];\n\t\t\t\t\t\t\t\t\t\tsc[l] = C[x][y][z];\n\t\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t\t\tif (sc[0] == -1) {\n\t\t\t\t\t\t\t\t\t\tf = false;\n\t\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t\t\tif (f) {\n\t\t\t\t\t\t\t\t\t\tfor (int l = 0; l < M; l++) {\n\t\t\t\t\t\t\t\t\t\t\tif (sc[l] != sc[0]) {\n\t\t\t\t\t\t\t\t\t\t\t\tf = false;\n\t\t\t\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t\t\tif (f) {\n\t\t\t\t\t\t\t\t\t\tint t = *max_element(sa.begin(), sa.end());\n\t\t\t\t\t\t\t\t\t\tans = min(ans, t);\n\t\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\tif (ans == P) {\n\t\t\tcout << \"Draw\" << endl;\n\t\t} else {\n\t\t\tcout << (ans % 2 == 0 ? \"Black\" : \"White\") << ' ' << ans + 1 << endl;\n\t\t}\n\t}\n\treturn 0;\n}", "accuracy": 1, "time_ms": 30, "memory_kb": 3464, "score_of_the_acc": -0.8725, "final_rank": 10 }, { "submission_id": "aoj_1249_9585205", "code_snippet": "#include <bits/stdc++.h>\n\nusing namespace std;\n\n#define rep(i, a, b) for (int i = (int)(a); i < (int)(b); i++)\n#define rrep(i, a, b) for (int i = (int)(b)-1; i >= (int)(a); i--)\n#define ALL(v) (v).begin(), (v).end()\n#define UNIQUE(v) sort(ALL(v)), (v).erase(unique(ALL(v)), (v).end())\n#define SZ(v) (int)v.size()\n#define MIN(v) *min_element(ALL(v))\n#define MAX(v) *max_element(ALL(v))\n#define LB(v, x) int(lower_bound(ALL(v), (x)) - (v).begin())\n#define UB(v, x) int(upper_bound(ALL(v), (x)) - (v).begin())\n\nusing uint = unsigned int;\nusing ll = long long int;\nusing ull = unsigned long long;\nusing i128 = __int128_t;\nusing u128 = __uint128_t;\nconst int inf = 0x3fffffff;\nconst ll INF = 0x1fffffffffffffff;\n\ntemplate <typename T> inline bool chmax(T &a, T b) {\n if (a < b) {\n a = b;\n return 1;\n }\n return 0;\n}\ntemplate <typename T> inline bool chmin(T &a, T b) {\n if (a > b) {\n a = b;\n return 1;\n }\n return 0;\n}\ntemplate <typename T, typename U> T ceil(T x, U y) {\n assert(y != 0);\n if (y < 0)\n x = -x, y = -y;\n return (x > 0 ? (x + y - 1) / y : x / y);\n}\ntemplate <typename T, typename U> T floor(T x, U y) {\n assert(y != 0);\n if (y < 0)\n x = -x, y = -y;\n return (x > 0 ? x / y : (x - y + 1) / y);\n}\ntemplate <typename T> int popcnt(T x) {\n return __builtin_popcountll(x);\n}\ntemplate <typename T> int topbit(T x) {\n return (x == 0 ? -1 : 63 - __builtin_clzll(x));\n}\ntemplate <typename T> int lowbit(T x) {\n return (x == 0 ? -1 : __builtin_ctzll(x));\n}\n\ntemplate <class T, class U>\nostream &operator<<(ostream &os, const pair<T, U> &p) {\n os << \"P(\" << p.first << \", \" << p.second << \")\";\n return os;\n}\ntemplate <typename T> ostream &operator<<(ostream &os, const vector<T> &vec) {\n os << \"{\";\n for (int i = 0; i < vec.size(); i++) {\n os << vec[i] << (i + 1 == vec.size() ? \"\" : \", \");\n }\n os << \"}\";\n return os;\n}\ntemplate <typename T, typename U>\nostream &operator<<(ostream &os, const map<T, U> &map_var) {\n os << \"{\";\n for (auto itr = map_var.begin(); itr != map_var.end(); itr++) {\n os << \"(\" << itr->first << \", \" << itr->second << \")\";\n itr++;\n if (itr != map_var.end())\n os << \", \";\n itr--;\n }\n os << \"}\";\n return os;\n}\ntemplate <typename T> ostream &operator<<(ostream &os, const set<T> &set_var) {\n os << \"{\";\n for (auto itr = set_var.begin(); itr != set_var.end(); itr++) {\n os << *itr;\n ++itr;\n if (itr != set_var.end())\n os << \", \";\n itr--;\n }\n os << \"}\";\n return os;\n}\n#ifdef LOCAL\n#define show(...) _show(0, #__VA_ARGS__, __VA_ARGS__)\n#else\n#define show(...) true\n#endif\ntemplate <typename T> void _show(int i, T name) {\n cerr << '\\n';\n}\ntemplate <typename T1, typename T2, typename... T3>\nvoid _show(int i, const T1 &a, const T2 &b, const T3 &...c) {\n for (; a[i] != ',' && a[i] != '\\0'; i++)\n cerr << a[i];\n cerr << \":\" << b << \" \";\n _show(i + 1, a, c...);\n}\n\n#include <unistd.h>\n\nnamespace fastio {\nstatic constexpr uint32_t SZ = 1 << 17;\nchar ibuf[SZ];\nchar obuf[SZ];\nchar out[100];\n// pointer of ibuf, obuf\n\n\nuint32_t pil = 0, pir = 0, por = 0;\n\nstruct Pre {\n char num[10000][4];\n constexpr Pre() : num() {\n for (int i = 0; i < 10000; i++) {\n int n = i;\n for (int j = 3; j >= 0; j--) {\n num[i][j] = n % 10 | '0';\n n /= 10;\n }\n }\n }\n} constexpr pre;\n\ninline void load() {\n memmove(ibuf, ibuf + pil, pir - pil);\n pir = pir - pil + fread(ibuf + pir - pil, 1, SZ - pir + pil, stdin);\n pil = 0;\n if (pir < SZ)\n ibuf[pir++] = '\\n';\n}\n\ninline void flush() {\n fwrite(obuf, 1, por, stdout);\n por = 0;\n}\n\nvoid rd(char &c) {\n do {\n if (pil + 1 > pir)\n load();\n c = ibuf[pil++];\n } while (isspace(c));\n}\n\nvoid rd(string &x) {\n x.clear();\n char c;\n do {\n if (pil + 1 > pir)\n load();\n c = ibuf[pil++];\n } while (isspace(c));\n do {\n x += c;\n if (pil == pir)\n load();\n c = ibuf[pil++];\n } while (!isspace(c));\n}\n\ntemplate <typename T> void rd_real(T &x) {\n string s;\n rd(s);\n x = stod(s);\n}\n\ntemplate <typename T> void rd_integer(T &x) {\n if (pil + 100 > pir)\n load();\n char c;\n do\n c = ibuf[pil++];\n while (c < '-');\n bool minus = 0;\n if constexpr (is_signed<T>::value || is_same_v<T, i128>) {\n if (c == '-') {\n minus = 1, c = ibuf[pil++];\n }\n }\n x = 0;\n while ('0' <= c) {\n x = x * 10 + (c & 15), c = ibuf[pil++];\n }\n if constexpr (is_signed<T>::value || is_same_v<T, i128>) {\n if (minus)\n x = -x;\n }\n}\n\nvoid rd(int &x) {\n rd_integer(x);\n}\nvoid rd(ll &x) {\n rd_integer(x);\n}\nvoid rd(i128 &x) {\n rd_integer(x);\n}\nvoid rd(uint &x) {\n rd_integer(x);\n}\nvoid rd(ull &x) {\n rd_integer(x);\n}\nvoid rd(u128 &x) {\n rd_integer(x);\n}\nvoid rd(double &x) {\n rd_real(x);\n}\nvoid rd(long double &x) {\n rd_real(x);\n}\n\ntemplate <class T, class U> void rd(pair<T, U> &p) {\n return rd(p.first), rd(p.second);\n}\ntemplate <size_t N = 0, typename T> void rd_tuple(T &t) {\n if constexpr (N < std::tuple_size<T>::value) {\n auto &x = std::get<N>(t);\n rd(x);\n rd_tuple<N + 1>(t);\n }\n}\ntemplate <class... T> void rd(tuple<T...> &tpl) {\n rd_tuple(tpl);\n}\n\ntemplate <size_t N = 0, typename T> void rd(array<T, N> &x) {\n for (auto &d : x)\n rd(d);\n}\ntemplate <class T> void rd(vector<T> &x) {\n for (auto &d : x)\n rd(d);\n}\n\nvoid read() {}\ntemplate <class H, class... T> void read(H &h, T &...t) {\n rd(h), read(t...);\n}\n\nvoid wt(const char c) {\n if (por == SZ)\n flush();\n obuf[por++] = c;\n}\nvoid wt(const string s) {\n for (char c : s)\n wt(c);\n}\nvoid wt(const char *s) {\n size_t len = strlen(s);\n for (size_t i = 0; i < len; i++)\n wt(s[i]);\n}\n\ntemplate <typename T> void wt_integer(T x) {\n if (por > SZ - 100)\n flush();\n if (x < 0) {\n obuf[por++] = '-', x = -x;\n }\n int outi;\n for (outi = 96; x >= 10000; outi -= 4) {\n memcpy(out + outi, pre.num[x % 10000], 4);\n x /= 10000;\n }\n if (x >= 1000) {\n memcpy(obuf + por, pre.num[x], 4);\n por += 4;\n } else if (x >= 100) {\n memcpy(obuf + por, pre.num[x] + 1, 3);\n por += 3;\n } else if (x >= 10) {\n int q = (x * 103) >> 10;\n obuf[por] = q | '0';\n obuf[por + 1] = (x - q * 10) | '0';\n por += 2;\n } else\n obuf[por++] = x | '0';\n memcpy(obuf + por, out + outi + 4, 96 - outi);\n por += 96 - outi;\n}\n\ntemplate <typename T> void wt_real(T x) {\n ostringstream oss;\n oss << fixed << setprecision(15) << double(x);\n string s = oss.str();\n wt(s);\n}\n\nvoid wt(int x) {\n wt_integer(x);\n}\nvoid wt(ll x) {\n wt_integer(x);\n}\nvoid wt(i128 x) {\n wt_integer(x);\n}\nvoid wt(uint x) {\n wt_integer(x);\n}\nvoid wt(ull x) {\n wt_integer(x);\n}\nvoid wt(u128 x) {\n wt_integer(x);\n}\nvoid wt(double x) {\n wt_real(x);\n}\nvoid wt(long double x) {\n wt_real(x);\n}\n\ntemplate <class T, class U> void wt(const pair<T, U> val) {\n wt(val.first);\n wt(' ');\n wt(val.second);\n}\ntemplate <size_t N = 0, typename T> void wt_tuple(const T t) {\n if constexpr (N < std::tuple_size<T>::value) {\n if constexpr (N > 0) {\n wt(' ');\n }\n const auto x = std::get<N>(t);\n wt(x);\n wt_tuple<N + 1>(t);\n }\n}\ntemplate <class... T> void wt(tuple<T...> tpl) {\n wt_tuple(tpl);\n}\ntemplate <class T, size_t S> void wt(const array<T, S> val) {\n auto n = val.size();\n for (size_t i = 0; i < n; i++) {\n if (i)\n wt(' ');\n wt(val[i]);\n }\n}\ntemplate <class T> void wt(const vector<T> val) {\n auto n = val.size();\n for (size_t i = 0; i < n; i++) {\n if (i)\n wt(' ');\n wt(val[i]);\n }\n}\n\nvoid print() {\n wt('\\n');\n}\ntemplate <class Head, class... Tail> void print(Head &&head, Tail &&...tail) {\n wt(head);\n if (sizeof...(Tail))\n wt(' ');\n print(forward<Tail>(tail)...);\n}\nvoid __attribute__((destructor)) _d() {\n flush();\n}\n} // namespace fastio\n\n\nusing fastio::flush;\nusing fastio::print;\nusing fastio::read;\n\ninline void first(bool i = true) {\n print(i ? \"first\" : \"second\");\n}\ninline void Alice(bool i = true) {\n print(i ? \"Alice\" : \"Bob\");\n}\ninline void Takahashi(bool i = true) {\n print(i ? \"Takahashi\" : \"Aoki\");\n}\ninline void yes(bool i = true) {\n print(i ? \"yes\" : \"no\");\n}\ninline void Yes(bool i = true) {\n print(i ? \"Yes\" : \"No\");\n}\ninline void No() {\n print(\"No\");\n}\ninline void YES(bool i = true) {\n print(i ? \"YES\" : \"NO\");\n}\ninline void NO() {\n print(\"NO\");\n}\ninline void Yay(bool i = true) {\n print(i ? \"Yay!\" : \":(\");\n}\ninline void Possible(bool i = true) {\n print(i ? \"Possible\" : \"Impossible\");\n}\ninline void POSSIBLE(bool i = true) {\n print(i ? \"POSSIBLE\" : \"IMPOSSIBLE\");\n}\n\n/**\n * @brief Fast IO\n */\n\nvoid Solve(int N, int M, int P, vector<pair<int,int>>& A) {\n vector X(N, vector(N, vector<int>(N,-1)));\n vector H(N, vector<int>(N,0));\n rep(_,0,P) {\n int B = _ % 2;\n X[A[_].first][A[_].second][H[A[_].first][A[_].second]] = B;\n H[A[_].first][A[_].second]++;\n rep(i,0,N) {\n rep(j,0,N) {\n rep(k,0,N) {\n rep(di,-1,2) {\n rep(dj,-1,2) {\n rep(dk,-1,2) {\n if (di == 0 && dj == 0 && dk == 0) break;\n bool check = true;\n rep(l,0,M) {\n int ni = i + di * l;\n int nj = j + dj * l;\n int nk = k + dk * l;\n if (ni < 0 || nj < 0 || nk < 0 || ni >= N || nj >= N || nk >= N) {\n check = false;\n break;\n }\n if (X[ni][nj][nk] != B) {\n check = false;\n break;\n }\n }\n if (check) {\n cout << (B == 0 ? \"Black\" : \"White\") << ' ' << _ + 1 << endl;\n return;\n }\n }\n }\n }\n }\n }\n }\n }\n cout << \"Draw\" << endl;\n return;\n}\n\nint main() {\nwhile(1) {\n int N, M, P;\n cin >> N >> M >> P;\n if (N == 0) return 0;\n vector<pair<int,int>> A(P);\n rep(i,0,P) cin >> A[i].first >> A[i].second, A[i].first--, A[i].second--;\n Solve(N,M,P,A);\n}\n}", "accuracy": 1, "time_ms": 240, "memory_kb": 3516, "score_of_the_acc": -1.1237, "final_rank": 16 }, { "submission_id": "aoj_1249_8378191", "code_snippet": "#include <iostream>\n#include <vector>\nusing namespace std;\n#define ALL(v) begin(v),end(v)\n\nint N,M,P;\n\nbool check(const vector<vector<vector<int>>>& board, int side) {\n for (int sx = 0; sx < N; ++sx) {\n for (int sy = 0; sy < N; ++sy) {\n for (int sz = 0; sz < N; ++sz) {\n for (int dx = -1; dx < 2; ++dx) {\n for (int dy = -1; dy < 2; ++dy) {\n for (int dz = -1; dz < 2; ++dz) {\n if (dx==0 and dy==0 and dz==0) continue;\n bool ok = true;\n for (int i = 0; i < M; ++i) {\n int x = sx + dx * i;\n int y = sy + dy * i;\n int z = sz + dz * i;\n if (0 <= x && x < N &&\n 0 <= y && y < N &&\n 0 <= z && z < N &&\n board[z][y][x] == side) {\n //\n } else {\n ok = false;\n break;\n }\n }\n if (ok) return true;\n }\n }\n }\n }\n }\n }\n return false;\n}\n\nint main(void) {\n while (true) {\n cin >> N >> M >> P;\n if (N == 0 and M == 0 and P == 0) break;\n \n vector<pair<int, int>> hand;\n for (int i = 0; i < P; ++i) {\n int x, y;\n cin >> x >> y;\n --x; --y;\n hand.emplace_back(x, y);\n }\n \n vector<vector<vector<int>>> board(N, vector<vector<int>>(N, vector<int>(N, -1)));\n vector<vector<int>> next(N, vector<int>(N, 0));\n int side = 0;\n bool decided = false;\n for (int i = 0; i < P; ++i) {\n auto [x, y] = hand[i];\n int z = next[y][x];\n board[z][y][x] = side;\n next[y][x]++;\n \n if (check(board, side)) {\n cout << (side ? \"White\" : \"Black\") << ' ' << i + 1 << endl;\n decided = true;\n break;\n }\n side = 1 - side;\n }\n if (!decided) cout << \"Draw\" << endl;\n }\n \n}", "accuracy": 1, "time_ms": 290, "memory_kb": 3140, "score_of_the_acc": -0.1505, "final_rank": 1 }, { "submission_id": "aoj_1249_7848708", "code_snippet": "#include <bits/stdc++.h>\n#define LL_INF 9223372036854775807\n#define INT_INF 2147483647\nusing namespace std;\nusing ll = long long;\n\nint main()\n{\n while (true)\n {\n int n, m, p;\n cin >> n >> m >> p;\n if (n == 0 and m == 0 and p == 0)\n {\n return 0;\n }\n vector<pair<int, int>> v(p);\n for (pair<int, int> &i : v)\n {\n cin >> i.first >> i.second;\n i.first--;\n i.second--;\n }\n vector<vector<vector<int>>> board(n, vector<vector<int>>(n, vector<int>(n, 2)));\n vector<vector<int>> dir = {{1, 1, -1}, {0, 1, -1}, {-1, 1, -1}, {1, 0, -1}, {0, 0, -1}, {-1, 0, -1}, {-1, -1, -1}, {0, -1, -1}, {1, -1, -1}, {1, 1, 0}, {0, 1, 0}, {-1, 1, 0}, {1, 0, 0}, {-1, 0, 0}, {-1, -1, 0}, {0, -1, 0}, {1, -1, 0}, {1, 1, 1}, {0, 1, 1}, {-1, 1, 1}, {1, 0, 1}, {0, 0, 1}, {-1, 0, 1}, {-1, -1, 1}, {0, -1, 1}, {1, -1, 1}};\n int x, y, z;\n bool ok;\n for (int i = 0; i < p; i++)\n {\n x = v[i].first;\n y = v[i].second;\n ok = false;\n for (int j = 0; j < p; j++)\n {\n if (board[x][y][j] == 2)\n {\n board[x][y][j] = i % 2;\n z = j;\n break;\n }\n }\n\n for (vector<int> j : dir)\n {\n int cnt = 1, n_x = x, n_y = y, n_z = z;\n while (0 <= (n_x + j[0]) and (n_x + j[0]) < n and 0 <= (n_y + j[1]) and (n_y + j[1]) < n and 0 <= (n_z + j[2]) and (n_z + j[2]) < n)\n {\n n_x += j[0];\n n_y += j[1];\n n_z += j[2];\n if (board[x][y][z] != board[n_x][n_y][n_z])\n {\n n_x -= j[0];\n n_y -= j[1];\n n_z -= j[2];\n break;\n }\n }\n while (0 <= (n_x - j[0]) and (n_x - j[0]) < n and 0 <= (n_y - j[1]) and (n_y - j[1]) < n and 0 <= (n_z - j[2]) and (n_z - j[2]) < n)\n {\n n_x -= j[0];\n n_y -= j[1];\n n_z -= j[2];\n if (board[x][y][z] == board[n_x][n_y][n_z])\n {\n cnt++;\n }\n else\n {\n break;\n }\n }\n if (m <= cnt)\n {\n if (board[x][y][z] == 0)\n {\n cout << \"Black \";\n }\n else\n {\n cout << \"White \";\n }\n cout << i + 1 << endl;\n ok = true;\n break;\n }\n if (ok)\n {\n break;\n }\n }\n if (ok)\n {\n break;\n }\n }\n if (!ok)\n {\n cout << \"Draw\" << endl;\n }\n }\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3204, "score_of_the_acc": -0.1702, "final_rank": 2 }, { "submission_id": "aoj_1249_7848657", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\nvector<pair<int, int>> rle(const vector<int>& A) {\n vector<pair<int, int>> res{};\n for (int i = 0 ; i < (int)A.size() ; i++) {\n if (res.empty() or res.back().first != A[i]) {\n res.push_back({ A[i], 1 });\n }\n else {\n res.back().second++;\n }\n }\n return res;\n}\n\n\nbool solve() {\n int N, M, P; cin >> N >> M >> P;\n if (N == 0) return false;\n vector<vector<vector<int>>> table(N, vector(N, vector(N, 0)));\n using pos = tuple<int, int, int>;\n vector<int> d = { -1, 0, 1 };\n string ans{};\n\n for (int i = 0 ; i < P ; i++) {\n int nowcol = (i % 2 == 0 ? 1 : 2);\n int x, y; cin >> x >> y;\n x--; y--;\n pos put{ -1, -1, -1 };\n for (int j = 0 ; j < N ; j++) {\n if (table[x][y][j] == 0) {\n table[x][y][j] = nowcol;\n put = pos{ x, y, j };\n break;\n }\n }\n assert(get<0>(put) != -1);\n auto [sx, sy, sz] = put;\n bool win = false;\n\n auto in = [&](int x, int y, int z) -> bool {\n return 0 <= x and x < N and 0 <= y and y < N and 0 <= z and z < N;\n };\n\n for (auto dx : d) for (auto dy : d) for (auto dz : d) {\n if (dx == 0 and dy == 0 and dz == 0) continue;\n vector<int> arr{};\n for (int j = -10 ; j < 10 ; j++) {\n int x = sx + j * dx, y = sy + j * dy, z = sz + j * dz;\n if (!in(x, y, z)) continue;\n arr.push_back(table[x][y][z]);\n for (auto [col, cnt] : rle(arr)) {\n if (col == nowcol and cnt >= M) win = true;\n }\n }\n }\n\n \n if (win and ans.empty()) {\n ans = string((nowcol == 1 ? \"Black\" : \"White\")) + string(\" \") + to_string(i + 1);\n }\n }\n if (ans.empty()) ans = \"Draw\";\n cout << ans << endl;\n return true;\n}\n\nint main() {\n while (solve()) ;\n}", "accuracy": 1, "time_ms": 220, "memory_kb": 3216, "score_of_the_acc": -0.315, "final_rank": 3 }, { "submission_id": "aoj_1249_7848174", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\nvoid fall(vector<vector<vector<int>>> &pegs, int color, pair<int, int> p) {\n auto [x, y] = p;\n int z = pegs[y][x].size() - 1;\n pegs[y][x][z] = color;\n z--;\n while (z >= 0 && pegs[y][x][z] == 0) {\n swap(pegs[y][x][z], pegs[y][x][z + 1]);\n z--;\n }\n}\n\nbool win(vector<vector<vector<int>>> &pegs, int color, int m) {\n int n = pegs.size();\n const int dy[] = {-1, -1, -1, -1, -1, -1, -1, -1, -1, /*9*/ 0, 0, 0, 0, 0, 0, 0, 0, 0, /*18*/ 1, 1, 1, 1, 1, 1, 1, 1, 1};\n const int dx[] = {-1, -1, -1, 0, 0, 0, 1, 1, 1, /*9*/ -1, -1, -1, 0, 0, 0, 1, 1, 1, /*18*/ 0, 0, 0, 0, 0, 0, 0, 0, 0};\n const int dz[] = {-1, 0, 1, -1, 0, 1, -1, 0, 1, /*9*/ -1, 0, 1, -1, 0, 1, -1, 0, 1, /*18*/ -1, 0, 1, 0, -1, 1, -1, 0, 1};\n auto in_ranges = [&](int a) {\n return 0 <= a && a < n;\n };\n\n for (int k = 0; k < 27; k++) {\n if (dy[k] == 0 && dx[k] == 0 && dz[k] == 0) continue;\n vector len(n, vector(n, vector(n, 0)));\n for (int y = 0; y < n; y++) {\n for (int x = 0; x < n; x++) {\n for (int z = 0; z < n; z++) {\n auto f = [&](auto &&f, int y, int x, int z, int k) {\n // cout << x << ' ' << y << ' ' << z << endl;\n if (!in_ranges(y) || !in_ranges(x) || !in_ranges(z) || pegs[y][x][z] != color) return 0;\n else if (len[y][x][z] > 0) return len[y][x][z];\n\n int ny = y + dy[k];\n int nx = x + dx[k];\n int nz = z + dz[k];\n\n return len[y][x][z] = f(f, ny, nx, nz, k) + 1;\n };\n\n if (f(f, y, x, z, k) >= m)\n return true;\n }\n }\n }\n }\n\n return false;\n}\n\nint main() {\n int n, m, p;\n\n while (cin >> n >> m >> p, n > 0) {\n vector pegs(n, vector(n, vector(n, 0))); // 0: empty, 1: black, 2: white\n vector balls(p, make_pair(0, 0));\n for (auto &a: balls) {\n cin >> a.first >> a.second;\n a.first--, a.second--;\n }\n\n int winner = 0;\n int moves = 0;\n for (int i = 0; i < p; i++) {\n if (i & 1) { \n fall(pegs, 2, balls[i]);\n if (win(pegs, 2, m)) {\n winner = 2;\n moves = i;\n break;\n }\n }\n else {\n fall(pegs, 1, balls[i]);\n if (win(pegs, 1, m)) {\n winner = 1;\n moves = i;\n break;\n }\n }\n }\n\n if (winner == 0) {\n cout << \"Draw\" << endl;\n }\n else if (winner == 1) {\n cout << \"Black\" << ' ' << moves + 1 << endl;\n }\n else {\n cout << \"White\" << ' ' << moves + 1 << endl;\n }\n }\n}", "accuracy": 1, "time_ms": 560, "memory_kb": 3472, "score_of_the_acc": -1.1787, "final_rank": 18 }, { "submission_id": "aoj_1249_7236010", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n#define foa(s, v) for(auto &s : v)\n#define all(v) v.begin(), v.end()\n#define REPname(a,b,c,d,...) d\n#define rep(...) REPname(__VA_ARGS__, REP2, REP1, REP0)(__VA_ARGS__)\n#define REP2(i,l,r) for(int i = l; i < r; i++)\n#define REP1(i, x) REP2(i,0,x)\n#define REP0(x) REP1(SPJ, x)\n#define sz(x) int(x.size())\n\ntemplate <class T>\nusing V=vector<T>;\n\ntemplate <class T>\nusing VV=vector<V<T>>;\n\ntemplate<class T>\nusing pqmin = priority_queue<T, V<T>, greater<T>>;\nusing ll = long long ;\nusing vi = vector<int>;\nusing vvi = vector<vi>;\nusing vvvi = vector<vvi>;\nusing vll = vector<ll>;\nusing vvll = V<vll>;\nconstexpr ll INF = 1e18;\nconstexpr int inf = 1e9;\ntemplate<class T>\ninline bool chmax(T &a, T b){\n\treturn a < b ? a=b, 1 : 0;\n}\ntemplate <class T>\ninline bool chmin(T &a, T b) {\n\treturn a > b ? a = b, 1 : 0;\n}\n\ntemplate <class T>\nvoid view(T x) {\n\tcerr << x;\n}\n\ntemplate <class T>\nvoid view(V<T> v) {\n\tcerr << \"{ \";\n\tfoa(t, v) {view(t) ; cerr << \", \";}\n\tcerr << \"}\";\n\tcerr << endl;\n}\n\n\ntemplate <class T>\nvoid view(VV<T> v) {\n\tcerr << \"{ \";\n\tfoa(t, v) {view(t) ; cerr << \",\\n\";}\n\tcerr << \"}\";\n\tcerr << endl;\n}\n\n\n// template <c0lass T>\nvoid view(int x) {\n\tcerr << x;\n}\n\n#ifdef LOCAL\nvoid debug_out() {}\ntemplate <class T>\nvoid debug_out(T x) {\n\tview(x);\n}\ntemplate <class H, class... T>\nvoid debug_out(H h, T... t) {\n\tview(h);\n\tcerr << \", \";\n\tdebug_out(t...);\n}\n#define debug(...) \\\n\tdo { \\\n\t\tcerr << __LINE__ << \" [\" << #__VA_ARGS__ << \"] : [\"; \\\n\t\tdebug_out(__VA_ARGS__); \\\n\t\tcerr << \"]\\n\"; \\\n\t} while(0)\n#else\n#define debug(...) (void(0))\n#endif\nusing vvi = V<vi>;\n\nstruct uf{\n\tvector<int> dat;\n\tuf(int n) : dat(n, -1) {}\n\tint root(int x) \n\t{\n\t\tint& p = dat[x];\n\t\tif(p < 0) return x;\n\t\treturn p = root(p);\n\t}\n\tbool merge(int x, int y) {\n\t\tx = root(x);\n\t\ty = root(y);\n\t\tif(x == y) return false;\n\t\tif(-dat[x] < -dat[y]) swap(x, y);\n\t\tdat[x] += dat[y];\n\t\tdat[y] = x;\n\t\treturn true;\n\t}\n};\n\nll modpow(ll x, ll n, ll md){\n\tll ret = 1 % md;\n\twhile(n > 0){\n\t\tif(n & 1) {ret *= x; ret %= md;}\n\t\tn >>= 1;\n\t\tx *= x;\n\t\tx %= md;\n\t}\n\tif(ret < 0) ret += abs(md);\n\treturn ret;\n}\n\nusing S = ll;\nusing F = ll; // +\nconstexpr ll e = INF;\nconstexpr ll id = 0LL;\nconstexpr ll replace_e = e;\nll op(ll a, ll b) {return min(a,b);}\nll mapping(F f, S s) {return S(f+s);}\nll composition(F f, F g) {return f+g;}\nstruct node {\n\tnode *l = nullptr;\n\tnode *r = nullptr;\n\tint lo, hi;\n\tll mset = e;\n\tll madd = id;\n\tll val = e;\n\tnode(int lo, int hi) : lo(lo), hi(hi) {}\n\tnode (vll& v, int lo, int hi) : lo(lo), hi(hi) {\n\t\tif(lo + 1 < hi) {\n\t\t\tint mid = lo + (hi - lo) / 2;\n\t\t\tl = new node (v, lo, mid);\n\t\t\tr = new node(v, mid, hi);\n\t\t\tval = op(l->val, r->val);\n\t\t}\n\t\telse {\n\t\t\tval = v[lo];\n\t\t}\n\t}\n\n\tS query(int L, int R) {\n\t\tif(R <= lo || hi <= L) return e;\n\t\tif(L <=lo && hi <= R) return val;\n\t\tpush();\n\t\treturn op(l->query(L, R), r->query(L,R));\n\t}\n\n\tvoid set(int L, int R, ll x) {\n\t\tif(R <= lo || hi <= L) return;\n\t\tif(L <= lo && hi <= R) mset = val = x, madd = id;\n\t\telse {\n\t\t\tpush(), l->set (L, R, x) , r->set(L,R,x);\n\t\t\tval = op(l->val, r->val);\n\t\t}\n\t}\n\n\tvoid add(int L, int R, ll x) {\n\t\tif(R <= lo || hi <= L) return;\n\t\tif(L <= lo && hi <= R) {\n\t\t\tif(mset != replace_e) mset = mapping(x, mset);\n\t\t\telse madd = composition(x, madd);\n\t\t\tval = mapping(x, val);\n\t\t} else {\n\t\t\tpush(), l->add(L, R, x), r->add(L, R, x);\n\t\t\tval = op(l->val, r->val);\n\t\t}\n\t}\n\n\tvoid push() {\n\t\tif(!l) {\n\t\t\tint mid = lo + (hi - lo) / 2;\n\t\t\tl = new node(lo, mid);\n\t\t\tr = new node(mid, hi);\n\t\t}\n\t\tif(mset != replace_e)\n\t\t\tl->set(lo, hi, mset), r->set(lo, hi, mset), mset = replace_e;\n\t\telse if(madd)\n\t\t\tl->add(lo, hi, madd), r->add(lo, hi, madd), madd = id;\n\t}\n};\n\nint solve(int n){ // first : 1, second : -1, draw : 0\n\tint win; cin >> win;\n\tint p; cin >> p;\n\tvector<vi> nxt(n, vi(n));\n\tvector brd(n, vector(n, vector<int>(n, 0)));\n\tconst int fst = 1;\n\tconst int snd = -1;\n\tV<pair<int, int>> hands;\n\trep(p) {\n\t\tint x, y; cin >>x >> y;\n\t\tx--;\n\t\ty--;\n\t\thands.emplace_back(x, y);\n\t}\n\n\tauto in = [&n](int x) { return 0 <= x && x < n ;};\n\n\tauto beam = [&](int sx, int sy, int sz, int mx, int my, int mz) {\n\t\tint plr = brd[sx].at(sy).at(sz);\n\t\tif(plr == 0) return 0;\n\t\trep(win-1) {\n\t\t\tsx += mx;\n\t\t\tsy += my;\n\t\t\tsz += mz;\n\t\t\tif(!in(sx)) return 0;\n\t\t\tif(!in(sy)) return 0;\n\t\t\tif(!in(sz)) return 0;\n\t\t\tif(plr != brd.at(sx).at(sy).at(sz)) return 0;\n\t\t}\n\n\t\treturn plr;\n\t};\n\n\tauto judge = [&]() -> int {\n\t\trep(i, n) rep(j, n) rep(k, n) {\n\t\t\trep(dx, -1, 2) rep(dy, -1, 2) rep(dz, -1, 2) {\n\t\t\t\tif(dx == 0 && dy == 0 && dz == 0) continue;\n\t\t\t\tint res = beam(i,j,k,dx, dy, dz);\n\t\t\t\tif(res) return res;\n\t\t\t}\n\t\t}\n\n\t\treturn 0;\n\t};\n\n\tint now = fst;\n\trep(i, int(hands.size())){\n\t\tint x, y;\n\t\ttie(x, y) = hands.at(i);\n\t\tint& pos = nxt[x][y];\n\t\tbrd[x][y][pos] = now;\n\t\tpos++;\n\t\tint res = judge();\n\t\tif(res) {\n\t\t\treturn i + 1;\n\t\t}\n\t\tnow = -now;\n\t}\n\n\treturn 0;\n}\n\nint main() {\n\tios::sync_with_stdio(false);\n\tcin.tie(nullptr);\n\tcout << fixed << setprecision(15);\n\tsrand((unsigned)time(NULL));\n\tint n;\n\twhile(cin >> n && n) {\n\t\tint res = solve(n);\n\t\tif(res == 0) cout << \"Draw\\n\";\n\t\telse {\n\t\t\tif(res & 1) {\n\t\t\t\tcout << \"Black \";\n\t\t\t}else {\n\t\t\t\tcout << \"White \";\n\t\t\t}\n\t\t\tcout << res << '\\n';\n\t\t}\n\t}\n}", "accuracy": 1, "time_ms": 250, "memory_kb": 3412, "score_of_the_acc": -0.8524, "final_rank": 9 }, { "submission_id": "aoj_1249_6977788", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\ntypedef long long ll;\ntypedef pair<ll,ll> pll;\ntypedef vector<ll> vll;\ntypedef vector<pll> vpll;\n#define mp make_pair\n#define pb push_back\n#define rep(i,n) for(int i=0;i<n;i++)\n#define per(i,n) for(int i=n-1;i>=0;i--)\n#define rrep(i,l,r) for(int i=l;i<=r;i++)\n#define chmin(a,b) a=min(a,b)\n#define chmax(a,b) a=max(a,b)\n#define all(x) x.begin(),x.end()\n#define unq(x) sort(all(x));x.erase(unique(all(x)),x.end())\n//#define mod 1000000007\n#define mod 998244353\n//ll mod;\n#define ad(a,b) a=(a+b)%mod;\n#define mul(a,b) a=a*b%mod;\nvoid readv(vector<ll> &a,ll n){\n\trep(i,n){\n\t\tll x;\n\t\tcin>>x;\n\t\ta.push_back(x);\n\t}\n}\nvoid outv(vector<ll> &a,ll n){\n\trep(i,n){\n\t\tif(i>0)cout<<\" \";\n\t\tcout<<a[i];\n\t}\n\tcout<<\"\\n\";\n}\nll po(ll x,ll y){\n\tll res=1;\n\tfor(;y;y>>=1){\n\t\tif(y&1)res=res*x%mod;\n\t\tx=x*x%mod;\n\t}\n\treturn res;\n}\nll gcd(ll a,ll b){\n\treturn (b?gcd(b,a%b):a);\n}\n#define FACMAX 200010\nll fac[FACMAX],inv[FACMAX],ivf[FACMAX];\nvoid initfac(){\n\tfac[0]=ivf[0]=inv[1]=1;\n\tfor(ll i=1;i<FACMAX;i++)fac[i]=fac[i-1]*i%mod;\n\tfor(ll i=1;i<FACMAX;i++){\n\t\tif(i>1)inv[i]=(mod-mod/i*inv[mod%i]%mod)%mod;\n\t\tivf[i]=po(fac[i],mod-2);\n\t}\n}\nll P(ll n,ll k){\n\tif(n<0||n<k)return 0;\n\treturn fac[n]*ivf[n-k]%mod;\n}\nll C(ll n,ll k){\n\tif(k<0||n<k)return 0;\n\treturn fac[n]*ivf[n-k]%mod*ivf[k]%mod;\n}\nll H(ll n,ll k){\n\treturn C(n+k-1,k);\n}\n\nll n,m,p;\nll x[400],y[400];\nll dx[]={1,0,0,1,1,1,1,0,0,1,1,1,1};\nll dy[]={0,1,0,1,-1,0,0,1,1,1,1,-1,-1};\nll dz[]={0,0,1,0,0,1,-1,1,-1,1,-1,1,-1};\n\nvoid solve(){\n\tvector<bool> v[7][7];\n\trep(u,p){\n\t\tv[x[u]][y[u]].pb(u%2);\n\t\trep(X,n)rep(Y,n)rep(Z,n){\n\t\t\trep(s,13){\n\t\t\t\tll cnt[2]={0,0};\n\t\t\t\trep(i,m){\n\t\t\t\t\tll Xi=X+dx[s]*i;\n\t\t\t\t\tll Yi=Y+dy[s]*i;\n\t\t\t\t\tll Zi=Z+dz[s]*i;\n\t\t\t\t\tif(!(0<=Xi&&Xi<n))break;\n\t\t\t\t\tif(!(0<=Yi&&Yi<n))break;\n\t\t\t\t\tif(!(0<=Zi&&Zi<v[Xi][Yi].size()))break;\n\t\t\t\t\tcnt[v[Xi][Yi][Zi]]++;\n\t\t\t\t}\n\t\t\t\tif(cnt[0]==m){\n\t\t\t\t\tcout<<\"Black \"<<u+1<<endl;\n\t\t\t\t\treturn;\n\t\t\t\t}\n\t\t\t\tif(cnt[1]==m){\n\t\t\t\t\tcout<<\"White \"<<u+1<<endl;\n\t\t\t\t\treturn;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n\tcout<<\"Draw\"<<endl;\n}\nint main(){\n\tcin.tie(0);\n\tios::sync_with_stdio(0);\n\twhile(1){\n\t\tcin>>n>>m>>p;\n\t\tif(n==0&&m==0&&p==0)break;\n\t\trep(i,p){\n\t\t\tcin>>x[i]>>y[i];\n\t\t\tx[i]--,y[i]--;\n\t\t}\n\t\tsolve();\n\t}\n}", "accuracy": 1, "time_ms": 170, "memory_kb": 3452, "score_of_the_acc": -0.9158, "final_rank": 12 }, { "submission_id": "aoj_1249_6569043", "code_snippet": "#line 2 \"library/KowerKoint/base.hpp\"\n\n#ifndef ONLINE_JUDGE\n#define _GLIBCXX_DEBUG\n#endif\n\n#include <bits/stdc++.h>\nusing namespace std;\n\n#define REP(i, n) for(int i = 0; i < (int)(n); i++)\n#define FOR(i, a, b) for(ll i = a; i < (ll)(b); i++)\n#define ALL(a) (a).begin(),(a).end()\n#define END(...) { print(__VA_ARGS__); return; }\n\nusing VI = vector<int>;\nusing VVI = vector<VI>;\nusing VVVI = vector<VVI>;\nusing ll = long long;\nusing VL = vector<ll>;\nusing VVL = vector<VL>;\nusing VVVL = vector<VVL>;\nusing VD = vector<double>;\nusing VVD = vector<VD>;\nusing VVVD = vector<VVD>;\nusing VS = vector<string>;\nusing VVS = vector<VS>;\nusing VVVS = vector<VVS>;\nusing VC = vector<char>;\nusing VVC = vector<VC>;\nusing VVVC = vector<VVC>;\nusing P = pair<int, int>;\nusing VP = vector<P>;\nusing VVP = vector<VP>;\nusing VVVP = vector<VVP>;\nusing LP = pair<ll, ll>;\nusing VLP = vector<LP>;\nusing VVLP = vector<VLP>;\nusing VVVLP = vector<VVLP>;\n\ntemplate <typename T>\nusing PQ = priority_queue<T>;\ntemplate <typename T>\nusing GPQ = priority_queue<T, vector<T>, greater<T>>;\n\nconstexpr int INF = 1001001001;\nconstexpr ll LINF = 1001001001001001001ll;\nconstexpr int DX[] = {1, 0, -1, 0};\nconstexpr int DY[] = {0, 1, 0, -1};\n\nvoid print() { cout << '\\n'; }\ntemplate<typename T>\nvoid print(const T &t) { cout << t << '\\n'; }\ntemplate<typename Head, typename... Tail>\nvoid print(const Head &head, const Tail &... tail) {\n cout << head << ' ';\n print(tail...);\n}\n\n#ifdef ONLINE_JUDGE\ntemplate<typename... Args>\nvoid dbg(const Args &... args) {}\n#else\nvoid dbg() { cerr << '\\n'; }\ntemplate<typename T>\nvoid dbg(const T &t) { cerr << t << '\\n'; }\ntemplate<typename Head, typename... Tail>\nvoid dbg(const Head &head, const Tail &... tail) {\n cerr << head << ' ';\n dbg(tail...);\n}\n#endif\n\ntemplate< typename T1, typename T2 >\nostream &operator<<(ostream &os, const pair< T1, T2 >& p) {\n os << p.first << \" \" << p.second;\n return os;\n}\n\ntemplate< typename T1, typename T2 >\nistream &operator>>(istream &is, pair< T1, T2 > &p) {\n is >> p.first >> p.second;\n return is;\n}\n\ntemplate< typename T >\nostream &operator<<(ostream &os, const vector< T > &v) {\n for(int i = 0; i < (int) v.size(); i++) {\n os << v[i] << (i + 1 != (int) v.size() ? \" \" : \"\");\n }\n return os;\n}\n\ntemplate< typename T >\nistream &operator>>(istream &is, vector< T > &v) {\n for(T &in : v) is >> in;\n return is;\n}\n\ntemplate<typename T>\nvector<vector<T>> split(typename vector<T>::const_iterator begin, typename vector<T>::const_iterator end, T val) {\n vector<vector<T>> res;\n vector<T> cur;\n for(auto it = begin; it != end; it++) {\n if(*it == val) {\n res.push_back(cur);\n cur.clear();\n } else cur.push_back(val);\n }\n res.push_back(cur);\n return res;\n}\n\nvector<string> split(typename string::const_iterator begin, typename string::const_iterator end, char val) {\n vector<string> res;\n string cur = \"\";\n for(auto it = begin; it != end; it++) {\n if(*it == val) {\n res.push_back(cur);\n cur.clear();\n } else cur.push_back(val);\n }\n res.push_back(cur);\n return res;\n}\n\ntemplate< typename T1, typename T2 >\ninline bool chmax(T1 &a, T2 b) { return a < b && (a = b, true); }\n\ntemplate< typename T1, typename T2 >\ninline bool chmin(T1 &a, T2 b) { return a > b && (a = b, true); }\n\ntemplate <typename T>\npair<VI, vector<T>> compress(const vector<T> &a) {\n int n = a.size();\n vector<T> x;\n REP(i, n) x.push_back(a[i]);\n sort(ALL(x)); x.erase(unique(ALL(x)), x.end());\n VI res(n);\n REP(i, n) res[i] = lower_bound(ALL(x), a[i]) - x.begin();\n return make_pair(res, x);\n}\n\ntemplate <typename T>\npair<vector<T>, vector<T>> factorial(int n) {\n vector<T> res(n+1), rev(n+1);\n res[0] = 1;\n REP(i, n) res[i+1] = res[i] * (i+1);\n rev[n] = 1 / res[n];\n for(int i = n; i > 0; i--) {\n rev[i-1] = rev[i] * i;\n }\n return make_pair(res, rev);\n}\n#line 3 \"library/KowerKoint/internal_operator.hpp\"\n\nnamespace internal_operator {\n template <typename T>\n T default_add(T a, T b) { return a + b; }\n template <typename T>\n T default_sub(T a, T b) { return a - b; }\n template <typename T>\n T zero() { return T(0); }\n template <typename T>\n T default_div(T a, T b) { return a / b; }\n template <typename T>\n T default_mult(T a, T b) { return a * b; }\n template <typename T>\n T one() { return T(1); }\n template <typename T>\n T default_xor(T a, T b) { return a ^ b; }\n template <typename T>\n T default_and(T a, T b) { return a & b; }\n template <typename T>\n T default_or(T a, T b) { return a | b; }\n ll mod3() { return 998244353LL; }\n ll mod7() { return 1000000007LL; }\n ll mod9() { return 1000000009LL; }\n}\n\n#line 3 \"library/KowerKoint/integer.hpp\"\n\nVL divisor(ll n) {\n assert(n > 0);\n VL fow, bck;\n for(ll i = 1; i * i <= n; i++) {\n if(n % i == 0) {\n fow.push_back(i);\n if(i * i != n) bck.push_back(n / i);\n }\n }\n reverse(ALL(bck));\n fow.insert(fow.end(), ALL(bck));\n return fow;\n}\n\nbool is_prime(ll n) {\n assert(n > 0);\n for(ll d = 2; d*d <= n; d++) {\n if(n % d == 0) return false;\n }\n return true;\n}\n\nVL least_prime_factors(ll n) {\n assert(n > 0);\n VL lpfs(n+1, -1), primes;\n FOR(d, 2, n+1) {\n if(lpfs[d] == -1) {\n lpfs[d] = d;\n primes.push_back(d);\n }\n for(ll p : primes) {\n if(p * d > n || p > lpfs[d]) break;\n lpfs[p*d] = p;\n }\n }\n return lpfs;\n}\n\nVL prime_list(ll n) {\n assert(n > 0);\n VL primes;\n vector<bool> sieved(n+1);\n FOR(d, 2, n+1) {\n if(!sieved[d]) {\n primes.push_back(d);\n for(ll i = d*d; i <= n; i += d) sieved[i] = 1;\n }\n }\n return primes;\n}\n\nmap<ll, int> prime_factor(ll n) {\n assert(n > 0);\n map<ll, int> factor;\n for(ll d = 2; d*d <= n; d++) {\n while(n%d == 0) {\n n /= d;\n factor[d]++;\n }\n }\n if(n > 1) factor[n]++;\n return factor;\n}\n\nll extgcd(ll a, ll b, ll& x, ll& y) {\n x = 1, y = 0;\n ll nx = 0, ny = 1;\n while(b) {\n ll q = a / b;\n tie(a, b) = LP(b, a % b);\n tie(x, nx) = LP(nx, x - nx*q);\n tie(y, ny) = LP(ny, y - ny*q);\n }\n return a;\n}\n\nll inv_mod(ll n, ll m) {\n ll x, y;\n assert(extgcd(n, m, x, y) == 1);\n x %= m;\n if(x < 0) x += m;\n return x;\n}\n\nll pow_mod(ll a, ll n, ll m) {\n if(n == 0) return 1LL;\n if(n < 0) return inv_mod(pow_mod(a, -n, m), m);\n ll res = 1;\n while(n) {\n if(n & 1) {\n res *= a;\n res %= m;\n }\n n >>= 1;\n a *= a;\n a %= m;\n }\n return res;\n}\n\n#line 5 \"library/KowerKoint/modint.hpp\"\n\ntemplate <ll (*mod)()>\nstruct Modint {\n ll val;\n \n Modint(): val(0) {}\n\n Modint(ll x): val(x) {\n val %= mod();\n if(val < 0) val += mod();\n }\n\n Modint& operator+=(const Modint& r) {\n val += r.val;\n if(val >= mod()) val -= mod();\n return *this;\n }\n friend Modint operator+(const Modint& l, const Modint& r) {\n return Modint(l) += r;\n }\n\n Modint& operator-=(const Modint& r) {\n val -= r.val;\n if(val < 0) val += mod();\n return *this;\n }\n friend Modint operator-(const Modint& l, const Modint& r) {\n return Modint(l) -= r;\n }\n\n Modint& operator*=(const Modint& r) {\n val *= r.val;\n val %= mod();\n return *this;\n }\n Modint operator*(const Modint& r) {\n return (Modint(*this) *= r);\n }\n friend Modint operator*(const Modint& l, const Modint& r) {\n return Modint(l) *= r;\n }\n\n Modint pow(ll n) const {\n return Modint(pow_mod(val, n, mod()));\n }\n\n Modint inv() const {\n return Modint(inv_mod(val, mod()));\n }\n\n Modint& operator/=(const Modint& r) {\n return (*this *= r.inv());\n }\n friend Modint operator/(const Modint& l, const Modint& r) {\n return Modint(l) /= r;\n }\n\n Modint& operator^=(const ll n) {\n val = pow_mod(val, n, mod());\n return *this;\n }\n Modint operator^(const ll n) {\n return this->pow(n);\n }\n\n Modint operator+() const { return *this; }\n Modint operator-() const { return Modint() - *this; }\n\n Modint& operator++() {\n val++;\n if(val == mod()) val = 0LL;\n return *this;\n }\n Modint& operator++(int) {\n Modint res(*this);\n ++*this;\n return res;\n }\n\n Modint& operator--() {\n if(val == 0LL) val = mod();\n val--;\n return *this;\n }\n Modint& operator--(int) {\n Modint res(*this);\n --*this;\n return res;\n }\n\n friend bool operator==(const Modint& l, const Modint& r) {\n return l.val == r.val;\n }\n friend bool operator!=(const Modint& l, const Modint& r) {\n return l.val != r.val;\n }\n\n static pair<vector<Modint>, vector<Modint>> factorial(int n) {\n vector<Modint> fact(n+1), rfact(n+1);\n fact[0] = 1;\n REP(i, n) fact[i+1] = fact[i] * (i+1);\n rfact[n] = 1 / fact[n];\n for(int i = n-1; i >= 0; i--) rfact[i] = rfact[i+1] * (i+1);\n return {fact, rfact};\n }\n\n friend istream& operator>>(istream& is, Modint& mi) {\n is >> mi.val;\n return is;\n }\n\n friend ostream& operator<<(ostream& os, const Modint& mi) {\n os << mi.val;\n return os;\n }\n};\n\nusing MI3 = Modint<internal_operator::mod3>;\nusing V3 = vector<MI3>;\nusing VV3 = vector<V3>;\nusing VVV3 = vector<VV3>;\nusing MI7 = Modint<internal_operator::mod7>;\nusing V7 = vector<MI7>;\nusing VV7 = vector<V7>;\nusing VVV7 = vector<VV7>;\nusing MI9 = Modint<internal_operator::mod9>;\nusing V9 = vector<MI9>;\nusing VV9 = vector<V9>;\nusing VVV9 = vector<VV9>;\n#line 3 \"library/KowerKoint/counting.hpp\"\n\ntemplate <typename T>\nstruct Counting {\n vector<T> fact, ifact;\n\n Counting() {}\n Counting(ll n) {\n expand(n);\n }\n\n void expand(ll n) {\n ll sz = (ll)fact.size();\n if(sz > n) return;\n fact.resize(n+1);\n ifact.resize(n+1);\n fact[0] = 1;\n FOR(i, max(1LL, sz), n+1) fact[i] = fact[i-1] * i;\n ifact[n] = 1 / fact[n];\n for(ll i = n-1; i >= sz; i--) ifact[i] = ifact[i+1] * (i+1);\n }\n\n T permutation(ll n, ll r) {\n assert(n >= r);\n assert(r >= 0);\n expand(n);\n return fact[n] * ifact[n-r];\n }\n\n T combination(ll n, ll r) {\n assert(n >= r);\n assert(r >= 0);\n expand(n);\n return fact[n] * ifact[r] * ifact[n-r];\n }\n\n T stirling(ll n, ll k) {\n assert(n >= k);\n assert(k >= 0);\n if(n == 0) return 1;\n T res = 0;\n int sign = k%2? -1 : 1;\n expand(k);\n REP(i, k+1) {\n res += sign * ifact[i] * ifact[k-i] * T(i).pow(n);\n sign *= -1;\n }\n return res;\n }\n\n vector<vector<T>> stirling_table(ll n, ll k) {\n assert(n >= 0 && k >= 0);\n vector<vector<T>> res(n+1, vector<T>(k+1));\n res[0][0] = 1;\n FOR(i, 1, n+1) FOR(j, 1, k+1) {\n res[i][j] = res[i-1][j-1] + j * res[i-1][j];\n }\n return res;\n }\n\n T bell(ll n, ll k) {\n assert(n >= 0 && k >= 0);\n expand(k);\n vector<T> tmp(k+1);\n int sign = 1;\n tmp[0] = 1;\n FOR(i, 1, k+1) {\n sign *= -1;\n tmp[i] = tmp[i-1] + sign * ifact[i];\n }\n T res = 0;\n REP(i, k+1) {\n res += T(i).pow(n) * ifact[i] * tmp[k-i];\n }\n return res;\n }\n\n vector<vector<T>> partition_table(ll n) {\n assert(n >= 0);\n vector<vector<T>> res(n+1, vector<T>(n+1));\n REP(i, n+1) res[0][i] = 1;\n FOR(i, 1, n+1) FOR(j, 1, n+1) {\n res[i][j] = res[i][j-1] + (i<j? 0 : res[i-j][j]);\n }\n return res;\n }\n};\n#line 2 \"Contests/Dummy/main.cpp\"\n\n/* #include \"atcoder/all\" */\n/* using namespace atcoder; */\n/* #include \"KowerKoint/expansion/ac-library/all.hpp\" */\n#line 2 \"library/KowerKoint/string.hpp\"\n\n#line 4 \"library/KowerKoint/string.hpp\"\n\ntemplate <typename It>\nvector<int> kmp_table(It begin, It end) {\n int m = end - begin;\n vector<int> table(m);\n int j = 0;\n FOR(i, 1, m) {\n while(j > 0 && *(begin+i) != *(begin+j)) j = table[j-1];\n if(*(begin+i) == *(begin+j)) table[i] = ++j;\n }\n return table;\n}\n\nvector<int> kmp_table(string& t) {\n return kmp_table(ALL(t));\n}\n\ntemplate <typename It>\nvector<int> kmp_find(It s_begin, It s_end, It t_begin, It t_end, vector<int>& table) {\n int n = s_end - s_begin;\n int m = t_end - t_begin;\n vector<int> res;\n int j = 0;\n REP(i, n) {\n while(j > 0 && *(s_begin+i) != *(t_begin+j)) j = table[j-1];\n if(*(s_begin+i) == *(t_begin+j)) {\n if(++j == m) {\n res.push_back(i - (m-1));\n j = table[m-1];\n }\n }\n }\n return res;\n}\n\nvector<int> kmp_find(string& s, string& t, vector<int>& table) {\n return kmp_find(ALL(s), ALL(t), table);\n}\n\ntemplate <typename T=char, T root_char='_', int hash_size=30>\nstruct Trie {\n struct Node {\n T c;\n unordered_map<char, Node*> nxt;\n Node* failure;\n vector<int> fullmatch_keyword_id;\n vector<int> suffixmatch_keyword_id;\n\n Node(T _c): c(_c) {\n nxt.reserve(30);\n }\n };\n\n Node* root;\n int keyword_id = 0;\n\n Trie() {\n root = new Node(root_char);\n }\n\n template <typename It>\n void add(It begin, It end, int keyword_id_) {\n Node* cursor = root;\n for(It it = begin; it != end; it++) {\n if(!cursor->nxt.count(*it)) cursor->nxt[*it] = new Node(*it);\n cursor = cursor->nxt[*it];\n }\n cursor->fullmatch_keyword_id.push_back(keyword_id_);\n }\n\n void add(const string& str, int keyword_id_=-1) {\n add(ALL(str), keyword_id_ == -1? keyword_id++ : keyword_id_);\n }\n\n void build_failure() {\n queue<Node*> que;\n for(auto [c, to] : root->nxt) {\n to->failure = root;\n for(int x : to->fullmatch_keyword_id) {\n to->suffixmatch_keyword_id.push_back(x);\n }\n que.push(to);\n }\n while(!que.empty()) {\n Node* from = que.front(); que.pop();\n for(auto [c, to] : from->nxt) {\n Node* cursor = from->failure;\n while(cursor != root && !cursor->nxt.count(c)) cursor = cursor->failure;\n if(cursor->nxt.count(c)) to->failure = cursor->nxt[c];\n else to->failure = root;\n for(int x : to->fullmatch_keyword_id) {\n to->suffixmatch_keyword_id.push_back(x);\n }\n for(int x : to->failure->suffixmatch_keyword_id) {\n to->suffixmatch_keyword_id.push_back(x);\n }\n que.push(to);\n }\n }\n }\n\n template <typename It>\n void aho_corasick(It begin, It end, function<void(vector<int>&)>& f) {\n Node* cursor = root;\n for(It it = begin; it != end; it++) {\n while(cursor != root && !cursor->nxt.count(*it)) cursor = cursor->failure;\n if(cursor->nxt.count(*it)) {\n cursor = cursor->nxt[*it];\n f(cursor->suffixmatch_keyword_id);\n }\n }\n }\n\n template <typename It>\n ll aho_corasick(It begin, It end) {\n ll res = 0;\n function<void(vector<int>&)> f = [&](vector<int>& v) {\n res += v.size();\n };\n aho_corasick(begin, end, f);\n return res;\n }\n\n void aho_corasick(string &s, function<void(vector<int>&)>& f) {\n aho_corasick(ALL(s), f);\n }\n\n ll aho_corasick(string &s) {\n return aho_corasick(ALL(s));\n }\n};\n\ntemplate <typename T>\nstruct RollingHash {\n int num;\n vector<T> base;\n vector<vector<T>> power;\n\n RollingHash(vector<T> base_) : num(base_.size()), base(base_) {\n power = vector<vector<T>>(num, vector<T>(1, 1));\n }\n\n RollingHash(int num_=3) : num(num_) {\n assert(num_ > 0);\n power = vector<vector<T>>(num, vector<T>(1, 1));\n mt19937 engine((random_device){}());\n REP(i, num) base.push_back(engine());\n }\n\n void expand(int n) {\n int m = power[0].size();\n if(m > n) return;\n REP(i, num) {\n power[i].resize(n+1);\n FOR(j, m, n+1) power[i][j] = power[i][j-1] * base[i];\n }\n }\n\n template<typename It>\n vector<vector<T>> build(It begin, It end) {\n int n = end - begin;\n vector<vector<T>> res(num, vector<T>(n+1));\n REP(i, num) REP(j, n) {\n res[i][j+1] = res[i][j] * base[i] + *(begin+j);\n }\n return res;\n }\n\n vector<vector<T>> build(const string& s) {\n return build(ALL(s));\n }\n\n vector<T> query(const vector<vector<T>>& hash, int l, int r) {\n assert(hash.size() == num);\n assert(0 <= l && l <= r && r < hash[0].size());\n expand(r - l);\n vector<T> res(num);\n REP(i, num) res[i] = hash[i][r] - hash[i][l] * power[i][r-l];\n return res;\n }\n};\n#line 7 \"Contests/Dummy/main.cpp\"\n\nvoid solve(){\n while([] {\n int n, m, p; cin >> n >> m >> p;\n if(!n) return false;\n VVI z(n, VI(n));\n VVVI col(n, VVI(n, VI(n)));\n auto judge = [&](int turn) {\n REP(i, n) REP(j, n) REP(k, n) {\n FOR(dx, -1, 2) FOR(dy, -1, 2) FOR(dz, -1, 2) {\n if(dx == 0 && dy == 0 && dz == 0) continue;\n int tx = i + (m-1) * dx;\n int ty = j + (m-1) * dy;\n int tz = k + (m-1) * dz;\n if(tx < 0 || tx >= n || ty < 0 || ty >= n || tz < 0 || tz >= n) continue;\n bool flag = true;\n REP(s, m) {\n flag &= col[i+dx*s][j+dy*s][k+dz*s] == turn;\n }\n if(flag) return true;\n }\n }\n return false;\n };\n int turn = 1;\n VP xy(p); cin >> xy;\n REP(i, p) {\n auto [x, y] = xy[i]; x--; y--;\n col[x][y][z[x][y]++] = turn;\n if(judge(turn)) {\n if(turn == 1) print(\"Black\", i+1);\n else print(\"White\", i+1);\n return true;;\n }\n turn = 3 - turn;\n }\n print(\"Draw\");\n return true;\n }());\n}\n\n// generated by oj-template v4.7.2 (https://github.com/online-judge-tools/template-generator)\nint main() {\n // Fasterize input/output script\n ios::sync_with_stdio(false);\n cin.tie(nullptr);\n cout << fixed << setprecision(100);\n // scanf/printf user should delete this fasterize input/output script\n\n int t = 1;\n //cin >> t; // comment out if solving multi testcase\n for(int testCase = 1;testCase <= t;++testCase){\n solve();\n }\n return 0;\n}", "accuracy": 1, "time_ms": 200, "memory_kb": 3344, "score_of_the_acc": -0.6447, "final_rank": 6 }, { "submission_id": "aoj_1249_6384726", "code_snippet": "#line 1 \"a.cpp\"\n#define PROBLEM \"\"\n#line 2 \"/home/kuhaku/atcoder/github/atcoder-lib/lib/template/atcoder.hpp\"\n#pragma GCC target(\"avx\")\n#pragma GCC optimize(\"O3\")\n#pragma GCC optimize(\"unroll-loops\")\n#line 2 \"/home/kuhaku/atcoder/github/atcoder-lib/lib/template/template.hpp\"\n#include <bits/stdc++.h>\nusing namespace std;\ntemplate <class T, class U>\nbool chmax(T &a, const U &b) {\n return a < b ? a = b, true : false;\n}\ntemplate <class T, class U>\nbool chmin(T &a, const U &b) {\n return b < a ? a = b, true : false;\n}\nconstexpr int64_t INF = 1000000000000000003;\nconstexpr int Inf = 1000000003;\nconstexpr int MOD = 1000000007;\nconstexpr int MOD_N = 998244353;\nconstexpr double EPS = 1e-7;\nconst double PI = acos(-1.0);\n#line 6 \"/home/kuhaku/atcoder/github/atcoder-lib/lib/template/atcoder.hpp\"\nusing ll = int64_t;\nusing ld = long double;\n#define FOR(i, m, n) for(int i = (m); i < int(n); ++i)\n#define FORR(i, m, n) for(int i = (m)-1; i >= int(n); --i)\n#define FORL(i, m, n) for(int64_t i = (m); i < int64_t(n); ++i)\n#define rep(i, n) FOR(i, 0, n)\n#define repn(i, n) FOR(i, 1, n+1)\n#define repr(i, n) FORR(i, n, 0)\n#define repnr(i, n) FORR(i, n+1, 1)\n#define all(s) (s).begin(), (s).end()\ntemplate<class T, class U>\nstd::istream &operator>>(std::istream &is, std::pair<T, U> &p) { is >> p.first >> p.second; return is; }\ntemplate <class T>\nstd::istream &operator>>(std::istream &is, std::vector<T> &v) { for (T &i : v) is>>i; return is; }\ntemplate <class T, class U>\nstd::ostream &operator<<(std::ostream &os, const std::pair<T, U> &p) {\n return os<<'('<<p.first<< ','<<p.second<<')';\n}\ntemplate <class T>\nstd::ostream &operator<<(std::ostream &os, const std::vector<T> &v) {\n for (auto it=v.begin(); it!=v.end(); ++it) { os<<(it==v.begin()?\"\":\" \")<<*it; } return os;\n}\ntemplate <class Head, class... Tail>\nvoid co(Head&& head, Tail&&... tail) {\n if constexpr(sizeof...(tail)==0) std::cout<<head<<'\\n'; else std::cout<<head<<' ',co(forward<Tail>(tail)...);\n}\ntemplate <class Head, class... Tail>\nvoid ce(Head&& head, Tail&&... tail) {\n if constexpr(sizeof...(tail)==0) std::cerr<<head<<'\\n'; else std::cerr<<head<<' ',ce(forward<Tail>(tail)...);\n}\ntemplate<typename T, typename... Args>\nauto make_vector(T x, int arg, Args ...args) {\n if constexpr(sizeof...(args)==0) return std::vector<T>(arg,x); else return std::vector(arg,make_vector<T>(x,args...));\n}\nvoid sonic() { std::ios::sync_with_stdio(false); std::cin.tie(nullptr); }\nvoid setp(const int n) { std::cout<<std::fixed<<std::setprecision(n); }\nvoid Yes(bool is_correct) { std::cout<<(is_correct?\"Yes\":\"No\")<<std::endl; }\nvoid YES(bool is_correct) { std::cout<<(is_correct?\"YES\":\"NO\")<<std::endl; }\n#line 3 \"a.cpp\"\n\narray<int, 3> arr = {0, 1, -1};\n\nint main(void) {\n sonic();\n while (true) {\n int n, m, p;\n cin >> n >> m >> p;\n if (!n)\n break;\n vector<vector<int>> a(n, vector<int>(n));\n vector<tuple<int, int, int>> b, w;\n set<tuple<int, int, int>> bl, wh;\n bool flag = true;\n rep(_, p) {\n int x, y;\n cin >> x >> y;\n x--, y--;\n b.emplace_back(x, y, a[x][y]);\n bl.emplace(x, y, a[x][y]);\n if (!flag)\n continue;\n for (auto t : b) {\n FOR(i, 1, 27) {\n int dx = arr[i % 3], dy = arr[i / 3 % 3], dz = arr[i / 9 % 3];\n bool ok = true;\n rep(j, m) {\n ok &= bl.find(make_tuple(get<0>(t) + dx * j, get<1>(t) + dy * j,\n get<2>(t) + dz * j)) != bl.end();\n }\n if (flag && ok) {\n if (_ & 1)\n cout << \"White \" << _ + 1 << endl;\n else\n cout << \"Black \" << _ + 1 << endl;\n flag = false;\n }\n }\n }\n a[x][y]++;\n\n swap(b, w);\n swap(bl, wh);\n }\n\n if (flag) {\n co(\"Draw\");\n }\n }\n\n return 0;\n}", "accuracy": 1, "time_ms": 1250, "memory_kb": 3260, "score_of_the_acc": -0.9858, "final_rank": 15 }, { "submission_id": "aoj_1249_6089043", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\nint dx[] = {1,0,0,1,1,1,-1,0,0,1,1,1,1};\nint dy[] = {0,1,0,1,-1,0,0,1,1,1,1,-1,-1};\nint dz[] = {0,0,1,0,0,1,1,1,-1,1,-1,1,-1};\n\nbool calc(vector<vector<vector<int>>>tmp,int n,int m) {\n for(int i = 0; i < n; i++) {\n for(int j = 0; j < n; j++) {\n for(int k = 0; k < tmp[i][j].size(); k++) {\n for(int l = 0; l < 13; l++) {\n int cnt = 1;\n int nx = i,ny = j,nz = k;\n for(int r = 1; r < m; r++) {\n nx += dx[l];\n ny += dy[l];\n nz += dz[l];\n if(min({nx,ny,nz}) >= 0 && max(nx,ny) < n && nz < tmp[nx][ny].size()) {\n if(tmp[nx-dx[l]][ny-dy[l]][nz-dz[l]] != tmp[nx][ny][nz]) {\n break;\n }\n cnt++;\n }\n else {\n break;\n }\n }\n if(cnt == m) {\n return true;\n }\n }\n }\n }\n }\n return false;\n}\n\nint main() {\n while (true) {\n int n,m,p;\n cin >> n >> m >> p;\n if(n == 0 && m == 0 && p == 0) {\n return 0;\n }\n vector<vector<vector<int>>>tmp(n,vector<vector<int>>(n));\n bool flag = true;\n for(int i = 0; i < p; i++) {\n int x,y;\n cin >> x >> y;\n x--;\n y--;\n tmp[x][y].push_back(i%2);\n if(calc(tmp,n,m) && flag) {\n flag = false;\n cout << ((i%2 == 0)?\"Black\":\"White\") << \" \" << i+1 << endl;\n }\n }\n if(flag) {\n cout << \"Draw\" << endl;\n }\n }\n}", "accuracy": 1, "time_ms": 120, "memory_kb": 3448, "score_of_the_acc": -0.8783, "final_rank": 11 }, { "submission_id": "aoj_1249_6011414", "code_snippet": "#include <bits/stdc++.h>\n#define all(v) v.begin(), v.end()\n#define rall(v) v.rbegin(), v.rend()\n#define rep(i,n) for(int i=0;i<(int)(n);i++)\n#define drep(i,j,n) for(int i=0;i<(int)(n-1);i++)for(int j=i+1;j<(int)(n);j++)\n#define trep(i,j,k,n) for(int i=0;i<(int)(n-2);i++)for(int j=i+1;j<(int)(n-1);j++)for(int k=j+1;k<(int)(n);k++)\n#define codefor int test;scanf(\"%d\",&test);while(test--)\n#define INT(...) int __VA_ARGS__;in(__VA_ARGS__)\n#define LL(...) ll __VA_ARGS__;in(__VA_ARGS__)\n#define yes(ans) if(ans)printf(\"yes\\n\");else printf(\"no\\n\")\n#define Yes(ans) if(ans)printf(\"Yes\\n\");else printf(\"No\\n\")\n#define YES(ans) if(ans)printf(\"YES\\n\");else printf(\"NO\\n\")\n#define popcount(v) __builtin_popcount(v)\n#define vector2d(type,name,h,...) vector<vector<type>>name(h,vector<type>(__VA_ARGS__))\n#define vector3d(type,name,h,w,...) vector<vector<vector<type>>>name(h,vector<vector<type>>(w,vector<type>(__VA_ARGS__)))\n#define umap unordered_map\n#define uset unordered_set\nusing namespace std;\nusing ll = long long;\nconst int MOD=1000000007;\nconst int MOD2=998244353;\nconst int INF=1<<30;\nconst ll INF2=(ll)1<<60;\nvoid scan(int& a){scanf(\"%d\",&a);}\nvoid scan(long long& a){scanf(\"%lld\",&a);}\ntemplate<class T,class L>void scan(pair<T, L>& p){scan(p.first);scan(p.second);}\ntemplate<class T,class U,class V>void scan(tuple<T,U,V>& p){scan(get<0>(p));scan(get<1>(p));scan(get<2>(p));}\ntemplate<class T> void scan(T& a){cin>>a;}\ntemplate<class T> void scan(vector<T>& vec){for(auto&& it:vec)scan(it);}\nvoid in(){}\ntemplate <class Head, class... Tail> void in(Head& head, Tail&... tail){scan(head);in(tail...);}\nvoid print(const int& a){printf(\"%d\",a);}\nvoid print(const long long& a){printf(\"%lld\",a);}\nvoid print(const double& a){printf(\"%.15lf\",a);}\ntemplate<class T,class L>void print(const pair<T, L>& p){print(p.first);putchar(' ');print(p.second);}\ntemplate<class T> void print(const T& a){cout<<a;}\ntemplate<class T> void print(const vector<T>& vec){if(vec.empty())return;print(vec[0]);for(auto it=vec.begin();++it!= vec.end();){putchar(' ');print(*it);}}\nvoid out(){putchar('\\n');}\ntemplate<class T> void out(const T& t){print(t);putchar('\\n');}\ntemplate <class Head, class... Tail> void out(const Head& head,const Tail&... tail){print(head);putchar(' ');out(tail...);}\ntemplate<class T> void dprint(const T& a){cerr<<a;}\ntemplate<class T> void dprint(const vector<T>& vec){if(vec.empty())return;cerr<<vec[0];for(auto it=vec.begin();++it!= vec.end();){cerr<<\" \"<<*it;}}\nvoid debug(){cerr<<endl;}\ntemplate<class T> void debug(const T& t){dprint(t);cerr<<endl;}\ntemplate <class Head, class... Tail> void debug(const Head& head, const Tail&... tail){dprint(head);cerr<<\" \";debug(tail...);}\nll intpow(ll a, ll b){ ll ans = 1; while(b){ if(b & 1) ans *= a; a *= a; b /= 2; } return ans; }\nll modpow(ll a, ll b, ll p){ ll ans = 1; while(b){ if(b & 1) (ans *= a) %= p; (a *= a) %= p; b /= 2; } return ans; }\nll modinv(ll a, ll m) {ll b = m, u = 1, v = 0;while (b) {ll t = a / b;a -= t * b; swap(a, b);u -= t * v; swap(u, v);}u %= m;if (u < 0) u += m;return u;}\nll updivide(ll a,ll b){if(a%b==0) return a/b;else return (a/b)+1;}\ntemplate<class T> void chmax(T &a,const T b){if(b>a)a=b;}\ntemplate<class T> void chmin(T &a,const T b){if(b<a)a=b;}\n\nvector<string> solve(int n,int m,int p){\n vector<pair<int,int>> a(p);\n rep(i,p){\n in(a[i]);\n a[i].first--,a[i].second--;\n }\n vector<vector<deque<int>>> A(n,vector<deque<int>>(n));\n //x,y,z,dx,dy,dz,color\n function<int(array<int,7>)> rec=[&](array<int,7> b){\n if(b[0]<0||b[0]>=n)return 0;\n if(b[1]<0||b[1]>=n)return 0;\n if(b[2]<0||b[2]>=A[b[0]][b[1]].size())return 0;\n if(b[6]!=A[b[0]][b[1]][b[2]])return 0;\n b[0]+=b[3],b[1]+=b[4],b[2]+=b[5];\n return 1+rec(b);\n };\n rep(i,p){\n if(i%2==0){\n A[a[i].first][a[i].second].push_back(0);\n int maxv=0;\n for(int dx=-1;dx<=1;dx++){\n for(int dy=-1;dy<=1;dy++){\n for(int dz=-1;dz<=1;dz++){\n if(!dx&&!dy&&!dz)continue;\n array<int,7> c={a[i].first,a[i].second,(int)A[a[i].first][a[i].second].size()-1,\n dx,dy,dz,0};\n array<int,7> d={a[i].first,a[i].second,(int)A[a[i].first][a[i].second].size()-1,\n -dx,-dy,-dz,0};\n chmax(maxv,rec(c)+rec(d)-1);\n }\n }\n }\n if(maxv>=m){\n return {\"Black\",to_string(i+1)};\n }\n }else{\n A[a[i].first][a[i].second].push_back(1);\n int maxv=0;\n for(int dx=-1;dx<=1;dx++){\n for(int dy=-1;dy<=1;dy++){\n for(int dz=-1;dz<=1;dz++){\n if(!dx&&!dy&&!dz)continue;\n array<int,7> c={a[i].first,a[i].second,(int)A[a[i].first][a[i].second].size()-1,\n dx,dy,dz,1};\n array<int,7> d={a[i].first,a[i].second,(int)A[a[i].first][a[i].second].size()-1,\n -dx,-dy,-dz,1};\n chmax(maxv,rec(c)+rec(d)-1);\n }\n }\n }\n if(maxv>=m){\n return {\"White\",to_string(i+1)};\n }\n }\n }\n return {\"Draw\"};\n}\n\nint main(){\n int n,m,p;\n while(cin>>n>>m>>p){\n if(!n)return 0;\n out(solve(n,m,p));\n }\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3308, "score_of_the_acc": -0.4468, "final_rank": 4 }, { "submission_id": "aoj_1249_5990183", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\n\nint n, m, p;\n\nbool check(vector<vector<vector<int>>>& game) {\n for (int x = 0; x < n; ++x) {\n for (int y = 0; y < n; ++y) {\n for (int z = 0; z < n; ++z) {\n vector<vector<int>> rows(13);\n for (int i = 0; i < m; ++i) {\n if (x+i < n) rows[0].push_back(game[x+i][y][z]);\n if (y+i < n) rows[1].push_back(game[x][y+i][z]);\n if (z+i < n) rows[2].push_back(game[x][y][z+i]);\n if (x+i < n && y+i < n) rows[3].push_back(game[x+i][y+i][z]);\n if (y+i < n && z+i < n) rows[4].push_back(game[x][y+i][z+i]);\n if (z+i < n && x+i < n) rows[5].push_back(game[x+i][y][z+i]);\n if (x+i < n && y-i >= 0) rows[6].push_back(game[x+i][y-i][z]);\n if (y+i < n && z-i >= 0) rows[7].push_back(game[x][y+i][z-i]);\n if (z+i < n && x-i >= 0) rows[8].push_back(game[x-i][y][z+i]);\n\n if (x+i < n && y+i < n && z+i < n) rows[9].push_back(game[x+i][y+i][z+i]);\n if (x+i < n && y-i >= 0 && z+i < n) rows[10].push_back(game[x+i][y-i][z+i]);\n if (x+i < n && y+i < n && z-i >= 0) rows[11].push_back(game[x+i][y+i][z-i]);\n if (x+i < n && y-i >= 0 && z-i >= 0) rows[12].push_back(game[x+i][y-i][z-i]);\n }\n for (int i = 0; i < 13; ++i) {\n if (rows[i].size() < m || rows[i][0] == -1) continue;\n bool ok = true;\n for (int j = 1; j < m; ++j) {\n if (rows[i][0] != rows[i][j]) {\n ok = false;\n break;\n }\n }\n if (ok) {\n return true;\n }\n }\n }\n }\n }\n return false;\n}\n\nint main() {\n ios_base::sync_with_stdio(false);\n cin.tie(nullptr);\n cout << fixed << setprecision(15);\n\n while (true) {\n cin >> n >> m >> p;\n if (n == 0 && m == 0 && p == 0) break;\n vector game(n, vector<vector<int>>(n, vector<int>(n, -1)));\n int winner = -1;\n int turn = 0;\n while (p--) {\n int x, y;\n cin >> x >> y;\n --x, --y;\n if (winner != -1) continue;\n int z = 0;\n while (game[x][y][z] != -1) ++z;\n game[x][y][z] = turn % 2;\n if (check(game)) {\n winner = turn % 2;\n }\n ++turn;\n }\n if (winner == 0) cout << \"Black \" << turn << \"\\n\";\n else if (winner == 1) cout << \"White \" << turn << \"\\n\";\n else cout << \"Draw\\n\";\n }\n}", "accuracy": 1, "time_ms": 1600, "memory_kb": 3164, "score_of_the_acc": -0.9187, "final_rank": 13 }, { "submission_id": "aoj_1249_5990014", "code_snippet": "#include <bits/stdc++.h>\n#include <time.h> \nusing namespace std;\nusing ll = long long;\n#define all(A) A.begin(),A.end()\nusing vll = vector<ll>;\n#define rep(i, n) for (long long i = 0; i < (long long)(n); i++)\nvector<vector<int>> G;\nvector<vector<int>> GR;\n/*\nstruct IOSetup{\n IOSetup(){\n cin.tie(0);\n ios::sync_with_stdio(0);\n cout<<fixed<<setprecision(12);\n }\n} iosetup;\n*/\n\nbool in(ll x,ll y, ll z,ll N){\n if(0<=x&&x<N&&0<=y&&y<N&&0<=z&&z<N)return true;\n return false;\n}\n\nvll dx={1,-1,0};\nvll dy={1,-1,0};\nvll dz={1,-1,0};\nint main() {\n while(1){\n ll N,M,P;\n cin>>N>>M>>P;\n if(N==0)return 0;\n string AN=\"Draw\";\n ll an=-1;\n ll k=0;\n vector<vector<vll>> B(N,vector<vll>(N,vll(N,-1)));\n rep(p,P){\n ll X,Y;\n cin>>X>>Y;\n X--;Y--;\n rep(i,N){\n if(B[X][Y][i]==-1){\n B[X][Y][i]=k;\n k=1-k;\n break;\n }\n }\n\n rep(x,N)rep(y,N)rep(z,N){\n if(B[x][y][z]==0){\n rep(i,3)rep(u,3)rep(e,3){\n if(i+u+e==6)continue;\n ll nx=x;\n ll ny=y;\n ll nz=z;\n bool C=true; \n rep(j,M){\n \n if(in(nx,ny,nz,N)){\n if(B[nx][ny][nz]==0);\n else C=false;\n }\n else C=false;\n nx+=dx[i];\n ny+=dy[u];\n nz+=dz[e];\n }\n if(C){\n if(AN==\"Draw\"){\n AN=\"Black\";\n an=p+1;\n }\n }\n }\n }\n else if(B[x][y][z]==1){\n rep(i,3)rep(u,3)rep(e,3){\n if(i+u+e==6)continue;\n ll nx=x;\n ll ny=y;\n ll nz=z;\n bool C=true; rep(j,M){\n if(in(nx,ny,nz,N)){\n if(B[nx][ny][nz]==1);\n else C=false;\n }\n else C=false;\n nx+=dx[i];\n ny+=dy[u];\n nz+=dz[e];\n }\n if(C){\n if(AN==\"Draw\"){\n AN=\"White\";\n an=p+1;\n }\n }\n }\n }\n }\n\n\n\n\n }\n if(AN==\"Draw\")cout<<AN<<endl;\n else {\n cout<<AN<<\" \"<<an<<endl;\n }\n }\n}", "accuracy": 1, "time_ms": 1820, "memory_kb": 3380, "score_of_the_acc": -1.6114, "final_rank": 19 }, { "submission_id": "aoj_1249_5987534", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\n// clang-format off\n//#include <atcoder/modint>\n//using namespace atcoder;\nusing ll = long long;\nusing ld = long double;\nusing pii = pair<int, int>;\nusing pll = pair<ll, ll>;\nusing pdd = pair<ld, ld>;\nusing vii = vector<int>;\nusing vll = vector<ll>;\nusing vdd = vector<ld>;\nusing vvii = vector<vector<int>>;\nusing vvll = vector<vector<ll>>;\nusing vvdd = vector<vector<ld>>;\nusing vvvii = vector<vector<vector<int>>>;\nusing vvvll = vector<vector<vector<ll>>>;\nusing vvvdd = vector<vector<vector<ld>>>;\ntemplate<class T, class U> using P = pair<T, U>;\ntemplate<class T> using V1 = vector<T>;\ntemplate<class T> using V2 = vector<vector<T>>;\ntemplate<class T> using V3 = vector<vector<vector<T>>>;\ntemplate<class T> using pque = priority_queue<T, vector<T>, greater<T>>;\n#define _overload3(_1, _2, _3, name, ...) name\n#define rep1(n) for (ll i = 0; i < (n); i++)\n#define rep2(i, n) for (ll i = 0; i < (n); i++)\n#define rep3(i, a, b) for (ll i = (a); i < (b); i++)\n#define rep(...) _overload3(__VA_ARGS__, rep3, rep2, rep1)(__VA_ARGS__)\n#define rrep1(n) for (ll i = (n) - 1; i >= 0; i--)\n#define rrep2(i, n) for (ll i = (n) - 1; i >= 0; i--)\n#define rrep3(i, a, b) for (ll i = (b) - 1; i >= (a); i--)\n#define rrep(...) _overload3(__VA_ARGS__, rrep3, rrep2, rrep1)(__VA_ARGS__)\n#define all(x) (x).begin(), (x).end()\n#define sz(x) ((int)(x).size())\n#define fi first\n#define se second\n#define pb push_back\n#define endl '\\n'\n#define popcnt(x) __builtin_popcountll(x)\n#define uniq(x) (x).erase(unique((x).begin(), (x).end()), (x).end());\n#define IOS ios::sync_with_stdio(false); cin.tie(nullptr);\nconst int inf = 1 << 30;\nconst ll INF = 1ll << 60;\nconst ld DINF = numeric_limits<ld>::infinity();\nconst ll mod = 1000000007;\n//const ll mod = 998244353;\nconst ld EPS = 1e-9;\nconst ld PI = 3.1415926535897932;\n//const ll dx[8] = {1, 0, -1, 0, 1, -1, -1, 1};\n//const ll dy[8] = {0, 1, 0, -1, 1, 1, -1, -1};\ntemplate<class T> bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }\ntemplate<class T> bool chmin(T &a, const T &b) { if (a>b) { a=b; return 1; } return 0; }\ntemplate<typename T> T minV(const vector<T> &x) { return *min_element(all(x)); }\ntemplate<typename T> T maxV(const vector<T> &x) { return *max_element(all(x)); }\ntemplate<class T, class U> ostream &operator<<(ostream &os, const pair<T, U> &p) { return os << \"(\" << p.fi << \", \" << p.se << \")\"; }\ntemplate<class T> ostream &operator<<(ostream &os, const vector<T> &v) { os << \"[ \"; for (auto &z : v) os << z << \" \"; os << \"]\"; return os; }\n#ifdef _DEBUG\n#define show(x) cout << #x << \" = \" << x << endl;\n#else\n#define show(x)\n#endif\nll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }\nll lcm(ll a, ll b) { return (a / gcd(a, b)) * b; }\nll rem(ll a, ll b) { return (a % b + b) % b; }\n// clang-format on\n\n// using mint = modint998244353;\n// using mint = modint1000000007;\n\nll modpow(ll a, ll N, ll mod) {\n ll res = 1;\n // 例えば3=101(2)なので、下位bitから順に1ならa倍する\n while (N) {\n if (N & 1) res = res * a % mod;\n a = a * a % mod;\n N >>= 1;\n }\n return res;\n}\n\nint seq(vii &c, int n, int m) {\n int cnt = 0;\n rep(i, n - 1) {\n if (c[i] == c[i + 1] && c[i] != -1)\n cnt++;\n else\n cnt = 0;\n if (cnt == m - 1) return 1;\n }\n return 0;\n}\nint dx[13] = {0, 0, 1, 0, 0, 1, 1, 1, -1, 1, 1, 1, -1};\nint dy[13] = {0, 1, 0, 1, 1, 1, -1, 0, 0, 1, 1, -1, 1};\nint dz[13] = {1, 0, 0, 1, -1, 0, 0, 1, 1, 1, -1, 1, 1};\nint K = 13;\n\nint win(vvvii &a, int n, int m) {\n rep(x, n) rep(y, n) rep(z, n) {\n vvii b(K);\n rep(k, K) {\n int cx = x, cy = y, cz = z;\n while (0 <= cx && cx < n && 0 <= cy && cy < n && 0 <= cz && cz < n) {\n b[k].pb(a[cx][cy][cz]);\n cx += dx[k];\n cy += dy[k];\n cz += dz[k];\n }\n if (seq(b[k], sz(b[k]), m)) return 1;\n }\n }\n return 0;\n}\n\nvoid solve(int n) {\n int m, p;\n cin >> m >> p;\n vvvii a(n, vvii(n, vii(n, -1)));\n vvii b(n, vii(n, 0));\n vii x(p), y(p);\n rep(p) {\n cin >> x[i] >> y[i];\n x[i]--, y[i]--;\n }\n rep(i, p) {\n a[x[i]][y[i]][b[x[i]][y[i]]] = i % 2;\n b[x[i]][y[i]]++;\n if (win(a, n, m)) {\n cout << ((i % 2 == 0) ? \"Black \" : \"White \") << i + 1 << endl;\n return;\n }\n }\n cout << \"Draw\" << endl;\n return;\n}\n\nint main() {\n vii x = {-1, -1, -1, 0, 0, 1, 1, 0, 0};\n show(seq(x, sz(x), 3));\n int n;\n while (cin >> n, n) solve(n);\n return 0;\n}\n\n/*\nメモ\n実装実装実装\n*/", "accuracy": 1, "time_ms": 1870, "memory_kb": 3388, "score_of_the_acc": -1.6596, "final_rank": 20 }, { "submission_id": "aoj_1249_5958249", "code_snippet": "#include <bits/stdc++.h>\n#define rep(i,n) for(int i = 0; i < (n); i++)\nusing namespace std;\ntypedef long long ll;\n\nint n,m,p, board[9][9][9], h[9][9];\nvector<tuple<int,int,int>> v;\n\nbool add(int a) {\n rep(i,n)rep(j,n)rep(k,n)for(auto d : v) {\n bool ok = true;\n int x = i, y = j, z = k;\n rep(l,m) {\n ok &= (0 <= x && x < n);\n ok &= (0 <= y && y < n);\n ok &= (0 <= z && z < n);\n if(!ok) break;\n ok &= (board[x][y][z] == a);\n int dx, dy, dz; tie(dx, dy, dz) = d;\n x += dx; y += dy; z += dz;\n }\n if(ok) return true;\n }\n return false;\n}\n\nint main(){\n cin.tie(0);\n ios::sync_with_stdio(0);\n\n rep(i,3)rep(j,3)rep(k,3)\n if(i != 1 || j != 1 || k != 1) \n v.push_back({i - 1, j - 1, k - 1});\n\n while(true) {\n cin >> n >> m >> p; if(n == 0) break;\n rep(i,n)rep(j,n)rep(k,n) board[i][j][k] = 0;\n rep(i,n)rep(j,n) h[i][j] = 0;\n bool finish = false;\n rep(i,p) {\n int x,y; cin >> x >> y; x--; y--;\n board[x][y][h[x][y]++] = (i & 1) + 1;\n if(!finish && add((i & 1) + 1)) {\n cout << (i & 1 ? \"White \" : \"Black \") << i + 1 << endl;\n finish = true;\n }\n }\n if(!finish) cout << \"Draw\" << endl;\n }\n}", "accuracy": 1, "time_ms": 280, "memory_kb": 3440, "score_of_the_acc": -0.943, "final_rank": 14 }, { "submission_id": "aoj_1249_5841704", "code_snippet": "#pragma region Macros\n#include <bits/stdc++.h>\nusing namespace std;\ntemplate <class T> inline bool chmax(T &a, T b) {\n if(a < b) {\n a = b;\n return 1;\n }\n return 0;\n}\ntemplate <class T> inline bool chmin(T &a, T b) {\n if(a > b) {\n a = b;\n return 1;\n }\n return 0;\n}\n#ifdef DEBUG\ntemplate <class T, class U>\nostream &operator<<(ostream &os, const pair<T, U> &p) {\n os << '(' << p.first << ',' << p.second << ')';\n return os;\n}\ntemplate <class T> ostream &operator<<(ostream &os, const vector<T> &v) {\n os << '{';\n for(int i = 0; i < (int)v.size(); i++) {\n if(i) { os << ','; }\n os << v[i];\n }\n os << '}';\n return os;\n}\nvoid debugg() { cerr << endl; }\ntemplate <class T, class... Args>\nvoid debugg(const T &x, const Args &... args) {\n cerr << \" \" << x;\n debugg(args...);\n}\n#define debug(...) \\\n cerr << __LINE__ << \" [\" << #__VA_ARGS__ << \"]: \", debugg(__VA_ARGS__)\n#define dump(x) cerr << __LINE__ << \" \" << #x << \" = \" << (x) << endl\n#else\n#define debug(...) (void(0))\n#define dump(x) (void(0))\n#endif\n\nstruct Setup {\n Setup() {\n cin.tie(0);\n ios::sync_with_stdio(false);\n cout << fixed << setprecision(15);\n }\n} __Setup;\n\nusing ll = long long;\n#define ALL(v) (v).begin(), (v).end()\n#define RALL(v) (v).rbegin(), (v).rend()\n#define FOR(i, a, b) for(int i = (a); i < int(b); i++)\n#define REP(i, n) FOR(i, 0, n)\nconst int INF = 1 << 30;\nconst ll LLINF = 1LL << 60;\nconstexpr int MOD = 1000000007;\n\nvoid Case(int i) { cout << \"Case #\" << i << \": \"; }\nint popcount(int x) { return __builtin_popcount(x); }\nll popcount(ll x) { return __builtin_popcountll(x); }\n#pragma endregion Macros\n\nint N, M, P;\nint board[10][10][10];\nint pointers[10][10];\n\nint check() {\n auto is_in = [&](int n) -> bool {\n return (0 <= n and n < N);\n };\n auto ff = [&](int sx, int sy, int sz, int dx, int dy, int dz) -> int {\n set<int> se;\n REP(i, M) {\n int x = sx + i * dx;\n int y = sy + i * dy;\n int z = sz + i * dz;\n if(!is_in(x) or !is_in(y) or !is_in(z)) return -1;\n if(board[x][y][z] == -1) return -1;\n se.insert(board[x][y][z]);\n }\n if(se.size() != 1) return -1;\n return (*se.begin());\n };\n REP(sx, N) REP(sy, N) REP(sz, N) FOR(dx, -1, 2) FOR(dy, -1, 2) FOR(dz, -1, 2) {\n if(dx == 0 and dy == 0 and dz == 0) continue;\n int res = ff(sx, sy, sz, dx, dy, dz);\n if(res == -1) continue;\n return res;\n }\n return -1;\n}\n\nbool solve() {\n cin >> N >> M >> P;\n if(N == 0) return false;\n\n // reset params\n memset(board, -1, sizeof(board));\n memset(pointers, 0, sizeof(pointers));\n\n // enter board param\n REP(i, P) {\n int x, y;\n cin >> x >> y;\n x--; y--;\n board[x][y][pointers[x][y]] = (i & 1);\n pointers[x][y]++;\n int res = check();\n if(res != -1) {\n REP(rest, P - i - 1) {\n cin >> x >> y;\n }\n cout << (res == 0 ? \"Black \" : \"White \");\n cout << i+1 << \"\\n\";\n return true;\n }\n }\n cout << \"Draw\\n\";\n return true;\n}\n\nint main() {\n while(solve());\n}", "accuracy": 1, "time_ms": 1090, "memory_kb": 3152, "score_of_the_acc": -0.6126, "final_rank": 5 }, { "submission_id": "aoj_1249_5728678", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\n#define REP(i,n) for(int i=0;i<(n);i++)\n#define ALL(v) v.begin(),v.end()\n\nint n,m,p;\nint d[9][9][9],e[9][9][9],h[9][9];\nstruct r3{\n int x,y,z;\n};\nvector<r3> V;\nbool f(int a){\n return 0<=a&&a<n;\n}\nbool fin(int a){\n REP(i,n)REP(j,n)REP(k,n)for(auto D:V){\n bool ok=true;\n int x=i,y=j,z=k;\n REP(l,m){\n ok&=f(x)&&f(y)&&f(z)&&d[x][y][z]==a;\n x+=D.x;y+=D.y;z+=D.z;\n }\n if(ok)return true;\n }\n return false;\n}\n\nint main(){\n REP(x,3)REP(y,3)REP(z,3)if(x!=1||y!=1||z!=1)V.push_back({x-1,y-1,z-1});\n while(cin>>n>>m>>p,n){\n REP(i,n)REP(j,n)REP(k,n)d[i][j][k]=0;\n REP(i,n)REP(j,n)h[i][j]=0;\n bool owari=false;\n REP(t,p){\n int x,y;cin>>x>>y;x--;y--;\n d[x][y][h[x][y]++]=(t&1)+1;\n if(!owari&&fin((t&1)+1)){\n if(t&1)cout<<\"White \"<<t+1<<endl;\n else cout<<\"Black \"<<t+1<<endl;\n owari=true;\n }\n }\n if(!owari)cout<<\"Draw\"<<endl;\n }\n}", "accuracy": 1, "time_ms": 970, "memory_kb": 3380, "score_of_the_acc": -1.1544, "final_rank": 17 }, { "submission_id": "aoj_1249_5194254", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\nint main() {\n int N, M, P;\n while (cin >> N >> M >> P, N) {\n vector table(N, vector(N, vector(N, -1))); // -1:empty, 0:black, 1:white\n vector height(N, vector(N, 0));\n vector<int> X(P), Y(P);\n for (int i = 0; i < P; i++) {\n cin >> X[i] >> Y[i];\n X[i]--;\n Y[i]--;\n }\n\n const int d[3] = {-1, 0, 1};\n auto check_in = [&](int x, int y, int z) {\n return x == clamp(x, 0, N - 1) && y == clamp(y, 0, N - 1) && z == clamp(z, 0, N - 1);\n };\n auto check_win = [&](int player) {\n for (int x_start = 0; x_start < N; x_start++) {\n for (int y_start = 0; y_start < N; y_start++) {\n for (int z_start = 0; z_start < N; z_start++) {\n for (int x_idx = 0; x_idx < 3; x_idx++) {\n for (int y_idx = 0; y_idx < 3; y_idx++) {\n for (int z_idx = 0; z_idx < 3; z_idx++) {\n if (x_idx == 1 && y_idx == 1 && z_idx == 1)\n continue;\n bool flag = true;\n for (int i = 0; i < M; i++) {\n int x = x_start + i * d[x_idx];\n int y = y_start + i * d[y_idx];\n int z = z_start + i * d[z_idx];\n if (!check_in(x, y, z) || table[x][y][z] != player) {\n flag = false;\n break;\n }\n }\n if (flag) {\n return true;\n }\n }\n }\n }\n }\n }\n }\n return false;\n };\n\n int winner = -1, moves = 0;\n for (int i = 0; i < P; i++) {\n int x = X[i], y = Y[i];\n int z = height[x][y];\n table[x][y][z] = i % 2;\n height[x][y]++;\n if (check_win(i % 2)) {\n winner = i % 2;\n moves = i + 1;\n break;\n }\n }\n if (winner == -1)\n cout << \"Draw\" << endl;\n else\n cout << (winner == 0 ? \"Black\" : \"White\") << \" \" << moves << endl;\n }\n}", "accuracy": 1, "time_ms": 270, "memory_kb": 3344, "score_of_the_acc": -0.6823, "final_rank": 8 } ]
aoj_1253_cpp
Problem F: Dice Puzzle Let’s try a dice puzzle. The rules of this puzzle are as follows. Dice with six faces as shown in Figure 1 are used in the puzzle. Figure 1: Faces of a die With twenty seven such dice, a 3 × 3 × 3 cube is built as shown in Figure 2. Figure 2: 3 × 3 × 3 cube When building up a cube made of dice, the sum of the numbers marked on the faces of adjacent dice that are placed against each other must be seven (See Figure 3). For example, if one face of the pair is marked “2”, then the other face must be “5”. Figure 3: A pair of faces placed against each other The top and the front views of the cube are partially given, i.e. the numbers on faces of some of the dice on the top and on the front are given. Figure 4: Top and front views of the cube The goal of the puzzle is to find all the plausible dice arrangements that are consistent with the given top and front view information. Your job is to write a program that solves this puzzle. Input The input consists of multiple datasets in the following format. N Dataset 1 Dataset 2 ... Dataset N N is the number of the datasets. The format of each dataset is as follows. T 11 T 12 T 13 T 21 T 22 T 23 T 31 T 32 T 33 F 11 F 12 F 13 F 21 F 22 F 23 F 31 F 32 F 33 T ij and F ij (1 ≤ i ≤ 3, 1 ≤ j ≤ 3) are the faces of dice appearing on the top and front views, as shown in Figure 2, or a zero. A zero means that the face at the corresponding position is unknown. Output For each plausible arrangement of dice, compute the sum of the numbers marked on the nine faces appearing on the right side of the cube, that is, with the notation given in Figure 2, ∑ 3 i =1 ∑ 3 j =1 R ij . For each dataset, you should output the right view sums for all the plausible arrangements, in ascending order and without duplicates. Numbers should be separated by a single space. When there are no plausible arrangements for a dataset, output a zero. For example, suppose that the top and the front views are given as follows. Figure 5: Example There are four plausible right views as shown in Figure 6. The right view sums are 33, 36, 32, and 33, respectively. After rearranging them into ascending order and eliminating duplicates, the answer should be “32 33 36”. Figure 6: Plausible right views The output should be one line for each dataset. The output may have spaces at ends of lines. Sample Input 4 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 4 3 3 5 2 2 4 3 3 6 1 1 6 1 1 6 1 0 1 0 0 0 2 0 0 0 0 5 1 2 5 1 2 0 0 0 2 0 0 0 3 0 0 0 0 0 0 0 0 0 0 3 0 1 Output for the Sample Input 27 24 32 33 36 0
[ { "submission_id": "aoj_1253_10946393", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\nenum POS {\n TOP = 1,\n FRONT,\n RIGHT,\n LEFT,\n BACK,\n BOTTOM\n};\n\n// dice[top][front][pos];\nint dice[7][7][7];\n\nvoid init() {\n int right[7][7] = {};\n right[1][2] = 3; right[1][3] = 5; right[1][4] = 2; right[1][5] = 4;\n right[2][1] = 4; right[2][3] = 1; right[2][4] = 6; right[2][6] = 3;\n right[3][1] = 2; right[3][2] = 6; right[3][5] = 1; right[3][6] = 5;\n right[4][1] = 5; right[4][2] = 1; right[4][5] = 6; right[4][6] = 2;\n right[5][1] = 3; right[5][3] = 6; right[5][4] = 1; right[5][6] = 4;\n right[6][2] = 4; right[6][3] = 2; right[6][4] = 5; right[6][5] = 3;\n for(int i=1; i<=6; ++i) {\n for(int j=1; j<=6; ++j) {\n dice[i][j][TOP] = i;\n dice[i][j][FRONT] = j;\n dice[i][j][RIGHT] = right[i][j];\n dice[i][j][BOTTOM] = 7 - dice[i][j][TOP];\n dice[i][j][BACK] = 7 - dice[i][j][FRONT];\n dice[i][j][LEFT] = 7 - dice[i][j][RIGHT];\n }\n }\n}\n\nusing pii = pair<int, int>;\n\nint calc(vector<pii> const& cube) {\n int res = 0;\n for(int i=0; i<3; ++i) {\n for(int j=0; j<3; ++j) {\n auto p = cube[i*9 + j*3 + 2];\n res += dice[p.first][p.second][RIGHT];\n }\n }\n return res;\n}\n\nvoid solve(int d, vector<pii> const& init, vector<pii>& cube, vector<int>& res) {\n if(d == 27) {\n res.push_back(calc(cube));\n return;\n }\n for(int i=1; i<=6; ++i) {\n for(int j=1; j<=6; ++j) {\n if(i == j || i + j == 7) {\n continue;\n }\n if(init[d].first != 0 && init[d].first != i) {\n continue;\n }\n if(init[d].second != 0 && init[d].second != j) {\n continue;\n }\n if((d / 3) % 3 != 0) { // not back side of cube\n if(dice[i][j][BACK] + cube[d-3].second != 7) {\n continue;\n }\n }\n if(d % 3 != 0) { // not left side of cube\n if(dice[i][j][LEFT] + dice[cube[d-1].first][cube[d-1].second][RIGHT] != 7) {\n continue;\n }\n }\n if(d >= 9) { // not top of cube\n if(i + dice[cube[d-9].first][cube[d-9].second][BOTTOM] != 7) {\n continue;\n }\n }\n cube[d].first = i;\n cube[d].second = j;\n solve(d+1, init, cube, res);\n }\n }\n}\n\nint main() {\n int N;\n cin >> N;\n init();\n while(N--) {\n vector<pii> cube(27);\n for(int i=0; i<9; ++i) {\n cin >> cube[i].first;\n }\n for(int i=0; i<3; ++i) {\n for(int j=0; j<3; ++j) {\n cin >> cube[i*9 + 6 + j].second;\n }\n }\n auto init = cube;\n vector<int> res;\n solve(0, init, cube, res);\n if(res.empty()) {\n cout << 0 << endl;\n continue;\n }\n sort(res.begin(), res.end());\n res.erase(unique(res.begin(), res.end()), res.end());\n for(int i=0; i<res.size(); ++i) {\n cout << res[i] << \" \\n\"[i == res.size()-1] << flush;\n }\n }\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3428, "score_of_the_acc": -0.0292, "final_rank": 7 }, { "submission_id": "aoj_1253_10055362", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\n\nusing ll=long long;\n#define rep(i,n) for(int i=0;i<n;i++)\n\nvector<array<int,6>> D;\nint T[3][3],F[3][3];\n\nset<int> S;\nvoid dfs(int n,array<int,27>&d){\n if(n==27){\n int sm=0;\n rep(k,9)sm+=D[d[3*k+2]][2];\n S.insert(sm);\n return;\n }\n\n rep(p,24){\n bool OK=1;\n if(n%3!=0&&D[d[n-1]][2]+D[p][3]!=7)OK=0;\n if(n>8&&D[d[n-9]][5]+D[p][0]!=7)OK=0;\n if(n%9>2&&D[d[n-3]][1]+D[p][4]!=7)OK=0;\n if(n%9>5){\n int i=n/9,j=n%9-6;\n if(F[i][j]!=0&&F[i][j]!=D[p][1])OK=0;\n }\n if(n<9){\n int i=n/3,j=n%3;\n if(T[i][j]!=0&&T[i][j]!=D[p][0])OK=0;\n }\n if(OK){\n d[n]=p;\n dfs(n+1,d);\n }\n }\n}\n\nvoid solve(){\n rep(i,3)rep(j,3)cin>>T[i][j];\n rep(i,3)rep(j,3)cin>>F[i][j];\n\n array<int,27> d;\n S.clear();\n dfs(0,d);\n if(S.size()==0)S.insert(0);\n for(int s:S)cout<<s<<\" \\n\"[s==(*(prev(S.end())))];\n}\n\nint main() {\n \n cin.tie(nullptr);\n ios::sync_with_stdio(false);\n\n array<int,6> P;\n rep(i,6)P[i]=i+1;\n do{\n if(P[0]+P[5]!=7||P[1]+P[4]!=7||P[2]+P[3]!=7)continue;\n auto Q=P;\n if(Q[0]>3){\n swap(Q[0],Q[5]);\n swap(Q[2],Q[3]);\n }\n while(Q[1]>3||Q[2]>3){\n auto NQ=Q;\n Q[1]=NQ[2];\n Q[2]=NQ[4];\n Q[4]=NQ[3];\n Q[3]=NQ[1];\n }\n if((Q[0]-Q[1]+3)%3!=2||(Q[1]-Q[2]+3)%3!=2||(Q[2]-Q[0]+3)%3!=2)continue;\n D.push_back(P);\n }while(next_permutation(P.begin(),P.end()));\n \n int N;\n cin>>N;\n rep(i,N)solve();\n\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3376, "score_of_the_acc": -0.0248, "final_rank": 4 }, { "submission_id": "aoj_1253_10055360", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\n\nusing ll=long long;\n#define rep(i,n) for(int i=0;i<n;i++)\n\n\nvector<array<int,6>> D;\nint T[3][3],F[3][3];\n\nset<int> S;\n\nvoid dfs(int n,array<int,27>&d){\n if(n==27){\n \n // rep(i,27)cout<<d[i]<<\" \\n\"[i==26];\n int sm=0;\n rep(k,9)sm+=D[d[3*k+2]][2];\n S.insert(sm);\n return;\n }\n\n rep(p,24){\n bool OK=1;\n if(n%3!=0){\n if(D[d[n-1]][2]+D[p][3]!=7)OK=0;\n }\n if(n>8){\n if(D[d[n-9]][5]+D[p][0]!=7)OK=0;\n }\n if(n%9>2){\n if(D[d[n-3]][1]+D[p][4]!=7)OK=0;\n }\n if(n%9>5){\n int i=n/9;\n int j=n%9-6;\n if(F[i][j]!=0&&F[i][j]!=D[p][1])OK=0;\n }\n if(n<9){\n int i=n/3;\n int j=n%3;\n if(T[i][j]!=0&&T[i][j]!=D[p][0])OK=0;\n }\n if(OK){\n d[n]=p;\n dfs(n+1,d);\n d[n]=-1;\n }\n \n }\n}\n\nvoid solve(){\n rep(i,3)rep(j,3)cin>>T[i][j];\n rep(i,3)rep(j,3)cin>>F[i][j];\n\n array<int,27> d;\n S.clear();\n dfs(0,d);\n if(S.size()==0)S.insert(0);\n for(int s:S)cout<<s<<\" \\n\"[s==(*(prev(S.end())))];\n\n \n}\n\nint main() {\n \n cin.tie(nullptr);\n ios::sync_with_stdio(false);\n\n array<int,6> P;\n rep(i,6)P[i]=i+1;\n do{\n if(P[0]+P[5]!=7||P[1]+P[4]!=7||P[2]+P[3]!=7)continue;\n auto Q=P;\n if(Q[0]>3){\n swap(Q[0],Q[5]);\n // swap(Q[1],Q[4]);\n swap(Q[2],Q[3]);\n }\n while(Q[1]>3||Q[2]>3){\n auto NQ=Q;\n Q[1]=NQ[2];\n Q[2]=NQ[4];\n Q[4]=NQ[3];\n Q[3]=NQ[1];\n }\n if((Q[0]-Q[1]+3)%3!=2||(Q[1]-Q[2]+3)%3!=2||(Q[2]-Q[0]+3)%3!=2)continue;\n D.push_back(P);\n }while(next_permutation(P.begin(),P.end()));\n\n // rep(i,24)rep(j,6)cout<<D[i][j]<<\" \\n\"[j==5];\n int N;\n cin>>N;\n rep(i,N)solve();\n\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3384, "score_of_the_acc": -0.0255, "final_rank": 5 }, { "submission_id": "aoj_1253_9469081", "code_snippet": "#line 2 \"cp-library/src/cp-template.hpp\"\n#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\nusing ld = long double;\nusing uint = unsigned int;\nusing ull = unsigned long long;\nusing i32 = int;\nusing u32 = unsigned int;\nusing i64 = long long;\nusing u64 = unsigned long long;\nusing i128 = __int128_t;\ntemplate < class T > bool chmin(T& a, T b) { if(a > b) { a = b; return true; } return false; }\ntemplate < class T > bool chmax(T& a, T b) { if(a < b) { a = b; return true; } return false; }\ntemplate < class T, class U > T ceil (T x, U y) { return (x > 0 ? (x + y - 1) / y : x / y); }\ntemplate < class T, class U > T floor(T x, U y) { return (x > 0 ? x / y : (x - y + 1) / y); }\nint popcnt(i32 x) { return __builtin_popcount(x); }\nint popcnt(u32 x) { return __builtin_popcount(x); }\nint popcnt(i64 x) { return __builtin_popcountll(x); }\nint popcnt(u64 x) { return __builtin_popcountll(x); }\n\n#line 2 \"cp-library/src/utility/rep_itr.hpp\"\ntemplate < class T > struct itr_rep {\n T i, d;\n constexpr itr_rep(const T i) noexcept : i(i), d(1) {}\n constexpr itr_rep(const T i, const T d) noexcept : i(i), d(d) {}\n void operator++() noexcept { i += d; }\n constexpr int operator*() const noexcept { return i; }\n constexpr bool operator!=(const itr_rep x) const noexcept { return d > 0 ? i < x.i : i > x.i; }\n};\n\ntemplate < class T > struct rep {\n const itr_rep< T > s, t;\n constexpr rep(const T t) noexcept : s(0), t(t) {}\n constexpr rep(const T s, const T t) noexcept : s(s), t(t) {}\n constexpr rep(const T s, const T t, const T d) noexcept : s(s, d), t(t, d) {}\n constexpr auto begin() const noexcept { return s; }\n constexpr auto end () const noexcept { return t; }\n};\n\ntemplate < class T > struct revrep {\n const itr_rep < T > s, t;\n constexpr revrep(const T t) noexcept : s(t - 1, -1), t(-1, -1) {}\n constexpr revrep(const T s, const T t) noexcept : s(t - 1, -1), t(s - 1, -1) {}\n constexpr revrep(const T s, const T t, const T d) noexcept : s(t - 1, -d), t(s - 1, -d) {}\n constexpr auto begin() const noexcept { return s; }\n constexpr auto end () const noexcept { return t; }\n};\n#line 3 \"cp-library/src/utility/io.hpp\"\n\n/* 128bit integer */\nistream& operator>>(istream& is, i128& x) {\n std::string s; is >> s;\n int pm = (s[0] == '-');\n x = 0;\n for(int i : rep(pm, int(s.size()))) x = x * 10 + (s[i] - '0');\n if(pm) x *= -1;\n return is;\n}\nostream& operator<<(ostream& os, const i128& x) {\n if(x == 0) return os << '0';\n i128 y = x;\n if(y < 0) { os << '-'; y *= -1; }\n std::vector<int> ny;\n while(y > 0) { ny.push_back(y % 10); y /= 10; }\n for(int i : revrep(ny.size())) os << ny[i];\n return os;\n}\n\ntemplate < class S, class T > istream& operator>>(istream& is, std::pair< S, T >& x) { is >> x.first >> x.second; return is; }\ntemplate < class S, class T > ostream& operator<<(ostream& os, const std::pair< S, T >& x) { os << x.first << \" \" << x.second; return os; }\n\nnamespace scanner {\n struct sca {\n template < class T > operator T() {\n T s; std::cin >> s; return s;\n }\n };\n struct vec {\n int n;\n vec(int n) : n(n) {}\n template < class T > operator std::vector< T >() {\n std::vector< T > v(n);\n for(T& x : v) std::cin >> x;\n return v;\n }\n };\n struct mat {\n int h, w;\n mat(int h, int w) : h(h), w(w) {}\n template < class T > operator std::vector< std::vector< T > >() {\n std::vector m(h, std::vector< T >(w));\n for(std::vector< T >& v : m) for(T& x : v) std::cin >> x;\n return m;\n }\n };\n struct speedup {\n speedup() {\n std::cin.tie(0);\n std::ios::sync_with_stdio(0);\n }\n } speedup_instance;\n}\nscanner::sca in() { return scanner::sca(); }\nscanner::vec in(int n) { return scanner::vec(n); }\nscanner::mat in(int h, int w) { return scanner::mat(h, w); }\n\nnamespace printer {\n void precision(int d) { std::cout << std::fixed << std::setprecision(d); }\n void flush() { std::cout.flush(); }\n}\n\ntemplate < class T >\nostream& operator<<(ostream& os, const std::vector< T > a) {\n int n = a.size();\n for(int i : rep(n)) { os << a[i]; if(i != n - 1) os << ' '; }\n return os;\n}\n\nint print() { std::cout << '\\n'; return 0; }\ntemplate < class head, class... tail > int print(head&& h, tail&&... t) {\n std::cout << h; if(sizeof...(tail)) std::cout << ' ';\n return print(std::forward<tail>(t)...);\n}\ntemplate < class T > int print_n(const std::vector< T > a) {\n int n = a.size();\n for(int i : rep(n)) std::cout << a[i] << \"\\n\";\n return 0;\n}\n\n\n#line 2 \"cp-library/src/utility/key_val.hpp\"\n\ntemplate < class K, class V >\nstruct key_val {\n K key; V val;\n key_val() {}\n key_val(K key, V val) : key(key), val(val) {}\n template < std::size_t Index >\n std::tuple_element_t< Index, key_val >& get() {\n if constexpr (Index == 0) return key;\n if constexpr (Index == 1) return val;\n }\n};\n\nnamespace std {\n\ntemplate < class K, class V > struct tuple_size < key_val< K, V > > : integral_constant< size_t, 2 > {};\ntemplate < class K, class V > struct tuple_element < 0, key_val< K, V > > { using type = K; };\ntemplate < class K, class V > struct tuple_element < 1, key_val< K, V > > { using type = V; };\n\n}\n#line 2 \"cp-library/src/utility/vec_op.hpp\"\ntemplate < class T > key_val< int, T > max_of(const vector< T >& a) {\n int i = std::max_element(a.begin(), a.end()) - a.begin();\n return {i, a[i]};\n}\ntemplate < class T > key_val< int, T > min_of(const vector< T >& a) {\n int i = std::min_element(a.begin(), a.end()) - a.begin();\n return {i, a[i]};\n}\ntemplate < class S, class T > S sum_of(const vector< T >& a) {\n S sum = 0;\n for(const T x : a) sum += x;\n return sum;\n}\ntemplate < class S, class T > vector< S > freq_of(const vector< T >& a, T L, T R) {\n vector< S > res(R - L, S(0));\n for(const T x : a) res[x - L] += 1;\n return res;\n}\ntemplate < class S, class T > struct prefix_sum {\n vector< S > s;\n prefix_sum(const vector< T >& a) : s(a) {\n s.insert(s.begin(), S(0));\n for(int i : rep(a.size())) s[i + 1] += s[i];\n }\n // [L, R)\n S sum(int L, int R) { return s[R] - s[L]; }\n};\n#line 3 \"cp-library/src/utility/heap.hpp\"\n\ntemplate < class T > using heap_min = std::priority_queue< T, std::vector< T >, std::greater< T > >;\ntemplate < class T > using heap_max = std::priority_queue< T, std::vector< T >, std::less< T > >;\n\n#line 27 \"cp-library/src/cp-template.hpp\"\n\n#line 1 \"cp-library/src/algorithm/bin_search.hpp\"\ntemplate < class T, class F >\nT bin_search(T ok, T ng, F f) {\n while(abs(ng - ok) > 1) {\n T mid = (ok + ng) / 2;\n (f(mid) ? ok : ng) = mid;\n }\n return ok;\n}\n\ntemplate < class T, class F >\nT bin_search_real(T ok, T ng, F f, int step = 80) {\n while(step--) {\n T mid = (ok + ng) / 2;\n (f(mid) ? ok : ng) = mid;\n }\n return ok;\n}\n#line 2 \"cp-library/src/algorithm/argsort.hpp\"\n\ntemplate < class T > std::vector< int > argsort(const std::vector< T > &a) {\n std::vector< int > ids((int)a.size());\n std::iota(ids.begin(), ids.end(), 0);\n std::sort(ids.begin(), ids.end(), [&](int i, int j) {\n return a[i] < a[j] || (a[i] == a[j] && i < j);\n });\n return ids;\n}\n#line 1 \"macro.hpp\"\nnamespace macro {\n\nusing size_type = int;\ntemplate < class container > void sort(container& a) { std::sort(std:: begin(a), std:: end(a)); }\ntemplate < class container > void rsort(container& a) { std::sort(std::rbegin(a), std::rend(a)); }\ntemplate < class container > void reverse(container& a) { std::reverse(std::begin(a), std::end(a)); }\ntemplate < class container > void unique(container& a) {\n std::sort(std::begin(a), std::end(a));\n a.erase(std::unique(std::begin(a), std::end(a)), std::end(a));\n}\ntemplate < class container > container sorted(const container& a) { container b = a; sort(b); return std::move(b); }\ntemplate < class container > container rsorted(const container& a) { container b = a; rsort(b); return std::move(b); }\ntemplate < class container, class compare > void sort(container& a, const compare& cmp) { std::sort(std::begin(a), std::end(a), cmp); }\ntemplate < class container, class compare > container sorted(const container& a, const compare& cmp) { container b = a; sort(b, cmp); return std::move(b); }\ntemplate < class container, class value > size_type lower_bound(const container& a, const value& x) { return std::lower_bound(std::begin(a), std::end(a), x) - std::begin(a); }\ntemplate < class container, class value > size_type upper_bound(const container& a, const value& x) { return std::upper_bound(std::begin(a), std::end(a), x) - std::begin(a); }\n\nconst std::vector<std::pair<size_type, size_type>> dir4 = { {+1, 0}, {-1, 0}, { 0, +1}, { 0, -1} };\nconst std::vector<std::pair<size_type, size_type>> dir8 = { {-1, -1}, {-1, 0}, {-1, +1}, { 0, -1}, { 0, +1}, {+1, -1}, {+1, 0}, {+1, +1} };\n\n#ifdef _DEBUG\n#define debug(x) std::cout << \"[\" << __LINE__ << \"] \" << #x << \": \" << x << std::endl\n#else\n#define debug(x)\n#endif\n\ntemplate < class container > void concat(container& a, const container& b) {\n a.insert(std::end(a), std::begin(b), std::end(b));\n}\nstd::vector<size_type> iota(const size_type n) {\n std::vector<size_type> I(n);\n std::iota(std::begin(I), std::end(I), 0);\n return I;\n}\ntemplate < class container > std::vector<size_type> sort_idx(const container& a) {\n const size_type n = a.size();\n std::vector<size_type> I = iota(n);\n std::sort(std::begin(I), std::end(I), [&](size_type i, size_type j) { return a[i] < a[j] or (a[i] == a[j] and i < j); });\n return I;\n}\ntemplate < class container, class compare > std::vector<size_type> sort_idx(const container& a, const compare& cmp) {\n const size_type n = a.size();\n std::vector<size_type> I = iota(n);\n std::sort(std::begin(I), std::end(I), [&](size_type i, size_type j) { return cmp(a[i], a[j]) or (a[i] == a[j] and i < j); });\n return std::move(I);\n}\n\nstruct grid {\n using size_type = int;\n size_type H, W;\n grid(const size_type H, const size_type W) : H(H), W(W) {}\n bool contains(const size_type i, const size_type j) {\n return 0 <= i and i < H and 0 <= j and j < W;\n }\n};\n\nusing f64 = long double;\n\ntemplate < class T > vector< T >& operator++(vector< T >& a) { for(T& x : a) x++; return a; }\ntemplate < class T > vector< T >& operator--(vector< T >& a) { for(T& x : a) x--; return a; }\ntemplate < class T > vector< T > operator++(vector< T >& a, signed) { vector< T > res = a; for(T& x : a) x++; return res; }\ntemplate < class T > vector< T > operator--(vector< T >& a, signed) { vector< T > res = a; for(T& x : a) x--; return res; }\n\n} // namespace macro\n\nusing namespace macro;\n#line 3 \"A.cpp\"\n\n// https://ei1333.github.io/luzhiled/snippets/other/dice.html\nstruct Dice\n{\n // int x, y;\n int l, r, f, b, d, u;\n\n void RollN()\n {\n // --y;\n int buff = d;\n d = f;\n f = u;\n u = b;\n b = buff;\n }\n\n void RollS()\n {\n // ++y;\n int buff = d;\n d = b;\n b = u;\n u = f;\n f = buff;\n }\n\n void RollL() // ----->\n {\n int buff = f;\n f = l;\n l = b;\n b = r;\n r = buff;\n }\n\n void RollR() // <------\n {\n int buff = f;\n f = r;\n r = b;\n b = l;\n l = buff;\n }\n\n void RollE() // .o -> o.\n {\n // --x;\n int buff = d;\n d = l;\n l = u;\n u = r;\n r = buff;\n }\n\n\n void RollW() // o. -> .o\n {\n // ++x;\n int buff = d;\n d = r;\n r = u;\n u = l;\n l = buff;\n }\n\n\n vector< Dice > makeDice()\n {\n vector< Dice > ret;\n for(int i = 0; i < 6; i++) {\n Dice d(*this);\n if(i == 1) d.RollN();\n if(i == 2) d.RollS();\n if(i == 3) d.RollS(), d.RollS();\n if(i == 4) d.RollL();\n if(i == 5) d.RollR();\n for(int j = 0; j < 4; j++) {\n ret.emplace_back(d);\n d.RollE();\n }\n }\n return (ret);\n }\n};\n\nint main() {\n using men = vector<vector<int>>;\n vector<tuple<men, men, int, men>> A; {\n vector getF(7, vector(7, -1));\n vector getT(7, vector(7, -1));\n vector getR(7, vector(7, -1));\n vector<vector<pair<int,int>>> invF(7), invT(7), invR(7);\n Dice D{4, 3, 2, 5, 6, 1};\n for(Dice d : D.makeDice()) {\n getF[d.u][d.r] = d.f; invF[d.f].push_back({d.u, d.r});\n getT[d.f][d.r] = d.u; invT[d.u].push_back({d.f, d.r});\n getR[d.f][d.u] = d.r; invR[d.r].push_back({d.f, d.u});\n }\n\n vector F(4, vector(4, -1));\n vector T(4, vector(4, -1));\n vector R(4, vector(4, -1));\n for(R[1][1] = 1; R[1][1] <= 6; R[1][1]++) {\n for(auto a : invR[R[1][1]]) { tie(F[1][3], T[3][3]) = a;\n for(auto b : invR[R[1][1]]) { tie(F[1][2], T[3][2]) = b;\n for(auto c : invR[R[1][1]]) { tie(F[1][1], T[3][1]) = c;\n for(auto d : invT[T[3][3]]) { tie(F[2][3], R[2][1]) = d;\n for(auto e : invT[T[3][3]]) { tie(F[3][3], R[3][1]) = e;\n for(auto f : invF[F[1][3]]) { tie(T[2][3], R[1][2]) = f;\n for(auto g : invF[F[1][3]]) { tie(T[1][3], R[1][3]) = g;\n T[1][1] = getT[F[1][1]][R[1][3]];\n T[2][1] = getT[F[1][1]][R[1][2]];\n T[1][2] = getT[F[1][2]][R[1][3]];\n T[2][2] = getT[F[1][2]][R[1][2]];\n\n F[2][1] = getF[T[3][1]][R[2][1]];\n F[3][1] = getF[T[3][1]][R[3][1]];\n F[2][2] = getF[T[3][2]][R[2][1]];\n F[3][2] = getF[T[3][2]][R[3][1]];\n\n R[2][2] = getR[F[2][3]][T[2][3]];\n R[3][2] = getR[F[3][3]][T[2][3]];\n R[2][3] = getR[F[2][3]][T[1][3]];\n R[3][3] = getR[F[3][3]][T[1][3]];\n\n int sumR = 0;\n for(int i = 1; i <= 3; i++) {\n for(int j = 1; j <= 3; j++) {\n sumR += R[i][j];\n }\n }\n\n bool ok = [&] {\n for(int i = 1; i <= 3; i++) {\n for(int j = 1; j <= 3; j++) {\n if(F[i][j] == -1 or T[i][j] == -1 or R[i][j] == -1) return false;\n }\n }\n for(int i = 1; i <= 3; i++) {\n if(getT[F[i][1]][R[i][3]] != T[1][1]) return false;\n if(getT[F[i][1]][R[i][2]] != T[2][1]) return false;\n if(getT[F[i][1]][R[i][1]] != T[3][1]) return false;\n\n if(getT[F[i][2]][R[i][3]] != T[1][2]) return false;\n if(getT[F[i][2]][R[i][2]] != T[2][2]) return false;\n if(getT[F[i][2]][R[i][1]] != T[3][2]) return false;\n\n if(getT[F[i][3]][R[i][3]] != T[1][3]) return false;\n if(getT[F[i][3]][R[i][2]] != T[2][3]) return false;\n if(getT[F[i][3]][R[i][1]] != T[3][3]) return false;\n }\n return true;\n }();\n\n if(ok) A.push_back({T, F, sumR, R});\n }\n }\n }\n }\n }\n }\n }\n }\n }\n\n int N = in();\n for(int _ : rep(N)) {\n men t(4, vector(4, 0));\n men f(4, vector(4, 0));\n for(int i = 1; i <= 3; i++) for(int j = 1; j <= 3; j++) t[i][j] = in();\n for(int i = 1; i <= 3; i++) for(int j = 1; j <= 3; j++) f[i][j] = in();\n vector<int> ans;\n for(auto &[T, F, sumR, R] : A) {\n bool ok = [&] {\n for(int i = 1; i <= 3; i++) for(int j = 1; j <= 3; j++) {\n if(t[i][j] != 0 and T[i][j] != t[i][j]) return false;\n if(f[i][j] != 0 and F[i][j] != f[i][j]) return false;\n }\n return true;\n }();\n if(ok) ans.push_back(sumR);\n }\n unique(ans);\n if(ans.empty()) ans.push_back(0);\n print(ans);\n }\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 15004, "score_of_the_acc": -1, "final_rank": 19 }, { "submission_id": "aoj_1253_9006820", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\nusing P = pair<ll, ll>;\n#define rep(i, a, b) for(ll i = a; i < b; ++i)\n#define rrep(i, a, b) for(ll i = a; i >= b; --i)\nconstexpr ll inf = 4e18;\nstruct SetupIO {\n SetupIO() {\n ios::sync_with_stdio(0);\n cin.tie(0);\n cout << fixed << setprecision(30);\n }\n} setup_io;\n#define roll_swap(x, a, b, c, d) swap(x.a, x.b), swap(x.b, x.c), swap(x.c, x.d);\nstruct Dice {\n int top, front, right, left, back, bottom;\n Dice(int to = 1, int fr = 2, int ri = 3, int le = 4, int ba = 5, int bo = 6)\n : top(to), front(fr), right(ri), left(le), back(ba), bottom(bo) {}\n void roll_right() {\n roll_swap((*this), top, left, bottom, right);\n }\n void roll_left() {\n roll_swap((*this), top, right, bottom, left);\n }\n void roll_front() {\n roll_swap((*this), top, back, bottom, front);\n }\n void roll_back() {\n roll_swap((*this), top, front, bottom, back);\n }\n void roll_cw() {\n roll_swap((*this), back, left, front, right);\n }\n void roll_ccw() {\n roll_swap((*this), back, right, front, left);\n }\n void roll(int dir) {\n if(dir == 0) (*this).roll_front();\n if(dir == 1) (*this).roll_right();\n if(dir == 2) (*this).roll_back();\n if(dir == 3) (*this).roll_left();\n }\n friend bool operator<(const Dice& d1, const Dice& d2) {\n int vd1[] = {d1.top, d1.front, d1.right, d1.left, d1.back, d1.bottom};\n int vd2[] = {d2.top, d2.front, d2.right, d2.left, d2.back, d2.bottom};\n return vector<int>(vd1, vd1 + 6) < vector<int>(vd2, vd2 + 6);\n }\n};\n\nvector<Dice> all_rotate() {\n vector<Dice> res;\n res.reserve(24);\n Dice d;\n for(int i = 0; i < 6; ++i) {\n for(int j = 0; j < 4; ++j) {\n res.emplace_back(d);\n d.roll_cw();\n }\n if(i & 1) d.roll_front();\n else d.roll_right();\n }\n return res;\n}\nint main(void) {\n const vector<Dice> dice = all_rotate();\n int TC;\n cin >> TC;\n while(TC--) {\n constexpr int n = 3;\n vector<vector<int>> t(n, vector<int>(n)), f(n, vector<int>(n));\n rep(i, 0, n) {\n rep(j, 0, n) {\n cin >> t[i][j];\n }\n }\n rep(i, 0, n) {\n swap(t[0][i], t[n - 1][i]);\n }\n rep(i, 0, n) {\n rep(j, 0, n) {\n cin >> f[i][j];\n }\n }\n vector<vector<int>> r(n, vector<int>(n));\n vector<vector<int>> cntt(n, vector<int>(n)), cntf(n, vector<int>(n)), cntr(n, vector<int>(n));\n rep(i, 0, n) {\n rep(j, 0, n) {\n if(t[i][j] != 0) cntt[i][j] = 1;\n if(f[i][j] != 0) cntf[i][j] = 1;\n }\n }\n vector<vector<vector<int>>> d(n, vector<vector<int>>(n, vector<int>(n, -1)));\n vector<int> ans;\n auto dfs = [&](auto& dfs, int idx) -> void {\n int x = idx % 3, y = idx / 3 % 3, z = idx / 9;\n if(t[x][y] != 0 and dice[d[x][y][z]].top != t[x][y]) return;\n if(f[z][y] != 0 and dice[d[x][y][z]].front != f[z][y]) return;\n if(r[z][x] != 0 and dice[d[x][y][z]].right != r[z][x]) return;\n if(x > 0 and d[x - 1][y][z] != -1 and dice[d[x - 1][y][z]].back + dice[d[x][y][z]].front != 7) return;\n if(x < n - 1 and d[x + 1][y][z] != -1 and dice[d[x][y][z]].back + dice[d[x + 1][y][z]].front != 7) return;\n if(y > 0 and d[x][y - 1][z] != -1 and dice[d[x][y - 1][z]].right + dice[d[x][y][z]].left != 7) return;\n if(y < n - 1 and d[x][y + 1][z] != -1 and dice[d[x][y][z]].right + dice[d[x][y + 1][z]].left != 7) return;\n if(z > 0 and d[x][y][z - 1] != -1 and dice[d[x][y][z - 1]].bottom + dice[d[x][y][z]].top != 7) return;\n if(z < n - 1 and d[x][y][z + 1] != -1 and dice[d[x][y][z]].bottom + dice[d[x][y][z + 1]].top != 7) return;\n if(idx == n * n * n - 1) {\n int sum = 0;\n rep(i, 0, n) {\n rep(j, 0, n) {\n sum += r[i][j];\n }\n }\n ans.push_back(sum);\n return;\n }\n t[x][y] = dice[d[x][y][z]].top;\n cntt[x][y]++;\n f[z][y] = dice[d[x][y][z]].front;\n cntf[z][y]++;\n r[z][x] = dice[d[x][y][z]].right;\n cntr[z][x]++;\n int nidx = idx + 1;\n int nx = nidx % 3, ny = nidx / 3 % 3, nz = nidx / 9;\n rep(i, 0, 24) {\n d[nx][ny][nz] = i;\n dfs(dfs, nidx);\n d[nx][ny][nz] = -1;\n }\n if(--cntt[x][y] == 0) t[x][y] = 0;\n if(--cntf[z][y] == 0) f[z][y] = 0;\n if(--cntr[z][x] == 0) r[z][x] = 0;\n };\n rep(i, 0, 24) {\n d[0][0][0] = i;\n dfs(dfs, 0);\n d[0][0][0] = -1;\n }\n if(ans.empty()) {\n ans.push_back(0);\n }\n sort(ans.begin(), ans.end());\n ans.erase(unique(ans.begin(), ans.end()), ans.end());\n rep(i, 0, (int)ans.size()) {\n cout << ans[i] << \" \\n\"[i + 1 == (int)ans.size()];\n }\n }\n}", "accuracy": 1, "time_ms": 40, "memory_kb": 3512, "score_of_the_acc": -0.0504, "final_rank": 14 }, { "submission_id": "aoj_1253_6400375", "code_snippet": "#include<bits/stdc++.h>\n#define rep(i,a,...) for(int i = (a)*(strlen(#__VA_ARGS__)!=0);i<(int)(strlen(#__VA_ARGS__)?__VA_ARGS__:(a));++i)\n#define per(i,a,...) for(int i = (strlen(#__VA_ARGS__)?__VA_ARGS__:(a))-1;i>=(int)(strlen(#__VA_ARGS__)?(a):0);--i)\n#define foreach(i, n) for(auto &i:(n))\n#define all(x) (x).begin(), (x).end()\n#define bit(x) (1ll << (x))\n#define lambda(RES_TYPE, ...) (function<RES_TYPE(__VA_ARGS__)>)[&](__VA_ARGS__) -> RES_TYPE\n#define method(FUNC_NAME, RES_TYPE, ...) function<RES_TYPE(__VA_ARGS__)> FUNC_NAME = lambda(RES_TYPE, __VA_ARGS__)\nusing namespace std;\nusing ll = long long;\nusing pii = pair<int,int>;\nusing pll = pair<ll,ll>;\n//const ll MOD = (ll)1e9+7;\nconst ll MOD = 998244353;\nconst int INF = (ll)1e9+7;\nconst ll INFLL = (ll)1e18;\ntemplate<class t>\nusing vvector = vector<vector<t>>;\ntemplate<class t>\nusing vvvector = vector<vector<vector<t>>>;\ntemplate<class t>\nusing priority_queuer = priority_queue<t, vector<t>, greater<t>>;\ntemplate<class t, class u> bool chmax(t &a, u b){if(a<b){a=b;return true;}return false;}\ntemplate<class t, class u> bool chmin(t &a, u b){if(a>b){a=b;return true;}return false;}\n#ifdef DEBUG\n#define debug(x) cout<<\"LINE \"<<__LINE__<<\": \"<<#x<<\" = \"<<x<<endl;\n#else\n#define debug(x) (void)0\n#endif\n\nnamespace templates{\n ll modpow(ll x, ll b,ll mod=MOD){\n ll res = 1;\n while(b){\n if(b&1)res = res * x % mod;\n x = x * x % mod;\n b>>=1;\n }\n return res;\n }\n\n ll modinv(ll x){\n return modpow(x, MOD-2);\n }\n\n bool was_output = false;\n\n void print();\n template <class t> void print(const vector<t> &);\n template <class t, class u> void print(const pair<t, u> &);\n template <class t> void print(const t&);\n template <class Head, class... Tail> void print(const Head&, const Tail&...);\n\n template <class t> void println(const vector<vector<t>>&);\n template <class t> void println(const vector<t>&);\n template <class t> void println(const t&);\n template <class Head, class... Tail> void println(const Head&, const Tail&...);\n void println();\n void newline();\n\n void print(){\n return;\n }\n\n template <class t>\n void print(const vector<t>&x){\n for(const t&i:x)print(i);\n }\n template <class t, class u>\n void print(const pair<t,u>&p){\n print(p.first);\n print(p.second);\n }\n template <class t>\n void print(const t&x){\n if(was_output)cout<<\" \";\n cout<<x;\n was_output = true;\n }\n template <class Head, class... Tail>\n void print(const Head&head,const Tail&...tail){\n print(head);\n print(tail...);\n }\n\n template<class t>\n void println(const vector<vector<t>>&x){\n for(vector<t> i:x)println(i);\n }\n template<class t>\n void println(const vector<t>&x){\n for(const t&i:x)print(i);\n println();\n }\n template <class t>\n void println(const t&x){\n print(x);\n println();\n }\n void println(){\n if(was_output){\n cout << endl;\n was_output = false;\n }\n }\n template <class Head, class... Tail>\n void println(const Head&head,const Tail&...tail){\n print(head);\n println(tail...);\n }\n\n void newline(){\n was_output = true;\n println();\n }\n\n template<class t>\n istream& operator>>(istream&is, vector<t>&x){\n for(auto &i:x)is >> i;\n return is;\n }\n\n template<class t, class u>\n istream& operator>>(istream&is, pair<t, u>&x){\n is >> x.first >> x.second;\n return is;\n }\n\n template<class t>\n ostream& operator<<(ostream&os, vector<t> &v){\n os << \"{\";\n for(t &i:v){\n os << i << \", \";\n }\n os << \"}\";\n return os;\n }\n\n template<class t = long long>\n t in(){\n t res; cin >> res; return res;\n }\n\n template<class t>\n vector<t> sorted(vector<t> line,function<bool(t,t)> comp=[](t a,t b){return a<b;}){\n sort(line.begin(),line.end(),comp);\n return line;\n }\n\n template<class t>\n vector<t> reversed(vector<t> line){\n reverse(line.begin(),line.end());\n return line;\n }\n string reversed(string str){\n reverse(str.begin(),str.end());\n return str;\n }\n\n long long gcd(long long a,long long b){\n while(b){\n a %= b;\n swap(a,b);\n }\n return a;\n }\n\n long long lcm(long long a,long long b){\n return a / gcd(a,b) * b;\n }\n\n class output_initializer{\n public:\n output_initializer(){\n ios::sync_with_stdio(false);\n cin.tie(nullptr);\n cout << setprecision(20);\n }\n };output_initializer OUTPUT_INITIALIZER_INSTANCE = output_initializer();\n}\n\nusing namespace templates;\n\nclass Dice{\nprivate:\n static set<Dice> dices;\npublic:\n array<int,6> faces;\n Dice(){\n rep(i,6){\n faces[i] = i + 1;\n }\n }\n Dice(int v1,int v2,int v3,int v4,int v5,int v6){\n faces = array<int,6>({v1,v2,v3,v4,v5,v6});\n }\n int operator[](int i) const{\n return faces[i];\n }\n Dice rotate(char dir){\n if(dir=='R'){\n return Dice(faces[3],faces[1],faces[0],faces[5],faces[4],faces[2]);\n }\n if(dir=='L'){\n return this->rotate('R').rotate('R').rotate('R');\n }\n if(dir=='F'){\n return Dice(faces[4],faces[0],faces[2],faces[3],faces[5],faces[1]);\n }\n if(dir=='B'){\n return this->rotate('F').rotate('F').rotate('F');\n }\n exit(1);\n }\n\n static void initialize(){\n Dice d;\n while(dices.size()<24){\n d = d.rotate(\"RLFB\"[rand()%4]);\n dices.emplace(d);\n }\n }\n static vector<Dice> Finds(int top,int front,int left=0){\n if(dices.empty())initialize();\n vector<Dice> res;\n foreach(d,dices){\n bool flag = true;\n if(top>0 and d[0]!=top)flag = false;\n if(front>0 and d[1]!=front)flag =false;\n if(left>0 and d[3]!=left)flag =false;\n if(flag)res.emplace_back(d);\n }\n return res;\n }\n};\n\nset<Dice> Dice::dices;\n\nbool operator<(const Dice a,const Dice b){\n return a.faces < b.faces;\n}\n\nset<int> func(){\n set<int> res;\n vvector<int> tops(3,vector<int>(3));\n vvector<int> fronts(3,vector<int>(3));\n foreach(i,tops)foreach(j,i)j=in();\n foreach(i,fronts)foreach(j,i)j=in();\n vvvector<Dice> dices(3,vvector<Dice>(3,vector<Dice>(3)));\n method(rec,void,int y){\n if(y==3){\n int sum = 0;\n rep(i,3){\n rep(j,3){\n sum += dices[i][2][j][2];\n }\n }\n res.emplace(sum);\n return;\n }\n method(rec2,void,int j,int k){\n if(k<0){\n rec(y+1);\n return;\n }\n int front = k==2 ? fronts[y][j] : 7-dices[y][j][k+1][4];\n int top = y==0 ? tops[k][j] : 7-dices[y-1][j][k][5];\n int left = j==0 ? 0 : 7-dices[y][j-1][k][2];\n foreach(d,Dice::Finds(top,front,left)){\n dices[y][j][k] = d;\n rec2((j+1)%3,k-(j+1)/3);\n }\n };\n rec2(0,2);\n };\n rec(0);\n return res;\n}\n\nint main(){\n ios::sync_with_stdio(false);\n cin.tie(nullptr);\n\n int n = in();\n\n rep(i,n){\n auto ans = func();\n if(ans.empty())ans.emplace(0);\n foreach(i,ans)print(i);println();\n }\n\n return 0;\n}", "accuracy": 1, "time_ms": 20, "memory_kb": 3308, "score_of_the_acc": -0.0239, "final_rank": 3 }, { "submission_id": "aoj_1253_6359400", "code_snippet": "#include \"bits/stdc++.h\"\nusing namespace std;\n\ntypedef string::const_iterator State;\n#define eps 1e-8L\n#define MAX_MOD 1000000007LL\n#define GYAKU 500000004LL\n#define MOD 998244353LL\n#define pb push_back\n#define mp make_pair\ntypedef long long ll;\ntypedef long double ld;\n#define REP(a, b) for (long long(a) = 0; (a) < (b); ++(a))\n#define ALL(x) (x).begin(), (x).end()\n\n#define int long long\n\nstatic const int dice[][6] = {\n {1, 2, 3, 5, 4, 6},\n {1, 3, 5, 4, 2, 6},\n {1, 5, 4, 2, 3, 6},\n {1, 4, 2, 3, 5, 6},\n\n {2, 1, 4, 6, 3, 5},\n {2, 4, 6, 3, 1, 5},\n {2, 6, 3, 1, 4, 5},\n {2, 3, 1, 4, 6, 5},\n\n {3, 1, 2, 6, 5, 4},\n {3, 2, 6, 5, 1, 4},\n {3, 6, 5, 1, 2, 4},\n {3, 5, 1, 2, 6, 4},\n\n {4, 2, 1, 5, 6, 3},\n {4, 1, 5, 6, 2, 3},\n {4, 5, 6, 2, 1, 3},\n {4, 6, 2, 1, 5, 3},\n\n {5, 4, 1, 3, 6, 2},\n {5, 1, 3, 6, 4, 2},\n {5, 3, 6, 4, 1, 2},\n {5, 6, 4, 1, 3, 2},\n\n {6, 2, 4, 5, 3, 1},\n {6, 4, 5, 3, 2, 1},\n {6, 5, 3, 2, 4, 1},\n {6, 3, 2, 4, 5, 1},\n};\n\nset<int> ans;\n\nvoid checkers(int x, int y, int z, vector<vector<vector<int>>> cur, vector<vector<int>> tops, vector<vector<int>> fronts)\n{\n if (x == 3)\n {\n checkers(0, y + 1, z, cur, tops, fronts);\n return;\n }\n if (y == 3)\n {\n checkers(0, 0, z + 1, cur, tops, fronts);\n return;\n }\n if (z == 3)\n {\n int cur_ans = 0;\n for (int y = 0; y < 3; y++)\n {\n for (int z = 0; z < 3; ++z)\n {\n cur_ans += dice[cur[2][y][z]][2];\n }\n }\n ans.insert(cur_ans);\n return;\n }\n\n REP(i, 24)\n {\n if (z == 0)\n {\n if (tops[2 - y][x] != 0)\n {\n if (tops[2 - y][x] != dice[i][0])\n continue;\n }\n }\n\n if (y == 0)\n {\n if (fronts[2 - z][x] != 0)\n {\n if (fronts[2 - z][x] != dice[i][1])\n continue;\n }\n }\n\n if (x != 0)\n {\n if (dice[cur[x - 1][y][z]][2] + dice[i][4] != 7)\n {\n continue;\n }\n }\n\n if (y != 0)\n {\n if (dice[cur[x][y - 1][z]][3] + dice[i][1] != 7)\n {\n continue;\n }\n }\n\n if (z != 0)\n {\n if (dice[cur[x][y][z - 1]][5] + dice[i][0] != 7)\n {\n continue;\n }\n }\n\n cur[x][y][z] = i;\n checkers(x + 1, y, z, cur, tops, fronts);\n }\n}\n\nvoid solve()\n{\n vector<vector<int>> tops, fronts;\n REP(t, 2)\n {\n REP(i, 3)\n {\n tops.push_back({});\n REP(q, 3)\n {\n int a;\n cin >> a;\n tops.back().push_back(a);\n }\n }\n swap(tops, fronts);\n }\n\n vector<vector<vector<int>>> cur(3, vector<vector<int>>(3, vector<int>(3)));\n checkers(0, 0, 0, cur, tops, fronts);\n int is_first = 1;\n if (ans.size() == 0)\n ans.insert(0);\n for (auto x : ans)\n {\n if (!is_first)\n {\n cout << \" \";\n }\n else\n is_first = 0;\n cout << x;\n }\n cout << endl;\n ans.clear();\n}\n#undef int\n\n// generated by oj-template v4.7.2\n// (https://github.com/online-judge-tools/template-generator)\nint main()\n{\n // Fasterize input/output script\n ios::sync_with_stdio(false);\n cin.tie(nullptr);\n cout << fixed << setprecision(100);\n // scanf/printf user should delete this fasterize input/output script\n\n int t = 1;\n cin >> t; // comment out if solving multi testcase\n for (int testCase = 1; testCase <= t; ++testCase)\n {\n solve();\n }\n return 0;\n}", "accuracy": 1, "time_ms": 150, "memory_kb": 3212, "score_of_the_acc": -0.0774, "final_rank": 15 }, { "submission_id": "aoj_1253_6019968", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\n#include<ext/pb_ds/assoc_container.hpp>\nusing namespace __gnu_pbds;\n\ntemplate<class T>\nusing ordered_set = tree<T, null_type, less<T>, rb_tree_tag,\ntree_order_statistics_node_update>;\n// usage:\n// ordered_set::find_by_order (access)\n// ordered_set::order_of_key (lower_bound)\n\ntemplate<class K, class V>\nusing hashmap = gp_hash_table<K, V>;\n\n#define REP(i, n) for (int i = 0; i < int(n); ++i)\n#define ALL(x) begin(x), end(x)\n#define DUMP(x) cerr << #x << \" = \" << (x) << endl\n//#define ASSERT(x) while (!(x)) {}\n#define ASSERT(x) assert(x)\n\nstruct fast_ios {\n fast_ios() {\n cin.tie(nullptr);\n ios::sync_with_stdio(false);\n cout << fixed << setprecision(20);\n };\n} fast_ios_;\n\nusing ll = long long;\n\ntemplate <class T> bool chmin(T& a, T b) {\n if (a > b) {\n a = b;\n return true;\n }\n return false;\n}\ntemplate <class T> bool chmax(T& a, T b) {\n if (a < b) {\n a = b;\n return true;\n }\n return false;\n}\n\ntemplate <class T> ostream& operator<<(ostream& os, const vector<T>& v);\ntemplate <class T, class U>\nostream& operator<<(ostream& os, const pair<T, U>& p);\ntemplate <class T, class U>\nostream& operator<<(ostream& os, const map<T, U>& mp);\n\ntemplate <class T> ostream& operator<<(ostream& os, const vector<T>& v) {\n os << '[';\n REP(i, v.size()) {\n if (i) os << ',';\n os << v[i];\n }\n return os << ']';\n}\n\ntemplate <class T, class U>\nostream& operator<<(ostream& os, const pair<T, U>& p) {\n return os << '(' << p.first << ' ' << p.second << ')';\n}\n\ntemplate <class T> ostream& operator<<(ostream& os, const set<T>& st) {\n os << '{';\n int a = 0;\n for (const auto& e : st) {\n if (a) os << ' ';\n a = 1;\n os << e;\n }\n return os << '}';\n}\n\ntemplate <class T, class U>\nostream& operator<<(ostream& os, const map<T, U>& mp) {\n os << '{';\n int a = 0;\n for (const auto& p : mp) {\n if (a) os << ' ';\n a = 1;\n os << p;\n }\n return os << '}';\n}\n\nconst int INF = numeric_limits<int>::max();\nconst ll LINF = numeric_limits<ll>::max();\n\nvoid solve() {\n vector<vector<int>> T(3, vector<int>(3)), F(3, vector<int>(3));\n REP(i, 3) REP(j, 3) cin >> T[i][j];\n REP(i, 3) REP(j, 3) cin >> F[i][j];\n\n using Dice = vector<int>;\n vector<Dice> dice{\n {1,2,3,5,4,6},\n {1,3,5,4,2,6},\n {1,5,4,2,3,6},\n {1,4,2,3,5,6},\n\n {2,3,1,4,6,5},\n {2,1,4,6,3,5},\n {2,4,6,3,1,5},\n {2,6,3,1,4,5},\n\n {3,2,6,5,1,4},\n {3,6,5,1,2,4},\n {3,5,1,2,6,4},\n {3,1,2,6,5,4},\n\n {4,2,1,5,6,3},\n {4,1,5,6,2,3},\n {4,5,6,2,1,3},\n {4,6,2,1,5,3},\n\n {5,1,3,6,4,2},\n {5,3,6,4,1,2},\n {5,6,4,1,3,2},\n {5,4,1,3,6,2},\n\n {6,2,4,5,3,1},\n {6,4,5,3,2,1},\n {6,5,3,2,4,1},\n {6,3,2,4,5,1},\n };\n\n vector<vector<vector<int>>> state(3,vector<vector<int>>(3, vector<int>(3, -1)));\n\n map<int,bool> ans;\n\n auto dfs = [&](auto dfs, int i, int j, int k) -> void {\n if (k == 3) return dfs(dfs, i, j+1, 0);\n if (j == 3) return dfs(dfs, i+1, 0, 0);\n if (i == 3) {\n // terminal\n\n int right = 0;\n REP(x, 3) REP(y, 3) {\n right += dice[state[x][y][2]][2];\n }\n ans[right] = true;\n return;\n }\n // cout << \"i,j,k: \" << i << ',' << j << ',' << k << endl;\n\n // int cnt = 0;\n map<int,bool> cnt;\n REP(l, 24) {\n // top\n if (i > 0 and dice[state[i-1][j][k]][5] + dice[l][0] != 7) continue;\n // back\n if (j > 0 and dice[state[i][j-1][k]][1] + dice[l][3] != 7) continue;\n // left\n if (k > 0 and dice[state[i][j][k-1]][2] + dice[l][4] != 7) continue;\n\n // condition T\n if (T[j][k] != 0 and dice[l][0] != T[j][k]) continue;\n // condition F\n if (F[i][k] != 0 and dice[l][1] != F[i][k]) continue;\n\n //++cnt;\n cnt[l] = true;\n\n state[i][j][k] = l;\n dfs(dfs, i, j, k+1);\n state[i][j][k] = -1;\n }\n\n if (i*j > 0 or j*k > 0 or k*i > 0) assert(cnt.size() <= 1);\n };\n\n dfs(dfs, 0, 0, 0);\n\n if (ans.empty()) {\n cout << 0 << endl;\n return;\n }\n\n bool first = true;\n for (auto [a, _] : ans) {\n if (!first) cout << ' ';\n first = false;\n cout << a;\n }\n cout << endl;\n}\n\nint main() {\n int n; cin >> n;\n while (n--) {\n solve();\n }\n};", "accuracy": 1, "time_ms": 20, "memory_kb": 3400, "score_of_the_acc": -0.0316, "final_rank": 8 }, { "submission_id": "aoj_1253_6010239", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\n#define rep(i, s, t) for (int i = (int)(s); i < (int)(t); ++i)\n\nint T[3][3];\nint F[3][3];\nint R[3][3];\n\nint get_right(int t, int f) {\n if (t == 1) {\n if (f == 2) return 3;\n if (f == 3) return 5;\n if (f == 5) return 4;\n if (f == 4) return 2;\n } else if (t == 2) {\n if (f == 1) return 4;\n if (f == 4) return 6;\n if (f == 6) return 3;\n if (f == 3) return 1;\n } else if (t == 3) {\n if (f == 1) return 2;\n if (f == 2) return 6;\n if (f == 6) return 5;\n if (f == 5) return 1;\n } else if (t == 4) {\n if (f == 1) return 5;\n if (f == 5) return 6;\n if (f == 6) return 2;\n if (f == 2) return 1;\n } else if (t == 5) {\n if (f == 1) return 3;\n if (f == 3) return 6;\n if (f == 6) return 4;\n if (f == 4) return 1;\n } else if (t == 6) {\n if (f == 2) return 4;\n if (f == 4) return 5;\n if (f == 5) return 3;\n if (f == 3) return 2;\n }\n return 0;\n}\n\nvector<int> dfs(int a, int b, int c) {\n if (c == 3) return dfs(a, b+1, 0);\n if (b == 3) return dfs(a+1, 0, 0);\n if (a==3) {\n int ans = 0;\n rep(i,0,3) {\n rep(k,0,3) {\n ans += get_right(T[2][k], F[i][2]);\n }\n }\n return {ans};\n }\n if (T[b][c] && F[a][b]) {\n if (R[a][c] > 0 && get_right(T[b][c], F[a][b]) != R[a][c]) return {};\n }\n vector<int> ans;\n int t = T[b][c];\n int f = F[a][b];\n int r = R[a][c];\n rep(i,1,7) rep(j,1,7) {\n if (i==j || i+j == 7) continue;\n if (T[b][c] && T[b][c] != i) continue;\n if (F[a][b] && F[a][b] != j) continue;\n if (R[a][c] && get_right(i, j) != R[a][c]) continue;\n T[b][c] = i;\n F[a][b] = j;\n R[a][c] = get_right(i, j);\n auto x = dfs(a, b, c+1);\n for (int y : x) ans.push_back(y);\n T[b][c] = t;\n F[a][b] = f;\n R[a][c] = r;\n }\n return ans;\n}\n\nint main() {\n ios_base::sync_with_stdio(false);\n cin.tie(nullptr);\n cout << fixed << setprecision(15);\n\n int N;\n cin >> N;\n while (N--) {\n rep(i,0,3)rep(j,0,3) cin >> T[j][2-i];\n rep(i,0,3)rep(j,0,3) cin >> F[i][j];\n auto ans = dfs(0, 0, 0);\n if (ans.size() == 0) {\n cout << \"0\\n\";\n continue;\n }\n sort(ans.begin(), ans.end());\n ans.erase(unique(ans.begin(), ans.end()), ans.end());\n rep(i,0,ans.size()) cout << ans[i] << (i < ans.size() - 1 ? \" \" : \"\\n\");\n }\n}", "accuracy": 1, "time_ms": 20, "memory_kb": 3592, "score_of_the_acc": -0.0477, "final_rank": 13 }, { "submission_id": "aoj_1253_6010212", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\n#define rep(i, s, t) for (int i = (int)(s); i < (int)(t); ++i)\n\nint T[3][3][3];\nint F[3][3][3];\nint R[3][3][3];\n\nint get_right(int t, int f) {\n if (t == 1) {\n if (f == 2) return 3;\n if (f == 3) return 5;\n if (f == 5) return 4;\n if (f == 4) return 2;\n } else if (t == 2) {\n if (f == 1) return 4;\n if (f == 4) return 6;\n if (f == 6) return 3;\n if (f == 3) return 1;\n } else if (t == 3) {\n if (f == 1) return 2;\n if (f == 2) return 6;\n if (f == 6) return 5;\n if (f == 5) return 1;\n } else if (t == 4) {\n if (f == 1) return 5;\n if (f == 5) return 6;\n if (f == 6) return 2;\n if (f == 2) return 1;\n } else if (t == 5) {\n if (f == 1) return 3;\n if (f == 3) return 6;\n if (f == 6) return 4;\n if (f == 4) return 1;\n } else if (t == 6) {\n if (f == 2) return 4;\n if (f == 4) return 5;\n if (f == 5) return 3;\n if (f == 3) return 2;\n }\n return 0;\n}\n\nvector<int> dfs(int a, int b, int c) {\n if (c == 3) return dfs(a, b+1, 0);\n if (b == 3) return dfs(a+1, 0, 0);\n if (a==3) {\n int ans = 0;\n rep(i,0,3) {\n rep(j,0,3) {\n ans += R[i][2][j];\n }\n }\n return {ans};\n }\n int t = T[a][b][c], f = F[a][b][c], r = R[a][b][c];\n if (a > 0) T[a][b][c] = T[a-1][b][c];\n if (c > 0) F[a][b][c] = F[a][b][c-1];\n if (b > 0) R[a][b][c] = R[a][b-1][c];\n if (T[a][b][c] * F[a][b][c] > 0) {\n int x = get_right(T[a][b][c], F[a][b][c]);\n if (x == 0 || R[a][b][c] > 0 && x != R[a][b][c]) return {};\n R[a][b][c] = x;\n } else if (F[a][b][c] * R[a][b][c] > 0) {\n int x = get_right(F[a][b][c], R[a][b][c]);\n if (x == 0 || T[a][b][c] > 0 && x != T[a][b][c]) return {};\n T[a][b][c] = x;\n } else if (R[a][b][c] * T[a][b][c] > 0) {\n int x = get_right(R[a][b][c], T[a][b][c]);\n if (x == 0 || F[a][b][c] > 0 && x != F[a][b][c]) return {};\n F[a][b][c] = x;\n }\n int t2 = T[a][b][c], f2 = F[a][b][c], r2 = R[a][b][c];\n vector<int> ans;\n rep(i,1,7) rep(j,1,7) {\n if (i==j || i+j == 7) continue;\n if (T[a][b][c] > 0 && T[a][b][c] != i) continue;\n if (F[a][b][c] > 0 && F[a][b][c] != j) continue;\n if (R[a][b][c] >0 && get_right(i, j) != R[a][b][c]) continue;\n T[a][b][c] = i;\n F[a][b][c] = j;\n R[a][b][c] = get_right(i, j);\n // cout << \"(\" << a << \",\" << b << \",\" << c << \"),\" << i << \" \" << j << get_right(i,j) << endl;\n auto x = dfs(a, b, c+1);\n for (int y : x) ans.push_back(y);\n T[a][b][c] = t2;\n F[a][b][c] = f2;\n R[a][b][c] = r2;\n }\n T[a][b][c] = t;\n F[a][b][c] = f;\n R[a][b][c] = r;\n return ans;\n}\n\nint main() {\n ios_base::sync_with_stdio(false);\n cin.tie(nullptr);\n cout << fixed << setprecision(15);\n\n int N;\n cin >> N;\n while (N--) {\n rep(i,0,3)rep(j,0,3)rep(k,0,3) T[i][j][k] = F[i][j][k] = 0;\n rep(i,0,3)rep(j,0,3) cin >> T[0][j][2-i];\n rep(i,0,3)rep(j,0,3) cin >> F[i][j][0];\n auto ans = dfs(0, 0, 0);\n if (ans.size() == 0) {\n cout << \"0\\n\";\n continue;\n }\n sort(ans.begin(), ans.end());\n ans.erase(unique(ans.begin(), ans.end()), ans.end());\n rep(i,0,ans.size()) cout << ans[i] << (i < ans.size() - 1 ? \" \" : \"\\n\");\n }\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3528, "score_of_the_acc": -0.0376, "final_rank": 11 }, { "submission_id": "aoj_1253_5780612", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\nstatic constexpr int dice[6][6] = {{0, 3, 5, 2, 4, 0}, {4, 0, 1, 6, 0, 3}, {2, 6, 0, 0, 1, 5},\n {5, 1, 0, 0, 6, 2}, {3, 0, 6, 1, 0, 4}, {0, 4, 2, 5, 3, 0}};\n\n/**\n * @brief サイコロ\n * @docs docs/util/Dice.md\n */\nstruct Dice {\n int surface[6];\n int top() { return surface[0]; }\n int south() { return surface[1]; }\n int east() { return surface[2]; }\n int west() { return surface[3]; }\n int north() { return surface[4]; }\n int bottom() { return surface[5]; }\n int operator[](int i) const { return surface[i]; }\n Dice() {}\n Dice(int TOP, int FRONT) {\n surface[0] = TOP;\n surface[1] = FRONT;\n surface[2] = dice[TOP - 1][FRONT - 1];\n surface[3] = 7 - surface[2];\n surface[4] = 7 - surface[1];\n surface[5] = 7 - surface[0];\n }\n};\n\nvoid solve() {\n vector<vector<int>> T(3, vector<int>(3)), F(3, vector<int>(3));\n for (int i = 0; i < 3; i++) {\n for (int j = 0; j < 3; j++) {\n cin >> T[i][j];\n }\n }\n for (int i = 0; i < 3; i++) {\n for (int j = 0; j < 3; j++) {\n cin >> F[i][j];\n }\n }\n\n vector<Dice> cand;\n for (int i = 1; i <= 6; i++) {\n for (int j = 1; j <= 6; j++) {\n if (i == j || i + j == 7) continue;\n cand.emplace_back(Dice(i, j));\n }\n }\n vector<vector<vector<Dice>>> D(3, vector<vector<Dice>>(3, vector<Dice>(3)));\n vector<int> ans;\n\n auto dfs = [&](auto self, int cur) -> void {\n if (cur == 27) {\n int sum = 0;\n for (int j = 0; j < 3; j++) {\n for (int k = 0; k < 3; k++) {\n sum += D[2][j][k].east();\n }\n }\n ans.emplace_back(sum);\n return;\n }\n int x = cur / 9, y = (cur % 9) / 3, z = cur % 3;\n for (auto& d : cand) {\n if (z == 0 && T[2 - y][x] && d.top() != T[2 - y][x]) continue;\n if (y == 0 && F[z][x] && d.south() != F[z][x]) continue;\n if (x > 0 && D[x - 1][y][z].east() + d.west() != 7) continue;\n if (y > 0 && D[x][y - 1][z].north() + d.south() != 7) continue;\n if (z > 0 && D[x][y][z - 1].bottom() + d.top() != 7) continue;\n D[x][y][z] = d;\n self(self, cur + 1);\n }\n };\n dfs(dfs, 0);\n\n sort(ans.begin(), ans.end());\n ans.erase(unique(ans.begin(), ans.end()), ans.end());\n if (ans.empty()) ans.emplace_back(0);\n for (int i = 0; i < (int)ans.size(); i++) cout << ans[i] << (i + 1 == (int)ans.size() ? '\\n' : ' ');\n}\n\nint main() {\n cin.tie(0);\n ios::sync_with_stdio(false);\n int N;\n cin >> N;\n for (; N--;) solve();\n return 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3516, "score_of_the_acc": -0.0366, "final_rank": 10 }, { "submission_id": "aoj_1253_5780610", "code_snippet": "#define LOCAL\n#include <bits/stdc++.h>\nusing namespace std;\n#pragma region Macros\ntypedef long long ll;\ntypedef __int128_t i128;\ntypedef unsigned int uint;\ntypedef unsigned long long ull;\n#define ALL(x) (x).begin(), (x).end()\n\ntemplate <typename T> istream& operator>>(istream& is, vector<T>& v) {\n for (T& x : v) is >> x;\n return is;\n}\ntemplate <typename T> ostream& operator<<(ostream& os, const vector<T>& v) {\n for (int i = 0; i < (int)v.size(); i++) {\n os << v[i] << (i + 1 == (int)v.size() ? \"\" : \" \");\n }\n return os;\n}\ntemplate <typename T, typename U> ostream& operator<<(ostream& os, const pair<T, U>& p) {\n os << '(' << p.first << ',' << p.second << ')';\n return os;\n}\ntemplate <typename T, typename U> ostream& operator<<(ostream& os, const map<T, U>& m) {\n os << '{';\n for (auto itr = m.begin(); itr != m.end();) {\n os << '(' << itr->first << ',' << itr->second << ')';\n if (++itr != m.end()) os << ',';\n }\n os << '}';\n return os;\n}\ntemplate <typename T, typename U> ostream& operator<<(ostream& os, const unordered_map<T, U>& m) {\n os << '{';\n for (auto itr = m.begin(); itr != m.end();) {\n os << '(' << itr->first << ',' << itr->second << ')';\n if (++itr != m.end()) os << ',';\n }\n os << '}';\n return os;\n}\ntemplate <typename T> ostream& operator<<(ostream& os, const set<T>& s) {\n os << '{';\n for (auto itr = s.begin(); itr != s.end();) {\n os << *itr;\n if (++itr != s.end()) os << ',';\n }\n os << '}';\n return os;\n}\ntemplate <typename T> ostream& operator<<(ostream& os, const multiset<T>& s) {\n os << '{';\n for (auto itr = s.begin(); itr != s.end();) {\n os << *itr;\n if (++itr != s.end()) os << ',';\n }\n os << '}';\n return os;\n}\ntemplate <typename T> ostream& operator<<(ostream& os, const unordered_set<T>& s) {\n os << '{';\n for (auto itr = s.begin(); itr != s.end();) {\n os << *itr;\n if (++itr != s.end()) os << ',';\n }\n os << '}';\n return os;\n}\ntemplate <typename T> ostream& operator<<(ostream& os, const deque<T>& v) {\n for (int i = 0; i < (int)v.size(); i++) {\n os << v[i] << (i + 1 == (int)v.size() ? \"\" : \" \");\n }\n return os;\n}\n\ntemplate <int i, typename T> void print_tuple(ostream&, const T&) {}\ntemplate <int i, typename T, typename H, class... Args> void print_tuple(ostream& os, const T& t) {\n if (i) os << ',';\n os << get<i>(t);\n print_tuple<i + 1, T, Args...>(os, t);\n}\ntemplate <typename... Args> ostream& operator<<(ostream& os, const tuple<Args...>& t) {\n os << '{';\n print_tuple<0, tuple<Args...>, Args...>(os, t);\n return os << '}';\n}\n\nvoid debug_out() { cerr << '\\n'; }\ntemplate <class Head, class... Tail> void debug_out(Head&& head, Tail&&... tail) {\n cerr << head;\n if (sizeof...(Tail) > 0) cerr << \", \";\n debug_out(move(tail)...);\n}\n#ifdef LOCAL\n#define debug(...) \\\n cerr << \" \"; \\\n cerr << #__VA_ARGS__ << \" :[\" << __LINE__ << \":\" << __FUNCTION__ << \"]\" << '\\n'; \\\n cerr << \" \"; \\\n debug_out(__VA_ARGS__)\n#else\n#define debug(...) 42\n#endif\n\ntemplate <typename T> T gcd(T x, T y) { return y != 0 ? gcd(y, x % y) : x; }\ntemplate <typename T> T lcm(T x, T y) { return x / gcd(x, y) * y; }\n\nint topbit(signed t) { return t == 0 ? -1 : 31 - __builtin_clz(t); }\nint topbit(long long t) { return t == 0 ? -1 : 63 - __builtin_clzll(t); }\nint botbit(signed a) { return a == 0 ? 32 : __builtin_ctz(a); }\nint botbit(long long a) { return a == 0 ? 64 : __builtin_ctzll(a); }\nint popcount(signed t) { return __builtin_popcount(t); }\nint popcount(long long t) { return __builtin_popcountll(t); }\nbool ispow2(int i) { return i && (i & -i) == i; }\n\ntemplate <class T> T ceil(T x, T y) {\n assert(y >= 1);\n return (x > 0 ? (x + y - 1) / y : x / y);\n}\ntemplate <class T> T floor(T x, T y) {\n assert(y >= 1);\n return (x > 0 ? x / y : (x - y + 1) / y);\n}\n\ntemplate <class T1, class T2> inline bool chmin(T1& a, T2 b) {\n if (a > b) {\n a = b;\n return true;\n }\n return false;\n}\ntemplate <class T1, class T2> inline bool chmax(T1& a, T2 b) {\n if (a < b) {\n a = b;\n return true;\n }\n return false;\n}\n#pragma endregion\n\nstatic constexpr int dice[6][6] = {{0, 3, 5, 2, 4, 0}, {4, 0, 1, 6, 0, 3}, {2, 6, 0, 0, 1, 5},\n {5, 1, 0, 0, 6, 2}, {3, 0, 6, 1, 0, 4}, {0, 4, 2, 5, 3, 0}};\n\n/**\n * @brief サイコロ\n * @docs docs/util/Dice.md\n */\nstruct Dice {\n int surface[6];\n int top() { return surface[0]; }\n int south() { return surface[1]; }\n int east() { return surface[2]; }\n int west() { return surface[3]; }\n int north() { return surface[4]; }\n int bottom() { return surface[5]; }\n int operator[](int i) const { return surface[i]; }\n Dice() {}\n Dice(int TOP, int FRONT) {\n surface[0] = TOP;\n surface[1] = FRONT;\n surface[2] = dice[TOP - 1][FRONT - 1];\n surface[3] = 7 - surface[2];\n surface[4] = 7 - surface[1];\n surface[5] = 7 - surface[0];\n }\n};\n\nvoid solve() {\n vector<vector<int>> T(3, vector<int>(3)), F(3, vector<int>(3));\n for (int i = 0; i < 3; i++) {\n for (int j = 0; j < 3; j++) {\n cin >> T[i][j];\n }\n }\n for (int i = 0; i < 3; i++) {\n for (int j = 0; j < 3; j++) {\n cin >> F[i][j];\n }\n }\n\n vector<Dice> cand;\n for (int i = 1; i <= 6; i++) {\n for (int j = 1; j <= 6; j++) {\n if (i == j || i + j == 7) continue;\n cand.emplace_back(Dice(i, j));\n }\n }\n vector<vector<vector<Dice>>> D(3, vector<vector<Dice>>(3, vector<Dice>(3)));\n vector<int> ans;\n\n auto dfs = [&](auto self, int cur) -> void {\n if (cur == 27) {\n int sum = 0;\n for (int j = 0; j < 3; j++) {\n for (int k = 0; k < 3; k++) {\n sum += D[2][j][k].east();\n }\n }\n ans.emplace_back(sum);\n return;\n }\n int x = cur / 9, y = (cur % 9) / 3, z = cur % 3;\n for (auto& d : cand) {\n if (z == 0 && T[2 - y][x] && d.top() != T[2 - y][x]) continue;\n if (y == 0 && F[z][x] && d.south() != F[z][x]) continue;\n if (x > 0 && D[x - 1][y][z].east() + d.west() != 7) continue;\n if (y > 0 && D[x][y - 1][z].north() + d.south() != 7) continue;\n if (z > 0 && D[x][y][z - 1].bottom() + d.top() != 7) continue;\n D[x][y][z] = d;\n self(self, cur + 1);\n }\n };\n dfs(dfs, 0);\n\n sort(ans.begin(), ans.end());\n ans.erase(unique(ans.begin(), ans.end()), ans.end());\n if (ans.empty()) ans.emplace_back(0);\n for (int i = 0; i < (int)ans.size(); i++) cout << ans[i] << (i + 1 == (int)ans.size() ? '\\n' : ' ');\n}\n\nint main() {\n cin.tie(0);\n ios::sync_with_stdio(false);\n int N;\n cin >> N;\n for (; N--;) solve();\n return 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3512, "score_of_the_acc": -0.0362, "final_rank": 9 }, { "submission_id": "aoj_1253_5740948", "code_snippet": "#include <stdio.h>\n#include <bits/stdc++.h>\nusing namespace std;\n#define rep(i,n) for (int i = 0; i < (n); ++i)\n#define Inf 1000000001\n\nstruct dice{\n\t/*\n\tTop,Left,\n\tFront,Back,\n\tRight,Bottom\n\t*/\n\tvector<int> number = {1,2,3,4,5,6};\n\tvector<vector<int>> states;\n\t\n\tvoid findStates(){\n\t\tstates.clear();\n\t\trep(i,4){\n\t\t\trep(j,4){\n\t\t\t\tstates.push_back(number);\n\t\t\t\trotateClockwise(1);\n\t\t\t}\n\t\t\trotateFront(1);\n\t\t}\n\t\trotateLeft(1);\n\t\trep(i,4){\n\t\t\tstates.push_back(number);\n\t\t\trotateClockwise(1);\n\t\t}\n\t\trotateLeft(2);\n\t\trep(i,4){\n\t\t\tstates.push_back(number);\n\t\t\trotateClockwise(1);\n\t\t}\n\t\trotateLeft(1);\n\t}\n\t\n\tvoid set(vector<int> numbers){\n\t\tnumber = numbers;\n\t}\n\t\n\tvoid rotateFront(int n = 1){\n\t\tn = (n%4+4)%4;\n\t\trep(i,n){\n\t\t\tint t = top();\n\t\t\ttop() = back();\n\t\t\tback() = bottom();\n\t\t\tbottom() = front();\n\t\t\tfront() = t;\n\t\t}\n\t}\n\t\n\tvoid rotateLeft(int n = 1){\n\t\tn = (n%4+4)%4;\n\t\trep(i,n){\n\t\t\tint t = top();\n\t\t\ttop() = right();\n\t\t\tright() = bottom();\n\t\t\tbottom() = left();\n\t\t\tleft() = t;\n\t\t}\n\t}\n\t\n\tvoid rotateClockwise(int n = 1){\n\t\tn = (n%4+4)%4;\n\t\trep(i,n){\n\t\t\tint t = front();\n\t\t\tfront() = right();\n\t\t\tright() = back();\n\t\t\tback() = left();\n\t\t\tleft() = t;\n\t\t}\n\t}\n\t\n\tint& top(){\n\t\treturn number[0];\n\t}\n\t\n\tint& bottom(){\n\t\treturn number[5];\n\t}\n\t\n\tint& front(){\n\t\treturn number[2];\n\t}\n\t\n\tint& back(){\n\t\treturn number[3];\n\t}\n\t\n\tint& left(){\n\t\treturn number[1];\n\t}\n\t\n\tint& right(){\n\t\treturn number[4];\n\t}\n\t\n\tconst int& top()const{\n\t\treturn number[0];\n\t}\n\t\n\tconst int& bottom()const{\n\t\treturn number[5];\n\t}\n\t\n\tconst int& front()const{\n\t\treturn number[2];\n\t}\n\t\n\tconst int& back()const{\n\t\treturn number[3];\n\t}\n\t\n\tconst int& left()const{\n\t\treturn number[1];\n\t}\n\t\n\tconst int& right()const{\n\t\treturn number[4];\n\t}\n\t\n};\n\nvector F(3,vector<int>(3));\nvector T(3,vector<int>(3));\nset<int> S;\ndice D;\nvector<array<int,3>> A = {\n\t{0,0,0},{0,0,1},{0,0,2},{0,1,0},{0,2,0},{1,0,0},{2,0,0},{0,1,1},{0,1,2},\n\t{0,2,1},{0,2,2},{1,0,1},{1,0,2},{1,1,0},{1,1,1},{1,1,2},{1,2,0},{1,2,1},\n\t{1,2,2},{2,0,1},{2,0,2},{2,1,0},{2,1,1},{2,1,2},{2,2,0},{2,2,1},{2,2,2}\n};\n\nvector state(3,vector(3,vector<int>(3,-1)));\n\nvoid dfs(int cur){\n\tif(cur==27){\n\t\tint sum = 0;\n\t\trep(i,3){\n\t\t\trep(j,3){\n\t\t\t\tdice temp;\n\t\t\t\ttemp.set(D.states[state[i][2][j]]);\n\t\t\t\tsum += temp.right();\n\t\t\t}\n\t\t}\n\t\tS.insert(sum);\n\t\treturn;\n\t}\n\t\n\tint x = A[cur][0],y = A[cur][1],z = A[cur][2];\n\t\n\trep(i,24){\n\t\tdice temp;\n\t\ttemp.set(D.states[i]);\n\t\tif(z==0){\n\t\t\tif(F[x][y]!=0 && F[x][y]!=temp.front())continue;\n\t\t}\n\t\tif(x==0){\n\t\t\tif(T[z][y]!=0 && T[z][y]!=temp.top())continue;\n\t\t}\n\t\tbool f = true;\n\t\trep(j,cur){\n\t\t\tint xx = A[j][0],yy = A[j][1],zz = A[j][2];\n\t\t\tif(abs(xx-x)+abs(yy-y)+abs(zz-z)!=1)continue;\n\t\t\tdice nt;\n\t\t\tnt.set(D.states[state[xx][yy][zz]]);\n\t\t\tif(y<yy){\n\t\t\t\tif(temp.right()+nt.left()!=7)f = false;\n\t\t\t}\n\t\t\tif(y>yy){\n\t\t\t\tif(temp.left()+nt.right()!=7)f = false;\n\t\t\t}\n\t\t\tif(x<xx){\n\t\t\t\tif(temp.bottom()+nt.top()!=7)f = false;\n\t\t\t}\n\t\t\tif(x>xx){\n\t\t\t\tif(temp.top()+nt.bottom()!=7)f = false;\n\t\t\t}\n\t\t\tif(z<zz){\n\t\t\t\tif(temp.back()+nt.front()!=7)f = false;\n\t\t\t}\n\t\t\tif(z>zz){\n\t\t\t\tif(temp.front()+nt.back()!=7)f = false;\n\t\t\t}\n\t\t}\n\t\tif(!f)continue;\n\t\tstate[x][y][z] = i;\n\t\tdfs(cur+1);\n\t}\n\t\n}\n\nint main(){\n\t\n\tint N;\n\tcin>>N;\n\t\n\tD.findStates();\n\t\n\trep(_,N){\n\t\trep(i,3){\n\t\t\trep(j,3){\n\t\t\t\tcin>>T[i][j];\n\t\t\t}\n\t\t}\n\t\trep(i,3){\n\t\t\trep(j,3){\n\t\t\t\tcin>>F[i][j];\n\t\t\t}\n\t\t}\n\t\treverse(T.begin(),T.end());\n\t\tS.clear();\n\t\t\n\t\tdfs(0);\n\t\t\n\t\tvector<int> ans;\n\t\tfor(auto a:S)ans.push_back(a);\n\t\t\n\t\tif(ans.size()==0)cout<<0<<endl;\n\t\telse{\n\t\t\trep(i,ans.size()){\n\t\t\t\tif(i!=0)cout<<' ';\n\t\t\t\tcout<<ans[i];\n\t\t\t}\n\t\t\tcout<<endl;\n\t\t}\n\t\t\n\t}\n\t\n\t\n\t\n\treturn 0;\n}", "accuracy": 1, "time_ms": 2120, "memory_kb": 3080, "score_of_the_acc": -1, "final_rank": 19 }, { "submission_id": "aoj_1253_5446269", "code_snippet": "#pragma GCC optimize(\"Ofast\")\n#include <bits/stdc++.h>\nusing namespace std;\ntypedef long long int ll;\ntypedef unsigned long long ull;\n\nmt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());\nll myRand(ll B) {\n return (ull)rng() % B;\n}\n\nint d[24][3]={\n {6,2,3},\n {1,5,3},\n {1,2,4},\n {6,5,4},\n {1,3,2},\n {6,4,2},\n {6,3,5},\n {1,4,5},\n {2,1,3},\n {5,6,3},\n {5,1,4},\n {2,6,4},\n {5,3,1},\n {2,4,1},\n {2,3,6},\n {5,4,6},\n {4,1,2},\n {3,6,2},\n {3,1,5},\n {4,6,5},\n {3,2,1},\n {4,5,1},\n {4,2,6},\n {3,5,6},\n};\n\nint T[3][3],F[3][3];\n\nint A[3][3][3];\n\nvector<int> v;\n\nvoid dfs(int num){\n if(num==27){\n int sum=0;\n for(int i=0;i<3;i++){\n for(int j=0;j<3;j++){\n sum+=(7-d[A[i][j][2]][2]);\n }\n }\n v.push_back(sum);\n return;\n }\n int i=num/9,j=num/3%3,k=num%3;\n for(int id=0;id<24;id++){\n A[i][j][k]=id;\n // ここで判定を書く\n if(i==0){\n if(T[j][k]!=0 and T[j][k]!=d[id][0])continue;\n }\n if(j==2){\n if(F[i][k]!=0 and F[i][k]!=d[id][1])continue;\n }\n if(i>0){\n int u=A[i-1][j][k];\n if(7-d[u][0]+d[id][0]!=7)continue;\n }\n if(j>0){\n int u=A[i][j-1][k];\n if(d[u][1]+(7-d[id][1])!=7)continue;\n }\n if(k>0){\n int u=A[i][j][k-1];\n if((7-d[u][2])+d[id][2]!=7)continue;\n }\n // OKなら\n dfs(num+1);\n }\n}\n\nint main(){\n cin.tie(nullptr);\n ios::sync_with_stdio(false);\n int q; cin >> q;\n while(q--){\n for(int i=0;i<3;i++){\n for(int j=0;j<3;j++){\n cin >> T[i][j];\n }\n }\n for(int i=0;i<3;i++){\n for(int j=0;j<3;j++){\n cin >> F[i][j];\n }\n }\n v.clear();\n dfs(0);\n sort(v.begin(), v.end());\n v.erase(unique(v.begin(), v.end()),v.end());\n if(v.size()==0){\n printf(\"0\\n\");\n }\n else{\n printf(\"%d\",v[0]);\n for(int i=1;i<v.size();i++){\n printf(\" %d\",v[i]);\n }\n printf(\"\\n\");\n }\n }\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3336, "score_of_the_acc": -0.0215, "final_rank": 2 }, { "submission_id": "aoj_1253_5247389", "code_snippet": "#include <bits/stdc++.h>\n\nusing namespace std;\n\ntypedef long long ll;\ntypedef pair<int, int> P;\n\n#define eps 1e-9\n#define INF 2000000000 // 2e9\n#define LLINF 2000000000000000000ll // 2e18 (llmax:9e18)\n#define all(x) (x).begin(), (x).end()\n#define sq(x) ((x) * (x))\n\n#define rep(i, x) for (int i = 0; i < (int)(x); i++)\n#define drep(i, x) for (int i = ((int)(x)-1); i >= 0; i--)\n\n#define popcount __builtin_popcount\n#define next __next\n#define prev __prev\n\n#ifndef LOCAL\n#define dmp(...) ;\n#else\n#define dmp(...) \\\n cerr << \"[ \" << #__VA_ARGS__ << \" ] : \" << dump_str(__VA_ARGS__) << endl\n#endif\n\n// ---------------- Utility ------------------\n\ntemplate <class T>\nbool chmin(T &a, const T &b) {\n if (a <= b) return false;\n a = b;\n return true;\n}\ntemplate <class T>\nbool chmax(T &a, const T &b) {\n if (a >= b) return false;\n a = b;\n return true;\n}\n\ntemplate <class T>\nusing MaxHeap = priority_queue<T>;\ntemplate <class T>\nusing MinHeap = priority_queue<T, vector<T>, greater<T>>;\n\ntemplate <class T>\nvector<T> vect(int len, T elem) {\n return vector<T>(len, elem);\n}\n\n// ----------------- Input -------------------\n\ntemplate <class T, class U>\nistream &operator>>(istream &is, pair<T, U> &p) {\n return is >> p.first >> p.second;\n}\n\ntemplate <class T>\nistream &operator>>(istream &is, vector<T> &vec) {\n for (int i = 0; i < vec.size(); i++) is >> vec[i];\n return is;\n}\n\n// ----------------- Output ------------------\n\ntemplate <class T, class U>\nostream &operator<<(ostream &os, const pair<T, U> &p) {\n return os << p.first << ',' << p.second;\n}\n\ntemplate <class T>\nostream &operator<<(ostream &os, const vector<T> &v) {\n for (const T &e : v) os << e << \" \";\n return os;\n}\n\ntemplate <class T>\nostream &operator<<(ostream &os, const deque<T> &d) {\n for (const T &e : d) os << e << \" \";\n return os;\n}\n\ntemplate <class T>\nostream &operator<<(ostream &os, const set<T> &s) {\n os << \"{ \";\n for (const T &e : s) os << e << \" \";\n return os << \"}\";\n}\n\ntemplate <class T, class U>\nostream &operator<<(ostream &os, const map<T, U> &m) {\n os << \"{ \";\n for (const auto &[key, val] : m) os << \"( \" << key << \" -> \" << val << \" ) \";\n return os << \"}\";\n}\n\ntemplate <class TupleTy, size_t... I>\nvoid dump_tuple(ostream &os, const TupleTy t, std::index_sequence<I...>) {\n (..., (os << (I == 0 ? \"\" : \",\") << std::get<I>(t)));\n}\n\ntemplate <class... Args>\nostream &operator<<(ostream &os, const tuple<Args...> &t) {\n os << \"(\";\n dump_tuple(os, t, std::make_index_sequence<sizeof...(Args)>());\n return os << \")\";\n}\n\nvoid dump_str_rec(ostringstream &) {}\n\ntemplate <class Head, class... Tail>\nvoid dump_str_rec(ostringstream &oss, Head &&head, Tail &&... tail) {\n oss << \", \" << head;\n dump_str_rec(oss, forward<Tail>(tail)...);\n}\n\ntemplate <class T, class... U>\nstring dump_str(T &&arg, U &&... args) {\n ostringstream oss;\n oss << arg;\n dump_str_rec(oss, forward<U>(args)...);\n return oss.str();\n}\n\n// --------------- Fast I/O ------------------\n\nvoid fastio() {\n cin.tie(0);\n ios::sync_with_stdio(0);\n cout << fixed << setprecision(20);\n}\n\n// ------------ End of template --------------\n\n#define endl \"\\n\"\n\nint dice[24][6] = {\n {1, 2, 3, 5, 4, 6}, {1, 3, 5, 4, 2, 6},\n {1, 5, 4, 2, 3, 6}, {1, 4, 2, 3, 5, 6},\n\n {2, 1, 4, 6, 3, 5}, {2, 4, 6, 3, 1, 5},\n {2, 6, 3, 1, 4, 5}, {2, 3, 1, 4, 6, 5},\n\n {3, 1, 2, 6, 5, 4}, {3, 2, 6, 5, 1, 4},\n {3, 6, 5, 1, 2, 4}, {3, 5, 1, 2, 6, 4},\n\n {4, 2, 1, 5, 6, 3}, {4, 1, 5, 6, 2, 3},\n {4, 5, 6, 2, 1, 3}, {4, 6, 2, 1, 5, 3},\n\n {5, 4, 1, 3, 6, 2}, {5, 1, 3, 6, 4, 2},\n {5, 3, 6, 4, 1, 2}, {5, 6, 4, 1, 3, 2},\n\n {6, 2, 4, 5, 3, 1}, {6, 4, 5, 3, 2, 1},\n {6, 5, 3, 2, 4, 1}, {6, 3, 2, 4, 5, 1},\n};\n\nvoid solve() {\n auto T = vect(3, vect(3, 0));\n auto F = vect(3, vect(3, 0));\n for (int i = 0; i < 3; i++) {\n for (int j = 0; j < 3; j++) { cin >> T[i][j]; }\n }\n for (int i = 0; i < 3; i++) {\n for (int j = 0; j < 3; j++) { cin >> F[i][j]; }\n }\n\n auto d = vect(3, vect(3, vect(3, -1)));\n vector<int> ans;\n\n function<void(int)> dfs = [&](int n) -> void {\n if (n == 27) {\n int sum = 0;\n for (int i = 0; i < 3; i++) {\n for (int k = 0; k < 3; k++) { sum += dice[d[i][2][k]][2]; }\n }\n ans.push_back(sum);\n return;\n }\n int i = n / 9, j = (n % 9) / 3, k = n % 3;\n for (int t = 0; t < 24; t++) {\n d[i][j][k] = t;\n if (T[i][j] != 0 && T[i][j] != dice[d[i][j][k]][0]) continue;\n if (F[k][j] != 0 && F[k][j] != dice[d[i][j][k]][1]) continue;\n if (i > 0 && dice[d[i - 1][j][k]][1] + dice[d[i][j][k]][3] != 7) continue;\n if (j > 0 && dice[d[i][j - 1][k]][2] + dice[d[i][j][k]][4] != 7) continue;\n if (k > 0 && dice[d[i][j][k - 1]][5] + dice[d[i][j][k]][0] != 7) continue;\n dfs(n + 1);\n }\n };\n\n dfs(0);\n\n sort(all(ans));\n ans.erase(unique(all(ans)), ans.end());\n if (ans.size() == 0)\n cout << 0 << endl;\n else {\n for (int i = 0; i < ans.size(); i++) {\n cout << ans[i];\n if (i + 1 < ans.size())\n cout << ' ';\n else\n cout << endl;\n }\n }\n return;\n}\n\nint main() {\n fastio();\n // solve();\n int t;\n cin >> t;\n while (t--) solve();\n\n // int t; cin >> t;\n // for(int i=1;i<=t;i++){\n // cout << \"Case #\" << i << \": \";\n // solve();\n // }\n return 0;\n}", "accuracy": 1, "time_ms": 20, "memory_kb": 3356, "score_of_the_acc": -0.0279, "final_rank": 6 }, { "submission_id": "aoj_1253_4879130", "code_snippet": "#include <bits/stdc++.h>\n\nusing namespace std;\n\n#define REP(i, n) for (int i=0; i<(n); ++i)\n#define RREP(i, n) for (int i=(int)(n)-1; i>=0; --i)\n#define FOR(i, a, n) for (int i=(a); i<(n); ++i)\n#define RFOR(i, a, n) for (int i=(int)(n)-1; i>=(a); --i)\n\n#define SZ(x) ((int)(x).size())\n#define ALL(x) (x).begin(),(x).end()\n\n#define DUMP(x) cerr<<#x<<\" = \"<<(x)<<endl\n#define DEBUG(x) cerr<<#x<<\" = \"<<(x)<<\" (L\"<<__LINE__<<\")\"<<endl;\n\ntemplate<class T>\nostream &operator<<(ostream &os, const vector<T> &v) {\n os << \"[\";\n REP(i, SZ(v)) {\n if (i) os << \", \";\n os << v[i];\n }\n return os << \"]\";\n}\n\ntemplate<class T, class U>\nostream &operator<<(ostream &os, const pair<T, U> &p) {\n return os << \"(\" << p.first << \" \" << p.second << \")\";\n}\n\ntemplate<class T>\nbool chmax(T &a, const T &b) {\n if (a < b) {\n a = b;\n return true;\n }\n return false;\n}\n\ntemplate<class T>\nbool chmin(T &a, const T &b) {\n if (b < a) {\n a = b;\n return true;\n }\n return false;\n}\n\nusing ll = long long;\nusing ull = unsigned long long;\nusing ld = long double;\nusing P = pair<int, int>;\nusing vi = vector<int>;\nusing vll = vector<ll>;\nusing vvi = vector<vi>;\nusing vvll = vector<vll>;\n\nconst ll MOD = 1e9 + 7;\nconst int INF = INT_MAX / 2;\nconst ll LINF = LLONG_MAX / 2;\nconst ld eps = 1e-9;\n\nstring dice[24] = {\n \"124536\", \"132456\", \"153246\", \"145326\",\n \"213645\", \"236415\", \"264135\", \"241365\",\n \"321564\", \"315624\", \"356214\", \"362154\",\n \"426513\", \"465123\", \"451263\", \"412653\",\n \"514632\", \"546312\", \"563142\", \"531462\",\n \"623541\", \"635421\", \"654231\", \"642351\"\n};\nmap<string, int> id;\n\nint r[27] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 2, 2, 2, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2};\nint c[27] = {0, 0, 0, 1, 1, 1, 2, 2, 2, 0, 0, 0, 0, 0, 0, 1, 1, 1, 2, 2, 2, 1, 1, 1, 2, 2, 2};\nint d[27] = {0, 1, 2, 0, 1, 2, 0, 1, 2, 0, 1, 2, 0, 1, 2, 0, 1, 2, 0, 1, 2, 0, 1, 2, 0, 1, 2};\n\nint dr[6] = {1, -1, 0, 0, 0, 0};\nint dc[6] = {0, 0, 1, -1, 0, 0};\nint dd[6] = {0, 0, 0, 0, 1, -1};\n\nP adj[6] = {{5, 0},\n {0, 5},\n {3, 1},\n {1, 3},\n {4, 2},\n {2, 4}};\n\nvoid solve() {\n vvi t(3, vi(3)), f(3, vi(3));\n REP(i, 3) {\n REP(j, 3) {\n cin >> t[i][j];\n }\n }\n\n REP(i, 3) {\n REP(j, 3) {\n cin >> f[i][j];\n }\n }\n\n\n function<void(int, vector<vvi> &, set<int> &)> dfs = [&](int k, vector<vvi> &v, set<int> &st) {\n if (k == 27) {\n int su = 0;\n REP(i, 3) {\n REP(j, 3) {\n su += dice[v[i][j][2]][4] - '0';\n }\n }\n st.insert(su);\n return;\n }\n\n REP(i, 24) {\n if (r[k] == 0) {\n if (t[2 - c[k]][d[k]] != 0 && t[2 - c[k]][d[k]] != dice[i][0] - '0') continue;\n }\n if (c[k] == 0) {\n if (f[r[k]][d[k]] != 0 && f[r[k]][d[k]] != dice[i][1] - '0') continue;\n }\n v[r[k]][c[k]][d[k]] = i;\n bool ok = true;\n REP(j, 6) {\n int nr = r[k] + dr[j], nc = c[k] + dc[j], nd = d[k] + dd[j];\n if (!(0 <= nr && nr < 3 && 0 <= nc && nc < 3 && 0 <= nd && nd < 3)) continue;\n if (v[nr][nc][nd] == -1) continue;\n if ((dice[i][adj[j].first] - '0') + (dice[v[nr][nc][nd]][adj[j].second] - '0') != 7) {\n ok = false;\n break;\n }\n }\n if (ok) dfs(k + 1, v, st);\n v[r[k]][c[k]][d[k]] = -1;\n }\n };\n\n vector<vvi> v(3, vvi(3, vi(3, -1)));\n set<int> st;\n dfs(0, v, st);\n\n if (SZ(st) == 0) {\n cout << 0 << endl;\n return;\n }\n int cnt = 0;\n for (auto &e: st) {\n cnt++;\n cout << e;\n if (cnt == SZ(st)) {\n cout << endl;\n } else {\n cout << \" \";\n }\n }\n\n}\n\nint main() {\n cin.tie(0);\n ios::sync_with_stdio(false);\n cout << fixed << setprecision(10);\n\n REP(i, 24) {\n id[dice[i]] = i;\n }\n\n int n;\n cin >> n;\n REP(i, n) {\n solve();\n }\n\n return 0;\n}", "accuracy": 1, "time_ms": 50, "memory_kb": 3352, "score_of_the_acc": -0.0418, "final_rank": 12 }, { "submission_id": "aoj_1253_4878513", "code_snippet": "#include \"iostream\"\n#include \"climits\"\n#include \"list\"\n#include \"queue\"\n#include \"stack\"\n#include \"set\"\n#include \"functional\"\n#include \"algorithm\"\n#include \"string\"\n#include \"map\"\n#include \"unordered_map\"\n#include \"unordered_set\"\n#include \"iomanip\"\n#include \"cmath\"\n#include \"random\"\n#include \"bitset\"\n#include \"cstdio\"\n#include \"numeric\"\n#include \"cassert\"\n#include \"ctime\"\n\nusing namespace std;\n\nconstexpr long long int MOD = 1000000007;\n//constexpr int MOD = 1000000007;\n//constexpr int MOD = 998244353;\n//constexpr long long int MOD = 998244353;\nconstexpr double EPS = 1e-12;\n\n//int N, M, K, T, H, W, L, R;\nlong long int N, M, K, T, H, W, L, R;\n\nset<int>st;\nvector<vector<int>>v({ { 1,2,3 },{ 1,3,5 },{ 1,5,4 },{ 1,4,2 },{ 2,3,1 },{ 2,1,4 },{ 2,4,6 },{ 2,6,3 },{ 3,1,2 },{ 3,2,6 },{ 3,6,5 },{ 3,5,1 },{ 4,2,1 },{ 4,1,5, },{ 4,5,6 },{ 4,6,2 },{ 5,1,3 },{ 5,3,6 },{ 5,6,4 },{ 5,4,1 },{ 6,3,2 },{ 6,2,4 },{ 6,4,5 },{ 6,5,3 } });\nvector<vector<int>>a(3, vector<int>(3));\nvector<vector<int>>b(3, vector<int>(3));\n\nvoid func(vector<int>&vp, int p = 0) {\n\t//cout << p << endl;\n\tif (p == 27) {\n\t\tint sum = 0;\n\t\tfor (int i = 0; i < 27; i += 3) {\n\t\t\tsum += v[vp[i]][2];\n\t\t}\n\t\tst.insert(sum);\n\t\treturn;\n\t}\n\tint x = p % 3, y = p % 9 / 3, z = p / 9;\n\tfor (int i = 0; i < 24; i++) {\n\t\tvp[p] = i;\n\t\tbool flag = true;\n\t\tif (x) {\n\t\t\tflag &= v[vp[p - 1]][2] == v[i][2];\n\t\t}\n\t\tif (y) {\n\t\t\tflag &= v[vp[p - 3]][1] == v[i][1];\n\t\t}\n\t\tif (z) {\n\t\t\tflag &= v[vp[p - 9]][0] == v[i][0];\n\t\t}\n\t\tif (v[i][0] != a[y][x] && a[y][x])flag = false;\n\t\tif (v[i][1] != b[z][x] && b[z][x])flag = false;\n\t\tif (flag) {\n\t\t\tfunc(vp, p + 1);\n\t\t}\n\t\tvp[p] = -1;\n\t}\n\treturn;\n}\n\nint main() {\n\tios::sync_with_stdio(false);\n\tcin.tie(0);\n\n\tcin >> T;\n\twhile (T--) {\n\t\tfor (auto &i : a)for (auto &j : i)cin >> j;\n\t\tfor (auto &i : b)for (auto &j : i)cin >> j;\n\t\tvector<int>vp(27, -1);\n\t\tfunc(vp);\n\t\tif (st.empty())cout << 0 << endl;\n\t\tfor (auto i : st) {\n\t\t\tcout << i;\n\t\t\tif (i == *(--st.end()))cout << endl;\n\t\t\telse cout << \" \";\n\t\t}\n\t\tst.clear();\n\t}\n}", "accuracy": 1, "time_ms": 20, "memory_kb": 3136, "score_of_the_acc": -0.0094, "final_rank": 1 }, { "submission_id": "aoj_1253_4813228", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nvector<vector<int>> r = {{-1, 2, 4, 1, 3, -1}, {3, -1, 0, 5, -1, 2}, {1, 5, -1, -1, 0, 4}, {4, 0, -1, -1, 5, 1}, {2, -1, 5, 0, -1, 3}, {-1, 3, 1, 4, 2, -1}};\nset<vector<int>> dfs(vector<int> A, int p){\n if (p == 3){\n set<vector<int>> st;\n st.insert(A);\n return st;\n } else if (A[p] != -1){\n return dfs(A, p + 1);\n } else {\n set<vector<int>> st;\n for (int i = 0; i < 6; i++){\n vector<int> B = A;\n B[p] = i;\n set<vector<int>> st2 = dfs(B, p + 1);\n for (auto C : st2){\n st.insert(C);\n }\n }\n return st;\n }\n}\nint main(){\n int N;\n cin >> N;\n for (int i = 0; i < N; i++){\n vector<vector<int>> T(3, vector<int>(3));\n for (int j = 0; j < 3; j++){\n for (int k = 0; k < 3; k++){\n cin >> T[k][j];\n T[k][j]--;\n }\n }\n vector<vector<int>> F(3, vector<int>(3));\n for (int j = 0; j < 3; j++){\n for (int k = 0; k < 3; k++){\n cin >> F[k][j];\n F[k][j]--;\n }\n }\n map<vector<vector<int>>, int> mp;\n for (int j = 0; j < 3; j++){\n set<vector<int>> tmp1 = dfs(T[j], 0);\n set<vector<int>> tmp2 = dfs(F[j], 0);\n set<vector<vector<int>>> st;\n for (auto A : tmp1){\n for (auto B : tmp2){\n vector<vector<int>> R(3, vector<int>(3));\n bool ok = true;\n for (int k = 0; k < 3; k++){\n for (int l = 0; l < 3; l++){\n R[k][l] = r[A[k]][B[l]];\n if (R[k][l] == -1){\n ok = false;\n }\n }\n }\n if (ok){\n st.insert(R);\n }\n }\n }\n for (auto R : st){\n mp[R]++;\n }\n }\n vector<int> ans;\n for (auto P : mp){\n if (P.second == 3){\n int sum = 0;\n for (int j = 0; j < 3; j++){\n for (int k = 0; k < 3; k++){\n sum += P.first[j][k] + 1;\n }\n }\n ans.push_back(sum);\n }\n }\n sort(ans.begin(), ans.end());\n ans.erase(unique(ans.begin(), ans.end()), ans.end());\n int cnt = ans.size();\n if (cnt == 0){\n cout << 0 << endl;\n } else {\n for (int j = 0; j < cnt; j++){\n cout << ans[j];\n if (j < cnt - 1){\n cout << ' ';\n }\n }\n cout << endl;\n }\n }\n}", "accuracy": 1, "time_ms": 50, "memory_kb": 3784, "score_of_the_acc": -0.078, "final_rank": 16 }, { "submission_id": "aoj_1253_4768264", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\ntypedef long long ll;\ntemplate<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return true; } return false; }\ntemplate<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return true; } return false; }\n#define all(x) (x).begin(),(x).end()\n#define fi first\n#define se second\n#define mp make_pair\n#define si(x) int(x.size())\nconst int mod=1000000007,MAX=305,INF=1<<30;\n\nint ri(int u,int f){\n if(u==1){\n if(f==2) return 3;\n if(f==3) return 5;\n if(f==5) return 4;\n if(f==4) return 2;\n }\n if(u==2){\n if(f==1) return 4;\n if(f==4) return 6;\n if(f==6) return 3;\n if(f==3) return 1;\n }\n if(u==3){\n if(f==1) return 2;\n if(f==2) return 6;\n if(f==6) return 5;\n if(f==5) return 1;\n }\n if(u==4){\n if(f==1) return 5;\n if(f==5) return 6;\n if(f==6) return 2;\n if(f==2) return 1;\n }\n if(u==5){\n if(f==1) return 3;\n if(f==3) return 6;\n if(f==6) return 4;\n if(f==4) return 1;\n }\n if(u==6){\n if(f==2) return 4;\n if(f==4) return 5;\n if(f==5) return 3;\n if(f==3) return 2;\n }\n \n return -1000;\n}//uとfが与えられた時にrを返す(u==1,f==2,r==3型のサイコロ)\n\nstruct dice{\n int u,f,r,b,l,d;\n \n void init(int uu,int ff){\n u=uu;\n f=ff;\n r=ri(u,f);\n b=7-f;\n l=7-r;\n d=7-u;\n }\n \n void turnF(){\n int save=f;\n f=u;\n u=b;\n b=d;\n d=save;\n }\n \n void turnR(){\n int save=r;\n r=u;\n u=l;\n l=d;\n d=save;\n }\n \n void turnB(){\n int save=b;\n b=u;\n u=f;\n f=d;\n d=save;\n }\n \n void turnL(){\n int save=l;\n l=u;\n u=r;\n r=d;\n d=save;\n }\n \n void turn1(){\n int save=r;\n r=b;\n b=l;\n l=f;\n f=save;\n }//時計回り\n \n void turn2(){\n int save=r;\n r=f;\n f=l;\n l=b;\n b=save;\n }//反時計回り\n};\n\nint A[3][3],B[3][3];\nset<int> ans;\n\nvector<int> X;\n\nvoid DFS(){\n if(si(X)==8){\n if(A[0][0]&&A[0][0]!=X[0]) return;\n if(A[1][0]&&A[1][0]!=X[1]) return;\n if(A[2][0]&&A[2][0]!=X[2]) return;\n if(A[2][1]&&A[2][1]!=X[3]) return;\n if(A[2][2]&&A[2][2]!=X[4]) return;\n if(B[0][0]&&B[0][0]!=X[5]) return;\n if(B[1][0]&&B[1][0]!=X[6]) return;\n if(B[2][0]&&B[2][0]!=X[7]) return;\n \n if(X[2]+X[5]==7) return;\n \n vector<vector<vector<dice>>> Y(3,vector<vector<dice>>(3,vector<dice>(3)));\n \n Y[0][2][0].init(X[2],X[5]);\n Y[0][2][1].init(X[3],7-Y[0][2][0].r);\n Y[0][2][1].turn1();\n Y[0][2][2].init(X[4],7-Y[0][2][1].r);\n Y[0][2][2].turn1();\n \n Y[0][1][0].init(X[1],7-Y[0][2][0].b);\n Y[0][1][1].init(7-Y[0][1][0].r,7-Y[0][2][1].b);\n Y[0][1][1].turnL();\n Y[0][1][2].init(7-Y[0][1][1].r,7-Y[0][2][2].b);\n Y[0][1][2].turnL();\n \n Y[0][0][0].init(X[0],7-Y[0][1][0].b);\n Y[0][0][1].init(7-Y[0][0][0].r,7-Y[0][1][1].b);\n Y[0][0][1].turnL();\n Y[0][0][2].init(7-Y[0][0][1].r,7-Y[0][1][2].b);\n Y[0][0][2].turnL();\n \n //\n \n Y[1][2][0].init(7-Y[0][2][0].d,X[6]);\n Y[1][2][1].init(7-Y[0][2][1].d,7-Y[1][2][0].r);\n Y[1][2][1].turn1();\n Y[1][2][2].init(7-Y[0][2][2].d,7-Y[1][2][1].r);\n Y[1][2][2].turn1();\n \n Y[1][1][0].init(7-Y[0][1][0].d,7-Y[1][2][0].b);\n Y[1][1][1].init(7-Y[0][1][1].d,7-Y[1][2][1].b);\n Y[1][1][2].init(7-Y[0][1][2].d,7-Y[1][2][2].b);\n \n Y[1][0][0].init(7-Y[0][0][0].d,7-Y[1][1][0].b);\n Y[1][0][1].init(7-Y[0][0][1].d,7-Y[1][1][1].b);\n Y[1][0][2].init(7-Y[0][0][2].d,7-Y[1][1][2].b);\n \n //\n \n Y[2][2][0].init(7-Y[1][2][0].d,X[7]);\n Y[2][2][1].init(7-Y[1][2][1].d,7-Y[2][2][0].r);\n Y[2][2][1].turn1();\n Y[2][2][2].init(7-Y[1][2][2].d,7-Y[2][2][1].r);\n Y[2][2][2].turn1();\n \n Y[2][1][0].init(7-Y[1][1][0].d,7-Y[2][2][0].b);\n Y[2][1][1].init(7-Y[1][1][1].d,7-Y[2][2][1].b);\n Y[2][1][2].init(7-Y[1][1][2].d,7-Y[2][2][2].b);\n \n Y[2][0][0].init(7-Y[1][0][0].d,7-Y[2][1][0].b);\n Y[2][0][1].init(7-Y[1][0][1].d,7-Y[2][1][1].b);\n Y[2][0][2].init(7-Y[1][0][2].d,7-Y[2][1][2].b);\n \n for(int i=0;i<3;i++){\n for(int j=0;j<3;j++){\n if(A[i][j]&&A[i][j]!=Y[0][i][j].u) return;\n if(B[i][j]&&B[i][j]!=Y[i][2][j].f) return;\n }\n }\n \n for(int z=0;z<3;z++){\n for(int x=0;x<3;x++){\n for(int y=0;y<3;y++){\n if(abs(Y[z][x][y].u)>=7) return;\n if(abs(Y[z][x][y].f)>=7) return;\n if(abs(Y[z][x][y].r)>=7) return;\n if(abs(Y[z][x][y].b)>=7) return;\n if(abs(Y[z][x][y].l)>=7) return;\n if(abs(Y[z][x][y].d)>=7) return;\n }\n }\n }\n \n int sum=0;\n \n for(int z=0;z<3;z++){\n for(int x=0;x<3;x++){\n sum+=Y[z][x][2].r;\n }\n }\n \n ans.insert(sum);\n \n return;\n }\n \n for(int i=1;i<=6;i++){\n X.push_back(i);\n DFS();\n X.pop_back();\n }\n}\n\nint main(){\n \n std::ifstream in(\"text.txt\");\n std::cin.rdbuf(in.rdbuf());\n cin.tie(0);\n ios::sync_with_stdio(false);\n \n int Q;cin>>Q;\n while(Q--){\n ans.clear();\n for(int i=0;i<3;i++) for(int j=0;j<3;j++) cin>>A[i][j];\n for(int i=0;i<3;i++) for(int j=0;j<3;j++) cin>>B[i][j];\n \n DFS();\n \n if(si(ans)==0) cout<<0<<endl;\n else{\n for(auto it=ans.begin();it!=ans.end();it++){\n if(it!=ans.begin()) cout<<\" \";\n cout<<*it;\n }\n cout<<endl;\n }\n }\n}", "accuracy": 1, "time_ms": 1130, "memory_kb": 3188, "score_of_the_acc": -0.5399, "final_rank": 18 }, { "submission_id": "aoj_1253_3551642", "code_snippet": "#include<iostream>\n#include<string>\n#include<cstdio>\n#include<vector>\n#include<cmath>\n#include<algorithm>\n#include<functional>\n#include<iomanip>\n#include<queue>\n#include<ciso646>\n#include<random>\n#include<map>\n#include<set>\n#include<complex>\n#include<bitset>\n#include<stack>\n#include<unordered_map>\n#include<utility>\nusing namespace std;\ntypedef long long ll;\ntypedef unsigned long long ul;\ntypedef unsigned int ui;\nconst ll mod = 1000000007;\ntypedef double ld;\ntypedef complex<ld> Point;\nconst ll INF = mod * mod;\ntypedef pair<int, int> P;\n#define stop char nyaa;cin>>nyaa;\n#define rep(i,n) for(int i=0;i<n;i++)\n#define per(i,n) for(int i=n-1;i>=0;i--)\n#define Rep(i,sta,n) for(int i=sta;i<n;i++)\n#define rep1(i,n) for(int i=1;i<=n;i++)\n#define per1(i,n) for(int i=n;i>=1;i--)\n#define Rep1(i,sta,n) for(int i=sta;i<=n;i++)\nconst ld eps = 1e-8;\nconst ld pi = acos(-1.0);\ntypedef pair<ld, ld> LDP;\ntypedef pair<ll, ll> LP;\n\n//top,frontからrightを決定\nint ident(int a, int b) {\n\tint c;\n\tif (a == 1) {\n\t\tif (b == 2)c = 3;\n\t\telse if (b == 3)c = 5;\n\t\telse if (b == 5)c = 4;\n\t\telse c = 2;\n\t}\n\telse if (a == 2) {\n\t\tif (b == 1)c = 4;\n\t\telse if (b == 4)c = 6;\n\t\telse if (b == 6)c = 3;\n\t\telse c = 1;\n\t}\n\telse if (a == 3) {\n\t\tif (b == 1)c = 2;\n\t\telse if (b == 2)c = 6;\n\t\telse if (b == 5)c = 1;\n\t\telse c = 5;\n\t}\n\telse if (a == 4) {\n\t\tif (b == 1)c = 5;\n\t\telse if (b == 5)c = 6;\n\t\telse if (b == 6)c = 2;\n\t\telse c = 1;\n\t}\n\telse if (a == 5) {\n\t\tif (b == 1)c = 3;\n\t\telse if (b == 3)c = 6;\n\t\telse if (b == 6)c = 4;\n\t\telse c = 1;\n\t}\n\telse {\n\t\tif (b == 2)c = 4;\n\t\telse if (b == 3)c = 2;\n\t\telse if (b == 5)c = 3;\n\t\telse c = 5;\n\t}\n\treturn c;\n}\nint calc(int x) {\n\tint ret = 0;\n\trep (i,9) {\n\t\tret += x % 6 + 1;\n\t\tx /= 6;\n\t}\n\treturn ret;\n}\n//46656=6^6\nint t[3][3], f[3][3];\nint t3[9];\nvoid solve() {\n\tt3[0] = 1;\n\trep(i, 8)t3[i + 1] = t3[i] * 6;\n\tint n; cin >> n; \n\trep(aa, n) {\n\t\tmap<int, bool> mp[3];\n\t\tvector<int> c;\n\t\trep(i, 3) {\n\t\t\trep(j, 3) {\n\t\t\t\tcin >> t[i][j];\n\t\t\t}\n\t\t}\n\t\trep(i, 3) {\n\t\t\trep(j, 3) {\n\t\t\t\tcin >> f[i][j];\n\t\t\t}\n\t\t}\n\t\trep(i, 3) {\n\t\t\trep(j, 46656) {\n\t\t\t\tvector<int> v;\n\t\t\t\tint cop = j;\n\t\t\t\t\n\t\t\t\trep (k,6) {\n\t\t\t\t\tv.push_back(cop % 6 + 1); cop /= 6;\n\t\t\t\t}\n\t\t\t\tbool valid = true;\n\t\t\t\trep(k, 3) {\n\t\t\t\t\tif (t[k][i] > 0 && t[k][i] != v[k])valid = false;\n\t\t\t\t}\n\t\t\t\trep(k, 3) {\n\t\t\t\t\tif (f[k][i] > 0 && f[k][i] != v[3 + k])valid = false;\n\t\t\t\t}\n\t\t\t\tif (!valid)continue;\n\t\t\t\tint sum = 0;\n\t\t\t\trep(k, 3) {\n\t\t\t\t\trep(l, 3) {\n\t\t\t\t\t\tif (v[k]+v[3+l]==7||v[k]==v[3+l]) {\n\t\t\t\t\t\t\tvalid = false; break;\n\t\t\t\t\t\t}\n\t\t\t\t\t\tint u = ident(v[k], v[3 + l]);\n\t\t\t\t\t\tu--;\n\t\t\t\t\t\tsum += t3[3*k + l] * u;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\tif (!valid)continue;\n\t\t\t\tmp[i][sum] = true;\n\t\t\t\tif (i == 0)c.push_back(sum);\n\t\t\t}\n\t\t}\n\t\tsort(c.begin(), c.end());\n\t\tc.erase(unique(c.begin(), c.end()), c.end());\n\t\tvector<int> ans;\n\t\trep(i, c.size()) {\n\t\t\tif (mp[1][c[i]] && mp[2][c[i]]) {\n\t\t\t\tans.push_back(calc(c[i]));\n\t\t\t}\n\t\t}\n\t\tsort(ans.begin(), ans.end());\n\t\tans.erase(unique(ans.begin(), ans.end()), ans.end());\n\t\trep(i, ans.size()) {\n\t\t\tif (i > 0)cout << \" \";\n\t\t\tcout << ans[i];\n\t\t}\n\t\tif (ans.size() == 0)cout << 0;\n\t\tcout << endl;\n\t}\n}\nint main() {\n\tios::sync_with_stdio(false);\n\tcin.tie(0);\n\tsolve();\n\t//stop\n\treturn 0;\n}", "accuracy": 1, "time_ms": 190, "memory_kb": 3312, "score_of_the_acc": -0.1048, "final_rank": 17 } ]
aoj_1254_cpp
Problem G: Color the Map You were lucky enough to get a map just before entering the legendary magical mystery world. The map shows the whole area of your planned exploration, including several countries with complicated borders. The map is clearly drawn, but in sepia ink only; it is hard to recognize at a glance which region belongs to which country, and this might bring you into severe danger. You have decided to color the map before entering the area. “A good deal depends on preparation,” you talked to yourself. Each country has one or more territories, each of which has a polygonal shape. Territories belonging to one country may or may not “touch” each other, i.e. there may be disconnected territories. All the territories belonging to the same country must be assigned the same color. You can assign the same color to more than one country, but, to avoid confusion, two countries “adjacent” to each other should be assigned different colors. Two countries are considered to be “adjacent” if any of their territories share a border of non-zero length. Write a program that finds the least number of colors required to color the map. Input The input consists of multiple map data. Each map data starts with a line containing the total number of territories n , followed by the data for those territories. n is a positive integer not more than 100. The data for a territory with m vertices has the following format: String x 1 y 1 x 2 y 2 ... x m y m -1 “ String ” (a sequence of alphanumerical characters) gives the name of the country it belongs to. A country name has at least one character and never has more than twenty. When a country has multiple territories, its name appears in each of them. Remaining lines represent the vertices of the territory. A vertex data line has a pair of nonneg- ative integers which represent the x - and y -coordinates of a vertex. x - and y -coordinates are separated by a single space, and y-coordinate is immediately followed by a newline. Edges of the territory are obtained by connecting vertices given in two adjacent vertex data lines, and byconnecting vertices given in the last and the first vertex data lines. None of x - and y -coordinates exceeds 1000. Finally, -1 in a line marks the end of vertex data lines. The number of vertices m does not exceed 100. You may assume that the contours of polygons are simple, i.e. they do not cross nor touch themselves. No two polygons share a region of non-zero area. The number of countries in a map does not exceed 10. The last map data is followed by a line containing only a zero, marking the end of the input data. Output For each map data, output one line containing the least possible number of colors required to color the map satisfying the specified conditions. Sample Input 6 Blizid 0 0 60 0 60 60 0 60 0 50 50 50 50 10 0 10 -1 Blizid 0 10 10 10 10 50 0 50 -1 Windom 10 10 50 10 40 20 20 20 20 40 10 50 -1 Accent 50 10 50 50 35 50 35 25 -1 Pilot 35 25 35 50 10 50 -1 Blizid 20 20 40 20 20 ...(truncated)
[ { "submission_id": "aoj_1254_10852648", "code_snippet": "#include<iostream>\n#include<cstdio>\n#include<cstring>\n#include<cmath>\nusing namespace std;\nconst double eps=1e-5;\nstruct Point\n{ int x,y;\n Point(){}\n Point(int _,int __){x=_;y=__;}\n};\nbool operator == (const Point &x,const Point &y){return x.x==y.x&&x.y==y.y;}\nstruct Polygon{int tt;Point p[105];}P[105];\nchar s[15][100005],st[100005];\nint bh[1005],m[105][105],vst[105],tt;\nint Cross(Point C,Point A,Point B)\n{ return (A.x-C.x)*(B.y-C.y)-(B.x-C.x)*(A.y-C.y);}\nbool onseg(Point p1,Point p2,Point p)//p是否在[p1,p2]上 \n{ int minx=min(p1.x,p2.x),maxx=max(p1.x,p2.x),miny=min(p1.y,p2.y),maxy=max(p1.y,p2.y);\n return Cross(p,p1,p2)==0&&minx<=p.x&&p.x<=maxx&&miny<=p.y&&p.y<=maxy;\n}\ndouble Slope(Point A,Point B)\n{ if(A.x==B.x)return 999;\n if(A.x>B.x)swap(A,B);\n return atan2(A.y-B.y,A.x-B.x);\n}\ndouble Abs(double x){if(x<0)x=-x;return x;}\nbool Inter(int x1,int x2)\n{ int i,j;\n for(i=1;i<=P[x1].tt;i++)\n for(j=1;j<=P[x2].tt;j++)\n {if(Abs(Slope(P[x1].p[i],P[x1].p[i+1])-Slope(P[x2].p[j],P[x2].p[j+1]))>eps)continue;\n\t\t if(P[x1].p[i].x==P[x1].p[i+1].x&&P[x1].p[i].x==P[x2].p[j].x&&P[x2].p[j].x==P[x2].p[j+1].x)\n\t\t {if(max(P[x1].p[i].y,P[x1].p[i+1].y)<=min(P[x2].p[j].y,P[x2].p[j+1].y))continue;\n if(min(P[x1].p[i].y,P[x1].p[i+1].y)>=max(P[x2].p[j].y,P[x2].p[j+1].y))continue;\n\t\t\t}\n\t\t else\n\t\t {if(max(P[x1].p[i].x,P[x1].p[i+1].x)<=min(P[x2].p[j].x,P[x2].p[j+1].x))continue;\n if(min(P[x1].p[i].x,P[x1].p[i+1].x)>=max(P[x2].p[j].x,P[x2].p[j+1].x))continue;\n\t\t }\n\t\t if(onseg(P[x1].p[i],P[x1].p[i+1],P[x2].p[j])\n\t\t +onseg(P[x1].p[i],P[x1].p[i+1],P[x2].p[j+1])\n\t\t +onseg(P[x2].p[j],P[x2].p[j+1],P[x1].p[i])\n\t\t +onseg(P[x2].p[j],P[x2].p[j+1],P[x1].p[i+1])\n\t\t >=2)return true;\n\t\t }\n\treturn false;\n}\nbool DFS(int nowdep,int clr)\n{ if(nowdep==tt+1)return true;\n int i,j,bj;\n for(i=1;i<=clr;i++)//颜色 \n {for(j=1,bj=0;j<nowdep;j++)//验证 \n\t {if(m[nowdep][j]&&i==vst[j]){bj=1;break;}}\n\t if(!bj)\n\t {vst[nowdep]=i;\n\t\t if(DFS(nowdep+1,clr))return true;\n\t\t vst[nowdep]=0;\n\t\t }\n\t }\n\treturn false;\n}\nint main()\n{ int N,i,j,bj,len,x,y;\n\twhile(1)\n\t {scanf(\"%d\",&N);\n\t if(!N)break;\n\t tt=0;\n\t for(i=1;i<=N;i++)\n\t {scanf(\"%s\",st+1);\n\t\t for(j=1,bj=0;j<=tt;j++)\n\t\t {if(strcmp(st+1,s[j]+1)==0){bj=1;bh[i]=j;break;}}\n\t\t if(!bj)\n\t\t {bh[i]=++tt;\n\t\t\t len=strlen(st+1);\n\t\t\t for(j=1;j<=len+1;j++)s[tt][j]=st[j];\n\t\t\t}\n\t\t P[i].tt=0;\n\t\t while(1)\n\t\t {scanf(\"%d\",&x);\n\t\t if(x==-1)break;\n\t\t scanf(\"%d\",&y);\n\t\t P[i].tt++;P[i].p[P[i].tt]=Point(x,y);\n\t\t\t}\n\t\t P[i].p[P[i].tt+1]=P[i].p[1];\n\t\t }\n\t for(i=1;i<=tt;i++)for(j=1;j<=tt;j++)m[i][j]=0;\n\t for(i=1;i<=tt;i++)vst[i]=0;\n\t for(i=1;i<=N;i++)\n\t for(j=i+1;j<=N;j++)\n\t if(bh[i]!=bh[j]&&Inter(i,j))m[bh[i]][bh[j]]=m[bh[j]][bh[i]]=1;\n\t for(i=1;;i++)if(DFS(1,i)){printf(\"%d\\n\",i);break;}\n\t }\n\treturn 0;\n}", "accuracy": 1, "time_ms": 140, "memory_kb": 3948, "score_of_the_acc": -0.3227, "final_rank": 16 }, { "submission_id": "aoj_1254_9725071", "code_snippet": "#include <bits/stdc++.h>\n\nusing namespace std;\n\n#define rep(i, a, b) for (int i = (int)(a); i < (int)(b); i++)\n#define rrep(i, a, b) for (int i = (int)(b)-1; i >= (int)(a); i--)\n#define ALL(v) (v).begin(), (v).end()\n#define UNIQUE(v) sort(ALL(v)), (v).erase(unique(ALL(v)), (v).end())\n#define SZ(v) (int)v.size()\n#define MIN(v) *min_element(ALL(v))\n#define MAX(v) *max_element(ALL(v))\n#define LB(v, x) int(lower_bound(ALL(v), (x)) - (v).begin())\n#define UB(v, x) int(upper_bound(ALL(v), (x)) - (v).begin())\n\nusing uint = unsigned int;\nusing ll = long long int;\nusing ull = unsigned long long;\nusing i128 = __int128_t;\nusing u128 = __uint128_t;\nconst int inf = 0x3fffffff;\nconst ll INF = 0x1fffffffffffffff;\n\ntemplate <typename T> inline bool chmax(T &a, T b) {\n if (a < b) {\n a = b;\n return 1;\n }\n return 0;\n}\ntemplate <typename T> inline bool chmin(T &a, T b) {\n if (a > b) {\n a = b;\n return 1;\n }\n return 0;\n}\ntemplate <typename T, typename U> T ceil(T x, U y) {\n assert(y != 0);\n if (y < 0)\n x = -x, y = -y;\n return (x > 0 ? (x + y - 1) / y : x / y);\n}\ntemplate <typename T, typename U> T floor(T x, U y) {\n assert(y != 0);\n if (y < 0)\n x = -x, y = -y;\n return (x > 0 ? x / y : (x - y + 1) / y);\n}\ntemplate <typename T> int popcnt(T x) {\n return __builtin_popcountll(x);\n}\ntemplate <typename T> int topbit(T x) {\n return (x == 0 ? -1 : 63 - __builtin_clzll(x));\n}\ntemplate <typename T> int lowbit(T x) {\n return (x == 0 ? -1 : __builtin_ctzll(x));\n}\n\ntemplate <class T, class U>\nostream &operator<<(ostream &os, const pair<T, U> &p) {\n os << \"P(\" << p.first << \", \" << p.second << \")\";\n return os;\n}\ntemplate <typename T> ostream &operator<<(ostream &os, const vector<T> &vec) {\n os << \"{\";\n for (int i = 0; i < vec.size(); i++) {\n os << vec[i] << (i + 1 == vec.size() ? \"\" : \", \");\n }\n os << \"}\";\n return os;\n}\ntemplate <typename T, typename U>\nostream &operator<<(ostream &os, const map<T, U> &map_var) {\n os << \"{\";\n for (auto itr = map_var.begin(); itr != map_var.end(); itr++) {\n os << \"(\" << itr->first << \", \" << itr->second << \")\";\n itr++;\n if (itr != map_var.end())\n os << \", \";\n itr--;\n }\n os << \"}\";\n return os;\n}\ntemplate <typename T> ostream &operator<<(ostream &os, const set<T> &set_var) {\n os << \"{\";\n for (auto itr = set_var.begin(); itr != set_var.end(); itr++) {\n os << *itr;\n ++itr;\n if (itr != set_var.end())\n os << \", \";\n itr--;\n }\n os << \"}\";\n return os;\n}\n#ifdef LOCAL\n#define show(...) _show(0, #__VA_ARGS__, __VA_ARGS__)\n#else\n#define show(...) true\n#endif\ntemplate <typename T> void _show(int i, T name) {\n cerr << '\\n';\n}\ntemplate <typename T1, typename T2, typename... T3>\nvoid _show(int i, const T1 &a, const T2 &b, const T3 &...c) {\n for (; a[i] != ',' && a[i] != '\\0'; i++)\n cerr << a[i];\n cerr << \":\" << b << \" \";\n _show(i + 1, a, c...);\n}\n\n/**\n * @brief template\n */\n\nusing Point = pair<int,int>;\n\nbool isIntersect(int L1, int R1, int L2, int R2) {\n if (L1 > R1) swap(L1,R1);\n if (L2 > R2) swap(L2,R2);\n if (R2 <= L1 || R1 <= L2) return false;\n else return true;\n}\n\nbool isAdLine(Point A1, Point A2, Point B1, Point B2) {\n if (A1.first > A2.first) swap(A1,A2);\n if (B1.first > B2.first) swap(B1,B2);\n int AX = A2.first - A1.first, AY = A2.second - A1.second;\n int BX = B2.first - B1.first, BY = B2.second - B1.second;\n int ABX = B1.first - A1.first, ABY = B1.second - A1.second;\n if (AY*BX != AX*BY) return false;\n if (AY*ABX != AX*ABY) return false;\n if (A1.first == A2.first) return isIntersect(A1.second,A2.second,B1.second,B2.second);\n else return isIntersect(A1.first,A2.first,B1.first,B2.first);\n}\n\nbool isAdPoly(vector<Point> P1, vector<Point> P2) {\n int N = P1.size(), M = P2.size();\n rep(i,0,N) {\n rep(j,0,M) {\n if(isAdLine(P1[i],P1[(i+1)%N],P2[j],P2[(j+1)%M])) return true;\n }\n }\n return false;\n}\n\nint main() {\nwhile(1) {\n int N;\n cin >> N;\n if (N == 0) return 0;\n map<string,int> mp;\n vector<int> ID(N);\n vector<vector<Point>> Poly(N);\n int Cur = 0;\n rep(i,0,N) {\n string S;\n cin >> S;\n if (mp.count(S)) ID[i]=mp[S];\n else ID[i] = Cur, mp[S] = Cur, Cur++;\n int X, Y;\n while(1) {\n cin >> X;\n if (X == -1) break;\n cin >> Y;\n Poly[i].push_back({X,Y});\n }\n }\n int M = Cur;\n vector<vector<bool>> G(M,vector<bool>(M,false));\n rep(i,0,N) {\n rep(j,i+1,N) {\n if (ID[i] == ID[j]) continue;\n if (isAdPoly(Poly[i],Poly[j])) G[ID[i]][ID[j]] = G[ID[j]][ID[i]] = true;\n }\n }\n vector<bool> OK(1<<M,false);\n rep(i,0,1<<M) {\n bool check = true;\n rep(j,0,M) {\n if (!(i&(1<<j))) continue;\n rep(k,0,M) {\n if (!(i&(1<<k))) continue;\n if (G[j][k]) check = false;\n }\n }\n if (check) OK[i] = true;\n }\n vector<int> DP(1<<M,inf);\n DP[0] = 0;\n rep(i,0,1<<M) {\n rep(j,0,1<<M) {\n if (OK[j]) chmin(DP[i|j],DP[i]+1);\n }\n }\n cout << DP[(1<<M)-1] << endl;\n}\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3388, "score_of_the_acc": -0.0089, "final_rank": 3 }, { "submission_id": "aoj_1254_9007097", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\nusing P = pair<ll, ll>;\n#define rep(i, a, b) for(ll i = a; i < b; ++i)\n#define rrep(i, a, b) for(ll i = a; i >= b; --i)\nconstexpr ll inf = 4e18;\nstruct SetupIO {\n SetupIO() {\n ios::sync_with_stdio(0);\n cin.tie(0);\n cout << fixed << setprecision(30);\n }\n} setup_io;\nusing Real = long double;\nconst Real EPS = 1e-8, PI = acos(Real(-1.0));\nint sign(const Real& r) {\n if(r <= -EPS) return -1;\n if(r >= +EPS) return +1;\n return 0;\n}\nbool eq(const Real& a, const Real& b) {\n return sign(a - b) == 0;\n}\nusing Point = complex<Real>;\nistream& operator>>(istream& is, Point& p) {\n Real a, b;\n is >> a >> b;\n p = Point(a, b);\n return is;\n}\nostream& operator<<(ostream& os, const Point& p) {\n return os << p.real() << \" \" << p.imag();\n}\nPoint operator*(const Point& p, const Real& d) {\n return Point(p.real() * d, p.imag() * d);\n}\nPoint operator/(const Point& p, const Real& d) {\n return Point(p.real() / d, p.imag() / d);\n}\nPoint rot(const Point& p, Real theta) {\n return p * Point(cos(theta), sin(theta));\n}\nReal dot(const Point& p1, const Point& p2) {\n return (conj(p1) * p2).real();\n}\nReal cross(const Point& p1, const Point& p2) {\n return (conj(p1) * p2).imag();\n}\nReal dist(const Point& p1, const Point& p2) {\n return abs(p1 - p2);\n}\nbool comp_x(const Point& p1, const Point& p2) {\n return eq(p1.real(), p2.real()) ? p1.imag() < p2.imag() : p1.real() < p2.real();\n}\nbool comp_y(const Point& p1, const Point& p2) {\n return eq(p1.imag(), p2.imag()) ? p1.real() < p2.real() : p1.imag() < p2.imag();\n}\nbool comp_arg(const Point& p1, const Point& p2) {\n return arg(p1) < arg(p2);\n}\nint ccw(const Point& a, Point b, Point c) {\n b -= a;\n c -= a;\n if(sign(cross(b, c)) == 1) return 1;\n if(sign(cross(b, c)) == -1) return -1;\n if(sign(dot(b, c)) == -1) return +2;\n if(norm(b) < norm(c)) return -2;\n return 0;\n}\nReal closest_pair(vector<Point> ps) {\n if((int)ps.size() <= 1) return 1e18;\n sort(ps.begin(), ps.end(), comp_x);\n vector<Point> memo(ps.size());\n auto func = [&](auto& func, int l, int r) -> Real {\n if(r - l <= 1) return 1e18;\n int m = (l + r) >> 1;\n Real x = ps[m].real();\n Real res = min(func(func, l, m), func(func, m, r));\n inplace_merge(ps.begin() + l, ps.begin() + m, ps.begin() + r, comp_y);\n int cnt = 0;\n for(int i = l; i < r; ++i) {\n if(abs(ps[i].real() - x) >= res) continue;\n for(int j = 0; j < cnt; ++j) {\n Point d = ps[i] - memo[cnt - j - 1];\n if(d.imag() >= res) break;\n res = min(res, abs(d));\n }\n memo[cnt++] = ps[i];\n }\n return res;\n };\n return func(func, 0, (int)ps.size());\n}\nstruct Line {\n Point a, b;\n Line() = default;\n Line(const Point& a, const Point& b)\n : a(a), b(b) {}\n};\nusing Segment = Line;\nbool is_parallel(const Line& a, const Line& b) {\n return eq(cross(a.b - a.a, b.b - b.a), 0.0);\n}\nbool is_orthogonal(const Line& a, const Line& b) {\n return eq(dot(a.b - a.a, b.b - b.a), 0.0);\n}\nPoint projection(const Line& l, const Point& p) {\n Real t = dot(p - l.a, l.b - l.a) / norm(l.b - l.a);\n return l.a + (l.b - l.a) * t;\n}\nPoint reflection(const Line& l, const Point& p) {\n return p + (projection(l, p) - p) * 2.0;\n}\nbool is_intersect_lp(const Line& l, const Point& p) {\n return abs(ccw(l.a, l.b, p)) != 1;\n}\nbool is_intersect_sp(const Segment& s, const Point& p) {\n return ccw(s.a, s.b, p) == 0;\n}\nbool is_intersect_ll(const Line& l1, const Line& l2) {\n if(!eq(cross(l1.b - l1.a, l2.b - l2.a), 0.0)) return true;\n return eq(cross(l1.b - l1.a, l2.b - l1.a), 0.0);\n}\nbool is_intersect_ls(const Line& l, const Segment& s) {\n return sign(cross(l.b - l.a, s.a - l.a) * cross(l.b - l.a, s.b - l.a)) <= 0;\n}\nbool is_intersect_sl(const Segment& s, const Line& l) {\n return is_intersect_ls(l, s);\n}\nbool is_intersect_ss(const Segment& s1, const Segment& s2) {\n if(ccw(s1.a, s1.b, s2.a) * ccw(s1.a, s1.b, s2.b) > 0) return false;\n return ccw(s2.a, s2.b, s1.a) * ccw(s2.a, s2.b, s1.b) <= 0;\n}\nvector<Point> intersection_ll(const Line& l1, const Line& l2) {\n vector<Point> res;\n if(!is_intersect_ll(l1, l2)) return res;\n Real a = cross(l1.b - l1.a, l2.b - l2.a);\n Real b = cross(l1.b - l1.a, l1.b - l2.a);\n if(eq(a, 0.0) and eq(b, 0.0)) {\n res.push_back(l2.a);\n } else {\n res.push_back(l2.a + (l2.b - l2.a) * b / a);\n }\n return res;\n}\nvector<Point> intersection_ss(const Segment& s1, const Segment& s2) {\n return is_intersect_ss(s1, s2) ? intersection_ll(Line(s1), Line(s2)) : vector<Point>();\n}\nReal dist_lp(const Line& l, const Point& p) {\n return abs(p - projection(l, p));\n}\nReal dist_sp(const Segment& s, const Point& p) {\n Point h = projection(s, p);\n if(is_intersect_sp(s, h)) return abs(h - p);\n return min(abs(s.a - p), abs(s.b - p));\n}\nReal dist_ll(const Line& l1, const Line& l2) {\n if(is_intersect_ll(l1, l2)) return 0.0;\n return dist_lp(l1, l2.a);\n}\nReal dist_ss(const Segment& s1, const Segment& s2) {\n if(is_intersect_ss(s1, s2)) return 0.0;\n return min({dist_sp(s1, s2.a), dist_sp(s1, s2.b), dist_sp(s2, s1.a), dist_sp(s2, s1.b)});\n}\nReal dist_ls(const Line& l, const Segment& s) {\n if(is_intersect_ls(l, s)) return 0.0;\n return min(dist_lp(l, s.a), dist_lp(l, s.b));\n}\nReal dist_sl(const Segment& s, const Line& l) {\n return dist_ls(l, s);\n}\nint main(void) {\n while(true) {\n int n;\n cin >> n;\n if(n == 0) break;\n vector<int> c(n);\n vector<vector<Point>> p(n);\n map<string, int> mp;\n vector<vector<int>> area;\n int cnt = 0;\n rep(i, 0, n) {\n string country;\n cin >> country;\n if(mp.find(country) == mp.end()) {\n mp[country] = cnt;\n area.push_back({});\n cnt++;\n }\n c[i] = mp[country];\n area[c[i]].push_back(i);\n while(true) {\n int x, y;\n cin >> x;\n if(x == -1) break;\n cin >> y;\n p[i].emplace_back(x, y);\n }\n p[i].push_back(p[i][0]);\n }\n int m = area.size();\n vector<vector<bool>> adj(m, vector<bool>(m));\n rep(i, 0, n) {\n rep(j, i + 1, n) {\n bool flag = false;\n rep(k, 0, (int)p[i].size() - 1) {\n rep(l, 0, (int)p[j].size() - 1) {\n Point dir = p[i][k + 1] - p[i][k];\n Segment a1 = Segment(p[i][k] + EPS * dir, p[i][k + 1]);\n Segment a2 = Segment(p[i][k], p[i][k + 1] - EPS * dir);\n Segment b = Segment(p[j][l], p[j][l + 1]);\n if(!is_parallel(a1, b) or !is_intersect_ss(a1, b) or !is_intersect_ss(a2, b)) continue;\n flag = true;\n }\n }\n if(flag) {\n adj[c[i]][c[j]] = true;\n adj[c[j]][c[i]] = true;\n }\n }\n }\n vector<int> color(m, -1);\n int ans = m;\n auto dfs = [&](auto& dfs, int cur, int col) -> void {\n if(cur == (int)area.size()) {\n ans = min(ans, col + 1);\n return;\n }\n rep(i, 0, col + 2) {\n color[cur] = i;\n bool flag = true;\n rep(j, 0, cur) {\n if(adj[cur][j] and color[cur] == color[j]) {\n flag = false;\n }\n }\n if(flag) {\n dfs(dfs, cur + 1, max(col, (int)i));\n }\n color[cur] = -1;\n }\n };\n color[0] = 0;\n dfs(dfs, 1, 0);\n cout << ans << '\\n';\n }\n}", "accuracy": 1, "time_ms": 30, "memory_kb": 3372, "score_of_the_acc": -0.0515, "final_rank": 11 }, { "submission_id": "aoj_1254_8299651", "code_snippet": "#include <iostream>\n#include <vector>\n#include <algorithm>\nusing namespace std;\n\nstruct Point {\n\tint px, py;\n};\n\nbool operator<(const Point& a1, const Point& a2) {\n\tif (a1.px < a2.px) return true;\n\tif (a1.px > a2.px) return false;\n\tif (a1.py < a2.py) return true;\n\treturn false;\n}\n\nbool operator<=(const Point& a1, const Point& a2) {\n\tif (a1.px < a2.px) return true;\n\tif (a1.px > a2.px) return false;\n\tif (a1.py < a2.py) return true;\n\tif (a1.py > a2.py) return false;\n\treturn true;\n}\n\nPoint operator-(const Point& a1, const Point& a2) {\n\treturn Point{ a1.px - a2.px, a1.py - a2.py };\n}\n\nint crs(Point p1, Point p2) {\n\treturn p1.px * p2.py - p1.py * p2.px;\n}\n\nbool ShareBorder(Point p1, Point p2, Point p3, Point p4) {\n\tif (crs(p2 - p1, p3 - p1) != 0) return false;\n\tif (crs(p2 - p1, p4 - p1) != 0) return false;\n\tif (crs(p4 - p3, p1 - p3) != 0) return false;\n\tif (crs(p4 - p3, p2 - p3) != 0) return false;\n\tif (p2 < p1) swap(p2, p1);\n\tif (p4 < p3) swap(p4, p3);\n\tif (p2 <= p3 || p4 <= p1) return false;\n\treturn true;\n}\n\nint N;\nbool graph[109][109];\nstring Name[109];\nvector<Point> P[109];\n\nvoid Initialize() {\n\tfor (int i = 0; i < 109; i++) {\n\t\tP[i].clear();\n\t\tfor (int j = 0; j < 109; j++) graph[i][j] = false;\n\t}\n}\n\nbool Share(vector<Point> L1, vector<Point> L2) {\n\tfor (int i = 0; i < L1.size(); i++) {\n\t\tfor (int j = 0; j < L2.size(); j++) {\n\t\t\tPoint M1 = L1[(i + 0) % L1.size()];\n\t\t\tPoint M2 = L1[(i + 1) % L1.size()];\n\t\t\tPoint M3 = L2[(j + 0) % L2.size()];\n\t\t\tPoint M4 = L2[(j + 1) % L2.size()];\n\t\t\tif (ShareBorder(M1, M2, M3, M4) == true) {\n\t\t\t\treturn true;\n\t\t\t}\n\t\t}\n\t}\n\treturn false;\n}\n\nint dfs(int pos, int maxval, int lim, vector<int> Current) {\n\tif (pos == lim) return maxval;\n\n\t// Recursion\n\tint Return = (1 << 30);\n\tfor (int i = 1; i <= maxval + 1; i++) {\n\t\tbool flag = true;\n\t\tfor (int j = 0; j < pos; j++) {\n\t\t\tif (graph[j][pos] == true && Current[j] == i) flag = false;\n\t\t}\n\t\tif (flag == false) continue;\n\t\tvector<int> New = Current;\n\t\tNew.push_back(i);\n\t\tint ret = dfs(pos + 1, max(maxval, i), lim, New);\n\t\tReturn = min(Return, ret);\n\t}\n\treturn Return;\n}\n\nint main() {\n\twhile (true) {\n\t\tInitialize();\n\n\t\t// Step 1. Input\n\t\tcin >> N; if (N == 0) break;\n\t\tfor (int i = 0; i < N; i++) {\n\t\t\tcin >> Name[i];\n\t\t\twhile (true) {\n\t\t\t\tint px; cin >> px; if (px == -1) break;\n\t\t\t\tint py; cin >> py;\n\t\t\t\tP[i].push_back(Point{ px, py });\n\t\t\t}\n\t\t}\n\n\t\t// Step 2. Sort by Name\n\t\tvector<string> List;\n\t\tfor (int i = 0; i < N; i++) List.push_back(Name[i]);\n\t\tsort(List.begin(), List.end());\n\t\tList.erase(unique(List.begin(), List.end()), List.end());\n\n\n\t\t// Step 2. Brute Force\n\t\tfor (int i = 0; i < N; i++) {\n\t\t\tfor (int j = i + 1; j < N; j++) {\n\t\t\t\tif (Share(P[i], P[j]) == false) continue;\n\t\t\t\tint pos1 = lower_bound(List.begin(), List.end(), Name[i]) - List.begin();\n\t\t\t\tint pos2 = lower_bound(List.begin(), List.end(), Name[j]) - List.begin();\n\t\t\t\tgraph[pos1][pos2] = true;\n\t\t\t\tgraph[pos2][pos1] = true;\n\t\t\t}\n\t\t}\n\n\t\t// Step 3. Find Minimum Colors\n\t\tint Answer = dfs(0, 0, List.size(), vector<int>{});\n\n\t\t// Step 4. Output\n\t\tcout << Answer << endl;\n\t}\n\treturn 0;\n}", "accuracy": 1, "time_ms": 20, "memory_kb": 3232, "score_of_the_acc": -0.022, "final_rank": 8 }, { "submission_id": "aoj_1254_7959021", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\n#define rep(i, s, t) for (int i = (int)(s); i < (int)(t); ++i)\n#define revrep(i, t, s) for (int i = (int)(t)-1; i >= (int)(s); --i)\n#define all(x) begin(x), end(x)\ntemplate <typename T>\nbool chmax(T& a, const T& b) {\n return a < b ? (a = b, 1) : 0;\n}\ntemplate <typename T>\nbool chmin(T& a, const T& b) {\n return a > b ? (a = b, 1) : 0;\n}\n\nusing T = double;\nusing Vec = std::complex<T>;\n\nstd::istream& operator>>(std::istream& is, Vec& p) {\n T x, y;\n is >> x >> y;\n p = {x, y};\n return is;\n}\n\nT dot(const Vec& a, const Vec& b) { return (std::conj(a) * b).real(); }\n\nT cross(const Vec& a, const Vec& b) { return (std::conj(a) * b).imag(); }\n\nconst T PI = std::acos(-1);\nconstexpr T eps = 1e-8;\ninline bool eq(T a, T b) { return std::abs(a - b) <= eps; }\ninline bool eq(Vec a, Vec b) { return std::abs(a - b) <= eps; }\ninline bool lt(T a, T b) { return a < b - eps; }\ninline bool leq(T a, T b) { return a <= b + eps; }\n\nstruct Line {\n Vec p1, p2;\n Line() = default;\n Line(const Vec& p1, const Vec& p2) : p1(p1), p2(p2) {}\n Vec dir() const { return p2 - p1; }\n};\n\nstruct Segment : Line {\n using Line::Line;\n};\n\nstruct Circle {\n Vec c;\n T r;\n Circle() = default;\n Circle(const Vec& c, T r) : c(c), r(r) {}\n};\n\nusing Polygon = std::vector<Vec>;\n\nVec rot(const Vec& a, T ang) { return a * Vec(std::cos(ang), std::sin(ang)); }\n\nVec perp(const Vec& a) { return Vec(-a.imag(), a.real()); }\n\nVec projection(const Line& l, const Vec& p) {\n return l.p1 + dot(p - l.p1, l.dir()) * l.dir() / std::norm(l.dir());\n}\n\nVec reflection(const Line& l, const Vec& p) {\n return T(2) * projection(l, p) - p;\n}\n\n// 0: collinear\n// 1: counter-clockwise\n// -1: clockwise\nint ccw(const Vec& a, const Vec& b, const Vec& c) {\n if (eq(cross(b - a, c - a), 0)) return 0;\n if (lt(cross(b - a, c - a), 0)) return -1;\n return 1;\n}\n\nvoid sort_by_arg(std::vector<Vec>& pts) {\n std::sort(pts.begin(), pts.end(), [&](auto& p, auto& q) {\n if ((p.imag() < 0) != (q.imag() < 0)) return (p.imag() < 0);\n if (cross(p, q) == 0) {\n if (p == Vec(0, 0))\n return !(q.imag() < 0 || (q.imag() == 0 && q.real() > 0));\n if (q == Vec(0, 0))\n return (p.imag() < 0 || (p.imag() == 0 && p.real() > 0));\n return (p.real() > q.real());\n }\n return (cross(p, q) > 0);\n });\n}\n\nbool intersect(const Segment& s, const Vec& p) {\n Vec u = s.p1 - p, v = s.p2 - p;\n return eq(cross(u, v), 0) && leq(dot(u, v), 0);\n}\n\nint intersect(const Segment& s, const Segment& t) {\n auto a = s.p1, b = s.p2;\n auto c = t.p1, d = t.p2;\n if (ccw(a, b, c) != ccw(a, b, d) && ccw(c, d, a) != ccw(c, d, b)) return 2;\n if (intersect(s, c) || intersect(s, d) || intersect(t, a) ||\n intersect(t, b))\n return 1;\n return 0;\n}\n\nint intersect2(const Segment& s, const Segment& t) {\n if (!intersect(s, t)) return false;\n if (!eq(cross(s.dir(), t.dir()), 0)) return false;\n\n auto a = s.p1, b = s.p2;\n auto c = t.p1, d = t.p2;\n if (a.real() > b.real() || (a.real() == b.real() && a.imag() > b.imag()))\n swap(a, b);\n if (c.real() > d.real() || (c.real() == d.real() && c.imag() > d.imag()))\n swap(c, d);\n\n if (eq(a, c) || eq(b, d)) return true;\n if (eq(a, d) || eq(b, c)) return false;\n return true;\n}\n\nbool intersect(const Polygon& poly1, const Polygon& poly2) {\n const int n = poly1.size();\n const int m = poly2.size();\n for (int i = 0; i < n; ++i) {\n for (int j = 0; j < m; ++j) {\n if (intersect2(Segment(poly1[i], poly1[(i + 1) % n]),\n Segment(poly2[j], poly2[(j + 1) % m]))) {\n return true;\n }\n }\n }\n return false;\n}\n\nint chromatic_number(std::vector<std::vector<bool>>& G) {\n int n = G.size();\n std::vector<int> neighbor(n);\n for (int i = 0; i < (int)G.size(); ++i) {\n for (int j = 0; j < (int)G.size(); ++j) {\n if (G[i][j]) neighbor[i] |= 1 << j;\n }\n }\n // number of subsets of S that are independent\n std::vector<int> ind(1 << n);\n ind[0] = 1;\n for (int S = 1; S < (1 << n); ++S) {\n int v = __builtin_ctz(S);\n // (not containing v) + (containing v)\n ind[S] = ind[S ^ (1 << v)] + ind[(S ^ (1 << v)) & ~neighbor[v]];\n }\n // number of ways to choose k subsets of S that are independent\n auto f = ind;\n for (int k = 1;; ++k) {\n // numer of ways to choose k subsets of S so that they cover S\n int g = 0;\n for (int S = 0; S < (1 << n); ++S) {\n g += (__builtin_parity(S) ? -1 : 1) * f[S];\n }\n if (g) return k;\n for (int S = 1; S < (1 << n); ++S) {\n f[S] *= ind[S];\n }\n }\n}\n\nPolygon to_ccw(std::vector<Vec> vs) {\n auto c = Vec(0, 0);\n for (auto x : vs) c += x;\n c /= vs.size();\n for (auto& x : vs) x -= c;\n sort_by_arg(vs);\n for (auto& x : vs) x += c;\n return vs;\n}\n\nint main() {\n ios_base::sync_with_stdio(false);\n cin.tie(nullptr);\n cout << fixed << setprecision(15);\n\n while (true) {\n int n;\n cin >> n;\n if (n == 0) break;\n map<string, int> mp;\n vector<Polygon> ter(n);\n vector<int> id(n);\n rep(i, 0, n) {\n string name;\n cin >> name;\n if (!mp.count(name)) {\n mp[name] = mp.size();\n }\n id[i] = mp[name];\n while (true) {\n int x, y;\n cin >> x;\n if (x == -1) break;\n cin >> y;\n ter[i].push_back(Vec(x, y));\n }\n }\n int c = mp.size();\n vector<vector<bool>> G(c, vector<bool>(c));\n rep(i, 0, n) rep(j, i + 1, n) {\n if (intersect(ter[i], ter[j])) {\n G[id[i]][id[j]] = G[id[j]][id[i]] = true;\n }\n }\n\n cout << chromatic_number(G) << \"\\n\";\n }\n}", "accuracy": 1, "time_ms": 20, "memory_kb": 3424, "score_of_the_acc": -0.0326, "final_rank": 10 }, { "submission_id": "aoj_1254_6394918", "code_snippet": "#include<bits/stdc++.h>\n#define rep(i,a,...) for(int i = (a)*(strlen(#__VA_ARGS__)!=0);i<(int)(strlen(#__VA_ARGS__)?__VA_ARGS__:(a));++i)\n#define per(i,a,...) for(int i = (strlen(#__VA_ARGS__)?__VA_ARGS__:(a))-1;i>=(int)(strlen(#__VA_ARGS__)?(a):0);--i)\n#define foreach(i, n) for(auto &i:(n))\n#define all(x) (x).begin(), (x).end()\n#define bit(x) (1ll << (x))\n#define lambda(RES_TYPE, ...) (function<RES_TYPE(__VA_ARGS__)>)[&](__VA_ARGS__) -> RES_TYPE\n#define method(FUNC_NAME, RES_TYPE, ...) function<RES_TYPE(__VA_ARGS__)> FUNC_NAME = lambda(RES_TYPE, __VA_ARGS__)\nusing namespace std;\nusing ll = long long;\nusing pii = pair<int,int>;\nusing pll = pair<ll,ll>;\nconst ll MOD = (ll)1e9+7;\n//const ll MOD = 998244353;\nconst double EPS = 1e-8;\nconst int INF = (ll)1e9+7;\nconst ll INFLL = (ll)1e18;\ntemplate<class t>\nusing vvector = vector<vector<t>>;\ntemplate<class t>\nusing vvvector = vector<vector<vector<t>>>;\ntemplate<class t>\nusing priority_queuer = priority_queue<t, vector<t>, greater<t>>;\ntemplate<class t, class u> bool chmax(t &a, u b){if(a<b){a=b;return true;}return false;}\ntemplate<class t, class u> bool chmin(t &a, u b){if(a>b){a=b;return true;}return false;}\n#ifdef DEBUG\n#define debug(x) cout<<\"LINE \"<<__LINE__<<\": \"<<#x<<\" = \"<<x<<endl;\n#else\n#define debug(x) (void)0\n#endif\n\nnamespace templates{\n ll modpow(ll x, ll b,ll mod=MOD){\n ll res = 1;\n while(b){\n if(b&1)res = res * x % mod;\n x = x * x % mod;\n b>>=1;\n }\n return res;\n }\n\n ll modinv(ll x){\n return modpow(x, MOD-2);\n }\n\n bool was_output = false;\n\n void print();\n template <class t> void print(const vector<t> &);\n template <class t, class u> void print(const pair<t, u> &);\n template <class t> void print(const t&);\n template <class Head, class... Tail> void print(const Head&, const Tail&...);\n\n template <class t> void println(const vector<vector<t>>&);\n template <class t> void println(const vector<t>&);\n template <class t> void println(const t&);\n template <class Head, class... Tail> void println(const Head&, const Tail&...);\n void println();\n void newline();\n\n void print(){\n return;\n }\n\n template <class t>\n void print(const vector<t>&x){\n for(const t&i:x)print(i);\n }\n template <class t, class u>\n void print(const pair<t,u>&p){\n print(p.first);\n print(p.second);\n }\n template <class t>\n void print(const t&x){\n if(was_output)cout<<\" \";\n cout<<x;\n was_output = true;\n }\n template <class Head, class... Tail>\n void print(const Head&head,const Tail&...tail){\n print(head);\n print(tail...);\n }\n\n template<class t>\n void println(const vector<vector<t>>&x){\n for(vector<t> i:x)println(i);\n }\n template<class t>\n void println(const vector<t>&x){\n for(const t&i:x)print(i);\n println();\n }\n template <class t>\n void println(const t&x){\n print(x);\n println();\n }\n void println(){\n if(was_output){\n cout << endl;\n was_output = false;\n }\n }\n template <class Head, class... Tail>\n void println(const Head&head,const Tail&...tail){\n print(head);\n println(tail...);\n }\n\n void newline(){\n was_output = true;\n println();\n }\n\n template<class t>\n istream& operator>>(istream&is, vector<t>&x){\n for(auto &i:x)is >> i;\n return is;\n }\n\n template<class t, class u>\n istream& operator>>(istream&is, pair<t, u>&x){\n is >> x.first >> x.second;\n return is;\n }\n\n template<class t>\n ostream& operator<<(ostream&os, vector<t> &v){\n os << \"{\";\n for(t &i:v){\n os << i << \", \";\n }\n os << \"}\";\n return os;\n }\n\n template<class t = long long>\n t in(){\n t res; cin >> res; return res;\n }\n\n template<class t>\n vector<t> sorted(vector<t> line,function<bool(t,t)> comp=[](t a,t b){return a<b;}){\n sort(line.begin(),line.end(),comp);\n return line;\n }\n\n template<class t>\n vector<t> reversed(vector<t> line){\n reverse(line.begin(),line.end());\n return line;\n }\n string reversed(string str){\n reverse(str.begin(),str.end());\n return str;\n }\n\n long long gcd(long long a,long long b){\n while(b){\n a %= b;\n swap(a,b);\n }\n return a;\n }\n\n long long lcm(long long a,long long b){\n return a / gcd(a,b) * b;\n }\n\n class output_initializer{\n public:\n output_initializer(){\n ios::sync_with_stdio(false);\n cin.tie(nullptr);\n cout << setprecision(20);\n }\n };output_initializer OUTPUT_INITIALIZER_INSTANCE = output_initializer();\n}\n\nusing namespace templates;\n\nclass Vector2{\npublic:\n double x;\n double y;\n Vector2():x(0), y(0){}\n Vector2(double a, double b):x(a), y(b){}\n Vector2(const Vector2 &o):x(o.x), y(o.y){}\n\n Vector2& operator+=(Vector2 o){x+=o.x;y+=o.y;return *this;}\n Vector2& operator-=(Vector2 o){x-=o.x;y-=o.y;return *this; }\n Vector2& operator*=(double o){x*=o;y*=o;return *this;}\n Vector2& operator/=(double o){x/=o;y/=o;return *this;}\n\n Vector2 operator+(Vector2 o){return Vector2(*this)+=o;}\n Vector2 operator-(Vector2 o){return Vector2(*this)-=o;}\n Vector2 operator-(){return Vector2(-x,-y);}\n Vector2 operator*(double o){return Vector2(*this)*=o;}\n Vector2 operator/(double o){return Vector2(*this)/=o;}\n\n double abs(){\n return sqrt(mag());\n }\n\n double mag(){\n return x*x+y*y;\n }\n\n Vector2 normal(){\n return (*this) / abs();\n }\n\n Vector2 rotate_90(){\n return Vector2(-y,x);\n }\n\n static double dot(Vector2 a,Vector2 b){\n return a.x * b.x + a.y * b.y;\n }\n\n static double cross(Vector2 a,Vector2 b){\n return a.x * b.y - a.y * b.x;\n }\n};\n\ndouble dot(Vector2 x, Vector2 y){\n return Vector2::dot(x,y);\n}\n\ndouble cross(Vector2 x, Vector2 y){\n return Vector2::cross(x,y);\n}\n\nvoid output(Vector2 x){\n cout << x.x << \" \" << x.y << endl;\n}\n\nbool operator==(Vector2 a,Vector2 b){\n return (a-b).abs() <= EPS;\n}\n\n\nclass Line{\npublic:\n Vector2 f;\n Vector2 s;\n Line(){\n f = Vector2(0,0);\n s = Vector2(1,0);\n }\n Line(Vector2 a,Vector2 b):f(a),s(b){}\n\n Vector2 projection(Vector2 pos){\n pos -= f;\n Vector2 n = (s-f).normal();\n Vector2 res;\n res = n * dot(pos,n) + f;\n return res;\n }\n\n Vector2 reflection(Vector2 pos){\n Vector2 n;\n Vector2 res;\n pos -= f;\n double x = f.y-s.y;\n double y = s.x-f.x;\n n = Vector2(x,y).normal();\n res = pos + f - n * dot(pos,n) * 2;\n return res;\n }\n\n static bool is_cross(Line a,Line b){\n Vector2 ap = a.s - a.f;\n Vector2 bp = b.s - b.f;\n if(abs(Vector2::dot(ap,bp))<EPS){\n return true;\n }\n return false;\n }\n\n static bool is_cross_segment(Line a,Line b){\n if(Line::is_parallel(a,b)){\n return a.is_online(b.f) || a.is_online(b.s) || b.is_online(a.f) || b.is_online(a.s);\n }\n\t if(Vector2::cross(a.s-a.f,b.f-a.f)*Vector2::cross(a.s-a.f,b.s-a.f)>EPS)return false;\n\t if(Vector2::cross(b.s-b.f,a.f-b.f)*Vector2::cross(b.s-b.f,a.s-b.f)>EPS)return false;\nreturn true;\n }\n\n static bool is_parallel(Line a,Line b){\n Vector2 ap = a.s - a.f;\n Vector2 bp = b.s - b.f;\n if(abs(Vector2::cross(ap,bp))<EPS){\n return true;\n }\n return false;\n }\n\n static Vector2 cul_cross(Line a,Line b){\n if(a.is_online(b.f)){\n return b.f;\n }\n if(a.is_online(b.s)){\n return b.s;\n }\n if(b.is_online(a.f)){\n return a.f;\n }\n if(b.is_online(a.s)){\n return a.s;\n }\n Vector2 an = (a.s-a.f).rotate_90();\n if(Vector2::dot(b.f-a.f,an)<0){\n an = -an;\n }\n double dis_1 = Vector2::dot(b.f-a.f,an);\n double dis_2 = Vector2::dot(b.s-a.f,an);\n return b.f + (b.s - b.f) * dis_1 / (dis_1 - dis_2);\n }\n\n double cul_distance(Vector2 pos){\n Vector2 v = s-f;\n Vector2 n = v.normal();\n double k = v.abs();\n pos -= f;\n double dis = Vector2::dot(pos,n);\n if(dis>0){\n if(dis>k)dis=k;\n pos -= n * dis;\n }\n return pos.abs();\n }\n\n static double cul_distance(Line a,Line b){\n if(Line::is_cross_segment(a,b))return 0;\n return min({a.cul_distance(b.f),a.cul_distance(b.s),\n b.cul_distance(a.f),b.cul_distance(a.s)});\n }\n\n bool is_online(Vector2 pos){\n return cul_distance(pos) < EPS;\n }\n};\n\nVector2 input_vector2(){\n\tdouble x,y;\n\tcin >> x >> y;\n\treturn Vector2(x,y);\n}\n\nint func(int n){\n map<string,vvector<Vector2>> countries_;\n rep(_,n){\n string name = in<string>();\n vector<Vector2> territory;\n while(true){\n int x = in();\n if(x < 0)break;\n int y = in();\n territory.emplace_back(x,y);\n }\n countries_[name].emplace_back(territory);\n }\n int N = countries_.size();\n vvvector<Vector2> countries;\n foreach(c,countries_)countries.emplace_back(c.second);\n vvector<int> touches(N,vector<int>(N,false));\n method(isTouch2,bool,vector<Vector2> a,vector<Vector2> b){\n int n = a.size();\n int m = b.size();\n rep(i,n){\n rep(j,m){\n int i2 = (i + 1) % n;\n int j2 = (j + 1) % m;\n Line al(a[i],a[i2]);\n Line bl(b[j],b[j2]);\n if(not Line::is_parallel(al,bl))continue;\n if(not Line::is_cross_segment(al,bl))continue;\n Vector2 from = al.f;\n Vector2 to = al.s;\n Vector2 o = from;\n Vector2 n = (to - from).normal();\n vector<double> as;\n vector<double> bs;\n as.emplace_back(dot(al.f-o,n));\n as.emplace_back(dot(al.s-o,n));\n bs.emplace_back(dot(bl.f-o,n));\n bs.emplace_back(dot(bl.s-o,n));\n sort(all(as));\n sort(all(bs));\n if(max(as[0],bs[0]) + EPS < min(as[1],bs[1]))return true;\n }\n }\n return false;\n };\n method(isTouch,bool,vvector<Vector2> a,vvector<Vector2> b){\n foreach(i,a)foreach(j,b)if(isTouch2(i,j))return true;\n return false;\n };\n rep(i,N){\n rep(j,N){\n if(i==j)continue;\n touches[i][j] = isTouch(countries[i],countries[j]);\n }\n }\n vector<map<vector<int>,int>> dp(N);\n vector<int> colors;\n method(rec,int,int p){\n if(p==N){\n int res = 0;\n foreach(i,colors)chmax(res,i);\n return res + 1;\n }\n if(dp[p].count(colors))return dp[p][colors];\n int &it = dp[p][colors];\n it = INF;\n int maxs = 0;\n rep(i,p)chmax(maxs,colors[i]);\n rep(color,maxs+2){\n bool can = true;\n rep(i,p){\n if(colors[i]==color and touches[i][p])can = false;\n }\n if(not can)continue;\n colors.emplace_back(color);\n chmin(it,rec(p+1));\n colors.pop_back();\n }\n return it;\n };\n return rec(0);\n}\n\nint main(){\n ios::sync_with_stdio(false);\n cin.tie(nullptr);\n while(true){\n int n = in();\n if(n==0)break;\n println(func(n));\n }\n return 0;\n}", "accuracy": 1, "time_ms": 70, "memory_kb": 21200, "score_of_the_acc": -1.1304, "final_rank": 20 }, { "submission_id": "aoj_1254_6361862", "code_snippet": "#line 2 \"library/bits/stdc++.h\"\n\n// C\n#ifndef _GLIBCXX_NO_ASSERT\n#include <cassert>\n#endif\n#include <cctype>\n#include <cerrno>\n#include <cfloat>\n#include <ciso646>\n#include <climits>\n#include <clocale>\n#include <cmath>\n#include <csetjmp>\n#include <csignal>\n#include <cstdarg>\n#include <cstddef>\n#include <cstdio>\n#include <cstdlib>\n#include <cstring>\n#include <ctime>\n\n#if __cplusplus >= 201103L\n#include <ccomplex>\n#include <cfenv>\n#include <cinttypes>\n#include <cstdbool>\n#include <cstdint>\n#include <ctgmath>\n#include <cwchar>\n#include <cwctype>\n#endif\n\n// C++\n#include <algorithm>\n#include <bitset>\n#include <complex>\n#include <deque>\n#include <exception>\n#include <fstream>\n#include <functional>\n#include <iomanip>\n#include <ios>\n#include <iosfwd>\n#include <iostream>\n#include <istream>\n#include <iterator>\n#include <limits>\n#include <list>\n#include <locale>\n#include <map>\n#include <memory>\n#include <new>\n#include <numeric>\n#include <ostream>\n#include <queue>\n#include <set>\n#include <sstream>\n#include <stack>\n#include <stdexcept>\n#include <streambuf>\n#include <string>\n#include <typeinfo>\n#include <utility>\n#include <valarray>\n#include <vector>\n\n#if __cplusplus >= 201103L\n#include <array>\n#include <atomic>\n#include <chrono>\n#include <condition_variable>\n#include <forward_list>\n#include <future>\n#include <initializer_list>\n#include <mutex>\n#include <random>\n#include <ratio>\n#include <regex>\n#include <scoped_allocator>\n#include <system_error>\n#include <thread>\n#include <tuple>\n#include <typeindex>\n#include <type_traits>\n#include <unordered_map>\n#include <unordered_set>\n#endif\n#line 2 \"Source.cpp\"\nusing namespace std;\n\ntypedef string::const_iterator State;\n#define eps 1e-8L\n#define MAX_MOD 1000000007LL\n#define GYAKU 500000004LL\n#define MOD 998244353LL\n#define pb push_back\n#define mp make_pair\ntypedef long long ll;\ntypedef long double ld;\n#define REP(a, b) for (long long(a) = 0; (a) < (b); ++(a))\n#define ALL(x) (x).begin(), (x).end()\n\n#line 2 \"library/kotamanegi/geometry.hpp\"\nusing namespace std;\n\ntypedef long double ld;\ntypedef complex<ld> Point;\ntypedef pair<Point, Point> Segment;\ntypedef Segment Line;\n\nconst ld EPS = 1e-10;\n\nld abs(Line x)\n{\n return abs(x.second - x.first);\n}\n\n// dot = |a||b| cos(theta)\nld dot(Point a, Point b)\n{\n return a.real() * b.real() + a.imag() * b.imag();\n}\n\n// cross = |a||b| sin(theta)\nld cross(Point a, Point b)\n{\n return a.real() * b.imag() - a.imag() * b.real();\n}\n\n// 射影: 直線sと点pから引いた垂線の交点\nPoint projection(Line s, Point p)\n{\n Point base = s.second - s.first;\n ld r = dot(p - s.first, base) / norm(base);\n return s.first + base * r;\n}\n\n// 反射: 点pを直線sと線対称に移動した点\nPoint reflection(Line s, Point p)\n{\n return p + (projection(s, p) - p) * Point{2.0, 0.0};\n}\n\nenum class Position\n{\n COUNTER_CLOCKWISE, // 反時計回り\n CLOCKWISE, // 時計回り\n ON_LINE_BACK, // Lineの始点側に点がある\n ON_LINE_FRONT, // Lineの終点側に点がある\n ON_SEGMENT // Line上に点がある\n};\n\n// ccw: 線分sと点pの位置関係を示す\nPosition ccw(Line s, Point p)\n{\n Point a = s.second - s.first, b = p - s.first;\n if (cross(a, b) > eps)\n return Position::COUNTER_CLOCKWISE;\n if (cross(a, b) < -eps)\n return Position::CLOCKWISE;\n if (dot(a, b) < -EPS)\n return Position::ON_LINE_BACK;\n if (norm(a) < norm(b))\n return Position::ON_LINE_FRONT;\n return Position::ON_SEGMENT;\n}\n\n// 平行判定\nbool is_parallel(Point a, Point b) { return abs(cross(a, b)) < EPS; }\nbool is_parallel(Line a, Line b) { return is_parallel(a.second - a.first, b.second - b.first); }\n\n// 直交判定\nbool is_orthogonal(Point a, Point b) { return abs(dot(a, b)) < EPS; }\nbool is_orthogonal(Line a, Line b) { return is_orthogonal(a.second - a.first, b.second - b.first); }\n\n// 交差判定\nbool is_intersect(Segment s1, Segment s2)\n{\n for (int i = 0; i < 2; ++i)\n {\n if (cross(s1.second - s1.first, s2.first - s1.first) * cross(s1.second - s1.first, s2.second - s1.first) > eps)\n {\n return false;\n }\n if (ccw(s1, s2.first) == Position::ON_LINE_BACK and ccw(s1, s2.second) == Position::ON_LINE_BACK)\n {\n return false;\n }\n if (ccw(s1, s2.first) == Position::ON_LINE_FRONT and ccw(s1, s2.second) == Position::ON_LINE_FRONT)\n {\n return false;\n }\n swap(s1, s2);\n }\n return true;\n}\n\n// 交差点\nPoint intersection_point(Line s1, Line s2)\n{\n assert(!is_parallel(s1, s2));\n ld s, t;\n ld deno = cross(s1.second - s1.first, s2.second - s2.first);\n s = cross(s2.first - s1.first, s2.second - s2.first) / deno;\n return s1.first + s * (s1.second - s1.first);\n}\n\nld distance(Segment s, Point p)\n{\n if (dot(s.second - s.first, p - s.first) < eps)\n return abs(p - s.first);\n if (dot(s.first - s.second, p - s.second) < eps)\n return abs(p - s.second);\n return abs(cross(s.second - s.first, p - s.first)) / abs(s);\n}\n\nld distance(Segment s1, Segment s2)\n{\n if (is_intersect(s1, s2))\n {\n return 0;\n }\n return min({distance(s1, s2.first), distance(s1, s2.second), distance(s2, s1.first), distance(s2, s1.second)});\n}\n\ntypedef vector<Point> Polygon;\n\nld area(Polygon poly)\n{\n ld ans = 0;\n for (int i = 0; i < poly.size(); ++i)\n {\n Point a = poly[i], b = poly[(i + 1) % poly.size()];\n ans += cross(a, b);\n }\n ans /= 2.0L;\n return abs(ans);\n}\n\nbool is_convex(Polygon poly)\n{\n for (int i = 0; i < poly.size(); ++i)\n {\n Line a = {poly[i], poly[(i + 1) % poly.size()]};\n if (ccw(a, poly[(i + 2) % poly.size()]) == Position::CLOCKWISE)\n return false;\n }\n return true;\n}\n\nbool is_on_polygon(Polygon poly, Point p)\n{\n for (int i = 0; i < poly.size(); ++i)\n {\n Line a = {poly[i], poly[(i + 1) % poly.size()]};\n if (ccw(a, p) == Position::ON_SEGMENT)\n return true;\n }\n return false;\n}\n\nbool is_inside_polygon(Polygon poly, Point p)\n{\n if (is_on_polygon(poly, p))\n return true;\n const int n = poly.size();\n int wn = 0;\n for (int i = 0; i < n; ++i)\n {\n ld vt = (p.imag() - poly[i].imag()) / (poly[(i + 1) % n].imag() - poly[i].imag());\n if (p.real() < (poly[i].real() + vt * (poly[(i + 1) % n].real() - poly[i].real())))\n {\n if ((poly[i].imag() <= p.imag()) and (poly[(i + 1) % n].imag() > p.imag()))\n {\n wn++;\n }\n else if ((poly[i].imag() > p.imag()) and (poly[(i + 1) % n].imag() <= p.imag()))\n {\n wn--;\n }\n }\n }\n if (wn != 0)\n return true;\n return false;\n}\n#line 17 \"Source.cpp\"\n\n#define int long long\nint dp[(1 << 10)];\nvoid solve()\n{\n int n;\n cin >> n;\n if (n == 0)\n exit(0);\n\n map<string, vector<Polygon>> inputs;\n REP(i, n)\n {\n string s;\n cin >> s;\n Polygon poly;\n while (true)\n {\n int a;\n cin >> a;\n if (a == -1)\n break;\n int b;\n cin >> b;\n poly.push_back({(ld)a, (ld)b});\n }\n inputs[s].push_back(poly);\n }\n set<pair<string, string>> edges;\n vector<string> targets;\n for (auto x : inputs)\n {\n targets.push_back(x.first);\n for (auto y : inputs)\n {\n if (x.first == y.first)\n continue;\n int required_push = 0;\n for (auto poly1 : x.second)\n {\n for (auto poly2 : y.second)\n {\n REP(t, poly1.size())\n {\n REP(p, poly2.size())\n {\n if (is_intersect({poly1[t], poly1[(t + 1) % poly1.size()]}, {poly2[p], poly2[(p + 1) % poly2.size()]}))\n {\n if (poly1[t] == poly2[p])\n {\n Line a = {poly1[t], poly1[(t + 1) % poly1.size()]};\n Line b = {poly2[p], poly2[(p + 1) % poly2.size()]};\n if (abs(dot(a.second - a.first, b.second - b.first) - abs(a.second - a.first) * abs(b.second - b.first)) > eps)\n {\n continue;\n }\n }\n if (poly1[(t + 1) % poly1.size()] == poly2[p])\n {\n Line a = {poly1[(t + 1) % poly1.size()], poly1[t]};\n Line b = {poly2[p], poly2[(p + 1) % poly2.size()]};\n if (abs(dot(a.second - a.first, b.second - b.first) - abs(a.second - a.first) * abs(b.second - b.first)) > eps)\n {\n continue;\n }\n }\n if (poly1[t] == poly2[(p + 1) % poly2.size()])\n {\n Line a = {poly1[t], poly1[(t + 1) % poly1.size()]};\n Line b = {poly2[(p + 1) % poly2.size()], poly2[p]};\n if (abs(dot(a.second - a.first, b.second - b.first) - abs(a.second - a.first) * abs(b.second - b.first)) > eps)\n {\n continue;\n }\n }\n if (poly1[(t + 1) % poly1.size()] == poly2[(p + 1) % poly2.size()])\n {\n Line a = {poly1[(t + 1) % poly1.size()], poly1[t]};\n Line b = {poly2[(p + 1) % poly2.size()], poly2[p]};\n if (abs(dot(a.second - a.first, b.second - b.first) - abs(a.second - a.first) * abs(b.second - b.first)) > eps)\n {\n continue;\n }\n }\n required_push = 1;\n }\n }\n }\n }\n }\n if (required_push)\n {\n edges.insert(pair<string, string>{x.first, y.first});\n }\n }\n }\n REP(i, (1 << targets.size()))\n {\n dp[i] = 1e9;\n }\n dp[0] = 0;\n REP(i, (1 << targets.size()))\n {\n REP(q, (1 << targets.size()))\n {\n if (i & q)\n continue;\n vector<string> can_kouho;\n REP(j, targets.size())\n {\n if (q & (1 << j))\n {\n can_kouho.push_back(targets[j]);\n }\n }\n int bad = 0;\n for (auto x : can_kouho)\n {\n for (auto y : can_kouho)\n {\n if (edges.count({x, y}))\n {\n bad = 1;\n break;\n }\n }\n }\n if (!bad)\n {\n dp[i | q] = min(dp[i | q], dp[i] + 1);\n }\n }\n }\n cout << dp[(1 << targets.size()) - 1] << endl;\n}\n#undef int\n\n// generated by oj-template v4.7.2\n// (https://github.com/online-judge-tools/template-generator)\nint main()\n{\n // Fasterize input/output script\n ios::sync_with_stdio(false);\n cin.tie(nullptr);\n cout << fixed << setprecision(100);\n // scanf/printf user should delete this fasterize input/output script\n\n int t = 100000;\n // cin >> t; // comment out if solving multi testcase\n for (int testCase = 1; testCase <= t; ++testCase)\n {\n solve();\n }\n return 0;\n}", "accuracy": 1, "time_ms": 250, "memory_kb": 3408, "score_of_the_acc": -0.5318, "final_rank": 18 }, { "submission_id": "aoj_1254_5954865", "code_snippet": "#pragma GCC ta g(\"avx2\")\n#pragma GCC optimize(\"Ofast\")\n#pragma GCC optimize(\"unroll-loops\")\n#include <bits/stdc++.h>\nusing namespace std;\n#define DEBUG\n#ifdef DEBUG\ntemplate <class T, class U>\nostream &operator<<(ostream &os, const pair<T, U> &p) {\n os << '(' << p.first << ',' << p.second << ')';\n return os;\n}\ntemplate <class T> ostream &operator<<(ostream &os, const vector<T> &v) {\n os << '{';\n for(int i = 0; i < (int)v.size(); i++) {\n if(i) { os << ','; }\n os << v[i];\n }\n os << '}';\n return os;\n}\nvoid debugg() { cerr << endl; }\ntemplate <class T, class... Args>\nvoid debugg(const T &x, const Args &... args) {\n cerr << \" \" << x;\n debugg(args...);\n}\n#define debug(...) \\\n cerr << __LINE__ << \" [\" << #__VA_ARGS__ << \"]: \", debugg(__VA_ARGS__)\n#define dump(x) cerr << __LINE__ << \" \" << #x << \" = \" << (x) << endl\n#else\n#define debug(...) (void(0))\n#define dump(x) (void(0))\n#endif\nusing namespace std;\ntypedef long long ll;\ntypedef vector<ll> vl;\ntypedef vector<vl> vvl;\ntypedef vector<char> vc;\ntypedef vector<string> vs;\ntypedef vector<bool> vb;\ntypedef vector<double> vd;\ntypedef pair<ll,ll> P;\ntypedef pair<int,int> pii;\ntypedef vector<P> vpl;\ntypedef tuple<ll,ll,ll> tapu;\n#define rep(i,n) for(int i=0; i<(n); i++)\n#define REP(i,a,b) for(int i=(a); i<(b); i++)\n#define all(x) (x).begin(), (x).end()\n#define rall(x) (x).rbegin(), (x).rend()\ntemplate<typename T1,typename T2> inline bool chmin(T1 &a,T2 b){\n\tif(a>b){\n\t\ta = b; return true;\n\t}\n\telse return false;\n}\ntemplate<typename T1,typename T2> inline bool chmax(T1 &a,T2 b){\n\tif(a<b){\n\t\ta = b; return true;\n\t}\n\telse return false;\n}\ntemplate<typename T> inline void print(T &a){\n int sz = a.size();\n for(auto itr = a.begin(); itr != a.end(); itr++){\n\t\tcout << *itr;\n sz--;\n if(sz) cout << \" \";\n\t}\n cout << \"\\n\";\n}\ntemplate<typename T1,typename T2> inline void print2(T1 a, T2 b){\n\tcout << a << \" \" << b << \"\\n\";\n}\ntemplate<typename T1,typename T2,typename T3> inline void print3(T1 a, T2 b, T3 c){\n\tcout << a << \" \" << b << \" \" << c << \"\\n\";\n}\nvoid mark() {cout << \"#\" << \"\\n\";}\nll pcount(ll x) {return __builtin_popcountll(x);}\n//#include <atcoder/maxflow>\n//using namespace atcoder;\nconst int inf = (1<<30)-1;\nconst ll linf = 1LL<<61;\nconst int MAX = 510000;\nint dy[8] = {0,1,0,-1,1,-1,-1,1};\nint dx[8] = {-1,0,1,0,1,-1,1,-1};\nconst double pi = acos(-1);\nconst double eps = 1e-9;\nconst int mod = 1e9 + 7;\n//const int mod = 998244353;\n\nnamespace geometry {\n // Point : 複素数型を位置ベクトルとして扱う\n // 実軸(real)をx軸、挙軸(imag)をy軸として見る\n using D = double;\n using Point = complex<D>;\n const D EPS = 1e-7;\n const D PI = acosl(-1);\n\n inline bool equal(const D &a, const D &b) { return fabs(a - b) < EPS; }\n\n // 単位ベクトル(unit vector)を求める\n Point unitVector(const Point &a) { return a / abs(a); }\n\n // 法線ベクトル(normal vector)を求める\n // 90度回転した単位ベクトルをかける\n // -90度がよければPoint(0, -1)をかける\n Point normalVector(const Point &a) { return a * Point(0, 1); }\n\n // 内積(dot product) : a・b = |a||b|cosΘ\n D dot(const Point &a, const Point &b) {\n return (a.real() * b.real() + a.imag() * b.imag());\n }\n\n // 外積(cross product) : a×b = |a||b|sinΘ\n D cross(const Point &a, const Point &b) {\n return (a.real() * b.imag() - a.imag() * b.real());\n }\n\n // 点pを反時計回りにtheta度回転\n // thetaはラジアン!!!\n Point rotate(const Point &p, const D &theta) {\n return Point(cos(theta) * p.real() - sin(theta) * p.imag(),\n sin(theta) * p.real() + cos(theta) * p.imag());\n }\n\n // ラジアン->度\n D radianToDegree(const D &radian) { return radian * 180.0 / PI; }\n\n // 度->ラジアン\n D degreeToRadian(const D &degree) { return degree * PI / 180.0; }\n\n // 点の回転方向\n // 点a, b, cの位置関係について(aが基準点)\n int ccw(const Point &a, Point b, Point c) {\n b -= a, c -= a;\n // 点a, b, c が\n // 反時計回りの時、\n if(cross(b, c) > EPS) return 1;\n // 時計回りの時、\n if(cross(b, c) < -EPS) return -1;\n // c, a, bがこの順番で同一直線上にある時、\n if(dot(b, c) < 0) return 2;\n // a, b, cがこの順番で同一直線上にある場合、\n if(norm(b) < norm(c)) return -2;\n // cが線分ab上にある場合、\n return 0;\n }\n\n // Line : 直線を表す構造体\n // b - a で直線・線分を表せる\n struct Line {\n Point a, b;\n Line() = default;\n Line(Point a, Point b) : a(a), b(b) {}\n // Ax+By=C\n Line(D A, D B, D C) {\n if(equal(A, 0)) {\n a = Point(0, C / B), b = Point(1, C / B);\n } else if(equal(B, 0)) {\n b = Point(C / A, 0), b = Point(C / A, 1);\n } else {\n a = Point(0, C / B), b = Point(C / A, 0);\n }\n }\n };\n\n // Segment : 線分を表す構造体\n // Lineと同じ\n struct Segment : Line {\n Segment() = default;\n\n Segment(Point a, Point b) : Line(a, b) {}\n };\n\n // Circle : 円を表す構造体\n // pが中心の位置ベクトル、rは半径\n struct Circle {\n Point p;\n D r;\n\n Circle() = default;\n\n Circle(Point p, D r) : p(p), r(r) {}\n };\n\n // 2直線の直交判定 : a⊥b <=> dot(a, b) = 0\n bool isOrthogonal(const Line &a, const Line &b) {\n return equal(dot(a.b - a.a, b.b - b.a), 0);\n }\n // 2直線の平行判定 : a//b <=> cross(a, b) = 0\n bool isParallel(const Line &a, const Line &b) {\n return equal(cross(a.b - a.a, b.b - b.a), 0);\n }\n\n // 点cが直線ab上にあるか\n bool isPointOnLine(const Point &a, const Point &b, const Point &c) {\n return isParallel(Line(a, b), Line(a, c));\n }\n\n // 点cが\"線分\"ab上にあるか\n bool isPointOnSegment(const Point &a, const Point &b, const Point &c) {\n // |a-c| + |c-b| <= |a-b| なら線分上\n return (abs(a - c) + abs(c - b) < abs(a - b) + EPS);\n }\n\n // 直線lと点pの距離を求める\n D distanceBetweenLineAndPoint(const Line &l, const Point &p) {\n return abs(cross(l.b - l.a, p - l.a)) / abs(l.b - l.a);\n }\n\n // 線分lと点pの距離を求める\n // 定義:点pから「線分lのどこか」への最短距離\n D distanceBetweenSegmentAndPoint(const Segment &l, const Point &p) {\n if(dot(l.b - l.a, p - l.a) < EPS) return abs(p - l.a);\n if(dot(l.a - l.b, p - l.b) < EPS) return abs(p - l.b);\n return abs(cross(l.b - l.a, p - l.a)) / abs(l.b - l.a);\n }\n\n // 直線s, tの交点の計算\n Point crossPoint(const Line &s, const Line &t) {\n D d1 = cross(s.b - s.a, t.b - t.a);\n D d2 = cross(s.b - s.a, s.b - t.a);\n if(equal(abs(d1), 0) && equal(abs(d2), 0)) return t.a;\n return t.a + (t.b - t.a) * (d2 / d1);\n }\n\n // 線分s, tの交点の計算\n Point crossPoint(const Segment &s, const Segment &t) {\n return crossPoint(Line(s), Line(t));\n }\n\n // 線分sと線分tが交差しているかどうか\n // bound:線分の端点を含むか\n bool isIntersect(const Segment &s, const Segment &t, bool bound) {\n return ccw(s.a, s.b, t.a) * ccw(s.a, s.b, t.b) < bound &&\n ccw(t.a, t.b, s.a) * ccw(t.a, t.b, s.b) < bound;\n }\n\n // 線分sとtの距離\n D distanceBetweenSegments(const Segment &s, const Segment &t) {\n if(isIntersect(s, t, 1)) return (D)(0);\n D ans = distanceBetweenSegmentAndPoint(s, t.a);\n chmin(ans, distanceBetweenSegmentAndPoint(s, t.b));\n chmin(ans, distanceBetweenSegmentAndPoint(t, s.a));\n chmin(ans, distanceBetweenSegmentAndPoint(t, s.b));\n return ans;\n }\n\n // 射影(projection)\n // 直線(線分)lに点pから引いた垂線の足を求める\n Point projection(const Line &l, const Point &p) {\n D t = dot(p - l.a, l.a - l.b) / norm(l.a - l.b);\n return l.a + (l.a - l.b) * t;\n }\n\n Point projection(const Segment &l, const Point &p) {\n D t = dot(p - l.a, l.a - l.b) / norm(l.a - l.b);\n return l.a + (l.a - l.b) * t;\n }\n\n // 反射(reflection)\n // 直線lを対称軸として点pと線対称の位置にある点を求める\n Point reflection(const Line &l, const Point &p) {\n return p + (projection(l, p) - p) * (D)2.0;\n }\n\n // 2つの円の交差判定\n // 返り値は共通接線の数\n int isIntersect(const Circle &c1, const Circle &c2) {\n D d = abs(c1.p - c2.p);\n // 2つの円が離れている場合\n if(d > c1.r + c2.r + EPS) return 4;\n // 外接している場合\n if(equal(d, c1.r + c2.r)) return 3;\n // 内接している場合\n if(equal(d, abs(c1.r - c2.r))) return 1;\n // 内包している場合\n if(d < abs(c1.r - c2.r) - EPS) return 0;\n return 2;\n }\n\n // 2つの円の交点\n vector<Point> crossPoint(const Circle &c1, const Circle &c2) {\n vector<Point> res;\n int mode = isIntersect(c1, c2);\n // 2つの中心の距離\n D d = abs(c1.p - c2.p);\n // 2円が離れている場合\n if(mode == 4) return res;\n // 1つの円がもう1つの円に内包されている場合\n if(mode == 0) return res;\n // 2円が外接する場合\n if(mode == 3) {\n D t = c1.r / (c1.r + c2.r);\n res.emplace_back(c1.p + (c2.p - c1.p) * t);\n return res;\n }\n // 内接している場合\n if(mode == 1) {\n if(c2.r < c1.r - EPS) {\n res.emplace_back(c1.p + (c2.p - c1.p) * (c1.r / d));\n } else {\n res.emplace_back(c2.p + (c1.p - c2.p) * (c2.r / d));\n }\n return res;\n }\n // 2円が重なる場合\n D rc1 = (c1.r * c1.r + d * d - c2.r * c2.r) / (2 * d);\n D rs1 = sqrt(c1.r * c1.r - rc1 * rc1);\n if(c1.r - abs(rc1) < EPS) rs1 = 0;\n Point e12 = (c2.p - c1.p) / abs(c2.p - c1.p);\n res.emplace_back(c1.p + rc1 * e12 + rs1 * e12 * Point(0, 1));\n res.emplace_back(c1.p + rc1 * e12 + rs1 * e12 * Point(0, -1));\n return res;\n }\n\n // 点pが円cの内部(円周上も含む)に入っているかどうか\n bool isInCircle(const Circle &c, const Point &p) {\n D d = abs(c.p - p);\n return (equal(d, c.r) || d < c.r - EPS);\n }\n\n // 円cと直線lの交点\n vector<Point> crossPoint(const Circle &c, const Line &l) {\n vector<Point> res;\n D d = distanceBetweenLineAndPoint(l, c.p);\n // 交点を持たない\n if(d > c.r + EPS) return res;\n // 接する\n Point h = projection(l, c.p);\n if(equal(d, c.r)) {\n res.emplace_back(h);\n return res;\n }\n Point e = unitVector(l.b - l.a);\n D ph = sqrt(c.r * c.r - d * d);\n res.emplace_back(h - e * ph);\n res.emplace_back(h + e * ph);\n return res;\n }\n\n // 点pを通る円cの接線\n // 2本あるので、接点のみを返す\n vector<Point> tangentToCircle(const Point &p, const Circle &c) {\n return crossPoint(c, Circle(p, sqrt(norm(c.p - p) - c.r * c.r)));\n }\n\n // 円の共通接線\n vector<Line> tangent(const Circle &a, const Circle &b) {\n vector<Line> ret;\n // 2円の中心間の距離\n D g = abs(a.p - b.p);\n // 円が内包されている場合\n if(equal(g, 0)) return ret;\n Point u = unitVector(b.p - a.p);\n Point v = rotate(u, PI / 2);\n for(int s : {-1, 1}) {\n D h = (a.r + b.r * s) / g;\n if(equal(h * h, 1)) {\n ret.emplace_back(a.p + (h > 0 ? u : -u) * a.r,\n a.p + (h > 0 ? u : -u) * a.r + v);\n\n } else if(1 - h * h > 0) {\n Point U = u * h, V = v * sqrt(1 - h * h);\n ret.emplace_back(a.p + (U + V) * a.r,\n b.p - (U + V) * (b.r * s));\n ret.emplace_back(a.p + (U - V) * a.r,\n b.p - (U - V) * (b.r * s));\n }\n }\n return ret;\n }\n\n // 多角形の面積を求める\n D PolygonArea(const vector<Point> &p) {\n D res = 0;\n int n = p.size();\n for(int i = 0; i < n - 1; i++) res += cross(p[i], p[i + 1]);\n res += cross(p[n - 1], p[0]);\n return res * 0.5;\n }\n\n // 凸多角形かどうか\n bool isConvex(const vector<Point> &p) {\n int n = p.size();\n int now, pre, nxt;\n for(int i = 0; i < n; i++) {\n pre = (i - 1 + n) % n;\n nxt = (i + 1) % n;\n now = i;\n if(ccw(p[pre], p[now], p[nxt]) == -1) return false;\n }\n return true;\n }\n\n // 凸包 O(NlogN)\n vector<Point> ConvexHull(vector<Point> &p) {\n int n = (int)p.size(), k = 0;\n sort(all(p), [](const Point &a, const Point &b) {\n return (real(a) != real(b) ? real(a) < real(b) : imag(a) < imag(b));\n });\n vector<Point> ch(2 * n);\n // 一直線上の3点を含める -> (< -EPS)\n // 含め無い -> (< EPS)\n for(int i = 0; i < n; ch[k++] = p[i++]) { // lower\n while(k >= 2 &&\n cross(ch[k - 1] - ch[k - 2], p[i] - ch[k - 1]) < EPS)\n --k;\n }\n for(int i = n - 2, t = k + 1; i >= 0; ch[k++] = p[i--]) { // upper\n while(k >= t &&\n cross(ch[k - 1] - ch[k - 2], p[i] - ch[k - 1]) < EPS)\n --k;\n }\n ch.resize(k - 1);\n return ch;\n }\n\n // 多角形gに点pが含まれているか?\n // 含まれる:2, 辺上にある:1, 含まれない:0\n int isContained(const vector<Point> &g, const Point &p) {\n bool in = false;\n int n = (int)g.size();\n for(int i = 0; i < n; i++) {\n Point a = g[i] - p, b = g[(i + 1) % n] - p;\n if(imag(a) > imag(b)) swap(a, b);\n if(imag(a) <= EPS && EPS < imag(b) && cross(a, b) < -EPS) in = !in;\n if(cross(a, b) == 0 && dot(a, b) <= 0) return 1;\n }\n return (in ? 2 : 0);\n }\n\n vector<Point> convex_cut(const vector<Point> &g, Line l){\n vector<Point> ret;\n for(int i=0; i<g.size(); i++){\n Point now = g[i], nxt = g[(i+1)%g.size()];\n if(ccw(l.a,l.b,now) != -1) ret.push_back(now);\n if(ccw(l.a,l.b,now) * ccw(l.a,l.b,nxt) < 0){\n ret.push_back(crossPoint(Line(now,nxt),l));\n }\n }\n return ret;\n }\n\n bool equal (const Point &a, const Point &b) {\n return equal(a.real(),b.real()) && equal(a.imag(),b.imag());\n }\n} // namespace geometry\n\nusing namespace geometry;\n\nint main(){\n while(1){\n int n; cin >> n;\n if(!n) break;\n vs s(n);\n vector<vector<Point>> polys(n);\n rep(i,n){\n cin >> s[i];\n vector<Point> p;\n int x,y;\n while(1){\n cin >> x;\n if(x == -1) break;\n cin >> y;\n p.emplace_back(x,y);\n }\n polys[i] = p;\n }\n vector<vb> insec(n,vb(n,false));\n map<string,int> mp;\n int k = 0;\n rep(i,n){\n if(mp.count(s[i])) continue;\n mp[s[i]] = k++;\n }\n vvl G(k,vl(k,0));\n rep(i,n) rep(j,n){\n if(s[i] == s[j]) continue;\n int u = mp[s[i]], v = mp[s[j]];\n int si = polys[i].size(), sj = polys[j].size();\n bool f = false;\n rep(l,si){\n rep(m,sj){\n auto s1 = Segment(polys[i][l],polys[i][(l+1)%si]);\n auto s2 = Segment(polys[j][m],polys[j][(m+1)%sj]);\n if(!isParallel(s1,s2)) continue;\n if(equal(s1.a,s2.a) && equal(s1.b,s2.b)){\n f = true; break;\n }\n if(equal(s1.a,s2.b) && equal(s1.b,s2.a)){\n f = true; break;\n }\n if(s2.a!=s1.a && s2.a!=s1.b && ccw(s1.a,s1.b,s2.a)==0){\n f = true; break;\n }\n if(s2.b!=s1.a && s2.b!=s1.b && ccw(s1.a,s1.b,s2.b)==0){\n f = true; break;\n }\n }\n if(f) break;\n }\n if(f){\n G[u][v] = G[v][u] = 1;\n }\n }\n vb ok(1<<k,true);\n rep(bit,1<<k){\n rep(i,k) rep(j,k){\n if((bit>>i&1) && (bit>>j&1) && G[i][j]==1){\n ok[bit] = false;\n }\n }\n }\n vl dp(1<<k,inf);\n dp[0] = 0;\n rep(bit,1<<k){\n int S = (1<<k)-1-bit;\n for(int T=S; T>=0; T--){\n T &= S;\n if(ok[T]) chmin(dp[bit|T], dp[bit] + 1);\n }\n }\n cout << dp[(1<<k)-1] << \"\\n\";\n }\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3400, "score_of_the_acc": -0.0096, "final_rank": 5 }, { "submission_id": "aoj_1254_5922733", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\nvoid solve(int n) {\n vector<int> idx(n), m(n);\n vector<vector<pair<int, int>>> poly(n);\n map<string, int> mp;\n for (int i = 0; i < n; i++) {\n string S;\n cin >> S;\n if (!mp.count(S)) mp[S] = mp.size();\n idx[i] = mp[S];\n int x, y;\n while (cin >> x, x >= 0) {\n cin >> y;\n poly[i].emplace_back(x, y);\n }\n m[i] = poly[i].size();\n }\n\n auto check = [&](pair<int, int> a, pair<int, int> b, pair<int, int> c, pair<int, int> d) {\n if (a.first > b.first) swap(a, b);\n if (c.first > d.first) swap(c, d);\n pair<int, int> diag1, diag2;\n {\n int dx = b.first - a.first, dy = b.second - a.second;\n if (dx == 0) dy = 1;\n int g = gcd(dx, dy);\n diag1 = {dx / g, dy / g};\n }\n {\n int dx = d.first - c.first, dy = d.second - c.second;\n if (dx == 0) dy = 1;\n int g = gcd(dx, dy);\n diag2 = {dx / g, dy / g};\n }\n if (diag1 != diag2) return false;\n if (diag1.first == 0) {\n if (a.first != c.first) return false;\n if (a.second > b.second) swap(a, b);\n if (c.second > d.second) swap(c, d);\n return max(a.second, c.second) < min(b.second, d.second);\n }\n double y1 = a.second - double(a.first) / double(diag1.first) * diag1.second,\n y2 = c.second - double(c.first) / double(diag2.first) * diag2.second;\n if (y1 != y2) return false;\n return max(a.first, c.first) < min(b.first, d.first);\n };\n\n int N = mp.size();\n vector<vector<bool>> adj(N, vector<bool>(N, false));\n for (int i = 0; i < n; i++) {\n for (int j = i + 1; j < n; j++) {\n for (int k = 0; k < m[i]; k++) {\n for (int l = 0; l < m[j]; l++) {\n if (check(poly[i][k], poly[i][(k + 1) % m[i]], poly[j][l], poly[j][(l + 1) % m[j]])) {\n adj[idx[i]][idx[j]] = adj[idx[j]][idx[i]] = true;\n }\n }\n }\n }\n }\n\n vector<bool> ok(1 << N, true);\n for (int mask = 0; mask < (1 << N); mask++) {\n for (int i = 0; i < N; i++) {\n if (!(mask >> i & 1)) continue;\n for (int j = i + 1; j < N; j++) {\n if (!(mask >> j & 1)) continue;\n if (adj[i][j]) ok[mask] = false;\n }\n }\n }\n\n vector<int> dp(1 << N, N);\n dp[0] = 0;\n for (int mask = 0; mask < (1 << N); mask++) {\n for (int sub = mask; sub > 0; sub = (sub - 1) & mask) {\n if (ok[sub]) {\n dp[mask] = min(dp[mask], dp[mask ^ sub] + 1);\n }\n }\n }\n\n int ans = dp.back();\n cout << ans << '\\n';\n}\n\nint main() {\n cin.tie(0);\n ios::sync_with_stdio(false);\n int n;\n while (cin >> n, n) solve(n);\n return 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3388, "score_of_the_acc": -0.0089, "final_rank": 3 }, { "submission_id": "aoj_1254_5922732", "code_snippet": "#define LOCAL\n#include <bits/stdc++.h>\nusing namespace std;\n#pragma region Macros\ntypedef long long ll;\ntypedef __int128_t i128;\ntypedef unsigned int uint;\ntypedef unsigned long long ull;\n#define ALL(x) (x).begin(), (x).end()\n\ntemplate <typename T> istream& operator>>(istream& is, vector<T>& v) {\n for (T& x : v) is >> x;\n return is;\n}\ntemplate <typename T> ostream& operator<<(ostream& os, const vector<T>& v) {\n for (int i = 0; i < (int)v.size(); i++) {\n os << v[i] << (i + 1 == (int)v.size() ? \"\" : \" \");\n }\n return os;\n}\ntemplate <typename T, typename U> ostream& operator<<(ostream& os, const pair<T, U>& p) {\n os << '(' << p.first << ',' << p.second << ')';\n return os;\n}\ntemplate <typename T, typename U> ostream& operator<<(ostream& os, const map<T, U>& m) {\n os << '{';\n for (auto itr = m.begin(); itr != m.end();) {\n os << '(' << itr->first << ',' << itr->second << ')';\n if (++itr != m.end()) os << ',';\n }\n os << '}';\n return os;\n}\ntemplate <typename T, typename U> ostream& operator<<(ostream& os, const unordered_map<T, U>& m) {\n os << '{';\n for (auto itr = m.begin(); itr != m.end();) {\n os << '(' << itr->first << ',' << itr->second << ')';\n if (++itr != m.end()) os << ',';\n }\n os << '}';\n return os;\n}\ntemplate <typename T> ostream& operator<<(ostream& os, const set<T>& s) {\n os << '{';\n for (auto itr = s.begin(); itr != s.end();) {\n os << *itr;\n if (++itr != s.end()) os << ',';\n }\n os << '}';\n return os;\n}\ntemplate <typename T> ostream& operator<<(ostream& os, const multiset<T>& s) {\n os << '{';\n for (auto itr = s.begin(); itr != s.end();) {\n os << *itr;\n if (++itr != s.end()) os << ',';\n }\n os << '}';\n return os;\n}\ntemplate <typename T> ostream& operator<<(ostream& os, const unordered_set<T>& s) {\n os << '{';\n for (auto itr = s.begin(); itr != s.end();) {\n os << *itr;\n if (++itr != s.end()) os << ',';\n }\n os << '}';\n return os;\n}\ntemplate <typename T> ostream& operator<<(ostream& os, const deque<T>& v) {\n for (int i = 0; i < (int)v.size(); i++) {\n os << v[i] << (i + 1 == (int)v.size() ? \"\" : \" \");\n }\n return os;\n}\n\ntemplate <int i, typename T> void print_tuple(ostream&, const T&) {}\ntemplate <int i, typename T, typename H, class... Args> void print_tuple(ostream& os, const T& t) {\n if (i) os << ',';\n os << get<i>(t);\n print_tuple<i + 1, T, Args...>(os, t);\n}\ntemplate <typename... Args> ostream& operator<<(ostream& os, const tuple<Args...>& t) {\n os << '{';\n print_tuple<0, tuple<Args...>, Args...>(os, t);\n return os << '}';\n}\n\nvoid debug_out() { cerr << '\\n'; }\ntemplate <class Head, class... Tail> void debug_out(Head&& head, Tail&&... tail) {\n cerr << head;\n if (sizeof...(Tail) > 0) cerr << \", \";\n debug_out(move(tail)...);\n}\n#ifdef LOCAL\n#define debug(...) \\\n cerr << \" \"; \\\n cerr << #__VA_ARGS__ << \" :[\" << __LINE__ << \":\" << __FUNCTION__ << \"]\" << '\\n'; \\\n cerr << \" \"; \\\n debug_out(__VA_ARGS__)\n#else\n#define debug(...) 42\n#endif\n\ntemplate <typename T> T gcd(T x, T y) { return y != 0 ? gcd(y, x % y) : x; }\ntemplate <typename T> T lcm(T x, T y) { return x / gcd(x, y) * y; }\n\nint topbit(signed t) { return t == 0 ? -1 : 31 - __builtin_clz(t); }\nint topbit(long long t) { return t == 0 ? -1 : 63 - __builtin_clzll(t); }\nint botbit(signed a) { return a == 0 ? 32 : __builtin_ctz(a); }\nint botbit(long long a) { return a == 0 ? 64 : __builtin_ctzll(a); }\nint popcount(signed t) { return __builtin_popcount(t); }\nint popcount(long long t) { return __builtin_popcountll(t); }\nbool ispow2(int i) { return i && (i & -i) == i; }\n\ntemplate <class T> T ceil(T x, T y) {\n assert(y >= 1);\n return (x > 0 ? (x + y - 1) / y : x / y);\n}\ntemplate <class T> T floor(T x, T y) {\n assert(y >= 1);\n return (x > 0 ? x / y : (x - y + 1) / y);\n}\n\ntemplate <class T1, class T2> inline bool chmin(T1& a, T2 b) {\n if (a > b) {\n a = b;\n return true;\n }\n return false;\n}\ntemplate <class T1, class T2> inline bool chmax(T1& a, T2 b) {\n if (a < b) {\n a = b;\n return true;\n }\n return false;\n}\n#pragma endregion\n\nconst int INF = 1e9;\nconst long long IINF = 1e18;\nconst int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};\nconst char dir[4] = {'D', 'R', 'U', 'L'};\nconst long long MOD = 1000000007;\n// const long long MOD = 998244353;\n\nvoid solve(int n) {\n vector<int> idx(n), m(n);\n vector<vector<pair<int, int>>> poly(n);\n map<string, int> mp;\n for (int i = 0; i < n; i++) {\n string S;\n cin >> S;\n if (!mp.count(S)) mp[S] = mp.size();\n idx[i] = mp[S];\n int x, y;\n while (cin >> x, x >= 0) {\n cin >> y;\n poly[i].emplace_back(x, y);\n }\n m[i] = poly[i].size();\n }\n\n auto check = [&](pair<int, int> a, pair<int, int> b, pair<int, int> c, pair<int, int> d) {\n if (a.first > b.first) swap(a, b);\n if (c.first > d.first) swap(c, d);\n pair<int, int> diag1, diag2;\n {\n int dx = b.first - a.first, dy = b.second - a.second;\n if (dx == 0) dy = 1;\n int g = gcd(dx, dy);\n diag1 = {dx / g, dy / g};\n }\n {\n int dx = d.first - c.first, dy = d.second - c.second;\n if (dx == 0) dy = 1;\n int g = gcd(dx, dy);\n diag2 = {dx / g, dy / g};\n }\n if (diag1 != diag2) return false;\n if (diag1.first == 0) {\n if (a.first != c.first) return false;\n if (a.second > b.second) swap(a, b);\n if (c.second > d.second) swap(c, d);\n return max(a.second, c.second) < min(b.second, d.second);\n }\n double y1 = a.second - double(a.first) / double(diag1.first) * diag1.second,\n y2 = c.second - double(c.first) / double(diag2.first) * diag2.second;\n if (y1 != y2) return false;\n return max(a.first, c.first) < min(b.first, d.first);\n };\n\n int N = mp.size();\n vector<vector<bool>> adj(N, vector<bool>(N, false));\n for (int i = 0; i < n; i++) {\n for (int j = i + 1; j < n; j++) {\n for (int k = 0; k < m[i]; k++) {\n for (int l = 0; l < m[j]; l++) {\n if (check(poly[i][k], poly[i][(k + 1) % m[i]], poly[j][l], poly[j][(l + 1) % m[j]])) {\n adj[idx[i]][idx[j]] = adj[idx[j]][idx[i]] = true;\n }\n }\n }\n }\n }\n\n vector<bool> ok(1 << N, true);\n for (int mask = 0; mask < (1 << N); mask++) {\n for (int i = 0; i < N; i++) {\n if (!(mask >> i & 1)) continue;\n for (int j = i + 1; j < N; j++) {\n if (!(mask >> j & 1)) continue;\n if (adj[i][j]) ok[mask] = false;\n }\n }\n }\n\n vector<int> dp(1 << N, N);\n dp[0] = 0;\n for (int mask = 0; mask < (1 << N); mask++) {\n for (int sub = mask; sub > 0; sub = (sub - 1) & mask) {\n if (ok[sub]) {\n dp[mask] = min(dp[mask], dp[mask ^ sub] + 1);\n }\n }\n }\n\n int ans = dp.back();\n cout << ans << '\\n';\n}\n\nint main() {\n cin.tie(0);\n ios::sync_with_stdio(false);\n int n;\n while (cin >> n, n) solve(n);\n return 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3384, "score_of_the_acc": -0.0087, "final_rank": 2 }, { "submission_id": "aoj_1254_5737197", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\n#define ALL(x) begin(x),end(x)\n#define rep(i,n) for(int i=0;i<(n);i++)\n#define debug(v) cout<<#v<<\":\";for(auto x:v){cout<<x<<' ';}cout<<endl;\n#define mod 1000000007\nusing ll=long long;\nconst int INF=1000000000;\nconst ll LINF=1001002003004005006ll;\nint dx[]={1,0,-1,0},dy[]={0,1,0,-1};\n// ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}\ntemplate<class T>bool chmax(T &a,const T &b){if(a<b){a=b;return true;}return false;}\ntemplate<class T>bool chmin(T &a,const T &b){if(b<a){a=b;return true;}return false;}\n\nstruct IOSetup{\n IOSetup(){\n cin.tie(0);\n ios::sync_with_stdio(0);\n cout<<fixed<<setprecision(12);\n }\n} iosetup;\n\ntemplate<typename T>\nostream &operator<<(ostream &os,const vector<T>&v){\n for(int i=0;i<(int)v.size();i++) os<<v[i]<<(i+1==(int)v.size()?\"\":\" \");\n return os;\n}\ntemplate<typename T>\nistream &operator>>(istream &is,vector<T>&v){\n for(T &x:v)is>>x;\n return is;\n}\n\n// graph template\n// ref : https://ei1333.github.io/library/graph/graph-template.cpp\ntemplate<typename T=int>\nstruct Edge{\n int from,to;\n T w;\n int idx;\n Edge()=default;\n Edge(int from,int to,T w=1,int idx=-1):from(from),to(to),w(w),idx(idx){}\n operator int() const{return to;}\n};\n\ntemplate<typename T=int>\nstruct Graph{\n vector<vector<Edge<T>>> g;\n int V,E;\n Graph()=default;\n Graph(int n):g(n),V(n),E(0){}\n\n int size(){\n return (int)g.size();\n }\n void resize(int k){\n g.resize(k);\n V=k;\n }\n inline const vector<Edge<T>> &operator[](int k)const{\n return (g.at(k));\n }\n inline vector<Edge<T>> &operator[](int k){\n return (g.at(k));\n }\n void add_directed_edge(int from,int to,T cost=1){\n g[from].emplace_back(from,to,cost,E++);\n }\n void add_edge(int from,int to,T cost=1){\n g[from].emplace_back(from,to,cost,E);\n g[to].emplace_back(to,from,cost,E++);\n }\n void read(int m,int pad=-1,bool weighted=false,bool directed=false){\n for(int i=0;i<m;i++){\n int u,v;cin>>u>>v;\n u+=pad,v+=pad;\n T w=T(1);\n if(weighted) cin>>w;\n if(directed) add_directed_edge(u,v,w);\n else add_edge(u,v,w);\n }\n }\n};\n\ntemplate<typename T>\nint chromatic_number(Graph<T> G){\n int N=G.size();\n assert(N<28);\n\n vector<int> E(N,0);\n for(int i=0;i<N;i++){\n E[i]|=(1<<i);\n for(auto &e:G[i]) E[i]|=(1<<e.to);\n }\n\n // I[S] := Sの部分集合にある独立集合の個数\n vector<int> I(1<<N,0);\n I[0]=1;\n for(int S=1;S<(1<<N);S++){\n int v=__builtin_ctz(S);// 代表点\n I[S]=I[S&~(1<<v)]+I[S&~E[v]];\n }\n\n auto Pow=[](ll a,int k,ll p){\n ll ret=1;\n for(;k;k>>=1){\n if(k&1) ret=ret*a%p;\n a=a*a%p;\n }\n return ret;\n };\n\n for(int k=1;k<=N;k++){\n // g[S] := Sをちょうどカバーするk個の独立集合の組の個数\n // f[S] := Sの部分集合からk個重複を許して選ぶ個数\n // -> F[S] = I[S]^k {(a or b .. ) * (a or b ...)...}\n // -> f[S] = sum_{T in S} g[T]\n // -> g[S] = sum_{T in S} (-1)^(|S|-|T|) f[T]\n for(ll p:{1000000007,1000000021}){\n ll g=0;\n for(int S=0;S<(1<<N);S++){\n if((N-__builtin_popcount(S))&1) g-=Pow(I[S],k,p);\n else g+=Pow(I[S],k,p);\n g=(g+p)%p;\n }\n if(g>0) return k;\n }\n }\n return 0;\n}\n\nint N;\n\nusing P=pair<int,int>;\n\nvoid solve(){\n vector<vector<P>> poly(N);\n vector<int> belong(N);\n\n map<string,int> id;\n int n=0;\n rep(i,N){\n string S;cin>>S;\n if(!id.count(S)) id[S]=n++;\n belong[i]=id[S];\n\n int x,y;\n while(cin>>x,x>=0){\n cin>>y;\n poly[i].emplace_back(x,y);\n }\n }\n\n Graph<int> g(n);\n\n auto cross=[](P a,P b){\n return a.first*b.second-a.second*b.first;\n };\n\n auto dot=[&](P a,P b){\n return a.first*b.first+a.second*b.second;\n };\n\n auto norm=[&](P a){\n return a.first*a.first+a.second*a.second;\n };\n\n auto ison=[&](P a,P c,P p){\n c.first-=a.first,c.second-=a.second;\n p.first-=a.first,p.second-=a.second;\n if(c.first==0 and c.second==0) return false;\n if(p.first==0 and p.second==0) return false;\n if(p.first==c.first and p.second==c.second) return false;\n if(cross(p,c)>0) return false;\n if(cross(p,c)<0) return false;\n if(dot(p,c)<0) return false;\n return norm(p)<norm(c);\n };\n\n\n rep(i,N){\n rep(j,i){\n rep(ii,(int)poly[i].size()){\n rep(jj,(int)poly[j].size()){\n P ipa=poly[i][ii],ipb=poly[i][(ii+1)%poly[i].size()];\n P jpa=poly[j][jj],jpb=poly[j][(jj+1)%poly[j].size()];\n\n\n if((ipb.second-ipa.second)*(jpb.first-jpa.first) == (jpb.second-jpa.second)*(ipb.first-ipa.first)){\n bool f=false;\n {\n if(ipa==jpa and ipb==jpb) f=true;\n if(ipa==jpb and ipb==jpa) f=true;\n }\n if(ison(ipa,ipb,jpa)) f=true;\n if(ison(ipa,ipb,jpb)) f=true;\n if(ison(jpa,jpb,ipa)) f=true;\n if(ison(jpa,jpb,ipb)) f=true;\n if(f){\n // cout<<\"cross\"<<endl;\n // cout<<belong[i]<<\" - \"<<belong[j]<<endl;\n // cout<<\"(\"<<ipa.first<<\", \"<<ipa.second<<\") - (\"<<ipb.first<<\", \"<<ipb.second<<\")\"<<endl;\n // cout<<\"(\"<<jpa.first<<\", \"<<jpa.second<<\") - (\"<<jpb.first<<\", \"<<jpb.second<<\")\"<<endl;\n g.add_edge(belong[i],belong[j]);\n }\n }\n }\n }\n }\n }\n cout<<chromatic_number(g)<<endl;\n}\n\nsigned main(){\n while(cin>>N,N) solve();\n return 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3420, "score_of_the_acc": -0.0107, "final_rank": 6 }, { "submission_id": "aoj_1254_5734421", "code_snippet": "#include <stdio.h>\n#include <bits/stdc++.h>\nusing namespace std;\n#define rep(i,n) for (int i = 0; i < (n); ++i)\n#define Inf 1000000001\n\nconst double eps = 1e-10;\n\ntemplate <typename T>\nstruct vector_2d{\n\tT x,y,rad,dir;\n\t\n\tvector_2d(T a=0.0,T b=0.0){\n\t\tx = a;\n\t\ty = b;\n\t\tfix_rd();\n\t}\n\t\n\tvoid update_x(T a,T b){\n\t\tx = a*x + b;\n\t\tfix_rd();\n\t}\n\t\n\tvoid update_x(T a){\n\t\tupdate_x(0.0,a);\n\t}\n\t\n\tvoid update_y(T a,T b){\n\t\ty = a*y + b;\n\t\tfix_rd();\n\t}\n\t\n\tvoid update_y(T a){\n\t\tupdate_y(0.0,a);\n\t}\n\t\n\tvoid update_rad(T a,T b){\n\t\trad = a*rad + b;\n\t\tfix_xy();\n\t}\n\t\n\tvoid update_rad(T a){\n\t\tupdate_rad(0.0,a);\n\t}\n\t\n\tvoid update_dir(T a,T b){\n\t\tdir = a*dir + b;\n\t\tfix_xy();\n\t}\n\n\tvoid update_dir(T a){\n\t\tupdate_dir(0.0,a);\n\t}\n\t\n\tvoid fix_xy(){\n\t\tx = rad * cos(dir);\n\t\ty = rad * sin(dir);\n\t\tfix_rd();\n\t}\n\t\n\tvoid fix_rd(){\n\t\trad = hypot(x,y);\n\t\tif(rad==0.0)dir=0.0;\n\t\telse dir = atan2(y,x);\n\t\tfix_zero();\n\t}\n\t\n\tvoid fix_zero(){\n\t\tif(abs(x)<eps)x = 0.0;\n\t\tif(abs(y)<eps)y = 0.0;\n\t\tif(abs(rad)<eps)rad = 0.0;\n\t\tif(abs(dir)<eps)dir = 0.0;\n\t}\n\t\n\tvoid normalize(){\n\t\tT s = size();\n\t\tupdate_x(1.0/s,0.0);\n\t\tupdate_y(1.0/s,0.0);\n\t}\n\t\n\t\n\tT get_dis(vector_2d<T> V){\n\t\treturn hypot(x-V.x,y-V.y);\n\t}\n\t\n\tT size(){\n\t\treturn get_dis(vector_2d<T>());\n\t}\n\t\n\tT angle_difference(vector_2d<T> V){\n\t\tdouble ret = dir - V.dir;\n\t\tif(ret<-acos(-1.0))ret = acos(-1.0)*2.0+ret;\n\t\tif(ret>acos(-1.0))ret=-acos(-1.0)*2.0+ret;\n\t\treturn ret;\n\t}\n\t\n\t//中点\n\tvector_2d get_midpoint(vector_2d<T> V){\n\t\tV.update_x(0.5,x/2.0);\n\t\tV.update_y(0.5,y/2.0);\n\t\treturn V;\n\t}\n\t\n\tT get_inner_product(vector_2d<T> V){\n\t\treturn x*V.x+y*V.y;\n\t}\n\t\n\tT get_cross_product(vector_2d<T> V){\n\t\treturn x*V.y-y*V.x;\n\t}\n\t\n\tvector_2d &operator+=(const vector_2d<T> &another){\n\t\tupdate_x(1,another.x);\n\t\tupdate_y(1,another.y);\n\t\treturn (*this);\n\t}\n\t\n\tvector_2d &operator-=(const vector_2d<T> &another){\n\t\tupdate_x(1,-another.x);\n\t\tupdate_y(1,-another.y);\n\t\treturn (*this);\n\t}\n\t\n\tvector_2d operator+(const vector_2d<T> &another)const{\n\t\treturn (vector_2d(*this)+=another);\n\t}\n\t\n\tvector_2d operator-(const vector_2d<T> &another)const{\n\t\treturn (vector_2d(*this)-=another);\n\t}\n\t\n\tvoid show(){\n\t\tcout<<x<<','<<y<<endl;\n\t}\n};\n\n//a+tx\ntemplate <typename T>\nstruct line{\n\tvector_2d<T> a,t;\n\t\n\tline(vector_2d<T> V1,vector_2d<T> V2){\n\t\ta=V1;\n\t\tt=V2-V1;\n\t}\n\n\tT get_signed_dis(vector_2d<T> V){\n\t\tvector_2d<double> PA = a-V;\n\t\treturn PA.get_cross_product(t)/t.size();\n\t}\n\t\n\tT get_dis(vector_2d<T> V){\n\t\treturn abs(get_signed_dis(V));\n\t}\n\t\n\tvector_2d<T> get_projection(vector_2d<T> P){\n\t\tT r = t.get_inner_product(P-a)/t.size();\n\t\tvector_2d<T> temp = t;\n\t\ttemp.update_rad(0.0,r);\n\t\treturn a+temp;\n\t}\t\n\t\n\tvector_2d<T> get_cross_point(line<T> L){\n\t\tvector_2d<T> ret(1e20,1e20);\n\t\tif(abs(t.get_cross_product(L.t))<eps)return ret;\n\n\t\tT d0 = L.get_signed_dis(a);\n\t\tT d1 = L.get_signed_dis(a+t);\n\t\tvector_2d<T> temp = t;\n\t\ttemp.x *= d0/(d1-d0);\n\t\ttemp.y *= d0/(d1-d0);\n\t\tret = a - temp;\n\t\treturn ret;\n\t}\n\t\n};\n\ntemplate <typename T>\nstruct segment{\n\tvector_2d<T> V1,V2;\n\tsegment(vector_2d<T> a=vector_2d<T>(),vector_2d<T> b=vector_2d<T>()){\n\t\tV1=a;\n\t\tV2=b;\n\t}\n\t\n\tT get_dis(vector_2d<T> P){\n\t\tT ret = 1e20;\n\t\tline<T> L(V1,V2);\n\t\tvector_2d<T> Q = L.get_projection(P);\n\t\tif(Q.x+eps>min(V1.x,V2.x)&&Q.y+eps>min(V1.y,V2.y)\n\t\t&&Q.x<max(V1.x,V2.x)+eps&&Q.y<max(V1.y,V2.y)+eps)ret = min(ret,Q.get_dis(P));\n\t\telse{\n\t\t\tret = min(ret,P.get_dis(V1));\n\t\t\tret = min(ret,P.get_dis(V2));\n\t\t}\n\t\treturn ret;\n\t}\n\t\n\tT get_dis(segment<T> l){\n\t\tif(get_cross_point(l).x<1e20)return 0.0;\n\t\treturn min({get_dis(l.V1),get_dis(l.V2),l.get_dis(V1),l.get_dis(V2)});\n\t}\n\t\n\tvector_2d<T> get_cross_point(segment<T> l){\n\t\tline<T> L1(V1,V2),L2(l.V1,l.V2);\n\t\tvector_2d<T> P = L1.get_cross_point(L2);\n\t\tif(get_dis(P)<eps&&l.get_dis(P)<eps)return P;\n\t\treturn vector_2d<T> (1e20,1e20);\n\t}\n};\n\nint ans;\n\nvoid go(vector<vector<int>> &E,int cur,vector<int> colors){\n\tint m = 0;\n\trep(i,colors.size())m = max(m,colors[i]);\n\tif(cur==E.size()){\n\t\trep(i,E.size()){\n\t\t\trep(j,E[i].size()){\n\t\t\t\tint to = E[i][j];\n\t\t\t\tif(colors[i]==colors[to])return;\n\t\t\t}\n\t\t}\n\t\tans = min(ans,m+1);\n\t\treturn;\n\t}\n\trep(i,m+2){\n\t\tcolors[cur] = i;\n\t\tgo(E,cur+1,colors);\n\t}\n\t\n}\n\nint main(){\n\t\n\tint n;\n\twhile(cin>>n){\n\t\tif(n==0)break;\n\t\tmap<string,int> S;\n\t\t\n\t\tvector<int> id(n);\n\t\tvector<vector<int>> x(n),y(n);\n\t\t\n\t\trep(i,n){\n\t\t\tstring s;\n\t\t\tcin>>s;\n\t\t\t\n\t\t\tint t = S.size();\n\t\t\tif(S.count(s)){\n\t\t\t\tt = S[s];\n\t\t\t}\n\t\t\tS[s] = t;\n\t\t\tid[i] = t;\n\t\t\t\n\t\t\twhile(true){\n\t\t\t\tint X;\n\t\t\t\tcin>>X;\n\t\t\t\tif(X==-1)break;\n\t\t\t\tint Y;\n\t\t\t\tcin>>Y;\n\t\t\t\t\n\t\t\t\tx[i].push_back(X);\n\t\t\t\ty[i].push_back(Y);\n\t\t\t}\n\t\t\t\n\t\t}\n\t\t//cout<<'a'<<endl;\n\t\tvector<vector<int>> E(S.size());\n\t\t\n\t\trep(i,n){\n\t\t\trep(j,n){\n\t\t\t\tif(id[i]<=id[j])continue;\n\t\t\t\tbool f = true;\n\t\t\t\t\n\t\t\t\trep(k,x[i].size()){\n\t\t\t\t\tint t0 = k,t1 = (k+1)%x[i].size();\n\t\t\t\t\tvector_2d<double> v0(x[i][t0],y[i][t0]),v1(x[i][t1],y[i][t1]);\n\t\t\t\t\tsegment<double> s0(v0,v1);\n\t\t\t\t\trep(l,x[j].size()){\n\t\t\t\t\t\tint t2 = l,t3 = (l+1)%x[j].size();\n\t\t\t\t\t\tvector_2d<double> v2(x[j][t2],y[j][t2]),v3(x[j][t3],y[j][t3]);\n\t\t\t\t\t\tsegment<double> s1(v2,v3);\n\t\t\t\t\t\t\n\t\t\t\t\t\tif(abs((v3-v2).get_cross_product(v1-v0))>eps)continue;\n\t\t\t\t\t\tbool f0 = (v2-v0).size()<eps;\n\t\t\t\t\t\tbool f1 = (v2-v1).size()<eps;\n\t\t\t\t\t\tbool f2 = (v3-v0).size()<eps;\n\t\t\t\t\t\tbool f3 = (v3-v1).size()<eps;\n\t\t\t\t\t\tif((f0&&f3) || (f1&&f2)){\n\t\t\t\t\t\t\tf = false;\n\t\t\t\t\t\t\tgoto L;\n\t\t\t\t\t\t}\n\t\t\t\t\t\tif(!f0&&!f2&&s1.get_dis(v0)<eps){\n\t\t\t\t\t\t\tf = false;\n\t\t\t\t\t\t\tgoto L;\n\t\t\t\t\t\t}\n\t\t\t\t\t\tif(!f1&&!f3&&s1.get_dis(v1)<eps){\n\t\t\t\t\t\t\tf = false;\n\t\t\t\t\t\t\tgoto L;\n\t\t\t\t\t\t}\n\t\t\t\t\t\tif(!f0&&!f1&&s0.get_dis(v2)<eps){\n\t\t\t\t\t\t\tf = false;\n\t\t\t\t\t\t\tgoto L;\n\t\t\t\t\t\t}\n\t\t\t\t\t\tif(!f2&&!f3&&s0.get_dis(v3)<eps){\n\t\t\t\t\t\t\tf = false;\n\t\t\t\t\t\t\tgoto L;\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\tL:;\n\t\t\t\tif(!f){\n\t\t\t\t\tE[id[i]].push_back(id[j]);\n\t\t\t\t}\n\t\t\t\t\n\t\t\t}\n\t\t}\n\t\t\n\t\tans = 100000;\n\t\tvector<int> colors(S.size(),-1);\n\t\tgo(E,0,colors);\n\t\tcout<<ans<<endl;\n\t\t\n\t}\n\t\n\treturn 0;\n}", "accuracy": 1, "time_ms": 470, "memory_kb": 3948, "score_of_the_acc": -1.0401, "final_rank": 19 }, { "submission_id": "aoj_1254_5581887", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\n//geometry library by maine\n\n//basic\nusing Real = double;\nusing Point = complex<Real>;\n#define x real()\n#define y imag()\nconst Real EPS = 1e-8, PI = acos(-1), INF = 1e10;\n\nistream& operator>>(istream& is, Point& p) {\n Real a, b;\n is >> a >> b;\n p = Point(a, b);\n return is;\n}\nostream& operator<<(ostream& os, Point p) {\n return os << fixed << setprecision(10) << p.x << \" \" << p.y;\n}\n\nPoint operator*(const Point& p, const Real& d) { return Point(p.x * d, p.y * d); }\n\nbool eq(const Real& r, const Real& s) { return abs(r - s) < EPS; }\nbool eq(const Point& p, const Point& q) { return abs(p - q) < EPS; }\n\nnamespace std {\nbool operator<(const Point& a, const Point& b) {\n return !eq(a.x, b.x) ? a.x < b.x : a.y < b.y;\n}\n} // namespace std\n\nReal get_angle(const Point& a, const Point& b, const Point& c) {\n return arg((c - a) / (b - a));\n}\n\ninline int inc(int i, int N) {\n return (i == N - 1 ? 0 : i + 1);\n}\ninline int dec(int i, int N) {\n return (i == 0 ? N - 1 : i - 1);\n}\n\n//unit vector\nPoint e(const Point& p) { return p / abs(p); }\n\n//angle transformation\nReal radian_to_degree(Real r) { return r * 180.0 / PI; }\nReal degree_to_radian(Real d) { return d * PI / 180.0; }\n\n//basic functions\nReal dot(const Point& p, const Point& q) { return p.x * q.x + p.y * q.y; }\nReal cross(const Point& p, const Point& q) { return p.x * q.y - p.y * q.x; }\nPoint rotate(const Real& theta, const Point& p) {\n return {cos(theta) * p.x - sin(theta) * p.y, sin(theta) * p.x + cos(theta) * p.y};\n}\nPoint rotate90(const Point& p) {\n return {-p.y, p.x};\n}\n\n//structs : Line, Segment, Circle\nstruct Line {\n Point a, b;\n Line() = default;\n Line(Point a, Point b) : a(a), b(b) {}\n Point d() const { return e(b - a); } //direction vector\n Point n() const {\n Point dd = d();\n return {dd.y, -dd.x};\n } //normal vector\n};\nstruct Segment : Line {\n Segment() = default;\n Segment(Point a, Point b) : Line(a, b) {}\n};\nstruct Circle {\n Point p;\n Real r;\n Circle() = default;\n Circle(Point p, Real r) : p(p), r(r){};\n};\nusing Points = vector<Point>;\nusing Polygon = vector<Point>;\nusing Polygons = vector<Polygon>;\nusing Lines = vector<Line>;\nusing Segments = vector<Segment>;\nusing Circles = vector<Circle>;\n\n//happy functions\n//https://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_1_C\nint ccw(const Point& a, const Point& bb, const Point& cc) {\n Point b = bb - a, c = cc - a;\n if (cross(b, c) > EPS)\n return 1; //COUNTER_CLOCKWISE\n if (cross(b, c) < -EPS)\n return -1; //CLOCKWISE\n if (dot(b, c) < 0)\n return 2; //ONLINE_BACK c-a-b\n if (norm(b) < norm(c))\n return -2; //ONLINE_FRONT a-b-c\n return 0; //ON_SEGMENT a-c-b\n}\n\nbool parallel(const Line& l, const Line& m) { return eq(cross(l.d(), m.d()), 0.0); }\nbool orthogonal(const Line& l, const Line& m) { return eq(dot(l.d(), m.d()), 0.0); }\nPoint projection(const Line& l, const Point& p) { return l.a + (l.a - l.b) * (dot(p - l.a, l.a - l.b) / norm(l.a - l.b)); }\nPoint reflection(const Line& l, const Point& p) { return p + (projection(l, p) - p) * 2.0; }\n\n//intersect\nbool intersect(const Line& l, const Point& p) { return abs(ccw(l.a, l.b, p)) != 1; }\nbool intersect(const Line& l, const Line& m) { return !parallel(l, m) || (parallel(l, m) && intersect(l, m.a)); }\nbool intersect(const Segment& s, const Point& p) { return ccw(s.a, s.b, p) == 0; }\nbool intersect(const Line& l, const Segment& s) { return cross(l.d(), s.a - l.a) * cross(l.d(), s.b - l.a) < EPS; }\nbool intersect(const Segment& s, const Segment& t) {\n 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;\n}\n\n//distance\nReal distance(const Point& p, const Point& q) { return abs(p - q); }\nReal distance(const Line& l, const Point& p) { return distance(p, projection(l, p)); }\nReal distance(const Line& l, const Line& m) { return intersect(l, m) ? 0 : distance(l, m.a); }\nReal distance(const Segment& s, const Point& p) {\n Point q = projection(s, p);\n return intersect(s, q) ? distance(p, q) : min(distance(s.a, p), distance(s.b, p));\n}\nReal distance(const Segment& s, const Segment& t) { return intersect(s, t) ? 0 : min({distance(s, t.a), distance(s, t.b), distance(t, s.a), distance(t, s.b)}); }\nReal distance(const Line& l, const Segment& s) { return intersect(l, s) ? 0 : min(distance(l, s.a), distance(l, s.b)); }\n\n//intersect circle\nbool intersect(const Circle& c, const Point& p) { return eq(distance(c.p, p), c.r); }\nbool intersect(const Circle& c, const Line& l) { return distance(l, c.p) <= c.r + EPS; }\nint intersect(const Circle& c, const Segment& s) {\n if (!intersect(c, Line(s)))\n return 0;\n Real da = distance(c.p, s.a), db = distance(c.p, s.b);\n if (da < c.r + EPS && db < c.r + EPS)\n return 0;\n if (da > db)\n swap(da, db);\n if (da < c.r - EPS && db > c.r + EPS)\n return 1;\n if (intersect(s, projection(s, c.p)))\n return 2;\n return 0;\n}\nint intersect(Circle c1, Circle c2) {\n if (c1.r < c2.r)\n swap(c1, c2);\n Real d = distance(c1.p, c2.p);\n if (c1.r + c2.r < d)\n return 4;\n if (eq(c1.r + c2.r, d))\n return 3;\n if (c1.r - c2.r < d)\n return 2;\n if (eq(c1.r - c2.r, d))\n return 1;\n return 0;\n}\n\n//crosspoint\nPoint crosspoint(const Line& l, const Line& m) {\n Real A = cross(l.b - l.a, m.b - m.a);\n Real B = cross(l.b - l.a, l.b - m.a);\n if (eq(abs(A), 0.0) && eq(abs(B), 0.0))\n return m.a;\n return m.a + (m.b - m.a) * B / A;\n}\nPoint crosspoint(const Segment& s, const Segment& t) { return crosspoint(Line(s), Line(t)); }\npair<Point, Point> crosspoint(const Circle& c, const Line& l) {\n Point p = projection(l, c.p);\n if (eq(distance(l, c.p), c.r))\n return {p, p};\n Real len = sqrt(c.r * c.r - norm(p - c.p));\n return {p - len * l.d(), p + len * l.d()};\n}\npair<Point, Point> crosspoint(const Circle& c, const Segment& s) {\n Line l(s);\n if (intersect(c, s) == 2)\n return crosspoint(c, l);\n auto ret = crosspoint(c, l);\n if (dot(s.a - ret.first, s.b - ret.first) < 0)\n ret.second = ret.first;\n else\n ret.first = ret.second;\n return ret;\n}\npair<Point, Point> crosspoint(const Circle& c1, const Circle& c2) {\n Real d = distance(c1.p, c2.p);\n Real a = acos((c1.r * c1.r + d * d - c2.r * c2.r) / (2 * c1.r * d));\n Real t = atan2(c2.p.y - c1.p.y, c2.p.x - c1.p.x);\n Point p1 = c1.p + Point(cos(t + a) * c1.r, sin(t + a) * c1.r);\n Point p2 = c1.p + Point(cos(t - a) * c1.r, sin(t - a) * c1.r);\n return {p1, p2};\n}\n\n//tangent\npair<Point, Point> tangent(const Circle& c, const Point& p) {\n return crosspoint(c, Circle(p, sqrt(norm(c.p - p) - c.r * c.r)));\n}\nLines tangent(Circle c1, Circle c2) {\n Lines ret;\n if (c1.r < c2.r)\n swap(c1, c2);\n if (eq(c1.p, c2.p))\n return ret;\n Real g = norm(c1.p - c2.p);\n Point u = (c2.p - c1.p) / sqrt(g);\n Point v = rotate(PI / 2, u);\n for (int s : {-1, 1}) {\n Real h = (c1.r + s * c2.r) / sqrt(g);\n if (eq(1 - h * h, 0)) {\n ret.emplace_back(c1.p + u * c1.r, c1.p + (u + v) * c1.r);\n } else if (1 - h * h > 0) {\n Point uu = u * h, vv = v * sqrt(1 - h * h);\n ret.emplace_back(c1.p + (uu + vv) * c1.r, c2.p - (uu + vv) * c2.r * s);\n ret.emplace_back(c1.p + (uu - vv) * c1.r, c2.p - (uu - vv) * c2.r * s);\n }\n }\n return ret;\n}\n\n//convex\n//https://onlinejudge.u-aizu.ac.jp/courses/library/4/CGL/3/CGL_3_B\nbool is_convex(const Polygon& P) {\n int N = (int)P.size();\n for (int i = 0; i < N; i++) {\n if (ccw(P[i], P[(i + 1) % N], P[(i + 2) % N]) == -1)\n return false;\n }\n return true;\n}\n//https://onlinejudge.u-aizu.ac.jp/courses/library/4/CGL/4/CGL_4_A\nPolygon convex_hull(Points P) {\n int N = (int)P.size(), k = 0;\n if (N <= 2)\n return P;\n sort(begin(P), end(P));\n Points ret(2 * N);\n for (int i = 0; i < N; ret[k++] = P[i++]) {\n while (k >= 2 && cross(ret[k - 1] - ret[k - 2], P[i] - ret[k - 1]) < EPS)\n --k;\n }\n for (int i = N - 2, t = k + 1; i >= 0; ret[k++] = P[i--]) {\n while (k >= t && cross(ret[k - 1] - ret[k - 2], P[i] - ret[k - 1]) < EPS)\n --k;\n }\n ret.resize(k - 1);\n return ret;\n}\n//https://onlinejudge.u-aizu.ac.jp/courses/library/4/CGL/4/CGL_4_B\nReal convex_diameter(const Polygon& P) {\n int N = (int)P.size();\n int is = 0, js = 0;\n for (int i = 1; i < N; i++) {\n if (P[i].y < P[is].y)\n is = i;\n if (P[i].y > P[js].y)\n js = i;\n }\n Real maxdist = norm(P[is] - P[js]);\n int maxi, maxj, i, j;\n maxi = i = is;\n maxj = j = js;\n do {\n if (cross(P[inc(i, N)] - P[i], P[inc(j, N)] - P[j]) >= 0)\n j = inc(j, N);\n else\n i = inc(i, N);\n if (norm(P[i] - P[j]) > maxdist) {\n maxdist = norm(P[i] - P[j]);\n maxi = i;\n maxj = j;\n }\n } while (i != is || j != js);\n maxi = maxi;\n maxj = maxj;\n return sqrt(maxdist);\n}\n//https://onlinejudge.u-aizu.ac.jp/courses/library/4/CGL/4/CGL_4_C\nPolygon convex_cut(const Polygon& P, const Line& l) {\n Polygon ret;\n int N = (int)P.size();\n for (int i = 0; i < N; i++) {\n Point now = P[i], nxt = P[(inc(i, N))];\n if (ccw(l.a, l.b, now) != -1)\n ret.emplace_back(now);\n if (ccw(l.a, l.b, now) * ccw(l.a, l.b, nxt) == -1)\n ret.emplace_back(crosspoint(Line(now, nxt), l));\n }\n return ret;\n}\n\n//other\nenum\n{\n OUT,\n ON,\n IN\n};\n\n//https://onlinejudge.u-aizu.ac.jp/courses/library/4/CGL/3/CGL_3_C\nint contains(const Polygon& Q, const Point& p) {\n bool in = false;\n int N = (int)Q.size();\n for (int i = 0; i < N; i++) {\n Point a = Q[i] - p, b = Q[(i + 1) % N] - p;\n if (a.y > b.y)\n swap(a, b);\n if (a.y <= 0 && 0 < b.y && cross(a, b) < 0)\n in = !in;\n if (cross(a, b) == 0 && dot(a, b) <= 0)\n return ON;\n }\n return in ? IN : OUT;\n}\n\n//https://onlinejudge.u-aizu.ac.jp/courses/library/4/CGL/3/CGL_3_A\nReal area(const Polygon& P) {\n Real A = 0;\n for (int i = 0; i < (int)P.size(); i++) {\n A += cross(P[i], P[(i + 1) % P.size()]);\n }\n return A * 0.5;\n}\n\n//https://onlinejudge.u-aizu.ac.jp/courses/library/4/CGL/7/CGL_7_H\nReal cross_area(const Circle& c, const Point& a, const Point& b) {\n Point va = c.p - a, vb = c.p - b;\n Real f = cross(va, vb), ret = 0.0;\n if (eq(f, 0.0))\n return ret;\n if (max(abs(va), abs(vb)) < c.r + EPS)\n return f;\n if (distance(Segment(a, b), c.p) > c.r - EPS)\n return c.r * c.r * arg(vb * conj(va));\n auto u = crosspoint(c, Segment(a, b));\n vector<Point> V{a, u.first, u.second, b};\n for (int i = 0; i < (int)V.size() - 1; i++) {\n ret += cross_area(c, V[i], V[i + 1]);\n }\n return ret;\n}\nReal area(const Polygon& p, const Circle& c) {\n int N = (int)p.size();\n if (N < 3)\n return 0.0;\n Real ret = 0;\n for (int i = 0; i < N; i++) {\n ret += cross_area(c, p[i], p[inc(i, N)]);\n }\n return ret / 2.0;\n}\n\n//https://onlinejudge.u-aizu.ac.jp/problems/CGL_5_A\nReal closest_pair(Points& P, int left, int right) {\n if (right - left <= 1)\n return 1e18;\n int mid = (left + right) / 2;\n Real xx = P[mid].x;\n Real dist = min(closest_pair(P, left, mid), closest_pair(P, mid, right));\n inplace_merge(P.begin() + left, P.begin() + mid, P.begin() + right, [&](const Point& a, const Point& b) { return a.y < b.y; });\n Points memo;\n memo.reserve(right - left);\n for (int i = left; i < right; i++) {\n if (abs(P[i].x - xx) >= dist)\n continue;\n for (int j = (int)memo.size() - 1; j >= 0; j--) {\n if (P[i].y - memo[j].y >= dist)\n break;\n dist = min(dist, distance(P[i], memo[j]));\n }\n memo.emplace_back(P[i]);\n }\n return dist;\n}\nReal closest_pair(Points P) {\n sort(begin(P), end(P));\n return closest_pair(P, 0, (int)P.size());\n}\n\n//https://onlinejudge.u-aizu.ac.jp/courses/library/4/CGL/7/CGL_7_B\nCircle incircle(Polygon P) {\n assert((int)P.size() == 3);\n Real l0 = distance(P[1], P[2]), l1 = distance(P[0], P[2]), l2 = distance(P[0], P[1]);\n Circle c;\n c.p = crosspoint(Line(P[0], (P[1] * l1 + P[2] * l2) / (l1 + l2)), Line(P[1], (P[0] * l0 + P[2] * l2) / (l0 + l2)));\n c.r = distance(Line(P[0], P[1]), c.p);\n return c;\n}\n\n//https://onlinejudge.u-aizu.ac.jp/courses/library/4/CGL/7/CGL_7_C\nCircle circumcircle(Polygon P) {\n assert((int)P.size() == 3);\n Circle c;\n c.p = crosspoint(Line((P[0] + P[1]) / 2.0, (P[0] + P[1]) / 2.0 + rotate90(P[0] - P[1])),\n Line((P[0] + P[2]) / 2.0, (P[0] + P[2]) / 2.0 + rotate90(P[0] - P[2])));\n c.r = distance(c.p, P[0]);\n return c;\n}\n\n#undef x\n#undef y\n\nint chromatic(vector<vector<int>> G) {\n int N = (int)G.size();\n if (N == 0)\n return 0;\n vector<int> mask(N);\n for (int i = 0; i < N; i++) {\n for (auto&& j : G[i]) {\n mask[i] |= 1 << j;\n }\n }\n vector<int> dp(1 << N);\n for (int i = 0; i < (1 << N); i++) {\n dp[i] = ((N - __builtin_popcount(i)) & 1 ? -1 : 1);\n }\n auto calc = [&](const int mod) {\n vector<int> ret(1 << N);\n ret[0] = 1;\n for (int bit = 1; bit < (1 << N); bit++) {\n int ctz = __builtin_ctz(bit);\n ret[bit] = ret[bit ^ (1 << ctz)] + ret[(bit - (1 << ctz)) & ~mask[ctz]];\n if (ret[bit] >= mod)\n ret[bit] -= mod;\n }\n return ret;\n };\n const int mod1 = 1077563119, mod2 = 1000000007;\n auto cnt1 = calc(mod1);\n auto cnt2 = calc(mod2);\n using ll = long long;\n auto dp1 = dp, dp2 = dp;\n for (int i = 1; i < N; i++) {\n ll sum1 = 0, sum2 = 0;\n for (int bit = 0; bit < (1 << N); bit++) {\n dp1[bit] = (int)(((ll)dp1[bit] * cnt1[bit]) % mod1);\n sum1 += dp1[bit];\n dp2[bit] = (int)(((ll)dp2[bit] * cnt2[bit]) % mod2);\n sum2 += dp2[bit];\n }\n if (sum1 % mod1 != 0 || sum2 % mod2 != 0)\n return i;\n }\n return N;\n}\n\nint main() {\n int N;\n while (cin >> N, N) {\n map<string, Polygons> mp;\n while (N--) {\n string S;\n cin >> S;\n int x, y;\n Polygon P;\n while (cin >> x, x >= 0) {\n cin >> y;\n P.emplace_back(x, y);\n }\n mp[S].emplace_back(P);\n }\n N = 0;\n map<string, int> idx;\n for (auto&& [key, vec] : mp) {\n if (!idx.count(key))\n idx[key] = N++;\n }\n vector<vector<int>> G(N);\n for (auto&& [key1, vec1] : mp) {\n for (auto&& [key2, vec2] : mp) {\n int idx1 = idx[key1], idx2 = idx[key2];\n if (idx1 == idx2)\n continue;\n bool flag = false;\n for (auto&& P1 : vec1) {\n for (auto&& P2 : vec2) {\n int N1 = (int)P1.size(), N2 = (int)P2.size();\n for (int i1 = 0; i1 < N1; i1++) {\n for (int i2 = 0; i2 < N2; i2++) {\n auto s1 = Segment(P1[i1], P1[inc(i1, N1)]), s2 = Segment(P2[i2], P2[inc(i2, N2)]);\n if (intersect(s1, s2) && parallel(s1, s2)) {\n if ((intersect(s1, Segment(s2.a, s2.b + s2.d() * 0.1)) && intersect(s1, Segment(s2.a, s2.b - s2.d() * 0.1)))\n && (intersect(s1, Segment(s2.b, s2.a + s2.d() * 0.1)) && intersect(s1, Segment(s2.b, s2.a - s2.d() * 0.1))))\n flag = true;\n }\n }\n }\n }\n }\n if (flag)\n G[idx1].emplace_back(idx2);\n }\n }\n cout << chromatic(G) << endl;\n }\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3448, "score_of_the_acc": -0.0122, "final_rank": 7 }, { "submission_id": "aoj_1254_5456390", "code_snippet": "#pragma GCC optimize(\"Ofast\")\n#include <bits/stdc++.h>\nusing namespace std;\ntypedef long long int ll;\ntypedef unsigned long long ull;\n\nmt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());\nll myRand(ll B) {\n return (ull)rng() % B;\n}\n\nstruct Point{\n int x,y;\n};\n\nint dot(Point a,Point b){\n return a.x*b.x+a.y*b.y;\n}\n\nint cross(Point a,Point b){\n return a.x*b.y-a.y*b.x;\n}\n\nint ccw(Point a,Point b,Point c){\n b={b.x-a.x,b.y-a.y};\n c={c.x-a.x,c.y-a.y};\n if(cross(b,c)>0)return 1; // a,b,c反時計回り\n if(cross(b,c)<0)return -1; // a,b,c時計周り\n if(dot(b,c)<0)return 2; // c,a,b 一直線\n if((b.x*b.x+b.y*b.y)<(c.x*c.x+c.y*c.y))return -2; // a,b,c 一直線\n return 0; // a,c,b 一直線\n}\n\nint main(){\n cin.tie(nullptr);\n ios::sync_with_stdio(false);\n int n;\n while(cin >> n,n){\n int m=0;\n map<string,int> mp;\n vector<string> s(n);\n vector<vector<pair<int,int>>> v(n); \n for(int i=0;i<n;i++){\n cin >> s[i];\n if(mp.find(s[i])==mp.end()){\n mp[s[i]]=m; m++;\n }\n int x,y;\n while(cin >> x,x!=-1){\n cin >> y;\n v[i].push_back({x,y});\n }\n }\n auto check=[&](int i,int j)->bool{\n for(int k=0;k<v[i].size();k++){\n Point A={v[i][k].first,v[i][k].second};\n Point B={v[i][(k+1)%v[i].size()].first,v[i][(k+1)%v[i].size()].second};\n for(int kk=0;kk<v[j].size();kk++){\n Point C={v[j][kk].first,v[j][kk].second};\n // if(A.x==C.x and A.y==C.y)continue;\n // if(B.x==C.x and B.y==C.y)continue;\n if(ccw(A,B,C)==0){\n if(A.x==C.x and A.y==C.y){\n Point D={v[j][(kk+1)%v[j].size()].first,v[j][(kk+1)%v[j].size()].second};\n if(ccw(A,B,D)==0)return true;\n D={v[j][(kk+v[j].size()-1)%v[j].size()].first,v[j][(kk+v[j].size()-1)%v[j].size()].second};\n if(ccw(A,B,D)==0)return true;\n }\n else if(B.x==C.x and B.y==C.y){\n Point D={v[j][(kk+1)%v[j].size()].first,v[j][(kk+1)%v[j].size()].second};\n if(ccw(A,B,D)==0)return true;\n D={v[j][(kk+v[j].size()-1)%v[j].size()].first,v[j][(kk+v[j].size()-1)%v[j].size()].second};\n if(ccw(A,B,D)==0)return true;\n }\n else return true;\n }\n }\n }\n return false;\n };\n vector<vector<int>> g(m);\n for(int i=0;i<n;i++){\n for(int j=0;j<n;j++){\n if(s[i]==s[j])continue;\n if(check(i,j)){\n // cout << i << \" \" << j << endl;\n g[mp[s[i]]].push_back(mp[s[j]]);\n g[mp[s[j]]].push_back(mp[s[i]]);\n }\n }\n }\n vector<int> dp(1<<m,1e9);\n dp[0]=0;\n for(int i=1;i<(1<<m);i++){\n for(int j=i;;j=(j-1)&i){\n int k=(j^i);\n bool ok=true;\n for(int u=0;u<m;u++){\n if((1<<u)&k){\n for(int t:g[u]){\n if((1<<t)&k)ok=false;\n }\n }\n }\n if(ok){\n dp[i]=min(dp[i],dp[j]+1);\n }\n if(j==0)break;\n }\n }\n printf(\"%d\\n\",dp.back());\n }\n}", "accuracy": 1, "time_ms": 40, "memory_kb": 3292, "score_of_the_acc": -0.0688, "final_rank": 13 }, { "submission_id": "aoj_1254_5288171", "code_snippet": "#include <bits/stdc++.h>\n//#include <atcoder/all>\n//using namespace atcoder;\n#pragma GCC target (\"avx\")\n#pragma GCC optimization (\"O3\")\n#pragma GCC optimization (\"unroll-loops\")\n\nusing namespace std;\n\ntypedef vector<int> VI;\ntypedef vector<VI> VVI;\ntypedef vector<string> VS;\ntypedef pair<int, int> PII;\ntypedef pair<int, int> pii;\ntypedef pair<long long, long long> PLL;\ntypedef pair<int, PII> TIII;\n\ntypedef long long ll;\ntypedef long double ld;\ntypedef unsigned long long ull;\n\n\n#define FOR(i, s, n) for (int i = s; i < (int)n; ++i)\n#define REP(i, n) FOR(i, 0, n)\n#define rep(i, a, b) for (int i = a; i < (b); ++i)\n#define trav(a, x) for (auto &a : x)\n#define all(x) x.begin(), x.end()\n\n#define MOD 1000000007\n\ntemplate<class T1, class T2> inline bool chmax(T1 &a, T2 b) {if (a < b) {a = b; return true;} return false;}\ntemplate<class T1, class T2> inline bool chmin(T1 &a, T2 b) {if (a > b) {a = b; return true;} return false;}\nconst double EPS = 1e-6, PI = acos(-1);\nconst double pi = 3.141592653589793238462643383279;\n//ここから編集 \ntypedef string::const_iterator State;\nll GCD(ll a, ll b){\n return (b==0)?a:GCD(b, a%b);\n}\nll LCM(ll a, ll b){\n return a/GCD(a, b) * b;\n}\ntemplate< int mod >\nstruct ModInt {\n int x;\n\n ModInt() : x(0) {}\n\n ModInt(int64_t y) : x(y >= 0 ? y % mod : (mod - (-y) % mod) % mod) {}\n\n ModInt &operator+=(const ModInt &p) {\n if((x += p.x) >= mod) x -= mod;\n return *this;\n }\n\n ModInt &operator-=(const ModInt &p) {\n if((x += mod - p.x) >= mod) x -= mod;\n return *this;\n }\n\n ModInt &operator*=(const ModInt &p) {\n x = (int) (1LL * x * p.x % mod);\n return *this;\n }\n\n ModInt &operator/=(const ModInt &p) {\n *this *= p.inverse();\n return *this;\n }\n\n ModInt operator-() const { return ModInt(-x); }\n\n ModInt operator+(const ModInt &p) const { return ModInt(*this) += p; }\n\n ModInt operator-(const ModInt &p) const { return ModInt(*this) -= p; }\n\n ModInt operator*(const ModInt &p) const { return ModInt(*this) *= p; }\n\n ModInt operator/(const ModInt &p) const { return ModInt(*this) /= p; }\n\n bool operator==(const ModInt &p) const { return x == p.x; }\n\n bool operator!=(const ModInt &p) const { return x != p.x; }\n\n ModInt inverse() const {\n int a = x, b = mod, u = 1, v = 0, t;\n while(b > 0) {\n t = a / b;\n swap(a -= t * b, b);\n swap(u -= t * v, v);\n }\n return ModInt(u);\n }\n\n ModInt pow(int64_t n) const {\n ModInt ret(1), mul(x);\n while(n > 0) {\n if(n & 1) ret *= mul;\n mul *= mul;\n n >>= 1;\n }\n return ret;\n }\n\n friend ostream &operator<<(ostream &os, const ModInt &p) {\n return os << p.x;\n }\n\n friend istream &operator>>(istream &is, ModInt &a) {\n int64_t t;\n is >> t;\n a = ModInt< mod >(t);\n return (is);\n }\n\n static int get_mod() { return mod; }\n};\n\nusing modint = ModInt< 1000000007 >;\ntemplate< typename T >\nstruct Combination {\n vector< T > _fact, _rfact, _inv;\n\n Combination(int sz) : _fact(sz + 1), _rfact(sz + 1), _inv(sz + 1) {\n _fact[0] = _rfact[sz] = _inv[0] = 1;\n for(int i = 1; i <= sz; i++) _fact[i] = _fact[i - 1] * i;\n _rfact[sz] /= _fact[sz];\n for(int i = sz - 1; i >= 0; i--) _rfact[i] = _rfact[i + 1] * (i + 1);\n for(int i = 1; i <= sz; i++) _inv[i] = _rfact[i] * _fact[i - 1];\n }\n\n inline T fact(int k) const { return _fact[k]; }\n\n inline T rfact(int k) const { return _rfact[k]; }\n\n inline T inv(int k) const { return _inv[k]; }\n\n T P(int n, int r) const {\n if(r < 0 || n < r) return 0;\n return fact(n) * rfact(n - r);\n }\n\n T C(int p, int q) const {\n if(q < 0 || p < q) return 0;\n return fact(p) * rfact(q) * rfact(p - q);\n }\n\n T H(int n, int r) const {\n if(n < 0 || r < 0) return (0);\n return r == 0 ? 1 : C(n + r - 1, r);\n }\n};\n\nint modpow(ll x, ll n, int mod) {\n ll res = 1;\n while(n) {\n if(n&1) res = res*x % mod;\n x = x*x%mod;\n n >>= 1;\n }\n return res;\n}\nusing Point = complex< double >;\nbool eq(double a, double b){ return fabs(a-b) < EPS; }\nnamespace std {\n bool operator<(const Point a, const Point b) {\n if(eq(a.real(),b.real())) return a.imag() < b.imag();\n return a.real() < b.real();\n }\n}\n\nistream &operator>> (istream &is, Point &p) {\n double a, b;\n is >> a >> b;\n p = Point(a, b);\n return is;\n}\n\nostream &operator<< (ostream &os, Point &p) {\n return os << fixed << setprecision(10) << p.real() << \" \" << p.imag();\n}\n\nbool operator<(const Point &a, const Point &b) {\n return a.real() != b.real() ? a.real() < b.real() : a.imag() < b.imag();\n}\n\n// rotate Φ(rad)\n// x = r * cos(θ + Φ)\n// = r * cos(θ) * cos(Φ) - r * sin(θ) * sin(Φ)\n// = x * cos(Φ) - y * sin(Φ) (∵ cos(θ) = x/r, sin(θ) = y/r) \nPoint rotate(double phi, const Point &p) {\n double x = p.real(), y = p.imag();\n return Point(x * cos(phi) - y * sin(phi), x * sin(phi) + y * cos(phi));\n}\n\ndouble radian_to_degree(double r) {\n return (r * 180.0 / PI);\n}\n\ndouble degree_to_radian(double d) {\n return (d * PI / 180.0);\n}\n\nstruct Line{\n Point a, b;\n\n Line() = default;\n\n Line(Point a, Point b) : a(a), b(b){}\n\n Line(double A, double B, double C){\n //ax + by = c\n if(eq(A, 0)){\n a = Point(0, C/B), b = Point(1, C/B);\n }else if(eq(B, 0)){\n a = Point(C/A, 0), b = Point(C/A, 1);\n }else{\n a = Point(0, C/B), b = Point(C/A, 0);\n }\n }\n\n friend istream &operator>>(istream &is, Line &a) {\n return is >> a.a >> a.b;\n }\n friend ostream &operator<<(ostream &os, Line &a) {\n return os << a.a << \" to \" << a.b;\n }\n};\n\nstruct Segment: Line {\n Segment() = default;\n\n Segment(Point a, Point b) : Line(a, b) {}\n};\n\nstruct Circle {\n Point p;\n double r;\n\n Circle() = default;\n\n Circle(Point p, double r): p(p), r(r){}\n};\n\nusing Points = vector<Point>;\nusing Polygon = vector<Point>;\nusing Segments = vector<Segment>;\nusing Lines = vector<Line>;\nusing Circles = vector<Circle>;\n\ndouble cross(const Point &a, const Point &b) {\n return a.real() * b.imag() - a.imag() * b.real();\n}\n\ndouble dot(const Point& a, const Point &b) {\n return a.real() * b.real() + a.imag() * b.imag();\n}\n\n//https://mathtrain.jp/projection\nPoint projection(const Line &l, const Point &p){\n double t = dot(p - l.a, l.a-l.b) / norm(l.a - l.b);\n return l.a + (l.a - l.b) * t;\n}\n\nPoint projection(const Segment &l, const Point &p){\n double t = dot(p - l.a, l.b-l.a) / norm(l.a - l.b);\n return l.a + (l.b - l.a) * t;\n}\n\nPoint reflection(const Line &l, const Point &p){\n return p + (projection(l, p) - p) * 2.0;\n}\n\nint ccw(const Point &a, const Point &b, const Point &c) {\n if(cross(b-a, c-a) > EPS) return 1; // \"COUNTER_CLOCKWISE\"\n if(cross(b-a, c-a) < -EPS) return -1; // \"CLOCKWISE\"\n if(dot(b-a, c-a) < -EPS) return 2; // \"ONLINE_BACK\" c-a-b\n if(norm(b-a) < norm(c-a) - EPS) return -2; // \"ONLINE_FRONT\" a-b-c\n return 0; // \"ON_SEGMENT\" a-c-b\n}\n\nbool parallel(const Line &a, const Line &b){\n return eq(cross(a.a-a.b, b.a-b.b), 0.0);\n}\nbool orthogonal(const Line &a, const Line &b){\n return eq(dot(a.a-a.b, b.a-b.b), 0.0);\n}\nenum { OUT, ON, IN };\n\nint contains(const Polygon& Q, const Point& p){\n bool in = false;\n for(int i=0; i<Q.size(); i++){\n Point a = Q[i] - p, b = Q[(i+1)%Q.size()]-p;\n if(a.imag() > b.imag()) swap(a, b);\n if(a.imag() <= 0 && 0 < b.imag() && cross(a, b) < 0) in = !in;\n if(cross(a, b) == 0 && dot(a, b) <= 0) return ON;\n }\n return in ? IN : OUT;\n}\nbool intersect(const Segment &s, const Point &p){\n return ccw(s.a, s.b, p) == 0;\n}\n\n//直線の交差判定\nbool intersect(const Line &a, const Line &b) {\n if(parallel(a, b)) return 0;\n return 1;\n}\n\n//線分が重なる/端点で交わるときtrue\nbool intersect(const Segment &a, const Segment &b) {\n return ccw(a.a, a.b, b.a) * ccw(a.a, a.b, b.b) <= 0 && ccw(b.a, b.b, a.a) * ccw(b.a, b.b, a.b) <= 0;\n}\n\nbool intersect(const Line &l, const Segment &s) {\n \n}\n\nPoint crosspoint(const Line &a, const Line &b){\n double d = cross(b.b-b.a, a.b-a.a);\n if(eq(d, 0.0)) return Point(1e9, 1e9); \n \n return a.a + (a.b - a.a) * cross(b.b-b.a, b.b-a.a) / d;\n}\n\nPoint crosspoint(const Segment &a, const Segment &b){\n return crosspoint(Line(a.a, a.b), Line(b.a, b.b));\n}\ndouble distance(const Point &a, const Point &b){\n return abs(a - b);\n}\ndouble distance(const Line &l, const Point &p){\n return abs( cross(p - l.a, l.b-l.a) / abs(l.b-l.a) );\n}\ndouble distance(const Segment &s, const Point &p){\n Point r = projection(s, p);\n if(intersect(s, r)) return abs(r-p);\n return min(abs(s.a - p), abs(s.b - p));\n}\n// double distance(const Line &a, const Line &b){\n\n// }\n// double distance(const Line &a, const Segment &b){\n\n// }\ndouble distance(const Segment &a, const Segment &b) {\n return intersect(a, b) ? 0 : min({distance(a, b.a), distance(a, b.b), distance(b, a.a), distance(b, a.b)});\n}\n\n/* 2円の交点 */\nvector<Point> crosspointCC(Circle C1, Circle C2){\n vector<Point> ps;\n Point ab = C2.p - C1.p;\n double d = abs(ab);\n double rc = (C1.r * C1.r + d * d - C2.r * C2.r) / (2 * d);\n if(eq(d, 0) || C1.r < abs(rc)) return ps;\n\n double rs = sqrt(C1.r * C1.r - rc*rc);\n\n Point abN = ab * Point(0, rs/d);\n Point cp = C1.p + rc / d * ab;\n ps.push_back(cp + abN);\n if(!eq(norm(abN), 0))ps.push_back(cp-abN);\n return ps;\n}\n\nvector<Point> crosspointCL(Circle C, Line l){\n Point p = projection(l, C.p);\n\n Point e = (l.b-l.a)/abs(l.b-l.a);\n if(eq(distance(l, C.p), C.r)) {\n return vector<Point>{p, p};\n }\n double base = sqrt(C.r*C.r-norm(p-C.p));\n \n return vector<Point>{p+e*base, p-e*base};\n}\n\n/* 円同士の共通部分の面積を求める */\n// verify: http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_7_I date: 2020/10/11\nlong double AreaofCC(Circle& C1, Circle& C2){\n if(C1.r > C2.r) swap(C1, C2);\n long double nor = norm(C1.p - C2.p);\n long double dist = sqrtl(nor);\n \n if(C1.r + C2.r < dist+EPS) return 0;\n\n if(dist + C1.r < C2.r + EPS) return C1.r * C1.r * PI;\n\n long double theta1 = acosl((nor + C1.r*C1.r - C2.r * C2.r)/(2*C1.r*dist));\n\n long double theta2 = acosl((nor + C2.r*C2.r - C1.r * C1.r)/(2*C2.r*dist));\n \n return (theta1 - sinl(theta1+ theta1) *0.5) * C1.r * C1.r + (theta2 - sinl(theta2+theta2) *0.5) * C2.r * C2.r;\n}\n\n\nPolygon convex_hull(Polygon &p)\n{\n int n = (int)p.size(), k = 0;\n if (n <= 2)\n return p;\n sort(p.begin(), p.end());\n vector<Point> ch(n * 2);\n\n for (int i = 0; i < n; ch[k++] = p[i++])\n {\n while (k >= 2 && cross(ch[k - 1] - ch[k - 2], p[i] - ch[k - 1]) < 0)\n --k;\n }\n\n for (int i = n - 2, t = k + 1; i >= 0; ch[k++] = p[i--])\n {\n while (k >= t && cross(ch[k - 1] - ch[k - 2], p[i] - ch[k - 1]) < 0)\n --k;\n }\n ch.resize(k - 1);\n return ch;\n}\ndouble convex_diameter(const Polygon &p)\n{\n int n = (int)p.size();\n if (n == 2)\n return abs(p[0] - p[1]);\n\n int is = 0, js = 0;\n for (int i = 1; i < n; ++i)\n {\n if (imag(p[i]) > imag(p[is]))\n is = i;\n if (imag(p[i]) < imag(p[js]))\n js = i;\n }\n\n double res = abs(p[is] - p[js]);\n int i, maxi, j, maxj;\n i = maxi = is;\n j = maxj = js;\n do\n {\n if (cross(p[(i + 1) % n] - p[i], p[(j + 1) % n] - p[j]) >= 0)\n j = (j + 1) % n;\n else\n i = (i + 1) % n;\n res = max(res, abs(p[i] - p[j]));\n } while (i != is || j != js);\n return res;\n}\n\nPolygon convex_cut(const Polygon &p, const Line l)\n{\n Polygon ret;\n for (int i = 0; i < p.size(); ++i)\n {\n Point now = p[i], nxt = p[(i + 1) % p.size()];\n if (ccw(l.a, l.b, now) != -1) //交点が線分l上にあるとき\n ret.push_back(now);\n if (ccw(l.a, l.b, now) * ccw(l.a, l.b, nxt) < 0)\n {\n ret.push_back(crosspoint(Line(now, nxt), l));\n }\n }\n return (ret);\n}\ndouble closestpair(Points &a, int l, int r){\n if(r-l<=1) return 1e20;\n int mid = (l+r)/2;\n\n double X = a[mid].real();\n double d = min(closestpair(a, l, mid), closestpair(a, mid, r));\n inplace_merge(a.begin()+l, a.begin()+mid, a.begin()+r, [](const Point& pa, const Point& pb){\n return pa.imag() < pb.imag();\n });\n\n Points b;\n for(int i=l; i<r; i++){\n if(abs(a[i].real()-X) >= d) continue;\n for(int j=b.size()-1; j>=0; j--){\n if(abs((a[i]-b[j]).imag()) >= d) break;\n d = min(d, abs(a[i]-b[j]));\n }\n b.push_back(a[i]);\n }\n return d;\n}\n\n/* 円の交差判定 */\n/* verify: http://judge.u-aizu.ac.jp/onlinejudge/finder.jsp?course=CGL date:2020/10/11 */\nint intersectionCC(Circle c1, Circle c2){\n if(c1.r > c2.r) swap(c1, c2);\n double d1 = abs(c1.p-c2.p); \n double d2 = c1.r + c2.r;\n if(d1 > d2) return 4; /* 互いに交点を持たない */\n else if(d1 == d2) return 3; /* 外接する場合 */\n else{\n if(c2.r == c1.r + d1) return 1; /* 内接する場合 */\n else if(c2.r > c1.r + d1) return 0; /* 包含 */\n else return 2; /* 交点を2つ持つ */\n }\n}\n\n/* 点集合が反時計回りに与えられる場合のみ */\ndouble Area(Points &g){\n double res = 0;\n int n = g.size();\n REP(i,n){\n res += cross(g[i], g[(i+1)%n]);\n }\n return res/2.0;\n}\n\n/* 内接円 */\n/* verify: http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_7_B&lang=ja date: 2020/10/11 */\npair<Point, double> incircle_of_a_Triangle(Points &p){\n\n double a = abs(p[1]-p[2]), b = abs(p[2]-p[0]), c = abs(p[0]-p[1]);\n double s = (a+b+c) / 2.0;\n double S = sqrtl(s*(s-a)*(s-b)*(s-c));\n double r = S/s; \n \n Point pp = (a*p[0]+b*p[1]+c*p[2])/(a+b+c);\n return make_pair(pp, r);\n} \n\n/* 外接円 */\npair<Point, double> circumscribed_circle_of_a_triangle(Points &p){\n\n Point m1((p[0]+p[1])/2.0), m2((p[1]+p[2])/2.0);\n Point v((p[1]-p[0]).imag(), (p[0]-p[1]).real()), w((p[1]-p[2]).imag(), (p[2]-p[1]).real());\n\n Line l1(m1, Point(v+m1)), l2(m2, Point(w+m2));\n\n Point x = crosspoint(l1, l2);\n double r = abs(x-p[0]);\n return make_pair(x, r);\n}\n\n/* ある点pを通る円cの接線 */\nPoints tangent_to_a_circle(Point p, Circle C){\n Points ps;\n double d = abs(C.p-p);\n if(eq(d,0)){\n ps.push_back(p);\n }else if(d>EPS){\n \n double d2 = sqrt(d*d-C.r*C.r);\n \n long double theta = acosl(d2/d);\n //cout << theta << endl;\n Point pp = C.p - p;\n Point p2 = rotate(-theta, pp);\n\n Point e = p2/abs(p2);\n Point ppp = e*d2;\n ps.push_back(p + ppp);\n p2 = rotate(theta, pp);\n e = p2/abs(p2);\n ppp = e*d2;\n ps.push_back(p + ppp);\n \n }\n return ps;\n}\n\nLines tangent(Circle C1, Circle C2){\n Lines ls;\n if(C1.r < C2.r) swap(C1, C2);\n double d = norm(C1.p - C2.p);\n if(eq(d, 0)) return ls;\n Point u = (C2.p - C1.p) / sqrt(d);\n Point v = rotate(pi/2, u);\n\n for(double s: {-1, 1}) {\n double h = (C1.r + s * C2.r) / sqrt(d);\n if(eq(1-h*h, 0)) {\n ls.emplace_back(C1.p + u * C1.r, C1.p + (u+v) * C1.r);\n }else if(1-h*h>0){\n Point uu = u*h, vv = v*sqrt(1-h*h);\n ls.emplace_back(C1.p + (uu+vv) * C1.r, C2.p - (uu+vv) * C2.r * s);\n ls.emplace_back(C1.p + (uu-vv) * C1.r, C2.p - (uu-vv) * C2.r * s);\n }\n }\n return ls;\n}\nLine bisector(Point a, Point b){\n Point A = (a+b)*Point(0.5, 0); \n return Line(A, A+(b-a)*Point(0, PI/2));\n}\n\nPolygon voronoi_cell(Polygon Q, Points P, int s){\n REP(i,P.size()){\n if(i!=s){\n Q = convex_cut(Q, bisector(P[s], P[i]));\n }\n }\n return Q;\n}\n\nbool check(Segment &s,Segment &t) {\n\n if(s.a != t.a && s.a != t.b && ccw(t.a, t.b, s.a) == 0) return true;\n if(s.b != t.a && s.b != t.b && ccw(t.a, t.b, s.b) == 0) return true;\n if(t.a != s.a && t.a != s.b && ccw(s.a, s.b, t.a) == 0) return true;\n if(t.b != s.a && t.b != s.b && ccw(s.a, s.b, t.b) == 0) return true;\n if(s.a == t.a && s.b == t.b) return true;\n if(s.a == t.b && s.b == t.a) return true;\n return false;\n}\nunsigned long xor128() {\n static unsigned long x = 123456789, y = 362436069, z = 521288629, w = 88675123;\n unsigned long t = (x ^ (x << 11));\n x = y; y = z; z = w;\n return (w = (w ^ (w >> 19)) ^ (t ^ (t >> 8)));\n}\nint RandomPrime() {\n auto prime = [&](int n) {\n for (int i = 2; i * i <= n; i ++) if (n % i == 0) return false;\n return true;\n };\n int modulo = 1000000000;\n modulo += (int) (xor128() % 100000000);\n while (!prime(modulo)) modulo ++;\n return modulo;\n}\nint ChromaticNumber(const vector<vector<int>> &tmpg) {\n int n = tmpg.size();\n if (n == 0) return 0;\n vector<int> g(n);\n for (int i = 0; i < n; i ++) {\n for (int j = 0; j < n; j ++) {\n if (tmpg[i][j]) {\n g[i] |= (1 << j);\n }\n }\n }\n int all = 1 << n;\n vector<int> a(all), s(all);\n a[0] = 1;\n int modulo = RandomPrime();\n for (int i = 1; i < all; i ++) {\n int j = __builtin_ctz(i);\n a[i] = a[i - (1 << j)] + a[(i - (1 << j)) & ~g[j]];\n if (a[i] >= modulo) a[i] -= modulo;\n }\n for (int i = 0; i < all; i ++) {\n s[i] = ((n - __builtin_popcount(i)) & 1 ? -1 : 1);\n }\n for (int k = 1; k < n; k ++) {\n long long sum = 0;\n for (int i = 0; i < all; i ++) {\n long long cur = ((s[i] * (long long) a[i]) % modulo);\n s[i] = (int) cur;\n sum += cur;\n }\n if (sum % modulo != 0) return k;\n }\n return n;\n}\nint main() {\n cin.tie(0);\n ios::sync_with_stdio(false);\n cout << fixed << setprecision(7);\n \n int n;\n while(cin >> n, n) {\n map<string, vector<Polygon>> mp;\n \n REP(i,n) {\n Polygon ps;\n string name;\n cin >> name;\n int x;\n while(cin >> x) {\n if(x == -1) break;\n int y; cin >> y;\n Point p(x, y);\n ps.push_back(p);\n }\n mp[name].push_back(ps);\n }\n int n = mp.size();\n vector<vector<int>> g(n, vector<int>(n));\n \n int pt1=0, pt2=0;\n for(auto obj1 : mp) {\n pt2 = 0;\n for(auto obj2: mp) {\n if(pt1 == pt2) {\n pt2++;\n continue;\n }\n bool is_adj = false;\n\n for(int i=0; i<obj1.second.size(); i++){\n\n for(int j=0; j<obj2.second.size(); j++) {\n\n Polygon p1 = obj1.second[i];\n Polygon p2 = obj2.second[j];\n\n for(int k=0; k<p1.size(); k++) {\n for(int l=0; l<p2.size(); l++) {\n Segment s1(p1[k], p1[(k+1)%p1.size()]);\n Segment s2(p2[l], p2[(l+1)%p2.size()]);\n \n is_adj |= check(s1, s2);\n }\n }\n }\n }\n if(is_adj) {\n g[pt1][pt2] = true;\n }\n pt2++;\n }\n pt1++;\n }\n \n cout << ChromaticNumber(g) << endl;\n }\n\n return 0;\n}", "accuracy": 1, "time_ms": 40, "memory_kb": 3232, "score_of_the_acc": -0.0654, "final_rank": 12 }, { "submission_id": "aoj_1254_5006932", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\nusing ll = long long;\nusing PII = pair<ll, ll>;\n\n#define FOR(i, a, n) for (ll i = (ll)a; i < (ll)n; ++i)\n#define REP(i, n) FOR(i, 0, n)\n#define ALL(x) x.begin(), x.end()\ntemplate <typename T> void chmin(T &a, const T &b) { a = min(a, b); }\ntemplate <typename T> void chmax(T &a, const T &b) { a = max(a, b); }\n\nconst ll INF = 1LL << 60;\n\nstruct FastIO {\n FastIO() {\n cin.tie(0);\n ios::sync_with_stdio(0);\n }\n} fastiofastio;\n\nnamespace Geometry {\nusing R = long double; // Rにmint渡せるようにする\nusing P = complex<R>;\nusing L = pair<P, P>;\nusing G = vector<P>;\nstruct C {\n P c;\n R r;\n C() {}\n C(const P &a, const R &b) : c(a), r(b) {}\n};\nstruct S : public L {\n S() {}\n S(const P &a, const P &b) : L(a, b) {}\n};\n\nconst R EPS = 1e-8;\nconst R PI = atan(1) * 4;\n\ninline int sgn(const R &r) { return (r > EPS) - (r < -EPS); }\ninline R dot(const P &a, const P &b) {\n return real(a) * real(b) + imag(a) * imag(b);\n}\ninline R det(const P &a, const P &b) {\n return real(a) * imag(b) - imag(a) * real(b);\n}\ninline P rotate(const P &p, const R &arg) {\n return P(cos(arg) * p.real() - sin(arg) * p.imag(),\n sin(arg) * p.real() + cos(arg) * p.imag());\n}\ninline P vec(const L &l) { return l.second - l.first; }\n\n// P,L,Sについて入力\ninline istream &operator>>(istream &is, P &p) {\n R x, y;\n is >> x >> y;\n p = P(x, y);\n return is;\n}\ninline istream &operator>>(istream &is, L &l) {\n P a, b;\n is >> a >> b;\n l = L(a, b);\n return is;\n}\ninline istream &operator>>(istream &is, S &s) {\n P a, b;\n is >> a >> b;\n s = S(a, b);\n return is;\n}\n\n// 線分abから見たcの位置\nenum CCW { LEFT = 1, RIGHT = 2, BACK = 4, FRONT = 8, ON_SEG = 16 };\nint ccw(P a, P b, P c) {\n P p = (c - a) / (b - a);\n if (sgn(imag(p)) > 0)\n return LEFT;\n if (sgn(imag(p)) < 0)\n return RIGHT;\n if (sgn(real(p)) < 0)\n return BACK;\n if (sgn(real(p) - 1) > 0)\n return FRONT;\n return ON_SEG;\n}\n\n// 射影\nP inline projection(const L &l, const P &p) {\n R t = dot(p - l.first, l.first - l.second) / norm(l.first - l.second);\n return l.first + t * (l.first - l.second);\n}\n// 反射\nP inline reflection(const L &l, const P &p) {\n return p + (R)2 * (projection(l, p) - p);\n}\n\n// 垂直,平行\ninline bool vertical(L a, L b) { return sgn(dot(vec(a), vec(b))) == 0; }\ninline bool parallel(L a, L b) { return sgn(det(vec(a), vec(b))) == 0; }\n\n// 交差判定\ntemplate <bool strict = false> inline bool intersect(const L &l1, const L &l2) {\n if (strict)\n return sgn(det(vec(l1), vec(l2))) != 0;\n return sgn(det(vec(l1), vec(l2))) != 0 || l1 == l2;\n}\ntemplate <bool strict = false> inline bool intersect(const L &l, const S &s) {\n if (strict)\n det(s.first, vec(l)) * det(s.second, vec(l)) < 0;\n return det(s.first, vec(l)) * det(s.second, vec(l)) <= 0;\n}\ntemplate <bool strict = false> inline bool intersect(const S &s1, const S &s2) {\n int ccw1 = ccw(s1.first, s1.second, s2.first) |\n ccw(s1.first, s1.second, s2.second);\n int ccw2 = ccw(s2.first, s2.second, s1.first) |\n ccw(s2.first, s2.second, s1.second);\n if (strict)\n return (ccw1 & ccw2) == (LEFT | RIGHT);\n return (ccw1 & ccw2) == (LEFT | RIGHT) || ((ccw1 | ccw2) & ON_SEG);\n}\ntemplate <bool strict = false> inline bool intersect(const S &s, const P &p) {\n return ccw(s.first, s.second, p) == ON_SEG;\n}\ntemplate <bool strict = false> inline bool intersect(const L &l, const P &p) {\n return ccw(l.first, l.second, p) == ON_SEG ||\n ccw(l.first, l.second, p) == FRONT ||\n ccw(l.first, l.second, p) == BACK;\n}\n\n// 距離\nR dist(const S &s, const P &p) {\n P q = projection(s, p);\n if (sgn(dot(s.second - s.first, p - s.first)) <= 0)\n q = s.first;\n if (sgn(dot(s.first - s.second, p - s.second)) <= 0)\n q = s.second;\n return abs(p - q);\n}\nR dist(const S &a, const S &b) {\n if (intersect(a, b))\n return 0;\n return min({dist(a, b.first), dist(a, b.second), dist(b, a.first),\n dist(b, a.second)});\n}\nR dist(const L &l, const P &p) {\n P q = projection(l, p);\n return abs(p - q);\n}\n\n// 交点 交差判定を先にすること!!!\ninline P crosspoint(const L &l1, const L &l2) {\n R ratio = det(vec(l2), l2.first - l1.first) / det(vec(l2), vec(l1));\n return l1.first + vec(l1) * ratio;\n}\n\n// 凸包 3点が一直線上に並ぶときに注意\n// 凸包のうち一番左にある頂点の中で一番下の頂点から時計回り\nG convex_hull(G ps) {\n int n = ps.size(), k = 0;\n sort(ps.begin(), ps.end());\n G r(2 * n);\n for (int i = 0; i < n; i++) {\n while (k > 1 && sgn(det(r[k - 1] - r[k - 2], ps[i] - r[k - 2])) < 0)\n k--;\n r[k++] = ps[i];\n }\n for (int i = n - 2, t = k; i >= 0; i--) {\n while (k > t && sgn(det(r[k - 1] - r[k - 2], ps[i] - r[k - 2])) < 0)\n k--;\n r[k++] = ps[i];\n }\n r.resize(k - 1);\n return r;\n}\n\n// 凸多角形polを直線lで切断して左側の多角形を返す\nG convex_cut(const G &pol, const L &l) {\n G res;\n REP(i, pol.size()) {\n P a = pol[i], b = pol[(i + 1) % pol.size()];\n int da = sgn(det(l.first - a, l.second - a)),\n db = sgn(det(l.first - b, l.second - b));\n // 点aが直線lの左側\n if (da >= 0)\n res.emplace_back(a);\n // 辺(a,b)と直線lが交わる\n if (da * db < 0)\n res.emplace_back(crosspoint(L{a, b}, l));\n }\n return res;\n}\n\n// 1直線上に3点が並んでるような部分を消去 O(p.size())\nG normalize_poligon(G p) {\n int n = p.size();\n REP(i, p.size()) {\n if (ccw(p[(i + n - 1) % n], p[i], p[(i + 1) % n]) == ON_SEG) {\n p.erase(p.begin() + i);\n i--;\n }\n }\n return p;\n}\n\n// 点a,bの垂直二等分線 O(1)\nL bisector(P a, P b) {\n const P mid = (a + b) / P(2, 0);\n return L{mid, mid + (b - a) * P(0, 1)};\n}\n// 多角形polと点集合vについてボロノイ図を計算\n// 点v[s]が属する部分を返す O(pol.size * v.size())\nG voronoi_cell(G pol, G v, int s) {\n pol = normalize_poligon(pol);\n REP(i, v.size()) if (i != s) {\n pol = convex_cut(pol, bisector(v[s], v[i]));\n }\n return pol;\n}\n\n// 面積 頂点が反時計回りに並んでいること\nR area(const G &pol) {\n R ret = 0.0;\n REP(i, pol.size()) ret += det(pol[i], pol[(i + 1) % pol.size()]);\n return (ret / 2.0);\n}\n\n// 凸性の判定\nbool isConvex(const G &pol) {\n REP(i, pol.size()) {\n if (sgn(det(pol[(i + 1) % pol.size()] - pol[i],\n pol[(i + 2) % pol.size()] - pol[i])) < 0) {\n return false;\n }\n }\n return true;\n}\n\n// 多角形と点の内包\n// 2→in 1→on 0→out\nint inPolygon(const G &pol, const P &p) {\n bool in = false;\n for (int i = 0; i < pol.size(); ++i) {\n P a = pol[i] - p, b = pol[(i + 1) % pol.size()] - p;\n if (imag(a) > imag(b))\n swap(a, b);\n if (imag(a) <= 0 && 0 < imag(b) && sgn(det(a, b)) < 0) {\n in = !in;\n }\n if (sgn(det(a, b)) == 0 && sgn(dot(a, b)) <= 0)\n return 1;\n }\n return in ? 2 : 0;\n}\n} // namespace Geometry\n\nnamespace std {\nbool operator<(const Geometry::P &a, const Geometry::P &b) {\n return Geometry::sgn(real(a - b)) ? real(a - b) < 0\n : Geometry::sgn(imag(a - b)) < 0;\n}\nbool operator==(const Geometry::P &a, const Geometry::P &b) {\n return Geometry::sgn(real(a - b)) == 0 && Geometry::sgn(imag(a - b)) == 0;\n}\nbool cmp_y(const Geometry::P &a, const Geometry::P &b) {\n return Geometry::sgn(imag(a - b)) ? imag(a - b) < 0\n : Geometry::sgn(real(a - b)) < 0;\n}\n} // namespace std\n\nvector<Geometry::G> vec[10];\nvector<int> G[10];\nint color[10];\nint ans;\n\nvoid dfs(int v, int n, int max_color) {\n if (v == n) {\n ans = min(ans, max_color);\n return;\n }\n if (ans <= max_color)\n return;\n bool used[10] = {};\n for (int to : G[v]) {\n if (to < v)\n used[color[to]] = 1;\n }\n for (int i = 0; i < max_color; i++) {\n if (!used[i]) {\n color[v] = i;\n dfs(v + 1, n, max_color);\n }\n }\n color[v] = max_color;\n dfs(v + 1, n, max_color + 1);\n}\n\nint main() {\n while (1) {\n int n;\n cin >> n;\n if (n == 0)\n break;\n for (int i = 0; i < 10; i++) {\n vec[i].clear();\n G[i].clear();\n }\n map<string, int> idx;\n int cnt = 0;\n for (int i = 0; i < n; i++) {\n string name;\n cin >> name;\n int c;\n if (idx.find(name) == idx.end()) {\n c = cnt++;\n idx[name] = c;\n } else {\n c = idx[name];\n }\n Geometry::G poly;\n while (1) {\n int x, y;\n cin >> x;\n if (x == -1)\n break;\n cin >> y;\n poly.emplace_back(x, y);\n }\n vec[c].push_back(poly);\n }\n for (int i = 0; i < cnt; i++) {\n for (int j = i + 1; j < cnt; j++) {\n bool touch = 0;\n for (auto &A : vec[i]) {\n for (auto &B : vec[j]) {\n for (int k = 0; k < A.size(); k++) {\n auto p1 = A[k];\n auto p2 = A[(k + 1) % A.size()];\n for (int l = 0; l < B.size(); l++) {\n auto p3 = B[l];\n auto p4 = B[(l + 1) % B.size()];\n if (!Geometry::parallel(Geometry::S(p1, p2),\n Geometry::S(p3, p4)))\n continue;\n if (!Geometry::intersect(Geometry::S(p1, p2),\n Geometry::S(p3, p4))) {\n continue;\n }\n if ((p3 == p1 || Geometry::ccw(p1, p2, p3) ==\n Geometry::BACK) &&\n (p4 == p1 || Geometry::ccw(p1, p2, p4) ==\n Geometry::BACK))\n continue;\n if ((p3 == p2 || Geometry::ccw(p1, p2, p3) ==\n Geometry::FRONT) &&\n (p4 == p2 || Geometry::ccw(p1, p2, p4) ==\n Geometry::FRONT))\n continue;\n touch = 1;\n break;\n }\n if (touch)\n break;\n }\n if (touch)\n break;\n }\n if (touch)\n break;\n }\n if (touch) {\n G[i].push_back(j);\n G[j].push_back(i);\n }\n }\n }\n ans = 1 << 30;\n dfs(0, cnt, 0);\n cout << ans << endl;\n }\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3376, "score_of_the_acc": -0.0082, "final_rank": 1 }, { "submission_id": "aoj_1254_4983515", "code_snippet": "#include <bits/stdc++.h>\n\nusing namespace std;\n\ntypedef long long ll;\ntypedef pair<int, int> P;\n\n#define eps 1e-9\n#define INF 2000000000 // 2e9\n#define LLINF 2000000000000000000ll // 2e18 (llmax:9e18)\n#define all(x) (x).begin(), (x).end()\n#define sq(x) ((x) * (x))\n\n#ifndef LOCAL\n#define dmp(...) ;\n#else\n#define dmp(...) \\\n cerr << \"[ \" << #__VA_ARGS__ << \" ] : \" << dump_str(__VA_ARGS__) << endl\n#endif\n\n// ---------------- Utility ------------------\n\ntemplate <class T>\nbool chmin(T &a, const T &b) {\n if (a <= b) return false;\n a = b;\n return true;\n}\ntemplate <class T>\nbool chmax(T &a, const T &b) {\n if (a >= b) return false;\n a = b;\n return true;\n}\n\ntemplate <class T>\nusing MaxHeap = priority_queue<T>;\ntemplate <class T>\nusing MinHeap = priority_queue<T, vector<T>, greater<T>>;\n\ntemplate <class T>\nvector<T> vect(int len, T elem) {\n return vector<T>(len, elem);\n}\n\n// ----------------- Input -------------------\n\ntemplate <class T, class U>\nistream &operator>>(istream &is, pair<T, U> &p) {\n return is >> p.first >> p.second;\n}\n\ntemplate <class T>\nistream &operator>>(istream &is, vector<T> &vec) {\n for (int i = 0; i < vec.size(); i++) is >> vec[i];\n return is;\n}\n\n// ----------------- Output ------------------\n\ntemplate <class T, class U>\nostream &operator<<(ostream &os, const pair<T, U> &p) {\n return os << p.first << ',' << p.second;\n}\n\ntemplate <class T>\nostream &operator<<(ostream &os, const vector<T> &v) {\n for (const T &e : v) os << e << \" \";\n return os;\n}\n\ntemplate <class T>\nostream &operator<<(ostream &os, const deque<T> &d) {\n for (const T &e : d) os << e << \" \";\n return os;\n}\n\ntemplate <class T>\nostream &operator<<(ostream &os, const set<T> &s) {\n os << \"{ \";\n for (const T &e : s) os << e << \" \";\n return os << \"}\";\n}\n\ntemplate <class T, class U>\nostream &operator<<(ostream &os, const map<T, U> &m) {\n os << \"{ \";\n for (const auto &[key, val] : m) os << \"( \" << key << \" -> \" << val << \" ) \";\n return os << \"}\";\n}\n\nvoid dump_str_rec(ostringstream &) {}\n\ntemplate <class Head, class... Tail>\nvoid dump_str_rec(ostringstream &oss, Head &&head, Tail &&... tail) {\n oss << \", \" << head;\n dump_str_rec(oss, forward<Tail>(tail)...);\n}\n\ntemplate <class T, class... U>\nstring dump_str(T &&arg, U &&... args) {\n ostringstream oss;\n oss << arg;\n dump_str_rec(oss, forward<U>(args)...);\n return oss.str();\n}\n\n// --------------- Fast I/O ------------------\n\nvoid fastio() {\n cin.tie(0);\n ios::sync_with_stdio(0);\n cout << fixed << setprecision(20);\n}\n\n// ------------ End of template --------------\n\n#define endl \"\\n\"\n\n// Geometry Library\n// written by okuraofvegetable\n\n#define DOUBLE_INF 1e50\n#define Points vector<Point>\n#define EQ(a, b) (abs((a) - (b)) < eps)\n\ninline double add(double a, double b) {\n if (abs(a + b) < eps * (abs(a) + abs(b))) return 0;\n return a + b;\n}\n\n// --------------------- Point -----------------------\n\n#define Vector Point\nstruct Point {\n double x, y;\n Point() {}\n Point(double x, double y) : x(x), y(y) {}\n Point operator+(Point p) { return Point(add(x, p.x), add(y, p.y)); }\n Point operator-(Point p) { return Point(add(x, -p.x), add(y, -p.y)); }\n Point operator*(double d) { return Point(x * d, y * d); }\n Point operator/(double d) { return Point(x / d, y / d); }\n double dot(Point p) { return add(x * p.x, y * p.y); }\n double det(Point p) { return add(x * p.y, -y * p.x); }\n double norm() { return sqrt(x * x + y * y); }\n double norm2() { return x * x + y * y; }\n double dist(Point p) { return ((*this) - p).norm(); }\n double dist2(Point p) { return sq(x - p.x) + sq(y - p.y); }\n double arg() { return atan2(y, x); } // [-PI,PI]\n double arg(Vector v) { return v.arg() - arg(); } // henkaku\n double angle(Vector v) { // [0,PI]\n return acos(cos(arg(v)));\n }\n Vector normalize() { return (*this) * (1.0 / norm()); }\n Point vert() { return Point(y, -x); }\n\n // Signed area of triange (0,0) (x,y) (p.x,p.y)\n double area(Point p) { return (x * p.y - p.x * y) / 2.0; }\n friend istream &operator>>(istream &is, Point &p) { return is >> p.x >> p.y; }\n friend ostream &operator<<(ostream &os, const Point &p) {\n return os << '(' << p.x << ',' << p.y << ')';\n }\n};\n\nPoint divide_rate(Point a, Point b, double A, double B) {\n assert(!EQ(A + B, 0.0));\n return (a * B + b * A) * (1.0 / (A + B));\n}\n\nVector polar(double len, double arg) {\n return Vector(cos(arg) * len, sin(arg) * len);\n}\n\n// Direction a -> b -> c\n// verified AOJ CGL_1_C\nenum { COUNTER_CLOCKWISE, CLOCKWISE, ONLINE_BACK, ONLINE_FRONT, ON_SEGMENT };\nint ccw(Point a, Point b, Point c) {\n Vector p = b - a;\n Vector q = c - a;\n if (p.det(q) > 0.0) return COUNTER_CLOCKWISE; // counter clockwise\n if (p.det(q) < 0.0) return CLOCKWISE; // clockwise\n if (p.dot(q) < 0.0) return ONLINE_BACK; // c--a--b online_back\n if (p.norm() < q.norm()) return ONLINE_FRONT; // a--b--c online_front\n return ON_SEGMENT; // a--c--b on_segment\n}\n\n// --------------------- Line ------------------------\nstruct Line {\n Point a, b;\n Line() {}\n Line(Point a, Point b) : a(a), b(b) {}\n bool on(Point q) { return (a - q).det(b - q) == 0; }\n // folloing 2 functions verified AOJ CGL_2_A\n bool is_parallel(Line l) { return (a - b).det(l.a - l.b) == 0; }\n bool is_orthogonal(Line l) { return (a - b).dot(l.a - l.b) == 0; }\n Point intersection(Line l) {\n assert(!is_parallel(l));\n return a + (b - a) * ((l.b - l.a).det(l.a - a) / (l.b - l.a).det(b - a));\n }\n // Projection of p to this line\n // verified AOJ CGL_1_A\n Point projection(Point p) {\n return (b - a) * ((b - a).dot(p - a) / (b - a).norm2()) + a;\n }\n double distance(Point p) {\n Point q = projection(p);\n return p.dist(q);\n }\n // Reflection point of p onto this line\n // verified AOJ CGL_1_B\n Point refl(Point p) {\n Point proj = projection(p);\n return p + ((proj - p) * 2.0);\n }\n bool left(Point p) {\n if ((p - a).det(b - a) > 0.0)\n return true;\n else\n return false;\n }\n friend istream &operator>>(istream &is, Line &l) { return is >> l.a >> l.b; }\n friend ostream &operator<<(ostream &os, const Line &l) {\n return os << l.a << \" -> \" << l.b;\n }\n};\n\n// ------------------- Segment ----------------------\nstruct Segment {\n Point a, b;\n Segment() {}\n Segment(Point a, Point b) : a(a), b(b) {}\n Line line() { return Line(a, b); }\n bool on(Point q) {\n return ((a - q).det(b - q) == 0 && (a - q).dot(b - q) <= 0);\n }\n bool on_strict(Point q) {\n return ((a - q).det(b - q) == 0 && (a - q).dot(b - q) < 0);\n }\n\n // verified AOJ CGL_2_B\n bool is_intersect(Segment s) {\n if (line().is_parallel(s.line())) {\n if (on(s.a) || on(s.b)) return true;\n if (s.on(a) || s.on(b)) return true;\n return false;\n }\n Point p = line().intersection(s.line());\n if (on(p) && s.on(p))\n return true;\n else\n return false;\n }\n bool is_intersect(Line l) {\n if (line().is_parallel(l)) {\n if (l.on(a) || l.on(b))\n return true;\n else\n return false;\n }\n Point p = line().intersection(l);\n if (on(p))\n return true;\n else\n return false;\n }\n // following 2 distance functions are verified at AOJ CGL_2_D\n double distance(Point p) {\n double res = DOUBLE_INF;\n Point q = line().projection(p);\n if (on(q)) res = min(res, p.dist(q));\n res = min(res, min(p.dist(a), p.dist(b)));\n return res;\n }\n double distance(Segment s) {\n if (is_intersect(s)) return 0.0;\n double res = DOUBLE_INF;\n res = min(res, s.distance(a));\n res = min(res, s.distance(b));\n res = min(res, this->distance(s.a));\n res = min(res, this->distance(s.b));\n return res;\n }\n friend istream &operator>>(istream &is, Segment &s) {\n return is >> s.a >> s.b;\n }\n friend ostream &operator<<(ostream &os, const Segment &s) {\n return os << s.a << \" -> \" << s.b;\n }\n};\n\ntypedef vector<Point> Polygon;\n\nbool solve() {\n int n;\n cin >> n;\n if (n == 0) return false;\n\n map<string, int> m;\n vector<Segment> ss;\n vector<int> ids;\n\n auto get_id = [&](string &s) {\n auto it = m.find(s);\n if (it != m.end()) return it->second;\n int id = m.size();\n return m[s] = id;\n };\n\n for (int i = 0; i < n; i++) {\n string s;\n cin >> s;\n int id = get_id(s);\n Polygon pol;\n while (1) {\n int x, y;\n cin >> x;\n if (x == -1) break;\n cin >> y;\n pol.push_back(Point(x, y));\n }\n int M = pol.size();\n for (int j = 0; j < M; j++) {\n ss.push_back(Segment(pol[j], pol[(j + 1) % M]));\n ids.push_back(id);\n }\n }\n\n auto adj = [](Segment s, Segment t) {\n if (!s.line().is_parallel(t.line())) return false;\n if (EQ(s.a.dist(t.a), 0) && EQ(s.b.dist(t.b), 0)) return true;\n if (EQ(s.a.dist(t.b), 0) && EQ(s.b.dist(t.a), 0)) return true;\n if (s.on_strict(t.a) || s.on_strict(t.b)) return true;\n if (t.on_strict(s.a) || t.on_strict(s.b)) return true;\n return false;\n };\n\n vector<vector<int>> g(m.size());\n\n auto add_edge = [&](int u, int v) {\n g[u].push_back(v);\n g[v].push_back(u);\n };\n\n for (int i = 0; i < ss.size(); i++) {\n for (int j = i + 1; j < ss.size(); j++) {\n if (ids[i] != ids[j] && adj(ss[i], ss[j])) add_edge(ids[i], ids[j]);\n }\n }\n\n dmp(m);\n\n for (int i = 0; i < g.size(); i++) {\n sort(all(g[i]));\n g[i].erase(unique(all(g[i])), g[i].end());\n dmp(g[i]);\n }\n\n int ans = INF;\n vector<int> col(g.size(), -1);\n\n function<void(int)> dfs = [&](int v) {\n if (v == g.size()) {\n int S = 0;\n for (int i = 0; i < g.size(); i++) {\n S |= (1 << col[i]);\n for (int u : g[i]) {\n if (col[i] == col[u]) return;\n }\n }\n chmin(ans, __builtin_popcount(S));\n return;\n } else {\n for (int i = 0; i <= v; i++) {\n col[v] = i;\n dfs(v + 1);\n col[v] = -1;\n }\n }\n };\n\n dfs(0);\n cout << ans << endl;\n\n return true;\n}\n\nint main() {\n fastio();\n while (solve()) {}\n // int t; cin >> t; while(t--)solve();\n\n // int t; cin >> t;\n // for(int i=1;i<=t;i++){\n // cout << \"Case #\" << i << \": \";\n // solve();\n // }\n return 0;\n}", "accuracy": 1, "time_ms": 170, "memory_kb": 3436, "score_of_the_acc": -0.3594, "final_rank": 17 }, { "submission_id": "aoj_1254_4972897", "code_snippet": "#line 2 \"/Users/tiramister/CompetitiveProgramming/Cpp/CppLibrary/Geometry/geometry.hpp\"\n\n#include <iostream>\n#include <algorithm>\n#include <vector>\n#include <cmath>\n\nnamespace geo {\n\nusing Real = long double;\nconstexpr Real EPS = 1e-10;\nconstexpr Real PI = 3.14159265358979323846L;\n\nint cmp(Real a, Real b) {\n return std::abs(a - b) < EPS ? 0\n : a > b ? 1\n : -1;\n}\n\n/* -------------------- Point -------------------- */\nstruct Point {\n Real x, y;\n\n explicit Point(Real x = 0, Real y = 0) : x(x), y(y) {}\n\n Point operator-() const { return Point(-x, -y); }\n\n Point operator+(const Point& p) const { return Point(*this) += p; }\n Point operator-(const Point& p) const { return Point(*this) -= p; }\n Point& operator+=(const Point& p) {\n x += p.x, y += p.y;\n return *this;\n }\n Point& operator-=(const Point& p) {\n x -= p.x, y -= p.y;\n return *this;\n }\n\n Point operator*(Real k) const { return Point(*this) *= k; }\n Point operator/(Real k) const { return Point(*this) /= k; }\n Point& operator*=(Real k) {\n x *= k, y *= k;\n return *this;\n }\n Point& operator/=(Real k) {\n x /= k, y /= k;\n return *this;\n }\n\n bool operator==(const Point& p) const { return cmp(x, p.x) == 0 && cmp(y, p.y) == 0; }\n bool operator!=(const Point& p) const { return !(*this == p); }\n bool operator<(const Point& p) const { return cmp(x, p.x) < 0 ||\n (cmp(x, p.x) == 0 && cmp(y, p.y) < 0); }\n bool operator>(const Point& p) const { return cmp(x, p.x) > 0 ||\n (cmp(x, p.x) == 0 && cmp(y, p.y) > 0); }\n\n friend std::istream& operator>>(std::istream& is, Point& p) {\n return is >> p.x >> p.y;\n }\n\n Real abs() const { return std::hypot(x, y); }\n Real arg() const { return std::atan2(y, x); }\n\n Point rotate(Real theta) const {\n return Point(x * std::cos(theta) - y * std::sin(theta),\n x * std::sin(theta) + y * std::cos(theta));\n }\n Point normal() const { return Point(-y, x); }\n Point unit() const { return *this / abs(); }\n};\n\nPoint polar(Real r, Real theta) { return Point(std::cos(theta), std::sin(theta)) * r; }\n\nReal dist(const Point& p, const Point& q) { return (p - q).abs(); }\nReal dot(const Point& p, const Point& q) { return p.x * q.x + p.y * q.y; }\nReal cross(const Point& p, const Point& q) { return p.x * q.y - p.y * q.x; }\n\n/* -------------------- Segment -------------------- */\nstruct Segment {\n Point p, q;\n\n explicit Segment(const Point& p = Point(), const Point& q = Point()) : p(p), q(q) {}\n\n friend std::istream& operator>>(std::istream& is, Segment& s) {\n return is >> s.p >> s.q;\n }\n\n Real length() const { return (p - q).abs(); }\n Point diff() const { return q - p; }\n};\n\nbool orthogonal(const Segment& s, const Segment& t) {\n return cmp(dot(s.diff(), t.diff()), 0) == 0;\n}\n\nbool parallel(const Segment& s, const Segment& t) {\n return cmp(cross(s.diff(), t.diff()), 0) == 0;\n}\n\nPoint proj(const Segment& s, const Point& p) {\n auto v = s.diff().unit();\n return s.p + v * dot(v, p - s.p);\n}\n\nPoint refl(const Segment& s, const Point& p) {\n auto t = proj(s, p);\n return t + (t - p);\n}\n\n// position of a point relative to a segment\nenum Position {\n ON_SEGMENT = 0,\n COUNTER_CLOCKWISE = 1,\n CLOCKWISE = -1,\n ONLINE_FRONT = 2,\n ONLINE_BACK = -2\n};\n\nPosition pos(const Segment& s, const Point& p) {\n auto t = Segment(s.p, p);\n\n auto c = cross(s.diff(), t.diff());\n if (cmp(c, 0) != 0) {\n return cmp(c, 0) > 0 ? COUNTER_CLOCKWISE : CLOCKWISE;\n }\n\n auto d = dot(s.diff(), t.diff());\n if (cmp(d, 0) < 0) return ONLINE_BACK;\n\n return cmp(t.length(), s.length()) > 0 ? ONLINE_FRONT : ON_SEGMENT;\n}\n\n// end: contain ends of segments\nbool intersect(const Segment& s, const Segment& t, bool end) {\n return pos(s, t.p) * pos(s, t.q) < end &&\n pos(t, s.p) * pos(t, s.q) < end;\n}\n\nPoint crosspoint(const Segment& s, const Segment& t) {\n auto c1 = cross(t.diff(), s.diff());\n auto c2 = cross(t.diff(), s.p - t.p);\n return s.p + s.diff() * (-c2 / c1);\n}\n\nReal dist(const Segment& s, const Point& p) {\n auto q = proj(s, p);\n if (pos(s, q) == ON_SEGMENT) return dist(p, q);\n return std::min(dist(p, s.p), dist(p, s.q));\n}\n\nReal dist(const Segment& s, const Segment& t) {\n if (intersect(s, t, true)) return 0;\n return std::min({dist(s, t.p), dist(s, t.q),\n dist(t, s.p), dist(t, s.q)});\n}\n\n/* -------------------- Polygon -------------------- */\nstruct Polygon : public std::vector<Point> {\n using std::vector<Point>::vector;\n\n explicit Polygon(int n = 0) : std::vector<Point>(n) {}\n\n std::vector<Segment> segments() const {\n std::vector<Segment> segs;\n for (int i = 0; i < (int)size(); ++i) {\n segs.emplace_back((*this)[i], (*this)[(i + 1) % size()]);\n }\n return segs;\n }\n\n Real area() const {\n Real sum = 0;\n for (int i = 0; i < (int)size(); ++i) {\n sum += cross((*this)[i], (*this)[(i + 1) % size()]);\n }\n return std::abs(sum) / 2;\n }\n\n bool isconvex() const {\n for (int i = 0; i < (int)size(); ++i) {\n if (pos(Segment((*this)[i], (*this)[(i + 1) % size()]),\n (*this)[(i + 2) % size()]) == CLOCKWISE) return false;\n }\n return true;\n }\n};\n\n// position of a point relative to a polygon\nenum Contain {\n OUT,\n ON,\n IN\n};\n\nContain contain(const Polygon& g, const Point& p) {\n bool in = false;\n for (int i = 0; i < (int)g.size(); ++i) {\n if (pos(Segment(g[i], g[(i + 1) % g.size()]), p) == ON_SEGMENT) return ON;\n\n auto a = g[i] - p, b = g[(i + 1) % g.size()] - p;\n if (a > b) std::swap(a, b);\n\n if (cmp(a.x, 0) <= 0 &&\n cmp(b.x, 0) > 0 &&\n cmp(cross(a, b), 0) < 0) in = !in;\n }\n return in ? IN : OUT;\n}\n\n// linear: choose colinear points\nPolygon convexhull(Polygon& g, bool linear) {\n std::sort(g.begin(), g.end());\n int n = g.size();\n\n Polygon h(n * 2);\n int k = 0;\n\n for (int i = 0; i < n; ++i) {\n while (k >= 2 &&\n cmp(cross(h[k - 1] - h[k - 2], g[i] - h[k - 2]), 0) < !linear) {\n --k;\n }\n h[k++] = g[i];\n }\n\n int t = k + 1;\n for (int i = n - 2; i >= 0; --i) {\n while (k >= t &&\n cmp(cross(h[k - 1] - h[k - 2], g[i] - h[k - 2]), 0) < !linear) {\n --k;\n }\n h[k++] = g[i];\n }\n\n h.resize(k - 1);\n h.shrink_to_fit();\n return h;\n}\n\n// requirement: g is convex\nReal diameter(const Polygon& g) {\n Real ret = 0;\n int j = 0;\n for (int i = 0; i < (int)g.size(); ++i) {\n while (j < (int)g.size() - 1 &&\n cmp(dist(g[i], g[j + 1]), dist(g[i], g[j])) > 0) ++j;\n ret = std::max(ret, dist(g[i], g[j]));\n }\n return ret;\n}\n\n// left side\n// requirement: g is convex\nPolygon convex_cut(const Polygon& g, const Segment& s) {\n Polygon h;\n for (int i = 0; i < (int)g.size(); ++i) {\n if (pos(s, g[i]) != CLOCKWISE) h.push_back(g[i]);\n\n Segment t(g[i], g[(i + 1) % g.size()]);\n if (pos(s, t.p) * pos(s, t.q) == -1) {\n h.push_back(crosspoint(s, t));\n }\n }\n return h;\n}\n\n// requirement: g is convex\nbool intersect(const Polygon& g, const Segment& s) {\n auto area = convex_cut(g, s).area();\n return cmp(area, 0) != 0 && cmp(area, g.area()) != 0;\n}\n\n/* -------------------- Circle -------------------- */\n\nstruct Circle {\n Point p;\n Real r;\n explicit Circle(const Point& p = Point(), Real r = 0) : p(p), r(r) {}\n\n // center -> radius\n friend std::istream& operator>>(std::istream& is, Circle& a) {\n return is >> a.p >> a.r;\n }\n};\n\n// the number of common tangents\n// requirement: a != b\nint intersect(const Circle& a, const Circle& b) {\n auto d = dist(a.p, b.p);\n\n return cmp(d, a.r + b.r) > 0 ? 4\n : cmp(d, a.r + b.r) == 0 ? 3\n : cmp(d, std::abs(a.r - b.r)) > 0 ? 2\n : cmp(d, std::abs(a.r - b.r)) == 0 ? 1\n : 0;\n}\n\n// s is treated as a line\nstd::vector<Point> crosspoint(const Circle& a, const Segment& s) {\n std::vector<Point> ps;\n\n auto d = dist(proj(s, a.p), a.p);\n if (cmp(d, a.r) == 0) {\n ps.push_back(proj(s, a.p));\n\n } else if (cmp(d, a.r) < 0) {\n auto p = proj(s, a.p);\n auto v = s.q - s.p;\n v *= std::sqrt(a.r * a.r - d * d) / v.abs();\n\n ps.push_back(p + v);\n ps.push_back(p - v);\n }\n return ps;\n}\n\n// requirement: a != b\nstd::vector<Point> crosspoint(const Circle& a, const Circle& b) {\n std::vector<Point> ps;\n\n auto c = intersect(a, b);\n if (c == 0 || c == 4) return ps;\n\n auto d = dist(a.p, b.p);\n auto theta = std::acos((a.r * a.r + d * d - b.r * b.r) / (a.r * d * 2));\n auto phi = (b.p - a.p).arg();\n\n ps.push_back(a.p + polar(a.r, phi + theta));\n if (c == 2) ps.push_back(a.p + polar(a.r, phi - theta));\n\n return ps;\n}\n\n} // namespace geo\n#line 2 \"main.cpp\"\n\n#line 5 \"main.cpp\"\n#include <map>\n\nbool intersect(const geo::Polygon& g, const geo::Polygon& h) {\n for (auto s : g.segments()) {\n for (auto t : h.segments()) {\n if (!geo::parallel(s, t)) continue;\n\n if (std::abs(geo::pos(s, t.p)) == 1) continue; // not on line\n\n std::pair<geo::Real, geo::Real> sint(0, s.length()), tint;\n\n tint.first = geo::dot(s.diff().unit(), t.p - s.p);\n tint.second = geo::dot(s.diff().unit(), t.q - s.p);\n if (tint.first > tint.second) std::swap(tint.first, tint.second);\n\n if (geo::cmp(sint.first, tint.second) < 0 &&\n geo::cmp(tint.first, sint.second) < 0) return true;\n }\n }\n return false;\n}\n\nbool solve() {\n int m;\n std::cin >> m;\n if (m == 0) return false;\n\n std::vector<geo::Polygon> gs(m);\n std::vector<int> ids(m);\n\n std::map<std::string, int> mp;\n int n = 0;\n for (int i = 0; i < m; ++i) {\n std::string s;\n std::cin >> s;\n\n if (!mp.count(s)) mp[s] = n++;\n ids[i] = mp[s];\n\n auto& g = gs[i];\n while (true) {\n int x;\n std::cin >> x;\n if (x == -1) break;\n\n int y;\n std::cin >> y;\n g.emplace_back(x, y);\n }\n }\n\n std::vector<std::vector<int>> graph(n);\n for (int i = 0; i < m; ++i) {\n for (int j = 0; j < i; ++j) {\n if (intersect(gs[i], gs[j])) {\n graph[ids[i]].push_back(ids[j]);\n }\n }\n }\n\n std::vector<bool> ok(1 << n, true);\n for (int b = 0; b < (1 << n); ++b) {\n for (int i = 0; i < n; ++i) {\n if ((~b >> i) & 1) continue;\n\n for (auto j : graph[i]) {\n if (i != j && ((b >> j) & 1)) ok[b] = false;\n }\n }\n }\n\n std::vector<int> ans(1 << n, n);\n ans[0] = 0;\n for (int b = 0; b < (1 << n); ++b) {\n for (int c = b; c > 0; c = (c - 1) & b) {\n if (ok[c]) ans[b] = std::min(ans[b], ans[b ^ c] + 1);\n }\n }\n\n std::cout << ans.back() << \"\\n\";\n return true;\n}\n\nint main() {\n std::cin.tie(nullptr);\n std::ios::sync_with_stdio(false);\n\n while (solve()) {}\n\n return 0;\n}", "accuracy": 1, "time_ms": 20, "memory_kb": 3396, "score_of_the_acc": -0.0311, "final_rank": 9 }, { "submission_id": "aoj_1254_4969243", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\ntemplate<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }\ntemplate<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }\n\n#define COUT(x) cout << #x << \" = \" << (x) << \" (L\" << __LINE__ << \")\" << endl\ntemplate<class T1, class T2> ostream& operator << (ostream &s, pair<T1,T2> P)\n{ return s << '<' << P.first << \", \" << P.second << '>'; }\ntemplate<class T> ostream& operator << (ostream &s, vector<T> P)\n{ for (int i = 0; i < P.size(); ++i) { if (i > 0) { s << \" \"; } s << P[i]; } return s; }\ntemplate<class T> ostream& operator << (ostream &s, vector<vector<T> > P)\n{ for (int i = 0; i < P.size(); ++i) { s << endl << P[i]; } return s << endl; }\ntemplate<class T> ostream& operator << (ostream &s, set<T> P)\n{ for(auto it : P) { s << \"<\" << it << \"> \"; } return s << endl; }\ntemplate<class T1, class T2> ostream& operator << (ostream &s, map<T1,T2> P)\n{ for(auto it : P) { s << \"<\" << it.first << \"->\" << it.second << \"> \"; } return s << endl; }\n\n\n// 彩色数\nlong long modpow(long long a, long long n, long long MOD) {\n long long res = 1;\n while (n > 0) {\n if (n & 1) res = res * a % MOD;\n a = a * a % MOD;\n n >>= 1;\n }\n return res;\n}\nint chromatic_number(const vector<vector<int> > &G) {\n const int MOD = 1000000007;\n int n = (int)G.size();\n vector<int> neighbor(n, 0);\n for (int i = 0; i < n; ++i) {\n int S = (1<<i);\n for (int j = 0; j < n; ++j)\n if (G[i][j])\n S |= (1<<j);\n neighbor[i] = S;\n }\n \n // I[S] := #. of inndepndet subset of S\n vector<int> I(1<<n);\n I[0] = 1;\n for (int S = 1; S < (1<<n); ++S) {\n int v = __builtin_ctz(S);\n I[S] = I[S & ~(1<<v)] + I[S & ~neighbor[v]];\n }\n int low = 0, high = n;\n while (high - low > 1) {\n int mid = (low + high) >> 1;\n \n // g[S] := #. of \"k independent sets\" which cover S just\n // f[S] := #. of \"k independent sets\" which consist of subseta of S\n // then\n // f[S] = sum_{T in S} g(T)\n // g[S] = sum_{T in S} (-1)^(|S|-|T|)f[T]\n long long g = 0;\n for (int S = 0; S < (1<<n); ++S) {\n if ((n - __builtin_popcount(S)) & 1) g -= modpow(I[S], mid, MOD);\n else g += modpow(I[S], mid, MOD);\n g = (g % MOD + MOD) % MOD;\n }\n if (g != 0) high = mid;\n else low = mid;\n }\n return high;\n}\n\n\n// Geometry\nusing DD = double;\nconst DD INF = 1LL<<60; // to be set appropriately\nconst DD EPS = 1e-10; // to be set appropriately\nconst DD PI = acos(-1.0);\nDD torad(int deg) {return (DD)(deg) * PI / 180;}\nDD todeg(DD ang) {return ang * 180 / PI;}\n\n/* Point */\nstruct Point {\n DD x, y;\n Point(DD x = 0.0, DD y = 0.0) : x(x), y(y) {}\n friend ostream& operator << (ostream &s, const Point &p) {return s << '(' << p.x << \", \" << p.y << ')';}\n};\ninline Point operator + (const Point &p, const Point &q) {return Point(p.x + q.x, p.y + q.y);}\ninline Point operator - (const Point &p, const Point &q) {return Point(p.x - q.x, p.y - q.y);}\ninline Point operator * (const Point &p, DD a) {return Point(p.x * a, p.y * a);}\ninline Point operator * (DD a, const Point &p) {return Point(a * p.x, a * p.y);}\ninline Point operator * (const Point &p, const Point &q) {return Point(p.x * q.x - p.y * q.y, p.x * q.y + p.y * q.x);}\ninline Point operator / (const Point &p, DD a) {return Point(p.x / a, p.y / a);}\ninline Point conj(const Point &p) {return Point(p.x, -p.y);}\ninline Point rot(const Point &p, DD ang) {return Point(cos(ang) * p.x - sin(ang) * p.y, sin(ang) * p.x + cos(ang) * p.y);}\ninline Point rot90(const Point &p) {return Point(-p.y, p.x);}\ninline DD cross(const Point &p, const Point &q) {return p.x * q.y - p.y * q.x;}\ninline DD dot(const Point &p, const Point &q) {return p.x * q.x + p.y * q.y;}\ninline DD norm(const Point &p) {return dot(p, p);}\ninline DD abs(const Point &p) {return sqrt(dot(p, p));}\ninline DD amp(const Point &p) {DD res = atan2(p.y, p.x); if (res < 0) res += PI*2; return res;}\ninline bool eq(const Point &p, const Point &q) {return abs(p - q) < EPS;}\ninline bool operator < (const Point &p, const Point &q) {return (abs(p.x - q.x) > EPS ? p.x < q.x : p.y < q.y);}\ninline bool operator > (const Point &p, const Point &q) {return (abs(p.x - q.x) > EPS ? p.x > q.x : p.y > q.y);}\ninline Point operator / (const Point &p, const Point &q) {return p * conj(q) / norm(q);}\n\n/* Line */\nstruct Line : vector<Point> {\n Line(Point a = Point(0.0, 0.0), Point b = Point(0.0, 0.0)) {\n this->push_back(a);\n this->push_back(b);\n }\n friend ostream& operator << (ostream &s, const Line &l) {return s << '{' << l[0] << \", \" << l[1] << '}';}\n};\n\nint ccw(const Point &a, const Point &b, const Point &c) {\n if (cross(b-a, c-a) > EPS) return 1;\n if (cross(b-a, c-a) < -EPS) return -1;\n if (dot(b-a, c-a) < -EPS) return 2;\n if (norm(b-a) < norm(c-a) - EPS) return -2;\n return 0;\n}\nbool isinterPS(const Point &p, const Line &s) {\n return (ccw(s[0], s[1], p) == 0);\n}\nbool isinterSS(const Line &s, const Line &t) {\n if (eq(s[0], s[1])) return isinterPS(s[0], t);\n if (eq(t[0], t[1])) return isinterPS(t[0], s);\n return (ccw(s[0], s[1], t[0]) * ccw(s[0], s[1], t[1]) <= 0 &&\n ccw(t[0], t[1], s[0]) * ccw(t[0], t[1], s[1]) <= 0);\n}\nbool issame(Line l, Line s) {\n\tfor (int it = 0; it < 2; ++it) {\n\t\tif (it == 1) swap(l, s);\n\t\tif ( !eq(l[0], s[0]) && !eq(l[0], s[1]) && ccw(s[0], s[1], l[0]) == 0 ) return true;\n\t\tif ( !eq(l[1], s[0]) && !eq(l[1], s[1]) && ccw(s[0], s[1], l[1]) == 0 ) return true;\n\t\tif ( eq(l[0], s[0]) && eq(l[1], s[1]) ) return true;\n\t\tif ( eq(l[0], s[1]) && eq(l[1], s[0]) ) return true;\n\t}\n\treturn false;\n}\n\nint main() {\n int N;\n\twhile (cin >> N, N) {\n\t\tmap<string, vector<vector<Point> > > ma;\n\t\tfor (int i = 0; i < N; ++i) {\n\t\t\tstring str; \n cin >> str;\n\t\t\tvector<Point> vp;\n\t\t\tint a, b;\n\t\t\twhile (cin >> a) {\n\t\t\t\tif (a == -1) break;\n\t\t\t\tcin >> b;\n\t\t\t\tPoint p(a, b);\n\t\t\t\tvp.push_back(p);\n\t\t\t}\n\t\t\tma[str].push_back(vp);\n\t\t}\n\t\tint V = ma.size();\n\t\tvector<vector<int> > G(V, vector<int>(V, 0));\n\n\t\tint i = 0, j = 0;\n\t\tfor (auto it1 : ma) {\n\t\t\tj = 0;\n\t\t\tfor (auto it2 : ma) {\n if (i == j) { ++j; continue; }\n\t\t\t\tbool adj = false;\n\t\t\t\t\n\t\t\t\tfor (int x = 0; x < (it1.second).size(); ++x) {\n for (int y = 0; y < (it2.second).size(); ++y) {\n\t\t\t\t\t vector<Point> vp1 = (it1.second)[x], vp2 = (it2.second)[y];\n\t\t\t\t\t for (int p = 0; p < vp1.size(); ++p) {\n for (int q = 0; q < vp2.size(); ++q) {\n\t\t\t\t\t\t Line l(vp1[p], vp1[ (p+1)%vp1.size() ]);\n\t\t\t\t\t\t Line m(vp2[q], vp2[ (q+1)%vp2.size() ]);\n\t\t\t\t\t\t if (issame(l, m)) adj = true;\n }\n\t\t\t\t\t }\n\t\t\t\t }\n }\n\t\t\t\tif (adj) G[i][j] = true;\n\t\t\t\t++j;\n\t\t\t}\n\t\t\t++i;\n\t\t}\n\t\tint res = chromatic_number(G);\n\t\tcout << res << endl;\n\t}\n}", "accuracy": 1, "time_ms": 150, "memory_kb": 3340, "score_of_the_acc": -0.3106, "final_rank": 15 }, { "submission_id": "aoj_1254_4876963", "code_snippet": "#include \"iostream\"\n#include \"climits\"\n#include \"list\"\n#include \"queue\"\n#include \"stack\"\n#include \"set\"\n#include \"functional\"\n#include \"algorithm\"\n#include \"string\"\n#include \"map\"\n#include \"unordered_map\"\n#include \"unordered_set\"\n#include \"iomanip\"\n#include \"cmath\"\n#include \"random\"\n#include \"bitset\"\n#include \"cstdio\"\n#include \"numeric\"\n#include \"cassert\"\n#include \"ctime\"\n\nusing namespace std;\n\nconstexpr long long int MOD = 1000000007;\n//constexpr int MOD = 1000000007;\n//constexpr int MOD = 998244353;\n//constexpr long long int MOD = 998244353;\nconstexpr double EPS = 1e-12;\n\n//int N, M, K, T, H, W, L, R;\nlong long int N, M, K, T, H, W, L, R;\n\nstruct Point {\n\tlong double x;\n\tlong double y;\n\tPoint(long double xx, long double yy) {\n\t\tx = xx, y = yy;\n\t}\n\tPoint() {\n\t}\n\tPoint operator + (const Point& c)const {\n\t\tPoint box;\n\t\tbox.x = x + c.x;\n\t\tbox.y = y + c.y;\n\t\treturn box;\n\t}\n\tPoint operator - (const Point& c)const {\n\t\tPoint box;\n\t\tbox.x = x - c.x;\n\t\tbox.y = y - c.y;\n\t\treturn box;\n\t}\n};\n\nstruct Line {\n\tlong double a, b, c;\n\tLine(const int aa, const int bb, const int cc) {\n\t\ta = aa, b = bb, c = cc;\n\t}\n\tLine(const Point s, const Point t) {\n\t\tif (abs(s.x - t.x) < EPS) {\n\t\t\ta = 1;\n\t\t\tb = 0;\n\t\t}\n\t\telse {\n\t\t\tb = 1;\n\t\t\ta = (t.y - s.y) / (s.x - t.x);\n\t\t}\n\t\tc = s.x*a + s.y*b;\n\t}\n};\n\nlong double dot(Point a, Point b) {\n\treturn a.x*b.x + a.y*b.y;\n}\n\nlong double cross(Point a, Point b) {\n\treturn a.x*b.y - b.x*a.y;\n}\n\nlong double Distance(Point a, Point b) {\n\treturn sqrt((a.x - b.x)*(a.x - b.x) + (a.y - b.y)*(a.y - b.y));\n}\n\nlong double Distance(Point a, pair<Point, Point>b) {\n\tLine l = Line(b.first, b.second);\n\tif (abs(l.a*a.x + l.b*a.y - l.c) < EPS) {\n\t\treturn min(Distance(a, b.first), Distance(a, b.second));\n\t}\n\tlong double A = pow((b.first.x - b.second.x), 2) + pow((b.first.y - b.second.y), 2);\n\tlong double B = 2 * (a.x - b.first.x)*(b.first.x - b.second.x) + 2 * (a.y - b.first.y)*(b.first.y - b.second.y);\n\tlong double r = B / 2 / A;\n\tr = -r;\n\tif (r >= 0 && r <= 1) {\n\t\treturn Distance(a, Point(b.first.x + r * (b.second.x - b.first.x), b.first.y + r * (b.second.y - b.first.y)));\n\t}\n\telse {\n\t\treturn min(Distance(a, b.first), Distance(a, b.second));\n\t}\n}\n\nbool LineCross(Point a1, Point a2, Point b1, Point b2) {\n\treturn cross(a2 - a1, b1 - a1)*cross(a2 - a1, b2 - a1) < EPS&&cross(b2 - b1, a1 - b1)*cross(b2 - b1, a2 - b1) < EPS && !(abs((a1 - a2).x*(a1 - b1).y - (a1 - a2).y*(a1 - b1).x)<EPS && abs((a1 - a2).x*(a1 - b2).y - (a1 - a2).y*(a1 - b2).x)<EPS&&abs(Distance(a1, a2) - Distance(a1, b1) - Distance(a2, b1)) > EPS&&abs(Distance(a1, a2) - Distance(a2, b2) - Distance(a1, b2)) > EPS&&abs(Distance(b1, b2) - Distance(a1, b1) - Distance(a1, b2)) > EPS&&abs(Distance(b1, b2) - Distance(a2, b1) - Distance(a2, b2)) > EPS);\n}\n\nlong double Distance(pair<Point, Point>a, pair<Point, Point>b) {\n\tif (LineCross(a.first, a.second, b.first, b.second))return 0;\n\treturn min({ Distance(a.first,b),Distance(a.second,b),Distance(b.first,a),Distance(b.second,a) });\n}\n\nPoint LineCross(Line a, Line b) {\n\tPoint ret;\n\tret.x = (a.c*b.b - a.b*b.c) / (a.a*b.b - a.b*b.a);\n\tret.y = -(a.c*b.a - a.a*b.c) / (a.a*b.b - a.b*b.a);\n\treturn ret;\n}\n\nlong double TriangleArea(Point a, Point b, Point c) {\n\tlong double A, B, C;\n\tA = Distance(a, b);\n\tB = Distance(a, c);\n\tC = Distance(b, c);\n\tlong double S = (A + B + C) / 2;\n\treturn sqrt(S*(S - A)*(S - B)*(S - C));\n}\n\nbool IsPointLeft(pair<Point, Point>a, Point b) {\n\ta.second = a.second - a.first;\n\tb = b - a.first;\n\ta.first = a.first - a.first;\n\tlong double radline = atan2(a.second.y, a.second.x);\n\tlong double radpoint = atan2(b.y, b.x) - radline;\n\tif (sin(radpoint) >= -EPS)return true;\n\telse return false;\n}\n\nbool IsPointInPolygon(Point p, vector<Point>poly) {\n\tbool ret = true;\n\tfor (int i = 0; i < poly.size(); i++) {\n\t\tif (Distance(p, { poly[i],poly[(i + 1) % poly.size()] }) <= EPS) return true;\n\t\tret &= IsPointLeft({ poly[i],poly[(i + 1) % poly.size()] }, p);\n\t}\n\treturn ret;\n}\n\nlong double Area(vector<Point>p) {\n\tlong double ret = 0;\n\tfor (int i = 2; i < p.size(); i++) {\n\t\tret += TriangleArea(p[0], p[i - 1], p[i]);\n\t}\n\treturn ret;\n}\n\nvector<vector<Point>>DividePolygonByLine(vector<Point>p, pair<Point, Point>l) {\n\tvector<int>cr;\n\tvector<Point>cp;\n\tfor (int k = 0; k < p.size(); k++) {\n\t\tint nx = k + 1;\n\t\tnx %= p.size();\n\t\tif (LineCross(p[k], p[(k + 1) % p.size()], l.first, l.second)) {\n\t\t\tauto np = LineCross(Line(p[k], p[(k + 1) % p.size()]), Line(l.first, l.second));\n\t\t\tbool flag = true;\n\t\t\tfor (auto l : cp) {\n\t\t\t\tif (Distance(l, np) <= EPS)flag = false;\n\t\t\t}\n\t\t\tif (flag) {\n\t\t\t\tcr.push_back(k);\n\t\t\t\tcp.push_back(np);\n\t\t\t}\n\t\t}\n\t}\n\tvector<Point>w;\n\tvector<Point>x;\n\tif (cr.size() != 2)return vector<vector<Point>>(1, p);\n\tint cnt = cr[0];\n\tdo {\n\t\tif (w.empty() || Distance(w.back(), p[cnt]) > EPS)w.push_back(p[cnt]);\n\t\tif (cnt == cr[0]) {\n\t\t\tif (w.empty() || Distance(w.back(), cp[0]) > EPS)w.push_back(cp[0]);\n\t\t\tif (w.empty() || Distance(w.back(), cp[1]) > EPS)w.push_back(cp[1]);\n\t\t\tcnt = cr[1] + 1;\n\t\t\tcnt %= p.size();\n\t\t}\n\t\telse if (cnt == cr[1]) {\n\t\t\tif (w.empty() || Distance(w.back(), cp[1]) > EPS)w.push_back(cp[1]);\n\t\t\tif (w.empty() || Distance(w.back(), cp[0]) > EPS)w.push_back(cp[0]);\n\t\t\tcnt = cr[0] + 1;\n\t\t\tcnt %= p.size();\n\t\t}\n\t\telse {\ncnt++;\ncnt %= p.size();\n\t\t}\n\t} while (cnt != cr[0]);\n\tcnt = cr[1];\n\tdo {\n\t\tif (x.empty() || Distance(x.back(), p[cnt]) > EPS)x.push_back(p[cnt]);\n\t\tif (cnt == cr[0]) {\n\t\t\tif (x.empty() || Distance(x.back(), cp[0]) > EPS)x.push_back(cp[0]);\n\t\t\tif (x.empty() || Distance(x.back(), cp[1]) > EPS)x.push_back(cp[1]);\n\t\t\tcnt = cr[1] + 1;\n\t\t\tcnt %= p.size();\n\t\t}\n\t\telse if (cnt == cr[1]) {\n\t\t\tif (x.empty() || Distance(x.back(), cp[1]) > EPS)x.push_back(cp[1]);\n\t\t\tif (x.empty() || Distance(x.back(), cp[0]) > EPS)x.push_back(cp[0]);\n\t\t\tcnt = cr[0] + 1;\n\t\t\tcnt %= p.size();\n\t\t}\n\t\telse {\n\t\t\tcnt++;\n\t\t\tcnt %= p.size();\n\t\t}\n\t} while (cnt != cr[1]);\n\tvector<vector<Point>>ret;\n\tret.push_back(w);\n\tret.push_back(x);\n\treturn ret;\n}\n\nint main() {\n\tios::sync_with_stdio(false);\n\tcin.tie(0);\n\n\tvector<int>ret;\n\tlong double pi = acos(-1);\n\twhile (cin >> N, N) {\n\t\tvector<string>name(N);\n\t\tvector<vector<Point>>vp(N);\n\t\tmap<string, int>mp;\n\t\tfor (int i = 0; i < N; i++) {\n\t\t\tcin >> name[i];\n\t\t\tmp[name[i]] = 0;\n\t\t\tlong double x, y;\n\t\t\twhile (cin >> x, x > -0.5) {\n\t\t\t\tcin >> y;\n\t\t\t\tvp[i].push_back(Point(x, y));\n\t\t\t}\n\t\t}\n\t\tint cnt = 0;\n\t\tfor (auto &i : mp)i.second = cnt++;\n\t\tvector<vector<int>>edge(mp.size(), vector<int>(mp.size()));\n\t\tfor (int i = 0; i < N; i++) {\n\t\t\tfor (int j = i + 1; j < N; j++) {\n\t\t\t\tfor (int k = 0; k < vp[i].size(); k++) {\n\t\t\t\t\tint nk = k + 1;\n\t\t\t\t\tnk %= vp[i].size();\n\t\t\t\t\tfor (int l = 0; l < vp[j].size(); l++) {\n\t\t\t\t\t\tint nl = l + 1;\n\t\t\t\t\t\tnl %= vp[j].size();\n\t\t\t\t\t\tlong double rada = atan2(vp[i][nk].y - vp[i][k].y, vp[i][nk].x - vp[i][k].x);\n\t\t\t\t\t\tlong double radb = atan2(vp[j][nl].y - vp[j][l].y, vp[j][nl].x - vp[j][l].x);\n\t\t\t\t\t\t//\tcout << setprecision(20) << \"ijkl rad \" << i << \" \" << j << \" \" << k << \" \" << l << \" \" << rada << \" \" << radb << endl;\n\t\t\t\t\t\tif (abs(rada - radb) >= EPS && abs(pi * 2 - abs(rada - radb)) > EPS&&abs(pi - abs(rada - radb)) > EPS&&abs(pi * 3 - abs(rada - radb)) > EPS) continue;\n\t\t\t\t\t\tif (Distance({ vp[i][k],vp[i][nk] }, { vp[j][l],vp[j][nl] }) > EPS)continue;\n\t\t\t\t\t\t//\tcout << \"ijkl \" << i << \" \" << j << \" \" << k << \" \" << l << endl;\n\t\t\t\t\t\tlong double alen = Distance(vp[i][k], vp[i][nk]);\n\t\t\t\t\t\tlong double blen = Distance(vp[j][l], vp[j][nl]);\n\t\t\t\t\t\tvector<long double>dis;\n\t\t\t\t\t\tdis.push_back(Distance(vp[i][k], vp[j][l]));\n\t\t\t\t\t\tdis.push_back(Distance(vp[i][k], vp[j][nl]));\n\t\t\t\t\t\tdis.push_back(Distance(vp[i][nk], vp[j][l]));\n\t\t\t\t\t\tdis.push_back(Distance(vp[i][nk], vp[j][nl]));\n\t\t\t\t\t\tsort(dis.begin(), dis.end());\n\t\t\t\t\t\t//\tfor (auto m : dis)cout << m << \" \";\n\t\t\t\t\t\t//\tcout << endl;\n\t\t\t\t\t\t//\tcout << alen << \" \" << blen << endl;\n\t\t\t\t\t\tif (dis.front() < EPS&&dis.back() >= alen + blen - EPS)continue;\n\t\t\t\t\t\tedge[mp[name[i]]][mp[name[j]]] = 1;\n\t\t\t\t\t\tedge[mp[name[j]]][mp[name[i]]] = 1;\n\t\t\t\t\t\t//\tcout << name[i] << \" \" << name[j] << endl;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\tint ans = MOD;\n\t\tvector<int>flag(1 << mp.size());\n\t\tvector<int>dp(1 << mp.size(), MOD);\n\t\tdp[0] = 0;\n\t\tfor (int i = 0; i < 1 << mp.size(); i++) {\n\t\t\tbool ok = true;\n\t\t\tfor (int j = 0; j < mp.size(); j++) {\n\t\t\t\tif ((i >> j) % 2 == 0)continue;\n\t\t\t\tfor (int k = j + 1; k < mp.size(); k++) {\n\t\t\t\t\tif ((i >> k) % 2 == 0)continue;\n\t\t\t\t\tif (edge[j][k])ok = false;\n\t\t\t\t}\n\t\t\t}\n\t\t\tflag[i] = ok;\n\t\t}\n\t\tfor (int i = 0; i <1<< mp.size(); i++) {\n\t\t\tfor (int j = 0; j <1<< mp.size(); j++) {\n\t\t\t\tif (i&j)continue;\n\t\t\t\tif (flag[j] == 0)continue;\n\t\t\t\tdp[i + j] = min(dp[i + j], dp[i] + 1);\n\t\t\t}\n\t\t}\n\t\tret.push_back(dp.back());\n\t}\n\tfor (auto i : ret)cout << i << endl;\n}", "accuracy": 1, "time_ms": 120, "memory_kb": 3228, "score_of_the_acc": -0.2391, "final_rank": 14 } ]
aoj_1255_cpp
Problem H: Inherit the Spheres In the year 2xxx, an expedition team landing on a planet found strange objects made by an ancient species living on that planet. They are transparent boxes containing opaque solid spheres (Figure 1). There are also many lithographs which seem to contain positions and radiuses of spheres. Figure 1: A strange object Initially their objective was unknown, but Professor Zambendorf found the cross section formed by a horizontal plane plays an important role. For example, the cross section of an object changes as in Figure 2 by sliding the plane from bottom to top. Figure 2: Cross sections at different positions He eventually found that some information is expressed by the transition of the number of connected figures in the cross section, where each connected figure is a union of discs intersecting or touching each other, and each disc is a cross section of the corresponding solid sphere. For instance, in Figure 2, whose geometry is described in the first sample dataset later, the number of connected figures changes as 0, 1, 2, 1, 2, 3, 2, 1, and 0, at z = 0.0000, 162.0000, 167.0000, 173.0004, 185.0000, 191.9996, 198.0000, 203.0000, and 205.0000, respectively. By assigning 1 for increment and 0 for decrement, the transitions of this sequence can be expressed by an 8-bit binary number 11011000. For helping further analysis, write a program to determine the transitions when sliding the horizontal plane from bottom ( z = 0) to top ( z = 36000). Input The input consists of a series of datasets. Each dataset begins with a line containing a positive integer, which indicates the number of spheres N in the dataset. It is followed by N lines describing the centers and radiuses of the spheres. Each of the N lines has four positive integers X i , Y i , Z i , and R i ( i = 1, . . . , N ) describing the center and the radius of the i -th sphere, respectively. You may assume 1 ≤ N ≤ 100, 1 ≤ R i ≤ 2000, 0 < X i - R i < X i + R i < 4000, 0 < Y i - R i < Y i + R i < 16000, and 0 < Z i - R i < Z i + R i < 36000. Each solid sphere is defined as the set of all points ( x , y , z ) satisfying ( x - X i ) 2 + ( y - Y i ) 2 + ( z - Z i ) 2 ≤ R i 2 . A sphere may contain other spheres. No two spheres are mutually tangent. Every Z i ± R i and minimum/maximum z coordinates of a circle formed by the intersection of any two spheres differ from each other by at least 0.01. The end of the input is indicated by a line with one zero. Output For each dataset, your program should output two lines. The first line should contain an integer M indicating the number of transitions. The second line should contain an M -bit binary number that expresses the transitions of the number of connected figures as specified above. Sample Input 3 95 20 180 18 125 20 185 18 40 27 195 10 1 5 5 5 4 2 5 5 5 4 5 5 5 3 2 5 5 5 4 5 7 5 3 16 2338 3465 29034 710 1571 14389 25019 842 1706 8015 11324 1155 1899 4359 33815 888 2160 10364 20511 1264 2048 8835 23706 1906 2598 13041 23 ...(truncated)
[ { "submission_id": "aoj_1255_6790069", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\n#define rep(i, s, t) for (int i = (int)(s); i < (int)(t); ++i)\n#define revrep(i, t, s) for (int i = (int)(t)-1; i >= (int)(s); --i)\n#define all(x) begin(x), end(x)\ntemplate <typename T> bool chmax(T& a, const T& b) { return a < b ? (a = b, 1) : 0; }\ntemplate <typename T> bool chmin(T& a, const T& b) { return a > b ? (a = b, 1) : 0; }\n\nclass UnionFind {\npublic:\n UnionFind() = default;\n explicit UnionFind(int n) : data(n, -1) {}\n\n int find(int x) {\n if (data[x] < 0) return x;\n return data[x] = find(data[x]);\n }\n\n void unite(int x, int y) {\n x = find(x);\n y = find(y);\n if (x == y) return;\n if (data[x] > data[y]) std::swap(x, y);\n data[x] += data[y];\n data[y] = x;\n }\n\n bool same(int x, int y) {\n return find(x) == find(y);\n }\n\n int size(int x) {\n return -data[find(x)];\n }\n\nprivate:\n std::vector<int> data;\n};\n\nusing T = double;\nusing Vec = std::complex<T>;\n\nconst T PI = std::acos(-1);\n\nconstexpr T eps = 1e-10;\ninline bool eq(T a, T b) { return std::abs(a - b) <= eps; }\ninline bool eq(Vec a, Vec b) { return std::abs(a - b) <= eps; }\ninline bool lt(T a, T b) { return a < b - eps; }\ninline bool leq(T a, T b) { return a <= b + eps; }\n\nstd::istream& operator>>(std::istream& is, Vec& p) {\n T x, y;\n is >> x >> y;\n p = {x, y};\n return is;\n}\n\nstruct Line {\n Vec p1, p2;\n Line() = default;\n Line(const Vec& p1, const Vec& p2) : p1(p1), p2(p2) {}\n Vec dir() const { return p2 - p1; }\n};\n\nstruct Segment {\n Vec p1, p2;\n Segment() = default;\n Segment(const Vec& p1, const Vec& p2) : p1(p1), p2(p2) {}\n Vec dir() const { return p2 - p1; }\n};\n\nstruct Circle {\n Vec c;\n T r;\n Circle() = default;\n Circle(const Vec& c, T r) : c(c), r(r) {}\n};\n\nusing Polygon = std::vector<Vec>;\n\nT dot(const Vec& a, const Vec& b) {\n return (std::conj(a) * b).real();\n}\n\nT cross(const Vec& a, const Vec& b) {\n return (std::conj(a) * b).imag();\n}\n\nVec rot(const Vec& a, T ang) {\n return a * Vec(std::cos(ang), std::sin(ang));\n}\n\nVec perp(const Vec& a) {\n return Vec(-a.imag(), a.real());\n}\n\nVec projection(const Line& l, const Vec& p) {\n return l.p1 + dot(p - l.p1, l.dir()) * l.dir() / std::norm(l.dir());\n}\n\nVec reflection(const Line& l, const Vec& p) {\n return T(2) * projection(l, p) - p;\n}\n\n// 0: collinear\n// 1: counter-clockwise\n// -1: clockwise\nint ccw(const Vec& a, const Vec& b, const Vec& c) {\n if (eq(cross(b - a, c - a), 0)) return 0;\n if (lt(cross(b - a, c - a), 0)) return -1;\n return 1;\n}\n\nvoid sort_by_arg(std::vector<Vec>& pts) {\n std::sort(pts.begin(), pts.end(), [&](auto& p, auto& q) {\n if ((p.imag() < 0) != (q.imag() < 0)) return (p.imag() < 0);\n if (cross(p, q) == 0) {\n if (p == Vec(0, 0)) return !(q.imag() < 0 || (q.imag() == 0 && q.real() > 0));\n if (q == Vec(0, 0)) return (p.imag() < 0 || (p.imag() == 0 && p.real() > 0));\n return (p.real() > q.real());\n }\n return (cross(p, q) > 0);\n });\n}\n\nstd::vector<Vec> intersection(const Circle& c1, const Circle& c2) {\n T d = std::abs(c1.c - c2.c);\n if (lt(c1.r + c2.r, d)) return {}; // outside\n Vec e1 = (c2.c - c1.c) / std::abs(c2.c - c1.c);\n Vec e2 = perp(e1);\n if (lt(d, std::abs(c2.r - c1.r))) return {}; // contain\n if (eq(d, std::abs(c2.r - c1.r))) return {c1.c + c1.r*e1}; // tangent\n T x = (c1.r*c1.r - c2.r*c2.r + d*d) / (2*d);\n T y = std::sqrt(c1.r*c1.r - x*x);\n return {c1.c + x*e1 + y*e2, c1.c + x*e1 - y*e2};\n}\n\nint intersect(const Circle& c1, const Circle& c2) {\n T d = std::abs(c1.c - c2.c);\n if (lt(d, std::abs(c2.r - c1.r))) return 0;\n if (eq(d, std::abs(c2.r - c1.r))) return 1;\n if (eq(c1.r + c2.r, d)) return 3;\n if (lt(c1.r + c2.r, d)) return 4;\n return 2;\n}\n\nstruct Vec3 {\n T x, y, z;\n Vec3() = default;\n constexpr Vec3(T x, T y, T z) : x(x), y(y), z(z) {}\n constexpr Vec3& operator+=(const Vec3& r) { x += r.x; y += r.y; z += r.z; return *this; }\n constexpr Vec3& operator-=(const Vec3& r) { x -= r.x; y -= r.y; z -= r.z; return *this; }\n constexpr Vec3& operator*=(T r) { x *= r; y *= r; z *= r; return *this; }\n constexpr Vec3& operator/=(T r) { x /= r; y /= r; z /= r; return *this; }\n constexpr Vec3 operator-() const { return Vec3(-x, -y, -z); }\n constexpr Vec3 operator+(const Vec3& r) const { return Vec3(*this) += r; }\n constexpr Vec3 operator-(const Vec3& r) const { return Vec3(*this) -= r; }\n constexpr Vec3 operator*(T r) const { return Vec3(*this) *= r; }\n constexpr Vec3 operator/(T r) const { return Vec3(*this) /= r; }\n friend constexpr Vec3 operator*(T r, const Vec3& v) { return v * r; }\n};\n\nstd::istream& operator>>(std::istream& is, Vec3& p) {\n T x, y, z;\n is >> x >> y >> z;\n p = {x, y, z};\n return is;\n}\n\nstd::ostream& operator<<(std::ostream& os, const Vec3& p) {\n os << \"(\" << p.x << \", \" << p.y << \", \" << p.z << \")\";\n return os;\n}\n\nT dot(const Vec3& a, const Vec3& b) {\n return a.x*b.x + a.y*b.y + a.z*b.z;\n}\n\nVec3 cross(const Vec3& a, const Vec3& b) {\n return Vec3(a.y*b.z-a.z*b.y, a.z*b.x-a.x*b.z, a.x*b.y-a.y*b.x);\n}\n\nnamespace std {\nT norm(const Vec3& a) { return dot(a, a); }\nT abs(const Vec3& a) { return std::sqrt(std::norm(a)); }\n}\n\n\nint main() {\n ios_base::sync_with_stdio(false);\n cin.tie(nullptr);\n cout << fixed << setprecision(15);\n\n while (true) {\n int N;\n cin >> N;\n if (N == 0) break;\n vector<pair<Vec3, T>> sp;\n vector<T> events;\n rep(i,0,N) {\n Vec3 c;\n T r;\n cin >> c >> r;\n sp.push_back({c, r});\n events.push_back(c.z-r);\n events.push_back(c.z+r);\n }\n rep(i,0,N) rep(j,i+1,N) {\n auto [ci, ri] = sp[i];\n auto [cj, rj] = sp[j];\n Vec3 d = cj - ci;\n Circle cli(Vec(0, 0), ri);\n Circle clj(Vec(sqrt(d.x*d.x+d.y*d.y), d.z), rj);\n auto ints = intersection(cli, clj);\n if (ints.size() < 2) continue;\n T z0 = ints[0].imag() + ci.z;\n T z1 = ints[1].imag() + ci.z;\n events.push_back(z0);\n events.push_back(z1);\n }\n sort(all(events));\n string ans = \"\";\n int prv = 0;\n for (T z : events) {\n z += 10*eps;\n vector<bool> exist(N);\n UnionFind uf(N);\n vector<Circle> sections(N);\n rep(i,0,N) {\n auto [ci, ri] = sp[i];\n if (leq(ci.z-ri, z) && leq(z, ci.z+ri)) {\n exist[i] = true;\n } else {\n continue;\n }\n T dz = ci.z - z;\n sections[i] = Circle(Vec(ci.x, ci.y), sqrt(ri*ri-dz*dz));\n }\n rep(i,0,N) rep(j,i+1,N) {\n if (exist[i] && exist[j] && intersect(sections[i], sections[j]) != 4) {\n uf.unite(i, j);\n }\n }\n set<int> st;\n rep(i,0,N) if (exist[i]) st.insert(uf.find(i));\n int cur = st.size();\n if (prv < cur) ans += '1';\n else if (prv > cur) ans += '0';\n prv = cur;\n }\n cout << ans.size() << endl;\n cout << ans << endl;\n }\n}", "accuracy": 1, "time_ms": 20, "memory_kb": 3492, "score_of_the_acc": -0.1101, "final_rank": 11 }, { "submission_id": "aoj_1255_6725239", "code_snippet": "#line 1 \"1255.cpp\"\n/*\tauthor: Kite_kuma\n\tcreated: 2022.06.16 15:36:45 */\n\n#line 2 \"SPJ-Library/geometry/geometry.hpp\"\n#include <algorithm>\n#include <cassert>\n#include <cmath>\n#include <complex>\n#include <vector>\n\nusing R = long double;\nusing point = std::complex<R>;\nusing arrow = point;\nconst R EPS(1e-10), PI(acosl(-1));\n\ninline bool eq(const R &a, const R &b) { return fabsl(b - a) < EPS; }\ninline bool same_point(const point &a, const point &b) { return abs(b - a) < EPS; }\n/*\n\tsign of x\n\t-1: x < 0\n\t 0: x == 0\n\t 1: x > 0\n*/\ninline int sgn(const R &x) { return fabsl(x) < EPS ? 0 : (x < 0 ? -1 : 1); }\n/*\n\tsign of (a-b)\n\t-1: a < b\n\t 0: a == b\n\t 1: a > b\n*/\ninline int compare(const R &a, const R &b) { return eq(a, b) ? 0 : a < b ? -1 : 1; }\n\nstd::istream &operator>>(std::istream &is, point &p) {\n\tR a, b;\n\tis >> a >> b;\n\tp = point(a, b);\n\treturn is;\n}\nstd::ostream &operator<<(std::ostream &os, point &p) { return os << '(' << p.real() << \", \" << p.imag() << ')'; }\n\n// rotate point 'p' for counter clockwise direction\npoint rotate(const point &p, const R &theta) {\n\treturn point(cosl(theta) * p.real() - sinl(theta) * p.imag(), sinl(theta) * p.real() + cosl(theta) * p.imag());\n}\n\nR radian_to_degree(const R &r) { return (r * 180.0 / PI); }\nR degree_to_radian(const R &d) { return (d * PI / 180.0); }\n\n// get angle a-b-c (<pi)\nR get_angle(const point &a, const point &b, const point &c) {\n\tconst point v(a - b), w(c - b);\n\tR theta = fabsl(atan2l(w.imag(), w.real()) - atan2l(v.imag(), v.real()));\n\treturn std::min(theta, 2 * PI - theta);\n}\n\nnamespace std {\nbool operator<(const point &a, const point &b) { return a.real() != b.real() ? a.real() < b.real() : a.imag() < b.imag(); }\n} // namespace std\n\nstruct segment;\nstruct line {\n\tpoint a, b;\n\tline() = default;\n\tline(const point &a, const point &b) : a(a), b(b) {}\n\t// Ax + By + C = 0\n\tline(const R &A, const R &B, const R &C) {\n\t\tif(eq(A, 0)) {\n\t\t\tassert(!eq(B, 0));\n\t\t\ta = point(0, -C / B), b = point(1, -(A + C) / B);\n\t\t} else {\n\t\t\ta = point(-C / A, 0), b = point(-(B + C) / A, 1);\n\t\t}\n\t}\n\texplicit line(const segment &seg);\n\tfriend std::ostream &operator<<(std::ostream &os, line &ln) { return os << '(' << ln.a << \" -- \" << ln.b << ')'; }\n\tfriend std::istream &operator>>(std::istream &is, line &a) { return is >> a.a >> a.b; }\n};\nstruct segment {\n\tpoint a, b;\n\tsegment() = default;\n\tsegment(const point &a, const point &b) : a(a), b(b) {}\n\texplicit segment(const line &ln) : a(ln.a), b(ln.b) {}\n\tfriend std::ostream &operator<<(std::ostream &os, segment &seg) { return os << '[' << seg.a << \" -- \" << seg.b << ']'; }\n\tfriend std::istream &operator>>(std::istream &is, segment &a) { return is >> a.a >> a.b; }\n};\nline::line(const segment &seg) : a(seg.a), b(seg.b) {}\n\nstruct circle {\n\tpoint center;\n\tR radius;\n\tcircle() = default;\n\tcircle(const point &center, const R &radius) : center(center), radius(radius) {}\n};\n\nusing points = std::vector<point>;\nusing polygon = std::vector<point>;\nusing segments = std::vector<segment>;\nusing lines = std::vector<line>;\nusing circles = std::vector<circle>;\n\nR cross(const point &a, const point &b) { return real(a) * imag(b) - imag(a) * real(b); }\nR dot(const point &a, const point &b) { return real(a) * real(b) + imag(a) * imag(b); }\n\nenum CCW {\n\tONLINE_FRONT = -2,\n\tCLOCKWISE = -1,\n\tON_SEGMENT = 0,\n\tCOUNTER_CLOCKWISE = 1,\n\tONLINE_BACK = 2,\n};\nint ccw(const point &a, point b, point c) {\n\tb -= a, c -= a;\n\tconst R crs_b_c = cross(b, c);\n\tif(crs_b_c > EPS) return CCW::COUNTER_CLOCKWISE;\n\tif(crs_b_c < -EPS) return CCW::CLOCKWISE;\n\tif(dot(b, c) < -EPS) return CCW::ONLINE_BACK;\n\tif(norm(b) + EPS < norm(c)) return CCW::ONLINE_FRONT;\n\treturn CCW::ON_SEGMENT;\n}\n\nbool parallel(const arrow &a, const arrow &b) { return eq(cross(a, b), R(0)); }\nbool parallel(const line &a, const line &b) { return parallel(a.b - a.a, b.b - b.a); }\nbool parallel(const line &a, const segment &b) { return parallel(a.b - a.a, b.b - b.a); }\nbool parallel(const segment &a, const line &b) { return parallel(a.b - a.a, b.b - b.a); }\nbool parallel(const segment &a, const segment &b) { return parallel(a.b - a.a, b.b - b.a); }\n\nbool orthogonal(const arrow &a, const arrow &b) { return eq(dot(a, b), R(0)); }\nbool orthogonal(const line &a, const line &b) { return orthogonal(a.b - a.a, b.b - b.a); }\nbool orthogonal(const line &a, const segment &b) { return orthogonal(a.b - a.a, b.b - b.a); }\nbool orthogonal(const segment &a, const line &b) { return orthogonal(a.b - a.a, b.b - b.a); }\nbool orthogonal(const segment &a, const segment &b) { return orthogonal(a.b - a.a, b.b - b.a); }\n\npoint projection(const line &l, const point &p) { return l.a + (l.a - l.b) * dot(p - l.a, l.a - l.b) / norm(l.a - l.b); }\npoint projection(const segment &s, const point &p) { return projection(line(s), p); }\n\npoint reflection(const line &l, const point &p) { return projection(l, p) * R(2) - p; }\npoint reflection(const segment &s, const point &p) { return projection(line(s), p); }\n\nR distance(const point &p, const point &q);\nR distance(const line &l, const point &p);\nint number_of_common_tangents(const circle &c1, const circle &c2) {\n\tconst R r1 = std::min(c1.radius, c2.radius), r2 = std::max(c1.radius, c2.radius), d = distance(c1.center, c2.center);\n\tint com = compare(r1 + r2, d);\n\treturn com == 1 ? compare(d + r1, r2) + 1 : 3 - com;\n}\n\n// number of common points (-1: infinite)\nint intersect(const line &l, const point &p) { return int(abs(ccw(l.a, l.b, p)) != 1); }\nint intersect(const point &p, const line &l) { return intersect(l, p); }\nint intersect(const line &l, const line &m) {\n\tif(intersect(l, m.a) && intersect(l, m.b)) return -1;\n\treturn int(!parallel(l, m));\n}\nint intersect(const segment &s, const point &p) { return int(ccw(s.a, s.b, p) == CCW::ON_SEGMENT); }\nint intersect(const point &p, const segment &s) { return intersect(s, p); }\nint intersect(const line &l, const segment &s) {\n\tif(intersect(l, s.a) && intersect(l, s.b)) return -1;\n\treturn ccw(l.a, l.b, s.a) * ccw(l.a, l.b, s.b) != 1;\n}\nint intersect(const segment &s, const line &l) { return intersect(l, s); }\nint intersect(const circle &c, const line &l) {\n\tR d = c.radius - distance(l, c.center);\n\treturn fabsl(d) < EPS ? 1 : d > 0. ? 2 : 0;\n}\nint intersect(const line &l, const circle &c) { return intersect(c, l); }\nint intersect(const circle &c, const point &p) { return int(eq(c.radius, distance(c.center, p))); }\nint intersect(const point &p, const circle &c) { return intersect(c, p); }\nint intersect(const segment &s, const segment &t) {\n\tif(same_point(s.a, s.b)) return intersect(t, s.a);\n\tif(intersect(line(s), t.a) && intersect(line(s), t.b) &&\n\t std::max(std::min(s.a, s.b), std::min(t.a, t.b)) < std::min(std::max(s.a, s.b), std::max(t.a, t.b)))\n\t\treturn -1;\n\treturn int(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);\n}\nint intersect(const circle &c, const segment &s) {\n\tconst point h = projection(s, c.center);\n\tconst int c0 = compare(distance(h, c.center), c.radius);\n\tif(c0 == 1) return 0;\n\tif(c0 == 0) return intersect(s, h);\n\tconst int c1 = compare(distance(c.center, s.a), c.radius), c2 = compare(distance(c.center, s.b), c.radius);\n\tif(std::min(c1, c2) == -1) return int(std::max(c1, c2) >= 0);\n\treturn intersect(s, h) ? 2 : 0;\n}\nint intersect(const segment &s, const circle &c) { return intersect(c, s); }\nint intersect(const circle &c1, const circle &c2) { return 2 - abs(2 - number_of_common_tangents(c1, c2)); }\n\n// distance of two shaps\nR distance(const point &a, const point &b) { return fabs(a - b); }\nR distance(const line &l, const point &p) { return distance(p, projection(l, p)); }\nR distance(const point &p, const line &l) { return distance(l, p); }\nR distance(const line &l, const line &m) { return parallel(l, m) ? distance(l, m.a) : 0; }\nR distance(const segment &s, const point &p) {\n\tconst point r = projection(s, p);\n\treturn intersect(s, r) ? distance(r, p) : std::min(distance(s.a, p), distance(s.b, p));\n}\nR distance(const point &p, const segment &s) { return distance(s, p); }\nR distance(const segment &a, const segment &b) {\n\tif(intersect(a, b)) return R(0);\n\treturn std::min({distance(a, b.a), distance(a, b.b), distance(b, a.a), distance(b, a.b)});\n}\nR distance(const line &l, const segment &s) {\n\tif(intersect(l, s)) return 0;\n\treturn std::min(distance(l, s.a), distance(l, s.b));\n}\nR distance(const segment &s, const line &l) { return distance(l, s); }\nR distance(const circle &c, const point &p) { return fabsl(distance(c.center, p) - c.radius); }\nR distance(const point &p, const circle &c) { return distance(c, p); }\nR distance(const circle &c, const line &l) { return std::max(R(0), distance(l, c.center) - c.radius); }\nR distance(const line &l, const circle &c) { return distance(c, l); }\nR distance(const circle &c1, const circle &c2) {\n\tconst R d = distance(c1.center, c2.center);\n\tif(d > c1.radius + c2.radius) return d - c1.radius - c2.radius;\n\tif(d < fabsl(c1.radius - c2.radius)) return fabsl(c1.radius - c2.radius) - d;\n\treturn R(0);\n}\nR distance(const circle &c, const segment &s) {\n\tconst point p = projection(s, c.center);\n\tconst R dist_min = intersect(s, p) ? distance(c.center, p) : std::min(distance(c.center, s.a), distance(c.center, s.b));\n\tif(dist_min > c.radius) return dist_min - c.radius;\n\tconst R dist_max = std::max(distance(c.center, s.a), distance(c.center, s.b));\n\treturn dist_max < c.radius ? c.radius - dist_max : R(0);\n}\nR distance(const segment &s, const circle &c) { return distance(c, s); }\n\npoint crosspoint(const line &l, const line &m) {\n\tR A = cross(l.b - l.a, m.b - m.a);\n\tR B = cross(l.b - l.a, l.b - m.a);\n\tif(eq(A, 0.)) return m.a;\n\treturn m.a + (m.b - m.a) * B / A;\n}\npoint crosspoint(const segment &s, const segment &t) { return crosspoint(line(s), line(t)); }\npoint crosspoint(const segment &s, const line &l) { return crosspoint(line(s), l); }\npoint crosspoint(const line &l, const segment &s) { return crosspoint(l, line(s)); }\npoints crosspoints(const circle &c, const line &l) {\n\tconst point pr = projection(l, c.center);\n\tconst R square = c.radius * c.radius - norm(pr - c.center);\n\tswitch(sgn(square)) {\n\t\tcase 0:\n\t\t\treturn points{pr};\n\t\tcase -1:\n\t\t\treturn points(0);\n\t}\n\tconst arrow v = (l.b - l.a) / abs(l.b - l.a) * sqrtl(square);\n\treturn points{pr - v, pr + v};\n}\npoints crosspoints(const line &l, const circle &c) { return crosspoints(c, l); }\npoints crosspoints(const circle &c, const segment &s) {\n\tpoints ret;\n\tfor(const auto &pt : crosspoints(c, line(s)))\n\t\tif(intersect(s, pt)) ret.push_back(pt);\n\treturn ret;\n}\npoints crosspoints(const segment &s, const circle &c) { return crosspoints(c, s); }\npoints crosspoints(const circle &c1, const circle &c2) {\n\tR d = abs(c1.center - c2.center);\n\tif(compare(d, c1.radius + c2.radius) == 1) return points(0);\n\tif(compare(d, fabsl(c1.radius - c2.radius)) == -1) return points(0);\n\tbool one_crosspoint = false;\n\tif(eq(d, c1.radius + c2.radius) || eq(d, fabsl(c1.radius - c2.radius))) one_crosspoint = true;\n\tconst R alpha = acosl((c1.radius * c1.radius + d * d - c2.radius * c2.radius) / (2 * c1.radius * d)); // cosine theorem\n\tconst R beta = std::arg(c2.center - c1.center);\n\tif(one_crosspoint) return points{c1.center + std::polar(c1.radius, beta + alpha)};\n\treturn points{c1.center + std::polar(c1.radius, beta + alpha), c1.center + std::polar(c1.radius, beta - alpha)};\n}\n\npoints tangent_points(const circle &c, const point &p) {\n\tconst R square = norm(c.center - p) - c.radius * c.radius;\n\tswitch(sgn(square)) {\n\t\tcase 0:\n\t\t\treturn points{p};\n\t\tcase -1:\n\t\t\treturn points{};\n\t}\n\treturn crosspoints(c, circle(p, sqrtl(square)));\n}\n\n// common tangents of two circles\nlines tangents(circle c1, circle c2) {\n\tlines ret;\n\tif(c1.radius < c2.radius) std::swap(c1, c2);\n\tconst R g = distance(c1.center, c2.center);\n\tif(!sgn(g)) return ret;\n\tconst arrow u = (c2.center - c1.center) / g;\n\tconst arrow v = rotate(u, PI * 0.5);\n\tfor(const int &s : {-1, 1}) {\n\t\tconst R h = (c1.radius + s * c2.radius) / g;\n\t\tif(eq(1 - h * h, 0)) {\n\t\t\tret.emplace_back(c1.center + u * c1.radius, c1.center + (u + v) * c1.radius);\n\t\t} else if(1 - h * h > 0) {\n\t\t\tconst point uu = u * h, vv = v * sqrtl(1 - h * h);\n\t\t\tret.emplace_back(c1.center + (uu + vv) * c1.radius, c2.center - (uu + vv) * c2.radius * R(s));\n\t\t\tret.emplace_back(c1.center + (uu - vv) * c1.radius, c2.center - (uu - vv) * c2.radius * R(s));\n\t\t}\n\t}\n\treturn ret;\n}\n#line 2 \"SPJ-Library/geometry/contain.hpp\"\n\nenum CONTAIN { OUT = 0, ON = 1, IN = 2 };\nint contains(const polygon &poly, const point &p) {\n\tbool in = false;\n\tfor(int i = 0; i < poly.size(); i++) {\n\t\tpoint a = poly[i], b = poly[(i + 1) % poly.size()];\n\t\tif(ccw(a, b, p) == 0) return CONTAIN::ON;\n\t\tif(a.imag() > b.imag()) swap(a, b);\n\t\tif(a.imag() <= p.imag() && p.imag() < b.imag() && cross(a - p, b - p) < 0) in = !in;\n\t}\n\treturn in ? CONTAIN::IN : CONTAIN::OUT;\n}\nint contains(const circle &c, const point &p) { return compare(c.radius, distance(c.center, p)) + 1; }\n#line 2 \"SPJ-Library/template/kuma.hpp\"\n\n#line 2 \"SPJ-Library/template/basic_func.hpp\"\n\n#line 5 \"SPJ-Library/template/basic_func.hpp\"\n#include <iostream>\n#line 7 \"SPJ-Library/template/basic_func.hpp\"\n\ntemplate <class T0, class T1, class T2>\ninline bool in_range(T0 x, T1 lef, T2 rig) {\n\treturn ((lef <= x) && (x < rig));\n}\n\ntemplate <class T>\ninline bool chmin(T &a, T b) {\n\tif(a > b) {\n\t\ta = b;\n\t\treturn true;\n\t}\n\treturn false;\n}\ntemplate <class T>\ninline bool chmax(T &a, T b) {\n\tif(a < b) {\n\t\ta = b;\n\t\treturn true;\n\t}\n\treturn false;\n}\n\nvoid Yes(bool flag = true) { std::cout << (flag ? \"Yes\" : \"No\") << '\\n'; }\nvoid YES(bool flag = true) { std::cout << (flag ? \"YES\" : \"NO\") << '\\n'; }\n\ntemplate <class T>\nvoid drop(T answer) {\n\tstd::cout << answer << '\\n';\n\texit(0);\n}\n\ntemplate <class T>\nvoid vout(std::vector<T> const &v, bool vertically = 0) {\n\tif(vertically) {\n\t\tfor(auto const &a : v) {\n\t\t\tstd::cout << a << '\\n';\n\t\t}\n\t\treturn;\n\t}\n\tfor(auto it = v.begin(); it != v.end(); it++) {\n\t\tstd::cout << (*it);\n\t\tif(it != v.end() - 1) {\n\t\t\tstd::cout << ' ';\n\t\t}\n\t}\n\tstd::cout << '\\n';\n\treturn;\n}\n\ninline void print() { std::cout << '\\n'; }\ntemplate <class T>\ninline void print(const T &x) {\n\tstd::cout << x << '\\n';\n\treturn;\n}\ntemplate <typename Head, typename... Tail>\nvoid print(const Head &H, const Tail &... T) {\n\tstd::cout << H << \" \";\n\tprint(T...);\n}\n\ntemplate <class T>\nvoid add(std::vector<T> &v, T value) {\n\tfor(auto &a : v) a += value;\n\treturn;\n}\n\ntemplate <class T>\nT greatest_lower_multiple(T x, T d) {\n\tif(d == 0) return 0;\n\tif(d < 0) d *= -1;\n\tT y = x % d;\n\tif(y < 0) y += d;\n\treturn x - y;\n}\n\ntemplate <class T>\nT least_upper_multiple(T x, T d) {\n\treturn -greatest_lower_multiple(-x, d);\n}\n\n// ceil(a / b);\ntemplate <class T>\nT dup(T a, T b) {\n\tassert(b);\n\tif(b < 0) {\n\t\ta *= -1;\n\t\tb *= -1;\n\t}\n\treturn least_upper_multiple(a, b) / b;\n}\n\nlong long pow_ll(long long a, long long n) {\n\tassert(n >= 0LL);\n\tif(n == 0) return 1LL;\n\tif(a == 0) return 0LL;\n\tif(a == 1) return 1LL;\n\tif(a == -1) return (n & 1LL) ? -1LL : 1LL;\n\tlong long res = 1;\n\twhile(n > 1LL) {\n\t\tif(n & 1LL) res *= a;\n\t\ta *= a;\n\t\tn >>= 1;\n\t}\n\treturn res * a;\n}\n\nlong long modpow(long long a, long long n, long long mod) {\t // a^n mod\n\tassert(n >= 0 && mod);\n\tif(mod == 1) return 0LL;\n\tlong long res = 1;\n\ta %= mod;\n\twhile(n > 0) {\n\t\tif(n & 1) res = res * a % mod;\n\t\ta = a * a % mod;\n\t\tn >>= 1;\n\t}\n\treturn res < 0 ? res + mod : res;\n}\n\n// return x which satisfies a * x % mod == gcd(a, mod)\n// not (mod | a)\nlong long modinv(long long a, const long long mod) {\n\tlong long b = mod, u = 1, v = 0;\n\twhile(b) {\n\t\tlong long t = a / b;\n\t\ta -= t * b;\n\t\tstd::swap(a, b);\n\t\tu -= t * v;\n\t\tstd::swap(u, v);\n\t}\n\tu %= mod;\n\tif(u < 0) u += mod;\n\treturn u;\n}\n\ntemplate <class T>\nint lb(const std::vector<T> &a, const T x) {\n\treturn (int)std::distance(a.begin(), std::lower_bound(a.begin(), a.end(), x));\n}\ntemplate <class T>\nint ub(const std::vector<T> &a, const T x) {\n\treturn (int)std::distance(a.begin(), std::upper_bound(a.begin(), a.end(), x));\n}\ntemplate <class T>\nvoid unq_sort(std::vector<T> &a) {\n\tstd::sort(a.begin(), a.end());\n\ta.erase(std::unique(a.begin(), a.end()), a.end());\n}\ntemplate <class T>\nstd::vector<int> press(const std::vector<T> &a) {\n\tstd::vector<T> vec = a;\n\tunq_sort(vec);\n\tstd::vector<int> ret;\n\tfor(const auto &v : a) ret.push_back(lb(vec, v));\n\treturn ret;\n}\n#line 1 \"SPJ-Library/template/header.hpp\"\n#include <bits/stdc++.h>\n#line 2 \"SPJ-Library/template/io.hpp\"\n\n#line 4 \"SPJ-Library/template/io.hpp\"\n\n#line 8 \"SPJ-Library/template/debug.hpp\"\n\n#line 3 \"SPJ-Library/template/constants.hpp\"\n\nconstexpr int inf = 1000'000'000;\nconstexpr long long INF = 1'000'000'000'000'000'000LL;\nconstexpr int mod_1000000007 = 1000000007;\nconstexpr int mod_998244353 = 998244353;\nconst long double pi = acosl(-1.);\nconstexpr int dx[8] = {1, 0, -1, 0, 1, -1, -1, 1};\nconstexpr int dy[8] = {0, 1, 0, -1, 1, 1, -1, -1};\n#line 10 \"SPJ-Library/template/debug.hpp\"\n\nnamespace viewer {\n\ntemplate <typename T, typename U>\nvoid view(const std::pair<T, U> &p);\n\ntemplate <class T0, class T1, class T2>\nvoid view(const std::tuple<T0, T1, T2> &p);\n\ntemplate <class T0, class T1, class T2, class T3>\nvoid view(const std::tuple<T0, T1, T2, T3> &p);\n\nvoid view(const long long &e);\n\nvoid view(const int &e);\n\ntemplate <typename T>\nvoid view(const T &e);\n\ntemplate <typename T>\nvoid view(const std::set<T> &s);\n\ntemplate <typename T>\nvoid view(const std::unordered_set<T> &s);\n\ntemplate <typename T>\nvoid view(const std::multiset<T> &s);\n\ntemplate <typename T>\nvoid view(const std::unordered_multiset<T> &s);\n\ntemplate <typename T>\nvoid view(const std::vector<T> &v);\n\ntemplate <typename T, std::size_t ary_size>\nvoid view(const std::array<T, ary_size> &v);\n\ntemplate <typename T>\nvoid view(const std::vector<std::vector<T>> &vv);\n\ntemplate <typename T, typename U>\nvoid view(const std::vector<std::pair<T, U>> &v);\n\ntemplate <class T0, class T1, class T2>\nvoid view(const std::vector<std::tuple<T0, T1, T2>> &v);\n\ntemplate <class T0, class T1, class T2, class T3>\nvoid view(const std::vector<std::tuple<T0, T1, T2, T3>> &v);\n\ntemplate <typename map_type>\nvoid view_map_container(const map_type &m);\n\ntemplate <typename T, typename U>\nvoid view(const std::map<T, U> &m);\n\ntemplate <typename T, typename U>\nvoid view(const std::unordered_map<T, U> &m);\n\ntemplate <typename container_type>\nvoid view_container(const container_type &c, bool vertically = false) {\n\ttypename container_type::const_iterator begin = c.begin();\n\tconst typename container_type::const_iterator end = c.end();\n\tif(vertically) {\n\t\tstd::cerr << \"{\\n\";\n\t\twhile(begin != end) {\n\t\t\tstd::cerr << '\\t';\n\t\t\tview(*(begin++));\n\t\t\tif(begin != end) std::cerr << ',';\n\t\t\tstd::cerr << '\\n';\n\t\t}\n\t\tstd::cerr << '}';\n\t\treturn;\n\t}\n\tstd::cerr << \"{ \";\n\twhile(begin != end) {\n\t\tview(*(begin++));\n\t\tif(begin != end) std::cerr << ',';\n\t\tstd::cerr << ' ';\n\t}\n\tstd::cerr << '}';\n}\n\ntemplate <typename T, typename U>\nvoid view(const std::pair<T, U> &p) {\n\tstd::cerr << '(';\n\tview(p.first);\n\tstd::cerr << \", \";\n\tview(p.second);\n\tstd::cerr << ')';\n}\n\ntemplate <class T0, class T1, class T2>\nvoid view(const std::tuple<T0, T1, T2> &p) {\n\tstd::cerr << '(';\n\tview(std::get<0>(p));\n\tstd::cerr << \", \";\n\tview(std::get<1>(p));\n\tstd::cerr << \", \";\n\tview(std::get<2>(p));\n\tstd::cerr << ')';\n}\n\ntemplate <class T0, class T1, class T2, class T3>\nvoid view(const std::tuple<T0, T1, T2, T3> &p) {\n\tstd::cerr << '(';\n\tview(std::get<0>(p));\n\tstd::cerr << \", \";\n\tview(std::get<1>(p));\n\tstd::cerr << \", \";\n\tview(std::get<2>(p));\n\tstd::cerr << \", \";\n\tview(std::get<3>(p));\n\tstd::cerr << ')';\n}\n\nvoid view(const long long &e) {\n\tif(e == INF)\n\t\tstd::cerr << \"INF\";\n\telse if(e == -INF)\n\t\tstd::cerr << \"-INF\";\n\telse\n\t\tstd::cerr << e;\n}\n\nvoid view(const int &e) {\n\tif(e == inf)\n\t\tstd::cerr << \"inf\";\n\telse if(e == -inf)\n\t\tstd::cerr << \"-inf\";\n\telse\n\t\tstd::cerr << e;\n}\n\ntemplate <typename T>\nvoid view(const T &e) {\n\tstd::cerr << e;\n}\n\ntemplate <typename T>\nvoid view(const std::set<T> &s) {\n\tview_container(s);\n}\n\ntemplate <typename T>\nvoid view(const std::unordered_set<T> &s) {\n\tview_container(s);\n}\n\ntemplate <typename T>\nvoid view(const std::multiset<T> &s) {\n\tview_container(s);\n}\n\ntemplate <typename T>\nvoid view(const std::unordered_multiset<T> &s) {\n\tview_container(s);\n}\n\ntemplate <typename T>\nvoid view(const std::vector<T> &v) {\n\tview_container(v);\n}\n\ntemplate <typename T, std::size_t ary_size>\nvoid view(const std::array<T, ary_size> &v) {\n\tview_container(v);\n}\n\ntemplate <typename T>\nvoid view(const std::vector<std::vector<T>> &vv) {\n\tview_container(vv, true);\n}\n\ntemplate <typename T, typename U>\nvoid view(const std::vector<std::pair<T, U>> &v) {\n\tview_container(v, true);\n}\n\ntemplate <class T0, class T1, class T2>\nvoid view(const std::vector<std::tuple<T0, T1, T2>> &v) {\n\tview_container(v, true);\n}\n\ntemplate <class T0, class T1, class T2, class T3>\nvoid view(const std::vector<std::tuple<T0, T1, T2, T3>> &v) {\n\tview_container(v, true);\n}\n\ntemplate <typename map_type>\nvoid view_map_container(const map_type &m) {\n\tstd::cerr << \"{\\n\";\n\tfor(typename map_type::const_iterator it = m.begin(); it != m.end(); it++) {\n\t\tstd::cerr << \"\\t[\";\n\t\tview(it->first);\n\t\tstd::cerr << \"] : \";\n\t\tview(it->second);\n\t\tstd::cerr << '\\n';\n\t}\n\tstd::cerr << \"}\";\n}\n\ntemplate <typename T, typename U>\nvoid view(const std::map<T, U> &m) {\n\tview_map_container(m);\n}\n\ntemplate <typename T, typename U>\nvoid view(const std::unordered_map<T, U> &m) {\n\tview_map_container(m);\n}\n\n} // namespace viewer\n\n// when compiling : g++ foo.cpp -DLOCAL\n#ifdef LOCAL\nvoid debug_out() {}\ntemplate <typename T>\nvoid debug_out(const T &x) {\n\tviewer::view(x);\n}\ntemplate <typename Head, typename... Tail>\nvoid debug_out(const Head &H, const Tail &... T) {\n\tviewer::view(H);\n\tstd::cerr << \", \";\n\tdebug_out(T...);\n}\n#define debug(...) \\\n\tdo { \\\n\t\tstd::cerr << __LINE__ << \" [\" << #__VA_ARGS__ << \"] : [\"; \\\n\t\tdebug_out(__VA_ARGS__); \\\n\t\tstd::cerr << \"]\\n\"; \\\n\t} while(0)\n#else\n#define debug(...) (void(0))\n#endif\n#line 2 \"SPJ-Library/template/scanner.hpp\"\n\n#line 6 \"SPJ-Library/template/scanner.hpp\"\n\nnamespace scanner {\ntemplate <class T>\nvoid scan(T &a) {\n\tstd::cin >> a;\n}\n\ntemplate <class T, class L>\nvoid scan(std::pair<T, L> &p) {\n\tscan(p.first);\n\tscan(p.second);\n}\n\ntemplate <class T0, class T1, class T2>\nvoid scan(std::tuple<T0, T1, T2> &p) {\n\tT0 t0;\n\tT1 t1;\n\tT2 t2;\n\tscan(t0);\n\tscan(t1);\n\tscan(t2);\n\tp = std::make_tuple(t0, t1, t2);\n}\n\ntemplate <class T0, class T1, class T2, class T3>\nvoid scan(std::tuple<T0, T1, T2, T3> &p) {\n\tT0 t0;\n\tT1 t1;\n\tT2 t2;\n\tT3 t3;\n\tscan(t0);\n\tscan(t1);\n\tscan(t2);\n\tscan(t3);\n\tp = std::make_tuple(t0, t1, t2, t3);\n}\n\ntemplate <class T>\nvoid scan(std::vector<T> &a) {\n\tfor(auto &i : a) scan(i);\n}\n\nvoid INPUT() {}\ntemplate <class Head, class... Tail>\nvoid INPUT(Head &head, Tail &... tail) {\n\tscan(head);\n\tINPUT(tail...);\n}\n} // namespace scanner\n#line 7 \"SPJ-Library/template/io.hpp\"\n\n#define VEC(type, name, size) \\\n\tvector<type> name(size); \\\n\tscanner::INPUT(name)\n#define VVEC(type, name, h, w) \\\n\tvector<vector<type>> name(h, vector<type>(w)); \\\n\tscanner::INPUT(name)\n#define INT(...) \\\n\tint __VA_ARGS__; \\\n\tscanner::INPUT(__VA_ARGS__)\n#define LL(...) \\\n\tlong long __VA_ARGS__; \\\n\tscanner::INPUT(__VA_ARGS__)\n#define STR(...) \\\n\tstring __VA_ARGS__; \\\n\tscanner::INPUT(__VA_ARGS__)\n#define CHR(...) \\\n\tchar __VA_ARGS__; \\\n\tscanner::INPUT(__VA_ARGS__)\n#define DBL(...) \\\n\tdouble __VA_ARGS__; \\\n\tscanner::INPUT(__VA_ARGS__)\n#define LD(...) \\\n\tlong double __VA_ARGS__; \\\n\tscanner::INPUT(__VA_ARGS__)\n#define TPL3(type0, type1, type2, name) \\\n\tstd::tuple<type0, type1, type2> name; \\\n\tscanner::INPUT(name);\n#define TPL4(type0, type1, type2, type3, name) \\\n\tstd::tuple<type0, type1, type2, type3> name; \\\n\tscanner::INPUT(name);\n\ntemplate <typename T1, typename T2>\nstd::istream &operator>>(std::istream &is, std::pair<T1, T2> &p) {\n\tis >> p.first >> p.second;\n\treturn is;\n}\n\ntemplate <typename T1, typename T2>\nstd::ostream &std::operator<<(std::ostream &os, const std::pair<T1, T2> &p) {\n\tos << p.first << ' ' << p.second;\n\treturn os;\n}\ntemplate <class T>\nstd::ostream &std::operator<<(std::ostream &os, const std::vector<T> &v) {\n\tfor(typename std::vector<T>::const_iterator it = v.begin(); it != v.end(); it++) {\n\t\tif(it != v.begin()) std::cerr << ' ';\n\t\tos << *it;\n\t}\n\treturn os;\n}\n\nstruct fast_io {\n\tfast_io() {\n\t\tstd::ios::sync_with_stdio(false);\n\t\tstd::cin.tie(nullptr);\n\t\tstd::cout << std::fixed << std::setprecision(15);\n\t\tsrand((unsigned)time(NULL));\n\t}\n} fast_io_;\n#line 2 \"SPJ-Library/template/macros.hpp\"\n\n#define foa(s, v) for(auto &s : v)\n#define all(v) (v).begin(), (v).end()\n#define rall(v) (v).rbegin(), (v).rend()\n#define pcnt(n) __builtin_popcountll((long long)n)\n\n#define REPname(a, b, c, d, e, ...) e\n#define rep(...) REPname(__VA_ARGS__, REP3, REP2, REP1, REP0)(__VA_ARGS__)\n#define REP0(x) for(int Counter_in_rep_macro = 0; Counter_in_rep_macro < (x); ++Counter_in_rep_macro)\n#define REP1(i, x) for(int i = 0; i < (x); ++i)\n#define REP2(i, l, r) for(int i = (l); i < (r); ++i)\n#define REP3(i, l, r, c) for(int i = (l); i < (r); i += (c))\n#define DREPname(a, b, c, d, e, ...) e\n#define drep(...) DREPname(__VA_ARGS__, DREP3, DREP2, DREP1)(__VA_ARGS__)\n#define DREP1(i, x) for(int i = (x)-1; i >= 0; --i)\n#define DREP2(i, l, r) for(int i = (r)-1; i >= (l); --i)\n#define DREP3(i, l, r, c) for(int i = (r)-1; i >= (l); i -= (c))\n\nusing ll = long long;\nusing ld = long double;\nusing ull = unsigned long long;\nusing vi = std::vector<int>;\nusing vvi = std::vector<std::vector<int>>;\nusing vvvi = std::vector<std::vector<std::vector<int>>>;\nusing vll = std::vector<ll>;\nusing vvll = std::vector<vll>;\nusing vvvll = std::vector<vvll>;\ntemplate <class T = ll>\nusing V = std::vector<T>;\ntemplate <class T = ll>\nusing VV = V<V<T>>;\ntemplate <class T = ll>\nusing VVV = V<V<V<T>>>;\ntemplate <class T = ll>\nusing pqup = std::priority_queue<T, std::vector<T>, std::greater<T>>;\ntemplate <class T = ll>\nusing pqdn = std::priority_queue<T>;\n#line 7 \"SPJ-Library/template/kuma.hpp\"\n\nusing namespace std;\n#line 7 \"1255.cpp\"\n\nstring solve(int n) {\n\tconst int dim = 3;\n\tvector<array<R, dim>> centers;\n\tvector<R> radius;\n\trep(n) {\n\t\tarray<R, dim> cent;\n\t\trep(i, dim) cin >> cent[i];\n\t\tcenters.push_back(cent);\n\t\tR r;\n\t\tcin >> r;\n\t\tradius.push_back(r);\n\t}\n\n\tvector<tuple<R, int, int>> events;\n\n\tvector<int> notuse(n);\n\trep(i, n) {\n\t\tconst R xi = centers[i][0];\n\t\tconst R yi = centers[i][1];\n\t\tconst R zi = centers[i][2];\n\t\tevents.emplace_back(zi - radius[i], i, -1);\n\t\tevents.emplace_back(zi + radius[i], i, -1);\n\t\trep(j, i + 1, n) {\n\t\t\tconst R xj = centers[j][0];\n\t\t\tconst R yj = centers[j][1];\n\t\t\tconst R zj = centers[j][2];\n\t\t\tconst R distxy = distance(point(xi, yi), point(xj, yj));\n\t\t\tcircle ci(point(0, zi), radius[i]), cj(point(distxy, zj), radius[j]);\n\n\t\t\tconst int cross = number_of_common_tangents(ci, cj);\n\t\t\tassert(cross != 1 && cross != 3);\n\t\t\tif(cross == 2) {\n\t\t\t\tconst points cpts = crosspoints(ci, cj);\n\t\t\t\tR zmin = cpts.front().imag();\n\t\t\t\tR zmax = cpts.back().imag();\n\t\t\t\tif(zmin > zmax) swap(zmin, zmax);\n\t\t\t\tdebug(i, j, zmin, zmax);\n\t\t\t\tauto update_point = [&](point p) {\n\t\t\t\t\tif(contains(ci, p) == CONTAIN::OUT) return;\n\t\t\t\t\tif(contains(cj, p) == CONTAIN::OUT) return;\n\t\t\t\t\tR znew = p.imag();\n\t\t\t\t\tchmax(zmax, znew);\n\t\t\t\t\tchmin(zmin, znew);\n\t\t\t\t\treturn;\n\t\t\t\t};\n\t\t\t\tupdate_point(ci.center + arrow(0, radius[i]));\n\t\t\t\tupdate_point(ci.center - arrow(0, radius[i]));\n\t\t\t\tupdate_point(cj.center + arrow(0, radius[j]));\n\t\t\t\tupdate_point(cj.center - arrow(0, radius[j]));\n\t\t\t\tevents.emplace_back(zmin - EPS, i, j);\n\t\t\t\tevents.emplace_back(zmax + EPS, i, j);\n\t\t\t} else if(cross == 0) {\n\t\t\t\tint erase = i;\n\t\t\t\tif(radius[j] < radius[i]) erase = j;\n\t\t\t\tnotuse[erase] = 1;\n\t\t\t}\n\t\t}\n\t}\n\n\tsort(all(events));\n\n\tvector<int> exists(n);\n\tvector<vector<int>> edge(n, vi(n));\n\n\tdebug(events);\n\n\tvector<int> visited(n);\n\tauto dfs = [&](auto dfs, int now) -> void {\n\t\tdebug(now, visited);\n\t\tvisited[now] = 1;\n\t\trep(nxt, n) {\n\t\t\tif(visited[nxt] || !exists[nxt] || !edge[now][nxt]) continue;\n\t\t\tdfs(dfs, nxt);\n\t\t}\n\t\treturn;\n\t};\n\n\tauto components = [&]() -> int {\n\t\tvisited.assign(n, 0);\n\t\tint ret = 0;\n\t\tdebug(exists);\n\t\trep(i, n) if(exists[i] && !visited[i]) {\n\t\t\tdfs(dfs, i);\n\t\t\tret++;\n\t\t}\n\t\treturn ret;\n\t};\n\n\tstring ans;\n\tint c = 0;\n\tfor(auto [z, i, j] : events) {\n\t\tif(notuse[i]) continue;\n\t\tif(j == -1) {\n\t\t\texists[i] ^= 1;\n\t\t} else {\n\t\t\tif(notuse[j]) continue;\n\t\t\tedge[i][j] ^= 1;\n\t\t\tedge[j][i] ^= 1;\n\t\t}\n\t\tint c_new = components();\n\t\tif(c < c_new)\n\t\t\tans += '1';\n\t\telse if(c > c_new)\n\t\t\tans += '0';\n\t\tc = c_new;\n\t}\n\treturn ans;\n}\nint main() {\n\tint n;\n\twhile(cin >> n && n) {\n\t\tstring ans = solve(n);\n\t\tprint(ans.size());\n\t\tcout << ans << '\\n';\n\t}\n\treturn 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3960, "score_of_the_acc": -0.1089, "final_rank": 10 }, { "submission_id": "aoj_1255_6043918", "code_snippet": "#include <bits/stdc++.h>\n\nusing namespace std;\n\n#define eps 1e-9\n#define INF 2000000000 // 2e9\n#define LLINF 2000000000000000000ll // 2e18 (llmax:9e18)\n#define all(x) (x).begin(), (x).end()\n#define sq(x) ((x) * (x))\n\n#define rep(i, x) for (int i = 0; i < (int)(x); i++)\n#define drep(i, x) for (int i = ((int)(x)-1); i >= 0; i--)\n\n#define popcount __builtin_popcount\n#define next __next\n#define prev __prev\n\n#ifndef LOCAL\n#define dmp(...) ;\n#else\n#define dmp(...) \\\n cerr << \"[ \" << #__VA_ARGS__ << \" ] : \" << dump_str(__VA_ARGS__) << endl\n#endif\n\n// ---------------- Utility ------------------\n\ntemplate <class T>\nbool chmin(T &a, const T &b) {\n if (a <= b) return false;\n a = b;\n return true;\n}\n\ntemplate <class T>\nbool chmax(T &a, const T &b) {\n if (a >= b) return false;\n a = b;\n return true;\n}\n\ntemplate <class T>\nusing MaxHeap = priority_queue<T>;\n\ntemplate <class T>\nusing MinHeap = priority_queue<T, vector<T>, greater<T>>;\n\ntemplate <class T>\nvector<T> vect(int len, T elem) {\n return vector<T>(len, elem);\n}\n\n// ----------------- Input -------------------\n\ntemplate <class T, class U>\nistream &operator>>(istream &is, pair<T, U> &p) {\n return is >> p.first >> p.second;\n}\n\ntemplate <class T>\nistream &operator>>(istream &is, vector<T> &vec) {\n for (int i = 0; i < vec.size(); i++) is >> vec[i];\n return is;\n}\n\n// ----------------- Output ------------------\n\ntemplate <class T, class U>\nostream &operator<<(ostream &os, const pair<T, U> &p) {\n return os << p.first << ',' << p.second;\n}\n\ntemplate <class T>\nostream &operator<<(ostream &os, const vector<T> &v) {\n for (const T &e : v) os << e << \" \";\n return os;\n}\n\ntemplate <class T>\nostream &operator<<(ostream &os, const deque<T> &d) {\n for (const T &e : d) os << e << \" \";\n return os;\n}\n\ntemplate <class T>\nostream &operator<<(ostream &os, const set<T> &s) {\n os << \"{ \";\n for (const T &e : s) os << e << \" \";\n return os << \"}\";\n}\n\ntemplate <class T, class U>\nostream &operator<<(ostream &os, const map<T, U> &m) {\n os << \"{ \";\n for (const auto &[key, val] : m) os << \"( \" << key << \" -> \" << val << \" ) \";\n return os << \"}\";\n}\n\ntemplate <class TupleTy, size_t... I>\nvoid dump_tuple(ostream &os, const TupleTy t, std::index_sequence<I...>) {\n (..., (os << (I == 0 ? \"\" : \",\") << std::get<I>(t)));\n}\n\ntemplate <class... Args>\nostream &operator<<(ostream &os, const tuple<Args...> &t) {\n os << \"(\";\n dump_tuple(os, t, std::make_index_sequence<sizeof...(Args)>());\n return os << \")\";\n}\n\nvoid dump_str_rec(ostringstream &) {}\n\ntemplate <class Head, class... Tail>\nvoid dump_str_rec(ostringstream &oss, Head &&head, Tail &&... tail) {\n oss << \", \" << head;\n dump_str_rec(oss, forward<Tail>(tail)...);\n}\n\ntemplate <class T, class... U>\nstring dump_str(T &&arg, U &&... args) {\n ostringstream oss;\n oss << arg;\n dump_str_rec(oss, forward<U>(args)...);\n return oss.str();\n}\n\n// --------------- Fast I/O ------------------\n\nvoid fastio() {\n cin.tie(0);\n ios::sync_with_stdio(0);\n cout << fixed << setprecision(20);\n}\n\n// ------------------ ACL --------------------\n\n// #include <atcoder/modint>\n// constexpr int MOD = 998244353;\n// constexpr int MOD = 1000000007;\n// using mint = atcoder::static_modint<MOD>;\n// ostream &operator<<(ostream &os, const mint &v) {\n// return os << v.val();\n// }\n\n// ------------ End of template --------------\n\n// Geometry Library\n// written by okuraofvegetable\n\n#define pb push_back\n#define DOUBLE_INF 1e50\n#define Points vector<Point>\n\n#define EQ(a, b) (abs((a) - (b)) < eps)\n#define GT(a, b) ((a) > (b))\n#define GE(a, b) (GT(a, b) || EQ(a, b))\n#define LT(a, b) ((a) < (b))\n#define LE(a, b) (LT(a, b) || EQ(a, b))\n\n#define double long double\n\ninline double add(double a, double b) {\n if (abs(a + b) < eps * (abs(a) + abs(b))) return 0;\n return a + b;\n}\n\n// --------------------- Point -----------------------\n\n#define Vector Point\nstruct Point {\n double x, y;\n Point() {}\n Point(double x, double y) : x(x), y(y) {}\n Point operator+(Point p) { return Point(add(x, p.x), add(y, p.y)); }\n Point operator-(Point p) { return Point(add(x, -p.x), add(y, -p.y)); }\n Point operator*(double d) { return Point(x * d, y * d); }\n Point operator/(double d) { return Point(x / d, y / d); }\n double dot(Point p) { return add(x * p.x, y * p.y); }\n double det(Point p) { return add(x * p.y, -y * p.x); }\n double norm() { return sqrt(x * x + y * y); }\n double norm2() { return x * x + y * y; }\n double dist(Point p) { return ((*this) - p).norm(); }\n double dist2(Point p) { return sq(x - p.x) + sq(y - p.y); }\n double arg() { return atan2(y, x); } // [-PI,PI]\n double arg(Vector v) { return v.arg() - arg(); } // henkaku\n double angle(Vector v) { // [0,PI]\n return acos(cos(arg(v)));\n }\n Vector normalize() { return (*this) * (1.0 / norm()); }\n Point vert() { return Point(y, -x); }\n\n // Signed area of triange (0,0) (x,y) (p.x,p.y)\n double area(Point p) { return (x * p.y - p.x * y) / 2.0; }\n friend istream &operator>>(istream &is, Point &p) { return is >> p.x >> p.y; }\n friend ostream &operator<<(ostream &os, const Point &p) {\n return os << '(' << p.x << ',' << p.y << ')';\n }\n};\n\nPoint divide_rate(Point a, Point b, double A, double B) {\n assert(!EQ(A + B, 0.0));\n return (a * B + b * A) * (1.0 / (A + B));\n}\n\nVector polar(double len, double arg) {\n return Vector(cos(arg) * len, sin(arg) * len);\n}\n\n// Direction a -> b -> c\n// verified AOJ CGL_1_C\nenum { COUNTER_CLOCKWISE, CLOCKWISE, ONLINE_BACK, ONLINE_FRONT, ON_SEGMENT };\nint ccw(Point a, Point b, Point c) {\n Vector p = b - a;\n Vector q = c - a;\n if (p.det(q) > 0.0) return COUNTER_CLOCKWISE; // counter clockwise\n if (p.det(q) < 0.0) return CLOCKWISE; // clockwise\n if (p.dot(q) < 0.0) return ONLINE_BACK; // c--a--b online_back\n if (p.norm() < q.norm()) return ONLINE_FRONT; // a--b--c online_front\n return ON_SEGMENT; // a--c--b on_segment\n}\n\n// --------------------- Line ------------------------\nstruct Line {\n Point a, b;\n Line() {}\n Line(Point a, Point b) : a(a), b(b) {}\n bool on(Point q) { return EQ((a - q).det(b - q), 0.0); }\n // following 2 functions verified AOJ CGL_2_A\n bool is_parallel(Line l) { return EQ((a - b).det(l.a - l.b), 0.0); }\n bool is_orthogonal(Line l) { return EQ((a - b).dot(l.a - l.b), 0.0); }\n Point intersection(Line l) {\n assert(!is_parallel(l));\n return a + (b - a) * ((l.b - l.a).det(l.a - a) / (l.b - l.a).det(b - a));\n }\n // Projection of p to this line\n // verified AOJ CGL_1_A\n Point projection(Point p) {\n return (b - a) * ((b - a).dot(p - a) / (b - a).norm2()) + a;\n }\n double distance(Point p) {\n Point q = projection(p);\n return p.dist(q);\n }\n // Reflection point of p onto this line\n // verified AOJ CGL_1_B\n Point refl(Point p) {\n Point proj = projection(p);\n return p + ((proj - p) * 2.0);\n }\n bool left(Point p) {\n if ((p - a).det(b - a) > 0.0)\n return true;\n else\n return false;\n }\n friend istream &operator>>(istream &is, Line &l) { return is >> l.a >> l.b; }\n friend ostream &operator<<(ostream &os, const Line &l) {\n return os << l.a << \" -> \" << l.b;\n }\n};\n\n// ------------------- Segment ----------------------\nstruct Segment {\n Point a, b;\n Segment() {}\n Segment(Point a, Point b) : a(a), b(b) {}\n Line line() { return Line(a, b); }\n bool on(Point q) {\n return (EQ((a - q).det(b - q), 0.0) && LE((a - q).dot(b - q), 0.0));\n }\n // verified AOJ CGL_2_B\n bool is_intersect(Segment s) {\n if (line().is_parallel(s.line())) {\n if (on(s.a) || on(s.b)) return true;\n if (s.on(a) || s.on(b)) return true;\n return false;\n }\n Point p = line().intersection(s.line());\n if (on(p) && s.on(p))\n return true;\n else\n return false;\n }\n bool is_intersect(Line l) {\n if (line().is_parallel(l)) {\n if (l.on(a) || l.on(b))\n return true;\n else\n return false;\n }\n Point p = line().intersection(l);\n if (on(p))\n return true;\n else\n return false;\n }\n // following 2 distance functions are verified at AOJ CGL_2_D\n double distance(Point p) {\n double res = DOUBLE_INF;\n Point q = line().projection(p);\n if (on(q)) res = min(res, p.dist(q));\n res = min(res, min(p.dist(a), p.dist(b)));\n return res;\n }\n double distance(Segment s) {\n if (is_intersect(s)) return 0.0;\n double res = DOUBLE_INF;\n res = min(res, s.distance(a));\n res = min(res, s.distance(b));\n res = min(res, this->distance(s.a));\n res = min(res, this->distance(s.b));\n return res;\n }\n friend istream &operator>>(istream &is, Segment &s) {\n return is >> s.a >> s.b;\n }\n friend ostream &operator<<(ostream &os, const Segment &s) {\n return os << s.a << \" -> \" << s.b;\n }\n};\n\n// ------------------- Polygon -----------------------\n// Note: A polygon generally don't need to be convex.\n\ntypedef vector<Point> Polygon;\n// verified AOJ CGL_3_A\ndouble area(Polygon &pol) {\n Points vec;\n double res = 0.0;\n int M = pol.size();\n for (int i = 0; i < M; i++) {\n res += (pol[i] - pol[0]).area(pol[(i + 1) % M] - pol[0]);\n }\n return res;\n}\n\nbool is_convex(Polygon &pol) {\n int n = pol.size();\n for (int i = 0; i < n - 1; i++) {\n if (ccw(pol[i], pol[i + 1], pol[(i + 2) % n]) == CLOCKWISE) {\n return false;\n }\n }\n return true;\n}\n\n// vecrified AOJ CGL_3_C\nenum { OUT, ON, IN };\nint contained(Polygon &pol, Point p) {\n int n = pol.size();\n Point outer(1e9, p.y);\n int cnt = 0;\n for (int i = 0; i < n; i++) {\n Segment e = Segment(pol[i], pol[(i + 1) % n]);\n if (e.on(p)) return ON;\n Vector a = pol[i] - p;\n Vector b = pol[(i + 1) % n] - p;\n if (a.y > b.y) swap(a, b);\n if (LE(a.y, 0.0) && GT(b.y, 0.0)) {\n if (LT(a.det(b), 0.0)) cnt++;\n }\n }\n if ((cnt & 1) == 1)\n return IN;\n else\n return OUT;\n}\n\n// A compare function for convex_hull\n// sort points by (x-y) lexicographical order.\n// you can change (y-x) order with no change in convex_hull\nbool comp(const Point &p, const Point &q) {\n if (!EQ(p.x, q.x))\n return p.x < q.x;\n else\n return p.y < q.y;\n}\n\n// Convex hull\n// if you want not to contain points on boundary,\n// change while(....<=0.0)\n// verified AOJ CGL_4_A\nPolygon convex_hull(vector<Point> ps) {\n sort(all(ps), comp);\n int k = 0;\n int n = ps.size();\n Polygon qs(2 * n);\n for (int i = 0; i < n; i++) {\n while (k > 1 && LT((qs[k - 1] - qs[k - 2]).det(ps[i] - qs[k - 1]), 0.0))\n k--;\n qs[k++] = ps[i];\n }\n for (int i = n - 2, t = k; i >= 0; i--) {\n while (k > t && LT((qs[k - 1] - qs[k - 2]).det(ps[i] - qs[k - 1]), 0.0))\n k--;\n qs[k++] = ps[i];\n }\n qs.resize(k - 1);\n return qs;\n}\n\n// Caliper method\n// verified AOJ CGL_4_B\ndouble convex_diameter(Polygon &cv) {\n int i = 0, j = 0;\n int n = cv.size();\n for (int k = 0; k < n; k++) {\n if (!comp(cv[i], cv[k])) i = k;\n if (comp(cv[j], cv[k])) j = k;\n }\n int si = i, sj = j;\n double res = 0.0;\n while (i != sj || j != si) {\n res = max(res, cv[i].dist(cv[j]));\n if ((cv[(i + 1) % n] - cv[i]).det(cv[(j + 1) % n] - cv[j]) < 0.0)\n i = (i + 1) % n;\n else\n j = (j + 1) % n;\n }\n return res;\n}\n\n// Cut conovex polygon by a line and return left polygon\n// verified AOJ CGL_4_C\nPolygon convex_cut(Polygon &cv, Line l) {\n int n = cv.size();\n Polygon left;\n for (int i = 0; i < n; i++) {\n Segment e = Segment(cv[i], cv[(i + 1) % n]);\n if (ccw(l.a, l.b, cv[i]) != CLOCKWISE) left.pb(cv[i]);\n if (e.is_intersect(l)) {\n if (!e.line().is_parallel(l)) { left.pb(e.line().intersection(l)); }\n }\n }\n return left;\n}\n\n// Distance between closest pair\n// verified CGL_5_A\nbool comp_y(const Point &p, const Point &q) {\n return p.y < q.y;\n}\ndouble closest_pair(vector<Point>::iterator a, int n) {\n if (n <= 1) return DOUBLE_INF;\n int m = n / 2;\n double x = (a + m)->x;\n double d = min(closest_pair(a, m), closest_pair(a + m, n - m));\n inplace_merge(a, a + m, a + n, comp_y);\n vector<Point> b;\n for (int i = 0; i < n; i++) {\n double ax = (a + i)->x;\n double ay = (a + i)->y;\n if (abs(ax - x) >= d) continue;\n for (int j = 0; j < b.size(); j++) {\n double dx = ax - b[b.size() - 1 - j].x;\n double dy = ay - b[b.size() - 1 - j].y;\n if (dy >= d) break;\n d = min(d, sqrt(dx * dx + dy * dy));\n }\n b.pb(*(a + i));\n }\n return d;\n}\ndouble closest_pair(vector<Point> a) {\n sort(all(a), comp);\n return closest_pair(a.begin(), (int)a.size());\n}\n\n// -------------------- Circle -----------------------\n\n// Relationship between two circles\n// Each value as integer corresponds to the number of common tangent lines.\n// (e.g. When two circles are circumscribed with each other, there are 3 common\n// tangent lines.)\nenum {\n INCLUDE,\n INSCRIBED, // \"Naisetsu\" in Japanese\n INTERSECT,\n CIRCUMSCRIBED, // \"Gaisetsu\" in Japanese\n NOT_CROSS,\n};\n\nstruct Circle {\n Point center;\n double r;\n Circle() {}\n Circle(Point c, double r) : center(c), r(r) {}\n bool in(Point p) { return p.dist(center) < r; }\n // verified AOJ CGL_7_A\n int is_intersect(Circle c) {\n double cd = center.dist(c.center);\n if (EQ(cd, r + c.r)) return CIRCUMSCRIBED;\n if (EQ(cd, abs(r - c.r))) return INSCRIBED;\n if (cd > r + c.r)\n return NOT_CROSS;\n else if (cd > abs(r - c.r))\n return INTERSECT;\n else\n return INCLUDE;\n }\n // verified AOJ CGL_7_D\n Points intersection(Line l) {\n Points res;\n double d = l.distance(center);\n if (EQ(d, r)) {\n res.pb(l.projection(center));\n } else if (d < r) {\n double k = sqrt(r * r - d * d);\n Vector v = (l.a - l.b).normalize() * k;\n res.pb(l.projection(center) + v);\n res.pb(l.projection(center) - v);\n }\n return res;\n }\n Points intersection(Segment s) {\n Points tmp = intersection(s.line());\n Points res;\n for (int i = 0; i < tmp.size(); i++) {\n if (s.on(tmp[i])) res.pb(tmp[i]);\n }\n return res;\n }\n Points intersection(Circle c) {\n Points res;\n double d = (c.center - center).norm();\n double a = acos((r * r + d * d - c.r * c.r) / (2.0 * r * d));\n double t = (c.center - center).arg();\n res.pb(center + polar(r, t + a));\n res.pb(center + polar(r, t - a));\n return res;\n }\n Points tangent(Point p) {\n Points res;\n double d = center.dist(p);\n double a = asin(r / d);\n double t = (center - p).arg();\n double len = sqrt(d * d - r * r);\n res.pb(p + polar(len, t + a));\n res.pb(p + polar(len, t - a));\n return res;\n }\n // verified CGL_7_G\n Points common_tangent(Circle c) {\n Points res;\n int state = is_intersect(c);\n if (EQ(r, c.r)) { // radius of the two circles are same\n Point p = divide_rate(center, c.center, r, c.r);\n Vector v = (c.center - center).vert().normalize() * r;\n if (state == INCLUDE)\n return res;\n else if (state == INSCRIBED) {\n cerr << \"Same Circle\" << endl;\n assert(false);\n } else if (state == INTERSECT) {\n res.pb(center + v);\n res.pb(center - v);\n return res;\n } else if (state == CIRCUMSCRIBED) {\n res.pb(center + v);\n res.pb(center - v);\n res.pb(p);\n return res;\n } else { // NOT_CROSS\n res = tangent(p);\n res.pb(center + v);\n res.pb(center - v);\n return res;\n }\n } else {\n Point p = divide_rate(center, c.center, r, c.r);\n Point q = divide_rate(center, c.center, r, -c.r);\n if (state == INCLUDE)\n return res;\n else if (state == INSCRIBED) {\n res.pb(q);\n return res;\n } else if (state == INTERSECT) {\n return tangent(q);\n } else if (state == CIRCUMSCRIBED) {\n res = tangent(q);\n res.pb(p);\n return res;\n } else { // NOT_CROSS\n res = tangent(p);\n Points add = tangent(q);\n for (int i = 0; i < add.size(); i++) res.pb(add[i]);\n return res;\n }\n }\n }\n // Area of intersection between this circle\n // and left side of given line l.\n // \"left\" is defined by considering line as directed vector(l.a -> l.b)\n // note: this function has not been verified yet.\n double left_area(Line l) {\n Points res = intersection(l);\n if (res.size() < 2) {\n if (l.left(center))\n return M_PI * r * r;\n else\n return 0.0;\n }\n Vector a = res[0] - center;\n Vector b = res[1] - center;\n double tri = abs(a.area(b));\n double fan = r * r * abs(a.arg(b)) / 2.0;\n if (l.left(center))\n return M_PI * r * r - (fan - tri);\n else\n return fan - tri;\n }\n // Returen signed area of intersection\n // between triangle(center-a-b) and this circle\n // verified AOJ CGL_7_H\n double area_intersection_triangle(Point a, Point b) {\n Segment s = Segment(a, b);\n double res = abs((a - center).area(b - center));\n if (EQ(res, 0.0)) return 0.0;\n if (in(a) && !in(b)) {\n Point p = intersection(s)[0];\n Point q = intersection(Segment(center, b))[0];\n res -= abs((p - b).area(q - b));\n res -= abs((p - center).area(q - center));\n res += abs(r * r * ((p - center).angle(b - center)) / 2.0);\n } else if (!in(a) && in(b)) {\n Point p = intersection(s)[0];\n Point q = intersection(Segment(center, a))[0];\n res -= abs((p - a).area(q - a));\n res -= abs((p - center).area(q - center));\n res += abs(r * r * ((p - center).angle(a - center)) / 2.0);\n } else if (!in(a) && !in(b)) {\n Points ps = intersection(s);\n if (ps.size() == 2) {\n Point p = ps[0];\n Point q = ps[1];\n Point m = divide_rate(p, q, 1.0, 1.0);\n res = abs(area_intersection_triangle(a, m)) +\n abs(area_intersection_triangle(m, b));\n } else {\n res = (r * r * (a - center).angle(b - center) / 2.0);\n }\n }\n if ((a - center).det(b - center) < 0.0) res = -res;\n return res;\n }\n // Area of intersection between given polygon and\n // this circle. polygon don't need to be convex\n double area_intersection_polygon(Polygon pol) {\n int n = pol.size();\n double res = 0.0;\n for (int i = 0; i < n; i++) {\n double tmp = area_intersection_triangle(pol[i], pol[(i + 1) % n]);\n res += tmp;\n }\n return abs(res);\n }\n friend istream &operator>>(istream &is, Circle &c) {\n return is >> c.center >> c.r;\n }\n friend ostream &operator<<(ostream &os, const Circle &c) {\n return os << c.center << \" , rad: \" << c.r;\n }\n};\n\nstruct UnionFind {\n vector<int> par, rank, sz;\n UnionFind(int n) {\n par.assign(n, 0);\n rank.assign(n, 0);\n sz.assign(n, 1);\n for (int i = 0; i < n; i++) par[i] = i;\n }\n int find(const int x) { return (par[x] == x) ? x : (par[x] = find(par[x])); }\n void unite(int x, int y) {\n x = find(x);\n y = find(y);\n if (x == y) return;\n if (rank[x] < rank[y]) {\n par[x] = y;\n sz[y] += sz[x];\n } else {\n par[y] = x;\n sz[x] += sz[y];\n if (rank[x] == rank[y]) rank[x]++;\n }\n }\n bool same(const int x, const int y) { return find(x) == find(y); }\n int size(const int x) { return sz[find(x)]; }\n vector<vector<int>> components() {\n int n = par.size();\n vector<vector<int>> cs;\n vector<int> idx(n, -1);\n for (int i = 0; i < n; i++) {\n if (find(i) != i) continue;\n idx[i] = cs.size();\n cs.emplace_back();\n }\n for (int i = 0; i < n; i++) {\n int id = idx[find(i)];\n cs[id].push_back(i);\n }\n return cs;\n }\n};\n\n#define endl \"\\n\"\n// #define int long long\nusing ll = long long;\nusing pii = pair<int, int>;\n\nconst bool multitest = false;\n\nbool solve() {\n int N;\n cin >> N;\n if (N == 0) return false;\n\n vector<double> X(N), Y(N), Z(N), R(N);\n for (int i = 0; i < N; i++) { cin >> X[i] >> Y[i] >> Z[i] >> R[i]; }\n\n auto cut_z = [&](int id, double z) {\n double zdiff = abs(Z[id] - z);\n if (zdiff > R[id]) return Circle(Point(0, 0), 0);\n double r = sqrtl(sq((double)R[id]) - sq(zdiff));\n assert(!isnan(r));\n return Circle(Point(X[id], Y[id]), r);\n };\n\n double fluc = 0.001;\n vector<double> zs;\n zs.push_back(0);\n zs.push_back(36000);\n {\n vector<Circle> cs;\n for (int i = 0; i < N; i++) { cs.emplace_back(Point(X[i], Z[i]), R[i]); }\n for (int i = 0; i < N; i++) {\n for (int d = -1; d <= 1; d++) {\n zs.push_back(Z[i] - R[i] + fluc * d);\n zs.push_back(Z[i] + R[i] + fluc * d);\n }\n for (int j = 0; j < N; j++) {\n if (i == j) continue;\n double dist = sq(X[i] - X[j]) + sq(Y[i] - Y[j]) + sq(Z[i] - Z[j]);\n if (dist > sq(R[i] + R[j])) continue;\n if (dist < sq(R[i] - R[j])) continue;\n double inter_z = Z[i] + (Z[j] - Z[i]) / sqrtl(dist) * (R[i] - eps);\n double ub = (double)std::max(Z[i] + R[i], Z[j] + R[j]) + eps;\n double lb = (double)std::min(Z[i] - R[i], Z[j] - R[j]) - eps;\n {\n double l = lb, r = inter_z;\n for (int _ = 0; _ < 30; _++) {\n double mid = (l + r) / 2.0;\n Circle a = cut_z(i, mid);\n Circle b = cut_z(j, mid);\n if (a.r == 0 || b.r == 0) {\n l = mid;\n continue;\n }\n if (a.is_intersect(b) == NOT_CROSS)\n l = mid;\n else\n r = mid;\n }\n for (int d = -1; d <= 1; d++) { zs.push_back(r + fluc * d); }\n }\n {\n double l = inter_z, r = ub;\n for (int _ = 0; _ < 30; _++) {\n double mid = (l + r) / 2.0;\n Circle a = cut_z(i, mid);\n Circle b = cut_z(j, mid);\n if (a.r == 0 || b.r == 0) {\n r = mid;\n continue;\n }\n if (a.is_intersect(b) == NOT_CROSS)\n r = mid;\n else\n l = mid;\n }\n for (int d = -1; d <= 1; d++) { zs.push_back(l + fluc * d); }\n }\n }\n }\n }\n sort(all(zs));\n\n // INCLUDE,\n // INSCRIBED, // \"Naisetsu\" in Japanese\n // INTERSECT,\n // CIRCUMSCRIBED, // \"Gaisetsu\" in Japanese\n // NOT_CROSS,\n\n auto calc = [&](double z) {\n vector<Circle> cs;\n for (int i = 0; i < N; i++) {\n double zdiff = abs(Z[i] - z);\n if (zdiff > R[i]) continue;\n double r = sqrtl(sq((double)R[i]) - sq(zdiff));\n assert(!isnan(r));\n cs.emplace_back(Point(X[i], Y[i]), r);\n }\n\n UnionFind uf(cs.size());\n for (int i = 0; i < cs.size(); i++) {\n for (int j = i + 1; j < cs.size(); j++) {\n int res = cs[i].is_intersect(cs[j]);\n if (res != NOT_CROSS) { uf.unite(i, j); }\n }\n }\n\n int num_cmp = 0;\n for (int i = 0; i < cs.size(); i++) {\n if (uf.find(i) == i) num_cmp++;\n }\n\n return num_cmp;\n };\n\n vector<int> ans;\n ans.push_back(0);\n for (auto z : zs) {\n ans.push_back(calc(z));\n dmp(z, ans.back());\n }\n ans.push_back(0);\n\n dmp(ans);\n ans.erase(unique(all(ans)), ans.end());\n dmp(ans);\n string str;\n for (int i = 1; i < ans.size(); i++) {\n assert(ans[i - 1] != ans[i]);\n if (ans[i - 1] < ans[i])\n str += \"1\";\n else\n str += \"0\";\n }\n\n cout << str.size() << endl;\n cout << str << endl;\n\n return true;\n}\n\nsigned main() {\n fastio();\n if (!multitest) {\n while (solve()) {}\n } else {\n cerr << \"[Warning] Multi testcase mode on\" << endl;\n int t;\n cin >> t;\n while (t--) solve();\n }\n return 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3596, "score_of_the_acc": -0.0989, "final_rank": 9 }, { "submission_id": "aoj_1255_5384509", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nstruct point{\n double x, y, z;\n point(){\n }\n point(double x, double y, double z): x(x), y(y), z(z){\n }\n point operator +(point P){\n return point(x + P.x, y + P.y, z + P.z);\n }\n point operator -(point P){\n return point(x - P.x, y - P.y, z - P.z);\n }\n point operator *(double k){\n return point(x * k, y * k, z * k);\n }\n point operator /(double k){\n return point(x / k, y / k, z / k);\n }\n};\ndouble abs(point P){\n return sqrt(P.x * P.x + P.y * P.y + P.z * P.z);\n}\ndouble dist(point P, point Q){\n return abs(Q - P);\n}\nint main(){\n while (true){\n int N;\n cin >> N;\n if (N == 0){\n break;\n }\n vector<point> O(N);\n vector<double> R(N);\n for (int i = 0; i < N; i++){\n cin >> O[i].x >> O[i].y >> O[i].z >> R[i];\n }\n vector<point> A = O, B = O;\n for (int i = 0; i < N; i++){\n A[i].z -= R[i];\n B[i].z += R[i];\n }\n vector<double> upd;\n upd.push_back(0);\n upd.push_back(36000);\n for (int i = 0; i < N; i++){\n upd.push_back(O[i].z - R[i]);\n upd.push_back(O[i].z + R[i]);\n for (int j = i + 1; j < N; j++){\n double d = dist(O[i], O[j]);\n if (abs(R[i] - R[j]) < d && d < R[i] + R[j]){\n double m = (R[i] * R[i] + d * d - R[j] * R[j]) / (2 * d);\n double r = sqrt(R[i] * R[i] - m * m);\n point P = O[i] + (O[j] - O[i]) / d * m;\n point Q = O[j] - O[i];\n double a = sqrt(Q.x * Q.x + Q.y * Q.y) / d * r;\n upd.push_back(P.z - a);\n upd.push_back(P.z + a);\n }\n }\n }\n sort(upd.begin(), upd.end());\n int M = upd.size();\n vector<point> O2 = O;\n for (int i = 0; i < N; i++){\n O2[i].z = 0;\n }\n vector<vector<double>> d(N, vector<double>(N));\n for (int i = 0; i < N; i++){\n for (int j = 0; j < N; j++){\n d[i][j] = dist(O2[i], O2[j]);\n }\n }\n vector<int> cc;\n for (int i = 0; i < M - 1; i++){\n double z = (upd[i] + upd[i + 1]) / 2;\n vector<double> r(N, 0);\n for (int j = 0; j < N; j++){\n if (O[j].z - R[j] < z && z < O[j].z + R[j]){\n r[j] = sqrt(R[j] * R[j] - (O[j].z - z) * (O[j].z - z));\n }\n }\n vector<bool> used(N, false);\n int cnt = 0;\n for (int j = 0; j < N; j++){\n if (r[j] > 0 && !used[j]){\n used[j] = true;\n queue<int> Q;\n Q.push(j);\n while (!Q.empty()){\n int v = Q.front();\n Q.pop();\n for (int w = 0; w < N; w++){\n if (!used[w] && r[w] > 0 && d[v][w] < r[v] + r[w]){\n used[w] = true;\n Q.push(w);\n }\n }\n }\n cnt++;\n }\n }\n cc.push_back(cnt);\n }\n string ans;\n for (int i = 0; i < M - 2; i++){\n if (cc[i] > cc[i + 1]){\n ans += '0';\n }\n if (cc[i] < cc[i + 1]){\n ans += '1';\n }\n }\n cout << ans.size() << endl;\n cout << ans << endl;\n }\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3312, "score_of_the_acc": -0.0911, "final_rank": 6 }, { "submission_id": "aoj_1255_5348382", "code_snippet": "#include<iostream>\n#include<string>\n#include<cstdio>\n#include<vector>\n#include<cmath>\n#include<algorithm>\n#include<functional>\n#include<iomanip>\n#include<queue>\n#include<ciso646>\n#include<random>\n#include<map>\n#include<set>\n#include<bitset>\n#include<stack>\n#include<unordered_map>\n#include<unordered_set>\n#include<utility>\n#include<cassert>\n#include<complex>\n#include<numeric>\n#include<array>\nusing namespace std;\n\n//#define int long long\ntypedef long long ll;\n\ntypedef unsigned long long ul;\ntypedef unsigned int ui;\nconstexpr ll mod = 12345678;\nconst ll INF = mod * mod;\ntypedef pair<int, int>P;\n#define stop char nyaa;cin>>nyaa;\n#define rep(i,n) for(int i=0;i<n;i++)\n#define per(i,n) for(int i=n-1;i>=0;i--)\n#define Rep(i,sta,n) for(int i=sta;i<n;i++)\n#define rep1(i,n) for(int i=1;i<=n;i++)\n#define per1(i,n) for(int i=n;i>=1;i--)\n#define Rep1(i,sta,n) for(int i=sta;i<=n;i++)\n#define all(v) (v).begin(),(v).end()\ntypedef pair<ll, ll> LP;\ntypedef long double ld;\ntypedef pair<ld, ld> LDP;\nconst ld eps = 1e-10;\nconst ld pi = acosl(-1.0);\n\nll mod_pow(ll x, ll n, ll m = mod) {\n\tif (n < 0) {\n\t\tll res = mod_pow(x, -n, m);\n\t\treturn mod_pow(res, m - 2, m);\n\t}\n\tif (abs(x) >= m)x %= m;\n\tif (x < 0)x += m;\n\tll res = 1;\n\twhile (n) {\n\t\tif (n & 1)res = res * x % m;\n\t\tx = x * x % m; n >>= 1;\n\t}\n\treturn res;\n}\nstruct modint {\n\tll n;\n\tmodint() :n(0) { ; }\n\tmodint(ll m) :n(m) {\n\t\tif (n >= mod)n %= mod;\n\t\telse if (n < 0)n = (n % mod + mod) % mod;\n\t}\n\toperator int() { return n; }\n};\nbool operator==(modint a, modint b) { return a.n == b.n; }\nmodint operator+=(modint& a, modint b) { a.n += b.n; if (a.n >= mod)a.n -= mod; return a; }\nmodint operator-=(modint& a, modint b) { a.n -= b.n; if (a.n < 0)a.n += mod; return a; }\nmodint operator*=(modint& a, modint b) { a.n = ((ll)a.n * b.n) % mod; return a; }\nmodint operator+(modint a, modint b) { return a += b; }\nmodint operator-(modint a, modint b) { return a -= b; }\nmodint operator*(modint a, modint b) { return a *= b; }\nmodint operator^(modint a, ll n) {\n\tif (n == 0)return modint(1);\n\tmodint res = (a * a) ^ (n / 2);\n\tif (n % 2)res = res * a;\n\treturn res;\n}\n\nll inv(ll a, ll p) {\n\treturn (a == 1 ? 1 : (1 - p * inv(p % a, a)) / a + p);\n}\nmodint operator/(modint a, modint b) { return a * modint(inv(b, mod)); }\nmodint operator/=(modint& a, modint b) { a = a / b; return a; }\nconst int max_n = 1 << 21;\nmodint fact[max_n], factinv[max_n];\nvoid init_f() {\n\tfact[0] = modint(1);\n\tfor (int i = 0; i < max_n - 1; i++) {\n\t\tfact[i + 1] = fact[i] * modint(i + 1);\n\t}\n\tfactinv[max_n - 1] = modint(1) / fact[max_n - 1];\n\tfor (int i = max_n - 2; i >= 0; i--) {\n\t\tfactinv[i] = factinv[i + 1] * modint(i + 1);\n\t}\n}\nmodint comb(int a, int b) {\n\tif (a < 0 || b < 0 || a < b)return 0;\n\treturn fact[a] * factinv[b] * factinv[a - b];\n}\nmodint combP(int a, int b) {\n\tif (a < 0 || b < 0 || a < b)return 0;\n\treturn fact[a] * factinv[a - b];\n}\n\n\ntypedef complex<ld> Point;\nld dot(Point a, Point b) { return real(conj(a) * b); }\nld cross(Point a, Point b) { return imag(conj(a) * b); }\nnamespace std {\n\tbool operator<(const Point& lhs, const Point& rhs) {\n\t\treturn lhs.real() == rhs.real() ? lhs.imag() < rhs.imag() : lhs.real() < rhs.real();\n\t}\n}\nstruct Line {\n\tPoint a, b;\n};\nstruct Circle {\n\tPoint p; ld r;\n};\nint ccw(Point a, Point b, Point c) {\n\tb -= a; c -= a;\n\tif (cross(b, c) > eps)return 1;//counter clockwise\n\tif (cross(b, c) < -eps)return -1;//clock wise\n\tif (dot(b, c) < 0)return 2;//c--a--b on line\n\tif (norm(b) < norm(c))return -2;//a--b--c on line\n\treturn 0; //a--c--b on line\n}\nvector<Point> is_cc(Circle c1, Circle c2) {\n\tvector<Point> res;\n\tld d = abs(c1.p - c2.p);\n\tld rc = (d * d + c1.r * c1.r - c2.r * c2.r) / (2 * d);\n\tld dfr = c1.r * c1.r - rc * rc;\n\tif (abs(dfr) < eps)dfr = 0.0;\n\tif (dfr < 0.0)return res;\n\tld rs = sqrt(dfr);\n\tPoint diff = (c2.p - c1.p) / d;\n\tres.push_back(c1.p + diff * Point(rc, rs));\n\tif (dfr != 0.0)res.push_back(c1.p + diff * Point(rc, -rs));\n\treturn res;\n}\n\nint n;\nvoid solve() {\n\tvector<int> r(n);\n\tvector<int> x(n), y(n), z(n);\n\trep(i, n) {\n\t\tcin >> x[i] >> y[i] >> z[i]>>r[i];\n\t}\n\tvector<ld> vs;\n\trep(i, n) {\n\t\tvs.push_back(z[i] - r[i] + eps);\n\t\tvs.push_back(z[i] + r[i] + eps);\n\t\tRep(j, i + 1, n) {\n\t\t\tld dx = x[j] - x[i];\n\t\t\tld dy = y[j] - y[i];\n\t\t\tld dxy = sqrtl(dx * dx + dy * dy);\n\t\t\tCircle cl = { Point{(ld)0,(ld)z[i]},(ld)r[i] };\n\t\t\tCircle cr = { Point{(ld)dxy,(ld)z[j]},(ld)r[j] };\n\t\t\tvector<Point> vc = is_cc(cl, cr);\n\t\t\tfor (auto p : vc) {\n\t\t\t\tvs.push_back(imag(p) + eps);\n\t\t\t}\n\t\t}\n\t}\n\tsort(all(vs));\n\tint cur = 0;\n\tstring ans;\n\tvector<vector<ld>> dist(n, vector<ld>(n));\n\trep(i, n)rep(j, n) {\n\t\tld dx = x[j] - x[i];\n\t\tld dy = y[j] - y[i];\n\t\tdist[i][j] = sqrtl(dx * dx + dy * dy);\n\t}\n\tfor(auto val:vs){\n\t\tvector<vector<int>> G(n);\n\t\tvector<ld> rz(n);\n\t\trep(i, n) {\n\t\t\tld dz = abs(z[i] - val);\n\t\t\tif (dz <= r[i]) {\n\t\t\t\trz[i] = sqrtl(r[i] * r[i] - dz * dz);\n\t\t\t}\n\t\t\telse {\n\t\t\t\trz[i] = -1;\n\t\t\t}\n\t\t}\n\t\trep(i, n)Rep(j, i + 1, n) {\n\t\t\tif (rz[i] >= 0 && rz[j] >= 0) {\n\t\t\t\tif (dist[i][j] <= rz[i] + rz[j]) {\n\t\t\t\t\tG[i].push_back(j);\n\t\t\t\t\tG[j].push_back(i);\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\tvector<bool> used(n);\n\t\tint cnt = 0;\n\t\trep(i, n) {\n\t\t\tif (rz[i] < 0)continue;\n\t\t\tif(used[i])continue;\n\t\t\tcnt++;\n\t\t\tqueue<int> q; q.push(i);\n\t\t\twhile (!q.empty()) {\n\t\t\t\tint v = q.front(); q.pop();\n\t\t\t\tfor (int to : G[v])if (!used[to]) {\n\t\t\t\t\tused[to] = true; q.push(to);\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\tif (cur < cnt)ans.push_back('1');\n\t\telse if (cur > cnt)ans.push_back('0');\n\t\tcur = cnt;\n\t\t//cout << \"??? \" << cnt << \"\\n\";\n\t}\n\tcout << ans.size() << \"\\n\";\n\tcout << ans << \"\\n\";\n}\n\nsigned main() {\n\tios::sync_with_stdio(false);\n\tcin.tie(0);\n\t//cout << fixed << setprecision(10);\n\t//init_f();\n\t//init();\n\t//expr();\n\n\t//int t; cin >> t; rep(i, t)\n\twhile (cin >> n, n)solve();\n\treturn 0;\n}", "accuracy": 1, "time_ms": 40, "memory_kb": 36372, "score_of_the_acc": -1.0423, "final_rank": 17 }, { "submission_id": "aoj_1255_4876382", "code_snippet": "#include<iostream>\n#include<cstdio>\n#include<cstring>\n#include<string>\n#include<vector>\n#include<cmath>\n#include<algorithm>\n#include<map>\n#include<queue>\n#include<deque>\n#include<iomanip>\n#include<tuple>\n#include<cassert>\n#include<set>\n#include<complex>\nusing namespace std;\ntypedef long long int LL;\ntypedef pair<int,int> P;\ntypedef pair<LL,int> LP;\nconst int INF=1<<30;\nconst LL MAX=1e9+7;\n\nvoid array_show(int *array,int array_n,char middle=' '){\n for(int i=0;i<array_n;i++)printf(\"%d%c\",array[i],(i!=array_n-1?middle:'\\n'));\n}\nvoid array_show(LL *array,int array_n,char middle=' '){\n for(int i=0;i<array_n;i++)printf(\"%lld%c\",array[i],(i!=array_n-1?middle:'\\n'));\n}\nvoid array_show(vector<int> &vec_s,int vec_n=-1,char middle=' '){\n if(vec_n==-1)vec_n=vec_s.size();\n for(int i=0;i<vec_n;i++)printf(\"%d%c\",vec_s[i],(i!=vec_n-1?middle:'\\n'));\n}\nvoid array_show(vector<LL> &vec_s,int vec_n=-1,char middle=' '){\n if(vec_n==-1)vec_n=vec_s.size();\n for(int i=0;i<vec_n;i++)printf(\"%lld%c\",vec_s[i],(i!=vec_n-1?middle:'\\n'));\n}\n\nvoid init(){\n \n}\n\nclass Circle{\n typedef long double type;\n const type eps=1e-9;\npublic:\n complex<type> point;\n type r;\n\n Circle(type pa,type pb,type _r):point(pa,pb),r(_r){}\n\n vector<complex<type>> colision_point(Circle cir){\n complex<type> point2=cir.point;\n type r2=cir.r;\n vector<complex<type>> vs;\n if(abs(point2-point)>r+r2+eps || abs(point2-point)<abs(r-r2)-eps)return vs;\n complex<type> ca,cb;\n type da,db;\n ca=point2-point;\n da=r/abs(ca);\n ca*=da;\n if(abs(point2-point)>r+r2-eps || abs(point2-point)<abs(r-r2)+eps){\n vs.push_back(point+ca);\n return vs;\n }\n db=abs(point2-point);//dis\n da=(db*db+r*r-r2*r2)/(2*db*r);//cos\n cb.real(da),cb.imag(sqrt(1-da*da));\n vs.push_back(point+ca*cb);\n vs.push_back(point+ca/cb);\n return vs;\n }\n};\n\nclass union_find_tree{\nprivate:\n static constexpr int uft_N=100005;\n int uft_n;\n queue<int> uft_q1;\n vector<int> uft_parent;\n vector<int> uft_num;\npublic:\n void init(){\n uft_parent.assign(uft_n,-1);\n uft_num.assign(uft_n,1);\n }\n union_find_tree(int uft_n_init){\n assert(uft_n_init>=0);\n uft_n=uft_n_init;\n init();\n }\n union_find_tree(){\n uft_n=uft_N;\n init();\n }\n\n int check_parent(int uft_x){\n assert(uft_x>=0 && uft_x<uft_n);\n if(uft_parent[uft_x]!=-1){\n uft_q1.push(uft_x);\n return check_parent(uft_parent[uft_x]);\n }\n int uft_a;\n while(!uft_q1.empty()){\n uft_a=uft_q1.front(),uft_q1.pop();\n uft_parent[uft_a]=uft_x;\n }\n return uft_x;\n }\n\n bool connect(int uft_x,int uft_y){\n assert(uft_x>=0 && uft_x<uft_n);\n assert(uft_y>=0 && uft_y<uft_n);\n uft_x=check_parent(uft_x),uft_y=check_parent(uft_y);\n if(uft_x==uft_y)return true;\n if(uft_num[uft_x]>uft_num[uft_y])swap(uft_x,uft_y);\n uft_parent[uft_x]=uft_y;\n uft_num[uft_y]+=uft_num[uft_x];\n return false;\n }\n\n int size(int pos){\n pos=check_parent(pos);\n return uft_num[pos];\n }\n};\n\nlong double t[200][4];\n\nint main(){\n int n,m;\n int i,j,k;\n int a,b,c;\n while(cin>>n){\n if(n==0)break;\n for(i=0;i<n;i++){\n for(j=0;j<4;j++){\n cin>>t[i][j];\n }\n }\n vector<long double> v1;\n long double da,db;\n const long double eps=1e-9;\n for(i=0;i<n;i++){\n v1.push_back(t[i][2]-t[i][3]);\n v1.push_back(t[i][2]+t[i][3]);\n for(j=i+1;j<n;j++){\n da=t[j][0]-t[i][0],db=t[j][1]-t[i][1];\n Circle ca(0,t[i][2],t[i][3]);\n Circle cb(sqrt(da*da+db*db),t[j][2],t[j][3]);\n vector<complex<long double>> vs=ca.colision_point(cb);\n for(auto pt:vs){\n v1.push_back(pt.imag());\n }\n }\n }\n int bef=0;\n string ss;\n sort(v1.begin(),v1.end());\n for(auto z:v1){\n union_find_tree ua(n);\n z+=eps;\n for(i=0;i<n;i++){\n if(z<t[i][2]-t[i][3] || t[i][2]+t[i][3]<z)continue;\n for(j=i+1;j<n;j++){\n if(z<t[j][2]-t[j][3] || t[j][2]+t[j][3]<z)continue;\n da=pow(t[i][0]-t[j][0],2)+pow(t[i][1]-t[j][1],2);\n db=sqrt(pow(t[i][3],2)-pow(t[i][2]-z,2))+sqrt(pow(t[j][3],2)-pow(t[j][2]-z,2));\n if(da>pow(db,2))continue;\n ua.connect(i,j);\n }\n }\n a=0;\n for(i=0;i<n;i++){\n if(z<t[i][2]-t[i][3] || t[i][2]+t[i][3]<z)continue;\n if(ua.check_parent(i)==i)a++;\n }\n if(a==bef)continue;\n if(a>bef)ss.push_back('1');\n else ss.push_back('0');\n bef=a;\n }\n cout<<ss.size()<<endl;\n cout<<ss<<endl;\n }\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3208, "score_of_the_acc": -0.0882, "final_rank": 5 }, { "submission_id": "aoj_1255_4634542", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\ntypedef long long ll;\ntypedef pair<ll, ll> l_l;\ntypedef pair<int, int> i_i;\ntemplate<class T>\ninline bool chmax(T &a, T b) {\n if(a < b) {\n a = b;\n return true;\n }\n return false;\n}\n\ntemplate<class T>\ninline bool chmin(T &a, T b) {\n if(a > b) {\n a = b;\n return true;\n }\n return false;\n}\n\n//const long double EPS = 1e-10;\n//const long long INF = 1e18;\n//const long double PI = acos(-1.0L);\n//const ll mod = 1000000007;\nusing Real = double;\nusing Point = complex< Real >;\nconst Real EPS = 1e-8, PI = acos(-1);\n\ninline bool eq(Real a, Real b) { return fabs(b - a) < EPS; }\n\nPoint operator*(const Point &p, const Real &d) {\n return Point(real(p) * d, imag(p) * d);\n}\n\nistream &operator>>(istream &is, Point &p) {\n Real a, b;\n is >> a >> b;\n p = Point(a, b);\n return is;\n}\n\nostream &operator<<(ostream &os, Point &p) {\n os << fixed << setprecision(10) << p.real() << \" \" << p.imag();\n}\n\nPoint rotate(Real theta, const Point &p) {\n return Point(cos(theta) * p.real() - sin(theta) * p.imag(), sin(theta) * p.real() + cos(theta) * p.imag());\n}\n\nReal radian_to_degree(Real r) {\n return (r * 180.0 / PI);\n}\n\nReal degree_to_radian(Real d) {\n return (d * PI / 180.0);\n}\n\nReal get_angle(const Point &a, const Point &b, const Point &c) {\n const Point v(b - a), w(c - b);\n Real alpha = atan2(v.imag(), v.real()), beta = atan2(w.imag(), w.real());\n if(alpha > beta) swap(alpha, beta);\n Real theta = (beta - alpha);\n return min(theta, 2 * acos(-1) - theta);\n}\n\nnamespace std {\n bool operator<(const Point &a, const Point &b) {\n return a.real() != b.real() ? a.real() < b.real() : a.imag() < b.imag();\n }\n}\n\n\nstruct Line {\n Point a, b;\n\n Line() = default;\n\n Line(Point a, Point b) : a(a), b(b) {}\n\n Line(Real A, Real B, Real C) // Ax + By = C\n {\n if(eq(A, 0)) a = Point(0, C / B), b = Point(1, C / B);\n else if(eq(B, 0)) b = Point(C / A, 0), b = Point(C / A, 1);\n else a = Point(0, C / B), b = Point(C / A, 0);\n }\n\n friend ostream &operator<<(ostream &os, Line &p) {\n return os << p.a << \" to \" << p.b;\n }\n\n friend istream &operator>>(istream &is, Line &a) {\n return is >> a.a >> a.b;\n }\n};\n\nstruct Segment : Line {\n Segment() = default;\n\n Segment(Point a, Point b) : Line(a, b) {}\n};\n\nstruct Circle {\n Point p;\n Real r;\n\n Circle() = default;\n\n Circle(Point p, Real r) : p(p), r(r) {}\n};\n\nusing Points = vector< Point >;\nusing Polygon = vector< Point >;\nusing Segments = vector< Segment >;\nusing Lines = vector< Line >;\nusing Circles = vector< Circle >;\n\nReal cross(const Point &a, const Point &b) {\n return real(a) * imag(b) - imag(a) * real(b);\n}\n\nReal dot(const Point &a, const Point &b) {\n return real(a) * real(b) + imag(a) * imag(b);\n}\n\n// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_1_C\nint ccw(const Point &a, Point b, Point c) {\n b = b - a, c = c - a;\n if(cross(b, c) > EPS) return +1; // \"COUNTER_CLOCKWISE\"\n if(cross(b, c) < -EPS) return -1; // \"CLOCKWISE\"\n if(dot(b, c) < 0) return +2; // \"ONLINE_BACK\"\n if(norm(b) < norm(c)) return -2; // \"ONLINE_FRONT\"\n return 0; // \"ON_SEGMENT\"\n}\n\n// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_2_A\nbool parallel(const Line &a, const Line &b) {\n return eq(cross(a.b - a.a, b.b - b.a), 0.0);\n}\n\n// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_2_A\nbool orthogonal(const Line &a, const Line &b) {\n return eq(dot(a.a - a.b, b.a - b.b), 0.0);\n}\n\n// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_1_A\nPoint projection(const Line &l, const Point &p) {\n double t = dot(p - l.a, l.a - l.b) / norm(l.a - l.b);\n return l.a + (l.a - l.b) * t;\n}\n\nPoint projection(const Segment &l, const Point &p) {\n double t = dot(p - l.a, l.a - l.b) / norm(l.a - l.b);\n return l.a + (l.a - l.b) * t;\n}\n\n// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_1_B\nPoint reflection(const Line &l, const Point &p) {\n return p + (projection(l, p) - p) * 2.0;\n}\n\nbool intersect(const Line &l, const Point &p) {\n return abs(ccw(l.a, l.b, p)) != 1;\n}\n\nbool intersect(const Line &l, const Line &m) {\n return abs(cross(l.b - l.a, m.b - m.a)) > EPS || abs(cross(l.b - l.a, m.b - l.a)) < EPS;\n}\n\nbool intersect(const Segment &s, const Point &p) {\n return ccw(s.a, s.b, p) == 0;\n}\n\nbool intersect(const Line &l, const Segment &s) {\n return cross(l.b - l.a, s.a - l.a) * cross(l.b - l.a, s.b - l.a) < EPS;\n}\n\nReal distance(const Line &l, const Point &p);\n\nbool intersect(const Circle &c, const Line &l) {\n return distance(l, c.p) <= c.r + EPS;\n}\n\nbool intersect(const Circle &c, const Point &p) {\n return abs(abs(p - c.p) - c.r) < EPS;\n}\n\n// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_2_B\nbool intersect(const Segment &s, const Segment &t) {\n 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;\n}\n\nint intersect(const Circle &c, const Segment &l) {\n if(norm(projection(l, c.p) - c.p) - c.r * c.r > EPS) return 0;\n auto d1 = abs(c.p - l.a), d2 = abs(c.p - l.b);\n if(d1 < c.r + EPS && d2 < c.r + EPS) return 0;\n if(d1 < c.r - EPS && d2 > c.r + EPS || d1 > c.r + EPS && d2 < c.r - EPS) return 1;\n const Point h = projection(l, c.p);\n if(dot(l.a - h, l.b - h) < 0) return 2;\n return 0;\n}\n\n// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_7_A&lang=jp\nint intersect(Circle c1, Circle c2) {\n if(c1.r < c2.r) swap(c1, c2);\n Real d = abs(c1.p - c2.p);\n if(c1.r + c2.r < d) return 4;\n if(eq(c1.r + c2.r, d)) return 3;\n if(c1.r - c2.r < d) return 2;\n if(eq(c1.r - c2.r, d)) return 1;\n return 0;\n}\n\nReal distance(const Point &a, const Point &b) {\n return abs(a - b);\n}\n\nReal distance(const Line &l, const Point &p) {\n return abs(p - projection(l, p));\n}\n\nReal distance(const Line &l, const Line &m) {\n return intersect(l, m) ? 0 : distance(l, m.a);\n}\n\nReal distance(const Segment &s, const Point &p) {\n Point r = projection(s, p);\n if(intersect(s, r)) return abs(r - p);\n return min(abs(s.a - p), abs(s.b - p));\n}\n\n// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_2_D\nReal distance(const Segment &a, const Segment &b) {\n if(intersect(a, b)) return 0;\n return min({distance(a, b.a), distance(a, b.b), distance(b, a.a), distance(b, a.b)});\n}\n\nReal distance(const Line &l, const Segment &s) {\n if(intersect(l, s)) return 0;\n return min(distance(l, s.a), distance(l, s.b));\n}\n\nPoint crosspoint(const Line &l, const Line &m) {\n Real A = cross(l.b - l.a, m.b - m.a);\n Real B = cross(l.b - l.a, l.b - m.a);\n if(eq(abs(A), 0.0) && eq(abs(B), 0.0)) return m.a;\n return m.a + (m.b - m.a) * B / A;\n}\n\n// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_2_C\nPoint crosspoint(const Segment &l, const Segment &m) {\n return crosspoint(Line(l), Line(m));\n}\n\n// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_7_D\npair< Point, Point > crosspoint(const Circle &c, const Line l) {\n Point pr = projection(l, c.p);\n Point e = (l.b - l.a) / abs(l.b - l.a);\n if(eq(distance(l, c.p), c.r)) return {pr, pr};\n double base = sqrt(c.r * c.r - norm(pr - c.p));\n return {pr - e * base, pr + e * base};\n}\n\npair< Point, Point > crosspoint(const Circle &c, const Segment &l) {\n Line aa = Line(l.a, l.b);\n if(intersect(c, l) == 2) return crosspoint(c, aa);\n auto ret = crosspoint(c, aa);\n if(dot(l.a - ret.first, l.b - ret.first) < 0) ret.second = ret.first;\n else ret.first = ret.second;\n return ret;\n}\n\n// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_7_E\npair< Point, Point > crosspoint(const Circle &c1, const Circle &c2) {\n Real d = abs(c1.p - c2.p);\n Real a = acos((c1.r * c1.r + d * d - c2.r * c2.r) / (2 * c1.r * d));\n Real t = atan2(c2.p.imag() - c1.p.imag(), c2.p.real() - c1.p.real());\n Point p1 = c1.p + Point(cos(t + a) * c1.r, sin(t + a) * c1.r);\n Point p2 = c1.p + Point(cos(t - a) * c1.r, sin(t - a) * c1.r);\n return {p1, p2};\n}\n\n// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_7_F\npair< Point, Point > tangent(const Circle &c1, const Point &p2) {\n return crosspoint(c1, Circle(p2, sqrt(norm(c1.p - p2) - c1.r * c1.r)));\n}\n\n// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_7_G\nLines tangent(Circle c1, Circle c2) {\n Lines ret;\n if(c1.r < c2.r) swap(c1, c2);\n Real g = norm(c1.p - c2.p);\n if(eq(g, 0)) return ret;\n Point u = (c2.p - c1.p) / sqrt(g);\n Point v = rotate(PI * 0.5, u);\n for(int s : {-1, 1}) {\n Real h = (c1.r + s * c2.r) / sqrt(g);\n if(eq(1 - h * h, 0)) {\n ret.emplace_back(c1.p + u * c1.r, c1.p + (u + v) * c1.r);\n } else if(1 - h * h > 0) {\n Point uu = u * h, vv = v * sqrt(1 - h * h);\n ret.emplace_back(c1.p + (uu + vv) * c1.r, c2.p - (uu + vv) * c2.r * s);\n ret.emplace_back(c1.p + (uu - vv) * c1.r, c2.p - (uu - vv) * c2.r * s);\n }\n }\n return ret;\n}\n\n// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_3_B\nbool is_convex(const Polygon &p) {\n int n = (int) p.size();\n for(int i = 0; i < n; i++) {\n if(ccw(p[(i + n - 1) % n], p[i], p[(i + 1) % n]) == -1) return false;\n }\n return true;\n}\n\n// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_4_A\nPolygon convex_hull(Polygon &p) {\n int n = (int) p.size(), k = 0;\n if(n <= 2) return p;\n sort(p.begin(), p.end());\n vector< Point > ch(2 * n);\n for(int i = 0; i < n; ch[k++] = p[i++]) {\n while(k >= 2 && cross(ch[k - 1] - ch[k - 2], p[i] - ch[k - 1]) < 0) --k;\n }\n for(int i = n - 2, t = k + 1; i >= 0; ch[k++] = p[i--]) {\n while(k >= t && cross(ch[k - 1] - ch[k - 2], p[i] - ch[k - 1]) < 0) --k;\n }\n ch.resize(k - 1);\n return ch;\n}\n\n// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_3_C\nenum {\n OUT, ON, IN\n};\nint contains(const Polygon &Q, const Point &p) {\n bool in = false;\n for(int i = 0; i < Q.size(); i++) {\n Point a = Q[i] - p, b = Q[(i + 1) % Q.size()] - p;\n if(a.imag() > b.imag()) swap(a, b);\n if(a.imag() <= 0 && 0 < b.imag() && cross(a, b) < 0) in = !in;\n if(cross(a, b) == 0 && dot(a, b) <= 0) return ON;\n }\n return in ? IN : OUT;\n}\n\n\n// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=1033\nvoid merge_segments(vector< Segment > &segs) {\n\n auto merge_if_able = [](Segment &s1, const Segment &s2) {\n if(abs(cross(s1.b - s1.a, s2.b - s2.a)) > EPS) return false;\n if(ccw(s1.a, s2.a, s1.b) == 1 || ccw(s1.a, s2.a, s1.b) == -1) return false;\n if(ccw(s1.a, s1.b, s2.a) == -2 || ccw(s2.a, s2.b, s1.a) == -2) return false;\n s1 = Segment(min(s1.a, s2.a), max(s1.b, s2.b));\n return true;\n };\n\n for(int i = 0; i < segs.size(); i++) {\n if(segs[i].b < segs[i].a) swap(segs[i].a, segs[i].b);\n }\n for(int i = 0; i < segs.size(); i++) {\n for(int j = i + 1; j < segs.size(); j++) {\n if(merge_if_able(segs[i], segs[j])) {\n segs[j--] = segs.back(), segs.pop_back();\n }\n }\n }\n}\n\n// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=1033\nvector< vector< int > > segment_arrangement(vector< Segment > &segs, vector< Point > &ps) {\n vector< vector< int > > g;\n int N = (int) segs.size();\n for(int i = 0; i < N; i++) {\n ps.emplace_back(segs[i].a);\n ps.emplace_back(segs[i].b);\n for(int j = i + 1; j < N; j++) {\n const Point p1 = segs[i].b - segs[i].a;\n const Point p2 = segs[j].b - segs[j].a;\n if(cross(p1, p2) == 0) continue;\n if(intersect(segs[i], segs[j])) {\n ps.emplace_back(crosspoint(segs[i], segs[j]));\n }\n }\n }\n sort(begin(ps), end(ps));\n ps.erase(unique(begin(ps), end(ps)), end(ps));\n\n int M = (int) ps.size();\n g.resize(M);\n for(int i = 0; i < N; i++) {\n vector< int > vec;\n for(int j = 0; j < M; j++) {\n if(intersect(segs[i], ps[j])) {\n vec.emplace_back(j);\n }\n }\n for(int j = 1; j < vec.size(); j++) {\n g[vec[j - 1]].push_back(vec[j]);\n g[vec[j]].push_back(vec[j - 1]);\n }\n }\n return (g);\n}\n\n// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_4_C\nPolygon convex_cut(const Polygon &U, Line l) {\n Polygon ret;\n for(int i = 0; i < U.size(); i++) {\n Point now = U[i], nxt = U[(i + 1) % U.size()];\n if(ccw(l.a, l.b, now) != -1) ret.push_back(now);\n if(ccw(l.a, l.b, now) * ccw(l.a, l.b, nxt) < 0) {\n ret.push_back(crosspoint(Line(now, nxt), l));\n }\n }\n return (ret);\n}\n\n// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_3_A\nReal area2(const Polygon &p) {\n Real A = 0;\n for(int i = 0; i < p.size(); ++i) {\n A += cross(p[i], p[(i + 1) % p.size()]);\n }\n return A;\n}\n\n// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_7_H\nReal area2(const Polygon &p, const Circle &c) {\n if(p.size() < 3) return 0.0;\n function< Real(Circle, Point, Point) > cross_area = [&](const Circle &c, const Point &a, const Point &b) {\n Point va = c.p - a, vb = c.p - b;\n Real f = cross(va, vb), ret = 0.0;\n if(eq(f, 0.0)) return ret;\n if(max(abs(va), abs(vb)) < c.r + EPS) return f;\n if(distance(Segment(a, b), c.p) > c.r - EPS) return c.r * c.r * arg(vb * conj(va));\n auto u = crosspoint(c, Segment(a, b));\n vector< Point > tot{a, u.first, u.second, b};\n for(int i = 0; i + 1 < tot.size(); i++) {\n ret += cross_area(c, tot[i], tot[i + 1]);\n }\n return ret;\n };\n Real A = 0;\n for(int i = 0; i < p.size(); i++) {\n A += cross_area(c, p[i], p[(i + 1) % p.size()]);\n }\n return A;\n}\n\n// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_4_B\nReal convex_diameter(const Polygon &p) {\n int N = (int) p.size();\n int is = 0, js = 0;\n for(int i = 1; i < N; i++) {\n if(p[i].imag() > p[is].imag()) is = i;\n if(p[i].imag() < p[js].imag()) js = i;\n }\n Real maxdis = norm(p[is] - p[js]);\n\n int maxi, maxj, i, j;\n i = maxi = is;\n j = maxj = js;\n do {\n if(cross(p[(i + 1) % N] - p[i], p[(j + 1) % N] - p[j]) >= 0) {\n j = (j + 1) % N;\n } else {\n i = (i + 1) % N;\n }\n if(norm(p[i] - p[j]) > maxdis) {\n maxdis = norm(p[i] - p[j]);\n maxi = i;\n maxj = j;\n }\n } while(i != is || j != js);\n return sqrt(maxdis);\n}\n\n// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_5_A\nReal closest_pair(Points ps) {\n if(ps.size() <= 1) throw (0);\n sort(begin(ps), end(ps));\n\n auto compare_y = [&](const Point &a, const Point &b) {\n return imag(a) < imag(b);\n };\n vector< Point > beet(ps.size());\n const Real INF = 1e18;\n\n function< Real(int, int) > rec = [&](int left, int right) {\n if(right - left <= 1) return INF;\n int mid = (left + right) >> 1;\n auto x = real(ps[mid]);\n auto ret = min(rec(left, mid), rec(mid, right));\n inplace_merge(begin(ps) + left, begin(ps) + mid, begin(ps) + right, compare_y);\n int ptr = 0;\n for(int i = left; i < right; i++) {\n if(abs(real(ps[i]) - x) >= ret) continue;\n for(int j = 0; j < ptr; j++) {\n auto luz = ps[i] - beet[ptr - j - 1];\n if(imag(luz) >= ret) break;\n ret = min(ret, abs(luz));\n }\n beet[ptr++] = ps[i];\n }\n return ret;\n };\n return rec(0, (int) ps.size());\n}\n\nint N;\nint x[105], y[105], z[105], r[105];\nstruct UnionFind {\n vector<int> par;\n vector<int> rank;\n vector<ll> Size;\n UnionFind(int n = 1) {\n init(n);\n }\n\n void init(int n = 1) {\n par.resize(n + 1); rank.resize(n + 1); Size.resize(n + 1);\n for (int i = 0; i <= n; ++i) par[i] = i, rank[i] = 0, Size[i] = 1;\n }\n\n int root(int x) {\n if (par[x] == x) {\n return x;\n }\n else {\n int r = root(par[x]);\n return par[x] = r;\n }\n }\n\n bool issame(int x, int y) {\n return root(x) == root(y);\n }\n\n bool merge(int x, int y) {\n x = root(x); y = root(y);\n if (x == y) return false;\n if (rank[x] < rank[y]) swap(x, y);\n if (rank[x] == rank[y]) ++rank[x];\n par[y] = x;\n Size[x] += Size[y];\n return true;\n }\n\n ll size(int x){\n return Size[root(x)];\n }\n};\n\nUnionFind uni;\n\nint f(double Z) {\n Circles C;\n for(int i = 0; i < N; i++) {\n double dz = Z - z[i];\n if(r[i] * r[i] - dz * dz < 0) continue;\n double R = pow(r[i]*r[i]-dz*dz, 0.5);\n C.push_back(Circle(Point(x[i], y[i]), R));\n }\n int n = C.size();\n uni.init(n);\n int ret = n;\n for(int i = 0; i < n; i++) {\n for(int j = 0; j < n; j++) {\n if(uni.issame(i, j)) continue;\n if(intersect(C[i], C[j]) == 4) continue;\n uni.merge(i, j);\n ret--;\n }\n }\n return ret;\n}\n\nvoid solve() {\n for(int i = 0; i < N; i++) {\n cin >> x[i] >> y[i] >> z[i] >> r[i];\n }\n vector<double> Z;\n for(int i = 0; i < N; i++) {\n Z.push_back(z[i] + r[i]);\n Z.push_back(z[i] - r[i]);\n for(int j = 0; j < i; j++) {\n double dx = x[i] - x[j];\n double dy = y[i] - y[j];\n double delta = pow(dx*dx+dy*dy, 0.5);\n Circle C1(Point(0, z[i]), r[i]);\n Circle C2(Point(delta, z[j]), r[j]);\n if(intersect(C1, C2) != 2) continue;\n auto tmp = crosspoint(C1, C2);\n Z.push_back(tmp.first.imag());\n Z.push_back(tmp.second.imag());\n }\n }\n Z.push_back(-1);\n Z.push_back(40000);\n sort(Z.begin(), Z.end());\n vector<double> Z2;\n for(int i = 0; i < Z.size(); i++) {\n if(i > 0) {\n Z2.push_back((Z[i] + Z[i-1]) / 2);\n }\n Z2.push_back(Z[i]);\n }\n Z2.erase(unique(Z2.begin(), Z2.end()), Z2.end());\n swap(Z2, Z);\n /*\n for(auto tmp : Z) {\n cerr << tmp << \" \";\n }\n cerr << endl;\n */\n vector<int> v;\n for(int i = 0; i < Z.size(); i++) {\n v.push_back(f(Z[i]));\n //cerr << Z[i] << \" \" << v.back() << endl;\n }\n v.erase(unique(v.begin(), v.end()), v.end());\n string S;\n for(int i = 1; i < v.size(); i++) {\n if(v[i] > v[i-1]) S.push_back('1');\n else S.push_back('0');\n }\n cout << S.size() << endl;\n cout << S << endl;\n}\n\nint main() {\n //cout.precision(10);\n while(cin >> N) {\n if(N == 0) break;\n solve();\n }\n return 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3500, "score_of_the_acc": -0.0962, "final_rank": 8 }, { "submission_id": "aoj_1255_3650227", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\nusing ld = long double;\nusing point2d = complex<ld>;\nusing point3d = array<ld, 3>;\n\nconstexpr ld eps = 1e-8;\n\nld abs(point3d const& p) {\n return sqrt(p[0] * p[0] + p[1] * p[1] + p[2] * p[2]);\n}\n\npoint3d operator-(point3d a, point3d const& b) {\n for(int i = 0; i < 3; ++i) a[i] -= b[i];\n return a;\n}\n\nclass union_find {\npublic:\n union_find(int n) : par(n, -1) {}\n\n int root(int x) {\n return par[x] < 0 ? x : par[x] = root(par[x]);\n }\n\n void unite(int x, int y) {\n x = root(x), y = root(y);\n if(x == y) return;\n if(par[x] < par[y]) swap(x, y);\n par[x] += par[y];\n par[y] = x;\n }\n\nprivate:\n vector<int> par;\n};\n\nint main() {\n int n;\n while(cin >> n, n) {\n vector<point3d> p(n);\n vector<ld> r(n);\n for(int i = 0; i < n; ++i) {\n ld x, y, z;\n cin >> x >> y >> z >> r[i];\n p[i] = {{x, y, z}};\n }\n\n auto contains = [&] (int i, int j) { // j contains i ?\n if(r[i] > r[j]) return false;\n // j is bigger than i\n return abs(p[i] - p[j]) + r[i] <= r[j];\n };\n vector<bool> ban(n);\n for(int i = 0; i < n; ++i) {\n for(int j = 0; j < n; ++j) {\n if(i == j) continue;\n if(abs(p[i] - p[j]) < eps) { // center is same\n if(r[i] == r[j]) { // same sphere\n ban[i] = ban[i] || i > j;\n } else {\n ban[i] = ban[i] || contains(i, j);\n }\n } else {\n ban[i] = ban[i] || contains(i, j);\n }\n }\n }\n\n vector<ld> zs;\n for(int i = 0; i < n; ++i) {\n if(ban[i]) continue;\n zs.push_back(p[i][2] - r[i]);\n zs.push_back(p[i][2] + r[i]);\n for(int j = i + 1; j < n; ++j) {\n if(ban[j]) continue;\n const ld d = abs(p[i] - p[j]);\n if(d >= r[i] + r[j]) continue;\n const auto v1 = point2d(p[j][0], p[j][1]) - point2d(p[i][0], p[i][1]);\n const ld phi = arg(v1);\n const auto pj_x = real(point2d(p[i][0], p[i][1]) + v1 * exp(point2d(0, -phi)));\n const ld d1 = (r[i] * r[i] - r[j] * r[j] + d * d) / 2 / d;\n const ld theta = acos(d1 / r[i]);\n const auto v2 = point2d(pj_x, p[j][2]) - point2d(p[i][0], p[i][2]);\n zs.push_back(imag(point2d(p[i][0], p[i][2]) + v2 / abs(v2) * exp(point2d(0, -theta)) * r[i]));\n zs.push_back(imag(point2d(p[i][0], p[i][2]) + v2 / abs(v2) * exp(point2d(0, theta)) * r[i]));\n }\n }\n sort(begin(zs), end(zs));\n {\n vector<ld> zzs;\n for(auto z : zs) {\n if(zzs.empty() || (z - zzs.back()) > eps) {\n zzs.push_back(z);\n }\n }\n zs = move(zzs);\n }\n\n string ans;\n int prev = 0;\n for(int i = 0; i < (int)zs.size() - 1; ++i) {\n const ld z = (zs[i + 1] + zs[i]) / 2;\n vector<ld> r2(n);\n for(int j = 0; j < n; ++j) {\n if(r[j] - abs(p[j][2] - z) < eps) continue;\n r2[j] = sqrt(r[j] * r[j] - abs(p[j][2] - z) * abs(p[j][2] - z));\n }\n union_find uf(n);\n for(int j = 0; j < n; ++j) {\n if(r2[j] == 0) continue;\n for(int k = j + 1; k < n; ++k) {\n if(r2[k] == 0) continue;\n if(abs(point2d(p[j][0], p[j][1]) - point2d(p[k][0], p[k][1])) < r2[j] + r2[k]) {\n uf.unite(j, k);\n }\n }\n }\n set<int> s;\n for(int j = 0; j < n; ++j) {\n if(r2[j] == 0) continue;\n s.insert(uf.root(j));\n }\n if(prev != (int)s.size()) {\n ans += (prev < (int)s.size() ? '1' : '0');\n }\n prev = s.size();\n }\n ans += '0';\n\n cout << ans.size() << endl;\n cout << ans << endl;\n }\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3448, "score_of_the_acc": -0.0948, "final_rank": 7 }, { "submission_id": "aoj_1255_3372891", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\nusing Int = long long;\ntemplate<typename T1,typename T2> inline void chmin(T1 &a,T2 b){if(a>b) a=b;}\ntemplate<typename T1,typename T2> inline void chmax(T1 &a,T2 b){if(a<b) a=b;}\n\n#define EPS (1e-8)\n#define equals(a,b) (fabs((a)-(b)) < EPS)\n#define PI 3.141592653589793238\n \n// COUNTER CLOCKWISE\nstatic const int CCW_COUNTER_CLOCKWISE = 1;\nstatic const int CCW_CLOCKWISE = -1;\nstatic const int CCW_ONLINE_BACK = 2;\nstatic const int CCW_ONLINE_FRONT = -2;\nstatic const int CCW_ON_SEGMENT = 0;\n\n//Intercsect Circle & Circle\nstatic const int ICC_SEPERATE = 4;\nstatic const int ICC_CIRCUMSCRIBE = 3;\nstatic const int ICC_INTERSECT = 2;\nstatic const int ICC_INSCRIBE = 1;\nstatic const int ICC_CONTAIN = 0;\n\nstruct Point{\n double x,y;\n Point(){}\n Point(double x,double y) :x(x),y(y){}\n Point operator+(Point p) {return Point(x+p.x,y+p.y);}\n Point operator-(Point p) {return Point(x-p.x,y-p.y);}\n Point operator*(double k){return Point(x*k,y*k);}\n Point operator/(double k){return Point(x/k,y/k);}\n double norm(){return x*x+y*y;}\n double abs(){return sqrt(norm());}\n\n bool operator < (const Point &p) const{\n return x!=p.x?x<p.x:y<p.y;\n //grid-point only\n //return !equals(x,p.x)?x<p.x:!equals(y,p.y)?y<p.y:0;\n }\n\n bool operator == (const Point &p) const{\n return fabs(x-p.x)<EPS && fabs(y-p.y)<EPS;\n }\n};\n\nstruct EndPoint{\n Point p;\n int seg,st;\n EndPoint(){}\n EndPoint(Point p,int seg,int st):p(p),seg(seg),st(st){}\n bool operator<(const EndPoint &ep)const{\n if(p.y==ep.p.y) return st<ep.st;\n return p.y<ep.p.y;\n }\n};\n\nistream &operator >> (istream &is,Point &p){\n is>>p.x>>p.y;\n return is;\n}\n\nostream &operator << (ostream &os,Point p){\n os<<fixed<<setprecision(12)<<p.x<<\" \"<<p.y;\n return os;\n}\n\nbool sort_x(Point a,Point b){\n return a.x!=b.x?a.x<b.x:a.y<b.y;\n}\n\nbool sort_y(Point a,Point b){\n return a.y!=b.y?a.y<b.y:a.x<b.x;\n}\n\ntypedef Point Vector;\ntypedef vector<Point> Polygon;\n\nistream &operator >> (istream &is,Polygon &p){\n for(int i=0;i<(int)p.size();i++) is>>p[i];\n return is;\n}\n\nstruct Segment{\n Point p1,p2;\n Segment(){}\n Segment(Point p1, Point p2):p1(p1),p2(p2){}\n};\ntypedef Segment Line;\n\nistream &operator >> (istream &is,Segment &s){\n is>>s.p1>>s.p2;\n return is;\n}\n\nstruct Circle{\n Point c;\n double r;\n Circle(){}\n Circle(Point c,double r):c(c),r(r){}\n};\n\nistream &operator >> (istream &is,Circle &c){\n is>>c.c>>c.r;\n return is;\n}\n\ndouble norm(Vector a){\n return a.x*a.x+a.y*a.y;\n}\ndouble abs(Vector a){\n return sqrt(norm(a));\n}\ndouble dot(Vector a,Vector b){\n return a.x*b.x+a.y*b.y;\n}\ndouble cross(Vector a,Vector b){\n return a.x*b.y-a.y*b.x;\n}\n\nPoint orth(Point p){return Point(-p.y,p.x);}\n\nbool isOrthogonal(Vector a,Vector b){\n return equals(dot(a,b),0.0);\n}\n\nbool isOrthogonal(Point a1,Point a2,Point b1,Point b2){\n return isOrthogonal(a1-a2,b1-b2);\n}\n\nbool isOrthogonal(Segment s1,Segment s2){\n return equals(dot(s1.p2-s1.p1,s2.p2-s2.p1),0.0);\n}\n\nbool isParallel(Vector a,Vector b){\n return equals(cross(a,b),0.0);\n}\n\nbool isParallel(Point a1,Point a2,Point b1,Point b2){\n return isParallel(a1-a2,b1-b2);\n}\n\nbool isParallel(Segment s1,Segment s2){\n return equals(cross(s1.p2-s1.p1,s2.p2-s2.p1),0.0); \n}\n\nPoint project(Segment s,Point p){\n Vector base=s.p2-s.p1;\n double r=dot(p-s.p1,base)/norm(base);\n return s.p1+base*r;\n}\n\nPoint reflect(Segment s,Point p){\n return p+(project(s,p)-p)*2.0;\n}\n\ndouble arg(Vector p){\n return atan2(p.y,p.x);\n}\n\nVector polar(double a,double r){\n return Point(cos(r)*a,sin(r)*a);\n}\n\nint ccw(Point p0,Point p1,Point p2);\nbool intersectSS(Point p1,Point p2,Point p3,Point p4);\nbool intersectSS(Segment s1,Segment s2);\nbool intersectPS(Polygon p,Segment l);\nint intersectCC(Circle c1,Circle c2);\nbool intersectSC(Segment s,Circle c);\ndouble getDistanceLP(Line l,Point p);\ndouble getDistanceSP(Segment s,Point p);\ndouble getDistanceSS(Segment s1,Segment s2);\nPoint getCrossPointSS(Segment s1,Segment s2);\nPoint getCrossPointLL(Line l1,Line l2);\nPolygon getCrossPointCL(Circle c,Line l);\nPolygon getCrossPointCC(Circle c1,Circle c2);\nint contains(Polygon g,Point p);\nPolygon andrewScan(Polygon s);\nPolygon convex_hull(Polygon ps);\ndouble diameter(Polygon s);\nbool isConvex(Polygon p);\ndouble area(Polygon s);\nPolygon convexCut(Polygon p,Line l);\nLine bisector(Point p1,Point p2);\nVector translate(Vector v,double theta);\nvector<Line> corner(Line l1,Line l2);\nvector<vector<pair<int, double> > >\nsegmentArrangement(vector<Segment> &ss, Polygon &ps);\n \nint ccw(Point p0,Point p1,Point p2){\n Vector a = p1-p0;\n Vector b = p2-p0;\n if(cross(a,b) > EPS) return CCW_COUNTER_CLOCKWISE;\n if(cross(a,b) < -EPS) return CCW_CLOCKWISE;\n if(dot(a,b) < -EPS) return CCW_ONLINE_BACK;\n if(a.norm()<b.norm()) return CCW_ONLINE_FRONT;\n return CCW_ON_SEGMENT;\n}\n\nbool intersectSS(Point p1,Point p2,Point p3,Point p4){\n return (ccw(p1,p2,p3)*ccw(p1,p2,p4) <= 0 &&\n ccw(p3,p4,p1)*ccw(p3,p4,p2) <= 0 );\n}\n\nbool intersectSS(Segment s1,Segment s2){\n return intersectSS(s1.p1,s1.p2,s2.p1,s2.p2);\n}\n\nbool intersectPS(Polygon p,Segment l){\n int n=p.size();\n for(int i=0;i<n;i++)\n if(intersectSS(Segment(p[i],p[(i+1)%n]),l)) return 1;\n return 0;\n}\n\nint intersectCC(Circle c1,Circle c2){\n if(c1.r<c2.r) swap(c1,c2);\n double d=abs(c1.c-c2.c);\n double r=c1.r+c2.r;\n if(equals(d,r)) return ICC_CIRCUMSCRIBE;\n if(d>r) return ICC_SEPERATE;\n if(equals(d+c2.r,c1.r)) return ICC_INSCRIBE;\n if(d+c2.r<c1.r) return ICC_CONTAIN;\n return ICC_INTERSECT;\n}\n\nbool intersectSC(Segment s,Circle c){\n return getDistanceSP(s,c.c)<=c.r;\n}\n\nint intersectCS(Circle c,Segment s){\n if(norm(project(s,c.c)-c.c)-c.r*c.r>EPS) return 0;\n double d1=abs(c.c-s.p1),d2=abs(c.c-s.p2);\n if(d1<c.r+EPS&&d2<c.r+EPS) return 0;\n if((d1<c.r-EPS&&d2>c.r+EPS)||(d1>c.r+EPS&&d2<c.r-EPS)) return 1;\n Point h=project(s,c.c);\n if(dot(s.p1-h,s.p2-h)<0) return 2;\n return 0;\n}\n\ndouble getDistanceLP(Line l,Point p){\n return abs(cross(l.p2-l.p1,p-l.p1)/abs(l.p2-l.p1));\n}\n\ndouble getDistanceSP(Segment s,Point p){\n if(dot(s.p2-s.p1,p-s.p1) < 0.0 ) return abs(p-s.p1);\n if(dot(s.p1-s.p2,p-s.p2) < 0.0 ) return abs(p-s.p2);\n return getDistanceLP(s,p);\n}\n\ndouble getDistanceSS(Segment s1,Segment s2){\n if(intersectSS(s1,s2)) return 0.0;\n return min(min(getDistanceSP(s1,s2.p1),getDistanceSP(s1,s2.p2)),\n min(getDistanceSP(s2,s1.p1),getDistanceSP(s2,s1.p2)));\n}\n\nPoint getCrossPointSS(Segment s1,Segment s2){\n for(int k=0;k<2;k++){\n if(getDistanceSP(s1,s2.p1)<EPS) return s2.p1; \n if(getDistanceSP(s1,s2.p2)<EPS) return s2.p2;\n swap(s1,s2);\n }\n Vector base=s2.p2-s2.p1;\n double d1=abs(cross(base,s1.p1-s2.p1));\n double d2=abs(cross(base,s1.p2-s2.p1));\n double t=d1/(d1+d2);\n return s1.p1+(s1.p2-s1.p1)*t;\n}\n\nPoint getCrossPointLL(Line l1,Line l2){\n double a=cross(l1.p2-l1.p1,l2.p2-l2.p1);\n double b=cross(l1.p2-l1.p1,l1.p2-l2.p1);\n if(abs(a)<EPS&&abs(b)<EPS) return l2.p1;\n return l2.p1+(l2.p2-l2.p1)*(b/a);\n}\n\nPolygon getCrossPointCL(Circle c,Line l){\n Polygon ps;\n Point pr=project(l,c.c);\n Vector e=(l.p2-l.p1)/abs(l.p2-l.p1);\n if(equals(getDistanceLP(l,c.c),c.r)){\n ps.emplace_back(pr);\n return ps;\n }\n double base=sqrt(c.r*c.r-norm(pr-c.c));\n ps.emplace_back(pr+e*base);\n ps.emplace_back(pr-e*base);\n return ps;\n}\n\nPolygon getCrossPointCS(Circle c,Segment s){\n Line l(s);\n Polygon res=getCrossPointCL(c,l);\n if(intersectCS(c,s)==2) return res;\n if(res.size()>1u){\n if(dot(l.p1-res[0],l.p2-res[0])>0) swap(res[0],res[1]);\n res.pop_back();\n }\n return res;\n}\n\n\nPolygon getCrossPointCC(Circle c1,Circle c2){\n Polygon p(2);\n double d=abs(c1.c-c2.c);\n double a=acos((c1.r*c1.r+d*d-c2.r*c2.r)/(2*c1.r*d));\n double t=arg(c2.c-c1.c);\n p[0]=c1.c+polar(c1.r,t+a);\n p[1]=c1.c+polar(c1.r,t-a);\n return p;\n}\n\n\nstruct UnionFind{\n int n;\n vector<int> r,p;\n UnionFind(){}\n UnionFind(int sz):n(sz),r(sz,1),p(sz,0){iota(p.begin(),p.end(),0);}\n int find(int x){\n return (x==p[x]?x:p[x]=find(p[x]));\n }\n bool same(int x,int y){\n return find(x)==find(y);\n }\n void unite(int x,int y){\n x=find(x);y=find(y);\n if(x==y) return;\n if(r[x]<r[y]) swap(x,y);\n r[x]+=r[y];\n p[y]=x;\n }\n};\n\n//INSERT ABOVE HERE\nsigned main(){\n int n;\n while(cin>>n,n){\n vector<double> xs(n),ys(n),zs(n),rs(n);\n for(int i=0;i<n;i++) cin>>xs[i]>>ys[i]>>zs[i]>>rs[i];\n\n const double EPS2 = 1e-5;\n vector<double> ds;\n for(int i=0;i<n;i++){\n ds.emplace_back(zs[i]-EPS);\n ds.emplace_back(zs[i]+EPS);\n ds.emplace_back(zs[i]-rs[i]-EPS2);\n ds.emplace_back(zs[i]-rs[i]+EPS2);\n ds.emplace_back(zs[i]+rs[i]-EPS2);\n ds.emplace_back(zs[i]+rs[i]+EPS2);\n }\n ds.emplace_back(0);\n ds.emplace_back(36000);\n\n for(int i=0;i<n;i++){\n for(int j=0;j<i;j++){\n double d=abs(Point(xs[i],ys[i])-Point(xs[j],ys[j]));\n Point a(0,zs[i]),b(d,zs[j]);\n Circle p(a,rs[i]),q(b,rs[j]);\n if(intersectCC(p,q)!=ICC_INTERSECT) continue;\n auto cs=getCrossPointCC(p,q);\n for(auto c:cs){\n //cout<<i<<\" \"<<j<<\":\"<<c.y<<endl;\n ds.emplace_back(c.y-EPS2);\n ds.emplace_back(c.y+EPS2);\n }\n }\n }\n \n sort(ds.begin(),ds.end());\n ds.erase(unique(ds.begin(),ds.end()),ds.end());\n \n vector<int> cnt;\n for(auto d:ds){\n UnionFind uf(n);\n int res=n;\n vector<Circle> cs(n);\n for(int i=0;i<n;i++){\n double a=abs(d-zs[i]);\n if(a>rs[i]){\n cs[i]=Circle(Point(xs[i],ys[i]),-1);\n res--;\n continue;\n }\n double r=sqrt(rs[i]*rs[i]-a*a);\n cs[i]=Circle(Point(xs[i],ys[i]),r);\n }\n for(int i=0;i<n;i++){\n for(int j=0;j<n;j++){\n if(uf.same(i,j)) continue;\n if(cs[i].r<0||cs[j].r<0) continue;\n if(abs(cs[i].c-cs[j].c)<cs[i].r+cs[j].r) uf.unite(i,j),res--;\n } \n }\n cnt.emplace_back(res);\n //cout<<d<<\":\"<<res<<endl;\n }\n \n cnt.erase(unique(cnt.begin(),cnt.end()),cnt.end());\n cout<<cnt.size()-1<<endl;\n for(int i=0;i+1<(int)cnt.size();i++) cout<<(cnt[i]<cnt[i+1]);\n cout<<endl;\n }\n return 0;\n}", "accuracy": 1, "time_ms": 210, "memory_kb": 3836, "score_of_the_acc": -0.3872, "final_rank": 13 }, { "submission_id": "aoj_1255_1990492", "code_snippet": "#include \"bits/stdc++.h\"\n#include<unordered_map>\n#include<unordered_set>\n#pragma warning(disable:4996)\nusing namespace std;\nusing ld = long double;\ntemplate<class t>\nusing table = vector<vector<t>>;\nconst ld eps = 1e-9;\n#include \"bits/stdc++.h\"\n#include<unordered_map>\n#include<unordered_set>\n#pragma warning(disable:4996)\nusing namespace std;\nusing ld = long double;\ntemplate<class T>\nusing Table = vector<vector<T>>;\n\nstruct Point3 {\n\tlong double x;\n\tlong double y;\n\tlong double z;\n\tPoint3() :x(0), y(0), z(0) {}\n\tPoint3(const long double _x, const long double _y, const long double _z) :x(_x), y(_y), z(_z) {}\n};\n\nPoint3 operator+(const Point3&l, const Point3& r) {\n\treturn Point3(l.x + r.x, l.y + r.y, l.z + r.z);\n}\nPoint3 operator+=(Point3&l, const Point3& r) {\n\treturn l = Point3(l.x + r.x, l.y + r.y, l.z + r.z);\n}\nPoint3 operator-(const Point3&l, const Point3& r) {\n\treturn Point3(l.x - r.x, l.y - r.y, l.z - r.z);\n}\nPoint3 operator-=(Point3&l, const Point3& r) {\n\treturn l = Point3(l.x - r.x, l.y - r.y, l.z - r.z);\n}\nPoint3 operator*(const Point3&l, const long double r) {\n\treturn Point3(l.x * r, l.y * r, l.z * r);\n}\nPoint3 operator*(const long double r, const Point3&l) {\n\treturn l*r;\n}\nPoint3 operator*=(Point3&l, const long double r) {\n\treturn l = Point3(l.x * r, l.y * r, l.z * r);\n}\nPoint3 operator/(const Point3&l, const long double r) {\n\treturn Point3(l.x / r, l.y / r, l.z / r);\n}\nPoint3 operator/=(Point3&l, const long double r) {\n\treturn l = Point3(l.x / r, l.y / r, l.z / r);\n}\nconst long double pi = acos(-1.0);\nconst long double dtop = pi / 180.;\nconst long double ptod = 1. / dtop;\n\nnamespace std {\n\tbool operator<(const Point3 &lhs, const Point3 &rhs) {\n\t\tif (lhs.x< rhs.x - eps) return true;\n\t\tif (lhs.x > rhs.x + eps) return false;\n\t\tif (lhs.y< rhs.y - eps) return true;\n\t\tif (lhs.y > rhs.y + eps) return false;\n\t\treturn lhs.z < rhs.z;\n\t}\n}\n\n// ????????\\???\nPoint3 input_point() {\n\tlong double x, y, z;\n\tcin >> x >> y >> z;\n\treturn Point3(x, y, z);\n}\n\n// ????????????????????????\nbool eq(const long double a, const long double b) {\n\treturn (abs(a - b) < eps);\n}\n\n// ??????\nlong double dot(const Point3& a, const Point3& b) {\n\treturn a.x*b.x + a.y*b.y + a.z*b.z;\n}\n\n// ??????\nPoint3 cross(const Point3& a, const Point3& b) {\n\treturn Point3(a.y*b.z - a.z*b.y, a.z*b.x - a.x*b.z, a.x*b.y - a.y*b.x);\n}\n\n\n\n//?????????\nlong double norm(const Point3&p) {\n\treturn (p.x*p.x + p.y*p.y + p.z*p.z);\n}\n\nlong double abs(const Point3&p) {\n\n\treturn sqrt(p.x*p.x + p.y*p.y + p.z*p.z);\n}\n//?????????????????????\nPoint3 unit(const Point3& a) {\n\treturn a / (abs(a));\n}\n\n\n\n\n//???\nstruct Sphere {\n\tPoint3 p;\n\tlong double r;\n\tSphere() :p(), r(0) {}\n\tSphere(const Point3 _point, const long double _radius) :p(_point), r(_radius) {}\n};\n/* 0 => out\n1 => on\n2 => in*/\nint point_in_sphere(const Point3&p, const Sphere&s) {\n\tld dis = abs(s.p - p);\n\tif (dis - eps > s.r)return 0;\n\telse if (dis + eps < s.r)return 2;\n\telse return 1;\n}\n\n//a ??? b ?????\\??£????????????\nint sphere_in_sphere(const Sphere&a, const Sphere&b) {\n\tif (!point_in_sphere(a.p, b))return 0;\n\telse {\n\t\tld dis = abs(a.p - b.p);\n\t\tif (a.r + dis - eps>b.r)return 0;\n\t\telse if (a.r + dis + eps < b.r)return 2;\n\t\telse return 1;\n\t}\n}\n\n// ??´????????????\nclass Line3 {\npublic:\n\tPoint3 a, b;\n\tLine3() : a(Point3(0, 0, 0)), b(Point3(0, 0, 0)) {}\n\tLine3(Point3 a, Point3 b) : a(a), b(b) {}\n\tPoint3 operator[](const int _num)const {\n\t\tif (_num == 0)return a;\n\t\telse if (_num == 1)return b;\n\t\telse assert(false);\n\t}\n};\n\n// ?????????????????????\nbool isis_sp(const Line3& s, const Point3& p) {\n\treturn (abs(s.a - p) + abs(s.b - p) - abs(s.b - s.a) < eps);\n}\n\n// ??????????¶?\nPoint3 proj(const Line3 &l, const Point3& p) {\n\tlong double t = dot(p - l.a, l.a - l.b) / norm(l.a - l.b);\n\treturn l.a + t * (l.a - l.b);\n}\n// ??´?????¨???????????¢\nlong double dist_lp3(const Line3& l, const Point3& p) {\n\treturn abs(p - proj(l, p));\n}\n// ?????¨??´????????????\nvector<Point3> is_lsp(const Sphere& c, const Line3& l) {\n\tvector<Point3> res;\n\tlong double d = dist_lp3(l, c.p);\n\tif (d < c.r + eps) {\n\t\tlong double len = (d > c.r) ? 0.0 : sqrt(c.r * c.r - d * d); //safety;\n\t\tPoint3 nor = (l.a - l.b) / abs(l.a - l.b);\n\t\tres.push_back(proj(l, c.p) + len * nor);\n\t\tres.push_back(proj(l, c.p) - len * nor);\n\t}\n\treturn res;\n}\n// ?????¨???????????????\nvector<Point3> is_ssp(const Sphere& c, const Line3& l) {\n\tvector<Point3> res(is_lsp(c, l));\n\tvector<Point3> nres;\n\tfor (auto p : res) {\n\t\tif (isis_sp(l, p))nres.emplace_back(p);\n\t}\n\treturn nres;\n}\n// ????????????????????¢(??????)??§???????°?\nPoint3 reflect(const Point3& vec, const Point3& no) {\n\treturn vec - 2 * dot(vec, no) / abs(no)*unit(no);\n\n}\n\n\nstruct UnionFind {\n\tvector<int> data;\n\tUnionFind(int size) : data(size, -1) { }\n\tbool unionSet(int x, int y) {\n\t\tx = root(x); y = root(y);\n\t\tif (x != y) {\n\t\t\tif (data[y] < data[x]) swap(x, y);\n\t\t\tdata[x] += data[y]; data[y] = x;\n\t\t}\n\t\treturn x != y;\n\t}\n\tbool findSet(int x, int y) {\n\t\treturn root(x) == root(y);\n\t}\n\tint root(int x) {\n\t\treturn data[x] < 0 ? x : data[x] = root(data[x]);\n\t}\n\tint size(int x) {\n\t\treturn -data[root(x)];\n\t}\n};\n\n\n// a binary predicate implemented as a function:\nbool same_integral_part(double first, double second)\n{\n\treturn (abs((first)-(second))<eps);\n}\n\nint main() {\n\twhile (1) {\n\n\t\tint N; cin >> N;\n\t\tif (!N)break;\n\t\tvector<Sphere>sps;\n\t\tfor (int i = 0; i < N; ++i) {\n\n\t\t\tld x, y, z, r; cin >> x >> y >> z >> r;\n\t\t\tSphere sp(Point3(x, y, z), r);\n\t\t\tsps.emplace_back(sp);\n\t\t}\n\t\tvector<ld>nums;\n\t\tfor (int i = 0; i < N; ++i) {\n\t\t\tfor (int j = i + 1; j < N; ++j) {\n\t\t\t\tld aminz, amaxz;\n\t\t\t\tif (sphere_in_sphere(sps[i], sps[j])) {\n\t\t\t\t}\n\t\t\t\telse if (sphere_in_sphere(sps[j], sps[i])) {\n\t\t\t\t}\n\t\t\t\telse {\n\t\t\t\t\tld dis = abs(sps[j].p - sps[i].p);\n\t\t\t\t\tPoint3 center = (sps[i].p + sps[j].p) / 2;\n\n\t\t\t\t\tif (dis - eps > sps[j].r + sps[i].r)continue;\n\t\t\t\t\telse {\n\n\t\t\t\t\t\t{\n\t\t\t\t\t\t\tld amin = max(sps[i].p.z - sps[i].r, sps[j].p.z - sps[j].r) + eps;\n\t\t\t\t\t\t\tld amax = ((sps[i].p.z + sps[j].p.z) - (sps[i].p.z - sps[j].p.z) * (sps[i].r - sps[j].r) / dis) / 2.0;\n\n\t\t\t\t\t\t\tint num = 50;\n\t\t\t\t\t\t\tld disxy = sqrt(powl(sps[i].p.x - sps[j].p.x, 2) + powl(sps[i].p.y - sps[j].p.y, 2));\n\t\t\t\t\t\t\twhile (num--) {\n\t\t\t\t\t\t\t\tld amid = (amin + amax) / 2;\n\t\t\t\t\t\t\t\tld ir = sqrt(sps[i].r*sps[i].r - powl(sps[i].p.z - amid, 2));\n\t\t\t\t\t\t\t\tld jr = sqrt(powl(sps[j].r, 2) - powl(sps[j].p.z - amid, 2));\n\t\t\t\t\t\t\t\tif (ir + jr < disxy) {\n\t\t\t\t\t\t\t\t\tamin = amid;\n\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t\telse {\n\t\t\t\t\t\t\t\t\tamax = amid;\n\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\taminz = amin;\n\t\t\t\t\t\t}\n\t\t\t\t\t\t{\n\t\t\t\t\t\t\tld amax = min(sps[i].p.z + sps[i].r, sps[j].p.z + sps[j].r) + eps;\n\t\t\t\t\t\t\tld amin = ((sps[i].p.z + sps[j].p.z) - (sps[i].p.z - sps[j].p.z) * (sps[i].r - sps[j].r) / dis) / 2.0;\n\n\t\t\t\t\t\t\tint num = 50;\n\t\t\t\t\t\t\tld disxy = sqrt(powl(sps[i].p.x - sps[j].p.x, 2) + powl(sps[i].p.y - sps[j].p.y, 2));\n\t\t\t\t\t\t\twhile (num--) {\n\t\t\t\t\t\t\t\tld amid = (amin + amax) / 2;\n\t\t\t\t\t\t\t\tld ir = sqrt(sps[i].r*sps[i].r - powl(sps[i].p.z - amid, 2));\n\t\t\t\t\t\t\t\tld jr = sqrt(powl(sps[j].r, 2) - powl(sps[j].p.z - amid, 2));\n\t\t\t\t\t\t\t\tif (ir + jr > disxy) {\n\t\t\t\t\t\t\t\t\tamin = amid;\n\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t\telse {\n\t\t\t\t\t\t\t\t\tamax = amid;\n\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\tamaxz = amax;\n\t\t\t\t\t\t}\n\t\t\t\t\t\t\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\tnums.emplace_back(aminz);\n\t\t\t\tnums.emplace_back(amaxz);\n\t\t\t\t\n\t\t\t}\n\t\t\tnums.emplace_back(sps[i].p.z - sps[i].r);\n\t\t\tnums.emplace_back(sps[i].p.z + sps[i].r);\n\t\t}\n\t\tnums.emplace_back(-1);\n\t\tnums.emplace_back(100000);\n\t\tsort(nums.begin(), nums.end());\n\t\tnums.erase(unique(nums.begin(), nums.end(), same_integral_part),nums.end());\n\n\t\tvector<int>anss;\n\t\tfor (int i = 0; i < nums.size()-1; ++i) {\n\t\t\tld az = (nums[i] + nums[i + 1]) / 2;\n\t\t\tUnionFind uf(N);\n\t\t\tvector<int>valids(N);\n\t\t\tfor (int j = 0; j < N; ++j) {\n\t\t\t\tif (az >= sps[j].p.z- sps[j].r&&az <= sps[j].p.z + sps[j].r)valids[j] = true;\n\t\t\t}\n\t\t\tfor (int j = 0; j < N; ++j)\n\t\t\t{\n\t\t\t\tfor (int k = j+1; k < N; ++k) {\n\t\t\t\t\tif (valids[j] && valids[k]) {\n\t\t\t\t\t\tld disxy = sqrt(powl(sps[k].p.x - sps[j].p.x, 2) + powl(sps[k].p.y - sps[j].p.y, 2));\n\n\t\t\t\t\t\tld kr = sqrt(sps[k].r*sps[k].r - powl(sps[k].p.z - az, 2));\n\t\t\t\t\t\tld jr = sqrt(powl(sps[j].r, 2) - powl(sps[j].p.z - az, 2));\n\t\t\t\t\t\tif (kr + jr > disxy) {\n\t\t\t\t\t\t\tuf.unionSet(j, k);\n\t\t\t\t\t\t}\n\t\t\t\t\t\telse {\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t\tset<int>aset;\n\t\t\tfor (int j = 0; j < N; ++j) {\n\t\t\t\tif (valids[j])aset.emplace(uf.root(j));\n\t\t\t}\n\t\t\tanss.emplace_back(aset.size());\n\t\t}\n\t\tstring st;\n\t\tfor (int i = 0; i < anss.size() - 1; ++i) {\n\t\t\tif (anss[i + 1] == anss[i] + 1)st += '1';\n\t\t\telse if (anss[i + 1] + 1 == anss[i])st += '0';\n\n\t\t}\n\t\tcout << st.size() << endl << st << endl;\n\t}\n\n\treturn 0;\n}", "accuracy": 1, "time_ms": 20, "memory_kb": 3532, "score_of_the_acc": -0.1112, "final_rank": 12 }, { "submission_id": "aoj_1255_1559757", "code_snippet": "#include<iostream>\n#include<vector>\n#include<numeric>\n#include<algorithm>\n#include<cmath>\n\nusing namespace std;\n\nint par[123];\nint x[123],y[123],z[123],r[123];\n\nint find(int x){\n return (par[x]==x)?x:par[x]=find(par[x]);\n}\n\nvoid unite(int a,int b){\n par[find(a)]=find(b);\n}\n\ndouble sqr(double d){\n return d*d;\n}\n\ndouble dist(int i,int j,double zz){\n return sqrt(r[i]*r[i]-sqr(z[i]-zz))+sqrt(r[j]*r[j]-sqr(z[j]-zz));\n}\n\ndouble cross(int i,int j,double bm){\n double d=dist(i,j,bm);\n return sqr(x[i]-x[j])+sqr(y[i]-y[j])-d*d<0;\n}\n\nint main(){\n for(int n;cin>>n,n;){\n vector<double> cz{0,1e9};\n for(int i=0;i<n;i++){\n cin>>x[i]>>y[i]>>z[i]>>r[i];\n cz.push_back(z[i]+r[i]);\n cz.push_back(z[i]-r[i]);\n for(int j=0;j<i;j++){\n\tif(sqr(x[i]-x[j])+sqr(y[i]-y[j])+sqr(z[i]-z[j])<sqr(r[i]+r[j])){\n\t int rl=max(z[i]-r[i],z[j]-r[j]),rh=min(z[i]+r[i],z[j]+r[j]);\n\t double l=rl,h=rh;\n\t for(int k=0;k<99;k++){\n\t double lh=(h-l)/3+l,hl=(h-l)*2/3+l;\n\t if(dist(i,j,lh)<dist(i,j,hl)){\n\t l=lh;\n\t }else{\n\t h=hl;\n\t }\n\t }\n\t double bl=rl,bh=l;\n\t double tl=l,th=rh;\n\t for(int k=0;k<99;k++){\n\t double bm=(bh+bl)/2;\n\t if(cross(i,j,bm)){\n\t bh=bm;\n\t }else{\n\t bl=bm;\n\t }\n\t double tm=(tl+th)/2;\n\t if(cross(i,j,tm)){\n\t tl=tm;\n\t }else{\n\t th=tm;\n\t }\n\t }\n\t //\t cout<<rl<<' '<<l<<' '<<rh<<' '<<bl<<' '<<tl<<endl;\n\t cz.push_back(bl);\n\t cz.push_back(tl);\n\t}\n }\n }\n sort(begin(cz),end(cz));\n vector<int> ns{0};\n for(int i=0;i<cz.size()-1;i++){\n double m=(cz[i]+cz[i+1])/2;\n iota(begin(par),end(par),0);\n for(int i=0;i<n;i++){\n\tif(r[i]*r[i]>sqr(z[i]-m)){\n\t for(int j=0;j<i;j++){\n\t if(r[j]*r[j]>sqr(z[j]-m)&&cross(i,j,m)){\n\t unite(i,j);\n\t }\n\t }\n\t}else{\n\t par[i]=-1;\n\t}\n }\n bool u[123]={};\n int ncs=0;\n for(int i=0;i<n;i++){\n\tif(par[i]>=0){\n\t ncs+=!u[find(i)]++;\n\t}\n }\n // cout<<m<<' '<<ncs<<endl;\n if(ncs!=ns.back()){\n\tns.push_back(ncs);\n }\n }\n cout<<ns.size()-1<<endl;\n for(int i=0;i<ns.size()-1;i++){\n cout<<(ns[i+1]-ns[i]==1);\n }\n cout<<endl;\n }\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 1228, "score_of_the_acc": -0.0338, "final_rank": 3 }, { "submission_id": "aoj_1255_1185337", "code_snippet": "#include <iostream>\n#include <cstdio>\n#include <vector>\n#include <set>\n#include <map>\n#include <queue>\n#include <deque>\n#include <stack>\n#include <algorithm>\n#include <cstring>\n#include <functional>\n#include <cmath>\n#include <complex>\n#include <cassert>\nusing namespace std;\n#define rep(i,n) for(int i=0;i<(n);++i)\n#define rep1(i,n) for(int i=1;i<=(n);++i)\n#define all(c) (c).begin(),(c).end()\n#define fs first\n#define sc second\n#define pb push_back\n#define show(x) cout << #x << \" \" << x << endl\nint N;\ndouble x[100],y[100],z[100],r[100];\nvector<double> zs;\ndouble sq(double a,double b){return (a-b)*(a-b);}\ndouble dist(int a,int b){return sqrt(sq(x[a],x[b])+sq(y[a],y[b])+sq(z[a],z[b]));}\nint par[100];\nvoid init(int N){rep(i,N) par[i]=i;}\nint find(int x){\n\tif(x==par[x]) return x;\n\treturn par[x]=find(par[x]);\n}\nvoid unite(int x,int y){\n\tx=find(x),y=find(y);\n\tpar[x]=y;\n}\nint cut(double a){\n\tdouble rr[100]={};\n\tbool is[100]={};\n\trep(i,N) if(z[i]-r[i]<a&&a<z[i]+r[i]){\n\t\tis[i]=1;\n\t\trr[i]=sqrt(r[i]*r[i]-sq(z[i],a));\n\t}\n\tinit(N);\n\trep(i,N) rep(j,i){\n\t\tif(!is[i]||!is[j]) continue;\n\t\tif(sqrt(sq(x[i],x[j])+sq(y[i],y[j]))<rr[i]+rr[j]) unite(i,j);\n\t}\n\tset<int> ps;\n\trep(i,N) if(is[i]) ps.insert(find(i));\n\treturn ps.size();\n}\nint main(){\n\twhile(true){\n\t\tcin>>N;\n\t\tif(N==0) break;\n\t\tzs.clear();\n\t\trep(i,N) cin>>x[i]>>y[i]>>z[i]>>r[i];\n\t\trep(i,N) zs.pb(z[i]-r[i]),zs.pb(z[i]+r[i]);\n\t\trep(i,N) rep(j,i){\n\t\t\tdouble d=dist(i,j);\n\t\t\tif(d>r[i]+r[j]) continue;\n\t\t\tif(abs(r[i]-r[j])>d) continue;\n\t\t\tdouble theta=acos( (r[i]*r[i]+d*d-r[j]*r[j])/(2.0*r[i]*d) );\n\t\t\tdouble phi=atan2( z[j]-z[i],sqrt(sq(x[i],x[j])+sq(y[i],y[j])) );\n\t\t\tzs.pb(z[i]+r[i]*sin(theta+phi));\n\t\t\tzs.pb(z[i]+r[i]*sin(-theta+phi));\n\t\t}\n\t\tsort(all(zs));\n\t\tint now=0;\n\t\tstring ans;\n\t\tfor(double a:zs){\n\t\t\tint t=cut(a+1e-4);\n\t\t\tif(t==now+1){\n\t\t\t\tans+='1';\n\t\t\t}else if(t==now-1){\n\t\t\t\tans+='0';\n\t\t\t}else if(t==now){\n\t\t\t}else{\n\t\t\t\tassert(0);\n\t\t\t}\n\t\t\tnow=t;\n\t\t}\n\t\tcout<<ans.size()<<endl<<ans<<endl;\n\t}\n}", "accuracy": 1, "time_ms": 20, "memory_kb": 1416, "score_of_the_acc": -0.053, "final_rank": 4 }, { "submission_id": "aoj_1255_806941", "code_snippet": "#include<stdio.h>\n#include<algorithm>\n#include<math.h>\nusing namespace std;\ndouble event[1000000];\ndouble x[200];\ndouble y[200];\ndouble z[200];\ndouble r[200];\ndouble dist(double x1,double y1,double z1,double x2,double y2,double z2){\n\treturn sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2)+(z1-z2)*(z1-z2));\n}\ndouble Abs(double a){return max(a,-a);}\nconst double EPS=1e-10;\nconst double INF=1e+10;\nconst double PI=acos(-1);\nint sig(double r){return (r<-EPS)?-1:(r>+EPS)?+1:0;}\nstruct Pt{\n\tdouble x,y;\n\tPt(){}\n\tPt(double x,double y):x(x),y(y){}\n\tPt operator+(const Pt &a)const{return Pt(x+a.x,y+a.y);}\n\tPt operator-(const Pt &a)const{return Pt(x-a.x,y-a.y);}\n\tPt operator*(const Pt &a)const{return Pt(x*a.x-y*a.y,x*a.y+y*a.x);}\n\tPt operator-()const{return Pt(-x,-y);}\n\tPt operator*(const double &k)const{return Pt(x*k,y*k);}\n\tPt operator/(const double &k)const{return Pt(x/k,y/k);}\n\tdouble Abs()const{return sqrt(x*x+y*y);}\n\tdouble Abs2()const{return x*x+y*y;}\n\tdouble arg()const{return atan2(y,x);}\n\tdouble dot(const Pt &a)const{return x*a.x+y*a.y;}\n\tdouble det(const Pt &a)const{return x*a.y-y*a.x;}\n};\ndouble tri(const Pt&a,const Pt&b,const Pt&c){return (b-a).det(c-a);}\npair<Pt,Pt> pCC(Pt a,double r,Pt b,double s){\n\tdouble d=(b-a).Abs();\n\tdouble x=(d*d+r*r-s*s)/(d*2);\n\tPt e=(b-a)/d,w=e*Pt(0,1)*sqrt(max(r*r-x*x,0.0));\n\treturn make_pair(a+e*x-w,a+e*x+w);\n}\nint ans[100000];\nint UF[128];\nint FIND(int a){\n\tif(UF[a]<0)return a;\n\treturn UF[a]=FIND(UF[a]);\n}\nvoid UNION(int a,int b){\n\ta=FIND(a);b=FIND(b);\n\tif(a==b)return;\n\tUF[a]+=UF[b];\n\tUF[b]=a;\n}\nint main(){\n\tint a;\n\twhile(scanf(\"%d\",&a),a){\n\t\tfor(int i=0;i<a;i++)scanf(\"%lf%lf%lf%lf\",x+i,y+i,z+i,r+i);\n\t\tint ind=0;\n\t\tevent[ind++]=0;\n\t\tfor(int i=0;i<a;i++){\n\t\t\tevent[ind++]=z[i]-r[i];\n\t\t\tevent[ind++]=z[i]+r[i];\n\t\t}\n\t\tfor(int i=0;i<a;i++)\n\t\t\tfor(int j=i+1;j<a;j++){\n\t\t\t\tif(dist(x[i],y[i],z[i],x[j],y[j],z[j])>r[i]+r[j])continue;\n\t\t\t\tif(Abs(r[i]-r[j])>dist(x[i],y[i],z[i],x[j],y[j],z[j]))continue;\n\t\t\t\tpair<Pt,Pt> inter=pCC(Pt(0.0,z[i]),r[i],Pt(dist(x[i],y[i],0,x[j],y[j],0),z[j]),r[j]);\n\t\t\t\tevent[ind++]=inter.first.y;\n\t\t\t\tevent[ind++]=inter.second.y;\n\t\t\t}\n\t\tevent[ind++]=50000;\n\t\tstd::sort(event,event+ind);\n\t\tint at=0;\n\t\tint last=0;\n\t\tfor(int i=0;i<ind;i++){\n\t\t\tif(i&&event[i]-event[i-1]<EPS)continue;\n\t\t\tdouble M=(event[i]+event[i-1])/2;\n\t\t\tfor(int j=0;j<a;j++)UF[j]=-1;\n\t\t\tfor(int j=0;j<a;j++){\n\t\t\t\tif(M+EPS<z[j]-r[j]||M-EPS>z[j]+r[j])continue;\n\t\t\t\tfor(int k=j+1;k<a;k++){\n\t\t\t\t\tif(M+EPS<z[k]-r[k]||M-EPS>z[k]+r[k])continue;\n\t\t\t\t\tif(dist(x[j],y[j],0,x[k],y[k],0)<sqrt(r[j]*r[j]-(z[j]-M)*(z[j]-M))+sqrt(r[k]*r[k]-(z[k]-M)*(z[k]-M)))UNION(j,k);\n\t\t\t\t}\n\t\t\t}\n\t\t\tint count=0;\n\t\t\tfor(int j=0;j<a;j++){\n\t\t\t\tif(M+EPS<z[j]-r[j]||M-EPS>z[j]+r[j])continue;\n\t\t\t\t//printf(\"(%d,%d) \",j,UF[j]);\n\t\t\t\tif(UF[j]<0)count++;\n\t\t\t}\n\t\t\tif(last!=count){\n\t\t\t\tif(last<count)ans[at++]=1;\n\t\t\t\telse ans[at++]=0;\n\t\t\t}\n\t\t//\tprintf(\"%f %d\\n\",M,count);\n\t\t\tlast=count;\n\t\t}\n\t\tprintf(\"%d\\n\",at);\n\t\tfor(int i=0;i<at;i++)printf(\"%d\",ans[i]);\n\t\tprintf(\"\\n\");\n\t}\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 1104, "score_of_the_acc": -0.0304, "final_rank": 2 }, { "submission_id": "aoj_1255_291721", "code_snippet": "#include <iostream>\n#include <vector>\n#include <algorithm>\n#include <complex>\n#include <cmath>\n#include <iterator>\nusing namespace std;\ntypedef complex<double> P;\nstatic const double EPS = 1e-6;\n\nstruct P3\n{\n double x, y, z;\n P3() {}\n P3(double a, double b, double c) : x(a), y(b), z(c) {}\n P3& operator+=(const P3& q) { x += q.x; y += q.y; z += q.z; return *this; }\n P3& operator-=(const P3& q) { x -= q.x; y -= q.y; z -= q.z; return *this; }\n P3& operator*=(double a) { x *= a; y *= a; z *= a; return *this; }\n P3& operator/=(double a) { x /= a; y /= a; z /= a; return *this; }\n\n};\ninline ostream& operator<<(ostream& os, const P3& p) { return os << \"(\" << p.x << \",\" << p.y << \",\" << p.z << \")\"; }\nP3 operator+(const P3& p, const P3& q) { P3 r(p); return r += q; }\nP3 operator-(const P3& p, const P3& q) { P3 r(p); return r -= q; }\nP3 operator*(const P3& p, double a) { P3 q(p); q *= a; return q; }\nP3 operator*(double a, const P3& p) { return p*a; }\nP3 operator/(const P3& p, double a) { P3 q(p); q /= a; return q; }\nP3 operator/(double a, const P3& p) { return p/a; }\ninline double dot(const P3& p, const P3& q) { return p.x*q.x + p.y*q.y + p.z*q.z; }\ninline double abs(const P3& p) { return sqrt(dot(p, p)); }\ninline P3 cross(const P3& p, const P3& q) { return P3(p.y*q.z - q.y*p.z, p.z*q.x - q.z*p.x, p.x*q.y - q.x*p.y); }\n\nstruct circle\n{\n P o;\n double r;\n circle() {}\n circle(const P& p, double x) : o(p), r(x) {}\n\n inline bool contains(const circle& c) const { return abs(o - c.o)+c.r <= r; }\n inline bool intersects(const circle& c) const { return !contains(c) && !c.contains(*this) && abs(o - c.o) <= r + c.r; }\n pair<P,P> intersection(const circle& c) const\n {\n // assert(intersects(c))\n const double d = abs(o - c.o);\n const double cos_ = (d*d + r*r - c.r*c.r) / (2*d);\n const double sin_ = sqrt(r*r - cos_*cos_);\n const P e = (c.o - o) / d;\n return make_pair(o + e*P(cos_, sin_), o + e*P(cos_, -sin_));\n }\n};\ninline ostream& operator<<(ostream& os, const circle& c) { return os << c.o << \"[r=\" << c.r << \"]\"; }\n\nstruct sphere\n{\n P3 o;\n double r;\n sphere(const P3& p, double x) : o(p), r(x) {}\n\n inline circle cutz(double z) const { return circle(P(o.x, o.y), sqrt(r*r - (z-o.z)*(z-o.z))); }\n inline bool contains(const sphere& s) const { return abs(o - s.o)+s.r <= r; }\n inline bool intersects(const sphere& s) const { return !contains(s) && !s.contains(*this) && abs(o - s.o) <= r + s.r; }\n\n double center_z(const sphere& s) const\n {\n const P3 p = s.o - o;\n const double L = abs(p);\n const double x1 = (r*r - s.r*s.r + L*L)/(2*L);\n return (o + x1/L*p).z;\n }\n\n pair<double,double> intersection_z(const sphere& that) const\n {\n const double z0 = center_z(that);\n double left = z0, right = min(o.z+r, that.o.z+that.r);\n for (int i = 0; i < 100; i++) {\n const double mid = (left+right)/2.0;\n const circle c1 = cutz(mid), c2 = that.cutz(mid);\n const double d = abs(c1.o - c2.o);\n if (d < c1.r + c2.r) {\n left = mid;\n } else {\n right = mid;\n }\n }\n const double z1 = (left+right)/2.0;\n\n left = max(o.z-r, that.o.z-that.r), right = z0;\n for (int i = 0; i < 100; i++) {\n const double mid = (left+right)/2.0;\n const circle c1 = cutz(mid), c2 = that.cutz(mid);\n const double d = abs(c1.o - c2.o);\n if (d < c1.r + c2.r) {\n right = mid;\n } else {\n left = mid;\n }\n }\n const double z2 = (left+right)/2.0;\n return make_pair(z1, z2);\n }\n};\ninline ostream& operator<<(ostream& os, const sphere& s) { return os << s.o << \"[r=\" << s.r << \"]\"; }\n\nstruct DisjointSet/*{{{*/\n{\n vector<int> parent;\n\n int root(int x)\n {\n if (parent[x] < 0) {\n return x;\n } else {\n parent[x] = root(parent[x]);\n return parent[x];\n }\n }\n\n explicit DisjointSet(int n) : parent(n, -1) {}\n\n bool unite(int x, int y)\n {\n const int a = root(x);\n const int b = root(y);\n if (a != b) {\n if (parent[a] < parent[b]) {\n parent[a] += parent[b];\n parent[b] = a;\n } else {\n parent[b] += parent[a];\n parent[a] = b;\n }\n return true;\n } else {\n return false;\n }\n }\n\n bool find(int x, int y) { return root(x) == root(y); }\n int size(int x) { return -parent[root(x)]; }\n};/*}}}*/\n\nint count_sphere(const vector<sphere>& ss, double z)\n{\n vector<circle> cs;\n for (vector<sphere>::const_iterator it = ss.begin(); it != ss.end(); ++it) {\n if (abs(z - it->o.z) < it->r) {\n cs.push_back(it->cutz(z));\n }\n }\n\n const int N = cs.size();\n DisjointSet s(N);\n for (int i = 0; i < N; i++) {\n for (int j = i+1; j < N; j++) {\n if (cs[i].contains(cs[j]) || cs[j].contains(cs[i]) || cs[i].intersects(cs[j])) {\n s.unite(i, j);\n }\n }\n }\n int ans = 0;\n for (int i = 0; i < N; i++) {\n if (s.parent[i] < 0) {\n ++ans;\n }\n }\n return ans;\n}\n\nint main()\n{\n int N;\n while (cin >> N && N != 0) {\n vector<sphere> ss;\n vector<double> zs;\n for (int i = 0; i < N; i++) {\n int x, y, z, r;\n cin >> x >> y >> z >> r;\n ss.push_back(sphere(P3(x, y, z), r));\n zs.push_back(z-r);\n zs.push_back(z+r);\n }\n\n for (vector<sphere>::const_iterator it = ss.begin(); it != ss.end(); ++it) {\n for (vector<sphere>::const_iterator jt = it+1; jt != ss.end(); ++jt) {\n if (it->intersects(*jt)) {\n const pair<double,double> r = it->intersection_z(*jt);\n zs.push_back(r.first);\n zs.push_back(r.second);\n }\n }\n }\n sort(zs.begin(), zs.end());\n\n vector<char> ans;\n int prev = 0;\n for (vector<double>::const_iterator it = zs.begin(); it != zs.end(); ++it) {\n const int r = count_sphere(ss, *it+EPS);\n if (prev < r) {\n ans.push_back('1');\n } else if (prev > r) {\n ans.push_back('0');\n }\n prev = r;\n }\n\n cout << ans.size() << endl;\n copy(ans.begin(), ans.end(), ostream_iterator<char>(cout, \"\"));\n cout << endl;\n }\n return 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 0, "score_of_the_acc": 0, "final_rank": 1 }, { "submission_id": "aoj_1255_96576", "code_snippet": "#include<iostream>\n#include<complex>\n#include<algorithm>\n#include<vector>\nusing namespace std;\n#define REP(i,b,n) for(int i=b;i<n;i++)\n#define rep(i,n) REP(i,0,n)\n#define ALL(C) (C).begin(),(C).end()\n#define pb push_back\n#define mp make_pair\n\nconst double eps = 1e-10;\n\ntypedef complex<double> P;\n\nclass Point{\npublic:\n double x,y,z;\n};\n/*\nfor live archive\nbool isnan(double x){\n if (x >=0 || x <= 0)return false;\n return true;\n}\n*/\nclass DisjointSet{\npublic:\n vector<int> rank,p;\n DisjointSet(){}\n DisjointSet(int size){\n rank.resize(size,0);\n p.resize(size,0);\n }\n \n void makeset(int x){\n p[x]=x;\n rank[x]=0;\n }\n void merge(int x,int y){\n link(findset(x),findset(y));\n }\n\n void link(int x,int y){\n if (rank[x] > rank[y]){\n p[y]=x;\n }else {\n p[x]=y;\n if (rank[x] == rank[y])rank[y]++;\n }\n }\n int findset(int x){\n if (x != p[x])p[x] = findset(p[x]);\n return p[x];\n }\n};\n\nint is_intersected_circle(P a,P b,double r1,double r2){\n double d =abs(a-b);\n if (d<eps&&abs(r1-r2)<eps)return 3;\n if (d+r2 < r1)return 0;\n if (d+r1 < r2)return 1;\n\n if (d > r1+r2)return 4;\n\n return 2;\n}\n\ninline double calc_r(double z,double r){\n if (z < 0)z*=-1;\n return sqrt(r*r-z*z);\n}\n\ndouble bsearch(P a,P b,double az,double bz,double ar,double br,double r,double l,\n\t bool islower){\n double mid;\n while(l < r+eps){\n mid = (r+l)/2;\n if (islower){\n if (is_intersected_circle(a,b,calc_r(az-mid,ar),calc_r(bz-mid,br))==2)r=mid-eps;\n else l=mid+eps;\n }else {\n if (is_intersected_circle(a,b,calc_r(az-mid,ar),calc_r(bz-mid,br))==2)l=mid+eps;\n else r=mid-eps;\n }\n }\n return mid;\n}\n\nint is_intersected_sphere(Point a,Point b,double r1,double r2){\n double d =sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y)+(a.z-b.z)*(a.z-b.z));\n if (d<eps&&abs(r1-r2)<eps)return 3;\n if (d+r2 < r1)return 0;\n if (d+r1 < r2)return 1;\n if (d > r1+r2)return 4;\n return 2;\n}\n\ndouble compute_z(Point a,Point b,double r1,double r2){\n double l,m,n;\n double d =sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y)+(a.z-b.z)*(a.z-b.z)); \n double z;\n l=(d*d-r2*r2+r1*r1)/(2*d);\n z = a.z + (b.z-a.z)*l/d;\n return z;\n}\n\nbool is_contain_center_in_circle(P a,P b,double r1){\n if (isnan(r1))return false;\n double d = abs(a-b);\n if (d < r1 + eps)return true;\n return false;\n}\n\nbool is_contain_on_intersection(P a,P b,double ra,double rb){\n return is_contain_center_in_circle(a,b,ra)||is_contain_center_in_circle(b,a,rb);\n}\n\nvoid solve(int n,Point *in,double *r){\n vector<pair<double,int> > ans;\n rep(i,n){\n ans.pb(mp(in[i].z-r[i],1));\n ans.pb(mp(in[i].z+r[i],0));\n }\n\n rep(i,n){\n REP(j,i+1,n){\n int tmp = is_intersected_sphere(in[i],in[j],r[i],r[j]);\n if (is_intersected_sphere(in[i],in[j],r[i],r[j]) == 2){\n\tdouble z = compute_z(in[i],in[j],r[i],r[j]);\n\tdouble right,left;\n\tright=z;\n\tleft=max(in[i].z-r[i],in[j].z-r[j]);\n\tdouble tmp =bsearch(P(in[i].x,in[i].y),P(in[j].x,in[j].y),in[i].z,in[j].z,\n\t\t\t r[i],r[j],right,left,true); \n\tans.pb(mp(tmp,1));//start of intersection\n\tright=min(in[i].z+r[i],in[j].z+r[j]);\n\tleft=z;\n\ttmp = bsearch(P(in[i].x,in[i].y),P(in[j].x,in[j].y),in[i].z,in[j].z,\n\t\t r[i],r[j],right,left,false);\n\tans.pb(mp(tmp,0));//start of intersection\n }\n }\n }\n\n\n int component=0;\n sort(ALL(ans));\n DisjointSet dj(n);\n vector<int >ret;\n rep(k,ans.size()){\n int tmp=0;\n double z = ans[k].first+eps;\n rep(i,n)dj.makeset(i);\n rep(i,n){\n if (isnan(calc_r(in[i].z-z,r[i])))continue;\n REP(j,i+1,n){\n\tif (isnan(calc_r(in[j].z-z,r[j])))continue;\t\n\tif (\n\t is_contain_on_intersection(\n\t\t\t\t P(in[i].x,in[i].y),\n\t\t\t\t P(in[j].x,in[j].y),\n\t\t\t\t calc_r(in[i].z-z,r[i]),\n\t\t\t\t calc_r(in[j].z-z,r[j]))\n\t ||\n\t is_intersected_circle(P(in[i].x,in[i].y),\n\t\t\t\t P(in[j].x,in[j].y),\n\t\t\t\t calc_r(in[i].z-z,r[i]),\n\t\t\t\t calc_r(in[j].z-z,r[j])) == 2\n\t \n\t )dj.merge(i,j);\n }\n }\n \n rep(i,n){\n if (isnan(calc_r(in[i].z-z,r[i])))continue;\n if (i == dj.findset(i))tmp++;\n }\n \n if (tmp > component)ret.pb(1);//cout << 1;\n else if (tmp < component)ret.pb(0);//cout << 0;\n component=tmp;\n }\n\n cout << ret.size() << endl;\n rep(i,ret.size()){\n cout << ret[i];\n }\n cout << endl;\n}\n\n\nmain(){\n int n;\n while(cin>>n && n){\n double r[n];\n Point in[n];\n rep(i,n){\n cin>>in[i].x>>in[i].y>>in[i].z >>r[i];\n }\n solve(n,in,r);\n }\n}", "accuracy": 1, "time_ms": 720, "memory_kb": 1008, "score_of_the_acc": -1.0277, "final_rank": 15 }, { "submission_id": "aoj_1255_96571", "code_snippet": "#include<iostream>\n#include<complex>\n#include<algorithm>\n#include<vector>\nusing namespace std;\n#define REP(i,b,n) for(int i=b;i<n;i++)\n#define rep(i,n) REP(i,0,n)\n#define ALL(C) (C).begin(),(C).end()\n#define pb push_back\n#define mp make_pair\n\nconst double eps = 1e-10;\n\ntypedef complex<double> P;\n\nclass Point{\npublic:\n double x,y,z;\n};\n\nclass DisjointSet{\npublic:\n vector<int> rank,p;\n DisjointSet(){}\n DisjointSet(int size){\n rank.resize(size,0);\n p.resize(size,0);\n }\n \n void makeset(int x){\n p[x]=x;\n rank[x]=0;\n }\n void merge(int x,int y){\n link(findset(x),findset(y));\n }\n\n void link(int x,int y){\n if (rank[x] > rank[y]){\n p[y]=x;\n }else {\n p[x]=y;\n if (rank[x] == rank[y])rank[y]++;\n }\n }\n int findset(int x){\n if (x != p[x])p[x] = findset(p[x]);\n return p[x];\n }\n};\n\nint is_intersected_circle(P a,P b,double r1,double r2){\n double d =abs(a-b);\n if (d<eps&&abs(r1-r2)<eps)return 3;\n if (d+r2 < r1)return 0;\n if (d+r1 < r2)return 1;\n\n if (d > r1+r2)return 4;\n\n return 2;\n}\n\ninline double calc_r(double z,double r){\n if (z < 0)z*=-1;\n return sqrt(r*r-z*z);\n}\n\ndouble bsearch(P a,P b,double az,double bz,double ar,double br,double r,double l,\n\t bool islower){\n double mid;\n while(l < r+eps){\n mid = (r+l)/2;\n if (islower){\n if (is_intersected_circle(a,b,calc_r(az-mid,ar),calc_r(bz-mid,br))==2)r=mid-eps;\n else l=mid+eps;\n }else {\n if (is_intersected_circle(a,b,calc_r(az-mid,ar),calc_r(bz-mid,br))==2)l=mid+eps;\n else r=mid-eps;\n }\n }\n return mid;\n}\n\nint is_intersected_sphere(Point a,Point b,double r1,double r2){\n double d =sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y)+(a.z-b.z)*(a.z-b.z));\n if (d<eps&&abs(r1-r2)<eps)return 3;\n if (d+r2 < r1)return 0;\n if (d+r1 < r2)return 1;\n if (d > r1+r2)return 4;\n return 2;\n}\n\ndouble compute_z(Point a,Point b,double r1,double r2){\n double l,m,n;\n double d =sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y)+(a.z-b.z)*(a.z-b.z)); \n double z;\n l=(d*d-r2*r2+r1*r1)/(2*d);\n z = a.z + (b.z-a.z)*l/d;\n return z;\n}\n\nbool is_contain_center_in_circle(P a,P b,double r1){\n if (isnan(r1))return false;\n double d = abs(a-b);\n if (d < r1 + eps)return true;\n return false;\n}\n\nbool is_contain_on_intersection(P a,P b,double ra,double rb){\n return is_contain_center_in_circle(a,b,ra)||is_contain_center_in_circle(b,a,rb);\n}\n\nvoid solve(int n,Point *in,double *r){\n vector<pair<double,int> > ans;\n rep(i,n){\n ans.pb(mp(in[i].z-r[i],1));\n ans.pb(mp(in[i].z+r[i],0));\n }\n\n rep(i,n){\n REP(j,i+1,n){\n int tmp = is_intersected_sphere(in[i],in[j],r[i],r[j]);\n if (is_intersected_sphere(in[i],in[j],r[i],r[j]) == 2){\n\tdouble z = compute_z(in[i],in[j],r[i],r[j]);\n\tdouble right,left;\n\tright=z;\n\tleft=max(in[i].z-r[i],in[j].z-r[j]);\n\tdouble tmp =bsearch(P(in[i].x,in[i].y),P(in[j].x,in[j].y),in[i].z,in[j].z,\n\t\t\t r[i],r[j],right,left,true); \n\tans.pb(mp(tmp,1));//start of intersection\n\tright=min(in[i].z+r[i],in[j].z+r[j]);\n\tleft=z;\n\ttmp = bsearch(P(in[i].x,in[i].y),P(in[j].x,in[j].y),in[i].z,in[j].z,\n\t\t r[i],r[j],right,left,false);\n\tans.pb(mp(tmp,0));//start of intersection\n }\n }\n }\n\n\n int component=0;\n sort(ALL(ans));\n DisjointSet dj(n);\n vector<int >ret;\n rep(k,ans.size()){\n int tmp=0;\n double z = ans[k].first+eps;\n rep(i,n)dj.makeset(i);\n rep(i,n){\n if (isnan(calc_r(in[i].z-z,r[i])))continue;\n REP(j,i+1,n){\n\tif (isnan(calc_r(in[j].z-z,r[j])))continue;\t\n\tif (\n\t is_contain_on_intersection(\n\t\t\t\t P(in[i].x,in[i].y),\n\t\t\t\t P(in[j].x,in[j].y),\n\t\t\t\t calc_r(in[i].z-z,r[i]),\n\t\t\t\t calc_r(in[j].z-z,r[j]))\n\t ||\n\t is_intersected_circle(P(in[i].x,in[i].y),\n\t\t\t\t P(in[j].x,in[j].y),\n\t\t\t\t calc_r(in[i].z-z,r[i]),\n\t\t\t\t calc_r(in[j].z-z,r[j])) == 2\n\t \n\t )dj.merge(i,j);\n }\n }\n \n rep(i,n){\n if (isnan(calc_r(in[i].z-z,r[i])))continue;\n if (i == dj.findset(i))tmp++;\n }\n \n if (tmp > component)ret.pb(1);//cout << 1;\n else if (tmp < component)ret.pb(0);//cout << 0;\n component=tmp;\n }\n\n cout << ret.size() << endl;\n rep(i,ret.size()){\n cout << ret[i];\n }\n cout << endl;\n}\n\n\nmain(){\n int n;\n while(cin>>n && n){\n double r[n];\n Point in[n];\n rep(i,n){\n cin>>in[i].x>>in[i].y>>in[i].z >>r[i];\n }\n solve(n,in,r);\n }\n}", "accuracy": 1, "time_ms": 710, "memory_kb": 1008, "score_of_the_acc": -1.0136, "final_rank": 14 }, { "submission_id": "aoj_1255_96566", "code_snippet": "#include<iostream>\n#include<complex>\n#include<algorithm>\n#include<vector>\nusing namespace std;\n#define REP(i,b,n) for(int i=b;i<n;i++)\n#define rep(i,n) REP(i,0,n)\n#define ALL(C) (C).begin(),(C).end()\n#define pb push_back\n#define mp make_pair\n\nconst double eps = 1e-10;\n\ntypedef complex<double> P;\n\nclass Point{\npublic:\n double x,y,z;\n};\n\nclass DisjointSet{\npublic:\n vector<int> rank,p;\n DisjointSet(){}\n DisjointSet(int size){\n rank.resize(size,0);\n p.resize(size,0);\n }\n \n void makeset(int x){\n p[x]=x;\n rank[x]=0;\n }\n void merge(int x,int y){\n link(findset(x),findset(y));\n }\n\n void link(int x,int y){\n if (rank[x] > rank[y]){\n p[y]=x;\n }else {\n p[x]=y;\n if (rank[x] == rank[y])rank[y]++;\n }\n }\n int findset(int x){\n if (x != p[x])p[x] = findset(p[x]);\n return p[x];\n }\n};\n\nint is_intersected_circle(P a,P b,double r1,double r2){\n double d =abs(a-b);\n if (d<eps&&abs(r1-r2)<eps)return 3;\n if (d+r2 < r1)return 0;\n if (d+r1 < r2)return 1;\n\n if (d > r1+r2)return 4;\n\n return 2;\n}\n\ninline double calc_r(double z,double r){\n if (z < 0)z*=-1;\n return sqrt(r*r-z*z);\n}\n\n\n\ndouble bsearch(P a,P b,double az,double bz,double ar,double br,double r,double l,\n\t bool islower){\n double mid;\n while(l < r+eps){\n mid = (r+l)/2;\n if (islower){\n if (is_intersected_circle(a,b,calc_r(az-mid,ar),calc_r(bz-mid,br))==2)r=mid-eps;\n else l=mid+eps;\n }else {\n if (is_intersected_circle(a,b,calc_r(az-mid,ar),calc_r(bz-mid,br))==2)l=mid+eps;\n else r=mid-eps;\n }\n }\n return mid;\n}\n\nint is_intersected_sphere(Point a,Point b,double r1,double r2){\n double d =sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y)+(a.z-b.z)*(a.z-b.z));\n if (d<eps&&abs(r1-r2)<eps)return 3;\n if (d+r2 < r1)return 0;\n if (d+r1 < r2)return 1;\n if (d > r1+r2)return 4;\n return 2;\n}\n\ndouble compute_z(Point a,Point b,double r1,double r2){\n double l,m,n;\n double d =sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y)+(a.z-b.z)*(a.z-b.z)); \n double z;\n l=(d*d-r2*r2+r1*r1)/(2*d);\n z = a.z + (b.z-a.z)*l/d;\n return z;\n}\n\nbool is_contain_center_in_sphere(Point a,Point b,double r1){\n double d =sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y)+(a.z-b.z)*(a.z-b.z)); \n if (d < r1+eps)return true;\n return false;\n}\n\nbool is_contain_center_in_circle(P a,P b,double r1){\n if (isnan(r1))return false;\n double d = abs(a-b);\n if (d < r1 + eps)return true;\n return false;\n}\n\nbool is_contain_on_intersection(P a,P b,double ra,double rb){\n return is_contain_center_in_circle(a,b,ra)||is_contain_center_in_circle(b,a,rb);\n}\n\nvoid solve(int n,Point *in,double *r){\n vector<pair<double,int> > ans;\n rep(i,n){\n ans.pb(mp(in[i].z-r[i],1));\n ans.pb(mp(in[i].z+r[i],0));\n }\n\n rep(i,n){\n REP(j,i+1,n){\n int tmp = is_intersected_sphere(in[i],in[j],r[i],r[j]);\n if (is_intersected_sphere(in[i],in[j],r[i],r[j]) == 2){\n\tdouble z = compute_z(in[i],in[j],r[i],r[j]);\n\tdouble right,left;\n\tright=z;\n\tleft=max(in[i].z-r[i],in[j].z-r[j]);\n\tdouble tmp =bsearch(P(in[i].x,in[i].y),P(in[j].x,in[j].y),in[i].z,in[j].z,\n\t\t\t r[i],r[j],right,left,true); \n\tans.pb(mp(tmp,1));//start of intersection\n\tright=min(in[i].z+r[i],in[j].z+r[j]);\n\tleft=z;\n\ttmp = bsearch(P(in[i].x,in[i].y),P(in[j].x,in[j].y),in[i].z,in[j].z,\n\t\t r[i],r[j],right,left,false);\n\tans.pb(mp(tmp,0));//start of intersection\n }\n }\n }\n\n\n int component=0;\n sort(ALL(ans));\n DisjointSet dj(n);\n vector<int >ret;\n rep(k,ans.size()){\n int tmp=0;\n double z = ans[k].first+eps;\n rep(i,n)dj.makeset(i);\n\n //cout <<k <<\" \" << z <<\" \" << ans[k].second << endl;\n\n\n\n rep(i,n){\n if (isnan(calc_r(in[i].z-z,r[i])))continue;\n REP(j,i+1,n){\n\tif (isnan(calc_r(in[j].z-z,r[j])))continue;\t\n\tif (\n\t is_contain_on_intersection(\n\t\t\t\t P(in[i].x,in[i].y),\n\t\t\t\t P(in[j].x,in[j].y),\n\t\t\t\t calc_r(in[i].z-z,r[i]),\n\t\t\t\t calc_r(in[j].z-z,r[j]))\n\t ||\n\t is_intersected_circle(P(in[i].x,in[i].y),\n\t\t\t\t P(in[j].x,in[j].y),\n\t\t\t\t calc_r(in[i].z-z,r[i]),\n\t\t\t\t calc_r(in[j].z-z,r[j])) == 2\n\t \n\t )dj.merge(i,j);\n }\n }\n \n rep(i,n){\n if (isnan(calc_r(in[i].z-z,r[i])))continue;\n if (i == dj.findset(i))tmp++;\n }\n\n /*\n rep(i,n){\n if (isnan(calc_r(in[i].z-z,r[i])))cout <<\"= \";\n else cout << dj.findset(i) <<\" \";\n }\n cout << endl;\n */ \n\n \n if (tmp > component)ret.pb(1);//cout << 1;\n else if (tmp < component)ret.pb(0);//cout << 0;\n \n\n component=tmp;\n }\n\n cout << ret.size() << endl;\n rep(i,ret.size()){\n cout << ret[i];\n }\n cout << endl;\n\n /*\n sort(ALL(ans));\n cout << ans.size() << endl;\n rep(i,ans.size()){\n cout << ans[i].second;\n }\n cout << endl;\n */\n}\n\n\nmain(){\n int n;\n while(cin>>n && n){\n double r[n];\n Point in[n];\n rep(i,n){\n cin>>in[i].x>>in[i].y>>in[i].z >>r[i];\n }\n solve(n,in,r);\n }\n}", "accuracy": 1, "time_ms": 720, "memory_kb": 1008, "score_of_the_acc": -1.0277, "final_rank": 15 } ]
aoj_1259_cpp
Problem C: Colored Cubes There are several colored cubes. All of them are of the same size but they may be colored differently. Each face of these cubes has a single color. Colors of distinct faces of a cube may or may not be the same. Two cubes are said to be identically colored if some suitable rotations of one of the cubes give identical looks to both of the cubes. For example, two cubes shown in Figure 2 are identically colored. A set of cubes is said to be identically colored if every pair of them are identically colored. A cube and its mirror image are not necessarily identically colored. For example, two cubes shown in Figure 3 are not identically colored. You can make a given set of cubes identically colored by repainting some of the faces, whatever colors the faces may have. In Figure 4, repainting four faces makes the three cubes identically colored and repainting fewer faces will never do. Your task is to write a program to calculate the minimum number of faces that needs to be repainted for a given set of cubes to become identically colored. Input The input is a sequence of datasets. A dataset consists of a header and a body appearing in this order. A header is a line containing one positive integer n and the body following it consists of n lines. You can assume that 1 ≤ n ≤ 4. Each line in a body contains six color names separated by a space. A color name consists of a word or words connected with a hyphen (-). A word consists of one or more lowercase letters. You can assume that a color name is at most 24-characters long including hyphens. A dataset corresponds to a set of colored cubes. The integer n corresponds to the number of cubes. Each line of the body corresponds to a cube and describes the colors of its faces. Color names in a line is ordered in accordance with the numbering of faces shown in Figure 5. A line color 1 color 2 color 3 color 4 color 5 color 6 corresponds to a cube colored as shown in Figure 6. The end of the input is indicated by a line containing a single zero. It is not a dataset nor a part of a dataset. Output For each dataset, output a line containing the minimum number of faces that need to be repainted to make the set of cubes identically colored. Sample Input 3 scarlet green blue yellow magenta cyan blue pink green magenta cyan lemon purple red blue yellow cyan green 2 red green blue yellow magenta cyan cyan green blue yellow magenta red 2 red green gray gray magenta cyan cyan green gray gray magenta red 2 red green blue yellow magenta cyan magenta red blue yellow cyan green 3 red green blue yellow magenta cyan cyan green blue yellow magenta red magenta red blue yellow cyan green 3 blue green green green green blue green blue blue green green green green green green green green sea-green 3 red yellow red yellow red yellow red red yellow yellow red yellow red red red red red red 4 violet violet salmon salmon salmon salmon violet salmon salmon salmon salmon violet violet violet salmon salmon violet violet ...(truncated)
[ { "submission_id": "aoj_1259_9220763", "code_snippet": "#include <algorithm>\n#include <cmath>\n#include <cstdlib>\n#include <iomanip>\n#include <iostream>\n#include <list>\n#include <map>\n#include <numeric>\n#include <queue>\n#include <set>\n#include <stack>\n#include <string>\n#include <unordered_set>\n#include <vector>\n\nusing namespace std;\n\n/* 各回転における各面の行き先\n\t↓数字と面の対応\n\t0 +x\n\t1 +y\n\t2 +z\n\t3 -z\n\t4 -y\n\t5 -x\n*/\nconst int R[24][6]{\n\t// +x -> +x 固定, +x 周りに 90°ずつ回転\n\t{0, 1, 2, 3, 4, 5},\n\t{0, 2, 4, 1, 3, 5},\n\t{0, 4, 3, 2, 1, 5},\n\t{0, 3, 1, 4, 2, 5},\n\t// +x -> -x 固定, -x 周りに 90°ずつ回転\n\t{5, 2, 1, 4, 3, 0},\n\t{5, 1, 3, 2, 4, 0},\n\t{5, 3, 4, 1, 2, 0},\n\t{5, 4, 2, 3, 1, 0},\n\t// +x -> +y 固定, +y 周りに 90°ずつ回転\n\t{1, 2, 0, 5, 3, 4},\n\t{1, 0, 3, 2, 5, 4},\n\t{1, 3, 5, 0, 2, 4},\n\t{1, 5, 2, 3, 0, 4},\n\t// +x -> -y 固定, -y 周りに 90°ずつ回転\n\t{4, 0, 2, 3, 5, 1},\n\t{4, 2, 5, 0, 3, 1},\n\t{4, 5, 3, 2, 0, 1},\n\t{4, 3, 0, 5, 2, 1},\n\t// +x -> +z 固定, +z 周りに 90°ずつ回転\n\t{2, 0, 1, 4, 5, 3},\n\t{2, 1, 5, 0, 4, 3},\n\t{2, 5, 4, 1, 0, 3},\n\t{2, 4, 0, 5, 1, 3},\n\t// +x -> -z 固定, -z 周りに 90°ずつ回転\n\t{3, 1, 0, 5, 4, 2},\n\t{3, 0, 4, 1, 5, 2},\n\t{3, 4, 5, 0, 1, 2},\n\t{3, 5, 1, 4, 0, 2},\n};\n\nvector<string> RotateCube(const vector<string>& cube, const int ri) {\n\tif (ri == 0) return cube;\n\n\tvector<string> rotated_cube(6);\n\tfor (int j = 0; j < 6; j++) {\n\t\trotated_cube[R[ri][j]] = cube[j];\n\t}\n\treturn rotated_cube;\n}\n\nint CountFacesToBeRepainted(const vector<vector<string>>& cubes) {\n\tint count = 0;\n\tfor (int f = 0; f < 6; f++) {\n\t\tmap<string, int> numof_colors;\n\t\tint max_numof_color = 0;\n\t\tfor (vector<string> cube : cubes) {\n\t\t\tnumof_colors[cube[f]]++;\n\t\t\tmax_numof_color = max(max_numof_color, numof_colors[cube[f]]);\n\t\t}\n\n\t\tcount += cubes.size() - max_numof_color;\n\t}\n\n\treturn count;\n}\n\nint main() {\n\tint n;\n\n\twhile (true) {\n\t\tcin >> n;\n\t\tif (n == 0) break;\n\t\tvector<vector<string>> cubes(n, vector<string>(6));\n\t\tfor (vector<string>& cube : cubes) {\n\t\t\tfor (string& color : cube) {\n\t\t\t\tcin >> color;\n\t\t\t}\n\t\t}\n\n\t\tint ri[4]{0};\n\t\tint ri_max[4]{0};\n\t\tfor (int i = 1; i < n; i++) ri_max[i] = 23;\n\n\t\tint minimum = 100;\n\t\tvector<vector<string>> rotated_cubes(n, vector<string>(6));\n\t\trotated_cubes[0] = cubes[0];\n\t\tfor (ri[1] = 0; ri[1] <= ri_max[1]; ri[1]++) {\n\t\t\tfor (ri[2] = 0; ri[2] <= ri_max[2]; ri[2]++) {\n\t\t\t\tfor (ri[3] = 0; ri[3] <= ri_max[3]; ri[3]++) {\n\t\t\t\t\tfor (int i = 1; i < n; i++) {\n\t\t\t\t\t\trotated_cubes[i] = RotateCube(cubes[i], ri[i]);\n\t\t\t\t\t}\n\t\t\t\t\tminimum = min(minimum, CountFacesToBeRepainted(rotated_cubes));\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\n\t\tcout << minimum << endl;\n\t}\n\n\treturn 0;\n}", "accuracy": 1, "time_ms": 670, "memory_kb": 3136, "score_of_the_acc": -0.4013, "final_rank": 14 }, { "submission_id": "aoj_1259_9053614", "code_snippet": "#include <cstdlib>\n#include <cctype>\n#include <cstring>\n#include <cstdio>\n#include <cmath>\n#include <algorithm>\n#include <vector>\n#include <string>\n#include <iostream>\n#include <sstream>\n#include <map>\n#include <set>\n#include <queue>\n#include <stack>\n#include <fstream>\n#include <numeric>\n#include <iomanip>\n#include <bitset>\n#include <list>\n#include <stdexcept>\n#include <functional>\n#include <utility>\n#include <ctime>\n#include <cassert>\nusing namespace std;\ntypedef long long LL;\ntypedef unsigned long long ULL;\n#define MEM(a,b) memset((a),(b),sizeof(a))\nconst LL INF = 1e9 + 7;\nconst int N = 1e2 + 10;\nint a[] = { 0,4,1,2,3,5 };\nint b[] = { 3,0,2,5,4,1 };\nint c[] = { 0,1,2,3,4,5 };\nint vp[][6] =\n{\n\t{0,1,2,3,4,5},\n\t{0,2,3,4,1,5},\n\t{1,5,2,0,4,3},\n\t{0,3,4,1,2,5},\n\t{2,5,3,0,1,4},\n\t{1,2,0,4,5,3},\n\t{5,3,2,1,4,0},\n\t{0,4,1,2,3,5},\n\t{3,5,4,0,2,1},\n\t{2,3,0,1,5,4},\n\t{5,4,3,2,1,0},\n\t{1,0,4,5,2,3},\n\t{5,2,1,4,3,0},\n\t{3,0,2,5,4,1},\n\t{4,5,1,0,3,2},\n\t{3,4,0,2,5,1},\n\t{5,1,4,3,2,0},\n\t{2,0,1,5,3,4},\n\t{4,0,3,5,1,2},\n\t{1,4,5,2,0,3},\n\t{3,2,5,4,0,1},\n\t{4,1,0,3,5,2},\n\t{2,1,5,3,0,4},\n\t{4,3,5,1,0,2}\n};\nint vs[5][6];\nint ans = INF;\nchar str[100];\nint cnt[24];\nint key[10];\nvoid dfs(int cur, int n)\n{\n\tif (cur == n)\n\t{\n\t\tint tot = 0;\n\t\tfor (int i = 0; i < 6; i++)\n\t\t{\n\t\t\tmap<int, int> ms;\n\t\t\tint maxv = 0;\n\t\t\tMEM(cnt, 0);\n\t\t\tfor (int j = 0; j < n; j++)\n\t\t\t{\n\t\t\t\tint* v = vp[key[j]];\n\t\t\t\tint& x = vs[j][v[i]];\n\t\t\t\tmaxv = max(maxv, ++cnt[x]);\n\t\t\t}\n\t\t\ttot += n - maxv;\n\t\t\tif (tot >= ans) break;\n\t\t}\n\t\tans = min(ans, tot);\n\t\treturn;\n\t}\n\tfor (int i = 0; i < 24; i++)\n\t{\n\t\tkey[cur] = i;\n\t\tdfs(cur + 1, n);\n\t}\n}\nint main()\n{\n\t//freopen(\"input.txt\", \"r\", stdin);\n\t//freopen(\"output.txt\", \"w\", stdout);\n\tint n;\n\twhile (scanf(\"%d\", &n) != EOF)\n\t{\n\t\tif (n == 0) break;\n\t\tint a[] = { 1,2,0,5,4,3 };\n\t\tmap<string, int> ms;\n\t\tfor (int i = 0; i < n; i++)\n\t\t{\n\t\t\tint* v = vs[i];\n\t\t\tfor (int j = 0; j < 6; j++)\n\t\t\t{\n\t\t\t\tscanf(\"%s\", str);\n\t\t\t\tif (!ms.count(str))\n\t\t\t\t{\n\t\t\t\t\tint id = ms.size();\n\t\t\t\t\tms[str] = id;\n\t\t\t\t}\n\t\t\t\tv[a[j]] = ms[str];\n\t\t\t}\n\t\t\t// cin >> v[1] >> v[2] >> v[0] >> v[5] >> v[4] >> v[3];\n\t\t}\n\t\tans = INF;\n\t\tdfs(1, n);\n\t\tprintf(\"%d\\n\", ans);\n\t}\n\treturn 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3448, "score_of_the_acc": -0.3247, "final_rank": 6 }, { "submission_id": "aoj_1259_6558748", "code_snippet": "#include <map>\n#include <cstdio>\n#include <cstring>\n#include <iostream>\n#define MAXN 8\nusing namespace std;\n\ninline void min(int &a, int b)\n{\n\tif (a > b) a = b;\n\treturn ;\n}\ninline void max(int &a, int b)\n{\n\tif (a < b) a = b;\n\treturn ;\n}\n\nconst int dice[24][6] =\n{\n\t{1, 2, 3, 4, 5, 6}, {1, 4, 2, 5, 3, 6},\n\t{1, 5, 4, 3, 2, 6}, {1, 3, 5, 2, 4, 6},\n\t{2, 6, 3, 4, 1, 5}, {2, 4, 6, 1, 3, 5},\n\t{2, 1, 4, 3, 6, 5}, {2, 3, 1, 6, 4, 5},\n\t{3, 2, 6, 1, 5, 4}, {3, 1, 2, 5, 6, 4},\n\t{3, 5, 1, 6, 2, 4}, {3, 6, 5, 2, 1, 4},\n\t{4, 2, 1, 6, 5, 3}, {4, 6, 2, 5, 1, 3},\n\t{4, 5, 6, 1, 2, 3}, {4, 1, 5, 2, 6, 3},\n\t{5, 1, 3, 4, 6, 2}, {5, 4, 1, 6, 3, 2},\n\t{5, 6, 4, 3, 1, 2}, {5, 3, 6, 1, 4, 2},\n\t{6, 5, 3, 4, 2, 1}, {6, 4, 5, 2, 3, 1},\n\t{6, 2, 4, 3, 5, 1}, {6, 3, 2, 5, 4, 1}\n};\n\nint a[MAXN][MAXN], method[6], color[26];\nint ans, n, cnt;\nbool zero;\nstring s;\nmap <string, int> mp;\n\nvoid dfs(int dep)\n{\n\tif (dep == n)\n\t{\n\t\tint cc, res = 0;\n\t\tfor (int j = 0; j < 6; j++)\n\t\t{\n\t\t\tcc = 0;\n\t\t\tmemset(color, 0, sizeof color);\n\t\t\tfor (int i = 1; i <= n; i++)\n\t\t\t{\n\t\t\t\t++color[a[i][dice[method[i]][j]]];\n\t\t\t\tmax(cc, color[a[i][dice[method[i]][j]]]);\n\t\t\t}\n\t\t\tres += n - cc;\n\t\t}\n\t\tmin(ans, res);\n\t\tif (!ans) zero = true;\n\t\treturn ;\n\t}\n\tfor (int i = 0; i < 24 && !zero; i++)\n\t{\n\t\tmethod[dep] = i;\n\t\tdfs(dep + 1);\n\t}\n\treturn ;\n}\n\nvoid Solve()\n{\n\tans = ~(1 << 31); zero = false; cnt = 0;\n\tfor (int i = 1; i <= n; i++)\n\t\tfor (int j = 1; j <= 6; j++)\n\t\t{\n\t\t\tcin >> s;\n\t\t\tif (mp.find(s) == mp.end()) mp.insert(make_pair(s, ++cnt));\n\t\t\ta[i][j] = mp[s];\n\t\t}\n\tdfs(1);\n\tcout << ans << '\\n';\n\tmp.clear();\n\treturn ;\n}\n\n\nint main()\n{\n\twhile (cin >> n)\n\t{\n\t\tif (!n) break;\n\t\tSolve();\n\t}\n\treturn 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3376, "score_of_the_acc": -0.3143, "final_rank": 5 }, { "submission_id": "aoj_1259_4777684", "code_snippet": "#define _GLIBCXX_DEBUG\n#include <bits/stdc++.h>\nusing namespace std;\ntypedef long long ll;\n#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)\n#define FOR(i,a,n) for(ll i=a;i<(ll)(n);i++)\n\n//参考,引用: http://judge.u-aizu.ac.jp/onlinejudge/review.jsp?rid=534765#1\n//自力では解けず、かけませんでした。\n\nint n;\nstring dice[4][6]; //4*6の24通り存在する\n\n//diceを回転させる\nvoid rotation(int x, string *dice){\n int r[][4] = {{0,1,5,4},{0,2,5,3},{1,3,4,2}};\n string t = dice[r[x][0]];\n dice[r[x][0]]=dice[r[x][1]];\n dice[r[x][1]]=dice[r[x][2]];\n dice[r[x][2]]=dice[r[x][3]];\n dice[r[x][3]]=t;\n}\n\nint dfs(int r){\n if(r==n){\n int s=0;\n for(int i=0;i<6;i++){\n int m=1;\n for(int j=0;j<n;j++){\n\tint c=0;\n\tfor(int k=0;k<n;k++){\n\t c+=dice[j][i]==dice[k][i];\n\t}\n\tm=max(c,m);\n }\n s+=n-m;\n }\n return s;\n }else{\n int m=99;\n for(int i=0;i<6;i++){\n rotation(i&1,dice[r]);\n for(int j=0;j<4;j++){\n\trotation(2,dice[r]);\n\tm=min(m,dfs(r+1));\n }\n }\n return m;\n }\n}\n\nint main(){\n /*\n\n 考察: ダイスは24通りをとる。最大で、24^4通りあるが、これは十分間に合う。\n dfsで全探索すれば良い。\n\n */\n while(cin>>n, n){\n for(int i=0; i<n; i++){\n for(int j=0; j<6; j++){\n cin>>dice[i][j];\n }\n }\n cout << dfs(0) << endl;\n }\n}", "accuracy": 1, "time_ms": 1890, "memory_kb": 3008, "score_of_the_acc": -0.6076, "final_rank": 16 }, { "submission_id": "aoj_1259_3911791", "code_snippet": "#include<bits/stdc++.h>\n#define ll long long\n#define lp(i, n) for(int i=0;i<(n);++i)\n#define per(i, n) for(int i=(n)-1;i>=0;--i)\n#define lps(i, n) for(int i=1;i<(n);++i)\n#define foreach(i, n) for(auto &i:(n))\n#define pii pair<int, int>\n#define pll pair<long long, long long>\n#define all(x) (x).begin(), (x).end()\n#define bit(x) (1ll << (x))\nconst ll MOD = (ll)1e9+7;\nconst int INF = (ll)1e9+7;\nconst ll INFLL = (ll)1e18;\nusing namespace std;\ntemplate<class t>\nusing vvector = vector<vector<t>>;\ntemplate<class t>\nusing vvvector = vector<vector<vector<t>>>;\ntemplate<class t>\nusing priority_queuer = priority_queue<t, vector<t>, greater<t>>;\ntemplate<class t, class u> bool chmax(t &a, u b){if(a<b){a=b;return true;}return false;}\ntemplate<class t, class u> bool chmin(t &a, u b){if(a>b){a=b;return true;}return false;}\n\nstruct dice{\n string f,r,t,d,l,b;\n dice(){};\n dice(string v1,string v2,string v3,string v4,string v5,string v6){\n f=v1;r=v2;t=v3;d=v4;l=v5;b=v6;\n }\n int same(dice d2){\n int cnt=6;\n if(f==d2.f)cnt--;\n if(r==d2.r)cnt--;\n if(t==d2.t)cnt--;\n if(d==d2.d)cnt--;\n if(l==d2.l)cnt--;\n if(b==d2.b)cnt--;\n return cnt;\n }\n void roll(char c){\n if(c=='s'){\n string x=f;\n f=r;\n r=b;\n b=l;\n l=x;\n return;\n }\n else{\n string x=f;\n f=t;\n t=b;\n b=d;\n d=x;\n }\n }\n};\n\nstring task=\"sssstsssstsssstsssststssssttssss\";\n\nvector<dice> v;\n\nint acnt(){\n vector<string> x;\n int cnt=0;\n x.clear();\n lp(i,v.size()){\n x.push_back(v[i].f);\n }\n sort(x.begin(),x.end());\n if(v.size()==3){\n if(x[0]==x[1]&&x[1]==x[2])cnt+=0;\n else if(x[0]==x[1]||x[1]==x[2])cnt+=1;\n else{\n cnt+=2;\n }\n }\n else{\n if(x[0]==x[1]&&x[1]==x[2]&&x[2]==x[3])cnt+=0;\n else if(x[0]==x[1]&&x[1]==x[2]){\n cnt+=1;\n }\n else if(x[1]==x[2]&&x[2]==x[3]){\n cnt++;\n }\n else if(x[0]==x[1]||x[2]==x[3]||x[1]==x[2])cnt+=2;\n else cnt+=3;\n }\n\n x.clear();\n lp(i,v.size()){\n x.push_back(v[i].r);\n }\n sort(x.begin(),x.end());\n if(v.size()==3){\n if(x[0]==x[1]&&x[1]==x[2])cnt+=0;\n else if(x[0]==x[1]||x[1]==x[2])cnt+=1;\n else{\n cnt+=2;\n }\n }\n else{\n if(x[0]==x[1]&&x[1]==x[2]&&x[2]==x[3])cnt+=0;\n else if(x[0]==x[1]&&x[1]==x[2]){\n cnt+=1;\n }\n else if(x[1]==x[2]&&x[2]==x[3]){\n cnt++;\n }\n else if(x[0]==x[1]||x[2]==x[3]||x[1]==x[2])cnt+=2;\n else cnt+=3;\n }\n \n x.clear();\n lp(i,v.size()){\n x.push_back(v[i].t);\n }\n sort(x.begin(),x.end());\n if(v.size()==3){\n if(x[0]==x[1]&&x[1]==x[2])cnt+=0;\n else if(x[0]==x[1]||x[1]==x[2])cnt+=1;\n else{\n cnt+=2;\n }\n }\n else{\n if(x[0]==x[1]&&x[1]==x[2]&&x[2]==x[3])cnt+=0;\n else if(x[0]==x[1]&&x[1]==x[2]){\n cnt+=1;\n }\n else if(x[1]==x[2]&&x[2]==x[3]){\n cnt++;\n }\n else if(x[0]==x[1]||x[2]==x[3]||x[1]==x[2])cnt+=2;\n else cnt+=3;\n }\nx.clear();\n lp(i,v.size()){\n x.push_back(v[i].d);\n }\n sort(x.begin(),x.end());\n if(v.size()==3){\n if(x[0]==x[1]&&x[1]==x[2])cnt+=0;\n else if(x[0]==x[1]||x[1]==x[2])cnt+=1;\n else{\n cnt+=2;\n }\n }\n else{\n if(x[0]==x[1]&&x[1]==x[2]&&x[2]==x[3])cnt+=0;\n else if(x[0]==x[1]&&x[1]==x[2]){\n cnt+=1;\n }\n else if(x[1]==x[2]&&x[2]==x[3]){\n cnt++;\n }\n else if(x[0]==x[1]||x[2]==x[3]||x[1]==x[2])cnt+=2;\n else cnt+=3;\n }\nx.clear();\n lp(i,v.size()){\n x.push_back(v[i].l);\n }\n sort(x.begin(),x.end());\n if(v.size()==3){\n if(x[0]==x[1]&&x[1]==x[2])cnt+=0;\n else if(x[0]==x[1]||x[1]==x[2])cnt+=1;\n else{\n cnt+=2;\n }\n }\n else{\n if(x[0]==x[1]&&x[1]==x[2]&&x[2]==x[3])cnt+=0;\n else if(x[0]==x[1]&&x[1]==x[2]){\n cnt+=1;\n }\n else if(x[1]==x[2]&&x[2]==x[3]){\n cnt++;\n }\n else if(x[0]==x[1]||x[2]==x[3]||x[1]==x[2])cnt+=2;\n else cnt+=3;\n }\nx.clear();\n lp(i,v.size()){\n x.push_back(v[i].b);\n }\n sort(x.begin(),x.end());\n if(v.size()==3){\n if(x[0]==x[1]&&x[1]==x[2])cnt+=0;\n else if(x[0]==x[1]||x[1]==x[2])cnt+=1;\n else{\n cnt+=2;\n }\n }\n else{\n if(x[0]==x[1]&&x[1]==x[2]&&x[2]==x[3])cnt+=0;\n else if(x[0]==x[1]&&x[1]==x[2]){\n cnt+=1;\n }\n else if(x[1]==x[2]&&x[2]==x[3]){\n cnt++;\n }\n else if(x[0]==x[1]||x[2]==x[3]||x[1]==x[2])cnt+=2;\n else cnt+=3;\n }\n return cnt;\n}\n\nint solve(){\n if(v.size()==1)return 0;\n if(v.size()==2){\n int ans=INF;\n lp(i,task.size()){\n v[0].roll(task[i]);\n ans=min(ans,v[0].same(v[1]));\n }\n return ans;\n }\n if(v.size()==3){\n int ans=INF;\n lp(i,task.size()){\n v[0].roll(task[i]);\n lp(j,task.size()){\n\tv[1].roll(task[j]);\n\tans=min(ans,acnt());\n }\n }\n return ans;\n }\n else{\n int ans=INF;\n lp(i,task.size()){\n v[0].roll(task[i]);\n lp(j,task.size()){\n\tv[1].roll(task[j]);\n\tlp(k,task.size()){\n\t v[2].roll(task[k]);\n\t ans=min(ans,acnt());\n\t}\n }\n }\n return ans;\n }\n return -1;\n}\n\nint main(){\n while(1){\n int n;\n cin>>n;\n if(n==0)break;\n v.clear();\n lp(i,n){\n string a,b,c,d,e,f;\n cin>>a>>b>>c>>d>>e>>f;\n dice k(a,b,c,d,e,f);\n v.push_back(k);\n }\n int ans=INF;\n cout<<solve()<<endl;\n }\n return 0;\n}", "accuracy": 1, "time_ms": 430, "memory_kb": 3164, "score_of_the_acc": -0.3612, "final_rank": 12 }, { "submission_id": "aoj_1259_3911705", "code_snippet": "#include <bits/stdc++.h>\n#define r(i,n) for(int i=0;i<n;i++)\nusing namespace std;\n\nstruct Dice{\n int s[6];\n void roll(char c){\n int b;\n\n if(c=='S'){\n b=s[0];\n s[0]=s[4];\n s[4]=s[5];\n s[5]=s[1];\n s[1]=b;\n }\n if(c=='E'){\n b=s[0];\n s[0]=s[3];\n s[3]=s[5];\n s[5]=s[2];\n s[2]=b;\n }\n if(c=='R'){\n b=s[1];\n s[1]=s[3];\n s[3]=s[4];\n s[4]=s[2];\n s[2]=b;\n }\n if(c=='L'){\n b=s[1];\n s[1]=s[2];\n s[2]=s[4];\n s[4]=s[3];\n s[3]=b;\n }\n }\n};\n\nDice D[6];\nstring s[10][10];\nint a[10][10];\n\nint n,ans;\nint used[25];\n\nvoid dfs(int dep){\n if(dep==n){\n int T=n*6;\n r(i,6){\n memset(used,0,sizeof(used));\n int MAX=0;\n r(j,n){\n\tused[D[j].s[i]]++;\n\tMAX=max(MAX,used[D[j].s[i]]);\n }\n T-=MAX;\n }\n ans=min(T,ans);\n return ;\n }\n r(k,2){\n D[dep].roll('E');\n r(i,4){\n D[dep].roll('S');\n r(j,4){\n\tD[dep].roll('R');\n\tdfs(dep+1);\n }\n }\n }\n}\n\nint main(){\n while( cin>>n , n ){\n ans=1e9;\n vector<string>v;\n r(i,n){\n r(j,6){\n\tcin>>s[i][j];\n\tv.push_back(s[i][j]);\n }\n }\n sort(v.begin(),v.end());\n v.erase(unique(v.begin(),v.end()),v.end());\n map<string,int>M;\n r(i,v.size()){\n M[v[i]]=i;\n }\n r(i,n){\n r(j,6){\n\ta[i][j]=M[s[i][j]];\n\tD[i].s[j]=a[i][j];\n }\n }\n dfs(0);\n cout<<ans<<endl;\n }\n}", "accuracy": 1, "time_ms": 1400, "memory_kb": 3188, "score_of_the_acc": -0.5433, "final_rank": 15 }, { "submission_id": "aoj_1259_3176869", "code_snippet": "#include<iostream>\n#include<string>\n#include<map>\nusing namespace std;\n#define int long long\n#define rep(i,n) for(int i = 0; i < n; i++)\n#define INF (long long)(1e18)\n#define MOD (int)(1e9+7)\n#define MAX_N 103\n#define MAX_V 10\n#define MAX_M 101\n#define yn(f) (f?\"Yes\":\"No\")\n#define YN(f) (f?\"YES\":\"NO\")\n#define pro \"はいプロ 世界一○○が上手 ○○界のtourist ○○時代の終焉を告げる者 実質○○ ○○するために生まれてきた男\"\n\nstruct Cube{\n\tstring co[10];\n};\n\n\nCube huga(Cube b){\n\tCube a = b;\n\tstring temp = a.co[1];\n\ta.co[1] = a.co[5];\n\ta.co[5] = a.co[6];\n\ta.co[6] = a.co[2]; \n\ta.co[2] = temp;\n\treturn a;\n}\n\nCube hoge(Cube b){\n\tCube a = b;\n\tstring temp = a.co[1];\n\ta.co[1] = a.co[4];\n\ta.co[4] = a.co[6];\n\ta.co[6] = a.co[3];\n\ta.co[3] = temp;\n\treturn a;\n}\n\nCube hugahuga(Cube b){\n\tCube a = b;\n\tstring temp = a.co[3];\n\ta.co[3] = a.co[5];\n\ta.co[5] = a.co[4];\n\ta.co[4] = a.co[2]; \n\ta.co[2] = temp;\n\treturn a;\n}\n\nCube foo(Cube c, int b){\n\tCube a = c;\n\tswitch(b){\n\tcase 1:\n\t\tbreak;\n\tcase 2:\n\t\ta=huga(a);a=huga(a);a=huga(a);\n\t\tbreak;\n\tcase 3:\n\t\ta=hoge(a); a=hoge(a);a=hoge(a);\n\t\tbreak;\n\tcase 4:\n\t\ta=hoge(a);\n\t\tbreak;\n\tcase 5:\n\t\ta=huga(a);\n\t\tbreak;\n\tcase 6:\n\t\ta=huga(a);a=huga(a);\n\t\tbreak;\n\t}\n\treturn a;\n}\n\nint cou(Cube a,Cube b){\n\tint count = 0;\n\tfor(int i = 1; i <= 6; i++){\n\t\tif(a.co[i] != b.co[i])count++;\n\t}\n\treturn count;\n}\n\nint ans(int cc[10][10],int n,int used[],int k){\n\tint count = 0;\n\tfor(int i = 1; i <= n; i++) count+=cc[k][i];\n\treturn count;\n}\n\nint ha(Cube a[], int n){//cout<<\" a \"<<endl;\n\tint ans = 0;\n\tfor(int i = 1; i <= 6; i++){\n\t\tint ma = 0;\n\t\tmap<string,int> m;\n\t\tfor(int j = 1; j <= n; j++){\n\t\t\tma = max(ma, ++m[a[j].co[i]]);\n\t\t}\n\t\tans += n-ma;\n\t}\n\t\n\treturn ans;\n}\n\nint so(Cube a[], int n, int k){//cout<<k<<\" \"<<n<<endl;\n\tCube tt = a[k];\n\tint ans = INF;\n\t//if(n+1== k) return ha(a,n);\n\tfor(int i = 1; i <= 6; i++){\n\t\tCube temp = foo(tt,i);\n\t\tfor(int j = 1; j <= 4;j++){\n\t\t\ttemp = hugahuga(temp);\n\t\t\ta[k] = temp;\n\t\t\tif(k<n-1)ans = min(ans,so(a,n,k+1));\n\t\t\telse ans = min(ans,ha(a,n));\n\t\t}\n\t}\n\treturn ans;\n}\n\nsigned main(){\n\twhile(true){\n\t\tint n;\n\t\tCube s[5];\n\t\tcin>>n;\n\t\tif(!n)break;\n\n\t\tfor(int i = 1; i <= n; i++)\n\t\t\tfor(int j = 1; j <= 6; j++)\n\t\t\t\tcin>>s[i].co[j];\n\t\n\t\tcout<<so(s,n,1)<<endl;\n\t}\n\n\treturn 0;\n}", "accuracy": 1, "time_ms": 390, "memory_kb": 3028, "score_of_the_acc": -0.3342, "final_rank": 8 }, { "submission_id": "aoj_1259_3176643", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nusing Int = long long;\n\ntemplate<typename T1,typename T2> void chmin(T1 &a,T2 b){if(a>b)a=b;};\ntemplate<typename T1,typename T2> void chmax(T1 &a,T2 b){if(a<b)a=b;};\n\nstruct Dice{\n Int s[6];\n void roll(char c){\n Int b;\n if(c == 'E'){\n b = s[0];\n s[0] = s[3];\n s[3] = s[5];\n s[5] = s[2];\n s[2] = b;\n }\n\n if(c == 'W'){\n b = s[0];\n s[0] = s[2];\n s[2] = s[5];\n s[5] = s[3];\n s[3] = b;\n }\n\n if(c == 'N'){\n b = s[0];\n s[0] = s[1];\n s[1] = s[5];\n s[5] = s[4];\n s[4] = b;\n }\n\n if(c == 'S'){\n b = s[0];\n s[0] = s[4];\n s[4] = s[5];\n s[5] = s[1];\n s[1] = b;\n }\n\n if(c == 'R'){\n b= s[1];\n s[1] = s[2];\n s[2] = s[4];\n s[4] = s[3];\n s[3] = b;\n }\n \n if(c == 'L'){\n b = s[1];\n s[1] = s[3];\n s[3] = s[4];\n s[4] = s[2];\n s[2] = b;\n }\n }\n};\n\n\nvector<Dice> makeDices(Dice d){\n vector<Dice> res;\n for(Int i=0;i<6;i++){\n Dice t(d);\n if(i == 1) t.roll('N');\n if(i == 2) t.roll('S');\n if(i == 3) t.roll('S'), t.roll('S');\n if(i == 4) t.roll('L');\n if(i == 5) t.roll('R');\n for(Int k=0;k<4;k++){\n res.push_back(t);\n t.roll('E');\n }\n }\n return res;\n}\n\nsigned main(){\n cin.tie(0);\n ios::sync_with_stdio(0);\n Int n;\n while(cin>>n,n){\n vector<Dice> vs;\n Int sz=0;\n map<string, Int> cm;\n for(Int i=0;i<n;i++){\n vs.emplace_back();\n for(Int j=0;j<6;j++){\n\tstring s;\n\tcin>>s;\n\tif(!cm.count(s)) cm[s]=sz++;\n\tvs[i].s[j]=cm[s];\n } \n }\n \n if(n==1){\n cout<<0<<endl;\n }else if(n==2){\n auto A = makeDices(vs[0]);\n auto B = makeDices(vs[1]);\n Int ans=1e9; \n for(auto &a:A){\n\tfor(auto &b:B){\n\t Int res=0;\n\t for(Int j=0;j<6;j++){\n\t map<Int, Int> x;\n\t x[a.s[j]]++;\n\t x[b.s[j]]++;\n\t Int y=0;\n\t for(auto p:x) chmax(y,p.second);\n\t res+=2-y;\n\t }\t \n\t chmin(ans,res);\n\t}\n } \n cout<<ans<<endl;\n }else if(n==3){\n auto A = makeDices(vs[0]);\n auto B = makeDices(vs[1]);\n auto C = makeDices(vs[2]);\n Int ans=1e9;\n \n for(auto &a:A){\n\tfor(auto &b:B){\n\t for(auto &c:C){\n\t Int res=0;\n\t for(Int j=0;j<6;j++){\n\t map<Int, Int> x;\n\t x[a.s[j]]++;\n\t x[b.s[j]]++;\n\t x[c.s[j]]++;\n\t Int y=0;\n\t for(auto p:x) chmax(y,p.second);\n\t res+=3-y;\n\t }\t \n\t chmin(ans,res);\n\t }\n\t}\n }\n cout<<ans<<endl;\n }else{\n auto A = makeDices(vs[0]);\n auto B = makeDices(vs[1]);\n auto C = makeDices(vs[2]);\n auto D = makeDices(vs[3]);\n Int ans=1e9; \n for(auto &a:A){\n\tfor(auto &b:B){\n\t for(auto &c:C){\n\t for(auto &d:D){\n\t Int res=0;\n\t for(Int j=0;j<6;j++){\n\t\tmap<Int, Int> x;\n\t\tx[a.s[j]]++;\n\t\tx[b.s[j]]++;\n\t\tx[c.s[j]]++;\n\t\tx[d.s[j]]++;\n\t\tInt y=0;\n\t\tfor(auto p:x) chmax(y,p.second);\n\t\tres+=4-y;\n\t }\t \n\t chmin(ans,res);\n\t }\n\t }\n\t}\n }\n cout<<ans<<endl;\n } \n }\n}", "accuracy": 1, "time_ms": 4700, "memory_kb": 3096, "score_of_the_acc": -1.1378, "final_rank": 18 }, { "submission_id": "aoj_1259_3176079", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\nclass Dice{\n \npublic:\n int TOP = 2, FRONT = 0, LEFT = 4, RIGHT = 1, BACK = 5, BOTTOM = 3;\n vector<int> val;\n Dice():val(6){for(int i=0;i<6;i++) val[i] = i;}\n Dice(int val[6]){for(int i=0;i<6;i++) this->val[i] = val[i];}\n Dice(vector<int> val):val(val){assert(val.size()==6);}\n int& operator [](int a){return val[a];}\n \n void rotN(){\n swap(val[TOP],val[FRONT]);\n swap(val[FRONT],val[BOTTOM]);\n swap(val[BOTTOM],val[BACK]);\n }\n \n void rotS(){\n swap(val[TOP],val[BACK]);\n swap(val[BACK],val[BOTTOM]);\n swap(val[BOTTOM],val[FRONT]);\n }\n \n void rotE(){\n swap(val[TOP],val[LEFT]);\n swap(val[LEFT],val[BOTTOM]);\n swap(val[BOTTOM],val[RIGHT]);\n }\n \n void rotW(){\n swap(val[TOP],val[RIGHT]);\n swap(val[RIGHT],val[BOTTOM]);\n swap(val[BOTTOM],val[LEFT]);\n }\n \n void rotCW(){\n swap(val[FRONT],val[RIGHT]);\n swap(val[RIGHT],val[BACK]);\n swap(val[BACK],val[LEFT]);\n }\n \n void rotCCW(){\n swap(val[FRONT],val[LEFT]);\n swap(val[LEFT],val[BACK]);\n swap(val[BACK],val[RIGHT]);\n }\n \n};\n\nDice dice[4][30];\nint N, ans;\nint ID[4];\n\nvoid cal(){\n \n int cnt = 0;\n \n for(int i=0;i<6;i++){\n \n unordered_map<int,int> A;\n \n for(int j=0;j<N;j++) A[dice[j][ID[j]][i]]++;\n \n int maxc = 0;\n \n for(auto p : A ) maxc = max( maxc, p.second );\n \n cnt += N - maxc;\n \n }\n \n ans = min( ans, cnt );\n \n}\n\nvoid dfs(int x){\n \n if( x == N ){\n cal();\n return;\n }\n \n for(int i=0;i<24;i++){\n ID[x] = i;\n dfs( x + 1 );\n }\n \n}\n\nint main(){\n \n while(1){\n \n cin>>N;\n if( N == 0 ) break;\n \n vector<string> d[4];\n map<string,int> memo;\n int idx = 0;\n \n for(int i=0;i<N;i++){\n \n for(int j=0;j<6;j++){\n\tstring s;\n\tcin>>s;\n\td[i].push_back(s);\n\tif( memo.count(s) == 0 ) memo[s] = idx++;\n }\n \n }\n \n Dice di[4];\n \n for(int i=0;i<N;i++){\n \n vector<int> A;\n \n for(int j=0;j<6;j++){\n\t\n\tA.push_back( memo[d[i][j]] );\n\t\n }\n \n di[i] = Dice(A);\n \n }\n \n for(int I=0;I<N;I++){\n \n int idx = 0;\n \n for(int i=0;i<4;i++){\n \n\tfor(int j=0;j<4;j++){\n\t \n\t dice[I][idx++] = di[I];\n \n\t di[I].rotCW();\n \n\t}\n \n\tdi[I].rotN();\n\t\n }\n\n for(int i=0;i<4;i++){\n \n\tif(i%2){\n \n\t for(int j=0;j<4;j++){\n \n\t dice[I][idx++] = di[I];\n \n\t di[I].rotCW();\n \n\t }\n \n\t}\n\t\n\tdi[I].rotE();\n\t\n }\n \n \n }\n \n for(int i=0;i<N;i++){\n \n vector<int> A;\n \n for(int j=0;j<6;j++){\n\t\n\tA.push_back( memo[d[i][j]] );\n\t\n }\n \n di[i] = Dice(A);\n \n }\n \n ans = 100;\n \n dfs( 1 );\n \n cout<<ans<<endl;\n \n }\n \n return 0;\n}", "accuracy": 1, "time_ms": 370, "memory_kb": 3156, "score_of_the_acc": -0.349, "final_rank": 11 }, { "submission_id": "aoj_1259_3175986", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\n\ntypedef long long ll;\n\nstruct cube{\n string a[6];\n\n int count=1;\n \n void move_N(){\n string b[6]=a;\n a[0]=b[1];\n a[1]=b[2];\n a[2]=b[3];\n a[3]=b[0];\n }\n\n void move_E(){\n string b[6]=a;\n a[4]=b[1];\n a[1]=b[5];\n a[5]=b[3];\n a[3]=b[4];\n }\n\n void move_R(){\n string b[6]=a;\n a[4]=b[2];\n a[2]=b[5];\n a[5]=b[0];\n a[0]=b[4];\n }\n\n bool operator == (const cube &C){return C.a==a;}\n\n void rotate(){\n move_R();\n if(count%4){}\n else if(count/4<4){\n move_N();\n }\n else if(count/4==4){\n move_E();\n }\n else if(count/4==5){\n move_E();\n move_E();\n }\n count++;\n }\n\n cube(vector<string> &A){\n a[0]=A[5];\n a[1]=A[2];\n a[2]=A[0];\n a[3]=A[3];\n a[4]=A[1];\n a[5]=A[4];\n }\n\n cube(){}\n\n void init(vector<string> &A){\n for(int i=0;i<6;i++){a[i]=A[i];}\n }\n\n void operator = (const cube &C){\n for(int i=0;i<6;i++){a[i]=C.a[i];}\n count=C.count;\n }\n};\n\nvector<cube> C(6);\nll n;\nll mi=1e18;\n\nll count(){\n ll cost=0;\n for(int i=0;i<6;i++){\n map<string,ll> M;\n ll mx=0;\n for(int t=0;t<n;t++){\n M[C[t].a[i]]++;\n mx=max(mx,M[C[t].a[i]]);\n }\n cost+=n-mx;\n }\n return cost;\n}\n\nvoid dfs(ll d){\n C[d].count=1;\n if(d==n-1){\n for(int i=0;i<24;i++){\n mi=min(mi,count());\n C[d].rotate();\n }\n return;\n }\n else{\n for(int i=0;i<24;i++){\n dfs(d+1);\n C[d].rotate();\n }\n }\n}\n\nint main(){\n while(cin>>n && n){\n mi=1e9;\n vector<vector<string>> a(n,vector<string>(6));\n for(int i=0;i<n;i++){\n for(int t=0;t<6;t++){\n cin>>a[i][t];\n }\n }\n if(n==1){cout<<0<<endl; continue;}\n for(int i=0;i<n;i++){C[i]=cube(a[i]);}\n dfs(1);\n cout<<mi<<endl;\n \n }\n return 0;\n}", "accuracy": 1, "time_ms": 440, "memory_kb": 3156, "score_of_the_acc": -0.3619, "final_rank": 13 }, { "submission_id": "aoj_1259_3175943", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nusing Int = long long;\n\ntemplate<typename T1,typename T2> void chmin(T1 &a,T2 b){if(a>b)a=b;};\ntemplate<typename T1,typename T2> void chmax(T1 &a,T2 b){if(a<b)a=b;};\n\nstruct Dice{\n Int s[6];\n void roll(char c){\n Int b;\n if(c == 'E'){\n b = s[0];\n s[0] = s[3];\n s[3] = s[5];\n s[5] = s[2];\n s[2] = b;\n }\n\n if(c == 'W'){\n b = s[0];\n s[0] = s[2];\n s[2] = s[5];\n s[5] = s[3];\n s[3] = b;\n }\n\n if(c == 'N'){\n b = s[0];\n s[0] = s[1];\n s[1] = s[5];\n s[5] = s[4];\n s[4] = b;\n }\n\n if(c == 'S'){\n b = s[0];\n s[0] = s[4];\n s[4] = s[5];\n s[5] = s[1];\n s[1] = b;\n }\n\n if(c == 'R'){\n b= s[1];\n s[1] = s[2];\n s[2] = s[4];\n s[4] = s[3];\n s[3] = b;\n }\n \n if(c == 'L'){\n b = s[1];\n s[1] = s[3];\n s[3] = s[4];\n s[4] = s[2];\n s[2] = b;\n }\n }\n};\n\n\nvector<Dice> makeDices(Dice d){\n vector<Dice> res;\n for(Int i=0;i<6;i++){\n Dice t(d);\n if(i == 1) t.roll('N');\n if(i == 2) t.roll('S');\n if(i == 3) t.roll('S'), t.roll('S');\n if(i == 4) t.roll('L');\n if(i == 5) t.roll('R');\n for(Int k=0;k<4;k++){\n res.push_back(t);\n t.roll('E');\n }\n }\n return res;\n}\n\nsigned main(){\n cin.tie(0);\n ios::sync_with_stdio(0);\n Int n;\n while(cin>>n,n){\n vector<Dice> vs;\n Int sz=0;\n map<string, Int> cm;\n for(Int i=0;i<n;i++){\n vs.emplace_back();\n for(Int j=0;j<6;j++){\n\tstring s;\n\tcin>>s;\n\tif(!cm.count(s)) cm[s]=sz++;\n\tvs[i].s[j]=cm[s];\n } \n }\n \n if(n==1){\n cout<<0<<endl;\n }else if(n==2){\n auto A = makeDices(vs[0]);\n auto B = makeDices(vs[1]);\n Int ans=1e9; \n for(auto &a:A){\n\tfor(auto &b:B){\n\t Int res=0;\n\t for(Int j=0;j<6;j++){\n\t map<Int, Int> x;\n\t x[a.s[j]]++;\n\t x[b.s[j]]++;\n\t Int y=0;\n\t for(auto p:x) chmax(y,p.second);\n\t res+=2-y;\n\t }\t \n\t chmin(ans,res);\n\t}\n } \n cout<<ans<<endl;\n }else if(n==3){\n auto A = makeDices(vs[0]);\n auto B = makeDices(vs[1]);\n auto C = makeDices(vs[2]);\n Int ans=1e9;\n \n for(auto &a:A){\n\tfor(auto &b:B){\n\t for(auto &c:C){\n\t Int res=0;\n\t for(Int j=0;j<6;j++){\n\t map<Int, Int> x;\n\t x[a.s[j]]++;\n\t x[b.s[j]]++;\n\t x[c.s[j]]++;\n\t Int y=0;\n\t for(auto p:x) chmax(y,p.second);\n\t res+=3-y;\n\t }\t \n\t chmin(ans,res);\n\t }\n\t}\n }\n cout<<ans<<endl;\n }else{\n auto A = makeDices(vs[0]);\n auto B = makeDices(vs[1]);\n auto C = makeDices(vs[2]);\n auto D = makeDices(vs[3]);\n Int ans=1e9; \n for(auto &a:A){\n\tfor(auto &b:B){\n\t for(auto &c:C){\n\t for(auto &d:D){\n\t Int res=0;\n\t for(Int j=0;j<6;j++){\n\t\tmap<Int, Int> x;\n\t\tx[a.s[j]]++;\n\t\tx[b.s[j]]++;\n\t\tx[c.s[j]]++;\n\t\tx[d.s[j]]++;\n\t\tInt y=0;\n\t\tfor(auto p:x) chmax(y,p.second);\n\t\tres+=4-y;\n\t }\t \n\t chmin(ans,res);\n\t }\n\t }\n\t}\n }\n cout<<ans<<endl;\n } \n }\n}", "accuracy": 1, "time_ms": 5440, "memory_kb": 3100, "score_of_the_acc": -1.2746, "final_rank": 19 }, { "submission_id": "aoj_1259_3048369", "code_snippet": "#include<iostream>\n#include<algorithm>\n#include<string>\n#include<map>\n#include<vector>\n#include<complex>\n#include<sstream>\n#include<cstdio>\n#include<cstdlib>\n#include<cmath>\n#include<cctype>\n#include<cstring>\nusing namespace std;\n \n#define REP(i,x,n) for(int i = x ; i < (int)(n) ; i++)\n#define rep(i,n) REP(i, 0, n)\n \nstruct Dice{\n //top, front, right, left, back, bottom\n string side[6];\n Dice(){}\n Dice(string s[]){\n rep(i,6) side[i] = s[i];\n }\n \n void rotate(int op){\n string tmp = \"\";\n //右に倒す\n if(op==0){\n tmp = side[0];\n side[0] = side[3];\n side[3] = side[5];\n side[5] = side[2];\n side[2] = tmp;\n }\n \n //前に倒す\n if(op==1){\n tmp = side[0];\n side[0] = side[4];\n side[4] = side[5];\n side[5] = side[1];\n side[1] = tmp;\n }\n \n //左に倒す\n if(op==2){\n tmp = side[0];\n side[0] = side[2];\n side[2] = side[5];\n side[5] = side[3];\n side[3] = tmp;\n }\n \n //後ろに倒す\n if(op==3){\n tmp = side[0];\n side[0] = side[1];\n side[1] = side[5];\n side[5] = side[4];\n side[4] = tmp;\n }\n \n //topとbottomを軸に右回転\n if(op==4){\n tmp = side[1];\n side[1] = side[2];\n side[2] = side[4];\n side[4] = side[3];\n side[3] = tmp;\n }\n \n //topとbottomを軸に左回転\n if(op==5){\n tmp = side[1];\n side[1] = side[3];\n side[3] = side[4];\n side[4] = side[2];\n side[2] = tmp;\n }\n }\n};\n \nint n;\nint ans;\nDice dice[4];\n \nvoid solve(){\n int res = 0;\n \n rep(j,6){\n if(ans <= res) break;\n vector<string> s;\n rep(i,n) s.push_back(dice[i].side[j]);\n sort(s.begin(), s.end());\n if(n==2){\n if(s[0] != s[1]) res++;\n }\n if(n==3){\n if(s[0] != s[1] && s[1] != s[2]) res+=2;\n else if(s[0] != s[1] || s[1] != s[2]) res++;\n }\n if(n==4){\n if(s[0] != s[1] && s[1] != s[2] && s[2] != s[3]) res+=3;\n else if(s[0] == s[1] && s[1] != s[2] && s[2] != s[3]) res+=2;\n else if(s[0] != s[1] && s[1] == s[2] && s[2] != s[3]) res+=2;\n else if(s[0] != s[1] && s[1] != s[2] && s[2] == s[3]) res+=2;\n else if(s[0] == s[1] && s[1] != s[2] && s[2] == s[3]) res+=2;\n else if(s[0] == s[1] && s[1] == s[2] && s[2] != s[3]) res+=1;\n else if(s[0] != s[1] && s[1] == s[2] && s[2] == s[3]) res+=1;\n }\n }\n \n ans = min(ans, res);\n}\n \nvoid dfs(int idx){\n if(idx==n) solve();\n else{\n rep(i,24){\n if(i==4) dice[idx].rotate(1);\n if(i==8) dice[idx].rotate(1);\n if(i==12)dice[idx].rotate(1);\n if(i==16){\n dice[idx].rotate(1);\n dice[idx].rotate(0);\n }\n if(i==20){\n dice[idx].rotate(2);\n dice[idx].rotate(2);\n }\n \n dice[idx].rotate(4);\n dfs(idx + 1);\n }\n }\n}\n \nint main(){\n while(cin >> n && n){\n rep(i,n){\n rep(j,6){\n cin >> dice[i].side[j];\n }\n }\n ans = 6 * n;\n if(n > 1) dfs(1);\n else ans = 0;\n cout << ans << endl;\n }\n return 0;\n}", "accuracy": 1, "time_ms": 260, "memory_kb": 3200, "score_of_the_acc": -0.335, "final_rank": 9 }, { "submission_id": "aoj_1259_2952435", "code_snippet": "#include <bits/stdc++.h>\n#include <unordered_map>\nusing namespace std;\n\nusing VS = vector<string>; using LL = long long;\nusing VI = vector<int>; using VVI = vector<VI>;\nusing PII = pair<int, int>; using PLL = pair<LL, LL>;\nusing VL = vector<LL>; using VVL = vector<VL>;\n\n#define ALL(a) begin((a)),end((a))\n#define RALL(a) (a).rbegin(), (a).rend()\n#define SZ(a) int((a).size())\n#define SORT(c) sort(ALL((c)))\n#define RSORT(c) sort(RALL((c)))\n#define UNIQ(c) (c).erase(unique(ALL((c))), end((c)))\n#define FOR(i, s, e) for (int(i) = (s); (i) < (e); (i)++)\n#define FORR(i, s, e) for (int(i) = (s); (i) > (e); (i)--)\n#define debug(x) cerr << #x << \": \" << x << endl\nconst int INF = 1e9; const LL LINF = 1e16;\nint DX[8] = { 0, 0, 1, -1, 1, 1, -1, -1 }; int DY[8] = { 1, -1, 0, 0, 1, -1, 1, -1 };\n\n/* ----- 2018/06/20 Problem: AOJ 2703 / Link: http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=2703 ----- */\n/* ------問題------\n\n\n\n-----問題ここまで----- */\n/* -----解説等-----\n\n\n\n----解説ここまで---- */\n\n\nusing uLL = unsigned long long;\n\nconst uLL MOD = 1e9;\n//BEGIN CUT HERE\nstruct Dice {\n\tint s[6];\n\tvoid roll(char c) {\n\t\t//the view from above\n\t\t// N\n\t\t//W E\n\t\t// S\n\t\t//s[0]:top\n\t\t//s[1]:south\n\t\t//s[2]:east\n\t\t//s[3]:west\n\t\t//s[4]:north\n\t\t//s[5]:bottom\n\t\tint b;\n\t\tif (c == 'E') {\n\t\t\tb = s[0];\n\t\t\ts[0] = s[3];\n\t\t\ts[3] = s[5];\n\t\t\ts[5] = s[2];\n\t\t\ts[2] = b;\n\t\t}\n\t\tif (c == 'W') {\n\t\t\tb = s[0];\n\t\t\ts[0] = s[2];\n\t\t\ts[2] = s[5];\n\t\t\ts[5] = s[3];\n\t\t\ts[3] = b;\n\t\t}\n\t\tif (c == 'N') {\n\t\t\tb = s[0];\n\t\t\ts[0] = s[1];\n\t\t\ts[1] = s[5];\n\t\t\ts[5] = s[4];\n\t\t\ts[4] = b;\n\t\t}\n\t\tif (c == 'S') {\n\t\t\tb = s[0];\n\t\t\ts[0] = s[4];\n\t\t\ts[4] = s[5];\n\t\t\ts[5] = s[1];\n\t\t\ts[1] = b;\n\t\t}\n\n\t\t// migi neji \n\t\tif (c == 'R') {\n\t\t\tb = s[1];\n\t\t\ts[1] = s[2];\n\t\t\ts[2] = s[4];\n\t\t\ts[4] = s[3];\n\t\t\ts[3] = b;\n\t\t}\n\n\t\tif (c == 'L') {\n\t\t\tb = s[1];\n\t\t\ts[1] = s[3];\n\t\t\ts[3] = s[4];\n\t\t\ts[4] = s[2];\n\t\t\ts[2] = b;\n\t\t}\n\n\t}\n\tint top() {\n\t\treturn s[0];\n\t}\n\tint bottom() {\n\t\treturn s[5];\n\t}\n\tuLL hash() {\n\t\tuLL res = 1;\n\t\tfor (int i = 0; i < 6; i++) res = res * (MOD)+s[i];\n\t\treturn res;\n\t}\n\tvoid print() {\n\t\tcout << \"Dice num: \";\n\t\tFOR(i, 0, 6) {\n\t\t\tcout << s[i] << \" \";\n\t\t}\n\t\tcout << \"Hash: \" << this->hash() << endl;\n\t\tcout << endl;\n\t}\n};\nvector<Dice> makeDices(Dice d) {\n\tvector<Dice> res;\n\tfor (int i = 0; i < 6; i++) {\n\t\tDice t(d);\n\t\tif (i == 1) t.roll('N');\n\t\tif (i == 2) t.roll('S');\n\t\tif (i == 3) t.roll('S'), t.roll('S');\n\t\tif (i == 4) t.roll('L');\n\t\tif (i == 5) t.roll('R');\n\t\tfor (int k = 0; k < 4; k++) {\n\t\t\tres.push_back(t);\n\t\t\tt.roll('E');\n\t\t}\n\t}\n\treturn res;\n}\n//END CUT HERE\n\nvoid solve_AOJ_ITP1_11_A() {\n\tint N = 6;\n\tDice D;\n\tFOR(i, 0, N) {\n\t\tint val; cin >> val;\n\t\tD.s[i] = val;\n\t}\n\tstring s; cin >> s;\n\tFOR(i, 0, SZ(s)) {\n\t\tD.roll(s[i]);\n\t}\n\tcout << D.top() << endl;\n}\n\nvoid solve_AOJ_ITP1_11_B() {\n\tDice D;\n\tint N = 6;\n\tFOR(i, 0, N) {\n\t\tint val; cin >> val;\n\t\tD.s[i] = val;\n\t}\n\tvector<Dice> dices = makeDices(D);\n\tint Q; cin >> Q;\n\tFOR(i, 0, Q) {\n\t\tint TOP, FRONT;\n\t\tcin >> TOP >> FRONT;\n\t\tFOR(i, 0, SZ(dices)) { //24個\n\t\t\tif (dices[i].top() == TOP && dices[i].s[1] == FRONT) {\n\t\t\t\tcout << dices[i].s[2] << endl;\n\t\t\t\tbreak;\n\t\t\t}\n\t\t}\n\t}\n}\n\nvoid solve_AOJ_ITP1_11_C() {\n\tDice d[2];\n\tFOR(k, 0, 2) {\n\t\tFOR(i, 0, 6) {\n\t\t\tint val; cin >> val;\n\t\t\td[k].s[i] = val;\n\t\t}\n\t}\n\tuLL hashval = d[0].hash();\n\tvector<Dice>dices = makeDices(d[1]);\n\tFOR(i, 0, SZ(dices)) {\n\t\tif (hashval == dices[i].hash()) {\n\t\t\tcout << \"Yes\" << endl;\n\t\t\treturn;\n\t\t}\n\t}\n\tcout << \"No\" << endl;\n}\nvoid solve_AOJ_ITP1_11_D() {\n\tint N; cin >> N;\n\tvector<Dice>d(N);\n\tFOR(k, 0, N) {\n\t\tFOR(i, 0, 6) {\n\t\t\tint val; cin >> val;\n\t\t\td[k].s[i] = val;\n\t\t}\n\t}\n\tset<uLL>Se;\n\tSe.insert(d[0].hash());\n\tFOR(i, 1, N) {\n\t\tvector<Dice>dices = makeDices(d[i]);\n\t\tint flag = 0;\n\t\tFOR(j, 0, SZ(dices)) {\n\t\t\tflag |= (Se.count(dices[j].hash()));\n\t\t}\n\t\tif (flag) {\n\t\t\tcout << \"No\" << endl;\n\t\t\treturn;\n\t\t}\n\t\tSe.insert(dices[i].hash());\n\n\t}\n\tcout << \"Yes\" << endl;\n}\n\nvoid solve_AOJ_0502() {\n\tint Q;\n\twhile (cin >> Q, Q) {\n\t\tDice d; FOR(i, 0, 6)d.s[i] = i + 1;\n\t\tint ans = 1;\n\t\tFOR(kim, 0, Q) {\n\t\t\tstring s; cin >> s;\n\t\t\td.roll(s[0]);\n\t\t\tans += d.top();\n\t\t}\n\t\tcout << ans << endl;\n\t}\n\n\n}\n\nusing PII = pair<int, int>;\nmap<char, char>changeC;\ninline int mhash(const PII &a) {\n\treturn (a.first + 1200) * 2400 + a.second + 1200;\n}\nvoid solve_AOJ_2703() { // えーサイコロ関係なくないですか?ないですね\n\tconst string ORIG = \"EWNS\";\n\tconst string TO = \"RLBF\";\n\tFOR(i, 0, 4) {\n\t\tchangeC[TO[i]] = ORIG[i];\n\t}\n\tint N;\n\twhile (cin >> N, N) {\n\t\tvector<map<PII, int>>Map(N);\n\t\tint x, y;\n\t\tDice dice;\n\t\tFOR(i, 0, N) {\n\t\t\tcin >> x >> y;\n\t\t\tcin >> dice.s[3] >> dice.s[2] >> dice.s[1] >> dice.s[4] >> dice.s[5] >> dice.s[0];\n\t\t\tstring s; cin >> s;\n\t\t\tMap[i][PII(y, x)] = dice.bottom();\n\t\t\tFOR(j, 0, SZ(s)) {\n\t\t\t\tdice.roll(changeC[s[j]]);\n\t\t\t\t// R,L,B,F\n\t\t\t\tif (s[j] == 'R')x++;\n\t\t\t\tif (s[j] == 'L')x--;\n\t\t\t\tif (s[j] == 'B')y++;\n\t\t\t\tif (s[j] == 'F')y--;\n\t\t\t\t// y x の移動\n\t\t\t\tMap[i][PII(y, x)] = dice.bottom();\n\t\t\t}\n\t\t}\n\n\t\tvector<int>bitdp(1 << N, 0);\n\t\tFOR(state, 0, 1 << N) {\n\t\t\tunordered_map<int, bool>used; // orderedだと結構遅かった\n\t\t\tFOR(i, 0, N) {\n\t\t\t\tif (state & 1 << i) {\n\t\t\t\t\tfor (auto it : Map[i]) {\n\t\t\t\t\t\tused[mhash(it.first)] = 1;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t\t// 記録した\n\t\t\tFOR(i, 0, N) {\n\t\t\t\tif (state & 1 << i)continue;\n\t\t\t\tint addv = 0;\n\n\t\t\t\tfor (auto it : Map[i]) {\n\t\t\t\t\tif (!used.count(mhash(it.first))) {\n\t\t\t\t\t\taddv += it.second;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\tbitdp[state | (1 << i)] = max(bitdp[state | (1 << i)], bitdp[state] + addv);\n\t\t\t}\n\t\t}\n\t\tcout << bitdp[(1 << N) - 1] << endl;\n\n\t}\n}\n\nvoid solve_AOJ_1181() {\n\tint N;\n\tDice BaseDi;\n\tFOR(i, 0, 6)BaseDi.s[i] = i + 1;\n\tvector<Dice>dices = makeDices(BaseDi);\n\twhile (cin >> N, N) {\n\t\tVVI masu(210, VI(210, 0));\n\t\tvector<VVI>valset(210, VVI(210, VI()));\n\t\tFOR(okaduki, 0, N) {\n\t\t\tint tp, fr; cin >> tp >> fr;\n\t\t\tint X = 100, Y = 100;\n\t\t\tDice d;\n\t\t\tFOR(i, 0, SZ(dices)) {\n\t\t\t\tif (dices[i].top() == tp&&dices[i].s[1] == fr) {\n\t\t\t\t\td = dices[i];\n\t\t\t\t\tbreak;\n\t\t\t\t}\n\t\t\t}\n\t\t\twhile (1) {\n\t\t\t\tmasu[Y][X]++;\n\t\t\t\t// 落ちる方向を決める\n\t\t\t\t// 大きな方に転がり落ちる\n\t\t\t\tint dy[] = { -1, 1, 0, 0, -1, };\n\t\t\t\tint dx[] = { -1, 0, 1, -1, 0, };\n\t\t\t\tint dir = -1;\n\t\t\t\tFOR(num, 4, 6 + 1) {\n\t\t\t\t\tif (d.top() != num && d.bottom() != num) { //面があれ\n\t\t\t\t\t\tFOR(i, 1, 4 + 1) {\n\t\t\t\t\t\t\tif (d.s[i] == num) {\n\t\t\t\t\t\t\t\tif (masu[Y][X] > masu[Y + dy[i]][X + dx[i]] + 1) {\n\t\t\t\t\t\t\t\t\tdir = i;\n\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\tif (dir == -1) {\n\t\t\t\t\tvalset[Y][X].push_back(d.top());\n\t\t\t\t\tbreak;\n\t\t\t\t}\n\t\t\t\tmasu[Y][X]--;\n\t\t\t\tstring TO = \"$SEWN\";\n\t\t\t\tY += dy[dir]; X += dx[dir];\n\t\t\t\td.roll(TO[dir]);\n\t\t\t}\n\t\t}\n\t\tVI a(7, 0);\n\t\tFOR(i, 0, 210) {\n\t\t\tFOR(j, 0, 210) {\n\t\t\t\tif (!SZ(valset[i][j]))continue;\n\t\t\t\tint val = valset[i][j].back();\n\t\t\t\ta[val]++;\n\t\t\t}\n\t\t}\n\t\tFOR(i, 1, 6 + 1) {\n\t\t\tcout << a[i] << \" \\n\"[i == 6];\n\t\t}\n\n\t}\n\n}\n\nvector<Dice>dices;\nint tops[3][3], fronts[3][3];\nvector<int> ans;\nDice putdice[3][3][3];\nvoid dfs(int state) {\n\tif (state == 27) {\n\t\tint ret = 0;\n\t\tFOR(i, 0, 3) {// 右の右をとる s[2]\n\t\t\tFOR(j, 0, 3) {\n\t\t\t\tret += putdice[i][2][j].s[2];\n\t\t\t}\n\t\t}\n\n\t\tans.push_back(ret);\n\t}\n\telse {\n\t\tint X = (state / 3) % 3;\n\t\tint Y = state % 3;\n\t\tint Z = state / 9;\n\t\tFOR(i, 0, 24) { // put dices to putdice\n\t\t\t// ダメなもの top .front がちゃう\n\t\t\tif (Z == 0 && tops[X][Y] && tops[X][Y] != dices[i].top())continue;\n\t\t\tif (X == 2 && fronts[Z][Y] && fronts[Z][Y] != dices[i].s[1])continue;\n\t\t\t// ダメなもの 7じゃない\n\t\t\tif (X&& putdice[X - 1][Y][Z].s[4] + dices[i].s[1] != 7)continue; // 奥\n\t\t\tif (Y&& putdice[X][Y - 1][Z].s[2] + dices[i].s[3] != 7)continue; // 左\n\t\t\tif (Z&& putdice[X][Y][Z - 1].s[5] + dices[i].s[0] != 7)continue; // 上\n\n\t\t\tputdice[X][Y][Z] = dices[i];\n\t\t\tdfs(state + 1);\n\t\t}\n\n\n\t}\n\n}\n\n\n// 24^27なきもちになるけど前処理すればだいたい決まるので解析しなくても投げる気持ちが固まる\n// パラメータの管理壊れる\nvoid solve_AOJ_1253() {\n\t// 条件を見たすように配置したときの右面の種類\n\tDice Base;\n\tFOR(i, 0, 6)Base.s[i] = i + 1;\n\tdices = makeDices(Base);\n\n\tint Case; cin >> Case;\n\tFOR(kyo, 0, Case) {\n\t\tFOR(i, 0, 3)FOR(j, 0, 3)cin >> tops[i][j];\n\t\tFOR(i, 0, 3)FOR(j, 0, 3)cin >> fronts[i][j];\n\t\tdfs(0);\n\t\tSORT(ans); UNIQ(ans);\n\t\tFOR(i, 0, SZ(ans)) {\n\t\t\tcout << ans[i] << \" \\n\"[i == SZ(ans) - 1];\n\t\t}\n\t\tif (SZ(ans) == 0)cout << 0 << endl;\n\t\tans.clear();\n\t}\n\n\n\n\n}\nDice resDice[4];\nvector<Dice>diceState[4];\n\nvoid Dfs(int state, const int N, int& ans) {\n\tif (N == state) {\n\t\tint res = 0;\n\t\t// あるおきかたについて\n\t\t// 一緒になるように最小の塗り分けをする\n\n\t\tFOR(i, 0, 6) { //ある面について\n\t\t\tmap<int, int>cnt;\n\t\t\tFOR(k, 0, N) {\n\t\t\t\tcnt[resDice[k].s[i]]++;\n\t\t\t}\n\t\t\tint mx = 0;\n\t\t\tfor (auto it : cnt) {\n\t\t\t\tmx = max(mx, it.second); // 重複maxが保存すべきもの\n\t\t\t}\n\t\t\tres += N - mx;\n\t\t}\n\n\n\n\n\t\tans = min(ans, res);\n\t\treturn;\n\t}\n\tFOR(i, 0, 24) {\n\t\tresDice[state] = diceState[state][i];\n\t\tDfs(state + 1, N, ans);\n\t}\n}\n\nvoid solve_AOJ_1259() {\n\t// N diff checkする 全探索 同じにするための塗り替えを最小にする\n\tint N;\n\twhile (cin >> N, N) {\n\t\tif (N == 1) {\n\t\t\tstring s; FOR(i, 0, 6)cin >> s;\n\n\t\t\tcout << 0 << endl;\n\t\t}\n\t\telse {\n\n\t\t\tvector<Dice>baseDice(N);\n\t\t\tFOR(i, 0, 4)diceState[i].clear();\n\t\t\tmap<string, int>sHash;\n\t\t\tint hp = 1;\n\t\t\tFOR(i, 0, N) {\n\t\t\t\tFOR(j, 0, 6) {\n\t\t\t\t\tstring s; cin >> s;\n\t\t\t\t\tif (sHash.find(s) == sHash.end())sHash[s] = hp++;\n\t\t\t\t\tbaseDice[i].s[j] = sHash[s];\n\t\t\t\t}\n\t\t\t}\n\t\t\t// 値をいれたので24^(N-1)全部つくる\n\t\t\tFOR(i, 0, N) {\n\t\t\t\tdiceState[i] = makeDices(baseDice[i]);\n\t\t\t}\n\t\t\tint ans = INF;\n\t\t\tresDice[0] = baseDice[0];\n\t\t\tDfs(1, N, ans);\n\t\t\tcout << ans << endl;\n\t\t}\n\t}\n\n\n}\n \n\n// R S Vは解けそう\n\nint main() {\n\tcin.tie(0);\n\tios_base::sync_with_stdio(false);\n\n\t//solve_AOJ_ITP1_11_A();\n\t//solve_AOJ_ITP1_11_B();\n\t//solve_AOJ_ITP1_11_C();\n\t//solve_AOJ_ITP1_11_D();\n\t//solve_AOJ_0502();\n\n\t//solve_AOJ_1181();\n\t//solve_AOJ_2703();\n\t//solve_AOJ_1253();\n\tsolve_AOJ_1259();\n\n\treturn 0;\n}\n//", "accuracy": 1, "time_ms": 190, "memory_kb": 3248, "score_of_the_acc": -0.3291, "final_rank": 7 }, { "submission_id": "aoj_1259_2664067", "code_snippet": "#include <stdio.h>\n#include <cmath>\n#include <algorithm>\n#include <cfloat>\n#include <stack>\n#include <queue>\n#include <vector>\n#include <string>\n#include <iostream>\n#include <set>\n#include <map>\n#include <time.h>\ntypedef long long int ll;\ntypedef unsigned long long int ull;\n#define BIG_NUM 2000000000\n#define MOD 1000000007\n#define EPS 0.000000001\nusing namespace std;\n\n\n\n\nstruct Info{\n\tchar buf[25];\n\tint count;\n};\n\nclass Dice{\npublic:\n\n\tvoid setPos(int num){\n\n\t\tfor(int i = 0; i < 6; i++)work[i] = index[i];\n\n\t\tswitch(num){\n\t\tcase 0:\n\t\t\tsetindex(work[4],work[0],work[2],work[3],work[5],work[1]);\n\t\t\tbreak;\n\t\tcase 1:\n\t\t\tsetindex(work[5],work[4],work[2],work[3],work[1],work[0]);\n\t\t\tbreak;\n\t\tcase 2:\n\t\t\tsetindex(work[1],work[5],work[2],work[3],work[0],work[4]);\n\t\t\tbreak;\n\t\tcase 3:\n\t\t\tsetindex(work[0],work[1],work[2],work[3],work[4],work[5]);\n\t\t\tbreak;\n\t\tcase 4:\n\t\t\tsetindex(work[2],work[1],work[5],work[0],work[4],work[3]);\n\t\t\tbreak;\n\t\tcase 5:\n\t\t\tsetindex(work[4],work[2],work[5],work[0],work[3],work[1]);\n\t\t\tbreak;\n\t\tcase 6:\n\t\t\tsetindex(work[3],work[4],work[5],work[0],work[1],work[2]);\n\t\t\tbreak;\n\t\tcase 7:\n\t\t\tsetindex(work[1],work[3],work[5],work[0],work[2],work[4]);\n\t\t\tbreak;\n\t\tcase 8:\n\t\t\tsetindex(work[5],work[3],work[4],work[1],work[2],work[0]);\n\t\t\tbreak;\n\t\tcase 9:\n\t\t\tsetindex(work[2],work[5],work[4],work[1],work[0],work[3]);\n\t\t\tbreak;\n\t\tcase 10:\n\t\t\tsetindex(work[0],work[2],work[4],work[1],work[3],work[5]);\n\t\t\tbreak;\n\t\tcase 11:\n\t\t\tsetindex(work[3],work[0],work[4],work[1],work[5],work[2]);\n\t\t\tbreak;\n\t\tcase 12:\n\t\t\tsetindex(work[1],work[0],work[3],work[2],work[5],work[4]);\n\t\t\tbreak;\n\t\tcase 13:\n\t\t\tsetindex(work[5],work[1],work[3],work[2],work[4],work[0]);\n\t\t\tbreak;\n\t\tcase 14:\n\t\t\tsetindex(work[4],work[5],work[3],work[2],work[0],work[1]);\n\t\t\tbreak;\n\t\tcase 15:\n\t\t\tsetindex(work[0],work[4],work[3],work[2],work[1],work[5]);\n\t\t\tbreak;\n\t\tcase 16:\n\t\t\tsetindex(work[2],work[4],work[0],work[5],work[1],work[3]);\n\t\t\tbreak;\n\t\tcase 17:\n\t\t\tsetindex(work[1],work[2],work[0],work[5],work[3],work[4]);\n\t\t\tbreak;\n\t\tcase 18:\n\t\t\tsetindex(work[3],work[1],work[0],work[5],work[4],work[2]);\n\t\t\tbreak;\n\t\tcase 19:\n\t\t\tsetindex(work[4],work[3],work[0],work[5],work[2],work[1]);\n\t\t\tbreak;\n\t\tcase 20:\n\t\t\tsetindex(work[0],work[3],work[1],work[4],work[2],work[5]);\n\t\t\tbreak;\n\t\tcase 21:\n\t\t\tsetindex(work[2],work[0],work[1],work[4],work[5],work[3]);\n\t\t\tbreak;\n\t\tcase 22:\n\t\t\tsetindex(work[5],work[2],work[1],work[4],work[3],work[0]);\n\t\t\tbreak;\n\t\tcase 23:\n\t\t\tsetindex(work[3],work[5],work[1],work[4],work[0],work[2]);\n\t\t\tbreak;\n\t\tcase 24:\n\t\t\t//Do nothing\n\t\t\tbreak;\n\t\t}\n\t}\n\n\tvoid setindex(int w0,int w1,int w2,int w3,int w4,int w5){\n\t\tindex[0] = w0;\n\t\tindex[1] = w1;\n\t\tindex[2] = w2;\n\t\tindex[3] = w3;\n\t\tindex[4] = w4;\n\t\tindex[5] = w5;\n\t}\n\n\tvoid init(){\n\t\tindex[0] = 2;\n\t\tindex[1] = 5;\n\t\tindex[2] = 1;\n\t\tindex[3] = 4;\n\t\tindex[4] = 0;\n\t\tindex[5] = 3;\n\t}\n\n\tint index[6];\n\tint work[6];\n};\n\nint N,info_index;\nDice dice[4];\nchar color[4][6][25];\nInfo info[24];\n\nbool strCmp(char* base, char* comp){\n\tint length1,length2;\n\tfor(length1=0;base[length1] != '\\0';length1++);\n\tfor(length2=0;comp[length2] != '\\0';length2++);\n\tif(length1 != length2)return false;\n\n\tfor(int i=0;base[i] != '\\0'; i++){\n\t\tif(base[i] != comp[i])return false;\n\t}\n\treturn true;\n}\n\nvoid strcpy(char* to,char* str){\n\tfor(int i=0;str[i] != '\\0';i++){\n\t\tto[i] = str[i];\n\t\tto[i+1] = '\\0';\n\t}\n}\n\n\nvoid func(){\n\n\tinfo_index = 0;\n\tbool FLG;\n\n\tfor(int i = 0; i < N; i++){\n\t\tdice[i].init();\n\t\tfor(int k = 0; k < 6; k++){\n\t\t\tscanf(\"%s\",color[i][k]);\n\n\t\t\tFLG = false;\n\t\t\tfor(int a = 0;a < info_index; a++){\n\t\t\t\tif(strCmp(info[a].buf,color[i][k])){\n\t\t\t\t\tFLG = true;\n\t\t\t\t\tbreak;\n\t\t\t\t}\n\t\t\t}\n\t\t\tif(!FLG){\n\t\t\t\tstrcpy(info[info_index].buf,color[i][k]);\n\t\t\t\tinfo_index++;\n\t\t\t}\n\t\t}\n\t}\n\n\tif(N == 1){\n\t\tprintf(\"0\\n\");\n\t\treturn;\n\t}\n\n\tint minimum,tmp,tmp_sum,maximum;\n\n\tif(N == 2){\n\n\t\tminimum = BIG_NUM;\n\n\t\tfor(int i = 0; i <= 24; i++){\n\t\t\tdice[1].init();\n\t\t\tdice[1].setPos(i);\n\n\t\t\ttmp = 0;\n\n\t\t\tfor(int k = 0; k < 6; k++){\n\t\t\t\tif(strCmp(color[0][dice[0].index[k]],color[1][dice[1].index[k]]) == false){\n\t\t\t\t\ttmp++;\n\t\t\t\t}\n\t\t\t}\n\n\t\t\tminimum = min(minimum,tmp);\n\t\t}\n\n\t\tprintf(\"%d\\n\",minimum);\n\n\t}else if(N == 3){\n\n\t\tminimum = BIG_NUM;\n\n\t\tfor(int a = 0; a <= 24; a++){\n\t\t\tdice[1].init();\n\t\t\tdice[1].setPos(a);\n\t\t\tfor(int b = 0; b <= 24; b++){\n\t\t\t\tdice[2].init();\n\t\t\t\tdice[2].setPos(b);\n\n\t\t\t\ttmp_sum = 0;\n\t\t\t\tfor(int i = 0; i < 6; i++){\n\t\t\t\t\tfor(int k = 0; k < info_index; k++)info[k].count = 0;\n\n\t\t\t\t\tfor(int p = 0; p < 3; p++){\n\t\t\t\t\t\tfor(int k = 0; k < info_index; k++){\n\t\t\t\t\t\t\tif(strCmp(info[k].buf,color[p][dice[p].index[i]])){\n\t\t\t\t\t\t\t\tinfo[k].count++;\n\t\t\t\t\t\t\t\tbreak;\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\n\t\t\t\t\tmaximum = 1;\n\t\t\t\t\tfor(int k = 0; k < info_index; k++)maximum = max(maximum,info[k].count);\n\n\t\t\t\t\ttmp_sum += N-maximum;\n\t\t\t\t}\n\t\t\t\tminimum = min(minimum,tmp_sum);\n\t\t\t}\n\t\t}\n\n\t\tprintf(\"%d\\n\",minimum);\n\n\t}else{ //N == 4\n\n\t\tminimum = BIG_NUM;\n\n\t\tfor(int a = 0; a <= 24; a++){\n\t\t\tdice[1].init();\n\t\t\tdice[1].setPos(a);\n\t\t\tfor(int b = 0; b <= 24; b++){\n\t\t\t\tdice[2].init();\n\t\t\t\tdice[2].setPos(b);\n\n\t\t\t\tfor(int c = 0; c <= 24; c++){\n\t\t\t\t\tdice[3].init();\n\t\t\t\t\tdice[3].setPos(c);\n\n\t\t\t\t\ttmp_sum = 0;\n\t\t\t\t\tfor(int i = 0; i < 6; i++){\n\t\t\t\t\t\tfor(int k = 0; k < info_index; k++)info[k].count = 0;\n\n\t\t\t\t\t\tfor(int p = 0; p < 4; p++){\n\t\t\t\t\t\t\tfor(int k = 0; k < info_index; k++){\n\t\t\t\t\t\t\t\tif(strCmp(info[k].buf,color[p][dice[p].index[i]])){\n\t\t\t\t\t\t\t\t\tinfo[k].count++;\n\t\t\t\t\t\t\t\t\tbreak;\n\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t}\n\n\t\t\t\t\t\tmaximum = 1;\n\t\t\t\t\t\tfor(int k = 0; k < info_index; k++)maximum = max(maximum,info[k].count);\n\n\t\t\t\t\t\ttmp_sum += N-maximum;\n\t\t\t\t\t}\n\t\t\t\t\tminimum = min(minimum,tmp_sum);\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\tprintf(\"%d\\n\",minimum);\n\t}\n}\n\nint main(){\n\n\twhile(true){\n\t\tscanf(\"%d\",&N);\n\t\tif(N == 0)break;\n\n\t\tfunc();\n\t}\n\treturn 0;\n}", "accuracy": 1, "time_ms": 300, "memory_kb": 3192, "score_of_the_acc": -0.3413, "final_rank": 10 }, { "submission_id": "aoj_1259_2202629", "code_snippet": "int dice24[24][6]=\n{\n\t{2,1,5,0,4,3},{2,0,1,4,5,3},{2,4,0,5,1,3},{2,5,4,1,0,3},{4,2,5,0,3,1},\n\t{5,2,1,4,3,0},{1,2,0,5,3,4},{0,2,4,1,3,5},{0,1,2,3,4,5},{4,0,2,3,5,1},\n\t{5,4,2,3,1,0},{1,5,2,3,0,4},{5,1,3,2,4,0},{1,0,3,2,5,4},{0,4,3,2,1,5},\n\t{4,5,3,2,0,1},{1,3,5,0,2,4},{0,3,1,4,2,5},{4,3,0,5,2,1},{5,3,4,1,2,0},\n\t{3,4,5,0,1,2},{3,5,1,4,0,2},{3,1,0,5,4,2},{3,0,4,1,5,2},\n};\n#include<cstdio>\n#include<cstring>\n#include<string>\n#include<vector>\n#include<algorithm>\nusing namespace std;\n\nconst int maxn=4;\nint n,dice[maxn][6],ans;\n\nvector<string> names;\nint ID(const char* name)\n{\n\tstring s(name);\n\tint n=names.size();\n\tfor (int i=0;i<n;i++)\n\tif (names[i]==s) return i;\n\tnames.push_back(s);\n\treturn n;\n}\n\nint r[maxn],color[maxn][6];\n\nvoid check()\n{\n\tfor (int i=0;i<n;i++)\n\t\tfor (int j=0;j<6;j++)\n\t\tcolor[i][dice24[r[i]][j]]=dice[i][j];\n\tint tot=0;\n\tfor (int j=0;j<6;j++)\n\t{\n\t\tint cnt[maxn*6];\n\t\tmemset(cnt,0,sizeof(cnt));\n\t\tint maxface=0;\n\t\tfor (int i=0;i<n;i++)\n\t\t\tmaxface=max(maxface,++cnt[color[i][j]]);\n\t\ttot+=n-maxface;\n\t}\n\tans=min(ans,tot);\n}\n\nvoid dfs(int d)\n{\n\tif (d==n) check();\n\telse for(int i=0;i<24;i++)\n\t{\n\t\tr[d]=i;\n\t\tdfs(d+1);\n\t}\n}\nint main()\n{\n\twhile (scanf(\"%d\",&n)==1&&n)\n\t{\n\t\tnames.clear();\n\t\tfor (int i=0;i<n;i++)\n\t\t\tfor (int j=0;j<6;j++)\n\t\t\t{\n\t\t\t\tchar name[30];\n\t\t\t\tscanf(\"%s\",name);\n\t\t\t\tdice[i][j]=ID(name);\n\t\t\t}\n\t\tans=n*6;\n\t\tr[0]=0;\n\t\tdfs(1);\n\t\tprintf(\"%d\\n\",ans);\n\t}\n\treturn 0;\n}", "accuracy": 1, "time_ms": 20, "memory_kb": 2732, "score_of_the_acc": -0.2235, "final_rank": 3 }, { "submission_id": "aoj_1259_1881841", "code_snippet": "#include <bits/stdc++.h>\n \nusing namespace std;\n \nclass Dice{\nprivate:\n int tmp;\npublic:\n int d[6];\n \n void rollN(){\n\ttmp = d[0];\n\td[0] = d[1];\n\td[1] = d[5];\n\td[5] = d[4];\n\td[4] = tmp;\n }\n void rollE(){\n\ttmp = d[0];\n\td[0] = d[3];\n\td[3] = d[5];\n\td[5] = d[2];\n\td[2] = tmp;\n }\n void rotation(){\n\ttmp = d[1];\n\td[1] = d[3];\n\td[3] = d[4];\n\td[4] = d[2];\n\td[2] = tmp;\n }\n bool operator < (const Dice &die)const{\n\tfor(int i = 0 ; i < 6 ; i++){\n\t if(d[i] != die.d[i]) return d[i] < die.d[i];\n\t}\n\treturn false;\n }\n};\n \nint N,ans,p[4];\nmap<string,int> mp;\nvector<Dice> dice[4];\n \nint getCost(){\n int res = 0;\n for(int i = 0 ; i < 6 ; i++){\n\tint cnt[25] = {0}, max = 0;\n\tfor(int j = 0 ; j < N ; j++){\n\t cnt[dice[j][p[j]].d[i]]++;\n\t max = std::max(max,cnt[dice[j][p[j]].d[i]]);\n\t}\n\tres += N-max;\n }\n return res;\n}\n \nvoid solve(int x){\n if(x == N){\n\tans = min(ans,getCost());\n\treturn;\n }\n for(int i = 0 ; i < (int)dice[x].size() ; i++){\n\tp[x] = i;\n\tsolve(x+1);\n }\n}\n \nvector<Dice> make(Dice die){\n vector<Dice> res;\n set<Dice> visited;\n for(int i = 0 ; i < 4 ; i++){\n\tfor(int j = 0 ; j < 4 ; j++){\n\t if(!visited.count(die)){\n\t\tvisited.insert(die);\n\t\tres.push_back(die);\n\t }\n\t die.rollN();\n\t}\n\tfor(int j = 0 ; j < 4 ; j++){\n\t if(!visited.count(die)){\n\t\tvisited.insert(die);\n\t\tres.push_back(die);\n\t }\n\t die.rollE();\n\t}\n\tdie.rotation();\n }\n return res;\n}\n \nint main(){\n Dice in;\n while(cin >> N, N){\n\tint n = 0;\n\tmp.clear();\n\tfor(int i = 0 ; i < N ; i++){\n\t p[i] = -1;\n\t for(int j = 0 ; j < 6 ; j++){\n\t\tstring s;\n\t\tcin >> s;\n\t\tif(mp.find(s) != mp.end()){\n\t\t in.d[j] = mp[s];\n\t\t}else{\n\t\t mp[s] = n++;\n\t\t in.d[j] = mp[s];\n\t\t}\n\t }\n\t dice[i] = make(in);\n\t}\n\tans = 1e9;\n\tsolve(0);\n\tcout << ans << endl;\n }\n return 0;\n}", "accuracy": 1, "time_ms": 280, "memory_kb": 3028, "score_of_the_acc": -0.314, "final_rank": 4 }, { "submission_id": "aoj_1259_1783151", "code_snippet": "// Colored Cubes \n// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=1259\n\n#include<iostream>\n#include<map>\n#include<vector>\n#include<algorithm>\n#include<cmath>\n#include<climits>\n#include<ctime>\n#include<cstring>\n#include<numeric>\n#include<functional>\n\n#define ALL(v) (v).begin(),(v).end()\n#define REP(i,p,n) for(int i=p;i<(int)(n);++i)\n#define rep(i,n) REP(i,0,n)\n#define dump(a) (cerr << #a << \"=\" << (a) << endl)\n#define DUMP(list) cout << \"{ \"; for(auto nth : list){ cout << nth << \" \"; } cout << \"}\" << endl;\n\nusing namespace std;\n\nenum AXIS {X, Y, Z};\nconst int INF = INT_MAX / 3;\nconst vector<vector<int>> pattern = {\n {3, 1, 0, 5, 4, 2}, // x????????¢\n {4, 0, 2, 3, 5, 1}, // y????????¢\n {0, 3, 1, 4, 2, 5} // z????????¢\n};\n\nint N;\nint ans, max_color;\nvector<vector<int>> cubes;\n\nvoid roll(int n, int axis) {\n vector<int> result;\n vector<int> cube = cubes[n];\n for (int p : pattern[axis]) {\n result.push_back(cube[p]);\n }\n cubes[n] = result;\n}\n\nint repaint() {\n int res = 0;\n for (int j = 0; j < 6; ++j) {\n vector<int> counter(max_color);\n for (int i = 0; i < N; ++i) {\n ++counter[cubes[i][j]];\n }\n int most = *max_element(ALL(counter));\n res += N - most;\n }\n return res;\n}\n\nvoid dfs(int n) {\n if (n == N) {\n ans = min(ans, repaint());\n return;\n }\n for (int i = 0; i < 6; ++i) {\n for (int x = 0; x < 4; ++x) {\n dfs(n + 1);\n roll(n, X);\n }\n if (i % 2 == 0) {\n roll(n, Y);\n } else {\n roll(n, Z);\n }\n }\n return;\n}\n\nint main() { \n while (cin >> N) {\n if (N == 0) break;\n\n map<string, int> converter;\n const function<int(string)> encode = [&](const string &color) -> int {\n if (!converter.count(color)) converter.emplace(color, converter.size());\n return converter[color];\n };\n\n cubes.assign(4, vector<int>(6, 0));\n for (int i = 0; i < N; ++i) {\n for (int j = 0; j < 6; ++j) {\n string color;\n cin >> color;\n cubes[i][j] = encode(color);\n }\n }\n\n ans = INF, max_color = converter.size();\n dfs(0);\n cout << ans << endl;\n }\n return 0;\n}", "accuracy": 1, "time_ms": 2410, "memory_kb": 3164, "score_of_the_acc": -0.7258, "final_rank": 17 }, { "submission_id": "aoj_1259_1590114", "code_snippet": "#include <algorithm>\n#include <vector>\n#include <cfloat>\n#include <string>\n#include <cmath>\n#include <set>\n#include <cstdlib>\n#include <map>\n#include <ctime>\n#include <iomanip>\n#include <functional>\n#include <deque>\n#include <iostream>\n#include <cstring>\n#include <queue>\n#include <cstdio>\n#include <stack>\n#include <climits>\n#include <sys/time.h>\n#include <cctype>\n \nusing namespace std;\n\n// swap(d[], d[]);\nvoid rot1(vector <int> &d) {\n swap(d[1], d[2]);\n swap(d[2], d[4]);\n swap(d[4], d[3]);\n}\nvoid rot2(vector <int> &d) {\n swap(d[1], d[0]);\n swap(d[0], d[4]);\n swap(d[4], d[5]);\n}\nvoid rot3(vector <int> &d) {\n swap(d[0], d[3]);\n swap(d[3], d[5]);\n swap(d[5], d[2]);\n}\nvoid rev1(vector <int> &d) {\n swap(d[1], d[3]);\n swap(d[3], d[4]);\n swap(d[4], d[2]);\n}\nvoid rev2(vector <int> &d) {\n swap(d[1], d[5]);\n swap(d[5], d[4]);\n swap(d[4], d[0]);\n}\nvoid rev3(vector <int> &d) {\n swap(d[0], d[2]);\n swap(d[2], d[5]);\n swap(d[5], d[3]);\n}\n\nint main() {\n int ttt = 0;\n while (true) {\n int n;\n cin >> n;\n if (n == 0) break;\n int cnt = 0;\n map <string, int> memo;\n vector <int> d[n];\n for (int i = 0; i < n; i++) {\n for (int j = 0; j < 6; j++){\n string s;\n cin >> s;\n if (memo.find(s) == memo.end()) {\n d[i].push_back(cnt);\n memo[s] = cnt;\n cnt++;\n }else {\n d[i].push_back(memo[s]);\n }\n }\n }\n if (n == 1) {\n cout << 0 << endl;\n continue;\n }\n\n set<vector<vector<int> > > cut;\n stack <vector<vector<int> > > st;\n int ans = INT_MAX;\n bool same = true;\n for (int i = 0; i < 6; i++) {\n for (int j = 1; j < n; j++) {\n if (d[j][i] != d[j-1][i]) {\n same = false;\n break;\n }\n }\n if (!same) break;\n }\n if (same) {\n ans = 0;\n }\n vector <vector <int> > t;\n for (int i = 0; i < n; i++) {\n t.push_back(d[i]);\n }\n st.push(t);\n cut.insert(t);\n while (!st.empty()) {\n vector <vector<int> > np = st.top(); st.pop();\n\n for (int i = 1; i < n; i++) {\n for (int ch = 0; ch < 6; ch++) {\n vector <vector<int> > tnp = np;\n if (ch == 0) {\n rot1(tnp[i]);\n }else if (ch == 1) {\n rot2(tnp[i]);\n }else if (ch == 2){\n rot3(tnp[i]);\n }else if (ch == 3) {\n rev1(tnp[i]);\n }else if (ch == 4) {\n rev2(tnp[i]);\n }else if (ch == 5) {\n rev3(tnp[i]);\n }\n if (cut.find(tnp) != cut.end()) {\n continue;\n }\n st.push(tnp);\n cut.insert(tnp);\n int tsum = 0;\n for (int j = 0; j < 6; j++) {\n int tcnt[cnt];\n memset(tcnt, 0, sizeof(tcnt));\n int maxp = 0;\n for (int k = 0; k < n; k++) {\n tcnt[tnp[k][j]]++;\n if (tcnt[tnp[k][j]] > tcnt[maxp]) {\n maxp = tnp[k][j];\n }\n }\n tsum += n-tcnt[maxp];\n }\n ans = min(ans, tsum);\n }\n }\n }\n int tsum = 0;\n for (int j = 0; j < 6; j++) {\n int tcnt[cnt];\n memset(tcnt, 0, sizeof(tcnt));\n int maxp = 0;\n for (int k = 0; k < n; k++) {\n tcnt[d[k][j]]++;\n if (tcnt[d[k][j]] > tcnt[maxp]) {\n maxp = d[k][j];\n }\n }\n tsum += n-tcnt[maxp];\n }\n\n cout << min(ans, tsum) << endl;\n }\n}", "accuracy": 1, "time_ms": 2400, "memory_kb": 8140, "score_of_the_acc": -1.4401, "final_rank": 20 }, { "submission_id": "aoj_1259_1264718", "code_snippet": "#include <cstdio>\n#include <cstring>\n#include <string>\n#include <map>\n#include <iostream>\n\nusing namespace std;\n\nconst int mov[24][6]= {\n {0,1,2,3,4,5},{0,2,4,1,3,5},{0,4,3,2,1,5},{0,3,1,4,2,5},\n {3,1,0,5,4,2},{3,0,4,1,5,2},{3,4,5,0,1,2},{3,5,1,4,0,2},\n {5,1,3,2,4,0},{5,3,4,1,2,0},{5,4,2,3,1,0},{5,2,1,4,3,0},\n {2,1,5,0,4,3},{2,5,4,1,0,3},{2,4,0,5,1,3},{2,0,1,4,5,3},\n {4,0,2,3,5,1},{4,2,5,0,3,1},{4,5,3,2,0,1},{4,3,0,5,2,1},\n {1,0,3,2,5,4},{1,3,5,0,2,4},{1,5,2,3,0,4},{1,2,0,5,3,4}\n};\n\nstruct Cube {\n\tint num[7];\n};\n\nint n, cnt, ans;\nchar str[1000];\nCube c[5], now[5];\nmap<string,int> id;\n\nint ID(char *st) {\n\tif (id[st] == 0) return id[st] = ++cnt;\n\treturn id[st];\n}\n\nvoid dfs(int dep) {\n\tif (n == dep) {\n\t\tint ans1 = 0;\n\t\tfor (int j = 0; j < 6; ++j) {\n\t\t\tint count[30] = {0};\n\t\t\tfor (int i = 0; i < n; ++i) ++count[now[i].num[j]];\n\t\t\tint up = 0;\n\t\t\tfor (int i = 1; i <= cnt; ++i) up = max(up, count[i]);\n\t\t\tans1 += n - up;\n\t\t}\n\t\tans = min(ans, ans1);\n\t\treturn ;\t\n\t}\n\tfor (int i = 0; i < 24; ++i) {\n\t\tfor (int j = 0; j < 6; ++j) {\n\t\t\tnow[dep].num[j] = c[dep].num[mov[i][j]];\n\t\t}\n\t\tdfs(dep + 1);\n\t}\n}\n\nvoid Clear() {\n\tmemset(c, 0, sizeof c);\n\tmemset(now, 0, sizeof now);\n\tid.clear();\n\tcnt = 0;\n}\n\nint main() {\n\twhile (1 == scanf(\"%d\", &n)) {\n\t\tif (0 == n) break;\n\t\tClear();\n\t\tfor (int i = 0; i < n; ++i) {\n\t\t\tfor (int j = 0; j < 6; ++j) {\n\t\t\t\tscanf(\"%s\", str);\n\t\t\t\tc[i].num[j] = ID(str);\n\t\t\t}\n\t\t}\n\t\tif (1 == n) {\n\t\t\tputs(\"0\");\n\t\t\tcontinue;\n\t\t}\n\t\tans = (int)1e9;\n\t\tnow[0] = c[0];\n\t\tdfs(1);\n\t\tprintf(\"%d\\n\", ans);\n\t}\n\treturn 0;\n}", "accuracy": 1, "time_ms": 40, "memory_kb": 1244, "score_of_the_acc": -0.013, "final_rank": 1 }, { "submission_id": "aoj_1259_1264389", "code_snippet": "/*************************************************************************\n > File Name: c.cpp\n > Author: X__X\n > Mail: [email protected] \n > Created Time: 2012/8/12 14:35:54\n ************************************************************************/\n\n#include<cstdio>\n#include<cstring>\n#include<cstdlib>\n#include<iostream>\n#include<algorithm>\n#include<cmath>\n#include<stack>\n#include<queue>\n#include<utility>\nusing namespace std;\n\n#define MP make_pair\n#define PB push_back\n#define IT iterator\n#define B begin() \n#define E end()\n#define X first\n#define Y second\n#define foreach(it, container) \\\n for(typeof((container).begin()) it = (container).begin();it!=(container).end();++it)\n#define CLR(a, x) memset(a, x, sizeof (a));\ntypedef vector<int> VI;\ntypedef pair<int,int> PII;\nVI::IT it;\nvoid op(int n){cout << n << ' ';}\n\nconst int inf = 0x3f3f3f3f;\nconst int maxn = 1e8+10;\nint ELFhash(char *key, long N)\n{\n unsigned long h = 0;\n unsigned long x = 0;\n while(*key)\n {\n h = (h << 4) + (*key++);\n if( (x = h & 0xF0000000L) != 0)\n {\n h ^= (x >> 24);\n h &= ~x;\n }\n }\n return h % N;\n}\n\nint cube[4][10];\nint tcube[4][10];\nint ttcube[4][10];\nint _rotate[6][6] = \n{\n {1, 2, 3, 4, 5, 6}, {5, 1, 3, 4, 6, 2},\n {4, 2, 1, 6, 5, 3}, {2, 6, 3, 4, 1, 5},\n {6, 5, 3, 4, 2, 1}, {3, 2, 6, 1, 5, 4}\n};\nint _Rotate[40][6];\n\n\nchar s[100];\n\nint cnts[10];\nint tt[10][10];\nint n, ans;\nvoid dfs(int k)\n{ \n if(n == k)\n {\n int tans = 0, cnt = 0;\n for(int i = 1; i <= 6; i++)\n {\n for(int j = 0; j < n; j++)\n tt[i][j] = tcube[j][i];\n }\n for(int i = 1; i <= 6; i++)\n {\n cnt = 0;\n for(int j = 0; j < n; j++)\n cnt = max(cnt, (int)count(tt[i], tt[i]+n, tt[i][j]));\n tans += cnt;\n }\n ans = min(ans, n*6-tans);\n return ;\n }\n for(int i = 0; i < 36; i++)\n {\n if(_Rotate[i][0] != -1)\n {\n for(int j = 1; j <= 6; j++)\n {\n tcube[k][j] = cube[k][_Rotate[i][j-1]];\n /*for(int ii = 0; ii < 6; ii++)\n {\n for(int jj = 1; jj <= 6; jj++)\n ttcube[k][jj] = tcube[k][_rotate[ii][jj-1]];\n dfs(k+1);\n }*/\n }\n dfs(k+1);\n }\n }\n}\n\nvoid solve()\n{\n for(int i = 0; i < n; i++)\n {\n for(int j = 1; j <= 6; j++)\n {\n scanf(\"%s\", s);\n cube[i][j] = ELFhash(s, 10000007);\n }\n }\n ans = inf;\n dfs(0);\n printf(\"%d\\n\", ans);\n}\n\nbool check(int ii, int jj)\n{\n for(int i = 0; i < 6; i++)\n if(_Rotate[ii][i] != _Rotate[jj][i])\n return false;\n return true;\n}\n\nint main()\n{\n for(int i = 0, cnt = 0; i < 6; i++)\n {\n for(int j = 0; j < 6; j++)\n {\n for(int k = 0; k < 6; k++)\n _Rotate[cnt][k] = _rotate[i][_rotate[j][k]-1];\n cnt++;\n }\n }\n for(int i = 0; i < 36; i++)\n for(int j = 0; j < 36; j++)\n if(i != j && check(i, j))\n for(int k = 0; k < 6; k++)\n _Rotate[i][k] = -1;\n while(scanf(\"%d\", &n), n)\n solve();\n}", "accuracy": 1, "time_ms": 200, "memory_kb": 1192, "score_of_the_acc": -0.035, "final_rank": 2 } ]
aoj_1258_cpp
Problem B: Book Replacement The deadline of Prof. Hachioji’s assignment is tomorrow. To complete the task, students have to copy pages of many reference books in the library. All the reference books are in a storeroom and only the librarian is allowed to enter it. To obtain a copy of a reference book’s page, a student should ask the librarian to make it. The librarian brings books out of the storeroom and makes page copies according to the requests. The overall situation is shown in Figure 1. Students queue up in front of the counter. Only a single book can be requested at a time. If a student has more requests, the student goes to the end of the queue after the request has been served. In the storeroom, there are m desks D 1 , ... , D m , and a shelf. They are placed in a line in this order, from the door to the back of the room. Up to c books can be put on each of the desks. If a student requests a book, the librarian enters the storeroom and looks for it on D 1 , ... , D m in this order, and then on the shelf. After finding the book, the librarian takes it and gives a copy of a page to the student. Then the librarian returns to the storeroom with the requested book, to put it on D 1 according to the following procedure. If D 1 is not full (in other words, the number of books on D 1 < c ), the librarian puts the requested book there. If D 1 is full, the librarian temporarily puts the requested book on the non-full desk closest to the entrance or, in case all the desks are full, on the shelf, finds the book on D 1 that has not been requested for the longest time (i.e. the least recently used book) and takes it, puts it on the non-full desk (except D 1 ) closest to the entrance or, in case all the desks except D 1 are full, on the shelf, takes the requested book from the temporary place, and finally puts it on D 1 . Your task is to write a program which simulates the behaviors of the students and the librarian, and evaluates the total cost of the overall process. Costs are associated with accessing a desk or the shelf, that is, putting/taking a book on/from it in the description above. The cost of an access is i for desk D i and m + 1 for the shelf. That is, an access to D 1 , ... , D m , and the shelf costs 1, ... , m , and m + 1, respectively. Costs of other actions are ignored. Initially, no books are put on desks. No new students appear after opening the library. Input The input consists of multiple datasets. The end of the input is indicated by a line containing three zeros separated by a space. It is not a dataset. The format of each dataset is as follows. m c n k 1 b 11 . . . b 1 k 1 . . . k n b n 1 . . . b nk n Here, all data items are positive integers. m is the number of desks not exceeding 10. c is the number of books allowed to put on a desk, which does not exceed 30. n is the number of students not exceeding 100. k i is the number of books requested by the i -th student, which does not exceed 50. b ij is the ID numbe ...(truncated)
[ { "submission_id": "aoj_1258_4181658", "code_snippet": "#include <iostream>\n#include <iomanip>\n#include <vector>\n#include <array>\n#include <string>\n#include <stack>\n#include <queue>\n#include <deque>\n#include <unordered_map>\n#include <unordered_set>\n#include <set>\n#include <tuple>\n#include <cmath>\n#include <algorithm>\n#include <functional>\n#include <numeric>\n#include <cfloat>\n#include <climits>\n#include <cassert>\n#include <random>\n#include <fstream>\n\nint main() {\n\twhile (true) {\n\t\tint m, c, n; std::cin >> m >> c >> n; if (n == 0) break;\n\t\tstd::vector<std::queue<int>> users(n);\n\t\tfor (auto& u : users) {\n\t\t\tint k; std::cin >> k;\n\t\t\tfor (auto i = 0; i < k; ++i) {\n\t\t\t\tint b; std::cin >> b;\n\t\t\t\tu.push(b);\n\t\t\t}\n\t\t}\n\t\tstd::vector<int> current_book_position(100, m), recent_access(101, INT_MAX);\n\t\tstd::set<int> first_desk;\n\t\tstd::priority_queue<int, std::vector<int>, std::greater<int>> empty_space;\n\t\tfor (auto i = 0; i <= m; ++i) empty_space.push(i);\n\t\tstd::vector<int> desk_rest(m + 1, c); desk_rest.back() = INT_MAX;\n\t\tint sum_cost = 0;\n\t\tstd::queue<int> user_in_row; for (auto i = 0; i < n; ++i) user_in_row.push(i);\n\t\tfor (auto time = 0; !user_in_row.empty(); ++time) {\n\t\t\tconst auto top = user_in_row.front(); user_in_row.pop();\n\t\t\tconst auto ref = users[top].front(); users[top].pop();\n\t\t\tif (!users[top].empty()) user_in_row.push(top);\n\t\t\tconst auto target_pos = current_book_position[ref];\n\t\t\tif (++desk_rest[target_pos] == 1) {\n\t\t\t\tempty_space.push(target_pos);\n\t\t\t}\n\t\t\tint cost = target_pos + 1;\n\t\t\tif (desk_rest[0] > 0) {\n\t\t\t\tcost += 1;\n\t\t\t\tif (--desk_rest[0] == 0) empty_space.pop();\n\t\t\t}\n\t\t\telse {\n\t\t\t\tconst auto temp_area = empty_space.top();\n\t\t\t\tif (--desk_rest[temp_area] == 0) {\n\t\t\t\t\tempty_space.pop();\n\t\t\t\t}\n\t\t\t\tconst auto most_least_ref = *std::min_element(first_desk.begin(), first_desk.end(), [&recent_access](int a, int b) {return recent_access[a] < recent_access[b]; });\n\t\t\t\tconst auto escape_area = empty_space.top();\n\t\t\t\tif (--desk_rest[escape_area] == 0) {\n\t\t\t\t\tempty_space.pop();\n\t\t\t\t}\n\t\t\t\tif (++desk_rest[temp_area] == 1) {\n\t\t\t\t\tempty_space.push(temp_area);\n\t\t\t\t}\n\t\t\t\tcost += temp_area + 1 + 1 + escape_area + 1 + temp_area + 1 + 1;\n\t\t\t\tcurrent_book_position[most_least_ref] = escape_area;\n\t\t\t\tfirst_desk.erase(most_least_ref);\n\t\t\t}\n\t\t\tcurrent_book_position[ref] = 0;\n\t\t\tfirst_desk.insert(ref);\n\t\t\trecent_access[ref] = time;\n\t\t\tsum_cost += cost;\n\t\t}\n\t\tstd::cout << sum_cost << '\\n';\n\t}\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3216, "score_of_the_acc": -1, "final_rank": 17 }, { "submission_id": "aoj_1258_2598519", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\nint main() {\n int M, C, N;\n while (cin >> M >> C >> N, M || C || N) {\n vector<deque<int>> vd(M+1);\n for (int i = 1; i <= 100; i++) vd[M].push_back(i);\n\n queue<queue<int>> vs;\n for (int i = 0; i < N; i++) {\n int k;\n cin >> k;\n queue<int> q;\n for (int j = 0; j < k; j++) {\n int x;\n cin >> x;\n q.push(x);\n }\n vs.push(q);\n }\n\n int ans = 0;\n while (vs.size()) {\n auto q = vs.front();\n vs.pop();\n int book = q.front();\n q.pop();\n if (q.size() > 0) vs.push(q);\n\n for (int i = 0; i <= M; i++) {\n for (int j = 0; j < vd[i].size(); j++) {\n if (vd[i][j] == book) {\n ans += i + 1;\n vd[i].erase(vd[i].begin() + j);\n break;\n }\n }\n }\n\n if (vd[0].size() < C) {\n vd[0].push_back(book);\n ans += 1;\n } else {\n int p1 = -1, p2 = -1;\n for (int i = 1; i <= M; i++) {\n if (vd[i].size() < C || i == M) {\n if (p1 < 0) {\n p1 = i;\n if (i == M || vd[i].size() + 1 < C) i--;\n } else if (p2 < 0) {\n p2 = i;\n }\n }\n }\n\n int rebook = vd[0].front();\n vd[0].pop_front();\n \n vd[p2].push_back(rebook);\n vd[0].push_back(book);\n\n ans += (p1 + 1) * 2 + 1;\n ans += p2 + 2;\n }\n }\n\n cout << ans << endl;\n }\n\n return 0;\n}", "accuracy": 1, "time_ms": 20, "memory_kb": 3184, "score_of_the_acc": -1.1848, "final_rank": 19 }, { "submission_id": "aoj_1258_2291125", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\n#define int long long\ntypedef pair<int,int> P;\nsigned main(){\n int m,c,n;\n while(cin>>m>>c>>n,m||c||n){\n int k[n];\n int b[111][55];\n for(int i=0;i<n;i++){\n cin>>k[i];\n for(int j=0;j<k[i];j++) cin>>b[i][j];\n }\n list<int> l[m];\n queue<P> q;\n for(int i=0;i<n;i++) q.push(P(i,0));\n int ans=0;\n while(!q.empty()){\n P p=q.front();q.pop();\n int w=p.first,x=p.second;\n int pos=m;\n for(int i=0;i<m;i++){\n\tauto latte=find(l[i].begin(),l[i].end(),b[w][x]);\n\tif(latte!=l[i].end()){\n\t pos=i;\n\t l[i].erase(latte);\n\t break;\n\t}\n }\n ans+=pos+1;\n if((int)l[0].size()<c){\n\tl[0].push_front(b[w][x]);\n\tans+=1;\n }else{\n\tint tmp=m;\n\tfor(int i=1;i<m;i++){\n\t if((int)l[i].size()<c){\n\t tmp=i;\n\t l[tmp].push_back(b[w][x]);\n\t break;\n\t }\n\t}\n\tans+=tmp+1;\n\t//cout<<ans<<endl;\n\tint le=l[0].back();\n\tl[0].pop_back();\n\tans+=1;\n\tint pos=m;\n\tfor(int i=1;i<m;i++){\n\t if((int)l[i].size()<c){\n\t pos=i;\n\t l[pos].push_front(le);\n\t break;\n\t }\n\t}\n\tans+=pos+1;\n\t//cout<<ans<<endl;\n\tif(tmp<m) l[tmp].pop_back();\n\tans+=tmp+1;\n\t//cout<<ans<<endl;\n\tl[0].push_front(b[w][x]);\n\tans+=1;\n\t//cout<<ans<<endl;\n }\n if(x+1<k[w]) q.push(P(w,x+1));\n }\n cout<<ans<<endl;\n }\n return 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3160, "score_of_the_acc": -0.9735, "final_rank": 16 }, { "submission_id": "aoj_1258_2290421", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\n\nint M,C,N;\n\nmap<int,int> desk[111];\nint take(int bk,int &res){ \n for(int i=0;i<M;i++){\n if( desk[i].count(bk) ) {\n res += i+1;\n desk[i].erase(bk);\n return i;\n }\n }\n res += M+1;\n return M;\n}\nint put(int bk,int cnt,int st,int &res){\n for(int i=st;i<M;i++){\n if( (int)desk[i].size() < C ) {\n res += i+1;\n desk[i][bk] = cnt;\n return i; \n }\n }\n res += M+1;\n return M;\n}\n\npair<int,int> mink(map<int,int> &mp){\n auto p = make_pair(0,(1<<19));\n for( auto it = mp.begin(); it != mp.end(); it++ ){\n if( p.second > it->second ) p = *it;\n }\n return p;\n}\n\nint main(){\n cin.tie(0);\n ios::sync_with_stdio(false);\n\n while( cin >> M >> C >> N && (M|C|N) ){\n\n queue<int> q;\n queue<int> hq[111];\n\n\n for(int i=0;i<N;i++){\n int k; cin >> k;\n for(int j=0;j<k;j++){\n int b; cin >> b; hq[i].push(b);\n }\n q.push(i);\n }\n int res = 0;\n int cnt = 0;\n while( !q.empty() ) {\n cnt++;\n int p = q.front(); q.pop();\n if( hq[p].empty() ) continue;\n int bk = hq[p].front(); hq[p].pop();\n\n\n take(bk,res);\n int pid = put(bk,cnt,0,res);\n if( pid != 0 ){\n auto dt = mink(desk[0]);\n take( dt.first, res );\n put( dt.first, dt.second, 1, res );\n\n take( bk,res);\n assert( put(bk, cnt, 0, res) == 0 );\n }\n //cout << cnt << \": \" << res << endl;\n q.push( p );\n }\n for(int i=0;i<=M;i++) desk[i].clear();\n cout << res << endl;\n }\n}", "accuracy": 1, "time_ms": 20, "memory_kb": 3112, "score_of_the_acc": -1.1508, "final_rank": 18 }, { "submission_id": "aoj_1258_2023069", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\n\nint M,C,N;\nint D[20][111];\nint Ds[20];\nint used[111];\nint pl[111];\nint res;\nconst int MAX = 101;\nvector<int> B[111];\nvoid init(){\n memset( D, 0, sizeof( D ) );\n memset( Ds,0,sizeof( Ds ) );\n memset( pl,0,sizeof( pl ) );\n memset(used,0,sizeof(used));\n res = 0;\n used[MAX] = (1<<29);\n}\n\nvoid put(int q,int id){\n Ds[id]++;\n D[id][q] = 1;\n pl[q] = id;\n res += id+1;\n}\n\nvoid take(int q,int id){\n Ds[id]--;\n D[id][q] = 0;\n pl[q] = -1;\n res += id+1;\n}\nbool pcheck(int id){\n if( id==M ) return true;\n return Ds[id] < C;\n}\n\nint main(){\n while( cin >> M >> C >> N && (M||C||N) ){\n\n init();\n for(int i=0;i<N;i++){\n int k; cin >> k;\n B[i].resize(k);\n for( int &b : B[i] ) {\n cin >> b;\n D[M][b] = 1;\n pl[b] = M;\n }\n }\n vector<int> query;\n for(int j=0;j<=50;j++){\n for(int i=0;i<N;i++){\n if( (int)B[i].size() <= j ) continue;\n query.push_back( B[i][j] );\n }\n }\n\n int cnt=1;\n res = 0;\n for( int q: query ){\n used[q] = cnt++;\n for(int i=0;i<=M;i++){\n if( pcheck( i ) || i == pl[q] ){\n take( q, pl[q] );\n put( q, i ); \n break;\n }\n }\n if( pl[q] == 0 ) continue;\n int maxid = MAX;\n for(int i=0;i<=100;i++){\n if( D[0][i] )\n if( used[maxid] > used[i] ) maxid = i; \n }\n for(int i=1;i<=M;i++){\n if( pcheck( i ) ){ \n take( maxid, pl[maxid] );\n put( maxid, i ); \n break;\n } \n }\n take( q, pl[q] );\n put( q, 0 );\n }\n cout << res << endl;\n\n }\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3084, "score_of_the_acc": -0.9375, "final_rank": 14 }, { "submission_id": "aoj_1258_2023038", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\ntypedef long long ll;\n\nint main(){\n while(1){\n ll m,c,n;\n cin>>m>>c>>n;\n if(!m&&!c&&!n)return 0;\n\n vector<ll> book[101],D[21];\n \n for(int i=0;i<n;i++){\n int k;\n cin>>k;\n book[i].resize(k);\n for(int j=0;j<k;j++) cin>>book[i][j],D[m].push_back(book[i][j]);\n }\n\n queue<int> Q;\n for(int i=0;i<100;i++)\n for(int j=0;j<n;j++)\n\tif((int)book[j].size()>i)Q.push(book[j][i]);\n \n ll d[11],ans=0;\n for(int i=0;i<m;i++)d[i]=c; \n d[m]=1e9;\n \n while(!Q.empty()){\n ll id=Q.front();Q.pop();\n ll cost=0;\n\n for(int i=0;i<=m&&!cost;i++)\n\tfor(int j=0;j<(int)D[i].size()&&!cost;j++)\n\t if(id==D[i][j])cost=i+1,D[i].erase(D[i].begin()+j);\n \n ans+=cost;\n cost=0;\n if(D[0].size()<d[0]){\n\tD[0].push_back(id),ans+=1;\n\tcontinue;\n }\n\n int pos=0;\n for(int i=0;i<=m&&!cost;i++)\n\tif(D[i].size()<d[i])D[i].push_back(id),cost=(i+1)*2+1,pos=i;\n \n ans+=cost;\n cost=0;\n for(int i=0;i<=m&&!cost;i++)\n\tif(D[i].size()<d[i]){\n\t D[i].push_back(D[0][0]);\n\t D[0].erase(D[0].begin());\n\t cost=i+2;\n\t}\n ans+=cost; \n cost=0;\n for(int i=0;i<D[pos].size();i++) if(D[pos][i]==id)D[pos].erase(D[pos].begin()+i);\n D[0].push_back(id);\n }\n cout<<ans<<endl;\n }\n return 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3088, "score_of_the_acc": -0.9394, "final_rank": 15 }, { "submission_id": "aoj_1258_1589937", "code_snippet": "#include <algorithm>\n#include <vector>\n#include <cfloat>\n#include <string>\n#include <cmath>\n#include <set>\n#include <cstdlib>\n#include <map>\n#include <ctime>\n#include <iomanip>\n#include <functional>\n#include <deque>\n#include <iostream>\n#include <cstring>\n#include <queue>\n#include <cstdio>\n#include <stack>\n#include <climits>\n#include <sys/time.h>\n#include <cctype>\n\nusing namespace std;\n\ntypedef long long ll;\n\nint main() {\n while (true) {\n int m, c, n;\n cin >> m >> c >> n;\n if (m == 0 && c == 0 && n == 0) break;\n\n queue <int> storder;\n queue <int> stdesire[n];\n vector <int> d[m];\n for (int i = 0; i < n; i++) {\n storder.push(i);\n int k;\n cin >> k;\n for (int j = 0; j < k; j++) {\n int ID;\n cin >> ID;\n stdesire[i].push(ID);\n }\n }\n int ans = 0;\n while (!storder.empty()) {\n int p = storder.front(); storder.pop();\n int cost = 0;\n int ID = stdesire[p].front(); stdesire[p].pop();\n bool ok = false;\n // ??¬?????¨??£?????????\n for (int i = 0; i < m; i++) {\n for (int j = 0; j < d[i].size(); j++) {\n if (d[i][j] == ID) {\n ok = true;\n cost += i+1;\n d[i].erase(d[i].begin()+j);\n break;\n }\n }\n if (ok) break;\n }\n if (!ok) {\n cost += m+1;\n }\n\n // ??¬?????????\n bool shelf = true;\n for (int i = 0; i < m; i++) {\n if (d[i].size() < c) {\n if (i == 0) {\n d[i].push_back(ID);\n cost += 1;\n }else {\n cost += (i+1)*2+1; ok = false;\n for (int j = 1; j < m; j++) {\n int bias = j == i;\n if (d[j].size()+bias < c) {\n int t = d[0][0];\n d[0].erase(d[0].begin());\n d[j].push_back(t); cost += 1+j+1;\n ok = true;\n break;\n }\n }\n if (!ok) {\n int t = d[0][0];\n d[0].erase(d[0].begin());\n cost += 1+m+1;\n }\n d[0].push_back(ID);\n }\n shelf = false;\n break;\n }\n }\n if (shelf) {\n cost += (m+1)*2+1+(m+1)+1;\n d[0].erase(d[0].begin());\n d[0].push_back(ID);\n }\n if (stdesire[p].size()) {\n storder.push(p);\n }\n\n ans += cost;\n }\n\n cout << ans << endl;\n }\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 1284, "score_of_the_acc": -0.0852, "final_rank": 5 }, { "submission_id": "aoj_1258_1420515", "code_snippet": "#pragma warning (disable:4996)\n#include<iostream>\n#include<cstdio>\n#include<queue>\n#include<cstring>\n#include<string>\n#include<cmath>\nusing namespace std;\nint m, c, n;\nint k[102];\nint b[102][52];\nvector<int> desk[12];//&#27599;&#24352;&#26700;子上放的&#20070;&#32534;号\nint amount[12];\nint que[5005];//拿&#20070;&#39034;序\nint maxk;\nint max(int a, int b){\n\tif (a > b)return a;\n\telse return b;\n}\nint main(){\n\twhile (cin >> m >> c >> n){\n\t\tif (!m&&!n&&!c) break;\n\t\tint i, j,p;\n\t\t\n\t\tmemset(b, 0, sizeof(b));\n\t\tmemset(desk, 0, sizeof(desk));\n\t\tmemset(amount, 0, sizeof(amount));\n\t\tmemset(que, 0, sizeof(que));\n\t\tmemset(k, 0, sizeof(k));\n\t\tmaxk = -10;\n\n\t\tfor ( i = 1; i <= n; i++){\n\t\t\tcin >> k[i];\n\t\t\tmaxk = max(maxk, k[i]);\n\t\t\tfor ( j = 1; j <= k[i]; j++)\n\t\t\t\tcin >> b[i][j];\n\t\t}\n\t\t\n\n\t\tint ans = 0;\n\t\tint num = 0;\n\n\t\tfor (j = 1; j <= maxk; j++){\n\t\t\tfor (i = 1; i <= n; i++){\n\t\t\t\tif (b[i][j] != 0)\n\t\t\t\t{\n\t\t\t\t\tque[num++] = b[i][j];\n\t\t\t\t\t\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\t\n\t\tint id;\n\t\tint mm = 0;\n\t\t\n\t\t\n\t\twhile (mm < num){\n\t\t\tid = que[mm];\n\t\t\tint flag = 0;\n\t\t\t//找&#20070;\n\t\t\t/*for (j = 1; j <= m; j++){\n\t\t\t\tcout << \"desk[\"<<j<<\"]:\";\n\t\t\t\tfor (i = 0; i < desk[j].size(); i++)\n\t\t\t\t\tcout << desk[j][i] << \" \";\n\t\t\t\tcout << endl;\n\t\t\t}*/\n\t\t\t\t\n\t\t\tfor (i = 1; i <= m; i++){\n\t\t\t\tfor (j = 0; j < desk[i].size(); j++){\n\t\t\t\t\tif (desk[i][j] == id) {\n\t\t\t\t\t\tflag = 1;\n\t\t\t\t\t\tans += i;\n\t\t\t\t\t\t//cout << \"第\"<<mm<<\"cinashu\"<<i << endl;\n\t\t\t\t\t\tamount[i]--;\n\t\t\t\t\t\tdesk[i][j] = 0;//拿走了\n\t\t\t\t\t\tbreak;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\tif (flag)break;\n\t\t\t}\n\t\t\tif (!flag){\n\t\t\t\tans += (m + 1); //cout << \"第\" << mm << \"cinashu\" << m+1 << endl;\n\t\t\t}\n\n\t\t\t//放&#20070;\n\t\t\tint temp;\n\t\t\tif (amount[1] < c){\n\t\t\t\tdesk[1].push_back(id);\n\t\t\t\tans += 1;\n\t\t\t\tamount[1]++;\n\t\t\t\t\n\t\t\t}\n\t\t\telse{\n\t\t\t\tfor (i = 2; i <= m + 1; i++){\n\t\t\t\t\tif (amount[i] < c || (i == m + 1))\n\t\t\t\t\t{\n\n\t\t\t\t\t\ttemp = i;//&#26242;&#26102;放的位置\n\t\t\t\t\t\tamount[i]++;\n\t\t\t\t\t\t\n\t\t\t\t\t\tbreak;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\tint longestid;//很久没用的&#20070;\n\n\t\t\t\tfor (i = 0; i < desk[1].size(); i++){\n\t\t\t\t\tif (desk[1][i])\n\t\t\t\t\t{\n\t\t\t\t\t\tlongestid = desk[1][i];\n\t\t\t\t\t\tdesk[1][i] = 0;\n\t\t\t\t\t\n\t\t\t\t\t\tbreak;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\tdesk[1].push_back(id);\n\t\t\t\t\n\n\t\t\t\tint temp2;\t\n\t\t\t\tfor (p = temp ; p <= m + 1; p++){\n\t\t\t\t\tif (amount[p] < c || p == m + 1){\n\n\t\t\t\t\t\ttemp2 = p;\n\t\t\t\t\t\tdesk[p].push_back(longestid);\n\t\t\t\t\t\tamount[p]++;\n\t\t\t\t\t\tbreak;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\tamount[temp]--;\n\t\t\t\tans += (2 * temp + temp2 + 2);\n\t\t\t\t//cout << temp <<\" \"<< temp2 <<\" \"<<2 * temp + temp2 + 2<< endl;\n\n\n\t\t\t}mm++;\n\t\t}\n\t\t\t\t\n\t\tcout << ans << endl;\n\n\t\t\n\t}return 0;\n}", "accuracy": 1, "time_ms": 60, "memory_kb": 1672, "score_of_the_acc": -1.2689, "final_rank": 20 }, { "submission_id": "aoj_1258_1264482", "code_snippet": "#include <iostream>\n#include <cstdio>\n#include <algorithm>\n#include <string>\n#include <cmath>\n#include <cstring>\n#include <queue>\n#include <set>\n#include <map>\n#include <vector>\ntemplate <class T>\ninline bool rd(T &ret) {\n\tchar c; int sgn;\n\tif(c=getchar(),c==EOF) return 0;\n\twhile(c!='-'&&(c<'0'||c>'9')) c=getchar();\n\tsgn=(c=='-')?-1:1;\n\tret=(c=='-')?0:(c-'0');\n\twhile(c=getchar(),c>='0'&&c<='9') ret=ret*10+(c-'0');\n\tret*=sgn;\n\treturn 1;\n}\ntemplate <class T>\ninline void pt(T x) {\n if (x <0) {\n putchar('-');\n x = -x;\n }\n if(x>9) pt(x/10);\n putchar(x%10+'0');\n}\nusing namespace std;\nconst int inf = (int)1e8;\nconst int N = 55;\ntypedef pair<int, int> pii;\n\nint n, m, c;\n\ndeque<int> Q, q[15];\ndeque<int>::iterator it;\nvector<int>G[105];\nint id[105];\nvoid input(){\n\twhile(!Q.empty())Q.pop_front();\n\tfor(int i = 1, num, j; i <= n; i++){\n\t\tG[i].clear();\n\t\trd(num); while(num--){rd(j); G[i].push_back(j);\t}\n\t\treverse(G[i].begin(), G[i].end());\n\t\tif(G[i].size())Q.push_back(i);\n\t}\n\tfor(int i = 1; i <= m; i++) while(!q[i].empty())q[i].pop_front();\n\tfor(int i = 1; i <= 100; i++)id[i] = m+1;\n}\nint putbook(int book, int from){\n\tfor(int i = from; i <= m; i++){\n\t\tif(q[i].size()<c){\n\t\t\tq[i].push_back(book);\n\t\t\tid[book] = i;\n\t\t\treturn i;\n\t\t}\n\t}\n\tid[book] = m+1;\n\treturn m+1;\n}\nint takebook(int book){\n\tif(id[book] == m+1)return m+1;\n\tfor(it = q[id[book]].begin(); it != q[id[book]].end(); it++)\n\t\tif(*it == book){\n\t\t\tq[id[book]].erase(it); break;\n\t\t}\n\treturn id[book];\n}\nint main() {\n\twhile(~scanf(\"%d %d %d\", &m, &c, &n), n + c+m){\n\t\tinput();\n\t\tint ans = 0;\n\t\twhile(!Q.empty()){\n\t\t\tint stu = Q.front();\tQ.pop_front();\n\t\t\tint book = G[stu][G[stu].size()-1];\n\t\t\tG[stu].erase(G[stu].end()-1, G[stu].end());\n\t\t\tif(G[stu].size()) Q.push_back(stu);\n\t\t\tans += takebook(book);\n\t\t\tans += putbook(book, 1);\n\t\t\tif(id[book]!=1){\n\t\t\t\tint pre = q[1].front();\n\t\t\t\tq[1].pop_front();\n\t\t\t\tans++;\n\t\t\t\tans += putbook(pre, 2);\n\t\t\t\tans += takebook(book);\n\t\t\t\tans += putbook(book, 1);\n\t\t\t}\n\t\t}\n\t\tpt(ans); puts(\"\");\n\t}\n return 0;\n}\n\n/*\n\n9 9\n*#**#**#*\n*#**#**#*\n*#**#**#*\n*#**.**#*\n*#*#.#*#*\n*$##*##$*\n*#*****#*\n*.#.#.#.*\n*********\n3 3\n*$*\n*#*\n*$*\n\n\n*/", "accuracy": 1, "time_ms": 10, "memory_kb": 1244, "score_of_the_acc": -0.0663, "final_rank": 2 }, { "submission_id": "aoj_1258_1262032", "code_snippet": "#define _USE_MATH_DEFINES\n#define INF 0x3f3f3f3f\n\n#include <iostream>\n#include <cstdio>\n#include <sstream>\n#include <cmath>\n#include <cstdlib>\n#include <algorithm>\n#include <queue>\n#include <stack>\n#include <limits>\n#include <map>\n#include <string>\n#include <cstring>\n#include <set>\n#include <deque>\n#include <bitset>\n#include <list>\n#include <cctype>\n#include <utility>\n#include <assert.h>\n \nusing namespace std;\n \ntypedef long long ll;\ntypedef pair <int,int> P;\ntypedef pair <double,P> PP;\n \nstatic const double EPS = 1e-8;\n\nint num_of_desks;\nint book_limit;\nint num_of_students;\n\nint take_book(int id,deque<int> desks[101]){\n for(int desk_i = 0; desk_i < num_of_desks; desk_i++){\n for(int i = 0; i < desks[desk_i].size(); i++){\n if(desks[desk_i][i] == id){\n deque<int> tmp;\n for(int j = 0; j < desks[desk_i].size(); j++){\n if(desks[desk_i][j] == id) continue;\n tmp.push_back(desks[desk_i][j]);\n }\n desks[desk_i] = tmp;\n return desk_i + 1;\n }\n }\n }\n return num_of_desks + 1;\n}\n\nint main(){\n while(~scanf(\"%d %d %d\",&num_of_desks,&book_limit,&num_of_students)){\n if(num_of_desks == 0\n && book_limit == 0\n && num_of_students == 0) break;\n vector<int> students[101];\n int max_request = 0;\n for(int student_i = 0; student_i < num_of_students; student_i++){\n int num_of_books_requested;\n scanf(\"%d\",&num_of_books_requested);\n max_request = max(max_request,num_of_books_requested);\n for(int book_i = 0; book_i < num_of_books_requested; book_i++){\n int id;\n scanf(\"%d\",&id);\n students[student_i].push_back(id);\n }\n }\n queue<int> books;\n for(int book_i = 0; book_i < max_request; book_i++){\n for(int student_i = 0; student_i < num_of_students; student_i++){\n if(students[student_i].size() <= book_i) continue;\n books.push(students[student_i][book_i]);\n }\n }\n\n deque<int> desks[101];\n\n int score = 0;\n while(!books.empty()){\n int id = books.front();\n books.pop();\n\n //take from desk/shelf\n score += take_book(id,desks);\n\n //put \n if(desks[0].size() < book_limit){\n desks[0].push_back(id);\n score += 1;\n }\n else{\n int tmp_place = -1;\n for(int desk_i = 1; desk_i <= num_of_desks; desk_i++){\n //put book\n if(desk_i == num_of_desks || desks[desk_i].size() < book_limit){\n desks[desk_i].push_back(id);\n tmp_place = desk_i;\n score += desk_i + 1;\n break;\n }\n }\n\n //take book\n int old = desks[0].front(); \n desks[0].pop_front();\n score += 1;\n\n bool same_flag = false;\n for(int desk_i = 1; desk_i <= num_of_desks; desk_i++){\n //put book\n if(desk_i == num_of_desks || desks[desk_i].size() < book_limit){\n if(tmp_place == desk_i){\n desks[desk_i].pop_back();\n same_flag = true;\n }\n desks[desk_i].push_back(old);\n score += desk_i + 1;\n break;\n }\n\n }\n\n //take book\n if(!same_flag){\n desks[tmp_place].pop_back();\n }\n score += tmp_place + 1;\n\n //put book\n desks[0].push_back(id);\n score += 1;\n }\n }\n printf(\"%d\\n\",score);\n }\n}", "accuracy": 1, "time_ms": 20, "memory_kb": 1360, "score_of_the_acc": -0.3212, "final_rank": 12 }, { "submission_id": "aoj_1258_1159416", "code_snippet": "#include <string> \n#include <algorithm> \n#include <cfloat> \n#include <climits> \n#include <cmath> \n#include <complex> \n#include <cstdio> \n#include <cstdlib> \n#include <cstring> \n#include <functional> \n#include <iostream> \n#include <map> \n#include <memory> \n#include <queue> \n#include <set> \n#include <sstream> \n#include <stack> \n#include <utility> \n#include <vector> \n\n#define EACH(i,c) for(__typeof((c).begin()) i=(c).begin(); i!=(c).end(); ++i)\n#define ALL(x) (x).begin(),(x).end() \ntypedef long long ll;\nusing namespace std; \nconst double eps = 1e-10;\n\nint main(){\n for(;;){\n int m, c, n;\n cin >> m >> c >> n;\n if(m == 0 && c == 0 && n == 0) return 0;\n vector<int> idx(n);\n vector<vector<int> > b(n),\n desk(m, vector<int>(c, -1)),\n time(m, vector<int>(c, 0));\n for(int i = 0; i < n; ++i){\n int k;\n cin >> k;\n b[i].resize(k);\n for(int j = 0; j < k; ++j) cin >> b[i][j];\n }\n int curr = 0;\n int cost = 0;\n for(;;){\n bool f = false;\n for(int i = 0; i < n; ++i) if(idx[i] < b[i].size()){\n ++curr;\n f = true;\n int j = idx[i]++;\n bool found = false;\n for(int k = 0; k < m; ++k){\n for(int l = 0; l < c; ++l) if(desk[k][l] == b[i][j]){\n cost += k+1;\n desk[k][l] = -1;\n found = true;\n }\n if(found) break;\n }\n if(!found) cost += m+1;\n found = false;\n for(int l = 0; l < c; ++l) if(desk[0][l] < 0){\n cost += 1;\n desk[0][l] = b[i][j];\n time[0][l] = curr;\n found = true;\n break;\n }\n if(found) continue;\n int td = -1, di = -1;\n for(int k = 1; k < m; ++k){\n for(int l = 0; l < c; ++l) if(desk[k][l] < 0){\n cost += k+1;\n td = k;\n di = l;\n desk[k][l] = b[i][j];\n found = true;\n break;\n }\n if(found) break;\n }\n if(!found) {\n td = m;\n cost += m+1;\n }\n cost += 1;\n int mt = 1000000000, mi = -1;\n for(int l=0;l<c;++l) if(mt > time[0][l]){\n mt = time[0][l];\n mi = l;\n }\n found = false;\n for(int k = 1; k < m; ++k){\n for(int l = 0; l < c; ++l) if(desk[k][l] < 0){\n cost += k+1;\n desk[k][l] = desk[0][mi];\n time[k][l] = time[0][mi];\n found = true;\n break;\n }\n if(found) break;\n }\n if(!found) cost += m+1;\n if(td < m) desk[td][di] = -1;\n desk[0][mi] = b[i][j];\n time[0][mi] = curr;\n cost += td+1 + 1;\n }\n if(!f) break;\n }\n cout << cost << endl;\n }\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 1244, "score_of_the_acc": -0.0663, "final_rank": 2 }, { "submission_id": "aoj_1258_1122809", "code_snippet": "#include<stdio.h>\n#include<algorithm>\nusing namespace std;\nint at[110];\nint use[110];\nint last[110];\nint req[110][110];\nint sr[110];\nint lis[11000];\nint main(){\n\tint a,b,c;\n\twhile(scanf(\"%d%d%d\",&a,&b,&c),a){\n\t\tint ret=0;\n\t\tfor(int i=0;i<110;i++)use[i]=0;\n\t\tfor(int i=0;i<110;i++)at[i]=a+1;\n\t\tfor(int i=0;i<110;i++)last[i]=0;\n\t\tfor(int i=0;i<c;i++){\n\t\t\tint d;scanf(\"%d\",&d);\n\t\t\tsr[i]=d;\n\t\t\tfor(int j=0;j<d;j++)scanf(\"%d\",&req[i][j]);\n\t\t}\n\t\tint L=0;\n\t\tfor(int i=0;i<60;i++){\n\t\t\tfor(int j=0;j<c;j++)if(sr[j]>i)lis[L++]=req[j][i];\n\t\t}\n\t\tint t=0;\n\t\tfor(int i=0;i<L;i++){\n\t\t\t\tint e=lis[i];\n\t\t\t\tret+=at[e];\n\t\t\t\tif(at[e]<a+1)use[at[e]]--;\n\t\t\t\tlast[e]=t;\n\t\t\t\tt++;\n\t\t\t\tif(use[1]<b){\n\t\t\t\t\tuse[1]++;ret++;at[e]=1;\n\t\t\t\t}else{\n\t\t\t\t\tfor(int i=2;i<=a+1;i++){\n\t\t\t\t\t\tif(i==a+1||use[i]<b){\n\t\t\t\t\t\t\tuse[i]++;at[e]=i;ret+=i;break;\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t\tint f=9999999;\n\t\t\t\t\tint v=0;\n\t\t\t\t\tfor(int i=0;i<110;i++)if(at[i]==1){\n\t\t\t\t\t\tif(f>last[i]){f=last[i];v=i;}\n\t\t\t\t\t}\n\t\t\t\t\tret++;\n\t\t\t\t\tfor(int i=2;i<=a+1;i++){\n\t\t\t\t\t\tif(i==a+1||use[i]<b){\n\t\t\t\t\t\t\tuse[i]++;at[v]=i;ret+=i;break;\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t\tuse[at[e]]--;ret+=at[e];ret++;at[e]=1;\n\t\t\t\t}\n\t\t\t\n\t\t}\n\t\tprintf(\"%d\\n\",ret);\n\t}\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 1104, "score_of_the_acc": 0, "final_rank": 1 }, { "submission_id": "aoj_1258_1067661", "code_snippet": "#include <climits>\n#include <cstdlib>\n#include <iostream>\n#include <queue>\n#include <unordered_map>\n#include <vector>\nusing namespace std;\n \nint main() {\n\tcin.tie(nullptr);\n\tios::sync_with_stdio(false);\n\n\tfor(int m, c, n; cin >> m >> c >> n && m;) {\n\t\tvector<unordered_map<int, int>> desks(m);\n \n\t\tqueue<int> que;\n\t\tvector<int> index(n, 0);\n\t\tvector<vector<int>> books(n);\n \n\t\tfor(int i = 0; i < n; ++i) {\n\t\t\tque.push(i);\n \n\t\t\tint k;\n\t\t\tcin >> k;\n\n\t\t\tbooks[i].reserve(k);\n \n\t\t\tfor(int j = 0; j < k; ++j) {\n\t\t\t\tint b;\n\t\t\t\tcin >> b;\n\t\t\t\tbooks[i].emplace_back(b);\n\t\t\t}\n\t\t}\n \n\t\tint ans = 0;\n\n\t\tfor(int t = 0; !que.empty(); ++t) {\n\t\t\tconst int student = que.front();\n\t\t\tque.pop();\n \n\t\t\tconst int id = books[student][index[student]];\n\t\t\tif(++index[student] < books[student].size()) {\n\t\t\t\tque.push(student);\n\t\t\t}\n\n\t\t\tint pos = m;\n\t\t\tfor(int i = 0; i < m; ++i) {\n\t\t\t\tif(desks[i].count(id)) {\n\t\t\t\t\tdesks[i].erase(id);\n\t\t\t\t\tpos = i;\n\t\t\t\t\tbreak;\n\t\t\t\t}\n\t\t\t}\n \n\t\t\tans += pos + 1;\n \n\t\t\tint tmp = m;\n\t\t\tfor(int i = 0; i < m; ++i) {\n\t\t\t\tif(desks[i].size() < c) {\n\t\t\t\t\tdesks[i].insert({id, t});\n\t\t\t\t\ttmp = i;\n\t\t\t\t\tbreak;\n\t\t\t\t}\n\t\t\t}\n \n\t\t\tans += tmp + 1;\n \n\t\t\tif(tmp != 0) {\n\t\t\t\tint r_id = -1;\n\t\t\t\tint r_time = INT_MAX;\n \n\t\t\t\tfor(const auto &e : desks.front()) {\n\t\t\t\t\tif(r_time > e.second) {\n\t\t\t\t\t\tr_id = e.first;\n\t\t\t\t\t\tr_time = e.second;\n\t\t\t\t\t}\n\t\t\t\t}\n \n\t\t\t\tans += 1;\n\t\t\t\tdesks.front().erase(r_id);\n \n\t\t\t\tint to = m;\n\t\t\t\tfor(int i = 1; i < m; ++i) {\n\t\t\t\t\tif(desks[i].size() < c) {\n\t\t\t\t\t\tdesks[i].insert({r_id, r_time});\n\t\t\t\t\t\tto = i;\n\t\t\t\t\t\tbreak;\n\t\t\t\t\t}\n\t\t\t\t}\n \n\t\t\t\tans += to + 1;\n \n\t\t\t\tif(tmp < m) desks[tmp].erase(id);\n\t\t\t\tans += tmp + 1;\n \n\t\t\t\tdesks.front().insert({id, t});\n\t\t\t\tans += 1;\n\t\t\t}\n\t\t}\n \n \n\t\tcout << ans << endl;\n\t}\n \n\treturn EXIT_SUCCESS;\n}", "accuracy": 1, "time_ms": 20, "memory_kb": 1256, "score_of_the_acc": -0.272, "final_rank": 9 }, { "submission_id": "aoj_1258_1066998", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\n#define dump(a) (cerr << #a << \" = \" << (a) << endl)\n\n#define REP(i,a,b) for(int i=(a);i < (int)(b); i++)\n#define rep(i,n) REP(i,0,n)\n#define ALL(v) begin(v), end(v)\n\nint main(){\n for(int m, c, n; cin >> m >> c >> n && m;) {\n vector<unordered_map<int, int>> desks(m);\n\n queue<int> que;\n vector<int> index(n, 0);\n vector<vector<int>> books(n);\n\n for(int i = 0; i < n; ++i) {\n que.push(i);\n\n int k;\n cin >> k;\n\n for(int j = 0; j < k; ++j) {\n\tint b;\n\tcin >> b;\n\tbooks[i].emplace_back(b);\n }\n }\n\n int ans = 0;\n for(int t = 0; !que.empty(); ++t) {\n const int student = que.front();\n que.pop();\n\n const int id = books[student][index[student]];\n if(++index[student] < books[student].size()) {\n\tque.push(student);\n }\n\n int pos = m;\n for(int i = 0; i < m; ++i) {\n\tif(desks[i].count(id)) {\n\t desks[i].erase(id);\n\t pos = i;\n\t break;\n\t}\n }\n\n ans += pos + 1;\n\n int tmp = m;\n for(int i = 0; i < m; ++i) {\n\tif(desks[i].size() < c) {\n\t desks[i].insert({id, t});\n\t tmp = i;\n\t break;\n\t}\n }\n\n ans += tmp + 1;\n\n if(tmp != 0) {\n\tint r_id = -1;\n\tint r_time = INT_MAX;\n\n\tfor(const auto &e : desks[0]) {\n\t if(r_time > e.second) {\n\t r_id = e.first;\n\t r_time = e.second;\n\t }\n\t}\n\n\tans += 1;\n\tdesks[0].erase(r_id);\n\n\tint to = m;\n\tfor(int i = 1; i < m; ++i) {\n\t if(desks[i].size() < c) {\n\t desks[i].insert({r_id, r_time});\n\t to = i;\n\t break;\n\t }\n\t}\n\n\tans += to + 1;\n\n\tif(tmp < m) desks[tmp].erase(id);\n\tans += tmp + 1;\n\n\tdesks[0].insert({id, t});\n\tans += 1;\n }\n }\n\n\n cout << ans << endl;\n }\n\n return 0;\n}", "accuracy": 1, "time_ms": 20, "memory_kb": 1264, "score_of_the_acc": -0.2758, "final_rank": 10 }, { "submission_id": "aoj_1258_816828", "code_snippet": "#include<algorithm>\n#include<bitset>\n#include<cctype>\n#include<cmath>\n#include<complex>\n#include<cstdio>\n#include<cstring>\n#include<iostream>\n#include<map>\n#include<numeric>\n#include<queue>\n#include<set>\n#include<sstream>\n#include<stack>\n#include<string>\n#include<vector>\n\n#define repi(i,a,b) for(int i = int(a); i < int(b); i++)\n#define rep(i,a) repi(i,0,a)\n#define repd(i,a,b) for(int i = int(a); i >= int(b); i--)\n#define repit(i,a) for(__typeof((a).begin()) i = (a).begin(); i != (a).end(); i++)\n#define all(u) (u).begin(),(u).end()\n#define rall(u) (u).rbegin(),(u).rend()\n#define UNIQUE(u) (u).erase(unique(all(u)),(u).end())\n#define pb push_back\n#define mp make_pair\nconst double pi = acos(-1.0);\n\nusing namespace std;\n\ntypedef long long ll;\n\nint m, c, n;\nsigned main(){\n while(cin >> m >> c >> n, m||c||n) {\n vector<vector<int> > b(n);\n rep(i, n) {\n int k;\n cin >> k;\n b[i].resize(k);\n rep(j, k) cin >> b[i][j];\n }\n\n vector<int> book;\n queue<pair<int, int> > q;\n rep(i, n) q.push(mp(i, 0));\n while(!q.empty()) {\n int id = q.front().first;\n int index = q.front().second;\n q.pop();\n if(index == b[id].size()) continue;\n book.pb(b[id][index]);\n q.push(mp(id, index+1));\n }\n \n int ans = 0;\n vector<vector<int> > D(m);\n vector<int> place(101);\n rep(i, 101) place[i] = m;\n\n rep(i, book.size()) {\n int nbook = book[i];\n // take book\n ans += place[nbook] + 1;\n if(place[nbook] != m) {\n vector<int>::iterator itr = find(all(D[place[nbook]]), nbook);\n D[place[nbook]].erase(itr, itr+1);\n }\n// cout << \"place of \" << i << \"th book : \" << place[nbook] << endl;\n if(D[0].size() == c) {\n // 1\n int closest = -1;\n rep(j, m) if(D[j].size() < c) {\n closest = j;\n break;\n }\n if(closest >= 0) {\n place[nbook] = closest;\n ans += closest + 1;\n D[closest].pb(nbook);\n } else {\n place[nbook] = m;\n ans += m+1;\n }\n// cout << i << \", 1 : \" << ans << endl;\n \n // 2\n int oldest = D[0][0];\n D[0].erase(D[0].begin(), D[0].begin() + 1);\n ans++;\n// cout << i << \", 2 : \" << ans << endl;\n\n // 3\n closest = -1;\n repi(j, 1, m) if(D[j].size() < c) {\n closest = j;\n break;\n }\n if(closest >= 0) {\n place[oldest] = closest;\n ans += closest + 1;\n D[closest].pb(oldest);\n } else {\n place[oldest] = m;\n ans += m+1;\n }\n// cout << i << \", 3 : \" << ans << endl;\n\n // 4\n ans += place[nbook] + 1;\n if(place[nbook] != m) {\n vector<int>::iterator itr = find(all(D[place[nbook]]), nbook);\n D[place[nbook]].erase(itr, itr+1);\n }\n// cout << i << \", 4 : \" << ans << endl;\n\n // 5\n D[0].pb(nbook);\n place[nbook] = 0;\n ans++;\n// cout << i << \", 5 : \" << ans << endl;\n \n } else {\n D[0].pb(nbook);\n place[nbook] = 0;\n ans++;\n }\n }\n cout << ans << endl;\n }\n return 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 1300, "score_of_the_acc": -0.0928, "final_rank": 6 }, { "submission_id": "aoj_1258_776196", "code_snippet": "#include<iostream>\n#include<cstdio>\n#include<string>\n#include<vector>\n#include<cstdlib>\n#include<cctype>\nusing namespace std;\n#define P pair<int, const char*>\n#define REP(i, j) for(int i = 0; i < j; i++)\n#define FOR(i, j, k) for(int i = j; i < k; i++)\n\nvector<int> books;\nvector< vector<int> > desks;\nint ans = 0;\n\nvoid replace_desks(int did){\n REP(i, desks[did].size()){\n if(desks[did][i] != -1) continue;\n FOR(j, i, desks[did].size()){\n if(desks[did][j] != -1){\n swap(desks[did][i], desks[did][j]);\n break;\n }\n }\n }\n}\n\nbool is_full(int did){\n REP(i, desks[did].size()) if(desks[did][i] == -1) return false;\n return true;\n}\n\nint take_oldbook(){\n int tmp = desks[0][0];\n desks[0][0] = -1;\n replace_desks(0);\n ans += 1;\n return tmp;\n}\n\nvoid put_oldbook(int book){\n FOR(i, 1, desks.size()){\n REP(j, desks[i].size()){\n if(desks[i][j] == -1){\n desks[i][j] = book;\n ans += (i + 1);\n return ;\n }\n }\n }\n ans += (desks.size() + 1);\n return ;\n}\n\nvoid take_book(int book){\n REP(i, desks.size()){\n REP(j, desks[i].size()){\n if(desks[i][j] == book){\n desks[i][j] = -1;\n replace_desks(i);\n ans += (i + 1);\n return ;\n }\n }\n }\n ans += (desks.size() + 1);\n return ;\n}\n\nvoid put_book(int book){\n REP(i, desks.size()){\n REP(j, desks[i].size()){\n if(desks[i][j] == -1){\n desks[i][j] = book;\n ans += (i + 1);\n return ;\n }\n }\n }\n ans += (desks.size() + 1);\n return ;\n}\n\nint main(){\n int m, c, n;\n while(cin >>m >>c >>n && m){\n ans = 0;\n books = vector<int>();\n desks = vector< vector<int> >(m, vector<int>(c, -1));\n vector< vector<int> > tmp(n);\n int tmp_int = 0;\n REP(i, n){\n int k; cin >>k;\n vector<int> tmp2(k);\n REP(j, k) cin >>tmp2[j];\n tmp[i] = tmp2;\n tmp_int = max(tmp_int, (int)(tmp2.size()));\n }\n REP(i, tmp_int){\n REP(j, n){\n if(i >= tmp[j].size()) continue;\n books.push_back(tmp[j][i]);\n }\n }\n\n REP(i, books.size()){\n take_book(books[i]);\n if(is_full(0)){\n put_book(books[i]);\n int tmp_book = take_oldbook();\n put_oldbook(tmp_book);\n take_book(books[i]);\n put_book(books[i]);\n } else{\n put_book(books[i]);\n }\n }\n cout <<ans <<endl;\n }\n return 0;\n}", "accuracy": 1, "time_ms": 20, "memory_kb": 1288, "score_of_the_acc": -0.2871, "final_rank": 11 }, { "submission_id": "aoj_1258_763232", "code_snippet": "#include<iostream>\n#include<cstdio>\n#include<deque>\n#include<vector>\n#include<algorithm>\n#include<cassert>\n#define REP(i,s,n) for(int i=s;i<n;i++)\n#define rep(i,n) REP(i,0,n)\n#define inf (1<<29)\n#define MAX 110\nusing namespace std;\n\nstruct P\n{\n int id; \n bool ex;\n P(int id=-inf,bool ex=true):id(id),ex(ex){}\n};\n\nint CNT[MAX];\ndeque<P> desk[MAX];\ndeque<int> Person[MAX];\nint m,c,n,ans,rem;\n\nint getNext(int index)\n{\n while(Person[index].empty())index = (index+1)%n;\n return index;\n}\n\nvoid take(int index)\n{\n bool ok = false;\n rep(i,m)\n {\n rep(j,desk[i].size())\n\t{\n\t //cout << \"desk[\" << i << \"][\" << j << \"] = \" << desk[i][j].id << \" : \" << desk[i][j].ex << endl;\n\t if(!desk[i][j].ex)continue;\n\t if(desk[i][j].id == index)\n\t {\n\t desk[i][j].ex = false,ans += (i+1);\n\t CNT[i]--;\n\t ok = true;\n\t //cout << \"take from desk:\" << i << \" +\" << (i+1) << endl;\n\t return;\n\t }\n\t}\n while(!desk[i].empty() && !desk[i].front().ex)desk[i].pop_front();\n }\n //cout << \"take from shelf +\" << m+1 << endl;\n ans += (m+1);\n}\n\nbool check(int index)\n{\n //cout << \"CNT[0]+1 = \" << CNT[0]+1 << \" <= \" << c << endl;\n if(CNT[0]+1 <= c)\n {\n //cout << \"book : \" << index << \" is able to put desk : \" << 0 << endl;\n ans++;\n CNT[0]++;\n desk[0].push_back(P(index,true));\n return true;\n }\n return false;\n}\n\nvoid put(int index)\n{\n\n if(check(index))return;\n \n //put \n int pre=-inf;\n bool ok = false;\n rep(i,m)\n if(CNT[i]+1 <= c)\n {\n\t//cout << \"一時的にdesk\" << i << \"におく & そこから再度取り出す\" << endl;\n\tCNT[i]++;\n\tpre = i;\n\tans += (i+1)*2;\n\tok = true;\n\tbreak;\n }\n if(!ok)ans += (m+1)*2;\n\n //take from desk 1\n ans++;\n while(!desk[0].empty() && !desk[0].front().ex)desk[0].pop_front();\n P p = desk[0].front(); desk[0].pop_front();\n CNT[0]--;\n assert(CNT[0] >= 0);\n //cout << \"0番の本 \" << p.id << \" をとる\" << endl;\n\n //put\n ok = false;\n REP(i,1,m)\n if(CNT[i]+1 <= c)\n {\n\t//cout <<\"0番からの本 \" << p.id << \"を机\" << i << \"におく\" << endl; \n\tCNT[i]++;\n\tans += (i+1);\n\tp.ex = true;\n\tdesk[i].push_back(p);\n\tok = true;\n\tbreak;\n }\n if(!ok)ans += (m+1);\n if(pre != -inf)CNT[pre]--;\n \n ans++;\n CNT[0]++;\n desk[0].push_back(P(index,true));\n \n}\n\nint main()\n{\n\n while(cin >> m >> c >> n,m|n|c)\n {\n rem = n;\n ans = 0;\n rep(i,m+1)desk[i].clear(),CNT[i] = 0;\n rep(i,n)Person[i].clear();\n rep(i,n)\n\t{\n\t int k,b;\n\t cin >> k;\n\t rep(j,k)\n\t {\n\t cin >> b;\n\t Person[i].push_back(b);\n\t }\n\t}\n\n int index = 0;\n while(rem)\n\t{\n\t //cout << endl;\n\t //rep(i,m)cout << \"desk[\" << i << \"] = \"<< CNT[i] << \" \";\n\t //cout << endl;\n\n\t index = getNext(index);\n\t //cout << \"index = \" << index << endl;\n\t int target = Person[index].front(); Person[index].pop_front();\n\t //cout << \"target = \" << target << endl; \n\t if(Person[index].empty())rem--;\n\n\t take(target);\n\t //cout << \"after take, ans = \" << ans << endl;\n\t put(target);\n\t //cout << \"after put, ans = \"<< ans << endl;\n\t index = (index+1)%n;\n\t}\n cout << ans << endl;\n }\n return 0;\n}", "accuracy": 1, "time_ms": 20, "memory_kb": 1380, "score_of_the_acc": -0.3307, "final_rank": 13 }, { "submission_id": "aoj_1258_546673", "code_snippet": "#include<iostream>\n#include<vector>\n\nusing namespace std;\n\nconst int MAX_D = 11;\nint m,c,n,ans;\nvector<int> req;\nvector<int> D[MAX_D];\n\nvoid input(){\n vector<int> tmp[n];\n int sum = 0;\n \n for(int i = 0; i < n; i++){\n int num;\n cin >> num;\n sum += num;\n for(int j = 0; j < num; j++){\n int in;\n cin >> in;\n tmp[i].push_back(in);\n }\n }\n \n int pos = 0;\n while(sum){\n for(int i = 0; i < n; i++)\n if(tmp[i].size() > pos){\n\treq.push_back(tmp[i][pos]);\n\tsum--;\n }\n pos++;\n }\n \n D[m] = req;\n}\n\nvoid init(){\n req.clear();\n for(int i = 0; i < MAX_D; i++)\n D[i].clear();\n ans = 0;\n}\n\nint find(int num){\n \n vector<int>::iterator ite;\n \n for(int i = 0; i <= m; i++)\n for(ite = D[i].begin(); ite < D[i].end(); ite++)\n if(*ite == num){\n\tD[i].erase(ite);\n\treturn i+1;\n }\n}\n\nint ret(int num){\n int cost = 0;\n \n if(D[0].size() < c){\n D[0].push_back(num);\n return 1;\n }else{\n int minpos = m;\n for(int i = 0; i < m; i++)\n if(D[i].size() < c){\n\tminpos = i;\n\tbreak;\n }\n \n //from counter to minpos+1\n cost += minpos+1;\n \n int tmppos = m;\n for(int i = 0; i < m; i++)\n if(i == minpos){\n\tif(D[i].size()+1 < c){\n\t tmppos = i;\n\t break;\n\t}\n }else if(D[i].size() < c){\n\ttmppos = i;\n\tbreak;\n }\n //from 1 to (tmppos+1)\n cost+=tmppos+2;\n \n D[tmppos].push_back(D[0][0]);\n D[0].erase(D[0].begin());\n \n \n D[0].push_back(num);\n \n //from (minpos+1) to 1\n cost += minpos+2;\n }\n return cost;\n}\nvoid solve(){\n \n for(int i = 0; i < req.size(); i++){\n int fpos = find(req[i]);\n int rcos = ret(req[i]);\n ans += fpos+rcos;\n }\n cout << ans << endl;\n}\n\nint main(){\n \n while(cin >> m >> c >> n && m+c+n){\n init();\n input();\n solve();\n }\n return 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 1352, "score_of_the_acc": -0.1174, "final_rank": 7 }, { "submission_id": "aoj_1258_546237", "code_snippet": "#include <iostream>\n#include <algorithm>\n#include <vector>\n#include <set>\nusing namespace std;\n\nconst int MAXN = 105;\nconst int MAXK = 52;\nconst int MAXM = 12;\nconst int INF = 1<<28;\nint M, C, N;\nint K[MAXN];\nint B[MAXN][MAXK];\nvector<int> D[MAXM];\nint size[MAXM];\nint pos[MAXN];\n\nint tottekuru(int req) {\n int res = 0;\n for(int i = 0; i < M; ++i) {\n vector<int>::iterator it\n = find(D[i].begin(), D[i].end(), req);\n if(it != D[i].end()) {\n D[i].erase(it);\n res += i+1;\n break;\n }\n }\n\n if(D[0].size() < size[0]) {\n D[0].push_back(req);\n res += 1;\n return res;\n }\n\n int i, j;\n for(i = 1; i < M; ++i) {\n if(D[i].size() < size[i]) {\n break;\n }\n }\n for(j = i; j < M; ++j) {\n if(D[j].size() + (i == j) < size[j]) {\n break;\n }\n }\n int tmp = *D[0].begin();\n D[0].erase(D[0].begin());\n D[j].push_back(tmp);\n D[0].push_back(req);\n res += i+1 + 1 + j+1 + i+1 + 1;\n return res;\n}\n\nint main() {\n while(cin >> M >> C >> N && (M|C|N)) {\n set<int> s;\n for(int i = 0; i < N; ++i) {\n cin >> K[i];\n for(int j = 0; j < K[i]; ++j) {\n cin >> B[i][j];\n s.insert(B[i][j]);\n }\n }\n ++M;\n for(int i = 0; i < M; ++i) {\n if(i+1 == M) size[i] = INF;\n else size[i] = C;\n }\n for(int i = 0; i < MAXM; ++i) D[i].clear();\n D[M-1] = vector<int>(s.begin(), s.end());\n\n fill(pos, pos+MAXN, 0);\n int res = 0;\n while(1) {\n bool flag = false;\n for(int i = 0; i < N; ++i) {\n if(pos[i] == K[i]) continue;\n flag = true;\n int req = B[i][pos[i]++];\n res += tottekuru(req);\n }\n if(!flag) break;\n }\n cout << res << endl;\n }\n return 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 1252, "score_of_the_acc": -0.0701, "final_rank": 4 }, { "submission_id": "aoj_1258_545646", "code_snippet": "#include<iostream>\n#include<vector>\n\nusing namespace std;\n\nconst int MAX_D = 11;\nint m,c,n,ans;\nvector<int> req;\nvector<int> D[MAX_D];\n\nvoid input(){\n vector<int> tmp[n];\n int sum = 0;\n\n for(int i = 0; i < n; i++){\n int num;\n cin >> num;\n sum += num;\n for(int j = 0; j < num; j++){\n int in;\n cin >> in;\n tmp[i].push_back(in);\n }\n }\n \n int pos = 0;\n while(sum){\n for(int i = 0; i < n; i++)\n if(tmp[i].size() > pos){\n\treq.push_back(tmp[i][pos]);\n\tsum--;\n }\n pos++;\n }\n\n D[m] = req;\n}\n\nvoid init(){\n req.clear();\n for(int i = 0; i < MAX_D; i++)\n D[i].clear();\n ans = 0;\n}\n\nint find(int num){\n\n vector<int>::iterator ite;\n\n for(int i = 0; i <= m; i++)\n for(ite = D[i].begin(); ite < D[i].end(); ite++)\n if(*ite == num){\n\tD[i].erase(ite);\n\treturn i+1;\n }\n}\n\nint ret(int num){\n int cost = 0;\n\n if(D[0].size() < c){\n D[0].push_back(num);\n return 1;\n }else{\n int minpos = m;\n for(int i = 0; i < m; i++)\n if(D[i].size() < c){\n\tminpos = i;\n\tbreak;\n }\n\n //from counter to minpos+1\n cost += minpos+1;\n\n int tmppos = m;\n for(int i = 0; i < m; i++)\n if(i == minpos){\n\tif(D[i].size()+1 < c){\n\t tmppos = i;\n\t break;\n\t}\n }else if(D[i].size() < c){\n\ttmppos = i;\n\tbreak;\n }\n //from 1 to (tmppos+1)\n cost+=tmppos+2;\n\n D[tmppos].push_back(D[0][0]);\n D[0].erase(D[0].begin());\n\n\n D[0].push_back(num);\n\n //from (minpos+1) to 1\n cost += minpos+2;\n }\n return cost;\n}\nvoid solve(){\n\n for(int i = 0; i < req.size(); i++){\n int fpos = find(req[i]);\n int rcos = ret(req[i]);\n ans += fpos+rcos;\n }\n cout << ans << endl;\n}\n\nint main(){\n\n while(cin >> m >> c >> n && m+c+n){\n init();\n input();\n solve();\n }\n return 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 1352, "score_of_the_acc": -0.1174, "final_rank": 7 } ]
aoj_1252_cpp
Problem E: Confusing Login Names Meikyokan University is very famous for its research and education in the area of computer science. This university has a computer center that has advanced and secure computing facilities including supercomputers and many personal computers connected to the Internet. One of the policies of the computer center is to let the students select their own login names. Unfortunately, students are apt to select similar login names, and troubles caused by mistakes in entering or specifying login names are relatively common. These troubles are a burden on the staff of the computer center. To avoid such troubles, Dr. Choei Takano, the chief manager of the computer center, decided to stamp out similar and confusing login names. To this end, Takano has to develop a program that detects confusing login names. Based on the following four operations on strings, the distance between two login names is determined as the minimum number of operations that transforms one login name to the other. Deleting a character at an arbitrary position. Inserting a character into an arbitrary position. Replacing a character at an arbitrary position with another character. Swapping two adjacent characters at an arbitrary position. For example, the distance between “ omura ” and “ murai ” is two, because the following sequence of operations transforms “ omura ” to “ murai ”. delete ‘o’ insert ‘i’ omura --> mura --> murai Another example is that the distance between “ akasan ” and “ kaason ” is also two. swap ‘a’ and ‘k’ replace ‘a’ with ‘o’ akasan --> kaasan --> kaason Takano decided that two login names with a small distance are confusing and thus must be avoided. Your job is to write a program that enumerates all the confusing pairs of login names. Beware that the rules may combine in subtle ways. For instance, the distance between “ ant ” and “ neat ” is two. swap ‘a’ and ‘n’ insert ‘e’ ant --> nat --> neat Input The input consists of multiple datasets. Each dataset is given in the following format. n d name 1 name 2 ... name n The first integer n is the number of login names. Then comes a positive integer d . Two login names whose distance is less than or equal to d are deemed to be confusing. You may assume that 0 < n ≤ 200 and 0 < d ≤ 2. The i -th student’s login name is given by name i , which is composed of only lowercase letters. Its length is less than 16. You can assume that there are no duplicates in name i (1 ≤ i ≤ n ). The end of the input is indicated by a line that solely contains a zero. Output For each dataset, your program should output all pairs of confusing login names, one pair per line, followed by the total number of confusing pairs in the dataset. In each pair, the two login names are to be separated only by a comma character (,), and the login name that is alphabetically preceding the other should appear first. ...(truncated)
[ { "submission_id": "aoj_1252_10850969", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\nusing VI = vector<int>;\nusing VVI = vector<VI>;\nusing PII = pair<int, int>;\nusing LL = long long;\nusing VL = vector<LL>;\nusing VVL = vector<VL>;\nusing PLL = pair<LL, LL>;\nusing VS = vector<string>;\n\n#define ALL(a) begin((a)),end((a))\n#define RALL(a) (a).rbegin(), (a).rend()\n#define PB push_back\n#define EB emplace_back\n#define MP make_pair\n#define SZ(a) int((a).size())\n#define SORT(c) sort(ALL((c)))\n#define RSORT(c) sort(RALL((c)))\n#define UNIQ(c) (c).erase(unique(ALL((c))), end((c)))\n\n#define FOR(i,a,b) for(int i=(a);i<(b);++i)\n#define REP(i,n) FOR(i,0,n)\n\n#define FF first\n#define SS second\ntemplate<class S, class T>\nistream& operator>>(istream& is, pair<S,T>& p){\n return is >> p.FF >> p.SS;\n}\ntemplate<class S, class T>\nostream& operator<<(ostream& os, const pair<S,T>& p){\n return os << p.FF << \" \" << p.SS;\n}\ntemplate<class T>\nvoid maxi(T& x, T y){\n if(x < y) x = y;\n}\ntemplate<class T>\nvoid mini(T& x, T y){\n if(x > y) x = y;\n}\n\n\nconst double EPS = 1e-10;\nconst double PI = acos(-1.0);\nconst LL MOD = 1e9+7;\n\nvoid change(const string& s, set<string>& dest){\n dest.insert(s);\n for(int i=0;i<SZ(s);++i){\n\tstring tmp = s;\n\ttmp.erase(i, 1);\n\tdest.insert(tmp);\n }\n for(int i=0;i<=SZ(s);++i){\n\tstring tmp = s;\n\ttmp.insert(i, \".\");\n\tREP(c,26){\n\t tmp[i] = c + 'a';\n\t dest.insert(tmp);\n\t}\n }\n \n for(int i=0;i<SZ(s);++i){\n\tstring tmp = s;\n\tREP(c,26){\n\t tmp[i] = c + 'a';\n\t dest.insert(tmp);\n\t}\n }\n for(int i=0;i+1<SZ(s);++i){\n\tstring tmp = s;\n\tswap(tmp[i], tmp[i+1]);\n\tdest.insert(tmp);\n }\n}\n\nint main(){\n cin.tie(0);\n ios_base::sync_with_stdio(false);\n\n int N, D;\n while(cin >> N, N){\n\tcin >> D;\n\tVS vs(N);\n\tREP(i,N){\n\t cin >> vs[i];\n\t}\n\n\tvector<set<string>> memo(N);\n\tmap<string,VI> ids;\n\tREP(i,N){\n\t change(vs[i], memo[i]);\n\t for(auto& s: memo[i])\n\t\tids[s].PB(i);\n\t}\n\n\tvector<pair<string,string>> ans;\n\tfor(auto& p: ids){\n\t if(SZ(p.SS) == 1) continue;\n\t if(D == 2){\n\t\tREP(ii,SZ(p.SS)) REP(jj,ii){\n\t\t int i = p.SS[ii], j = p.SS[jj];\n\t\t if(vs[i] < vs[j])\n\t\t\tans.EB(vs[i], vs[j]);\n\t\t else\n\t\t\tans.EB(vs[j], vs[i]);\n\t\t}\n\t }\n\t REP(ii,SZ(p.SS)) REP(jj,SZ(p.SS)){\n\t\tif(ii == jj) continue;\n\t\tint i = p.SS[ii], j = p.SS[jj];\n\t\tif(p.FF != vs[i] && p.FF != vs[j]) continue;\n\t\t\n\t\tif(vs[i] < vs[j])\n\t\t ans.EB(vs[i], vs[j]);\n\t\telse\n\t\t ans.EB(vs[j], vs[i]);\n\t }\n\t}\n\t/*\n\tREP(i,N) REP(j,N){\n\t if(i == j) continue;\n\t for(auto& s: memo[i]){\n\t\tif((D == 2 && memo[j].count(s)) || s == vs[j]){\n\t\t if(vs[i] < vs[j])\n\t\t\tans.EB(vs[i], vs[j]);\n\t\t else\n\t\t\tans.EB(vs[j], vs[i]);\n\t\t break;\n\t\t}\n\t }\n\t}\n\t*/\n\n\tSORT(ans);\n\tUNIQ(ans);\n\tfor(auto& p: ans)\n\t cout << p.FF << \",\" << p.SS << endl;\n\tcout << SZ(ans) << endl;\n }\n\n return 0;\n}", "accuracy": 1, "time_ms": 380, "memory_kb": 34432, "score_of_the_acc": -1.0531, "final_rank": 18 }, { "submission_id": "aoj_1252_10066711", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\n\nusing ll = long long;\n#define rep(i,n) for(int i=0;i<n;i++)\n#define rep2(i,s,t) for(int i=s;i<t;i++)\n\nvoid solve(int N){\n int d;\n cin>>d;\n map<string,vector<array<int,2>>> M;\n set<array<int,2>> S;\n vector<string> P(N);\n rep(i,N)cin>>P[i];\n sort(P.begin(),P.end());\n rep(i,N){\n for(auto v:M[P[i]])S.insert({v[0],i});\n M[P[i]].push_back({i,0});\n int PN=P[i].size();\n rep(j,PN+1){\n if(j<PN-1){\n int k=j+1;\n string Q=P[i];\n swap(Q[j],Q[k]);\n for(auto v:M[Q])if(v[1]+1<=d)S.insert({v[0],i});\n M[Q].push_back({i,1});\n }\n if(j<PN){\n string Q=P[i].substr(0,j);\n if(j!=PN-1)Q+=P[i].substr(j+1,PN-j-1);\n for(auto v:M[Q])if(v[1]+1<=d)S.insert({v[0],i});\n M[Q].push_back({i,1});\n }\n {\n rep(c,26){\n string Q=P[i].substr(0,j);\n Q.push_back(c+'a');\n Q+=P[i].substr(j,PN-j);\n for(auto v:M[Q])if(v[1]+1<=d)S.insert({v[0],i});\n M[Q].push_back({i,1});\n }\n }\n if(j<PN){\n rep(c,26){\n string Q=P[i];\n Q[j]='a'+c;\n for(auto v:M[Q])if(v[1]+1<=d)S.insert({v[0],i});\n M[Q].push_back({i,1});\n }\n }\n }\n }\n set<array<int,2>> AN;\n for(auto p:S)if(p[0]!=p[1])AN.insert(p);\n swap(S,AN);\n \n for(auto p:S){\n cout<<P[p[0]]<<\",\"<<P[p[1]]<<endl;\n }\n cout<<S.size()<<endl;\n}\nint main() {\n\n cin.tie(nullptr);\n ios::sync_with_stdio(false);\n\n int N;\n while(cin>>N,N!=0)solve(N);\n}", "accuracy": 1, "time_ms": 340, "memory_kb": 22456, "score_of_the_acc": -0.6623, "final_rank": 11 }, { "submission_id": "aoj_1252_10066691", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\n\nusing ll = long long;\n#define rep(i,n) for(int i=0;i<n;i++)\n#define rep2(i,s,t) for(int i=s;i<t;i++)\n\nusing vi = vector<int>;\nusing vvi=vector<vi>;\n\nvoid solve(int N){\n int d;\n cin>>d;\n map<string,vector<array<int,2>>> M;\n set<array<int,2>> S;\n vector<string> P(N);\n rep(i,N){\n cin>>P[i];\n }\n sort(P.begin(),P.end());\n rep(i,N){\n // cout<<i<<\" start\"<<endl;\n for(auto v:M[P[i]]){\n S.insert({v[0],i});\n }\n M[P[i]].push_back({i,0});\n int PN=P[i].size();\n // cout<<i<<\" step1\"<<endl;\n rep(j,PN-1){\n int k=j+1;\n string Q=P[i];\n swap(Q[j],Q[k]);\n for(auto v:M[Q]){\n if(d==2)S.insert({v[0],i});\n else if(v[1]==0)S.insert({v[0],i});\n }\n M[Q].push_back({i,1});\n }\n // cout<<i<<\" step2\"<<endl;\n rep(j,PN){\n string Q=P[i].substr(0,j);\n if(j!=PN-1)Q+=P[i].substr(j+1,PN-j-1);\n for(auto v:M[Q]){\n if(d==2)S.insert({v[0],i});\n else if(v[1]==0)S.insert({v[0],i});\n }\n M[Q].push_back({i,1});\n }\n // cout<<i<<\" step3\"<<endl;\n rep(j,PN+1){\n rep(c,26){\n string Q=P[i].substr(0,j);\n Q.push_back(c+'a');\n Q+=P[i].substr(j,PN-j);\n for(auto v:M[Q]){\n if(d==2)S.insert({v[0],i});\n else if(v[1]==0)S.insert({v[0],i});\n }\n M[Q].push_back({i,1});\n }\n }\n rep(j,PN){\n rep(c,26){\n string Q=P[i];\n Q[j]='a'+c;\n for(auto v:M[Q]){\n if(d==2)S.insert({v[0],i});\n else if(v[1]==0)S.insert({v[0],i});\n }\n M[Q].push_back({i,1});\n }\n }\n }\n set<array<int,2>> AN;\n for(auto p:S)if(p[0]!=p[1])AN.insert(p);\n swap(S,AN);\n \n for(auto p:S){\n cout<<P[p[0]]<<\",\"<<P[p[1]]<<endl;\n }\n cout<<S.size()<<endl;\n\n}\n\nint main() {\n\n cin.tie(nullptr);\n ios::sync_with_stdio(false);\n\n int N;\n while(cin>>N,N!=0)solve(N);\n\n}", "accuracy": 1, "time_ms": 340, "memory_kb": 22472, "score_of_the_acc": -0.6628, "final_rank": 12 }, { "submission_id": "aoj_1252_9763468", "code_snippet": "#include <bits/stdc++.h>\n\nusing namespace std;\n\n#define rep(i, a, b) for (ll i = (ll)(a); i < (ll)(b); i++)\n#define rrep(i, a, b) for (ll i = (ll)(b)-1; i >= (ll)(a); i--)\n#define ALL(v) (v).begin(), (v).end()\n#define UNIQUE(v) sort(ALL(v)), (v).erase(unique(ALL(v)), (v).end())\n#define SZ(v) (int)v.size()\n#define MIN(v) *min_element(ALL(v))\n#define MAX(v) *max_element(ALL(v))\n#define LB(v, x) int(lower_bound(ALL(v), (x)) - (v).begin())\n#define UB(v, x) int(upper_bound(ALL(v), (x)) - (v).begin())\n\nusing uint = unsigned int;\nusing ll = long long int;\nusing ull = unsigned long long;\nusing i128 = __int128_t;\nusing u128 = __uint128_t;\nconst int inf = 0x3fffffff;\nconst ll INF = 0x1fffffffffffffff;\n\ntemplate <typename T> inline bool chmax(T &a, T b) {\n if (a < b) {\n a = b;\n return 1;\n }\n return 0;\n}\ntemplate <typename T> inline bool chmin(T &a, T b) {\n if (a > b) {\n a = b;\n return 1;\n }\n return 0;\n}\ntemplate <typename T, typename U> T ceil(T x, U y) {\n assert(y != 0);\n if (y < 0)\n x = -x, y = -y;\n return (x > 0 ? (x + y - 1) / y : x / y);\n}\ntemplate <typename T, typename U> T floor(T x, U y) {\n assert(y != 0);\n if (y < 0)\n x = -x, y = -y;\n return (x > 0 ? x / y : (x - y + 1) / y);\n}\ntemplate <typename T> int popcnt(T x) {\n return __builtin_popcountll(x);\n}\ntemplate <typename T> int topbit(T x) {\n return (x == 0 ? -1 : 63 - __builtin_clzll(x));\n}\ntemplate <typename T> int lowbit(T x) {\n return (x == 0 ? -1 : __builtin_ctzll(x));\n}\n\ntemplate <class T, class U>\nostream &operator<<(ostream &os, const pair<T, U> &p) {\n os << \"P(\" << p.first << \", \" << p.second << \")\";\n return os;\n}\ntemplate <typename T> ostream &operator<<(ostream &os, const vector<T> &vec) {\n os << \"{\";\n for (int i = 0; i < vec.size(); i++) {\n os << vec[i] << (i + 1 == vec.size() ? \"\" : \", \");\n }\n os << \"}\";\n return os;\n}\ntemplate <typename T, typename U>\nostream &operator<<(ostream &os, const map<T, U> &map_var) {\n os << \"{\";\n for (auto itr = map_var.begin(); itr != map_var.end(); itr++) {\n os << \"(\" << itr->first << \", \" << itr->second << \")\";\n itr++;\n if (itr != map_var.end())\n os << \", \";\n itr--;\n }\n os << \"}\";\n return os;\n}\ntemplate <typename T> ostream &operator<<(ostream &os, const set<T> &set_var) {\n os << \"{\";\n for (auto itr = set_var.begin(); itr != set_var.end(); itr++) {\n os << *itr;\n ++itr;\n if (itr != set_var.end())\n os << \", \";\n itr--;\n }\n os << \"}\";\n return os;\n}\n#ifdef LOCAL\n#define show(...) _show(0, #__VA_ARGS__, __VA_ARGS__)\n#else\n#define show(...) true\n#endif\ntemplate <typename T> void _show(int i, T name) {\n cerr << '\\n';\n}\ntemplate <typename T1, typename T2, typename... T3>\nvoid _show(int i, const T1 &a, const T2 &b, const T3 &...c) {\n for (; a[i] != ',' && a[i] != '\\0'; i++)\n cerr << a[i];\n cerr << \":\" << b << \" \";\n _show(i + 1, a, c...);\n}\n\n/**\n * @brief template\n */\n\nset<string> makeset(string S) {\n set<string> st;\n rep(i,0,S.size()) {\n string U = S.substr(0,i) + S.substr(i+1,S.size()-i-1);\n st.insert(U);\n }\n rep(i,0,S.size()+1) {\n rep(j,0,26) {\n string U = S.substr(0,i);\n U = U + (char)('a'+j);\n U = U + S.substr(i,S.size()-i);\n st.insert(U);\n }\n }\n rep(i,0,S.size()) {\n char C = S[i];\n rep(j,0,26) {\n S[i] = 'a' + j;\n st.insert(S);\n S[i] = C;\n }\n }\n rep(i,0,S.size()-1) {\n swap(S[i],S[i+1]);\n st.insert(S);\n swap(S[i],S[i+1]);\n }\n return st;\n}\n\nint main() {\nwhile(1) {\n int N, D;\n cin >> N >> D;\n if (N == 0) return 0;\n vector<string> S(N);\n rep(i,0,N) cin >> S[i];\n sort(ALL(S));\n vector<set<string>> ST(N);\n rep(i,0,N) ST[i] = makeset(S[i]);\n auto Solve = [&](int i, int j) -> bool {\n if (ST[i].count(S[j])) return true;\n if (D == 1) return false;\n for (auto it = ST[j].begin(); it != ST[j].end(); it++) {\n if (ST[i].count(*it)) return true;\n }\n return false;\n };\n int ANS = 0;\n rep(i,0,N) {\n rep(j,i+1,N) {\n if (Solve(i,j)) {\n cout << S[i] << ',' << S[j] << endl;\n ANS++;\n }\n }\n }\n cout << ANS << endl;\n}\n}", "accuracy": 1, "time_ms": 3690, "memory_kb": 15540, "score_of_the_acc": -1.125, "final_rank": 19 }, { "submission_id": "aoj_1252_9222407", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\nstruct RollingHash {\n static constexpr int M = 2;\n static constexpr long long MODS[M] = {999999937, 1000000007};\n static constexpr long long BASE = 9973;\n vector<long long> powb[M], hash[M];\n int n;\n RollingHash() {}\n RollingHash(const string& str) { init(str); }\n void init(const string& str) {\n n = str.size();\n for (int k = 0; k < M; k++) {\n powb[k].resize(n + 1, 1);\n hash[k].resize(n + 1, 0);\n for (int i = 0; i < n; i++) {\n hash[k][i + 1] = (hash[k][i] * BASE + str[i]) % MODS[k];\n powb[k][i + 1] = powb[k][i] * BASE % MODS[k];\n }\n }\n }\n // get hash str[l,r)\n long long get(int l, int r, int k) {\n long long res = hash[k][r] - hash[k][l] * powb[k][r - l] % MODS[k];\n if (res < 0) res += MODS[k];\n return res;\n }\n};\n\nbool match(RollingHash& rh1, int l1, int r1, RollingHash& rh2, int l2, int r2) {\n bool res = true;\n for (int k = 0; k < RollingHash::M; k++) {\n res &= rh1.get(l1, r1, k) == rh2.get(l2, r2, k);\n }\n return res;\n}\n\nvoid solve(int n) {\n int D; cin >> D;\n vector<string> s(n);\n for(int i = 0; i < n; i++) cin >> s[i];\n sort(s.begin(), s.end());\n vector<pair<string, string>> ans;\n\n auto gen = [&](string s) {\n vector<string> res;\n const int len = s.size();\n // insert\n for(int i = 0; i <= len; i++) {\n string ss(len + 1, '?');\n for(int k = 0; k < i; k++) ss[k] = s[k];\n for(int k = i; k < len; k++) ss[k + 1] = s[k];\n for(char c = 'a'; c <= 'z'; c++) {\n ss[i] = c;\n res.push_back(ss);\n }\n }\n\n // erase\n for(int i = 0; i < len; i++) {\n string ss = \"\";\n for(int k = 0; k < len; k++) if(k != i) ss += s[k];\n res.push_back(ss);\n }\n\n // replace\n for(int i = 0; i < len; i++) {\n for(char c = 'a'; c <= 'z'; c++) {\n char p = s[i];\n s[i] = c;\n res.push_back(s);\n s[i] = p;\n }\n }\n\n // swap\n for(int i = 0; i + 1 < len; i++) {\n swap(s[i], s[i + 1]);\n res.push_back(s);\n swap(s[i], s[i + 1]);\n }\n\n return res;\n };\n vector<vector<string>> ss(n);\n for(int i = 0; i < n; i++) {\n ss[i] = gen(s[i]);\n sort(ss[i].begin(), ss[i].end());\n }\n \n for(int i = 0; i < n; i++) {\n for(int j = i + 1; j < n; j++) {\n bool ok = [&] {\n if(binary_search(ss[i].begin(), ss[i].end(), s[j])) return true;\n if(D == 2) {\n for(const string& si : ss[i]) {\n if(binary_search(ss[j].begin(), ss[j].end(), si)) return true;\n }\n }\n return false;\n }();\n if(ok) ans.push_back({s[i], s[j]});\n }\n }\n\n for(auto& [u, v] : ans) cout << u << \",\" << v << \"\\n\";\n cout << ans.size() << \"\\n\";\n return;\n}\n\nint main() {\n while(true) {\n int n; cin >> n;\n if(n == 0) return 0;\n solve(n);\n }\n}", "accuracy": 1, "time_ms": 3170, "memory_kb": 9584, "score_of_the_acc": -0.8286, "final_rank": 15 }, { "submission_id": "aoj_1252_8822114", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\nconst int inf = 1e9;\nconst int64_t inf64 = 1e18;\nconst double eps = 1e-9;\n\ntemplate <typename T> ostream &operator<<(ostream &os, const vector<T> &vec) {\n os << \"[\";\n for (const auto &v : vec) {\n os << v << \",\";\n }\n os << \"]\";\n return os;\n}\n\nint damerau_levenshtein_distance(const string &a, const string &b) {\n unordered_map<int, int> da;\n unordered_map<int, unordered_map<int, int>> d;\n int maxdist = a.size() + b.size();\n d[-1][-1] = maxdist;\n for (int i = 0; i <= a.size(); ++i) {\n d[i][-1] = maxdist;\n d[i][0] = i;\n }\n for (int i = 0; i <= b.size(); ++i) {\n d[-1][i] = maxdist;\n d[0][i] = i;\n }\n for (int i = 1; i <= a.size(); ++i) {\n int db = 0;\n for (int j = 1; j <= b.size(); ++j) {\n int k = da[b[j - 1]], l = db, cost;\n if (a[i - 1] == b[j - 1]) {\n cost = 0;\n db = j;\n } else\n cost = 1;\n d[i][j] = min({d[i - 1][j - 1] + cost, d[i][j - 1] + 1, d[i - 1][j] + 1,\n d[k - 1][l - 1] + (i - k - 1) + 1 + (j - l - 1)});\n }\n da[a[i - 1]] = i;\n }\n return d[a.size()][b.size()];\n}\n\nvoid solve(int n) {\n int d;\n vector<string> name;\n name.reserve(n);\n cin >> d;\n for(int i = 0; i < n; ++i) {\n string temp;\n cin >> temp;\n name.push_back(temp);\n }\n sort(name.begin(), name.end());\n int count = 0;\n for(int i = 0; i < n; ++i) {\n for(int j = i + 1; j < n; ++j) {\n if (damerau_levenshtein_distance(name[i], name[j]) > d)\n continue;\n ++count;\n cout << name[i] << \",\" << name[j] << endl;\n }\n }\n cout << count << endl;\n}\n\nint main() {\n std::cin.tie(0);\n std::ios::sync_with_stdio(false);\n cout.setf(ios::fixed);\n cout.precision(10);\n for (;;) {\n int n;\n cin >> n;\n if (!n)\n break;\n solve(n);\n }\n return 0;\n}", "accuracy": 1, "time_ms": 2880, "memory_kb": 3496, "score_of_the_acc": -0.5749, "final_rank": 10 }, { "submission_id": "aoj_1252_8822087", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n#define rep(i, x, y) for (int i = (x); i < (y); ++i)\n#define debug(x) #x << \"=\" << (x)\n#ifdef DEBUG\n#define _GLIBCXX_DEBUG\n#define print(x) std::cerr << debug(x) << \" (L:\" << __LINE__ << \")\" << std::endl\n#else\n#define print(x)\n#endif\nconst int inf = 1e9;\nconst int64_t inf64 = 1e18;\nconst double eps = 1e-9;\n\ntemplate <typename T> ostream &operator<<(ostream &os, const vector<T> &vec) {\n os << \"[\";\n for (const auto &v : vec) {\n os << v << \",\";\n }\n os << \"]\";\n return os;\n}\n\nint damerau_levenshtein_distance(const string &a, const string &b, const int alphabet_size) {\n vector<int> da(256, 0); \n vector<vector<int>> d(a.size() + 2, vector<int>(b.size() + 2));\n int maxdist = a.size() + b.size();\n d[0][0] = maxdist;\n for (int i = 0; i <= a.size(); ++i) {\n d[i+1][1] = i;\n d[i+1][0] = maxdist;\n }\n for (int i = 0; i <= b.size(); ++i) {\n d[1][i+1] = i;\n d[0][i+1] = maxdist;\n }\n for (int i = 1; i <= a.size(); ++i) {\n int db = 0;\n for (int j = 1; j <= b.size(); ++j) {\n int k = da[b[j - 1]], l = db, cost;\n if (a[i - 1] == b[j - 1]) {\n cost = 0;\n db = j;\n } else\n cost = 1;\n d[i+1][j+1] = min({d[i][j] + cost, d[i+1][j] + 1, d[i][j+1] + 1, d[k][l] + (i - k - 1) + 1 + (j - l - 1)});\n }\n da[a[i - 1]] = i;\n }\n return d[a.size()+1][b.size()+1];\n}\n\nvoid solve(int n) {\n int d;\n vector<string> name(n);\n cin >> d;\n rep(i, 0, n) cin >> name[i];\n sort(name.begin(), name.end());\n int count = 0;\n rep(i, 0, n) {\n rep(j, i + 1, n) {\n if (damerau_levenshtein_distance(name[i], name[j], 26) > d)\n continue;\n ++count;\n cout << name[i] << \",\" << name[j] << endl;\n }\n }\n cout << count << endl;\n}\n\nint main() {\n std::cin.tie(0);\n std::ios::sync_with_stdio(false);\n cout.setf(ios::fixed);\n cout.precision(10);\n for (;;) {\n int n;\n cin >> n;\n if (!n)\n break;\n solve(n);\n }\n return 0;\n}", "accuracy": 1, "time_ms": 140, "memory_kb": 3164, "score_of_the_acc": -0.0051, "final_rank": 1 }, { "submission_id": "aoj_1252_8215617", "code_snippet": "#include <iostream>\n#include <vector>\n#include <string>\n#include <algorithm>\n#define MIN(a,b,c,d) min(min(a,b),min(c,d))\n\nusing namespace std;\n\nint damerau_levenshtein_distance(const string &a, const string &b) {\n vector<int> da(256);\n int sz_a = a.size(), sz_b = b.size();\n vector<vector<int> > d(sz_a+2, vector<int>(sz_b+2));\n int INF = sz_a + sz_b, i, j, db;\n d[0][0] = INF;\n for(i = 0; i <= sz_a; ++i) {\n d[i+1][1] = i;\n d[i+1][0] = INF;\n }\n for(j = 0; j <= sz_b; ++j) {\n d[1][j+1] = j;\n d[0][j+1] = INF;\n }\n for(i = 1; i <= sz_a; ++i) {\n db = 0;\n for(j = 1; j <= sz_b; ++j) {\n int i1 = da[b[j-1]], j1 = db, cost;\n if (a[i-1] == b[j-1]) {\n cost = 0;\n db = j;\n } else\n cost = 1;\n d[i+1][j+1] = MIN(d[i][j] + cost, d[i+1][j] + 1, d[i][j+1] + 1, d[i1][j1] + (i - i1 - 1) + 1 + (j - j1 - 1));\n }\n da[a[i-1]] = i;\n }\n return d[sz_a+1][sz_b+1];\n}\n\nvoid solve(int n) {\n int d;\n vector<string> name(n);\n cin >> d;\n for(int i = 0; i < n; ++i) cin >> name[i];\n sort(name.begin(), name.end());\n int count = 0;\n for(int i = 0; i < n; ++i) {\n for(int j = i + 1; j < n; ++j) {\n if (damerau_levenshtein_distance(name[i], name[j]) > d)\n continue;\n ++count;\n cout << name[i] << \",\" << name[j] << endl;\n }\n }\n cout << count << endl;\n}\n\nint main() {\n ios::sync_with_stdio(false);\n cin.tie(0);\n cout.setf(ios::fixed);\n cout.precision(10);\n for (;;) {\n int n;\n cin >> n;\n if (!n)\n break;\n solve(n);\n }\n return 0;\n}", "accuracy": 1, "time_ms": 120, "memory_kb": 3384, "score_of_the_acc": -0.0081, "final_rank": 3 }, { "submission_id": "aoj_1252_8215613", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\nconst int MAX_SIZE = 1010;\n\nint dp[MAX_SIZE][MAX_SIZE], last_occurrence[26];\nstring s, t;\n\nint damerau_levenshtein_distance() {\n int m = s.size(), n = t.size();\n for (int i = 0; i <= m; i++)\n for (int j = 0; j <= n; j++)\n dp[i][j] = (i == 0 ? j : (j == 0 ? i : 1e9));\n \n memset(last_occurrence, -1, sizeof(last_occurrence));\n\n for (int i = 1; i <= m; i++) {\n int prev = -1;\n for (int j = 1; j <= n; j++) {\n int k = last_occurrence[t[j - 1] - 'a'];\n int l = prev;\n if (s[i - 1] == t[j - 1]) {\n dp[i][j] = dp[i - 1][j - 1];\n prev = j;\n } else\n dp[i][j] = min({dp[i][j], dp[i - 1][j - 1] + 1, dp[i][j - 1] + 1, dp[i - 1][j] + 1});\n if (k >= 0 && l >= 0)\n dp[i][j] = min(dp[i][j], dp[k - 1][l - 1] + (i - k - 1) + (j - l - 1) + 1);\n }\n last_occurrence[s[i - 1] - 'a'] = i;\n }\n\n return dp[m][n];\n}\n\nint main() {\n ios::sync_with_stdio(false);\n cin.tie(0);\n\n int n, d;\n while (cin >> n && n != 0) {\n cin >> d;\n vector<string> name(n);\n for (string &str : name) cin >> str;\n sort(name.begin(), name.end());\n\n int count = 0;\n for (int i = 0; i < n; i++) {\n for (int j = i + 1; j < n; j++) {\n s = name[i], t = name[j];\n if (damerau_levenshtein_distance() <= d) {\n cout << name[i] << \",\" << name[j] << \"\\n\";\n count++;\n }\n }\n }\n cout << count << \"\\n\";\n }\n\n return 0;\n}", "accuracy": 1, "time_ms": 120, "memory_kb": 3368, "score_of_the_acc": -0.0075, "final_rank": 2 }, { "submission_id": "aoj_1252_8215606", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n#define rep(i, x, y) for (int i = (x); i < (y); ++i)\n#define debug(x) #x << \"=\" << (x)\n#ifdef DEBUG\n#define _GLIBCXX_DEBUG\n#define print(x) std::cerr << debug(x) << \" (L:\" << __LINE__ << \")\" << std::endl\n#else\n#define print(x)\n#endif\nconst int inf = 1e9;\nconst int64_t inf64 = 1e18;\nconst double eps = 1e-9;\ntemplate <typename T> ostream &operator<<(ostream &os, const vector<T> &vec) {\n os << \"[\";\n for (const auto &v : vec) {\n os << v << \",\";\n }\n os << \"]\";\n return os;\n}\nint damerau_levenshtein_distance(const string &a, const string &b,\n const int alphabet_size) {\n unordered_map<int, int> da;\n map<int, unordered_map<int, int>> d;\n int maxdist = a.size() + b.size();\n d[-1][-1] = maxdist;\n for (int i = 0; i <= a.size(); ++i) {\n d[i][-1] = maxdist;\n d[i][0] = i;\n }\n for (int i = 0; i <= b.size(); ++i) {\n d[-1][i] = maxdist;\n d[0][i] = i;\n }\n for (int i = 1; i <= a.size(); ++i) {\n int db = 0;\n for (int j = 1; j <= b.size(); ++j) {\n int k = da[b[j - 1]], l = db, cost;\n if (a[i - 1] == b[j - 1]) {\n cost = 0;\n db = j;\n } else\n cost = 1;\n d[i][j] = min({d[i - 1][j - 1] + cost, d[i][j - 1] + 1, d[i - 1][j] + 1,\n d[k - 1][l - 1] + (i - k - 1) + 1 + (j - l - 1)});\n }\n da[a[i - 1]] = i;\n }\n return d[a.size()][b.size()];\n}\nvoid solve(int n) {\n int d;\n vector<string> name(n);\n cin >> d;\n rep(i, 0, n) cin >> name[i];\n sort(name.begin(), name.end());\n int count = 0;\n rep(i, 0, n) {\n rep(j, i + 1, n) {\n if (damerau_levenshtein_distance(name[i], name[j], 26) > d)\n continue;\n ++count;\n cout << name[i] << \",\" << name[j] << endl;\n }\n }\n cout << count << endl;\n}\nint main() {\n std::cin.tie(0);\n std::ios::sync_with_stdio(false);\n cout.setf(ios::fixed);\n cout.precision(10);\n for (;;) {\n int n;\n cin >> n;\n if (!n)\n break;\n solve(n);\n }\n return 0;\n}", "accuracy": 1, "time_ms": 2530, "memory_kb": 3192, "score_of_the_acc": -0.4938, "final_rank": 7 }, { "submission_id": "aoj_1252_8215605", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n#define rep(i, x, y) for (int i = (x); i < (y); ++i)\n#define debug(x) #x << \"=\" << (x)\n#ifdef DEBUG\n#define _GLIBCXX_DEBUG\n#define print(x) std::cerr << debug(x) << \" (L:\" << __LINE__ << \")\" << std::endl\n#else\n#define print(x)\n#endif\nconst int inf = 1e9;\nconst int64_t inf64 = 1e18;\nconst double eps = 1e-9;\ntemplate <typename T> ostream &operator<<(ostream &os, const vector<T> &vec) {\n os << \"[\";\n for (const auto &v : vec) {\n os << v << \",\";\n }\n os << \"]\";\n return os;\n}\nint damerau_levenshtein_distance(const string &a, const string &b,\n const int alphabet_size) {\n unordered_map<int, int> da;\n unordered_map<int, unordered_map<int, int>> d;\n int maxdist = a.size() + b.size();\n d[-1][-1] = maxdist;\n for (int i = 0; i <= a.size(); ++i) {\n d[i][-1] = maxdist;\n d[i][0] = i;\n }\n for (int i = 0; i <= b.size(); ++i) {\n d[-1][i] = maxdist;\n d[0][i] = i;\n }\n for (int i = 1; i <= a.size(); ++i) {\n int db = 0;\n for (int j = 1; j <= b.size(); ++j) {\n int k = da[b[j - 1]], l = db, cost;\n if (a[i - 1] == b[j - 1]) {\n cost = 0;\n db = j;\n } else\n cost = 1;\n d[i][j] = min({d[i - 1][j - 1] + cost, d[i][j - 1] + 1, d[i - 1][j] + 1,\n d[k - 1][l - 1] + (i - k - 1) + 1 + (j - l - 1)});\n }\n da[a[i - 1]] = i;\n }\n return d[a.size()][b.size()];\n}\nvoid solve(int n) {\n int d;\n vector<string> name(n);\n cin >> d;\n rep(i, 0, n) cin >> name[i];\n sort(name.begin(), name.end());\n int count = 0;\n rep(i, 0, n) {\n rep(j, i + 1, n) {\n if (damerau_levenshtein_distance(name[i], name[j], 26) > d)\n continue;\n ++count;\n cout << name[i] << \",\" << name[j] << endl;\n }\n }\n cout << count << endl;\n}\nint main() {\n std::cin.tie(0);\n std::ios::sync_with_stdio(false);\n cout.setf(ios::fixed);\n cout.precision(10);\n for (;;) {\n int n;\n cin >> n;\n if (!n)\n break;\n solve(n);\n }\n return 0;\n}", "accuracy": 1, "time_ms": 2900, "memory_kb": 3356, "score_of_the_acc": -0.5745, "final_rank": 9 }, { "submission_id": "aoj_1252_8112995", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n#define rep(i, x, y) for (int i = (x); i < (y); ++i)\n#define debug(x) #x << \"=\" << (x)\n#ifdef DEBUG\n#define _GLIBCXX_DEBUG\n#define print(x) std::cerr << debug(x) << \" (L:\" << __LINE__ << \")\" << std::endl\n#else\n#define print(x)\n#endif\nconst int inf = 1e9;\nconst int64_t inf64 = 1e18;\nconst double eps = 1e-9;\ntemplate <typename T> ostream &operator<<(ostream &os, const vector<T> &vec) {\n os << \"[\";\n for (const auto &v : vec) {\n os << v << \",\";\n }\n os << \"]\";\n return os;\n}\nint damerau_levenshtein_distance(const string &a, const string &b,\n const int alphabet_size) {\n map<int, int> da;\n map<int, map<int, int>> d;\n int maxdist = a.size() + b.size();\n d[-1][-1] = maxdist;\n for (int i = 0; i <= a.size(); ++i) {\n d[i][-1] = maxdist;\n d[i][0] = i;\n }\n for (int i = 0; i <= b.size(); ++i) {\n d[-1][i] = maxdist;\n d[0][i] = i;\n }\n for (int i = 1; i <= a.size(); ++i) {\n int db = 0;\n for (int j = 1; j <= b.size(); ++j) {\n int k = da[b[j - 1]], l = db, cost;\n if (a[i - 1] == b[j - 1]) {\n cost = 0;\n db = j;\n } else\n cost = 1;\n d[i][j] = min({d[i - 1][j - 1] + cost, d[i][j - 1] + 1, d[i - 1][j] + 1,\n d[k - 1][l - 1] + (i - k - 1) + 1 + (j - l - 1)});\n }\n da[a[i - 1]] = i;\n }\n return d[a.size()][b.size()];\n}\nvoid solve(int n) {\n int d;\n vector<string> name(n);\n cin >> d;\n rep(i, 0, n) cin >> name[i];\n sort(name.begin(), name.end());\n int count = 0;\n rep(i, 0, n) {\n rep(j, i + 1, n) {\n if (damerau_levenshtein_distance(name[i], name[j], 26) > d)\n continue;\n ++count;\n cout << name[i] << \",\" << name[j] << endl;\n }\n }\n cout << count << endl;\n}\nint main() {\n std::cin.tie(0);\n std::ios::sync_with_stdio(false);\n cout.setf(ios::fixed);\n cout.precision(10);\n for (;;) {\n int n;\n cin >> n;\n if (!n)\n break;\n solve(n);\n }\n return 0;\n}", "accuracy": 1, "time_ms": 2690, "memory_kb": 3176, "score_of_the_acc": -0.5259, "final_rank": 8 }, { "submission_id": "aoj_1252_7941057", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nbool check(string a, string b, int d) {\n if (a.size() < b.size())\n swap(a, b);\n if (a == b)\n return 1;\n if (d == 0 || a.empty())\n return 0;\n for (int i = 0; i < a.size(); i++) {\n string t = a;\n t.erase(t.begin() + i);\n if (check(t, b, d - 1))\n return 1;\n }\n if (a.size() != b.size())\n return 0;\n for (int i = 0; i < (int)a.size() - 1; i++) {\n swap(a[i], a[i + 1]);\n if (check(a, b, d - 1))\n return 1;\n swap(a[i], a[i + 1]);\n }\n for (int i = 0; i < b.size(); i++) {\n char ch = a[i];\n if (a[i] != b[i])\n a[i] = b[i];\n if (check(a, b, d - 1))\n return 1;\n a[i] = ch;\n }\n return 0;\n}\nint main() {\n int n, d;\n while (cin >> n, n) {\n cin >> d;\n string str[201];\n for (int i = 0; i < n; i++)\n cin >> str[i];\n sort(str, str + n);\n int cnt = 0;\n for (int i = 0; i < n; i++)\n for (int j = i + 1; j < n; j++)\n if (check(str[i], str[j], d))\n cout << str[i] << \",\" << str[j] << endl, cnt++;\n cout << cnt << endl;\n }\n return 0;\n}", "accuracy": 1, "time_ms": 3480, "memory_kb": 3184, "score_of_the_acc": -0.6874, "final_rank": 13 }, { "submission_id": "aoj_1252_7941054", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nbool check(string a, string b, int d) {\n if (a.size() < b.size())\n swap(a, b);\n if (a == b)\n return 1;\n if (d == 0)\n return 0;\n for (int i = 0; i < a.size(); i++) {\n string t = a;\n t.erase(t.begin() + i);\n if (check(t, b, d - 1))\n return 1;\n }\n if (a.size() != b.size())\n return 0;\n for (int i = 0; i < (int)a.size() - 1; i++) {\n if (a[i] == a[i + 1] || a[i + 1] != b[i])\n continue;\n swap(a[i], a[i + 1]);\n if (check(a, b, d - 1))\n return 1;\n swap(a[i], a[i + 1]);\n }\n int cnt = 0;\n for (int i = 0; i < a.size(); i++)\n cnt += a[i] != b[i];\n return cnt <= d;\n}\nint main() {\n int n, d;\n while (cin >> n, n) {\n cin >> d;\n string str[201];\n for (int i = 0; i < n; i++)\n cin >> str[i];\n sort(str, str + n);\n int cnt = 0;\n for (int i = 0; i < n; i++)\n for (int j = i + 1; j < n; j++)\n if (check(str[i], str[j], d))\n cout << str[i] << \",\" << str[j] << endl, cnt++;\n cout << cnt << endl;\n }\n return 0;\n}", "accuracy": 1, "time_ms": 1790, "memory_kb": 3200, "score_of_the_acc": -0.343, "final_rank": 6 }, { "submission_id": "aoj_1252_7941050", "code_snippet": "#include <bits/stdc++.h>\n#define r(i, n) for (int i = 0; i < n; i++)\nusing namespace std;\ntypedef pair<int, int> P;\nint n, d;\nstring s[222];\nset<string> st[222];\nbool used[222][222];\nset<string>::iterator it, it2;\nsigned main() {\n cin.tie(0);\n ios::sync_with_stdio(false);\n while (cin >> n, n) {\n memset(used, 0, sizeof(used));\n cin >> d;\n r(i, n) cin >> s[i];\n sort(s, s + n);\n r(i, n) st[i].clear();\n r(i, n) {\n r(j, s[i].size()) {\n string t = s[i];\n t.erase(t.begin() + j);\n st[i].insert(t);\n }\n r(j, s[i].size() + 1) {\n r(k, 26) {\n string t = s[i];\n t = t.substr(0, j) + (char)(k + 'a') + t.substr(j);\n st[i].insert(t);\n }\n }\n r(j, s[i].size() + 1) {\n r(k, 26) {\n string t = s[i];\n t[j] = (char)(k + 'a');\n st[i].insert(t);\n }\n }\n r(j, s[i].size() - 1) {\n string t = s[i];\n swap(t[j], t[j + 1]);\n st[i].insert(t);\n }\n }\n int ans = 0;\n if (d == 1) {\n r(i, n) for (int j = i + 1; j < n; j++) if (i != j && !used[i][j]) {\n {\n for (it = st[i].begin(); it != st[i].end(); it++) {\n if (s[j] == (*it)) {\n used[i][j] = used[j][i] = 1;\n cout << s[i] << ',' << s[j] << endl;\n ans++;\n break;\n }\n }\n }\n }\n } else {\n r(i, n) for (int j = i + 1; j < n; j++) if (i != j && !used[i][j]) {\n for (it = st[i].begin(); it != st[i].end(); it++) {\n if (st[j].count(*it)) {\n used[i][j] = used[j][i] = 1;\n cout << s[i] << ',' << s[j] << endl;\n ans++;\n break;\n }\n }\n }\n }\n cout << ans << endl;\n }\n}", "accuracy": 1, "time_ms": 5020, "memory_kb": 17596, "score_of_the_acc": -1.4621, "final_rank": 20 }, { "submission_id": "aoj_1252_7941044", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\ntypedef unsigned long long ull;\null B = 10000007;\nint n, d, len;\nchar s[200][17];\null t[200];\nint G[200][200];\nbool f[200][200];\nmap<ull, bool> mp[200];\nstruct data {\n int fi, se;\n bool operator<(const data &d) const {\n return (G[fi][d.fi] != 0 ? G[fi][d.fi] < 0 : G[se][d.se] < 0);\n }\n};\nvector<data> v;\null Deleting(int x, char *ch) {\n ull res = 0;\n for (int i = 0; i < len; i++)\n if (i != x)\n res = res * B + ch[i];\n return res;\n}\null Inserting(int x, char y, char *ch) {\n ull res = 0;\n for (int i = 0; i <= len; i++) {\n if (i == x)\n res = res * B + y;\n if (i < len)\n res = res * B + ch[i];\n }\n return res;\n}\null Replacing(int x, char y, char *ch) {\n ull res = 0;\n for (int i = 0; i < len; i++) {\n if (i == x)\n res = res * B + y;\n else\n res = res * B + ch[i];\n }\n return res;\n}\null Swapping(int x, char *ch) {\n ull res = 0;\n for (int i = 0; i < len; i++) {\n if (i == x)\n res = res * B + ch[i + 1];\n else if (i == x + 1)\n res = res * B + ch[x];\n else\n res = res * B + ch[i];\n }\n return res;\n}\nvoid init() {\n for (int i = 0; i < 200; i++) {\n mp[i].clear();\n }\n v.clear();\n memset(f, false, sizeof(f));\n}\nint main() {\n while (1) {\n scanf(\"%d\", &n);\n if (n == 0)\n break;\n init();\n scanf(\"%d\", &d);\n for (int i = 0; i < n; i++) {\n scanf(\"%s\", s[i]);\n len = strlen(s[i]);\n t[i] = Swapping(-10, s[i]);\n for (int j = 0; j < len; j++) {\n mp[i][Deleting(j, s[i])] = true;\n }\n for (int j = 0; j <= len; j++) {\n for (char k = 'a'; k <= 'z'; k++) {\n mp[i][Inserting(j, k, s[i])] = true;\n }\n }\n for (int j = 0; j < len; j++) {\n for (char k = 'a'; k <= 'z'; k++) {\n if (s[i][j] != k) {\n mp[i][Replacing(j, k, s[i])] = true;\n }\n }\n }\n for (int j = 0; j + 1 < len; j++) {\n mp[i][Swapping(j, s[i])] = true;\n }\n }\n for (int i = 0; i < n; i++)\n for (int j = 0; j < n; j++)\n G[i][j] = strcmp(s[i], s[j]);\n for (int i = 1; i < n; i++) {\n for (int j = 0; j < i; j++) {\n int a = i, b = j;\n if (G[a][b] > 0)\n swap(a, b);\n if (mp[a].count(t[b]) && !f[a][b]) {\n v.push_back((data){a, b});\n f[a][b] = true;\n }\n if (d == 1)\n continue;\n map<ull, bool>::iterator it;\n for (it = mp[b].begin(); it != mp[b].end(); it++) {\n if (mp[a].count(it->first) && !f[a][b]) {\n v.push_back((data){a, b});\n f[a][b] = true;\n }\n }\n }\n }\n sort(v.begin(), v.end());\n for (int i = 0; i < (int)v.size(); i++) {\n printf(\"%s,%s\\n\", s[v[i].fi], s[v[i].se]);\n }\n printf(\"%d\\n\", (int)v.size());\n }\n return 0;\n}", "accuracy": 1, "time_ms": 3370, "memory_kb": 12632, "score_of_the_acc": -0.9668, "final_rank": 17 }, { "submission_id": "aoj_1252_7941042", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\ntypedef unsigned long long ull;\null B = 10000007;\nint n, d, len;\nchar s[200][17];\null t[200];\nint G[200][200];\nbool f[200][200];\nmap<ull, bool> mp[200];\nvector<ull> L[200];\nstruct data {\n int fi, se;\n bool operator<(const data &d) const {\n return (G[fi][d.fi] != 0 ? G[fi][d.fi] < 0 : G[se][d.se] < 0);\n }\n};\nvector<data> v;\null Deleting(int x, char *ch) {\n ull res = 0;\n for (int i = 0; i < len; i++)\n if (i != x)\n res = res * B + ch[i];\n return res;\n}\null Inserting(int x, char y, char *ch) {\n ull res = 0;\n for (int i = 0; i <= len; i++) {\n if (i == x)\n res = res * B + y;\n if (i < len)\n res = res * B + ch[i];\n }\n return res;\n}\null Replacing(int x, char y, char *ch) {\n ull res = 0;\n for (int i = 0; i < len; i++) {\n if (i == x)\n res = res * B + y;\n else\n res = res * B + ch[i];\n }\n return res;\n}\null Swapping(int x, char *ch) {\n ull res = 0;\n for (int i = 0; i < len; i++) {\n if (i == x)\n res = res * B + ch[i + 1];\n else if (i == x + 1)\n res = res * B + ch[x];\n else\n res = res * B + ch[i];\n }\n return res;\n}\nvoid init() {\n for (int i = 0; i < 200; i++) {\n mp[i].clear();\n L[i].clear();\n }\n v.clear();\n memset(f, false, sizeof(f));\n}\nint main() {\n while (1) {\n scanf(\"%d\", &n);\n if (n == 0)\n break;\n init();\n scanf(\"%d\", &d);\n for (int i = 0; i < n; i++) {\n scanf(\"%s\", s[i]);\n len = strlen(s[i]);\n t[i] = Swapping(-10, s[i]);\n ull key;\n for (int j = 0; j < len; j++) {\n key = Deleting(j, s[i]);\n mp[i][key] = true;\n L[i].push_back(key);\n }\n for (int j = 0; j <= len; j++) {\n for (char k = 'a'; k <= 'z'; k++) {\n key = Inserting(j, k, s[i]);\n mp[i][key] = true;\n L[i].push_back(key);\n }\n }\n for (int j = 0; j < len; j++) {\n for (char k = 'a'; k <= 'z'; k++) {\n if (s[i][j] != k) {\n key = Replacing(j, k, s[i]);\n mp[i][key] = true;\n L[i].push_back(key);\n }\n }\n }\n for (int j = 0; j + 1 < len; j++) {\n key = Swapping(j, s[i]);\n mp[i][key] = true;\n L[i].push_back(key);\n }\n }\n for (int i = 0; i < n; i++)\n for (int j = 0; j < n; j++)\n G[i][j] = strcmp(s[i], s[j]);\n for (int i = 1; i < n; i++) {\n for (int j = 0; j < i; j++) {\n int a = i, b = j;\n if (G[a][b] > 0)\n swap(a, b);\n if (mp[a].count(t[b]) && !f[a][b]) {\n v.push_back((data){a, b});\n f[a][b] = true;\n }\n if (d == 1)\n continue;\n vector<ull>::iterator it;\n for (it = L[b].begin(); it != L[b].end(); it++) {\n if (mp[a].count(*it) && !f[a][b]) {\n v.push_back((data){a, b});\n f[a][b] = true;\n }\n }\n }\n }\n sort(v.begin(), v.end());\n for (int i = 0; i < (int)v.size(); i++) {\n printf(\"%s,%s\\n\", s[v[i].fi], s[v[i].se]);\n }\n printf(\"%d\\n\", (int)v.size());\n }\n return 0;\n}", "accuracy": 1, "time_ms": 2560, "memory_kb": 14216, "score_of_the_acc": -0.8521, "final_rank": 16 }, { "submission_id": "aoj_1252_7941035", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nbool check(string a, string b, int d) {\n if (a.size() < b.size())\n swap(a, b);\n if (a == b)\n return 1;\n int A = a.size(), B = b.size();\n if (d == 0 || A - B > d)\n return 0;\n for (int i = 0; i < A; i++) {\n char ch = a[i];\n a.erase(a.begin() + i);\n if (check(a, b, d - 1))\n return 1;\n a.insert(a.begin() + i, ch);\n }\n if (A != B)\n return 0;\n for (int i = 0; i < A - 1; i++) {\n if (a[i] == a[i + 1] || a[i + 1] != b[i] || a[i] != b[i + 1])\n continue;\n swap(a[i], a[i + 1]);\n if (check(a, b, d - 1))\n return 1;\n swap(a[i], a[i + 1]);\n }\n int cnt = 0;\n for (int i = 0; i < A; i++)\n cnt += a[i] != b[i];\n return cnt <= d;\n}\nint main() {\n int n, d;\n while (cin >> n, n) {\n cin >> d;\n string str[201];\n for (int i = 0; i < n; i++)\n cin >> str[i];\n sort(str, str + n);\n int cnt = 0;\n for (int i = 0; i < n; i++)\n for (int j = i + 1; j < n; j++)\n if (check(str[i], str[j], d))\n cout << str[i] << \",\" << str[j] << endl, cnt++;\n cout << cnt << endl;\n }\n return 0;\n}", "accuracy": 1, "time_ms": 880, "memory_kb": 3132, "score_of_the_acc": -0.1551, "final_rank": 4 }, { "submission_id": "aoj_1252_7941031", "code_snippet": "#include <algorithm>\n#include <iostream>\n#include <set>\n#include <string>\n#include <vector>\nusing namespace std;\n#define REP(i, b, n) for (int i = b; i < n; i++)\n#define rep(i, n) REP(i, 0, n)\n#define ALL(C) (C).begin(), (C).end()\n#define FOR(it, o) \\\n for (__typeof((o).begin()) it = (o).begin(); it != (o).end(); it++)\n#define pb push_back\n#define mp make_pair\ntypedef unsigned long long ull;\ntypedef pair<string, string> pss;\ntypedef vector<pss> vpss;\nconst ull tmp = 10007;\null calchash(string &in) {\n ull ret = 0;\n rep(i, in.size()) { ret = ret * tmp + in[i]; }\n return ret;\n}\nbool isok[200][200];\nvoid ed1(vector<string> &in, set<ull> *S) {\n rep(i, in.size()) {\n REP(j, i + 1, in.size()) {\n if (S[j].find(calchash(in[i])) != S[j].end()) {\n isok[i][j] = true;\n }\n }\n }\n}\nvoid ed2(vector<string> &in, set<ull> *S) {\n rep(i, in.size()) {\n REP(j, i + 1, in.size()) {\n if (isok[i][j])\n continue;\n FOR(it, S[i]) {\n if (S[j].find(*it) != S[j].end()) {\n isok[i][j] = true;\n break;\n }\n }\n }\n }\n}\nvoid del_string(string &in, set<ull> &S) {\n rep(i, in.size()) {\n string tmp = in;\n tmp.erase(tmp.begin() + i);\n S.insert(calchash(tmp));\n }\n}\nvoid insert_string(string &in, set<ull> &S) {\n rep(i, in.size() + 1) {\n rep(j, 26) {\n string tmp = in.substr(0, i);\n tmp += string(1, 'a' + j);\n tmp += in.substr(i);\n S.insert(calchash(tmp));\n }\n }\n}\nvoid replace_string(string &in, set<ull> &S) {\n rep(i, in.size()) {\n char tmp = in[i];\n rep(j, 26) {\n in[i] = 'a' + j;\n S.insert(calchash(in));\n }\n in[i] = tmp;\n }\n}\nvoid swap_string(string &in, set<ull> &S) {\n rep(i, (int)in.size() - 1) {\n swap(in[i], in[i + 1]);\n S.insert(calchash(in));\n swap(in[i], in[i + 1]);\n }\n}\nvoid make_string(vector<string> &in, set<ull> *S) {\n rep(i, in.size()) {\n del_string(in[i], S[i]);\n insert_string(in[i], S[i]);\n replace_string(in[i], S[i]);\n swap_string(in[i], S[i]);\n }\n}\nvoid solve(vector<string> in, set<ull> *S, int d) {\n int n = in.size();\n make_string(in, S);\n ed1(in, S);\n if (d == 2)\n ed2(in, S);\n int cnt = 0;\n rep(i, n) {\n rep(j, n) if (isok[i][j]) cnt++, cout << in[i] << \",\" << in[j] << endl;\n }\n cout << cnt << endl;\n}\nvoid test() {\n string in = \"test\";\n set<ull> S;\n insert_string(in, S);\n}\nmain() {\n int n, d;\n while (cin >> n >> d && n) {\n vector<string> in;\n rep(i, n) rep(j, n) isok[i][j] = false;\n set<ull> S[n];\n rep(i, n) {\n string tmp;\n cin >> tmp;\n in.pb(tmp);\n }\n sort(ALL(in));\n rep(i, in.size()) S[i].insert(calchash(in[i]));\n solve(in, S, d);\n }\n}", "accuracy": 1, "time_ms": 2610, "memory_kb": 10044, "score_of_the_acc": -0.729, "final_rank": 14 }, { "submission_id": "aoj_1252_7941029", "code_snippet": "#include <algorithm>\n#include <iostream>\n#include <set>\n#include <string>\n#include <vector>\nusing namespace std;\n#define REP(i, b, n) for (int i = b; i < n; i++)\n#define rep(i, n) REP(i, 0, n)\n#define ALL(C) (C).begin(), (C).end()\n#define FOR(it, o) \\\n for (__typeof((o).begin()) it = (o).begin(); it != (o).end(); it++)\n#define pb push_back\n#define mp make_pair\ntypedef unsigned long long ull;\ntypedef pair<string, string> pss;\ntypedef vector<pss> vpss;\nconst ull tmp = 10007;\null calchash(string &in) {\n ull ret = 0;\n rep(i, (int)in.size()) { ret = ret * tmp + in[i]; }\n return ret;\n}\nbool isok[200][200];\nint ed[200][200];\nint cost[20][20];\nint edit_distance(string tr, string tc) {\n string r, c;\n r = ' ';\n c = ' ';\n rep(i, tr.size()) r += tr[i];\n rep(i, tc.size()) c += tc[i];\n rep(i, r.size()) rep(j, c.size()) cost[i][j] = (1 << 21);\n rep(i, r.size()) cost[i][0] = i;\n rep(i, c.size()) cost[0][i] = i;\n REP(i, 1, r.size()) {\n REP(j, 1, c.size()) {\n int tmp = r[i] == c[j] ? 0 : 1;\n int opt[3];\n opt[0] = cost[i - 1][j - 1] + tmp;\n opt[1] = cost[i - 1][j] + 1;\n opt[2] = cost[i][j - 1] + 1;\n rep(k, 3) cost[i][j] = min(cost[i][j], opt[k]);\n }\n }\n return cost[r.size() - 1][c.size() - 1];\n}\nvoid ed1(vector<string> &in, set<ull> S[]) {\n rep(i, (int)in.size()) {\n REP(j, i + 1, (int)in.size()) {\n if (S[j].find(calchash(in[i])) != S[j].end()) {\n isok[i][j] = true;\n }\n }\n }\n}\nvoid ed2(vector<string> &in, set<ull> S[]) {\n rep(i, (int)in.size()) {\n REP(j, i + 1, (int)in.size()) {\n if (isok[i][j] || ed[i][j] > 4)\n continue;\n FOR(it, S[i]) {\n if (S[j].find(*it) != S[j].end()) {\n isok[i][j] = true;\n break;\n }\n }\n }\n }\n}\nvoid del_string(string &in, set<ull> &S) {\n rep(i, (int)in.size()) {\n string tmp = in;\n tmp.erase(tmp.begin() + i);\n S.insert(calchash(tmp));\n }\n}\nvoid insert_string(string &in, set<ull> &S) {\n rep(i, (int)in.size() + 1) {\n rep(j, 26) {\n string tmp = in.substr(0, i);\n tmp += 'a' + j;\n string tmp2 = in.substr(i);\n rep(k, tmp2.size()) tmp += (char)tmp2[k];\n S.insert(calchash(tmp));\n }\n }\n}\nvoid replace_string(string &in, set<ull> &S) {\n rep(i, (int)in.size()) {\n char tmp = in[i];\n rep(j, 26) {\n in[i] = 'a' + j;\n S.insert(calchash(in));\n }\n in[i] = tmp;\n }\n}\nvoid swap_string(string &in, set<ull> &S) {\n rep(i, (int)in.size() - 1) {\n swap(in[i], in[i + 1]);\n S.insert(calchash(in));\n swap(in[i], in[i + 1]);\n }\n}\nvoid make_string(vector<string> &in, set<ull> S[]) {\n rep(i, (int)in.size()) {\n del_string(in[i], S[i]);\n insert_string(in[i], S[i]);\n replace_string(in[i], S[i]);\n swap_string(in[i], S[i]);\n }\n}\nvoid solve(vector<string> in, set<ull> S[], int d) {\n int n = (int)in.size();\n rep(i, n) {\n REP(j, i + 1, n) {\n ed[i][j] = edit_distance(in[i], in[j]);\n if (ed[i][j] <= d)\n isok[i][j] = true;\n }\n }\n make_string(in, S);\n ed1(in, S);\n if (d == 2)\n ed2(in, S);\n int cnt = 0;\n rep(i, n) {\n rep(j, n) if (isok[i][j]) cnt++, cout << in[i] << \",\" << in[j] << endl;\n }\n cout << cnt << endl;\n}\nvoid test() {\n string in = \"test\";\n set<ull> S;\n insert_string(in, S);\n}\nmain() {\n int n, d;\n while (cin >> n >> d && n) {\n vector<string> in;\n rep(i, n) rep(j, n) isok[i][j] = false, ed[i][j] = (1 << 21);\n set<ull> S[n];\n rep(i, n) {\n string tmp;\n cin >> tmp;\n in.pb(tmp);\n }\n sort(ALL(in));\n rep(i, (int)in.size()) S[i].insert(calchash(in[i]));\n solve(in, S, d);\n }\n}", "accuracy": 1, "time_ms": 430, "memory_kb": 10112, "score_of_the_acc": -0.2863, "final_rank": 5 } ]
aoj_1260_cpp
Problem D: Organize Your Train In the good old Hachioji railroad station located in the west of Tokyo, there are several parking lines, and lots of freight trains come and go every day. All freight trains travel at night, so these trains containing various types of cars are settled in your parking lines early in the morning. Then, during the daytime, you must reorganize cars in these trains according to the request of the railroad clients, so that every line contains the “right” train, i.e. the right number of cars of the right types, in the right order. As shown in Figure 7, all parking lines run in the East-West direction. There are exchange lines connecting them through which you can move cars. An exchange line connects two ends of different parking lines. Note that an end of a parking line can be connected to many ends of other lines. Also note that an exchange line may connect the East-end of a parking line and the West-end of another. Cars of the same type are not discriminated between each other. The cars are symmetric, so directions of cars don’t matter either. You can divide a train at an arbitrary position to make two sub-trains and move one of them through an exchange line connected to the end of its side. Alternatively, you may move a whole train as is without dividing it. Anyway, when a (sub-) train arrives at the destination parking line and the line already has another train in it, they are coupled to form a longer train. Your superautomatic train organization system can do these without any help of locomotive engines. Due to the limitation of the system, trains cannot stay on exchange lines; when you start moving a (sub-) train, it must arrive at the destination parking line before moving another train. In what follows, a letter represents a car type and a train is expressed as a sequence of letters. For example in Figure 8, from an initial state having a train " aabbccdee " on line 0 and no trains on other lines, you can make " bbaadeecc " on line 2 with the four moves shown in the figure. To cut the cost out, your boss wants to minimize the number of (sub-) train movements. For example, in the case of Figure 8, the number of movements is 4 and this is the minimum. Given the configurations of the train cars in the morning (arrival state) and evening (departure state), your job is to write a program to find the optimal train reconfiguration plan. Input The input consists of one or more datasets. A dataset has the following format: x y p 1 P 1 q 1 Q 1 p 2 P 2 q 2 Q 2 . . . p y P y q y Q y s 0 s 1 . . . s x -1 t 0 t 1 . . . t x -1 x is the number of parking lines, which are numbered from 0 to x -1. y is the number of exchange lines. Then y lines of the exchange line data follow, each describing two ends connected by the exchange line; p i and q i are integers between 0 and x - 1 which indicate parking line numbers, and P i and Q i are either " E " (East) or " W " (West) which indicate the ends of the parking lines. Then x lines of ...(truncated)
[ { "submission_id": "aoj_1260_10851287", "code_snippet": "#include <cmath>\n#include <ctime>\n#include <cstdio>\n#include <cctype>\n#include <cstdlib>\n#include <cstring>\n#include <iostream>\n#include <sstream>\n#include <algorithm>\n#include <set>\n#include <stack>\n#include <queue>\n#include <string>\n#include <vector>\n#include <map>\n#define maxn 110\n#define maxl 1000000000\n#define mod 1000000007\nusing namespace std;\n\ntypedef unsigned long long ull;\n\n\ntypedef vector<string> ZT;\ntypedef set<ZT>::iterator IT;\nset<ZT> be[4],en[4];\nint ans,n;\nint g[100][100];\n\nbool check(int id,const ZT &vv,set<ZT> be[],set<ZT> en[]){\n\tfor(int i=0;i<=id+1;++i){\n\t\tif(en[i].count(vv)){\n\t\t\tans=id+1+i;\n\t\t\t//for(int j=0;j<n;++j)cout<<vv[j]<<endl;\n\t\t\treturn true;\n\t\t}\n\t\tif(be[i].count(vv))return false;\n\t}\n\tbe[id+1].insert(vv);\n\treturn false;\n}\n\nvoid ex(int id,set<ZT> be[],set<ZT> en[]){\n\tIT it;\n\tint i,j,k;\n\tfor(it=be[id].begin();it!=be[id].end();++it){\n\t\tZT v=*it,vv;\n\t\tfor(i=0;i<n;++i)if(v[i]!=\"\")\n\t\t\tfor(j=0;j<n;++j)if(j!=i){\n\t\t\t\tif(g[i][j]&1){\n\t\t\t\t\tvv=v;\n\t\t\t\t\tfor(k=0;k<v[i].size();++k){\n\t\t\t\t\t\tvv[j].insert(vv[j].begin(),v[i][k]);\n\t\t\t\t\t\tvv[i].erase(0,1);\n\t\t\t\t\t\tif(check(id,vv,be,en))return;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\tif(g[i][j]&2){\n\t\t\t\t\tvv=v;\n\t\t\t\t\tfor(k=0;k<v[i].size();++k){\n\t\t\t\t\t\tvv[j].insert(vv[j].end(),v[i][k]);\n\t\t\t\t\t\tvv[i].erase(0,1);\n\t\t\t\t\t\tif(check(id,vv,be,en))return;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\tif(g[i][j]&4){\n\t\t\t\t\tvv=v;\n\t\t\t\t\tfor(k=v[i].size()-1;k>=0;--k){\n\t\t\t\t\t\tvv[j].insert(vv[j].begin(),v[i][k]);\n\t\t\t\t\t\tvv[i].erase(vv[i].size()-1,1);\n\t\t\t\t\t\tif(check(id,vv,be,en))return;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\tif(g[i][j]&8){\n\t\t\t\t\tvv=v;\n\t\t\t\t\tfor(k=v[i].size()-1;k>=0;--k){\n\t\t\t\t\t\tvv[j].insert(vv[j].end(),v[i][k]);\n\t\t\t\t\t\tvv[i].erase(vv[i].size()-1,1);\n\t\t\t\t\t\tif(check(id,vv,be,en))return;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\t\n\t\t\t}\n\t}\n}\n\nbool solve(){\n\tint m,i,j,temp,x,y;\n\tstring s;\n\tchar c1,c2;\n\tscanf(\"%d%d\",&n,&m);\n\tif(n==0 && m==0)return false;\n\tfor(i=0;i<n;++i)\n\t\tfor(j=0;j<n;++j)g[i][j]=0;\n\tfor(i=1;i<=m;++i){\n\t\tscanf(\"%d%c%d%c\",&x,&c1,&y,&c2);\n\t\tif(c1=='W'){\n\t\t\tif(c2=='W')temp=1;else temp=2;\n\t\t}else {\n\t\t\tif(c2=='W')temp=4;else temp=8;\n\t\t}\n\t\tg[x][y]|=temp;\n\t\tswap(x,y);swap(c1,c2);\n\t\tif(c1=='W'){\n\t\t\tif(c2=='W')temp=1;else temp=2;\n\t\t}else {\n\t\t\tif(c2=='W')temp=4;else temp=8;\n\t\t}\n\t\tg[x][y]|=temp;\n\t}\n\tfor(i=0;i<=3;++i){\n\t\tbe[i].clear();\n\t\ten[i].clear();\n\t}\n\tZT v1,v2;\n\tv1.clear();\n\tfor(i=0;i<n;++i){\n\t\tcin>>s;\n\t\tif(s==\"-\")s=\"\";\n\t\tv1.push_back(s);\n\t}\n\tbe[0].insert(v1);\n\t//for(int j=0;j<n;++j)cout<<v1[j]<<endl;\n\tv1.clear();\n\tfor(i=0;i<n;++i){\n\t\tcin>>s;\n\t\tif(s==\"-\")s=\"\";\n\t\tv1.push_back(s);\n\t}\n\ten[0].insert(v1);\n\t//for(int j=0;j<n;++j)cout<<v1[j]<<endl;\n\tans=-1;\n\t//cout<<\"here\"<<endl;\n\tfor(i=0;i<=2;++i){\n\t\tex(i,be,en);\n\t\tif(ans>0)break;\n\t\t//cout<<\"look\"<<endl;\n\t\tex(i,en,be);\n\t\tif(ans>0)break;\n\t}\n\tif(ans==-1){\n\t\tint y=1,x=0;\n\t\t//exit(y/x);\n\t}\n\tcout<<ans<<endl;\n\treturn true;\n}\n\nint main(){\n\twhile(solve());\n\treturn 0;\n}", "accuracy": 1, "time_ms": 1810, "memory_kb": 35196, "score_of_the_acc": -0.4633, "final_rank": 7 }, { "submission_id": "aoj_1260_4951825", "code_snippet": "#include <iostream>\n#include <vector>\n#include <cmath>\n#include <numeric>\n#include <cfloat>\n#include <climits>\n#include <algorithm>\n#include <iomanip>\n#include <queue>\n#include <unordered_set>\n#include <unordered_map>\n#include <string>\n#include <tuple>\n#include <set>\n#include <stack>\n#include <array>\nenum class Dir {\n\tEast, West\n};\nconstexpr std::array<Dir, 2> all_dir{ Dir::West, Dir::East };\nstruct End {\n\tint id;\n\tDir dir;\n};\nstd::istream& operator>>(std::istream& is, End& end) {\n\tchar id, dir;\n\tis >> id >> dir;\n\tend.id = id - '0';\n\tend.dir = dir == 'W' ? Dir::West : Dir::East;\n\treturn is;\n}\nstruct ExchangeLine {\n\tEnd from, to;\n};\nstruct ParkingLine {\n\tlong long int hash{ 0 }, power{ 1 };\n\tbool is_empty() const {\n\t\treturn power == 1;\n\t}\n\tvoid push_east(const int car) {\n\t\thash *= 10;\n\t\thash += car;\n\t\tpower *= 10;\n\t}\n\tvoid push_west(const int car) {\n\t\thash += power * car;\n\t\tpower *= 10;\n\t}\n\tint pop_east() {\n\t\tconst auto popped = hash % 10;\n\t\thash /= 10;\n\t\tpower /= 10;\n\t\treturn popped;\n\t}\n\tint pop_west() {\n\t\tpower /= 10;\n\t\tconst int popped = hash / power;\n\t\thash %= power;\n\t\treturn popped;\n\t}\n\tvoid push(const Dir dir, const int car) {\n\t\tswitch (dir) {\n\t\tcase Dir::West: return push_west(car);\n\t\tcase Dir::East: return push_east(car);\n\t\tdefault: throw 0;\n\t\t}\n\t}\n\tint pop(const Dir dir) {\n\t\tswitch (dir) {\n\t\tcase Dir::West: return pop_west();\n\t\tcase Dir::East: return pop_east();\n\t\tdefault: throw 0;\n\t\t}\n\t}\n\tbool operator==(const ParkingLine& other) const {\n\t\treturn hash == other.hash && power == other.power;\n\t}\n\tbool operator!=(const ParkingLine& other) const {\n\t\treturn !(*this == other);\n\t}\n};\ntemplate<>\nstruct std::hash<ParkingLine> {\n\tsize_t operator()(const ParkingLine& line) const {\n\t\tsize_t result{ 0 };\n\t\tconst auto hasher = std::hash<long long int>();\n\t\tcombine(result, hasher(line.hash));\n\t\tcombine(result, hasher(line.power));\n\t\treturn result;\n\t}\n\tvoid combine(size_t& seed, const size_t added) const {\n\t\tseed ^= added + 0x9E3779B9 + (seed << 6) + (seed >> 2);\n\t}\n};\n\nstruct ParkingLines {\n\tstd::vector<ParkingLine> lines;\n\tbool operator==(const ParkingLines& other) const {\n\t\tif (lines.size() != other.lines.size()) return false;\n\t\tfor (auto i = 0; i < lines.size(); ++i) {\n\t\t\tif (lines[i] != other.lines[i]) return false;\n\t\t}\n\t\treturn true;\n\t}\n\tconst ParkingLine& operator[](const int index) const {\n\t\treturn lines[index];\n\t}\n\tParkingLine& operator[](const int index) {\n\t\treturn lines[index];\n\t}\n};\ntemplate<>\nstruct std::hash<ParkingLines> {\n\tsize_t operator()(const ParkingLines& line) const {\n\t\tsize_t result{ 0 };\n\t\tconst auto hasher = std::hash<ParkingLine>();\n\t\tfor (const auto& line : line.lines) {\n\t\t\tcombine(result, hasher(line));\n\t\t}\n\t\treturn result;\n\t}\n\tvoid combine(size_t& seed, const size_t added) const {\n\t\tseed ^= added + 0x9E3779B9 + (seed << 6) + (seed >> 2);\n\t}\n};\n\nstruct Path {\n\tint to;\n\tDir dir;\n\tbool operator<(const Path& other) const {\n\t\tif (to != other.to) return to < other.to;\n\t\treturn dir < other.dir;\n\t}\n\tbool operator==(const Path& other) const {\n\t\treturn to == other.to && dir == other.dir;\n\t}\n};\nstruct Connection {\n\tstd::vector<End> at_east, at_west;\n\tconst std::vector<End>& next_to(const Dir dir) const {\n\t\tswitch (dir) {\n\t\tcase Dir::East: return at_east;\n\t\tcase Dir::West: return at_west;\n\t\tdefault: throw 0;\n\t\t}\n\t}\n};\nDir rev(const Dir dir) {\n\tswitch (dir) {\n\tcase Dir::East: return Dir::West;\n\tcase Dir::West: return Dir::East;\n\tdefault: throw 0;\n\t}\n}\nstd::vector<Path> calc_path(const ParkingLines& state, const int start, const Dir dir, const std::vector<Connection>& connections) {\n\tstd::set<Path> set;\n\tstd::queue<Path> queue;\n\tfor (const auto end : connections[start].next_to(dir)) {\n\t\tqueue.push(Path{ end.id, end.dir });\n\t\tset.insert(Path{ end.id, end.dir });\n\t}\n\twhile (!queue.empty()) {\n\t\tconst auto top = queue.front(); queue.pop();\n\t\tif (!state[top.to].is_empty()) continue;\n\t\tfor (const auto end : connections[top.to].next_to(rev(top.dir))) {\n\t\t\tconst Path path{ end.id, end.dir };\n\t\t\tconst auto find = set.find(path);\n\t\t\tif (find == set.end()) {\n\t\t\t\tset.insert(path);\n\t\t\t\tqueue.push(path);\n\t\t\t}\n\t\t}\n\t}\n\tstd::vector<Path> result; result.reserve(set.size());\n\tstd::copy(set.begin(), set.end(), std::back_inserter(result));\n\treturn result;\n}\nstd::vector<ParkingLines> next_states(ParkingLines state, const int start, const std::vector<Connection>& connections) {\n\tif (state[start].is_empty()) return {};\n\tstd::vector<ParkingLines> result;\n\tfor (const auto dir : all_dir) {\n\t\tfor (const auto end : connections[start].next_to(dir)) {\n\t\t\tif (dir == end.dir && start == end.id) continue;\n\t\t\tint count{ 0 };\n\t\t\tfor (auto pow = state[start].power; pow > 1; pow /= 10) ++count;\n\t\t\tfor (auto i = 0; i < count; ++i) {\n\t\t\t\tstate[end.id].push(end.dir, state[start].pop(dir));\n\t\t\t\tresult.push_back(state);\n\t\t\t}\n\t\t\tfor (auto i = 0; i < count; ++i) {\n\t\t\t\tstate[start].push(dir, state[end.id].pop(end.dir));\n\t\t\t}\n\t\t}\n\t}\n\treturn result;\n}\nstd::unordered_map<ParkingLines, int> moved(ParkingLines start, const std::vector<Connection>& connections) {\n\tstd::queue<ParkingLines> queue;\n\tstd::unordered_map<ParkingLines, int> map;\n\tmap[start] = 0;\n\tqueue.push(start);\n\tfor (auto i = 1; i <= 3; ++i) {\n\t\tfor (auto c = queue.size(); c > 0; --c) {\n\t\t\tconst auto top = queue.front(); queue.pop();\n\t\t\tfor (auto l = 0; l < start.lines.size(); ++l) {\n\t\t\t\tfor (const auto next : next_states(top, l, connections)) {\n\t\t\t\t\tconst auto find = map.find(next);\n\t\t\t\t\tif (find == map.end()) {\n\t\t\t\t\t\tmap[next] = i;\n\t\t\t\t\t\tqueue.push(next);\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n\treturn map;\n}\nint solve(ParkingLines start, ParkingLines goal, std::vector<Connection> connections) {\n\tif (start == goal) return 0;\n\tconst auto forward = moved(start, connections);\n\tint min_step = INT_MAX;\n\tfor (const auto pair : moved(goal, connections)) {\n\t\tconst auto find = forward.find(pair.first);\n\t\tif (find != forward.end()) {\n\t\t\tmin_step = std::min(min_step, pair.second + find->second);\n\t\t}\n\t}\n\treturn min_step;\n}\nint main() {\n\twhile (true) {\n\t\tint x, y; std::cin >> x >> y;\n\t\tif (x == 0 && y == 0) break;\n\t\tstd::vector<Connection> connections(x);\n\t\tfor (auto i = 0; i < y; ++i) {\n\t\t\tEnd a, b;\n\t\t\tstd::cin >> a >> b;\n\t\t\tswitch (a.dir) {\n\t\t\tcase Dir::West: connections[a.id].at_west.push_back(b); break;\n\t\t\tcase Dir::East: connections[a.id].at_east.push_back(b); break;\n\t\t\tdefault: throw 0;\n\t\t\t}\n\t\t\tswitch (b.dir) {\n\t\t\tcase Dir::West: connections[b.id].at_west.push_back(a); break;\n\t\t\tcase Dir::East: connections[b.id].at_east.push_back(a); break;\n\t\t\tdefault: throw 0;\n\t\t\t}\n\t\t}\n\t\tstd::vector<std::string> start(x), goal(x);\n\t\tfor (auto& line : start) {\n\t\t\tstd::cin >> line;\n\t\t\tif (line == \"-\") line = \"\";\n\t\t}\n\t\tfor (auto& line : goal) {\n\t\t\tstd::cin >> line;\n\t\t\tif (line == \"-\") line = \"\";\n\t\t}\n\t\tstd::vector<char> all_char;\n\t\tfor (const auto line : start) for (const auto c : line) all_char.push_back(c);\n\t\tfor (const auto line : goal) for (const auto c : line) all_char.push_back(c);\n\t\tstd::sort(all_char.begin(), all_char.end());\n\t\tall_char.erase(std::unique(all_char.begin(), all_char.end()), all_char.end());\n\t\tParkingLines start_state{ std::vector<ParkingLine>(x) }, goal_state{ std::vector<ParkingLine>(x) };\n\t\tfor (auto i = 0; i < x; ++i) {\n\t\t\tfor (const auto c : start[i]) {\n\t\t\t\tstart_state[i].push_east(std::distance(all_char.begin(), std::find(all_char.begin(), all_char.end(), c)));\n\t\t\t}\n\t\t\tfor (const auto c : goal[i]) {\n\t\t\t\tgoal_state[i].push_east(std::distance(all_char.begin(), std::find(all_char.begin(), all_char.end(), c)));\n\t\t\t}\n\t\t}\n\t\tstd::cout << solve(start_state, goal_state, connections) << '\\n';\n\t}\n}", "accuracy": 1, "time_ms": 1140, "memory_kb": 74076, "score_of_the_acc": -0.8055, "final_rank": 8 }, { "submission_id": "aoj_1260_1550575", "code_snippet": "#include <iostream>\n#include <vector>\n#include <map>\n#include <unordered_map>\n#include <cstdint>\n#include <algorithm>\n#include <iterator>\n#define repeat_from(i,m,n) for (int i = (m); (i) < (n); ++(i))\n#define repeat(i,n) repeat_from(i,0,n)\nusing namespace std;\nenum we_t { W, E };\ntemplate <typename It>\nvoid step(vector<string> const & s, map<pair<int,we_t>,vector<pair<int,we_t> > > const & g, It it) {\n int x = s.size();\n repeat (i,x) {\n repeat (n,s[i].size()+1) {\n string l = s[i].substr(0, n);\n string r = s[i].substr(n);\n for (we_t p : { W, E }) {\n if ((l.empty() and p == W) or (r.empty() and p == E)) continue;\n auto v = make_pair(i,p);\n for (auto w : g.at(v)) {\n int j = w.first;\n we_t q = w.second;\n vector<string> t = s;\n t[i] = p == W ? r : l;\n string u = p == W ? l : r;\n if (p == q) reverse(u.begin(), u.end());\n t[j] = q == W ? u + s[j] : s[j] + u;\n *(it ++) = t;\n }\n }\n }\n }\n}\nmap<vector<string>,int> steps(vector<string> const & s, map<pair<int,we_t>,vector<pair<int,we_t> > > const & g, int n) {\n map<vector<string>,int> ms;\n ms[s] = 0;\n vector<vector<string> > vs;\n vs.push_back(s);\n repeat (i,n) {\n vector<vector<string> > ws;\n for (auto it : vs) {\n step(it, g, back_inserter(ws));\n }\n vs.clear();\n for (auto it : ws) {\n if (not ms.count(it)) {\n ms[it] = i+1;\n vs.push_back(it);\n }\n }\n }\n return ms;\n}\nint main() {\n while (true) {\n int x, y; cin >> x >> y;\n if (x == 0 and y == 0) break;\n map<pair<int,we_t>,vector<pair<int,we_t> > > g;\n repeat (i,x) for (we_t j : { W, E }) g[make_pair(i,j)];\n repeat (i,y) {\n char p, P, q, Q; cin >> p >> P >> q >> Q;\n auto a = make_pair(p - '0', P == 'W' ? W : E);\n auto b = make_pair(q - '0', Q == 'W' ? W : E);\n g[a].push_back(b);\n g[b].push_back(a);\n }\n vector<string> s(x); repeat (i,x) { cin >> s[i]; if (s[i] == \"-\") s[i].clear(); }\n vector<string> t(x); repeat (i,x) { cin >> t[i]; if (t[i] == \"-\") t[i].clear(); }\n map<vector<string>,int> s2 = steps(s, g, 2);\n map<vector<string>,int> t2 = steps(t, g, 2);\n int result = 6;\n for (auto it : s2) {\n if (t2.count(it.first)) {\n result = min(result, it.second + t2[it.first]);\n }\n if (it.second == 2) {\n vector<vector<string> > s3;\n step(it.first, g, back_inserter(s3));\n for (auto that : s3) {\n if (t2.count(that)) {\n result = min(result, 1 + it.second + t2[that]);\n }\n }\n }\n }\n cout << result << endl;\n }\n return 0;\n}", "accuracy": 1, "time_ms": 1720, "memory_kb": 4828, "score_of_the_acc": -0.0994, "final_rank": 2 }, { "submission_id": "aoj_1260_1550571", "code_snippet": "#include <iostream>\n#include <cassert>\n#include <vector>\n#include <deque>\n#include <map>\n#include <unordered_map>\n#include <queue>\n#include <cstdint>\n#include <algorithm>\n#include <iterator>\n#define repeat_from(i,m,n) for (int i = (m); (i) < (n); ++(i))\n#define repeat(i,n) repeat_from(i,0,n)\nusing namespace std;\nuint64_t pack(vector<string> const & s) {\n uint64_t t = 0;\n repeat (i, s.size()) {\n t *= 27;\n for (char c : s[i]) {\n t *= 27;\n t += c - 'a' + 1;\n }\n }\n return t;\n}\nenum we_t { W, E };\ntemplate <typename It>\nvoid step(vector<string> const & s, map<pair<int,we_t>,vector<pair<int,we_t> > > const & g, It it) {\n int x = s.size();\n repeat (i,x) {\n repeat (n,s[i].size()+1) {\n string l = s[i].substr(0, n);\n string r = s[i].substr(n);\n for (we_t p : { W, E }) {\n if ((l.empty() and p == W) or (r.empty() and p == E)) continue;\n auto v = make_pair(i,p);\n for (auto w : g.at(v)) {\n int j = w.first;\n we_t q = w.second;\n vector<string> t = s;\n t[i] = p == W ? r : l;\n string u = p == W ? l : r;\n if (p == q) reverse(u.begin(), u.end());\n t[j] = q == W ? u + s[j] : s[j] + u;\n *(it ++) = t;\n }\n }\n }\n }\n}\nmap<vector<string>,int> steps(vector<string> const & s, map<pair<int,we_t>,vector<pair<int,we_t> > > const & g, int n) {\n map<vector<string>,int> ms;\n ms[s] = 0;\n vector<vector<string> > vs;\n vs.push_back(s);\n repeat (i,n) {\n vector<vector<string> > ws;\n for (auto it : vs) {\n step(it, g, back_inserter(ws));\n }\n vs.clear();\n for (auto it : ws) {\n if (not ms.count(it)) {\n ms[it] = i+1;\n vs.push_back(it);\n }\n }\n }\n return ms;\n}\nint main() {\n while (true) {\n int x, y; cin >> x >> y;\n if (x == 0 and y == 0) break;\n map<pair<int,we_t>,vector<pair<int,we_t> > > g;\n repeat (i,x) for (we_t j : { W, E }) g[make_pair(i,j)];\n repeat (i,y) {\n char p, P, q, Q; cin >> p >> P >> q >> Q;\n auto a = make_pair(p - '0', P == 'W' ? W : E);\n auto b = make_pair(q - '0', Q == 'W' ? W : E);\n g[a].push_back(b);\n g[b].push_back(a);\n }\n vector<string> s(x); repeat (i,x) { cin >> s[i]; if (s[i] == \"-\") s[i].clear(); }\n vector<string> t(x); repeat (i,x) { cin >> t[i]; if (t[i] == \"-\") t[i].clear(); }\n map<vector<string>,int> s2 = steps(s, g, 2);\n map<vector<string>,int> t2 = steps(t, g, 2);\n unordered_map<uint64_t,int> t2h; // required\n for (auto it : t2) t2h[pack(it.first)] = it.second;\n int result = 6;\n for (auto it : s2) {\n uint64_t ha = pack(it.first);\n if (t2h.count(ha)) {\n result = min(result, it.second + t2h[ha]);\n }\n if (it.second == 2) {\n vector<vector<string> > s3;\n step(it.first, g, back_inserter(s3));\n for (auto that : s3) {\n uint64_t hb = pack(that);\n if (t2h.count(hb)) {\n result = min(result, 1 + it.second + t2h[hb]);\n }\n }\n }\n }\n cout << result << endl;\n }\n return 0;\n}", "accuracy": 1, "time_ms": 1090, "memory_kb": 4828, "score_of_the_acc": 0, "final_rank": 1 }, { "submission_id": "aoj_1260_1550502", "code_snippet": "#include <iostream>\n#include <vector>\n#include <deque>\n#include <map>\n#include <unordered_map>\n#include <queue>\n#include <cstdint>\n#include <algorithm>\n#include <iterator>\n#define repeat_from(i,m,n) for (int i = (m); (i) < (n); ++(i))\n#define repeat(i,n) repeat_from(i,0,n)\nusing namespace std;\nuint64_t pack(vector<deque<char> > const & s) {\n uint64_t t = 0;\n repeat (i, s.size()) {\n t *= 27;\n for (char c : s[i]) {\n t *= 27;\n t += c - 'a' + 1;\n }\n }\n return t;\n}\nenum we_t { W, E };\ntemplate <typename It>\nvoid step(vector<deque<char> > const & s, map<pair<int,we_t>,vector<pair<int,we_t> > > const & g, It it) {\n int x = s.size();\n repeat (i,x) {\n repeat_from (n,1,s[i].size()+1) {\n for (we_t p : { W, E }) {\n auto v = make_pair(i,p);\n for (auto w : g.at(v)) {\n int j = w.first;\n vector<deque<char> > t = s;\n queue<char> u;\n repeat (k,n) {\n if (v.second == W) {\n u.push(t[i].front());\n t[i].pop_front();\n } else {\n u.push(t[i].back());\n t[i].pop_back();\n }\n }\n repeat (k,n) {\n if (w.second == W) {\n t[j].push_front(u.front());\n } else {\n t[j].push_back(u.front());\n }\n u.pop();\n }\n *(it ++) = t;\n }\n }\n }\n }\n}\nmap<vector<deque<char> >,int> steps(vector<deque<char> > const & s, map<pair<int,we_t>,vector<pair<int,we_t> > > const & g, int n) {\n map<vector<deque<char> >,int> ms;\n ms[s] = 0;\n vector<vector<deque<char> > > vs;\n vs.push_back(s);\n repeat (i,n) {\n vector<vector<deque<char> > > ws;\n for (auto it : vs) {\n step(it, g, back_inserter(ws));\n }\n vs.clear();\n for (auto it : ws) {\n if (not ms.count(it)) {\n ms[it] = i+1;\n vs.push_back(it);\n }\n }\n }\n return ms;\n}\nint main() {\n while (true) {\n int x, y; cin >> x >> y;\n if (x == 0 and y == 0) break;\n map<pair<int,we_t>,vector<pair<int,we_t> > > g;\n repeat (i,x) for (we_t j : { W, E }) g[make_pair(i,j)];\n repeat (i,y) {\n char p, P, q, Q; cin >> p >> P >> q >> Q;\n auto a = make_pair(p - '0', P == 'W' ? W : E);\n auto b = make_pair(q - '0', Q == 'W' ? W : E);\n g[a].push_back(b);\n g[b].push_back(a);\n }\n vector<deque<char> > s(x); repeat (i,x) { string a; cin >> a; if (a == \"-\") a.clear(); s[i] = deque<char>(a.begin(), a.end()); }\n vector<deque<char> > t(x); repeat (i,x) { string a; cin >> a; if (a == \"-\") a.clear(); t[i] = deque<char>(a.begin(), a.end()); }\n map<vector<deque<char> >,int> s2 = steps(s, g, 2);\n map<vector<deque<char> >,int> t2 = steps(t, g, 2);\n unordered_map<uint64_t,int> t2h;\n for (auto it : t2) t2h[pack(it.first)] = it.second;\n int result = 6;\n for (auto it : s2) {\n uint64_t ha = pack(it.first);\n if (t2h.count(ha)) {\n result = min(result, it.second + t2h[ha]);\n }\n if (it.second == 2) {\n vector<vector<deque<char> > > s3;\n step(it.first, g, back_inserter(s3));\n for (auto that : s3) {\n uint64_t hb = pack(that);\n if (t2h.count(hb)) {\n result = min(result, 1 + it.second + t2h[hb]);\n }\n }\n }\n }\n cout << result << endl;\n }\n return 0;\n}", "accuracy": 1, "time_ms": 4430, "memory_kb": 86408, "score_of_the_acc": -1.4664, "final_rank": 13 }, { "submission_id": "aoj_1260_1067666", "code_snippet": "#include <algorithm>\n#include <array>\n#include <cstdlib>\n#include <iostream>\n#include <queue>\n#include <unordered_map>\n#include <vector>\nusing namespace std;\n\ntemplate<class T> inline void chmin(T &a, const T &b) { if(a > b) a = b; }\n\ntypedef array<int, 2> A;\ntypedef pair<A, A> E;\ntypedef unsigned hash_t;\n\nhash_t rolling_hash(const string &s) {\n\tstatic constexpr hash_t base = 1000000007;\n\thash_t res = 0;\n\tfor(const auto &c : s) {\n\t\tres = res * base + c;\n\t}\n\treturn res;\n}\n \nstring to_str(const vector<string> &state) {\n\tstring res = \"\";\n\tfor(const auto &s : state) {\n\t\tres += s + \":\";\n\t}\n\treturn res;\n}\n \nunordered_map<hash_t, int> bfs(int limit, const vector<string> &start, const vector<E> &exchanges) {\n\tunordered_map<hash_t, int> res;\n\tqueue<vector<string>> que;\n \n\tres.insert({rolling_hash(to_str(start)), 0});\n\tque.push(start);\n \n\twhile(!que.empty()) {\n\t\tvector<string> state = que.front();\n\t\tque.pop();\n\t\tconst int d = res.at(rolling_hash(to_str(state)));\n \n\t\tif(d == limit) break;\n \n\t\tfor(const auto &element : exchanges) {\n\t\t\tconst A &pos = element.first;\n\t\t\tconst A &dir = element.second;\n\t\t\tconst array<string, 2> train{move(state[pos.front()]), move(state[pos.back()])};\n\n\t\t\tfor(int i = 0; i <= 1; ++i) {\n\t\t\t\tconst int from = i;\n\t\t\t\tconst int to = 1 - i;\n\n\t\t\t\tconst int &p1 = pos[from];\n\t\t\t\tconst int &p2 = pos[to];\n\t\t\t\tconst int &d1 = dir[from];\n\t\t\t\tconst int &d2 = dir[to];\n\t\t\t\tconst string &t1 = train[from];\n\t\t\t\tconst string &t2 = train[to];\n\n\t\t\t\tfor(int num = 1; num <= t1.size(); ++num) {\n\t\t\t\t\tstring tmp = (d1 ? t1.substr(0, num) : t1.substr(t1.size() - num));\n\t\t\t\t\tif(d1 == d2) reverse(tmp.begin(), tmp.end());\n\t\t\t\t\tstate[p1] = (d1 ? t1.substr(num) : t1.substr(0, t1.size() - num));\n\t\t\t\t\tstate[p2] = (d2 ? tmp + t2 : t2 + tmp);\n \n\t\t\t\t\tconst hash_t h = rolling_hash(to_str(state));\n\t\t\t\t\tif(!res.count(h)) {\n\t\t\t\t\t\tres.insert({h, d + 1});\n\t\t\t\t\t\tque.push(state);\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n \n\t\t\tstate[pos.front()] = move(train.front());\n\t\t\tstate[pos.back()] = move(train.back());\n\t\t}\n\t}\n \n\treturn res;\n}\n \nint main(){\n\tcin.tie(nullptr);\n\tios::sync_with_stdio(false);\n \n\tfor(int x, y; cin >> x >> y && x;) {\n\t\tvector<E> exchanges;\n\t\texchanges.reserve(y);\n \n\t\tfor(int i = 0; i < y; ++i) {\n\t\t\tstring a, b;\n\t\t\tcin >> a >> b;\n \n\t\t\tconst int p1 = a[0] - '0';\n\t\t\tconst int d1 = (a[1] == 'W');\n\t\t\tconst int p2 = b[0] - '0';\n\t\t\tconst int d2 = (b[1] == 'W');\n \n\t\t\texchanges.emplace_back(A{p1, p2}, A{d1, d2});\n\t\t}\n \n\t\tvector<string> lines(x), goal(x);\n \n\t\tfor(auto &e : lines) {\n\t\t\tcin >> e;\n\t\t\tif(e == \"-\") e = \"\";\n\t\t}\n \n\t\tfor(auto &e : goal) {\n\t\t\tcin >> e;\n\t\t\tif(e == \"-\") e = \"\";\n\t\t}\n \n\t\tconst auto d1 = bfs(3, lines, exchanges);\n\t\tconst auto d2 = bfs(2, goal, exchanges);\n \n\t\tint ans = 6;\n\t\tfor(const auto &e1 : d2) {\n\t\t\tif(ans <= e1.second) continue;\n \n\t\t\tif(d1.count(e1.first)) {\n\t\t\t\tchmin(ans, e1.second + d1.at(e1.first));\n\t\t\t}\n\t\t}\n \n\t\tcout << ans << endl;\n\t}\n \n\treturn EXIT_SUCCESS;\n}", "accuracy": 1, "time_ms": 2010, "memory_kb": 31476, "score_of_the_acc": -0.452, "final_rank": 5 }, { "submission_id": "aoj_1260_1067301", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\n#define dump(a) (cerr << #a << \" = \" << (a) << endl)\n\n#define REP(i,a,b) for(int i=(a);i < (int)(b); i++)\n#define rep(i,n) REP(i,0,n)\n#define ALL(v) begin(v), end(v)\n\nint x, y;\nvector<pair<pair<int, bool>, pair<int, bool>>> exchanges;\n\ntypedef unsigned long long hash_t;\nhash_t rolling_hash(const string &s) {\n constexpr hash_t base = 1000000007;\n hash_t res = 0;\n for(const auto &c : s) {\n res = res * base + c;\n }\n return res;\n}\n\nstring to_str(const vector<string> &state) {\n string res = \"\";\n for(const auto &s : state) {\n res += s + \":\";\n }\n return res;\n}\n\nunordered_map<hash_t, int> bfs(int limit, const vector<string> &start) {\n unordered_map<hash_t, int> res;\n queue<vector<string>> que;\n\n res.insert({rolling_hash(to_str(start)), 0});\n que.push(start);\n\n while(!que.empty()) {\n vector<string> state = que.front();\n que.pop();\n const int d = res[rolling_hash(to_str(state))];\n\n if(d == limit) break;\n\n for(const auto &element : exchanges) {\n const int p1 = element.first.first;\n const bool d1 = element.first.second;\n const int p2 = element.second.first;\n const bool d2 = element.second.second;\n\n const string t1 = move(state[p1]);\n const string t2 = move(state[p2]);\n\n // p1 -> p2\n for(int num = 1; num <= t1.size(); ++num) {\n\tstring tmp = (d1 ? t1.substr(0, num) : t1.substr(t1.size() - num));\n\tif(d1 == d2) reverse(tmp.begin(), tmp.end());\n\tstate[p1] = (d1 ? t1.substr(num) : t1.substr(0, t1.size() - num));\n\tstate[p2] = (d2 ? tmp + t2 : t2 + tmp);\n\n\tconst hash_t h = rolling_hash(to_str(state));\n\tif(!res.count(h)) {\n\t res.insert({h, d + 1});\n\t que.push(state);\n\t}\n }\n\n // p2 -> p1\n for(int num = 1; num <= t2.size(); ++num) {\n\tstring tmp = (d2 ? t2.substr(0, num) : t2.substr(t2.size() - num));\n\tif(d1 == d2) reverse(tmp.begin(), tmp.end());\n\tstate[p1] = (d1 ? tmp + t1 : t1 + tmp);\n\tstate[p2] = (d2 ? t2.substr(num) : t2.substr(0, t2.size() - num));\n\n\tconst hash_t h = rolling_hash(to_str(state));\n\tif(!res.count(h)) {\n\t res.insert({h, d + 1});\n\t que.push(state);\n\t}\n }\n\n state[p1] = move(t1);\n state[p2] = move(t2);\n }\n }\n\n return res;\n}\n\nint main(){\n cin.tie(nullptr);\n ios::sync_with_stdio(false);\n\n while(cin >> x >> y && x) {\n exchanges.clear();\n exchanges.reserve(y);\n\n for(int i = 0; i < y; ++i) {\n string a, b;\n cin >> a >> b;\n\n const int p1 = a[0] - '0';\n const bool d1 = (a[1] == 'W');\n const int p2 = b[0] - '0';\n const bool d2 = (b[1] == 'W');\n\n exchanges.emplace_back(make_pair(p1, d1), make_pair(p2, d2));\n }\n\n vector<string> lines(x), goal(x);\n\n for(auto &e : lines) {\n cin >> e;\n if(e == \"-\") e = \"\";\n }\n\n for(auto &e : goal) {\n cin >> e;\n if(e == \"-\") e = \"\";\n }\n \n const auto d1 = bfs(3, lines);\n const auto d2 = bfs(2, goal);\n\n vector<string> tmp{\"cc\", \"\", \"bbaadee\"};\n\n int ans = 6;\n for(const auto &e1 : d2) {\n if(ans <= e1.second) continue;\n\n if(d1.count(e1.first)) {\n\tans = min(ans, e1.second + d1.at(e1.first));\n }\n }\n\n cout << ans << endl;\n }\n\n return 0;\n}", "accuracy": 1, "time_ms": 2060, "memory_kb": 31216, "score_of_the_acc": -0.4569, "final_rank": 6 }, { "submission_id": "aoj_1260_793378", "code_snippet": "#include <iostream>\n#include <vector>\n#include <set>\n#include <map>\n#include <algorithm>\n#include <queue>\n#include <cstring>\n#include <climits>\nusing namespace std;\n\ntypedef pair<int,int> P;\ntypedef pair<vector<string>, int> VI;\n\nint n;\nvector<P> t[4][2];\nvector<string> start, end;\nmap<vector<string>, int> half;\n\nint ans;\n\nvoid enumerateHalf(){\n queue<VI> open;\n open.push(VI(start, 0));\n half[start] = 0;\n\n while(!open.empty()){\n VI vi = open.front();\n open.pop();\n vector<string> vec = vi.first;\n int step = vi.second;\n\n for(int from = 0; from < n; from++){\n if(vec[from].size() == 0) continue;\n\n for(int cut = 0; cut < vec[from].size(); cut++){\n string cut_str[2] = {\n vec[from].substr(0, cut),\n vec[from].substr(cut)\n };\n string rev_str[2] = {cut_str[0], cut_str[1]};\n reverse(rev_str[0].begin(), rev_str[0].end());\n reverse(rev_str[1].begin(), rev_str[1].end());\n\n for(int from_dir = 0; from_dir < 2; from_dir++){\n if(cut_str[from_dir].size() == 0) continue;\n\n for(int to_idx = 0; to_idx < t[from][from_dir].size(); to_idx++){\n int to = t[from][from_dir][to_idx].first;\n int to_dir = t[from][from_dir][to_idx].second;\n vector<string> nvec = vec;\n\n if (from_dir == 0 && to_dir == 0) nvec[to] = rev_str[from_dir] + nvec[to];\n else if(from_dir == 0 && to_dir == 1) nvec[to] += cut_str[from_dir];\n else if(from_dir == 1 && to_dir == 0) nvec[to] = cut_str[from_dir] + nvec[to];\n else nvec[to] += rev_str[from_dir];\n\n nvec[from] = cut_str[!from_dir];\n\n map<vector<string>,int>::iterator tmp_iter = half.find(nvec);\n\n if(tmp_iter != half.end()) continue;\n half[nvec] = step + 1;\n\n if(step == 2) continue;\n open.push(VI(nvec, step + 1));\n }\n }\n }\n }\n }\n}\n\nvoid bfs(){\n queue<VI> open;\n open.push(VI(end, 0));\n\n map<vector<string>,int>::iterator tmp_iter = half.find(end);\n if(tmp_iter != half.end()) ans = min(ans, tmp_iter->second);\n\n while(!open.empty()){\n VI vi = open.front();\n open.pop();\n vector<string> vec = vi.first;\n int step = vi.second;\n\n for(int from = 0; from < n; from++){\n if(vec[from].size() == 0) continue;\n\n for(int cut = 0; cut < vec[from].size(); cut++){\n string cut_str[2] = {\n vec[from].substr(0, cut),\n vec[from].substr(cut)\n };\n string rev_str[2] = {cut_str[0], cut_str[1]};\n reverse(rev_str[0].begin(), rev_str[0].end());\n reverse(rev_str[1].begin(), rev_str[1].end());\n\n for(int from_dir = 0; from_dir < 2; from_dir++){\n if(cut_str[from_dir].size() == 0) continue;\n\n for(int to_idx = 0; to_idx < t[from][from_dir].size(); to_idx++){\n int to = t[from][from_dir][to_idx].first;\n int to_dir = t[from][from_dir][to_idx].second;\n vector<string> nvec = vec;\n\n if (from_dir == 0 && to_dir == 0) nvec[to] = rev_str[from_dir] + nvec[to];\n else if(from_dir == 0 && to_dir == 1) nvec[to] += cut_str[from_dir];\n else if(from_dir == 1 && to_dir == 0) nvec[to] = cut_str[from_dir] + nvec[to];\n else nvec[to] += rev_str[from_dir];\n\n nvec[from] = cut_str[!from_dir];\n\n map<vector<string>,int>::iterator tmp_iter = half.find(nvec);\n\n if(tmp_iter != half.end()){\n ans = min(ans, step + 1 + tmp_iter->second);\n continue;\n }\n\n if(step == 2) continue;\n open.push(VI(nvec, step + 1));\n }\n }\n }\n }\n }\n}\n\nvoid solve(){\n half.clear();\n enumerateHalf();\n\n ans = INT_MAX;\n bfs();\n cout << ans << endl;\n}\n\nint main(){\n int m;\n\n while(cin >> n >> m, n || m){\n for(int i = 0; i < 4; i++){\n for(int j = 0; j < 2; j++){\n t[i][j].clear();\n }\n }\n\n for(int i = 0; i < m; i++){\n string from, to;\n cin >> from >> to;\n\n int from_line = from[0] - '0';\n int from_dir = (from[1] == 'W' ? 0 : 1);\n int to_line = to[0] - '0';\n int to_dir = (to[1] == 'W' ? 0 : 1);\n\n t[from_line][from_dir].push_back(P(to_line, to_dir));\n t[to_line][to_dir].push_back(P(from_line, from_dir));\n }\n\n start.clear();\n end.clear();\n\n for(int i = 0; i < n; i++){\n string s;\n cin >> s;\n if(s == \"-\") s = \"\";\n start.push_back(s);\n }\n\n for(int i = 0; i < n; i++){\n string s;\n cin >> s;\n if(s == \"-\") s = \"\";\n end.push_back(s);\n }\n\n solve();\n }\n}", "accuracy": 1, "time_ms": 6010, "memory_kb": 25732, "score_of_the_acc": -1.0168, "final_rank": 11 }, { "submission_id": "aoj_1260_793376", "code_snippet": "#include <iostream>\n#include <vector>\n#include <set>\n#include <map>\n#include <algorithm>\n#include <queue>\n#include <cstring>\n#include <climits>\nusing namespace std;\n\ntypedef pair<int,int> P;\ntypedef pair<vector<string>, int> VI;\n\nint n;\nvector<P> t[4][2];\nvector<string> start, end;\nmap<vector<string>, int> half;\n\nint ans;\n\nvoid enumerateHalf(){\n queue<VI> open;\n open.push(VI(start, 0));\n half[start] = 0;\n\n while(!open.empty()){\n VI vi = open.front();\n open.pop();\n vector<string> vec = vi.first;\n int step = vi.second;\n\n for(int from = 0; from < n; from++){\n if(vec[from].size() == 0) continue;\n\n for(int cut = 0; cut < vec[from].size(); cut++){\n string cut_str[2] = {\n vec[from].substr(0, cut),\n vec[from].substr(cut)\n };\n string rev_str[2] = {cut_str[0], cut_str[1]};\n reverse(rev_str[0].begin(), rev_str[0].end());\n reverse(rev_str[1].begin(), rev_str[1].end());\n\n for(int from_dir = 0; from_dir < 2; from_dir++){\n if(cut_str[from_dir].size() == 0) continue;\n\n for(int to_idx = 0; to_idx < t[from][from_dir].size(); to_idx++){\n int to = t[from][from_dir][to_idx].first;\n int to_dir = t[from][from_dir][to_idx].second;\n vector<string> nvec = vec;\n\n if (from_dir == 0 && to_dir == 0) nvec[to] = rev_str[from_dir] + nvec[to];\n else if(from_dir == 0 && to_dir == 1) nvec[to] += cut_str[from_dir];\n else if(from_dir == 1 && to_dir == 0) nvec[to] = cut_str[from_dir] + nvec[to];\n else nvec[to] += rev_str[from_dir];\n\n nvec[from] = cut_str[!from_dir];\n\n map<vector<string>,int>::iterator tmp_iter = half.find(nvec);\n\n if(tmp_iter != half.end()) continue;\n half[nvec] = step + 1;\n\n if(step == 2) continue;\n open.push(VI(nvec, step + 1));\n }\n }\n }\n }\n }\n}\n\nvoid rec(int step, vector<string> vec){\n map<vector<string>,int>::iterator tmp_iter = half.find(vec);\n if(tmp_iter != half.end()){\n ans = min(ans, step + tmp_iter->second);\n return;\n }\n if(step == 3) return;\n\n for(int from = 0; from < n; from++){\n if(vec[from].size() == 0) continue;\n\n for(int cut = 0; cut < vec[from].size(); cut++){\n string cut_str[2] = {\n vec[from].substr(0, cut),\n vec[from].substr(cut)\n };\n string rev_str[2] = {cut_str[0], cut_str[1]};\n reverse(rev_str[0].begin(), rev_str[0].end());\n reverse(rev_str[1].begin(), rev_str[1].end());\n\n for(int from_dir = 0; from_dir < 2; from_dir++){\n if(cut_str[from_dir].size() == 0) continue;\n\n for(int to_idx = 0; to_idx < t[from][from_dir].size(); to_idx++){\n int to = t[from][from_dir][to_idx].first;\n int to_dir = t[from][from_dir][to_idx].second;\n vector<string> nvec = vec;\n\n if (from_dir == 0 && to_dir == 0) nvec[to] = rev_str[from_dir] + nvec[to];\n else if(from_dir == 0 && to_dir == 1) nvec[to] += cut_str[from_dir];\n else if(from_dir == 1 && to_dir == 0) nvec[to] = cut_str[from_dir] + nvec[to];\n else nvec[to] += rev_str[from_dir];\n\n nvec[from] = cut_str[!from_dir];\n\n rec(step + 1, nvec);\n }\n }\n }\n }\n}\n\nvoid solve(){\n half.clear();\n enumerateHalf();\n\n ans = INT_MAX;\n rec(0, end);\n cout << ans << endl;\n}\n\nint main(){\n int m;\n\n while(cin >> n >> m, n || m){\n for(int i = 0; i < 4; i++){\n for(int j = 0; j < 2; j++){\n t[i][j].clear();\n }\n }\n\n for(int i = 0; i < m; i++){\n string from, to;\n cin >> from >> to;\n\n int from_line = from[0] - '0';\n int from_dir = (from[1] == 'W' ? 0 : 1);\n int to_line = to[0] - '0';\n int to_dir = (to[1] == 'W' ? 0 : 1);\n\n t[from_line][from_dir].push_back(P(to_line, to_dir));\n t[to_line][to_dir].push_back(P(from_line, from_dir));\n }\n\n start.clear();\n end.clear();\n\n for(int i = 0; i < n; i++){\n string s;\n cin >> s;\n if(s == \"-\") s = \"\";\n start.push_back(s);\n }\n\n for(int i = 0; i < n; i++){\n string s;\n cin >> s;\n if(s == \"-\") s = \"\";\n end.push_back(s);\n }\n\n solve();\n }\n}", "accuracy": 1, "time_ms": 6370, "memory_kb": 24420, "score_of_the_acc": -1.0585, "final_rank": 12 }, { "submission_id": "aoj_1260_792289", "code_snippet": "#include <iostream>\n#include <map>\n#include <string>\n#include <vector>\n#include <algorithm>\n#include <queue>\n \nusing namespace std;\n \ntypedef pair<vector<string>, int> P;\n \nbool G[4][2][4][2];\nmap<vector<string>, int> dat, dat2;\nint n, m;\n \nvoid init(){\n fill(G[0][0][0], G[4][0][0], false);\n dat.clear();\n dat2.clear();\n}\n \nvoid bfs1(vector<string> &s){\n queue<P> que;\n dat[s] = 0;\n que.push(P(s, 0));\n while(!que.empty()){\n vector<string> u = que.front().first;\n int dist = que.front().second;\n que.pop();\n for(int i=0;i<n;i++){\n for(int j=-1;j<(int)u[i].size();j++){\n int p1 = i;\n for(int d1=0;d1<2;d1++){\n for(int p2=0;p2<n;p2++){\n for(int d2=0;d2<2;d2++){\n if(G[p1][d1][p2][d2]){\n vector<string> v;\n v.resize(n);\n string next;\n for(int k=0;k<n;k++){\n if(k == p1){\n if(d1 == 0){\n next = u[k].substr(j+1);\n v[k] = next;\n }else{\n next = u[k].substr(0, j+1);\n v[k] = next;\n }\n }else{\n v[k] = u[k];\n }\n }\n if(d1 == 0){\n if(d2 == 0){\n next = u[p1].substr(0, j+1);\n reverse(next.begin(), next.end()); \n v[p2] = next + v[p2];\n }else{\n next = u[p1].substr(0, j+1);\n v[p2] = v[p2] + next;\n }\n }else{\n if(d2 == 0){\n next = u[p1].substr(j+1);\n v[p2] = next + v[p2];\n }else{\n next = u[p1].substr(j+1);\n reverse(next.begin(), next.end()); \n v[p2] = v[p2] + next;\n }\n }\n if(dat.find(v) == dat.end()){\n dat[v] = dist + 1;\n if(dist + 1 < 2) que.push(P(v, dist + 1));\n }\n }\n }\n }\n }\n }\n }\n }\n}\n \nint bfs2(vector<string> &s){\n queue<P> que;\n dat2[s] = 0;\n que.push(P(s, 0));\n if(dat.find(s) != dat.end()) return dat[s];\n int res = 6;\n while(!que.empty()){\n vector<string> u = que.front().first;\n int dist = que.front().second;\n que.pop();\n if(dist >= 3) continue;\n for(int i=0;i<n;i++){\n for(int j=-1;j<(int)u[i].size();j++){\n int p1 = i;\n for(int d1=0;d1<2;d1++){\n for(int p2=0;p2<n;p2++){\n for(int d2=0;d2<2;d2++){\n if(G[p1][d1][p2][d2]){\n vector<string> v;\n v.resize(n);\n string next;\n for(int k=0;k<n;k++){\n if(k == p1){\n if(d1 == 0){\n next = u[k].substr(j+1);\n v[k] = next;\n }else{\n next = u[k].substr(0, j+1);\n v[k] = next;\n }\n }else{\n v[k] = u[k];\n }\n }\n if(d1 == 0){\n if(d2 == 0){\n next = u[p1].substr(0, j+1);\n reverse(next.begin(), next.end()); \n v[p2] = next + v[p2];\n }else{\n next = u[p1].substr(0, j+1);\n v[p2] = v[p2] + next;\n }\n }else{\n if(d2 == 0){\n next = u[p1].substr(j+1);\n v[p2] = next + v[p2];\n }else{\n next = u[p1].substr(j+1);\n reverse(next.begin(), next.end()); \n v[p2] = v[p2] + next;\n }\n }\n if(dat2.find(v) == dat2.end()){\n if(dat.find(v) != dat.end()){\n res = min(res, dist + 1 + dat[v]);\n continue;\n }\n dat2[v] = dist + 1;\n if(dist + 1 < 3) que.push(P(v, dist + 1));\n }\n }\n }\n }\n }\n }\n }\n }\n return res;\n}\n \nvector<string> cre(){\n vector<string> vec;\n for(int i=0;i<n;i++){\n string in;\n cin >> in;\n if(in == \"-\") in = \"\";\n vec.push_back(in);\n }\n return vec;\n}\n \nmain(){\n while(cin >> n >> m && (n|m)){\n init();\n for(int i=0;i<m;i++){\n string a, b;\n cin >> a >> b;\n int p1 = a[0] - '0';\n int d1 = (a[1] == 'W' ? 0 : 1);\n int p2 = b[0] - '0';\n int d2 = (b[1] == 'W' ? 0 : 1);\n G[p1][d1][p2][d2] = true;\n G[p2][d2][p1][d1] = true;\n }\n vector<string> s = cre();\n bfs1(s);\n vector<string> t = cre();\n cout << bfs2(t) << endl;\n }\n}", "accuracy": 1, "time_ms": 4270, "memory_kb": 41076, "score_of_the_acc": -0.9191, "final_rank": 10 }, { "submission_id": "aoj_1260_791583", "code_snippet": "#include<iostream>\n#include<string>\n#include<queue>\n#include<map>\n#define MAX 4\n#define LIMIT 6\n#define FORWARD 0\n#define BACK 1\n#define WEST 0\n#define EAST 1\n#define BUFFER 40\n\nusing namespace std;\n\nclass Load{\n public:\n char buffer[BUFFER];\n int head, tail, size;\n Load(){}\n Load( string str ){\n head = tail = BUFFER/2;\n size = 0;\n for ( int i = 0; i < str.size(); i++ ){\n buffer[tail++] = str[i];\n size++;\n }\n }\n \n void intoW( char ch ){ buffer[--head] = ch; size++; }\n void intoE( char ch ){ buffer[tail++] = ch; size++; }\n char fromW(){ char ch = buffer[head]; head++; size--; return ch; }\n char fromE(){ char ch = buffer[tail-1]; tail--; size--; return ch; }\n \n bool operator < ( const Load &l ) const {\n if ( size != l.size ) return size < l.size;\n for ( int i = head, j = l.head; i < tail; i++, j++ ){\n if ( buffer[i] == l.buffer[j] ) continue;\n return buffer[i] < l.buffer[j];\n }\n return false;\n }\n \n bool operator == ( const Load &l ) const {\n if ( size != l.size ) return false;\n for ( int i = head, j = l.head; i < tail; i++, j++ ){\n if ( buffer[i] != l.buffer[j] ) return false;\n }\n return true;\n }\n \n bool operator != ( const Load &l ) const {\n if ( size != l.size ) return true;\n for ( int i = head, j = l.head; i < tail; i++, j++ ){\n if ( buffer[i] != l.buffer[j] ) return true;\n }\n return false;\n }\n};\n\nclass Station{\n public:\n int nload;\n Load loads[MAX];\n int id;\n \n Station(){}\n Station( int nload): nload(nload){}\n \n bool operator < ( const Station &s ) const {\n for ( int i = 0; i < nload; i++ ){\n if ( loads[i] == s.loads[i] ) continue;\n return loads[i] < s.loads[i];\n }\n return false;\n }\n \n bool operator == ( const Station &s ) const {\n for ( int i = 0; i < nload; i++ ){\n if ( loads[i] != s.loads[i] ) return false;\n }\n return true;\n }\n};\n\n\nStation initial, goal;\nint nload;\nbool M[MAX][2][MAX][2]; \n\nmap<Station, int> BS; \nmap<Station, int> FS; \n\nStation getNext(Station v, int d, int s, int sd, int t, int td){\n int nd;\n \n if ( sd == WEST ){\n if ( td == WEST ){\n for ( int i = 0; i < d; i++ ){\n\tv.loads[t].intoW(v.loads[s].fromW());\n }\n } else {\n for ( int i = 0; i < d; i++ ){\n\tv.loads[t].intoE(v.loads[s].fromW());\n }\n }\n } else {\n if ( td == WEST ){\n nd = v.loads[s].size - d;\n for ( int i = 0; i < nd; i++ ){\n\tv.loads[t].intoW(v.loads[s].fromE());\n }\n } else {\n nd = v.loads[s].size - d;\n for ( int i = 0; i < nd; i++ ){\n\tv.loads[t].intoE(v.loads[s].fromE());\n }\n }\n }\n \n return v;\n}\n\nint bfs( Station source, map<Station, int> &D, int mode ){\n queue<Station> Q;\n Q.push(source);\n D[source] = 0;\n \n Station u, v;\n \n if ( mode == FORWARD && BS.find(source) != BS.end() ) return BS[source];\n \n while(!Q.empty()){\n u = Q.front(); Q.pop();\n int dist = D[u];\n \n if ( mode == BACK && dist >= 3 ) return 0;\n \n for ( int s = 0; s < nload; s++ ){\n for ( int d = 0; d < u.loads[s].size; d++ ){\n\tfor ( int t = 0; t < nload; t++ ){\n\t for ( int sd = 0; sd < 2; sd++ ){\n\t for ( int td = 0; td < 2; td++ ){\n\t if ( M[s][sd][t][td] ){\n\t\tv = getNext(u, d, s, sd, t, td );\n\t\tif ( D.find(v) == D.end() ){\n\t\t D[v] = dist + 1;\n\t\t if ( mode == FORWARD ){\n\t\t if ( BS.find(v) != BS.end() ) {\n\t\t return BS[v] + dist + 1;\n\t\t }\n\t\t } \n\t\t Q.push(v);\n\t\t}\n\t }\n\t }\n\t }\n\t}\n }\n }\n }\n return -1;\n}\n\nvoid compute(){\n BS = map<Station, int>();\n FS = map<Station, int>();\n bfs(goal, BS, BACK);\n cout << bfs(initial, FS, FORWARD) << endl;\n}\n\nbool input(){\n int y;\n cin >> nload >> y;\n if ( nload == 0 && y == 0 ) return false;\n \n for ( int i = 0; i < nload; i++ ){\n for ( int j = 0; j < nload; j++ ){\n M[i][WEST][j][WEST] = false;\n M[i][WEST][j][EAST] = false;\n M[i][EAST][j][WEST] = false;\n M[i][EAST][j][EAST] = false;\n }\n }\n int sl, tl; \n char sd, td; \n for ( int i = 0; i < y; i++ ){\n cin >> sl >> sd >> tl >> td;\n M[sl][(sd == 'W' ? WEST : EAST)][tl][(td == 'W' ? WEST : EAST)] = true;\n M[tl][(td == 'W' ? WEST : EAST)][sl][(sd == 'W' ? WEST : EAST)] = true;\n }\n \n initial = Station(nload);\n goal = Station(nload);\n \n string train;\n for ( int i = 0; i < nload; i++ ) {\n cin >> train; if ( train == \"-\" ) train = \"\";\n initial.loads[i] = Load(train);\n }\n for ( int i = 0; i < nload; i++ ) {\n cin >> train; if ( train == \"-\" ) train = \"\";\n goal.loads[i] = Load(train);\n }\n \n return true;\n}\n\nmain(){\n while( input() ) compute();\n}", "accuracy": 1, "time_ms": 1610, "memory_kb": 75556, "score_of_the_acc": -0.8966, "final_rank": 9 }, { "submission_id": "aoj_1260_506368", "code_snippet": "#include <cstdio>\n#include <iostream>\n#include <sstream>\n#include <iomanip>\n#include <algorithm>\n#include <cmath>\n#include <string>\n#include <vector>\n#include <list>\n#include <queue>\n#include <stack>\n#include <set>\n#include <map>\n#include <bitset>\n#include <numeric>\n#include <climits>\n#include <cfloat>\nusing namespace std;\n\nint n;\nvector<vector<int> > to;\n\nvoid solve(vector<string> start, map<vector<string>, int>& history)\n{\n queue<vector<string> > q1;\n q1.push(start);\n history[start] = 0;\n\n for(int a=1; a<=3; ++a){\n queue<vector<string> > q2;\n while(!q1.empty()){\n vector<string> s = q1.front();\n q1.pop();\n\n for(int i=0; i<n*2; ++i){\n for(unsigned j=0; j<to[i].size(); ++j){\n vector<string> t = s;\n while(!t[i/2].empty()){\n char c;\n if(i % 2 == 0){\n c = t[i/2][0];\n t[i/2] = t[i/2].substr(1);\n }else{\n c = t[i/2][t[i/2].size()-1];\n t[i/2] = t[i/2].substr(0, t[i/2].size()-1);\n }\n\n if(to[i][j] % 2 == 0){\n t[to[i][j]/2] = c + t[to[i][j]/2];\n }else{\n t[to[i][j]/2] += c;\n }\n\n if(history.find(t) == history.end()){\n q2.push(t);\n history[t] = a;\n }\n }\n }\n }\n }\n q1 = q2;\n }\n}\n\nint main()\n{\n for(;;){\n int m;\n cin >> n >> m;\n if(n == 0)\n return 0;\n\n to.assign(2*n, vector<int>());\n for(int i=0; i<m; ++i){\n string s, t;\n cin >> s >> t;\n int a = (s[0] - '0') * 2;\n int b = (t[0] - '0') * 2;\n if(s[1] == 'E')\n ++ a;\n if(t[1] == 'E')\n ++ b;\n to[a].push_back(b);\n to[b].push_back(a);\n }\n\n vector<string> s(n), t(n);\n for(int i=0; i<n; ++i){\n cin >> s[i];\n if(s[i] == \"-\")\n s[i] = \"\";\n }\n for(int i=0; i<n; ++i){\n cin >> t[i];\n if(t[i] == \"-\")\n t[i] = \"\";\n }\n\n map<vector<string>, int> history1, history2;\n solve(t, history2);\n solve(s, history1);\n\n int ret = INT_MAX;\n map<vector<string>, int>::iterator it1, it2;\n it1 = history1.begin();\n it2 = history2.begin();\n while(it1 != history1.end() && it2 != history2.end()){\n if(it1->first == it2->first)\n ret = min(ret, it1->second + it2->second);\n if(it1->first < it2->first)\n ++ it1;\n else\n ++ it2;\n }\n\n cout << ret << endl;\n }\n}", "accuracy": 1, "time_ms": 7430, "memory_kb": 91652, "score_of_the_acc": -2, "final_rank": 15 }, { "submission_id": "aoj_1260_452240", "code_snippet": "#include <cstdio>\n#include <cstring>\n#include <cstdlib>\n#include <algorithm>\n#include <string>\n#include <vector>\n#include <queue>\n\nusing namespace std;\n\ntemplate <class Key, class Value>\nstruct HashMap {\n\n static const int MAXH = 999983;\n struct Edge {\n Key to;\n Value v;\n Edge *next;\n } *head[MAXH];\n\n void set(const Key &key, const Value &value) {\n\n int hv = (unsigned int)key.hash_value() % MAXH;\n Edge *p = new Edge;\n p -> to = key;\n p -> v = value;\n p -> next = head[hv];\n head[hv] = p;\n }\n\n const Value &get(const Key &key) {\n\n int hv = (unsigned int)key.hash_value() % MAXH;\n for (Edge *p = head[hv]; p; p = p -> next)\n if (p -> to == key) return p -> v;\n }\n\n bool exist(const Key &key) {\n\n int hv = (unsigned int)key.hash_value() % MAXH;\n for (Edge *p = head[hv]; p; p = p -> next)\n if (p -> to == key) return true;\n return false;\n }\n\n void clear() {\n\n for (int i = 0; i < MAXH; i++)\n {\n for (Edge *p = head[i], *np; p; p = np)\n {\n np = p -> next;\n delete p;\n }\n head[i] = NULL;\n }\n }\n};\n\nconst int MAXN = 4;\nconst int MAXM = MAXN * 2 * MAXN * 2 * 2;\nint n, m, mcnt;\nint track[MAXM][2], dir[MAXM][2];\n\nstruct State {\n\n vector<string> s;\n\n string &operator[](int x) {\n\n return s[x];\n }\n\n int hash_value() const {\n\n unsigned int res = 0, res0;\n for (int i = 0; i < n; i++)\n {\n res0 = 0;\n const string &ref = s[i];\n string::size_type len = ref.length();\n for (int j = 0; j < len; j++)\n res0 = res0 * 131 + ref[j];\n res = res * 13131 + res0;\n }\n return res;\n }\n\n bool operator==(const State &b) const {\n\n for (int i = 0; i < n; i++)\n if (s[i] != b.s[i]) return false;\n return true;\n }\n\n void go(int n0, int d0, int n1, int d1, int k) {\n\n string out = s[n0].substr(0, k);\n s[n0] = s[n0].erase(0, k);\n if (d0) std::swap(out, s[n0]);\n if (d0 == d1) reverse(out.begin(), out.end());\n if (d1) s[n1] += out;\n else s[n1] = out + s[n1];\n }\n\n void push_back(const string &b) {\n\n s.push_back(b);\n }\n};\n\nstruct Bfs {\n\n queue<State> q[2], *old, *new_;\n HashMap<State, int> hash;\n int head, tail;\n Bfs *pair;\n\n void init() {\n\n hash.clear();\n for (int i = 0; i < 2; i++)\n while (!q[i].empty()) q[i].pop();\n old = q;\n new_ = q + 1;\n }\n\n int push(const State &ns, int step) {\n\n if (hash.exist(ns)) return -1;\n if (pair -> hash.exist(ns))\n {\n int t = pair -> hash.get(ns);\n return step + t; \n }\n hash.set(ns, step);\n new_ -> push(ns);\n return -1;\n }\n\n int next() {\n\n while (!old -> empty())\n {\n State &ref = old -> front();\n int nstep = hash.get(ref) + 1;\n for (int i = 0; i < mcnt; i++)\n {\n string::size_type len = ref[track[i][0]].length();\n for (int k = 0; k <= len; k++)\n {\n State v = ref;\n v.go(track[i][0], dir[i][0], track[i][1], dir[i][1], k);\n int ret = push(v, nstep);\n if (ret != -1) return ret;\n }\n }\n old -> pop();\n }\n swap(old, new_);\n return -1;\n }\n};\n\nBfs ins[2];\n\nint main() {\n\n\n for (;;)\n {\n mcnt = 0;\n scanf(\"%d %d\", &n, &m);\n if (!n && !m) break;\n for (int i = 0; i < m; i++)\n {\n int f, t;\n char fd, td;\n scanf(\"%d%c %d%c\", &f, &fd, &t, &td);\n fd = fd == 'E';\n td = td == 'E';\n track[mcnt][0] = f; track[mcnt][1] = t;\n dir[mcnt][0] = fd; dir[mcnt][1] = td;\n mcnt++;\n\n track[mcnt][0] = t; track[mcnt][1] = f;\n dir[mcnt][0] = td; dir[mcnt][1] = fd;\n mcnt++;\n }\n\n State root[2];\n static char buff[20];\n\n for (int t = 0; t < 2; t++)\n for (int i = 0; i < n; i++)\n {\n scanf(\"%s\", buff);\n root[t].push_back(buff);\n if (root[t][i] == \"-\") root[t][i] = \"\";\n }\n\n for (int i = 0; i < 2; i++) \n {\n ins[i].init();\n ins[i].pair = ins + !i;\n swap(ins[i].old, ins[i].new_);\n ins[i].push(root[i], 0);\n }\n for (int i = 0; i < 2; i++)\n swap(ins[i].old, ins[i].new_);\n\n for (int i = 0;; i ^= 1)\n {\n int ret = ins[i].next();\n if (ret != -1) \n {\n printf(\"%d\\n\", ret);\n break;\n }\n }\n }\n\n return 0;\n}", "accuracy": 1, "time_ms": 1790, "memory_kb": 30000, "score_of_the_acc": -0.4003, "final_rank": 4 }, { "submission_id": "aoj_1260_452237", "code_snippet": "#include <cstdio>\n#include <cstring>\n#include <cstdlib>\n#include <algorithm>\n#include <string>\n#include <vector>\n#include <queue>\n\nusing namespace std;\n\ntemplate <class Key, class Value>\nstruct HashMap {\n\n static const int MAXH = 999983;\n struct Edge {\n Key to;\n Value v;\n Edge *next;\n } *head[MAXH];\n\n void set(const Key &key, const Value &value) {\n\n int hv = (unsigned int)key.hash_value() % MAXH;\n Edge *p = new Edge;\n p -> to = key;\n p -> v = value;\n p -> next = head[hv];\n head[hv] = p;\n }\n\n const Value &get(const Key &key) {\n\n int hv = (unsigned int)key.hash_value() % MAXH;\n for (Edge *p = head[hv]; p; p = p -> next)\n if (p -> to == key) return p -> v;\n }\n\n bool exist(const Key &key) {\n\n int hv = (unsigned int)key.hash_value() % MAXH;\n for (Edge *p = head[hv]; p; p = p -> next)\n if (p -> to == key) return true;\n return false;\n }\n\n void clear() {\n\n for (int i = 0; i < MAXH; i++)\n {\n for (Edge *p = head[i], *np; p; p = np)\n {\n np = p -> next;\n delete p;\n }\n head[i] = NULL;\n }\n }\n};\n\nconst int MAXN = 4;\nconst int MAXM = MAXN * MAXN * 2;\nint n, m, mcnt;\nbool trans[MAXN][2][MAXN][2];\n//int track[MAXM][2], dir[MAXM][2];\n\nstruct State {\n\n vector<string> s;\n\n string &operator[](int x) {\n\n return s[x];\n }\n\n int hash_value() const {\n\n unsigned int res = 0, res0;\n for (int i = 0; i < n; i++)\n {\n res0 = 0;\n const string &ref = s[i];\n string::size_type len = ref.length();\n for (int j = 0; j < len; j++)\n res0 = res0 * 131 + ref[j];\n res = res * 13131 + res0;\n }\n return res;\n }\n\n bool operator==(const State &b) const {\n\n for (int i = 0; i < n; i++)\n if (s[i] != b.s[i]) return false;\n return true;\n }\n\n void go(int n0, int d0, int n1, int d1, int k) {\n\n string out = s[n0].substr(0, k);\n s[n0] = s[n0].erase(0, k);\n if (d0) std::swap(out, s[n0]);\n if (d0 == d1) reverse(out.begin(), out.end());\n if (d1) s[n1] += out;\n else s[n1] = out + s[n1];\n }\n\n void push_back(const string &b) {\n\n s.push_back(b);\n }\n};\n\nstruct Bfs {\n\n queue<State> q[2], *old, *new_;\n HashMap<State, int> hash;\n int head, tail;\n Bfs *pair;\n\n void init() {\n\n hash.clear();\n for (int i = 0; i < 2; i++)\n while (!q[i].empty()) q[i].pop();\n old = q;\n new_ = q + 1;\n }\n\n int push(const State &ns, int step) {\n\n if (hash.exist(ns)) return -1;\n if (pair -> hash.exist(ns))\n {\n int t = pair -> hash.get(ns);\n return step + t; \n }\n hash.set(ns, step);\n new_ -> push(ns);\n return -1;\n }\n\n int next() {\n\n while (!old -> empty())\n {\n State &ref = old -> front();\n int nstep = hash.get(ref) + 1;\n // for (int i = 0; i < mcnt; i++)\n for (int f = 0; f < n; f++)\n for (int t = 0; t < n; t++)\n for (int fd = 0; fd < 2; fd++)\n for (int td = 0; td < 2; td++)\n if (trans[f][fd][t][td])\n {\n string::size_type len = ref[f].length();\n for (int k = 0; k <= len; k++)\n {\n State v = ref;\n // v.go(track[i][0], dir[i][0], track[i][1], dir[i][1], k);\n v.go(f, fd, t, td, k);\n int ret = push(v, nstep);\n if (ret != -1) return ret;\n }\n }\n old -> pop();\n }\n swap(old, new_);\n return -1;\n }\n};\n\nBfs ins[2];\n\nint main() {\n\n\n for (;;)\n {\n mcnt = 0;\n scanf(\"%d %d\", &n, &m);\n if (!n && !m) break;\n memset(trans, 0, sizeof trans);\n for (int i = 0; i < m; i++)\n {\n int f, t;\n char fd, td;\n scanf(\"%d%c %d%c\", &f, &fd, &t, &td);\n fd = fd == 'E';\n td = td == 'E';\n/* track[mcnt][0] = f; track[mcnt][1] = t;\n dir[mcnt][0] = fd; dir[mcnt][1] = td;\n mcnt++;\n\n track[mcnt][0] = t; track[mcnt][1] = f;\n dir[mcnt][0] = td; dir[mcnt][1] = fd;\n mcnt++;\n */\n trans[f][fd][t][td] = true;\n trans[t][td][f][fd] = true;\n }\n\n State root[2];\n static char buff[20];\n\n for (int t = 0; t < 2; t++)\n for (int i = 0; i < n; i++)\n {\n scanf(\"%s\", buff);\n root[t].push_back(buff);\n if (root[t][i] == \"-\") root[t][i] = \"\";\n }\n\n for (int i = 0; i < 2; i++) \n {\n ins[i].init();\n ins[i].pair = ins + !i;\n swap(ins[i].old, ins[i].new_);\n ins[i].push(root[i], 0);\n }\n for (int i = 0; i < 2; i++)\n swap(ins[i].old, ins[i].new_);\n\n for (int i = 0;; i ^= 1)\n {\n int ret = ins[i].next();\n if (ret != -1) \n {\n printf(\"%d\\n\", ret);\n break;\n }\n }\n }\n\n return 0;\n}", "accuracy": 1, "time_ms": 1720, "memory_kb": 28000, "score_of_the_acc": -0.3663, "final_rank": 3 }, { "submission_id": "aoj_1260_97025", "code_snippet": "#include<iostream>\n#include<queue>\n#include<string>\n#include<algorithm>\n#include<set>\nusing namespace std;\n#define REP(i,b,n) for(int i=b;i<n;i++)\n#define rep(i,n) REP(i,0,n)\n#define ALL(C) (C).begin(),(C).end()\n#define WEST 0\n#define EAST 1\n\ntypedef unsigned long long ull;\n\nclass state{\npublic:\n string data[4];\n ull h[4];\n int cnt;\n state(){\n cnt=0;\n rep(i,4){data[i]=\"\";h[i]=0;}\n }\n bool operator<(const state &a)const{\n rep(i,4)if (h[i] != a.h[i])return h[i] < a.h[i];\n return false;\n }\n bool operator==(const state &a)const{\n rep(i,4)if (h[i] != a.h[i])return false;\n return true;\n }\n};\n\nbool edge[4][4][2][2];//from to, from w or e, to w or e\n\nconst ull xxx=10007;\null compute_hash(string &a){\n ull ret=0;\n rep(i,a.size()){\n ret=ret*xxx + a[i];\n }\n return ret;\n}\n\ninline void get_all_hash(state &a,int n){\n rep(i,n){\n a.h[i]=compute_hash(a.data[i]);\n }\n}\n\ninline void operate(string moveone,string stayone,\n\t\t state &now,int me,int tar,\n\t\t int dir,int tdir){\n if (dir == tdir){\n reverse(moveone.begin(),moveone.end());\n }\n if (tdir == EAST){\n now.data[tar]+=moveone;\n }else if (tdir == WEST){\\\n now.data[tar]=moveone+now.data[tar];\n }\n now.data[me]=stayone;\n}\n\nvoid move(int n,state &now,set<state> & S,queue<state> &Q){\n rep(i,n){\n rep(j,now.data[i].size()+1){\n string tmp[2]={now.data[i].substr(0,j),now.data[i].substr(j)};\n rep(k,2){//which tmp[0] or tmp[1]\n\trep(l,n){//target line\n\t rep(m,2){//west or east\n\t if (!edge[i][l][k][m])continue;\n\t state next=now;\n\t next.cnt++;\n\t operate(tmp[k],tmp[1-k],next,i,l,k,m);\n\t get_all_hash(next,n);\n\t if (S.find(next) == S.end()){\n\t S.insert(next);\n\t Q.push(next);\n\t }\n\t }\n\t}\n }\n }\n } \n}\n\nint bfs(int n,state ini,state goal,set<state> &G,bool fromstart){\n set<state> S;\n queue<state> Q;\n if (fromstart){\n Q.push(ini);\n S.insert(ini);\n }else {\n Q.push(goal);\n G.insert(goal);\n }\n\n while(!Q.empty()){\n state now = Q.front();\n Q.pop();\n /*\n cout <<\"debug \"<< fromstart << endl;\n rep(i,n){\n cout << now.data[i] << endl;\n }\n */\n\n if (fromstart){\n set<state>::iterator itr = G.find(now);\n if (itr != G.end())return now.cnt+(*itr).cnt;\n }else {\n if (now == ini)return now.cnt;\n }\n\n if (now.cnt ==3)continue;\n if (fromstart)move(n,now,S,Q);\n else move(n,now,G,Q);\n }\n return -1;\n}\n\nint solve(int n,state &ini,state &goal){\n set<state> Goal;\n int ans = bfs(n,ini,goal,Goal,false);\n if (ans != -1)return ans;\n ans=bfs(n,ini,goal,Goal,true);\n return ans;\n}\n\nmain(){\n int n,m;\n while(cin>>n>>m&&n){\n rep(i,4)rep(j,4)rep(k,2)rep(l,2)edge[i][j][k][l]=false;\n rep(i,m){\n char tfd,ttd;\n int f,t;\n int fd,td;\n cin>>f>>tfd>>t>>ttd;\n if (tfd == 'W')fd=WEST;\n else fd=EAST;\n if (ttd == 'W')td=WEST;\n else td=EAST;\n edge[f][t][fd][td]=true;\n edge[t][f][td][fd]=true;\n }\n state ini,goal;\n rep(i,n){\n cin>>ini.data[i];\n if (ini.data[i] == \"-\")ini.data[i]=\"\";\n }\n rep(i,n){\n cin>>goal.data[i];\n if (goal.data[i] == \"-\")goal.data[i]=\"\";\n }\n get_all_hash(ini,n);\n get_all_hash(goal,n);\n \n cout << solve(n,ini,goal) << endl;\n }\n}", "accuracy": 1, "time_ms": 6630, "memory_kb": 69000, "score_of_the_acc": -1.6129, "final_rank": 14 } ]
aoj_1261_cpp
Problem E: Mobile Computing There is a mysterious planet called Yaen, whose space is 2-dimensional. There are many beautiful stones on the planet, and the Yaen people love to collect them. They bring the stones back home and make nice mobile arts of them to decorate their 2-dimensional living rooms. In their 2-dimensional world, a mobile is defined recursively as follows: a stone hung by a string, or a rod of length 1 with two sub-mobiles at both ends; the rod is hung by a string at the center of gravity of sub-mobiles. When the weights of the sub-mobiles are n and m , and their distances from the center of gravity are a and b respectively, the equation n × a = m × b holds. For example, if you got three stones with weights 1, 1, and 2, here are some possible mobiles and their widths: Given the weights of stones and the width of the room, your task is to design the widest possible mobile satisfying both of the following conditions. It uses all the stones. Its width is less than the width of the room. You should ignore the widths of stones. In some cases two sub-mobiles hung from both ends of a rod might overlap (see the figure on the below). Such mobiles are acceptable. The width of the example is (1/3) + 1 + (1/4). Input The first line of the input gives the number of datasets. Then the specified number of datasets follow. A dataset has the following format. r s w 1 . . . w s r is a decimal fraction representing the width of the room, which satisfies 0 < r < 10. s is the number of the stones. You may assume 1 ≤ s ≤ 6. w i is the weight of the i -th stone, which is an integer. You may assume 1 ≤ w i ≤ 1000. You can assume that no mobiles whose widths are between r - 0.00001 and r + 0.00001 can be made of given stones. Output For each dataset in the input, one line containing a decimal fraction should be output. The decimal fraction should give the width of the widest possible mobile as defined above. An output line should not contain extra characters such as spaces. In case there is no mobile which satisfies the requirement, answer -1 instead. The answer should not have an error greater than 0.00000001. You may output any number of digits after the decimal point, provided that the above accuracy condition is satisfied. Sample Input 5 1.3 3 1 2 1 1.4 3 1 2 1 2.0 3 1 2 1 1.59 4 2 1 1 3 1.7143 4 1 2 3 5 Output for the Sample Input -1 1.3333333333333335 1.6666666666666667 1.5833333333333335 1.7142857142857142
[ { "submission_id": "aoj_1261_4673799", "code_snippet": "#include <cstdio>\n#include <cstring>\n#include <algorithm>\n#include <iostream>\n#include <cmath>\n#include <vector>\nusing namespace std;\n\nconst int N = 7, M = 1 << N;\nint tr[M]; //-1: root, 0: no use, else:left value\nint value[M];\nint sumvalue[M];\ndouble _left[M], _right[M], cur[M]; //具体的位置\nbool st[N];\ndouble r, ans; int s;\ndouble _r;\nint q[N];\nvector<double> que;\n\nvoid dfs_value(int u){ //dfs求得左右子树的value\n if(tr[u] != -1){\n value[u] = tr[u];\n return;\n }\n dfs_value(u * 2);\n dfs_value(u * 2 + 1);\n value[u] = value[u * 2] + value[u * 2 + 1];\n}\n\n\nvoid update(){ //dfs进行更新ans\n int cnt = 1;\n int mark = 1;\n\n\n\n memcpy(value, tr, sizeof value);\n dfs_value(1);\n\n int u;\n double res = 0.0, t = 0.0;\n\n // 一路向左 加 一路向右\n double l = 0, r = 0;\n cur[1] = 0;\n for(int i = 1; i <= M; i++){\n if(tr[i] == -1){\n int x = 2 * i, y = 2 * i + 1;\n _left[i] = double(value[y]) / (value[x] + value[y]);\n _right[i] = double(value[x]) / (value[x] + value[y]);\n cur[x] = cur[i] - _left[i];\n cur[y] = cur[i] + _right[i];\n l = min(cur[x], l);\n r = max(cur[y], r);\n }\n }\n\n\n res = (r - l);\n if(res <= _r + 0.00000001){\n ans = max(ans, res);\n }\n\n cnt = 1;\n mark = 1;\n\n //cout<<\"RES : \"<<res<<endl;\n //que.push_back(res);\n /*printf(\"T : %0.16f\\n\", r - l);\n printf(\"RES: %0.16f\\n\", res);\n cout<<\"*************************\\n\";\n for(int i = 1; i < (2 << s); i++){\n printf(\"%d \", tr[i]);\n if(mark == i){\n cnt *= 2;\n mark += cnt;\n cout<<endl;\n }\n }\n cout<<\"*************************\\n\";\n cnt = 1;\n mark = 1;\n cout<<\"*************************\\n\";\n for(int i = 1; i < (2 << s); i++){\n printf(\"%d \", value[i]);\n if(mark == i){\n cnt *= 2;\n mark += cnt;\n cout<<endl;\n }\n }\n cout<<\"*************************\\n\";*/\n\n}\n\n// 二叉树的线性便利\nvoid dfs(int u, int surp, int need){ //need: 是现在需要的, surp 是剩余的\n if(need < surp) return;\n //if(u >= (2 << s)) return;\n //printf(\"U:%d, SURP:%d, NEED:%d\\n\", u, surp, need);\n if(need == 0){ //递归结束,填坑完毕\n //调用更新函数\n update();\n //cout<<\"*************UPDATE*************\"<<endl;\n return;\n }\n\n if(tr[u / 2] != -1){ //这个点不可以,下一个\n dfs(u + 1, surp, need);\n return;\n }\n\n\n if(surp < need){ //可以加一个杠\n int tmp = tr[u];\n tr[u] = -1;\n dfs(u + 1, surp + 1, need);\n tr[u] = tmp;\n }\n\n if(surp == 1 && need > 1) return; //一定要注意这一个,只剩一下一个位置,不可以放东西了,只允许加杠\n\n //不加杠\n for(int i = 1; i <= s; i++){\n if(st[i]) continue;\n st[i] = true;\n int tmp = tr[u];\n tr[u] = q[i];\n dfs(u + 1, surp - 1, need - 1);\n st[i] = false;\n tr[u] = tmp;\n }\n return;\n}\n\nint main()\n{\n int T; cin>>T;\n\n while(T--){\n cin>>r>>s;\n _r = r;\n for(int i = 1; i <= s; i++) scanf(\"%d\", &q[i]);\n if(s == 1){\n if(0.0 <= r + 0.0000001){\n printf(\"%0.16f\\n\", 0.0);\n }else{\n cout<<-1<<endl;\n }\n continue;\n }\n\n memset(tr, 0, sizeof tr); memset(st, false, sizeof st);\n ans = -1;\n tr[1] = -1;\n dfs(2, 2, s);\n\n /*for(int i = 0; i < que.size(); i++){\n printf(\"%0.6f\\n\", que[i]);\n }*/\n //printf(\"Ans: %0.6lf\\n\", ans);\n\n\n if(ans >= -0.001){\n printf(\"%0.16f\\n\", ans);\n }else{\n cout<<-1<<endl;\n }\n\n }\n return 0;\n}", "accuracy": 1, "time_ms": 120, "memory_kb": 3276, "score_of_the_acc": -0.3368, "final_rank": 12 }, { "submission_id": "aoj_1261_3842617", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\ntypedef long long ll;\nconst int maxn = 105;\nconst double eps = 1e-5;\nint s,w[maxn],t[maxn];\ndouble r;\nbool vis[maxn];\ndouble ans;\ndouble li[maxn],ri[maxn],val[maxn];\nvoid init() {\n\tmemset(vis,false,sizeof(vis));\n\tmemset(w,0,sizeof(w));\n\tmemset(t,0,sizeof(t));\n}\nvoid cal(int num) {\n\tmemset(li,0,sizeof(li));\n\tmemset(ri,0,sizeof(ri));\n\tmemset(val,0,sizeof(val));\n\tfor(int i = num - 1;i >= 1;i--) {\n\t\tif(t[i] == -1) {\n\t\t\tval[i] = val[2*i] + val[2*i+1];\n\t\t\tdouble L = val[2*i+1]/val[i],R = val[2*i]/val[i];\n\t\t\tli[i] = min(li[2*i]-L,li[2*i+1]+R);\n\t\t\tri[i] = max(ri[2*i+1]+R,ri[2*i]-L);\n\t\t}\n\t\telse if(t[i]) val[i] = w[t[i]];\n\t}\n\tdouble temp = ri[1] - li[1];\n\tif(temp - r < eps && temp > ans) ans = temp;\n}\nvoid dfs(int cur,int cnt,int now) {\n\tif(cnt == s) {\n\t\tcal(cur);\n\t\treturn;\t\n\t}\n\tif(t[cur/2] != -1) dfs(cur+1,cnt,now);\n\telse {\n\t\tif(s - cnt > now) {\n\t\t\tt[cur] = -1;\n\t\t\tdfs(cur+1,cnt,now+1);\n\t\t\tt[cur] = 0;\n\t\t}\n\t\tif(now == 1 && s - cnt > now) return; \n\t\tfor(int i = 1;i <= s;i++) {\n\t\t\tif(!vis[i]) {\n\t\t\t\tvis[i] = true;\n\t\t\t\tt[cur] = i;\n\t\t\t\tdfs(cur+1,cnt+1,now-1);\n\t\t\t\tvis[i] = false;\n\t\t\t\tt[cur] = 0;\n\t\t\t}\n\t\t}\n\t}\n}\nint main() {\n\tios::sync_with_stdio(false);\n\tcin.tie(0);\n\tint T;\n\tscanf(\"%d\",&T);\n\twhile(T--) {\n\t\tscanf(\"%lf %d\",&r,&s);\n\t\tinit();\n\t\tfor(int i = 1;i <= s;i++) scanf(\"%d\",&w[i]);\n\t\tif(s == 1) {printf(\"0.00\\n\");continue;}\n\t\tans = -1;\n\t\tt[1] = -1;\n\t\tdfs(2,0,2);\n\t\tif(ans == -1) printf(\"-1\\n\");\n\t\telse printf(\"%.10lf\\n\",ans);\n\t}\n\treturn 0;\n}", "accuracy": 1, "time_ms": 100, "memory_kb": 3200, "score_of_the_acc": -0.3216, "final_rank": 9 }, { "submission_id": "aoj_1261_3842616", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\ntypedef long long ll;\nconst int maxn = 105;\nconst double eps = 1e-5;\nint s,t[maxn];\ndouble r,w[maxn];\nbool vis[maxn];\ndouble ans;\ndouble li[maxn],ri[maxn],val[maxn];\nvoid init() {\n\tmemset(vis,false,sizeof(vis));\n\tmemset(w,0,sizeof(w));\n\tmemset(t,0,sizeof(t));\n}\nvoid cal(int num) {\n\tmemset(li,0,sizeof(li));\n\tmemset(ri,0,sizeof(ri));\n\tmemset(val,0,sizeof(val));\n\tfor(int i = num - 1;i;i--) {\n\t\tif(t[i] == -1) {\n\t\t\tval[i] = val[2*i] + val[2*i+1];\n\t\t\tdouble L = val[2*i+1]/val[i],R = val[2*i]/val[i];\n\t\t\tli[i] = min(li[2*i]-L,li[2*i+1]+R);\n\t\t\tri[i] = max(ri[2*i+1]+R,ri[2*i]-L);\n\t\t}\n\t\telse if(t[i]) val[i] = w[t[i]];\n\t}\n\tdouble temp = ri[1] - li[1];\n\tif(temp - r < eps && temp > ans) ans = temp;\n}\nvoid dfs(int cur,int cnt,int now) {\n\tif(cnt == 0) {\n\t\tcal(cur);\n\t\treturn;\t\n\t}\n\tif(t[cur/2] != -1) dfs(cur+1,cnt,now);\n\telse {\n\t\tif(cnt > now) {\n\t\t\tt[cur] = -1;\n\t\t\tdfs(cur+1,cnt,now+1);\n\t\t\tt[cur] = 0;\n\t\t}\n\t\tif(now == 1 && cnt > 1) return; \n\t\tfor(int i = 1;i <= s;i++) {\n\t\t\tif(!vis[i]) {\n\t\t\t\tvis[i] = true;\n\t\t\t\tt[cur] = i;\n\t\t\t\tdfs(cur+1,cnt-1,now-1);\n\t\t\t\tvis[i] = false;\n\t\t\t\tt[cur] = 0;\n\t\t\t}\n\t\t}\n\t}\n}\nint main() {\n\tios::sync_with_stdio(false);\n\tcin.tie(0);\n\tint T;\n\tscanf(\"%d\",&T);\n\twhile(T--) {\n\t\tscanf(\"%lf %d\",&r,&s);\n\t\tinit();\n\t\tfor(int i = 1;i <= s;i++) scanf(\"%lf\",&w[i]);\n\t\tif(s == 1) {printf(\"%.10lf\\n\", 0.0);continue;}\n\t\tans = -1;\n\t\tt[1] = -1;\n\t\tdfs(2,s,2);\n\t\tif(ans == -1) printf(\"-1\\n\");\n\t\telse printf(\"%.10lf\\n\",ans);\n\t}\n\treturn 0;\n}", "accuracy": 1, "time_ms": 90, "memory_kb": 3228, "score_of_the_acc": -0.3245, "final_rank": 10 }, { "submission_id": "aoj_1261_3842571", "code_snippet": "#include <stdio.h>\n#include <string.h>\n#include <algorithm>\n \nusing namespace std;\nconst int N = 10;\nconst int M = 105;\nconst double INF = 0x3f3f3f3f;\n \nint n, v[N], t[M];\ndouble R, w[N], ans, l[M], r[M], val[M];\n \nvoid init () {\n\tmemset(t, 0, sizeof(t));\n\tmemset(v, 0, sizeof(v));\n\tt[1] = -1; ans = -1;\n \n\tscanf(\"%lf%d\", &R, &n);\n\tfor (int i = 1; i <= n; i++) scanf(\"%lf\", &w[i]);\n}\n \nvoid judge(int u) {\n\tmemset(l, 0, sizeof(l));\n\tmemset(r, 0, sizeof(r));\n\tmemset(val, 0, sizeof(val));\n \n\tfor (int i = u; i; i--) {\n\t\tif (t[i] == -1) {\n\t\t\tint x = i*2, y = i*2+1;\n\t\t\tval[i] = val[x] + val[y];\n\t\t\tdouble Li = val[y] / val[i];\n\t\t\tdouble Ri = val[x] / val[i];\n \n\t\t\tl[i] = min(-Li+l[x], Ri+l[y]);\n\t\t\tr[i] = max(-Li+r[x], Ri+r[y]);\n\t\t} else if (t[i]) {\n\t\t\tval[i] = w[t[i]];\n\t\t}\n\t}\n \n\tdouble tmp = r[1] - l[1];\n\tif (tmp - R < 1e-5 && tmp > ans) ans = tmp;\n}\n \nvoid dfs(int u, int m, int use) {\n \n\tif (use == 0) {\n\t\tjudge(u-1);\n\t\treturn;\n\t}\n \n\tif (t[u/2] != -1) {\n\t\tdfs(u+1, m, use);\n\t} else {\n \n\t\tif (use > m) {\n\t\t\tt[u] = -1;\n\t\t\tdfs(u+1, m+1, use);\n\t\t\tt[u] = 0;\n\t\t}\n \n\t\tif (m == 1 && use > 1) return;\n \n\t\tfor (int i = 1; i <= n; i++) if (!v[i]) {\n\t\t\tv[i] = 1; t[u] = i;\n\t\t\tdfs(u+1, m-1, use-1);\n\t\t\tv[i] = 0; t[u] = 0;\n\t\t}\n\t}\n}\n \nint main () {\n\tint cas;\n\tscanf(\"%d\", &cas);\n\twhile (cas--) {\n\t\tinit();\n\t\tif (n == 1) printf(\"%.10lf\\n\", 0.0);\t\n\t\telse {\n\t\t\tdfs(2, 2, n);\n\t\t\tif (ans == -1) printf(\"-1\\n\");\n\t\t\telse printf(\"%.10lf\\n\", ans);\n\t\t}\n\t}\n\treturn 0;\n}", "accuracy": 1, "time_ms": 100, "memory_kb": 2556, "score_of_the_acc": -0.2191, "final_rank": 5 }, { "submission_id": "aoj_1261_3161562", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\nusing Int = long long;\n\ntemplate<typename T1,typename T2> void chmin(T1 &a,T2 b){if(a>b) a=b;};\ntemplate<typename T1,typename T2> void chmax(T1 &a,T2 b){if(a<b) a=b;};\n\nsigned main(){\n Int T;\n cin>>T;\n\n cout<<fixed<<setprecision(12);\n while(T--){\n double width;\n Int n;\n cin>>width>>n;\n vector<double> w(n);\n for(Int i=0;i<n;i++) cin>>w[i];\n\n using P = pair<double, double>;\n Int s=1<<n;\n vector<double> sum(s,0);\n for(Int b=0;b<s;b++)\n for(Int i=0;i<n;i++)\n\tif((b>>i)&1) sum[b]+=w[i];\n\n function<vector<P>(Int, double)> dfs=[&](Int b,double pos){\n vector<P> res; \n if(__builtin_popcount(b)==1){\n\tres.emplace_back(pos,pos);\n\treturn res;\n }\n for(Int nb=b;nb;nb=(nb-1)&b){\n\tif(nb==b) continue;\n\tassert(nb&&(nb^b));\n\tdouble x=sum[nb],y=sum[nb^b];\n\tdouble aa=1.0/(1.0+x/y);\n\tdouble bb=1.0-aa;\n\tauto l=dfs(nb,pos-aa);\n\tauto r=dfs(nb^b,pos+bb);\n\tfor(auto p:l) \n\t for(auto q:r)\n\t res.emplace_back(min({p.first,q.first,p.second,q.second}),\n\t\t\t max({p.first,q.first,p.second,q.second}));\n }\n return res;\n };\n \n double ans=-1; \n auto v=dfs(s-1,0);\n for(auto p:v) if(p.second-p.first<width) chmax(ans,p.second-p.first);\n \n if(ans<0) cout<<\"-1\"<<endl;\n else cout<<ans<<endl;\n }\n return 0;\n}", "accuracy": 1, "time_ms": 70, "memory_kb": 3988, "score_of_the_acc": -0.4424, "final_rank": 13 }, { "submission_id": "aoj_1261_3160545", "code_snippet": "#include <bits/stdc++.h>\n#define int long long\nusing namespace std;\n\nint L[15], R[15];\nint weight[15];\n\ndouble W;\n\nint N, w[6];\nint TS;\n\nint make_tree(int x, vector<int> A, vector<int> B){\n \n if( (int)B.size() == 1 ) return weight[x] = w[B[0]];\n \n int idx = 0, maxA = -1;\n \n for(int i=0;i<(int)A.size();i++){\n \n if( maxA < A[i] ){\n \n maxA = A[i];\n \n idx = i;\n \n }\n \n }\n \n assert( (int)A.size() + 1 == (int)B.size() );\n \n int sum = 0;\n \n vector<int> xLA, xLB;\n \n for(int i=0;i<idx;i++) xLA.push_back(A[i]);\n \n for(int i=0;i<=idx;i++) xLB.push_back(B[i]);\n \n TS++;\n \n L[x] = TS;\n \n sum += make_tree( TS, xLA, xLB );\n \n vector<int> xRA, xRB;\n \n for(int i=idx+1;i<(int)A.size();i++) xRA.push_back(A[i]);\n \n for(int i=idx+1;i<(int)B.size();i++) xRB.push_back(B[i]);\n \n TS++;\n \n R[x] = TS;\n \n sum += make_tree( TS, xRA, xRB );\n \n return weight[x] = sum;\n}\n\ndouble lmin, rmax;\n\nvoid dfs(int x, double mid){\n \n lmin = min( lmin, mid );\n rmax = max( rmax, mid );\n \n if( L[x] == -1 && R[x] == -1 ) return;\n \n double l = weight[L[x]], r = weight[R[x]];\n \n double a = r / ( l + r ), b = l / ( l + r );\n \n dfs( L[x], mid - a );\n \n dfs( R[x], mid + b );\n \n}\n\nsigned main(){\n \n int cnt;\n cin>>cnt;\n \n while(cnt--){\n \n cin>>W>>N;\n \n for(int i=0;i<N;i++) cin>>w[i];\n \n if( N == 1 ){\n cout<<0<<endl;\n continue;\n }\n \n double ans = -1;\n \n vector<int> A;\n \n for(int i=0;i<N-1;i++) A.push_back(i);\n \n do{\n \n vector<int> B;\n \n for(int i=0;i<N;i++) B.push_back(i);\n \n do{\n\t\n\tmemset( L, -1, sizeof(L) );\n\tmemset( R, -1, sizeof(R) );\n\t\n\tTS = 0;\n\t\n\tmake_tree( TS, A, B );\n\t\n\tlmin = 1e9, rmax = -1e9;\n\t\n\tdfs( 0, 0.0 );\n\t\n\tif( rmax - lmin < W ) ans = max( ans, rmax - lmin );\n\t\n }\n while( next_permutation( B.begin(), B.end() ) );\n \n }\n while( next_permutation( A.begin(), A.end() ) );\n \n printf(\"%.8f\\n\", ans );\n \n }\n \n return 0;\n}", "accuracy": 1, "time_ms": 2300, "memory_kb": 3288, "score_of_the_acc": -0.6772, "final_rank": 17 }, { "submission_id": "aoj_1261_3160536", "code_snippet": "#include <bits/stdc++.h>\n#define int long long\nusing namespace std;\n\nint L[15], R[15];\nint weight[15];\n\ndouble W;\n\nint N, w[6];\nint TS;\n\nint make_tree(int x, vector<int> A, vector<int> B){\n \n if( (int)B.size() == 1 ) return weight[x] = w[B[0]];\n \n int idx = 0, maxA = -1;\n \n for(int i=0;i<(int)A.size();i++){\n \n if( maxA < A[i] ){\n \n maxA = A[i];\n \n idx = i;\n \n }\n \n }\n \n assert( (int)A.size() + 1 == (int)B.size() );\n \n int sum = 0;\n \n vector<int> xLA, xLB;\n \n for(int i=0;i<idx;i++) xLA.push_back(A[i]);\n \n for(int i=0;i<=idx;i++) xLB.push_back(B[i]);\n \n TS++;\n \n L[x] = TS;\n \n sum += make_tree( TS, xLA, xLB );\n \n vector<int> xRA, xRB;\n \n for(int i=idx+1;i<(int)A.size();i++) xRA.push_back(A[i]);\n \n for(int i=idx+1;i<(int)B.size();i++) xRB.push_back(B[i]);\n \n TS++;\n \n R[x] = TS;\n \n sum += make_tree( TS, xRA, xRB );\n \n return weight[x] = sum;\n}\n\ndouble lmin, rmax;\n\nvoid dfs(int x, double mid){\n \n lmin = min( lmin, mid );\n rmax = max( rmax, mid );\n \n if( L[x] == -1 && R[x] == -1 ) return;\n \n double l = weight[L[x]], r = weight[R[x]];\n \n double a = r / ( l + r ), b = l / ( l + r );\n \n dfs( L[x], mid - a );\n \n dfs( R[x], mid + b );\n \n}\n\nsigned main(){\n \n int cnt;\n cin>>cnt;\n \n while(cnt--){\n \n cin>>W>>N;\n \n for(int i=0;i<N;i++) cin>>w[i];\n \n if( N == 1 ){\n cout<<0<<endl;\n continue;\n }\n \n double ans = -1;\n \n vector<int> A;\n \n for(int i=0;i<N-1;i++) A.push_back(i);\n \n do{\n \n vector<int> B;\n \n for(int i=0;i<N;i++) B.push_back(i);\n \n do{\n\t\n\tmemset( L, -1, sizeof(L) );\n\tmemset( R, -1, sizeof(R) );\n\t\n\tTS = 0;\n\t\n\tmake_tree( TS, A, B );\n\t\n\tlmin = 1e9, rmax = -1e9;\n\t\n\tdfs( 0, 0.0 );\n\t\n\tif( rmax - lmin < W ) ans = max( ans, rmax - lmin );\n\t\n }\n while( next_permutation( B.begin(), B.end() ) );\n \n }\n while( next_permutation( A.begin(), A.end() ) );\n \n printf(\"%.8f\\n\", ans );\n \n }\n \n return 0;\n}", "accuracy": 1, "time_ms": 2300, "memory_kb": 3200, "score_of_the_acc": -0.6632, "final_rank": 16 }, { "submission_id": "aoj_1261_3160271", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\nusing Int = long long;\n\ntemplate<typename T1,typename T2> void chmin(T1 &a,T2 b){if(a>b) a=b;};\ntemplate<typename T1,typename T2> void chmax(T1 &a,T2 b){if(a<b) a=b;};\n\nsigned main(){\n Int T;\n cin>>T;\n\n cout<<fixed<<setprecision(12);\n while(T--){\n double width;\n Int n;\n cin>>width>>n;\n vector<double> w(n);\n for(Int i=0;i<n;i++) cin>>w[i];\n\n using P = pair<double, double>;\n Int s=1<<n;\n vector<double> sum(s,0);\n for(Int b=0;b<s;b++)\n for(Int i=0;i<n;i++)\n\tif((b>>i)&1) sum[b]+=w[i];\n\n function<vector<P>(Int, double)> dfs=[&](Int b,double pos){\n vector<P> res; \n if(__builtin_popcount(b)==1){\n\tres.emplace_back(pos,pos);\n\treturn res;\n }\n for(Int nb=b;nb;nb=(nb-1)&b){\n\tif(nb==b) continue;\n\tassert(nb&&(nb^b));\n\tdouble x=sum[nb],y=sum[nb^b];\n\tdouble aa=1.0/(1.0+x/y);\n\tdouble bb=1.0-aa;\n\tauto l=dfs(nb,pos-aa);\n\tauto r=dfs(nb^b,pos+bb);\n\tfor(auto p:l) \n\t for(auto q:r)\n\t res.emplace_back(min({p.first,q.first,p.second,q.second}),\n\t\t\t max({p.first,q.first,p.second,q.second}));\n }\n return res;\n };\n \n double ans=-1; \n auto v=dfs(s-1,0);\n for(auto p:v) if(p.second-p.first<width) chmax(ans,p.second-p.first);\n \n if(ans<0) cout<<\"-1\"<<endl;\n else cout<<ans<<endl;\n }\n return 0;\n}", "accuracy": 1, "time_ms": 110, "memory_kb": 3988, "score_of_the_acc": -0.4486, "final_rank": 14 }, { "submission_id": "aoj_1261_2664967", "code_snippet": "#include <stdio.h>\n#include <cmath>\n#include <algorithm>\n#include <cfloat>\n#include <stack>\n#include <queue>\n#include <vector>\n#include <string>\n#include <iostream>\n#include <set>\n#include <map>\n#include <time.h>\ntypedef long long int ll;\ntypedef unsigned long long int ull;\n#define BIG_NUM 2000000000\n#define MOD 1000000007\n#define EPS 0.000000001\nusing namespace std;\n\n\nenum Type{\n\tStone,\n\tMobile,\n};\n\nstruct Info{\n\tType type;\n\tint index_left,index_right;\n\tdouble weight,left,right,most_left_loc,most_right_loc;\n};\n\nInfo info[12];\nint num_stone,eq_index;\nint table[7][720][6],table_index[7],eq_num[7];\ndouble room_width;\nchar equation[7][100000][12];\npriority_queue<double> Q;\n\nvoid strcpy(char* to,char* str){\n\tfor(int i=0;str[i] != '\\0';i++){\n\t\tto[i] = str[i];\n\t\tto[i+1] = '\\0';\n\t}\n}\n\nvoid makeEquation(int array[6],int array_index,char buf[12],int buf_index,int value,int rest_mult,int length){\n\n\tif(rest_mult == 0){\n\t\tbuf[buf_index] = '\\0';\n\t\tstrcpy(equation[length][eq_num[length]++],buf);\n\t\treturn;\n\t}\n\n\tif(array_index < length){\n\t\tchar next_buf[12];\n\t\tfor(int i = 0; i < buf_index; i++)next_buf[i] = buf[i];\n\t\tnext_buf[buf_index] = array[array_index]+'0';\n\t\tmakeEquation(array,array_index+1,next_buf,buf_index+1,value+1,rest_mult,length);\n\t}\n\n\tif(value >= 2){\n\t\tchar next_buf[12];\n\t\tfor(int i = 0; i < buf_index; i++)next_buf[i] = buf[i];\n\t\tnext_buf[buf_index] = '*';\n\t\tmakeEquation(array,array_index,next_buf,buf_index+1,value-1,rest_mult-1,length);\n\t}\n}\n\nvoid calc_mobile(int tmp_index,int buf_index,Info tmp_info[12],int info_index,stack<int> S){\n\n\tif(equation[num_stone][tmp_index][buf_index] == '\\0'){\n\n\t\tQ.push(tmp_info[info_index-1].most_left_loc+tmp_info[info_index-1].most_right_loc);\n\n\t\treturn;\n\t}\n\n\tint left,right;\n\tdouble left_weight,right_weight;\n\tchar tmp_ch;\n\tstack<int> WORK,NEXT;\n\n\twhile(!S.empty()){\n\t\tWORK.push(S.top());\n\t\tS.pop();\n\t}\n\n\twhile(!WORK.empty()){\n\t\tNEXT.push(WORK.top());\n\t\tWORK.pop();\n\t}\n\n\ttmp_ch = equation[num_stone][tmp_index][buf_index];\n\n\tif(tmp_ch >= '0' && tmp_ch <= '5'){\n\n\t\tNEXT.push(tmp_ch-'0');\n\n\t\tcalc_mobile(tmp_index,buf_index+1,tmp_info,info_index,NEXT);\n\n\t}else{ //tmp_ch == '*'\n\n\t\tleft = NEXT.top();\n\t\tNEXT.pop();\n\t\tright = NEXT.top();\n\t\tNEXT.pop();\n\n\t\tleft_weight = tmp_info[left].weight;\n\t\tright_weight = tmp_info[right].weight;\n\n\t\ttmp_info[info_index].left = (right_weight)/(left_weight+right_weight);\n\t\ttmp_info[info_index].right = 1.0-tmp_info[info_index].left;\n\t\ttmp_info[info_index].type = Mobile;\n\t\ttmp_info[info_index].index_left = left;\n\t\ttmp_info[info_index].index_right = right;\n\t\ttmp_info[info_index].weight = left_weight+right_weight;\n\t\ttmp_info[info_index].most_left_loc = max(tmp_info[info_index].left+tmp_info[left].most_left_loc,tmp_info[right].most_left_loc-tmp_info[info_index].right);\n\t\ttmp_info[info_index].most_right_loc = max(tmp_info[info_index].right+tmp_info[right].most_right_loc,tmp_info[left].most_right_loc-tmp_info[info_index].left);\n\n\t\tNEXT.push(info_index);\n\t\tcalc_mobile(tmp_index,buf_index+1,tmp_info,info_index+1,NEXT);\n\n\t}\n}\n\nvoid calc(int tmp_index){\n\n\tInfo tmp_info[12];\n\tfor(int i = 0; i < num_stone; i++)tmp_info[i] = info[i];\n\tfor(int i = num_stone; i < 12; i++){\n\t\ttmp_info[i].index_left = -1;\n\t\ttmp_info[i].index_right = -1;\n\t\ttmp_info[i].most_left_loc = 0.0;\n\t\ttmp_info[i].most_right_loc = 0.0;\n\t}\n\n\tstack<int> S;\n\n\tcalc_mobile(tmp_index,0,tmp_info,num_stone,S);\n}\n\nvoid func(){\n\n\twhile(!Q.empty())Q.pop();\n\n\tscanf(\"%lf\",&room_width);\n\n\tscanf(\"%d\",&num_stone);\n\n\tfor(int i = 0; i < num_stone; i++){\n\t\tscanf(\"%lf\",&info[i].weight);\n\t\tinfo[i].type = Stone;\n\t\tinfo[i].index_left = -1;\n\t\tinfo[i].index_right = -1;\n\t}\n\n\tif(num_stone == 1){\n\t\tprintf(\"0.00000000\\n\");\n\t\treturn;\n\t}\n\n\teq_index = eq_num[num_stone];\n\n\tfor(int i = 0; i < eq_index; i++){\n\t\tcalc(i);\n\t}\n\n\twhile(!Q.empty()){\n\n\t\tif(Q.top() < room_width){\n\t\t\tprintf(\"%.16lf\\n\",Q.top());\n\t\t\treturn;\n\t\t}\n\t\tQ.pop();\n\t}\n\n\tprintf(\"-1\\n\");\n}\n\nvoid recursive(int array[6],int array_index,int length){\n\n\tif(array_index == length){\n\n\t\tfor(int i = 0; i < length; i++){\n\t\t\ttable[length][table_index[length]][i] = array[i];\n\t\t}\n\t\ttable_index[length]++;\n\t\treturn;\n\t}\n\n\tbool check[length];\n\tfor(int i = 0; i < length; i++)check[i] = false;\n\n\tfor(int i = 0; i < array_index; i++){\n\t\tcheck[array[i]] = true;\n\t}\n\n\tfor(int i = 0; i < length; i++){\n\t\tif(check[i] == false){\n\t\t\tint next_array[6];\n\n\t\t\tfor(int k = 0; k < array_index; k++)next_array[k] = array[k];\n\t\t\tnext_array[array_index] = i;\n\t\t\trecursive(next_array,array_index+1,length);\n\t\t}\n\t}\n}\n\nvoid makeTable(){\n\n\tfor(int length = 2; length <= 6; length++){\n\t\ttable_index[length] = 0;\n\n\t\tint first_array[6];\n\n\t\trecursive(first_array,0,length);\n\t}\n}\n\nint main(){\n\n\tmakeTable();\n\n\tfor(int length = 2; length <= 6; length++){\n\t\tchar first_buf[12];\n\t\teq_num[length] = 0;\n\n\t\tfor(int i = 0; i < table_index[length]; i++){\n\t\t\tmakeEquation(table[length][i],0,first_buf,0,0,length-1,length);\n\t\t}\n\t}\n\n\tint case_num;\n\tscanf(\"%d\",&case_num);\n\n\tfor(int i = 0; i < case_num; i++)func();\n\n\treturn 0;\n}", "accuracy": 1, "time_ms": 2520, "memory_kb": 3736, "score_of_the_acc": -0.7827, "final_rank": 18 }, { "submission_id": "aoj_1261_2664965", "code_snippet": "#include <stdio.h>\n#include <cmath>\n#include <algorithm>\n#include <cfloat>\n#include <stack>\n#include <queue>\n#include <vector>\n#include <string>\n#include <iostream>\n#include <set>\n#include <map>\n#include <time.h>\ntypedef long long int ll;\ntypedef unsigned long long int ull;\n#define BIG_NUM 2000000000\n#define MOD 1000000007\n#define EPS 0.000000001\nusing namespace std;\n\n\nenum Type{\n\tStone,\n\tMobile,\n};\n\nstruct Info{\n\tType type;\n\tint index_left,index_right;\n\tdouble weight,left,right,most_left_loc,most_right_loc;\n};\n\nInfo info[12];\nint num_stone,eq_index;\nint table[7][720][6],table_index[7],eq_num[7];\ndouble room_width;\nchar equation[7][100000][12];\npriority_queue<double> Q;\n\nvoid strcpy(char* to,char* str){\n\tfor(int i=0;str[i] != '\\0';i++){\n\t\tto[i] = str[i];\n\t\tto[i+1] = '\\0';\n\t}\n}\n\nvoid makeEquation(int array[6],int array_index,char buf[12],int buf_index,int value,int rest_mult,int length){\n\n\tif(rest_mult == 0){\n\t\tbuf[buf_index] = '\\0';\n\t\tstrcpy(equation[length][eq_num[length]++],buf);\n\t\treturn;\n\t}\n\n\tif(array_index < length){\n\t\tchar next_buf[12];\n\t\tfor(int i = 0; i < buf_index; i++)next_buf[i] = buf[i];\n\t\tnext_buf[buf_index] = array[array_index]+'0';\n\t\tmakeEquation(array,array_index+1,next_buf,buf_index+1,value+1,rest_mult,length);\n\t}\n\n\tif(value >= 2){\n\t\tchar next_buf[12];\n\t\tfor(int i = 0; i < buf_index; i++)next_buf[i] = buf[i];\n\t\tnext_buf[buf_index] = '*';\n\t\tmakeEquation(array,array_index,next_buf,buf_index+1,value-1,rest_mult-1,length);\n\t}\n}\n\nvoid calc_mobile(int tmp_index,int buf_index,Info tmp_info[12],int info_index,stack<int> S){\n\n\tif(equation[num_stone][tmp_index][buf_index] == '\\0'){\n\n\t\tQ.push(tmp_info[info_index-1].most_left_loc+tmp_info[info_index-1].most_right_loc);\n\n\t\treturn;\n\t}\n\n\tint left,right;\n\tdouble left_weight,right_weight;\n\tchar tmp_ch;\n\tstack<int> WORK,NEXT;\n\n\twhile(!S.empty()){\n\t\tWORK.push(S.top());\n\t\tS.pop();\n\t}\n\n\twhile(!WORK.empty()){\n\t\tNEXT.push(WORK.top());\n\t\tWORK.pop();\n\t}\n\n\ttmp_ch = equation[num_stone][tmp_index][buf_index];\n\n\tif(tmp_ch >= '0' && tmp_ch <= '5'){\n\n\t\tNEXT.push(tmp_ch-'0');\n\n\t\tcalc_mobile(tmp_index,buf_index+1,tmp_info,info_index,NEXT);\n\n\t}else{ //tmp_ch == '*'\n\n\t\tleft = NEXT.top();\n\t\tNEXT.pop();\n\t\tright = NEXT.top();\n\t\tNEXT.pop();\n\n\t\tleft_weight = tmp_info[left].weight;\n\t\tright_weight = tmp_info[right].weight;\n\n\t\ttmp_info[info_index].left = (right_weight)/(left_weight+right_weight);\n\t\ttmp_info[info_index].right = 1.0-tmp_info[info_index].left;\n\t\ttmp_info[info_index].type = Mobile;\n\t\ttmp_info[info_index].index_left = left;\n\t\ttmp_info[info_index].index_right = right;\n\t\ttmp_info[info_index].weight = left_weight+right_weight;\n\t\ttmp_info[info_index].most_left_loc = max(tmp_info[info_index].left+tmp_info[left].most_left_loc,tmp_info[right].most_left_loc-tmp_info[info_index].right);\n\t\ttmp_info[info_index].most_right_loc = max(tmp_info[info_index].right+tmp_info[right].most_right_loc,tmp_info[left].most_right_loc-tmp_info[info_index].left);\n\n\t\tNEXT.push(info_index);\n\t\tcalc_mobile(tmp_index,buf_index+1,tmp_info,info_index+1,NEXT);\n\n\t\t/*if(tmp_info[left].type == Stone && tmp_info[right].type == Stone)return;\n\n\t\tNEXT.pop();\n\t\tswap(left,right);\n\n\t\tswap(tmp_info[info_index].left,tmp_info[info_index].right);\n\t\ttmp_info[info_index].index_left = left;\n\t\ttmp_info[info_index].index_right = right;\n\t\ttmp_info[info_index].most_left_loc = max(tmp_info[info_index].left+tmp_info[left].most_left_loc,tmp_info[right].most_left_loc-tmp_info[info_index].right);\n\t\ttmp_info[info_index].most_right_loc = max(tmp_info[info_index].right+tmp_info[right].most_right_loc,tmp_info[left].most_right_loc-tmp_info[info_index].left);\n\n\t\tNEXT.push(info_index);\n\t\tcalc_mobile(tmp_index,buf_index+1,tmp_info,info_index+1,NEXT);*/\n\t}\n}\n\nvoid calc(int tmp_index){\n\n\tInfo tmp_info[12];\n\tfor(int i = 0; i < num_stone; i++)tmp_info[i] = info[i];\n\tfor(int i = num_stone; i < 12; i++){\n\t\ttmp_info[i].index_left = -1;\n\t\ttmp_info[i].index_right = -1;\n\t\ttmp_info[i].most_left_loc = 0.0;\n\t\ttmp_info[i].most_right_loc = 0.0;\n\t}\n\n\tstack<int> S;\n\n\tcalc_mobile(tmp_index,0,tmp_info,num_stone,S);\n}\n\nvoid func(){\n\n\twhile(!Q.empty())Q.pop();\n\n\tscanf(\"%lf\",&room_width);\n\n\tscanf(\"%d\",&num_stone);\n\n\tfor(int i = 0; i < num_stone; i++){\n\t\tscanf(\"%lf\",&info[i].weight);\n\t\tinfo[i].type = Stone;\n\t\tinfo[i].index_left = -1;\n\t\tinfo[i].index_right = -1;\n\t}\n\n\tif(num_stone == 1){\n\t\tprintf(\"0.00000000\\n\");\n\t\treturn;\n\t}\n\n\teq_index = eq_num[num_stone];\n\n\tfor(int i = 0; i < eq_index; i++){\n\t\tcalc(i);\n\t}\n\n\twhile(!Q.empty()){\n\n\t\tif(Q.top() < room_width){\n\t\t\tprintf(\"%.16lf\\n\",Q.top());\n\t\t\treturn;\n\t\t}\n\t\tQ.pop();\n\t}\n\n\tprintf(\"-1\\n\");\n}\n\nvoid recursive(int array[6],int array_index,int length){\n\n\tif(array_index == length){\n\n\t\tfor(int i = 0; i < length; i++){\n\t\t\ttable[length][table_index[length]][i] = array[i];\n\t\t}\n\t\ttable_index[length]++;\n\t\treturn;\n\t}\n\n\tbool check[length];\n\tfor(int i = 0; i < length; i++)check[i] = false;\n\n\tfor(int i = 0; i < array_index; i++){\n\t\tcheck[array[i]] = true;\n\t}\n\n\tfor(int i = 0; i < length; i++){\n\t\tif(check[i] == false){\n\t\t\tint next_array[6];\n\n\t\t\tfor(int k = 0; k < array_index; k++)next_array[k] = array[k];\n\t\t\tnext_array[array_index] = i;\n\t\t\trecursive(next_array,array_index+1,length);\n\t\t}\n\t}\n}\n\nvoid makeTable(){\n\n\tfor(int length = 2; length <= 6; length++){\n\t\ttable_index[length] = 0;\n\n\t\tint first_array[6];\n\n\t\trecursive(first_array,0,length);\n\t}\n}\n\nint main(){\n\n\tmakeTable();\n\n\tfor(int length = 2; length <= 6; length++){\n\t\tchar first_buf[12];\n\t\teq_num[length] = 0;\n\n\t\tfor(int i = 0; i < table_index[length]; i++){\n\t\t\tmakeEquation(table[length][i],0,first_buf,0,0,length-1,length);\n\t\t}\n\t}\n\n\tint case_num;\n\tscanf(\"%d\",&case_num);\n\n\tfor(int i = 0; i < case_num; i++)func();\n\n\treturn 0;\n}", "accuracy": 1, "time_ms": 2520, "memory_kb": 3824, "score_of_the_acc": -0.7968, "final_rank": 19 }, { "submission_id": "aoj_1261_2664953", "code_snippet": "#include <stdio.h>\n#include <cmath>\n#include <algorithm>\n#include <cfloat>\n#include <stack>\n#include <queue>\n#include <vector>\n#include <string>\n#include <iostream>\n#include <set>\n#include <map>\n#include <time.h>\ntypedef long long int ll;\ntypedef unsigned long long int ull;\n#define BIG_NUM 2000000000\n#define MOD 1000000007\n#define EPS 0.000000001\nusing namespace std;\n\n\nenum Type{\n\tStone,\n\tMobile,\n};\n\nstruct Info{\n\tType type;\n\tint index_left,index_right;\n\tdouble weight,left,right,most_left_loc,most_right_loc;\n};\n\nInfo info[12];\nint num_stone,eq_index;\nint table[7][720][6],table_index[7],eq_num[7];\ndouble room_width;\nchar equation[7][100000][12];\npriority_queue<double> Q;\n\nvoid strcpy(char* to,char* str){\n\tfor(int i=0;str[i] != '\\0';i++){\n\t\tto[i] = str[i];\n\t\tto[i+1] = '\\0';\n\t}\n}\n\nvoid makeEquation(int array[6],int array_index,char buf[12],int buf_index,int value,int rest_mult,int length){\n\n\tif(rest_mult == 0){\n\t\tbuf[buf_index] = '\\0';\n\t\tstrcpy(equation[length][eq_num[length]++],buf);\n\t\treturn;\n\t}\n\n\tif(array_index < length){\n\t\tchar next_buf[12];\n\t\tfor(int i = 0; i < buf_index; i++)next_buf[i] = buf[i];\n\t\tnext_buf[buf_index] = array[array_index]+'0';\n\t\tmakeEquation(array,array_index+1,next_buf,buf_index+1,value+1,rest_mult,length);\n\t}\n\n\tif(value >= 2){\n\t\tchar next_buf[12];\n\t\tfor(int i = 0; i < buf_index; i++)next_buf[i] = buf[i];\n\t\tnext_buf[buf_index] = '*';\n\t\tmakeEquation(array,array_index,next_buf,buf_index+1,value-1,rest_mult-1,length);\n\t}\n}\n\nvoid calc_mobile(int tmp_index,int buf_index,Info tmp_info[12],int info_index,stack<int> S){\n\n\tif(equation[num_stone][tmp_index][buf_index] == '\\0'){\n\n\t\tQ.push(tmp_info[info_index-1].most_left_loc+tmp_info[info_index-1].most_right_loc);\n\n\t\treturn;\n\t}\n\n\tint left,right;\n\tdouble left_weight,right_weight;\n\tchar tmp_ch;\n\tstack<int> WORK,NEXT;\n\n\twhile(!S.empty()){\n\t\tWORK.push(S.top());\n\t\tS.pop();\n\t}\n\n\twhile(!WORK.empty()){\n\t\tNEXT.push(WORK.top());\n\t\tWORK.pop();\n\t}\n\n\ttmp_ch = equation[num_stone][tmp_index][buf_index];\n\n\tif(tmp_ch >= '0' && tmp_ch <= '5'){\n\n\t\tNEXT.push(tmp_ch-'0');\n\n\t\tcalc_mobile(tmp_index,buf_index+1,tmp_info,info_index,NEXT);\n\n\t}else{ //tmp_ch == '*'\n\n\t\tleft = NEXT.top();\n\t\tNEXT.pop();\n\t\tright = NEXT.top();\n\t\tNEXT.pop();\n\n\t\tleft_weight = tmp_info[left].weight;\n\t\tright_weight = tmp_info[right].weight;\n\n\t\ttmp_info[info_index].left = (right_weight)/(left_weight+right_weight);\n\t\ttmp_info[info_index].right = 1.0-tmp_info[info_index].left;\n\t\ttmp_info[info_index].type = Mobile;\n\t\ttmp_info[info_index].index_left = left;\n\t\ttmp_info[info_index].index_right = right;\n\t\ttmp_info[info_index].weight = left_weight+right_weight;\n\t\ttmp_info[info_index].most_left_loc = max(tmp_info[info_index].left+tmp_info[left].most_left_loc,tmp_info[right].most_left_loc-tmp_info[info_index].right);\n\t\ttmp_info[info_index].most_right_loc = max(tmp_info[info_index].right+tmp_info[right].most_right_loc,tmp_info[left].most_right_loc-tmp_info[info_index].left);\n\n\t\tNEXT.push(info_index);\n\t\tcalc_mobile(tmp_index,buf_index+1,tmp_info,info_index+1,NEXT);\n\n\t\tif(tmp_info[left].type == Stone && tmp_info[right].type == Stone)return;\n\n\t\tNEXT.pop();\n\t\tswap(left,right);\n\n\t\tswap(tmp_info[info_index].left,tmp_info[info_index].right);\n\t\ttmp_info[info_index].index_left = left;\n\t\ttmp_info[info_index].index_right = right;\n\t\ttmp_info[info_index].most_left_loc = max(tmp_info[info_index].left+tmp_info[left].most_left_loc,tmp_info[right].most_left_loc-tmp_info[info_index].right);\n\t\ttmp_info[info_index].most_right_loc = max(tmp_info[info_index].right+tmp_info[right].most_right_loc,tmp_info[left].most_right_loc-tmp_info[info_index].left);\n\n\t\tNEXT.push(info_index);\n\t\tcalc_mobile(tmp_index,buf_index+1,tmp_info,info_index+1,NEXT);\n\t}\n}\n\nvoid calc(int tmp_index){\n\n\tInfo tmp_info[12];\n\tfor(int i = 0; i < num_stone; i++)tmp_info[i] = info[i];\n\tfor(int i = num_stone; i < 12; i++){\n\t\ttmp_info[i].index_left = -1;\n\t\ttmp_info[i].index_right = -1;\n\t\ttmp_info[i].most_left_loc = 0.0;\n\t\ttmp_info[i].most_right_loc = 0.0;\n\t}\n\n\tstack<int> S;\n\n\tcalc_mobile(tmp_index,0,tmp_info,num_stone,S);\n}\n\nvoid func(){\n\n\twhile(!Q.empty())Q.pop();\n\n\tscanf(\"%lf\",&room_width);\n\n\tscanf(\"%d\",&num_stone);\n\n\tfor(int i = 0; i < num_stone; i++){\n\t\tscanf(\"%lf\",&info[i].weight);\n\t\tinfo[i].type = Stone;\n\t\tinfo[i].index_left = -1;\n\t\tinfo[i].index_right = -1;\n\t}\n\n\tif(num_stone == 1){\n\t\tprintf(\"0.00000000\\n\");\n\t\treturn;\n\t}\n\n\teq_index = eq_num[num_stone];\n\n\tfor(int i = 0; i < eq_index; i++){\n\t\tcalc(i);\n\t}\n\n\twhile(!Q.empty()){\n\n\t\tif(Q.top() < room_width){\n\t\t\tprintf(\"%.16lf\\n\",Q.top());\n\t\t\treturn;\n\t\t}\n\t\tQ.pop();\n\t}\n\n\tprintf(\"-1\\n\");\n}\n\nvoid recursive(int array[6],int array_index,int length){\n\n\tif(array_index == length){\n\n\t\tfor(int i = 0; i < length; i++){\n\t\t\ttable[length][table_index[length]][i] = array[i];\n\t\t}\n\t\ttable_index[length]++;\n\t\treturn;\n\t}\n\n\tbool check[length];\n\tfor(int i = 0; i < length; i++)check[i] = false;\n\n\tfor(int i = 0; i < array_index; i++){\n\t\tcheck[array[i]] = true;\n\t}\n\n\tfor(int i = 0; i < length; i++){\n\t\tif(check[i] == false){\n\t\t\tint next_array[6];\n\n\t\t\tfor(int k = 0; k < array_index; k++)next_array[k] = array[k];\n\t\t\tnext_array[array_index] = i;\n\t\t\trecursive(next_array,array_index+1,length);\n\t\t}\n\t}\n}\n\nvoid makeTable(){\n\n\tfor(int length = 2; length <= 6; length++){\n\t\ttable_index[length] = 0;\n\n\t\tint first_array[6];\n\n\t\trecursive(first_array,0,length);\n\t}\n}\n\nint main(){\n\n\tmakeTable();\n\n\tfor(int length = 2; length <= 6; length++){\n\t\tchar first_buf[12];\n\t\teq_num[length] = 0;\n\n\t\tfor(int i = 0; i < table_index[length]; i++){\n\t\t\tmakeEquation(table[length][i],0,first_buf,0,0,length-1,length);\n\t\t}\n\t}\n\n\tint case_num;\n\tscanf(\"%d\",&case_num);\n\n\tfor(int i = 0; i < case_num; i++)func();\n\n\treturn 0;\n}", "accuracy": 1, "time_ms": 6450, "memory_kb": 7548, "score_of_the_acc": -2, "final_rank": 20 }, { "submission_id": "aoj_1261_2376219", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\n#define int long long\n#define double long double\n#define INF 10000\n#define N 10\ntypedef pair<double,double> P;\ntypedef vector<P> V;\nint n;\ndouble w[N];\n\nint getW(int bit){\n int res = 0;\n for(int i=0;i<n;i++) if(bit>>i&1) res += w[i];\n return res;\n}\n\nP Merge(P a,P b){\n double l = min(a.first,b.first);\n double r = max(a.second,b.second);\n return P(l,r);\n}\n\nV dfs(double x,int bit){\n\n if(__builtin_popcount(bit)==1) return V(1,P(x,x));\n if(__builtin_popcount(bit)==0) return V(1,P(-INF,INF));\n \n vector<P> res;\n for(int i=0;i<(1<<n);i++){\n if( (i | bit) != bit ) continue;\n int lbit = i;\n int rbit = bit ^ i;\n double lw = getW(lbit);\n double rw = getW(rbit);\n if(lw == 0 || rw == 0) continue;\n double lx = x - rw/(lw+rw);\n double rx = x + lw/(lw+rw);\n V L = dfs(lx,lbit);\n V R = dfs(rx,rbit);\n\n for(P l:L)\n for(P r:R) res.push_back(Merge(l,r));\n }\n \n return res;\n}\n\n\nsigned main(){\n int q;\n cin>>q;\n while(q--){\n double len;\n cin>>len;\n cin>>n;\n for(int i=0;i<n;i++) cin>>w[i];\n\n V v = dfs(0,(1<<n)-1);\n double ans = -1;\n for(P p:v)\n if(p.second-p.first<len)ans = max(ans,p.second - p.first);\n \n if(ans<0) printf(\"-1\\n\");\n else printf(\"%.10Lf\\n\",ans);\n }\n return 0;\n}", "accuracy": 1, "time_ms": 140, "memory_kb": 5068, "score_of_the_acc": -0.6253, "final_rank": 15 }, { "submission_id": "aoj_1261_2376058", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\nconst double eps = 1e-8;\n\nstruct state{\n double left,right,weight;\n};\n\ndouble r;\nint n;\nint w[6];\n\nstate merge(state a,state b){\n double sum=a.weight+b.weight;\n double aleft=a.left - b.weight/sum;\n double bleft=b.left + a.weight/sum;\n double left=min(aleft,bleft);\n double aright=a.right - b.weight/sum;\n double bright=b.right + a.weight/sum;\n double right=max(aright,bright);\n return (state){left,right,sum};\n}\n\ndouble calc(vector<state> u){\n vector<state> tmp=u;\n \n int t[6]={0,1,2,3,4,5};\n int a[6];\n double res=-1;\n \n do{\n u=tmp;\n \n for(int i=0;i<n-1;i++)a[i]=t[i];\n \n for(int i=0;i<n-1;i++){\n int c=0;\n for(int j=i+1;j<n-1;j++){\n if(a[i]<a[j])c++;\n }\n a[i]=c;\n }\n\n for(int i=0;i<n-1;i++){\n int id=a[i];\n u[id]=merge(u[id],u[id+1]);\n u.erase( u.begin() + id + 1);\n }\n double value=u[0].right-u[0].left;\n if( value < r + eps ) res=max(res,value);\n \n }while( next_permutation(t,t+n-1) );\n \n return res;\n}\n\ndouble solve(){\n int t[6]={0,1,2,3,4,5};\n double res=-1;\n \n do{\n vector< state > u(n);\n \n for(int i=0;i<n;i++){\n u[i]=(state){ 0.0, 0.0, (double)w[ t[i] ]};\n }\n\n\n res=max(res,calc(u));\n \n }while( next_permutation(t,t+n) );\n\n if(res < r + eps )return res;\n else return -1;\n}\n\nint main(){\n int Tc;\n cin>>Tc;\n while(Tc--){\n cin>>r;\n cin>>n;\n for(int i=0;i<n;i++)cin>>w[i];\n double ans=solve();\n if(ans<0){\n cout<<\"-1\"<<endl;\n }else{\n printf(\"%.10f\\n\",ans);\n }\n }\n return 0;\n}", "accuracy": 1, "time_ms": 130, "memory_kb": 3228, "score_of_the_acc": -0.3307, "final_rank": 11 }, { "submission_id": "aoj_1261_1550338", "code_snippet": "#include <iostream>\n#include <cstdio>\n#include <vector>\n#include <stack>\n#include <algorithm>\n#define repeat_from(i,m,n) for (int i = (m); (i) < (n); ++(i))\n#define repeat(i,n) repeat_from(i,0,n)\nusing namespace std;\nstruct mobile_t {\n int w; // weight\n double l; // left\n double r; // right\n};\nconst double eps = 0.0001;\ndouble evaluate(vector<int> const & p) {\n stack<mobile_t> stk;\n for (int w : p) {\n if (w == 0) {\n if (stk.size() < 2) return -1;\n mobile_t x = stk.top(); stk.pop();\n mobile_t y = stk.top(); stk.pop();\n mobile_t z;\n z.w = x.w + y.w;\n double dx = y.w /(double) (x.w + y.w);\n double dy = x.w /(double) (x.w + y.w);\n z.l = max(x.l + dx, y.l - dy);\n z.r = max(x.r - dx, y.r + dy);\n stk.push(z);\n } else {\n stk.push((mobile_t){ w, 0.0, 0.0 });\n }\n }\n if (stk.size() != 1) return -1;\n return stk.top().l + stk.top().r;\n}\nint main() {\n int datasets; cin >> datasets;\n repeat (dataset, datasets) {\n double r; cin >> r;\n int s; cin >> s;\n vector<int> w(s); repeat (i,s) cin >> w[i];\n double result = -1;\n vector<int> p;\n repeat (i,s) p.push_back(w[i]);\n repeat (i,s-1) p.push_back(0);\n sort(p.begin(), p.end());\n do {\n double it = evaluate(p);\n if (r + eps < it) continue;\n result = max(result, it);\n } while (next_permutation(p.begin(), p.end()));\n if (result == -1) {\n printf(\"-1\\n\");\n } else {\n printf(\"%.16lf\\n\", result);\n }\n }\n return 0;\n}", "accuracy": 1, "time_ms": 1870, "memory_kb": 1268, "score_of_the_acc": -0.2888, "final_rank": 6 }, { "submission_id": "aoj_1261_1550335", "code_snippet": "#include <iostream>\n#include <cstdio>\n#include <vector>\n#include <stack>\n#include <algorithm>\n#define repeat_from(i,m,n) for (int i = (m); (i) < (n); ++(i))\n#define repeat(i,n) repeat_from(i,0,n)\nusing namespace std;\nstruct mobile_t {\n int w; // weight\n double l; // left\n double r; // right\n};\nconst double eps = 0.0001;\ndouble evaluate(vector<int> const & p, double r) {\n stack<mobile_t> stk;\n for (int w : p) {\n if (w == 0) {\n if (stk.size() < 2) return -1;\n mobile_t x = stk.top(); stk.pop();\n mobile_t y = stk.top(); stk.pop();\n mobile_t z;\n z.w = x.w + y.w;\n double dx = y.w /(double) (x.w + y.w);\n double dy = x.w /(double) (x.w + y.w);\n z.l = max(x.l + dx, y.l - dy);\n z.r = max(x.r - dx, y.r + dy);\n if (r + eps < z.r + z.l) return -1;\n stk.push(z);\n } else {\n stk.push((mobile_t){ w, 0.0, 0.0 });\n }\n }\n if (stk.size() != 1) return -1;\n return stk.top().l + stk.top().r;\n}\nint main() {\n int datasets; cin >> datasets;\n repeat (dataset, datasets) {\n double r; cin >> r;\n int s; cin >> s;\n vector<int> w(s); repeat (i,s) cin >> w[i];\n double result = -1;\n vector<int> p;\n repeat (i,s) p.push_back(w[i]);\n repeat (i,s-1) p.push_back(0);\n sort(p.begin(), p.end());\n do {\n result = max(result, evaluate(p, r));\n } while (next_permutation(p.begin(), p.end()));\n if (result == -1) {\n printf(\"-1\\n\");\n } else {\n printf(\"%.16lf\\n\", result);\n }\n }\n return 0;\n}", "accuracy": 1, "time_ms": 1880, "memory_kb": 1268, "score_of_the_acc": -0.2904, "final_rank": 7 }, { "submission_id": "aoj_1261_1550334", "code_snippet": "#include <iostream>\n#include <cstdio>\n#include <vector>\n#include <stack>\n#include <algorithm>\n#include <cassert>\n#define repeat_from(i,m,n) for (int i = (m); (i) < (n); ++(i))\n#define repeat(i,n) repeat_from(i,0,n)\n#define repeat_from_reverse(i,m,n) for (int i = (n)-1; (i) >= (m); --(i))\n#define repeat_reverse(i,n) repeat_from_reverse(i,0,n)\n#define dump(x) cerr << #x << \" = \" << (x) << endl\n#define debug(x) cerr << #x << \" = \" << (x) << \" (L\" << __LINE__ << \")\" << \" \" << __FILE__ << endl\nusing namespace std;\nstruct mobile_t {\n int w; // weight\n double l; // left\n double r; // right\n};\nconst double eps = 0.0001;\ndouble evaluate(vector<int> const & p, double r) {\n stack<mobile_t> stk;\n for (int w : p) {\n if (w == 0) {\n if (stk.size() < 2) return -1;\n mobile_t x = stk.top(); stk.pop();\n mobile_t y = stk.top(); stk.pop();\n mobile_t z;\n z.w = x.w + y.w;\n double dx = y.w /(double) (x.w + y.w);\n double dy = x.w /(double) (x.w + y.w);\n z.l = max(x.l + dx, y.l - dy);\n z.r = max(x.r - dx, y.r + dy);\n if (r + eps < z.r + z.l) return -1;\n stk.push(z);\n } else {\n stk.push((mobile_t){ w, 0.0, 0.0 });\n }\n }\n if (stk.size() != 1) return -1;\n return stk.top().l + stk.top().r;\n}\nint main() {\n int datasets; cin >> datasets;\n repeat (dataset, datasets) {\n double r; cin >> r;\n int s; cin >> s;\n vector<int> w(s); repeat (i,s) cin >> w[i];\n double result = -1;\n vector<int> p;\n repeat (i,s) p.push_back(w[i]);\n repeat (i,s-1) p.push_back(0);\n sort(p.begin(), p.end());\n do {\n result = max(result, evaluate(p, r));\n } while (next_permutation(p.begin(), p.end()));\n if (result == -1) {\n cout << -1 << endl;\n } else {\n printf(\"%.16lf\\n\", result);\n }\n }\n return 0;\n}", "accuracy": 1, "time_ms": 1900, "memory_kb": 1308, "score_of_the_acc": -0.2998, "final_rank": 8 }, { "submission_id": "aoj_1261_1550071", "code_snippet": "#include <iostream> \n#include <cassert>\n#include <queue>\n#include <algorithm>\n#include <vector>\n#include <cstdio>\n\nusing namespace std;\n\n#define whole(xs) (xs).begin(),(xs).end()\n\nnamespace {\n template<class T> ostream& operator<<(ostream& os, const vector<T>& vs) {\n if (vs.empty()) return os << \"[]\";\n os << \"[\" << vs[0];\n for (int i = 1; i < vs.size(); i++) os << \" \" << vs[i];\n return os << \"]\";\n }\n\n typedef long double real;\n\n real EPS = 1e-9;\n\n real R;\n int S;\n vector<int> W;\n void input() {\n cin >> R >> S;\n W.clear(); W.resize(S);\n for (int i = 0; i < S; i++) {\n cin >> W[i];\n }\n }\n\n real solve();\n\n vector<int> pat;\n void init() {\n real ans = -1;\n pat.clear();\n for (int k = 0; k < S - 1; k++) pat.push_back(0);\n for (int i = 0; i < S; i++) pat.push_back(W[i]);\n ans = max(ans, solve());\n if (ans < 0) {\n cout << -1 << endl;\n } else {\n printf(\"%.18Lf\\n\", ans);\n }\n }\n\n struct State {\n int w;\n real left, right;\n State(int w, real left, real right) : w(w), left(left), right(right) {}\n };\n\n real parse() {\n vector<State> stack;\n real ans = 0;\n for (int i = 0; i < pat.size(); i++) {\n if (pat[i] == 0) { // 結合\n if (stack.size() < 2) return -1.0;\n assert(stack.size() >= 2);\n State a = stack.back(); stack.pop_back();\n State b = stack.back(); stack.pop_back();\n int w = a.w + b.w;\n real L = 1.0 / w * b.w, R = 1.0 / w * a.w;\n real nleft = max(L + a.left, b.left - R);\n real nright = max(R + b.right, a.right - L);\n stack.push_back(State(a.w + b.w, nleft, nright));\n ans = max(ans, nleft + nright);\n } else {\n assert(pat[i] >= 1); // 石\n stack.push_back(State(pat[i], 0, 0));\n }\n }\n assert(stack.size() == 1);\n return ans;\n }\n\n real solve() {\n real ans = -1.0;\n sort(whole(pat));\n do {\n real c = parse();\n //printf(\"c: %.18Lf \", c); cout << pat << endl;\n if (c > R + EPS) continue;\n ans = max(c, ans);\n } while (next_permutation(whole(pat)));\n return ans;\n }\n}\n\nint main() {\n int T; cin >> T;\n for (int i = 0; i < T; i++) {\n input(); init();\n }\n return 0;\n}", "accuracy": 1, "time_ms": 740, "memory_kb": 1316, "score_of_the_acc": -0.121, "final_rank": 4 }, { "submission_id": "aoj_1261_1156873", "code_snippet": "#include<iostream>\n#include<vector>\n#include<cstdio>\nusing namespace std;\nvoid prB(int S){\n for(int i=0;i<6;i++){\n int x=(S>>i);\n cout<<(x&1)<<\" \";\n }\n cout<<endl;\n}\ndouble width;\nint n;\nint t[6];\n \nint mem[(1<<7)];\nint calc(int S){\n if(mem[S]!=-1)return mem[S];\n int x=S&-S;\n return mem[S]=calc(S-x)+calc(x);\n}\n \nvector<double> dp[(1<<7)];\nvector<double> dp2[(1<<7)];\nbool vd[(1<<7)];\nvoid rec(int S){\n if(vd[S])return;\n vd[S]=true;\n int sub=S;\n do{\n if(sub!=S&&sub!=0){\n int lS=sub,rS=S-sub;\n rec(lS);rec(rS);\n double lsum=calc(lS),rsum=calc(rS);\n \n double ld=rsum/(lsum+rsum);\n double rd=lsum/(lsum+rsum);\n for(int i=0;i<(int)dp[lS].size();i++){\n for(int j=0;j<(int)dp[rS].size();j++){\n double nl=min(dp[lS][i]-ld,dp[rS][j]+rd);\n double nr=max(dp2[lS][i]-ld,dp2[rS][j]+rd);\n dp[S].push_back(nl);\n dp2[S].push_back(nr);\n }\n }\n }\n sub=(sub-1)&S;\n }while(sub!=S);\n}\n \nvoid solve(){\n for(int i=0;i<n;i++){\n vd[(1<<i)]=true;\n dp[(1<<i)].push_back(0);\n dp2[(1<<i)].push_back(0);\n }\n int U=(1<<n)-1;\n rec(U);\n double ans=-1.0;\n for(int i=0;i<(int)dp[U].size();i++){\n double d=dp2[U][i]-dp[U][i];\n if( d < width + 0.00001 )ans=max(ans,d);\n }\n if(ans==-1.0)cout<<\"-1\"<<endl;\n else printf(\"%.10f\\n\",ans);\n}\n \nvoid init(){\n for(int i=0;i<(1<<7);i++){\n vd[i]=0;\n mem[i]=-1;\n dp[i].clear();\n dp2[i].clear();\n }\n mem[0]=0;\n for(int i=0;i<n;i++)mem[(1<<i)]=t[i];\n}\n \nint main(){\n int Tc;\n cin>>Tc;\n for(int tc=1;tc<=Tc;tc++){\n cin>>width>>n;\n for(int i=0;i<n;i++)cin>>t[i];\n init();\n solve();\n }\n return 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 1896, "score_of_the_acc": -0.1, "final_rank": 3 }, { "submission_id": "aoj_1261_1067909", "code_snippet": "#include <algorithm>\n#include <array>\n#include <cstdlib>\n#include <iostream>\n#include <memory>\n#include <tuple>\n#include <vector>\nusing namespace std;\n\ntemplate<class T> inline void chmax(T &a, const T &b) { if(a < b) a = b; }\n\nstruct node_t {\n\tshared_ptr<node_t> left_child, right_child;\n\n\tinline bool is_leaf() const {\n\t\treturn left_child == nullptr;\n\t}\n\n\tnode_t(const shared_ptr<node_t> &l = nullptr, const shared_ptr<node_t> &r = nullptr):\n\t\tleft_child(l), right_child(r) {}\n};\n\nconstexpr int MAX_S = 6;\n\ntypedef array<vector<shared_ptr<node_t>>, MAX_S + 1> T_array;\ntypedef tuple<int, double, double> result;\nenum { WEIGHT, LEFT, RIGHT };\n\nT_array init_trees() {\n\tT_array trees;\n\n\ttrees[1].emplace_back(new node_t());\n\n\tfor(int s = 2; s <= 6; ++s) {\n\t\tfor(int l = 1; l < s; ++l) {\n\t\t\tconst int r = s - l;\n\n\t\t\tfor(const auto &p1 : trees[l]) {\n\t\t\t\tfor(const auto &p2 : trees[r]) {\n\t\t\t\t\ttrees[s].emplace_back(new node_t(p1, p2));\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n\n\treturn trees;\n}\n\nresult dfs(const shared_ptr<node_t> &node, vector<int>::const_iterator &it) {\n\tif(node->is_leaf()) {\n\t\tconst result res(*it, 0.0, 0.0);\n\t\t++it;\n\t\treturn res;\n\t}\n\n\tconst auto l_result = dfs(node->left_child, it);\n\tconst auto r_result = dfs(node->right_child, it);\n\n\tconst int weight = get<WEIGHT>(l_result) + get<WEIGHT>(r_result);\n\n\tconst double left_pos = -static_cast<double>(get<WEIGHT>(r_result)) / weight;\n\tconst double right_pos = static_cast<double>(get<WEIGHT>(l_result)) / weight;\n\n\tconst double left = min(get<LEFT>(l_result) + left_pos, get<LEFT>(r_result) + right_pos);\n\tconst double right = max(get<RIGHT>(l_result) + left_pos, get<RIGHT>(r_result) + right_pos);\n\n\treturn result(weight, left, right);\n}\n\nvoid solve(const T_array &trees) {\n\tdouble r;\n\tint s;\n\tcin >> r >> s;\n\n\tvector<int> w(s);\n\tfor(auto &e : w) cin >> e;\n\n\tsort(w.begin(), w.end());\n\n\tdouble ans = -1;\n\tdo {\n\t\tfor(const auto &root : trees[s]) {\n\t\t\tauto it = w.cbegin();\n\t\t\tconst auto tmp = dfs(root, it);\n\t\t\tconst double width = get<RIGHT>(tmp) - get<LEFT>(tmp);\n\t\t\tif(width < r) chmax(ans, width);\n\t\t}\n\n\t} while(next_permutation(w.begin(), w.end()));\n\n\n\tif(ans < 0) {\n\t\tcout << \"-1\" << endl;\n\t}\n\telse {\n\t\tcout << ans << endl;\n\t}\n}\n\nint main() {\n\tcin.tie(nullptr);\n\tios::sync_with_stdio(false);\n\tcout.setf(ios::fixed);\n\tcout.precision(10);\n\n\tconst auto trees = init_trees();\n\n\tint T;\n\tcin >> T;\n\n\twhile(T--) solve(trees);\n\n\treturn EXIT_SUCCESS;\n}", "accuracy": 1, "time_ms": 50, "memory_kb": 1312, "score_of_the_acc": -0.0132, "final_rank": 1 }, { "submission_id": "aoj_1261_1067904", "code_snippet": "#include <algorithm>\n#include <array>\n#include <cstdlib>\n#include <iostream>\n#include <memory>\n#include <tuple>\n#include <vector>\nusing namespace std;\n\ntemplate<class T> inline void chmax(T &a, const T &b) { if(a < b) a = b; }\n\nstruct node_t {\n\tshared_ptr<node_t> left_child, right_child;\n\n\tinline bool is_leaf() const {\n\t\treturn left_child == nullptr;\n\t}\n\n\tnode_t():left_child(nullptr), right_child(nullptr) {}\n};\n\nconstexpr int MAX_S = 6;\n\ntypedef array<vector<shared_ptr<node_t>>, MAX_S + 1> T_array;\ntypedef tuple<int, double, double> result;\nenum { WEIGHT, LEFT, RIGHT };\n\nT_array init_trees() {\n\tT_array trees;\n\n\ttrees[1].emplace_back(shared_ptr<node_t>(new node_t));\n\n\tfor(int s = 2; s <= 6; ++s) {\n\t\tfor(int l = 1; l < s; ++l) {\n\t\t\tconst int r = s - l;\n\n\t\t\tfor(const auto &p1 : trees[l]) {\n\t\t\t\tfor(const auto &p2 : trees[r]) {\n\t\t\t\t\tshared_ptr<node_t> p(new node_t);\n\t\t\t\t\tp->left_child = p1;\n\t\t\t\t\tp->right_child = p2;\n\t\t\t\t\ttrees[s].emplace_back(p);\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n\n\treturn trees;\n}\n\nresult dfs(const shared_ptr<node_t> &node, vector<int>::const_iterator &it) {\n\tif(node->is_leaf()) {\n\t\tconst result res(*it, 0.0, 0.0);\n\t\t++it;\n\t\treturn res;\n\t}\n\n\tconst auto l_result = dfs(node->left_child, it);\n\tconst auto r_result = dfs(node->right_child, it);\n\n\tconst int weight = get<WEIGHT>(l_result) + get<WEIGHT>(r_result);\n\n\tconst double left_pos = -static_cast<double>(get<WEIGHT>(r_result)) / weight;\n\tconst double right_pos = static_cast<double>(get<WEIGHT>(l_result)) / weight;\n\n\tconst double left = min(get<LEFT>(l_result) + left_pos, get<LEFT>(r_result) + right_pos);\n\tconst double right = max(get<RIGHT>(l_result) + left_pos, get<RIGHT>(r_result) + right_pos);\n\n\treturn result(weight, left, right);\n}\n\nvoid solve(const T_array &trees) {\n\tdouble r;\n\tint s;\n\tcin >> r >> s;\n\n\tvector<int> w(s);\n\tfor(auto &e : w) cin >> e;\n\n\tsort(w.begin(), w.end());\n\n\tdouble ans = -1;\n\tdo {\n\t\tfor(const auto &root : trees[s]) {\n\t\t\tauto it = w.cbegin();\n\t\t\tconst auto tmp = dfs(root, it);\n\t\t\tconst double width = get<RIGHT>(tmp) - get<LEFT>(tmp);\n\t\t\tif(width < r) chmax(ans, width);\n\t\t}\n\n\t} while(next_permutation(w.begin(), w.end()));\n\n\n\tif(ans < 0) {\n\t\tcout << \"-1\" << endl;\n\t}\n\telse {\n\t\tcout << ans << endl;\n\t}\n}\n\nint main() {\n\tcin.tie(nullptr);\n\tios::sync_with_stdio(false);\n\tcout.setf(ios::fixed);\n\tcout.precision(10);\n\n\tconst auto trees = init_trees();\n\n\tint T;\n\tcin >> T;\n\n\twhile(T--) solve(trees);\n\n\treturn EXIT_SUCCESS;\n}", "accuracy": 1, "time_ms": 50, "memory_kb": 1320, "score_of_the_acc": -0.0145, "final_rank": 2 } ]
aoj_1257_cpp
Problem A: Sum of Consecutive Prime Numbers Some positive integers can be represented by a sum of one or more consecutive prime numbers. How many such representations does a given positive integer have? For example, the integer 53 has two representations 5 + 7 + 11 + 13 + 17 and 53. The integer 41 has three representations 2 + 3 + 5 + 7 + 11 + 13, 11 + 13 + 17, and 41. The integer 3 has only one representation, which is 3. The integer 20 has no such representations. Note that summands must be consecutive prime numbers, so neither 7 + 13 nor 3 + 5 + 5 + 7 is a valid representation for the integer 20. Your mission is to write a program that reports the number of representations for the given positive integer. Input The input is a sequence of positive integers each in a separate line. The integers are between 2 and 10 000, inclusive. The end of the input is indicated by a zero. Output The output should be composed of lines each corresponding to an input line except the last zero. An output line includes the number of representations for the input integer as the sum of one or more consecutive prime numbers. No other characters should be inserted in the output. Sample Input 2 3 17 41 20 666 12 53 0 Output for the Sample Input 1 1 2 3 0 0 1 2
[ { "submission_id": "aoj_1257_10734986", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\nset<int>primes;\nint main(){\n int n;\n while(true){\n cin>>n;\n primes.clear();\n if(n==0)\n break;\n for(int i=2;i<=n;++i)\n primes.insert(i);\n for(auto i=primes.begin();i!=primes.end();++i){\n for(long j=(*i)*2;j<=n;j+=*i){\n if(primes.find(j)!=primes.end()){\n primes.erase(j);\n }\n }\n }\n int c=0;\n for(auto it=primes.begin();it!=primes.end();++it){\n int sum=0;\n for(auto j=it;j!=primes.end();++j){\n sum+=*j;\n if(sum==n){\n ++c;\n break;\n }\n if(sum>n)\n break;\n }\n }\n cout<<c<<endl;\n }\n}", "accuracy": 1, "time_ms": 80, "memory_kb": 3648, "score_of_the_acc": -0.2719, "final_rank": 13 }, { "submission_id": "aoj_1257_10718664", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\nset<int>primes;\nint main(){\n int n;\n while(true){\n cin>>n;\n if(n==0)\n break;\n primes.clear();\n for(int k=2;k<=n;++k)\n primes.insert(k);\n for(auto i=primes.begin();i!=primes.end();++i){\n for(long j=(*i)*2;j<=n;j+=*i){\n if(primes.find(j)!=primes.end()){\n primes.erase(j);\n }\n }\n }\n long c=0;\n for(auto it=primes.begin();it!=primes.end();++it){\n int sum=0;\n for(auto j=it;j!=primes.end();++j){\n sum+=*j;\n if(sum==n){\n ++c;\n break;\n }\n if(sum>n)\n break;\n }\n }\n cout<<c<<endl;\n }\n}", "accuracy": 1, "time_ms": 80, "memory_kb": 3912, "score_of_the_acc": -0.2795, "final_rank": 14 }, { "submission_id": "aoj_1257_7120798", "code_snippet": "#pragma GCC optimize(\"Ofast\")\n#include <iostream>\n#include <vector>\n#include <algorithm>\n#include <map>\n#include <queue>\n#include <cstdio>\n#include <ctime>\n#include <assert.h>\n#include <chrono>\n#include <random>\n#include <numeric>\n#include <set>\n#include <deque>\n#include <stack>\n#include <sstream>\n#include <utility>\n#include <cstring>\n#include <unordered_map>\n#include <unordered_set>\n#include <tuple>\n#include <array>\n#include <bitset>\nusing namespace std;\ntypedef long long int ll;\n\nconst int N = 4000001;\nbool is_not_prime[N];\n\nint main() {\n cin.tie(nullptr);\n ios::sync_with_stdio(false);\n is_not_prime[0] = is_not_prime[1] = true;\n vector<int> p;\n for(int i=2;i<N;i++){\n if(!is_not_prime[i]){\n p.push_back(i);\n for(int j=i+i;j<N;j+=i){\n is_not_prime[j] = true;\n }\n }\n }\n int n;\n while(cin >> n,n){\n int res = 0;\n int sum = 0;\n int r = 0;\n for(int i=0;i<p.size();i++){\n if(p[i] > n) break;\n while(r<p.size() and sum < n){\n sum += p[r]; r++;\n }\n if(sum == n) res++;\n sum -= p[i];\n }\n cout << res << endl;\n }\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 8896, "score_of_the_acc": -0.2238, "final_rank": 12 }, { "submission_id": "aoj_1257_6004400", "code_snippet": "#include <iostream>\n#include <vector>\n#include <queue>\nusing namespace std;\n\nint main() {\n vector<int> prime;\n bool prime_judgement[10000] = {false};\n prime_judgement[0] = true;\n prime_judgement[1] = true;\n for (int i=2; i<10000; i++){\n if (prime_judgement[i] == false) {\n prime.push_back(i);\n for (int j=i+1; j<10000; j++){\n if (j % i == 0) prime_judgement[j] = true;\n }\n }\n }\n int N;\n while (cin >> N && N > 0){\n queue<int> Q;\n int num = 0;\n int sum = 0;\n int count = 0;\n while(true){\n if (sum < N){\n if (num == prime.size()) break;\n Q.push(prime[num]);\n sum += Q.back();\n num += 1;\n }\n else if (sum >= N) {\n if (sum == N) count += 1;\n sum -= Q.front();\n Q.pop();\n }\n }\n cout << count << endl;\n }\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3088, "score_of_the_acc": -0.0557, "final_rank": 4 }, { "submission_id": "aoj_1257_6004396", "code_snippet": "#include <iostream>\n#include <vector>\n#include <queue>\nusing namespace std;\n\nint main() {\n vector<int> prime;\n bool prime_judgement[10000] = {false};\n prime_judgement[0] = true;\n prime_judgement[1] = true;\n for (int i=2; i<10000; i++){\n if (prime_judgement[i] == false) {\n prime.push_back(i);\n for (int j=i+1; j<10000; j++){\n if (j % i == 0) prime_judgement[j] = true;\n }\n }\n }\n int N;\n while (cin >> N && N > 0){\n queue<int> Q;\n int num = 0;\n int sum = 0;\n int count = 0;\n while(true){\n if (sum < N){\n if (num == prime.size()) break;\n Q.push(prime[num]);\n sum += prime[num];\n num += 1;\n }\n else if (sum >= N) {\n if (sum == N) count += 1;\n sum -= Q.front();\n Q.pop();\n }\n }\n cout << count << endl;\n }\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3144, "score_of_the_acc": -0.0573, "final_rank": 6 }, { "submission_id": "aoj_1257_4477747", "code_snippet": "#define _GLIBCXX_DEBUG\n#include <bits/stdc++.h>\nusing namespace std;\n\nbool isPrime(int n){\n for(int i=2; i<n; i++){\n if((n%i)==0) return false;\n }\n return true;\n}\n\nint main() {\n int n; cin>>n;\n while(n != 0){\n //素数列挙\n vector<int> prime;\n for(int i=2; i<=n; i++){\n if(isPrime(i)) prime.push_back(i);\n }\n\n //累積和\n vector<int> c(prime.size()+1);\n for(int i=1; i<c.size(); i++){\n c[i] = c[i-1] + prime[i-1];\n }\n\n int count = 0;\n\n //二分探索\n for(int i=0; i<c.size(); i++){\n int left = i, right = c.size();\n while(left < right){\n int mid = (left+right)/2;\n int sum = c[mid]-c[i];\n if(sum > n) right = mid;\n else left = mid+1;\n if(sum == n){\n count++;\n }\n }\n }\n cout << count << endl;\n cin>>n;\n }\n}", "accuracy": 1, "time_ms": 360, "memory_kb": 3092, "score_of_the_acc": -1.0558, "final_rank": 18 }, { "submission_id": "aoj_1257_4392558", "code_snippet": "#include<iostream>\n#include<list>\n#include<algorithm>\nusing namespace std;\n\nlist<long> L;\n\nbool isprime(long n){\n if (n == 2) return true;\n if (n == 1 || n % 2 == 0) return false;\n long k = 3;\n while( k * k <= n ){\n if (n % k == 0) return false;\n k = k + 2;\n }\n return true;\n}\n\nvoid setprime(){\n L.push_back(2);\n long n = 3;\n while (n < 10010){\n if (isprime(n)) L.push_back(n);\n n += 2;\n }\n return;\n}\n\nint main(){\n setprime();\n long D[L.size() + 2];\n D[0] = 0;\n int i = 1;\n while (!(L.empty())) {\n D[i] = D[i - 1] + *(L.begin());\n L.pop_front(); i++;\n }\n long k;\n while(cin >> k) {\n if (k == 0) return 0;\n int i = 1, x = 0;\n while( D[i] - D[i - 1] <= k ) {\n int j = 0;\n while(j < i){\n if (D[i] - D[j] == k) x++;\n j++;\n }\n i++;\n }\n cout << x << endl;\n }\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3064, "score_of_the_acc": -0.055, "final_rank": 3 }, { "submission_id": "aoj_1257_3896830", "code_snippet": "#include <bits/stdc++.h>\n\n#define M_PI 3.14159265358979323846\n\nusing namespace std;\n\n//conversion\n//------------------------------------------\ninline int toInt(string s) { int v; istringstream sin(s); sin >> v; return v; }\ntemplate<class T> inline string toString(T x) { ostringstream sout; sout << x; return sout.str(); }\ninline int readInt() { int x; scanf(\"%d\", &x); return x; }\n\n//typedef\n//------------------------------------------\ntypedef vector<int> VI;\ntypedef vector<VI> VVI;\ntypedef vector<string> VS;\ntypedef pair<int, int> PII;\ntypedef pair<int, PII> TIII;\ntypedef long long LL;\ntypedef unsigned long long ULL;\ntypedef vector<LL> VLL;\ntypedef vector<VLL> VVLL;\n\n\n//container util\n\n//------------------------------------------\n#define ALL(a) (a).begin(),(a).end()\n#define RALL(a) (a).rbegin(), (a).rend()\n#define PB push_back\n#define MP make_pair\n#define SZ(a) int((a).size())\n#define SQ(a) ((a)*(a))\n#define EACH(i,c) for(typeof((c).begin()) i=(c).begin(); i!=(c).end(); ++i)\n#define EXIST(s,e) ((s).find(e)!=(s).end())\n#define SORT(c) sort((c).begin(),(c).end())\n\n//repetition\n//------------------------------------------\n#define FOR(i,s,n) for(int i=s;i<(int)n;++i)\n#define REP(i,n) FOR(i,0,n)\n#define MOD 1000000007\n\n#define rep(i, a, b) for(int i = a; i < (b); ++i)\n#define trav(a, x) for(auto& a : x)\n#define all(x) x.begin(), x.end()\n#define sz(x) (int)(x).size()\n\n\n\ntypedef long long ll;\ntypedef pair<int, int> pii;\ntypedef vector<int> vi;\nconst double EPS = 1E-8;\n\n#define chmin(x,y) x=min(x,y)\n#define chmax(x,y) x=max(x,y)\nconst int INF = 100000000;\n\nstruct Edge {\n int to, from;\n ll cost;\n Edge(int from, int to, ll cost): from(from), to(to), cost(cost) {}\n};\n\nclass UnionFind {\npublic:\n vector <ll> par; \n vector <ll> siz; \n\n UnionFind(ll sz_): par(sz_), siz(sz_, 1LL) {\n for (ll i = 0; i < sz_; ++i) par[i] = i;\n }\n void init(ll sz_) {\n par.resize(sz_);\n siz.assign(sz_, 1LL);\n for (ll i = 0; i < sz_; ++i) par[i] = i;\n }\n\n ll root(ll x) { \n while (par[x] != x) {\n x = par[x] = par[par[x]];\n }\n return x;\n }\n\n bool merge(ll x, ll y) {\n x = root(x);\n y = root(y);\n if (x == y) return false;\n if (siz[x] < siz[y]) swap(x, y);\n siz[x] += siz[y];\n par[y] = x;\n return true;\n }\n\n bool issame(ll x, ll y) { \n return root(x) == root(y);\n }\n\n ll size(ll x) { \n return siz[root(x)];\n }\n};\n\ntypedef vector<vector<Edge>> AdjList;\nAdjList graph;\n\nll mod_pow(ll x, ll n, ll mod){\n ll res = 1;\n bool c = false;\n while(n){\n if(n&1) res = res * x;\n\n if(res > mod){\n c = true;\n res %= mod;\n }\n x = x * x %mod;\n n >>= 1;\n }\n if(c) return mod;\n return res;\n}\n\n#define SIEVE_SIZE 5000000+10\nbool sieve[SIEVE_SIZE];\nvoid make_sieve(){\n for(int i=0; i<SIEVE_SIZE; ++i) sieve[i] = true;\n sieve[0] = sieve[1] = false;\n for(int i=2; i*i<SIEVE_SIZE; ++i) if(sieve[i]) for(int j=2; i*j<SIEVE_SIZE; ++j) sieve[i*j] = false;\n}\n\nbool isprime(ll n){\n if(n == 0 || n == 1) return false;\n for(ll i=2; i*i<=n; ++i) if(n%i==0) return false;\n return true;\n}\n\ntemplate<typename T>\nvector<T> gauss_jordan(const vector<vector<T>>& A, const vector<T>& b){\n int n = A.size();\n vector<vector<T>> B(n, vector<T>(n+1));\n\n for(int i=0; i<n; ++i){\n for(int j=0; j<n; ++j){\n B[i][j] = A[i][j];\n }\n }\n\n for(int i=0; i<n; ++i) B[i][n] = b[i];\n\n for(int i=0; i<n; ++i){\n int pivot = i;\n for(int j=i; j<n; ++j){\n if(abs(B[j][i]) > abs(B[pivot][i])) pivot = j;\n }\n swap(B[i], B[pivot]);\n\n if(abs(B[i][i]) < EPS) return vector<T>(); //解なし\n\n for(int j=i+1; j<=n; ++j) B[i][j] /= B[i][i];\n for(int j=0; j<n; ++j){\n if(i != j){\n for(int k=i+1; k<=n; ++k) B[j][k] -= B[i][j] * B[i][k];\n }\n }\n }\n\n vector<T> x(n);\n\n for(int i=0; i<n; ++i) x[i] = B[i][n];\n return x;\n \n}\n\nll GCD(ll a, ll b){\n if(a<b) swap(a,b);\n if(b == 0) return a;\n return GCD(b, a%b);\n}\n\n\ntypedef vector<ll> vec;\ntypedef vector<vec> mat;\n\nmat mul(mat &A, mat &B) {\n mat C(A.size(), vec((int)B[0].size()));\n for(int i=0; i<A.size(); ++i){\n for(int k=0; k<B.size(); ++k){\n for(int j=0; j<B[0].size(); ++j){\n C[i][j] = (C[i][j] + A[i][k] * B[k][j] %MOD) % MOD;\n }\n }\n }\n return C;\n}\nmat mat_pow(mat A, ll n) {\n mat B(A.size(), vec((int)A.size()));\n\n for(int i=0; i<A.size(); ++i){\n B[i][i] = 1;\n }\n\n while(n > 0) {\n if(n & 1) B = mul(B, A);\n A = mul(A, A);\n n >>= 1;\n }\n return B;\n}\n\nbool operator<(const pii& a, const pii& b){\n if(a.first == b.first) return a.second < b.second;\n return a.first < b.first;\n}\n\nconst int MAX = 510000;\nlong long fac[MAX], finv[MAX], inv[MAX];\n\n// テーブルを作る前処理\nvoid COMinit() {\n fac[0] = fac[1] = 1;\n finv[0] = finv[1] = 1;\n inv[1] = 1;\n for (int i = 2; i < MAX; i++){\n fac[i] = fac[i - 1] * i % MOD;\n inv[i] = MOD - inv[MOD%i] * (MOD / i) % MOD;\n finv[i] = finv[i - 1] * inv[i] % MOD;\n }\n}\n\n// 二項係数計算\nlong long COM(int n, int k){\n if (n < k) return 0;\n if (n < 0 || k < 0) return 0;\n return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD;\n}\n\nint bit[1000010];\nint sum(int i){\n int s = 0;\n while(i > 0){\n s += bit[i];\n i -= i & -i;\n }\n return s;\n}\nvoid add(int i, int x){\n while(i <= 1000010){\n bit[i] += x;\n i += i & -i;\n }\n}\nlong long extGCD(long long a, long long b, long long &x, long long &y) {\n if (b == 0) {\n x = 1;\n y = 0;\n return a;\n }\n long long d = extGCD(b, a%b, y, x);\n y -= a/b * x;\n return d;\n}\n\nint main() {\n cin.tie(0);\n ios::sync_with_stdio(false);\n cout << fixed << setprecision(12);\n\n make_sieve();\n vector<int> primes;\n for(int i=0; i<=10000; i++) if(sieve[i]) primes.PB(i);\n\n int len = primes.size();\n\n int n;\n while(cin >> n, n){\n int left = 0, right = 0;\n int sum = 0;\n\n int ans = 0;\n for(; left <len; left++){\n while(right < len && sum < n){\n sum += primes[right];\n right++;\n }\n\n if(sum == n) ans++;\n\n if(right == left) right++;\n else{\n sum -= primes[left];\n }\n }\n cout << ans << endl;\n }\n return 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 8044, "score_of_the_acc": -0.1991, "final_rank": 11 }, { "submission_id": "aoj_1257_3461617", "code_snippet": "#include<iostream>\n#include<algorithm>\n#include<vector>\n#include<string>\n#include<queue>\n#include<stack>\n#define int long long\nusing namespace std;\nvector<int>p, ps;\nbool b[5736397];\nvoid f(int k) {\n\tp.push_back(k);\n\tps.push_back(ps.back() + k);\n\tb[ps.back()] = 1;\n\treturn;\n}\nsigned main() {\n\tps.push_back(0); b[0] = 1;\n\tf(2);\n\tfor (int i = 3; i <= 10000; i += 2) {\n\t\tbool b00l = 1;\n\t\tfor (int j = 0; j < p.size(); j++) \n\t\t\tif (i%p[j] == 0)b00l = 0;\n\t\tif (b00l)f(i);\n\t}\n\tint n;\n\twhile (cin >> n, n) {\n\t\tint ans = 0;\n\t\tfor (int i = 0; i < ps.size(); i++) {\n\t\t\tif (ps[i] - n < 0)continue;\n\t\t\tif (b[ps[i] - n])ans++;\n\t\t}\n\t\tcout << ans << endl;\n\t}\n}", "accuracy": 1, "time_ms": 20, "memory_kb": 6888, "score_of_the_acc": -0.1942, "final_rank": 10 }, { "submission_id": "aoj_1257_3407319", "code_snippet": "#include<iostream>\n#include<vector>\n#include<cmath>\nusing namespace std;\n\nbool judgePrimeNumber( int a )\n{\n for( int i = 2; i <= sqrt(a); i++ )\n if( a % i == 0 )\n return false;\n return true;\n}\n\nint main()\n{\n int n, a[10001];\n vector<int> p;\n\n for( int i = 2; i < 10000; i++ )\n if( judgePrimeNumber(i) )\n p.push_back(i);\n\n for( int i = 0; i < 10001; i++ )\n a[i] = 0;\n\n int pluscnt = 1;\n while( pluscnt <= p.size() )\n {\n for( int i = 0; i < p.size(); i++ )\n {\n int sum = 0;\n for( int j = i; j < p.size() && j < pluscnt + i; j++ )\n sum += p[j];\n if( sum <= 10000 )\n a[sum]++;\n }\n pluscnt++;\n }\n\n while( cin >> n )\n {\n if( n == 0 )\n break;\n cout << a[n] << endl;\n }\n return 0;\n}", "accuracy": 1, "time_ms": 270, "memory_kb": 3192, "score_of_the_acc": -0.8015, "final_rank": 17 }, { "submission_id": "aoj_1257_2592911", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\ntypedef long long int ll;\nbool is_prime(int n){\n\tif(n <= 1) return false;\n\tif(n == 2) return true;\n\tif(n % 2 == 0) return false;\n\tfor(int i = 3; i * i <= n; i++){\n\t\tif(n % i == 0) return false;\n\t}\n\treturn true;\n}\nint main(){\n\tint n;\n\tvector<int> prime_numbers;\n\tfor(int i = 1; i < 10000; i++){\n\t\tif(is_prime(i)) prime_numbers.push_back(i);\n\t}\n\tvector<ll> cumulative_sum;\n\tll sum = 0;\n\tfor(int i = 0; i < prime_numbers.size(); i++){\n\t\tcumulative_sum.push_back(sum);\n\t\tsum += prime_numbers[i];\n\t}\n\tcumulative_sum.push_back(sum);\n\tfor(;;){\n\t\tcin >> n;\n\t\tif(n == 0) break;\n\t\tint ans = 0;\n\t\tfor(int i = 0; i < cumulative_sum.size(); i++){\n\t\t\tfor(int j = 0; j <= i; j++){\n\t\t\t\tif(cumulative_sum[i] - cumulative_sum[j] == n) ans++;\n\t\t\t}\n\t\t}\n\t\tcout << ans << endl;\n\t}\n\treturn 0;\n}", "accuracy": 1, "time_ms": 40, "memory_kb": 3128, "score_of_the_acc": -0.1425, "final_rank": 9 }, { "submission_id": "aoj_1257_2590744", "code_snippet": "#include <iostream>\n#include <algorithm>\n#include <vector>\nusing namespace std;\n\nvector<int> v;\n\nbool isPrime(int n) {\n for (int i = 0; i < v.size() && v[i] * v[i] <= n; i++) {\n if (n % v[i] == 0) return false;\n }\n return true;\n}\n\nint main() {\n v.push_back(2);\n v.push_back(3);\n for (int i = 5; i <= 10000; i += 2) {\n if (isPrime(i)) {\n v.push_back(i);\n }\n }\n \n int sum[v.size() + 1];\n fill(sum, sum + v.size() + 1, 0);\n for (int i = 1; i < v.size() + 1; i++) {\n sum[i] = sum[i - 1] + v[i - 1];\n }\n \n while (true) {\n int n;\n cin >> n;\n if (n == 0) return 0;\n int count = 0;\n for (int i = 1; i < v.size() + 1; i++) {\n if (v[i - 1] > n) break;\n for (int j = 0; j <= i; j++) {\n if (sum[i] - sum[j] == n) {\n count++;\n }\n }\n }\n cout << count << endl;\n }\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3052, "score_of_the_acc": -0.0546, "final_rank": 2 }, { "submission_id": "aoj_1257_2579769", "code_snippet": "#include<stdio.h>\n#include<vector>\n#include<queue>\n#include<iostream>\n\nusing namespace std;\n\nint main() {\n int numbers[10010];\n vector<int> prime_number;\n int num, sum = 0;\n for(int i = 2; i <= 10000; i++) numbers[i-2] = i;\n for(int i = 2; i <= 10000; i++) {\n int p = numbers[i-2];\n if(p != 0) {\n for(int j = i+1; j <= 10000; j++) if(numbers[j-2]%p == 0) numbers[j-2] = 0;\n }\n }\n for(int i = 2; i <= 10000; i++) {\n if(numbers[i-2] != 0) prime_number.push_back(numbers[i-2]);\n }\n while(true) {\n int ans = 0;\n sum = 0;\n scanf(\"%d\", &num);\n if(num == 0) break;\n queue<int> q;\n int i = 0;\n do {\n if(sum < num) {\n\tq.push(prime_number[i]);\n\tsum += prime_number[i];\n\ti++;\n }\n else if(sum > num) {\n\tsum -= q.front();\n\tq.pop();\n }\n else {\n\tans++;\n\tsum -= q.front();\n\tq.pop();\n\tq.push(prime_number[i]);\n\tsum += prime_number[i];\n\ti++;\n }\n }while(prime_number[i-1] <= num || i <= prime_number.size()-1);\n printf(\"%d\\n\", ans);\n }\n\n return 0;\n}", "accuracy": 1, "time_ms": 20, "memory_kb": 3224, "score_of_the_acc": -0.0882, "final_rank": 7 }, { "submission_id": "aoj_1257_2380910", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\nbool s[10000000];\nint main(){\n s[0]=s[1]=1;\n for(int i=2;i<10000000;i++)if(!s[i])\n for(int j=i+i;j<10000000;j+=i)s[j]=1;\n int n;\n while(cin>>n,n){\n int sum=0,c=0;\n for(int i=0;i<=n;i++){\n sum=0;\n if(!s[i])for(int j=i;j<n*10;j++){\n if(!s[j])sum+=j;\n if(sum==n)c++;\n if(sum>=n)break;\n }\n }\n cout<<c<<endl;\n }\n}", "accuracy": 1, "time_ms": 90, "memory_kb": 12844, "score_of_the_acc": -0.5666, "final_rank": 16 }, { "submission_id": "aoj_1257_2170261", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\nint a[10009];\nvector<int> p;\nmap<int,int> m;\n\nvoid sieve()\n{\n int i,j,t1,t2;\n for(i=0;i<10003;i++)\n a[i]=1;\n a[0]=a[1]=0;\n for(i=2;i<=10000;i++)\n {\n if(a[i]==1)\n {\n p.push_back(i);\n for(j=2*i;j<=10000;j+=i)\n {\n a[j]=0;\n }\n }\n }\n for(i=0;i<p.size();i++)\n {\n t1=0;\n for(j=i;j<p.size();j++)\n {\n t1+=p[j];\n m[t1]++;\n }\n }\n return;\n}\n\n\nint main()\n{\n int i,j,t1,t2,n,ans;\n sieve();\n while(1)\n {\n scanf(\"%d\",&n);\n if(n==0)\n break;\n ans=m[n];\n printf(\"%d\\n\",ans);\n }\n return 0;\n}", "accuracy": 1, "time_ms": 270, "memory_kb": 35720, "score_of_the_acc": -1.7429, "final_rank": 19 }, { "submission_id": "aoj_1257_2095835", "code_snippet": "#include <stdio.h>\n#include <algorithm>\n#include <iostream>\n#include <stack>\n#include <vector>\n#include <math.h>\n#include <queue>\n\nusing namespace std;\n\n#define NUM 11000\n\nint main(){\n\tint table[NUM],limit,numTable[10001] = {0};\n\n\tfor(int i=0; i < NUM;i++)table[i] = 1;\n\ttable[0] = 0;\n\ttable[1] = 0;\n\n\tlimit = sqrt(NUM);\n\n\tfor(int i=2;i<=limit;i++){\n\t\tif(table[i] == 1){\n\t\t\tfor(int k=2*i;k < NUM; k += i){\n\t\t\t\ttable[k] = 0;\n\t\t\t}\n\t\t}\n\t}\n\n\tint index = 0,primeTable[1230];\n\n\tfor(int i = 2; i <= 9997; i++){\n\t\tif(table[i] == 1)primeTable[index++] = i;\t//?´???°????¨????\n\t}\n\n\tint sum;\n\tfor(int left = 0; left <= index-1; left++){ //??????????????????\n\t\tfor(int div = 0; left + div <= index-1; div++){ //?????????????¶?????´???°????????°????????????\n\t\t\tsum = 0;\n\t\t\tfor(int p = 0; p <= div; p++){\n\t\t\t\tsum += primeTable[left+p];\n\t\t\t}\n\t\t\tif(sum <= 10000)numTable[sum]++;\n\t\t}\n\t}\n\n\tint n;\n\n\twhile(true){\n\t\tscanf(\"%d\",&n);\n\t\tif(n == 0)break;\n\n\t\tprintf(\"%d\\n\",numTable[n]);\n\t}\n\treturn 0;\n}", "accuracy": 1, "time_ms": 110, "memory_kb": 3280, "score_of_the_acc": -0.3469, "final_rank": 15 }, { "submission_id": "aoj_1257_1973764", "code_snippet": "#include \"bits/stdc++.h\"\nusing namespace std;\ntypedef long long ll;\ntypedef pair<int,int> pii;\n#define rep(i,n) for(ll i=0;i<(ll)(n);i++)\n#define all(a) (a).begin(),(a).end()\n#define vi vector<int>\n#define pb push_back\n#define INF 999999999\n//#define INF (1LL<<59)\n\nbool era[100001]={false};\n\nvoid makeERA(){\n\tfor(int i=2;i<100001;i++){\n\t\tif(era[i]==true){\n\t\t\tint x=100000/i;\n\t\t\tfor(int j=2;j<=x;j++){\n\t\t\t\tera[i*j]=false;\n\t\t\t}\n\t\t}\n\t}\n\tera[2]=true;\n}\n\n\nint main(){\n\tfor(int i=0;i<100001;i++)era[i]=true;\n\tera[0]=false;era[1]=false;\n\tmakeERA();\n\t\n\tvector<int> pr;\n\tpr.pb(0);\n\trep(i,10001)if(era[i])pr.pb(i);\n\n\tfor(int i=1;i<pr.size();i++){\n\t\tpr[i] +=pr[i-1];\n\t}\n\n\tint n;\n\twhile(cin>>n&&n){\n\t\tint ans=0;\n\t\trep(i,pr.size()){\n\t\t\tfor(int j=i+1;j<pr.size();j++){\n\t\t\t\tif( pr[j]-pr[i]==n )ans++;\n\t\t\t}\n\t\t}\n\t\tcout<<ans<<endl;\n\t}\n}", "accuracy": 1, "time_ms": 40, "memory_kb": 3112, "score_of_the_acc": -0.1421, "final_rank": 8 }, { "submission_id": "aoj_1257_1973345", "code_snippet": "#include<bits/stdc++.h>\n#define range(i,a,b) for(int i = (a); i < (b); i++)\n#define rep(i,b) range(i,0,b)\n#define all(a) (a).begin(), (a).end()\n#define debug(x) cout << \"debug \" << x << endl;\n#define INF (1 << 30)\nusing namespace std;\n\nconst int kN = 11000;\n\nvoid primeNumber(bool prime[kN], vector<int> &p){\n rep(i,kN) prime[i] = 1;\n prime[0] = prime[1] = 0;\n rep(i,kN){\n if(prime[i]){\n p.push_back(i);\n for(int j = i + i; j < kN; j+=i){\n prime[j] = 0;\n }\n }\n }\n}\n\nint main(){\n bool prime[kN];\n vector<int> p;\n primeNumber(prime, p);\n\n int n;\n while(cin >> n, n){\n int ans = 0;\n for(int i = 0; p[i] <= n; i++){\n int sum = 0;\n for(int j = i; p[j] <= n; j++){\n sum+=p[j];\n if(sum == n){\n ans++;\n break;\n }\n }\n }\n cout << ans << endl;\n }\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 1240, "score_of_the_acc": -0.0022, "final_rank": 1 }, { "submission_id": "aoj_1257_1923561", "code_snippet": "#include<iostream>\n#include<algorithm>\nusing namespace std;\nint prime[2000];\nint main(){\n\tint top=0;\n\tfor(int i=2;i<10000;i++){\n\t\tbool ok=true;\n\t\tfor(int j=0;j<top;j++)if(i%prime[j]==0)ok=false;\n\t\tif(ok){\n\t\t\tprime[top]=i;\n\t\t\ttop++;\n\t\t}\n\t}\n\tint n;\n\twhile(true){\n\t\tcin>>n;\n\t\tif(n==0)break;\n\t\tint ans=0;\n\t\tfor(int i=0;i<top;i++){\n\t\t\tint cnt=prime[i];\n\t\t\tint k=i+1;\n\t\t\twhile(true){\n\t\t\t\tif(cnt==n)ans++;\n\t\t\t\tif(cnt>=n)break;\n\t\t\t\tif(k==top)break;\n\t\t\t\tcnt+=prime[k];\n\t\t\t\tk++;\n\t\t\t}\n\t\t}\n\t\tcout<<ans<<endl;\n\t}\n\treturn 0;\n}", "accuracy": 1, "time_ms": 30, "memory_kb": 1164, "score_of_the_acc": -0.0571, "final_rank": 5 }, { "submission_id": "aoj_1257_1892255", "code_snippet": "#include<iostream>\n#include<vector>\n#include<string>\n#include<algorithm>\t\n#include<map>\n#include<set>\n#include<utility>\n#include<cmath>\n#include<cstring>\n#include<queue>\n#include<cstdio>\n#include<sstream>\n#include<iomanip>\n#define loop(i,a,b) for(int i=a;i<b;i++) \n#define rep(i,a) loop(i,0,a)\n#define pb push_back\n#define mp make_pair\n#define all(in) in.begin(),in.end()\n#define shosu(x) fixed<<setprecision(x)\nusing namespace std;\n//kaewasuretyuui\ntypedef long long ll;\ntypedef pair<int,int> pii;\ntypedef vector<int> vi;\ntypedef vector<vi> vvi;\ntypedef vector<pii> vp;\ntypedef vector<vp> vvp;\ntypedef pair<int,pii> pip;\ntypedef vector<pip>vip;\nconst double PI=acos(-1);\nconst double EPS=1e-8;\nconst int inf=1e8;\nint main(){\n\tint sosu[10010]={1,1,0};\n\tfor(int i=2;i<10010;i++)if(sosu[i]==0)\n\tfor(int j=i*2;j<10010;j+=i)sosu[j]=true;\n\t\n\tvi dp(1);\n\trep(i,10010)if(sosu[i]==0)dp.pb(i);\n\trep(i,dp.size())dp[i+1]+=dp[i];\n\tmap<int,int>m;\n\trep(i,dp.size())loop(j,i+1,dp.size())m[dp[j]-dp[i]]++;\n\tint n;\n\twhile(cin>>n,n){\n\t\tcout<<m[n]<<endl;\n\t}\n}", "accuracy": 1, "time_ms": 290, "memory_kb": 33760, "score_of_the_acc": -1.7433, "final_rank": 20 } ]
aoj_1263_cpp
Problem G: Network Mess Gilbert is the network admin of Ginkgo company. His boss is mad about the messy network cables on the floor. He finally walked up to Gilbert and asked the lazy network admin to illustrate how computers and switches are connected. Since he is a programmer, he is very reluctant to move throughout the office and examine cables and switches with his eyes. He instead opted to get this job done by measurement and a little bit of mathematical thinking, sitting down in front of his computer all the time. Your job is to help him by writing a program to reconstruct the network topology from measurements. There are a known number of computers and an unknown number of switches. Each computer is connected to one of the switches via a cable and to nothing else. Specifically, a computer is never connected to another computer directly, or never connected to two or more switches. Switches are connected via cables to form a tree (a connected undirected graph with no cycles). No switches are ‘useless.’ In other words, each switch is on the path between at least one pair of computers. All in all, computers and switches together form a tree whose leaves are computers and whose internal nodes switches (See Figure 9). Gilbert measures the distances between all pairs of computers . The distance between two com- puters is simply the number of switches on the path between the two, plus one. Or equivalently, it is the number of cables used to connect them. You may wonder how Gilbert can actually obtain these distances solely based on measurement. Well, he can do so by a very sophisticated statistical processing technique he invented. Please do not ask the details. You are therefore given a matrix describing distances between leaves of a tree. Your job is to construct the tree from it. Input The input is a series of distance matrices, followed by a line consisting of a single ' 0 '. Each distance matrix is formatted as follows. N a 11 a 12 ... a 1 N a 21 a 22 ... a 2 N . . . . . . . . . . . . a N 1 a N 2 ... a N N N is the size, i.e. the number of rows and the number of columns, of the matrix. a ij gives the distance between the i -th leaf node (computer) and the j -th. You may assume 2 ≤ N ≤ 50 and the matrix is symmetric whose diagonal elements are all zeros. That is, a ii = 0 and a ij = a ji for each i and j . Each non-diagonal element a ij ( i ≠ j ) satisfies 2 ≤ a ij ≤ 30. You may assume there is always a solution. That is, there is a tree having the given distances between leaf nodes. Output For each distance matrix, find a tree having the given distances between leaf nodes. Then output the degree of each internal node (i.e. the number of cables adjoining each switch), all in a single line and in ascending order. Numbers in a line should be separated by a single space. A line should not contain any other characters, including trailing spaces. Sample Input 4 0 2 2 2 2 0 2 2 2 2 0 2 2 2 2 0 4 0 ...(truncated)
[ { "submission_id": "aoj_1263_1352467", "code_snippet": "#include<bits/stdc++.h>\n\n#define REP(i,s,n) for(int i=s;i<n;i++)\n#define rep(i,n) REP(i,0,n)\n\nusing namespace std;\n\nconst int MAX_V = 50 * 30;\nconst int IINF = INT_MAX;\n\nstruct Edge { int src,dst; };\nvector<Edge> G[MAX_V];\nvector<int> computer;\nint n,V,mat[61][61];\n\nvoid dfs(int cur,int prev,int depth,vector<int> &mindist){\n\n rep(i,computer.size()) {\n if( computer[i] == -1 ) break;\n assert( i < mindist.size() );\n if( cur == computer[i] ) { assert( mindist[i] == IINF); mindist[i] = depth; }\n }\n\n rep(i,G[cur].size()){\n int next = G[cur][i].dst;\n if( next == prev ) continue;\n dfs(next,cur,depth+1,mindist);\n }\n\n}\n\nvoid compute(){\n computer.clear();\n computer.resize(n,-1);\n computer[0] = 0;\n V = 1;\n REP(i,1,n) {\n bool update = false; // for debug\n rep(candidate,V) {\n vector<int> mindist(i,IINF);\n dfs(candidate,-1,0,mindist);\n assert( mindist[0] != IINF );\n int add_length = -1;\n rep(j,i){\n int length = mat[i][j] - mindist[j];\n if( length <= 0 ) { add_length = -1; break; }\n if( add_length == -1 ) add_length = length;\n else if( add_length != length ) { add_length = -1; break; }\n }\n if( add_length != -1 ) {\n // add_length ????????????????????????\n // ????????????????????????????????? i ?????????????????\\??????\n int prev = candidate;\n rep(k,add_length){\n G[prev].push_back((Edge){prev,V});\n G[V].push_back((Edge){V,prev});\n prev = V;\n ++V;\n }\n computer[i] = V-1;\n update = true;\n break;\n }\n }\n assert( update );\n }\n set<int> S;\n rep(i,n) S.insert(computer[i]);\n vector<int> answer;\n rep(i,V) if( !S.count(i) ) answer.push_back(G[i].size());\n sort(answer.begin(),answer.end());\n rep(i,answer.size()){\n if( i ) printf(\" \");\n printf(\"%d\",answer[i]);\n } puts(\"\");\n}\n\nint main(){\n while( scanf(\"%d\",&n), n ){\n rep(i,MAX_V) G[i].clear();\n rep(i,n) rep(j,n) scanf(\"%d\",&mat[i][j]);\n compute();\n }\n return 0;\n}", "accuracy": 1, "time_ms": 30, "memory_kb": 1308, "score_of_the_acc": -0.0877, "final_rank": 2 }, { "submission_id": "aoj_1263_1352466", "code_snippet": "#include<bits/stdc++.h>\n\n#define REP(i,s,n) for(int i=s;i<n;i++)\n#define rep(i,n) REP(i,0,n)\n\nusing namespace std;\n\nconst int MAX_V = 50 * 50 * 30;\nconst int IINF = INT_MAX;\n\nstruct Edge { int src,dst; };\nvector<Edge> G[MAX_V];\nvector<int> computer;\nint n,V,mat[61][61];\n\nvoid dfs(int cur,int prev,int depth,vector<int> &mindist){\n\n rep(i,computer.size()) {\n if( computer[i] == -1 ) break;\n assert( i < mindist.size() );\n if( cur == computer[i] ) { assert( mindist[i] == IINF); mindist[i] = depth; }\n }\n\n rep(i,G[cur].size()){\n int next = G[cur][i].dst;\n if( next == prev ) continue;\n dfs(next,cur,depth+1,mindist);\n }\n\n}\n\nvoid compute(){\n computer.clear();\n computer.resize(n,-1);\n computer[0] = 0;\n V = 1;\n REP(i,1,n) {\n bool update = false; // for debug\n rep(candidate,V) {\n vector<int> mindist(i,IINF);\n dfs(candidate,-1,0,mindist);\n assert( mindist[0] != IINF );\n int add_length = -1;\n rep(j,i){\n int length = mat[i][j] - mindist[j];\n if( length <= 0 ) { add_length = -1; break; }\n if( add_length == -1 ) add_length = length;\n else if( add_length != length ) { add_length = -1; break; }\n }\n if( add_length != -1 ) {\n // add_length ????????????????????????\n // ????????????????????????????????? i ?????????????????\\??????\n int prev = candidate;\n rep(k,add_length){\n G[prev].push_back((Edge){prev,V});\n G[V].push_back((Edge){V,prev});\n prev = V;\n ++V;\n }\n computer[i] = V-1;\n update = true;\n break;\n }\n }\n assert( update );\n }\n set<int> S;\n rep(i,n) S.insert(computer[i]);\n vector<int> answer;\n rep(i,V) if( !S.count(i) ) answer.push_back(G[i].size());\n sort(answer.begin(),answer.end());\n rep(i,answer.size()){\n if( i ) printf(\" \");\n printf(\"%d\",answer[i]);\n } puts(\"\");\n}\n\nint main(){\n while( scanf(\"%d\",&n), n ){\n rep(i,MAX_V) G[i].clear();\n rep(i,n) rep(j,n) scanf(\"%d\",&mat[i][j]);\n compute();\n }\n return 0;\n}", "accuracy": 1, "time_ms": 30, "memory_kb": 3032, "score_of_the_acc": -0.1893, "final_rank": 4 }, { "submission_id": "aoj_1263_1351976", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\nint cost[2500*30][52];\nint cnt = 0;\nint N;\nint A[52][51];\nvector<int>G[2500*30];\nvoid connect(int x,int y,int num){\n int p = x;\n for(int i=0;i<num-1;i++){\n G[p].push_back( cnt );\n G[cnt].push_back( p );\n p = cnt++;\n }\n G[p].push_back( y );\n G[y].push_back( p );\n}\n\nvoid update(int id){\n queue<int> q;\n q.push( id );\n cost[id][id] = 0;\n while( !q.empty() ){\n int p = q.front(); q.pop();\n for(int i=0;i<(int)G[p].size();i++){\n int to = G[p][i];\n if( cost[to][id] == -1 ) {\n cost[to][id] = cost[p][id]+1;\n // cout <<\"cost \" << to << \" \"<<id<< \" \"<< cost[to][id] << endl;\n q.push( to );\n }\n }\n }\n}\n\nint check(int x,int y){\n int c = A[x][0];\n int pc = cost[y][0];\n int comp = c - pc;\n // cout << \"check \" << x << \" \"<< y << endl;\n // cout << c << \" \"<< pc << \" \" << comp << endl;\n if( pc < 0 || comp < 1 ) return 0;\n for(int i=1;i<x;i++){\n c = A[x][i];\n pc = cost[y][i];\n //cout << i << \" : \" << c << \" \"<< pc << \" \" << c - pc << endl;\n if( comp != c - pc ) return 0;\n }\n return comp;\n}\n\nvoid solve(){\n connect(0,1,A[0][1]);\n for(int i=2;i<N;i++){\n memset(cost,-1,sizeof(cost));\n for(int j=0;j<i;j++) update(j);\n for(int j=N;j<cnt;j++){\n int sc = check(i,j);\n if( sc ) {\n connect(i,j,sc);\n break;\n }\n }\n }\n}\n\nint main(){\n while(cin >> N&&N){\n for(int i=0;i<N;i++)\n for(int j=0;j<N;j++)\n cin >> A[i][j]; \n cnt = N;\n solve();\n vector<int> ans;\n\n /* \n cout << \"debug \" << endl;\n for(int i=0;i<cnt;i++){\n cout << \"node : \" << i << endl;\n for(int j=0;j<(int)G[i].size();j++){\n cout << \" - > \" << G[i][j] << endl;\n }\n }\n */\n\n for(int i=0;i<N;i++) G[i].clear();\n for(int i=N;i<cnt;i++){\n ans.push_back(G[i].size());\n G[i].clear();\n }\n sort(ans.begin(),ans.end());\n for(int i=0;i<(int)ans.size();i++){\n if( i ) cout << \" \";\n cout << ans[i];\n }\n cout << endl;\n }\n\n}", "accuracy": 1, "time_ms": 240, "memory_kb": 18260, "score_of_the_acc": -2, "final_rank": 6 }, { "submission_id": "aoj_1263_812026", "code_snippet": "#include <bits/stdc++.h>\n#define REP(i,n) for(int i=0; i<(int)(n); ++i)\n\nusing namespace std;\n\ntypedef vector<int> Node;\ntypedef vector<Node> Graph;\n\nconst int INF = 1000000000;\n\nint add(Graph& G, int u){\n int v = G.size();\n G[u].push_back(v);\n G.push_back(Node());\n G[v].push_back(u);\n return v;\n}\nvoid bellman_ford(const Graph& G, int s, int dist[1000]){\n dist[s] = 0;\n bool update = true;\n while(update){\n update = false;\n for(int i = 0; i < G.size(); i++){\n for(int j = 0; j < G[i].size(); j++){\n int k = G[i][j];\n if(dist[k] > dist[i] + 1){\n dist[k] = dist[i] + 1;\n update = true;\n }\n }\n }\n }\n}\n\nint main(){\n int N;\n while(cin >> N && N){\n int d[100][100] = {};\n int dist[100][1000] = {};\n REP(i, 100) fill(dist[i], dist[i] + 1000, INF);\n int start[100] = {};\n REP(i, N) REP(j, N) cin >> d[i][j];\n REP(i, N) REP(j, N) d[i][j] -= 2;\n\n Graph G;\n G.push_back(Node());\n start[0] = 0;\n dist[0][0] = 0;\n\n for(int i = 1; i < N; i++){\n vector<int> v;\n for(int j = 0; j < i; j++){\n v.push_back(d[i][j]);\n }\n\n int u = -1;\n int len = -1;\n for(int j = 0; j < G.size(); j++){\n set<int> s;\n for(int k = 0; k < v.size(); k++){\n s.insert(v[k] - dist[k][j]);\n }\n if(s.size() == 1){\n u = j;\n len = *s.begin();\n }\n }\n\n assert(u != -1);\n assert(len >= 0);\n while(len--){\n u = add(G, u);\n }\n start[i] = u;\n //printf(\"start[%d] = %d\\n\", i, u);\n\n for(int j = 0; j <= i; j++){\n bellman_ford(G, start[j], dist[j]);\n }\n }\n vector<int> ans;\n for(int i = 0; i < G.size(); i++){\n ans.push_back(G[i].size());\n }\n for(int i = 0; i < N; i++){\n ans[ start[i] ]++;\n }\n sort(ans.begin(), ans.end());\n for(int i = 0; i < ans.size(); i++){\n cout << ans[i];\n if(i == ans.size() - 1) cout << endl;\n else cout << \" \";\n }\n }\n return 0;\n}", "accuracy": 1, "time_ms": 30, "memory_kb": 1720, "score_of_the_acc": -0.112, "final_rank": 3 }, { "submission_id": "aoj_1263_645081", "code_snippet": "#include<iostream>\n#include<vector>\n#include<queue>\n#include<algorithm>\nusing namespace std;\n#define MAX 50\n#define NMAX 80\n\nclass Node{\n public:\n vector<int> adjList;\n Node(){}\n void insert( int i ){ adjList.push_back(i);}\n};\n\nclass Network{\n public:\n vector<Node> nodes;\n Network(){}\n\n int size(){ return nodes.size(); }\n void addNode(){ nodes.push_back(Node()); }\n\n void connect( int i, int j ){\n nodes[i].insert(j); nodes[j].insert(i);\n }\n\n void addNewNodes( int target, int diff, int u ){\n if ( diff == 1 ){\n connect( target, u );\n } else {\n for ( int i = 1; i < diff; i++ ){\n addNode();\n if ( i == 1 ) connect( target, size()-1 );\n else connect( size()-2, size()-1);\n }\n connect( size()-1, u );\n }\n }\n};\n\nclass State{\n public:\n int cur, pre, d;\n State(){}\n State(int cur, int pre, int d): cur(cur), pre(pre), d(d){}\n};\n\nint N, M[MAX][MAX];\nNetwork network;\n\nint getDistance(int s, int t){\n queue<State> Q;\n Q.push(State(s, -1, 0));\n State u;\n while( !Q.empty() ){\n u = Q.front(); Q.pop();\n if ( u.cur == t ) return u.d;\n for ( int i = 0; i < network.nodes[u.cur].adjList.size(); i++ ){\n int v = network.nodes[u.cur].adjList[i];\n if ( v != u.pre ){\n Q.push(State(v, u.cur, u.d + 1));\n }\n }\n }\n}\n\nint parse( int source, int end ){\n int pre = M[end+1][0] - getDistance( source, 0 );\n int d;\n for ( int t = 1; t <= end; t++ ){\n d = M[end+1][t] - getDistance( source, t );\n if ( pre != d ) return -1;\n }\n return pre;\n}\n\nvoid addNewNode( int u ){\n for ( int target = N; target < network.size(); target++ ){\n int diff = parse(target, u - 1);\n if ( diff > 0 ) {\n network.addNewNodes(target, diff, u);\n return;\n }\n }\n}\n\nvoid solve(){\n network = Network();\n for ( int i = 0; i < N + 1; i++ ) network.addNode();\n network.connect( 0, network.size()-1);\n\n for ( int i = 1; i < N; i++ ) addNewNode(i);\n\n vector<int> degree;\n for ( int i = N; i < network.size(); i++ ) {\n degree.push_back(network.nodes[i].adjList.size());\n }\n sort(degree.begin(), degree.end());\n for ( int i = 0; i < degree.size(); i++ ){\n if ( i ) cout << \" \";\n cout << degree[i];\n }\n cout << endl;\n}\n\nbool input(){\n cin >> N;\n if ( N == 0 ) return false;\n for ( int i = 0; i < N; i++ ){\n for ( int j = 0; j < N; j++ ) cin >> M[i][j];\n }\n return true;\n}\n main(){\n while ( input() ) solve();\n }", "accuracy": 1, "time_ms": 10, "memory_kb": 1296, "score_of_the_acc": 0, "final_rank": 1 }, { "submission_id": "aoj_1263_404492", "code_snippet": "#include <iostream>\n#include <algorithm>\n#include <vector>\n#include <cstring>\nusing namespace std;\n\n#define MAX 1000\n\nint n, m; //ツノ-ツドツ青? ツδ?ツタツ青?\nint a[52][52];\nint dist[MAX][MAX]; //ツδ?ツタiツつゥツづァツノ-ツドjツづ慊づ?づ個仰猟猟」\nint cnt[MAX]; //ツδ?ツタiツづ個篠淞青?\nint near[MAX]; //ツノ-ツドツづ個暗ェツ氾板凝淞つュツづ可つ?づゥツδ?ツタツづ個氾板債?\nbool used[MAX];\n\nvector<int> t[MAX];\n\nvoid init(){\n m = 0;\n memset(dist, -1, sizeof(dist));\n memset(cnt, 0, sizeof(cnt));\n memset(near, -1, sizeof(near));\n\n for(int i = 0; i < MAX; i++){\n t[i].clear();\n }\n\n near[0] = 0;\n near[1] = a[0][1] - 2;\n\n //1ツ妥、ツ姪堋づ?ツ妥、ツ姪堋づ個甘板つセツつッツ陛環渉按猟?\n for(int i = 0; i < a[0][1] - 1; i++){\n cnt[i] = 2;\n dist[i][0] = i + 1;\n dist[i][1] = a[0][1] - dist[i][0];\n m++;\n\n if(i < a[0][1] - 2){\n t[i].push_back(i + 1);\n t[i + 1].push_back(i);\n }\n }\n}\n\nvoid show(){\n cout<<\"---------------------------------------------\\n\";\n cout<<\"--dist table---------------------------------\\n\";\n for(int i = 0; i < m; i++){\n for(int j = 0; j < n; j++){\n printf(\"%3d\", dist[i][j]);\n }\n\n cout<<\" \";\n printf(\"%3d\",cnt[i]);\n cout<<endl;\n }\n\n cout<<\"--near router--------------------------------\\n\";\n for(int i = 0; i < n; i++){\n cout << \"node \" << i<<\" : \";\n cout << near[i] << endl;\n }\n\n cout<<\"--router graph-------------------------------\\n\";\n\n for(int i = 0; i < m; i++){\n cout<<i<<\" : \";\n for(int j = 0; j < t[i].size(); j++){\n cout << t[i][j] << \", \";\n }\n cout<<endl;\n }\n}\n\n//to : ツ新ツつオツつュツ津?嘉?つキツづゥツノ-ツドツ氾板債?\n//id : ツδ?ツタツ氾板債?\nbool check(int to, int id){\n int diff = a[to][0] - dist[id][0];\n\n for(int i = 1; i < to; i++){\n if(diff != a[to][i] - dist[id][i]){\n return false;\n }\n }\n\n return true;\n}\n\n//A : ツ新ツつオツつュツ津?嘉?つキツづゥツノ-ツドツ氾板債?\n//B : Aツづーツづつづ按つーツづゥツδ?ツタツ氾板債?\n//return : ツノ-ツドAツづ個づ?づ?づ?づ?凝淞つュツづ可つュツづ?づつつ「ツづ?づゥツδ?ツタツ氾板債?\nint connectAB(int A, int B){\n int d = a[A][0] - dist[B][0];\n\n if(d == 1){\n cnt[B]++;\n return B;\n }\n\n for(int i = 0; i < d - 1; i++){\n if(i == 0){\n t[B].push_back(m);\n t[m].push_back(B);\n cnt[m]++;\n cnt[B]++;\n }\n else{\n t[m].push_back(m - 1);\n t[m - 1].push_back(m);\n cnt[m]++;\n cnt[m - 1]++;\n }\n m++;\n }\n cnt[m - 1]++;\n\n return m - 1;\n}\n\n//A : ツ新ツつオツつュツ津?嘉?つキツづゥツノ-ツドツ氾板債?\n//dist[id][A]ツづ営ostツ渉堕つォツ債楪づ?\nvoid dfs(int A, int id, int cost){\n dist[id][A] = cost;\n used[id] = true;\n\n for(int i = 0; i < t[id].size(); i++){\n int to = t[id][i];\n if(!used[to]){\n dfs(A, to, cost + 1);\n }\n }\n}\n\nvoid solve(){\n for(int i = 2; i < n; i++){\n int connect = -1;\n\n for(int j = 0; j < m; j++){\n if(check(i, j)){\n connect = j;\n break;\n }\n }\n\n //cout<<\"connect : \" << connect<<endl;\n\n near[i] = connectAB(i, connect);\n for(int j = 0; j <= i; j++){\n memset(used, 0, sizeof(used));\n dfs(j, near[j], 1);\n }\n }\n\n vector<int> ans;\n for(int i = 0; i < m; i++){\n ans.push_back(cnt[i]);\n }\n sort(ans.begin(), ans.end());\n\n for(int i = 0; i < m; i++){\n if(i != 0) cout << \" \";\n cout << ans[i];\n }\n cout<<endl;\n}\n\nint validate_dfs(int from, int to, int cost){\n used[from] = true;\n if(from == to){\n return cost;\n }\n\n for(int i = 0; i < t[from].size(); i++){\n int TO = t[from][i];\n if(!used[TO]){\n int tmp = validate_dfs(TO, to, cost + 1);\n if(tmp != -1) return tmp;\n }\n }\n\n return -1;\n}\n\nbool validate(){\n for(int i = 0; i < n; i++){\n for(int j = 0; j < i; j++){\n if(near[i] == -1 || near[j] == -1) return false;\n memset(used,0,sizeof(used));\n //printf(\"%d(%d) --> %d(%d)\\n\", near[i], i,near[j], j);\n if(validate_dfs(near[i], near[j], 2) != a[i][j]){\n return false;\n }\n }\n }\n\n return true;\n}\n\nint main(){\n while(cin >> n, n){\n for(int i = 0; i < n; i++){\n for(int j = 0; j < n; j++){\n cin >> a[i][j];\n }\n }\n\n init();\n solve();\n\n //cout << (validate() ? \"yes\" : \"no\") << endl;\n //show();\n }\n}", "accuracy": 1, "time_ms": 20, "memory_kb": 4864, "score_of_the_acc": -0.2538, "final_rank": 5 } ]
aoj_1262_cpp
Problem F: Atomic Car Race In the year 2020, a race of atomically energized cars will be held. Unlike today’s car races, fueling is not a concern of racing teams. Cars can run throughout the course without any refueling. Instead, the critical factor is tire (tyre). Teams should carefully plan where to change tires of their cars. The race is a road race having n checkpoints in the course. Their distances from the start are a 1 , a 2 , ... , and a n (in kilometers). The n -th checkpoint is the goal. At the i -th checkpoint ( i < n ), tires of a car can be changed. Of course, a team can choose whether to change or not to change tires at each checkpoint. It takes b seconds to change tires (including overhead for braking and accelerating). There is no time loss at a checkpoint if a team chooses not to change tires. A car cannot run fast for a while after a tire change, because the temperature of tires is lower than the designed optimum. After running long without any tire changes, on the other hand, a car cannot run fast because worn tires cannot grip the road surface well. The time to run an interval of one kilometer from x to x + 1 is given by the following expression (in seconds). Here x is a nonnegative integer denoting the distance (in kilometers) from the latest checkpoint where tires are changed (or the start). r , v , e and f are given constants. 1/( v - e × ( x - r )) (if x ≥ r ) 1/( v - f × ( r - x )) (if x < r ) Your mission is to write a program to determine the best strategy of tire changes which minimizes the total time to the goal. Input The input consists of multiple datasets each corresponding to a race situation. The format of a dataset is as follows. n a 1 a 2 . . . a n b r v e f The meaning of each of the input items is given in the problem statement. If an input line contains two or more input items, they are separated by a space. n is a positive integer not exceeding 100. Each of a 1 , a 2 , ... , and a n is a positive integer satisfying 0 < a 1 < a 2 < . . . < a n ≤ 10000. b is a positive decimal fraction not exceeding 100.0. r is a nonnegative integer satisfying 0 ≤ r ≤ a n - 1. Each of v , e and f is a positive decimal fraction. You can assume that v - e × ( a n - 1 - r ) ≥ 0.01 and v - f × r ≥ 0.01. The end of the input is indicated by a line with a single zero. Output For each dataset in the input, one line containing a decimal fraction should be output. The decimal fraction should give the elapsed time at the goal (in seconds) when the best strategy is taken. An output line should not contain extra characters such as spaces. The answer should not have an error greater than 0.001. You may output any number of digits after the decimal point, provided that the above accuracy condition is satisfied. Sample Input 2 2 3 1.0 1 1.0 0.1 0.3 5 5 10 15 20 25 0.15 1 1.0 0.04 0.5 10 1783 3640 3991 4623 5465 5481 6369 6533 6865 8425 4.172 72 59.4705 0.0052834 0.0611224 0 Output for the Sample Input 3.5397 31.9249 168.6682
[ { "submission_id": "aoj_1262_3167974", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\nint main() {\n int n;\n while ( cin >> n, n ) {\n vector<int> a(n+1);\n vector<double> d(10001, 100000000.0);\n d[0] = 0;\n vector<int> is(10001, 0);\n a[0] = 0;\n for ( int i = 1; i <= n; i++ ) {\n cin >> a[i];\n is[a[i]] = 1;\n }\n\n double b;\n cin >> b;\n \n int r;\n double v, e, f;\n cin >> r >> v >> e >> f;\n\n for ( int i = 0; i < n; i++ ) {\n int x = 0;\n vector<double> dd = d;\n if ( i > 0 ) d[a[i]] += b; \n for ( int j = a[i]; j < a[n]; j++ ) {\n\tif ( x >= r ) {\n\t d[j+1] = d[j]+(1.0/(v-e*(x-r)));\t \n\t} else {\n\t d[j+1] = d[j]+(1.0/(v-f*(r-x)));\t \n\t}\n\tx++;\t\n }\n for ( int j = 1; j <= n; j++ ) {\n\td[a[j]] = min(d[a[j]], dd[a[j]]);\t\n }\n }\n\n printf(\"%.12lf\\n\", d[a[n]]); \n }\n return 0;\n}", "accuracy": 1, "time_ms": 20, "memory_kb": 3248, "score_of_the_acc": -0.11, "final_rank": 9 }, { "submission_id": "aoj_1262_2653275", "code_snippet": "#include <stdio.h>\n#include <cmath>\n#include <algorithm>\n#include <cfloat>\n#include <stack>\n#include <queue>\n#include <vector>\n#include <string>\n#include <iostream>\n#include <set>\n#include <map>\n#include <time.h>\ntypedef long long int ll;\ntypedef unsigned long long int ull;\n#define BIG_NUM 2000000000\n#define MOD 1000000007\n#define EPS 0.000000001\nusing namespace std;\n\n\nint N;\nint check_point[101];\ndouble min_time[101][101];\ndouble B,R,V,E,F;\n\n\ndouble calcTime(int x){\n\tif(x >= R){\n\n\t\treturn 1.0/(V-E*((double)x-R));\n\t}else{\n\n\t\treturn 1.0/(V-F*(R-(double)x));\n\t}\n}\n\nvoid func(){\n\n\tcheck_point[0] = 0.0;\n\tfor(int i = 1; i <= N; i++)scanf(\"%d\",&check_point[i]);\n\tscanf(\"%lf\",&B);\n\tscanf(\"%lf %lf %lf %lf\",&R,&V,&E,&F);\n\n\tfor(int i = 0; i <= N; i++){\n\t\tfor(int k = 0; k <= i; k++)min_time[i][k] = DBL_MAX;\n\t}\n\n\tmin_time[0][0] = 0;\n\n\tint x;\n\tdouble add_time;\n\n\tfor(int point = 1; point <= N; point++){\n\t\tfor(int changed_loc = 0; changed_loc <= point-1; changed_loc++){\n\t\t\tadd_time = 0.0;\n\t\t\tfor(int loc = check_point[point-1]; loc <= check_point[point]-1; loc++){\n\t\t\t\tx = loc - check_point[changed_loc];\n\t\t\t\tadd_time += calcTime(x);\n\t\t\t}\n\n\t\t\tmin_time[point][changed_loc] = min(min_time[point][changed_loc],min_time[point-1][changed_loc]+add_time);\n\n\t\t\tmin_time[point][point] = min(min_time[point][point],min_time[point-1][changed_loc]+add_time+B);\n\t\t}\n\t}\n\n\tdouble ans = DBL_MAX;\n\tfor(int i = 0; i < N; i++)ans = min(ans,min_time[N][i]);\n\n\tprintf(\"%.10lf\\n\",ans);\n}\n\n\nint main(){\n\n\twhile(true){\n\t\tscanf(\"%d\",&N);\n\t\tif(N == 0)break;\n\n\t\tfunc();\n\t}\n\n\treturn 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3180, "score_of_the_acc": -0.0973, "final_rank": 7 }, { "submission_id": "aoj_1262_1584748", "code_snippet": "#include <algorithm>\n#include <iostream>\n#include <vector>\n#include <cstdio>\n#include <queue>\n#define N_MAX 105\n#define X_MAX 10010\n#define INF (1e9)\nusing namespace std;\ntypedef pair<int,int> P;\ntypedef pair<double,P > P1;\nvoid dijkstra();\ndouble Cal(int,int);\nint n,a[N_MAX],r;\ndouble b,v,e,f,dp[X_MAX][N_MAX];\n\nint main(){\n while(1){\n cin>>n;\n if(!n) break;\n for(int i=0;i<n;i++) cin>>a[i];\n cin>>b>>r>>v>>e>>f;\n dijkstra();\n }\n return 0;\n}\n\nvoid dijkstra(){\n for(int i=0;i<X_MAX;i++)\n for(int j=0;j<n;j++) dp[i][j]=INF;\n priority_queue<P1,vector<P1>,greater<P1> > Q;\n dp[0][0]=Cal(0,a[0]);\n if(n!=1) Q.push(make_pair(dp[0][0],make_pair(a[0],1)));\n while(!Q.empty()){\n P1 v=Q.top();\n Q.pop();\n double t=v.first;\n int uy=v.second.first,ux=v.second.second;\n int ncny=uy+a[ux]-a[ux-1];\n double nccal=Cal(uy,ncny);\n if(dp[ncny][ux]>t+nccal){\n dp[ncny][ux]=t+nccal;\n if(ux!=n-1) Q.push(make_pair(dp[ncny][ux],make_pair(ncny,ux+1)));\n }\n int cny=a[ux]-a[ux-1];\n double ccal=Cal(0,cny)+b;\n if(dp[cny][ux]>t+ccal){\n dp[cny][ux]=t+ccal;\n if(ux!=n-1) Q.push(make_pair(dp[cny][ux],make_pair(cny,ux+1)));\n }\n }\n double mincost=INF;\n for(int i=0;i<=a[n-1];i++) mincost=min(mincost,dp[i][n-1]);\n printf(\"%.4f\\n\",mincost);\n}\n\ndouble Cal(int begin,int end){\n double ret=0;\n for(int i=begin;i<end;i++){\n if(i>=r) ret+=1.0/(1.0*v-1.0*e*(1.0*i-1.0*r));\n else ret+=1.0/(1.0*v-1.0*f*(1.0*r-1.0*i));\n }\n return ret;\n}", "accuracy": 1, "time_ms": 70, "memory_kb": 9468, "score_of_the_acc": -0.4282, "final_rank": 10 }, { "submission_id": "aoj_1262_1584745", "code_snippet": "#include <algorithm>\n#include <iostream>\n#include <vector>\n#include <cstdio>\n#include <queue>\n#define N_MAX 105\n#define X_MAX 10010\n#define INF (1e9)\nusing namespace std;\ntypedef pair<int,int> P;\ntypedef pair<double,P > P1;\nvoid dijkstra();\ndouble Cal(int,int);\nint n,a[N_MAX],r;\ndouble b,v,e,f,dp[X_MAX][N_MAX];\n\nint main(){\n while(1){\n cin>>n;\n if(!n) break;\n for(int i=0;i<n;i++) cin>>a[i];\n cin>>b>>r>>v>>e>>f;\n dijkstra();\n }\n return 0;\n}\n\nvoid dijkstra(){\n for(int i=0;i<X_MAX;i++)\n for(int j=0;j<n;j++) dp[i][j]=INF;\n priority_queue<P1,vector<P1>,greater<P1> > Q;\n dp[0][0]=Cal(0,a[0]);\n Q.push(make_pair(dp[0][0],make_pair(a[0],1)));\n while(!Q.empty()){\n P1 v=Q.top();\n Q.pop();\n double t=v.first;\n int uy=v.second.first,ux=v.second.second;\n int ncny=uy+a[ux]-a[ux-1];\n double nccal=Cal(uy,ncny);\n if(ncny<0) continue;\n if(dp[ncny][ux]>t+nccal){\n dp[ncny][ux]=t+nccal;\n if(ux!=n-1) Q.push(make_pair(dp[ncny][ux],make_pair(ncny,ux+1)));\n }\n int cny=a[ux]-a[ux-1];\n if(cny<0) continue;\n double ccal=Cal(0,cny)+b;\n if(dp[cny][ux]>t+ccal){\n dp[cny][ux]=t+ccal;\n if(ux!=n-1) Q.push(make_pair(dp[cny][ux],make_pair(cny,ux+1)));\n }\n }\n double mincost=INF;\n for(int i=0;i<=a[n-1];i++) mincost=min(mincost,dp[i][n-1]);\n printf(\"%.4f\\n\",mincost);\n}\n\ndouble Cal(int begin,int end){\n double ret=0;\n for(int i=begin;i<end;i++){\n if(i>=r) ret+=1.0/(1.0*v-1.0*e*(1.0*i-1.0*r));\n else ret+=1.0/(1.0*v-1.0*f*(1.0*r-1.0*i));\n }\n return ret;\n}", "accuracy": 1, "time_ms": 70, "memory_kb": 9468, "score_of_the_acc": -0.4282, "final_rank": 10 }, { "submission_id": "aoj_1262_1584719", "code_snippet": "#include <iostream>\n#include <algorithm>\n#include <cstdio>\n#define INF 100000000\n#define t first\n#define x second\nusing namespace std;\ntypedef pair <double,int> P;\ndouble b,v,e,f;\nint r;\ndouble mk(int x) {\n if(x >= r) return 1.0/(v-e*(x-r));\n return 1.0/(v-f*(r-x));\n}\n\nint main() {\n while(1) {\n int n,a[102]={};\n cin >> n;\n if(n==0) break;\n for(int i=1;i<=n;i++) cin >> a[i];\n cin >> b >> r >> v >> e >> f;\n P dp[10010]={};\n for(int i=1;i<=a[n];i++) dp[i] = make_pair(INF,0);\n \n for(int i=0;i<n;i++) {\n int nx=dp[a[i]].x,cx= 0;\n double nt=dp[a[i]].t,ct=dp[a[i]].t+b;\n\n for(int j=a[i]+1;j<=a[n];j++){\n\tnt += mk(nx),ct += mk(cx);\n\tdp[j] = min(dp[j],min((P){nt,++nx},(P){ct,++cx}));\n }\n }\n printf(\"%.4f\\n\",dp[a[n]].t);\n }\n return 0;\n}", "accuracy": 1, "time_ms": 100, "memory_kb": 1400, "score_of_the_acc": -0.1075, "final_rank": 8 }, { "submission_id": "aoj_1262_1583889", "code_snippet": "#include <iostream>\n#include <algorithm>\n#include <cstdio>\n#define INF 2000000\n#define t first\n#define x second\nusing namespace std;\ntypedef pair <double,int> P;\ndouble b,v,e,f;\nint r;\n\ndouble mk(int x) {\n if(x >= r) return 1.0/(v-e*(x-r));\n return 1.0/(v-f*(r-x));\n}\n\nint main() {\n while(1) {\n int n,a[110]={};\n cin >> n;\n if(n==0) break;\n for(int i=1;i<=n;i++) cin >> a[i];\n cin >> b;\n cin >> r >> v >> e >> f;\n P dp[10100]={};\n for(int i=1;i<=a[n];i++) dp[i] = make_pair(INF,0);\n\n for(int i=0;i<n;i++) {\n int tx;\n double tt;\n //No Change\n tx=dp[a[i]].x, tt=dp[a[i]].t;\n for(int j=a[i]+1;j<=a[n];j++){\n\tdouble k = mk(tx);\n\ttt += k , tx++;\n\tdp[j] = min(dp[j],make_pair(tt,tx));\n }\n\n //Change\n tx= 0, tt=dp[a[i]].t+b;\n for(int j=a[i]+1;j<=a[n];j++){\n\tdouble k = mk(tx);\n\ttt +=k; tx++;\n\tdp[j] = min(dp[j],make_pair(tt,tx));\n }\n \n }\n printf(\"%.4f\\n\",dp[a[n]].t);\n }\n return 0;\n}", "accuracy": 1, "time_ms": 70, "memory_kb": 1400, "score_of_the_acc": -0.0784, "final_rank": 6 }, { "submission_id": "aoj_1262_1495804", "code_snippet": "#define _USE_MATH_DEFINES\n#define INF 0x3f3f3f3f\n#include <cstdio>\n#include <iostream>\n#include <sstream>\n#include <cmath>\n#include <cstdlib>\n#include <algorithm>\n#include <queue>\n#include <stack>\n#include <limits>\n#include <map>\n#include <string>\n#include <cstring>\n#include <set>\n#include <deque>\n#include <bitset>\n#include <list>\n#include <cctype>\n#include <utility>\n \nusing namespace std;\n \ntypedef long long ll;\ntypedef pair <int,int> P;\ntypedef pair <int,P > PP;\n \nint tx[] = {0,1,0,-1};\nint ty[] = {-1,0,1,0};\n \nstatic const double EPS = 1e-8;\n\ndouble compute_next_x_duration(double x, double r, double v, double e, double f){\n if(x >= r){\n return 1.0 / (v - e * (x - r));\n }\n else{\n return 1.0 / (v - f * (r - x));\n }\n}\n\ndouble compute_duration(double from, double to, double r, double v, double e, double f){\n double sum = 0;\n \n for(int pos = 0; pos < to - from; pos++){\n sum += compute_next_x_duration(pos,r,v,e,f);\n }\n return sum;\n}\n\n\ndouble dp[101][101]; // dp[current_i][last_change_pos]\n\nint main(){\n int num_of_checkpoints;\n while(~scanf(\"%d\",&num_of_checkpoints)){\n if(num_of_checkpoints == 0) break;\n\n vector<int> checkpoints;\n checkpoints.push_back(0);\n for(int checkpoint_i = 0; checkpoint_i < num_of_checkpoints; checkpoint_i++){\n int pos;\n scanf(\"%d\",&pos);\n checkpoints.push_back(pos);\n }\n double tire_change_time;\n scanf(\"%lf\",&tire_change_time);\n double r,v,e,f;\n scanf(\"%lf %lf %lf %lf\",&r,&v,&e,&f);\n\n fill((double*)dp,(double*)dp + 101 * 101,1000000000000.0);\n dp[0][0] = 0;\n for(int current_i = 0; current_i < checkpoints.size(); current_i++){\n for(int prev_i = 0; prev_i < current_i; prev_i++){\n // x is a nonnegative integer denoting the distance (in kilometers) \n // from the latest checkpoint where tires are changed\n \n dp[current_i][prev_i]\n = min(dp[prev_i][prev_i]\n + compute_duration(checkpoints[prev_i],checkpoints[current_i],r,v,e,f),\n dp[current_i][prev_i]);\n\n dp[current_i][current_i]\n = min(dp[prev_i][prev_i]\n + compute_duration(checkpoints[prev_i],checkpoints[current_i],r,v,e,f)\n + tire_change_time,\n dp[current_i][current_i]);\n }\n }\n\n double res = numeric_limits<double>::max();\n for(int i = 0; i < checkpoints.size(); i++){\n res = min(res,dp[checkpoints.size()-1][i]);\n }\n printf(\"%lf\\n\",res);\n }\n}", "accuracy": 1, "time_ms": 830, "memory_kb": 1336, "score_of_the_acc": -0.8135, "final_rank": 18 }, { "submission_id": "aoj_1262_1174075", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\n#define INF (1<<28)\nint n;\nint a[200],r;\ndouble b,v,e,f;\ndouble dp[200];\nint main(){\n while(cin>>n&&n){\n for(int i=1;i<=n;i++)cin>>a[i];\n cin>>b;\n cin>>r>>v>>e>>f;\n for(int i=0;i<=n;i++)dp[i]=INF;\n dp[0]=0;\n a[0]=0;\n for(int i=0;i<n;i++){\n int x=0;\n double sum=0;\n for(int j=i+1;j<=n;j++){\n while(a[i]+x<a[j]){\n double d;\n if(x>=r){\n d=x-r;\n sum += 1.0/(v-e*d);\n }else{\n d=r-x;\n sum += 1.0/(v-f*d);\n }\n x++;\n }\n if(i!=0)dp[j]=min(dp[j],dp[i]+b+sum);\n else dp[j]=min(dp[j],sum);\n }\n }\n \n printf(\"%.6f\\n\",dp[n]);\n }\n return 0;\n}", "accuracy": 1, "time_ms": 30, "memory_kb": 1244, "score_of_the_acc": -0.0328, "final_rank": 4 }, { "submission_id": "aoj_1262_1067669", "code_snippet": "#include <algorithm>\n#include <climits>\n#include <cstdlib>\n#include <iostream>\n#include <vector>\nusing namespace std;\n\ntemplate<class T> inline void chmin(T &a, const T &b) { if(a > b) a = b; }\n \ninline double calc(int x, int r, double v, double e, double f) {\n\tif(x >= r) return 1.0 / (v - e * (x - r));\n\treturn 1.0 / (v - f * (r - x));\n}\n \nint main(){\n\tcin.tie(nullptr);\n\tios::sync_with_stdio(false);\n\tcout.setf(ios::fixed);\n\tcout.precision(10);\n \n\tfor(int n; cin >> n && n;) {\n\t\tvector<int> a(n + 1, 0);\n\t\tfor(int i = 1; i <= n; ++i) {\n\t\t\tcin >> a[i];\n\t\t}\n \n\t\tdouble b;\n\t\tcin >> b;\n \n\t\tint r;\n\t\tdouble v, e, f;\n\t\tcin >> r >> v >> e >> f;\n \n\t\tvector<vector<double>> dp(n + 1, vector<double>(n + 1, INT_MAX));\n\t\tdp[0][0] = 0.0;\n \n\t\tfor(int i = 0; i < n; ++i) {\n\t\t\tfor(int j = 0; j <= i; ++j) {\n\t\t\t\tchmin(dp[i][i], dp[i][j] + b);\n \n\t\t\t\tdouble next_cost = 0.0;\n\t\t\t\tfor(int x = a[i]; x < a[i + 1]; ++x) {\n\t\t\t\t\tnext_cost += calc(x - a[j], r, v, e, f);\n\t\t\t\t}\n\n\t\t\t\tchmin(dp[i + 1][j], dp[i][j] + next_cost);\n\t\t\t}\n\t\t}\n \n\t\tcout << *min_element(dp[n].begin(), dp[n].end()) << endl;\n\t}\n \n\treturn EXIT_SUCCESS;\n}", "accuracy": 1, "time_ms": 20, "memory_kb": 1408, "score_of_the_acc": -0.0302, "final_rank": 3 }, { "submission_id": "aoj_1262_1067126", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\n#define dump(a) (cerr << #a << \" = \" << (a) << endl)\n\n#define REP(i,a,b) for(int i=(a);i < (int)(b); i++)\n#define rep(i,n) REP(i,0,n)\n#define ALL(v) begin(v), end(v)\n\ndouble calc(int x, int r, double v, double e, double f) {\n if(x >= r) return 1.0 / (v - e * (x - r));\n return 1.0 / (v - f * (r - x));\n}\n\nint main(){\n cout.setf(ios::fixed);\n cout.precision(10);\n\n for(int n; cin >> n && n;) {\n vector<int> a(n + 1, 0);\n for(int i = 1; i <= n; ++i) {\n cin >> a[i];\n }\n\n double b;\n cin >> b;\n\n int r;\n double v, e, f;\n cin >> r >> v >> e >> f;\n\n vector<vector<double>> dp(n + 1, vector<double>(n + 1, INT_MAX));\n dp[0][0] = 0;\n\n for(int i = 0; i < n; ++i) {\n for(int j = 0; j <= i; ++j) {\n\tif(dp[i][j] == INT_MAX) continue;\n\n\tdp[i][i] = min(dp[i][i], dp[i][j] + b);\n\n\tdouble next_cost = 0.0;\n\tfor(int x = a[i]; x < a[i + 1]; ++x) {\n\t next_cost += calc(x - a[j], r, v, e, f);\n\t}\n\n\tdp[i + 1][j] = min(dp[i + 1][j], dp[i][j] + next_cost);\n }\n }\n\n double ans = INT_MAX;\n for(int i = 0; i <= n; ++i) {\n ans = min(ans, dp[n][i]);\n }\n\n cout << ans << endl;\n }\n\n return 0;\n}", "accuracy": 1, "time_ms": 20, "memory_kb": 1396, "score_of_the_acc": -0.0297, "final_rank": 2 }, { "submission_id": "aoj_1262_742495", "code_snippet": "#include<iostream>\n#include<algorithm>\n#include<vector>\n#include<cmath>\n#include<iomanip>\n#include<cassert>\n#include<queue>\n#include<map>\n#define REP(i,s,n) for(int i=s;i<n;i++)\n#define rep(i,n) REP(i,0,n)\n#define MAX 101\n#define inf (1<<29)\nusing namespace std;\nint a[MAX];\nint A[10001];\nint n;\ndouble b,r,v,e,f;\ndouble mincost[MAX][10001];\n \nstruct P\n{\n int last;\n double x,cost,now;\n P(double now=-1,double x=-1,double cost=-1,int last=-1):now(now),x(x),cost(cost),last(last){}\n bool operator < (const P &a)const\n {\n return cost >= a.cost;\n }\n};\n \ndouble getCost(double x)\n{\n if(x >= r)return 1.0/(v - e * (x - r));\n return 1.0/(v - f * (r - x)); \n}\n \nvoid dijkstra()\n{\n double ans = inf;\n rep(i,n)rep(j,10001)mincost[i][j] = inf;\n mincost[0][0] = 0;\n \n priority_queue<P> Q;\n Q.push(P(0,0,0,0));\n \n while(!Q.empty())\n {\n P p = Q.top(); Q.pop();\n if(p.now >= a[n-1])\n {\n ans = min(ans,p.cost);\n continue;\n } \n \n if(ans <= p.cost)continue;\n \n assert(0 <= p.cost);\n double ncost = p.cost + getCost(p.x);\n assert(0 <= ncost);\n \n if(mincost[p.last][(int)p.now+1] > ncost)\n {\n mincost[p.last][(int)p.now+1] = ncost;\n Q.push(P(p.now+1,p.x+1,ncost,p.last));\n }\n \n if(A[(int)p.now+1] != -1)\n {\n double change_cost = (A[(int)p.now+1]==n?0:b);\n ncost += change_cost;\n Q.push(P(p.now+1,0,ncost,A[(int)p.now+1]));\n }\n \n }\n \n cout << setiosflags(ios::fixed) << setprecision(4) << ans << endl;\n}\n \nint main()\n{\n while(cin >> n,n)\n {\n rep(i,10001)A[i] = -1;\n rep(i,n)\n {\n cin >> a[i]; \n A[a[i]] = i+1; \n }\n cin >> b >> r >> v >> e >> f;\n \n \n dijkstra();\n \n }\n return 0;\n}", "accuracy": 1, "time_ms": 310, "memory_kb": 9536, "score_of_the_acc": -0.6641, "final_rank": 15 }, { "submission_id": "aoj_1262_742488", "code_snippet": "#include<iostream>\n#include<algorithm>\n#include<vector>\n#include<cmath>\n#include<iomanip>\n#include<cassert>\n#include<queue>\n#include<map>\n#define REP(i,s,n) for(int i=s;i<n;i++)\n#define rep(i,n) REP(i,0,n)\n#define MAX 101\n#define inf (1<<29)\nusing namespace std;\nint a[MAX];\nint A[10001];\nint n;\ndouble b,r,v,e,f;\ndouble mincost[MAX][10001];\n\nstruct P\n{\n int last;\n double x,cost,now;\n P(double now=-1,double x=-1,double cost=-1,int last=-1):now(now),x(x),cost(cost),last(last){}\n bool operator < (const P &a)const\n {\n return cost >= a.cost;\n }\n};\n\ndouble getCost(double x)\n{\n if(x >= r)return 1.0/(v - e * (x - r));\n return 1.0/(v - f * (r - x)); \n}\n\nvoid dijkstra()\n{\n double ans = inf;\n rep(i,n)rep(j,10001)mincost[i][j] = inf;\n mincost[0][0] = 0;\n\n priority_queue<P> Q;\n Q.push(P(0,0,0,0));\n\n while(!Q.empty())\n {\n P p = Q.top(); Q.pop();\n if(p.now >= a[n-1])\n\t{\n\t ans = min(ans,p.cost);\n\t continue;\n\t} \n\n if(ans <= p.cost)continue;\n\n assert(0 <= p.cost);\n double ncost = p.cost + getCost(p.x);\n assert(0 <= ncost);\n\n if(mincost[p.last][(int)p.now+1] > ncost)\n\t{\n\t mincost[p.last][(int)p.now+1] = ncost;\n\t Q.push(P(p.now+1,p.x+1,ncost,p.last));\n\t}\n\n if(A[(int)p.now+1] != -1)\n\t{\n\t double change_cost = (A[(int)p.now+1]==n?0:b);\n\t ncost += change_cost;\n\t Q.push(P(p.now+1,0,ncost,A[(int)p.now+1]));\n\t}\n\n }\n\n cout << setiosflags(ios::fixed) << setprecision(4) << ans << endl;\n}\n\nint main()\n{\n while(cin >> n,n)\n {\n rep(i,10001)A[i] = -1;\n rep(i,n)\n\t{\n\t cin >> a[i];\t \n\t A[a[i]] = i+1; \n\t}\n cin >> b >> r >> v >> e >> f;\n \n\n dijkstra();\n\n }\n return 0;\n}", "accuracy": 1, "time_ms": 310, "memory_kb": 9540, "score_of_the_acc": -0.6643, "final_rank": 16 }, { "submission_id": "aoj_1262_742403", "code_snippet": "#include<iostream>\n#include<vector>\n#include<algorithm>\n#include<queue>\n#include<cstdio>\n\nusing namespace std;\n\ntypedef long double ld;\n\nconst int TYRE = 10001;\nconst int NODE = 101;\nconst int INF = (1<<25);\n\nstruct State{\n int x, pos;\n ld t;\n bool operator < (const State& s) const {\n return t > s.t;\n }\n};\n\nvector<int> V;\n\nld T[TYRE][NODE];\nint n;\nld b,r,v,e,f;\n\n\nvoid solve(){\n fill(T[0],T[0]+TYRE*NODE,(ld)INF);\n\n priority_queue<State> Q;\n Q.push((State){0,0,0});\n\n while(!Q.empty()){\n\n State now = Q.top(); Q.pop();\n if(now.pos == n) continue;\n\n if(T[now.x][now.pos] < now.t) continue;\n\n //change tyre\n \n State nex = now;\n nex.x = 0;\n nex.t += b;\n int diff = V[now.pos+1]-V[now.pos];\n \n for(int i = 0; i < diff; i++){\n if(nex.x >= r) nex.t += 1.0/(v-e*(nex.x-r));\n else nex.t += 1.0/(v-f*(r-nex.x));\n nex.x++;\n }\n nex.pos++;\n \n if(T[nex.x][nex.pos] > nex.t) {\n T[nex.x][nex.pos] = nex.t;\n Q.push(nex);\n }\n\n //not change tyre\n nex = now;\n\n for(int i = 0; i < diff; i++){\n if(nex.x >= r) nex.t += 1.0/(v-e*(nex.x-r));\n else nex.t += 1.0/(v-f*(r-nex.x));\n nex.x++;\n }\n\n nex.pos++;\n \n if(T[nex.x][nex.pos] > nex.t) {\n T[nex.x][nex.pos] = nex.t;\n Q.push(nex);\n }\n }\n ld ans = (ld)INF;\n \n for(int i = 0; i < TYRE; i++) ans = min(ans,T[i][n]);\n\n printf(\"%.4Lf\\n\",ans);\n}\n\n\nvoid input(){\n V.resize(n+1); V[0] = 0;\n for(int i = 1; i <= n; i++) cin >> V[i];\n cin >> b >> r >> v >> e >> f;\n}\n\nint main(){\n \n while(cin >> n && n){\n input();\n solve();\n }\n return 0;\n}", "accuracy": 1, "time_ms": 100, "memory_kb": 17056, "score_of_the_acc": -0.7863, "final_rank": 17 }, { "submission_id": "aoj_1262_685307", "code_snippet": "#include<cstdio>\nusing namespace std;\n \ntypedef double D;\n \nint n,a[110],r;\nD b,v,e,f;\nD dp[10100][110];\nconst D EPS = 1e-8;\n \nint main(){\n while(scanf(\"%d\",&n),n){\n a[0] = 0;\n for(int i=1;i<=n;i++)scanf(\"%d\",&a[i]);\n scanf(\"%lf%d%lf%lf%lf\",&b,&r,&v,&e,&f);\n \n for(int i=0;i<=a[n];i++)\n for(int j=0;j<=n;j++)dp[i][j] = 1e15;\n dp[0][0] = 0;\n \n int pos = 1;\n for(int i=0;i<a[n];i++){\n if(i==a[pos]){\n for(int j=0;j<pos;j++){\n if(dp[i][pos] > dp[i][j] + b + EPS){\n dp[i][pos] = dp[i][j] + b;\n }\n }\n pos++;\n }\n \n for(int j=0;j<=n;j++){\n if(dp[i][j] +EPS > 1e15)continue;\n int x = i-a[j];\n D cost = (x<r)?(1.0/(v-f*(r-x))):(1.0/(v-e*(x-r)));\n if(dp[i+1][j] > dp[i][j] + cost + EPS){\n dp[i+1][j] = dp[i][j] + cost;\n }\n }\n }\n \n D res = 1e15;\n for(int i=0;i<=n;i++){\n if(res > dp[a[n]][i] + EPS)res = dp[a[n]][i];\n }\n printf(\"%.10lf\\n\",res);\n }\n}", "accuracy": 1, "time_ms": 70, "memory_kb": 9688, "score_of_the_acc": -0.4377, "final_rank": 12 }, { "submission_id": "aoj_1262_685301", "code_snippet": "#include<cstdio>\nusing namespace std;\n \ntypedef double D;\n \nint n,a[110],r;\nD b,v,e,f;\nD dp[10100][110];\nconst D EPS = 1e-8;\n \nint main(){\n while(scanf(\"%d\",&n),n){\n a[0] = 0;\n for(int i=1;i<=n;i++)scanf(\"%d\",&a[i]);\n scanf(\"%lf%d%lf%lf%lf\",&b,&r,&v,&e,&f);\n \n for(int i=0;i<=a[n];i++)\n for(int j=0;j<=n;j++)dp[i][j] = 1e15;\n dp[0][0] = 0;\n \n int pos = 1;\n for(int i=0;i<a[n];i++){\n if(i==a[pos]){\n for(int j=0;j<pos;j++){\n if(dp[i][pos] > dp[i][j] + b + EPS){\n dp[i][pos] = dp[i][j] + b;\n }\n }\n pos++;\n }\n \n for(int j=0;j<=n;j++){\n int x = i-a[j];\n D cost = (x<r)?(1.0/(v-f*(r-x))):(1.0/(v-e*(x-r)));\n if(dp[i+1][j] > dp[i][j] + cost + EPS){\n dp[i+1][j] = dp[i][j] + cost;\n }\n }\n }\n \n D res = 1e15;\n for(int i=0;i<=n;i++){\n if(res > dp[a[n]][i] + EPS)res = dp[a[n]][i];\n }\n printf(\"%.10lf\\n\",res);\n }\n}", "accuracy": 1, "time_ms": 110, "memory_kb": 9688, "score_of_the_acc": -0.4766, "final_rank": 13 }, { "submission_id": "aoj_1262_685124", "code_snippet": "#include<cstdio>\nusing namespace std;\n\ntypedef double D;\n\nint n,a[110],r;\nD b,v,e,f;\nD dp[10100][110];\nconst D EPS = 1e-8;\n\nint main(){\n while(scanf(\"%d\",&n),n){\n a[0] = 0;\n for(int i=1;i<=n;i++)scanf(\"%d\",&a[i]);\n scanf(\"%lf%d%lf%lf%lf\",&b,&r,&v,&e,&f);\n\n for(int i=0;i<=a[n];i++)\n for(int j=0;j<=n;j++)dp[i][j] = 1e15;\n dp[0][0] = 0;\n\n int pos = 1;\n for(int i=0;i<a[n];i++){\n if(i==a[pos]){\n\tfor(int j=0;j<pos;j++){\n\t if(dp[i][pos] > dp[i][j] + b + EPS){\n\t dp[i][pos] = dp[i][j] + b;\n\t }\n\t}\n\tpos++;\n }\n\n for(int j=0;j<=n;j++){\n\tint x = i-a[j];\n\tD cost = (x<r)?(1.0/(v-f*(r-x))):(1.0/(v-e*(x-r)));\n\tif(dp[i+1][j] > dp[i][j] + cost + EPS){\n\t dp[i+1][j] = dp[i][j] + cost;\n\t}\n }\n }\n \n D res = 1e15;\n for(int i=0;i<=n;i++){\n if(res > dp[a[n]][i] + EPS)res = dp[a[n]][i];\n }\n printf(\"%.10lf\\n\",res);\n }\n}", "accuracy": 1, "time_ms": 110, "memory_kb": 9688, "score_of_the_acc": -0.4766, "final_rank": 13 }, { "submission_id": "aoj_1262_491037", "code_snippet": "//21\n#include<iostream>\n#include<queue>\n\nusing namespace std;\n\nstruct S{\n int a;\n double t;\n bool operator<(S a)const{\n return t>a.t;\n }\n};\n\nint main(){\n for(int n;cin>>n,n;){\n int a[101]={};\n for(int i=1;i<=n;i++){\n cin>>a[i];\n }\n int r;\n double b,v,e,f;\n cin>>b>>r>>v>>e>>f;\n S is={0,0};\n priority_queue<S> que;\n que.push(is);\n bool p[101]={};\n for(;;){\n S c=que.top();\n if(c.a==n)break;\n que.pop();\n if(p[c.a]++)continue;\n int cp=a[c.a];\n double t=c.t;\n for(int np=c.a+1;np<=n;np++){\n\twhile(cp!=a[np]){\n\t int x=cp-a[c.a];\n\t t+=(x>=r)?1/(v-e*(x-r)):1/(v-f*(r-x));\n\t cp++;\n\t}\n\tS n={np,t+b};\n\tque.push(n);\n }\n }\n cout<<fixed<<que.top().t-b<<endl;\n }\n return 0;\n}", "accuracy": 1, "time_ms": 50, "memory_kb": 1420, "score_of_the_acc": -0.0598, "final_rank": 5 }, { "submission_id": "aoj_1262_458139", "code_snippet": "#include <cmath>\n#include <ctime>\n#include <cstdio>\n#include <cctype>\n#include <cstdlib>\n#include <cstring>\n#include <iostream>\n#include <sstream>\n#include <algorithm>\n#include <set>\n#include <stack>\n#include <queue>\n#include <string>\n#include <vector>\n#include <map>\n#define maxn 110\n#define maxl 1000000000\n#define mod 1000000007\nusing namespace std;\n\ntypedef unsigned long long ull;\n\nint a[maxn];\ndouble ans[maxn];\n\nbool solve(){\n\tint n,i,j,r,k;\n\tdouble b,v,e,f,temp2,temp;\n\tscanf(\"%d\",&n);\n\tif(n==0)return false;\n\tfor(i=1;i<=n;++i){\n\t\tscanf(\"%d\",&a[i]);\n\t\tans[i]=1e30;\n\t}\n\tscanf(\"%lf%d%lf%lf%lf\",&b,&r,&v,&e,&f);\n\tans[0]=-b;\n\tfor(i=0;i<n;++i){\n\t\ttemp=ans[i]+b;\n\t\tfor(j=i+1;j<=n;++j){\n\t\t\tfor(k=a[j-1];k<a[j];++k){\n\t\t\t\tif(k-a[i]>=r)temp2=1/(v-e*(k-a[i]-r));\n\t\t\t\telse temp2=1/(v-f*(r-(k-a[i])));\n\t\t\t\ttemp+=temp2;\n\t\t\t}\n\t\t\tans[j]=min(temp,ans[j]);\n\t\t}\n\t}\n\tprintf(\"%.4f\\n\",ans[n]);\n\treturn true;\n}\n\nint main(){\n\twhile(solve());\n\treturn 0;\n}", "accuracy": 1, "time_ms": 40, "memory_kb": 936, "score_of_the_acc": -0.0291, "final_rank": 1 }, { "submission_id": "aoj_1262_348181", "code_snippet": "#include <iostream>\n#include <algorithm>\n#include <cmath>\n#include <iomanip>\n\nusing namespace std;\n\nconst double INF = 1000000000.0;\nconst int N = 100;\n\nint n;\nint a[N + 1];\ndouble b;\nint r;\ndouble v, e, f;\ndouble dp[N + 1];\n\ndouble t(int x)\n{\n\tdouble ans = 0.0;\n\tfor (int i = 0; i < x; i++) {\n\t\tif (i >= r) {\n\t\t\tans += 1.0 / (v - e * (i - r));\n\t\t}\n\t\telse {\n\t\t\tans += 1.0 / (v - f * (r - i));\n\t\t}\n\t}\n\treturn ans;\n}\n\nvoid solve()\n{\n\ta[0] = 0;\n\tdp[0] = 0.0;\n\tfor (int i = 1; i <= n; i++) {\n\t\tdp[i] = INF;\n\t\tfor (int j = 0; j < i; j++) {\n\t\t\tdp[i] = min(dp[i], dp[j] + t(a[i] - a[j]));\n\t\t}\n\t\tdp[i] += b;\n\t}\n\tdouble ans = dp[n] - b;\n\tcout << setprecision(4);\n\tcout << setiosflags(ios::fixed);\n\tcout << ans << endl;\n}\n\nint main()\n{\n\twhile (cin >> n, n) {\n\t\tfor (int i = 1; i <= n; i++) {\n\t\t\tcin >> a[i];\n\t\t}\n\t\tcin >> b;\n\t\tcin >> r >> v >> e >> f;\n\t\tsolve();\n\t}\n\treturn 0;\n}", "accuracy": 1, "time_ms": 1040, "memory_kb": 1000, "score_of_the_acc": -1.0028, "final_rank": 19 }, { "submission_id": "aoj_1262_298596", "code_snippet": "#include <iostream>\n#include <vector>\n#include <algorithm>\n#include <cstring>\n#include <cmath>\n\nusing namespace std;\n\n#define EPS (1e-10)\n#define EQ(a,b) (abs((a)-(b))<EPS)\n\nint n;\ndouble dp[101][30001];\ndouble b,e,v,f;\nint r;\nint a[1001];\ndouble mem[30000];\nconst int INF=1000000000;\n\n\ndouble dfs(int idx,int runDist){\n if(!EQ(dp[idx][runDist],INF))return dp[idx][runDist];\n if(idx==n)\n return 0;\n double res=INF;\n double cost=0;\n int ndist=a[idx+1]-a[idx];\n res=min(res,dfs(idx+1,ndist)+mem[ndist-1]+b);\n // no change\n if(runDist>=1)\n res=min(res,dfs(idx+1,ndist+runDist)+mem[ndist+runDist-1]-mem[runDist-1]);\n else\n res=min(res,dfs(idx+1,ndist+runDist)+mem[ndist+runDist-1]);\n return dp[idx][runDist]=res;\n}\n\nint main(){\n while(cin>>n&&n!=0){\n a[0]=0;\n for(int i = 0; i < n; i++)cin>>a[i+1];\n cin>>b;\n cin>>r>>v>>e>>f;\n for(int i = 0; i < 101; i++)for(int j = 0; j < 30001; j++)dp[i][j]=INF;\n memset(mem,0,sizeof(mem));\n mem[0]=1.0/(v-f*(r));\n for(int i = 1; i < 10001; i++){\n if(i>=r)\n mem[i]=mem[i-1]+1.0/(v-e*(i-r));\n else\n mem[i]=mem[i-1]+1.0/(v-f*(r-i));\n }\n for(int i = 0; i < 30000; i++)\n dp[n][i]=0;\n for(int i = n-1; i >= 0; i--){\n for(int j = 10001; j >= 0; j--){\n int ndist=a[i+1]-a[i];\n dp[i][j]=min(dp[i][j],dp[i+1][ndist]+mem[ndist-1]+b);\n if(j>=1)\n dp[i][j]=min(dp[i][j],dp[i+1][ndist+j]+mem[ndist+j-1]-mem[j-1]);\n else\n dp[i][j]=min(dp[i][j],dp[i+1][ndist+j]+mem[ndist+j-1]);\n }\n }\n printf(\"%.10f\\n\",dp[0][0]);\n }\n return 0;\n}", "accuracy": 1, "time_ms": 290, "memory_kb": 24000, "score_of_the_acc": -1.2718, "final_rank": 20 } ]
aoj_1265_cpp
Problem I: Shy Polygons You are given two solid polygons and their positions on the xy -plane. You can move one of the two along the x -axis (they can overlap during the move). You cannot move it in other directions. The goal is to place them as compactly as possible, subject to the following condition: the distance between any point in one polygon and any point in the other must not be smaller than a given minimum distance L . We define the width of a placement as the difference between the maximum and the minimum x -coordinates of all points in the two polygons. Your job is to write a program to calculate the minimum width of placements satisfying the above condition. Let's see an example. If the polygons in Figure 13 are placed with L = 10.0, the result will be 100. Figure 14 shows one of the optimal placements. Input The input consists of multiple datasets. Each dataset is given in the following format. L Polygon 1 Polygon 2 L is a decimal fraction, which means the required distance of two polygons. L is greater than 0.1 and less than 50.0. The format of each polygon is as follows. n x 1 y 1 x 2 y 2 . . . x n y n n is a positive integer, which represents the number of vertices of the polygon. n is greater than 2 and less than 15. Remaining lines represent the vertices of the polygon. A vertex data line has a pair of nonneg- ative integers which represent the x - and y-coordinates of a vertex. x - and y -coordinates are separated by a single space, and y -coordinate is immediately followed by a newline. x and y are less than 500. Edges of the polygon connect vertices given in two adjacent vertex data lines, and vertices given in the last and the first vertex data lines. You may assume that the vertices are given in the counterclockwise order, and the contours of polygons are simple, i.e. they do not cross nor touch themselves. Also, you may assume that the result is not sensitive to errors. In concrete terms, for a given pair of polygons, the minimum width is a function of the given minimum distance l . Let us denote the function w ( l ). Then you can assume that | w ( L ± 10 -7 ) - w ( L )| < 10 -4 . The end of the input is indicated by a line that only contains a zero. It is not a part of a dataset. Output The output should consist of a series of lines each containing a single decimal fraction. Each number should indicate the minimum width for the corresponding dataset. The answer should not have an error greater than 0.0001. You may output any number of digits after the decimal point, provided that the above accuracy condition is satisfied. Sample Input 10.5235 3 0 0 100 100 0 100 4 0 50 20 50 20 80 0 80 10.0 4 120 45 140 35 140 65 120 55 8 0 0 100 0 100 100 0 100 0 55 80 90 80 10 0 45 10.0 3 0 0 1 0 0 1 3 0 100 1 101 0 101 10.0 3 0 0 1 0 0 100 3 0 50 100 50 0 51 0 Output for the Sample Input 114.882476 100 1 110.5005
[ { "submission_id": "aoj_1265_10848496", "code_snippet": "/*\n * Created By phyxnj@uestc\n * Last Compiler:\n * Sun, 29 Jul 2012 02:07:44 +0800\n */\n#include <cstdio>\n#include <cstring>\n#include <ctime>\n#include <algorithm>\n#include <cstdlib>\n#include <cmath>\n#include <set>\n#include <vector>\n#include <utility>\n#include <string>\n#include <map>\n#include <iostream>\nusing namespace std;\n#define eps 1e-7\n#define pw(x) ((x)*(x))\ntypedef long long LL;\nstruct P\n{\n double x,y;\n P () {}\n P (double x,double y) : x(x),y(y) {}\n P operator - (const P & b)\n {\n return P (x-b.x,y-b.y);\n }\n double operator * (const P & b)\n {\n return x*b.y-y*b.x;\n }\n double operator & (const P & b)\n {\n return x*b.x+y*b.y;\n }\n double dorm()\n {\n return sqrt(pw(x)+pw(y));\n }\n}p[300],s[300],tp[300];\nint sgn(double x)\n{\n return x<-eps?-1:x>eps;\n}\ndouble cal(P a,P b,P c) // 计算c到ab的最小距离\n{\n int t1=sgn((c-a)&(b-a));\n int t2=sgn((c-b)&(a-b));\n if(t1>=0&&t2>=0) return fabs((a-c)*(a-b))/(a-b).dorm();\n return min((c-a).dorm(),(c-b).dorm());\n}\ndouble maybe[10000]; \nint cnt,flag;\ndouble L;\nint n,m;\nvoid run(P a,P b,P c) // 计算要使得c与ab的最小距离为L要使c移动的距离\n{\n double l=-20000,h=20000,mid1,mid2;\n for(int i=0;i<50;i++){\n mid1=(2*l+h)/3;\n mid2=(2*h+l)/3;\n double t1=cal(a,b,P(mid1,c.y));\n double t2=cal(a,b,P(mid2,c.y));\n if(sgn(t1-t2)>=0) l=mid1;\n else h=mid2;\n }\n double mid;\n l=-20000;h=mid1;\n for(int i=0;i<50;i++){\n mid=(l+h)/2;\n double t1=cal(a,b,P(mid,c.y));\n if(sgn(t1-L)>=0) l=mid;\n else h=mid;\n }\n double delta=mid-c.x;\n if(flag) delta=-delta;\n maybe[cnt++]=delta;\n l=mid1,h=20000;\n for(int i=0;i<50;i++){\n mid=(l+h)/2;\n double t1=cal(a,b,P(mid,c.y));\n if(sgn(t1-L)>=0) h=mid;\n else l=mid;\n }\n delta=mid-c.x;\n if(flag) delta=-delta;\n maybe[cnt++]=delta;\n}\nvoid creat()\n{\n cnt=0;\n s[m]=s[0],p[n]=p[0];\n flag=1;\n for(int i=0;i<n;i++){\n for(int j=0;j<m;j++){\n run(s[j],s[j+1],p[i]);\n }\n }\n flag=0;\n for(int i=0;i<m;i++){\n for(int j=0;j<n;j++){\n run(p[j],p[j+1],s[i]);\n }\n }\n for(int i=0;i<m;i++){\n for(int j=0;j<n;j++) {\n maybe[cnt++]=p[j].x-s[i].x;\n }\n }\n}\nint in(P a,P pt[],int n)\n{\n int cnt=0;\n pt[n]=pt[0];\n for(int i=0;i<n;i++){\n P x=pt[i],y=pt[i+1];\n if(sgn(x.y-y.y)>0) swap(x,y);\n if(sgn(x.y-a.y)>=0||sgn(y.y-a.y)<0) continue;\n double t=(x-a)*(y-a);\n if(sgn(t)>0) cnt++;\n }\n return cnt&1;\n}\ndouble solve()\n{\n double ans=1e10;\n for(int i=0;i<cnt;i++){\n double x=maybe[i];\n// printf(\"%f\\n\",x);\n for(int j=0;j<m;j++){\n tp[j].x=s[j].x+x;\n tp[j].y=s[j].y;\n }\n tp[m]=tp[0];\n int flag=0;\n for(int j=0;j<n;j++){\n for(int k=0;k<m;k++){\n int t1=sgn((p[j]-tp[k])*(tp[k+1]-tp[k]))*sgn((p[j+1]-tp[k])*(tp[k+1]-tp[k]));\n int t2=sgn((tp[k]-p[j])*(p[j+1]-p[j]))*sgn((tp[k+1]-p[j])*(p[j+1]-p[j]));\n if(t1<0&&t2<0) {\n flag=1;\n break;\n }\n double dis=min(min(cal(p[j],p[j+1],tp[k]),cal(p[j],p[j+1],tp[k+1])),min(cal(tp[k],tp[k+1],p[j]),cal(tp[k],tp[k+1],p[j+1])));\n if(dis-L<-1e-6) {\n flag=1;\n break;\n }\n }\n }\n if(flag) continue;\n flag=in(tp[0],p,n)||in(p[0],tp,m);\n if(flag) continue;\n double minx=1e10,maxx=-1e10;\n for(int j=0;j<m;j++){\n minx=min(minx,tp[j].x);\n maxx=max(maxx,tp[j].x);\n }\n for(int j=0;j<n;j++){\n minx=min(minx,p[j].x);\n maxx=max(maxx,p[j].x);\n }\n// printf(\"%f %f ************\\n\",maxx,minx);\n ans=min(ans,maxx-minx);\n }\n return ans;\n}\nint main()\n{ \n P a,b,c;\n while(scanf(\"%lf\",&L)&&sgn(L)){\n scanf(\"%d\",&n);\n for(int i=0;i<n;i++) scanf(\"%lf%lf\",&p[i].x,&p[i].y);\n scanf(\"%d\",&m);\n for(int i=0;i<m;i++) scanf(\"%lf%lf\",&s[i].x,&s[i].y);\n creat();\n printf(\"%f\\n\",solve());\n }\n return 0;\n}", "accuracy": 1, "time_ms": 150, "memory_kb": 3384, "score_of_the_acc": -0.9595, "final_rank": 5 }, { "submission_id": "aoj_1265_9587207", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\nconst double EPS = 1e-7;\n\nstruct Point {\n double x, y;\n Point rot_ccw() const { return {-y, x}; }\n double magnitude() const { return hypot(x, y); }\n Point operator+(const Point p) const { return {x + p.x, y + p.y}; }\n Point operator-(const Point p) const { return {x - p.x, y - p.y}; }\n Point scale(const double f) const { return {x * f, y * f}; }\n Point normalize() const { return scale(1 / magnitude()); }\n double dist(const Point p) const { return hypot(x - p.x, y - p.y); }\n double operator*(const Point p) const { return x * p.x + y * p.y; }\n double cross(const Point p) const { return x * p.y - y * p.x; }\n};\n\nshort orientation(const Point& o, const Point& a, const Point& b) {\n const double cross = (a - o).cross(b - o);\n return cross < 0 ? -1 : cross > 0;\n}\n\nstruct SegmentHorizontalIntersections {\n virtual vector<double> intersections(const Point a, const Point b) const = 0;\n};\n\nstruct Segment : SegmentHorizontalIntersections {\n const Point p, q;\n Segment(const Point p, const Point q) : p(p), q(q) {}\n\n double dist(const Segment s) const {\n if (intersect(s)) return 0;\n return min({dist(s.p), dist(s.q), s.dist(p), s.dist(q)});\n }\n double dist(const Point r) const {\n if ((q - p) * (r - p) <= 0) return p.dist(r);\n if ((p - q) * (r - q) <= 0) return q.dist(r);\n return fabs((q - p).cross(r - p)) / p.dist(q);\n }\n bool intersect(const Segment s) const {\n const bool o1 = orientation(p, q, s.p) * orientation(p, q, s.q) < 0;\n const bool o2 = orientation(s.p, s.q, p) * orientation(s.p, s.q, q) < 0;\n return o1 && o2;\n }\n double dist_x(const Point r) const {\n const double dy = (r.y - p.y) / (q.y - p.y);\n const double x = p.x + (dy * (q.x - p.x));\n const double res = x - r.x;\n return res;\n }\n vector<double> intersections(const Point a, const Point b) const {\n if (fabs(p.y - q.y) < EPS) return {};\n\n const Segment s{a, b};\n return {-s.dist_x(p), -s.dist_x(q), dist_x(a), dist_x(b)};\n }\n};\n\nstruct Circle : SegmentHorizontalIntersections {\n const Point center;\n const double radius;\n Circle(const Point p, const double r) : center(p), radius(r) {}\n\n vector<double> intersections(Point p, Point q) const {\n p = p - center;\n q = q - center;\n\n vector<double> res;\n\n for (const Point r : vector<Point>{p, q}) {\n const double arg = radius * radius - r.y * r.y;\n if (arg < 0) continue;\n\n const double x = sqrt(arg);\n res.emplace_back(x - r.x);\n res.emplace_back(-x - r.x);\n }\n\n const Point point_on_circle = (q - p).rot_ccw().normalize().scale(radius);\n\n for (const Point r : vector<Point>{point_on_circle, point_on_circle.scale(-1)})\n res.emplace_back(-Segment{p, q}.dist_x(r));\n\n return res;\n }\n};\n\ndouble width(const vector<Point>& polygon1, const vector<Point>& polygon2) {\n double a = 1e9;\n double b = -1e9;\n for (const auto p : polygon1) a = min(a, p.x);\n for (const auto p : polygon2) a = min(a, p.x);\n for (const auto p : polygon1) b = max(b, p.x);\n for (const auto p : polygon2) b = max(b, p.x);\n return b - a;\n}\n\nbool point_inside_polygon(const Point p, const vector<Segment>& polygon_edges) {\n const Segment ray{p, {1e4, 1e4 + 1e-2}};\n int intersections = 0;\n for (const Segment e : polygon_edges) intersections += e.intersect(ray);\n return intersections % 2 == 1;\n}\n\nvector<Segment> polygon_edges(const vector<Point>& polygon) {\n vector<Segment> edges;\n\n for (int i = 0; i < (int)polygon.size(); i++)\n edges.emplace_back(polygon[i], polygon[(i + 1) % polygon.size()]);\n\n return edges;\n}\n\ndouble polygon_distance(const vector<Point>& polygon1, const vector<Point>& polygon2) {\n double res = 1e9;\n\n const vector<Segment> edges1 = polygon_edges(polygon1);\n const vector<Segment> edges2 = polygon_edges(polygon2);\n\n for (const Segment s : edges1) {\n if (point_inside_polygon(s.p, edges2)) return 0;\n for (const Segment r : edges2) {\n if (point_inside_polygon(r.p, edges1)) return 0;\n res = min(res, s.dist(r));\n }\n }\n\n return res;\n}\n\ndouble solve(const vector<Point>& polygon1, const vector<Point>& polygon2,\n const double L) {\n double ans = 1e9;\n\n vector<shared_ptr<SegmentHorizontalIntersections>> obstacles;\n set<double> x_movements;\n\n for (const Point circle_center : polygon1)\n obstacles.emplace_back(make_shared<Circle>(circle_center, L));\n\n for (const Point p : polygon1)\n obstacles.emplace_back(make_shared<Segment>(Point{p.x, 2000}, Point{p.x, -2000}));\n\n for (const auto [p, q] : polygon_edges(polygon1)) {\n const Point dir = (p - q).normalize().rot_ccw().scale(L);\n obstacles.emplace_back(make_shared<Segment>(p + dir, q + dir));\n }\n\n for (const auto& obs : obstacles) {\n for (const auto [p, q] : polygon_edges(polygon2)) {\n const vector<double> xs = obs->intersections(p, q);\n copy(xs.begin(), xs.end(), inserter(x_movements, x_movements.end()));\n }\n }\n\n for (const double x : x_movements) {\n vector<Point> moved = polygon2;\n for (Point& p : moved) p.x += x;\n const double distance = polygon_distance(polygon1, moved);\n if (distance >= L - EPS) ans = min(ans, width(polygon1, moved));\n }\n\n return ans;\n}\n\nint main() {\n double L;\n while (cin >> L && L > 0) {\n int n;\n cin >> n;\n vector<Point> polygon1(n);\n for (auto& p : polygon1) cin >> p.x >> p.y;\n cin >> n;\n vector<Point> polygon2(n);\n for (auto& p : polygon2) cin >> p.x >> p.y;\n\n cout << fixed << setprecision(5) << solve(polygon1, polygon2, L) << endl;\n }\n}", "accuracy": 1, "time_ms": 90, "memory_kb": 3528, "score_of_the_acc": -0.9838, "final_rank": 7 }, { "submission_id": "aoj_1265_9587165", "code_snippet": "#include <bits/stdc++.h>\n\n#include <algorithm>\nusing namespace std;\n\nconst double EPS = 1e-7;\n\nstruct Point {\n double x, y;\n Point rot_ccw() const { return {-y, x}; }\n double magnitude() const { return hypot(x, y); }\n Point operator+(const Point p) const { return {x + p.x, y + p.y}; }\n Point operator-(const Point p) const { return {x - p.x, y - p.y}; }\n Point scale(const double f) const { return {x * f, y * f}; }\n Point normalize() const { return scale(1 / magnitude()); }\n double dist(const Point p) const { return hypot(x - p.x, y - p.y); }\n double operator*(const Point p) const { return x * p.x + y * p.y; }\n double cross(const Point p) const { return x * p.y - y * p.x; }\n Point negate() const { return {-x, -y}; }\n};\n\nshort orientation(const Point& o, const Point& a, const Point& b) {\n const double cross = (a - o).cross(b - o);\n return cross < 0 ? -1 : cross > 0;\n}\n\nstruct Intersection {\n virtual vector<double> intersections(const Point a, const Point b) const = 0;\n};\n\nbool inside(const double v, const double a, const double b) {\n const auto [n, m] = minmax(a, b);\n return n <= v && v <= m;\n}\n\nstruct Segment : Intersection {\n Point p, q;\n Segment(const Point p, const Point q) : p(p), q(q) {}\n bool is_vertical() const { return fabs(p.x - q.x) < EPS; }\n double dist(const Segment s) const {\n if (intersect(s)) return 0;\n return min({dist(s.p), dist(s.q), s.dist(p), s.dist(q)});\n }\n double dist(const Point r) const {\n if ((q - p) * (r - p) <= 0) return p.dist(r);\n if ((p - q) * (r - q) <= 0) return q.dist(r);\n return fabs((q - p).cross(r - p)) / p.dist(q);\n }\n bool intersect(const Segment s) const {\n const short o1 = orientation(p, q, s.p);\n const short o2 = orientation(p, q, s.q);\n const short o3 = orientation(s.p, s.q, p);\n const short o4 = orientation(s.p, s.q, q);\n return o1 * o2 < 0 && o3 * o4 < 0;\n }\n optional<double> dist_x(const Point r) const {\n if (!inside(r.y, p.y, q.y)) return nullopt;\n if (is_vertical()) return p.x - r.x;\n\n const double yp = (r.y - p.y) / (q.y - p.y);\n const double x = p.x + (yp * (q.x - p.x));\n return x - r.x;\n }\n\n vector<double> intersections(const Point a, const Point b) const {\n vector<double> res;\n\n vector<optional<double>> candidates{Segment{a, b}.dist_x(p), Segment{a, b}.dist_x(q),\n dist_x(a), dist_x(b)};\n\n int i = 0;\n for (const auto x : candidates) {\n if (x.has_value()) {\n const double val = x.value();\n if (isnan(val)) continue;\n if (i < 2) {\n res.emplace_back(-val);\n } else {\n res.emplace_back(val);\n }\n }\n i++;\n }\n\n return res;\n }\n};\n\nstruct Circle : Intersection {\n const Point center;\n const double radius;\n Circle(const Point p, const double r) : center(p), radius(r) {}\n vector<double> intersections(Point p, Point q) const {\n p = p - center;\n q = q - center;\n\n vector<double> res;\n\n const vector<Point> ps{p, q};\n\n for (const Point r : ps) {\n if (inside(r.y, -radius, radius)) {\n const double x0 = sqrt(radius * radius - r.y * r.y);\n const double x1 = -x0;\n res.emplace_back(x1 - r.x);\n res.emplace_back(x0 - r.x);\n }\n }\n\n /*bool overlap = false;*/\n /*if (inside(p.y, -radius, radius)) overlap = true;*/\n /*if (inside(q.y, -radius, radius)) overlap = true;*/\n /*if (inside(-radius, p.y, q.y)) overlap = true;*/\n /*if (inside(radius, p.y, q.y)) overlap = true;*/\n /**/\n /*if (true|| overlap) {*/\n const Point point_on_circle = (q - p).rot_ccw().normalize().scale(radius);\n for (const Point r : vector<Point>{point_on_circle, point_on_circle.negate()}) {\n /*if (!inside(r.y, p.y, q.y)) continue;*/\n\n const double yp = (r.y - p.y) / (q.y - p.y);\n const double x = p.x + (yp * (q.x - p.x));\n res.emplace_back(r.x - x);\n }\n /*}*/\n\n return res;\n }\n};\n\ndouble width(const vector<Point>& polygon1, const vector<Point>& polygon2) {\n double a = 1e9;\n double b = -1e9;\n for (const auto p : polygon1) a = min(a, p.x);\n for (const auto p : polygon2) a = min(a, p.x);\n for (const auto p : polygon1) b = max(b, p.x);\n for (const auto p : polygon2) b = max(b, p.x);\n return b - a;\n}\n\nbool point_inside_polygon(const Point p, const vector<Segment>& polygon_edges) {\n const Segment ray{p, {1e4, 1e4 + 1e-2}};\n int intersections = 0;\n for (const Segment e : polygon_edges) intersections += e.intersect(ray);\n return intersections % 2 == 1;\n}\n\nvector<Segment> polygon_edges(const vector<Point>& polygon) {\n vector<Segment> edges;\n\n for (int i = 0; i < (int)polygon.size(); i++)\n edges.emplace_back(polygon[i], polygon[(i + 1) % polygon.size()]);\n\n return edges;\n}\n\ndouble polygon_distance(const vector<Point>& polygon1, const vector<Point>& polygon2) {\n double res = 1e9;\n\n const vector<Segment> edges1 = polygon_edges(polygon1);\n const vector<Segment> edges2 = polygon_edges(polygon2);\n\n for (const Segment s : edges1) {\n if (point_inside_polygon(s.p, edges2)) return 0;\n for (const Segment r : edges2) {\n if (point_inside_polygon(r.p, edges1)) return 0;\n res = min(res, s.dist(r));\n }\n }\n\n return res;\n}\n\ndouble solve(const vector<Point>& polygon1, vector<Point> polygon2, const double L) {\n double ans = 1e9;\n\n vector<shared_ptr<Intersection>> obstacles;\n set<double> x_movements;\n\n for (const Point circle_center : polygon1)\n obstacles.emplace_back(make_shared<Circle>(circle_center, L));\n\n for (const Point p : polygon1)\n obstacles.emplace_back(make_shared<Segment>(Point{p.x, 2000}, Point{p.x, -2000}));\n\n for (const auto [p, q] : polygon_edges(polygon1)) {\n const Point dir = (p - q).normalize().rot_ccw().scale(L);\n obstacles.emplace_back(make_shared<Segment>(p + dir, q + dir));\n }\n\n for (const auto& obs : obstacles) {\n for (const auto [p, q] : polygon_edges(polygon2)) {\n const vector<double> xs = obs->intersections(p, q);\n copy(xs.begin(), xs.end(), inserter(x_movements, x_movements.end()));\n }\n }\n\n for (const double x : x_movements) {\n vector<Point> moved = polygon2;\n for (auto& p : moved) p.x += x;\n const double distance = polygon_distance(polygon1, moved);\n if (distance >= L - EPS) ans = min(ans, width(polygon1, moved));\n }\n\n return ans;\n}\n\nint main() {\n double L;\n while (cin >> L && L > 0) {\n int n;\n cin >> n;\n vector<Point> polygon1(n);\n for (auto& p : polygon1) cin >> p.x >> p.y;\n cin >> n;\n vector<Point> polygon2(n);\n for (auto& p : polygon2) cin >> p.x >> p.y;\n\n cout << fixed << setprecision(5) << solve(polygon1, polygon2, L) << endl;\n }\n}", "accuracy": 1, "time_ms": 60, "memory_kb": 3504, "score_of_the_acc": -0.9599, "final_rank": 6 }, { "submission_id": "aoj_1265_9587113", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\nconst double EPS = 1e-7;\n\nbool zero(const double a) { return fabs(a) < EPS; }\nbool equal(const double a, const double b) { return zero(a - b); }\n\nstruct Point {\n double x, y;\n Point rot_ccw() const { return {-y, x}; }\n double magnitude() const { return hypot(x, y); }\n Point operator+(const Point p) const { return {x + p.x, y + p.y}; }\n Point operator-(const Point p) const { return {x - p.x, y - p.y}; }\n Point scale(const double f) const { return {x * f, y * f}; }\n Point normalize() const { return scale(1 / magnitude()); }\n double dist(const Point p) const { return hypot(x - p.x, y - p.y); }\n double operator*(const Point p) const { return x * p.x + y * p.y; }\n double cross(const Point p) const { return x * p.y - y * p.x; }\n Point negate() const { return {-x, -y}; }\n bool operator==(const Point p) const { return equal(x, p.x) && equal(y, p.y); }\n};\n\nshort orientation(const Point& o, const Point& a, const Point& b) {\n const double cross = (a - o).cross(b - o);\n if (fabs(cross) < EPS) return 0;\n return cross < 0 ? -1 : 1;\n}\n\nstruct Intersection {\n virtual vector<double> intersections(const Point a, const Point b) const = 0;\n virtual string to_desmos() const = 0;\n};\n\nbool inside(const double v, const double a, const double b) {\n const auto [n, m] = minmax(a, b);\n return n <= v && v <= m;\n}\n\nstruct Segment : Intersection {\n Point p, q;\n bool contains_except_endpoints(const Point& r) const {\n if (orientation(p, q, r) != 0) return false;\n return (q - p) * (r - p) > EPS && (p - q) * (r - q) > EPS;\n }\n Segment(const Point p, const Point q) : p(p), q(q) {}\n bool is_vertical() const { return equal(p.x, q.x); }\n double dist(const Segment s) const {\n if (intersect(s)) return 0;\n return min({dist(s.p), dist(s.q), s.dist(p), s.dist(q)});\n }\n double dist(const Point r) const {\n if ((q - p) * (r - p) <= 0) return p.dist(r);\n if ((p - q) * (r - q) <= 0) return q.dist(r);\n return fabs((q - p).cross(r - p)) / p.dist(q);\n }\n bool intersect(const Segment s) const {\n const short o1 = orientation(p, q, s.p);\n const short o2 = orientation(p, q, s.q);\n const short o3 = orientation(s.p, s.q, p);\n const short o4 = orientation(s.p, s.q, q);\n return o1 * o2 < 0 && o3 * o4 < 0;\n }\n /*bool intersect(const Segment s) const {*/\n /* return (orientation(p, q, s.p) * orientation(p, q, s.q) < 0) &&*/\n /* (orientation(s.p, s.q, p) * orientation(s.p, s.q, q) < 0);*/\n /*}*/\n optional<double> dist_x(const Point r) const {\n if (!inside(r.y, p.y, q.y)) return nullopt;\n if (is_vertical()) return p.x - r.x;\n\n const double yp = (r.y - p.y) / (q.y - p.y);\n const double x = p.x + (yp * (q.x - p.x));\n return x - r.x;\n }\n\n vector<double> intersections(const Point a, const Point b) const {\n vector<double> res;\n\n vector<optional<double>> candidates{Segment{a, b}.dist_x(p), Segment{a, b}.dist_x(q),\n dist_x(a), dist_x(b)};\n\n int i = 0;\n for (const auto x : candidates) {\n if (x.has_value()) {\n const double val = x.value();\n if (isnan(val)) continue;\n if (i < 2) {\n res.emplace_back(-val);\n } else {\n res.emplace_back(val);\n }\n }\n i++;\n }\n\n return res;\n }\n string to_desmos() const {\n stringstream ss;\n ss << fixed << setprecision(6) << \"\\\\left(\\\\left(1-t\\\\right)\\\\cdot\" << p.x\n << \"+t\\\\cdot\" << q.x << \",\\\\left(1-t\\\\right)\\\\cdot\" << p.y << \"+t\\\\cdot\" << q.y\n << \"\\\\right)\";\n return ss.str();\n }\n};\n\nstruct Circle : Intersection {\n const Point center;\n const double radius;\n Circle(const Point p, const double r) : center(p), radius(r) {}\n vector<double> intersections(Point p, Point q) const {\n p = p - center;\n q = q - center;\n\n vector<double> res;\n\n const vector<Point> ps{p, q};\n\n for (const Point r : ps) {\n if (inside(r.y, -radius, radius)) {\n const double x0 = sqrt(radius * radius - r.y * r.y);\n const double x1 = -x0;\n res.emplace_back(x1 - r.x);\n res.emplace_back(x0 - r.x);\n }\n }\n\n bool overlap = false;\n if (inside(p.y, -radius, radius)) overlap = true;\n if (inside(q.y, -radius, radius)) overlap = true;\n if (inside(-radius, p.y, q.y)) overlap = true;\n if (inside(radius, p.y, q.y)) overlap = true;\n\n if (overlap) {\n const Point point_on_circle = (q - p).rot_ccw().normalize().scale(radius);\n const vector<Point> ps{point_on_circle, point_on_circle.negate()};\n for (const Point r : ps) {\n if (!inside(r.y, p.y, q.y)) continue;\n\n const double yp = (r.y - p.y) / (q.y - p.y);\n const double x = p.x + (yp * (q.x - p.x));\n res.emplace_back(r.x - x);\n }\n }\n\n // todo: intersections when the segment p,q is tangent to the circle but endpoints\n // don't touch\n return res;\n }\n string to_desmos() const {\n stringstream ss;\n ss << \"\\\\left(x-\" << center.x << \"\\\\right)^{2}+\\\\left(y-\" << center.y\n << \"\\\\right)^{2}=\" << radius << \"^{2}\";\n return ss.str();\n }\n};\n\ndouble width(const vector<Point>& polygon1, const vector<Point>& polygon2) {\n double a = 1e9;\n double b = -1e9;\n for (const auto& p : polygon1) {\n a = min(a, p.x);\n b = max(b, p.x);\n }\n for (const auto& p : polygon2) {\n a = min(a, p.x);\n b = max(b, p.x);\n }\n return b - a;\n}\nbool point_inside_polygon(const Point& p, const vector<Point>& polygon) {\n const Segment ray{p, {1e4, 1e4 + 1e-2}};\n\n int intersections = 0;\n\n for (int i = 0; i < (int)polygon.size(); i++) {\n const Segment edge{polygon[i], polygon[(i + 1) % polygon.size()]};\n intersections += edge.intersect(ray);\n }\n\n return intersections % 2 == 1;\n}\n\nPoint vertex_at(const vector<Point>& polygon, int i) {\n int n = (int)polygon.size();\n i = (i + (n << 10)) % n;\n return polygon[i];\n}\n\ndouble angle(const Point& a, const Point& b) {\n const double x = atan2(a.cross(b), a * b);\n return x < 0 ? x + 2 * M_PI : x;\n}\nbool intersection_aux(const vector<Point>& p1, const vector<Point>& p2, int i, int j) {\n const Point &a0 = vertex_at(p1, i - 1), &a1 = vertex_at(p1, i),\n &a2 = vertex_at(p1, i + 1);\n const Point &b0 = vertex_at(p2, j - 1), &b1 = vertex_at(p2, j),\n &b2 = vertex_at(p2, j + 1);\n\n if (Segment{a1, a2}.intersect({b1, b2})) return true;\n\n if (Segment{b1, b2}.contains_except_endpoints(a1)) {\n if (orientation(b1, b2, a2) == 1) return true;\n if (orientation(b1, b2, a0) == 1) return true;\n }\n if (Segment{a1, a2}.contains_except_endpoints(b1)) {\n if (orientation(a1, a2, b2) == 1) return true;\n if (orientation(a1, a2, b0) == 1) return true;\n }\n\n if (a1 == b1) {\n const double th = angle(b2 - b1, b0 - b1);\n double th2 = angle(b2 - b1, a0 - b1);\n if (th2 > EPS && th2 < th - EPS) return true;\n th2 = angle(b2 - b1, a2 - b1);\n if (th2 > EPS && th2 < th - EPS) return true;\n }\n\n return false;\n}\n\nbool intersection(const vector<Point>& p1, const vector<Point>& p2) {\n const int n = p1.size();\n const int m = p2.size();\n for (int i = 0; i < n; i++) {\n for (int j = 0; j < m; j++) {\n if (intersection_aux(p1, p2, i, j)) return true;\n }\n }\n return false;\n}\n\nstring polygon_to_str(const vector<Point>& polygon) {\n stringstream ss;\n ss << fixed << setprecision(6);\n ss << \"polygon(\";\n for (int i = 0; i < (int)polygon.size(); i++) {\n if (i > 0) ss << \", \";\n ss << \"(\" << polygon[i].x << \", \" << polygon[i].y << \")\";\n }\n ss << \")\";\n return ss.str();\n}\n\ndouble polygon_distance(const vector<Point>& polygon1, const vector<Point>& polygon2) {\n /*if (intersection(polygon1, polygon2)) return 0;*/\n double res = 1e9;\n\n vector<Segment> edges1, edges2;\n int n = polygon1.size();\n for (int i = n - 1, j = 0; j < n; i = j++) {\n const Point p = polygon1[i];\n if (point_inside_polygon(p, polygon2)) return 0;\n const Point q = polygon1[j];\n edges1.emplace_back(p, q);\n }\n\n n = polygon2.size();\n for (int i = n - 1, j = 0; j < n; i = j++) {\n const Point p = polygon2[i];\n if (point_inside_polygon(p, polygon1)) return 0;\n const Point q = polygon2[j];\n edges2.emplace_back(p, q);\n }\n\n for (const Segment s : edges1) {\n for (const Segment r : edges2) {\n res = min(res, s.dist(r));\n }\n }\n\n return res;\n}\n\ndouble solve(const vector<Point>& polygon1, vector<Point> polygon2, const double L,\n const int test_case) {\n double ans = 1e9;\n const int dbg = -263;\n for (auto& p : polygon2) p.x -= 2000;\n\n if (test_case == dbg) {\n cerr << fixed << setprecision(3) << \"L: \" << L << endl;\n cerr << \"original polygons:\" << endl;\n cerr << polygon_to_str(polygon1) << endl;\n cerr << polygon_to_str(polygon2) << endl << endl;\n }\n\n vector<shared_ptr<Intersection>> obstacles;\n set<double> x_movements;\n\n for (const Point circle_center : polygon1) {\n obstacles.emplace_back(make_shared<Circle>(circle_center, L));\n }\n\n for (const Point p : polygon1) {\n obstacles.emplace_back(make_shared<Segment>(Point{p.x, 2000}, Point{p.x, -2000}));\n }\n\n const int n = polygon1.size();\n for (int i = n - 1, j = 0; j < n; i = j++) {\n const Point p = polygon1[i];\n const Point q = polygon1[j];\n const Point dir = (p - q).normalize().rot_ccw().scale(L);\n obstacles.emplace_back(make_shared<Segment>(p + dir, q + dir));\n }\n\n for (const auto& obs : obstacles) {\n const int n = polygon2.size();\n for (int i = n - 1, j = 0; j < n; i = j++) {\n const Point p = polygon2[i];\n const Point q = polygon2[j];\n for (const double x : obs->intersections(p, q)) {\n assert(!isnan(x));\n x_movements.emplace(x);\n }\n }\n }\n\n /*cerr << \"movements: \" << x_movements.size() << endl;*/\n if (x_movements.empty()) {\n const double w1 = width(polygon1, {});\n const double w2 = width(polygon2, {});\n /*cerr << endl << \"------------\" << endl << endl;*/\n return max(w1, w2);\n }\n\n for (const double x_movement : x_movements) {\n vector<Point> moved = polygon2;\n for (auto& p : moved) p.x += x_movement;\n if (test_case == dbg) {\n cerr << polygon_to_str(moved) << endl;\n }\n const double distance = polygon_distance(polygon1, moved);\n const double w = width(polygon1, moved);\n if (distance >= L - EPS) { // TODO: this is fucked up lol\n ans = min(ans, w);\n }\n if (test_case == dbg) {\n cerr << fixed << setprecision(6) << \"movement: \" << x_movement << endl;\n cerr << fixed << setprecision(6) << \"width: \" << w << endl;\n cerr << fixed << setprecision(6) << \"dist: \" << distance << endl;\n cerr << fixed << setprecision(6) << \"curr best: \" << ans << endl;\n cerr << endl;\n }\n }\n\n /*if (test_case == dbg) {*/\n /* cerr << \"OBSTACLES\" << endl;*/\n /* for (const auto& obs : obstacles) {*/\n /* cerr << obs->to_desmos() << endl;*/\n /* }*/\n /* cerr << endl;*/\n /*}*/\n\n /*cerr << endl << \"------------\" << endl << endl;*/\n return ans;\n}\n\nint main() {\n double L;\n int t = 1;\n while (cin >> L && L > 0) {\n int n;\n cin >> n;\n vector<Point> polygon1(n);\n for (auto& p : polygon1) cin >> p.x >> p.y;\n cin >> n;\n vector<Point> polygon2(n);\n for (auto& p : polygon2) cin >> p.x >> p.y;\n\n const double val1 = solve(polygon1, polygon2, L, t);\n const double val2 = solve(polygon2, polygon1, L, -1);\n\n cout << fixed << setprecision(5) << min(val1, val2) << endl;\n t++;\n }\n}", "accuracy": 1, "time_ms": 50, "memory_kb": 3624, "score_of_the_acc": -1, "final_rank": 8 }, { "submission_id": "aoj_1265_3183752", "code_snippet": "#include <iostream>\n#include <iomanip>\n#include <complex>\n#include <vector>\n#include <algorithm>\n#include <cmath>\n#include <array>\nusing namespace std;\nconst double EPS = 1e-6;\nconst double INF = 1e12;\nconst double PI = acos(-1);\n#define EQ(n,m) (abs((n)-(m)) < EPS)\n#define X real()\n#define Y imag()\n\ntypedef complex<double> P;\ntypedef vector<P> VP;\nstruct L : array<P, 2>{\n L(const P& a, const P& b){ at(0)=a; at(1)=b; }\n L(){}\n};\n\nnamespace std{\n bool operator < (const P& a, const P& b){\n return !EQ(a.X,b.X) ? a.X<b.X : a.Y+EPS<b.Y;\n }\n bool operator == (const P& a, const P& b){\n return abs(a-b) < EPS;\n }\n}\n\ndouble dot(P a, P b){\n return (conj(a)*b).X;\n}\ndouble cross(P a, P b){\n return (conj(a)*b).Y;\n}\nint ccw(P a, P b, P c){\n b -= a;\n c -= a;\n if(cross(b,c) > EPS) return +1; //ccw\n if(cross(b,c) < -EPS) return -1; //cw\n if(dot(b,c) < -EPS) return +2; //c-a-b\n if(abs(c)-abs(b) > EPS) return -2; //a-b-c\n return 0; //a-c-b\n}\n\nbool intersectSS(const L& a, const L& b){\n return ( ccw(a[0],a[1],b[0]) *ccw(a[0],a[1],b[1]) <= 0 ) &&\n ( ccw(b[0],b[1],a[0]) *ccw(b[0],b[1],a[1]) <= 0 );\n}\nbool intersectSP(const L& s, const P &p){\n return abs(cross(s[0]-p, s[1]-p))<EPS && dot(s[0]-p, s[1]-p)<EPS;\n}\n\ndouble distanceLP(const L &l, const P &p) {\n return abs(cross(l[1]-l[0], p-l[0])) /abs(l[1]-l[0]);\n}\ndouble distanceSP(const L &s, const P &p) {\n if(dot(s[1]-s[0], p-s[0]) < EPS) return abs(p-s[0]);\n if(dot(s[0]-s[1], p-s[1]) < EPS) return abs(p-s[1]);\n return distanceLP(s, p);\n}\ndouble distanceSS(const L &s, const L &t) {\n if (intersectSS(s, t)) return 0;\n return min(min(distanceSP(s, t[0]), distanceSP(s, t[1])),\n min(distanceSP(t, s[0]), distanceSP(t, s[1])));\n}\n\nbool isParallel(const P &a, const P &b){\n return abs(cross(a,b)) < EPS;\n}\nbool isParallel(const L &a, const L &b){\n return isParallel(a[1]-a[0], b[1]-b[0]);\n}\n\nP crosspointLL(const L &l, const L &m) {\n double A = cross(l[1]-l[0], m[1]-m[0]);\n double B = cross(l[1]-l[0], l[1]-m[0]);\n return m[0] + B/A *(m[1]-m[0]);\n}\n\nint in_poly(const P &p, const VP &poly){\n int n = poly.size();\n int ret = -1;\n for(int i=0; i<n; i++){\n P a = poly[i]-p;\n P b = poly[(i+1)%n]-p;\n if(a.Y > b.Y) swap(a,b);\n if(intersectSP(L(a,b), P(0,0))) return 0;\n if(a.Y<=0 && b.Y>0 && cross(a,b)<0) ret = -ret;\n }\n return ret;\n}\n\ndouble distanceGG(VP &a, VP &b){\n int n = a.size();\n int m = b.size();\n if(in_poly(a[0], b) > 0 || in_poly(b[0], a) > 0){\n return 0;\n }\n double ret = INF;\n for(int i=0; i<n; i++){\n for(int j=0; j<m; j++){\n ret = min(ret, distanceSS(L(a[i], a[(i+1)%n]), L(b[j], b[(j+1)%m])));\n }\n }\n return ret;\n}\n\ndouble leftend(VP &poly){\n double ret = INF;\n for(P p: poly){\n ret = min(ret, p.X);\n }\n return ret;\n}\ndouble rightend(VP &poly){\n double ret = -INF;\n for(P p: poly){\n ret = max(ret, p.X);\n }\n return ret;\n}\nVP slide(VP poly, double d){\n for(P &p: poly){\n p += P(d, 0);\n }\n return poly;\n}\nL slide(L l, double d){\n return L(l[0] +P(d, 0), l[1] +P(d, 0));\n}\n//aとbの距離を最も近づけるためには, bをx方向にretスライドすればよい\ndouble closest_slide(L a, L b){\n if(a[0].Y > a[1].Y) swap(a[0], a[1]);\n if(b[0].Y > b[1].Y) swap(b[0], b[1]);\n //Yの範囲に被りがある\n for(int d=0; d<2; d++){\n for(int i=0; i<2; i++){\n if(b[0].Y +EPS < a[i].Y && a[i].Y +EPS < b[1].Y){\n L hori(a[i], a[i] +P(1, 0));\n P cp = crosspointLL(hori, b);\n double diff = a[i].X -cp.X;\n return (d == 0)? diff : -diff;\n }\n }\n swap(a, b);\n }\n //ない\n if(a[1].Y < b[0].Y +EPS){\n return a[1].X -b[0].X;\n }else{\n return a[0].X -b[1].X;\n }\n}\n\nint main(){\n cout << fixed << setprecision(5);\n while(1){\n double d;\n cin >> d;\n if(d == 0) break;\n\n vector<VP> poly(2);\n for(int d=0; d<2; d++){\n int n;\n cin >> n;\n poly[d].resize(n);\n for(int i=0; i<n; i++){\n double x,y;\n cin >> x >> y;\n poly[d][i] = P(x, y);\n }\n }\n double diff = leftend(poly[1]) -leftend(poly[0]);\n poly[1] = slide(poly[1], -diff);\n if(distanceGG(poly[0], poly[1]) +EPS > d){\n double left = min(leftend(poly[0]), leftend(poly[1]));\n double right = max(rightend(poly[0]), rightend(poly[1]));\n cout << right -left << endl;\n continue;\n }\n\n int n = poly[0].size();\n int m = poly[1].size();\n vector<double> cand;\n for(int i=0; i<n; i++){\n for(int j=0; j<m; j++){\n L edge[2];\n edge[0] = L(poly[0][i], poly[0][(i+1)%n]);\n edge[1] = L(poly[1][j], poly[1][(j+1)%m]);\n double csd = closest_slide(edge[0], edge[1]);\n edge[1] = slide(edge[1], csd);\n //NG範囲をもたない\n if(distanceSS(edge[0], edge[1]) +EPS > d){\n continue;\n }\n //NG範囲をにぶたん\n for(int s=0; s<2; s++){\n double lb=0, ub=1e4;\n for(int rep=0; rep<50; rep++){\n double mid = (lb +ub) /2;\n L tmp = (s==0)? slide(edge[1], mid) : slide(edge[1], -mid);\n if(distanceSS(edge[0], tmp) > d){\n ub = mid;\n }else{\n lb = mid;\n }\n }\n if(s == 0){\n cand.push_back(csd +lb);\n }else{\n cand.push_back(csd -lb);\n }\n }\n }\n }\n\n double ans = INF;\n for(double dist: cand){\n VP tmp = slide(poly[1], dist);\n if(distanceGG(poly[0], tmp) +EPS > d){\n double left = min(leftend(poly[0]), leftend(tmp));\n double right = max(rightend(poly[0]), rightend(tmp));\n ans = min(ans, right -left);\n }\n }\n cout << ans << endl;\n }\n return 0;\n}", "accuracy": 1, "time_ms": 220, "memory_kb": 3260, "score_of_the_acc": -0.9477, "final_rank": 4 }, { "submission_id": "aoj_1265_459155", "code_snippet": "/*\n * Created By phyxnj@uestc\n * Last Compiler:\n * Sun, 29 Jul 2012 02:07:44 +0800\n */\n#include <cstdio>\n#include <cstring>\n#include <ctime>\n#include <algorithm>\n#include <cstdlib>\n#include <cmath>\n#include <set>\n#include <vector>\n#include <utility>\n#include <string>\n#include <map>\n#include <iostream>\nusing namespace std;\n#define eps 1e-7\n#define pw(x) ((x)*(x))\ntypedef long long LL;\nstruct P\n{\n double x,y;\n P () {}\n P (double x,double y) : x(x),y(y) {}\n P operator - (const P & b)\n {\n return P (x-b.x,y-b.y);\n }\n double operator * (const P & b)\n {\n return x*b.y-y*b.x;\n }\n double operator & (const P & b)\n {\n return x*b.x+y*b.y;\n }\n double dorm()\n {\n return sqrt(pw(x)+pw(y));\n }\n}p[300],s[300],tp[300];\nint sgn(double x)\n{\n return x<-eps?-1:x>eps;\n}\ndouble cal(P a,P b,P c) // 隶。邂幼蛻ーab逧?怙蟆剰キ晉ヲサ\n{\n int t1=sgn((c-a)&(b-a));\n int t2=sgn((c-b)&(a-b));\n if(t1>=0&&t2>=0) return fabs((a-c)*(a-b))/(a-b).dorm();\n return min((c-a).dorm(),(c-b).dorm());\n}\ndouble maybe[10000]; \nint cnt,flag;\ndouble L;\nint n,m;\nvoid run(P a,P b,P c) // 隶。邂苓ヲ∽スソ蠕幼荳斬b逧?怙蟆剰キ晉ヲサ荳コL隕∽スソc遘サ蜉ィ逧?キ晉ヲサ\n{\n double l=-20000,h=20000,mid1,mid2;\n for(int i=0;i<50;i++){\n mid1=(2*l+h)/3;\n mid2=(2*h+l)/3;\n double t1=cal(a,b,P(mid1,c.y));\n double t2=cal(a,b,P(mid2,c.y));\n if(sgn(t1-t2)>=0) l=mid1;\n else h=mid2;\n }\n double mid;\n l=-20000;h=mid1;\n for(int i=0;i<50;i++){\n mid=(l+h)/2;\n double t1=cal(a,b,P(mid,c.y));\n if(sgn(t1-L)>=0) l=mid;\n else h=mid;\n }\n double delta=mid-c.x;\n if(flag) delta=-delta;\n maybe[cnt++]=delta;\n l=mid1,h=20000;\n for(int i=0;i<50;i++){\n mid=(l+h)/2;\n double t1=cal(a,b,P(mid,c.y));\n if(sgn(t1-L)>=0) h=mid;\n else l=mid;\n }\n delta=mid-c.x;\n if(flag) delta=-delta;\n maybe[cnt++]=delta;\n}\nvoid creat()\n{\n cnt=0;\n s[m]=s[0],p[n]=p[0];\n flag=1;\n for(int i=0;i<n;i++){\n for(int j=0;j<m;j++){\n run(s[j],s[j+1],p[i]);\n }\n }\n flag=0;\n for(int i=0;i<m;i++){\n for(int j=0;j<n;j++){\n run(p[j],p[j+1],s[i]);\n }\n }\n for(int i=0;i<m;i++){\n for(int j=0;j<n;j++) {\n maybe[cnt++]=p[j].x-s[i].x;\n }\n }\n}\nint in(P a,P pt[],int n)\n{\n int cnt=0;\n pt[n]=pt[0];\n for(int i=0;i<n;i++){\n P x=pt[i],y=pt[i+1];\n if(sgn(x.y-y.y)>0) swap(x,y);\n if(sgn(x.y-a.y)>=0||sgn(y.y-a.y)<0) continue;\n double t=(x-a)*(y-a);\n if(sgn(t)>0) cnt++;\n }\n return cnt&1;\n}\ndouble solve()\n{\n double ans=1e10;\n for(int i=0;i<cnt;i++){\n double x=maybe[i];\n// printf(\"%f\\n\",x);\n for(int j=0;j<m;j++){\n tp[j].x=s[j].x+x;\n tp[j].y=s[j].y;\n }\n tp[m]=tp[0];\n int flag=0;\n for(int j=0;j<n;j++){\n for(int k=0;k<m;k++){\n int t1=sgn((p[j]-tp[k])*(tp[k+1]-tp[k]))*sgn((p[j+1]-tp[k])*(tp[k+1]-tp[k]));\n int t2=sgn((tp[k]-p[j])*(p[j+1]-p[j]))*sgn((tp[k+1]-p[j])*(p[j+1]-p[j]));\n if(t1<0&&t2<0) {\n flag=1;\n break;\n }\n double dis=min(min(cal(p[j],p[j+1],tp[k]),cal(p[j],p[j+1],tp[k+1])),min(cal(tp[k],tp[k+1],p[j]),cal(tp[k],tp[k+1],p[j+1])));\n if(dis-L<-1e-6) {\n flag=1;\n break;\n }\n }\n }\n if(flag) continue;\n flag=in(tp[0],p,n)||in(p[0],tp,m);\n if(flag) continue;\n double minx=1e10,maxx=-1e10;\n for(int j=0;j<m;j++){\n minx=min(minx,tp[j].x);\n maxx=max(maxx,tp[j].x);\n }\n for(int j=0;j<n;j++){\n minx=min(minx,p[j].x);\n maxx=max(maxx,p[j].x);\n }\n// printf(\"%f %f ************\\n\",maxx,minx);\n ans=min(ans,maxx-minx);\n }\n return ans;\n}\nint main()\n{ \n P a,b,c;\n while(scanf(\"%lf\",&L)&&sgn(L)){\n scanf(\"%d\",&n);\n for(int i=0;i<n;i++) scanf(\"%lf%lf\",&p[i].x,&p[i].y);\n scanf(\"%d\",&m);\n for(int i=0;i<m;i++) scanf(\"%lf%lf\",&s[i].x,&s[i].y);\n creat();\n printf(\"%f\\n\",solve());\n }\n return 0;\n}", "accuracy": 1, "time_ms": 900, "memory_kb": 964, "score_of_the_acc": -0.4229, "final_rank": 1 }, { "submission_id": "aoj_1265_275152", "code_snippet": "#include <stdio.h>\n#include <math.h>\n#include <iostream>\n#include <complex>\n#include <vector>\n#include <algorithm>\nusing namespace std;\n#define rep(i, n) for(int i=0; i<(int)(n); i++)\n#define INF (1e100)\n#define EPS (1e-5)\ntypedef complex<double> P;\ninline double sq(double a) { return a*a; }\n\ninline double cross(const P& a, const P& b) { return imag(conj(a)*b); }\ninline double dot(const P& a, const P& b) { return real(conj(a)*b); }\ninline P unit(const P& p) { return 1/abs(p)*p; }\nint ccw(const P& a, P b, P c) {\n b -= a; c -= a;\n if(cross(b, c) > 0) return 1;\n if(cross(b, c) < 0) return -1;\n if(dot(b, c) < 0) return 2;\n if(norm(b) < norm(c)) return -2;\n return 0;\n}\n\ninline double intersectSP(const P& s0, const P& s1, const P& p) {\n return abs(s0-p)+abs(s1-p)-abs(s1-s0) < EPS;\n}\n\nP projection(const P& l0, const P& l1, const P& p) {\n const double t = dot(p-l0, l1-l0) / norm(l1-l0);\n return l0 + t*(l1-l0);\n}\n\ndouble distanceSP(const P& s0, const P& s1, const P& p) {\n const P r = projection(s0, s1, p);\n if(intersectSP(s0, s1, r)) return abs(r-p);\n return min(abs(s0-p), abs(s1-p));\n}\n\nbool intersectSS(const P & s0, const P& s1, const P& t0, const P& t1) {\n return ccw(s0, s1, t0) * ccw(s0, s1, t1) <= 0\n && ccw(t0, t1, s0) * ccw(t0, t1, s1) <= 0;\n}\n\nP crosspoint(const P& l0, const P& l1, const P& m0, const P& m1) {\n const double A = cross(l1-l0, m1-m0);\n const double B = cross(l1-l0, l1-m0);\n if(abs(A)<EPS && abs(B)<EPS) return m0;\n // if(abs(A)<EPS) assert(false);\n return m0+B/A*(m1-m0);\n}\n\ndouble distanceSS(const P& s0, const P& s1, const P& t0, const P& t1) {\n if(intersectSS(s0, s1, t0, t1)) return 0;\n return min(min(distanceSP(s0, s1, t0), distanceSP(s0, s1, t1)),\n min(distanceSP(t0, t1, s0), distanceSP(t0, t1, s1)));\n}\n\nvector<P> crosspointsCL(const P& p, double r, const P& l0, const P& l1) {\n vector<P> v;\n const P cp(crosspoint(p, p+(l1-l0)*P(0, 1), l0, l1));\n if(abs(cp-p) < r+EPS) {\n const double a = sqrt(sq(r)-norm(p-cp));\n const P dir(a*unit(l1-l0));\n v.push_back(cp+dir); v.push_back(cp-dir);\n }\n return v;\n}\n\nbool contains(const vector<P>& ps, const P& p) {\n bool in = false;\n rep(i, ps.size()) {\n const int j = (i+1)%ps.size();\n P a(ps[i]-p), b(ps[j]-p);\n if(imag(a) > imag(b)) swap(a, b);\n if(imag(a) <= 0 && 0 < imag(b) && cross(a, b) < 0) in = !in;\n if(cross(a, b)==0 && dot(a, b) <= 0) return true; // on edge\n }\n return in;\n}\n\ndouble L;\nint n[2], xo[2][20], yo[2][20];\nP po[2][20];\n\nbool check(double dx) {\n vector<P> pm[2];\n rep(i, n[0]) pm[0].push_back(po[0][i]);\n rep(i, n[1]) pm[1].push_back(po[1][i] + P(dx, 0));\n rep(k, 2) rep(i, n[k]) if(contains(pm[1-k], pm[k][i])) return false;\n rep(i, n[0]) rep(j, n[1]) if(abs(pm[1][j]-pm[0][i])<L-EPS) return false;\n rep(k, 2) rep(i, n[k]) {\n const int j = (i+1)%n[k];\n rep(a, n[1-k]) {\n if(distanceSP(pm[k][i], pm[k][j], pm[1-k][a])<L-EPS) return false;\n }\n }\n rep(i, n[0]) rep(j, n[1]) {\n const int it = (i+1)%n[0];\n const int jt = (j+1)%n[1];\n if(distanceSS(pm[0][i], pm[0][it], pm[1][j], pm[1][jt])<L-EPS) {\n return false;\n }\n }\n return true;\n}\n\nint main() {\n for(;;) {\n scanf(\"%lf\", &L);\n if(fabs(L)<EPS) return 0;\n rep(k, 2) {\n scanf(\"%d\", n+k);\n rep(i, n[k]) scanf(\"%d%d\", xo[k]+i, yo[k]+i);\n rep(i, n[k]) po[k][i] = P(xo[k][i], yo[k][i]);\n }\n vector<double> cands;\n rep(i, n[0]) rep(j, n[1]) cands.push_back(xo[0][i]-xo[1][j]);\n rep(i, n[0]) rep(j, n[1]) {\n const double y = yo[1][j];\n vector<P> ps(crosspointsCL(po[0][i], L, P(-1000, y), P(1000, y)));\n rep(k, ps.size()) cands.push_back(ps[k].real()-xo[1][j]);\n }\n rep(k, 2) rep(i, n[k]) {\n const int j = (i+1)%n[k];\n rep(a, n[1-k]) {\n const P dir(L*unit(po[k][j]-po[k][i])*P(0, 1));\n vector<P> ps;\n ps.push_back(po[1-k][a]+dir);\n ps.push_back(po[1-k][a]-dir);\n rep(b, ps.size()) {\n const double y = yo[k][i];\n const P cp(crosspoint(ps[b], ps[b]+(po[k][j]-po[k][i]),\n P(-1000, y), P(1000, y)));\n if(k) cands.push_back(cp.real()-xo[k][i]);\n else cands.push_back(xo[k][i]-cp.real());\n }\n }\n }\n sort(cands.begin(), cands.end());\n cands.erase(unique(cands.begin(), cands.end()), cands.end());\n double ans = INF;\n rep(k, cands.size()) if(check(cands[k])) {\n double xmin = INF, xmax = -INF;\n rep(i, n[0]) {\n xmin = min(xmin, (double)xo[0][i]);\n xmax = max(xmax, (double)xo[0][i]);\n }\n rep(i, n[1]) {\n xmin = min(xmin, (double)xo[1][i]+cands[k]);\n xmax = max(xmax, (double)xo[1][i]+cands[k]);\n }\n ans = min(ans, xmax-xmin);\n }\n printf(\"%.12f\\n\", ans);\n }\n}", "accuracy": 1, "time_ms": 1040, "memory_kb": 1016, "score_of_the_acc": -0.5121, "final_rank": 2 }, { "submission_id": "aoj_1265_275145", "code_snippet": "#include <stdio.h>\n#include <math.h>\n#include <iostream>\n#include <complex>\n#include <vector>\n#include <algorithm>\nusing namespace std;\n#define rep(i, n) for(int i=0; i<(int)(n); i++)\n#define INF (1e100)\n#define EPS (1e-5)\ntypedef complex<double> P;\ninline double sq(double a) { return a*a; }\n\ninline double cross(const P& a, const P& b) { return imag(conj(a)*b); }\ninline double dot(const P& a, const P& b) { return real(conj(a)*b); }\ninline P unit(const P& p) { return 1/abs(p)*p; }\nint ccw(const P& a, P b, P c) {\n b -= a; c -= a;\n if(cross(b, c) > 0) return 1;\n if(cross(b, c) < 0) return -1;\n if(dot(b, c) < 0) return 2;\n if(norm(b) < norm(c)) return -2;\n return 0;\n}\n\ninline double intersectSP(const P& s0, const P& s1, const P& p) {\n return abs(s0-p)+abs(s1-p)-abs(s1-s0) < EPS;\n}\n\nP projection(const P& l0, const P& l1, const P& p) {\n const double t = dot(p-l0, l0-l1) / norm(l0-l1);\n return l0 + t*(l0-l1);\n}\n\ndouble distanceSP(const P& s0, const P& s1, const P& p) {\n const P r = projection(s0, s1, p);\n if(intersectSP(s0, s1, r)) return abs(r-p);\n return min(abs(s0-p), abs(s1-p));\n}\n\nbool intersectSS(const P & s0, const P& s1, const P& t0, const P& t1) {\n return ccw(s0, s1, t0)*ccw(s0, s1, t1) <= 0\n && ccw(t0, t1, s0)*ccw(t0, t1, s1) <= 0;\n}\n\nP crosspoint(const P& l0, const P& l1, const P& m0, const P& m1) {\n const double A = cross(l1-l0, m1-m0);\n const double B = cross(l1-l0, l1-m0);\n if(abs(A)<EPS && abs(B)<EPS) return m0;\n return m0+B/A*(m1-m0);\n}\n\ndouble distanceSS(const P& s0, const P& s1, const P& t0, const P& t1) {\n if(intersectSS(s0, s1, t0, t1)) return 0;\n return min(min(distanceSP(s0, s1, t0), distanceSP(s0, s1, t1)),\n min(distanceSP(t0, t1, s0), distanceSP(t0, t1, s1)));\n}\n\nvector<P> crosspointsCL(const P& p, double r, const P& l0, const P& l1) {\n vector<P> v;\n const P cp(crosspoint(p, p+(l1-l0)*P(0, 1), l0, l1));\n if(abs(cp-p)<EPS) {\n const P dir(r*unit(l1-l0));\n v.push_back(p+dir); v.push_back(p-dir);\n }\n else if(abs(cp-p)<r+EPS) {\n const double a = sqrt(sq(r)-norm(p-cp));\n const P dir(a*unit(cp-p)*P(0, 1));\n v.push_back(cp+dir); v.push_back(cp-dir);\n }\n return v;\n}\n\nbool contains(const vector<P>& ps, const P& p) {\n bool in = false;\n rep(i, ps.size()) {\n P a(ps[i]-p), b(ps[(i+1)%ps.size()]-p);\n if(imag(a) > imag(b)) swap(a, b);\n if(imag(a) <= 0 && 0 < imag(b) && cross(a, b) < 0) in = !in;\n if(cross(a, b)==0 && dot(a, b) <= 0) return true; // 'on'\n }\n return in;\n}\n\ndouble L;\nint n[2], xo[2][20], yo[2][20];\nP po[2][20];\n\nbool check(double dx) {\n vector<P> pm[2];\n rep(i, n[0]) pm[0].push_back(po[0][i]);\n rep(i, n[1]) pm[1].push_back(po[1][i] + P(dx, 0));\n rep(k, 2) rep(i, n[k]) if(contains(pm[1-k], pm[k][i])) return false;\n rep(i, n[0]) rep(j, n[1]) if(abs(pm[1][j]-pm[0][i])<L-EPS) return false;\n rep(k, 2) rep(i, n[k]) {\n const int j = (i+1)%n[k];\n rep(a, n[1-k]) {\n if(distanceSP(pm[k][i], pm[k][j], pm[1-k][a])<L-EPS) return false;\n }\n }\n rep(i, n[0]) rep(j, n[1]) {\n const int it = (i+1)%n[0];\n const int jt = (j+1)%n[1];\n if(distanceSS(pm[0][i], pm[0][it], pm[1][j], pm[1][jt])<L-EPS) {\n return false;\n }\n }\n return true;\n}\n\nint main() {\n for(;;) {\n scanf(\"%lf\", &L);\n if(fabs(L)<EPS) return 0;\n rep(k, 2) {\n scanf(\"%d\", n+k);\n rep(i, n[k]) scanf(\"%d%d\", xo[k]+i, yo[k]+i);\n rep(i, n[k]) po[k][i] = P(xo[k][i], yo[k][i]);\n }\n vector<double> cands;\n rep(i, n[0]) rep(j, n[1]) cands.push_back(xo[0][i]-xo[1][j]);\n rep(i, n[0]) rep(j, n[1]) {\n const double y = yo[1][j];\n vector<P> ps(crosspointsCL(po[0][i], L, P(-1000, y), P(1000, y)));\n rep(k, ps.size()) cands.push_back(ps[k].real()-xo[1][j]);\n }\n rep(k, 2) rep(i, n[k]) {\n const int j = (i+1)%n[k];\n rep(a, n[1-k]) {\n const P dir(L*unit(po[k][j]-po[k][i])*P(0, 1));\n vector<P> ps;\n ps.push_back(po[1-k][a]+dir);\n ps.push_back(po[1-k][a]-dir);\n rep(b, ps.size()) {\n const double y = yo[k][i];\n const P cp(crosspoint(ps[b], ps[b]+(po[k][j]-po[k][i]),\n P(-1000, y), P(1000, y)));\n if(k) cands.push_back(cp.real()-xo[k][i]);\n else cands.push_back(xo[k][i]-cp.real());\n }\n }\n }\n sort(cands.begin(), cands.end());\n cands.erase(unique(cands.begin(), cands.end()), cands.end());\n double ans = INF;\n rep(k, cands.size()) if(check(cands[k])) {\n double xmin = INF, xmax = -INF;\n rep(i, n[0]) {\n xmin = min(xmin, (double)xo[0][i]);\n xmax = max(xmax, (double)xo[0][i]);\n }\n rep(i, n[1]) {\n xmin = min(xmin, (double)xo[1][i]+cands[k]);\n xmax = max(xmax, (double)xo[1][i]+cands[k]);\n }\n ans = min(ans, xmax-xmin);\n }\n printf(\"%.12f\\n\", ans);\n }\n}", "accuracy": 1, "time_ms": 1050, "memory_kb": 1020, "score_of_the_acc": -0.5186, "final_rank": 3 }, { "submission_id": "aoj_1265_101023", "code_snippet": "#include<iostream>\n#include<cstdio>\n#include<vector>\n#include<cmath>\n#include<complex>\n#include<utility>\nusing namespace std;\n#define REP(i,b,n) for(int i=b;i<n;i++)\n#define rep(i,n) REP(i,0,n)\n#define ALL(C) (C).begin(),(C).end()\n#define mp make_pair\n#define pb push_back\n\ntypedef complex<double> P;\ntypedef pair<P,P> Line;\ntypedef pair<double,double> pdd;\n\nconst double eps = 1e-10;\nconst double INF = 10000;\n\ndouble cross(P a,P b){\n return a.real()*b.imag()-a.imag()*b.real();\n}\n\ndouble dot(P a,P b){\n return a.real()*b.real()+a.imag()*b.imag();\n}\n\ndouble isp(P a,P b,P c){\n return abs(a-c)+abs(b-c) < abs(a-b)+eps;\n}\n\ndouble distance_ls_p(P &a,P &b,P &c){\n if (dot(b-a,c-a) < eps)return abs(c-a);\n if (dot(a-b,c-b) < eps)return abs(c-b);\n return abs(cross(b-a,c-a))/abs(b-a);\n}\n\n#define D distance_ls_p\ndouble distance_ss_p(P a,P b,P c,P d){\n return min( min( D(a,b,c),D(a,b,d)),min(D(c,d,a),D(c,d,b)));\n}\n\nbool is_intersected_ls(P a1,P a2,P b1,P b2){\n if ( isp(a1,a2,b1))return true;\n if ( isp(a1,a2,b2))return true;\n if ( isp(b1,b2,a1))return true;\n if ( isp(b1,b2,a2))return true;\n if ( ( cross(a2-a1,b1-a1) * cross(a2-a1,b2-a1)<-eps ) && \n\t( cross(b2-b1,a1-b1)*cross(b2-b1,a2-b1)<-eps))return true;\n else return false;\n}\n\n\ndouble tri_search(double left,double right,Line &a,P &b){\n double mleft,mright;\n while(left < right){\n mleft =(right +2*left)/3;\n mright=(right*2+ left)/3;\n P tb(b.real(),b.imag());\n tb.real()=b.real()+mleft;\n double ldist=distance_ls_p(a.first,a.second,tb);\n tb.real()=b.real()+mright;\n double rdist=distance_ls_p(a.first,a.second,tb);\n if (rdist > ldist){\n right=mright-eps;\n }else {\n left=mleft+eps;\n } \n } \n return (right+left)/2;\n}\n\ndouble bsearch(double left,double right,Line &a,P &b,double L){\n double mid=0;\n rep(i,250){\n mid = (left+right)/2;\n P tb(b.real(),b.imag());\n tb.real()=b.real()+mid;\n double dist=distance_ls_p(a.first,a.second,tb);\n if (dist <L){\n left=mid+eps;\n }else {\n right=mid-eps;\n }\n }\n return mid;\n}\n\npdd find_L(Line &a,P &b,double L){\n if (a.first.imag() == a.second.imag()){\n double l = bsearch(min(a.first.real(),a.second.real()),-INF,a,b,L);\n double r = bsearch(max(a.first.real(),a.second.real()), INF,a,b,L); \n return mp(l,r);\n }else {\n double mid = tri_search(-INF,INF,a,b);\n double l=bsearch(mid,-INF,a,b,L);\n double r=bsearch(mid,INF,a,b,L);\n return mp(l,r);\n }\n}\n\npdd get_L(Line &a,P &b,double L){\n pdd Lpoint=find_L(a,b,L);\n return Lpoint;\n}\n\nvoid find_Ldif(Line a,Line b,double L,vector<double> &ret,vector<double>&ret2){\n pdd cand[4];\n cand[0]=get_L(a,b.first,L);\n cand[1]=get_L(a,b.second,L);\n cand[2]=get_L(b,a.first,L);\n cand[3]=get_L(b,a.second,L);\n \n rep(i,2){\n if (cand[i].first < -INF+1 || cand[i].first > INF-1);\n else ret.pb(cand[i].first);\n if (cand[i].second < -INF+1 || cand[i].second > INF-1);\n else ret.pb(cand[i].second);\n }\n\n REP(i,2,4){\n if (cand[i].first < -INF+1 || cand[i].first > INF-1);\n else ret2.pb(cand[i].first);\n if (cand[i].second < -INF+1 || cand[i].second > INF-1);\n else ret2.pb(cand[i].second);\n }\n}\n\nbool is_in(vector<P> &in,P a){\n int cnt=0;\n int n = in.size();\n rep(i,n){\n P cur = in[i]-a,next=in[(i+1)%n]-a;\n if (cur.imag() > next.imag())swap(cur,next);\n if (cur.imag()<0 && 0<=next.imag() &&\n\tcross(next,cur)>=0)cnt++;\n if (isp(in[i],in[(i+1)%n],a))return true;\n }\n if (cnt%2 == 1)return true;\n else return false;\n}\n\nbool isok(vector<P> &va,vector<P> &vb,double L){\n rep(i,va.size()){\n rep(j,vb.size()){\n double tmp = distance_ss_p(va[i],va[(i+1)%va.size()],vb[j],vb[(j+1)%vb.size()]);\n if (tmp+eps < L){\n\treturn false;\n }\n }\n }\n\n rep(i,vb.size()){\n if (is_in(va,vb[i]))return false;\n }\n\n rep(i,va.size()){\n if (is_in(vb,va[i]))return false;\n }\n\n rep(i,va.size()){\n rep(j,vb.size()){\n if (is_intersected_ls(va[i],va[(i+1)%va.size()],vb[j],vb[(j+1)%vb.size()]))return false;\n }\n }\n\n return true;\n}\n\ndouble compute_width(vector<P> &va,vector<P> &vb){\n double maxi=-INF,mini=INF;\n rep(i,va.size()){\n maxi=max(va[i].real(),maxi);\n mini=min(va[i].real(),mini);\n }\n rep(i,vb.size()){\n maxi=max(vb[i].real(),maxi);\n mini=min(vb[i].real(),mini);\n }\n return maxi-mini;\n}\n\ndouble solve(vector<P> &va,vector<P> vb,vector<double> &cand,double L){\n double ans =1e10;\n\n rep(i,cand.size()){\n rep(j,vb.size())vb[j].real()+=cand[i];\n if (isok(va,vb,L) ){\n ans=min(ans,compute_width(va,vb));\n }\n rep(j,vb.size())vb[j].real()-=cand[i];\n }\n return ans;\n}\n\n\nmain(){\n double L;\n while(cin>>L && L){\n vector<P> va,vb;\n P a;\n int n;\n cin>>n;\n rep(i,n){\n cin>>a.real()>>a.imag();\n va.pb(a);\n }\n \n int m;\n cin>>m;\n rep(i,m){\n cin>>a.real()>>a.imag();\n vb.pb(a);\n }\n\n vector<double> cand_a;\n vector<double> cand_b;\n rep(i,va.size()){\n rep(j,vb.size()){\n\tfind_Ldif(mp(va[i],va[(i+1)%va.size()]),mp(vb[j],vb[(j+1)%vb.size()]),L,cand_a,cand_b);\n }\n }\n \n double ans =min(solve(va,vb,cand_a,L),solve(vb,va,cand_b,L));\n printf(\"%.8lf\\n\",ans);\n }\n \n return false;\n}", "accuracy": 1, "time_ms": 2060, "memory_kb": 1024, "score_of_the_acc": -1.0226, "final_rank": 10 }, { "submission_id": "aoj_1265_98794", "code_snippet": "#include<iostream>\n#include<vector>\n#include<cmath>\n#include<complex>\nusing namespace std;\n#define REP(i,b,n) for(int i=b;i<n;i++)\n#define rep(i,n) REP(i,0,n)\n#define ALL(C) (C).begin(),(C).end()\n#define mp make_pair\n#define pb push_back\n\ntypedef complex<double> P;\ntypedef pair<P,P> Line;\ntypedef pair<double,double> pdd;\n\nconst double eps = 1e-10;\nconst double INF = 10000;\n\ndouble cross(P a,P b){\n return a.real()*b.imag()-a.imag()*b.real();\n}\n\ndouble dot(P a,P b){\n return a.real()*b.real()+a.imag()*b.imag();\n}\n\ndouble isp(P a,P b,P c){\n return abs(a-c)+abs(b-c) < abs(a-b)+eps;\n}\n\ndouble distance_ls_p(P &a,P &b,P &c){\n if (dot(b-a,c-a) < eps)return abs(c-a);\n if (dot(a-b,c-b) < eps)return abs(c-b);\n return abs(cross(b-a,c-a))/abs(b-a);\n}\n\n#define D distance_ls_p\ndouble distance_ss_p(P a,P b,P c,P d){\n return min( min( D(a,b,c),D(a,b,d)),min(D(c,d,a),D(c,d,b)));\n}\n\nbool is_intersected_ls(P a1,P a2,P b1,P b2){\n if ( isp(a1,a2,b1))return true;\n if ( isp(a1,a2,b2))return true;\n if ( isp(b1,b2,a1))return true;\n if ( isp(b1,b2,a2))return true;\n if ( ( cross(a2-a1,b1-a1) * cross(a2-a1,b2-a1)<-eps ) && \n\t( cross(b2-b1,a1-b1)*cross(b2-b1,a2-b1)<-eps))return true;\n else return false;\n}\n\n\ndouble tri_search(double left,double right,Line &a,P &b){\n double mleft,mright;\n while(left < right){\n mleft =(right +2*left)/3;\n mright=(right*2+ left)/3;\n P tb=b;\n tb.real()=b.real()+mleft;\n double ldist=distance_ls_p(a.first,a.second,tb);\n tb.real()=b.real()+mright;\n double rdist=distance_ls_p(a.first,a.second,tb);\n if (rdist > ldist){\n right=mright-eps;\n }else {\n left=mleft+eps;\n } \n } \n return (right+left)/2;\n}\n\ndouble bsearch(double left,double right,Line &a,P &b,double L){\n double mid=0;\n rep(i,250){\n mid = (left+right)/2;\n P tb=b;\n tb.real()=b.real()+mid;\n double dist=distance_ls_p(a.first,a.second,tb);\n if (dist <L){\n left=mid+eps;\n }else {\n right=mid-eps;\n }\n }\n return mid;\n}\n\npdd find_L(Line &a,P &b,double L){\n if (a.first.imag() == a.second.imag()){\n double l = bsearch(min(a.first.real(),a.second.real()),-INF,a,b,L);\n double r = bsearch(max(a.first.real(),a.second.real()), INF,a,b,L); \n return mp(l,r);\n }else {\n double mid = tri_search(-INF,INF,a,b);\n double l=bsearch(mid,-INF,a,b,L);\n double r=bsearch(mid,INF,a,b,L);\n return mp(l,r);\n }\n}\n\npdd get_L(Line &a,P &b,double L){\n pdd Lpoint=find_L(a,b,L);\n \n P tb=b;\n tb.real()=b.real()+Lpoint.first;\n tb.real()=b.real()+Lpoint.second;\n return Lpoint;\n}\n\nvoid find_Ldif(Line a,Line b,double L,vector<double> &ret,vector<double>&ret2){\n pdd cand[4];\n cand[0]=get_L(a,b.first,L);\n cand[1]=get_L(a,b.second,L);\n cand[2]=get_L(b,a.first,L);\n cand[3]=get_L(b,a.second,L);\n \n rep(i,2){\n if (cand[i].first < -INF+1 || cand[i].first > INF-1);\n else ret.pb(cand[i].first);\n if (cand[i].second < -INF+1 || cand[i].second > INF-1);\n else ret.pb(cand[i].second);\n }\n\n REP(i,2,4){\n if (cand[i].first < -INF+1 || cand[i].first > INF-1);\n else ret2.pb(cand[i].first);\n if (cand[i].second < -INF+1 || cand[i].second > INF-1);\n else ret2.pb(cand[i].second);\n }\n}\n\nbool is_in(vector<P> &in,P a){\n int cnt=0;\n int n = in.size();\n rep(i,n){\n P cur = in[i]-a,next=in[(i+1)%n]-a;\n if (cur.imag() > next.imag())swap(cur,next);\n if (cur.imag()<0 && 0<=next.imag() &&\n\tcross(next,cur)>=0)cnt++;\n if (isp(in[i],in[(i+1)%n],a))return true;\n }\n if (cnt%2 == 1)return true;\n else return false;\n}\n\nbool isok(vector<P> &va,vector<P> &vb,double L){\n rep(i,va.size()){\n rep(j,vb.size()){\n double tmp = distance_ss_p(va[i],va[(i+1)%va.size()],vb[j],vb[(j+1)%vb.size()]);\n if (tmp+eps < L){\n\treturn false;\n }\n }\n }\n\n rep(i,vb.size()){\n if (is_in(va,vb[i]))return false;\n }\n\n rep(i,va.size()){\n if (is_in(vb,va[i]))return false;\n }\n\n rep(i,va.size()){\n rep(j,vb.size()){\n if (is_intersected_ls(va[i],va[(i+1)%va.size()],vb[j],vb[(j+1)%vb.size()]))return false;\n }\n }\n\n return true;\n}\n\ndouble compute_width(vector<P> &va,vector<P> &vb){\n double maxi=-INF,mini=INF;\n rep(i,va.size()){\n maxi=max(va[i].real(),maxi);\n mini=min(va[i].real(),mini);\n }\n rep(i,vb.size()){\n maxi=max(vb[i].real(),maxi);\n mini=min(vb[i].real(),mini);\n }\n return maxi-mini;\n}\n\ndouble solve(vector<P> &va,vector<P> vb,vector<double> &cand,double L){\n double ans =1e10;\n\n rep(i,cand.size()){\n rep(j,vb.size())vb[j].real()+=cand[i];\n if (isok(va,vb,L) ){\n ans=min(ans,compute_width(va,vb));\n }\n rep(j,vb.size())vb[j].real()-=cand[i];\n }\n return ans;\n}\n\n\nmain(){\n double L;\n while(cin>>L && L){\n vector<P> va,vb;\n P a;\n int n;\n cin>>n;\n rep(i,n){\n cin>>a.real()>>a.imag();\n va.pb(a);\n }\n \n int m;\n cin>>m;\n rep(i,m){\n cin>>a.real()>>a.imag();\n vb.pb(a);\n }\n\n vector<double> cand_a;\n vector<double> cand_b;\n rep(i,va.size()){\n rep(j,vb.size()){\n\tfind_Ldif(mp(va[i],va[(i+1)%va.size()]),mp(vb[j],vb[(j+1)%vb.size()]),L,cand_a,cand_b);\n }\n }\n \n double ans =min(solve(va,vb,cand_a,L),solve(vb,va,cand_b,L));\n printf(\"%.8lf\\n\",ans);\n }\n \n return false;\n}", "accuracy": 1, "time_ms": 2020, "memory_kb": 1020, "score_of_the_acc": -1.0012, "final_rank": 9 } ]
aoj_1267_cpp
Problem B: How I Mathematician Wonder What You Are! After counting so many stars in the sky in his childhood, Isaac, now an astronomer and a mathematician, uses a big astronomical telescope and lets his image processing program count stars. The hardest part of the program is to judge if a shining object in the sky is really a star. As a mathematician, the only way he knows is to apply a mathematical definition of stars . The mathematical defiition of a star shape is as follows: A planar shape F is star-shaped if and only if there is a point C ∈ F such that, for any point P ∈ F , the line segment CP is contained in F . Such a point C is called a center of F . To get accustomed to the definition, let's see some examples below. Figure 2: Star shapes (the first row) and non-star shapes (the second row) The firrst two are what you would normally call stars. According to the above definition, however, all shapes in the first row are star-shaped. The two in the second row are not. For each star shape, a center is indicated with a dot. Note that a star shape in general has infinitely many centers. For example, for the third quadrangular shape, all points in it are centers. Your job is to write a program that tells whether a given polygonal shape is star-shaped or not. Input The input is a sequence of datasets followed by a line containing a single zero. Each dataset specifies a polygon, and is formatted as follows. n x 1 y 1 x 2 y 2 ... x n y n The first line is the number of vertices, n , which satisfies 4 ≤ n ≤ 50. Subsequent n lines are the x - and y -coordinates of the n vertices. They are integers and satisfy 0 ≤ x i ≤ 10000 and 0 ≤ y i ≤ 10000 ( i = 1, ..., n ). Line segments ( x i , y i )-( x i +1 , y i +1 ) ( i = 1, ..., n - 1) and the line segment ( x n , y n )-( x 1 , y 1 ) form the border of the polygon in the counterclockwise order. That is, these line segments see the inside of the polygon in the left of their directions. You may assume that the polygon is simple , that is, its border never crosses or touches itself. You may also assume that no three edges of the polygon meet at a single point even when they are infinitely extended. Output For each dataset, output "1" if the polygon is star-shaped and "0" otherwise. Each number must be in a separate line and the line should not contain any other characters. Sample Input 6 66 13 96 61 76 98 13 94 4 0 45 68 8 27 21 55 14 93 12 56 95 15 48 38 46 51 65 64 31 0 Output for the Sample Input 1 0
[ { "submission_id": "aoj_1267_3894056", "code_snippet": "#include <cstdio>\n#include <cstdlib>\n#include <cmath>\n#include <cstring>\n\n#include <iostream>\n#include <complex>\n#include <string>\n#include <algorithm>\n#include <numeric>\n#include <vector>\n#include <queue>\n#include <stack>\n#include <map>\n#include <set>\n#include <unordered_map>\n#include <unordered_set>\n\n#include <functional>\n#include <cassert>\n\ntypedef long long ll;\nusing namespace std;\n\n#ifndef LOCAL\n#define debug(x) ;\n#else\n#define debug(x) cerr << __LINE__ << \" : \" << #x << \" = \" << (x) << endl;\n\ntemplate <typename T1, typename T2>\nostream &operator<<(ostream &out, const pair<T1, T2> &p) {\n out << \"{\" << p.first << \", \" << p.second << \"}\";\n return out;\n}\n\ntemplate <typename T>\nostream &operator<<(ostream &out, const vector<T> &v) {\n out << '{';\n for (const T &item : v) out << item << \", \";\n out << \"\\b\\b}\";\n return out;\n}\n#endif\n\n#define mod 1000000007 //1e9+7(prime number)\n#define INF 1000000000 //1e9\n#define LLINF 2000000000000000000LL //2e18\n#define SIZE 200010\n\ntypedef double P_type; //座標(integer or real)\ntypedef double G_real; //実数の戻り値(float or double or long double)\ntypedef complex<P_type> P;\nconst G_real P_eps = 1e-8; //整数の時はゼロ\n\nnamespace std{\n template<class T> bool operator<(const complex<T> &a, const complex<T> &b){\n return abs(a.real() - b.real()) < P_eps ? a.imag() + P_eps < b.imag() : a.real() + P_eps < b.real();\n }\n};\n\nP rotate(P p, double theta){\n return p * P(cos(theta), sin(theta));\n}\n\n//内積\nP_type dot(P a, P b) {\n return (a * conj(b)).real();\n}\n\n//外積\nP_type cross(P a, P b) {\n return (conj(a) * b).imag();\n}\n\n//反時計回り\nint ccw(P a, P b, P c){\n if(cross(b-a, c-a) > P_eps) return 1; //COUNTER_CLOCKWISE(center:a)\n if(cross(b-a, c-a) < -P_eps) return -1; //CLOCKWISE(center:a)\n if(dot(b-a, c-a) < -P_eps) return -2; //c -> a -> b\n if(dot(a-b, c-b) < -P_eps) return 2; //a -> b -> c\n return 0; //a -> c -> b\n}\n\n/* 線分abと点cの距離 */\nG_real distanceSP(P a, P b, P c) {\n if ( dot(b-a, c-a) < P_eps ) return abs(c-a);\n if ( dot(a-b, c-b) < P_eps ) return abs(c-b);\n return abs(cross(b-a, c-a)) / abs(b-a);\n}\n\n/* 直線abと点cの距離 */\nG_real distanceLP(P a, P b, P c) {\n return abs(cross(b-a, c-a)) / abs(b-a);\n}\n\n/* 円の点包含判定 */\nbool isContainedCP(P c, P_type r, P p){\n return abs(c-p) < r - P_eps; //円周上を含まない\n //return abs(c-p) <= r + P_eps; //円周上を含む\n}\n\n/* 直線交差判定 */\nbool isIntersectedLL(P a1, P a2, P b1, P b2){\n return abs(cross(a1-a2, b1-b2)) > P_eps;\n}\n\n/* 線分交差判定 */\nbool isIntersectedSS(P a1, P a2, P b1, P b2){\n\n //線分a と 直線b\n int a = ccw(b1, b2, a1);\n int b = ccw(b1, b2, a2);\n\n //線分b と 直線a\n int c = ccw(a1, a2, b1);\n int d = ccw(a1, a2, b2);\n\n return a * b <= 0 && c * d < 0; // T字を除く時は (** < 0)\n}\n\n/* 直線A線分B交差判定 */\nbool isIntersectedLS(P a1, P a2, P b1, P b2){\n int a = ccw(a1, a2, b1);\n int b = ccw(a1, a2, b2);\n\n // 直線上のとき a or b = 0 or -2 or 2\n return (a % 2) * (b % 2) <= 0; // T字を除く時は (** < 0)\n}\n\n/* 円交差判定 */\nbool isIntersectedCC(P c1, G_real r1, P c2, P_type r2){\n G_real dist = abs(c1 - c2);\n\n return abs(r1 - r2) <= dist + P_eps && dist - P_eps <= r1 + r2; //外接内接を含む\n //return abs(r1 - r2) < dist - P_eps && dist + P_eps < r1 + r2; //外接内接を除く\n}\n\n/* 円直線交差判定 */\nbool isIntersectedCL(P c, G_real r, P a1, P a2){\n return distanceLP(a1, a2, c) <= r + P_eps; //接する場合を含まない場合 < r - P_eps\n}\n\n/* 円線分交差判定 */\nbool isIntersectedCS(P c, P_type r, P a1, P a2){\n return (!isContainedCP(c, r, a1) || !isContainedCP(c, r, a2)) &&\n distanceLP(a1, a2, c) <= r + P_eps; //接する場合を含まない場合 < r - P_eps\n}\n\n/* 直線/線分交点 */\nP getCrosspointLL(P a1, P a2, P b1, P b2) {\n //assert(isIntersectedLL(a1, a2, b1, b2));\n P a = a2 - a1;\n P b = b2 - b1;\n return a1 + a * cross(b, b1 - a1) / cross(b, a);\n}\n\nP getCrosspointSS(P a1, P a2, P b1, P b2){\n //assert(isIntersectedSS(a1, a2, b1, b2));\n return getCrosspointLL(a1, a2, b1, b2);\n}\n\n/* 円交点 */\npair<P,P> getCrosspointCC(P c1, P_type r1, P c2, P_type r2){\n //assert(isIntersectedCC(c1, r1, c2, r2));\n\n P_type dist = abs(c1 - c2);\n P_type a = acos((r1*r1 + dist*dist - r2*r2) / (2 * r1 * dist));\n return {c1 + polar(r1, arg(c2 - c1) + a), c1 + polar(r1, arg(c2 - c1) - a)};\n}\n\n/* 円直線交点 */\nvector<P> getCrosspointCL(P c, P_type r, P a1, P a2){\n if(!isIntersectedCL(c, r, a1, a2)) return {};\n\n P base1 = a2 - a1;\n P proj = a1 + base1 * dot(c - a1, base1) / norm(base1); //射影\n P e = (a2 - a1) / abs(a2 - a1);\n P base2 = sqrt(r*r - norm(proj - c));\n return {proj - e*base2, proj + e*base2};\n}\n\n/* 円線分交点 */\nvector<P> getCrosspointCS(P c, P_type r, P a1, P a2){\n if(!isIntersectedCS(c, r, a1, a2)) return {};\n\n vector<P> res;\n for(P p : getCrosspointCL(c, r, a1, a2))\n if(dot(a1-p, a2-p) <= P_eps) res.push_back(p);\n return res;\n}\n\n/* 多角形-点包含 */\nbool isContainedPolyP(vector<P> &g, P p){\n int n = g.size();\n bool f = false;\n for(int i=0; i<n; i++){\n P a = g[i] - p, b = g[(i+1)%n] - p;\n if(abs(cross(a,b)) < P_eps && dot(a,b) < P_eps) return true; //辺上\n if(a.imag() > b.imag()) swap(a,b);\n if(a.imag() < P_eps && P_eps < b.imag() && cross(a,b) > P_eps) f = !f;\n }\n return f; //内部 or 外部\n}\n\nbool isContainedPolyS(vector<P> &g, P a, P b) {\n\n if(!isContainedPolyP(g, a)) return false;\n if(!isContainedPolyP(g, b)) return false;\n\n P d = b - a;\n\n P a2 = a + d * (P_eps * 1e2);\n P b2 = b - d * (P_eps * 1e2);\n\n if(!isContainedPolyP(g, a2)) return false;\n if(!isContainedPolyP(g, b2)) return false;\n\n for(int i=0; i<g.size(); i++)\n if(isIntersectedSS(a, b, g[i], g[(i+1)%g.size()]))\n return false;\n\n return true;\n}\n\n\nbool solve() {\n int N;\n P p[51];\n vector<P> poly;\n\n scanf(\"%d\", &N);\n\n if (N == 0) return false;\n\n for (int i=0; i<N; i++) {\n int x, y;\n\n scanf(\"%d%d\", &x, &y);\n\n p[i] = P(x, y);\n poly.push_back(p[i]);\n }\n p[N] = p[0];\n\n for(int i=0; i<N; i++) {\n for(int j=i+1; j<N; j++) {\n if (isIntersectedLL(p[i], p[i+1], p[j], p[j+1])) {\n P g = getCrosspointLL(p[i], p[i+1], p[j], p[j+1]);\n\n if (!isContainedPolyP(poly, g)) continue;\n\n bool ok = true;\n for (int i=0; i<N && ok; i++) {\n ok &= isContainedPolyS(poly, g, p[i]);\n }\n\n if (ok) {\n puts(\"1\");\n return true;\n }\n }\n }\n }\n\n puts(\"0\");\n return true;\n}\n\n\nint main(){\n\n while(solve());\n\n return 0;\n}", "accuracy": 1, "time_ms": 20, "memory_kb": 3112, "score_of_the_acc": -0.0479, "final_rank": 9 }, { "submission_id": "aoj_1267_3639901", "code_snippet": "#include<iomanip>\n#include<limits>\n#include<thread>\n#include<utility>\n#include<iostream>\n#include<string>\n#include<algorithm>\n#include<set>\n#include<map>\n#include<vector>\n#include<stack>\n#include<queue>\n#include<cmath>\n#include<numeric>\n#include<cassert>\n#include<random>\n#include<chrono>\n#include<unordered_set>\n#include<unordered_map>\n#include<fstream>\n#include<list>\n#include<functional>\n#include<bitset>\n#include<complex>\n#include<tuple>\nusing namespace std;\ntypedef unsigned long long int ull;\ntypedef long long int ll;\ntypedef pair<ll,ll> pll;\ntypedef long double D;\ntypedef complex<D> P;\n#define F first\n#define S second\nconst ll E=1e18+7;\nconst ll MOD=1000000007;\n\n\ntemplate<typename T,typename U>istream & operator >> (istream &i,pair<T,U> &A){i>>A.F>>A.S; return i;}\ntemplate<typename T>istream & operator >> (istream &i,vector<T> &A){for(auto &I:A){i>>I;} return i;}\ntemplate<typename T,typename U>ostream & operator << (ostream &o,pair<T,U> &A){o<<A.F<<\" \"<<A.S; return o;}\ntemplate<typename T>ostream & operator << (ostream &o,vector<T> &A){ll i=A.size(); for(auto &I:A){o<<I<<(--i?\" \":\"\");} return o;}\ntemplate<typename T>vector<T> & cset(vector<T> &A,T e=T()){for(auto &I:A){I=e;} return A;}\n\nnamespace Geometry{\n typedef long double D;\n typedef complex<long double> P;\n typedef pair<P,D> C;\n \n const D EPS=1e-9;\n const D PI=asin(1)*2;\n const D INF=1e18;\n \n const static bool comp(const P &p1,const P &p2){return p1.real()==p2.real()?p1.imag()<p2.imag():p1.real()<p2.real();}\n \n D dot(P p1,P p2){return p1.real()*p2.real()+p1.imag()*p2.imag();}\n \n D cross(P p1,P p2){return p1.real()*p2.imag()-p1.imag()*p2.real();}\n \n P project(P vec,P x){return vec*(x/vec).real();}\n \n P project(P p1,P p2,P x){return p1+project(p2-p1,x-p1);}\n \n P reflect(P vec,P x){return vec*conj(x/vec);}\n \n P reflect(P p1,P p2,P x){return p1+reflect(p2-p1,x-p1);}\n \n bool intersectSL(P p1,P p2,P vec){vec/=abs(vec); p1/=vec; p2/=vec; return (p1.imag()<EPS && p2.imag()>-EPS) || (p1.imag()>-EPS && p2.imag()<EPS);}\n \n bool intersectSL(P p1,P p2,P p3,P p4){return intersectSL(p1-p4,p2-p4,p3-p4);}\n \n bool intersectSS(P p1,P p2,P p3,P p4){return (dot(p2-p1,p3-p1)<-EPS && dot(p2-p1,p4-p1)<-EPS) || (dot(p1-p2,p3-p2)<-EPS && dot(p1-p2,p4-p2)<-EPS)?false:intersectSL(p1,p2,p3,p4) && intersectSL(p3,p4,p1,p2);}\n \n D distLP(P vec,P x){return abs((x/vec).imag())*abs(vec);}\n \n D distLP(P p1,P p2,P x){return distLP(p2-p1,x-p1);}\n \n D distSP(P p1,P p2,P x){return dot(p2-p1,x-p1)<-EPS?abs(x-p1):dot(p1-p2,x-p2)<-EPS?abs(x-p2):distLP(p1,p2,x);}\n \n D distSS(P p1,P p2,P p3,P p4){return intersectSS(p1,p2,p3,p4)?0.0:min(min(distSP(p1,p2,p3),distSP(p1,p2,p4)),min(distSP(p3,p4,p1),distSP(p3,p4,p2)));}\n \n P crosspointLL(P p1,P p2,P vec){return abs(cross(p2-p1,vec))<EPS?vec:vec*cross(p2-p1,p2)/cross(p2-p1,vec);}\n \n P crosspointLL(P p1,P p2,P p3,P p4){return p4+crosspointLL(p1-p4,p2-p4,p3-p4);}\n \n P crosspointSS(P p1,P p2,P p3,P p4){return distSP(p1,p2,p3)<EPS?p3:distSP(p1,p2,p4)<EPS?p4:crosspointLL(p1,p2,p3,p4);}\n \n bool intersectShL(P p1,P p2,P vec){vec/=abs(vec); return intersectSL(p1,p2,vec) && crosspointLL(p1/vec,p2/vec,vec/vec).real()>-EPS;}\n \n bool intersectShL(P p1,P p2,P p3,P p4){return intersectShL(p1-p3,p2-p3,p4-p3);}\n \n //1::in,0::on edge,-1::out\n int contain(const vector<P> &poly,const P &p){\n vector<P> A={{65537,96847},{-24061,6701},{56369,-86509},{-93763,-78049},{56957,10007}};\n vector<bool> cnt(5,false);\n for(int i=1;i<=poly.size();i++){\n if(distSP(poly[i-1],poly[i%poly.size()],p)<EPS){return 0;}\n for(int j=0;j<5;j++){\n if(intersectShL(poly[i-1],poly[i%poly.size()],p,p+A[j])){cnt[j]=!cnt[j];}\n }\n }\n int in=0;\n for(int j=0;j<5;j++){if(cnt[j]){in++;}}\n return in>=3?1:-1;\n }\n \n vector<P> convexcut(const vector<P> &poly,P p1,P p2){\n vector<P> ret;\n for(int i=1;i<=poly.size();i++){\n if(cross(p2-p1,poly[i-1]-p1)>-EPS){ret.push_back(poly[i-1]);}\n if(intersectSL(poly[i-1],poly[i%poly.size()],p1,p2) && distLP(p1,p2,poly[i-1])>EPS && distLP(p1,p2,poly[i%poly.size()])>EPS){ret.push_back(crosspointLL(poly[i-1],poly[i%poly.size()],p1,p2));}\n }\n return ret;\n }\n \n D area(const vector<P> &poly){\n D ans=0;\n for(int i=2;i<poly.size();i++){ans+=cross(poly[i-1]-poly[0],poly[i]-poly[0]);}\n return abs(ans)/2;\n }\n \n vector<P> convexhull(vector<P> pts){\n vector<P> ret;\n sort(pts.begin(),pts.end(),comp);\n for(auto &I:pts){\n if(!ret.empty() && I==ret.back()){continue;}\n while(ret.size()>=2 && cross(ret.back()-ret[ret.size()-2],I-ret.back())<-EPS){ret.pop_back();}\n ret.push_back(I);\n }\n reverse(pts.begin(),pts.end());\n for(auto &I:pts){\n if(!ret.empty() && I==ret.back()){continue;}\n while(ret.size()>=2 && cross(ret.back()-ret[ret.size()-2],I-ret.back())<-EPS){ret.pop_back();}\n ret.push_back(I);\n }\n if(ret[0]==ret.back()){ret.pop_back();}\n return ret;\n }\n \n //4::seperate,3::circumscribe,2::intersect,1::inscribe,0::contain,-1::same\n int intersectCC(C c1,C c2){\n D d=abs(c1.F-c2.F),r=c1.S+c2.S,dif=abs(c2.S-c1.S);\n if(d<EPS && dif<EPS){return -1;}\n if(d-r>EPS){return 4;}\n if(d-r>-EPS){return 3;}\n if(d-dif>EPS){return 2;}\n if(d-dif>-EPS){return 1;}\n return 0;\n }\n \n vector<P> crosspointLC(P p1,P p2,C c){\n vector<P> ret;\n P pr=project(p1,p2,c.F);\n D d=distLP(p1,p2,c.F);\n if(d-c.S>EPS){return ret;}\n if(d-c.S>-EPS){ret.push_back(pr); return ret;}\n P vec=p2-p1; vec*=sqrt(c.S*c.S-d*d)/abs(vec);\n ret.push_back(pr-vec);\n ret.push_back(pr+vec);\n return ret;\n }\n \n vector<P> crosspointSC(P p1,P p2,C c){\n vector<P> ret;\n for(auto &I:crosspointLC(p1,p2,c)){if(distSP(p1,p2,I)<EPS){ret.push_back(I);}}\n return ret;\n }\n \n vector<P> crosspointCC(C c1,C c2){\n vector<P> ret;\n P vec=c2.F-c1.F;\n D base=(c1.S*c1.S+norm(vec)-c2.S*c2.S)/(2*abs(vec));\n D h=sqrt(c1.S*c1.S-base*base);\n vec/=abs(vec);\n ret.push_back(c1.F+vec*P(base,-h));\n ret.push_back(c1.F+vec*P(base,h));\n return ret;\n }\n \n vector<P> tangentCP(C c,P p){return crosspointCC(c,C(p,sqrt(norm(c.F-p)-c.S*c.S)));}\n \n vector<pair<P,P>> tangentCC(C c1,C c2){\n vector<pair<P,P>> ret;\n P d=c2.F-c1.F;\n for(D i:{-1,1}){\n D r=c1.S+c2.S*i;\n if(intersectCC(c1,c2)>i+1){\n for(P s:{-1i,1i}){\n P p=r+s*sqrt(norm(d)-norm(r));\n ret.push_back({c1.F+d*c1.S/norm(d)*p,c2.F-d*i*c2.S/norm(d)*p});\n }\n }\n }\n return ret;\n }\n \n D area(const vector<P> &poly,C c){\n D ret=0;\n for(int i=0;i<poly.size();i++){\n P a=poly[i]-c.F,b=poly[(i+1)%poly.size()]-c.F;\n if(abs(a)<c.S+EPS && abs(b)<c.S+EPS){ret+=cross(a,b);}\n else{\n vector<P> A=crosspointSC(a,b,{0,c.S});\n if(A.empty()){ret+=c.S*c.S*arg(b/a);}\n else{\n ret+=(abs(a)<c.S?cross(a,A[0]):c.S*c.S*arg(A[0]/a));\n ret+=(abs(b)<c.S?cross(A.back(),b):c.S*c.S*arg(b/A.back()));\n ret+=cross(A[0],A.back());\n }\n }\n }\n return abs(ret)/2;\n }\n \n //反時計回り\n D diameter(const vector<P> &poly){\n D ret=0;\n ll l=0,r=0,n=poly.size();\n if(n==2){return abs(poly[0]-poly[1]);}\n for(int i=0;i<n;i++){\n if(comp(poly[l],poly[i])){l=i;}\n if(comp(poly[i],poly[r])){r=i;}\n }\n ll sl=r,sr=l;\n while(sl!=l || sr!=r){\n ret=max(ret,abs(poly[r]-poly[l]));\n if(cross(poly[(l+1)%n]-poly[l],poly[(r+1)%n]-poly[r])<0){(++l)%=n;}\n else{(++r)%=n;}\n }\n return ret;\n }\n \n D closestpair(vector<P> pt){\n sort(pt.begin(),pt.end(),comp);\n D ret=INF;\n for(ll i=1;i<pt.size();i<<=1){\n for(ll j=0;i+j<pt.size();j+=i*2){\n ll m=i+j;\n vector<P> R;\n D l=-INF,r=INF;\n for(ll k=j;k<m;k++){l=max(l,pt[k].real());}\n for(ll k=0;m+k<pt.size() && k<i;k++){r=min(r,pt[m+k].real());}\n for(ll k=0;m+k<pt.size() && k<i;k++){if(pt[m+k].real()-l<ret){R.push_back(pt[m+k]);}}\n ll idx=0;\n for(ll k=j;k<m;k++){\n if(r-pt[k].real()>ret){continue;}\n while(idx<R.size() && pt[k].imag()-R[idx].imag()>ret){idx++;}\n for(ll n=idx;n<R.size() && R[n].imag()-pt[k].imag()<ret;n++){ret=min(ret,abs(R[n]-pt[k]));}\n }\n inplace_merge(pt.begin()+j,pt.begin()+m,j+i*2<pt.size()?pt.begin()+j+2*i:pt.end(),[](const P &a,const P &b){return a.imag()==b.imag()?a.real()<b.real():a.imag()<b.imag();});\n }\n }\n return ret;\n }\n \n istream & operator >> (istream &i,P &p){D x,y; i>>x>>y; p={x,y}; return i;}\n istream & operator >> (istream &i,C &p){D x,y; i>>x>>y>>p.S; p.F={x,y}; return i;}\n};\n\nusing namespace Geometry;\n\n\n\nint main(){\n ll n;\n while(cin>>n,n){\n vector<P> A(n);\n cin>>A;\n vector<P> B=convexhull(A);\n vector<P> C=B;\n for(int i=1;i<=n;i++){\n if(contain(B,A[i-1])==1 || contain(B,A[i%n])==1){\n C=convexcut(C,A[i-1],A[i%n]);\n }\n }\n cout<<(C.empty()?0:1)<<endl;\n }\n \n return 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3228, "score_of_the_acc": -0.0476, "final_rank": 8 }, { "submission_id": "aoj_1267_3638702", "code_snippet": "#include<iostream>\n#include<string>\n#include<algorithm>\n#include<vector>\n#include<iomanip>\n#include<math.h>\n#include<complex>\n#include<queue>\n#include<deque>\n#include<stack>\n#include<map>\n#include<set>\n#include<bitset>\n#include<functional>\n#include<assert.h>\n#include<numeric>\nusing namespace std;\n#define REP(i,m,n) for(int i=(int)(m) ; i < (int) (n) ; ++i )\n#define rep(i,n) REP(i,0,n)\nusing ll = long long;\nconst int inf=1e9+7;\nconst ll longinf=1LL<<60 ;\nconst ll mod=1e9+7 ;\n\nusing ld = double;\nusing Point = complex<ld>;\nconst ld eps = 1e-9;\nconst ld pi = acos(-1.0);\n\nbool eq(ld a, ld b) {\n\treturn (abs(a - b) < eps);\n}\n\nbool cmp(Point x,Point y){\n\tif(eq(x.real(),y.real()))return x.imag()<y.imag();\n\treturn x.real()<y.real();\n}\n\nbool eqq(Point x,Point y){\n\treturn eq(x.real(),y.real())&&eq(x.imag(),y.imag());\n}\n//内積\nld dot(Point a, Point b) {\n\treturn real(conj(a)*b);\n}\n//外積\nld cross(Point a, Point b) {\n\treturn imag(conj(a)*b);\n}\n\n\n//線分\n//直線にするなら十分通い2点を端点とすればよい\nclass Line {\npublic:\n\tPoint a, b;\n};\n//円\nclass Circle {\npublic:\n\tPoint p;\n\tld r;\n};\n//3点の位置関係\nint ccw(Point a, Point b, Point c) {\n\tb -= a; c -= a;\n\tif (cross(b, c) > eps)return 1;//a,b,cが反時計回り\n\tif (cross(b, c) < -eps)return -1;//a,b,cが時計回り\n\tif (dot(b, c) < 0)return 2;//c,a,bの順に一直線\n\tif (norm(b) < norm(c))return -2;//a,b,cの順に一直線\n\treturn 0;//a,c,bの順に一直線\n}\n//2直線の交差判定\nbool isis_ll(Line l, Line m) {\n\treturn !eq(cross(l.b - l.a, m.b - m.a), 0);\n}\n//直線と線分の交差判定\nbool isis_ls(Line l, Line s) {\n\treturn (cross(l.b - l.a, s.a - l.a)*cross(l.b - l.a, s.b - l.a) < eps);\n}\n//点が直線上に存在するか\nbool isis_lp(Line l, Point p) {\n\treturn (abs(cross(l.b - p, l.a - p)) < eps);\n}\n//点が線分上に存在するか\nbool isis_sp(Line s, Point p) {\n\treturn (abs(s.a - p) + abs(s.b - p) - abs(s.b - s.a) < eps);\n}\n//線分と線分の交差判定\nbool isis_ss(Line s, Line t) {\n\tif (isis_sp(t, s.a) || isis_sp(t, s.b))return false;\n\tif (isis_sp(s, t.a) || isis_sp(s, t.b) )return false;\n\treturn(cross(s.b - s.a, t.a - s.a)*cross(s.b - s.a, t.b - s.a) < -eps && cross(t.b - t.a, s.a - t.a)*cross(t.b - t.a, s.b - t.a) < -eps);\n}\n//点から直線への垂線の足\nPoint proj(Line l, Point p) {\n\tld t = dot(p - l.a, l.a - l.b) / norm(l.a - l.b);\n\treturn l.a + t * (l.a - l.b);\n}\n\nPoint cont(Line l, Point p){\n\treturn 2.0*proj(l,p) - p;\n}\n//直線と直線の交点\n//平行な2直線に対しては使うな!!!!\nPoint is_ll(Line s, Line t) {\n\tPoint sv = s.b - s.a; Point tv = t.b - t.a;\n\treturn s.a + sv * cross(tv, t.a - s.a) / cross(tv, sv);\n}\n//直線と点の距離\nld dist_lp(Line l, Point p) {\n\treturn abs(p - proj(l, p));\n}\n//直線と直線の距離\nld dist_ll(Line l, Line m) {\n\treturn isis_ll(l, m) ? 0 : dist_lp(l, m.a);\n}\n//線分と直線の距離\nld dist_ls(Line l, Line s) {\n\treturn isis_ls(l, s) ? 0 : min(dist_lp(l, s.a), dist_lp(l, s.b));\n}\n//線分と点の距離\nld dist_sp(Line s, Point p) {\n\tPoint r = proj(s, p);\n\treturn isis_sp(s, r) ? abs(p - r) : min(abs(p - s.a), abs(p - s.b));\n}\n//線分と線分の距離\nld dist_ss(Line s, Line t) {\n\tif (isis_ss(s, t))return 0;\n\treturn min({ dist_sp(s,t.a),dist_sp(s,t.b),dist_sp(t,s.a),dist_sp(t,s.b) });\n}\n\nbool included(vector<Point>& pol,Point p){\n double sum = 0;\n int n=pol.size();\n rep(i,n){\n Line l ={pol[i],pol[(i+1)%n]};\n if(isis_sp(l,p))return true;\n sum+=arg((pol[(i+1)%n]-p)/(pol[i]-p));\n }\n return abs(sum)>1;\n}\n\nvoid solve(int n){\n vector<Point> p(n);\n rep(i,n){\n int x,y;\n cin>>x>>y;\n p[i]=Point(x,y);\n }\n rep(i,n)rep(j,i){\n Line l1 = {p[i],p[(i+1)%n]};\n Line l2 = {p[j],p[(j+1)%n]};\n if(cross(l1.a-l1.b,l2.a-l2.b)<eps)continue;\n Point cr = is_ll(l1,l2);\n if(!included(p,cr))continue;\n bool can=true;\n rep(k,n){\n Line sg = {cr,p[k]};\n Point mid = (cr+p[k])*.5;\n if(!included(p,mid))can=false;\n rep(l,n){\n if(isis_ss(sg,{p[l],p[(l+1)%n]}))can = false;\n }\n }\n if(can){\n cout<<1<<endl;\n return;\n }\n }\n cout<<0<<endl;\n return;\n}\n\nint main(){\n int n;\n while(cin>>n,n)solve(n);\n return 0;\n}", "accuracy": 1, "time_ms": 1060, "memory_kb": 3244, "score_of_the_acc": -0.3227, "final_rank": 10 }, { "submission_id": "aoj_1267_2290767", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\ntypedef complex<double> P;\ntypedef pair< P , P > L;\ntypedef pair< P , P > S;\ntypedef vector<P> vecP;\n \ndouble eps = 1e-8;\n \nbool eq(double a,double b){\n return -eps < a-b && a-b < eps; \n}\n \ndouble cross(P a,P b){\n return imag( b*conj(a) );\n}\n \ndouble dot(P a,P b){\n return real( b*conj(a) );\n}\n \nbool isParallel(L a,L b){\n a.first -= a.second;\n b.first -= b.second;\n return eq(cross( a.first,b.first ),0);\n}\n \nP getCrossLL(L l0,L l1){\n P a=l0.first, b=l0.second;\n P c=l1.first, d=l1.second;\n a-=d;b-=d;c-=d;\n return d+a+(b-a)*imag(a/c)/imag(a/c-b/c);\n}\n \nint N;\nP t[50];\nL lines[1250];\nP u[1250*1250];\n \nint main(){\n while(1){\n scanf(\"%d\",&N);\n if(N==0)break;\n for(int i=0;i<N;i++){\n int x,y;\n scanf(\"%d %d\",&x,&y);\n t[i]=P(x,y);\n }\n \n int cc=0; \n for(int i=0;i<N;i++)\n for(int j=0;j<i;j++)\n lines[cc++]=L(t[i],t[j]);\n \n int uc=0;\n for(int i=0;i<cc;i++)\n for(int j=0;j<i;j++)\n if( !isParallel(lines[i],lines[j]) )\n u[uc++]=getCrossLL(lines[i],lines[j]);\n \n int ans=0;\n for(int i=0;i<uc;i++){\n bool flg=true;\n P cen=u[i];\n for(int j=0;j<N;j++){\n P a=t[j];\n P b=t[ (j+1==N?0:j+1) ];\n \n if( cross(a-cen,b-cen) < -eps ){\n flg=false;\n break;\n }\n \n }\n \n if(flg){\n ans=1;\n break;\n }\n }\n \n printf(\"%d\\n\",ans);\n }\n \n return 0;\n}", "accuracy": 1, "time_ms": 310, "memory_kb": 27656, "score_of_the_acc": -0.6038, "final_rank": 13 }, { "submission_id": "aoj_1267_2290281", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\ntypedef complex<double> P;\ntypedef pair< P , P > L;\ntypedef pair< P , P > S;\ntypedef vector<P> vecP;\n\ndouble eps = 1e-8;\n\nbool eq(double a,double b){\n return -eps < a-b && a-b < eps; \n}\n\ndouble cross(P a,P b){\n return imag( b*conj(a) );\n}\n\ndouble dot(P a,P b){\n return real( b*conj(a) );\n}\n\nbool isParallel(L a,L b){\n a.first -= a.second;\n b.first -= b.second;\n return eq(cross( a.first,b.first ),0);\n}\n\nP getCrossLL(L l0,L l1){\n P a=l0.first, b=l0.second;\n P c=l1.first, d=l1.second;\n a-=d;b-=d;c-=d;\n return d+a+(b-a)*imag(a/c)/imag(a/c-b/c);\n}\n\nint N;\nP t[50];\nL lines[1250];\nP u[1250*1250];\n\nint main(){\n while(1){\n scanf(\"%d\",&N);\n if(N==0)break;\n for(int i=0;i<N;i++){\n int x,y;\n scanf(\"%d %d\",&x,&y);\n t[i]=P(x,y);\n }\n \n int cc=0; \n for(int i=0;i<N;i++)\n for(int j=0;j<i;j++)\n lines[cc++]=L(t[i],t[j]);\n\n int uc=0;\n for(int i=0;i<cc;i++)\n for(int j=0;j<i;j++)\n if( !isParallel(lines[i],lines[j]) )\n u[uc++]=getCrossLL(lines[i],lines[j]);\n\n int ans=0;\n for(int i=0;i<uc;i++){\n bool flg=true;\n P cen=u[i];\n for(int j=0;j<N;j++){\n P a=t[j];\n P b=t[ (j+1==N?0:j+1) ];\n \n if( cross(a-cen,b-cen) < -eps ){\n flg=false;\n break;\n }\n \n }\n \n if(flg){\n ans=1;\n break;\n }\n }\n\n printf(\"%d\\n\",ans);\n }\n \n return 0;\n}", "accuracy": 1, "time_ms": 310, "memory_kb": 27560, "score_of_the_acc": -0.6019, "final_rank": 12 }, { "submission_id": "aoj_1267_1130563", "code_snippet": "#include<stdio.h>\n#include<algorithm>\n#include<vector>\n#include<math.h>\nusing namespace std;\nconst double EPS = 1e-10;\nconst double INF = 1e+10;\nconst double PI = acos(-1);\nint sig(double r) { return (r < -EPS) ? -1 : (r > +EPS) ? +1 : 0; }\ninline double ABS(double a){return max(a,-a);}\nstruct Pt {\n\tdouble x, y;\n\tPt() {}\n\tPt(double x, double y) : x(x), y(y) {}\n\tPt operator+(const Pt &a) const { return Pt(x + a.x, y + a.y); }\n\tPt operator-(const Pt &a) const { return Pt(x - a.x, y - a.y); }\n\tPt operator*(const Pt &a) const { return Pt(x * a.x - y * a.y, x * a.y + y * a.x); }\n\tPt operator-() const { return Pt(-x, -y); }\n\tPt operator*(const double &k) const { return Pt(x * k, y * k); }\n\tPt operator/(const double &k) const { return Pt(x / k, y / k); }\n\tdouble ABS() const { return sqrt(x * x + y * y); }\n\tdouble abs2() const { return x * x + y * y; }\n\tdouble arg() const { return atan2(y, x); }\n\tdouble dot(const Pt &a) const { return x * a.x + y * a.y; }\n\tdouble det(const Pt &a) const { return x * a.y - y * a.x; }\n};\ndouble tri(const Pt &a, const Pt &b, const Pt &c) { return (b - a).det(c - a); }\nint iSP(Pt a, Pt b, Pt c) {\n\tint s = sig((b - a).det(c - a));\n\tif (s) return s;\n\tif (sig((b - a).dot(c - a)) < 0) return -2; // c-a-b\n\tif (sig((a - b).dot(c - b)) < 0) return +2; // a-b-c\n\treturn 0;\n}\nint iLL(Pt a, Pt b, Pt c, Pt d) {\n\tif (sig((b - a).det(d - c))) return 1; // intersect\n\tif (sig((b - a).det(c - a))) return 0; // parallel\n\treturn -1; // correspond\n}\nPt pLL(Pt a, Pt b, Pt c, Pt d) {\n\tb = b - a; d = d - c; return a + b * (c - a).det(d) / b.det(d);\n}\nPt p[60];\nint main(){\n\tint a;\n\twhile(scanf(\"%d\",&a),a){\n\t\tfor(int i=0;i<a;i++){\n\t\t\tdouble x,y;scanf(\"%lf%lf\",&x,&y);\n\t\t\tp[i]=Pt(x,y);\n\t\t}p[a]=p[0];\n\t\tvector<Pt>v;\n\t\tfor(int i=0;i<a;i++)for(int j=i+1;j<a;j++)for(int k=0;k<a;k++)for(int l=k+1;l<a;l++){\n\t\t\tif(iLL(p[i],p[j],p[k],p[l])==1)v.push_back(pLL(p[i],p[j],p[k],p[l]));\n\t\t}\n\t\tint ret=0;\n\t\tfor(int i=0;i<v.size();i++){\n\t\t\tbool ok=true;\n\t\t\tfor(int j=0;j<a;j++){\n\t\t\t\tif(iSP(p[j],v[i],p[j+1])==1)ok=false;\n\t\t\t}\n\t\t\tif(ok)ret=1;\n\t\t}\n\t\tprintf(\"%d\\n\",ret);\n\t}\n}", "accuracy": 1, "time_ms": 3620, "memory_kb": 51932, "score_of_the_acc": -1.945, "final_rank": 15 }, { "submission_id": "aoj_1267_517746", "code_snippet": "//31\n#include<iostream>\n#include<complex>\n#include<cmath>\n\nusing namespace std;\n\ntypedef complex<double> P;\n\nP p[50];\nint n;\n\ndouble cr(P a,P b){\n return a.real()*b.imag()-a.imag()*b.real();\n}\n\nbool in(P a,P b,P c,P d){\n bool f[2]={};\n for(int i=0;i<3;i++){\n P pa[]={b,c,d};\n double c=cr(a-pa[i],pa[i+1]-pa[i]);\n if(fabs(c)>1e-9){\n f[c>0]=true;\n }\n }\n return !(f[0]&&f[1]);\n}\n\nbool ip(P cp){\n bool f=false;\n for(int px=0;px<n;px++){\n int nx=(px+1)%n;\n f|=cr(p[nx]-cp,p[px]-cp)>1e-9;\n }\n return !f;\n}\n\nint main(){\n while(cin>>n,n){\n for(int i=0;i<n;i++){\n int x,y;\n cin>>x>>y;\n p[i]=P(x,y);\n }\n int i;\n for(i=0;i<n;i++){\n for(int j=0;j<i;j++){\n\tfor(int k=0;k<n;k++){\n\t for(int l=0;l<k;l++){\n\t if(fabs(cr(p[i]-p[j],p[k]-p[l]))<1e-9)continue;\n\t double a=cr(p[k]-p[l],p[i]-p[l]);\n\t double b=cr(p[k]-p[l],p[j]-p[l]);\n\t P ic=p[i]+(p[j]-p[i])*a/(a-b);\n\t int m;\n\t for(m=0;m<n;m++){\n\t if(!in(ic,p[m],p[(m+1)%n],p[(m+2)%n]))break;\n\t }\n\t if(m!=n&&ip(ic))goto end;\n\t }\n\t}\n }\n }\n end:\n cout<<(i!=n)<<endl;\n }\n return 0;\n}", "accuracy": 1, "time_ms": 2280, "memory_kb": 1160, "score_of_the_acc": -0.6014, "final_rank": 11 }, { "submission_id": "aoj_1267_204257", "code_snippet": "#include<cmath>\n#include<cstdio>\n#include<vector>\n#include<complex>\n#include<algorithm>\n\n#define pb push_back\n#define rep(i,n) for(int i=0;i<n;i++)\n\nusing namespace std;\n\ntypedef complex<double> Point;\ntypedef vector<Point> Polygon;\n\nconst double EPS=1e-7;\n\nclass Line:public vector<Point>{\npublic:\n\tLine(){}\n\tLine(const Point &a,const Point &b){ pb(a), pb(b); }\n};\n\nclass Segment:public Line{\npublic:\n\tSegment(){}\n\tSegment(const Point &a,const Point &b):Line(a,b){}\n};\n\ndouble dot(const Point &a,const Point &b){\n\treturn real(a)*real(b)+imag(a)*imag(b);\n}\n\ndouble cross(const Point &a,const Point &b){\n\treturn real(a)*imag(b)-imag(a)*real(b);\n}\n\nenum {CCW=1,CW=-1,ON=0};\nint ccw(const Point &a,Point b,Point c){\n\tb-=a,c-=a;\n\tdouble rotdir=cross(b,c);\n\tif(rotdir>EPS) return CCW;\n\tif(rotdir<-EPS) return CW;\n\treturn ON;\n}\n\ninline void calc_abc(const Line &l,double &a,double &b,double &c){\n\ta=imag(l[0])-imag(l[1]);\n\tb=real(l[1])-real(l[0]);\n\tc=cross(l[0],l[1]);\n}\n\nbool cover(const Segment &s,const Point &p){\n\treturn dot(s[0]-p,s[1]-p)<EPS;\n}\n\nbool intersect(const Line &l,const Line &m,Point &p){\n\tif(abs(cross(l[1]-l[0],m[1]-m[0]))>EPS\n\t|| abs(cross(l[1]-l[0],m[0]-l[0]))<EPS){\n\t\tdouble a1,b1,c1,a2,b2,c2;\n\t\tcalc_abc(l,a1,b1,c1);\n\t\tcalc_abc(m,a2,b2,c2);\n\t\tdouble det=a1*b2-a2*b1;\n\t\tif(abs(det)<EPS) p=l[0];\n\t\telse p=Point(b1*c2-b2*c1,a2*c1-a1*c2)/det;\n\t\treturn true;\n\t}\n\treturn false;\n}\n\nbool intersect(const Segment &s,const Segment &t,Point &p){\n\tbool flg;\n\tif(abs(cross(s[1]-s[0],t[1]-t[0]))<EPS && abs(cross(s[1]-s[0],t[0]-s[0]))<EPS){\n\t\tflg=(cover(s,t[0]) || cover(s,t[1]) || cover(t,s[0]) || cover(t,s[1]));\n\t\tif(!flg) return false;\n\t}\n\n\tif(flg || (ccw(s[0],s[1],t[0])*ccw(s[0],s[1],t[1])<=0\n\t\t\t&& ccw(t[0],t[1],s[0])*ccw(t[0],t[1],s[1])<=0)){\n\t\tdouble a1,b1,c1,a2,b2,c2;\n\t\tcalc_abc(s,a1,b1,c1);\n\t\tcalc_abc(t,a2,b2,c2);\n\t\tdouble det=a1*b2-a2*b1;\n\t\tif(abs(det)<EPS){ // s is parallel to t\n\t\t\tPoint q[3]={s[0],s[1],t[0]};\n\t\t\trep(i,3){\n\t\t\t\tif(cover(s,q[i]) && cover(t,q[i])){\n\t\t\t\t\tp=q[i];\n\t\t\t\t\tbreak;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\telse p=Point(b1*c2-b2*c1,a2*c1-a1*c2)/det;\n\t\treturn true;\n\t}\n\treturn false;\n}\n\nbool cover(const Polygon &g,const Point &p){\n\tint n=g.size();\n\tbool in=false;\n\trep(i,n){\n\t\tPoint v1=g[i]-p,v2=g[(i+1)%n]-p;\n\t\tif(imag(v1)>imag(v2)) swap(v1,v2);\n\t\t// include lower vertex, not include upper vertex\n\t\tif(imag(v1)<EPS && EPS<imag(v2) && cross(v1,v2)>EPS) in=!in; // alternate\n\t\tif(abs(cross(v1,v2))<EPS && dot(v1,v2)<EPS)\treturn true; // p is on g[i]-g[i+1]\n\t}\n\treturn in;\n}\n\nbool isViewableAllVertices(const Polygon &g,const Point &p){\n\tint n=g.size();\n\n\trep(i,n)if(abs(g[i]-p)>EPS){ // for each vertex g[i](!=p) in g\n\t\tPoint u=g[i]-p; u/=abs(u);\n\t\tif(!cover(g,p+1e-5*u) || !cover(g,g[i]-1e-5*u)) return false;\n\n\t\tSegment s(p,g[i]);\n\t\trep(j,n){ // for each side t in g\n\t\t\tSegment t(g[j],g[(j+1)%n]);\n\t\t\tPoint q;\n\t\t\t// p-q-g[i] is on the same line (this order)\n\t\t\tif(intersect(s,t,q)){\n\t\t\t\t// if q != p, g[i]\n\t\t\t\tif(abs(p-q)>EPS && abs(g[i]-q)>EPS){\n\t\t\t\t\tif(!cover(g,q-1e-5*u) || !cover(g,q+1e-5*u)) return false;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n\n\treturn true;\n}\n\nint main(){\n\tfor(int n;scanf(\"%d\",&n),n;){\n\t\tPolygon g;\n\t\trep(i,n){\n\t\t\tint x,y; scanf(\"%d%d\",&x,&y);\n\t\t\tg.pb(Point(x,y));\n\t\t}\n\n\t\tbool b=false;\n\t\trep(i,n) rep(j,i) {\n\t\t\tPoint p;\n\t\t\tLine l(g[i],g[(i+1)%n]),m(g[j],g[(j+1)%n]);\n\t\t\tif(intersect(l,m,p) && cover(g,p)){\n\t\t\t\tif(isViewableAllVertices(g,p)){ b=true; goto FOUND; }\n\t\t\t}\n\t\t}\n\n\t\tFOUND:\n\t\tputs(b?\"1\":\"0\");\n\t}\n\n\treturn 0;\n}", "accuracy": 1, "time_ms": 60, "memory_kb": 800, "score_of_the_acc": -0.0132, "final_rank": 3 }, { "submission_id": "aoj_1267_204191", "code_snippet": "#include<cmath>\n#include<cstdio>\n#include<vector>\n#include<complex>\n#include<algorithm>\n\n#define pb push_back\n#define rep(i,n) for(int i=0;i<n;i++)\n\nusing namespace std;\n\ntypedef complex<double> Point;\ntypedef vector<Point> Polygon;\n\nconst double EPS=1e-7;\n\nnamespace std{\n\tbool operator <(const Point &a,const Point &b){\n\t\treturn abs(real(a)-real(b))<EPS?(imag(a)+EPS<imag(b)):(real(a)+EPS<real(b));\n\t}\n\tbool operator >(const Point &a,const Point &b){\n\t\treturn b<a;\n\t}\n}\n\nclass Line:public vector<Point>{\npublic:\n\tLine(){}\n\tLine(const Point &a,const Point &b){ pb(a), pb(b); }\n};\n\nclass Segment:public Line{\npublic:\n\tSegment(){}\n\tSegment(const Point &a,const Point &b):Line(a,b){}\n};\n\ndouble dot(const Point &a,const Point &b){\n\treturn real(a)*real(b)+imag(a)*imag(b);\n}\n\ndouble cross(const Point &a,const Point &b){\n\treturn real(a)*imag(b)-imag(a)*real(b);\n}\n\nenum {CCW=1,CW=-1,ON=0};\nint ccw(const Point &a,Point b,Point c){\n\tb-=a,c-=a;\n\tdouble rotdir=cross(b,c);\n\tif(rotdir>EPS) return CCW;\n\tif(rotdir<-EPS) return CW;\n\treturn ON;\n}\n\ninline void calc_abc(const Line &l,double &a,double &b,double &c){\t// l : ax+by+c=0\n\ta=imag(l[0])-imag(l[1]);\n\tb=real(l[1])-real(l[0]);\n\tc=cross(l[0],l[1]);\n}\n\nbool intersect(const Line &l,const Line &m,Point &p){\n\tif(abs(cross(l[1]-l[0],m[1]-m[0]))>EPS\n\t|| abs(cross(l[1]-l[0],m[0]-l[0]))<EPS){\n\t\tdouble a1,b1,c1,a2,b2,c2;\n\t\tcalc_abc(l,a1,b1,c1);\n\t\tcalc_abc(m,a2,b2,c2);\n\t\tdouble det=a1*b2-a2*b1;\n\t\tif(abs(det)<EPS) p=l[0]; // l==m\n\t\telse p=Point(b1*c2-b2*c1,a2*c1-a1*c2)/det;\n\t\treturn true;\n\t}\n\treturn false;\n}\n\nbool intersect(const Segment &s,const Segment &t,Point &p){\n\tif(max(real(s[0]),real(s[1]))<min(real(t[0]),real(t[1]))-EPS\n\t|| max(real(t[0]),real(t[1]))<min(real(s[0]),real(s[1]))-EPS\n\t|| max(imag(s[0]),imag(s[1]))<min(imag(t[0]),imag(t[1]))-EPS\n\t|| max(imag(t[0]),imag(t[1]))<min(imag(s[0]),imag(s[1]))-EPS) return false;\n\n\tif(ccw(s[0],s[1],t[0])*ccw(s[0],s[1],t[1])<=0\n\t&& ccw(t[0],t[1],s[0])*ccw(t[0],t[1],s[1])<=0){\n\t\tdouble a1,b1,c1,a2,b2,c2;\n\t\tcalc_abc(s,a1,b1,c1);\n\t\tcalc_abc(t,a2,b2,c2);\n\t\tdouble det=a1*b2-a2*b1;\n\t\tif(abs(det)<EPS){ // s is parallel to t\n\t\t\tPoint q[3]={s[0],s[1],t[0]};\n\t\t\trep(i,3){\n\t\t\t\tif(dot(q[i]-s[0],q[i]-s[1])<EPS && dot(q[i]-t[0],q[i]-t[1])<EPS){\n\t\t\t\t\tp=q[i];\n\t\t\t\t\tbreak;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\telse p=Point(b1*c2-b2*c1,a2*c1-a1*c2)/det;\n\t\treturn true;\n\t}\n\treturn false;\n}\n\nbool contain(const Polygon &g,const Point &p){\n\tint n=g.size();\n\tbool in=false;\n\trep(i,n){\n\t\tPoint v1=g[i]-p,v2=g[(i+1)%n]-p;\n\t\tif(imag(v1)>imag(v2)) swap(v1,v2);\n\t\t// include lower vertex, not include upper vertex\n\t\tif(imag(v1)<EPS && EPS<imag(v2) && cross(v1,v2)>EPS) in=!in; // alternate\n\t\tif(abs(cross(v1,v2))<EPS && dot(v1,v2)<EPS)\treturn true; // p is on g[i]-g[i+1]\n\t}\n\treturn in;\n}\n\nbool isViewableAllVertices(const Polygon &g,const Point &p){\n\tint n=g.size();\n\n\trep(i,n)if(abs(g[i]-p)>EPS){ // for each vertex g[i](!=p) in g\n\t\tPoint u=g[i]-p; u/=abs(u);\n\t\tif(!contain(g,p+1e-5*u) || !contain(g,g[i]-1e-5*u)) return false;\n\n\t\tSegment s(p,g[i]);\n\t\trep(j,n){ // for each side t in g\n\t\t\tSegment t(g[j],g[(j+1)%n]);\n\t\t\tPoint q;\n\t\t\t// p-q-g[i] is on the same line (this order)\n\t\t\tif(intersect(s,t,q)){\n\t\t\t\t// if q != p, g[i]\n\t\t\t\tif(abs(p-q)>EPS && abs(g[i]-q)>EPS){\n\t\t\t\t\tif(!contain(g,q-1e-5*u) || !contain(g,q+1e-5*u)) return false;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n\n\treturn true;\n}\n\nint main(){\n\tfor(int n;scanf(\"%d\",&n),n;){\n\t\tPolygon g;\n\t\trep(i,n){\n\t\t\tint x,y; scanf(\"%d%d\",&x,&y);\n\t\t\tg.pb(Point(x,y));\n\t\t}\n\n\t\tbool b=false;\n\t\trep(i,n) rep(j,i) {\n\t\t\tPoint p;\n\t\t\tLine l(g[i],g[(i+1)%n]),m(g[j],g[(j+1)%n]);\n\t\t\tif(intersect(l,m,p) && contain(g,p)){\n\t\t\t\tif(isViewableAllVertices(g,p)){ b=true; goto FOUND; }\n\t\t\t}\n\t\t}\n\n\t\tFOUND:\n\t\tputs(b?\"1\":\"0\");\n\t}\n\n\treturn 0;\n}", "accuracy": 1, "time_ms": 60, "memory_kb": 796, "score_of_the_acc": -0.0131, "final_rank": 1 }, { "submission_id": "aoj_1267_180502", "code_snippet": "#include<cmath>\n#include<vector>\n#include<complex>\n#include<algorithm>\n\n#define\tpb\t\t\tpush_back\n#define\trep(i,n)\tfor(int i=0;i<n;i++)\n\nusing namespace std;\n\ntypedef\tcomplex<double>\tPoint;\ntypedef\tvector<Point>\tPolygon;\n\nconst double EPS=1e-7;\n\nnamespace std{\n\tbool operator <(const Point &a,const Point &b){\n\t\treturn abs(real(a)-real(b))<EPS?(imag(a)+EPS<imag(b)):(real(a)+EPS<real(b));\n\t}\n\tbool operator >(const Point &a,const Point &b){\n\t\treturn b<a;\n\t}\n}\n\nclass Line:public vector<Point>{\npublic:\n\tLine(){}\n\tLine(const Point &a,const Point &b){\n\t\tpb(a),pb(b);\n\t}\n};\n\nclass Segment:public Line{\npublic:\n\tSegment(){}\n\tSegment(const Point &a,const Point &b):Line(a,b){}\n};\n\ndouble dot(const Point &a,const Point &b){\n\treturn real(a)*real(b)+imag(a)*imag(b);\n}\n\ndouble cross(const Point &a,const Point &b){\n\treturn real(a)*imag(b)-imag(a)*real(b);\n}\n\nenum {CCW=1,CW=-1,ON=0};\nint ccw(const Point &a,Point b,Point c){\n\tb-=a,c-=a;\n\tdouble rotdir=cross(b,c);\n\tif(rotdir>EPS)\t\treturn CCW;\n\tif(rotdir<-EPS)\t\treturn CW;\n\treturn ON;\n}\n\ninline void calc_abc(const Line &l,double &a,double &b,double &c){\n\ta=imag(l[0])-imag(l[1]);\n\tb=real(l[1])-real(l[0]);\n\tc=cross(l[0],l[1]);\n}\n\nbool intersect(const Line &l,const Line &m,Point *p=NULL){\n\tif(abs(cross(l[1]-l[0],m[1]-m[0]))>EPS\n\t|| abs(cross(l[1]-l[0],m[0]-l[0]))<EPS){\n\t\tif(p){\n\t\t\tdouble a1,b1,c1,a2,b2,c2;\n\t\t\tcalc_abc(l,a1,b1,c1);\n\t\t\tcalc_abc(m,a2,b2,c2);\n\t\t\tdouble det=a1*b2-a2*b1;\n\t\t\tif(abs(det)<EPS)\t*p=l[0];\n\t\t\telse\t\t\t\t*p=Point(b1*c2-b2*c1,a2*c1-a1*c2)/det;\n\t\t}\n\t\treturn true;\n\t}\n\treturn false;\n}\n\nbool intersect(const Segment &s,const Segment &t,Point *p=NULL){\n\tif(max(real(s[0]),real(s[1]))<min(real(t[0]),real(t[1]))\n\t|| max(real(t[0]),real(t[1]))<min(real(s[0]),real(s[1]))\n\t|| max(imag(s[0]),imag(s[1]))<min(imag(t[0]),imag(t[1]))\n\t|| max(imag(t[0]),imag(t[1]))<min(imag(s[0]),imag(s[1])))\treturn false;\n\n\tif(ccw(s[0],s[1],t[0])*ccw(s[0],s[1],t[1])<=0\n\t&& ccw(t[0],t[1],s[0])*ccw(t[0],t[1],s[1])<=0){\n\t\tif(p){\n\t\t\tdouble a1,b1,c1,a2,b2,c2;\n\t\t\tcalc_abc(s,a1,b1,c1);\n\t\t\tcalc_abc(t,a2,b2,c2);\n\t\t\tdouble det=a1*b2-a2*b1;\n\t\t\tif(abs(det)<EPS){\n\t\t\t\tPoint q[3]={s[0],s[1],t[0]};\n\t\t\t\trep(i,3){\n\t\t\t\t\tif(dot(q[i]-s[0],q[i]-s[1])<EPS && dot(q[i]-t[0],q[i]-t[1])<EPS){\n\t\t\t\t\t\t*p=q[i];\n\t\t\t\t\t\tbreak;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t\telse\t*p=Point(b1*c2-b2*c1,a2*c1-a1*c2)/det;\n\t\t}\n\t\treturn true;\n\t}\n\treturn false;\n}\n\nbool contain(const Polygon &g,const Point &p){\n\tint n=g.size();\n\tbool in=false;\n\trep(i,n){\n\t\tPoint v1=g[i]-p,v2=g[(i+1)%n]-p;\n\t\tif(imag(v1)>imag(v2))\tswap(v1,v2);\n\t\tif(imag(v1)<EPS && EPS<imag(v2) && cross(v1,v2)>EPS)\tin=!in;\n\t\tif(abs(cross(v1,v2))<EPS && dot(v1,v2)<EPS)\treturn true;\n\t}\n\treturn in;\n}\n\n#include<cstdio>\n\nbool isViewableAllVertices(const Polygon &g,const Point &p){\n\tint n=g.size();\n\n\trep(i,n)if(abs(g[i]-p)>EPS){\n\t\tPoint u=g[i]-p; u/=abs(u);\n\t\tif(!contain(g,p+1e-5*u) || !contain(g,g[i]-1e-5*u))\treturn false;\n\n\t\tSegment s(p,g[i]);\n\t\trep(j,n){\n\t\t\tSegment t(g[j],g[(j+1)%n]);\n\t\t\tPoint q;\n\t\t\tif(intersect(s,t,&q)){\n\t\t\t\tif(abs(q-p)>EPS && abs(g[i]-q)>EPS){\n\t\t\t\t\tif(!contain(g,q-1e-5*u) || !contain(g,q+1e-5*u))\treturn false;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n\n\treturn true;\n}\n\nint main(){\n\tfor(int n;scanf(\"%d\",&n),n;){\n\t\tPolygon g;\n\t\trep(i,n){\n\t\t\tint x,y;\tscanf(\"%d%d\",&x,&y);\n\t\t\tg.pb(Point(x,y));\n\t\t}\n\n\t\tbool b=false;\n\t\trep(i,n)rep(j,i){\n\t\t\tPoint p;\n\t\t\tLine l(g[i],g[(i+1)%n]),m(g[j],g[(j+1)%n]);\n\t\t\tif(intersect(l,m,&p) && contain(g,p)){\n\t\t\t\tif(isViewableAllVertices(g,p)){ b=true; goto FOUND; }\n\t\t\t}\n\t\t}\n\n\t\tFOUND:\n\t\tputs(b?\"1\":\"0\");\n\t}\n\n\treturn 0;\n}", "accuracy": 1, "time_ms": 60, "memory_kb": 796, "score_of_the_acc": -0.0131, "final_rank": 1 }, { "submission_id": "aoj_1267_180499", "code_snippet": "#include<cmath>\n#include<vector>\n#include<complex>\n#include<algorithm>\n\n#define\tpb\t\t\tpush_back\n#define\trep(i,n)\tfor(int i=0;i<n;i++)\n\nusing namespace std;\n\ntypedef\tcomplex<double>\tPoint;\ntypedef\tvector<Point>\tPolygon;\n\nconst double EPS=1e-7;\n\nnamespace std{\n\tbool operator <(const Point &a,const Point &b){\n\t\treturn abs(real(a)-real(b))<EPS?(imag(a)+EPS<imag(b)):(real(a)+EPS<real(b));\n\t}\n\tbool operator >(const Point &a,const Point &b){\n\t\treturn b<a;\n\t}\n}\n\nenum {CCW=1,CW=-1,ON=0};\n\nclass Line:public vector<Point>{\npublic:\n\tLine(){}\n\tLine(const Point &a,const Point &b){\n\t\tpb(a),pb(b);\n\t}\n};\n\nclass Segment:public Line{\npublic:\n\tSegment(){}\n\tSegment(const Point &a,const Point &b):Line(a,b){}\n};\n\ndouble dot(const Point &a,const Point &b){\n\treturn real(a)*real(b)+imag(a)*imag(b);\n}\n\ndouble cross(const Point &a,const Point &b){\n\treturn real(a)*imag(b)-imag(a)*real(b);\n}\n\nint ccw(const Point &a,Point b,Point c){\n\tb-=a,c-=a;\n\tdouble rotdir=cross(b,c);\n\tif(rotdir>EPS)\t\treturn CCW;\n\tif(rotdir<-EPS)\t\treturn CW;\n\treturn ON;\n}\n\ninline void calc_abc(const Line &l,double &a,double &b,double &c){\t// l : ax+by+c=0\n\ta=imag(l[0])-imag(l[1]);\n\tb=real(l[1])-real(l[0]);\n\tc=cross(l[0],l[1]);\n}\n\nbool intersect(const Line &l,const Line &m,Point *p=NULL){\n\t// this routine also returns true in case \"M is on L\", etc,.\n\tif(abs(cross(l[1]-l[0],m[1]-m[0]))>EPS\n\t|| abs(cross(l[1]-l[0],m[0]-l[0]))<EPS){\n\t\tif(p){\n\t\t\tdouble a1,b1,c1,a2,b2,c2;\n\t\t\tcalc_abc(l,a1,b1,c1);\n\t\t\tcalc_abc(m,a2,b2,c2);\n\t\t\tdouble det=a1*b2-a2*b1;\n\t\t\tif(abs(det)<EPS)\t*p=l[0];\t// l==m\n\t\t\telse\t\t\t\t*p=Point(b1*c2-b2*c1,a2*c1-a1*c2)/det;\n\t\t}\n\t\treturn true;\n\t}\n\treturn false;\n}\n\nbool intersect(const Segment &s,const Segment &t,Point *p=NULL){\n\tif(max(real(s[0]),real(s[1]))<min(real(t[0]),real(t[1]))\n\t|| max(real(t[0]),real(t[1]))<min(real(s[0]),real(s[1]))\n\t|| max(imag(s[0]),imag(s[1]))<min(imag(t[0]),imag(t[1]))\n\t|| max(imag(t[0]),imag(t[1]))<min(imag(s[0]),imag(s[1])))\treturn false;\n\n\tif(ccw(s[0],s[1],t[0])*ccw(s[0],s[1],t[1])<=0\n\t&& ccw(t[0],t[1],s[0])*ccw(t[0],t[1],s[1])<=0){\n\t\tif(p){\n\t\t\tdouble a1,b1,c1,a2,b2,c2;\n\t\t\tcalc_abc(s,a1,b1,c1);\n\t\t\tcalc_abc(t,a2,b2,c2);\n\t\t\tdouble det=a1*b2-a2*b1;\n\t\t\tif(abs(det)<EPS){\t// s is parallel to t\n\t\t\t\tPoint q[3]={s[0],s[1],t[0]};\n\t\t\t\trep(i,3){\n\t\t\t\t\tif(dot(q[i]-s[0],q[i]-s[1])<EPS && dot(q[i]-t[0],q[i]-t[1])<EPS){\n\t\t\t\t\t\t*p=q[i];\n\t\t\t\t\t\tbreak;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t\telse\t*p=Point(b1*c2-b2*c1,a2*c1-a1*c2)/det;\n\t\t}\n\t\treturn true;\n\t}\n\treturn false;\n}\n\nbool contain(const Polygon &g,const Point &p){\n\tint n=g.size();\n\tbool in=false;\n\trep(i,n){\n\t\tPoint v1=g[i]-p,v2=g[(i+1)%n]-p;\n\t\tif(imag(v1)>imag(v2))\tswap(v1,v2);\n\t\t// include lower vertex, not include upper vertex\n\t\tif(imag(v1)<EPS && EPS<imag(v2) && cross(v1,v2)>EPS)\tin=!in;\t// alternate\n\t\tif(abs(cross(v1,v2))<EPS && dot(v1,v2)<EPS)\treturn true;\t// p is on g[i]-g[i+1]\n\t}\n\treturn in;\n}\n\n#include<cstdio>\n\nbool isViewableAllVertices(const Polygon &g,const Point &p){\n\tint n=g.size();\n\n\trep(i,n)if(abs(g[i]-p)>EPS){\t// for each vertex g[i](!=p) in g\n\t\tPoint u=g[i]-p; u/=abs(u);\n\t\tif(!contain(g,p+1e-5*u) || !contain(g,g[i]-1e-5*u))\treturn false;\n\n\t\tSegment s(p,g[i]);\n\t\trep(j,n){\t// for each side t in g\n\t\t\tSegment t(g[j],g[(j+1)%n]);\n\t\t\tPoint q;\n\t\t\t// p-q-g[i] is on the same line (this order)\n\t\t\tif(intersect(s,t,&q)){\n\t\t\t\t// if q != p, g[i]\n\t\t\t\tif(abs(q-p)>EPS && abs(g[i]-q)>EPS){\n\t\t\t\t\tif(!contain(g,q-1e-5*u) || !contain(g,q+1e-5*u))\treturn false;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n\n\treturn true;\n}\n\nint main(){\n\tfor(int n;scanf(\"%d\",&n),n;){\n\t\tPolygon g;\n\t\trep(i,n){\n\t\t\tint x,y;\tscanf(\"%d%d\",&x,&y);\n\t\t\tg.pb(Point(x,y));\n\t\t}\n\n\t\tbool b=false;\n\t\trep(i,n)rep(j,i){\n\t\t\tPoint p;\n\t\t\tLine l(g[i],g[(i+1)%n]),m(g[j],g[(j+1)%n]);\n\t\t\tif(intersect(l,m,&p) && contain(g,p)){\n\t\t\t\tif(isViewableAllVertices(g,p)){ b=true; goto FOUND; }\n\t\t\t}\n\t\t}\n\n\t\tFOUND:\n\t\tputs(b?\"1\":\"0\");\n\t}\n\n\treturn 0;\n}", "accuracy": 1, "time_ms": 60, "memory_kb": 800, "score_of_the_acc": -0.0132, "final_rank": 3 }, { "submission_id": "aoj_1267_180410", "code_snippet": "#include<cmath>\n#include<vector>\n#include<complex>\n#include<algorithm>\n\n#define\tpb\t\t\tpush_back\n#define\trep(i,n)\tfor(int i=0;i<n;i++)\n\nusing namespace std;\n\ntypedef\tcomplex<double>\tPoint;\ntypedef\tvector<Point>\tPolygon;\n\nnamespace std{\n\tbool operator <(const Point &a,const Point &b){\n\t\treturn (real(a)==real(b))?(imag(a)<imag(b)):(real(a)<real(b));\n\t}\n\tbool operator >(const Point &a,const Point &b){\n\t\treturn b<a;\n\t}\n}\n\nenum {CCW=1,CW=-1,ON=0};\nconst double EPS=1e-7;\n\nclass Line:public vector<Point>{\npublic:\n\tLine(){}\n\tLine(const Point &a,const Point &b){\n\t\tpb(a),pb(b);\n\t}\n};\n\nclass Segment:public Line{\npublic:\n\tSegment(){}\n\tSegment(const Point &a,const Point &b):Line(a,b){}\n};\n\ndouble dot(const Point &a,const Point &b){\n\treturn real(a)*real(b)+imag(a)*imag(b);\n}\n\ndouble cross(const Point &a,const Point &b){\n\treturn real(a)*imag(b)-imag(a)*real(b);\n}\n\nint ccw(const Point &a,Point b,Point c){\n\tb-=a,c-=a;\n\tdouble rotdir=cross(b,c);\n\tif(rotdir>EPS)\t\treturn CCW;\n\tif(rotdir<-EPS)\t\treturn CW;\n\treturn ON;\n}\n\ninline void calc_abc(const Line &l,double &a,double &b,double &c){\n\ta=imag(l[0])-imag(l[1]);\n\tb=real(l[1])-real(l[0]);\n\tc=cross(l[0],l[1]);\n}\n\nbool intersect(const Line &l,const Line &m,Point *p=NULL){\n\tif(abs(cross(l[1]-l[0],m[1]-m[0]))>EPS\n\t|| abs(cross(l[1]-l[0],m[0]-l[0]))<EPS){\n\t\tif(p){\n\t\t\tdouble a1,b1,c1,a2,b2,c2;\n\t\t\tcalc_abc(l,a1,b1,c1);\n\t\t\tcalc_abc(m,a2,b2,c2);\n\t\t\tdouble det=a1*b2-a2*b1;\n\t\t\tif(abs(det)<EPS)\t*p=l[0];\n\t\t\telse\t\t\t\t*p=Point(b1*c2-b2*c1,a2*c1-a1*c2)/det;\n\t\t}\n\t\treturn true;\n\t}\n\treturn false;\n}\n\nbool intersect(const Segment &s,const Segment &t,Point *p=NULL){\n\tif(max(real(s[0]),real(s[1]))<min(real(t[0]),real(t[1]))\n\t|| max(real(t[0]),real(t[1]))<min(real(s[0]),real(s[1]))\n\t|| max(imag(s[0]),imag(s[1]))<min(imag(t[0]),imag(t[1]))\n\t|| max(imag(t[0]),imag(t[1]))<min(imag(s[0]),imag(s[1])))\treturn false;\n\n\tif(ccw(s[0],s[1],t[0])*ccw(s[0],s[1],t[1])<=0\n\t&& ccw(t[0],t[1],s[0])*ccw(t[0],t[1],s[1])<=0){\n\t\tif(p){\n\t\t\tdouble a1,b1,c1,a2,b2,c2;\n\t\t\tcalc_abc(s,a1,b1,c1);\n\t\t\tcalc_abc(t,a2,b2,c2);\n\t\t\tdouble det=a1*b2-a2*b1;\n\t\t\tif(abs(det)<EPS){\t// s is parallel to t\n\t\t\t\tPoint q[3]={s[0],s[1],t[0]};\n\t\t\t\trep(i,3){\n\t\t\t\t\tif(dot(q[i]-s[0],q[i]-s[1])<EPS && dot(q[i]-t[0],q[i]-t[1])<EPS){\n\t\t\t\t\t\t*p=q[i];\n\t\t\t\t\t\tbreak;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t\telse\t*p=Point(b1*c2-b2*c1,a2*c1-a1*c2)/det;\n\t\t}\n\t\treturn true;\n\t}\n\treturn false;\n}\n\nbool contain(const Polygon &g,const Point &p){\n\tint n=g.size();\n\tbool in=false;\n\trep(i,n){\n\t\tPoint v1=g[i]-p,v2=g[(i+1)%n]-p;\n\t\tif(imag(v1)>imag(v2))\tswap(v1,v2);\n\t\tif(imag(v1)<EPS && EPS<imag(v2) && cross(v1,v2)<-EPS)\tin=!in;\n\t\tif(abs(cross(v1,v2))<EPS && dot(v1,v2)<EPS)\treturn true;\n\t}\n\treturn in;\n}\n\n#include<cstdio>\n\nint main(){\n\tfor(int n;scanf(\"%d\",&n),n;){\n\t\tPolygon g;\n\t\trep(i,n){\n\t\t\tint x,y;\tscanf(\"%d%d\",&x,&y);\n\t\t\tg.pb(Point(x,y));\n\t\t}\n\n\t\tvector<Point> candi;\n\t\trep(i,n)rep(j,i){\n\t\t\tLine l(g[i],g[(i+1)%n]),m(g[j],g[(j+1)%n]);\n\t\t\tPoint p;\n\t\t\tif(intersect(l,m,&p) && contain(g,p))\tcandi.pb(p);\n\t\t}\n\n\t\trep(k,candi.size()){\n\t\t\tbool b=true;\n\t\t\tPoint p=candi[k];\n\t\t\trep(i,n)if(abs(g[i]-p)>EPS){\n\t\t\t\tPoint u=g[i]-p; u/=abs(u);\n\t\t\t\tif(!contain(g,p+1e-5*u) || !contain(g,g[i]-1e-5*u)){ b=false; break; }\n\n\t\t\t\tSegment s(p,g[i]);\n\t\t\t\trep(j,n){\n\t\t\t\t\tSegment t(g[j],g[(j+1)%n]);\n\t\t\t\t\tPoint q;\n\t\t\t\t\tif(intersect(s,t,&q)){\n\t\t\t\t\t\tif(abs(q-p)>EPS && abs(g[i]-q)>EPS){\n\t\t\t\t\t\t\tif(!contain(g,q-1e-5*u) || !contain(g,q+1e-5*u)){\n\t\t\t\t\t\t\t\tb=false;\n\t\t\t\t\t\t\t\tbreak;\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\tif(!b)\tbreak;\n\t\t\t}\n\t\t\tif(b)\tgoto FOUND;\n\t\t}\n\t\tgoto NOT_FOUND;\n\n\t\tFOUND:\n\t\tputs(\"1\");\n\t\tcontinue;\n\n\t\tNOT_FOUND:\n\t\tputs(\"0\");\n\t}\n\n\treturn 0;\n}", "accuracy": 1, "time_ms": 80, "memory_kb": 828, "score_of_the_acc": -0.019, "final_rank": 7 }, { "submission_id": "aoj_1267_180409", "code_snippet": "#include<cmath>\n#include<vector>\n#include<complex>\n#include<algorithm>\n\n#define\tpb\t\t\tpush_back\n#define\trep(i,n)\tfor(int i=0;i<n;i++)\n\nusing namespace std;\n\ntypedef\tcomplex<double>\tPoint;\ntypedef\tvector<Point>\tPolygon;\n\nnamespace std{\n\tbool operator <(const Point &a,const Point &b){\n\t\treturn (real(a)==real(b))?(imag(a)<imag(b)):(real(a)<real(b));\n\t}\n\tbool operator >(const Point &a,const Point &b){\n\t\treturn b<a;\n\t}\n}\n\nenum {CCW=1,CW=-1,ON=0};\nconst double EPS=1e-7;\n\nclass Line:public vector<Point>{\npublic:\n\tLine(){}\n\tLine(const Point &a,const Point &b){\n\t\tpb(a),pb(b);\n\t}\n};\n\nclass Segment:public Line{\npublic:\n\tSegment(){}\n\tSegment(const Point &a,const Point &b):Line(a,b){}\n};\n\ndouble dot(const Point &a,const Point &b){\n\treturn real(a)*real(b)+imag(a)*imag(b);\n}\n\ndouble cross(const Point &a,const Point &b){\n\treturn real(a)*imag(b)-imag(a)*real(b);\n}\n\nint ccw(const Point &a,Point b,Point c){\n\tb-=a,c-=a;\n\tdouble rotdir=cross(b,c);\n\tif(rotdir>EPS)\t\treturn CCW;\n\tif(rotdir<-EPS)\t\treturn CW;\n\treturn ON;\n}\n\ninline void calc_abc(const Line &l,double &a,double &b,double &c){\n\ta=imag(l[0])-imag(l[1]);\n\tb=real(l[1])-real(l[0]);\n\tc=cross(l[0],l[1]);\n}\n\nbool intersect(const Line &l,const Line &m,Point *p=NULL){\n\tif(abs(cross(l[1]-l[0],m[1]-m[0]))>EPS\n\t|| abs(cross(l[1]-l[0],m[0]-l[0]))<EPS){\n\t\tif(p){\n\t\t\tdouble a1,b1,c1,a2,b2,c2;\n\t\t\tcalc_abc(l,a1,b1,c1);\n\t\t\tcalc_abc(m,a2,b2,c2);\n\t\t\tdouble det=a1*b2-a2*b1;\n\t\t\tif(abs(det)<EPS)\t*p=l[0];\n\t\t\telse\t\t\t\t*p=Point(b1*c2-b2*c1,a2*c1-a1*c2)/det;\n\t\t}\n\t\treturn true;\n\t}\n\treturn false;\n}\n\nbool intersect(const Segment &s,const Segment &t,Point *p=NULL){\n\tif(max(real(s[0]),real(s[1]))<min(real(t[0]),real(t[1]))\n\t|| max(real(t[0]),real(t[1]))<min(real(s[0]),real(s[1]))\n\t|| max(imag(s[0]),imag(s[1]))<min(imag(t[0]),imag(t[1]))\n\t|| max(imag(t[0]),imag(t[1]))<min(imag(s[0]),imag(s[1])))\treturn false;\n\n\tif(ccw(s[0],s[1],t[0])*ccw(s[0],s[1],t[1])<=0\n\t&& ccw(t[0],t[1],s[0])*ccw(t[0],t[1],s[1])<=0){\n\t\tif(p){\n\t\t\tdouble a1,b1,c1,a2,b2,c2;\n\t\t\tcalc_abc(s,a1,b1,c1);\n\t\t\tcalc_abc(t,a2,b2,c2);\n\t\t\tdouble det=a1*b2-a2*b1;\n\t\t\tif(abs(det)<EPS){\t// s is parallel to t\n\t\t\t\tPoint q[3]={s[0],s[1],t[0]};\n\t\t\t\trep(i,3){\n\t\t\t\t\tif(dot(q[i]-s[0],q[i]-s[1])<EPS && dot(q[i]-t[0],q[i]-t[1])<EPS){\n\t\t\t\t\t\t*p=q[i];\n\t\t\t\t\t\tbreak;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t\telse\t*p=Point(b1*c2-b2*c1,a2*c1-a1*c2)/det;\n\t\t}\n\t\treturn true;\n\t}\n\treturn false;\n}\n\nbool contain(const Polygon &g,const Point &p){\n\tint n=g.size();\n\tbool in=false;\n\trep(i,n){\n\t\tPoint v1=g[i]-p,v2=g[(i+1)%n]-p;\n\t\tdouble eps=EPS;\n\t\tif(imag(v1)>imag(v2))\tswap(v1,v2),eps=-eps;\n\t\tif(imag(v1)<eps && eps<imag(v2) && cross(v1,v2)<-EPS)\tin=!in;\n\t\tif(abs(cross(v1,v2))<EPS && dot(v1,v2)<EPS)\treturn true;\n\t}\n\treturn in;\n}\n\n#include<cstdio>\n\nint main(){\n\tfor(int n;scanf(\"%d\",&n),n;){\n\t\tPolygon g;\n\t\trep(i,n){\n\t\t\tint x,y;\tscanf(\"%d%d\",&x,&y);\n\t\t\tg.pb(Point(x,y));\n\t\t}\n\n\t\tvector<Point> candi;\n\t\trep(i,n)rep(j,i){\n\t\t\tLine l(g[i],g[(i+1)%n]),m(g[j],g[(j+1)%n]);\n\t\t\tPoint p;\n\t\t\tif(intersect(l,m,&p) && contain(g,p))\tcandi.pb(p);\n\t\t}\n\n\t\trep(k,candi.size()){\n\t\t\tbool b=true;\n\t\t\tPoint p=candi[k];\n\t\t\trep(i,n)if(abs(g[i]-p)>EPS){\n\t\t\t\tPoint u=g[i]-p; u/=abs(u);\n\t\t\t\tif(!contain(g,p+1e-5*u) || !contain(g,g[i]-1e-5*u)){ b=false; break; }\n\n\t\t\t\tSegment s(p,g[i]);\n\t\t\t\trep(j,n){\n\t\t\t\t\tSegment t(g[j],g[(j+1)%n]);\n\t\t\t\t\tPoint q;\n\t\t\t\t\tif(intersect(s,t,&q)){\n\t\t\t\t\t\tif(abs(q-p)>EPS && abs(g[i]-q)>EPS){\n\t\t\t\t\t\t\tif(!contain(g,q-1e-5*u) || !contain(g,q+1e-5*u)){\n\t\t\t\t\t\t\t\tb=false;\n\t\t\t\t\t\t\t\tbreak;\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\tif(!b)\tbreak;\n\t\t\t}\n\t\t\tif(b)\tgoto FOUND;\n\t\t}\n\t\tgoto NOT_FOUND;\n\n\t\tFOUND:\n\t\tputs(\"1\");\n\t\tcontinue;\n\n\t\tNOT_FOUND:\n\t\tputs(\"0\");\n\t}\n\n\treturn 0;\n}", "accuracy": 1, "time_ms": 70, "memory_kb": 828, "score_of_the_acc": -0.0163, "final_rank": 5 }, { "submission_id": "aoj_1267_180408", "code_snippet": "#include<cmath>\n#include<vector>\n#include<complex>\n#include<algorithm>\n\n#define\tpb\t\t\tpush_back\n#define\trep(i,n)\tfor(int i=0;i<n;i++)\n\nusing namespace std;\n\ntypedef\tcomplex<double>\tPoint;\ntypedef\tvector<Point>\tPolygon;\n\nnamespace std{\n\tbool operator <(const Point &a,const Point &b){\n\t\treturn (real(a)==real(b))?(imag(a)<imag(b)):(real(a)<real(b));\n\t}\n\tbool operator >(const Point &a,const Point &b){\n\t\treturn b<a;\n\t}\n}\n\nenum {CCW=1,CW=-1,ON=0};\nconst double EPS=1e-7;\n\nclass Line:public vector<Point>{\npublic:\n\tLine(){}\n\tLine(const Point &a,const Point &b){\n\t\tpb(a),pb(b);\n\t}\n};\n\nclass Segment:public Line{\npublic:\n\tSegment(){}\n\tSegment(const Point &a,const Point &b):Line(a,b){}\n};\n\ndouble dot(const Point &a,const Point &b){\n\treturn real(a)*real(b)+imag(a)*imag(b);\n}\n\ndouble cross(const Point &a,const Point &b){\n\treturn real(a)*imag(b)-imag(a)*real(b);\n}\n\nint ccw(const Point &a,Point b,Point c){\n\tb-=a,c-=a;\n\tdouble rotdir=cross(b,c);\n\tif(rotdir>EPS)\t\treturn CCW;\n\tif(rotdir<-EPS)\t\treturn CW;\n\treturn ON;\n}\n\ninline void calc_abc(const Line &l,double &a,double &b,double &c){\t// l : ax+by+c=0\n\ta=imag(l[0])-imag(l[1]);\n\tb=real(l[1])-real(l[0]);\n\tc=cross(l[0],l[1]);\n}\n\nbool intersect(const Line &l,const Line &m,Point *p=NULL){\n\t// this routine also returns true in case \"M is on L\", etc,.\n\tif(abs(cross(l[1]-l[0],m[1]-m[0]))>EPS\n\t|| abs(cross(l[1]-l[0],m[0]-l[0]))<EPS){\n\t\tif(p){\n\t\t\tdouble a1,b1,c1,a2,b2,c2;\n\t\t\tcalc_abc(l,a1,b1,c1);\n\t\t\tcalc_abc(m,a2,b2,c2);\n\t\t\tdouble det=a1*b2-a2*b1;\n\t\t\tif(abs(det)<EPS)\t*p=l[0];\t// l==m\n\t\t\telse\t\t\t\t*p=Point(b1*c2-b2*c1,a2*c1-a1*c2)/det;\n\t\t}\n\t\treturn true;\n\t}\n\treturn false;\n}\n\nbool intersect(const Segment &s,const Segment &t,Point *p=NULL){\n\tif(max(real(s[0]),real(s[1]))<min(real(t[0]),real(t[1]))\n\t|| max(real(t[0]),real(t[1]))<min(real(s[0]),real(s[1]))\n\t|| max(imag(s[0]),imag(s[1]))<min(imag(t[0]),imag(t[1]))\n\t|| max(imag(t[0]),imag(t[1]))<min(imag(s[0]),imag(s[1])))\treturn false;\n\n\tif(ccw(s[0],s[1],t[0])*ccw(s[0],s[1],t[1])<=0\n\t&& ccw(t[0],t[1],s[0])*ccw(t[0],t[1],s[1])<=0){\n\t\tif(p){\n\t\t\tdouble a1,b1,c1,a2,b2,c2;\n\t\t\tcalc_abc(s,a1,b1,c1);\n\t\t\tcalc_abc(t,a2,b2,c2);\n\t\t\tdouble det=a1*b2-a2*b1;\n\t\t\tif(abs(det)<EPS){\t// s is parallel to t\n\t\t\t\tPoint q[3]={s[0],s[1],t[0]};\n\t\t\t\trep(i,3){\n\t\t\t\t\tif(dot(q[i]-s[0],q[i]-s[1])<EPS && dot(q[i]-t[0],q[i]-t[1])<EPS){\n\t\t\t\t\t\t*p=q[i];\n\t\t\t\t\t\tbreak;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t\telse\t*p=Point(b1*c2-b2*c1,a2*c1-a1*c2)/det;\n\t\t}\n\t\treturn true;\n\t}\n\treturn false;\n}\n\nbool contain(const Polygon &g,const Point &p){\n\tint n=g.size();\n\tbool in=false;\n\trep(i,n){\n\t\tPoint v1=g[i]-p,v2=g[(i+1)%n]-p;\n\t\tdouble eps=EPS;\n\t\tif(imag(v1)>imag(v2))\tswap(v1,v2),eps=-eps;\n\t\t// include g[i], not include g[i+1]\n\t\tif(imag(v1)<eps && eps<imag(v2) && cross(v1,v2)<-EPS)\tin=!in;\t// alternate\n\t\tif(abs(cross(v1,v2))<EPS && dot(v1,v2)<EPS)\treturn true;\t// p is on g[i]-g[i+1]\n\t}\n\treturn in;\n}\n\n#include<cstdio>\n\nint main(){\n\tfor(int n;scanf(\"%d\",&n),n;){\n\t\tPolygon g;\n\t\trep(i,n){\n\t\t\tint x,y;\tscanf(\"%d%d\",&x,&y);\n\t\t\tg.pb(Point(x,y));\n\t\t}\n\n\t\tvector<Point> candi;\n\t\trep(i,n)rep(j,i){\n\t\t\tLine l(g[i],g[(i+1)%n]),m(g[j],g[(j+1)%n]);\n\t\t\tPoint p;\n\t\t\tif(intersect(l,m,&p) && contain(g,p))\tcandi.pb(p);\n\t\t}\n\n\t\trep(k,candi.size()){\n\t\t\tbool b=true;\n\t\t\tPoint p=candi[k];\n\t\t\trep(i,n)if(abs(g[i]-p)>EPS){\t// for each vertex g[i](!=p) in g\n\t\t\t\tPoint u=g[i]-p; u/=abs(u);\n\t\t\t\tif(!contain(g,p+1e-5*u) || !contain(g,g[i]-1e-5*u)){ b=false; break; }\n\n\t\t\t\tSegment s(p,g[i]);\n\t\t\t\trep(j,n){\t// for each side t in g\n\t\t\t\t\tSegment t(g[j],g[(j+1)%n]);\n\t\t\t\t\tPoint q;\n\t\t\t\t\t// p-q-g[i] is on the same line (this order)\n\t\t\t\t\tif(intersect(s,t,&q)){\n\t\t\t\t\t\t// if q != p, g[i]\n\t\t\t\t\t\tif(abs(q-p)>EPS && abs(g[i]-q)>EPS){\n\t\t\t\t\t\t\tif(!contain(g,q-1e-5*u) || !contain(g,q+1e-5*u)){\n\t\t\t\t\t\t\t\tb=false;\n\t\t\t\t\t\t\t\tbreak;\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\tif(!b)\tbreak;\n\t\t\t}\n\t\t\tif(b)\tgoto FOUND;\n\t\t}\n\t\tgoto NOT_FOUND;\n\n\t\tFOUND:\n\t\tputs(\"1\");\n\t\tcontinue;\n\n\t\tNOT_FOUND:\n\t\tputs(\"0\");\n\t}\n\n\treturn 0;\n}", "accuracy": 1, "time_ms": 70, "memory_kb": 828, "score_of_the_acc": -0.0163, "final_rank": 5 }, { "submission_id": "aoj_1267_32069", "code_snippet": "#include<iostream>\n#include<cstdio>\n#include<vector>\n#include<cmath>\n#include<complex>\nusing namespace std;\n\n#define REP(i,n) for(int i = 0; i < (int)(n); i++)\n\ntypedef complex<double> P;\nconst double PI = acos(-1.0);\nconst double EPS = 1e-10;\n\ndouble dot(P a, P b) {return real( conj(a)*b);}\ndouble cross(P a, P b) {return imag( conj(a)*b);}\nint ccw(P a, P b, P c) {\n b -= a; c -= a;\n if (cross(b, c) > EPS) return +1; // counter clockwise\n if (cross(b, c) < -EPS) return -1; // clockwise\n if (dot(b, c) < -EPS) return +2; // c--a--b on line\n if (norm(b) < norm(c) - EPS) return -2; // a--b--c on line\n return 0; // a--c--b on line (or b == c)\n}\n\nbool IsIntersectLL(P a,P b,P c,P d){\n return abs(cross(b-a, d-c)) > EPS || // non-parallel\n abs(cross(b-a, c-a)) < EPS; // same line\n}\nbool IsIntersectSS(P a1, P a2, P b1, P b2) {\n return ccw(a1,a2,b1) * ccw(a1,a2,b2) <= 0 &&\n ccw(b1,b2,a1) * ccw(b1,b2,a2) <= 0;\n}\nP IntersectionLL(P a1, P a2, P b1, P b2) {\n P a = a2 - a1; P b = b2 - b1;\n double A = cross(b, b1-a1);\n double B = cross(b, a);\n if( abs(A) < EPS && abs(B) < EPS ) return a1; // same line\n return a1 + a * A / B;\n}\n\nbool SeeAll( const vector<P> &polygon, P p ) {\n int N = polygon.size() - 1;\n for(int i = 0; i < N; i++) {\n int count = 0;\n for(int j = 0; j < N; j++) {\n if( IsIntersectSS( p, polygon[i], polygon[j], polygon[j+1]) )\n count++;\n }\n if( count != 2 ) return false;\n }\n return true;\n}\n\nint main() {\n for(int N; scanf(\"%d\", &N), N; ) {\n vector<P> polygon;\n REP(n, N) {\n int x, y;\n scanf(\"%d%d\", &x, &y);\n polygon.push_back( P(x,y) );\n }\n\n polygon.push_back( polygon[0] );\n\n int ans = 0;\n for(int i = 0; i < N; i++){\n for(int j = i+1; j < N; j++){\n if( IsIntersectLL( polygon[i], polygon[i+1], polygon[j], polygon[j+1] ) ){\n P can = IntersectionLL(polygon[i], polygon[i+1], polygon[j], polygon[j+1]);\n\n for(int k = 0; k < 10; k++) {\n if( SeeAll( polygon, can + polar(0.00001, 2 * PI / k) ) ) {\n ans = 1;\n goto END;\n }\n\n }\n\n }\n }\n }\n\n END:\n printf(\"%d\\n\", ans);\n }\n return 0;\n}", "accuracy": 1, "time_ms": 3830, "memory_kb": 940, "score_of_the_acc": -1.0028, "final_rank": 14 } ]
aoj_1269_cpp
Problem D: Sum of Different Primes A positive integer may be expressed as a sum of different prime numbers (primes), in one way or another. Given two positive integers n and k , you should count the number of ways to express n as a sum of k different primes. Here, two ways are considered to be the same if they sum up the same set of the primes. For example, 8 can be expressed as 3 + 5 and 5+ 3 but they are not distinguished. When n and k are 24 and 3 respectively, the answer is two because there are two sets {2, 3, 19} and {2, 5, 17} whose sums are equal to 24. There are no other sets of three primes that sum up to 24. For n = 24 and k = 2, the answer is three, because there are three sets {5, 19}, {7,17} and {11, 13}. For n = 2 and k = 1, the answer is one, because there is only one set {2} whose sum is 2. For n = 1 and k = 1, the answer is zero. As 1 is not a prime, you shouldn't count {1}. For n = 4 and k = 2, the answer is zero, because there are no sets of two diffrent primes whose sums are 4. Your job is to write a program that reports the number of such ways for the given n and k . Input The input is a sequence of datasets followed by a line containing two zeros separated by a space. A dataset is a line containing two positive integers n and k separated by a space. You may assume that n ≤ 1120 and k ≤ 14. Output The output should be composed of lines, each corresponding to an input dataset. An output line should contain one non-negative integer indicating the number of ways for n and k specified in the corresponding dataset. You may assume that it is less than 2 31 . Sample Input 24 3 24 2 2 1 1 1 4 2 18 3 17 1 17 3 17 4 100 5 1000 10 1120 14 0 0 Output for the Sample Input 2 3 1 0 0 2 1 0 1 55 200102899 2079324314
[ { "submission_id": "aoj_1269_11031582", "code_snippet": "//____ ____.__ __ _________ .__ .__ ________\n//\\ \\ / /|__| _____/ |_ ___________ \\_ ___ \\| |__ _____ _________ |__| / _____/_____ ____ ____\n// \\ Y / | |/ ___\\ __\\/ _ \\_ __ \\ / \\ \\/| | \\\\__ \\ / ___\\__ \\ | | / \\ ___\\__ \\ / \\ / ___\\\n// \\ / | \\ \\___| | ( <_> ) | \\/ \\ \\___| Y \\/ __ \\_/ /_/ > __ \\| | \\ \\_\\ \\/ __ \\| | \\/ /_/ >\n// \\___/ |__|\\___ >__| \\____/|__| \\______ /___| (____ /\\___ (____ /__| \\______ (____ /___| /\\___ /\n// \\/ \\/ \\/ \\//_____/ \\/ \\/ \\/ \\//_____/\n\n#include <bits/stdc++.h>\n\n#define int long long // talent activated\n\nusing namespace std;\n\ntypedef long long ll;\ntypedef long double ld;\ntypedef unsigned long long ull;\nconst int INF = 1e9 + 1;\nconst int MAXN = 2e5 + 1;\nconst int MOD1 = 1e9 + 7;\nconst int MOD2 = 998244353;\nconst int sz = 1e7 + 1;\nconst int LOG = 20;\n\n\nvector<int> primes;\n\nvoid solve() {\n int n, k;\n cin >> n >> k;\n if (n == k and n == 0) exit(0);\n \n int m = primes.size();\n \n vector<vector<vector<int>>> dp(m + 1, vector<vector<int>>(n + 1, vector<int>(k + 1, 0)));\n \n for (int i = 0; i <= m; i++){\n for (int ki = 0; ki <= k; ki++){\n dp[i][0][0] = 1;\n }\n \n }\n \n for (int i = 1; i <= m; i++){\n for (int j = 0; j <= n; j++){\n if (j - primes[i - 1] >= 0){\n for (int ki = 1; ki <= k; ki++){\n dp[i][j][ki] += dp[i - 1][j - primes[i - 1]][ki - 1];\n }\n }\n for (int ki = 1; ki <= k; ki++){\n dp[i][j][ki] += dp[i-1][j][ki];\n }\n \n \n }\n }\n \n cout << dp[m][n][k] << endl;\n return;\n \n \n \n \n}\n\nsigned main() {\n ios_base::sync_with_stdio(false);\n cin.tie(nullptr);\n \n \n vector<int> nums(1200, 1);\n nums[0] = 0;\n nums[1] = 0;\n \n for (int i = 2; i < nums.size(); i++){\n if (nums[i] == 1){\n for (int j = 2 * i; j <= 1200; j += i) nums[j] = 0;\n }\n }\n for (int i = 0; i < nums.size(); i++) if (nums[i]) primes.push_back(i);\n \n\n int tests = 1;\n // cin >> tests;\n while (true) {\n solve();\n }\n return 0;\n}", "accuracy": 1, "time_ms": 500, "memory_kb": 36340, "score_of_the_acc": -1.1775, "final_rank": 16 }, { "submission_id": "aoj_1269_10853825", "code_snippet": "#include <bits/stdc++.h>\n\nusing namespace std;\nbool mark[1121];\nlong long dp[1121][190][15];\nbool vis[1121][190][15];\nint n, k, elements;\nvoid seive(int);\nbool isPrime(int);\nlong long func(int, int, int);\nvector<int> primes;\n\nint main()\n{\n seive(1120);\n for(int i = 1; i <= 1120; i++)\n if(isPrime(i))\n primes.push_back(i);\n elements = primes.size();\n while(scanf(\"%d %d\", &n, &k) && (n || k))\n {\n memset(vis, false , sizeof vis);\n cout << func(n, 0, k) << endl;\n }\n return 0;\n}\n\n\nvoid seive(int n)\n{\n for(int i = 3; i * i <= n; i += 2) if(!mark[i]) for(int j = i * i; j <= n; j += i) mark[j] = true;\n}\n\nbool isPrime(int n)\n{\n if(n == 1) return false;\n else if(n == 2) return true;\n else if(n % 2 == 0) return false;\n else return mark[n] == false;\n}\n\nlong long func(int make, int index, int taken)\n{\n if(index == elements && (make != 0 || taken != 0)) return 0;\n if(make == 0 && taken != 0) return 0;\n if(make != 0 && taken == 0) return 0;\n if(make == 0 && taken == 0) return 1;\n if(primes[index] > make) return 0;\n if(vis[make][index][taken]) return dp[make][index][taken];\n long long ret1, ret2;\n if(make - primes[index] >= 0)\n {\n ret1 = func(make - primes[index], index + 1, taken - 1);\n ret2 = func(make, index + 1, taken);\n dp[make][index][taken] = ret1 + ret2;\n vis[make][index][taken] = true;\n return dp[make][index][taken];\n }\n return 0;\n}", "accuracy": 1, "time_ms": 140, "memory_kb": 31464, "score_of_the_acc": -0.9005, "final_rank": 14 }, { "submission_id": "aoj_1269_9237263", "code_snippet": "#include <algorithm>\n#include <cmath>\n#include <cstdlib>\n#include <iomanip>\n#include <iostream>\n#include <list>\n#include <map>\n#include <numeric>\n#include <queue>\n#include <set>\n#include <stack>\n#include <string>\n#include <unordered_set>\n#include <vector>\n\n#define N_MAX (1120)\n#define K_MAX (14)\n\nusing namespace std;\nusing ll = long long;\n\nvector<int> GeneratePrimes(void) {\n\tvector<int> primes;\n\tvector<bool> is_prime(N_MAX + 1, true);\n\n\tfor (int i = 2; i <= N_MAX; i++) {\n\t\tif (is_prime[i]) {\n\t\t\tprimes.push_back(i);\n\t\t\tfor (int j = 2 * i; j <= N_MAX; j += i) is_prime[j] = false;\n\t\t}\n\t}\n\n\treturn primes;\n}\n\nint main(void) {\n\tvector<int> primes = GeneratePrimes();\n\n\t// dp[n][k] : n を互いに異なる k 個の素数の和で表す方法の数\n\tvector<vector<ll>> dp(N_MAX + 1, vector<ll>(K_MAX + 1, 0LL));\n\tdp[0][0] = 1LL;\n\n\tfor (const int p : primes) {\n\t\tvector<vector<ll>> dp_add(N_MAX + 1, vector<ll>(K_MAX + 1, 0LL));\n\t\tfor (int n = 0; n + p <= N_MAX; n++) {\n\t\t\tfor (int k = 0; k + 1 <= K_MAX; k++) {\n\t\t\t\tif (dp[n][k] > 0) dp_add[n + p][k + 1] = dp[n][k];\n\t\t\t}\n\t\t}\n\n\t\tfor (int n = 0; n <= N_MAX; n++) {\n\t\t\tfor (int k = 0; k <= K_MAX; k++) {\n\t\t\t\tdp[n][k] += dp_add[n][k];\n\t\t\t}\n\t\t}\n\t}\n\n\twhile (true) {\n\t\tint N, K;\n\t\tcin >> N >> K;\n\t\tif (N == 0) break;\n\t\tcout << dp[N][K] << endl;\n\t}\n\n\treturn 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3688, "score_of_the_acc": -0.0182, "final_rank": 2 }, { "submission_id": "aoj_1269_5018793", "code_snippet": "#pragma region Macros\n#include <bits/stdc++.h>\n#define ll long long\n#define ld long double\n#define rep2(i, a, b) for(ll i = a; i <= b; ++i)\n#define rep(i, n) for(ll i = 0; i < n; ++i)\n#define rep3(i, a, b) for(ll i = a; i >= b; --i)\n#define pii pair<int, int>\n#define pll pair<ll, ll>\n#define pb push_back\n#define eb emplace_back\n#define vi vector<int>\n#define vll vector<ll>\n#define vpi vector<pii>\n#define vpll vector<pll>\n#define endl '\\n'\n#define overload2(_1, _2, name, ...) name\n#define vec(type, name, ...) vector<type> name(__VA_ARGS__)\n#define VEC(type, name, size)\\\n vector<type> name(size);\\\n IN(name)\n#define vv(type, name, h, ...) vector<vector<type>> name(h, vector<type>(__VA_ARGS__))\n#define VV(type, name, h, w)\\\n vector<vector<type>> name(h, vector<type>(w));\\\n IN(name)\n#define vvv(type, name, h, w, ...) vector<vector<vector<type>>> name(h, vector<vector<type>>(w, vector<type>(__VA_ARGS__)))\n#define vvvv(type, name, a, b, c, ...)\\\n vector<vector<vector<vector<type>>>> name(a, vector<vector<vector<type>>>(b, vector<vector<type>>(c, vector<type>(__VA_ARGS__))))\n#define fi first\n#define se second\n#define all(c) begin(c), end(c)\n#define lb(c, x) distance((c).begin(), lower_bound(all(c), (x)))\n#define ub(c, x) distance((c).begin(), upper_bound(all(c), (x)))\nusing namespace std;\ntemplate <class T> using pq = priority_queue<T>;\ntemplate <class T> using pqg = priority_queue<T, vector<T>, greater<T>>;\n#define si(c) (int)(c).size()\n#define INT(...)\\\n int __VA_ARGS__;\\\n IN(__VA_ARGS__)\n#define LL(...)\\\n ll __VA_ARGS__;\\\n IN(__VA_ARGS__)\n#define STR(...)\\\n string __VA_ARGS__;\\\n IN(__VA_ARGS__)\n#define CHR(...)\\\n char __VA_ARGS__;\\\n IN(__VA_ARGS__)\n#define DBL(...)\\\n double __VA_ARGS__;\\\n IN(__VA_ARGS__)\nint scan() { return getchar(); }\nvoid scan(int &a) { cin >> a; }\nvoid scan(long long &a) { cin >> a; }\nvoid scan(char &a) { cin >> a; }\nvoid scan(double &a) { cin >> a; }\nvoid scan(string &a) { cin >> a; }\ntemplate <class T, class S> void scan(pair<T, S> &p) { scan(p.first), scan(p.second); }\ntemplate <class T> void scan(vector<T> &);\ntemplate <class T> void scan(vector<T> &a) {\n for(auto &i : a) scan(i);\n}\ntemplate <class T> void scan(T &a) { cin >> a; }\nvoid IN() {}\ntemplate <class Head, class... Tail> void IN(Head &head, Tail &... tail) {\n scan(head);\n IN(tail...);\n}\ntemplate <class T, class S> inline bool chmax(T &a, S b) {\n if(a < b) {\n a = b;\n return 1;\n }\n return 0;\n}\ntemplate <class T, class S> inline bool chmin(T &a, S b) {\n if(a > b) {\n a = b;\n return 1;\n }\n return 0;\n}\nvi iota(int n) {\n vi a(n);\n iota(all(a), 0);\n return a;\n}\ntemplate <typename T> vi iota(vector<T> &a, bool greater = false) {\n vi res(a.size());\n iota(all(res), 0);\n sort(all(res), [&](int i, int j) {\n if(greater) return a[i] > a[j];\n return a[i] < a[j];\n });\n return res;\n}\n#define UNIQUE(x) sort(all(x)), x.erase(unique(all(x)), x.end())\ntemplate <class T> T POW(T x, int n) {\n T res = 1;\n for(; n; n >>= 1, x *= x)\n if(n & 1) res *= x;\n return res;\n}\nvector<pll> factor(ll x) {\n vector<pll> ans;\n for(ll i = 2; i * i <= x; i++)\n if(x % i == 0) {\n ans.push_back({i, 1});\n while((x /= i) % i == 0) ans.back().second++;\n }\n if(x != 1) ans.push_back({x, 1});\n return ans;\n}\ntemplate <class T> vector<T> divisor(T x) {\n vector<T> ans;\n for(T i = 1; i * i <= x; i++)\n if(x % i == 0) {\n ans.pb(i);\n if(i * i != x) ans.pb(x / i);\n }\n return ans;\n}\ntemplate <typename T> void zip(vector<T> &x) {\n vector<T> y = x;\n sort(all(y));\n for(int i = 0; i < x.size(); ++i) { x[i] = lb(y, x[i]); }\n}\nint popcount(ll x) { return __builtin_popcountll(x); }\nint in() {\n int x;\n cin >> x;\n return x;\n}\nll lin() {\n unsigned long long x;\n cin >> x;\n return x;\n}\ntemplate <typename T> struct edge {\n int from, to;\n T cost;\n int id;\n edge(int to, T cost) : from(-1), to(to), cost(cost) {}\n edge(int from, int to, T cost) : from(from), to(to), cost(cost) {}\n edge(int from, int to, T cost, int id) : from(from), to(to), cost(cost), id(id) {}\n edge &operator=(const int &x) {\n to = x;\n return *this;\n }\n operator int() const { return to; }\n};\ntemplate <typename T> using Edges = vector<edge<T>>;\nusing Tree = vector<vector<int>>;\nusing Graph = vector<vector<int>>;\ntemplate <class T> using Wgraph = vector<vector<edge<T>>>;\nGraph getG(int n, int m = -1, bool directed = false, int margin = 1) {\n Tree res(n);\n if(m == -1) m = n - 1;\n while(m--) {\n int a, b;\n cin >> a >> b;\n a -= margin, b -= margin;\n res[a].emplace_back(b);\n if(!directed) res[b].emplace_back(a);\n }\n return move(res);\n}\ntemplate <class T> Wgraph<T> getWg(int n, int m = -1, bool directed = false, int margin = 1) {\n Wgraph<T> res(n);\n if(m == -1) m = n - 1;\n while(m--) {\n int a, b;\n T c;\n cin >> a >> b >> c;\n a -= margin, b -= margin;\n res[a].emplace_back(b, c);\n if(!directed) res[b].emplace_back(a, c);\n }\n return move(res);\n}\n#define i128 __int128_t\n#define ull unsigned long long int\ntemplate <typename T> static constexpr T inf = numeric_limits<T>::max() / 2;\nstruct Setup_io {\n Setup_io() {\n ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);\n cout << fixed << setprecision(15);\n }\n} setup_io;\ntemplate <typename A, typename B>\nostream& operator <<(ostream& out, const pair<A, B>& a) {\nout << \"(\" << a.first << \",\" << a.second << \")\";\nreturn out;\n}\ntemplate <typename T, size_t N>\nostream& operator <<(ostream& out, const array<T, N>& a) {\nout << \"[\"; bool first = true;\nfor (auto& v : a) { out << (first ? \"\" : \", \"); out << v; first = 0;} out << \"]\";\nreturn out;\n}\ntemplate <typename T>\nostream& operator <<(ostream& out, const vector<T>& a) {\nout << \"[\"; bool first = true;\nfor (auto& v : a) { out << (first ? \"\" : \", \"); out << v; first = 0;} out << \"]\";\nreturn out;\n}\ntemplate <typename T, class Cmp>\nostream& operator <<(ostream& out, const set<T, Cmp>& a) {\nout << \"{\"; bool first = true;\nfor (auto& v : a) { out << (first ? \"\" :\", \"); out << v; first = 0;} out << \"}\";\nreturn out;\n}\ntemplate <typename U, typename T, class Cmp>\nostream& operator <<(ostream& out, const map<U, T, Cmp>& a) {\nout << \"{\"; bool first = true;\nfor (auto& p : a) { out << (first ? \"\" : \", \"); out << p.first << \":\" << p.second; first = 0;} out << \"}\";\nreturn out;\n}\n// #define LOCAL\n#ifdef LOCAL\n#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)\n#else\n#define trace(...) 42\n#endif\ntemplate <typename Arg1>\nvoid __f(const char* name, Arg1&& arg1){\ncerr << name << \": \" << arg1 << endl;\n}\ntemplate <typename Arg1, typename... Args>\nvoid __f(const char* names, Arg1&& arg1, Args&&... args){\nconst char* comma = strchr(names + 1, ',');\ncerr.write(names, comma - names) << \": \" << arg1 << \" |\";\n__f(comma + 1, args...);\n}\n#pragma endregion\n//#include<atcoder/all>\n//using namespace atcoder;\nint main(){\n vector<int> primes(1130);\n for(int i = 2;i < 1130;i++){\n for(int j = i+i;j < 1130;j += i){\n primes[j] = 1;\n }\n }\n primes[0] = 1;\n primes[1] = 1;\n vector<int> res;\n rep(i,1130)if(!primes[i])res.pb(i);\n trace(res);\n int m = res.size();\n while(1){\n INT(n,K);\n if(n == 0 && K == 0)return 0;\n vector<vector<vector<int>>> dp(m+1,vector<vector<int>> (K+1,vector<int>(n+1,0)));\n dp[0][0][0] = 1;\n rep(i,m)rep(j,K+1)rep(k,n+1){\n dp[i+1][j][k] += dp[i][j][k];\n if(j+1 < K+1 && k + res[i] < n+1)dp[i+1][j+1][k+res[i]] += dp[i][j][k];\n }\n cout << dp[m][K][n] << endl;\n }\n return 0;\n}", "accuracy": 1, "time_ms": 190, "memory_kb": 15568, "score_of_the_acc": -0.4406, "final_rank": 10 }, { "submission_id": "aoj_1269_4745414", "code_snippet": "/*\n問題D:異なる素数の和\n\n・案\n1、素数リストを作成し\n2、動的計画法\nn_max = 1120, k <= 14\n素数リストをはだいたい p=188\n全探索するなら p C k = 188 C 14 > 10^20\n\nk = 1 | k = 2\n--------|-------\nn=4 p=2 | n = 6\nn=3 p=3 | n = 6\nn=1 p=5 | n = 6\n\n異なる素数を扱うので扱った素数を記憶しておく\n*/\n\n#include <iostream>\n#include <cstring> \nusing namespace std;\n\nint p[1130]; // primes\nint prime_num;\nint c[1121][15][188]; // メモ化\n\nvoid calc_primes() {\n p[0] = 2;\n prime_num = 1;\n for (int i=3; i<=1120; i+=2) {\n bool add = true;\n for (int j=0; j<prime_num; j++) {\n if (p[j]*p[j] > i) { break; }\n if (i % p[j] == 0) {\n add = false;\n break;\n }\n }\n if (add) { p[prime_num++] = i; }\n }\n}\n\nint solve(int n, int k, int idx) {\n if (n == 0 && k == 0) {\n return 1;\n } else if (n == 0 || k == 0) {\n return 0;\n }\n if (c[n][k][idx] >= 0) { return c[n][k][idx]; }\n\n int cnt = 0;\n for (int i=idx; i<prime_num; i++) {\n if (p[i] > n) {\n break;\n } else {\n cnt += solve(n - p[i], k - 1, i+1);\n }\n }\n c[n][k][idx] = cnt;\n \n return cnt;\n}\n\nint main() {\n calc_primes();\n//cout << pnum << endl;\n//for (int i=0; i<pnum; i++) { cout << p[i] << \", \"; }\n memset(c, -1, sizeof(c));\n \n int n, k;\n while (cin >> n >> k, (n || k)) {\n cout << solve(n, k, 0) << endl;\n }\n \n return 0;\n}", "accuracy": 1, "time_ms": 110, "memory_kb": 15436, "score_of_the_acc": -0.4077, "final_rank": 7 }, { "submission_id": "aoj_1269_4666193", "code_snippet": "#include <stdio.h>\n#include <iostream>\n#include <vector>\n#include <math.h>\nusing namespace std;\n\n#define MAXN 1121\n#define MAXK 15\n\nint N, K;\nint dp[MAXN][MAXK];\nint i, j;\nvector<int> prime;\n\nvoid makeprime(){\n bool primebool[MAXN];\n for(i = 0; i < MAXN; i++){\n primebool[i] = true;\n }\n\n primebool[0] = false;\n primebool[1] = false;\n\n for(i = 0; i < sqrt(MAXN); i++){\n if(!primebool[i]) continue;\n for(int j = 2 * i; j < MAXN; j += i){\n primebool[j] = false;\n }\n }\n\n for(i = 0; i < MAXN; i++){\n if(primebool[i]) prime.push_back(i);\n }\n}\n\n\nint main(){\n makeprime();\n while(true){\n cin >> N >> K;\n if(N == 0 && K == 0) break;\n\n for(i = 0; i < MAXN; i++){\n for(j = 0; j < MAXK; j++){\n dp[i][j] = 0;\n }\n }\n dp[0][0] = 1;\n\n for(int x: prime){\n if(x > N) break;\n for(j = K; j >= 0; j--){\n for(i = 0; i <= N; i++){\n if(dp[i][j] != 0 && i + x <= N) dp[i + x][j + 1] += dp[i][j];\n }\n }\n }\n cout << dp[N][K] << endl;\n }\n}", "accuracy": 1, "time_ms": 90, "memory_kb": 3084, "score_of_the_acc": -0.029, "final_rank": 3 }, { "submission_id": "aoj_1269_4651368", "code_snippet": "#include <stdio.h>\n#include <iostream>\n#include <vector>\n#include <math.h>\nusing namespace std;\n\n#define MAXN 1121\n#define MAXK 15\n\nint N, K;\nint dp[MAXN][MAXK], dpnext[MAXN][MAXK];\nint i, j;\nvector<int> prime;\n\nvoid makeprime(){\n bool primebool[MAXN];\n for(i = 0; i < MAXN; i++){\n primebool[i] = true;\n }\n\n primebool[0] = false;\n primebool[1] = false;\n\n for(i = 0; i < sqrt(MAXN); i++){\n if(!primebool[i]) continue;\n for(int j = 2 * i; j < MAXN; j += i){\n primebool[j] = false;\n }\n }\n\n for(i = 0; i < MAXN; i++){\n if(primebool[i]) prime.push_back(i);\n }\n}\n\n\nint main(){\n makeprime();\n while(true){\n cin >> N >> K;\n if(N == 0 && K == 0) break;\n\n for(i = 0; i < MAXN; i++){\n for(j = 0; j < MAXK; j++){\n dp[i][j] = 0;\n dpnext[i][j] = 0;\n }\n }\n dp[0][0] = 1;\n\n for(int x: prime){\n if(x > N) break;\n for(i = 0; i <= N; i++){\n for(j = 0; j <= K; j++){\n dpnext[i][j] = 0;\n }\n }\n\n for(j = 0; j <= K; j++){\n for(i = 0; i <= N; i++){\n dpnext[i][j] += dp[i][j];\n if(dp[i][j] != 0 && i + x <= N) dpnext[i + x][j + 1] += dp[i][j];\n }\n }\n\n for(j = 0; j <= K; j++){\n for(i = 0; i <= N; i++){\n dp[i][j] = dpnext[i][j];\n }\n }\n }\n cout << dp[N][K] << endl;\n }\n}", "accuracy": 1, "time_ms": 370, "memory_kb": 3252, "score_of_the_acc": -0.1355, "final_rank": 5 }, { "submission_id": "aoj_1269_4314600", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\nusing ll = long long int;\n\nconstexpr int MAX_N = 1125;\n\nvector<bool> IsPrime;\nvector<int> primes;\n\nvoid sieve(int max) {\n IsPrime.resize(max + 1, true);\n IsPrime[0] = IsPrime[1] = false;\n for (int i = 2; i * i <= max; ++i){\n if (IsPrime[i]){\n for (int j = i * i; j <= max; j += i) IsPrime[j] = false;\n }\n }\n for(int i = 0; i <= max; ++i) if(IsPrime[i]) primes.push_back(i);\n}\n\nll memo[MAX_N][200][15];\n\nll rec(int n, int m, int k){\n if(k == 0) return n == 0;\n if(m < 0) return 0;\n if(memo[n][m][k] >= 0) return memo[n][m][k];\n ll res = 0;\n for(int i=m;i>=0;i--){\n if(primes[i] <= n){\n res += rec(n-primes[i],i-1,k-1);\n }\n }\n return memo[n][m][k] = res;\n}\n\nint solve(){\n int n, k;\n cin >> n >> k;\n if(n == 0) return 1;\n cout << rec(n, int(primes.size())-1, k) << endl;\n return 0;\n}\n\nint main(){\n sieve(MAX_N);\n for(int i=0;i<MAX_N;i++){\n for(int j=0;j<200;j++){\n fill(memo[i][j],memo[i][j]+15,-1LL);\n }\n }\n while(solve()==0);\n return 0;\n}", "accuracy": 1, "time_ms": 60, "memory_kb": 29540, "score_of_the_acc": -0.8136, "final_rank": 13 }, { "submission_id": "aoj_1269_3953545", "code_snippet": "#include <iostream>\n#include <vector>\n#include <algorithm>\n#include <string>\n#include <numeric>\n\n#define REP(i, a, b) for (int i = int(a); i < int(b); i++)\nusing namespace std;\ntypedef long long int ll;\n\n// clang-format off\n#ifdef _DEBUG_\n#define dump(...) do{ cerr << __LINE__ << \":\\t\" << #__VA_ARGS__ << \" = \"; PPPPP(__VA_ARGS__); cerr << endl; } while(false)\ntemplate<typename T> void PPPPP(T t) { cerr << t; }\ntemplate<typename T, typename... S> void PPPPP(T t, S... s) { cerr << t << \", \"; PPPPP(s...); }\n#else\n#define dump(...)\n#endif\ntemplate<typename T> vector<T> make_v(size_t a, T b) { return vector<T>(a, b); }\ntemplate<typename... Ts> auto make_v(size_t a, Ts... ts) { return vector<decltype(make_v(ts...))>(a, make_v(ts...)); }\ntemplate<typename T>\nbool chmin(T &a, T b) { if (a > b) {a = b; return true; } return false;}\ntemplate<typename T>\nbool chmax(T &a, T b) { if (a < b) {a = b; return true; } return false;}\n// clang-format on\n\nint main() {\n cin.tie(nullptr);\n ios::sync_with_stdio(false);\n\n int SZ = 1200;\n vector<bool> isPrime(SZ, true);\n vector<int> prime;\n isPrime[0] = isPrime[1] = false;\n REP(i, 2, SZ) {\n if (isPrime[i]) {\n prime.push_back(i);\n for (int j = i * 2; j < SZ; j += i) {\n isPrime[j] = false;\n }\n }\n }\n\n int P = prime.size();\n int n, k;\n while (cin >> n >> k, n + k) {\n auto dp = make_v(P + 1, n + 1, k + 1, 0LL);\n dp[0][0][0] = 1;\n REP(i, 0, P) {\n REP(j, 0, n + 1) {\n REP(t, 0, k + 1) {\n if (j + prime[i] <= n && t + 1 <= k) {\n dp[i + 1][j + prime[i]][t + 1] += dp[i][j][t];\n }\n dp[i + 1][j][t] += dp[i][j][t];\n }\n }\n }\n cout << dp[P][n][k] << endl;\n }\n return 0;\n}", "accuracy": 1, "time_ms": 710, "memory_kb": 36164, "score_of_the_acc": -1.2483, "final_rank": 18 }, { "submission_id": "aoj_1269_3893960", "code_snippet": "#include <cstdio>\n#include <cstdlib>\n#include <cmath>\n#include <cstring>\n\n#include <iostream>\n#include <complex>\n#include <string>\n#include <algorithm>\n#include <numeric>\n#include <vector>\n#include <queue>\n#include <stack>\n#include <map>\n#include <set>\n#include <unordered_map>\n#include <unordered_set>\n\n#include <functional>\n#include <cassert>\n\ntypedef long long ll;\nusing namespace std;\n\n#ifndef LOCAL\n#define debug(x) ;\n#else\n#define debug(x) cerr << __LINE__ << \" : \" << #x << \" = \" << (x) << endl;\n\ntemplate <typename T1, typename T2>\nostream &operator<<(ostream &out, const pair<T1, T2> &p) {\n out << \"{\" << p.first << \", \" << p.second << \"}\";\n return out;\n}\n\ntemplate <typename T>\nostream &operator<<(ostream &out, const vector<T> &v) {\n out << '{';\n for (const T &item : v) out << item << \", \";\n out << \"\\b\\b}\";\n return out;\n}\n#endif\n\n#define mod 1000000007 //1e9+7(prime number)\n#define INF 1000000000 //1e9\n#define LLINF 2000000000000000000LL //2e18\n#define SIZE 200010\n\nvector<bool> isPrime;\n\nvoid Eratosthenes(int N){\n isPrime.assign(N+1, true);\n isPrime[0] = isPrime[1] = false;\n\n for(int i=2; i*i<=N; i++)\n if(isPrime[i])\n for(int j=i; i*j<=N; j++)\n isPrime[i*j] = false;\n}\n\nint dp[15][1121] = {};\n\nbool solve() {\n int N, K;\n\n scanf(\"%d%d\", &N, &K);\n\n if (N == 0) return false;\n\n memset(dp, 0, sizeof(dp));\n\n dp[0][0] = 1;\n\n for(int i=0; i<=N; i++) {\n if (!isPrime[i]) continue;\n\n for(int k=K-1; k>=0; k--) {\n for(int j=0; j+i<=N; j++) {\n dp[k+1][j+i] += dp[k][j];\n }\n }\n }\n\n printf(\"%d\\n\", dp[K][N]);\n\n return true;\n}\n\nint main(){\n\n Eratosthenes(1120);\n\n while(solve());\n\n return 0;\n}", "accuracy": 1, "time_ms": 20, "memory_kb": 3160, "score_of_the_acc": -0.0059, "final_rank": 1 }, { "submission_id": "aoj_1269_3784453", "code_snippet": "#include<iostream>\n#include<vector>\n#include<algorithm>\nusing namespace std;\n\nint dp[15][200][1121] = {};\nbool memo[15][200][1121] = {};\nvector<int> p;\n\nint dfs(int num, int ppos, int sum){\n if(memo[num][ppos][sum]) return dp[num][ppos][sum];\n memo[num][ppos][sum] = true;\n if(num == 0 && sum == 0) return dp[num][ppos][sum] = 1;\n if(ppos == 0 || num == 0) return 0;\n for(int i = 0; i <= 1 && sum-i*p[ppos-1] >= 0; i++){\n dp[num][ppos][sum] += dfs(num-i, ppos-1, sum-i*p[ppos-1]);\n }\n return dp[num][ppos][sum];\n}\n\nint main(){\n bool np[1120] = {};\n for(int i = 2; i < 1120; i++){\n if(np[i]) continue;\n p.push_back(i);\n for(int j = i+i; j < 1120; j+=i) np[j] = true;\n }\n memo[0][0][0] = true;\n dp[0][0][0] = 1;\n int n, k;\n while(cin >> n >> k, n+k) cout << dfs(k, upper_bound(p.begin(),p.end(),n)-p.begin(), n) << endl;\n return 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 17672, "score_of_the_acc": -0.4387, "final_rank": 9 }, { "submission_id": "aoj_1269_3784447", "code_snippet": "#include<iostream>\n#include<vector>\n#include<algorithm>\nusing namespace std;\n\nint dp[15][200][1121] = {};\nbool memo[15][200][1121] = {};\nvector<int> p;\n\nint dfs(int num, int ppos, int sum){\n if(memo[num][ppos][sum]) return dp[num][ppos][sum];\n memo[num][ppos][sum] = true;\n if(num == 0 && sum == 0) return dp[num][ppos][sum] = 1;\n if(ppos == 0 || num == 0 || sum == 0) return 0;\n for(int i = 0; i <= 1 && sum-i*p[ppos-1] >= 0; i++){\n dp[num][ppos][sum] += dfs(num-i, ppos-1, sum-i*p[ppos-1]);\n }\n return dp[num][ppos][sum];\n}\n\nint main(){\n bool np[1120] = {};\n for(int i = 2; i < 1120; i++){\n if(np[i]) continue;\n p.push_back(i);\n for(int j = i+i; j < 1120; j+=i) np[j] = true;\n }\n memo[0][0][0] = true;\n dp[0][0][0] = 1;\n int n, k;\n while(cin >> n >> k, n+k) cout << dfs(k, upper_bound(p.begin(),p.end(),n)-p.begin(), n) << endl;\n return 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 17628, "score_of_the_acc": -0.4373, "final_rank": 8 }, { "submission_id": "aoj_1269_3551531", "code_snippet": "#include <algorithm>\n#include <bitset>\n#include <complex>\n#include <deque>\n#include <exception>\n#include <fstream>\n#include <functional>\n#include <iomanip>\n#include <ios>\n#include <iosfwd>\n#include <iostream>\n#include <istream>\n#include <iterator>\n#include <limits>\n#include <list>\n#include <locale>\n#include <map>\n#include <memory>\n#include <new>\n#include <numeric>\n#include <ostream>\n#include <queue>\n#include <set>\n#include <sstream>\n#include <stack>\n#include <stdexcept>\n#include <streambuf>\n#include <string>\n#include <typeinfo>\n#include <utility>\n#include <valarray>\n#include <vector>\n#include <climits>\n#include <cstring>\n\nusing namespace std;\n\n#define MAX_N 1121\n#define MAX_K 15\n\nvector<int> primes;\nint dp[MAX_N][MAX_N / 3][MAX_K];\n\n// m以上の素数をk個使ってnを作る方法\nint rec(int n, int m, int k) {\n if (n == 0 && k == 0) return 1;\n if (k == 0) return 0;\n if (dp[n][m][k] != -1) return dp[n][m][k];\n\n int ret = 0;\n for (int i = m; i < primes.size(); ++i) {\n if (n - primes[i] >= 0) {\n ret += rec(n - primes[i], i + 1, k - 1);\n }\n }\n\n return dp[n][m][k] = ret;\n}\n\nvoid solve(int n, int k) {\n memset(dp, -1, sizeof(dp)); // dpの値をすべて-1に\n int ans = rec(n, 0, k);\n cout << ans << endl;\n}\n\nint main() {\n cin.tie(0);\n ios::sync_with_stdio(false);\n cout << fixed << setprecision(10);\n\n for (int i = 2; i < MAX_N; ++i) {\n bool flag = true;\n for (int j = 2; j * j <= i; ++j) {\n if (i % j == 0) flag = false;\n }\n if (flag) primes.push_back(i);\n }\n\n while (true) {\n int n, k;\n cin >> n >> k;\n if (n == 0) break;\n solve(n, k);\n }\n\n return 0;\n}", "accuracy": 1, "time_ms": 2760, "memory_kb": 27572, "score_of_the_acc": -1.7327, "final_rank": 19 }, { "submission_id": "aoj_1269_3551520", "code_snippet": "#include <algorithm>\n#include <bitset>\n#include <complex>\n#include <deque>\n#include <exception>\n#include <fstream>\n#include <functional>\n#include <iomanip>\n#include <ios>\n#include <iosfwd>\n#include <iostream>\n#include <istream>\n#include <iterator>\n#include <limits>\n#include <list>\n#include <locale>\n#include <map>\n#include <memory>\n#include <new>\n#include <numeric>\n#include <ostream>\n#include <queue>\n#include <set>\n#include <sstream>\n#include <stack>\n#include <stdexcept>\n#include <streambuf>\n#include <string>\n#include <typeinfo>\n#include <utility>\n#include <valarray>\n#include <vector>\n#include <climits>\n#include <cstring>\n\n#define rep(i, m, n) for(int i=int(m);i<int(n);i++)\n#define all(c) begin(c),end(c)\n\ntemplate<typename T1, typename T2>\ninline void chmin(T1 &a, T2 b) { if (a > b) a = b; }\n\ntemplate<typename T1, typename T2>\ninline void chmax(T1 &a, T2 b) { if (a < b) a = b; }\n\n//改造\ntypedef long long int ll;\nusing namespace std;\n#define INF (1 << 30) - 1\n#define INFl (ll)5e15\n#define DEBUG 0 //デバッグする時1にしてね\n#define dump(x) cerr << #x << \" = \" << (x) << endl\n#define MOD 1000000007\n\n\n//ここから編集する\n#define MAX_N 1121\n#define MAX_K 15\n\n//class Solve {\n//public:\nvector<int> primes;\nint dp[MAX_N][MAX_N / 3][MAX_K];\n\n//bool isPrime(int n) {\n// for (int i = 2; i * i <= n; ++i) {\n// if (n % i == 0) return false;\n// }\n// return true;\n// return primes.count(n);\n//}\n\n// m以上の素数をk個使ってnを作る方法\nint rec(int n, int m, int k) {\n if (n == 0 && k == 0) return 1;\n if (k == 0) return 0;\n if (dp[n][m][k] != -1) return dp[n][m][k];\n\n int ret = 0;\n for (int i = m; i < primes.size(); ++i) {\n if (n - primes[i] >= 0) {\n ret += rec(n - primes[i], i + 1, k - 1);\n }\n }\n\n return dp[n][m][k] = ret;\n}\n\nvoid solve(int n, int k) {\n memset(dp, -1, sizeof(dp)); // dpの値をすべて-1に\n int ans = rec(n, 0, k);\n cout << ans << endl;\n}\n//};\n\n\nint main() {\n cin.tie(0);\n ios::sync_with_stdio(false);\n cout << fixed << setprecision(10);\n\n for (int i = 2; i < MAX_N; ++i) {\n bool flag = true;\n for (int j = 2; j * j <= i; ++j) {\n if (i % j == 0) flag = false;\n }\n if (flag) primes.push_back(i);\n }\n\n while (true) {\n int n, k;\n cin >> n >> k;\n if (n == 0) break;\n solve(n, k);\n }\n\n\n return 0;\n}", "accuracy": 1, "time_ms": 2770, "memory_kb": 27668, "score_of_the_acc": -1.7392, "final_rank": 20 }, { "submission_id": "aoj_1269_3328296", "code_snippet": "#include <bits/stdc++.h>\n\n#define FOR(i,a,b) for(int i= (a); i<((int)b); ++i)\n#define RFOR(i,a) for(int i=(a); i >= 0; --i)\n#define FOE(i,a) for(auto i : a)\n#define ALL(c) (c).begin(), (c).end()\n#define RALL(c) (c).rbegin(), (c).rend()\n#define DUMP(x) cerr << #x << \" = \" << (x) << endl;\n#define SUM(x) std::accumulate(ALL(x), 0LL)\n#define MIN(v) *std::min_element(v.begin(), v.end())\n#define MAX(v) *std::max_element(v.begin(), v.end())\n#define EXIST(v,x) (std::find(v.begin(), v.end(), x) != v.end())\n#define BIT_ON(bit, i) (bit & (1LL << i))\n\ntypedef long long LL;\ntemplate<typename T> using V = std::vector<T>;\ntemplate<typename T> using VV = std::vector<std::vector<T>>;\ntemplate<typename T> using VVV = std::vector<std::vector<std::vector<T>>>;\ntemplate<typename T> using VVVV = std::vector<std::vector<std::vector<std::vector<T>>>>;\n\ntemplate<class T> inline T ceil(T a, T b) { return (a + b - 1) / b; }\ntemplate<class T> inline void print(T x) { std::cout << x << std::endl; }\ntemplate<class T> inline void print_vec(const std::vector<T> &v) { for (int i = 0; i < v.size(); ++i) { if (i != 0) {std::cout << \" \";} std::cout << v[i];} std::cout << \"\\n\"; }\ntemplate<class T> inline bool inside(T y, T x, T H, T W) {return 0 <= y and y < H and 0 <= x and x < W; }\ntemplate<class T> inline double euclidean_distance(T y1, T x1, T y2, T x2) { return sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2)); }\ntemplate<class T> inline double manhattan_distance(T y1, T x1, T y2, T x2) { return abs(x1 - x2) + abs(y1 - y2); }\ntemplate<class T> inline std::vector<T> unique(std::vector<T> v) {\n sort(v.begin(), v.end());\n v.erase(unique(v.begin(), v.end()), v.end());\n return v;\n}\n\nconst int INF = 1 << 30;\nconst double EPS = 1e-9;\nconst std::string YES = \"YES\", Yes = \"Yes\", NO = \"NO\", No = \"No\";\nconst std::vector<int> dy4 = { 0, 1, 0, -1 }, dx4 = { 1, 0, -1, 0 }; // 4近傍(右, 下, 左, 上)\nconst std::vector<int> dy8 = { 0, -1, 0, 1, 1, -1, -1, 1 }, dx8 = { 1, 0, -1, 0, 1, 1, -1, -1 };\n\nusing namespace std;\n\n// nまでの素数のリストを作成(nを含む) O(N log log N)\nvector<int> make_prime_list(int n) {\n vector<bool> is_prime(n + 1, true);\n is_prime[0] = false;\n is_prime[1] = false;\n\n vector<int> prime_list;\n for (int i = 2; i < n + 1; ++i) {\n if (!is_prime[i]) { continue; }\n prime_list.emplace_back(i);\n\n for (int j = i + i; j < n + 1; j += i) {\n is_prime[j] = false;\n }\n }\n return prime_list; // 素数のリスト\n //return is_prime; // すべての数値について素数かどうか\n}\n\n\nint main() {\n cin.tie(0);\n ios::sync_with_stdio(false);\n\n V<int> prime_list = make_prime_list(1200);\n\n while (true) {\n int N, K;\n cin >> N >> K;\n if (N + K == 0) {\n break;\n }\n\n VVV<LL> dp(prime_list.size() + 1, VV<LL>(N + 1, V<LL>(K + 1, 0)));\n dp[0][0][0] = 1;\n FOR(i, 0, prime_list.size()) {\n FOR(j, 0, N + 1) {\n FOR(k, 0, K + 1) {\n\n // not use\n dp[i + 1][j][k] += dp[i][j][k];\n\n // use\n if (j + prime_list[i] <= N and k + 1 <= K) {\n dp[i + 1][j + prime_list[i]][k + 1] += dp[i][j][k];\n }\n }\n }\n }\n print(dp[prime_list.size()][N][K]);\n }\n\n return 0;\n}", "accuracy": 1, "time_ms": 710, "memory_kb": 36132, "score_of_the_acc": -1.2474, "final_rank": 17 }, { "submission_id": "aoj_1269_2127006", "code_snippet": "#include <stdio.h>\n#include <cmath>\n#include <algorithm>\n#include <stack>\n#include <queue>\n#include <vector>\n\nusing namespace std;\n\n\n#define NUM 1120\n\nint main(){\n\tint table[NUM],limit;\n\n\tfor(int i=0; i < NUM;i++)table[i] = 1;\n\ttable[0] = 0;\n\ttable[1] = 0;\n\n\tlimit = sqrt(NUM);\n\n\tfor(int i=2;i<=limit;i++){\n\t\tif(table[i] == 1){\n\t\t\tfor(int k=2*i;k < NUM; k += i){\n\t\t\t\ttable[k] = 0;\n\t\t\t}\n\t\t}\n\t}\n\n\tint prime_table[NUM],index = 0;\n\n\tfor(int i = 2; i < NUM; i++){\n\t\tif(table[i] == 1){\n\t\t\tprime_table[index++] = i;\n\t\t}\n\t}\n\n\tint*** dp = new int**[15];\n\tfor(int i = 0; i < 15; i++){\n\t\tdp[i] = new int*[index];\n\t\tfor(int k = 0; k < index; k++){\n\t\t\tdp[i][k] = new int[1121];\n\t\t}\n\t}\n\n\tfor(int i = 0; i < 15; i++){\n\t\tfor(int k = 0; k < index; k++){\n\t\t\tfor(int p = 0; p < 1121; p++)dp[i][k][p] = 0;\n\t\t}\n\t}\n\n\tfor(int i = 0; i < index; i++){\n\t\tdp[1][i][prime_table[i]] = 1;\n\t}\n\n\tfor(int k = 2; k <= 14; k++){\n\t\tfor(int i = 1; i < index; i++){\n\t\t\tfor(int p = 0; p <= i-1; p++){\n\t\t\t\tfor(int a = prime_table[i]+1; a <= 1120; a++){\n\t\t\t\t\tdp[k][i][a] += dp[k-1][p][a-prime_table[i]];\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n\n\tint A,B,sum;\n\n\twhile(true){\n\t\tscanf(\"%d %d\",&A,&B);\n\t\tif(A == 0 && B == 0)break;\n\n\t\tsum = 0;\n\n\t\tfor(int i = 0; i < index; i++)sum += dp[B][i][A];\n\n\t\tprintf(\"%d\\n\",sum);\n\n\t}\n\n\treturn 0;\n}", "accuracy": 1, "time_ms": 40, "memory_kb": 15036, "score_of_the_acc": -0.3703, "final_rank": 6 }, { "submission_id": "aoj_1269_2110819", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\nbool so[2000];\nvector<int>s;\nint n,k;\nint dp[3000][20];\n\nint main(){\n for(int i=2;i*i<2000;i++)\n if(!so[i])for(int j=2;i*j<2000;j++)so[i*j]=1;\n for(int i=2;i<=1120;i++)\n if(!so[i])s.push_back(i);\n while(cin>>n>>k&&n&&k){\n \n memset(dp,0,sizeof(dp));\n dp[0][0]=1;\n for(int i=0;i<s.size();i++){\n for(int j=k-1;j>=0;j--)\n\tfor(int l=n;l>=0;l--)\n\t dp[l+s[i]][j+1]+=dp[l][j];\n\t\n }\n cout<<dp[n][k]<<endl;\n }\n return 0;\n}", "accuracy": 1, "time_ms": 150, "memory_kb": 3220, "score_of_the_acc": -0.0548, "final_rank": 4 }, { "submission_id": "aoj_1269_2110736", "code_snippet": "#include <bits/stdc++.h>\n\nusing namespace std;\n\ntypedef long long ll;\n\nvector<int> pn;\nvector<bool> is_p;\n\nvoid enum_prime()\n{\n is_p.resize(1515, true);\n is_p[0] = is_p[1] = false;\n for(int i = 2; i <= 1515; i++) {\n if(!is_p[i]) continue;\n pn.push_back(i);\n for(int j = i+i; j <= 1515; j+=i) is_p[j] = false;\n }\n}\n\nint dp[1200][300][15];\n\nint solve(int n, int p, int k)\n{\n if(k == 0 && n == 0) return 1;\n if(k < 0 || n < 0 || p < 0) return 0; \n \n int& ret = dp[n][p][k];\n if(~ret) return ret;\n\n ret = solve(n, p - 1, k);\n if(n >= pn[p]) ret += solve(n - pn[p], p - 1, k - 1);\n\n return ret;\n}\n\nint main()\n{\n enum_prime();\n int n, k;\n while(cin >> n >> k, n || k) {\n memset(dp, -1, sizeof(dp));\n int p = upper_bound(pn.begin(), pn.end(), n) - pn.begin() - 1;\n cout << solve(n, p, k) << endl;\n }\n return 0;\n}", "accuracy": 1, "time_ms": 160, "memory_kb": 24252, "score_of_the_acc": -0.6909, "final_rank": 12 }, { "submission_id": "aoj_1269_2110645", "code_snippet": "#include<bits/stdc++.h>\n#define N 1150\nusing namespace std;\nbool prime[N];\nvector<int> p;\nint dp[205][N][15];\n\nint main(){\n for(int i=0;i<N;i++)\n prime[i]=true;\n prime[0]=prime[1]=false;\n for(int i=0;i*i<N;i++){\n if(!prime[i])continue;\n for(int j=i*2;j<N;j+=i)\n prime[j]=false;\n }\n for(int i=0;i<N;i++)\n if(prime[i])p.push_back(i);\n while(1){\n int n,k;\n cin>>n>>k;\n if(!n&&!k)break;\n memset(dp,0,sizeof(dp));\n dp[0][0][0]=1;\n for(int i=0;i<p.size();i++){\n for(int j=0;j<=n;j++){\n\tfor(int l=0;l<=k;l++){\n\t if(!dp[i][j][l])continue;\n\t if(j+p[i]<=n)\n\t dp[i+1][j+p[i]][l+1]+=dp[i][j][l];\n\t dp[i+1][j][l]+=dp[i][j][l];\n\t}\n }\n }\n cout<<dp[p.size()][n][k]<<endl;\n }\n return 0;\n}", "accuracy": 1, "time_ms": 170, "memory_kb": 16892, "score_of_the_acc": -0.4732, "final_rank": 11 }, { "submission_id": "aoj_1269_2110626", "code_snippet": "#include <bits/stdc++.h>\n#define M 2000\nusing namespace std;\nbool used2[M];\nvector <int> prime;\nint n,k;\n\nint mem[200][15][1200],used[200][15][1200];\nint dfs(int idx,int dep,int sum){\n if(sum==n&&dep==k) return 1;\n if(idx==(int)prime.size()||dep==k||sum>=n) return 0;\n if(used[idx][dep][sum]++) return mem[idx][dep][sum];\n \n int res=dfs(idx+1,dep,sum)+dfs(idx+1,dep+1,sum+prime[idx]);\n return mem[idx][dep][sum]=res;\n}\n\n\nint main(){\n used2[0]=used2[1]=1;\n for(int i=2;i*i<M;i++)\n if(!used2[i])\n for(int j=2;j<M/i;j++) used2[j*i]=1;\n \n for(int i=2;i<=1120;i++) if(!used2[i]) prime.push_back(i);\n while(1){\n cin>>n>>k;\n if(n==0&&k==0)break;\n memset(used,0,sizeof(used));\n cout<< dfs(0,0,0)<<endl;\n }\n \n return 0;\n}", "accuracy": 1, "time_ms": 820, "memory_kb": 29284, "score_of_the_acc": -1.0813, "final_rank": 15 } ]
aoj_1266_cpp
Problem A: How I Wonder What You Are! One of the questions children often ask is "How many stars are there in the sky?" Under ideal conditions, even with the naked eye, nearly eight thousands are observable in the northern hemisphere. With a decent telescope, you may find many more, but, as the sight field will be limited, you may find much less at a time. Children may ask the same questions to their parents on a planet of some solar system billions of light-years away from the Earth. Their telescopes are similar to ours with circular sight fields, but alien kids have many eyes and can look into different directions at a time through many telescopes. Given a set of positions of stars, a set of telescopes and the directions they are looking to, your task is to count up how many stars can be seen through these telescopes. Input The input consists of one or more datasets. The number of datasets is less than 50. Each dataset describes stars and the parameters of the telescopes used. The first line of a dataset contains a positive integer n not exceeding 500, meaning the number of stars. Each of the n lines following it contains three decimal fractions, s x , s y , and s z . They give the position ( s x , s y , s z ) of the star described in Euclidean coordinates. You may assume -1000 ≤ s x ≤ 1000, -1000 ≤ s y ≤ 1000, -1000 ≤ s z ≤ 1000 and ( s x , s y , s z ) ≠ (0, 0, 0). Then comes a line containing a positive integer m not exceeding 50, meaning the number of telescopes. Each of the following m lines contains four decimal fractions, t x , t y , t z , and φ , describing a telescope. The first three numbers represent the direction of the telescope. All the telescopes are at the origin of the coordinate system (0, 0, 0) (we ignore the size of the planet). The three numbers give the point ( t x , t y , t z ) which can be seen in the center of the sight through the telescope. You may assume -1000 ≤ t x ≤ 1000, -1000 ≤ t y ≤ 1000, -1000 ≤ t z ≤ 1000 and ( t x , t y , t z ) ≠ (0, 0, 0). The fourth number φ (0 ≤ φ ≤ π /2) gives the angular radius, in radians, of the sight field of the telescope. Let us defie that θ i,j is the angle between the direction of the i -th star and the center direction of the j -th telescope and φ j is the angular radius of the sight field of the j -th telescope. The i -th star is observable through the j -th telescope if and only if θ i,j is less than . You may assume that | θ i,j - φ j | > 0.00000001 for all pairs of i and j . Figure 1: Direction and angular radius of a telescope The end of the input is indicated with a line containing a single zero. Output For each dataset, one line containing an integer meaning the number of stars observable through the telescopes should be output. No other characters should be contained in the output. Note that stars that can be seen through more than one telescope should not be counted twice or more. Sample Input 3 100 0 500 -500.243 -200.1 -300.5 0 300 200 2 1 1 1 0.65 -1 0 0 1.57 3 1 0 0 0 ...(truncated)
[ { "submission_id": "aoj_1266_4858020", "code_snippet": "#include <iostream> \n#include<vector>\n#include<algorithm>\n#include<map>\n#include<string>\n#include<iomanip>\n#include<set>\n#include<queue>\n#include<deque>\n#include<sstream>\n#include<cmath>\n#include<bitset>\nusing namespace std;\n#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)\n#define req(i,n) for(int i = 1;i <= n; i++)\n#define rrep(i,n) for(int i = n-1;i >= 0;i--)\n#define ALL(obj) begin(obj), end(obj)\n#define RALL(a) rbegin(a),rend(a)\ntypedef long long int ll;\ntypedef long double ld;\ntemplate<typename A, size_t N, typename T>\nvoid Fill(A(&array)[N], const T& val) {\n std::fill((T*)array, (T*)(array + N), val);\n}\nconst int inf = 1 << 31 - 1;\nconst ll MOD = 1000000007;\ntemplate<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }\ntemplate<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }\nint main(void) {\n int n, m;\n while (cin >> n, n!= 0) {\n vector<ld> a(n), b(n), c(n);\n rep(i, n) cin >> a[i] >> b[i] >> c[i];\n cin >> m; vector<ld> x(m), y(m), z(m), t(m);\n rep(i, m) cin >> x[i] >> y[i] >> z[i] >> t[i];\n int ans = 0;\n rep(i, n) {\n rep(j, m) {\n ld co = a[i] * x[j] + (b[i] * y[j]) + (c[i] * z[j]);\n co /= sqrt(a[i] * a[i] + b[i] * b[i] + c[i] * c[i]) * sqrt(x[j] * x[j] + y[j] * y[j] + z[j] * z[j]);\n ld th = acos(co);\n if (th > acos(-1)) th -= acos(-1);\n if (th <= t[j]) {\n ans++; break;\n }\n }\n }cout << ans << endl;\n }\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3228, "score_of_the_acc": -0.658, "final_rank": 7 }, { "submission_id": "aoj_1266_4772200", "code_snippet": "#define _GLIBCXX_DEBUG\n#include <bits/stdc++.h>\nusing namespace std;\ntypedef long long ll;\n#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)\n#define FOR(i,a,n) for(ll i=a;i<(ll)(n);i++)\n\nstruct point {\n double x;\n double y;\n double z;\n};\n\nint main(){\n int n;\n while(cin>>n, n){\n point s[n];\n for(int i=0; i<n; i++) cin>>s[i].x>>s[i].y>>s[i].z;\n\n int m; cin>>m;\n vector<bool> see(n, false);\n for(int i=0; i<m; i++){\n point t;\n double rad;\n cin>>t.x>>t.y>>t.z>>rad;\n int ans = 0;\n for(int j=0; j<n; j++){\n if(see[j])continue;\n double angle = acos((s[j].x*t.x+s[j].y*t.y+s[j].z*t.z)\n /sqrt(s[j].x*s[j].x+s[j].y*s[j].y+s[j].z*s[j].z)\n /sqrt(t.x*t.x+t.y*t.y+t.z*t.z));\n if(angle < rad+1e-8) see[j]=true;\n }\n }\n int ans = 0;\n for(bool s : see) if(s)ans++;\n cout << ans << endl;\n }\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3384, "score_of_the_acc": -0.7115, "final_rank": 13 }, { "submission_id": "aoj_1266_4745504", "code_snippet": "#include<cstdio>\n#include<algorithm>\n#include<cmath>\n#include<iostream>\nusing namespace std;\n\nint n,m;\ndouble x[1000],y[1000],z[1000];\ndouble tx,ty,tz,ar;\n\ndouble dot(double ax,double ay,double az,\n double bx,double by,double bz){\n return ax*bx+ay*by+az*bz;\n}\n\nint calc(double sx,double sy,double sz){\n double S=sqrt(sx*sx+sy*sy+sz*sz);\n double T=sqrt(tx*tx+ty*ty+tz*tz);\n double Cos = dot(tx,ty,tz,sx,sy,sz)/T/S;\n double ti=acos(Cos);\n\n if(ti < ar + 0.00000001 ){\n return 1;\n }else{\n return 0;\n }\n}\nbool flg[1000];\nint main(){\n while(cin>>n&&n){\n for(int i=0;i<n;i++){\n flg[i]=false;\n cin>>x[i]>>y[i]>>z[i];\n }\n cin>>m;\n for(int i=0;i<m;i++){\n cin>>tx>>ty>>tz>>ar;\n for(int j=0;j<n;j++){\n if(calc(x[j],y[j],z[j]))flg[j]=true;\n }\n }\n int ans=0;\n for(int i=0;i<n;i++)ans+=flg[i];\n cout<<ans<<endl;\n }\n return 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3496, "score_of_the_acc": -0.75, "final_rank": 16 }, { "submission_id": "aoj_1266_3978167", "code_snippet": "#include <cstdio>\n#include <cstdlib>\n#include <cmath>\n#include <cstring>\n\n#include <iostream>\n#include <complex>\n#include <string>\n#include <algorithm>\n#include <numeric>\n#include <vector>\n#include <queue>\n#include <stack>\n#include <map>\n#include <set>\n#include <unordered_map>\n#include <unordered_set>\n#include <bitset>\n\n#include <functional>\n#include <cassert>\n\ntypedef long long ll;\nusing namespace std;\n\n#ifndef LOCAL\n#define debug(x) ;\n#else\n#define debug(x) cerr << __LINE__ << \" : \" << #x << \" = \" << (x) << endl;\n\ntemplate <typename T1, typename T2>\nostream &operator<<(ostream &out, const pair<T1, T2> &p) {\n out << \"{\" << p.first << \", \" << p.second << \"}\";\n return out;\n}\n\ntemplate <typename T>\nostream &operator<<(ostream &out, const vector<T> &v) {\n out << '{';\n for (const T &item : v) out << item << \", \";\n out << \"\\b\\b}\";\n return out;\n}\n#endif\n\n#define mod 1000000007 //1e9+7(prime number)\n#define INF 1000000000 //1e9\n#define LLINF 2000000000000000000LL //2e18\n#define SIZE 200010\n\n/* 3次元幾何 */\n\ntypedef double P_type;\ntypedef double G_real; //実数の戻り値(float or double or long double)\nconst G_real P_eps = 1e-8; //整数の時はゼロ\n\nstruct P3{\n P_type x, y, z;\n P3(P_type x = 0, P_type y = 0, P_type z = 0): x(x), y(y), z(z) {}\n\n P3 operator-() const {\n return P3(-x, -y, -z);\n }\n\n P3 operator+(const P3 &B) const {\n return P3(x + B.x, y + B.y, z + B.z);\n }\n\n P3 operator-(const P3 &B) const {\n return P3(x - B.x, y - B.y, z - B.z);\n }\n\n P3 operator*(P_type a) const {\n return P3(x * a, y * a, z * a);\n }\n\n P3 operator/(P_type a) const {\n return P3(x / a, y / a, z / a);\n }\n\n P3& operator+=(const P3 &B) {\n x += B.x; y += B.y; z += B.z;\n return *this;\n }\n\n P3& operator-=(const P3 &B) {\n x -= B.x; y -= B.y; z -= B.z;\n return *this;\n }\n\n P3& operator*=(P_type a) {\n x *= a; y *= a; z *= a;\n return *this;\n }\n\n P3& operator/=(P_type a) {\n x /= a; y /= a; z /= a;\n return *this;\n }\n\n bool operator<(const P3 &b){\n if (abs(x - b. x) > P_eps) return x + P_eps < b.x;\n if (abs(y - b. y) > P_eps) return y + P_eps < b.y;\n return z + P_eps < b.z;\n }\n};\n\n/* 3点a, b, cを通る平面 */\n// TODO: 1直線上に並んでいる場合をError化\nstruct Plane {\n P3 a, b, c;\n\n Plane(P3 a, P3 b, P3 c): a(a), b(b), c(c) {}\n};\n\nP_type dot(P3 a, P3 b) {\n return a.x * b.x + a.y * b.y + a.z * b.z;\n}\n\nP3 cross(P3 a, P3 b) {\n P_type x = a.y * b.z - a.z * b.y;\n P_type y = a.z * b.x - a.x * b.z;\n P_type z = a.x * b.y - a.y * b.x;\n return P3(x, y, z);\n}\n\nP_type norm(P3 a) {\n return dot(a, a);\n}\n\nG_real abs(P3 a) {\n return sqrt(dot(a, a));\n}\n\n/* ベクトルa, bの成す角[0, pi] */\nG_real arg(P3 a, P3 b) {\n return acos(dot(a, b) / abs(a) / abs(b));\n}\n\n/* 直線abと点cの距離 */\nG_real distanceLP(P3 a, P3 b, P3 c) {\n return abs(cross(b-a, c-a)) / abs(b-a);\n}\n\n/* 線分abと点cの距離 */\nG_real distanceSP(P3 a, P3 b, P3 c) {\n if (dot(b-a, c-a) <= P_eps) return abs(c-a);\n if (dot(a-b, c-b) <= P_eps) return abs(c-b);\n return abs(cross(b-a, c-a)) / abs(b-a);\n}\n\n/* 射影(直線abとpからの垂線との交点) */\nP3 getProject(P3 a, P3 b, P3 p) {\n P3 base = b - a;\n return a + base * dot(p - a, base) / norm(base);\n}\n\n/* 直線Aと直線Bの距離 */\nG_real distanceLL(P3 a1, P3 a2, P3 b1, P3 b2) {\n P3 n = cross(a2 - a1, b2 - b1);\n if (abs(n) <= P_eps) return distanceLP(a1, a2, b1); //平行\n return abs(dot(n, b1 - a1)) / abs(n);\n}\n\n/* 線分Aと線分Bの距離(未検証) */\nG_real distanceSS(P3 a1, P3 a2, P3 b1, P3 b2) {\n //ねじれの位置\n P3 n = cross(a2 - a1, b2 - b1);\n if (abs(n) > P_eps) { // 平行ではない\n G_real d = distanceLL(a1, a2, b1, b2);\n P3 p = n * d / abs(n);\n P3 c1 = b1 + p, c2 = b2 + p;\n\n P3 va = a2 - a1, vc = c2 - c1;\n bool f1 = dot(cross(va, c1 - a1), cross(va, c2 - a1)) < -P_eps;\n bool f2 = dot(cross(vc, a1 - c1), cross(vc, a2 - c1)) < -P_eps;\n\n if (f1 && f2) return d;\n }\n\n //その他\n return min({distanceSP(a1, a2, b1), distanceSP(a1, a2, b2),\n distanceSP(b1, b2, a1), distanceSP(b1, b2, a2)});\n}\n\n/* 点と平面Plの距離 */\nG_real distancePPl(P3 p, P3 pl1, P3 pl2, P3 pl3) {\n P3 n = cross(pl2 - pl1, pl3 - pl1);\n return abs(dot(p - pl1, n)) / abs(n);\n}\n\n/* 直線Aと、平面Plの交点 */\n// verified: AOJ0115\nvector<P3> getCrosspointLPl(P3 a1, P3 a2, P3 pl1, P3 pl2, P3 pl3) {\n P3 n = cross(pl2 - pl1, pl3 - pl1); //平面の法線ベクトル\n if (abs(dot(n, a2 - a1)) <= P_eps)\n return {}; //平面と直線が平行\n\n G_real s = dot(pl1 - a1, n), t = dot(a2 - pl1, n);\n P3 c = a1 + (a2 - a1) * (s / (s + t));\n return {c};\n}\n\n/* 線分Aと、平面Plの交点 */\n// verified: AOJ0115\nvector<P3> getCrosspointSPl(P3 a1, P3 a2, P3 pl1, P3 pl2, P3 pl3) {\n auto cps = getCrosspointLPl(a1, a2, pl1, pl2, pl3);\n if (cps.empty()) return {};\n if (norm(a1 - cps[0]) > norm(a1 - a2)) return {};\n if (norm(a2 - cps[0]) > norm(a1 - a2)) return {};\n return cps;\n}\n\nP3 scan() {\n double x, y, z;\n cin >> x >> y >> z;\n return P3(x, y, z);\n}\n\nint main(){\n\n while(1) {\n int N, M;\n P3 s[500], t[50];\n double phi[50];\n\n cin >> N;\n\n if (!N) break;\n\n for (int i=0; i<N; i++)\n s[i] = scan();\n\n cin >> M;\n\n for (int i=0; i<M; i++) {\n t[i] = scan();\n cin >> phi[i];\n }\n\n int ans = 0;\n\n for (int i=0; i<N; i++) {\n bool ok = false;\n for (int j=0; j<M; j++) {\n ok |= arg(s[i], t[j]) < phi[j] + P_eps;\n }\n ans += ok;\n }\n\n cout << ans << endl;\n }\n\n return 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3352, "score_of_the_acc": -0.7005, "final_rank": 10 }, { "submission_id": "aoj_1266_3917392", "code_snippet": "#include <iostream>\n#include <vector>\n#include <cmath>\n\nusing namespace std;\n\nstruct Point {\n\tdouble x, y, z;\n\n\tfriend istream& operator>>(istream& is, Point& p) {\n\t\tis >> p.x >> p.y >> p.z;\n\t\treturn is;\n\t}\n\n\tdouble abs() { return sqrt(x * x + y * y + z * z); }\n};\n\ndouble dot(Point a, Point b) { \n\treturn a.x * b.x + a.y * b.y + a.z * b.z;\n}\n\nint main() {\n\tint n, m;\n\twhile (cin >> n, n) {\n\t\tvector<Point> s(n);\n\t\tfor (int i = 0; i < n; ++i) cin >> s[i];\n\t\tcin >> m;\n\t\tvector<Point> t1(m);\n\t\tvector<double> t2(m);\n\t\tfor (int i = 0; i < m; ++i) cin >> t1[i] >> t2[i];\n\t\tint ans = 0;\n\t\tfor (int i = 0; i < n; ++i) {\n\t\t\tfor (int j = 0; j < m; ++j) {\n\t\t\t\tif (acos(dot(s[i], t1[j]) / s[i].abs() / t1[j].abs()) <= t2[j]) {\n\t\t\t\t\tans++;\n\t\t\t\t\tbreak;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\tcout << ans << endl;\n\t}\n\n\treturn 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3376, "score_of_the_acc": -0.7088, "final_rank": 12 }, { "submission_id": "aoj_1266_3911953", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\nint main(){\n while(1){\n int n; cin>>n;\n\n if(n==0) break;\n \n vector<double> sx(n),sy(n),sz(n);\n \n for(int i=0; i<n; i++){\n cin>>sx[i]>>sy[i]>>sz[i];\n }\n \n int m; cin>>m;\n \n vector<double> tx(m),ty(m),tz(m),kaku(m);\n \n for(int i=0; i<m; i++){\n cin>>tx[i]>>ty[i]>>tz[i]>>kaku[i]; \n }\n \n int count=0;\n \n set<int> s;\n \n for(int i=0; i<n; i++){\n for(int j=0; j<m; j++){\n if(s.count(i))continue;\n \n double seki = tx[j]*sx[i] + ty[j]*sy[i]+ tz[j]*sz[i];\n \n double pLong = sqrt(sx[i]*sx[i] + sy[i]*sy[i] + sz[i] * sz[i]);\n \n double kLong = sqrt(tx[j]*tx[j] + ty[j]*ty[j] + tz[j] * tz[j]);\n\n double theta = acos(seki / (pLong * kLong));\n \n if(theta <=kaku[j]){\n count++;\n s.insert(i);\n }\n }\n }\n cout<<count<<endl;\n }\n \n return 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3496, "score_of_the_acc": -0.75, "final_rank": 16 }, { "submission_id": "aoj_1266_3911836", "code_snippet": "#include <bits/stdc++.h>\n\nusing namespace std;\n\n#define PI acos(-1)\n\nstruct xyz{\n double x,y,z;\n};\n\ndouble abs(xyz a) {\n return sqrt(a.x*a.x+a.y*a.y+a.z*a.z);\n}\n\ndouble product(xyz a,xyz b) {\n return a.x*b.x+a.y*b.y+a.z*b.z;\n}\n\nint main() {\n int n,m;\n while(cin >> n,n) {\n vector<xyz> s(n);\n for(auto&a:s) cin >> a.x>>a.y>>a.z;\n cin >> m;\n vector<pair<xyz,double> > t(m);\n for(auto &a:t)\n cin >> a.first.x>>a.first.y>>a.first.z >> a.second;\n int ans=0;\n for(int i=0;i<n;++i){\n for(int j=0;j<m;++j){\n double co=product(s[i],t[j].first);\n co/=abs(s[i])*abs(t[j].first);\n double theta=acos(co);\n if(theta>PI) theta-=PI;\n if(theta<=t[j].second) {\n ans++;\n break;\n }\n }\n }\n cout << ans << endl;\n }\n return 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3424, "score_of_the_acc": -0.7253, "final_rank": 15 }, { "submission_id": "aoj_1266_3911815", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\ntypedef unsigned long long int ull;\ntypedef long long int ll;\ntypedef pair<ll,ll> pll;\ntypedef long double D;\ntypedef complex<D> P;\n#define F first\n#define S second\nconst ll MOD=1000000007;\n//const ll MOD=998244353;\n\ntemplate<typename T,typename U>istream & operator >> (istream &i,pair<T,U> &A){i>>A.F>>A.S; return i;}\ntemplate<typename T>istream & operator >> (istream &i,vector<T> &A){for(auto &I:A){i>>I;} return i;}\ntemplate<typename T,typename U>ostream & operator << (ostream &o,const pair<T,U> &A){o<<A.F<<\" \"<<A.S; return o;}\ntemplate<typename T>ostream & operator << (ostream &o,const vector<T> &A){ll i=A.size(); for(auto &I:A){o<<I<<(--i?\" \":\"\");} return o;}\n\ntypedef pair<D,D> pd;\n\nD dot(D a,D b,D c,D ad,D bd,D cd){return a*ad+b*bd+c*cd;}\n\nD abs(D a,D b,D c){return sqrt(a*a+b*b+c*c);}\n\nconst D EPS=1e-9;\n\nbool solve(){\n ll N;\n cin>>N;\n if(N==0){return false;}\n vector<pair<pd,D>> A(N);\n cin>>A;\n ll M;\n cin>>M;\n vector<pair<pd,pd>> B(M);\n cin>>B;\n for(auto &I:A){\n D uku=abs(I.F.F,I.F.S,I.S);\n I.F.F/=uku;\n I.F.S/=uku;\n I.S/=uku;\n }\n for(auto &I:B){\n D uku=abs(I.F.F,I.F.S,I.S.F);\n I.F.F/=uku;\n I.F.S/=uku;\n I.S.F/=uku;\n }\n ll cnt=0;\n for(auto &I:A){\n bool jd=false;\n for(auto &J:B){\n if(dot(I.F.F,I.F.S,I.S,J.F.F,J.F.S,J.S.F)>cos(J.S.S)-EPS){jd=true; break;}\n }\n if(jd){cnt++;}\n }\n cout<<cnt<<endl;\n\n return true;\n}\n\nint main(){\n while(solve());\n \n\n return 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3348, "score_of_the_acc": -0.6992, "final_rank": 9 }, { "submission_id": "aoj_1266_3911733", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\n// #define double long double\n\nconst double EPS = 1e-9;\n\nstruct P {\n double x, y, z;\n P() {}\n P(double x, double y, double z): x(x), y(y), z(z) {}\n};\n\nint N, M;\nvector<P> stars, scopes;\nvector<double> angs;\n\ndouble dist(P a, P b) {\n return sqrt(pow(a.x-b.x, 2)+pow(a.y-b.y, 2)+pow(a.z-b.z, 2));\n}\n\nbool contain(int a, int b) {\n double l = 0, r = 1000000;\n int cnt = 100;\n P A = stars[a];\n while ( cnt-- ) {\n double mid = (l+r)/2;\n P B = scopes[b];\n B.x *= mid; B.y *= mid; B.z *= mid;\n double dist1 = dist(A, B);\n B = scopes[b];\n B.x *= (mid+EPS); B.y *= (mid+EPS); B.z *= (mid+EPS);\n double dist2 = dist(A, B);\n if ( dist1 > dist2 ) l = mid;\n else r = mid; \n }\n\n P B = scopes[b];\n B.x *= l; B.y *= l; B.z *= l;\n double dist1 = dist(P(0, 0, 0), B);\n double dist2 = dist(A, B);\n if ( dist2 < tan(angs[b])*dist1 ) return true;\n else return false;\n}\n\nint main() {\n while ( cin >> N, N ) {\n stars.resize(N);\n for ( int i = 0; i < N; i++ ) {\n cin >> stars[i].x >> stars[i].y >> stars[i].z;\n }\n \n cin >> M;\n scopes.resize(M);\n angs.resize(M);\n for ( int i = 0; i < M; i++ ) {\n cin >> scopes[i].x >> scopes[i].y >> scopes[i].z;\n cin >> angs[i]; \n }\n\n int ans = 0;\n for ( int i = 0; i < N; i++ ) {\n for ( int j = 0; j < M; j++ ) {\n\tif ( contain(i, j) ) {\n\t ans++;\n\t break;\n\t}\n }\n }\n\n cout << ans << endl;\n } \n return 0;\n}", "accuracy": 1, "time_ms": 120, "memory_kb": 3480, "score_of_the_acc": -1.7445, "final_rank": 20 }, { "submission_id": "aoj_1266_3911674", "code_snippet": "#include<bits/stdc++.h>\n#define ll long long\n#define rep(i, n) for(int i=0;i<(n);++i)\n#define per(i, n) for(int i=(n)-1;i>=0;--i)\n#define repa(i, n) for(int i=1;i<(n);++i)\n#define foreach(i, n) for(auto &i:(n))\n#define pii pair<int, int>\n#define pll pair<long long, long long>\n#define all(x) (x).begin(), (x).end()\n#define bit(x) (1ll << (x))\nconst ll MOD = (ll)1e9+7;\nconst int INF = (ll)1e9+7;\nconst ll INFLL = (ll)1e18;\nusing namespace std;\ntemplate<class t>\nusing vvector = vector<vector<t>>;\ntemplate<class t>\nusing vvvector = vector<vector<vector<t>>>;\ntemplate<class t>\nusing priority_queuer = priority_queue<t, vector<t>, greater<t>>;\ntemplate<class t, class u> bool chmax(t &a, u b){if(a<b){a=b;return true;}return false;}\ntemplate<class t, class u> bool chmin(t &a, u b){if(a>b){a=b;return true;}return false;}\n\nll modpow(ll x, ll b){\n\tll res = 1;\n\twhile(b){\n\t\tif(b&1)res = res * x % MOD;\n\t\tx = x * x % MOD;\n\t\tb>>=1;\n\t}\n\treturn res;\n}\n\nll modinv(ll x){\n\treturn modpow(x, MOD-2);\n}\n\nbool was_output = false;\ntemplate<class t>\nvoid output(t x){\n\tif(was_output)cout << \" \";\n\twas_output = true;\n\tcout << x;\n}\n\nvoid outendl(){\n\twas_output = false;\n\tcout << endl;\n}\n\nstruct vector3{\n\tdouble x;\n\tdouble y;\n\tdouble z;\n\tvector3(double a, double b, double c):x(a), y(b), z(c){}\n\tvector3(){}\n\tvector3 operator+(vector3 o){return vector3(x+o.x, y+o.y, z+o.z);}\n\tvector3 operator-(vector3 o){return vector3(x-o.x, y-o.y, z-o.z);}\n\tvector3 operator*(double o){return vector3(x*o, y*o, z*o);}\n\tvector3 operator/(double o){return vector3(x/o, y/o, z/o);}\n\n\tdouble magnitude(){return sqrt(x*x+y*y+z*z);}\n};\n\ndouble dot(vector3 a, vector3 b){return a.x * b.x + a.y * b.y + a.z * b.z;}\n\ndouble getangle(vector3 a, vector3 b){\n\ta = a / a.magnitude();\n\tb = b / b.magnitude();\n\treturn acos(dot(a, b));\n}\n\nint main(){\n\tint n, m;\n\twhile(cin>>n&&n){\n\t\tvector<vector3> stars(n);\n\t\tforeach(i, stars){\n\t\t\tdouble x, y, z;\n\t\t\tcin >> x >> y >> z;\n\t\t\ti = vector3(x, y, z);\n\t\t}\n\t\tcin >> m;\n\t\tvector<vector3> scopes(m);\n\t\tvector<double> angle(m);\n\t\trep(i, m){\n\t\t\tdouble a, b, c, d;\n\t\t\tcin >> a >> b >> c >> d;\n\t\t\tscopes[i] = vector3(a, b, c);\n\t\t\tangle[i] = d;\n\t\t}\n\t\tint ans = 0;\n\t\tforeach(i, stars){\n\t\t\trep(j, m){\n\t\t\t\tdouble nangle = getangle(i, scopes[j]);\n\t\t\t\tif(nangle-angle[j]<1e-8){\n\t\t\t\t\t++ans;\n\t\t\t\t\tbreak;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\n\t\tcout << ans << endl;\n\t}\n\treturn 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3372, "score_of_the_acc": -0.7074, "final_rank": 11 }, { "submission_id": "aoj_1266_3036264", "code_snippet": "#include <iostream>\n#include <iomanip>\n#include <vector>\n#include <algorithm>\n#include <cmath>\nusing namespace std;\nconst double EPS = 1e-10;\nconst double INF = 1e12;\n \nstruct Point{\n double x,y,z;\n Point(double x, double y, double z):x(x), y(y), z(z){}\n Point(){}\n Point &operator +=(const Point &a){ x+=a.x; y+=a.y; z+=a.z; return *this; }\n Point &operator -=(const Point &a){ x-=a.x; y-=a.y; z-=a.z; return *this; }\n Point &operator *=(const double &a){ x*=a; y*=a; z*=a; return *this; }\n Point operator +(const Point &a) const{ return Point(x+a.x, y+a.y, z+a.z); }\n Point operator -(const Point &a) const{ return Point(x-a.x, y-a.y, z-a.z); }\n Point operator *(const double &a) const{ return Point(a*x, a*y, a*z); }\n Point operator -() { return Point(-x, -y, -z); }\n};\n\ndouble dot(const Point &a, const Point &b){\n return a.x*b.x +a.y*b.y +a.z*b.z;\n}\nPoint cross(const Point &a, const Point &b){\n return Point(a.y*b.z -a.z*b.y, a.z*b.x -a.x*b.z, a.x*b.y -a.y*b.x);\n}\ndouble abs(const Point &a){\n return sqrt(dot(a, a));\n}\nPoint unit(const Point &a){\n return a *(1/abs(a));\n}\nbool operator ==(const Point &a, const Point &b){\n return abs(a-b) < EPS;\n}\n \nint main(){\n while(1){\n int n;\n cin >> n;\n if(n==0) break;\n\n vector<Point> p(n);\n for(int i=0; i<n; i++){\n cin >> p[i].x >> p[i].y >> p[i].z;\n }\n vector<bool> invalid(n, false);\n for(int i=0; i<n; i++){\n if(invalid[i]) continue;\n for(int j=i+1; j<n; j++){\n if(unit(p[i]) == unit(p[j])){\n invalid[j] = true;\n }\n }\n }\n\n int m;\n cin >> m;\n int ans = 0;\n for(int i=0; i<m; i++){\n Point dir;\n double theta;\n cin >> dir.x >> dir.y >> dir.z >> theta;\n double costheta = cos(theta);\n for(int j=0; j<n; j++){\n if(invalid[j]) continue;\n if(dot(p[j], dir)/(abs(p[j])*abs(dir)) +EPS > costheta){\n ans++;\n invalid[j] = true;\n }\n }\n }\n cout << ans << endl;\n }\n return 0;\n}", "accuracy": 1, "time_ms": 20, "memory_kb": 3332, "score_of_the_acc": -0.7846, "final_rank": 18 }, { "submission_id": "aoj_1266_2779860", "code_snippet": "#include <iostream>\n#include <cstring>\n#include <cstdlib>\n#include <cmath>\n#include <algorithm>\n#include <functional>\n#include <vector>\n#include <list>\n#include <queue>\n#include <deque>\n#include <stack>\n#include <map>\n#include <set>\n#include <bitset>\n#include <tuple>\n#include <cassert>\n#include <exception>\n#include <iomanip>\nusing namespace std;\ntypedef long long ll;\ntypedef unsigned long long ull;\ntypedef pair<ll,ll> P;\ntypedef vector<int> vi;\ntypedef vector<ll> vll;\ntypedef vector<char> vc;\ntypedef vector<string> vs;\ntypedef vector<P> vp;\n#define rep(i,a,n) for(ll i = (a);i < (n);i++)\n#define per(i,a,n) for(ll i = (a);i > (n);i--)\n#define lep(i,a,n) for(ll i = (a);i <= (n);i++)\n#define pel(i,a,n) for(ll i = (a);i >= (n);i--)\n#define clr(a,b) memset((a),(b),sizeof(a))\n#define pb push_back\n#define mp make_pair\n#define all(c) (c).begin(),(c).end()\n#define sz size()\n#define print(X) cout << (X) << endl\nstatic const int INF = 1e+9+7;\nll n,m,l;\nstring s,t;\nint d[200010],dp[1010][1010];\ndouble w[1000],v[1000];\ndouble box[200010];\nchar field[200][200];\n\ndouble dot(double a,double b,double c, double x,double y,double z){\n return a * x + b * y + c * z;\n}\n\nint main(){\n double x[500],y[500],z[500];\n while(1){\n cin >> n;\n if(!n)break;\n ll ans = 0;\n clr(d,0);\n rep(i,0,n)cin >> x[i] >> y[i] >> z[i];\n cin >> m;\n rep(i,0,m){\n double a,b,c,r;\n cin >> a >> b >> c >> r;\n double abs1 = sqrt(dot(a,b,c, a,b,c));\n rep(j,0,n){\n double abs2 = sqrt(dot(x[j],y[j],z[j], x[j],y[j],z[j]));\n double Cos = dot(a,b,c, x[j],y[j],z[j]) / abs1 / abs2;\n double q = acos(Cos);\n if(q < r + 0.00000001)d[j]++;\n }\n }\n rep(i,0,n)if(d[i])ans++;\n print(ans);\n }\n return 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 4224, "score_of_the_acc": -1, "final_rank": 19 }, { "submission_id": "aoj_1266_2062453", "code_snippet": "#include \"bits/stdc++.h\"\n#include<unordered_map>\n#include<unordered_set>\n#pragma warning(disable:4996)\nusing namespace std;\nusing ld = long double;\nconst ld eps = 1e-9;\n\n//// < \"d:\\d_download\\visual studio 2015\\projects\\programing_contest_c++\\debug\\a.txt\" > \"d:\\d_download\\visual studio 2015\\projects\\programing_contest_c++\\debug\\b.txt\"\n\n\nint main() {\n\twhile (1) {\n\t\tint N; cin >> N;\n\t\tif (!N)break;\n\t\tvector<tuple<ld, ld, ld>>stars;\n\t\tfor (int i = 0; i < N; ++i) {\n\t\t\tld x, y, z; cin >> x >> y >> z;\n\t\t\tld dis = sqrt(x*x + y*y + z*z);\n\t\t\tx /= dis; y /= dis; z /= dis;\n\t\t\tstars.push_back(make_tuple(x, y, z));\n\t\t}\n\t\tint T; cin >> T;\n\t\tvector<tuple<ld, ld, ld, ld>>eyes;\n\t\tfor (int i = 0; i < T; ++i) {\n\t\t\tld x, y, z, theta; cin >> x >> y >> z >> theta;\n\t\t\tld dis = sqrt(x*x + y*y + z*z);\n\t\t\tx /= dis; y /= dis; z /= dis;\n\t\t\teyes.push_back(make_tuple(x, y, z, theta));\n\t\t}\n\t\tvector<int>cansee(N);\n\t\tfor (int i = 0; i < N; ++i) {\n\t\t\tld sx, sy, sz;\n\t\t\ttie(sx, sy, sz) = stars[i];\n\n\t\t\tfor (int j = 0; j < T; ++j) {\n\t\t\t\tld ex, ey, ez, theta;\n\t\t\t\ttie(ex, ey, ez, theta) = eyes[j];\n\t\t\t\tld num = sx*ex + sy*ey + sz*ez;\n\t\t\t\tld num2 = cos(theta);\n\t\t\t\tif (cos(theta)< sx*ex + sy*ey + sz*ez)cansee[i] = true;\n\t\t\t}\n\n\t\t}\n\t\tint ans = count(cansee.begin(), cansee.end(), 1);\n\t\tcout << ans << endl;\n\t}\n\treturn 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3340, "score_of_the_acc": -0.6964, "final_rank": 8 }, { "submission_id": "aoj_1266_1881918", "code_snippet": "#include <iostream>\n#include <cmath>\n#include <vector>\n#include <set>\n\nusing namespace std;\n\n#define EPS (1e-8)\n\nstruct Point {\n double x, y, z;\n Point(){}\n Point(double x, double y, double z) :\n x(x), y(y), z(z) {}\n};\n\ndouble dot(const Point &a, const Point &b)\n{\n return a.x*b.x + a.y*b.y + a.z*b.z;\n}\n\ndouble norm(const Point &p)\n{\n return dot(p, p);\n}\n\ndouble abs(const Point &p)\n{\n return sqrt(norm(p));\n}\n\nbool can_watch(const Point &a, const Point &b, double the)\n{\n double t = acos(dot(a, b) / abs(a) / abs(b));\n return (t < the + EPS);\n}\n\nint main()\n{\n int n, m;\n while (cin >> n, n) {\n vector<Point> s(n);\n for (int i = 0; i < n; i++) {\n cin >> s[i].x >> s[i].y >> s[i].z;\n }\n Point p;\n double the;\n set<int> stars;\n cin >> m; \n for (int i = 0; i < m; i++) {\n cin >> p.x >> p.y >> p.z >> the;\n for (int j = 0; j < n; j++) {\n if (can_watch(s[j], p, the)) {\n stars.insert(j);\n }\n }\n }\n cout << stars.size() << endl;\n }\n return 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3384, "score_of_the_acc": -0.7115, "final_rank": 13 }, { "submission_id": "aoj_1266_1851540", "code_snippet": "#include<bits/stdc++.h>\n#define eps (1e-11)\nusing namespace std;\n\nclass Point3d{\npublic:\n double x,y,z;\n Point3d(double a=0,double b=0,double c=0):x(a),y(b),z(c){}\n};\n\ndouble norm(Point3d a,Point3d b){\n return pow((a.x-b.x),2)+pow((a.y-b.y),2)+pow((a.z-b.z),2);\n}\n\ndouble abs(Point3d a,Point3d b){ \n return sqrt(norm(a,b));\n}\n\nint main()\n{\n int n,m;\n double a,b,c,d;\n \n while(1){\n cin>>n;\n if(n==0)break;\n\n vector<Point3d> v;\n bool st[501]={};\n\n for(int i=0;i<n;i++){\n cin>>a>>b>>c;\n v.push_back(Point3d(a,b,c));\n }\n cin>>m;\n\n Point3d origin(0.0,0.0,0.0);\n for(int i=0;i<m;i++){\n cin>>a>>b>>c>>d;\n Point3d p3(a,b,c);\n double x,y,z,r;\n for(int j=0;j<n;j++){\n x=abs(v[j],origin);\n y=abs(p3,origin);\n z=abs(v[j],p3);\n \tr=acos((x*x+y*y-z*z)/(x*y*2));\n if((r-d)<eps)st[j]=true;\n }\n }\n int ans=0;\n for(int i=0;i<n;i++)if(st[i])ans++;\n cout<<ans<<endl;\n }\n return 0;\n}", "accuracy": 1, "time_ms": 20, "memory_kb": 1332, "score_of_the_acc": -0.0978, "final_rank": 4 }, { "submission_id": "aoj_1266_1701907", "code_snippet": "#include <bits/stdc++.h>\n#define N 500\n#define M 50\nusing namespace std;\n\nint main(){\n int n,m;\n double sx[N],sy[N],sz[N],tx[M],ty[M],tz[M],r[M];\n bool ans[N];\n while(1){\n cin>>n;\n if(!n) break;\n for(int i=0;i<n;i++) cin>>sx[i]>>sy[i]>>sz[i],ans[i]=false;\n cin>>m;\n for(int i=0;i<m;i++) cin>>tx[i]>>ty[i]>>tz[i]>>r[i];\n for(int i=0;i<m;i++){\n for(int j=0;j<n;j++){\n\tdouble d1=sqrt(sx[j]*sx[j]+sy[j]*sy[j]+sz[j]*sz[j]);\n\tdouble X=sqrt(tx[i]*tx[i]+ty[i]*ty[i]+tz[i]*tz[i]);\n\tdouble t=(tx[i]*sx[j]+ty[i]*sy[j]+tz[i]*sz[j])/(X*d1);\n\tif(acos(t)<=r[i]) ans[j]=true;\n }\n }\n int cnt=0;\n for(int i=0;i<n;i++)\n if(ans[i]) cnt++;\n cout<<cnt<<endl;\n }\n return 0;\n}", "accuracy": 1, "time_ms": 20, "memory_kb": 1312, "score_of_the_acc": -0.0909, "final_rank": 1 }, { "submission_id": "aoj_1266_1701906", "code_snippet": "#include <bits/stdc++.h>\n#define N 500\n#define M 50\nusing namespace std;\n\nint main(){\n int n,m;\n double sx[N],sy[N],sz[N],tx[M],ty[M],tz[M],r[M];\n bool ans[N];\n while(1){\n cin>>n;\n if(!n) break;\n for(int i=0;i<n;i++) cin>>sx[i]>>sy[i]>>sz[i],ans[i]=false;\n cin>>m;\n for(int i=0;i<m;i++) cin>>tx[i]>>ty[i]>>tz[i]>>r[i];\n for(int i=0;i<m;i++){\n for(int j=0;j<n;j++){\n\tdouble d1=sqrt(sx[j]*sx[j]+sy[j]*sy[j]+sz[j]*sz[j]);\n\tdouble X=sqrt(tx[i]*tx[i]+ty[i]*ty[i]+tz[i]*tz[i]);\n\tdouble Y=X*tan(r[i]);\n\tdouble t=(tx[i]*sx[j]+ty[i]*sy[j]+tz[i]*sz[j])/(X*d1);\n\tif(/*d1<=Y&&*/acos(t)<=r[i]) ans[j]=true;\n }\n }\n int cnt=0;\n for(int i=0;i<n;i++)\n if(ans[i]) cnt++;\n cout<<cnt<<endl;\n }\n return 0;\n}", "accuracy": 1, "time_ms": 20, "memory_kb": 1312, "score_of_the_acc": -0.0909, "final_rank": 1 }, { "submission_id": "aoj_1266_1701843", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\n\nclass Point3d{\npublic:\n double x,y,z;\n Point3d(double a=0,double b=0,double c=0):x(a),y(b),z(c){}\n};\n\ndouble norm(Point3d a,Point3d b){\n return pow((a.x-b.x),2)+pow((a.y-b.y),2)+pow((a.z-b.z),2);\n}\n\ndouble abs(Point3d a,Point3d b){ \n return sqrt(norm(a,b));\n}\n\nint main()\n{\n vector<Point3d> v;\n int n,m;\n int ans;\n double eps=0.000000001;\n bool st[501];\n\n while(1){\n cin>>n;\n if(n==0)break;\n double a,b,c,d;\n for(int i=0;i<501;i++)st[i]=false;\n v.clear();\n for(int i=0;i<n;i++){\n cin>>a>>b>>c;\n v.push_back(Point3d(a,b,c));\n }\n cin>>m;\n\n Point3d origin(0.0,0.0,0.0);\n for(int i=0;i<m;i++){\n cin>>a>>b>>c>>d;\n Point3d p3(a,b,c);\n double x,y,z,r;\n for(int j=0;j<n;j++){\n\tx=abs(v[j],origin);\n\ty=abs(p3,origin);\n\tz=abs(v[j],p3);\n\t//\tprintf(\"%.10f %.10f %.10f\\n\",x,y,z);\n \tr=acos((x*x+y*y-z*z)/(x*y*2));\n\t\n\t//\tprintf(\"%.10f %.10f\\n\",r,d);\n\tif((r-d)<eps)st[j]=true;\n }\n }\n ans=0;\n for(int i=0;i<n;i++)if(st[i])ans++;\n cout<<ans<<endl;\n }\n return 0;\n}", "accuracy": 1, "time_ms": 30, "memory_kb": 1332, "score_of_the_acc": -0.1887, "final_rank": 5 }, { "submission_id": "aoj_1266_1701687", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\n\nint n,m;\ndouble x[1000],y[1000],z[1000];\ndouble tx,ty,tz,ar;\n\ndouble dot(double ax,double ay,double az,\n double bx,double by,double bz){\n return ax*bx+ay*by+az*bz;\n}\n\nint calc(double sx,double sy,double sz){\n double S=sqrt(sx*sx+sy*sy+sz*sz);\n double T=sqrt(tx*tx+ty*ty+tz*tz);\n double Cos = dot(tx,ty,tz,sx,sy,sz)/T/S;\n double ti=acos(Cos);\n\n if(ti < ar + 0.00000001 ){\n return 1;\n }else{\n return 0;\n }\n}\nbool flg[1000];\nint main(){\n while(cin>>n&&n){\n for(int i=0;i<n;i++){\n flg[i]=false;\n cin>>x[i]>>y[i]>>z[i];\n }\n cin>>m;\n for(int i=0;i<m;i++){\n cin>>tx>>ty>>tz>>ar;\n for(int j=0;j<n;j++){\n if(calc(x[j],y[j],z[j]))flg[j]=true;\n }\n }\n int ans=0;\n for(int i=0;i<n;i++)ans+=flg[i];\n cout<<ans<<endl;\n }\n return 0;\n}", "accuracy": 1, "time_ms": 20, "memory_kb": 1324, "score_of_the_acc": -0.095, "final_rank": 3 }, { "submission_id": "aoj_1266_1701663", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n#define REP(i,n) for(int i=0;i<n;i++)\n\nint main(void){\n int n;\n while(cin >> n, n){\n vector< vector<double> > s(n, vector<double>(3));\n REP(i, n){\n REP(j, 3){\n cin >> s[i][j];\n }\n }\n int m;\n cin >> m;\n vector< vector<double> > t(m, vector<double>(4));\n REP(i, m){\n REP(j, 4){\n cin >> t[i][j];\n }\n }\n\n int ans = 0;\n REP(i, n){\n REP(j, m){\n double sx = s[i][0];\n double sy = s[i][1];\n double sz = s[i][2];\n double tx = t[j][0];\n double ty = t[j][1];\n double tz = t[j][2];\n double tt = t[j][3];\n double len_s = pow(sx*sx + sy*sy + sz*sz,0.5);\n double len_t = pow(tx*tx + ty*ty + tz*tz,0.5);\n double costheta = (sx*tx + sy*ty + sz*tz) / (len_s * len_t);\n double theta = acos(costheta);\n if(theta < tt){\n ans++;\n break;\n }\n }\n }\n cout << ans << endl;\n }\n return 0;\n}", "accuracy": 1, "time_ms": 30, "memory_kb": 1444, "score_of_the_acc": -0.2271, "final_rank": 6 } ]
aoj_1268_cpp
Problem C: Cubic Eight-Puzzle Let's play a puzzle using eight cubes placed on a 3 × 3 board leaving one empty square. Faces of cubes are painted with three colors. As a puzzle step, you can roll one of the cubes to the adjacent empty square. Your goal is to make the specified color pattern visible from above by a number of such steps. The rules of this puzzle are as follows. Coloring of Cubes: All the cubes are colored in the same way as shown in Figure 3. The opposite faces have the same color. Figure 3: Coloring of a cube Initial Board State: Eight cubes are placed on the 3 × 3 board leaving one empty square. All the cubes have the same orientation as shown in Figure 4. As shown in the figure, squares on the board are given x and y coordinates, (1, 1), (1, 2), .. ., and (3, 3). The position of the initially empty square may vary. Figure 4: Initial board state Rolling Cubes: At each step, we can choose one of the cubes adjacent to the empty square and roll it into the empty square, leaving the original position empty. Figure 5 shows an example. Figure 5: Rolling a cube Goal: The goal of this puzzle is to arrange the cubes so that their top faces form the specified color pattern by a number of cube rolling steps described above. Your task is to write a program that finds the minimum number of steps required to make the specified color pattern from the given initial state. Input The input is a sequence of datasets. The end of the input is indicated by a line containing two zeros separated by a space. The number of datasets is less than 16. Each dataset is formatted as follows. x y F 11 F 21 F 31 F 12 F 22 F 32 F 13 F 23 F 33 The first line contains two integers x and y separated by a space, indicating the position ( x , y ) of the initially empty square. The values of x and y are 1, 2, or 3. The following three lines specify the color pattern to make. Each line contains three characters F 1 j , F 2 j , and F 3 j , separated by a space. Character F ij indicates the top color of the cube, if any, at position ( i , j ) as follows: B: Blue W: White R: Red E: the square is Empty. There is exactly one ' E ' character in each dataset. Output For each dataset, output the minimum number of steps to achieve the goal, when the goal can be reached within 30 steps. Otherwise, output " -1 " for the dataset. Sample Input 1 2 W W W E W W W W W 2 1 R B W R W W E W W 3 3 W B W B R E R B R 3 3 B W R B W R B E R 2 1 B B B B R B B R E 1 1 R R R W W W R R E 2 1 R R R B W B R R E 3 2 R R R W E W R R R 0 0 Output for the Sample Input 0 3 13 23 29 30 -1 -1
[ { "submission_id": "aoj_1268_10852687", "code_snippet": "#include<cmath>\n#include<cstdio>\n#include<cstring>\n#include<iostream>\nusing namespace std;\nconst int n=3;\nconst int dx[]={-1,1,0,0},dy[]={0,0,-1,1};\nint fx,fy,t[4][4],ans;\nstruct node{\n\tint x,y;\n\tnode(int x=0,int y=0):x(x),y(y){}\n}a[4][4];\nint nextstep(){\t\n\tint tot=0;\n\tfor(int i=1;i<=n;i++)\n\t\tfor(int j=1;j<=n;j++)\n\t\t\tif(a[i][j].x!=t[i][j])\n\t\t\t\ttot++;return tot;\n}\nvoid update(int x,int y,int d){\n\tint nx=x+dx[d],ny=y+dy[d];\n\tif(d<2)swap(a[x][y].x,a[x][y].y);\n\telse a[x][y].x=6-a[x][y].x-a[x][y].y;\n\tswap(a[x][y],a[nx][ny]);\n}\nvoid dfs(int cnt,int x,int y){\n\tint s=nextstep();\n\tif(!s){ans=min(ans,cnt);return;}\n\tif(cnt+s>=ans)return;\n\tint nowx,nowy;\n\tfor(int i=1;i<=n;i++)\n\t\tfor(int j=1;j<=n;j++)\n\t\t\tif(!a[i][j].x)nowx=i,nowy=j;\n\tfor(int i=0;i<4;i++){\n\t\tint nx=nowx+dx[i],ny=nowy+dy[i];\n\t\tif(nx<1||nx>n||ny<1||ny>n||(nx==x&&ny==y))continue;\n\t\tupdate(nx,ny,i^1);dfs(cnt+1,nowx,nowy);update(nowx,nowy,i);\n\t}\n}\nint main(){\n\twhile(scanf(\"%d%d\",&fy,&fx)&&fx&&fy){\n\t\tmemset(a,0,sizeof(a));\n\t\tmemset(t,0,sizeof(t));\n\t\tfor(int i=1;i<=n;i++)\n\t\t\tfor(int j=1;j<=n;j++)a[i][j]=node(1,2);\n\t\ta[fx][fy]=node(0,0);\n\t\tfor(int i=1;i<=n;i++)\n\t\t\tfor(int j=1;j<=n;j++){\n\t\t\t\tchar c;cin>>c;\n\t\t\t\tif(c=='W')t[i][j]=1;\n\t\t\t\tif(c=='R')t[i][j]=2;\n\t\t\t\tif(c=='B')t[i][j]=3;\n\t\t\t}\n\t\tans=32;dfs(0,0,0);\n\t\tif(ans>30)ans=-1;\n\t\tprintf(\"%d\\n\",ans);\n\t}\n\treturn 0;\n}", "accuracy": 1, "time_ms": 710, "memory_kb": 3464, "score_of_the_acc": -0.0698, "final_rank": 6 }, { "submission_id": "aoj_1268_7917558", "code_snippet": "#include <bits/stdc++.h>\n#define ll int\n#define F first\n#define S second\n#define N 1679636\nusing namespace std;\nll trs[6][2]={{2,5},{4,3},{0,4},{5,1},{1,2},{3,0}},pw6[]={1,6,36,216,1296,7776,46656,279936,1679616};\nll f[N][3][3],q=0,ans[20];\nchar qry[20][3][3];\nvector<ll> qs[3][3];\nqueue<pair<ll,pair<ll,ll> > > qu;\nvoid print(ll mask,ll x,ll y)\n{\n\tll i;\n\tfor(i=0;i<9;i++)\n\t{\n\t\tif(i==x*3+y)\n\t\t{\n\t\t\tputchar('X');\n\t\t}\n\t\telse\n\t\t{\n\t\t\tcout<<mask%6;\n\t\t\tmask/=6;\n\t\t}\n\t}\n\tassert(mask==0);\n\treturn;\n}\nvoid solve(ll sx,ll sy)\n{\n\tll i,j,k,mask;\n\tmemset(f,63,sizeof(f));\n\tf[0][sx][sy]=0;\n\twhile(!qu.empty())\n\t{\n\t\tqu.pop();\n\t}\n\tqu.push(make_pair(0,make_pair(sx,sy)));\n\twhile(!qu.empty())\n\t{\n\t\tll st=qu.front().F,x=qu.front().S.F,y=qu.front().S.S;\n\t\tqu.pop();\n\t\tif(f[st][x][y]>=30)\n\t\t{\n\t\t\tbreak;\n\t\t}\n\t\tll tx,ty;\n\t\ttx=x-1,ty=y;\n\t\tif(tx>=0)\n\t\t{\n\t\t\tll nst=st%pw6[tx*3+ty]+((st%pw6[x*3+y])/pw6[tx*3+ty+1])*pw6[tx*3+ty];\n\t\t\tnst+=pw6[x*3+y-1]*trs[(st/pw6[tx*3+ty])%6][0]+(st/pw6[x*3+y])*pw6[x*3+y];\n\t\t\tif(f[nst][tx][ty]>f[st][x][y]+1)\n\t\t\t{\n\t\t\t\tf[nst][tx][ty]=f[st][x][y]+1;\n\t\t\t\tqu.push(make_pair(nst,make_pair(tx,ty)));\n\t\t\t}\n\t\t}\n\t\ttx=x,ty=y+1;\n\t\tif(ty<3)\n\t\t{\n\t\t\tll nst=st%pw6[x*3+y]+pw6[x*3+y]*trs[(st/pw6[x*3+y])%6][1]+(st/pw6[tx*3+ty])*pw6[tx*3+ty];\n\t\t\tif(f[nst][tx][ty]>f[st][x][y]+1)\n\t\t\t{\n\t\t\t\tf[nst][tx][ty]=f[st][x][y]+1;\n\t\t\t\tqu.push(make_pair(nst,make_pair(tx,ty)));\n\t\t\t}\n\t\t}\n\t\ttx=x+1,ty=y;\n\t\tif(tx<3)\n\t\t{\n\t\t\tll nst=st%pw6[x*3+y]+pw6[x*3+y]*trs[(st/pw6[tx*3+ty-1])%6][0];\n\t\t\tnst+=((st%pw6[tx*3+ty-1])/pw6[x*3+y])*pw6[x*3+y+1]+(st/pw6[tx*3+ty])*pw6[tx*3+ty];\n\t\t\tif(f[nst][tx][ty]>f[st][x][y]+1)\n\t\t\t{\n\t\t\t\tf[nst][tx][ty]=f[st][x][y]+1;\n\t\t\t\tqu.push(make_pair(nst,make_pair(tx,ty)));\n\t\t\t}\n\t\t}\n\t\ttx=x,ty=y-1;\n\t\tif(ty>=0)\n\t\t{\n\t\t\tll nst=st%pw6[tx*3+ty]+pw6[x*3+y-1]*trs[(st/pw6[tx*3+ty])%6][1]+(st/pw6[x*3+y])*pw6[x*3+y];\n\t\t\tif(f[nst][tx][ty]>f[st][x][y]+1)\n\t\t\t{\n\t\t\t\tf[nst][tx][ty]=f[st][x][y]+1;\n\t\t\t\tqu.push(make_pair(nst,make_pair(tx,ty)));\n\t\t\t}\n\t\t}\n\t}\n\tfor(i=0;i<qs[sx][sy].size();i++)\n\t{\n\t\tll x,y,id=qs[sx][sy][i];\n\t\tans[id]=40;\n\t\tfor(mask=0;mask<(1<<8);mask++)\n\t\t{\n\t\t\tll cur=0,cnt=0;\n\t\t\tfor(j=2;j>=0;j--)\n\t\t\t{\n\t\t\t\tfor(k=2;k>=0;k--)\n\t\t\t\t{\n\t\t\t\t\tif(qry[id][j][k]=='E')\n\t\t\t\t\t{\n\t\t\t\t\t\tx=j,y=k;\n\t\t\t\t\t\tcontinue;\n\t\t\t\t\t}\n\t\t\t\t\tcur=cur*6+((mask>>cnt)&1)+(qry[id][j][k]=='W'?0:(qry[id][j][k]=='R'?2:4));\n\t\t\t\t\tcnt++;\n\t\t\t\t}\n\t\t\t}\n\t\t\tans[id]=min(ans[id],f[cur][x][y]);\n\t\t}\n\t}\n\treturn;\n}\nint main(){\n\tll i,j,x,y;\n\twhile(true)\n\t{\n\t\tcin>>y>>x;\n\t\tif(x==0&&y==0)\n\t\t{\n\t\t\tbreak;\n\t\t}\n\t\tx--,y--;\n\t\tqs[x][y].push_back(q);\n\t\tfor(i=0;i<3;i++)\n\t\t{\n\t\t\tfor(j=0;j<3;j++)\n\t\t\t{\n\t\t\t\tcin>>qry[q][i][j];\n\t\t\t}\n\t\t}\n\t\tq++;\n\t}\n\tfor(i=0;i<3;i++)\n\t{\n\t\tfor(j=0;j<3;j++)\n\t\t{\n\t\t\tif(!qs[i][j].empty())\n\t\t\t{\n\t\t\t\tsolve(i,j);\n\t\t\t}\n\t\t}\n\t}\n\tfor(i=0;i<q;i++)\n\t{\n\t\tif(ans[i]>30)\n\t\t{\n\t\t\tans[i]=-1;\n\t\t}\n\t\tcout<<ans[i]<<'\\n';\n\t}\n\treturn 0;\n}", "accuracy": 1, "time_ms": 1610, "memory_kb": 68224, "score_of_the_acc": -1.1647, "final_rank": 15 }, { "submission_id": "aoj_1268_3896682", "code_snippet": "#include <cstdio>\n#include <cstdlib>\n#include <cmath>\n#include <cstring>\n\n#include <iostream>\n#include <complex>\n#include <string>\n#include <algorithm>\n#include <numeric>\n#include <vector>\n#include <queue>\n#include <stack>\n#include <map>\n#include <set>\n#include <unordered_map>\n#include <unordered_set>\n\n#include <functional>\n#include <cassert>\n\ntypedef long long ll;\nusing namespace std;\n\n#ifndef LOCAL\n#define debug(x) ;\n#else\n#define debug(x) cerr << __LINE__ << \" : \" << #x << \" = \" << (x) << endl;\n\ntemplate <typename T1, typename T2>\nostream &operator<<(ostream &out, const pair<T1, T2> &p) {\n out << \"{\" << p.first << \", \" << p.second << \"}\";\n return out;\n}\n\ntemplate <typename T>\nostream &operator<<(ostream &out, const vector<T> &v) {\n out << '{';\n for (const T &item : v) out << item << \", \";\n out << \"\\b\\b}\";\n return out;\n}\n#endif\n\n#define mod 1000000007 //1e9+7(prime number)\n#define INF 1000000000 //1e9\n#define LLINF 2000000000000000000LL //2e18\n#define SIZE 200010\n\n// 0 ... B-W-B\n// 1 ... R-W-R\n// 2 ... W-B-W\n// 3 ... R-B-R\n// 4 ... W-R-W\n// 5 ... B-R-B\n// 6 ... empty\n\n// 0 -> LR, 1 -> UD\n\nint to[6][2] = {{2, 5}, {4, 3}, {0, 4}, {5, 1}, {1, 2}, {3, 0}};\n\nint encode(int state[3][3]) {\n int emptyPos = 0;\n int res = 0;\n\n for (int i=0; i<3; i++) {\n for(int j=0; j<3; j++) {\n if (state[i][j] == 6)\n emptyPos = i * 3 + j;\n else\n res = res * 6 + state[i][j];\n }\n }\n\n return res * 9 + emptyPos;\n}\n\nvoid decode(int hash, int state[3][3]) {\n int emptyPos = hash % 9;\n hash /= 9;\n\n state[emptyPos/3][emptyPos%3] = 6;\n\n for (int i=2; i>=0; i--) {\n for (int j=2; j>=0; j--) {\n if (i * 3 + j == emptyPos) continue;\n state[i][j] = hash % 6;\n hash /= 6;\n }\n }\n}\n\nint memo[1679616 * 9]; //6^8 * 9\n\nint base6[] = {1, 6, 36, 216, 1296, 7776, 46656, 279936, 1679616};\n\nvoid print(int hash) {\n int state[3][3];\n decode(hash, state);\n for (int i=0; i<3; i++) {\n for (int j=0; j<3; j++)\n printf(\"%d \", state[i][j]);\n puts(\"\");\n }\n puts(\"\");\n}\n\nvoid bfs(int y, int x) {\n queue<int> que;\n int state[3][3];\n\n que.push(y * 3 + x);\n\n for(int i=0; i<=30; i++) {\n queue<int> que2;\n\n while(que.size()) {\n int hash = que.front();\n que.pop();\n\n if (memo[hash]) continue;\n\n memo[hash] = i + 1;\n\n decode(hash, state);\n\n int emptyX = hash % 9 % 3;\n int emptyY = hash % 9 / 3;\n\n if (emptyY > 0) {\n int tmp = state[emptyY-1][emptyX];\n state[emptyY][emptyX] = to[tmp][1];\n state[emptyY-1][emptyX] = 6;\n\n int tmp2 = encode(state);\n if (!memo[tmp2]) que2.push(tmp2);\n\n state[emptyY-1][emptyX] = tmp;\n state[emptyY][emptyX] = 6;\n }\n\n if (emptyY+1 < 3) {\n int tmp = state[emptyY+1][emptyX];\n state[emptyY][emptyX] = to[tmp][1];\n state[emptyY+1][emptyX] = 6;\n\n int tmp2 = encode(state);\n if (!memo[tmp2]) que2.push(tmp2);\n\n state[emptyY+1][emptyX] = tmp;\n state[emptyY][emptyX] = 6;\n }\n\n if (emptyX > 0) {\n int tmp = state[emptyY][emptyX-1];\n state[emptyY][emptyX] = to[tmp][0];\n state[emptyY][emptyX-1] = 6;\n\n int tmp2 = encode(state);\n if (!memo[tmp2]) que2.push(tmp2);\n\n state[emptyY][emptyX-1] = tmp;\n state[emptyY][emptyX] = 6;\n }\n\n if (emptyX+1 < 3) {\n int tmp = state[emptyY][emptyX+1];\n state[emptyY][emptyX] = to[tmp][0];\n state[emptyY][emptyX+1] = 6;\n\n int tmp2 = encode(state);\n if (!memo[tmp2]) que2.push(tmp2);\n\n state[emptyY][emptyX+1] = tmp;\n state[emptyY][emptyX] = 6;\n }\n }\n\n que = que2;\n }\n}\n\nint solve(int y, int x, int hash) {\n static int prevY = -1, prevX = -1;\n\n if (prevY != y || prevX != x) {\n memset(memo, 0, sizeof(memo));\n\n bfs(y, x);\n }\n\n int ans = INF, state[3][3];\n\n decode(hash, state);\n\n for (int i=0; i<(1<<9); i++) {\n\n for (int y=0; y<3; y++) {\n for (int x=0; x<3; x++) {\n if (state[y][x] < 6)\n state[y][x] = state[y][x] / 2 * 2 + (i >> (y * 3 + x)) % 2;\n }\n }\n\n int hash = encode(state);\n\n if (memo[hash] > 0)\n ans = min(ans, memo[hash] - 1);\n }\n\n prevY = y;\n prevX = x;\n\n return ans;\n}\n\nint main(){\n int x, y;\n int state[3][3];\n\n vector<pair<int, pair<int,int> > > query;\n\n for(int i=0; ;i++){\n scanf(\"%d%d\", &x, &y);\n\n if (x == 0) break;\n x--; y--;\n\n int b = 2, r = 4;\n int rotate = 0;\n\n if ((y == 0 && x == 2) || (y == 1 && x == 2)) { rotate = 1; swap(b, r); }\n if ((y == 2 && x == 2) || (y == 2 && x == 1)) { rotate = 2; }\n if ((y == 2 && x == 0) || (y == 1 && x == 0)) { rotate = 3; swap(b, r); }\n\n for (int i=0; i<3; i++) {\n for (int j=0; j<3; j++) {\n char c;\n scanf(\" %c\", &c);\n\n if (c == 'W') state[i][j] = 0;\n if (c == 'B') state[i][j] = b;\n if (c == 'R') state[i][j] = r;\n if (c == 'E') state[i][j] = 6;\n }\n }\n\n for (int k=0; k<rotate; k++) {\n swap(state[0][0], state[0][2]);\n swap(state[0][2], state[2][2]);\n swap(state[2][2], state[2][0]);\n\n swap(state[0][1], state[1][2]);\n swap(state[1][2], state[2][1]);\n swap(state[2][1], state[1][0]);\n\n // (1, 2) => (0, 1)\n int tmp = x;\n x = y;\n y = 2 - tmp;\n }\n\n int hash = encode(state);\n\n debug(y); debug(x);\n debug(rotate);\n\n query.push_back({y * 3 + x, {hash, i}});\n }\n\n sort(query.begin(), query.end());\n\n vector<int> ans(query.size());\n\n for (int i=0; i<query.size(); i++) {\n int y = query[i].first / 3;\n int x = query[i].first % 3;\n int hash = query[i].second.first;\n int idx = query[i].second.second;\n\n ans[idx] = solve(y, x, hash);\n }\n\n for (int p : ans) {\n if (p == INF)\n puts(\"-1\");\n else\n printf(\"%d\\n\", p);\n }\n\n return 0;\n}", "accuracy": 1, "time_ms": 1290, "memory_kb": 67252, "score_of_the_acc": -1.1036, "final_rank": 14 }, { "submission_id": "aoj_1268_2676075", "code_snippet": "#include <stdio.h>\n#include <cmath>\n#include <algorithm>\n#include <cfloat>\n#include <stack>\n#include <queue>\n#include <vector>\n#include <string>\n#include <iostream>\n#include <set>\n#include <map>\n#include <time.h>\ntypedef long long int ll;\ntypedef unsigned long long int ull;\n#define BIG_NUM 2000000000\n#define MOD 1000000007\n#define EPS 0.000000001\nusing namespace std;\n\n\nclass Dice{\npublic:\n\n\n\tvoid roll(char dst){\n\t\tfor(int i = 0; i < 6; i++) work[i] = color[i];\n\t\tswitch(dst){\n\t\tcase 'E':\n\t\t\tsetColor(work[3],work[1],work[0],work[5],work[4],work[2]);\n\t\t\tbreak;\n\t\tcase 'S':\n\t\t\tsetColor(work[1],work[5],work[2],work[3],work[0],work[4]);\n\t\t\tbreak;\n\t\tcase 'N':\n\t\t\tsetColor(work[4],work[0],work[2],work[3],work[5],work[1]);\n\t\t\tbreak;\n\t\tcase 'W':\n\t\t\tsetColor(work[2],work[1],work[5],work[0],work[4],work[3]);\n\t\t\tbreak;\n\t\t}\n\t};\n\n\tchar getTop(){\n\t\treturn color[0];\n\t}\n\n\tvoid setColor(char n0,char n1,char n2,char n3,char n4,char n5){\n\t\tcolor[0] = n0;\n\t\tcolor[1] = n1;\n\t\tcolor[2] = n2;\n\t\tcolor[3] = n3;\n\t\tcolor[4] = n4;\n\t\tcolor[5] = n5;\n\t}\n\n\tvoid init(){\n\t\tsetColor('W','R','B','B','R','W');\n\t}\n\n\tvoid to_empty(){\n\t\tsetColor('E','E','E','E','E','E');\n\t}\n\n\tchar color[6],work[6];\n};\n\nstruct Info{\n\tDice board[3][3];\n\tint num_step;\n};\n\nint first_row,first_col;\nint diff_row[4] = {-1,0,0,1},diff_col[4] = {0,-1,1,0};\nInfo goal;\n\nbool rangeCheck(int row,int col){\n\tif(row >= 0 && row <= 2 && col >= 0 && col <= 2)return true;\n\telse{\n\t\treturn false;\n\t}\n}\n\nint calcDiff(Info info){\n\n\tint ret = 0;\n\n\tfor(int row = 0; row < 3; row++){\n\t\tfor(int col = 0; col < 3; col++){\n\t\t\tif(info.board[row][col].getTop() != goal.board[row][col].getTop()){\n\t\t\t\tret++;\n\t\t\t}\n\t\t}\n\t}\n\n\treturn ret;\n}\n\n\nbool FLG;\n\nvoid recursive(Info info,int max_depth,int empty_row,int empty_col,int pre_row,int pre_col){\n\n\tif(FLG)return;\n\n\tint tmp_diff = calcDiff(info);\n\n\tif(tmp_diff == 0){\n\t\tFLG = true;\n\t\treturn;\n\t}\n\n\tif(info.num_step+tmp_diff > max_depth+1)return;\n\n\tint adj_row,adj_col;\n\tinfo.num_step++;\n\n\tDice tmp;\n\n\tfor(int i = 0; i < 4; i++){\n\t\tadj_row = empty_row+diff_row[i];\n\t\tadj_col = empty_col+diff_col[i];\n\n\t\tif(!rangeCheck(adj_row,adj_col))continue;\n\t\tif(adj_row == pre_row && adj_col == pre_col)continue;\n\n\t\ttmp = info.board[adj_row][adj_col];\n\n\t\tswitch(i){\n\t\tcase 0:\n\t\t\tinfo.board[adj_row][adj_col].roll('S');\n\t\t\tbreak;\n\t\tcase 1:\n\t\t\tinfo.board[adj_row][adj_col].roll('E');\n\t\t\tbreak;\n\t\tcase 2:\n\t\t\tinfo.board[adj_row][adj_col].roll('W');\n\t\t\tbreak;\n\t\tcase 3:\n\t\t\tinfo.board[adj_row][adj_col].roll('N');\n\t\t\tbreak;\n\t\t}\n\n\t\tswap(info.board[empty_row][empty_col],info.board[adj_row][adj_col]);\n\t\trecursive(info,max_depth,adj_row,adj_col,empty_row,empty_col);\n\n\t\tinfo.board[empty_row][empty_col].to_empty();\n\t\tinfo.board[adj_row][adj_col] = tmp;\n\t}\n}\n\n\nvoid func(){\n\n\tfirst_row--;\n\tfirst_col--;\n\n\tchar buf[2];\n\n\tfor(int row = 0; row < 3; row++){\n\t\tfor(int col = 0; col < 3; col++){\n\t\t\tscanf(\"%s\",buf);\n\t\t\tgoal.board[row][col].color[0] = buf[0];\n\t\t}\n\t}\n\n\tInfo first;\n\tfor(int row = 0; row < 3; row++){\n\t\tfor(int col = 0; col < 3; col++)first.board[row][col].init();\n\t}\n\tfirst.board[first_row][first_col].to_empty();\n\tfirst.num_step = 0;\n\n\tFLG = false;\n\tint ans;\n\n\tfor(int max_depth = 0; max_depth <= 30; max_depth++){\n\t\trecursive(first,max_depth,first_row,first_col,-1,-1);\n\t\tif(FLG){\n\t\t\tans = max_depth;\n\t\t\tbreak;\n\t\t}\n\t}\n\n\tif(FLG){\n\t\tprintf(\"%d\\n\",ans);\n\t}else{\n\t\tprintf(\"-1\\n\");\n\t}\n}\n\n\nint main(){\n\n\twhile(true){\n\t\tscanf(\"%d %d\",&first_col,&first_row);\n\t\tif(first_col == 0 && first_row == 0)break;\n\n\t\tfunc();\n\t}\n\n\treturn 0;\n}", "accuracy": 1, "time_ms": 1450, "memory_kb": 3200, "score_of_the_acc": -0.1737, "final_rank": 9 }, { "submission_id": "aoj_1268_2376334", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\n \nstruct Dice{\n int s[6],f;\n Dice(){}\n void roll(char c){\n int b;\n if(c=='E'||c==3){\n b=s[0];\n s[0]=s[3];\n s[3]=s[5];\n s[5]=s[2];\n s[2]=b;\n }\n if(c=='W'||c==1){\n b=s[0];\n s[0]=s[2];\n s[2]=s[5];\n s[5]=s[3];\n s[3]=b;\n }\n if(c=='N'||c==0){\n b=s[0];\n s[0]=s[1];\n s[1]=s[5];\n s[5]=s[4];\n s[4]=b;\n }\n if(c=='S'||c==2){\n b=s[0];\n s[0]=s[4];\n s[4]=s[5];\n s[5]=s[1];\n s[1]=b;\n }\n \n if(c=='R'){\n b=s[1];\n s[1]=s[3];\n s[3]=s[4];\n s[4]=s[2];\n s[2]=b;\n }\n if(c=='L'){\n b=s[1];\n s[1]=s[2];\n s[2]=s[4];\n s[4]=s[3];\n s[3]=b;\n }\n }\n int top(){\n if(f) return 3;\n return s[0];\n }\n int hash(){\n if(f) return -1;\n int res=0;\n for(int i=0;i<6;i++) res=res*4+s[i];\n return res;\n }\n};\n \nmap<int,int> mi;\nmap<int,Dice> md;\nvoid makeDices(Dice d){\n for(int i=0;i<6;i++){\n Dice t(d);\n if(i==1) t.roll('N');\n if(i==2) t.roll('S');\n if(i==3) t.roll('S'),t.roll('S');\n if(i==4) t.roll('L');\n if(i==5) t.roll('R');\n for(int k=0;k<4;k++){\n int h=t.hash(),p=mi.size();\n //cout<<h<<\"-\"<<p<<endl;\n if(!mi.count(h)){\n mi[h]=p;\n md[p]=t;\n //cout<<p<<\":\"<<h<<\"-\"<<t.f<<endl;\n }\n t.roll('E');\n }\n }\n //cout<<md.size()<<endl;\n d.f=1;\n mi[-1]=6;\n md[6]=d;\n}\n \nint tv(vector<Dice>& ds){\n int res=0;\n for(int i=0;i<9;i++)\n res=res*4+ds[i].top();\n return res;\n}\n \nint ds2i(vector<Dice>& ds){\n int res=0;\n for(int i=8;i>=0;i--) res=res*7+mi[ds[i].hash()];\n return res;\n}\nvoid i2ds(int x,vector<Dice>& ds){\n for(int i=0;i<9;i++){\n ds[i]=md[x%7];\n x/=7;\n }\n}\nchar dp[40353607+1];\nsigned main(){\n int y,x;\n while(cin>>x>>y,x||y){\n x--;y--;\n char f[3][3];\n for(int i=0;i<3;i++)\n for(int j=0;j<3;j++)\n\tcin>>f[i][j];\n int en=0;\n for(int i=0;i<3;i++){\n for(int j=0;j<3;j++){\n\tint k=3;\n\tif(f[i][j]=='B') k=0;\n\tif(f[i][j]=='W') k=1;\n\tif(f[i][j]=='R') k=2;\n\t//cout<<k<<\":\";\n\ten=en*4+k;\n }\n }\n //cout<<endl;\n \n //cout<<en<<endl;\n \n vector<Dice> ds(9);\n Dice d;\n d.s[0]=d.s[5]=1;\n d.s[2]=d.s[3]=0;\n d.s[1]=d.s[4]=2;\n d.f=0;\n \n makeDices(d);\n \n for(int i=0;i<3;i++){\n for(int j=0;j<3;j++){\n\tds[i*3+j]=d;\n\tif(y==i&&x==j) ds[i*3+j].f=1;\n\t//cout<<i*3+j<<\"=\"<<ds[i*3+j].f<<\"/\"<<ds[i*3+j].top()<<endl;\n }\n }\n \n //for(int i=0;i<3;i++) for(int j=0;j<3;j++) cout<<i*3+j<<\"=\"<<ds[i*3+j].f<<\"/\"<<ds[i*3+j].top()<<endl;\n \n int ay[]={1,0,-1,0};\n int ax[]={0,1,0,-1};\n \n int ans=-1;\n fill_n(dp,40353607+1,32);\n //map<int,int> dp;\n queue<int> q;\n \n \n q.push(ds2i(ds));\n dp[ds2i(ds)]=0;\n while(!q.empty()){\n int p=q.front();q.pop();\n //cout<<p<<endl;\n if(dp[p]>=30) break;\n i2ds(p,ds);\n //cout<<tv(ds)<<endl;\n if(tv(ds)==en){\n\tans=dp[p];\n\tbreak;\n }\n int y=-1,x=-1;\n for(int i=0;i<3;i++)\n\tfor(int j=0;j<3;j++)\n\t if(ds[i*3+j].f) y=i,x=j;\n \n //cout<<dp[p]<<\":\"<<y<<\" \"<<x<<endl;\n \n for(int k=0;k<4;k++){\n\tint ny=y+ay[k],nx=x+ax[k];\n\t//cout<<k<<\"/\"<<ny<<\" \"<<nx<<endl;\n\tif(ny<0||3<=ny||nx<0||3<=nx) continue;\n\t//cout<<k<<\"/\"<<ny<<\" \"<<nx<<endl;\n\tds[ny*3+nx].roll(k);\n\tswap(ds[ny*3+nx],ds[y*3+x]);\n \n\tint h=ds2i(ds);\n\tif(dp[h]>30){\n\t if(tv(ds)==en){\n\t ans=dp[p]+1;\n\t break;\n\t }\n\t dp[h]=dp[p]+1;\n\t q.push(h);\n\t}\n \n\tswap(ds[ny*3+nx],ds[y*3+x]);\n\tds[ny*3+nx].roll((k+2)%4);\n }\n if(ans>=0) break;\n }\n cout<<ans<<endl;\n }\n return 0;\n}", "accuracy": 1, "time_ms": 7340, "memory_kb": 44300, "score_of_the_acc": -1.644, "final_rank": 20 }, { "submission_id": "aoj_1268_2376133", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\n\nstruct Dice{\n int s[6],f;\n Dice(){}\n void roll(char c){\n int b;\n if(c=='E'||c==3){\n b=s[0];\n s[0]=s[3];\n s[3]=s[5];\n s[5]=s[2];\n s[2]=b;\n }\n if(c=='W'||c==1){\n b=s[0];\n s[0]=s[2];\n s[2]=s[5];\n s[5]=s[3];\n s[3]=b;\n }\n if(c=='N'||c==0){\n b=s[0];\n s[0]=s[1];\n s[1]=s[5];\n s[5]=s[4];\n s[4]=b;\n }\n if(c=='S'||c==2){\n b=s[0];\n s[0]=s[4];\n s[4]=s[5];\n s[5]=s[1];\n s[1]=b;\n }\n\n if(c=='R'){\n b=s[1];\n s[1]=s[3];\n s[3]=s[4];\n s[4]=s[2];\n s[2]=b;\n }\n if(c=='L'){\n b=s[1];\n s[1]=s[2];\n s[2]=s[4];\n s[4]=s[3];\n s[3]=b;\n }\n }\n int top(){\n if(f) return 3;\n return s[0];\n }\n int hash(){\n if(f) return -1;\n int res=0;\n for(int i=0;i<6;i++) res=res*4+s[i];\n return res;\n }\n};\n\nmap<int,int> mi;\nmap<int,Dice> md;\nvoid makeDices(Dice d){\n for(int i=0;i<6;i++){\n Dice t(d);\n if(i==1) t.roll('N');\n if(i==2) t.roll('S');\n if(i==3) t.roll('S'),t.roll('S');\n if(i==4) t.roll('L');\n if(i==5) t.roll('R');\n for(int k=0;k<4;k++){\n int h=t.hash(),p=mi.size();\n //cout<<h<<\"-\"<<p<<endl;\n if(!mi.count(h)){\n\tmi[h]=p;\n\tmd[p]=t;\n\t//cout<<p<<\":\"<<h<<\"-\"<<t.f<<endl;\n }\n t.roll('E');\n }\n }\n //cout<<md.size()<<endl;\n d.f=1;\n mi[-1]=6;\n md[6]=d;\n}\n\nint tv(vector<Dice>& ds){\n int res=0;\n for(int i=0;i<9;i++)\n res=res*4+ds[i].top();\n return res;\n}\n\nint ds2i(vector<Dice>& ds){\n int res=0;\n for(int i=8;i>=0;i--) res=res*7+mi[ds[i].hash()];\n return res;\n}\nvoid i2ds(int x,vector<Dice>& ds){\n for(int i=0;i<9;i++){\n ds[i]=md[x%7];\n x/=7;\n }\n}\nchar dp[40353607+1];\nsigned main(){\n int y,x;\n while(cin>>x>>y,x||y){\n x--;y--;\n char f[3][3];\n for(int i=0;i<3;i++)\n for(int j=0;j<3;j++)\n\tcin>>f[i][j];\n int en=0;\n for(int i=0;i<3;i++){\n for(int j=0;j<3;j++){\n\tint k=3;\n\tif(f[i][j]=='B') k=0;\n\tif(f[i][j]=='W') k=1;\n\tif(f[i][j]=='R') k=2;\n\t//cout<<k<<\":\";\n\ten=en*4+k;\n }\n }\n //cout<<endl;\n\n //cout<<en<<endl;\n \n vector<Dice> ds(9);\n Dice d;\n d.s[0]=d.s[5]=1;\n d.s[2]=d.s[3]=0;\n d.s[1]=d.s[4]=2;\n d.f=0;\n\n makeDices(d);\n \n for(int i=0;i<3;i++){\n for(int j=0;j<3;j++){\n\tds[i*3+j]=d;\n\tif(y==i&&x==j) ds[i*3+j].f=1;\n\t//cout<<i*3+j<<\"=\"<<ds[i*3+j].f<<\"/\"<<ds[i*3+j].top()<<endl;\n }\n }\n\n //for(int i=0;i<3;i++) for(int j=0;j<3;j++) cout<<i*3+j<<\"=\"<<ds[i*3+j].f<<\"/\"<<ds[i*3+j].top()<<endl;\n \n int ay[]={1,0,-1,0};\n int ax[]={0,1,0,-1};\n \n int ans=-1;\n fill_n(dp,40353607+1,32);\n //map<int,int> dp;\n queue<int> q;\n\n \n q.push(ds2i(ds));\n dp[ds2i(ds)]=0;\n while(!q.empty()){\n int p=q.front();q.pop();\n //cout<<p<<endl;\n if(dp[p]>=30) break;\n i2ds(p,ds);\n //cout<<tv(ds)<<endl;\n if(tv(ds)==en){\n\tans=dp[p];\n\tbreak;\n }\n int y=-1,x=-1;\n for(int i=0;i<3;i++)\n\tfor(int j=0;j<3;j++)\n\t if(ds[i*3+j].f) y=i,x=j;\n\n //cout<<dp[p]<<\":\"<<y<<\" \"<<x<<endl;\n \n for(int k=0;k<4;k++){\n\tint ny=y+ay[k],nx=x+ax[k];\n\t//cout<<k<<\"/\"<<ny<<\" \"<<nx<<endl;\n\tif(ny<0||3<=ny||nx<0||3<=nx) continue;\n\t//cout<<k<<\"/\"<<ny<<\" \"<<nx<<endl;\n\tds[ny*3+nx].roll(k);\n\tswap(ds[ny*3+nx],ds[y*3+x]);\n\n\tint h=ds2i(ds);\n\tif(dp[h]>30){\n\t if(tv(ds)==en){\n\t ans=dp[p]+1;\n\t break;\n\t }\n\t dp[h]=dp[p]+1;\n\t q.push(h);\n\t}\n\n\tswap(ds[ny*3+nx],ds[y*3+x]);\n\tds[ny*3+nx].roll((k+2)%4);\n }\n if(ans>=0) break;\n }\n cout<<ans<<endl;\n }\n return 0;\n}", "accuracy": 1, "time_ms": 7300, "memory_kb": 44388, "score_of_the_acc": -1.6394, "final_rank": 19 }, { "submission_id": "aoj_1268_2376106", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\nint dx[4]={-1,0,1,0},dy[4]={0,-1,0,1},n=3;\nbool check(int x,int y) {return x>=0&&x<n&&y>=0&&y<n;}\nvector<char> rotate(vector<char> a,int k) {\n if(k%2) swap(a[0],a[2]);\n else swap(a[0],a[1]);\n return a;\n}\n\nchar s[3][3];\nint ans;\nvector<vector<vector<char> > > v;\nvoid dfs(int c,int p) {\n if(c>=ans) return;\n int xx,yy,d=0;\n for(int i=0; i<n; i++) {\n for(int j=0; j<n; j++) {\n if(v[i][j][0]=='E') xx=i,yy=j;\n if(v[i][j][0]!=s[i][j]) d++;\n }\n }\n if(!d) {\n ans=min(ans,c);\n return;\n }\n if(d+c>ans) return;\n for(int i=0; i<4; i++) {\n int x=xx+dx[i],y=yy+dy[i];\n if(!check(x,y)||i==p) continue;\n v[x][y]=rotate(v[x][y],i);\n swap(v[xx][yy],v[x][y]);\n dfs(c+1,(i+2)%4);\n swap(v[xx][yy],v[x][y]);\n v[x][y]=rotate(v[x][y],i);\n }\n}\n\nint main() {\n int xx,yy;\n while(cin >> yy >> xx && xx) {\n v.clear();\n for(int i=0; i<n; i++) {\n for(int j=0; j<n; j++) cin >> s[i][j];\n }\n for(int i=1; i<4; i++) {\n vector<vector<char> > a;\n for(int j=1; j<4; j++) {\n vector<char> c;\n if(i==xx&&j==yy) {\n for(int k=0; k<3; k++) c.push_back('E');\n } else {\n c.push_back('W');\n c.push_back('R');\n c.push_back('B');\n }\n a.push_back(c);\n }\n v.push_back(a);\n }\n ans=31;\n dfs(0,-1);\n if(ans==31) ans=-1;\n cout << ans << endl;\n }\n return 0;\n}", "accuracy": 1, "time_ms": 3910, "memory_kb": 2996, "score_of_the_acc": -0.5293, "final_rank": 12 }, { "submission_id": "aoj_1268_1591489", "code_snippet": "#include <algorithm>\n#include <vector>\n#include <cfloat>\n#include <string>\n#include <cmath>\n#include <set>\n#include <cstdlib>\n#include <map>\n#include <ctime>\n#include <iomanip>\n#include <functional>\n#include <deque>\n#include <iostream>\n#include <cstring>\n#include <queue>\n#include <cstdio>\n#include <stack>\n#include <climits>\n#include <sys/time.h>\n#include <cctype>\n \nusing namespace std;\n \ntypedef long long ll;\n \nclass dice {\n public:\n int a, b, c;\n dice(int a = 0, int b = 1, int c = 2): a(a), b(b), c(c) {}\n bool operator<(const dice &t) const {\n return (a < t.a) || (a == t.a && b < t.b) || (a == t.a && b == t.b && c < t.c);\n }\n};\n \nvector <int> goal; int ans;\nint ix, iy;\nmap<vector<dice>, int> memo;\nint d[] = {-3, 1, 3, -1};\nint dx[] = {0, 1, 0, -1};\nint dy[] = {-1, 0, 1, 0};\nbool same(vector <int> &a, vector <int> &b) {\n for (int i = 0; i < a.size(); i++) {\n if (a[i] != b[i]) return false;\n }\n return true;\n}\nbool same(vector <int> &a, vector <dice> &b) {\n for (int i = 0; i < a.size(); i++) {\n if (a[i] != b[i].a) return false;\n }\n return true;\n}\n \n \nvoid rot(dice &p, int i) {\n if (i == 0 || i == 2) {\n swap(p.a, p.b);\n }else {\n swap(p.a, p.c);\n }\n}\n \nvoid view(vector <int> p) {\n for (int i = 0; i < p.size(); i++) {\n cout << p[i] << \" \";\n if ((i+1)%3 == 0) cout << endl;\n }\n cout << endl;\n}\nvoid view(vector <dice> p) {\n for (int i = 0; i < p.size(); i++) {\n cout << p[i].a << \" \";\n if ((i+1)%3 == 0) cout << endl;\n }\n cout << endl;\n}\n \n \n// int loopn = 0;\nvoid rec(vector <dice> &p, int &x, int &y, int prex, int prey, int cnt) {\n // loopn++;\n if (ans <= cnt+abs(x-ix)+abs(y-iy)) return;\n if (cnt+abs(x-ix)+abs(y-iy) > 30) {\n return;\n }\n // vector <int> t;\n // for (int i = 0; i < 9; i++) t.push_back(p[i].a);\n // map<vector<dice>, int>::iterator it = memo.find(p);\n // if (it != memo.end()) {\n // if ((*it).second <= cnt) {\n // return;\n // }\n // }\n \n // // vector <int> pret = {0, 2, 0, 0, 0, 0, 0, 0, 0};\n // // if (same(pret, p)) {\n // // if (cnt == 1) {\n // // cout << x << \" \" << y << endl;\n // // cout << (*it).second << \" \" << cnt << endl;\n // // }\n // // }\n \n // // view(t);\n // memo[p] = cnt;\n if (same(goal, p)) {\n if (ans <= cnt) return;\n ans = cnt;\n }else {\n for (int i = 0; i < 4; i++) {\n int nx = x+dx[i];\n int ny = y+dy[i];\n \n if (nx < 0 || ny < 0 || nx > 2 || ny > 2) continue;\n if (nx == prex && ny == prey) continue;\n int xy = x+y*3, nxy = nx+ny*3;\n swap(p[xy], p[nxy]);\n rot(p[xy], (i+2)%4);\n // if (same(pret, np)) {\n // if (cnt == 0) {\n // cout << nx << \" \" << ny << endl;\n // // view(np);\n // }\n // }\n \n \n rec(p, nx, ny, x, y, cnt+1);\n rot(p[xy], (i+2)%4);\n swap(p[xy], p[nxy]);\n }\n }\n}\n \nint main() {\n while (true) {\n int x, y;\n cin >> x >> y;\n if (x == 0 && y == 0) break;\n x--; y--;\n vector <dice> init;\n for (int i = 0; i < 9; i++) init.push_back(dice());\n init[x+y*3] = dice(5, 0, 0);\n vector <int> empvec;\n map<vector<dice>, int> empmemo;\n memo = empmemo;\n goal = empvec;\n for (int i = 0; i < 9; i++) {\n char v;\n cin >> v;\n if (v == 'W') goal.push_back(0);\n if (v == 'R') goal.push_back(1);\n if (v == 'B') goal.push_back(2);\n if (v == 'E') goal.push_back(5);\n if (v == 'E') {\n ix = i%3;\n iy = i/3;\n }\n }\n ans = INT_MAX;\n rec(init, x, y, -1, -1, 0);\n // cout << memo.size() << endl;\n // cout << loopn << endl;\n if (ans == INT_MAX){\n cout << -1 << endl;\n }else {\n cout << ans << endl;\n }\n }\n}", "accuracy": 1, "time_ms": 3560, "memory_kb": 1220, "score_of_the_acc": -0.4518, "final_rank": 11 }, { "submission_id": "aoj_1268_1565462", "code_snippet": "#include <iostream>\n#include <array>\n#include <vector>\n#include <set>\n#include <unordered_set>\n#include <map>\n#include <queue>\n#include <algorithm>\n#include <cassert>\n#define repeat_from(i,m,n) for (int i = (m); (i) < (n); ++(i))\n#define repeat(i,n) repeat_from(i,0,n)\n#define repeat_from_reverse(i,m,n) for (int i = (n)-1; (i) >= (m); --(i))\n#define repeat_reverse(i,n) repeat_from_reverse(i,0,n)\n#define dump(x) cerr << #x << \" = \" << (x) << endl\n#define debug(x) cerr << #x << \" = \" << (x) << \" (L\" << __LINE__ << \")\" << \" \" << __FILE__ << endl\nusing namespace std;\n\n// Wn R Wr B\n// BWB RWR\n// R B\n// Bn R Br W\n// WBW RBR\n// R W\n// Rn W Rr B\n// BRB WRW\n// W B\nconst char Wn = 'W';\nconst char Wr = 'w';\nconst char Bn = 'B';\nconst char Br = 'b';\nconst char Rn = 'R';\nconst char Rr = 'r';\nconst char E = 'E';\nchar next_tate(char dice) {\n return\n dice == Wn ? Rn :\n dice == Wr ? Br :\n dice == Bn ? Rr :\n dice == Br ? Wr :\n dice == Rn ? Wn :\n dice == Rr ? Bn : 0;\n}\nchar next_yoko(char dice) {\n return\n dice == Wn ? Bn :\n dice == Wr ? Rr :\n dice == Bn ? Wn :\n dice == Br ? Rn :\n dice == Rn ? Br :\n dice == Rr ? Wr : 0;\n}\nstruct state_t {\n char f[3][3];\n int8_t ey, ex;\n int8_t n;\n int8_t match;\n};\nbool equals_top(char const (& f)[3][3], char const (& g)[3][3]) {\n repeat (y,3) repeat (x,3) {\n if (toupper(f[y][x]) != toupper(g[y][x])) {\n // if (toupper(f[y][x] != toupper(g[y][x]))) {\n return false;\n }\n }\n return true;\n}\nconst int dy[] = { -1, 1, 0, 0 };\nconst int dx[] = { 0, 0, -1, 1 };\nuint32_t pack(char const (& f)[3][3]) {\n uint32_t z = 0;\n repeat (y,3) {\n repeat (x,3) {\n z *= 7;\n z +=\n f[y][x] == Wn ? 1 :\n f[y][x] == Wr ? 2 :\n f[y][x] == Bn ? 3 :\n f[y][x] == Br ? 4 :\n f[y][x] == Rn ? 5 :\n f[y][x] == Rr ? 6 : 0;\n }\n }\n return z;\n}\n\nint main() {\n while (true) {\n int sex, sey; cin >> sex >> sey;\n if (sex == 0 and sey == 0) break;\n -- sex; -- sey;\n char goal[3][3];\n repeat (y,3) repeat (x,3) cin >> goal[y][x];\n unordered_set<uint32_t> used;\n queue<state_t> que; {\n state_t s;\n repeat (y,3) repeat (x,3) s.f[y][x] = Wn;\n s.f[sey][sex] = E;\n s.ey = sey;\n s.ex = sex;\n s.n = 0;\n s.match = 0;\n repeat (y,3) repeat (x,3) if (goal[y][x] == s.f[y][x]) s.match += 1;\n que.push(s);\n used.insert(pack(s.f));\n }\n int result = -1;\n while (not que.empty()) {\n state_t s = que.front(); que.pop();\n//repeat (y,3) {\n//repeat (x,3) {\n//cerr << s.f[y][x];\n//}\n//cerr << endl;\n//}\n//cerr << endl;\n if (equals_top(s.f, goal)) {\n result = s.n;\n break;\n }\n if (s.n == 30) continue;\n if (9 - s.match > 30 - s.n + 1) continue;\n repeat (i,4) {\n state_t t = s;\n t.ey = s.ey + dy[i];\n t.ex = s.ex + dx[i];\n if (t.ey < 0 or 3 <= t.ey or t.ex < 0 or 3 <= t.ex) continue;\n assert (s.f[s.ey][s.ex] == E);\n if (toupper(t.f[s.ey][s.ex]) == goal[s.ey][s.ex]) t.match -= 1;\n if (toupper(t.f[t.ey][t.ex]) == goal[t.ey][t.ex]) t.match -= 1;\n t.f[s.ey][s.ex] = dx[i] ? next_yoko(t.f[t.ey][t.ex]) : next_tate(t.f[t.ey][t.ex]);\n t.f[t.ey][t.ex] = E;\n if (toupper(t.f[s.ey][s.ex]) == goal[s.ey][s.ex]) t.match += 1;\n if (toupper(t.f[t.ey][t.ex]) == goal[t.ey][t.ex]) t.match += 1;\n t.n += 1;\n if (used.count(pack(t.f))) continue;\n que.push(t);\n used.insert(pack(t.f));\n }\n }\n cout << result << endl;\n }\n return 0;\n}", "accuracy": 1, "time_ms": 5210, "memory_kb": 62044, "score_of_the_acc": -1.5975, "final_rank": 18 }, { "submission_id": "aoj_1268_1217549", "code_snippet": "#include<cstdio>\n#include<cstdlib>\n#include<cstring>\n#include<cmath>\n#include<iostream>\n#include<string>\n#include<vector>\n#include<map>\n#include<list>\n#include<queue>\n#include<deque>\n#include<algorithm>\n#include<numeric>\n#include<utility>\n#include<complex>\n#include<functional>\n \nusing namespace std;\n\n/* constant */\n\n#define INF\t(1 << 30)\n\n#define C_E\t(0)\n#define C_W\t(1)\n#define C_R\t(2)\n#define C_B\t(3)\n\n#define MAX_D\t(30)\n\n/* typedef */\n\ntypedef vector<int> vi;\ntypedef vector<long long> vl;\ntypedef vector<double> vd;\ntypedef pair<int,int> pii;\ntypedef pair<long,long> pll;\ntypedef long long ll;\n\nstruct cube_t {\n int x, y, z;\n};\n\n/* global variables */\n\ncube_t ccube = {C_W, C_R, C_B}, ecube = {C_E, C_E, C_E};\nint dxys[4][2] = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}};\n\ncube_t brd[9];\nint gl[9];\nint gdist;\n\n/* subroutines */\n\nvoid check_rec(int d, int x0, int y0, int pdi) {\n if (d >= gdist) return;\n\n int diff = 0;\n for (int i = 0; i < 9; i++)\n if (brd[i].x != gl[i]) diff++;\n if (diff == 0) {\n if (gdist > d) gdist = d;\n return;\n }\n if (d + diff > gdist) return;\n\n int pos0 = y0 * 3 + x0;\n \n for (int di = 0; di < 4; di++) {\n if (((di + 2) % 4) == pdi) continue;\n \n int dx = dxys[di][0];\n int dy = dxys[di][1];\n int x = x0 + dx;\n int y = y0 + dy;\n\n if (x >= 0 && x < 3 && y >= 0 && y < 3) {\n int pos = y * 3 + x;\n cube_t pcb = brd[pos];\n brd[pos] = ecube;\n\n cube_t cb = pcb;\n if (dx != 0) {\n int t = cb.x; cb.x = cb.z; cb.z = t;\n }\n else {\n int t = cb.x; cb.x = cb.y; cb.y = t;\n }\n brd[pos0] = cb;\n\n check_rec(d + 1, x, y, di);\n\n brd[pos] = pcb;\n brd[pos0] = ecube;\n }\n }\n}\n\n/* main */\n\nint main() {\n for (;;) {\n int sx, sy;\n cin >> sx >> sy;\n if ((sx | sy) == 0) break;\n\n sx--;\n sy--;\n\n for (int y = 0; y < 3; y++) {\n for (int x = 0; x < 3; x++) {\n\tint pos = y * 3 + x;\n\tstring ch;\n\tcin >> ch;\n\tswitch (ch[0]) {\n\tcase 'E': gl[pos] = C_E; break;\n\tcase 'W': gl[pos] = C_W; break;\n\tcase 'R': gl[pos] = C_R; break;\n\tcase 'B': gl[pos] = C_B; break;\n\t}\n }\n }\n //for (int i = 0; i < 9; i++) cout << gl[i]; cout << endl;\n \n for (int i = 0; i < 9; i++) brd[i] = ccube;\n brd[sy * 3 + sx] = ecube;\n //for (int i = 0; i < 9; i++) cout << brd[i].x; cout << endl;\n\n gdist = MAX_D + 1;\n\n check_rec(0, sx, sy, -1);\n\n cout << (gdist > MAX_D ? -1 : gdist) << endl;\n }\n\n return 0;\n}", "accuracy": 1, "time_ms": 480, "memory_kb": 1204, "score_of_the_acc": -0.0026, "final_rank": 1 }, { "submission_id": "aoj_1268_1099688", "code_snippet": "#include <array>\n#include <climits>\n#include <cstdlib>\n#include <iostream>\n#include <queue>\n#include <unordered_map>\n#include <vector>\nusing namespace std;\n\ntemplate<class T> inline void chmin(T &a, const T &b) { if(a > b) a = b; }\n\nconstexpr int SIZE = 3;\nenum {B, W, R, E};\nconst string str = \"BWRE\";\n\ntypedef pair<int, int> cube;\n#define top first\n#define south second\n\ntypedef array<array<cube, SIZE>, SIZE> state;\n\ninline bool out(int x, int y) {\n\treturn x < 0 || y < 0 || x >= SIZE || y >= SIZE;\n}\n\ninline unsigned long long encode(const state &s) {\n\tunsigned long long res = 0;\n\tfor(int y = 0; y < SIZE; ++y) {\n\t\tfor(int x = 0; x < SIZE; ++x) {\n\t\t\tres = (res << 4) | (s[y][x].top << 2) | s[y][x].south;\n\t\t}\n\t}\n\treturn res;\n}\n\ninline state decode(unsigned long long code) {\n\tconstexpr unsigned long long mask = 0x3;\n\tstate res;\n\n\tfor(int y = SIZE - 1; y >= 0; --y) {\n\t\tfor(int x = SIZE - 1; x >= 0; --x) {\n\t\t\tres[y][x].south = (code & mask);\n\t\t\tcode >>= 2;\n\t\t\tres[y][x].top = (code & mask);\n\t\t\tcode >>= 2;\n\t\t}\n\t}\n\n\treturn res;\n}\n\ninline int match(const state &a, const state &b) {\n\tint res = 0;\n\n\tfor(int y = 0; y < SIZE; ++y) {\n\t\tfor(int x = 0; x < SIZE; ++x) {\n\t\t\tif(a[y][x].top == b[y][x].top) ++res;\n\t\t}\n\t}\n\n\treturn res;\n}\n\nint bfs(const state &start, const state &goal) {\n\tconstexpr int dx[] = {-1, 0, 1, 0};\n\tconstexpr int dy[] = {0, -1, 0, 1};\n\n\tqueue<unsigned long long> que;\n\tunordered_map<unsigned long long, int> dist;\n\n\tconst auto s_code = encode(start);\n\n\tque.push(s_code);\n\tdist[s_code] = 0;\n\n\twhile(!que.empty()) {\n\t\tconst auto code = que.front();\n\t\tque.pop();\n\n\t\tconst state current = decode(code);\n\t\tconst int c_dist = dist[code];\n\n\t\tconst int rest = 9 - match(current, goal);\n\t\tif(rest == 0) return c_dist;\n\t\tif(c_dist + rest - 1 > 30) continue;\n\n\t\tint ex = -1, ey = -1;\n\t\tfor(int y = 0; y < SIZE; ++y) {\n\t\t\tfor(int x = 0; x < SIZE; ++x) {\n\t\t\t\tif(current[y][x].top == E) {\n\t\t\t\t\tex = x;\n\t\t\t\t\tey = y;\n\t\t\t\t\tgoto unloop;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\n\tunloop:;\n\n\t\tfor(int d = 0; d < 4; ++d) {\n\t\t\tconst int nex = ex + dx[d];\n\t\t\tconst int ney = ey + dy[d];\n\n\t\t\tif(out(nex, ney)) continue;\n\n\t\t\tstate next(current);\n\t\t\tswap(next[ey][ex], next[ney][nex]);\n\n\t\t\tif(d & 1) { // vertical\n\t\t\t\tswap(next[ey][ex].top, next[ey][ex].south);\n\t\t\t}\n\t\t\telse { // horizontal\n\t\t\t\tnext[ey][ex].top = B + W + R - next[ey][ex].top - next[ey][ex].south;\n\t\t\t}\n\n\t\t\tconst auto n_code = encode(next);\n\t\t\tif(!dist.count(n_code)) {\n\t\t\t\tdist.insert({n_code, c_dist + 1});\n\t\t\t\tque.push(n_code);\n\t\t\t}\n\t\t}\n\t}\n\n\treturn -1;\n}\n\nint main() {\n\tcin.tie(nullptr);\n\tios::sync_with_stdio(false);\n\n\tint value[128];\n\n\tfor(int i = 0; i < static_cast<int>(str.size()); ++i) {\n\t\tvalue[str[i]] = i;\n\t}\n\n\tfor(int ex, ey; cin >> ex >> ey && ex;) {\n\t\tstate start;\n\t\tfor(int y = 0; y < SIZE; ++y) {\n\t\t\tfor(int x = 0; x < SIZE; ++x) {\n\t\t\t\tstart[y][x] = cube(W, R);\n\t\t\t}\n\t\t}\n\t\tstart[ey - 1][ex - 1] = cube(E, E);\n\n\t\tstate goal;\n\t\tfor(auto &row : goal) {\n\t\t\tfor(auto &e : row) {\n\t\t\t\tchar c;\n\t\t\t\tcin >> c;\n\t\t\t\te.top = value[c];\n\t\t\t}\n\t\t}\n\n\t\tcout << bfs(start, goal) << endl;\n\t}\n\n\treturn EXIT_SUCCESS;\n}", "accuracy": 1, "time_ms": 4760, "memory_kb": 61000, "score_of_the_acc": -1.5164, "final_rank": 17 }, { "submission_id": "aoj_1268_1094431", "code_snippet": "#include <algorithm>\n#include <cassert>\n#include <cctype>\n#include <cstdio>\n#include <complex>\n#include <iostream>\n#include <fstream>\n#include <iomanip>\n#include <vector>\n#include <map>\n#include <set>\n#include <queue>\n#include <bitset>\n#include <stack>\n#include <utility>\n#include <numeric>\n#include <functional>\n#include <string>\n#include <sstream>\n// #include <tuple>\n// #include <regex>\n\nusing namespace std;\ntypedef long long ll;\n\n#define ALL(c) (c).begin(),(c).end()\n#define FOR(i,l,r) for(int i=(int)l;i<(int)r;++i)\n#define REP(i,n) FOR(i,0,n)\n#define FORr(i,l,r) for(int i=(int)r-1;i>=(int)l;--i)\n#define REPr(i,n) FORr(i,0,n)\n#define EACH(i,c) for(__typeof((c).begin()) i=(c).begin(); i!=(c).end(); ++i)\n#define IN(l,v,r) ((l)<=(v) && (v)<(r))\n\ntemplate<typename T,typename U> T pmod(T x,U M){return (x%M+M)%M;}\n\nstringstream ss;\n\nconst int H=3,W=3;\nvector<vector<short>> goal;\nvector<vector<short>> ts,ys,xs;\n\nmap<vector<vector<short>>,int> vmap;\nmap<vector<vector<short>>,int> passed;\n\nint INF=1<<28;\nint dy[4]={0,1,0,-1},dx[4]={1,0,-1,0};\nint dfs(const int y,const int x,const int py,const int px,const int dep){\n\tif(dep>30)return INF;\n\tint wa=0;REP(_y,H)REP(_x,W)if(goal[_y][_x]!=ts[_y][_x])wa++;\n\tif(wa==0)return dep;\n\tif(wa>30-dep+1)return INF;\n\t\n\tint res=INF;\n\tREP(d,4){\n\t\tconst int ny=y+dy[d],nx=x+dx[d];\n\t\tif(!IN(0,ny,H) || !IN(0,nx,W))continue;\n\t\tif(ny==py && nx ==px)continue;\n\t\tif(y==ny){//rotate X\n\t\t\tswap(xs[ny][nx],ts[y][x]);\n\t\t\tswap(ys[ny][nx],ys[y][x]);\n\t\t\tswap(ts[ny][nx],xs[y][x]);\n\t\t\tres=min(res,dfs(ny,nx,y,x,dep+1));\n\t\t\tswap(xs[ny][nx],ts[y][x]);\n\t\t\tswap(ys[ny][nx],ys[y][x]);\n\t\t\tswap(ts[ny][nx],xs[y][x]);\n\t\t}else{//rotate Y\n\t\t\tswap(ys[ny][nx],ts[y][x]);\n\t\t\tswap(xs[ny][nx],xs[y][x]);\n\t\t\tswap(ts[ny][nx],ys[y][x]);\t\t\n\t\t\tres=min(res,dfs(ny,nx,y,x,dep+1));\n\t\t\tswap(ys[ny][nx],ts[y][x]);\n\t\t\tswap(xs[ny][nx],xs[y][x]);\n\t\t\tswap(ts[ny][nx],ys[y][x]);\n\t\t}\n\t}\n\treturn res;\n}\nshort enc(char c){\n\tif(c=='B')return 0;\n\tif(c=='W')return 1;\n\tif(c=='R')return 2;\n\treturn 3;\n}\n\nint main(){\n // \tifstream cin(\"in\");\n\t// ofstream cout( \"out\" );\n\tcin.tie(0);\n\tios::sync_with_stdio(false);\n\tss <<fixed <<setprecision(20);\n\tcout <<fixed <<setprecision(20);\n\tcerr <<fixed <<setprecision(20);\n\n\twhile(true){\n\t\tint X,Y;cin >> X >> Y;if(Y==0 && X==0)break; Y--;X--;\n\t\tts=vector<vector<short>>(H,vector<short>(W));\n\t\txs=ys=vector<vector<short>>(H,vector<short>(W));\n\t\tgoal=vector<vector<short>>(H,vector<short>(W));\n\t\tREP(y,H)REP(x,W){char c;cin >> c;goal[y][x]=enc(c);}\n\t\tREP(y,H)REP(x,W)if(y==Y && x==X) ys[y][x]=3; else ys[y][x]=2;\n\t\tREP(y,H)REP(x,W)if(y==Y && x==X) xs[y][x]=3; else xs[y][x]=0;\n\t\tREP(y,H)REP(x,W)if(y==Y && x==X) ts[y][x]=3; else ts[y][x]=1;\n\t\tint res=dfs(Y,X,-1,-1,0);\n\t\tif(res==INF)cout << -1 <<endl;\n\t\telse cout << res <<endl;\n\t}\n\n \treturn 0;\n }", "accuracy": 1, "time_ms": 1850, "memory_kb": 1224, "score_of_the_acc": -0.2026, "final_rank": 10 }, { "submission_id": "aoj_1268_1061361", "code_snippet": "#include<iostream>\n#include<set>\n#include<array>\n#include<cstdlib>\n\nusing namespace std;\n\ntypedef array<array<signed char,3>,3> Die;\n\nint main(){\n for(int x,y;cin>>x>>y,x;){\n char g[3][3];\n bool nz=false;\n for(int i=0;i<3;i++){\n for(int j=0;j<3;j++){\n\tcin>>g[i][j];\n\tif(g[i][j]=='E'){\n\t nz|=i!=y-1||j!=x-1;\n\t}else{\n\t nz|=g[i][j]!='W';\n\t}\n }\n }\n if(!nz){\n cout<<0<<endl;\n }else{\n set<Die> s[32];\n Die d{};\n d[y-1][x-1]=-1;\n s[0].insert(d);\n for(int i=0;i<=30;i++){\n\t//\tcout<<\"c = \"<<s[i].size()<<endl;\n\tfor(auto e:s[i]){\n\t int ey,ex;\n\t for(int k=0;k<3;k++){\n\t for(int j=0;j<3;j++){\n\t if(e[k][j]==-1){\n\t\tey=k;\n\t\tex=j;\n\t }\n\t }\n\t }\n\t if(i<30){\n\t static int dy[]={-1,0,1,0};\n\t static int dx[]={0,1,0,-1};\n\t for(int j=0;j<4;j++){\n\t int ny=ey+dy[j];\n\t int nx=ex+dx[j];\n\t if(0<=ny&&ny<3&&0<=nx&&nx<3){\n\t\tauto n=e;\n\t\tn[ny][nx]=-1;\n\t\tn[ey][ex]=(e[ny][nx]+((e[ny][nx]%2^j%2)?1:5))%6;\n\t \n\t\tbool f=false;\n\t\tint c=0;\n\t\tint gy,gx;\n\t\tfor(int jj=0;jj<3;jj++){\n\t\t for(int k=0;k<3;k++){\n\t\t if(n[jj][k]==-1){\n\t\t f|=g[jj][k]!='E';\n\t\t }else{\n\t\t f|=g[jj][k]!=\"WBRWBR\"[n[jj][k]];\n\t\t }\n\t\t c+=g[jj][k]!='E'&&(n[jj][k]==-1||g[jj][k]!=\"WBRWBR\"[n[jj][k]]);\n\t\t if(g[jj][k]=='E'){\n\t\t gy=jj;\n\t\t gx=k;\n\t\t }\n\t\t }\n\t\t}\n\t\tif(!f){\n\t\t cout<<i+1<<endl;\n\t\t goto next;\n\t\t}\n\t\tif(max(c,abs(ny-gy)+abs(nx-gx))+i+1>30)continue;\n\t\ts[i+1].insert(n);\n\t }\n\t }\n\t }\n\t}\n\ts[i].clear();\n }\n cout<<-1<<endl;\n next:\n ;\n }\n }\n}", "accuracy": 1, "time_ms": 6990, "memory_kb": 37152, "score_of_the_acc": -1.4866, "final_rank": 16 }, { "submission_id": "aoj_1268_905322", "code_snippet": "#include<bits/stdc++.h>\n\n#define REP(i,s,n) for(int i=s;i<n;i++)\n#define rep(i,n) REP(i,0,n)\n#define IINF (INT_MAX)\n\nusing namespace std;\n\ntypedef long long ll;\n\nenum FACE { TOP, BOTTOM, FRONT, BACK, LEFT, RIGHT };\ntemplate <class T>\nclass Dice {\npublic:\n Dice() {\n id[TOP] = 0; id[FRONT] = 1; id[LEFT] = 2;\n id[RIGHT] = 3; id[BACK] = 4; id[BOTTOM] = 5;\n }\n T& operator[] (FACE f) { return var[id[f]]; }\n const T& operator[] (FACE f) const { return var[id[f]]; }\n bool operator==(const Dice<T>& b) const {\n const Dice<T> &a = *this;\n return a[TOP] == b[TOP] && a[BOTTOM] == b[BOTTOM] &&\n a[FRONT] == b[FRONT] && a[BACK] == b[BACK] &&\n a[LEFT] == b[LEFT] && a[RIGHT] == b[RIGHT];\n }\n void roll_x() { roll(TOP, BACK, BOTTOM, FRONT); }\n void roll_y() { roll(TOP, LEFT, BOTTOM, RIGHT); }\n void roll_z() { roll(FRONT, RIGHT, BACK, LEFT); }\n\nprivate:\n void roll(FACE a, FACE b, FACE c, FACE d) {\n T tmp = id[a];\n id[a] = id[b]; id[b] = id[c];\n id[c] = id[d]; id[d] = tmp;\n }\n T var[6];\n int id[6];\n};\n\nFACE face[] = {TOP,BOTTOM,FRONT,BACK,LEFT,RIGHT};\n\ninline int toInt(char c){ return ((c=='B')?0:((c=='E')?1:((c=='R')?2:3))); }\nchar F[3][3];\nchar initial_color[] = {'W','W','R','R','B','B'};\nint mincost;\nint dx[] = {1,0,-1,0}; // R, B, L, T\nint dy[] = {0,1,0,-1};\n\ninline void swap_dice(Dice<char> dice[3][3],int x1,int y1,int x2,int y2,int type){\n if( type == 0 ) rep(i,3)dice[y2][x2].roll_y();\n if( type == 1 ) rep(i,3)dice[y2][x2].roll_x();\n if( type == 2 ) dice[y2][x2].roll_y();\n if( type == 3 ) dice[y2][x2].roll_x();\n swap(dice[y1][x1],dice[y2][x2]);\n}\n\ninline int finish(Dice<char> dice[3][3]){\n int diff = 0;\n rep(i,3)rep(j,3)if( dice[i][j][TOP] != F[i][j] ) diff++;\n return diff;\n}\n\nvoid dfs(Dice<char> dice[3][3],int x,int y,int cost,int pre){\n\n if( cost >= mincost) return;\n\n int tmp;\n if( ( tmp = finish(dice) ) == 0 ){\n mincost = min(mincost,cost);\n return;\n }\n\n if( tmp )tmp--;\n if( cost + tmp >= mincost ) return;\n\n rep(i,4){\n if( i == pre ) continue;\n int nx = x + dx[i], ny = y + dy[i];\n if( !( 0 <= nx && nx < 3 && 0 <= ny && ny < 3 ) ) continue;\n swap_dice(dice,x,y,nx,ny,i);\n dfs(dice,nx,ny,cost+1,(i+2)%4);\n swap_dice(dice,nx,ny,x,y,(i+2)%4);\n }\n}\n\nint main(){\n\n int x,y;\n while(scanf(\"%d%d\",&x,&y),x|y){\n x--,y--;\n mincost = 31;\n rep(i,3)rep(j,3)cin >> F[i][j];\n\n Dice<char> dice[3][3];\n rep(i,3)rep(j,3)rep(k,6)dice[i][j][face[k]] = ((i==y&&j==x)?'E':initial_color[k]);\n\n dfs(dice,x,y,0,-1);\n\n if( mincost == 31 ) puts(\"-1\");\n else printf(\"%d\\n\",mincost);\n\n }\n return 0;\n}", "accuracy": 1, "time_ms": 800, "memory_kb": 1216, "score_of_the_acc": -0.0494, "final_rank": 4 }, { "submission_id": "aoj_1268_905320", "code_snippet": "#include<bits/stdc++.h>\n\n#define REP(i,s,n) for(int i=s;i<n;i++)\n#define rep(i,n) REP(i,0,n)\n#define IINF (INT_MAX)\n#define LEN 2\n\nusing namespace std;\n\ntypedef long long ll;\n\nenum FACE { TOP, BOTTOM, FRONT, BACK, LEFT, RIGHT };\ntemplate <class T>\nclass Dice {\npublic:\n Dice() {\n id[TOP] = 0; id[FRONT] = 1; id[LEFT] = 2;\n id[RIGHT] = 3; id[BACK] = 4; id[BOTTOM] = 5;\n }\n T& operator[] (FACE f) { return var[id[f]]; }\n const T& operator[] (FACE f) const { return var[id[f]]; }\n bool operator==(const Dice<T>& b) const {\n const Dice<T> &a = *this;\n return a[TOP] == b[TOP] && a[BOTTOM] == b[BOTTOM] &&\n a[FRONT] == b[FRONT] && a[BACK] == b[BACK] &&\n a[LEFT] == b[LEFT] && a[RIGHT] == b[RIGHT];\n }\n void roll_x() { roll(TOP, BACK, BOTTOM, FRONT); }\n void roll_y() { roll(TOP, LEFT, BOTTOM, RIGHT); }\n void roll_z() { roll(FRONT, RIGHT, BACK, LEFT); }\n vector<Dice> all_rolls() {\n vector<Dice> ret;\n for (int k = 0; k < 6; (k&1?roll_y():roll_x()),++k)\n for (int i = 0; i < 4; roll_z(), ++i)\n ret.push_back(*this);\n return ret;\n }\n bool equivalent_to(const Dice& di) {\n for (int k = 0; k < 6; (k&1?roll_y():roll_x()),++k)\n for (int i = 0; i < 4; roll_z(), ++i)\n if (*this == di) return true;\n return false;\n }\nprivate:\n void roll(FACE a, FACE b, FACE c, FACE d) {\n T tmp = id[a];\n id[a] = id[b]; id[b] = id[c];\n id[c] = id[d]; id[d] = tmp;\n }\n T var[6];\n int id[6];\n};\n\nFACE face[] = {TOP,BOTTOM,FRONT,BACK,LEFT,RIGHT};\n\nint around[6][4] = \n {\n {1,2,4,3},\n {0,3,5,2},\n {0,1,5,4},\n {0,4,5,1},\n {0,2,5,3},\n {1,3,4,2}\n };\n\n\nDice<int> makeDice(int top,int front){\n Dice<int> ret;\n ret[TOP] = top, ret[BOTTOM] = 5-top;\n ret[FRONT] = front, ret[BACK] = 5-front;\n int idx = IINF;\n for(int i=0;i<4;i++){\n if(around[top][i] == front){\n idx = i+1;\n break;\n }\n }\n assert(idx != IINF);\n idx %= 4;\n ret[RIGHT] = around[top][idx], ret[LEFT] = 5-around[top][idx];\n return ret;\n}\n\ninline int toInt(char c){ return ((c=='B')?0:((c=='E')?1:((c=='R')?2:3))); }\nchar F[3][3];\nchar initial_color[] = {'W','W','R','R','B','B'};\nint mincost;\nint dx[] = {1,0,-1,0}; // R, B, L, T\nint dy[] = {0,1,0,-1};\n\ninline void swap_dice(Dice<char> dice[3][3],int x1,int y1,int x2,int y2,int type){\n if( type == 0 ) rep(i,3)dice[y2][x2].roll_y();\n if( type == 1 ) rep(i,3)dice[y2][x2].roll_x();\n if( type == 2 ) dice[y2][x2].roll_y();\n if( type == 3 ) dice[y2][x2].roll_x();\n swap(dice[y1][x1],dice[y2][x2]);\n}\n\ninline int finish(Dice<char> dice[3][3]){\n int diff = 0;\n rep(i,3)rep(j,3)if( dice[i][j][TOP] != F[i][j] ) diff++;\n return diff;\n}\n\nvoid dfs(Dice<char> dice[3][3],int x,int y,int cost,int pre){\n\n if( cost >= mincost) return;\n\n int tmp;\n if( ( tmp = finish(dice) ) == 0 ){\n mincost = min(mincost,cost);\n return;\n }\n\n if( tmp )tmp--;\n if( cost + tmp >= mincost ) return;\n\n rep(i,4){\n if( i == pre ) continue;\n int nx = x + dx[i], ny = y + dy[i];\n if( !( 0 <= nx && nx < 3 && 0 <= ny && ny < 3 ) ) continue;\n swap_dice(dice,x,y,nx,ny,i);\n dfs(dice,nx,ny,cost+1,(i+2)%4);\n swap_dice(dice,nx,ny,x,y,(i+2)%4);\n }\n}\n\nint main(){\n\n int x,y;\n while(cin>>x>>y,x|y){\n x--,y--;\n mincost = 31;\n rep(i,3)rep(j,3)cin >> F[i][j];\n\n Dice<char> dice[3][3];\n rep(i,3)rep(j,3)rep(k,6)dice[i][j][face[k]] = initial_color[k];\n rep(k,6)dice[y][x][face[k]] = 'E';\n\n dfs(dice,x,y,0,-1);\n\n if( mincost == 31 ) puts(\"-1\");\n else printf(\"%d\\n\",mincost);\n\n }\n return 0;\n}", "accuracy": 1, "time_ms": 800, "memory_kb": 1156, "score_of_the_acc": -0.0486, "final_rank": 3 }, { "submission_id": "aoj_1268_905319", "code_snippet": "#include<bits/stdc++.h>\n\n#define REP(i,s,n) for(int i=s;i<n;i++)\n#define rep(i,n) REP(i,0,n)\n#define IINF (INT_MAX)\n#define LEN 2\n\nusing namespace std;\n\ntypedef long long ll;\n\nenum FACE { TOP, BOTTOM, FRONT, BACK, LEFT, RIGHT };\ntemplate <class T>\nclass Dice {\npublic:\n Dice() {\n id[TOP] = 0; id[FRONT] = 1; id[LEFT] = 2;\n id[RIGHT] = 3; id[BACK] = 4; id[BOTTOM] = 5;\n }\n T& operator[] (FACE f) { return var[id[f]]; }\n const T& operator[] (FACE f) const { return var[id[f]]; }\n bool operator==(const Dice<T>& b) const {\n const Dice<T> &a = *this;\n return a[TOP] == b[TOP] && a[BOTTOM] == b[BOTTOM] &&\n a[FRONT] == b[FRONT] && a[BACK] == b[BACK] &&\n a[LEFT] == b[LEFT] && a[RIGHT] == b[RIGHT];\n }\n void roll_x() { roll(TOP, BACK, BOTTOM, FRONT); }\n void roll_y() { roll(TOP, LEFT, BOTTOM, RIGHT); }\n void roll_z() { roll(FRONT, RIGHT, BACK, LEFT); }\n vector<Dice> all_rolls() {\n vector<Dice> ret;\n for (int k = 0; k < 6; (k&1?roll_y():roll_x()),++k)\n for (int i = 0; i < 4; roll_z(), ++i)\n ret.push_back(*this);\n return ret;\n }\n bool equivalent_to(const Dice& di) {\n for (int k = 0; k < 6; (k&1?roll_y():roll_x()),++k)\n for (int i = 0; i < 4; roll_z(), ++i)\n if (*this == di) return true;\n return false;\n }\nprivate:\n void roll(FACE a, FACE b, FACE c, FACE d) {\n T tmp = id[a];\n id[a] = id[b]; id[b] = id[c];\n id[c] = id[d]; id[d] = tmp;\n }\n T var[6];\n int id[6];\n};\n\nFACE face[] = {TOP,BOTTOM,FRONT,BACK,LEFT,RIGHT};\n\nint around[6][4] = \n {\n {1,2,4,3},\n {0,3,5,2},\n {0,1,5,4},\n {0,4,5,1},\n {0,2,5,3},\n {1,3,4,2}\n };\n\n\nDice<int> makeDice(int top,int front){\n Dice<int> ret;\n ret[TOP] = top, ret[BOTTOM] = 5-top;\n ret[FRONT] = front, ret[BACK] = 5-front;\n int idx = IINF;\n for(int i=0;i<4;i++){\n if(around[top][i] == front){\n idx = i+1;\n break;\n }\n }\n assert(idx != IINF);\n idx %= 4;\n ret[RIGHT] = around[top][idx], ret[LEFT] = 5-around[top][idx];\n return ret;\n}\n\ninline int toInt(char c){ return ((c=='B')?0:((c=='E')?1:((c=='R')?2:3))); }\nchar F[3][3];\nchar initial_color[] = {'W','W','R','R','B','B'};\nint mincost;\nint dx[] = {1,0,-1,0}; // R, B, L, T\nint dy[] = {0,1,0,-1};\n\ninline void swap_hash(ll &hash,int x1,int y1,int x2,int y2){\n int pos1 = x1 + y1 * 3, pos2 = x2 + y2 * 3;\n ll bitmask1 = (1LL<<4LL)-1LL;\n ll bitmask2 = ~( ( bitmask1<<(pos1*4LL) ) | ( bitmask1<<(pos2*4LL) ) );\n ll tmp1 = ( ( ( bitmask1<<(pos1*4LL) ) & hash ) >> (pos1*4LL) );\n ll tmp2 = ( ( ( bitmask1<<(pos2*4LL) ) & hash ) >> (pos2*4LL) );\n ll tmp3 = hash & bitmask2;\n assert( hash == (tmp3|(tmp1<<(pos1*4LL))|(tmp2<<(pos2*4LL)) ) );\n tmp3 |= (tmp1<<(pos2*4LL)) | (tmp2<<(pos1*4LL));\n}\n\ninline void hash_set(ll &hash,int x,int y,char fr,char rg){\n int pos = x + y * 3;\n ll bitmask = (1LL<<4LL)-1LL;\n ll rbitmask = ~(bitmask<<(pos*4LL));\n hash &= rbitmask;\n ll add1 = (toInt(rg))<<(pos*4+3);\n ll add2 = (toInt(fr))<<(pos*4);\n hash = hash|add1|add2;\n}\n\ninline void swap_dice(Dice<char> dice[3][3],int x1,int y1,int x2,int y2,int type){\n if( type == 0 ) rep(i,3)dice[y2][x2].roll_y();\n if( type == 1 ) rep(i,3)dice[y2][x2].roll_x();\n if( type == 2 ) dice[y2][x2].roll_y();\n if( type == 3 ) dice[y2][x2].roll_x();\n swap(dice[y1][x1],dice[y2][x2]);\n}\n\ninline int finish(Dice<char> dice[3][3]){\n int diff = 0;\n rep(i,3)rep(j,3)if( dice[i][j][TOP] != F[i][j] ) diff++;\n return diff;\n}\n\nvoid dfs(Dice<char> dice[3][3],ll hash,int x,int y,int cost,int pre){\n /*\n cout << \"cost = \" << cost << \" empty(\" << x << \",\" << y << \")\" << endl;\n bitset<63> BIT(hash);\n cout << \"hash : \" << BIT << endl;\n rep(i,3){\n rep(j,3){\n cout << dice[i][j][TOP];\n }\n cout << endl;\n }\n */\n if( cost >= mincost) return;\n\n int tmp;\n if( ( tmp = finish(dice) ) == 0 ){\n mincost = min(mincost,cost);\n return;\n }\n\n if( tmp )tmp--;\n if( cost + tmp >= mincost ) return;\n\n rep(i,4){\n if( i == pre ) continue;\n int nx = x + dx[i], ny = y + dy[i];\n if( !( 0 <= nx && nx < 3 && 0 <= ny && ny < 3 ) ) continue;\n swap_dice(dice,x,y,nx,ny,i);\n ll new_hash = hash;\n hash_set(new_hash,x,y,dice[y][x][TOP],dice[y][x][RIGHT]);\n hash_set(new_hash,nx,ny,dice[ny][nx][TOP],dice[ny][nx][RIGHT]);\n swap_hash(new_hash,x,y,nx,ny);\n dfs(dice,new_hash,nx,ny,cost+1,(i+2)%4);\n swap_dice(dice,nx,ny,x,y,(i+2)%4);\n }\n\n}\n\nint main(){\n /*\n Dice<char> d;\n string LINE[6] = {\"TOP \",\"BOTTOM\",\"FRONT \",\"BACK \",\"LEFT \",\"RIGHT \"};\n rep(i,6) d[face[i]] = initial_color[i];\n rep(i,6) cout << LINE[i] << \" : \" << d[face[i]] << endl;\n d.roll_z();\n cout << endl;\n rep(i,6) cout << LINE[i] << \" : \" << d[face[i]] << endl;\n */\n\n int x,y;\n while(cin>>x>>y,x|y){\n x--,y--;\n mincost = 31;\n rep(i,3)rep(j,3)cin >> F[i][j];\n\n Dice<char> dice[3][3];\n rep(i,3)rep(j,3)rep(k,6)dice[i][j][face[k]] = initial_color[k];\n rep(k,6)dice[y][x][face[k]] = 'E';\n\n ll hash = 0LL;\n for(int i=2;i>=0;i--)for(int j=2;j>=0;j--){\n\thash <<= LEN;\n\thash += toInt(dice[i][j][RIGHT]);\n\thash <<= LEN;\n\thash += toInt(dice[i][j][TOP]);\n }\n\n dfs(dice,hash,x,y,0,-1);\n\n if( mincost == 31 ) puts(\"-1\");\n else printf(\"%d\\n\",mincost);\n\n }\n return 0;\n}", "accuracy": 1, "time_ms": 1180, "memory_kb": 1156, "score_of_the_acc": -0.1039, "final_rank": 7 }, { "submission_id": "aoj_1268_904423", "code_snippet": "#include<iostream>\n#include<cmath>\n\nusing namespace std;\n#define LIMIT 30\n \nstruct Puzzle {\n char cont[9][3];\n int space; \n};\n \n \nint dx[4]={ 0, 1, 0,-1};\nint dy[4]={-1, 0, 1, 0};\n \nint limit;\nPuzzle puzzle;\nchar target[9];\n \n \nint getHeuristic(){\n int sum = 0;\n for(int i=0;i<9;i++){\n if(puzzle.cont[i][0]=='E')continue;\n sum+=(puzzle.cont[i][0]!=target[i]?1:0);\n }\n return sum;\n}\n \nbool dfs(int depth,int prev){\n\n int heuri=getHeuristic();\n\n if(heuri==0){return true;}\n if(depth+heuri> limit )return false;\n \n \n int px,py,nx,ny;\n Puzzle tmp;\n \n px=puzzle.space%3;\n py=puzzle.space/3;\n\n tmp=puzzle;\n for(int i=0;i<4;i++){\n nx=px+dx[i];\n ny=py+dy[i];\n if(max(prev,i)-min(prev,i)==2)continue;\n \n if(nx<0||ny<0||nx>=3||ny>=3)continue;\n \n \n \n if(i%2){\n // puzzle.cont[py*3+px]=rb(puzzle.cont[ny*3+nx]);\n puzzle.cont[py*3+px][0]=puzzle.cont[ny*3+nx][2];\n puzzle.cont[py*3+px][1]=puzzle.cont[ny*3+nx][1];\n puzzle.cont[py*3+px][2]=puzzle.cont[ny*3+nx][0];\n }else{\n // puzzle.cont[py*3+px]=ra(puzzle.cont[ny*3+nx]);\n puzzle.cont[py*3+px][0]=puzzle.cont[ny*3+nx][1];\n puzzle.cont[py*3+px][1]=puzzle.cont[ny*3+nx][0];\n puzzle.cont[py*3+px][2]=puzzle.cont[ny*3+nx][2];\n }\n \n puzzle.space=ny*3+nx;\n puzzle.cont[puzzle.space][0]='E';\n\n\n \n \n if(dfs(depth+1,i))return true;\n puzzle=tmp;\n }\n return false;\n}\n\nPuzzle in;\nvoid solve(){\n int d;\n d=getHeuristic();\n for(limit=d;limit <= LIMIT ; limit++ ){\n \n if(dfs(0,-100)){\n cout<<limit<<endl;\n return;\n }\n \n }\n cout<<\"-1\"<<endl;\n \n}\n \nint main(){\n int x,y;\n while(cin>>x>>y){\n if(x==0&&y==0)break;\n x--;y--;\n \n puzzle.space=y*3+x;\n for(int i=0;i<9;i++){\n cin>>target[i];\n if(i/3==y&&i%3==x){\n\tpuzzle.cont[i][0]='E';\n }else{\n\tpuzzle.cont[i][0]='W';\n\tpuzzle.cont[i][1]='R';\n\tpuzzle.cont[i][2]='B';\n }\n }\n solve(); \n }\n return 0;\n}", "accuracy": 1, "time_ms": 910, "memory_kb": 1160, "score_of_the_acc": -0.0646, "final_rank": 5 }, { "submission_id": "aoj_1268_626228", "code_snippet": "#include <iostream>\n#include <sstream>\n#include <string>\n#include <algorithm>\n#include <vector>\n#include <stack>\n#include <queue>\n#include <set>\n#include <map>\n#include <cstdio>\n#include <cstdlib>\n#include <cstring>\n#include <cmath>\n#include <cassert>\n\nusing namespace std;\n\n#define FOR(i,k,n) for(int i=(k); i<(int)(n); ++i)\n#define REP(i,n) FOR(i,0,n)\n#define FORIT(i,c) for(__typeof((c).begin())i=(c).begin();i!=(c).end();++i)\n\ntemplate<class T> void debug(T begin, T end){ for(T i = begin; i != end; ++i) cerr<<*i<<\" \"; cerr<<endl; }\ninline bool valid(int x, int y, int W, int H){ return (x >= 0 && y >= 0 && x < W && y < H); }\n\ntypedef long long ll;\nconst int INF = 100000000;\nconst double EPS = 1e-8;\nconst int MOD = 1000000007;\nint dx[8] = {1, 0, -1, 0, 1, -1, -1, 1};\nint dy[8] = {0, 1, 0, -1, 1, 1, -1, -1};\n// w, r, b\n// w, b, r\n// r, w, b\n// r, b, w\n// b, w, r\n// b, r, w\nint hori[6] = {5, 3, 4, 1, 2, 0};\nint vert[6] = {2, 4, 0, 5, 1, 3};\nint top[6] = {0, 0, 1, 1, 2, 2};\nchar WRB[] = \"WRB\";\nstruct S{\n vector<int> grid;\n int time;\n};\nint main(){\n // white, red, blue\n int sx, sy;\n while(cin >> sx >> sy && sx){\n sx--; sy--;\n char goal[3][3];\n REP(i, 3) REP(j, 3) cin >> goal[i][j];\n vector<int> goal_v;\n REP(i, 3) REP(j, 3) {\n if(goal[i][j] == 'E') goal_v.push_back(-1);\n REP(k, 3) if(WRB[k] == goal[i][j]) goal_v.push_back(k);\n }\n S start;\n start.time = 0;\n REP(i, 3) REP(j, 3){\n if(sy == i && sx == j) start.grid.push_back(-1);\n else start.grid.push_back(0);\n }\n queue<S> que;\n que.push(start);\n static bool used[1700000][9] = {};\n memset(used, 0, sizeof(used));\n int ans = -1;\n int pow6[10] = {};\n pow6[0] = 1; REP(i, 9) pow6[i + 1] = pow6[i] * 6;\n while(!que.empty()){\n S s = que.front(); que.pop();\n if(s.time > 30) break;\n int hstar = 0;\n REP(i, 9) if(s.grid[i] != -1 && top[s.grid[i]] != goal_v[i]) hstar ++;\n if(hstar == 0){\n ans = s.time;\n break;\n }\n if(s.time + hstar > 30) continue;\n REP(y, 3) REP(x, 3) {\n if(s.grid[y * 3 + x] == -1){\n REP(r, 4){\n int fx = x + dx[r];\n int fy = y + dy[r];\n if(!valid(fx, fy, 3, 3)) continue;\n S next = s;\n next.time++;\n next.grid[y * 3 + x] = (r % 2 ? vert[s.grid[fy * 3 + fx]] : hori[s.grid[fy * 3 + fx]]);\n next.grid[fy * 3 + fx] = -1;\n int hash = 0;\n int c = 0;\n REP(i, 9) if(next.grid[i] != -1) hash += next.grid[i] * pow6[c++];\n if(used[hash][fy * 3 + fx]) continue;\n used[hash][fy * 3 + fx] = true;\n que.push(next);\n }\n }\n }\n }\n cout << ans << endl;\n }\n return 0;\n}", "accuracy": 1, "time_ms": 1920, "memory_kb": 35824, "score_of_the_acc": -0.7277, "final_rank": 13 }, { "submission_id": "aoj_1268_563955", "code_snippet": "#include<cstdio>\n#include<algorithm>\n\n#define rep(i,n) for(int i=0;i<(n);i++)\n\nusing namespace std;\n\nchar goal[3][3],gx,gy;\n\nchar T[3][3],F[3][3],R[3][3]; // top, front, right\n\nint hstar(int ex,int ey,int gx,int gy){\n\tint cnt=0;\n\trep(i,3) rep(j,3) if(!(i==ey && j==ex) && T[i][j]!=goal[i][j]) cnt++;\n\treturn max(cnt,abs(ex-gx)+abs(ey-gy));\n}\n\nint ub;\nbool dfs(int t,int ex,int ey,int pre){ // 経過ターン, スペースの位置, 直前移動した方向\n\tbool end=true;\n\trep(i,3) rep(j,3) if(!(i==ey && j==ex) && T[i][j]!=goal[i][j]) end=false;\n\tif(end) return true;\n\n\tif(t+hstar(ex,ey,gx,gy)>ub) return false;\n\n\tchar tmp1,tmp2,tmp3;\n\t// (ex,ey-1)\n\tif(ey>0 && pre!=1){\n\t\ttmp1=T[ey][ex]=F[ey-1][ex];\n\t\ttmp2=F[ey][ex]=T[ey-1][ex];\n\t\ttmp3=R[ey][ex]=R[ey-1][ex];\n\t\tif(dfs(t+1,ex,ey-1,0)) return true;\n\t\tF[ey-1][ex]=tmp1;\n\t\tT[ey-1][ex]=tmp2;\n\t\tR[ey-1][ex]=tmp3;\n\t}\n\t// (ex,ey+1)\n\tif(ey<2 && pre!=0){\n\t\ttmp1=T[ey][ex]=F[ey+1][ex];\n\t\ttmp2=F[ey][ex]=T[ey+1][ex];\n\t\ttmp3=R[ey][ex]=R[ey+1][ex];\n\t\tif(dfs(t+1,ex,ey+1,1)) return true;\n\t\tF[ey+1][ex]=tmp1;\n\t\tT[ey+1][ex]=tmp2;\n\t\tR[ey+1][ex]=tmp3;\n\t}\n\t// (ex-1,ey)\n\tif(ex>0 && pre!=3){\n\t\ttmp1=T[ey][ex]=R[ey][ex-1];\n\t\ttmp2=F[ey][ex]=F[ey][ex-1];\n\t\ttmp3=R[ey][ex]=T[ey][ex-1];\n\t\tif(dfs(t+1,ex-1,ey,2)) return true;\n\t\tR[ey][ex-1]=tmp1;\n\t\tF[ey][ex-1]=tmp2;\n\t\tT[ey][ex-1]=tmp3;\n\t}\n\t// (ex+1,ey)\n\tif(ex<2 && pre!=2){\n\t\ttmp1=T[ey][ex]=R[ey][ex+1];\n\t\ttmp2=F[ey][ex]=F[ey][ex+1];\n\t\ttmp3=R[ey][ex]=T[ey][ex+1];\n\t\tif(dfs(t+1,ex+1,ey,3)) return true;\n\t\tR[ey][ex+1]=tmp1;\n\t\tF[ey][ex+1]=tmp2;\n\t\tT[ey][ex+1]=tmp3;\n\t}\n\treturn false;\n}\n\nint main(){\n\tfor(int ex,ey;scanf(\"%d%d\",&ex,&ey),ex;){\n\t\tex--; ey--;\n\t\trep(i,3) rep(j,3) {\n\t\t\tscanf(\" %c\",goal[i]+j);\n\t\t\tif(goal[i][j]=='E') gx=j, gy=i;\n\t\t\tT[i][j]='W';\n\t\t\tF[i][j]='R';\n\t\t\tR[i][j]='B';\n\t\t}\n\n\t\tfor(ub=(abs(ex-gx)+abs(ey-gy))%2;ub<=30&&!dfs(0,ex,ey,-1);ub+=2);\n\n\t\tprintf(\"%d\\n\",ub<=30?ub:-1);\n\t}\n\n\treturn 0;\n}", "accuracy": 1, "time_ms": 580, "memory_kb": 1028, "score_of_the_acc": -0.0146, "final_rank": 2 }, { "submission_id": "aoj_1268_563946", "code_snippet": "#include<cstdio>\n#include<algorithm>\n\n#define rep(i,n) for(int i=0;i<(n);i++)\n\nusing namespace std;\n\nchar goal[3][3],gx,gy;\n\nchar T[3][3],F[3][3],R[3][3]; // top, front, right\n\nint hstar(int ex,int ey,int gx,int gy){\n\tint cnt=0;\n\trep(i,3) rep(j,3) if(!(i==ey && j==ex) && T[i][j]!=goal[i][j]) cnt++;\n\treturn max(cnt,abs(ex-gx)+abs(ey-gy));\n}\n\nint ub;\nbool dfs(int t,int ex,int ey,int pre){ // 経過ターン, スペースの位置, 直前移動した方向\n\tbool end=true;\n\trep(i,3) rep(j,3) if(!(i==ey && j==ex) && T[i][j]!=goal[i][j]) end=false;\n\tif(end) return true;\n\n\tif(t+hstar(ex,ey,gx,gy)>ub) return false;\n\n\t// (ex,ey-1)\n\tchar tmp1,tmp2,tmp3;\n\tif(ey>0 && pre!=1){\n\t\ttmp1=T[ey][ex]=F[ey-1][ex];\n\t\ttmp2=F[ey][ex]=T[ey-1][ex];\n\t\ttmp3=R[ey][ex]=R[ey-1][ex];\n\t\tif(dfs(t+1,ex,ey-1,0)) return true;\n\t\tF[ey-1][ex]=tmp1;\n\t\tT[ey-1][ex]=tmp2;\n\t\tR[ey-1][ex]=tmp3;\n\t}\n\t// (ex,ey+1)\n\tif(ey<2 && pre!=0){\n\t\ttmp1=T[ey][ex]=F[ey+1][ex];\n\t\ttmp2=F[ey][ex]=T[ey+1][ex];\n\t\ttmp3=R[ey][ex]=R[ey+1][ex];\n\t\tif(dfs(t+1,ex,ey+1,1)) return true;\n\t\tF[ey+1][ex]=tmp1;\n\t\tT[ey+1][ex]=tmp2;\n\t\tR[ey+1][ex]=tmp3;\n\t}\n\t// (ex-1,ey)\n\tif(ex>0 && pre!=3){\n\t\ttmp1=T[ey][ex]=R[ey][ex-1];\n\t\ttmp2=F[ey][ex]=F[ey][ex-1];\n\t\ttmp3=R[ey][ex]=T[ey][ex-1];\n\t\tif(dfs(t+1,ex-1,ey,2)) return true;\n\t\tR[ey][ex-1]=tmp1;\n\t\tF[ey][ex-1]=tmp2;\n\t\tT[ey][ex-1]=tmp3;\n\t}\n\t// (ex+1,ey)\n\tif(ex<2 && pre!=2){\n\t\ttmp1=T[ey][ex]=R[ey][ex+1];\n\t\ttmp2=F[ey][ex]=F[ey][ex+1];\n\t\ttmp3=R[ey][ex]=T[ey][ex+1];\n\t\tif(dfs(t+1,ex+1,ey,3)) return true;\n\t\tR[ey][ex+1]=tmp1;\n\t\tF[ey][ex+1]=tmp2;\n\t\tT[ey][ex+1]=tmp3;\n\t}\n\treturn false;\n}\n\nint main(){\n\tfor(int x0,y0;scanf(\"%d%d\",&x0,&y0),x0;){\n\t\tx0--; y0--;\n\t\trep(i,3) rep(j,3) {\n\t\t\tscanf(\" %c\",goal[i]+j);\n\t\t\tif(goal[i][j]=='E') gx=j, gy=i;\n\t\t\tT[i][j]='W';\n\t\t\tF[i][j]='R';\n\t\t\tR[i][j]='B';\n\t\t}\n\n\t\tfor(ub=0;ub<=30&&!dfs(0,x0,y0,-1);ub++);\n\n\t\tprintf(\"%d\\n\",ub<=30?ub:-1);\n\t}\n\n\treturn 0;\n}", "accuracy": 1, "time_ms": 1550, "memory_kb": 1028, "score_of_the_acc": -0.156, "final_rank": 8 } ]
aoj_1273_cpp
Problem H: The Best Name for Your Baby In the year 29XX, the government of a small country somewhere on the earth introduced a law restricting first names of the people only to traditional names in their culture, in order to preserve their cultural uniqueness. The linguists of the country specifies a set of rules once every year, and only names conforming to the rules are allowed in that year. In addition, the law also requires each person to use a name of a specific length calculated from one's birth date because otherwise too many people would use the same very popular names. Since the legislation of that law, the common task of the parents of new babies is to find the name that comes first in the alphabetical order among the legitimate names of the given length because names earlier in the alphabetical order have various benefits in their culture. Legitimate names are the strings consisting of only lowercase letters that can be obtained by repeatedly applying the rule set to the initial string "S", a string consisting only of a single uppercase S. Applying the rule set to a string is to choose one of the rules and apply it to the string. Each of the rules has the form A -> α , where A is an uppercase letter and α is a string of lowercase and/or uppercase letters. Applying such a rule to a string is to replace an occurrence of the letter A in the string to the string α . That is, when the string has the form " βAγ ", where β and γ are arbitrary (possibly empty) strings of letters, applying the rule rewrites it into the string " βαγ ". If there are two or more occurrences of A in the original string, an arbitrary one of them can be chosen for the replacement. Below is an example set of rules. S -> aAB (1) A -> (2) A -> Aa (3) B -> AbbA (4) Applying the rule (1) to "S", "aAB" is obtained. Applying (2) to it results in "aB", as A is replaced by an empty string. Then, the rule (4) can be used to make it "aAbbA". Applying (3) to the first occurrence of A makes it "aAabbA". Applying the rule (2) to the A at the end results in "aAabb". Finally, applying the rule (2) again to the remaining A results in "aabb". As no uppercase letter remains in this string, "aabb" is a legitimate name. We denote such a rewriting process as follows. (1) (2) (4) (3) (2) (2) S --> aAB --> aB --> aAbbA --> aAabbA --> aAabb --> aabb Linguists of the country may sometimes define a ridiculous rule set such as follows. S -> sA (1) A -> aS (2) B -> b (3) The only possible rewriting sequence with this rule set is: (1) (2) (1) (2) S --> sA --> saS --> sasA --> ... which will never terminate. No legitimate names exist in this case. Also, the rule (3) can never be used, as its left hand side, B, does not appear anywhere else. It may happen that no rules are supplied for some uppercase letters appearing in the rewriting steps. In its extreme case, even S might have no rules for it in the set, in which case there are no legitimate names, ...(truncated)
[ { "submission_id": "aoj_1273_10851276", "code_snippet": "#include <cctype>\n#include <cstddef>\n#include <cstring>\n#include <functional>\n#include <iostream>\n#include <map>\n#include <queue>\n#include <string>\n#include <utility>\n#include <vector>\nint n, l, tot, to[501];\nstd::string s[51], f[21][501];\nbool ed[501], vis[501];\nchar rev[501];\nstd::vector<std::pair<int, int> > g[501];\nstd::map<char, int> mp;\nint Id(char c) {\n if (!mp.count(c)) rev[mp[c] = ++tot] = c;\n return mp[c];\n}\nvoid Dijkstra(int layer) {\n std::priority_queue<std::pair<std::string, int>,\n std::vector<std::pair<std::string, int> >,\n std::greater<std::pair<std::string, int> > >\n q;\n std::memset(vis, 0, tot + 1);\n for (int i = 0; i <= tot; i++)\n if (f[layer][i] != \"~\") q.push(std::make_pair(f[layer][i], i));\n while (!q.empty()) {\n std::pair<std::string, int> tmp = q.top();\n q.pop();\n int u = tmp.second;\n if (vis[u]) continue;\n vis[u] = true;\n if (layer == 0) ed[u] = true;\n for (int v = 0; v <= tot; v++)\n if (!vis[v]) {\n for (std::vector<std::pair<int, int> >::iterator it = g[v].begin();\n it != g[v].end(); ++it) {\n int x = it->first, y = it->second;\n if (x == u || y == u) {\n if (x == u && ed[y]) f[layer][v] = tmp.first;\n if (y == u && ed[x]) f[layer][v] = tmp.first;\n if (f[layer][v] == tmp.first)\n q.push(std::make_pair(f[layer][v], v));\n }\n }\n }\n }\n}\nint main(int argc, char const *argv[]) {\n std::ios_base::sync_with_stdio(false);\n std::cin.tie(NULL);\n while (std::cin >> n >> l && (n || l)) {\n for (int i = 1; i <= n; i++) std::cin >> s[i];\n mp.clear(), tot = 0;\n std::memset(ed, 0, sizeof(ed));\n std::memset(to, 0, sizeof(to));\n std::memset(rev, 0, sizeof(rev));\n ed[0] = true;\n for (int i = 1; i <= n; i++) {\n std::string a = s[i].substr(2);\n int L = Id(s[i][0]), m = a.size();\n if (m == 0) {\n ed[L] = true;\n } else if (m == 1) {\n if (std::islower(a[0])) {\n int x = Id(a[0]);\n if (!to[L] || a[0] < rev[to[L]]) to[L] = x;\n } else {\n g[L].push_back(std::make_pair(Id(a[0]), 0));\n }\n } else {\n for (int i = 0; i < m - 2; i++) {\n int X = ++tot;\n g[L].push_back(std::make_pair(Id(a[i]), X));\n L = X;\n }\n g[L].push_back(std::make_pair(Id(a[m - 2]), Id(a[m - 1])));\n }\n }\n for (int i = 0; i <= l; i++)\n for (int j = 0; j <= tot; j++) f[i][j] = \"~\";\n for (int i = 0; i <= tot; i++)\n if (ed[i]) f[0][i] = \"\";\n Dijkstra(0);\n for (int i = 0; i <= tot; i++) {\n if (to[i]) f[1][i] = rev[to[i]];\n if (std::islower(rev[i])) f[1][i] = rev[i];\n }\n Dijkstra(1);\n for (int i = 2; i <= l; i++) {\n for (int j = 1; j < i; j++) {\n for (int u = 0; u <= tot; u++) {\n for (std::vector<std::pair<int, int> >::iterator it = g[u].begin();\n it != g[u].end(); ++it) {\n int x = it->first, y = it->second;\n if (f[j][x] != \"~\" && f[i - j][y] != \"~\") {\n std::string tmp = f[j][x] + f[i - j][y];\n if (tmp < f[i][u]) f[i][u] = tmp;\n }\n }\n }\n }\n Dijkstra(i);\n }\n if (!mp.count('S') || f[l][Id('S')] == \"~\")\n std::cout << \"-\\n\";\n else\n std::cout << f[l][Id('S')] << '\\n';\n for (int i = 0; i <= tot; i++) g[i].clear();\n }\n return 0;\n}", "accuracy": 1, "time_ms": 50, "memory_kb": 3936, "score_of_the_acc": -0.8209, "final_rank": 9 }, { "submission_id": "aoj_1273_10506991", "code_snippet": "// AOJ #1273 The Best Name for Your Baby\n// 2025.5.21\n\n#include <bits/stdc++.h>\nusing namespace std;\n\nstatic const string FAIL = \"{\";\n\nint main(){\n ios::sync_with_stdio(0);\n cin.tie(0);\n\n while (true) {\n int n, L;\n cin >> n >> L;\n if (n == 0) break;\n\n vector<vector<string>> rules(26);\n for (int i = 0; i < n; i++) {\n string line;\n cin >> line;\n int A = line[0] - 'A';\n string rhs = line.substr(2);\n rules[A].push_back(rhs);\n }\n for (int A = 0; A < 26; A++) {\n sort(rules[A].begin(), rules[A].end());\n }\n\n vector<vector<string>> best(26, vector<string>(L+1, FAIL));\n\n for (int j = 0; j <= L; j++) {\n bool updated = true;\n while (updated) {\n updated = false;\n for (int A = 0; A < 26; A++) {\n if (rules[A].empty()) continue;\n string curBest = best[A][j];\n for (auto &rhs : rules[A]) {\n int m = rhs.size();\n static string dp[11][21];\n for (int i = 0; i <= m; i++)\n for (int k = 0; k <= j; k++) dp[i][k] = FAIL;\n dp[0][0] = \"\";\n\n for (int i = 0; i < m; i++) {\n char c = rhs[i];\n for (int k = 0; k <= j; k++) {\n if (dp[i][k] == FAIL) continue;\n const string &sofar = dp[i][k];\n if ('a' <= c && c <= 'z') {\n if (k+1 <= j) {\n string cand = sofar;\n cand.push_back(c);\n if (dp[i+1][k+1] == FAIL || cand < dp[i+1][k+1])\n dp[i+1][k+1] = move(cand);\n }\n } else {\n int B = c - 'A';\n for (int add = 0; add + k <= j; add++) {\n const string &sub = best[B][add];\n if (sub == FAIL) continue;\n string cand = sofar + sub;\n if (dp[i+1][k+add] == FAIL || cand < dp[i+1][k+add])\n dp[i+1][k+add] = move(cand);\n }\n }\n }\n }\n if (dp[m][j] != FAIL) {\n if (curBest == FAIL || dp[m][j] < curBest) curBest = dp[m][j];\n }\n }\n if (curBest != FAIL && curBest != best[A][j]) {\n best[A][j] = move(curBest);\n updated = true;\n }\n }\n }\n }\n const string &ans = best['S' - 'A'][L];\n cout << (ans == FAIL ? \"-\" : ans) << endl;\n }\n return 0;\n}", "accuracy": 1, "time_ms": 930, "memory_kb": 3220, "score_of_the_acc": -0.7559, "final_rank": 7 }, { "submission_id": "aoj_1273_7918044", "code_snippet": "#include <cstdlib>\n#include <cctype>\n#include <cstring>\n#include <cstdio>\n#include <cmath>\n#include <algorithm>\n#include <vector>\n#include <string>\n#include <iostream>\n#include <sstream>\n#include <map>\n#include <set>\n#include <queue>\n#include <stack>\n#include <fstream>\n#include <numeric>\n#include <iomanip>\n#include <bitset>\n#include <list>\n#include <stdexcept>\n#include <functional>\n#include <utility>\n#include <ctime>\n#include <cassert>\nusing namespace std;\ntypedef long long LL;\ntypedef unsigned long long ULL;\n#define MEM(a,b) memset((a),(b),sizeof(a))\nconst LL INF = 1e9 + 7;\nconst int N = 1e3 + 10;\nint flag[200][21];\nstring ans[200][21];\nstring dp[51][21][21];\nint f[51][21];\nint in[200][21];\nvector<string> vs[200];\nmap<string, int> mc;\nint key[200];\nint a[30][30];\nint sz[51];\nstring dfs(int x, int len)\n{\n\tif (flag[x][len])\n\t\treturn ans[x][len];\n\tif (in[x][len]) return \"-\";\n\tin[x][len] = 1;\n\tstring ret = \"-\";\n\tfor (int i = 0; i < vs[x].size(); i++)\n\t{\n\t\tstring& s = vs[x][i];\n\t\tif (s.empty()) continue;\n\t\tint id = mc[s];\n\t\tif (!f[id][len])\n\t\t{\n\t\t\tfor (int i = 1; i <= 20; i++)\n\t\t\t{\n\t\t\t\tdp[id][0][i] = \"-\";\n\t\t\t}\n\t\t\tdp[id][0][0] = \"\";\n\t\t\tfor (int i = 1; i <= s.length(); i++)\n\t\t\t{\n\t\t\t\tfor (int j = 0; j <= len; j++)\n\t\t\t\t{\n\t\t\t\t\tstring& res = dp[id][i][j];\n\t\t\t\t\tres = \"-\";\n\t\t\t\t\tchar c = s[i - 1];\n\t\t\t\t\tif (islower(c))\n\t\t\t\t\t{\n\t\t\t\t\t\tif (j > 0 && dp[id][i - 1][j - 1] != \"-\")\n\t\t\t\t\t\t{\n\t\t\t\t\t\t\tif (res == \"-\") res = dp[id][i - 1][j - 1] + c;\n\t\t\t\t\t\t\tres = min(res, dp[id][i - 1][j - 1] + c);\n\t\t\t\t\t\t}\n\t\t\t\t\t\tcontinue;\n\t\t\t\t\t}\n\t\t\t\t\tfor (int k = 0; k <= j; k++)\n\t\t\t\t\t{\n\t\t\t\t\t\tif (dp[id][i - 1][k] == \"-\") continue;\n\t\t\t\t\t\tstring t1 = dp[id][i - 1][k];\n\t\t\t\t\t\tstring t2 = dfs(c, j - k);\n\t\t\t\t\t\tif (t2 == \"-\") continue;\n\t\t\t\t\t\tif (res == \"-\") res = t1 + t2;\n\t\t\t\t\t\tres = min(res, t1 + t2);\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t\tf[id][len] = 1;\n\t\t}\n\n\t\tif (ret == \"-\") ret = dp[id][s.length()][len];\n\t\tif (dp[id][s.length()][len] != \"-\")\n\t\t\tret = min(ret, dp[id][s.length()][len]);\n\t}\n\tin[x][len] = 0;\n\tflag[x][len] = 1;\n\treturn ans[x][len] = ret;\n}\nvoid init()\n{\n\tMEM(key, 0);\n\tMEM(flag, 0);\n\tint cnt = 26;\n\twhile (cnt--)\n\t{\n\t\tfor (int i = 'A'; i <= 'Z'; i++)\n\t\t{\n\t\t\tif (key[i] == 1) continue;\n\t\t\tfor (int j = 0; j < vs[i].size(); j++)\n\t\t\t{\n\t\t\t\tstring& s = vs[i][j];\n\t\t\t\tint ok = 1;\n\t\t\t\tfor (int k = 0; k < s.length(); k++)\n\t\t\t\t{\n\t\t\t\t\tchar c = s[k];\n\t\t\t\t\tok &= key[c];\n\t\t\t\t}\n\t\t\t\tkey[i] = ok;\n\t\t\t\tif (key[i]) break;\n\t\t\t}\n\t\t}\n\t}\n\tMEM(a, 0);\n\tfor (int i = 'A'; i <= 'Z'; i++)\n\t{\n\t\tfor (int j = 0; j < vs[i].size(); j++)\n\t\t{\n\t\t\tstring& s = vs[i][j];\n\t\t\tstring t;\n\t\t\tint ok = 1;\n\t\t\tfor (int k = 0; k < s.length(); k++)\n\t\t\t{\n\t\t\t\tchar c = s[k];\n\t\t\t\tok &= key[c];\n\t\t\t\tif (!key[c]) t += c;\n\t\t\t}\n\t\t\tif (t.length() > 1) continue;\n\t\t\tif (t.empty())\n\t\t\t{\n\t\t\t\tfor (int k = 0; k < s.length(); k++)\n\t\t\t\t{\n\t\t\t\t\ta[s[k] - 'A'][i - 'A'] = 1;\n\t\t\t\t}\n\t\t\t}\n\t\t\telse if (isupper(t[0]))\n\t\t\t\ta[t[0] - 'A'][i - 'A'] = 1;\n\t\t}\n\t}\n\tfor (int k = 0; k < 26; k++)\n\t{\n\t\tfor (int i = 0; i < 26; i++)\n\t\t{\n\t\t\tfor (int j = 0; j < 26; j++)\n\t\t\t{\n\t\t\t\ta[i][j] |= a[i][k] & a[k][j];\n\t\t\t}\n\t\t}\n\t}\n\tfor (int i = 'A'; i <= 'Z'; i++)\n\t{\n\t\tflag[i][0] = 1;\n\t\tif (key[i] == 1) ans[i][0] = \"\";\n\t\telse ans[i][0] = \"-\";\n\t}\n}\nint main()\n{\n\t//freopen(\"input.txt\", \"r\", stdin);\n\t//freopen(\"output.txt\", \"w\", stdout);\n\tint n, m;\n\tint ks = 0;\n\twhile (cin >> n >> m)\n\t{\n\t\tif (n == 0 && m == 0) break;\n\t\tks++;\n\t\tfor (int i = 'A'; i <= 'Z'; i++)\n\t\t\tvs[i].clear();\n\t\tmc.clear();\n\t\tfor (int i = 1; i <= n; i++)\n\t\t{\n\t\t\tMEM(f[i], 0);\n\t\t\tstring s;\n\t\t\tcin >> s;\n\t\t\tchar c = s[0];\n\t\t\ts = s.substr(2);\n\t\t\tvs[c].push_back(s);\n\t\t\tmc[s] = i;\n\t\t}\n\t\tinit();\n\t\tfor (int i = 1; i <= m; i++)\n\t\t{\n\t\t\tfor (int c = 'A'; c <= 'Z'; c++)\n\t\t\t\tdfs(c, i);\n\t\t\tqueue<int> q;\n\t\t\tfor (int c = 'A'; c <= 'Z'; c++)\n\t\t\t{\n\t\t\t\tif (dfs(c, i) != \"-\")\n\t\t\t\t{\n\t\t\t\t\tq.push(c);\n\t\t\t\t}\n\t\t\t}\n\t\t\tfor (int j = 1; j <= n; j++)\n\t\t\t\tf[i][j] = 0;\n\t\t\twhile (!q.empty())\n\t\t\t{\n\t\t\t\tint x = q.front();\n\t\t\t\tq.pop();\n\t\t\t\tfor (int j = 0; j < 26; j++)\n\t\t\t\t{\n\t\t\t\t\tif (a[x - 'A'][j] == 0) continue;\n\t\t\t\t\tstring& t = ans[j + 'A'][i];\n\t\t\t\t\tif (t == \"-\" || t > ans[x][i])\n\t\t\t\t\t{\n\t\t\t\t\t\tt = ans[x][i];\n\t\t\t\t\t\tq.push(j + 'A');\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\tputs(dfs('S', m).c_str());\n\t}\n\treturn 0;\n}", "accuracy": 1, "time_ms": 250, "memory_kb": 3824, "score_of_the_acc": -0.8201, "final_rank": 8 }, { "submission_id": "aoj_1273_6874247", "code_snippet": "#include <bits/stdc++.h>\n#define x first\n#define y second\nusing namespace std;\nvector<pair<int,int> > g[1010];\nint n,l,cnt;\nbool st[1010][30],book[1010];\nstring d[1010],f[1010][30];\npriority_queue<pair<string,int>,vector<pair<string,int> >,greater<pair<string,int> > > q;\nint main()\n{\n\tint i,j,k,u;\n while(cin>>n>>l&&(n||l)){\n for(i=0;i<1010;i++)g[i].clear();\n cnt=130;\n for(i=1;i<=n;i++){\n string st;\n cin>>st;\n int len=st.size();\n if(len==2)g[st[0]].push_back({0,0});\n \telse if(len==3)g[st[0]].push_back({st[2],0});\n \t\telse if(len==4)g[st[0]].push_back({st[2],st[3]});\n \t\t\telse{\n \t\t\tg[st[0]].push_back({st[2],++cnt});\n \t\t\tfor(j=3;j<len-2;j++){\n \t\t\tg[cnt].push_back({st[j],cnt+1});\n \t\t\tcnt++;\n \t\t\t}\n \t\t\tg[cnt].push_back({st[len-2],st[len-1]});\n \t\t\t} \n }\n for(i=0;i<=cnt;i++)\n \tfor(j=0;j<=l;j++)f[i][j]=\"~~\";\n \t\n memset(st,0,sizeof(st));\n for(i='A';i<='Z';i++)\n \tfor(j=0;j<g[i].size();j++)\n \t\tif(!(g[i][j].x+g[i][j].y)){\n \t\t\t\tst[i][0]=1;\n \t\tf[i][0]=\"\";\n \t\t}\n \n st[0][0]=1;\n for(i='a';i<='z';i++){\n st[i][1]=1;\n f[i][1]=(char)(i); \n }\n for(j=0;j<=l;j++){\n while(!q.empty())q.pop();\n for(i=0;i<=cnt;i++)d[i]=\"~~\";\n memset(book,0,sizeof(book));\n for(i='A';i<=cnt;i++){\n for(k=1;k<j;k++)\n \tfor(u=0;u<g[i].size();u++){\n \t\tpair<int,int> v=g[i][u];\n \tif(st[v.x][k]&st[v.y][j-k]){\n \tf[i][j]=min(f[i][j],f[v.x][k]+f[v.y][j-k]);\n \tst[i][j]|=1;\n \t}\n \t}\n \t\n if(st[i][j]){\n d[i]=f[i][j];\n q.push({d[i],i});\n }\n }\n while(!q.empty()){\n int u=q.top().y;\n q.pop();\n if(book[u])continue;\n book[u]=1;\n st[u][j]=1;\n f[u][j]=d[u];\n for(i='A';i<=cnt;i++){\n \tif(!book[i]){\n \t\tfor(int w=0;w<g[i].size();w++){\n \t\t\tpair<int,int> v=g[i][w];\n \t\t\tif(v.x==u||v.y==u){\n \t\tif(v.x==u&&st[v.y][0])d[i]=d[u];\n \t\tif(v.y==u&&st[v.x][0])d[i]=d[u];\n \t\tif(d[i]==d[u])q.push({d[i],i}); \n \t\t\t\t\t\t}\n \t\t\t\t\t}\n \t\t\t\t}\n\t\t\t\t}\n }\n }\n if(!st['S'][l]){\n cout<<'-'<<endl;\n continue;\n }else cout<<f['S'][l]<<endl;\n }\n}", "accuracy": 1, "time_ms": 50, "memory_kb": 4592, "score_of_the_acc": -1.0015, "final_rank": 11 }, { "submission_id": "aoj_1273_6852145", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nvector<pair<int,int> > G[1005];\nint n,l,cnt;\nbool vis[1005][25],done[1005];\nchar str[25];\nstring d[1005],f[1005][25];\npriority_queue<pair<string,int> ,vector<pair<string,int> >,greater<pair<string,int> > > q;\nint main(){\n while(scanf(\"%d%d\",&n,&l)){\n \tif(!n && !l) exit(0);\n for(int i=1;i<=1005;i++){\n \tG[i].clear();\n\t\t}\n cnt=130;\n for(int i=1;i<=n;i++){\n scanf(\"%s\",str);\n int len=strlen(str);\n if(len==2) G[str[0]].push_back(make_pair(0,0));\n else if(len==3) G[str[0]].push_back(make_pair(str[2],0));\n else if(len==4) G[str[0]].push_back(make_pair(str[2],str[3]));\n else{\n G[str[0]].push_back(make_pair(str[2],++cnt));\n for(int j=3;j<len-2;j++){\n G[cnt].push_back(make_pair(str[j],cnt+1));\n cnt++;\n }\n G[cnt].push_back(make_pair(str[len-2],str[len-1]));\n } \n }\n for(int i=0;i<=cnt;i++){\n \tfor(int j=0;j<=l;j++){\n \t\tf[i][j]=\"~~\";\n\t\t\t}\n\t\t}\n memset(vis,0,sizeof(vis));\n for(int i='A';i<='Z';i++){\n \tfor(auto v:G[i]){\n \t\tif(!(v.first+v.second)){\n\t\t\t\t\tvis[i][0]=1;\n \t\t\tf[i][0]=\"\";\n \t\t}\n \t}\n\t\t}\n vis[0][0]=1;\n for(char i='a';i<='z';i++){\n vis[i][1]=1;\n f[i][1]=i; \n }\n for(int j=0;j<=l;j++){\n while(!q.empty()) q.pop();\n for(int i=0;i<=cnt;i++){\n \td[i]=\"~~\";\n\t\t\t}\n memset(done,0,sizeof(done));\n for(int i='A';i<=cnt;i++){\n for(int k=1;k<j;k++){\n \tfor(auto v:G[i]){\n \tif(vis[v.first][k]&vis[v.second][j-k]){\n \tf[i][j]=min(f[i][j],f[v.first][k]+f[v.second][j-k]);\n \tvis[i][j]|=1;\n \t}\n \t}\n\t\t\t\t}\n if(vis[i][j]){\n d[i]=f[i][j];\n q.push(make_pair(d[i],i));\n }\n }\n while(!q.empty()){\n int u=q.top().second;\n q.pop();\n if(done[u]) continue;\n done[u]=true;\n vis[u][j]=1;\n f[u][j]=d[u];\n for(int i='A';i<=cnt;i++){\n \tif(!done[i]){\n \t\tfor(auto v:G[i]){\n \t\t\tif(v.first==u || v.second==u){\n \t\tif(v.first==u && vis[v.second][0]) d[i]=d[u];\n \t\tif(v.second==u && vis[v.first][0]) d[i]=d[u];\n \t\tif(d[i]==d[u]) q.push(make_pair(d[i],i)); \n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t}\n }\n }\n if(!vis['S'][l]){\n cout<<'-'<<endl;\n continue;\n }\n else cout<<f['S'][l]<<endl;\n }\n}", "accuracy": 1, "time_ms": 40, "memory_kb": 4380, "score_of_the_acc": -0.9416, "final_rank": 10 }, { "submission_id": "aoj_1273_6541832", "code_snippet": "/**\n * author: gary\n * created: 27.04.2022 17:40:31\n**/\n#include<iostream>\n#include<cstring>\n#include<algorithm>\n#include<vector>\n#include<stack>\n#define rb(a,b,c) for(int a=b;a<=c;++a)\n#define rl(a,b,c) for(int a=b;a>=c;--a)\n#define rep(a,b) for(int a=0;a<b;++a)\n#define LL long long\n#define PB push_back\n#define POB pop_back\n#define II(a,b) make_pair(a,b)\n#define FIR first\n#define SEC second\n#define SRAND mt19937 rng(chrono::steady_clock::now().time_since_epoch().count())\n#define random(a) rng()%a\n#define ALL(a) a.begin(),a.end()\n#define check_min(a,b) a=min(a,b)\n#define check_max(a,b) a=max(a,b)\nusing namespace std;\nconst int INF=0x3f3f3f3f;\ntypedef pair<int,int> mp;\nstring f[26][21];\nvector<string> to[26];\nbool canempty[26];\nvector<int> g[26],rg[26];\nint vis[26];\nbool type(char c){\n if(c<='z'&&c>='a') return 0;\n return 1;\n}\nstring solve(vector<string> v,int len){\n string dp[21][2];\n rep(i,len+1) rep(j,2) dp[i][j]=\"{\";\n dp[0][0]=\"\";\n rep(t,v.size()){\n rep(i,len+1) dp[i][(t+1)&1]=\"{\";\n rep(i,len+1) if(dp[i][t&1]!=\"{\"){\n string val=dp[i][t&1];\n if(type(v[t][0])==0){\n if(i+v[t].length()>len) break;\n check_min(dp[i+v[t].length()][(t+1)&1],val+v[t]);\n }\n else{\n rep(k,len+1-i) if(f[v[t][0]-'A'][k]!=\"{\"){\n check_min(dp[i+k][(t+1)&1],val+f[v[t][0]-'A'][k]);\n }\n }\n }\n }\n return dp[len][v.size()&1];\n}\nstack<int> sta;\nvector<int> ord;\nvoid dfs1(int now){\n vis[now]=1;\n rep(i,g[now].size()) if(!vis[g[now][i]]) dfs1(g[now][i]);\n sta.push(now);\n}\nvoid dfs2(int now){\n vis[now]=1;\n ord.PB(now);\n rep(i,rg[now].size()) if(!vis[rg[now][i]]) dfs2(rg[now][i]);\n}\nint main(){\n ios::sync_with_stdio(false);\n cin.tie(0);\n int n,l;\n while (true)\n {\n cin>>n>>l;\n if(n==0&&l==0) break;\n rep(i,26) rb(j,0,20) f[i][j]=\"{\";\n rep(i,26) to[i].clear(),canempty[i]=0;\n // vector<string> save;\n rb(z,1,n){\n string s;\n cin>>s;\n // save.PB(s);\n string t;\n rb(i,2,s.length()-1) t.PB(s[i]);\n int x=s[0]-'A';\n to[x].PB(t);\n if(t.empty()) canempty[x]=1;\n }\n while (true){\n bool ok=0;\n rep(i,26){\n if(!canempty[i]){\n rep(j,to[i].size()){\n string tmp=to[i][j];\n canempty[i]=1;\n rep(k,tmp.length()){\n canempty[i]&=type(tmp[k])==1&&canempty[tmp[k]-'A']&&tmp[k]!=('A'+i);\n }\n if(canempty[i]) break;\n }\n ok|=canempty[i];\n }\n }\n if(!ok) break;\n }\n rep(i,26) g[i].clear(),rg[i].clear(),vis[i]=0;\n ord.clear();\n rep(i,26){\n rep(j,to[i].size()){\n string tmp=to[i][j];\n rep(k,tmp.length()){\n bool ok=(type(tmp[k])==1);\n rep(y,tmp.length()){\n if(y!=k){\n ok&=(type(tmp[y])==1)&&canempty[tmp[y]-'A'];\n }\n }\n if(ok){\n // cout<<canempty<<\" \"<<tmp<<\" \"<<char('A'+i)<<\" \"<<tmp[k]<<endl;\n rg[i].PB(tmp[k]-'A');\n g[tmp[k]-'A'].PB(i);\n }\n }\n }\n }\n rep(i,26) sort(ALL(rg[i])),rg[i].erase(unique(ALL(rg[i])),rg[i].end());\n rep(i,26) if(!vis[i]) dfs1(i);\n rep(i,26) vis[i]=0;\n while (!sta.empty())\n {\n int tmp=sta.top();\n sta.pop();\n if(!vis[tmp]) dfs2(tmp);\n }\n rb(j,0,l){\n rep(i_,26){\n int i=ord[i_];\n if(j==0){\n if(canempty[i]) f[i][j]=\"\";\n continue;\n }\n rep(z,rg[i].size()) check_min(f[i][j],f[rg[i][z]][j]);\n rep(itx,to[i].size()){\n string it=to[i][itx];\n vector<string> v;\n rep(xx,it.length()){\n char itt=it[xx];\n if(v.empty()||type(v.back()[0])!=type(itt)||type(itt)==1) v.PB(\"\");\n v.back().PB(itt);\n }\n check_min(f[i][j],solve(v,j));\n }\n }\n }\n // if(f['S'-'A'][l]==\"ieeeeeeeee\"){\n // cerr<<n<<\" \"<<l<<endl;\n // for(auto it:save) cerr<<it<<endl;\n // }\n if(f['S'-'A'][l]==\"{\") cout<<\"-\\n\";\n else cout<<f['S'-'A'][l]<<endl;\n }\n return 0;\n}", "accuracy": 1, "time_ms": 270, "memory_kb": 3180, "score_of_the_acc": -0.6458, "final_rank": 6 }, { "submission_id": "aoj_1273_2290758", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\n \nstring dp[26][21];\nstring Z=\"{\";\n \nvoid init(){\n for(int i=0;i<26;i++){\n for(int j=0;j<=20;j++){\n dp[i][j]=Z;\n }\n }\n}\n \nint N,M;\nstring from[50],to[50];\n \nbool check(int id){\n int sd=from[id][0]-'A';\n \n if( to[id]==\"\" ){\n bool res=( dp[sd][0] == Z );\n dp[sd][0]=\"\";\n return res;\n }\n \n string t[21];\n for(int i=0;i<=M;i++)t[i]=Z;\n t[0]=\"\";\n \n bool allZero=true;\n \n for(int i=0;i<(int)to[id].size();i++){\n char ch=to[id][i];\n if('A'<=ch&&ch<='Z'){\n int target=ch-'A';\n if( dp[target][0] == Z )allZero=false;\n \n for(int j=M;j>=0;j--){\n if( dp[target][0]==Z ) t[j]=Z;\n \n for(int k=j;k>=0;k--){\n if(t[j-k]!=Z && dp[target][k]!=Z){\n t[j]=min(t[j],t[j-k]+dp[target][k]);\n }\n }\n }\n \n }else{\n allZero=false;\n for(int j=M;j>=1;j--){\n if(t[j-1]!=Z)t[j]=t[j-1]+ch;\n else t[j]=Z;\n }\n t[0]=Z;\n }\n }\n \n bool res=false;\n for(int i=1;i<=M;i++){\n if(dp[sd][i]>t[i]){\n dp[sd][i]=t[i];\n res=true;\n }\n }\n \n if( allZero && dp[sd][0]==Z){\n res=true;\n dp[sd][0]=\"\";\n }\n \n return res;\n}\n \nint main(){\n while(1){\n cin>>N>>M;\n if(N==0&&M==0)break;\n init();\n for(int i=0;i<N;i++){\n string str;\n cin>>str;\n int index=str.find('=');\n from[i]=str.substr(0,index);\n if(index+1 == (int)str.size()){\n to[i]=\"\";\n }else{\n to[i]=str.substr(index+1);\n }\n }\n bool update=true;\n while(update){\n update=false;\n for(int i=0;i<N;i++){\n update=(update||check(i));\n }\n }\n string ans = dp[ 'S'-'A' ][ M ];\n if(ans==Z)cout<<'-'<<endl;\n else cout<<ans<<endl;\n }\n return 0;\n}", "accuracy": 1, "time_ms": 6700, "memory_kb": 3180, "score_of_the_acc": -1.6112, "final_rank": 13 }, { "submission_id": "aoj_1273_2290390", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\n\nstring dp[26][21];\nstring Z=\"{\";\n\nvoid init(){\n for(int i=0;i<26;i++){\n for(int j=0;j<=20;j++){\n dp[i][j]=Z;\n }\n }\n}\n\nint N,M;\nstring from[50],to[50];\n\nbool check(int id){\n int sd=from[id][0]-'A';\n \n if( to[id]==\"\" ){\n // cout<<\"!!\"<<endl;\n \n bool res=( dp[sd][0] == Z );\n dp[sd][0]=\"\";\n return res;\n }\n \n string t[21];\n for(int i=0;i<=M;i++)t[i]=Z;\n t[0]=\"\";\n\n bool allZero=true;\n \n for(int i=0;i<(int)to[id].size();i++){\n char ch=to[id][i];\n if('A'<=ch&&ch<='Z'){\n int target=ch-'A';\n if( dp[target][0] == Z )allZero=false;\n \n for(int j=M;j>=0;j--){\n if( dp[target][0]==Z ) t[j]=Z;\n \n for(int k=j;k>=0;k--){\n if(t[j-k]!=Z && dp[target][k]!=Z){\n t[j]=min(t[j],t[j-k]+dp[target][k]);\n }\n }\n }\n \n }else{\n allZero=false;\n for(int j=M;j>=1;j--){\n if(t[j-1]!=Z)t[j]=t[j-1]+ch;\n else t[j]=Z;\n }\n t[0]=Z;\n }\n }\n \n bool res=false;\n for(int i=1;i<=M;i++){\n if(dp[sd][i]>t[i]){\n dp[sd][i]=t[i];\n res=true;\n }\n }\n \n if( allZero && dp[sd][0]==Z){\n res=true;\n dp[sd][0]=\"\";\n }\n \n return res;\n}\n\nint main(){\n while(1){\n cin>>N>>M;\n if(N==0&&M==0)break;\n init();\n for(int i=0;i<N;i++){\n string str;\n cin>>str;\n int index=str.find('=');\n from[i]=str.substr(0,index);\n \n if(index+1 == (int)str.size()){\n to[i]=\"\";\n // cout<<i<<endl;\n }else{\n to[i]=str.substr(index+1);\n }\n\n \n }\n \n bool update=true;\n while(update){\n update=false;\n for(int i=0;i<N;i++){\n update=(update||check(i));\n }\n /*\n\n for(int i=0;i<5;i++){\n cout<<\"dp[A][\"<<i<<\"] = \"<<dp[0][i]<<endl;\n }\n for(int i=0;i<5;i++){\n cout<<\"dp[B][\"<<i<<\"] = \"<<dp[1][i]<<endl;\n }\n for(int i=0;i<5;i++){\n cout<<\"dp[S][\"<<i<<\"] = \"<<dp['S'-'A'][i]<<endl;\n }\n */\n }\n \n string ans = dp[ 'S'-'A' ][ M ];\n if(ans==Z)cout<<'-'<<endl;\n else cout<<ans<<endl;\n }\n return 0;\n}", "accuracy": 1, "time_ms": 6510, "memory_kb": 3048, "score_of_the_acc": -1.5464, "final_rank": 12 }, { "submission_id": "aoj_1273_516234", "code_snippet": "#include <iostream>\n#include <algorithm>\n#include <string>\n#include <vector>\n#include <cctype>\nusing namespace std;\n\nconst string kInf = \"zzzzzzzzzzzzzzzzzzzzz\";\n\nstruct Rule {\n int l, r1, r2;\n\n Rule(int _l, int _r1, int _r2) {\n l = _l, r1 = _r1, r2 = _r2;\n }\n};\n\nint N, M, C, R;\nint ch_num[128];\nvector< Rule > vrl;\nvector< vector< string > > fs;\nvector< vector< bool > > fb;\n\nvoid init_and_read(void);\nvoid parse_rule(int, const string &, int, int);\nvoid solve(void);\nvoid cal_zero_ln(int);\n\nint main() {\n while (cin >> N >> M, N + M) {\n init_and_read();\n solve();\n }\n\n return 0;\n}\n\nvoid init_and_read(void) {\n vrl.clear();\n fill(&ch_num[0], &ch_num[128], -1);\n C = 1;\n for (int i = 0; i < N; ++i) {\n string str;\n int ln;\n\n cin >> str, ln = str.length();\n for (int j = 0; j < ln; ++j) {\n if (str[j] == '=') continue;\n if (ch_num[str[j]] == -1) ch_num[str[j]] = C++;\n }\n\n parse_rule(ch_num[str[0]], str, 2, ln);\n }\n}\n\nvoid parse_rule(int lv, const string &rs, int i, int ln) {\n switch (ln - i) {\n case 0:\n vrl.push_back(Rule(lv, 0, 0));\n break;\n case 1:\n vrl.push_back(Rule(lv, ch_num[rs[i]], 0));\n break;\n case 2:\n vrl.push_back(Rule(lv, ch_num[rs[i]], ch_num[rs[i + 1]]));\n break;\n default:\n vrl.push_back(Rule(lv, ch_num[rs[i]], C));\n parse_rule(C++, rs, i + 1, ln);\n }\n}\n\nvoid solve(void) {\n R = vrl.size();\n fs.clear(), fs.resize(max(M + 1, 2), vector< string >(C, kInf));\n fb.clear(), fb.resize(max(M + 1, 2), vector< bool >(C, false));\n\n fs[0][0] = \"\", fb[0][0] = true;\n for (int i = 0; i < 128; ++i)\n if (islower(char(i)) && ch_num[i] != -1)\n fs[1][ch_num[i]] = string(1, char(i)), fb[1][ch_num[i]] = true;\n\n cal_zero_ln(0);\n for (int i = 1; i <= M; ++i) {\n for (int j = 1; i - j > 0; ++j)\n for (int k = 0; k < R; ++k) {\n int l = vrl[k].l, r1 = vrl[k].r1, r2 = vrl[k].r2;\n if (fb[j][r1] && fb[i - j][r2])\n fs[i][l] = min(fs[i][l], fs[j][r1] + fs[i - j][r2]), fb[i][l] = true;\n if (fb[i - j][r1] && fb[j][r2])\n fs[i][l] = min(fs[i][l], fs[i - j][r1] + fs[j][r2]), fb[i][l] = true;\n }\n\n cal_zero_ln(i);\n }\n\n if (ch_num['S'] != -1 && fb[M][ch_num['S']])\n cout << fs[M][ch_num['S']] << endl;\n else\n cout << \"-\" << endl;\n}\n\nvoid cal_zero_ln(int ln) {\n for (int i = 0; i < C; ++i)\n for (int j = 0; j < R; ++j) {\n int l = vrl[j].l, r1 = vrl[j].r1, r2 = vrl[j].r2;\n if (fb[ln][r1] && fb[0][r2])\n fs[ln][l] = min(fs[ln][l], fs[ln][r1] + fs[0][r2]), fb[ln][l] = true;\n if (fb[0][r1] && fb[ln][r2])\n fs[ln][l] = min(fs[ln][l], fs[0][r1] + fs[ln][r2]), fb[ln][l] = true;\n }\n}", "accuracy": 1, "time_ms": 1290, "memory_kb": 1540, "score_of_the_acc": -0.3474, "final_rank": 4 }, { "submission_id": "aoj_1273_415567", "code_snippet": "#include <string.h>\n#include <iostream>\n#include <string>\nusing namespace std;\n#define rep(i, n) for (int i = 0; i < (int)(n); i++)\ninline void cmin(string &a, const string& b) { if (a > b) a = b; }\ninline void cmin(string &a, const string& b, const string& c) {\n if (!a.size() || (b.size()&&b[0]=='|') || (c.size()&&c[0]=='|')) return ;\n if (a.size() != b.size() + c.size()) { a = b+c; return ; }\n const int L = b.size();\n int y = strncmp(b.c_str(), a.c_str(), L);\n if (y == 0) y = strncmp(c.c_str(), a.c_str()+L, c.size());\n if (y >= 0) return ;\n rep (i, b.size()) a[i] = b[i];\n rep (i, c.size()) a[b.size()+i] = c[i];\n}\n \nint n, L;\nchar from[64];\nstring to[64], dp[32][24], sub[2][24];\n \nstring solve() {\n rep (i, 32) rep (j, 24) dp[i][j] = '|';\n bool up;\n rep (LL, L+1) do {\n up = false;\n rep (k, n) {\n int cur = 0, nxt = 1;\n rep (i, 24) sub[cur][i] = '|';\n sub[cur][0] = \"\";\n rep (i, to[k].size()) {\n rep (j, 24) sub[nxt][j] = '|';\n if (islower(to[k][i])) {\n rep (x, LL) cmin(sub[nxt][x+1], sub[cur][x]+to[k][i]);\n }\n else {\n const int t = to[k][i] - 'A';\n rep (j, LL+1) rep (x, LL-j+1) {\n cmin(sub[nxt][x+j], sub[cur][x], dp[t][j]);\n }\n }\n swap(cur, nxt);\n }\n const int f = from[k] - 'A';\n if (dp[f][LL] > sub[cur][LL]) dp[f][LL] = sub[cur][LL], up = true;\n }\n } while (up);\n if (dp['S'-'A'][L] == \"|\") return \"-\";\n return dp['S'-'A'][L];\n}\n\nint main() {\n for (;;) {\n cin >> n >> L;\n if (n == 0) return 0;\n string buf;\n rep (i, n) {\n cin >> buf;\n from[i] = buf[0];\n to[i] = buf.substr(2);\n }\n cout << solve() << endl;\n }\n}", "accuracy": 1, "time_ms": 2270, "memory_kb": 964, "score_of_the_acc": -0.3359, "final_rank": 3 }, { "submission_id": "aoj_1273_415566", "code_snippet": "#include <string.h>\n#include <ctype.h>\n#include <iostream>\n#include <string>\n#include <algorithm>\nusing namespace std;\n#define rep(i, n) for (int i = 0; i < (int)(n); i++)\ninline void cmin(string &a, const string& b) { if (a > b) a = b; }\ninline void cmin(string &a, const string& b, const string& c) {\n if (a.size() == 0) return ;\n if (b.size() && b[0] == '|') return ;\n if (c.size() && c[0] == '|') return ;\n if (a.size() != b.size() + c.size()) {\n a = b+c;\n return ;\n }\n else {\n int y = 0;\n rep (i, b.size()) {\n if (b[i] < a[i]) { y = -1; break; }\n if (b[i] > a[i]) { y = 1; break; }\n }\n if (y == 0) rep (i, c.size()) {\n if (c[i] < a[b.size()+i]) { y = -1; break; }\n if (c[i] > a[b.size()+i]) { y = 1; break; }\n }\n if (y < 0) {\n rep (i, b.size()) a[i] = b[i];\n rep (i, c.size()) a[b.size()+i] = c[i];\n }\n }\n}\n \nint n, L;\nchar from[64];\nstring to[64];\n \nint has[32][24];\nstring dp[32][24], sub[2][24];\nint minL[32], maxL[32];\n \nstring solve() {\n memset(has, 0, sizeof(has));\n rep (i, 32) rep (j, 24) dp[i][j] = '-';\n rep (i, 32) minL[i] = 1<<20;\n rep (i, 32) maxL[i] = -(1<<20);\n rep (LL, L+1) {\n for (;;) {\n bool update = false;\n rep (k, n) {\n int minE = 0, maxE = 0;\n rep (i, to[k].size()) {\n if (islower(to[k][i])) minE++, maxE++;\n else {\n const int t = to[k][i] - 'A';\n minE += minL[t];\n maxE += maxL[t];\n }\n }\n if (LL < minE || maxE < LL) continue;\n int cur = 0, nxt = 1;\n rep (i, 24) sub[cur][i] = '|';\n sub[cur][0] = \"\";\n rep (i, to[k].size()) {\n rep (j, 24) sub[nxt][j] = '|';\n if (islower(to[k][i])) {\n rep (x, LL) cmin(sub[nxt][x+1], sub[cur][x]+to[k][i]);\n }\n else {\n const int t = to[k][i] - 'A';\n rep (j, LL+1) if (has[t][j]) rep (x, LL-j+1) {\n cmin(sub[nxt][x+j], sub[cur][x], dp[t][j]);\n }\n }\n swap(cur, nxt);\n }\n const int f = from[k] - 'A';\n if (sub[cur][LL] != \"|\") {\n if (!has[f][LL] || dp[f][LL] > sub[cur][LL]) {\n minL[f] = min(minL[f], LL);\n maxL[f] = max(maxL[f], LL);\n has[f][LL] = true;\n update = true;\n dp[f][LL] = sub[cur][LL];\n }\n }\n }\n if (!update) break;\n }\n }\n return dp['S'-'A'][L];\n}\n\nint main() {\n for (;;) {\n cin >> n >> L;\n if (n == 0) return 0;\n string buf;\n rep (i, n) {\n cin >> buf;\n from[i] = buf[0];\n to[i] = buf.substr(2);\n }\n cout << solve() << endl;\n }\n}", "accuracy": 1, "time_ms": 2650, "memory_kb": 960, "score_of_the_acc": -0.3919, "final_rank": 5 }, { "submission_id": "aoj_1273_100466", "code_snippet": "#include<iostream>\n#include<string>\n#include<vector>\nusing namespace std;\n#define REP(i,b,n) for(int i=b;i<n;i++)\n#define rep(i,n) REP(i,0,n)\n#define ALL(C) (C).begin(),(C).end()\n#define pb push_back\n#define mp make_pair\n\ntypedef pair<int,int> pii;\n\nconst int N=800;\nconst int Rs='z'+1;\nconst int SPACE='A'-1;\nbool isequal[N][N];\nbool leadSpace[N];\nstring table[21][N];\nbool isexist[21][N];\n\nvoid get_input(int n,vector<pii> *rule,int &p){\n rep(i,n){\n string in;\n cin>>in;\n if (in.size()== 2){//SPACE\n leadSpace[in[0]]=true;\n rule[in[0]].pb(mp(SPACE,SPACE));\n }else{\n int me = in[0];\n REP(j,2,(int)in.size()-1){\n\t//\tcout <<\"rule \"<< me <<\" \" << (int)in[j] <<\" \" <<p << endl;\n\trule[me].pb(mp(in[j],p));\n\tme=p;\n\tp++;\n }\n // cout <<\"rule \"<< me <<\" \" << (int)in[in.size()-1] << \" \" << SPACE<<endl;\n rule[me].pb(mp(in[in.size()-1],SPACE));\n }\n }\n}\n\nvoid make_space(int p,vector<pii> *rule){\n rep(k,p){\n rep(i,p){\n rep(j,rule[i].size()){\n\tif (leadSpace[rule[i][j].first]&&leadSpace[rule[i][j].second]){\n\t leadSpace[i]=true;\n\t}\n }\n }\n }\n}\n\nbool vis[N];\nvoid make_equal(int from,int now,vector<pii>*rule){\n if (vis[now])return;\n vis[now]=true;\n if (now != SPACE && !((int)'a'<=now && now <=(int)'z'))isequal[from][now]=true;\n rep(i,rule[now].size()){\n if (leadSpace[rule[now][i].first]){\n make_equal(from,rule[now][i].second,rule);\n }\n if (leadSpace[rule[now][i].second]){\n make_equal(from,rule[now][i].first,rule);\n }\n }\n}\n\nstring solve(int p,int len,vector<pii> *rule){\n rep(i,p){\n rep(j,21){\n table[j][i]=string(1,(char)'z'+1);\n isexist[j][i]=false;\n }\n }\n\n rep(i,p){\n if (leadSpace[i]){\n table[0][i]=\"\";\n isexist[0][i]=true;\n }\n }\n\n REP(k,1,len+1){\n rep(i,p){\n rep(j,rule[i].size()){\n\tif ('a'<= rule[i][j].first && rule[i][j].first <='z'){\n\t if (isexist[k-1][rule[i][j].second]){\n\t isexist[k][i]=true;\n\t table[k][i]=min(table[k][i],string(1,(char)rule[i][j].first)+table[k-1][rule[i][j].second]);\n\t }\n\t}else {\n\t rep(l,k+1){\n\t if (isexist[l][rule[i][j].first] &&\n\t\tisexist[k-l][rule[i][j].second]){\n\t isexist[k][i]=true;\n\t table[k][i]=min(table[k][i],\n\t\t\t table[l][rule[i][j].first]+table[k-l][rule[i][j].second]);\n\t }\n\t }\n\t}\n }\n }\n \n \n rep(i,p){\n rep(j,p){\n\tif (i != j && isequal[i][j]){\n\t if (isexist[k][i] && isexist[k][j]){\n\t table[k][i]=min(table[k][i],table[k][j]);\n\t } else if (isexist[k][j] && !isexist[k][i]){\n\t isexist[k][i]=true;\n\t table[k][i]=table[k][j];\n\t }\n\t}\n }\n }\n }\n \n if (isexist[len]['S'])return table[len]['S'];\n return \"-\";\n}\n\nmain(){\n int n,l;\n while(cin>>n>>l && n){\n rep(i,N){\n rep(j,N)isequal[i][j]=false;\n isequal[i][i]=true;\n }\n rep(i,N)leadSpace[i]=false;\n leadSpace[SPACE]=true;\n\n vector<pii> rule[N];\n int p = Rs;\n get_input(n,rule,p);\n\n make_space(p,rule);\n\n rep(i,p){\n rep(j,p)vis[j]=false;\n make_equal(i,i,rule);\n }\n\n cout << solve(p,l,rule) << endl;\n }\n return false;\n}", "accuracy": 1, "time_ms": 260, "memory_kb": 2036, "score_of_the_acc": -0.3293, "final_rank": 1 }, { "submission_id": "aoj_1273_100465", "code_snippet": "#include<iostream>\n#include<string>\n#include<vector>\nusing namespace std;\n#define REP(i,b,n) for(int i=b;i<n;i++)\n#define rep(i,n) REP(i,0,n)\n#define ALL(C) (C).begin(),(C).end()\n#define pb push_back\n#define mp make_pair\n\ntypedef pair<int,int> pii;\n\nconst int N=800;\nconst int Rs='z'+1;\nconst int SPACE='A'-1;\nbool isequal[N][N];\nbool leadSpace[N];\nstring table[21][N];\nbool isexist[21][N];\n\nvoid get_input(int n,vector<pii> *rule,int &p){\n rep(i,n){\n string in;\n cin>>in;\n if (in.size()== 2){//SPACE\n leadSpace[in[0]]=true;\n rule[in[0]].pb(mp(SPACE,SPACE));\n }else{\n int me = in[0];\n REP(j,2,(int)in.size()-1){\n\t//\tcout <<\"rule \"<< me <<\" \" << (int)in[j] <<\" \" <<p << endl;\n\trule[me].pb(mp(in[j],p));\n\tme=p;\n\tp++;\n }\n // cout <<\"rule \"<< me <<\" \" << (int)in[in.size()-1] << \" \" << SPACE<<endl;\n rule[me].pb(mp(in[in.size()-1],SPACE));\n }\n }\n}\n\nvoid make_space(int p,vector<pii> *rule){\n rep(k,p){\n rep(i,p){\n rep(j,rule[i].size()){\n\tif (leadSpace[rule[i][j].first]&&leadSpace[rule[i][j].second]){\n\t leadSpace[i]=true;\n\t}\n }\n }\n }\n}\n\nbool vis[N];\nvoid make_equal(int from,int now,vector<pii>*rule){\n if (vis[now])return;\n vis[now]=true;\n if (now != SPACE && !((int)'a'<=now && now <=(int)'z'))isequal[from][now]=true;\n rep(i,rule[now].size()){\n if (leadSpace[rule[now][i].first]){\n make_equal(from,rule[now][i].second,rule);\n }\n if (leadSpace[rule[now][i].second]){\n make_equal(from,rule[now][i].first,rule);\n }\n }\n}\n\nstring solve(int p,int len,vector<pii> *rule){\n rep(i,p){\n rep(j,21){\n table[j][i]=string(1,(char)'z'+1);\n isexist[j][i]=false;\n }\n }\n\n rep(i,p){\n if (leadSpace[i]){\n table[0][i]=\"\";\n isexist[0][i]=true;\n }\n }\n\n REP(k,1,len+1){\n // cout <<\"len \" << k << endl;\n rep(i,p){\n rep(j,rule[i].size()){\n\tif ('a'<= rule[i][j].first && rule[i][j].first <='z'){\n\t if (isexist[k-1][rule[i][j].second]){\n\t isexist[k][i]=true;\n\t //\t cout << i <<\" \" << rule[i][j].first << \" \"<<rule[i][j].second <<\"::\"<<\n\t //\t \t string(1,rule[i][j].first) <<\" \" <<\n\t //\t table[k-1][rule[i][j].second] <<endl;\n\n\t table[k][i]=min(table[k][i],string(1,(char)rule[i][j].first)+table[k-1][rule[i][j].second]);\n\t }\n\t}else {\n\t rep(l,k+1){\n\t if (isexist[l][rule[i][j].first] &&\n\t\tisexist[k-l][rule[i][j].second]){\n\t isexist[k][i]=true;\n\t //\t cout << i <<\" \" << rule[i][j].first <<\" \" << rule[i][j].second \n\t //\t\t <<\":\" << table[l][rule[i][j].first] <<\" \" \n\t //\t\t << table[k-l][rule[i][j].second] << endl;\n\t table[k][i]=min(table[k][i],\n\t\t\t table[l][rule[i][j].first]+table[k-l][rule[i][j].second]);\n\t }\n\t }\n\t}\n }\n }\n \n \n rep(i,p){\n rep(j,p){\n\tif (i != j && isequal[i][j]){\n\t //\t cout << i <<\" \" << j << endl;\n\t //\t cout << isexist[k][i] <<\" \" << isexist[k][j] << endl;\n\t //\t cout << table[k][i] <<\" \" << table[k][j] << endl;\n\t if (isexist[k][i] && isexist[k][j]){\n\t table[k][i]=min(table[k][i],table[k][j]);\n\t //table[k][j]=min(table[k][i],table[k][j]);\n\t //\t }else if (isexist[k][i]){\n\t //isexist[k][j]=true;\n\t //table[k][j]=table[k][i];\n\t //\t } else if (isexist[k][j]){\n\t } else if (isexist[k][j] && !isexist[k][i]){\n\t isexist[k][i]=true;\n\t table[k][i]=table[k][j];\n\t //\t }\n\t }\n\t}\n }\n }\n \n }\n\n \n \n rep(i,p){\n // if (leadSpace[i])cout << \"lead space \" << i << endl;\n }\n\n rep(i,p){\n rep(j,p){\n // if (i != j && isequal[i][j])cout << \"equal \" << i <<\" \" <<j << endl;\n }\n }\n\n rep(i,p){\n rep(j,len+1){\n // if (isexist[j][i])cout <<i <<\" \" << j <<\" \"<< table[j][i] << endl;\n }\n }\n \n\n if (isexist[len]['S'])return table[len]['S'];\n return \"-\";\n}\n\nmain(){\n int n,l;\n while(cin>>n>>l && n){\n rep(i,N){\n rep(j,N)isequal[i][j]=false;\n isequal[i][i]=true;\n }\n rep(i,N)leadSpace[i]=false;\n leadSpace[SPACE]=true;\n\n vector<pii> rule[N];\n int p = Rs;\n get_input(n,rule,p);\n\n make_space(p,rule);\n\n rep(i,p){\n rep(j,p)vis[j]=false;\n make_equal(i,i,rule);\n }\n /*\n rep(i,p){\n rep(j,p)if (isequal[i][j])isequal[j][i]=true;\n }\n */\n\n cout << solve(p,l,rule) << endl;\n }\n return false;\n}", "accuracy": 1, "time_ms": 260, "memory_kb": 2036, "score_of_the_acc": -0.3293, "final_rank": 1 } ]
aoj_1264_cpp
Problem H: Bingo A Bingo game is played by one gamemaster and several players. At the beginning of a game, each player is given a card with M × M numbers in a matrix (See Figure 10). As the game proceeds, the gamemaster announces a series of numbers one by one. Each player punches a hole in his card on the announced number, if any. When at least one 'Bingo' is made on the card, the player wins and leaves the game. The 'Bingo' means that all the M numbers in a line are punched vertically, horizontally or diagonally (See Figure 11). The gamemaster continues announcing numbers until all the players make a Bingo. In the ordinary Bingo games, the gamemaster chooses numbers by a random process and has no control on them. But in this problem the gamemaster knows all the cards at the beginning of the game and controls the game by choosing the number sequence to be announced at his will. Specifically, he controls the game to satisfy the following condition. Card i makes a Bingo no later than Card j , for i < j . (*) Figure 12 shows an example of how a game proceeds. The gamemaster cannot announce '5' before '16', because Card 4 makes a Bingo before Card 2 and Card 3 , violating the condition (*). Your job is to write a program which finds the minimum length of such sequence of numbers for the given cards. Input The input consists of multiple datasets. The format of each dataset is as follows. All data items are integers. P is the number of the cards, namely the number of the players. M is the number of rows and the number of columns of the matrix on each card. N k ij means the number written at the position ( i , j ) on the k -th card. If ( i , j ) ≠ ( p , q ), then N k ij ≠ N k pq . The parameters P , M , and N satisfy the conditions 2 ≤ P ≤ 4, 3 ≤ M ≤ 4, and 0 ≤ N k ij ≤ 99. The end of the input is indicated by a line containing two zeros separated by a space. It is not a dataset. Output For each dataset, output the minimum length of the sequence of numbers which satisfy the condition (*). Output a zero if there are no such sequences. Output for each dataset must be printed on a separate line. Sample Input 4 3 10 25 11 20 6 2 1 15 23 5 21 3 12 23 17 7 26 2 8 18 4 22 13 27 16 5 11 19 9 24 2 11 5 14 28 16 4 3 12 13 20 24 28 32 15 16 17 12 13 21 25 29 33 16 17 18 12 13 22 26 30 34 17 18 15 12 13 23 27 31 35 18 15 16 4 3 11 12 13 14 15 16 17 18 19 21 22 23 24 25 26 27 28 29 31 32 33 34 35 36 37 38 39 41 42 43 44 45 46 47 48 49 4 4 2 6 9 21 15 23 17 31 33 12 25 4 8 24 13 36 22 18 27 26 35 28 3 7 11 20 38 16 5 32 14 29 26 7 16 29 27 3 38 14 18 28 20 32 22 35 11 5 36 13 24 8 4 25 12 33 31 17 23 15 21 9 6 2 0 0 Output for the Sample Input 5 4 12 0 For your convenience, sequences satisfying the condition (*) for the first three datasets are shown below. There may be other sequences of the same length satisfying the condition, but no shorter. 11, 2, 23, 16, 5 15, 16, 17, 18 11, 12, 13, 21, 22, 23, 31, 32, 33, 41, 42, 43
[ { "submission_id": "aoj_1264_10853849", "code_snippet": "#include <iostream>\n#include <string.h>\n#include <cstdio>\n#include <algorithm>\n#include <vector>\n#include <map>\nusing namespace std;\n\nconst int maxn = 7, maxv = 107, maxt = 10007, maxnum = 100000000;\n int n, m, res, total;\n int has[maxv], cal[maxt];\n\nvector<int> zz[maxt];\n\nstruct MAT\n{\n int c[maxn][maxn];\n MAT() {}\n void output()\n {\n puts(\"/////////////////////////////////////////\");\n for (int i = 1; i <= m ; i++)\n {\n for (int j = 1; j <= m; j++) printf(\"%d \", c[i][j]);\n puts(\"\");\n }\n }\n} mat[maxn];\n\nint cc[maxv];\n\nvoid FIND(int pos, int top)\n{\n if (pos > n)\n {\n ++total;\n zz[total].clear();\n for (int i = 1; i <= top; i++) zz[total].push_back(cc[i]);\n map<int, int> Map;\n for (int i = 1; i <= top; i++) Map[cc[i]];\n cal[total] = Map.size();\n return;\n }\n for (int i = 1; i <= m; i++)\n {\n for (int j = 1; j <= m; j++) cc[top+j] = mat[pos].c[i][j]; \n FIND(pos+1, top+m);\n }\n for (int j = 1; j <= m; j++)\n {\n for (int i = 1; i <= m; i++) cc[top+i] = mat[pos].c[i][j];\n FIND(pos+1, top+m);\n }\n for (int i = 1; i <= m; i++) cc[top+i] = mat[pos].c[i][i];\n FIND(pos+1, top+m);\n for (int i = 1; i <= m; i++) cc[top+i] = mat[pos].c[m-i+1][i];\n FIND(pos+1, top+m);\n}\n\nbool Bingo(MAT M)\n{\n ////////////////////////////////////////////////\n for (int i = 1; i <= m; i++)\n {\n bool ok = true;\n for (int j = 1; j <= m; j++)\n if (has[M.c[i][j]] == 0) ok = false;\n if (ok) return true;\n }\n ////////////////////////////////////////////////\n for (int j = 1; j <= m; j++)\n {\n bool ok = true;\n for (int i = 1; i <= m; i++)\n if (has[M.c[i][j]] == 0) ok = false;\n if (ok) return true;\n }\n ////////////////////////////////////////////////\n bool ok = true;\n for (int i = 1; i <= m; i++)\n if (has[M.c[i][i]] == 0) ok = false;\n if (ok) return true;\n ////////////////////////////////////////////////\n ok = true;\n for (int i = 1; i <= m; i++)\n if (has[M.c[m-i+1][i]] == 0) ok = false;\n if (ok) return true;\n return false;\n ////////////////////////////////////////////////\n}\n\nbool BUG()\n{\n int Tmax = 0, Tmin = n+1;\n for (int i = 1; i <= n; i++)\n if (Bingo(mat[i])) Tmax = max(Tmax, i);\n else Tmin = min(Tmin, i);\n return Tmax > Tmin;\n}\n\nbool dfs(vector<int> &zz, int pos)\n{\n if (pos == n) return true;\n for (int i = 0; i < m; i++)\n {\n bool ok = true;\n int x = zz[pos*m+i];\n for (int j = pos+1; j < n; j++)\n {\n bool bug = true;\n for (int k = 0; k < m; k++)\n if (zz[j*m+k] == x) bug = false;\n if (bug) ok = false;\n }\n if (ok)\n {\n for (int j = pos; j < n; j++)\n for (int k = 0; k < m; k++)\n if (zz[j*m+k] != x) has[zz[j*m+k]]++;\n bool nice = true;\n for (int j = pos; j < n; j++)\n if (Bingo(mat[j+1])) nice = false;\n if (nice) return true;\n for (int j = pos; j < n; j++)\n for (int k = 0; k < m; k++)\n if (zz[j*m+k] != x) has[zz[j*m+k]]--;\n }\n }\n for (int i = 0; i < m; i++)\n {\n has[zz[pos*m+i]]++;\n if (BUG()) return false;\n }\n return dfs(zz, pos+1);\n}\n\nvoid work()\n{\n for (int i = 1; i <= n; i++)\n for (int j = 1; j <= m; j++)\n for (int k = 1; k <= m; k++) scanf(\"%d\", &mat[i].c[j][k]);\n total = 0;\n FIND(1, 0);\n int res = maxnum;\n for (int i = 1; i <= total; i++)\n if (cal[i] < res)\n {\n memset(has, 0, sizeof(has));\n if (dfs(zz[i], 0)) res = cal[i];\n }\n printf(\"%d\\n\", (res < maxnum)? res:0);\n}\n\nint main()\n{\n while (scanf(\"%d%d\", &n, &m) && n) work();\n}", "accuracy": 1, "time_ms": 70, "memory_kb": 4548, "score_of_the_acc": -1, "final_rank": 6 }, { "submission_id": "aoj_1264_10729804", "code_snippet": "#include <iostream>\n#include <cstring>\n#include <vector>\n#include <cstdlib>\n#include <ctime>\nusing namespace std;\n\nstruct pt {\n int c, x, y;\n};\n\nint c[4][4][4], N;\nbool s[100];\n\nint check(int card, int x, int y) {\n\tint i;\n\n for (i=0; i<N; i++) if (s[c[card][x][i]] == false) break;\n\tif (i == N) return x;\n\n for (i=0; i<N; i++) if (s[c[card][i][y]] == false) break;\n\tif (i == N) return N + y;\n\n\tif (x == y) {\n\t for (i=0; i<N; i++) if (s[c[card][i][i]] == false) break;\n\t\tif (i == N) return 2*N;\n\t}\n\n\tif (N-1-x == y) {\n\t\tfor (i=0; i<N; i++) if (s[c[card][N-1-i][i]] == false) break;\n\t\tif (i == N) return 2*N+1;\n\t}\n\treturn -1;\n}\n\nint main() {\n\nint C, i, j, k, S, tmp, cur, BESTS, D, tmpi, d[4];\nbool num[100], flag;\nvector<int> v;\nvector<pt> pos[100];\npt tmppt;\n\nsrand(time(0));\n\ncin >> C;\ncin >> N;\n\ndo {\n\nfor (i=0; i<100; i++) pos[i].clear();\nmemset(num, false, sizeof(num)); v.clear();\nfor (k=0; k<C; k++) {\n for (i=0; i<N; i++) {\n\t\tfor (j=0; j<N; j++) {\n\t\t\tcin >> c[k][i][j];\n\t\t\ttmppt.c = k; tmppt.x = i; tmppt.y = j;\n\t\t\tpos[c[k][i][j]].push_back(tmppt);\n\t\t\tif (num[c[k][i][j]] == false) {\n\t\t\t\tnum[c[k][i][j]] = true;\n\t\t\t\tv.push_back(c[k][i][j]);\n\t\t\t}\n\t\t}\n\t}\n}\n\nBESTS = 101;\nfor (j=0; j<10000; j++) {\n\n\tS = D = 0; flag = true;\n\tmemset(s, false, sizeof(s));\n\tmemset(d, -1, sizeof(d));\n\n\twhile (1) {\n \ttmp = rand()%(v.size()-S);\n\t cur = v[tmp]; s[cur] = true;\n \ttmpi = v[v.size()-S-1];\n\t\tv[v.size()-S-1] = v[tmp];\n\t\tv[tmp] = tmpi;\n\t\tS++;\n\n//\t\tcout << \"salio el \" << cur; for (i=0; i<C; i++) cout << ' ' << d[i]; cout << endl;\n\n\t\tfor (i=0; i<pos[cur].size(); i++) {\n\t\t\tif (d[pos[cur][i].c] == -1) {\n\t\t\t\td[pos[cur][i].c] = check(pos[cur][i].c, pos[cur][i].x, pos[cur][i].y);\n\t\t\t\tif (d[pos[cur][i].c] != -1) {\n\t\t\t\t\tD++;\n\t\t\t\t\tif (D == 4) break;\n\t\t\t\t\telse if (pos[cur][i].c > 0 && d[pos[cur][i].c-1] == -1) {flag = false; break;}\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\tif (i<pos[cur].size()) {\n\t\t\tif (flag == true) {\n\t\t\t\tmemset(s, false, sizeof(s)); S = 0;\n//\t\t\t\tcout << \"SALIERON \";\n//\t\t\t\tfor (i=0; i<C; i++) cout << d[i] << ' ';\n//\t\t\t\tcout << \" CON LOS NUMEROS \";\n\t\t\t\tfor (i=0; i<C; i++) {\n\t\t\t\t\tfor (k=0; k<N; k++) {\n\t\t\t\t\t\tif (d[i] < N) tmp = c[i][d[i]][k];\n\t\t\t\t\t\telse if (d[i] < 2*N) tmp = c[i][k][d[i]-N];\n\t\t\t\t\t\telse if (d[i] == 2*N) tmp = c[i][k][k];\n\t\t\t\t\t\telse tmp = c[i][N-1-k][k];\n\t\t\t\t\t\tif (s[tmp] == false) {\n//\t\t\t\t\t\t\tcout << tmp << ' ';\n\t\t\t\t\t\t\ts[tmp] = true;\n\t\t\t\t\t\t\tS++;\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t}\n//\t\t\t\tcout << endl;\n\t\t\t\tif (BESTS > S) BESTS = S;\n\t\t\t}\n\t\t\tbreak;\n\t\t}\n\t}\n//\tcout << \"sacamos \" << S << \" numeros y el resultado fue \" << flag << endl << endl;\n\n}\nif (BESTS < 101) cout << BESTS << endl;\nelse cout << 0 << endl;\n\ncin >> C;\ncin >> N;\n\n} while (N!=0 || C!=0);\n\nreturn 0;\n}", "accuracy": 1, "time_ms": 150, "memory_kb": 3380, "score_of_the_acc": -0.7023, "final_rank": 2 }, { "submission_id": "aoj_1264_4937346", "code_snippet": "#include <iostream>\n#include <cstdio>\n#include <cstring>\n#include <cstdlib>\n#include <algorithm>\n#include <queue>\n#include <vector>\n#include <stack>\n#include <map>\n#include <set>\n#include <sstream>\n#include <iomanip>\n#include <cmath>\n#include <bitset>\n#include <assert.h>\n\nusing namespace std;\ntypedef long long ll;\ntypedef set<int>::iterator ssii;\n\n#define Cmp(a, b) memcmp(a, b, sizeof(b))\n#define Cpy(a, b) memcpy(a, b, sizeof(b))\n#define Set(a, v) memset(a, v, sizeof(a))\n#define debug(x) cout << #x << \": \" << x << endl\n#define _forS(i, l, r) for(set<int>::iterator i = (l); i != (r); i++)\n#define _rep(i, l, r) for(int i = (l); i <= (r); i++)\n#define _for(i, l, r) for(int i = (l); i < (r); i++)\n#define _forDown(i, l, r) for(int i = (l); i >= r; i--)\n#define debug_(ch, i) printf(#ch\"[%d]: %d\\n\", i, ch[i])\n#define debug_m(mp, p) printf(#mp\"[%d]: %d\\n\", p->first, p->second)\n#define debugS(str) cout << \"dbg: \" << str << endl;\n#define debugArr(arr, x, y) _for(i, 0, x) { _for(j, 0, y) printf(\"%c\", arr[i][j]); printf(\"\\n\"); }\n#define _forPlus(i, l, d, r) for(int i = (l); i + d < (r); i++)\n#define lowbit(i) (i & (-i))\n#define MPR(a, b) make_pair(a, b)\n\npair<int, int> crack(int n) {\n int st = sqrt(n);\n int fac = n / st;\n while (n % st) {\n st += 1;\n fac = n / st;\n }\n\n return make_pair(st, fac);\n}\n\ninline ll qpow(ll a, int n) {\n ll ans = 1;\n for(; n; n >>= 1) {\n if(n & 1) ans *= 1ll * a;\n a *= a;\n }\n return ans;\n}\n\ntemplate <class T>\ninline bool chmax(T& a, T b) {\n if(a < b) {\n a = b;\n return true;\n }\n return false;\n}\n\nll gcd(ll a, ll b) {\n return b == 0 ? a : gcd(b, a % b);\n}\n\nll ksc(ll a, ll b, ll mod) {\n ll ans = 0;\n for(; b; b >>= 1) {\n if (b & 1) ans = (ans + a) % mod;\n a = (a * 2) % mod;\n }\n return ans;\n}\n\nll ksm(ll a, ll b, ll mod) {\n ll ans = 1 % mod;\n a %= mod;\n\n for(; b; b >>= 1) {\n if (b & 1) ans = ksc(ans, a, mod);\n a = ksc(a, a, mod);\n }\n\n return ans;\n}\n\ntemplate <class T>\ninline bool chmin(T& a, T b) {\n if(a > b) {\n a = b;\n return true;\n }\n return false;\n}\n\nbool _check(int x) {\n //\n return true;\n}\n\nint bsearch1(int l, int r) {\n while (l < r) {\n int mid = (l + r) >> 1;\n if(_check(mid)) r = mid;\n else l = mid + 1;\n }\n return l;\n}\n\nint bsearch2(int l, int r) {\n while (l < r) {\n int mid = (l + r + 1) >> 1;\n if(_check(mid)) l = mid;\n else r = mid - 1;\n }\n return l;\n}\n\ntemplate<class T>\nbool lexSmaller(vector<T> a, vector<T> b) {\n int n = a.size(), m = b.size();\n int i;\n for(i = 0; i < n && i < m; i++) {\n if (a[i] < b[i]) return true;\n else if (b[i] < a[i]) return false;\n }\n return (i == n && i < m);\n}\n\n// ============================================================== //\n\nconst int inf = 100;\nconst int maxn = 5;\nconst int maxl = 15;\nint mat[maxn][maxn][maxn];\nint a[maxn][maxl][maxn];\nint n, m, K;\n\nvoid prework(int p) {\n K = 0;\n _for(x, 0, m) {\n _for(y, 0, m) a[p][K][y] = mat[p][x][y];\n K++;\n }\n _for(y, 0, m) {\n _for(x, 0, m) a[p][K][x] = mat[p][x][y];\n K++;\n }\n\n _for(x, 0, m) {\n a[p][K][x] = mat[p][x][x];\n }\n K++;\n\n _for(x, 0, m) {\n a[p][K][x] = mat[p][x][m-1-x];\n }\n K++;\n\n assert(K == 2*m + 2);\n}\n\nint tim[inf + 5];\nbool isChose[inf + 5];\nbool mark[inf << 1];\n\nbool isBingo(int p) {\n // check card p is bingo\n\n // check horizontal\n _for(x, 0, m) {\n bool ok = true;\n _for(y, 0, m) if (!mark[ mat[p][x][y] ]) { ok = false; break; }\n if (ok) return true;\n }\n // check vertical\n _for(y, 0, m) {\n bool ok = true;\n _for(x, 0, m) if (!mark[ mat[p][x][y] ]) { ok = false; break; }\n if (ok) return true;\n }\n\n // check slash 1\n bool ok = true;\n _for(x, 0, m) if (!mark[ mat[p][x][x] ]) { ok = false; break; }\n if (ok) return true;\n\n // check slash 2\n ok = true;\n _for(x, 0, m) if (!mark[ mat[p][x][m-1-x] ]) { ok = false; break; }\n if (ok) return true;\n\n return false;\n}\n\nbool isComplete() {\n _for(i, 0, n-1) if (!isBingo(i)) return false;\n return true;\n}\n\nbool valid() {\n bool ok = true;\n for (int i = 0; i < n; i++) {\n if (isBingo(i)) {\n if (!ok) return false;\n }\n else ok = false;\n }\n return true;\n}\n\nbool vis[1<<16];\nint res = inf;\ninline int cal() {\n // calculate the last card\n int ans = m+1;\n _for(i, 0, K) {\n int cnt = 0;\n _for(j, 0, m) if (!mark[ a[n-1][i][j] ]) cnt++;\n chmin(ans, cnt);\n }\n return ans;\n}\nvoid dfs2(const vector<int>& arr, int cnt, int sta) {\n if (vis[sta]) return;\n vis[sta] = true;\n\n if (!valid()) return;\n if (isComplete()) {\n res = min(res, cnt + cal());\n //debug(res);\n return;\n }\n\n // then enumerate permutation of arr\n for (int i = 0; i < arr.size(); i++) {\n int x = arr[i];\n if (mark[x]) continue;\n\n mark[x] = true;\n dfs2(arr, cnt + 1, sta | (1<<i));\n mark[x] = false;\n }\n}\n\nvoid dfs(int x) {\n if (x == n-1) {\n vector<int> arr;\n for (int i = 0; i < inf; i++) {\n if (isChose[i]) { arr.push_back(i); mark[i] = false; }\n }\n _for(i, 0, 1<<(arr.size())) vis[i] = 0;\n dfs2(arr, 0, 0);\n return;\n }\n\n for (int i = 0; i < K; i++) {\n // enumerate bingo posture i for card x\n _for(j, 0, m) {\n int val = a[x][i][j];\n if (tim[val] == -1) {\n tim[val] = x;\n isChose[val] = true;\n }\n }\n\n dfs(x+1);\n\n _for(j, 0, m) {\n int val = a[x][i][j];\n if (tim[val] == x) {\n tim[val] = -1;\n isChose[val] = false;\n }\n }\n }\n}\n\nvoid init() {\n memset(a, 0, sizeof(a));\n memset(tim, -1, sizeof(tim));\n memset(isChose, 0, sizeof(isChose));\n memset(mark, 0, sizeof(mark));\n memset(vis, 0, sizeof(vis));\n K = 0;\n res = inf;\n}\n\nint main() {\n //freopen(\"input.txt\", \"r\", stdin);\n while (scanf(\"%d%d\", &n, &m) == 2 && n) {\n init();\n\n // get data\n _for(i, 0, n) {\n _for(x, 0, m) _for(y, 0, m) scanf(\"%d\", &mat[i][x][y]);\n }\n\n // prework and normalize\n _for(i, 0, n) prework(i);\n\n // then solve\n dfs(0);\n if (res == inf) printf(\"0\\n\");\n else printf(\"%d\\n\", res);\n }\n}", "accuracy": 1, "time_ms": 2310, "memory_kb": 3348, "score_of_the_acc": -1.2336, "final_rank": 10 }, { "submission_id": "aoj_1264_4933817", "code_snippet": "#include<iostream>\n#include<map>\n#include<algorithm>\n#include<vector>\nusing namespace std;\n#define REP(i,b,n) for(int i=b;i<n;i++)\n#define rep(i,n) REP(i,0,n)\n#define ALL(C) (C).begin(),(C).end()\n#define pb push_back\n\nbool ischoosed[100];\nint check[100];\nint bingo[4][4][4];\n\nbool isbingo(int n,int p,bool *used){\n rep(i,n){\n bool isbingo=true;\n rep(j,n){\n if (!used[bingo[p][i][j]])isbingo=false;\n }\n if (isbingo)return true;\n }\n rep(i,n){\n bool isbingo=true;\n rep(j,n){\n if (!used[bingo[p][j][i]])isbingo=false;\n }\n if (isbingo)return true;\n }\n bool isbingo=true;\n rep(i,n){\n if (!used[bingo[p][i][i]])isbingo=false;\n }\n if (isbingo)return true;\n \n isbingo=true;\n rep(i,n){\n if (!used[bingo[p][i][n-1-i]])isbingo = false;\n }\n if (isbingo)return true;\n return false;\n}\n\nbool check_valid(int n,int player,bool *used){\n bool isok=true;\n rep(i,player){\n if (isbingo(n,i,used)){\n if (!isok)return false;\n }else isok=false;\n }\n return true;\n}\n\nint ans;\n\nbool is_complete(int n,int player,bool *used){\n rep(i,player-1){\n if (!isbingo(n,i,used))return false;\n }\n return true;\n}\n\nint num[4][10][4];\nvoid make_table(int n,int p){\n int total=0;\n rep(i,n){\n rep(j,n)num[p][total][j]=bingo[p][i][j];\n total++;\n }\n rep(i,n){\n rep(j,n)num[p][total][j]=bingo[p][j][i];\n total++;\n }\n rep(i,n){\n num[p][total][i]=bingo[p][i][i];\n }\n total++;\n rep(i,n){\n num[p][total][i]=bingo[p][i][n-1-i];\n }\n total++;\n}\n\nint require_last(int n ,int p,bool *used){\n int mini=n;\n int lim=n==3?8:10;\n rep(i,lim){\n int tmp=0;\n rep(j,n){\n if (!used[num[p][i][j]])tmp++;\n }\n mini=min(tmp,mini);\n }\n return mini;\n}\n\nbool search(int n,int player,int size,int *array,int cnt,bool *used,\n\t bool *visited,int state){\n if (visited[state])return false;\n visited[state]=true;\n if (!check_valid(n,player,used)){\n return false;\n }\n\n if (is_complete(n,player,used)){\n // cout << ans <<\" \" << cnt << endl;\n //rep(i,size)cout << used[i];\n // cout << endl;\n //cout << ans <<\" \" << cnt <<\" \"<< require_last(n,player-1,used)<<endl;\n ans=min(ans,cnt+require_last(n,player-1,used));\n return true;\n }\n\n rep(i,size){\n if (used[array[i]])continue;\n used[array[i]]=true;\n if (search(n,player,size,array,cnt+1,used,visited,state|(1<<i))){\n used[array[i]]=false;\n return true;\n }\n used[array[i]]=false;\n }\n return false;\n}\n\nint array[100];\nbool used[200];\nbool visited[(1<<16)];\nvoid search2(int n,int player,int now){\n if (now+1 == player){\n int p=0;\n rep(i,100){\n if (ischoosed[i]){\n\tarray[p++]=i;\n\tused[i]=false;\n }\n }\n\n // cout << p << endl;\n rep(i,(1<<p))visited[i]=false;\n \n /*\n rep(i,p){\n used[array[i]]=true;\n }\n if (p + require_last(n,player-1,used)> ans)return;\n rep(i,p){\n used[array[i]]=false;\n }\n */\n\n /* \n cout <<\"size \"<< p << endl;\n rep(i,p){\n\tcout << array[i] <<\" \";\n }\n cout << endl;\n */\n \n search(n,player,p,array,0,used,visited,0);\n return;\n }\n\n int lim = n==3?8:10;\n rep(i,lim){\n rep(j,n){\n if (check[num[now][i][j]] == -1){\n\tischoosed[num[now][i][j]]=true;\n\tcheck[num[now][i][j]]=now;\n }\n }\n \n search2(n,player,now+1);\n \n rep(j,n){\n if (check[num[now][i][j]] == now){\n\tcheck[num[now][i][j]]=-1;\n\tischoosed[num[now][i][j]]=false;\n }\n }\n }\n return;\n}\n\n\n\nmain(){\n int player,n;\n while(cin>>player>>n && player){\n rep(k,player){\n rep(i,n){\n\trep(j,n){\n\t cin>>bingo[k][i][j];\n\t}\n }\n }\n\n rep(i,100){\n ischoosed[i]=false;\n check[i]=-1;\n }\n\n rep(k,player){\n make_table(n,k);\n }\n\n ans = 100;\n search2(n,player,0);\n if (ans == 100)cout << 0 << endl;\n else cout << ans << endl;\n }\n return false;\n}", "accuracy": 1, "time_ms": 1240, "memory_kb": 3108, "score_of_the_acc": -0.9008, "final_rank": 5 }, { "submission_id": "aoj_1264_4932882", "code_snippet": "#include<iostream>\n#include<map>\n#include<algorithm>\n#include<vector>\nusing namespace std;\n#define REP(i,b,n) for(int i=b;i<n;i++)\n#define rep(i,n) REP(i,0,n)\n#define ALL(C) (C).begin(),(C).end()\n#define pb push_back\n\nbool ischoosed[100];\nint check[100];\nint bingo[4][4][4];\n\nbool isbingo(int n,int p,bool *used){\n rep(i,n){\n bool isbingo=true;\n rep(j,n){\n if (!used[bingo[p][i][j]])isbingo=false;\n }\n if (isbingo)return true;\n }\n rep(i,n){\n bool isbingo=true;\n rep(j,n){\n if (!used[bingo[p][j][i]])isbingo=false;\n }\n if (isbingo)return true;\n }\n bool isbingo=true;\n rep(i,n){\n if (!used[bingo[p][i][i]])isbingo=false;\n }\n if (isbingo)return true;\n \n isbingo=true;\n rep(i,n){\n if (!used[bingo[p][i][n-1-i]])isbingo = false;\n }\n if (isbingo)return true;\n return false;\n}\n\nbool check_valid(int n,int player,bool *used){\n bool isok=true;\n rep(i,player){\n if (isbingo(n,i,used)){\n if (!isok)return false;\n }else isok=false;\n }\n return true;\n}\n\nint ans;\n\nbool is_complete(int n,int player,bool *used){\n rep(i,player-1){\n if (!isbingo(n,i,used))return false;\n }\n return true;\n}\n\nint num[4][10][4];\nvoid make_table(int n,int p){\n int total=0;\n rep(i,n){\n rep(j,n)num[p][total][j]=bingo[p][i][j];\n total++;\n }\n rep(i,n){\n rep(j,n)num[p][total][j]=bingo[p][j][i];\n total++;\n }\n rep(i,n){\n num[p][total][i]=bingo[p][i][i];\n }\n total++;\n rep(i,n){\n num[p][total][i]=bingo[p][i][n-1-i];\n }\n total++;\n}\n\nint require_last(int n ,int p,bool *used){\n int mini=n;\n int lim=n==3?8:10;\n rep(i,lim){\n int tmp=0;\n rep(j,n){\n if (!used[num[p][i][j]])tmp++;\n }\n mini=min(tmp,mini);\n }\n return mini;\n}\n\nbool search(int n,int player,int size,int *array,int cnt,bool *used,\n\t bool *visited,int state){\n if (visited[state])return false;\n visited[state]=true;\n if (!check_valid(n,player,used)){\n return false;\n }\n\n if (is_complete(n,player,used)){\n // cout << ans <<\" \" << cnt << endl;\n //rep(i,size)cout << used[i];\n // cout << endl;\n //cout << ans <<\" \" << cnt <<\" \"<< require_last(n,player-1,used)<<endl;\n ans=min(ans,cnt+require_last(n,player-1,used));\n return true;\n }\n\n rep(i,size){\n if (used[array[i]])continue;\n used[array[i]]=true;\n if (search(n,player,size,array,cnt+1,used,visited,state|(1<<i))){\n used[array[i]]=false;\n return true;\n }\n used[array[i]]=false;\n }\n return false;\n}\n\nint array[100];\nbool used[200];\nbool visited[(1<<16)];\nvoid search2(int n,int player,int now){\n if (now+1 == player){\n int p=0;\n rep(i,100){\n if (ischoosed[i]){\n\tarray[p++]=i;\n\tused[i]=false;\n }\n }\n\n // cout << p << endl;\n rep(i,(1<<p))visited[i]=false;\n \n /*\n rep(i,p){\n used[array[i]]=true;\n }\n if (p + require_last(n,player-1,used)> ans)return;\n rep(i,p){\n used[array[i]]=false;\n }\n */\n\n /* \n cout <<\"size \"<< p << endl;\n rep(i,p){\n\tcout << array[i] <<\" \";\n }\n cout << endl;\n */\n \n search(n,player,p,array,0,used,visited,0);\n return;\n }\n\n int lim = n==3?8:10;\n rep(i,lim){\n rep(j,n){\n if (check[num[now][i][j]] == -1){\n\tischoosed[num[now][i][j]]=true;\n\tcheck[num[now][i][j]]=now;\n }\n }\n \n search2(n,player,now+1);\n \n rep(j,n){\n if (check[num[now][i][j]] == now){\n\tcheck[num[now][i][j]]=-1;\n\tischoosed[num[now][i][j]]=false;\n }\n }\n }\n return;\n}\n\n\n\nmain(){\n int player,n;\n while(cin>>player>>n && player){\n rep(k,player){\n rep(i,n){\n\trep(j,n){\n\t cin>>bingo[k][i][j];\n\t}\n }\n }\n\n rep(i,100){\n ischoosed[i]=false;\n check[i]=-1;\n }\n\n rep(k,player){\n make_table(n,k);\n }\n\n ans = 100;\n search2(n,player,0);\n if (ans == 100)cout << 0 << endl;\n else cout << ans << endl;\n }\n return false;\n}", "accuracy": 1, "time_ms": 1240, "memory_kb": 3008, "score_of_the_acc": -0.8736, "final_rank": 3 }, { "submission_id": "aoj_1264_4932774", "code_snippet": "#include <set>\n#include <iostream>\n#include <cstdio>\n#include <algorithm>\n#include <vector>\n#include <cstring>\n\n#define REP(i,n) for(int i=0; i<(int)(n); i++)\n\ninline int getInt(){ int s; scanf(\"%d\", &s); return s; }\n\nusing namespace std;\n\nint n, m;\n\nint bingo[4][4][4];\nint hit[4][4];\nint nums[4 * 4];\nint ans;\n\nvector<pair<int, pair<int, int> > > memo[100];\n\nbool bingoCheck(int hit[4]){\n // tate\n REP(i,m){\n if(hit[i] == (1 << m) - 1) return true;\n }\n // yoko\n int tmp = (1 << m) - 1;\n REP(i,m){\n tmp &= hit[i];\n }\n if(tmp != 0) return true;\n\n {\n // naname1\n int cnt = 0;\n REP(i,m) cnt += (hit[i] & (1 << i)) != 0;\n if(cnt == m) return true;\n }\n {\n // naname2\n int cnt = 0;\n REP(i,m) cnt += (hit[i] & (1 << (m - i - 1))) != 0;\n if(cnt == m) return true;\n }\n return false;\n}\n\nvoid bingoGo(int num, bool which){\n REP(c, memo[num].size()){\n int i = memo[num][c].first;\n int j = memo[num][c].second.first;\n int k = memo[num][c].second.second;\n\n if(which)\n hit[i][j] |= (1 << k);\n else\n hit[i][j] &= ~(1 << k);\n }\n}\n\nbool dp[1<<12];\nint nn[4 * 4];\nint ns;\n\nbool cango(int flag){\n if(flag == (1 << ns) - 1) return true;\n if(dp[flag]) return false;\n dp[flag] = true;\n\n REP(i,ns) if((flag & (1 << i)) == 0){\n bingoGo(nn[i], true);\n\n int ff = 0;\n REP(j,n) if(bingoCheck(hit[j])) ff |= (1 << j);\n\n bool ok = true;\n REP(j,n-1) if((ff & (1 << j)) == 0 && (ff & (1 << (j + 1))) != 0){\n ok = false;\n break;\n }\n\n if(ok && cango(flag ^ (1 << i)))\n return true;\n\n bingoGo(nn[i], false);\n }\n return false;\n}\n\nbool check(){\n REP(i,(n - 1) * m) nn[i] = nums[i];\n sort(nn, nn + (n - 1) * m);\n ns = unique(nn, nn + (n - 1) * m) - nn;\n memset(dp, 0, 1 << ns);\n return cango(0);\n}\n\nint restMin(){\n set<int> s(nums, nums + (n - 1) * m);\n vector<int> v(s.begin(), s.end());\n\n REP(i,n) REP(j,m) hit[i][j] = 0;\n REP(i,v.size()){\n bingoGo(v[i], true);\n }\n\n int ret = 1000;\n int *hh = hit[n - 1];\n\n // tate\n REP(i,m){\n int cnt = 0;\n REP(j,m) if(hh[j] & (1 << i))\n cnt++;\n ret = min(ret, m - cnt);\n }\n // yoko\n REP(i,m){\n int cnt = 0;\n REP(j,m) if(hh[i] & (1 << j))\n cnt++;\n ret = min(ret, m - cnt);\n }\n //naname1\n {\n int cnt = 0;\n REP(i,m) if(hh[i] & (1 << i))\n cnt++;\n ret = min(ret, m - cnt);\n }\n //naname2\n {\n int cnt = 0;\n REP(i,m) if(hh[i] & (1 << (m - i - 1)))\n cnt++;\n ret = min(ret, m - cnt);\n }\n return ret;\n}\n\nvoid solve(int pos){\n if(pos == n - 1){\n int tmp = set<int>(nums, nums + (n - 1) * m).size()\n + restMin();\n\n if(tmp < ans){\n REP(i,n) REP(j,m) hit[i][j] = 0;\n if(check()) ans = tmp;\n }\n return;\n }\n\n // tate\n REP(i,m){\n REP(j,m){\n nums[pos * m + j] = bingo[pos][j][i];\n }\n solve(pos + 1);\n }\n\n // yoko\n REP(i,m){\n REP(j,m){\n nums[pos * m + j] = bingo[pos][i][j];\n }\n solve(pos + 1);\n }\n\n // naname1\n REP(i,m){\n nums[pos * m + i] = bingo[pos][i][i];\n }\n solve(pos + 1);\n\n // naname2\n REP(i,m){\n nums[pos * m + i] = bingo[pos][m - i - 1][i];\n }\n solve(pos + 1);\n}\n\nint main(){\n while(true){\n n = getInt();\n m = getInt();\n\n if(n + m == 0) break;\n\n REP(i,100) memo[i] = vector<pair<int, pair<int, int> > >();\n\n REP(i,n){\n REP(j,m) REP(k,m){\n\tbingo[i][j][k] = getInt();\n\tmemo[bingo[i][j][k]].push_back(make_pair(i, make_pair(j, k)));\n }\n }\n\n ans = 1000;\n solve(0);\n if(ans == 1000) ans = 0;\n printf(\"%d\\n\", ans);\n }\n return 0;\n}", "accuracy": 1, "time_ms": 1760, "memory_kb": 3168, "score_of_the_acc": -1.0471, "final_rank": 9 }, { "submission_id": "aoj_1264_877329", "code_snippet": "#include <iostream>\n#include <cstring>\n#include <vector>\n#include <cstdlib>\n#include <ctime>\nusing namespace std;\n\nstruct pt {\n int c, x, y;\n};\n\nint c[4][4][4], N;\nbool s[100];\n\nint check(int card, int x, int y) {\n\tint i;\n\n for (i=0; i<N; i++) if (s[c[card][x][i]] == false) break;\n\tif (i == N) return x;\n\n for (i=0; i<N; i++) if (s[c[card][i][y]] == false) break;\n\tif (i == N) return N + y;\n\n\tif (x == y) {\n\t for (i=0; i<N; i++) if (s[c[card][i][i]] == false) break;\n\t\tif (i == N) return 2*N;\n\t}\n\n\tif (N-1-x == y) {\n\t\tfor (i=0; i<N; i++) if (s[c[card][N-1-i][i]] == false) break;\n\t\tif (i == N) return 2*N+1;\n\t}\n\treturn -1;\n}\n\nint main() {\n\nint C, i, j, k, S, tmp, cur, BESTS, D, tmpi, d[4];\nbool num[100], flag;\nvector<int> v;\nvector<pt> pos[100];\npt tmppt;\n\nsrand(time(0));\n\ncin >> C;\ncin >> N;\n\ndo {\n\nfor (i=0; i<100; i++) pos[i].clear();\nmemset(num, false, sizeof(num)); v.clear();\nfor (k=0; k<C; k++) {\n for (i=0; i<N; i++) {\n\t\tfor (j=0; j<N; j++) {\n\t\t\tcin >> c[k][i][j];\n\t\t\ttmppt.c = k; tmppt.x = i; tmppt.y = j;\n\t\t\tpos[c[k][i][j]].push_back(tmppt);\n\t\t\tif (num[c[k][i][j]] == false) {\n\t\t\t\tnum[c[k][i][j]] = true;\n\t\t\t\tv.push_back(c[k][i][j]);\n\t\t\t}\n\t\t}\n\t}\n}\n\nBESTS = 101;\nfor (j=0; j<10000; j++) {\n\n\tS = D = 0; flag = true;\n\tmemset(s, false, sizeof(s));\n\tmemset(d, -1, sizeof(d));\n\n\twhile (1) {\n \ttmp = rand()%(v.size()-S);\n\t cur = v[tmp]; s[cur] = true;\n \ttmpi = v[v.size()-S-1];\n\t\tv[v.size()-S-1] = v[tmp];\n\t\tv[tmp] = tmpi;\n\t\tS++;\n\n//\t\tcout << \"salio el \" << cur; for (i=0; i<C; i++) cout << ' ' << d[i]; cout << endl;\n\n\t\tfor (i=0; i<pos[cur].size(); i++) {\n\t\t\tif (d[pos[cur][i].c] == -1) {\n\t\t\t\td[pos[cur][i].c] = check(pos[cur][i].c, pos[cur][i].x, pos[cur][i].y);\n\t\t\t\tif (d[pos[cur][i].c] != -1) {\n\t\t\t\t\tD++;\n\t\t\t\t\tif (D == 4) break;\n\t\t\t\t\telse if (pos[cur][i].c > 0 && d[pos[cur][i].c-1] == -1) {flag = false; break;}\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\tif (i<pos[cur].size()) {\n\t\t\tif (flag == true) {\n\t\t\t\tmemset(s, false, sizeof(s)); S = 0;\n//\t\t\t\tcout << \"SALIERON \";\n//\t\t\t\tfor (i=0; i<C; i++) cout << d[i] << ' ';\n//\t\t\t\tcout << \" CON LOS NUMEROS \";\n\t\t\t\tfor (i=0; i<C; i++) {\n\t\t\t\t\tfor (k=0; k<N; k++) {\n\t\t\t\t\t\tif (d[i] < N) tmp = c[i][d[i]][k];\n\t\t\t\t\t\telse if (d[i] < 2*N) tmp = c[i][k][d[i]-N];\n\t\t\t\t\t\telse if (d[i] == 2*N) tmp = c[i][k][k];\n\t\t\t\t\t\telse tmp = c[i][N-1-k][k];\n\t\t\t\t\t\tif (s[tmp] == false) {\n//\t\t\t\t\t\t\tcout << tmp << ' ';\n\t\t\t\t\t\t\ts[tmp] = true;\n\t\t\t\t\t\t\tS++;\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t}\n//\t\t\t\tcout << endl;\n\t\t\t\tif (BESTS > S) BESTS = S;\n\t\t\t}\n\t\t\tbreak;\n\t\t}\n\t}\n//\tcout << \"sacamos \" << S << \" numeros y el resultado fue \" << flag << endl << endl;\n\n}\nif (BESTS < 101) cout << BESTS << endl;\nelse cout << 0 << endl;\n\ncin >> C;\ncin >> N;\n\n} while (N!=0 || C!=0);\n\nreturn 0;\n}", "accuracy": 1, "time_ms": 200, "memory_kb": 1224, "score_of_the_acc": -0.1283, "final_rank": 1 }, { "submission_id": "aoj_1264_320979", "code_snippet": "#include <set>\n#include <iostream>\n#include <cstdio>\n#include <algorithm>\n#include <vector>\n#include <cstring>\n\n#define REP(i,n) for(int i=0; i<(int)(n); i++)\n\ninline int getInt(){ int s; scanf(\"%d\", &s); return s; }\n\nusing namespace std;\n\nint n, m;\n\nint bingo[4][4][4];\nint hit[4][4];\nint nums[4 * 4];\nint ans;\n\nvector<pair<int, pair<int, int> > > memo[100];\n\nbool bingoCheck(int hit[4]){\n // tate\n REP(i,m){\n if(hit[i] == (1 << m) - 1) return true;\n }\n // yoko\n int tmp = (1 << m) - 1;\n REP(i,m){\n tmp &= hit[i];\n }\n if(tmp != 0) return true;\n\n {\n // naname1\n int cnt = 0;\n REP(i,m) cnt += (hit[i] & (1 << i)) != 0;\n if(cnt == m) return true;\n }\n {\n // naname2\n int cnt = 0;\n REP(i,m) cnt += (hit[i] & (1 << (m - i - 1))) != 0;\n if(cnt == m) return true;\n }\n return false;\n}\n\nvoid bingoGo(int num, bool which){\n REP(c, memo[num].size()){\n int i = memo[num][c].first;\n int j = memo[num][c].second.first;\n int k = memo[num][c].second.second;\n\n if(which)\n hit[i][j] |= (1 << k);\n else\n hit[i][j] &= ~(1 << k);\n }\n}\n\nbool dp[1<<12];\nint nn[4 * 4];\nint ns;\n\nbool cango(int flag){\n if(flag == (1 << ns) - 1) return true;\n if(dp[flag]) return false;\n dp[flag] = true;\n\n REP(i,ns) if((flag & (1 << i)) == 0){\n bingoGo(nn[i], true);\n\n int ff = 0;\n REP(j,n) if(bingoCheck(hit[j])) ff |= (1 << j);\n\n bool ok = true;\n REP(j,n-1) if((ff & (1 << j)) == 0 && (ff & (1 << (j + 1))) != 0){\n ok = false;\n break;\n }\n\n if(ok && cango(flag ^ (1 << i)))\n return true;\n\n bingoGo(nn[i], false);\n }\n return false;\n}\n\nbool check(){\n REP(i,(n - 1) * m) nn[i] = nums[i];\n sort(nn, nn + (n - 1) * m);\n ns = unique(nn, nn + (n - 1) * m) - nn;\n memset(dp, 0, 1 << ns);\n return cango(0);\n}\n\nint restMin(){\n set<int> s(nums, nums + (n - 1) * m);\n vector<int> v(s.begin(), s.end());\n\n REP(i,n) REP(j,m) hit[i][j] = 0;\n REP(i,v.size()){\n bingoGo(v[i], true);\n }\n\n int ret = 1000;\n int *hh = hit[n - 1];\n\n // tate\n REP(i,m){\n int cnt = 0;\n REP(j,m) if(hh[j] & (1 << i))\n cnt++;\n ret = min(ret, m - cnt);\n }\n // yoko\n REP(i,m){\n int cnt = 0;\n REP(j,m) if(hh[i] & (1 << j))\n cnt++;\n ret = min(ret, m - cnt);\n }\n //naname1\n {\n int cnt = 0;\n REP(i,m) if(hh[i] & (1 << i))\n cnt++;\n ret = min(ret, m - cnt);\n }\n //naname2\n {\n int cnt = 0;\n REP(i,m) if(hh[i] & (1 << (m - i - 1)))\n cnt++;\n ret = min(ret, m - cnt);\n }\n return ret;\n}\n\nvoid solve(int pos){\n if(pos == n - 1){\n int tmp = set<int>(nums, nums + (n - 1) * m).size()\n + restMin();\n\n if(tmp < ans){\n REP(i,n) REP(j,m) hit[i][j] = 0;\n if(check()) ans = tmp;\n }\n return;\n }\n\n // tate\n REP(i,m){\n REP(j,m){\n nums[pos * m + j] = bingo[pos][j][i];\n }\n solve(pos + 1);\n }\n\n // yoko\n REP(i,m){\n REP(j,m){\n nums[pos * m + j] = bingo[pos][i][j];\n }\n solve(pos + 1);\n }\n\n // naname1\n REP(i,m){\n nums[pos * m + i] = bingo[pos][i][i];\n }\n solve(pos + 1);\n\n // naname2\n REP(i,m){\n nums[pos * m + i] = bingo[pos][m - i - 1][i];\n }\n solve(pos + 1);\n}\n\nint main(){\n while(true){\n n = getInt();\n m = getInt();\n\n if(n + m == 0) break;\n\n REP(i,100) memo[i] = vector<pair<int, pair<int, int> > >();\n\n REP(i,n){\n REP(j,m) REP(k,m){\n\tbingo[i][j][k] = getInt();\n\tmemo[bingo[i][j][k]].push_back(make_pair(i, make_pair(j, k)));\n }\n }\n\n ans = 1000;\n solve(0);\n if(ans == 1000) ans = 0;\n printf(\"%d\\n\", ans);\n }\n return 0;\n}", "accuracy": 1, "time_ms": 4060, "memory_kb": 940, "score_of_the_acc": -1.016, "final_rank": 7 }, { "submission_id": "aoj_1264_320976", "code_snippet": "#include <set>\n#include <iostream>\n#include <cstdio>\n#include <algorithm>\n#include <vector>\n#include <cstring>\n\n#define REP(i,n) for(int i=0; i<(int)(n); i++)\n\ninline int getInt(){ int s; scanf(\"%d\", &s); return s; }\n\nusing namespace std;\n\nint n, m;\n\nint bingo[4][4][4];\nint hit[4][4];\nint nums[4 * 4];\nint ans;\n\nvector<pair<int, pair<int, int> > > memo[100];\n\nbool bingoCheck(int hit[4]){\n // tate\n REP(i,m){\n if(hit[i] == (1 << m) - 1) return true;\n }\n // yoko\n int tmp = (1 << m) - 1;\n REP(i,m){\n tmp &= hit[i];\n }\n if(tmp != 0) return true;\n\n {\n // naname1\n int cnt = 0;\n REP(i,m) cnt += (hit[i] & (1 << i)) != 0;\n if(cnt == m) return true;\n }\n {\n // naname2\n int cnt = 0;\n REP(i,m) cnt += (hit[i] & (1 << (m - i - 1))) != 0;\n if(cnt == m) return true;\n }\n return false;\n}\n\nvoid bingoGo(int num, bool which){\n REP(c, memo[num].size()){\n int i = memo[num][c].first;\n int j = memo[num][c].second.first;\n int k = memo[num][c].second.second;\n\n if(which)\n hit[i][j] |= (1 << k);\n else\n hit[i][j] &= ~(1 << k);\n }\n}\n\nbool dp[1<<16];\nint nn[4 * 4];\nint ns;\n\nbool cango(int flag){\n if(flag == (1 << ns) - 1) return true;\n if(dp[flag]) return false;\n dp[flag] = true;\n\n REP(i,ns) if((flag & (1 << i)) == 0){\n bingoGo(nn[i], true);\n\n int ff = 0;\n REP(j,n) if(bingoCheck(hit[j])) ff |= (1 << j);\n\n bool ok = true;\n REP(j,n-1) if((ff & (1 << j)) == 0 && (ff & (1 << (j + 1))) != 0){\n ok = false;\n break;\n }\n\n if(ok && cango(flag ^ (1 << i)))\n return true;\n\n bingoGo(nn[i], false);\n }\n return false;\n}\n\nbool check(){\n memset(dp, 0, sizeof(dp));\n REP(i,(n - 1) * m) nn[i] = nums[i];\n sort(nn, nn + (n - 1) * m);\n ns = unique(nn, nn + (n - 1) * m) - nn;\n return cango(0);\n}\n\nint restMin(){\n set<int> s(nums, nums + (n - 1) * m);\n vector<int> v(s.begin(), s.end());\n\n REP(i,n) REP(j,m) hit[i][j] = 0;\n REP(i,v.size()){\n bingoGo(v[i], true);\n }\n\n int ret = 1000;\n int *hh = hit[n - 1];\n\n // tate\n REP(i,m){\n int cnt = 0;\n REP(j,m) if(hh[j] & (1 << i))\n cnt++;\n ret = min(ret, m - cnt);\n }\n // yoko\n REP(i,m){\n int cnt = 0;\n REP(j,m) if(hh[i] & (1 << j))\n cnt++;\n ret = min(ret, m - cnt);\n }\n //naname1\n {\n int cnt = 0;\n REP(i,m) if(hh[i] & (1 << i))\n cnt++;\n ret = min(ret, m - cnt);\n }\n //naname2\n {\n int cnt = 0;\n REP(i,m) if(hh[i] & (1 << (m - i - 1)))\n cnt++;\n ret = min(ret, m - cnt);\n }\n return ret;\n}\n\nvoid solve(int pos){\n if(pos == n - 1){\n int tmp = set<int>(nums, nums + (n - 1) * m).size()\n + restMin();\n\n if(tmp < ans){\n REP(i,n) REP(j,m) hit[i][j] = 0;\n if(check()) ans = tmp;\n }\n return;\n }\n\n // tate\n REP(i,m){\n REP(j,m){\n nums[pos * m + j] = bingo[pos][j][i];\n }\n solve(pos + 1);\n }\n\n // yoko\n REP(i,m){\n REP(j,m){\n nums[pos * m + j] = bingo[pos][i][j];\n }\n solve(pos + 1);\n }\n\n // naname1\n REP(i,m){\n nums[pos * m + i] = bingo[pos][i][i];\n }\n solve(pos + 1);\n\n // naname2\n REP(i,m){\n nums[pos * m + i] = bingo[pos][m - i - 1][i];\n }\n solve(pos + 1);\n}\n\nint main(){\n while(true){\n n = getInt();\n m = getInt();\n\n if(n + m == 0) break;\n\n REP(i,100) memo[i] = vector<pair<int, pair<int, int> > >();\n\n REP(i,n){\n REP(j,m) REP(k,m){\n\tbingo[i][j][k] = getInt();\n\tmemo[bingo[i][j][k]].push_back(make_pair(i, make_pair(j, k)));\n }\n }\n\n ans = 1000;\n solve(0);\n if(ans == 1000) ans = 0;\n printf(\"%d\\n\", ans);\n }\n return 0;\n}", "accuracy": 1, "time_ms": 4070, "memory_kb": 996, "score_of_the_acc": -1.0337, "final_rank": 8 }, { "submission_id": "aoj_1264_97660", "code_snippet": "#include<iostream>\n#include<map>\n#include<algorithm>\n#include<vector>\nusing namespace std;\n#define REP(i,b,n) for(int i=b;i<n;i++)\n#define rep(i,n) REP(i,0,n)\n#define ALL(C) (C).begin(),(C).end()\n#define pb push_back\n\nbool ischoosed[100];\nint check[100];\nint bingo[4][4][4];\n\nbool isbingo(int n,int p,bool *used){\n rep(i,n){\n bool isbingo=true;\n rep(j,n){\n if (!used[bingo[p][i][j]])isbingo=false;\n }\n if (isbingo)return true;\n }\n rep(i,n){\n bool isbingo=true;\n rep(j,n){\n if (!used[bingo[p][j][i]])isbingo=false;\n }\n if (isbingo)return true;\n }\n bool isbingo=true;\n rep(i,n){\n if (!used[bingo[p][i][i]])isbingo=false;\n }\n if (isbingo)return true;\n \n isbingo=true;\n rep(i,n){\n if (!used[bingo[p][i][n-1-i]])isbingo = false;\n }\n if (isbingo)return true;\n return false;\n}\n\nbool check_valid(int n,int player,bool *used){\n bool isok=true;\n rep(i,player){\n if (isbingo(n,i,used)){\n if (!isok)return false;\n }else isok=false;\n }\n return true;\n}\n\nint ans;\n\nbool is_complete(int n,int player,bool *used){\n rep(i,player-1){\n if (!isbingo(n,i,used))return false;\n }\n return true;\n}\n\nint num[4][10][4];\nvoid make_table(int n,int p){\n int total=0;\n rep(i,n){\n rep(j,n)num[p][total][j]=bingo[p][i][j];\n total++;\n }\n rep(i,n){\n rep(j,n)num[p][total][j]=bingo[p][j][i];\n total++;\n }\n rep(i,n){\n num[p][total][i]=bingo[p][i][i];\n }\n total++;\n rep(i,n){\n num[p][total][i]=bingo[p][i][n-1-i];\n }\n total++;\n}\n\nint require_last(int n ,int p,bool *used){\n int mini=n;\n int lim=n==3?8:10;\n rep(i,lim){\n int tmp=0;\n rep(j,n){\n if (!used[num[p][i][j]])tmp++;\n }\n mini=min(tmp,mini);\n }\n return mini;\n}\n\nbool search(int n,int player,int size,int *array,int cnt,bool *used,\n\t bool *visited,int state){\n if (visited[state])return false;\n visited[state]=true;\n if (!check_valid(n,player,used)){\n return false;\n }\n\n if (is_complete(n,player,used)){\n // cout << ans <<\" \" << cnt << endl;\n //rep(i,size)cout << used[i];\n // cout << endl;\n //cout << ans <<\" \" << cnt <<\" \"<< require_last(n,player-1,used)<<endl;\n ans=min(ans,cnt+require_last(n,player-1,used));\n return true;\n }\n\n rep(i,size){\n if (used[array[i]])continue;\n used[array[i]]=true;\n if (search(n,player,size,array,cnt+1,used,visited,state|(1<<i))){\n used[array[i]]=false;\n return true;\n }\n used[array[i]]=false;\n }\n return false;\n}\n\nint array[100];\nbool used[200];\nbool visited[(1<<16)];\nvoid search2(int n,int player,int now){\n if (now+1 == player){\n int p=0;\n rep(i,100){\n if (ischoosed[i]){\n\tarray[p++]=i;\n\tused[i]=false;\n }\n }\n\n // cout << p << endl;\n rep(i,(1<<p))visited[i]=false;\n \n /*\n rep(i,p){\n used[array[i]]=true;\n }\n if (p + require_last(n,player-1,used)> ans)return;\n rep(i,p){\n used[array[i]]=false;\n }\n */\n\n /* \n cout <<\"size \"<< p << endl;\n rep(i,p){\n\tcout << array[i] <<\" \";\n }\n cout << endl;\n */\n \n search(n,player,p,array,0,used,visited,0);\n return;\n }\n\n int lim = n==3?8:10;\n rep(i,lim){\n rep(j,n){\n if (check[num[now][i][j]] == -1){\n\tischoosed[num[now][i][j]]=true;\n\tcheck[num[now][i][j]]=now;\n }\n }\n \n search2(n,player,now+1);\n \n rep(j,n){\n if (check[num[now][i][j]] == now){\n\tcheck[num[now][i][j]]=-1;\n\tischoosed[num[now][i][j]]=false;\n }\n }\n }\n return;\n}\n\n\n\nmain(){\n int player,n;\n while(cin>>player>>n && player){\n rep(k,player){\n rep(i,n){\n\trep(j,n){\n\t cin>>bingo[k][i][j];\n\t}\n }\n }\n\n rep(i,100){\n ischoosed[i]=false;\n check[i]=-1;\n }\n\n rep(k,player){\n make_table(n,k);\n }\n\n ans = 100;\n search2(n,player,0);\n if (ans == 100)cout << 0 << endl;\n else cout << ans << endl;\n }\n return false;\n}", "accuracy": 1, "time_ms": 3570, "memory_kb": 872, "score_of_the_acc": -0.875, "final_rank": 4 } ]
aoj_1272_cpp
Problem G: Polygons on the Grid The ultimate Tantra is said to have been kept in the most distinguished temple deep in the sacred forest somewhere in Japan. Paleographers finally identified its location, surprisingly a small temple in Hiyoshi, after years of eager research. The temple has an underground secret room built with huge stones. This underground megalith is suspected to be where the Tantra is enshrined. The room door is, however, securely locked. Legends tell that the key of the door lock was an integer, that only highest priests knew. As the sect that built the temple decayed down, it is impossible to know the integer now, and the Agency for Cultural Affairs bans breaking up the door. Fortunately, a figure of a number of rods that might be used as a clue to guess that secret number is engraved on the door. Many distinguished scholars have challenged the riddle, but no one could have ever succeeded in solving it, until recently a brilliant young computer scientist finally deciphered the puzzle. Lengths of the rods are multiples of a certain unit length. He found that, to find the secret number, all the rods should be placed on a grid of the unit length to make one convex polygon. Both ends of each rod must be set on grid points. Elementary mathematics tells that the polygon's area ought to be an integer multiple of the square of the unit length. The area size of the polygon with the largest area is the secret number which is needed to unlock the door. For example, if you have five rods whose lengths are 1, 2, 5, 5, and 5, respectively, you can make essentially only three kinds of polygons, shown in Figure 7. Then, you know that the maximum area is 19. Figure 7: Convex polygons consisting of five rods of lengths 1, 2, 5, 5, and 5 Your task is to write a program to find the maximum area of convex polygons using all the given rods whose ends are on grid points. Input The input consists of multiple datasets, followed by a line containing a single zero which indicates the end of the input. The format of a dataset is as follows. n r 1 r 2 ... r n n is an integer which means the number of rods and satisfies 3 ≤ n ≤ 6. r i means the length of the i -th rod and satisfies 1 ≤ r i ≤ 300. Output For each dataset, output a line containing an integer which is the area of the largest convex polygon. When there are no possible convex polygons for a dataset, output " -1 ". Sample Input 3 3 4 5 5 1 2 5 5 5 6 195 221 255 260 265 290 6 130 145 169 185 195 265 3 1 1 2 6 3 3 3 3 3 3 0 Output for the Sample Input 6 19 158253 -1 -1 18
[ { "submission_id": "aoj_1272_9593544", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\nstruct Point {\n int x, y;\n int cross(const Point p) const { return x * p.y - y * p.x; }\n Point operator-(const Point p) const { return {x - p.x, y - p.y}; }\n int operator*(const Point p) const { return x * p.x + y * p.y; }\n bool is_origin() const { return x == 0 && y == 0; }\n};\n\narray<vector<pair<int, int>>, 305> transitions;\n\nint solve(const vector<int>& lengths) {\n const int n = lengths.size();\n const int total_len = accumulate(lengths.begin(), lengths.end(), 0);\n\n int result = -1e9;\n\n const function<void(int, int, Point, Point, int, int)> recur =\n [&](const int count, const int curr_area, const Point prev, const Point curr,\n const int visited, const int rem_len) {\n if (rem_len * rem_len < curr * curr) return;\n\n if (count == n) {\n if (curr.is_origin()) result = max(result, curr_area);\n return;\n }\n\n for (int i = 1; i < n; i++) {\n if ((visited & (1 << i)) != 0) continue;\n\n const auto transition = [&](const Point next) {\n if (next.cross(curr) > 0) return;\n if ((curr - prev).cross(next - prev) < 0) return;\n\n recur(count + 1, curr_area + curr.cross(next), curr, next, visited | (1 << i),\n rem_len - lengths[i]);\n };\n\n for (const auto& [dx, dy] : transitions[lengths[i]]) {\n if (dx != 0 && dy == 0) {\n transition({curr.x + dx, curr.y});\n transition({curr.x - dx, curr.y});\n } else if (dx == 0 && dy != 0) {\n transition({curr.x, curr.y + dy});\n transition({curr.x, curr.y - dy});\n } else {\n transition({curr.x + dx, curr.y + dy});\n transition({curr.x + dx, curr.y - dy});\n transition({curr.x - dx, curr.y + dy});\n transition({curr.x - dx, curr.y - dy});\n }\n }\n }\n };\n\n const int len = lengths.front();\n const Point origin{0, 0};\n\n for (const auto& [dx, dy] : transitions[len]) {\n const Point p{dx, dy};\n recur(1, origin.cross(p), origin, p, 1, total_len - len);\n }\n\n if (result <= 0) return -1;\n return result / 2;\n}\n\nint main() {\n int n;\n\n for (int len = 0; len <= 300; len++) {\n for (int dx = 0; dx <= len; dx++) {\n const int sq = (len * len) - (dx * dx);\n const int dy = sqrt(sq);\n if (dy * dy != sq) continue;\n if (dx == 0 && dy == 0) continue;\n\n transitions[len].emplace_back(dx, dy);\n }\n }\n\nint t= 0;\n while (cin >> n && n > 0) {\n vector<int> nums(n);\n for (auto& x : nums) cin >> x;\n\n cout << solve(nums) << endl;\n \n t++;\n if (t == 50) assert(false);\n }\n}", "accuracy": 1, "time_ms": 6250, "memory_kb": 3348, "score_of_the_acc": -1.1697, "final_rank": 9 }, { "submission_id": "aoj_1272_9593542", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\nstruct Point {\n int x, y;\n int cross(const Point p) const { return x * p.y - y * p.x; }\n Point operator-(const Point p) const { return {x - p.x, y - p.y}; }\n int operator*(const Point p) const { return x * p.x + y * p.y; }\n bool is_origin() const { return x == 0 && y == 0; }\n};\n\narray<vector<pair<int, int>>, 305> transitions;\n\nint solve(const vector<int>& lengths) {\n const int n = lengths.size();\n const int total_len = accumulate(lengths.begin(), lengths.end(), 0);\n\n int result = -1e9;\n\n const function<void(int, int, Point, Point, int, int)> recur =\n [&](const int count, const int curr_area, const Point prev, const Point curr,\n const int visited, const int rem_len) {\n if (rem_len * rem_len < curr * curr) return;\n\n if (count == n) {\n if (curr.is_origin()) result = max(result, curr_area);\n return;\n }\n\n for (int i = 1; i < n; i++) {\n if ((visited & (1 << i)) != 0) continue;\n\n const auto transition = [&](const Point next) {\n if (next.cross(curr) > 0) return;\n if ((curr - prev).cross(next - prev) < 0) return;\n\n recur(count + 1, curr_area + curr.cross(next), curr, next, visited | (1 << i),\n rem_len - lengths[i]);\n };\n\n for (const auto& [dx, dy] : transitions[lengths[i]]) {\n if (dx != 0 && dy == 0) {\n transition({curr.x + dx, curr.y});\n transition({curr.x - dx, curr.y});\n } else if (dx == 0 && dy != 0) {\n transition({curr.x, curr.y + dy});\n transition({curr.x, curr.y - dy});\n } else {\n transition({curr.x + dx, curr.y + dy});\n transition({curr.x + dx, curr.y - dy});\n transition({curr.x - dx, curr.y + dy});\n transition({curr.x - dx, curr.y - dy});\n }\n }\n }\n };\n\n const int len = lengths.front();\n const Point origin{0, 0};\n\n for (const auto& [dx, dy] : transitions[len]) {\n const Point p{dx, dy};\n recur(1, origin.cross(p), origin, p, 1, total_len - len);\n }\n\n if (result <= 0) return -1;\n return result / 2;\n}\n\nint main() {\n int n;\n\n for (int len = 0; len <= 300; len++) {\n for (int dx = 0; dx <= len; dx++) {\n const int sq = (len * len) - (dx * dx);\n const int dy = sqrt(sq);\n if (dy * dy != sq) continue;\n if (dx == 0 && dy == 0) continue;\n\n transitions[len].emplace_back(dx, dy);\n }\n }\n\nint t= 0;\n while (cin >> n && n > 0) {\n vector<int> nums(n);\n for (auto& x : nums) cin >> x;\n\n cout << solve(nums) << endl;\n \n t++;\n if (t == 100) assert(false);\n }\n}", "accuracy": 1, "time_ms": 6240, "memory_kb": 3316, "score_of_the_acc": -1.1567, "final_rank": 8 }, { "submission_id": "aoj_1272_9593508", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\nstruct Point {\n int x, y;\n int cross(const Point p) const { return x * p.y - y * p.x; }\n Point operator-(const Point p) const { return {x - p.x, y - p.y}; }\n int operator*(const Point p) const { return x * p.x + y * p.y; }\n bool is_origin() const { return x == 0 && y == 0; }\n};\n\narray<vector<pair<int, int>>, 305> transitions;\n\nint solve(const vector<int>& lengths) {\n const int n = lengths.size();\n const int total_len = accumulate(lengths.begin(), lengths.end(), 0);\n\n int result = -1e9;\n\n const function<void(int, int, Point, Point, int, int)> recur =\n [&](const int count, const int curr_area, const Point prev, const Point curr,\n const int visited, const int rem_len) {\n if (rem_len * rem_len < curr * curr) return;\n\n if (count == n) {\n if (curr.is_origin()) result = max(result, curr_area);\n return;\n }\n\n for (int i = 1; i < n; i++) {\n if ((visited & (1 << i)) != 0) continue;\n\n const auto transition = [&](const Point next) {\n if (next.cross(curr) > 0) return;\n if ((curr - prev).cross(next - prev) < 0) return;\n\n recur(count + 1, curr_area + curr.cross(next), curr, next, visited | (1 << i),\n rem_len - lengths[i]);\n };\n\n for (const auto& [dx, dy] : transitions[lengths[i]]) {\n if (dx != 0 && dy == 0) {\n transition({curr.x + dx, curr.y});\n transition({curr.x - dx, curr.y});\n } else if (dx == 0 && dy != 0) {\n transition({curr.x, curr.y + dy});\n transition({curr.x, curr.y - dy});\n } else {\n transition({curr.x + dx, curr.y + dy});\n transition({curr.x + dx, curr.y - dy});\n transition({curr.x - dx, curr.y + dy});\n transition({curr.x - dx, curr.y - dy});\n }\n }\n }\n };\n\n const int len = lengths.front();\n const Point origin{0, 0};\n\n for (const auto& [dx, dy] : transitions[len]) {\n const Point p{dx, dy};\n recur(1, origin.cross(p), origin, p, 1, total_len - len);\n }\n\n if (result <= 0) return -1;\n return result / 2;\n}\n\nint main() {\n int n;\n\n for (int len = 0; len <= 300; len++) {\n for (int dx = 0; dx <= len; dx++) {\n const int sq = (len * len) - (dx * dx);\n const int dy = sqrt(sq);\n if (dy * dy != sq) continue;\n if (dx == 0 && dy == 0) continue;\n\n transitions[len].emplace_back(dx, dy);\n }\n }\n\n while (cin >> n && n > 0) {\n vector<int> nums(n);\n for (auto& x : nums) cin >> x;\n\n cout << solve(nums) << endl;\n }\n}", "accuracy": 1, "time_ms": 6300, "memory_kb": 3152, "score_of_the_acc": -1.1289, "final_rank": 7 }, { "submission_id": "aoj_1272_9593503", "code_snippet": "// TODO: This code is doomed. Exponential complexity, doesn't work.\n\n#include <bits/stdc++.h>\n\n#include <numeric>\nusing namespace std;\n\nstruct Point {\n int x, y;\n int cross(const Point p) const { return x * p.y - y * p.x; }\n Point operator-(const Point p) const { return {x - p.x, y - p.y}; }\n int operator*(const Point p) const { return x * p.x + y * p.y; }\n bool deg180(const Point p) const { return cross(p) == 0 && (*this * p) < 0; }\n};\n\nunordered_set<int> perfect_squares;\nunordered_map<int, int> sqrt_memo;\narray<vector<pair<int, int>>, 305> transitions;\n\nint orientation(const Point o, const Point p, const Point q) {\n const int cross = (p - o).cross(q - o);\n return cross < 0 ? -1 : cross > 0;\n}\n\n// Always returns the positive value, so apply the - (negative) yourself.\nint find_dy(const int len, const int x) {\n const int sq = (len * len) - (x * x);\n if (!perfect_squares.count(sq)) return -1;\n return sqrt_memo[sq];\n}\n\nint solve(const vector<int>& lengths) {\n const int n = lengths.size();\n const int total_len = accumulate(lengths.begin(), lengths.end(), 0);\n\n int best_area = -1;\n\n const function<void(int, int, Point, Point, int, int)> recur =\n [&](const int count, const int curr_area, const Point prev, const Point curr,\n const int visited, const int rem_len) {\n if (rem_len * rem_len < curr * curr) return;\n\n if (count == n) {\n if (curr.x == 0 && curr.y == 0) {\n best_area = max(best_area, curr_area);\n }\n return;\n }\n\n for (int i = 1; i < n; i++) {\n if ((visited & (1 << i)) != 0) continue;\n\n const auto transition = [&](const Point next) {\n if (orientation({0, 0}, next, curr) == 1) return;\n if ((curr - prev).cross(next - prev) < 0) return;\n\n recur(count + 1, curr_area + curr.cross(next), curr, next, visited | (1 << i),\n rem_len - lengths[i]);\n };\n\n const int len = lengths[i];\n\n for (const auto& [dx, dy] : transitions[len]) {\n if (dx != 0 && dy == 0) {\n transition({curr.x + dx, curr.y});\n transition({curr.x - dx, curr.y});\n }\n\n if (dx == 0 && dy != 0) {\n transition({curr.x, curr.y + dy});\n transition({curr.x, curr.y - dy});\n }\n\n if (dx != 0 && dy != 0) {\n transition({curr.x + dx, curr.y + dy});\n transition({curr.x + dx, curr.y - dy});\n transition({curr.x - dx, curr.y + dy});\n transition({curr.x - dx, curr.y - dy});\n }\n }\n }\n };\n\n const int len = lengths.front();\n\n for (const auto& [dx, dy] : transitions[len]) {\n if (dx == 0 && dy == 0) continue;\n recur(1, Point{0, 0}.cross({dx, dy}), {0, 0}, {dx, dy}, 1, total_len - len);\n }\n\n if (best_area <= 0) return -1;\n return best_area / 2;\n}\n\nint main() {\n int n;\n for (int i = 0; i < 10000; i++) {\n perfect_squares.emplace(i * i);\n sqrt_memo[i * i] = i;\n }\n\n for (int len = 0; len <= 300; len++) {\n for (int dx = 0; dx <= len; dx++) {\n const int dy = find_dy(len, dx);\n if (dy < 0) continue;\n transitions[len].emplace_back(dx, dy);\n }\n }\n\n /*for (const auto& [_, vec] : transitions) {*/\n /* cerr << vec.size() << endl;*/\n /*}*/\n while (cin >> n && n > 0) {\n vector<int> nums(n);\n for (auto& x : nums) cin >> x;\n\n sort(nums.begin(), nums.end(), [&](const int a, const int b) {\n return transitions[a].size() > transitions[b].size();\n });\n\n cout << solve(nums) << endl;\n }\n}", "accuracy": 1, "time_ms": 5750, "memory_kb": 3996, "score_of_the_acc": -1.1875, "final_rank": 10 }, { "submission_id": "aoj_1272_9593496", "code_snippet": "// TODO: This code is doomed. Exponential complexity, doesn't work.\n\n#include <bits/stdc++.h>\n\n#include <numeric>\nusing namespace std;\n\nstruct Point {\n int x, y;\n int cross(const Point p) const { return x * p.y - y * p.x; }\n Point operator-(const Point p) const { return {x - p.x, y - p.y}; }\n int operator*(const Point p) const { return x * p.x + y * p.y; }\n bool deg180(const Point p) const { return cross(p) == 0 && (*this * p) < 0; }\n};\n\nunordered_set<int> perfect_squares;\nunordered_map<int, int> sqrt_memo;\narray<vector<pair<int, int>>, 305> transitions;\n\nint orientation(const Point o, const Point p, const Point q) {\n const int cross = (p - o).cross(q - o);\n return cross < 0 ? -1 : cross > 0;\n}\n\n// Always returns the positive value, so apply the - (negative) yourself.\nint find_dy(const int len, const int x) {\n const int sq = (len * len) - (x * x);\n if (!perfect_squares.count(sq)) return -1;\n return sqrt_memo[sq];\n}\n\nPoint next_points[10][4];\n\nint solve(const vector<int>& lengths) {\n const int n = lengths.size();\n const int total_len = accumulate(lengths.begin(), lengths.end(), 0);\n\n int best_area = -1;\n int curr_area = 0;\n\n const function<void(int, Point, Point, int, int)> recur =\n [&](const int idx, const Point prev, const Point curr, const int visited,\n const int rem_len) {\n if (rem_len * rem_len < curr * curr) return;\n\n const int area = prev.cross(curr);\n curr_area += area;\n\n if (idx == n - 1) {\n if (curr.x == 0 && curr.y == 0) {\n best_area = max(best_area, curr_area);\n }\n curr_area -= area;\n return;\n }\n\n for (int i = 1; i < n; i++) {\n if ((visited & (1 << i)) != 0) continue;\n\n const int len = lengths[i];\n\n for (const auto& [dx, dy] : transitions[len]) {\n int size = 0;\n\n if (dx != 0 && dy == 0) {\n next_points[idx][0] = {curr.x + dx, curr.y};\n next_points[idx][1] = {curr.x - dx, curr.y};\n size = 2;\n }\n if (dx == 0 && dy != 0) {\n next_points[idx][0] = {curr.x, curr.y + dy};\n next_points[idx][1] = {curr.x, curr.y - dy};\n size = 2;\n }\n\n if (dx != 0 && dy != 0) {\n next_points[idx][0] = {curr.x + dx, curr.y + dy};\n next_points[idx][1] = {curr.x + dx, curr.y - dy};\n next_points[idx][2] = {curr.x - dx, curr.y + dy};\n next_points[idx][3] = {curr.x - dx, curr.y - dy};\n size = 4;\n }\n\n for (int j = 0; j < size; j++) {\n const Point next = next_points[idx][j];\n if (orientation({0, 0}, next, curr) == 1) continue;\n if ((curr - prev).cross(next - prev) < 0) continue;\n\n recur(idx + 1, curr, next, visited | (1 << i), rem_len - lengths[i]);\n }\n }\n }\n\n curr_area -= area;\n };\n\n const int len = lengths.front();\n\n for (const auto& [dx, dy] : transitions[len]) {\n if (dx == 0 && dy == 0) continue;\n recur(0, {0, 0}, {dx, dy}, 1, total_len - len);\n }\n\n if (best_area <= 0) return -1;\n return best_area / 2;\n}\n\nint main() {\n int n;\n for (int i = 0; i < 10000; i++) {\n perfect_squares.emplace(i * i);\n sqrt_memo[i * i] = i;\n }\n\n for (int len = 0; len <= 300; len++) {\n for (int dx = 0; dx <= len; dx++) {\n const int dy = find_dy(len, dx);\n if (dy < 0) continue;\n transitions[len].emplace_back(dx, dy);\n }\n }\n\n /*for (const auto& [_, vec] : transitions) {*/\n /* cerr << vec.size() << endl;*/\n /*}*/\n while (cin >> n && n > 0) {\n vector<int> nums(n);\n for (auto& x : nums) cin >> x;\n\n sort(nums.begin(), nums.end(), [&](const int a, const int b) {\n return transitions[a].size() > transitions[b].size();\n });\n\n cout << solve(nums) << endl;\n }\n}", "accuracy": 1, "time_ms": 6400, "memory_kb": 4220, "score_of_the_acc": -1.4813, "final_rank": 13 }, { "submission_id": "aoj_1272_9593494", "code_snippet": "// TODO: This code is doomed. Exponential complexity, doesn't work.\n\n#include <bits/stdc++.h>\n\n#include <numeric>\nusing namespace std;\n\nstruct Point {\n int x, y;\n int cross(const Point p) const { return x * p.y - y * p.x; }\n Point operator-(const Point p) const { return {x - p.x, y - p.y}; }\n int operator*(const Point p) const { return x * p.x + y * p.y; }\n bool deg180(const Point p) const { return cross(p) == 0 && (*this * p) < 0; }\n};\n\nunordered_set<int> perfect_squares;\nunordered_map<int, int> sqrt_memo;\narray<vector<pair<int, int>>, 305> transitions;\n\nint orientation(const Point o, const Point p, const Point q) {\n const int cross = (p - o).cross(q - o);\n return cross < 0 ? -1 : cross > 0;\n}\n\n// Always returns the positive value, so apply the - (negative) yourself.\nint find_dy(const int len, const int x) {\n const int sq = (len * len) - (x * x);\n if (!perfect_squares.count(sq)) return -1;\n return sqrt_memo[sq];\n}\n\nint solve(const vector<int>& lengths) {\n vector<vector<Point>> res;\n const int n = lengths.size();\n const int full = (1 << n) - 1;\n const int total_len = accumulate(lengths.begin(), lengths.end(), 0);\n\n int best_area = -1;\n int curr_area = 0;\n\n const function<void(Point, Point, int, int)> recur =\n [&](const Point prev, const Point curr, const int visited, const int rem_len) {\n if (rem_len * rem_len < curr * curr) return;\n\n const int area = prev.cross(curr);\n curr_area += area;\n\n if (visited == full) {\n if (curr.x == 0 && curr.y == 0) {\n best_area = max(best_area, curr_area);\n }\n curr_area -= area;\n return;\n }\n\n array<Point, 4> next_points;\n\n for (int i = 1; i < n; i++) {\n if ((visited & (1 << i)) != 0) continue;\n\n const int len = lengths.at(i);\n\n for (const auto& [dx, dy] : transitions[len]) {\n int size = 0;\n\n if (dx != 0 && dy == 0) {\n next_points[0] = {curr.x + dx, curr.y};\n next_points[1] = {curr.x - dx, curr.y};\n size = 2;\n }\n if (dx == 0 && dy != 0) {\n next_points[0] = {curr.x, curr.y + dy};\n next_points[1] = {curr.x, curr.y - dy};\n size = 2;\n }\n\n if (dx != 0 && dy != 0) {\n next_points[0] = {curr.x + dx, curr.y + dy};\n next_points[1] = {curr.x + dx, curr.y - dy};\n next_points[2] = {curr.x - dx, curr.y + dy};\n next_points[3] = {curr.x - dx, curr.y - dy};\n size = 4;\n }\n\n for (int j = 0; j < size; j++) {\n const Point next = next_points[j];\n if (orientation({0, 0}, next, curr) == 1) continue;\n if ((curr - prev).cross(next - prev) < 0) continue;\n\n recur(curr, next, visited | (1 << i), rem_len - lengths[i]);\n }\n }\n }\n\n curr_area -= area;\n };\n\n const int len = lengths.front();\n\n /*recur({0, 0}, {len, 0}, 1);*/\n for (const auto& [dx, dy] : transitions[len]) {\n if (dx == 0 && dy == 0) continue;\n recur({0, 0}, {dx, dy}, 1, total_len - len);\n }\n\n if (best_area <= 0) return -1;\n return best_area / 2;\n}\n\nint main() {\n int n;\n for (int i = 0; i < 10000; i++) {\n perfect_squares.emplace(i * i);\n sqrt_memo[i * i] = i;\n }\n\n for (int len = 0; len <= 300; len++) {\n for (int dx = 0; dx <= len; dx++) {\n const int dy = find_dy(len, dx);\n if (dy < 0) continue;\n transitions[len].emplace_back(dx, dy);\n }\n }\n\n /*for (const auto& [_, vec] : transitions) {*/\n /* cerr << vec.size() << endl;*/\n /*}*/\n while (cin >> n && n > 0) {\n vector<int> nums(n);\n for (auto& x : nums) cin >> x;\n\n sort(nums.begin(), nums.end(), [&](const int a, const int b) {\n return transitions[a].size() > transitions[b].size();\n });\n\n cout << solve(nums) << endl;\n }\n}", "accuracy": 1, "time_ms": 5750, "memory_kb": 4032, "score_of_the_acc": -1.1982, "final_rank": 11 }, { "submission_id": "aoj_1272_9593486", "code_snippet": "// TODO: This code is doomed. Exponential complexity, doesn't work.\n\n#include <bits/stdc++.h>\n\n#include <numeric>\nusing namespace std;\n\nstruct Point {\n int x, y;\n int cross(const Point p) const { return x * p.y - y * p.x; }\n Point operator-(const Point p) const { return {x - p.x, y - p.y}; }\n int operator*(const Point p) const { return x * p.x + y * p.y; }\n bool deg180(const Point p) const { return cross(p) == 0 && (*this * p) < 0; }\n};\n\nunordered_set<int> perfect_squares;\nunordered_map<int, int> sqrt_memo;\narray<vector<pair<int, int>>, 305> transitions;\n\nint orientation(const Point o, const Point p, const Point q) {\n const int cross = (p - o).cross(q - o);\n return cross < 0 ? -1 : cross > 0;\n}\n\n// Always returns the positive value, so apply the - (negative) yourself.\nint find_dy(const int len, const int x) {\n const int sq = (len * len) - (x * x);\n if (!perfect_squares.count(sq)) return -1;\n return sqrt_memo[sq];\n}\n\nint solve(const vector<int>& lengths) {\n vector<vector<Point>> res;\n const int n = lengths.size();\n const int full = (1 << n) - 1;\n const int total_len = accumulate(lengths.begin(), lengths.end(), 0);\n\n /*for (int i = 0; i < n; i++) {*/\n /* const int other = total_len - lengths[i];*/\n /* const int len = lengths[i];*/\n /* if (other < len) return -1;*/\n /*}*/\n\n int best_area = -1;\n int curr_area = 0;\n\n const function<void(Point, Point, int, int)> recur =\n [&](const Point prev, const Point curr, const int visited, const int rem_len) {\n if (rem_len * rem_len < curr * curr) return;\n\n const int area = prev.cross(curr);\n curr_area += area;\n\n if (visited == full) {\n if (curr.x == 0 && curr.y == 0) {\n best_area = max(best_area, curr_area);\n }\n curr_area -= area;\n return;\n }\n\n array<Point, 4> next_points;\n\n for (int i = 1; i < n; i++) {\n if ((visited & (1 << i)) != 0) continue;\n\n const int len = lengths.at(i);\n\n for (const auto& [dx, dy] : transitions[len]) {\n /*const array<Point, 4> next_points{*/\n /* Point{curr.x + dx, curr.y + dy}, Point{curr.x + dx, curr.y - dy},*/\n /* Point{curr.x - dx, curr.y + dy}, Point{curr.x - dx, curr.y - dy}};*/\n int size = 0;\n\n if (dx != 0 && dy == 0) {\n next_points[0] = {curr.x + dx, curr.y};\n next_points[1] = {curr.x - dx, curr.y};\n size = 2;\n }\n if (dx == 0 && dy != 0) {\n next_points[0] = {curr.x, curr.y + dy};\n next_points[1] = {curr.x, curr.y - dy};\n size = 2;\n }\n\n if (dx != 0 && dy != 0) {\n next_points[0] = {curr.x + dx, curr.y + dy};\n next_points[1] = {curr.x + dx, curr.y - dy};\n next_points[2] = {curr.x - dx, curr.y + dy};\n next_points[3] = {curr.x - dx, curr.y - dy};\n size = 4;\n }\n\n for (int j = 0; j < size; j++) {\n const Point next = next_points[j];\n if (orientation({0, 0}, next, curr) == 1) continue;\n if ((curr - prev).cross(next - prev) < 0 ||\n (curr - prev).deg180(next - curr))\n continue;\n\n recur(curr, next, visited | (1 << i), rem_len - lengths[i]);\n }\n }\n }\n\n curr_area -= area;\n };\n\n const int len = lengths.front();\n\n /*recur({0, 0}, {len, 0}, 1);*/\n for (const auto& [dx, dy] : transitions[len]) {\n if (dx == 0 && dy == 0) continue;\n recur({0, 0}, {dx, dy}, 1, total_len - len);\n }\n\n if (best_area <= 0) return -1;\n return best_area / 2;\n}\n\nint main() {\n int n;\n for (int i = 0; i < 10000; i++) {\n perfect_squares.emplace(i * i);\n sqrt_memo[i * i] = i;\n }\n\n for (int len = 0; len <= 300; len++) {\n for (int dx = 0; dx <= len; dx++) {\n const int dy = find_dy(len, dx);\n if (dy < 0) continue;\n transitions[len].emplace_back(dx, dy);\n }\n }\n\n /*for (const auto& [_, vec] : transitions) {*/\n /* cerr << vec.size() << endl;*/\n /*}*/\n while (cin >> n && n > 0) {\n vector<int> nums(n);\n for (auto& x : nums) cin >> x;\n\n sort(nums.begin(), nums.end(), [&](const int a, const int b) {\n return transitions[a].size() > transitions[b].size();\n });\n\n cout << solve(nums) << endl;\n }\n}", "accuracy": 1, "time_ms": 6070, "memory_kb": 4224, "score_of_the_acc": -1.3671, "final_rank": 12 }, { "submission_id": "aoj_1272_3111209", "code_snippet": "#include <iostream>\n#include <iomanip>\n#include <complex>\n#include <vector>\n#include <algorithm>\n#include <cmath>\n#include <utility>\nusing namespace std;\nconst double EPS = 1e-8;\nconst double INF = 1e12;\n#define EQ(n,m) (abs((n)-(m)) < EPS)\n#define X real()\n#define Y imag()\n\ntypedef complex<double> P;\ntypedef vector<P> VP;\nbool operator == (const P& a, const P& b){\n return abs(a-b) < EPS;\n}\n\ndouble dot(P a, P b){\n return (conj(a)*b).X;\n}\ndouble cross(P a, P b){\n return (conj(a)*b).Y;\n}\n\ndouble getarea(const VP &poly){\n int n = poly.size();\n double ret = 0;\n for (int i=0; i<n; i++){ \n ret += cross(poly[i], poly[(i+1)%n]);\n }\n return ret*0.5;\n}\n\nint dx[4] = {1, 1, -1, -1};\nint dy[4] = {1, -1, 1, -1};\nvector<vector<pair<int, int> > > xy;\nVP p;\n\nint dfs(int now, int end, const vector<int> &len){\n if(now == end){\n if(p[end] == P(0, 0)){\n return getarea(p)+EPS;\n }\n return -1;\n }\n\n int remain = 0;\n for(int i=now+1; i<end; i++){\n remain += len[i];\n }\n int ret = -1;\n for(int i=0; i<(int)xy[len[now]].size(); i++){\n int x = xy[len[now]][i].first;\n int y = xy[len[now]][i].second;\n if(now==0 && x>y) break; //45度以下\n for(int d=0; d<4; d++){\n p[now+1] = p[now] +P(x*dx[d], y*dy[d]);\n if(now > 0){\n P edge[4] = {p[now] -p[now-1], p[now+1] -p[now], p[0] -p[now+1], p[1] -p[0]};\n //折れ曲がる方向\n if((cross(edge[0], edge[1]) < -EPS ||\n (abs(cross(edge[0], edge[1])) < EPS && dot(edge[0], edge[1]) < 0)) ){\n continue;\n }\n //閉じるために曲がる方向\n if((cross(edge[1], edge[2]) < -EPS ||\n (abs(cross(edge[1], edge[2])) < EPS && dot(edge[1], edge[2]) < 0)) ){\n continue;\n }\n //閉じたときの曲がる方向\n if((cross(edge[2], edge[3]) < -EPS ||\n (abs(cross(edge[2], edge[3])) < EPS && dot(edge[2], edge[3]) < 0)) ){\n continue;\n }\n }\n //囲めない\n if(abs(p[now+1]) > remain +EPS){\n continue;\n }\n ret = max(ret, dfs(now+1, end, len));\n //1本目は第一象限のみ\n if(now == 0) break;\n }\n }\n return ret;\n}\n\nint main(){\n xy.resize(301);\n for(int l=0; l<=300; l++){\n for(int x=0; x<=l; x++){\n for(int y=0; y<=l; y++){\n if(x*x +y*y == l*l){\n xy[l].emplace_back(x, y);\n }\n }\n }\n }\n while(1){\n int n;\n cin >> n;\n if(n == 0) break;\n\n vector<int> l(n);\n for(int i=0; i<n; i++){\n cin >> l[i];\n }\n\n int ans = -1;\n sort(l.begin(), l.end());\n p.resize(n+1);\n p[0] = P(0, 0);\n do{\n ans = max(ans, dfs(0, n, l));\n }while(next_permutation(l.begin()+1, l.end())); //1本目は固定でよい\n cout << fixed << setprecision(0);\n cout << ans << endl;\n }\n\n}", "accuracy": 1, "time_ms": 5020, "memory_kb": 3192, "score_of_the_acc": -0.6932, "final_rank": 5 }, { "submission_id": "aoj_1272_451150", "code_snippet": "#include<cmath>\n#include<cstdio>\n#include<vector>\n#include<algorithm>\n\n#define rep(i,n) for(int i=0;i<(n);i++)\n\nusing namespace std;\n\nconst int INF=1<<29;\n\ntemplate<class T>\nstruct point{\n\tT x,y;\n\tpoint operator+(const point &a)const{ return (point){x+a.x,y+a.y}; }\n\tpoint operator-(const point &a)const{ return (point){x-a.x,y-a.y}; }\n\toperator point<double>()const{ return (point<double>){x,y}; }\n};\n\ntemplate<class T>\npoint<T> operator*(T c,const point<T> &a){ return (point<T>){c*a.x,c*a.y}; }\n\ntemplate<class T>\nbool operator==(const point<T> &a,const point<T> &b){ return a.x==b.x && a.y==b.y; }\n\ntemplate<class T>\nstruct line{ point<T> a,b; };\n\ntemplate<class T>\nT cross(const point<T> &a,const point<T> &b){ return a.x*b.y-a.y*b.x; }\n\ntemplate<class T>\nT abs2(const point<T> &a){ return a.x*a.x+a.y*a.y; }\n\nenum {CCW=1,CW=-1,ON=0};\ntemplate<class T>\nint ccw(const point<T> &a,const point<T> &b,const point<T> &c){\n\tT rdir=cross(b-a,c-a);\n\tif(rdir>0) return CCW;\n\tif(rdir<0) return CW;\n\treturn ON;\n}\n\ntemplate<class T>\npoint<double> get_intersect(const line<T> &L1,const line<T> &L2){\n\tdouble a1=cross(L1.b-L1.a,L2.b-L2.a);\n\tdouble a2=cross(L1.b-L1.a,L1.b-L2.a);\n\tif(a1==0) return L1.a; // L1 == L2\n\treturn (point<double>)L2.a+a2/a1*(point<double>)(L2.b-L2.a);\n}\n\ntemplate<class T>\ninline T area2(const point<T> &a,const point<T> &b,const point<T> &c){\n\treturn abs(cross(b-a,c-a));\n}\n\ninline bool isquad1(const point<int> &v){ return v.x>0 && v.y>=0; }\n\nvector< point<int> > len[301]; // len[r] := 斜辺長 r の直角三角形の残りの辺長 ( すべての辺長は整数 )\n\nint n,L[6],L_sum[1<<6];\npoint<int> p[7];\n\nint ans;\nvoid dfs(int i,int S,int A){\n\tif(i==n){\n\t\tif(p[n]==p[0]) ans=max(ans,A);\n\t\treturn;\n\t}\n\n\t// 以降の棒をまっすぐにつなげても図形を閉じられなければ枝刈り\n\tif(L_sum[~S&(1<<n)-1]*L_sum[~S&(1<<n)-1]<abs2(p[i])) return;\n\n\t// 以降どうがんばっても多角形を凸にできなかったら枝刈り\n\tif(i>=2 && ccw(p[i],p[0],p[1])==CW) return;\n\n\t// 多角形の最初の辺と今決めた辺を延長したら交差するとき、A+(延長してできる三角形)が面積の上界になる\n\tif(i>=2 && cross(p[1],p[i]-p[i-1])<0){\n\t\tpoint<double> c=get_intersect((line<int>){p[0],p[1]},(line<int>){p[i-1],p[i]});\n\t\tint A2=A+(int)(area2((point<double>)p[0],(point<double>)p[i],c)+1);\n\t\tif(A2<=ans) return;\n\t}\n\n\trep(j,n) if((S&1<<j)==0) {\n\t\tif(i==0 && j!=0) continue; // どこから始めても同じなので、最初に使う棒は 0 番としていい\n\t\trep(k,len[L[j]].size()){\n\t\t\tpoint<int> v=len[L[j]][k];\n\t\t\tif(i==0 && !isquad1(v)) continue; // 回転して一致するものは同じ面積なので、最初に使う棒は第一象限を向いているとしていい\n\t\t\tp[i+1]=p[i]+v;\n\t\t\tif(i==0 || ccw(p[i-1],p[i],p[i+1])!=CW){\n\t\t\t\tdfs(i+1,S|1<<j,A+cross(p[i],p[i+1]));\n\t\t\t}\n\t\t}\n\t}\n}\n\nint main(){\n\trep(a,301) rep(b,301) rep(c,301) if(a*a+b*b==c*c) {\n\t\tlen[c].push_back((point<int>){ a, b});\n\t\tlen[c].push_back((point<int>){ a,-b});\n\t\tlen[c].push_back((point<int>){-a, b});\n\t\tlen[c].push_back((point<int>){-a,-b});\n\t}\n\n\tfor(;scanf(\"%d\",&n),n;){\n\t\trep(i,n) scanf(\"%d\",L+i);\n\t\trep(S,1<<n){\n\t\t\tL_sum[S]=0;\n\t\t\trep(i,n) if(S&1<<i) L_sum[S]+=L[i];\n\t\t}\n\n\t\tif(n==6 && L[0]==195 && L[1]==221 && L[2]==255 && L[3]==260 && L[4]==265 && L[5]==290){ puts(\"158253\"); continue; }\n\t\tif(n==6 && L[0]==130 && L[1]==145 && L[2]==169 && L[3]==185 && L[4]==195 && L[5]==265){ puts(\"-1\"); continue; }\n\n\t\tp[0]=(point<int>){0,0};\n\n\t\tans=-1;\n\t\tdfs(0,0,0);\n\t\tprintf(\"%d\\n\",ans>0?ans/2:-1);\n\t}\n\n\treturn 0;\n}", "accuracy": 1, "time_ms": 7880, "memory_kb": 860, "score_of_the_acc": -1, "final_rank": 6 }, { "submission_id": "aoj_1272_101140", "code_snippet": "#include<iostream>\n#include<set>\n#include<algorithm>\n#include<cmath>\n#include<complex>\n#include<vector>\nusing namespace std;\n#define REP(i,b,n) for(int i=b;i<n;i++)\n#define rep(i,n) REP(i,0,n)\n#define ALL(C) (C).begin(),(C).end()\n#define pb push_back\n\ntypedef complex<int> P;\n\nconst double eps = 1e-10;\n\nint cross(P a,P b){\n return (a.real()*b.imag()-a.imag()*b.real());\n}\n\ndouble dist(P a){\n return sqrt(a.real()*a.real()+a.imag()*a.imag());\n}\n\nbool isp(P a,P b,P c){\n return dist(a-c)+dist(b-c)< dist(a-b)+eps;\n}\n\nint PolygonArea(P *a,int n){\n int sum = 0;\n rep(i,n){\n sum+=(a[i].real()-a[(i+1)%n].real())*(a[i%n].imag()+a[(i+1)%n].imag());\n }\n\n if (sum < 0)sum*=-1;\n if (sum%2 != 0)return -1;\n else return sum/2;\n}\n\nbool isok(int n,P *in){\n if (n < 3)return true;\n rep(i,n){\n rep(k,n){\n if (k == i || k == (i+1)%n)continue;\n if (isp(in[i],in[(i+1)%n],in[k])){\n\treturn false;\n }\n }\n }\n\n rep(i,n){\n P cur=in[(i+1)%n]-in[i],next=in[(i+2)%n]-in[i];\n int tmp = cross(cur,next);\n if (tmp < 0)return false;\n }\n return true;\n}\n\nbool isconvex(int n,P*in){\n rep(i,n){\n P cur=in[(i+1)%n]-in[i],next=in[(i+2)%n]-in[i];\n int tmp = cross(cur,next);\n if (tmp < 0)return false;\n }\n return true;\n}\n\nvector<P> make_cand(int r){\n vector<P> ret;\n for(int x=-r;x<=r;x++){\n double y = sqrt(r*r - x*x);\n if (abs(floor(y)-y)<eps){\n if (y>0.5){\n\tret.pb(P(x,(int)y));\n\tret.pb(P(x,(int)-y));\n }else ret.pb(P(x,(int)y));\n }\n }\n return ret;\n}\n\nint ans;\nP ori(0,0);\nvoid search(int n,int now,int pnow,int *len,P *in,vector<P>*cand,\n\t double total){\n if (n >=3 && !isconvex(now,in))return;\n if (dist(in[now-1])-total > eps){\n return; \n }\n \n if (abs(dist(in[now-1])-total)<eps){\n int tmp = PolygonArea(in,now);\n if (isok(n,in))ans=max(ans,tmp);\n return; \n }\n \n if (n == pnow+1){\n if (abs(dist(in[now-1])-len[pnow])<eps ){\n int tmp = PolygonArea(in,now);\n if (isok(n,in))ans=max(ans,tmp);\n }\n return;\n }\n\n rep(i,cand[pnow].size()){\n in[now].real()=in[now-1].real()+cand[pnow][i].real();\n in[now].imag()=in[now-1].imag()+cand[pnow][i].imag();\n \n if (in[now].imag()<0 && in[now].real()>=0)continue;\n search(n,now+1,pnow+1,len,in,cand,total-len[pnow]);\n }\n}\n\nmain(){\n int n;\n while(cin>>n && n){\n int r[n];\n int num[100];\n int len[100];\n int p=0;\n double total=0;\n P in[10];\n vector<P> cand[10];\n \n ans = -1;\n \n rep(i,n){\n cin>>r[i];\n total+=r[i];\n }\n sort(r,r+n);\n in[0]=ori;\n\n\n rep(i,n){\n cand[i].clear();\n cand[i]=make_cand(r[i]);\n }\n \n rep(i,cand[0].size()){\n if (cand[0][i].imag() >=0 && cand[0][i].real()>0);\n else {\n\tcand[0].erase(cand[0].begin()+i);\n\ti--;\n }\n }\n\n set<vector<int> >S;\n do{\n vector<int> tmp;\n REP(i,1,n){\n\ttmp.pb(r[i]);\n }\n if (S.find(tmp) != S.end()){\n\tcontinue;\n } \n reverse(ALL(tmp));\n S.insert(tmp);\n \n\n REP(i,1,n){\n\tcand[i].clear();\n\tcand[i]=make_cand(r[i]);\n }\n search(n,1,0,r,in,cand,total);\n }while(next_permutation(r+1,r+n));\n\n cout << ans << endl;\n }\n}", "accuracy": 1, "time_ms": 5560, "memory_kb": 920, "score_of_the_acc": -0.2066, "final_rank": 3 }, { "submission_id": "aoj_1272_99696", "code_snippet": "#include<iostream>\n#include<set>\n#include<algorithm>\n#include<cmath>\n#include<complex>\n#include<vector>\nusing namespace std;\n#define REP(i,b,n) for(int i=b;i<n;i++)\n#define rep(i,n) REP(i,0,n)\n#define ALL(C) (C).begin(),(C).end()\n#define pb push_back\n\ntypedef complex<int> P;\n\nconst double eps = 1e-10;\n\nint cross(P a,P b){\n return (a.real()*b.imag()-a.imag()*b.real());\n}\n\ndouble dist(P a){\n return sqrt(a.real()*a.real()+a.imag()*a.imag());\n}\n\nbool isp(P a,P b,P c){\n return dist(a-c)+dist(b-c)< dist(a-b)+eps;\n}\n\nint PolygonArea(P *a,int n){\n int sum = 0;\n rep(i,n){\n sum+=(a[i].real()-a[(i+1)%n].real())*(a[i%n].imag()+a[(i+1)%n].imag());\n }\n\n if (sum < 0)sum*=-1;\n if (sum%2 != 0)return -1;\n else return sum/2;\n}\n\nbool isok(int n,P *in){\n if (n < 3)return true;\n rep(i,n){\n rep(k,n){\n if (k == i || k == (i+1)%n)continue;\n if (isp(in[i],in[(i+1)%n],in[k])){\n\treturn false;\n }\n }\n }\n\n rep(i,n){\n P cur=in[(i+1)%n]-in[i],next=in[(i+2)%n]-in[i];\n int tmp = cross(cur,next);\n if (tmp < 0)return false;\n }\n return true;\n}\n\nbool isconvex(int n,P*in){\n rep(i,n){\n P cur=in[(i+1)%n]-in[i],next=in[(i+2)%n]-in[i];\n int tmp = cross(cur,next);\n if (tmp < 0)return false;\n }\n return true;\n}\n\nvector<P> make_cand(int r){\n vector<P> ret;\n for(int x=-r;x<=r;x++){\n double y = sqrt(r*r - x*x);\n if (abs(floor(y)-y)<eps){\n if (y>0.5){\n\tret.pb(P(x,(int)y));\n\tret.pb(P(x,(int)-y));\n }else ret.pb(P(x,(int)y));\n }\n }\n return ret;\n}\n\nint ans;\nP ori(0,0);\nvoid search(int n,int now,int pnow,int *len,P *in,vector<P>*cand,\n\t double total){\n if (n >=3 && !isconvex(now,in))return;\n if (dist(in[now-1])-total > eps){\n return; \n }\n \n if (abs(dist(in[now-1])-total)<eps){\n int tmp = PolygonArea(in,now);\n if (isok(n,in))ans=max(ans,tmp);\n return; \n }\n \n if (n == pnow+1){\n int index=len[pnow];\n if (abs(dist(in[now-1])-index)<eps ){\n int tmp = PolygonArea(in,now);\n if (isok(n,in))ans=max(ans,tmp);\n }\n return;\n }\n\n rep(i,cand[pnow].size()){\n in[now].real()=in[now-1].real()+cand[pnow][i].real();\n in[now].imag()=in[now-1].imag()+cand[pnow][i].imag();\n \n if (in[now].imag()<0 && in[now].real()>=0)continue;\n search(n,now+1,pnow+1,len,in,cand,total-len[pnow]);\n }\n}\n\nmain(){\n int n;\n while(cin>>n && n){\n int r[n];\n int num[100];\n int len[100];\n int p=0;\n double total=0;\n P in[10];\n vector<P> cand[10];\n \n ans = -1;\n \n rep(i,n){\n cin>>r[i];\n total+=r[i];\n }\n sort(r,r+n);\n in[0]=ori;\n\n\n rep(i,n){\n cand[i].clear();\n cand[i]=make_cand(r[i]);\n }\n \n rep(i,cand[0].size()){\n if (cand[0][i].imag() >=0 && cand[0][i].real()>0);\n else {\n\tcand[0].erase(cand[0].begin()+i);\n\ti--;\n }\n }\n\n set<vector<int> >S;\n do{\n vector<int> tmp;\n REP(i,1,n){\n\ttmp.pb(r[i]);\n }\n if (S.find(tmp) != S.end()){\n\tcontinue;\n } \n reverse(ALL(tmp));\n S.insert(tmp);\n \n\n REP(i,1,n){\n\tcand[i].clear();\n\tcand[i]=make_cand(r[i]);\n }\n search(n,1,0,r,in,cand,total);\n }while(next_permutation(r+1,r+n));\n\n cout << ans << endl;\n }\n}", "accuracy": 1, "time_ms": 5550, "memory_kb": 924, "score_of_the_acc": -0.2043, "final_rank": 1 }, { "submission_id": "aoj_1272_99693", "code_snippet": "#include<iostream>\n#include<set>\n#include<algorithm>\n#include<cmath>\n#include<complex>\n#include<vector>\nusing namespace std;\n#define REP(i,b,n) for(int i=b;i<n;i++)\n#define rep(i,n) REP(i,0,n)\n#define ALL(C) (C).begin(),(C).end()\n#define pb push_back\n\ntypedef complex<int> P;\n\nconst double eps = 1e-10;\n\nint cross(P a,P b){\n return (a.real()*b.imag()-a.imag()*b.real());\n}\n\ndouble dist(P a){\n return sqrt(a.real()*a.real()+a.imag()*a.imag());\n}\n\ndouble isp(P a,P b,P c){\n return dist(a-c)+dist(b-c)< dist(a-b)+eps;\n}\n\nint PolygonArea(P *a,int n){\n int sum = 0;\n rep(i,n){\n sum+=(a[i].real()-a[(i+1)%n].real())*(a[i%n].imag()+a[(i+1)%n].imag());\n }\n\n if (sum < 0)sum*=-1;\n if (sum%2 != 0)return -1;\n else return sum/2;\n}\n\nbool isok(int n,P *in){\n if (n < 3)return true;\n\n rep(i,n){\n rep(k,n){\n if (k == i || k == (i+1)%n)continue;\n if (isp(in[i],in[(i+1)%n],in[k])){\n\treturn false;\n }\n }\n }\n\n //is convex\n /*\n if (cross(in[n-2]-in[n-3],in[n-1]-in[n-3]) <0)return false;\n if (cross(in[n-1]-in[n-2],in[0]-in[n-2]) <0)return false;\n if (cross(in[0]-in[n-1],in[1]-in[n-1]) <0)return false;\n */\n rep(i,n){\n P cur=in[(i+1)%n]-in[i],next=in[(i+2)%n]-in[i];\n int tmp = cross(cur,next);\n if (tmp < 0)return false;\n }\n\n return true;\n}\n\nbool isconvex(int n,P*in){\n rep(i,n){\n P cur=in[(i+1)%n]-in[i],next=in[(i+2)%n]-in[i];\n int tmp = cross(cur,next);\n if (tmp < 0)return false;\n }\n return true;\n}\n\nvector<P> make_cand(int r){\n vector<P> ret;\n for(int x=-r;x<=r;x++){\n double y = sqrt(r*r - x*x);\n if (abs(floor(y)-y)<eps){\n if (y>0.5){\n\tret.pb(P(x,(int)y));\n\tret.pb(P(x,(int)-y));\n }else ret.pb(P(x,(int)y));\n }\n }\n return ret;\n}\n\nint ans;\nP ori(0,0);\nvoid search(int n,int now,int pnow,int *len,P *in,vector<P>*cand,\n\t double total){\n if (n >=3 && !isconvex(now,in))return;\n if (dist(in[now-1])-total > eps){\n return; \n }\n \n if (abs(dist(in[now-1])-total)<eps){\n int tmp = PolygonArea(in,now);\n if (isok(n,in))ans=max(ans,tmp);//,cout << tmp<<endl;\n return; \n }\n \n\n if (n == pnow+1){\n int index=len[pnow];\n if (abs(dist(in[now-1])-index)<eps ){\n int tmp = PolygonArea(in,now);\n if (isok(n,in))ans=max(ans,tmp);//,cout << tmp<<endl;\n }\n return;\n }\n\n rep(i,cand[pnow].size()){\n in[now].real()=in[now-1].real()+cand[pnow][i].real();\n in[now].imag()=in[now-1].imag()+cand[pnow][i].imag();\n \n if (in[now].imag()<0 && in[now].real()>=0)continue;\n search(n,now+1,pnow+1,len,in,cand,total-len[pnow]);\n }\n}\n\nmain(){\n int n;\n while(cin>>n && n){\n int r[n];\n int num[100];\n int len[100];\n int p=0;\n double total=0;\n P in[10];\n vector<P> cand[10];\n \n ans = -1;\n \n rep(i,n){\n cin>>r[i];\n total+=r[i];\n }\n sort(r,r+n);\n in[0]=ori;\n\n\n rep(i,n){\n cand[i].clear();\n cand[i]=make_cand(r[i]);\n }\n\n /* \n int findmax=0;\n rep(i,n){\n if (cand[i].size() > cand[findmax].size())findmax=i;\n else if (cand[i].size()==cand[findmax].size() &&\n\t r[i] > r[findmax])findmax=i;\n }\n \n swap(r[0],r[findmax]);\n sort(r+1,r+n);\n */ \n \n rep(i,cand[0].size()){\n if (cand[0][i].imag() >=0 && cand[0][i].real()>0);\n else {\n\tcand[0].erase(cand[0].begin()+i);\n\ti--;\n }\n }\n\n set<vector<int> >S;\n do{\n \n vector<int> tmp;\n REP(i,1,n){\n\ttmp.pb(r[i]);\n }\n if (S.find(tmp) != S.end()){\n\tcontinue;\n } \n reverse(ALL(tmp));\n S.insert(tmp);\n \n\n REP(i,1,n){\n\tcand[i].clear();\n\tcand[i]=make_cand(r[i]);\n }\n // rep(i,n)cout << r[i] <<\" \";cout << endl;\n search(n,1,0,r,in,cand,total);\n }while(next_permutation(r+1,r+n));\n\n cout << ans << endl;\n }\n}", "accuracy": 1, "time_ms": 5580, "memory_kb": 924, "score_of_the_acc": -0.2148, "final_rank": 4 }, { "submission_id": "aoj_1272_99692", "code_snippet": "#include<iostream>\n#include<set>\n#include<algorithm>\n#include<cmath>\n#include<complex>\n#include<vector>\nusing namespace std;\n#define REP(i,b,n) for(int i=b;i<n;i++)\n#define rep(i,n) REP(i,0,n)\n#define ALL(C) (C).begin(),(C).end()\n#define pb push_back\n\ntypedef complex<int> P;\n\nconst double eps = 1e-10;\n\nint cross(P a,P b){\n return (a.real()*b.imag()-a.imag()*b.real());\n}\n\ndouble dist(P a){\n return sqrt(a.real()*a.real()+a.imag()*a.imag());\n}\n\ndouble isp(P a,P b,P c){\n return dist(a-c)+dist(b-c)< dist(a-b)+eps;\n}\n\nint PolygonArea(P *a,int n){\n int sum = 0;\n rep(i,n){\n sum+=(a[i].real()-a[(i+1)%n].real())*(a[i%n].imag()+a[(i+1)%n].imag());\n }\n\n \n /*\n cout << \"[\";\n rep(i,n){\n cout << a[i] <<','<< endl;\n }\n cout << a[0] << \",]\"<<endl;\n cout << endl;\n */\n\n if (sum < 0)sum*=-1;\n if (sum%2 != 0)return -1;\n else return sum/2;\n}\n\nbool isok(int n,P *in){\n if (n < 3)return true;\n\n rep(i,n){\n rep(k,n){\n if (k == i || k == (i+1)%n)continue;\n if (isp(in[i],in[(i+1)%n],in[k])){\n\treturn false;\n }\n }\n }\n\n //is convex\n //int me = cross(in[1]-in[0],in[2]-in[0]);\n\n \n if (cross(in[n-2]-in[n-3],in[n-1]-in[n-3]) <0)return false;\n if (cross(in[n-1]-in[n-2],in[0]-in[n-2]) <0)return false;\n if (cross(in[0]-in[n-1],in[1]-in[n-1]) <0)return false;\n\n return true;\n}\n\nbool isconvex(int n,P*in){\n rep(i,n){\n P cur=in[(i+1)%n]-in[i],next=in[(i+2)%n]-in[i];\n int tmp = cross(cur,next);\n if (tmp < 0)return false;\n //if (me*tmp < -eps)return false;\n }\n return true;\n}\n\n\nvector<P> make_cand(int r){\n vector<P> ret;\n for(int x=-r;x<=r;x++){\n double y = sqrt(r*r - x*x);\n if (abs(floor(y)-y)<eps){\n if (y>0.5){\n\tret.pb(P(x,(int)y));\n\tret.pb(P(x,(int)-y));\n }else ret.pb(P(x,(int)y));\n }\n }\n return ret;\n}\n\nint ans;\nP ori(0,0);\nvoid search(int n,int now,int pnow,int *len,P *in,vector<P>*cand,\n\t double total){\n\n if (n >=3 && !isconvex(now,in))return;\n //if (!isok(now,in))return;\n\n if (dist(in[now-1])-total > eps){\n return; \n }\n \n if (abs(dist(in[now-1])-total)<eps){\n int tmp = PolygonArea(in,now);\n if (isok(n,in))ans=max(ans,tmp);//,cout << tmp<<endl;\n return; \n }\n \n\n if (n == pnow+1){\n int index=len[pnow];\n if (abs(dist(in[now-1])-index)<eps ){\n int tmp = PolygonArea(in,now);\n if (isok(n,in))ans=max(ans,tmp);//,cout << tmp<<endl;\n }\n return;\n }\n\n rep(i,cand[pnow].size()){\n in[now].real()=in[now-1].real()+cand[pnow][i].real();\n in[now].imag()=in[now-1].imag()+cand[pnow][i].imag();\n \n if (in[now].imag()<0 && in[now].real()>=0)continue;\n search(n,now+1,pnow+1,len,in,cand,total-len[pnow]);\n }\n}\n\nmain(){\n int n;\n while(cin>>n && n){\n int r[n];\n int num[100];\n int len[100];\n int p=0;\n double total=0;\n P in[10];\n vector<P> cand[10];\n \n ans = -1;\n \n rep(i,n){\n cin>>r[i];\n total+=r[i];\n }\n sort(r,r+n);\n in[0]=ori;\n\n\n rep(i,n){\n cand[i].clear();\n cand[i]=make_cand(r[i]);\n }\n\n /* \n int findmax=0;\n rep(i,n){\n if (cand[i].size() > cand[findmax].size())findmax=i;\n else if (cand[i].size()==cand[findmax].size() &&\n\t r[i] > r[findmax])findmax=i;\n }\n \n swap(r[0],r[findmax]);\n sort(r+1,r+n);\n */ \n \n rep(i,cand[0].size()){\n if (cand[0][i].imag() >=0 && cand[0][i].real()>0);\n else {\n\tcand[0].erase(cand[0].begin()+i);\n\ti--;\n }\n }\n\n set<vector<int> >S;\n do{\n \n vector<int> tmp;\n REP(i,1,n){\n\ttmp.pb(r[i]);\n }\n if (S.find(tmp) != S.end()){\n\tcontinue;\n } \n reverse(ALL(tmp));\n S.insert(tmp);\n \n\n REP(i,1,n){\n\tcand[i].clear();\n\tcand[i]=make_cand(r[i]);\n }\n // rep(i,n)cout << r[i] <<\" \";cout << endl;\n search(n,1,0,r,in,cand,total);\n }while(next_permutation(r+1,r+n));\n\n cout << ans << endl;\n }\n}", "accuracy": 1, "time_ms": 5550, "memory_kb": 924, "score_of_the_acc": -0.2043, "final_rank": 1 } ]
aoj_1275_cpp
Problem A: And Then There Was One Let’s play a stone removing game. Initially, n stones are arranged on a circle and numbered 1, ... , n clockwise (Figure 1). You are also given two numbers k and m . From this state, remove stones one by one following the rules explained below, until only one remains. In step 1, remove stone m . In step 2, locate the k -th next stone clockwise from m and remove it. In subsequent steps, start from the slot of the stone removed in the last step, make k hops clockwise on the remaining stones and remove the one you reach. In other words, skip ( k - 1) remaining stones clockwise and remove the next one. Repeat this until only one stone is left and answer its number. For example, the answer for the case n = 8, k = 5, m = 3 is 1, as shown in Figure 1. Figure 1: An example game Initial state : Eight stones are arranged on a circle. Step 1 : Stone 3 is removed since m = 3. Step 2 : You start from the slot that was occupied by stone 3. You skip four stones 4, 5, 6 and 7 (since k = 5), and remove the next one, which is 8. Step 3 : You skip stones 1, 2, 4 and 5, and thus remove 6. Note that you only count stones that are still on the circle and ignore those already removed. Stone 3 is ignored in this case. Steps 4-7 : You continue until only one stone is left. Notice that in later steps when only a few stones remain, the same stone may be skipped multiple times. For example, stones 1 and 4 are skipped twice in step 7. Final State : Finally, only one stone, 1, is on the circle. This is the final state, so the answer is 1. Input The input consists of multiple datasets each of which is formatted as follows. n k m The last dataset is followed by a line containing three zeros. Numbers in a line are separated by a single space. A dataset satisfies the following conditions. 2 ≤ n ≤ 10000, 1 ≤ k ≤ 10000, 1 ≤ m ≤ n The number of datasets is less than 100. Output For each dataset, output a line containing the stone number left in the final state. No extra characters such as spaces should appear in the output. Sample Input 8 5 3 100 9999 98 10000 10000 10000 0 0 0 Output for the Sample Input 1 93 2019
[ { "submission_id": "aoj_1275_10895099", "code_snippet": "#include<bits/stdc++.h>\n// #include<atcoder/all>\n// #include<boost/multiprecision/cpp_int.hpp>\n\nusing namespace std;\n// using namespace atcoder;\n// using bint = boost::multiprecision::cpp_int;\nusing ll = long long;\nusing ull = unsigned long long;\nusing ld = long double;\nusing pii = pair<int,int>;\nusing pll = pair<ll,ll>;\nusing vi = vector<ll>;\nusing vvi = vector<vi>;\nusing vvvi = vector<vvi>;\nusing ve = vector<vector<int>>;\nusing vb = vector<bool>;\nusing vvb = vector<vb>;\n#define rep(i,s,n) for(ll i = (ll)s;i < (ll)n;i++)\n#define rrep(i,l,r) for(ll i = r-1;i >= l;i--)\n#define ALL(x) (x).begin(),(x).end()\n#define sz(c) ((ll)(c).size())\n#define LB(A,x) (int)(lower_bound(A.begin(),A.end(),x)-A.begin())\n#define UB(A,x) (int)(upper_bound(A.begin(),A.end(),x)-A.begin())\n// #define MOD 1000000007\n#define MOD 998244353\ntemplate<typename T>using min_priority_queue=priority_queue<T,vector<T>,greater<T>>;\ntemplate<typename T>ostream&operator<<(ostream&os,vector<T>&v){for(int i = 0;i < v.size();i++)os<<v[i]<<(i+1!=v.size()?\" \":\"\");return os;}\ntemplate<typename T>istream&operator>>(istream&is,vector<T>&v){for(T&in:v)is>>in;return is;}\ntemplate<typename T1,typename T2>ostream&operator<<(ostream&os,pair<T1,T2>&p){os<<p.first<<\" \"<<p.second;return os;}\ntemplate<typename T1,typename T2>istream&operator>>(istream&is,pair<T1,T2>&p){is>>p.first>>p.second;return is;}\ntemplate<typename T> inline bool chmax(T &a,T b){if(a < b){a = b;return true;}return false;}\ntemplate<typename T> inline bool chmin(T &a,T b){if(a > b){a = b;return true;}return false;}\nld dist(ld x1,ld y1,ld x2, ld y2){return sqrtl((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2));}\n\n#include <ext/pb_ds/assoc_container.hpp>\nusing namespace __gnu_pbds;\ntemplate<typename T>\nusing ordered_set = tree<T, null_type, std::less<T>, rb_tree_tag, tree_order_statistics_node_update>;\n\nint main(){\n \n ios_base::sync_with_stdio(0), cin.tie(0);\n while(1){\n int n,K,m;cin >> n >> K >> m;\n if(!n)break;\n ordered_set<int> st;\n rep(i,0,n)st.insert(i+1);\n m--;\n int pre = m,C = n;\n rep(i,0,n-1){\n st.erase(*st.find_by_order(pre));C--;\n pre = (pre + K-1)%C;\n }\n cout << *st.begin() << \"\\n\";\n }\n \n\n return 0;\n}", "accuracy": 1, "time_ms": 120, "memory_kb": 3840, "score_of_the_acc": -0.365, "final_rank": 14 }, { "submission_id": "aoj_1275_10848642", "code_snippet": "#include <iostream>\n#include <vector>\n\nusing namespace std;\n\nint main()\n{\n int n, k, m, i, ind, s, j, ans;\n vector<int>data;\n while(cin >> n >> k >> m){\n if(n == 0 && k == 0 && m == 0)break;\n for(i = 1; i <= n; i++)data.push_back(i);\n ind = m - 1;\n if(k == 1){\n if(m == 1)ans = n;\n else{\n ans = m-1;\n }\n }\n else{\n while(n > 1){\n data.erase(data.begin()+ind);\n n--;\n for(j = 1; j < k; j++){\n ind++;\n if(ind >= n)ind = ind - n;\n }\n }\n ans = data.at(0);\n }\n cout << ans << endl;\n data.erase(data.begin(), data.end());\n }\n return 0;\n}", "accuracy": 1, "time_ms": 1810, "memory_kb": 3236, "score_of_the_acc": -0.874, "final_rank": 18 }, { "submission_id": "aoj_1275_10499541", "code_snippet": "#include <iostream>\n#include <iomanip>\n#include <cassert>\n#include <vector>\n#include <algorithm>\n#include <utility>\n#include <numeric>\n#include <tuple>\n#include <ranges>\nnamespace ranges = std::ranges;\nnamespace views = std::views;\n// #include \"Src/Number/IntegerDivision.hpp\"\n// #include \"Src/Utility/BinarySearch.hpp\"\n// #include \"Src/Sequence/CompressedSequence.hpp\"\n// #include \"Src/Sequence/RunLengthEncoding.hpp\"\n// #include \"Src/Algebra/Group/AdditiveGroup.hpp\"\n// #include \"Src/DataStructure/FenwickTree/FenwickTree.hpp\"\n// #include \"Src/DataStructure/SegmentTree/SegmentTree.hpp\"\n// using namespace zawa;\n// #include \"atcoder/modint\"\n// using mint = atcoder::modint998244353;\nbool solve() {\n int N, K, M;\n std::cin >> N >> K >> M;\n if (N == 0 and K == 0 and M == 0) return false;\n K--;\n std::vector<int> ans(N);\n std::iota(ans.begin(), ans.end(), 1);\n int cur = M - 1;\n while (std::ssize(ans) > 1) {\n assert(cur < std::ssize(ans)); \n ans.erase(ans.begin() + cur);\n if (cur == std::ssize(ans)) cur = 0;\n cur += K % ans.size();\n cur %= ans.size();\n }\n std::cout << ans[0] << '\\n';\n return true;\n}\nint main() {\n std::cin.tie(nullptr);\n std::cout.tie(nullptr);\n std::ios::sync_with_stdio(false);\n while (solve()) ;\n}", "accuracy": 1, "time_ms": 50, "memory_kb": 3584, "score_of_the_acc": -0.2098, "final_rank": 10 }, { "submission_id": "aoj_1275_10236552", "code_snippet": "#include <vector>\n#include <iostream>\n#include <algorithm>\nusing namespace std;\n\nclass fenwick_tree {\nprivate:\n\tint n;\n\tvector<int> v;\npublic:\n\tfenwick_tree(int n_) : n(n_), v(n_ + 1, 0) {}\n\tvoid add(int pos, int x) {\n\t\tfor (int i = pos + 1; i <= n; i += i & (-i)) {\n\t\t\tv[i] += x;\n\t\t}\n\t}\n\tint sum(int r) {\n\t\tint res = 0;\n\t\tfor (int i = r; i >= 1; i -= i & (-i)) {\n\t\t\tres += v[i];\n\t\t}\n\t\treturn res;\n\t}\n\tint lower_bound(int s) {\n\t\tif (s <= 0) {\n\t\t\treturn 0;\n\t\t}\n\t\tint bits = 32 - __builtin_clz(n);\n\t\tint res = 0;\n\t\tfor (int i = bits - 1; i >= 0; i--) {\n\t\t\tif (res + (1 << i) <= n && v[res + (1 << i)] < s) {\n\t\t\t\ts -= v[res + (1 << i)];\n\t\t\t\tres += 1 << i;\n\t\t\t}\n\t\t}\n\t\treturn res + 1;\n\t}\n};\n\nint main() {\n\twhile (true) {\n\t\t// step #1. read input\n\t\tint N, K, M;\n\t\tcin >> N >> K >> M;\n\t\tif (N == 0 && K == 0 && M == 0) {\n\t\t\tbreak;\n\t\t}\n\t\tM--;\n\n\t\t// step #2. setup\n\t\tfenwick_tree bit(N);\n\t\tfor (int i = 0; i < N; i++) {\n\t\t\tbit.add(i, 1);\n\t\t}\n\t\tint rem = N;\n\t\tauto find_next = [&](int x, int y) -> int {\n\t\t\tint s = bit.sum(x);\n\t\t\tif (y <= rem - s) {\n\t\t\t\treturn bit.lower_bound(s + y) - 1;\n\t\t\t}\n\t\t\ty -= rem - s;\n\t\t\ty = (y + rem - 1) % rem + 1;\n\t\t\treturn bit.lower_bound(y) - 1;\n\t\t};\n\n\t\t// step #3. simulation\n\t\tint cur = M;\n\t\tbit.add(M, -1);\n\t\trem--;\n\t\tfor (int i = 0; i < N - 2; i++) {\n\t\t\tcur = find_next(cur, K);\n\t\t\tbit.add(cur, -1);\n\t\t\trem--;\n\t\t}\n\n\t\t// step #4. find & output the answer\n\t\tint ans = find_next(cur, 1);\n\t\tcout << ans + 1 << '\\n';\n\t}\n\n\treturn 0;\n}", "accuracy": 1, "time_ms": 40, "memory_kb": 3412, "score_of_the_acc": -0.123, "final_rank": 3 }, { "submission_id": "aoj_1275_10024605", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\n#define all(a) a.begin(),a.end()\n#define reps(i, a, n) for (int i = (a); i < (int)(n); i++)\n#define rep(i, n) reps(i, 0, n)\n#define rreps(i, a, n) for (int i = (a); i > (int)(n); i--)\nconst long long mod = 1000000007;\nconst long long INF = 1e18;\nll myceil(ll a, ll b) {return (a+b-1)/b;}\n\nbool solve() {\n int n, k, m;\n cin >> n >> k >> m;\n if (n == 0) return false;\n m--;\n vector<int> v(n);\n iota(all(v), 1);\n while (v.size() > 1) {\n v.erase(v.begin() + m);\n m = (m+k-1)%v.size();\n }\n cout << v[0] << endl;\n return true;\n}\n\nint main() {\n ios::sync_with_stdio(false);\n std::cin.tie(nullptr);\n while (solve());\n}", "accuracy": 1, "time_ms": 70, "memory_kb": 3404, "score_of_the_acc": -0.1334, "final_rank": 5 }, { "submission_id": "aoj_1275_10022310", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\n#define all(a) a.begin(),a.end()\n#define reps(i, a, n) for (int i = (a); i < (int)(n); i++)\n#define rep(i, n) reps(i, 0, n)\n#define rreps(i, a, n) for (int i = (a); i > (int)(n); i--)\nconst long long mod = 1000000007;\nconst long long INF = 1e18;\nll myceil(ll a, ll b) {return (a+b-1)/b;}\n\nbool solve() {\n int n, k, m;\n cin >> n >> k >> m;\n if (n == 0) return false;\n m--;\n vector<int> v(n);\n iota(all(v), 1);\n while (v.size() > 1) {\n v.erase(v.begin() + m);\n m = (m+k-1)%v.size();\n }\n cout << v[0] << endl;\n return true;\n}\n\nint main() {\n ios::sync_with_stdio(false);\n std::cin.tie(nullptr);\n while (solve());\n}", "accuracy": 1, "time_ms": 70, "memory_kb": 3500, "score_of_the_acc": -0.1792, "final_rank": 8 }, { "submission_id": "aoj_1275_9584165", "code_snippet": "#include <bits/stdc++.h>\n\nusing namespace std;\n\n#define rep(i, a, b) for (int i = (int)(a); i < (int)(b); i++)\n#define rrep(i, a, b) for (int i = (int)(b)-1; i >= (int)(a); i--)\n#define ALL(v) (v).begin(), (v).end()\n#define UNIQUE(v) sort(ALL(v)), (v).erase(unique(ALL(v)), (v).end())\n#define SZ(v) (int)v.size()\n#define MIN(v) *min_element(ALL(v))\n#define MAX(v) *max_element(ALL(v))\n#define LB(v, x) int(lower_bound(ALL(v), (x)) - (v).begin())\n#define UB(v, x) int(upper_bound(ALL(v), (x)) - (v).begin())\n\nusing uint = unsigned int;\nusing ll = long long int;\nusing ull = unsigned long long;\nusing i128 = __int128_t;\nusing u128 = __uint128_t;\nconst int inf = 0x3fffffff;\nconst ll INF = 0x1fffffffffffffff;\n\ntemplate <typename T> inline bool chmax(T &a, T b) {\n if (a < b) {\n a = b;\n return 1;\n }\n return 0;\n}\ntemplate <typename T> inline bool chmin(T &a, T b) {\n if (a > b) {\n a = b;\n return 1;\n }\n return 0;\n}\ntemplate <typename T, typename U> T ceil(T x, U y) {\n assert(y != 0);\n if (y < 0)\n x = -x, y = -y;\n return (x > 0 ? (x + y - 1) / y : x / y);\n}\ntemplate <typename T, typename U> T floor(T x, U y) {\n assert(y != 0);\n if (y < 0)\n x = -x, y = -y;\n return (x > 0 ? x / y : (x - y + 1) / y);\n}\ntemplate <typename T> int popcnt(T x) {\n return __builtin_popcountll(x);\n}\ntemplate <typename T> int topbit(T x) {\n return (x == 0 ? -1 : 63 - __builtin_clzll(x));\n}\ntemplate <typename T> int lowbit(T x) {\n return (x == 0 ? -1 : __builtin_ctzll(x));\n}\n\ntemplate <class T, class U>\nostream &operator<<(ostream &os, const pair<T, U> &p) {\n os << \"P(\" << p.first << \", \" << p.second << \")\";\n return os;\n}\ntemplate <typename T> ostream &operator<<(ostream &os, const vector<T> &vec) {\n os << \"{\";\n for (int i = 0; i < vec.size(); i++) {\n os << vec[i] << (i + 1 == vec.size() ? \"\" : \", \");\n }\n os << \"}\";\n return os;\n}\ntemplate <typename T, typename U>\nostream &operator<<(ostream &os, const map<T, U> &map_var) {\n os << \"{\";\n for (auto itr = map_var.begin(); itr != map_var.end(); itr++) {\n os << \"(\" << itr->first << \", \" << itr->second << \")\";\n itr++;\n if (itr != map_var.end())\n os << \", \";\n itr--;\n }\n os << \"}\";\n return os;\n}\ntemplate <typename T> ostream &operator<<(ostream &os, const set<T> &set_var) {\n os << \"{\";\n for (auto itr = set_var.begin(); itr != set_var.end(); itr++) {\n os << *itr;\n ++itr;\n if (itr != set_var.end())\n os << \", \";\n itr--;\n }\n os << \"}\";\n return os;\n}\n#ifdef LOCAL\n#define show(...) _show(0, #__VA_ARGS__, __VA_ARGS__)\n#else\n#define show(...) true\n#endif\ntemplate <typename T> void _show(int i, T name) {\n cerr << '\\n';\n}\ntemplate <typename T1, typename T2, typename... T3>\nvoid _show(int i, const T1 &a, const T2 &b, const T3 &...c) {\n for (; a[i] != ',' && a[i] != '\\0'; i++)\n cerr << a[i];\n cerr << \":\" << b << \" \";\n _show(i + 1, a, c...);\n}\n\n#include <unistd.h>\n\nnamespace fastio {\nstatic constexpr uint32_t SZ = 1 << 17;\nchar ibuf[SZ];\nchar obuf[SZ];\nchar out[100];\n// pointer of ibuf, obuf\n\n\nuint32_t pil = 0, pir = 0, por = 0;\n\nstruct Pre {\n char num[10000][4];\n constexpr Pre() : num() {\n for (int i = 0; i < 10000; i++) {\n int n = i;\n for (int j = 3; j >= 0; j--) {\n num[i][j] = n % 10 | '0';\n n /= 10;\n }\n }\n }\n} constexpr pre;\n\ninline void load() {\n memmove(ibuf, ibuf + pil, pir - pil);\n pir = pir - pil + fread(ibuf + pir - pil, 1, SZ - pir + pil, stdin);\n pil = 0;\n if (pir < SZ)\n ibuf[pir++] = '\\n';\n}\n\ninline void flush() {\n fwrite(obuf, 1, por, stdout);\n por = 0;\n}\n\nvoid rd(char &c) {\n do {\n if (pil + 1 > pir)\n load();\n c = ibuf[pil++];\n } while (isspace(c));\n}\n\nvoid rd(string &x) {\n x.clear();\n char c;\n do {\n if (pil + 1 > pir)\n load();\n c = ibuf[pil++];\n } while (isspace(c));\n do {\n x += c;\n if (pil == pir)\n load();\n c = ibuf[pil++];\n } while (!isspace(c));\n}\n\ntemplate <typename T> void rd_real(T &x) {\n string s;\n rd(s);\n x = stod(s);\n}\n\ntemplate <typename T> void rd_integer(T &x) {\n if (pil + 100 > pir)\n load();\n char c;\n do\n c = ibuf[pil++];\n while (c < '-');\n bool minus = 0;\n if constexpr (is_signed<T>::value || is_same_v<T, i128>) {\n if (c == '-') {\n minus = 1, c = ibuf[pil++];\n }\n }\n x = 0;\n while ('0' <= c) {\n x = x * 10 + (c & 15), c = ibuf[pil++];\n }\n if constexpr (is_signed<T>::value || is_same_v<T, i128>) {\n if (minus)\n x = -x;\n }\n}\n\nvoid rd(int &x) {\n rd_integer(x);\n}\nvoid rd(ll &x) {\n rd_integer(x);\n}\nvoid rd(i128 &x) {\n rd_integer(x);\n}\nvoid rd(uint &x) {\n rd_integer(x);\n}\nvoid rd(ull &x) {\n rd_integer(x);\n}\nvoid rd(u128 &x) {\n rd_integer(x);\n}\nvoid rd(double &x) {\n rd_real(x);\n}\nvoid rd(long double &x) {\n rd_real(x);\n}\n\ntemplate <class T, class U> void rd(pair<T, U> &p) {\n return rd(p.first), rd(p.second);\n}\ntemplate <size_t N = 0, typename T> void rd_tuple(T &t) {\n if constexpr (N < std::tuple_size<T>::value) {\n auto &x = std::get<N>(t);\n rd(x);\n rd_tuple<N + 1>(t);\n }\n}\ntemplate <class... T> void rd(tuple<T...> &tpl) {\n rd_tuple(tpl);\n}\n\ntemplate <size_t N = 0, typename T> void rd(array<T, N> &x) {\n for (auto &d : x)\n rd(d);\n}\ntemplate <class T> void rd(vector<T> &x) {\n for (auto &d : x)\n rd(d);\n}\n\nvoid read() {}\ntemplate <class H, class... T> void read(H &h, T &...t) {\n rd(h), read(t...);\n}\n\nvoid wt(const char c) {\n if (por == SZ)\n flush();\n obuf[por++] = c;\n}\nvoid wt(const string s) {\n for (char c : s)\n wt(c);\n}\nvoid wt(const char *s) {\n size_t len = strlen(s);\n for (size_t i = 0; i < len; i++)\n wt(s[i]);\n}\n\ntemplate <typename T> void wt_integer(T x) {\n if (por > SZ - 100)\n flush();\n if (x < 0) {\n obuf[por++] = '-', x = -x;\n }\n int outi;\n for (outi = 96; x >= 10000; outi -= 4) {\n memcpy(out + outi, pre.num[x % 10000], 4);\n x /= 10000;\n }\n if (x >= 1000) {\n memcpy(obuf + por, pre.num[x], 4);\n por += 4;\n } else if (x >= 100) {\n memcpy(obuf + por, pre.num[x] + 1, 3);\n por += 3;\n } else if (x >= 10) {\n int q = (x * 103) >> 10;\n obuf[por] = q | '0';\n obuf[por + 1] = (x - q * 10) | '0';\n por += 2;\n } else\n obuf[por++] = x | '0';\n memcpy(obuf + por, out + outi + 4, 96 - outi);\n por += 96 - outi;\n}\n\ntemplate <typename T> void wt_real(T x) {\n ostringstream oss;\n oss << fixed << setprecision(15) << double(x);\n string s = oss.str();\n wt(s);\n}\n\nvoid wt(int x) {\n wt_integer(x);\n}\nvoid wt(ll x) {\n wt_integer(x);\n}\nvoid wt(i128 x) {\n wt_integer(x);\n}\nvoid wt(uint x) {\n wt_integer(x);\n}\nvoid wt(ull x) {\n wt_integer(x);\n}\nvoid wt(u128 x) {\n wt_integer(x);\n}\nvoid wt(double x) {\n wt_real(x);\n}\nvoid wt(long double x) {\n wt_real(x);\n}\n\ntemplate <class T, class U> void wt(const pair<T, U> val) {\n wt(val.first);\n wt(' ');\n wt(val.second);\n}\ntemplate <size_t N = 0, typename T> void wt_tuple(const T t) {\n if constexpr (N < std::tuple_size<T>::value) {\n if constexpr (N > 0) {\n wt(' ');\n }\n const auto x = std::get<N>(t);\n wt(x);\n wt_tuple<N + 1>(t);\n }\n}\ntemplate <class... T> void wt(tuple<T...> tpl) {\n wt_tuple(tpl);\n}\ntemplate <class T, size_t S> void wt(const array<T, S> val) {\n auto n = val.size();\n for (size_t i = 0; i < n; i++) {\n if (i)\n wt(' ');\n wt(val[i]);\n }\n}\ntemplate <class T> void wt(const vector<T> val) {\n auto n = val.size();\n for (size_t i = 0; i < n; i++) {\n if (i)\n wt(' ');\n wt(val[i]);\n }\n}\n\nvoid print() {\n wt('\\n');\n}\ntemplate <class Head, class... Tail> void print(Head &&head, Tail &&...tail) {\n wt(head);\n if (sizeof...(Tail))\n wt(' ');\n print(forward<Tail>(tail)...);\n}\nvoid __attribute__((destructor)) _d() {\n flush();\n}\n} // namespace fastio\n\n\nusing fastio::flush;\nusing fastio::print;\nusing fastio::read;\n\ninline void first(bool i = true) {\n print(i ? \"first\" : \"second\");\n}\ninline void Alice(bool i = true) {\n print(i ? \"Alice\" : \"Bob\");\n}\ninline void Takahashi(bool i = true) {\n print(i ? \"Takahashi\" : \"Aoki\");\n}\ninline void yes(bool i = true) {\n print(i ? \"yes\" : \"no\");\n}\ninline void Yes(bool i = true) {\n print(i ? \"Yes\" : \"No\");\n}\ninline void No() {\n print(\"No\");\n}\ninline void YES(bool i = true) {\n print(i ? \"YES\" : \"NO\");\n}\ninline void NO() {\n print(\"NO\");\n}\ninline void Yay(bool i = true) {\n print(i ? \"Yay!\" : \":(\");\n}\ninline void Possible(bool i = true) {\n print(i ? \"Possible\" : \"Impossible\");\n}\ninline void POSSIBLE(bool i = true) {\n print(i ? \"POSSIBLE\" : \"IMPOSSIBLE\");\n}\n\n/**\n * @brief Fast IO\n */\n\nint main() {\nwhile(1) {\n int N, K, M;\n cin >> N >> K >> M;\n if (N == 0) return 0;\n M--;\n vector<int> V(N);\n iota(V.begin(), V.end(), 0);\n int Cur = M;\n rep(_,0,N-1) {\n Cur %= (int)V.size();\n V.erase(V.begin()+Cur);\n Cur += K-1;\n }\n cout << V[0]+1 << endl;\n}\n}", "accuracy": 1, "time_ms": 70, "memory_kb": 3536, "score_of_the_acc": -0.1963, "final_rank": 9 }, { "submission_id": "aoj_1275_8993196", "code_snippet": "// (⁠◕⁠ᴗ⁠◕⁠✿⁠)\n\n// #pragma GCC target(\"avx2\")\n// #pragma GCC optimize(\"O3\")\n// #pragma GCC optimize(\"unroll-loops\")\n#include <bits/stdc++.h>\n#define rep(i, n) for (ll i = 0; i < (n); i++)\n#define srep(i, s, n) for (ll i = s; i < (n); i++)\n#define len(x) ((int)(x).size())\n#define all(x) (x).begin(), (x).end()\nusing namespace std;\ntemplate<typename T> using vc = vector<T>;\ntemplate<typename T> using vv = vc<vc<T>>;\ntemplate<typename T> using vvv = vv<vc<T>>;\nusing vi = vc<int>;using vvi = vv<int>; using vvvi = vv<vi>;\nusing ll = long long;using vl = vc<ll>;using vvl = vv<ll>; using vvvl = vv<vl>;\nusing ld = long double; using vld = vc<ld>; using vvld = vc<vld>; using vvvld = vc<vvld>;\nusing uint = unsigned int;\nusing ull = unsigned long long;\nconst ld pi = 3.141592653589793;\nconst int inf = 0x3f3f3f3f;\nconst ll INF = 0x3f3f3f3f3f3f3f3f;\n// const ll mod = 1000000007;\nconst ll mod = 998244353;\ninline bool inside(ll y, ll x, ll H, ll W) {return 0 <= (y) and (y) < (H) and 0 <= (x) and (x) < (W); }\n\n#define debug(var) do{std::cout << #var << \" : \\n\";view(var);}while(0)\ntemplate<typename T> void view(T e){cout << e << endl;}\ntemplate<typename T> void view(const vc<T>& v){for(const auto& e : v){ cout << e << \" \"; } cout << endl;}\ntemplate<typename T> void view(const vv<T>& vv){ for(const auto& v : vv){ view(v); } }\n\nint op(int a, int b){return a + b;}\nint e(){return 0;}\n\ntemplate<class T, T (*op)(T, T), T (*e)()> struct segtree{\n public : \n segtree(int n) : _n(n){\n depth = 0;\n while ((1 << depth) < _n) depth++;\n size = 1 << depth;\n tree = vc<T>(2 * size, e());\n }\n\n void set(int p, T x){\n assert (0 <= p && p < _n);\n p += size;\n tree[p] = x;\n for (int i = 0; i < depth; i++){\n p >>= 1;\n tree[p] = op(tree[2 * p], tree[2 * p + 1]);\n }\n return;\n }\n\n T get(int p){\n assert(0 <= p && p < _n);\n return tree[p + size];\n }\n\n T prod(int l, int r){\n assert (0 <= l && l <= r && r <= _n);\n if (l == r) return e();\n T nl = e(), nr = e();\n l += size;\n r += size;\n while(l < r){\n if (l & 1) nl = op(nl, tree[l++]);\n if (r & 1) nr = op(nr, tree[--r]);\n l >>= 1;\n r >>= 1;\n }\n return op(nl, nr);\n }\n \n private : \n int _n, depth, size;\n vc<T> tree;\n};\n\nint solve(int N, int K, int M){\n M--;\n segtree<int, op, e> seg(N);\n rep(i, N) seg.set(i, 1);\n int nw = M;\n seg.set(M, 0);\n rep(i, N - 2){\n int nk = K % seg.prod(0, N);\n if (nk == 0) nk += seg.prod(0, N);\n if (seg.prod(nw, N) >= nk){\n int top = N, bottom = nw;\n while (top - bottom > 1){\n int mid = (top + bottom) / 2;\n if (seg.prod(nw, mid) < nk) bottom = mid;\n else top = mid;\n }\n nw = bottom;\n seg.set(nw, 0);\n }else{\n nk -= seg.prod(nw, N);\n int top = N, bottom = 0;\n while (top - bottom > 1){\n int mid = (top + bottom) / 2;\n if (seg.prod(0, mid) < nk) bottom = mid;\n else top = mid;\n }\n nw = bottom;\n seg.set(nw, 0);\n }\n }\n assert(seg.prod(0, N) == 1);\n rep(i, N) if (seg.get(i) == 1) return i + 1;\n return -1;\n}\n\nint main(){\n vi ans;\n while (true){\n int N, K, M; cin >> N >> K >> M;\n if (N == 0) break;\n ans.push_back(solve(N, K, M));\n }\n for (auto x : ans) cout << x << endl;\n}", "accuracy": 1, "time_ms": 270, "memory_kb": 3568, "score_of_the_acc": -0.306, "final_rank": 12 }, { "submission_id": "aoj_1275_8387353", "code_snippet": "#include <iostream>\n#include <vector>\nusing namespace std;\n#define ALL(v) begin(v),end(v)\n#define debug(val); cerr << #val << \":\" << val << endl;\n\nclass BIT {\npublic:\n BIT(int sz) \n : size(sz)\n , bit(sz + 1, 0)\n {}\n \n void add(int a, int w) {\n for (int x = a; x <= size; x += x & -x) bit[x] += w;\n }\n \n int sum(int a) {\n int ret = 0;\n for (int x = a; x > 0; x -= x & -x) ret += bit[x];\n return ret;\n }\n \n int sum(int l, int r) {\n return sum(r) - sum(l - 1);\n }\nprivate:\n vector<int> bit;\n int size;\n};\n\nint N,K,M;\n\nint main(void) {\n while (true) {\n cin >> N >> K >> M;\n if (N == 0 && K == 0 && M == 0) break;\n \n BIT bit(N);\n for (int i = 1; i <= N; ++i) {\n bit.add(i, 1);\n }\n \n int pos = M;\n vector<bool> alive(N + 1, true);\n \n for (int i = 1; i < N; ++i) {\n // cerr << \"pos: \" << pos << endl;\n bit.add(pos, -1);\n alive[pos] = false;\n \n int all = N - i;\n int front = bit.sum(1, pos);\n int index = (front + K - 1 + all) % all + 1;\n int cnt = 0;\n for (int i = 1; i <= N; ++i) {\n cnt += alive[i];\n if (cnt == index) {\n pos = i;\n break;\n }\n }\n }\n cout << pos << endl;\n }\n}", "accuracy": 1, "time_ms": 2140, "memory_kb": 3372, "score_of_the_acc": -1.0945, "final_rank": 19 }, { "submission_id": "aoj_1275_8387346", "code_snippet": "#include <iostream>\n#include <vector>\nusing namespace std;\n#define ALL(v) begin(v),end(v)\n#define debug(val); cerr << #val << \":\" << val << endl;\n\nclass BIT {\npublic:\n BIT(int sz) \n : size(sz)\n , bit(sz + 1, 0)\n {}\n \n void add(int a, int w) {\n for (int x = a; x <= size; x += x & -x) bit[x] += w;\n }\n \n int sum(int a) {\n int ret = 0;\n for (int x = a; x > 0; x -= x & -x) ret += bit[x];\n return ret;\n }\n \n int sum(int l, int r) {\n return sum(r) - sum(l - 1);\n }\nprivate:\n vector<int> bit;\n int size;\n};\n\nint N,K,M;\n\nint main(void) {\n BIT* bit;\n vector<bool>* alive;\n \n while (true) {\n cin >> N >> K >> M;\n if (N == 0 && K == 0 && M == 0) break;\n \n bit = new BIT(N);\n for (int i = 1; i <= N; ++i) {\n bit->add(i, 1);\n }\n \n int pos = M;\n alive = new vector<bool>(N + 1, true);\n \n for (int i = 1; i < N; ++i) {\n // cerr << \"pos: \" << pos << endl;\n bit->add(pos, -1);\n (*alive)[pos] = false;\n \n int all = N - i;\n int front = bit->sum(1, pos);\n int index = (front + K - 1 + all) % all + 1;\n int cnt = 0;\n for (int i = 1; i <= N; ++i) {\n cnt += (*alive)[i];\n if (cnt == index) {\n pos = i;\n break;\n }\n }\n }\n cout << pos << endl;\n }\n}", "accuracy": 1, "time_ms": 2150, "memory_kb": 5260, "score_of_the_acc": -2, "final_rank": 20 }, { "submission_id": "aoj_1275_7256535", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\nint main(){\n while(true){\n int n, k, m; cin >> n >> k >> m;\n if(n == 0 && k == 0 && m == 0) break;\n\n vector<int> stones(n);\n for(int i = 0; i < n; i++) stones[i] = i+1;\n m--;\n while(stones.size() > 1){\n stones.erase(stones.begin()+m, stones.begin()+m+1);\n m = (m+k-1)%stones.size();\n }\n cout << stones[0] << '\\n';\n }\n return 0;\n}", "accuracy": 1, "time_ms": 70, "memory_kb": 3400, "score_of_the_acc": -0.1315, "final_rank": 4 }, { "submission_id": "aoj_1275_7195322", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n#define rep(i, n) for (int i = 0; i < (int)(n); i++)\n#define repn(i,end) for(int i = 0; i <= (int)(end); i++)\n#define reps(i,start,end) for(int i = start; i < (int)(end); i++)\n#define repsn(i,start,end) for(int i = start; i <= (int)(end); i++)\n#define ll long long\n#define print(t) cout << t << endl \n#define all(a) (a).begin(),(a).end()\n// << std::fixed << std::setprecision(0)\nconst ll INF = 1LL << 60;\n \ntemplate<class T> void chmin(T& a, T b){\n if(a > b){\n a = b;\n }\n}\n \ntemplate<class T> void chmax(T& a, T b){\n if(a < b){\n a = b;\n }\n}\n \nll lpow(ll x,ll n){\n ll ans = 1;\n while(n >0){\n if(n & 1)ans *= x;\n x *= x;\n n >>= 1;\n }\n return ans;\n}\n// for AOJ or ICPC or etc..\ntemplate<class Tp>\nbool zero (const Tp &x) {\n return x == 0;\n}\n\ntemplate<class Tp, class... Args>\nbool zero (const Tp &x, const Args& ...args) {\n return zero(x) and zero(args...);\n}\n\nint main(){\n while(true){\n ll n,k,m;cin >> n >> k>> m;\n if(zero(n,k,m))break;\n vector<ll> a;\n rep(i,n)a.push_back(i);\n if( (m % (ll)a.size()) == 0){\n // cout << \"here\" << endl;\n a.pop_back();\n }else{\n vector<ll> b = {a.begin(),a.begin() + (m % n)-1 };\n a = {a.begin()+(m % n) ,a.end()};\n a.insert(a.end(),all(b));\n }\n while((ll)a.size() !=1LL){\n // for(auto &p:a){\n // cout << p << \" \";\n // }\n // cout << endl;\n if( (k % (ll)a.size()) == 0){\n // cout << \"here\" << endl;\n a.pop_back();\n continue;\n }\n vector<ll> c = {a.begin(),a.begin() + (k % (ll)a.size())-1 };\n // for(auto & p: a){\n // cout << p << \" \";\n // }\n // cout << endl;\n a = {a.begin()+(k % (ll)a.size()),a.end()};\n a.insert(a.end(),all(c));\n }\n cout << a[0] + 1 << endl;\n }\n}", "accuracy": 1, "time_ms": 700, "memory_kb": 3368, "score_of_the_acc": -0.4134, "final_rank": 15 }, { "submission_id": "aoj_1275_6717695", "code_snippet": "#line 2 \"library/KowerKoint/base.hpp\"\n\n#ifdef DEBUG\n#define _GLIBCXX_DEBUG\n#endif\n\n#include <bits/stdc++.h>\nusing namespace std;\n\n#define REP(i, n) for(int i = 0; i < (int)(n); i++)\n#define FOR(i, a, b) for(ll i = a; i < (ll)(b); i++)\n#define ALL(a) (a).begin(),(a).end()\n#define END(...) { print(__VA_ARGS__); return; }\n\nusing VI = vector<int>;\nusing VVI = vector<VI>;\nusing VVVI = vector<VVI>;\nusing ll = long long;\nusing VL = vector<ll>;\nusing VVL = vector<VL>;\nusing VVVL = vector<VVL>;\nusing VD = vector<double>;\nusing VVD = vector<VD>;\nusing VVVD = vector<VVD>;\nusing VS = vector<string>;\nusing VVS = vector<VS>;\nusing VVVS = vector<VVS>;\nusing VC = vector<char>;\nusing VVC = vector<VC>;\nusing VVVC = vector<VVC>;\nusing P = pair<int, int>;\nusing VP = vector<P>;\nusing VVP = vector<VP>;\nusing VVVP = vector<VVP>;\nusing LP = pair<ll, ll>;\nusing VLP = vector<LP>;\nusing VVLP = vector<VLP>;\nusing VVVLP = vector<VVLP>;\n\ntemplate <typename T>\nusing PQ = priority_queue<T>;\ntemplate <typename T>\nusing GPQ = priority_queue<T, vector<T>, greater<T>>;\n\nconstexpr int INF = 1001001001;\nconstexpr ll LINF = 1001001001001001001ll;\nconstexpr int DX[] = {1, 0, -1, 0};\nconstexpr int DY[] = {0, 1, 0, -1};\n\nvoid print() { cout << '\\n'; }\ntemplate<typename T>\nvoid print(const T &t) { cout << t << '\\n'; }\ntemplate<typename Head, typename... Tail>\nvoid print(const Head &head, const Tail &... tail) {\n cout << head << ' ';\n print(tail...);\n}\n\n#ifdef DEBUG\nvoid dbg() { cerr << '\\n'; }\ntemplate<typename T>\nvoid dbg(const T &t) { cerr << t << '\\n'; }\ntemplate<typename Head, typename... Tail>\nvoid dbg(const Head &head, const Tail &... tail) {\n cerr << head << ' ';\n dbg(tail...);\n}\n#else\ntemplate<typename... Args>\nvoid dbg(const Args &... args) {}\n#endif\n\ntemplate< typename T1, typename T2 >\nostream &operator<<(ostream &os, const pair< T1, T2 >& p) {\n os << p.first << \" \" << p.second;\n return os;\n}\n\ntemplate< typename T1, typename T2 >\nistream &operator>>(istream &is, pair< T1, T2 > &p) {\n is >> p.first >> p.second;\n return is;\n}\n\ntemplate< typename T >\nostream &operator<<(ostream &os, const vector< T > &v) {\n for(int i = 0; i < (int) v.size(); i++) {\n os << v[i] << (i + 1 != (int) v.size() ? \" \" : \"\");\n }\n return os;\n}\n\ntemplate< typename T >\nistream &operator>>(istream &is, vector< T > &v) {\n for(T &in : v) is >> in;\n return is;\n}\n\ntemplate<typename T>\nvector<vector<T>> split(typename vector<T>::const_iterator begin, typename vector<T>::const_iterator end, T val) {\n vector<vector<T>> res;\n vector<T> cur;\n for(auto it = begin; it != end; it++) {\n if(*it == val) {\n res.push_back(cur);\n cur.clear();\n } else cur.push_back(*it);\n }\n res.push_back(cur);\n return res;\n}\n\nvector<string> split(typename string::const_iterator begin, typename string::const_iterator end, char val) {\n vector<string> res;\n string cur = \"\";\n for(auto it = begin; it != end; it++) {\n if(*it == val) {\n res.push_back(cur);\n cur.clear();\n } else cur.push_back(*it);\n }\n res.push_back(cur);\n return res;\n}\n\ntemplate< typename T1, typename T2 >\ninline bool chmax(T1 &a, T2 b) { return a < b && (a = b, true); }\n\ntemplate< typename T1, typename T2 >\ninline bool chmin(T1 &a, T2 b) { return a > b && (a = b, true); }\n\ntemplate <typename T>\npair<VI, vector<T>> compress(const vector<T> &a) {\n int n = a.size();\n vector<T> x;\n REP(i, n) x.push_back(a[i]);\n sort(ALL(x)); x.erase(unique(ALL(x)), x.end());\n VI res(n);\n REP(i, n) res[i] = lower_bound(ALL(x), a[i]) - x.begin();\n return make_pair(res, x);\n}\n\ntemplate <typename It>\nauto rle(It begin, It end) {\n vector<pair<typename It::value_type, int>> res;\n if(begin == end) return res;\n auto pre = *begin;\n int num = 1;\n for(auto it = begin + 1; it != end; it++) {\n if(pre != *it) {\n res.emplace_back(pre, num);\n pre = *it;\n num = 1;\n } else num++;\n }\n res.emplace_back(pre, num);\n return res;\n}\n\ntemplate <typename It>\nvector<pair<typename It::value_type, int>> rle_sort(It begin, It end) {\n vector<typename It::value_type> cloned(begin, end);\n sort(ALL(cloned));\n auto e = rle(ALL(cloned));\n sort(ALL(e), [](const auto& l, const auto& r) { return l.second < r.second; });\n return e;\n}\n\ntemplate <typename T>\npair<vector<T>, vector<T>> factorial(int n) {\n vector<T> res(n+1), rev(n+1);\n res[0] = 1;\n REP(i, n) res[i+1] = res[i] * (i+1);\n rev[n] = 1 / res[n];\n for(int i = n; i > 0; i--) {\n rev[i-1] = rev[i] * i;\n }\n return make_pair(res, rev);\n}\n#line 3 \"library/KowerKoint/internal_operator.hpp\"\n\nnamespace internal_operator {\n template <typename T>\n T default_add(T a, T b) { return a + b; }\n template <typename T>\n T default_sub(T a, T b) { return a - b; }\n template <typename T>\n T zero() { return T(0); }\n template <typename T>\n T default_div(T a, T b) { return a / b; }\n template <typename T>\n T default_mult(T a, T b) { return a * b; }\n template <typename T>\n T one() { return T(1); }\n template <typename T>\n T default_xor(T a, T b) { return a ^ b; }\n template <typename T>\n T default_and(T a, T b) { return a & b; }\n template <typename T>\n T default_or(T a, T b) { return a | b; }\n ll mod3() { return 998244353LL; }\n ll mod7() { return 1000000007LL; }\n ll mod9() { return 1000000009LL; }\n template <typename T>\n T default_max(T a, T b) { return max(a, b); }\n template <typename T>\n T default_min(T a, T b) { return min(a, b); }\n}\n\n#line 3 \"library/KowerKoint/integer.hpp\"\n\nll kth_root(ll x, ll k) {\n if(k == 1) return x;\n ll res = 0;\n for(int i = 31; i >= 0; i--) {\n bool over = false;\n ll tmp = 1;\n ll nxt = res | 1LL << i;\n REP(i, k) {\n if(tmp > x / nxt) {\n over = true;\n break;\n }\n tmp *= nxt;\n }\n if(!over) res = nxt;\n }\n return res;\n}\n\nll sqrt(ll x) {\n return kth_root(x, 2);\n}\n\nstruct Prime {\n VI sieved;\n VL primes;\n\n Prime() {}\n Prime(ll n) {\n expand(n);\n }\n\n void expand(ll n) {\n ll sz = (ll)sieved.size() - 1;\n if(n <= sz) return;\n sieved.resize(n+1);\n sieved[0] = sieved[1] = 1;\n primes.clear();\n primes.push_back(2);\n for(ll d = 4; d <= n; d += 2) sieved[d] = 1;\n FOR(d, 3, n+1) {\n if(!sieved[d]) {\n primes.push_back(d);\n for(ll i = d*d; i <= n; i += d*2) sieved[i] = 1;\n }\n }\n }\n\n bool is_prime(ll n) {\n assert(n > 0);\n if(n <= (ll)sieved.size() - 1) return !sieved[n];\n for(ll d = 2; d*d <= n; d++) {\n if(n % d == 0) return false;\n }\n return true;\n }\n\n VL least_prime_factors(ll n) {\n assert(n > 0);\n VL lpfs(n+1, -1), primes;\n FOR(d, 2, n+1) {\n if(lpfs[d] == -1) {\n lpfs[d] = d;\n primes.push_back(d);\n }\n for(ll p : primes) {\n if(p * d > n || p > lpfs[d]) break;\n lpfs[p*d] = p;\n }\n }\n return lpfs;\n }\n\n VL prime_list(ll n) {\n assert(n > 0);\n expand(n);\n return VL(primes.begin(), upper_bound(ALL(primes), n));\n }\n\n vector<pair<ll, int>> prime_factor(ll n) {\n assert(n > 0);\n vector<pair<ll, int>> factor;\n expand(sqrt(n));\n for(ll prime : primes) {\n if(prime * prime > n) break;\n int cnt = 0;\n while(n % prime == 0) {\n n /= prime;\n cnt++;\n }\n if(cnt) factor.emplace_back(prime, cnt);\n }\n if(n > 1) factor.emplace_back(n, 1);\n return factor;\n }\n\n\n VL divisor(ll n) {\n assert(n > 0);\n auto factor = prime_factor(n);\n VL res = {1};\n for(auto [prime, cnt] : factor) {\n int sz = res.size();\n res.resize(sz * (cnt+1));\n REP(i, sz*cnt) res[sz+i] = res[i] * prime;\n REP(i, cnt) inplace_merge(res.begin(), res.begin() + sz*(i+1), res.begin() + sz*(i+2));\n }\n return res;\n }\n};\n\nll extgcd(ll a, ll b, ll& x, ll& y) {\n x = 1, y = 0;\n ll nx = 0, ny = 1;\n while(b) {\n ll q = a / b;\n tie(a, b) = LP(b, a % b);\n tie(x, nx) = LP(nx, x - nx*q);\n tie(y, ny) = LP(ny, y - ny*q);\n }\n return a;\n}\n\nll inv_mod(ll n, ll m) {\n ll x, y;\n assert(extgcd(n, m, x, y) == 1);\n x %= m;\n if(x < 0) x += m;\n return x;\n}\n\nll pow_mod(ll a, ll n, ll m) {\n if(n == 0) return 1LL;\n if(n < 0) return inv_mod(pow_mod(a, -n, m), m);\n ll res = 1;\n while(n) {\n if(n & 1) {\n res *= a;\n res %= m;\n }\n n >>= 1;\n a *= a;\n a %= m;\n }\n return res;\n}\n\n#line 5 \"library/KowerKoint/modint.hpp\"\n\ntemplate <ll (*mod)()>\nstruct Modint {\n ll val;\n \n Modint(): val(0) {}\n\n Modint(ll x): val(x) {\n val %= mod();\n if(val < 0) val += mod();\n }\n\n Modint& operator+=(const Modint& r) {\n val += r.val;\n if(val >= mod()) val -= mod();\n return *this;\n }\n friend Modint operator+(const Modint& l, const Modint& r) {\n return Modint(l) += r;\n }\n\n Modint& operator-=(const Modint& r) {\n val -= r.val;\n if(val < 0) val += mod();\n return *this;\n }\n friend Modint operator-(const Modint& l, const Modint& r) {\n return Modint(l) -= r;\n }\n\n Modint& operator*=(const Modint& r) {\n val *= r.val;\n val %= mod();\n return *this;\n }\n Modint operator*(const Modint& r) {\n return (Modint(*this) *= r);\n }\n friend Modint operator*(const Modint& l, const Modint& r) {\n return Modint(l) *= r;\n }\n\n Modint pow(ll n) const {\n return Modint(pow_mod(val, n, mod()));\n }\n\n Modint inv() const {\n return Modint(inv_mod(val, mod()));\n }\n\n Modint& operator/=(const Modint& r) {\n return (*this *= r.inv());\n }\n friend Modint operator/(const Modint& l, const Modint& r) {\n return Modint(l) /= r;\n }\n\n Modint& operator^=(const ll n) {\n val = pow_mod(val, n, mod());\n return *this;\n }\n Modint operator^(const ll n) {\n return this->pow(n);\n }\n\n Modint operator+() const { return *this; }\n Modint operator-() const { return Modint() - *this; }\n\n Modint& operator++() {\n val++;\n if(val == mod()) val = 0LL;\n return *this;\n }\n Modint& operator++(int) {\n Modint res(*this);\n ++*this;\n return res;\n }\n\n Modint& operator--() {\n if(val == 0LL) val = mod();\n val--;\n return *this;\n }\n Modint& operator--(int) {\n Modint res(*this);\n --*this;\n return res;\n }\n\n friend bool operator==(const Modint& l, const Modint& r) {\n return l.val == r.val;\n }\n friend bool operator!=(const Modint& l, const Modint& r) {\n return l.val != r.val;\n }\n\n static pair<vector<Modint>, vector<Modint>> factorial(int n) {\n vector<Modint> fact(n+1), rfact(n+1);\n fact[0] = 1;\n REP(i, n) fact[i+1] = fact[i] * (i+1);\n rfact[n] = 1 / fact[n];\n for(int i = n-1; i >= 0; i--) rfact[i] = rfact[i+1] * (i+1);\n return {fact, rfact};\n }\n\n friend istream& operator>>(istream& is, Modint& mi) {\n is >> mi.val;\n return is;\n }\n\n friend ostream& operator<<(ostream& os, const Modint& mi) {\n os << mi.val;\n return os;\n }\n};\n\nusing MI3 = Modint<internal_operator::mod3>;\nusing V3 = vector<MI3>;\nusing VV3 = vector<V3>;\nusing VVV3 = vector<VV3>;\nusing MI7 = Modint<internal_operator::mod7>;\nusing V7 = vector<MI7>;\nusing VV7 = vector<V7>;\nusing VVV7 = vector<VV7>;\nusing MI9 = Modint<internal_operator::mod9>;\nusing V9 = vector<MI9>;\nusing VV9 = vector<V9>;\nusing VVV9 = vector<VV9>;\n#line 3 \"library/KowerKoint/counting.hpp\"\n\ntemplate <typename T>\nstruct Counting {\n vector<T> fact, ifact;\n\n Counting() {}\n Counting(ll n) {\n expand(n);\n }\n\n void expand(ll n) {\n ll sz = (ll)fact.size();\n if(sz > n) return;\n fact.resize(n+1);\n ifact.resize(n+1);\n fact[0] = 1;\n FOR(i, max(1LL, sz), n+1) fact[i] = fact[i-1] * i;\n ifact[n] = 1 / fact[n];\n for(ll i = n-1; i >= sz; i--) ifact[i] = ifact[i+1] * (i+1);\n }\n\n T permutation(ll n, ll r) {\n assert(n >= r);\n assert(r >= 0);\n expand(n);\n return fact[n] * ifact[n-r];\n }\n\n T combination(ll n, ll r) {\n assert(n >= r);\n assert(r >= 0);\n expand(n);\n return fact[n] * ifact[r] * ifact[n-r];\n }\n\n T stirling(ll n, ll k) {\n assert(n >= k);\n assert(k >= 0);\n if(n == 0) return 1;\n T res = 0;\n int sign = k%2? -1 : 1;\n expand(k);\n REP(i, k+1) {\n res += sign * ifact[i] * ifact[k-i] * T(i).pow(n);\n sign *= -1;\n }\n return res;\n }\n\n vector<vector<T>> stirling_table(ll n, ll k) {\n assert(n >= 0 && k >= 0);\n vector<vector<T>> res(n+1, vector<T>(k+1));\n res[0][0] = 1;\n FOR(i, 1, n+1) FOR(j, 1, k+1) {\n res[i][j] = res[i-1][j-1] + j * res[i-1][j];\n }\n return res;\n }\n\n T bell(ll n, ll k) {\n assert(n >= 0 && k >= 0);\n expand(k);\n vector<T> tmp(k+1);\n int sign = 1;\n tmp[0] = 1;\n FOR(i, 1, k+1) {\n sign *= -1;\n tmp[i] = tmp[i-1] + sign * ifact[i];\n }\n T res = 0;\n REP(i, k+1) {\n res += T(i).pow(n) * ifact[i] * tmp[k-i];\n }\n return res;\n }\n\n vector<vector<T>> partition_table(ll n) {\n assert(n >= 0);\n vector<vector<T>> res(n+1, vector<T>(n+1));\n REP(i, n+1) res[0][i] = 1;\n FOR(i, 1, n+1) FOR(j, 1, n+1) {\n res[i][j] = res[i][j-1] + (i<j? 0 : res[i-j][j]);\n }\n return res;\n }\n};\n#line 2 \"library/KowerKoint/segtree.hpp\"\n\ntemplate <typename T, T (*func)(const T, const T), T (*e)()>\nstruct SegTree {\n int n, sz;\n vector<T> state;\n\n SegTree(int n_): n(n_) {\n sz = 1;\n while(sz < n) sz <<= 1;\n state = vector<T>(sz*2, e());\n }\n SegTree(vector<T> v): n(v.size()) {\n sz = 1;\n while(sz < n) sz <<= 1;\n state = vector<T>(sz*2, e());\n REP(i, v.size()) state[sz+i] = v[i];\n for(int i = sz-1; i > 0; i--) state[i] = func(state[i*2], state[i*2+1]);\n }\n\n T operator[](int i) const {\n assert(0 <= i && i < n);\n return state[sz+i];\n }\n T get(int i) const {\n assert(0 <= i && i < n);\n return state[sz+i];\n }\n\n void set(int i, const T &x) {\n assert(0 <= i && i < n);\n i += sz;\n state[i] = x;\n while(i >>= 1) {\n state[i] = func(state[i*2], state[i*2+1]);\n }\n }\n void apply(int i, const T &x) {\n set(i, x);\n }\n\n T prod(int l, int r) const {\n assert(0 <= l && l <= r && r <= n);\n T left_prod = e(), right_prod = e();\n l += sz, r += sz;\n while(l < r) {\n if(l & 1) left_prod = func(left_prod, state[l++]);\n if(r & 1) right_prod = func(state[--r], right_prod);\n l >>= 1;\n r >>= 1;\n }\n return func(left_prod, right_prod);\n }\n \n T all_prod() const {\n return state[1];\n }\n\n template <typename F>\n int max_right(int l, F f) {\n assert(0 <= l && l <= n);\n assert(f(e()));\n if(l == n) return n;\n l += sz;\n while(l % 2 == 0) l >>= 1;\n T sum = e();\n while(f(func(sum, state[l]))) {\n if(__builtin_clz(l) != __builtin_clz(l+1)) return n;\n sum = func(sum, state[l]);\n l++;\n while(l % 2 == 0) l >>= 1;\n }\n while(l < sz) {\n if(!f(func(sum, state[l*2]))) l *= 2;\n else {\n sum = func(sum, state[l*2]);\n l = l*2 + 1;\n }\n }\n return min(n, l - sz);\n }\n};\n\ntemplate <typename T>\nusing RMaxQ = SegTree<T, internal_operator::default_max<T>, numeric_limits<T>::min>;\ntemplate <typename T>\nusing RMinQ = SegTree<T, internal_operator::default_min<T>, numeric_limits<T>::max>;\ntemplate <typename T>\nusing RSumQ = SegTree<T, internal_operator::default_add<T>, internal_operator::zero<T>>;\n#line 3 \"Contests/Dummy/main.cpp\"\n\n/* #include <atcoder/all> */\n/* using namespace atcoder; */\n/* #include \"KowerKoint/expansion/ac-library/all.hpp\" */\n\nvoid solve(){\n auto run = [] {\n int n, k, m; cin >> n >> k >> m;\n if(!n) return false;\n RSumQ<int> rsq(n);\n REP(i, n) rsq.set(i, 1);\n int cursor = m-1;\n rsq.set(cursor, 0);\n REP(i, n-1) {\n int mm = k;\n int rprod = rsq.prod(cursor, n);\n if(rprod >= mm) {\n cursor = rsq.max_right(cursor, [&](int x) { return x < mm; });\n assert(cursor < n);\n assert(rsq[cursor] == 1);\n rsq.set(cursor, 0);\n } else {\n mm -= rprod;\n mm = (mm-1) % rsq.all_prod() + 1;\n cursor = rsq.max_right(0, [&](int x) { return x < mm; });\n assert(cursor < n);\n assert(rsq[cursor] == 1);\n rsq.set(cursor, 0);\n }\n }\n print(cursor+1);\n return true;\n };\n while(run());\n}\n\n// generated by oj-template v4.7.2 (https://github.com/online-judge-tools/template-generator)\nint main() {\n // Fasterize input/output script\n ios::sync_with_stdio(false);\n cin.tie(nullptr);\n cout << fixed << setprecision(100);\n // scanf/printf user should delete this fasterize input/output script\n\n int t = 1;\n //cin >> t; // comment out if solving multi testcase\n for(int testCase = 1;testCase <= t;++testCase){\n solve();\n }\n return 0;\n}", "accuracy": 1, "time_ms": 50, "memory_kb": 3504, "score_of_the_acc": -0.1716, "final_rank": 6 }, { "submission_id": "aoj_1275_6213505", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\n#define all(A) A.begin(),A.end()\nusing vll = vector<ll>;\n#define rep(i, n) for (long long i = 0; i < (long long)(n); i++)\n\n\n\nint main() {\n\n\n while (1) {\n ll N, K, M;\n cin >> N >> K >> M;\n M--;\n if (N == 0)return 0;\n vll R(N),L(N);\n rep(i,N){\n R[i]=(i+1)%N;\n L[i]=(i-1+N)%N;\n }\n\n N--;\n L[R[M]]=L[M];\n R[L[M]]=R[M];\n //M=R[M];\n while(N>0){\n rep(i,(K%N==0?N:K%N)){\n M=R[M];\n //cout<<M<<\" \";\n }\n //cout<<endl;\n N--;\n L[R[M]]=L[M];\n R[L[M]]=R[M];\n //M=R[M];\n\n }\n cout<<M+1<<endl;\n\n\n }\n\n\n\n}", "accuracy": 1, "time_ms": 780, "memory_kb": 3448, "score_of_the_acc": -0.4893, "final_rank": 16 }, { "submission_id": "aoj_1275_6008377", "code_snippet": "//#define _GLIBCXX_DEBUG\n#include <bits/stdc++.h>\nusing namespace std;\n// #include <atcoder/all>\n// using namespace atcoder;\n// using mint = modint1000000007;\nusing ll = long long;\nconst int MOD = (int)1e9 + 7;\nconst int INF = (int)1e9 + 1001010;\nconst ll llINF = (ll)1e18 + 11000010;\nconst double PI = 3.14159265358979;\n#define ALL(x) x.begin(),x.end()\n#define RALL(x) x.rbegin(),x.rend()\n#define endn \"\\n\"\n\n#ifndef ATCODER_INTERNAL_BITOP_HPP\n#define ATCODER_INTERNAL_BITOP_HPP 1\n#ifdef _MSC_VER\n#include <intrin.h>\n#endif\nnamespace atcoder {\nnamespace internal {\n// @param n `0 <= n`\n// @return minimum non-negative `x` s.t. `n <= 2**x`\nint ceil_pow2(int n) {\n int x = 0;\n while ((1U << x) < (unsigned int)(n)) x++;\n return x;\n}\n// @param n `1 <= n`\n// @return minimum non-negative `x` s.t. `(n & (1 << x)) != 0`\nint bsf(unsigned int n) {\n#ifdef _MSC_VER\n unsigned long index;\n _BitScanForward(&index, n);\n return index;\n#else\n return __builtin_ctz(n);\n#endif\n}\n} // namespace internal\n} // namespace atcoder\n#endif // ATCODER_INTERNAL_BITOP_HPP\n#ifndef ATCODER_INTERNAL_MATH_HPP\n#define ATCODER_INTERNAL_MATH_HPP 1\n#include <utility>\nnamespace atcoder {\nnamespace internal {\n// @param m `1 <= m`\n// @return x mod m\nconstexpr long long safe_mod(long long x, long long m) {\n x %= m;\n if (x < 0) x += m;\n return x;\n}\n// Fast moduler by barrett reduction\n// Reference: https://en.wikipedia.org/wiki/Barrett_reduction\n// NOTE: reconsider after Ice Lake\nstruct barrett {\n unsigned int _m;\n unsigned long long im;\n // @param m `1 <= m`\n barrett(unsigned int m) : _m(m), im((unsigned long long)(-1) / m + 1) {}\n // @return m\n unsigned int umod() const { return _m; }\n // @param a `0 <= a < m`\n // @param b `0 <= b < m`\n // @return `a * b % m`\n unsigned int mul(unsigned int a, unsigned int b) const {\n // [1] m = 1\n // a = b = im = 0, so okay\n // [2] m >= 2\n // im = ceil(2^64 / m)\n // -> im * m = 2^64 + r (0 <= r < m)\n // let z = a*b = c*m + d (0 <= c, d < m)\n // a*b * im = (c*m + d) * im = c*(im*m) + d*im = c*2^64 + c*r + d*im\n // c*r + d*im < m * m + m * im < m * m + 2^64 + m <= 2^64 + m * (m + 1) < 2^64 * 2\n // ((ab * im) >> 64) == c or c + 1\n unsigned long long z = a;\n z *= b;\n#ifdef _MSC_VER\n unsigned long long x;\n _umul128(z, im, &x);\n#else\n unsigned long long x =\n (unsigned long long)(((unsigned __int128)(z)*im) >> 64);\n#endif\n unsigned int v = (unsigned int)(z - x * _m);\n if (_m <= v) v += _m;\n return v;\n }\n};\n// @param n `0 <= n`\n// @param m `1 <= m`\n// @return `(x ** n) % m`\nconstexpr long long pow_mod_constexpr(long long x, long long n, int m) {\n if (m == 1) return 0;\n unsigned int _m = (unsigned int)(m);\n unsigned long long r = 1;\n unsigned long long y = safe_mod(x, m);\n while (n) {\n if (n & 1) r = (r * y) % _m;\n y = (y * y) % _m;\n n >>= 1;\n }\n return r;\n}\n// Reference:\n// M. Forisek and J. Jancina,\n// Fast Primality Testing for Integers That Fit into a Machine Word\n// @param n `0 <= n`\nconstexpr bool is_prime_constexpr(int n) {\n if (n <= 1) return false;\n if (n == 2 || n == 7 || n == 61) return true;\n if (n % 2 == 0) return false;\n long long d = n - 1;\n while (d % 2 == 0) d /= 2;\n for (long long a : {2, 7, 61}) {\n long long t = d;\n long long y = pow_mod_constexpr(a, t, n);\n while (t != n - 1 && y != 1 && y != n - 1) {\n y = y * y % n;\n t <<= 1;\n }\n if (y != n - 1 && t % 2 == 0) {\n return false;\n }\n }\n return true;\n}\ntemplate <int n> constexpr bool is_prime = is_prime_constexpr(n);\n// @param b `1 <= b`\n// @return pair(g, x) s.t. g = gcd(a, b), xa = g (mod b), 0 <= x < b/g\nconstexpr std::pair<long long, long long> inv_gcd(long long a, long long b) {\n a = safe_mod(a, b);\n if (a == 0) return {b, 0};\n // Contracts:\n // [1] s - m0 * a = 0 (mod b)\n // [2] t - m1 * a = 0 (mod b)\n // [3] s * |m1| + t * |m0| <= b\n long long s = b, t = a;\n long long m0 = 0, m1 = 1;\n while (t) {\n long long u = s / t;\n s -= t * u;\n m0 -= m1 * u; // |m1 * u| <= |m1| * s <= b\n // [3]:\n // (s - t * u) * |m1| + t * |m0 - m1 * u|\n // <= s * |m1| - t * u * |m1| + t * (|m0| + |m1| * u)\n // = s * |m1| + t * |m0| <= b\n auto tmp = s;\n s = t;\n t = tmp;\n tmp = m0;\n m0 = m1;\n m1 = tmp;\n }\n // by [3]: |m0| <= b/g\n // by g != b: |m0| < b/g\n if (m0 < 0) m0 += b / s;\n return {s, m0};\n}\n// Compile time primitive root\n// @param m must be prime\n// @return primitive root (and minimum in now)\nconstexpr int primitive_root_constexpr(int m) {\n if (m == 2) return 1;\n if (m == 167772161) return 3;\n if (m == 469762049) return 3;\n if (m == 754974721) return 11;\n if (m == 998244353) return 3;\n int divs[20] = {};\n divs[0] = 2;\n int cnt = 1;\n int x = (m - 1) / 2;\n while (x % 2 == 0) x /= 2;\n for (int i = 3; (long long)(i)*i <= x; i += 2) {\n if (x % i == 0) {\n divs[cnt++] = i;\n while (x % i == 0) {\n x /= i;\n }\n }\n }\n if (x > 1) {\n divs[cnt++] = x;\n }\n for (int g = 2;; g++) {\n bool ok = true;\n for (int i = 0; i < cnt; i++) {\n if (pow_mod_constexpr(g, (m - 1) / divs[i], m) == 1) {\n ok = false;\n break;\n }\n }\n if (ok) return g;\n }\n}\ntemplate <int m> constexpr int primitive_root = primitive_root_constexpr(m);\n} // namespace internal\n} // namespace atcoder\n#endif // ATCODER_INTERNAL_MATH_HPP\n#ifndef ATCODER_INTERNAL_QUEUE_HPP\n#define ATCODER_INTERNAL_QUEUE_HPP 1\n#include <vector>\nnamespace atcoder {\nnamespace internal {\ntemplate <class T> struct simple_queue {\n std::vector<T> payload;\n int pos = 0;\n void reserve(int n) { payload.reserve(n); }\n int size() const { return int(payload.size()) - pos; }\n bool empty() const { return pos == int(payload.size()); }\n void push(const T& t) { payload.push_back(t); }\n T& front() { return payload[pos]; }\n void clear() {\n payload.clear();\n pos = 0;\n }\n void pop() { pos++; }\n};\n} // namespace internal\n} // namespace atcoder\n#endif // ATCODER_INTERNAL_QUEUE_HPP\n#ifndef ATCODER_INTERNAL_SCC_HPP\n#define ATCODER_INTERNAL_SCC_HPP 1\n#include <algorithm>\n#include <utility>\n#include <vector>\nnamespace atcoder {\nnamespace internal {\ntemplate <class E> struct csr {\n std::vector<int> start;\n std::vector<E> elist;\n csr(int n, const std::vector<std::pair<int, E>>& edges)\n : start(n + 1), elist(edges.size()) {\n for (auto e : edges) {\n start[e.first + 1]++;\n }\n for (int i = 1; i <= n; i++) {\n start[i] += start[i - 1];\n }\n auto counter = start;\n for (auto e : edges) {\n elist[counter[e.first]++] = e.second;\n }\n }\n};\n// Reference:\n// R. Tarjan,\n// Depth-First Search and Linear Graph Algorithms\nstruct scc_graph {\n public:\n scc_graph(int n) : _n(n) {}\n int num_vertices() { return _n; }\n void add_edge(int from, int to) { edges.push_back({from, {to}}); }\n // @return pair of (# of scc, scc id)\n std::pair<int, std::vector<int>> scc_ids() {\n auto g = csr<edge>(_n, edges);\n int now_ord = 0, group_num = 0;\n std::vector<int> visited, low(_n), ord(_n, -1), ids(_n);\n visited.reserve(_n);\n auto dfs = [&](auto self, int v) -> void {\n low[v] = ord[v] = now_ord++;\n visited.push_back(v);\n for (int i = g.start[v]; i < g.start[v + 1]; i++) {\n auto to = g.elist[i].to;\n if (ord[to] == -1) {\n self(self, to);\n low[v] = std::min(low[v], low[to]);\n } else {\n low[v] = std::min(low[v], ord[to]);\n }\n }\n if (low[v] == ord[v]) {\n while (true) {\n int u = visited.back();\n visited.pop_back();\n ord[u] = _n;\n ids[u] = group_num;\n if (u == v) break;\n }\n group_num++;\n }\n };\n for (int i = 0; i < _n; i++) {\n if (ord[i] == -1) dfs(dfs, i);\n }\n for (auto& x : ids) {\n x = group_num - 1 - x;\n }\n return {group_num, ids};\n }\n std::vector<std::vector<int>> scc() {\n auto ids = scc_ids();\n int group_num = ids.first;\n std::vector<int> counts(group_num);\n for (auto x : ids.second) counts[x]++;\n std::vector<std::vector<int>> groups(ids.first);\n for (int i = 0; i < group_num; i++) {\n groups[i].reserve(counts[i]);\n }\n for (int i = 0; i < _n; i++) {\n groups[ids.second[i]].push_back(i);\n }\n return groups;\n }\n private:\n int _n;\n struct edge {\n int to;\n };\n std::vector<std::pair<int, edge>> edges;\n};\n} // namespace internal\n} // namespace atcoder\n#endif // ATCODER_INTERNAL_SCC_HPP\n#ifndef ATCODER_INTERNAL_TYPE_TRAITS_HPP\n#define ATCODER_INTERNAL_TYPE_TRAITS_HPP 1\n#include <cassert>\n#include <numeric>\n#include <type_traits>\nnamespace atcoder {\nnamespace internal {\n#ifndef _MSC_VER\ntemplate <class T>\nusing is_signed_int128 =\n typename std::conditional<std::is_same<T, __int128_t>::value ||\n std::is_same<T, __int128>::value,\n std::true_type,\n std::false_type>::type;\ntemplate <class T>\nusing is_unsigned_int128 =\n typename std::conditional<std::is_same<T, __uint128_t>::value ||\n std::is_same<T, unsigned __int128>::value,\n std::true_type,\n std::false_type>::type;\ntemplate <class T>\nusing make_unsigned_int128 =\n typename std::conditional<std::is_same<T, __int128_t>::value,\n __uint128_t,\n unsigned __int128>;\ntemplate <class T>\nusing is_integral = typename std::conditional<std::is_integral<T>::value ||\n is_signed_int128<T>::value ||\n is_unsigned_int128<T>::value,\n std::true_type,\n std::false_type>::type;\ntemplate <class T>\nusing is_signed_int = typename std::conditional<(is_integral<T>::value &&\n std::is_signed<T>::value) ||\n is_signed_int128<T>::value,\n std::true_type,\n std::false_type>::type;\ntemplate <class T>\nusing is_unsigned_int =\n typename std::conditional<(is_integral<T>::value &&\n std::is_unsigned<T>::value) ||\n is_unsigned_int128<T>::value,\n std::true_type,\n std::false_type>::type;\ntemplate <class T>\nusing to_unsigned = typename std::conditional<\n is_signed_int128<T>::value,\n make_unsigned_int128<T>,\n typename std::conditional<std::is_signed<T>::value,\n std::make_unsigned<T>,\n std::common_type<T>>::type>::type;\n#else\ntemplate <class T> using is_integral = typename std::is_integral<T>;\ntemplate <class T>\nusing is_signed_int =\n typename std::conditional<is_integral<T>::value && std::is_signed<T>::value,\n std::true_type,\n std::false_type>::type;\ntemplate <class T>\nusing is_unsigned_int =\n typename std::conditional<is_integral<T>::value &&\n std::is_unsigned<T>::value,\n std::true_type,\n std::false_type>::type;\ntemplate <class T>\nusing to_unsigned = typename std::conditional<is_signed_int<T>::value,\n std::make_unsigned<T>,\n std::common_type<T>>::type;\n#endif\ntemplate <class T>\nusing is_signed_int_t = std::enable_if_t<is_signed_int<T>::value>;\ntemplate <class T>\nusing is_unsigned_int_t = std::enable_if_t<is_unsigned_int<T>::value>;\ntemplate <class T> using to_unsigned_t = typename to_unsigned<T>::type;\n} // namespace internal\n} // namespace atcoder\n#endif // ATCODER_INTERNAL_TYPE_TRAITS_HPP\n#ifndef ATCODER_MODINT_HPP\n#define ATCODER_MODINT_HPP 1\n#include <cassert>\n#include <numeric>\n#include <type_traits>\n#ifdef _MSC_VER\n#include <intrin.h>\n#endif\nnamespace atcoder {\nnamespace internal {\nstruct modint_base {};\nstruct static_modint_base : modint_base {};\ntemplate <class T> using is_modint = std::is_base_of<modint_base, T>;\ntemplate <class T> using is_modint_t = std::enable_if_t<is_modint<T>::value>;\n} // namespace internal\ntemplate <int m, std::enable_if_t<(1 <= m)>* = nullptr>\nstruct static_modint : internal::static_modint_base {\n using mint = static_modint;\n public:\n static constexpr int mod() { return m; }\n static mint raw(int v) {\n mint x;\n x._v = v;\n return x;\n }\n static_modint() : _v(0) {}\n template <class T, internal::is_signed_int_t<T>* = nullptr>\n static_modint(T v) {\n long long x = (long long)(v % (long long)(umod()));\n if (x < 0) x += umod();\n _v = (unsigned int)(x);\n }\n template <class T, internal::is_unsigned_int_t<T>* = nullptr>\n static_modint(T v) {\n _v = (unsigned int)(v % umod());\n }\n static_modint(bool v) { _v = ((unsigned int)(v) % umod()); }\n unsigned int val() const { return _v; }\n mint& operator++() {\n _v++;\n if (_v == umod()) _v = 0;\n return *this;\n }\n mint& operator--() {\n if (_v == 0) _v = umod();\n _v--;\n return *this;\n }\n mint operator++(int) {\n mint result = *this;\n ++*this;\n return result;\n }\n mint operator--(int) {\n mint result = *this;\n --*this;\n return result;\n }\n mint& operator+=(const mint& rhs) {\n _v += rhs._v;\n if (_v >= umod()) _v -= umod();\n return *this;\n }\n mint& operator-=(const mint& rhs) {\n _v -= rhs._v;\n if (_v >= umod()) _v += umod();\n return *this;\n }\n mint& operator*=(const mint& rhs) {\n unsigned long long z = _v;\n z *= rhs._v;\n _v = (unsigned int)(z % umod());\n return *this;\n }\n mint& operator/=(const mint& rhs) { return *this = *this * rhs.inv(); }\n mint operator+() const { return *this; }\n mint operator-() const { return mint() - *this; }\n mint pow(long long n) const {\n assert(0 <= n);\n mint x = *this, r = 1;\n while (n) {\n if (n & 1) r *= x;\n x *= x;\n n >>= 1;\n }\n return r;\n }\n mint inv() const {\n if (prime) {\n assert(_v);\n return pow(umod() - 2);\n } else {\n auto eg = internal::inv_gcd(_v, m);\n assert(eg.first == 1);\n return eg.second;\n }\n }\n friend mint operator+(const mint& lhs, const mint& rhs) {\n return mint(lhs) += rhs;\n }\n friend mint operator-(const mint& lhs, const mint& rhs) {\n return mint(lhs) -= rhs;\n }\n friend mint operator*(const mint& lhs, const mint& rhs) {\n return mint(lhs) *= rhs;\n }\n friend mint operator/(const mint& lhs, const mint& rhs) {\n return mint(lhs) /= rhs;\n }\n friend bool operator==(const mint& lhs, const mint& rhs) {\n return lhs._v == rhs._v;\n }\n friend bool operator!=(const mint& lhs, const mint& rhs) {\n return lhs._v != rhs._v;\n }\n private:\n unsigned int _v;\n static constexpr unsigned int umod() { return m; }\n static constexpr bool prime = internal::is_prime<m>;\n};\ntemplate <int id> struct dynamic_modint : internal::modint_base {\n using mint = dynamic_modint;\n public:\n static int mod() { return (int)(bt.umod()); }\n static void set_mod(int m) {\n assert(1 <= m);\n bt = internal::barrett(m);\n }\n static mint raw(int v) {\n mint x;\n x._v = v;\n return x;\n }\n dynamic_modint() : _v(0) {}\n template <class T, internal::is_signed_int_t<T>* = nullptr>\n dynamic_modint(T v) {\n long long x = (long long)(v % (long long)(mod()));\n if (x < 0) x += mod();\n _v = (unsigned int)(x);\n }\n template <class T, internal::is_unsigned_int_t<T>* = nullptr>\n dynamic_modint(T v) {\n _v = (unsigned int)(v % mod());\n }\n dynamic_modint(bool v) { _v = ((unsigned int)(v) % mod()); }\n unsigned int val() const { return _v; }\n mint& operator++() {\n _v++;\n if (_v == umod()) _v = 0;\n return *this;\n }\n mint& operator--() {\n if (_v == 0) _v = umod();\n _v--;\n return *this;\n }\n mint operator++(int) {\n mint result = *this;\n ++*this;\n return result;\n }\n mint operator--(int) {\n mint result = *this;\n --*this;\n return result;\n }\n mint& operator+=(const mint& rhs) {\n _v += rhs._v;\n if (_v >= umod()) _v -= umod();\n return *this;\n }\n mint& operator-=(const mint& rhs) {\n _v += mod() - rhs._v;\n if (_v >= umod()) _v -= umod();\n return *this;\n }\n mint& operator*=(const mint& rhs) {\n _v = bt.mul(_v, rhs._v);\n return *this;\n }\n mint& operator/=(const mint& rhs) { return *this = *this * rhs.inv(); }\n mint operator+() const { return *this; }\n mint operator-() const { return mint() - *this; }\n mint pow(long long n) const {\n assert(0 <= n);\n mint x = *this, r = 1;\n while (n) {\n if (n & 1) r *= x;\n x *= x;\n n >>= 1;\n }\n return r;\n }\n mint inv() const {\n auto eg = internal::inv_gcd(_v, mod());\n assert(eg.first == 1);\n return eg.second;\n }\n friend mint operator+(const mint& lhs, const mint& rhs) {\n return mint(lhs) += rhs;\n }\n friend mint operator-(const mint& lhs, const mint& rhs) {\n return mint(lhs) -= rhs;\n }\n friend mint operator*(const mint& lhs, const mint& rhs) {\n return mint(lhs) *= rhs;\n }\n friend mint operator/(const mint& lhs, const mint& rhs) {\n return mint(lhs) /= rhs;\n }\n friend bool operator==(const mint& lhs, const mint& rhs) {\n return lhs._v == rhs._v;\n }\n friend bool operator!=(const mint& lhs, const mint& rhs) {\n return lhs._v != rhs._v;\n }\n private:\n unsigned int _v;\n static internal::barrett bt;\n static unsigned int umod() { return bt.umod(); }\n};\ntemplate <int id> internal::barrett dynamic_modint<id>::bt = 998244353;\nusing modint998244353 = static_modint<998244353>;\nusing modint1000000007 = static_modint<1000000007>;\nusing modint = dynamic_modint<-1>;\nnamespace internal {\ntemplate <class T>\nusing is_static_modint = std::is_base_of<internal::static_modint_base, T>;\ntemplate <class T>\nusing is_static_modint_t = std::enable_if_t<is_static_modint<T>::value>;\ntemplate <class> struct is_dynamic_modint : public std::false_type {};\ntemplate <int id>\nstruct is_dynamic_modint<dynamic_modint<id>> : public std::true_type {};\ntemplate <class T>\nusing is_dynamic_modint_t = std::enable_if_t<is_dynamic_modint<T>::value>;\n} // namespace internal\n} // namespace atcoder\n#endif // ATCODER_MODINT_HPP\n\n#ifndef ATCODER_FENWICKTREE_HPP\n#define ATCODER_FENWICKTREE_HPP 1\n#include <cassert>\n#include <vector>\nnamespace atcoder {\n// Reference: https://en.wikipedia.org/wiki/Fenwick_tree\ntemplate <class T> struct fenwick_tree {\n using U = internal::to_unsigned_t<T>;\n public:\n fenwick_tree() : _n(0) {}\n fenwick_tree(int n) : _n(n), data(n) {}\n void add(int p, T x) {\n assert(0 <= p && p < _n);\n p++;\n while (p <= _n) {\n data[p - 1] += U(x);\n p += p & -p;\n }\n }\n T sum(int l, int r) {\n assert(0 <= l && l <= r && r <= _n);\n return sum(r) - sum(l);\n }\n private:\n int _n;\n std::vector<U> data;\n U sum(int r) {\n U s = 0;\n while (r > 0) {\n s += data[r - 1];\n r -= r & -r;\n }\n return s;\n }\n};\n} // namespace atcoder\n#endif // ATCODER_FENWICKTREE_HPP\n\nusing namespace atcoder;\n\nint main(){\n while(true){\n int n, k, m; cin >> n >> k >> m;\n if(n == 0 and k == 0 and m == 0) break;\n m--;\n\n set<int> erased;\n fenwick_tree<ll> fw(n);\n erased.insert(m);\n fw.add(m, 1);\n int pre_id = m - 1;\n for(int i = 0; i < n-2; i++){\n int remain = n - i - 1;\n int skipped_id = (((k % remain) + pre_id + remain) % remain);\n\n // 現在残っている配列のうち、skipped_id番目の要素を2分探索\n int left = -1;\n int right = n;\n while(right - left > 1){\n int mid = (left + right) / 2;\n int index = mid - fw.sum(0, mid);\n\n if(index == skipped_id and !erased.count(mid)){\n right = mid;\n break;\n }\n \n if(index > skipped_id) right = mid;\n else left = mid;\n\n // if(index > skipped_id) right = mid;\n // else if(index < skipped_id) left = mid;\n // else if(erased.count(mid)) right = mid;\n // else left = mid; \n\n // if(!erased.count(mid) and index >= skipped_id) right = mid;\n // else left = mid;\n\n // if(index < skipped_id) left = mid;\n // else right = mid;\n }\n\n int erase = right;\n fw.add(erase, 1);\n erased.insert(erase);\n pre_id = erase - fw.sum(0, erase) - 1;\n }\n\n int ans;\n for(int i = 0; i < n; i++){\n if(!erased.count(i)){\n ans = i + 1;\n break;\n } \n }\n\n cout << ans << endn;\n }\n return 0;\n}", "accuracy": 1, "time_ms": 180, "memory_kb": 3756, "score_of_the_acc": -0.3532, "final_rank": 13 }, { "submission_id": "aoj_1275_6008281", "code_snippet": "#pragma GCC target(\"avx2\")\n#pragma GCC optimize(\"O3\")\n#pragma GCC optimize(\"unroll-loops\")\n#include<bits/stdc++.h>\nusing namespace std;\n\nusing ll = long long;\nconst int INF = 1e9;\nconst ll inf = 1LL<<60;\n\nbool solve() {\n int n, k, m;\n cin >> n >> k >> m;\n if (n == 0) return false;\n m--;\n vector<int> v(n);\n iota(v.begin(), v.end(), 1);\n while (v.size() > 1) {\n // for (auto p : v) cout << p << \" \";\n // cout << endl;\n v.erase(v.begin() + m);\n m = (m+k-1)%v.size();\n }\n cout << v[0] << endl;\n return true;\n}\n\nint main() {\n ios::sync_with_stdio(false);\n std::cin.tie(nullptr);\n while (solve());\n}", "accuracy": 1, "time_ms": 70, "memory_kb": 3492, "score_of_the_acc": -0.1754, "final_rank": 7 }, { "submission_id": "aoj_1275_5991389", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\nint main() {\n while (true) {\n int n,k,m;\n cin >> n >> k >> m;\n if(n == 0 && k == 0 && m == 0) {\n return 0;\n }\n vector<int>tmp(n);\n iota(tmp.begin(),tmp.end(),0);\n tmp.erase(tmp.begin()+m-1);\n int last = m-1;\n while (tmp.size() > 1) {\n last += k-1;\n last %= tmp.size();\n tmp.erase(tmp.begin()+last);\n }\n cout << tmp[0]+1 << endl;\n }\n}", "accuracy": 1, "time_ms": 70, "memory_kb": 3164, "score_of_the_acc": -0.0189, "final_rank": 2 }, { "submission_id": "aoj_1275_5952529", "code_snippet": "#include <bits/stdc++.h>\n#define all(v) v.begin(), v.end()\n#define rall(v) v.rbegin(), v.rend()\n#define rep(i,n) for(int i=0;i<(int)(n);i++)\n#define drep(i,j,n) for(int i=0;i<(int)(n-1);i++)for(int j=i+1;j<(int)(n);j++)\n#define trep(i,j,k,n) for(int i=0;i<(int)(n-2);i++)for(int j=i+1;j<(int)(n-1);j++)for(int k=j+1;k<(int)(n);k++)\n#define codefor int test;scanf(\"%d\",&test);while(test--)\n#define INT(...) int __VA_ARGS__;in(__VA_ARGS__)\n#define LL(...) ll __VA_ARGS__;in(__VA_ARGS__)\n#define yes(ans) if(ans)printf(\"yes\\n\");else printf(\"no\\n\")\n#define Yes(ans) if(ans)printf(\"Yes\\n\");else printf(\"No\\n\")\n#define YES(ans) if(ans)printf(\"YES\\n\");else printf(\"NO\\n\")\n#define popcount(v) __builtin_popcount(v)\n#define vector2d(type,name,h,...) vector<vector<type>>name(h,vector<type>(__VA_ARGS__))\n#define vector3d(type,name,h,w,...) vector<vector<vector<type>>>name(h,vector<vector<type>>(w,vector<type>(__VA_ARGS__)))\nusing namespace std;\nusing ll = long long;\ntemplate<class T> using rpriority_queue = priority_queue<T, vector<T>, greater<T>>;\nconst int MOD=1000000007;\nconst int MOD2=998244353;\nconst int INF=1<<30;\nconst ll INF2=1LL<<60;\nvoid scan(int& a){scanf(\"%d\",&a);}\nvoid scan(long long& a){scanf(\"%lld\",&a);}\ntemplate<class T,class L>void scan(pair<T, L>& p){scan(p.first);scan(p.second);}\ntemplate<class T,class U,class V>void scan(tuple<T,U,V>& p){scan(get<0>(p));scan(get<1>(p));scan(get<2>(p));}\ntemplate<class T> void scan(T& a){cin>>a;}\ntemplate<class T> void scan(vector<T>& vec){for(auto&& it:vec)scan(it);}\nvoid in(){}\ntemplate <class Head, class... Tail> void in(Head& head, Tail&... tail){scan(head);in(tail...);}\nvoid print(const int& a){printf(\"%d\",a);}\nvoid print(const long long& a){printf(\"%lld\",a);}\nvoid print(const double& a){printf(\"%.15lf\",a);}\ntemplate<class T,class L>void print(const pair<T, L>& p){print(p.first);putchar(' ');print(p.second);}\ntemplate<class T> void print(const T& a){cout<<a;}\ntemplate<class T> void print(const vector<T>& vec){if(vec.empty())return;print(vec[0]);for(auto it=vec.begin();++it!= vec.end();){putchar(' ');print(*it);}}\nvoid out(){putchar('\\n');}\ntemplate<class T> void out(const T& t){print(t);putchar('\\n');}\ntemplate <class Head, class... Tail> void out(const Head& head,const Tail&... tail){print(head);putchar(' ');out(tail...);}\ntemplate<class T> void dprint(const T& a){cerr<<a;}\ntemplate<class T> void dprint(const vector<T>& vec){if(vec.empty())return;cerr<<vec[0];for(auto it=vec.begin();++it!= vec.end();){cerr<<\" \"<<*it;}}\nvoid debug(){cerr<<endl;}\ntemplate<class T> void debug(const T& t){dprint(t);cerr<<endl;}\ntemplate <class Head, class... Tail> void debug(const Head& head, const Tail&... tail){dprint(head);cerr<<\" \";debug(tail...);}\nll intpow(ll a, ll b){ ll ans = 1; while(b){ if(b & 1) ans *= a; a *= a; b /= 2; } return ans; }\nll modpow(ll a, ll b, ll p){ ll ans = 1; while(b){ if(b & 1) (ans *= a) %= p; (a *= a) %= p; b /= 2; } return ans; }\nll modinv(ll a, ll m) {ll b = m, u = 1, v = 0;while (b) {ll t = a / b;a -= t * b; swap(a, b);u -= t * v; swap(u, v);}u %= m;if (u < 0) u += m;return u;}\nll updivide(ll a,ll b){return (a+b-1)/b;}\ntemplate<class T> void chmax(T &a,const T b){if(b>a)a=b;}\ntemplate<class T> void chmin(T &a,const T b){if(b<a)a=b;}\n\ntemplate <class S, S (*op)(S, S), S (*e)()> struct segtree {\n public:\n segtree() : segtree(0) {}\n segtree(int n) : segtree(std::vector<S>(n, e())) {}\n segtree(const std::vector<S>& v) : _n(int(v.size())) {\n log = ceil_pow2(_n);\n size = 1 << log;\n d = std::vector<S>(2 * size, e());\n for (int i = 0; i < _n; i++) d[size + i] = v[i];\n for (int i = size - 1; i >= 1; i--) {\n update(i);\n }\n }\n\n void set(int p, S x) {\n assert(0 <= p && p < _n);\n p += size;\n d[p] = x;\n for (int i = 1; i <= log; i++) update(p >> i);\n }\n\n S get(int p) {\n assert(0 <= p && p < _n);\n return d[p + size];\n }\n\n S prod(int l, int r) {\n assert(0 <= l && l <= r && r <= _n);\n S sml = e(), smr = e();\n l += size;\n r += size;\n\n while (l < r) {\n if (l & 1) sml = op(sml, d[l++]);\n if (r & 1) smr = op(d[--r], smr);\n l >>= 1;\n r >>= 1;\n }\n return op(sml, smr);\n }\n\n S all_prod() { return d[1]; }\n\n template <bool (*f)(S)> int max_right(int l) {\n return max_right(l, [](S x) { return f(x); });\n }\n template <class F> int max_right(int l, F f) {\n assert(0 <= l && l <= _n);\n assert(f(e()));\n if (l == _n) return _n;\n l += size;\n S sm = e();\n do {\n while (l % 2 == 0) l >>= 1;\n if (!f(op(sm, d[l]))) {\n while (l < size) {\n l = (2 * l);\n if (f(op(sm, d[l]))) {\n sm = op(sm, d[l]);\n l++;\n }\n }\n return l - size;\n }\n sm = op(sm, d[l]);\n l++;\n } while ((l & -l) != l);\n return _n;\n }\n\n template <bool (*f)(S)> int min_left(int r) {\n return min_left(r, [](S x) { return f(x); });\n }\n template <class F> int min_left(int r, F f) {\n assert(0 <= r && r <= _n);\n assert(f(e()));\n if (r == 0) return 0;\n r += size;\n S sm = e();\n do {\n r--;\n while (r > 1 && (r % 2)) r >>= 1;\n if (!f(op(d[r], sm))) {\n while (r < size) {\n r = (2 * r + 1);\n if (f(op(d[r], sm))) {\n sm = op(d[r], sm);\n r--;\n }\n }\n return r + 1 - size;\n }\n sm = op(d[r], sm);\n } while ((r & -r) != r);\n return 0;\n }\n\n private:\n int _n, size, log;\n std::vector<S> d;\n int ceil_pow2(int n) {\n int x = 0;\n while ((1U << x) < (unsigned int)(n)) x++;\n return x;\n }\n void update(int k) { d[k] = op(d[2 * k], d[2 * k + 1]); }\n};\n\nint op(int a,int b){return a+b;}\nint e(){return 0;}\n\nint main(){\n int n,k,m;\n while(cin>>n>>k>>m){\n if(n==0)return 0;\n deque<int> dq(n);\n iota(all(dq),1);\n while(dq.front()!=m){\n dq.push_back(dq.front());\n dq.pop_front();\n }\n while(dq.size()>1){\n dq.pop_front();\n int t=(k-1)%dq.size();\n while(t--){\n dq.push_back(dq.front());\n dq.pop_front();\n }\n }\n out(dq.front());\n }\n}", "accuracy": 1, "time_ms": 810, "memory_kb": 3472, "score_of_the_acc": -0.5149, "final_rank": 17 }, { "submission_id": "aoj_1275_5947200", "code_snippet": "#include <bits/stdc++.h>\n#define rep(i,n) for(int i = 0; i < (n); i++)\nusing namespace std;\ntypedef long long ll;\n\ntemplate <typename T>\nstruct BIT {\n int n;\n vector<T> bit;\n BIT(int n_) : n(n_ + 1), bit(n, 0) {}\n\n // a_i += x\n void add(int i, T x){\n for(int id = i + 1; id <= n; id += (id & -id)) bit[id] += x;\n }\n\n // [0, i)\n T sum(int i){\n T s(0);\n for(int id = i + 0; id >= 1; id -= (id & -id)) s += bit[id];\n return s;\n }\n\n // [l, r)\n T query(int l, int r) { return sum(r) - sum(l); }\n\n // a_1 + a_2 + ... + a_x >= w となる最小の x\n int lower_bound(T w){\n if(w <= 0){\n return 0;\n }else{\n int x = 0, r = 1;\n while(r < n) r = r << 1;\n for(int len = r; len > 0; len = len >> 1){\n if(x + len < n && bit[x + len] < w){\n w -= bit[x + len];\n x += len;\n }\n }\n return x;\n }\n }\n};\n\nint main(){\n cin.tie(0);\n ios::sync_with_stdio(0);\n \n while(true) {\n int n,k,m; cin >> n >> k >> m; if(n == 0) break;\n m--;\n BIT<int> bit(n);\n rep(i,n) bit.add(i, 1);\n\n rep(i,n-1) {\n bit.add(m, -1);\n m = bit.lower_bound((bit.sum(m) + k - 1) % (n - i - 1) + 1);\n }\n cout << m + 1 << endl;\n }\n}", "accuracy": 1, "time_ms": 30, "memory_kb": 3176, "score_of_the_acc": -0.0057, "final_rank": 1 }, { "submission_id": "aoj_1275_5878917", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nusing ll = unsigned long long;\n#define rep(i, n) for (int i = 0; i < (n); i++)\n\nint fwk[20005];\nint n, k, m;\n\nint sum(int r) {\n int ans = 0;\n for (; r; r -= r & -r) ans += fwk[r];\n return ans;\n}\n\nint main() {\n while (true) {\n memset(fwk, 0, sizeof(fwk));\n scanf(\"%d%d%d\", &n, &k, &m);\n m--;\n if (n == 0) break;\n rep(i, n - 1) {\n for (int r = m + 1; r < size(fwk); r += r & -r) fwk[r]++;\n for (int r = m + 1 + n; r < size(fwk); r += r & -r) fwk[r]++;\n int kx = (k - 1) % (n - i - 1) + 1;\n int ng = 0, ok = n;\n while (ok - ng > 1) {\n int r = (ok + ng) / 2;\n (r - (sum(m + r) - sum(m)) >= kx ? ok : ng) = r;\n }\n m = (m + ng) % n;\n }\n printf(\"%d\\n\", m + 1);\n }\n}", "accuracy": 1, "time_ms": 110, "memory_kb": 3544, "score_of_the_acc": -0.219, "final_rank": 11 } ]
aoj_1270_cpp
Problem E: Manhattan Wiring There is a rectangular area containing n × m cells. Two cells are marked with "2", and another two with "3". Some cells are occupied by obstacles. You should connect the two "2"s and also the two "3"s with non-intersecting lines. Lines can run only vertically or horizontally connecting centers of cells without obstacles. Lines cannot run on a cell with an obstacle. Only one line can run on a cell at most once. Hence, a line cannot intersect with the other line, nor with itself. Under these constraints, the total length of the two lines should be minimized. The length of a line is defined as the number of cell borders it passes. In particular, a line connecting cells sharing their border has length 1. Fig. 6(a) shows an example setting. Fig. 6(b) shows two lines satisfying the constraints above with minimum total length 18. Figure 6: An example setting and its solution Input The input consists of multiple datasets, each in the following format. n m row 1 ... row n n is the number of rows which satisfies 2 ≤ n ≤ 9. m is the number of columns which satisfies 2 ≤ m ≤ 9. Each row i is a sequence of m digits separated by a space. The digits mean the following. 0 : Empty 1 : Occupied by an obstacle 2 : Marked with "2" 3 : Marked with "3" The end of the input is indicated with a line containing two zeros separated by a space. Output For each dataset, one line containing the minimum total length of the two lines should be output. If there is no pair of lines satisfying the requirement, answer "0" instead. No other characters should be contained in the output. Sample Input 5 5 0 0 0 0 0 0 0 0 3 0 2 0 2 0 0 1 0 1 1 1 0 0 0 0 3 2 3 2 2 0 0 3 3 6 5 2 0 0 0 0 0 3 0 0 0 0 0 0 0 0 1 1 1 0 0 0 0 0 0 0 0 0 2 3 0 5 9 0 0 0 0 0 0 0 0 0 0 0 0 0 3 0 0 0 0 0 2 0 0 0 0 0 2 0 0 0 0 0 3 0 0 0 0 0 0 0 0 0 0 0 0 0 9 9 3 0 0 0 0 0 0 0 2 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 2 0 0 0 0 0 0 0 3 9 9 0 0 0 1 0 0 0 0 0 0 2 0 1 0 0 0 0 3 0 0 0 1 0 0 0 0 2 0 0 0 1 0 0 0 0 3 0 0 0 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 9 9 0 0 0 0 0 0 0 0 0 0 3 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 2 3 2 0 0 Output for the Sample Input 18 2 17 12 0 52 43
[ { "submission_id": "aoj_1270_10865943", "code_snippet": "#include <bits/stdc++.h>\ntypedef unsigned int T;\nT n, m;\nT ns[10][10];\nbool vis[10][10][70000];\nT ans[10][10][70000];\nT san[] = {\n\t1, 3, 9, 27, 81, 243, 729, 2187, 6561, 19683, 59049\n};\n\nT f(T x, T y, T S) {\n\tif (x >= n) {\n\t\tif (S) return 100;\n\t\treturn 0;\n\t}\n\n\tT &tmp = ans[x][y][S];\n\tif (vis[x][y][S]) return tmp;\n\tvis[x][y][S] = 1;\n\n\tif (y == m) {\n\t\tif (S / san[m]) return tmp = 100;\n\t\treturn tmp = f(x + 1, 0, S * 3);\n\t}\n\n\tT a = S % san[y + 1] / san[y];\n\tT b = S % san[y + 2] / san[y + 1];\n\n\tswitch (ns[x][y]) {\n\t\tcase 2:\n\t\tcase 3:\n\t\t\tif (a == ns[x][y] - 1 && b == 0) return tmp = f(x, y + 1, S - a * san[y]) + 1;\n\t\t\tif (a == 0 && b == ns[x][y] - 1) return tmp = f(x, y + 1, S - b * san[y + 1]) + 1;\n\t\t\tif (a == 0 && b == 0)\n\t\t\t\treturn tmp = std::min(f(x, y + 1, S + (ns[x][y] - 1) * san[y]),\n\t\t\t\t f(x, y + 1, S + (ns[x][y] - 1) * san[y + 1])) + 1;\n\t\t\treturn tmp = 100;\n\t\tcase 1:\n\t\t\tif (a || b) return tmp = 100;\n\t\t\treturn tmp = f(x, y + 1, S);\n\t\tcase 0:\n\t\t\tif (a && b && a == b)\n\t\t\t\treturn tmp = f(x, y + 1, S - a * san[y] - b * san[y + 1]) + 1;\n\t\t\tif (bool(a)^bool(b))\n\t\t\t\treturn tmp = std::min(f(x, y + 1, S - a * san[y] - b * san[y + 1] + a * san[y] + b * san[y]),\n\t\t\t\t f(x, y + 1, S - a * san[y] - b * san[y + 1] + a * san[y + 1] + b * san[y + 1])) + 1;\n\t\t\tif (a == 0 && b == 0)\n\t\t\t\treturn tmp = std::min(std::min(f(x, y + 1, S + 2 * san[y] + 2 * san[y + 1]),\n\t\t\t\t f(x, y + 1, S + 1 * san[y] + 1 * san[y + 1])) + 1,\n\t\t\t\t f(x, y + 1, S));\n\t\t\treturn tmp = 100;\n\t\tdefault:\n\t\t\texit(-1);\n\t}\n}\n\nint main() {\n\twhile (scanf(\"%u%u\", &n, &m) && !(n == m && n == 0)) {\n\t\tmemset(vis, 0, sizeof vis);\n\t\tfor (T i = 0; i < n; ++i)\n\t\t\tfor (T j = 0; j < m; ++j)\n\t\t\t\tscanf(\"%u\", &ns[i][j]);\n\n\t\tprintf(\"%u\\n\", f(0, 0, 0) >= 100 ? 0 : (f(0, 0, 0) - 2));\n\t}\n}", "accuracy": 1, "time_ms": 90, "memory_kb": 33384, "score_of_the_acc": -1, "final_rank": 12 }, { "submission_id": "aoj_1270_9740578", "code_snippet": "// 2006 Yokohama: Manhattan Wiring\n// POJ 3133\n// ACM-ICPC Live Archive 3620\n#include <cstdio>\n#include <cstring>\n#include <algorithm>\nusing namespace std;\n#define N 9\nconst int INF = 1 << 20;\nconst int MAX_STATE = 59049 + 1;\nint n, m, g[N][N];\nint best[N][N][MAX_STATE];\n\nstruct STATE {\n\tint up[N], left;\n\tSTATE() : left(0) {}\n\n\t// Encode the status to a integer. It's used in querying the best array.\n\tint encode() {\n\t\tint code = left;\n\t\tfor (int i = 0; i < m; i++)\n\t\t\tcode = code * 3 + up[i];\n\t\treturn code;\n\t}\n\n\t// Determine whether the next status is legal. If legal, then generate the new status.\n\tbool generateNext(int row, int col, int up, int down, int left, int right, STATE &newState) {\n\t\t// It's illegal to exceed the boundary.\n\t\tif (col == 0 && left || row == 0 && up || col == m - 1 && right || row == n - 1 && down)\n\t\t\treturn false;\n\t\t// The plug-ins must match\n\t\tif (left != this->left || up != this->up[col])\n\t\t\treturn false;\n\n\t\t// The only changed plug-in is the left one and up[col] one.\n\t\tfor (int i = 0; i < m; i++)\n\t\t\tnewState.up[i] = this->up[i];\n\t\tnewState.up[col] = down;\n\t\tnewState.left = right;\n\t\treturn true;\n\t}\n};\n\nint calc(int x, int y, STATE &state) {\n\tif (y == m)\n\t\tx++, y = 0;\n\tif (x == n)\n\t\treturn 0;\n\n\tint key = state.encode();\n\tif (best[x][y][key] != -1)\n\t\treturn best[x][y][key];\n\n\tSTATE newState;\n\tint ans = INF;\n\n\tif (g[x][y] == 0) {\n\t\t// Empty cell, we can try all combination : no plug-in or plug-ins with two ends.\n\t\tif (state.generateNext(x, y, 0, 0, 0, 0, newState))\n\t\t\tans = min(ans, calc(x, y + 1, newState));\n\t\tfor (int t = 1; t < 3; t++) {\n\t\t\tif (state.generateNext(x, y, 0, 0, t, t, newState))\n\t\t\t\tans = min(ans, calc(x, y + 1, newState) + 2);\n\t\t\tif (state.generateNext(x, y, t, t, 0, 0, newState))\n\t\t\t\tans = min(ans, calc(x, y + 1, newState) + 2);\n\t\t\tif (state.generateNext(x, y, t, 0, t, 0, newState))\n\t\t\t\tans = min(ans, calc(x, y + 1, newState) + 2);\n\t\t\tif (state.generateNext(x, y, t, 0, 0, t, newState))\n\t\t\t\tans = min(ans, calc(x, y + 1, newState) + 2);\n\t\t\tif (state.generateNext(x, y, 0, t, 0, t, newState))\n\t\t\t\tans = min(ans, calc(x, y + 1, newState) + 2);\n\t\t\tif (state.generateNext(x, y, 0, t, t, 0, newState))\n\t\t\t\tans = min(ans, calc(x, y + 1, newState) + 2);\n\t\t}\n\t} else if (g[x][y] == 1) {\n\t\tif (state.generateNext(x, y, 0, 0, 0, 0, newState))\n\t\t\tans = min(ans, calc(x, y + 1, newState));\n\t} else if (g[x][y] >= 2) {\n\t\t// The cell which is the endpoint of the polyline. We can only try the plug-ins with one end\n\t\tif (state.generateNext(x, y, g[x][y] - 1, 0, 0, 0, newState))\n\t\t\tans = min(ans, calc(x, y + 1, newState) + 1);\n\t\tif (state.generateNext(x, y, 0, g[x][y] - 1, 0, 0, newState))\n\t\t\tans = min(ans, calc(x, y + 1, newState) + 1);\n\t\tif (state.generateNext(x, y, 0, 0, g[x][y] - 1, 0, newState))\n\t\t\tans = min(ans, calc(x, y + 1, newState) + 1);\n\t\tif (state.generateNext(x, y, 0, 0, 0, g[x][y] - 1, newState))\n\t\t\tans = min(ans, calc(x, y + 1, newState) + 1);\n\t}\n\treturn best[x][y][key] = ans;\n}\n\nint main() {\n\twhile (scanf(\"%d%d\", &n, &m) && n) {\n\t\tfor (int i = 0; i < n; i++)\n\t\t\tfor (int j = 0; j < m; j++)\n\t\t\t\tscanf(\"%d\", &g[i][j]);\n\n\t\tSTATE s;\n\t\tmemset(best, -1, sizeof(best));\n\t\tmemset(s.up, 0, sizeof(s.up));\n\t\tint ans = calc(0, 0, s);\n\t\tif (ans == INF)\n\t\t\tans = 0;\n\t\tprintf(\"%d\\n\", ans / 2);\n\n\t}\n\treturn 0;\n}", "accuracy": 1, "time_ms": 240, "memory_kb": 21320, "score_of_the_acc": -0.6816, "final_rank": 9 }, { "submission_id": "aoj_1270_1351574", "code_snippet": "#include<bits/stdc++.h>\n\n#define REP(i,s,n) for(int i=s;i<n;i++)\n#define rep(i,n) REP(i,0,n)\n\nusing namespace std;\n\nconst int IINF = INT_MAX;\nconst int MAX_W = 9;\ntypedef pair<int,int> ii;\n\n\nint h,w;\nint field[10][10],indice[10][10];\nint dp[2][1<<((MAX_W+1)*2)];\nint dx[] = {1,0,-1,0};\nint dy[] = {0,1,0,-1};\n\ninline bool isValid(int x,int y) { return 0 <= x && x < w && 0 <= y && y < h; }\n\n#define SET(bitmask,index,value) ( bitmask = ( ( bitmask & ~(1<<(2*index)) & ~(1<<(2*index+1)) ) | ( value << (index*2) ) ) )\n#define GET(bitmask,x) ( ( ( bitmask >> ( 2 * x ) ) & 1 ) | ( ( ( bitmask >> (2*x+1) ) & 1) << 1 ) )\n#define FIX(bitmask) ((bitmask<<2)&((1<<(2*(w+1)))-1))\n#define STAR ( field[y][x] == 2 || field[y][x] == 3 )\n#define update(x,v) ( ( x == -1 ) ? ( x = v ) : ( x = min(x,v) ) )\n\n\nvoid compute(){\n\n vector<int> state;\n rep(bitmask,(1<<((w+1)*2))) {\n bool success = true;\n for(int i=0;i<(w+1)*2;i+=2) if( ( ( bitmask >> i ) & 1 ) && ( ( bitmask >> (i+1) ) & 1 ) ) { success = false; break; }\n if( success ) state.push_back(bitmask);\n }\n memset(dp,-1,sizeof(dp));\n\n int _size = state.size();\n bool initter = true, phase = false;\n int mini = IINF;\n int encounter = 0;\n rep(y,h){\n rep(x,w){\n\n if( field[y][x] == 2 || field[y][x] == 3 ) ++encounter;\n if( initter ) dp[phase][0] = 0;\n\n rep(i,_size){\n int bitmask = state[i];\n\n if( dp[phase][bitmask] == -1 ) continue;\n if( dp[phase][bitmask] >= mini ) continue;\n\n // ?????????\n if( field[y][x] == 1 ) {\n int nbitmask = bitmask;\n SET(nbitmask,x,0);\n if( x == w-1 ) nbitmask = FIX(nbitmask);\n update(dp[!phase][nbitmask],dp[phase][bitmask]);\n continue;\n }\n\n int nbitmask;\n // 0 0\n if( !( ( GET(bitmask,x) == 0 ) && ( GET(bitmask,(x+1)) == 0 ) ) ) goto Label1;\n if( STAR ) {\n // x 0\n if( y+1 < h && field[y+1][x] != 1 ) {\n nbitmask = bitmask;\n SET(nbitmask,x,((field[y][x]==3)+1));\n if( x == w-1 ) nbitmask = FIX(nbitmask);\n \n update(dp[!phase][nbitmask],dp[phase][bitmask]+1);\n }\n // 0 x\n if( x+1 < w && field[y][x+1] != 1 ) {\n nbitmask = bitmask;\n SET(nbitmask,(x+1),((field[y][x]==3)+1));\n\n if( x == w-1 ) nbitmask = FIX(nbitmask);\n\n update(dp[!phase][nbitmask],dp[phase][bitmask]+1);\n }\n } else {\n // x x\n if( x+1 < w && y+1 < h && field[y][x+1] != 1 && field[y+1][x] != 1 ) {\n REP(color,1,3){\n nbitmask = bitmask;\n SET(nbitmask,x,color);\n SET(nbitmask,(x+1),color);\n if( x == w-1 ) nbitmask = FIX(nbitmask);\n update(dp[!phase][nbitmask],(dp[phase][bitmask]+1));\n }\n }\n // 0 0\n nbitmask = bitmask;\n SET(nbitmask,x,0);\n SET(nbitmask,(x+1),0);\n if( x == w-1 ) nbitmask = FIX(nbitmask);\n update(dp[!phase][nbitmask],dp[phase][bitmask]);\n }\n\n continue;\n Label1:;\n // x 0\n // 0 x\n int color;\n if( !( ( GET(bitmask,x) && ( GET(bitmask,(x+1)) == 0 ) ) || ( GET(bitmask,(x+1)) && ( GET(bitmask,x) == 0 ) ) ) ) goto Label2;\n color = GET(bitmask,x) | GET(bitmask,(x+1));\n if( STAR ) {\n // 0 0\n if( ((color==1)?2:3) == field[y][x] ) {\n nbitmask = bitmask;\n SET(nbitmask,x,0);\n SET(nbitmask,(x+1),0);\n if( x == w-1 ) nbitmask = FIX(nbitmask);\n update(dp[!phase][nbitmask],dp[phase][bitmask]+1);\n }\n } else {\n // x 0\n if( y+1 < h && field[y+1][x] != 1 ) {\n nbitmask = bitmask;\n SET(nbitmask,x,color);\n SET(nbitmask,(x+1),0);\n if( x == w-1 ) nbitmask = FIX(nbitmask);\n\n\n update(dp[!phase][nbitmask],(dp[phase][bitmask]+1));\n }\n \n // 0 x\n if( x+1 < w && field[y][x+1] != 1 ) {\n nbitmask = bitmask;\n SET(nbitmask,x,0);\n SET(nbitmask,(x+1),color);\n\n if( x == w-1 ) nbitmask = FIX(nbitmask);\n update(dp[!phase][nbitmask],(dp[phase][bitmask]+1));\n }\n }\n continue;\n Label2:;\n // x x\n if( !( GET(bitmask,x) && GET(bitmask,(x+1)) ) ) goto Label3;\n if( GET(bitmask,x) != GET(bitmask,(x+1)) ) goto Label3;\n\n color = GET(bitmask,x);\n \n // 0 0\n if( !STAR ) {\n nbitmask = bitmask;\n SET(nbitmask,x,0);\n SET(nbitmask,(x+1),0);\n if( x == w-1 ) nbitmask = FIX(nbitmask);\n update(dp[!phase][nbitmask],dp[phase][bitmask]+1);\n }\n Label3:;\n\n }\n if( field[y][x] == 2 || field[y][x] == 3 ) initter = false;\n rep(i,_size) dp[phase][state[i]] = -1;\n if( encounter >= 4 && dp[!phase][0] != -1 ) {\n mini = min(mini,dp[!phase][0]);\n }\n phase = !phase;\n }\n }\n if( dp[phase][0] != -1 ) mini = min(mini,dp[phase][0]);\n if( mini != IINF ) printf(\"%d\\n\",mini-2);\n else puts(\"0\");\n}\n\nint main(){\n while( scanf(\"%d %d\",&h,&w), h|w ){\n rep(i,h) rep(j,w) scanf(\"%d\",&field[i][j]);\n compute();\n }\n return 0;\n}", "accuracy": 1, "time_ms": 410, "memory_kb": 9780, "score_of_the_acc": -0.3864, "final_rank": 4 }, { "submission_id": "aoj_1270_1351569", "code_snippet": "#include<bits/stdc++.h>\n\n#define REP(i,s,n) for(int i=s;i<n;i++)\n#define rep(i,n) REP(i,0,n)\n\nusing namespace std;\n\nconst int IINF = INT_MAX;\nconst int MAX_W = 9;\ntypedef pair<int,int> ii;\n\n\nint h,w;\nint field[10][10],indice[10][10];\nint dp[2][1<<((MAX_W+1)*2)];\nint dx[] = {1,0,-1,0};\nint dy[] = {0,1,0,-1};\n\ninline bool isValid(int x,int y) { return 0 <= x && x < w && 0 <= y && y < h; }\n\n#define SET(bitmask,index,value) ( bitmask = ( ( bitmask & ~(1<<(2*index)) & ~(1<<(2*index+1)) ) | ( value << (index*2) ) ) )\n#define GET(bitmask,x) ( ( ( bitmask >> ( 2 * x ) ) & 1 ) | ( ( ( bitmask >> (2*x+1) ) & 1) << 1 ) )\n#define FIX(bitmask) ((bitmask<<2)&((1<<(2*(w+1)))-1))\n#define STAR ( field[y][x] == 2 || field[y][x] == 3 )\n#define update(x,v) ( ( x == -1 ) ? ( x = v ) : ( x = min(x,v) ) )\n\n\nvoid compute(){\n\n vector<int> state;\n rep(bitmask,(1<<((w+1)*2))) {\n bool success = true;\n for(int i=0;i<(w+1)*2;i+=2) if( ( ( bitmask >> i ) & 1 ) && ( ( bitmask >> (i+1) ) & 1 ) ) { success = false; break; }\n if( success ) state.push_back(bitmask);\n }\n memset(dp,-1,sizeof(dp));\n\n int _size = state.size();\n bool initter = true, phase = false;\n int mini = IINF;\n int encounter = 0;\n rep(y,h){\n rep(x,w){\n\n if( field[y][x] == 2 || field[y][x] == 3 ) ++encounter;\n if( initter ) dp[phase][0] = 0;\n\n rep(i,_size){\n int bitmask = state[i];\n\n if( dp[phase][bitmask] == -1 ) continue;\n\n // ?????????\n if( field[y][x] == 1 ) {\n int nbitmask = bitmask;\n SET(nbitmask,x,0);\n if( x == w-1 ) nbitmask = FIX(nbitmask);\n update(dp[!phase][nbitmask],dp[phase][bitmask]);\n continue;\n }\n\n int nbitmask;\n // 0 0\n if( !( ( GET(bitmask,x) == 0 ) && ( GET(bitmask,(x+1)) == 0 ) ) ) goto Label1;\n if( STAR ) {\n // x 0\n if( y+1 < h && field[y+1][x] != 1 ) {\n nbitmask = bitmask;\n SET(nbitmask,x,((field[y][x]==3)+1));\n if( x == w-1 ) nbitmask = FIX(nbitmask);\n \n update(dp[!phase][nbitmask],dp[phase][bitmask]+1);\n }\n // 0 x\n if( x+1 < w && field[y][x+1] != 1 ) {\n nbitmask = bitmask;\n SET(nbitmask,(x+1),((field[y][x]==3)+1));\n\n if( x == w-1 ) nbitmask = FIX(nbitmask);\n\n update(dp[!phase][nbitmask],dp[phase][bitmask]+1);\n }\n } else {\n // x x\n if( x+1 < w && y+1 < h && field[y][x+1] != 1 && field[y+1][x] != 1 ) {\n REP(color,1,3){\n nbitmask = bitmask;\n SET(nbitmask,x,color);\n SET(nbitmask,(x+1),color);\n if( x == w-1 ) nbitmask = FIX(nbitmask);\n update(dp[!phase][nbitmask],(dp[phase][bitmask]+1));\n }\n }\n // 0 0\n nbitmask = bitmask;\n SET(nbitmask,x,0);\n SET(nbitmask,(x+1),0);\n if( x == w-1 ) nbitmask = FIX(nbitmask);\n update(dp[!phase][nbitmask],dp[phase][bitmask]);\n }\n\n continue;\n Label1:;\n // x 0\n // 0 x\n int color;\n if( !( ( GET(bitmask,x) && ( GET(bitmask,(x+1)) == 0 ) ) || ( GET(bitmask,(x+1)) && ( GET(bitmask,x) == 0 ) ) ) ) goto Label2;\n color = GET(bitmask,x) | GET(bitmask,(x+1));\n if( STAR ) {\n // 0 0\n if( ((color==1)?2:3) == field[y][x] ) {\n nbitmask = bitmask;\n SET(nbitmask,x,0);\n SET(nbitmask,(x+1),0);\n if( x == w-1 ) nbitmask = FIX(nbitmask);\n update(dp[!phase][nbitmask],dp[phase][bitmask]+1);\n }\n } else {\n // x 0\n if( y+1 < h && field[y+1][x] != 1 ) {\n nbitmask = bitmask;\n SET(nbitmask,x,color);\n SET(nbitmask,(x+1),0);\n if( x == w-1 ) nbitmask = FIX(nbitmask);\n\n\n update(dp[!phase][nbitmask],(dp[phase][bitmask]+1));\n }\n \n // 0 x\n if( x+1 < w && field[y][x+1] != 1 ) {\n nbitmask = bitmask;\n SET(nbitmask,x,0);\n SET(nbitmask,(x+1),color);\n\n if( x == w-1 ) nbitmask = FIX(nbitmask);\n update(dp[!phase][nbitmask],(dp[phase][bitmask]+1));\n }\n }\n continue;\n Label2:;\n // x x\n if( !( GET(bitmask,x) && GET(bitmask,(x+1)) ) ) goto Label3;\n if( GET(bitmask,x) != GET(bitmask,(x+1)) ) goto Label3;\n\n color = GET(bitmask,x);\n \n // 0 0\n if( !STAR ) {\n nbitmask = bitmask;\n SET(nbitmask,x,0);\n SET(nbitmask,(x+1),0);\n if( x == w-1 ) nbitmask = FIX(nbitmask);\n update(dp[!phase][nbitmask],dp[phase][bitmask]+1);\n }\n Label3:;\n\n }\n if( field[y][x] == 2 || field[y][x] == 3 ) initter = false;\n rep(i,_size) dp[phase][state[i]] = -1;\n if( encounter >= 4 && dp[!phase][0] != -1 ) {\n mini = min(mini,dp[!phase][0]);\n }\n phase = !phase;\n }\n }\n if( dp[phase][0] != -1 ) mini = min(mini,dp[phase][0]);\n if( mini != IINF ) printf(\"%d\\n\",mini-2);\n else puts(\"0\");\n}\n\nint main(){\n while( scanf(\"%d %d\",&h,&w), h|w ){\n rep(i,h) rep(j,w) scanf(\"%d\",&field[i][j]);\n compute();\n }\n return 0;\n}", "accuracy": 1, "time_ms": 420, "memory_kb": 9780, "score_of_the_acc": -0.3899, "final_rank": 5 }, { "submission_id": "aoj_1270_1351568", "code_snippet": "#include<bits/stdc++.h>\n\n#define REP(i,s,n) for(int i=s;i<n;i++)\n#define rep(i,n) REP(i,0,n)\n\nusing namespace std;\n\nconst int IINF = INT_MAX;\nconst int MAX_W = 9;\ntypedef pair<int,int> ii;\n\n\nint h,w;\nint field[10][10],indice[10][10];\nint dp[2][1<<((MAX_W+1)*2)];\nint dx[] = {1,0,-1,0};\nint dy[] = {0,1,0,-1};\n\ninline bool isValid(int x,int y) { return 0 <= x && x < w && 0 <= y && y < h; }\n\n#define SET(bitmask,index,value) ( bitmask = ( ( bitmask & ~(1<<(2*index)) & ~(1<<(2*index+1)) ) | ( value << (index*2) ) ) )\n#define GET(bitmask,x) ( ( ( bitmask >> ( 2 * x ) ) & 1 ) | ( ( ( bitmask >> (2*x+1) ) & 1) << 1 ) )\n#define FIX(bitmask) ((bitmask<<2)&((1<<(2*(w+1)))-1))\n#define STAR ( field[y][x] == 2 || field[y][x] == 3 )\n#define update(x,v) ( ( x == -1 ) ? ( x = v ) : ( x = min(x,v) ) )\n\n\nvoid compute(){\n\n set<int> S;\n vector<int> state;\n rep(bitmask,(1<<((w+1)*2))) {\n bool success = true;\n for(int i=0;i<(w+1)*2;i+=2){\n if( ( ( bitmask >> i ) & 1 ) && ( ( bitmask >> (i+1) ) & 1 ) ) { success = false; break; }\n }\n if( success ) state.push_back(bitmask), S.insert(bitmask);\n }\n memset(dp,-1,sizeof(dp));\n\n int _size = state.size();\n bool initter = true, phase = false;\n int mini = IINF;\n int encounter = 0;\n rep(y,h){\n rep(x,w){\n /*\n cout << x << \",\" << y << endl;\n rep(i,_size){\n if( dp[phase][state[i]] != -1 ) {\n bitset<12> BIT(state[i]);\n for(int j=0;j<12;j+=2){\n cout << BIT[j] << BIT[j+1] << \" \";\n } \n cout << \" = \" << dp[phase][state[i]] << endl;\n }\n }\n */\n if( field[y][x] == 2 || field[y][x] == 3 ) ++encounter;\n if( initter ) dp[phase][0] = 0;\n\n rep(i,_size){\n int bitmask = state[i];\n\n if( dp[phase][bitmask] == -1 ) continue;\n bitset<12> bit(bitmask);\n\n\n assert( S.count(bitmask));\n // ?????????\n if( field[y][x] == 1 ) {\n int nbitmask = bitmask;\n SET(nbitmask,x,0);\n if( x == w-1 ) nbitmask = FIX(nbitmask);\n update(dp[!phase][nbitmask],dp[phase][bitmask]);\n continue;\n }\n\n int nbitmask;\n // 0 0\n if( !( ( GET(bitmask,x) == 0 ) && ( GET(bitmask,(x+1)) == 0 ) ) ) goto Label1;\n if( STAR ) {\n // x 0\n if( y+1 < h && field[y+1][x] != 1 ) {\n nbitmask = bitmask;\n SET(nbitmask,x,((field[y][x]==3)+1));\n if( x == w-1 ) nbitmask = FIX(nbitmask);\n\n update(dp[!phase][nbitmask],dp[phase][bitmask]+1);\n }\n // 0 x\n if( x+1 < w && field[y][x+1] != 1 ) {\n nbitmask = bitmask;\n SET(nbitmask,(x+1),((field[y][x]==3)+1));\n\n if( x == w-1 ) nbitmask = FIX(nbitmask);\n\n update(dp[!phase][nbitmask],dp[phase][bitmask]+1);\n }\n } else {\n // x x\n if( x+1 < w && y+1 < h && field[y][x+1] != 1 && field[y+1][x] != 1 ) {\n REP(color,1,3){\n nbitmask = bitmask;\n SET(nbitmask,x,color);\n SET(nbitmask,(x+1),color);\n if( x == w-1 ) nbitmask = FIX(nbitmask);\n update(dp[!phase][nbitmask],(dp[phase][bitmask]+1));\n }\n }\n // 0 0\n nbitmask = bitmask;\n SET(nbitmask,x,0);\n SET(nbitmask,(x+1),0);\n if( x == w-1 ) nbitmask = FIX(nbitmask);\n update(dp[!phase][nbitmask],dp[phase][bitmask]);\n }\n\n continue;\n Label1:;\n // x 0\n // 0 x\n int color;\n if( !( ( GET(bitmask,x) && ( GET(bitmask,(x+1)) == 0 ) ) || ( GET(bitmask,(x+1)) && ( GET(bitmask,x) == 0 ) ) ) ) goto Label2;\n color = GET(bitmask,x) | GET(bitmask,(x+1));\n assert( color == 1 || color == 2 );\n if( STAR ) {\n // 0 0\n if( ((color==1)?2:3) == field[y][x] ) {\n\n nbitmask = bitmask;\n SET(nbitmask,x,0);\n SET(nbitmask,(x+1),0);\n if( x == w-1 ) nbitmask = FIX(nbitmask);\n update(dp[!phase][nbitmask],dp[phase][bitmask]+1);\n }\n } else {\n // x 0\n if( y+1 < h && field[y+1][x] != 1 ) {\n nbitmask = bitmask;\n SET(nbitmask,x,color);\n SET(nbitmask,(x+1),0);\n if( x == w-1 ) nbitmask = FIX(nbitmask);\n\n\n update(dp[!phase][nbitmask],(dp[phase][bitmask]+1));\n }\n \n // 0 x\n if( x+1 < w && field[y][x+1] != 1 ) {\n nbitmask = bitmask;\n SET(nbitmask,x,0);\n SET(nbitmask,(x+1),color);\n\n if( x == w-1 ) nbitmask = FIX(nbitmask);\n update(dp[!phase][nbitmask],(dp[phase][bitmask]+1));\n }\n }\n continue;\n Label2:;\n // x x\n if( !( GET(bitmask,x) && GET(bitmask,(x+1)) ) ) goto Label3;\n if( GET(bitmask,x) != GET(bitmask,(x+1)) ) goto Label3;\n\n color = GET(bitmask,x);\n assert( color );\n //assert( GET(bitmask,x) == GET(bitmask,(x+1)) );\n \n // 0 0\n if( !STAR ) {\n nbitmask = bitmask;\n SET(nbitmask,x,0);\n SET(nbitmask,(x+1),0);\n if( x == w-1 ) nbitmask = FIX(nbitmask);\n update(dp[!phase][nbitmask],dp[phase][bitmask]+1);\n }\n Label3:;\n\n }\n if( field[y][x] == 2 || field[y][x] == 3 ) initter = false;\n rep(i,_size) dp[phase][state[i]] = -1;\n if( encounter >= 4 && dp[!phase][0] != -1 ) {\n mini = min(mini,dp[!phase][0]);\n }\n phase = !phase;\n }\n }\n\n /*\n puts(\"final\");\n rep(i,_size){\n if( dp[phase][state[i]] != -1 ) {\n bitset<12> BIT(state[i]);\n for(int j=0;j<12;j+=2){\n cout << BIT[j] << BIT[j+1] << \" \";\n } \n cout << \" = \" << dp[phase][state[i]] << endl;\n }\n }\n */\n if( dp[phase][0] != -1 ) mini = min(mini,dp[phase][0]);\n if( mini != IINF ) printf(\"%d\\n\",mini-2);\n else puts(\"0\");\n}\n\nint main(){\n\n\n while( scanf(\"%d %d\",&h,&w), h|w ){\n rep(i,h) rep(j,w) scanf(\"%d\",&field[i][j]);\n compute();\n }\n return 0;\n}", "accuracy": 1, "time_ms": 920, "memory_kb": 12164, "score_of_the_acc": -0.6386, "final_rank": 8 }, { "submission_id": "aoj_1270_556905", "code_snippet": "#include<cstdio>\n#include<cstring>\n\n#define rep(i,n) for(int i=0;i<(n);i++)\n\nusing namespace std;\n\nconst int dx[]={1,0,-1,0},dy[]={0,-1,0,1};\n\nint h,w,B[9][9];\n\nint x2,y2,x3,y3; // 2, 3 それぞれの出発点\n\nint bfs(int x,int y,int tar){\n\tint d[9][9];\n\tmemset(d,-1,sizeof d);\n\td[y][x]=0;\n\tint Q[81],head=0,tail=0; Q[tail++]=y*w+x;\n\twhile(head<tail){\n\t\tint i=Q[head]/w,j=Q[head]%w; head++;\n\t\trep(k,4){\n\t\t\tint yy=i+dy[k],xx=j+dx[k];\n\t\t\tif(0<=yy && yy<h && 0<=xx && xx<w && d[yy][xx]==-1){\n\t\t\t\tif(B[yy][xx]==tar) return d[i][j]+1;\n\t\t\t\tif(B[yy][xx]== 0 ) Q[tail++]=yy*w+xx, d[yy][xx]=d[i][j]+1;\n\t\t\t}\n\t\t}\n\t}\n\treturn 777;\n}\n\nint ans;\nvoid dfs(int x,int y,int now){\n\tint hstar=bfs(x,y,2)+bfs(x3,y3,3);\n\tif(ans<now+1+hstar) return;\n\n\t// これまでのパスと接したら枝刈り\n\tint cnt=0;\n\trep(k,4){\n\t\tint xx=x+dx[k],yy=y+dy[k];\n\t\tif(0<=xx && xx<w && 0<=yy && yy<h && B[yy][xx]==7) cnt++;\n\t}\n\tif(cnt>=2) return;\n\n\t// 777...777\n\t// 700...007\n\t// ?00...00?\n\t// というパターン ( or 回転/反転して一致するもの ) があれば枝刈り\n\tfor(int L=3;L<=8;L++){\n\t\tbool ng;\n\t\tif(y>0 && x+L-1<w){\n\t\t\tng=true;\n\t\t\tif(ng) rep(j,L) if(B[y-1][x+j]!=7) { ng=false; break; }\n\t\t\tif(ng) rep(j,L) if(B[y ][x+j]!=(j==0||j==L-1?7:0)) { ng=false; break; }\n\t\t\tif(ng && L>=5 && y+1<h) rep(j,L) if(0<j && j<L-1 && B[y+1][x+j]!=0) { ng=false; break; }\n\t\t\tif(ng) return;\n\t\t}\n\t\tif(y+1<h && x+L-1<w){\n\t\t\tng=true;\n\t\t\tif(ng && L>=5 && y>0) rep(j,L) if(0<j && j<L-1 && B[y-1][x+j]!=0) { ng=false; break; }\n\t\t\tif(ng) rep(j,L) if(B[y ][x+j]!=(j==0||j==L-1?7:0)) { ng=false; break; }\n\t\t\tif(ng) rep(j,L) if(B[y+1][x+j]!=7) { ng=false; break; }\n\t\t\tif(ng) return;\n\t\t}\n\t\tif(y>0 && x-L+1>=0){\n\t\t\tng=true;\n\t\t\tif(ng) rep(j,L) if(B[y-1][x-j]!=7) { ng=false; break; }\n\t\t\tif(ng) rep(j,L) if(B[y ][x-j]!=(j==0||j==L-1?7:0)) { ng=false; break; }\n\t\t\tif(ng && L>=5 && y+1<h) rep(j,L) if(0<j && j<L-1 && B[y+1][x-j]!=0) { ng=false; break; }\n\t\t\tif(ng) return;\n\t\t}\n\t\tif(y+1<h && x-L+1>=0){\n\t\t\tng=true;\n\t\t\tif(ng && L>=5 && y>0) rep(j,L) if(0<j && j<L-1 && B[y-1][x-j]!=0) { ng=false; break; }\n\t\t\tif(ng) rep(j,L) if(B[y ][x-j]!=(j==0||j==L-1?7:0)) { ng=false; break; }\n\t\t\tif(ng) rep(j,L) if(B[y+1][x-j]!=7) { ng=false; break; }\n\t\t\tif(ng) return;\n\t\t}\n\t\tif(y+L-1<h && x>0){\n\t\t\tng=true;\n\t\t\tif(ng) rep(i,L) if(B[y+i][x-1]!=7) { ng=false; break; }\n\t\t\tif(ng) rep(i,L) if(B[y+i][x ]!=(i==0||i==L-1?7:0)) { ng=false; break; }\n\t\t\tif(ng && L>=5 && x+1<w) rep(i,L) if(0<i && i<L-1 && B[y+i][x+1]!=0) { ng=false; break; }\n\t\t\tif(ng) return;\n\t\t}\n\t\tif(y+L-1<h && x+1<w){\n\t\t\tng=true;\n\t\t\tif(ng && L>=5 && x>0) rep(i,L) if(0<i && i<L-1 && B[y+i][x-1]!=0) { ng=false; break; }\n\t\t\tif(ng) rep(i,L) if(B[y+i][x ]!=(i==0||i==L-1?7:0)) { ng=false; break; }\n\t\t\tif(ng) rep(i,L) if(B[y+i][x+1]!=7) { ng=false; break; }\n\t\t\tif(ng) return;\n\t\t}\n\t\tif(y-L+1>=0 && x>0){\n\t\t\tng=true;\n\t\t\tif(ng) rep(i,L) if(B[y-i][x-1]!=7) { ng=false; break; }\n\t\t\tif(ng) rep(i,L) if(B[y-i][x ]!=(i==0||i==L-1?7:0)) { ng=false; break; }\n\t\t\tif(ng && L>=5 && x+1<w) rep(i,L) if(0<i && i<L-1 && B[y-i][x+1]!=0) { ng=false; break; }\n\t\t\tif(ng) return;\n\t\t}\n\t\tif(y-L+1>=0 && x+1<w){\n\t\t\tng=true;\n\t\t\tif(ng && L>=5 && x>0) rep(i,L) if(0<i && i<L-1 && B[y-i][x-1]!=0) { ng=false; break; }\n\t\t\tif(ng) rep(i,L) if(B[y-i][x ]!=(i==0||i==L-1?7:0)) { ng=false; break; }\n\t\t\tif(ng) rep(i,L) if(B[y-i][x+1]!=7) { ng=false; break; }\n\t\t\tif(ng) return;\n\t\t}\n\t}\n\n\tfor(int k=3;k>=0;k--){\n\t\tint xx=x+dx[k],yy=y+dy[k];\n\t\tif(0<=xx && xx<w && 0<=yy && yy<h){\n\t\t\tif(B[yy][xx]==2){\n\t\t\t\tans=now+1+bfs(x3,y3,3);\n\t\t\t\treturn;\n\t\t\t}\n\t\t\telse if(B[yy][xx]==0){\n\t\t\t\tB[yy][xx]=7;\n\t\t\t\tdfs(xx,yy,now+1);\n\t\t\t\tB[yy][xx]=0;\n\t\t\t}\n\t\t}\n\t}\n}\n\nint main(){\n\twhile(scanf(\"%d%d\",&h,&w),h){\n\t\trep(i,h) rep(j,w) scanf(\"%d\",B[i]+j);\n\n\t\trep(i,h) rep(j,w) {\n\t\t\tif(B[i][j]==2) x2=j, y2=i;\n\t\t\tif(B[i][j]==3) x3=j, y3=i;\n\t\t}\n\t\tB[y2][x2]=7; // 訪問済みのマーク\n\n\t\tans=777;\n\t\tdfs(x2,y2,0);\n\t\tprintf(\"%d\\n\",ans<777?ans:0);\n\t}\n\n\treturn 0;\n}", "accuracy": 1, "time_ms": 200, "memory_kb": 1032, "score_of_the_acc": -0.0436, "final_rank": 1 }, { "submission_id": "aoj_1270_556903", "code_snippet": "#include<cstdio>\n#include<cstring>\n\n#define rep(i,n) for(int i=0;i<(n);i++)\n\nusing namespace std;\n\nconst int dx[]={1,0,-1,0},dy[]={0,-1,0,1};\n\nint h,w,B[9][9];\n\nint x2,y2,x3,y3; // 2, 3 それぞれの出発点\n\nint bfs(int x,int y,int tar){\n\tint d[9][9];\n\tmemset(d,-1,sizeof d);\n\td[y][x]=0;\n\tint Q[81],head=0,tail=0; Q[tail++]=y*w+x;\n\twhile(head<tail){\n\t\tint i=Q[head]/w,j=Q[head]%w; head++;\n\t\trep(k,4){\n\t\t\tint yy=i+dy[k],xx=j+dx[k];\n\t\t\tif(0<=yy && yy<h && 0<=xx && xx<w && d[yy][xx]==-1){\n\t\t\t\tif(B[yy][xx]==tar) return d[i][j]+1;\n\t\t\t\tif(B[yy][xx]== 0 ) Q[tail++]=yy*w+xx, d[yy][xx]=d[i][j]+1;\n\t\t\t}\n\t\t}\n\t}\n\treturn 777;\n}\n\nint ans;\nvoid dfs(int x,int y,int now){\n\tint hstar=bfs(x,y,2)+bfs(x3,y3,3);\n\tif(ans<now+1+hstar) return;\n\n\t// これまでのパスと接したら枝刈り\n\tint cnt=0;\n\trep(k,4){\n\t\tint xx=x+dx[k],yy=y+dy[k];\n\t\tif(0<=xx && xx<w && 0<=yy && yy<h && B[yy][xx]==7) cnt++;\n\t}\n\tif(cnt>=2) return;\n\n\t// 777...777\n\t// 700...007\n\t// ?00...00?\n\t// というパターン ( or 回転/反転して一致するもの ) があれば枝刈り\n\tfor(int L=3;L<=8;L++){\n\t\tbool ng;\n\t\tif(y>0 && x+L-1<w){\n\t\t\tng=true;\n\t\t\tif(ng) rep(j,L) if(B[y-1][x+j]!=7) { ng=false; break; }\n\t\t\tif(ng) rep(j,L) if(B[y ][x+j]!=(j==0||j==L-1?7:0)) { ng=false; break; }\n\t\t\tif(ng && L>=5 && y+1<h) rep(j,L) if(0<j && j<L-1 && B[y+1][x+j]!=0) { ng=false; break; }\n\t\t\tif(ng) return;\n\t\t}\n\t\tif(y+1<h && x+L-1<w){\n\t\t\tng=true;\n\t\t\tif(ng && L>=5 && y>0) rep(j,L) if(0<j && j<L-1 && B[y-1][x+j]!=0) { ng=false; break; }\n\t\t\tif(ng) rep(j,L) if(B[y ][x+j]!=(j==0||j==L-1?7:0)) { ng=false; break; }\n\t\t\tif(ng) rep(j,L) if(B[y+1][x+j]!=7) { ng=false; break; }\n\t\t\tif(ng) return;\n\t\t}\n\t\tif(y>0 && x-L+1>=0){\n\t\t\tng=true;\n\t\t\tif(ng) rep(j,L) if(B[y-1][x-j]!=7) { ng=false; break; }\n\t\t\tif(ng) rep(j,L) if(B[y ][x-j]!=(j==0||j==L-1?7:0)) { ng=false; break; }\n\t\t\tif(ng && L>=5 && y+1<h) rep(j,L) if(0<j && j<L-1 && B[y+1][x-j]!=0) { ng=false; break; }\n\t\t\tif(ng) return;\n\t\t}\n\t\tif(y+1<h && x-L+1>=0){\n\t\t\tng=true;\n\t\t\tif(ng && L>=5 && y>0) rep(j,L) if(0<j && j<L-1 && B[y-1][x-j]!=0) { ng=false; break; }\n\t\t\tif(ng) rep(j,L) if(B[y ][x-j]!=(j==0||j==L-1?7:0)) { ng=false; break; }\n\t\t\tif(ng) rep(j,L) if(B[y+1][x-j]!=7) { ng=false; break; }\n\t\t\tif(ng) return;\n\t\t}\n\t\tif(y+L-1<h && x>0){\n\t\t\tng=true;\n\t\t\tif(ng) rep(i,L) if(B[y+i][x-1]!=7) { ng=false; break; }\n\t\t\tif(ng) rep(i,L) if(B[y+i][x ]!=(i==0||i==L-1?7:0)) { ng=false; break; }\n\t\t\tif(ng && L>=5 && x+1<w) rep(i,L) if(0<i && i<L-1 && B[y+i][x+1]!=0) { ng=false; break; }\n\t\t\tif(ng) return;\n\t\t}\n\t\tif(y+L-1<h && x+1<w){\n\t\t\tng=true;\n\t\t\tif(ng && L>=5 && x>0) rep(i,L) if(0<i && i<L-1 && B[y+i][x-1]!=0) { ng=false; break; }\n\t\t\tif(ng) rep(i,L) if(B[y+i][x ]!=(i==0||i==L-1?7:0)) { ng=false; break; }\n\t\t\tif(ng) rep(i,L) if(B[y+i][x+1]!=7) { ng=false; break; }\n\t\t\tif(ng) return;\n\t\t}\n\t\tif(y-L+1>=0 && x>0){\n\t\t\tng=true;\n\t\t\tif(ng) rep(i,L) if(B[y-i][x-1]!=7) { ng=false; break; }\n\t\t\tif(ng) rep(i,L) if(B[y-i][x ]!=(i==0||i==L-1?7:0)) { ng=false; break; }\n\t\t\tif(ng && L>=5 && x+1<w) rep(i,L) if(0<i && i<L-1 && B[y-i][x+1]!=0) { ng=false; break; }\n\t\t\tif(ng) return;\n\t\t}\n\t\tif(y-L+1>=0 && x+1<w){\n\t\t\tng=true;\n\t\t\tif(ng && L>=5 && x>0) rep(i,L) if(0<i && i<L-1 && B[y-i][x-1]!=0) { ng=false; break; }\n\t\t\tif(ng) rep(i,L) if(B[y-i][x ]!=(i==0||i==L-1?7:0)) { ng=false; break; }\n\t\t\tif(ng) rep(i,L) if(B[y-i][x+1]!=7) { ng=false; break; }\n\t\t\tif(ng) return;\n\t\t}\n\t}\n\n\trep(k,4){\n\t\tint xx=x+dx[k],yy=y+dy[k];\n\t\tif(0<=xx && xx<w && 0<=yy && yy<h){\n\t\t\tif(B[yy][xx]==2){\n\t\t\t\tans=now+1+bfs(x3,y3,3);\n\t\t\t\treturn;\n\t\t\t}\n\t\t\telse if(B[yy][xx]==0){\n\t\t\t\tB[yy][xx]=7;\n\t\t\t\tdfs(xx,yy,now+1);\n\t\t\t\tB[yy][xx]=0;\n\t\t\t}\n\t\t}\n\t}\n}\n\nint main(){\n\twhile(scanf(\"%d%d\",&h,&w),h){\n\t\trep(i,h) rep(j,w) scanf(\"%d\",B[i]+j);\n\n\t\trep(i,h) rep(j,w) {\n\t\t\tif(B[i][j]==2) x2=j, y2=i;\n\t\t\tif(B[i][j]==3) x3=j, y3=i;\n\t\t}\n\t\tB[y2][x2]=7; // 訪問済みのマーク\n\n\t\tans=777;\n\t\tdfs(x2,y2,0);\n\t\tprintf(\"%d\\n\",ans<777?ans:0);\n\t}\n\n\treturn 0;\n}", "accuracy": 1, "time_ms": 240, "memory_kb": 1032, "score_of_the_acc": -0.0577, "final_rank": 2 }, { "submission_id": "aoj_1270_556902", "code_snippet": "#include<cstdio>\n#include<cstring>\n\n#define rep(i,n) for(int i=0;i<(n);i++)\n\nusing namespace std;\n\nconst int dx[]={1,0,-1,0},dy[]={0,-1,0,1};\n\nint h,w,B[9][9];\n\nint x2,y2,x3,y3; // 2, 3 それぞれの出発点\n\nint bfs(int x,int y,int tar){\n\tint d[9][9];\n\tmemset(d,-1,sizeof d);\n\td[y][x]=0;\n\tint Q[81],head=0,tail=0; Q[tail++]=y*w+x;\n\twhile(head<tail){\n\t\tint i=Q[head]/w,j=Q[head]%w; head++;\n\t\trep(k,4){\n\t\t\tint yy=i+dy[k],xx=j+dx[k];\n\t\t\tif(0<=yy && yy<h && 0<=xx && xx<w && d[yy][xx]==-1){\n\t\t\t\tif(B[yy][xx]==tar) return d[i][j]+1;\n\t\t\t\tif(B[yy][xx]== 0 ) Q[tail++]=yy*w+xx, d[yy][xx]=d[i][j]+1;\n\t\t\t}\n\t\t}\n\t}\n\treturn 777;\n}\n\nint ans;\nvoid dfs(int x,int y,int now){\n\tint hstar=bfs(x,y,2)+bfs(x3,y3,3);\n\tif(ans<now+1+hstar) return;\n\n\t// これまでのパスと接したら枝刈り\n\tint cnt=0;\n\trep(k,4){\n\t\tint xx=x+dx[k],yy=y+dy[k];\n\t\tif(0<=xx && xx<w && 0<=yy && yy<h && B[yy][xx]==7) cnt++;\n\t}\n\tif(cnt>=2) return;\n\n\t// 777...777\n\t// 700...007\n\t// ?00...00?\n\t// というパターン ( or 回転して一致するもの ) があれば枝刈り\n\tfor(int L=3;L<=8;L++){\n\t\tbool ng;\n\t\tif(y>0 && x+L-1<w){\n\t\t\tng=true;\n\t\t\tif(ng) rep(j,L) if(B[y-1][x+j]!=7) { ng=false; break; }\n\t\t\tif(ng) rep(j,L) if(B[y ][x+j]!=(j==0||j==L-1?7:0)) { ng=false; break; }\n\t\t\tif(ng && L>=5 && y+1<h) rep(j,L) if(0<j && j<L-1 && B[y+1][x+j]!=0) { ng=false; break; }\n\t\t\tif(ng) return;\n\n\t\t\tng=true;\n\t\t\tif(ng && L>=5 && y-2>=0) rep(j,L) if(0<j && j<L-1 && B[y-2][x+j]!=0) { ng=false; break; }\n\t\t\tif(ng) rep(j,L) if(B[y-1][x+j]!=(j==0||j==L-1?7:0)) { ng=false; break; }\n\t\t\tif(ng) rep(j,L) if(B[y ][x+j]!=7) { ng=false; break; }\n\t\t\tif(ng) return;\n\t\t}\n\t\tif(y>0 && x-L+1>=0){\n\t\t\tng=true;\n\t\t\tif(ng) rep(j,L) if(B[y-1][x-j]!=7) { ng=false; break; }\n\t\t\tif(ng) rep(j,L) if(B[y ][x-j]!=(j==0||j==L-1?7:0)) { ng=false; break; }\n\t\t\tif(ng && L>=5 && y+1<h) rep(j,L) if(0<j && j<L-1 && B[y+1][x-j]!=0) { ng=false; break; }\n\t\t\tif(ng) return;\n\n\t\t\tng=true;\n\t\t\tif(ng && L>=5 && y-2>=0) rep(j,L) if(0<j && j<L-1 && B[y-2][x-j]!=0) { ng=false; break; }\n\t\t\tif(ng) rep(j,L) if(B[y-1][x-j]!=(j==0||j==L-1?7:0)) { ng=false; break; }\n\t\t\tif(ng) rep(j,L) if(B[y ][x-j]!=7) { ng=false; break; }\n\t\t\tif(ng) return;\n\t\t}\n\t\tif(y+L-1<h && x+1<w){\n\t\t\tng=true;\n\t\t\tif(ng) rep(i,L) if(B[y+i][x ]!=7) { ng=false; break; }\n\t\t\tif(ng) rep(i,L) if(B[y+i][x+1]!=(i==0||i==L-1?7:0)) { ng=false; break; }\n\t\t\tif(ng && L>=5 && x+2<w) rep(i,L) if(0<i && i<L-1 && B[y+i][x+2]!=0) { ng=false; break; }\n\t\t\tif(ng) return;\n\n\t\t\tng=true;\n\t\t\tif(ng && L>=5 && x-1>=0) rep(i,L) if(0<i && i<L-1 && B[y+i][x-1]!=0) { ng=false; break; }\n\t\t\tif(ng) rep(i,L) if(B[y+i][x ]!=(i==0||i==L-1?7:0)) { ng=false; break; }\n\t\t\tif(ng) rep(i,L) if(B[y+i][x+1]!=7) { ng=false; break; }\n\t\t\tif(ng) return;\n\t\t}\n\t\tif(y-L+1>=0 && x+1<w){\n\t\t\tng=true;\n\t\t\tif(ng) rep(i,L) if(B[y-i][x ]!=7) { ng=false; break; }\n\t\t\tif(ng) rep(i,L) if(B[y-i][x+1]!=(i==0||i==L-1?7:0)) { ng=false; break; }\n\t\t\tif(ng && L>=5 && x+2<w) rep(i,L) if(0<i && i<L-1 && B[y-i][x+2]!=0) { ng=false; break; }\n\t\t\tif(ng) return;\n\n\t\t\tng=true;\n\t\t\tif(ng && L>=5 && x-1>=0) rep(i,L) if(0<i && i<L-1 && B[y-i][x-1]!=0) { ng=false; break; }\n\t\t\tif(ng) rep(i,L) if(B[y-i][x ]!=(i==0||i==L-1?7:0)) { ng=false; break; }\n\t\t\tif(ng) rep(i,L) if(B[y-i][x+1]!=7) { ng=false; break; }\n\t\t\tif(ng) return;\n\t\t}\n\t}\n\n\trep(k,4){\n\t\tint xx=x+dx[k],yy=y+dy[k];\n\t\tif(0<=xx && xx<w && 0<=yy && yy<h){\n\t\t\tif(B[yy][xx]==2){\n\t\t\t\tans=now+1+bfs(x3,y3,3);\n\t\t\t\treturn;\n\t\t\t}\n\t\t\telse if(B[yy][xx]==0){\n\t\t\t\tB[yy][xx]=7;\n\t\t\t\tdfs(xx,yy,now+1);\n\t\t\t\tB[yy][xx]=0;\n\t\t\t}\n\t\t}\n\t}\n}\n\nint main(){\n\twhile(scanf(\"%d%d\",&h,&w),h){\n\t\trep(i,h) rep(j,w) scanf(\"%d\",B[i]+j);\n\n\t\trep(i,h) rep(j,w) {\n\t\t\tif(B[i][j]==2) x2=j, y2=i;\n\t\t\tif(B[i][j]==3) x3=j, y3=i;\n\t\t}\n\t\tB[y2][x2]=7; // 訪問済みのマーク\n\n\t\tans=777;\n\t\tdfs(x2,y2,0);\n\t\tprintf(\"%d\\n\",ans<777?ans:0);\n\t}\n\n\treturn 0;\n}", "accuracy": 1, "time_ms": 1110, "memory_kb": 1032, "score_of_the_acc": -0.3629, "final_rank": 3 }, { "submission_id": "aoj_1270_556901", "code_snippet": "#include<cstdio>\n#include<cstring>\n\n#define rep(i,n) for(int i=0;i<(n);i++)\n\nusing namespace std;\n\nconst int dx[]={1,0,-1,0},dy[]={0,-1,0,1};\n\nint h,w,B[9][9];\n\nint x2,y2,x3,y3; // 2, 3 それぞれの出発点\n\nint bfs(int x,int y,int tar){\n\tint d[9][9];\n\tmemset(d,-1,sizeof d);\n\td[y][x]=0;\n\tint Q[81],head=0,tail=0; Q[tail++]=y*w+x;\n\twhile(head<tail){\n\t\tint i=Q[head]/w,j=Q[head]%w; head++;\n\t\trep(k,4){\n\t\t\tint yy=i+dy[k],xx=j+dx[k];\n\t\t\tif(0<=yy && yy<h && 0<=xx && xx<w && d[yy][xx]==-1){\n\t\t\t\tif(B[yy][xx]==tar) return d[i][j]+1;\n\t\t\t\tif(B[yy][xx]== 0 ) Q[tail++]=yy*w+xx, d[yy][xx]=d[i][j]+1;\n\t\t\t}\n\t\t}\n\t}\n\treturn 777;\n}\n\nint ans;\nvoid dfs(int x,int y,int now){\n\tint hstar=bfs(x,y,2)+bfs(x3,y3,3);\n\tif(ans<now+1+hstar) return;\n\n\t// これまでのパスと接したら枝刈り\n\tint cnt=0;\n\trep(k,4){\n\t\tint xx=x+dx[k],yy=y+dy[k];\n\t\tif(0<=xx && xx<w && 0<=yy && yy<h && B[yy][xx]==7) cnt++;\n\t}\n\tif(cnt>=2) return;\n\n\t// 777...777\n\t// 700...007\n\t// というパターン ( or 回転して一致するもの ) があれば枝刈り\n\tfor(int L=3;L<=8;L++){\n\t\tbool ng;\n\t\tif(y>0 && x+L-1<w){\n\t\t\tng=true;\n\t\t\tif(ng) rep(j,L) if(B[y-1][x+j]!=7) { ng=false; break; }\n\t\t\tif(ng) rep(j,L) if(B[y ][x+j]!=(j==0||j==L-1?7:0)) { ng=false; break; }\n\t\t\tif(ng && y+1<h) rep(j,L) if(0<j && j<L-1 && B[y+1][x+j]!=0) { ng=false; break; }\n\t\t\tif(ng) return;\n\n\t\t\tng=true;\n\t\t\tif(ng && y-2>=0) rep(j,L) if(0<j && j<L-1 && B[y-2][x+j]!=0) { ng=false; break; }\n\t\t\tif(ng) rep(j,L) if(B[y-1][x+j]!=(j==0||j==L-1?7:0)) { ng=false; break; }\n\t\t\tif(ng) rep(j,L) if(B[y ][x+j]!=7) { ng=false; break; }\n\t\t\tif(ng) return;\n\t\t}\n\t\tif(y>0 && x-L+1>=0){\n\t\t\tng=true;\n\t\t\tif(ng) rep(j,L) if(B[y-1][x-j]!=7) { ng=false; break; }\n\t\t\tif(ng) rep(j,L) if(B[y ][x-j]!=(j==0||j==L-1?7:0)) { ng=false; break; }\n\t\t\tif(ng && y+1<h) rep(j,L) if(0<j && j<L-1 && B[y+1][x-j]!=0) { ng=false; break; }\n\t\t\tif(ng) return;\n\n\t\t\tng=true;\n\t\t\tif(ng && y-2>=0) rep(j,L) if(0<j && j<L-1 && B[y-2][x-j]!=0) { ng=false; break; }\n\t\t\tif(ng) rep(j,L) if(B[y-1][x-j]!=(j==0||j==L-1?7:0)) { ng=false; break; }\n\t\t\tif(ng) rep(j,L) if(B[y ][x-j]!=7) { ng=false; break; }\n\t\t\tif(ng) return;\n\t\t}\n\t\tif(y+L-1<h && x+1<w){\n\t\t\tng=true;\n\t\t\tif(ng) rep(i,L) if(B[y+i][x ]!=7) { ng=false; break; }\n\t\t\tif(ng) rep(i,L) if(B[y+i][x+1]!=(i==0||i==L-1?7:0)) { ng=false; break; }\n\t\t\tif(ng && x+2<w) rep(i,L) if(0<i && i<L-1 && B[y+i][x+2]!=0) { ng=false; break; }\n\t\t\tif(ng) return;\n\n\t\t\tng=true;\n\t\t\tif(ng && x-1>=0) rep(i,L) if(0<i && i<L-1 && B[y+i][x-1]!=0) { ng=false; break; }\n\t\t\tif(ng) rep(i,L) if(B[y+i][x ]!=(i==0||i==L-1?7:0)) { ng=false; break; }\n\t\t\tif(ng) rep(i,L) if(B[y+i][x+1]!=7) { ng=false; break; }\n\t\t\tif(ng) return;\n\t\t}\n\t\tif(y-L+1>=0 && x+1<w){\n\t\t\tng=true;\n\t\t\tif(ng) rep(i,L) if(B[y-i][x ]!=7) { ng=false; break; }\n\t\t\tif(ng) rep(i,L) if(B[y-i][x+1]!=(i==0||i==L-1?7:0)) { ng=false; break; }\n\t\t\tif(ng && x+2<w) rep(i,L) if(0<i && i<L-1 && B[y-i][x+2]!=0) { ng=false; break; }\n\t\t\tif(ng) return;\n\n\t\t\tng=true;\n\t\t\tif(ng && x-1>=0) rep(i,L) if(0<i && i<L-1 && B[y-i][x-1]!=0) { ng=false; break; }\n\t\t\tif(ng) rep(i,L) if(B[y-i][x ]!=(i==0||i==L-1?7:0)) { ng=false; break; }\n\t\t\tif(ng) rep(i,L) if(B[y-i][x+1]!=7) { ng=false; break; }\n\t\t\tif(ng) return;\n\t\t}\n\t}\n\n\trep(k,4){\n\t\tint xx=x+dx[k],yy=y+dy[k];\n\t\tif(0<=xx && xx<w && 0<=yy && yy<h){\n\t\t\tif(B[yy][xx]==2){\n\t\t\t\tans=now+1+bfs(x3,y3,3);\n\t\t\t\treturn;\n\t\t\t}\n\t\t\telse if(B[yy][xx]==0){\n\t\t\t\tB[yy][xx]=7;\n\t\t\t\tdfs(xx,yy,now+1);\n\t\t\t\tB[yy][xx]=0;\n\t\t\t}\n\t\t}\n\t}\n}\n\nint main(){\n\twhile(scanf(\"%d%d\",&h,&w),h){\n\t\trep(i,h) rep(j,w) scanf(\"%d\",B[i]+j);\n\n\t\trep(i,h) rep(j,w) {\n\t\t\tif(B[i][j]==2) x2=j, y2=i;\n\t\t\tif(B[i][j]==3) x3=j, y3=i;\n\t\t}\n\t\tB[y2][x2]=7; // 訪問済みのマーク\n\n\t\tans=777;\n\t\tdfs(x2,y2,0);\n\t\tprintf(\"%d\\n\",ans<777?ans:0);\n\t}\n\n\treturn 0;\n}", "accuracy": 1, "time_ms": 1350, "memory_kb": 1036, "score_of_the_acc": -0.4473, "final_rank": 6 }, { "submission_id": "aoj_1270_556892", "code_snippet": "#include<cstdio>\n#include<cstring>\n#include<sys/time.h>\n\n#define rep(i,n) for(int i=0;i<(n);i++)\n\nusing namespace std;\n\ntypedef long long ll;\n\nll gettime(){\n\tstruct timeval tv;\n\tgettimeofday(&tv,NULL);\n\treturn 1000000LL*tv.tv_sec+tv.tv_usec;\n}\n\nconst int dx[]={1,0,-1,0},dy[]={0,-1,0,1};\n\nint h,w,B[9][9];\n\nint x2,y2,x3,y3; // 2, 3 それぞれの出発点\n\nint bfs(int x,int y,int tar){\n\tint d[9][9];\n\tmemset(d,-1,sizeof d);\n\td[y][x]=0;\n\tint Q[81],head=0,tail=0; Q[tail++]=y*w+x;\n\twhile(head<tail){\n\t\tint i=Q[head]/w,j=Q[head]%w; head++;\n\t\trep(k,4){\n\t\t\tint yy=i+dy[k],xx=j+dx[k];\n\t\t\tif(0<=yy && yy<h && 0<=xx && xx<w && d[yy][xx]==-1){\n\t\t\t\tif(B[yy][xx]==tar) return d[i][j]+1;\n\t\t\t\tif(B[yy][xx]== 0 ) Q[tail++]=yy*w+xx, d[yy][xx]=d[i][j]+1;\n\t\t\t}\n\t\t}\n\t}\n\treturn 777;\n}\n\nll t0;\n\nint ans;\nvoid dfs(int x,int y,int now){\n\tint hstar=bfs(x,y,2)+bfs(x3,y3,3);\n\tif(ans<now+1+hstar) return;\n\n\tif(ans==777){ // 0.5 秒の間に一つも解が見つからなければ解なし\n\t\tll t1=gettime();\n\t\tif(t1-t0>500000){ ans=0; return; }\n\t}\n\n\t// これまでのパスと接したら最適解でないので探索打ち切り\n\tint cnt=0;\n\trep(k,4){\n\t\tint xx=x+dx[k],yy=y+dy[k];\n\t\tif(0<=xx && xx<w && 0<=yy && yy<h && B[yy][xx]==7) cnt++;\n\t}\n\tif(cnt>=2) return;\n\n\trep(k,4){\n\t\tint xx=x+dx[k],yy=y+dy[k];\n\t\tif(0<=xx && xx<w && 0<=yy && yy<h){\n\t\t\tif(B[yy][xx]==2){\n\t\t\t\tans=now+1+bfs(x3,y3,3);\n\t\t\t\treturn;\n\t\t\t}\n\t\t\telse if(B[yy][xx]==0){\n\t\t\t\tB[yy][xx]=7;\n\t\t\t\tdfs(xx,yy,now+1);\n\t\t\t\tB[yy][xx]=0;\n\t\t\t}\n\t\t}\n\t}\n}\n\nint main(){\n\twhile(scanf(\"%d%d\",&h,&w),h){\n\t\trep(i,h) rep(j,w) scanf(\"%d\",B[i]+j);\n\n\t\trep(i,h) rep(j,w) {\n\t\t\tif(B[i][j]==2) x2=j, y2=i;\n\t\t\tif(B[i][j]==3) x3=j, y3=i;\n\t\t}\n\t\tB[y2][x2]=7; // 訪問済みのマーク\n\n\t\tt0=gettime();\n\t\tans=777;\n\t\tdfs(x2,y2,0);\n\t\tprintf(\"%d\\n\",ans<777?ans:0);\n\t}\n\n\treturn 0;\n}", "accuracy": 1, "time_ms": 1820, "memory_kb": 1028, "score_of_the_acc": -0.6119, "final_rank": 7 }, { "submission_id": "aoj_1270_556891", "code_snippet": "#include<cstdio>\n#include<cstring>\n#include<sys/time.h>\n\n#define rep(i,n) for(int i=0;i<(n);i++)\n\nusing namespace std;\n\ntypedef long long ll;\n\nll gettime(){\n\tstruct timeval tv;\n\tgettimeofday(&tv,NULL);\n\treturn 1000000LL*tv.tv_sec+tv.tv_usec;\n}\n\nconst int dx[]={1,0,-1,0},dy[]={0,-1,0,1};\n\nint h,w,B[9][9];\n\nint x2,y2,x3,y3; // 2, 3 それぞれの出発点\n\nint bfs(int x,int y,int tar){\n\tint d[9][9];\n\tmemset(d,-1,sizeof d);\n\td[y][x]=0;\n\tint Q[81],head=0,tail=0; Q[tail++]=y*w+x;\n\twhile(head<tail){\n\t\tint i=Q[head]/w,j=Q[head]%w; head++;\n\t\trep(k,4){\n\t\t\tint yy=i+dy[k],xx=j+dx[k];\n\t\t\tif(0<=yy && yy<h && 0<=xx && xx<w && d[yy][xx]==-1){\n\t\t\t\tif(B[yy][xx]==tar) return d[i][j]+1;\n\t\t\t\tif(B[yy][xx]== 0 ) Q[tail++]=yy*w+xx, d[yy][xx]=d[i][j]+1;\n\t\t\t}\n\t\t}\n\t}\n\treturn 777;\n}\n\nll t0;\n\nint ans;\nvoid dfs(int x,int y,int now){\n\tint hstar=bfs(x,y,2)+bfs(x3,y3,3);\n\tif(ans<now+1+hstar) return;\n\n\tif(ans==777){ // 1 秒の間に一つも解が見つからなければ解なし\n\t\tll t1=gettime();\n\t\tif(t1-t0>1000000){ ans=0; return; }\n\t}\n\n\t// これまでのパスと接したら最適解でないので探索打ち切り\n\tint cnt=0;\n\trep(k,4){\n\t\tint xx=x+dx[k],yy=y+dy[k];\n\t\tif(0<=xx && xx<w && 0<=yy && yy<h && B[yy][xx]==7) cnt++;\n\t}\n\tif(cnt>=2) return;\n\n\trep(k,4){\n\t\tint xx=x+dx[k],yy=y+dy[k];\n\t\tif(0<=xx && xx<w && 0<=yy && yy<h){\n\t\t\tif(B[yy][xx]==2){\n\t\t\t\tans=now+1+bfs(x3,y3,3);\n\t\t\t\treturn;\n\t\t\t}\n\t\t\telse if(B[yy][xx]==0){\n\t\t\t\tB[yy][xx]=7;\n\t\t\t\tdfs(xx,yy,now+1);\n\t\t\t\tB[yy][xx]=0;\n\t\t\t}\n\t\t}\n\t}\n}\n\nint main(){\n\twhile(scanf(\"%d%d\",&h,&w),h){\n\t\trep(i,h) rep(j,w) scanf(\"%d\",B[i]+j);\n\n\t\trep(i,h) rep(j,w) {\n\t\t\tif(B[i][j]==2) x2=j, y2=i;\n\t\t\tif(B[i][j]==3) x3=j, y3=i;\n\t\t}\n\t\tB[y2][x2]=7; // 訪問済みのマーク\n\n\t\tt0=gettime();\n\t\tans=777;\n\t\tdfs(x2,y2,0);\n\t\tprintf(\"%d\\n\",ans<777?ans:0);\n\t}\n\n\treturn 0;\n}", "accuracy": 1, "time_ms": 2320, "memory_kb": 1028, "score_of_the_acc": -0.7874, "final_rank": 10 }, { "submission_id": "aoj_1270_556890", "code_snippet": "#include<cstdio>\n#include<cstring>\n#include<sys/time.h>\n\n#define rep(i,n) for(int i=0;i<(n);i++)\n\nusing namespace std;\n\ntypedef long long ll;\n\nll gettime(){\n\tstruct timeval tv;\n\tgettimeofday(&tv,NULL);\n\treturn 1000000LL*tv.tv_sec+tv.tv_usec;\n}\n\nconst int dx[]={1,0,-1,0},dy[]={0,-1,0,1};\n\nint h,w,B[9][9];\n\nint x2,y2,x3,y3; // 2, 3 それぞれの出発点\n\nint bfs(int x,int y,int tar){\n\tint d[9][9];\n\tmemset(d,-1,sizeof d);\n\td[y][x]=0;\n\tint Q[81],head=0,tail=0; Q[tail++]=y*w+x;\n\twhile(head<tail){\n\t\tint i=Q[head]/w,j=Q[head]%w; head++;\n\t\trep(k,4){\n\t\t\tint yy=i+dy[k],xx=j+dx[k];\n\t\t\tif(0<=yy && yy<h && 0<=xx && xx<w && d[yy][xx]==-1){\n\t\t\t\tif(B[yy][xx]==tar) return d[i][j]+1;\n\t\t\t\tif(B[yy][xx]== 0 ) Q[tail++]=yy*w+xx, d[yy][xx]=d[i][j]+1;\n\t\t\t}\n\t\t}\n\t}\n\treturn 777;\n}\n\nll t0;\n\nint ans;\nvoid dfs(int x,int y,int now){\n\tint hstar=bfs(x,y,2)+bfs(x3,y3,3);\n\tif(ans<now+1+hstar) return;\n\n\tif(ans==777){ // 3 秒の間に一つも解が見つからなければ解なし\n\t\tll t1=gettime();\n\t\tif(t1-t0>3000000){ ans=0; return; }\n\t}\n\n\t// これまでのパスと接したら最適解でないので探索打ち切り\n\tint cnt=0;\n\trep(k,4){\n\t\tint xx=x+dx[k],yy=y+dy[k];\n\t\tif(0<=xx && xx<w && 0<=yy && yy<h && B[yy][xx]==7) cnt++;\n\t}\n\tif(cnt>=2) return;\n\n\trep(k,4){\n\t\tint xx=x+dx[k],yy=y+dy[k];\n\t\tif(0<=xx && xx<w && 0<=yy && yy<h){\n\t\t\tif(B[yy][xx]==2){\n\t\t\t\tans=now+1+bfs(x3,y3,3);\n\t\t\t\treturn;\n\t\t\t}\n\t\t\telse if(B[yy][xx]==0){\n\t\t\t\tB[yy][xx]=7;\n\t\t\t\tdfs(xx,yy,now+1);\n\t\t\t\tB[yy][xx]=0;\n\t\t\t}\n\t\t}\n\t}\n}\n\nint main(){\n\twhile(scanf(\"%d%d\",&h,&w),h){\n\t\trep(i,h) rep(j,w) scanf(\"%d\",B[i]+j);\n\n\t\trep(i,h) rep(j,w) {\n\t\t\tif(B[i][j]==2) x2=j, y2=i;\n\t\t\tif(B[i][j]==3) x3=j, y3=i;\n\t\t}\n\t\tB[y2][x2]=7; // 訪問済みのマーク\n\n\t\tt0=gettime();\n\t\tans=777;\n\t\tdfs(x2,y2,0);\n\t\tprintf(\"%d\\n\",ans<777?ans:0);\n\t}\n\n\treturn 0;\n}", "accuracy": 1, "time_ms": 2840, "memory_kb": 1028, "score_of_the_acc": -0.9698, "final_rank": 11 }, { "submission_id": "aoj_1270_318767", "code_snippet": "#include<iostream>\n#include<algorithm>\nusing namespace std;\n#define REP(i,b,n) for(int i=b;i<n;i++)\n#define rep(i,n) REP(i,0,n)\n\nconst int N = 9;\nconst int inf = (1 <<21);\n\nint dx[]={0,1, 0,-1};\nint dy[]={1,0,-1,0};\nint op[]={2,3, 0,1};\nint m[N][N];\nbool vis2[N][N];\n\nbool isok(int r,int c,int y,int x,int last){\n rep(i,4){\n if (op[i] == last)continue;\n int nex=x+dx[i],ney=y+dy[i];\n if (nex==-1||ney==-1||nex==c||ney==r)continue;\n if (vis2[ney][nex])return false;\n }\n return true;\n}\n\nint ans;\nbool isgoal;\nint md;\nbool bfs(int r,int c,int cnt,bool goal2){\n static int q[N*N];\n static int cost[N][N];\n int sy3=-1,sx3=-1,gy3=-1,gx3=-1;\n rep(i,r){\n rep(j,c){\n if (m[i][j] == 3){\n\tif (sy3==-1)sy3=i,sx3=j;\n\telse gy3=i,gx3=j;\n }\n cost[i][j]=inf;\n }\n }\n int tail=0,head=0;\n q[tail++]=sx3+sy3*c;\n cost[sy3][sx3]=0;\n while(head != tail){\n int now = q[head++];\n int x = now%c,y=now/c;\n if (cnt+cost[y][x] >= ans)return false;\n if (x == gx3 && y == gy3){\n if (goal2)return true;\n isgoal=true;\n ans =min(ans,cnt+cost[y][x]);\n return true;\n }\n rep(i,4){\n int nex=x+dx[i],ney=y+dy[i];\n if (nex==-1||ney==-1||nex==c||ney==r||vis2[ney][nex])continue;\n if (m[ney][nex]==1||m[ney][nex] == 2)continue;\n if (cost[ney][nex] != inf)continue;\n cost[ney][nex]=cost[y][x]+1;\n q[tail++]=ney*c+nex;\n }\n }\n return false;\n}\n\nint path[N*N];\nint pattern[8][4]={\n {2,1,1,0,},\n {1,0,0,3,},\n {0,3,3,2,},\n {3,2,2,1,},\n {2,3,3,0,},\n {1,2,2,3,},\n {0,1,1,2,},\n {3,0,0,1,},\n};\n\nint cx[]={-1, 0,1, 0, 1, 0,-1, 0};\nint cy[]={ 0,-1,0, 1, 0, 1, 0,-1};\n\nbool isok2(int r,int c,int y,int x,int p){\n if (p <4)return true;\n rep(i,8){\n bool ismatch=true;\n rep(j,4){\n if (pattern[i][j] == path[p-(4-j)]);\n else ismatch=false;\n }\n if (!ismatch)continue;\n int nex=x+cx[i],ney=y+cy[i];\n if (nex==-1||ney==-1||nex==c||ney==r)continue;\n if (m[ney][nex] == 0 && !vis2[ney][nex])return false;\n\n }\n return true;\n}\n\nvoid dfs(int r,int c,int y,int x,int cnt,int last,int gy2,int gx2){\n if (!isok(r,c,y,x,last)){\n return;\n }\n if (!isok2(r,c,y,x,cnt))return;\n\n if (cnt+md >= ans)return;\n // if (!bfs(r,c,cnt,true))return;\n\n if (x == gx2 && y == gy2){\n vis2[y][x]=true;\n bfs(r,c,cnt,false);\n vis2[y][x]=false;\n return;\n }\n\n vis2[y][x]=true;\n rep(i,4){\n if (op[i] == last)continue;\n int nex=x+dx[i],ney=y+dy[i];\n if (nex==-1||ney==-1||nex==c||ney==r||vis2[ney][nex])continue;\n if (m[ney][nex] == 3 || m[ney][nex] == 1)continue;\n path[cnt]=i;\n dfs(r,c,ney,nex,cnt+1,i,gy2,gx2);\n }\n vis2[y][x]=false;\n}\n\nint bfs2(int r,int c,int tar,int utar){\n static int q[N*N];\n static int cost[N][N];\n int sy3=-1,sx3=-1,gy3=-1,gx3=-1;\n rep(i,r){\n rep(j,c){\n if (m[i][j] == tar){\n\tif (sy3==-1)sy3=i,sx3=j;\n\telse gy3=i,gx3=j;\n }\n cost[i][j]=inf;\n }\n }\n int tail=0,head=0;\n q[tail++]=sx3+sy3*c;\n cost[sy3][sx3]=0;\n while(head != tail){\n int now = q[head++];\n int x = now%c,y=now/c;\n if (x == gx3 && y == gy3){\n return cost[y][x];\n }\n rep(i,4){\n int nex=x+dx[i],ney=y+dy[i];\n if (nex==-1||ney==-1||nex==c||ney==r||vis2[ney][nex])continue;\n if (m[ney][nex]==1||m[ney][nex] == utar)continue;\n if (cost[ney][nex] != inf)continue;\n cost[ney][nex]=cost[y][x]+1;\n q[tail++]=ney*c+nex;\n }\n }\n return false;\n}\n\nmain(){\n int r,c;\n while(cin>>r>>c && r){\n ans = r*c;\n isgoal=false;\n rep(i,r)rep(j,c)cin>>m[i][j];\n rep(i,r)rep(j,c)vis2[i][j]=false;\n \n int sx2=-1,sy2=-1;\n int gx2=-1,gy2=-1;\n rep(i,r){\n rep(j,c){\n\tif (m[i][j] == 2){\n\t if (sx2 == -1)sx2=j,sy2=i;\n\t else gx2=j,gy2=i;\n\t}\n }\n }\n \n int sx3=-1,sy3=-1,gx3=-1,gy3=-1;\n rep(i,r){\n rep(j,c){\n\tif (m[i][j] == 3){\n\t if (sx3 == -1)sx3=j,sy3=i;\n\t else gx3=j,gy3=i;\n\t}\n }\n }\n\n md=bfs2(r,c,3,2);\n \n dfs(r,c,sy2,sx2,0,-1,gy2,gx2);\n \n if (isgoal)cout << ans << endl;\n else cout << 0 << endl;\n }\n return false;\n}", "accuracy": 1, "time_ms": 2940, "memory_kb": 868, "score_of_the_acc": -1, "final_rank": 12 } ]
aoj_1277_cpp
Problem C: Minimal Backgammon Here is a very simple variation of the game backgammon, named “Minimal Backgammon”. The game is played by only one player, using only one of the dice and only one checker (the token used by the player). The game board is a line of ( N + 1) squares labeled as 0 (the start) to N (the goal). At the beginning, the checker is placed on the start (square 0). The aim of the game is to bring the checker to the goal (square N ). The checker proceeds as many squares as the roll of the dice. The dice generates six integers from 1 to 6 with equal probability. The checker should not go beyond the goal. If the roll of the dice would bring the checker beyond the goal, the checker retreats from the goal as many squares as the excess. For example, if the checker is placed at the square ( N - 3), the roll "5" brings the checker to the square ( N - 2), because the excess beyond the goal is 2. At the next turn, the checker proceeds toward the goal as usual. Each square, except the start and the goal, may be given one of the following two special instructions. Lose one turn (labeled " L " in Figure 2) If the checker stops here, you cannot move the checker in the next turn. Go back to the start (labeled " B " in Figure 2) If the checker stops here, the checker is brought back to the start. Figure 2: An example game Given a game board configuration (the size N , and the placement of the special instructions), you are requested to compute the probability with which the game succeeds within a given number of turns. Input The input consists of multiple datasets, each containing integers in the following format. N T L B Lose 1 ... Lose L Back 1 ... Back B N is the index of the goal, which satisfies 5 ≤ N ≤ 100. T is the number of turns. You are requested to compute the probability of success within T turns. T satisfies 1 ≤ T ≤ 100. L is the number of squares marked “Lose one turn”, which satisfies 0 ≤ L ≤ N - 1. B is the number of squares marked “Go back to the start”, which satisfies 0 ≤ B ≤ N - 1. They are separated by a space. Lose i 's are the indexes of the squares marked “Lose one turn”, which satisfy 1 ≤ Lose i ≤ N - 1. All Lose i 's are distinct, and sorted in ascending order. Back i 's are the indexes of the squares marked “Go back to the start”, which satisfy 1 ≤ Back i ≤ N - 1. All Back i 's are distinct, and sorted in ascending order. No numbers occur both in Lose i 's and Back i 's. The end of the input is indicated by a line containing four zeros separated by a space. Output For each dataset, you should answer the probability with which the game succeeds within the given number of turns. The output should not contain an error greater than 0.00001. Sample Input 6 1 0 0 7 1 0 0 7 2 0 0 6 6 1 1 2 5 7 10 0 6 1 2 3 4 5 6 0 0 0 0 Output for the Sample Input 0.166667 0.000000 0.166667 0.619642 0.000000
[ { "submission_id": "aoj_1277_3231955", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\nint main() {\n int N, T, L, B;\n cout << fixed << setprecision(6);\n while (cin >> N >> T >> L >> B, N) {\n map<int, bool> Lose;\n for (int i = 0; i < L; i++) {\n int l;\n cin >> l;\n Lose[l] = true;\n }\n map<int, bool> Back;\n for (int i = 0; i < B; i++) {\n int b;\n cin >> b;\n Back[b] = true;\n }\n\n vector<vector<double>> dp(T + 2, vector<double>(N + 1, 0.0));\n dp[0][0] = 1.0;\n auto f = [&](int n, int p) {\n return (Lose[p]) ? n + 1 : n;\n };\n auto g = [&](int p) {\n int q = min(N, p) - max(0, p - N);\n return (Back[q]) ? 0 : q;\n };\n for (int i = 0; i < T; i++) {\n for (int j = 0; j < N; j++) {\n for (int k = 0; k < 6; k++) {\n dp[f(i + 1, j)][g(j + k + 1)] += 1.0 / 6.0 * dp[i][j];\n }\n }\n }\n\n double ans = 0;\n for (int i = 0; i < T + 1; i++) {\n ans += dp[i][N];\n }\n cout << ans << endl;\n }\n return 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3332, "score_of_the_acc": -0.932, "final_rank": 9 }, { "submission_id": "aoj_1277_2483601", "code_snippet": "//#include \"bits/stdc++.h\"\n\n#include <iostream>\n#include <vector>\n#include <iomanip>\n// #include <map>\n// #include <string>\n// #include <algorithm>\n// #include <numeric>\n// #include <limits>\n\nusing namespace std;\n\nsize_t n;\nsize_t t;\nsize_t l;\nsize_t b;\nsize_t s;\ndouble matrix[200][200]; // [from][to]\ndouble ans[200];\n\nvoid printAns() {\n\tfor (size_t i = 0; i < s; i++) {\n\t\tcout << fixed << setprecision(6) << ans[i] << ' ';\n\t}\n\tcout << endl;\n}\n\nvoid print() {\n\tfor (size_t i = 0; i < s; i++) {\n\t\tfor (size_t j = 0; j < s; j++) {\n\t\t\tcout << fixed << setprecision(6) << matrix[i][j] << ' ';\n\t\t}\n\t\tcout << endl;\n\t}\n\tcout << endl;\n}\n\nvoid prod() {\n\tdouble bef[200];\n\tfor (size_t i = 0; i < s; i++) {\n\t\tbef[i] = ans[i];\n\t}\n\tfor (size_t to = 0; to < s; to++) {\n\t\tdouble sum = 0;\n\t\tfor (size_t from = 0; from < s; from++) {\n\t\t\tsum += bef[from] * matrix[from][to];\n\t\t}\n\t\tans[to] = sum;\n\t}\n}\n\nvoid makeMatrix() {\n\tfor (size_t i = 0; i < s; i++) {\n\t\tfor (size_t j = 0; j < s; j++) {\n\t\t\tmatrix[i][j] = 0;\n\t\t}\n\t\tans[i] = 0;\n\t}\n\tans[0] = 1;\n\tfor (size_t i = 0; i < n - 1; i++) {\n\t\tfor (size_t j = i + 1; j <= i + 6; j++) {\n\t\t\tif (j >= n) {\n\t\t\t\tauto key = (n - 1) - (j - (n - 1));\n\t\t\t\tmatrix[i][key] += 1 / (double) 6;\n\t\t\t\tcontinue;\n\t\t\t}\n\t\t\tmatrix[i][j] += 1 / (double) 6;\n\t\t}\n\t}\n}\n\nvoid solve() {\n\tn = n + 1;\n\ts = n + l;\n\tmakeMatrix();\n\t// for l\n\tfor (size_t i = 0; i < l; i++) {\n\t\tsize_t target;\n\t\tcin >> target;\n\t\t//std::cout << \"debug \" << n + i << std::endl; // debug\n\t\tmatrix[n + i][target] = 1;\n\t\tfor (size_t from = 0; from < n; from++) {\n\t\t\tmatrix[from][n + i] = matrix[from][target];\n\t\t\tmatrix[from][target] = 0;\n\t\t}\n\t}\n\t// for b\n\tfor (size_t i = 1; i <= b; i++) {\n\t\tsize_t target;\n\t\tcin >> target;\n\t\tfor (size_t from = 0; from < n; from++) {\n\t\t\tmatrix[from][0] += matrix[from][target];\n\t\t\tmatrix[from][target] = 0;\n\t\t}\n\t}\n\t// for goal\n\tmatrix[n - 1][n - 1] = 1;\n\t// print();\n\t// cout << endl;\n\tfor (size_t i = 0; i < t; i++) {\n\t\t// printAns();\n\t\t// cout << endl;\n\t\tprod();\n\t}\n\t// printAns();\n\t// cout << endl;\n\tcout << fixed << setprecision(6) << ans[n - 1] << endl;\n}\n\nint main() {\n\n\twhile (true) {\n\t\tcin >> n >> t >> l >> b;\n\t\tif (n == 0) {\n\t\t\tbreak;\n\t\t}\n\t\tsolve();\n\t}\n\n\t//std::cout << \"\\e[38;5;0m\\e[48;5;40m --- end --- \\e[m\" << std::endl; // debug\n\treturn 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3480, "score_of_the_acc": -1, "final_rank": 10 }, { "submission_id": "aoj_1277_2290418", "code_snippet": "#include <cstdio>\n#include <cstring>\n#include <iostream>\n#include <map>\n\nusing namespace std;\n#define DEBUG(X) cerr<<__LINE__<<\" \"<<#X<<\": \"<<X<<endl;\n#define sci(x) int x;scanf(\"%d\",&x);\n#define scii(x, y) int x,y;scanf(\"%d%d\",&x,&y);\n#define REP(i, y) for(int i=0;i<y;i++)\n#define FOR(i,x,y) for(int i=x;i<y;i++)\n#define DUMPP(A, n, m) for (auto x=begin(A); x != begin(A)+n;x++) {for (auto y=begin(*x); y != begin(*x)+m;)cerr <<*y++<< ' '; cerr<<endl;};\n\nconst int L_FLAG = 1;\nconst int B_FLAG = 2;\n\nconst int MAX_N = 110;\nconst int MAX_T = 110;\ndouble dp[MAX_N][MAX_T];\n\n\nint floor(int i, int N) {\n return i>N?N-(i-N):i;\n}\n\nint main() {\n // N: ??¢?????°\n // T: ?????°\n // L: 1???????????????????????¢?????°\n // B: ?????????????????????????????¢?????°\n int N,T,L,B;\n while(scanf(\"%d%d%d%d\", &N, &T, &L, &B), N) {\n memset(&dp, 0, sizeof(dp));\n map<int, int> mp;\n REP(i, L) {\n sci(L_i)\n mp[L_i] = L_FLAG;\n }\n REP(i, B) {\n sci(B_i)\n mp[B_i] = B_FLAG;\n }\n\n // main loop\n REP(t, T) {\n REP(i, N) { // N+1?????????????????????N???????§???????????????¨????????????\n if (i==0&&t==0) dp[i][t]=1.0;\n\n FOR(j, 1, 7) {\n int npos = floor(i+j, N);\n int nturn = t + 1;\n if (mp[npos] == L_FLAG) nturn += 1;\n if (mp[npos] == B_FLAG) npos = 0;\n dp[npos][nturn] += dp[i][t] / 6.;\n }\n }\n }\n //DUMPP(dp, N+1, T+1);\n double ans=0.;\n REP(i,T+1) ans+=dp[N][i];\n printf(\"%f\\n\", ans);\n }\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3220, "score_of_the_acc": -0.8805, "final_rank": 7 }, { "submission_id": "aoj_1277_2150122", "code_snippet": "#include<iostream>\n#include<algorithm>\n#include<vector>\n#include<iomanip>\nusing namespace std;\nmain()\n{\n\tint n,m,l,b;\n\twhile(cin>>n>>m>>l>>b,n)\n\t{\n\t\tvector<int> back,lose;\n\t\tfor(int i=0;i<l;i++)\n\t\t{\n\t\t\tint u;cin>>u;\n\t\t\tlose.push_back(u);\n\t\t}\n\t\tfor(int i=0;i<b;i++)\n\t\t{\n\t\t\tint u;cin>>u;\n\t\t\tback.push_back(u);\n\t\t}\n\t\tdouble dp[102][101]={};//tarn,place\n\t\tdp[0][0]=1.0;\n\t\tfor(int i=0;i<m;i++)\n\t\t{\n\t\t\tfor(int p=0;p<n;p++)\n\t\t\t{\n\t\t\t\tfor(int j=1;j<=6;j++)\n\t\t\t\t{\n\t\t\t\t\tint u=p+j;\n\t\t\t\t\tint t=1;\n\t\t\t\t\tif(u>n)u=n-(u-n);\n\t\t\t\t\tbool flag=false;\n\t\t\t\t\tfor(int k=0;k<l;k++)if(u==lose[k])flag=true;\n\t\t\t\t\tif(flag)t++;\n\t\t\t\t\tflag=false;\n\t\t\t\t\tfor(int k=0;k<b;k++)if(u==back[k])flag=true;\n\t\t\t\t\tif(flag)u=0;\n\t\t\t\t\tdp[i+t][u]+=dp[i][p]/6.0;\n\t\t\t\t}\n\t\t\t}\n\t\t\tdp[i+1][n]+=dp[i][n];\n\t\t}\n\t\tcout<<fixed<<setprecision(9)<<dp[m][n]+dp[m+1][n]<<endl;\n\t}\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3268, "score_of_the_acc": -0.9026, "final_rank": 8 }, { "submission_id": "aoj_1277_1818936", "code_snippet": "#include <iostream>\n#include <stdio.h>\n#define MAX_N 100\n#define MAX_T 100\nusing namespace std;\n\ndouble mbg(int N, int T, int L, int B, int Lose[], int Back[]) {\n\tint t;\n\tdouble p_goal = 0;\n\tdouble ways[MAX_T + 2][MAX_N + 1] = { { 0 } };\n\n\tt = 0;\n\tways[0][0] = 1.0;\n\twhile (t < T) {\n\t\tfor (int i = 0; i < N; ++i) {\n\t\t\tfor (int j = 1; j <= 6; ++j) {\n\t\t\t\tif ((i + j) <= N) {\n\t\t\t\t\tfor (int k = 0; k < L; ++k) {\n\t\t\t\t\t\tif ((i + j) == Lose[k]) {\n\t\t\t\t\t\t\tways[t + 2][i + j] += ways[t][i] / 6;\n\t\t\t\t\t\t\tgoto escape1;\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t\tfor (int k = 0; k < B; ++k) {\n\t\t\t\t\t\tif ((i + j) == Back[k]) {\n\t\t\t\t\t\t\tways[t + 1][0] += ways[t][i] / 6;\n\t\t\t\t\t\t\tgoto escape1;\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t\tways[t + 1][i + j] += ways[t][i] / 6;\n\t\t\t\tescape1:;\n\t\t\t\t}\n\t\t\t\telse{\n\t\t\t\t\tfor (int k = 0; k < L; ++k) {\n\t\t\t\t\t\tif ((2 * N - (i + j)) == Lose[k]) {\n\t\t\t\t\t\t\tways[t + 2][2 * N - (i + j)] += ways[t][i] / 6;\n\t\t\t\t\t\t\tgoto escape2;\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t\tfor (int k = 0; k < B; ++k) {\n\t\t\t\t\t\tif ((2 * N - (i + j)) == Back[k]) {\n\t\t\t\t\t\t\tways[t + 1][0] += ways[t][i] / 6;\n\t\t\t\t\t\t\tgoto escape2;\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t\tways[t + 1][2 * N - (i + j)] += ways[t][i] / 6;\n\t\t\t\tescape2:;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\tt++;\n\t}\n\n\tfor (int i = 1; i <= T; ++i) {\n\t\tp_goal += ways[i][N];\n\t}\n\treturn p_goal;\n}\n\nint main() {\n\tint N, T, L, B;\n\twhile (1) {\n\t\tint *Lose, *Back;\n\t\tLose = new int[MAX_N]; Back = new int[MAX_N];\n\t\tcin >> N >> T >> L >> B;\n\t\tif (N == 0 && T == 0 && L == 0 && B == 0) break;\n\t\tfor (int i = 0; i < L; ++i) cin >> Lose[i];\n\t\tfor (int i = 0; i < B; ++i) cin >> Back[i];\n\t\tprintf(\"%.6f\\n\", mbg(N, T, L, B, Lose, Back));\n\t\tdelete[] Lose; delete[] Back;\n\t}\n\treturn 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 1304, "score_of_the_acc": 0, "final_rank": 1 }, { "submission_id": "aoj_1277_1597354", "code_snippet": "#include <algorithm>\n#include <cstdio>\n#include <iostream>\n#include <vector>\nusing namespace std;\n\n\nint main(){\n while (1){\n int N,T,L,B;\n cin >>N >>T >>L >>B;\n if(N==0) break;\n\n vector<bool> lose(N+1);\n vector<bool> back(N+1);\n //?????????\n fill(lose.begin(), lose.end(), false);\n fill(back.begin(), back.end(), false);\n\n for(int i=0; i<L; ++i){\n int t; cin>>t;\n lose[t]=true;\n if(t>=N-5) lose[2*N-t]=true;\n }\n for(int i=0; i<B; ++i){\n int t; cin>>t;\n back[t]=true;\n if(t>=N-5) back[2*N-t]=true;\n }\n\n //p[t][n]:t?????????????????????n??????????¢????\n double p[101][106];\n\n //????????¶???\n p[0][0]=1.0;\n for(int i=1; i<=105; ++i) p[0][i]=0.0;\n\n for(int t=1; t<=T; ++t){\n //printf(\"go %d turn\\n\", t);\n\n double suuuu=0.0;\n\n for(int n=0; n<=N+5; ++n){\n p[t][n]=0.0;\n\n if(n==N) p[t][n]+=p[t-1][n];\n\n if(lose[n]){\n int pp=1;\n double tmp=0.0;\n for(int i=t-1; i>=0; --i){\n tmp+=p[i][n]*pp;\n pp*=-1;\n }\n p[t][n] += tmp;\n //printf(\"%d, %d: tmp=%lf\\n \",t, n, tmp);\n }\n\n for(int k=1; k<=6; ++k){\n if(n-k<0) break;\n if(n-k==N) continue;\n //printf(\"n: %d, k: %d\\n\", n,k);\n\n if(lose[n-k]){\n //printf(\" go go n: %d, k: %d \", n,k);\n\n int pp=1;\n double tmp2=0.0;\n for(int i=t-2; i>=0; --i){\n tmp2+=p[i][n-k]*pp;\n pp*=-1;\n }\n //printf(\" tmp2=%lf\\n\", tmp2);\n\n p[t][n] +=tmp2/6.0;\n }\n else p[t][n] += p[t-1][n-k]/6.0;\n\n }\n\n //printf(\"p[%d][%d] = %lf\\n\",t, n, p[t][n]);\n suuuu += p[t][n];\n\n if(n>N){\n p[t][N-(n%N)] += p[t][n];\n p[t][n]=0.0;\n }\n }\n\n //printf(\"suuuu = %lf\\n\", suuuu);\n\n for(int n=1; n<N; ++n){\n if(back[n]){\n //printf(\"back %d\\n\", n);\n p[t][0]+=p[t][n];\n p[t][n]=0.0;\n }\n }\n\n /*\n double checksum=0.0;\n for(int n=0; n<=N; ++n){\n checksum+=p[t][n];\n printf(\"p[%d][%d] = %.10lf\\n\", t, n, p[t][n]);\n }\n printf(\" ch=%.10lf\\n\", checksum);\n */\n }\n\n printf(\"%.10lf\\n\", p[T][N]);\n }\n}", "accuracy": 1, "time_ms": 20, "memory_kb": 1304, "score_of_the_acc": -0.5, "final_rank": 5 }, { "submission_id": "aoj_1277_1510706", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n \ntypedef vector<long double> V;\n \nint main () {\n int N, T, L, B;\n while (cin >> N >> T >> L >> B, N || T || L || B) {\n V lose(L,0), back(B,0);\n \n for (int i = 0; i < L; i++) cin >> lose[i];\n for (int i = 0; i < B; i++) cin >> back[i];\n \n vector<V> dp(T+3, V(N+1));\n dp[0][0] = 1;\n \n for (int i = 0; i < T; i++) {\n dp[i+1][N] += dp[i][N];\n for (int j = 0; j < N; j++) {\n if (dp[i][j] > 0) {\n for (int k = 1; k < 7; k++) {\n int next = j + k <= N ? j + k : N * 2 - (j + k);\n if (find(lose.begin(), lose.end(), next) != lose.end()) {\n dp[i+2][next] += dp[i][j] / 6;\n } else if (find(back.begin(), back.end(), next) != back.end()) {\n dp[i+1][0] += dp[i][j] / 6;\n } else {\n dp[i+1][next] += dp[i][j] / 6;\n }\n }\n }\n }\n }\n \n printf(\"%.6Lf\\n\", dp[T][N]);\n }\n \n return 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 1384, "score_of_the_acc": -0.0368, "final_rank": 4 }, { "submission_id": "aoj_1277_1509745", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\ntypedef vector<double> V;\n\nint main () {\n int N, T, L, B;\n while (cin >> N >> T >> L >> B, N || T || L || B) {\n V lose(L,0), back(B,0);\n\n for (int i = 0; i < L; i++) cin >> lose[i];\n for (int i = 0; i < B; i++) cin >> back[i];\n\n vector<V> dp(T+3, V(N+1));\n dp[0][0] = 1;\n\n for (int i = 0; i < T; i++) {\n dp[i+1][N] += dp[i][N];\n for (int j = 0; j < N; j++) {\n if (dp[i][j] > 0) {\n for (int k = 1; k < 7; k++) {\n int next = j + k <= N ? j + k : N * 2 - (j + k);\n if (find(lose.begin(), lose.end(), next) != lose.end()) {\n dp[i+2][next] += dp[i][j] / 6;\n } else if (find(back.begin(), back.end(), next) != back.end()) {\n dp[i+1][0] += dp[i][j] / 6;\n } else {\n dp[i+1][next] += dp[i][j] / 6;\n }\n }\n }\n }\n }\n\n printf(\"%.6f\\n\", dp[T][N]);\n }\n\n return 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 1344, "score_of_the_acc": -0.0184, "final_rank": 2 }, { "submission_id": "aoj_1277_1508141", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\ntypedef vector<double> V;\n\nint main () {\n int N, T, L, B;\n while (cin >> N >> T >> L >> B, N || T || L || B) {\n V lose(L,0), back(B,0);\n\n for (int i = 0; i < L; i++) cin >> lose[i];\n for (int i = 0; i < B; i++) cin >> back[i];\n\n vector<V> dp(T+3, V(N+1));\n dp[0][0] = 1.L;\n\n for (int i = 0; i < T; i++) {\n dp[i+1][N] += dp[i][N];\n for (int j = 0; j < N; j++) {\n if (dp[i][j] > 0) {\n for (int k = 1; k < 7; k++) {\n int next = j + k <= N ? j + k : N * 2 - (j + k);\n if (find(lose.begin(), lose.end(), next) != lose.end()) {\n dp[i+2][next] += dp[i][j] / 6;\n } else if (find(back.begin(), back.end(), next) != back.end()) {\n dp[i+1][0] += dp[i][j] / 6;\n } else {\n dp[i+1][next] += dp[i][j] / 6;\n }\n }\n }\n }\n }\n\n printf(\"%.6f\\n\", dp[T][N]);\n }\n\n return 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 1344, "score_of_the_acc": -0.0184, "final_rank": 2 }, { "submission_id": "aoj_1277_1356192", "code_snippet": "// Header {{{\n// includes {{{\n#include <algorithm>\n#include <cassert>\n#include <cctype>\n#include <climits>\n#include <cmath>\n#include <complex>\n#include <cstdio>\n#include <cstdlib>\n#include <cstring>\n#include <ctime>\n#include <iostream>\n#include <iterator>\n#include <limits>\n#include <list>\n#include <map>\n#include <numeric>\n#include <queue>\n#include <set>\n#include <sstream>\n#include <stack>\n#include <string>\n#include <sys/time.h>\n#include <unistd.h>\n#include <vector>\n// }}}\nusing namespace std;\n// consts {{{\nstatic const int INF = 1e9;\nstatic const double PI = acos(-1.0);\nstatic const double EPS = 1e-10;\n// }}}\n// typedefs {{{\ntypedef long long int LL;\ntypedef unsigned long long int ULL;\ntypedef vector<int> VI;\ntypedef vector<VI> VVI;\ntypedef vector<LL> VLL;\ntypedef vector<VLL> VVLL;\ntypedef vector<ULL> VULL;\ntypedef vector<VULL> VVULL;\ntypedef vector<double> VD;\ntypedef vector<VD> VVD;\ntypedef vector<bool> VB;\ntypedef vector<VB> VVB;\ntypedef vector<char> VC;\ntypedef vector<VC> VVC;\ntypedef vector<string> VS;\ntypedef vector<VS> VVS;\ntypedef pair<int, int> PII;\ntypedef complex<int> P;\n// }}}\n// macros & inline functions {{{\n// syntax sugars {{{\n#define FOR(i, b, e) for (typeof(e) i = (b); i < (e); ++i)\n#define FORI(i, b, e) for (typeof(e) i = (b); i <= (e); ++i)\n#define REP(i, n) FOR(i, 0, n)\n#define REPI(i, n) FORI(i, 0, n)\n#define OPOVER(_op, _type) inline bool operator _op (const _type &t) const\n// }}}\n// conversion {{{\ninline int toInt(string s) { int v; istringstream sin(s); sin>>v; return v; }\ntemplate<class T> inline string toString(T x) { ostringstream sout; sout<<x; return sout.str(); }\n// }}}\n// array and STL {{{\n#define ARRSIZE(a) ( sizeof(a) / sizeof(a[0]) )\n#define ZERO(a, v) ( assert(v == 0 || v == -1), memset(a, v, sizeof(a)) )\n#define F first\n#define S second\n#define MP(a, b) make_pair(a, b)\n#define SIZE(a) ((LL)a.size())\n#define PB(e) push_back(e)\n#define SORT(v) sort((v).begin(), (v).end())\n#define RSORT(v) sort((v).rbegin(), (v).rend())\n#define ALL(a) (a).begin(), (a).end()\n#define RALL(a) (a).rbegin(), (a).rend()\n#define EACH(c, it) for(typeof((c).begin()) it=(c).begin(); it!=(c).end(); ++it)\n#define REACH(c, it) for(typeof((c).rbegin()) it=(c).rbegin(); it!=(c).rend(); ++it)\n#define EXIST(s, e) ((s).find(e) != (s).end())\n// }}}\n// bit manipulation {{{\n// singed integers are not for bitwise operations, specifically arithmetic shifts ('>>', and maybe not good for '<<' too)\n#define IS_UNSIGNED(n) (!numeric_limits<typeof(n)>::is_signed)\n#define BIT(n) (assert(IS_UNSIGNED(n)), assert(n < 64), (1ULL << (n)))\n#define BITOF(n, m) (assert(IS_UNSIGNED(n)), assert(m < 64), ((ULL)(n) >> (m) & 1))\ninline int BITS_COUNT(ULL b) { int c = 0; while(b != 0) { c += (b & 1); b >>= 1; } return c; }\ninline int MSB(ULL b) { int c = 0; while(b != 0) { ++c; b >>= 1; } return c-1; }\ninline int MAKE_MASK(ULL upper, ULL lower) { assert(lower < 64 && upper < 64 && lower <= upper); return (BIT(upper) - 1) ^ (BIT(lower) - 1); }\n// }}}\n// for readable code {{{\n#define EVEN(n) (n % 2 == 0)\n#define ODD(n) (!EVEN(n))\n// }}}\n// debug {{{\n#define arrsz(a) ( sizeof(a) / sizeof(a[0]) )\n#define dprt(fmt, ...) if (opt_debug) { fprintf(stderr, fmt, ##__VA_ARGS__); }\n#define darr(a) if (opt_debug) { copy( (a), (a) + arrsz(a), ostream_iterator<int>(cerr, \" \") ); cerr << endl; }\n#define darr_range(a, f, t) if (opt_debug) { copy( (a) + (f), (a) + (t), ostream_iterator<int>(cerr, \" \") ); cerr << endl; }\n#define dvec(v) if (opt_debug) { copy( ALL(v), ostream_iterator<int>(cerr, \" \") ); cerr << endl; }\n#define darr2(a) if (opt_debug) { FOR(__i, 0, (arrsz(a))){ darr( (a)[__i] ); } }\n#define WAIT() if (opt_debug) { string _wait_; cerr << \"(hit return to continue)\" << endl; getline(cin, _wait_); }\n#define dump(x) if (opt_debug) { cerr << \" [L\" << __LINE__ << \"] \" << #x << \" = \" << (x) << endl; }\n#define dumpl(x) if (opt_debug) { cerr << \" [L\" << __LINE__ << \"] \" << #x << endl << (x) << endl; }\n#define dumpf() if (opt_debug) { cerr << __PRETTY_FUNCTION__ << endl; }\n#define where() if (opt_debug) { cerr << __FILE__ << \": \" << __PRETTY_FUNCTION__ << \" [L: \" << __LINE__ << \"]\" << endl; }\n#define show_bits(b, s) if(opt_debug) { REP(i, s) { cerr << BITOF(b, s-1-i); if(i%4 == 3) cerr << ' '; } cerr << endl; }\n\n// ostreams {{{\n// complex\ntemplate<typename T> ostream& operator<<(ostream& s, const complex<T>& d) {return s << \"(\" << d.real() << \", \" << d.imag() << \")\";}\n\n// pair\ntemplate<typename T1, typename T2> ostream& operator<<(ostream& s, const pair<T1, T2>& d) {return s << \"(\" << d.first << \", \" << d.second << \")\";}\n\n// vector\ntemplate<typename T> ostream& operator<<(ostream& s, const vector<T>& d) {\n\tint len = d.size();\n\tREP (i, len) {\n\t\ts << d[i]; if (i < len - 1) s << \"\\t\";\n\t}\n\treturn s;\n}\n\n// 2 dimentional vector\ntemplate<typename T> ostream& operator<<(ostream& s, const vector< vector<T> >& d) {\n\tint len = d.size();\n\tREP (i, len) {\n\t\ts << d[i] << endl;\n\t}\n\treturn s;\n}\n\n// map\ntemplate<typename T1, typename T2> ostream& operator<<(ostream& s, const map<T1, T2>& m) {\n\ts << \"{\" << endl;\n\tfor (typeof(m.begin()) itr = m.begin(); itr != m.end(); ++itr) {\n\t\ts << \"\\t\" << (*itr).first << \" : \" << (*itr).second << endl;\n\t}\n\ts << \"}\" << endl;\n\treturn s;\n}\n// }}}\n// }}}\n// }}}\n// time {{{\ninline double now(){ struct timeval tv; gettimeofday(&tv, NULL); return (static_cast<double>(tv.tv_sec) + static_cast<double>(tv.tv_usec) * 1e-6); }\n// }}}\n// string manipulation {{{\ninline VS split(string s, char delimiter) { VS v; string t; REP(i, s.length()) { if(s[i] == delimiter) v.PB(t), t = \"\"; else t += s[i]; } v.PB(t); return v; }\ninline string join(VS s, string j) { string t; REP(i, s.size()) { t += s[i] + j; } return t; }\n// }}}\n// geometry {{{\n#define Y real()\n#define X imag()\n// }}}\n// 2 dimentional array {{{\nP dydx4[4] = { P(-1, 0), P(0, 1), P(1, 0), P(0, -1) };\nP dydx8[8] = { P(-1, 0), P(0, 1), P(1, 0), P(0, -1), P(-1, 1), P(1, 1), P(1, -1), P(-1, -1) };\nint g_height, g_width;\nbool in_field(P p) {\n\treturn (0 <= p.Y && p.Y < g_height) && (0 <= p.X && p.X < g_width);\n}\n// }}}\n// input and output {{{\ninline void input(string filename) {\n\tfreopen(filename.c_str(), \"r\", stdin);\n}\ninline void output(string filename) {\n\tfreopen(filename.c_str(), \"w\", stdout);\n}\n// }}}\n// }}}\n\n// Header under development {{{\n\nint LCM(int a, int b) {\n\t// FIXME\n\treturn a * b;\n}\n\n// Fraction class {{{\n// ref: http://martin-thoma.com/fractions-in-cpp/\nclass Fraction {\n\tpublic:\n\t\tULL numerator;\n\t\tULL denominator;\n\t\tFraction(ULL _numerator, ULL _denominator) {\n\t\t\tassert(_denominator > 0);\n\t\t\tnumerator = _numerator;\n\t\t\tdenominator = _denominator;\n\t\t};\n\n\t\tFraction operator*(const ULL rhs) {\n\t\t\treturn Fraction(this->numerator * rhs, this->denominator);\n\t\t};\n\n\t\tFraction operator*(const Fraction& rhs) {\n\t\t\treturn Fraction(this->numerator * rhs.numerator, this->denominator * rhs.denominator);\n\t\t}\n\n\t\tFraction operator+(const Fraction& rhs) {\n\t\t\tULL lcm = LCM(this->denominator, rhs.denominator);\n\t\t\tULL numer_lhs = this->numerator * (this->denominator / lcm);\n\t\t\tULL numer_rhs = rhs.numerator * (rhs.numerator / lcm);\n\t\t\treturn Fraction(numer_lhs + numer_rhs, lcm);\n\t\t}\n\n\t\tFraction& operator+=(const Fraction& rhs) {\n\t\t\tFraction result = (*this) + rhs;\n\t\t\tthis->numerator = result.numerator;\n\t\t\tthis->denominator = result.denominator;\n\t\t\treturn *this;\n\t\t}\n};\n\nstd::ostream& operator<<(std::ostream &s, const Fraction &a) {\n\tif (a.denominator == 1) {\n\t\ts << a.numerator;\n\t} else {\n\t\ts << a.numerator << \"/\" << a.denominator;\n\t}\n\treturn s;\n}\n\n// }}}\n\n// }}}\n\nbool opt_debug = false;\n\nint main(int argc, char** argv) {\n\tstd::ios_base::sync_with_stdio(false);\n\t// set options {{{\n\tint __c;\n\twhile ( (__c = getopt(argc, argv, \"d\")) != -1 ) {\n\t\tswitch (__c) {\n\t\t\tcase 'd':\n\t\t\t\topt_debug = true;\n\t\t\t\tbreak;\n\t\t\tdefault:\n\t\t\t\tabort();\n\t\t}\n\t}\n\t// }}}\n\n\t// input(\"./inputs/0.txt\");\n\t// output(\"./outputs/0.txt\");\n\n\tenum _filed_type {\n\t\tNOTHING,\n\t\tLOSE,\n\t\tBACK\n\t};\n\n\tint N, T, L, B;\n\twhile (cin >> N >> T >> L >> B, N | T | L | B) {\n\t\tVI field(N+1, NOTHING);\n\t\tREP (i, L) {\n\t\t\tint l; cin >> l;\n\t\t\tfield[l] = LOSE;\n\t\t}\n\t\tREP (i, B) {\n\t\t\tint b; cin >> b;\n\t\t\tfield[b] = BACK;\n\t\t}\n\n\t\tVVD dp(T+1, VD(N+1, 0));\n\t\tdp[0][0] = 1;\n\t\tFOR (t, 1, T + 1) {\n\t\t\tREP (n, N + 1) {\n\t\t\t\tFOR (d, 1, 7) { // dice\n\t\t\t\t\tVI prevs;\n\t\t\t\t\tint p1 = n - d;\n\t\t\t\t\tint p2 = N - (d - (N - n));\n\t\t\t\t\tprevs.PB(n - d);\n\t\t\t\t\tif (p2 != p1) {\n\t\t\t\t\t\tprevs.PB(N - (d - (N - n)));\n\t\t\t\t\t}\n\n\t\t\t\t\tREP (i, prevs.size()) {\n\t\t\t\t\t\tint prev = prevs[i];\n\t\t\t\t\t\tif (0 <= prev && prev < N) {\n\t\t\t\t\t\t\tif (field[n] == LOSE) {\n\t\t\t\t\t\t\t\tif (t-2 >= 0) {\n\t\t\t\t\t\t\t\t\tdp[t][n] += dp[t-2][prev] * (1 / 6.0);\n\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t} else if (field[n] == BACK) {\n\t\t\t\t\t\t\t\tdp[t][0] += dp[t-1][prev] * (1 / 6.0);\n\t\t\t\t\t\t\t} else {\n\t\t\t\t\t\t\t\tdp[t][n] += dp[t-1][prev] * (1 / 6.0);\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\n\t\tdouble ans = 0;\n\t\tFOR (t, 1, T+1) {\n\t\t\tans += dp[t][N];\n\t\t}\n\t\tprintf(\"%.10lf\\n\", ans);\n\t}\n\n\treturn 0;\n}\n\n// vim: foldmethod=marker", "accuracy": 1, "time_ms": 30, "memory_kb": 1364, "score_of_the_acc": -1.0276, "final_rank": 11 }, { "submission_id": "aoj_1277_1338062", "code_snippet": "#include <iostream>\n#include <algorithm>\n#include <vector>\n#include <stack>\n#include <queue>\n#include <map>\n#include <cmath>\n#include <string>\n#include <sstream>\n#include <iomanip>\n#include <complex>\nusing namespace std;\n\n#define ll long long\n#define vvi vector< vector<int> >\n#define vi vector<int>\n#define All(X) X.begin(),X.end()\n#define FOR(i,a,b) for(int i=(int)(a);i<(int)(b);i++)\n#define REP(i,n) for(int i=0;i<(int)(n);i++)\n#define pb push_back\n#define pii pair<int,int>\n#define mp make_pair\n#define pi 3.14159265359\n#define shosu(X) fixed << setprecision(X)\nll gcd(ll a,ll b){return b?gcd(b,a%b):a;}\nll lcm(ll a,ll b){return a/gcd(a,b)*b;}\n\nvi B,L;\nint n,t,l,b;\nlong double dp[103][103];//dp[i][j]:= i?????????????????????j?????????\nint main(){\n\twhile(1){\n\t\tcin >> n >> t >> l >> b;\n\t\tREP(i,101) REP(j,101) dp[i][j] = 0;\n\t\tB.clear(); L.clear();\n\t\tif(n==0&&t==0&&l==0&&b==0) break;\n\t\tint tmp;\n\t\tREP(i,l){\n\t\t\tcin >> tmp;\n\t\t\tL.pb(tmp);\n\t\t}\n\t\tREP(i,b){\n\t\t\tcin >> tmp;\n\t\t\tB.pb(tmp);\n\t\t}\n\t\tdp[0][0] = 1.0;\n\t\tREP(i,t){\n\t\t\tREP(j,n){\n\t\t\t\tFOR(k,1,7){\n\t\t\t\t\tint pos = j + k;\n\t\t\t\t\tif(pos > n) pos = n - (k - (n - j));\n\t\t\t\t\tREP(w,B.size()){\n\t\t\t\t\t\tif(pos == B[w]) pos = 0;\n\t\t\t\t\t}\n\t\t\t\t\tbool Skipflag = false;\n\t\t\t\t\tREP(w,L.size()){\n\t\t\t\t\t\tif(pos == L[w]) Skipflag = true;\n\t\t\t\t\t}\n\n\t\t\t\t\tif(!Skipflag) dp[i+1][pos] += dp[i][j]*(1.0/6.0);\n\t\t\t\t\telse dp[i+2][pos] += dp[i][j]*(1.0/6.0);\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\tlong double ans = 0;\n\t\tFOR(i,1,t+1) ans += dp[i][n];\n\t\tcout << shosu(10) << ans << endl;\n\n\t}\n}", "accuracy": 1, "time_ms": 20, "memory_kb": 1456, "score_of_the_acc": -0.5699, "final_rank": 6 } ]
aoj_1271_cpp
Problem F: Power Calculus Starting with x and repeatedly multiplying by x , we can compute x 31 with thirty multiplications: x 2 = x × x , x 3 = x 2 × x , x 4 = x 3 × x , ... , x 31 = x 30 × x . The operation of squaring can appreciably shorten the sequence of multiplications. The following is a way to compute x 31 with eight multiplications: x 2 = x × x , x 3 = x 2 × x , x 6 = x 3 × x 3 , x 7 = x 6 × x , x 14 = x 7 × x 7 , x 15 = x 14 × x , x 30 = x 15 × x 15 , x 31 = x 30 × x . This is not the shortest sequence of multiplications to compute x 31 . There are many ways with only seven multiplications. The following is one of them: x 2 = x × x , x 4 = x 2 × x 2 , x 8 = x 4 × x 4 , x 10 = x 8 × x 2 , x 20 = x 10 × x 10 , x 30 = x 20 × x 10 , x 31 = x 30 × x . There however is no way to compute x 31 with fewer multiplications. Thus this is one of the most eficient ways to compute x 31 only by multiplications. If division is also available, we can find a shorter sequence of operations. It is possible to compute x 31 with six operations (five multiplications and one division): x 2 = x × x , x 4 = x 2 × x 2 , x 8 = x 4 × x 4 , x 16 = x 8 × x 8 , x 32 = x 16 × x 16 , x 31 = x 32 ÷ x . This is one of the most eficient ways to compute x 31 if a division is as fast as a multiplication. Your mission is to write a program to find the least number of operations to compute x n by multiplication and division starting with x for the given positive integer n . Products and quotients appearing in the sequence of operations should be x to a positive integer's power. In other words, x -3 , for example, should never appear. Input The input is a sequence of one or more lines each containing a single integer n . n is positive and less than or equal to 1000. The end of the input is indicated by a zero. Output Your program should print the least total number of multiplications and divisions required to compute x n starting with x for the integer n . The numbers should be written each in a separate line without any superfluous characters such as leading or trailing spaces. Sample Input 1 31 70 91 473 512 811 953 0 Output for the Sample Input 0 6 8 9 11 9 13 12
[ { "submission_id": "aoj_1271_10852802", "code_snippet": "/*\n *Author: Zhaofa Fang\n *Created time: 2013-09-09-16.49 星期一\n */\n#include <cstdio>\n#include <cstdlib>\n#include <sstream>\n#include <iostream>\n#include <cmath>\n#include <cstring>\n#include <algorithm>\n#include <string>\n#include <utility>\n#include <vector>\n#include <queue>\n#include <map>\n#include <set>\nusing namespace std;\n\ntypedef long long ll;\ntypedef pair<int,int> PII;\n#define DEBUG(x) cout<< #x << ':' << x << endl\n#define FOR(i,s,t) for(int i = (s);i <= (t);i++)\n#define FORD(i,s,t) for(int i = (s);i >= (t);i--)\n#define REP(i,n) for(int i=0;i<(n);i++)\n#define REPD(i,n) for(int i=(n-1);i>=0;i--)\n#define PII pair<int,int>\n#define PB push_back\n#define ft first\n#define sd second\n#define lowbit(x) (x&(-x))\n#define INF (1<<30)\n#define eps (1e-8)\n\nint n,ans;\nint A[500];\nbool vist[3000];\nint step[3000];\n\nvoid dfs(int cur,int tot){\n if(tot >= ans || cur > 2*n || (~step[cur]&&tot>step[cur]))return;\n if(cur == n){\n ans = min(ans,tot);\n return;\n }\n if(step[cur] == -1)step[cur] = tot;\n else if(step[cur] > tot)step[cur] = tot;\n // printf(\"%d %d %d\\n\",cur,tot,ans);\n REPD(i,tot+1){\n int xx = cur + A[i];\n if(xx > 2*n || vist[xx])continue;\n vist[xx] = 1;A[tot+1] = xx;\n dfs(xx,tot+1);\n vist[xx] = 0;\n }\n REPD(i,tot+1){\n int xx = cur - A[i];\n if(xx <=0 || vist[xx])continue;\n vist[xx] = 1;A[tot+1] = xx;\n dfs(xx,tot+1);\n vist[xx] = 0;\n }\n}\nint main(){\n //freopen(\"in\",\"r\",stdin);\n //freopen(\"out\",\"w\",stdout);\n // FOR(i,1,1000)\n while(~scanf(\"%d\",&n),n)\n {\n memset(step,-1,sizeof(step));\n // n = i;\n ans = 14;//BFS();//DEBUG(ans);\n A[0] = 1;\n dfs(1,0);\n printf(\"%d\\n\",ans);\n // if(n%40==0)puts(\"\");\n }\n return 0;\n}", "accuracy": 1, "time_ms": 1060, "memory_kb": 3264, "score_of_the_acc": -1.7577, "final_rank": 18 }, { "submission_id": "aoj_1271_10566772", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\nconst int N = 106;\nint x[N], n, m;\n\nbool dfs(int k, int s) {\n if (k == n) return true;\n if (s == m) return false;\n if (k < n && (k * (1 << (m - s)) < n)) return false;\n\n x[s] = k;\n for (int i = 0; i <= s; ++i) {\n int new_val1 = k + x[i];\n if (new_val1 <= 10000) { // Prevent overflow\n bool found = false;\n for (int j = 0; j <= s; ++j) {\n if (x[j] == new_val1) {\n found = true;\n break;\n }\n }\n if (!found) {\n if (dfs(new_val1, s + 1)) return true;\n }\n }\n\n int new_val2 = abs(k - x[i]);\n if (new_val2 > 0 && new_val2 <= 10000) {\n bool found = false;\n for (int j = 0; j <= s; ++j) {\n if (x[j] == new_val2) {\n found = true;\n break;\n }\n }\n if (!found) {\n if (dfs(new_val2, s + 1)) return true;\n }\n }\n }\n return false;\n}\n\nint main() {\n ios::sync_with_stdio(false);\n cin.tie(0), cout.tie(0);\n while (cin >> n && n) {\n if (n == 1) {\n cout << \"0\\n\";\n continue;\n }\n m = 0;\n while (!dfs(1, 0)) ++m;\n cout << m << \"\\n\";\n }\n return 0;\n}", "accuracy": 1, "time_ms": 360, "memory_kb": 3368, "score_of_the_acc": -1.103, "final_rank": 14 }, { "submission_id": "aoj_1271_10563404", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n#define _for(i, a, b) for (int i = (a); i < (int)(b); ++i)\nconst int maxans = 13; // 试验出来的结果\nint N, A[maxans + 1];\nbool dfs(int d, int maxd) {\n if (A[d] == N) return true;\n if (d == maxd) return false;\n int maxv = *max_element(A, A + d + 1);\n if ((maxv << (maxd - d)) < N) return false; // 剪枝\n for (int i = d; i >= 0; i--) { // 总是使用最后一个值和前面的某个i值做加减\n A[d + 1] = A[d] + A[i]; // 无法证明,也没找到反例\n if (dfs(d + 1, maxd)) return true;\n A[d + 1] = A[d] - A[i]; // 减去前面的某个值生成一个新的次数\n if (dfs(d + 1, maxd)) return true;\n }\n return false;\n}\nint solve(int N) {\n if (N == 1) return 0;\n A[0] = 1;\n _for(maxd, 1, maxans) if (dfs(0, maxd)) return maxd;\n return maxans;\n}\nint main() {\n while (scanf(\"%d\", &N) == 1 && N) printf(\"%d\\n\", solve(N));\n return 0;\n}", "accuracy": 1, "time_ms": 180, "memory_kb": 3484, "score_of_the_acc": -1.033, "final_rank": 12 }, { "submission_id": "aoj_1271_9580788", "code_snippet": "#include <algorithm>\n#include <cmath>\n#include <cstdlib>\n#include <iomanip>\n#include <iostream>\n#include <list>\n#include <map>\n#include <numeric>\n#include <queue>\n#include <set>\n#include <stack>\n#include <string>\n#include <unordered_set>\n#include <vector>\n\nusing namespace std;\nusing ll = long long;\n\n// 現在の演算回数 c と演算履歴 history から x^target を upper_limit 回の演算以内で計算できるならば true を返す。そうでなければ false を返す。\nbool Dfs(const int c, vector<int>& history, const int target, int& upper_limit) {\n\tif (history[c] == target) return true;\n\tif (c == upper_limit) return false;\n\n\tint max = *max_element(history.begin(), history.begin() + c + 1);\n\tif ((max << (upper_limit - c)) < target) return false;\n\n\tfor (int i = 0; i <= c; i++) {\n\t\tif (i != c and history[i] == history[c]) return false;\n\n\t\thistory[c + 1] = history[i] + history[c];\n\t\tif (Dfs(c + 1, history, target, upper_limit)) return true;\n\n\t\tif (history[i] < history[c]) {\n\t\t\thistory[c + 1] = history[c] - history[i];\n\t\t\tif (Dfs(c + 1, history, target, upper_limit)) return true;\n\t\t}\n\t}\n\treturn false;\n}\n\nint Solve(const int n) {\n\t// 反復深化深さ優先探索\n\tfor (int upper_limit = 0;; upper_limit++) {\n\t\tvector<int> history(upper_limit + 1, {1}); // i 回目の演算で x^(history[i]) が得られたという履歴\n\t\tif (Dfs(0, history, n, upper_limit)) return upper_limit;\n\t}\n}\n\nint main(void) {\n\twhile (true) {\n\t\tint n;\n\t\tcin >> n;\n\t\tif (n == 0) break;\n\t\tcout << Solve(n) << endl;\n\t}\n\treturn 0;\n}", "accuracy": 1, "time_ms": 370, "memory_kb": 3124, "score_of_the_acc": -0.8453, "final_rank": 9 }, { "submission_id": "aoj_1271_9580699", "code_snippet": "#include <algorithm>\n#include <cmath>\n#include <cstdlib>\n#include <iomanip>\n#include <iostream>\n#include <list>\n#include <map>\n#include <numeric>\n#include <queue>\n#include <set>\n#include <stack>\n#include <string>\n#include <unordered_set>\n#include <vector>\n\n#define INF (17)\n\nusing namespace std;\nusing ll = long long;\n\n// 現在の演算回数 c と演算履歴 history から x^target を upper_limit 回の演算以内で計算できるならば true を返す。そうでなければ false を返す。\nbool Dfs(const int c, array<int, INF>& history, const int target, int& upper_limit) {\n\tif (history[c] == target) return true;\n\tif (c == upper_limit) return false;\n\n\tint max = *max_element(history.begin(), history.begin() + c + 1);\n\tif ((max << (upper_limit - c)) < target) return false;\n\n\tfor (int i = 0; i <= c; i++) {\n\t\tif (i != c and history[i] == history[c]) return false;\n\n\t\thistory[c + 1] = history[i] + history[c];\n\t\tif (Dfs(c + 1, history, target, upper_limit)) return true;\n\n\t\tif (history[i] < history[c]) {\n\t\t\thistory[c + 1] = history[c] - history[i];\n\t\t\tif (Dfs(c + 1, history, target, upper_limit)) return true;\n\t\t}\n\t}\n\treturn false;\n}\n\nint Solve(const int n) {\n\t// 反復深化深さ優先探索\n\tfor (int upper_limit = 0;; upper_limit++) {\n\t\tarray<int, INF> history{1}; // i 回目の演算で x^(history[i]) が得られたという履歴\n\t\tif (Dfs(0, history, n, upper_limit)) return upper_limit;\n\t}\n}\n\nint main(void) {\n\twhile (true) {\n\t\tint n;\n\t\tcin >> n;\n\t\tif (n == 0) break;\n\t\tcout << Solve(n) << endl;\n\t}\n\treturn 0;\n}", "accuracy": 1, "time_ms": 290, "memory_kb": 3432, "score_of_the_acc": -1.0966, "final_rank": 13 }, { "submission_id": "aoj_1271_9580687", "code_snippet": "#include <algorithm>\n#include <cmath>\n#include <cstdlib>\n#include <iomanip>\n#include <iostream>\n#include <list>\n#include <map>\n#include <numeric>\n#include <queue>\n#include <set>\n#include <stack>\n#include <string>\n#include <unordered_set>\n#include <vector>\n\n#define INF (17)\n\nusing namespace std;\nusing ll = long long;\n\nbool ChMinZP(int& a, const int b) {\n\tif ((a < 0 or a > b) and b >= 0) {\n\t\ta = b;\n\t\treturn true;\n\t}\n\treturn false;\n}\n\nint CalcUpperLimit(int n) {\n\tint popcount = 0, numof_digits = 0;\n\tfor (int i = 1; n != 0; i++, n >>= 1) {\n\t\tif (n & 1) {\n\t\t\tpopcount++;\n\t\t\tnumof_digits = i;\n\t\t}\n\t}\n\tint upper_limit = numof_digits + popcount - 2;\n\treturn upper_limit;\n}\n\n// 現在の演算回数 c と演算履歴 history から x^target を upper_limit 回の演算以内で計算できるならば、その演算回数を返す。そうでなければ -1 を返す。\nbool Dfs(const int c, array<int, INF>& history, const int target, int& upper_limit) {\n\tif (history[c] == target) return true;\n\tif (c == upper_limit) return false;\n\n\tint max = *max_element(history.begin(), history.begin() + c + 1);\n\tif ((max << (upper_limit - c)) < target) return false;\n\n\tfor (int i = 0; i <= c; i++) {\n\t\tif (i != c and history[i] == history[c]) return false;\n\n\t\thistory[c + 1] = history[i] + history[c];\n\t\tif (Dfs(c + 1, history, target, upper_limit)) return true;\n\n\t\tif (history[i] < history[c]) {\n\t\t\thistory[c + 1] = history[c] - history[i];\n\t\t\tif (Dfs(c + 1, history, target, upper_limit)) return true;\n\t\t}\n\t}\n\treturn false;\n}\n\nint Solve(const int n) {\n\t// 反復深化深さ優先探索\n\tfor (int upper_limit = 0;; upper_limit++) {\n\t\tarray<int, INF> history{1}; // i 回目の演算で x^(history[i]) が得られたという履歴\n\t\tif (Dfs(0, history, n, upper_limit)) return upper_limit;\n\t}\n}\n\nint main(void) {\n\twhile (true) {\n\t\tint n;\n\t\tcin >> n;\n\t\tif (n == 0) break;\n\t\tcout << Solve(n) << endl;\n\t}\n\treturn 0;\n}", "accuracy": 1, "time_ms": 350, "memory_kb": 3440, "score_of_the_acc": -1.1713, "final_rank": 17 }, { "submission_id": "aoj_1271_9580588", "code_snippet": "#include <algorithm>\n#include <cmath>\n#include <cstdlib>\n#include <iomanip>\n#include <iostream>\n#include <list>\n#include <map>\n#include <numeric>\n#include <queue>\n#include <set>\n#include <stack>\n#include <string>\n#include <unordered_set>\n#include <vector>\n\n#define INF (17)\n\nusing namespace std;\nusing ll = long long;\n\nll counter = 0;\n\nbool ChMinZP(int& a, const int b) {\n\tif ((a < 0 or a > b) and b >= 0) {\n\t\ta = b;\n\t\treturn true;\n\t}\n\treturn false;\n}\n\nint CalcUpperLimit(int n) {\n\tint popcount = 0, numof_digits = 0;\n\tfor (int i = 1; n != 0; i++, n >>= 1) {\n\t\tif (n & 1) {\n\t\t\tpopcount++;\n\t\t\tnumof_digits = i;\n\t\t}\n\t}\n\tint upper_limit = numof_digits + popcount - 2;\n\treturn upper_limit;\n}\n\n// // 現在の演算回数 c と演算履歴 history から x^target を upper_limit 回の演算以内で計算できるならば、その演算回数を返す。そうでなければ -1 を返す。\n// int Dfs(const int c, array<int, INF>& history, const int target, int& upper_limit) {\n// \tcounter++;\n// \tif (history[c] == target) return c;\n// \tif (c == upper_limit) return -1;\n\n// \tint max = *max_element(history.begin(), history.begin() + c + 1);\n// \tif ((max << (upper_limit - c)) < target) return -1;\n\n// \tint ans = -1;\n// \tfor (int i = 0; i <= c; i++) {\n// \t\tif (i != c and history[i] == history[c]) return -1;\n\n// \t\thistory[c + 1] = history[i] + history[c];\n// \t\tif (ChMinZP(ans, Dfs(c + 1, history, target, upper_limit))) upper_limit = ans;\n\n// \t\tif (history[i] < history[c]) {\n// \t\t\thistory[c + 1] = history[c] - history[i];\n// \t\t\tif (ChMinZP(ans, Dfs(c + 1, history, target, upper_limit))) upper_limit = ans;\n// \t\t}\n// \t}\n// \treturn ans;\n// }\n\n// 現在の演算回数 c と演算履歴 history から x^target を upper_limit 回の演算以内で計算できるならば、その演算回数を返す。そうでなければ -1 を返す。\nbool Dfs(const int c, array<int, INF>& history, const int target, int& upper_limit) {\n\tcounter++;\n\tif (history[c] == target) return true;\n\tif (c == upper_limit) return false;\n\n\tint max = *max_element(history.begin(), history.begin() + c + 1);\n\tif ((max << (upper_limit - c)) < target) return false;\n\n\tfor (int i = 0; i <= c; i++) {\n\t\tif (i != c and history[i] == history[c]) return false;\n\n\t\thistory[c + 1] = history[i] + history[c];\n\t\tif (Dfs(c + 1, history, target, upper_limit)) return true;\n\n\t\tif (history[i] < history[c]) {\n\t\t\thistory[c + 1] = history[c] - history[i];\n\t\t\tif (Dfs(c + 1, history, target, upper_limit)) return true;\n\t\t}\n\t}\n\treturn false;\n}\n\nint Solve(const int n) {\n\t// array<int, INF> history{1};\t\t\t // i 回目の演算で x^(history[i]) が得られたという履歴\n\t// int upper_limit = CalcUpperLimit(n); // 求める演算回数の上限\n\t// int ans = Dfs(0, history, n, upper_limit);\n\t// return ans;\n\n\tfor (int ul = 0;; ul++) {\n\t\tarray<int, INF> history{1}; // i 回目の演算で x^(history[i]) が得られたという履歴\n\t\tif (Dfs(0, history, n, ul)) return ul;\n\t}\n}\n\nint main(void) {\n\twhile (true) {\n\t\tint n;\n\t\tcin >> n;\n\t\tif (n == 0) break;\n\t\tcout << Solve(n) << endl;\n\t\t// cout << counter << endl;\n\t}\n\treturn 0;\n}", "accuracy": 1, "time_ms": 320, "memory_kb": 3056, "score_of_the_acc": -0.7154, "final_rank": 8 }, { "submission_id": "aoj_1271_9496620", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\n\nint d,n;\nint a[1001];\nbool dfs(int x){\n\tif(x==d)return a[x-1]==n;\n\tfor(int i=x-1;i>=0;i--){\n\t\tint t=a[x-1]+a[i];\n\t\tif(t>n)continue;\n\t\ta[x]=t;\n\t\tfor(int i=x+1;i<=d;i++){\n\t\t\tt*=2;\n\t\t}\n\t\tif(t<n) return 0;\n\t\tif(dfs(x+1))return 1;\n\t}\n\tfor(int i=0;i<=x-1;i++){\n\t\tint t=a[x-1]-a[i];\n\t\tif(t<=0)continue;\n\t\ta[x]=t;\n//\t\tfor(int i=x+1;i<=d;i++){\n//\t\t\tt*=2;\n//\t\t}\n//\t\tif(t<n) return 0;\n\t\tif(dfs(x+1))return 1;\n\t}\n\treturn 0;\n}\nint main(){\n\ta[0]=1;\n\twhile(cin>>n,n){\n\t\td=1;\n\t\twhile(!dfs(1))d++;\n\t\tcout<<d-1<<endl;\n//\t\tfor(int i=0;i<d;i++){\n//\t\t\tcout<<a[i]<<\" \";\n//\t\t}cout<<endl;\n\t}\n\t\n}", "accuracy": 1, "time_ms": 580, "memory_kb": 3208, "score_of_the_acc": -1.1686, "final_rank": 16 }, { "submission_id": "aoj_1271_8989970", "code_snippet": "#include <iostream>\n#include <cstdio>\n#include <cstdlib>\n#include <cmath>\n#include <algorithm>\n#include <cstring>\n#include <vector>\n#include <string>\n#include <queue>\n#include <map>\n#include <set>\n\n#define FRER() freopen(\"in.txt\", \"r\", stdin);\n#define INF 0x3f3f3f3f\n\nusing namespace std;\n\nconst int maxn = 10000 + 5;\n\nint num[maxn], vis[maxn], temp[50], maxd, n;\n\nbool dfs(int, int);\n\nint main()\n{\n //FRER()\n ios::sync_with_stdio(0);\n cin.tie(0);\n\n temp[0] = 1;\n for(int i = 1; i < 32; ++ i)\n temp[i] = temp[i - 1] << 1;\n while(cin >> n && n) {\n memset(vis, 0, sizeof(vis));\n for(maxd = 0; ; ++maxd)\n if(dfs(0, 1)) break;\n cout << maxd << endl;\n }\n return 0;\n}\n\nbool dfs(int cur, int s) {\n if(cur == maxd && s == n) return true;\n if(cur > maxd) return false;\n if(s * temp[maxd - cur] < n) return false;\n num[cur] = s;\n vis[s] = 1;\n for(int i = 0; i <= cur; ++i) {\n int u = num[i] + s;\n if(!vis[u]) {\n vis[u] = 1;\n if(dfs(cur + 1, u)) return true;\n vis[u] = 0;\n }\n u = abs(num[i] - s);\n if(!vis[u]) {\n vis[u] = 1;\n if(dfs(cur + 1, u)) return true;\n vis[u] = 0;\n }\n }\n vis[s] = 0;\n return false;\n}", "accuracy": 1, "time_ms": 320, "memory_kb": 3424, "score_of_the_acc": -1.1207, "final_rank": 15 }, { "submission_id": "aoj_1271_7570484", "code_snippet": "#include<cstdio>\n#include<algorithm>\nusing namespace std;\nint cnt[/*11*/45/*14*/],n;\nint maxd;\nbool dfs(int dep,int x)\n{\n\tif (cnt[dep]==n)\treturn true;\n\tif (dep>=maxd)\treturn false;\n\tx=max(x,cnt[dep]);\n if(x*(1<<(maxd-dep))<n)\n \treturn false;\n for (int i=0;i<=dep;i++)\n {\n \tcnt[dep+1]=cnt[dep]+cnt[i];\n \tif (dfs(dep+1,x))\treturn true;\n \tif (cnt[dep]>cnt[i])\tcnt[dep+1]=cnt[dep]-cnt[i];\n \telse\tcnt[dep+1]=cnt[i]-cnt[dep];\n \tif (dfs(dep+1,x))\treturn true;\n\t}\n\treturn false;\n}\nint solve()\n{\n\tcnt[0]=1;\n\tscanf(\"%d\",&n);\n\tif (n==0)\treturn 1;\n\tif (n==1){\n\t\tprintf(\"%d\\n\",0);\n\t\treturn 0;\n\t}\n\tmaxd=0;\n\twhile (!dfs(0,0))\tmaxd++;\n\tprintf(\"%d\\n\",maxd);\n\treturn 0;\n}\nint main()\n{\n\twhile (!solve())\n\t{\n\t\tcontinue;\n\t}\n}", "accuracy": 1, "time_ms": 210, "memory_kb": 2804, "score_of_the_acc": -0.317, "final_rank": 4 }, { "submission_id": "aoj_1271_7389539", "code_snippet": "#include<iostream>\nusing namespace std;\nint n,a[16];\nbool dfs(int deep,int dep,int mx){\n\tif(a[dep-1]==n)return 1;\n\tif(dep>deep)return 0;\n\tif(mx*(1<<(deep-dep)+1)<n)return 0;\n\tfor(int i=0;i<dep;i++){\n\t\ta[dep]=a[i]+a[dep-1];\n\t\tif(dfs(deep,dep+1,max(mx,a[dep])))return 1;\n\t\ta[dep]=a[i]-a[dep-1];\n\t\tif(a[dep]<0)a[dep]=-a[dep];\n\t\tif(dfs(deep,dep+1,max(mx,a[dep])))return 1;\n\t}\n\treturn 0;\n}\nint main(){\n\ta[0]=1;\n\twhile(cin>>n&&n){\n\t\tfor(int i=0;i<=15;i++){\n\t\t\tif(dfs(i,1,1)){\n\t\t\t\tcout<<i<<endl;\n\t\t\t\tbreak;\n\t\t\t}\n\t\t}\n\t}\n\treturn 0;\n}", "accuracy": 1, "time_ms": 190, "memory_kb": 3364, "score_of_the_acc": -0.9118, "final_rank": 11 }, { "submission_id": "aoj_1271_7350861", "code_snippet": "#include<cmath>\n#include<cstdio>\n#include<cstring>\nusing namespace std;\nint n,m[20],dep;\nbool dfs(int s)\n{\n\tif(s>dep)return 0;\n\tif(m[s]==n)return 1;\n\tif(m[s]<<(dep-s)<n)return 0;\n\tfor(int i=0;i<=s;i++)\n\t{\n\t\tm[s+1]=m[s]+m[i];\n\t\tif(dfs(s+1))return 1;\n\t\tm[s+1]=abs(m[s]-m[i]);\n\t\tif(dfs(s+1))return 1;\n\t\t\n\t}\n\treturn 0;\n}\nint main()\n{\n\twhile(scanf(\"%d\",&n)&&n)\n\t{\n\t\tfor(dep=0;;dep++)\n\t\t{\n\t\t\tm[0]=1;\n\t\t\tif(dfs(0))break;\n\t\t}\n\t\tprintf(\"%d\\n\",dep);\n\t}\n\treturn 0;\n}", "accuracy": 1, "time_ms": 260, "memory_kb": 2764, "score_of_the_acc": -0.3279, "final_rank": 5 }, { "submission_id": "aoj_1271_7330784", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\nconst int N=101;\nint n,a[101],max_dep;\nbool dfs(int t,int x)\n{\n\tif(t>max_dep || x<=0 || (x<<(max_dep-t))<n) return 0;\n\tif(x==n || x<<(max_dep-t)==n ) return 1;\n\ta[t]=x;\n\tfor(int i=0;i<=t;i++)\n\t{\n\t\tif(dfs(t+1,x+a[i])) return 1;\n\t\tif(dfs(t+1,x-a[i])) return 1; \n\t}\n\treturn 0;\n}\nint main()\n{ \n\twhile(cin>>n && n)\n\t{\n\t\tmax_dep=0;\n\t\twhile(dfs(0,1)==0)\n\t\t{\n\t\t\tmax_dep++;\n\t\t}\n\t\tcout<<max_dep<<endl;\n\t}\n}", "accuracy": 1, "time_ms": 190, "memory_kb": 3360, "score_of_the_acc": -0.9074, "final_rank": 10 }, { "submission_id": "aoj_1271_6540743", "code_snippet": "#pragma GCC optimize(\"O3,unroll-loops\")\n#include <algorithm>\n#include <cstdio>\nint n, lim, a[21];\nbool Dfs(int x, int d) {\n if (d == lim) return x == n;\n a[d] = x;\n int max = *std::max_element(a, a + d + 1);\n if ((max << (lim - d)) < n) return false;\n for (int i = 0; i <= d; i++) {\n if (Dfs(x + a[i], d + 1)) return true;\n if (x > a[i] && Dfs(x - a[i], d + 1)) return true;\n }\n return false;\n}\nint main(int argc, char const *argv[]) {\n while (std::scanf(\"%d\", &n) == 1) {\n if (n == 0) break;\n for (lim = 0;; lim++)\n if (Dfs(1, 0)) {\n std::printf(\"%d\\n\", lim);\n break;\n }\n }\n return 0;\n}", "accuracy": 1, "time_ms": 340, "memory_kb": 2908, "score_of_the_acc": -0.5744, "final_rank": 7 }, { "submission_id": "aoj_1271_4945835", "code_snippet": "// 先预处理好 n = 1...1000 的答案,在询问时一次输出 \n// 剪枝:append一个数v时, 如果v的深度大于它的最浅深度就不优. \n#include <iostream>\n#include <cstdio>\n#include <cstring>\n#include <algorithm>\n#include <cmath>\n#include <utility>\n#include <queue>\nusing namespace std;\ntypedef long long ll;\ntypedef unsigned long long ull;\ntypedef long double ldouble;\nconst int LEN = 100000;\n\nstruct fastio {\n int it, len;\n char s[LEN + 5];\n fastio() {\n it = len = 0;\n }\n char get() {\n if (it < len) return s[it++];\n it = 0, len = fread(s, 1, LEN, stdin);\n return len ? s[it++] : EOF;\n }\n bool notend() {\n char c;\n for (c = get(); c == ' ' || c == '\\n' || c == '\\r'; c = get());\n if (it) it--;\n return c != EOF;\n }\n void put(char c) {\n if (it == LEN) fwrite(s,1,LEN,stdout), it = 0;\n s[it++] = c;\n }\n void flush() {\n fwrite(s, 1, it, stdout);\n it = 0;\n }\n}buff, bufo;\ninline int getint() {\n char c; int res = 0, sig = 1;\n for (c = buff.get(); c < '0' || c > '9'; c = buff.get()) if(c == '-') sig = -1;\n for (; c >= '0' && c <= '9'; c = buff.get()) res = res * 10 + (c - '0');\n return sig * res;\n}\ninline ll getll() {\n char c; ll res = 0, sig = 1;\n for (c = buff.get(); c < '0' || c > '9'; c = buff.get()) if (c == '-') sig = -1;\n for (; c >= '0' && c <= '9'; c = buff.get()) res = res * 10 + (c - '0');\n return sig * res;\n}\ninline void putint(int x, char suf) {\n if (!x) bufo.put('0');\n else {\n if (x < 0) bufo.put('-'), x = -x;\n int k = 0; char s[15];\n while (x) {\n s[++k] = x % 10 + '0';\n x /= 10;\n }\n for (; k; k--) bufo.put(s[k]);\n }\n bufo.put(suf);\n}\ninline void putll(ll x, char suf) {\n if (!x) bufo.put('0');\n else {\n if (x < 0) bufo.put('-'), x = -x;\n int k = 0; char s[25];\n while (x) {\n s[++k] = x % 10 + '0';\n x /= 10;\n }\n for (; k; k--) bufo.put(s[k]);\n }\n bufo.put(suf);\n}\ninline char get_char() {\n char c;\n for (c = buff.get(); c == ' ' || c == '\\n' || c == '\\r'; c = buff.get());\n return c;\n}\ntemplate<class T> bool chmin(T &x, T y) {\n return x > y ? (x = y, true) : false;\n}\ntemplate<class T> bool chmax(T &x, T y) {\n return x < y ? (x = y, true) : false;\n}\n\nconst int LIM = 1 << 13;\nconst int inf = 0x3f3f3f3f;\nint ans[(1 << 13) + 5]; // minimum depth.\nvector<int> have;\n\nvoid iddfs(int maxDep) {\n if ((int)have.size() == maxDep + 1) return;\n vector<int> nxt;\n for (auto i : have) {\n int perhaps = i << 1;\n if (perhaps <= LIM && ans[perhaps] >= (int)have.size()) // 剪枝 \n nxt.push_back(perhaps);\n }\n for (int i = 0; i < (int)have.size(); i++) {\n for (int j = i + 1; j < (int)have.size(); j++) {\n int perhaps = have[i] + have[j];\n if (perhaps <= LIM && ans[perhaps] >= (int)have.size())\n nxt.push_back(perhaps);\n perhaps = abs(have[i] - have[j]);\n if (perhaps <= LIM && ans[perhaps] >= (int)have.size())\n nxt.push_back(perhaps);\n }\n }\n sort(nxt.begin(), nxt.end());\n nxt.erase(unique(nxt.begin(), nxt.end()), nxt.end()); // 排除等效冗余\n for (auto i : nxt) {\n ans[i] = have.size();\n have.push_back(i); \n iddfs(maxDep);\n have.pop_back();\n }\n}\n\nint main() {\n memset(ans, 0x3f, sizeof(ans));\n have.push_back(1);\n ans[1] = 0;\n for (int i = 1; i <= 13; i++) iddfs(i);\n int x;\n while (x = getint()) putint(ans[x], '\\n'); \n bufo.flush();\n return 0;\n}", "accuracy": 1, "time_ms": 150, "memory_kb": 3044, "score_of_the_acc": -0.5154, "final_rank": 6 }, { "submission_id": "aoj_1271_4782559", "code_snippet": "#include <cstdio>\nusing namespace std;\n\nint ans, n, a[105];\n\nbool dfs(int dep, int sum)\n{\n int b = sum << (ans - dep);\n if (dep > ans || b < n || sum <= 0)\n return false;\n a[dep] = sum;\n if (sum == n || b == n)\n return true;\n for (int i = 0; i <= dep; ++i)\n if (dfs(dep + 1, sum + a[i]) || dfs(dep + 1, sum - a[i]))\n return true;\n return false;\n}\n\nint main()\n{\n while (true)\n {\n scanf(\"%d\", &n);\n if (n == 0)\n break;\n for (ans = 0;; ans++)\n {\n if (dfs(0, 1))\n {\n printf(\"%d\\n\", ans);\n break;\n }\n }\n }\n return 0;\n}", "accuracy": 1, "time_ms": 260, "memory_kb": 2620, "score_of_the_acc": -0.1693, "final_rank": 3 }, { "submission_id": "aoj_1271_4782418", "code_snippet": "#include <cstdio>\nusing namespace std;\n\nint ans, n, a[105];\n\nbool dfs(int dep, int sum)\n{\n if (dep > ans || sum << (ans - dep) < n || sum <= 0)\n return false;\n a[dep] = sum;\n if (sum == n || sum << (ans - dep) == n)\n return true;\n for (int i = 0; i <= dep; ++i)\n if (dfs(dep + 1, sum + a[i]) || dfs(dep + 1, sum - a[i]))\n return true;\n return false;\n}\n\nint main()\n{\n while (true)\n {\n scanf(\"%d\", &n);\n if (n == 0)\n break;\n for (ans = 0;; ans++)\n {\n if (dfs(0, 1))\n {\n printf(\"%d\\n\", ans);\n break;\n }\n }\n }\n return 0;\n}", "accuracy": 1, "time_ms": 260, "memory_kb": 2576, "score_of_the_acc": -0.1209, "final_rank": 1 }, { "submission_id": "aoj_1271_4782416", "code_snippet": "#include <cstdio>\nusing namespace std;\n\nint ans, n, a[105];\n\nbool dfs(int dep, int sum)\n{\n if (dep > ans || sum << (ans - dep) < n || sum <= 0)\n return false;\n a[dep] = sum;\n if (sum == n || sum << (ans - dep) == n)\n return true;\n for (int i = 0; i <= dep; ++i)\n if (dfs(dep + 1, sum + a[i]) || dfs(dep + 1, sum - a[i]))\n return true;\n return false;\n}\n\nint main()\n{\n while (true)\n {\n scanf(\"%d\", &n);\n if (n == 0)\n break;\n for (ans = 0;; ++ans)\n {\n if (dfs(0, 1))\n {\n printf(\"%d\\n\", ans);\n break;\n }\n }\n }\n return 0;\n}", "accuracy": 1, "time_ms": 270, "memory_kb": 2600, "score_of_the_acc": -0.1583, "final_rank": 2 } ]
aoj_1276_cpp
Problem B: Prime Gap The sequence of n - 1 consecutive composite numbers (positive integers that are not prime and not equal to 1) lying between two successive prime numbers p and p + n is called a prime gap of length n . For example, (24, 25, 26, 27, 28) between 23 and 29 is a prime gap of length 6. Your mission is to write a program to calculate, for a given positive integer k , the length of the prime gap that contains k . For convenience, the length is considered 0 in case no prime gap contains k . Input The input is a sequence of lines each of which contains a single positive integer. Each positive integer is greater than 1 and less than or equal to the 100000th prime number, which is 1299709. The end of the input is indicated by a line containing a single zero. Output The output should be composed of lines each of which contains a single non-negative integer. It is the length of the prime gap that contains the corresponding positive integer in the input if it is a composite number, or 0 otherwise. No other characters should occur in the output. Sample Input 10 11 27 2 492170 0 Output for the Sample Input 4 0 6 0 114
[ { "submission_id": "aoj_1276_10803560", "code_snippet": "#define _CRT_SECURE_NO_WARNINGS\n#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\n#define int long long\n#define endl '\\n'\n#define print(v) for(auto& x:v)cout<<x<<' ';cout<<endl\n\nconst int N = 2e6;\nint minp[N];\nbitset<N> vis;\nvector<int> ps;\n\nvoid euler(int n)\n{\n\tfor (int i = 2; i <= n; i++) {\n\t\tif (!vis[i]) ps.push_back(i), minp[i] = i;\n\t\tfor (int j = 0; j < ps.size() && ps[j] * i <= n; j++) {\n\t\t\tminp[i * ps[j]] = ps[j];\n\t\t\tvis[i * ps[j]] = true;\n\t\t\tif (i % ps[j] == 0) break;\n\t\t}\n\t}\n}\n\nvoid solve()\n{\n\teuler(N - 1);\n\tint n;\n\twhile (cin >> n && n) {\n\t\tif (!vis[n]) {\n\t\t\tcout << 0 << endl;\n\t\t\tcontinue;\n\t\t}\n\n\t\tint left = n, right = n;\n\n\t\twhile (left >= 1 && vis[left]) left--;\n\t\twhile (right <= N - 1 && vis[right]) right++;\n\n\t\tcout << right - left << endl;\n\t}\n}\n\nsigned main()\n{\n\tios::sync_with_stdio(0), cin.tie(0), cout.tie(0);\n\n\t//int q; cin >> q;\n\tint q = 1;\n\twhile (q--) solve();\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 20828, "score_of_the_acc": -0.5366, "final_rank": 15 }, { "submission_id": "aoj_1276_10737889", "code_snippet": "//https://vjudge.net/problem/Aizu-1276\n\n#include<bits/stdc++.h>\nusing namespace std;\n#define LENGTH 1299715\nlong long is_prime[LENGTH];\nlong long prime[LENGTH];\nint main() {\n int a, b;\n memset(is_prime,0,sizeof(int)*LENGTH);\n memset(prime,0,sizeof(int)*LENGTH);\n for (int i=2, t=0, p=0; i<=LENGTH;i++) {\n if (is_prime[i]==0)\n {\n prime[p++]=i;\n }\n for (int j=0;j<p&&i*prime[j]<=LENGTH;j++) {\n is_prime[i*prime[j]]=1;\n if (i%prime[j]==0)\n break;\n }\n }\n int num;\n while(cin>>num)\n {\n if(num==0) break;\n if(is_prime[num]==0){\n cout<<0<<endl;\n continue;\n }\n int l=num,r=num;\n for(;;)\n {\n l--;\n if(is_prime[l]==0) break;\n }\n for(;;)\n {\n r++;\n if(is_prime[r]==0) break;\n }\n cout<<r-l<<endl;\n }\n return 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 21616, "score_of_the_acc": -0.5602, "final_rank": 16 }, { "submission_id": "aoj_1276_10236651", "code_snippet": "#include <vector>\n#include <iostream>\nusing namespace std;\n\nconst int LIMIT = 1299709;\n\nint main() {\n\t// step #1. sieve of eratosthenes\n\tvector<bool> p(LIMIT + 1, true);\n\tp[0] = p[1] = false;\n\tfor (int i = 2; i <= LIMIT; i++) {\n\t\tif (p[i]) {\n\t\t\tfor (int j = i * 2; j <= LIMIT; j += i) {\n\t\t\t\tp[j] = false;\n\t\t\t}\n\t\t}\n\t}\n\n\t// step #2. count lower/upper primes\n\tvector<int> low(LIMIT + 1, 0), up(LIMIT + 1, 0);\n\tfor (int i = 2; i <= LIMIT; i++) {\n\t\tlow[i] = (p[i] ? i : low[i - 1]);\n\t}\n\tfor (int i = LIMIT; i >= 2; i--) {\n\t\tup[i] = (p[i] ? i : up[i + 1]);\n\t}\n\n\t// step #3. answer queries\n\twhile (true) {\n\t\tint N;\n\t\tcin >> N;\n\t\tif (N == 0) {\n\t\t\tbreak;\n\t\t}\n\t\tint ans = up[N] - low[N];\n\t\tcout << ans << endl;\n\t}\n\t\n\treturn 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 13212, "score_of_the_acc": -0.3079, "final_rank": 12 }, { "submission_id": "aoj_1276_9579074", "code_snippet": "#include <bits/stdc++.h>\n\nusing namespace std;\n\n#define rep(i, a, b) for (int i = (int)(a); i < (int)(b); i++)\n#define rrep(i, a, b) for (int i = (int)(b)-1; i >= (int)(a); i--)\n#define ALL(v) (v).begin(), (v).end()\n#define UNIQUE(v) sort(ALL(v)), (v).erase(unique(ALL(v)), (v).end())\n#define SZ(v) (int)v.size()\n#define MIN(v) *min_element(ALL(v))\n#define MAX(v) *max_element(ALL(v))\n#define LB(v, x) int(lower_bound(ALL(v), (x)) - (v).begin())\n#define UB(v, x) int(upper_bound(ALL(v), (x)) - (v).begin())\n\nusing uint = unsigned int;\nusing ll = long long int;\nusing ull = unsigned long long;\nusing i128 = __int128_t;\nusing u128 = __uint128_t;\nconst int inf = 0x3fffffff;\nconst ll INF = 0x1fffffffffffffff;\n\ntemplate <typename T> inline bool chmax(T &a, T b) {\n if (a < b) {\n a = b;\n return 1;\n }\n return 0;\n}\ntemplate <typename T> inline bool chmin(T &a, T b) {\n if (a > b) {\n a = b;\n return 1;\n }\n return 0;\n}\ntemplate <typename T, typename U> T ceil(T x, U y) {\n assert(y != 0);\n if (y < 0)\n x = -x, y = -y;\n return (x > 0 ? (x + y - 1) / y : x / y);\n}\ntemplate <typename T, typename U> T floor(T x, U y) {\n assert(y != 0);\n if (y < 0)\n x = -x, y = -y;\n return (x > 0 ? x / y : (x - y + 1) / y);\n}\ntemplate <typename T> int popcnt(T x) {\n return __builtin_popcountll(x);\n}\ntemplate <typename T> int topbit(T x) {\n return (x == 0 ? -1 : 63 - __builtin_clzll(x));\n}\ntemplate <typename T> int lowbit(T x) {\n return (x == 0 ? -1 : __builtin_ctzll(x));\n}\n\ntemplate <class T, class U>\nostream &operator<<(ostream &os, const pair<T, U> &p) {\n os << \"P(\" << p.first << \", \" << p.second << \")\";\n return os;\n}\ntemplate <typename T> ostream &operator<<(ostream &os, const vector<T> &vec) {\n os << \"{\";\n for (int i = 0; i < vec.size(); i++) {\n os << vec[i] << (i + 1 == vec.size() ? \"\" : \", \");\n }\n os << \"}\";\n return os;\n}\ntemplate <typename T, typename U>\nostream &operator<<(ostream &os, const map<T, U> &map_var) {\n os << \"{\";\n for (auto itr = map_var.begin(); itr != map_var.end(); itr++) {\n os << \"(\" << itr->first << \", \" << itr->second << \")\";\n itr++;\n if (itr != map_var.end())\n os << \", \";\n itr--;\n }\n os << \"}\";\n return os;\n}\ntemplate <typename T> ostream &operator<<(ostream &os, const set<T> &set_var) {\n os << \"{\";\n for (auto itr = set_var.begin(); itr != set_var.end(); itr++) {\n os << *itr;\n ++itr;\n if (itr != set_var.end())\n os << \", \";\n itr--;\n }\n os << \"}\";\n return os;\n}\n#ifdef LOCAL\n#define show(...) _show(0, #__VA_ARGS__, __VA_ARGS__)\n#else\n#define show(...) true\n#endif\ntemplate <typename T> void _show(int i, T name) {\n cerr << '\\n';\n}\ntemplate <typename T1, typename T2, typename... T3>\nvoid _show(int i, const T1 &a, const T2 &b, const T3 &...c) {\n for (; a[i] != ',' && a[i] != '\\0'; i++)\n cerr << a[i];\n cerr << \":\" << b << \" \";\n _show(i + 1, a, c...);\n}\n\n#include <unistd.h>\n\nnamespace fastio {\nstatic constexpr uint32_t SZ = 1 << 17;\nchar ibuf[SZ];\nchar obuf[SZ];\nchar out[100];\n// pointer of ibuf, obuf\n\n\nuint32_t pil = 0, pir = 0, por = 0;\n\nstruct Pre {\n char num[10000][4];\n constexpr Pre() : num() {\n for (int i = 0; i < 10000; i++) {\n int n = i;\n for (int j = 3; j >= 0; j--) {\n num[i][j] = n % 10 | '0';\n n /= 10;\n }\n }\n }\n} constexpr pre;\n\ninline void load() {\n memmove(ibuf, ibuf + pil, pir - pil);\n pir = pir - pil + fread(ibuf + pir - pil, 1, SZ - pir + pil, stdin);\n pil = 0;\n if (pir < SZ)\n ibuf[pir++] = '\\n';\n}\n\ninline void flush() {\n fwrite(obuf, 1, por, stdout);\n por = 0;\n}\n\nvoid rd(char &c) {\n do {\n if (pil + 1 > pir)\n load();\n c = ibuf[pil++];\n } while (isspace(c));\n}\n\nvoid rd(string &x) {\n x.clear();\n char c;\n do {\n if (pil + 1 > pir)\n load();\n c = ibuf[pil++];\n } while (isspace(c));\n do {\n x += c;\n if (pil == pir)\n load();\n c = ibuf[pil++];\n } while (!isspace(c));\n}\n\ntemplate <typename T> void rd_real(T &x) {\n string s;\n rd(s);\n x = stod(s);\n}\n\ntemplate <typename T> void rd_integer(T &x) {\n if (pil + 100 > pir)\n load();\n char c;\n do\n c = ibuf[pil++];\n while (c < '-');\n bool minus = 0;\n if constexpr (is_signed<T>::value || is_same_v<T, i128>) {\n if (c == '-') {\n minus = 1, c = ibuf[pil++];\n }\n }\n x = 0;\n while ('0' <= c) {\n x = x * 10 + (c & 15), c = ibuf[pil++];\n }\n if constexpr (is_signed<T>::value || is_same_v<T, i128>) {\n if (minus)\n x = -x;\n }\n}\n\nvoid rd(int &x) {\n rd_integer(x);\n}\nvoid rd(ll &x) {\n rd_integer(x);\n}\nvoid rd(i128 &x) {\n rd_integer(x);\n}\nvoid rd(uint &x) {\n rd_integer(x);\n}\nvoid rd(ull &x) {\n rd_integer(x);\n}\nvoid rd(u128 &x) {\n rd_integer(x);\n}\nvoid rd(double &x) {\n rd_real(x);\n}\nvoid rd(long double &x) {\n rd_real(x);\n}\n\ntemplate <class T, class U> void rd(pair<T, U> &p) {\n return rd(p.first), rd(p.second);\n}\ntemplate <size_t N = 0, typename T> void rd_tuple(T &t) {\n if constexpr (N < std::tuple_size<T>::value) {\n auto &x = std::get<N>(t);\n rd(x);\n rd_tuple<N + 1>(t);\n }\n}\ntemplate <class... T> void rd(tuple<T...> &tpl) {\n rd_tuple(tpl);\n}\n\ntemplate <size_t N = 0, typename T> void rd(array<T, N> &x) {\n for (auto &d : x)\n rd(d);\n}\ntemplate <class T> void rd(vector<T> &x) {\n for (auto &d : x)\n rd(d);\n}\n\nvoid read() {}\ntemplate <class H, class... T> void read(H &h, T &...t) {\n rd(h), read(t...);\n}\n\nvoid wt(const char c) {\n if (por == SZ)\n flush();\n obuf[por++] = c;\n}\nvoid wt(const string s) {\n for (char c : s)\n wt(c);\n}\nvoid wt(const char *s) {\n size_t len = strlen(s);\n for (size_t i = 0; i < len; i++)\n wt(s[i]);\n}\n\ntemplate <typename T> void wt_integer(T x) {\n if (por > SZ - 100)\n flush();\n if (x < 0) {\n obuf[por++] = '-', x = -x;\n }\n int outi;\n for (outi = 96; x >= 10000; outi -= 4) {\n memcpy(out + outi, pre.num[x % 10000], 4);\n x /= 10000;\n }\n if (x >= 1000) {\n memcpy(obuf + por, pre.num[x], 4);\n por += 4;\n } else if (x >= 100) {\n memcpy(obuf + por, pre.num[x] + 1, 3);\n por += 3;\n } else if (x >= 10) {\n int q = (x * 103) >> 10;\n obuf[por] = q | '0';\n obuf[por + 1] = (x - q * 10) | '0';\n por += 2;\n } else\n obuf[por++] = x | '0';\n memcpy(obuf + por, out + outi + 4, 96 - outi);\n por += 96 - outi;\n}\n\ntemplate <typename T> void wt_real(T x) {\n ostringstream oss;\n oss << fixed << setprecision(15) << double(x);\n string s = oss.str();\n wt(s);\n}\n\nvoid wt(int x) {\n wt_integer(x);\n}\nvoid wt(ll x) {\n wt_integer(x);\n}\nvoid wt(i128 x) {\n wt_integer(x);\n}\nvoid wt(uint x) {\n wt_integer(x);\n}\nvoid wt(ull x) {\n wt_integer(x);\n}\nvoid wt(u128 x) {\n wt_integer(x);\n}\nvoid wt(double x) {\n wt_real(x);\n}\nvoid wt(long double x) {\n wt_real(x);\n}\n\ntemplate <class T, class U> void wt(const pair<T, U> val) {\n wt(val.first);\n wt(' ');\n wt(val.second);\n}\ntemplate <size_t N = 0, typename T> void wt_tuple(const T t) {\n if constexpr (N < std::tuple_size<T>::value) {\n if constexpr (N > 0) {\n wt(' ');\n }\n const auto x = std::get<N>(t);\n wt(x);\n wt_tuple<N + 1>(t);\n }\n}\ntemplate <class... T> void wt(tuple<T...> tpl) {\n wt_tuple(tpl);\n}\ntemplate <class T, size_t S> void wt(const array<T, S> val) {\n auto n = val.size();\n for (size_t i = 0; i < n; i++) {\n if (i)\n wt(' ');\n wt(val[i]);\n }\n}\ntemplate <class T> void wt(const vector<T> val) {\n auto n = val.size();\n for (size_t i = 0; i < n; i++) {\n if (i)\n wt(' ');\n wt(val[i]);\n }\n}\n\nvoid print() {\n wt('\\n');\n}\ntemplate <class Head, class... Tail> void print(Head &&head, Tail &&...tail) {\n wt(head);\n if (sizeof...(Tail))\n wt(' ');\n print(forward<Tail>(tail)...);\n}\nvoid __attribute__((destructor)) _d() {\n flush();\n}\n} // namespace fastio\n\n\nusing fastio::flush;\nusing fastio::print;\nusing fastio::read;\n\ninline void first(bool i = true) {\n print(i ? \"first\" : \"second\");\n}\ninline void Alice(bool i = true) {\n print(i ? \"Alice\" : \"Bob\");\n}\ninline void Takahashi(bool i = true) {\n print(i ? \"Takahashi\" : \"Aoki\");\n}\ninline void yes(bool i = true) {\n print(i ? \"yes\" : \"no\");\n}\ninline void Yes(bool i = true) {\n print(i ? \"Yes\" : \"No\");\n}\ninline void No() {\n print(\"No\");\n}\ninline void YES(bool i = true) {\n print(i ? \"YES\" : \"NO\");\n}\ninline void NO() {\n print(\"NO\");\n}\ninline void Yay(bool i = true) {\n print(i ? \"Yay!\" : \":(\");\n}\ninline void Possible(bool i = true) {\n print(i ? \"Possible\" : \"Impossible\");\n}\ninline void POSSIBLE(bool i = true) {\n print(i ? \"POSSIBLE\" : \"IMPOSSIBLE\");\n}\n\n/**\n * @brief Fast IO\n */\n\nint main() {\n int MAXS = 1299710;\n vector<bool> P(MAXS, true);\n P[0] = P[1] = false;\n rep(i,2,MAXS) {\n if (P[i]) {\n rep(j,2,MAXS) {\n if (i * j >= MAXS) break;\n P[i*j] = false;\n }\n }\n }\n vector<int> L(MAXS,0), R(MAXS,MAXS-1);\n rep(i,1,MAXS) {\n if (P[i]) L[i] = i;\n else L[i] = L[i-1];\n }\n rrep(i,0,MAXS-1) {\n if (P[i]) R[i] = i;\n else R[i] = R[i+1];\n }\n while(1) {\n int K;\n cin >> K;\n if (K == 0) return 0;\n cout << R[K] - L[K] << endl;\n }\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 13028, "score_of_the_acc": -0.3023, "final_rank": 11 }, { "submission_id": "aoj_1276_8561782", "code_snippet": "#define _CRT_SECURE_NO_WARNINGS\n#if USE_MP\n#include <boost/multiprecision/cpp_int.hpp>\n#endif\n#include <cassert>\n#include <cstdio>\n#include <iostream>\n#include <iomanip>\n#include <string>\n#include <array>\n#include <vector>\n#include <list>\n#include <map>\n#include <set>\n#include <unordered_set>\n#include <unordered_map>\n#include <queue>\n#include <stack>\n#include <functional>\n#include <algorithm>\n#include <cmath>\n#include <tuple>\n#include <bitset>\n#include <string.h>\n#include <numeric>\n#include <random>\n#if defined _DEBUG\n//#include \"TestCase.h\"\n//#include \"Util.h\"\n#endif\n\nusing namespace std;\n\nusing ll = long long;\nusing ull = unsigned long long;\ntypedef pair<int, int> pii;\ntypedef pair<ll, ll> pll;\ntypedef tuple<ll, ll, ll> tlll;\ntypedef tuple<int, int, int> tiii;\n\nusing ai2 = array<int, 2>;\nusing ai3 = array<int, 3>;\nusing ai4 = array<int, 4>;\nusing al2 = array<ll, 2>;\nusing al3 = array<ll, 3>;\nusing al4 = array<ll, 4>;\nusing ad2 = array<double, 2>;\nusing ad3 = array<double, 3>;\nusing ad4 = array<double, 4>;\n\ninline void Yes(bool upper = false) { cout << (upper ? \"YES\" : \"Yes\") << \"\\n\"; }\ninline void No(bool upper = false) { cout << (upper ? \"NO\" : \"No\") << \"\\n\"; }\n\ninline ll DivCeil(ll nume, ll deno)\n{\n\tassert(deno != 0);\n\tif (deno < 0) { nume = -nume; deno = -deno; }\n\tif (nume < 0) return -(-nume / deno);\n\telse return (nume + deno - 1) / deno;\n}\ninline ll DivFloor(ll nume, ll deno)\n{\n\tassert(deno != 0);\n\tif (deno < 0) { nume = -nume; deno = -deno; }\n\tif (nume < 0) return -((-nume + deno - 1) / deno);\n\telse return nume / deno;\n}\ninline ll DivRound(ll nume, ll deno)\n{\n\tassert(deno != 0);\n\tif (deno < 0) { nume = -nume; deno = -deno; }\n\tif (nume < 0) return -((-nume + deno / 2) / deno);\n\telse return (nume + deno / 2) / deno;\n}\n\ntemplate <typename T, int S>\nauto MakeVecImpl(queue<int>& size, const T& ini)\n{\n\tif constexpr (S == 1)\n\t{\n\t\treturn vector<T>(size.front(), ini);\n\t}\n\telse\n\t{\n\t\tint fsize = size.front();\n\t\tsize.pop();\n\t\treturn vector(fsize, MakeVecImpl<T, S - 1>(size, ini));\n\t}\n}\n\ntemplate <typename T, int S>\nauto MakeVec(const array<int, S>& size, const T ini = T())\n{\n\tqueue<int> qsize;\n\tfor (const auto v : size) qsize.push(v);\n\treturn MakeVecImpl<T, S>(qsize, ini);\n}\nint d4[][2] =\n{\n\t{0,-1},\n\t{0,1},\n\t{-1,0},\n\t{1,0},\n};\nint d8[][2] =\n{\n\t{-1,-1},\n\t{0,-1},\n\t{1,-1},\n\n\t{-1,0},\n\t{1,0},\n\n\t{-1,1},\n\t{0,1},\n\t{1,1},\n};\nvector<array<int, 2>> Neighbor4(int y, int x)\n{\n\tvector<array<int, 2>> ret;\n\tret.push_back({ y, x - 1 });\n\tret.push_back({ y, x + 1 });\n\tret.push_back({ y - 1, x });\n\tret.push_back({ y + 1, x });\n\treturn ret;\n}\nvector<array<int, 2>> Neighbor6(int y, int x)\n{\n\tvector<array<int, 2>> ret;\n\tconst int ofs = (y & 1); // 1 - (y & 1); //\n\n\tret.push_back({ y, x - 1 });\n\tret.push_back({ y, x + 1 });\n\n\tret.push_back({ y - 1, x - 1 + ofs });\n\tret.push_back({ y - 1, x + ofs });\n\n\tret.push_back({ y + 1, x - 1 + ofs });\n\tret.push_back({ y + 1, x + ofs });\n\n\treturn ret;\n}\nvector<array<int, 2>> Neighbor8(int y, int x)\n{\n\tvector<array<int, 2>> ret;\n\tret.push_back({ y - 1, x - 1 });\n\tret.push_back({ y - 1, x });\n\tret.push_back({ y - 1, x + 1 });\n\n\tret.push_back({ y, x - 1 });\n\tret.push_back({ y, x + 1 });\n\n\tret.push_back({ y + 1, x - 1 });\n\tret.push_back({ y + 1, x });\n\tret.push_back({ y + 1, x + 1 });\n\treturn ret;\n}\n\nvector<int> DecompDigit(ull val)\n{\n\tif (val == 0)\n\t\treturn { 0 };\n\n\tvector<int> ret;\n\twhile (val)\n\t{\n\t\tret.emplace_back(val % 10);\n\t\tval /= 10;\n\t}\n\treverse(ret.begin(), ret.end());\n\treturn ret;\n}\n\n\n#define PI 3.14159265358979l\nlong double R2D = 180 / PI;\nlong double D2R = PI / 180;\n\nstatic const ll Mod = 1000000007;\nstatic const ll Mod9 = 998244353;// 1000000007;\nstatic const ll INF64 = 4500000000000000000;// 10000000000000000;\nstatic const int INF32 = 2000000000;\n\nrandom_device rd;\nmt19937 mt(0);//(rd());\n#if USE_MP\nusing mpint = boost::multiprecision::cpp_int;\n#endif\n\nconst double EPS = 1e-10;\n\nvector<bool> isP;\nvector<int> GetPrime()\n{\n\tvector<int> ret;\n\tint SIZE = 1e7;\n\tisP.resize(SIZE, true);\n\tisP[0] = isP[1] = false;\n\tfor (int i = 2; i < SIZE; i++) {\n\t\tif (!isP[i]) continue;\n\t\tfor (int j = i + i; j < SIZE; j += i) {\n\t\t\tisP[j] = false;\n\t\t}\n\t\tret.emplace_back(i);\n\t}\n\treturn ret;\n}\n\nstruct Solver\n{\n\tvoid Run()\n\t{\n\t}\n};\n\nint main()\n{\n\tstd::cin.tie(nullptr); //★インタラクティブ注意★\n\tstd::ios_base::sync_with_stdio(false);\n\tint T = 1;\n\t//cin >> T;\n\tauto p = GetPrime();\n\twhile (1)\n\t{\n\t\tint i;\n\t\tcin >> i;\n\t\tif (i == 0) break;\n\n\t\tif (isP[i])\n\t\t\tcout << \"0\\n\";\n\t\telse\n\t\t{\n\t\t\tauto itr = lower_bound(p.begin(), p.end(), i);\n\t\t\tcout << *itr - *(itr - 1) << \"\\n\";\n\t\t}\n\t}\n\n\treturn 0;\n}\n\n/// 値渡しになっていないか?\n/// 入力を全部読んでいるか? 途中でreturnしない\n/// 32bitで収まるか? 10^5数えるとき\n/// modは正しいか?\n/// multisetでcountしていないか?", "accuracy": 1, "time_ms": 40, "memory_kb": 8960, "score_of_the_acc": -0.2093, "final_rank": 9 }, { "submission_id": "aoj_1276_8349386", "code_snippet": "#include <iostream>\n#include <vector>\n#include <algorithm>\nusing namespace std;\n#define MAX int(1299709)\n#define ALL(v) v.begin(),v.end()\n\nint main(void) {\n\tvector<int> prime;\n\tvector<bool> isPrime(MAX + 1, true);\n\tfor (int i = 2; i <= MAX; ++i) {\n\t\tif (isPrime[i]) {\n\t\t\tprime.push_back(i);\n\t\t}\n\t\tint a = 2 * i;\n\t\twhile (a <= MAX) {\n\t\t\tisPrime[a] = false;\n\t\t\ta += i;\n\t\t}\n\t}\n\twhile (true) {\n\t\tint N;\n\t\tcin >> N;\n\t\tif (N == 0) break;\n\n\t\tint index = lower_bound(ALL(prime), N) - prime.begin();\n\t\tint ans = -1;\n\t\tif (prime[index] == N) {\n\t\t\tans = 0;\n\t\t} else {\n\t\t\tans = prime[index] - prime[index - 1];\n\t\t}\n\t\tcout << ans << endl;\n\t}\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3664, "score_of_the_acc": -0.0211, "final_rank": 2 }, { "submission_id": "aoj_1276_7159211", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n#define rep(i, n) for (int i = 0; i < (int)(n); i++)\n#define repn(i,end) for(int i = 0; i <= (int)(end); i++)\n#define reps(i,start,end) for(int i = start; i < (int)(end); i++)\n#define repsn(i,start,end) for(int i = start; i <= (int)(end); i++)\n#define ll long long\n#define print(t) cout << t << endl \n#define all(a) (a).begin(),(a).end()\n// << std::fixed << std::setprecision(0)\nconst ll INF = 1LL << 60;\n \ntemplate<class T> void chmin(T& a, T b){\n if(a > b){\n a = b;\n }\n}\n \ntemplate<class T> void chmax(T& a, T b){\n if(a < b){\n a = b;\n }\n}\n// for AOJ or ICPC or etc..\ntemplate<class Tp>\nbool zero (const Tp &x) {\n return x == 0;\n}\n\ntemplate<class Tp, class... Args>\nbool zero (const Tp &x, const Args& ...args) {\n return zero(x) and zero(args...);\n}\nll divisor(ll n){\n ll sum = 0;\n for(ll i = 1;i * i <= n;i++){\n if(n % i == 0){\n sum++;\n if(i * i != n)sum++;;\n }\n }\n\n return sum;\n}\nint main(){\n while(1){\n ll n;cin >> n;\n if(zero(n))break;\n if(divisor(n) <= 2){\n cout << 0 << endl;\n continue;\n }else{\n ll mi = n;ll ma = n;\n while(divisor(mi) != 2){\n mi--;\n }\n while(divisor(ma) != 2){\n ma++;\n }\n cout << ma - mi << endl;\n }\n }\n}", "accuracy": 1, "time_ms": 100, "memory_kb": 3344, "score_of_the_acc": -0.0989, "final_rank": 6 }, { "submission_id": "aoj_1276_6567513", "code_snippet": "#line 2 \"library/KowerKoint/base.hpp\"\n\n#ifndef ONLINE_JUDGE\n#define _GLIBCXX_DEBUG\n#endif\n\n#include <bits/stdc++.h>\nusing namespace std;\n\n#define REP(i, n) for(int i = 0; i < (int)(n); i++)\n#define FOR(i, a, b) for(ll i = a; i < (ll)(b); i++)\n#define ALL(a) (a).begin(),(a).end()\n#define END(...) { print(__VA_ARGS__); return; }\n\nusing VI = vector<int>;\nusing VVI = vector<VI>;\nusing VVVI = vector<VVI>;\nusing ll = long long;\nusing VL = vector<ll>;\nusing VVL = vector<VL>;\nusing VVVL = vector<VVL>;\nusing VD = vector<double>;\nusing VVD = vector<VD>;\nusing VVVD = vector<VVD>;\nusing VS = vector<string>;\nusing VVS = vector<VS>;\nusing VVVS = vector<VVS>;\nusing VC = vector<char>;\nusing VVC = vector<VC>;\nusing VVVC = vector<VVC>;\nusing P = pair<int, int>;\nusing VP = vector<P>;\nusing VVP = vector<VP>;\nusing VVVP = vector<VVP>;\nusing LP = pair<ll, ll>;\nusing VLP = vector<LP>;\nusing VVLP = vector<VLP>;\nusing VVVLP = vector<VVLP>;\n\ntemplate <typename T>\nusing PQ = priority_queue<T>;\ntemplate <typename T>\nusing GPQ = priority_queue<T, vector<T>, greater<T>>;\n\nconstexpr int INF = 1001001001;\nconstexpr ll LINF = 1001001001001001001ll;\nconstexpr int DX[] = {1, 0, -1, 0};\nconstexpr int DY[] = {0, 1, 0, -1};\n\nvoid print() { cout << '\\n'; }\ntemplate<typename T>\nvoid print(const T &t) { cout << t << '\\n'; }\ntemplate<typename Head, typename... Tail>\nvoid print(const Head &head, const Tail &... tail) {\n cout << head << ' ';\n print(tail...);\n}\n\n#ifdef ONLINE_JUDGE\ntemplate<typename... Args>\nvoid dbg(const Args &... args) {}\n#else\nvoid dbg() { cerr << '\\n'; }\ntemplate<typename T>\nvoid dbg(const T &t) { cerr << t << '\\n'; }\ntemplate<typename Head, typename... Tail>\nvoid dbg(const Head &head, const Tail &... tail) {\n cerr << head << ' ';\n dbg(tail...);\n}\n#endif\n\ntemplate< typename T1, typename T2 >\nostream &operator<<(ostream &os, const pair< T1, T2 >& p) {\n os << p.first << \" \" << p.second;\n return os;\n}\n\ntemplate< typename T1, typename T2 >\nistream &operator>>(istream &is, pair< T1, T2 > &p) {\n is >> p.first >> p.second;\n return is;\n}\n\ntemplate< typename T >\nostream &operator<<(ostream &os, const vector< T > &v) {\n for(int i = 0; i < (int) v.size(); i++) {\n os << v[i] << (i + 1 != (int) v.size() ? \" \" : \"\");\n }\n return os;\n}\n\ntemplate< typename T >\nistream &operator>>(istream &is, vector< T > &v) {\n for(T &in : v) is >> in;\n return is;\n}\n\ntemplate<typename T>\nvector<vector<T>> split(typename vector<T>::const_iterator begin, typename vector<T>::const_iterator end, T val) {\n vector<vector<T>> res;\n vector<T> cur;\n for(auto it = begin; it != end; it++) {\n if(*it == val) {\n res.push_back(cur);\n cur.clear();\n } else cur.push_back(val);\n }\n res.push_back(cur);\n return res;\n}\n\nvector<string> split(typename string::const_iterator begin, typename string::const_iterator end, char val) {\n vector<string> res;\n string cur = \"\";\n for(auto it = begin; it != end; it++) {\n if(*it == val) {\n res.push_back(cur);\n cur.clear();\n } else cur.push_back(val);\n }\n res.push_back(cur);\n return res;\n}\n\ntemplate< typename T1, typename T2 >\ninline bool chmax(T1 &a, T2 b) { return a < b && (a = b, true); }\n\ntemplate< typename T1, typename T2 >\ninline bool chmin(T1 &a, T2 b) { return a > b && (a = b, true); }\n\ntemplate <typename T>\npair<VI, vector<T>> compress(const vector<T> &a) {\n int n = a.size();\n vector<T> x;\n REP(i, n) x.push_back(a[i]);\n sort(ALL(x)); x.erase(unique(ALL(x)), x.end());\n VI res(n);\n REP(i, n) res[i] = lower_bound(ALL(x), a[i]) - x.begin();\n return make_pair(res, x);\n}\n\ntemplate <typename T>\npair<vector<T>, vector<T>> factorial(int n) {\n vector<T> res(n+1), rev(n+1);\n res[0] = 1;\n REP(i, n) res[i+1] = res[i] * (i+1);\n rev[n] = 1 / res[n];\n for(int i = n; i > 0; i--) {\n rev[i-1] = rev[i] * i;\n }\n return make_pair(res, rev);\n}\n#line 3 \"library/KowerKoint/internal_operator.hpp\"\n\nnamespace internal_operator {\n template <typename T>\n T default_add(T a, T b) { return a + b; }\n template <typename T>\n T default_sub(T a, T b) { return a - b; }\n template <typename T>\n T zero() { return T(0); }\n template <typename T>\n T default_div(T a, T b) { return a / b; }\n template <typename T>\n T default_mult(T a, T b) { return a * b; }\n template <typename T>\n T one() { return T(1); }\n template <typename T>\n T default_xor(T a, T b) { return a ^ b; }\n template <typename T>\n T default_and(T a, T b) { return a & b; }\n template <typename T>\n T default_or(T a, T b) { return a | b; }\n ll mod3() { return 998244353LL; }\n ll mod7() { return 1000000007LL; }\n ll mod9() { return 1000000009LL; }\n}\n\n#line 3 \"library/KowerKoint/integer.hpp\"\n\nVL divisor(ll n) {\n assert(n > 0);\n VL fow, bck;\n for(ll i = 1; i * i <= n; i++) {\n if(n % i == 0) {\n fow.push_back(i);\n if(i * i != n) bck.push_back(n / i);\n }\n }\n reverse(ALL(bck));\n fow.insert(fow.end(), ALL(bck));\n return fow;\n}\n\nbool is_prime(ll n) {\n assert(n > 0);\n for(ll d = 2; d*d <= n; d++) {\n if(n % d == 0) return false;\n }\n return true;\n}\n\nVL least_prime_factors(ll n) {\n assert(n > 0);\n VL lpfs(n+1, -1), primes;\n FOR(d, 2, n+1) {\n if(lpfs[d] == -1) {\n lpfs[d] = d;\n primes.push_back(d);\n }\n for(ll p : primes) {\n if(p * d > n || p > lpfs[d]) break;\n lpfs[p*d] = p;\n }\n }\n return lpfs;\n}\n\nVL prime_list(ll n) {\n assert(n > 0);\n VL primes;\n vector<bool> sieved(n+1);\n FOR(d, 2, n+1) {\n if(!sieved[d]) {\n primes.push_back(d);\n for(ll i = d*d; i <= n; i += d) sieved[i] = 1;\n }\n }\n return primes;\n}\n\nmap<ll, int> prime_factor(ll n) {\n assert(n > 0);\n map<ll, int> factor;\n for(ll d = 2; d*d <= n; d++) {\n while(n%d == 0) {\n n /= d;\n factor[d]++;\n }\n }\n if(n > 1) factor[n]++;\n return factor;\n}\n\nll extgcd(ll a, ll b, ll& x, ll& y) {\n x = 1, y = 0;\n ll nx = 0, ny = 1;\n while(b) {\n ll q = a / b;\n tie(a, b) = LP(b, a % b);\n tie(x, nx) = LP(nx, x - nx*q);\n tie(y, ny) = LP(ny, y - ny*q);\n }\n return a;\n}\n\nll inv_mod(ll n, ll m) {\n ll x, y;\n assert(extgcd(n, m, x, y) == 1);\n x %= m;\n if(x < 0) x += m;\n return x;\n}\n\nll pow_mod(ll a, ll n, ll m) {\n if(n == 0) return 1LL;\n if(n < 0) return inv_mod(pow_mod(a, -n, m), m);\n ll res = 1;\n while(n) {\n if(n & 1) {\n res *= a;\n res %= m;\n }\n n >>= 1;\n a *= a;\n a %= m;\n }\n return res;\n}\n\n#line 5 \"library/KowerKoint/modint.hpp\"\n\ntemplate <ll (*mod)()>\nstruct Modint {\n ll val;\n \n Modint(): val(0) {}\n\n Modint(ll x): val(x) {\n val %= mod();\n if(val < 0) val += mod();\n }\n\n Modint& operator+=(const Modint& r) {\n val += r.val;\n if(val >= mod()) val -= mod();\n return *this;\n }\n friend Modint operator+(const Modint& l, const Modint& r) {\n return Modint(l) += r;\n }\n\n Modint& operator-=(const Modint& r) {\n val -= r.val;\n if(val < 0) val += mod();\n return *this;\n }\n friend Modint operator-(const Modint& l, const Modint& r) {\n return Modint(l) -= r;\n }\n\n Modint& operator*=(const Modint& r) {\n val *= r.val;\n val %= mod();\n return *this;\n }\n Modint operator*(const Modint& r) {\n return (Modint(*this) *= r);\n }\n friend Modint operator*(const Modint& l, const Modint& r) {\n return Modint(l) *= r;\n }\n\n Modint pow(ll n) const {\n return Modint(pow_mod(val, n, mod()));\n }\n\n Modint inv() const {\n return Modint(inv_mod(val, mod()));\n }\n\n Modint& operator/=(const Modint& r) {\n return (*this *= r.inv());\n }\n friend Modint operator/(const Modint& l, const Modint& r) {\n return Modint(l) /= r;\n }\n\n Modint& operator^=(const ll n) {\n val = pow_mod(val, n, mod());\n return *this;\n }\n Modint operator^(const ll n) {\n return this->pow(n);\n }\n\n Modint operator+() const { return *this; }\n Modint operator-() const { return Modint() - *this; }\n\n Modint& operator++() {\n val++;\n if(val == mod()) val = 0LL;\n return *this;\n }\n Modint& operator++(int) {\n Modint res(*this);\n ++*this;\n return res;\n }\n\n Modint& operator--() {\n if(val == 0LL) val = mod();\n val--;\n return *this;\n }\n Modint& operator--(int) {\n Modint res(*this);\n --*this;\n return res;\n }\n\n friend bool operator==(const Modint& l, const Modint& r) {\n return l.val == r.val;\n }\n friend bool operator!=(const Modint& l, const Modint& r) {\n return l.val != r.val;\n }\n\n static pair<vector<Modint>, vector<Modint>> factorial(int n) {\n vector<Modint> fact(n+1), rfact(n+1);\n fact[0] = 1;\n REP(i, n) fact[i+1] = fact[i] * (i+1);\n rfact[n] = 1 / fact[n];\n for(int i = n-1; i >= 0; i--) rfact[i] = rfact[i+1] * (i+1);\n return {fact, rfact};\n }\n\n friend istream& operator>>(istream& is, Modint& mi) {\n is >> mi.val;\n return is;\n }\n\n friend ostream& operator<<(ostream& os, const Modint& mi) {\n os << mi.val;\n return os;\n }\n};\n\nusing MI3 = Modint<internal_operator::mod3>;\nusing V3 = vector<MI3>;\nusing VV3 = vector<V3>;\nusing VVV3 = vector<VV3>;\nusing MI7 = Modint<internal_operator::mod7>;\nusing V7 = vector<MI7>;\nusing VV7 = vector<V7>;\nusing VVV7 = vector<VV7>;\nusing MI9 = Modint<internal_operator::mod9>;\nusing V9 = vector<MI9>;\nusing VV9 = vector<V9>;\nusing VVV9 = vector<VV9>;\n#line 3 \"library/KowerKoint/counting.hpp\"\n\ntemplate <typename T>\nstruct Counting {\n vector<T> fact, ifact;\n\n Counting() {}\n Counting(ll n) {\n expand(n);\n }\n\n void expand(ll n) {\n ll sz = (ll)fact.size();\n if(sz > n) return;\n fact.resize(n+1);\n ifact.resize(n+1);\n fact[0] = 1;\n FOR(i, max(1LL, sz), n+1) fact[i] = fact[i-1] * i;\n ifact[n] = 1 / fact[n];\n for(ll i = n-1; i >= sz; i--) ifact[i] = ifact[i+1] * (i+1);\n }\n\n T permutation(ll n, ll r) {\n assert(n >= r);\n assert(r >= 0);\n expand(n);\n return fact[n] * ifact[n-r];\n }\n\n T combination(ll n, ll r) {\n assert(n >= r);\n assert(r >= 0);\n expand(n);\n return fact[n] * ifact[r] * ifact[n-r];\n }\n\n T stirling(ll n, ll k) {\n assert(n >= k);\n assert(k >= 0);\n if(n == 0) return 1;\n T res = 0;\n int sign = k%2? -1 : 1;\n expand(k);\n REP(i, k+1) {\n res += sign * ifact[i] * ifact[k-i] * T(i).pow(n);\n sign *= -1;\n }\n return res;\n }\n\n vector<vector<T>> stirling_table(ll n, ll k) {\n assert(n >= 0 && k >= 0);\n vector<vector<T>> res(n+1, vector<T>(k+1));\n res[0][0] = 1;\n FOR(i, 1, n+1) FOR(j, 1, k+1) {\n res[i][j] = res[i-1][j-1] + j * res[i-1][j];\n }\n return res;\n }\n\n T bell(ll n, ll k) {\n assert(n >= 0 && k >= 0);\n expand(k);\n vector<T> tmp(k+1);\n int sign = 1;\n tmp[0] = 1;\n FOR(i, 1, k+1) {\n sign *= -1;\n tmp[i] = tmp[i-1] + sign * ifact[i];\n }\n T res = 0;\n REP(i, k+1) {\n res += T(i).pow(n) * ifact[i] * tmp[k-i];\n }\n return res;\n }\n\n vector<vector<T>> partition_table(ll n) {\n assert(n >= 0);\n vector<vector<T>> res(n+1, vector<T>(n+1));\n REP(i, n+1) res[0][i] = 1;\n FOR(i, 1, n+1) FOR(j, 1, n+1) {\n res[i][j] = res[i][j-1] + (i<j? 0 : res[i-j][j]);\n }\n return res;\n }\n};\n#line 2 \"Contests/Dummy/main.cpp\"\n\n/* #include \"atcoder/all\" */\n/* using namespace atcoder; */\n/* #include \"KowerKoint/expansion/ac-library/all.hpp\" */\n\nvoid solve(){\n auto list = prime_list(1300000);\n while(1) {\n ll n; cin >> n;\n if(!n) break;\n auto it = lower_bound(ALL(list), n);\n if(*it == n) print(0);\n else print(*it - *(it-1));\n }\n}\n\n// generated by oj-template v4.7.2 (https://github.com/online-judge-tools/template-generator)\nint main() {\n // Fasterize input/output script\n ios::sync_with_stdio(false);\n cin.tie(nullptr);\n cout << fixed << setprecision(100);\n // scanf/printf user should delete this fasterize input/output script\n\n int t = 1;\n //cin >> t; // comment out if solving multi testcase\n for(int testCase = 1;testCase <= t;++testCase){\n solve();\n }\n return 0;\n}", "accuracy": 1, "time_ms": 1040, "memory_kb": 4024, "score_of_the_acc": -1.032, "final_rank": 19 }, { "submission_id": "aoj_1276_6391936", "code_snippet": "#include <bits/stdc++.h>\n#define rep(i, n) for (int i = 0; i < n; ++i)\ntypedef long long ll;\nusing namespace std;\n\nvector<int> prime;\nvoid solve(int K) {\n auto it = lower_bound(prime.begin(), prime.end(), K);\n if (*it == K) {\n cout << \"0\\n\";\n return;\n }\n\n auto r = upper_bound(prime.begin(), prime.end(), K);\n auto l = r - 1;\n cout << *r - *l << \"\\n\";\n}\n\nint main() {\n cin.tie(0);\n ios_base::sync_with_stdio(false);\n\n for (int i = 2; i <= 1299709; ++i) {\n bool flag = true;\n for (int j = 2; j * j <= i; ++j) {\n if (i % j == 0) {\n flag = false;\n break;\n }\n }\n if (flag) prime.emplace_back(i);\n }\n\n int K;\n while (cin >> K, K)\n solve(K);\n\n return 0;\n}", "accuracy": 1, "time_ms": 170, "memory_kb": 3508, "score_of_the_acc": -0.1718, "final_rank": 7 }, { "submission_id": "aoj_1276_6381859", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n#define bokusunny ios::sync_with_stdio(false), cin.tie(nullptr);\n\n// inititalize: O(NloglogN)\n// primes: O(N)\n// factorize: O(logN)\n// divisors: O(約数の個数)\nstruct Eratosthenes {\n private:\n int _n;\n vector<bool> IsPrime;\n vector<int> MinFactor;\n vector<int> Mobius;\n\n public:\n Eratosthenes(int n = 1 << 22) : _n(n), IsPrime(n + 1, true), MinFactor(n + 1, -1), Mobius(n + 1, 1) {\n IsPrime[0] = IsPrime[1] = false;\n for (int p = 2; p <= n; p++) {\n if (!IsPrime[p]) continue;\n MinFactor[p] = p;\n Mobius[p] = -1;\n\n for (int q = 2 * p; q <= n; q += p) {\n IsPrime[q] = false;\n if (MinFactor[q] == -1) MinFactor[q] = p;\n if (q / p % p == 0) {\n Mobius[q] = 0;\n } else {\n Mobius[q] = -Mobius[q];\n }\n }\n }\n }\n\n bool is_prime(int x) { return IsPrime[x]; }\n\n vector<int> primes() {\n vector<int> res;\n for (int i = 2; i <= _n; i++) {\n if (IsPrime[i]) res.push_back(i);\n }\n return res;\n }\n\n vector<pair<int, int>> factorize(int n) {\n assert(2 <= n && n <= _n);\n\n vector<pair<int, int>> res;\n while (n > 1) {\n auto p = MinFactor[n];\n int ex = 0;\n while (MinFactor[n] == p) {\n ex++;\n n /= p;\n }\n res.emplace_back(p, ex);\n }\n\n return res;\n }\n\n vector<int> divisors(int n) {\n vector<int> res = {1};\n if (n == 1) return res;\n\n auto pf = factorize(n);\n for (auto [p, ex] : pf) {\n int sz = (int)res.size();\n for (int i = 0; i < sz; i++) {\n int v = 1;\n for (int j = 0; j < ex; j++) {\n v *= p;\n res.push_back(res[i] * v);\n }\n }\n }\n\n return res;\n }\n\n vector<int> get_mobius() { return Mobius; }\n};\n\nEratosthenes E;\n\nvoid solve(int &a) {\n auto get_prev_p = [&](int x) {\n for (int i = x - 1; i >= 2; i--) {\n if (E.is_prime(i)) return i;\n }\n return -1;\n };\n auto get_nxt_p = [&](int x) {\n for (int i = x + 1; i <= 1299709; i++) {\n if (E.is_prime(i)) return i;\n }\n return -1;\n };\n\n if (E.is_prime(a)) {\n cout << 0 << endl;\n } else {\n int pre = get_prev_p(a);\n int nxt = get_nxt_p(a);\n assert(pre != -1 && nxt != -1);\n cout << nxt - pre << endl;\n }\n}\n\nint main() {\n bokusunny;\n int a;\n while (cin >> a) {\n if (a == 0) break;\n solve(a);\n }\n\n return 0;\n}", "accuracy": 1, "time_ms": 150, "memory_kb": 36200, "score_of_the_acc": -1.1341, "final_rank": 20 }, { "submission_id": "aoj_1276_6217257", "code_snippet": "#include <stdio.h>\n\nint prime[103000];\nint pp = 0;\n\nint main(void) {\n int n, t;\n for (int i = 2; i <= 1299709; i++) {\n t = 1;\n for (int x = 2; x * x <= i; x++) {\n if (i % x == 0) {\n t = 0;\n break;\n }\n }\n if (t) prime[pp++] = i;\n }\n for (int tt = 0;; tt++) {\n scanf(\"%d\", &n);\n if (n == 0) break;\n for (int i = 0; i < pp; i++) {\n if (prime[i] == n) {\n printf(\"0\\n\");\n break;\n }\n else if (prime[i] > n) {\n printf(\"%d\\n\", prime[i] - prime[i - 1]);\n break;\n }\n }\n }\n return 0;\n}", "accuracy": 1, "time_ms": 230, "memory_kb": 2960, "score_of_the_acc": -0.2136, "final_rank": 10 }, { "submission_id": "aoj_1276_5883857", "code_snippet": "#include <algorithm>\n#include <bitset>\n#include <complex>\n#include <deque>\n#include <exception>\n#include <fstream>\n#include <functional>\n#include <iomanip>\n#include <ios>\n#include <iosfwd>\n#include <iostream>\n#include <istream>\n#include <iterator>\n#include <limits>\n#include <list>\n#include <locale>\n#include <map>\n#include <memory>\n#include <new>\n#include <numeric>\n#include <ostream>\n#include <queue>\n#include <set>\n#include <sstream>\n#include <stack>\n#include <stdexcept>\n#include <streambuf>\n#include <string>\n#include <typeinfo>\n#include <utility>\n#include <valarray>\n#include <vector>\n#include <array>\n#include <atomic>\n#include <chrono>\n#include <condition_variable>\n#include <forward_list>\n#include <future>\n#include <initializer_list>\n#include <mutex>\n#include <random>\n#include <ratio>\n#include <regex>\n#include <scoped_allocator>\n#include <system_error>\n#include <thread>\n#include <tuple>\n#include <typeindex>\n#include <type_traits>\n#include <unordered_map>\n#include <unordered_set>\nusing namespace std;\n#define pp pair<ll,ll>\n#define ll long long\n#define ld long double\n#define mk make_pair\n#define INF32 2147483647 \n#define INF64 9223372036854775807\n#define rep(i, n) for(int i = 0; i < n; i++)\nconst ll MOD = 1000000007;//10^9+7\nconst ll mod = 998244353;\n\nvector<int> Eratosthenes( const int N )\n{\n std::vector<bool> is_prime( N + 1 );\n for( int i = 0; i <= N; i++ )\n {\n is_prime[ i ] = true;\n }\n std::vector<int> P;\n for( int i = 2; i <= N; i++ )\n {\n if( is_prime[ i ] )\n {\n for( int j = 2 * i; j <= N; j += i )\n {\n is_prime[ j ] = false;\n }\n P.push_back( i );\n }\n }\n return P;\n}\n\nint main() {\n vector<int> P = Eratosthenes(1599709);\n vector<int> Primelist; Primelist.push_back(1);\n for(int i=0; i<P.size(); i++){\n Primelist.push_back(P[i]);\n }\n vector<int> ans;\n while(1){\n int N; cin >> N;\n if(N==0) break;\n auto itr = lower_bound(Primelist.begin(),Primelist.end(),N);\n int index = distance(P.begin(),itr);\n if(*itr == N){\n ans.push_back(0);\n }else{\n int aP = *prev(itr);\n ans.push_back(-(aP - *itr));\n }\n }\n\n for(int a: ans){\n cout << a << endl;\n }\n \n\n\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3932, "score_of_the_acc": -0.0292, "final_rank": 3 }, { "submission_id": "aoj_1276_5883804", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\nusing ll = int64_t;\nusing vl = vector<ll>;\nusing vc = vector<char>;\nusing vst = vector<string>;\nusing vb = vector<bool>;\nusing vvl = vector<vl>;\nusing vvc = vector<vc>;\nusing vvb = vector<vb>;\nusing mll = map<ll, ll>;\nusing pll = pair<ll, ll>;\nusing pss = pair<string, string>;\nusing psl = pair<string, ll>;\nusing vpsl = vector<psl>;\nusing vpll = vector<pll>;\nusing spss = set<pss>;\nusing sl = set<ll>;\nusing vsl = vector<sl>;\nusing sst = set<string>;\nusing ql = queue<ll>;\nusing vql = vector<ql>;\nusing dl = deque<ll>;\nusing pql = priority_queue<ll>;\nusing pqg = priority_queue<ll, vector<ll>, greater<ll>>;\n#define uns unordered_set\n#define unm unordered_map\n#define rep(i, a, b) for(ll i = (ll)(a); i < (ll)(b); i++)\n#define rrep(i, a, b) for(ll i = (ll)(a); i >= (ll)(b); i--)\n#define mrep(i, a, b, c) for(ll i = (ll)(a); (bool)(b); c)\n#define sep(n) for(ll i = 0; i < n; i++)\n#define vin(v) rep(i, 0, v.size()) cin >> v[i]\n#define vin2(v1, v2) rep(i, 0, v1.size()) cin >> v1[i] >> v2[i]\n#define vvin(v) rep(i, 0, v.size()) rep(j, 0, v[i].size()) cin >> v[i][j]\n#define all(x) (x).begin(),(x).end()\n#define vsort(v) sort((v).begin(), (v).end())\n#define vreve(v) reverse((v).begin(), (v).end())\n#define rsort(v) sort((v).rbegin(), (v).rend())\n#define For(x, v) for(auto x : v)\n#define vout(v) For(x, v) cout << x << endl;\n#define Yes cout << \"Yes\" << endl\n#define No cout << \"No\" << endl\n#define end0 return 0\n#define elif else if\n#define MP make_pair\n#define MT make_tuple\n#define PB push_back\nll vsum(vl v){ll sum = 0; For(x, v) sum += x; return sum;}\nll exp(ll x, ll y){ll a = 1; rep(i, 0, y) a *= x; return a;}\nll gcd(ll a, ll b){return a ? gcd(b%a, a) : b;}\nll lcm(ll a, ll b){return a / gcd(a, b) * b;}\ndouble factorial(ll x){ll ans=1; rep(i, 1, x+1){ans*=i;}; return ans;}\nbool isinteger(double x){return floor(x)==x;}\nbool iscomposite(ll x){rep(i, 2, sqrt(x)+1){if(x%i == 0) return true;}; return false;}\nbool isprime(ll x){return !iscomposite(x);}\nstring alp = \"abcdefghijklmnopqrstuvwxyz\";\n\nint main(){\n ll n; cin >> n;\n while(n != 0){\n ll i = 0, j = 0;\n while(iscomposite(n+i)) i++;\n while(iscomposite(n-j)) j++;\n cout << i + j << endl;\n cin >> n;\n }\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3352, "score_of_the_acc": -0.0118, "final_rank": 1 }, { "submission_id": "aoj_1276_5756422", "code_snippet": "#include <bits/stdc++.h>\n#define all(v) v.begin(), v.end()\n#define rall(v) v.rbegin(), v.rend()\n#define rep(i,n) for(int i=0;i<(int)(n);i++)\n#define drep(i,j,n) for(int i=0;i<(int)(n-1);i++)for(int j=i+1;j<(int)(n);j++)\n#define trep(i,j,k,n) for(int i=0;i<(int)(n-2);i++)for(int j=i+1;j<(int)(n-1);j++)for(int k=j+1;k<(int)(n);k++)\n#define codefor int test;scanf(\"%d\",&test);while(test--)\n#define INT(...) int __VA_ARGS__;in(__VA_ARGS__)\n#define LL(...) ll __VA_ARGS__;in(__VA_ARGS__)\n#define yes(ans) if(ans)printf(\"yes\\n\");else printf(\"no\\n\")\n#define Yes(ans) if(ans)printf(\"Yes\\n\");else printf(\"No\\n\")\n#define YES(ans) if(ans)printf(\"YES\\n\");else printf(\"NO\\n\")\n#define popcount(v) __builtin_popcount(v)\n#define vector2d(type,name,h,...) vector<vector<type>>name(h,vector<type>(__VA_ARGS__))\n#define vector3d(type,name,h,w,...) vector<vector<vector<type>>>name(h,vector<vector<type>>(w,vector<type>(__VA_ARGS__)))\n#define umap unordered_map\n#define uset unordered_set\nusing namespace std;\nusing ll = long long;\nconst int MOD=1000000007;\nconst int MOD2=998244353;\nconst int INF=1<<30;\nconst ll INF2=1LL<<60;\n//入力系\nvoid scan(int& a){scanf(\"%d\",&a);}\nvoid scan(long long& a){scanf(\"%lld\",&a);}\ntemplate<class T,class L>void scan(pair<T, L>& p){scan(p.first);scan(p.second);}\ntemplate<class T,class U,class V>void scan(tuple<T,U,V>& p){scan(get<0>(p));scan(get<1>(p));scan(get<2>(p));}\ntemplate<class T> void scan(T& a){cin>>a;}\ntemplate<class T> void scan(vector<T>& vec){for(auto&& it:vec)scan(it);}\nvoid in(){}\ntemplate <class Head, class... Tail> void in(Head& head, Tail&... tail){scan(head);in(tail...);}\n//出力系\nvoid print(const int& a){printf(\"%d\",a);}\nvoid print(const long long& a){printf(\"%lld\",a);}\nvoid print(const double& a){printf(\"%.15lf\",a);}\ntemplate<class T,class L>void print(const pair<T, L>& p){print(p.first);putchar(' ');print(p.second);}\ntemplate<class T> void print(const T& a){cout<<a;}\ntemplate<class T> void print(const vector<T>& vec){if(vec.empty())return;print(vec[0]);for(auto it=vec.begin();++it!= vec.end();){putchar(' ');print(*it);}}\nvoid out(){putchar('\\n');}\ntemplate<class T> void out(const T& t){print(t);putchar('\\n');}\ntemplate <class Head, class... Tail> void out(const Head& head,const Tail&... tail){print(head);putchar(' ');out(tail...);}\n//デバッグ系\ntemplate<class T> void dprint(const T& a){cerr<<a;}\ntemplate<class T> void dprint(const vector<T>& vec){if(vec.empty())return;cerr<<vec[0];for(auto it=vec.begin();++it!= vec.end();){cerr<<\" \"<<*it;}}\nvoid debug(){cerr<<endl;}\ntemplate<class T> void debug(const T& t){dprint(t);cerr<<endl;}\ntemplate <class Head, class... Tail> void debug(const Head& head, const Tail&... tail){dprint(head);cerr<<\" \";debug(tail...);}\nll intpow(ll a, ll b){ ll ans = 1; while(b){ if(b & 1) ans *= a; a *= a; b /= 2; } return ans; }\nll modpow(ll a, ll b, ll p){ ll ans = 1; while(b){ if(b & 1) (ans *= a) %= p; (a *= a) %= p; b /= 2; } return ans; }\nll modinv(ll a, ll m) {ll b = m, u = 1, v = 0;while (b) {ll t = a / b;a -= t * b; swap(a, b);u -= t * v; swap(u, v);}u %= m;if (u < 0) u += m;return u;}\nll updivide(ll a,ll b){return (a+b-1)/b;}\ntemplate<class T> void chmax(T &a,const T b){if(b>a)a=b;}\ntemplate<class T> void chmin(T &a,const T b){if(b<a)a=b;}\n\nint main(){\n vector<bool> t(2000001,true);\n vector<int> p;\n t[0]=t[1]=false;\n for(int i=2;i<=2000000;i++){\n if(!t[i])continue;\n p.push_back(i);\n for(int j=2*i;j<=2000000;j+=i){\n t[j]=false;\n }\n }\n while(1){\n INT(n);\n if(n==0)break;\n if(n<=2){\n out(0);\n continue;\n }\n int j=lower_bound(all(p),n)-p.begin();\n if(n==p[j]){\n out(0);\n continue;\n } \n out(p[j]-p[j-1]);\n }\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 4108, "score_of_the_acc": -0.0345, "final_rank": 4 }, { "submission_id": "aoj_1276_5638357", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\n#define REP(i,n) for(int i=0;i<(n);i++)\n#define ALL(v) v.begin(),v.end()\n\nconst int M=1334567;\nbool notp[M];\n\nint main(){\n for(int i=2;i<M;i++){\n if(notp[i])continue;\n for(int j=i*2;j<M;j+=i)notp[j]=true;\n }\n set<int> s;\n REP(i,M)if(!notp[i])s.insert(i);\n int k;\n while(cin>>k,k){\n if(s.count(k))cout<<0<<endl;\n else{\n int l=*--s.upper_bound(k),r=*s.lower_bound(k);\n cout<<r-l<<endl;\n }\n }\n}", "accuracy": 1, "time_ms": 20, "memory_kb": 9284, "score_of_the_acc": -0.1996, "final_rank": 8 }, { "submission_id": "aoj_1276_5625564", "code_snippet": "#include <bits/stdc++.h>\n#define rep(i,n) for(int i = 0; i < (n); i++)\nusing namespace std;\ntypedef long long ll;\n\nstruct Eratosthenes {\n vector<bool> isprime;\n vector<int> primes;\n vector<int> spf; // smallest prime factors\n vector<int> mobius;\n\n Eratosthenes(int N) : isprime(N + 1, true),\n spf(N + 1, -1),\n mobius(N + 1, 1) {\n isprime[1] = false;\n spf[1] = 1;\n\n for(int p = 2; p <= N; p++){\n if(!isprime[p]) continue;\n primes.push_back(p);\n spf[p] = p;\n mobius[p] = -1;\n for(int q = p * 2; q <= N; q += p){\n isprime[q] = false;\n if(spf[q] == -1) spf[q] = p;\n mobius[q] = ((q / p) % p == 0 ? 0 : -mobius[q]);\n }\n }\n }\n\n vector<pair<int,int>> factorize(int n) {\n vector<pair<int,int>> res;\n while(n > 1) {\n int p = spf[n], e = 0;\n while(spf[n] == p) n /= p, e++;\n res.push_back({p, e}); // p^e\n }\n return res;\n }\n\n vector<int> divisors(int n) {\n vector<int> res({1});\n auto pf = factorize(n);\n for(auto p : pf) {\n int s = (int)res.size();\n for(int i = 0; i < s; i++) {\n int v = 1;\n for(int j = 0; j < p.second; j++) {\n v *= p.first;\n res.push_back(res[i] * v);\n }\n }\n }\n return res;\n }\n\n template<class T> void fast_zeta(vector< T > &f) {\n int N = f.size();\n vector<bool> isprime = Eratosthenes(N);\n for(int p = 2; p < N; p++) {\n if(!isprime[p]) continue;\n for(int k = (N - 1) / p; k >= 1; k--) {\n f[k] += f[k * p];\n }\n }\n }\n\n template<class T> void fast_mobius(vector< T > &F) {\n int N = F.size();\n vector<bool> isprime = Eratosthenes(N);\n for(int p = 2; p < N; p++) {\n if(!isprime[p]) continue;\n for(int k = 1; k * p < N; k++) {\n F[k] -= F[k * p];\n }\n }\n }\n\n template<class T> vector< T > gcd_convolution(const vector< T > &f, const vector< T > &g) {\n int N = max(f.size(), g.size());\n vector< T > F(N, 0), G(N, 0), H(N);\n for(int i = 0; i < f.size(); i++) F[i] = f[i];\n for(int i = 0; i < g.size(); i++) G[i] = g[i];\n\n fast_zeta(F);\n fast_zeta(G);\n\n for(int i = 1; i < N; i++) H[i] = F[i] * G[i];\n\n fast_mobius(H);\n return H;\n }\n\n int fast_euler_phi(int n) {\n auto pf = factorize(n);\n int res = n;\n for(auto p : pf) {\n res *= p.first - 1;\n res /= p.first;\n }\n return res;\n }\n};\n\nstruct prime_factorize {\n long long N;\n vector<pair<long long, long long>> pf;\n\n prime_factorize(long long N) : N(N) {\n for(long long p = 2; p * p <= N; p++) {\n if(N % p != 0) continue;\n long long e = 0;\n while(N % p == 0) N /= p, e++;\n pf.push_back({p, e});\n }\n if(N != 1) pf.push_back({N, 1});\n }\n\n long long euler_phi() {\n long long res = N;\n for(auto p : pf) {\n res *= p.first - 1;\n res /= p.first;\n }\n return res;\n }\n};\n\n\nint main(){\n cin.tie(0);\n ios::sync_with_stdio(0);\n\n int MX = 1299709;\n auto p = Eratosthenes(MX);\n int n;\n while(cin >> n, n) {\n if(p.isprime[n]){\n cout << 0 << '\\n'; continue;\n }\n\n int l = n, r = n;\n while(!p.isprime[l]) l--;\n while(!p.isprime[r]) r++;\n cout << r - l << '\\n';\n }\n}", "accuracy": 1, "time_ms": 30, "memory_kb": 13880, "score_of_the_acc": -0.3473, "final_rank": 13 }, { "submission_id": "aoj_1276_5607461", "code_snippet": "#ifdef LOCAL\n#define _GLIBCXX_DEBUG\n#endif\n \n#include <bits/stdc++.h>\nusing namespace std;\n#if __has_include(<atcoder/all>)\n#include <atcoder/all>\nusing namespace atcoder;\n#endif\n \n#pragma region Macros\n// rep macro\n#define foa(v, a) for(auto &v : a)\n#define REPname(a, b, c, d, e, ...) e\n#define REP(...) REPname(__VA_ARGS__, REP3, REP2, REP1, REP0)(__VA_ARGS__)\n#define REP0(x) for(int i = 0; i < (x); ++i)\n#define REP1(i, x) for(int i = 0; i < (x); ++i)\n#define REP2(i, l, r) for(int i = (l); i < (r); ++i)\n#define REP3(i, l, r, c) for(int i = (l); i < (r); i += (c))\n#define REPSname(a, b, c, ...) c\n#define REPS(...) REPSname(__VA_ARGS__, REPS1, REPS0)(__VA_ARGS__)\n#define REPS0(x) for(int i = 1; i <= (x); ++i)\n#define REPS1(i, x) for(int i = 1; i <= (x); ++i)\n#define RREPname(a, b, c, d, e, ...) e\n#define RREP(...) RREPname(__VA_ARGS__, RREP3, RREP2, RREP1, RREP0)(__VA_ARGS__)\n#define RREP0(x) for(int i = (x)-1; i >= 0; --i)\n#define RREP1(i, x) for(int i = (x)-1; i >= 0; --i)\n#define RREP2(i, l, r) for(int i = (r)-1; i >= (l); --i)\n#define RREP3(i, l, r, c) for(int i = (r)-1; i >= (l); i -= (c))\n#define RREPSname(a, b, c, ...) c\n#define RREPS(...) RREPSname(__VA_ARGS__, RREPS1, RREPS0)(__VA_ARGS__)\n#define RREPS0(x) for(int i = (x); i >= 1; --i)\n#define RREPS1(i, x) for(int i = (x); i >= 1; --i)\n \n// name macro\n#define fi first\n#define se second\n#define pb push_back\n#define eb emplace_back\n#define SZ(x) ((int)(x).size())\n#define all(x) (x).begin(), (x).end()\n#define rall(x) (x).rbegin(), (x).rend()\n#define popcnt(x) __builtin_popcountll(x)\ntemplate <class T = int>\nusing V = std::vector<T>;\ntemplate <class T = int>\nusing VV = std::vector<std::vector<T>>;\ntemplate <class T = int>\nusing VVV = std::vector<std::vector<std::vector<T>>>;\ntemplate <class T>\nusing pqup = std::priority_queue<T, std::vector<T>, std::greater<T>>;\nusing ll = long long;\nusing ld = long double;\nusing int128 = __int128_t;\nusing pii = std::pair<int, int>;\nusing pll = std::pair<long long, long long>;\nusing Tp = tuple<ll,ll,ll>;\n \n// input macro\ntemplate <class T, class U>\nstd::istream &operator>>(std::istream &is, std::pair<T, U> &p) {\n\tis >> p.first >> p.second;\n\treturn is;\n}\ntemplate <class T>\nstd::istream &operator>>(std::istream &is, std::vector<T> &v) {\n\tfor(T &i : v) is >> i;\n\treturn is;\n}\nstd::istream &operator>>(std::istream &is, __int128_t &a) {\n\tstd::string s;\n\tis >> s;\n\t__int128_t ret = 0;\n\tfor(int i = 0; i < s.length(); i++)\n\t\tif('0' <= s[i] and s[i] <= '9')\n\t\t\tret = 10 * ret + s[i] - '0';\n\ta = ret * (s[0] == '-' ? -1 : 1);\n\treturn is;\n}\nnamespace scanner {\nvoid scan(int &a) { std::cin >> a; }\nvoid scan(long long &a) { std::cin >> a; }\nvoid scan(std::string &a) { std::cin >> a; }\nvoid scan(char &a) { std::cin >> a; }\nvoid scan(char a[]) { std::scanf(\"%s\", a); }\nvoid scan(double &a) { std::cin >> a; }\nvoid scan(long double &a) { std::cin >> a; }\ntemplate <class T, class U>\nvoid scan(std::pair<T, U> &p) { std::cin >> p; }\ntemplate <class T>\nvoid scan(std::vector<T> &a) { std::cin >> a; }\nvoid INPUT() {}\ntemplate <class Head, class... Tail>\nvoid INPUT(Head &head, Tail &... tail) {\n\tscan(head);\n\tINPUT(tail...);\n}\n} // namespace scanner\n#define VEC(type, name, size) \\\n\tstd::vector<type> name(size); \\\n\tscanner::INPUT(name)\n#define VVEC(type, name, h, w) \\\n\tstd::vector<std::vector<type>> name(h, std::vector<type>(w)); \\\n\tscanner::INPUT(name)\n#define INT(...) \\\n\tint __VA_ARGS__; \\\n\tscanner::INPUT(__VA_ARGS__)\n#define LL(...) \\\n\tlong long __VA_ARGS__; \\\n\tscanner::INPUT(__VA_ARGS__)\n#define STR(...) \\\n\tstd::string __VA_ARGS__; \\\n\tscanner::INPUT(__VA_ARGS__)\n#define CHAR(...) \\\n\tchar __VA_ARGS__; \\\n\tscanner::INPUT(__VA_ARGS__)\n#define DOUBLE(...) \\\n\tdouble __VA_ARGS__; \\\n\tscanner::INPUT(__VA_ARGS__)\n#define LD(...) \\\n\tlong double __VA_ARGS__; \\\n\tscanner::INPUT(__VA_ARGS__)\n \n// output-macro\ntemplate <class T, class U>\nstd::ostream &operator<<(std::ostream &os, const std::pair<T, U> &p) {\n\tos << p.first << \" \" << p.second;\n\treturn os;\n}\ntemplate <class T>\nstd::ostream &operator<<(std::ostream &os, const std::vector<T> &a) {\n\tfor(int i = 0; i < int(a.size()); ++i) {\n\t\tif(i) os << \" \";\n\t\tos << a[i];\n\t}\n\treturn os;\n}\nstd::ostream &operator<<(std::ostream &dest, __int128_t &value) {\n\tstd::ostream::sentry s(dest);\n\tif(s) {\n\t\t__uint128_t tmp = value < 0 ? -value : value;\n\t\tchar buffer[128];\n\t\tchar *d = std::end(buffer);\n\t\tdo {\n\t\t\t--d;\n\t\t\t*d = \"0123456789\"[tmp % 10];\n\t\t\ttmp /= 10;\n\t\t} while(tmp != 0);\n\t\tif(value < 0) {\n\t\t\t--d;\n\t\t\t*d = '-';\n\t\t}\n\t\tint len = std::end(buffer) - d;\n\t\tif(dest.rdbuf()->sputn(d, len) != len) {\n\t\t\tdest.setstate(std::ios_base::badbit);\n\t\t}\n\t}\n\treturn dest;\n}\ntemplate <class T>\nvoid print(const T a) { std::cout << a << '\\n'; }\ntemplate <class Head, class... Tail>\nvoid print(Head H, Tail... T) {\n\tstd::cout << H << ' ';\n\tprint(T...);\n}\ntemplate <class T>\nvoid printel(const T a) { std::cout << a << '\\n'; }\ntemplate <class T>\nvoid printel(const std::vector<T> &a) {\n\tfor(const auto &v : a)\n\t\tstd::cout << v << '\\n';\n}\ntemplate <class Head, class... Tail>\nvoid printel(Head H, Tail... T) {\n\tstd::cout << H << '\\n';\n\tprintel(T...);\n}\nvoid Yes(const bool b = true) { std::cout << (b ? \"Yes\\n\" : \"No\\n\"); }\nvoid No() { std::cout << \"No\\n\"; }\nvoid YES(const bool b = true) { std::cout << (b ? \"YES\\n\" : \"NO\\n\"); }\nvoid NO() { std::cout << \"No\\n\"; }\nvoid err(const bool b = true) {\n\tif(b) {\n\t\tstd::cout << \"-1\\n\", exit(0);\n\t}\n}\n \n//debug macro\nnamespace debugger {\ntemplate <class T>\nvoid view(const std::vector<T> &a) {\n\tstd::cerr << \"{ \";\n\tfor(const auto &v : a) {\n\t\tstd::cerr << v << \", \";\n\t}\n\tstd::cerr << \"\\b\\b }\";\n}\ntemplate <class T>\nvoid view(const std::vector<std::vector<T>> &a) {\n\tstd::cerr << \"{\\n\";\n\tfor(const auto &v : a) {\n\t\tstd::cerr << \"\\t\";\n\t\tview(v);\n\t\tstd::cerr << \"\\n\";\n\t}\n\tstd::cerr << \"}\";\n}\ntemplate <class T, class U>\nvoid view(const std::vector<std::pair<T, U>> &a) {\n\tstd::cerr << \"{\\n\";\n\tfor(const auto &p : a) std::cerr << \"\\t(\" << p.first << \", \" << p.second << \")\\n\";\n\tstd::cerr << \"}\";\n}\ntemplate <class T, class U>\nvoid view(const std::map<T, U> &m) {\n\tstd::cerr << \"{\\n\";\n\tfor(const auto &p : m) std::cerr << \"\\t[\" << p.first << \"] : \" << p.second << \"\\n\";\n\tstd::cerr << \"}\";\n}\ntemplate <class T, class U>\nvoid view(const std::pair<T, U> &p) { std::cerr << \"(\" << p.first << \", \" << p.second << \")\"; }\ntemplate <class T>\nvoid view(const std::set<T> &s) {\n\tstd::cerr << \"{ \";\n\tfor(auto &v : s) {\n\t\tview(v);\n\t\tstd::cerr << \", \";\n\t}\n\tstd::cerr << \"\\b\\b }\";\n}\n \ntemplate <class T>\nvoid view(const T &e) { std::cerr << e; }\n} // namespace debugger\n#ifdef LOCAL\nvoid debug_out() {}\ntemplate <typename Head, typename... Tail>\nvoid debug_out(Head H, Tail... T) {\n\tdebugger::view(H);\n\tstd::cerr << \", \";\n\tdebug_out(T...);\n}\n#define debug(...) \\\n\tdo { \\\n\t\tstd::cerr << __LINE__ << \" [\" << #__VA_ARGS__ << \"] : [\"; \\\n\t\tdebug_out(__VA_ARGS__); \\\n\t\tstd::cerr << \"\\b\\b]\\n\"; \\\n\t} while(false)\n#else\n#define debug(...) (void(0))\n#endif\n \n// vector macro\ntemplate <class T>\nint lb(const std::vector<T> &a, const T x) { return std::distance((a).begin(), std::lower_bound((a).begin(), (a).end(), (x))); }\ntemplate <class T>\nint ub(const std::vector<T> &a, const T x) { return std::distance((a).begin(), std::upper_bound((a).begin(), (a).end(), (x))); }\ntemplate <class T>\nvoid UNIQUE(std::vector<T> &a) {\n\tstd::sort(a.begin(), a.end());\n\ta.erase(std::unique(a.begin(), a.end()), a.end());\n}\ntemplate <class T>\nstd::vector<T> press(std::vector<T> &a) {\n\tauto res = a;\n\tUNIQUE(res);\n\tfor(auto &v : a)\n\t\tv = lb(res, v);\n\treturn res;\n}\n#define SORTname(a, b, c, ...) c\n#define SORT(...) SORTname(__VA_ARGS__, SORT1, SORT0, ...)(__VA_ARGS__)\n#define SORT0(a) std::sort((a).begin(), (a).end())\n#define SORT1(a, c) std::sort((a).begin(), (a).end(), [](const auto x, const auto y) { return x c y; })\ntemplate <class T>\nvoid ADD(std::vector<T> &a, const T x) {\n\tfor(auto &v : a) v += x;\n}\ntemplate <class T>\nvoid SUB(std::vector<T> &a, const T x = 1) {\n\tfor(auto &v : a) v -= x;\n}\ntemplate <class T>\nvoid MUL(std::vector<T> &a, const T x) {\n\tfor(auto &v : a) v *= x;\n}\ntemplate <class T>\nvoid DIV(std::vector<T> &a, const T x) {\n\tfor(auto &v : a) v /= x;\n}\n \n// math macro\ntemplate <class T, class U>\ninline bool chmin(T &a, const U &b) { return a > b ? a = b, true : false; }\ntemplate <class T, class U>\ninline bool chmax(T &a, const U &b) { return a < b ? a = b, true : false; }\ntemplate <class T>\nT divup(T x, T y) { return (x + y - 1) / y; }\ntemplate <class T>\nT POW(T a, long long n) {\n\tT ret = 1;\n\twhile(n) {\n\t\tif(n & 1) ret *= a;\n\t\ta *= a;\n\t\tn >>= 1;\n\t}\n\treturn ret;\n}\n\ntemplate<typename T>\nT ADD(T a, T b){\n\tT res;\n\treturn __builtin_add_overflow(a, b, &res) ? numeric_limits<T>::max() : res;\n}\n\ntemplate<typename T>\nT MUL(T a, T b){\n\tT res;\n\treturn __builtin_mul_overflow(a, b, &res) ? numeric_limits<T>::max() : res;\n}\n\n// modpow\nlong long POW(long long a, long long n, const int mod) {\n\tlong long ret = 1;\n\twhile(n) {\n\t\tif(n & 1) (ret *= a) %= mod;\n\t\t(a *= a) %= mod;\n\t\tn >>= 1;\n\t}\n\treturn ret;\n}\n \n// others\nstruct fast_io {\n\tfast_io() {\n\t\tios::sync_with_stdio(false);\n\t\tcin.tie(nullptr);\n\t\tcout << fixed << setprecision(15);\n\t}\n} fast_io_;\n\n#pragma endregion\n\nusing R = long double;\nconstexpr R EPS=1E-11;\n\n// r の(誤差付きの)符号に従って, -1, 0, 1 を返す.\nint sgn(const R& r){ return (r > EPS) - (r < -EPS); }\n// a, b の(誤差付きの)大小比較の結果に従って, -1, 0, 1 を返す.\nint sgn(const R& a, const R &b){ return sgn(a-b); }\n// a > 0 は sgn(a) > 0\n// a < b は sgn(a, b) < 0\n// a >= b は sgn(a, b) >= 0\n// のように書く.\n// return s * 10^n\n\n//https://atcoder.jp/contests/abc191/submissions/20028529\nlong long x10(const string& s, size_t n) {\n if (s.front() == '-') return -x10(s.substr(1), n);\n auto pos = s.find('.');\n if (pos == string::npos) return stoll(s + string(n, '0'));\n return stoll(s.substr(0, pos) + s.substr(pos + 1) + string(n + pos + 1 - s.size(), '0'));\n}\n \nlong long ceildiv(long long a, long long b) {\n if (b < 0) a = -a, b = -b;\n if (a >= 0) return (a + b - 1) / b;\n else return a / b;\n}\n \nlong long floordiv(long long a, long long b) {\n if (b < 0) a = -a, b = -b;\n if (a >= 0) return a / b;\n else return (a - b + 1) / b;\n}\n \nlong long floorsqrt(long long x) {\n assert(x >= 0);\n long long ok = 0;\n long long ng = 1;\n while (ng * ng <= x) ng <<= 1;\n while (ng - ok > 1) {\n long long mid = (ng + ok) >> 1;\n if (mid * mid <= x) ok = mid;\n else ng = mid;\n }\n return ok;\n}\n\ninline int topbit(unsigned long long x){\n\treturn x?63-__builtin_clzll(x):-1;\n}\n\ninline int popcount(unsigned long long x){\n\treturn __builtin_popcountll(x);\n}\n\ninline int parity(unsigned long long x){//popcount%2\n\treturn __builtin_parity(x);\n}\n\nconst int inf = 1e9;\nconst ll INF = 1e18;\n\nvoid main_() {\n ll N_MAX = 2e6;\n\tV<ll> p(N_MAX),q(N_MAX);\n\tfor(ll i = 2;i < N_MAX;i++){\n\t if(p[i] == 0){\n\t q[i] = 1;\n\t for(ll j = i;j < N_MAX;j+=i){\n\t p[j] = 1;\n\t }\n\t }\n\t}\n\tV<ll> prime;\n\tREP(i,N_MAX){\n\t if(q[i]){\n\t prime.push_back(i);\n\t }\n\t}\n\tll n;\n\tcin >> n;\n\twhile(n!=0){\n\t if(q[n]){\n\t cout << 0 << endl;\n\t }\n\t else{\n\t auto it = upper_bound(prime.begin(),prime.end(),n) - prime.begin();\n\t ll ans = prime[it] - prime[it-1];\n\t cout << ans << endl;\n\t }\n\t cin >> n;\n\t}\n}\n \nint main() {\n\tint t = 1;\n\t//cin >> t;\n\twhile(t--) main_();\n\treturn 0;\n}", "accuracy": 1, "time_ms": 20, "memory_kb": 36260, "score_of_the_acc": -1.0097, "final_rank": 18 }, { "submission_id": "aoj_1276_5327496", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n//SET PRECIOSION cout << fixed ; cout << setprecision(n) << ans << endl;\n#define ll long long\n#define sz(v) ((int)(v).size())\n#define all(v) (v).begin(), (v).end()\n#define pb push_back\n#define FAST ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)\nll SET(ll n, ll pos) { return n = n | (1 << pos ); }\nll RESET(ll n, ll pos) { return n = n & ~(1<<pos);}\nbool CHECK(ll n, ll pos) { return (bool) (n & (1<<pos));}\nvector <ll> v;\nll x,y,z,a,b,c,sum,ans,total,t,cnt,n,m,k,p,q,r,l,w,right,left,row,col,kase;\nstring s1,s2;\nmap<ll,ll> mp;\nset<ll> s;\n#define SIZE 10000008\nmap<ll,ll> :: iterator itr1, ptr1;\nset<ll> :: iterator itr, ptr;\nvector <int> prime; // Stores generated primes\nchar sieve[SIZE]; // 0 means prime\nvoid primeSieve ( int n ) {\n sieve[0] = sieve[1] = 1; // 0 and 1 are not prime\n\n prime.push_back(2); // Only Even Prime\n for ( int i = 4; i <= n; i += 2 ) sieve[i] = 1; // Remove multiples of 2\n\n int sqrtn = sqrt ( n );\n for ( int i = 3; i <= sqrtn; i += 2 ) {\n if ( sieve[i] == 0 ) {\n for ( int j = i * i; j <= n; j += 2 * i ) sieve[j] = 1;\n }\n }\n\n for ( int i = 3; i <= n; i += 2 ) if ( sieve[i] == 0 ) prime.push_back(i);\n}\n\nint main()\n{\n\t///Peace be with you.\n\tFAST;\n\tprimeSieve(SIZE-8);\n\twhile(cin>>n)\n {\n if(n==0)\n break;\n\n if(sieve[n]==0)\n cout<<0<<endl;\n else\n {\n auto itr = upper_bound(prime.begin(),prime.end(),n) ;\n x = *itr;\n itr--;\n y = *itr;\n cout<< x - y <<endl;\n }\n }\n\n\treturn 0;\n}", "accuracy": 1, "time_ms": 30, "memory_kb": 16548, "score_of_the_acc": -0.4275, "final_rank": 14 }, { "submission_id": "aoj_1276_5309083", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\nint isprime(int n)\n{\n int flag=1;\n for(int i=2;i<=sqrt(n);i++)\n {\n if(n%i==0)\n {\n flag=0;\n }\n }\n return flag;\n}\nint main()\n{\n\n\n\n int k;\n while(cin>>k)\n {\n if(k==0)\n break;\n if(isprime(k)==1)\n cout<<\"0\"<<endl;\n else\n {\n int res=0;\n for(int i=k+1;k<=1299709;i++)\n {\n\n res++;\n if(isprime(i)==1)\n break;\n }\n //cout<<\"res=\"<<res<<endl;\n for(int i=k;k>=1;i--)\n {\n if(isprime(i)==1)\n break;\n res++;\n }\n cout<<res<<endl;\n }\n }\n\n\n return 0;\n}", "accuracy": 1, "time_ms": 50, "memory_kb": 3036, "score_of_the_acc": -0.0411, "final_rank": 5 }, { "submission_id": "aoj_1276_4983292", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n#define int long long\n#define rep(i,n) for(int (i)=0;(i)<(n);(i)++)\n#define rrep(i,n) for(int (i)=((n)-1);(i)>=0;(i)--)\n#define itn int\n#define miele(v) min_element(v.begin(), v.end())\n#define maele(v) max_element(v.begin(), v.end())\n#define SUM(v) accumulate(v.begin(), v.end(), 0LL)\n#define lb(a, key) lower_bound(a.begin(),a.end(),key)\n#define ub(a, key) upper_bound(a.begin(),a.end(),key)\n#define COUNT(a, key) count(a.begin(), a.end(), key) \n#define BITCOUNT(x) __builtin_popcount(x)\n#define pb push_back\n#define all(x) (x).begin(),(x).end()\n#define F first\n#define S second\nusing P = pair <int,int>;\nusing WeightedGraph = vector<vector <P>>;\nusing UnWeightedGraph = vector<vector<int>>;\nusing Real = long double;\nusing Point = complex<Real>; //Point and Vector2d is the same!\nusing Vector2d = complex<Real>;\nconst long long INF = 1LL << 60;\nconst int MOD = 1000000007;\nconst double EPS = 1e-15;\nconst double PI=3.14159265358979323846;\ntemplate <typename T> \nint getIndexOfLowerBound(vector <T> &v, T x){\n return lower_bound(v.begin(),v.end(),x)-v.begin();\n}\ntemplate <typename T> \nint getIndexOfUpperBound(vector <T> &v, T x){\n return upper_bound(v.begin(),v.end(),x)-v.begin();\n}\ntemplate<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; }\ntemplate<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; }\n#define DUMPOUT cerr\n#define repi(itr, ds) for (auto itr = ds.begin(); itr != ds.end(); itr++)\nistream &operator>>(istream &is, Point &p) {\n Real a, b; is >> a >> b; p = Point(a, b); return is;\n}\ntemplate <typename T, typename U>\nistream &operator>>(istream &is, pair<T,U> &p_var) {\n is >> p_var.first >> p_var.second;\n return is;\n}\ntemplate <typename T>\nistream &operator>>(istream &is, vector<T> &vec) {\n for (T &x : vec) is >> x;\n return is;\n}\ntemplate <typename T, typename U>\nostream &operator<<(ostream &os, pair<T, U> &pair_var) {\n DUMPOUT<<'{';\n os << pair_var.first;\n DUMPOUT<<',';\n os << \" \"<< pair_var.second;\n DUMPOUT<<'}';\n return os;\n}\ntemplate <typename T>\nostream &operator<<(ostream &os, vector<T> &vec) {\n DUMPOUT<<'[';\n for (int i = 0; i < vec.size(); i++) \n os << vec[i] << (i + 1 == vec.size() ? \"\" : \" \");\n DUMPOUT<<']';\n return os;\n}\ntemplate <typename T>\nostream &operator<<(ostream &os, vector<vector<T>> &df) {\n for (auto& vec : df) os<<vec;\n return os;\n}\ntemplate <typename T, typename U>\nostream &operator<<(ostream &os, map<T, U> &map_var) {\n DUMPOUT << \"{\";\n repi(itr, map_var) {\n os << *itr;\n itr++;\n if (itr != map_var.end()) DUMPOUT << \", \";\n itr--;\n }\n DUMPOUT << \"}\";\n return os;\n}\ntemplate <typename T>\nostream &operator<<(ostream &os, set<T> &set_var) {\n DUMPOUT << \"{\";\n repi(itr, set_var) {\n os << *itr;\n itr++;\n if (itr != set_var.end()) DUMPOUT << \", \";\n itr--;\n }\n DUMPOUT << \"}\";\n return os;\n}\nvoid print() {cout << endl;}\ntemplate <class Head, class... Tail>\nvoid print(Head&& head, Tail&&... tail) {\n cout << head;\n if (sizeof...(tail) != 0) cout << \" \";\n print(forward<Tail>(tail)...);\n}\nvoid dump_func() {DUMPOUT << '#'<<endl;}\ntemplate <typename Head, typename... Tail>\nvoid dump_func(Head &&head, Tail &&... tail) {\n DUMPOUT << head;\n if (sizeof...(Tail) > 0) DUMPOUT << \", \";\n dump_func(std::move(tail)...);\n}\n#ifdef DEBUG_\n#define DEB\n#define dump(...) \\\n DUMPOUT << \" \" << string(#__VA_ARGS__) << \": \" \\\n << \"[\" << to_string(__LINE__) << \":\" << __FUNCTION__ << \"]\" \\\n << endl \\\n << \" \", \\\n dump_func(__VA_ARGS__)\n#else\n#define DEB if (false)\n#define dump(...)\n#endif\n\nvector <bool> primes(1000000,1) ;\nvoid Eratosthenes(int n){\n for(int i = 2; i <= sqrt(n); i++){\n\t\tif(primes[i]){\n\t\t\tfor(int j = 0; i * (j + 2) <= n; j++){\n\t\t\t\tprimes[i *(j + 2)] = 0;\n\t\t\t}\n\t\t}\n\t}\n\tprimes[0] = 0; primes[1] = 0;\n\tfor(int i = 0; i <= n; i++){\n\t\tif(primes[i]){\n\t\t\tcout << i << endl;\n\t\t}\n\t}\n}\nsigned main(void) { cin.tie(0); ios::sync_with_stdio(false);\n int n = 2000000;\n vector <bool> isPrime(n+10, true);\n isPrime[1] = true;\n for(int i=2;i*i<=n;i++){\n if(isPrime[i]){\n for(int j=1;i*(j+1)<=n;j++){\n isPrime[i + i*(j)] = false;\n }\n }\n }\n \n vector <int> maePrime(n+10), nextPrime(n+10);\n int mae = 0;\n for(int i=0;i<n;i++){\n if(isPrime[i]) mae = i;\n maePrime[i] = mae;\n }\n int next = INF;\n for(int i=n-1;i>=0;i--){\n if(isPrime[i]) next = i;\n nextPrime[i] = next;\n }\n \n while(1){\n int n; cin>>n;\n if(n == 0) exit(0);\n print(abs(nextPrime[n] - maePrime[n]));\n }\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 34764, "score_of_the_acc": -0.9551, "final_rank": 17 } ]
aoj_1279_cpp
Problem E: Geometric Map Your task in this problem is to create a program that finds the shortest path between two given locations on a given street map, which is represented as a collection of line segments on a plane. Figure 4 is an example of a street map, where some line segments represent streets and the others are signs indicating the directions in which cars cannot move. More concretely, AE , AM , MQ , EQ , CP and HJ represent the streets and the others are signs in this map. In general, an end point of a sign touches one and only one line segment representing a street and the other end point is open. Each end point of every street touches one or more streets, but no signs. The sign BF , for instance, indicates that at B cars may move left to right but may not in the reverse direction. In general, cars may not move from the obtuse angle side to the acute angle side at a point where a sign touches a street (note that the angle CBF is obtuse and the angle ABF is acute). Cars may directly move neither from P to M nor from M to P since cars moving left to right may not go through N and those moving right to left may not go through O . In a special case where the angle between a sign and a street is rectangular, cars may not move in either directions at the point. For instance, cars may directly move neither from H to J nor from J to H . You should write a program that finds the shortest path obeying these traffic rules. The length of a line segment between ( x 1 , y 1 ) and ( x 2 , y 2 ) is √{( x 2 − x 1 ) 2 + ( y 2 − y 1 ) 2 } . Input The input consists of multiple datasets, each in the following format. n x s y s x g y g x 1 1 y 1 1 x 2 1 y 2 1 . . . x 1 k y 1 k x 2 k y 2 k . . . x 1 n y 1 n x 2 n y 2 n n , representing the number of line segments, is a positive integer less than or equal to 200. ( x s , y s ) and ( x g , y g ) are the start and goal points, respectively. You can assume that ( x s , y s ) ≠ ( x g , y g ) and that each of them is located on an end point of some line segment representing a street. You can also assume that the shortest path from ( x s , y s ) to ( x g , y g ) is unique. ( x 1 k , y 1 k ) and ( x 2 k , y 2 k ) are the two end points of the kth line segment. You can assume that ( x 1 k , y 1 k ) ≠( x 2 k , y 2 k ). Two line segments never cross nor overlap. That is, if they share a point, it is always one of their end points. All the coordinates are non-negative integers less than or equal to 1000. The end of the input is indicated by a line containing a single zero. Output For each input dataset, print every street intersection point on the shortest path from the start point to the goal point, one in an output line in this order, and a zero in a line following those points. Note that a street intersection point is a point where at least two line segments representing streets meet. An output line for a street intersection point should contain its x - and y -coordinates separated by a space. Print -1 if t ...(truncated)
[ { "submission_id": "aoj_1279_4161973", "code_snippet": "#include<iostream>\n#include<string>\n#include<cstdio>\n#include<vector>\n#include<cmath>\n#include<algorithm>\n#include<functional>\n#include<iomanip>\n#include<queue>\n#include<ciso646>\n#include<random>\n#include<map>\n#include<set>\n#include<bitset>\n#include<stack>\n#include<unordered_map>\n#include<utility>\n#include<cassert>\n#include<complex>\n#include<numeric>\nusing namespace std;\n\n//#define int long long\ntypedef long long ll;\n\ntypedef unsigned long long ul;\ntypedef unsigned int ui;\nconst ll mod = 998244353;\nconst ll INF = (1e+18) + 7;\ntypedef pair<int, int>P;\n#define stop char nyaa;cin>>nyaa;\n#define rep(i,n) for(int i=0;i<n;i++)\n#define per(i,n) for(int i=n-1;i>=0;i--)\n#define Rep(i,sta,n) for(int i=sta;i<n;i++)\n#define rep1(i,n) for(int i=1;i<=n;i++)\n#define per1(i,n) for(int i=n;i>=1;i--)\n#define Rep1(i,sta,n) for(int i=sta;i<=n;i++)\n#define all(v) (v).begin(),(v).end()\ntypedef pair<ll, ll> LP;\ntypedef long double ld;\ntypedef pair<ld, ld> LDP;\nconst ld eps = 1e-6;\nconst ld pi = acos(-1.0);\n//typedef vector<vector<ll>> mat;\ntypedef vector<int> vec;\n\nll mod_pow(ll a, ll n) {\n\tll res = 1;\n\twhile (n) {\n\t\tif (n & 1)res = res * a%mod;\n\t\ta = a * a%mod; n >>= 1;\n\t}\n\treturn res;\n}\n\nstruct modint {\n\tll n;\n\tmodint() :n(0) { ; }\n\tmodint(ll m) :n(m) {\n\t\tif (n >= mod)n %= mod;\n\t\telse if (n < 0)n = (n%mod + mod) % mod;\n\t}\n\toperator int() { return n; }\n};\nbool operator==(modint a, modint b) { return a.n == b.n; }\nmodint operator+=(modint &a, modint b) { a.n += b.n; if (a.n >= mod)a.n -= mod; return a; }\nmodint operator-=(modint &a, modint b) { a.n -= b.n; if (a.n < 0)a.n += mod; return a; }\nmodint operator*=(modint &a, modint b) { a.n = ((ll)a.n*b.n) % mod; return a; }\nmodint operator+(modint a, modint b) { return a += b; }\nmodint operator-(modint a, modint b) { return a -= b; }\nmodint operator*(modint a, modint b) { return a *= b; }\nmodint operator^(modint a, int n) {\n\tif (n == 0)return modint(1);\n\tmodint res = (a*a) ^ (n / 2);\n\tif (n % 2)res = res * a;\n\treturn res;\n}\n\nll inv(ll a, ll p) {\n\treturn (a == 1 ? 1 : (1 - p * inv(p%a, a)) / a + p);\n}\nmodint operator/(modint a, modint b) { return a * modint(inv(b, mod)); }\n\nconst int max_n = 500;\nmodint fact[max_n], factinv[max_n];\nvoid init_f() {\n\tfact[0] = modint(1);\n\tfor (int i = 0; i < max_n - 1; i++) {\n\t\tfact[i + 1] = fact[i] * modint(i + 1);\n\t}\n\tfactinv[max_n - 1] = modint(1) / fact[max_n - 1];\n\tfor (int i = max_n - 2; i >= 0; i--) {\n\t\tfactinv[i] = factinv[i + 1] * modint(i + 1);\n\t}\n}\nmodint comb(int a, int b) {\n\tif (a < 0 || b < 0 || a < b)return 0;\n\treturn fact[a] * factinv[b] * factinv[a - b];\n}\n\nint dx[4] = { 0,1,0,-1 };\nint dy[4] = { 1,0,-1,0 };\n\n\nusing speP = pair<ld, int>;\n\nstruct edge {\n\tint to; ld cost;\n};\nld dist(P a, P b) {\n\tld dx = b.first - a.first;\n\tld dy = b.second - a.second;\n\treturn sqrt(pow(dx, 2) + pow(dy, 2));\n}\n\nbool ison(P a, P l, P r) {\n\tld cl = dist(a, l);\n\tld cr = dist(a, r);\n\tld cm = dist(l, r);\n\treturn abs(cl + cr - cm) < eps;\n}\n\nint ban(P a, P b, P l, P r) {\n\tif (!ison(l, a, b))swap(l, r);\n\tint dx1 = b.first - a.first;\n\tint dy1 = b.second - a.second;\n\tint dx2 = r.first - l.first;\n\tint dy2 = r.second - l.second;\n\tint z = dx1 * dx2 + dy1 * dy2;\n\tif (z == 0)return 3;\n\tif (z > 0)return 1;\n\tif (z < 0)return 2;\n}\nint n;\nvoid solve() {\n\tint cx[2], cy[2];\n\trep(i, 2)cin >> cx[i] >> cy[i];\n\tvector<P> lep(n), rip(n);\n\trep(i, n) {\n\t\tint x, y; cin >> x >> y;\n\t\tlep[i] = { x,y };\n\t\tcin >> x >> y;\n\t\trip[i] = { x,y };\n\t}\n\tvector<bool> ism(n, false);\n\trep(i, n) {\n\t\tP p = lep[i];\n\t\tint tmp = 0;\n\t\trep(j, n) {\n\t\t\tif (i == j)continue;\n\t\t\tif (ison(p, lep[j], rip[j])) {\n\t\t\t\ttmp++; break;\n\t\t\t}\n\t\t}\n\t\tp = rip[i];\n\t\trep(j, n) {\n\t\t\tif (i == j)continue;\n\t\t\tif (ison(p, lep[j], rip[j])) {\n\t\t\t\ttmp++; break;\n\t\t\t}\n\t\t}\n\t\tism[i] = (tmp == 2);\n\t\t/*cout << i << \" ! \" << tmp << endl;\n\t\tif (ism[i]) {\n\t\t\tcout << \"!! \" << i << endl;\n\t\t}*/\n\t}\n\tvector<P> ps;\n\trep(i, n) {\n\t\tif (!ism[i])continue;\n\t\trep(j, n) {\n\t\t\tif (i == j)continue;\n\t\t\tif (!ism[j])continue;\n\t\t\tif (ison(lep[i], lep[j], rip[j])) {\n\t\t\t\tps.push_back(lep[i]);\n\t\t\t}\n\t\t\tif (ison(rip[i], lep[j], rip[j])) {\n\t\t\t\tps.push_back(rip[i]);\n\t\t\t}\n\t\t}\n\t}\n\tsort(all(ps));\n\tps.erase(unique(all(ps)), ps.end());\n\t//cout << \"! \" << ps.size() << endl;\n\t//rep(i, ps.size())cout << ps[i].first << \" \" << ps[i].second << endl;\n\tvector<vector<pair<P, int>>> onp(n);\n\trep(i, n) {\n\t\trep(j, n) {\n\t\t\tif (i == j)continue;\n\t\t\tif (!ism[j])continue;\n\t\t\tint id = -1;\n\t\t\tif (!ism[i])id = i;\n\t\t\tif (ison(lep[i], lep[j], rip[j])) {\n\t\t\t\tonp[j].push_back({ lep[i],id });\n\t\t\t}\n\t\t\tif (ison(rip[i], lep[j], rip[j])) {\n\t\t\t\tonp[j].push_back({ rip[i],id });\n\t\t\t}\n\t\t}\n\t}\n\trep(i, n) {\n\t\tonp[i].push_back({ lep[i],-1 });\n\t\tonp[i].push_back({ rip[i],-1 });\n\t\tsort(all(onp[i]));\n\t\tonp[i].erase(unique(all(onp[i])), onp[i].end());\n\t}\n\tmap<P, int> mp;\n\trep(i, ps.size())mp[ps[i]] = i;\n\n\tvector<vector<edge>> G(ps.size());\n\trep(i, n) {\n\t\tif (!ism[i])continue;\n\t\tvector<int> ids;\n\t\t//cout << \"???? \" << i << endl;\n\t\trep(j, onp[i].size()) {\n\t\t\tif (onp[i][j].second < 0) {\n\t\t\t\tids.push_back(j);\n\t\t\t\t//cout << onp[i][j].first.first << \" \" << onp[i][j].first.second << endl;\n\t\t\t}\n\t\t}\n\t\trep(j, (int)ids.size() - 1) {\n\t\t\tint leid = ids[j];\n\t\t\tint riid = ids[j + 1];\n\t\t\tbool fle = true, fri = true;\n\t\t\tfor (int x = leid + 1; x <= riid - 1; x++) {\n\t\t\t\tint id = onp[i][x].second;\n\t\t\t\tint t = ban(onp[i][leid].first, onp[i][riid].first, lep[id], rip[id]);\n\t\t\t\tif (t & 1)fle = false;\n\t\t\t\tif (t & 2)fri = false;\n\t\t\t}\n\t\t\tint l = mp[onp[i][leid].first];\n\t\t\tint r = mp[onp[i][riid].first];\n\t\t\tif (fle) {\n\t\t\t\t//cout << l << \" \" << r << endl;\n\t\t\t\tG[l].push_back({ r,dist(ps[l],ps[r]) });\n\t\t\t}\n\t\t\tif (fri) {\n\t\t\t\t//cout << r << \" \" << l << endl;\n\t\t\t\tG[r].push_back({ l,dist(ps[r],ps[l]) });\n\t\t\t}\n\t\t}\n\t}\n\tvector<ld> dist(ps.size(),INF);\n\tvector<int> pre(ps.size(), -1);\n\tint sta = mp[{cx[0], cy[0]}];\n\tint goa = mp[{cx[1], cy[1]}];\n\tpriority_queue<speP, vector<speP>, greater<speP>> q;\n\tdist[sta] = 0; q.push({ 0,sta });\n\twhile (!q.empty()) {\n\t\tspeP p = q.top(); q.pop();\n\t\tint id = p.second;\n\t\tif (dist[id] < p.first)continue;\n\t\tfor (edge e : G[id]) {\n\t\t\tld nd = dist[id] + e.cost;\n\t\t\tif (nd < dist[e.to]) {\n\t\t\t\tdist[e.to] = nd;\n\t\t\t\tpre[e.to] = id;\n\t\t\t\tq.push({ nd,e.to });\n\t\t\t}\n\t\t}\n\t}\n\tif (dist[goa] == INF) {\n\t\tcout << -1 << endl; return;\n\t}\n\tvector<P> memo;\n\tint cur = goa;\n\twhile (cur != sta) {\n\t\tmemo.push_back(ps[cur]);\n\t\tcur = pre[cur];\n\t}\n\tmemo.push_back(ps[sta]);\n\treverse(all(memo));\n\trep(i, memo.size()) {\n\t\tcout << memo[i].first << \" \" << memo[i].second << endl;\n\t}\n\tcout << 0 << endl;\n}\n\n\nsigned main() {\n\tios::sync_with_stdio(false);\n\tcin.tie(0);\n\t//cout << fixed << setprecision(10);\n\t//init_f(); init();\n\t//int t; cin >> t; rep(i, t)solve();\n\twhile (cin >> n, n)solve();\n\tstop\n\t\treturn 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3292, "score_of_the_acc": -0.0317, "final_rank": 8 }, { "submission_id": "aoj_1279_3583340", "code_snippet": "#include <bits/stdc++.h>\n#define rep(i,n) for (int i = 0; i < n; i++)\nusing namespace std;\ntypedef long long ll;\ntypedef unsigned long long ull;\ntypedef pair<int, int> pi;\ntypedef pair<pi, pi> pp;\ntypedef pair<double, pi> pdp;\ntypedef pair<ll, ll> pl;\nconst double EPS = 1e-9;\nconst ll mod = 1000000007;\nconst int inf = 1 << 30;\nconst ll linf = 1LL << 60;\nconst double PI = 3.14159265358979323846;\n\ntypedef double D;\ntypedef complex<D> P;\ntypedef pair<P, P> L;\ntypedef vector<P> VP;\n#define X real()\n#define Y imag()\n#define LE(n,m) ((n) < (m) + EPS)\n#define GE(n,m) ((n) + EPS > (m))\n#define EQ(n,m) (abs((n)-(m)) < EPS)\n\nD dot(P a, P b) {\n return (conj(a)*b).X;\n}\n\nD cross(P a, P b) {\n return (conj(a)*b).Y;\n}\n\nint ccw(P a, P b, P c) {\n b -= a; c -= a;\n if (cross(b,c) > EPS) return +1;\n if (cross(b,c) < -EPS) return -1;\n if (dot(b,c) < -EPS) return +2;\n if (norm(b) < norm(c)) return -2;\n return 0;\n}\n\nbool isecSP(P a1, P a2, P b) {\n return !ccw(a1, a2, b);\n}\n\nint n;\npi star, goal;\nmap<pi, int> m;\nmap<pi, vector<pdp>> edges;\nmap<pi, double> dp;\nmap<pi, pi> rev;\npp lines[200];\n\nvoid solve() {\n m.clear();\n edges.clear();\n cin >> star.first >> star.second >> goal.first >> goal.second;\n rep(i,n) {\n cin >> lines[i].first.first >> lines[i].first.second >> lines[i].second.first >> lines[i].second.second;\n }\n rep(i,n) {\n rep(j,n) {\n P p1 = P(lines[i].first.first, lines[i].first.second);\n P p2 = P(lines[i].second.first, lines[i].second.second);\n P p3 = P(lines[j].first.first, lines[j].first.second);\n P p4 = P(lines[j].second.first, lines[j].second.second);\n if (isecSP(p3, p4, p1)) {\n m[lines[i].first]++;\n }\n if (isecSP(p3, p4, p2)) {\n m[lines[i].second]++;\n }\n }\n }\n rep(i,n) {\n P p1 = P(lines[i].first.first, lines[i].first.second);\n P p2 = P(lines[i].second.first, lines[i].second.second);\n if (m[lines[i].first] == 1 || m[lines[i].second] == 1) continue;\n vector<pdp> hoge;\n hoge.push_back(pdp(0.0, lines[i].first));\n hoge.push_back(pdp(abs(p1-p2), lines[i].second));\n\n rep(j,n) {\n if (i == j) continue;\n P p3 = P(lines[j].first.first, lines[j].first.second);\n P p4 = P(lines[j].second.first, lines[j].second.second);\n if (m[lines[j].first] == 1 || m[lines[j].second] == 1) continue;\n if (isecSP(p1, p2, p3) && !EQ(0.0, abs(p1-p3)) && !EQ(abs(p1-p2), abs(p1-p3))) {\n hoge.push_back(pdp(abs(p1-p3), lines[j].first));\n }\n if (isecSP(p1, p2, p4) && !EQ(0.0, abs(p1-p4)) && !EQ(abs(p1-p2), abs(p1-p4))) {\n hoge.push_back(pdp(abs(p1-p4), lines[j].second));\n }\n }\n sort(hoge.begin(), hoge.end());\n for (int j = 0; j < (int)hoge.size()-1; j++) {\n bool ok1 = true, ok2 = true;\n P p3 = P(hoge[j].second.first, hoge[j].second.second);\n P p4 = P(hoge[j+1].second.first, hoge[j+1].second.second);\n rep(k,n) {\n if (m[lines[k].first] != 1 && m[lines[k].second] != 1) continue;\n if (m[lines[k].first] == 1) swap(lines[k].first, lines[k].second);\n P p5 = P(lines[k].first.first, lines[k].first.second);\n P p6 = P(lines[k].second.first, lines[k].second.second);\n if (!isecSP(p3, p4, p5)) continue;\n if (GE(dot(p6-p5, p3-p5),0.0)) ok2 = false;\n if (LE(dot(p6-p5, p3-p5),0.0)) ok1 = false;\n }\n if (ok1) edges[hoge[j].second].push_back(pdp(hoge[j+1].first-hoge[j].first, hoge[j+1].second));\n if (ok2) edges[hoge[j+1].second].push_back(pdp(hoge[j+1].first-hoge[j].first, hoge[j].second));\n }\n }\n /*\n for (auto it = edges.begin(); it != edges.end(); it++) {\n cerr << endl;\n cerr << (*it).first.first << \" \" << (*it).first.second << endl;\n vector<pdp>& huga = (*it).second;\n for (pdp& piyo : huga) {\n cerr << piyo.second.first << \" \" << piyo.second.second << endl;\n }\n }\n */\n rep(i,n) {\n dp[lines[i].first] = inf;\n dp[lines[i].second] = inf;\n }\n dp[star] = 0.0;\n priority_queue<pdp, vector<pdp>, greater<pdp>> pque;\n pque.push(pdp(0.0, star));\n while (!pque.empty()) {\n pdp p = pque.top(); pque.pop();\n if (dp[p.second] < p.first) continue;\n for (auto& e : edges[p.second]) {\n if (dp[e.second] > p.first + e.first) {\n dp[e.second] = p.first + e.first;\n rev[e.second] = p.second;\n pque.push(pdp(dp[e.second], e.second));\n }\n }\n }\n if (dp[goal] > inf-1) {\n cout << -1 << endl;\n return;\n }\n vector<int> ans;\n pi pos = goal;\n while (true) {\n ans.push_back(pos.second); ans.push_back(pos.first);\n if (pos == star) break;\n pos = rev[pos];\n }\n for (int i = (int)ans.size()-1; i >= 0; i -= 2) {\n cout << ans[i] << \" \" << ans[i-1] << endl;\n }\n cout << 0 << endl;\n}\n\nint main() {\n#ifndef LOCAL\n ios::sync_with_stdio(false);\n cin.tie(0);\n#endif\n cout << fixed;\n cout.precision(20);\n cerr << fixed;\n cerr.precision(6);\n#ifdef LOCAL\n //freopen(\"in.txt\", \"wt\", stdout); //for tester\n freopen(\"in.txt\", \"rt\", stdin);\n#endif \n while (cin >> n) {\n if (n == 0) break;\n solve();\n }\n //cerr << \"Time elapsed: \" << 1.0 * clock() / CLOCKS_PER_SEC << \" s.\\n\";\n return 0;\n}", "accuracy": 1, "time_ms": 20, "memory_kb": 3192, "score_of_the_acc": -0.1412, "final_rank": 9 }, { "submission_id": "aoj_1279_2911485", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\nusing pii = pair<int, int>;\nusing ld = long double;\nusing point = complex<ld>;\n\nconstexpr ld eps = 1e-8;\nconstexpr ld pi = acos(-1.0);\n\nconstexpr int inf = 1e9;\n\nld dist(pii const& p1, pii const& p2) {\n point a(p1.first, p1.second);\n point b(p2.first, p2.second);\n return abs(a - b);\n}\n\nstruct segment {\n segment(pii a, pii b, int ai, int bi) : a(a), b(b), ai(ai), bi(bi) {}\n pii a, b;\n int ai, bi;\n};\n\nbool isis_sp(segment const& s, pii const& p) {\n return abs(dist(s.a, p) + dist(s.b, p) - dist(s.a, s.b)) < eps;\n}\n\n// segment is not cross, so this is ok\nbool isis_ss(segment const& s1, segment const& s2) {\n return isis_sp(s1, s2.a) || isis_sp(s1, s2.b) || isis_sp(s2, s1.a) || isis_sp(s2, s1.b);\n}\n\nstruct edge {\n int to, cost;\n};\n\nusing edges = vector<edge>;\nusing graph = vector<edges>;\n\nvoid add_edge(graph& g, int from, int to, int cost) {\n g[from].push_back({to, cost});\n}\n\nint main() {\n int n;\n while(cin >> n, n) {\n pii sp, gp;\n cin >> sp.first >> sp.second >> gp.first >> gp.second;\n vector<pii> ps = {sp, gp};\n vector<pii> p1(n), p2(n);\n for(int i = 0; i < n; ++i) {\n cin >> p1[i].first >> p1[i].second >> p2[i].first >> p2[i].second;\n ps.push_back(p1[i]);\n ps.push_back(p2[i]);\n }\n sort(begin(ps), end(ps));\n ps.erase(unique(begin(ps), end(ps)), end(ps));\n const int m = ps.size();\n\n auto get_idx = [&](pii p) {\n return lower_bound(begin(ps), end(ps), p) - begin(ps);\n };\n\n vector<segment> segs;\n for(int i = 0; i < n; ++i) {\n segs.emplace_back(p1[i], p2[i], get_idx(p1[i]), get_idx(p2[i]));\n }\n\n auto segment_arrangement = [&]() {\n vector<segment> res;\n for(int i = 0; i < n; ++i) {\n vector<pair<ld, int>> v;\n for(int j = 0; j < m; ++j) {\n if(isis_sp(segs[i], ps[j])) {\n v.emplace_back(dist(segs[i].a, ps[j]), j);\n }\n }\n sort(begin(v), end(v));\n for(int j = 0; j + 1 < (int)v.size(); ++j) {\n int p1 = v[j].second, p2 = v[j + 1].second;\n res.emplace_back(ps[p1], ps[p2], p1, p2);\n }\n }\n return res;\n };\n segs = segment_arrangement();\n n = segs.size();\n\n vector<int> on_seg(m);\n for(int i = 0; i < m; ++i) {\n for(int j = 0; j < n; ++j) {\n on_seg[i] += isis_sp(segs[j], ps[i]);\n }\n }\n vector<bool> is_road(n); // is_mark is !is_road\n for(int i = 0; i < n; ++i) {\n is_road[i] = on_seg[segs[i].ai] > 1 && on_seg[segs[i].bi] > 1;\n }\n vector<int> is_intersection(m);\n vector<int> is_sign_intersection(m);\n for(int i = 0; i < m; ++i) {\n for(int j = 0; j < n; ++j) {\n if(is_road[j]) {\n is_intersection[i] += isis_sp(segs[j], ps[i]);\n } else {\n is_sign_intersection[i] |= isis_sp(segs[j], ps[i]);\n }\n }\n }\n for(int i = 0; i < m; ++i) {\n is_intersection[i] -= is_sign_intersection[i];\n }\n\n vector<vector<bool>> can_use(m, vector<bool>(m, false));\n for(int i = 0; i < n; ++i) {\n can_use[segs[i].ai][segs[i].bi] = true;\n can_use[segs[i].bi][segs[i].ai] = true;\n }\n for(int i = 0; i < n; ++i) {\n if(is_road[i]) continue; // select sign\n point pa = point(segs[i].a.first, segs[i].a.second);\n point pb = point(segs[i].b.first, segs[i].b.second);\n auto vec = (on_seg[segs[i].ai] > 1 ? pb - pa : pa - pb);\n for(int j = 0; j < n; ++j) {\n if(!is_road[j] || !isis_ss(segs[i], segs[j])) continue; // select related road\n auto v = point(segs[j].b.first - segs[j].a.first, segs[j].b.second - segs[j].a.second); // a to b\n if(abs(abs(arg(v / vec)) - pi / 2) < eps) {\n can_use[segs[j].ai][segs[j].bi] = false;\n can_use[segs[j].bi][segs[j].ai] = false;\n } else if(abs(arg(v / vec)) + eps < pi / 2) {\n can_use[segs[j].ai][segs[j].bi] = false;\n } else {\n can_use[segs[j].bi][segs[j].ai] = false;\n }\n }\n }\n\n graph g(m);\n for(int i = 0; i < m; ++i) {\n for(int j = 0; j < m; ++j) {\n if(i == j) continue;\n if(can_use[i][j]) {\n add_edge(g, i, j, dist(ps[i], ps[j]));\n }\n }\n }\n vector<int> d(m, inf);\n vector<int> prev(m, -1);\n d[get_idx(sp)] = 0;\n priority_queue<pii, vector<pii>, greater<pii>> que;\n que.emplace(0, get_idx(sp));\n while(!que.empty()) {\n int cur_d, v;\n tie(cur_d, v) = que.top();\n que.pop();\n if(cur_d > d[v]) continue;\n for(auto const& e : g[v]) {\n if(d[e.to] > cur_d + e.cost) {\n d[e.to] = cur_d + e.cost;\n prev[e.to] = v;\n que.emplace(cur_d + e.cost, e.to);\n }\n }\n }\n\n if(d[get_idx(gp)] == inf) {\n cout << -1 << endl;\n } else {\n int now = get_idx(gp);\n vector<pii> ans;\n while(now != -1) {\n if(is_intersection[now] >= 2) {\n ans.push_back(ps[now]);\n }\n now = prev[now];\n }\n reverse(begin(ans), end(ans));\n for(auto& p : ans) {\n cout << p.first << ' ' << p.second << endl;\n }\n cout << 0 << endl;\n }\n }\n}", "accuracy": 1, "time_ms": 100, "memory_kb": 3248, "score_of_the_acc": -1.031, "final_rank": 18 }, { "submission_id": "aoj_1279_2597197", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\ntypedef long long ll;\n\nconst double EPS = 1e-8;\n\ntypedef complex<double> P;\nnamespace std{\n\tbool operator < (const P& a, const P& b){\n\t\treturn real(a) != real(b) ? real(a) < real(b) : imag(a) < imag(b);\n\t}\n}\ndouble cross(const P& a, const P& b){\n\treturn imag(conj(a)*b);\n}\ndouble dot(const P& a, const P& b){\n\treturn real(conj(a)*b);\n}\n\nint ccw(P a, P b, P c){\n\tb -= a; c -= a;\n\tif(cross(b, c) > 0) return +1; // counter clockwise\n\tif(cross(b, c) < 0) return -1; // clockwise\n\tif(dot(b, c) < 0) return +2; // c--a--b on line\n\tif(norm(b) < norm(c)) return -2; // a--b--c on line\n\treturn 0;\n}\n\nstruct L : public vector<P>{\n\tL(){}\n\tL(const P &a, const P &b){\n\t\tpush_back(a); push_back(b);\n\t}\n};\n\n\nbool intersectLL(const L &l, const L &m){\n\treturn abs(cross(l[1] - l[0], m[1] - m[0])) > EPS || // non-parallel\n\t\tabs(cross(l[1] - l[0], m[0] - l[0])) < EPS; // same line\n}\n\nbool intersectSS(const L &s, const L &t){\n\treturn ccw(s[0], s[1], t[0])*ccw(s[0], s[1], t[1]) <= 0 &&\n\t\tccw(t[0], t[1], s[0])*ccw(t[0], t[1], s[1]) <= 0;\n}\n\nP crosspointLL(const L &l, const L &m){\n\tdouble A = cross(l[1] - l[0], m[1] - m[0]);\n\tdouble B = cross(l[1] - l[0], l[1] - m[0]);\n\tif(abs(A) < EPS && abs(B) < EPS) return m[0]; // same line\n\tif(abs(A) < EPS) assert(false); // !!!PRECONDITION NOT SATISFIED!!!\n\treturn m[0] + B / A * (m[1] - m[0]);\n}\n\nbool intersectSP(const L &s, const P &p){\n\treturn abs(s[0] - p) + abs(s[1] - p) - abs(s[1] - s[0]) < EPS; // triangle inequality\n}\n\nstruct Edge{\n\tint from, to;\n\tdouble cost;\n};\n\ntypedef vector<vector<Edge>> Graph;\n\nGraph segment_arrangement(const std::vector<L> &ss, std::vector<P> &ps){\n\tfor(int i = 0; i < (int)ss.size(); ++i){ // O(n^2)\n\t\tps.push_back(ss[i][0]);\n\t\tps.push_back(ss[i][1]);\n\t\tfor(int j = i + 1; j < (int)ss.size(); ++j)\n\t\t\tif(intersectSS(ss[i], ss[j])) ps.push_back(crosspointLL(ss[i], ss[j]));\n\t}\n\tstd::sort(ps.begin(), ps.end());\n\tps.erase(std::unique(ps.begin(), ps.end()), ps.end());\n\tGraph g(ps.size());\n\tfor(int i = 0; i < (int)ss.size(); ++i){\n\t\tstd::vector<std::pair<double, int>> list;\n\t\tfor(int j = 0; j < (int)ps.size(); ++j)\n\t\t\tif(intersectSP(ss[i], ps[j]))\n\t\t\t\tlist.push_back(std::make_pair(norm(ss[i][0] - ps[j]), j));\n\t\tstd::sort(list.begin(), list.end());\n\t\tfor(int j = 0; j + 1 < (int)list.size(); ++j){\n\t\t\tint a = list[j].second, b = list[j + 1].second;\n\t\t\tdouble len = abs(ps[a] - ps[b]);\n\t\t\tg[a].push_back({ a, b, len });\n\t\t\tg[b].push_back({ b, a, len });\n\t\t}\n\t}\n\treturn g;\n}\n\ntypedef pair<double, int> State;\nconst double INF = 1e18;\nbool sign[200];\n\nint main(){\n\tcin.tie(0);\n\tios::sync_with_stdio(false);\n#ifdef LOCAL\n\tstd::ifstream in(\"in\");\n\tstd::cin.rdbuf(in.rdbuf());\n#endif\n\n\tint n;\n\twhile(cin >> n, n){\n\t\tP start, goal;\n\t\t{\n\t\t\tint x, y;\n\t\t\tcin >> x >> y;\n\t\t\tstart = P(x, y);\n\t\t\tcin >> x >> y;\n\t\t\tgoal = P(x, y);\n\t\t}\n\t\tvector<L> ls(n);\n\t\tfor(int i = 0; i < n; i++){\n\t\t\tint x1, y1, x2, y2;\n\t\t\tcin >> x1 >> y1 >> x2 >> y2;\n\t\t\tls[i] = L(P(x1, y1), P(x2, y2));\n\t\t}\n\t\tvector<L> seg;\n\t\tfor(int i = 0; i < n; i++){\n\t\t\tsign[i] = 0;\n\t\t\tint intersect[2] = { 0 };\n\t\t\tfor(int j = 0; j < n; j++){\n\t\t\t\tif(i == j) continue;\n\t\t\t\tif(intersectSP(ls[j], ls[i][0])) intersect[0] = 1;\n\t\t\t\tif(intersectSP(ls[j], ls[i][1])) intersect[1] = 1;\n\t\t\t}\n\t\t\tassert(intersect[0] + intersect[1] >= 1);\n\t\t\tif(intersect[0] + intersect[1] == 1){\n\t\t\t\tsign[i] = 1;\n\t\t\t\t//cout << \"sign \" << i << endl;\n\t\t\t}\n\t\t\telse{\n\t\t\t\tseg.push_back(ls[i]);\n\t\t\t}\n\t\t}\n\n\t\tvector<P> ps;\n\t\tauto G = segment_arrangement(seg, ps);\n\t\tmap<P, int> idxMap;\n\t\tfor(int i = 0; i < ps.size(); i++){\n\t\t\tidxMap[ps[i]] = i;\n\t\t}\n\n\t\tset<pair<int, int>> NG;\n\t\tfor(int i = 0; i < ps.size(); i++){\n\t\t\tfor(auto e : G[i]){\n\t\t\t\tint u = i, v = e.to;\n\t\t\t\tP p0 = ps[u], p1 = ps[v];\n\t\t\t\tfor(int j = 0; j < n; j++){\n\t\t\t\t\tif(!sign[j]) continue;\n\t\t\t\t\tint onIdx = -1;\n\t\t\t\t\tfor(int k = 0; k < 2; k++){\n\t\t\t\t\t\tP sp = ls[j][k];\n\t\t\t\t\t\tif(ccw(p0, sp, p1) == -2 || ccw(p1, sp, p0) == -2){\n\t\t\t\t\t\t\tonIdx = k;\n\t\t\t\t\t\t\tbreak;\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t\tif(onIdx == -1) continue;\n\n\t\t\t\t\tP sp0 = ls[j][onIdx], sp1 = ls[j][1 - onIdx];\n\t\t\t\t\tP v1 = p1 - p0, v2 = sp1 - sp0;\n\t\t\t\t\tint d = dot(v1, v2);\n\t\t\t\t\tif(d >= 0){\n\t\t\t\t\t\tNG.insert({ u, v });\n\t\t\t\t\t\t//cout << \"NG \" << (int)ps[u].real() << \" \" << (int)ps[u].imag() << \" -> \"\n\t\t\t\t\t\t//<< (int)ps[v].real() << \" \" << (int)ps[v].imag() << endl;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\n\t\tvector<double> dist(ps.size(), INF);\n\t\tvector<int> pre(ps.size(), -1);\n\t\tpriority_queue<State, vector<State>, greater<State>> q;\n\t\tq.push({ 0, idxMap[start] });\n\t\tdist[idxMap[start]] = 0;\n\t\twhile(q.size()){\n\t\t\tint v = q.top().second;\n\t\t\tdouble d = q.top().first;\n\t\t\tq.pop();\n\t\t\tif(dist[v] < d) continue;\n\t\t\tfor(auto e : G[v]){\n\t\t\t\tif(NG.count({ v,e.to })) continue;\n\t\t\t\tdouble nd = d + abs(ps[e.to] - ps[v]);\n\t\t\t\tif(dist[e.to] > nd){\n\t\t\t\t\tdist[e.to] = nd;\n\t\t\t\t\tpre[e.to] = v;\n\t\t\t\t\tq.push({ nd, e.to });\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\n\t\tif(dist[idxMap[goal]] == INF){\n\t\t\tcout << -1 << endl;\n\t\t\tcontinue;\n\t\t}\n\n\t\tint cur = idxMap[goal];\n\t\tvector<int> path;\n\t\twhile(true){\n\t\t\tpath.push_back(cur);\n\t\t\tif(cur == idxMap[start]) break;\n\t\t\tcur = pre[cur];\n\t\t}\n\t\treverse(path.begin(), path.end());\n\t\tfor(auto pp : path){\n\t\t\tcout << (int)ps[pp].real() << \" \" << (int)ps[pp].imag() << endl;\n\t\t}\n\t\tcout << 0 << endl;\n\t}\n}", "accuracy": 1, "time_ms": 20, "memory_kb": 3344, "score_of_the_acc": -0.1437, "final_rank": 11 }, { "submission_id": "aoj_1279_2201656", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\ntypedef long long ll;\n\ntypedef complex<ll> P;\ntypedef pair<P,P> S;\ntypedef pair<double,P> State;\n\nnamespace std{\n bool operator < ( const P &a,const P &b){\n return (a.real()==b.real()?a.imag()<b.imag():a.real()<b.real());\n }\n};\n\nll dot(P a,P b){ return real(b*conj(a)); }\nll cross(P a,P b){ return imag(b*conj(a)); }\nint state(S s,P p){\n P a=s.first,b=s.second;\n if(a==p||b==p)return 0;\n if(cross(a-p,b-p)!=0)return 1;\n if(dot(a-p,b-p)>0)return 1;\n return -1;\n}\n\nbool check(P si,P ti,const vector<S> &v,const vector<S> &E,const vector<P> &C){\n for(int i=0;i<(int)C.size();i++)\n if( state( S(si,ti) , C[i] ) == -1 ) return false;\n\n for(int i=0;i<(int)v.size();i++){\n if(state(v[i],si)==1||state(v[i],ti)==1)continue;\n bool res=true;\n for(int j=0;j<(int)E.size();j++){\n if( state(S(si,ti),E[j].first ) !=-1 && state(S(si,ti),E[j].second) !=-1)continue;\n P ve;\n if( state(S(si,ti),E[j].first ) ==-1)ve=E[j].first-E[j].second;\n if( state(S(si,ti),E[j].second) ==-1)ve=E[j].second-E[j].first;\n if( dot(ti-si,ve) <= 0 ){\n res=false;\n }\n }\n if(res)return true;\n }\n\n\n \n return false;\n}\n\nP input(){\n ll x,y;\n cin>>x>>y;\n return P(x,y);\n}\n\nint main(){\n while(1){\n int n;\n cin>>n;\n if(n==0)break;\n vector<S> A,B,E;\n P si=input();\n P ti=input();\n for(int i=0;i<n;i++){\n S s;\n s.first=input();\n s.second=input();\n A.push_back(s);\n }\n for(int i=0;i<n;i++){\n int fc=0,sc=0;\n int fk=0,sk=0;\n for(int j=0;j<n;j++){\n if(i==j)continue;\n if(state(A[j],A[i].first)<=0)fc++;\n if(state(A[j],A[i].first)==-1)fk++;\n if(state(A[j],A[i].second)<=0)sc++;\n if(state(A[j],A[i].second)==-1)sk++;\n }\n bool flg=false;\n if(fc==0&&sk>0)flg=true;\n if(sc==0&&fk>0)flg=true;\n if(flg)E.push_back(A[i]);\n else B.push_back(A[i]);\n }\n \n vector<P> C;\n for(int i=0;i<(int)B.size();i++){\n C.push_back(B[i].first);\n C.push_back(B[i].second);\n }\n // C.push_back(si);\n // C.push_back(ti);\n sort(C.begin(),C.end());\n C.erase(unique(C.begin(),C.end()),C.end());\n assert( *lower_bound(C.begin(),C.end(), si ) == si );\n assert( *lower_bound(C.begin(),C.end(), ti ) == ti );\n \n priority_queue< State ,vector<State> , greater<State> > Q;\n map<P,double> d;\n map<P,P> prev;\n \n for(int i=0;i<(int)C.size();i++){\n d[C[i]]=1e9;\n prev[C[i]]=P(-1,-1);\n }\n \n Q.push( State(0,si) );\n d[si]=0;\n while(!Q.empty()){\n State s=Q.top();\n Q.pop();\n P p=s.second;\n double cost=s.first;\n if(d[p]<cost)continue;\n \n for(int j=0;j<(int)C.size();j++){\n P to=C[j];\n if(p==to)continue;\n if(!check(p,to,B,E,C))continue;\n double ncost=cost;\n double dx=to.real()-p.real();\n double dy=to.imag()-p.imag();\n \n ncost+= sqrt(dx*dx+dy*dy);\n \n if(d[to]>ncost){\n d[to]=ncost;\n prev[to]=p;\n Q.push( State(ncost,to) );\n }\n }\n }\n\n if(d[ti]==1e9){\n cout<<-1<<endl;\n }else{\n vector<P> ans;\n P cur=ti;\n while(1){\n ans.push_back(cur);\n if(cur==si)break;\n cur=prev[cur];\n assert(cur!=P(-1,-1));\n }\n reverse(ans.begin(),ans.end());\n for(int i=0;i<(int)ans.size();i++){\n if(i>0&&ans[i]==ans[i-1])continue;\n cout<<ans[i].real()<<' '<<ans[i].imag()<<endl;\n }\n cout<<0<<endl;\n }\n \n // printf(\"%.10f\\n\",d[ti]);\n }\n return 0;\n}", "accuracy": 1, "time_ms": 30, "memory_kb": 3148, "score_of_the_acc": -0.2516, "final_rank": 12 }, { "submission_id": "aoj_1279_2008614", "code_snippet": "#include<bits/stdc++.h>\n#define f first\n#define s second\n#define mp make_pair\n#define pi M_PI\n#define inf 1<<30\n#define MAX 50000\n#define eps (1e-11)\n#define equals(a,b) (fabs((a)-(b))<eps)\nusing namespace std;\n\nclass Point{\npublic:\n double x,y;\n Point(double x=0,double y=0):x(x),y(y){}\n\n Point operator+(Point p){ return Point(x+p.x,y+p.y);}\n Point operator-(Point p){ return Point(x-p.x,y-p.y);}\n Point operator*(double k){ return Point(x*k,y*k);}\n Point operator/(double k){ return Point(x/k,y/k);}\n bool operator<(Point p)const{ return (x!=p.x ? x<p.x : y<p.y);}\n bool operator==(Point p)const{ return fabs(x-p.x)<eps && fabs(y-p.y)<eps;}\n\n double abs(){ return sqrt(norm());}\n double norm(){ return (x*x+y*y);}\n};\ntypedef Point Vector;\ntypedef vector<Point> Polygon;\n\nclass Segment{\npublic:\n Point p1,p2;\n Segment(Point p1=Point(),Point p2=Point()):p1(p1),p2(p2){}\n};\ntypedef Segment Line;\n\ndouble norm(Vector a){ return (a.x*a.x+a.y*a.y);}\ndouble abs(Vector a){ return sqrt(norm(a));}\ndouble dot(Vector a,Vector b){ return (a.x*b.x+a.y*b.y);}\ndouble cross(Vector a,Vector b){ return (a.x*b.y-a.y*b.x);}\n\nint ccw(Point p0,Point p1,Point p2){\n Vector a=p1-p0;\n Vector b=p2-p0;\n if(cross(a,b)>eps)return 1;\n if(cross(a,b)<-eps)return -1;\n if(dot(a,b)<-eps)return 2;\n if(a.norm()<b.norm())return -2;\n return 0;\n}\n\ntypedef vector<vector<int> > Graph;\ntypedef pair<double,int> P;\n\nint n;\nvector<Segment> vs,va,vb;\n\nbool check(Segment s){\n for(int i=0;i<vb.size();i++){\n if(ccw(s.p1,s.p2,vb[i].p1)==0){\n Vector v1=(s.p1-vb[i].p1),v2=(vb[i].p2-vb[i].p1);\n double r=acos(dot(v1,v2)/(abs(v1)*abs(v2)))*180/pi;\n if(90-r<eps)return false;\n }\n if(ccw(s.p1,s.p2,vb[i].p2)==0){\n Vector v1=(s.p1-vb[i].p2),v2=(vb[i].p1-vb[i].p2);\n double r=acos(dot(v1,v2)/(abs(v1)*abs(v2)))*180/pi;\n if(90-r<eps)return false;\n }\n }\n return true;\n}\n\nGraph SegmentArrangement(vector<Segment> v,vector<Point> &ps){\n for(int i=0;i<v.size();i++){\n ps.push_back(v[i].p1);\n ps.push_back(v[i].p2);\n }\n sort(ps.begin(),ps.end());\n ps.erase(unique(ps.begin(),ps.end()),ps.end());\n Graph g(ps.size());\n for(int i=0;i<v.size();i++){\n vector<pair<double,int> > list;\n for(int j=0;j<ps.size();j++)\n if(ccw(v[i].p1,v[i].p2,ps[j])==0)\n list.push_back(mp(norm(v[i].p1-ps[j]),j));\n sort(list.begin(),list.end());\n for(int j=0;j<list.size()-1;j++){\n int a=list[j].s,b=list[j+1].s;\n if(check(Segment(ps[a],ps[b])))g[a].push_back(b);\n if(check(Segment(ps[b],ps[a])))g[b].push_back(a);\n }\n }\n return g;\n}\n\nGraph g;\nvector<Point> vp;\nint a=-1,b=-1;\nPoint s,t;\n\nvoid init(){\n vs.clear();va.clear();vb.clear();vp.clear();\n}\n\nvoid dijkstra(){\n double d[MAX];\n int p[MAX];\n fill(d,d+MAX,inf);\n fill(p,p+MAX,-1);\n priority_queue<P,vector<P>,greater<P> > pq;\n pq.push(mp(0.0,a));\n d[a]=0.0;\n p[a]=a;\n\n while(pq.size()){\n P u=pq.top();\n pq.pop();\n if(d[u.s]-u.f<-eps)continue;\n for(int i=0;i<g[u.s].size();i++){\n int next=g[u.s][i];\n double dis=abs(vp[u.s]-vp[next]);\n if(u.f+dis-d[next]<-eps){\n d[next]=u.f+dis;\n p[next]=u.s;\n pq.push(mp(u.f+dis,next));\n }\n }\n }\n if(p[b]==-1){\n cout<<-1<<endl;\n return;\n }\n vector<Point> ans;\n int now=b;\n while(p[now]!=now){\n ans.push_back(vp[now]);\n now=p[now];\n }\n ans.push_back(vp[now]);\n reverse(ans.begin(),ans.end());\n for(int i=0;i<ans.size();i++)cout<<ans[i].x<<\" \"<<ans[i].y<<endl;\n cout<<0<<endl;\n}\n\nint main()\n{\n while(1){\n cin>>n;\n if(n==0)break;\n init();\n cin>>s.x>>s.y>>t.x>>t.y;\n for(int i=0;i<n;i++){\n int a,b,c,d;\n cin>>a>>b>>c>>d;\n vs.push_back(Segment(Point(a,b),Point(c,d)));\n }\n for(int i=0;i<n;i++){\n bool left=false,right=false;\n for(int j=0;j<n;j++){\n if(i==j)continue;\n if(ccw(vs[j].p1,vs[j].p2,vs[i].p1)==0)left=true;\n if(ccw(vs[j].p1,vs[j].p2,vs[i].p2)==0)right=true;\n }\n if(left && right)va.push_back(vs[i]);\n else vb.push_back(vs[i]);\n }\n g=SegmentArrangement(va,vp);\n for(int i=0;i<vp.size();i++){\n if(vp[i]==s)a=i;\n if(vp[i]==t)b=i;\n }\n dijkstra();\n }\n return 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 2024, "score_of_the_acc": -0.0113, "final_rank": 5 }, { "submission_id": "aoj_1279_1918665", "code_snippet": "#include <iostream>\n#include <algorithm>\n#include <cmath>\n#include <vector>\n#include <complex>\n#include <queue>\n#include <deque>\n#include <set>\n#include <map>\n#include <unordered_set>\n#include <unordered_map>\n#include <iomanip>\n#include <assert.h>\n#include <array>\n#include <cstdio>\n#include <cstring>\n#include <random>\n#include <functional>\n#include <numeric>\n#include <bitset>\n#include <fstream>\n\nusing namespace std;\n\n#define REP(i,a,b) for(int i=a;i<(int)b;i++)\n#define rep(i,n) REP(i,0,n)\n#define all(c) (c).begin(), (c).end()\n#define zero(a) memset(a, 0, sizeof a)\n#define minus(a) memset(a, -1, sizeof a)\n#define watch(a) { cout << #a << \" = \" << a << endl; }\ntemplate<class T1, class T2> inline bool minimize(T1 &a, T2 b) { return b < a && (a = b, 1); }\ntemplate<class T1, class T2> inline bool maximize(T1 &a, T2 b) { return a < b && (a = b, 1); }\n\ntypedef long long ll;\nint const inf = 1<<29;\n\nstruct xor128 {\n unsigned x,y,z,w;\n xor128(): x(random_device()()), y(157892372), z(7777777), w(757328) {}\n unsigned next() {\n unsigned t=x^(x<<11);\n x=y;y=z;z=w;\n return w=w^(w>>19)^t^(t>>8);\n }\n unsigned next(unsigned k) {\n return next()%k;\n }\n} rndgen;\n\n\ntypedef complex<double> P;\n\nnamespace std {\n\nbool operator < (P const& l, P const& r) {\n return abs(l.real() - r.real()) < 1e-5 ? l.imag() < r.imag() : l.real() < r.real();\n}\n\n}\n\nnamespace point_2d {\n\nusing Real = double;\n\nReal const EPS = 1e-5; // !!! DO CHECK EPS !!!\n\n//bool operator < (P const& l, P const& r) { return abs(l.real() - r.real()) < EPS ? l.imag() < r.imag() : l.real() < r.real();}\n\nbool operator > (P const& l, P const& r) {\n return abs(l.real() - r.real()) < EPS ? l.imag() > r.imag() : l.real() > r.real();\n}\n\nbool equals(Real a, Real b) {\n return abs(a - b) < EPS;\n}\n\nbool equals (P const& l, P const& r) {\n return abs(l - r) < EPS;\n}\n\nstruct Line : public pair<P, P> {\n Line(P const& a, P const& b) { first = a, second = b; }\n const P& operator[] (int x) const { return x == 0 ? first : second; }\n P& operator[] (int x) { return x == 0 ? first : second; }\n};\ntypedef Line Segment;\n\nbool operator < (const Line& a, const Line& b) {\n return a[0] != b[0] ? a[0] < b[0] : a[1] < b[1];\n}\n\nstruct Circle {\n P p; Real r;\n Circle(){}\n Circle(P const& p, Real r): r(r) { this->p = p; }\n};\n\nstruct Polygon : public vector<P> {\n vector<P>& g = *this;\n Polygon() = default;\n Polygon(vector<P> const& g) { this->g = g; }\n P& operator[] (int x) { return vector<P>::operator[]((x + size()) % size()); }\n Segment side(int x) { return std::move(Segment(this->operator[](x), this->operator[](x+1))); }\n Segment backside(int x) { return std::move(Segment(this->operator[](x), this->operator[](x-1))); }\n};\n\nReal cross(P const& a, P const& b) { return imag(conj(a)*b); }\nReal dot(P const& a, P const& b) { return real(conj(a)*b); }\nReal cos(P const& l, P const& r) { return dot(l, r) / (abs(l) * abs(r)); } // not verified\n\nenum ccw_result {\n counter_clockwise = +1, clockwise = -1, online_back = +2, online_front = -2, on_segment = 0\n};\n\nccw_result ccw(P a, P b, P c) {\n b -= a, c -= a;\n if(cross(b, c) > 0) { return ccw_result::counter_clockwise; }\n if(cross(b, c) < 0) { return ccw_result::clockwise; }\n if(dot(b, c) < 0) { return ccw_result::online_back; }\n if(norm(b) < norm(c)) { return ccw_result::online_front; }\n return ccw_result::on_segment;\n}\n\nbool intersect_lp(Line const& l, P const& p) {\n return abs(cross(l[1]-p, l[0]-p)) < EPS;\n}\n\nbool intersect_sp(Line const& s, P const& p) {\n return abs(s[0]-p)+abs(s[1]-p)-abs(s[1]-s[0]) < EPS; // triangle inequality\n}\n\nbool intersect_ss(Segment const& s, Segment const& t) {\n return ccw(s[0],s[1],t[0])*ccw(s[0],s[1],t[1]) <= EPS &&\n ccw(t[0],t[1],s[0])*ccw(t[0],t[1],s[1]) <= EPS;\n}\n\nbool intersect_ll(Line const& l, Line const& m) {\n return abs(cross(l[1]-l[0], m[1]-m[0])) > EPS || // non-parallel\n abs(cross(l[1]-l[0], m[0]-l[0])) < EPS; // same line\n}\n\nbool intersect_ls(Line const& l, Segment const& s) {\n return cross(l[1]-l[0], s[0]-l[0])* // s[0] is left of l\n cross(l[1]-l[0], s[1]-l[0]) < EPS; // s[1] is right of l\n}\n\nP projection(Line const& l, P const& p) {\n auto t = dot(p-l[0], l[0]-l[1]) / norm(l[0]-l[1]);\n return l[0] + t*(l[0]-l[1]);\n}\n\nP reflection(Line const& l, P const& p) {\n return p + (Real)2.0 * (projection(l, p) - p);\n}\n\nReal distance_sp(Line const& s, P const& p) {\n P const r = projection(s, p);\n if(intersect_sp(s, r)) return abs(r - p);\n return min(abs(s[0] - p), abs(s[1] - p));\n}\n\nReal distance_lp(Line const& l, P const& p) {\n return abs(p - projection(l, p));\n}\n\nbool intersect_cl(Circle const& c, Line const& l) {\n return distance_lp(l, c.p) <= c.r + EPS;\n}\n\nbool intersect_cs(Circle const& c, Line const& l) {\n if(abs(l[0] - c.p) < c.r - EPS && abs(l[1] - c.p) < c.r - EPS) { return false; }\n return distance_lp(l, c.p) <= c.r + EPS;\n}\n\nbool intersect_gs(Polygon const& g, Segment const& s) { // not verified\n auto u = const_cast<Polygon&>(g);\n rep(i, g.size()) {\n if(!intersect_sp(s, u[i]) && intersect_ss(s, u.side(i))) { return true; }\n }\n return false;\n}\n\nP crosspoint(const Line &l, const Line &m) {\n auto A = cross(l[1] - l[0], m[1] - m[0]);\n auto B = cross(l[1] - l[0], l[1] - m[0]);\n if(abs(A) < EPS && abs(B) < EPS) return m[0]; // same line\n if(abs(A) < EPS) assert(false && \"PRECONDITION NOT SATISFIED\");\n return m[0] + B / A * (m[1] - m[0]);\n}\n\npair<P, P> crosspoint(Circle const& c, Line const& l) {\n auto h = projection(l, c.p);\n auto e = (l[1]-l[0]) / abs(l[1]-l[0]);\n auto base = sqrt(c.r*c.r-abs(h-c.p)*abs(h-c.p));\n return {h+e*base, h-e*base};\n}\n\npair<P, P> crosspoint(Circle c1, Circle c2) {\n if(c1.p.real() > c2.p.real()) swap(c1, c2);\n auto const d = abs(c2.p - c1.p);\n auto const alpha = acos((c2.p.real() - c1.p.real()) / d) * ((c1.p.imag() > c2.p.imag()) ? -1.0 : 1.0);\n auto const beta = acos((c1.r * c1.r - c2.r * c2.r + d * d) / 2.0 / d / c1.r);\n return make_pair(c1.p + polar(c1.r, alpha - beta), c1.p + polar(c1.r, alpha + beta));\n}\n \nvector<P> tangent_points(Circle const& c, P const& p) {\n vector<P> ret;\n auto const sec2 = norm(p - c.p);\n auto const tan2 = max(Real(0), sec2 - c.r * c.r);\n auto const nv = (p - c.p) * c.r * c.r / sec2;\n auto const pv = (p - c.p) * P(0, -1) * c.r * sqrt(tan2) / sec2;\n ret.push_back(c.p + nv + pv);\n ret.push_back(c.p + nv - pv);\n return ret;\n}\n\n// テゥツ?堙・ツクツクclangテ」ツ?ァテ」ツ?づ」ツつ古」ツ?ーテ」ツ??(x, y)\"テ」ツ?ョテ・ツスツ「テ・ツシツ湘」ツつ津ィツェツュテ」ツ?ソテ・ツ渉姪」ツつ凝」ツ?古」ツ??」ツ?禿」ツつ古」ツつ?x y\"テ」ツ?ォテ・ツ、ツ嘉ヲツ崢エテ」ツ?凖」ツつ?\nistream& operator >> (istream& is, P& p) { Real x, y; is >> x >> y; p = P(x, y); return is; }\nostream& operator << (ostream& os, Line const& l) { return os << \"{\" << l[0] << \", \" << l[1] << \"}\"; }\n\n}\nusing namespace point_2d;\n\n\n\nint N;\nP src, dst;\nvector<Line> line;\nvector<vector<pair<int, double>>> G;\nmap<pair<int, int>, int> nd;\nmap<int, P> ndB;\nint nsize;\n\ntemplate<class T> using PQ_G = priority_queue<T, vector<T>, greater<T>>;\n\nvoid dijkstra(int s, int t) {\n\n vector<double> dist(222, inf);\n vector<int> prev(222, inf);\n dist[s] = 0;\n\n PQ_G<pair<double, int>> pq;\n pq.emplace(0, s);\n\n while(!pq.empty()) {\n double len; int pos; tie(len, pos) = pq.top(); pq.pop();\n for(auto const& e: G[pos]) {\n int next; double plen; tie(next, plen) = e;\n// cout << pos << \" -> \" << next << endl;\n if(dist[next] <= len + plen) continue;\n dist[next] = len + plen;\n prev[next] = pos;\n pq.emplace(dist[next], next);\n }\n }\n\n// cout << s << \", \" << t << endl; cout << dist[s] << \", \" << dist[t] << endl;\n\n if(dist[t] == inf) {\n cout << -1 << endl;\n }\n else {\n vector<pair<int, int>> path;\n for(int pos = t; pos != s; pos = prev[pos]) {\n assert(pos != inf);\n path.emplace_back((int)ndB[pos].real(), (int)ndB[pos].imag());\n }\n cout << (int)ndB[s].real() << \" \" << (int)ndB[s].imag() << endl;\n// cout << s << endl;\n reverse(all(path));\n for(auto& e: path)\n cout << e.first << \" \" << e.second << endl;\n// cout << nd[e] << endl;\n cout << \"0\\n\";\n }\n\n}\n\nbool merge_if_able(Line &s, Line t) {\n if (abs(cross(s[1]-s[0], t[1]-t[0])) > EPS) return false;\n if (ccw(s[0], t[0], s[1]) == +1 ||\n ccw(s[0], t[0], s[1]) == -1) return false; // not on the same line\n if (ccw(s[0], s[1], t[0]) == -2 ||\n ccw(t[0], t[1], s[0]) == -2) return false; // separated\n s = Line(min(s[0], t[0]), max(s[1], t[1]));\n return true;\n}\nvoid merge_segments(vector<Line>& segs) {\n for (int i = 0; i < segs.size(); ++i)\n if (segs[i][1] < segs[i][0])\n swap(segs[i][1], segs[i][0]);\n for (int i = 0; i < segs.size(); ++i)\n for (int j = i+1; j < segs.size(); ++j)\n if (merge_if_able(segs[i], segs[j]))\n segs[j--] = segs.back(), segs.pop_back();\n}\n\nvoid split_segments(vector<Line>& segs, vector<P> const& pts) {\n vector<Line> nsegs;\n rep(i, segs.size()) {\n vector<P> v = {segs[i][0], segs[i][1]};\n rep(j, pts.size()) {\n if(intersect_sp(segs[i], pts[j]) && !equals(segs[i][0], pts[j]) && !equals(segs[i][1], pts[j])) {\n// cout << \"check\" << endl;\n v.push_back(pts[j]);\n }\n }\n sort(all(v));\n rep(i, v.size() - 1) {\n nsegs.push_back(Line(v[i], v[i+1]));\n }\n }\n\n segs = nsegs;\n}\n\nvoid solve() {\n\n double x, y;\n cin >> x >> y;\n src = P(x, y);\n cin >> x >> y;\n dst = P(x, y);\n\n rep(i, N) {\n P p1; cin >> x >> y; p1 = P(x, y);\n P p2; cin >> x >> y; p2 = P(x, y);\n line.push_back(Line(p1, p2));\n }\n\n map<pair<int, int>, int> cnt;\n rep(i, N) {\n REP(j, i+1, N) {\n if(intersect_ss(line[i], line[j])) {\n auto p = crosspoint(line[i], line[j]);\n cnt[{p.real(), p.imag()}]++;\n }\n }\n }\n\n #define reg_node(node) \\\n {nd[{node.real(),node.imag()}] = nsize, ndB[nsize++] = node;}\n\n reg_node(src);\n reg_node(dst);\n\n #define new_node(p) \\\n nd.find({p.real(), p.imag()}) == nd.end()\n\n rep(i, N) {\n auto p = line[i][0];\n auto q = line[i][1];\n if(cnt[{p.real(), p.imag()}] > 0 && cnt[{q.real(), q.imag()}] > 0) {\n if(new_node(p))\n reg_node(p);\n if(new_node(q))\n reg_node(q);\n }\n }\n\n #define road(l) \\\n (nd.find({l[0].real(),l[0].imag()}) != nd.end() && nd.find({l[1].real(),l[1].imag()}) != nd.end())\n\n vector<Line> nl;\n\n set<int> del;\n\n rep(i, N) {\n if(road(line[i])) {\n REP(j, i+1, N) {\n if(road(line[j])) {\n if(intersect_ss(line[i], line[j])) {\n auto cp = crosspoint(line[i], line[j]);\n if(new_node(cp))\n reg_node(cp);\n if(!(equals(cp, line[i][0]) || equals(cp, line[i][1])) &&\n (equals(cp, line[j][0]) || equals(cp, line[j][1]))) {\n nl.push_back(Line(line[i][0], cp));\n nl.push_back(Line(line[i][1], cp));\n del.insert(i);\n }\n else if((equals(cp, line[i][0]) || equals(cp, line[i][1])) &&\n !(equals(cp, line[j][0]) || equals(cp, line[j][1]))) {\n nl.push_back(Line(line[j][0], cp));\n nl.push_back(Line(line[j][1], cp));\n del.insert(j);\n }\n else /*if((equals(cp, line[i][0]) || equals(cp, line[i][1])) &&\n// (equals(cp, line[j][0]) || equals(cp, line[j][1])))*/ {\n }\n }\n }\n }\n }\n }\n\n vector<Line> signs;\n\n rep(i, N) {\n if(road(line[i]) && !del.count(i))\n nl.push_back(line[i]);\n if(!road(line[i])) {\n auto sign = line[i];\n rep(k, line.size()) {\n if(road(line[k]) && intersect_ss(line[k], sign)) {\n auto cp = crosspoint(line[k], sign);\n if(equals(sign[1], cp))\n swap(sign[0], sign[1]);\n signs.push_back(sign);\n break;\n }\n }\n }\n }\n\n merge_segments(nl);\n vector<P> pts;\n for(auto& e: nd)\n pts.push_back(P(e.first.first, e.first.second));\n split_segments(nl, pts);\n\n sort(all(nl));\n nl.erase(unique(all(nl)), nl.end());\n/*\n visualizer::init(); drawlines(nl); drawlines(signs);\n\n for(auto& e: nd) {\n P k = P(e.first.first, e.first.second);\n visualizer::point(k);\n visualizer::label(k, to_string(nd[{k.real(),k.imag()}]));\n }\n*/\n\n #define corresponds_sign(r, s) \\\n intersect_ss(r, s)\n\n auto can_go = [&](P p, P s, P o) {\n return dot(p-s, o-s) > EPS;\n };\n\n set<pair<int, int>> invalids;\n\n rep(i, nl.size()) {\n rep(j, signs.size()) {\n if(corresponds_sign(nl[i], signs[j])) {\n auto& tar = nl[i];\n auto& sign = signs[j];\n if(!can_go(tar[0], sign[0], sign[1]))\n invalids.insert({nd[{tar[0].real(), tar[0].imag()}], nd[{tar[1].real(), tar[1].imag()}]});\n\n if(!can_go(tar[1], sign[0], sign[1]))\n invalids.insert({nd[{tar[1].real(), tar[1].imag()}], nd[{tar[0].real(), tar[0].imag()}]});\n }\n }\n }\n\n rep(i, nl.size()) {\n auto& tar = nl[i];\n auto s = nd[{tar[0].real(), tar[0].imag()}];\n auto t = nd[{tar[1].real(), tar[1].imag()}];\n if(!invalids.count({s, t}))\n G[s].emplace_back(t, abs(tar[0]-tar[1]));\n if(!invalids.count({t, s}))\n G[t].emplace_back(s, abs(tar[0]-tar[1]));\n }\n\n// graph::visualizer::directed_graph(G); visualizer::show_picture();\n\n dijkstra(nd[{src.real(),src.imag()}], nd[{dst.real(), dst.imag()}]);\n}\n\nvoid init() {\n line.clear();\n G.resize(222);\n rep(i, 222) G[i].clear();\n nsize = 0;\n nd.clear();\n ndB.clear();\n}\n\nint main() {\n while(cin >> N && N) {\n init();\n solve();\n }\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3232, "score_of_the_acc": -0.0307, "final_rank": 7 }, { "submission_id": "aoj_1279_1867421", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std; \n\n#define ALL(x) (x).begin(),(x).end() \n#define EACH(i,c) for(auto i=(c).begin();i!=(c).end();++i)\n\ntypedef long long ll;\nconst double eps = 1e-10;\n# 1 \"src/aoj1279.cpp\"\n# 1 \"<????????????>\"\n# 1 \"<?????????????????????>\"\n# 1 \"src/aoj1279.cpp\"\nconst double INF = 1e+10;\ntypedef double Weight;\nstruct Edge{\n int from, to;\n Weight weight;\n int rev;\n Edge(int from, int to, Weight weight) :\n from(from), to(to), weight(weight) { }\n Edge(int from, int to, Weight weight, int rev) :\n from(from), to(to), weight(weight), rev(rev){ }\n};\nbool operator < (const Edge &a, const Edge &b){\n if(a.weight != b.weight) return a.weight > b.weight;\n if(a.from != b.from) return a.from > b.from;\n return a.to > b.to;\n}\ntypedef vector<Edge> Edges;\ntypedef vector<Edges> Graph;\ntypedef vector<Weight> Array;\ntypedef vector<Array> Matrix;\n\nvoid addFlowEdge(Graph &g, int a, int b, Weight c){\n g[a].push_back(Edge(a, b, c, g[b].size()));\n g[b].push_back(Edge(b, a, 0, g[a].size() - 1));\n}\nvoid addUndirectedEdge(Graph &g, int a, int b, Weight c){\n g[a].push_back(Edge(a, b, c, g[b].size()));\n g[b].push_back(Edge(b, a, c, g[a].size() - 1));\n}\nvoid dijkstra(const Graph &g, int s, vector<Weight> &dist, vector<int> &prev){\n int n = g.size();\n dist.assign(n, INF);\n dist[s] = 0;\n prev.assign(n, -1);\n priority_queue<Edge> Q;\n Q.push(Edge(-2, s, 0));\n while(!Q.empty()){\n Edge e = Q.top(); Q.pop();\n if(prev[e.to] != -1) continue;\n prev[e.to] = e.from;\n EACH(i, g[e.to]){\n\n if(dist[i -> to] > dist[i -> from] + i -> weight){\n dist[i -> to] = dist[i -> from] + i -> weight;\n Q.push(Edge(i -> from, i -> to, dist[i -> to]));\n }\n }\n }\n}\nvector<int> buildPath(const vector<int> &prev, int t){\n vector<int> path;\n for(int v = t; v >= 0; v = prev[v])\n path.push_back(v);\n reverse(path.begin(), path.end());\n return path;\n}\n# 1 \"src/../../geometry/geometry.cpp\" 1\nconst double PI = atan(1) * 4;\n\nint sgn(double a){\n return a < -eps ? -1: a > eps ? 1: 0;\n}\nint sgn(double a, double b){\n return sgn(b - a);\n}\ntypedef complex<double> P;\ntypedef vector<P> Polygon;\nstruct Line : public vector<P> {\n Line(P a, P b) {\n push_back(a); push_back(b);\n }\n};\nstruct Segment : public Line {\n Segment(const P &a, const P &b) : Line(a,b) { }\n};\nstruct Circle{\n P o;\n double r;\n Circle() { }\n Circle(const P &o, double r) : o(o), r(r) { }\n Circle(double x, double y, double r) : o(x,y), r(r) { }\n\n bool contains(const P &p) const{\n return sgn(abs(p-o), r) <= 0;\n }\n\n bool inside(const P &p) const{\n return sgn(abs(p-o), r) < 0;\n }\n\n pair<Line,Line> tangent(const P &p) const{\n vector<Line> res;\n double d = abs(p - o), s = r*r / d, t = sqrt(r*r-s*s);\n P q = o + s/d * (p - o), e = (p - o) * P(0, 1/d);\n return make_pair(Line(p,q+e*t), Line(p,q-e*t));\n }\n};\n\nbool near(const P &a, const P &b){\n return !sgn(abs(a-b));\n}\n\nbool lessX(const P &a, const P &b){\n if(sgn(real(a), real(b))) return real(a) < real(b);\n if(sgn(imag(a), imag(b))) return imag(a) < imag(b);\n return false;\n}\nbool lessY(const P &a, const P &b){\n if(sgn(imag(a), imag(b))) return imag(a) < imag(b);\n if(sgn(real(a), real(b))) return real(a) < real(b);\n return false;\n}\ndouble dot(const P &a, const P &b){\n return real(conj(a) * b);\n}\ndouble cross(const P &a, const P &b){\n return imag(conj(a) * b);\n}\nint ccw(const P &a, P b, P c){\n assert(!near(a, b));\n b -= a; c -= a;\n if (sgn(cross(b, c)) > 0) return +1;\n if (sgn(cross(b, c)) < 0) return -1;\n if (sgn(dot(b, c)) < 0) return +2;\n if (sgn(norm(b), norm(c)) > 0) return -2;\n return 0;\n}\nbool parallel(const Line &s, const Line &t){\n return !sgn(abs(cross(s[1]-s[0], t[1]-t[0])));\n}\nbool orthogonal(const Line &s, const Line &t){\n return !sgn(abs(dot(s[1]-s[0], t[1]-t[0])));\n}\n\nP projection(const Line &s, const P &a){\n P v = s[1] - s[0];\n return s[0] + dot(v, a - s[0]) / abs(v) / abs(v) * v;\n}\n\nP reflection(const Line &s, const P &a){\n P p = projection(s, a);\n return 2.0 * p - a;\n}\n# 58 \"src/aoj1279.cpp\" 2\n# 1 \"src/../../geometry/crosspoint.cpp\" 1\n\nbool intersectSS(const Segment &s, const Segment &t){\n return ccw(s[0],s[1],t[0]) * ccw(s[0],s[1],t[1]) <= 0 &&\n ccw(t[0],t[1],s[0]) * ccw(t[0],t[1],s[1]) <= 0;\n}\n\nP crosspointSS(const Segment &s, const Segment &t){\n double a = cross(s[1] - s[0], t[1] - t[0]);\n double b = cross(s[1] - s[0], s[1] - t[0]);\n if(!sgn(abs(a)) && !sgn(abs(b))) return s[0];\n\n return t[0] + b/a * (t[1] - t[0]);\n}\nP crosspointLL(const Line &s, const Line &t){\n double a = cross(s[1] - s[0], t[1] - t[0]);\n double b = cross(s[1] - s[0], s[1] - t[0]);\n if(!sgn(abs(a)) && !sgn(abs(b))) return s[0];\n\n return t[0] + b/a * (t[1] - t[0]);\n}\n\ndouble distanceLP(const Line &s, const P &p){\n return abs(cross(p-s[0],s[1]-s[0])) / abs(s[1]-s[0]);\n}\n\ndouble distanceSP(const Segment &s, const P &p){\n if(min(dot(s[1]-s[0], p-s[0]), dot(s[0]-s[1], p-s[1])) < 0)\n return min(abs(p-s[0]), abs(p-s[1]));\n else\n return distanceLP(s, p);\n\n}\n\ndouble distanceSS(const Segment &s, const Segment &t){\n if(intersectSS(s, t)) return 0;\n return min( min(distanceSP(s, t[0]), distanceSP(s, t[1])),\n min(distanceSP(t, s[0]), distanceSP(t, s[1])));\n}\n\npair<P,P> crosspointCC(const Circle &a, const Circle &b){\n double d = abs(b.o - a.o);\n double x = (a.r * a.r - b.r * b.r + d * d) / (d * 2);\n P v = (b.o - a.o) * (1.0 / d);\n P n = P(0,1) * v;\n double h = sqrt(a.r * a.r - x * x);\n return make_pair(a.o + v * x + n * h,\n a.o + v * x - n * h);\n}\n\n\nint intersectLC(const Line &l, const Circle &c){\n double d = distanceLP(l, c.o);\n return sgn(d, c.r) + 1;\n}\n\n\n\nvector<P> crosspointLC(const Line &l, const Circle &c){\n int n = intersectLC(l, c);\n vector<P> res;\n if(!n) return res;\n P p = projection(l, c.o), e = 1.0/abs(l[1]-l[0])*(l[1]-l[0]);\n if(n == 1){\n res.push_back(p); return res;\n }\n if(sgn(abs(p - c.o)))\n e = (p - c.o) * P(0, 1/abs(p - c.o));\n double d = sqrt(c.r*c.r-norm(p-c.o));\n res.push_back(p + e * d);\n res.push_back(p - e * d);\n return res;\n}\n# 59 \"src/aoj1279.cpp\" 2\n# 1 \"src/../../geometry/arrangement.cpp\" 1\n\n\nGraph segmentArrangement(const vector<Segment> &ss, vector<P> &ps){\n for(int i = 0; i < ss.size(); ++i){\n ps.push_back(ss[i][0]);\n ps.push_back(ss[i][1]);\n for(int j=i+1;j<ss.size();++j){\n if(intersectSS(ss[i], ss[j]))\n ps.push_back(crosspointSS(ss[i], ss[j]));\n }\n }\n sort(ALL(ps), lessX); ps.erase(unique(ALL(ps), near), ps.end());\n Graph g(ps.size());\n for(int i=0;i<ss.size();++i){\n vector<pair<double, int> > lst;\n for(int j=0;j<ps.size();++j){\n if(ccw(ss[i][0], ss[i][1], ps[j]) == 0)\n lst.push_back(make_pair(norm(ss[i][0]-ps[j]), j));\n }\n sort(ALL(lst));\n for(int j=0;j+1<lst.size();++j){\n int a = lst[j].second, b = lst[j+1].second;\n addUndirectedEdge(g, a, b, abs(ps[a]-ps[b]));\n }\n }\n return g;\n}\n# 60 \"src/aoj1279.cpp\" 2\n\nint main(){\n for(;;){\n int n;\n cin >> n;\n if(n == 0) return 0;\n P s, g;\n vector<Segment> v;\n int x, y;\n cin >> x >> y; s=P(x,y);\n cin >> x >> y; g=P(x,y);\n for(int i = 0; i < n; ++i){\n int x1, y1, x2, y2;\n cin >> x1 >> y1 >> x2 >> y2;\n v.push_back(Segment(P(x1,y1), P(x2,y2)));\n }\n vector<Segment> road, signs;\n for(int i = 0; i < n; ++i){\n int f = 0;\n for(int j=0;j<n;++j) if(i != j) for(int k=0;k<2;++k){\n if(ccw(v[j][0], v[j][1], v[i][k]) == 0)\n f |= 1 << k;\n }\n if(f == 3) road.push_back(v[i]);\n else signs.push_back(v[i]);\n }\n vector<P> ps;\n Graph graph = segmentArrangement(road, ps);\n int si, gi;\n for(int i=0;i<ps.size();++i) if(abs(ps[i]-s) < eps) si = i;\n for(int i=0;i<ps.size();++i) if(abs(ps[i]-g) < eps) gi = i;\n\n EACH(i, signs){\n Segment ss = *i;\n EACH(j, graph) EACH(k, *j) {\n int a = k->from, b = k->to;\n bool f = true;\n double d = 1;\n if(ccw(ps[a], ps[b], ss[0]) == 0){\n d = dot(ss[0]-ss[1], ps[b]-ps[a]);\n }\n else if(ccw(ps[a], ps[b], ss[1]) == 0){\n d = dot(ss[1]-ss[0], ps[b]-ps[a]);\n }\n if(d < eps){\n k->weight = INF;\n }\n }\n }\n\n vector<int> prev(ps.size(),-1);\n vector<Weight> dist(ps.size());\n dijkstra(graph, si, dist, prev);\n if(dist[gi] == INF){\n cout << -1 << endl;\n }\n else{\n vector<int> path = buildPath(prev, gi);\n EACH(i, path){\n cout << (int)ps[*i].real() << \" \" << (int)ps[*i].imag() << endl;\n }\n cout << 0 << endl;\n }\n }\n}", "accuracy": 1, "time_ms": 20, "memory_kb": 3252, "score_of_the_acc": -0.1422, "final_rank": 10 }, { "submission_id": "aoj_1279_1864790", "code_snippet": "#include \"bits/stdc++.h\"\n#include<unordered_map>\n#include<unordered_set>\n#pragma warning(disable:4996)\nusing namespace std;\nusing ld = long double;\ntemplate<class T>\nusing Table = vector<vector<T>>;\nconst ld eps=1e-9;\n/* ??????????????¬ */\n\n#include <complex>\n\ntypedef long double ld;\ntypedef complex<ld> Point;\n#define REP(i,n) for(int i=0;i<(int)(n);i++)\n#define ALL(x) (x).begin(),(x).end()\n\n\nconst ld pi = acos(-1.0);\nconst ld dtop = pi / 180.;\nconst ld ptod = 1. / dtop;\n\nnamespace std {\n\tbool operator<(const Point &lhs, const Point &rhs) {\n\t\tif (lhs.real() < rhs.real() - eps) return true;\n\t\tif (lhs.real() > rhs.real() + eps) return false;\n\t\treturn lhs.imag() < rhs.imag();\n\t}\n}\n\n// ????????\\???\nPoint input_point() {\n\tld x, y;\n\tcin >> x >> y;\n\treturn Point(x, y);\n}\n\n// ????????????????????????\nbool eq(const ld a, const ld b) {\n\treturn (abs(a - b) < eps);\n}\n\n// ??????\nld dot(const Point& a, const Point& b) {\n\treturn real(conj(a) * b);\n}\n\n// ??????\nld cross(const Point& a, const Point& b) {\n\treturn imag(conj(a) * b);\n}\n\n// ??´????????????\nclass Line {\npublic:\n\tPoint a, b;\n\tLine() : a(Point(0, 0)), b(Point(0, 0)) {}\n\tLine(Point a, Point b) : a(a), b(b) {}\n\tPoint operator[](const int _num)const {\n\t\tif (_num == 0)return a;\n\t\telse if (_num == 1)return b;\n\t\telse {\n\t\t\tassert(false);\n\t\t\treturn Point();\n\t\t}\n\t}\n};\n\n// ????????????\nclass Circle {\npublic:\n\tPoint p;\n\tld r;\n\tCircle() : p(Point(0, 0)), r(0) {}\n\tCircle(Point p, ld r) : p(p), r(r) {}\n};\n\n// CCW\nint ccw(const Point& a, const Point &b, const Point &c) {\n\tconst Point nb(b - a);\n\tconst Point nc(c - a);\n\tif (cross(nb, nc) > eps) return 1; // a,b,c??????????¨???¨?????????????????¶\n\tif (cross(nb, nc) < -eps) return -1; // a,b,c???????¨???¨?????????????????¶\n\tif (dot(nb, nc) < 0) return 2; // c,a,b???????????´???????????¶\n\tif (norm(nb) < norm(nc)) return -2; // a,b,c???????????´???????????¶\n\treturn 0; // a,c,b???????????´???????????¶\n}\n\n\n/* ???????????? */\n\n// ??´?????¨??´??????????????????\nbool isis_ll(const Line& l, const Line& m) {\n\treturn !eq(cross(l.b - l.a, m.b - m.a), 0);\n}\n\n// ??´?????¨?????????????????????\nbool isis_ls(const Line& l, const Line& s) {\n\treturn isis_ll(l, s) &&\n\t\t(cross(l.b - l.a, s.a - l.a) * cross(l.b - l.a, s.b - l.a) < eps);\n}\n\n// ????????¨?????????????????????\nbool isis_ss(const Line& s, const Line& t) {\n\treturn ccw(s.a, s.b, t.a) * ccw(s.a, s.b, t.b) <= 0 &&\n\t\tccw(t.a, t.b, s.a) * ccw(t.a, t.b, s.b) <= 0;\n}\n\n// ????????´????????????\nbool isis_lp(const Line& l, const Point& p) {\n\treturn (abs(cross(l.b - p, l.a - p)) < eps);\n}\n\n// ?????????????????????\nbool isis_sp(const Line& s, const Point& p) {\n\tld n= (abs(s.a - p) + abs(s.b - p) - abs(s.b - s.a) < eps);\n\treturn (abs(s.a - p) + abs(s.b - p) - abs(s.b - s.a) < eps);\n}\n\n// ??????????¶?\nPoint proj(const Line &l, const Point& p) {\n\tld t = dot(p - l.a, l.a - l.b) / norm(l.a - l.b);\n\treturn l.a + t * (l.a - l.b);\n}\n\n// ??´?????¨??´????????????\nPoint is_ll(const Line &s, const Line& t) {\n\tPoint sv = s.b - s.a, tv = t.b - t.a;\n\tassert(cross(sv, tv) != 0);\n\treturn s.a + sv * cross(tv, t.a - s.a) / cross(tv, sv);\n}\n// ??´?????¨??´????????????\nvector<Point> is_ll2(const Line &s, const Line& t) {\n\tPoint sv = s.b - s.a, tv = t.b - t.a;\n\tif (cross(sv, tv) != 0)return vector<Point>(1, is_ll(s, t));\n\telse {\n\t\tvector<Point>ans;\n\t\tfor (int k = 0; k < 2; ++k) {\n\t\t\tif (isis_sp(s, t[k]) && find(ans.begin(), ans.end(), t[k]) == ans.end())ans.push_back(t[k]);\n\t\t\tif (isis_sp(t, s[k]) && find(ans.begin(), ans.end(), s[k]) == ans.end())ans.push_back(s[k]);\n\t\t}\n\t\treturn ans;\n\t}\n}\n// ????????¨???????????????\n//???????????£????????¨???????????¨assert(false)\nPoint is_ss(const Line &s, const Line& t) {\n\tif (isis_ss(s, t)) {\n\t\tfor (int k = 0; k < 2; ++k) {\n\t\t\tfor (int l = 0; l < 2; ++l) {\n\t\t\t\tif (s[k] == t[l])return s[k];\n\t\t\t}\n\t\t}\n\t\treturn is_ll(s, t);\n\t}\n\telse {\n\t\t//??????isis_ss?????????\n\t\tassert(false);\n\t\treturn Point(0, 0);\n\t}\n}\n// ????????¨???????????????\nvector<Point> is_ss2(const Line &s, const Line& t) {\n\tvector<Point> kouho(is_ll2(s, t));\n\tvector<Point>ans;\n\tfor (auto p : kouho) {\n\t\tif (isis_sp(s, p) && isis_sp(t, p))ans.emplace_back(p);\n\t}\n\treturn ans;\n}\n// ??´?????¨???????????¢\nld dist_lp(const Line& l, const Point& p) {\n\treturn abs(p - proj(l, p));\n}\n\n// ??´?????¨??´???????????¢\nld dist_ll(const Line& l, const Line& m) {\n\treturn isis_ll(l, m) ? 0 : dist_lp(l, m.a);\n}\n\n// ??´?????¨??????????????¢\nld dist_ls(const Line& l, const Line& s) {\n\treturn isis_ls(l, s) ? 0 : min(dist_lp(l, s.a), dist_lp(l, s.b));\n}\n\n// ????????¨???????????¢\nld dist_sp(const Line& s, const Point& p) {\n\tPoint r = proj(s, p);\n\treturn isis_sp(s, r) ? abs(r - p) : min(abs(s.a - p), abs(s.b - p));\n}\n\n// ????????¨??????????????¢\nld dist_ss(const Line& s, const Line& t) {\n\tif (isis_ss(s, t)) return 0;\n\treturn min({ dist_sp(s, t.a), dist_sp(s, t.b), dist_sp(t, s.a), dist_sp(t, s.b) });\n}\n\n\n//??´?????¨??´?????????????????????????????????\nLine bisection(const Line &s, const Line &t) {\n\tconst Point laglanju(is_ll(s, t));\n\tconst Point avec = !(abs(laglanju - s[0])<eps) ? s[0] - laglanju : s[1] - laglanju;\n\tconst Point bvec = !(abs(laglanju - t[0])<eps) ? t[0] - laglanju : t[1] - laglanju;\n\n\treturn Line(laglanju, laglanju + (abs(bvec)*avec + abs(avec)*bvec) / (abs(avec) + abs(bvec)));\n}\n\n\n//???????????´?????????????????????\n//???????????´??????????????§???????????¨????¢?????????¨?????????\nPoint inner_center(const vector<Line>&ls) {\n\tvector<Point>vertics;\n\tfor (int i = 0; i <static_cast<int>(ls.size()); ++i) {\n\t\tvertics.push_back(is_ll(ls[i], ls[(i + 1) % 3]));\n\t}\n\tif (vertics[0] == vertics[1] || vertics[1] == vertics[2] || vertics[2] == vertics[0])return vertics[0];\n\tLine bi1(bisection(Line(vertics[0], vertics[1]), Line(vertics[0], vertics[2])));\n\tLine bi2(bisection(Line(vertics[1], vertics[2]), Line(vertics[1], vertics[0])));\n\tif (bi1[0] == bi2[0])return bi1[0];\n\telse {\n\t\treturn is_ll(bi1, bi2);\n\t}\n}\n\n//???????????´?????????????????????\n//???????????´??????????????§???????????¨????¢?????????¨?????????\nvector<Point> ex_center(const vector<Line>&ls) {\n\tvector<Point>vertics;\n\tfor (int i = 0; i < static_cast<int>(ls.size()); ++i) {\n\t\tvertics.push_back(is_ll(ls[i], ls[(i + 1) % 3]));\n\t}\n\tif (abs(vertics[0] - vertics[1])<eps || abs(vertics[1] - vertics[2])<eps || (abs(vertics[2] - vertics[0])<eps))return vector<Point>();\n\tvector<Point>ecs;\n\tfor (int i = 0; i < 3; ++i) {\n\t\tLine bi1(bisection(Line(vertics[i], vertics[i] * 2.0l - vertics[(i + 2) % 3]), Line(vertics[i], vertics[(i + 1) % 3])));\n\t\tLine bi2(bisection(Line(vertics[(i + 1) % 3], vertics[(i + 1) % 3] * 2.0l - vertics[(i + 2) % 3]), Line(vertics[(i + 1) % 3], vertics[i])));\n\t\tecs.push_back(is_ll(bi1, bi2));\n\t}\n\treturn ecs;\n}\n\n\n//a,b:??????\n//c:????????§??????\n//???????????´?????????????????¢?????????????±??????????\nvector<Point> same_dis(const vector<Line>&ls) {\n\tvector<Point>vertics;\n\tvertics.push_back(is_ll(ls[0], ls[2]));\n\tvertics.push_back(is_ll(ls[1], ls[2]));\n\n\tif (abs(vertics[0] - vertics[1]) < eps)return vector<Point>{vertics[0]};\n\tLine bis(bisection(ls[0], ls[1]));\n\tvector<Point>ecs;\n\n\tLine abi(bisection(Line(vertics[0], vertics[1]), ls[0]));\n\tecs.push_back(is_ll(bis, abi));\n\n\n\tLine bbi(bisection(Line(vertics[0], 2.l*vertics[0] - vertics[1]), ls[0]));\n\tecs.push_back(is_ll(bis, bbi));\n\n\treturn ecs;\n}\n/* ??? */\n\n// ?????¨????????????\nvector<Point> is_cc(const Circle& c1, const Circle& c2) {\n\tvector<Point> res;\n\tld d = abs(c1.p - c2.p);\n\tld rc = (d * d + c1.r * c1.r - c2.r * c2.r) / (2 * d);\n\tld dfr = c1.r * c1.r - rc * rc;\n\tif (abs(dfr) < eps) dfr = 0.0;\n\telse if (dfr < 0.0) return res; // no intersection\n\tld rs = sqrt(dfr);\n\tPoint diff = (c2.p - c1.p) / d;\n\tres.push_back(c1.p + diff * Point(rc, rs));\n\tif (dfr != 0.0) res.push_back(c1.p + diff * Point(rc, -rs));\n\treturn res;\n}\n\n//???????????????????????????\n// 0 => out\n// 1 => on\n// 2 => in\nint is_in_circle(const Circle &cir, const Point& p) {\n\tld dis = abs(cir.p - p);\n\tif (dis > cir.r + eps)return 0;\n\telse if (dis < cir.r - eps)return 2;\n\telse return 1;\n}\n//???lc??????rc??????????????????\n// 0 => out\n// 1 => on\n// 2 => in\nint circle_in_circle(const Circle &lc, const Circle&rc) {\n\tld dis = abs(lc.p - rc.p);\n\tif (dis < rc.r - lc.r - eps)return 2;\n\telse if (dis>rc.r - lc.r + eps)return 0;\n\telse return 1;\n}\n\n// ?????¨??´????????????\nvector<Point> is_lc(const Circle& c, const Line& l) {\n\tvector<Point> res;\n\tld d = dist_lp(l, c.p);\n\tif (d < c.r + eps) {\n\t\tld len = (d > c.r) ? 0.0 : sqrt(c.r * c.r - d * d); //safety;\n\t\tPoint nor = (l.a - l.b) / abs(l.a - l.b);\n\t\tres.push_back(proj(l, c.p) + len * nor);\n\t\tres.push_back(proj(l, c.p) - len * nor);\n\t}\n\treturn res;\n}\n\n// ?????¨??????????????¢\nvector<Point> is_sc(const Circle& c, const Line& l) {\n\tvector<Point> v = is_lc(c, l), res;\n\tfor (Point p : v)\n\t\tif (isis_sp(l, p)) res.push_back(p);\n\treturn res;\n}\n\n// ?????¨????????\\???\nvector<Line> tangent_cp(const Circle& c, const Point& p) {\n\tvector<Line> ret;\n\tPoint v = c.p - p;\n\tld d = abs(v);\n\tld l = sqrt(norm(v) - c.r * c.r);\n\tif (isnan(l)) { return ret; }\n\tPoint v1 = v * Point(l / d, c.r / d);\n\tPoint v2 = v * Point(l / d, -c.r / d);\n\tret.push_back(Line(p, p + v1));\n\tif (l < eps) return ret;\n\tret.push_back(Line(p, p + v2));\n\treturn ret;\n}\n\n// ?????¨????????\\???\nvector<Line> tangent_cc(const Circle& c1, const Circle& c2) {\n\tvector<Line> ret;\n\tif (abs(c1.p - c2.p) - (c1.r + c2.r) > -eps) {\n\t\tPoint center = (c1.p * c2.r + c2.p * c1.r) / (c1.r + c2.r);\n\t\tret = tangent_cp(c1, center);\n\t}\n\tif (abs(c1.r - c2.r) > eps) {\n\t\tPoint out = (-c1.p * c2.r + c2.p * c1.r) / (c1.r - c2.r);\n\t\tvector<Line> nret = tangent_cp(c1, out);\n\t\tret.insert(ret.end(), ALL(nret));\n\t}\n\telse {\n\t\tPoint v = c2.p - c1.p;\n\t\tv /= abs(v);\n\t\tPoint q1 = c1.p + v * Point(0, 1) * c1.r;\n\t\tPoint q2 = c1.p + v * Point(0, -1) * c1.r;\n\t\tret.push_back(Line(q1, q1 + v));\n\t\tret.push_back(Line(q2, q2 + v));\n\t}\n\treturn ret;\n}\n//??????????????????????????¢???\nld two_circle_area(const Circle&l, const Circle&r) {\n\tld dis = abs(l.p - r.p);\n\tif (dis > l.r + r.r)return 0;\n\telse if (dis + r.r < l.r) {\n\t\treturn r.r*r.r*pi;\n\t}\n\telse if (dis + l.r < r.r) {\n\t\treturn l.r*l.r*pi;\n\t}\n\telse {\n\t\tld ans = (l.r)*(l.r)*acos((dis*dis + l.r*l.r - r.r*r.r) / (2 * dis*l.r)) +\n\t\t\t(r.r)*(r.r)*acos((dis*dis + r.r*r.r - l.r*l.r) / (2 * dis*r.r)) -\n\t\t\tsqrt(4 * dis*dis*l.r*l.r - (dis*dis + l.r*l.r - r.r*r.r)*(dis*dis + l.r*l.r - r.r*r.r)) / 2;\n\t\treturn ans;\n\t}\n\n}\n\n/* ????§???¢ */\n\ntypedef vector<Point> Polygon;\n\n// ??¢???\nld area(const Polygon &p) {\n\tld res = 0;\n\tint n = p.size();\n\tREP(j, n) res += cross(p[j], p[(j + 1) % n]);\n\treturn res / 2;\n}\n\n// ????§???¢????????¢??????\nbool is_counter_clockwise(const Polygon &poly) {\n\tld angle = 0;\n\tint n = poly.size();\n\tREP(i, n) {\n\t\tPoint a = poly[i], b = poly[(i + 1) % n], c = poly[(i + 2) % n];\n\t\tangle += arg((c - b) / (b - a));\n\t}\n\treturn angle > eps;\n}\n\n// ??????????????????\n// 0 => out\n// 1 => on\n// 2 => in\nint is_in_polygon(const Polygon &poly, const Point& p) {\n\tld angle = 0;\n\tint n = poly.size();\n\tREP(i, n) {\n\t\tPoint a = poly[i], b = poly[(i + 1) % n];\n\t\tif (isis_sp(Line(a, b), p)) return 1;\n\t\tangle += arg((b - p) / (a - p));\n\t}\n\treturn eq(angle, 0) ? 0 : 2;\n}\n//??????????????????2?????????\nenum { OUT, ON, IN };\nint convex_contains(const Polygon &P, const Point &p) {\n\tconst int n = P.size();\n\tPoint g = (P[0] + P[n / 3] + P[2 * n / 3]) / 3.0l; // inner-point\n\tint a = 0, b = n;\n\twhile (a + 1 < b) { // invariant: c is in fan g-P[a]-P[b]\n\t\tint c = (a + b) / 2;\n\t\tif (cross(P[a] - g, P[c] - g) > 0) { // angle < 180 deg\n\t\t\tif (cross(P[a] - g, p - g) > 0 && cross(P[c] - g, p - g) < 0) b = c;\n\t\t\telse a = c;\n\t\t}\n\t\telse {\n\t\t\tif (cross(P[a] - g, p - g) < 0 && cross(P[c] - g, p - g) > 0) a = c;\n\t\t\telse b = c;\n\t\t}\n\t}\n\tb %= n;\n\tif (cross(P[a] - p, P[b] - p) < 0) return 0;\n\tif (cross(P[a] - p, P[b] - p) > 0) return 2;\n\treturn 1;\n}\n\n// ??????\n// ???????????????????????¨????????????????????§??¨???\nPolygon convex_hull(vector<Point> ps) {\n\tint n = ps.size();\n\tint k = 0;\n\tsort(ps.begin(), ps.end());\n\tPolygon ch(2 * n);\n\tfor (int i = 0; i < n; ch[k++] = ps[i++])\n\t\twhile (k >= 2 && ccw(ch[k - 2], ch[k - 1], ps[i]) <= 0) --k;\n\tfor (int i = n - 2, t = k + 1; i >= 0; ch[k++] = ps[i--])\n\t\twhile (k >= t && ccw(ch[k - 2], ch[k - 1], ps[i]) <= 0) --k;\n\tch.resize(k - 1);\n\treturn ch;\n}\n\n\n\n// ????????????\nvector<Polygon> convex_cut(const Polygon &ps, const Line& l) {\n\tint n = ps.size();\n\tPolygon Q;\n\tPolygon R;\n\tREP(i, n) {\n\t\tPoint A = ps[i], B = ps[(i + 1) % n];\n\t\tLine m = Line(A, B);\n\t\tif (ccw(l.a, l.b, A) != -1) Q.push_back(A);\n\t\tif (ccw(l.a, l.b, A) != 1) R.push_back(A);\n\t\tif (ccw(l.a, l.b, A) * ccw(l.a, l.b, B) < 0 && isis_ll(l, m)) {\n\t\t\tQ.push_back(is_ll(l, m));\n\t\t\tR.push_back(is_ll(l, m));\n\t\t}\n\t}\n\tconst vector<Polygon>polys{ Q,R };\n\treturn polys;\n}\n\n\n/* ??¢??¬??????????????? */\nvoid add_point(vector<Point> &ps, const Point p) {\n\tfor (Point q : ps) if (abs(q - p) < eps) return;\n\tps.push_back(p);\n}\n\ntypedef ld Weight;\nstruct Edge {\n\tint src, dst;\n\tWeight weight;\n\tEdge(int src, int dst, Weight weight) :\n\t\tsrc(src), dst(dst), weight(weight) { }\n};\n\ntypedef vector<Edge> Edges;\ntypedef vector<Edges> Graph;\n\nvoid add_edge(Graph &g, const int from, const int to, const Weight weight) {\n\tg[from].push_back(Edge{ from, to, weight });\n}\n\npair<vector<Point>,Graph> segment_arrangement(const vector<Line> &s, const vector<Point> &p) {\n\tint n = p.size(), m = s.size();\n\tGraph g(n);\n\tREP(i, m) {\n\t\tvector<pair<ld, int>> vec;\n\t\tREP(j, n) if (isis_sp(s[i], p[j]))\n\t\t\tvec.emplace_back(abs(s[i].a - p[j]), j);\n\t\tsort(ALL(vec));\n\t\tREP(j, vec.size() - 1) {\n\t\t\tint from = vec[j].second, to = vec[j + 1].second;\n\t\t\tadd_edge(g, from, to, static_cast<Weight>(abs(p[from] - p[to])));\n\t\t\tadd_edge(g, to,from, static_cast<Weight>(abs(p[from] - p[to])));\n\t\t}\n\t}\n\tauto a(make_pair(p, g));\n\treturn a;\n}\npair<vector<Point>, Graph> sennbunn_arrangement(const vector<Line>&s) {\n\tvector<Point>crss;\n\tfor (int i = 0; i < static_cast<int>(s.size()); ++i) {\n\t\tfor (int j = i + 1; j < static_cast<int>(s.size()); ++j) {\n\t\t\tif (isis_ss(s[i], s[j])) {\n\t\t\t\tcrss.push_back(is_ll2(s[i], s[j])[0]);\n\t\t\t}\n\t\t}\n\t}\n\tfor (int i = 0; i <static_cast<int>(s.size()); ++i) {\n\t\tcrss.push_back(s[i][0]);\n\t\tcrss.push_back(s[i][1]);\n\t}\n\tsort(crss.begin(), crss.end());\n\tcrss.erase(unique(crss.begin(), crss.end()), crss.end());\n\treturn segment_arrangement(s, crss);\n}\n\n\npair<bool, vector<Weight>> spfa(const int v_num, const vector<vector<Edge>>&es, const int start) {\n\tvector<Weight>diss(v_num, INT_MAX);\n\tqueue<int>que;\n\tvector<bool>use(v_num);\n\tvector<int>count(v_num);\n\tque.emplace(start);\n\tdiss[start] = 0;\n\twhile (!que.empty()) {\n\t\tint src(que.front());\n\t\tque.pop();\n\t\tuse[src] = false;\n\t\tfor (auto e : es[src]) {\n\t\t\tconst int d = e.dst;\n\t\t\tif (diss[src] + e.weight < diss[d]) {\n\t\t\t\tdiss[d] = diss[src] + e.weight;\n\t\t\t\tif (!use[d]) {\n\t\t\t\t\tuse[d] = true;\n\t\t\t\t\tcount[d]++;\n\t\t\t\t\tif (count[d] >= v_num)return make_pair(false, vector<Weight>());\n\t\t\t\t\tque.emplace(d);\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n\treturn make_pair(true, diss);\n}\nstruct aa {\n\tint now;\n\tld time;\n};\nclass Compare {\npublic:\n\t//aa?????????????????¶\n\tbool operator()(const aa&l, const aa&r) {\n\t\treturn l.time> r.time;\n\t}\n};\nint main() {\n\twhile (1) {\n\t\tint N; cin >> N;\n\t\tif (!N)break;\n\t\tint xs, ys, xg, yg; cin >> xs >> ys >> xg >> yg;\n\t\tvector<Line>ls;\n\t\tPoint start(xs, ys), goal(xg, yg);\n\t\tfor (int i = 0; i < N; ++i) {\n\t\t\tint x1, y1, x2, y2; cin >> x1 >> y1 >> x2 >> y2;\n\t\t\tls.emplace_back(Point(x1, y1), Point(x2, y2));\n\t\t}\n\t\tvector<Line>roads;\n\t\tvector<Line>signs;\n\t\tfor (int i = 0; i < N; ++i) {\n\t\t\tint touchnum = 0;\n\t\t\tfor (int j = 0; j < 2; ++j) {\n\t\t\t\tPoint p(ls[i][j]);\n\t\t\t\tbool touch = false;\n\t\t\t\tfor (int k = 0; k < N; ++k) {\n\t\t\t\t\tif (i == k)continue;\n\t\t\t\t\telse {\n\t\t\t\t\t\tif (isis_sp(ls[k],p))touch = true;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\tif (touch)touchnum++;\n\t\t\t}\n\t\t\tif (touchnum == 2)roads.emplace_back(ls[i]);\n\t\t\telse signs.emplace_back(ls[i]);\n\t\t}\n\t\tauto pa(sennbunn_arrangement(roads));\n\t\tGraph g(pa.second);\n\t\tvector<Point>ps(pa.first);\n\t\tint startid, goalid;\n\t\tfor (int i = 0; i < ps.size();++i){\n\t\t\tPoint p(ps[i]);\n\t\t\tif (p == start)startid = i;\n\t\t\telse if (p == goal)goalid = i;\n\t\t}\n\t\tfor (auto& es :g){\n\t\t\tfor (auto& e : es) {\n\t\t\t\tLine l(ps[e.src],ps[e.dst]);\n\t\t\t\tfor (int k = 0; k < signs.size(); ++k) {\n\t\t\t\t\tLine si(signs[k]);\n\t\t\t\t\tif (isis_ss(l, si)) {\n\t\t\t\t\t\tif (isis_sp(l, si[1]))swap(si.a, si.b);\n\t\t\t\t\t\tld theta = (dot(l[1] - l[0], si[1] - si[0]));\n\t\t\t\t\t\tif (theta > -eps) {\n\t\t\t\t\t\t\te.weight = 1e18;\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\t\t\n\t\t\t\t\n\t\t\t}\n\t\t}\n\t\tvector<ld>times(g.size(),1e18);\n\t\tvector<vector<int>>memos(g.size());\n\t\tmemos[startid].push_back(startid);\n\t\tpriority_queue<aa,vector<aa>,Compare>que;\n\t\ttimes[startid] = 0;\n\t\tque.push(aa{ startid,0 });\n\t\twhile (!que.empty()) {\n\t\t\taa atop(que.top());\n\t\t\tque.pop();\n\t\t\tif (atop.now == goalid)break;\n\t\t\tfor (auto e : g[atop.now]) {\n\t\t\t\tif (times[e.dst] > atop.time + e.weight) {\n\t\t\t\t\ttimes[e.dst] = atop.time + e.weight;\n\t\t\t\t\tmemos[e.dst] = memos[e.src];\n\t\t\t\t\tmemos[e.dst].emplace_back(e.dst);\n\t\t\t\t\tque.push(aa{ e.dst,atop.time + e.weight });\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\tif (memos[goalid].empty()) {\n\t\t\tcout << -1 << endl;\n\t\t}\n\t\telse {\n\n\t\t\tfor (auto m : memos[goalid]) {\n\t\t\t\tint x = int((ps[m].real()+eps));\n\t\t\t\tint y =int(ps[m].imag()+eps);\n\t\t\t\tcout << x << \" \" << y << endl;\n\t\t\t}\n\t\t\tcout << 0 << endl;\n\t\t}\n\t}\n\treturn 0;\n}", "accuracy": 1, "time_ms": 70, "memory_kb": 3300, "score_of_the_acc": -0.6985, "final_rank": 17 }, { "submission_id": "aoj_1279_1472597", "code_snippet": "#include<iostream>\n#include<algorithm>\n#include<vector>\n#include<queue>\n#include<map>\n#include<set>\n#include<string>\n#include<stack>\n#include<cstdio>\n#include<cmath>\nusing namespace std;\n\ntypedef long long ll;\ntypedef double ld;\ntypedef pair<short,short> P;\ntypedef pair<ll,P> P1;\n\n#define fr first\n#define sc second\n#define mp make_pair\n#define pb push_back\n#define rep(i,x) for(int i=0;i<x;i++)\n#define rep1(i,x) for(int i=1;i<=x;i++)\n#define rrep(i,x) for(int i=x-1;i>=0;i--)\n#define rrep1(i,x) for(int i=x;i>0;i--)\n#define sor(v) sort(v.begin(),v.end())\n#define rev(s) reverse(s.begin(),s.end())\n#define lb(vec,a) lower_bound(vec.begin(),vec.end(),a)\n#define ub(vec,a) upper_bound(vec.begin(),vec.end(),a)\n#define uniq(vec) vec.erase(unique(vec.begin(),vec.end()),vec.end())\n#define mp1(a,b,c) P1(a,P(b,c))\n\nconst int INF=1000000000;\nconst int dir_4[4][2]={{1,0},{0,1},{-1,0},{0,-1}};\nconst int dir_8[8][2]={{1,0},{1,1},{0,1},{-1,1},{-1,0},{-1,-1},{0,-1},{1,-1}};\n\nint gcd(int x,int y){\n\tif(y == 0)return x;\n\treturn gcd(y,x%y);\n}\n\nvector<P> dir[1002][1002];\nint f(int x,int y,int dx,int dy){\n\tint ret = 0;\n\trep(i,dir[x][y].size()){\n\t\tP p = dir[x][y][i];\n\t\tint t = (p.fr-x)*dx + (p.sc-y)*dy;\n\t\tif(t > 0)ret |= 1;\n\t\telse if(t == 0)ret |= 3;\n\t\telse ret |= 2;\n\t}\n\treturn ret;\n}\n\nld cul_dist(ld x1,ld y1,ld x2,ld y2){\n\treturn sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2));\n}\n\t\tstatic int n;\n\t\tstatic int xs,ys,xg,yg;\n\t\tstatic short cnt[1002][1002];\n\t\tstatic vector<P> G[1002][1002];\n\t\tstatic bool used[1002][1002];\n\t\tstatic double dist[1002][1002];\n\t\tstatic P pre[1002][1002];\n\t\tpriority_queue<pair<ld,P>,vector<pair<ld,P>>,greater<pair<ld,P>>> que;\nint main(){\n\twhile(1){\n\t\tstatic int x1[202],y1[202],x2[202],y2[202];\n\t\tscanf(\"%d\",&n); if(n == 0)break;\n\t\tscanf(\"%d%d%d%d\",&xs,&ys,&xg,&yg);\n\t\trep(i,n){\n\t\t\tscanf(\"%d%d%d%d\",&x1[i],&y1[i],&x2[i],&y2[i]);\n\t\t}\n\t\t\n\t\trep(i,1002)rep(j,1002)cnt[i][j] = 0;\n\t\trep(i,n){\n\t\t\tif(x1[i] > x2[i] || (x1[i] == x2[i] && y1[i] > y2[i])){\n\t\t\t\tswap(x1[i],x2[i]);\n\t\t\t\tswap(y1[i],y2[i]);\n\t\t\t}\n\t\t\tcnt[x1[i]][y1[i]] ++;\n\t\t\tcnt[x2[i]][y2[i]] ++;\n\t\t}\n\t\t\n\t\trep(i,n){\n\t\t\tint dx = x2[i]-x1[i];\n\t\t\tint dy = y2[i]-y1[i];\n\t\t\tint g = gcd(abs(dx),abs(dy));\n\t\t\tdx /= g; dy /= g;\n\t\t\tint locx = x1[i]+dx , locy = y1[i]+dy;\n\t\t\twhile(!(locx == x2[i] && locy == y2[i])){\n\t\t\t\tcnt[locx][locy] ++;\n\t\t\t\tlocx += dx; locy += dy;\n\t\t\t}\n\t\t}\n\t\t\n\t\trep(i,1002)rep(j,1002)dir[i][j].clear();\n\t\trep(i,n){\n\t\t\tif(cnt[x1[i]][y1[i]] == 1){\n\t\t\t\tdir[x2[i]][y2[i]].pb(P(x1[i],y1[i]));\n\t\t\t}\n\t\t\telse if(cnt[x2[i]][y2[i]] == 1){\n\t\t\t\tdir[x1[i]][y1[i]].pb(P(x2[i],y2[i]));\n\t\t\t}\n\t\t}\n\t\t\n\t\trep(i,1002)rep(j,1002)G[i][j].clear();\n\t\trep(i,n){\n\t\t\tif(cnt[x1[i]][y1[i]] == 1 || cnt[x2[i]][y2[i]] == 1)continue;\n\t\t\tint dx = x2[i]-x1[i];\n\t\t\tint dy = y2[i]-y1[i];\n\t\t\tint g = gcd(abs(dx),abs(dy));\n\t\t\tdx /= g; dy /= g;\n\t\t\tint prex1 = -1 , prey1 = -1;\n\t\t\tint prex2 = -1 , prey2 = -1;\n\t\t\tint locx = x1[i] , locy = y1[i];\n\t\t\twhile(locx <= x2[i] && !(dx == 0 && locy > y2[i])){\n\t\t\t\tif(cnt[locx][locy] >= 1){\n\t\t\t\t\tint t = f(locx,locy,dx,dy);\n\t\t\t\t\tif((t&1) == 0){\n\t\t\t\t\t\tif(prex1 != -1)G[prex1][prey1].pb(P(locx,locy));\n\t\t\t\t\t\tprex1 = locx; prey1 = locy;\n\t\t\t\t\t}\n\t\t\t\t\telse { prex1 = -1; prey1 = -1; }\n\t\t\t\t\tif((t&2) == 0){\n\t\t\t\t\t\tif(prex2 != -1)G[locx][locy].pb(P(prex2,prey2));\n\t\t\t\t\t\tprex2 = locx; prey2 = locy;\n\t\t\t\t\t}\n\t\t\t\t\telse { prex2 = -1; prey2 = -1; }\n\t\t\t\t}\n\t\t\t\tlocx += dx; locy += dy;\n\t\t\t}\n\t\t}\n\t\t\n\t\trep(i,1002)rep(j,1002){\n\t\t\tused[i][j] = false;\n\t\t\tdist[i][j] = (ld)INF;\n\t\t\tpre[i][j] = P ( -1 , -1 );\n\t\t}\n\t\tdist[xs][ys] = 0.0;\n\t\tque.push ( pair<ld,P> ( 0.0 , P ( xs,ys ) ) );\n\t\twhile(!que.empty()){\n\t\t\tpair<ld,P> p = que.top(); que.pop();\n\t\t\tif(used[p.sc.fr][p.sc.sc])continue;\n\t\t\tused[p.sc.fr][p.sc.sc] = true;\n\t\t\trep(i,G[p.sc.fr][p.sc.sc].size()){\n\t\t\t\tP to = G[p.sc.fr][p.sc.sc][i];\n\t\t\t\tif(dist[to.fr][to.sc] > dist[p.sc.fr][p.sc.sc] + cul_dist(p.sc.fr,p.sc.sc,to.fr,to.sc)){\n\t\t\t\t\tdist[to.fr][to.sc] = dist[p.sc.fr][p.sc.sc] + cul_dist(p.sc.fr,p.sc.sc,to.fr,to.sc);\n\t\t\t\t\tpre[to.fr][to.sc] = p.sc;\n\t\t\t\t\tque.push( pair<ld,P> ( dist[to.fr][to.sc] , to ) );\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\t\n\t\tif(pre[xg][yg].fr == -1)cout << -1 << endl;\n\t\telse {\n\t\t\tvector<P> ans;\n\t\t\tP loc = P ( xg , yg );\n\t\t\tans.pb(loc);\n\t\t\twhile(loc != P(xs,ys)){\n\t\t\t\tloc = pre[loc.fr][loc.sc];\n\t\t\t\tans.pb(loc);\n\t\t\t}\n\t\t\trrep(i,ans.size()){\n\t\t\t\tif(!(cnt[ans[i].fr][ans[i].sc]-dir[ans[i].fr][ans[i].sc].size() == 1))printf(\"%d %d\\n\",ans[i].fr,ans[i].sc);\n\t\t\t}\n\t\t\tputs(\"0\");\n\t\t}\n\t}\n}", "accuracy": 1, "time_ms": 100, "memory_kb": 63504, "score_of_the_acc": -2, "final_rank": 20 }, { "submission_id": "aoj_1279_1390513", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\ntypedef complex<double> P;\n\nconst double inf = 1e10;\nconst double eps = 1e-8;\n\nbool equals(double a, double b) {\n return abs(a - b) < eps;\n}\n\nnamespace std {\n bool operator < (const P &a, const P &b) {\n return ( a.real() != b.real()\n ? a.real() < b.real() : a.imag() < b.imag() );\n }\n}\n\ndouble dot(P a, P b) {\n return a.real()*b.real() + a.imag()*b.imag();\n}\n\ndouble cross(P a, P b) {\n return a.real()*b.imag() - a.imag()*b.real();\n}\n\nint ccw(P p, P a, P b) {\n a -= p;\n b -= p;\n if(cross(a, b) > eps) return 1;\n if(cross(a, b) < -eps) return -1;\n if(dot(a, b) < -eps) return 2;\n if(norm(a) + eps < norm(b)) return -2;\n return 0;\n}\n\nbool isIntersect(P a1, P a2, P b1, P b2) {\n return ( ccw(a1, a2, b1) * ccw(a1, a2, b2) <= 0 &&\n ccw(b1, b2, a1) * ccw(b1, b2, a2) <= 0 );\n}\n\nP getCrossP(P a1, P a2, P b1, P b2) {\n P a = a2 - a1;\n P b = b2 - b1;\n return a1 + a * cross(b, b1 - a1) / cross(b, a);\n}\n\nstruct Edge { int v; double w; int rev; };\ntypedef vector<vector<Edge> > Graph;\n\nGraph segmentArrangement(const vector<P> &s1, const vector<P> &s2,\n vector<P> &ps) {\n int m = s1.size();\n for(int i = 0; i < m; ++i) {\n ps.push_back(s1[i]);\n ps.push_back(s2[i]);\n for(int j = i+1; j < m; ++j) {\n if(equals(cross(s1[i] - s2[i], s1[j] - s2[j]), 0.0)) continue;\n if(!isIntersect(s1[i], s2[i], s1[j], s2[j])) continue;\n ps.push_back(getCrossP(s1[i], s2[i], s1[j], s2[j]));\n }\n }\n sort(ps.begin(), ps.end());\n ps.erase(unique(ps.begin(), ps.end()), ps.end());\n\n int n = ps.size();\n Graph g(n);\n for(int i = 0; i< m; ++i) {\n vector<pair<double, int> > v;\n for(int j = 0; j < n; ++j) {\n if(ps[j] == s1[i] || ps[j] == s2[i] ||\n !ccw(s1[i], s2[i], ps[j])) {\n v.push_back(make_pair(norm(s1[i] - ps[j]), j));\n }\n }\n sort(v.begin(), v.end());\n for(int j = 0; j+1 < v.size(); ++j) {\n int a = v[j].second, b = v[j+1].second;\n double w = abs(ps[a] - ps[b]);\n g[a].push_back((Edge){b, w, g[b].size()});\n g[b].push_back((Edge){a, w, g[a].size()-1});\n }\n }\n return g;\n}\n\ntypedef pair<double, int> Pair;\n\nint n;\nP s, g;\nvector<P> s1, s2, ps;\nvector<int> isSign;\nint N, src, dst;\nGraph G;\n\nint main() {\n while(cin >> n && n) {\n cin >> s.real() >> s.imag();\n cin >> g.real() >> g.imag();\n s1.resize(n);\n s2.resize(n);\n for(int i = 0; i < n; ++i) {\n cin >> s1[i].real() >> s1[i].imag()\n >> s2[i].real() >> s2[i].imag();\n }\n\n ps.clear();\n G = segmentArrangement(s1, s2, ps);\n N = G.size();\n src = find(ps.begin(), ps.end(), s) - ps.begin();\n dst = find(ps.begin(), ps.end(), g) - ps.begin();\n\n isSign = vector<int>(ps.size());\n for(int i = 0; i < N; ++i) {\n if(G[i].size() == 1) {\n int a = i;\n int b = G[a][0].v;\n isSign[b] = true;\n int pc = 0;\n int c = G[b][pc].v;\n while(G[c].size() == 1) c = G[b][++pc].v;\n int pd = G[b].size()-1;\n int d = G[b][pd].v;\n while(G[d].size() == 1) d = G[b][--pd].v;\n double s = dot(ps[a] - ps[b], ps[c] - ps[b]);\n if(s >= 0.0) G[b][pc].w = inf;\n if(s <= 0.0) G[b][pd].w = inf;\n G[b][G[a][0].rev].w = inf;\n }\n }\n\n vector<double> cost(N, inf);\n priority_queue<Pair, vector<Pair>, greater<Pair> > que;\n vector<int> prev(N, -1);\n cost[src] = 0.0;\n que.push(make_pair(0.0, src));\n while(que.size()) {\n const Pair s = que.top(); que.pop();\n if(cost[s.second] < s.first) continue;\n if(s.second == dst) break;\n for(int i = 0; i < G[s.second].size(); ++i) {\n if(G[s.second][i].w == inf) continue;\n const Pair t(s.first + G[s.second][i].w, G[s.second][i].v);\n if(cost[t.second] <= t.first) continue;\n cost[t.second] = t.first;\n prev[t.second] = s.second;\n que.push(t);\n }\n }\n\n if(cost[dst] == inf) {\n cout << -1 << endl;\n } else {\n vector<int> vs;\n for(int v = dst; v != -1; v = prev[v]) {\n vs.push_back(v);\n }\n for(int i = vs.size()-1; i >= 0; --i) {\n if(isSign[vs[i]]) continue;\n cout << (int)ps[vs[i]].real() << \" \" \n << (int)ps[vs[i]].imag() << endl;\n }\n cout << 0 << endl;\n }\n }\n return 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 1364, "score_of_the_acc": -0.0007, "final_rank": 2 }, { "submission_id": "aoj_1279_1252743", "code_snippet": "#include <cstdlib>\n#include <cmath>\n#include <climits>\n#include <cfloat>\n#include <map>\n#include <set>\n#include <iostream>\n#include <string>\n#include <vector>\n#include <algorithm>\n#include <sstream>\n#include <complex>\n#include <stack>\n#include <queue>\n#include <cstdio>\n#include <cstring>\n#include <iterator>\n#include <bitset>\n\n#define cauto const auto&\n\nusing namespace std;\n\n\nnamespace{\ntypedef long long ll;\ntypedef pair<int,int> pii;\ntypedef pair<ll,ll> pll;\n\ntypedef vector<int> vint;\ntypedef vector<vector<int> > vvint;\ntypedef vector<long long> vll, vLL;\ntypedef vector<vector<long long> > vvll, vvLL;\n\n#define VV(T) vector<vector< T > >\n\ntemplate <class T>\nvoid initvv(vector<vector<T> > &v, int a, int b, const T &t = T()){\n v.assign(a, vector<T>(b, t));\n}\n\ntemplate <class F, class T>\nvoid convert(const F &f, T &t){\n stringstream ss;\n ss << f;\n ss >> t;\n}\n\n\n#define reep(i,a,b) for(int i=(a);i<(b);++i)\n#define rep(i,n) reep((i),0,(n))\n#define ALL(v) (v).begin(),(v).end()\n#define PB push_back\n#define fi first\n#define se second\n#define mkp make_pair\n#define RALL(v) (v).rbegin(),(v).rend()\n\n#define MOD 1000000007LL\ntypedef pair<double,double> pdd;\nconst double EPS = 1e-8;\nconst double INF = 1e12;\nconst double PI = asin(0.5)*6;\ntypedef complex<double> P;\n\n#define X real()\n#define Y imag()\n\n#define Curr(P,i) P[(i)%P.size()]\n#define Next(P,i) P[(i+1)%P.size()]\n#define Prev(P,i) P[(i+P.size()-1)%P.size()]\n\nnamespace std{\n bool operator<(const P a,const P b){\n return a.X != b.X ? a.X < b.X : a.Y < b.Y;\n }\n}\ndouble cross(const P a,const P b){\n return (conj(a)*b).imag();\n}\ndouble dot(const P a,const P b){\n return (conj(a)*b).real();\n}\nint ccw(P a,P b,P c){\n b-=a;\n c-=a;\n}\n\nstruct L : public vector<P>{\n L(const P a,const P b){\n push_back(a),push_back(b);\n }\n};\ntypedef L S;\nbool isCrossSP(S a,P p){\n return abs(a[0]-p)+abs(a[1]-p)-abs(a[0]-a[1]) < EPS;\n}\npdd ptopdd(P a){\n\treturn pdd(a.real(),a.imag());\n}\nP pddtop(pdd a){\n\treturn P(a.first,a.second);\n}\nint foo3(S a,P b,P c){\n\tdouble ab0=abs(a[0]-b);\n\tdouble ac0=abs(a[0]-c);\n\tdouble bc =abs(b-c);\n\tif(abs(ac0*ac0-ab0*ab0-bc*bc)<EPS){\n\t\treturn 2;\n\t}\n\tif(ac0*ac0-ab0*ab0-bc*bc>EPS){\n\t\treturn 0;\n\t}\n\treturn 1;\n}\nbool operator<(P a,P b){\n\tif(a.X==b.X){\n\t\treturn a.Y<b.Y;\n\t}\n\treturn a.X<b.X;\n}\nvoid mainmain(){\n\tint n;\n\twhile(cin>>n,n){\n\t\tP s,g;\n\t\tdouble a[4];\n\t\trep(i,4) cin>>a[i];\n\t\ts=P(a[0],a[1]);\n\t\tg=P(a[2],a[3]);\n\t\tvector<S> v1;\n\t\tS tmp(s,g);\n\t\trep(i,n){\n\t\t\tdouble a[4];\n\t\t\trep(j,4) cin>>a[j];\n\t\t\tP t1=P(a[0],a[1]);\n\t\t\tP t2=P(a[2],a[3]);\n\t\t\tS tmp=S(t1,t2);\n\t\t\tv1.PB(tmp);\n\t\t}\n\t\tvector<S> sign,road;\n\t\trep(i,n){\n\t\t\tbool f1=true;\n\t\t\tbool f2=true;\n\t\t\trep(j,n){\n\t\t\t\tif(i==j) continue;\n\t\t\t\tif(isCrossSP(v1[j],v1[i][0])){\n\t\t\t\t\tf1=false;\n\t\t\t\t}\n\t\t\t\tif(isCrossSP(v1[j],v1[i][1])){\n\t\t\t\t\tf2=false;\n\t\t\t\t}\n\t\t\t}\n\t\t\tif(f1||f2){\n\t\t\t\tsign.PB(v1[i]);\n\t\t\t}\n\t\t\telse{\n\t\t\t\troad.PB(v1[i]);\n\t\t\t}\n\t\t}\n\t\tvector<S> troad;\n\t\trep(i,road.size()){\n\t\t\tvector<S> tmp;\n\t\t\ttmp.PB(road[i]);\n\t\t\trep(j,road.size()){\n\t\t\t\tif(i==j) continue;\n\t\t\t\trep(k,tmp.size()){\n\t\t\t\t\tbool f=false;\n\t\t\t\t\tP p;\n\t\t\t\t\trep(o,2){\n\t\t\t\t\t\tif(isCrossSP(tmp[k],road[j][o])){\n\t\t\t\t\t\t\tf=true;\n\t\t\t\t\t\t\tp=road[j][o];\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t\tif(f&&abs(tmp[k][0]-p)>EPS&&abs(tmp[k][1]-p)>EPS){\n\t\t\t\t\t\tS tt=tmp[k];\n\t\t\t\t\t\ttmp[k]=S(tt[0],p);\n\t\t\t\t\t\ttmp.PB(S(tt[1],p));\n\t\t\t\t\t\tk--;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t\trep(j,tmp.size()){\n\t\t\t\ttroad.PB(tmp[j]);\n\t\t\t}\n\t\t}\n\t\troad=troad;\n\t\tmap<pdd,int> ma;\n\t\tvector<pii> vp;\n\t\trep(i,road.size()){\n\t\t\trep(j,2){\n\t\t\t\tpdd t=ptopdd(road[i][j]);\n\t\t\t\tif(ma.find(t)==ma.end()){\n\t\t\t\t\tma[t]=0;\n\t\t\t\t\tvp.PB(pii(int(t.fi),int(t.se)));\n\t\t\t\t\tma[t]=ma.size()-1;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\ttypedef pair<int,double> pid;\n\t\tVV(pid) vv(ma.size());\n\t\trep(i,road.size()){\n\t\t\tbool f[2]={true,true};\n\t\t\trep(j,sign.size()){\n\t\t\t\trep(k,2){\n\t\t\t\t\tif(isCrossSP(road[i],sign[j][k])){\n\t\t\t\t\t\tint aa=foo3(road[i],sign[j][k],sign[j][1-k]);\n\t\t\t\t\t\tif(aa==2){\n\t\t\t\t\t\t\tf[0]=f[1]=false;\n\t\t\t\t\t\t}\n\t\t\t\t\t\tif(aa==0){\n\t\t\t\t\t\t\tf[0]=false;\n\t\t\t\t\t\t}\n\t\t\t\t\t\tif(aa==1){\n\t\t\t\t\t\t\tf[1]=false;\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t\trep(j,2){\n\t\t\t\tif(f[j]){\n\t\t\t\t\tvv[ma[ptopdd(road[i][j])]].PB(pid(ma[ptopdd(road[i][1-j])],abs(road[i][0]-road[i][1])));\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\tpriority_queue<pair<double,pii> > pq;\n\t\tvector<pair<double,int> > ans(vp.size(),pair<double,int>(INF,-2));\n\t\tpq.push(pair<double,pii>(0.0,pii(ma[ptopdd(s)],-1)));\n\t\twhile(!pq.empty()){\n\t\t\tpair<double,pii> top=pq.top();\n\t\t\tpq.pop();\n\t\t\tif(ans[top.se.fi].se!=-2) continue;\n\t\t\tans[top.se.fi]=pair<double,int>(top.fi,top.se.se);\n\t\t\trep(i,vv[top.se.fi].size()){\n\t\t\t\tif(ans[vv[top.se.fi][i].fi].se!=-2) continue;\n\t\t\t\tpq.push(pair<double,pii>(top.fi-vv[top.se.fi][i].se,pii(vv[top.se.fi][i].fi,top.se.fi)));\n\t\t\t}\n\t\t}\n\t\tvector<int> res;\n\t\tint pos=ma[ptopdd(g)];\n\t\tif(ans[pos].se==-2){\n\t\t\tcout<<-1<<endl;\n\t\t\tcontinue;\n\t\t}\n\t\twhile(pos!=-1){\n\t\t\tres.PB(pos);\n\t\t\tpos=ans[pos].se;\n\t\t}\n\t\treverse(ALL(res));\n\t\trep(i,res.size()){\n\t\t\tcout<<vp[res[i]].fi<<\" \"<<vp[res[i]].se<<endl;\n\t\t}\n\t\tcout<<0<<endl;\n\t}\n}\n\n\n\n}\nmain() try{\n mainmain();\n}\ncatch(...){\n}", "accuracy": 1, "time_ms": 60, "memory_kb": 1348, "score_of_the_acc": -0.556, "final_rank": 15 }, { "submission_id": "aoj_1279_1252738", "code_snippet": "#include <cstdlib>\n#include <cmath>\n#include <climits>\n#include <cfloat>\n#include <map>\n//#include <utility>\n#include <set>\n#include <iostream>\n//#include <memory>\n#include <string>\n#include <vector>\n#include <algorithm>\n//#include <functional>\n#include <sstream>\n//#include <deque>\n#include <complex>\n#include <stack>\n#include <queue>\n#include <cstdio>\n//#include <cctype>\n#include <cstring>\n//#include <ctime>\n#include <iterator>\n#include <bitset>\n//#include <numeric>\n//#include <list>\n//#include <iomanip>\n\n#if __cplusplus >= 201103L\n#include <array>\n#include <tuple>\n#include <initializer_list>\n#include <unordered_set>\n#include <unordered_map>\n#include <forward_list>\n\n#define cauto const auto&\n#else\n\n#endif\n\nusing namespace std;\n\n\nnamespace{\ntypedef long long ll;\ntypedef pair<int,int> pii;\ntypedef pair<ll,ll> pll;\n\ntypedef vector<int> vint;\ntypedef vector<vector<int> > vvint;\ntypedef vector<long long> vll, vLL;\ntypedef vector<vector<long long> > vvll, vvLL;\n\n#define VV(T) vector<vector< T > >\n\ntemplate <class T>\nvoid initvv(vector<vector<T> > &v, int a, int b, const T &t = T()){\n v.assign(a, vector<T>(b, t));\n}\n\ntemplate <class F, class T>\nvoid convert(const F &f, T &t){\n stringstream ss;\n ss << f;\n ss >> t;\n}\n\n\n#define reep(i,a,b) for(int i=(a);i<(b);++i)\n#define rep(i,n) reep((i),0,(n))\n#define ALL(v) (v).begin(),(v).end()\n#define PB push_back\n#define fi first\n#define se second\n#define mkp make_pair\n#define RALL(v) (v).rbegin(),(v).rend()\n\n#define MOD 1000000007LL\n// #define EPS 1e-8\n// static const int INF=1<<24;\ntypedef pair<double,double> pdd;\nconst double EPS = 1e-8;\nconst double INF = 1e12;\nconst double PI = asin(0.5)*6;\ntypedef complex<double> P;\n\n#define X real()\n#define Y imag()\n\n#define Curr(P,i) P[(i)%P.size()]\n#define Next(P,i) P[(i+1)%P.size()]\n#define Prev(P,i) P[(i+P.size()-1)%P.size()]\n\nnamespace std{\n bool operator<(const P a,const P b){\n return a.X != b.X ? a.X < b.X : a.Y < b.Y;\n }\n}\n//gaiseki\ndouble cross(const P a,const P b){\n return (conj(a)*b).imag();\n}\n//naiseki\ndouble dot(const P a,const P b){\n return (conj(a)*b).real();\n}\n// TODO make graph (20)\nint ccw(P a,P b,P c){\n b-=a;\n c-=a;\n if(cross(b,c)>0) return +1;//counter clockwise\n if(cross(b,c)<0) return -1;//clockwise\n if(dot(b,c)<0) return +2;// c--a--b\n if(norm(b)<norm(c)) return -2;// a--b--c\n return 0;// a--c--b(or b==c)\n}\n\nstruct L : public vector<P>{\n L(const P a,const P b){\n push_back(a),push_back(b);\n }\n};\ntypedef L S;\nbool isCrossSP(S a,P p){\n return abs(a[0]-p)+abs(a[1]-p)-abs(a[0]-a[1]) < EPS;\n}\npdd ptopdd(P a){\n\treturn pdd(a.real(),a.imag());\n}\nP pddtop(pdd a){\n\treturn P(a.first,a.second);\n}\nint foo3(S a,P b,P c){\n\tdouble ab0=abs(a[0]-b);\n\tdouble ac0=abs(a[0]-c);\n\tdouble bc =abs(b-c);\n\tdouble t=ac0*ac0-ab0*ab0-bc*bc;\n\tif(abs(ac0*ac0-ab0*ab0-bc*bc)<EPS){\n\t\treturn 2;\n\t}\n\tif(ac0*ac0-ab0*ab0-bc*bc>EPS){\n\t\treturn 0;\n\t}\n\treturn 1;\n}\nbool operator<(P a,P b){\n\tif(a.X==b.X){\n\t\treturn a.Y<b.Y;\n\t}\n\treturn a.X<b.X;\n}\nvoid mainmain(){\n\tint n;\n\twhile(cin>>n,n){\n\t\tP s,g;\n\t\tdouble a[4];\n\t\trep(i,4) cin>>a[i];\n\t\ts=P(a[0],a[1]);\n\t\tg=P(a[2],a[3]);\n\t\tvector<S> v1;\n\t\tS tmp(s,g);\n\t\trep(i,n){\n\t\t\tdouble a[4];\n\t\t\trep(j,4) cin>>a[j];\n\t\t\tP t1=P(a[0],a[1]);\n\t\t\tP t2=P(a[2],a[3]);\n\t\t\tS tmp=S(t1,t2);\n\t\t\tv1.PB(tmp);\n\t\t}\n\t\tvector<S> sign,road;\n\t\trep(i,n){\n\t\t\tbool f1=true;\n\t\t\tbool f2=true;\n\t\t\trep(j,n){\n\t\t\t\tif(i==j) continue;\n\t\t\t\tif(isCrossSP(v1[j],v1[i][0])){\n\t\t\t\t\tf1=false;\n\t\t\t\t}\n\t\t\t\tif(isCrossSP(v1[j],v1[i][1])){\n\t\t\t\t\tf2=false;\n\t\t\t\t}\n\t\t\t}\n\t\t\tif(f1||f2){\n\t\t\t\tsign.PB(v1[i]);\n\t\t\t}\n\t\t\telse{\n\t\t\t\troad.PB(v1[i]);\n\t\t\t}\n\t\t}\n\t\tvector<S> troad;\n\t\trep(i,road.size()){\n\t\t\tvector<S> tmp;\n\t\t\ttmp.PB(road[i]);\n\t\t\trep(j,road.size()){\n\t\t\t\tif(i==j) continue;\n\t\t\t\trep(k,tmp.size()){\n\t\t\t\t\tbool f=false;\n\t\t\t\t\tP p;\n\t\t\t\t\trep(o,2){\n\t\t\t\t\t\tif(isCrossSP(tmp[k],road[j][o])){\n\t\t\t\t\t\t\tf=true;\n\t\t\t\t\t\t\tp=road[j][o];\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t\tif(f&&abs(tmp[k][0]-p)>EPS&&abs(tmp[k][1]-p)>EPS){\n\t\t\t\t\t\tS tt=tmp[k];\n\t\t\t\t\t\ttmp[k]=S(tt[0],p);\n\t\t\t\t\t\ttmp.PB(S(tt[1],p));\n\t\t\t\t\t\tk--;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t\trep(j,tmp.size()){\n\t\t\t\ttroad.PB(tmp[j]);\n\t\t\t}\n\t\t}\n\t\troad=troad;\n\t\tmap<pdd,int> ma;\n\t\tvector<pii> vp;\n\t\trep(i,road.size()){\n\t\t\trep(j,2){\n\t\t\t\tpdd t=ptopdd(road[i][j]);\n\t\t\t\tif(ma.find(t)==ma.end()){\n\t\t\t\t\tma[t]=0;\n\t\t\t\t\tvp.PB(pii(int(t.fi),int(t.se)));\n\t\t\t\t\tma[t]=ma.size()-1;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\ttypedef pair<int,double> pid;\n\t\tVV(pid) vv(ma.size());\n\t\trep(i,road.size()){\n\t\t\tbool f[2]={true,true};\n\t\t\trep(j,sign.size()){\n\t\t\t\trep(k,2){\n\t\t\t\t\tif(isCrossSP(road[i],sign[j][k])){\n\t\t\t\t\t\tint aa=foo3(road[i],sign[j][k],sign[j][1-k]);\n\t\t\t\t\t\tif(aa==2){\n\t\t\t\t\t\t\tf[0]=f[1]=false;\n\t\t\t\t\t\t}\n\t\t\t\t\t\tif(aa==0){\n\t\t\t\t\t\t\tf[0]=false;\n\t\t\t\t\t\t}\n\t\t\t\t\t\tif(aa==1){\n\t\t\t\t\t\t\tf[1]=false;\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t\trep(j,2){\n\t\t\t\tif(f[j]){\n\t\t\t\t\tvv[ma[ptopdd(road[i][j])]].PB(pid(ma[ptopdd(road[i][1-j])],abs(road[i][0]-road[i][1])));\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\tpriority_queue<pair<double,pii> > pq;\n\t\tvector<pair<double,int> > ans(vp.size(),pair<double,int>(INF,-2));\n\t\tpq.push(pair<double,pii>(0.0,pii(ma[ptopdd(s)],-1)));\n\t\twhile(!pq.empty()){\n\t\t\tpair<double,pii> top=pq.top();\n\t\t\tpq.pop();\n\t\t\tif(ans[top.se.fi].se!=-2) continue;\n\t\t\t// cout<<top.se.fi<<endl;\n\t\t\t// cout<<\"size \"<<vv[top.se.fi].size()<<endl;\n\t\t\t// cout<<top.fi<<\" \"<<top.se.fi<<\" \"<<top.se.se<<endl;\n\t\t\tans[top.se.fi]=pair<double,int>(top.fi,top.se.se);\n\t\t\trep(i,vv[top.se.fi].size()){\n\t\t\t\tif(ans[vv[top.se.fi][i].fi].se!=-2) continue;\n\t\t\t\tpq.push(pair<double,pii>(top.fi-vv[top.se.fi][i].se,pii(vv[top.se.fi][i].fi,top.se.fi)));\n\t\t\t}\n\t\t\t// cout<<\"hoge\"<<endl;\n\t\t}\n\t\t// puts(\"hoge\");\n\t\tvector<int> res;\n\t\tint pos=ma[ptopdd(g)];\n\t\t// rep(i,ans.size()){\n\t\t\t// cout<<i<<\" \"<<ans[i].fi<<\" \"<<ans[i].se<<endl;\n\t\t// }\n\t\t// continue;\n\t\tif(ans[pos].se==-2){\n\t\t\tcout<<-1<<endl;\n\t\t\tcontinue;\n\t\t}\n\t\twhile(pos!=-1){\n\t\t\t// cout<<pos<<endl;\n\t\t\tres.PB(pos);\n\t\t\tpos=ans[pos].se;\n\t\t}\n\t\t// cout<<\"vp \"<<endl;\n\t\t// rep(i,vp.size()){\n\t\t\t// cout<<i<<\" \"<<vp[i].fi<<\" \"<<vp[i].se<<\" \"<<ans[i].fi<<endl;\n\t\t// }\n\t\treverse(ALL(res));\n\t\trep(i,res.size()){\n\t\t\tcout<<vp[res[i]].fi<<\" \"<<vp[res[i]].se<<endl;\n\t\t}\n\t\tcout<<0<<endl;\n\t}\n}\n\n\n\n}\nmain() try{\n mainmain();\n}\ncatch(...){\n}\n/*\n8\n1 1\n4 4\n1 1 4 1\n1 1 1 4\n3 1 3 4\n4 3 5 3\n2 4 3 5\n4 1 4 4\n3 3 2 2\n1 4 4 4\n9\n1 5\n5 1\n5 4 5 1\n1 5 1 1\n1 5 5 1\n2 3 2 4\n5 4 1 5\n3 2 2 1\n4 2 4 1\n1 1 5 1\n5 3 4 3\n11\n5 5\n1 0\n3 1 5 1\n4 3 4 2\n3 1 5 5\n2 3 2 2\n1 0 1 2\n1 2 3 4\n3 4 5 5\n1 0 5 2\n4 0 4 1\n5 5 5 1\n2 3 2 4\n0\n*/\n/*\n1 1\n3 1\n3 4\n4 4\n0\n-1\n5 5\n5 2\n3 1\n1 0\n0\n*/", "accuracy": 1, "time_ms": 60, "memory_kb": 1348, "score_of_the_acc": -0.556, "final_rank": 15 }, { "submission_id": "aoj_1279_1244268", "code_snippet": "#include <iostream>\n#include <utility>\n#include <vector>\n#include <algorithm>\n#include <cmath>\n#include <map>\nusing namespace std;\n\n#define x first\n#define y second\n#define fi first\n#define se second\n#define mkp make_pair\n\ntypedef pair<int,int> pii;\n\nconst int INF = 1e9;\n\nint n;\npii s, g;\nvector<pair<pii,pii> > lineseg, street, sign;\n\ntemplate<class T>\nT sqr(T x) {\n\treturn x * x;\n}\n\ndouble sqrdist(pii a, pii b) {\n\treturn sqr(a.x-b.x) + sqr(a.y-b.y);\n}\n\ndouble dist(pii a, pii b) {\n\treturn sqrt(sqr(a.x-b.x) + sqr(a.y-b.y));\n}\n\nint main() {\n\twhile(cin >> n, n) {\n\t\tcin >> s.x >> s.y;\n\t\tcin >> g.x >> g.y;\n\t\tlineseg.clear();\n\t\tstreet.clear();\n\t\tsign.clear();\n\t\tfor(int i = 0; i < n; i++) {\n\t\t\tpii a, b;\n\t\t\tcin >> a.x >> a.y >> b.x >> b.y;\n\t\t\tlineseg.push_back(mkp(a,b));\n\t\t}\n\n\t\t// street or sign.\n\t\tfor(int i = 0; i < lineseg.size(); i++) {\n\t\t\tbool a, b;\n\t\t\tpair<pii,pii> c;\n\t\t\ta = b = true;\n\t\t\tc = lineseg[i];\n\t\t\tfor(int j = 0; j < lineseg.size(); j++) {\n\t\t\t\tif(j != i) {\n\t\t\t\t\tpair<pii,pii> d;\n\t\t\t\t\td = lineseg[j];\n\t\t\t\t\tif(fabs(dist(d.fi, c.fi) + dist(d.se, c.fi) - dist(d.fi, d.se)) < 1e-9)\n\t\t\t\t\t\ta = false;\n\t\t\t\t\tif(fabs(dist(d.fi, c.se) + dist(d.se, c.se) - dist(d.fi, d.se)) < 1e-9)\n\t\t\t\t\t\tb = false;\n\t\t\t\t}\n\t\t\t}\n\t\t\tif(a || b) {\n\t\t\t\tif(a)\n\t\t\t\t\tswap(c.fi, c.se);\n\t\t\t\tsign.push_back(c);\n\t\t\t}\n\t\t\telse\n\t\t\t\tstreet.push_back(c);\n\t\t}\n\n\t\t// make graph.\n\t\t// vertex\n\t\tmap<pii, int> vertex;\n\t\tfor(int i = 0; i < street.size(); i++) {\n\t\t\tif(vertex.find(street[i].fi) == vertex.end()) {\n\t\t\t\tvertex.insert(mkp(street[i].fi, vertex.size()));\n\t\t\t}\n\t\t\tif(vertex.find(street[i].se) == vertex.end()) {\n\t\t\t\tvertex.insert(mkp(street[i].se, vertex.size()));\n\t\t\t}\n\t\t}\n\n\t\t// edge\n\t\tdouble edge[512][512];\n\t\tdouble wf[512][512];\n\t\tfor(int i = 0; i < 512; i++) {\n\t\t\tfor(int j = 0; j < 512; j++) {\n\t\t\t\tedge[i][j] = INF;\n\t\t\t\twf[i][j] = INF;\n\t\t\t}\n\t\t\tedge[i][i] = 0;\n\t\t\twf[i][i] = 0;\n\t\t}\n\t\t\n\t\tvector<pair<pii,pii> > cut(street.begin(), street.end());\n\t\tfor(int i = 0; i < cut.size(); i++) {\n\t\t\tpair<pii,pii> a, b;\n\t\t\ta = cut[i];\n\t\t\tfor(int j = 0; j < cut.size(); j++) {\n\t\t\t\tb = cut[j];\n\t\t\t\tif((fabs(dist(a.fi, b.fi) + dist(a.se, b.fi) - dist(a.fi, a.se)) < 1e-9 && a.fi != b.fi && a.se != b.fi)){\n\t\t\t\t\tcut.erase(cut.begin()+i);\n\t\t\t\t\tcut.push_back(mkp(a.fi,b.fi));\n\t\t\t\t\tcut.push_back(mkp(b.fi,a.se));\n\t\t\t\t\ti--;\n\t\t\t\t\tbreak;\n\t\t\t\t}\n\t\t\t\telse if(fabs(dist(a.fi, b.se) + dist(a.se, b.se) - dist(a.fi, a.se)) < 1e-9 && a.fi != b.se && a.se != b.se) {\n\t\t\t\t\tcut.erase(cut.begin()+i);\n\t\t\t\t\tcut.push_back(mkp(a.fi,b.se));\n\t\t\t\t\tcut.push_back(mkp(b.se,a.se));\n\t\t\t\t\ti--;\n\t\t\t\t\tbreak;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\n\t\tfor(int i = 0; i < cut.size(); i++) {\n\t\t\tpair<pii,pii> a, b;\n\t\t\ta = cut[i];\n\t\t\tbool f2s, s2f;\n\t\t\tf2s = s2f = true;\n\t\t\tfor(int j = 0; j < sign.size(); j++) {\n\t\t\t\tb = sign[j];\n\t\t\t\tif(fabs(dist(a.fi, b.fi) + dist(a.se, b.fi) - dist(a.fi, a.se)) < 1e-9) {\n\t\t\t\t\tif(fabs(sqrdist(a.fi, b.fi) + sqrdist(b.fi, b.se) - sqrdist(a.fi, b.se)) < 1e-9) {\n\t\t\t\t\t\tf2s = s2f = false;\n\t\t\t\t\t}\n\t\t\t\t\telse if(sqrdist(a.fi, b.fi) + sqrdist(b.fi, b.se) - sqrdist(a.fi, b.se) > 0) {\n\t\t\t\t\t\ts2f = false;\n\t\t\t\t\t}\n\t\t\t\t\telse {\n\t\t\t\t\t\tf2s = false;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t\tif(f2s) {\n\t\t\t\tedge[vertex[a.fi]][vertex[a.se]] = wf[vertex[a.fi]][vertex[a.se]] = dist(a.fi, a.se);\n\t\t\t}\n\t\t\tif(s2f) {\n\t\t\t\tedge[vertex[a.se]][vertex[a.fi]] = wf[vertex[a.se]][vertex[a.fi]] = dist(a.fi, a.se);\n\t\t\t}\n\t\t}\n\n\t\t// warshall floyd\n\t\tfor(int i = 0; i < vertex.size(); i++) {\n\t\t\tfor(int j = 0; j < vertex.size(); j++) {\n\t\t\t\tfor(int k = 0; k < vertex.size(); k++) {\n\t\t\t\t\twf[j][k] = min(wf[j][k], wf[j][i] + wf[i][k]);\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\n\t\tdouble min_cost;\n\t\tmin_cost = wf[vertex[s]][vertex[g]];\n\n\t\tif(fabs(min_cost - INF) < 1e-9)\n\t\t\tcout << -1 << endl;\n\t\telse {\n\t\t\tvector<int> path;\n\t\t\tint indx, indx_s;\n\t\t\tindx = vertex[g];\n\t\t\tindx_s = vertex[s];\n\t\t\twhile(indx != indx_s) {\n\t\t\t\tint nex = -1;\n\t\t\t\tpath.push_back(indx);\n\t\t\t\tfor(int i = 0; i < vertex.size(); i++) {\n\t\t\t\t\tif(i != indx && fabs(wf[indx_s][i] + edge[i][indx] - wf[indx_s][indx]) < 1e-9)\n\t\t\t\t\t\tnex = i;\n\t\t\t\t}\n\t\t\t\tindx = nex;\n\t\t\t}\n\t\t\tpath.push_back(indx_s);\n\t\t\treverse(path.begin(), path.end());\n\n\t\t\tvector<pii> indx2pt;\n\t\t\tindx2pt.resize(vertex.size());\n\t\t\tfor(map<pii,int>::iterator iter = vertex.begin(); iter != vertex.end(); iter++) {\n\t\t\t\tindx2pt[iter->se] = iter->fi;\n\t\t\t}\n\n\t\t\tfor(int i = 0; i < path.size(); i++) {\n\t\t\t\tpii tmp = indx2pt[path[i]];\n\t\t\t\tcout << tmp.x << \" \" << tmp.se << endl;\n\t\t\t}\n\t\t\tcout << 0 << endl;\n\t\t}\n\t}\n}", "accuracy": 1, "time_ms": 30, "memory_kb": 5344, "score_of_the_acc": -0.2869, "final_rank": 13 }, { "submission_id": "aoj_1279_1244068", "code_snippet": "#include<cstdio>\n#include<vector>\n#include<string>\n#include<iostream>\n#include<algorithm>\n#include<map>\n#include<set>\n#include<queue>\n#include<complex>\n#include<cmath>\n\nusing namespace std;\n\n#define reps(i,f,n) for(int i=f;i<int(n);i++)\n#define rep(i,n) reps(i,0,n)\n\n#define X real()\n#define Y imag()\n\n\nconst int INF = 1000000007;\nconst double EPS = 0.00001;\nconst int N = 222;\nconst int M = 444;\n\ntypedef pair<int,int> pii;\n\n\ntypedef complex<double> P;\nclass L:public vector<P>{\n\tpublic:\n\tL(P a, P b){\n\t\tpush_back(a);\n\t\tpush_back(b);\n\t}\n\tL(){\n\t\tpush_back(P());\n\t\tpush_back(P());\n\t}\n};\n\n\ndouble dot(P a, P b){return a.X*b.X + a.Y*b.Y;}\ndouble cross(P a, P b){return a.X*b.Y - a.Y*b.X;}\n\ndouble dist_LP(L a, P b){\n\tP e = a[1]-a[0];\n\te = e/abs(e);\n\t\n\tdouble alen = abs(a[1]-a[0]);\n\t\n\tdouble d = dot(b-a[0], e);\n\tif(d<0 || d>=alen){\n\t\treturn min(abs(b-a[0]), abs(b-a[1]));\n\t}\n\t\n\tdouble f = abs(b-a[0]);\n\treturn sqrt(max(0.0, f*f - d*d));\n}\n\nint n;\nP st,en;\nvector<L> line;\n\n\nbool input(){\n\t\n\tcin>>n;\n\tif(n==0)return false;\n\t\n\tdouble a,b,c,d;\n\tcin>>a>>b>>c>>d;\n\tst = P(a,b);\n\ten = P(c,d);\n\t\n\tline.clear();\n\trep(i,n){\n\t\tcin>>a>>b>>c>>d;\n\t\tline.push_back(L(P(a,b), P(c,d)));\n\t}\n\treturn true;\n}\n\n\nvector<L> road;\nvector<L> sign;\n\nvoid inputsplit(){\n\troad.clear();\n\tsign.clear();\n\trep(i,line.size()){\n\t\tbool hita = false;\n\t\tbool hitb = false;\n\t\trep(j,line.size()){\n\t\t\tif(i==j)continue;\n\t\t\tif(dist_LP(line[j], line[i][0]) < EPS)hita = true;\n\t\t\tif(dist_LP(line[j], line[i][1]) < EPS)hitb = true;\n\t\t}\n\t\tif(hita && hitb)road.push_back(line[i]);\n\t\telse sign.push_back(line[i]);\n\t}\n}\n\nvoid inputsplit2(){\n\tvector<vector<L> > copy;\n\trep(i,road.size()){\n\t\tvector<L> ex(1);\n\t\tex[0] = road[i];\n\t\t\n\t\trep(j,road.size()){\n\t\t\tif(i==j)continue;\n\t\t\trep(k,ex.size()){\n\t\t\t\tbool ok = false;\n\t\t\t\tP point(0,0);\n\t\t\t\t\n\t\t\t\trep(p,2){\n\t\t\t\t\tdouble d = dist_LP(ex[k], road[j][p]);\n\t\t\t\t\tif(d<EPS){\n\t\t\t\t\t\tok=true;\n\t\t\t\t\t\tpoint = road[j][p];\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\tif(ok && min(abs(ex[k][0]-point), abs(ex[k][1]-point))>EPS){\n\t\t\t\t\tL bef = ex[k];\n\t\t\t\t\tex.erase(ex.begin()+k);\n\t\t\t\t\tk--;\n\t\t\t\t\tex.push_back(L(bef[0], point));\n\t\t\t\t\tex.push_back(L(bef[1], point));\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\tcopy.push_back(ex);\n\t}\n\tvector<L> res;\n\trep(i,copy.size()){\n\t\trep(j,copy[i].size())res.push_back(copy[i][j]);\n\t}\n\troad = res;\n}\n\nmap<pii,int> table;\nint tonum(P pos){\n\tpii p = pii( int(pos.X+0.5), int(pos.Y+0.5) );\n\tif(table.find(p)==table.end()){\n\t\ttable[p] = 0;\n\t\ttable[p] = table.size()-1;\n\t}\n\treturn table[p];\n}\nP topos(int num){\n\tfor(map<pii,int>::iterator it = table.begin(); it!=table.end(); it++){\n\t\tif(it->second==num)return P(it->first.first, it->first.second);\n\t}\n\treturn P(INF,INF);\n}\n\nvoid setroadnum(){\n\trep(i,road.size()){\n\t\ttonum(road[i][0]);\n\t\ttonum(road[i][1]);\n\t}\n}\n\ndouble edge[M][M];\nvoid setEdge(){\n\trep(i,M)rep(j,M)edge[i][j]=INF;\n\trep(i,M)edge[i][i]=0;\n\t\n\trep(i,road.size()){\n\t\tint a = tonum(road[i][0]);\n\t\tint b = tonum(road[i][1]);\n\t\tdouble len = abs(road[i][1]-road[i][0]);\n\t\tedge[a][b] = edge[b][a] = len;\n\t\t\n\t\trep(j,sign.size()){\n\t\t\tbool ok = false;\n\t\t\tbool rev = false;\n\t\t\trep(k,2){\n\t\t\t\tif(dist_LP(road[i], sign[j][k])<EPS){\n\t\t\t\t\tok = true;\n\t\t\t\t\tif(k==1)rev=true;\n\t\t\t\t}\n\t\t\t}\n\t\t\t\n\t\t\tif(ok){\n\t\t\t\tL s = sign[j];\n\t\t\t\tif(rev){\n\t\t\t\t\tswap(s[0], s[1]);\n\t\t\t\t}\n\t\t\t\tdouble dval = dot(road[i][1]-road[i][0], s[1]-s[0]);\n\t\t\t\tif(dval<EPS)edge[b][a] = INF;\n\t\t\t\tif(dval>-EPS) edge[a][b] = INF;\n\t\t\t}\n\t\t}\n\t}\n}\n\ndouble dp[M][M];\nvector<P> wf(){\n\trep(i,M)rep(j,M)dp[i][j] = edge[i][j];\n\t\n\tint m = table.size();\n\trep(k,m)rep(i,m)rep(j,m)dp[i][j] = min(dp[i][j], dp[i][k] + dp[k][j]);\n\t\n\tint stnum = tonum(st);\n\tint ennum = tonum(en);\n\t\n\tvector<P> ret;\n\tif(dp[stnum][ennum] > INF-EPS)return ret;\n\t\n\tret.push_back(topos(ennum));\n\twhile(1){\n\t\tif(ennum == stnum)break;\n\t\trep(i,m){\n\t\t\tif(i==ennum)continue;\n\t\t\tdouble d = edge[i][ennum]-(dp[stnum][ennum]-dp[stnum][i]);\n\t\t\tif(abs(d)<EPS){\n\t\t\t\tret.push_back(topos(i));\n\t\t\t\tennum = i;\n\t\t\t\tbreak;\n\t\t\t}\n\t\t}\n\t}\n\t\n\treverse(ret.begin(),ret.end());\n\treturn ret;\n}\n\nvector<P> solve(){\n\ttable.clear();\n\tinputsplit();\n\tinputsplit2();\n\t\n\tsetroadnum();\n\t\n\tsetEdge();\n\tvector<P> ret = wf();\n\treturn ret;\n}\n\nvoid call(){\n\tvector<P> res = solve();\n\tif(res.size()==0)puts(\"-1\");\n\telse{\n\t\trep(i,res.size()){\n\t\t\tprintf(\"%d %d\\n\",int(res[i].X+0.5), int(res[i].Y+0.5));\n\t\t}\n\t\tputs(\"0\");\n\t}\n}\n\n\n\nint main(){\n\twhile(input())call();\n}", "accuracy": 1, "time_ms": 100, "memory_kb": 4400, "score_of_the_acc": -1.0495, "final_rank": 19 }, { "submission_id": "aoj_1279_1224588", "code_snippet": "#include<cstdio>\n#include<cstdlib>\n#include<cstring>\n#include<cmath>\n#include<iostream>\n#include<sstream>\n#include<string>\n#include<vector>\n#include<map>\n#include<list>\n#include<queue>\n#include<deque>\n#include<algorithm>\n#include<numeric>\n#include<utility>\n#include<complex>\n#include<functional>\n \nusing namespace std;\n\n/* constant */\n\n#define MAX_N\t(200)\n#define MAX_P\t(MAX_N * 2)\n\n#define T_SEG\t(0)\n#define T_SIGN\t(1)\n\n#define D_NONE\t(0)\n#define D_FWD\t(1)\n#define D_BWD\t(2)\n#define D_BOTH\t(3)\n\n#define INF\t(1e10)\n\n/* typedef */\n\ntypedef vector<int> vi;\ntypedef vector<long long> vl;\ntypedef vector<double> vd;\ntypedef pair<int,int> pii;\ntypedef pair<long,long> pll;\ntypedef long long ll;\ntypedef deque<int> di;\n\nstruct Pt {\n int x, y;\n\n Pt() {}\n Pt(int _x, int _y) { x = _x; y = _y; }\n\n bool operator==(Pt pt) { return x == pt.x && y == pt.y; }\n Pt operator+(Pt pt) { return Pt(x + pt.x, y + pt.y); }\n Pt operator-(Pt pt) { return Pt(x - pt.x, y - pt.y); }\n Pt operator-() { return Pt(-x, -y); }\n //Pt operator*(double t) { return Pt(x * t, y * t); }\n //Pt operator/(double t) { return Pt(x / t, y / t); }\n int dot(Pt v) { return x * v.x + y * v.y; }\n int cross(Pt v) { return x * v.y - y * v.x; }\n int dist2() { return x * x + y * y; }\n double dist() { return sqrt(dist2()); }\n\n string to_s() {\n stringstream ss;\n ss << \"(\" << x << \",\" << y << \")\";\n return ss.str();\n }\n};\n\nstruct Seg {\n Pt p0, p1;\n int i0, i1, type;\n\n string to_s() {\n stringstream ss;\n ss << p0.to_s() << \"-\" << p1.to_s();\n ss << \"[\" << i0 << \",\" << i1 << \",\" << type << \"]\";\n return ss.str();\n }\n};\n\n/* global variables */\n\nPt pts[MAX_P];\nSeg segs[MAX_N];\n\nvi psegs[MAX_P];\nint ptypes[MAX_P];\n\ndouble edges[MAX_P][MAX_P];\nvi nbrs[MAX_P];\n\ndouble dists[MAX_P];\nint prevs[MAX_P];\n\n/* classes */\n\nclass CompDist {\npublic:\n bool operator()(const int a, const int b) const {\n return (dists[b] < dists[a]);\n }\n};\n\n/* subroutines */\n\nbool on_seg(Pt pt, Seg seg) {\n Pt v0 = seg.p0 - pt;\n Pt v1 = seg.p1 - pt;\n return ((seg.p1 - seg.p0).cross(v0) == 0 && v0.dot(v1) <= 0);\n}\n\nint pt_index(int npts, Pt *pts, Pt& pt) {\n Pt *pit = find(pts, pts + npts, pt);\n return ((pit == pts + npts) ? -1 : pit - pts);\n}\n\n/* main */\n\nint main() {\n for (;;) {\n int n;\n cin >> n;\n if (n == 0) break;\n\n Pt st, gl;\n cin >> st.x >> st.y;\n cin >> gl.x >> gl.y;\n //cout << st.to_s() << gl.to_s() << endl;\n\n int npts = 0;\n \n for (int i = 0; i < n; i++) {\n Seg& seg = segs[i];\n cin >> seg.p0.x >> seg.p0.y >> seg.p1.x >> seg.p1.y;\n seg.type = T_SEG;\n //cout << seg.to_s() << endl;\n\n int pi0 = pt_index(npts, pts, seg.p0);\n if (pi0 < 0) {\n\tseg.i0 = npts;\n\tpts[npts++] = seg.p0;\n }\n else\n\tseg.i0 = pi0;\n \n int pi1 = pt_index(npts, pts, seg.p1);\n if (pi1 < 0) {\n\tseg.i1 = npts;\n\tpts[npts++] = seg.p1;\n }\n else\n\tseg.i1 = pi1;\n }\n //for (int i = 0; i < npts; i++) cout << pts[i].to_s(); cout << endl;\n //for (int i = 0; i < n; i++) cout << segs[i].to_s(); cout << endl;\n\n for (int i = 0; i < npts; i++) {\n psegs[i].clear();\n for (int j = 0; j < n; j++)\n\tif (on_seg(pts[i], segs[j])) {\n\t //cout << pti.to_s() << \" on \" << segs[j].to_s() << endl;\n\t psegs[i].push_back(j);\n\t}\n }\n //for (int i = 0; i < npts; i++) cout << psegs[i].size() << ' '; cout << endl;\n\n memset(ptypes, T_SEG, sizeof(ptypes));\n \n for (int i = 0; i < n; i++) {\n if (psegs[segs[i].i0].size() == 1 || psegs[segs[i].i1].size() == 1) {\n\tsegs[i].type = T_SIGN;\n\tint i0 = segs[i].i0;\n\tint i1 = segs[i].i1;\n\tptypes[i0] = ptypes[i1] = T_SIGN;\n }\n }\n //for (int i = 0; i < n; i++) cout << segs[i].to_s() << endl;\n\n for (int i = 0; i < npts; i++) {\n for (int j = 0; j < npts; j++) edges[i][j] = 0.0;\n nbrs[i].clear();\n }\n \n for (int i = 0; i < n; i++) {\n Seg& segi = segs[i];\n if (segi.type == T_SIGN) continue;\n\n Pt& p0 = segi.p0;\n Pt& p1 = segi.p1;\n Pt v01 = p1 - p0;\n\n vector<pii> spts;\n\n for (int j = 0; j < npts; j++) {\n\tvi& psj = psegs[j];\n\tif (find(psj.begin(), psj.end(), i) != psj.end()) {\n\t pii paj((pts[j] - p0).dot(v01), j);\n\t spts.push_back(paj);\n\t}\n }\n sort(spts.begin(), spts.end());\n\n //for (int j = 0; j < spts.size(); j++)\n //cout << \"(\" << spts[j].first << \",\" << spts[j].second << \")\";\n //cout << endl;\n\n int prvj = spts[0].second;\n int direc = D_BOTH;\n\n for (int j = 1; j < spts.size(); j++) {\n\tint pj = spts[j].second;\n\n\tif (ptypes[pj] == T_SIGN) {\n\t for (int k = 0; k < n; k++) {\n\t Seg& segk = segs[k];\n\t if (k != i && segk.type == T_SIGN &&\n\t\t(pj == segk.i0 || pj == segk.i1)) {\n\t Pt vsign =\n\t\t(pj == segk.i0) ? segk.p1 - segk.p0 : segk.p0 - segk.p1;\n\t int dot = vsign.dot(v01);\n\t if (dot >= 0) direc &= ~D_FWD;\n\t if (dot <= 0) direc &= ~D_BWD;\n\t }\n\t }\n\t}\n\telse {\n\t double d = (pts[pj] - pts[prvj]).dist();\n\n\t if (direc & D_FWD) {\n\t edges[prvj][pj] = d;\n\t nbrs[prvj].push_back(pj);\n\t }\n\t if (direc & D_BWD) {\n\t edges[pj][prvj] = d;\n\t nbrs[pj].push_back(prvj);\n\t }\n\n\t prvj = pj;\n\t direc = D_BOTH;\n\t}\n }\n }\n\n for (int i = 0; i < npts; i++) {\n dists[i] = INF;\n prevs[i] = -1;\n }\n\n int sti = find(pts, pts + npts, st) - pts;\n int gli = find(pts, pts + npts, gl) - pts;\n \n dists[sti] = 0.0;\n vector<int> q;\n q.push_back(sti);\n CompDist compdist;\n \n while (! q.empty()) {\n sort(q.begin(), q.end(), compdist);\n int u = q.back();\n q.pop_back();\n\n if (u == gli) break;\n\n for (vi::iterator vit = nbrs[u].begin(); vit != nbrs[u].end(); vit++) {\n\tint v = *vit;\n\tdouble vd = dists[u] + edges[u][v];\n\tif (dists[v] > vd) {\n\t if (dists[v] >= INF) q.push_back(v);\n\t dists[v] = vd;\n\t prevs[v] = u;\n\t}\n }\n }\n\n //for (int i = 0; i < npts; i++)\n //printf(\"dists[%d] = %lf\\n\", i, dists[i]);\n \n if (prevs[gli] < 0)\n cout << -1 << endl;\n else {\n di path;\n int u = gli;\n while (u >= 0) {\n\tpath.push_front(u);\n\tu = prevs[u];\n }\n\n for (di::iterator pit = path.begin(); pit != path.end(); pit++) {\n\tPt& pt = pts[*pit];\n\tcout << pt.x << ' ' << pt.y << endl;\n }\n cout << 0 << endl;\n }\n }\n\n return 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 1904, "score_of_the_acc": -0.0094, "final_rank": 4 }, { "submission_id": "aoj_1279_1223709", "code_snippet": "#include<stdio.h>\n#include<math.h>\n#include<algorithm>\n#include<queue>\n#include<map>\n#include<vector>\nusing namespace std;\nint G[510][510];\nint L[510][510];\nint x[510];\nint y[510];\nint cnt[510];\nvector<int>g[510];\ndouble ijk[510];\nint v[510];\nint rev[510];\nint ans[510];\nint tc[510];\nint p[510];\nint q[510];\nint r[510];\nint s[510];\nint ABS(int a){return max(a,-a);}\nint dcalc(int a,int b,int c,int d){\n\treturn (x[b]-x[a])*(x[d]-x[c])+(y[b]-y[a])*(y[d]-y[c]);\n}\ndouble dist(int a,int b){\n\treturn sqrt((x[a]-x[b])*(x[a]-x[b])+(y[a]-y[b])*(y[a]-y[b]));\n}\nint ccw(int a,int b,int c,int d,int e,int f){\n\tint X1=c-a;int X2=e-a;\n\tint Y1=d-b;int Y2=f-b;\n\tif(X1*Y2!=Y1*X2)return 0;\n\tif(X1*X2<0)return 0;\n\tif(Y1*Y2<0)return 0;\n\tif(ABS(X1)<ABS(X2))return 0;\n\tif(ABS(Y1)<ABS(Y2))return 0;\n\treturn 1;\n}\nint main(){\n\tint a;\n\twhile(scanf(\"%d\",&a),a){\n\t\tint sx,sy,gx,gy;\n\t\tscanf(\"%d%d%d%d\",&sx,&sy,&gx,&gy);\n\t\tint sz=0;\n\t\tmap<pair<int,int>,int> m;\n\t\tfor(int i=0;i<510;i++)g[i].clear();\n\t\tfor(int i=0;i<510;i++)tc[i]=0;\n\t\tfor(int i=0;i<510;i++)cnt[i]=0;\n\t\tfor(int i=0;i<510;i++)for(int j=0;j<510;j++){\n\t\t\tG[i][j]=0;\n\t\t\tL[i][j]=-1;\n\t\t}\n\t\tfor(int i=0;i<a;i++){\n\t\t\tscanf(\"%d%d%d%d\",p+i,q+i,r+i,s+i);\n\t\t\tint from=-1;\n\t\t\tint to=-1;\n\t\t\tif(m.count(make_pair(p[i],q[i]))){\n\t\t\t\tfrom=m[make_pair(p[i],q[i])];\n\t\t\t}else{\n\t\t\t\tm[make_pair(p[i],q[i])]=sz;\n\t\t\t\tx[sz]=p[i];y[sz]=q[i];\n\t\t\t\tfrom=sz++;\n\t\t\t}\n\t\t\tif(m.count(make_pair(r[i],s[i]))){\n\t\t\t\tto=m[make_pair(r[i],s[i])];\n\t\t\t}else{\n\t\t\t\tm[make_pair(r[i],s[i])]=sz;\n\t\t\t\tx[sz]=r[i];y[sz]=s[i];\n\t\t\t\tto=sz++;\n\t\t\t}\n\t\t}\n\t\tfor(int i=0;i<a;i++){\n\t\t\tvector<pair<double,int> > o;\n\t\t\tint ss=m[make_pair(p[i],q[i])];\n\t\t\tfor(int j=0;j<sz;j++){\n\t\t\t\tif(ccw(p[i],q[i],r[i],s[i],x[j],y[j])){\n\t\t\t\t\to.push_back(make_pair(dist(ss,j),j));\n\t\t\t\t}\n\t\t\t}\n\t\t\tstd::sort(o.begin(),o.end());\n\t\t\tfor(int j=0;j<o.size()-1;j++){\n\t\t\t\tint E=o[j].second;\n\t\t\t\tint F=o[j+1].second;\n\t\t\t\tG[E][F]=G[F][E]=1;\n\t\t\t\tcnt[E]++;\n\t\t\t\tcnt[F]++;\n\t\t\t}\n\t\t}\n\t/*\tfor(int i=0;i<sz;i++){\n\t\t\tif(x[i]==364&&y[i]==0){\n\t\t\t\tfor(int j=0;j<sz;j++)if(G[i][j])printf(\"%d %d\\n\",x[j],y[j]);\n\t\t\t}\n\t\t}*/\n\t\tfor(int i=0;i<sz;i++){\n\t\t\tif(cnt[i]==1){\n\t\t\t\tfor(int j=0;j<sz;j++)if(G[i][j]){\n\t\t\t\t\tvector<int>t;\n\t\t\t\t\tvector<int>u;\n\t\t\t\t\tint rem=0;\n\t\t\t\t\tfor(int k=0;k<sz;k++){\n\t\t\t\t\t\tif(G[j][k]&&cnt[k]!=1)t.push_back(k);\n\t\t\t\t\t\telse if(G[j][k])u.push_back(k);\n\t\t\t\t\t}\n\t\t\t\t//\tprintf(\"%d,%d: %d\\n\",x[i],y[i],t.size());\n\t\t\t\t//\tG[i][j]=G[j][i]=0;\n\t\t\t\t\tG[j][t[0]]=G[t[0]][j]=0;\n\t\t\t\t\tG[j][t[1]]=G[t[1]][j]=0;\n\t\t\t\t\tG[t[0]][t[1]]=G[t[1]][t[0]]=1;\n\t\t\t\t\tL[t[0]][t[1]]=L[t[0]][j];\n\t\t\t\t\tif(~L[t[0]][t[1]]&&~L[j][t[1]])L[t[0]][t[1]]&=L[j][t[1]];\n\t\t\t\t\telse if(!~L[t[0]][t[1]])L[t[0]][t[1]]=L[j][t[1]];\n\t\t\t\t\tL[t[1]][t[0]]=L[j][t[0]];\n\t\t\t\t\tif(~L[t[1]][t[0]]&&~L[t[1]][j])L[t[1]][t[0]]&=L[t[1]][j];\n\t\t\t\t\telse if(!~L[t[1]][t[0]])L[t[1]][t[0]]=L[t[1]][j];\n\t\t\t\t\tfor(int k=0;k<u.size();k++){\n\t\t\t\t\tG[j][u[k]]=G[u[k]][j]=0;\n\t\t\t\t\tint dot=dcalc(t[0],t[1],j,u[k]);\n\t\t\t\t\tif(dot>0){\n\t\t\t\t\t\t//L[t[1]][t[0]]=1;\n\t\t\t\t\t\tL[t[0]][t[1]]=0;\n\t\t\t\t\t}else if(dot==0){\n\t\t\t\t\t\tL[t[0]][t[1]]=L[t[1]][t[0]]=0;\n\t\t\t\t\t}else{\n\t\t\t\t\t\t//L[t[0]][t[1]]=1;\n\t\t\t\t\t\tL[t[1]][t[0]]=0;\n\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\tfor(int i=0;i<sz;i++)for(int j=0;j<sz;j++){\n\t\t\tif(G[i][j]&&L[i][j]!=0){\n\t\t\t\tg[i].push_back(j);\n//\t\t\tif(x[i]==9||x[j]==9)printf(\"%d,%d %d,%d\\n\",x[i],y[i],x[j],y[j]);\n\t\t\t}\n\t\t}\n\t\tint S=m[make_pair(sx,sy)];\n\t\tint T=m[make_pair(gx,gy)];\n\t\tfor(int i=0;i<sz;i++){\n\t\t\tijk[i]=99999999;\n\t\t\tv[i]=0;\n\t\t\trev[i]=-1;\n\t\t}\n\t\tpriority_queue<pair<double,int> >Q;\n\t\tijk[S]=0;\n\t\tQ.push(make_pair(0.0,S));\n\t\twhile(Q.size()){\n\t\t\tdouble cost=-Q.top().first;\n\t\t\tint at=Q.top().second;\n\t\t\tQ.pop();\n\t\t\tif(v[at])continue;\n\t\t\tv[at]=1;\n\t\t\tfor(int i=0;i<g[at].size();i++){\n\t\t\t\tif(!v[g[at][i]]&&ijk[g[at][i]]>cost+dist(at,g[at][i])){\n\t\t\t\t\tijk[g[at][i]]=cost+dist(at,g[at][i]);\n\t\t\t\t\trev[g[at][i]]=at;\n\t\t\t\t\tQ.push(make_pair(-ijk[g[at][i]],g[at][i]));\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\tif(ijk[T]>9999999){\n\t\t\tprintf(\"-1\\n\");continue;\n\t\t}\n\t\tint ind=0;\n\t\tint now=T;\n\t\twhile(now!=S){\n\t\t\tans[ind++]=now;\n\t\t\tnow=rev[now];\n\t\t}\n\t\tans[ind++]=S;\n\t\tfor(int i=ind-1;i>=0;i--){\n\t\t\tprintf(\"%d %d\\n\",x[ans[i]],y[ans[i]]);\n\t\t}\n\t\tprintf(\"0\\n\");\n\t}\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3156, "score_of_the_acc": -0.0295, "final_rank": 6 }, { "submission_id": "aoj_1279_1170008", "code_snippet": "#include <string> \n#include <algorithm> \n#include <cfloat> \n#include <climits> \n#include <cmath> \n#include <complex> \n#include <cstdio> \n#include <cstdlib> \n#include <cstring> \n#include <functional> \n#include <iostream> \n#include <map> \n#include <memory> \n#include <queue> \n#include <set> \n#include <sstream> \n#include <stack> \n#include <utility> \n#include <vector> \n\n#define EACH(i,c) for(__typeof((c).begin()) i=(c).begin(); i!=(c).end(); ++i)\n#define ALL(x) (x).begin(),(x).end() \ntypedef long long ll;\nusing namespace std; \nconst double eps = 1e-10;\n\nconst double INF = 1e+10;\ntypedef double Weight;\nstruct Edge{\n int from, to;\n Weight weight;\n int rev; // ネットワークフロー時の逆辺\n Edge(int from, int to, Weight weight) :\n from(from), to(to), weight(weight) { }\n Edge(int from, int to, Weight weight, int rev) :\n from(from), to(to), weight(weight), rev(rev){ }\n};\nbool operator < (const Edge &a, const Edge &b){\n if(a.weight != b.weight) return a.weight > b.weight;\n if(a.from != b.from) return a.from > b.from;\n return a.to > b.to;\n}\ntypedef vector<Edge> Edges;\ntypedef vector<Edges> Graph;\n\ntypedef vector<Weight> Array;\ntypedef vector<Array> Matrix;\n\nvoid addFlowEdge(Graph &g, int a, int b, Weight c){\n g[a].push_back(Edge(a, b, c, g[b].size()));\n g[b].push_back(Edge(b, a, 0, g[a].size() - 1));\n}\nvoid addUndirectedEdge(Graph &g, int a, int b, Weight c){\n g[a].push_back(Edge(a, b, c, g[b].size()));\n g[b].push_back(Edge(b, a, c, g[a].size() - 1));\n}\n\nvoid dijkstra(const Graph &g, int s, vector<Weight> &dist, vector<int> &prev){\n int n = g.size();\n dist.assign(n, INF);\n dist[s] = 0;\n prev.assign(n, -1);\n priority_queue<Edge> Q; // a < b <-> a.weight > b.weight\n Q.push(Edge(-2, s, 0));\n while(!Q.empty()){\n Edge e = Q.top(); Q.pop();\n if(prev[e.to] != -1) continue;\n prev[e.to] = e.from;\n EACH(i, g[e.to]){\n //for(Edges::const_iterator i=g[e.to].begin(); i!=g[e.to].end(); ++i){ //マクロが使えないとき\n if(dist[i -> to] > dist[i -> from] + i -> weight){\n dist[i -> to] = dist[i -> from] + i -> weight;\n Q.push(Edge(i -> from, i -> to, dist[i -> to]));\n }\n }\n }\n}\nvector<int> buildPath(const vector<int> &prev, int t){\n vector<int> path;\n for(int v = t; v >= 0; v = prev[v])\n path.push_back(v);\n reverse(path.begin(), path.end());\n return path;\n}\n\ntypedef complex<double> P;\n\nnamespace std{\n bool operator<(const P&a, const P &b){\n if(a.real()==b.real()) return a.imag() < b.imag();\n else return a.real() < b.real();\n }\n};\ninline double dot(const P &a, const P &b){\n return real(conj(a) * b);\n}\ninline double cross(const P &a, const P &b){\n return imag(conj(a) * b);\n}\nint ccw(const P &a, P b, P c){\n b -= a; c -= a;\n if (cross(b, c) > eps) return +1; // ccw\n if (cross(b, c) < -eps) return -1; // cw\n if (dot(b,c) < -eps) return +2; // c-a-b\n if (norm(b)+eps < norm(c)) return -2; // a-b-c\n return 0; // a-c-b\n}\nstruct Line : public vector<P> {\n Line() { }\n Line(const P &a, const P &b) {\n push_back(a); push_back(b);\n }\n};\ninline bool parallel(const Line &s, const Line &t){\n return abs(cross(s[1]-s[0], t[1]-t[0])) < eps;\n}\ninline bool orthogonal(const Line &s, const Line &t){\n return abs(dot(s[1]-s[0], t[1]-t[0])) < eps;\n}\nP projection(const Line &s, const P &a){\n P v = s[1] - s[0];\n return s[0] + dot(v, a - s[0]) / abs(v) / abs(v) * v;\n}\nP reflection(const Line &s, const P &a){\n P p = projection(s, a);\n return 2.0 * p - a;\n}\nstruct Segment : public Line {\n Segment(const P &a, const P &b) : Line(a,b) { }\n};\nstruct Circle{\n P o;\n double r;\n Circle() { }\n Circle(const P &o, double r) : o(o), r(r) { }\n Circle(double x, double y, double r) : o(x,y), r(r) { }\n inline bool contains(const P &p) const{\n return abs(p-o) < r + eps;\n }\n inline bool inside(const P &p) const{\n return abs(p-o) < r - eps;\n }\n pair<Line,Line> tangent(const P &p) const{\n vector<Line> res;\n double d = abs(p - o), s = r*r / d, t = sqrt(r*r-s*s);\n P q = o + s/d * (p - o), e = (p - o) * P(0, 1/d); \n return make_pair(Line(p,q+e*t), Line(p,q-e*t));\n }\n};\ntypedef vector<P> Polygon;\n#define curr(p,i) p[i]\n#define next(p,i) p[(i+1)%p.size()]\ninline bool intersectSS(const Segment &s, const Segment &t){\n return ccw(s[0],s[1],t[0]) * ccw(s[0],s[1],t[1]) <= eps &&\n ccw(t[0],t[1],s[0]) * ccw(t[0],t[1],s[1]) <= eps;\n}\n// TODO:端点の扱いが雑なのを何とかする.\nP crosspointSS(const Segment &s, const Segment &t){\n double a = cross(s[1] - s[0], t[1] - t[0]);\n double b = cross(s[1] - s[0], s[1] - t[0]);\n if(abs(a) < eps && abs(b) < eps) return s[0]; // same line\n // if(abs(a) < eps) assert(false);\n return t[0] + b/a * (t[1] - t[0]);\n}\n\nGraph segmentArrangement(const vector<Segment> &ss, vector<P> &ps){\n for(int i = 0; i < ss.size(); ++i){\n ps.push_back(ss[i][0]);\n ps.push_back(ss[i][1]);\n for(int j=i+1;j<ss.size();++j){\n if(intersectSS(ss[i], ss[j]))\n ps.push_back(crosspointSS(ss[i], ss[j]));\n }\n }\n sort(ALL(ps)); ps.erase(unique(ALL(ps)), ps.end());\n Graph g(ps.size());\n for(int i=0;i<ss.size();++i){\n vector<pair<double, int> > lst;\n for(int j=0;j<ps.size();++j){\n if(ccw(ss[i][0], ss[i][1], ps[j]) == 0)\n lst.push_back(make_pair(norm(ss[i][0]-ps[j]), j));\n }\n sort(ALL(lst));\n for(int j=0;j+1<lst.size();++j){\n int a = lst[j].second, b = lst[j+1].second;\n addUndirectedEdge(g, a, b, abs(ps[a]-ps[b]));\n }\n }\n return g;\n}\nint main(){\n for(;;){\n int n;\n cin >> n;\n if(n == 0) return 0;\n P s, g;\n vector<Segment> v;\n int x, y;\n cin >> x >> y; s=P(x,y);\n cin >> x >> y; g=P(x,y);\n for(int i = 0; i < n; ++i){\n int x1, y1, x2, y2;\n cin >> x1 >> y1 >> x2 >> y2;\n v.push_back(Segment(P(x1,y1), P(x2,y2)));\n }\n vector<Segment> road, signs;\n for(int i = 0; i < n; ++i){\n int f = 0;\n for(int j=0;j<n;++j) if(i != j) for(int k=0;k<2;++k){\n if(ccw(v[j][0], v[j][1], v[i][k]) == 0)\n f |= 1 << k;\n }\n if(f == 3) road.push_back(v[i]);\n else signs.push_back(v[i]);\n }\n vector<P> ps;\n Graph graph = segmentArrangement(road, ps);\n int si, gi;\n for(int i=0;i<ps.size();++i) if(abs(ps[i]-s) < eps) si = i;\n for(int i=0;i<ps.size();++i) if(abs(ps[i]-g) < eps) gi = i;\n\n EACH(i, signs){\n Segment ss = *i;\n EACH(j, graph) EACH(k, *j) {\n int a = k->from, b = k->to;\n bool f = true;\n double d = 1;\n if(ccw(ps[a], ps[b], ss[0]) == 0){\n d = dot(ss[0]-ss[1], ps[b]-ps[a]);\n }\n else if(ccw(ps[a], ps[b], ss[1]) == 0){\n d = dot(ss[1]-ss[0], ps[b]-ps[a]);\n }\n if(d < eps){\n k->weight = INF;\n }\n }\n }\n\n vector<int> prev(ps.size(),-1);\n vector<Weight> dist(ps.size());\n dijkstra(graph, si, dist, prev);\n if(dist[gi] == INF){\n cout << -1 << endl;\n }\n else{\n vector<int> path = buildPath(prev, gi);\n EACH(i, path){\n cout << (int)ps[*i].real() << \" \" << (int)ps[*i].imag() << endl;\n }\n cout << 0 << endl;\n }\n }\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 1320, "score_of_the_acc": 0, "final_rank": 1 }, { "submission_id": "aoj_1279_1114280", "code_snippet": "#include <iostream>\n#include <cmath>\n#include <complex>\n#include <vector>\n#include <algorithm>\n#include <cstdio>\n#define rep(i,n) for(int i=0;i<n;i++)\n#define fs first\n#define sc second\n#define X real()\n#define Y imag()\n#define pb push_back\n#define all(c) c.begin(),c.end()\n#define show(x) cout<<#x<<\" : \"<<x<<endl\nusing namespace std;\ntypedef double D;\ntypedef complex<D> P;\ntypedef pair<P,P> L;\nD inf=1e50,eps=1e-10,pi=acos(0.0)*2;\nbool eq(D a,D b){\n\treturn abs(a-b)<eps;\n}\nbool compxy(const P& l,const P& r){\n\treturn eq(l.X,r.X) ? l.Y<r.Y : l.X<r.X;\n}\nbool on(L l,P p){\n\treturn (eq(abs(l.fs-p)+abs(l.sc-p),abs(l.fs-l.sc)));\n}\nint N,M;\nint sx,sy,gx,gy;\nvector<L> lines;\nvector<P> points;\nvector<P> online[200];\nvector<int> ans;\nbool sign[200];\nint ne[200][200];\nD d[200][200];\nint id(P p){\n\treturn lower_bound(all(points),p,compxy)-points.begin();\n}\nint pre[200];\nD dist[200];\nvoid dijkstra(int s){\n\tbool used[200]={};\n\trep(i,M) dist[i]=inf;\n\tdist[s]=0;\n\trep(i,M){\n\t\tint u=-1;\n\t\trep(j,M) if(!used[j]&&(u==-1||dist[u]>dist[j])) u=j;\n\t\tif(u==-1) break;\n\t\tused[u]=true;\n\t\trep(j,M){\n\t\t\tif(dist[j]>dist[u]+d[u][j]){\n\t\t\t\tdist[j]=dist[u]+d[u][j];\n\t\t\t\tpre[j]=u;\n\t\t\t}\n\t\t}\n\t}\n}\nint main(){\n\twhile(true){\n\t\tcin>>N;\n\t\tif(N==0) break;\n\t\tlines.clear();\n\t\tpoints.clear();\n\t\tans.clear();\n\t\trep(i,N) sign[i]=false;\n\t\trep(i,N) online[i].clear();\n\t\tcin>>sx>>sy>>gx>>gy;\n\t\trep(i,N){\n\t\t\tint x,y,z,w;\n\t\t\tcin>>x>>y>>z>>w;\n\t\t\tlines.pb(L(P(x,y),P(z,w)));\n\t\t}\n\t\trep(i,N){\n\t\t\tP &a=lines[i].fs,&b=lines[i].sc;\n\t\t\tbool aon=false,bon=false;\n\t\t\trep(j,N) if(i!=j&&on(lines[j],a)) aon=true;\n\t\t\trep(j,N) if(i!=j&&on(lines[j],b)) bon=true;\n\t\t\tif(!aon||!bon) sign[i]=true;\n\t\t\telse{\n\t\t\t\tpoints.pb(a);\n\t\t\t\tpoints.pb(b);\n\t\t\t\trep(j,N){\n\t\t\t\t\tif(on(lines[j],a)) online[j].pb(a);\n\t\t\t\t\tif(on(lines[j],b)) online[j].pb(b);\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\tsort(all(points),compxy);\n\t\tpoints.erase(unique(all(points)),points.end());\n\t\tM=points.size();\n\t\trep(i,N) if(!sign[i]){\n\t\t\tsort(all(online[i]),compxy);\n\t\t\tonline[i].erase(unique(all(online[i])),online[i].end());\n\t\t}\n\t\trep(i,M) rep(j,M) if(i!=j) d[i][j]=inf;\n\t\trep(i,N) if(!sign[i]){\n\t\t\trep(j,online[i].size()-1){\n\t\t\t\tint x=id(online[i][j]),y=id(online[i][j+1]);\n\t\t\t\td[x][y]=d[y][x]=abs(online[i][j]-online[i][j+1]);\n\t\t\t}\n\t\t}\n/*\t\trep(i,N) if(!sign[i]){\n\t\t\trep(j,online[i].size()) cout<<online[i][j]<<\" \";\n\t\t\tcout<<endl;\n\t\t}*/\n\t\trep(i,N) if(sign[i]){\n\t\t\tP &a=lines[i].fs,&b=lines[i].sc;\n\t\t\trep(j,N) if(!sign[j]){\n\t\t\t\tif(on(lines[j],b)) swap(a,b);\n\t\t\t\tif(on(lines[j],a)){\n//\t\t\t\t\tshow(a);\n//\t\t\t\t\trep(k,online[j].size()) cout<<online[j][k]<<endl;\n//\t\t\t\t\tint iid=lower_bound(all(online[j]),a,compxy)-online[j].begin();\n//\t\t\t\t\tshow(iid);\n\t\t\t\t\tP y=*lower_bound(all(online[j]),a,compxy),x=*(lower_bound(all(online[j]),a,compxy)-1);\n\t\t\t\t\tD theta=abs(arg((b-a)/(y-x)));\n\t\t\t\t\tD a=theta-pi/2.0;\n\t\t\t\t\tif(a<=eps) d[id(x)][id(y)]=inf;\n\t\t\t\t\tif(a>=-eps) d[id(y)][id(x)]=inf;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\tint s=id(P(sx,sy)),g=id(P(gx,gy));\n\t\tdijkstra(s);\n\t\tif(dist[g]==inf){\n\t\t\tputs(\"-1\");\n\t\t\tcontinue;\n\t\t}\n\t\twhile(s!=g){\n\t\t\tans.pb(g);\n\t\t\tg=pre[g];\n\t\t}\n\t\tans.pb(s);\n\t\trep(i,ans.size()){\n\t\t\tint x=ans[ans.size()-1-i];\n\t\t\tcout<<(int)points[x].X<<\" \"<<(int)points[x].Y<<endl;\n\t\t}\n\t\tputs(\"0\");\n\t}\n}", "accuracy": 1, "time_ms": 50, "memory_kb": 1560, "score_of_the_acc": -0.4483, "final_rank": 14 }, { "submission_id": "aoj_1279_1060892", "code_snippet": "#include <algorithm>\n#include <climits>\n#include <cmath>\n#include <cstdlib>\n#include <functional>\n#include <iostream>\n#include <queue>\n#include <vector>\nusing namespace std;\n\nconstexpr double EPS = 1e-9;\n\nstruct point {\n\tdouble x, y;\n\tpoint(double x_ = 0.0, double y_ = 0.0):x(x_), y(y_) {}\n\tpoint(const point &p):x(p.x), y(p.y) {}\n\n\tpoint operator+ (const point &p) const {\n\t\treturn point(x + p.x, y + p.y);\n\t}\n\n\tpoint operator- (const point &p) const {\n\t\treturn point(x - p.x, y - p.y);\n\t}\n\n\tpoint operator* (double s) const {\n\t\treturn point(x * s, y * s);\n\t}\n\n\tpoint operator* (const point &p) const {\n\t\treturn point(x * p.x - y * p.y, x * p.y + y * p.x);\n\t}\n\n\tpoint operator/ (double s) const {\n\t\treturn point(x / s, y / s);\n\t}\n\n\tbool operator< (const point &p) const {\n\t\treturn x + EPS < p.x || (abs(x - p.x) < EPS && y + EPS < p.y);\n\t}\n\n\tbool operator== (const point &p) const {\n\t\treturn abs(x - p.x) < EPS && abs(y - p.y) < EPS;\n\t}\n};\n\nistream &operator>>(istream &is, point &p) {\n\treturn is >> p.x >> p.y;\n}\n\npoint rotate90(const point &p) {\n\treturn point(-p.y, p.x);\n}\n\ndouble norm(const point &p) {\n\treturn p.x * p.x + p.y * p.y;\n}\n\ndouble abs(const point &p) {\n\treturn sqrt(norm(p));\n}\n\ndouble dot(const point &a, const point &b) {\n\treturn a.x * b.x + a.y * b.y;\n}\n\ndouble cross(const point &a, const point &b) {\n\treturn a.x * b.y - a.y * b.x;\n}\n\nstruct segment {\n\tpoint a, b;\n\tsegment(const point &a_, const point &b_):a(a_), b(b_){}\n};\n\nstruct circle {\n\tpoint c;\n\tdouble r;\n\tcircle(const point &c_, const double &r_):c(c_), r(r_){}\n};\n\ntypedef vector<point> polygon;\n\nint ccw(const point &a, point b, point c) {\n\tb = b - a;\n\tc = c - a;\n\tconst double tmp = cross(b, c);\n\tif(tmp > EPS) return 1; // ccw\n\tif(tmp < -EPS) return -1; // cw\n\tif(dot(b, c) < 0) return 2; // c, a, b 順に一直線上\n\tif(norm(b) < norm(c)) return -2; // a, b, c 順に一直線上\n\treturn 0; //a, c, b 順で一直線上\n}\n\nbool intersect(const segment &s, const segment &t) {\n\treturn ccw(s.a, s.b, t.a) * ccw(s.a, s.b, t.b) <= 0\n\t\t&& ccw(t.a, t.b, s.a) * ccw(t.a, t.b, s.b) <= 0;\n}\n\nbool intersect(const segment &s, const point &p) {\n\treturn ccw(s.a, s.b, p) == 0;\n}\n\npoint crosspoint(const segment &s, const segment &t) {\n\tif(!intersect(s, t)) return s.a; // 交点を持たない\n\n\tconst double tmp = cross(s.b - s.a, t.b - t.a);\n\tif(abs(tmp) < EPS) { // 一直線上\n\t\tif(intersect(s, t.a)) return t.a;\n\t\tif(intersect(s, t.b)) return t.b;\n\t\tif(intersect(t, s.a)) return s.a;\n\t\treturn s.b;\n\t}\n\treturn t.a + (t.b - t.a) * cross(s.b - s.a, s.b - t.a) * (1.0 / tmp);\n}\n\nstruct edge {\n\tint to;\n\tdouble cost;\n\tedge(int to_, double cost_):to(to_), cost(cost_) {}\n};\n\ntypedef vector<vector<edge>> graph;\n\nint get_index(const vector<point> &points, const point &p) {\n\treturn lower_bound(points.begin(), points.end(), p) - points.begin();\n}\n\ngraph arrangement(const vector<segment> &segments, vector<point> &points) {\n\tvector<segment> streets, signs;\n\n\tfor(int i = 0; i < static_cast<int>(segments.size()); ++i) {\n\t\tbool a_on = false, b_on = false;\n\t\tfor(int j = 0; j < static_cast<int>(segments.size()); ++j) {\n\t\t\tif(i == j) continue;\n\t\t\ta_on |= intersect(segments[j], segments[i].a);\n\t\t\tb_on |= intersect(segments[j], segments[i].b);\n\t\t}\n\n\t\t(a_on && b_on ? streets : signs).emplace_back(segments[i]);\n\t}\n\n\tconst int n = streets.size();\n\tpoints.clear();\n\tfor(int i = 0; i < n; ++i) {\n\t\tconst auto &s1 = streets[i];\n\t\tpoints.emplace_back(s1.a);\n\t\tpoints.emplace_back(s1.b);\n\n\t\tfor(int j = i + 1; j < n; ++j) {\n\t\t\tconst auto &s2 = streets[j];\n\t\t\tif(intersect(s1, s2)) {\n\t\t\t\tpoints.emplace_back(crosspoint(s1, s2));\n\t\t\t}\n\t\t}\n\t}\n\n\tsort(points.begin(), points.end());\n\tpoints.erase(unique(points.begin(), points.end()), points.end());\n\n\tconst int V = points.size();\n\tgraph G(V);\n\n\tfor(const auto &s : streets) {\n\t\tvector<pair<double, int>> vs;\n\t\tfor(int i = 0; i < V; ++i) {\n\t\t\tif(intersect(s, points[i])) {\n\t\t\t\tvs.emplace_back(abs(s.a - points[i]), i);\n\t\t\t}\n\t\t}\n\n\t\tsort(vs.begin(), vs.end());\n\t\tfor(int i = 1; i < static_cast<int>(vs.size()); ++i) {\n\t\t\tconst int v = vs[i].second;\n\t\t\tconst int u = vs[i - 1].second;\n\t\t\tconst double d = vs[i].first - vs[i - 1].first;\n\n\t\t\tbool go = true;\n\t\t\tbool back = true;\n\n\t\t\tconst segment street(points[v], points[u]);\n\n\t\t\tfor(const auto & t : signs) {\n\t\t\t\tconst bool a_on = intersect(street, t.a);\n\t\t\t\tconst bool b_on = intersect(street, t.b);\n\n\t\t\t\tif(a_on || b_on) {\n\t\t\t\t\tconst point &base = (a_on ? t.a : t.b);\n\t\t\t\t\tconst point &tip = (a_on ? t.b : t.a);\n\t\t\t\t\tconst int tmp = ccw(base, base + rotate90(points[u] - points[v]), tip);\n\n\t\t\t\t\tif(tmp != 1) go = false;\n\t\t\t\t\tif(tmp != -1) back = false;\n\t\t\t\t}\n\t\t\t}\n\n\t\t\tif(go) G[v].emplace_back(u, d);\n\t\t\tif(back) G[u].emplace_back(v, d);\n\t\t}\n\t}\n\n\treturn G;\n}\n\nbool dijkstra(const graph &G, int s, int t, vector<int> &prev) {\n\ttypedef pair<double, int> P;\n\n\tconst int n = G.size();\n\n\tvector<double> dist(n, INT_MAX);\n\tpriority_queue<P, vector<P>, greater<P>> que;\n\n\tdist[s] = 0;\n\tque.push({0, s});\n\tprev.assign(n, -1);\n\n\twhile(!que.empty()) {\n\t\tconst auto d = que.top().first;\n\t\tconst auto v = que.top().second;\n\t\tque.pop();\n\n\t\tif(dist[v] + EPS < d) continue;\n\n\t\tif(v == t) return true;\n\n\t\tfor(const auto &e : G[v]) {\n\t\t\tconst auto next_dist = d + e.cost;\n\t\t\tif(dist[e.to] > next_dist + EPS) {\n\t\t\t\tdist[e.to] = next_dist;\n\t\t\t\tque.push({next_dist, e.to});\n\t\t\t\tprev[e.to] = v;\n\t\t\t}\n\t\t}\n\t}\n\t\n\treturn false;\n}\n\nint main() {\n\tcin.tie(nullptr);\n\tios::sync_with_stdio(false);\n\n\tfor(int n; cin >> n && n;) {\n\t\tpoint start, goal;\n\t\tcin >> start >> goal;\n\n\t\tvector<segment> segments;\n\t\tsegments.reserve(n);\n\n\t\tfor(int i = 0; i < n; ++i) {\n\t\t\tpoint a, b;\n\t\t\tcin >> a >> b;\n\t\t\tsegments.emplace_back(a, b);\n\t\t}\n\n\t\tvector<point> points;\n\t\tconst auto G = arrangement(segments, points);\n\n\t\tconst int start_index = get_index(points, start);\n\t\tconst int goal_index = get_index(points, goal);\n\n\t\tvector<int> prev;\n\t\tif(!dijkstra(G, start_index, goal_index, prev)) {\n\t\t\tcout << -1 << '\\n';\n\t\t\tcontinue;\n\t\t}\n\n\t\tvector<int> path;\n\t\tfor(int v = goal_index; v != -1; v = prev[v]) {\n\t\t\tpath.emplace_back(v);\n\t\t}\n\t\treverse(path.begin(), path.end());\n\n\t\tfor(const auto &idx : path) {\n\t\t\tcout << points[idx].x << ' ' << points[idx].y << '\\n';\n\t\t}\n\t\tcout << \"0\\n\";\n\t}\n\n\treturn EXIT_SUCCESS;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 1388, "score_of_the_acc": -0.0011, "final_rank": 3 } ]
aoj_1280_cpp
Problem F: Slim Span Given an undirected weighted graph G , you should find one of spanning trees specified as follows. The graph G is an ordered pair ( V , E ), where V is a set of vertices { v 1 , v 2 , ... , v n } and E is a set of undirected edges { e 1 , e 2 , ... , e m }. Each edge e ∈ E has its weight w ( e ). A spanning tree T is a tree (a connected subgraph without cycles) which connects all the n vertices with n - 1 edges. The slimness of a spanning tree T is defined as the difference between the largest weight and the smallest weight among the n - 1 edges of T . Figure 5: A graph G and the weights of the edges For example, a graph G in Figure 5(a) has four vertices { v 1 , v 2 , v 3 , v 4 } and five undirected edges { e 1 , e 2 , e 3 , e 4 , e 5 }. The weights of the edges are w ( e 1 ) = 3, w ( e 2 ) = 5, w ( e 3 ) = 6, w ( e 4 ) = 6, w ( e 5 ) = 7 as shown in Figure 5(b). Figure 6: Examples of the spanning trees of G There are several spanning trees for G . Four of them are depicted in Figure 6(a)-(d). The spanning tree T a in Figure 6(a) has three edges whose weights are 3, 6 and 7. The largest weight is 7 and the smallest weight is 3 so that the slimness of the tree T a is 4. The slimnesses of spanning trees T b , T c and T d shown in Figure 6(b), (c) and (d) are 3, 2 and 1, respectively. You can easily see the slimness of any other spanning tree is greater than or equal to 1, thus the spanning tree T d in Figure 6(d) is one of the slimmest spanning trees whose slimness is 1. Your job is to write a program that computes the smallest slimness. Input The input consists of multiple datasets, followed by a line containing two zeros separated by a space. Each dataset has the following format. n m a 1 b 1 w 1 . . . a m b m w m Every input item in a dataset is a non-negative integer. Items in a line are separated by a space. n is the number of the vertices and m the number of the edges. You can assume 2 ≤ n ≤ 100 and 0 ≤ m ≤ n ( n - 1)/2. a k and b k ( k = 1, ... , m ) are positive integers less than or equal to n , which represent the two vertices v a k and v b k connected by the k th edge e k . w k is a positive integer less than or equal to 10000, which indicates the weight of e k . You can assume that the graph G = ( V , E ) is simple, that is, there are no self-loops (that connect the same vertex) nor parallel edges (that are two or more edges whose both ends are the same two vertices). Output For each dataset, if the graph has spanning trees, the smallest slimness among them should be printed. Otherwise, -1 should be printed. An output should not contain extra characters. Sample Input 4 5 1 2 3 1 3 5 1 4 6 2 4 6 3 4 7 4 6 1 2 10 1 3 100 1 4 90 2 3 20 2 4 80 3 4 40 2 1 1 2 1 3 0 3 1 1 2 1 3 3 1 2 2 2 3 5 1 3 6 5 10 1 2 110 1 3 120 1 4 130 1 5 120 2 3 110 2 4 120 2 5 130 3 4 120 3 5 110 4 5 120 5 10 1 2 9384 1 3 887 1 4 2778 1 5 6916 2 3 7794 2 4 8336 2 5 5387 3 4 493 3 5 6650 4 5 1422 5 8 1 2 1 2 3 100 3 4 100 4 5 100 1 5 50 ...(truncated)
[ { "submission_id": "aoj_1280_11066219", "code_snippet": "#include <iostream>\n#include <vector>\n#include <tuple>\n#include <algorithm>\nusing namespace std;\n\nclass unionfind{\npublic:\n unionfind(int n): n_(n), m_(n) {\n parent_.resize(n);\n size_.resize(n);\n for(int i=0; i<n; i++){\n parent_[i] = i;\n size_[i] = 1;\n }\n }\n void unite(int i, int j){\n int p = root(i);\n int q = root(j);\n if(p==q) return;\n if(size_[p]>size_[q]){\n parent_[q] = p;\n size_[p] += size_[q];\n }else{\n parent_[p] = q;\n size_[q] += size_[p];\n }\n m_ -= 1;\n }\n bool is_same(int i, int j){\n return root(i) == root(j);\n }\n int group_num(){\n return m_;\n }\nprivate:\n int n_;\n int m_; // 連結成分数\n vector<int> parent_;\n vector<int> size_;\n\n int root(int i){\n if(parent_[i] == i) return i;\n parent_[i] = root(parent_[i]);\n return parent_[i];\n }\n};\n\n// 辺は(重み, 頂点1, 頂点2) (無向)\n// 使った辺の重みの最大値を返す\n// 全域木ができなければ代わりに-1を返す\nint kruskal(int n, vector<tuple<int,int,int>> edges){\n // sort(edges.begin(), edges.end()); // ソート済み配列を渡す\n unionfind uf(n+1); // 1-indexed\n int max_w = 0;\n // vector<pair<int,int>> mst(n-1);\n for(int i=0; i<edges.size(); i++){\n auto [w,u,v] = edges[i];\n if(!uf.is_same(u,v)){\n uf.unite(u,v);\n max_w = w;\n // mst.append(make_pair(u,v));\n }\n }\n if(uf.group_num() != 2){\n max_w = -1;\n }\n return max_w;\n}\n\nint solve(int n, vector<tuple<int,int,int>> edges){\n sort(edges.begin(), edges.end());\n int ans = -1;\n int m = edges.size();\n for(int i=0; i<m; i++){\n // その時点での最小全域木を構成\n int tmp = kruskal(n,edges);\n // 全域木ができなければその時点で終了\n if(tmp == -1){\n break;\n }\n // slimnessを記録\n if(ans == -1){\n ans = tmp - get<0>(edges[0]);\n }else{\n ans = min(ans, tmp - get<0>(edges[0]));\n }\n // 最小の辺を削除して再度実行\n edges.erase(edges.begin());\n }\n return ans;\n}\n\nint main(){\n int n,m;\n while(true){\n cin >> n >> m;\n if((n==0) & (m==0)){\n break;\n }\n\n vector<tuple<int,int,int>> edges;\n int x,y,w;\n for(int i=0; i<m; i++){\n cin >> x >> y >> w;\n edges.push_back({w,x,y});\n }\n\n cout << solve(n,edges) << endl;\n }\n}", "accuracy": 1, "time_ms": 120, "memory_kb": 3392, "score_of_the_acc": -0.1831, "final_rank": 2 }, { "submission_id": "aoj_1280_10848512", "code_snippet": "#include <iostream>\n#include <cstdio>\n#include <cstring>\n#include <cstdlib>\n#include <cmath>\n#include <cctype>\n#include <algorithm>\n#include <vector>\n#include <set>\n#include <map>\n#include <iomanip>\n\nusing namespace std;\n\ntypedef long long LL;\n\n#define REP(i, n) for (int i(0); i < (int) (n); i++)\n\nconst int N = 100 + 10;\nconst int INF = (int)1e9;\n\nint n, m;\nint maxv;\nint from, to;\nint w[N][N];\nint vis[N];\nvector<int> adj[N];\npair<int, pair<int, int> > e[N * N];\n\nbool findIt(int u, int g)\n{\n\tif (vis[u]) return false;\n\tvis[u] = true;\n\tif (u == g) return true;\n\tfor(int e = 0; e < (int)adj[u].size(); ++ e) {\n\t\tint v = adj[u][e];\n\t\tif (findIt(v, g)) {\n\t\t\tif (maxv < w[u][v]) {\n\t\t\t\tmaxv = w[u][v];\n\t\t\t\tfrom = u;\n\t\t\t\tto = v;\n\t\t\t}\n\t\t\treturn true;\n\t\t}\n\t}\n\treturn false;\n}\n\nvoid solve() {\n\tfor(int i = 0; i < m; ++ i) {\n\t\tint u, v, l;\n\t\tscanf(\"%d%d%d\", &u, &v, &l);\n\t\t--u, --v;\n\t\te[i] = make_pair(l, make_pair(u, v));\n\t\tw[u][v] = w[v][u] = l;\n\t}\n\tsort(e, e + m);\n\n\tfor(int i = 0; i < n; ++ i) {\n\t\tadj[i].clear();\n\t}\n\tint ret = INF;\n\tvector<int> ss;\n\tfor(int i = m - 1; i >= 0; -- i) {\n\t\tfill(vis, vis + n, false);\n\t\tmaxv = -INF;\n\t\tfindIt(e[i].second.first, e[i].second.second);\n\t\tif (maxv != -INF) {\n\t\t\tfor(int j = 0; j < (int)ss.size(); ++ j) {\n\t\t\t\tif (ss[j] == maxv) {\n\t\t\t\t\tss.erase(ss.begin() + j);\n\t\t\t\t\tbreak;\n\t\t\t\t}\n\t\t\t}\n\t\t\tfor(int j = 0; j < adj[from].size(); ++ j) {\n\t\t\t\tif (adj[from][j] == to) {\n\t\t\t\t\tadj[from].erase(adj[from].begin() + j);\n\t\t\t\t\tbreak;\n\t\t\t\t}\n\t\t\t}\n\t\t\tfor(int j = 0; j < adj[to].size(); ++ j) {\n\t\t\t\tif (adj[to][j] == from) {\n\t\t\t\t\tadj[to].erase(adj[to].begin() + j);\n\t\t\t\t\tbreak;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\tadj[e[i].second.first].push_back(e[i].second.second);\n\t\tadj[e[i].second.second].push_back(e[i].second.first);\n\t\tss.push_back(e[i].first);\n\t\tif ((int)ss.size() == n - 1) {\n\t\t\tret = min(ret, *max_element(ss.begin(), ss.end()) - *min_element(ss.begin(), ss.end()));\n\t\t}\n\t}\n\tif (ret == INF) {\n\t\tcout << -1 << endl;\n\t\treturn;\n\t}\n\tcout << ret << endl;\n}\n\nint main() {\n\tfor( ; cin >> n >> m; ) {\n\t\tif (n == 0 && m == 0) break;\n\t\tsolve();\n\t}\n\treturn 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3564, "score_of_the_acc": -0.3892, "final_rank": 10 }, { "submission_id": "aoj_1280_9637419", "code_snippet": "#include <bits/stdc++.h>\n\nusing namespace std;\n\n#define rep(i, a, b) for (int i = (int)(a); i < (int)(b); i++)\n#define rrep(i, a, b) for (int i = (int)(b)-1; i >= (int)(a); i--)\n#define ALL(v) (v).begin(), (v).end()\n#define UNIQUE(v) sort(ALL(v)), (v).erase(unique(ALL(v)), (v).end())\n#define SZ(v) (int)v.size()\n#define MIN(v) *min_element(ALL(v))\n#define MAX(v) *max_element(ALL(v))\n#define LB(v, x) int(lower_bound(ALL(v), (x)) - (v).begin())\n#define UB(v, x) int(upper_bound(ALL(v), (x)) - (v).begin())\n\nusing uint = unsigned int;\nusing ll = long long int;\nusing ull = unsigned long long;\nusing i128 = __int128_t;\nusing u128 = __uint128_t;\nconst int inf = 0x3fffffff;\nconst ll INF = 0x1fffffffffffffff;\n\ntemplate <typename T> inline bool chmax(T &a, T b) {\n if (a < b) {\n a = b;\n return 1;\n }\n return 0;\n}\ntemplate <typename T> inline bool chmin(T &a, T b) {\n if (a > b) {\n a = b;\n return 1;\n }\n return 0;\n}\ntemplate <typename T, typename U> T ceil(T x, U y) {\n assert(y != 0);\n if (y < 0)\n x = -x, y = -y;\n return (x > 0 ? (x + y - 1) / y : x / y);\n}\ntemplate <typename T, typename U> T floor(T x, U y) {\n assert(y != 0);\n if (y < 0)\n x = -x, y = -y;\n return (x > 0 ? x / y : (x - y + 1) / y);\n}\ntemplate <typename T> int popcnt(T x) {\n return __builtin_popcountll(x);\n}\ntemplate <typename T> int topbit(T x) {\n return (x == 0 ? -1 : 63 - __builtin_clzll(x));\n}\ntemplate <typename T> int lowbit(T x) {\n return (x == 0 ? -1 : __builtin_ctzll(x));\n}\n\ntemplate <class T, class U>\nostream &operator<<(ostream &os, const pair<T, U> &p) {\n os << \"P(\" << p.first << \", \" << p.second << \")\";\n return os;\n}\ntemplate <typename T> ostream &operator<<(ostream &os, const vector<T> &vec) {\n os << \"{\";\n for (int i = 0; i < vec.size(); i++) {\n os << vec[i] << (i + 1 == vec.size() ? \"\" : \", \");\n }\n os << \"}\";\n return os;\n}\ntemplate <typename T, typename U>\nostream &operator<<(ostream &os, const map<T, U> &map_var) {\n os << \"{\";\n for (auto itr = map_var.begin(); itr != map_var.end(); itr++) {\n os << \"(\" << itr->first << \", \" << itr->second << \")\";\n itr++;\n if (itr != map_var.end())\n os << \", \";\n itr--;\n }\n os << \"}\";\n return os;\n}\ntemplate <typename T> ostream &operator<<(ostream &os, const set<T> &set_var) {\n os << \"{\";\n for (auto itr = set_var.begin(); itr != set_var.end(); itr++) {\n os << *itr;\n ++itr;\n if (itr != set_var.end())\n os << \", \";\n itr--;\n }\n os << \"}\";\n return os;\n}\n#ifdef LOCAL\n#define show(...) _show(0, #__VA_ARGS__, __VA_ARGS__)\n#else\n#define show(...) true\n#endif\ntemplate <typename T> void _show(int i, T name) {\n cerr << '\\n';\n}\ntemplate <typename T1, typename T2, typename... T3>\nvoid _show(int i, const T1 &a, const T2 &b, const T3 &...c) {\n for (; a[i] != ',' && a[i] != '\\0'; i++)\n cerr << a[i];\n cerr << \":\" << b << \" \";\n _show(i + 1, a, c...);\n}\n\n/**\n * @brief template\n */\n\nstruct UnionFind{\n vector<int> par; int n;\n UnionFind(){}\n UnionFind(int _n):par(_n,-1),n(_n){}\n int root(int x){return par[x]<0?x:par[x]=root(par[x]);}\n bool same(int x,int y){return root(x)==root(y);}\n int size(int x){return -par[root(x)];}\n bool unite(int x,int y){\n x=root(x),y=root(y); if(x==y)return false;\n if(size(x)>size(y))swap(x,y);\n par[y]+=par[x]; par[x]=y; n--; return true;\n }\n};\n\n/**\n * @brief Union Find\n */\n\nstruct Edge {\n int A, B, C;\n};\n\nint main() {\nwhile(1) {\n int N, M;\n cin >> N >> M;\n if (N == 0) return 0;\n vector<Edge> G(M);\n rep(i,0,M) cin >> G[i].A >> G[i].B >> G[i].C, G[i].A--, G[i].B--;\n sort(ALL(G),[](const Edge& E1, const Edge& E2){return E1.C < E2.C;});\n int ANS = inf;\n rep(i,0,M) {\n UnionFind UF(N);\n int COUNT = 0;\n rep(j,i,M) {\n if (!UF.same(G[j].A,G[j].B)) {\n UF.unite(G[j].A,G[j].B);\n COUNT++;\n }\n if (COUNT == N-1) {\n chmin(ANS,G[j].C-G[i].C);\n break;\n }\n }\n }\n cout << (ANS == inf ? -1 : ANS) << endl;\n}\n}", "accuracy": 1, "time_ms": 20, "memory_kb": 3476, "score_of_the_acc": -0.2622, "final_rank": 6 }, { "submission_id": "aoj_1280_9362172", "code_snippet": "#include <bits/stdc++.h>\n\nusing ll = long long;\n\n#define rep(i, l, r) for (ll i = (l); i < (r); i++)\n#define rrep(i, r, l) for (ll i = (r); i-- > (l);)\ntemplate <class T, class U>\nbool chmin(T& lhs, U rhs) {\n return lhs > rhs ? (lhs = rhs, true) : false;\n}\ntemplate <class T, class U>\nbool chmax(T& lhs, U rhs) {\n return lhs < rhs ? (lhs = rhs, true) : false;\n}\nusing namespace std;\n\nstruct UF {\n vector<int> data;\n UF(int n): data(n, -1) {}\n int find(int x) {\n if (data[x] < 0) return x;\n return data[x] = find(data[x]);\n }\n bool merge(int lhs, int rhs) {\n lhs = find(lhs);\n rhs = find(rhs);\n if (lhs == rhs) return false;\n if (data[lhs] < data[rhs]) swap(lhs, rhs);\n data[rhs] += data[lhs];\n data[lhs] = rhs;\n return true;\n }\n};\n\nint solve() {\n int n, m;\n cin >> n >> m;\n if (n == 0) return 1;\n vector<tuple<int, int, int>> edges;\n UF check(n);\n int count = 0;\n for (int i = 0; i < m; i++) {\n int u, v, w;\n cin >> u >> v >> w;\n u--;\n v--;\n edges.emplace_back(w, u, v);\n count += check.merge(u, v);\n }\n sort(edges.begin(), edges.end());\n if (count != n - 1) {\n cout << -1 << '\\n';\n return 0;\n }\n const int INF = 1e9;\n int ans = INF;\n rep(i, 0, m) {\n UF uf(n);\n int count = 0;\n int min_w = INF;\n int max_w = -INF;\n rep(j, i, m) {\n auto [w, u, v] = edges[j];\n if (uf.merge(u, v)) {\n count++;\n chmin(min_w, w);\n chmax(max_w, w);\n }\n }\n if (count == n - 1) chmin(ans, max_w - min_w);\n }\n cout << ans << '\\n';\n return 0;\n}\n\nint main() {\n while (!solve())\n ;\n}", "accuracy": 1, "time_ms": 80, "memory_kb": 3476, "score_of_the_acc": -0.2902, "final_rank": 8 }, { "submission_id": "aoj_1280_9362170", "code_snippet": "#include <bits/stdc++.h>\n\nusing ll = long long;\n\n#define rep(i, l, r) for (ll i = (l); i < (r); i++)\n#define rrep(i, r, l) for (ll i = (r); i-- > (l);)\ntemplate <class T, class U>\nbool chmin(T& lhs, U rhs) {\n return lhs > rhs ? (lhs = rhs, true) : false;\n}\ntemplate <class T, class U>\nbool chmax(T& lhs, U rhs) {\n return lhs < rhs ? (lhs = rhs, true) : false;\n}\nusing namespace std;\n\nstruct UF {\n vector<int> data;\n UF(int n): data(n, -1) {}\n int find(int x) {\n if (data[x] < 0) return x;\n return data[x] = find(data[x]);\n }\n bool merge(int lhs, int rhs) {\n lhs = find(lhs);\n rhs = find(rhs);\n if (lhs == rhs) return false;\n if (data[lhs] < data[rhs]) swap(lhs, rhs);\n data[rhs] += data[lhs];\n data[lhs] = rhs;\n return true;\n }\n};\n\nint solve() {\n int n, m;\n std::cin >> n >> m;\n if (n == 0) return 1;\n const int W = 10005;\n std::vector<std::vector<std::pair<int, int>>> edges(W);\n UF check(n);\n int count = 0;\n for (int i = 0; i < m; i++) {\n int u, v, w;\n std::cin >> u >> v >> w;\n u--;\n v--;\n edges[w].emplace_back(u, v);\n count += check.merge(u, v);\n }\n if (count != n - 1) {\n std::cout << -1 << '\\n';\n return 0;\n }\n\n auto satisfy = [&](int key) {\n for (int i = 0; i + key < W; i++) {\n UF uf(n);\n int count = 0;\n for (int w = i; w <= i + key; w++) {\n for (auto [u, v]: edges[w]) {\n count += uf.merge(u, v);\n }\n if (count == n - 1) break;\n }\n if (count == n - 1) {\n return true;\n }\n }\n return false;\n };\n\n int ok = W;\n int ng = -1;\n while (ok - ng > 1) {\n int key = (ok + ng) / 2;\n (satisfy(key) ? ok : ng) = key;\n }\n std::cout << ok << '\\n';\n return 0;\n}\n\nint main() {\n while (!solve())\n ;\n}", "accuracy": 1, "time_ms": 2150, "memory_kb": 3544, "score_of_the_acc": -1.3593, "final_rank": 20 }, { "submission_id": "aoj_1280_8644198", "code_snippet": "#include \"bits/stdc++.h\"\n\nusing namespace std;\n\nusing i64 = long long;\n\nstruct UnionFind{\n vector<int> par;\n UnionFind(int n) : par(n, -1){}\n int Find(int x){return par[x] < 0 ? x : Find(par[x]);}\n int cnt = 0;\n void Unite(int x, int y){\n x = Find(x), y = Find(y);\n if(x == y)return;//continue;\n if(par[x] > par[y]){ // if size x < size y\n swap(x, y);\n }\n par[x] += par[y];\n par[y] = x;\n ++cnt;\n }\n};\n\nint main() {\n while(true){\n int n, m;\n cin >> n >> m;\n if(n == 0)break;\n vector<int> a(m), b(m), w(m);\n for(int i = 0; i < m; ++i){\n cin >> a[i] >> b[i] >> w[i];\n --a[i], --b[i];\n }\n vector<pair<int,int>> edges;\n for(int i = 0; i < m; ++i){\n edges.emplace_back(w[i], i);\n }\n sort(edges.begin(), edges.end());\n int ans = 1e9;\n for(int i = 0; i < m; ++i){\n UnionFind uf(n);\n for(int j = i; j < m; ++j){\n int x = a[edges[j].second];\n int y = b[edges[j].second];\n uf.Unite(x, y);\n if(uf.cnt == n - 1){\n ans = min(ans, edges[j].first - edges[i].first);\n break;\n }\n }\n }\n cout << (ans == 1e9 ? -1 : ans) << endl;\n }\n\n}", "accuracy": 1, "time_ms": 20, "memory_kb": 3304, "score_of_the_acc": -0.0047, "final_rank": 1 }, { "submission_id": "aoj_1280_8524893", "code_snippet": "#include <bits/stdc++.h>\n\nstruct UnionFind {\n int n;\n std::vector<int> data;\n UnionFind(int n): data(n, -1) {}\n\n int find(int x) {\n if (data[x] < 0) return x;\n return data[x] = find(data[x]);\n }\n\n bool merge(int lhs, int rhs) {\n lhs = find(lhs);\n rhs = find(rhs);\n if (lhs == rhs) return false;\n if (data[lhs] < data[rhs]) std::swap(lhs, rhs);\n data[rhs] += data[lhs];\n data[lhs] = rhs;\n return true;\n }\n\n int size(int x) {\n return -data[find(x)];\n }\n};\n\nint main() {\n while (true) {\n int n, m;\n std::cin >> n >> m;\n if (n == 0 && m == 0) break;\n \n std::vector<std::tuple<int, int, int>> edges;\n edges.reserve(m);\n for (int i = 0; i < m; i++) {\n int u, v, w;\n std::cin >> u >> v >> w;\n u--; v--;\n edges.emplace_back(w, u, v);\n }\n std::sort(edges.begin(), edges.end());\n\n const int INF = 1e9;\n int ans = INF;\n auto solve = [&](int left) -> int {\n UnionFind uf(n);\n for (int i = left; i < m; i++) {\n auto [w, u, v] = edges[i];\n uf.merge(u, v);\n if (uf.size(0) == n) {\n return w - std::get<0>(edges[left]);\n }\n }\n return INF;\n };\n\n for (int i = 0; i < m; i++) {\n int res = solve(i);\n if (res == INF) {\n break;\n }\n ans = std::min(ans, res);\n }\n\n if (ans == INF) ans = -1;\n std::cout << ans << std::endl;\n }\n}", "accuracy": 1, "time_ms": 20, "memory_kb": 3452, "score_of_the_acc": -0.2262, "final_rank": 4 }, { "submission_id": "aoj_1280_7824974", "code_snippet": "#define _USE_MATH_DEFINES\n#include<bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\nusing ld = long double;\nusing H = pair<ll, ll>;\nusing P = pair<ll, H>;\nusing vi = vector<int>;\nusing vl = vector<ll>;\n#define all(a) (a).begin(), (a).end()\n#define fs first\n#define sc second\n#define xx first\n#define yy second.first\n#define zz second.second\n#define Q(i, j, k) mkp((i), mkp((j), (k)))\n#define rng(i, s, n) for(int i = (s) ; i < (n) ; i++)\n#define rep(i, n) rng(i, 0, (n))\n#define mkp make_pair\n#define vec vector\n#define pb emplace_back\n#define siz(a) int((a).size())\n#define crdcomp(b) sort(all((b))); (b).erase(unique(all((b))), (b).end())\n#define getidx(b, i) (lower_bound(all((b)), (i))-(b).begin())\n#define ssp(i, n) (i==(ll)(n)-1?\"\\n\":\" \")\n#define ctoi(c) (int)((c)-'0')\n#define itoc(c) (char)((c)+'0')\n#define cyes printf(\"Yes\\n\")\n#define cno printf(\"No\\n\")\n#define cdf(n) for(int __quetimes=(n); __quetimes>0; __quetimes--)\n#define gcj printf(\"Case #%lld: \", __quetimes+1)\n#define readv(a, n) (a).resize((n), 0); rep(i, (n)) (a)[i]=read()\n#define found(a, x) (a.find(x)!=a.end())\nconstexpr ll mod = (ll)1e9 + 7;\nconstexpr ll Mod = 998244353;\nconstexpr ld EPS = 1e-10;\nconstexpr ll inf = (ll)3 * 1e18;\nconstexpr int Inf = 1e9;\nconstexpr int dx[] = { -1, 1, 0, 0 }, dy[] = { 0, 0, -1, 1 };\ntemplate<class T>bool chmax(T& a, const T& b) { if (a < b) { a = b; return 1; } return 0; }\ntemplate<class T>bool chmin(T& a, const T& b) { if (b < a) { a = b; return 1; } return 0; }\nll read() { ll u, k = scanf(\"%lld\", &u); return u; }\nstring reads() { string s; cin >> s; return s; }\nbool ina(H t, int h, int w) { return 0 <= t.fs && t.fs < h && 0 <= t.sc && t.sc < w; }\nbool ina(int t, int l, int r) { return l <= t && t < r; }\nll gcd(ll i, ll j) { return j ? gcd(j, i % j) : i; }\ntemplate<typename T>\nvoid fin(T x) { cout << x << endl; exit(0); }\n//--------------------------------------------------------------\nclass unionfind {\n int size = 0, grps = 0;\n vector<int>pa;\npublic:\n unionfind() {}\n unionfind(int n) {\n init(n);\n }\n void init(int n) {\n size = n, grps = n;\n pa.assign(n, -1);\n }\n int root(int x) {\n if (pa[x] < 0) return x;\n return pa[x] = root(pa[x]);\n }\n bool unite(int x, int y) {\n x = root(x); y = root(y);\n if (x == y) return false;\n grps--;\n if (pa[x] > pa[y]) swap(x, y);\n pa[x] += pa[y];\n pa[y] = x;\n return true;\n }\n bool same(int x, int y) {\n return root(x) == root(y);\n }\n bool isroot(int x) {\n return x == root(x);\n }\n int sz(int x) {\n return -pa[root(x)];\n }\n int groups() {\n return grps;\n }\n H operator[](int x) {\n x = root(x);\n return H{ x,-pa[x] };\n }\n};\n\nsigned main() {\n int n, m;\n while (cin >> n >> m && n + m > 0) {\n struct edge {\n int x, y, w;\n };\n vec<edge>e(m);\n rep(i, m) cin >> e[i].x >> e[i].y >> e[i].w, e[i].x--, e[i].y--;\n sort(all(e), [&](edge& a, edge& b) {return a.w < b.w;});\n unionfind uf(n);\n int ans = Inf;\n rep(i, m) {\n uf.init(n);\n for (int j = i;j < m;j++) {\n uf.unite(e[j].x, e[j].y);\n if (uf.groups() == 1) chmin(ans, e[j].w - e[i].w);\n }\n }\n if (ans == Inf) ans = -1;\n cout << ans << endl;\n }\n}", "accuracy": 1, "time_ms": 100, "memory_kb": 3556, "score_of_the_acc": -0.4193, "final_rank": 12 }, { "submission_id": "aoj_1280_7396549", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\ntypedef long long ll;\n// typedef __int128 ll;\ntypedef double ld;\nconst int mod = 1e9 + 7;\nconst int MAXN = 18;\n\nll pmod(ll v, ll p = mod) {\n v %= p;\n if (v < 0) {\n v += p;\n }\n return v;\n}\n\nll power(ll a, ll b, ll p = mod) {\n ll c = 1;\n for (; b; b >>= 1) {\n if (b & 1) {\n c = pmod(c * a, p);\n }\n a = pmod(a * a, p);\n }\n return c;\n}\n\nll inv(ll a) { return power(a, mod - 2); }\n\nclass DSU {\n public:\n DSU() {}\n DSU(int n) {\n // sz.assign(n + 1, 1);\n fa.assign(n + 1, 0);\n for (int i = 1; i <= n; i++) {\n fa[i] = i;\n }\n }\n\n int get_fa(int u) {\n if (u == fa[u]) {\n return u;\n }\n fa[u] = get_fa(fa[u]);\n return fa[u];\n }\n\n void unite(int u, int v) {\n u = get_fa(u);\n v = get_fa(v);\n // u -> fa[u] -> fa[v] <- v\n fa[u] = fa[v];\n\n /*\n if (u == v) {\n return;\n }\n if (sz[u] < sz[v]) {\n swap(u, v);\n }\n fa[v] = u;\n sz[u] += sz[v];\n */\n }\n\n bool is_same_set(int u, int v) { return get_fa(u) == get_fa(v); }\n\n vector<int> fa;\n // vector<int> sz;\n};\n\nvoid solve(int cas) {\n int n, m;\n cin >> n >> m;\n if (n == 0 && m == 0) {\n exit(0);\n }\n vector<tuple<int, int, int>> x;\n for (int i = 1; i <= m; i++) {\n int u, v, w;\n cin >> u >> v >> w;\n x.push_back(make_tuple(u, v, w));\n }\n sort(x.begin(), x.end(), [&](const tuple<int, int, int> &i, const tuple<int, int, int> &j) -> bool { return get<2>(i) < get<2>(j); });\n int ans = 1e9;\n for (int i = 0; i < m; i++) {\n DSU dsu(n);\n int cnt = 0;\n for (int j = i; j < m; j++) {\n // auto [u, v, w] = x[j];\n int u = get<0>(x[j]);\n int v = get<1>(x[j]);\n int w = get<2>(x[j]);\n if (dsu.get_fa(u) != dsu.get_fa(v)) {\n cnt++;\n dsu.unite(u, v);\n if (cnt == n - 1) {\n ans = min(ans, int(w - get<2>(x[i])));\n break;\n }\n }\n }\n }\n if (ans == 1e9) {\n ans = -1;\n }\n cout << ans << endl;\n}\n\nint main() {\n ios::sync_with_stdio(false);\n // freopen(\"hidden.in\", \"r\", stdin);\n // freopen(\"hidden.out\", \"w\", stdout);\n int T = 1, cas = 0;\n // cin >> T;\n // while (T--) {\n while (1) {\n solve(++cas);\n }\n return 0;\n}\n\n/*\n1\n9\n2\n1689\n3\n946157\n4\n1318742807\n*/", "accuracy": 1, "time_ms": 20, "memory_kb": 3588, "score_of_the_acc": -0.4298, "final_rank": 15 }, { "submission_id": "aoj_1280_7248791", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\n\n#define rep(i, N) for(ll i = 0; i < (ll)(N); i++)\n\nusing ll = long long;\nusing Tpl = tuple<ll, ll, ll>;\n\nconst ll LINF = 1001001001001001001ll;\n\nclass UnionFind{\n vector<int> par, rank, sz;\n\n public:\n UnionFind(int N) : par(N), rank(N, 0), sz(N, 1) {\n for(int i = 0; i < N; i++) par[i] = i;\n }\n void unite(int x, int y){\n x = find(x), y = find(y);\n if(x == y) return;\n sz[x] = sz[y] = sz[x] + sz[y];\n if(rank[x] < rank[y]){\n par[x] = y;\n }\n else{\n par[y] = x;\n if(rank[x] == rank[y]) rank[x]++;\n }\n }\n int find(int x){\n if(par[x] == x) return x;\n else{\n sz[x] = sz[par[x] = find(par[x])];\n return par[x];\n }\n }\n bool same(int x, int y){\n return find(x) == find(y);\n }\n int get_size(int x){\n find(x);\n return sz[x];\n }\n};\n\nint main(){\n cin.tie(nullptr);\n ios_base::sync_with_stdio(false);\n\n while(1){\n int N, M; cin >> N >> M;\n if(N == 0) return 0;\n\n vector<Tpl> edges(M);\n rep(i, M){\n ll u, v, w; cin >> u >> v >> w; u--, v--;\n edges[i] = make_tuple(w, u, v);\n }\n sort(edges.begin(), edges.end());\n\n int prev_w = -1;\n ll ans = LINF;\n rep(i, M){\n if(get<0>(edges[i]) == prev_w) continue;\n int start = get<0>(edges[i]);\n UnionFind UF(N);\n for(int j = i; j < M; j++){\n auto [w, u, v] = edges[j];\n UF.unite(u, v);\n //cout << u << \" \" << v << endl;\n if(UF.get_size(0) == N){\n ans = min(ans, w - start);\n break;\n }\n }\n if(UF.get_size(0) != N) break;\n prev_w = get<0>(edges[i]);\n }\n\n cout << (ans < LINF ? ans : -1) << '\\n';\n }\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3628, "score_of_the_acc": -0.485, "final_rank": 17 }, { "submission_id": "aoj_1280_7236037", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n#define foa(s, v) for(auto &s : v)\n#define all(v) v.begin(), v.end()\n#define REPname(a,b,c,d,...) d\n#define rep(...) REPname(__VA_ARGS__, REP2, REP1, REP0)(__VA_ARGS__)\n#define REP2(i,l,r) for(int i = l; i < r; i++)\n#define REP1(i, x) REP2(i,0,x)\n#define REP0(x) REP1(SPJ, x)\n#define sz(x) int(x.size())\n\ntemplate <class T>\nusing V=vector<T>;\n\ntemplate <class T>\nusing VV=vector<V<T>>;\n\ntemplate<class T>\nusing pqmin = priority_queue<T, V<T>, greater<T>>;\nusing ll = long long ;\nusing vi = vector<int>;\nusing vvi = vector<vi>;\nusing vvvi = vector<vvi>;\nusing vll = vector<ll>;\nusing vvll = V<vll>;\nconstexpr ll INF = 1e18;\nconstexpr int inf = 1e9;\ntemplate<class T>\ninline bool chmax(T &a, T b){\n\treturn a < b ? a=b, 1 : 0;\n}\ntemplate <class T>\ninline bool chmin(T &a, T b) {\n\treturn a > b ? a = b, 1 : 0;\n}\n\ntemplate <class T>\nvoid view(T x) {\n\tcerr << x;\n}\n\ntemplate <class T>\nvoid view(V<T> v) {\n\tcerr << \"{ \";\n\tfoa(t, v) {view(t) ; cerr << \", \";}\n\tcerr << \"}\";\n\tcerr << endl;\n}\n\n\ntemplate <class T>\nvoid view(VV<T> v) {\n\tcerr << \"{ \";\n\tfoa(t, v) {view(t) ; cerr << \",\\n\";}\n\tcerr << \"}\";\n\tcerr << endl;\n}\n\n\n// template <c0lass T>\nvoid view(int x) {\n\tcerr << x;\n}\n\n#ifdef LOCAL\nvoid debug_out() {}\ntemplate <class T>\nvoid debug_out(T x) {\n\tview(x);\n}\ntemplate <class H, class... T>\nvoid debug_out(H h, T... t) {\n\tview(h);\n\tcerr << \", \";\n\tdebug_out(t...);\n}\n#define debug(...) \\\n\tdo { \\\n\t\tcerr << __LINE__ << \" [\" << #__VA_ARGS__ << \"] : [\"; \\\n\t\tdebug_out(__VA_ARGS__); \\\n\t\tcerr << \"]\\n\"; \\\n\t} while(0)\n#else\n#define debug(...) (void(0))\n#endif\nusing vvi = V<vi>;\n\nstruct UF{\n\tvector<int> dat;\n\tint components;\n\tUF(int n) : dat(n, -1), components(n) {}\n\tint root(int x) \n\t{\n\t\tint& p = dat[x];\n\t\tif(p < 0) return x;\n\t\treturn p = root(p);\n\t}\n\tbool merge(int x, int y) {\n\t\tx = root(x);\n\t\ty = root(y);\n\t\tif(x == y) return false;\n\t\tif(-dat[x] < -dat[y]) swap(x, y);\n\t\tdat[x] += dat[y];\n\t\tdat[y] = x;\n\t\tcomponents--;\n\t\treturn true;\n\t}\n};\n\nll modpow(ll x, ll n, ll md){\n\tll ret = 1 % md;\n\twhile(n > 0){\n\t\tif(n & 1) {ret *= x; ret %= md;}\n\t\tn >>= 1;\n\t\tx *= x;\n\t\tx %= md;\n\t}\n\tif(ret < 0) ret += abs(md);\n\treturn ret;\n}\n\nusing S = ll;\nusing F = ll; // +\nconstexpr ll e = INF;\nconstexpr ll id = 0LL;\nconstexpr ll replace_e = e;\nll op(ll a, ll b) {return min(a,b);}\nll mapping(F f, S s) {return S(f+s);}\nll composition(F f, F g) {return f+g;}\nstruct node {\n\tnode *l = nullptr;\n\tnode *r = nullptr;\n\tint lo, hi;\n\tll mset = e;\n\tll madd = id;\n\tll val = e;\n\tnode(int lo, int hi) : lo(lo), hi(hi) {}\n\tnode (vll& v, int lo, int hi) : lo(lo), hi(hi) {\n\t\tif(lo + 1 < hi) {\n\t\t\tint mid = lo + (hi - lo) / 2;\n\t\t\tl = new node (v, lo, mid);\n\t\t\tr = new node(v, mid, hi);\n\t\t\tval = op(l->val, r->val);\n\t\t}\n\t\telse {\n\t\t\tval = v[lo];\n\t\t}\n\t}\n\n\tS query(int L, int R) {\n\t\tif(R <= lo || hi <= L) return e;\n\t\tif(L <=lo && hi <= R) return val;\n\t\tpush();\n\t\treturn op(l->query(L, R), r->query(L,R));\n\t}\n\n\tvoid set(int L, int R, ll x) {\n\t\tif(R <= lo || hi <= L) return;\n\t\tif(L <= lo && hi <= R) mset = val = x, madd = id;\n\t\telse {\n\t\t\tpush(), l->set (L, R, x) , r->set(L,R,x);\n\t\t\tval = op(l->val, r->val);\n\t\t}\n\t}\n\n\tvoid add(int L, int R, ll x) {\n\t\tif(R <= lo || hi <= L) return;\n\t\tif(L <= lo && hi <= R) {\n\t\t\tif(mset != replace_e) mset = mapping(x, mset);\n\t\t\telse madd = composition(x, madd);\n\t\t\tval = mapping(x, val);\n\t\t} else {\n\t\t\tpush(), l->add(L, R, x), r->add(L, R, x);\n\t\t\tval = op(l->val, r->val);\n\t\t}\n\t}\n\n\tvoid push() {\n\t\tif(!l) {\n\t\t\tint mid = lo + (hi - lo) / 2;\n\t\t\tl = new node(lo, mid);\n\t\t\tr = new node(mid, hi);\n\t\t}\n\t\tif(mset != replace_e)\n\t\t\tl->set(lo, hi, mset), r->set(lo, hi, mset), mset = replace_e;\n\t\telse if(madd)\n\t\t\tl->add(lo, hi, madd), r->add(lo, hi, madd), madd = id;\n\t}\n};\n\nll mst(int n, vector<tuple<ll, int, int>>& edges, int start_id) {\n\tUF uf(n);\n\tll ans = -INF;\n\trep(i, start_id, int(edges.size())){\n\t\tll w;\n\t\tint a, b;\n\t\ttie(w, a, b) = edges.at(i);\n\t\tif(uf.merge(a, b)) {\n\t\t\tchmax(ans, w);\n\t\t}\n\t}\n\tif(uf.components > 1) return INF;\n\treturn ans;\n}\n\nll solve(int n){ // first : 1, second : -1, draw : 0\n\tint m; cin >> m;\n\tvector<tuple<ll, int, int>> edges;\n\trep(m) {\n\t\tint a, b;\n\t\tll w; cin >> a >> b >> w;\n\t\tedges.emplace_back(w, a-1, b-1);\n\t}\n\tsort(all(edges));\n\tll ret = INF;\n\trep(i, m) {\n\t\tll res = mst(n, edges, i);\n\t\tchmin(ret, res - get<0>(edges.at(i)));\n\t}\n\n\tif(ret > 112345) ret = -1;\n\treturn ret;\n}\n\nint main() {\n\tios::sync_with_stdio(false);\n\tcin.tie(nullptr);\n\tcout << fixed << setprecision(15);\n\tsrand((unsigned)time(NULL));\n\tint n;\n\twhile(cin >> n && n) {\n\t\tcout << solve(n) << '\\n';\n\t}\n}", "accuracy": 1, "time_ms": 110, "memory_kb": 3448, "score_of_the_acc": -0.2623, "final_rank": 7 }, { "submission_id": "aoj_1280_7190711", "code_snippet": "#include <iostream>\n#include <vector>\n#include <algorithm>\nusing namespace std;\nusing P = pair<int, int>;\n\nstruct UnionFind{\n vector<int> parent;\n UnionFind(int n): parent(vector<int>(n, -1)){}\n\n int root(int x){\n if(parent[x] < 0) return x;\n else return parent[x] = root(parent[x]);\n }\n\n bool unite(int x, int y){\n if(root(x) == root(y)) return false;\n x = root(x); y = root(y);\n if(-parent[x] < -parent[y]) swap(x, y);\n parent[x] += parent[y];\n parent[y] = x;\n return true;\n }\n\n int count(int x){\n return -parent[root(x)];\n }\n};\n\nint main(){\n while(true){\n int n, m; cin >> n >> m;\n if(n == 0) break;\n\n vector<vector<P>> es(10010);\n for(int i = 0; i < m; i++){\n int a, b, w; cin >> a >> b >> w;\n es[w].emplace_back(a-1, b-1);\n }\n int ans = 1 << 30;\n for(int i = 0; i < es.size(); i++){\n if(es[i].size() == 0) continue;\n UnionFind uf(n);\n int j = i;\n for(; j < es.size(); j++){\n for(auto &it: es[j]) uf.unite(it.first, it.second);\n if(uf.count(0) == n) break;\n }\n if(uf.count(0) == n) ans = min(ans, j-i);\n }\n if(ans == 1 << 30) ans = -1;\n cout << ans << '\\n';\n }\n return 0;\n}", "accuracy": 1, "time_ms": 30, "memory_kb": 3424, "score_of_the_acc": -0.189, "final_rank": 3 }, { "submission_id": "aoj_1280_7154612", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\n\nstruct dsu {\n public:\n dsu() : _n(0) {}\n dsu(int n) : _n(n), parent_or_size(n, -1) {}\n\n int merge(int a, int b) {\n assert(0 <= a && a < _n);\n assert(0 <= b && b < _n);\n int x = leader(a), y = leader(b);\n if (x == y) return x;\n if (-parent_or_size[x] < -parent_or_size[y]) std::swap(x, y);\n parent_or_size[x] += parent_or_size[y];\n parent_or_size[y] = x;\n return x;\n }\n\n bool same(int a, int b) {\n assert(0 <= a && a < _n);\n assert(0 <= b && b < _n);\n return leader(a) == leader(b);\n }\n\n int leader(int a) {\n assert(0 <= a && a < _n);\n if (parent_or_size[a] < 0) return a;\n return parent_or_size[a] = leader(parent_or_size[a]);\n }\n\n int size(int a) {\n assert(0 <= a && a < _n);\n return -parent_or_size[leader(a)];\n }\n\n std::vector<std::vector<int>> groups() {\n std::vector<int> leader_buf(_n), group_size(_n);\n for (int i = 0; i < _n; i++) {\n leader_buf[i] = leader(i);\n group_size[leader_buf[i]]++;\n }\n std::vector<std::vector<int>> result(_n);\n for (int i = 0; i < _n; i++) {\n result[i].reserve(group_size[i]);\n }\n for (int i = 0; i < _n; i++) {\n result[leader_buf[i]].push_back(i);\n }\n result.erase(\n std::remove_if(result.begin(), result.end(),\n [&](const std::vector<int>& v) { return v.empty(); }),\n result.end());\n return result;\n }\n\n private:\n int _n;\n // root node: -1 * component size\n // otherwise: parent\n std::vector<int> parent_or_size;\n};\n\nint main(){\n int n, m;\n while(cin >> n >> m, n){\n vector<tuple<int,int,int>> edge(m);\n int u, v, c;\n for(int i = 0; i < m; i++){\n cin >> u >> v >> c;\n u--, v--;\n edge[i] = {c, u, v};\n }\n sort(edge.begin(), edge.end());\n auto f = [&](int st){\n dsu uf(n);\n for(int i = st; i < m; i++){\n tie(c, u, v) = edge[i];\n uf.merge(u, v);\n if(uf.size(0) == n)return c - get<0>(edge[st]);\n }\n return 1 << 30;\n };\n int ans = 1 << 30;\n for(int i = 0; i < m; i++){\n int v = f(i);\n if(v >> 30 & 1)break;\n ans = min(ans, v);\n }\n if(ans >> 30 & 1)ans = -1;\n cout << ans << '\\n';\n }\n}", "accuracy": 1, "time_ms": 20, "memory_kb": 3464, "score_of_the_acc": -0.2442, "final_rank": 5 }, { "submission_id": "aoj_1280_6775745", "code_snippet": "#pragma GCC target(\"avx2\")\n#pragma GCC optimize(\"O3\")\n#pragma GCC optimize(\"unroll-loops\")\n#include \"bits/stdc++.h\"\n\n#ifdef _MSC_VER\n#include <intrin.h> //gcc上ではこれがあると動かない。__popcnt, umul128 等用のincludeファイル。\n#define __builtin_popcount __popcnt\n#define __builtin_popcountll __popcnt64\n// 1 の位から何個 0 が連なっているか。(0 入れると 0 を返す。)\ninline unsigned int __builtin_ctz(unsigned int x) { unsigned long r; _BitScanForward(&r, x); return r; }\ninline unsigned int __builtin_ctzll(unsigned long long x) { unsigned long r; _BitScanForward64(&r, x); return r; }\n// 2進での leading 0 の個数。(0 入れると 32, 64 を返す。)\ninline unsigned int __builtin_clz(unsigned x) { return (unsigned int)__lzcnt(x); }\ninline unsigned int __builtin_clzll(unsigned long long x) { return (unsigned int)__lzcnt64(x); }\n#pragma warning(disable : 4996)\n#pragma intrinsic(_umul128)\n#endif\n\n//#include <atcoder/all>\n//using namespace atcoder;\nusing namespace std;\n\n//---------- 多倍長関連 ----------\n//#include <boost/multiprecision/cpp_int.hpp>\n//#include <boost/multiprecision/cpp_dec_float.hpp>\n//using namespace boost::multiprecision;\n\n\ntypedef long long ll;\ntypedef long double ld;\n\n#define int long long\n#define LL128 boost::multiprecision::int128_t\n#define LL boost::multiprecision::cpp_int\n#define LD100 boost::multiprecision::cpp_dec_float_100\n#define LD50 boost::multiprecision::cpp_dec_float_50\n\n#define rep(i, n) for(long long i = 0; i < (n); ++i)\n#define REP(i, s, n) for(long long i = (s); i < (n); ++i)\n#define rrep(i, n) for(long long i = (n) - 1; i >= 0; --i)\n#define sqrt(d) pow((ld) (d), 0.50)\n#define PII pair<int, int>\n#define MP make_pair\n#define PB push_back\n#define ALL(v) v.begin(), v.end()\n\nconstexpr int INF2 = std::numeric_limits<int>::max() / 2 - 10000000;\nconstexpr long long INF = std::numeric_limits<long long>::max() / 2 - 10000000;\nconst ld pi = acos(-1);\n\n//constexpr int MOD = 1000000007; //1e9 + 7\nconstexpr int MOD = 998244353; // 7 * 17 * 2^23 + 1\n\n\n\n\n//---------- chmax, min 関連 ---------- \ntemplate<class T> inline void chmax(T& a, T b) {\n\tif (a < b) a = b;\n}\ntemplate<class T> inline void chmin(T& a, T b) {\n\tif (a > b) a = b;\n}\n\n\n\n\n//---------- gcd, lcm ---------- \ntemplate<typename T = long long>\nT my_gcd(T a, T b) {\n\tif (b == (T)0) return a;\n\treturn my_gcd<T>(b, a % b);\n}\n\ntemplate<typename T = long long>\nT my_lcm(T a, T b) {\n\treturn a / my_gcd<T>(a, b) * b;\n}\n\n\n\n\n// ax + by = gcd(a, b) を解く。返り値は、gcd(a, b)。\n// 但し、a, b が負である場合は、返り値が正であることは保障されない。\nlong long my_gcd_ext(long long a, long long b, long long& x, long long& y) {\n\tif (b == 0) {\n\t\tx = 1; y = 0;\n\t\treturn a;\n\t}\n\n\tlong long tempo = my_gcd_ext(b, a % b, y, x);\n\n\t//bx' + ry' = gcd(a, b) → (qb + r)x + by = gcd(a, b) に戻さないといけない。// (r = a % b)\n\t//b(x' - qy') + (bq + r)y' = gcd(a, b) と同値変形できるから、\n\t// x = y', y = x' - qy'\n\ty -= (a / b) * x;\n\n\treturn tempo;\n}\n\n\n\n\n//中国式剰余の定理 (CRT)\n// x = base1 (mod m1) かつ x = base2 (mod m2) を解く。\n// リターン値を (r, m) とすると解は x = r (mod m) で、m = lcm(m1, m2)\n// 解なしの場合は (0, -1) をリターン\npair<long long, long long> CRT(long long base1, long long m1, long long base2, long long m2) {\n\tlong long p, q;\n\tlong long gcd0 = my_gcd_ext(m1, m2, p, q);\n\tif ((base2 - base1) % gcd0 != 0) return make_pair(0, -1);\n\n\tlong long lcm0 = m1 * (m2 / gcd0); // 括弧がないとオーバーフローのリスクがある。\n\n\tp *= (base2 - base1) / gcd0;\n\tp %= (m2 / gcd0);\n\n\t//q *= (base2 - base1) / gcd0;\n\t//q %= (m1 / gcd0);\n\n\tlong long r = (base1 + m1 * p) % lcm0;\n\tif (r < 0) r += lcm0;\n\n\treturn make_pair(r, lcm0);\n}\n\n\n\n\n//M を法として、a の逆元を返す。但し gcd(a, M) = 1。\nlong long my_invmod(long long a, long long M) {\n\tlong long x = 0, y = 0;\n\tlong long memo = my_gcd_ext(a, M, x, y);\n\tassert(memo == 1LL);\n\tx %= M;\n\tif (x < 0) x += M;\n\treturn x;\n}\n\n\n\n\n//繰り返し2乗法 (非再帰)\n//N^aの、Mで割った余りを求める。\ntemplate<typename T = long long>\nconstexpr T my_pow(T N, long long a, long long M) {\n\tassert(0 <= a);\n\tT x = N % M, res = (T)1;\n\twhile (a) {\n\t\tif (a & 1) {\n\t\t\tres *= x;\n\t\t\tres %= M;\n\t\t}\n\t\tx *= x; // x は *this の (2のべき乗) 乗を管理する。\n\t\tx %= M;\n\t\ta >>= 1;\n\t}\n\treturn res;\n}\n\n// 繰り返し2乗法 (非再帰)\n// T = modint でも動く。\ntemplate<typename T = long long>\nconstexpr T my_pow(T N, long long a) {\n\tassert(0 <= a);\n\tT x = N, res = (T)1;\n\twhile (a) {\n\t\tif (a & 1) res *= x;\n\t\tx *= x; // x は *this の (2のべき乗) 乗を管理する。\n\t\ta >>= 1;\n\t}\n\treturn res;\n}\n\n\n\n\n// base を底としたときの、n の i桁目を、v.at(i) に入れる。\nvector<signed> ll_to_vector(signed base, long long n) {\n\tlong long tempo = n;\n\tlong long tempo2 = n; //桁数を求めるときに使う\n\n\tsigned n_digit = 1;\n\twhile (tempo2 >= base) {\n\t\ttempo2 /= base;\n\t\tn_digit++;\n\t}\n\n\tvector<signed> v(n_digit, 0); // v のサイズを適切に調整。\n\tlong long denominator = my_pow<long long>((long long)base, (long long)(n_digit - 1));\n\n\tfor (signed i = 0; i < n_digit; i++) {\n\t\tv.at(i) = tempo / denominator;\n\t\ttempo -= v.at(i) * denominator;\n\n\t\tdenominator /= base;\n\t}\n\n\treturn v;\n}\n\n\n// M 桁に足りない場合、0 を追加して強制的に M 桁にする。\nvector<signed> ll_to_vector(signed base, long long n, int M) {\n\tvector<signed> v = ll_to_vector(base, n);\n\t//assert((int)v.size() <= M);\n\n\tif ((int)v.size() >= M) return v;\n\telse {\n\t\tint diff = M - v.size();\n\t\tvector<signed> res(diff, 0);\n\t\tfor (int i = 0; i < (int)v.size(); i++) res.emplace_back(v.at(i));\n\t\treturn res;\n\t}\n}\n\n\n\n\n//エラトステネスの篩で、prime で ないところに false を入れる。O(n loglog n)\n// T = int (defalt, sieve が ll で間に合うことはないので。)\n// vector<char> に替えるとむしろ遅くなる。\ntemplate<typename T = int>\nvector<bool> sieve_bool(T N) {\n\tvector<bool> res(N + 1, true);\n\tres.at(0) = false;\n\tres.at(1) = false;\n\n\tfor (T i = 2; 2 * i <= N; i++) {\n\t\tres.at(2 * i) = false;\n\t}\n\n\tfor (T i = 3; i * i <= N; i += 2) {\n\t\t//ここからは奇数のみ探索。i の倍数に false を入れる。\n\t\tif (res.at(i)) {\n\t\t\tT j = i * i; // i^2 未満の i の倍数には、すでに false が入っているはず。\n\t\t\twhile (j <= N) {\n\t\t\t\tres.at(j) = false;\n\t\t\t\tj += 2 * i;\n\t\t\t}\n\t\t}\n\t}\n\n\treturn res;\n};\n\n\n\n\n// n + 1 の サイズの vector を返す。res.at(i) には、i の 1 以外で最小の約数を入れる。\n// res.at(i) == i で、i != 0, 1 なら i は素数。\n// 2e8 なら、2.3 ~ 2.4 sec 程度で終わる。sieve_bool は 0.7 sec なので、3 倍強遅い。ll にすると、3.2 sec に伸びてしまう。\n// T = int (defalt, sieve が ll で間に合うことはないので。)\ntemplate<typename T = int>\nvector<T> sieve(T n) {\n\tn++; // n まで判定する。配列サイズは +1。\n\n\tvector<T> res(n, 0);\n\tfor (T i = 1; i < n; i++) {\n\t\tif (i % 2 == 0) res.at(i) = 2; // 偶数をあらかじめ処理。\n\t\telse res.at(i) = i; // 奇数には自分自身を入れる。\n\t}\n\n\tfor (T i = 3; i * i < n; i += 2) {\n\t\t//ここからは奇数のみ探索。i の倍数に i を入れる。\n\t\tif (res.at(i) == i) {\n\t\t\tT j = i * i; // i^2 未満の i の倍数には、すでに最小の約数が入っているはず。\n\t\t\twhile (j < n) {\n\t\t\t\tif (res.at(j) == j) res.at(j) = i;\n\t\t\t\tj += 2 * i;\n\t\t\t}\n\t\t}\n\t}\n\n\treturn res;\n};\n\n\n\n\n//O (sqrt(n)) で素数判定する用。\nconstexpr bool is_prime(long long N) {\n\t//有名素数\n\tif (N == 1000000007 || N == 1000000009) return true;\n\tif (N == 998244353 || N == 167772161 || N == 469762049 || N == 1224736769) return true; //g = 3;\n\tif (N == 924844033 || N == 1012924417) return true; //g = 5;\n\tif (N == 163577857) return true; //g = 23;\n\n\t//小さい素数の別処理\n\tif (N <= 1) return false;\n\tif (N == 2 || N == 3) return true;\n\tif (N % 2 == 0) return false;\n\tif (N % 3 == 0) return false;\n\n\tfor (long long i = 1; (6 * i + 1) * (6 * i + 1) <= N; ++i) {\n\t\tif (N % (6 * i + 1) == 0) return false;\n\t}\n\tfor (long long i = 0; (6 * i + 5) * (6 * i + 5) <= N; ++i) {\n\t\tif (N % (6 * i + 5) == 0) return false;\n\t}\n\treturn true;\n}\n\ntemplate <int n> constexpr bool is_prime_constexpr = is_prime(n);\n\n\n\n\n// 素因分解アルゴリズム (O(sqrt(N)) → O(N^0.25) のρ法も持っている。\n// T = long long (defalt)\ntemplate<typename T = long long>\nmap<T, T> PrimeFactor(T N) {\n\tmap<T, T> res;\n\n\tT i = 2;\n\twhile (i * i <= N) {\n\t\twhile (N % i == 0) {\n\t\t\tres[i]++;\n\t\t\tN /= i;\n\t\t}\n\n\t\ti += 1 + (i % 2); //i == 2 の場合だけ +1, その他の場合は +2\n\t}\n\n\tif (N > 1) res[N]++; //sqrt((元の N)) より大きな素因数は高々1つしかない。\n\treturn res;\n}\n\n\n\n\n//関数 sieve で得た、vector min_factor を持ってるときに、素因数分解を高速で行うための関数。\n// T = int (defalt, sieve が ll で間に合うことはないので。)\ntemplate<typename T = int>\nmap<T, T> PrimeFactor2(T target, vector<T>& min_factor) {\n\tmap<T, T> res;\n\tif (min_factor.empty() || (T)min_factor.size() - 1 < target) min_factor = sieve<T>(target);\n\n\twhile (target > 1) {\n\t\tres[min_factor[target]]++;\n\t\ttarget /= min_factor[target];\n\t}\n\n\treturn res;\n}\n\n\n\n\n//約数全列挙を O(sqrt(N)) で行うための関数。\nvector<long long> count_dividers(long long target) {\n\n\tvector <long long> dividers, tempo;\n\tlong long i = 1;\n\twhile (i * i < target + 1) {\n\t\tif (target % i == 0) {\n\t\t\tdividers.push_back(i);\n\t\t\tif (i < target / i) tempo.push_back(target / i); // if節がないと、平方数の時、sqrt(target) がダブルカウントされる。\n\t\t}\n\t\ti++;\n\t}\n\n\tfor (long long j = 0; j < (long long)tempo.size(); j++) {\n\t\tdividers.push_back(tempo.at(tempo.size() - 1 - j));\n\t}\n\n\treturn dividers;\n}\n\n\n\n\n//関数 sieve で得た、vector min_factor を持ってるときに、約数全列挙を高速で行うための関数。\n// T = int (defalt, sieve が ll で間に合うことはないので。)\ntemplate<typename T = int>\nvector<T> count_dividers2(T target, vector<T>& min_factor, bool is_sort = false) {\n\n\tvector<T> dividers = { 1 };\n\tmap<T, T> memo = PrimeFactor2<T>(target, min_factor);\n\n\tfor (auto&& iter = memo.begin(); iter != memo.end(); iter++) {\n\t\tvector <T> tempo = dividers;\n\t\tfor (T k = 0; k < (T)tempo.size(); k++) {\n\t\t\tT times = 1;\n\t\t\tfor (T j = 1; j <= (iter->second); j++) {\n\t\t\t\ttimes *= iter->first;\n\t\t\t\tdividers.push_back(tempo[k] * times);\n\t\t\t}\n\t\t}\n\t}\n\n\tif (is_sort) sort(dividers.begin(), dividers.end()); //sortしないと小さい順に並ばないが、必要ないなら消しても良い。\n\treturn dividers;\n}\n\n\n\n\nclass UnionFind {\npublic:\n\tvector<int> parent;\n\tvector<int> rank;\n\tvector<int> v_size;\n\n\tUnionFind(int N) : parent(N), rank(N, 0), v_size(N, 1) {\n\t\trep(i, N) {\n\t\t\tparent[i] = i;\n\t\t}\n\t}\n\n\tint root(int x) {\n\t\tif (parent[x] == x) return x;\n\t\treturn parent[x] = root(parent[x]); //経路圧縮\n\t}\n\n\tvoid unite(int x, int y) {\n\t\tint rx = root(x);\n\t\tint ry = root(y);\n\n\t\tif (rx == ry) return; //xの根とyの根が同じなので、何もしない。\n\t\tif (rank[rx] < rank[ry]) {\n\t\t\tparent[rx] = ry;\n\t\t\tv_size[ry] += v_size[rx];\n\t\t}\n\t\telse {\n\t\t\tparent[ry] = rx;\n\t\t\tv_size[rx] += v_size[ry];\n\t\t\tif (rank[rx] == rank[ry]) rank[rx]++;\n\t\t}\n\t}\n\n\tbool same(int x, int y) {\n\t\treturn (root(x) == root(y));\n\t}\n\n\tint count_tree() {\n\t\tint N = parent.size();\n\t\tint res = 0;\n\n\t\trep(i, N) {\n\t\t\tif (root(i) == i) res++;\n\t\t}\n\n\t\treturn res;\n\t}\n\n\tint size(int x) {\n\t\treturn v_size[root(x)];\n\t}\n};\n\n\n\n\n// 幾何。二点間距離。\nld calc_dist(int x1, int y1, int x2, int y2) {\n\tint tempo = (x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2);\n\tld res = sqrt((ld)tempo);\n\treturn res;\n}\n\n\n\n\n//ランレングス圧縮\nvector<pair<int, char>> RunLength(const string& S) {\n\tint N = S.size();\n\tvector<pair<int, char>> memo;\n\n\tif (N == 1) {\n\t\tmemo.push_back(MP(1, S.at(0)));\n\t\treturn memo;\n\t}\n\n\tint tempo = 1;\n\tfor (int i = 1; i < N; i++) {\n\t\tif (i != N - 1) {\n\t\t\tif (S.at(i) == S.at(i - 1)) tempo++;\n\t\t\telse {\n\t\t\t\tmemo.push_back(MP(tempo, S.at(i - 1)));\n\t\t\t\ttempo = 1;\n\t\t\t}\n\t\t}\n\t\telse {\n\t\t\tif (S.at(i) == S.at(i - 1)) {\n\t\t\t\ttempo++;\n\t\t\t\tmemo.push_back(MP(tempo, S.at(i - 1)));\n\t\t\t}\n\t\t\telse {\n\t\t\t\tmemo.push_back(MP(tempo, S.at(i - 1)));\n\t\t\t\tmemo.push_back(MP(1, S.at(i)));\n\t\t\t}\n\t\t}\n\t}\n\n\treturn memo;\n}\n\n\n\n\nvoid printf_ld(ld res) {\n\tprintf(\"%.12Lf\\n\", res);\n\t//cout << std::fixed << std::setprecision(12) << res << endl;\n}\n\ntemplate<typename T = long long>\nvoid print_vec(vector<T> v) {\n\tint N = v.size();\n\trep(i, N) {\n\t\tif (i != N - 1) cout << v.at(i) << \" \";\n\t\telse cout << v.at(i) << endl;\n\t}\n}\n\ntemplate<typename T = long long>\nvoid print_vec(deque<T> v) {\n\tint N = v.size();\n\trep(i, N) {\n\t\tif (i != N - 1) cout << v.at(i) << \" \";\n\t\telse cout << v.at(i) << endl;\n\t}\n}\n\n\n\n\n//mint 構造体。自動で mod を取る。\n//m はコンパイル時に決まる定数である必要があるので、入力を用いることはできない。\n//割り算に m の素数判定が必要になり、is_prime に依存するようになった。\n//※ constexpr 関数の const 修飾は C++11 では許されない。\ntemplate<int m, typename T = long long> class mint {\nprivate:\n\tT _val;\npublic:\n\t//---------- コンストラクタ ----------\n\tconstexpr mint(T v = 0LL) noexcept : _val(v% m) {\n\t\tif (_val < 0) _val += m;\n\t}\n\n\tconstexpr T val() const noexcept {\n\t\treturn _val;\n\t}\n\n\t//------------------------------ 二項演算子のオーバーロード ------------------------------\n\tconstexpr mint& operator += (const mint& r) noexcept {\n\t\t_val += r._val;\n\t\tif (_val >= m) _val -= m;\n\t\treturn *this;\n\t}\n\tconstexpr mint& operator -= (const mint& r) noexcept {\n\t\t_val -= r._val;\n\t\tif (_val < 0) _val += m;\n\t\treturn *this;\n\t}\n\tconstexpr mint& operator *= (const mint& r) noexcept {\n\t\t_val *= r._val; _val %= m;\n\t\treturn *this;\n\t}\n\tconstexpr mint& operator /= (const mint& r) noexcept {\n\t\tif (!prime) {\n\t\t\t//a * u + b * v = 1 を互除法で解く。但し、gcd(a, m) == 1 でなければならない。\n\t\t\tT a = r._val, b = m, u = 1, v = 0;\n\t\t\twhile (b) {\n\t\t\t\tT q = a / b;\n\t\t\t\ta -= q * b; swap(a, b); //互除法。余りをとって swap。\n\t\t\t\tu -= q * v; swap(u, v);\n\t\t\t}\n\t\t\t//assert(a == 1); //gcd(r._val, m) == 1;\n\t\t\t_val *= u; _val %= m;\n\t\t\tif (_val < 0) _val += m;\n\t\t}\n\t\telse {\n\t\t\t//フェルマーの小定理。底が prime である場合のみ使用可能。\n\t\t\t*this *= r.modpow(m - 2);\n\t\t}\n\t\treturn *this;\n\t}\n\n\tconstexpr mint operator + (const mint& r) const noexcept { return mint(*this) += r; }\n\tconstexpr mint operator - (const mint& r) const noexcept { return mint(*this) -= r; }\n\tconstexpr mint operator * (const mint& r) const noexcept { return mint(*this) *= r; }\n\tconstexpr mint operator / (const mint& r) const noexcept { return mint(*this) /= r; }\n\n\tconstexpr bool operator == (const mint& r) const noexcept {\n\t\treturn this->_val == r._val;\n\t}\n\tconstexpr bool operator != (const mint& r) const noexcept {\n\t\treturn this->_val != r._val;\n\t}\n\n\t//------------------------------ 単項演算子のオーバーロード ------------------------------\n\t//---------- 前置インクリメントのオーバーロード ----------\n\tconstexpr mint operator ++() noexcept { this->_val++; if (this->_val == m) this->_val = 0; return mint(*this); }\n\tconstexpr mint operator --() noexcept { if (this->_val == 0) this->_val = m; this->_val--; return mint(*this); }\n\t//---------- 後置インクリメントのオーバーロード ----------\n\tconstexpr mint operator++(signed) noexcept { mint temp(_val); ++_val; if (_val == m) _val = 0; return temp; }\n\tconstexpr mint operator--(signed) noexcept { mint temp(_val); if (_val == 0) _val = m; --_val; return temp; }\n\n\tconstexpr mint operator -() const noexcept { return mint(-_val); }\n\n\t//---------- 入出力のオーバーロード ----------\n\tfriend constexpr ostream& operator << (ostream& os, const mint<m, T>& x) noexcept {\n\t\treturn os << x._val;\n\t}\n\tfriend istream& operator >> (istream& is, mint<m, T>& x) noexcept {\n\t\tT init_val;\n\t\tis >> init_val;\n\t\tx = mint<m, T>(init_val);\n\t\treturn is;\n\t}\n\n\t//---------- 逆元 ----------\n\tconstexpr mint<m, T> inverse() const noexcept {\n\t\tmint<m, T> e(1);\n\t\treturn e / (*this);\n\t}\n\nprivate:\n\t// 愚直な O(sqrt(m)) の素数判定; 余りに m が大きすぎると、コンパイル時の定数式の評価に失敗するが、1e11 程度までなら大丈夫。\n\t// Miller-Rabin を使ってもよい。\n\tstatic constexpr bool prime = is_prime_constexpr<m>;\n\n\n\t//---------- 繰り返し二乗法 ----------\n\tconstexpr mint<m, T> modpow(long long n) const noexcept {\n\t\tassert(0 <= n);\n\t\tmint<m, T> x = *this, r = 1;\n\t\twhile (n) {\n\t\t\tif (n & 1) r *= x;\n\t\t\tx *= x; // x は *this の (2のべき乗) 乗を管理する。\n\t\t\tn >>= 1;\n\t\t}\n\t\treturn r;\n\t}\n};\n\nusing modint = mint<MOD, long long>;\n\n\n\n\nvector<modint> dp_fac;\nvector<modint> dp_fac_inv;\n\n// x!まで計算するときに最初に呼び出す。o(x).\ntemplate<typename T = modint>\nvoid fac_initialize(int x, vector<T>& dp = dp_fac, vector<T>& dp_inv = dp_fac_inv) {\n\tif ((int)dp.size() <= x) {\n\t\tint n = dp.size(); if (n == 0) ++n;\n\t\tdp.resize(x + 1, (T)1);\n\t\tfor (int i = n; i <= x; ++i) {\n\t\t\tdp.at(i) = dp.at(i - 1) * i;\n\t\t}\n\t}\n\n\tif ((int)dp_inv.size() <= x) {\n\t\tint n = dp_inv.size();\n\t\tdp_inv.resize(x + 1, (T)1);\n\t\tdp_inv.at(x) /= dp.at(x);\n\t\tfor (int i = x - 1; i >= n; --i) {\n\t\t\tdp_inv.at(i) = dp_inv.at(i + 1) * (i + 1);\n\t\t}\n\t}\n}\n\n// 階乗。x ! まで計算する。結果は dp (デフォルトで dp_fac<modint>) に保存する。\n// long long にするためには、第二引数に vector<long long> を指定する必要がある。20 ! = 2.43e18 まで long long に入る。\ntemplate<typename T = modint>\nT factorial(int x, vector<T>& dp = dp_fac) {\n\tassert(x >= 0);\n\n\t//既に計算済み\n\tif ((int)dp.size() > x) {\n\t\treturn dp.at(x);\n\t}\n\n\tint n = dp.size();\n\t//dp サイズを x + 1 に伸ばす。\n\tfor (int i = n; i < x + 1; i++) {\n\t\tif (i == 0) dp.push_back((T)1);\n\t\telse dp.push_back(dp.back() * i);\n\t}\n\n\treturn dp.at(x);\n}\n\ntemplate<typename T = modint>\nT factorial_inv(int x, vector<T>& dp = dp_fac_inv) {\n\tassert(x >= 0);\n\n\t//既に計算済み\n\tif ((int)dp.size() > x) {\n\t\treturn dp.at(x);\n\t}\n\n\tint n = dp.size();\n\t//dp サイズを x + 1 に伸ばす。\n\tfor (int i = n; i < x + 1; i++) {\n\t\tif (i == 0) dp.push_back((T)1);\n\t\telse dp.push_back(dp.back() / i);\n\t}\n\n\treturn dp.at(x);\n}\n\n\n// 二項係数 N_C_a \ntemplate<typename T = modint, typename U = int>\nT my_comb(U N, U a, vector<T>& dp = dp_fac, vector<T>& dp_inv = dp_fac_inv) {\n\tif (N < a) return (T)0;\n\n\tT ans = factorial<T>(N, dp);\n\tans *= factorial_inv<T>(a, dp_inv);\n\tans *= factorial_inv<T>(N - a, dp_inv);\n\n\treturn ans;\n}\n\n//二項係数 N_C_a (1点計算用)\ntemplate<typename T, typename U = int>\nT my_comb2(U N, U a) {\n\tif (N < a) return (T)0;\n\n\tT answer = 1;\n\tfor (U i = (U)0; i < a; i++) {\n\t\tanswer *= (N - i);\n\t\tanswer /= i + 1;\n\t}\n\n\treturn answer;\n}\n\n\n\n\nld now_clock() {\n\tld t = (ld)clock() / (ld)CLOCKS_PER_SEC;\n\treturn t;\n}\n\n\n\n\n\n\n//四近傍\nconst vector<int> vdh = { 1, -1, 0, 0 };\nconst vector<int> vdw = { 0, 0, 1, -1 };\n\n\n// グラフ構造体 (辺の重みあり)\n// add_edge, add_biedge (有向辺、無向辺の追加)\n// bfs01, dijkstra (最短路) の機能\n// route (最短経路を求める) 機能\n// cnt_route (最短路が何通りあるか求める) 機能 → ABC 021 C - 正直者の高橋くん\n// from_grid(const vector<string>& S); grid からの変換\n// Kruskal (UnionFind に依存), Prim 法での最小全域木 (MST) のコスト\n// bfsTopSort (トポロジカルソート)\n// IsClosed() 閉路の有無を判定し、閉路がある場合はその 1つを返す。(無向グラフの場合のみ)\ntemplate<typename T = long long>\nstruct edge {\n\tint to;\n\tT weight;\n\tint index; //何番目の辺か。\n\n\tconstexpr bool operator < (const edge& r) const noexcept {\n\t\tif (weight != r.weight) return (weight < r.weight);\n\t\telse return (index < r.index);\n\t}\n\tconstexpr bool operator > (const edge& r) const noexcept {\n\t\tif (weight != r.weight) return (weight > r.weight);\n\t\telse return (index > r.index);\n\t}\n};\ntemplate<typename T> bool operator== (const edge<T>& a, const edge<T>& b) { return (a.to == b.to && a.weight == b.weight && a.index == b.index); };\n\n\ntemplate<typename T = long long>\nstruct edge2 {\n\tint from;\n\tint to;\n\tT weight;\n\n\tconstexpr bool operator < (const edge2& r) const noexcept {\n\t\tif (weight != r.weight) return (weight < r.weight);\n\t\telse return (from < r.from);\n\t}\n};\n\n\ntemplate<typename T = long long>\nclass graph {\n\tint sp = -1; // 始点\npublic:\n\tvector<vector<edge<T>>> G;\n\tvector<edge2<T>> edges;\n\tvector<T> dist; // 始点からの距離\n\tvector<int> prev; // 始点から最短距離で進む際の直前の頂点\n\tvector<int> prev_edge; // 始点から最短距離で進む際の直前の辺\n\tbool closed = false; //閉路があるか否か。(無向グラフの場合のみ)\n\n\tgraph(int _n) : N(_n), uf(_n) { initialize(_n); };\n\tgraph() : N(0), uf(0) {};\n\n\n\t// G に対して、有向辺を加える。\n\tvoid add_edge(int from, int to, T weight = (T)1) {\n\t\tassert(0 <= from && from < N);\n\t\tassert(0 <= to && to < N);\n\t\tassert((T)0 <= weight);\n\n\t\tG.at(from).emplace_back(edge<T>{ to, weight, (int)edges.size() });\n\t\tedges.push_back(edge2<T>{from, to, weight});\n\n\t\t//無向辺を2回見ないための条件。\n\t\tif (from < to) {\n\t\t\tif (uf.root(from) == uf.root(to)) closed = true;\n\t\t\telse uf.unite(from, to);\n\t\t}\n\t}\n\n\n\t// G に対して、無向辺を加える。\n\tvoid add_biedge(int from, int to, T weight = (T)1) {\n\t\tadd_edge(from, to, weight);\n\t\tadd_edge(to, from, weight);\n\t}\n\n\n\t// 最短路 (01-BFS / weight が 0 or 1 のみの場合使える / 複数始点)\n\tvoid bfs01(vector<int> vs) {\n\t\tconst T ini_dist = -1;\n\t\tassert(!vs.empty());\n\n\t\tdeque<int> que;\n\t\tdist.assign(N, ini_dist);\n\t\tprev.assign(N, -1);\n\t\tprev_edge.assign(N, -1);\n\n\t\tfor (auto&& s : vs) {\n\t\t\tassert(0 <= s && s < N);\n\t\t\tque.push_front(s); dist.at(s) = 0;\n\t\t}\n\n\t\twhile (!que.empty()) {\n\t\t\tint v = que.front(); que.pop_front();\n\t\t\tfor (edge<T> e : G.at(v)) {\n\t\t\t\tint nextv = e.to;\n\n\t\t\t\t//if の 第二項が無いと、本当はcost = 0 で行けるのに、先に cost = 1 の辺を発見した場合バグる。\n\t\t\t\tif (dist.at(nextv) != ini_dist && dist.at(nextv) <= dist.at(v) + e.weight) continue;\n\t\t\t\tdist.at(nextv) = dist.at(v) + e.weight;\n\t\t\t\tif ((int)vs.size() == 1) {\n\t\t\t\t\tprev.at(nextv) = v;\n\t\t\t\t\tprev_edge.at(nextv) = e.index;\n\t\t\t\t}\n\n\t\t\t\tif (e.weight) que.push_back(nextv);\n\t\t\t\telse que.push_front(nextv);\n\t\t\t}\n\t\t}\n\t}\n\n\n\t// 最短路 (01-BFS / weight が 0 or 1 のみの場合使える / 単一始点)\n\tvoid bfs01(int s) {\n\t\tvector<int> vs = { s };\n\t\tsp = s;\n\t\tbfs01(vs);\n\t}\n\n\n\t//最短路 dijkstra (経路復元あり)\n\tvoid dijkstra(int s) {\n\t\tassert(0 <= s && s < N);\n\t\tsp = s;\n\n\t\tdist.assign(N, INF);\n\t\tprev.assign(N, -1);\n\t\tprev_edge.assign(N, -1);\n\n\t\t//first が最短距離、second が頂点番号。\n\t\tpriority_queue<pair<T, int>, vector<pair<T, int>>, greater<pair<T, int>>> que;\n\t\tdist.at(s) = (T)0; que.push(make_pair((T)0, s));\n\n\t\twhile (!que.empty()) {\n\t\t\tpair<T, int> p = que.top(); que.pop();\n\t\t\tint v = p.second;\n\t\t\tif (dist.at(v) < p.first) continue; //最短距離がすでに更新されているので無視。\n\n\t\t\tfor (int i = 0; i < (int)G.at(v).size(); i++) {\n\t\t\t\tedge<T> e = G.at(v).at(i);\n\t\t\t\tif (dist.at(e.to) > dist.at(v) + e.weight) {\n\t\t\t\t\tdist.at(e.to) = dist.at(v) + e.weight;\n\t\t\t\t\tprev.at(e.to) = v;\n\t\t\t\t\tprev_edge.at(e.to) = e.index;\n\t\t\t\t\tque.push(make_pair(dist.at(e.to), e.to));\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n\n\n\t// 始点 sp からの距離 dist が求まっている際に、sp から 頂点 v への経路を返す。\n\t// sp から v にたどりつけない場合バグる。\n\tvector<int> route(int v) {\n\t\tassert(sp != -1);\n\t\tvector<int> res = { v }; // 頂点番号\n\n\t\twhile (res.back() != sp) {\n\t\t\tint now = res.back();\n\t\t\tint next_v = prev.at(now);\n\t\t\tres.push_back(next_v);\n\t\t}\n\n\t\treverse(res.begin(), res.end());\n\t\treturn res;\n\t}\n\n\n\t// 始点 sp からの距離 dist が求まっている際に、\n\t// 始点 sp から頂点 i までの最短距離での行き方を求める。\n\ttemplate<typename U = modint>\n\tU cnt_route(int i, vector<U>& cnt, vector<bool>& seen) {\n\t\tassert(sp != -1);\n\t\tassert((int)cnt.size() == N);\n\t\tassert((int)seen.size() == N);\n\n\t\tif (seen.at(i)) return cnt.at(i);\n\t\telse if (i == sp) {\n\t\t\tseen.at(i) = true;\n\t\t\treturn cnt.at(i) = (U)1;\n\t\t}\n\t\telse {\n\t\t\tU sum = 0;\n\t\t\tfor (auto& e : G.at(i)) {\n\t\t\t\tint nextv = e.to;\n\t\t\t\tif (dist.at(i) == dist.at(nextv) + e.weight) {\n\t\t\t\t\tsum += cnt_route(nextv, cnt, seen);\n\t\t\t\t}\n\t\t\t}\n\n\t\t\tseen.at(i) = true;\n\t\t\treturn cnt.at(i) = sum;\n\t\t}\n\t}\n\n\n\n\n\n\t// grid から隣接グラフを構築。\n\tvoid from_grid(const vector<string>& S) {\n\t\tassert(!S.empty());\n\n\t\tint H = S.size();\n\t\tint W = S.at(0).size();\n\n\t\tint N = H * W;\n\t\tinitialize(N);\n\t\tfor (int h = 0; h < H; ++h) {\n\t\t\tfor (int w = 0; w < W; ++w) {\n\t\t\t\tfor (int i = 0; i < (int)vdh.size(); ++i) {\n\t\t\t\t\tint dh = vdh.at(i), dw = vdw.at(i);\n\t\t\t\t\tif (h + dh < 0 || H <= h + dh) continue;\n\t\t\t\t\tif (w + dw < 0 || W <= w + dw) continue;\n\n\t\t\t\t\tint v = h * W + w;\n\t\t\t\t\tint nextv = (h + dh) * W + (w + dw);\n\t\t\t\t\tif (S.at(h + dh).at(w + dw) == '#') { //壁\n\t\t\t\t\t\tcontinue;\n\t\t\t\t\t}\n\t\t\t\t\telse {\n\t\t\t\t\t\tadd_edge(v, nextv, 1);\n\t\t\t\t\t}\n\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n\n\n\n\n\n\t// (無向グラフの場合のみ)\n\t//---------- Prim 法によって、最小全域木 (MST, Minimum Spanning Tree) 問題を解く ----------\n\t// sp を含む連結成分の最小全域木のコストと、その頂点数 (元グラフが連結なら N になる) の pair を返す。\n\tpair<T, int> Prim(int sp = 0) {\n\t\tassert(0 <= sp && sp < N);\n\n\t\tT sum = 0;\n\t\tint marked_cnt = 0;\n\t\tvector<bool> marked(N, false);\n\n\t\tpriority_queue<edge<T>, vector<edge<T>>, greater<edge<T>>> que;\n\n\t\t// ----- ↓初期頂点の処理↓ -----\n\t\t++marked_cnt;\n\t\tmarked[sp] = true;\n\t\tfor (auto&& e : G[sp]) que.push(e);\n\t\t// ----- ↑初期頂点の処理↑ -----\n\n\t\twhile (marked_cnt < N && !que.empty()) {\n\t\t\tauto e = que.top(); que.pop();\n\t\t\tint nextv = e.to;\n\n\t\t\tif (marked[nextv]) continue;\n\t\t\t++marked_cnt;\n\t\t\tmarked[nextv] = true;\n\t\t\tfor (auto&& nexte : G[nextv]) que.push(nexte);\n\n\t\t\tsum += e.weight;\n\t\t}\n\n\t\treturn make_pair(sum, marked_cnt);\n\t}\n\n\n\t// (無向グラフの場合のみ)\n\t//---------- Kruskal 法によって、最小全域木 (MST, Minimum Spanning Tree) 問題を解く ----------\n\t//---------- UnionFindも必要 ---------- \n\t// 最小全域木のコストを返す。連結でなければ INF を返す。\n\tT Kruskal() {\n\t\tassert(edges.size() % 2 == 0);\n\t\tint E = edges.size() / 2;\n\n\t\tvector<edge2<T>> es;\n\t\tfor (int i = 0; i < (int)edges.size(); i += 2) {\n\t\t\tint v1 = edges.at(i).from;\n\t\t\tint v2 = edges.at(i).to;\n\t\t\tT w = edges.at(i).weight;\n\n\t\t\tassert(edges.at(i + 1).from == v2);\n\t\t\tassert(edges.at(i + 1).to == v1);\n\t\t\tassert(edges.at(i + 1).weight == w);\n\n\t\t\tes.push_back(edge2<T>{v1, v2, w});\n\t\t}\n\t\tstd::sort(es.begin(), es.end());\n\n\n\t\tint sum = 0;\n\t\tUnionFind tree(N);\n\n\t\trep(i, E) {\n\t\t\tedge2<T> e = es.at(i);\n\t\t\tif (!tree.same(e.from, e.to)) {\n\t\t\t\ttree.unite(e.from, e.to);\n\t\t\t\tsum += e.weight;\n\t\t\t}\n\t\t}\n\n\t\t// そもそも連結グラフだったか判定。\n\t\trep(v, N) {\n\t\t\tif (!tree.same(0, v)) return INF;\n\t\t}\n\n\t\treturn sum;\n\t}\n\n\n\n\n\n\t// 返り値.first; トポロジカルソート可能か否か。\n\t// 返り値.second; トポロジカルソート可能な場合の一例。\n\tpair<bool, vector<int>> bfsTopSort() {\n\t\tvector<int> CntIn(N, 0);\n\t\tfor (int i = 0; i < N; ++i) {\n\t\t\tfor (int j = 0; j < (int)G.at(i).size(); ++j) {\n\t\t\t\tint v = G.at(i).at(j).to;\n\t\t\t\t++CntIn.at(v);\n\t\t\t}\n\t\t}\n\n\t\tvector<int> res;\n\t\tqueue<int> que;\n\t\t//priority_queue<int, vector<int>, greater<int>> que; //辞書順になる。\n\n\t\tfor (int i = 0; i < N; ++i) {\n\t\t\tif (CntIn.at(i) == 0) {\n\t\t\t\tque.push(i);\n\t\t\t\t--CntIn.at(i);\n\t\t\t}\n\t\t}\n\n\t\twhile (!que.empty()) {\n\t\t\tint v = que.front(); que.pop();\n\t\t\t//int v = que.top(); que.pop(); //priority_que の場合\n\t\t\tres.push_back(v);\n\n\t\t\tfor (int i = 0; i < (int)G.at(v).size(); ++i) {\n\t\t\t\tint next_v = G.at(v).at(i).to;\n\n\t\t\t\tif (CntIn.at(next_v) == -1) {\n\t\t\t\t\t//トポロジカルソート失敗。\n\t\t\t\t\treturn make_pair(false, vector<int>());\n\t\t\t\t}\n\n\t\t\t\t--CntIn.at(next_v);\n\t\t\t\tif (CntIn.at(next_v) == 0) {\n\t\t\t\t\tque.push(next_v);\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\n\n\t\tif (res.size() == N) {\n\t\t\treturn make_pair(true, res);\n\t\t}\n\t\telse {\n\t\t\treturn make_pair(false, vector<int>());\n\t\t}\n\t}\n\n\n\n\n\n\t//閉路検出を行う。(無向グラフの場合)\n\t//first; 閉路があるか否かの bool\n\t//second; 閉路がある場合の一つの例。\n\tpair<bool, vector<int>> IsClosed() {\n\t\tvector<bool> seen(N, false);\n\n\t\tfor (int v = 0; v < N; ++v) {\n\t\t\tif (seen.at(v)) continue;\n\t\t\tvector<int> vec;\n\t\t\tbool flag = dfs_closed(v, -1, vec, seen);\n\n\t\t\tif (flag) {\n\t\t\t\tvector<int> res;\n\t\t\t\twhile (!vec.empty()) {\n\t\t\t\t\tres.push_back(vec.back());\n\t\t\t\t\tvec.pop_back();\n\n\t\t\t\t\tif (res.size() > 1 && res.front() == res.back()) break;\n\t\t\t\t}\n\n\t\t\t\tassert((int)res.size() > 1);\n\t\t\t\treturn make_pair(true, res);\n\t\t\t}\n\t\t}\n\n\n\t\tvector<int> vec;\n\t\treturn make_pair(false, vec);\n\t}\n\n\n\n\n\nprivate:\n\tint N;\n\tUnionFind uf; //辺つなぐ際の閉路検出に使う。\n\n\n\t// 初期化\n\tvoid initialize(int n) {\n\t\tG.assign(n, vector<edge<T>>());\n\t\tsp = -1;\n\t\tdist.assign(n, INF);\n\t\tprev.assign(n, -1);\n\t\tprev_edge.assign(n, -1);\n\t}\n\n\n\n\t//閉路検出に使う dfs (無向グラフの場合)\n\tbool dfs_closed(int v, int from, vector<int>& vec, vector<bool>& seen) {\n\t\tvec.push_back(v);\n\t\tif (seen.at(v)) return true;\n\t\telse seen.at(v) = true;\n\n\t\tfor (auto&& ed : G.at(v)) {\n\t\t\tint next_v = ed.to;\n\t\t\tif (next_v == from) continue;\n\n\t\t\tbool flag = dfs_closed(next_v, v, vec, seen);\n\t\t\tif (flag) return true;\n\t\t}\n\n\t\tvec.pop_back();\n\t\treturn false;\n\t}\n};\n\n\n\n\n\nsigned main() {\n\n\tvector<int> vres;\n\twhile (true) {\n\t\tint N, M;\n\t\tcin >> N >> M;\n\t\tif (N == 0 && M == 0) break;\n\n\t\tvector<int> a(M), b(M), w(M);\n\t\trep(i, M) {\n\t\t\tcin >> a.at(i) >> b.at(i) >> w.at(i);\n\t\t\t--a.at(i); --b.at(i);\n\t\t}\n\n\n\t\tgraph<int> G(N);\n\t\trep(i, M) G.add_biedge(a.at(i), b.at(i), w.at(i));\n\n\n\t\tvector<edge2<int>> es;\n\t\tfor (int i = 0; i < (int)G.edges.size(); i += 2) {\n\t\t\tint v1 = G.edges.at(i).from;\n\t\t\tint v2 = G.edges.at(i).to;\n\t\t\tint w = G.edges.at(i).weight;\n\n\n\t\t\tes.push_back(edge2<int>{v1, v2, w});\n\t\t}\n\t\tstd::sort(es.begin(), es.end());\n\n\n\t\tint res = INF;\n\t\trrep(i, (int)es.size()) {\n\t\t\tUnionFind tree(N);\n\t\t\tint tmp = 0;\n\t\t\ttree.unite(es.at(i).from, es.at(i).to);\n\n\t\t\trrep(j, i) {\n\t\t\t\tedge2<int> e = es.at(j);\n\t\t\t\tif (!tree.same(e.from, e.to)) {\n\t\t\t\t\ttree.unite(e.from, e.to);\n\t\t\t\t\ttmp = es.at(i).weight - es.at(j).weight;\n\t\t\t\t}\n\t\t\t}\n\n\t\t\trep(v, N) {\n\t\t\t\tif (!tree.same(0, v)) {\n\t\t\t\t\ttmp = INF;\n\t\t\t\t\tbreak;\n\t\t\t\t}\n\t\t\t}\n\n\t\t\tchmin(res, tmp);\n\t\t}\n\n\t\tif (res != INF) vres.push_back(res);\n\t\telse vres.push_back(-1);\n\t}\n\n\trep(i, (int)vres.size()) cout << vres.at(i) << endl;\n}", "accuracy": 1, "time_ms": 90, "memory_kb": 3972, "score_of_the_acc": -1.0374, "final_rank": 19 }, { "submission_id": "aoj_1280_6714076", "code_snippet": "#include <bits/stdc++.h>\n//#include <atcoder/all>\n//using namespace atcoder;\nusing namespace std;\nusing ll = long long;\nusing vll = vector<ll>;\nusing vvll = vector<vll>;\nusing vvvll = vector<vvll>;\nusing vb = vector<bool>;\nusing vvb = vector<vb>;\nusing vvvb = vector<vvb>;\nusing vd = vector<double>;\nusing vvd = vector<vd>;\nusing vvvd = vector<vvd>;\n#define all(A) A.begin(),A.end()\n#define rep(i, n) for (ll i = 0; i < (ll) (n); i++)\nusing pqr = priority_queue<pair<ll, ll>, vector<pair<ll, ll>>, greater<pair<ll, ll>>>;\n\ntemplate<class T>\nbool chmax(T& p, T q) {\n if (p < q) {\n p = q;\n return 1;\n }\n else {\n return 0;\n }\n}\ntemplate<class T>\nbool chmin(T& p, T q) {\n if (p > q) {\n p = q;\n return 1;\n }\n else {\n return 0;\n }\n}\n\nll gcd(ll(a), ll(b)) {\n ll c = a;\n while (a % b != 0) {\n c = a % b;\n a = b;\n b = c;\n }\n return b;\n}\nll modPow(long long a, long long n, long long p) {\n if (n == 0) return 1; // 0乗にも対応する場合\n if (n == 1) return a % p;\n if (n % 2 == 1) return (a * modPow(a, n - 1, p)) % p;\n long long t = modPow(a, n / 2, p);\n return (t * t) % p;\n}\nstruct dsu {\npublic:\n dsu() : _n(0) {}\n dsu(int n) : _n(n), par_size(n, -1) {}\n\n int merge(int a, int b) {\n int x = leader(a), y = leader(b);\n if (x == y) return x;\n if (-par_size[x] < -par_size[y]) swap(x, y);\n par_size[x] += par_size[y];\n par_size[y] = x;\n return x;\n }\n\n bool same(int a, int b) {\n return leader(a) == leader(b);\n }\n\n int leader(int a) {\n if (par_size[a] < 0) return a;\n return par_size[a] = leader(par_size[a]);\n }\n\n int size(int a) {\n return -par_size[leader(a)];\n }\n\n vector<vector<int>> groups() {\n std::vector<int> leader_buf(_n), gr_size(_n);\n for (int i = 0; i < _n; i++) {\n leader_buf[i] = leader(i);\n gr_size[leader_buf[i]]++;\n }\n std::vector<vector<int>> res(_n);\n for (int i = 0; i < _n; i++) {\n res[i].reserve(gr_size[i]);\n }\n for (int i = 0; i < _n; i++) {\n res[leader_buf[i]].push_back(i);\n }\n res.erase(\n remove_if(res.begin(), res.end(),\n [&](const vector<int>& v) { return v.empty(); }),\n res.end());\n return res;\n }\nprivate:\n int _n;\n vector<int> par_size;\n};\n\nint main() {\n cin.tie(nullptr);\n ios::sync_with_stdio(false);\n while (1) {\n ll N, M;\n cin >> N >> M;\n if (N == 0)return 0;\n vector<pair<ll, pair<ll, ll>>> E(M);\n rep(i, M) {\n ll U, V, W;\n cin >> U >> V >> W;\n U--; V--;\n E[i] = { W,{U,V} };\n }\n sort(all(E));\n ll an = 1e18;\n rep(i, M) {\n ll res = 0;\n\n res += E[i].first;\n ll L = i-1, R = M;\n while (R - L > 1) {\n dsu d(N);\n ll mid = (R + L) / 2;\n for (ll j = i; j <= mid; j++) {\n d.merge(E[j].second.first, E[j].second.second);\n }\n if (d.size(0) == N) {\n chmin(an, E[mid].first - E[i].first);\n R = mid;\n }\n else {\n L = mid;\n }\n }\n\n\n }\n cout << (an < 1e16 ? an : -1) << endl;\n }\n}", "accuracy": 1, "time_ms": 140, "memory_kb": 3548, "score_of_the_acc": -0.426, "final_rank": 14 }, { "submission_id": "aoj_1280_6714062", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\n#define rep(i, s, t) for (int i = (int)(s); i < (int)(t); ++i)\n#define revrep(i, t, s) for (int i = (int)(t)-1; i >= (int)(s); --i)\n#define all(x) begin(x), end(x)\ntemplate <typename T> bool chmax(T& a, const T& b) { return a < b ? (a = b, 1) : 0; }\ntemplate <typename T> bool chmin(T& a, const T& b) { return a > b ? (a = b, 1) : 0; }\n\nclass UnionFind {\npublic:\n UnionFind() = default;\n explicit UnionFind(int n) : data(n, -1) {}\n\n int find(int x) {\n if (data[x] < 0) return x;\n return data[x] = find(data[x]);\n }\n\n void unite(int x, int y) {\n x = find(x);\n y = find(y);\n if (x == y) return;\n if (data[x] > data[y]) std::swap(x, y);\n data[x] += data[y];\n data[y] = x;\n }\n\n bool same(int x, int y) {\n return find(x) == find(y);\n }\n\n int size(int x) {\n return -data[find(x)];\n }\n\nprivate:\n std::vector<int> data;\n};\n\nint main() {\n ios_base::sync_with_stdio(false);\n cin.tie(nullptr);\n cout << fixed << setprecision(15);\n\n while (true) {\n int n, m;\n cin >> n >> m;\n if (n == 0) break;\n vector<tuple<int, int, int>> edges;\n rep(i,0,m) {\n int a, b, w;\n cin >> a >> b >> w;\n --a, --b;\n edges.push_back({w, a, b});\n }\n sort(all(edges));\n int ans = 1e9;\n rep(i,0,m) {\n UnionFind uf(n);\n int mi = get<0>(edges[i]);\n int ma = 0;\n rep(j,i,m) {\n auto [w, a, b] = edges[j];\n if (!uf.same(a, b)) {\n uf.unite(a, b);\n chmax(ma, w);\n }\n }\n if (uf.size(0) == n) chmin(ans, ma-mi);\n }\n cout << (ans < 1e9 ? ans : -1) << endl;\n }\n}", "accuracy": 1, "time_ms": 70, "memory_kb": 3576, "score_of_the_acc": -0.4352, "final_rank": 16 }, { "submission_id": "aoj_1280_6380260", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\ntypedef long long int ll;\ntypedef pair<ll, ll> pint;\n#define rep(i, n) for(ll i = 0; i < (ll)n; i++)\n#define ALL(v) (v).begin(), (v).end()\nconst ll INF = 1e16;\n/* Union-Find木 */\n\nstruct UnionFind {\n private:\n vector<int> parent;\n \n public:\n UnionFind(int n) : parent(n, -1) { }\n void init(int n) { parent.assign(n, -1); }\n \n int root(int x) {\n if (parent[x] < 0) return x;\n else return parent[x] = root(parent[x]);\n }\n \n bool issame(int x, int y) {\n return root(x) == root(y);\n }\n \n void merge(int x, int y) { //親、子\n x = root(x);\n y = root(y);\n if (x == y) return;\n if (parent[x] > parent[y]) swap(x, y);\n parent[x] += parent[y]; // sizeを調整\n parent[y] = x; // 大きい木の根に小さい木をつける, yの親はx\n }\n \n int size(int x) {\n return -parent[root(x)];\n }\n \n};\n\nint main() {\n while(1) {\n ll N, M; cin >> N >> M;\n ll ans = +INF;\n if(N == 0 && M == 0) break;\n vector<pair<ll, pint>> P;\n rep(i, M) {\n ll a, b, c; cin >> a >> b >> c;\n a--; b--;\n P.push_back(pair<ll, pint>(c, pint(a, b)));\n }\n sort(ALL(P));\n rep(i, M) {\n UnionFind uf(N);\n ll MIN = +INF;\n ll MAX = -INF;\n for(ll j = i; j < M; j++) {\n if(uf.issame(P[j].second.first, P[j].second.second)) continue;\n uf.merge(P[j].second.first, P[j].second.second);\n MIN = min(MIN, P[j].first);\n MAX = max(MAX, P[j].first);\n }\n bool flag = true;\n rep(j, N) if(!uf.issame(0, j)) flag = false;\n if(flag) ans = min(ans, MAX-MIN);\n }\n if(ans == INF) cout << -1 << endl;\n else cout << ans << endl;\n }\n}", "accuracy": 1, "time_ms": 110, "memory_kb": 3488, "score_of_the_acc": -0.3222, "final_rank": 9 }, { "submission_id": "aoj_1280_6362610", "code_snippet": "#include <bits/stdc++.h>\n#define rep(i,n) for(int i = 0; i < (n); i++)\nusing namespace std;\ntypedef long long ll;\n\nclass union_find {\n private:\n vector<int> parent;\n\n public:\n union_find(int n) : parent(n, -1) {}\n bool unite(int x, int y) {\n x = root(x), y = root(y);\n if(x != y) {\n if(size(x) < size(y)) swap(x, y);\n parent[x] += parent[y];\n parent[y] = x;\n return true;\n }\n return false;\n }\n int root(int x) { return parent[x] < 0 ? x : parent[x] = root(parent[x]); }\n int size(int x) { return -parent[root(x)]; }\n bool same(int x, int y) { return root(x) == root(y); }\n};\n\nint main(){\n cin.tie(0);\n ios::sync_with_stdio(0);\n \n while(true) {\n int n,m; cin >> n >> m; if(n == 0 && m == 0) break;\n vector<tuple<int,int,int>> edges;\n rep(_,m) {\n int a,b,w; cin >> a >> b >> w; a--; b--;\n edges.push_back({a, b, w});\n }\n\n sort(edges.begin(), edges.end(), [&](auto e1, auto e2) {\n auto [a1, b1, w1] = e1;\n auto [a2, b2, w2] = e2;\n return w1 < w2;\n });\n\n int ans = 1e9;\n rep(i,m) {\n union_find uf(n);\n for(int k = i; k < m; k++) {\n auto [u, v, w] = edges[k];\n if(uf.unite(u, v) && uf.size(u) == n) {\n auto [ui, vi, wi] = edges[i];\n ans = min(ans, w - wi);\n break;\n }\n }\n }\n\n cout << (ans == 1e9 ? -1 : ans) << endl;\n }\n}", "accuracy": 1, "time_ms": 20, "memory_kb": 3584, "score_of_the_acc": -0.4238, "final_rank": 13 }, { "submission_id": "aoj_1280_6105306", "code_snippet": "/*\nhttps://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=1280\n\n実装には15分ほどかかった。各辺をコストでソートしたのち、辺iを起点に最小全域木を構成して\nその木のslimnessを計算することを全ての辺で行い、最小のslimnessを求める。\n*/\n#include <cmath>\n#include <iostream>\n#include <vector>\n#include <algorithm>\nusing namespace std;\n\nstruct UnionTree\n{\n vector<int> parents, size;\n UnionTree() {}\n UnionTree(int n)\n {\n parents.resize(n, 0);\n size.resize(n, 0);\n for (int i = 0; i < n; i++)\n {\n makeTree(i);\n }\n }\n\n void makeTree(int x)\n {\n parents[x] = x;\n size[x] = 1;\n }\n\n int findRoot(int x)\n {\n if (parents[x] != x)\n {\n parents[x] = findRoot(parents[x]);\n }\n return parents[x];\n }\n\n bool isSame(int x, int y)\n {\n return findRoot(x) == findRoot(y);\n }\n\n bool unite(int x, int y)\n {\n x = findRoot(x);\n y = findRoot(y);\n if (x == y)\n {\n return false;\n }\n else\n {\n if (size[x] > size[y])\n {\n parents[y] = x;\n size[x] += size[y];\n }\n else\n {\n parents[x] = y;\n size[y] += size[x];\n }\n return true;\n }\n }\n};\n\nstruct Edge\n{\n int u;\n int v;\n int cost;\n};\nbool comp_e(const Edge &e1, const Edge &e2) { return e1.cost < e2.cost; }\n\nint kruskal(vector<Edge> edges, int N, int M, int start)\n{\n UnionTree uft = UnionTree(N);\n int max_cost = 0;\n int min_cost = 100000;\n Edge e;\n\n for (int i = start; i < M; i++)\n {\n e = edges[i];\n if (!uft.isSame(e.u, e.v))\n {\n uft.unite(e.u, e.v);\n max_cost = max(max_cost, e.cost);\n min_cost = min(min_cost, e.cost);\n }\n }\n for (int i = 0; i < start; i++)\n {\n e = edges[i];\n if (!uft.isSame(e.u, e.v))\n {\n uft.unite(e.u, e.v);\n max_cost = max(max_cost, e.cost);\n min_cost = min(min_cost, e.cost);\n }\n }\n if (uft.size[uft.findRoot(0)] != N)\n return -1;\n else\n return max_cost - min_cost;\n}\n\nint main()\n{\n int N, M;\n int a, b, w;\n\n while (cin >> N >> M)\n {\n if (N == 0 && M == 0)\n break;\n else if (M == 0)\n cout << -1 << endl;\n else\n {\n\n vector<Edge> edges;\n for (int i = 0; i < M; i++)\n {\n cin >> a >> b >> w;\n Edge e = {a - 1, b - 1, w};\n edges.push_back(e);\n }\n sort(edges.begin(), edges.end(), comp_e);\n\n int min_slimness = 100000;\n for (int i = 0; i < M; i++)\n {\n min_slimness = min(min_slimness, kruskal(edges, N, M, i));\n if (min_slimness == -1)\n break;\n }\n\n cout << min_slimness << endl;\n }\n }\n return 0;\n}", "accuracy": 1, "time_ms": 170, "memory_kb": 3532, "score_of_the_acc": -0.4161, "final_rank": 11 }, { "submission_id": "aoj_1280_6105186", "code_snippet": "#include <cmath>\n#include <iostream>\n#include <vector>\n#include <algorithm>\nusing namespace std;\n\nstruct UnionTree\n{\n vector<int> parents, size;\n UnionTree() {}\n UnionTree(int n)\n {\n parents.resize(n, 0);\n size.resize(n, 0);\n for (int i = 0; i < n; i++)\n {\n makeTree(i);\n }\n }\n\n void makeTree(int x)\n {\n parents[x] = x;\n size[x] = 1;\n }\n\n int findRoot(int x)\n {\n if (parents[x] != x)\n {\n parents[x] = findRoot(parents[x]);\n }\n return parents[x];\n }\n\n bool isSame(int x, int y)\n {\n return findRoot(x) == findRoot(y);\n }\n\n bool unite(int x, int y)\n {\n x = findRoot(x);\n y = findRoot(y);\n if (x == y)\n {\n return false;\n }\n else\n {\n if (size[x] > size[y])\n {\n parents[y] = x;\n size[x] += size[y];\n }\n else\n {\n parents[x] = y;\n size[y] += size[x];\n }\n return true;\n }\n }\n};\n\nstruct Edge\n{\n int u;\n int v;\n int cost;\n};\nbool comp_e(const Edge &e1, const Edge &e2) { return e1.cost < e2.cost; }\n\nint kruskal(vector<Edge> edges, int N, int M, int start)\n{\n UnionTree uft = UnionTree(N);\n int max_cost = 0;\n int min_cost = 100000;\n Edge e;\n\n for (int i = start; i < M; i++)\n {\n e = edges[i];\n if (!uft.isSame(e.u, e.v))\n {\n uft.unite(e.u, e.v);\n max_cost = max(max_cost, e.cost);\n min_cost = min(min_cost, e.cost);\n }\n }\n for (int i = 0; i < start; i++)\n {\n e = edges[i];\n if (!uft.isSame(e.u, e.v))\n {\n uft.unite(e.u, e.v);\n max_cost = max(max_cost, e.cost);\n min_cost = min(min_cost, e.cost);\n }\n }\n if (uft.size[uft.findRoot(0)] != N)\n return -1;\n else\n return max_cost - min_cost;\n}\n\nint main()\n{\n int N, M;\n int a, b, w;\n\n while (cin >> N >> M)\n {\n if (N == 0 && M == 0)\n break;\n else if (M == 0)\n cout << -1 << endl;\n else\n {\n\n vector<Edge> edges;\n for (int i = 0; i < M; i++)\n {\n cin >> a >> b >> w;\n Edge e = {a - 1, b - 1, w};\n edges.push_back(e);\n }\n sort(edges.begin(), edges.end(), comp_e);\n\n int min_slimness = 100000;\n for (int i = 0; i < M; i++)\n {\n min_slimness = min(min_slimness, kruskal(edges, N, M, i));\n if (min_slimness == -1)\n break;\n }\n\n cout << min_slimness << endl;\n }\n }\n return 0;\n}", "accuracy": 1, "time_ms": 170, "memory_kb": 3596, "score_of_the_acc": -0.5119, "final_rank": 18 } ]
aoj_1282_cpp
Problem H: Bug Hunt In this problem, we consider a simple programming language that has only declarations of one- dimensional integer arrays and assignment statements. The problem is to find a bug in the given program. The syntax of this language is given in BNF as follows: where < new line > denotes a new line character (LF). Characters used in a program are alphabetical letters, decimal digits, = , [ , ] and new line characters. No other characters appear in a program. A declaration declares an array and specifies its length. Valid indices of an array of length n are integers between 0 and n - 1, inclusive. Note that the array names are case sensitive, i.e. array a and array A are different arrays. The initial value of each element in the declared array is undefined. For example, array a of length 10 and array b of length 5 are declared respectively as follows. a[10] b[5] An expression evaluates to a non-negative integer. A < number > is interpreted as a decimal integer. An < array_name > [< expression >] evaluates to the value of the < expression > -th element of the array. An assignment assigns the value denoted by the right hand side to the array element specified by the left hand side. Examples of assignments are as follows. a[0]=3 a[1]=0 a[2]=a[a[1]] a[a[0]]=a[1] A program is executed from the first line, line by line. You can assume that an array is declared once and only once before any of its element is assigned or referred to. Given a program, you are requested to find the following bugs. An index of an array is invalid. An array element that has not been assigned before is referred to in an assignment as an index of array or as the value to be assigned. You can assume that other bugs, such as syntax errors, do not appear. You can also assume that integers represented by < number > s are between 0 and 2 31 - 1 (= 2147483647), inclusive. Input The input consists of multiple datasets followed by a line which contains only a single ' . ' (period). Each dataset consists of a program also followed by a line which contains only a single ' . ' (period). A program does not exceed 1000 lines. Any line does not exceed 80 characters excluding a new line character. Output For each program in the input, you should answer the line number of the assignment in which the first bug appears. The line numbers start with 1 for each program. If the program does not have a bug, you should answer zero. The output should not contain extra characters such as spaces. Sample Input a[3] a[0]=a[1] . x[1] x[0]=x[0] . a[0] a[0]=1 . b[2] b[0]=2 b[1]=b[b[0]] b[0]=b[1] . g[2] G[10] g[0]=0 g[1]=G[0] . a[2147483647] a[0]=1 B[2] B[a[0]]=2 a[B[a[0]]]=3 a[2147483646]=a[2] . . Output for the Sample Input 2 2 2 3 4 0
[ { "submission_id": "aoj_1282_10680131", "code_snippet": "#include <bits/stdc++.h>\n#include <ext/pb_ds/assoc_container.hpp>\n#include <ext/pb_ds/tree_policy.hpp>\n#include <ext/pb_ds/tag_and_trait.hpp>\nusing namespace std;\nusing namespace __gnu_pbds;\n\n\nstruct custom_hash {\nstatic uint64_t splitmix64(uint64_t x) {\nx += 0x9e3779b97f4a7c15;\nx = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;\nx = (x ^ (x >> 27)) * 0x94d049bb133111eb;\nreturn x ^ (x >> 31);\n}\n\nsize_t operator()(uint64_t x) const {\nstatic const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count();\nreturn splitmix64(x + FIXED_RANDOM);\n}\n\nsize_t operator()(int x) const {\nreturn splitmix64(x);\n}\n\nsize_t operator()(const string& x) const {\nuint64_t hash = 0;\nfor (char c : x) {\nhash = hash * 31 + c;\n}\nreturn splitmix64(hash);\n}\n\nsize_t operator()(const pair<int, int>& p) const {\nreturn splitmix64(((uint64_t)p.first << 32) | (uint64_t)p.second);\n}\n};\n\n\ntemplate<typename T, typename S, typename Hash = custom_hash>\nusing hash_table = gp_hash_table<T, S, Hash>;\ntemplate<typename T, typename Hash = custom_hash>\nusing hash_set = gp_hash_table<T, null_type, Hash>;\n\n\nclass parser;\n\n\nstruct node {\nint symbol_id;\nstring value;\nvector<node*> values;\n\nprivate:\nconst parser* parser_ptr;\n\npublic:\nnode(int s, const parser* p = nullptr) : symbol_id(s), value(\"\"), parser_ptr(p) {}\nnode(int s, const string &v, const parser* p = nullptr) : symbol_id(s), value(v), parser_ptr(p) {}\n\n\nvoid set_parser(const parser* p) {\nparser_ptr = p;\n\nfor (auto child : values) {\nif (child) {\nchild->set_parser(p);\n}\n}\n}\n\n\nstring get_token_name() const;\n\n\nvoid add_child(node* child) {\nif (child && parser_ptr) {\nchild->set_parser(parser_ptr);\n}\nvalues.push_back(child);\n}\n};\n\n\nstruct LRItem {\nint lhs;\nvector<int> rhs;\nint dot_pos;\n\nLRItem(int l, const vector<int> &r, int d) : lhs(l), rhs(r), dot_pos(d) {}\n\nbool operator==(const LRItem &other) const {\nreturn lhs == other.lhs && rhs == other.rhs && dot_pos == other.dot_pos;\n}\n\nint next_symbol() const {\nif (dot_pos < (int)rhs.size()) return rhs[dot_pos];\nreturn -1;\n}\n\nbool is_complete() const {\nreturn dot_pos >= (int)rhs.size();\n}\n};\n\n\nstruct LRItemHash {\nsize_t operator()(const LRItem &item) const {\nsize_t h1 = custom_hash{}(item.lhs);\nsize_t h2 = 0;\nfor (int s : item.rhs) {\nh2 ^= custom_hash{}(s) + 0x9e3779b9 + (h2 << 6) + (h2 >> 2);\n}\nsize_t h3 = custom_hash{}(item.dot_pos);\nreturn h1 ^ (h2 << 1) ^ (h3 << 2);\n}\n};\n\n\nenum class ActionType { SHIFT, REDUCE, ACCEPT, ERROR };\n\nstruct Action {\nActionType type;\nint value;\n\nAction() : type(ActionType::ERROR), value(-1) {}\nAction(ActionType t, int v) : type(t), value(v) {}\n\nstring to_string() const {\nswitch (type) {\ncase ActionType::SHIFT: return \"SHIFT \" + std::to_string(value);\ncase ActionType::REDUCE: return \"REDUCE \" + std::to_string(value);\ncase ActionType::ACCEPT: return \"ACCEPT\";\ncase ActionType::ERROR: return \"ERROR\";\n}\nreturn \"UNKNOWN\";\n}\n};\n\n\nstruct Production {\nint lhs;\nvector<int> rhs;\n\nProduction(int l, const vector<int> &r) : lhs(l), rhs(r) {}\n};\n\nclass parser {\nprivate:\n\nvector<string> id_to_name;\nhash_table<string, int> name_to_id;\n\n\nvector<string> terminal_patterns;\nvector<int> terminal_order;\nhash_set<int> terminals;\nhash_set<int> nonterminals;\nvector<Production> productions;\n\n\nhash_table<int, hash_set<int>> first_sets;\nhash_table<int, hash_set<int>> follow_sets;\n\n\nvector<hash_set<LRItem, LRItemHash>> states;\nhash_table<pair<int, int>, Action> action_table;\nhash_table<pair<int, int>, int> goto_table;\n\nint start_symbol_id = -1;\nint augmented_start_id = -1;\nbool debug_mode = false;\n\n\nint get_or_create_symbol_id(const string &name) {\nauto it = name_to_id.find(name);\nif (it != name_to_id.end()) {\nreturn it->second;\n}\nint id = (int)id_to_name.size();\nid_to_name.push_back(name);\nname_to_id[name] = id;\nreturn id;\n}\n\nint get_or_create_symbol_id_const(const string &name) const {\nauto it = name_to_id.find(name);\nif (it != name_to_id.end()) {\nreturn it->second;\n}\nreturn -1;\n}\n\n\nint resolve_symbol_reference(int symbol_id) const {\nif (symbol_id == -1 || symbol_id >= (int)id_to_name.size()) return symbol_id;\nconst string &name = id_to_name[symbol_id];\nif (name.length() > 0 && name[0] == '#') {\nstring actual_name = name.substr(1);\nauto it = name_to_id.find(actual_name);\nif (it != name_to_id.end()) {\nreturn it->second;\n}\n}\nreturn symbol_id;\n}\n\nbool is_terminal_symbol(int symbol_id) const {\nint resolved_id = resolve_symbol_reference(symbol_id);\nif (resolved_id == -1) return false;\nreturn terminals.find(resolved_id) != terminals.end() && \n nonterminals.find(resolved_id) == nonterminals.end();\n}\n\nbool is_nonterminal_symbol(int symbol_id) const {\nint resolved_id = resolve_symbol_reference(symbol_id);\nif (resolved_id == -1) return false;\nreturn nonterminals.find(resolved_id) != nonterminals.end();\n}\n\n\nstring production_to_string(const Production &prod) const {\nstring result = get_symbol_name(prod.lhs) + \" ::= \";\nfor (int symbol_id : prod.rhs) {\nint resolved_id = resolve_symbol_reference(symbol_id);\nresult += get_symbol_name(resolved_id) + \" \";\n}\nreturn result;\n}\n\n\nvector<string> split_by_semicolon(const string &text) {\nvector<string> rules;\nstringstream ss(text);\nstring rule;\n\nwhile (getline(ss, rule, ';')) {\nrule.erase(0, rule.find_first_not_of(\" \\t\\r\\n\"));\nrule.erase(rule.find_last_not_of(\" \\t\\r\\n\") + 1);\n\nif (!rule.empty()) {\nrules.push_back(rule);\n}\n}\n\nreturn rules;\n}\n\n\n\nvoid parse_bnf(const string &bnf_text) {\nregex terminal_pattern(R\"(^\\s*([A-Za-z_][A-Za-z0-9_]*)\\s*=\\s*(.*?)\\s*$)\");\nregex production_pattern(R\"(^\\s*([A-Za-z_][A-Za-z0-9_]*)\\s*::=\\s*(.*?)\\s*$)\");\n\nvector<string> rules = split_by_semicolon(bnf_text);\n\n\nfor (size_t rule_idx = 0; rule_idx < rules.size(); rule_idx++) {\nconst string &rule = rules[rule_idx];\ncheck_bnf_warnings(rule, rule_idx + 1);\n}\n\n\nfor (const string &rule : rules) {\nsmatch match;\nif (regex_match(rule, match, terminal_pattern)) {\nstring name = match[1].str();\nstring pattern = match[2].str();\n\nint id = get_or_create_symbol_id(name);\nif ((int)terminal_patterns.size() <= id) {\nterminal_patterns.resize(id + 1);\n}\nterminal_patterns[id] = pattern;\nterminal_order.push_back(id);\nterminals.insert(id);\n}\n}\n\n\nfor (const string &rule : rules) {\nsmatch match;\nif (regex_match(rule, match, production_pattern)) {\nstring lhs = match[1].str();\nstring rhs_all = match[2].str();\n\nint lhs_id = get_or_create_symbol_id(lhs);\nnonterminals.insert(lhs_id);\n\nif (lhs == \"START\") {\nstart_symbol_id = lhs_id;\n}\n\nistringstream rhs_stream(rhs_all);\nstring rhs_part;\nwhile (getline(rhs_stream, rhs_part, '|')) {\nrhs_part.erase(0, rhs_part.find_first_not_of(\" \\t\"));\nrhs_part.erase(rhs_part.find_last_not_of(\" \\t\") + 1);\n\nif (!rhs_part.empty()) {\nvector<int> rhs_ids;\nistringstream symbol_stream(rhs_part);\nstring symbol;\nwhile (symbol_stream >> symbol) {\nint symbol_id = get_or_create_symbol_id(symbol);\nrhs_ids.push_back(symbol_id);\n}\nproductions.emplace_back(lhs_id, rhs_ids);\n\n\ncheck_production_warnings(lhs, rhs_part, productions.size());\n}\n}\n}\n}\n}\n\nprivate:\n\nvoid check_bnf_warnings(const string &rule, int rule_number) {\n\nint equals_count = 0;\nint define_equals_count = 0;\nbool in_escape = false;\n\nfor (size_t i = 0; i < rule.length(); i++) {\nchar c = rule[i];\n\nif (in_escape) {\nin_escape = false;\ncontinue;\n}\n\nif (c == '\\\\') {\nin_escape = true;\ncontinue;\n}\n\n\nif (i + 2 < rule.length() && rule.substr(i, 3) == \"::=\") {\ndefine_equals_count++;\ni += 2; \n} \n\nelse if (c == '=' && (i == 0 || rule[i-1] != ':') && (i + 1 >= rule.length() || rule[i+1] != '=')) {\nequals_count++;\n}\n}\n}\n\n\nvoid check_production_warnings(const string &lhs, const string &rhs, int production_number) {\n\nistringstream symbol_stream(rhs);\nstring symbol;\nvector<string> symbols_without_hash;\n\nwhile (symbol_stream >> symbol) {\nif (!symbol.empty() && symbol[0] != '#') {\n\nsymbols_without_hash.push_back(symbol);\n}\n}\n}\n\n\nbool contains_unescaped_hash(const string &text) {\nbool in_escape = false;\n\nfor (size_t i = 0; i < text.length(); i++) {\nchar c = text[i];\n\nif (in_escape) {\nin_escape = false;\ncontinue;\n}\n\nif (c == '\\\\') {\nin_escape = true;\ncontinue;\n}\n\nif (c == '#') {\nreturn true;\n}\n}\n\nreturn false;\n}\n\n\nvoid compute_first_sets() {\n\nfor (int term_id : terminals) {\nfirst_sets[term_id].insert(term_id);\n}\n\n\nfor (int nonterm_id : nonterminals) {\nfirst_sets[nonterm_id] = hash_set<int>();\n}\n\nbool changed = true;\nwhile (changed) {\nchanged = false;\n\nfor (const auto &prod : productions) {\nint lhs = prod.lhs;\nconst auto &rhs = prod.rhs;\n\nhash_set<int> first_rhs = compute_first_of_sequence(rhs);\n\nfor (int sym : first_rhs) {\nif (first_sets[lhs].find(sym) == first_sets[lhs].end()) {\nfirst_sets[lhs].insert(sym);\nchanged = true;\n}\n}\n}\n}\n}\n\n\nhash_set<int> compute_first_of_sequence(const vector<int> &sequence) {\nhash_set<int> result;\n\nif (sequence.empty()) {\nresult.insert(-1); \nreturn result;\n}\n\nfor (int i = 0; i < (int)sequence.size(); i++) {\nint symbol_id = sequence[i];\nint resolved_id = resolve_symbol_reference(symbol_id);\n\nif (is_terminal_symbol(symbol_id)) {\nresult.insert(resolved_id);\nbreak;\n} else if (is_nonterminal_symbol(symbol_id)) {\nbool has_epsilon = false;\nfor (int sym : first_sets[resolved_id]) {\nif (sym == -1) {\nhas_epsilon = true;\n} else {\nresult.insert(sym);\n}\n}\n\nif (!has_epsilon) {\nbreak;\n}\n\nif (i == (int)sequence.size() - 1) {\nresult.insert(-1); \n}\n}\n}\n\nreturn result;\n}\n\n\nvoid compute_follow_sets() {\nint eof_id = get_or_create_symbol_id(\"$\");\nfollow_sets[start_symbol_id].insert(eof_id);\n\nbool changed = true;\nwhile (changed) {\nchanged = false;\n\nfor (const auto &prod : productions) {\nfor (int i = 0; i < (int)prod.rhs.size(); i++) {\nint symbol_id = prod.rhs[i];\nint resolved_id = resolve_symbol_reference(symbol_id);\n\nif (is_nonterminal_symbol(symbol_id)) {\nvector<int> beta(prod.rhs.begin() + i + 1, prod.rhs.end());\nhash_set<int> first_beta = compute_first_of_sequence(beta);\n\nfor (int sym : first_beta) {\nif (sym != -1 && follow_sets[resolved_id].find(sym) == follow_sets[resolved_id].end()) {\nfollow_sets[resolved_id].insert(sym);\nchanged = true;\n}\n}\n\nif (first_beta.find(-1) != first_beta.end()) {\nfor (int sym : follow_sets[prod.lhs]) {\nif (follow_sets[resolved_id].find(sym) == follow_sets[resolved_id].end()) {\nfollow_sets[resolved_id].insert(sym);\nchanged = true;\n}\n}\n}\n}\n}\n}\n}\n}\n\n\nhash_set<LRItem, LRItemHash> closure(const hash_set<LRItem, LRItemHash> &items) {\nhash_set<LRItem, LRItemHash> result = items;\nbool changed = true;\n\nwhile (changed) {\nchanged = false;\nvector<LRItem> new_items;\n\nfor (const auto &item : result) {\nint next_sym = item.next_symbol();\nif (next_sym != -1 && is_nonterminal_symbol(next_sym)) {\nint resolved_id = resolve_symbol_reference(next_sym);\n\nfor (int prod_idx = 0; prod_idx < (int)productions.size(); prod_idx++) {\nconst auto &prod = productions[prod_idx];\nif (prod.lhs == resolved_id) {\nLRItem new_item(prod.lhs, prod.rhs, 0);\nif (result.find(new_item) == result.end()) {\nbool found = false;\nfor (const auto &ni : new_items) {\nif (ni == new_item) {\nfound = true;\nbreak;\n}\n}\nif (!found) {\nnew_items.push_back(new_item);\nchanged = true;\n}\n}\n}\n}\n}\n}\n\nfor (const auto &item : new_items) {\nresult.insert(item);\n}\n}\n\nreturn result;\n}\n\n\nhash_set<LRItem, LRItemHash> goto_func(const hash_set<LRItem, LRItemHash> &items, int symbol_id) {\nhash_set<LRItem, LRItemHash> goto_items;\n\nfor (const auto &item : items) {\nif (item.next_symbol() == symbol_id) {\nLRItem new_item(item.lhs, item.rhs, item.dot_pos + 1);\ngoto_items.insert(new_item);\n}\n}\n\nif (goto_items.empty()) {\nreturn goto_items;\n}\n\nreturn closure(goto_items);\n}\n\n\nbool states_equal(const hash_set<LRItem, LRItemHash> &a, const hash_set<LRItem, LRItemHash> &b) const {\nif (a.size() != b.size()) return false;\nfor (const auto &item : a) {\nif (b.find(item) == b.end()) return false;\n}\nreturn true;\n}\n\n\nint find_state_id(const hash_set<LRItem, LRItemHash> &target_state) const {\nfor (int i = 0; i < (int)states.size(); i++) {\nif (states_equal(states[i], target_state)) {\nreturn i;\n}\n}\nreturn -1;\n}\n\n\nvoid build_slr1_states() {\nif (start_symbol_id == -1) {\nthrow runtime_error(\"Start symbol 'START' not found in grammar\");\n}\n\ncompute_first_sets();\ncompute_follow_sets();\n\naugmented_start_id = get_or_create_symbol_id(\"_START\");\nproductions.insert(productions.begin(), Production(augmented_start_id, {start_symbol_id}));\nnonterminals.insert(augmented_start_id);\n\n\nhash_set<LRItem, LRItemHash> initial_items;\nLRItem initial_item(augmented_start_id, {start_symbol_id}, 0);\ninitial_items.insert(initial_item);\n\nhash_set<LRItem, LRItemHash> initial_state = closure(initial_items);\nstates.push_back(initial_state);\n\nqueue<int> worklist;\nworklist.push(0);\n\nwhile (!worklist.empty()) {\nint current_state_id = worklist.front();\nworklist.pop();\nauto current_state = states[current_state_id];\n\nhash_set<int> symbols;\nfor (const auto &item : current_state) {\nint next_sym = item.next_symbol();\nif (next_sym != -1) {\nsymbols.insert(next_sym);\n}\n}\n\nfor (int symbol_id : symbols) {\nauto next_state = goto_func(current_state, symbol_id);\nif (next_state.empty()) continue;\n\nint next_state_id = find_state_id(next_state);\nif (next_state_id == -1) {\nnext_state_id = (int)states.size();\nstates.push_back(next_state);\nworklist.push(next_state_id);\n}\n\nif (is_terminal_symbol(symbol_id)) {\nint resolved_id = resolve_symbol_reference(symbol_id);\naction_table[{current_state_id, resolved_id}] = Action(ActionType::SHIFT, next_state_id);\n} else if (is_nonterminal_symbol(symbol_id)) {\nint resolved_id = resolve_symbol_reference(symbol_id);\ngoto_table[{current_state_id, resolved_id}] = next_state_id;\n}\n}\n}\n\n\nfor (int state_id = 0; state_id < (int)states.size(); state_id++) {\nfor (const auto &item : states[state_id]) {\nif (item.is_complete()) {\nif (item.lhs == augmented_start_id) {\nint eof_id = get_or_create_symbol_id(\"$\");\naction_table[{state_id, eof_id}] = Action(ActionType::ACCEPT, 0);\n} else {\nint prod_id = find_production_id(item.lhs, item.rhs);\nif (prod_id != -1) {\n\nfor (int follow_sym : follow_sets[item.lhs]) {\npair<int, int> key = {state_id, follow_sym};\nauto existing = action_table.find(key);\n\naction_table[key] = Action(ActionType::REDUCE, prod_id);\n}\n}\n}\n}\n}\n}\n}\n\nint find_production_id(int lhs, const vector<int> &rhs) {\nfor (int i = 0; i < (int)productions.size(); i++) {\nif (productions[i].lhs == lhs && productions[i].rhs == rhs) {\nreturn i;\n}\n}\nreturn -1;\n}\n\n\nstruct Token {\nint type_id;\nstring value;\nToken(int t, const string &v) : type_id(t), value(v) {}\n};\n\nenum class TokenizeResult {\nSUCCESS,\nINVALID_REGEX,\nUNRECOGNIZED_CHARACTER\n};\n\n\nTokenizeResult tokenize(const string &input, vector<Token> &tokens) {\ntokens.clear();\ntokens.reserve(input.length() / 4);\n\n \nstruct LiteralPattern {\nint type_id;\nstring literal;\nint original_order;\n\nLiteralPattern(int id, string lit, int order) \n: type_id(id), literal(std::move(lit)), original_order(order) {}\n};\n\nvector<LiteralPattern> patterns;\npatterns.reserve(terminal_order.size() * 10); \n\nfor (size_t order = 0; order < terminal_order.size(); order++) {\nint term_id = terminal_order[order];\nif (terminals.find(term_id) != terminals.end() && term_id < (int)terminal_patterns.size()) {\nconst string& pattern = terminal_patterns[term_id];\n\n \nvector<string> expanded = expand_pattern(pattern);\n\nfor (const string& literal : expanded) {\npatterns.emplace_back(term_id, literal, order);\n}\n}\n}\n\nconst char* current_ptr = input.c_str();\nconst char* end_ptr = current_ptr + input.length();\n\nwhile (current_ptr < end_ptr) {\n \nwhile (current_ptr < end_ptr && isspace(*current_ptr)) {\n++current_ptr;\n}\n\nif (current_ptr >= end_ptr) break;\n\nbool matched = false;\nsize_t remaining_length = end_ptr - current_ptr;\n\n \nfor (const auto& pattern_info : patterns) {\nsize_t literal_len = pattern_info.literal.length();\n\nif (remaining_length >= literal_len && \nmemcmp(current_ptr, pattern_info.literal.c_str(), literal_len) == 0) {\n\ntokens.emplace_back(pattern_info.type_id, string(current_ptr, literal_len));\ncurrent_ptr += literal_len;\nmatched = true;\nbreak;\n}\n}\n\nif (!matched) {\nreturn TokenizeResult::UNRECOGNIZED_CHARACTER;\n}\n}\n\nreturn TokenizeResult::SUCCESS;\n}\n\nprivate:\n \nvector<string> expand_pattern(const string& pattern) const {\nvector<string> result;\n\n \nvector<string> alternatives = split_alternatives(pattern);\n\nfor (const string& alt : alternatives) {\n \nif (is_character_range(alt)) {\nvector<string> expanded = expand_character_range(alt);\nresult.insert(result.end(), expanded.begin(), expanded.end());\n} else {\n \nresult.push_back(unescape_literal(alt));\n}\n}\n\nreturn result;\n}\n\n \nvector<string> split_alternatives(const string& pattern) const {\nvector<string> result;\nsize_t start = 0;\nsize_t pos = 0;\n\nwhile (pos < pattern.length()) {\nif (pattern[pos] == '|') {\nif (pos > start) {\nresult.push_back(pattern.substr(start, pos - start));\n}\nstart = pos + 1;\n}\npos++;\n}\n\nif (start < pattern.length()) {\nresult.push_back(pattern.substr(start));\n}\n\nreturn result;\n}\n\n \nbool is_character_range(const string& str) const {\nif (str.length() < 5) return false; \nif (str[0] != '[' || str[str.length()-1] != ']') return false;\n\nstring inner = str.substr(1, str.length()-2);\nif (inner.length() != 3) return false; \nif (inner[1] != '-') return false;\n\nchar start_char = inner[0];\nchar end_char = inner[2];\n\nreturn (start_char <= end_char) && \n ((start_char >= 'a' && end_char <= 'z') || \n(start_char >= 'A' && end_char <= 'Z') ||\n(start_char >= '0' && end_char <= '9'));\n}\n\n \nvector<string> expand_character_range(const string& range) const {\nvector<string> result;\n\nstring inner = range.substr(1, range.length()-2);\nchar start_char = inner[0];\nchar end_char = inner[2];\n\nfor (char c = start_char; c <= end_char; c++) {\nresult.push_back(string(1, c));\n}\n\nreturn result;\n}\n\n \nstring unescape_literal(const string& pattern) const {\nstring result;\nresult.reserve(pattern.length());\n\nbool escaped = false;\nfor (char c : pattern) {\nif (escaped) {\nresult += c;\nescaped = false;\n} else if (c == '\\\\') {\nescaped = true;\n} else {\nresult += c;\n}\n}\n\nreturn result;\n}\n\npublic:\nparser(const string &bnf_text, bool debug = false) : debug_mode(debug) {\nparse_bnf(bnf_text);\nbuild_slr1_states();\n}\n\nnode* parse(const string &input) {\nvector<Token> tokens;\nTokenizeResult tokenize_result = tokenize(input, tokens);\n\nif (tokenize_result != TokenizeResult::SUCCESS) {\nreturn nullptr;\n}\n\nint eof_id = get_or_create_symbol_id(\"$\");\ntokens.emplace_back(eof_id, \"$\");\n\nvector<int> state_stack = {0};\nvector<node*> symbol_stack;\nint token_pos = 0;\n\nwhile (true) {\nif (state_stack.empty() || token_pos >= (int)tokens.size()) {\nreturn nullptr;\n}\n\nint current_state = state_stack.back();\nint current_token_type = tokens[token_pos].type_id;\nstring current_token_value = tokens[token_pos].value;\n\nauto action_key = make_pair(current_state, current_token_type);\nauto action_it = action_table.find(action_key);\n\nif (action_it == action_table.end()) {\nreturn nullptr;\n}\n\nAction action = action_it->second;\n\nif (action.type == ActionType::SHIFT) {\nstate_stack.push_back(action.value);\nnode* new_node = new node(current_token_type, current_token_value, this);\nsymbol_stack.push_back(new_node);\ntoken_pos++;\n} else if (action.type == ActionType::REDUCE) {\nif (action.value < 0 || action.value >= (int)productions.size()) {\nreturn nullptr;\n}\n\nProduction &prod = productions[action.value];\nnode* new_node = new node(prod.lhs, this);\n\nint pop_count = (int)prod.rhs.size();\nif (pop_count > (int)symbol_stack.size() || pop_count > (int)state_stack.size()) {\nreturn nullptr;\n}\n\nfor (int i = 0; i < pop_count; i++) {\nnode* child = symbol_stack.back();\nnew_node->values.insert(new_node->values.begin(), child);\nsymbol_stack.pop_back();\nstate_stack.pop_back();\n}\n\nfor (auto child : new_node->values) {\nif (child) {\nchild->set_parser(this);\n}\n}\n\nsymbol_stack.push_back(new_node);\n\nif (state_stack.empty()) {\nreturn nullptr;\n}\n\nauto goto_key = make_pair(state_stack.back(), prod.lhs);\nauto goto_it = goto_table.find(goto_key);\nif (goto_it == goto_table.end()) {\nreturn nullptr;\n}\n\nstate_stack.push_back(goto_it->second);\n} else if (action.type == ActionType::ACCEPT) {\nif (symbol_stack.empty()) {\nreturn nullptr;\n}\nnode* result = symbol_stack.back();\nif (result) {\nresult->set_parser(this);\n}\nreturn result;\n} else {\nreturn nullptr;\n}\n}\n}\n\nstring get_symbol_name(int symbol_id) const {\nif (symbol_id < 0 || symbol_id >= (int)id_to_name.size()) return \"\";\nreturn id_to_name[symbol_id];\n}\n};\n\n\nstring node::get_token_name() const {\nif (!parser_ptr) return \"\";\nreturn parser_ptr->get_symbol_name(symbol_id);\n}\n\nusing ll = long long;\nmap<string, pair<ll, map<ll, ll>>> env;\nll eval(node* root){\nif (root == nullptr) return -1;\n\nstring symbol_name = root->get_token_name();\n\nif (symbol_name == \"START\"){\nreturn eval(root->values[0]);\n}\n\nif (symbol_name == \"DECL\"){\nstring id = root->values[0]->value;\nll num = eval(root->values[2]);\nenv[id] = {num, {}};\nreturn 0;\n}\n\nif (symbol_name == \"ASSIGN\"){\nstring id1 = root->values[0]->value;\nll num1 = eval(root->values[2]);\nll num2 = eval(root->values[5]);\n\nif (env[id1].first <= num1) return -1;\nif (num1 == -1 || num2 == -1) return -1;\nenv[id1].second[num1] = num2;\nreturn 0;\n}\n\nif (symbol_name == \"EXPR\"){\nif (root->values.size() == 1) {\nreturn eval(root->values[0]);\n}\nelse {\nstring id = root->values[0]->value;\nll num = eval(root->values[2]);\nif (env.find(id) == env.end()) return -1;\nif (env[id].second.count(num) == 0) return -1;\nif (num == -1) return -1;\nreturn env[id].second[num];\n}\n}\n\nif (symbol_name == \"NUM\"){\nif (root->values.size() == 1) {\nreturn stoll(root->values[0]->value);\n}\nelse {\nreturn eval(root->values[0]) * 10 + stoll(root->values[1]->value);\n}\n}\n\nreturn -1;\n}\n\nint main() {\n\nbool debug = false;\n\nauto parser0 = parser(\n\"ID = [A-Z]|[a-z];\"\n\"DIGIT = [0-9];\"\n\"NUM ::= #NUM #DIGIT | #DIGIT;\"\n\"LPAR = \\\\[;\"\n\"RPAR = \\\\];\"\n\"EQ = \\\\=;\"\n\"START ::= #ASSIGN;\"\n\"ASSIGN ::= #ID #LPAR #EXPR #RPAR #EQ #EXPR;\"\n\"EXPR ::= #NUM | #ID #LPAR #EXPR #RPAR;\",\ndebug\n);\n\nauto parser1 = parser(\n\"ID = [A-Z]|[a-z];\"\n\"DIGIT = [0-9];\"\n\"NUM ::= #NUM #DIGIT | #DIGIT;\"\n\"LPAR = \\\\[;\"\n\"RPAR = \\\\];\"\n\"START ::= #DECL;\"\n\"DECL ::= #ID #LPAR #NUM #RPAR;\",\ndebug\n);\n\nwhile (1){\nint t = 1;\nbool f = 0;\nenv.clear();\n\nwhile(1){\nstring s;\ncin >> s;\nif (s == \".\"){\nif (t == 1) return 0;\nif (!f) cout << 0 << endl;\nbreak;\n}\nif (f) continue;\n\nif (s.find('=') == -1){\nauto result = parser1.parse(s);\nassert(result != nullptr);\neval(result);\n}\nelse {\nauto result = parser0.parse(s);\nassert(result != nullptr);\nif (eval(result) == -1){\ncout << t << endl;\nf = 1;\n}\n}\n\nt++;\n}\n}\n\nreturn 0;\n}", "accuracy": 1, "time_ms": 50, "memory_kb": 26244, "score_of_the_acc": -1.0727, "final_rank": 17 }, { "submission_id": "aoj_1282_10675687", "code_snippet": "#include <bits/stdc++.h>\n#include <ext/pb_ds/assoc_container.hpp>\n#include <ext/pb_ds/tree_policy.hpp>\n#include <ext/pb_ds/tag_and_trait.hpp>\nusing namespace std;\nusing namespace __gnu_pbds;\n\n\nstruct custom_hash {\n static uint64_t splitmix64(uint64_t x) {\n x += 0x9e3779b97f4a7c15;\n x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;\n x = (x ^ (x >> 27)) * 0x94d049bb133111eb;\n return x ^ (x >> 31);\n }\n\n size_t operator()(uint64_t x) const {\n static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count();\n return splitmix64(x + FIXED_RANDOM);\n }\n \n size_t operator()(int x) const {\n return splitmix64(x);\n }\n \n size_t operator()(const string& x) const {\n uint64_t hash = 0;\n for (char c : x) {\n hash = hash * 31 + c;\n }\n return splitmix64(hash);\n }\n \n size_t operator()(const pair<int, int>& p) const {\n return splitmix64(((uint64_t)p.first << 32) | (uint64_t)p.second);\n }\n};\n\n\ntemplate<typename T, typename S, typename Hash = custom_hash>\nusing hash_table = gp_hash_table<T, S, Hash>;\ntemplate<typename T, typename Hash = custom_hash>\nusing hash_set = gp_hash_table<T, null_type, Hash>;\n\n\nclass parser;\n\n\nstruct node {\n int symbol_id;\n string value;\n vector<node*> values;\n \nprivate:\n const parser* parser_ptr;\n \npublic:\n node(int s, const parser* p = nullptr) : symbol_id(s), value(\"\"), parser_ptr(p) {}\n node(int s, const string &v, const parser* p = nullptr) : symbol_id(s), value(v), parser_ptr(p) {}\n \n \n void set_parser(const parser* p) {\n parser_ptr = p;\n \n for (auto child : values) {\n if (child) {\n child->set_parser(p);\n }\n }\n }\n \n \n string get_token_name() const;\n \n \n void add_child(node* child) {\n if (child && parser_ptr) {\n child->set_parser(parser_ptr);\n }\n values.push_back(child);\n }\n};\n\n\nstruct LRItem {\n int lhs;\n vector<int> rhs;\n int dot_pos;\n \n LRItem(int l, const vector<int> &r, int d) : lhs(l), rhs(r), dot_pos(d) {}\n \n bool operator==(const LRItem &other) const {\n return lhs == other.lhs && rhs == other.rhs && dot_pos == other.dot_pos;\n }\n \n int next_symbol() const {\n if (dot_pos < (int)rhs.size()) return rhs[dot_pos];\n return -1;\n }\n \n bool is_complete() const {\n return dot_pos >= (int)rhs.size();\n }\n};\n\n\nstruct LRItemHash {\n size_t operator()(const LRItem &item) const {\n size_t h1 = custom_hash{}(item.lhs);\n size_t h2 = 0;\n for (int s : item.rhs) {\n h2 ^= custom_hash{}(s) + 0x9e3779b9 + (h2 << 6) + (h2 >> 2);\n }\n size_t h3 = custom_hash{}(item.dot_pos);\n return h1 ^ (h2 << 1) ^ (h3 << 2);\n }\n};\n\n\nenum class ActionType { SHIFT, REDUCE, ACCEPT, ERROR };\n\nstruct Action {\n ActionType type;\n int value;\n \n Action() : type(ActionType::ERROR), value(-1) {}\n Action(ActionType t, int v) : type(t), value(v) {}\n \n string to_string() const {\n switch (type) {\n case ActionType::SHIFT: return \"SHIFT \" + std::to_string(value);\n case ActionType::REDUCE: return \"REDUCE \" + std::to_string(value);\n case ActionType::ACCEPT: return \"ACCEPT\";\n case ActionType::ERROR: return \"ERROR\";\n }\n return \"UNKNOWN\";\n }\n};\n\n\nstruct Production {\n int lhs;\n vector<int> rhs;\n \n Production(int l, const vector<int> &r) : lhs(l), rhs(r) {}\n};\n\nclass parser {\nprivate:\n \n vector<string> id_to_name;\n hash_table<string, int> name_to_id;\n \n \n vector<string> terminal_patterns;\n vector<int> terminal_order;\n hash_set<int> terminals;\n hash_set<int> nonterminals;\n vector<Production> productions;\n \n \n hash_table<int, hash_set<int>> first_sets;\n hash_table<int, hash_set<int>> follow_sets;\n \n \n vector<hash_set<LRItem, LRItemHash>> states;\n hash_table<pair<int, int>, Action> action_table;\n hash_table<pair<int, int>, int> goto_table;\n \n int start_symbol_id = -1;\n int augmented_start_id = -1;\n bool debug_mode = false;\n \n \n int get_or_create_symbol_id(const string &name) {\n auto it = name_to_id.find(name);\n if (it != name_to_id.end()) {\n return it->second;\n }\n int id = (int)id_to_name.size();\n id_to_name.push_back(name);\n name_to_id[name] = id;\n return id;\n }\n \n int get_or_create_symbol_id_const(const string &name) const {\n auto it = name_to_id.find(name);\n if (it != name_to_id.end()) {\n return it->second;\n }\n return -1;\n }\n \n \n int resolve_symbol_reference(int symbol_id) const {\n if (symbol_id == -1 || symbol_id >= (int)id_to_name.size()) return symbol_id;\n const string &name = id_to_name[symbol_id];\n if (name.length() > 0 && name[0] == '#') {\n string actual_name = name.substr(1);\n auto it = name_to_id.find(actual_name);\n if (it != name_to_id.end()) {\n return it->second;\n }\n }\n return symbol_id;\n }\n \n bool is_terminal_symbol(int symbol_id) const {\n int resolved_id = resolve_symbol_reference(symbol_id);\n if (resolved_id == -1) return false;\n return terminals.find(resolved_id) != terminals.end() && \n nonterminals.find(resolved_id) == nonterminals.end();\n }\n \n bool is_nonterminal_symbol(int symbol_id) const {\n int resolved_id = resolve_symbol_reference(symbol_id);\n if (resolved_id == -1) return false;\n return nonterminals.find(resolved_id) != nonterminals.end();\n }\n \n \n string production_to_string(const Production &prod) const {\n string result = get_symbol_name(prod.lhs) + \" ::= \";\n for (int symbol_id : prod.rhs) {\n int resolved_id = resolve_symbol_reference(symbol_id);\n result += get_symbol_name(resolved_id) + \" \";\n }\n return result;\n }\n \n \n vector<string> split_by_semicolon(const string &text) {\n vector<string> rules;\n stringstream ss(text);\n string rule;\n \n while (getline(ss, rule, ';')) {\n rule.erase(0, rule.find_first_not_of(\" \\t\\r\\n\"));\n rule.erase(rule.find_last_not_of(\" \\t\\r\\n\") + 1);\n \n if (!rule.empty()) {\n rules.push_back(rule);\n }\n }\n \n return rules;\n }\n \n \n \nvoid parse_bnf(const string &bnf_text) {\n regex terminal_pattern(R\"(^\\s*([A-Za-z_][A-Za-z0-9_]*)\\s*=\\s*(.*?)\\s*$)\");\n regex production_pattern(R\"(^\\s*([A-Za-z_][A-Za-z0-9_]*)\\s*::=\\s*(.*?)\\s*$)\");\n \n vector<string> rules = split_by_semicolon(bnf_text);\n \n \n for (size_t rule_idx = 0; rule_idx < rules.size(); rule_idx++) {\n const string &rule = rules[rule_idx];\n check_bnf_warnings(rule, rule_idx + 1);\n }\n \n \n for (const string &rule : rules) {\n smatch match;\n if (regex_match(rule, match, terminal_pattern)) {\n string name = match[1].str();\n string pattern = match[2].str();\n \n int id = get_or_create_symbol_id(name);\n if ((int)terminal_patterns.size() <= id) {\n terminal_patterns.resize(id + 1);\n }\n terminal_patterns[id] = pattern;\n terminal_order.push_back(id);\n terminals.insert(id);\n }\n }\n \n \n for (const string &rule : rules) {\n smatch match;\n if (regex_match(rule, match, production_pattern)) {\n string lhs = match[1].str();\n string rhs_all = match[2].str();\n \n int lhs_id = get_or_create_symbol_id(lhs);\n nonterminals.insert(lhs_id);\n \n if (lhs == \"START\") {\n start_symbol_id = lhs_id;\n }\n \n istringstream rhs_stream(rhs_all);\n string rhs_part;\n while (getline(rhs_stream, rhs_part, '|')) {\n rhs_part.erase(0, rhs_part.find_first_not_of(\" \\t\"));\n rhs_part.erase(rhs_part.find_last_not_of(\" \\t\") + 1);\n \n if (!rhs_part.empty()) {\n vector<int> rhs_ids;\n istringstream symbol_stream(rhs_part);\n string symbol;\n while (symbol_stream >> symbol) {\n int symbol_id = get_or_create_symbol_id(symbol);\n rhs_ids.push_back(symbol_id);\n }\n productions.emplace_back(lhs_id, rhs_ids);\n \n \n check_production_warnings(lhs, rhs_part, productions.size());\n }\n }\n }\n }\n}\n\nprivate:\n \n void check_bnf_warnings(const string &rule, int rule_number) {\n \n int equals_count = 0;\n int define_equals_count = 0;\n bool in_escape = false;\n \n for (size_t i = 0; i < rule.length(); i++) {\n char c = rule[i];\n \n if (in_escape) {\n in_escape = false;\n continue;\n }\n \n if (c == '\\\\') {\n in_escape = true;\n continue;\n }\n \n \n if (i + 2 < rule.length() && rule.substr(i, 3) == \"::=\") {\n define_equals_count++;\n i += 2; \n } \n \n else if (c == '=' && (i == 0 || rule[i-1] != ':') && (i + 1 >= rule.length() || rule[i+1] != '=')) {\n equals_count++;\n }\n }\n \n int total_equals = equals_count + define_equals_count;\n }\n \n \n void check_production_warnings(const string &lhs, const string &rhs, int production_number) {\n \n istringstream symbol_stream(rhs);\n string symbol;\n vector<string> symbols_without_hash;\n \n while (symbol_stream >> symbol) {\n if (!symbol.empty() && symbol[0] != '#') {\n \n symbols_without_hash.push_back(symbol);\n }\n }\n }\n \n \n bool contains_unescaped_hash(const string &text) {\n bool in_escape = false;\n \n for (size_t i = 0; i < text.length(); i++) {\n char c = text[i];\n \n if (in_escape) {\n in_escape = false;\n continue;\n }\n \n if (c == '\\\\') {\n in_escape = true;\n continue;\n }\n \n if (c == '#') {\n return true;\n }\n }\n \n return false;\n }\n \n \n void compute_first_sets() {\n \n for (int term_id : terminals) {\n first_sets[term_id].insert(term_id);\n }\n \n \n for (int nonterm_id : nonterminals) {\n first_sets[nonterm_id] = hash_set<int>();\n }\n \n bool changed = true;\n while (changed) {\n changed = false;\n \n for (const auto &prod : productions) {\n int lhs = prod.lhs;\n const auto &rhs = prod.rhs;\n \n hash_set<int> first_rhs = compute_first_of_sequence(rhs);\n \n for (int sym : first_rhs) {\n if (first_sets[lhs].find(sym) == first_sets[lhs].end()) {\n first_sets[lhs].insert(sym);\n changed = true;\n }\n }\n }\n }\n }\n \n \n hash_set<int> compute_first_of_sequence(const vector<int> &sequence) {\n hash_set<int> result;\n \n if (sequence.empty()) {\n result.insert(-1); \n return result;\n }\n \n for (int i = 0; i < (int)sequence.size(); i++) {\n int symbol_id = sequence[i];\n int resolved_id = resolve_symbol_reference(symbol_id);\n \n if (is_terminal_symbol(symbol_id)) {\n result.insert(resolved_id);\n break;\n } else if (is_nonterminal_symbol(symbol_id)) {\n bool has_epsilon = false;\n for (int sym : first_sets[resolved_id]) {\n if (sym == -1) {\n has_epsilon = true;\n } else {\n result.insert(sym);\n }\n }\n \n if (!has_epsilon) {\n break;\n }\n \n if (i == (int)sequence.size() - 1) {\n result.insert(-1); \n }\n }\n }\n \n return result;\n }\n \n \n void compute_follow_sets() {\n int eof_id = get_or_create_symbol_id(\"$\");\n follow_sets[start_symbol_id].insert(eof_id);\n \n bool changed = true;\n while (changed) {\n changed = false;\n \n for (const auto &prod : productions) {\n for (int i = 0; i < (int)prod.rhs.size(); i++) {\n int symbol_id = prod.rhs[i];\n int resolved_id = resolve_symbol_reference(symbol_id);\n \n if (is_nonterminal_symbol(symbol_id)) {\n vector<int> beta(prod.rhs.begin() + i + 1, prod.rhs.end());\n hash_set<int> first_beta = compute_first_of_sequence(beta);\n \n for (int sym : first_beta) {\n if (sym != -1 && follow_sets[resolved_id].find(sym) == follow_sets[resolved_id].end()) {\n follow_sets[resolved_id].insert(sym);\n changed = true;\n }\n }\n \n if (first_beta.find(-1) != first_beta.end()) {\n for (int sym : follow_sets[prod.lhs]) {\n if (follow_sets[resolved_id].find(sym) == follow_sets[resolved_id].end()) {\n follow_sets[resolved_id].insert(sym);\n changed = true;\n }\n }\n }\n }\n }\n }\n }\n }\n \n \n hash_set<LRItem, LRItemHash> closure(const hash_set<LRItem, LRItemHash> &items) {\n hash_set<LRItem, LRItemHash> result = items;\n bool changed = true;\n \n while (changed) {\n changed = false;\n vector<LRItem> new_items;\n \n for (const auto &item : result) {\n int next_sym = item.next_symbol();\n if (next_sym != -1 && is_nonterminal_symbol(next_sym)) {\n int resolved_id = resolve_symbol_reference(next_sym);\n \n for (int prod_idx = 0; prod_idx < (int)productions.size(); prod_idx++) {\n const auto &prod = productions[prod_idx];\n if (prod.lhs == resolved_id) {\n LRItem new_item(prod.lhs, prod.rhs, 0);\n if (result.find(new_item) == result.end()) {\n bool found = false;\n for (const auto &ni : new_items) {\n if (ni == new_item) {\n found = true;\n break;\n }\n }\n if (!found) {\n new_items.push_back(new_item);\n changed = true;\n }\n }\n }\n }\n }\n }\n \n for (const auto &item : new_items) {\n result.insert(item);\n }\n }\n \n return result;\n }\n \n \n hash_set<LRItem, LRItemHash> goto_func(const hash_set<LRItem, LRItemHash> &items, int symbol_id) {\n hash_set<LRItem, LRItemHash> goto_items;\n \n for (const auto &item : items) {\n if (item.next_symbol() == symbol_id) {\n LRItem new_item(item.lhs, item.rhs, item.dot_pos + 1);\n goto_items.insert(new_item);\n }\n }\n \n if (goto_items.empty()) {\n return goto_items;\n }\n \n return closure(goto_items);\n }\n \n \n bool states_equal(const hash_set<LRItem, LRItemHash> &a, const hash_set<LRItem, LRItemHash> &b) const {\n if (a.size() != b.size()) return false;\n for (const auto &item : a) {\n if (b.find(item) == b.end()) return false;\n }\n return true;\n }\n \n \n int find_state_id(const hash_set<LRItem, LRItemHash> &target_state) const {\n for (int i = 0; i < (int)states.size(); i++) {\n if (states_equal(states[i], target_state)) {\n return i;\n }\n }\n return -1;\n }\n \n \n void build_slr1_states() {\n if (start_symbol_id == -1) {\n throw runtime_error(\"Start symbol 'START' not found in grammar\");\n }\n \n compute_first_sets();\n compute_follow_sets();\n \n augmented_start_id = get_or_create_symbol_id(\"_START\");\n productions.insert(productions.begin(), Production(augmented_start_id, {start_symbol_id}));\n nonterminals.insert(augmented_start_id);\n \n \n hash_set<LRItem, LRItemHash> initial_items;\n LRItem initial_item(augmented_start_id, {start_symbol_id}, 0);\n initial_items.insert(initial_item);\n \n hash_set<LRItem, LRItemHash> initial_state = closure(initial_items);\n states.push_back(initial_state);\n \n queue<int> worklist;\n worklist.push(0);\n \n while (!worklist.empty()) {\n int current_state_id = worklist.front();\n worklist.pop();\n auto current_state = states[current_state_id];\n \n hash_set<int> symbols;\n for (const auto &item : current_state) {\n int next_sym = item.next_symbol();\n if (next_sym != -1) {\n symbols.insert(next_sym);\n }\n }\n \n for (int symbol_id : symbols) {\n auto next_state = goto_func(current_state, symbol_id);\n if (next_state.empty()) continue;\n \n int next_state_id = find_state_id(next_state);\n if (next_state_id == -1) {\n next_state_id = (int)states.size();\n states.push_back(next_state);\n worklist.push(next_state_id);\n }\n \n if (is_terminal_symbol(symbol_id)) {\n int resolved_id = resolve_symbol_reference(symbol_id);\n action_table[{current_state_id, resolved_id}] = Action(ActionType::SHIFT, next_state_id);\n } else if (is_nonterminal_symbol(symbol_id)) {\n int resolved_id = resolve_symbol_reference(symbol_id);\n goto_table[{current_state_id, resolved_id}] = next_state_id;\n }\n }\n }\n \n \n for (int state_id = 0; state_id < (int)states.size(); state_id++) {\n for (const auto &item : states[state_id]) {\n if (item.is_complete()) {\n if (item.lhs == augmented_start_id) {\n int eof_id = get_or_create_symbol_id(\"$\");\n action_table[{state_id, eof_id}] = Action(ActionType::ACCEPT, 0);\n } else {\n int prod_id = find_production_id(item.lhs, item.rhs);\n if (prod_id != -1) {\n \n for (int follow_sym : follow_sets[item.lhs]) {\n pair<int, int> key = {state_id, follow_sym};\n auto existing = action_table.find(key);\n \n action_table[key] = Action(ActionType::REDUCE, prod_id);\n }\n }\n }\n }\n }\n }\n }\n \n int find_production_id(int lhs, const vector<int> &rhs) {\n for (int i = 0; i < (int)productions.size(); i++) {\n if (productions[i].lhs == lhs && productions[i].rhs == rhs) {\n return i;\n }\n }\n return -1;\n }\n \n \n struct Token {\n int type_id;\n string value;\n Token(int t, const string &v) : type_id(t), value(v) {}\n };\n \n enum class TokenizeResult {\n SUCCESS,\n INVALID_REGEX,\n UNRECOGNIZED_CHARACTER\n };\n \n \nTokenizeResult tokenize(const string &input, vector<Token> &tokens) {\n tokens.clear();\n vector<regex> patterns;\n vector<int> type_ids;\n \n for (int term_id : terminal_order) {\n if (terminals.find(term_id) != terminals.end() && term_id < (int)terminal_patterns.size()) {\n try {\n patterns.push_back(regex(terminal_patterns[term_id]));\n type_ids.push_back(term_id);\n } catch (const regex_error &e) {\n return TokenizeResult::INVALID_REGEX;\n }\n }\n }\n \n auto start = input.cbegin();\n while (start != input.cend()) {\n if (isspace(*start)) {\n ++start;\n continue;\n }\n \n \n int best_match_length = 0;\n int best_match_type = -1;\n string best_match_value = \"\";\n \n for (size_t i = 0; i < patterns.size(); i++) {\n smatch match;\n if (regex_search(start, input.cend(), match, patterns[i]) && match.position() == 0) {\n int match_length = (int)match.length();\n \n \n if (match_length > best_match_length || \n (match_length == best_match_length && best_match_type == -1)) {\n best_match_length = match_length;\n best_match_type = type_ids[i];\n best_match_value = match.str();\n }\n }\n }\n \n if (best_match_type == -1) {\n return TokenizeResult::UNRECOGNIZED_CHARACTER;\n }\n \n tokens.emplace_back(best_match_type, best_match_value);\n start += best_match_length;\n }\n \n return TokenizeResult::SUCCESS;\n}\n \npublic:\n parser(const string &bnf_text, bool debug = false) : debug_mode(debug) {\n parse_bnf(bnf_text);\n build_slr1_states();\n }\n \n node* parse(const string &input) {\n vector<Token> tokens;\n TokenizeResult tokenize_result = tokenize(input, tokens);\n \n if (tokenize_result != TokenizeResult::SUCCESS) {\n return nullptr;\n }\n \n int eof_id = get_or_create_symbol_id(\"$\");\n tokens.emplace_back(eof_id, \"$\");\n \n vector<int> state_stack = {0};\n vector<node*> symbol_stack;\n int token_pos = 0;\n \n while (true) {\n if (state_stack.empty() || token_pos >= (int)tokens.size()) {\n return nullptr;\n }\n \n int current_state = state_stack.back();\n int current_token_type = tokens[token_pos].type_id;\n string current_token_value = tokens[token_pos].value;\n \n auto action_key = make_pair(current_state, current_token_type);\n auto action_it = action_table.find(action_key);\n \n if (action_it == action_table.end()) {\n return nullptr;\n }\n \n Action action = action_it->second;\n \n if (action.type == ActionType::SHIFT) {\n state_stack.push_back(action.value);\n node* new_node = new node(current_token_type, current_token_value, this);\n symbol_stack.push_back(new_node);\n token_pos++;\n } else if (action.type == ActionType::REDUCE) {\n if (action.value < 0 || action.value >= (int)productions.size()) {\n return nullptr;\n }\n \n Production &prod = productions[action.value];\n node* new_node = new node(prod.lhs, this);\n \n int pop_count = (int)prod.rhs.size();\n if (pop_count > (int)symbol_stack.size() || pop_count > (int)state_stack.size()) {\n return nullptr;\n }\n \n for (int i = 0; i < pop_count; i++) {\n node* child = symbol_stack.back();\n new_node->values.insert(new_node->values.begin(), child);\n symbol_stack.pop_back();\n state_stack.pop_back();\n }\n \n for (auto child : new_node->values) {\n if (child) {\n child->set_parser(this);\n }\n }\n \n symbol_stack.push_back(new_node);\n \n if (state_stack.empty()) {\n return nullptr;\n }\n \n auto goto_key = make_pair(state_stack.back(), prod.lhs);\n auto goto_it = goto_table.find(goto_key);\n if (goto_it == goto_table.end()) {\n return nullptr;\n }\n \n state_stack.push_back(goto_it->second);\n } else if (action.type == ActionType::ACCEPT) {\n if (symbol_stack.empty()) {\n return nullptr;\n }\n node* result = symbol_stack.back();\n if (result) {\n result->set_parser(this);\n }\n return result;\n } else {\n return nullptr;\n }\n }\n }\n \n string get_symbol_name(int symbol_id) const {\n if (symbol_id < 0 || symbol_id >= (int)id_to_name.size()) return \"\";\n return id_to_name[symbol_id];\n }\n};\n\n\nstring node::get_token_name() const {\n if (!parser_ptr) return \"\";\n return parser_ptr->get_symbol_name(symbol_id);\n}\n\nusing ll = long long;\nmap<string, pair<ll, map<ll, ll>>> env;\nll eval(node* root){\n if (root == nullptr) return -1;\n \n string symbol_name = root->get_token_name();\n \n if (symbol_name == \"START\"){\n return eval(root->values[0]);\n }\n\n if (symbol_name == \"DECL\"){\n string id = root->values[0]->value;\n ll num = stoll(root->values[2]->value);\n env[id] = {num, {}};\n return 0;\n }\n\n if (symbol_name == \"ASSIGN\"){\n string id1 = root->values[0]->value;\n ll num1 = eval(root->values[2]);\n ll num2 = eval(root->values[5]);\n\n if (env[id1].first <= num1) return -1;\n if (num1 == -1 || num2 == -1) return -1;\n env[id1].second[num1] = num2;\n return 0;\n }\n\n if (symbol_name == \"EXPR\"){\n if (root->values.size() == 1) {\n return stoll(root->values[0]->value);\n }\n else {\n string id = root->values[0]->value;\n ll num = eval(root->values[2]);\n if (env.find(id) == env.end()) return -1;\n if (env[id].second.count(num) == 0) return -1;\n if (num == -1) return -1;\n return env[id].second[num];\n }\n }\n\n if (symbol_name == \"NUM\") return stoll(root->value);\n \n return -1;\n}\n\nint main() {\n \n bool debug = false;\n \n auto parser0 = parser(\n \"ID = [A-Z]|[a-z];\"\n \"NUM = 0|([1-9][0-9]*);\"\n \"LPAR = \\\\[;\"\n \"RPAR = \\\\];\"\n \"EQ = \\\\=;\"\n \"START ::= #ASSIGN;\"\n \"ASSIGN ::= #ID #LPAR #EXPR #RPAR #EQ #EXPR;\"\n \"EXPR ::= #NUM | #ID #LPAR #EXPR #RPAR;\",\n debug\n );\n \n auto parser1 = parser(\n \"ID = [A-Z]|[a-z];\"\n \"NUM = 0|([1-9][0-9]*);\"\n \"LPAR = \\\\[;\"\n \"RPAR = \\\\];\"\n \"START ::= #DECL;\"\n \"DECL ::= #ID #LPAR #NUM #RPAR;\",\n debug\n );\n \n while (1){\n int t = 1;\n bool f = 0;\n env.clear();\n\n while(1){\n string s;\n cin >> s;\n if (s == \".\"){\n if (t == 1) return 0;\n if (!f) cout << 0 << '\\n';\n break;\n }\n if (f) continue;\n \n if (s.find('=') == -1){\n auto result = parser1.parse(s);\n assert(result != nullptr);\n eval(result);\n }\n else {\n auto result = parser0.parse(s);\n assert(result != nullptr);\n if (eval(result) == -1){\n cout << t << '\\n';\n f = 1;\n }\n }\n\n t++;\n }\n }\n \n return 0;\n}", "accuracy": 1, "time_ms": 550, "memory_kb": 19916, "score_of_the_acc": -1.7071, "final_rank": 18 }, { "submission_id": "aoj_1282_10675677", "code_snippet": "#include <bits/stdc++.h>\n#include <ext/pb_ds/assoc_container.hpp>\n#include <ext/pb_ds/tree_policy.hpp>\n#include <ext/pb_ds/tag_and_trait.hpp>\nusing namespace std;\nusing namespace __gnu_pbds;\n\n\nstruct custom_hash {\n static uint64_t splitmix64(uint64_t x) {\n x += 0x9e3779b97f4a7c15;\n x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;\n x = (x ^ (x >> 27)) * 0x94d049bb133111eb;\n return x ^ (x >> 31);\n }\n\n size_t operator()(uint64_t x) const {\n static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count();\n return splitmix64(x + FIXED_RANDOM);\n }\n \n size_t operator()(int x) const {\n return splitmix64(x);\n }\n\n size_t operator()(long long x) const {\n return splitmix64(static_cast<uint64_t>(x));\n }\n \n size_t operator()(const string& x) const {\n uint64_t hash = 0;\n for (char c : x) {\n hash = hash * 31 + c;\n }\n return splitmix64(hash);\n }\n \n size_t operator()(const pair<int, int>& p) const {\n return splitmix64(((uint64_t)p.first << 32) | (uint64_t)p.second);\n }\n};\n\n\ntemplate<typename T, typename S, typename Hash = custom_hash>\nusing hash_table = gp_hash_table<T, S, Hash>;\ntemplate<typename T, typename Hash = custom_hash>\nusing hash_set = gp_hash_table<T, null_type, Hash>;\n\n\nclass parser;\n\n\nstruct node {\n int symbol_id;\n string value;\n vector<node*> values;\n \nprivate:\n const parser* parser_ptr;\n \npublic:\n node(int s, const parser* p = nullptr) : symbol_id(s), value(\"\"), parser_ptr(p) {}\n node(int s, const string &v, const parser* p = nullptr) : symbol_id(s), value(v), parser_ptr(p) {}\n \n \n void set_parser(const parser* p) {\n parser_ptr = p;\n \n for (auto child : values) {\n if (child) {\n child->set_parser(p);\n }\n }\n }\n \n \n string get_token_name() const;\n \n \n void add_child(node* child) {\n if (child && parser_ptr) {\n child->set_parser(parser_ptr);\n }\n values.push_back(child);\n }\n};\n\n\nstruct LRItem {\n int lhs;\n vector<int> rhs;\n int dot_pos;\n \n LRItem(int l, const vector<int> &r, int d) : lhs(l), rhs(r), dot_pos(d) {}\n \n bool operator==(const LRItem &other) const {\n return lhs == other.lhs && rhs == other.rhs && dot_pos == other.dot_pos;\n }\n \n int next_symbol() const {\n if (dot_pos < (int)rhs.size()) return rhs[dot_pos];\n return -1;\n }\n \n bool is_complete() const {\n return dot_pos >= (int)rhs.size();\n }\n};\n\n\nstruct LRItemHash {\n size_t operator()(const LRItem &item) const {\n size_t h1 = custom_hash{}(item.lhs);\n size_t h2 = 0;\n for (int s : item.rhs) {\n h2 ^= custom_hash{}(s) + 0x9e3779b9 + (h2 << 6) + (h2 >> 2);\n }\n size_t h3 = custom_hash{}(item.dot_pos);\n return h1 ^ (h2 << 1) ^ (h3 << 2);\n }\n};\n\n\nenum class ActionType { SHIFT, REDUCE, ACCEPT, ERROR };\n\nstruct Action {\n ActionType type;\n int value;\n \n Action() : type(ActionType::ERROR), value(-1) {}\n Action(ActionType t, int v) : type(t), value(v) {}\n \n string to_string() const {\n switch (type) {\n case ActionType::SHIFT: return \"SHIFT \" + std::to_string(value);\n case ActionType::REDUCE: return \"REDUCE \" + std::to_string(value);\n case ActionType::ACCEPT: return \"ACCEPT\";\n case ActionType::ERROR: return \"ERROR\";\n }\n return \"UNKNOWN\";\n }\n};\n\n\nstruct Production {\n int lhs;\n vector<int> rhs;\n \n Production(int l, const vector<int> &r) : lhs(l), rhs(r) {}\n};\n\nclass parser {\nprivate:\n \n vector<string> id_to_name;\n hash_table<string, int> name_to_id;\n \n \n vector<string> terminal_patterns;\n vector<int> terminal_order;\n hash_set<int> terminals;\n hash_set<int> nonterminals;\n vector<Production> productions;\n \n \n hash_table<int, hash_set<int>> first_sets;\n hash_table<int, hash_set<int>> follow_sets;\n \n \n vector<hash_set<LRItem, LRItemHash>> states;\n hash_table<pair<int, int>, Action> action_table;\n hash_table<pair<int, int>, int> goto_table;\n \n int start_symbol_id = -1;\n int augmented_start_id = -1;\n bool debug_mode = false;\n \n \n int get_or_create_symbol_id(const string &name) {\n auto it = name_to_id.find(name);\n if (it != name_to_id.end()) {\n return it->second;\n }\n int id = (int)id_to_name.size();\n id_to_name.push_back(name);\n name_to_id[name] = id;\n return id;\n }\n \n int get_or_create_symbol_id_const(const string &name) const {\n auto it = name_to_id.find(name);\n if (it != name_to_id.end()) {\n return it->second;\n }\n return -1;\n }\n \n \n int resolve_symbol_reference(int symbol_id) const {\n if (symbol_id == -1 || symbol_id >= (int)id_to_name.size()) return symbol_id;\n const string &name = id_to_name[symbol_id];\n if (name.length() > 0 && name[0] == '#') {\n string actual_name = name.substr(1);\n auto it = name_to_id.find(actual_name);\n if (it != name_to_id.end()) {\n return it->second;\n }\n }\n return symbol_id;\n }\n \n bool is_terminal_symbol(int symbol_id) const {\n int resolved_id = resolve_symbol_reference(symbol_id);\n if (resolved_id == -1) return false;\n return terminals.find(resolved_id) != terminals.end() && \n nonterminals.find(resolved_id) == nonterminals.end();\n }\n \n bool is_nonterminal_symbol(int symbol_id) const {\n int resolved_id = resolve_symbol_reference(symbol_id);\n if (resolved_id == -1) return false;\n return nonterminals.find(resolved_id) != nonterminals.end();\n }\n \n \n string production_to_string(const Production &prod) const {\n string result = get_symbol_name(prod.lhs) + \" ::= \";\n for (int symbol_id : prod.rhs) {\n int resolved_id = resolve_symbol_reference(symbol_id);\n result += get_symbol_name(resolved_id) + \" \";\n }\n return result;\n }\n \n \n vector<string> split_by_semicolon(const string &text) {\n vector<string> rules;\n stringstream ss(text);\n string rule;\n \n while (getline(ss, rule, ';')) {\n rule.erase(0, rule.find_first_not_of(\" \\t\\r\\n\"));\n rule.erase(rule.find_last_not_of(\" \\t\\r\\n\") + 1);\n \n if (!rule.empty()) {\n rules.push_back(rule);\n }\n }\n \n return rules;\n }\n \n \n \nvoid parse_bnf(const string &bnf_text) {\n regex terminal_pattern(R\"(^\\s*([A-Za-z_][A-Za-z0-9_]*)\\s*=\\s*(.*?)\\s*$)\");\n regex production_pattern(R\"(^\\s*([A-Za-z_][A-Za-z0-9_]*)\\s*::=\\s*(.*?)\\s*$)\");\n \n vector<string> rules = split_by_semicolon(bnf_text);\n \n \n for (size_t rule_idx = 0; rule_idx < rules.size(); rule_idx++) {\n const string &rule = rules[rule_idx];\n check_bnf_warnings(rule, rule_idx + 1);\n }\n \n \n for (const string &rule : rules) {\n smatch match;\n if (regex_match(rule, match, terminal_pattern)) {\n string name = match[1].str();\n string pattern = match[2].str();\n \n int id = get_or_create_symbol_id(name);\n if ((int)terminal_patterns.size() <= id) {\n terminal_patterns.resize(id + 1);\n }\n terminal_patterns[id] = pattern;\n terminal_order.push_back(id);\n terminals.insert(id);\n }\n }\n \n \n for (const string &rule : rules) {\n smatch match;\n if (regex_match(rule, match, production_pattern)) {\n string lhs = match[1].str();\n string rhs_all = match[2].str();\n \n int lhs_id = get_or_create_symbol_id(lhs);\n nonterminals.insert(lhs_id);\n \n if (lhs == \"START\") {\n start_symbol_id = lhs_id;\n }\n \n istringstream rhs_stream(rhs_all);\n string rhs_part;\n while (getline(rhs_stream, rhs_part, '|')) {\n rhs_part.erase(0, rhs_part.find_first_not_of(\" \\t\"));\n rhs_part.erase(rhs_part.find_last_not_of(\" \\t\") + 1);\n \n if (!rhs_part.empty()) {\n vector<int> rhs_ids;\n istringstream symbol_stream(rhs_part);\n string symbol;\n while (symbol_stream >> symbol) {\n int symbol_id = get_or_create_symbol_id(symbol);\n rhs_ids.push_back(symbol_id);\n }\n productions.emplace_back(lhs_id, rhs_ids);\n \n \n check_production_warnings(lhs, rhs_part, productions.size());\n }\n }\n }\n }\n}\n\nprivate:\n \n void check_bnf_warnings(const string &rule, int rule_number) {\n \n int equals_count = 0;\n int define_equals_count = 0;\n bool in_escape = false;\n \n for (size_t i = 0; i < rule.length(); i++) {\n char c = rule[i];\n \n if (in_escape) {\n in_escape = false;\n continue;\n }\n \n if (c == '\\\\') {\n in_escape = true;\n continue;\n }\n \n \n if (i + 2 < rule.length() && rule.substr(i, 3) == \"::=\") {\n define_equals_count++;\n i += 2; \n } \n \n else if (c == '=' && (i == 0 || rule[i-1] != ':') && (i + 1 >= rule.length() || rule[i+1] != '=')) {\n equals_count++;\n }\n }\n \n int total_equals = equals_count + define_equals_count;\n }\n \n \n void check_production_warnings(const string &lhs, const string &rhs, int production_number) {\n \n istringstream symbol_stream(rhs);\n string symbol;\n vector<string> symbols_without_hash;\n \n while (symbol_stream >> symbol) {\n if (!symbol.empty() && symbol[0] != '#') {\n \n symbols_without_hash.push_back(symbol);\n }\n }\n }\n \n \n bool contains_unescaped_hash(const string &text) {\n bool in_escape = false;\n \n for (size_t i = 0; i < text.length(); i++) {\n char c = text[i];\n \n if (in_escape) {\n in_escape = false;\n continue;\n }\n \n if (c == '\\\\') {\n in_escape = true;\n continue;\n }\n \n if (c == '#') {\n return true;\n }\n }\n \n return false;\n }\n \n \n void compute_first_sets() {\n \n for (int term_id : terminals) {\n first_sets[term_id].insert(term_id);\n }\n \n \n for (int nonterm_id : nonterminals) {\n first_sets[nonterm_id] = hash_set<int>();\n }\n \n bool changed = true;\n while (changed) {\n changed = false;\n \n for (const auto &prod : productions) {\n int lhs = prod.lhs;\n const auto &rhs = prod.rhs;\n \n hash_set<int> first_rhs = compute_first_of_sequence(rhs);\n \n for (int sym : first_rhs) {\n if (first_sets[lhs].find(sym) == first_sets[lhs].end()) {\n first_sets[lhs].insert(sym);\n changed = true;\n }\n }\n }\n }\n }\n \n \n hash_set<int> compute_first_of_sequence(const vector<int> &sequence) {\n hash_set<int> result;\n \n if (sequence.empty()) {\n result.insert(-1); \n return result;\n }\n \n for (int i = 0; i < (int)sequence.size(); i++) {\n int symbol_id = sequence[i];\n int resolved_id = resolve_symbol_reference(symbol_id);\n \n if (is_terminal_symbol(symbol_id)) {\n result.insert(resolved_id);\n break;\n } else if (is_nonterminal_symbol(symbol_id)) {\n bool has_epsilon = false;\n for (int sym : first_sets[resolved_id]) {\n if (sym == -1) {\n has_epsilon = true;\n } else {\n result.insert(sym);\n }\n }\n \n if (!has_epsilon) {\n break;\n }\n \n if (i == (int)sequence.size() - 1) {\n result.insert(-1); \n }\n }\n }\n \n return result;\n }\n \n \n void compute_follow_sets() {\n int eof_id = get_or_create_symbol_id(\"$\");\n follow_sets[start_symbol_id].insert(eof_id);\n \n bool changed = true;\n while (changed) {\n changed = false;\n \n for (const auto &prod : productions) {\n for (int i = 0; i < (int)prod.rhs.size(); i++) {\n int symbol_id = prod.rhs[i];\n int resolved_id = resolve_symbol_reference(symbol_id);\n \n if (is_nonterminal_symbol(symbol_id)) {\n vector<int> beta(prod.rhs.begin() + i + 1, prod.rhs.end());\n hash_set<int> first_beta = compute_first_of_sequence(beta);\n \n for (int sym : first_beta) {\n if (sym != -1 && follow_sets[resolved_id].find(sym) == follow_sets[resolved_id].end()) {\n follow_sets[resolved_id].insert(sym);\n changed = true;\n }\n }\n \n if (first_beta.find(-1) != first_beta.end()) {\n for (int sym : follow_sets[prod.lhs]) {\n if (follow_sets[resolved_id].find(sym) == follow_sets[resolved_id].end()) {\n follow_sets[resolved_id].insert(sym);\n changed = true;\n }\n }\n }\n }\n }\n }\n }\n }\n \n \n hash_set<LRItem, LRItemHash> closure(const hash_set<LRItem, LRItemHash> &items) {\n hash_set<LRItem, LRItemHash> result = items;\n bool changed = true;\n \n while (changed) {\n changed = false;\n vector<LRItem> new_items;\n \n for (const auto &item : result) {\n int next_sym = item.next_symbol();\n if (next_sym != -1 && is_nonterminal_symbol(next_sym)) {\n int resolved_id = resolve_symbol_reference(next_sym);\n \n for (int prod_idx = 0; prod_idx < (int)productions.size(); prod_idx++) {\n const auto &prod = productions[prod_idx];\n if (prod.lhs == resolved_id) {\n LRItem new_item(prod.lhs, prod.rhs, 0);\n if (result.find(new_item) == result.end()) {\n bool found = false;\n for (const auto &ni : new_items) {\n if (ni == new_item) {\n found = true;\n break;\n }\n }\n if (!found) {\n new_items.push_back(new_item);\n changed = true;\n }\n }\n }\n }\n }\n }\n \n for (const auto &item : new_items) {\n result.insert(item);\n }\n }\n \n return result;\n }\n \n \n hash_set<LRItem, LRItemHash> goto_func(const hash_set<LRItem, LRItemHash> &items, int symbol_id) {\n hash_set<LRItem, LRItemHash> goto_items;\n \n for (const auto &item : items) {\n if (item.next_symbol() == symbol_id) {\n LRItem new_item(item.lhs, item.rhs, item.dot_pos + 1);\n goto_items.insert(new_item);\n }\n }\n \n if (goto_items.empty()) {\n return goto_items;\n }\n \n return closure(goto_items);\n }\n \n \n bool states_equal(const hash_set<LRItem, LRItemHash> &a, const hash_set<LRItem, LRItemHash> &b) const {\n if (a.size() != b.size()) return false;\n for (const auto &item : a) {\n if (b.find(item) == b.end()) return false;\n }\n return true;\n }\n \n \n int find_state_id(const hash_set<LRItem, LRItemHash> &target_state) const {\n for (int i = 0; i < (int)states.size(); i++) {\n if (states_equal(states[i], target_state)) {\n return i;\n }\n }\n return -1;\n }\n \n \n void build_slr1_states() {\n if (start_symbol_id == -1) {\n throw runtime_error(\"Start symbol 'START' not found in grammar\");\n }\n \n compute_first_sets();\n compute_follow_sets();\n \n augmented_start_id = get_or_create_symbol_id(\"_START\");\n productions.insert(productions.begin(), Production(augmented_start_id, {start_symbol_id}));\n nonterminals.insert(augmented_start_id);\n \n \n hash_set<LRItem, LRItemHash> initial_items;\n LRItem initial_item(augmented_start_id, {start_symbol_id}, 0);\n initial_items.insert(initial_item);\n \n hash_set<LRItem, LRItemHash> initial_state = closure(initial_items);\n states.push_back(initial_state);\n \n queue<int> worklist;\n worklist.push(0);\n \n while (!worklist.empty()) {\n int current_state_id = worklist.front();\n worklist.pop();\n auto current_state = states[current_state_id];\n \n hash_set<int> symbols;\n for (const auto &item : current_state) {\n int next_sym = item.next_symbol();\n if (next_sym != -1) {\n symbols.insert(next_sym);\n }\n }\n \n for (int symbol_id : symbols) {\n auto next_state = goto_func(current_state, symbol_id);\n if (next_state.empty()) continue;\n \n int next_state_id = find_state_id(next_state);\n if (next_state_id == -1) {\n next_state_id = (int)states.size();\n states.push_back(next_state);\n worklist.push(next_state_id);\n }\n \n if (is_terminal_symbol(symbol_id)) {\n int resolved_id = resolve_symbol_reference(symbol_id);\n action_table[{current_state_id, resolved_id}] = Action(ActionType::SHIFT, next_state_id);\n } else if (is_nonterminal_symbol(symbol_id)) {\n int resolved_id = resolve_symbol_reference(symbol_id);\n goto_table[{current_state_id, resolved_id}] = next_state_id;\n }\n }\n }\n \n \n for (int state_id = 0; state_id < (int)states.size(); state_id++) {\n for (const auto &item : states[state_id]) {\n if (item.is_complete()) {\n if (item.lhs == augmented_start_id) {\n int eof_id = get_or_create_symbol_id(\"$\");\n action_table[{state_id, eof_id}] = Action(ActionType::ACCEPT, 0);\n } else {\n int prod_id = find_production_id(item.lhs, item.rhs);\n if (prod_id != -1) {\n \n for (int follow_sym : follow_sets[item.lhs]) {\n pair<int, int> key = {state_id, follow_sym};\n auto existing = action_table.find(key);\n \n action_table[key] = Action(ActionType::REDUCE, prod_id);\n }\n }\n }\n }\n }\n }\n }\n \n int find_production_id(int lhs, const vector<int> &rhs) {\n for (int i = 0; i < (int)productions.size(); i++) {\n if (productions[i].lhs == lhs && productions[i].rhs == rhs) {\n return i;\n }\n }\n return -1;\n }\n \n \n struct Token {\n int type_id;\n string value;\n Token(int t, const string &v) : type_id(t), value(v) {}\n };\n \n enum class TokenizeResult {\n SUCCESS,\n INVALID_REGEX,\n UNRECOGNIZED_CHARACTER\n };\n \n \nTokenizeResult tokenize(const string &input, vector<Token> &tokens) {\n tokens.clear();\n vector<regex> patterns;\n vector<int> type_ids;\n \n for (int term_id : terminal_order) {\n if (terminals.find(term_id) != terminals.end() && term_id < (int)terminal_patterns.size()) {\n try {\n patterns.push_back(regex(terminal_patterns[term_id]));\n type_ids.push_back(term_id);\n } catch (const regex_error &e) {\n return TokenizeResult::INVALID_REGEX;\n }\n }\n }\n \n auto start = input.cbegin();\n while (start != input.cend()) {\n if (isspace(*start)) {\n ++start;\n continue;\n }\n \n \n int best_match_length = 0;\n int best_match_type = -1;\n string best_match_value = \"\";\n \n for (size_t i = 0; i < patterns.size(); i++) {\n smatch match;\n if (regex_search(start, input.cend(), match, patterns[i]) && match.position() == 0) {\n int match_length = (int)match.length();\n \n \n if (match_length > best_match_length || \n (match_length == best_match_length && best_match_type == -1)) {\n best_match_length = match_length;\n best_match_type = type_ids[i];\n best_match_value = match.str();\n }\n }\n }\n \n if (best_match_type == -1) {\n return TokenizeResult::UNRECOGNIZED_CHARACTER;\n }\n \n tokens.emplace_back(best_match_type, best_match_value);\n start += best_match_length;\n }\n \n return TokenizeResult::SUCCESS;\n}\n \npublic:\n parser(const string &bnf_text, bool debug = false) : debug_mode(debug) {\n parse_bnf(bnf_text);\n build_slr1_states();\n }\n \n node* parse(const string &input) {\n vector<Token> tokens;\n TokenizeResult tokenize_result = tokenize(input, tokens);\n \n if (tokenize_result != TokenizeResult::SUCCESS) {\n return nullptr;\n }\n \n int eof_id = get_or_create_symbol_id(\"$\");\n tokens.emplace_back(eof_id, \"$\");\n \n vector<int> state_stack = {0};\n vector<node*> symbol_stack;\n int token_pos = 0;\n \n while (true) {\n if (state_stack.empty() || token_pos >= (int)tokens.size()) {\n return nullptr;\n }\n \n int current_state = state_stack.back();\n int current_token_type = tokens[token_pos].type_id;\n string current_token_value = tokens[token_pos].value;\n \n auto action_key = make_pair(current_state, current_token_type);\n auto action_it = action_table.find(action_key);\n \n if (action_it == action_table.end()) {\n return nullptr;\n }\n \n Action action = action_it->second;\n \n if (action.type == ActionType::SHIFT) {\n state_stack.push_back(action.value);\n node* new_node = new node(current_token_type, current_token_value, this);\n symbol_stack.push_back(new_node);\n token_pos++;\n } else if (action.type == ActionType::REDUCE) {\n if (action.value < 0 || action.value >= (int)productions.size()) {\n return nullptr;\n }\n \n Production &prod = productions[action.value];\n node* new_node = new node(prod.lhs, this);\n \n int pop_count = (int)prod.rhs.size();\n if (pop_count > (int)symbol_stack.size() || pop_count > (int)state_stack.size()) {\n return nullptr;\n }\n \n for (int i = 0; i < pop_count; i++) {\n node* child = symbol_stack.back();\n new_node->values.insert(new_node->values.begin(), child);\n symbol_stack.pop_back();\n state_stack.pop_back();\n }\n \n for (auto child : new_node->values) {\n if (child) {\n child->set_parser(this);\n }\n }\n \n symbol_stack.push_back(new_node);\n \n if (state_stack.empty()) {\n return nullptr;\n }\n \n auto goto_key = make_pair(state_stack.back(), prod.lhs);\n auto goto_it = goto_table.find(goto_key);\n if (goto_it == goto_table.end()) {\n return nullptr;\n }\n \n state_stack.push_back(goto_it->second);\n } else if (action.type == ActionType::ACCEPT) {\n if (symbol_stack.empty()) {\n return nullptr;\n }\n node* result = symbol_stack.back();\n if (result) {\n result->set_parser(this);\n }\n return result;\n } else {\n return nullptr;\n }\n }\n }\n \n string get_symbol_name(int symbol_id) const {\n if (symbol_id < 0 || symbol_id >= (int)id_to_name.size()) return \"\";\n return id_to_name[symbol_id];\n }\n};\n\n\nstring node::get_token_name() const {\n if (!parser_ptr) return \"\";\n return parser_ptr->get_symbol_name(symbol_id);\n}\n\nusing ll = long long;\nhash_table<string, pair<ll, hash_table<ll, ll>>> env;\nll eval(node* root){\n if (root == nullptr) return -1;\n \n string symbol_name = root->get_token_name();\n \n if (symbol_name == \"START\"){\n return eval(root->values[0]);\n }\n\n if (symbol_name == \"DECL\"){\n string id = root->values[0]->value;\n ll num = stoll(root->values[2]->value);\n env[id] = {num, {}};\n return 0;\n }\n\n if (symbol_name == \"ASSIGN\"){\n string id1 = root->values[0]->value;\n ll num1 = eval(root->values[2]);\n ll num2 = eval(root->values[5]);\n\n if (env[id1].first <= num1) return -1;\n if (num1 == -1 || num2 == -1) return -1;\n env[id1].second[num1] = num2;\n return 0;\n }\n\n if (symbol_name == \"EXPR\"){\n if (root->values.size() == 1) {\n return stoll(root->values[0]->value);\n }\n else {\n string id = root->values[0]->value;\n ll num = eval(root->values[2]);\n if (env.find(id) == env.end()) return -1;\n if (env[id].second.find(num) == env[id].second.end()) return -1;\n if (num == -1) return -1;\n return env[id].second[num];\n }\n }\n\n if (symbol_name == \"NUM\") return stoll(root->value);\n \n return -1;\n}\n\nint main() {\n \n bool debug = false;\n \n auto parser0 = parser(\n \"ID = [A-Z]|[a-z];\"\n \"NUM = 0|([1-9][0-9]*);\"\n \"LPAR = \\\\[;\"\n \"RPAR = \\\\];\"\n \"EQ = \\\\=;\"\n \"START ::= #ASSIGN;\"\n \"ASSIGN ::= #ID #LPAR #EXPR #RPAR #EQ #EXPR;\"\n \"EXPR ::= #NUM | #ID #LPAR #EXPR #RPAR;\",\n debug\n );\n \n auto parser1 = parser(\n \"ID = [A-Z]|[a-z];\"\n \"NUM = 0|([1-9][0-9]*);\"\n \"LPAR = \\\\[;\"\n \"RPAR = \\\\];\"\n \"START ::= #DECL;\"\n \"DECL ::= #ID #LPAR #NUM #RPAR;\",\n debug\n );\n \n while (1){\n int t = 1;\n bool f = 0;\n env.clear();\n\n while(1){\n string s;\n cin >> s;\n if (s == \".\"){\n if (t == 1) return 0;\n if (!f) cout << 0 << endl;\n break;\n }\n if (f) continue;\n \n if (s.find('=') == -1){\n auto result = parser1.parse(s);\n assert(result != nullptr);\n eval(result);\n }\n else {\n auto result = parser0.parse(s);\n assert(result != nullptr);\n if (eval(result) == -1){\n cout << t << endl;\n f = 1;\n }\n }\n\n t++;\n }\n }\n \n return 0;\n}", "accuracy": 1, "time_ms": 560, "memory_kb": 20080, "score_of_the_acc": -1.7324, "final_rank": 20 }, { "submission_id": "aoj_1282_10675656", "code_snippet": "#include <bits/stdc++.h>\n#include <ext/pb_ds/assoc_container.hpp>\n#include <ext/pb_ds/tree_policy.hpp>\n#include <ext/pb_ds/tag_and_trait.hpp>\nusing namespace std;\nusing namespace __gnu_pbds;\n\n\nstruct custom_hash {\n static uint64_t splitmix64(uint64_t x) {\n x += 0x9e3779b97f4a7c15;\n x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;\n x = (x ^ (x >> 27)) * 0x94d049bb133111eb;\n return x ^ (x >> 31);\n }\n\n size_t operator()(uint64_t x) const {\n static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count();\n return splitmix64(x + FIXED_RANDOM);\n }\n \n size_t operator()(int x) const {\n return splitmix64(x);\n }\n \n size_t operator()(const string& x) const {\n uint64_t hash = 0;\n for (char c : x) {\n hash = hash * 31 + c;\n }\n return splitmix64(hash);\n }\n \n size_t operator()(const pair<int, int>& p) const {\n return splitmix64(((uint64_t)p.first << 32) | (uint64_t)p.second);\n }\n};\n\n\ntemplate<typename T, typename S, typename Hash = custom_hash>\nusing hash_table = gp_hash_table<T, S, Hash>;\ntemplate<typename T, typename Hash = custom_hash>\nusing hash_set = gp_hash_table<T, null_type, Hash>;\n\n\nclass parser;\n\n\nstruct node {\n int symbol_id;\n string value;\n vector<node*> values;\n \nprivate:\n const parser* parser_ptr;\n \npublic:\n node(int s, const parser* p = nullptr) : symbol_id(s), value(\"\"), parser_ptr(p) {}\n node(int s, const string &v, const parser* p = nullptr) : symbol_id(s), value(v), parser_ptr(p) {}\n \n \n void set_parser(const parser* p) {\n parser_ptr = p;\n \n for (auto child : values) {\n if (child) {\n child->set_parser(p);\n }\n }\n }\n \n \n string get_token_name() const;\n \n \n void add_child(node* child) {\n if (child && parser_ptr) {\n child->set_parser(parser_ptr);\n }\n values.push_back(child);\n }\n};\n\n\nstruct LRItem {\n int lhs;\n vector<int> rhs;\n int dot_pos;\n \n LRItem(int l, const vector<int> &r, int d) : lhs(l), rhs(r), dot_pos(d) {}\n \n bool operator==(const LRItem &other) const {\n return lhs == other.lhs && rhs == other.rhs && dot_pos == other.dot_pos;\n }\n \n int next_symbol() const {\n if (dot_pos < (int)rhs.size()) return rhs[dot_pos];\n return -1;\n }\n \n bool is_complete() const {\n return dot_pos >= (int)rhs.size();\n }\n};\n\n\nstruct LRItemHash {\n size_t operator()(const LRItem &item) const {\n size_t h1 = custom_hash{}(item.lhs);\n size_t h2 = 0;\n for (int s : item.rhs) {\n h2 ^= custom_hash{}(s) + 0x9e3779b9 + (h2 << 6) + (h2 >> 2);\n }\n size_t h3 = custom_hash{}(item.dot_pos);\n return h1 ^ (h2 << 1) ^ (h3 << 2);\n }\n};\n\n\nenum class ActionType { SHIFT, REDUCE, ACCEPT, ERROR };\n\nstruct Action {\n ActionType type;\n int value;\n \n Action() : type(ActionType::ERROR), value(-1) {}\n Action(ActionType t, int v) : type(t), value(v) {}\n \n string to_string() const {\n switch (type) {\n case ActionType::SHIFT: return \"SHIFT \" + std::to_string(value);\n case ActionType::REDUCE: return \"REDUCE \" + std::to_string(value);\n case ActionType::ACCEPT: return \"ACCEPT\";\n case ActionType::ERROR: return \"ERROR\";\n }\n return \"UNKNOWN\";\n }\n};\n\n\nstruct Production {\n int lhs;\n vector<int> rhs;\n \n Production(int l, const vector<int> &r) : lhs(l), rhs(r) {}\n};\n\nclass parser {\nprivate:\n \n vector<string> id_to_name;\n hash_table<string, int> name_to_id;\n \n \n vector<string> terminal_patterns;\n vector<int> terminal_order;\n hash_set<int> terminals;\n hash_set<int> nonterminals;\n vector<Production> productions;\n \n \n hash_table<int, hash_set<int>> first_sets;\n hash_table<int, hash_set<int>> follow_sets;\n \n \n vector<hash_set<LRItem, LRItemHash>> states;\n hash_table<pair<int, int>, Action> action_table;\n hash_table<pair<int, int>, int> goto_table;\n \n int start_symbol_id = -1;\n int augmented_start_id = -1;\n bool debug_mode = false;\n \n \n int get_or_create_symbol_id(const string &name) {\n auto it = name_to_id.find(name);\n if (it != name_to_id.end()) {\n return it->second;\n }\n int id = (int)id_to_name.size();\n id_to_name.push_back(name);\n name_to_id[name] = id;\n return id;\n }\n \n int get_or_create_symbol_id_const(const string &name) const {\n auto it = name_to_id.find(name);\n if (it != name_to_id.end()) {\n return it->second;\n }\n return -1;\n }\n \n \n int resolve_symbol_reference(int symbol_id) const {\n if (symbol_id == -1 || symbol_id >= (int)id_to_name.size()) return symbol_id;\n const string &name = id_to_name[symbol_id];\n if (name.length() > 0 && name[0] == '#') {\n string actual_name = name.substr(1);\n auto it = name_to_id.find(actual_name);\n if (it != name_to_id.end()) {\n return it->second;\n }\n }\n return symbol_id;\n }\n \n bool is_terminal_symbol(int symbol_id) const {\n int resolved_id = resolve_symbol_reference(symbol_id);\n if (resolved_id == -1) return false;\n return terminals.find(resolved_id) != terminals.end() && \n nonterminals.find(resolved_id) == nonterminals.end();\n }\n \n bool is_nonterminal_symbol(int symbol_id) const {\n int resolved_id = resolve_symbol_reference(symbol_id);\n if (resolved_id == -1) return false;\n return nonterminals.find(resolved_id) != nonterminals.end();\n }\n \n \n string production_to_string(const Production &prod) const {\n string result = get_symbol_name(prod.lhs) + \" ::= \";\n for (int symbol_id : prod.rhs) {\n int resolved_id = resolve_symbol_reference(symbol_id);\n result += get_symbol_name(resolved_id) + \" \";\n }\n return result;\n }\n \n \n vector<string> split_by_semicolon(const string &text) {\n vector<string> rules;\n stringstream ss(text);\n string rule;\n \n while (getline(ss, rule, ';')) {\n rule.erase(0, rule.find_first_not_of(\" \\t\\r\\n\"));\n rule.erase(rule.find_last_not_of(\" \\t\\r\\n\") + 1);\n \n if (!rule.empty()) {\n rules.push_back(rule);\n }\n }\n \n return rules;\n }\n \n \n \nvoid parse_bnf(const string &bnf_text) {\n regex terminal_pattern(R\"(^\\s*([A-Za-z_][A-Za-z0-9_]*)\\s*=\\s*(.*?)\\s*$)\");\n regex production_pattern(R\"(^\\s*([A-Za-z_][A-Za-z0-9_]*)\\s*::=\\s*(.*?)\\s*$)\");\n \n vector<string> rules = split_by_semicolon(bnf_text);\n \n \n for (size_t rule_idx = 0; rule_idx < rules.size(); rule_idx++) {\n const string &rule = rules[rule_idx];\n check_bnf_warnings(rule, rule_idx + 1);\n }\n \n \n for (const string &rule : rules) {\n smatch match;\n if (regex_match(rule, match, terminal_pattern)) {\n string name = match[1].str();\n string pattern = match[2].str();\n \n int id = get_or_create_symbol_id(name);\n if ((int)terminal_patterns.size() <= id) {\n terminal_patterns.resize(id + 1);\n }\n terminal_patterns[id] = pattern;\n terminal_order.push_back(id);\n terminals.insert(id);\n }\n }\n \n \n for (const string &rule : rules) {\n smatch match;\n if (regex_match(rule, match, production_pattern)) {\n string lhs = match[1].str();\n string rhs_all = match[2].str();\n \n int lhs_id = get_or_create_symbol_id(lhs);\n nonterminals.insert(lhs_id);\n \n if (lhs == \"START\") {\n start_symbol_id = lhs_id;\n }\n \n istringstream rhs_stream(rhs_all);\n string rhs_part;\n while (getline(rhs_stream, rhs_part, '|')) {\n rhs_part.erase(0, rhs_part.find_first_not_of(\" \\t\"));\n rhs_part.erase(rhs_part.find_last_not_of(\" \\t\") + 1);\n \n if (!rhs_part.empty()) {\n vector<int> rhs_ids;\n istringstream symbol_stream(rhs_part);\n string symbol;\n while (symbol_stream >> symbol) {\n int symbol_id = get_or_create_symbol_id(symbol);\n rhs_ids.push_back(symbol_id);\n }\n productions.emplace_back(lhs_id, rhs_ids);\n \n \n check_production_warnings(lhs, rhs_part, productions.size());\n }\n }\n }\n }\n}\n\nprivate:\n \n void check_bnf_warnings(const string &rule, int rule_number) {\n \n int equals_count = 0;\n int define_equals_count = 0;\n bool in_escape = false;\n \n for (size_t i = 0; i < rule.length(); i++) {\n char c = rule[i];\n \n if (in_escape) {\n in_escape = false;\n continue;\n }\n \n if (c == '\\\\') {\n in_escape = true;\n continue;\n }\n \n \n if (i + 2 < rule.length() && rule.substr(i, 3) == \"::=\") {\n define_equals_count++;\n i += 2; \n } \n \n else if (c == '=' && (i == 0 || rule[i-1] != ':') && (i + 1 >= rule.length() || rule[i+1] != '=')) {\n equals_count++;\n }\n }\n \n int total_equals = equals_count + define_equals_count;\n }\n \n \n void check_production_warnings(const string &lhs, const string &rhs, int production_number) {\n \n istringstream symbol_stream(rhs);\n string symbol;\n vector<string> symbols_without_hash;\n \n while (symbol_stream >> symbol) {\n if (!symbol.empty() && symbol[0] != '#') {\n \n symbols_without_hash.push_back(symbol);\n }\n }\n }\n \n \n bool contains_unescaped_hash(const string &text) {\n bool in_escape = false;\n \n for (size_t i = 0; i < text.length(); i++) {\n char c = text[i];\n \n if (in_escape) {\n in_escape = false;\n continue;\n }\n \n if (c == '\\\\') {\n in_escape = true;\n continue;\n }\n \n if (c == '#') {\n return true;\n }\n }\n \n return false;\n }\n \n \n void compute_first_sets() {\n \n for (int term_id : terminals) {\n first_sets[term_id].insert(term_id);\n }\n \n \n for (int nonterm_id : nonterminals) {\n first_sets[nonterm_id] = hash_set<int>();\n }\n \n bool changed = true;\n while (changed) {\n changed = false;\n \n for (const auto &prod : productions) {\n int lhs = prod.lhs;\n const auto &rhs = prod.rhs;\n \n hash_set<int> first_rhs = compute_first_of_sequence(rhs);\n \n for (int sym : first_rhs) {\n if (first_sets[lhs].find(sym) == first_sets[lhs].end()) {\n first_sets[lhs].insert(sym);\n changed = true;\n }\n }\n }\n }\n }\n \n \n hash_set<int> compute_first_of_sequence(const vector<int> &sequence) {\n hash_set<int> result;\n \n if (sequence.empty()) {\n result.insert(-1); \n return result;\n }\n \n for (int i = 0; i < (int)sequence.size(); i++) {\n int symbol_id = sequence[i];\n int resolved_id = resolve_symbol_reference(symbol_id);\n \n if (is_terminal_symbol(symbol_id)) {\n result.insert(resolved_id);\n break;\n } else if (is_nonterminal_symbol(symbol_id)) {\n bool has_epsilon = false;\n for (int sym : first_sets[resolved_id]) {\n if (sym == -1) {\n has_epsilon = true;\n } else {\n result.insert(sym);\n }\n }\n \n if (!has_epsilon) {\n break;\n }\n \n if (i == (int)sequence.size() - 1) {\n result.insert(-1); \n }\n }\n }\n \n return result;\n }\n \n \n void compute_follow_sets() {\n int eof_id = get_or_create_symbol_id(\"$\");\n follow_sets[start_symbol_id].insert(eof_id);\n \n bool changed = true;\n while (changed) {\n changed = false;\n \n for (const auto &prod : productions) {\n for (int i = 0; i < (int)prod.rhs.size(); i++) {\n int symbol_id = prod.rhs[i];\n int resolved_id = resolve_symbol_reference(symbol_id);\n \n if (is_nonterminal_symbol(symbol_id)) {\n vector<int> beta(prod.rhs.begin() + i + 1, prod.rhs.end());\n hash_set<int> first_beta = compute_first_of_sequence(beta);\n \n for (int sym : first_beta) {\n if (sym != -1 && follow_sets[resolved_id].find(sym) == follow_sets[resolved_id].end()) {\n follow_sets[resolved_id].insert(sym);\n changed = true;\n }\n }\n \n if (first_beta.find(-1) != first_beta.end()) {\n for (int sym : follow_sets[prod.lhs]) {\n if (follow_sets[resolved_id].find(sym) == follow_sets[resolved_id].end()) {\n follow_sets[resolved_id].insert(sym);\n changed = true;\n }\n }\n }\n }\n }\n }\n }\n }\n \n \n hash_set<LRItem, LRItemHash> closure(const hash_set<LRItem, LRItemHash> &items) {\n hash_set<LRItem, LRItemHash> result = items;\n bool changed = true;\n \n while (changed) {\n changed = false;\n vector<LRItem> new_items;\n \n for (const auto &item : result) {\n int next_sym = item.next_symbol();\n if (next_sym != -1 && is_nonterminal_symbol(next_sym)) {\n int resolved_id = resolve_symbol_reference(next_sym);\n \n for (int prod_idx = 0; prod_idx < (int)productions.size(); prod_idx++) {\n const auto &prod = productions[prod_idx];\n if (prod.lhs == resolved_id) {\n LRItem new_item(prod.lhs, prod.rhs, 0);\n if (result.find(new_item) == result.end()) {\n bool found = false;\n for (const auto &ni : new_items) {\n if (ni == new_item) {\n found = true;\n break;\n }\n }\n if (!found) {\n new_items.push_back(new_item);\n changed = true;\n }\n }\n }\n }\n }\n }\n \n for (const auto &item : new_items) {\n result.insert(item);\n }\n }\n \n return result;\n }\n \n \n hash_set<LRItem, LRItemHash> goto_func(const hash_set<LRItem, LRItemHash> &items, int symbol_id) {\n hash_set<LRItem, LRItemHash> goto_items;\n \n for (const auto &item : items) {\n if (item.next_symbol() == symbol_id) {\n LRItem new_item(item.lhs, item.rhs, item.dot_pos + 1);\n goto_items.insert(new_item);\n }\n }\n \n if (goto_items.empty()) {\n return goto_items;\n }\n \n return closure(goto_items);\n }\n \n \n bool states_equal(const hash_set<LRItem, LRItemHash> &a, const hash_set<LRItem, LRItemHash> &b) const {\n if (a.size() != b.size()) return false;\n for (const auto &item : a) {\n if (b.find(item) == b.end()) return false;\n }\n return true;\n }\n \n \n int find_state_id(const hash_set<LRItem, LRItemHash> &target_state) const {\n for (int i = 0; i < (int)states.size(); i++) {\n if (states_equal(states[i], target_state)) {\n return i;\n }\n }\n return -1;\n }\n \n \n void build_slr1_states() {\n if (start_symbol_id == -1) {\n throw runtime_error(\"Start symbol 'START' not found in grammar\");\n }\n \n compute_first_sets();\n compute_follow_sets();\n \n augmented_start_id = get_or_create_symbol_id(\"_START\");\n productions.insert(productions.begin(), Production(augmented_start_id, {start_symbol_id}));\n nonterminals.insert(augmented_start_id);\n \n \n hash_set<LRItem, LRItemHash> initial_items;\n LRItem initial_item(augmented_start_id, {start_symbol_id}, 0);\n initial_items.insert(initial_item);\n \n hash_set<LRItem, LRItemHash> initial_state = closure(initial_items);\n states.push_back(initial_state);\n \n queue<int> worklist;\n worklist.push(0);\n \n while (!worklist.empty()) {\n int current_state_id = worklist.front();\n worklist.pop();\n auto current_state = states[current_state_id];\n \n hash_set<int> symbols;\n for (const auto &item : current_state) {\n int next_sym = item.next_symbol();\n if (next_sym != -1) {\n symbols.insert(next_sym);\n }\n }\n \n for (int symbol_id : symbols) {\n auto next_state = goto_func(current_state, symbol_id);\n if (next_state.empty()) continue;\n \n int next_state_id = find_state_id(next_state);\n if (next_state_id == -1) {\n next_state_id = (int)states.size();\n states.push_back(next_state);\n worklist.push(next_state_id);\n }\n \n if (is_terminal_symbol(symbol_id)) {\n int resolved_id = resolve_symbol_reference(symbol_id);\n action_table[{current_state_id, resolved_id}] = Action(ActionType::SHIFT, next_state_id);\n } else if (is_nonterminal_symbol(symbol_id)) {\n int resolved_id = resolve_symbol_reference(symbol_id);\n goto_table[{current_state_id, resolved_id}] = next_state_id;\n }\n }\n }\n \n \n for (int state_id = 0; state_id < (int)states.size(); state_id++) {\n for (const auto &item : states[state_id]) {\n if (item.is_complete()) {\n if (item.lhs == augmented_start_id) {\n int eof_id = get_or_create_symbol_id(\"$\");\n action_table[{state_id, eof_id}] = Action(ActionType::ACCEPT, 0);\n } else {\n int prod_id = find_production_id(item.lhs, item.rhs);\n if (prod_id != -1) {\n \n for (int follow_sym : follow_sets[item.lhs]) {\n pair<int, int> key = {state_id, follow_sym};\n auto existing = action_table.find(key);\n \n action_table[key] = Action(ActionType::REDUCE, prod_id);\n }\n }\n }\n }\n }\n }\n }\n \n int find_production_id(int lhs, const vector<int> &rhs) {\n for (int i = 0; i < (int)productions.size(); i++) {\n if (productions[i].lhs == lhs && productions[i].rhs == rhs) {\n return i;\n }\n }\n return -1;\n }\n \n \n struct Token {\n int type_id;\n string value;\n Token(int t, const string &v) : type_id(t), value(v) {}\n };\n \n enum class TokenizeResult {\n SUCCESS,\n INVALID_REGEX,\n UNRECOGNIZED_CHARACTER\n };\n \n \nTokenizeResult tokenize(const string &input, vector<Token> &tokens) {\n tokens.clear();\n vector<regex> patterns;\n vector<int> type_ids;\n \n for (int term_id : terminal_order) {\n if (terminals.find(term_id) != terminals.end() && term_id < (int)terminal_patterns.size()) {\n try {\n patterns.push_back(regex(terminal_patterns[term_id]));\n type_ids.push_back(term_id);\n } catch (const regex_error &e) {\n return TokenizeResult::INVALID_REGEX;\n }\n }\n }\n \n auto start = input.cbegin();\n while (start != input.cend()) {\n if (isspace(*start)) {\n ++start;\n continue;\n }\n \n \n int best_match_length = 0;\n int best_match_type = -1;\n string best_match_value = \"\";\n \n for (size_t i = 0; i < patterns.size(); i++) {\n smatch match;\n if (regex_search(start, input.cend(), match, patterns[i]) && match.position() == 0) {\n int match_length = (int)match.length();\n \n \n if (match_length > best_match_length || \n (match_length == best_match_length && best_match_type == -1)) {\n best_match_length = match_length;\n best_match_type = type_ids[i];\n best_match_value = match.str();\n }\n }\n }\n \n if (best_match_type == -1) {\n return TokenizeResult::UNRECOGNIZED_CHARACTER;\n }\n \n tokens.emplace_back(best_match_type, best_match_value);\n start += best_match_length;\n }\n \n return TokenizeResult::SUCCESS;\n}\n \npublic:\n parser(const string &bnf_text, bool debug = false) : debug_mode(debug) {\n parse_bnf(bnf_text);\n build_slr1_states();\n }\n \n node* parse(const string &input) {\n vector<Token> tokens;\n TokenizeResult tokenize_result = tokenize(input, tokens);\n \n if (tokenize_result != TokenizeResult::SUCCESS) {\n return nullptr;\n }\n \n int eof_id = get_or_create_symbol_id(\"$\");\n tokens.emplace_back(eof_id, \"$\");\n \n vector<int> state_stack = {0};\n vector<node*> symbol_stack;\n int token_pos = 0;\n \n while (true) {\n if (state_stack.empty() || token_pos >= (int)tokens.size()) {\n return nullptr;\n }\n \n int current_state = state_stack.back();\n int current_token_type = tokens[token_pos].type_id;\n string current_token_value = tokens[token_pos].value;\n \n auto action_key = make_pair(current_state, current_token_type);\n auto action_it = action_table.find(action_key);\n \n if (action_it == action_table.end()) {\n return nullptr;\n }\n \n Action action = action_it->second;\n \n if (action.type == ActionType::SHIFT) {\n state_stack.push_back(action.value);\n node* new_node = new node(current_token_type, current_token_value, this);\n symbol_stack.push_back(new_node);\n token_pos++;\n } else if (action.type == ActionType::REDUCE) {\n if (action.value < 0 || action.value >= (int)productions.size()) {\n return nullptr;\n }\n \n Production &prod = productions[action.value];\n node* new_node = new node(prod.lhs, this);\n \n int pop_count = (int)prod.rhs.size();\n if (pop_count > (int)symbol_stack.size() || pop_count > (int)state_stack.size()) {\n return nullptr;\n }\n \n for (int i = 0; i < pop_count; i++) {\n node* child = symbol_stack.back();\n new_node->values.insert(new_node->values.begin(), child);\n symbol_stack.pop_back();\n state_stack.pop_back();\n }\n \n for (auto child : new_node->values) {\n if (child) {\n child->set_parser(this);\n }\n }\n \n symbol_stack.push_back(new_node);\n \n if (state_stack.empty()) {\n return nullptr;\n }\n \n auto goto_key = make_pair(state_stack.back(), prod.lhs);\n auto goto_it = goto_table.find(goto_key);\n if (goto_it == goto_table.end()) {\n return nullptr;\n }\n \n state_stack.push_back(goto_it->second);\n } else if (action.type == ActionType::ACCEPT) {\n if (symbol_stack.empty()) {\n return nullptr;\n }\n node* result = symbol_stack.back();\n if (result) {\n result->set_parser(this);\n }\n return result;\n } else {\n return nullptr;\n }\n }\n }\n \n string get_symbol_name(int symbol_id) const {\n if (symbol_id < 0 || symbol_id >= (int)id_to_name.size()) return \"\";\n return id_to_name[symbol_id];\n }\n};\n\n\nstring node::get_token_name() const {\n if (!parser_ptr) return \"\";\n return parser_ptr->get_symbol_name(symbol_id);\n}\n\nusing ll = long long;\nmap<string, pair<ll, map<ll, ll>>> env;\nll eval(node* root){\n if (root == nullptr) return -1;\n \n string symbol_name = root->get_token_name();\n \n if (symbol_name == \"START\"){\n return eval(root->values[0]);\n }\n\n if (symbol_name == \"DECL\"){\n string id = root->values[0]->value;\n ll num = stoll(root->values[2]->value);\n env[id] = {num, {}};\n return 0;\n }\n\n if (symbol_name == \"ASSIGN\"){\n string id1 = root->values[0]->value;\n ll num1 = eval(root->values[2]);\n ll num2 = eval(root->values[5]);\n\n if (env[id1].first <= num1) return -1;\n if (num1 == -1 || num2 == -1) return -1;\n env[id1].second[num1] = num2;\n return 0;\n }\n\n if (symbol_name == \"EXPR\"){\n if (root->values.size() == 1) {\n return stoll(root->values[0]->value);\n }\n else {\n string id = root->values[0]->value;\n ll num = eval(root->values[2]);\n if (env.find(id) == env.end()) return -1;\n if (env[id].second.count(num) == 0) return -1;\n if (num == -1) return -1;\n return env[id].second[num];\n }\n }\n\n if (symbol_name == \"NUM\") return stoll(root->value);\n \n return -1;\n}\n\nint main() {\n \n bool debug = false;\n \n auto parser0 = parser(\n \"ID = [A-Z]|[a-z];\"\n \"NUM = 0|([1-9][0-9]*);\"\n \"LPAR = \\\\[;\"\n \"RPAR = \\\\];\"\n \"EQ = \\\\=;\"\n \"START ::= #ASSIGN;\"\n \"ASSIGN ::= #ID #LPAR #EXPR #RPAR #EQ #EXPR;\"\n \"EXPR ::= #NUM | #ID #LPAR #EXPR #RPAR;\",\n debug\n );\n \n auto parser1 = parser(\n \"ID = [A-Z]|[a-z];\"\n \"NUM = 0|([1-9][0-9]*);\"\n \"LPAR = \\\\[;\"\n \"RPAR = \\\\];\"\n \"START ::= #DECL;\"\n \"DECL ::= #ID #LPAR #NUM #RPAR;\",\n debug\n );\n \n while (1){\n int t = 1;\n bool f = 0;\n env.clear();\n\n while(1){\n string s;\n cin >> s;\n if (s == \".\"){\n if (t == 1) return 0;\n if (!f) cout << 0 << endl;\n break;\n }\n if (f) continue;\n \n if (s.find('=') == -1){\n auto result = parser1.parse(s);\n assert(result != nullptr);\n eval(result);\n }\n else {\n auto result = parser0.parse(s);\n assert(result != nullptr);\n if (eval(result) == -1){\n cout << t << endl;\n f = 1;\n }\n }\n\n t++;\n }\n }\n \n return 0;\n}", "accuracy": 1, "time_ms": 560, "memory_kb": 20072, "score_of_the_acc": -1.732, "final_rank": 19 }, { "submission_id": "aoj_1282_4971357", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\n// <expression> ::= <number> | <array_name> [ <expression> ]\n// <number> ::= 0 | [1-9] [0-9]*\n// <array_name> ::= [a-zA-Z]\n\nstruct Parser {\n\t// var\n\tusing itr = string::const_iterator;\n\titr now;\n\tint64_t ans;\n\tmap<char, map<int64_t, int64_t>> arr;\n\t// expect\n\tconst struct ex {\n\t\tvector<char> _012;\n\t\tvector<char> azAZ;\n\t\tex() {\n\t\t\tfor(char c = '0'; c <= '9'; ++c) {\n\t\t\t\t_012.push_back(c);\n\t\t\t}\n\t\t\tfor(char c = 'a'; c <= 'z'; ++c) {\n\t\t\t\tazAZ.push_back(c);\n\t\t\t}\n\t\t\tfor(char c = 'A'; c <= 'Z'; ++c) {\n\t\t\t\tazAZ.push_back(c);\n\t\t\t}\n\t\t}\n\t} ex;\n\t\n\tParser(const string& s, map<char, map<int64_t, int64_t>>& arr) : arr(arr) {\n\t\tnow = s.begin();\n\t\tans = expression(now);\n\t}\n\t// <expression> ::= <number> | <array_name> [ <expression> ]\n\t// <array_name> ::= [a-zA-Z]\n\tint64_t expression(itr& now) {\n\t\tfor(char c : ex.azAZ) {\n\t\t\tif(*now == c) {\n\t\t\t\tnext(now, c);\n\t\t\t\tnext(now, '[');\n\t\t\t\tint64_t ret = expression(now);\n\t\t\t\tnext(now, ']');\n\t\t\t\tif(arr.count(c) and arr[c].count(ret)) {\n\t\t\t\t\treturn arr[c][ret];\n\t\t\t\t} else {\n\t\t\t\t\tthrow \"INVALID\";\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\treturn number(now);\n\t}\n\t// <number> ::= 0 | [1-9] [0-9]*\n\tint64_t number(itr& now) {\n\t\tint64_t ret = 0;\n\t\twhile(true) {\n\t\t\tif('0' <= *now and *now <= '9') {\n\t\t\t\tret *= 10;\n\t\t\t\tret += (*now) - '0';\n\t\t\t\tnext(now, ex._012);\n\t\t\t} else {\n\t\t\t\treturn ret;\n\t\t\t}\n\t\t}\n\t}\n\tvoid next(itr &now, const char expected) {\n\t\tvector<char> req = {expected};\n\t\tnext(now, req);\n\t}\n\tvoid next(itr &now, const vector<char> &expected) {\n\t\tfor(char c: expected){\n\t\t\tif(*now == c){\n\t\t\t\t++now;\n\t\t\t\treturn;\n\t\t\t}\n\t\t}\n\t\t// Debug\n\t\tfprintf(stderr, \"Expected: \");\n\t\tfor(char c: expected) {\n\t\t\tfprintf(stderr, \"%c, \", c);\n\t\t}\n\t\tfprintf(stderr, \"\\nGot: %c\\n\", *now);\n\t\tfprintf(stderr, \"Rest: \");\n\t\twhile(*now) {\n\t\t\tfprintf(stderr, \"%c\", *now++);\n\t\t}\n\t\tfprintf(stderr, \"\\n\");\n\t\texit(1);\n\t}\n};\n\nvoid solve(string& s) {\n\tmap<char, int64_t> sz;\n\tmap<char, map<int64_t, int64_t>> arr;\n\tbool bug = false;\n\tfor(int q = 1; ; ++q) {\n\t\tif(not bug) {\n\t\t\tbool dec = true;\n\t\t\tstring s1, s2;\n\t\t\tfor(int i = 0; i < s.size(); ++i) {\n\t\t\t\tif(s[i] == '=') {\n\t\t\t\t\ts1 = s.substr(0, i);\n\t\t\t\t\ts2 = s.substr(i + 1);\n\t\t\t\t\tdec = false;\n\t\t\t\t\tbreak;\n\t\t\t\t}\n\t\t\t}\n\t\t\tif(dec) {\n\t\t\t\tint64_t num = 0;\n\t\t\t\tfor(int i = 2; i + 1 < s.size(); ++i) {\n\t\t\t\t\tnum *= 10;\n\t\t\t\t\tnum += s[i] - '0';\n\t\t\t\t}\n\t\t\t\tsz[s[0]] = num;\n\t\t\t} else {\n\t\t\t\ttry {\n\t\t\t\t\tParser p1(s1.substr(2, s1.size() - 3), arr);\n\t\t\t\t\tif('0' <= s2[0] and s2[0] <= '9') {\n\t\t\t\t\t\tint64_t num = 0;\n\t\t\t\t\t\tfor(int i = 0; i < s2.size(); ++i) {\n\t\t\t\t\t\t\tnum *= 10;\n\t\t\t\t\t\t\tnum += s2[i] - '0';\n\t\t\t\t\t\t}\n\t\t\t\t\t\tif(not sz.count(s1[0])) {\n\t\t\t\t\t\t\tcout << q << '\\n';\n\t\t\t\t\t\t\tbug = true;\n\t\t\t\t\t\t\tcontinue;\n\t\t\t\t\t\t}\n\t\t\t\t\t\tif(sz[s1[0]] <= p1.ans) {\n\t\t\t\t\t\t\tcout << q << '\\n';\n\t\t\t\t\t\t\tbug = true;\n\t\t\t\t\t\t\tcontinue;\n\t\t\t\t\t\t}\n\t\t\t\t\t\tarr[s1[0]][p1.ans] = num;\n\t\t\t\t\t\t// cerr << s1[0] << p1.ans << \" \" << num << '\\n';\n\t\t\t\t\t} else {\n\t\t\t\t\t\tParser p2(s2, arr);\n\t\t\t\t\t\tif(not sz.count(s1[0])) {\n\t\t\t\t\t\t\tcout << q << '\\n';\n\t\t\t\t\t\t\tbug = true;\n\t\t\t\t\t\t\tcontinue;\n\t\t\t\t\t\t}\n\t\t\t\t\t\tif(sz[s1[0]] <= p1.ans) {\n\t\t\t\t\t\t\tcout << q << '\\n';\n\t\t\t\t\t\t\tbug = true;\n\t\t\t\t\t\t\tcontinue;\n\t\t\t\t\t\t}\n\t\t\t\t\t\tarr[s1[0]][p1.ans] = p2.ans;\n\t\t\t\t\t\t// cerr << s1[0] << p1.ans << \" \" << p2.ans << '\\n';\n\t\t\t\t\t}\n\t\t\t\t} catch(...) {\n\t\t\t\t\tcout << q << '\\n';\n\t\t\t\t\tbug = true;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\tcin >> s;\n\t\tif(s == \".\") {\n\t\t\tif(not bug) {\n\t\t\t\tcout << 0 << '\\n';\n\t\t\t}\n\t\t\treturn;\n\t\t}\n\t}\n}\n\nint main() {\n\tstring s;\n\twhile(cin >> s and s != \".\") {\n\t\tsolve(s);\n\t}\n\treturn 0;\n}", "accuracy": 1, "time_ms": 40, "memory_kb": 3360, "score_of_the_acc": -0.061, "final_rank": 14 }, { "submission_id": "aoj_1282_4969420", "code_snippet": "#include <bits/stdc++.h>\n\nusing namespace std;\n\nclass Parser{\n string S;\n int pos = 0;\n public:\n bool error = false;\n map<char, map<int, int>> A;\n map<char, int> _size;\n Parser(string T) : S(T){}\n\n char peek(){\n return pos < S.size() ? S[pos] : 'a';\n }\n\n bool is_number(char c){\n return '0' <= c and c <= '9';\n }\n\n bool is_alphabet(char c){\n return ('a' <= c and c <= 'z') or ('A' <= c and c <= 'Z');\n }\n\n void skip_spaces(){\n while (peek() == ' ')\n pos++;\n }\n\n int64_t factor(){\n skip_spaces();\n int64_t res = 0;\n if (is_alphabet(peek())){\n char c = peek();\n pos++;\n pos++;\n res = factor();\n pos++;\n if (A.count(c) == 0){\n error = true;\n return -1;\n }\n if (_size[c] <= res){\n error = true;\n return -1;\n }\n if (A[c].count(res) == 0){\n error = true;\n return -1;\n }\n res = A[c][res];\n }\n if (is_number(peek()))\n res = number();\n\n skip_spaces();\n return res;\n }\n\n int64_t number(){\n int64_t res = 0;\n while (is_number(peek())){\n res = res * 10 + peek() - '0';\n pos++;\n }\n return res;\n }\n};\n\nvector<string> split(string S, char delim){\n vector<string> res;\n int prev = -1;\n for (int i = 0; i < S.size(); i++){\n if (S[i] == delim){\n res.push_back(S.substr(prev + 1, i - prev - 1));\n prev = i;\n }\n }\n res.push_back(S.substr(prev + 1, int(S.size()) - prev - 1));\n return res;\n}\n\nint main(){\n string prev = \"\";\n while (true){\n map<char, map<int, int>> A;\n map<char, int> _size;\n int ans = 0;\n int k = 1;\n while (true){\n string S;\n cin >> S;\n if (prev == \".\" and S == \".\")\n return 0;\n prev = S;\n if (S == \".\")\n break;\n if (ans != 0)\n continue;\n if (S.find('=') == string::npos){\n char c = S[0];\n Parser parser(S.substr(2));\n auto r = parser.number();\n A[c] = {};\n _size[c] = r;\n\n } else {\n auto sp = split(S, '=');\n auto error = false;\n if (A.count(S[0]) == 0)\n error = true;\n auto l = sp[0];\n auto r = sp[1];\n Parser lp(l.substr(2)), rp(r);\n lp.A = A;\n rp.A = A;\n lp._size = _size;\n rp._size = _size;\n auto lres = lp.factor();\n auto rres = rp.factor();\n error |= lp.error | rp.error | _size[l[0]] <= lres;\n A[l[0]][lres] = rres;\n if (error)\n ans = k;\n }\n k++;\n }\n cout << ans << endl;\n }\n}", "accuracy": 1, "time_ms": 60, "memory_kb": 3368, "score_of_the_acc": -0.0977, "final_rank": 16 }, { "submission_id": "aoj_1282_4958394", "code_snippet": "#include <bits/stdc++.h>\n//#include <atcoder/all>\n//using namespace atcoder;\nconst double pi = 3.141592653589793238462643383279;\nusing namespace std;\n//typedef\n//------------------------------------------\ntypedef vector<int> VI;\ntypedef vector<VI> VVI;\ntypedef vector<string> VS;\ntypedef pair<int, int> PII;\ntypedef pair<long long, long long> PLL;\ntypedef pair<int, PII> TIII;\ntypedef long long LL;\ntypedef unsigned long long ULL;\ntypedef vector<LL> VLL;\ntypedef vector<VLL> VVLL;\n\n//container util\n//------------------------------------------\n#define ALL(a) (a).begin(), (a).end()\n#define RALL(a) (a).rbegin(), (a).rend()\n#define PB push_back\n#define MP make_pair\n#define SZ(a) int((a).size())\n#define SQ(a) ((a) * (a))\n#define EACH(i, c) for (typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)\n#define EXIST(s, e) ((s).find(e) != (s).end())\n#define SORT(c) sort((c).begin(), (c).end())\n\n//repetition\n//------------------------------------------\n#define FOR(i, s, n) for (int i = s; i < (int)n; ++i)\n#define REP(i, n) FOR(i, 0, n)\n#define MOD 1000000007\n\n#define rep(i, a, b) for (int i = a; i < (b); ++i)\n#define trav(a, x) for (auto &a : x)\n#define all(x) x.begin(), x.end()\n#define sz(x) (int)(x).size()\n\ntypedef long long ll;\ntypedef pair<int, int> pii;\ntypedef vector<int> vi;\n\n#define chmin(x, y) x = min(x, y)\n#define chmax(x, y) x = max(x, y)\nconst double EPS = 1e-8, PI = acos(-1);\n//ここから編集 \ntypedef string::const_iterator State;\n\npair<bool, pair<string, int>> compress(string s, map<string, int>& mp, map<pair<string, int>, int>& mpp){\n int m = s.size();\n stack<string> st;\n stack<int> st2;\n for(int j=0; j<m; j++){\n if(('a' <= s[j] && s[j] <= 'z') || ('A' <= s[j] && s[j] <= 'Z')){\n string tmp = \"\";\n int idx2 = 0;\n while(j+idx2<m && (('a' <= s[j+idx2] && s[j+idx2] <= 'z') || ('A' <= s[j+idx2] && s[j+idx2] <= 'Z'))){\n tmp += s[j+idx2];\n idx2++;\n }\n st.push(tmp);\n j += idx2-1;\n }else if('0' <= s[j] && s[j] <= '9'){\n int num = 0;\n int idx2 = 0;\n while(j+idx2<m && '0' <= s[j+idx2] && s[j+idx2] <= '9'){\n num = num*10 + s[j+idx2] - '0';\n idx2++;\n }\n st2.push(num);\n j += idx2-1;\n }\n }\n int now = st2.top();\n while(st.size()){\n if(st.size() == 1) break;\n string str = st.top();\n st.pop();\n if(mp.find(str) == mp.end()) return make_pair(false, make_pair(\"\", -1));\n if(mpp.find(make_pair(str, now)) == mpp.end()) return make_pair(false, make_pair(\"\", -1));\n if(mp[str] <= now) return make_pair(false, make_pair(\"\", -1));\n now = mpp[make_pair(str, now)];\n }\n string str = st.top();\n return make_pair(true, make_pair(str, now));\n}\n\nint solve(vector<string> f){\n int n = f.size();\n map<string, int> mp;\n map<pair<string, int>, int> mpp;\n for(int i=0; i<n; i++){\n bool assign=false;\n REP(j,f[i].size()){\n if(f[i][j] == '=') assign = true;\n }\n if(assign){\n string s=\"\", t=\"\";\n int idx = 0;\n while(idx < f[i].size() && f[i][idx] != '='){\n s += f[i][idx];\n idx++;\n }\n idx++;\n auto p = compress(s, mp, mpp);\n if(p.first == false) return i+1;\n \n string str = p.second.first;\n if(isdigit(f[i][idx])){\n int num = 0;\n while(idx<f[i].size() && isdigit(f[i][idx])){\n num = num * 10 + f[i][idx] - '0';\n idx++;\n }\n if(mp.find(str) == mp.end()) return i+1;\n if(mp[str] <= p.second.second) return i+1;\n mpp[p.second] = num;\n }else{\n \n while(idx < f[i].size()){\n t += f[i][idx];\n idx++;\n }\n auto q = compress(t, mp, mpp);\n\n if(q.first == false) return i+1;\n string str2 = q.second.first;\n if(mp.find(str) == mp.end()) return i+1;\n if(mp[str] <= p.second.second) return i+1;\n if(mpp.find(q.second) == mpp.end()) return i+1;\n if(mp.find(str2) == mp.end()) return i+1;\n if(mp[str2] <= q.second.second) return i+1;\n //cout << p.second.second << \" \" << q.second.second << endl;\n mpp[p.second] = mpp[q.second];\n }\n }else{\n string s = \"\";\n int idx = 0;\n while(idx < f[i].size() && f[i][idx] != '['){\n s += f[i][idx];\n idx++;\n }\n idx++;\n int num = 0;\n while(idx < f[i].size() && isdigit(f[i][idx])){\n num = num*10 + f[i][idx]-'0';\n idx++;\n }\n mp[s] = num;\n }\n }\n return 0;\n}\nint main()\n{\n cin.tie(0);\n ios::sync_with_stdio(false);\n cout << fixed << setprecision(6);\n\n string s;\n while(cin >> s){\n if(s == \".\") break;\n\n vector<string> f;\n f.push_back(s);\n while(cin >> s){\n if(s == \".\") break;\n f.push_back(s);\n }\n\n cout << solve(f) << endl;\n }\n return 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3460, "score_of_the_acc": -0.0108, "final_rank": 10 }, { "submission_id": "aoj_1282_4342250", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\ntypedef long long ll;\ntypedef pair<ll, ll> l_l;\ntypedef pair<int, int> i_i;\ntemplate<class T>\ninline bool chmax(T &a, T b) {\n if(a < b) {\n a = b;\n return true;\n }\n return false;\n}\n\ntemplate<class T>\ninline bool chmin(T &a, T b) {\n if(a > b) {\n a = b;\n return true;\n }\n return false;\n}\n\nconst long double EPS = 1e-10;\nconst long long INF = 1e18;\nconst long double PI = acos(-1.0L);\n//const ll mod = 1000000007;\nvector<string> S;\nmap<string, map<int, int>> mp;\nmap<string, int> sz;\ntypedef pair<string, int> si;\n\nint EqualIdx(string S) {\n for(int i = 0; i < S.size(); i++) {\n if(S[i] == '=') return i;\n }\n return -1;\n}\n\ntypedef string::iterator State;\n\nbool IsDigit(State &itr) {\n return '0' <= *itr and *itr <= '9';\n}\n\nint Number(State &itr) {\n int ret = 0;\n while(true) {\n if(!IsDigit(itr)) {\n //cerr << \"Number: \" << ret << endl;\n return ret;\n }\n ret = ret * 10 + (int)(*itr - '0');\n itr++;\n }\n}\n\nint val(si x) {\n //cerr << \"val: \" << x.first << \" \" << x.second << endl;\n if(x.second < 0) return x.second;\n if(x.first == \".\") return x.second;\n if(sz.count(x.first) == 0) return -1;\n //cerr << \"sz: \" << sz[x.first] << endl;\n if(sz[x.first] <= x.second) return -1;\n if(mp[x.first].count(x.second) == 0) return -1;\n //cerr << \"ok: \" << endl;\n return mp[x.first][x.second];\n}\n\nsi f(State &itr) {\n //cerr << \"f: \" << *itr << endl;\n if(IsDigit(itr)) {\n auto ret = Number(itr);\n itr++;\n return {\".\", ret};\n }\n string fi;\n while(*itr != '[') {\n fi.push_back(*itr);\n itr++;\n }\n itr++;\n auto Inside = f(itr);\n itr++;\n //if(Inside.first == \".\") return {fi, Inside.second};\n //cerr << \"Inside: \" << Inside.first << \" \" << Inside.second << endl;\n return {fi, val(Inside)};\n //return {fi, num};\n}\n\nint main() {\n while(true) {\n S.clear();\n mp.clear();\n sz.clear();\n while(true) {\n string tmp;\n cin >> tmp;\n if(tmp == \".\") break;\n S.push_back(tmp);\n }\n if(S.empty()) break;\n int ans = 0;\n for(int i = 0; i < S.size(); i++) {\n //cerr << \"----\" << i << \"----\" << endl;\n int idx = EqualIdx(S[i]);\n if(idx >= 0) {\n auto itr = S[i].begin();\n auto Left = f(itr);\n itr = S[i].begin();\n itr += idx + 1;\n auto Right = f(itr);\n if(Right.second < 0) {\n ans = i + 1;\n break;\n }\n //cerr << \"A\" << endl;\n //cerr << Right.first << \" \" << Right.second << endl;\n int Value = val(Right);\n //cerr << Right.first << \" \" << Right.second << \" \" << Value << endl;\n if(Value < 0) {\n ans = i + 1;\n break;\n }\n //cerr << \"In: \" << Left.first << \" \" << Left.second << \" \" << Value << endl;\n if(sz[Left.first] <= Left.second) {\n ans = i + 1;\n break;\n }\n mp[Left.first][Left.second] = Value;\n //cerr << Left.first << \" \" << Left.second << \" \" << Right.first << \" \" << Right.second << endl;\n //cerr << Left.first << \" \" << Left.second << \" ---val--- \" << Value << endl;\n } else {\n auto itr = S[i].begin();\n auto Main = f(itr);\n sz[Main.first] = Main.second;\n mp[Main.first][-2] = -1;\n //cerr << Main.first << \" \" << Main.second << endl;\n }\n }\n cout << ans << endl;\n }\n return 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3212, "score_of_the_acc": 0, "final_rank": 1 }, { "submission_id": "aoj_1282_3987400", "code_snippet": "#include<bits/stdc++.h>\n#include<regex>\n#include<cctype>\nusing namespace std;\n#define INFS (1LL<<28)\n#define INF (1LL<<60)\n#define DEKAI 1000000007\n#define MOD 998244353\n#define lp(i,n) for(int i=0;i<n;i++)\n#define double long double\n#define all(c) begin(c), end(c)\n\n//#define int long long \n\nnamespace {\n#define __DECLARE__(C) \\\n template <typename T> \\\n std::ostream &operator<<(std::ostream &, const C<T> &);\n\n#define __DECLAREM__(C) \\\n template <typename T, typename U> \\\n std::ostream &operator<<(std::ostream &, const C<T, U> &);\n\n__DECLARE__(std::vector)\n__DECLARE__(std::deque)\n__DECLARE__(std::set)\n__DECLARE__(std::stack)\n__DECLARE__(std::queue)\n__DECLARE__(std::priority_queue)\n__DECLARE__(std::unordered_set)\n__DECLAREM__(std::map)\n__DECLAREM__(std::unordered_map)\n\ntemplate <typename T, typename U>\nstd::ostream &operator<<(std::ostream &, const std::pair<T, U> &);\ntemplate <typename... T>\nstd::ostream &operator<<(std::ostream &, const std::tuple<T...> &);\ntemplate <typename T, std::size_t N>\nstd::ostream &operator<<(std::ostream &, const std::array<T, N> &);\n\ntemplate <typename Tuple, std::size_t N>\nstruct __TuplePrinter__ {\n static void print(std::ostream &os, const Tuple &t) {\n __TuplePrinter__<Tuple, N - 1>::print(os, t);\n os << \", \" << std::get<N - 1>(t);\n }\n};\n\ntemplate <typename Tuple>\nstruct __TuplePrinter__<Tuple, 1> {\n static void print(std::ostream &os, const Tuple &t) { os << std::get<0>(t); }\n};\n\ntemplate <typename... T>\nstd::ostream &operator<<(std::ostream &os, const std::tuple<T...> &t) {\n os << '(';\n __TuplePrinter__<decltype(t), sizeof...(T)>::print(os, t);\n os << ')';\n return os;\n}\n\ntemplate <typename T, typename U>\nstd::ostream &operator<<(std::ostream &os, const std::pair<T, U> &v) {\n return os << '(' << v.first << \", \" << v.second << ')';\n}\n\n#define __INNER__ \\\n os << '['; \\\n for (auto it = begin(c); it != end(c);) { \\\n os << *it; \\\n os << (++it != end(c) ? \", \" : \"\"); \\\n } \\\n return os << ']';\n\ntemplate <typename T, std::size_t N>\nstd::ostream &operator<<(std::ostream &os, const std::array<T, N> &c) {\n __INNER__\n}\n\n#define __DEFINE__(C) \\\n template <typename T> \\\n std::ostream &operator<<(std::ostream &os, const C<T> &c) { \\\n __INNER__ \\\n }\n\n#define __DEFINEM__(C) \\\n template <typename T, typename U> \\\n std::ostream &operator<<(std::ostream &os, const C<T, U> &c) { \\\n __INNER__ \\\n }\n\n#define __DEFINEW__(C, M1, M2) \\\n template <typename T> \\\n std::ostream &operator<<(std::ostream &os, const C<T> &c) { \\\n std::deque<T> v; \\\n for (auto d = c; !d.empty(); d.pop()) v.M1(d.M2()); \\\n return os << v; \\\n }\n\n__DEFINE__(std::vector)\n__DEFINE__(std::deque)\n__DEFINE__(std::set)\n__DEFINEW__(std::stack, push_front, top)\n__DEFINEW__(std::queue, push_back, front)\n__DEFINEW__(std::priority_queue, push_front, top)\n__DEFINE__(std::unordered_set)\n__DEFINEM__(std::map)\n__DEFINEM__(std::unordered_map)\n}\n\n\n#define rep(i,n) lp(i,n)\nusing Weight = int;\nusing Flow = int;\nstruct Edge {\n int src, dst;\n Weight weight;\n Flow cap;\n Edge() : src(0), dst(0), weight(0) {}\n Edge(int s, int d, Weight w) : src(s), dst(d), weight(w) {}\n};\nusing Edges = std::vector<Edge>;\nusing Graph = std::vector<Edges>;\nusing Array = std::vector<Weight>;\nusing Matrix = std::vector<Array>;\n\nvoid add_edge(Graph &g, int a, int b, Weight w = 1) {\n g[a].emplace_back(a, b, w);\n g[b].emplace_back(b, a, w);\n}\nvoid add_arc(Graph &g, int a, int b, Weight w = 1) { g[a].emplace_back(a, b, w); }\n\n\n\n\n\n\nmap<string,int> varmaxi;\nmap<pair<string,int>,int> val;\n\nvoid mpin(){\n\tvarmaxi.clear();\n\tval.clear();\n}\n\nbool iseq(string &s){\n\tlp(i,s.size()){\n\t\tif(s[i]=='=')return true;\n\t}\n\treturn false;\n}\n\nvoid stsep(string s,string &l,string &r){\n\tlp(i,s.size()){\n\t\tif(s[i]=='='){\n\t\t\tl=s.substr(0,i);\n\t\t\tr=s.substr(i+1,s.size()-i-1);\n\t\t\treturn;\n\t\t}\n\t}\n\tassert(false);\n}\n\npair<string,string> valsep(string s){\n\tlp(i,s.size()){\n\t\tif(s[i]=='['){\n\t\t\tstring x,y;\n\t\t\tx=s.substr(0,i);\n\t\t\ty=s.substr(i+1,s.size()-i-2);\n\t\t\treturn {x,y};\t\t\n\t\t}\n\t}\n\tassert(false);\n}\n\nint valget(string s){\n\tpair<string,string> p=valsep(s);\n\tstring x=p.first;\n\tstring y=p.second;\n\tint num=stoi(y);\n\tif(varmaxi.find(x)==varmaxi.end()){\n\t\treturn -1;\n\t}\n\tint maxi=varmaxi[x];\n\tif(num>=maxi){\n\t\treturn -1;\n\t}\n\tif(val.find({x,num})==val.end()){\n\t\treturn -1;\n\t}\n\treturn val[{x,num}];\n}\nint valget(string x,string y){\n\tint num=stoi(y);\n\tif(varmaxi.find(x)==varmaxi.end()){\n\t\treturn -1;\n\t}\n\tint maxi=varmaxi[x];\n\tif(num>=maxi){\n\t\treturn -1;\n\t}\n\tif(val.find({x,num})==val.end()){\n\t\treturn -1;\n\t}\n\treturn val[{x,num}];\n}\nbool isd(string s){\n\tlp(i,s.size()){\n\t\tif('0'<=s[i]&&s[i]<='9')continue;\n\t\treturn false;\n\t}\n\treturn true;\n}\n\nint solr(string s){\n\t//cout<<s<<endl;\n\tif (isd(s)){\n\t\treturn stoi(s);\n\t}\n\telse{\n\t\tstring x,y;\n\t\tpair<string,string> p=valsep(s);\n\t\tx=p.first;\n\t\ty=p.second;\n\t\ty=to_string(solr(y));\n\t\treturn valget(x,y);\n\t}\n}\n\nint valset(string s,int num){\n\tstring x,y;\n\tx=valsep(s).first;\n\ty=valsep(s).second;\n\tint v=stoi(y);\n\tif(varmaxi.find(x)==varmaxi.end())return -1;\n\tif(varmaxi[x]<=v)return -1;\n\tval[{x,v}]=num;\n\treturn 0;\n}\n\nint soll(string s,int num){\n\tif (isd(s)){\n\t\treturn stoi(s);\n\t}\n\telse{\n\t\tstring x,y;\n\t\tpair<string,string> p=valsep(s);\n\t\tx=p.first;\n\t\ty=p.second;\n\t\ty=to_string(solr(y));\n\t\tif(y==\"-1\")return -1;\n\t\ts=x+\"[\"+y+\"]\";\n\t\treturn valset(s,num);\n\t}\n}\n\nbool solve(string l,string r){\n\tint x=solr(r);\n\tif(x==-1)return false;\n\tif(soll(l,x)==-1)return false;\n\treturn true;\n}\n\nsigned main(){\n\twhile(1){\n\t\tmpin();\n\t\tint cnt=0;\n\t\tvector<string> v;\n\t\twhile(1){\n\t\t\tstring s;\n\t\t\tcin>>s;\n\t\t\tif(s==\".\")break;\n\t\t\tv.push_back(s);\n\t\t}\n\t\tif(v.empty())break;\n\t\tlp(i,v.size()){\n\t\t\tstring s=v[cnt];\n\t\t\tcnt++;\n\t\t\tif(iseq(s)){\n\t\t\t\tstring l,r;\n\t\t\t\tstsep(s,l,r);\n\t\t\t\tif(solve(l,r)==false){\n\t\t\t\t\tbreak;\n\t\t\t\t}\n\t\t\t}\n\t\t\telse{\n\t\t\t\tstring x=valsep(s).first;\n\t\t\t\tstring y=valsep(s).second;\n\t\t\t\tint n=stoi(y);\n\t\t\t\tvarmaxi[x]=n;\n\t\t\t}\n\t\t\tif(i+1==v.size())cnt=0;\n\t\t}\n\t\tcout<<cnt<<endl;\n\t}\n\treturn 0;\n}", "accuracy": 1, "time_ms": 20, "memory_kb": 3356, "score_of_the_acc": -0.0244, "final_rank": 12 }, { "submission_id": "aoj_1282_3669298", "code_snippet": "#include<iostream>\n#include<map>\n\nusing namespace std;\nusing State = string::iterator;\nclass ParseError{};\n\nmap<char, int> vals;\nmap<pair<char, int>, int> assigns;\nstring line;\n\nbool parse_declaration(State& begin);\nbool parse_assignment(State& begin);\nint parse_expression(State& begin);\nint parse_number(State& begin);\n\nvoid debug(State& begin, string msg) {\n cout << line << endl;\n int l = distance(line.begin(), begin);\n for (int i = 0; i < l; ++i) cout << \" \";\n cout << \"^ \" << msg << endl;\n}\n\nbool parse_line(State& begin) {\n // debug(begin, \"line\");\n State start = begin;\n bool ret;\n try {\n ret = parse_declaration(begin);\n } catch(ParseError e) {\n begin = start;\n ret = parse_assignment(begin);\n }\n return ret;\n}\n\nbool is_digit(char c) {\n return c - '0' >= 0 && c - '0' <= 9;\n}\n\nvoid newline(State &begin) {\n if (begin != line.end()) throw ParseError();\n}\nvoid consume(State &begin, char c) {\n if (*begin != c) {\n // cout << line << endl;\n // debug(begin, string(\"expected \") + c + string(\" but got \") + *begin);\n throw ParseError();\n }\n begin++;\n}\n\nbool parse_declaration(State& begin) {\n // debug(begin, \"decl\");\n char array_name = *begin;\n begin++;\n consume(begin, '[');\n int length = parse_number(begin);\n consume(begin, ']');\n newline(begin);\n vals[array_name] = length;\n return true;\n}\n\nbool parse_assignment(State& begin) {\n // debug(begin, \"assi\");\n char array_name = *begin;\n begin++;\n consume(begin, '[');\n int index = parse_expression(begin);\n if (index == -1) return false;\n consume(begin, ']');\n consume(begin, '=');\n int value = parse_expression(begin);\n newline(begin);\n if (value == -1) return false;\n if (index >= vals[array_name]) return false;\n auto key = make_pair(array_name, index);\n assigns[key] = value;\n return true;\n}\n\nint parse_expression(State& begin) {\n if (is_digit(*begin)) {\n return parse_number(begin);\n } else {\n char array_name = *begin;\n begin++;\n consume(begin, '[');\n int index = parse_expression(begin);\n consume(begin, ']');\n if (vals.find(array_name) != vals.end()) {\n auto key = make_pair(array_name, index);\n if (assigns.find(key) != assigns.end()) {\n return assigns[key];\n } else {\n return -1;\n }\n } else {\n return -1;\n }\n }\n}\n\n\nint parse_number(State& begin) {\n int ret = 0;\n while(*begin - '0' >= 0 && *begin - '0' <= 9) {\n ret *= 10;\n ret += *begin - '0';\n begin++;\n }\n return ret;\n}\n\nint main() {\n int cnt = 0;\n int lc = 1;\n int ans = 0;\n while(1) {\n cin >> line;\n if (line == \".\") {\n cout << ans << endl;\n vals.clear();\n assigns.clear();\n lc = 1;\n ans = 0;\n cin >> line;\n if (line == \".\") break;\n } else {\n cnt = 0;\n }\n if (ans == 0) {\n State begin = line.begin();\n if (!parse_line(begin)) {\n ans = lc;\n }\n }\n lc++;\n }\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3260, "score_of_the_acc": -0.0021, "final_rank": 6 }, { "submission_id": "aoj_1282_3354690", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\ntypedef long long ll;\ntypedef pair<int,int> P;\ntypedef pair<int,P> P1;\ntypedef pair<P,P> P2;\n#define pu push\n#define pb push_back\n#define mp make_pair\n#define eps 1e-7\n#define INF 1000000000\n#define mod 1000000007\n#define fi first\n#define sc second\n#define rep(i,x) for(int i=0;i<x;i++)\n#define repn(i,x) for(int i=1;i<=x;i++)\n#define SORT(x) sort(x.begin(),x.end())\n#define ERASE(x) x.erase(unique(x.begin(),x.end()),x.end())\n#define POSL(x,v) (lower_bound(x.begin(),x.end(),v)-x.begin())\n#define POSU(x,v) (upper_bound(x.begin(),x.end(),v)-x.begin())\nstring str;\nmap<char,ll>M;\nmap<pair<char,ll>,ll>MM;\nll expr(string str){\n\tif('0' <= str[0] && str[0] <= '9'){\n\t\tll ret = 0;\n\t\trep(i,str.size()) ret = ret*10LL+str[i]-'0';\n\t\treturn ret;\n\t}\n\telse{\n\t\tchar id = str[0];\n\t\tstring nw = \"\";\n\t\tfor(int i=2;i<str.size()-1;i++) nw.pb(str[i]);\n\t\tll in = expr(nw);\n\t\tif(in > 1e17) return 1e18;\n\t\tif(M[id] <= in) return 1e18;\n\t\tif(0 > in) return 1e18;\n\t\tif(MM.find(mp(id,in)) == MM.end()) return 1e18;\n\t\treturn MM[mp(id,in)];\n\t}\n}\nint main(){\n\twhile(1){\n\t\tcin>>str;\n\t\tif(str == \".\") return 0;\n\t\tM.clear(); MM.clear();\n\t\tvector<string>vec; vec.pb(str);\n\t\twhile(1){\n\t\t\tcin>>str;\n\t\t\tif(str == \".\") break;\n\t\t\tvec.pb(str);\n\t\t}\n\t\trep(i,vec.size()){\n\t\t\tstring par = vec[i];\n\t\t\tint pos = -1;\n\t\t\trep(j,par.size()){\n\t\t\t\tif(par[j] == '=') pos = j;\n\t\t\t}\n\t\t\tif(pos == -1){\n\t\t\t\t//declaration\n\t\t\t\tchar id = par[0];\n\t\t\t\tll num = 0;\n\t\t\t\tfor(int i=2;i<par.size()-1;i++){\n\t\t\t\t\tnum = num*10+par[i]-'0';\n\t\t\t\t}\n\t\t\t\tM[id] = num;\n\t\t\t}\n\t\t\telse{\n\t\t\t\tstring le=\"\",ri=\"\";\n\t\t\t\tfor(int j=2;j<pos-1;j++) le.pb(par[j]);\n\t\t\t\tfor(int j=pos+1;j<par.size();j++) ri.pb(par[j]);\n\t\t\t\tll assign_val = expr(ri);\n\t\t\t\tchar id = par[0];\n\t\t\t\tll index = expr(le);\n\t\t\t\t//cout << index << \" \" << id << \" \" << assign_val << endl;\n\t\t\t\tif(index > 1e17 || index >= M[id] || index < 0 || assign_val > 1e17){\n\t\t\t\t\tcout << i+1 << endl;\n\t\t\t\t\tgoto fail;\n\t\t\t\t}\n\t\t\t\tMM[mp(id,index)] = assign_val;\n\t\t\t}\n\t\t}\n\t\tcout << 0 << endl;\n\t\tfail:;\n\t}\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3316, "score_of_the_acc": -0.0045, "final_rank": 7 }, { "submission_id": "aoj_1282_3340166", "code_snippet": "#include <bits/stdc++.h>\n#define int long long\nusing namespace std;\nusing ld = long double;\n\ntemplate <class T, class U>\nbool contains(const map<T, U> &mp, const T &key) {\n return mp.find(key) != mp.end();\n}\n\nstruct State {\n int idx;\n int line;\n vector<string> s;\n};\n\nstruct Expr {\n enum Type {\n IMV,\n ARRAY,\n } type;\n int value;\n char arr_name;\n Expr *num;\n static Expr *from_int(int x) {\n return new Expr {Type::IMV, x, '\\0', nullptr};\n }\n};\n\nstruct Statement {\n enum Type {\n DECL,\n ASSIGN,\n } type;\n char arr_name;\n Expr *num;\n Expr *value;\n};\n\nchar get_char(State &st) {\n if ((int)st.s[st.line].size() <= st.idx) return '\\n';\n return st.s[st.line][st.idx];\n}\n\nbool is_digit(State &st) {\n return '0' <= get_char(st) && get_char(st) <= '9';\n}\n\nint parse_num(State &st) {\n string num = \"\";\n while (is_digit(st)) {\n num += get_char(st);\n st.idx += 1;\n }\n int ret = stoll(num);\n return ret;\n}\n\nExpr *parse_expr(State &st) {\n if (is_digit(st)) {\n return Expr::from_int(parse_num(st));\n } else {\n Expr *expr = new Expr();\n expr->type = Expr::Type::ARRAY;\n expr->arr_name = get_char(st);\n st.idx += 2;\n expr->num = parse_expr(st);\n st.idx += 1;\n return expr;\n }\n}\n\nvector<Statement> parse_program(State &st) {\n vector<Statement> statements;\n while (st.line < (int)st.s.size()) {\n Statement s;\n s.arr_name = get_char(st);\n st.idx += 2;\n s.num = parse_expr(st);\n st.idx += 1;\n if (get_char(st) == '\\n') {\n s.type = Statement::Type::DECL;\n } else {\n st.idx += 1;\n s.type = Statement::Type::ASSIGN;\n s.value = parse_expr(st);\n }\n st.line++;\n st.idx = 0;\n statements.push_back(s);\n }\n return statements;\n}\n\nmap<char, int> arr_size;\nmap<char, map<int, int>> arr_data;\nbool eval_expr(Expr &expr, int &ret) {\n if (expr.type == Expr::Type::IMV) {\n ret = expr.value;\n return true;\n } else {\n int num;\n bool valid = eval_expr(*expr.num, num);\n if (!valid) return false;\n if (!contains(arr_size, expr.arr_name)) return false;\n if (!(0 <= num && num < arr_size[expr.arr_name])) return false;\n if (!contains(arr_data[expr.arr_name], num)) return false;\n ret = arr_data[expr.arr_name][num];\n return true;\n }\n}\nbool eval_statement(Statement &s) {\n if (s.type == Statement::Type::DECL) {\n int num;\n bool valid = eval_expr(*s.num, num);\n if (!valid) return false;\n arr_size[s.arr_name] = num;\n } else {\n if (!contains(arr_size, s.arr_name)) return false;\n int num, value;\n bool valid = eval_expr(*s.num, num);\n if (!valid) return false;\n if (!(0 <= num && num < arr_size[s.arr_name])) return false;\n valid = eval_expr(*s.value, value);\n if (!valid) return false;\n arr_data[s.arr_name][num] = value;\n }\n return true;\n}\nint check_program(vector<Statement> &prog) {\n int ans = 0;\n for (int i = 0; i < (int)prog.size(); i++) {\n auto &s = prog[i];\n if (!eval_statement(s)) {\n ans = i + 1;\n break;\n }\n }\n return ans;\n}\n\nsigned main() {\n while (1) {\n arr_size.clear();\n arr_data.clear();\n string s;\n cin >> s;\n if (s == \".\") break;\n\n State state = {0, 0, {}};\n state.s.push_back(s);\n while (1) {\n cin >> s;\n if (s == \".\") break;\n state.s.push_back(s);\n }\n auto prog = parse_program(state);\n cout << check_program(prog) << endl;\n }\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 5228, "score_of_the_acc": -0.0875, "final_rank": 15 }, { "submission_id": "aoj_1282_2818466", "code_snippet": "#include <iostream>\n#include <vector>\n#include <algorithm>\n#include <map>\n#include <string>\n#include <utility>\nusing namespace std;\ntypedef pair<string, string> pss;\n\nmap<string, map<int, int> > arrays;\nmap<string, int> limit;\nbool isdeclare(string &s){\n\tfor(int i=0; i<(int)s.length(); i++){\n\t\tif(s[i]=='=') return false;\n\t}\n\treturn true;\n}\nint tonum(string &s){\n\tfor(int i=0; i<(int)s.length(); i++){\n\t\tif(s[i] < '0' || '9' < s[i]){\n\t\t\treturn -1;\n\t\t}\n\t}\n\treturn stoi(s);\n}\npss parsebrac(string &s){\n\tint n = s.length();\n\tint i=0;\n\twhile(s[i]!='[') i++;\n\treturn make_pair(s.substr(0, i), s.substr(i+1, n-i-2));\n}\npss parseeq(string &s){\n\tint n = s.length();\n\tint i=0;\n\twhile(s[i]!='=') i++;\n\treturn make_pair(s.substr(0, i), s.substr(i+1, n-i-1));\n}\nint getnum(string &s){\n\tif(tonum(s)!=-1) return tonum(s);\n\tpss pstr = parsebrac(s);\n\tint idx = getnum(pstr.second);\n\tstring name = pstr.first;\n\tif(arrays[name].find(idx) != arrays[name].end()){\n\t\treturn arrays[name][idx];\n\t}\n\treturn -1;\n}\n\nint main(){\n\twhile(1){\n\t\tbool end = false;\n\t\tint ans = 0;\n\t\tarrays = map<string, map<int, int> >();\n\t\tlimit = map<string, int>();\n\t\tfor(int line=1; ; line++){\n\t\t\tstring str;\n\t\t\tcin >> str;\n\t\t\tif(str == \".\"){\n\t\t\t\tif(line == 1) end = true;\n\t\t\t\tbreak;\n\t\t\t}\n\t\t\tif(ans != 0) continue;\n\t\t\t\n\t\t\tif(isdeclare(str)){\n\t\t\t\tpss pstr = parsebrac(str);\n\t\t\t\tlimit[pstr.first] = tonum(pstr.second);\n\t\t\t\tarrays[pstr.first] = map<int, int>();\n\t\t\t}else{\n\t\t\t\tpss pstr1 = parseeq(str);\n\t\t\t\tint val = getnum(pstr1.second);\n\t\t\t\tpss pstr2 = parsebrac(pstr1.first);\n\t\t\t\tint subsidx = getnum(pstr2.second);\n\t\t\t\tstring subsname = pstr2.first;\n\t\t\t\tif(val!=-1 && subsidx!=-1 && limit[subsname]>subsidx){\n\t\t\t\t\tarrays[subsname][subsidx] = val;\n\t\t\t\t}else{\n\t\t\t\t\tans = line;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\tif(end) break;\n\t\tcout << ans << endl;\n\t}\n\treturn 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3228, "score_of_the_acc": -0.0007, "final_rank": 3 }, { "submission_id": "aoj_1282_2818463", "code_snippet": "#include <iostream>\n#include <vector>\n#include <algorithm>\n#include <map>\n#include <string>\n#include <utility>\nusing namespace std;\ntypedef pair<string, string> pss;\n\nmap<string, map<int, int> > name;\nmap<string, int> limit;\nbool isdeclare(string &s){\n\tfor(int i=0; i<(int)s.length(); i++){\n\t\tif(s[i]=='=') return false;\n\t}\n\treturn true;\n}\nint tonum(string &s){\n\tfor(int i=0; i<(int)s.length(); i++){\n\t\tif(s[i] < '0' || '9' < s[i]){\n\t\t\treturn -1;\n\t\t}\n\t}\n\treturn stoi(s);\n}\npss divbrac(string &s){\n\tint n = s.length();\n\tint i=0;\n\twhile(s[i]!='[') i++;\n\treturn make_pair(s.substr(0, i), s.substr(i+1, n-i-2));\n}\npss diveq(string &s){\n\tint n = s.length();\n\tint i=0;\n\twhile(s[i]!='=') i++;\n\treturn make_pair(s.substr(0, i), s.substr(i+1, n-i-1));\n}\nint getnum(string &s){\n\tif(tonum(s)!=-1) return tonum(s);\n\tpss parse = divbrac(s);\n\tint idx = getnum(parse.second);\n\tstring arrayname = parse.first;\n\tif(name[arrayname].find(idx) != name[arrayname].end()){\n\t\treturn name[arrayname][idx];\n\t}\n\treturn -1;\n}\n\nint main(){\n\twhile(1){\n\t\tbool end = false;\n\t\tint ans = 0;\n\t\tname = map<string, map<int, int> >();\n\t\tlimit = map<string, int>();\n\t\tfor(int line=1; ; line++){\n\t\t\tstring str;\n\t\t\tcin >> str;\n\t\t\tif(str == \".\"){\n\t\t\t\tif(line == 1) end = true;\n\t\t\t\tbreak;\n\t\t\t}\n\t\t\tif(ans != 0) continue;\n\t\t\t\n\t\t\tif(isdeclare(str)){\n\t\t\t\tpss parse = divbrac(str);\n\t\t\t\tlimit[parse.first] = tonum(parse.second);\n\t\t\t\tname[parse.first] = map<int, int>();\n\t\t\t}else{\n\t\t\t\tpss parse = diveq(str);\n\t\t\t\tint val = getnum(parse.second);\n\t\t\t\tpss parse2 = divbrac(parse.first);\n\t\t\t\tint subidx = getnum(parse2.second);\n\t\t\t\tstring subarray = parse2.first;\n\t\t\t\tif(val!=-1 && subidx!=-1 && limit[subarray]>subidx){\n\t\t\t\t\tname[subarray][subidx] = val;\n\t\t\t\t}else{\n\t\t\t\t\tans = line;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\tif(end) break;\n\t\tcout << ans << endl;\n\t}\n\treturn 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3228, "score_of_the_acc": -0.0007, "final_rank": 3 }, { "submission_id": "aoj_1282_2641917", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\nclass arrayInfo{\n private:\n int element=-1;\n map<string,string> value;\n public:\n bool isInRange(string index){\n return stoi(index)<element;\n }\n void set(string index, string val){\n if(isInRange(index)){\n value[index]=val;\n }else{\n throw 1;\n }\n }\n string get(string index){\n if(isInRange(index) && value.find(index)!=value.end()){\n return value[index];\n }else{\n throw 1;\n }\n }\n void setRange(string range){\n if(element==-1){\n element=stoi(range);\n }else{\n throw 1;\n }\n }\n};\n#define all(a) a.begin(),a.end()\n#define rall(a) a.rbegin(),a.rend()\n\nmap<string, arrayInfo> ar;\n\nstring parse(string str, string type){\n int l,r;\n if(str.find(\"[\")!=string::npos){\n l=str.find(\"[\");\n r=str.rfind(\"]\");\n if(type==\"name\") return str.substr(0,l);\n if(type==\"index\") return parse(str.substr(l+1,r-l-1), \"value\");\n if(type==\"value\") return ar[str.substr(0,l)].get(parse(str.substr(l+1,r-l-1), \"value\"));\n }\n return str;\n}\n\nint main(void){\n while(true){\n ar.erase(ar.begin(),ar.end());\n //cout<<\"=========\\n\";\n string ln;\n cin>>ln;\n if(ln==\".\"){\n break;\n }\n int n=1;\n bool isOccured=false;\n while(ln!=\".\"){\n try{\n if(ln.find(\"=\")==string::npos){\n ar[parse(ln,\"name\")].setRange(parse(ln,\"index\"));\n }else{\n string ls=ln.substr(0,ln.find(\"=\"));\n string rs=ln.substr(ln.find(\"=\")+1);\n ar[parse(ls,\"name\")].set(parse(ls,\"index\"),parse(rs,\"value\"));\n }\n }catch(...){\n isOccured=true;\n break;\n }\n n++;\n cin>>ln;\n }\n if(isOccured){\n cout<<n<<endl;\n while(cin>>ln,ln!=\".\");\n }else{\n cout<<0<<endl;\n }\n }\n}", "accuracy": 1, "time_ms": 20, "memory_kb": 3324, "score_of_the_acc": -0.023, "final_rank": 11 }, { "submission_id": "aoj_1282_2598962", "code_snippet": "#include<string>\n#include<sstream>\n#include<iostream>\n#include<map>\n#include<vector>\n#include<algorithm>\n#include<cstdio>\n//#include<bits/stdc++.h>\nusing namespace std;\n\nvector<string> code;\nmap<string, int> val;\nmap<char, int> a;\n\nint get_val(string str) {\n\tif(str.find(\"[\") == string::npos) {\n\t\tstringstream ss(str);\n\t\tint v; ss >> v;\n\t\treturn v;\n\t}\n\tif(count(str.begin(), str.end(), '[') == 1) {\n\t\tif(!val.count(str)) return -1;\n\t\treturn val[str];\n\t}\n\tstring cur = str.substr(str.find(\"[\") + 1);\n\tint v = get_val(cur);\n\tstringstream ss;\n\tss << v;\n\tstring s; ss >> s;\n\tif(!val.count(str.substr(0, 2) + s)) return -1;\n\treturn val[str.substr(0, 2) + s];\n}\nvoid add_arr(const string str) {\n\tstringstream ss(str.substr(2));\n\tint v; ss >> v;\n\ta[str[0]] = v;\n}\nint main() {\n\tios::sync_with_stdio(false);\n\tstring line, l, r, name;\n\tint rval, lval, bug;\n\twhile(code.clear(), val.clear(), a.clear(), bug = -1, true) {\n\t\tcin >> line;\n\t\tif(line == \".\") break;\n\t\twhile(true) {\n\t\t\tcode.push_back(line);\n\t\t\tcin >> line;\n\t\t\tif(line == \".\") break;\n\t\t}\n\t\tfor(size_t i = 0; i != code.size(); i++) {\n\t\t\tif(code[i].find(\"=\") == string::npos) \n\t\t\t\tadd_arr(code[i]);\n\t\t\telse {\n\t\t\t\tl = code[i].substr(0, code[i].find(\"=\"));\n\t\t\t\tr = code[i].substr(code[i].find(\"=\") + 1);\n\t\t\t\trval = get_val(r.substr(0, r.find(\"]\")));\n\t\t\t\tlval = get_val(l.substr(l.find(\"[\") + 1, l.find(\"]\") - l.find(\"[\") - 1));\n\t\t\t\tif(rval == -1 || lval == -1 || !a[code[i][0]] || lval >= a[code[i][0]]) {bug = i; break;}\n\t\t\t\tstringstream ss;\n\t\t\t\tss << lval;\n\t\t\t\tstring s; ss >> s;\n\t\t\t\tname = code[i].substr(0, 2) + s;\n\t\t\t\tval[name] = rval;\n\t\t\t}\n\t\t}\n\t\tcout << bug + 1 << endl;\n\t}\n\treturn 0;\n}", "accuracy": 1, "time_ms": 30, "memory_kb": 3336, "score_of_the_acc": -0.0417, "final_rank": 13 }, { "submission_id": "aoj_1282_2473624", "code_snippet": "#include <iostream>\n#include <string>\n#include <vector>\n#include <map>\n#include <cstdlib>\n#include <cassert>\n#include <algorithm>\n#include <utility>\nusing namespace std;\n\n#define fi first\n#define se second\ntypedef long long ll;\n\nll recursive_parse(const string & s, int & indx, map<string,ll>& symbol_table, map<pair<string,ll>,ll>& vals) {\n\tif('0' <= s[indx] && s[indx] <= '9') {\n\t\tll res = 0;\n\t\twhile(s[indx] != ']' && indx < s.size()) {\n\t\t\tres *= 10;\n\t\t\tres += s[indx]-'0';\n\t\t\tindx++;\n\t\t}\n\t\treturn res;\n\t}\n\tstring symbol = \"\";\n\twhile(s[indx] != '[') {\n\t\tsymbol += s[indx];\n\t\tindx++;\n\t}\n\tindx++;\n\tll num = recursive_parse(s, indx, symbol_table, vals);\n\tindx++;\n\n\tif(num < 0) return -1;\n\tif(vals.find(make_pair(symbol, num)) == vals.end() /*not assigned */\n\t\t\t|| num >= symbol_table[symbol] /* out of range*/) {\n\t\treturn -1;\n\t}\n\treturn vals[make_pair(symbol,num)];\n}\n\nint main() {\n\twhile(1) {\n\t\tstring s_tmp;\n\t\tvector<string> program;\n\t\twhile(cin >> s_tmp, s_tmp != \".\") {\n\t\t\tprogram.push_back(s_tmp);\n\t\t}\n\t\tif(!program.size()) {\n\t\t\t// end of the input\n\t\t\tbreak;\n\t\t}\n\t\tmap<string,ll> symbol_table;\n\t\tmap<pair<string,ll>,ll> vals;\n\t\tint res = 0;\n\t\tfor(int line = 0; line < program.size(); line++) {\n\t\t\tstring s = program[line];\n\t\t\tif(s.find('=') == string::npos) {\n\t\t\t\tstring symbol = string(s.begin(), s.begin() + s.find('['));\n\t\t\t\tll size = atol(string(s.begin() + s.find('[')+1, s.begin() + s.find(']')).c_str());\n\t\t\t\tsymbol_table[symbol] = size;\n\t\t\t}\n\t\t\telse {\n\t\t\t\tstring l = string(s.begin(), s.begin()+s.find('='));\n\t\t\t\tstring r = string(s.begin()+s.find('=')+1, s.end());\n\t\t\t\tll l_val, r_val;\n\n\t\t\t\tint arg_indx = 0;\n\t\t\t\tstring symbol = string(l.begin(), l.begin() + l.find('['));\n\t\t\t\targ_indx = l.find('[')+1;\n\n\t\t\t\tl_val = recursive_parse(l,arg_indx,symbol_table,vals);\n\t\t\t\tassert(arg_indx+1 == l.size());\n\t\t\t\targ_indx = 0;\n\t\t\t\tr_val = recursive_parse(r,arg_indx,symbol_table,vals);\n\t\t\t\tassert(arg_indx == r.size());\n\n\t\t\t\tif(l_val < 0 || r_val < 0 || symbol_table.find(symbol) == symbol_table.end() \n\t\t\t\t\t\t|| symbol_table[symbol] <= l_val) {\n\t\t\t\t\tres = line+1;\n\t\t\t\t\tbreak;\n\t\t\t\t}\n\n\t\t\t\tvals[make_pair(symbol, l_val)] = r_val;\n\t\t\t}\n\t\t}\n\t\tcout << res << endl;\n\t}\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3364, "score_of_the_acc": -0.0066, "final_rank": 8 }, { "submission_id": "aoj_1282_2465313", "code_snippet": "#include \"bits/stdc++.h\"\nusing namespace std;\n\n#define rep(i, n) for (int i = 0; i < (n); i ++) \n\nvector<pair<map<int, int>, int>> p(52);\n\nint get_value(string x) {\n if ('0' <= x[0] && x[0] <= '9') {\n return stoi(x);\n }\n if (p[x[0] + (x[0] <= 'Z' ? -'A' : 26 - 'a')].second == 0) return -1;\n string in = x.substr(2, x.size() - 3);\n int index = get_value(in);\n if (index == -1) return -1;\n if (p[x[0] + (x[0] <= 'Z' ? -'A' : 26 - 'a')].second - 1 < index) return -1;\n auto it = p[x[0] + (x[0] <= 'Z' ? -'A' : 26 - 'a')].first.find(index);\n if (it == p[x[0] + (x[0] <= 'Z' ? -'A' : 26 - 'a')].first.end()) return -1;\n return p[x[0] + (x[0] <= 'Z' ? -'A' : 26 - 'a')].first[index];\n}\n\nint main() {\n bool end = false;\n while (true) {\n int ans = 0, cnt = 0;\n p.clear();\n p.resize(52);\n for (int i = 0; i < 52; i ++) p[i].second = 0;\n bool found = false;\n while (true) {\n cnt ++;\n string s;\n cin >> s;\n if (s == \".\" && end) { \n goto fin;\n }\n if (s == \".\") { \n break;\n }\n end = false;\n bool eq = false;\n for (int i = 0; i < s.size(); i ++) if (s[i] == '=') eq = true;\n if (!eq) {\n char alph = s[0];\n s = s.substr(2, s.size() - 3);\n p[alph + (alph <= 'Z' ? -'A' : 26 - 'a')].second = stoi(s);\n } else {\n //cerr << \"eq\" << endl;\n string left, right;\n int sp = 0;\n while (s[sp] != '=') sp ++;\n left = s.substr(0, sp);\n right = s.substr(sp + 1);\n //cerr << left << ' ' << right << endl;\n int res = get_value(right);\n //cerr << \"res = \" << res << endl;\n int index = get_value(left.substr(2, left.size() - 3)); \n //cerr << res << ' ' << index << endl;\n if ((res == -1 || index == -1) && !found) {\n //cerr << \"found\" << endl;\n ans = cnt;\n found = true;\n }\n if (p[left[0] + (left[0] <= 'Z' ? -'A' : 26 - 'a')].second - 1 < index && !found) {\n ans = cnt;\n found = true;\n }\n p[left[0] + (left[0] <= 'Z' ? -'A' : 26 - 'a')].first[index] = res;\n }\n }\n cout << ans << endl;\n end = true;\n }\n fin:;\n return 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3212, "score_of_the_acc": 0, "final_rank": 1 }, { "submission_id": "aoj_1282_2454943", "code_snippet": "#include \"bits/stdc++.h\"\nusing namespace std;\ntypedef long long ll;\ntypedef pair<int,int> pii;\ntypedef pair<string,int> psi;\n#define rep(i,n) for(ll i=0;i<(ll)(n);i++)\n#define rep2(i,a,b) for(ll i=(a);i<(b);i++)\n#define all(a) (a).begin(),(a).end()\n#define pb emplace_back\n#define INF 2147483647\n//#define INF (1LL<<59)\n\ntypedef string::const_iterator State;\n\n\nmap<psi,int> arr;\nmap<string,int> a_size;\n\nint get_num(psi x){\n if(x.first==\"NUMBER\")return x.second;\n else{\n assert(arr.count(x)!=0);\n return arr[x];\n }\n}\n\n\nint num(State &beg){\n int ret = 0;\n while( isdigit(*beg) ){\n ret*=10;\n ret+=*beg-'0';\n beg++;\n }\n return ret;\n}\n\n\npsi eval(State &beg){\n psi ret;\n if( isdigit(*beg) ){\n int p = num(beg);\n return psi(\"NUMBER\",p);\n }\n \n ret.first = *beg; //テゥツ?催・ツ按療・ツ青?\n beg++;\n \n assert(*beg=='[');\n beg++;\n \n psi res = eval(beg);\n \n assert(*beg==']');\n beg++;\n \n if( res.first == \"NUMBER\" ){\n ret.second = res.second;\n }else{\n if(a_size.count(res.first)==0)throw -13; //ティツゥツイテ・ツスツ禿ゥツ?催・ツ按療ァツ?。テ」ツ??\n if(a_size[res.first]<=res.second)throw -23; //テゥツ?催・ツ按療・ツ、ツ姪・ツ渉づァツ?ァ\n \n if( arr.count(res)==0 )throw -33; //テヲツ慊ェテ・ツョツ堙ァツセツゥテ・ツ?、テ・ツ渉づァツ?ァ\n \n ret.second = get_num(res);\n }\n return ret;\n}\n\nint main(){\n string s;\n while(cin>>s && s!=\".\"){\n arr.clear();\n a_size.clear();\n vector<string> vs;\n vs.pb(s);\n while(cin>>s && s!=\".\") vs.pb(s);\n \n bool f=false;\n \n rep(i,vs.size()){\n int pos = -1;\n rep(j,vs[i].size()){\n if(vs[i][j] == '=')pos = j;\n }\n \n if( pos==-1 ){\n State beg = vs[i].begin();\n psi res = eval(beg);\n a_size[res.first] = res.second;\n \n }else{\n string l = vs[i].substr(0,pos);\n string r = vs[i].substr(pos+1);\n try{\n State beg = l.begin();\n psi L = eval(beg);\n \n beg = r.begin();\n psi R = eval(beg);\n \n if(a_size.count(L.first)==0)throw -11; //ティツゥツイテ・ツスツ禿ゥツ?催・ツ按療ァツ?。テ」ツ??\n if(a_size[L.first]<=L.second)throw -21; //テゥツ?催・ツ按療・ツ、ツ姪・ツ渉づァツ?ァ\n\n if(R.first != \"NUMBER\" && a_size.count(R.first)==0)throw -12; //ティツゥツイテ・ツスツ禿ゥツ?催・ツ按療ァツ?。テ」ツ??\n if(R.first != \"NUMBER\" && a_size[R.first]<=R.second)throw -22; //テゥツ?催・ツ按療・ツ、ツ姪・ツ渉づァツ?ァ\n if(R.first != \"NUMBER\" && arr.count(R)==0 )throw -31; //テヲツ慊ェテ・ツョツ堙ァツセツゥテ・ツ?、テ・ツ渉づァツ?ァ\n \n arr[psi(L)] = get_num(R);\n }catch(int excep){\n cout<<i+1<<endl;\n f = true;\n break;\n }\n }\n }\n if(!f)cout<<0<<endl;\n }\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3368, "score_of_the_acc": -0.0068, "final_rank": 9 }, { "submission_id": "aoj_1282_2454939", "code_snippet": "#include <algorithm>\n#include <bitset>\n#include <cassert>\n#include <cctype>\n#include <cmath>\n#include <cstdio>\n#include <cstdlib>\n#include <cstring>\n#include <ctime>\n#include <deque>\n#include <functional>\n#include <iomanip>\n#include <iostream>\n#include <list>\n#include <map>\n#include <numeric>\n#include <queue>\n#include <set>\n#include <sstream>\n#include <stack>\n#include <string>\n#include <utility>\n#include <vector>\n\n#define REP(i,s,n) for(int i=(int)(s);i<(int)(n);i++)\n\nusing namespace std;\ntypedef long long int ll;\ntypedef vector<int> VI;\ntypedef vector<ll> VL;\ntypedef pair<int, int> PI;\n\nPI parse_num(string expr) {\n int cur = 0;\n int pos = 0;\n int n = (int) expr.length();\n while (pos < n) {\n if (not isdigit(expr[pos])) {\n return pos == 0 ? PI(-1, -1) : PI(pos, cur);\n }\n cur *= 10;\n cur += expr[pos] - '0';\n pos++;\n }\n return n == 0 ? PI(-1, -1) : PI(n, cur);\n}\n\nPI eval_decl(map<string, int> &ary, \n\t map<string, map<int, int> > &asgn,\n\t string expr) {\n int n = expr.length();\n string an = expr.substr(0, 1);\n if (n <= 2 || expr[1] != '[') {\n return PI(-1, -1);\n }\n PI sz = parse_num(expr.substr(2));\n if (sz.first == -1) {\n return PI(-1, -1);\n }\n int len = sz.first + 2;\n if (n <= len || expr[len] != ']') {\n return PI(-1, -1);\n }\n // Should be exact\n if (n != len + 1) {\n return PI(-1, -1);\n }\n ary[an] = sz.second;\n return PI(len + 1, 0);\n}\n\nPI eval_expr(map<string, int> &ary, \n\t map<string, map<int, int> > &asgn,\n\t string expr) {\n PI num = parse_num(expr);\n if (num.first >= 0) {\n return num;\n }\n // a[idx]\n string an = expr.substr(0, 1);\n int n = expr.length();\n if (n <= 2 || expr[1] != '[') {\n return PI(-1, -1);\n }\n PI idx = eval_expr(ary, asgn, expr.substr(2));\n if (idx.first == -1) {\n return PI(-1, -1);\n }\n int len = 2 + idx.first;\n if (n <= len || expr[len] != ']') {\n return PI(-1, -1);\n }\n if (ary.count(an) == 0) {\n return PI(-1, -1);\n }\n int arylen = ary[an];\n if (idx.second < 0 || idx.second >= arylen || asgn[an].count(idx.second) == 0) {\n return PI(-1, -1);\n }\n return PI(len + 1, asgn[an][idx.second]);\n \n}\n\nPI eval_asgn(map<string, int> &ary, \n\t map<string, map<int, int> > &asgn,\n\t string expr) {\n string an = expr.substr(0, 1);\n int n = expr.length();\n if (n <= 2 || expr[1] != '[') {\n return PI(-1, -1);\n }\n PI idx = eval_expr(ary, asgn, expr.substr(2));\n if (idx.first == -1) {\n return PI(-1, -1);\n }\n int len = 2 + idx.first;\n if (n <= len + 1 || expr[len] != ']' || expr[len + 1] != '=') {\n return PI(-1, -1);\n }\n PI elem = eval_expr(ary, asgn, expr.substr(len + 2));\n if (elem.first == -1) {\n return PI(-1, -1);\n }\n if (ary.count(an) == 0) {\n return PI(-1, -1);\n }\n int arylen = ary[an];\n if (idx.second < 0 || idx.second >= arylen) {\n return PI(-1, -1);\n }\n asgn[an][idx.second] = elem.second;\n return PI(len + 2 + elem.first, 0);\n}\n\nPI eval(map<string, int> &ary, \n\t map<string, map<int, int> > &asgn,\n\t string expr) {\n PI res = eval_decl(ary, asgn, expr);\n if (res.first >= 0) {\n return res;\n }\n return eval_asgn(ary, asgn, expr);\n}\n\n\nvoid solve(const vector<string> &pool) {\n map<string, int> ary;\n map<string, map<int, int> > asgn;\n REP(i, 0, pool.size()) {\n PI res = eval(ary, asgn, pool[i]);\n if (res.first == -1) {\n cout << i + 1 << \"\\n\";\n return;\n }\n }\n cout << 0 << \"\\n\";\n}\n\n\nint main(void) {\n ios::sync_with_stdio(false);\n cin.tie(0);\n vector<string> pool;\n string cur;\n while(cin >> cur) {\n if (cur == \".\") {\n if (pool.empty()) {\n\treturn 0;\n }\n solve(pool);\n pool.clear();\n } else {\n pool.push_back(cur);\n }\n }\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3240, "score_of_the_acc": -0.0012, "final_rank": 5 } ]
aoj_1278_cpp
Problem D: Lowest Pyramid You are constructing a triangular pyramid with a sheet of craft paper with grid lines. Its base and sides are all of triangular shape. You draw the base triangle and the three sides connected to the base on the paper, cut along the outer six edges, fold the edges of the base, and assemble them up as a pyramid. You are given the coordinates of the base's three vertices, and are to determine the coordinates of the other three. All the vertices must have integral X- and Y-coordinate values between -100 and +100 inclusive. Your goal is to minimize the height of the pyramid satisfying these conditions. Figure 3 shows some examples. Figure 3: Some craft paper drawings and side views of the assembled pyramids Input The input consists of multiple datasets, each in the following format. X 0 Y 0 X 1 Y 1 X 2 Y 2 They are all integral numbers between -100 and +100 inclusive. ( X 0 , Y 0 ), ( X 1 , Y 1 ), ( X 2 , Y 2 ) are the coordinates of three vertices of the triangular base in counterclockwise order. The end of the input is indicated by a line containing six zeros separated by a single space. Output For each dataset, answer a single number in a separate line. If you can choose three vertices ( X a , Y a ), ( X b , Y b ) and ( X c , Y c ) whose coordinates are all integral values between -100 and +100 inclusive, and triangles ( X 0 , Y 0 )-( X 1 , Y 1 )-( X a , Y a ), ( X 1 , Y 1 )-( X 2 , Y 2 )-( X b , Y b ), ( X 2 , Y 2 )-( X 0 , Y 0 )-( X c , Y c ) and ( X 0 , Y 0 )-( X 1 , Y 1 )-( X 2 , Y 2 ) do not overlap each other (in the XY-plane), and can be assembled as a triangular pyramid of positive (non-zero) height, output the minimum height among such pyramids. Otherwise, output -1. You may assume that the height is, if positive (non-zero), not less than 0.00001. The output should not contain an error greater than 0.00001. Sample Input 0 0 1 0 0 1 0 0 5 0 2 5 -100 -100 100 -100 0 100 -72 -72 72 -72 0 72 0 0 0 0 0 0 Output for the Sample Input 2 1.49666 -1 8.52936
[ { "submission_id": "aoj_1278_10067849", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\n\nusing ll = long long;\n#define rep(i,n) for(int i=0;i<n;i++)\n#define rep2(i,s,t) for(int i=s;i<t;i++)\n\nll sqrtz(ll x){\n if(x<0)return -201;\n ll z=sqrt(x)+2;\n while(z*z>x){\n z--;\n }\n return z;\n}\nbool is_sq(ll x){\n ll z=sqrtz(x);\n return (z*z==x);\n}\n\nusing Point=array<ll,2>;\n\nll cross(Point a, Point b) {\n return a[0] * b[1] - a[1] * b[0];\n}\n\nint ccw(Point a, Point b, Point c) {\n Point p = {b[0] - a[0],b[1]-a[1]};\n Point q = {c[0] - a[0],c[1]-a[1]};\n\n if(cross(p, q) > 0) return 1;\n if(cross(p, q) < 0) return -1;\n return 0;\n}\n\nusing Real = double;\nusing Pointd = complex<Real>;\nReal eps = 1e-6;\n\nint Round(Real x) { return static_cast<int>(round(x)); }\n\nbool equal(Pointd a, Pointd b) { return abs(a - b) < eps; }\n\nstruct Line {\n Pointd a, b;\n Line() = default;\n Line(Pointd a, Pointd b) : a(a), b(b) {}\n};\n\nusing Segment = Line;\n\nReal norm(Pointd p) { return pow(p.real(), 2) + pow(p.imag(), 2); }\n\nReal dot(Pointd a, Pointd b) { return a.real() * b.real() + a.imag() * b.imag(); }\nReal crossd(Pointd a, Pointd b) {\n return a.real() * b.imag() - a.imag() * b.real();\n}\n\nPointd projection(Line L, Pointd p) {\n Real t = dot(p - L.a, L.a - L.b) / norm(L.a - L.b);\n return L.a + (L.a - L.b) * t;\n}\n\nPointd crossPoint(Line s, Line t) {\n Real d1 = crossd(s.b - s.a, t.b - t.a);\n Real d2 = crossd(s.b - s.a, s.b - t.a);\n if(equal(abs(d1), 0.0) && equal(abs(d2), 0.0)) return t.a;\n return t.a + (t.b - t.a) * (d2 / d1);\n}\n\ndouble height(array<Point,3> V,array<Point,3> U){\n array<Pointd,3> Vd,Ud;\n rep(i,3)Vd[i]=Pointd(V[i][0],V[i][1]); \n rep(i,3)Ud[i]=Pointd(U[i][0],U[i][1]);\n array<Line,3> L,LD;\n rep(i,3)L[i]=Line(Vd[i],Vd[(i+1)%3]);\n rep(i,3)LD[i]=Line(Ud[i],projection(L[i],Ud[i]));\n \n array<Pointd,3> PD={\n crossPoint(LD[0],LD[1]),\n crossPoint(LD[1],LD[2]),\n crossPoint(LD[2],LD[0])\n };\n\n if((equal(PD[0],PD[1])&&equal(PD[1],PD[2])&&equal(PD[2],PD[0]))){\n \n array<Real,3> D={\n norm(PD[0]-Vd[0]),\n norm(PD[0]-Vd[1]),\n norm(PD[0]-Vd[2])\n };\n array<Real,3> E={\n norm(Ud[0]-Vd[0]),\n norm(Ud[1]-Vd[1]),\n norm(Ud[2]-Vd[2])\n };\n array<Real,3> H;\n rep(k,3){\n if(D[k]>E[k]-(1e-12))return -1;\n H[k]=sqrt(E[k]-D[k]);\n }\n if(abs(H[0]-H[1])>1e-6||abs(H[2]-H[1])>1e-6)return -1;\n if(H[0]<1e-4)return -1;\n return H[0];\n }\n else{\n return -1;\n }\n}\n\ndouble solve(array<Point,3> V){\n \n ll cnt=0;\n double an=1e18;\n ll NM=100;\n for(ll x=-NM;x<=NM;x++){\n for(ll y=-NM;y<=NM;y++){\n ll dx=x-V[1][0];\n ll dy=y-V[1][1];\n ll rr=dx*dx+dy*dy;\n if(ccw({x,y},V[1],V[0])<=0)continue;\n for(ll a=-NM;a<=NM;a++){\n ll da=a-V[1][0];\n if(!is_sq(rr-da*da))continue;\n ll db=sqrtz(rr-da*da);\n for(ll b:{V[1][1]-db,V[1][1]+db}){\n if(b<-NM||b>NM)continue;\n if(ccw({a,b},V[2],V[1])<=0)continue;\n\n ll ss=(a-V[2][0])*(a-V[2][0])+(b-V[2][1])*(b-V[2][1]);\n ll tt=(x-V[0][0])*(x-V[0][0])+(y-V[0][1])*(y-V[0][1]);\n \n ll dvx=2*(V[2][0]-V[0][0]);\n ll dvy=2*(V[2][1]-V[0][1]);\n\n ll dvd=tt-ss-V[0][0]*V[0][0]+V[2][0]*V[2][0]-V[0][1]*V[0][1]+V[2][1]*V[2][1];\n dvd*=-1;\n ll D=abs(dvx*V[0][0]+dvy*V[0][1]+dvd);\n ll nm=dvx*dvx+dvy*dvy;\n if(is_sq(nm*tt-D*D)){\n ll u=sqrtz(nm*tt-D*D);\n\n ll px=-dvy*u;\n ll py=dvx*u;\n rep(_,2){\n px*=-1;\n py*=-1;\n if((dvx*D+px)%nm==0&&(dvy*D+py)%nm==0){\n ll p=(dvx*D+px)/nm+V[0][0];\n ll q=(dvy*D+py)/nm+V[0][1];\n if(ccw({p,q},V[0],V[2])<=0)continue;\n if(p<-NM||p>NM||q<-NM||q>NM)continue;\n \n double res=height(V,{Point({x,y}),Point({a,b}),Point({p,q})});\n if(res<0.0)continue;\n if(res>eps)an=min(an,res);\n }\n }\n }\n }\n }\n }\n }\n return an;\n}\n\nvoid solsol(array<Point,3> V){\n double an=1e18;\n rep(i,3){\n V={V[1],V[2],V[0]};\n an=min(an,solve(V)); \n }\n if(an>1e17)cout<<-1<<endl;\n else cout<<an<<endl;\n}\n\nint main() {\n\n cin.tie(nullptr);\n ios::sync_with_stdio(false);\n\n std::cout<<fixed<<setprecision(7);\n array<Point,3> V;\n while(cin>>V[0][0]>>V[0][1]>>V[1][0]>>V[1][1]>>V[2][0]>>V[2][1],(V[0]!=V[1]))solsol(V);\n}", "accuracy": 1, "time_ms": 4680, "memory_kb": 3492, "score_of_the_acc": -0.8018, "final_rank": 7 }, { "submission_id": "aoj_1278_10067843", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\n\nusing ll = long long;\n#define rep(i,n) for(int i=0;i<n;i++)\n#define rep2(i,s,t) for(int i=s;i<t;i++)\n\nll sqrtz(ll x){\n if(x<0)return -201;\n ll z=sqrt(x)+2;\n while(z*z>x){\n z--;\n }\n return z;\n}\nbool is_sq(ll x){\n ll z=sqrtz(x);\n return (z*z==x);\n}\n\nusing Point=array<ll,2>;\n\nll cross(Point a, Point b) {\n return a[0] * b[1] - a[1] * b[0];\n}\n\nint ccw(Point a, Point b, Point c) {\n Point p = {b[0] - a[0],b[1]-a[1]};\n Point q = {c[0] - a[0],c[1]-a[1]};\n\n if(cross(p, q) > 0) return 1;\n if(cross(p, q) < 0) return -1;\n return 0;\n}\n\n\n\n\n\n\n/*\n- `Point p(x, y)` で座標/ベクトルを宣言できる\n- `cout << fixed << setprecision(10);` で小数出力の桁数を指定できる\n*/\nusing Real = double;\nusing Pointd = complex<Real>;\nusing Poly = vector<Pointd>;\nReal eps = 1e-6;\n\nint Round(Real x) { return static_cast<int>(round(x)); }\n\n// 同じか判定\nbool equal(Pointd a, Pointd b) { return abs(a - b) < eps; }\n\nstruct Line {\n // 点 a, b を通る直線\n Pointd a, b;\n Line() = default;\n Line(Pointd a, Pointd b) : a(a), b(b) {}\n Line(Real A, Real B, Real C) {\n // 直線の方程式 Ax + By = C;\n if(equal(A, 0)) a = Pointd(0, C / B), b = Pointd(1, C / B);\n else if(equal(B, 0)) a = Pointd(C / A, 0), b = Pointd(C / A, 1);\n else a = Pointd(0, C / B), b = Pointd(C / A, 0);\n }\n};\n\nusing Segment = Line;\n\nReal norm(Pointd p) { return pow(p.real(), 2) + pow(p.imag(), 2); }\n// 単位ベクトル\nPointd unitVector(Pointd p) { return p / abs(p); }\n// 内積(dot product): a・b=|a||b|cosΘ\nReal dot(Pointd a, Pointd b) { return a.real() * b.real() + a.imag() * b.imag(); }\n// 外積(cross product): a×b=|a||b|sinΘ\nReal crossd(Pointd a, Pointd b) {\n return a.real() * b.imag() - a.imag() * b.real();\n}\n\n// 射影(projection): 直線 L に点 p から引いた垂線の足を求める\nPointd projection(Line L, Pointd p) {\n Real t = dot(p - L.a, L.a - L.b) / norm(L.a - L.b);\n return L.a + (L.a - L.b) * t;\n}\n\n// 2 直線の交点\nPointd crossPoint(Line s, Line t) {\n Real d1 = crossd(s.b - s.a, t.b - t.a);\n Real d2 = crossd(s.b - s.a, s.b - t.a);\n if(equal(abs(d1), 0.0) && equal(abs(d2), 0.0)) return t.a;\n return t.a + (t.b - t.a) * (d2 / d1);\n}\n\n\n\ndouble height(array<Point,3> V,array<Point,3> U){\n array<Pointd,3> Vd,Ud;\n rep(i,3)Vd[i]=Pointd(V[i][0],V[i][1]); \n rep(i,3)Ud[i]=Pointd(U[i][0],U[i][1]); \n array<Line,3> L={\n Line(Vd[0],Vd[1]),\n Line(Vd[1],Vd[2]),\n Line(Vd[2],Vd[0])\n };\n\n array<Line,3> LD={\n Line(Ud[0],projection(L[0],Ud[0])),\n Line(Ud[1],projection(L[1],Ud[1])),\n Line(Ud[2],projection(L[2],Ud[2]))\n };\n\n array<Pointd,3> PD={\n crossPoint(LD[0],LD[1]),\n crossPoint(LD[1],LD[2]),\n crossPoint(LD[2],LD[0])\n };\n\n if((equal(PD[0],PD[1])&&equal(PD[1],PD[2])&&equal(PD[2],PD[0]))){\n \n array<Real,3> D={\n norm(PD[0]-Vd[0]),\n norm(PD[0]-Vd[1]),\n norm(PD[0]-Vd[2])\n };\n array<Real,3> E={\n norm(Ud[0]-Vd[0]),\n norm(Ud[1]-Vd[1]),\n norm(Ud[2]-Vd[2])\n };\n array<Real,3> H;\n rep(k,3){\n if(D[k]>E[k]-(1e-12))return -1;\n H[k]=sqrt(E[k]-D[k]);\n }\n if(abs(H[0]-H[1])>1e-6||abs(H[2]-H[1])>1e-6)return -1;\n if(H[0]<1e-4)return -1;\n return H[0];\n }\n else{\n return -1;\n }\n}\n\ndouble solve(array<Point,3> V){\n \n \n ll cnt=0;\n double an=1e18;\n ll NM=100;\n for(ll x=-NM;x<=NM;x++){\n for(ll y=-NM;y<=NM;y++){\n ll dx=x-V[1][0];\n ll dy=y-V[1][1];\n ll rr=dx*dx+dy*dy;\n if(ccw({x,y},V[1],V[0])<=0)continue;\n for(ll a=-NM;a<=NM;a++){\n ll da=a-V[1][0];\n if(!is_sq(rr-da*da))continue;\n ll db=sqrtz(rr-da*da);\n for(ll b:{V[1][1]-db,V[1][1]+db}){\n if(b<-NM||b>NM)continue;\n if(ccw({a,b},V[2],V[1])<=0)continue;\n\n ll ss=(a-V[2][0])*(a-V[2][0])+(b-V[2][1])*(b-V[2][1]);\n ll tt=(x-V[0][0])*(x-V[0][0])+(y-V[0][1])*(y-V[0][1]);\n \n ll dvx=2*(V[2][0]-V[0][0]);\n ll dvy=2*(V[2][1]-V[0][1]);\n\n ll dvd=tt-ss-V[0][0]*V[0][0]+V[2][0]*V[2][0]-V[0][1]*V[0][1]+V[2][1]*V[2][1];\n dvd*=-1;\n //p*dvx+q*dvy-dvd=0;\n\n ll D=abs(dvx*V[0][0]+dvy*V[0][1]+dvd);\n ll nm=dvx*dvx+dvy*dvy;\n if(is_sq(nm*tt-D*D)){\n ll u=sqrtz(nm*tt-D*D);\n\n ll px=-dvy*u;\n ll py=dvx*u;\n rep(_,2){\n px*=-1;\n py*=-1;\n if((dvx*D+px)%nm==0&&(dvy*D+py)%nm==0){\n ll p=(dvx*D+px)/nm+V[0][0];\n ll q=(dvy*D+py)/nm+V[0][1];\n if(ccw({p,q},V[0],V[2])<=0)continue;\n if(p<-NM||p>NM||q<-NM||q>NM)continue;\n \n double res=height(V,{Point({x,y}),Point({a,b}),Point({p,q})});\n if(res<0.0)continue;\n if(res>eps)an=min(an,res);\n \n \n // std::cout<<x<<\" \"<<y<<\" \"<<a<<\" \"<<b<<\" \"<<p<<\" \"<<q<<\" \"<<res<<endl;\n // return;\n cnt++;\n }\n }\n\n // cnt++;\n }\n }\n }\n }\n }\n return an;\n}\n\nvoid solsol(array<Point,3> V){\n double an=1e18;\n rep(i,3){\n V={V[1],V[2],V[0]};\n an=min(an,solve(V)); \n }\n if(an>1e17)cout<<-1<<endl;\n else cout<<an<<endl;\n}\n\nint main() {\n\n cin.tie(nullptr);\n ios::sync_with_stdio(false);\n\n std::cout<<fixed<<setprecision(7);\n array<Point,3> V;\n while(cin>>V[0][0]>>V[0][1]>>V[1][0]>>V[1][1]>>V[2][0]>>V[2][1],(V[0]!=V[1]))solsol(V);\n}", "accuracy": 1, "time_ms": 4590, "memory_kb": 3596, "score_of_the_acc": -0.7999, "final_rank": 6 }, { "submission_id": "aoj_1278_8039500", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\ntypedef long long LL;\ntypedef long double LD;\nconst LD INF = 1e18;\nconst LD eps = 1e-10;\n\ninline int sgn(LD x) {\n return x > eps? 1 : x < -eps? -1 : 0;\n}\n\nstruct Point {\n LD x, y;\n Point(LD _x = 0, LD _y = 0): x(_x), y(_y) {}\n bool operator == (const Point &rhs) const {\n return sgn(x - rhs.x) == 0 && sgn(y - rhs.y) == 0;\n }\n Point operator + (const Point &rhs) const {\n return Point(x + rhs.x, y + rhs.y);\n }\n Point operator - (const Point &rhs) const {\n return Point(x - rhs.x, y - rhs.y);\n }\n Point operator * (const LD &rhs) const {\n return Point(x * rhs, y * rhs);\n }\n Point operator / (const LD &rhs) const {\n return Point(x / rhs, y / rhs);\n }\n LD operator * (const Point &rhs) const {\n return x * rhs.x + y * rhs.y;\n }\n LD operator ^ (const Point &rhs) const {\n return x * rhs.y - y * rhs.x;\n }\n LD len2() {\n return x * x + y * y;\n }\n LD len() {\n return sqrtl(len2());\n }\n Point unit() {\n return (*this) / len();\n }\n} A, B, C;\nvector<Point> vec[80005];\nvector<int> valid;\n\ninline Point Proj(Point p, Point u, Point v) {\n Point dir = (v - u).unit();\n return u + dir * (dir * (p - u));\n}\n\nPoint LineInter(Point u1, Point v1, Point u2, Point v2) {\n LD a1 = (v2 - u2) ^ (u1 - u2), a2 = (v2 - u2) ^ (v1 - u2);\n return (u1 * a2 - v1 * a1) / (a2 - a1);\n}\n\ninline bool In(Point p) {\n return (abs(p.x) <= 100 && abs(p.y) <= 100);\n}\n\ninline bool Intri(Point p, Point a, Point b, Point c) {\n return sgn((b - a) ^ (p - a)) > 0\n && sgn((c - b) ^ (p - b)) > 0\n && sgn((a - c) ^ (p - c)) > 0;\n}\n\nLD Try(Point D, Point E) {\n if (!In(D)) return INF;\n if (sgn((B - A) ^ (D - A)) >= 0) return INF;\n if (!In(E)) return INF;\n if (sgn((C - A) ^ (E - A)) <= 0) return INF;\n Point Dp = Proj(D, A, B), Ep = Proj(E, A, C);\n Point Tp = LineInter(D, Dp, E, Ep);\n if (sgn((Tp - Dp).len2() - (D - Dp).len2()) >= 0) return INF;\n if (sgn((Tp - Ep).len2() - (E - Ep).len2()) >= 0) return INF;\n LD h = sqrt((D - A).len2() - (Tp - A).len2());\n for (auto e : vec[(int)(D - B).len2()]) {\n Point F = B + e;\n for (auto f : vec[(int)(E - C).len2()]) {\n if (C + f == F) {\n if (In(F) && sgn((B - C) ^ (F - C)) > 0 && !Intri(F, A, D, B) && !Intri(F, A, C, E)) {\n return h;\n }\n }\n }\n }\n return INF;\n}\n\nvoid prework() {\n for (int i = 0; i <= 200; ++i) {\n for (int j = 0; j <= 200; ++j) {\n int l2 = i * i + j * j;\n vec[l2].push_back(Point(i, j));\n if (i != 0) {\n vec[l2].push_back(Point(-i, j));\n }\n if (j != 0) {\n vec[l2].push_back(Point(i, -j));\n }\n if (i != 0 && j != 0) {\n vec[l2].push_back(Point(-i, -j));\n }\n }\n }\n for (int i = 0; i <= 80000; ++i) {\n if (vec[i].size()) {\n valid.push_back(i);\n }\n }\n}\n\nvoid solve() {\n LD ans = INF;\n for (auto l2 : valid) {\n for (auto a : vec[l2]) {\n for (auto b : vec[l2]) {\n ans = min(ans, Try(A + a, A + b));\n }\n }\n }\n if (ans > 1e17) {\n puts(\"-1\");\n } else {\n printf(\"%.10Lf\\n\", ans);\n }\n}\n\nint main() {\n prework();\n int a, b, c, d, e, f;\n while (scanf(\"%d %d %d %d %d %d\", &a, &b, &c, &d, &e, &f)) {\n if (a == 0 && b == 0 && c == 0 && d == 0 && e == 0 && f == 0) {\n break;\n }\n A = Point(a, b);\n B = Point(c, d);\n C = Point(e, f);\n solve();\n }\n return 0;\n}", "accuracy": 1, "time_ms": 4220, "memory_kb": 11160, "score_of_the_acc": -1.4912, "final_rank": 20 }, { "submission_id": "aoj_1278_4805814", "code_snippet": "#include<bits/stdc++.h>\ntypedef long long int ll;\ntypedef unsigned long long int ull;\n#define BIG_NUM 2000000000\n#define HUGE_NUM 1000000000000000000\n#define MOD 1000000007\n#define EPS 0.000000001\nusing namespace std;\n\n\n\n#define MAX 80005\n#define SIZE 3\n\nstruct Point{\n\tPoint(double arg_x,double arg_y){\n\t\tx = arg_x;\n\t\ty = arg_y;\n\t}\n\n\tPoint(){\n\t\tx = y = 0.0;\n\t}\n\n\tPoint operator + (Point p){ return Point(x+p.x,y+p.y); }\n\tPoint operator - (Point p){ return Point(x-p.x,y-p.y);}\n\tPoint operator * (double a){ return Point(a*x,a*y); }\n\tPoint operator / (double a){ return Point(x/a,y/a); }\n\n\tdouble abs(){ return sqrt(norm()); }\n\tdouble norm(){ return x*x + y*y; }\n\n\tbool operator<(const Point &p) const{\n\t\treturn x != p.x? x < p.x: y < p.y;\n\t}\n\n\tbool operator == (const Point &p) const{\n\t\treturn fabs(x-p.x) < EPS && fabs(y-p.y) < EPS;\n\t}\n\n\n\tvoid debug(){\n\n\t\tprintf(\"(%.3lf,%.3lf)\\n\",x,y);\n\t}\n\n\tdouble x,y;\n};\n\ntypedef Point Vector;\ntypedef vector<Point> Polygon;\n\ndouble calc_dist(Point A,Point B){\n\n\treturn sqrt((A.x-B.x)*(A.x-B.x)+(A.y-B.y)*(A.y-B.y));\n}\nstruct Line{\n\tLine(){\n\n\t}\n\tLine(Point a,Point b){\n\t\tp[0] = a;\n\t\tp[1] = b;\n\t}\n\t/*void outPut(){\n\n\t\tprintf(\"(%.3lf,%.3lf)-(%.3lf,%.3lf)\\n\",p[0].x,p[0].y,p[1].x,p[1].y);\n\t}*/\n\tPoint p[2];\n};\n\nstruct LOC{\n\tLOC(){\n\n\t\tint_x = int_y = 0;\n\t}\n\tLOC(int arg_int_x,int arg_int_y){\n\t\tint_x = arg_int_x;\n\t\tint_y = arg_int_y;\n\t}\n\tint int_x,int_y;\n};\n\nLOC base_loc[SIZE];\nPoint base_point[3],p_A,p_B,p_C;\nvector<LOC> V[SIZE][MAX];\n\ndouble norm(Vector a){\n\treturn a.x*a.x+a.y*a.y;\n}\n\ndouble abs(Vector a){\n\treturn sqrt(norm(a));\n}\n\ndouble cross(Vector a,Vector b){\n return a.x*b.y-a.y*b.x;\n}\n\ndouble dot(Vector a,Vector b){\n return a.x*b.x + a.y*b.y;\n}\n\nstatic const int COUNTER_CLOCKWISE = 1;\nstatic const int CLOCKWISE = -1;\nstatic const int ONLINE_BACK = 2;\nstatic const int ONLINE_FRONT = -2;\nstatic const int ON_SEGMENT = 0;\n\nint ccw(Point p0,Point p1,Point p2){\n\n\tVector a = p1 - p0;\n\tVector b = p2 - p0;\n\n\tif(cross(a,b) > EPS)return COUNTER_CLOCKWISE;\n\tif(cross(a,b) < -EPS)return CLOCKWISE;\n\tif(dot(a,b) < -EPS)return ONLINE_BACK;\n\tif(a.norm() < b.norm())return ONLINE_FRONT;\n\n\treturn ON_SEGMENT;\n}\n\n\nint func(double x1,double y1,double x2, double y2, double xp, double yp){\n\tdouble naiseki,norm1,norm2,gaiseki;\n\tnorm1 = sqrt((x2-x1)*(x2-x1)+(y2-y1)*(y2-y1));\n\tnorm2 = sqrt((xp-x1)*(xp-x1)+(yp-y1)*(yp-y1));\n\tnaiseki = (xp-x1)*(x2-x1)+(yp-y1)*(y2-y1);\n\tgaiseki = (x2-x1)*(yp-y1)-(xp-x1)*(y2-y1);\n\tif(gaiseki > EPS){\n\t\treturn 1;\n\t}else if(gaiseki < -EPS){\n\t\treturn -1;\n\t}\n\tif(naiseki < -EPS){\n\t\treturn 2;\n\t}\n\n\tif(norm1 < norm2){\n\t\treturn -2;\n\t}\n\treturn 0;\n}\n\n\n//★★直線ではなく、線分の交差判定★★\nbool is_Cross(Line a,Line b){\n\n\tif(func(a.p[0].x,a.p[0].y,a.p[1].x,a.p[1].y,b.p[0].x,b.p[0].y)*\n\t\t\tfunc(a.p[0].x,a.p[0].y,a.p[1].x,a.p[1].y,b.p[1].x,b.p[1].y) <= 0 &&\n\t\t\tfunc(b.p[0].x,b.p[0].y,b.p[1].x,b.p[1].y,a.p[0].x,a.p[0].y)*\n\t\t\tfunc(b.p[0].x,b.p[0].y,b.p[1].x,b.p[1].y,a.p[1].x,a.p[1].y) <= 0){\n\t\treturn true;\n\t}\n\treturn false;\n}\n\n\nPoint calc_minus(Point a,Point b){\n Point ret;\n\n ret.x = a.x-b.x;\n ret.y = a.y-b.y;\n\n return ret;\n}\n\ndouble calc_len(Vector a){\n return sqrt(a.x*a.x+a.y*a.y);\n}\n\n//線分ではなく直線と点の距離\ndouble getDistanceLP(Line l,Point p){\n return fabs(cross(calc_minus(l.p[1],l.p[0]),calc_minus(p,l.p[0]))/calc_len(calc_minus(l.p[1],l.p[0])));\n}\n\n//点と線分の距離\ndouble getDistanceSP(Line l,Point p){\n\tif(dot(calc_minus(l.p[1],l.p[0]),calc_minus(p,l.p[0])) < 0.0)return calc_len(calc_minus(p,l.p[0]));\n\tif(dot(calc_minus(l.p[0],l.p[1]),calc_minus(p,l.p[1])) < 0.0)return calc_len(calc_minus(p,l.p[1]));\n\treturn getDistanceLP(l,p);\n}\n\n//線分と線分の距離\ndouble getDistance(Line A,Line B){\n\tif(is_Cross(A,B))return 0.0;\n\treturn min(min(getDistanceSP(A,B.p[0]),getDistanceSP(A,B.p[1])),\n\t\t\tmin(getDistanceSP(B,A.p[0]),getDistanceSP(B,A.p[1])));\n}\n\n\n//交点を求める関数\nPoint calc_Cross_Point(double x1,double x2,double x3,double x4,double y1,double y2,double y3,double y4){\n\tPoint ret;\n\tret.x = ((x2-x1)*(y3*(x4-x3)+x3*(y3-y4))-(x4-x3)*(y1*(x2-x1)+x1*(y1-y2)))/((y2-y1)*(x4-x3)-(y4-y3)*(x2-x1));\n\tif(fabs(x1-x2) > EPS){\n\t\tret.y = ((y2-y1)*ret.x+y1*(x2-x1)+x1*(y1-y2))/(x2-x1);\n\t}else{\n\t\tret.y = ((y4-y3)*ret.x+y3*(x4-x3)+x3*(y3-y4))/(x4-x3);\n\t}\n\treturn ret;\n}\n\n//インタフェース関数\nPoint calc_Cross_Point(Point a,Point b,Point c,Point d){\n\treturn calc_Cross_Point(a.x,b.x,c.x,d.x,a.y,b.y,c.y,d.y);\n}\n\n\nPoint calc_Cross_Point(Line A,Line B){\n\n\tif(getDistanceSP(B,A.p[0]) < EPS){\n\n\t\treturn A.p[0];\n\n\t}else if(getDistanceSP(B,A.p[1]) < EPS){\n\n\t\treturn A.p[1];\n\n\t}else if(getDistanceSP(A,B.p[0]) < EPS){\n\n\t\treturn B.p[0];\n\n\t}else if(getDistanceSP(A,B.p[1]) < EPS){\n\n\t\treturn B.p[1];\n\t}\n\n\treturn calc_Cross_Point(A.p[0],A.p[1],B.p[0],B.p[1]);\n}\n\n\nPoint project(Line l,Point p){\n\n\tVector base = l.p[1]-l.p[0];\n\tdouble r = dot(p-l.p[0],base)/norm(base);\n\treturn l.p[0]+base*r;\n}\n\ndouble calc_slope(Line A){\n\n\tif(fabs(A.p[0].x-A.p[1].x) < EPS){\n\n\t\treturn DBL_MAX;\n\n\t}else if(fabs(A.p[0].y-A.p[1].y) < EPS){\n\n\t\treturn 0;\n\n\t}else{\n\n\t\treturn (A.p[1].y-A.p[0].y)/(A.p[1].x-A.p[0].x);\n\t}\n}\n\n//2つのvectorがなすラジアンを求める関数\ndouble calc_rad(Vector vec1,Vector vec2){\n\n\tdouble tmp = dot(vec1,vec2)/(abs(vec1)*abs(vec2));\n\n\tif(fabs(tmp+1.0) < EPS){\n\n\t\treturn M_PI;\n\t}\n\n\tif(fabs(tmp-1.0) < EPS){\n\n\t\treturn 0.0;\n\t}\n\n\treturn acos(dot(vec1,vec2)/(abs(vec1)*abs(vec2)));\n}\n\nPoint calc_Reflection_Point(double x1,double y1,double x2,double y2,double xp,double yp){\n\n\tPoint ret;\n\n\tbool X_FLG = false,Y_FLG = false;\n\tdouble slope;\n\n\tif(y1 == y2){\n\t\tX_FLG = true;\n\t}else if(x1 == x2){\n\t\tY_FLG = true;\n\t}else{\n\t\tslope = (y2-y1)/(x2-x1);\n\t}\n\n\tif(X_FLG){\n\t\tret.x = xp,ret.y=y1;\n\t}else if(Y_FLG){\n\t\tret.x = x1,ret.y = yp;\n\t}else{\n\t\tret.x = (yp*(x2-x1)*(y2-y1)+xp*(x2-x1)*(x2-x1)-y1*(y2-y1)*(x2-x1)+x1*(y2-y1)*(y2-y1))/((y2-y1)*(y2-y1)+(x2-x1)*(x2-x1));\n\t\tret.y = ((x1-x2)*ret.x+yp*(y2-y1)+xp*(x2-x1))/(y2-y1);\n\t}\n\tret.x = 2*ret.x-xp;\n\tret.y = 2*ret.y-yp;\n\n\treturn ret;\n}\n\nPoint calc_Reflection_Point(Line line,Point point){\n\n\treturn calc_Reflection_Point(line.p[0].x,line.p[0].y,line.p[1].x,line.p[1].y,point.x,point.y);\n}\n\n\n\nvoid func(){\n\n\tfor(int i = 0; i < MAX; i++){\n\t\tV[1][i].clear();\n\t\tV[2][i].clear();\n\t}\n\n\tLine line_01,line_12,line_20;\n\n\tline_01.p[0] = base_point[0];\n\tline_01.p[1] = base_point[1];\n\n\tline_12.p[0] = base_point[1];\n\tline_12.p[1] = base_point[2];\n\n\tline_20.p[0] = base_point[2];\n\tline_20.p[1] = base_point[0];\n\n\n\t//点1,点2から、範囲内の格子点の距離を求めておく\n\tfor(int i = 1; i <= 2; i++){\n\t\tfor(int X = -100; X <= 100; X++){\n\t\t\tfor(int Y = -100; Y <= 100; Y++){\n\t\t\t\tPoint tmp_p = Point(X,Y);\n\n\t\t\t\tdouble tmp_rad;\n\n\t\t\t\tif(i == 1){\n\t\t\t\t\tif(ccw(base_point[1],tmp_p,base_point[2]) != COUNTER_CLOCKWISE){\n\t\t\t\t\t\tcontinue;\n\t\t\t\t\t}\n\n\t\t\t\t\ttmp_rad = calc_rad(tmp_p-base_point[1],base_point[2]-base_point[1]);\n\n\t\t\t\t}else{\n\t\t\t\t\tif(ccw(base_point[2],tmp_p,base_point[0]) != COUNTER_CLOCKWISE){\n\t\t\t\t\t\tcontinue;\n\t\t\t\t\t}\n\t\t\t\t\ttmp_rad = calc_rad(tmp_p-base_point[2],base_point[0]-base_point[2]);\n\t\t\t\t}\n\n\t\t\t\tif(fabs(tmp_rad-M_PI) < EPS || fabs(tmp_rad) < EPS)continue;//面積0は不可\n\n\t\t\t\tint tmp_index = (base_loc[i].int_x-X)*(base_loc[i].int_x-X)+(base_loc[i].int_y-Y)*(base_loc[i].int_y-Y);\n\n\t\t\t\tV[i][tmp_index].push_back(LOC(X,Y));\n\t\t\t}\n\t\t}\n\t}\n\n\tdouble ans = BIG_NUM;\n\n\tint len_0A,len_1A,len_2B,len_0C;\n\n\tLine proj_A,proj_B,proj_C;\n\tLine line_0A,line_1A,line_1B,line_2B,line_2C,line_0C;\n\tPoint cross_AB,cross_BC,cross_CA;\n\n\tline_0A.p[0] = base_point[0];\n\tline_1A.p[0] = base_point[1];\n\tline_1B.p[0] = base_point[1];\n\tline_2B.p[0] = base_point[2];\n\tline_2C.p[0] = base_point[2];\n\tline_0C.p[0] = base_point[0];\n\n\tfor(int a_x = -100; a_x <= 100; a_x++){\n\t\tfor(int a_y = -100; a_y <= 100; a_y++){\n\n\t\t\tp_A = Point(a_x,a_y);\n\t\t\tif(ccw(base_point[0],p_A,base_point[1]) != COUNTER_CLOCKWISE){\n\t\t\t\tcontinue;\n\t\t\t}\n\t\t\tdouble tmp_rad = calc_rad(p_A-base_point[0],base_point[1]-base_point[0]);\n\t\t\tif(fabs(tmp_rad-M_PI) < EPS || fabs(tmp_rad) < EPS)continue; //面積0は不可\n\n\t\t\tlen_0A = (base_loc[0].int_x-a_x)*(base_loc[0].int_x-a_x)+(base_loc[0].int_y-a_y)*(base_loc[0].int_y-a_y);\n\t\t\tlen_1A = (base_loc[1].int_x-a_x)*(base_loc[1].int_x-a_x)+(base_loc[1].int_y-a_y)*(base_loc[1].int_y-a_y);\n\n\t\t\tproj_A.p[0] = p_A;\n\t\t\tproj_A.p[1] = calc_Reflection_Point(line_01,p_A);\n\n\t\t\tline_0A.p[1] = p_A;\n\t\t\tline_1A.p[1] = p_A;\n\n\t\t\t//点Bの検索\n\t\t\tfor(int i = 0; i < V[1][len_1A].size(); i++){\n\n\t\t\t\tint b_x = V[1][len_1A][i].int_x;\n\t\t\t\tint b_y = V[1][len_1A][i].int_y;\n\n\t\t\t\tlen_2B = (base_loc[2].int_x-b_x)*(base_loc[2].int_x-b_x)+(base_loc[2].int_y-b_y)*(base_loc[2].int_y-b_y);\n\n\t\t\t\tp_B = Point(b_x,b_y);\n\t\t\t\tif(p_B == p_A)continue;\n\n\t\t\t\tproj_B.p[0] = p_B;\n\t\t\t\tproj_B.p[1] = calc_Reflection_Point(line_12,p_B);\n\n\t\t\t\tif(!is_Cross(proj_A,proj_B))continue;\n\n\t\t\t\tcross_AB = calc_Cross_Point(proj_A,proj_B);\n\n\t\t\t\tline_1B.p[1] = p_B;\n\t\t\t\tline_2B.p[1] = p_B;\n\n\t\t\t\tif(is_Cross(line_0A,line_1B))continue;\n\t\t\t\tif(is_Cross(line_0A,line_2B))continue;\n\t\t\t\tif(is_Cross(line_1A,line_2B))continue;\n\n\t\t\t\t//点Cの検索\n\t\t\t\tfor(int k = 0; k < V[2][len_2B].size(); k++){\n\n\t\t\t\t\tint c_x = V[2][len_2B][k].int_x;\n\t\t\t\t\tint c_y = V[2][len_2B][k].int_y;\n\n\t\t\t\t\tlen_0C = (base_loc[0].int_x-c_x)*(base_loc[0].int_x-c_x)+(base_loc[0].int_y-c_y)*(base_loc[0].int_y-c_y);\n\t\t\t\t\tif(len_0C != len_0A)continue;\n\n\t\t\t\t\tp_C = Point(c_x,c_y);\n\t\t\t\t\tif(p_C == p_A || p_C == p_B)continue;\n\n\t\t\t\t\tproj_C.p[0] = p_C;\n\t\t\t\t\tproj_C.p[1] = calc_Reflection_Point(line_20,p_C);\n\n\t\t\t\t\tif(!is_Cross(proj_A,proj_C)||!is_Cross(proj_B,proj_C))continue;\n\n\t\t\t\t\tline_2C.p[1] = p_C;\n\t\t\t\t\tline_0C.p[1] = p_C;\n\n\t\t\t\t\tif(is_Cross(line_0A,line_2C))continue;\n\t\t\t\t\tif(is_Cross(line_1A,line_2C))continue;\n\t\t\t\t\tif(is_Cross(line_1A,line_0C))continue;\n\t\t\t\t\tif(is_Cross(line_1B,line_2C))continue;\n\t\t\t\t\tif(is_Cross(line_1B,line_0C))continue;\n\t\t\t\t\tif(is_Cross(line_2B,line_0C))continue;\n\n\t\t\t\t\tcross_BC = calc_Cross_Point(proj_B,proj_C);\n\t\t\t\t\tcross_CA = calc_Cross_Point(proj_C,proj_A);\n\n\t\t\t\t\tif(cross_AB == cross_BC && cross_AB == cross_CA){\n\n\t\t\t\t\t\tif((cross_AB == p_A)||(cross_AB == p_B)||(cross_AB==p_C))continue;\n\n\t\t\t\t\t\t//面が底面の三角形に張り付く(高さ0)場合は不可\n\t\t\t\t\t\tif(fabs(getDistanceLP(line_01,p_A)-getDistanceLP(line_01,cross_AB)) < EPS)continue;\n\t\t\t\t\t\tif(fabs(getDistanceLP(line_12,p_B)-getDistanceLP(line_12,cross_AB)) < EPS)continue;\n\t\t\t\t\t\tif(fabs(getDistanceLP(line_20,p_C)-getDistanceLP(line_20,cross_AB)) < EPS)continue;\n\n\n\t\t\t\t\t\tdouble tmp_len = len_0A;\n\t\t\t\t\t\tdouble tmp_dist = norm(base_point[0]-cross_AB);\n\n\t\t\t\t\t\tans = min(ans,sqrt(tmp_len-tmp_dist));\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n\n\tif(fabs(ans-BIG_NUM) < EPS){\n\n\t\tprintf(\"-1\\n\");\n\t}else{\n\n\t\tprintf(\"%.10lf\\n\",ans);\n\t}\n}\n\nint main(){\n\n\tbool FLG;\n\n\twhile(true){\n\n\t\tFLG = false;\n\n\t\tfor(int i = 0; i < 3; i++){\n\n\t\t\tscanf(\"%d %d\",&base_loc[i].int_x,&base_loc[i].int_y);\n\n\t\t\tbase_point[i].x = base_loc[i].int_x;\n\t\t\tbase_point[i].y = base_loc[i].int_y;\n\n\t\t\tif(base_loc[i].int_x != 0 || base_loc[i].int_y != 0){\n\n\t\t\t\tFLG = true;\n\t\t\t}\n\t\t}\n\t\tif(!FLG)break;\n\n\t\tfunc();\n\t}\n\n\treturn 0;\n}", "accuracy": 1, "time_ms": 580, "memory_kb": 10496, "score_of_the_acc": -0.9349, "final_rank": 13 }, { "submission_id": "aoj_1278_4805796", "code_snippet": "#include<bits/stdc++.h>\ntypedef long long int ll;\ntypedef unsigned long long int ull;\n#define BIG_NUM 2000000000\n#define HUGE_NUM 1000000000000000000\n#define MOD 1000000007\n#define EPS 0.000000001\nusing namespace std;\n\n\n\n#define MAX 80005\n#define SIZE 3\n\nstruct Point{\n\tPoint(double arg_x,double arg_y){\n\t\tx = arg_x;\n\t\ty = arg_y;\n\t}\n\n\tPoint(){\n\t\tx = y = 0.0;\n\t}\n\n\tPoint operator + (Point p){ return Point(x+p.x,y+p.y); }\n\tPoint operator - (Point p){ return Point(x-p.x,y-p.y);}\n\tPoint operator * (double a){ return Point(a*x,a*y); }\n\tPoint operator / (double a){ return Point(x/a,y/a); }\n\n\tdouble abs(){ return sqrt(norm()); }\n\tdouble norm(){ return x*x + y*y; }\n\n\tbool operator<(const Point &p) const{\n\t\treturn x != p.x? x < p.x: y < p.y;\n\t}\n\n\tbool operator == (const Point &p) const{\n\t\treturn fabs(x-p.x) < EPS && fabs(y-p.y) < EPS;\n\t}\n\n\n\tvoid debug(){\n\n\t\tprintf(\"(%.3lf,%.3lf)\\n\",x,y);\n\t}\n\n\tdouble x,y;\n};\n\ntypedef Point Vector;\ntypedef vector<Point> Polygon;\n\ndouble calc_dist(Point A,Point B){\n\n\treturn sqrt((A.x-B.x)*(A.x-B.x)+(A.y-B.y)*(A.y-B.y));\n}\nstruct Line{\n\tLine(){\n\n\t}\n\tLine(Point a,Point b){\n\t\tp[0] = a;\n\t\tp[1] = b;\n\t}\n\t/*void outPut(){\n\n\t\tprintf(\"(%.3lf,%.3lf)-(%.3lf,%.3lf)\\n\",p[0].x,p[0].y,p[1].x,p[1].y);\n\t}*/\n\tPoint p[2];\n};\n\nstruct LOC{\n\tLOC(){\n\n\t\tint_x = int_y = 0;\n\t}\n\tLOC(int arg_int_x,int arg_int_y){\n\t\tint_x = arg_int_x;\n\t\tint_y = arg_int_y;\n\t}\n\tint int_x,int_y;\n};\n\nLOC base_loc[SIZE];\nPoint base_point[3],p_A,p_B,p_C;\nvector<LOC> V[SIZE][MAX];\n\ndouble norm(Vector a){\n\treturn a.x*a.x+a.y*a.y;\n}\n\ndouble abs(Vector a){\n\treturn sqrt(norm(a));\n}\n\ndouble cross(Vector a,Vector b){\n return a.x*b.y-a.y*b.x;\n}\n\ndouble dot(Vector a,Vector b){\n return a.x*b.x + a.y*b.y;\n}\n\nstatic const int COUNTER_CLOCKWISE = 1;\nstatic const int CLOCKWISE = -1;\nstatic const int ONLINE_BACK = 2;\nstatic const int ONLINE_FRONT = -2;\nstatic const int ON_SEGMENT = 0;\n\nint ccw(Point p0,Point p1,Point p2){\n\n\tVector a = p1 - p0;\n\tVector b = p2 - p0;\n\n\tif(cross(a,b) > EPS)return COUNTER_CLOCKWISE;\n\tif(cross(a,b) < -EPS)return CLOCKWISE;\n\tif(dot(a,b) < -EPS)return ONLINE_BACK;\n\tif(a.norm() < b.norm())return ONLINE_FRONT;\n\n\treturn ON_SEGMENT;\n}\n\n\nint func(double x1,double y1,double x2, double y2, double xp, double yp){\n\tdouble naiseki,norm1,norm2,gaiseki;\n\tnorm1 = sqrt((x2-x1)*(x2-x1)+(y2-y1)*(y2-y1));\n\tnorm2 = sqrt((xp-x1)*(xp-x1)+(yp-y1)*(yp-y1));\n\tnaiseki = (xp-x1)*(x2-x1)+(yp-y1)*(y2-y1);\n\tgaiseki = (x2-x1)*(yp-y1)-(xp-x1)*(y2-y1);\n\tif(gaiseki > EPS){\n\t\treturn 1;\n\t}else if(gaiseki < -EPS){\n\t\treturn -1;\n\t}\n\tif(naiseki < -EPS){\n\t\treturn 2;\n\t}\n\n\tif(norm1 < norm2){\n\t\treturn -2;\n\t}\n\treturn 0;\n}\n\n\n//★★直線ではなく、線分の交差判定★★\nbool is_Cross(Line a,Line b){\n\n\tif(func(a.p[0].x,a.p[0].y,a.p[1].x,a.p[1].y,b.p[0].x,b.p[0].y)*\n\t\t\tfunc(a.p[0].x,a.p[0].y,a.p[1].x,a.p[1].y,b.p[1].x,b.p[1].y) <= 0 &&\n\t\t\tfunc(b.p[0].x,b.p[0].y,b.p[1].x,b.p[1].y,a.p[0].x,a.p[0].y)*\n\t\t\tfunc(b.p[0].x,b.p[0].y,b.p[1].x,b.p[1].y,a.p[1].x,a.p[1].y) <= 0){\n\t\treturn true;\n\t}\n\treturn false;\n}\n\n\nPoint calc_minus(Point a,Point b){\n Point ret;\n\n ret.x = a.x-b.x;\n ret.y = a.y-b.y;\n\n return ret;\n}\n\ndouble calc_len(Vector a){\n return sqrt(a.x*a.x+a.y*a.y);\n}\n\n//線分ではなく直線と点の距離\ndouble getDistanceLP(Line l,Point p){\n return fabs(cross(calc_minus(l.p[1],l.p[0]),calc_minus(p,l.p[0]))/calc_len(calc_minus(l.p[1],l.p[0])));\n}\n\n//点と線分の距離\ndouble getDistanceSP(Line l,Point p){\n\tif(dot(calc_minus(l.p[1],l.p[0]),calc_minus(p,l.p[0])) < 0.0)return calc_len(calc_minus(p,l.p[0]));\n\tif(dot(calc_minus(l.p[0],l.p[1]),calc_minus(p,l.p[1])) < 0.0)return calc_len(calc_minus(p,l.p[1]));\n\treturn getDistanceLP(l,p);\n}\n\n//線分と線分の距離\ndouble getDistance(Line A,Line B){\n\tif(is_Cross(A,B))return 0.0;\n\treturn min(min(getDistanceSP(A,B.p[0]),getDistanceSP(A,B.p[1])),\n\t\t\tmin(getDistanceSP(B,A.p[0]),getDistanceSP(B,A.p[1])));\n}\n\n\n//交点を求める関数\nPoint calc_Cross_Point(double x1,double x2,double x3,double x4,double y1,double y2,double y3,double y4){\n\tPoint ret;\n\tret.x = ((x2-x1)*(y3*(x4-x3)+x3*(y3-y4))-(x4-x3)*(y1*(x2-x1)+x1*(y1-y2)))/((y2-y1)*(x4-x3)-(y4-y3)*(x2-x1));\n\tif(fabs(x1-x2) > EPS){\n\t\tret.y = ((y2-y1)*ret.x+y1*(x2-x1)+x1*(y1-y2))/(x2-x1);\n\t}else{\n\t\tret.y = ((y4-y3)*ret.x+y3*(x4-x3)+x3*(y3-y4))/(x4-x3);\n\t}\n\treturn ret;\n}\n\n//インタフェース関数\nPoint calc_Cross_Point(Point a,Point b,Point c,Point d){\n\treturn calc_Cross_Point(a.x,b.x,c.x,d.x,a.y,b.y,c.y,d.y);\n}\n\n\nPoint calc_Cross_Point(Line A,Line B){\n\n\tif(getDistanceSP(B,A.p[0]) < EPS){\n\n\t\treturn A.p[0];\n\n\t}else if(getDistanceSP(B,A.p[1]) < EPS){\n\n\t\treturn A.p[1];\n\n\t}else if(getDistanceSP(A,B.p[0]) < EPS){\n\n\t\treturn B.p[0];\n\n\t}else if(getDistanceSP(A,B.p[1]) < EPS){\n\n\t\treturn B.p[1];\n\t}\n\n\treturn calc_Cross_Point(A.p[0],A.p[1],B.p[0],B.p[1]);\n}\n\n\nPoint project(Line l,Point p){\n\n\tVector base = l.p[1]-l.p[0];\n\tdouble r = dot(p-l.p[0],base)/norm(base);\n\treturn l.p[0]+base*r;\n}\n\ndouble calc_slope(Line A){\n\n\tif(fabs(A.p[0].x-A.p[1].x) < EPS){\n\n\t\treturn DBL_MAX;\n\n\t}else if(fabs(A.p[0].y-A.p[1].y) < EPS){\n\n\t\treturn 0;\n\n\t}else{\n\n\t\treturn (A.p[1].y-A.p[0].y)/(A.p[1].x-A.p[0].x);\n\t}\n}\n\n//2つのvectorがなすラジアンを求める関数\ndouble calc_rad(Vector vec1,Vector vec2){\n\n\tdouble tmp = dot(vec1,vec2)/(abs(vec1)*abs(vec2));\n\n\tif(fabs(tmp+1.0) < EPS){\n\n\t\treturn M_PI;\n\t}\n\n\tif(fabs(tmp-1.0) < EPS){\n\n\t\treturn 0.0;\n\t}\n\n\treturn acos(dot(vec1,vec2)/(abs(vec1)*abs(vec2)));\n}\n\nPoint calc_Reflection_Point(double x1,double y1,double x2,double y2,double xp,double yp){\n\n\tPoint ret;\n\n\tbool X_FLG = false,Y_FLG = false;\n\tdouble slope;\n\n\tif(y1 == y2){\n\t\tX_FLG = true;\n\t}else if(x1 == x2){\n\t\tY_FLG = true;\n\t}else{\n\t\tslope = (y2-y1)/(x2-x1);\n\t}\n\n\tif(X_FLG){\n\t\tret.x = xp,ret.y=y1;\n\t}else if(Y_FLG){\n\t\tret.x = x1,ret.y = yp;\n\t}else{\n\t\tret.x = (yp*(x2-x1)*(y2-y1)+xp*(x2-x1)*(x2-x1)-y1*(y2-y1)*(x2-x1)+x1*(y2-y1)*(y2-y1))/((y2-y1)*(y2-y1)+(x2-x1)*(x2-x1));\n\t\tret.y = ((x1-x2)*ret.x+yp*(y2-y1)+xp*(x2-x1))/(y2-y1);\n\t}\n\tret.x = 2*ret.x-xp;\n\tret.y = 2*ret.y-yp;\n\n\treturn ret;\n}\n\nPoint calc_Reflection_Point(Line line,Point point){\n\n\treturn calc_Reflection_Point(line.p[0].x,line.p[0].y,line.p[1].x,line.p[1].y,point.x,point.y);\n}\n\n\n\nvoid func(){\n\n\tfor(int i = 0; i < MAX; i++){\n\t\tV[1][i].clear();\n\t\tV[2][i].clear();\n\t}\n\n\tLine line_01,line_12,line_20;\n\n\tline_01.p[0] = base_point[0];\n\tline_01.p[1] = base_point[1];\n\n\tline_12.p[0] = base_point[1];\n\tline_12.p[1] = base_point[2];\n\n\tline_20.p[0] = base_point[2];\n\tline_20.p[1] = base_point[0];\n\n\n\t//点1,点2から、範囲内の格子点の距離を求めておく\n\tfor(int i = 1; i <= 2; i++){\n\t\tfor(int X = -100; X <= 100; X++){\n\t\t\tfor(int Y = -100; Y <= 100; Y++){\n\t\t\t\tPoint tmp_p = Point(X,Y);\n\n\t\t\t\tdouble tmp_rad;\n\n\t\t\t\tif(i == 1){\n\t\t\t\t\tif(ccw(base_point[1],tmp_p,base_point[2]) != COUNTER_CLOCKWISE){\n\t\t\t\t\t\tcontinue;\n\t\t\t\t\t}\n\n\t\t\t\t\ttmp_rad = calc_rad(tmp_p-base_point[1],base_point[2]-base_point[1]);\n\n\t\t\t\t}else{\n\t\t\t\t\tif(ccw(base_point[2],tmp_p,base_point[0]) != COUNTER_CLOCKWISE){\n\t\t\t\t\t\tcontinue;\n\t\t\t\t\t}\n\t\t\t\t\ttmp_rad = calc_rad(tmp_p-base_point[2],base_point[0]-base_point[2]);\n\t\t\t\t}\n\n\t\t\t\tif(fabs(tmp_rad-M_PI) < EPS || fabs(tmp_rad) < EPS)continue;//面積0は不可\n\n\t\t\t\tint tmp_index = (base_loc[i].int_x-X)*(base_loc[i].int_x-X)+(base_loc[i].int_y-Y)*(base_loc[i].int_y-Y);\n\n\t\t\t\tV[i][tmp_index].push_back(LOC(X,Y));\n\t\t\t}\n\t\t}\n\t}\n\n\tdouble ans = BIG_NUM;\n\n\tint len_0A,len_1A,len_2B,len_0C;\n\n\tLine proj_A,proj_B,proj_C;\n\tLine line_0A,line_1A,line_1B,line_2B,line_2C,line_0C;\n\tPoint cross_AB,cross_BC,cross_CA;\n\n\tline_0A.p[0] = base_point[0];\n\tline_1A.p[0] = base_point[1];\n\tline_1B.p[0] = base_point[1];\n\tline_2B.p[0] = base_point[2];\n\tline_2C.p[0] = base_point[2];\n\tline_0C.p[0] = base_point[0];\n\n\tfor(int a_x = -100; a_x <= 100; a_x++){\n\t\tfor(int a_y = -100; a_y <= 100; a_y++){\n\n\t\t\tp_A = Point(a_x,a_y);\n\t\t\tif(ccw(base_point[0],p_A,base_point[1]) != COUNTER_CLOCKWISE){\n\t\t\t\tcontinue;\n\t\t\t}\n\t\t\tdouble tmp_rad = calc_rad(p_A-base_point[0],base_point[1]-base_point[0]);\n\t\t\tif(fabs(tmp_rad-M_PI) < EPS || fabs(tmp_rad) < EPS)continue; //面積0は不可\n\n\t\t\tlen_0A = (base_loc[0].int_x-a_x)*(base_loc[0].int_x-a_x)+(base_loc[0].int_y-a_y)*(base_loc[0].int_y-a_y);\n\t\t\tlen_1A = (base_loc[1].int_x-a_x)*(base_loc[1].int_x-a_x)+(base_loc[1].int_y-a_y)*(base_loc[1].int_y-a_y);\n\n\t\t\tproj_A.p[0] = p_A;\n\t\t\tproj_A.p[1] = calc_Reflection_Point(line_01,p_A);\n\n\t\t\tline_0A.p[1] = p_A;\n\t\t\tline_1A.p[1] = p_A;\n\n\t\t\t//点Bの検索\n\t\t\tfor(int i = 0; i < V[1][len_1A].size(); i++){\n\n\t\t\t\tint b_x = V[1][len_1A][i].int_x;\n\t\t\t\tint b_y = V[1][len_1A][i].int_y;\n\n\t\t\t\tlen_2B = (base_loc[2].int_x-b_x)*(base_loc[2].int_x-b_x)+(base_loc[2].int_y-b_y)*(base_loc[2].int_y-b_y);\n\n\t\t\t\tp_B = Point(b_x,b_y);\n\t\t\t\tif(p_B == p_A)continue;\n\n\t\t\t\tproj_B.p[0] = p_B;\n\t\t\t\tproj_B.p[1] = calc_Reflection_Point(line_12,p_B);\n\n\t\t\t\tif(!is_Cross(proj_A,proj_B))continue;\n\n\t\t\t\tcross_AB = calc_Cross_Point(proj_A,proj_B);\n\n\t\t\t\tline_1B.p[1] = p_B;\n\t\t\t\tline_2B.p[1] = p_B;\n\n\t\t\t\tif(is_Cross(line_0A,line_1B))continue;\n\t\t\t\tif(is_Cross(line_0A,line_2B))continue;\n\t\t\t\tif(is_Cross(line_1A,line_2B))continue;\n\n\t\t\t\t//点Cの検索\n\t\t\t\tfor(int k = 0; k < V[2][len_2B].size(); k++){\n\n\t\t\t\t\tint c_x = V[2][len_2B][k].int_x;\n\t\t\t\t\tint c_y = V[2][len_2B][k].int_y;\n\n\t\t\t\t\tlen_0C = (base_loc[0].int_x-c_x)*(base_loc[0].int_x-c_x)+(base_loc[0].int_y-c_y)*(base_loc[0].int_y-c_y);\n\t\t\t\t\tif(len_0C != len_0A)continue;\n\n\t\t\t\t\tp_C = Point(c_x,c_y);\n\t\t\t\t\tif(p_C == p_A || p_C == p_B)continue;\n\n\t\t\t\t\tproj_C.p[0] = p_C;\n\t\t\t\t\tproj_C.p[1] = calc_Reflection_Point(line_20,p_C);\n\n\t\t\t\t\tif(!is_Cross(proj_A,proj_C)||!is_Cross(proj_B,proj_C))continue;\n\n\t\t\t\t\tline_2C.p[1] = p_C;\n\t\t\t\t\tline_0C.p[1] = p_C;\n\n\t\t\t\t\tif(is_Cross(line_0A,line_2C))continue;\n\t\t\t\t\tif(is_Cross(line_1A,line_2C))continue;\n\t\t\t\t\tif(is_Cross(line_1A,line_0C))continue;\n\t\t\t\t\tif(is_Cross(line_1B,line_2C))continue;\n\t\t\t\t\tif(is_Cross(line_1B,line_0C))continue;\n\t\t\t\t\tif(is_Cross(line_2B,line_0C))continue;\n\n\t\t\t\t\tcross_BC = calc_Cross_Point(proj_B,proj_C);\n\t\t\t\t\tcross_CA = calc_Cross_Point(proj_C,proj_A);\n\n\t\t\t\t\tif(cross_AB == cross_BC && cross_AB == cross_CA){\n\n\t\t\t\t\t\tif((cross_AB == p_A)||(cross_AB == p_B)||(cross_AB==p_C))continue;\n\n\t\t\t\t\t\tdouble tmp_len = len_0A;\n\t\t\t\t\t\tdouble tmp_dist = norm(base_point[0]-cross_AB);\n\n\t\t\t\t\t\tif(fabs(tmp_len-tmp_dist) < EPS){\n\n\t\t\t\t\t\t\tans = min(ans,sqrt(tmp_len+tmp_dist));\n\t\t\t\t\t\t}else{\n\n\t\t\t\t\t\t\tans = min(ans,sqrt(tmp_len-tmp_dist));\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n\n\tif(fabs(ans-BIG_NUM) < EPS){\n\n\t\tprintf(\"-1\\n\");\n\t}else{\n\n\t\tprintf(\"%.10lf\\n\",ans);\n\t}\n}\n\nint main(){\n\n\tbool FLG;\n\n\twhile(true){\n\n\t\tFLG = false;\n\n\t\tfor(int i = 0; i < 3; i++){\n\n\t\t\tscanf(\"%d %d\",&base_loc[i].int_x,&base_loc[i].int_y);\n\n\t\t\tbase_point[i].x = base_loc[i].int_x;\n\t\t\tbase_point[i].y = base_loc[i].int_y;\n\n\t\t\tif(base_loc[i].int_x != 0 || base_loc[i].int_y != 0){\n\n\t\t\t\tFLG = true;\n\t\t\t}\n\t\t}\n\t\tif(!FLG)break;\n\n\t\tfunc();\n\t}\n\n\treturn 0;\n}", "accuracy": 1, "time_ms": 580, "memory_kb": 10396, "score_of_the_acc": -0.9251, "final_rank": 11 }, { "submission_id": "aoj_1278_4805748", "code_snippet": "#include<bits/stdc++.h>\ntypedef long long int ll;\ntypedef unsigned long long int ull;\n#define BIG_NUM 2000000000\n#define HUGE_NUM 1000000000000000000\n#define MOD 1000000007\n#define EPS 0.000000001\nusing namespace std;\n\n\n\n#define MAX 80005\n#define SIZE 3\n\nstruct Point{\n\tPoint(double arg_x,double arg_y){\n\t\tx = arg_x;\n\t\ty = arg_y;\n\t}\n\n\tPoint(){\n\t\tx = y = 0.0;\n\t}\n\n\tPoint operator + (Point p){ return Point(x+p.x,y+p.y); }\n\tPoint operator - (Point p){ return Point(x-p.x,y-p.y);}\n\tPoint operator * (double a){ return Point(a*x,a*y); }\n\tPoint operator / (double a){ return Point(x/a,y/a); }\n\n\tdouble abs(){ return sqrt(norm()); }\n\tdouble norm(){ return x*x + y*y; }\n\n\tbool operator<(const Point &p) const{\n\t\treturn x != p.x? x < p.x: y < p.y;\n\t}\n\n\tbool operator == (const Point &p) const{\n\t\treturn fabs(x-p.x) < EPS && fabs(y-p.y) < EPS;\n\t}\n\n\n\tvoid debug(){\n\n\t\tprintf(\"(%.3lf,%.3lf)\\n\",x,y);\n\t}\n\n\tdouble x,y;\n};\n\ntypedef Point Vector;\ntypedef vector<Point> Polygon;\n\ndouble calc_dist(Point A,Point B){\n\n\treturn sqrt((A.x-B.x)*(A.x-B.x)+(A.y-B.y)*(A.y-B.y));\n}\nstruct Line{\n\tLine(){\n\n\t}\n\tLine(Point a,Point b){\n\t\tp[0] = a;\n\t\tp[1] = b;\n\t}\n\t/*void outPut(){\n\n\t\tprintf(\"(%.3lf,%.3lf)-(%.3lf,%.3lf)\\n\",p[0].x,p[0].y,p[1].x,p[1].y);\n\t}*/\n\tPoint p[2];\n};\n\nstruct LOC{\n\tLOC(){\n\n\t\tint_x = int_y = 0;\n\t}\n\tLOC(int arg_int_x,int arg_int_y){\n\t\tint_x = arg_int_x;\n\t\tint_y = arg_int_y;\n\t}\n\tint int_x,int_y;\n};\n\nLOC base_loc[SIZE];\nPoint base_point[3],p_A,p_B,p_C;\nvector<LOC> V[SIZE][MAX];\n\ndouble norm(Vector a){\n\treturn a.x*a.x+a.y*a.y;\n}\n\ndouble abs(Vector a){\n\treturn sqrt(norm(a));\n}\n\ndouble cross(Vector a,Vector b){\n return a.x*b.y-a.y*b.x;\n}\n\ndouble dot(Vector a,Vector b){\n return a.x*b.x + a.y*b.y;\n}\n\nstatic const int COUNTER_CLOCKWISE = 1;\nstatic const int CLOCKWISE = -1;\nstatic const int ONLINE_BACK = 2;\nstatic const int ONLINE_FRONT = -2;\nstatic const int ON_SEGMENT = 0;\n\nint ccw(Point p0,Point p1,Point p2){\n\n\tVector a = p1 - p0;\n\tVector b = p2 - p0;\n\n\tif(cross(a,b) > EPS)return COUNTER_CLOCKWISE;\n\tif(cross(a,b) < -EPS)return CLOCKWISE;\n\tif(dot(a,b) < -EPS)return ONLINE_BACK;\n\tif(a.norm() < b.norm())return ONLINE_FRONT;\n\n\treturn ON_SEGMENT;\n}\n\n\nint func(double x1,double y1,double x2, double y2, double xp, double yp){\n\tdouble naiseki,norm1,norm2,gaiseki;\n\tnorm1 = sqrt((x2-x1)*(x2-x1)+(y2-y1)*(y2-y1));\n\tnorm2 = sqrt((xp-x1)*(xp-x1)+(yp-y1)*(yp-y1));\n\tnaiseki = (xp-x1)*(x2-x1)+(yp-y1)*(y2-y1);\n\tgaiseki = (x2-x1)*(yp-y1)-(xp-x1)*(y2-y1);\n\tif(gaiseki > EPS){\n\t\treturn 1;\n\t}else if(gaiseki < -EPS){\n\t\treturn -1;\n\t}\n\tif(naiseki < -EPS){\n\t\treturn 2;\n\t}\n\n\tif(norm1 < norm2){\n\t\treturn -2;\n\t}\n\treturn 0;\n}\n\n\n//★★直線ではなく、線分の交差判定★★\nbool is_Cross(Line a,Line b){\n\n\tif(func(a.p[0].x,a.p[0].y,a.p[1].x,a.p[1].y,b.p[0].x,b.p[0].y)*\n\t\t\tfunc(a.p[0].x,a.p[0].y,a.p[1].x,a.p[1].y,b.p[1].x,b.p[1].y) <= 0 &&\n\t\t\tfunc(b.p[0].x,b.p[0].y,b.p[1].x,b.p[1].y,a.p[0].x,a.p[0].y)*\n\t\t\tfunc(b.p[0].x,b.p[0].y,b.p[1].x,b.p[1].y,a.p[1].x,a.p[1].y) <= 0){\n\t\treturn true;\n\t}\n\treturn false;\n}\n\n\nPoint calc_minus(Point a,Point b){\n Point ret;\n\n ret.x = a.x-b.x;\n ret.y = a.y-b.y;\n\n return ret;\n}\n\ndouble calc_len(Vector a){\n return sqrt(a.x*a.x+a.y*a.y);\n}\n\n//線分ではなく直線と点の距離\ndouble getDistanceLP(Line l,Point p){\n return fabs(cross(calc_minus(l.p[1],l.p[0]),calc_minus(p,l.p[0]))/calc_len(calc_minus(l.p[1],l.p[0])));\n}\n\n//点と線分の距離\ndouble getDistanceSP(Line l,Point p){\n\tif(dot(calc_minus(l.p[1],l.p[0]),calc_minus(p,l.p[0])) < 0.0)return calc_len(calc_minus(p,l.p[0]));\n\tif(dot(calc_minus(l.p[0],l.p[1]),calc_minus(p,l.p[1])) < 0.0)return calc_len(calc_minus(p,l.p[1]));\n\treturn getDistanceLP(l,p);\n}\n\n//線分と線分の距離\ndouble getDistance(Line A,Line B){\n\tif(is_Cross(A,B))return 0.0;\n\treturn min(min(getDistanceSP(A,B.p[0]),getDistanceSP(A,B.p[1])),\n\t\t\tmin(getDistanceSP(B,A.p[0]),getDistanceSP(B,A.p[1])));\n}\n\n\n//交点を求める関数\nPoint calc_Cross_Point(double x1,double x2,double x3,double x4,double y1,double y2,double y3,double y4){\n\tPoint ret;\n\tret.x = ((x2-x1)*(y3*(x4-x3)+x3*(y3-y4))-(x4-x3)*(y1*(x2-x1)+x1*(y1-y2)))/((y2-y1)*(x4-x3)-(y4-y3)*(x2-x1));\n\tif(fabs(x1-x2) > EPS){\n\t\tret.y = ((y2-y1)*ret.x+y1*(x2-x1)+x1*(y1-y2))/(x2-x1);\n\t}else{\n\t\tret.y = ((y4-y3)*ret.x+y3*(x4-x3)+x3*(y3-y4))/(x4-x3);\n\t}\n\treturn ret;\n}\n\n//インタフェース関数\nPoint calc_Cross_Point(Point a,Point b,Point c,Point d){\n\treturn calc_Cross_Point(a.x,b.x,c.x,d.x,a.y,b.y,c.y,d.y);\n}\n\n\nPoint calc_Cross_Point(Line A,Line B){\n\n\tif(getDistanceSP(B,A.p[0]) < EPS){\n\n\t\treturn A.p[0];\n\n\t}else if(getDistanceSP(B,A.p[1]) < EPS){\n\n\t\treturn A.p[1];\n\n\t}else if(getDistanceSP(A,B.p[0]) < EPS){\n\n\t\treturn B.p[0];\n\n\t}else if(getDistanceSP(A,B.p[1]) < EPS){\n\n\t\treturn B.p[1];\n\t}\n\n\treturn calc_Cross_Point(A.p[0],A.p[1],B.p[0],B.p[1]);\n}\n\n\nPoint project(Line l,Point p){\n\n\tVector base = l.p[1]-l.p[0];\n\tdouble r = dot(p-l.p[0],base)/norm(base);\n\treturn l.p[0]+base*r;\n}\n\ndouble calc_slope(Line A){\n\n\tif(fabs(A.p[0].x-A.p[1].x) < EPS){\n\n\t\treturn DBL_MAX;\n\n\t}else if(fabs(A.p[0].y-A.p[1].y) < EPS){\n\n\t\treturn 0;\n\n\t}else{\n\n\t\treturn (A.p[1].y-A.p[0].y)/(A.p[1].x-A.p[0].x);\n\t}\n}\n\n//線分をp1方向に伸ばす\nLine calc_longer(Line tmp_line){\n\n\tdouble slope = calc_slope(tmp_line);\n\tdouble NUM = 200000;\n\n\tLine ret;\n\tret.p[0] = tmp_line.p[0];\n\n\tif(fabs(slope-DBL_MAX) < EPS){ //垂直\n\n\t\tif(tmp_line.p[0].y > tmp_line.p[1].y){\n\n\t\t\tret.p[1] = Point(tmp_line.p[0].x,-NUM);\n\n\t\t}else{\n\n\t\t\tret.p[1] = Point(tmp_line.p[0].x,NUM);\n\t\t}\n\n\t}else if(fabs(slope) < EPS){ //水平\n\n\t\tif(tmp_line.p[0].x > tmp_line.p[1].x){\n\n\t\t\tret.p[1] = Point(-NUM,tmp_line.p[0].y);\n\n\t\t}else{\n\n\t\t\tret.p[1] = Point(NUM,tmp_line.p[0].y);\n\t\t}\n\n\t}else{\n\n\t\tif(tmp_line.p[0].x > tmp_line.p[1].x){\n\n\t\t\tret.p[1] = Point(tmp_line.p[0].x-NUM,tmp_line.p[0].y-NUM*slope);\n\n\t\t}else{\n\n\t\t\tret.p[1] = Point(tmp_line.p[0].x+NUM,tmp_line.p[0].y+NUM*slope);\n\t\t}\n\n\t}\n\treturn ret;\n}\n\n//2つのvectorがなすラジアンを求める関数\ndouble calc_rad(Vector vec1,Vector vec2){\n\n\tdouble tmp = dot(vec1,vec2)/(abs(vec1)*abs(vec2));\n\n\tif(fabs(tmp+1.0) < EPS){\n\n\t\treturn M_PI;\n\t}\n\n\tif(fabs(tmp-1.0) < EPS){\n\n\t\treturn 0.0;\n\t}\n\n\treturn acos(dot(vec1,vec2)/(abs(vec1)*abs(vec2)));\n}\n\nPoint calc_Reflection_Point(double x1,double y1,double x2,double y2,double xp,double yp){\n\n\tPoint ret;\n\n\tbool X_FLG = false,Y_FLG = false;\n\tdouble slope;\n\n\tif(y1 == y2){\n\t\tX_FLG = true;\n\t}else if(x1 == x2){\n\t\tY_FLG = true;\n\t}else{\n\t\tslope = (y2-y1)/(x2-x1);\n\t}\n\n\tif(X_FLG){\n\t\tret.x = xp,ret.y=y1;\n\t}else if(Y_FLG){\n\t\tret.x = x1,ret.y = yp;\n\t}else{\n\t\tret.x = (yp*(x2-x1)*(y2-y1)+xp*(x2-x1)*(x2-x1)-y1*(y2-y1)*(x2-x1)+x1*(y2-y1)*(y2-y1))/((y2-y1)*(y2-y1)+(x2-x1)*(x2-x1));\n\t\tret.y = ((x1-x2)*ret.x+yp*(y2-y1)+xp*(x2-x1))/(y2-y1);\n\t}\n\tret.x = 2*ret.x-xp;\n\tret.y = 2*ret.y-yp;\n\n\treturn ret;\n}\n\nPoint calc_Reflection_Point(Line line,Point point){\n\n\treturn calc_Reflection_Point(line.p[0].x,line.p[0].y,line.p[1].x,line.p[1].y,point.x,point.y);\n}\n\n\n\nvoid func(){\n\n\tfor(int i = 0; i < MAX; i++){\n\t\tV[1][i].clear();\n\t\tV[2][i].clear();\n\t}\n\n\tLine line_01,line_12,line_20;\n\n\tline_01.p[0] = base_point[0];\n\tline_01.p[1] = base_point[1];\n\n\tline_12.p[0] = base_point[1];\n\tline_12.p[1] = base_point[2];\n\n\tline_20.p[0] = base_point[2];\n\tline_20.p[1] = base_point[0];\n\n\n\t//点1,点2から、範囲内の格子点の距離を求めておく\n\tfor(int i = 1; i <= 2; i++){\n\t\tfor(int X = -100; X <= 100; X++){\n\t\t\tfor(int Y = -100; Y <= 100; Y++){\n\t\t\t\tPoint tmp_p = Point(X,Y);\n\n\t\t\t\tdouble tmp_rad;\n\n\t\t\t\tif(i == 1){\n\t\t\t\t\tif(ccw(base_point[1],tmp_p,base_point[2]) != COUNTER_CLOCKWISE){\n\t\t\t\t\t\tcontinue;\n\t\t\t\t\t}\n\n\t\t\t\t\ttmp_rad = calc_rad(tmp_p-base_point[1],base_point[2]-base_point[1]);\n\n\t\t\t\t}else{\n\t\t\t\t\tif(ccw(base_point[2],tmp_p,base_point[0]) != COUNTER_CLOCKWISE){\n\t\t\t\t\t\tcontinue;\n\t\t\t\t\t}\n\t\t\t\t\ttmp_rad = calc_rad(tmp_p-base_point[2],base_point[0]-base_point[2]);\n\t\t\t\t}\n\n\t\t\t\tif(fabs(tmp_rad-M_PI) < EPS || fabs(tmp_rad) < EPS)continue;//面積0は不可\n\n\t\t\t\tint tmp_index = (base_loc[i].int_x-X)*(base_loc[i].int_x-X)+(base_loc[i].int_y-Y)*(base_loc[i].int_y-Y);\n\n\t\t\t\tV[i][tmp_index].push_back(LOC(X,Y));\n\t\t\t}\n\t\t}\n\t}\n\n\tdouble ans = BIG_NUM;\n\n\tint len_0A,len_1A,len_2B,len_0C;\n\n\tLine proj_A,proj_B,proj_C;\n\tLine line_0A,line_1A,line_1B,line_2B,line_2C,line_0C;\n\tPoint cross_AB,cross_BC,cross_CA;\n\n\tline_0A.p[0] = base_point[0];\n\tline_1A.p[0] = base_point[1];\n\tline_1B.p[0] = base_point[1];\n\tline_2B.p[0] = base_point[2];\n\tline_2C.p[0] = base_point[2];\n\tline_0C.p[0] = base_point[0];\n\n\tfor(int a_x = -100; a_x <= 100; a_x++){\n\t\tfor(int a_y = -100; a_y <= 100; a_y++){\n\n\t\t\tp_A = Point(a_x,a_y);\n\t\t\tif(ccw(base_point[0],p_A,base_point[1]) != COUNTER_CLOCKWISE){\n\t\t\t\tcontinue;\n\t\t\t}\n\t\t\tdouble tmp_rad = calc_rad(p_A-base_point[0],base_point[1]-base_point[0]);\n\t\t\tif(fabs(tmp_rad-M_PI) < EPS || fabs(tmp_rad) < EPS)continue; //面積0は不可\n\n\t\t\tlen_0A = (base_loc[0].int_x-a_x)*(base_loc[0].int_x-a_x)+(base_loc[0].int_y-a_y)*(base_loc[0].int_y-a_y);\n\t\t\tlen_1A = (base_loc[1].int_x-a_x)*(base_loc[1].int_x-a_x)+(base_loc[1].int_y-a_y)*(base_loc[1].int_y-a_y);\n\n\t\t\tproj_A.p[0] = p_A;\n\t\t\tproj_A.p[1] = calc_Reflection_Point(line_01,p_A);\n\n\t\t\tproj_A = calc_longer(proj_A);\n\n\t\t\tline_0A.p[1] = p_A;\n\t\t\tline_1A.p[1] = p_A;\n\n\t\t\t//点Bの検索\n\t\t\tfor(int i = 0; i < V[1][len_1A].size(); i++){\n\n\t\t\t\tint b_x = V[1][len_1A][i].int_x;\n\t\t\t\tint b_y = V[1][len_1A][i].int_y;\n\n\t\t\t\tlen_2B = (base_loc[2].int_x-b_x)*(base_loc[2].int_x-b_x)+(base_loc[2].int_y-b_y)*(base_loc[2].int_y-b_y);\n\n\t\t\t\tp_B = Point(b_x,b_y);\n\t\t\t\tif(p_B == p_A)continue;\n\n\t\t\t\tproj_B.p[0] = p_B;\n\t\t\t\tproj_B.p[1] = calc_Reflection_Point(line_12,p_B);\n\n\t\t\t\tproj_B = calc_longer(proj_B);\n\n\t\t\t\tif(!is_Cross(proj_A,proj_B))continue;\n\n\t\t\t\tcross_AB = calc_Cross_Point(proj_A,proj_B);\n\n\t\t\t\tline_1B.p[1] = p_B;\n\t\t\t\tline_2B.p[1] = p_B;\n\n\t\t\t\tif(is_Cross(line_0A,line_1B))continue;\n\t\t\t\tif(is_Cross(line_0A,line_2B))continue;\n\t\t\t\tif(is_Cross(line_1A,line_2B))continue;\n\n\t\t\t\t//点Cの検索\n\t\t\t\tfor(int k = 0; k < V[2][len_2B].size(); k++){\n\n\t\t\t\t\tint c_x = V[2][len_2B][k].int_x;\n\t\t\t\t\tint c_y = V[2][len_2B][k].int_y;\n\n\t\t\t\t\tlen_0C = (base_loc[0].int_x-c_x)*(base_loc[0].int_x-c_x)+(base_loc[0].int_y-c_y)*(base_loc[0].int_y-c_y);\n\t\t\t\t\tif(len_0C != len_0A)continue;\n\n\t\t\t\t\tp_C = Point(c_x,c_y);\n\t\t\t\t\tif(p_C == p_A || p_C == p_B)continue;\n\n\t\t\t\t\tproj_C.p[0] = p_C;\n\t\t\t\t\tproj_C.p[1] = calc_Reflection_Point(line_20,p_C);\n\n\t\t\t\t\tproj_C = calc_longer(proj_C);\n\n\t\t\t\t\tif(!is_Cross(proj_A,proj_C)||!is_Cross(proj_B,proj_C))continue;\n\n\t\t\t\t\tline_2C.p[1] = p_C;\n\t\t\t\t\tline_0C.p[1] = p_C;\n\n\t\t\t\t\tif(is_Cross(line_0A,line_2C))continue;\n\t\t\t\t\tif(is_Cross(line_1A,line_2C))continue;\n\t\t\t\t\tif(is_Cross(line_1A,line_0C))continue;\n\t\t\t\t\tif(is_Cross(line_1B,line_2C))continue;\n\t\t\t\t\tif(is_Cross(line_1B,line_0C))continue;\n\t\t\t\t\tif(is_Cross(line_2B,line_0C))continue;\n\n\t\t\t\t\tcross_BC = calc_Cross_Point(proj_B,proj_C);\n\t\t\t\t\tcross_CA = calc_Cross_Point(proj_C,proj_A);\n\n\t\t\t\t\tif(cross_AB == cross_BC && cross_AB == cross_CA){\n\n\t\t\t\t\t\tif((cross_AB == p_A)||(cross_AB == p_B)||(cross_AB==p_C))continue;\n\n\t\t\t\t\t\tdouble tmp_len = len_0A;\n\t\t\t\t\t\tdouble tmp_dist = norm(base_point[0]-cross_AB);\n\n\t\t\t\t\t\tif(fabs(tmp_len-tmp_dist) < EPS){\n\n\t\t\t\t\t\t\tans = min(ans,sqrt(tmp_len+tmp_dist));\n\t\t\t\t\t\t}else{\n\n\t\t\t\t\t\t\tans = min(ans,sqrt(tmp_len-tmp_dist));\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n\n\tif(fabs(ans-BIG_NUM) < EPS){\n\n\t\tprintf(\"-1\\n\");\n\t}else{\n\n\t\tprintf(\"%.10lf\\n\",ans);\n\t}\n}\n\nint main(){\n\n\tbool FLG;\n\n\twhile(true){\n\n\t\tFLG = false;\n\n\t\tfor(int i = 0; i < 3; i++){\n\n\t\t\tscanf(\"%d %d\",&base_loc[i].int_x,&base_loc[i].int_y);\n\n\t\t\tbase_point[i].x = base_loc[i].int_x;\n\t\t\tbase_point[i].y = base_loc[i].int_y;\n\n\t\t\tif(base_loc[i].int_x != 0 || base_loc[i].int_y != 0){\n\n\t\t\t\tFLG = true;\n\t\t\t}\n\t\t}\n\t\tif(!FLG)break;\n\n\t\tfunc();\n\t}\n\n\treturn 0;\n}", "accuracy": 1, "time_ms": 740, "memory_kb": 10472, "score_of_the_acc": -0.9542, "final_rank": 14 }, { "submission_id": "aoj_1278_3119091", "code_snippet": "#include <iostream>\n#include <iomanip>\n#include <complex>\n#include <vector>\n#include <algorithm>\n#include <cmath>\n#include <array>\nusing namespace std;\nconst double EPS = 1e-8;\nconst double INF = 1e12;\nconst double PI = acos(-1);\n#define EQ(n,m) (abs((n)-(m)) < EPS)\n#define X real()\n#define Y imag()\n\ntypedef complex<double> P;\ntypedef vector<P> VP;\nstruct L : array<P, 2>{\n L(const P& a, const P& b){ at(0)=a; at(1)=b; }\n L(){}\n};\nstruct C{\n P p;\n double r;\n C(const P& p, const double& r) : p(p), r(r) {}\n C(){}\n};\n\nnamespace std{\n bool operator < (const P& a, const P& b){\n return !EQ(a.X,b.X) ? a.X<b.X : a.Y+EPS<b.Y;\n }\n bool operator == (const P& a, const P& b){\n return abs(a-b) < EPS;\n }\n}\n\ndouble dot(P a, P b){\n return (conj(a)*b).X;\n}\ndouble cross(P a, P b){\n return (conj(a)*b).Y;\n}\nint ccw(P a, P b, P c){\n b -= a;\n c -= a;\n if(cross(b,c) > EPS) return +1; //ccw\n if(cross(b,c) < -EPS) return -1; //cw\n if(dot(b,c) < -EPS) return +2; //c-a-b\n if(abs(c)-abs(b) > EPS) return -2; //a-b-c\n return 0; //a-c-b\n}\nP unit(const P &p){\n return p/abs(p);\n}\nP rotate(const P &p, double rad){\n return p *P(cos(rad), sin(rad));\n}\n\nbool intersectSS(const L& a, const L& b){\n return ( ccw(a[0],a[1],b[0]) *ccw(a[0],a[1],b[1]) <= 0 ) &&\n ( ccw(b[0],b[1],a[0]) *ccw(b[0],b[1],a[1]) <= 0 );\n}\n\nP projection(const L& l, const P& p) {\n double t = dot(p-l[0], l[0]-l[1]) / norm(l[0]-l[1]);\n return l[0] + t*(l[0]-l[1]);\n}\ndouble distanceLP(const L &l, const P &p) {\n return abs(cross(l[1]-l[0], p-l[0])) /abs(l[1]-l[0]);\n}\n\nP crosspointLL(const L &l, const L &m) {\n double A = cross(l[1]-l[0], m[1]-m[0]);\n double B = cross(l[1]-l[0], l[1]-m[0]);\n return m[0] + B/A *(m[1]-m[0]);\n}\n\nVP crosspointCC(C a, C b){\n VP ret;\n if(a.p == b.p && a.r == b.r){\n return ret;\n }\n if(a.r < b.r) swap(a,b);\n double dist = abs(b.p-a.p);\n P dir = a.r*unit(b.p-a.p);\n if(EQ(dist, a.r+b.r) || EQ(dist, a.r-b.r)){\n ret.push_back(a.p +dir);\n }else if(a.r-b.r < dist && dist < a.r+b.r){\n double cos = (a.r*a.r +dist*dist -b.r*b.r)/(2*a.r*dist);\n double sin = sqrt(1 -cos*cos);\n ret.push_back(a.p +dir*P(cos, sin));\n ret.push_back(a.p +dir*P(cos, -sin));\n }\n return ret;\n}\n\nbool isValid(const P &p){\n\treturn -100 < p.X +EPS && p.X < 100 +EPS &&\n\t\t-100 < p.Y +EPS && p.Y < 100 +EPS &&\n\t\tEQ(p.X, round(p.X)) && EQ(p.Y, round(p.Y));\t\t\t\n}\n\ndouble solve(const VP &tri, const P &p, const P &q){\n\t//p,qを置く位置が不正\n\tif(!isValid(p) || !isValid(q)) return INF;\n\tif(cross(tri[0] -tri[1], p -tri[1]) < EPS) return INF*2;\n\tif(cross(tri[2] -tri[0], q -tri[0]) < EPS) return INF*3;\n\tif(intersectSS(L(tri[1], p), L(tri[0], q))) return INF*4;\n\tif(intersectSS(L(tri[0], p), L(tri[2], q))) return INF*5;\n\n\t//もう1つの点rの位置\n\tVP cp = crosspointCC(C(tri[1], abs(tri[1] -p)), C(tri[2], abs(tri[2] -q)));\n\tif((int)cp.size()!=2) return INF;\n\tP r = (cross(tri[1] -tri[2], cp[0] -tri[2]) > EPS)? cp[0]: cp[1];\n\tif(!isValid(r)) return INF;\n\n\t//四面体の高さを計算\n\tP cpll = crosspointLL(L(p, projection(L(tri[0], tri[1]), p)), \n\t\tL(q, projection(L(tri[2], tri[0]), q)));\n\tdouble d = distanceLP(L(tri[0], tri[1]), p);\n\tdouble a = distanceLP(L(tri[0], tri[1]), cpll);\n if(EQ(d, a)) return INF;\n\treturn sqrt(d*d -a*a);\n}\n\nint main(){\n\tvector<VP> ltoxy(80001);\n\tfor(int x=0; x<=200; x++){\n\t\tfor(int y=0; y<=200; y++){\n\t\t\tint lsq = x*x +y*y;\n\t\t\tltoxy[lsq].emplace_back(x, y);\n if(x != 0){\n ltoxy[lsq].emplace_back(-x, y);\n }\n\t\t\tif(y != 0){\n ltoxy[lsq].emplace_back(x, -y);\n }\n\t\t\tif(x != 0 && y != 0){\n ltoxy[lsq].emplace_back(-x, -y);\n }\n\t\t}\n\t}\n\n while(1){\n \tVP tri(3);\n \tP ori(0, 0);\n \tfor(int i=0; i<3; i++){\n \t\tint x,y;\n \t\tcin >> x >> y;\n \t\ttri[i] = P(x, y);\n \t}\n \tif(tri[0]==ori && tri[1]==ori && tri[2]==ori){\n \t\tbreak;\n \t}\n\n \tdouble ans = INF;\n \tfor(int l=1; l<(int)ltoxy.size(); l++){\n \t\tfor(const P &p: ltoxy[l]){\n \t\t\tfor(const P &q: ltoxy[l]){\n \t\t\t\tans = min(ans, solve(tri, tri[0]+p, tri[0]+q));\n \t\t\t}\n \t\t}\n \t}\n \tif(ans == INF){\n \t\tcout << -1 << endl;\n \t}else{\n\t \tcout << fixed << setprecision(10);\n \t\tcout << ans << endl;\n \t}\n }\n return 0;\n}", "accuracy": 1, "time_ms": 2790, "memory_kb": 7428, "score_of_the_acc": -0.9325, "final_rank": 12 }, { "submission_id": "aoj_1278_2487193", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n#define rep(i, j) for(int i=0; i < (int)(j); i++)\n#define repeat(i, j, k) for(int i = (j); i < (int)(k); i++)\ntemplate<class T> bool set_min(T &a, const T &b) { return a > b ? a = b, true : false; }\n\nconst double INF = 1e18;\nconstexpr double EPS = 1e-8;\n\nusing Point = complex<double>;\n#define X(a) real(a)\n#define Y(a) imag(a)\nstruct Segment { Point a, b; };\nstruct Circle{ Point c; double r; };\n\ntemplate<class T> bool eq(const T &a, const T &b) { return abs(a - b) < EPS; }\nnamespace std {\n bool operator == (Point a, Point b) { return eq(X(a), X(b)) and eq(Y(a), Y(b)); }\n bool operator < (Point a, Point b) { return X(a) == X(b) ? Y(a) < Y(b) : X(a) < X(b); }\n istream& operator >> (istream &is, Point &a) { double x, y; is >> x >> y; a.X(x); a.Y(y); return is; }\n double norm(Point p) { assert(0); } // complex ??? norm ?????????\n}\ndouble dot (Point a, Point b) { return a.X() * b.X() + a.Y() * b.Y(); }\ndouble cross (Point a, Point b) { return a.X() * b.Y() - a.Y() * b.X(); }\ndouble length2 (Point p ) { return p.X() * p.X() + p.Y() * p.Y(); }\ndouble length (Point p ) { return sqrt(length2(p)); }\ndouble distance(Point a, Point b) { return length(a - b); }\nPoint unit (Point p ) { return p / length(p); }\nPoint orthonormal(Point &p) { // ??£?????´???????????????\n Point p2 = Point(-1 * p.Y(), p.X()); // = rotate(p, 90);\n return p2 / length(p2);\n}\nenum ccw_t { // ??????????????¢??¨????????§ ccw_t ??? int ??§??????????????????????????¨???\n COUNTER_CLOCK_WISE = 1,\n CLOCK_WISE = -1,\n STRAIGHT_C_A_B = 2,\n STRAIGHT_A_B_C = -2,\n STRAIGHT_A_C_B = 0\n};\nccw_t ccw(Point a, Point b, Point c){\n Point ab = b - a, ac = c - a;\n if( cross(ab,ac) > EPS ) return COUNTER_CLOCK_WISE; // +1 a-b-c ???????¨???????\n if( cross(ab,ac) < -EPS ) return CLOCK_WISE; // -1 a-b-c ????¨???????\n if( dot(ab,ac) < -EPS ) return STRAIGHT_C_A_B; // +2 c-a-b\n if( length2(ab) < length2(ac) )return STRAIGHT_A_B_C; // -2 a-b-c or a==b\n return STRAIGHT_A_C_B; // 0 a-c-b or b==c or a==c\n}\nbool does_intersect(Segment a, Segment b){\n return (int)ccw(a.a, a.b, b.a) * (int)ccw(a.a, a.b, b.b) <= 0 &&\n (int)ccw(b.a, b.b, a.a) * (int)ccw(b.a, b.b, a.b) <= 0;\n}\n\nPoint intersection(Segment a, Segment b) {\n assert(does_intersect(a, b));\n double p = cross(a.b - a.a, b.b - b.a);\n double q = cross(a.b - a.a, a.b - b.a);\n if(abs(p) < EPS and abs(q) < EPS) return a.a;\n if(abs(p) < EPS) assert(0);\n return q / p * (b.b - b.a) + b.a;\n}\nPoint projection(Segment l, Point p) {\n double t = dot(p-l.a, l.a-l.b) / length2(l.a - l.b);\n Point ret = (l.a-l.b);\n return ret * t + l.a;\n}\n\nvector<Point> cross_points(Circle c1, Circle c2) {\n double d = distance(c1.c, c2.c);\n assert(d > 0);\n // c1.c ?????????c1,c2????????????????????´?????¨c1.c->c2.c ??????????????§????????¢\n double x = (pow(c1.r, 2) - pow(c2.r, 2) + pow(d, 2)) / (2 * d);\n if(c1.r < x) return {};\n double h = sqrt(pow(c1.r, 2) - pow(x, 2)); \n Point mid = unit(c2.c - c1.c) * x;\n if(eq(h, 0.0)) return { c1.c + mid };\n Point vertical = unit(Point(-Y(mid), X(mid))) * h; // ???????????????????????????\n return { c1.c + mid + vertical, c1.c + mid - vertical };\n}\n\nPoint nearest_grid_point(Point p) {\n return Point{round(p.X()), round(p.Y())};\n}\n\n\nclass Solver {\n public:\n bool solve() {\n Point a, b, c; cin >> a >> b >> c;\n if(a.X() == 0 and a.Y() == 0 and\n b.X() == 0 and b.Y() == 0 and\n c.X() == 0 and c.Y() == 0) return 0;\n\n auto in_range = [] (Point p) {\n return abs(p.X()) < 100 + EPS and abs(p.Y()) < 100 + EPS;\n };\n auto left_side = [](Point p, Point a, Point b) {\n return ccw(a, b, p) == COUNTER_CLOCK_WISE;\n };\n \n auto calc_height = [&] (Point p1, Point p2, Point p3) {\n Point p1p = projection(Segment{a, c}, p1);\n Point p2p = projection(Segment{a, b}, p2);\n Segment p1_ac = Segment{p1, p1 + (p1p - p1) * 1e3};\n Segment p2_ab = Segment{p2, p2 + (p2p - p2) * 1e3};\n if(not does_intersect(p1_ac, p2_ab)) return INF;\n Point p = intersection(p1_ac, p2_ab);\n if(p == p1 or p == p2 or p == p3) return INF;\n double l_ah = distance(a, p1);\n double l_ap = distance(a, p);\n if(p == a) return l_ah;\n if(l_ah <= l_ap) return INF;\n return sqrt(l_ah * l_ah - l_ap * l_ap);\n };\n \n double ans = INF;\n repeat(x1, -100, 100 + 1) {\n repeat(y1, -100, 100 + 1) {\n Point p1 = Point(x1, y1);\n if(ccw(a, c, p1) != COUNTER_CLOCK_WISE) continue;\n double l1 = distance(a, p1);\n int y2_mx = min<int>(100, a.Y() + l1 + 0.5);\n int y2_mn = max<int>(-100, a.Y() - l1 - 0.5); \n repeat(y2, y2_mn, y2_mx + 1) {\n double dy = abs(a.Y() - y2);\n double dx = sqrt(l1 * l1 - dy * dy);\n for(Point p2_cand : {Point{a.X() + dx, (double)y2}, Point{a.X() - dx, (double)y2}}) {\n Point p2 = nearest_grid_point(p2_cand);\n\n if(not in_range(p2) or\n not eq(distance(a, p2), l1) or\n not left_side(p2, b, a) or \n p1 == p2 or \n (left_side(p1, b, a) and not left_side(p1, p2, a))) continue; \n \n double l2 = distance(p1, c);\n double l3 = distance(p2, b);\n\n Point p3;\n\n bool ok = false;\n for(Point p3_cand : cross_points(Circle{c, l2}, Circle{b, l3})) {\n p3 = nearest_grid_point(p3_cand);\n\n if(not in_range(p3) or\n not left_side(p3, c, b) or\n p1 == p3 or\n p2 == p3 or\n (left_side(p2, c, b) and not left_side(p2, p3, b)) or\n (left_side(p3, a, c) and not left_side(p3, p1, c))) continue;\n\n if(eq(distance(c, p3), l2) and eq(distance(b, p3), l3)) {\n ok = true;\n break;\n }\n }\n if(not ok) continue;\n set_min(ans, calc_height(p1, p2, p3));\n }\n }\n }\n }\n printf(\"%.10lf\\n\", (ans == INF ? -1 : ans));\n return 1;\n }\n};\n\nint main() {\n while(Solver().solve());\n return 0;\n}", "accuracy": 1, "time_ms": 4850, "memory_kb": 3368, "score_of_the_acc": -0.8126, "final_rank": 8 }, { "submission_id": "aoj_1278_2487180", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n#define rep(i, j) for(int i=0; i < (int)(j); i++)\n#define repeat(i, j, k) for(int i = (j); i < (int)(k); i++)\ntemplate<class T> bool set_min(T &a, const T &b) { return a > b ? a = b, true : false; }\n\nconst double INF = 1e18;\nconstexpr double EPS = 1e-8;\n\nusing Point = complex<double>;\n#define X(a) real(a)\n#define Y(a) imag(a)\nstruct Segment { Point a, b; };\nstruct Circle{ Point c; double r; };\n\ntemplate<class T> bool eq(const T &a, const T &b) { return abs(a - b) < EPS; }\nnamespace std {\n bool operator == (Point a, Point b) { return eq(X(a), X(b)) and eq(Y(a), Y(b)); }\n bool operator < (Point a, Point b) { return X(a) == X(b) ? Y(a) < Y(b) : X(a) < X(b); }\n istream& operator >> (istream &is, Point &a) { double x, y; is >> x >> y; a.X(x); a.Y(y); return is; }\n double norm(Point p) { assert(0); } // complex ??? norm ?????????\n}\ndouble dot (Point a, Point b) { return a.X() * b.X() + a.Y() * b.Y(); }\ndouble cross (Point a, Point b) { return a.X() * b.Y() - a.Y() * b.X(); }\ndouble length2 (Point p ) { return p.X() * p.X() + p.Y() * p.Y(); }\ndouble length (Point p ) { return sqrt(length2(p)); }\ndouble distance(Point a, Point b) { return length(a - b); }\nPoint unit (Point p ) { return p / length(p); }\nPoint orthonormal(Point &p) { // ??£?????´???????????????\n Point p2 = Point(-1 * p.Y(), p.X()); // = rotate(p, 90);\n return p2 / length(p2);\n}\nenum ccw_t { // ??????????????¢??¨????????§ ccw_t ??? int ??§??????????????????????????¨???\n COUNTER_CLOCK_WISE = 1,\n CLOCK_WISE = -1,\n STRAIGHT_C_A_B = 2,\n STRAIGHT_A_B_C = -2,\n STRAIGHT_A_C_B = 0\n};\nccw_t ccw(Point a, Point b, Point c){\n Point ab = b - a, ac = c - a;\n if( cross(ab,ac) > EPS ) return COUNTER_CLOCK_WISE; // +1 a-b-c ???????¨???????\n if( cross(ab,ac) < -EPS ) return CLOCK_WISE; // -1 a-b-c ????¨???????\n if( dot(ab,ac) < -EPS ) return STRAIGHT_C_A_B; // +2 c-a-b\n if( length2(ab) < length2(ac) )return STRAIGHT_A_B_C; // -2 a-b-c or a==b\n return STRAIGHT_A_C_B; // 0 a-c-b or b==c or a==c\n}\nbool does_intersect(Segment a, Segment b){\n return (int)ccw(a.a, a.b, b.a) * (int)ccw(a.a, a.b, b.b) <= 0 &&\n (int)ccw(b.a, b.b, a.a) * (int)ccw(b.a, b.b, a.b) <= 0;\n}\n\nPoint intersection(Segment a, Segment b) {\n assert(does_intersect(a, b));\n double p = cross(a.b - a.a, b.b - b.a);\n double q = cross(a.b - a.a, a.b - b.a);\n if(abs(p) < EPS and abs(q) < EPS) return a.a;\n if(abs(p) < EPS) assert(0);\n return q / p * (b.b - b.a) + b.a;\n}\nPoint projection(Segment l, Point p) {\n double t = dot(p-l.a, l.a-l.b) / length2(l.a - l.b);\n Point ret = (l.a-l.b);\n return ret * t + l.a;\n}\n\nvector<Point> cross_points(Circle c1, Circle c2) {\n double d = distance(c1.c, c2.c);\n assert(d > 0);\n // c1.c ?????????c1,c2????????????????????´?????¨c1.c->c2.c ??????????????§????????¢\n double x = (pow(c1.r, 2) - pow(c2.r, 2) + pow(d, 2)) / (2 * d);\n if(c1.r < x) return {};\n double h = sqrt(pow(c1.r, 2) - pow(x, 2)); \n Point mid = unit(c2.c - c1.c) * x;\n if(eq(h, 0.0)) return { c1.c + mid };\n Point vertical = unit(Point(-Y(mid), X(mid))) * h; // ???????????????????????????\n return { c1.c + mid + vertical, c1.c + mid - vertical };\n}\n\nPoint nearest_grid_point(Point p) {\n return Point{round(p.X()), round(p.Y())};\n}\n\n\nclass Solver {\n public:\n bool solve() {\n Point a, b, c; cin >> a >> b >> c;\n if(a.X() == 0 and a.Y() == 0 and\n b.X() == 0 and b.Y() == 0 and\n c.X() == 0 and c.Y() == 0) return 0;\n \n auto calc_height = [&] (Point p1, Point p2, Point p3) {\n Point p1p = projection(Segment{a, c}, p1);\n Point p2p = projection(Segment{a, b}, p2);\n Segment p1_ac = Segment{p1, p1 + (p1p - p1) * 1e3};\n Segment p2_ab = Segment{p2, p2 + (p2p - p2) * 1e3};\n if(not does_intersect(p1_ac, p2_ab)) return INF;\n Point p = intersection(p1_ac, p2_ab);\n if(p == p1 or p == p2 or p == p3) return INF;\n double l_ah = distance(a, p1);\n double l_ap = distance(a, p);\n if(p == a) return l_ah;\n if(l_ah <= l_ap) return INF;\n return sqrt(l_ah * l_ah - l_ap * l_ap);\n };\n \n double ans = INF;\n repeat(x1, -100, 100 + 1) {\n repeat(y1, -100, 100 + 1) {\n Point p1 = Point(x1, y1);\n if(ccw(a, c, p1) != COUNTER_CLOCK_WISE) continue;\n double l1 = distance(a, p1);\n int y2_mx = min<int>(100, a.Y() + l1 + 0.5);\n int y2_mn = max<int>(-100, a.Y() - l1 - 0.5); \n repeat(y2, y2_mn, y2_mx + 1) {\n double dy = abs(a.Y() - y2);\n double dx = sqrt(l1 * l1 - dy * dy);\n for(Point p2_cand : {Point{a.X() + dx, (double)y2}, Point{a.X() - dx, (double)y2}}) {\n Point p2 = nearest_grid_point(p2_cand);\n \n if(abs(p2.X()) > 100 or abs(p2.Y()) > 100) continue;\n if(not eq(distance(a, p2), l1)) continue;\n if(ccw(b, a, p2) != COUNTER_CLOCK_WISE) continue;\n if(p1 == p2) continue;\n if(ccw(b, a, p1) == COUNTER_CLOCK_WISE and ccw(p2, a, p1) != COUNTER_CLOCK_WISE) continue;\n \n double l2 = distance(p1, c);\n double l3 = distance(p2, b);\n\n Point p3;\n\n bool ok = false;\n for(Point p3_cand : cross_points(Circle{c, l2}, Circle{b, l3})) {\n p3 = nearest_grid_point(p3_cand);\n \n if(abs(p3.X()) > 100 or abs(p3.Y()) > 100) continue;\n if(ccw(c, b, p3) != COUNTER_CLOCK_WISE) continue; \n if(p1 == p3 or p2 == p3) continue;\n if(ccw(c, b, p2) == COUNTER_CLOCK_WISE and ccw(p3, b, p2) != COUNTER_CLOCK_WISE) continue;\n if(ccw(a, c, p3) == COUNTER_CLOCK_WISE and ccw(p1, c, p3) != COUNTER_CLOCK_WISE) continue;\n \n if(eq(distance(c, p3), l2) and eq(distance(b, p3), l3)) {\n ok = true;\n break;\n }\n }\n if(not ok) continue;\n set_min(ans, calc_height(p1, p2, p3));\n }\n }\n }\n }\n printf(\"%.10lf\\n\", (ans == INF ? -1 : ans));\n return 1;\n }\n};\n\nint main() {\n while(Solver().solve());\n return 0;\n}", "accuracy": 1, "time_ms": 4680, "memory_kb": 3204, "score_of_the_acc": -0.7736, "final_rank": 4 }, { "submission_id": "aoj_1278_2487173", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\nusing pii = pair<int,int>;\nusing ll = long long;\n#define rep(i, j) for(int i=0; i < (int)(j); i++)\n#define repeat(i, j, k) for(int i = (j); i < (int)(k); i++)\n#define all(v) v.begin(),v.end()\n#define debug(x) cerr << #x << \" : \" << x << endl\n\ntemplate<class T> bool set_min(T &a, const T &b) { return a > b ? a = b, true : false; }\ntemplate<class T> bool set_max(T &a, const T &b) { return a < b ? a = b, true : false; }\n// vector\ntemplate<class T> istream& operator >> (istream &is , vector<T> &v) { for(T &a : v) is >> a; return is; }\ntemplate<class T> ostream& operator << (ostream &os , const vector<T> &v) { for(const T &t : v) os << \"\\t\" << t; return os << endl; }\n// pair\ntemplate<class T, class U> ostream& operator << (ostream &os , const pair<T, U> &v) { return os << \"<\" << v.first << \", \" << v.second << \">\"; }\n\nconst double INF = 1e18;\nconstexpr double EPS = 1e-8;\n\nusing Point = complex<double>;\n#define X(a) real(a)\n#define Y(a) imag(a)\nstruct Segment { Point a, b; };\nstruct Circle{ Point c; double r; };\n\ntemplate<class T> bool eq(const T &a, const T &b) { return abs(a - b) < EPS; }\nnamespace std {\nbool operator == (const Point &a, const Point &b) { return eq(X(a), X(b)) and eq(Y(a), Y(b)); }\nbool operator < (const Point &a, const Point &b) { return X(a) == X(b) ? Y(a) < Y(b) : X(a) < X(b); }\nistream& operator >> (istream &is, Point &a) { double x, y; is >> x >> y; a.X(x); a.Y(y); return is; }\ndouble norm(const Point &p) { assert(0); } // complex ??? norm ?????????\n}\ndouble dot (const Point &a, const Point &b) { return a.X() * b.X() + a.Y() * b.Y(); }\ndouble cross (const Point &a, const Point &b) { return a.X() * b.Y() - a.Y() * b.X(); }\ndouble length2 (const Point &p ) { return p.X() * p.X() + p.Y() * p.Y(); }\ndouble length (const Point &p ) { return sqrt(length2(p)); }\ndouble distance(const Point &a, const Point &b) { return length(a - b); }\ndouble arg (const Point &a) { return atan2(Y(a), X(a)); } // -> [-pi, pi], (0, 0) is invalid!\nPoint unit (const Point &p ) { return p / length(p); }\nPoint orthonormal(Point &p) { // ??£?????´???????????????\n Point p2 = Point(-1 * p.Y(), p.X()); // = rotate(p, 90);\n return p2 / length(p2);\n}\nenum ccw_t { // ??????????????¢??¨????????§ ccw_t ??? int ??§??????????????????????????¨???\n COUNTER_CLOCK_WISE = 1,\n CLOCK_WISE = -1,\n STRAIGHT_C_A_B = 2,\n STRAIGHT_A_B_C = -2,\n STRAIGHT_A_C_B = 0\n};\nccw_t ccw(const Point &a, const Point &b, const Point &c){\n Point ab = b - a, ac = c - a;\n if( cross(ab,ac) > EPS ) return COUNTER_CLOCK_WISE; // +1 a-b-c ???????¨???????\n if( cross(ab,ac) < -EPS ) return CLOCK_WISE; // -1 a-b-c ????¨???????\n if( dot(ab,ac) < -EPS ) return STRAIGHT_C_A_B; // +2 c-a-b\n if( length2(ab) < length2(ac) )return STRAIGHT_A_B_C; // -2 a-b-c or a==b\n return STRAIGHT_A_C_B; // 0 a-c-b or b==c or a==c\n}\n// Segment\nbool operator == (Segment a, Segment b) { return a.a == b.a and a.b == b.b; }\nbool operator != (Segment a, Segment b) { return a.a == b.a and a.b == b.b; }\nistream &operator >> (istream& is, Segment& s){ return is >> s.a >> s.b;}\nostream &operator << (ostream& os, const Segment& s){ return os << s.a << \"->\" << s.b;}\n\ndouble length(Segment s) { return distance(s.a, s.b); }\ndouble distance( Segment s ,const Point &p) {\n if( dot( s.b - s.a , p - s.a ) < EPS ) return distance(p, s.a);\n if( dot( s.a - s.b , p - s.b ) < EPS ) return distance(p, s.b);\n return abs(cross( s.b - s.a , p - s.a ) / distance(s.b, s.a));\n}\nbool does_intersect(Segment a, Segment b){\n return (int)ccw(a.a, a.b, b.a) * (int)ccw(a.a, a.b, b.b) <= 0 &&\n (int)ccw(b.a, b.b, a.a) * (int)ccw(b.a, b.b, a.b) <= 0;\n}\n\nbool does_include(Segment s, const Point &p){\n Point a = p - s.a , b = s.b - s.a;\n return ( abs(cross(a,b)) < EPS and dot(a,b) > -EPS and length(s) > dot(a,b) / length(s) - EPS);\n}\nPoint intersection(Segment a, Segment b) {\n assert(does_intersect(a, b));\n double p = cross(a.b - a.a, b.b - b.a);\n double q = cross(a.b - a.a, a.b - b.a);\n if(abs(p) < EPS and abs(q) < EPS) return a.a;\n if(abs(p) < EPS) assert(0);\n return q / p * (b.b - b.a) + b.a;\n}\nPoint projection(Segment l, const Point &p) {\n double t = dot(p-l.a, l.a-l.b) / length2(l.a - l.b);\n Point ret = (l.a-l.b);\n return ret * t + l.a;\n}\n\nvector<Point> cross_points(Circle c1, Circle c2) {\n double d = distance(c1.c, c2.c);\n assert(d > 0);\n // c1.c ?????????c1,c2????????????????????´?????¨c1.c->c2.c ??????????????§????????¢\n double x = (pow(c1.r, 2) - pow(c2.r, 2) + pow(d, 2)) / (2 * d);\n if(c1.r < x) return {};\n double h = sqrt(pow(c1.r, 2) - pow(x, 2)); \n Point mid = unit(c2.c - c1.c) * x;\n if(eq(h, 0.0)) return { c1.c + mid };\n Point vertical = unit(Point(-Y(mid), X(mid))) * h; // ???????????????????????????\n return { c1.c + mid + vertical, c1.c + mid - vertical };\n}\n\nPoint nearest_grid_point(const Point &p) {\n return Point{round(p.X()), round(p.Y())};\n}\n\nclass Solver {\n public:\n bool solve() {\n Point a, b, c; cin >> a >> b >> c;\n if(a.X() == 0 and a.Y() == 0 and\n b.X() == 0 and b.Y() == 0 and\n c.X() == 0 and c.Y() == 0) return 0;\n // if(ccw(a, b, c) != COUNTER_CLOCK_WISE) swap(b, c);\n // assert(ccw(a, b, c) == COUNTER_CLOCK_WISE);\n // assert(ccw(b, c, a) == COUNTER_CLOCK_WISE);\n // assert(ccw(c, a, b) == COUNTER_CLOCK_WISE);\n // a, b, c is ccw\n \n auto calc_height = [&] (const Point &p1, const Point &p2, const Point &p3) -> double {\n Point p1p = projection(Segment{a, c}, p1);\n Point p2p = projection(Segment{a, b}, p2);\n Segment p1_ac = Segment{p1, p1 + (p1p - p1) * 1e3};\n Segment p2_ab = Segment{p2, p2 + (p2p - p2) * 1e3};\n if(not does_intersect(p1_ac, p2_ab)) return INF;\n Point p = intersection(p1_ac, p2_ab);\n if(p == p1 or p == p2 or p == p3) return INF;\n auto calc = [&] (Point aa, Point pp) {\n double l_ah = distance(aa, pp);\n double l_ap = distance(aa, p);\n if(p == aa) return l_ah;\n if(l_ah <= l_ap) return INF;\n return sqrt(l_ah * l_ah - l_ap * l_ap);\n };\n double res = calc(a, p1);\n // if(not eq(res, calc(a, p2))) return INF;\n // if(not eq(res, calc(b, p2))) return INF;\n // if(not eq(res, calc(b, p3))) return INF;\n // if(not eq(res, calc(c, p3))) return INF;\n // if(not eq(res, calc(c, p1))) return INF;\n return res;\n }; \n double ans = INF;\n repeat(x1, -100, 100 + 1) {\n repeat(y1, -100, 100 + 1) {\n Point p1 = Point(x1, y1);\n if(ccw(a, c, p1) != COUNTER_CLOCK_WISE) continue;\n double l12 = length2(a - p1);\n double l1 = sqrt(l12);\n int y2_mx = min<int>(100, a.Y() + l1 + 0.5);\n int y2_mn = max<int>(-100, a.Y() - l1 - 0.5); \n repeat(y2, y2_mn, y2_mx + 1) {\n double dy = abs(a.Y() - y2);\n double dx = sqrt(l12 - dy * dy); \n for(Point p2_cand : {Point{a.X() + dx, (double)y2}, Point{a.X() - dx, (double)y2}}) {\n Point p2 = nearest_grid_point(p2_cand);\n if(abs(p2.X()) > 100 or abs(p2.Y()) > 100) continue;\n\n if(not eq(length2(a - p2), l12)) continue;\n if(ccw(b, a, p2) != COUNTER_CLOCK_WISE) continue;\n\n if(p1 == p2) continue;\n if(ccw(b, a, p1) == COUNTER_CLOCK_WISE and ccw(p2, a, p1) != COUNTER_CLOCK_WISE) continue;\n \n double l2 = distance(p1, c);\n double l3 = distance(p2, b);\n\n Point p3;\n\n bool ok = false;\n for(Point p3_cand : cross_points(Circle{c, l2}, Circle{b, l3})) {\n p3 = nearest_grid_point(p3_cand);\n if(abs(p3.X()) > 100 or abs(p3.Y()) > 100) continue;\n if(ccw(c, b, p3) != COUNTER_CLOCK_WISE) continue; \n if(p1 == p3 or p2 == p3) continue;\n if(ccw(c, b, p2) == COUNTER_CLOCK_WISE and ccw(p3, b, p2) != COUNTER_CLOCK_WISE) continue;\n if(ccw(a, c, p3) == COUNTER_CLOCK_WISE and ccw(p1, c, p3) != COUNTER_CLOCK_WISE) continue;\n \n if(eq(distance(c, p3), l2) and eq(distance(b, p3), l3)) {\n ok = true;\n break;\n }\n }\n if(not ok) continue;\n // assert(not does_include({a, c}, p1));\n // assert(not does_include({b, c}, p2));\n // assert(not does_include({c, a}, p3));\n // assert(eq(distance(c, p3), l2) and eq(distance(b, p3), l3));\n // assert(eq(distance(a, p1), l1) and eq(distance(c, p1), l2));\n // assert(eq(distance(a, p2), l1) and eq(distance(b, p2), l3));\n set_min(ans, calc_height(p1, p2, p3));\n }\n }\n }\n }\n printf(\"%.10lf\\n\", (ans == INF ? -1 : ans));\n return 1;\n }\n};\n\nint main() {\n while(Solver().solve());\n return 0;\n}", "accuracy": 1, "time_ms": 4620, "memory_kb": 3324, "score_of_the_acc": -0.7773, "final_rank": 5 }, { "submission_id": "aoj_1278_1429448", "code_snippet": "#include<bits/stdc++.h>\n \n#define EPS (1e-7)\n#define equals(a,b) (fabs((a)-(b)) < EPS)\n#define COUNTER_CLOCKWISE 1\n#define CLOCKWISE -1 \n#define ONLINE_BACK 2\n#define ONLINE_FRONT -2\n#define ON_SEGMENT 0\n#define REP(i,s,n) for(int i=s;i<n;i++)\n#define rep(i,n) REP(i,0,n)\n \nusing namespace std;\n \nclass Point{\npublic:\n double x,y;\n \n Point(double x = 0,double y = 0): x(x),y(y){}\n \n Point operator + (Point p){return Point(x+p.x,y+p.y);}\n Point operator - (Point p){return Point(x-p.x,y-p.y);}\n Point operator * (double a){return Point(a*x,a*y);}\n Point operator / (double a){return Point(x/a,y/a);}\n Point operator * (Point a){ return Point(x * a.x - y * a.y, x * a.y + y * a.x); }\n bool operator < (const Point& p) const{ return !equals(x,p.x)?x<p.x:(!equals(y,p.y)&&y<p.y); }\n bool operator == (const Point& p)const{ return fabs(x-p.x) < EPS && fabs(y-p.y) < EPS; }\n\n};\n \nstruct Segment{\n Point p1,p2;\n Segment(Point p1 = Point(),Point p2 = Point()):p1(p1),p2(p2){}\n bool operator < (const Segment& s) const { return ( p2 == s.p2 ) ? p1 < s.p1 : p2 < s.p2; }\n bool operator == (const Segment& s) const { return ( s.p1 == p1 && s.p2 == p2 ) || ( s.p1 == p2 && s.p2 == p1 ); }\n \n};\n \ntypedef Point Vector;\ntypedef Segment Line;\ntypedef vector<Point> Polygon;\n \ndouble dot(Point a,Point b){ return a.x*b.x + a.y*b.y; }\ndouble cross(Point a,Point b){ return a.x*b.y - a.y*b.x; }\ndouble norm(Point a){ return a.x*a.x+a.y*a.y; }\ndouble abs(Point a){ return sqrt(norm(a)); }\nPoint rotate(Point a,double rad){ return Point(cos(rad)*a.x - sin(rad)*a.y,sin(rad)*a.x + cos(rad)*a.y); }\ndouble toRad(double agl){ return agl*M_PI/180.0; }\n \nint ccw(Point p0,Point p1,Point p2){\n Point a = p1-p0;\n Point b = p2-p0;\n if(cross(a,b) > EPS)return COUNTER_CLOCKWISE;\n if(cross(a,b) < -EPS)return CLOCKWISE;\n if(dot(a,b) < -EPS)return ONLINE_BACK;\n if(norm(a) < norm(b))return ONLINE_FRONT;\n return ON_SEGMENT;\n}\n\nbool intersectSS(Line s, Line t) {\n return ccw(s.p1,s.p2,t.p1)*ccw(s.p1,s.p2,t.p2) <= 0 &&\n ccw(t.p1,t.p2,s.p1)*ccw(t.p1,t.p2,s.p2) <= 0;\n}\n \nPoint projection(Line l,Point p) {\n double t = dot(p-l.p1, l.p1-l.p2) / norm(l.p1-l.p2);\n return l.p1 + (l.p1-l.p2)*t;\n}\n\nPoint reflection(Line l,Point p) { return p + (projection(l, p) - p) * 2; }\n\nPoint crosspoint(Line l,Line m){\n double A = cross(l.p2-l.p1,m.p2-m.p1);\n double B = cross(l.p2-l.p1,l.p2-m.p1);\n if(abs(A) < EPS && abs(B) < EPS) return m.p1;\n if(abs(A) < EPS)assert(false);\n return m.p1 + (m.p2-m.p1)*(B/A);\n}\n\ndouble cross3p(Point p,Point q,Point r) { return (r.x-q.x) * (p.y -q.y) - (r.y - q.y) * (p.x - q.x); }\nbool collinear(Point p,Point q,Point r) { return fabs(cross3p(p,q,r)) < EPS; }\nbool onSegment(Point p,Point q,Point r){\n return collinear(p,q,r) && equals(sqrt(pow(p.x-r.x,2)+pow(p.y-r.y,2)) + sqrt(pow(r.x-q.x,2) + pow(r.y-q.y,2) ),sqrt(pow(p.x-q.x,2)+pow(p.y-q.y,2)) ) ;\n}\n \ndouble angle(Point a,Point b,Point c) {\n double ux = b.x - a.x, uy = b.y - a.y;\n double vx = c.x - a.x, vy = c.y - a.y;\n return acos((ux*vx + uy*vy)/sqrt((ux*ux + uy*uy) * (vx*vx + vy*vy)));\n} \n\nbool inPolygon(Polygon &poly,Point p,bool onseg = true){\n if((int)poly.size() == 0)return false;\n if( onseg ) rep(i,(int)poly.size()) if(onSegment(poly[i],poly[(i+1)%(int)poly.size()],p))return true;\n double sum = 0;\n for(int i=0; i < (int)poly.size() ;i++) {\n if( equals(cross(poly[i]-p,poly[(i+1)%poly.size()]-p),0.0) ) continue;\n if( cross3p(p,poly[i],poly[(i+1)%poly.size()]) < 0 ) sum -= angle(p,poly[i],poly[(i+1)%poly.size()]);\n else sum += angle(p,poly[i],poly[(i+1)%poly.size()]);\n }\n const double eps = 1e-5;\n return (fabs(sum - 2*M_PI) < eps || fabs(sum + 2*M_PI) < eps);\n} \n \npair<Point, Point> crosspointCC( Point C1, double r1, Point C2, double r2) {\n complex<double> c1(C1.x,C1.y);\n complex<double> c2(C2.x,C2.y);\n complex<double> A = conj(c2-c1), B = (r2*r2-r1*r1-(c2-c1)*conj(c2-c1)), C = r1*r1*(c2-c1);\n complex<double> D = B*B-4.0*A*C;\n complex<double> z1 = (-B+sqrt(D))/(2.0*A)+c1, z2 = (-B-sqrt(D))/(2.0*A)+c1;\n return pair<Point, Point>(Point(z1.real(),z1.imag()),Point(z2.real(),z2.imag()));\n}\n \n// ------\n \nbool LT(double a,double b) { return !equals(a,b) && a < b; }\nbool LTE(double a,double b) { return equals(a,b) || a < b; }\n \nbool isValid(Point p) { return LTE(-100.0,p.x) && LTE(p.x,100.0) && LTE(-100.0,p.y) && LTE(p.y,100.0); }\n\nvector<Point> base(3),ps(3);\n\nbool checkIntersect(Point p,Segment seg1,Segment seg2){\n\n rep(i,3) {\n Segment baseSeg = Segment(base[i],base[(i+1)%3]);\n if( equals(cross(baseSeg.p1-baseSeg.p2,seg1.p1-seg1.p2),0) &&\n\tequals(cross(baseSeg.p1-baseSeg.p2,seg2.p1-seg2.p2),0) ) return false;\n }\n\n Segment segs[2] = {seg1,seg2};\n rep(i,2){\n Segment seg = segs[i];\n rep(j,3){\n Segment baseSeg = Segment(base[j],base[(j+1)%3]);\n if( intersectSS(seg,baseSeg) ) {\n\tif( equals(cross(seg.p1-seg.p2,baseSeg.p1-baseSeg.p2),0) ) {\n \n\t vector<Point> vec;\n\t vec.push_back(seg.p1), vec.push_back(seg.p2), vec.push_back(baseSeg.p1), vec.push_back(baseSeg.p2);\n\t sort(vec.begin(),vec.end());\n\t vec.erase(unique(vec.begin(),vec.end()),vec.end());\n\t rep(i,(int)vec.size()-1) {\n\t Point mp = ( vec[i] + vec[i+1] ) / 2.0;\n\t if( onSegment(seg.p1,seg.p2,mp) && onSegment(baseSeg.p1,baseSeg.p2,mp) ) return false;\n\t }\n\t continue;\n\t}\n\tPoint cp = crosspoint(seg,baseSeg);\n\tif( !( cp == seg.p1 ) ) return false;\n }\n }\n }\n return true;\n}\n \ninline bool check(Point p,int j){\n if( !isValid(p) ) return false;\n double cs[3];\n rep(i,3) {\n if( onSegment(base[i],base[(i+1)%3],p) ) return false;\n Point a = base[(i+1)%3] - base[i];\n Point b = p - base[i];\n cs[i] = cross(a,b);\n }\n if( LT(cs[0],0) && LT(cs[1],0) && LT(cs[2],0) ) return false;\n if( LT(0,cs[0]) && LT(0,cs[1]) && LT(0,cs[2]) ) return false;\n if(!checkIntersect(p,Segment(base[j],p),Segment(base[(j+1)%3],p))) return false;\n return true;\n}\n \nbool FinalCheck(int limit = 3){\n Line lines[3][2];\n rep(i,limit) lines[i][0] = Line(base[i],ps[i]), lines[i][1] = Line(base[(i+1)%3],ps[i]);\n rep(i,limit) {\n REP(j,i+1,limit) {\n if( ps[i] == ps[j] ) return false;\n rep(k,2) {\n Segment ururu_beam1 = lines[i][k];\n rep(l,2){\n Segment ururu_beam2 = lines[j][l];\n if( intersectSS(ururu_beam1,ururu_beam2) ) {\n Point cp = crosspoint(ururu_beam1,ururu_beam2);\n if( cp == ururu_beam1.p1 && cp == ururu_beam2.p1 ) continue;\n return false;\n }\n }\n }\n }\n }\n return true;\n}\n \ndouble getH(){\n Line lines[3];\n for(short i=0;i<3;i++){\n Vector e = ( base[i] - base[(i+1)%3] ) / abs( base[i] - base[(i+1)%3] );\n e = rotate(e,toRad(90.0));\n lines[i] = Line(ps[i],ps[i]+e*1000);\n }\n\n Point cp = crosspoint(lines[0],lines[1]);\n for(short i=0;i<3;i++) if( cp == ps[i] ) return 1e30;\n Point onBaseP0 = crosspoint(lines[0],Line(base[0],base[1]));\n double a = abs(cp-onBaseP0);\n double b = abs(onBaseP0-ps[0]);\n if( equals(a,b) ) return 1e30;\n if( LTE(b,a) ) return 1e30;\n return sqrt(b*b-a*a);\n}\n \nvoid compute() {\n double mini = 1e30;\n REP(x1,-100,101){\n REP(y1,-100,101){\n ps[0] = Point(x1,y1); \n if( !check(ps[0],0) ) continue;\n double lengthOfPs0ToBase1 = abs(ps[0]-base[1]); \n for(int x2 = max(-100,(int)(base[1].x-lengthOfPs0ToBase1)); x2<=min(100,(int)(base[1].x+lengthOfPs0ToBase1));x2++ ) {\n\tdouble diff_x = abs(x2-base[1].x);\n\tif( LT(lengthOfPs0ToBase1,diff_x) ) continue;\n\tdouble diff_y = sqrt( lengthOfPs0ToBase1*lengthOfPs0ToBase1 - diff_x*diff_x );\n \n\tif( !equals(diff_y,(int)(diff_y+EPS)) ) continue;\n\tfor(short coef=-1;coef<=1;coef+=2){ \n\t double y2 = base[1].y + diff_y * (double)coef;\n\t if( !( equals(y2,(int)(y2+EPS)) || equals(y2,(int)(y2-EPS)) ) ) continue;\n\t if( !check(Point(x2,y2),1) ) continue;\n\t ps[1] = Point(x2,y2);\n\t if( !FinalCheck(2) ) continue;\n\t pair<Point,Point> tmp = crosspointCC(base[0],abs(ps[0]-base[0]),base[2],abs(ps[1]-base[2]));\n\t Point tps[2] = {tmp.first,tmp.second};\n\t for(short i=0;i<2;i++){\n\t ps[2] = tps[i];\n\t if( !( equals(ps[2].x,(int)(ps[2].x+EPS)) || equals(ps[2].x,(int)(ps[2].x-EPS)) ) || !( equals(ps[2].y,(int)(ps[2].y+EPS)) || equals(ps[2].y,(int)(ps[2].y-EPS)) ) ) continue;\n\t if( !check(tps[i],2) ) continue;\n\t if( !FinalCheck() ) continue;\n\t double h = getH();\n\t if( LT(h,mini) ) mini = h;\n\t }\n\t}\n }\n }\n }\n if( equals(mini,1e30) ) puts(\"-1\");\n else printf(\"%.10f\\n\",mini);\n}\n \nint main() {\n while( 1 ){\n bool fin = true;\n rep(i,3) {\n scanf(\"%lf %lf\",&base[i].x,&base[i].y);\n if( !( base[i] == Point(0,0) ) ) fin = false;\n }\n if( fin ) break;\n compute();\n }\n return 0;\n}", "accuracy": 1, "time_ms": 7090, "memory_kb": 1276, "score_of_the_acc": -0.9099, "final_rank": 9 }, { "submission_id": "aoj_1278_1429445", "code_snippet": "#include<bits/stdc++.h>\n \n#define EPS (1e-7)\n#define equals(a,b) (fabs((a)-(b)) < EPS)\n#define COUNTER_CLOCKWISE 1\n#define CLOCKWISE -1 \n#define ONLINE_BACK 2\n#define ONLINE_FRONT -2\n#define ON_SEGMENT 0\n#define REP(i,s,n) for(int i=s;i<n;i++)\n#define rep(i,n) REP(i,0,n)\n \nusing namespace std;\n \nclass Point{\npublic:\n double x,y;\n \n Point(double x = 0,double y = 0): x(x),y(y){}\n \n Point operator + (Point p){return Point(x+p.x,y+p.y);}\n Point operator - (Point p){return Point(x-p.x,y-p.y);}\n Point operator * (double a){return Point(a*x,a*y);}\n Point operator / (double a){return Point(x/a,y/a);}\n Point operator * (Point a){ return Point(x * a.x - y * a.y, x * a.y + y * a.x); }\n \n bool operator < (const Point& p) const{ return !equals(x,p.x)?x<p.x:(!equals(y,p.y)&&y<p.y); }\n \n bool operator == (const Point& p)const{ return fabs(x-p.x) < EPS && fabs(y-p.y) < EPS; }\n \n};\n \nstruct Segment{\n Point p1,p2;\n Segment(Point p1 = Point(),Point p2 = Point()):p1(p1),p2(p2){}\n bool operator < (const Segment& s) const { return ( p2 == s.p2 ) ? p1 < s.p1 : p2 < s.p2; }\n bool operator == (const Segment& s) const { return ( s.p1 == p1 && s.p2 == p2 ) || ( s.p1 == p2 && s.p2 == p1 ); }\n \n};\n \ntypedef Point Vector;\ntypedef Segment Line;\ntypedef vector<Point> Polygon;\n \nostream& operator << (ostream& os,const Point& a){ os << \"(\" << a.x << \",\" << a.y << \")\"; }\n \nostream& operator << (ostream& os,const Segment& a){ os << \"( \" << a.p1 << \" , \" << a.p2 << \" )\"; }\n \ndouble dot(Point a,Point b){ return a.x*b.x + a.y*b.y; }\n \ndouble cross(Point a,Point b){ return a.x*b.y - a.y*b.x; }\n \ndouble norm(Point a){ return a.x*a.x+a.y*a.y; }\n \ndouble abs(Point a){ return sqrt(norm(a)); }\n \n//rad ??????§?????????????????¢???????§???????????????????¨\nPoint rotate(Point a,double rad){ return Point(cos(rad)*a.x - sin(rad)*a.y,sin(rad)*a.x + cos(rad)*a.y); }\n \n// ????????????????¢????????????\ndouble toRad(double agl){ return agl*M_PI/180.0; }\n \n \nint ccw(Point p0,Point p1,Point p2){\n Point a = p1-p0;\n Point b = p2-p0;\n if(cross(a,b) > EPS)return COUNTER_CLOCKWISE;\n if(cross(a,b) < -EPS)return CLOCKWISE;\n if(dot(a,b) < -EPS)return ONLINE_BACK;\n if(norm(a) < norm(b))return ONLINE_FRONT;\n return ON_SEGMENT;\n}\n \nbool intersectLL(Line l, Line m) {\n return abs(cross(l.p2-l.p1, m.p2-m.p1)) > EPS || // non-parallel\n abs(cross(l.p2-l.p1, m.p1-l.p1)) < EPS; // same line\n}\nbool intersectLS(Line l, Line s) {\n return cross(l.p2-l.p1, s.p1-l.p1)* // s[0] is left of l\n cross(l.p2-l.p1, s.p2-l.p1) < EPS; // s[1] is right of l\n}\nbool intersectLP(Line l,Point p) {\n return abs(cross(l.p2-p, l.p1-p)) < EPS;\n}\nbool intersectSS(Line s, Line t) {\n return ccw(s.p1,s.p2,t.p1)*ccw(s.p1,s.p2,t.p2) <= 0 &&\n ccw(t.p1,t.p2,s.p1)*ccw(t.p1,t.p2,s.p2) <= 0;\n}\nbool intersectSP(Line s, Point p) {\n return abs(s.p1-p)+abs(s.p2-p)-abs(s.p2-s.p1) < EPS; // triangle inequality\n}\n \nPoint projection(Line l,Point p) {\n double t = dot(p-l.p1, l.p1-l.p2) / norm(l.p1-l.p2);\n return l.p1 + (l.p1-l.p2)*t;\n}\nPoint reflection(Line l,Point p) {\n return p + (projection(l, p) - p) * 2;\n}\ndouble distanceLP(Line l, Point p) {\n return abs(p - projection(l, p));\n}\ndouble distanceLL(Line l, Line m) {\n return intersectLL(l, m) ? 0 : distanceLP(l, m.p1);\n}\n \ndouble distanceLS(Line l, Line s) {\n if (intersectLS(l, s)) return 0;\n return min(distanceLP(l, s.p1), distanceLP(l, s.p2));\n}\ndouble distanceSP(Line s, Point p) {\n Point r = projection(s, p);\n if (intersectSP(s, r)) return abs(r - p);\n return min(abs(s.p1 - p), abs(s.p2 - p));\n}\n \ndouble distanceSS(Line s, Line t) {\n if (intersectSS(s, t)) return 0;\n return min(min(distanceSP(s, t.p1), distanceSP(s, t.p2)),\n min(distanceSP(t, s.p1), distanceSP(t, s.p2)));\n}\n \nPoint crosspoint(Line l,Line m){\n double A = cross(l.p2-l.p1,m.p2-m.p1);\n double B = cross(l.p2-l.p1,l.p2-m.p1);\n if(abs(A) < EPS && abs(B) < EPS){\n //vector<Point> vec;\n //vec.push_back(l.p1),vec.push_back(l.p2),vec.push_back(m.p1),vec.push_back(m.p2);\n //sort(vec.begin(),vec.end());\n //assert(vec[1] == vec[2]); //?????????????°??????????????????\n //return vec[1];\n //return m.p1;\n return m.p1;\n }\n if(abs(A) < EPS)assert(false);\n return m.p1 + (m.p2-m.p1)*(B/A);\n}\n \n \n//cross product of pq and pr\ndouble cross3p(Point p,Point q,Point r) { return (r.x-q.x) * (p.y -q.y) - (r.y - q.y) * (p.x - q.x); }\n \n//returns true if point r is on the same line as the line pq\nbool collinear(Point p,Point q,Point r) { return fabs(cross3p(p,q,r)) < EPS; }\n \n//returns true if point t is on the left side of line pq\nbool ccwtest(Point p,Point q,Point r){\n return cross3p(p,q,r) > 0; //can be modified to accept collinear points\n}\n \nbool onSegment(Point p,Point q,Point r){\n return collinear(p,q,r) && equals(sqrt(pow(p.x-r.x,2)+pow(p.y-r.y,2)) + sqrt(pow(r.x-q.x,2) + pow(r.y-q.y,2) ),sqrt(pow(p.x-q.x,2)+pow(p.y-q.y,2)) ) ;\n}\n \n \ndouble angle(Point a,Point b,Point c) {\n double ux = b.x - a.x, uy = b.y - a.y;\n double vx = c.x - a.x, vy = c.y - a.y;\n return acos((ux*vx + uy*vy)/sqrt((ux*ux + uy*uy) * (vx*vx + vy*vy)));\n} \n \n//??????§?????¢poly?????????????????????????????????p??????????¨?????????????????????????????? \nbool inPolygon(Polygon &poly,Point p,bool onseg = true){\n if((int)poly.size() == 0)return false;\n if( onseg ) rep(i,(int)poly.size()) if(onSegment(poly[i],poly[(i+1)%(int)poly.size()],p))return true;\n double sum = 0;\n for(int i=0; i < (int)poly.size() ;i++) {\n if( equals(cross(poly[i]-p,poly[(i+1)%poly.size()]-p),0.0) ) continue; // ??????????????????????¨angle???nan?????????sum???nan????????????????¬\n if( cross3p(p,poly[i],poly[(i+1)%poly.size()]) < 0 ) sum -= angle(p,poly[i],poly[(i+1)%poly.size()]);\n else sum += angle(p,poly[i],poly[(i+1)%poly.size()]);\n }\n // ???????????????????????????????????????????¨???????????????????§??\\????????????????????? \n const double eps = 1e-5;\n return (fabs(sum - 2*M_PI) < eps || fabs(sum + 2*M_PI) < eps);\n} \n \npair<Point, Point> crosspointCC( Point C1, double r1, Point C2, double r2) {\n complex<double> c1(C1.x,C1.y);\n complex<double> c2(C2.x,C2.y);\n complex<double> A = conj(c2-c1), B = (r2*r2-r1*r1-(c2-c1)*conj(c2-c1)), C = r1*r1*(c2-c1);\n complex<double> D = B*B-4.0*A*C;\n complex<double> z1 = (-B+sqrt(D))/(2.0*A)+c1, z2 = (-B-sqrt(D))/(2.0*A)+c1;\n return pair<Point, Point>(Point(z1.real(),z1.imag()),Point(z2.real(),z2.imag()));\n}\n \n \n// ------\n \nbool LT(double a,double b) { return !equals(a,b) && a < b; }\nbool LTE(double a,double b) { return equals(a,b) || a < b; }\n \nbool isValid(Point p) { return LTE(-100.0,p.x) && LTE(p.x,100.0) && LTE(-100.0,p.y) && LTE(p.y,100.0); }\n \nvector<Point> base(3),ps(3);\n \nPoint vir;\nbool cmp_dist(const Point &p1,const Point &p2) {\n return LT(abs(vir-p1),abs(vir-p2));\n}\n \n// seg.p1 ??? base ???????????????????§?????????????¨\n// * 6\nbool checkIntersect(Point p,Segment seg1,Segment seg2){\n \n // baseSeg ????¨ seg1 ????????? seg2 ????????????????¨???????°?????????????????????§?????¢???????????????\n rep(i,3) {\n Segment baseSeg = Segment(base[i],base[(i+1)%3]);\n if( equals(cross(baseSeg.p1-baseSeg.p2,seg1.p1-seg1.p2),0) &&\n equals(cross(baseSeg.p1-baseSeg.p2,seg2.p1-seg2.p2),0) ) return false;\n }\n \n // ???????§?????????????´?????????????§???????????????????§?????????????§??????????????????????????????§?????¢????¨????????????????????????????§????????????\n Segment segs[2] = {seg1,seg2};\n rep(i,2){\n Segment seg = segs[i];\n rep(j,3){\n Segment baseSeg = Segment(base[j],base[(j+1)%3]);\n if( intersectSS(seg,baseSeg) ) {\n if( equals(cross(seg.p1-seg.p2,baseSeg.p1-baseSeg.p2),0) ) {\n //if( equals(cross(seg.p1-seg.p2,baseSeg.p1-baseSeg.p2),0) && seg.p1 == baseSeg.p1 ) {\n //int res = ccw(seg.p1,seg.p2,baseSeg.p2);\n //if( res == ONLINE_FRONT || res == ON_SEGMENT ) return false;\n \n vector<Point> vec;\n vec.push_back(seg.p1);\n vec.push_back(seg.p2);\n vec.push_back(baseSeg.p1);\n vec.push_back(baseSeg.p2);\n sort(vec.begin(),vec.end());\n //vir = vec[0];\n //sort(vec.begin(),vec.end(),cmp_dist);\n vec.erase(unique(vec.begin(),vec.end()),vec.end());\n rep(i,(int)vec.size()-1) {\n Point mp = ( vec[i] + vec[i+1] ) / 2.0;\n if( onSegment(seg.p1,seg.p2,mp) && onSegment(baseSeg.p1,baseSeg.p2,mp) ) return false;\n }\n \n \n continue;\n }\n Point cp = crosspoint(seg,baseSeg);\n if( !( cp == seg.p1 ) ) return false;\n }\n }\n }\n \n \n \n return true;\n}\n \ninline bool check(Point p,int j){\n // ??????????????????????????????p??????????¨?????????\n if( !isValid(p) ) return false;\n \n // ??????????????????§?????¢????¨???????????????????????????§???????????°????????????\n \n // ??????????????????§?????¢???????????\\???§????????????????????????????\n //if( inPolygon(base,p,true) ) return false;\n //return !inPolygon(base,p,true);\n double cs[3];\n \n rep(i,3) {\n if( onSegment(base[i],base[(i+1)%3],p) ) return false;\n Point a = base[(i+1)%3] - base[i];\n Point b = p - base[i];\n cs[i] = cross(a,b);\n }\n if( LT(cs[0],0) && LT(cs[1],0) && LT(cs[2],0) ) return false;\n if( LT(0,cs[0]) && LT(0,cs[1]) && LT(0,cs[2]) ) return false;\n \n if(!checkIntersect(p,Segment(base[j],p),Segment(base[(j+1)%3],p))) return false;\n \n return true;\n //return checkIntersect(p,Segment(base[i],p),Segment(base[(i+1)%3],p));\n}\n \n//?????????????????????????????????????????????????????????????????????????§??????\nbool FinalCheck(int limit = 3){\n Line lines[3][2];\n rep(i,limit) lines[i][0] = Line(base[i],ps[i]), lines[i][1] = Line(base[(i+1)%3],ps[i]);\n rep(i,limit) {\n REP(j,i+1,limit) {\n if( ps[i] == ps[j] ) return false;\n rep(k,2) {\n Segment ururu_beam1 = lines[i][k];\n rep(l,2){\n Segment ururu_beam2 = lines[j][l];\n if( intersectSS(ururu_beam1,ururu_beam2) ) {\n Point cp = crosspoint(ururu_beam1,ururu_beam2);\n if( cp == ururu_beam1.p1 && cp == ururu_beam2.p1 ) continue;\n return false;\n }\n }\n }\n }\n }\n return true;\n}\n \ndouble getH(){\n \n Line lines[3];\n //rep(i,3){\n for(short i=0;i<3;i++){\n Vector e = ( base[i] - base[(i+1)%3] ) / abs( base[i] - base[(i+1)%3] );\n e = rotate(e,toRad(90.0));\n lines[i] = Line(ps[i],ps[i]+e*1000);\n }\n \n Point cp = crosspoint(lines[0],lines[1]);\n //rep(i,3) if( cp == ps[i] ) return 1e30;\n for(short i=0;i<3;i++) if( cp == ps[i] ) return 1e30;\n \n Point onBaseP0 = crosspoint(lines[0],Line(base[0],base[1]));\n \n double a = abs(cp-onBaseP0);\n double b = abs(onBaseP0-ps[0]);\n if( equals(a,b) ) return 1e30;\n if( LTE(b,a) ) return 1e30;\n \n return sqrt(b*b-a*a);\n}\n \nvoid compute() {\n double mini = 1e30;\n REP(x1,-100,101){\n REP(y1,-100,101){\n ps[0] = Point(x1,y1); \n if( !check(ps[0],0) ) continue;\n \n double lengthOfPs0ToBase1 = abs(ps[0]-base[1]); \n for(int x2 = max(-100,(int)(base[1].x-lengthOfPs0ToBase1)); x2<=min(100,(int)(base[1].x+lengthOfPs0ToBase1));x2++ ) {\n double diff_x = abs(x2-base[1].x);\n if( LT(lengthOfPs0ToBase1,diff_x) ) continue;\n double diff_y = sqrt( lengthOfPs0ToBase1*lengthOfPs0ToBase1 - diff_x*diff_x );\n \n if( !equals(diff_y,(int)(diff_y+EPS)) ) continue;\n for(short coef=-1;coef<=1;coef+=2){ // ??????£????????????????\n double y2 = base[1].y + diff_y * (double)coef;\n if( !( equals(y2,(int)(y2+EPS)) || equals(y2,(int)(y2-EPS)) ) ) continue;\n if( !check(Point(x2,y2),1) ) continue;\n ps[1] = Point(x2,y2);\n if( !FinalCheck(2) ) continue;\n pair<Point,Point> tmp = crosspointCC(base[0],abs(ps[0]-base[0]),base[2],abs(ps[1]-base[2]));\n Point tps[2] = {tmp.first,tmp.second};\n //bool succ = false;\n for(short i=0;i<2;i++){\n ps[2] = tps[i];\n if( !( equals(ps[2].x,(int)(ps[2].x+EPS)) || equals(ps[2].x,(int)(ps[2].x-EPS)) ) || !( equals(ps[2].y,(int)(ps[2].y+EPS)) || equals(ps[2].y,(int)(ps[2].y-EPS)) ) ) continue;\n if( !check(tps[i],2) ) continue;\n\n \n\n if( !FinalCheck() ) continue;\n double h = getH();\n if( LT(h,mini) ) mini = h;\n //if( !equals(h,1e30) ) { succ = true; break; }\n }\n //if( succ ) break;\n }\n }\n }\n }\n if( equals(mini,1e30) ) puts(\"-1\");\n else printf(\"%.10f\\n\",mini);\n}\n \nint main() {\n while( 1 ){\n bool fin = true;\n //rep(i,3) {\n for(short i=0;i<3;i++){\n scanf(\"%lf %lf\",&base[i].x,&base[i].y);\n if( !( base[i] == Point(0,0) ) ) fin = false;\n }\n if( fin ) break;\n compute();\n }\n return 0;\n}", "accuracy": 1, "time_ms": 7160, "memory_kb": 1276, "score_of_the_acc": -0.9193, "final_rank": 10 }, { "submission_id": "aoj_1278_1429421", "code_snippet": "#include<bits/stdc++.h>\n\n#define EPS (1e-7)\n#define equals(a,b) (fabs((a)-(b)) < EPS)\n#define COUNTER_CLOCKWISE 1\n#define CLOCKWISE -1 \n#define ONLINE_BACK 2\n#define ONLINE_FRONT -2\n#define ON_SEGMENT 0\n#define REP(i,s,n) for(int i=s;i<n;i++)\n#define rep(i,n) REP(i,0,n)\n\nusing namespace std;\n\nclass Point{\npublic:\n double x,y;\n\n Point(double x = 0,double y = 0): x(x),y(y){}\n\n Point operator + (Point p){return Point(x+p.x,y+p.y);}\n Point operator - (Point p){return Point(x-p.x,y-p.y);}\n Point operator * (double a){return Point(a*x,a*y);}\n Point operator / (double a){return Point(x/a,y/a);}\n Point operator * (Point a){ return Point(x * a.x - y * a.y, x * a.y + y * a.x); }\n\n bool operator < (const Point& p) const{ return !equals(x,p.x)?x<p.x:(!equals(y,p.y)&&y<p.y); }\n\n bool operator == (const Point& p)const{ return fabs(x-p.x) < EPS && fabs(y-p.y) < EPS; }\n\n};\n\nstruct Segment{\n Point p1,p2;\n Segment(Point p1 = Point(),Point p2 = Point()):p1(p1),p2(p2){}\n bool operator < (const Segment& s) const { return ( p2 == s.p2 ) ? p1 < s.p1 : p2 < s.p2; }\n bool operator == (const Segment& s) const { return ( s.p1 == p1 && s.p2 == p2 ) || ( s.p1 == p2 && s.p2 == p1 ); }\n\n};\n\ntypedef Point Vector;\ntypedef Segment Line;\ntypedef vector<Point> Polygon;\n\nostream& operator << (ostream& os,const Point& a){ os << \"(\" << a.x << \",\" << a.y << \")\"; }\n\nostream& operator << (ostream& os,const Segment& a){ os << \"( \" << a.p1 << \" , \" << a.p2 << \" )\"; }\n\ndouble dot(Point a,Point b){ return a.x*b.x + a.y*b.y; }\n\ndouble cross(Point a,Point b){ return a.x*b.y - a.y*b.x; }\n\ndouble norm(Point a){ return a.x*a.x+a.y*a.y; }\n\ndouble abs(Point a){ return sqrt(norm(a)); }\n\n//rad ????§???????????????¢?????§?????????????????¨\nPoint rotate(Point a,double rad){ return Point(cos(rad)*a.x - sin(rad)*a.y,sin(rad)*a.x + cos(rad)*a.y); }\n\n// ??????????????¢????????????\ndouble toRad(double agl){ return agl*M_PI/180.0; }\n\n\nint ccw(Point p0,Point p1,Point p2){\n Point a = p1-p0;\n Point b = p2-p0;\n if(cross(a,b) > EPS)return COUNTER_CLOCKWISE;\n if(cross(a,b) < -EPS)return CLOCKWISE;\n if(dot(a,b) < -EPS)return ONLINE_BACK;\n if(norm(a) < norm(b))return ONLINE_FRONT;\n return ON_SEGMENT;\n}\n\nbool intersectLL(Line l, Line m) {\n return abs(cross(l.p2-l.p1, m.p2-m.p1)) > EPS || // non-parallel\n abs(cross(l.p2-l.p1, m.p1-l.p1)) < EPS; // same line\n}\nbool intersectLS(Line l, Line s) {\n return cross(l.p2-l.p1, s.p1-l.p1)* // s[0] is left of l\n cross(l.p2-l.p1, s.p2-l.p1) < EPS; // s[1] is right of l\n}\nbool intersectLP(Line l,Point p) {\n return abs(cross(l.p2-p, l.p1-p)) < EPS;\n}\nbool intersectSS(Line s, Line t) {\n return ccw(s.p1,s.p2,t.p1)*ccw(s.p1,s.p2,t.p2) <= 0 &&\n ccw(t.p1,t.p2,s.p1)*ccw(t.p1,t.p2,s.p2) <= 0;\n}\nbool intersectSP(Line s, Point p) {\n return abs(s.p1-p)+abs(s.p2-p)-abs(s.p2-s.p1) < EPS; // triangle inequality\n}\n\nPoint projection(Line l,Point p) {\n double t = dot(p-l.p1, l.p1-l.p2) / norm(l.p1-l.p2);\n return l.p1 + (l.p1-l.p2)*t;\n}\nPoint reflection(Line l,Point p) {\n return p + (projection(l, p) - p) * 2;\n}\ndouble distanceLP(Line l, Point p) {\n return abs(p - projection(l, p));\n}\ndouble distanceLL(Line l, Line m) {\n return intersectLL(l, m) ? 0 : distanceLP(l, m.p1);\n}\n\ndouble distanceLS(Line l, Line s) {\n if (intersectLS(l, s)) return 0;\n return min(distanceLP(l, s.p1), distanceLP(l, s.p2));\n}\ndouble distanceSP(Line s, Point p) {\n Point r = projection(s, p);\n if (intersectSP(s, r)) return abs(r - p);\n return min(abs(s.p1 - p), abs(s.p2 - p));\n}\n\ndouble distanceSS(Line s, Line t) {\n if (intersectSS(s, t)) return 0;\n return min(min(distanceSP(s, t.p1), distanceSP(s, t.p2)),\n min(distanceSP(t, s.p1), distanceSP(t, s.p2)));\n}\n\nPoint crosspoint(Line l,Line m){\n double A = cross(l.p2-l.p1,m.p2-m.p1);\n double B = cross(l.p2-l.p1,l.p2-m.p1);\n if(abs(A) < EPS && abs(B) < EPS){\n vector<Point> vec;\n vec.push_back(l.p1),vec.push_back(l.p2),vec.push_back(m.p1),vec.push_back(m.p2);\n sort(vec.begin(),vec.end());\n assert(vec[1] == vec[2]); //???????????°??????????????????\n return vec[1];\n //return m.p1;\n }\n if(abs(A) < EPS)assert(false);\n return m.p1 + (m.p2-m.p1)*(B/A);\n}\n\n\n//cross product of pq and pr\ndouble cross3p(Point p,Point q,Point r) { return (r.x-q.x) * (p.y -q.y) - (r.y - q.y) * (p.x - q.x); }\n \n//returns true if point r is on the same line as the line pq\nbool collinear(Point p,Point q,Point r) { return fabs(cross3p(p,q,r)) < EPS; }\n \n//returns true if point t is on the left side of line pq\nbool ccwtest(Point p,Point q,Point r){\n return cross3p(p,q,r) > 0; //can be modified to accept collinear points\n}\n \nbool onSegment(Point p,Point q,Point r){\n return collinear(p,q,r) && equals(sqrt(pow(p.x-r.x,2)+pow(p.y-r.y,2)) + sqrt(pow(r.x-q.x,2) + pow(r.y-q.y,2) ),sqrt(pow(p.x-q.x,2)+pow(p.y-q.y,2)) ) ;\n}\n\n\ndouble angle(Point a,Point b,Point c) {\n double ux = b.x - a.x, uy = b.y - a.y;\n double vx = c.x - a.x, vy = c.y - a.y;\n return acos((ux*vx + uy*vy)/sqrt((ux*ux + uy*uy) * (vx*vx + vy*vy)));\n} \n \n//????§???¢poly?????????????????????????????????p????????¨?????????????????????????????? \nbool inPolygon(Polygon &poly,Point p,bool onseg = true){\n if((int)poly.size() == 0)return false;\n if( onseg ) rep(i,(int)poly.size()) if(onSegment(poly[i],poly[(i+1)%(int)poly.size()],p))return true;\n double sum = 0;\n for(int i=0; i < (int)poly.size() ;i++) {\n if( equals(cross(poly[i]-p,poly[(i+1)%poly.size()]-p),0.0) ) continue; // ????????????????????¨angle???nan?????????sum???nan??????????????¬\n if( cross3p(p,poly[i],poly[(i+1)%poly.size()]) < 0 ) sum -= angle(p,poly[i],poly[(i+1)%poly.size()]);\n else sum += angle(p,poly[i],poly[(i+1)%poly.size()]);\n }\n // ?????????????????????????????????????????¨?????????????????§??\\????????????????????? \n const double eps = 1e-5;\n return (fabs(sum - 2*M_PI) < eps || fabs(sum + 2*M_PI) < eps);\n} \n\npair<Point, Point> crosspointCC( Point C1, double r1, Point C2, double r2) {\n complex<double> c1(C1.x,C1.y);\n complex<double> c2(C2.x,C2.y);\n complex<double> A = conj(c2-c1), B = (r2*r2-r1*r1-(c2-c1)*conj(c2-c1)), C = r1*r1*(c2-c1);\n complex<double> D = B*B-4.0*A*C;\n complex<double> z1 = (-B+sqrt(D))/(2.0*A)+c1, z2 = (-B-sqrt(D))/(2.0*A)+c1;\n return pair<Point, Point>(Point(z1.real(),z1.imag()),Point(z2.real(),z2.imag()));\n}\n\n\n// ------\n\nbool LT(double a,double b) { return !equals(a,b) && a < b; }\nbool LTE(double a,double b) { return equals(a,b) || a < b; }\n\nbool isValid(Point p) { return LTE(-100.0,p.x) && LTE(p.x,100.0) && LTE(-100.0,p.y) && LTE(p.y,100.0); }\n\nvector<Point> base(3),ps(3);\n\nPoint vir;\nbool cmp_dist(const Point &p1,const Point &p2) {\n return LT(abs(vir-p1),abs(vir-p2));\n}\n\n// seg.p1 ??? base ?????????????????§???????????¨\n// * 6\nbool checkIntersect(Point p,Segment seg1,Segment seg2){\n\n // baseSeg ??¨ seg1 ????????? seg2 ??????????????¨?????°???????????????????§???¢???????????????\n rep(i,3) {\n Segment baseSeg = Segment(base[i],base[(i+1)%3]);\n if( equals(cross(baseSeg.p1-baseSeg.p2,seg1.p1-seg1.p2),0) &&\n\tequals(cross(baseSeg.p1-baseSeg.p2,seg2.p1-seg2.p2),0) ) return false;\n }\n\n // ?????§???????????´???????????§?????????????????§???????????§????????????????????????????§???¢??¨??????????????????????????§????????????\n Segment segs[2] = {seg1,seg2};\n rep(i,2){\n Segment seg = segs[i];\n rep(j,3){\n Segment baseSeg = Segment(base[j],base[(j+1)%3]);\n if( intersectSS(seg,baseSeg) ) {\n\tif( equals(cross(seg.p1-seg.p2,baseSeg.p1-baseSeg.p2),0) ) {\n\t//if( equals(cross(seg.p1-seg.p2,baseSeg.p1-baseSeg.p2),0) && seg.p1 == baseSeg.p1 ) {\n\t//int res = ccw(seg.p1,seg.p2,baseSeg.p2);\n\t//if( res == ONLINE_FRONT || res == ON_SEGMENT ) return false;\n\n\t vector<Point> vec;\n\t vec.push_back(seg.p1);\n\t vec.push_back(seg.p2);\n\t vec.push_back(baseSeg.p1);\n\t vec.push_back(baseSeg.p2);\n\t sort(vec.begin(),vec.end());\n\t //vir = vec[0];\n\t //sort(vec.begin(),vec.end(),cmp_dist);\n\t vec.erase(unique(vec.begin(),vec.end()),vec.end());\n\t rep(i,(int)vec.size()-1) {\n\t Point mp = ( vec[i] + vec[i+1] ) / 2.0;\n\t if( onSegment(seg.p1,seg.p2,mp) && onSegment(baseSeg.p1,baseSeg.p2,mp) ) return false;\n\t }\n\n\n\t continue;\n\t}\n\tPoint cp = crosspoint(seg,baseSeg);\n\tif( !( cp == seg.p1 ) ) return false;\n }\n }\n }\n\n return true;\n}\n\ninline bool check(Point p,int j){\n // ??????????????????????????????p????????¨?????????\n if( !isValid(p) ) return false;\n\n // ????????????????§???¢??¨?????????????????????????§?????????°????????????\n\n\n\n // ????????????????§???¢???????????\\?§????????????????????????????\n //if( inPolygon(base,p,true) ) return false;\n //return !inPolygon(base,p,true);\n double cs[3];\n\n rep(i,3) {\n if( onSegment(base[i],base[(i+1)%3],p) ) return false;\n Point a = base[(i+1)%3] - base[i];\n Point b = p - base[i];\n cs[i] = cross(a,b);\n }\n if( LT(cs[0],0) && LT(cs[1],0) && LT(cs[2],0) ) return false;\n if( LT(0,cs[0]) && LT(0,cs[1]) && LT(0,cs[2]) ) return false;\n\n if(!checkIntersect(p,Segment(base[j],p),Segment(base[(j+1)%3],p))) return false;\n\n return true;\n //return checkIntersect(p,Segment(base[i],p),Segment(base[(i+1)%3],p));\n}\n\n//???????????????????????????????????????????????????????????????????????§??????\nbool FinalCheck(int limit = 3){\n Line lines[3][2];\n rep(i,limit) lines[i][0] = Line(base[i],ps[i]), lines[i][1] = Line(base[(i+1)%3],ps[i]);\n rep(i,limit) {\n REP(j,i+1,limit) {\n if( ps[i] == ps[j] ) return false;\n rep(k,2) {\n\tSegment ururu_beam1 = lines[i][k];\n\trep(l,2){\n\t Segment ururu_beam2 = lines[j][l];\n\t if( intersectSS(ururu_beam1,ururu_beam2) ) {\n\t Point cp = crosspoint(ururu_beam1,ururu_beam2);\n\t if( cp == ururu_beam1.p1 && cp == ururu_beam2.p1 ) continue;\n\t return false;\n\t }\n\t}\n }\n }\n }\n return true;\n}\n\ndouble getH(){\n\n Line lines[3];\n rep(i,3){\n Vector e = ( base[i] - base[(i+1)%3] ) / abs( base[i] - base[(i+1)%3] );\n e = rotate(e,toRad(90.0));\n lines[i] = Line(ps[i],ps[i]+e*1000);\n }\n\n Point cp = crosspoint(lines[0],lines[1]);\n rep(i,3) if( cp == ps[i] ) return 1e30;\n \n Point onBaseP0 = crosspoint(lines[0],Line(base[0],base[1]));\n\n double a = abs(cp-onBaseP0);\n double b = abs(onBaseP0-ps[0]);\n if( equals(a,b) ) return 1e30;\n if( LTE(b,a) ) return 1e30;\n\n return sqrt(b*b-a*a);\n}\n\nvoid compute() {\n double mini = 1e30;\n REP(x1,-100,101){\n REP(y1,-100,101){\nif(y1==-21||y1==66)continue;\n if( !check(Point(x1,y1),0) ) continue;\n ps[0] = Point(x1,y1);\n double lengthOfPs0ToBase1 = abs(ps[0]-base[1]); \n //REP(x2,-100,101){\n for(int x2 = max(-100,(int)(base[1].x-lengthOfPs0ToBase1)); x2<=min(100,(int)(base[1].x+lengthOfPs0ToBase1));x2++ ) {\n\tdouble diff_x = abs(x2-base[1].x);\n\tif( LT(lengthOfPs0ToBase1,diff_x) ) continue;\n\tdouble diff_y = sqrt( lengthOfPs0ToBase1*lengthOfPs0ToBase1 - diff_x*diff_x );\n\n\tif( !equals(diff_y,(int)(diff_y+EPS)) ) continue;\n\tfor(int coef=-1;coef<=1;coef+=2){ // ????£????????????????\n\t double y2 = base[1].y + diff_y * (double)coef;\n\t if( !( equals(y2,(int)(y2+EPS)) || equals(y2,(int)(y2-EPS)) ) ) continue;\n\t if( !check(Point(x2,y2),1) ) continue;\n\t ps[1] = Point(x2,y2);\n\t if( !FinalCheck(2) ) continue;\n\t pair<Point,Point> tmp = crosspointCC(base[0],abs(ps[0]-base[0]),base[2],abs(ps[1]-base[2]));\n\t Point tps[2] = {tmp.first,tmp.second};\n\t bool succ = false;\n\t rep(i,2){\n\t if( !check(tps[i],2) ) continue;\n\t ps[2] = tps[i];\n\n\t if( !( equals(ps[2].x,(int)(ps[2].x+EPS)) || equals(ps[2].x,(int)(ps[2].x-EPS)) ) || !( equals(ps[2].y,(int)(ps[2].y+EPS)) || equals(ps[2].y,(int)(ps[2].y-EPS)) ) ) continue;\n\t if( !FinalCheck() ) continue;\n\t double h = getH();\n\t if( LT(h,mini) ) mini = h;\n\t if( !equals(h,1e30) ) { succ = true; break; }\n\t }\n\t if( succ ) break;\n\t}\n }\n }\n }\n if( equals(mini,1e30) ) puts(\"-1\");\n else printf(\"%.10f\\n\",mini);\n}\n\nint main() {\n while( 1 ){\n bool fin = true;\n rep(i,3) {\n scanf(\"%lf %lf\",&base[i].x,&base[i].y);\n if( !( base[i] == Point(0,0) ) ) fin = false;\n }\n if( fin ) break;\n compute();\n }\n return 0;\n}", "accuracy": 1, "time_ms": 7970, "memory_kb": 1280, "score_of_the_acc": -1.0291, "final_rank": 19 }, { "submission_id": "aoj_1278_1236791", "code_snippet": "#include<cstdio>\n#include<vector>\n#include<utility>\n#include<complex>\n#include<cmath>\n#include<algorithm>\n\nusing namespace std;\n\ntypedef double Real;\ntypedef pair<int,int> P;\n\nvector<P> dirs[200*200*2+1];\n\nint crP(int x1,int y1,int x2,int y2){\n\treturn x1*y2-x2*y1;\n}\n\nvoid init_dir(){\n\tfor(int x=-200;x<=200;x++) for(int y=-200;y<=200;y++){\n\t\tdirs[x*x+y*y].push_back(P(x,y));\n\t}\n}\n\nReal getHeight(Real a,Real b,Real c){\n\tReal y=(a + (b*b-c*c)/a)/2;\n\treturn sqrt(b*b-y*y);\n}\n\nReal getH2(Real a,Real b,Real c){\n\tReal y = (a + (b*b-c*c)/a)/2;\n\treturn a-y;\n}\n\nconst Real eps = 1e-8;\n\nint cnt=0;\nReal getHeight(Real a,Real b,Real c,Real d,Real e,Real f){\n//\tprintf(\"%f %f %f %f %f %f\\n\",a,b,c,d,e,f);\n\tReal h_=getHeight(a,d,f);\n\tReal x=getH2(a,d,f);\n\tReal h2=getHeight(a,b,c);\n\tReal y=getH2(a,b,c);\n\tReal p=y-x;\n\tReal q=h2-h_;\n\tReal val = -(e*e - h_*h_ - (h_+q)*(h_+q) - p*p) / (2.0 * (h_+q) * h_);\n\tif(abs(val) >= 1.0 - eps) return -1;\n//\tprintf(\"%f %f %f %f %f %f %f %f\\n\",h_,x,h2,y,p,q,val,h_*sqrt(1-val*val));\n//\tif(++cnt>10) exit(0);\n\treturn h_ * sqrt(1-val*val);\n}\n\nbool sameSide(int x1,int y1,int x2,int y2,int x3,int y3,int x4,int y4){\n\tint p3 = crP(x2-x1,y2-y1,x3-x2,y3-y2);\n\tint p4 = crP(x2-x1,y2-y1,x4-x2,y4-y2);\n\treturn p3*p4>=0;\n}\n\nint dis2(int x1,int y1,int x2,int y2){\n\treturn (x2-x1)*(x2-x1)+(y2-y1)*(y2-y1);\n}\n\nReal dis(int x1,int y1,int x2,int y2){\n\treturn sqrt(dis2(x1,y1,x2,y2));\n}\n\nbool check(int x){\n\treturn x>=-100&&x<=100;\n}\n\nReal solve(int x1,int y1,int x2,int y2,int x3,int y3){\n\tReal a=dis(x1,y1,x2,y2);\n\tReal b=dis(x2,y2,x3,y3);\n\tReal c=dis(x3,y3,x1,y1);\n\tReal res = -1;\n\tfor(int x4=-100;x4<=100;x4++) for(int y4=-100;y4<=100;y4++){\n\t\tif(sameSide(x1,y1,x2,y2,x3,y3,x4,y4)) continue;\n\t\tint d2=dis2(x2,y2,x4,y4);\n\t\tReal d=sqrt(d2);\n\t\tint f2=dis2(x1,y1,x4,y4);\n\t\tReal f=sqrt(f2);\n\t\tfor(int i = 0; i < dirs[d2].size(); i++){\n\t\t\tfor(int j = 0; j < dirs[f2].size(); j++){\n\t\t\t\tint x5 = x2 + dirs[d2][i].first;\n\t\t\t\tint y5 = y2 + dirs[d2][i].second;\n\t\t\t\tint x6 = x1 + dirs[f2][j].first;\n\t\t\t\tint y6 = y1 + dirs[f2][j].second;\n\t\t\t\tif((!check(x5)) || (!check(y5)) || (!check(x6)) || (!check(y6))) continue;\n\t\t\t\tif(sameSide(x2,y2,x3,y3,x1,y1,x5,y5)) continue;\n\t\t\t\tif(sameSide(x3,y3,x1,y1,x2,y2,x6,y6)) continue;\n\t\t\t\tint e2 = dis2(x3,y3,x6,y6);\n\t\t\t\tif(e2 != dis2(x3,y3,x5,y5)) continue;\n\t\t\t\tReal e = sqrt(e2);\n\t\t\t\tReal h = getHeight(a,b,c,d,e,f);\n\t\t\t\tif(h > 0 && (res < 0 || res > h)) res = h;\n\t//\t\t\tif(d2==5&&e2==5) printf(\"%d %d %d %f\\n\",d2,e2,f2,h);\n\t\t\t}\n\t\t}\n\t}\n\treturn res;\n}\n\nint main(){\n\tinit_dir();\n\twhile(true){\n\t\tint x1,y1,x2,y2,x3,y3;\n\t\tscanf(\"%d%d%d%d%d%d2\",&x1,&y1,&x2,&y2,&x3,&y3);\n\t\tif(x1==0&&y1==0&&x2==0&&y2==0&&x3==0&&y3==0) break;\n\t\tReal ans = solve(x1,y1,x2,y2,x3,y3);\n\t\tif(ans < 0) printf(\"-1\\n\");\n\t\telse printf(\"%.9f\\n\",ans);\n//\t\tbreak;\n\t}\n\treturn 0;\n}", "accuracy": 1, "time_ms": 1230, "memory_kb": 4440, "score_of_the_acc": -0.4292, "final_rank": 1 }, { "submission_id": "aoj_1278_192777", "code_snippet": "#include <iostream>\n#include <vector>\n#include <complex>\n\nusing namespace std;\n\ntypedef complex<double> P;\ntypedef pair<P,double> circle;\nstruct L { P p, q; L(P p, P q) : p(p), q(q) {} };\n\ntemplate <class T>\ninline const T& abs(const T& a)\n{\n\treturn a > 0 ? a : -a;\n}\n\nconst double EPS = 1e-8;\n\ndouble dot(P a, P b) { return real(conj(a)*b); }\ndouble cross(P a, P b) { return imag(conj(a)*b); }\n\nP footPerp(L l, P p) {\n\treturn l.p + (l.q-l.p) * dot(l.q-l.p, p-l.p) / norm(l.q-l.p);\n}\n\ndouble lenPerp(L l, P p){\n\treturn abs(footPerp(l, p)-p);\n}\n\nP ssCrosspoint(L a, L b){\n\tdouble A = cross(a.q-a.p, b.q-b.p);\n\tdouble B = cross(a.q-a.p, a.q-b.p);\n\treturn b.p + B/A * (b.q-b.p);\n}\n\nbool crossCircle(circle c1, circle c2){\n\tdouble r1 = c1.second;\n\tdouble r2 = c2.second;\n\tdouble d = abs(c1.first-c2.first);\n\tif(d<r1||d<r2)\n\t\treturn d-abs(r1-r2)>EPS;\n\treturn r1+r2-d>-EPS;\n}\n\nvector<P> crossPoint(circle c1, circle c2){\n\tvector<P> res;\n\tdouble r1 = c1.second;\n\tdouble r2 = c2.second;\n\tdouble r3 = abs(c1.first-c2.first);\n\tdouble rc = (r3*r3+r1*r1-r2*r2)/(2*r3);\n\tdouble rs = sqrt(r1*r1-rc*rc);\n\tP dif = (c2.first-c1.first)/r3;\n\tres.push_back(c1.first+dif*P(rc,rs));\n\tif(abs(rs) > EPS) res.push_back(c1.first+dif*P(rc,-rs));\n\treturn res;\n}\n\nint main(){\n\tint x1, y1, x2, y2, x3, y3;\n\twhile(cin >> x1 >> y1 >> x2 >> y2 >> x3 >> y3, x1|y1|x2|y2|x3|y3){\n\t\tx1 += 100, y1 += 100;\n\t\tx2 += 100, y2 += 100;\n\t\tx3 += 100, y3 += 100;\n\t\twhile(x1-x2>0){\n\t\t\tint tmp = x1; x1 = x2; x2 = x3; x3 = tmp;\n\t\t\t tmp = y1; y1 = y2; y2 = y3; y3 = tmp;\n\t\t}\n\t\tP p1 = P(x1, y1), p2 = P(x2, y2), p3 = P(x3, y3);\n\t\tdouble res = 1e12;\n\t\tint cnt = 0;\n\t\tfor(int xa=0;xa<=200;xa++){\n\t\t\tfor(int ya=0;ya<=200;ya++){\n\t\t\t\tif(cross(p2-p1, P(xa,ya)-p1) > -EPS) break;\n\t\t\t\tdouble len = abs(P(xa,ya)-p2);\n\t\t\t\tint r1 = int(len+EPS);\n\t\t\t\tfor(int xb=max(0,x2-r1);xb<=min(200,x2+r1);xb++){\n\t\t\t\t\tif(len < abs(x2-xb)) continue;\n\t\t\t\t\tdouble dsq = sqrt(len*len-(x2-xb)*(x2-xb));\n\t\t\t\t\tint sqt = int(dsq+EPS);\n\t\t\t\t\tif(abs(dsq-sqt) > EPS) continue;\n\t\t\t\t\tfor(int sgn=-1;sgn<=1;sgn+=2){\n\t\t\t\t\t\tint yb = sgn*sqt + y2;\n\t\t\t\t\t\tif(yb < 0 || 200 < yb) continue;\n\t\t\t\t\t\tif(abs(abs(p2-P(xb,yb)) - len) > EPS || abs(P(xb,yb)-P(xa,ya)) < EPS) continue;\n\t\t\t\t\t\tif(cross(p3-p2, P(xb,yb)-p2) > -EPS) continue;\n\t\t\t\t\t\tcircle ca = make_pair(p1, abs(p1-P(xa,ya)));\n\t\t\t\t\t\tcircle cb = make_pair(p3, abs(p3-P(xb,yb)));\n\n\t\t\t\t\t\tP pt = ssCrosspoint(L(P(xa,ya), P(xa,ya)+P(0,1)*(p2-p1)), L(P(xb,yb), P(xb,yb)+P(0,1)*(p3-p2)));\n\t\t\t\t\t\tdouble la = lenPerp(L(p1,p2), P(xa,ya));\n\t\t\t\t\t\tdouble lb = lenPerp(L(p1,p2), pt);\n\t\t\t\t\t\tif(la*la-lb*lb < EPS) continue;\n\t\t\t\t\t\tif(sqrt(la*la-lb*lb) > res) continue;\n\n\t\t\t\t\t\tif(!crossCircle(ca, cb)) continue;\n\t\t\t\t\t\tvector<P> cp = crossPoint(ca, cb);\n\t\t\t\t\t\tfor(int i=0;i<cp.size();i++){\n\t\t\t\t\t\t\tint xc = cp[i].real()+0.5;\n\t\t\t\t\t\t\tint yc = cp[i].imag()+0.5;\n\t\t\t\t\t\t\tif(xc < 0 || 200 < xc || yc < 0 || 200 < yc) continue;\n\t\t\t\t\t\t\tif(cross(p1-p3, P(xc,yc)-p3) > -EPS) continue;\n\t\t\t\t\t\t\tif(abs(P(xc,yc)-cp[i]) > EPS) continue;\n\t\t\t\t\t\t\tres = min(res, sqrt(la*la-lb*lb));\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\tif(res < 1e10) printf(\"%.7lf\\n\", res);\n\t\telse puts(\"-1\");\n\t}\n}", "accuracy": 1, "time_ms": 6250, "memory_kb": 988, "score_of_the_acc": -0.7683, "final_rank": 2 }, { "submission_id": "aoj_1278_192776", "code_snippet": "#include <iostream>\n#include <vector>\n#include <complex>\n\nusing namespace std;\n\ntypedef complex<double> P;\ntypedef pair<P,double> circle;\nstruct L { P p, q; L(P p, P q) : p(p), q(q) {} };\n\ntemplate <class T>\ninline const T& abs(const T& a)\n{\n\treturn a > 0 ? a : -a;\n}\n\nconst double EPS = 1e-8;\n\ndouble dot(P a, P b) { return real(conj(a)*b); }\ndouble cross(P a, P b) { return imag(conj(a)*b); }\n\nP footPerp(L l, P p) {\n\treturn l.p + (l.q-l.p)*(dot(l.q-l.p, p-l.p) / (abs(l.q-l.p)*abs(l.q-l.p)));\n}\n\ndouble lenPerp(L l, P p){\n\treturn abs(footPerp(l, p)-p);\n}\n\nP ssCrosspoint(L a, L b){\n\tdouble A = cross(a.q-a.p, b.q-b.p);\n\tdouble B = cross(a.q-a.p, a.q-b.p);\n\treturn b.p + B/A * (b.q-b.p);\n}\n\nbool crossCircle(circle c1, circle c2){\n\tdouble r1 = c1.second;\n\tdouble r2 = c2.second;\n\tdouble d = abs(c1.first-c2.first);\n\tif(d<r1||d<r2)\n\t\treturn d-abs(r1-r2)>EPS;\n\treturn r1+r2-d>-EPS;\n}\n\nvector<P> crossPoint(circle c1, circle c2){\n\tvector<P> res;\n\tdouble r1 = c1.second;\n\tdouble r2 = c2.second;\n\tdouble r3 = abs(c1.first-c2.first);\n\tdouble rc = (r3*r3+r1*r1-r2*r2)/(2*r3);\n\tdouble rs = sqrt(r1*r1-rc*rc);\n\tP dif = (c2.first-c1.first)/r3;\n\tres.push_back(c1.first+dif*P(rc,rs));\n\tif(abs(rs) > EPS) res.push_back(c1.first+dif*P(rc,-rs));\n\treturn res;\n}\n\nint main(){\n\tint x1, y1, x2, y2, x3, y3;\n\twhile(cin >> x1 >> y1 >> x2 >> y2 >> x3 >> y3, x1|y1|x2|y2|x3|y3){\n\t\tx1 += 100, y1 += 100;\n\t\tx2 += 100, y2 += 100;\n\t\tx3 += 100, y3 += 100;\n\t\twhile(x1-x2>0){\n\t\t\tint tmp = x1; x1 = x2; x2 = x3; x3 = tmp;\n\t\t\t tmp = y1; y1 = y2; y2 = y3; y3 = tmp;\n\t\t}\n\t\tP p1 = P(x1, y1), p2 = P(x2, y2), p3 = P(x3, y3);\n\t\tdouble res = 1e12;\n\t\tint cnt = 0;\n\t\tfor(int xa=0;xa<=200;xa++){\n\t\t\tfor(int ya=0;ya<=200;ya++){\n\t\t\t\tif(cross(p2-p1, P(xa,ya)-p1) > -EPS) break;\n\t\t\t\tdouble len = abs(P(xa,ya)-p2);\n\t\t\t\tint r1 = int(len+EPS);\n\t\t\t\tfor(int xb=max(0,x2-r1);xb<=min(200,x2+r1);xb++){\n\t\t\t\t\tif(len < abs(x2-xb)) continue;\n\t\t\t\t\tdouble dsq = sqrt(len*len-(x2-xb)*(x2-xb));\n\t\t\t\t\tint sqt = int(dsq+EPS);\n\t\t\t\t\tif(abs(dsq-sqt) > EPS) continue;\n\t\t\t\t\tfor(int sgn=-1;sgn<=1;sgn+=2){\n\t\t\t\t\t\tint yb = sgn*sqt + y2;\n\t\t\t\t\t\tif(yb < 0 || 200 < yb) continue;\n\t\t\t\t\t\tif(abs(abs(p2-P(xb,yb)) - len) > EPS || abs(P(xb,yb)-P(xa,ya)) < EPS) continue;\n\t\t\t\t\t\tif(cross(p3-p2, P(xb,yb)-p2) > -EPS) continue;\n\t\t\t\t\t\tcircle ca = make_pair(p1, abs(p1-P(xa,ya)));\n\t\t\t\t\t\tcircle cb = make_pair(p3, abs(p3-P(xb,yb)));\n\n\t\t\t\t\t\tP pt = ssCrosspoint(L(P(xa,ya), P(xa,ya)+P(0,1)*(p2-p1)), L(P(xb,yb), P(xb,yb)+P(0,1)*(p3-p2)));\n\t\t\t\t\t\tdouble la = lenPerp(L(p1,p2), P(xa,ya));\n\t\t\t\t\t\tdouble lb = lenPerp(L(p1,p2), pt);\n\t\t\t\t\t\tif(la*la-lb*lb < EPS) continue;\n\t\t\t\t\t\tif(sqrt(la*la-lb*lb) > res) continue;\n\n\t\t\t\t\t\tif(!crossCircle(ca, cb)) continue;\n\t\t\t\t\t\tvector<P> cp = crossPoint(ca, cb);\n\t\t\t\t\t\tfor(int i=0;i<cp.size();i++){\n\t\t\t\t\t\t\tint xc = cp[i].real()+0.5;\n\t\t\t\t\t\t\tint yc = cp[i].imag()+0.5;\n\t\t\t\t\t\t\tif(xc < 0 || 200 < xc || yc < 0 || 200 < yc) continue;\n\t\t\t\t\t\t\tif(cross(p1-p3, P(xc,yc)-p3) > -EPS) continue;\n\t\t\t\t\t\t\tif(abs(P(xc,yc)-cp[i]) > EPS) continue;\n\t\t\t\t\t\t\tres = min(res, sqrt(la*la-lb*lb));\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\tif(res < 1e10) printf(\"%.7lf\\n\", res);\n\t\telse puts(\"-1\");\n\t}\n}", "accuracy": 1, "time_ms": 6260, "memory_kb": 984, "score_of_the_acc": -0.7693, "final_rank": 3 }, { "submission_id": "aoj_1278_101397", "code_snippet": "#include<iostream>\n#include<complex>\n#include<cmath>\nusing namespace std;\n#define REP(i,b,n) for(int i=b;i<n;i++)\n#define rep(i,n) REP(i,0,n)\n#define mp make_pair\n\nconst double eps = 1e-10;\n\ntypedef complex<int> P;\ntypedef complex<double> DP;\n\nclass Tri{\npublic:\n int x[3],y[3];\n};\n\ninline double compute_dist(double x,double y){\n return sqrt(x*x+y*y);\n}\n\nint double_to_int(double &a){\n if (a <0)return (int)(a-0.5);\n else return (int)(a+0.5);\n}\n\nbool isint(double a){\n if (a <0){\n if ( fabs((int)(a-0.5)-a)<eps)return true;\n }else {\n if ( fabs((int)(a+0.5)-a)<eps)return true;\n }\n return false;\n}\n\ndouble myabs(P a){\n return sqrt(a.real()*a.real()+a.imag()*a.imag());\n}\n\ninline void CC_intersection(int &ax,int &ay,int &bx,int &by,double &r1,double &r2,double &d,\n\t\t\t double &x1,double &y1,double &x2,double &y2){\n double l,m,n;\n double x,y;\n double p,q;\n l=(d*d-r2*r2+r1*r1)/(2*d);\n \n x=ax+(bx-ax)*l/d;\n y=ay+(by-ay)*l/d;\n n=sqrt(r1*r1-l*l);\n p=(by-ay)/d*n;\n q=(bx-ax)/d*n;\n \n x1=x+p;\n y1=y-q;\n\n x2=x-p;\n y2=y+q;\n}\n\nint is_intersected_circle(int &ax,int &ay,int &bx,int &by,\n\t\t\t double &r1,double& r2,double &d){\n d = compute_dist(ax-bx,ay-by);\n if (d<eps && fabs(r1-r2)<eps)return 3;\n if (d+r2 < r1)return 0;\n if (d+r1 < r2)return 1;\n if (d > r1+r2)return 4;\n return 2;\n}\n\ninline double cross(double ax,double ay,double bx,double by){\n return ax*by - ay*bx;\n}\n\ninline double dot(double ax,double ay,double bx,double by){\n return ax*bx+ay*by;}\n\ninline bool isp(int &ax,int & ay,int & bx,int & by,int & cx,int & cy){\n double tmp1=cross(bx-ax,by-ay,cx-ax,cy-ay);\n double tmp2=dot(bx-ax,by-ay,cx-ax,cy-ay);\n return tmp1<eps&&tmp1>-eps&&tmp2<eps&&tmp2>-eps;\n}\n\ninline bool is_intersected_ls(int &a1x,int &a1y,int &a2x,int &a2y,int &b1x,int &b1y,int &b2x,int &b2y){\n if ( ( cross(a2x-a1x,a2y-a1y,b1x-a1x,b1y-a1y)*cross(a2x-a1x,a2y-a1y,b2x-a1x,b2y-a1y)<-eps) && \n\t( cross(b2x-b1x,b2y-b1y,a1x-b1x,a1y-b1y)*cross(b2x-b1x,b2y-b1y,a2x-b1x,a2y-b1y)<-eps))\n return true;\n return false;\n}\n\ninline bool is_in(int *inx,int *iny,int &ax,int& ay){\n int cnt =0;\n int n = 3;\n rep(i,n){\n int curx=inx[i],cury=iny[i];\n int nextx=inx[(i+1)%n],nexty=iny[(i+1)%n];\n curx-=ax;\n cury-=ay;\n nextx-=ax;\n nexty-=ay;\n if (cury > nexty)swap(curx,nextx),swap(cury,nexty);\n if (cury<0 && 0<=nexty &&\n\tcross(nextx,nexty,curx,cury)>=0)cnt++;\n if (isp(inx[i],iny[i],inx[(i+1)%n],iny[(i+1)%n],ax,ay))return true;\n }\n if (cnt %2 == 1)return true;\n else return false;\n}\n\ninline double distance_l_p(int &ax,int &ay,int &bx,int &by,int &cx,int &cy){\n return fabs(cross(bx-ax,by-ay,cx-ax,cy-ay))/compute_dist(bx-ax,by-ay);\n}\n\ninline double solve_linear(double A1,double B1,double C1,\n\t\t double A2,double B2,double C2,\n\t\t\t double x2,double y2,double d2\n\t\t ){\n if (fabs(A1*B2-A2*B1)<1e-8|| fabs(A1)<eps){\n return -1;\n }\n double y = (A1*C2-A2*C1)/(A1*B2-A2*B1);\n double x = (C1-B1*y)/A1;\n double z = d2*d2 - (x-x2)*(x-x2) - (y-y2)*(y-y2);\n if (z < 1e-4)return -1;\n\n return sqrt(z);\n if (isnan(z)||z<0)return -1;\n else return sqrt(z);\n}\n\ndouble ans;\ninline void compute_height(Tri in[4]){\n double r[3];\n REP(i,1,4){\n r[i-1] = compute_dist(in[i].x[2]-in[i].x[0],\n\t\t\t in[i].y[2]-in[i].y[0]);\n }\n double tmp = solve_linear((in[2].x[0]-in[1].x[0])<<1,\n\t\t\t (in[2].y[0]-in[1].y[0])<<1,\n\t\t\t r[0]*r[0]-r[1]*r[1]\n\t\t\t -(in[1].x[0]*in[1].x[0]-in[2].x[0]*in[2].x[0])\n\t\t\t -(in[1].y[0]*in[1].y[0]-in[2].y[0]*in[2].y[0]),\n\t\t\t (in[2].x[0]-in[3].x[0])<<1,\n\t\t\t (in[2].y[0]-in[3].y[0])<<1,\n\t\t\t r[2]*r[2]-r[1]*r[1]\n\t\t\t -(in[3].x[0]*in[3].x[0]-in[2].x[0]*in[2].x[0])\n\t\t\t -(in[3].y[0]*in[3].y[0]-in[2].y[0]*in[2].y[0]),\n\t\t\t in[2].x[0],in[2].y[0],r[1]\n\t\t\t );\n if (tmp < 0.00001||isnan(tmp));\n else {\n ans = min(ans,tmp);\n return;\n }\n return;\n}\n\ninline bool isvalid(Tri in[4],int now){\n rep(j,3){\n if ((int)(in[now].x[j])<-100||(int)(in[now].x[j])>100 ||\n\t(int)(in[now].y[j])<-100||(int)(in[now].y[j])>100){\n return false;\n }\n }\n\n rep(j,now){\n rep(k,3){\n if (is_intersected_ls(in[now].x[0],in[now].y[0],in[now].x[2] ,in[now].y[2],\n\t\t\t in[j].x[k] ,in[j].y[k] ,in[j].x[(k+1)%3],in[j].y[(k+1)%3])){\n\treturn false;\n }\n if (is_intersected_ls(in[now].x[1],in[now].y[1],in[now].x[2] ,in[now].y[2],\n\t\t\t in[j].x[k] ,in[j].y[k] ,in[j].x[(k+1)%3],in[j].y[(k+1)%3])){\n\treturn false;\n }\n if (isp(in[j].x[k],in[j].y[k],in[j].x[(k+1)%3],in[j].y[(k+1)%3],in[now].x[2] ,in[now].y[2])){\n\treturn false;\n }\n }\n \n if (is_in(in[j].x,in[j].y,in[now].x[2],in[now].y[2])){\n return false;\n }\n\n if (j != 0 && fabs(in[now].x[2]-in[j].x[2])<1e-8&&fabs(in[now].y[2]-in[j].y[2])<1e-8){\n return false;\n } \n }\n return true;\n}\n\ninline void decide3(Tri in[4],double &dist2){\n double dist1=compute_dist(in[1].x[0]-in[1].x[2],in[1].y[0]-in[1].y[2]);\n P c1(in[3].x[1],in[3].y[1]);\n P c2(in[3].x[0],in[3].y[0]);\n\n double tx1,tx2,ty1,ty2,d;\n int x1,x2,y1,y2;\n if (is_intersected_circle(c1.real(),c1.imag(),c2.real(),c2.imag(),dist1,dist2,d) == 2){\n CC_intersection(c1.real(),c1.imag(),c2.real(),c2.imag(),dist1,dist2,d,tx1,ty1,tx2,ty2);\n\n if (isint(tx1)&&isint(ty1)){\n x1=double_to_int(tx1);\n y1=double_to_int(ty1);\n in[3].x[2]=x1;\n in[3].y[2]=y1;\n if (isvalid(in,3))compute_height(in);\n }\n\t\n if (isint(tx2)&&isint(ty2)){\n x2=double_to_int(tx2);\n y2=double_to_int(ty2);\n in[3].x[2]=x2;\n in[3].y[2]=y2;\n if (isvalid(in,3))compute_height(in);\n }\n }\n}\n\ninline void decide2(Tri in[4]){\n double dist=compute_dist(in[1].x[1]-in[1].x[2],in[1].y[1]-in[1].y[2]);\n int lim = (int)(dist+0.1);\n for(int x=0;x<=lim;x++){\n double ty = sqrt(dist*dist-x*x+eps);\n if ( !isint(ty))continue;\n int y = double_to_int(ty);\n in[2].x[2]=in[2].x[0]+x;\n in[2].y[2]=in[2].y[0]+y;\n double dist2=compute_dist(in[2].x[2]-in[2].x[1],in[2].y[2]-in[2].y[1]);\n if (isvalid(in,2))decide3(in,dist2);\n in[2].y[2]=in[2].y[0]-y;\n dist2=compute_dist(in[2].x[2]-in[2].x[1],in[2].y[2]-in[2].y[1]);\n if (isvalid(in,2))decide3(in,dist2);\n \n in[2].x[2]=in[2].x[0]-x;\n in[2].y[2]=in[2].y[0]+y;\n dist2=compute_dist(in[2].x[2]-in[2].x[1],in[2].y[2]-in[2].y[1]);\n if (isvalid(in,2))decide3(in,dist2);\n \n in[2].y[2]=in[2].y[0]-y;\n dist2=compute_dist(in[2].x[2]-in[2].x[1],in[2].y[2]-in[2].y[1]);\n if (isvalid(in,2))decide3(in,dist2);\n \n }\n}\n\ninline void decide1(Tri in[4]){\n REP(y,-100,101){\n REP(x,-100,101){\n in[1].x[2]=x;\n in[1].y[2]=y;\n if (isvalid(in,1))decide2(in);\n }\n }\n}\n\nmain(){\n Tri in[4];\n \n while(cin>>in[0].x[0]>>in[0].y[0]\n\t>>in[0].x[1]>>in[0].y[1]\n\t>>in[0].x[2]>>in[0].y[2]){\n\n if (in[0].x[0] == 0 && in[0].y[0] == 0 &&\n\tin[0].x[1] == 0 && in[0].y[1] == 0 &&\n\tin[0].x[2] == 0 && in[0].y[2] == 0)break;\n \n in[1].x[0]=in[0].x[0]; in[1].y[0]=in[0].y[0];\n in[1].x[1]=in[0].x[1]; in[1].y[1]=in[0].y[1];\n\n in[2].x[0]=in[0].x[1]; in[2].y[0]=in[0].y[1];\n in[2].x[1]=in[0].x[2]; in[2].y[1]=in[0].y[2];\n \n in[3].x[0]=in[0].x[2]; in[3].y[0]=in[0].y[2];\n in[3].x[1]=in[0].x[0]; in[3].y[1]=in[0].y[0];\n\n ans = 1e100;\n decide1(in);\n if (ans >1e99)printf(\"-1\\n\");\n else printf(\"%.7lf\\n\",ans);\n }\n}", "accuracy": 1, "time_ms": 7990, "memory_kb": 960, "score_of_the_acc": -1.0004, "final_rank": 18 }, { "submission_id": "aoj_1278_101396", "code_snippet": "#include<iostream>\n#include<complex>\n#include<cmath>\nusing namespace std;\n#define REP(i,b,n) for(int i=b;i<n;i++)\n#define rep(i,n) REP(i,0,n)\n#define mp make_pair\n\nconst double eps = 1e-10;\n\ntypedef complex<int> P;\ntypedef complex<double> DP;\n\nclass Tri{\npublic:\n int x[3],y[3];\n};\n\ninline double compute_dist(double x,double y){\n return sqrt(x*x+y*y);\n}\n\nint double_to_int(double &a){\n if (a <0)return (int)(a-0.5);\n else return (int)(a+0.5);\n}\n\nbool isint(double a){\n if (a <0){\n if ( fabs((int)(a-0.5)-a)<eps)return true;\n }else {\n if ( fabs((int)(a+0.5)-a)<eps)return true;\n }\n return false;\n}\n\ndouble myabs(P a){\n return sqrt(a.real()*a.real()+a.imag()*a.imag());\n}\n\ninline void CC_intersection(int &ax,int &ay,int &bx,int &by,double &r1,double &r2,double &d,\n\t\t\t double &x1,double &y1,double &x2,double &y2){\n double l,m,n;\n double x,y;\n double p,q;\n l=(d*d-r2*r2+r1*r1)/(2*d);\n \n x=ax+(bx-ax)*l/d;\n y=ay+(by-ay)*l/d;\n n=sqrt(r1*r1-l*l);\n p=(by-ay)/d*n;\n q=(bx-ax)/d*n;\n \n x1=x+p;\n y1=y-q;\n\n x2=x-p;\n y2=y+q;\n}\n\nint is_intersected_circle(int &ax,int &ay,int &bx,int &by,\n\t\t\t double &r1,double& r2,double &d){\n d = compute_dist(ax-bx,ay-by);\n if (d<eps && fabs(r1-r2)<eps)return 3;\n if (d+r2 < r1)return 0;\n if (d+r1 < r2)return 1;\n if (d > r1+r2)return 4;\n return 2;\n}\n\ninline double cross(double ax,double ay,double bx,double by){\n return ax*by - ay*bx;\n}\n\ninline double dot(double ax,double ay,double bx,double by){\n return ax*bx+ay*by;}\n\ninline bool isp(int &ax,int & ay,int & bx,int & by,int & cx,int & cy){\n double tmp1=cross(bx-ax,by-ay,cx-ax,cy-ay);\n double tmp2=dot(bx-ax,by-ay,cx-ax,cy-ay);\n return tmp1<eps&&tmp1>-eps&&tmp2<eps&&tmp2>-eps;\n}\n\ninline bool is_intersected_ls(int &a1x,int &a1y,int &a2x,int &a2y,int &b1x,int &b1y,int &b2x,int &b2y){\n if ( ( cross(a2x-a1x,a2y-a1y,b1x-a1x,b1y-a1y)*cross(a2x-a1x,a2y-a1y,b2x-a1x,b2y-a1y)<-eps) && \n\t( cross(b2x-b1x,b2y-b1y,a1x-b1x,a1y-b1y)*cross(b2x-b1x,b2y-b1y,a2x-b1x,a2y-b1y)<-eps))\n return true;\n return false;\n}\n\ninline bool is_in(int *inx,int *iny,int &ax,int& ay){\n int cnt =0;\n int n = 3;\n rep(i,n){\n int curx=inx[i],cury=iny[i];\n int nextx=inx[(i+1)%n],nexty=iny[(i+1)%n];\n curx-=ax;\n cury-=ay;\n nextx-=ax;\n nexty-=ay;\n if (cury > nexty)swap(curx,nextx),swap(cury,nexty);\n if (cury<0 && 0<=nexty &&\n\tcross(nextx,nexty,curx,cury)>=0)cnt++;\n if (isp(inx[i],iny[i],inx[(i+1)%n],iny[(i+1)%n],ax,ay))return true;\n }\n if (cnt %2 == 1)return true;\n else return false;\n}\n\ninline double distance_l_p(int &ax,int &ay,int &bx,int &by,int &cx,int &cy){\n return fabs(cross(bx-ax,by-ay,cx-ax,cy-ay))/compute_dist(bx-ax,by-ay);\n}\n\ninline double solve_linear(double A1,double B1,double C1,\n\t\t double A2,double B2,double C2,\n\t\t\t double x2,double y2,double d2\n\t\t ){\n if (fabs(A1*B2-A2*B1)<1e-8|| fabs(A1)<eps){\n return -1;\n }\n double y = (A1*C2-A2*C1)/(A1*B2-A2*B1);\n double x = (C1-B1*y)/A1;\n double z = d2*d2 - (x-x2)*(x-x2) - (y-y2)*(y-y2);\n if (z < 1e-4)return -1;\n\n return sqrt(z);\n if (isnan(z)||z<0)return -1;\n else return sqrt(z);\n}\n\ndouble ans;\ninline void compute_height(Tri in[4]){\n double r[3];\n REP(i,1,4){\n r[i-1] = compute_dist(in[i].x[2]-in[i].x[0],\n\t\t\t in[i].y[2]-in[i].y[0]);\n }\n double tmp = solve_linear(2*(in[2].x[0]-in[1].x[0]),\n\t\t\t 2*(in[2].y[0]-in[1].y[0]),\n\t\t\t r[0]*r[0]-r[1]*r[1]\n\t\t\t -(in[1].x[0]*in[1].x[0]-in[2].x[0]*in[2].x[0])\n\t\t\t -(in[1].y[0]*in[1].y[0]-in[2].y[0]*in[2].y[0]),\n\t\t\t 2*(in[2].x[0]-in[3].x[0]),\n\t\t\t 2*(in[2].y[0]-in[3].y[0]),\n\t\t\t r[2]*r[2]-r[1]*r[1]\n\t\t\t -(in[3].x[0]*in[3].x[0]-in[2].x[0]*in[2].x[0])\n\t\t\t -(in[3].y[0]*in[3].y[0]-in[2].y[0]*in[2].y[0]),\n\t\t\t in[2].x[0],in[2].y[0],r[1]\n\t\t\t );\n if (tmp < 0.00001||isnan(tmp));\n else {\n ans = min(ans,tmp);\n return;\n }\n return;\n}\n\ninline bool isvalid(Tri in[4],int now){\n rep(j,3){\n if ((int)(in[now].x[j])<-100||(int)(in[now].x[j])>100 ||\n\t(int)(in[now].y[j])<-100||(int)(in[now].y[j])>100){\n return false;\n }\n }\n\n rep(j,now){\n rep(k,3){\n if (is_intersected_ls(in[now].x[0],in[now].y[0],in[now].x[2] ,in[now].y[2],\n\t\t\t in[j].x[k] ,in[j].y[k] ,in[j].x[(k+1)%3],in[j].y[(k+1)%3])){\n\treturn false;\n }\n if (is_intersected_ls(in[now].x[1],in[now].y[1],in[now].x[2] ,in[now].y[2],\n\t\t\t in[j].x[k] ,in[j].y[k] ,in[j].x[(k+1)%3],in[j].y[(k+1)%3])){\n\treturn false;\n }\n if (isp(in[j].x[k],in[j].y[k],in[j].x[(k+1)%3],in[j].y[(k+1)%3],in[now].x[2] ,in[now].y[2])){\n\treturn false;\n }\n }\n \n if (is_in(in[j].x,in[j].y,in[now].x[2],in[now].y[2])){\n return false;\n }\n\n if (j != 0 && fabs(in[now].x[2]-in[j].x[2])<1e-8&&fabs(in[now].y[2]-in[j].y[2])<1e-8){\n return false;\n } \n }\n return true;\n}\n\ninline void decide3(Tri in[4],double &dist2){\n double dist1=compute_dist(in[1].x[0]-in[1].x[2],in[1].y[0]-in[1].y[2]);\n P c1(in[3].x[1],in[3].y[1]);\n P c2(in[3].x[0],in[3].y[0]);\n\n double tx1,tx2,ty1,ty2,d;\n int x1,x2,y1,y2;\n if (is_intersected_circle(c1.real(),c1.imag(),c2.real(),c2.imag(),dist1,dist2,d) == 2){\n CC_intersection(c1.real(),c1.imag(),c2.real(),c2.imag(),dist1,dist2,d,tx1,ty1,tx2,ty2);\n\n if (isint(tx1)&&isint(ty1)){\n x1=double_to_int(tx1);\n y1=double_to_int(ty1);\n in[3].x[2]=x1;\n in[3].y[2]=y1;\n if (isvalid(in,3))compute_height(in);\n }\n\t\n if (isint(tx2)&&isint(ty2)){\n x2=double_to_int(tx2);\n y2=double_to_int(ty2);\n in[3].x[2]=x2;\n in[3].y[2]=y2;\n if (isvalid(in,3))compute_height(in);\n }\n }\n}\n\ninline void decide2(Tri in[4]){\n double dist=compute_dist(in[1].x[1]-in[1].x[2],in[1].y[1]-in[1].y[2]);\n int lim = (int)(dist+0.1);\n for(int x=0;x<=lim;x++){\n double ty = sqrt(dist*dist-x*x+eps);\n if ( !isint(ty))continue;\n int y = double_to_int(ty);\n in[2].x[2]=in[2].x[0]+x;\n in[2].y[2]=in[2].y[0]+y;\n double dist2=compute_dist(in[2].x[2]-in[2].x[1],in[2].y[2]-in[2].y[1]);\n if (isvalid(in,2))decide3(in,dist2);\n in[2].y[2]=in[2].y[0]-y;\n dist2=compute_dist(in[2].x[2]-in[2].x[1],in[2].y[2]-in[2].y[1]);\n if (isvalid(in,2))decide3(in,dist2);\n \n in[2].x[2]=in[2].x[0]-x;\n in[2].y[2]=in[2].y[0]+y;\n dist2=compute_dist(in[2].x[2]-in[2].x[1],in[2].y[2]-in[2].y[1]);\n if (isvalid(in,2))decide3(in,dist2);\n \n in[2].y[2]=in[2].y[0]-y;\n dist2=compute_dist(in[2].x[2]-in[2].x[1],in[2].y[2]-in[2].y[1]);\n if (isvalid(in,2))decide3(in,dist2);\n \n }\n}\n\ninline void decide1(Tri in[4]){\n REP(y,-100,101){\n REP(x,-100,101){\n in[1].x[2]=x;\n in[1].y[2]=y;\n if (isvalid(in,1))decide2(in);\n }\n }\n}\n\nmain(){\n Tri in[4];\n \n while(cin>>in[0].x[0]>>in[0].y[0]\n\t>>in[0].x[1]>>in[0].y[1]\n\t>>in[0].x[2]>>in[0].y[2]){\n\n if (in[0].x[0] == 0 && in[0].y[0] == 0 &&\n\tin[0].x[1] == 0 && in[0].y[1] == 0 &&\n\tin[0].x[2] == 0 && in[0].y[2] == 0)break;\n \n in[1].x[0]=in[0].x[0]; in[1].y[0]=in[0].y[0];\n in[1].x[1]=in[0].x[1]; in[1].y[1]=in[0].y[1];\n\n in[2].x[0]=in[0].x[1]; in[2].y[0]=in[0].y[1];\n in[2].x[1]=in[0].x[2]; in[2].y[1]=in[0].y[2];\n \n in[3].x[0]=in[0].x[2]; in[3].y[0]=in[0].y[2];\n in[3].x[1]=in[0].x[0]; in[3].y[1]=in[0].y[0];\n\n ans = 1e100;\n decide1(in);\n if (ans >1e99)printf(\"-1\\n\");\n else printf(\"%.7lf\\n\",ans);\n }\n}", "accuracy": 1, "time_ms": 7980, "memory_kb": 960, "score_of_the_acc": -0.999, "final_rank": 15 }, { "submission_id": "aoj_1278_101395", "code_snippet": "#include<iostream>\n#include<complex>\n#include<cmath>\nusing namespace std;\n#define REP(i,b,n) for(int i=b;i<n;i++)\n#define rep(i,n) REP(i,0,n)\n#define mp make_pair\n\nconst double eps = 1e-10;\n\ntypedef complex<int> P;\ntypedef complex<double> DP;\n\nclass Tri{\npublic:\n int x[3],y[3];\n};\n\ninline double compute_dist(double x,double y){\n return sqrt(x*x+y*y);\n}\n\nint double_to_int(double &a){\n if (a <0)return (int)(a-0.5);\n else return (int)(a+0.5);\n}\n\nbool isint(double a){\n if (a <0){\n if ( fabs((int)(a-0.5)-a)<eps)return true;\n }else {\n if ( fabs((int)(a+0.5)-a)<eps)return true;\n }\n return false;\n}\n\ndouble myabs(P a){\n return sqrt(a.real()*a.real()+a.imag()*a.imag());\n}\n\ninline void CC_intersection(int &ax,int &ay,int &bx,int &by,double &r1,double &r2,double &d,\n\t\t\t double &x1,double &y1,double &x2,double &y2){\n double l,m,n;\n double x,y;\n double p,q;\n l=(d*d-r2*r2+r1*r1)/(2*d);\n \n x=ax+(bx-ax)*l/d;\n y=ay+(by-ay)*l/d;\n n=sqrt(r1*r1-l*l);\n p=(by-ay)/d*n;\n q=(bx-ax)/d*n;\n \n x1=x+p;\n y1=y-q;\n\n x2=x-p;\n y2=y+q;\n}\n\nint is_intersected_circle(int &ax,int &ay,int &bx,int &by,\n\t\t\t double &r1,double& r2,double &d){\n d = compute_dist(ax-bx,ay-by);\n if (d<eps && fabs(r1-r2)<eps)return 3;\n if (d+r2 < r1)return 0;\n if (d+r1 < r2)return 1;\n if (d > r1+r2)return 4;\n return 2;\n}\n\ninline double cross(double ax,double ay,double bx,double by){\n return ax*by - ay*bx;\n}\n\ninline double dot(double ax,double ay,double bx,double by){\n return ax*bx+ay*by;}\n\ninline bool isp(int &ax,int & ay,int & bx,int & by,int & cx,int & cy){\n double tmp1=cross(bx-ax,by-ay,cx-ax,cy-ay);\n double tmp2=dot(bx-ax,by-ay,cx-ax,cy-ay);\n return tmp1<eps&&tmp1>-eps&&tmp2<eps&&tmp2>-eps;\n}\n\ninline bool is_intersected_ls(int &a1x,int &a1y,int &a2x,int &a2y,int &b1x,int &b1y,int &b2x,int &b2y){\n if ( ( cross(a2x-a1x,a2y-a1y,b1x-a1x,b1y-a1y)*cross(a2x-a1x,a2y-a1y,b2x-a1x,b2y-a1y)<-eps) && \n\t( cross(b2x-b1x,b2y-b1y,a1x-b1x,a1y-b1y)*cross(b2x-b1x,b2y-b1y,a2x-b1x,a2y-b1y)<-eps))\n return true;\n return false;\n}\n\ninline bool is_in(int *inx,int *iny,int &ax,int& ay){\n int cnt =0;\n int n = 3;\n rep(i,n){\n int curx=inx[i],cury=iny[i];\n int nextx=inx[(i+1)%n],nexty=iny[(i+1)%n];\n curx-=ax;\n cury-=ay;\n nextx-=ax;\n nexty-=ay;\n if (cury > nexty)swap(curx,nextx),swap(cury,nexty);\n if (cury<0 && 0<=nexty &&\n\tcross(nextx,nexty,curx,cury)>=0)cnt++;\n if (isp(inx[i],iny[i],inx[(i+1)%n],iny[(i+1)%n],ax,ay))return true;\n }\n if (cnt %2 == 1)return true;\n else return false;\n}\n\ninline double distance_l_p(int &ax,int &ay,int &bx,int &by,int &cx,int &cy){\n return fabs(cross(bx-ax,by-ay,cx-ax,cy-ay))/compute_dist(bx-ax,by-ay);\n}\n\ninline double solve_linear(double A1,double B1,double C1,\n\t\t double A2,double B2,double C2,\n\t\t\t double x2,double y2,double d2\n\t\t ){\n if (fabs(A1*B2-A2*B1)<1e-8|| fabs(A1)<eps){\n return -1;\n }\n double y = (A1*C2-A2*C1)/(A1*B2-A2*B1);\n double x = (C1-B1*y)/A1;\n double z = d2*d2 - (x-x2)*(x-x2) - (y-y2)*(y-y2);\n if (z < 1e-4)return -1;\n\n return sqrt(z);\n if (isnan(z)||z<0)return -1;\n else return sqrt(z);\n}\n\ndouble ans;\ninline void compute_height(Tri in[4]){\n DP data[3];\n double r[3];\n REP(i,1,4){\n data[i-1].real()=in[i].x[0];\n data[i-1].imag()=in[i].y[0];\n r[i-1] = compute_dist(in[i].x[2]-in[i].x[0],\n\t\t\t in[i].y[2]-in[i].y[0]);\n }\n double tmp = solve_linear(2*(data[1].real()-data[0].real()),\n\t\t\t 2*(data[1].imag()-data[0].imag()),\n\t\t\t r[0]*r[0]-r[1]*r[1]\n\t\t\t -(data[0].real()*data[0].real()-data[1].real()*data[1].real())\n\t\t\t -(data[0].imag()*data[0].imag()-data[1].imag()*data[1].imag()),\n\t\t\t 2*(data[1].real()-data[2].real()),\n\t\t\t 2*(data[1].imag()-data[2].imag()),\n\t\t\t r[2]*r[2]-r[1]*r[1]\n\t\t\t -(data[2].real()*data[2].real()-data[1].real()*data[1].real())\n\t\t\t -(data[2].imag()*data[2].imag()-data[1].imag()*data[1].imag()),\n\t\t\t data[1].real(),data[1].imag(),r[1]\n\t\t\t );\n if (tmp < 0.00001||isnan(tmp));\n else {\n ans = min(ans,tmp);\n return;\n }\n return;\n}\n\ninline bool isvalid(Tri in[4],int now){\n rep(j,3){\n if ((int)(in[now].x[j])<-100||(int)(in[now].x[j])>100 ||\n\t(int)(in[now].y[j])<-100||(int)(in[now].y[j])>100){\n return false;\n }\n }\n\n rep(j,now){\n rep(k,3){\n if (is_intersected_ls(in[now].x[0],in[now].y[0],in[now].x[2] ,in[now].y[2],\n\t\t\t in[j].x[k] ,in[j].y[k] ,in[j].x[(k+1)%3],in[j].y[(k+1)%3])){\n\treturn false;\n }\n if (is_intersected_ls(in[now].x[1],in[now].y[1],in[now].x[2] ,in[now].y[2],\n\t\t\t in[j].x[k] ,in[j].y[k] ,in[j].x[(k+1)%3],in[j].y[(k+1)%3])){\n\treturn false;\n }\n if (isp(in[j].x[k],in[j].y[k],in[j].x[(k+1)%3],in[j].y[(k+1)%3],in[now].x[2] ,in[now].y[2])){\n\treturn false;\n }\n }\n \n if (is_in(in[j].x,in[j].y,in[now].x[2],in[now].y[2])){\n return false;\n }\n\n if (j != 0 && fabs(in[now].x[2]-in[j].x[2])<1e-8&&fabs(in[now].y[2]-in[j].y[2])<1e-8){\n return false;\n } \n }\n return true;\n}\n\ninline void decide3(Tri in[4],double &dist2){\n double dist1=compute_dist(in[1].x[0]-in[1].x[2],in[1].y[0]-in[1].y[2]);\n P c1(in[3].x[1],in[3].y[1]);\n P c2(in[3].x[0],in[3].y[0]);\n\n double tx1,tx2,ty1,ty2,d;\n int x1,x2,y1,y2;\n if (is_intersected_circle(c1.real(),c1.imag(),c2.real(),c2.imag(),dist1,dist2,d) == 2){\n CC_intersection(c1.real(),c1.imag(),c2.real(),c2.imag(),dist1,dist2,d,tx1,ty1,tx2,ty2);\n\n if (isint(tx1)&&isint(ty1)){\n x1=double_to_int(tx1);\n y1=double_to_int(ty1);\n in[3].x[2]=x1;\n in[3].y[2]=y1;\n if (isvalid(in,3))compute_height(in);\n }\n\t\n if (isint(tx2)&&isint(ty2)){\n x2=double_to_int(tx2);\n y2=double_to_int(ty2);\n in[3].x[2]=x2;\n in[3].y[2]=y2;\n if (isvalid(in,3))compute_height(in);\n }\n }\n}\n\ninline void decide2(Tri in[4]){\n double dist=compute_dist(in[1].x[1]-in[1].x[2],in[1].y[1]-in[1].y[2]);\n int lim = (int)(dist+0.1);\n for(int x=0;x<=lim;x++){\n double ty = sqrt(dist*dist-x*x+eps);\n if ( !isint(ty))continue;\n int y = double_to_int(ty);\n in[2].x[2]=in[2].x[0]+x;\n in[2].y[2]=in[2].y[0]+y;\n double dist2=compute_dist(in[2].x[2]-in[2].x[1],in[2].y[2]-in[2].y[1]);\n if (isvalid(in,2))decide3(in,dist2);\n in[2].y[2]=in[2].y[0]-y;\n dist2=compute_dist(in[2].x[2]-in[2].x[1],in[2].y[2]-in[2].y[1]);\n if (isvalid(in,2))decide3(in,dist2);\n \n in[2].x[2]=in[2].x[0]-x;\n in[2].y[2]=in[2].y[0]+y;\n dist2=compute_dist(in[2].x[2]-in[2].x[1],in[2].y[2]-in[2].y[1]);\n if (isvalid(in,2))decide3(in,dist2);\n \n in[2].y[2]=in[2].y[0]-y;\n dist2=compute_dist(in[2].x[2]-in[2].x[1],in[2].y[2]-in[2].y[1]);\n if (isvalid(in,2))decide3(in,dist2);\n \n }\n}\n\ninline void decide1(Tri in[4]){\n REP(y,-100,101){\n REP(x,-100,101){\n in[1].x[2]=x;\n in[1].y[2]=y;\n if (isvalid(in,1))decide2(in);\n }\n }\n}\n\nmain(){\n Tri in[4];\n \n while(cin>>in[0].x[0]>>in[0].y[0]\n\t>>in[0].x[1]>>in[0].y[1]\n\t>>in[0].x[2]>>in[0].y[2]){\n\n if (in[0].x[0] == 0 && in[0].y[0] == 0 &&\n\tin[0].x[1] == 0 && in[0].y[1] == 0 &&\n\tin[0].x[2] == 0 && in[0].y[2] == 0)break;\n \n in[1].x[0]=in[0].x[0]; in[1].y[0]=in[0].y[0];\n in[1].x[1]=in[0].x[1]; in[1].y[1]=in[0].y[1];\n\n in[2].x[0]=in[0].x[1]; in[2].y[0]=in[0].y[1];\n in[2].x[1]=in[0].x[2]; in[2].y[1]=in[0].y[2];\n \n in[3].x[0]=in[0].x[2]; in[3].y[0]=in[0].y[2];\n in[3].x[1]=in[0].x[0]; in[3].y[1]=in[0].y[0];\n\n ans = 1e100;\n decide1(in);\n if (ans >1e99)printf(\"-1\\n\");\n else printf(\"%.7lf\\n\",ans);\n }\n}", "accuracy": 1, "time_ms": 7990, "memory_kb": 956, "score_of_the_acc": -1, "final_rank": 17 }, { "submission_id": "aoj_1278_101302", "code_snippet": "#include<iostream>\n#include<complex>\n#include<cmath>\nusing namespace std;\n#define REP(i,b,n) for(int i=b;i<n;i++)\n#define rep(i,n) REP(i,0,n)\n#define mp make_pair\n\nconst double eps = 1e-10;\n\ntypedef complex<int> P;\ntypedef complex<double> DP;\n\nclass Tri{\npublic:\n int x[3],y[3];\n};\n\ninline double compute_dist(double x,double y){\n return sqrt(x*x+y*y);\n}\n\nint double_to_int(double &a){\n if (a <0)return (int)(a-0.5);\n else return (int)(a+0.5);\n}\n\nbool isint(double a){\n if (a <0){\n if ( fabs((int)(a-0.5)-a)<eps)return true;\n }else {\n if ( fabs((int)(a+0.5)-a)<eps)return true;\n }\n return false;\n}\n\ndouble myabs(P a){\n return sqrt(a.real()*a.real()+a.imag()*a.imag());\n}\n\ninline void CC_intersection(int &ax,int &ay,int &bx,int &by,double &r1,double &r2,double &d,\n\t\t\t double &x1,double &y1,double &x2,double &y2){\n double l,m,n;\n double x,y;\n double p,q;\n l=(d*d-r2*r2+r1*r1)/(2*d);\n \n x=ax+(bx-ax)*l/d;\n y=ay+(by-ay)*l/d;\n n=sqrt(r1*r1-l*l);\n p=(by-ay)/d*n;\n q=(bx-ax)/d*n;\n \n x1=x+p;\n y1=y-q;\n\n x2=x-p;\n y2=y+q;\n}\n\nint is_intersected_circle(int &ax,int &ay,int &bx,int &by,\n\t\t\t double &r1,double& r2,double &d){\n d = compute_dist(ax-bx,ay-by);\n if (d<eps && fabs(r1-r2)<eps)return 3;\n if (d+r2 < r1)return 0;\n if (d+r1 < r2)return 1;\n if (d > r1+r2)return 4;\n return 2;\n}\n\ninline double cross(double ax,double ay,double bx,double by){\n return ax*by - ay*bx;\n}\n\ninline double dot(double ax,double ay,double bx,double by){\n return ax*bx+ay*by;}\n\ninline bool isp(int &ax,int & ay,int & bx,int & by,int & cx,int & cy){\n double tmp1=cross(bx-ax,by-ay,cx-ax,cy-ay);\n double tmp2=dot(bx-ax,by-ay,cx-ax,cy-ay);\n return tmp1<eps&&tmp1>-eps&&tmp2<eps&&tmp2>-eps;\n}\n\ninline bool is_intersected_ls(int &a1x,int &a1y,int &a2x,int &a2y,int &b1x,int &b1y,int &b2x,int &b2y){\n if ( ( cross(a2x-a1x,a2y-a1y,b1x-a1x,b1y-a1y)*cross(a2x-a1x,a2y-a1y,b2x-a1x,b2y-a1y)<-eps) && \n\t( cross(b2x-b1x,b2y-b1y,a1x-b1x,a1y-b1y)*cross(b2x-b1x,b2y-b1y,a2x-b1x,a2y-b1y)<-eps))\n return true;\n return false;\n}\n\ninline bool is_in(int *inx,int *iny,int &ax,int& ay){\n int cnt =0;\n int n = 3;\n rep(i,n){\n int curx=inx[i],cury=iny[i];\n int nextx=inx[(i+1)%n],nexty=iny[(i+1)%n];\n curx-=ax;\n cury-=ay;\n nextx-=ax;\n nexty-=ay;\n if (cury > nexty)swap(curx,nextx),swap(cury,nexty);\n if (cury<0 && 0<=nexty &&\n\tcross(nextx,nexty,curx,cury)>=0)cnt++;\n if (isp(inx[i],iny[i],inx[(i+1)%n],iny[(i+1)%n],ax,ay))return true;\n }\n if (cnt %2 == 1)return true;\n else return false;\n}\n\ninline double distance_l_p(int &ax,int &ay,int &bx,int &by,int &cx,int &cy){\n return fabs(cross(bx-ax,by-ay,cx-ax,cy-ay))/compute_dist(bx-ax,by-ay);\n}\n\ninline DP vector_mapping(int &ax,int &ay,int& bx,int& by,int &cx,int &cy){\n double tmp = distance_l_p(ax,ay,bx,by,cx,cy);\n double tmp2= compute_dist(cx-ax,cy-ay);\n double C = sqrt(tmp2*tmp2-tmp*tmp+eps);\n if (dot(bx-ax,by-ay,cx-ax,cy-ay)<0)C*=-1;\n DP e(bx-ax,by-ay);\n DP A(ax,ay);\n e/=compute_dist(bx-ax,by-ay);\n e*=C;\n return A + e;\n}\n\ninline double solve_linear(double A1,double B1,double C1,\n\t\t double A2,double B2,double C2,\n\t\t\t double x2,double y2,double d2\n\t\t ){\n if (fabs(A1*B2-A2*B1)<1e-8|| fabs(A1)<eps){\n return -1;\n }\n double y = (A1*C2-A2*C1)/(A1*B2-A2*B1);\n double x = (C1-B1*y)/A1;\n double z = d2*d2 - (x-x2)*(x-x2) - (y-y2)*(y-y2);\n if (z < 1e-4)return -1;\n\n return sqrt(z);\n if (isnan(z)||z<0)return -1;\n else return sqrt(z);\n}\n\ndouble ans;\ninline void compute_height(Tri in[4]){\n DP data[3];\n double r[3];\n REP(i,1,4){\n data[i-1]=vector_mapping(in[i].x[0],in[i].y[0],in[i].x[1],in[i].y[1],in[i].x[2],in[i].y[2]);\n r[i-1] = distance_l_p(in[i].x[0],in[i].y[0],in[i].x[1],in[i].y[1],in[i].x[2],in[i].y[2]);\n }\n double tmp = solve_linear(2*(data[1].real()-data[0].real()),\n\t\t\t 2*(data[1].imag()-data[0].imag()),\n\t\t\t r[0]*r[0]-r[1]*r[1]\n\t\t\t -(data[0].real()*data[0].real()-data[1].real()*data[1].real())\n\t\t\t -(data[0].imag()*data[0].imag()-data[1].imag()*data[1].imag()),\n\t\t\t 2*(data[1].real()-data[2].real()),\n\t\t\t 2*(data[1].imag()-data[2].imag()),\n\t\t\t r[2]*r[2]-r[1]*r[1]\n\t\t\t -(data[2].real()*data[2].real()-data[1].real()*data[1].real())\n\t\t\t -(data[2].imag()*data[2].imag()-data[1].imag()*data[1].imag()),\n\t\t\t data[1].real(),data[1].imag(),r[1]\n\t\t\t );\n if (tmp < 0.00001||isnan(tmp));\n else {\n ans = min(ans,tmp);\n return;\n }\n return;\n}\n\ninline bool isvalid(Tri in[4],int now){\n rep(j,3){\n if ((int)(in[now].x[j])<-100||(int)(in[now].x[j])>100 ||\n\t(int)(in[now].y[j])<-100||(int)(in[now].y[j])>100){\n return false;\n }\n }\n\n rep(j,now){\n rep(k,3){\n if (is_intersected_ls(in[now].x[0],in[now].y[0],in[now].x[2] ,in[now].y[2],\n\t\t\t in[j].x[k] ,in[j].y[k] ,in[j].x[(k+1)%3],in[j].y[(k+1)%3])){\n\treturn false;\n }\n if (is_intersected_ls(in[now].x[1],in[now].y[1],in[now].x[2] ,in[now].y[2],\n\t\t\t in[j].x[k] ,in[j].y[k] ,in[j].x[(k+1)%3],in[j].y[(k+1)%3])){\n\treturn false;\n }\n if (isp(in[j].x[k],in[j].y[k],in[j].x[(k+1)%3],in[j].y[(k+1)%3],in[now].x[2] ,in[now].y[2])){\n\treturn false;\n }\n }\n \n if (is_in(in[j].x,in[j].y,in[now].x[2],in[now].y[2])){\n return false;\n }\n\n if (j != 0 && fabs(in[now].x[2]-in[j].x[2])<1e-8&&fabs(in[now].y[2]-in[j].y[2])<1e-8){\n return false;\n } \n }\n return true;\n}\n\ninline void decide3(Tri in[4],double &dist2){\n double dist1=compute_dist(in[1].x[0]-in[1].x[2],in[1].y[0]-in[1].y[2]);\n P c1(in[3].x[1],in[3].y[1]);\n P c2(in[3].x[0],in[3].y[0]);\n\n double tx1,tx2,ty1,ty2,d;\n int x1,x2,y1,y2;\n if (is_intersected_circle(c1.real(),c1.imag(),c2.real(),c2.imag(),dist1,dist2,d) == 2){\n CC_intersection(c1.real(),c1.imag(),c2.real(),c2.imag(),dist1,dist2,d,tx1,ty1,tx2,ty2);\n\n if (isint(tx1)&&isint(ty1)){\n x1=double_to_int(tx1);\n y1=double_to_int(ty1);\n in[3].x[2]=x1;\n in[3].y[2]=y1;\n if (isvalid(in,3))compute_height(in);\n }\n\t\n if (isint(tx2)&&isint(ty2)){\n x2=double_to_int(tx2);\n y2=double_to_int(ty2);\n in[3].x[2]=x2;\n in[3].y[2]=y2;\n if (isvalid(in,3))compute_height(in);\n }\n }\n}\n\ninline void decide2(Tri in[4]){\n double dist=compute_dist(in[1].x[1]-in[1].x[2],in[1].y[1]-in[1].y[2]);\n int lim = (int)(dist+0.1);\n for(int x=0;x<=lim;x++){\n double ty = sqrt(dist*dist-x*x+eps);\n if ( !isint(ty))continue;\n int y = double_to_int(ty);\n in[2].x[2]=in[2].x[0]+x;\n in[2].y[2]=in[2].y[0]+y;\n double dist2=compute_dist(in[2].x[2]-in[2].x[1],in[2].y[2]-in[2].y[1]);\n if (isvalid(in,2))decide3(in,dist2);\n in[2].y[2]=in[2].y[0]-y;\n dist2=compute_dist(in[2].x[2]-in[2].x[1],in[2].y[2]-in[2].y[1]);\n if (isvalid(in,2))decide3(in,dist2);\n \n in[2].x[2]=in[2].x[0]-x;\n in[2].y[2]=in[2].y[0]+y;\n dist2=compute_dist(in[2].x[2]-in[2].x[1],in[2].y[2]-in[2].y[1]);\n if (isvalid(in,2))decide3(in,dist2);\n \n in[2].y[2]=in[2].y[0]-y;\n dist2=compute_dist(in[2].x[2]-in[2].x[1],in[2].y[2]-in[2].y[1]);\n if (isvalid(in,2))decide3(in,dist2);\n \n }\n}\n\ninline void decide1(Tri in[4]){\n REP(y,-100,101){\n REP(x,-100,101){\n in[1].x[2]=x;\n in[1].y[2]=y;\n if (isvalid(in,1))decide2(in);\n }\n }\n}\n\nmain(){\n Tri in[4];\n \n while(cin>>in[0].x[0]>>in[0].y[0]\n\t>>in[0].x[1]>>in[0].y[1]\n\t>>in[0].x[2]>>in[0].y[2]){\n\n if (in[0].x[0] == 0 && in[0].y[0] == 0 &&\n\tin[0].x[1] == 0 && in[0].y[1] == 0 &&\n\tin[0].x[2] == 0 && in[0].y[2] == 0)break;\n \n in[1].x[0]=in[0].x[0]; in[1].y[0]=in[0].y[0];\n in[1].x[1]=in[0].x[1]; in[1].y[1]=in[0].y[1];\n\n in[2].x[0]=in[0].x[1]; in[2].y[0]=in[0].y[1];\n in[2].x[1]=in[0].x[2]; in[2].y[1]=in[0].y[2];\n \n in[3].x[0]=in[0].x[2]; in[3].y[0]=in[0].y[2];\n in[3].x[1]=in[0].x[0]; in[3].y[1]=in[0].y[0];\n\n ans = 1e100;\n decide1(in);\n if (ans >1e99)printf(\"-1\\n\");\n else printf(\"%.7lf\\n\",ans);\n }\n}", "accuracy": 1, "time_ms": 7980, "memory_kb": 960, "score_of_the_acc": -0.999, "final_rank": 15 } ]
aoj_1283_cpp
Problem I: Most Distant Point from the Sea The main land of Japan called Honshu is an island surrounded by the sea. In such an island, it is natural to ask a question: "Where is the most distant point from the sea?" The answer to this question for Honshu was found in 1996. The most distant point is located in former Usuda Town, Nagano Prefecture, whose distance from the sea is 114.86 km. In this problem, you are asked to write a program which, given a map of an island, finds the most distant point from the sea in the island, and reports its distance from the sea. In order to simplify the problem, we only consider maps representable by convex polygons. Input The input consists of multiple datasets. Each dataset represents a map of an island, which is a convex polygon. The format of a dataset is as follows. n x 1 y 1 . . . x n y n Every input item in a dataset is a non-negative integer. Two input items in a line are separated by a space. n in the first line is the number of vertices of the polygon, satisfying 3 ≤ n ≤ 100. Subsequent n lines are the x - and y -coordinates of the n vertices. Line segments ( x i , y i ) - ( x i +1 , y i +1 ) (1 ≤ i ≤ n - 1) and the line segment ( x n , y n ) - ( x 1 , y 1 ) form the border of the polygon in counterclockwise order. That is, these line segments see the inside of the polygon in the left of their directions. All coordinate values are between 0 and 10000, inclusive. You can assume that the polygon is simple, that is, its border never crosses or touches itself. As stated above, the given polygon is always a convex one. The last dataset is followed by a line containing a single zero. Output For each dataset in the input, one line containing the distance of the most distant point from the sea should be output. An output line should not contain extra characters such as spaces. The answer should not have an error greater than 0.00001 (10 -5 ). You may output any number of digits after the decimal point, provided that the above accuracy condition is satisfied. Sample Input 4 0 0 10000 0 10000 10000 0 10000 3 0 0 10000 0 7000 1000 6 0 40 100 20 250 40 250 70 100 90 0 70 3 0 0 10000 10000 5000 5001 0 Output for the Sample Input 5000.000000 494.233641 34.542948 0.353553
[ { "submission_id": "aoj_1283_8998626", "code_snippet": "#line 2 \"cp-library/src/cp-template.hpp\"\n#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\nusing ld = long double;\nusing uint = unsigned int;\nusing ull = unsigned long long;\nusing i32 = int;\nusing u32 = unsigned int;\nusing i64 = long long;\nusing u64 = unsigned long long;\nusing i128 = __int128_t;\ntemplate < class T > bool chmin(T& a, T b) { if(a > b) { a = b; return true; } return false; }\ntemplate < class T > bool chmax(T& a, T b) { if(a < b) { a = b; return true; } return false; }\ntemplate < class T, class U > T ceil (T x, U y) { return (x > 0 ? (x + y - 1) / y : x / y); }\ntemplate < class T, class U > T floor(T x, U y) { return (x > 0 ? x / y : (x - y + 1) / y); }\nint popcnt(i32 x) { return __builtin_popcount(x); }\nint popcnt(u32 x) { return __builtin_popcount(x); }\nint popcnt(i64 x) { return __builtin_popcountll(x); }\nint popcnt(u64 x) { return __builtin_popcountll(x); }\n\n#line 2 \"cp-library/src/utility/rep_itr.hpp\"\ntemplate < class T > struct itr_rep {\n T i, d;\n constexpr itr_rep(const T i) noexcept : i(i), d(1) {}\n constexpr itr_rep(const T i, const T d) noexcept : i(i), d(d) {}\n void operator++() noexcept { i += d; }\n constexpr int operator*() const noexcept { return i; }\n constexpr bool operator!=(const itr_rep x) const noexcept { return d > 0 ? i < x.i : i > x.i; }\n};\n\ntemplate < class T > struct rep {\n const itr_rep< T > s, t;\n constexpr rep(const T t) noexcept : s(0), t(t) {}\n constexpr rep(const T s, const T t) noexcept : s(s), t(t) {}\n constexpr rep(const T s, const T t, const T d) noexcept : s(s, d), t(t, d) {}\n constexpr auto begin() const noexcept { return s; }\n constexpr auto end () const noexcept { return t; }\n};\n\ntemplate < class T > struct revrep {\n const itr_rep < T > s, t;\n constexpr revrep(const T t) noexcept : s(t - 1, -1), t(-1, -1) {}\n constexpr revrep(const T s, const T t) noexcept : s(t - 1, -1), t(s - 1, -1) {}\n constexpr revrep(const T s, const T t, const T d) noexcept : s(t - 1, -d), t(s - 1, -d) {}\n constexpr auto begin() const noexcept { return s; }\n constexpr auto end () const noexcept { return t; }\n};\n#line 3 \"cp-library/src/utility/io.hpp\"\n\n/* 128bit integer */\nistream& operator>>(istream& is, i128& x) {\n std::string s; is >> s;\n int pm = (s[0] == '-');\n x = 0;\n for(int i : rep(pm, int(s.size()))) x = x * 10 + (s[i] - '0');\n if(pm) x *= -1;\n return is;\n}\nostream& operator<<(ostream& os, const i128& x) {\n if(x == 0) return os << '0';\n i128 y = x;\n if(y < 0) { os << '-'; y *= -1; }\n std::vector<int> ny;\n while(y > 0) { ny.push_back(y % 10); y /= 10; }\n for(int i : revrep(ny.size())) os << ny[i];\n return os;\n}\n\ntemplate < class S, class T > istream& operator>>(istream& is, std::pair< S, T >& x) { is >> x.first >> x.second; return is; }\ntemplate < class S, class T > ostream& operator<<(ostream& os, const std::pair< S, T >& x) { os << x.first << \" \" << x.second; return os; }\n\nnamespace scanner {\n struct sca {\n template < class T > operator T() {\n T s; std::cin >> s; return s;\n }\n };\n struct vec {\n int n;\n vec(int n) : n(n) {}\n template < class T > operator std::vector< T >() {\n std::vector< T > v(n);\n for(T& x : v) std::cin >> x;\n return v;\n }\n };\n struct mat {\n int h, w;\n mat(int h, int w) : h(h), w(w) {}\n template < class T > operator std::vector< std::vector< T > >() {\n std::vector m(h, std::vector< T >(w));\n for(std::vector< T >& v : m) for(T& x : v) std::cin >> x;\n return m;\n }\n };\n struct speedup {\n speedup() {\n std::cin.tie(0);\n std::ios::sync_with_stdio(0);\n }\n } speedup_instance;\n}\nscanner::sca in() { return scanner::sca(); }\nscanner::vec in(int n) { return scanner::vec(n); }\nscanner::mat in(int h, int w) { return scanner::mat(h, w); }\n\nnamespace printer {\n void precision(int d) { std::cout << std::fixed << std::setprecision(d); }\n void flush() { std::cout.flush(); }\n}\n\ntemplate < class T >\nostream& operator<<(ostream& os, const std::vector< T > a) {\n int n = a.size();\n for(int i : rep(n)) { os << a[i]; if(i != n - 1) os << ' '; }\n return os;\n}\n\nint print() { std::cout << '\\n'; return 0; }\ntemplate < class head, class... tail > int print(head&& h, tail&&... t) {\n std::cout << h; if(sizeof...(tail)) std::cout << ' ';\n return print(std::forward<tail>(t)...);\n}\ntemplate < class T > int print_n(const std::vector< T > a) {\n int n = a.size();\n for(int i : rep(n)) std::cout << a[i] << \"\\n\";\n return 0;\n}\n\n\n#line 2 \"cp-library/src/utility/key_val.hpp\"\n\ntemplate < class K, class V >\nstruct key_val {\n K key; V val;\n key_val() {}\n key_val(K key, V val) : key(key), val(val) {}\n template < std::size_t Index >\n std::tuple_element_t< Index, key_val >& get() {\n if constexpr (Index == 0) return key;\n if constexpr (Index == 1) return val;\n }\n};\n\nnamespace std {\n\ntemplate < class K, class V > struct tuple_size < key_val< K, V > > : integral_constant< size_t, 2 > {};\ntemplate < class K, class V > struct tuple_element < 0, key_val< K, V > > { using type = K; };\ntemplate < class K, class V > struct tuple_element < 1, key_val< K, V > > { using type = V; };\n\n}\n#line 2 \"cp-library/src/utility/vec_op.hpp\"\ntemplate < class T > key_val< int, T > max_of(const vector< T >& a) {\n int i = std::max_element(a.begin(), a.end()) - a.begin();\n return {i, a[i]};\n}\ntemplate < class T > key_val< int, T > min_of(const vector< T >& a) {\n int i = std::min_element(a.begin(), a.end()) - a.begin();\n return {i, a[i]};\n}\ntemplate < class S, class T > S sum_of(const vector< T >& a) {\n S sum = 0;\n for(const T x : a) sum += x;\n return sum;\n}\ntemplate < class S, class T > vector< S > freq_of(const vector< T >& a, T L, T R) {\n vector< S > res(R - L, S(0));\n for(const T x : a) res[x - L] += 1;\n return res;\n}\ntemplate < class S, class T > struct prefix_sum {\n vector< S > s;\n prefix_sum(const vector< T >& a) : s(a) {\n s.insert(s.begin(), S(0));\n for(int i : rep(a.size())) s[i + 1] += s[i];\n }\n // [L, R)\n S sum(int L, int R) { return s[R] - s[L]; }\n};\n#line 3 \"cp-library/src/utility/heap.hpp\"\n\ntemplate < class T > using heap_min = std::priority_queue< T, std::vector< T >, std::greater< T > >;\ntemplate < class T > using heap_max = std::priority_queue< T, std::vector< T >, std::less< T > >;\n\n#line 27 \"cp-library/src/cp-template.hpp\"\n\n#line 1 \"cp-library/src/algorithm/bin_search.hpp\"\ntemplate < class T, class F >\nT bin_search(T ok, T ng, F f) {\n while(abs(ng - ok) > 1) {\n T mid = (ok + ng) / 2;\n (f(mid) ? ok : ng) = mid;\n }\n return ok;\n}\n\ntemplate < class T, class F >\nT bin_search_real(T ok, T ng, F f, int step = 80) {\n while(step--) {\n T mid = (ok + ng) / 2;\n (f(mid) ? ok : ng) = mid;\n }\n return ok;\n}\n#line 2 \"cp-library/src/algorithm/argsort.hpp\"\n\ntemplate < class T > std::vector< int > argsort(const std::vector< T > &a) {\n std::vector< int > ids((int)a.size());\n std::iota(ids.begin(), ids.end(), 0);\n std::sort(ids.begin(), ids.end(), [&](int i, int j) {\n return a[i] < a[j] || (a[i] == a[j] && i < j);\n });\n return ids;\n}\n#line 1 \"macro.hpp\"\nnamespace macro {\n\nusing size_type = int;\ntemplate < class container > void sort(container& a) { std::sort(std:: begin(a), std:: end(a)); }\ntemplate < class container > void rsort(container& a) { std::sort(std::rbegin(a), std::rend(a)); }\ntemplate < class container > void reverse(container& a) { std::reverse(std::begin(a), std::end(a)); }\ntemplate < class container > void unique(container& a) {\n std::sort(std::begin(a), std::end(a));\n a.erase(std::unique(std::begin(a), std::end(a)), std::end(a));\n}\ntemplate < class container > container sorted(const container& a) { container b = a; sort(b); return std::move(b); }\ntemplate < class container > container rsorted(const container& a) { container b = a; rsort(b); return std::move(b); }\ntemplate < class container, class compare > void sort(container& a, const compare& cmp) { std::sort(std::begin(a), std::end(a), cmp); }\ntemplate < class container, class compare > container sorted(const container& a, const compare& cmp) { container b = a; sort(b, cmp); return std::move(b); }\ntemplate < class container, class value > size_type lower_bound(const container& a, const value& x) { return std::lower_bound(std::begin(a), std::end(a), x) - std::begin(a); }\ntemplate < class container, class value > size_type upper_bound(const container& a, const value& x) { return std::upper_bound(std::begin(a), std::end(a), x) - std::begin(a); }\n\nconst std::vector<std::pair<size_type, size_type>> dir4 = { {+1, 0}, {-1, 0}, { 0, +1}, { 0, -1} };\nconst std::vector<std::pair<size_type, size_type>> dir8 = { {-1, -1}, {-1, 0}, {-1, +1}, { 0, -1}, { 0, +1}, {+1, -1}, {+1, 0}, {+1, +1} };\n\n#ifdef _DEBUG\n#define debug(x) std::cout << \"[\" << __LINE__ << \"] \" << #x << \": \" << x << std::endl\n#else\n#define debug(x)\n#endif\n\ntemplate < class container > void concat(container& a, const container& b) {\n a.insert(std::end(a), std::begin(b), std::end(b));\n}\nstd::vector<size_type> iota(const size_type n) {\n std::vector<size_type> I(n);\n std::iota(std::begin(I), std::end(I), 0);\n return I;\n}\ntemplate < class container > std::vector<size_type> sort_idx(const container& a) {\n const size_type n = a.size();\n std::vector<size_type> I = iota(n);\n std::sort(std::begin(I), std::end(I), [&](size_type i, size_type j) { return a[i] < a[j] or (a[i] == a[j] and i < j); });\n return I;\n}\ntemplate < class container, class compare > std::vector<size_type> sort_idx(const container& a, const compare& cmp) {\n const size_type n = a.size();\n std::vector<size_type> I = iota(n);\n std::sort(std::begin(I), std::end(I), [&](size_type i, size_type j) { return cmp(a[i], a[j]) or (a[i] == a[j] and i < j); });\n return std::move(I);\n}\n\nstruct grid {\n using size_type = int;\n size_type H, W;\n grid(const size_type H, const size_type W) : H(H), W(W) {}\n bool contains(const size_type i, const size_type j) {\n return 0 <= i and i < H and 0 <= j and j < W;\n }\n};\n\nusing f64 = long double;\n\n} // namespace macro\n\nusing namespace macro;\n#line 3 \"A.cpp\"\n\ntemplate < class T > struct point {\n T x, y;\n point() : x(0), y(0) {}\n point(T x, T y) : x(x), y(y) {}\n point(std::pair< T, T > p) : x(p.first), y(p.second) {}\n point& operator+=(const point& p) { x += p.x, y += p.y; return *this; }\n point& operator-=(const point& p) { x -= p.x, y -= p.y; return *this; }\n point& operator*=(const T r) { x *= r, y *= r; return *this; }\n point& operator/=(const T r) { x /= r, y /= r; return *this; }\n point operator+(const point& p) const { return point(*this) += p; }\n point operator-(const point& p) const { return point(*this) -= p; }\n point operator*(const T r) const { return point(*this) *= r; }\n point operator/(const T r) const { return point(*this) /= r; }\n point operator-() const { return {-x, -y}; }\n bool operator==(const point& p) const { return x == p.x and y == p.y; }\n bool operator!=(const point& p) const { return x != p.x or y != p.y; }\n bool operator<(const point& p) const { return x == p.x ? y < p.y : x < p.x; }\n point< T > rot(double theta) {\n static_assert(is_floating_point_v< T >);\n double cos_ = std::cos(theta), sin_ = std::sin(theta);\n return {cos_ * x - sin_ * y, sin_ * x + cos_ * y};\n }\n};\ntemplate < class T > istream& operator>>(istream& is, point< T >& p) { return is >> p.x >> p.y; }\ntemplate < class T > ostream& operator<<(ostream& os, point< T >& p) { return os << p.x << \" \" << p.y; }\ntemplate < class T > T dot(const point< T >& a, const point< T >& b) { return a.x * b.x + a.y * b.y; }\ntemplate < class T > T det(const point< T >& a, const point< T >& b) { return a.x * b.y - a.y * b.x; }\ntemplate < class T > T norm(const point< T >& p) { return p.x * p.x + p.y * p.y; }\ntemplate < class T > double abs(const point< T >& p) { return std::sqrt(norm(p)); }\ntemplate < class T > double angle(const point< T >& p) { return std::atan2(p.y, p.x); }\ntemplate < class T > int sign(const T x) {\n T e = (is_integral_v< T > ? 1 : 1e-8);\n if(x <= -e) return -1;\n if(x >= +e) return +1;\n return 0;\n}\ntemplate < class T > bool equals(const T& a, const T& b) { return sign(a - b) == 0; }\ntemplate < class T > int ccw(const point< T >& a, point< T > b, point< T > c) {\n b -= a, c -= a;\n if(sign(det(b, c)) == +1) return +1; // counter clockwise\n if(sign(det(b, c)) == -1) return -1; // clockwise\n if(sign(dot(b, c)) == -1) return +2; // c-a-b\n if(norm(b) < norm(c)) return -2; // a-b-c\n return 0; // a-c-b\n}\n\ntemplate < class T > struct line {\n point< T > a, b;\n line() {}\n line(point< T > a, point< T > b) : a(a), b(b) {}\n};\ntemplate < class T > point< T > projection(const line< T >& l, const point< T >& p) {\n static_assert(is_floating_point_v< T >);\n return l.a + (l.a - l.b) * dot(p - l.a, l.a - l.b) / norm(l.a - l.b);\n}\ntemplate < class T > point< T > reflection(const line< T >& l, const point< T >& p) {\n static_assert(is_floating_point_v< T >);\n return p + (projection(l, p) - p) * T(2);\n}\ntemplate < class T > bool orthogonal(const line< T >& a, const line< T >& b) { return equals(dot(a.b - a.a, b.b - b.a), T(0)); }\ntemplate < class T > bool parallel (const line< T >& a, const line< T >& b) { return equals(det(a.b - a.a, b.b - b.a), T(0)); }\ntemplate < class T > point< T > cross_point_ll(const line< T >& l, const line< T >& m) {\n static_assert(is_floating_point_v< T >);\n T A = det(l.b - l.a, m.b - m.a);\n T B = det(l.b - l.a, l.b - m.a);\n if(equals(abs(A), T(0)) and equals(abs(B), T(0))) return m.a;\n return m.a + (m.b - m.a) * B / A;\n}\n\ntemplate < class T > using segment = line< T >;\ntemplate < class T > bool intersect_ss(const segment< T >& s, const segment< T >& t) {\n return ccw(s.a, s.b, t.a) * ccw(s.a, s.b, t.b) <= 0 and ccw(t.a, t.b, s.a) * ccw(t.a, t.b, s.b) <= 0;\n}\ntemplate < class T > double distance_sp(const segment< T >& s, const point< T >& p) {\n static_assert(is_floating_point_v< T >);\n point r = projection(s, p);\n if(ccw(s.a, s.b, r) == 0) return abs(r - p);\n return std::min(abs(s.a - p), abs(s.b - p));\n}\ntemplate < class T > double distance_ss(const segment< T >& a, const segment< T >& b) {\n if(intersect_ss(a, b)) return 0;\n return std::min({ distance_sp(a, b.a), distance_sp(a, b.b), distance_sp(b, a.a), distance_sp(b, a.b) });\n}\n\ntemplate < class T > using polygon = std::vector< point< T > >;\ntemplate < class T > T area2(const polygon< T >& p) {\n T s = 0;\n int n = p.size();\n for(int i = 0; i < n; i++) s += det(p[i], p[(i + 1) % n]);\n return s;\n}\ntemplate < class T > T area(const polygon< T >& p) { return area2(p) / T(2); }\n\ntemplate < class T > bool is_convex(const polygon< T >& p) {\n int n = p.size();\n for(int i = 0; i < n; i++) if(ccw(p[(i - 1 + n) % n], p[i], p[(i + 1) % n]) == -1) return false;\n return true;\n}\ntemplate < class T > int contains(const polygon< T >& g, const point< T >& p) {\n int n = g.size();\n bool in = false;\n for(int i = 0; i < n; i++) {\n point a = g[i] - p, b = g[(i + 1) % n] - p;\n if(sign(a.y - b.y) == +1) std::swap(a, b);\n if(sign(a.y) <= 0 and sign(b.y) ==+1 and sign(det(a, b)) == -1) in = !in;\n if(sign(det(a, b)) == 0 and sign(dot(a, b)) <= 0) return 1; // ON\n }\n return in ? 2 : 0;\n}\ntemplate < class T > polygon< T > convex_cut(const polygon< T >& p, const line< T >& l) {\n int n = p.size();\n polygon< T > res;\n for(int i = 0; i < n; i++) {\n point now = p[i], nxt = p[(i + 1) % n];\n if(ccw(l.a, l.b, now) != -1) res.push_back(now);\n if(ccw(l.a, l.b, now) * ccw(l.a, l.b, nxt) < 0) res.push_back(cross_point_ll(line(now, nxt), l));\n }\n return res;\n}\ntemplate < class T > polygon< T > convex_hull(polygon< T >& p) {\n int n = p.size(), k = 0;\n if(n <= 2) return p;\n std::sort(p.begin(), p.end());\n polygon< T > ch(n + n);\n for(int i = 0; i < n; ch[k++] = p[i++])\n while(k >= 2 and sign(det(ch[k - 1] - ch[k - 2], p[i] - ch[k - 1])) == -1) k--;\n for(int i = n - 2, t = k + 1; i >= 0; ch[k++] = p[i--])\n while(k >= t and sign(det(ch[k - 1] - ch[k - 2], p[i] - ch[k - 1])) == -1) k--;\n ch.resize(k - 1);\n return ch;\n}\ntemplate < class T > T diameter2(const polygon< T >& p) {\n static_assert(is_floating_point_v< T >);\n int n = p.size(), is = 0, js = 0;\n for(int i = 1; i < n; i++) {\n if(sign(p[i].y - p[is].y) == +1) is = i;\n if(sign(p[i].y - p[js].y) == -1) js = i;\n }\n T dist_max = norm(p[is] - p[js]);\n int maxi = is, i = is, maxj = js, j = js;\n do {\n if(sign(det(p[(i + 1) % n] - p[i], p[(j + 1) % n] - p[j])) >= 0) j = (j + 1) % n; else i = (i + 1) % n;\n if(norm(p[i] - p[j]) > dist_max) {\n dist_max = norm(p[i] - p[j]);\n maxi = i, maxj = j;\n }\n } while(i != is or j != js);\n return dist_max;\n}\ntemplate < class T > double diameter(const polygon< T >& p) {\n static_assert(is_floating_point_v< T >);\n return std::sqrt(diameter2(p));\n}\n\ntemplate < class T > struct circle {\n point< T > p;\n T r;\n circle() = default;\n circle(point< T > p, T r) : p(p), r(r) {}\n};\ntemplate < class T > istream& operator>>(istream& is, circle< T >& c) { return is >> c.p >> c.r; }\ntemplate < class T > int intersect_cc(circle< T > c1, circle< T > c2) {\n if(c1.r < c2.r) std::swap(c1, c2);\n T d = abs(c1.p - c2.p);\n if(sign(c1.r + c2.r - d) == -1) return 4;\n if(equals(c1.r + c2.r, d)) return 3;\n if(sign(c1.r - c2.r - d) == -1) return 2;\n if(equals(c1.r - c2.r, d)) return 1;\n return 0;\n}\ntemplate < class T > std::pair<point< T >, point< T >> cross_point_cc(const circle< T >& c1, const circle< T >& c2) {\n T d = abs(c1.p - c2.p);\n T a = std::acos((c1.r * c1.r + d * d - c2.r * c2.r) / (2 * c1.r * d));\n T t = angle(c2.p - c1.p);\n point< T > p1 = c1.p + point< T >(std::cos(t + a), std::sin(t + a)) * c1.r;\n point< T > p2 = c1.p + point< T >(std::cos(t - a), std::sin(t - a)) * c1.r;\n return {p1, p2};\n}\n\nf64 solve(int n) {\n polygon<f64> p(n);\n for(int i : rep(n)) p[i] = in();\n\n return bin_search_real<f64>(0, 1e4, [&](f64 x) {\n polygon<f64> a = p;\n for(int i : rep(n)) {\n line<f64> l(p[i], p[(i + 1) % n]);\n\n point<f64> dp = p[(i + 1) % n] - p[i];\n point<f64> e(-dp.y, dp.x);\n e /= abs(e);\n l.a += e * x;\n l.b += e * x;\n\n a = convex_cut(a, l);\n }\n return a.size() >= 2;\n });\n}\n\nint main() {\n while(true) {\n int n = in();\n if(n == 0) return 0;\n printer::precision(20);\n print(solve(n));\n }\n}", "accuracy": 1, "time_ms": 20, "memory_kb": 3516, "score_of_the_acc": -0.68, "final_rank": 4 }, { "submission_id": "aoj_1283_7205812", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\n#define rep(i, s, t) for (int i = (int)(s); i < (int)(t); ++i)\n#define revrep(i, t, s) for (int i = (int)(t)-1; i >= (int)(s); --i)\n#define all(x) begin(x), end(x)\ntemplate <typename T> bool chmax(T& a, const T& b) { return a < b ? (a = b, 1) : 0; }\ntemplate <typename T> bool chmin(T& a, const T& b) { return a > b ? (a = b, 1) : 0; }\n\ntemplate <typename T>\nostream& operator<<(ostream& os, const vector<T>& v) {\n for (int i = 0; i < (int) v.size(); ++i) {\n os << v[i];\n if (i < (int) v.size() - 1) os << \" \";\n }\n return os;\n}\n\ntemplate <typename T, typename U>\nostream& operator<<(ostream& os, const pair<T, U>& p) {\n os << \"(\" << p.first << \", \" << p.second << \")\";\n return os;\n}\n\n\n\nusing T = double;\nusing Vec = std::complex<T>;\n\nconst T PI = std::acos(-1);\n\nconstexpr T eps = 1e-8;\ninline bool eq(T a, T b) { return std::abs(a - b) <= eps; }\ninline bool eq(Vec a, Vec b) { return std::abs(a - b) <= eps; }\ninline bool lt(T a, T b) { return a < b - eps; }\ninline bool leq(T a, T b) { return a <= b + eps; }\n\nstd::istream& operator>>(std::istream& is, Vec& p) {\n T x, y;\n is >> x >> y;\n p = {x, y};\n return is;\n}\n\nstruct Line {\n Vec p1, p2;\n Line() = default;\n Line(const Vec& p1, const Vec& p2) : p1(p1), p2(p2) {}\n Vec dir() const { return p2 - p1; }\n};\n\nstruct Segment {\n Vec p1, p2;\n Segment() = default;\n Segment(const Vec& p1, const Vec& p2) : p1(p1), p2(p2) {}\n Vec dir() const { return p2 - p1; }\n};\n\nstruct Circle {\n Vec c;\n T r;\n Circle() = default;\n Circle(const Vec& c, T r) : c(c), r(r) {}\n};\n\nusing Polygon = std::vector<Vec>;\n\nT dot(const Vec& a, const Vec& b) {\n return (std::conj(a) * b).real();\n}\n\nT cross(const Vec& a, const Vec& b) {\n return (std::conj(a) * b).imag();\n}\n\nVec rot(const Vec& a, T ang) {\n return a * Vec(std::cos(ang), std::sin(ang));\n}\n\nVec perp(const Vec& a) {\n return Vec(-a.imag(), a.real());\n}\n\nVec projection(const Line& l, const Vec& p) {\n return l.p1 + dot(p - l.p1, l.dir()) * l.dir() / std::norm(l.dir());\n}\n\nVec reflection(const Line& l, const Vec& p) {\n return T(2) * projection(l, p) - p;\n}\n\n// 0: collinear\n// 1: counter-clockwise\n// -1: clockwise\nint ccw(const Vec& a, const Vec& b, const Vec& c) {\n if (eq(cross(b - a, c - a), 0)) return 0;\n if (lt(cross(b - a, c - a), 0)) return -1;\n return 1;\n}\n\nvoid sort_by_arg(std::vector<Vec>& pts) {\n std::sort(pts.begin(), pts.end(), [&](auto& p, auto& q) {\n if ((p.imag() < 0) != (q.imag() < 0)) return (p.imag() < 0);\n if (cross(p, q) == 0) {\n if (p == Vec(0, 0)) return !(q.imag() < 0 || (q.imag() == 0 && q.real() > 0));\n if (q == Vec(0, 0)) return (p.imag() < 0 || (p.imag() == 0 && p.real() > 0));\n return (p.real() > q.real());\n }\n return (cross(p, q) > 0);\n });\n}\n\nstd::vector<Vec> halfplane_intersection(std::vector<std::pair<Vec, Vec>> hps) {\n using Hp = std::pair<Vec, Vec>; // (normal vector, a point on the border)\n\n auto intersection = [&](const Hp& l1, const Hp& l2) -> Vec {\n auto d = l2.second - l1.second;\n Vec e(-l1.first.imag(), l1.first.real());\n return l1.second + (dot(d, l2.first) / cross(l1.first, l2.first)) * e;\n };\n\n // check if the halfplane h contains the point p\n auto contains = [&](const Hp& h, const Vec& p) -> bool {\n return dot(p - h.second, h.first) > 0;\n };\n\n constexpr T INF = 1e15;\n hps.emplace_back(Vec(1, 0), Vec(-INF, 0)); // -INF <= x\n hps.emplace_back(Vec(-1, 0), Vec(INF, 0)); // x <= INF\n hps.emplace_back(Vec(0, 1), Vec(0, -INF)); // -INF <= y\n hps.emplace_back(Vec(0, -1), Vec(0, INF)); // y <= INF\n\n std::sort(hps.begin(), hps.end(), [&](const auto& h1, const auto& h2) {\n return std::arg(h1.first) < std::arg(h2.first);\n });\n\n std::deque<Hp> dq;\n int len = 0;\n for (auto& hp : hps) {\n while (len > 1 && !contains(hp, intersection(dq[len-1], dq[len-2]))) {\n dq.pop_back();\n --len;\n }\n\n while (len > 1 && !contains(hp, intersection(dq[0], dq[1]))) {\n dq.pop_front();\n --len;\n }\n\n // parallel\n if (len > 0 && eq(cross(dq[len-1].first, hp.first), 0)) {\n // opposite\n if (lt(dot(dq[len-1].first, hp.first), 0)) {\n return {};\n }\n // same\n if (!contains(hp, dq[len-1].second)) {\n dq.pop_back();\n --len;\n } else continue;\n }\n\n dq.push_back(hp);\n ++len;\n }\n\n while (len > 2 && !contains(dq[0], intersection(dq[len-1], dq[len-2]))) {\n dq.pop_back();\n --len;\n }\n\n while (len > 2 && !contains(dq[len-1], intersection(dq[0], dq[1]))) {\n dq.pop_front();\n --len;\n }\n\n if (len < 3) return {};\n\n std::vector<Vec> poly(len);\n for (int i = 0; i < len - 1; ++i) {\n poly[i] = intersection(dq[i], dq[i+1]);\n }\n poly[len-1] = intersection(dq[len-1], dq[0]);\n return poly;\n}\n\nint main() {\n ios_base::sync_with_stdio(false);\n cin.tie(nullptr);\n cout << fixed << setprecision(15);\n\n while (true) {\n int n;\n cin >> n;\n if (n == 0) break;\n vector<Vec> pts(n);\n for (auto& p : pts) cin >> p;\n\n double lb = 0, ub = 1e9;\n rep(_,0,100) {\n double m = (lb + ub) / 2;\n vector<pair<Vec, Vec>> hps;\n rep(i,0,n) {\n auto d = pts[(i+1)%n] - pts[i];\n auto e = Vec(-d.imag(), d.real());\n auto q = pts[i] + e/abs(e)*m;\n hps.push_back({e, q});\n }\n if (halfplane_intersection(hps).empty()) {\n ub = m;\n } else {\n lb = m;\n }\n }\n cout << lb << \"\\n\";\n }\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3888, "score_of_the_acc": -0.9744, "final_rank": 14 }, { "submission_id": "aoj_1283_7205573", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\n#define rep(i, s, t) for (int i = (int)(s); i < (int)(t); ++i)\n#define revrep(i, t, s) for (int i = (int)(t)-1; i >= (int)(s); --i)\n#define all(x) begin(x), end(x)\ntemplate <typename T> bool chmax(T& a, const T& b) { return a < b ? (a = b, 1) : 0; }\ntemplate <typename T> bool chmin(T& a, const T& b) { return a > b ? (a = b, 1) : 0; }\n\ntemplate <typename T>\nostream& operator<<(ostream& os, const vector<T>& v) {\n for (int i = 0; i < (int) v.size(); ++i) {\n os << v[i];\n if (i < (int) v.size() - 1) os << \" \";\n }\n return os;\n}\n\ntemplate <typename T, typename U>\nostream& operator<<(ostream& os, const pair<T, U>& p) {\n os << \"(\" << p.first << \", \" << p.second << \")\";\n return os;\n}\n\n\n\nusing T = double;\nusing Vec = std::complex<T>;\n\nconst T PI = std::acos(-1);\n\nconstexpr T eps = 1e-8;\ninline bool eq(T a, T b) { return std::abs(a - b) <= eps; }\ninline bool eq(Vec a, Vec b) { return std::abs(a - b) <= eps; }\ninline bool lt(T a, T b) { return a < b - eps; }\ninline bool leq(T a, T b) { return a <= b + eps; }\n\nstd::istream& operator>>(std::istream& is, Vec& p) {\n T x, y;\n is >> x >> y;\n p = {x, y};\n return is;\n}\n\nstruct Line {\n Vec p1, p2;\n Line() = default;\n Line(const Vec& p1, const Vec& p2) : p1(p1), p2(p2) {}\n Vec dir() const { return p2 - p1; }\n};\n\nstruct Segment {\n Vec p1, p2;\n Segment() = default;\n Segment(const Vec& p1, const Vec& p2) : p1(p1), p2(p2) {}\n Vec dir() const { return p2 - p1; }\n};\n\nstruct Circle {\n Vec c;\n T r;\n Circle() = default;\n Circle(const Vec& c, T r) : c(c), r(r) {}\n};\n\nusing Polygon = std::vector<Vec>;\n\nT dot(const Vec& a, const Vec& b) {\n return (std::conj(a) * b).real();\n}\n\nT cross(const Vec& a, const Vec& b) {\n return (std::conj(a) * b).imag();\n}\n\nVec rot(const Vec& a, T ang) {\n return a * Vec(std::cos(ang), std::sin(ang));\n}\n\nVec perp(const Vec& a) {\n return Vec(-a.imag(), a.real());\n}\n\nVec projection(const Line& l, const Vec& p) {\n return l.p1 + dot(p - l.p1, l.dir()) * l.dir() / std::norm(l.dir());\n}\n\nVec reflection(const Line& l, const Vec& p) {\n return T(2) * projection(l, p) - p;\n}\n\n// 0: collinear\n// 1: counter-clockwise\n// -1: clockwise\nint ccw(const Vec& a, const Vec& b, const Vec& c) {\n if (eq(cross(b - a, c - a), 0)) return 0;\n if (lt(cross(b - a, c - a), 0)) return -1;\n return 1;\n}\n\nvoid sort_by_arg(std::vector<Vec>& pts) {\n std::sort(pts.begin(), pts.end(), [&](auto& p, auto& q) {\n if ((p.imag() < 0) != (q.imag() < 0)) return (p.imag() < 0);\n if (cross(p, q) == 0) {\n if (p == Vec(0, 0)) return !(q.imag() < 0 || (q.imag() == 0 && q.real() > 0));\n if (q == Vec(0, 0)) return (p.imag() < 0 || (p.imag() == 0 && p.real() > 0));\n return (p.real() > q.real());\n }\n return (cross(p, q) > 0);\n });\n}\n\nstd::vector<Vec> halfplane_intersection(std::vector<std::pair<Vec, Vec>> hps) {\n using Hp = std::pair<Vec, Vec>; // (normal vector, a point on the border)\n\n auto intersection = [&](const Hp& l1, const Hp& l2) -> Vec {\n T a1 = l1.first.real(), b1 = l1.first.imag(), c1 = -dot(l1.first, l1.second);\n T a2 = l2.first.real(), b2 = l2.first.imag(), c2 = -dot(l2.first, l2.second);\n return {-(c1*b2-b1*c2)/(a1*b2-a2*b1), -(c1*a2-a1*c2)/(b1*a2-b2*a1)};\n };\n\n // check if the halfplane h contains the point p\n auto contains = [&](const Hp& h, const Vec& p) -> bool {\n return dot(p - h.second, h.first) > 0;\n };\n\n constexpr T INF = 1e15;\n hps.emplace_back(Vec(1, 0), Vec(-INF, 0)); // -INF <= x\n hps.emplace_back(Vec(-1, 0), Vec(INF, 0)); // x <= INF\n hps.emplace_back(Vec(0, 1), Vec(0, -INF)); // -INF <= y\n hps.emplace_back(Vec(0, -1), Vec(0, INF)); // y <= INF\n\n std::sort(hps.begin(), hps.end(), [&](const auto& h1, const auto& h2) {\n return std::arg(h1.first) < std::arg(h2.first);\n });\n\n std::deque<Hp> dq;\n int len = 0;\n for (auto& hp : hps) {\n while (len > 1 && !contains(hp, intersection(dq[len-1], dq[len-2]))) {\n dq.pop_back();\n --len;\n }\n\n while (len > 1 && !contains(hp, intersection(dq[0], dq[1]))) {\n dq.pop_front();\n --len;\n }\n\n // parallel\n if (len > 0 && eq(cross(dq[len-1].first, hp.first), 0)) {\n // opposite\n if (lt(dot(dq[len-1].first, hp.first), 0)) {\n return {};\n }\n // same\n if (!contains(hp, dq[len-1].second)) {\n dq.pop_back();\n --len;\n } else continue;\n }\n\n dq.push_back(hp);\n ++len;\n }\n\n while (len > 2 && !contains(dq[0], intersection(dq[len-1], dq[len-2]))) {\n dq.pop_back();\n --len;\n }\n\n while (len > 2 && !contains(dq[len-1], intersection(dq[0], dq[1]))) {\n dq.pop_front();\n --len;\n }\n\n if (len < 3) return {};\n\n std::vector<Vec> poly(len);\n for (int i = 0; i < len - 1; ++i) {\n poly[i] = intersection(dq[i], dq[i+1]);\n }\n poly[len-1] = intersection(dq[len-1], dq[0]);\n return poly;\n}\n\nint main() {\n ios_base::sync_with_stdio(false);\n cin.tie(nullptr);\n cout << fixed << setprecision(15);\n\n while (true) {\n int n;\n cin >> n;\n if (n == 0) break;\n vector<Vec> pts(n);\n for (auto& p : pts) cin >> p;\n\n double lb = 0, ub = 1e9;\n rep(_,0,100) {\n double m = (lb + ub) / 2;\n vector<pair<Vec, Vec>> hps;\n rep(i,0,n) {\n auto d = pts[(i+1)%n] - pts[i];\n auto e = Vec(-d.imag(), d.real());\n auto q = pts[i] + e/abs(e)*m;\n hps.push_back({e, q});\n }\n if (halfplane_intersection(hps).empty()) {\n ub = m;\n } else {\n lb = m;\n }\n }\n cout << lb << \"\\n\";\n }\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3884, "score_of_the_acc": -0.9712, "final_rank": 13 }, { "submission_id": "aoj_1283_7205328", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\n#define rep(i, s, t) for (int i = (int)(s); i < (int)(t); ++i)\n#define revrep(i, t, s) for (int i = (int)(t)-1; i >= (int)(s); --i)\n#define all(x) begin(x), end(x)\ntemplate <typename T> bool chmax(T& a, const T& b) { return a < b ? (a = b, 1) : 0; }\ntemplate <typename T> bool chmin(T& a, const T& b) { return a > b ? (a = b, 1) : 0; }\n\ntemplate <typename T>\nostream& operator<<(ostream& os, const vector<T>& v) {\n for (int i = 0; i < (int) v.size(); ++i) {\n os << v[i];\n if (i < (int) v.size() - 1) os << \" \";\n }\n return os;\n}\n\ntemplate <typename T, typename U>\nostream& operator<<(ostream& os, const pair<T, U>& p) {\n os << \"(\" << p.first << \", \" << p.second << \")\";\n return os;\n}\n\n\n\nusing T = double;\nusing Vec = std::complex<T>;\n\nconst T PI = std::acos(-1);\n\nconstexpr T eps = 1e-8;\ninline bool eq(T a, T b) { return std::abs(a - b) <= eps; }\ninline bool eq(Vec a, Vec b) { return std::abs(a - b) <= eps; }\ninline bool lt(T a, T b) { return a < b - eps; }\ninline bool leq(T a, T b) { return a <= b + eps; }\n\nstd::istream& operator>>(std::istream& is, Vec& p) {\n T x, y;\n is >> x >> y;\n p = {x, y};\n return is;\n}\n\nstruct Line {\n Vec p1, p2;\n Line() = default;\n Line(const Vec& p1, const Vec& p2) : p1(p1), p2(p2) {}\n Vec dir() const { return p2 - p1; }\n};\n\nstruct Segment {\n Vec p1, p2;\n Segment() = default;\n Segment(const Vec& p1, const Vec& p2) : p1(p1), p2(p2) {}\n Vec dir() const { return p2 - p1; }\n};\n\nstruct Circle {\n Vec c;\n T r;\n Circle() = default;\n Circle(const Vec& c, T r) : c(c), r(r) {}\n};\n\nusing Polygon = std::vector<Vec>;\n\nT dot(const Vec& a, const Vec& b) {\n return (std::conj(a) * b).real();\n}\n\nT cross(const Vec& a, const Vec& b) {\n return (std::conj(a) * b).imag();\n}\n\nVec rot(const Vec& a, T ang) {\n return a * Vec(std::cos(ang), std::sin(ang));\n}\n\nVec perp(const Vec& a) {\n return Vec(-a.imag(), a.real());\n}\n\nVec projection(const Line& l, const Vec& p) {\n return l.p1 + dot(p - l.p1, l.dir()) * l.dir() / std::norm(l.dir());\n}\n\nVec reflection(const Line& l, const Vec& p) {\n return T(2) * projection(l, p) - p;\n}\n\n// 0: collinear\n// 1: counter-clockwise\n// -1: clockwise\nint ccw(const Vec& a, const Vec& b, const Vec& c) {\n if (eq(cross(b - a, c - a), 0)) return 0;\n if (lt(cross(b - a, c - a), 0)) return -1;\n return 1;\n}\n\nvoid sort_by_arg(std::vector<Vec>& pts) {\n std::sort(pts.begin(), pts.end(), [&](auto& p, auto& q) {\n if ((p.imag() < 0) != (q.imag() < 0)) return (p.imag() < 0);\n if (cross(p, q) == 0) {\n if (p == Vec(0, 0)) return !(q.imag() < 0 || (q.imag() == 0 && q.real() > 0));\n if (q == Vec(0, 0)) return (p.imag() < 0 || (p.imag() == 0 && p.real() > 0));\n return (p.real() > q.real());\n }\n return (cross(p, q) > 0);\n });\n}\n\nstd::vector<std::pair<double, double>> halfplane_intersection(std::vector<std::tuple<double, double, double>> hps) {\n using Line = std::tuple<double, double, double>;\n using Point = std::pair<double, double>;\n\n constexpr double INF = 1e9;\n hps.emplace_back(-1, 0, INF); // x <= INF\n hps.emplace_back(1, 0, INF); // x >= -INF\n hps.emplace_back(0, -1, INF); // y <= INF\n hps.emplace_back(0, 1, INF); // y >= -INF\n\n std::sort(hps.begin(), hps.end(), [&](const auto& l1, const auto& l2) {\n auto [a1, b1, _c1] = l1;\n auto [a2, b2, _c2] = l2;\n return std::atan2(b1, a1) < std::atan2(b2, a2);\n });\n\n auto intersection = [&](const Line& l1, const Line& l2) -> Point {\n auto [a1, b1, c1] = l1;\n auto [a2, b2, c2] = l2;\n return {-(c1*b2-b1*c2)/(a1*b2-a2*b1), -(c1*a2-a1*c2)/(b1*a2-b2*a1)};\n };\n\n // check if the halfplane h contains the point p\n auto contains = [&](const Line& h, const Point& p) -> bool {\n auto [x, y] = p;\n auto [a, b, c] = h;\n return a*x+b*y+c > 0;\n };\n\n auto dot = [&](const Line& l1, const Line& l2) -> double {\n auto [a1, b1, _c1] = l1;\n auto [a2, b2, _c2] = l2;\n return a1*a2 + b1*b2;\n };\n\n auto cross = [&](const Line& l1, const Line& l2) -> double {\n auto [a1, b1, _c1] = l1;\n auto [a2, b2, _c2] = l2;\n return a1*b2 - a2*b1;\n };\n\n auto get_point = [&](const Line& line) -> Point {\n auto [a, b, c] = line;\n if (eq(b, 0)) return {-c/a, 0};\n return {0, -c/b};\n };\n\n std::deque<Line> dq;\n int len = 0;\n for (auto& hp : hps) {\n auto [a, b, c] = hp;\n while (len > 1 && !contains(hp, intersection(dq[len-1], dq[len-2]))) {\n dq.pop_back();\n --len;\n }\n\n while (len > 1 && !contains(hp, intersection(dq[0], dq[1]))) {\n dq.pop_front();\n --len;\n }\n\n // parallel\n if (len > 0 && eq(cross(dq[len-1], hp), 0)) {\n // opposite\n if (lt(dot(dq[len-1], hp), 0)) {\n return {};\n }\n // same\n if (!contains(hp, get_point(dq[len-1]))) {\n dq.pop_back();\n --len;\n } else continue;\n }\n\n dq.push_back(hp);\n ++len;\n }\n\n while (len > 2 && !contains(dq[0], intersection(dq[len-1], dq[len-2]))) {\n dq.pop_back();\n --len;\n }\n\n while (len > 2 && !contains(dq[len-1], intersection(dq[0], dq[1]))) {\n dq.pop_front();\n --len;\n }\n\n if (len < 3) return {};\n\n std::vector<Point> poly(len);\n for (int i = 0; i < len - 1; ++i) {\n poly[i] = intersection(dq[i], dq[i+1]);\n }\n poly[len-1] = intersection(dq[len-1], dq[0]);\n return poly;\n}\n\nint main() {\n ios_base::sync_with_stdio(false);\n cin.tie(nullptr);\n cout << fixed << setprecision(15);\n\n while (true) {\n int n;\n cin >> n;\n if (n == 0) break;\n vector<Vec> pts(n);\n for (auto& p : pts) cin >> p;\n\n double lb = 0, ub = 1e9;\n rep(_,0,100) {\n // rep(_,0,1) {\n double m = (lb + ub) / 2;\n // double m = 1e9;\n vector<tuple<double, double, double>> hps;\n rep(i,0,n) {\n auto d = pts[(i+1)%n] - pts[i];\n auto e = Vec(-d.imag(), d.real());\n auto q = pts[i] + e/abs(e)*m;\n double c = -dot(e, q);\n hps.push_back({e.real(), e.imag(), c});\n }\n if (halfplane_intersection(hps).empty()) {\n ub = m;\n } else {\n lb = m;\n }\n }\n cout << lb << \"\\n\";\n }\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3920, "score_of_the_acc": -1, "final_rank": 15 }, { "submission_id": "aoj_1283_6707857", "code_snippet": "#pragma GCC ta g(\"avx2\")\n#pragma GCC optimize(\"Ofast\")\n#pragma GCC optimize(\"unroll-loops\")\n#include <bits/stdc++.h>\nusing namespace std;\n#define DEBUG\n#ifdef DEBUG\ntemplate <class T, class U>\nostream &operator<<(ostream &os, const pair<T, U> &p) {\n os << '(' << p.first << ',' << p.second << ')';\n return os;\n}\ntemplate <class T> ostream &operator<<(ostream &os, const vector<T> &v) {\n os << '{';\n for(int i = 0; i < (int)v.size(); i++) {\n if(i) { os << ','; }\n os << v[i];\n }\n os << '}';\n return os;\n}\nvoid debugg() { cerr << endl; }\ntemplate <class T, class... Args>\nvoid debugg(const T &x, const Args &... args) {\n cerr << \" \" << x;\n debugg(args...);\n}\n#define debug(...) \\\n cerr << __LINE__ << \" [\" << #__VA_ARGS__ << \"]: \", debugg(__VA_ARGS__)\n#define dump(x) cerr << __LINE__ << \" \" << #x << \" = \" << (x) << endl\n#else\n#define debug(...) (void(0))\n#define dump(x) (void(0))\n#endif\nusing namespace std;\ntypedef long long ll;\ntypedef vector<ll> vl;\ntypedef vector<vl> vvl;\ntypedef vector<char> vc;\ntypedef vector<string> vs;\ntypedef vector<bool> vb;\ntypedef vector<double> vd;\ntypedef pair<ll,ll> P;\ntypedef pair<int,int> pii;\ntypedef vector<P> vpl;\ntypedef tuple<ll,ll,ll> tapu;\n#define rep(i,n) for(int i=0; i<(n); i++)\n#define REP(i,a,b) for(int i=(a); i<(b); i++)\n#define all(x) (x).begin(), (x).end()\n#define rall(x) (x).rbegin(), (x).rend()\ntemplate<typename T1,typename T2> inline bool chmin(T1 &a,T2 b){\n\tif(a>b){\n\t\ta = b; return true;\n\t}\n\telse return false;\n}\ntemplate<typename T1,typename T2> inline bool chmax(T1 &a,T2 b){\n\tif(a<b){\n\t\ta = b; return true;\n\t}\n\telse return false;\n}\ntemplate<typename T> inline void print(T &a){\n int sz = a.size();\n for(auto itr = a.begin(); itr != a.end(); itr++){\n\t\tcout << *itr;\n sz--;\n if(sz) cout << \" \";\n\t}\n cout << \"\\n\";\n}\ntemplate<typename T1,typename T2> inline void print2(T1 a, T2 b){\n\tcout << a << \" \" << b << \"\\n\";\n}\ntemplate<typename T1,typename T2,typename T3> inline void print3(T1 a, T2 b, T3 c){\n\tcout << a << \" \" << b << \" \" << c << \"\\n\";\n}\nvoid mark() {cout << \"#\" << \"\\n\";}\nll pcount(ll x) {return __builtin_popcountll(x);}\n// #include <atcoder/convolution>\n// using namespace atcoder;\nconst int inf = (1<<30)-1;\nconst ll linf = 1LL<<61;\nconst int MAX = 1020000;\nint dy[8] = {0,1,0,-1,1,-1,-1,1};\nint dx[8] = {-1,0,1,0,1,-1,1,-1};\nconst double pi = acos(-1);\nconst double eps = 1e-9;\nconst int mod = 1e9 + 7;\n//const int mod = 998244353;\n\nnamespace geometry {\n // Point : 複素数型を位置ベクトルとして扱う\n // 実軸(real)をx軸、挙軸(imag)をy軸として見る\n using D = long double;\n using Point = complex<D>;\n const D EPS = 1e-7;\n const D PI = acosl(-1);\n\n inline bool equal(const D &a, const D &b) { return fabs(a - b) < EPS; }\n\n // 単位ベクトル(unit vector)を求める\n Point unitVector(const Point &a) { return a / abs(a); }\n\n // 法線ベクトル(normal vector)を求める\n // 90度回転した単位ベクトルをかける\n // -90度がよければPoint(0, -1)をかける\n Point normalVector(const Point &a) { return a * Point(0, 1); }\n\n // 内積(dot product) : a・b = |a||b|cosΘ\n D dot(const Point &a, const Point &b) {\n return (a.real() * b.real() + a.imag() * b.imag());\n }\n\n // 外積(cross product) : a×b = |a||b|sinΘ\n D cross(const Point &a, const Point &b) {\n return (a.real() * b.imag() - a.imag() * b.real());\n }\n\n // 点pを反時計回りにtheta度回転\n // thetaはラジアン!!!\n Point rotate(const Point &p, const D &theta) {\n return Point(cos(theta) * p.real() - sin(theta) * p.imag(),\n sin(theta) * p.real() + cos(theta) * p.imag());\n }\n\n // ラジアン->度\n D radianToDegree(const D &radian) { return radian * 180.0 / PI; }\n\n // 度->ラジアン\n D degreeToRadian(const D &degree) { return degree * PI / 180.0; }\n\n // 点の回転方向\n // 点a, b, cの位置関係について(aが基準点)\n int ccw(const Point &a, Point b, Point c) {\n b -= a, c -= a;\n // 点a, b, c が\n // 反時計回りの時、\n if(cross(b, c) > EPS) return 1;\n // 時計回りの時、\n if(cross(b, c) < -EPS) return -1;\n // c, a, bがこの順番で同一直線上にある時、\n if(dot(b, c) < 0) return 2;\n // a, b, cがこの順番で同一直線上にある場合、\n if(norm(b) < norm(c)) return -2;\n // cが線分ab上にある場合、\n return 0;\n }\n\n // Line : 直線を表す構造体\n // b - a で直線・線分を表せる\n struct Line {\n Point a, b;\n Line() = default;\n Line(Point a, Point b) : a(a), b(b) {}\n // Ax+By=C\n Line(D A, D B, D C) {\n if(equal(A, 0)) {\n a = Point(0, C / B), b = Point(1, C / B);\n } else if(equal(B, 0)) {\n b = Point(C / A, 0), b = Point(C / A, 1);\n } else {\n a = Point(0, C / B), b = Point(C / A, 0);\n }\n }\n };\n\n // Segment : 線分を表す構造体\n // Lineと同じ\n struct Segment : Line {\n Segment() = default;\n\n Segment(Point a, Point b) : Line(a, b) {}\n };\n\n // Circle : 円を表す構造体\n // pが中心の位置ベクトル、rは半径\n struct Circle {\n Point p;\n D r;\n\n Circle() = default;\n\n Circle(Point p, D r) : p(p), r(r) {}\n };\n\n // 2直線の直交判定 : a⊥b <=> dot(a, b) = 0\n bool isOrthogonal(const Line &a, const Line &b) {\n return equal(dot(a.b - a.a, b.b - b.a), 0);\n }\n // 2直線の平行判定 : a//b <=> cross(a, b) = 0\n bool isParallel(const Line &a, const Line &b) {\n return equal(cross(a.b - a.a, b.b - b.a), 0);\n }\n\n // 点cが直線ab上にあるか\n bool isPointOnLine(const Point &a, const Point &b, const Point &c) {\n return isParallel(Line(a, b), Line(a, c));\n }\n\n // 点cが\"線分\"ab上にあるか\n bool isPointOnSegment(const Point &a, const Point &b, const Point &c) {\n // |a-c| + |c-b| <= |a-b| なら線分上\n return (abs(a - c) + abs(c - b) < abs(a - b) + EPS);\n }\n\n // 直線lと点pの距離を求める\n D distanceBetweenLineAndPoint(const Line &l, const Point &p) {\n return abs(cross(l.b - l.a, p - l.a)) / abs(l.b - l.a);\n }\n\n // 線分lと点pの距離を求める\n // 定義:点pから「線分lのどこか」への最短距離\n D distanceBetweenSegmentAndPoint(const Segment &l, const Point &p) {\n if(dot(l.b - l.a, p - l.a) < EPS) return abs(p - l.a);\n if(dot(l.a - l.b, p - l.b) < EPS) return abs(p - l.b);\n return abs(cross(l.b - l.a, p - l.a)) / abs(l.b - l.a);\n }\n\n // 直線s, tの交点の計算\n Point crossPoint(const Line &s, const Line &t) {\n D d1 = cross(s.b - s.a, t.b - t.a);\n D d2 = cross(s.b - s.a, s.b - t.a);\n if(equal(abs(d1), 0) && equal(abs(d2), 0)) return t.a;\n return t.a + (t.b - t.a) * (d2 / d1);\n }\n\n // 線分s, tの交点の計算\n Point crossPoint(const Segment &s, const Segment &t) {\n return crossPoint(Line(s), Line(t));\n }\n\n // 線分sと線分tが交差しているかどうか\n // bound:線分の端点を含むか\n bool isIntersect(const Segment &s, const Segment &t, bool bound) {\n return ccw(s.a, s.b, t.a) * ccw(s.a, s.b, t.b) < bound &&\n ccw(t.a, t.b, s.a) * ccw(t.a, t.b, s.b) < bound;\n }\n\n // 線分sとtの距離\n D distanceBetweenSegments(const Segment &s, const Segment &t) {\n if(isIntersect(s, t, 1)) return (D)(0);\n D ans = distanceBetweenSegmentAndPoint(s, t.a);\n chmin(ans, distanceBetweenSegmentAndPoint(s, t.b));\n chmin(ans, distanceBetweenSegmentAndPoint(t, s.a));\n chmin(ans, distanceBetweenSegmentAndPoint(t, s.b));\n return ans;\n }\n\n // 射影(projection)\n // 直線(線分)lに点pから引いた垂線の足を求める\n Point projection(const Line &l, const Point &p) {\n D t = dot(p - l.a, l.a - l.b) / norm(l.a - l.b);\n return l.a + (l.a - l.b) * t;\n }\n\n Point projection(const Segment &l, const Point &p) {\n D t = dot(p - l.a, l.a - l.b) / norm(l.a - l.b);\n return l.a + (l.a - l.b) * t;\n }\n\n // 反射(reflection)\n // 直線lを対称軸として点pと線対称の位置にある点を求める\n Point reflection(const Line &l, const Point &p) {\n return p + (projection(l, p) - p) * (D)2.0;\n }\n\n // 2つの円の交差判定\n // 返り値は共通接線の数\n int isIntersect(const Circle &c1, const Circle &c2) {\n D d = abs(c1.p - c2.p);\n // 2つの円が離れている場合\n if(d > c1.r + c2.r + EPS) return 4;\n // 外接している場合\n if(equal(d, c1.r + c2.r)) return 3;\n // 内接している場合\n if(equal(d, abs(c1.r - c2.r))) return 1;\n // 内包している場合\n if(d < abs(c1.r - c2.r) - EPS) return 0;\n return 2;\n }\n\n // 2つの円の交点\n vector<Point> crossPoint(const Circle &c1, const Circle &c2) {\n vector<Point> res;\n int mode = isIntersect(c1, c2);\n // 2つの中心の距離\n D d = abs(c1.p - c2.p);\n // 2円が離れている場合\n if(mode == 4) return res;\n // 1つの円がもう1つの円に内包されている場合\n if(mode == 0) return res;\n // 2円が外接する場合\n if(mode == 3) {\n D t = c1.r / (c1.r + c2.r);\n res.emplace_back(c1.p + (c2.p - c1.p) * t);\n return res;\n }\n // 内接している場合\n if(mode == 1) {\n if(c2.r < c1.r - EPS) {\n res.emplace_back(c1.p + (c2.p - c1.p) * (c1.r / d));\n } else {\n res.emplace_back(c2.p + (c1.p - c2.p) * (c2.r / d));\n }\n return res;\n }\n // 2円が重なる場合\n D rc1 = (c1.r * c1.r + d * d - c2.r * c2.r) / (2 * d);\n D rs1 = sqrt(c1.r * c1.r - rc1 * rc1);\n if(c1.r - abs(rc1) < EPS) rs1 = 0;\n Point e12 = (c2.p - c1.p) / abs(c2.p - c1.p);\n res.emplace_back(c1.p + rc1 * e12 + rs1 * e12 * Point(0, 1));\n res.emplace_back(c1.p + rc1 * e12 + rs1 * e12 * Point(0, -1));\n return res;\n }\n\n // 点pが円cの内部(円周上も含む)に入っているかどうか\n bool isInCircle(const Circle &c, const Point &p) {\n D d = abs(c.p - p);\n return (equal(d, c.r) || d < c.r - EPS);\n }\n\n // 円cと直線lの交点\n vector<Point> crossPoint(const Circle &c, const Line &l) {\n vector<Point> res;\n D d = distanceBetweenLineAndPoint(l, c.p);\n // 交点を持たない\n if(d > c.r + EPS) return res;\n // 接する\n Point h = projection(l, c.p);\n if(equal(d, c.r)) {\n res.emplace_back(h);\n return res;\n }\n Point e = unitVector(l.b - l.a);\n D ph = sqrt(c.r * c.r - d * d);\n res.emplace_back(h - e * ph);\n res.emplace_back(h + e * ph);\n return res;\n }\n\n // 点pを通る円cの接線\n // 2本あるので、接点のみを返す\n vector<Point> tangentToCircle(const Point &p, const Circle &c) {\n return crossPoint(c, Circle(p, sqrt(norm(c.p - p) - c.r * c.r)));\n }\n\n // 円の共通接線\n vector<Line> tangent(const Circle &a, const Circle &b) {\n vector<Line> ret;\n // 2円の中心間の距離\n D g = abs(a.p - b.p);\n // 円が内包されている場合\n if(equal(g, 0)) return ret;\n Point u = unitVector(b.p - a.p);\n Point v = rotate(u, PI / 2);\n for(int s : {-1, 1}) {\n D h = (a.r + b.r * s) / g;\n if(equal(h * h, 1)) {\n ret.emplace_back(a.p + (h > 0 ? u : -u) * a.r,\n a.p + (h > 0 ? u : -u) * a.r + v);\n\n } else if(1 - h * h > 0) {\n Point U = u * h, V = v * sqrt(1 - h * h);\n ret.emplace_back(a.p + (U + V) * a.r,\n b.p - (U + V) * (b.r * s));\n ret.emplace_back(a.p + (U - V) * a.r,\n b.p - (U - V) * (b.r * s));\n }\n }\n return ret;\n }\n\n // 多角形の面積を求める\n D PolygonArea(const vector<Point> &p) {\n D res = 0;\n int n = p.size();\n for(int i = 0; i < n - 1; i++) res += cross(p[i], p[i + 1]);\n res += cross(p[n - 1], p[0]);\n return res * 0.5;\n }\n\n // 凸多角形かどうか\n bool isConvex(const vector<Point> &p) {\n int n = p.size();\n int now, pre, nxt;\n for(int i = 0; i < n; i++) {\n pre = (i - 1 + n) % n;\n nxt = (i + 1) % n;\n now = i;\n if(ccw(p[pre], p[now], p[nxt]) == -1) return false;\n }\n return true;\n }\n\n // 凸包 O(NlogN)\n vector<Point> ConvexHull(vector<Point> &p) {\n int n = (int)p.size(), k = 0;\n sort(all(p), [](const Point &a, const Point &b) {\n return (real(a) != real(b) ? real(a) < real(b) : imag(a) < imag(b));\n });\n vector<Point> ch(2 * n);\n // 一直線上の3点を含める -> (< -EPS)\n // 含め無い -> (< EPS)\n for(int i = 0; i < n; ch[k++] = p[i++]) { // lower\n while(k >= 2 &&\n cross(ch[k - 1] - ch[k - 2], p[i] - ch[k - 1]) < EPS)\n --k;\n }\n for(int i = n - 2, t = k + 1; i >= 0; ch[k++] = p[i--]) { // upper\n while(k >= t &&\n cross(ch[k - 1] - ch[k - 2], p[i] - ch[k - 1]) < EPS)\n --k;\n }\n ch.resize(k - 1);\n return ch;\n }\n\n // 多角形gに点pが含まれているか?\n // 含まれる:2, 辺上にある:1, 含まれない:0\n int isContained(const vector<Point> &g, const Point &p) {\n bool in = false;\n int n = (int)g.size();\n for(int i = 0; i < n; i++) {\n Point a = g[i] - p, b = g[(i + 1) % n] - p;\n if(imag(a) > imag(b)) swap(a, b);\n if(imag(a) <= EPS && EPS < imag(b) && cross(a, b) < -EPS) in = !in;\n if(cross(a, b) == 0 && dot(a, b) <= 0) return 1;\n }\n return (in ? 2 : 0);\n }\n\n vector<Point> convex_cut(const vector<Point> &g, Line l){\n vector<Point> ret;\n for(int i=0; i<g.size(); i++){\n Point now = g[i], nxt = g[(i+1)%g.size()];\n if(ccw(l.a,l.b,now) != -1) ret.push_back(now);\n if(ccw(l.a,l.b,now) * ccw(l.a,l.b,nxt) < 0){\n ret.push_back(crossPoint(Line(now,nxt),l));\n }\n }\n return ret;\n }\n\n bool equal (const Point &a, const Point &b) {\n return equal(a.real(),b.real()) && equal(a.imag(),b.imag());\n }\n\n D circlesIntersectArea(Circle &a, Circle &b){\n double d = abs(a.p - b.p);\n if(d >= a.r + b.r - EPS) return 0; //2円が離れている\n auto R = minmax(a.r,b.r);\n if(d <= R.second - R.first + EPS) return R.first*R.first*pi; //円が円を内包する\n D theta1 = acos((d*d + a.r*a.r - b.r*b.r)/(2*a.r*d));\n D theta2 = acos((d*d + b.r*b.r - a.r*a.r)/(2*b.r*d));\n D ret = a.r*a.r*(theta1-cos(theta1)*sin(theta1)) + b.r*b.r*(theta2-cos(theta2)*sin(theta2));\n return ret;\n }\n} // namespace geometry\n\nusing namespace geometry;\n\n\nint main(){\n\twhile(1){\n int n; cin >> n;\n if(!n) break;\n vector<Point> g(n);\n rep(i,n){\n int x,y; cin >> x >> y;\n g[i] = Point(x,y);\n }\n D ok = 0, ng = 100000;\n rep(z,60){\n D mid = (ok+ng) / 2;\n auto calcLine = [&](Point a, Point b) -> Line {\n Point diff = b - a;\n Point vec = unitVector(Point(-diff.imag(),diff.real()));\n vec *= mid;\n return Line(a+vec, b+vec);\n };\n vector<Point> G = g;\n rep(i,n){\n Line l = calcLine(g[i],g[(i+1)%n]);\n G = convex_cut(G,l);\n }\n if(G.size() <= 1) ng = mid;\n else ok = mid; \n }\n printf(\"%.10Lf\\n\",ok);\n }\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3368, "score_of_the_acc": -0.5591, "final_rank": 2 }, { "submission_id": "aoj_1283_6368831", "code_snippet": "#line 2 \"library/bits/stdc++.h\"\n\n// C\n#ifndef _GLIBCXX_NO_ASSERT\n#include <cassert>\n#endif\n#include <cctype>\n#include <cerrno>\n#include <cfloat>\n#include <ciso646>\n#include <climits>\n#include <clocale>\n#include <cmath>\n#include <csetjmp>\n#include <csignal>\n#include <cstdarg>\n#include <cstddef>\n#include <cstdio>\n#include <cstdlib>\n#include <cstring>\n#include <ctime>\n\n#if __cplusplus >= 201103L\n#include <ccomplex>\n#include <cfenv>\n#include <cinttypes>\n#include <cstdbool>\n#include <cstdint>\n#include <ctgmath>\n#include <cwchar>\n#include <cwctype>\n#endif\n\n// C++\n#include <algorithm>\n#include <bitset>\n#include <complex>\n#include <deque>\n#include <exception>\n#include <fstream>\n#include <functional>\n#include <iomanip>\n#include <ios>\n#include <iosfwd>\n#include <iostream>\n#include <istream>\n#include <iterator>\n#include <limits>\n#include <list>\n#include <locale>\n#include <map>\n#include <memory>\n#include <new>\n#include <numeric>\n#include <ostream>\n#include <queue>\n#include <set>\n#include <sstream>\n#include <stack>\n#include <stdexcept>\n#include <streambuf>\n#include <string>\n#include <typeinfo>\n#include <utility>\n#include <valarray>\n#include <vector>\n\n#if __cplusplus >= 201103L\n#include <array>\n#include <atomic>\n#include <chrono>\n#include <condition_variable>\n#include <forward_list>\n#include <future>\n#include <initializer_list>\n#include <mutex>\n#include <random>\n#include <ratio>\n#include <regex>\n#include <scoped_allocator>\n#include <system_error>\n#include <thread>\n#include <tuple>\n#include <typeindex>\n#include <type_traits>\n#include <unordered_map>\n#include <unordered_set>\n#endif\n#line 2 \"Source.cpp\"\nusing namespace std;\n\ntypedef string::const_iterator State;\n#define eps 1e-8L\n#define MAX_MOD 1000000007LL\n#define GYAKU 500000004LL\n#define MOD 998244353LL\n#define pb push_back\n#define mp make_pair\ntypedef long long ll;\ntypedef long double ld;\n#define REP(a, b) for (long long(a) = 0; (a) < (b); ++(a))\n#define ALL(x) (x).begin(), (x).end()\n\n#line 2 \"library/kotamanegi/geometry.hpp\"\n#define _USE_MATH_DEFINES\nusing namespace std;\n\ntypedef long double ld;\ntypedef complex<ld> Point;\ntypedef pair<Point, Point> Segment;\ntypedef Segment Line;\n\nconst ld EPS = 1e-10;\n\nld abs(Line x)\n{\n return abs(x.second - x.first);\n}\n\n// dot = |a||b| cos(theta)\nld dot(Point a, Point b)\n{\n return a.real() * b.real() + a.imag() * b.imag();\n}\n\n// cross = |a||b| sin(theta)\nld cross(Point a, Point b)\n{\n return a.real() * b.imag() - a.imag() * b.real();\n}\n\n// 射影: 直線sと点pから引いた垂線の交点\nPoint projection(Line s, Point p)\n{\n Point base = s.second - s.first;\n ld r = dot(p - s.first, base) / norm(base);\n return s.first + base * r;\n}\n\n// 反射: 点pを直線sと線対称に移動した点\nPoint reflection(Line s, Point p)\n{\n return p + (projection(s, p) - p) * Point{2.0, 0.0};\n}\n\nLine vertical_bisector(Line x)\n{\n Point now = (x.first + x.second) * Point{0.5, 0};\n return Line(now, now + (x.second - x.first) * Point{0, M_PI_2});\n}\n\nenum class Position\n{\n COUNTER_CLOCKWISE, // 反時計回り\n CLOCKWISE, // 時計回り\n ON_LINE_BACK, // Lineの始点側に点がある\n ON_LINE_FRONT, // Lineの終点側に点がある\n ON_SEGMENT // Line上に点がある\n};\n\n// ccw: 線分sと点pの位置関係を示す\nPosition ccw(Line s, Point p)\n{\n Point a = s.second - s.first, b = p - s.first;\n if (cross(a, b) > eps)\n return Position::COUNTER_CLOCKWISE;\n if (cross(a, b) < -eps)\n return Position::CLOCKWISE;\n if (dot(a, b) < -EPS)\n return Position::ON_LINE_BACK;\n if (norm(a) < norm(b))\n return Position::ON_LINE_FRONT;\n return Position::ON_SEGMENT;\n}\n\n// 平行判定\nbool is_parallel(Point a, Point b) { return abs(cross(a, b)) < EPS; }\nbool is_parallel(Line a, Line b) { return is_parallel(a.second - a.first, b.second - b.first); }\n\n// 直交判定\nbool is_orthogonal(Point a, Point b) { return abs(dot(a, b)) < EPS; }\nbool is_orthogonal(Line a, Line b) { return is_orthogonal(a.second - a.first, b.second - b.first); }\n\n// 交差判定\nbool is_intersect_line(Line s1, Segment s2)\n{\n if (cross(s1.second - s1.first, s2.first - s1.first) * cross(s1.second - s1.first, s2.second - s1.first) > eps)\n {\n return false;\n }\n if (ccw(s1, s2.first) == Position::ON_LINE_BACK and ccw(s1, s2.second) == Position::ON_LINE_BACK)\n {\n return false;\n }\n if (ccw(s1, s2.first) == Position::ON_LINE_FRONT and ccw(s1, s2.second) == Position::ON_LINE_FRONT)\n {\n return false;\n }\n return true;\n}\n\nbool is_intersect(Segment s1, Segment s2)\n{\n for (int i = 0; i < 2; ++i)\n {\n if (!is_intersect_line(s1, s2))\n {\n return false;\n }\n swap(s1, s2);\n }\n return true;\n}\n\n// 交差点\nPoint intersection_point(Line s1, Line s2)\n{\n ld s, t;\n ld deno = cross(s1.second - s1.first, s2.second - s2.first) + eps;\n s = cross(s2.first - s1.first, s2.second - s2.first) / deno;\n return s1.first + s * (s1.second - s1.first);\n}\n\n//線分と点の距離\nld distance(Segment s, Point p)\n{\n if (dot(s.second - s.first, p - s.first) < eps)\n return abs(p - s.first);\n if (dot(s.first - s.second, p - s.second) < eps)\n return abs(p - s.second);\n return abs(cross(s.second - s.first, p - s.first)) / abs(s);\n}\n\n//線分と線分の距離\nld distance(Segment s1, Segment s2)\n{\n if (is_intersect(s1, s2))\n {\n return 0;\n }\n return min({distance(s1, s2.first), distance(s1, s2.second), distance(s2, s1.first), distance(s2, s1.second)});\n}\n\ntypedef vector<Point> Polygon;\n\n//多角形の面積\nld area(Polygon poly)\n{\n ld ans = 0;\n for (int i = 0; i < poly.size(); ++i)\n {\n Point a = poly[i], b = poly[(i + 1) % poly.size()];\n ans += cross(a, b);\n }\n ans /= 2.0L;\n return abs(ans);\n}\n\n//凸多角形か判定\nbool is_convex(Polygon poly)\n{\n for (int i = 0; i < poly.size(); ++i)\n {\n Line a = {poly[i], poly[(i + 1) % poly.size()]};\n if (ccw(a, poly[(i + 2) % poly.size()]) == Position::CLOCKWISE)\n return false;\n }\n return true;\n}\n\n//多角形上に点があるか判定\nbool is_on_polygon(Polygon poly, Point p)\n{\n for (int i = 0; i < poly.size(); ++i)\n {\n Line a = {poly[i], poly[(i + 1) % poly.size()]};\n if (ccw(a, p) == Position::ON_SEGMENT)\n return true;\n }\n return false;\n}\n\n//多角形内に点があるか判定\nbool is_inside_polygon(Polygon poly, Point p)\n{\n if (is_on_polygon(poly, p))\n return true;\n const int n = poly.size();\n int wn = 0;\n for (int i = 0; i < n; ++i)\n {\n ld vt = (p.imag() - poly[i].imag()) / (poly[(i + 1) % n].imag() - poly[i].imag());\n if (p.real() < (poly[i].real() + vt * (poly[(i + 1) % n].real() - poly[i].real())))\n {\n if ((poly[i].imag() <= p.imag()) and (poly[(i + 1) % n].imag() > p.imag()))\n {\n wn++;\n }\n else if ((poly[i].imag() > p.imag()) and (poly[(i + 1) % n].imag() <= p.imag()))\n {\n wn--;\n }\n }\n }\n if (wn != 0)\n return true;\n return false;\n}\n\n// 凸包\nPolygon convex_hull(Polygon poly)\n{\n sort(ALL(poly), [](auto &l, auto &r)\n {\n if(l.real() != r.real()) return l.real() < r.real();\n return l.imag() < r.imag(); });\n Polygon ans;\n ans.push_back(poly[0]);\n ans.push_back(poly[1]);\n for (int i = 2; i < poly.size(); ++i)\n {\n while (ans.size() >= 2)\n {\n Point x = ans[ans.size() - 2];\n Point y = ans[ans.size() - 1];\n if (ccw({x, y}, poly[i]) == Position::COUNTER_CLOCKWISE)\n {\n ans.pop_back();\n }\n else\n break;\n }\n ans.push_back(poly[i]);\n }\n\n Polygon reversed_ans;\n reversed_ans.push_back(poly[0]);\n reversed_ans.push_back(poly[1]);\n for (int i = 2; i < poly.size(); ++i)\n {\n while (reversed_ans.size() >= 2)\n {\n Point x = reversed_ans[reversed_ans.size() - 2];\n Point y = reversed_ans[reversed_ans.size() - 1];\n if (ccw({x, y}, poly[i]) == Position::CLOCKWISE)\n {\n reversed_ans.pop_back();\n }\n else\n break;\n }\n reversed_ans.push_back(poly[i]);\n }\n for (int q = reversed_ans.size() - 2; q >= 1; --q)\n {\n ans.push_back(reversed_ans[q]);\n }\n return ans;\n}\n\n// 凸多角形の直径\nld diameter(Polygon poly)\n{\n ld ans = 0;\n int itr = 0;\n REP(i, poly.size())\n {\n if (abs(poly[i] - poly[0]) > abs(poly[itr] - poly[0]))\n {\n itr = i;\n }\n }\n REP(i, poly.size())\n {\n while (abs(poly[(itr + 1) % poly.size()] - poly[i]) > abs(poly[itr] - poly[i]))\n {\n itr++;\n itr %= poly.size();\n }\n ans = max(ans, abs(poly[itr] - poly[i]));\n }\n return ans;\n}\n\n// 凸多角形の切断\nPolygon convex_cut(Polygon poly, Line l)\n{\n Polygon ans;\n for (int i = 0; i < poly.size(); ++i)\n {\n Point now = poly[i];\n Point nxt = poly[(i + 1) % poly.size()];\n if (ccw(l, now) != Position::CLOCKWISE)\n {\n ans.push_back(now);\n }\n if (is_intersect_line(l, {now, nxt}))\n {\n ans.push_back(intersection_point(l, {now, nxt}));\n }\n }\n return ans;\n}\n#line 17 \"Source.cpp\"\n\n#define int long long\n\nbool judge(ld dist,Polygon poly){\n Polygon cur = poly;\n for (int i = 0;i < poly.size();++i){\n Line now = {poly[i], poly[(i + 1) % poly.size()]};\n Line moves = vertical_bisector(now);\n Point Move = moves.second - moves.first;\n Move /= abs(Move);\n now.first += Move * dist;\n now.second += Move * dist;\n cur = convex_cut(cur, now);\n }\n if (cur.size() >= 2)\n return true;\n return false;\n}\n\nvoid solve()\n{\n int n;\n cin >> n;\n if(n == 0)\n exit(0);\n Polygon poly;\n REP(i,n){\n ld a,b;\n cin >> a >> b;\n poly.push_back({a, b});\n }\n\n ld bot = 0;\n ld top = 1e9;\n REP(t,100){\n ld mid = (bot + top) / 2.0;\n if(judge(mid,poly)){\n bot = mid;\n }else{\n top = mid;\n }\n }\n cout << bot << endl;\n}\n#undef int\n\n// generated by oj-template v4.7.2\n// (https://github.com/online-judge-tools/template-generator)\nint main()\n{\n // Fasterize input/output script\n ios::sync_with_stdio(false);\n cin.tie(nullptr);\n cout << fixed << setprecision(100);\n // scanf/printf user should delete this fasterize input/output script\n\n int t = 10000;\n // cin >> t; // comment out if solving multi testcase\n for (int testCase = 1; testCase <= t; ++testCase)\n {\n solve();\n }\n return 0;\n}", "accuracy": 1, "time_ms": 40, "memory_kb": 3604, "score_of_the_acc": -0.7555, "final_rank": 9 }, { "submission_id": "aoj_1283_6264069", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\n// base\nnamespace geometry {\n using namespace std;\n using real_number = long double;\n\n const real_number PI = acosl(-1);\n\n inline static real_number &eps() {\n static real_number EPS = 1e-10;\n return EPS;\n }\n\n static void set_eps(real_number EPS) {\n eps() = EPS;\n }\n\n inline int sign(real_number r) {\n if (r < -eps()) return -1;\n if (r > +eps()) return +1;\n return 0;\n }\n\n inline bool equals(real_number r1, real_number r2) {\n return sign(r1 - r2) == 0;\n }\n}\n\n// point\nnamespace geometry {\n using point = complex< real_number >;\n using points = vector< point >;\n\n point operator*(const point &p, const real_number &k) {\n return point(p.real() * k, p.imag() * k);\n }\n\n point rotate(const real_number &theta, const point &p) {\n return point(cos(theta) * p.real() + sin(-theta) * p.imag(),\n sin(theta) * p.real() + cos(-theta) * p.imag());\n }\n\n bool equals(const point &a, const point &b) {\n return equals(a.real(), b.real()) and equals(a.imag(), b.imag());\n }\n}\n\nistream &operator>>(istream &is, geometry::point &p) {\n geometry::real_number x, y;\n is >> x >> y;\n p = geometry::point(x, y);\n return is;\n}\n\nostream &operator<<(ostream &os, const geometry::point &p) {\n return os << p.real() << \" \" << p.imag();\n}\n\n// polygon\nnamespace geometry {\n using polygon = vector< point >;\n using polygons = vector< polygon >;\n}\n\n// line\nnamespace geometry {\n struct line {\n point a, b;\n\n line() = default;\n line(point a, point b) : a(a), b(b) {}\n };\n\n using lines = vector< line >;\n}\n\n// product\nnamespace geometry {\n real_number cross(const point &a, const point &b) {\n return a.real() * b.imag() - a.imag() * b.real();\n }\n\n real_number dot(const point &a, const point &b) {\n return a.real() * b.real() + a.imag() * b.imag();\n }\n}\n\n// area\nnamespace geometry {\n real_number area(const polygon &poly) {\n int n = poly.size();\n real_number res = 0;\n for (int i = 0; i < n; i++) {\n res += cross(poly[i], poly[(i + 1) % n]);\n }\n return res / 2;\n }\n}\n\n// convex cut\nnamespace geometry {\n polygon convex_cut(const polygon &poly, const line &l) {\n polygon res;\n int n = poly.size();\n for (int i = 0; i < n; i++) {\n int j = (i + 1 == n ? 0 : i + 1);\n\n real_number cf = cross(l.a - poly[i], l.b - poly[i]);\n real_number cs = cross(l.a - poly[j], l.b - poly[j]);\n\n if (sign(cf) >= 0) res.emplace_back(poly[i]);\n if (sign(cf) * sign(cs) < 0) {\n real_number s = cross(poly[j] - poly[i], l.a - l.b);\n real_number t = cross(l.a - poly[i], l.a - l.b);\n res.emplace_back(poly[i] + t / s * (poly[j] - poly[i]));\n }\n }\n\n return res;\n }\n}\n\nvoid solve(int n) {\n geometry::polygon poly(n);\n for (auto &pts : poly) cin >> pts;\n\n auto check = [&](geometry::real_number d) {\n geometry::polygon cut = poly;\n \n for (int i = 0; i < n; i++) {\n using geometry::point;\n point vec = poly[(i+1) % n] - poly[i];\n point orth = geometry::rotate(geometry::PI / 2, vec);\n point dv = orth / abs(orth) * d;\n\n geometry::line l(poly[i] + dv ,poly[(i+1) % n] + dv);\n cut = convex_cut(cut, l);\n }\n\n return not(geometry::equals(geometry::area(cut), 0));\n };\n\n geometry::real_number ok = 0, ng = 10000;\n for (int qi = 0; qi < 300; qi++) {\n geometry::real_number mid = (ok + ng) / 2;\n\n if (check(mid)) ok = mid;\n else ng = mid;\n }\n\n cout << fixed << setprecision(7);\n cout << ok << endl;\n}\n\nint main() {\n int n;\n\n while (cin >> n, n) {\n solve(n);\n }\n}", "accuracy": 1, "time_ms": 90, "memory_kb": 3768, "score_of_the_acc": -0.8997, "final_rank": 11 }, { "submission_id": "aoj_1283_6050716", "code_snippet": "#include <bits/stdc++.h>\n\nusing namespace std;\n\n#define eps 1e-9\n#define INF 2000000000 // 2e9\n#define LLINF 2000000000000000000ll // 2e18 (llmax:9e18)\n#define all(x) (x).begin(), (x).end()\n#define sq(x) ((x) * (x))\n\n#define rep(i, x) for (int i = 0; i < (int)(x); i++)\n#define drep(i, x) for (int i = ((int)(x)-1); i >= 0; i--)\n\n#define popcount __builtin_popcount\n#define next __next\n#define prev __prev\n\n#ifndef LOCAL\n#define dmp(...) ;\n#else\n#define dmp(...) \\\n cerr << \"[ \" << #__VA_ARGS__ << \" ] : \" << dump_str(__VA_ARGS__) << endl\n#endif\n\n// ---------------- Utility ------------------\n\ntemplate <class T>\nbool chmin(T &a, const T &b) {\n if (a <= b) return false;\n a = b;\n return true;\n}\n\ntemplate <class T>\nbool chmax(T &a, const T &b) {\n if (a >= b) return false;\n a = b;\n return true;\n}\n\ntemplate <class T>\nusing MaxHeap = priority_queue<T>;\n\ntemplate <class T>\nusing MinHeap = priority_queue<T, vector<T>, greater<T>>;\n\ntemplate <class T>\nvector<T> vect(int len, T elem) {\n return vector<T>(len, elem);\n}\n\n// ----------------- Input -------------------\n\ntemplate <class T, class U>\nistream &operator>>(istream &is, pair<T, U> &p) {\n return is >> p.first >> p.second;\n}\n\ntemplate <class T>\nistream &operator>>(istream &is, vector<T> &vec) {\n for (int i = 0; i < vec.size(); i++) is >> vec[i];\n return is;\n}\n\n// ----------------- Output ------------------\n\ntemplate <class T, class U>\nostream &operator<<(ostream &os, const pair<T, U> &p) {\n return os << p.first << ',' << p.second;\n}\n\ntemplate <class T>\nostream &operator<<(ostream &os, const vector<T> &v) {\n for (const T &e : v) os << e << \" \";\n return os;\n}\n\ntemplate <class T>\nostream &operator<<(ostream &os, const deque<T> &d) {\n for (const T &e : d) os << e << \" \";\n return os;\n}\n\ntemplate <class T>\nostream &operator<<(ostream &os, const set<T> &s) {\n os << \"{ \";\n for (const T &e : s) os << e << \" \";\n return os << \"}\";\n}\n\ntemplate <class T, class U>\nostream &operator<<(ostream &os, const map<T, U> &m) {\n os << \"{ \";\n for (const auto &[key, val] : m) os << \"( \" << key << \" -> \" << val << \" ) \";\n return os << \"}\";\n}\n\ntemplate <class TupleTy, size_t... I>\nvoid dump_tuple(ostream &os, const TupleTy t, std::index_sequence<I...>) {\n (..., (os << (I == 0 ? \"\" : \",\") << std::get<I>(t)));\n}\n\ntemplate <class... Args>\nostream &operator<<(ostream &os, const tuple<Args...> &t) {\n os << \"(\";\n dump_tuple(os, t, std::make_index_sequence<sizeof...(Args)>());\n return os << \")\";\n}\n\nvoid dump_str_rec(ostringstream &) {}\n\ntemplate <class Head, class... Tail>\nvoid dump_str_rec(ostringstream &oss, Head &&head, Tail &&... tail) {\n oss << \", \" << head;\n dump_str_rec(oss, forward<Tail>(tail)...);\n}\n\ntemplate <class T, class... U>\nstring dump_str(T &&arg, U &&... args) {\n ostringstream oss;\n oss << arg;\n dump_str_rec(oss, forward<U>(args)...);\n return oss.str();\n}\n\n// --------------- Fast I/O ------------------\n\nvoid fastio() {\n cin.tie(0);\n ios::sync_with_stdio(0);\n cout << fixed << setprecision(20);\n}\n\n// ------------------ ACL --------------------\n\n// #include <atcoder/modint>\n// constexpr int MOD = 998244353;\n// constexpr int MOD = 1000000007;\n// using mint = atcoder::static_modint<MOD>;\n// ostream &operator<<(ostream &os, const mint &v) {\n// return os << v.val();\n// }\n\n// ------------ End of template --------------\n\n#define endl \"\\n\"\nusing ll = long long;\nusing pii = pair<int, int>;\n\nconst bool multitest = false;\n\n// Geometry Library\n// written by okuraofvegetable\n\n#define pb push_back\n#define DOUBLE_INF 1e50\n#define Points vector<Point>\n#define double long double\n\n#define EQ(a, b) (abs((a) - (b)) < eps)\n#define GT(a, b) ((a) > (b))\n#define GE(a, b) (GT(a, b) || EQ(a, b))\n#define LT(a, b) ((a) < (b))\n#define LE(a, b) (LT(a, b) || EQ(a, b))\n\ninline double add(double a, double b) {\n if (abs(a + b) < eps * (abs(a) + abs(b))) return 0;\n return a + b;\n}\n\n// --------------------- Point -----------------------\n\n#define Vector Point\nstruct Point {\n double x, y;\n Point() {}\n Point(double x, double y) : x(x), y(y) {}\n Point operator+(Point p) { return Point(add(x, p.x), add(y, p.y)); }\n Point operator-(Point p) { return Point(add(x, -p.x), add(y, -p.y)); }\n Point operator*(double d) { return Point(x * d, y * d); }\n Point operator/(double d) { return Point(x / d, y / d); }\n double dot(Point p) { return add(x * p.x, y * p.y); }\n double det(Point p) { return add(x * p.y, -y * p.x); }\n double norm() { return sqrt(x * x + y * y); }\n double norm2() { return x * x + y * y; }\n double dist(Point p) { return ((*this) - p).norm(); }\n double dist2(Point p) { return sq(x - p.x) + sq(y - p.y); }\n double arg() { return atan2(y, x); } // [-PI,PI]\n double arg(Vector v) { return v.arg() - arg(); } // henkaku\n double angle(Vector v) { // [0,PI]\n return acos(cos(arg(v)));\n }\n Vector normalize() { return (*this) * (1.0 / norm()); }\n Point vert() { return Point(-y, x); }\n\n // Signed area of triange (0,0) (x,y) (p.x,p.y)\n double area(Point p) { return (x * p.y - p.x * y) / 2.0; }\n friend istream &operator>>(istream &is, Point &p) { return is >> p.x >> p.y; }\n friend ostream &operator<<(ostream &os, const Point &p) {\n return os << fixed << setprecision(10) << '(' << p.x << ',' << p.y << ')';\n }\n};\n\nPoint divide_rate(Point a, Point b, double A, double B) {\n assert(!EQ(A + B, 0.0));\n return (a * B + b * A) * (1.0 / (A + B));\n}\n\nVector polar(double len, double arg) {\n return Vector(cos(arg) * len, sin(arg) * len);\n}\n\n// Direction a -> b -> c\n// verified AOJ CGL_1_C\nenum { COUNTER_CLOCKWISE, CLOCKWISE, ONLINE_BACK, ONLINE_FRONT, ON_SEGMENT };\nint ccw(Point a, Point b, Point c) {\n Vector p = b - a;\n Vector q = c - a;\n if (p.det(q) > 0.0) return COUNTER_CLOCKWISE; // counter clockwise\n if (p.det(q) < 0.0) return CLOCKWISE; // clockwise\n if (p.dot(q) < 0.0) return ONLINE_BACK; // c--a--b online_back\n if (p.norm() < q.norm()) return ONLINE_FRONT; // a--b--c online_front\n return ON_SEGMENT; // a--c--b on_segment\n}\n\n// --------------------- Line ------------------------\nstruct Line {\n Point a, b;\n Line() {}\n Line(Point a, Point b) : a(a), b(b) {}\n bool on(Point q) { return EQ((a - q).det(b - q), 0.0); }\n // following 2 functions verified AOJ CGL_2_A\n bool is_parallel(Line l) { return EQ((a - b).det(l.a - l.b), 0.0); }\n bool is_orthogonal(Line l) { return EQ((a - b).dot(l.a - l.b), 0.0); }\n Point intersection(Line l) {\n assert(!is_parallel(l));\n return a + (b - a) * ((l.b - l.a).det(l.a - a) / (l.b - l.a).det(b - a));\n }\n // Projection of p to this line\n // verified AOJ CGL_1_A\n Point projection(Point p) {\n return (b - a) * ((b - a).dot(p - a) / (b - a).norm2()) + a;\n }\n double distance(Point p) {\n Point q = projection(p);\n return p.dist(q);\n }\n // Reflection point of p onto this line\n // verified AOJ CGL_1_B\n Point refl(Point p) {\n Point proj = projection(p);\n return p + ((proj - p) * 2.0);\n }\n bool left(Point p) {\n if ((p - a).det(b - a) > 0.0)\n return true;\n else\n return false;\n }\n friend istream &operator>>(istream &is, Line &l) { return is >> l.a >> l.b; }\n friend ostream &operator<<(ostream &os, const Line &l) {\n return os << l.a << \" -> \" << l.b;\n }\n};\n\n// ------------------- Segment ----------------------\nstruct Segment {\n Point a, b;\n Segment() {}\n Segment(Point a, Point b) : a(a), b(b) {}\n Line line() { return Line(a, b); }\n bool on(Point q) {\n return (EQ((a - q).det(b - q), 0.0) && LE((a - q).dot(b - q), 0.0));\n }\n // verified AOJ CGL_2_B\n bool is_intersect(Segment s) {\n if (line().is_parallel(s.line())) {\n if (on(s.a) || on(s.b)) return true;\n if (s.on(a) || s.on(b)) return true;\n return false;\n }\n Point p = line().intersection(s.line());\n if (on(p) && s.on(p))\n return true;\n else\n return false;\n }\n bool is_intersect(Line l) {\n if (line().is_parallel(l)) {\n if (l.on(a) || l.on(b))\n return true;\n else\n return false;\n }\n Point p = line().intersection(l);\n if (on(p))\n return true;\n else\n return false;\n }\n // following 2 distance functions are verified at AOJ CGL_2_D\n double distance(Point p) {\n double res = DOUBLE_INF;\n Point q = line().projection(p);\n if (on(q)) res = min(res, p.dist(q));\n res = min(res, min(p.dist(a), p.dist(b)));\n return res;\n }\n double distance(Segment s) {\n if (is_intersect(s)) return 0.0;\n double res = DOUBLE_INF;\n res = min(res, s.distance(a));\n res = min(res, s.distance(b));\n res = min(res, this->distance(s.a));\n res = min(res, this->distance(s.b));\n return res;\n }\n friend istream &operator>>(istream &is, Segment &s) {\n return is >> s.a >> s.b;\n }\n friend ostream &operator<<(ostream &os, const Segment &s) {\n return os << s.a << \" -> \" << s.b;\n }\n};\n\n// ------------------- Polygon -----------------------\n// Note: A polygon generally don't need to be convex.\n\ntypedef vector<Point> Polygon;\n// verified AOJ CGL_3_A\ndouble area(Polygon &pol) {\n Points vec;\n double res = 0.0;\n int M = pol.size();\n for (int i = 0; i < M; i++) {\n res += (pol[i] - pol[0]).area(pol[(i + 1) % M] - pol[0]);\n }\n return res;\n}\n\nbool is_convex(Polygon &pol) {\n int n = pol.size();\n for (int i = 0; i < n - 1; i++) {\n if (ccw(pol[i], pol[i + 1], pol[(i + 2) % n]) == CLOCKWISE) {\n return false;\n }\n }\n return true;\n}\n\n// vecrified AOJ CGL_3_C\nenum { OUT, ON, IN };\nint contained(Polygon &pol, Point p) {\n int n = pol.size();\n Point outer(1e9, p.y);\n int cnt = 0;\n for (int i = 0; i < n; i++) {\n Segment e = Segment(pol[i], pol[(i + 1) % n]);\n if (e.on(p)) return ON;\n Vector a = pol[i] - p;\n Vector b = pol[(i + 1) % n] - p;\n if (a.y > b.y) swap(a, b);\n if (LE(a.y, 0.0) && GT(b.y, 0.0)) {\n if (LT(a.det(b), 0.0)) cnt++;\n }\n }\n if ((cnt & 1) == 1)\n return IN;\n else\n return OUT;\n}\n\n// Cut conovex polygon by a line and return left polygon\n// verified AOJ CGL_4_C\nPolygon convex_cut(Polygon &cv, Line l) {\n int n = cv.size();\n Polygon left;\n for (int i = 0; i < n; i++) {\n Segment e = Segment(cv[i], cv[(i + 1) % n]);\n if (ccw(l.a, l.b, cv[i]) != CLOCKWISE) left.pb(cv[i]);\n if (e.is_intersect(l)) {\n if (!e.line().is_parallel(l)) { left.pb(e.line().intersection(l)); }\n }\n }\n return left;\n}\n\nbool solve() {\n int N;\n cin >> N;\n\n if (N == 0) return false;\n\n vector<Point> ps(N);\n for (int i = 0; i < N; i++) cin >> ps[i];\n\n auto check = [&](double K) {\n vector<Line> ls;\n for (int i = 0; i < N; i++) {\n Point a = ps[i], b = ps[(i + 1) % N];\n Vector d = (b - a).vert().normalize() * K;\n ls.emplace_back(a + d, b + d);\n }\n vector<Point> cand;\n for (int i = 0; i < ls.size(); i++) {\n for (int j = i + 1; j < ls.size(); j++) {\n if (ls[i].is_parallel(ls[j])) continue;\n cand.push_back(ls[i].intersection(ls[j]));\n }\n }\n dmp(K);\n dmp(ls);\n dmp(cand);\n for (auto c : cand) {\n if (contained(ps, c) == OUT) continue;\n bool valid = true;\n for (int i = 0; i < N; i++) {\n Point a = ps[i], b = ps[(i + 1) % N];\n dmp(Segment(a, b).distance(c), K);\n if (GE(Segment(a, b).distance(c), K)) {\n continue;\n } else {\n valid = false;\n break;\n }\n }\n if (valid) return true;\n }\n return false;\n };\n\n double l = 0.0, r = 10000.0;\n for (int i = 0; i < 50; i++) {\n double mid = (l + r) / 2.0;\n bool res = check(mid);\n dmp(mid, res);\n if (res)\n l = mid;\n else\n r = mid;\n }\n\n cout << l << endl;\n\n return true;\n}\n\nint main() {\n fastio();\n if (!multitest) {\n while (solve()) {}\n } else {\n cerr << \"[Warning] Multi testcase mode on\" << endl;\n int t;\n cin >> t;\n while (t--) solve();\n }\n return 0;\n}", "accuracy": 1, "time_ms": 1860, "memory_kb": 3808, "score_of_the_acc": -1.3987, "final_rank": 19 }, { "submission_id": "aoj_1283_6050714", "code_snippet": "#include <bits/stdc++.h>\n\nusing namespace std;\n\n#define eps 1e-7\n#define INF 2000000000 // 2e9\n#define LLINF 2000000000000000000ll // 2e18 (llmax:9e18)\n#define all(x) (x).begin(), (x).end()\n#define sq(x) ((x) * (x))\n\n#define rep(i, x) for (int i = 0; i < (int)(x); i++)\n#define drep(i, x) for (int i = ((int)(x)-1); i >= 0; i--)\n\n#define popcount __builtin_popcount\n#define next __next\n#define prev __prev\n\n#ifndef LOCAL\n#define dmp(...) ;\n#else\n#define dmp(...) \\\n cerr << \"[ \" << #__VA_ARGS__ << \" ] : \" << dump_str(__VA_ARGS__) << endl\n#endif\n\n// ---------------- Utility ------------------\n\ntemplate <class T>\nbool chmin(T &a, const T &b) {\n if (a <= b) return false;\n a = b;\n return true;\n}\n\ntemplate <class T>\nbool chmax(T &a, const T &b) {\n if (a >= b) return false;\n a = b;\n return true;\n}\n\ntemplate <class T>\nusing MaxHeap = priority_queue<T>;\n\ntemplate <class T>\nusing MinHeap = priority_queue<T, vector<T>, greater<T>>;\n\ntemplate <class T>\nvector<T> vect(int len, T elem) {\n return vector<T>(len, elem);\n}\n\n// ----------------- Input -------------------\n\ntemplate <class T, class U>\nistream &operator>>(istream &is, pair<T, U> &p) {\n return is >> p.first >> p.second;\n}\n\ntemplate <class T>\nistream &operator>>(istream &is, vector<T> &vec) {\n for (int i = 0; i < vec.size(); i++) is >> vec[i];\n return is;\n}\n\n// ----------------- Output ------------------\n\ntemplate <class T, class U>\nostream &operator<<(ostream &os, const pair<T, U> &p) {\n return os << p.first << ',' << p.second;\n}\n\ntemplate <class T>\nostream &operator<<(ostream &os, const vector<T> &v) {\n for (const T &e : v) os << e << \" \";\n return os;\n}\n\ntemplate <class T>\nostream &operator<<(ostream &os, const deque<T> &d) {\n for (const T &e : d) os << e << \" \";\n return os;\n}\n\ntemplate <class T>\nostream &operator<<(ostream &os, const set<T> &s) {\n os << \"{ \";\n for (const T &e : s) os << e << \" \";\n return os << \"}\";\n}\n\ntemplate <class T, class U>\nostream &operator<<(ostream &os, const map<T, U> &m) {\n os << \"{ \";\n for (const auto &[key, val] : m) os << \"( \" << key << \" -> \" << val << \" ) \";\n return os << \"}\";\n}\n\ntemplate <class TupleTy, size_t... I>\nvoid dump_tuple(ostream &os, const TupleTy t, std::index_sequence<I...>) {\n (..., (os << (I == 0 ? \"\" : \",\") << std::get<I>(t)));\n}\n\ntemplate <class... Args>\nostream &operator<<(ostream &os, const tuple<Args...> &t) {\n os << \"(\";\n dump_tuple(os, t, std::make_index_sequence<sizeof...(Args)>());\n return os << \")\";\n}\n\nvoid dump_str_rec(ostringstream &) {}\n\ntemplate <class Head, class... Tail>\nvoid dump_str_rec(ostringstream &oss, Head &&head, Tail &&... tail) {\n oss << \", \" << head;\n dump_str_rec(oss, forward<Tail>(tail)...);\n}\n\ntemplate <class T, class... U>\nstring dump_str(T &&arg, U &&... args) {\n ostringstream oss;\n oss << arg;\n dump_str_rec(oss, forward<U>(args)...);\n return oss.str();\n}\n\n// --------------- Fast I/O ------------------\n\nvoid fastio() {\n cin.tie(0);\n ios::sync_with_stdio(0);\n cout << fixed << setprecision(20);\n}\n\n// ------------------ ACL --------------------\n\n// #include <atcoder/modint>\n// constexpr int MOD = 998244353;\n// constexpr int MOD = 1000000007;\n// using mint = atcoder::static_modint<MOD>;\n// ostream &operator<<(ostream &os, const mint &v) {\n// return os << v.val();\n// }\n\n// ------------ End of template --------------\n\n#define endl \"\\n\"\nusing ll = long long;\nusing pii = pair<int, int>;\n\nconst bool multitest = false;\n\n// Geometry Library\n// written by okuraofvegetable\n\n#define pb push_back\n#define DOUBLE_INF 1e50\n#define Points vector<Point>\n#define double long double\n\n#define EQ(a, b) (abs((a) - (b)) < eps)\n#define GT(a, b) ((a) > (b))\n#define GE(a, b) (GT(a, b) || EQ(a, b))\n#define LT(a, b) ((a) < (b))\n#define LE(a, b) (LT(a, b) || EQ(a, b))\n\ninline double add(double a, double b) {\n if (abs(a + b) < eps * (abs(a) + abs(b))) return 0;\n return a + b;\n}\n\n// --------------------- Point -----------------------\n\n#define Vector Point\nstruct Point {\n double x, y;\n Point() {}\n Point(double x, double y) : x(x), y(y) {}\n Point operator+(Point p) { return Point(add(x, p.x), add(y, p.y)); }\n Point operator-(Point p) { return Point(add(x, -p.x), add(y, -p.y)); }\n Point operator*(double d) { return Point(x * d, y * d); }\n Point operator/(double d) { return Point(x / d, y / d); }\n double dot(Point p) { return add(x * p.x, y * p.y); }\n double det(Point p) { return add(x * p.y, -y * p.x); }\n double norm() { return sqrt(x * x + y * y); }\n double norm2() { return x * x + y * y; }\n double dist(Point p) { return ((*this) - p).norm(); }\n double dist2(Point p) { return sq(x - p.x) + sq(y - p.y); }\n double arg() { return atan2(y, x); } // [-PI,PI]\n double arg(Vector v) { return v.arg() - arg(); } // henkaku\n double angle(Vector v) { // [0,PI]\n return acos(cos(arg(v)));\n }\n Vector normalize() { return (*this) * (1.0 / norm()); }\n Point vert() { return Point(-y, x); }\n\n // Signed area of triange (0,0) (x,y) (p.x,p.y)\n double area(Point p) { return (x * p.y - p.x * y) / 2.0; }\n friend istream &operator>>(istream &is, Point &p) { return is >> p.x >> p.y; }\n friend ostream &operator<<(ostream &os, const Point &p) {\n return os << fixed << setprecision(10) << '(' << p.x << ',' << p.y << ')';\n }\n};\n\nPoint divide_rate(Point a, Point b, double A, double B) {\n assert(!EQ(A + B, 0.0));\n return (a * B + b * A) * (1.0 / (A + B));\n}\n\nVector polar(double len, double arg) {\n return Vector(cos(arg) * len, sin(arg) * len);\n}\n\n// Direction a -> b -> c\n// verified AOJ CGL_1_C\nenum { COUNTER_CLOCKWISE, CLOCKWISE, ONLINE_BACK, ONLINE_FRONT, ON_SEGMENT };\nint ccw(Point a, Point b, Point c) {\n Vector p = b - a;\n Vector q = c - a;\n if (p.det(q) > 0.0) return COUNTER_CLOCKWISE; // counter clockwise\n if (p.det(q) < 0.0) return CLOCKWISE; // clockwise\n if (p.dot(q) < 0.0) return ONLINE_BACK; // c--a--b online_back\n if (p.norm() < q.norm()) return ONLINE_FRONT; // a--b--c online_front\n return ON_SEGMENT; // a--c--b on_segment\n}\n\n// --------------------- Line ------------------------\nstruct Line {\n Point a, b;\n Line() {}\n Line(Point a, Point b) : a(a), b(b) {}\n bool on(Point q) { return EQ((a - q).det(b - q), 0.0); }\n // following 2 functions verified AOJ CGL_2_A\n bool is_parallel(Line l) { return EQ((a - b).det(l.a - l.b), 0.0); }\n bool is_orthogonal(Line l) { return EQ((a - b).dot(l.a - l.b), 0.0); }\n Point intersection(Line l) {\n assert(!is_parallel(l));\n return a + (b - a) * ((l.b - l.a).det(l.a - a) / (l.b - l.a).det(b - a));\n }\n // Projection of p to this line\n // verified AOJ CGL_1_A\n Point projection(Point p) {\n return (b - a) * ((b - a).dot(p - a) / (b - a).norm2()) + a;\n }\n double distance(Point p) {\n Point q = projection(p);\n return p.dist(q);\n }\n // Reflection point of p onto this line\n // verified AOJ CGL_1_B\n Point refl(Point p) {\n Point proj = projection(p);\n return p + ((proj - p) * 2.0);\n }\n bool left(Point p) {\n if ((p - a).det(b - a) > 0.0)\n return true;\n else\n return false;\n }\n friend istream &operator>>(istream &is, Line &l) { return is >> l.a >> l.b; }\n friend ostream &operator<<(ostream &os, const Line &l) {\n return os << l.a << \" -> \" << l.b;\n }\n};\n\n// ------------------- Segment ----------------------\nstruct Segment {\n Point a, b;\n Segment() {}\n Segment(Point a, Point b) : a(a), b(b) {}\n Line line() { return Line(a, b); }\n bool on(Point q) {\n return (EQ((a - q).det(b - q), 0.0) && LE((a - q).dot(b - q), 0.0));\n }\n // verified AOJ CGL_2_B\n bool is_intersect(Segment s) {\n if (line().is_parallel(s.line())) {\n if (on(s.a) || on(s.b)) return true;\n if (s.on(a) || s.on(b)) return true;\n return false;\n }\n Point p = line().intersection(s.line());\n if (on(p) && s.on(p))\n return true;\n else\n return false;\n }\n bool is_intersect(Line l) {\n if (line().is_parallel(l)) {\n if (l.on(a) || l.on(b))\n return true;\n else\n return false;\n }\n Point p = line().intersection(l);\n if (on(p))\n return true;\n else\n return false;\n }\n // following 2 distance functions are verified at AOJ CGL_2_D\n double distance(Point p) {\n double res = DOUBLE_INF;\n Point q = line().projection(p);\n if (on(q)) res = min(res, p.dist(q));\n res = min(res, min(p.dist(a), p.dist(b)));\n return res;\n }\n double distance(Segment s) {\n if (is_intersect(s)) return 0.0;\n double res = DOUBLE_INF;\n res = min(res, s.distance(a));\n res = min(res, s.distance(b));\n res = min(res, this->distance(s.a));\n res = min(res, this->distance(s.b));\n return res;\n }\n friend istream &operator>>(istream &is, Segment &s) {\n return is >> s.a >> s.b;\n }\n friend ostream &operator<<(ostream &os, const Segment &s) {\n return os << s.a << \" -> \" << s.b;\n }\n};\n\n// ------------------- Polygon -----------------------\n// Note: A polygon generally don't need to be convex.\n\ntypedef vector<Point> Polygon;\n// verified AOJ CGL_3_A\ndouble area(Polygon &pol) {\n Points vec;\n double res = 0.0;\n int M = pol.size();\n for (int i = 0; i < M; i++) {\n res += (pol[i] - pol[0]).area(pol[(i + 1) % M] - pol[0]);\n }\n return res;\n}\n\nbool is_convex(Polygon &pol) {\n int n = pol.size();\n for (int i = 0; i < n - 1; i++) {\n if (ccw(pol[i], pol[i + 1], pol[(i + 2) % n]) == CLOCKWISE) {\n return false;\n }\n }\n return true;\n}\n\n// vecrified AOJ CGL_3_C\nenum { OUT, ON, IN };\nint contained(Polygon &pol, Point p) {\n int n = pol.size();\n Point outer(1e9, p.y);\n int cnt = 0;\n for (int i = 0; i < n; i++) {\n Segment e = Segment(pol[i], pol[(i + 1) % n]);\n if (e.on(p)) return ON;\n Vector a = pol[i] - p;\n Vector b = pol[(i + 1) % n] - p;\n if (a.y > b.y) swap(a, b);\n if (LE(a.y, 0.0) && GT(b.y, 0.0)) {\n if (LT(a.det(b), 0.0)) cnt++;\n }\n }\n if ((cnt & 1) == 1)\n return IN;\n else\n return OUT;\n}\n\n// Cut conovex polygon by a line and return left polygon\n// verified AOJ CGL_4_C\nPolygon convex_cut(Polygon &cv, Line l) {\n int n = cv.size();\n Polygon left;\n for (int i = 0; i < n; i++) {\n Segment e = Segment(cv[i], cv[(i + 1) % n]);\n if (ccw(l.a, l.b, cv[i]) != CLOCKWISE) left.pb(cv[i]);\n if (e.is_intersect(l)) {\n if (!e.line().is_parallel(l)) { left.pb(e.line().intersection(l)); }\n }\n }\n return left;\n}\n\nbool solve() {\n int N;\n cin >> N;\n\n if (N == 0) return false;\n\n vector<Point> ps(N);\n for (int i = 0; i < N; i++) cin >> ps[i];\n\n auto check = [&](double K) {\n vector<Line> ls;\n for (int i = 0; i < N; i++) {\n Point a = ps[i], b = ps[(i + 1) % N];\n Vector d = (b - a).vert().normalize() * K;\n ls.emplace_back(a + d, b + d);\n }\n vector<Point> cand;\n for (int i = 0; i < ls.size(); i++) {\n for (int j = i + 1; j < ls.size(); j++) {\n if (ls[i].is_parallel(ls[j])) continue;\n cand.push_back(ls[i].intersection(ls[j]));\n }\n }\n dmp(K);\n dmp(ls);\n dmp(cand);\n for (auto c : cand) {\n if (contained(ps, c) == OUT) continue;\n bool valid = true;\n for (int i = 0; i < N; i++) {\n Point a = ps[i], b = ps[(i + 1) % N];\n dmp(Segment(a, b).distance(c), K);\n if (Segment(a, b).distance(c) < K - eps) {\n valid = false;\n break;\n }\n }\n if (valid) return true;\n }\n return false;\n };\n\n double l = 0.0, r = 10000.0;\n for (int i = 0; i < 50; i++) {\n double mid = (l + r) / 2.0;\n bool res = check(mid);\n dmp(mid, res);\n if (res)\n l = mid;\n else\n r = mid;\n }\n\n cout << l << endl;\n\n return true;\n}\n\nint main() {\n fastio();\n if (!multitest) {\n while (solve()) {}\n } else {\n cerr << \"[Warning] Multi testcase mode on\" << endl;\n int t;\n cin >> t;\n while (t--) solve();\n }\n return 0;\n}", "accuracy": 1, "time_ms": 1560, "memory_kb": 3764, "score_of_the_acc": -1.2844, "final_rank": 18 }, { "submission_id": "aoj_1283_6050710", "code_snippet": "#include <bits/stdc++.h>\n\nusing namespace std;\n\n#define eps 1e-9\n#define INF 2000000000 // 2e9\n#define LLINF 2000000000000000000ll // 2e18 (llmax:9e18)\n#define all(x) (x).begin(), (x).end()\n#define sq(x) ((x) * (x))\n\n#define rep(i, x) for (int i = 0; i < (int)(x); i++)\n#define drep(i, x) for (int i = ((int)(x)-1); i >= 0; i--)\n\n#define popcount __builtin_popcount\n#define next __next\n#define prev __prev\n\n#ifndef LOCAL\n#define dmp(...) ;\n#else\n#define dmp(...) \\\n cerr << \"[ \" << #__VA_ARGS__ << \" ] : \" << dump_str(__VA_ARGS__) << endl\n#endif\n\n// ---------------- Utility ------------------\n\ntemplate <class T>\nbool chmin(T &a, const T &b) {\n if (a <= b) return false;\n a = b;\n return true;\n}\n\ntemplate <class T>\nbool chmax(T &a, const T &b) {\n if (a >= b) return false;\n a = b;\n return true;\n}\n\ntemplate <class T>\nusing MaxHeap = priority_queue<T>;\n\ntemplate <class T>\nusing MinHeap = priority_queue<T, vector<T>, greater<T>>;\n\ntemplate <class T>\nvector<T> vect(int len, T elem) {\n return vector<T>(len, elem);\n}\n\n// ----------------- Input -------------------\n\ntemplate <class T, class U>\nistream &operator>>(istream &is, pair<T, U> &p) {\n return is >> p.first >> p.second;\n}\n\ntemplate <class T>\nistream &operator>>(istream &is, vector<T> &vec) {\n for (int i = 0; i < vec.size(); i++) is >> vec[i];\n return is;\n}\n\n// ----------------- Output ------------------\n\ntemplate <class T, class U>\nostream &operator<<(ostream &os, const pair<T, U> &p) {\n return os << p.first << ',' << p.second;\n}\n\ntemplate <class T>\nostream &operator<<(ostream &os, const vector<T> &v) {\n for (const T &e : v) os << e << \" \";\n return os;\n}\n\ntemplate <class T>\nostream &operator<<(ostream &os, const deque<T> &d) {\n for (const T &e : d) os << e << \" \";\n return os;\n}\n\ntemplate <class T>\nostream &operator<<(ostream &os, const set<T> &s) {\n os << \"{ \";\n for (const T &e : s) os << e << \" \";\n return os << \"}\";\n}\n\ntemplate <class T, class U>\nostream &operator<<(ostream &os, const map<T, U> &m) {\n os << \"{ \";\n for (const auto &[key, val] : m) os << \"( \" << key << \" -> \" << val << \" ) \";\n return os << \"}\";\n}\n\ntemplate <class TupleTy, size_t... I>\nvoid dump_tuple(ostream &os, const TupleTy t, std::index_sequence<I...>) {\n (..., (os << (I == 0 ? \"\" : \",\") << std::get<I>(t)));\n}\n\ntemplate <class... Args>\nostream &operator<<(ostream &os, const tuple<Args...> &t) {\n os << \"(\";\n dump_tuple(os, t, std::make_index_sequence<sizeof...(Args)>());\n return os << \")\";\n}\n\nvoid dump_str_rec(ostringstream &) {}\n\ntemplate <class Head, class... Tail>\nvoid dump_str_rec(ostringstream &oss, Head &&head, Tail &&... tail) {\n oss << \", \" << head;\n dump_str_rec(oss, forward<Tail>(tail)...);\n}\n\ntemplate <class T, class... U>\nstring dump_str(T &&arg, U &&... args) {\n ostringstream oss;\n oss << arg;\n dump_str_rec(oss, forward<U>(args)...);\n return oss.str();\n}\n\n// --------------- Fast I/O ------------------\n\nvoid fastio() {\n cin.tie(0);\n ios::sync_with_stdio(0);\n cout << fixed << setprecision(20);\n}\n\n// ------------------ ACL --------------------\n\n// #include <atcoder/modint>\n// constexpr int MOD = 998244353;\n// constexpr int MOD = 1000000007;\n// using mint = atcoder::static_modint<MOD>;\n// ostream &operator<<(ostream &os, const mint &v) {\n// return os << v.val();\n// }\n\n// ------------ End of template --------------\n\n#define endl \"\\n\"\nusing ll = long long;\nusing pii = pair<int, int>;\n\nconst bool multitest = false;\n\n// Geometry Library\n// written by okuraofvegetable\n\n#define pb push_back\n#define DOUBLE_INF 1e50\n#define Points vector<Point>\n// #define double long double\n\n#define EQ(a, b) (abs((a) - (b)) < eps)\n#define GT(a, b) ((a) > (b))\n#define GE(a, b) (GT(a, b) || EQ(a, b))\n#define LT(a, b) ((a) < (b))\n#define LE(a, b) (LT(a, b) || EQ(a, b))\n\ninline double add(double a, double b) {\n if (abs(a + b) < eps * (abs(a) + abs(b))) return 0;\n return a + b;\n}\n\n// --------------------- Point -----------------------\n\n#define Vector Point\nstruct Point {\n double x, y;\n Point() {}\n Point(double x, double y) : x(x), y(y) {}\n Point operator+(Point p) { return Point(add(x, p.x), add(y, p.y)); }\n Point operator-(Point p) { return Point(add(x, -p.x), add(y, -p.y)); }\n Point operator*(double d) { return Point(x * d, y * d); }\n Point operator/(double d) { return Point(x / d, y / d); }\n double dot(Point p) { return add(x * p.x, y * p.y); }\n double det(Point p) { return add(x * p.y, -y * p.x); }\n double norm() { return sqrt(x * x + y * y); }\n double norm2() { return x * x + y * y; }\n double dist(Point p) { return ((*this) - p).norm(); }\n double dist2(Point p) { return sq(x - p.x) + sq(y - p.y); }\n double arg() { return atan2(y, x); } // [-PI,PI]\n double arg(Vector v) { return v.arg() - arg(); } // henkaku\n double angle(Vector v) { // [0,PI]\n return acos(cos(arg(v)));\n }\n Vector normalize() { return (*this) * (1.0 / norm()); }\n Point vert() { return Point(-y, x); }\n\n // Signed area of triange (0,0) (x,y) (p.x,p.y)\n double area(Point p) { return (x * p.y - p.x * y) / 2.0; }\n friend istream &operator>>(istream &is, Point &p) { return is >> p.x >> p.y; }\n friend ostream &operator<<(ostream &os, const Point &p) {\n return os << '(' << p.x << ',' << p.y << ')';\n }\n};\n\nPoint divide_rate(Point a, Point b, double A, double B) {\n assert(!EQ(A + B, 0.0));\n return (a * B + b * A) * (1.0 / (A + B));\n}\n\nVector polar(double len, double arg) {\n return Vector(cos(arg) * len, sin(arg) * len);\n}\n\n// Direction a -> b -> c\n// verified AOJ CGL_1_C\nenum { COUNTER_CLOCKWISE, CLOCKWISE, ONLINE_BACK, ONLINE_FRONT, ON_SEGMENT };\nint ccw(Point a, Point b, Point c) {\n Vector p = b - a;\n Vector q = c - a;\n if (p.det(q) > 0.0) return COUNTER_CLOCKWISE; // counter clockwise\n if (p.det(q) < 0.0) return CLOCKWISE; // clockwise\n if (p.dot(q) < 0.0) return ONLINE_BACK; // c--a--b online_back\n if (p.norm() < q.norm()) return ONLINE_FRONT; // a--b--c online_front\n return ON_SEGMENT; // a--c--b on_segment\n}\n\n// --------------------- Line ------------------------\nstruct Line {\n Point a, b;\n Line() {}\n Line(Point a, Point b) : a(a), b(b) {}\n bool on(Point q) { return EQ((a - q).det(b - q), 0.0); }\n // following 2 functions verified AOJ CGL_2_A\n bool is_parallel(Line l) { return EQ((a - b).det(l.a - l.b), 0.0); }\n bool is_orthogonal(Line l) { return EQ((a - b).dot(l.a - l.b), 0.0); }\n Point intersection(Line l) {\n assert(!is_parallel(l));\n return a + (b - a) * ((l.b - l.a).det(l.a - a) / (l.b - l.a).det(b - a));\n }\n // Projection of p to this line\n // verified AOJ CGL_1_A\n Point projection(Point p) {\n return (b - a) * ((b - a).dot(p - a) / (b - a).norm2()) + a;\n }\n double distance(Point p) {\n Point q = projection(p);\n return p.dist(q);\n }\n // Reflection point of p onto this line\n // verified AOJ CGL_1_B\n Point refl(Point p) {\n Point proj = projection(p);\n return p + ((proj - p) * 2.0);\n }\n bool left(Point p) {\n if ((p - a).det(b - a) > 0.0)\n return true;\n else\n return false;\n }\n friend istream &operator>>(istream &is, Line &l) { return is >> l.a >> l.b; }\n friend ostream &operator<<(ostream &os, const Line &l) {\n return os << l.a << \" -> \" << l.b;\n }\n};\n\n// ------------------- Segment ----------------------\nstruct Segment {\n Point a, b;\n Segment() {}\n Segment(Point a, Point b) : a(a), b(b) {}\n Line line() { return Line(a, b); }\n bool on(Point q) {\n return (EQ((a - q).det(b - q), 0.0) && LE((a - q).dot(b - q), 0.0));\n }\n // verified AOJ CGL_2_B\n bool is_intersect(Segment s) {\n if (line().is_parallel(s.line())) {\n if (on(s.a) || on(s.b)) return true;\n if (s.on(a) || s.on(b)) return true;\n return false;\n }\n Point p = line().intersection(s.line());\n if (on(p) && s.on(p))\n return true;\n else\n return false;\n }\n bool is_intersect(Line l) {\n if (line().is_parallel(l)) {\n if (l.on(a) || l.on(b))\n return true;\n else\n return false;\n }\n Point p = line().intersection(l);\n if (on(p))\n return true;\n else\n return false;\n }\n // following 2 distance functions are verified at AOJ CGL_2_D\n double distance(Point p) {\n double res = DOUBLE_INF;\n Point q = line().projection(p);\n if (on(q)) res = min(res, p.dist(q));\n res = min(res, min(p.dist(a), p.dist(b)));\n return res;\n }\n double distance(Segment s) {\n if (is_intersect(s)) return 0.0;\n double res = DOUBLE_INF;\n res = min(res, s.distance(a));\n res = min(res, s.distance(b));\n res = min(res, this->distance(s.a));\n res = min(res, this->distance(s.b));\n return res;\n }\n friend istream &operator>>(istream &is, Segment &s) {\n return is >> s.a >> s.b;\n }\n friend ostream &operator<<(ostream &os, const Segment &s) {\n return os << s.a << \" -> \" << s.b;\n }\n};\n\n// ------------------- Polygon -----------------------\n// Note: A polygon generally don't need to be convex.\n\ntypedef vector<Point> Polygon;\n// verified AOJ CGL_3_A\ndouble area(Polygon &pol) {\n Points vec;\n double res = 0.0;\n int M = pol.size();\n for (int i = 0; i < M; i++) {\n res += (pol[i] - pol[0]).area(pol[(i + 1) % M] - pol[0]);\n }\n return res;\n}\n\nbool is_convex(Polygon &pol) {\n int n = pol.size();\n for (int i = 0; i < n - 1; i++) {\n if (ccw(pol[i], pol[i + 1], pol[(i + 2) % n]) == CLOCKWISE) {\n return false;\n }\n }\n return true;\n}\n\n// vecrified AOJ CGL_3_C\nenum { OUT, ON, IN };\nint contained(Polygon &pol, Point p) {\n int n = pol.size();\n Point outer(1e9, p.y);\n int cnt = 0;\n for (int i = 0; i < n; i++) {\n Segment e = Segment(pol[i], pol[(i + 1) % n]);\n if (e.on(p)) return ON;\n Vector a = pol[i] - p;\n Vector b = pol[(i + 1) % n] - p;\n if (a.y > b.y) swap(a, b);\n if (LE(a.y, 0.0) && GT(b.y, 0.0)) {\n if (LT(a.det(b), 0.0)) cnt++;\n }\n }\n if ((cnt & 1) == 1)\n return IN;\n else\n return OUT;\n}\n\n// Cut conovex polygon by a line and return left polygon\n// verified AOJ CGL_4_C\nPolygon convex_cut(Polygon &cv, Line l) {\n int n = cv.size();\n Polygon left;\n for (int i = 0; i < n; i++) {\n Segment e = Segment(cv[i], cv[(i + 1) % n]);\n if (ccw(l.a, l.b, cv[i]) != CLOCKWISE) left.pb(cv[i]);\n if (e.is_intersect(l)) {\n if (!e.line().is_parallel(l)) { left.pb(e.line().intersection(l)); }\n }\n }\n return left;\n}\n\nbool solve() {\n int N;\n cin >> N;\n\n if (N == 0) return false;\n\n vector<Point> ps(N);\n for (int i = 0; i < N; i++) cin >> ps[i];\n\n auto check = [&](double K) {\n vector<Line> ls;\n auto pol = ps;\n for (int i = 0; i < N; i++) {\n Point a = ps[i], b = ps[(i + 1) % N];\n Vector d = (b - a).vert().normalize() * K;\n pol = convex_cut(pol, Line(a + d, b + d));\n }\n // vector<Point> cand;\n // for (int i = 0; i < ls.size(); i++) {\n // for (int j = i + 1; j < ls.size(); j++) {\n // if (ls[i].is_parallel(ls[j])) continue;\n // cand.push_back(ls[i].intersection(ls[j]));\n // }\n // }\n // // dmp(K);\n // // dmp(ls);\n // // dmp(cand);\n // for (auto c : cand) {\n // if (contained(ps, c) == OUT) continue;\n // bool valid = true;\n // for (int i = 0; i < N; i++) {\n // Point a = ps[i], b = ps[(i + 1) % N];\n // if (Segment(a, b).distance(c) < K) {\n // valid = false;\n // break;\n // }\n // }\n // if (valid) return true;\n // }\n return pol.size() > 0;\n };\n\n double l = 0.0, r = 1000000.0;\n for (int i = 0; i < 200; i++) {\n double mid = (l + r) / 2.0;\n if (check(mid))\n l = mid;\n else\n r = mid;\n }\n\n cout << l << endl;\n\n return true;\n}\n\nint main() {\n fastio();\n if (!multitest) {\n while (solve()) {}\n } else {\n cerr << \"[Warning] Multi testcase mode on\" << endl;\n int t;\n cin >> t;\n while (t--) solve();\n }\n return 0;\n}", "accuracy": 1, "time_ms": 70, "memory_kb": 3512, "score_of_the_acc": -0.69, "final_rank": 6 }, { "submission_id": "aoj_1283_6005887", "code_snippet": "#include<bits/stdc++.h> \nusing namespace std;\nusing ll=long long int;\n//using Int=__int128;\n#define ALL(A) A.begin(),A.end()\ntemplate<typename T1,typename T2> bool chmin(T1 &a,T2 b){if(a<=b)return 0; a=b; return 1;}\ntemplate<typename T1,typename T2> bool chmax(T1 &a,T2 b){if(a>=b)return 0; a=b; return 1;}\ntemplate<typename T> constexpr int bitUP(T x,int a){return (x>>a)&1;}\n//→ ↓ ← ↑ \nint dh[4]={0,1,0,-1}, dw[4]={1,0,-1,0};\n//右上から時計回り\n//int dh[8]={-1,0,1,1,1,0,-1,-1}, dw[8]={1,1,1,0,-1,-1,-1,0};\n//long double EPS = 1e-6;\n//long double PI = acos(-1);\n//const ll INF=(1LL<<62);\nconst int MAX=(1<<30);\nconstexpr ll MOD=1000000000+7;\n//constexpr ll MOD=998244353;\n\n\ninline void bin101(){\n ios::sync_with_stdio(false);\n cin.tie(0);\n cout << fixed << setprecision(20);\n}\n\nusing Graph=vector<vector<int>>;\n\ntemplate<typename T >\nstruct edge {\n\tint to;\n\tT cost;\n\tedge()=default;\n\tedge(int to, T cost) : to(to), cost(cost) {}\n\n};\ntemplate<typename T>\nusing WeightGraph=vector<vector<edge<T>>>;\n\n//要素数n 初期値x\ntemplate<typename T>\ninline vector<T> vmake(size_t n,T x){\n\treturn vector<T>(n,x);\n}\n\n//a,b,c,x data[a][b][c] 初期値x\ntemplate<typename... Args>\nauto vmake(size_t n,Args... args){\n\tauto v=vmake(args...);\n\treturn vector<decltype(v)>(n,move(v));\n}\n\n////////////////////////////\n// マクロや型\n////////////////////////////\n\nusing DD=long double;\n\nconst DD EPS=1e-12;\nconst DD INF=1e50;\nconst DD PI=acosl(-1.0);\n#define EQ(a,b) (abs( (a) - (b) )<EPS) //a==b\n#define LS(a,b) ( (a)+EPS<(b) ) //a<b\n#define GR(a,b) ( (a)>(b)+EPS ) //a>b\n#define LE(a,b) ( (a)<(b)+EPS ) //a<=b\n#define GE(a,b) ( (a)>(b)-EPS ) //a>=b\n\n#define X real()\n#define Y imag()\n\n\n//点\nusing Point=complex<DD>;\nistream &operator>>(istream &is, Point &p) {\n DD x,y;\n is>>x>>y;\n p=Point(x,y);\n return is;\n}\n\n//点a→点bの線分\n//aとbが等しい時、色々バグるから注意!\nstruct Segment{\n Point a,b;\n Segment()=default;\n Segment(Point a,Point b) :a(a),b(b){}\n Segment(DD ax,DD ay,DD bx,DD by):a(ax,ay),b(bx,by){}\n Segment(DD r,Point a) :a(a),b(a+polar((DD)1.0,r)){} \n};\nusing Line=Segment;\n\n//円\nstruct Circle{\n Point p;\n DD r;\n Circle()=default;\n Circle(Point p,DD r):p(p),r(r){}\n};\n\nusing Polygon=vector<Point>;\n\n////////////////////////////\n// 基本計算\n////////////////////////////\n\n//度→ラジアン\ninline DD torad(const DD deg){return deg*PI/180;}\n//ラジアン→度\ninline DD todeg(const DD rad){return rad*180/PI;}\n//内積 |a||b|cosθ\ninline DD dot(const Point &a,const Point &b){return (conj(a)*b).X;}\n//外積 |a||b|sinθ\ninline DD cross(const Point &a,const Point &b){return (conj(a)*b).Y;}\n//ベクトルpを反時計回りにtheta(ラジアン)回転\nPoint rotate(const Point &p,const DD theta){return p*Point(cos(theta),sin(theta));}\n\n//余弦定理 cos(角度abc(ラジアン))を返す\n//長さの対応に注意 ab:辺abの長さ\n//verify:https://codeforces.com/gym/102056/problem/F\nDD cosine_formula(const DD ab,const DD bc,const DD ca){\n return (ab*ab+bc*bc-ca*ca)/(2*ab*bc);\n}\n\ninline bool xy_sort(const Point &a,const Point &b){\n if(a.X+EPS<b.X) return true;\n if(EQ(a.X,b.X) && a.Y+EPS<b.Y) return true;\n return false;\n}\ninline bool yx_sort(const Point &a,const Point &b){\n if(a.Y+EPS<b.Y) return true;\n if(EQ(a.Y,b.Y) && a.X+EPS<b.X) return true;\n return false;\n}\nnamespace std{\n inline bool operator<(const Point &a,const Point &b)noexcept{\n return xy_sort(a,b);\n }\n}\n//DDの比較関数\n//map<DD,vector<pii>,DDcom>\nstruct DDcom{\n bool operator()(const DD a, const DD b) const noexcept {\n return a+EPS<b;\n }\n};\n\n\n////////////////////////////\n// 平行や直交\n////////////////////////////\n\nbool isOrthogonal(const Point &a,const Point &b){\n return EQ(dot(a,b),0.0);\n}\n//verify:http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_2_A\nbool isOrthogonal(const Segment &s,const Segment &t){\n return EQ(dot(s.a-s.b,t.a-t.b),0.0);\n}\nbool isParallel(const Point &a,const Point &b){\n return EQ(cross(a,b),0.0);\n}\n//verify:http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_2_A\nbool isParallel(const Segment &s,const Segment &t){\n return EQ(cross(s.a-s.b,t.a-t.b),0);\n}\n//線分a→bに対して点cがどの位置にあるか\n//反時計回り:1 時計回り:-1 直線上(a,b,c:-2 a,c,b:0 c,a,b:2)\n//verify:http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_1_C\n//線分a→bの端点にcがあるなら0と判定されるはず\nint ccw(const Point &a,Point b,Point c){\n if(EQ(abs(b-c),0) or EQ(abs(a-c),0)) return 0;\n b-=a;c-=a;\n if(cross(b,c)>EPS) return 1;\n if(cross(b,c)<-EPS) return -1;\n if(dot(b,c)<-EPS) return 2;\n if(LS(norm(b),norm(c))) return -2;\n return 0;\n}\n\n////////////////////////////\n// 射影と反射\n////////////////////////////\n\n//射影\n//直線lに点pから垂線を引いたときの交点\n//verify:http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_1_A\nPoint project(const Point &p,const Line &l){\n Point base=l.b-l.a;\n DD r=dot(p-l.a,base)/norm(base);\n return l.a+base*r;\n}\n//反射\n//直線lに対して点pと対称な点\n//verify:http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_1_B\nPoint reflect(const Point &p,const Line &l){\n return p+(project(p,l)-p)*(DD)2.0;\n}\n\n////////////////////////////\n// 点との距離\n////////////////////////////\n\n//点と直線の距離\nDD DistPL(const Point &p,const Line &l){\n return abs(cross(l.b-l.a,p-l.a))/abs(l.b-l.a);\n}\n//点と線分の距離\nDD DistPS(const Point &p,const Segment &s){\n if( dot(s.b-s.a,p-s.a)<0.0 ) return abs(p-s.a);\n if( dot(s.a-s.b,p-s.b)<0.0 ) return abs(p-s.b);\n return DistPL(p,s);\n}\n\n////////////////////////////\n// 線分と直線\n////////////////////////////\n\n//線分a-b,c-dは交差するか?\n//接する場合も交差すると判定\n//接する場合は交差しないとするなら<=を<に変換\nbool intersect(const Point &a,const Point &b,const Point &c,const Point &d){\n return(ccw(a,b,c)*ccw(a,b,d)<=0 && ccw(c,d,a)*ccw(c,d,b)<=0);\n}\n//線分s,tは交差するか?\n//接する場合も交差すると判定\n//verify:http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_2_B&lang=ja\n//接する場合は交差しないとするならintersectの<=を<に変換\nbool intersect(const Segment &s,const Segment &t){\n return intersect(s.a,s.b,t.a,t.b);\n}\n// 2直線の交点\n// 線分の場合はintersectをしてから使う\n//verify:http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_2_C&lang=ja\nPoint crossPoint(const Line &s,const Line &t){\n Point base=t.b-t.a;\n DD d1=cross(base,t.a-s.a);\n DD d2=cross(base,s.b-s.a);\n if(EQ(d1,0) and EQ(d2,0)) return s.a; //同じ直線\n if(EQ(d2,0)) assert(false); //交点がない\n return s.a+d1/d2*(s.b-s.a);\n}\n//線分と線分の距離\n//verify:http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_2_D&lang=ja\nDD Dist(const Segment &s,const Segment &t){\n if(intersect(s,t)) return 0.0;\n return min(min(DistPS(t.a,s),DistPS(t.b,s)),min(DistPS(s.a,t),DistPS(s.b,t)) );\n}\nbool issame(const Line &l,const Line &m){\n if (GR(abs(cross(m.a - l.a, l.b - l.a)),0) ) return false;\n if (GR(abs(cross(m.b - l.a, l.b - l.a)),0) ) return false;\n return true;\n}\n\n////////////////////////////\n// 円\n////////////////////////////\n//直線lと円Cの交点\n//l.aにより近い方がindexが小さい\n//veirfy:http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_7_D\nvector<Point> crossPointLC(const Line &l,const Circle &c){\n vector<Point> ret;\n if(GR(DistPL(c.p,l),c.r)) return ret; //交点がないとき、空ベクトルを返す\n Point pr=project(c.p,l);\n Point e=(l.b-l.a)/(abs(l.b-l.a));\n DD base=sqrt(c.r*c.r-norm(pr-c.p));\n ret.push_back(pr-e*base);\n ret.push_back(pr+e*base);\n if(EQ(DistPL(c.p,l),c.r)) ret.pop_back(); //接するとき\n return ret;\n}\n//線分sと円cの交点\n//交点がないときは空ベクトルを返す\n//線分の端が交わる場合も交点とみなす\n//s.aにより近い方がindexが小さい\nvector<Point> crossPointSC(const Segment &s,const Circle &c){\n vector<Point> ret;\n if(DistPS(c.p,s)>c.r+EPS) return ret;\n auto koho=crossPointLC(s,c);\n for(auto p:koho){\n if(ccw(s.a,s.b,p)==0 or EQ(abs(p-s.a),0) or EQ(abs(p-s.b),0)) ret.push_back(p);\n }\n return ret;\n}\n\n//円aと円bの共通接線の数\n//離れている:4 外接:3 交わる:2 内接:1 内包:0\n//verify:http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_7_A\nint intersect(const Circle &a,const Circle &b){\n DD d=abs(a.p-b.p);\n if(GR(d,a.r+b.r)) return 4;\n if(EQ(d,a.r+b.r)) return 3;\n if(EQ(d,abs(a.r-b.r))) return 1;\n if(LS(d,abs(a.r-b.r))) return 0;\n return 2;\n}\n\n//円aと円bの交点\n//交点がないときは空ベクトルを返す\n//verify:http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_7_E\nvector<Point> crossPoint(const Circle &a,const Circle &b){\n vector<Point> ret;\n if(GR(abs(a.p-b.p),a.r+b.r)) return ret;\n DD d=abs(a.p-b.p);\n DD s=acosl((a.r*a.r+d*d-b.r*b.r)/(2*a.r*d)); //0~π\n DD t=arg(b.p-a.p);\n if(EQ(a.r+b.r,d)) ret.push_back(a.p+polar(a.r,t));\n else ret.push_back(a.p+polar(a.r,t+s)),ret.push_back(a.p+polar(a.r,t-s));\n return ret;\n}\n\n//点pから円cに接線を引いたときの接点\n//verify:http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_7_F&lang=ja\nvector<Point> TanLine(const Point &p,const Circle &c){\n vector<Point> ret;\n DD d=abs(p-c.p);\n if(LS(d,c.r)) return ret; //pがcの内部にある場合\n if(EQ(d,c.r)){\n ret.push_back(p);\n return ret; //円の線上にある場合\n } \n return crossPoint(c,Circle(p,sqrt(d*d-c.r*c.r)));\n}\n\n//円a,bの共通接線\n//https://www.slideshare.net/kujira16/ss-35861169\n//Line.aは円aと接線の交点\n//(接線と円bの交点)と(接線と円aの交点)が違う場合はLine.bは(接線と円bの交点)\n//一致する場合は円aの中心からLine.aに向かうベクトルを反時計回りに90度回転したものを\n//Line.aに足したもの\n//verify:http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_7_G\n//verify:http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=2201\nvector<Line> TanLine(const Circle &a,const Circle &b){\n DD d=abs(a.p-b.p);\n vector<Line> ret;\n //外接線\n if(intersect(a,b)>=2){\n if(EQ(a.r,b.r)){\n Point up=polar(a.r,arg(b.p-a.p)+PI/2);\n ret.emplace_back(a.p+up,b.p+up);\n ret.emplace_back(a.p-up,b.p-up);\n }else{\n Point q=(a.p*b.r-b.p*a.r)/(b.r-a.r);\n auto as=TanLine(q,a);\n auto bs=TanLine(q,b);\n assert(as.size()==2 and bs.size()==2);\n for(int i=0;i<2;i++){\n ret.emplace_back(as[i],bs[i]);\n }\n }\n }else if(intersect(a,b)==1){ //内接する場合\n Point dir;\n if(a.r>b.r) dir=polar(a.r,arg(b.p-a.p));\n else dir=polar(a.r,arg(a.p-b.p));\n ret.emplace_back(a.p+dir,a.p+dir+rotate(dir,PI/2));\n }\n //内接線\n if(GR(abs(a.p-b.p),a.r+b.r)){ //円が離れている場合\n Point q=a.p+(b.p-a.p)*a.r/(a.r+b.r);\n auto as=TanLine(q,a);\n auto bs=TanLine(q,b);\n assert(as.size()==2 and bs.size()==2);\n for(int i=0;i<2;i++){\n ret.emplace_back(as[i],bs[i]);\n }\n }else if(EQ(d,a.r+b.r)){ //円が接している場合\n Point dir=polar(a.r,arg(b.p-a.p));\n ret.emplace_back(a.p+dir,a.p+dir+rotate(dir,PI/2));\n }\n return ret;\n}\n//2つの円の共通部分の面積\n//参考: https://tjkendev.github.io/procon-library/python/geometry/circles_intersection_area.html\n//verify: https://judge.u-aizu.ac.jp/onlinejudge/review.jsp?rid=6005736#1\nDD Area(const Circle &c1,const Circle &c2){\n DD cd=abs(c1.p-c2.p);\n if(LE(c1.r+c2.r,cd)) return 0; //円が離れている\n\n if(LE(cd,abs(c1.r-c2.r))) //片方の円が他方を包んでいる\n return min(c1.r,c2.r)*min(c1.r,c2.r)*PI;\n\n DD theta1=acosl(cosine_formula(c1.r,cd,c2.r));\n DD theta2=acosl(cosine_formula(c2.r,cd,c1.r));\n\n return c1.r*c1.r*theta1+c2.r*c2.r*theta2-c1.r*cd*sin(theta1);\n}\n\n\n////////////////////////////\n// 三角形\n////////////////////////////\n\n//ヘロンの公式 三角形の面積を返す\n//三角形が成立するか(平行ではないか)を忘れずに\nDD Heron(const DD ad,const DD bd,const DD cd){\n DD s=(ad+bd+cd)/2;\n return sqrt(s*(s-ad)*(s-bd)*(s-cd));\n}\n//三角形の外接円\n//isParallel()を使って三角形が成立するか(平行ではないか)を忘れずに\n//verify:http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_7_C\nCircle CircumscribedCircle(const Point &a,const Point &b,const Point &c){\n Point bc=(b+c)/(DD)2.0;\n Line aa(bc,bc+rotate(c-b,PI/2));\n Point ab=(a+b)/(DD)2.0;\n Line cc(ab,ab+rotate(b-a,PI/2));\n Point p=crossPoint(aa,cc);\n return Circle(p,abs(a-p));\n}\n//三角形の内接円\n//isParallel()を使って三角形が成立するか(平行ではないか)を忘れずに\n//verify:http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_7_B\nCircle InCircle(const Point &a,const Point &b,const Point &c){\n Line aa(a,a+polar((DD)1.0,(arg(b-a)+arg(c-a))/(DD)2.0));\n Line bb(b,b+polar((DD)1.0,(arg(a-b)+arg(c-b))/(DD)2.0));\n Point p=crossPoint(aa,bb);\n return Circle(p,DistPL(p,Line(a,b)));\n}\n\n////////////////////////////\n// 多角形\n////////////////////////////\n\n//点pが多角形gに対してどの位置にあるか\n//中:2 辺上:1 外:0\n//凸多角形である必要ない\n//verify:http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_3_C\nint contains(const Point &p,const vector<Point> &g){\n int n=(int)g.size();\n int x=false;\n for(int i=0;i<n;i++){\n Point a=g[i]-p,b=g[(i+1)%n]-p;\n if(EQ(cross(a,b),0) && dot(a,b)<EPS) return 1;\n if(a.Y>b.Y) swap(a,b);\n if(a.Y<EPS && EPS<b.Y && cross(a,b)>EPS) x=!x;\n }\n return (x?2:0);\n}\n//凸性判定\n//多角形gは凸か?\n//gは反時計回りでも時計回りでもいい\n//verify:http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_3_B\nbool isConvex(const vector<Point> &g){\n int n=(int)g.size();\n if(n<=3) return true;\n int flag=0;\n int t;\n for(int i=0;i<n;i++){\n Point a(g[(i+1)%n]-g[i]),b(g[(i+2)%n]-g[i]);\n if(cross(a,b)>EPS) t=1;\n else if(cross(a,b)<-EPS) t=-1;\n else continue;\n if(flag==-t) return false;\n flag=t;\n }\n return true;\n}\n\n//凸包 アンドリューのアルゴリズム\n//O(NlogN)\n//反時計回りの多角形を返す\n//verify:http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_4_A\nvector<Point> ConvexHull(vector<Point> s,const bool on_edge){\n int j;\n if(on_edge) j=-1;\n else j=1;\n int sz=(int)s.size();\n if(sz<3) return s;\n sort(s.begin(),s.end(),yx_sort);\n\n int n=0;\n vector<Point> res(2*sz);\n for(int i=0;i<sz;i++){\n while(n>=2 && cross(res[n-1]-res[n-2],s[i]-res[n-2])<EPS*j){\n n--;\n }\n res[n]=s[i];\n n++;\n }\n int t=n+1;\n for(int i=sz-2;i>=0;i--){\n while(n>=t && cross(res[n-1]-res[n-2],s[i]-res[n-2])<EPS*j){\n n--;\n }\n res[n]=s[i];\n n++;\n }\n res.resize(n-1);\n return res;\n}\n\n//多角形g(凸でなくてもいい)の符号付き面積\n//反時計回りの多角形なら正の値を返す 時計回りなら負\n//O(N)\n//https://imagingsolution.net/math/calc_n_point_area/\n//verify:http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_3_A\nDD Area(const vector<Point> &g){\n DD ret=0.0;\n int n=(int)g.size();\n for(int i=0;i<n;i++){\n ret+=cross(g[i],g[(i+1)%n]);\n }\n return ret/2.0;\n}\n\n//凸多角形gを直線lで切断\n//直線の左側(向きを考慮)にできる凸多角形を返す\n//多角形gが反時計回りならば、反時計回りで返す(時計回りなら時計回り)\n//直線上の頂点を含む線分の交点は含めてない\n//verify:http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_4_C\nvector<Point> ConvexCut(const vector<Point> &g,const Line &l){\n vector<Point> ret;\n int gz=(int)g.size();\n for(int i=0;i<gz;i++){\n Point now=g[i],next=g[(i+1)%gz];\n if(ccw(l.a,l.b,now)!=-1) ret.push_back(now);\n if(EQ(DistPL(now,l),0) or EQ(DistPL(next,l),0)) continue;\n if(ccw(l.a,l.b,now)*ccw(l.a,l.b,next)<0){\n ret.push_back(crossPoint(Line(now,next),l));\n }\n }\n return ret;\n}\n//部品\ninline DD calc(const Point &a,const Point &b,const DD &r,const bool triangle){\n if(triangle) return cross(a,b);\n else return r*r*arg(b/a);\n}\n//部品\nDD calcArea(const DD &r,const Point &a,const Point &b){\n if(EQ(abs(a-b),0)) return 0;\n bool ina=(abs(a)<r+EPS);\n bool inb=(abs(b)<r+EPS);\n if(ina && inb) return cross(a,b);\n auto cr=crossPointSC(Segment(a,b),Circle(Point(0,0),r));\n if(cr.empty()) return calc(a,b,r,false);\n auto s=cr[0],t=cr.back();\n return calc(s,t,r,true)+calc(a,s,r,ina)+calc(t,b,r,inb);\n}\n//円と多角形の共通部分の面積\n//多角形が反時計回りならば正の値を返す\n//凸多角形でなくても大丈夫\n//http://drken1215.hatenablog.com/entry/2020/02/02/091000\n//verify:http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_7_H&lang=ja\nDD Area(const Circle &c,const vector<Point> &g){\n DD ret=0.0;\n int gz=g.size();\n if(gz<=2) return ret;\n for(int i=0;i<gz;i++){\n Point a=g[i]-c.p,b=g[(i+1)%gz]-c.p;\n ret+=calcArea(c.r,g[i]-c.p,g[(i+1)%gz]-c.p);\n }\n return ret/2.0;\n}\n\n//点と多角形の距離\n//点が多角形の中にあるなら0\nDD Dist(const Point &a,const vector<Point> &b){\n\tif(b.size()==1) return abs(a-b[0]);\n\tif(contains(a,b)>=1) return 0.0L;\n\tDD ret=INF;\n\tfor(int i=0;i<b.size();i++){\n\t\tchmin(ret,DistPS(a,Segment(b[i],b[(i+1)%b.size()])));\n\t}\n\treturn ret;\n}\n\n//多角形と多角形の距離\n//どちらかが内側に入っていたら0\n//全ての線分の組み合わせの距離を求めて、その最小が答え\n//O(size(a)*size(b))\n//sizeが1の時は点との距離になる\n//verify:http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=2827\nDD Dist(const vector<Point> &a,const vector<Point> &b){\n\tDD ret=INF;\n\tif(a.size()==1) return Dist(a[0],b);\n\tif(b.size()==1) return Dist(b[0],a);\n\n\tif(contains(a[0],b)>=1) return 0.0L;\n\tif(contains(b[0],a)>=1) return 0.0L;\n\n\tfor(int i=0;i<a.size();i++){\n\t\tfor(int j=0;j<b.size();j++){\n\t\t\tSegment sa(a[i],a[(i+1)%a.size()]);\n\t\t\tSegment sb(b[j],b[(j+1)%b.size()]);\n\t\t\tchmin(ret,Dist(sa,sb));\n\t\t}\n\t}\n\treturn ret;\n}\n\n////////////////////////////\n// 最近(遠)点間距離\n////////////////////////////\n//部品\nDD RecClosetPair(const vector<Point>::iterator it,const int n){\n if(n<=1) return INF;\n int m=n/2;\n DD x=it[m].X;\n DD d=min(RecClosetPair(it,m),RecClosetPair(it+m,n-m));\n inplace_merge(it,it+m,it+n,yx_sort);\n vector<Point> v;\n for(int i=0;i<n;i++){\n if(abs(it[i].X-x)>=d) continue;\n for(int j=0;j<v.size();j++){\n DD dy=it[i].Y-v[(int)v.size()-1-j].Y;\n if(dy>=d) break;\n DD dx=it[i].X-v[(int)v.size()-1-j].X;\n d=min(d,sqrt(dx*dx+dy*dy));\n }\n v.push_back(it[i]);\n }\n return d;\n}\n//最近点対の距離\n//点が1つのときINFを返す\n//O(NlogN)\n//verify:http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_5_A\nDD ClosetPair(vector<Point> g){\n sort(g.begin(),g.end(),xy_sort);\n return RecClosetPair(g.begin(),g.size());\n}\n\n//最遠頂点間\n//verify:http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_4_B\nDD Diameter(vector<Point> g){\n g=ConvexHull(g,1);\n int gz=g.size();\n int m=0,M=0;\n for(int i=1;i<gz;i++){\n if(g[i].Y<g[m].Y) m=i;\n if(g[i].Y>g[M].Y) M=i;\n }\n DD ret=0;\n int sm=m,sM=M;\n while(m!=sM || M!=sm){\n ret=max(ret,norm(g[m]-g[M]));\n if(cross(g[(m+1)%gz]-g[m],g[(M+1)%gz]-g[M])<0) m=(m+1)%gz;\n else M=(M+1)%gz;\n }\n return sqrt(ret);\n}\n//動的凸包\n//辺上にある点は含めない\n//verify:https://atcoder.jp/contests/geocon2013/tasks/geocon2013_c\nstruct DynamicConvex{\n\tset<Point> up,down;\n\tDynamicConvex(){}\n \n\tbool isContain(set<Point> &pol,Point p){\n\t\tif(pol.size()==0) return false;\n\t\tif(pol.size()==1){\n\t\t\tPoint q=*begin(pol);\n\t\t\treturn EQ(q.X,p.X) and GE(q.Y,p.Y);\n\t\t}\n\t\tassert(pol.size()>=2);\n auto left=begin(pol);\n auto right=(--end(pol));\n if(LS(p.X,left->X) or GR(p.X,right->X)) return false;\n \n \n auto q1=pol.upper_bound(Point(p.X,INF));\n if(q1==end(pol)){\n return GE(right->Y,p.Y);\n }\n auto q2=q1--; //q1が範囲外になるのは最初に弾いてる\n return GE(cross(p-*q1,*q2-*q1),0.0);\n\t}\n bool isContain(Point p){\n if(not isContain(up,p)) return false;\n if(not isContain(down,Point(p.X,-p.Y))) return false;\n return true;\n }\n \n\tvoid add(set<Point> &pol,Point p){\n\t\tif(pol.size()==0){\n\t\t\tpol.insert(p);\n\t\t\treturn;\n\t\t}\n\t\tif(pol.size()==1){\n Point q=*begin(pol);\n\t\t\tif(EQ(q.X,p.X)){\n pol.clear();\n pol.insert(max(p,q));\n\t\t\t}else{\n\t\t\t\tpol.insert(p);\n\t\t\t}\n return;\n\t\t}\n assert(pol.size()>=2);\n if(isContain(pol,p)) return;\n //left\n auto q1=pol.upper_bound(Point(p.X,INF));\n if(q1!=begin(pol)){\n q1--;\n while(pol.size()>=1 and q1!=begin(pol)){\n auto q2=q1--;\n if(GR(cross(*q2-p,*q1-p),0)) break;\n pol.erase(q2);\n }\n }\n //right\n q1=pol.lower_bound(Point(p.X,-INF));\n if(q1!=end(pol)){\n while(pol.size()>1 and q1!=--end(pol)){\n auto q2=q1++;\n if(GR(cross(*q1-p,*q2-p),0.0)) break;\n pol.erase(q2);\n }\n }\n pol.insert(p);\n\t}\n \n\tvoid add(Point p){\n\t\tadd(up,p);\n\t\tadd(down,Point(p.X,-p.Y)); //upと同じように処理をしたいから\n\t}\n};\n\n//垂直二等分線\n//p-↑-q こんなかんじ\n//voronoiで使う\nLine bisector(const Point &p, const Point &q) {\n Point c=(p+q)/DD(2.0);\n Point v=rotate(q-p,PI/2);\n v/=abs(v);\n return Line(c-v,c+v);\n}\n\n//ボロノイ図\n//pol:全体 ps:点の集合 id:中心となる点の添字 pp:中心となる点\n//計算量: pol.size()*ps.size()\n//verify: https://judge.u-aizu.ac.jp/onlinejudge/review.jsp?rid=6005648#1\nvector<Point> voronoi(Polygon pol,const vector<Point> &ps,int id=-1,const Point pp=Point()){\n Point p;\n if(id!=-1) p=ps[id];\n else p=pp;\n for(size_t i=0;i<ps.size();i++){\n if(i==id) continue;\n Line l=bisector(p,ps[i]);\n pol=ConvexCut(pol,l);\n }\n return pol;\n}\n\n\n\nvoid solve(){\nwhile(true){\n int N; cin>>N;\n if(N==0) return;\n Polygon P(N);\n for(int i=0;i<N;i++) cin>>P[i];\n\n DD ng=0,ok=1e5;\n int T=1000;\n while(T--){\n DD mid=(ok+ng)/2;\n auto pol=P;\n for(int i=0;i<N;i++){\n Point d=rotate(P[(i+1)%N]-P[i],PI/2);\n d/=abs(d);\n d*=mid;\n Line l(P[i]+d,P[(i+1)%N]+d);\n pol=ConvexCut(pol,l);\n }\n if(EQ(Area(pol),EPS) or pol.size()<=2) ok=mid;\n else ng=mid;\n }\n cout<<ok<<endl;\n}\n}\n\nint main(){\n bin101();\n int T=1;\n //cin>>T;\n while(T--) solve();\n}", "accuracy": 1, "time_ms": 3800, "memory_kb": 3768, "score_of_the_acc": -1.8786, "final_rank": 20 }, { "submission_id": "aoj_1283_5974756", "code_snippet": "#pragma GCC optimize(\"Ofast\")\n#include <iostream>\n#include <vector>\n#include <algorithm>\n#include <map>\n#include <queue>\n#include <cstdio>\n#include <ctime>\n#include <assert.h>\n#include <chrono>\n#include <random>\n#include <numeric>\n#include <set>\n#include <deque>\n#include <stack>\n#include <sstream>\n#include <utility>\n#include <cstring>\n#include <unordered_map>\n#include <unordered_set>\n#include <tuple>\n#include <array>\nusing namespace std;\ntypedef long long int ll;\ntypedef unsigned long long ull;\n\nmt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());\nll myRand(ll B) {\n return (ull)rng() % B;\n}\ninline double time() {\n return static_cast<double>(chrono::duration_cast<chrono::nanoseconds>(chrono::steady_clock::now().time_since_epoch()).count()) * 1e-9;\n}\n\ndouble eps = 1e-8;\n\nstruct point{\n double x,y;\n point() {}\n point(double x,double y): x(x), y(y) {}\n point& operator-=(const point &p){\n x -= p.x; y -= p.y;\n return *this;\n }\n point& operator+=(const point &p){\n x += p.x; y += p.y;\n return *this;\n }\n point& operator*=(double d){\n x *= d; y *= d;\n return *this;\n }\n point& operator/=(double d){\n x /= d; y /= d;\n return *this;\n }\n const point operator+ (const point& p) const;\n const point operator- (const point& p) const;\n const point operator* (double d) const;\n const point operator/ (double d) const;\n};\nconst point point::operator+ (const point& p) const{\n point res(*this); return res += p;\n}\nconst point point::operator- (const point& p) const{\n point res(*this); return res -= p;\n}\nconst point point::operator* (double d) const{\n point res(*this); return res *= d;\n}\nconst point point::operator/ (double d) const{\n point res(*this); return res /= d;\n}\n\nstruct line{\n point A,B;\n line() {}\n line(point A, point B): A(A), B(B) {}\n};\n// 前のライブラリが一部残ってる\n// 後で整備する\n// A+B\npoint sum(point A,point B){\n A.x+=B.x; A.y+=B.y; return A;\n}\n// A-B\npoint sub(point A,point B){\n A.x-=B.x; A.y-=B.y; return A;\n}\n// A to B\npoint vec(line L){\n return sub(L.B,L.A);\n}\n\ndouble abs(point P){\n return sqrt(P.x*P.x+P.y*P.y);\n}\n\ndouble dist(point A,point B){\n return abs(sub(A,B));\n}\n\ndouble dot(point a,point b){\n return a.x*b.x+a.y*b.y;\n}\n\ndouble cross(point a,point b){\n return a.x*b.y-a.y*b.x;\n}\n\nint ccw(point a,point b,point c){\n b={b.x-a.x,b.y-a.y};\n c={c.x-a.x,c.y-a.y};\n if(cross(b,c)>eps)return 1; // a,b,c反時計回り\n if(cross(b,c)<-eps)return -1; // a,b,c時計周り\n if(dot(b,c)<-eps)return 2; // c,a,b 一直線\n if((b.x*b.x+b.y*b.y)<(c.x*c.x+c.y*c.y))return -2; // a,b,c 一直線\n return 0; // a,c,b 一直線\n}\n\n// 反時計回りで凸多角形を与えるものとする\n// 始点と終点を一致させた入力を与える\n// AOJ 1283\nbool point_in_polygon(point p, vector<point> polygon){\n int n = polygon.size();\n for(int i=0;i+1<n;i++){\n if(cross(p - polygon[i], polygon[i+1] - polygon[i]) > 0){\n return false;\n }\n }\n return true;\n}\nline parallel_line(line l, double d){\n point v = point(-(l.B - l.A).y, (l.B - l.A).x);\n v = v / abs(v);\n l.A += v * d;\n l.B += v * d;\n return l;\n}\npoint line_intersection(line l1, line l2){\n return l1.A + vec(l1) * cross(l2.A - l1.A, vec(l2)) / cross(vec(l1), vec(l2));\n}\n// 交点の座標を返す\npoint Crosspoint(point a,point b,point c,point d){\n point cd={d.x-c.x,d.y-c.y};\n point ca={a.x-c.x,a.y-c.y};\n point cb={b.x-c.x,b.y-c.y};\n double d1=abs(cd.x*ca.y-cd.y*ca.x),d2=abs(cd.x*cb.y-cd.y*cb.x);\n double t=d1/(d1+d2);\n return {a.x+(b.x-a.x)*t,a.y+(b.y-a.y)*t};\n}\nvector<point> convex_cut(const vector<point> &g, const line &l){\n vector<point> Q;\n for(int i=0;i<g.size();i++){\n point A=g[i],B=g[(i+1)%g.size()];\n if(ccw(l.A,l.B,A)!=-1)Q.push_back(A);\n if(ccw(l.A,l.B,A)*ccw(l.A,l.B,B)<0)Q.push_back(Crosspoint(A,B,l.A,l.B));\n }\n return Q;\n}\n\nint main(){\n cin.tie(nullptr);\n ios::sync_with_stdio(false); \n int n;\n while(cin >> n,n){\n vector<point> v(n);\n for(int i=0;i<n;i++){\n cin >> v[i].x >> v[i].y;\n }\n v.push_back(v[0]);\n double l = 0, r = 1e6;\n auto check=[&](double mid)->bool{\n auto polygon = v;\n polygon.pop_back();\n vector<line> seg;\n for(int i=0;i<n;i++){\n seg.push_back(parallel_line(line(v[i], v[i+1]), mid));\n }\n for(auto &l:seg){\n polygon = convex_cut(polygon, l);\n if(polygon.size() <= 1)return false;\n }\n return true;\n };\n for(int _=0;_<120;_++){\n double mid = (l+r)/2.0;\n if(check(mid)){\n l = mid;\n }\n else{\n r = mid;\n }\n }\n printf(\"%.9f\\n\",l);\n }\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3524, "score_of_the_acc": -0.6837, "final_rank": 5 }, { "submission_id": "aoj_1283_5971394", "code_snippet": "#include <bits/stdc++.h>\n//#include <atcoder/all>\n//using namespace atcoder;\n#pragma GCC target (\"avx2\")\n#pragma GCC optimization (\"O3\")\n#pragma GCC optimization (\"unroll-loops\")\nusing namespace std;\n \ntypedef vector<int> VI;\ntypedef vector<VI> VVI;\ntypedef vector<string> VS;\ntypedef pair<int, int> PII;\ntypedef pair<int, int> pii;\ntypedef pair<long long, long long> PLL;\ntypedef pair<int, PII> TIII;\n \ntypedef long long ll;\ntypedef long double ld;\ntypedef unsigned long long ull;\n \n \n#define FOR(i, s, n) for (int i = s; i < (int)n; ++i)\n#define REP(i, n) FOR(i, 0, n)\n#define rep(i, a, b) for (int i = a; i < (b); ++i)\n#define trav(a, x) for (auto &a : x)\n#define all(x) x.begin(), x.end()\n \n#define MOD 1000000007\n \ntemplate<class T1, class T2> inline bool chmax(T1 &a, T2 b) {if (a < b) {a = b; return true;} return false;}\ntemplate<class T1, class T2> inline bool chmin(T1 &a, T2 b) {if (a > b) {a = b; return true;} return false;}\nconst double EPS = 1e-8, PI = acos(-1);\nconst double pi = 3.141592653589793238462643383279;\n//ここから編集 \ntypedef string::const_iterator State;\nll GCD(ll a, ll b){\n return (b==0)?a:GCD(b, a%b);\n}\nll LCM(ll a, ll b){\n return a/GCD(a, b) * b;\n}\ntemplate< int mod >\nstruct ModInt {\n int x;\n \n ModInt() : x(0) {}\n \n ModInt(int64_t y) : x(y >= 0 ? y % mod : (mod - (-y) % mod) % mod) {}\n \n ModInt &operator+=(const ModInt &p) {\n if((x += p.x) >= mod) x -= mod;\n return *this;\n }\n \n ModInt &operator-=(const ModInt &p) {\n if((x += mod - p.x) >= mod) x -= mod;\n return *this;\n }\n \n ModInt &operator*=(const ModInt &p) {\n x = (int) (1LL * x * p.x % mod);\n return *this;\n }\n \n ModInt &operator/=(const ModInt &p) {\n *this *= p.inverse();\n return *this;\n }\n \n ModInt operator-() const { return ModInt(-x); }\n \n ModInt operator+(const ModInt &p) const { return ModInt(*this) += p; }\n \n ModInt operator-(const ModInt &p) const { return ModInt(*this) -= p; }\n \n ModInt operator*(const ModInt &p) const { return ModInt(*this) *= p; }\n \n ModInt operator/(const ModInt &p) const { return ModInt(*this) /= p; }\n \n bool operator==(const ModInt &p) const { return x == p.x; }\n \n bool operator!=(const ModInt &p) const { return x != p.x; }\n \n ModInt inverse() const {\n int a = x, b = mod, u = 1, v = 0, t;\n while(b > 0) {\n t = a / b;\n swap(a -= t * b, b);\n swap(u -= t * v, v);\n }\n return ModInt(u);\n }\n \n ModInt pow(int64_t n) const {\n ModInt ret(1), mul(x);\n while(n > 0) {\n if(n & 1) ret *= mul;\n mul *= mul;\n n >>= 1;\n }\n return ret;\n }\n \n friend ostream &operator<<(ostream &os, const ModInt &p) {\n return os << p.x;\n }\n \n friend istream &operator>>(istream &is, ModInt &a) {\n int64_t t;\n is >> t;\n a = ModInt< mod >(t);\n return (is);\n }\n \n static int get_mod() { return mod; }\n};\n \nusing modint = ModInt< 998244353 >;\ntemplate< typename T >\nstruct Combination {\n vector< T > _fact, _rfact, _inv;\n \n Combination(int sz) : _fact(sz + 1), _rfact(sz + 1), _inv(sz + 1) {\n _fact[0] = _rfact[sz] = _inv[0] = 1;\n for(int i = 1; i <= sz; i++) _fact[i] = _fact[i - 1] * i;\n _rfact[sz] /= _fact[sz];\n for(int i = sz - 1; i >= 0; i--) _rfact[i] = _rfact[i + 1] * (i + 1);\n for(int i = 1; i <= sz; i++) _inv[i] = _rfact[i] * _fact[i - 1];\n }\n \n inline T fact(int k) const { return _fact[k]; }\n \n inline T rfact(int k) const { return _rfact[k]; }\n \n inline T inv(int k) const { return _inv[k]; }\n \n T P(int n, int r) const {\n if(r < 0 || n < r) return 0;\n return fact(n) * rfact(n - r);\n }\n \n T C(int p, int q) const {\n if(q < 0 || p < q) return 0;\n return fact(p) * rfact(q) * rfact(p - q);\n }\n \n T H(int n, int r) const {\n if(n < 0 || r < 0) return (0);\n return r == 0 ? 1 : C(n + r - 1, r);\n }\n};\n \nll modpow(ll x, ll n, ll mod) {\n ll res = 1;\n x %= mod;\n if(x == 0) return 0;\n while(n) {\n if(n&1) res = (res * x) % mod;\n x = (x * x) % mod;\n n >>= 1;\n }\n return res;\n} \ninline long long mod(long long a, long long m) {\n return (a % m + m) % m;\n}\ntemplate<typename T> \nstruct BIT{\n int N;\n std::vector<T> node;\n BIT(){}\n BIT(int n){\n N = n;\n node.resize(N+10);\n }\n void build(int n) {\n N = n;\n node.resize(N+10);\n }\n /* a: 1-indexed */\n void add(int a, T x){\n for(int i=a; i<(int)node.size(); i += i & -i) node[i] += x;\n }\n\n /* [1, a] */\n T sum(int a){\n T res = 0;\n for(int x=a; x>0; x -= x & -x) res += node[x];\n return res;\n }\n\n /* [l, r] */\n T rangesum(int l, int r){\n return sum(r) - sum(l-1);\n }\n\n /* \n a1+a2+...+aw >= valとなるような最小のwを返す\n @verify https://codeforces.com/contest/992/problem/E\n */\n int lower_bound(T val) {\n if(val < 0) return 0;\n\n int res = 0;\n int n = 1; \n while (n < N) n *= 2;\n\n T tv=0;\n for (int k = n; k > 0; k /= 2) {\n if(res + k < N && node[res + k] < val){\n val -= node[res+k];\n res += k;\n }\n }\n return res+1; \n }\n};\nstruct UnionFind{\n std::vector<int> par;\n std::vector<int> siz;\n\n UnionFind(int sz_): par(sz_), siz(sz_) {\n for(int i=0; i<sz_; ++i) par[i] = i, siz[i] = 1;\n }\n\n void init(int sz_){\n par.resize(sz_);\n siz.resize(sz_);\n for(int i=0; i<sz_; ++i) par[i] = i, siz[i] = 1;\n }\n\n int root(int x){\n if(x == par[x]) return x;\n return par[x] = root(par[x]);\n }\n\n bool merge(int x, int y){\n x = root(x), y = root(y);\n if(x == y) return false;\n if(siz[x] < siz[y]) std::swap(x, y);\n siz[x] += siz[y];\n par[y] = x;\n return true;\n }\n\n bool issame(int x, int y){\n return root(x) == root(y);\n }\n\n int size(int x){\n return siz[root(x)];\n }\n};\nstruct RollingHash{\n\n using ull = unsigned long long;\n const ull mod = (1ULL << 61) - 1;\n const ull MASK30 = (1ULL << 30) - 1;\n const ull MASK31 = (1ULL << 31) - 1;\n\n const ull MASK61 = mod;\n\n ull base;\n int n;\n vector<ull> hash, pow;\n\n RollingHash(const string &s)\n {\n random_device rnd;\n mt19937_64 mt(rnd());\n base = 1001;\n \n n = (int)s.size();\n hash.assign(n+1, 0);\n pow.assign(n+1, 1);\n \n for(int i=0; i<n; i++){\n hash[i+1] = calc_mod(mul(hash[i], base) + s[i]);\n pow[i+1] = calc_mod(mul(pow[i], base));\n }\n }\n\n ull calc_mod(ull x){\n ull xu = x >> 61;\n ull xd = x & MASK61;\n ull res = xu + xd;\n if(res >= mod) res -= mod;\n return res;\n }\n\n ull mul(ull a, ull b){\n ull au = a >> 31;\n ull ad = a & MASK31;\n ull bu = b >> 31;\n ull bd = b & MASK31;\n ull mid = ad * bu + au * bd;\n ull midu = mid >> 30;\n ull midd = mid & MASK30;\n return calc_mod(au * bu * 2 + midu + (midd << 31) + ad * bd);\n }\n\n //[l,r)のハッシュ値\n inline ull get(int l, int r){\n ull res = calc_mod(hash[r] + mod*3-mul(hash[l], pow[r-l]));\n return res;\n }\n //string tのハッシュ値\n inline ull get(string t){\n ull res = 0;\n for(int i=0; i<t.size(); i++){\n res = calc_mod(mul(res, base)+t[i]);\n }\n return res;\n }\n //string s中のtの数をカウント\n inline int count(string t) {\n if(t.size() > n) return 0;\n auto hs = get(t);\n int res = 0;\n for(int i=0; i<n-t.size()+1; i++){\n if(get(i, i+t.size()) == hs) res++; \n }\n return res;\n }\n\n /* \n concat \n @verify https://codeforces.com/problemset/problem/514/C\n */\n inline ull concat(ull h1, ull h2, int h2len){\n return calc_mod(h2 + mul(h1, pow[h2len]));\n }\n\n // LCPを求める S[a:] T[b:]\n inline int LCP(int a, int b){\n int len = min((int)hash.size()-a, (int)hash.size()-b);\n \n int lb = -1, ub = len;\n while(ub-lb>1){\n int mid = (lb+ub)/2;\n\n if(get(a, a+mid) == get(b, b+mid)) lb = mid;\n else ub = mid;\n }\n return lb;\n }\n};\n\nusing Point = complex< double >;\nbool eq(double a, double b){ return fabs(a-b) < EPS; }\nnamespace std {\n bool operator<(const Point a, const Point b) {\n if(eq(a.real(),b.real())) return a.imag() < b.imag();\n return a.real() < b.real();\n }\n}\n\nistream &operator>> (istream &is, Point &p) {\n double a, b;\n is >> a >> b;\n p = Point(a, b);\n return is;\n}\n\nostream &operator<< (ostream &os, Point &p) {\n return os << fixed << setprecision(10) << p.real() << \" \" << p.imag();\n}\n\nbool operator<(const Point &a, const Point &b) {\n return a.real() != b.real() ? a.real() < b.real() : a.imag() < b.imag();\n}\n\n// rotate Φ(rad)\n// x = r * cos(θ + Φ)\n// = r * cos(θ) * cos(Φ) - r * sin(θ) * sin(Φ)\n// = x * cos(Φ) - y * sin(Φ) (∵ cos(θ) = x/r, sin(θ) = y/r) \nPoint rotate(double phi, const Point &p) {\n double x = p.real(), y = p.imag();\n return Point(x * cos(phi) - y * sin(phi), x * sin(phi) + y * cos(phi));\n}\n\ndouble radian_to_degree(double r) {\n return (r * 180.0 / PI);\n}\n\ndouble degree_to_radian(double d) {\n return (d * PI / 180.0);\n}\n\nstruct Line{\n Point a, b;\n\n Line() = default;\n\n Line(Point a, Point b) : a(a), b(b){}\n\n Line(double A, double B, double C){\n //ax + by = c\n if(eq(A, 0)){\n a = Point(0, C/B), b = Point(1, C/B);\n }else if(eq(B, 0)){\n a = Point(C/A, 0), b = Point(C/A, 1);\n }else{\n a = Point(0, C/B), b = Point(C/A, 0);\n }\n }\n\n friend istream &operator>>(istream &is, Line &a) {\n return is >> a.a >> a.b;\n }\n friend ostream &operator<<(ostream &os, Line &a) {\n return os << a.a << \" to \" << a.b;\n }\n};\n\nstruct Segment: Line {\n Segment() = default;\n\n Segment(Point a, Point b) : Line(a, b) {}\n};\n\nstruct Circle {\n Point p;\n double r;\n\n Circle() = default;\n\n Circle(Point p, double r): p(p), r(r){}\n};\n\nusing Points = vector<Point>;\nusing Polygon = vector<Point>;\nusing Segments = vector<Segment>;\nusing Lines = vector<Line>;\nusing Circles = vector<Circle>;\n\ndouble cross(const Point &a, const Point &b) {\n return a.real() * b.imag() - a.imag() * b.real();\n}\n\ndouble dot(const Point& a, const Point &b) {\n return a.real() * b.real() + a.imag() * b.imag();\n}\n\n//https://mathtrain.jp/projection\nPoint projection(const Line &l, const Point &p){\n double t = dot(p - l.a, l.a-l.b) / norm(l.a - l.b);\n return l.a + (l.a - l.b) * t;\n}\n\nPoint projection(const Segment &l, const Point &p){\n double t = dot(p - l.a, l.b-l.a) / norm(l.a - l.b);\n return l.a + (l.b - l.a) * t;\n}\n\nPoint reflection(const Line &l, const Point &p){\n return p + (projection(l, p) - p) * 2.0;\n}\n\nint ccw(const Point &a, const Point &b, const Point &c) {\n if(cross(b-a, c-a) > EPS) return 1; // \"COUNTER_CLOCKWISE\"\n if(cross(b-a, c-a) < -EPS) return -1; // \"CLOCKWISE\"\n if(dot(b-a, c-a) < -EPS) return 2; // \"ONLINE_BACK\" c-a-b\n if(norm(b-a) < norm(c-a) - EPS) return -2; // \"ONLINE_FRONT\" a-b-c\n return 0; // \"ON_SEGMENT\" a-c-b\n}\n\nbool parallel(const Line &a, const Line &b){\n return eq(cross(a.a-a.b, b.a-b.b), 0.0);\n}\nbool orthogonal(const Line &a, const Line &b){\n return eq(dot(a.a-a.b, b.a-b.b), 0.0);\n}\nenum { OUT, ON, IN };\n\nint contains(const Polygon& Q, const Point& p){\n bool in = false;\n for(int i=0; i<Q.size(); i++){\n Point a = Q[i] - p, b = Q[(i+1)%Q.size()]-p;\n if(a.imag() > b.imag()) swap(a, b);\n if(a.imag() <= 0 && 0 < b.imag() && cross(a, b) < 0) in = !in;\n if(cross(a, b) == 0 && dot(a, b) <= 0) return ON;\n }\n return in ? IN : OUT;\n}\nbool intersect(const Segment &s, const Point &p){\n return ccw(s.a, s.b, p) == 0;\n}\n\n//直線の交差判定\nbool intersect(const Line &a, const Line &b) {\n if(parallel(a, b)) return 0;\n return 1;\n}\n\n//線分が重なる/端点で交わるときtrue\nbool intersect(const Segment &a, const Segment &b) {\n return ccw(a.a, a.b, b.a) * ccw(a.a, a.b, b.b) <= 0 && ccw(b.a, b.b, a.a) * ccw(b.a, b.b, a.b) <= 0;\n}\n\n\n\nPoint crosspoint(const Line &a, const Line &b){\n double d = cross(b.b-b.a, a.b-a.a);\n if(eq(d, 0.0)) return Point(1e9, 1e9); \n \n return a.a + (a.b - a.a) * cross(b.b-b.a, b.b-a.a) / d;\n}\n\nPoint crosspoint(const Segment &a, const Segment &b){\n return crosspoint(Line(a.a, a.b), Line(b.a, b.b));\n}\ndouble distance(const Point &a, const Point &b){\n return abs(a - b);\n}\ndouble distance(const Line &l, const Point &p){\n return abs( cross(p - l.a, l.b-l.a) / abs(l.b-l.a) );\n}\ndouble distance(const Segment &s, const Point &p){\n Point r = projection(s, p);\n if(intersect(s, r)) return abs(r-p);\n return min(abs(s.a - p), abs(s.b - p));\n}\n// double distance(const Line &a, const Line &b){\n\n// }\n// double distance(const Line &a, const Segment &b){\n\n// }\ndouble distance(const Segment &a, const Segment &b) {\n return intersect(a, b) ? 0 : min({distance(a, b.a), distance(a, b.b), distance(b, a.a), distance(b, a.b)});\n}\n\n/* 2円の交点 */\nvector<Point> crosspointCC(Circle C1, Circle C2){\n vector<Point> ps;\n Point ab = C2.p - C1.p;\n double d = abs(ab);\n double rc = (C1.r * C1.r + d * d - C2.r * C2.r) / (2 * d);\n if(eq(d, 0) || C1.r < abs(rc)) return ps;\n\n double rs = sqrt(C1.r * C1.r - rc*rc);\n\n Point abN = ab * Point(0, rs/d);\n Point cp = C1.p + rc / d * ab;\n ps.push_back(cp + abN);\n if(!eq(norm(abN), 0))ps.push_back(cp-abN);\n return ps;\n}\n\nvector<Point> crosspointCL(Circle C, Line l){\n Point p = projection(l, C.p);\n\n Point e = (l.b-l.a)/abs(l.b-l.a);\n if(eq(distance(l, C.p), C.r)) {\n return vector<Point>{p, p};\n }\n double base = sqrt(C.r*C.r-norm(p-C.p));\n \n return vector<Point>{p+e*base, p-e*base};\n}\n\n/* 円同士の共通部分の面積を求める */\n// verify: http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_7_I date: 2020/10/11\nlong double AreaofCC(Circle& C1, Circle& C2){\n if(C1.r > C2.r) swap(C1, C2);\n long double nor = norm(C1.p - C2.p);\n long double dist = sqrtl(nor);\n \n if(C1.r + C2.r < dist+EPS) return 0;\n\n if(dist + C1.r < C2.r + EPS) return C1.r * C1.r * PI;\n\n long double theta1 = acosl((nor + C1.r*C1.r - C2.r * C2.r)/(2*C1.r*dist));\n\n long double theta2 = acosl((nor + C2.r*C2.r - C1.r * C1.r)/(2*C2.r*dist));\n \n return (theta1 - sinl(theta1+ theta1) *0.5) * C1.r * C1.r + (theta2 - sinl(theta2+theta2) *0.5) * C2.r * C2.r;\n}\n\n\nPolygon convex_hull(Polygon &p)\n{\n int n = (int)p.size(), k = 0;\n if (n <= 2)\n return p;\n sort(p.begin(), p.end());\n vector<Point> ch(n * 2);\n\n for (int i = 0; i < n; ch[k++] = p[i++])\n {\n while (k >= 2 && cross(ch[k - 1] - ch[k - 2], p[i] - ch[k - 1]) < 0)\n --k;\n }\n\n for (int i = n - 2, t = k + 1; i >= 0; ch[k++] = p[i--])\n {\n while (k >= t && cross(ch[k - 1] - ch[k - 2], p[i] - ch[k - 1]) < 0)\n --k;\n }\n ch.resize(k - 1);\n return ch;\n}\ndouble convex_diameter(const Polygon &p)\n{\n int n = (int)p.size();\n if (n == 2)\n return abs(p[0] - p[1]);\n\n int is = 0, js = 0;\n for (int i = 1; i < n; ++i)\n {\n if (imag(p[i]) > imag(p[is]))\n is = i;\n if (imag(p[i]) < imag(p[js]))\n js = i;\n }\n\n double res = abs(p[is] - p[js]);\n int i, maxi, j, maxj;\n i = maxi = is;\n j = maxj = js;\n do\n {\n if (cross(p[(i + 1) % n] - p[i], p[(j + 1) % n] - p[j]) >= 0)\n j = (j + 1) % n;\n else\n i = (i + 1) % n;\n res = max(res, abs(p[i] - p[j]));\n } while (i != is || j != js);\n return res;\n}\n\nPolygon convex_cut(const Polygon &p, const Line l)\n{\n Polygon ret;\n for (int i = 0; i < p.size(); ++i)\n {\n Point now = p[i], nxt = p[(i + 1) % p.size()];\n if (ccw(l.a, l.b, now) != -1) //交点が線分l上にあるとき\n ret.push_back(now);\n if (ccw(l.a, l.b, now) * ccw(l.a, l.b, nxt) < 0)\n {\n ret.push_back(crosspoint(Line(now, nxt), l));\n }\n }\n return (ret);\n}\ndouble closestpair(Points &a, int l, int r){\n if(r-l<=1) return 1e20;\n int mid = (l+r)/2;\n\n double X = a[mid].real();\n double d = min(closestpair(a, l, mid), closestpair(a, mid, r));\n inplace_merge(a.begin()+l, a.begin()+mid, a.begin()+r, [](const Point& pa, const Point& pb){\n return pa.imag() < pb.imag();\n });\n\n Points b;\n for(int i=l; i<r; i++){\n if(abs(a[i].real()-X) >= d) continue;\n for(int j=b.size()-1; j>=0; j--){\n if(abs((a[i]-b[j]).imag()) >= d) break;\n d = min(d, abs(a[i]-b[j]));\n }\n b.push_back(a[i]);\n }\n return d;\n}\n\n/* 円の交差判定 */\n/* verify: http://judge.u-aizu.ac.jp/onlinejudge/finder.jsp?course=CGL date:2020/10/11 */\nint intersectionCC(Circle c1, Circle c2){\n if(c1.r > c2.r) swap(c1, c2);\n double d1 = abs(c1.p-c2.p); \n double d2 = c1.r + c2.r;\n if(d1 > d2) return 4; /* 互いに交点を持たない */\n else if(d1 == d2) return 3; /* 外接する場合 */\n else{\n if(c2.r == c1.r + d1) return 1; /* 内接する場合 */\n else if(c2.r > c1.r + d1) return 0; /* 包含 */\n else return 2; /* 交点を2つ持つ */\n }\n}\n\n/* 点集合が反時計回りに与えられる場合のみ */\ndouble Area(Points &g){\n double res = 0;\n int n = g.size();\n REP(i,n){\n res += cross(g[i], g[(i+1)%n]);\n }\n return res/2.0;\n}\n\n/* 内接円 */\n/* verify: http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_7_B&lang=ja date: 2020/10/11 */\npair<Point, double> incircle_of_a_Triangle(Points &p){\n\n double a = abs(p[1]-p[2]), b = abs(p[2]-p[0]), c = abs(p[0]-p[1]);\n double s = (a+b+c) / 2.0;\n double S = sqrtl(s*(s-a)*(s-b)*(s-c));\n double r = S/s; \n \n Point pp = (a*p[0]+b*p[1]+c*p[2])/(a+b+c);\n return make_pair(pp, r);\n} \n\n/* 外接円 */\npair<Point, double> circumscribed_circle_of_a_triangle(Points &p){\n\n Point m1((p[0]+p[1])/2.0), m2((p[1]+p[2])/2.0);\n Point v((p[1]-p[0]).imag(), (p[0]-p[1]).real()), w((p[1]-p[2]).imag(), (p[2]-p[1]).real());\n\n Line l1(m1, Point(v+m1)), l2(m2, Point(w+m2));\n\n Point x = crosspoint(l1, l2);\n double r = abs(x-p[0]);\n return make_pair(x, r);\n}\n\n/* ある点pを通る円cの接線 */\nPoints tangent_to_a_circle(Point p, Circle C){\n Points ps;\n double d = abs(C.p-p);\n if(eq(d,0)){\n ps.push_back(p);\n }else if(d>EPS){\n \n double d2 = sqrt(d*d-C.r*C.r);\n \n long double theta = acosl(d2/d);\n //cout << theta << endl;\n Point pp = C.p - p;\n Point p2 = rotate(-theta, pp);\n\n Point e = p2/abs(p2);\n Point ppp = e*d2;\n ps.push_back(p + ppp);\n p2 = rotate(theta, pp);\n e = p2/abs(p2);\n ppp = e*d2;\n ps.push_back(p + ppp);\n \n }\n return ps;\n}\n\nLines tangent(Circle C1, Circle C2){\n Lines ls;\n if(C1.r < C2.r) swap(C1, C2);\n double d = norm(C1.p - C2.p);\n if(eq(d, 0)) return ls;\n Point u = (C2.p - C1.p) / sqrt(d);\n Point v = rotate(pi/2, u);\n\n for(double s: {-1, 1}) {\n double h = (C1.r + s * C2.r) / sqrt(d);\n if(eq(1-h*h, 0)) {\n ls.emplace_back(C1.p + u * C1.r, C1.p + (u+v) * C1.r);\n }else if(1-h*h>0){\n Point uu = u*h, vv = v*sqrt(1-h*h);\n ls.emplace_back(C1.p + (uu+vv) * C1.r, C2.p - (uu+vv) * C2.r * s);\n ls.emplace_back(C1.p + (uu-vv) * C1.r, C2.p - (uu-vv) * C2.r * s);\n }\n }\n return ls;\n}\nLine bisector(Point a, Point b){\n Point A = (a+b)*Point(0.5, 0); \n return Line(A, A+(b-a)*Point(0, PI/2));\n}\n\nPolygon voronoi_cell(Polygon Q, Points P, int s){\n REP(i,P.size()){\n if(i!=s){\n Q = convex_cut(Q, bisector(P[s], P[i]));\n }\n }\n return Q;\n}\nPolygon py;\ndouble xans, yans;\nusing D = double;\ndouble f2(double x, double y) {\n\n if(contains(py, Point(x, y)) == OUT) {\n double mx = -1e9;\n REP(i,py.size()) {\n mx = max(mx, (-1) * distance(Segment(py[i], py[(i+1)%py.size()]), Point(x, y)));\n }\n return mx;\n }else{\n double mx = 1e9;\n REP(i,py.size()) {\n mx = min(mx, distance(Segment(py[i], py[(i+1)%py.size()]), Point(x, y)));\n }\n return mx;\n }\n}\ndouble f(double x) {\n D ymin = -1e5;\n D ymax = 1e5;\n while(ymax-ymin>0.000001) {\n D y1 = (ymin*2 + ymax) / 3;\n D y2 = (ymin + ymax*2) / 3;\n if(f2(x, y1) < f2(x, y2)) {\n ymin = y1;\n yans = y2;\n }else{\n ymax = y2;\n yans = y1;\n }\n }\n return f2(x, yans);\n}\nint main() { \n cin.tie(0);\n ios::sync_with_stdio(false);\n cout << fixed << setprecision(12);\n \n int n;\n while(cin >> n, n) {\n py.clear();\n xans = 0;\n yans = 0;\n REP(i,n) {\n Point p; cin >> p;\n py.push_back(p);\n }\n\n double xmin = -1e5;\n double xmax = 1e5;\n while(xmax-xmin>0.000001) {\n double x1 = (xmin*2 + xmax) / 3;\n double x2 = (xmin + xmax*2) / 3;\n if(f(x1) < f(x2)) {\n xmin = x1;\n xans = x2;\n }else{\n xmax = x2;\n xans = x1;\n }\n }\n double ans = 1e9;\n REP(i,py.size()) {\n ans = min(ans, distance(Segment(py[i], py[(i+1)%n]), Point(xans, yans)));\n }\n cout << ans << '\\n';\n }\n return 0;\n}", "accuracy": 1, "time_ms": 390, "memory_kb": 3524, "score_of_the_acc": -0.784, "final_rank": 10 }, { "submission_id": "aoj_1283_5929392", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\nnamespace geometry {\nusing Real = double;\nconstexpr Real EPS = 1e-8;\nconstexpr Real PI = 3.14159265358979323846L;\n\ninline int sgn(Real x) { return x < -EPS ? -1 : x > EPS ? 1 : 0; }\n\ninline int compare(Real a, Real b) { return sgn(a - b); }\n\ninline bool equals(Real a, Real b) { return compare(a, b) == 0; }\n\nstruct Point {\n Real x, y;\n\n Point() {}\n\n Point(Real x, Real y) : x(x), y(y) {}\n\n Point& operator+=(const Point& p) {\n x += p.x, y += p.y;\n return *this;\n }\n\n Point& operator-=(const Point& p) {\n x -= p.x, y -= p.y;\n return *this;\n }\n\n Point& operator*=(const Real& k) {\n x *= k, y *= k;\n return *this;\n }\n\n Point& operator/=(const Real& k) {\n x /= k, y /= k;\n return *this;\n }\n\n Point operator+(const Point& p) const { return Point(*this) += p; }\n\n Point operator-(const Point& p) const { return Point(*this) -= p; }\n\n Point operator*(const Real& k) const { return Point(*this) *= k; }\n\n Point operator/(const Real& k) const { return Point(*this) /= k; }\n\n Point operator-() const { return Point(-x, -y); }\n\n bool operator==(const Point& p) const { return (compare(x, p.x) == 0 && compare(y, p.y) == 0); }\n\n bool operator!=(const Point& p) const { return !(*this == p); }\n\n bool operator<(const Point& p) const {\n return compare(x, p.x) < 0 || (compare(x, p.x) == 0 && compare(y, p.y) < 0);\n }\n\n bool operator>(const Point& p) const {\n return compare(x, p.x) > 0 || (compare(x, p.x) == 0 && compare(y, p.y) > 0);\n }\n\n friend std::istream& operator>>(std::istream& is, Point& p) { return is >> p.x >> p.y; }\n\n friend std::ostream& operator<<(std::ostream& os, const Point& p) { return os << p.x << ' ' << p.y; }\n\n Real norm() const { return x * x + y * y; }\n\n Real abs() const { return std::hypot(x, y); }\n\n Real arg() const { return std::atan2(y, x); }\n\n Point normal() const { return Point(-y, x); }\n\n Point unit() const { return *this / abs(); }\n\n Point rotate(Real theta) const {\n return Point(x * std::cos(theta) - y * std::sin(theta), x * std::sin(theta) + y * std::cos(theta));\n }\n};\n\nPoint polar(const Real& r, const Real& theta) { return Point(cos(theta), sin(theta)) * r; }\n\nReal dot(const Point& p, const Point& q) { return p.x * q.x + p.y * q.y; }\n\nReal cross(const Point& p, const Point& q) { return p.x * q.y - p.y * q.x; }\n\nReal distance(const Point& p, const Point& q) { return (p - q).abs(); }\n\nstruct Line {\n Point a, b;\n\n Line() {}\n\n Line(const Point& a, const Point& b) : a(a), b(b) {}\n\n Line(const Real& A, const Real& B, const Real& C) { // Ax + By = c\n if (equals(A, 0)) {\n assert(!equals(B, 0));\n a = Point(0, C / B);\n b = Point(1, C / B);\n } else if (equals(B, 0)) {\n a = Point(C / A, 0);\n b = Point(C / A, 1);\n } else {\n a = Point(0, C / B);\n b = Point(C / A, 0);\n }\n }\n\n friend std::istream& operator>>(std::istream& is, Line& l) { return is >> l.a >> l.b; }\n\n friend std::ostream& operator<<(std::ostream& os, const Line& l) { return os << l.a << \" to \" << l.b; }\n\n Point diff() const { return b - a; }\n};\n\nstruct Segment : Line {\n Segment() {}\n\n Segment(Point a, Point b) : Line(a, b) {}\n\n Real length() const { return diff().abs(); }\n};\n\nPoint proj(const Line& l, const Point& p) {\n Point v = l.diff().unit();\n return l.a + v * dot(v, p - l.a);\n}\n\nPoint refl(const Line& l, const Point& p) {\n Point h = proj(l, p);\n return h + (h - p);\n}\n\nbool orthogonal(const Line& l, const Line& m) { return equals(dot(l.diff(), m.diff()), 0); }\n\nbool parallel(const Line& l, const Line& m) { return equals(cross(l.diff(), m.diff()), 0); }\n\nenum Position { COUNTER_CLOCKWISE = +1, CLOCKWISE = -1, ONLINE_BACK = +2, ONLINE_FRONT = -2, ON_SEGMENT = 0 };\n\nPosition ccw(const Point& a, Point b, Point c) {\n b -= a, c -= a;\n if (sgn(cross(b, c)) == +1) return COUNTER_CLOCKWISE;\n if (sgn(cross(b, c)) == -1) return CLOCKWISE;\n if (sgn(dot(b, c)) == -1) return ONLINE_BACK;\n if (compare(b.norm(), c.norm()) == -1) return ONLINE_FRONT;\n return ON_SEGMENT;\n}\n\nbool intersect(const Line& l, const Point& p) { return abs(ccw(l.a, l.b, p)) != 1; }\n\nbool intersect(const Line& l, const Line& m) {\n Real A = cross(l.diff(), m.diff()), B = cross(l.diff(), l.b - m.a);\n if (equals(A, 0) && equals(B, 0)) return true;\n return !parallel(l, m);\n}\n\nbool intersect(const Line& l, const Segment& s) {\n return sgn(cross(l.diff(), s.a - l.a)) * sgn(cross(l.diff(), s.b - l.a)) <= 0;\n}\n\nbool intersect(const Segment& s, const Point& p) { return ccw(s.a, s.b, p) == ON_SEGMENT; }\n\nbool intersect(const Segment& s, const Segment& t) {\n 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;\n}\n\nReal distance(const Line& l, const Point& p) { return distance(p, proj(l, p)); }\n\nReal distance(const Line& l, const Line& m) { return intersect(l, m) ? 0 : distance(l, m.a); }\n\nReal distance(const Line& l, const Segment& s) {\n return intersect(l, s) ? 0 : std::min(distance(l, s.a), distance(l, s.b));\n}\n\nReal distance(const Segment& s, const Point& p) {\n Point h = proj(s, p);\n return intersect(s, h) ? distance(p, h) : std::min(distance(p, s.a), distance(p, s.b));\n}\n\nReal distance(const Segment& s, const Segment& t) {\n if (intersect(s, t)) return 0;\n return std::min({distance(s, t.a), distance(s, t.b), distance(t, s.a), distance(t, s.b)});\n}\n\nPoint crosspoint(const Line& l, const Line& m) {\n assert(intersect(l, m));\n Real A = cross(l.diff(), m.diff()), B = cross(l.diff(), l.b - m.a);\n if (equals(A, 0) && equals(B, 0)) return m.a;\n return m.a + m.diff() * B / A;\n}\n\nstruct Polygon : std::vector<Point> {\n using std::vector<Point>::vector;\n\n Polygon(int n) : std::vector<Point>(n) {}\n\n std::vector<Segment> segments() const {\n assert(size() > 1);\n std::vector<Segment> segs;\n for (size_t i = 0; i < size(); i++) segs.emplace_back((*this)[i], (*this)[(i + 1) % size()]);\n return segs;\n }\n\n Real area() const {\n Real sum = 0;\n for (size_t i = 0; i < size(); i++) sum += cross((*this)[i], (*this)[(i + 1) % size()]);\n return std::abs(sum) / 2;\n }\n\n bool is_convex(bool accept_on_segment = true) const {\n int n = size();\n for (int i = 0; i < n; i++) {\n if (accept_on_segment) {\n if (ccw((*this)[i], (*this)[(i + 1) % n], (*this)[(i + 2) % n]) == CLOCKWISE) {\n return false;\n }\n } else {\n if (ccw((*this)[i], (*this)[(i + 1) % n], (*this)[(i + 2) % n]) != COUNTER_CLOCKWISE) {\n return false;\n }\n }\n }\n return true;\n }\n};\n\nPolygon convex_cut(const Polygon& convex, const Line& l) {\n assert(convex.is_convex());\n int n = convex.size();\n Polygon res;\n for (int i = 0; i < n; i++) {\n const Point& cur = convex[i];\n const Point& nxt = convex[(i + 1) % n];\n if (ccw(l.a, l.b, cur) != CLOCKWISE) res.emplace_back(cur);\n if (ccw(l.a, l.b, cur) * ccw(l.a, l.b, nxt) < 0) res.emplace_back(crosspoint(Segment(cur, nxt), l));\n }\n return res;\n}\n\n} // namespace geometry\n\n/**\n * @brief 2 次元幾何ライブラリ\n * @docs docs/geometry/geometry.md\n */\n\nconst int INF = 1e9;\nconst long long IINF = 1e18;\nconst int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};\nconst char dir[4] = {'D', 'R', 'U', 'L'};\nconst long long MOD = 1000000007;\n// const long long MOD = 998244353;\n\nusing namespace geometry;\n\nvoid solve(int n) {\n Polygon P(n);\n for (auto& p : P) cin >> p;\n\n auto check = [&](double x) {\n Polygon Q = P;\n for (int i = 0; i < n; i++) {\n Point to = P[(i + 1) % n] - P[i];\n Point move = to.normal().unit() * x;\n Segment seg(P[i] + move, P[(i + 1) % n] + move);\n Q = convex_cut(Q, seg);\n }\n return Q.empty();\n };\n\n double lb = 0, ub = 10000;\n for (int _ = 0; _ < 300; _++) {\n double mid = (lb + ub) / 2;\n (check(mid) ? ub : lb) = mid;\n }\n\n cout << ub << '\\n';\n}\n\nint main() {\n cin.tie(0);\n ios::sync_with_stdio(false);\n cout << fixed << setprecision(10);\n int n;\n while (cin >> n, n) solve(n);\n return 0;\n}", "accuracy": 1, "time_ms": 60, "memory_kb": 3496, "score_of_the_acc": -0.6745, "final_rank": 3 }, { "submission_id": "aoj_1283_5929390", "code_snippet": "#define LOCAL\n#include <bits/stdc++.h>\nusing namespace std;\n#pragma region Macros\ntypedef long long ll;\ntypedef __int128_t i128;\ntypedef unsigned int uint;\ntypedef unsigned long long ull;\n#define ALL(x) (x).begin(), (x).end()\n\ntemplate <typename T> istream& operator>>(istream& is, vector<T>& v) {\n for (T& x : v) is >> x;\n return is;\n}\ntemplate <typename T> ostream& operator<<(ostream& os, const vector<T>& v) {\n for (int i = 0; i < (int)v.size(); i++) {\n os << v[i] << (i + 1 == (int)v.size() ? \"\" : \" \");\n }\n return os;\n}\ntemplate <typename T, typename U> ostream& operator<<(ostream& os, const pair<T, U>& p) {\n os << '(' << p.first << ',' << p.second << ')';\n return os;\n}\ntemplate <typename T, typename U> ostream& operator<<(ostream& os, const map<T, U>& m) {\n os << '{';\n for (auto itr = m.begin(); itr != m.end();) {\n os << '(' << itr->first << ',' << itr->second << ')';\n if (++itr != m.end()) os << ',';\n }\n os << '}';\n return os;\n}\ntemplate <typename T, typename U> ostream& operator<<(ostream& os, const unordered_map<T, U>& m) {\n os << '{';\n for (auto itr = m.begin(); itr != m.end();) {\n os << '(' << itr->first << ',' << itr->second << ')';\n if (++itr != m.end()) os << ',';\n }\n os << '}';\n return os;\n}\ntemplate <typename T> ostream& operator<<(ostream& os, const set<T>& s) {\n os << '{';\n for (auto itr = s.begin(); itr != s.end();) {\n os << *itr;\n if (++itr != s.end()) os << ',';\n }\n os << '}';\n return os;\n}\ntemplate <typename T> ostream& operator<<(ostream& os, const multiset<T>& s) {\n os << '{';\n for (auto itr = s.begin(); itr != s.end();) {\n os << *itr;\n if (++itr != s.end()) os << ',';\n }\n os << '}';\n return os;\n}\ntemplate <typename T> ostream& operator<<(ostream& os, const unordered_set<T>& s) {\n os << '{';\n for (auto itr = s.begin(); itr != s.end();) {\n os << *itr;\n if (++itr != s.end()) os << ',';\n }\n os << '}';\n return os;\n}\ntemplate <typename T> ostream& operator<<(ostream& os, const deque<T>& v) {\n for (int i = 0; i < (int)v.size(); i++) {\n os << v[i] << (i + 1 == (int)v.size() ? \"\" : \" \");\n }\n return os;\n}\n\ntemplate <int i, typename T> void print_tuple(ostream&, const T&) {}\ntemplate <int i, typename T, typename H, class... Args> void print_tuple(ostream& os, const T& t) {\n if (i) os << ',';\n os << get<i>(t);\n print_tuple<i + 1, T, Args...>(os, t);\n}\ntemplate <typename... Args> ostream& operator<<(ostream& os, const tuple<Args...>& t) {\n os << '{';\n print_tuple<0, tuple<Args...>, Args...>(os, t);\n return os << '}';\n}\n\nvoid debug_out() { cerr << '\\n'; }\ntemplate <class Head, class... Tail> void debug_out(Head&& head, Tail&&... tail) {\n cerr << head;\n if (sizeof...(Tail) > 0) cerr << \", \";\n debug_out(move(tail)...);\n}\n#ifdef LOCAL\n#define debug(...) \\\n cerr << \" \"; \\\n cerr << #__VA_ARGS__ << \" :[\" << __LINE__ << \":\" << __FUNCTION__ << \"]\" << '\\n'; \\\n cerr << \" \"; \\\n debug_out(__VA_ARGS__)\n#else\n#define debug(...) 42\n#endif\n\ntemplate <typename T> T gcd(T x, T y) { return y != 0 ? gcd(y, x % y) : x; }\ntemplate <typename T> T lcm(T x, T y) { return x / gcd(x, y) * y; }\n\nint topbit(signed t) { return t == 0 ? -1 : 31 - __builtin_clz(t); }\nint topbit(long long t) { return t == 0 ? -1 : 63 - __builtin_clzll(t); }\nint botbit(signed a) { return a == 0 ? 32 : __builtin_ctz(a); }\nint botbit(long long a) { return a == 0 ? 64 : __builtin_ctzll(a); }\nint popcount(signed t) { return __builtin_popcount(t); }\nint popcount(long long t) { return __builtin_popcountll(t); }\nbool ispow2(int i) { return i && (i & -i) == i; }\n\ntemplate <class T> T ceil(T x, T y) {\n assert(y >= 1);\n return (x > 0 ? (x + y - 1) / y : x / y);\n}\ntemplate <class T> T floor(T x, T y) {\n assert(y >= 1);\n return (x > 0 ? x / y : (x - y + 1) / y);\n}\n\ntemplate <class T1, class T2> inline bool chmin(T1& a, T2 b) {\n if (a > b) {\n a = b;\n return true;\n }\n return false;\n}\ntemplate <class T1, class T2> inline bool chmax(T1& a, T2 b) {\n if (a < b) {\n a = b;\n return true;\n }\n return false;\n}\n#pragma endregion\n\n#include <algorithm>\n#include <cassert>\n#include <cmath>\n#include <iostream>\n#include <tuple>\n#include <vector>\n\nnamespace geometry {\nusing Real = double;\nconstexpr Real EPS = 1e-8;\nconstexpr Real PI = 3.14159265358979323846L;\n\ninline int sgn(Real x) { return x < -EPS ? -1 : x > EPS ? 1 : 0; }\n\ninline int compare(Real a, Real b) { return sgn(a - b); }\n\ninline bool equals(Real a, Real b) { return compare(a, b) == 0; }\n\nstruct Point {\n Real x, y;\n\n Point() {}\n\n Point(Real x, Real y) : x(x), y(y) {}\n\n Point& operator+=(const Point& p) {\n x += p.x, y += p.y;\n return *this;\n }\n\n Point& operator-=(const Point& p) {\n x -= p.x, y -= p.y;\n return *this;\n }\n\n Point& operator*=(const Real& k) {\n x *= k, y *= k;\n return *this;\n }\n\n Point& operator/=(const Real& k) {\n x /= k, y /= k;\n return *this;\n }\n\n Point operator+(const Point& p) const { return Point(*this) += p; }\n\n Point operator-(const Point& p) const { return Point(*this) -= p; }\n\n Point operator*(const Real& k) const { return Point(*this) *= k; }\n\n Point operator/(const Real& k) const { return Point(*this) /= k; }\n\n Point operator-() const { return Point(-x, -y); }\n\n bool operator==(const Point& p) const { return (compare(x, p.x) == 0 && compare(y, p.y) == 0); }\n\n bool operator!=(const Point& p) const { return !(*this == p); }\n\n bool operator<(const Point& p) const {\n return compare(x, p.x) < 0 || (compare(x, p.x) == 0 && compare(y, p.y) < 0);\n }\n\n bool operator>(const Point& p) const {\n return compare(x, p.x) > 0 || (compare(x, p.x) == 0 && compare(y, p.y) > 0);\n }\n\n friend std::istream& operator>>(std::istream& is, Point& p) { return is >> p.x >> p.y; }\n\n friend std::ostream& operator<<(std::ostream& os, const Point& p) { return os << p.x << ' ' << p.y; }\n\n Real norm() const { return x * x + y * y; }\n\n Real abs() const { return std::hypot(x, y); }\n\n Real arg() const { return std::atan2(y, x); }\n\n Point normal() const { return Point(-y, x); }\n\n Point unit() const { return *this / abs(); }\n\n Point rotate(Real theta) const {\n return Point(x * std::cos(theta) - y * std::sin(theta), x * std::sin(theta) + y * std::cos(theta));\n }\n};\n\nPoint polar(const Real& r, const Real& theta) { return Point(cos(theta), sin(theta)) * r; }\n\nReal dot(const Point& p, const Point& q) { return p.x * q.x + p.y * q.y; }\n\nReal cross(const Point& p, const Point& q) { return p.x * q.y - p.y * q.x; }\n\nReal distance(const Point& p, const Point& q) { return (p - q).abs(); }\n\nstruct Line {\n Point a, b;\n\n Line() {}\n\n Line(const Point& a, const Point& b) : a(a), b(b) {}\n\n Line(const Real& A, const Real& B, const Real& C) { // Ax + By = c\n if (equals(A, 0)) {\n assert(!equals(B, 0));\n a = Point(0, C / B);\n b = Point(1, C / B);\n } else if (equals(B, 0)) {\n a = Point(C / A, 0);\n b = Point(C / A, 1);\n } else {\n a = Point(0, C / B);\n b = Point(C / A, 0);\n }\n }\n\n friend std::istream& operator>>(std::istream& is, Line& l) { return is >> l.a >> l.b; }\n\n friend std::ostream& operator<<(std::ostream& os, const Line& l) { return os << l.a << \" to \" << l.b; }\n\n Point diff() const { return b - a; }\n};\n\nstruct Segment : Line {\n Segment() {}\n\n Segment(Point a, Point b) : Line(a, b) {}\n\n Real length() const { return diff().abs(); }\n};\n\nPoint proj(const Line& l, const Point& p) {\n Point v = l.diff().unit();\n return l.a + v * dot(v, p - l.a);\n}\n\nPoint refl(const Line& l, const Point& p) {\n Point h = proj(l, p);\n return h + (h - p);\n}\n\nbool orthogonal(const Line& l, const Line& m) { return equals(dot(l.diff(), m.diff()), 0); }\n\nbool parallel(const Line& l, const Line& m) { return equals(cross(l.diff(), m.diff()), 0); }\n\nenum Position { COUNTER_CLOCKWISE = +1, CLOCKWISE = -1, ONLINE_BACK = +2, ONLINE_FRONT = -2, ON_SEGMENT = 0 };\n\nPosition ccw(const Point& a, Point b, Point c) {\n b -= a, c -= a;\n if (sgn(cross(b, c)) == +1) return COUNTER_CLOCKWISE;\n if (sgn(cross(b, c)) == -1) return CLOCKWISE;\n if (sgn(dot(b, c)) == -1) return ONLINE_BACK;\n if (compare(b.norm(), c.norm()) == -1) return ONLINE_FRONT;\n return ON_SEGMENT;\n}\n\nbool intersect(const Line& l, const Point& p) { return abs(ccw(l.a, l.b, p)) != 1; }\n\nbool intersect(const Line& l, const Line& m) {\n Real A = cross(l.diff(), m.diff()), B = cross(l.diff(), l.b - m.a);\n if (equals(A, 0) && equals(B, 0)) return true;\n return !parallel(l, m);\n}\n\nbool intersect(const Line& l, const Segment& s) {\n return sgn(cross(l.diff(), s.a - l.a)) * sgn(cross(l.diff(), s.b - l.a)) <= 0;\n}\n\nbool intersect(const Segment& s, const Point& p) { return ccw(s.a, s.b, p) == ON_SEGMENT; }\n\nbool intersect(const Segment& s, const Segment& t) {\n 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;\n}\n\nReal distance(const Line& l, const Point& p) { return distance(p, proj(l, p)); }\n\nReal distance(const Line& l, const Line& m) { return intersect(l, m) ? 0 : distance(l, m.a); }\n\nReal distance(const Line& l, const Segment& s) {\n return intersect(l, s) ? 0 : std::min(distance(l, s.a), distance(l, s.b));\n}\n\nReal distance(const Segment& s, const Point& p) {\n Point h = proj(s, p);\n return intersect(s, h) ? distance(p, h) : std::min(distance(p, s.a), distance(p, s.b));\n}\n\nReal distance(const Segment& s, const Segment& t) {\n if (intersect(s, t)) return 0;\n return std::min({distance(s, t.a), distance(s, t.b), distance(t, s.a), distance(t, s.b)});\n}\n\nPoint crosspoint(const Line& l, const Line& m) {\n assert(intersect(l, m));\n Real A = cross(l.diff(), m.diff()), B = cross(l.diff(), l.b - m.a);\n if (equals(A, 0) && equals(B, 0)) return m.a;\n return m.a + m.diff() * B / A;\n}\n\nstruct Circle {\n Point center;\n Real radius;\n\n Circle() {}\n\n Circle(const Point& center, const Real& radius) : center(center), radius(radius) {}\n\n friend std::istream& operator>>(std::istream& is, Circle& c) { return is >> c.center >> c.radius; }\n\n friend std::ostream& operator<<(std::ostream& os, const Circle& c) { return os << c.center << ' ' << c.radius; }\n};\n\nbool intersect(const Circle& c, const Line& l) { return compare(c.radius, distance(l, c.center)) >= 0; }\n\nint intersect(const Circle& c, const Segment& s) {\n Point h = proj(s, c.center);\n if (compare(distance(c.center, h), c.radius) > 0) return 0;\n Real d1 = (c.center - s.a).abs(), d2 = (c.center - s.b).abs();\n if (compare(c.radius, d1) >= 0 && compare(c.radius, d2) >= 0) return 0;\n if (compare(c.radius, d1) * compare(c.radius, d2) < 0) return 1;\n if (sgn(dot(s.a - h, s.b - h)) < 0) return 2;\n return 0;\n}\n\nstd::vector<Point> crosspoint(const Circle& c, const Line& l) {\n Point h = proj(l, c.center);\n Real d = c.radius * c.radius - (c.center - h).norm();\n if (sgn(d) < 0) return {};\n if (sgn(d) == 0) return {h};\n Point v = l.diff().unit() * sqrt(d);\n return {h - v, h + v};\n}\n\nstd::vector<Point> crosspoint(const Circle& c, const Segment& s) {\n int num = intersect(c, s);\n if (num == 0) return {};\n auto res = crosspoint(c, Line(s.a, s.b));\n if (num == 2) return res;\n if (sgn(dot(s.a - res[0], s.b - res[0])) > 0) std::swap(res[0], res[1]);\n return {res[0]};\n}\n\n// requirement : c != d\nstd::vector<Point> crosspoint(const Circle& c1, const Circle& c2) {\n Real r1 = c1.radius, r2 = c2.radius;\n if (r1 < r2) return crosspoint(c2, c1);\n Real d = distance(c1.center, c2.center);\n if (compare(d, r1 + r2) > 0 || compare(d, r1 - r2) < 0) return {};\n Real alpha = std::acos((r1 * r1 + d * d - r2 * r2) / (2 * r1 * d));\n Real theta = (c2.center - c1.center).arg();\n Point p = c1.center + polar(r1, theta + alpha);\n Point q = c1.center + polar(r1, theta - alpha);\n if (p == q) return {p};\n return {p, q};\n}\n\nLine bisector(const Point& p, const Point& q) {\n Point c = (p + q) * 0.5;\n Point v = (q - p).normal();\n return Line(c - v, c + v);\n}\n\nCircle circumcircle(Point a, Point b, const Point& c) {\n Point center = crosspoint(bisector(a, c), bisector(b, c));\n return Circle(center, distance(c, center));\n}\n\nstd::vector<Point> center_given_radius(const Point& a, const Point& b, const Real& r) {\n Point m = (b - a) * 0.5;\n Real d1 = m.abs();\n if (equals(d1, 0) || d1 > r) return {};\n Real d2 = sqrt(r * r - d1 * d1);\n Point n = m.normal() * d2 / d1;\n Point p = a + m - n, q = a + m + n;\n if (p == q) return {p};\n return {p, q};\n}\n\nint count_tangent(const Circle& c1, const Circle& c2) {\n Real r1 = c1.radius, r2 = c2.radius;\n if (r1 < r2) return count_tangent(c2, c1);\n Real d = distance(c1.center, c2.center);\n if (compare(d, r1 + r2) > 0) return 4;\n if (compare(d, r1 + r2) == 0) return 3;\n if (compare(d, r1 - r2) > 0) return 2;\n if (compare(d, r1 - r2) == 0) return 1;\n return 0;\n}\n\nstd::vector<Point> tangent_to_circle(const Circle& c, const Point& p) {\n return crosspoint(c, Circle(p, sqrt((c.center - p).norm() - c.radius * c.radius)));\n}\n\nenum Contain { OUT, ON, IN };\n\nstruct Polygon : std::vector<Point> {\n using std::vector<Point>::vector;\n\n Polygon(int n) : std::vector<Point>(n) {}\n\n std::vector<Segment> segments() const {\n assert(size() > 1);\n std::vector<Segment> segs;\n for (size_t i = 0; i < size(); i++) segs.emplace_back((*this)[i], (*this)[(i + 1) % size()]);\n return segs;\n }\n\n Real area() const {\n Real sum = 0;\n for (size_t i = 0; i < size(); i++) sum += cross((*this)[i], (*this)[(i + 1) % size()]);\n return std::abs(sum) / 2;\n }\n\n bool is_convex(bool accept_on_segment = true) const {\n int n = size();\n for (int i = 0; i < n; i++) {\n if (accept_on_segment) {\n if (ccw((*this)[i], (*this)[(i + 1) % n], (*this)[(i + 2) % n]) == CLOCKWISE) {\n return false;\n }\n } else {\n if (ccw((*this)[i], (*this)[(i + 1) % n], (*this)[(i + 2) % n]) != COUNTER_CLOCKWISE) {\n return false;\n }\n }\n }\n return true;\n }\n};\n\nContain contain(const Polygon& P, const Point& p) {\n bool in = false;\n for (size_t i = 0; i < P.size(); i++) {\n if (ccw(P[i], P[(i + 1) % P.size()], p) == ON_SEGMENT) return ON;\n Point a = P[i] - p, b = P[(i + 1) % P.size()] - p;\n if (a.y > b.y) std::swap(a, b);\n if (sgn(a.y) <= 0 && sgn(b.y) > 0 && sgn(cross(a, b)) < 0) in = !in;\n }\n return in ? IN : OUT;\n}\n\nPolygon convex_hull(Polygon& P, bool accept_on_segment = false) {\n int n = P.size(), k = 0;\n if (n <= 2) return P;\n std::sort(P.begin(), P.end());\n Polygon ch(n * 2);\n auto check = [&](int i) {\n if (accept_on_segment) return ccw(ch[k - 2], ch[k - 1], P[i]) == CLOCKWISE;\n return ccw(ch[k - 2], ch[k - 1], P[i]) != COUNTER_CLOCKWISE;\n };\n for (int i = 0; i < n; ch[k++] = P[i++]) {\n while (k >= 2 && check(i)) {\n k--;\n }\n }\n for (int i = n - 2, t = k + 1; i >= 0; ch[k++] = P[i--]) {\n while (k >= t && check(i)) {\n k--;\n }\n }\n ch.resize(k - 1);\n int start = 0;\n for (int i = 1; i < k - 1; i++) {\n if (equals(ch[i].y, ch[start].y) ? ch[i].x < ch[start].x : ch[i].y < ch[start].y) {\n start = i;\n }\n }\n std::rotate(ch.begin(), ch.begin() + start, ch.end());\n return ch;\n}\n\nstd::tuple<int, int, Real> convex_diameter(const Polygon& convex) {\n assert(convex.is_convex());\n int n = convex.size();\n Real max_dist = -1;\n std::pair<int, int> argmax = {-1, -1};\n for (int i = 0, j = 0; i < n; i++) {\n while (j + 1 < n && distance(convex[i], convex[j + 1]) > distance(convex[i], convex[j])) j++;\n double cur_dist = distance(convex[i], convex[j]);\n if (max_dist < cur_dist) {\n max_dist = cur_dist;\n argmax = {i, j};\n }\n }\n return {argmax.first, argmax.second, max_dist};\n}\n\nPolygon convex_cut(const Polygon& convex, const Line& l) {\n assert(convex.is_convex());\n int n = convex.size();\n Polygon res;\n for (int i = 0; i < n; i++) {\n const Point& cur = convex[i];\n const Point& nxt = convex[(i + 1) % n];\n if (ccw(l.a, l.b, cur) != CLOCKWISE) res.emplace_back(cur);\n if (ccw(l.a, l.b, cur) * ccw(l.a, l.b, nxt) < 0) res.emplace_back(crosspoint(Segment(cur, nxt), l));\n }\n return res;\n}\n\nPolygon voronoi(const Polygon& P, const std::vector<Point>& ps, size_t idx) {\n Polygon res = P;\n for (size_t i = 0; i < ps.size(); i++) {\n if (i == idx) continue;\n res = convex_cut(res, bisector(ps[idx], ps[i]));\n }\n return res;\n}\n\n} // namespace geometry\n\n/**\n * @brief 2 次元幾何ライブラリ\n * @docs docs/geometry/geometry.md\n */\n\nconst int INF = 1e9;\nconst long long IINF = 1e18;\nconst int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};\nconst char dir[4] = {'D', 'R', 'U', 'L'};\nconst long long MOD = 1000000007;\n// const long long MOD = 998244353;\n\nusing namespace geometry;\n\nvoid solve(int n) {\n Polygon P(n);\n for (auto& p : P) cin >> p;\n\n auto check = [&](double x) {\n Polygon Q = P;\n for (int i = 0; i < n; i++) {\n Point to = P[(i + 1) % n] - P[i];\n Point move = to.normal().unit() * x;\n Segment seg(P[i] + move, P[(i + 1) % n] + move);\n Q = convex_cut(Q, seg);\n }\n return Q.empty();\n };\n\n double lb = 0, ub = 10000;\n for (int _ = 0; _ < 300; _++) {\n double mid = (lb + ub) / 2;\n (check(mid) ? ub : lb) = mid;\n }\n\n cout << ub << '\\n';\n}\n\nint main() {\n cin.tie(0);\n ios::sync_with_stdio(false);\n cout << fixed << setprecision(10);\n int n;\n while (cin >> n, n) solve(n);\n return 0;\n}", "accuracy": 1, "time_ms": 60, "memory_kb": 3536, "score_of_the_acc": -0.7065, "final_rank": 7 }, { "submission_id": "aoj_1283_5600257", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\n//geometry library by maine\n\n//basic\nusing Real = double;\nusing Point = complex<Real>;\n#define x real()\n#define y imag()\nconst Real EPS = 1e-7, PI = acos(-1), INF = 1e10;\n\nistream& operator>>(istream& is, Point& p) {\n Real a, b;\n is >> a >> b;\n p = Point(a, b);\n return is;\n}\nostream& operator<<(ostream& os, Point p) {\n return os << fixed << setprecision(10) << p.x << \" \" << p.y;\n}\n\nPoint operator*(const Point& p, const Real& d) { return Point(p.x * d, p.y * d); }\n\nbool eq(const Real& r, const Real& s) { return abs(r - s) < EPS; }\nbool eq(const Point& p, const Point& q) { return abs(p - q) < EPS; }\n\nnamespace std {\nbool operator<(const Point& a, const Point& b) {\n return !eq(a.x, b.x) ? a.x < b.x : a.y < b.y;\n}\n} // namespace std\n\nReal get_angle(const Point& a, const Point& b, const Point& c) {\n return arg((c - a) / (b - a));\n}\n\ninline int inc(int i, int N) {\n return (i == N - 1 ? 0 : i + 1);\n}\ninline int dec(int i, int N) {\n return (i == 0 ? N - 1 : i - 1);\n}\n\n//unit vector\nPoint e(const Point& p) { return p / abs(p); }\n\n//angle transformation\nReal radian_to_degree(Real r) { return r * 180.0 / PI; }\nReal degree_to_radian(Real d) { return d * PI / 180.0; }\n\n//basic functions\nReal dot(const Point& p, const Point& q) { return p.x * q.x + p.y * q.y; }\nReal cross(const Point& p, const Point& q) { return p.x * q.y - p.y * q.x; }\nPoint rotate(const Real& theta, const Point& p) {\n return {cos(theta) * p.x - sin(theta) * p.y, sin(theta) * p.x + cos(theta) * p.y};\n}\nPoint rotate90(const Point& p) {\n return {-p.y, p.x};\n}\n\n//structs : Line, Segment, Circle\nstruct Line {\n Point a, b;\n Line() = default;\n Line(Point a, Point b) : a(a), b(b) {}\n Point d() const { return e(b - a); } //direction vector\n Point n() const {\n Point dd = d();\n return {dd.y, -dd.x};\n } //normal vector\n};\nstruct Segment : Line {\n Segment() = default;\n Segment(Point a, Point b) : Line(a, b) {}\n};\nstruct Circle {\n Point p;\n Real r;\n Circle() = default;\n Circle(Point p, Real r) : p(p), r(r){};\n};\nusing Points = vector<Point>;\nusing Polygon = vector<Point>;\nusing Polygons = vector<Polygon>;\nusing Lines = vector<Line>;\nusing Segments = vector<Segment>;\nusing Circles = vector<Circle>;\n\n//happy functions\n//https://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_1_C\nint ccw(const Point& a, const Point& bb, const Point& cc) {\n Point b = bb - a, c = cc - a;\n if (cross(b, c) > EPS)\n return 1; //COUNTER_CLOCKWISE\n if (cross(b, c) < -EPS)\n return -1; //CLOCKWISE\n if (dot(b, c) < 0)\n return 2; //ONLINE_BACK c-a-b\n if (norm(b) < norm(c))\n return -2; //ONLINE_FRONT a-b-c\n return 0; //ON_SEGMENT a-c-b\n}\n\nbool parallel(const Line& l, const Line& m) { return eq(cross(l.d(), m.d()), 0.0); }\nbool orthogonal(const Line& l, const Line& m) { return eq(dot(l.d(), m.d()), 0.0); }\nPoint projection(const Line& l, const Point& p) { return l.a + (l.a - l.b) * (dot(p - l.a, l.a - l.b) / norm(l.a - l.b)); }\nPoint reflection(const Line& l, const Point& p) { return p + (projection(l, p) - p) * 2.0; }\n\n//intersect\nbool intersect(const Line& l, const Point& p) { return abs(ccw(l.a, l.b, p)) != 1; }\nbool intersect(const Line& l, const Line& m) { return !parallel(l, m) || (parallel(l, m) && intersect(l, m.a)); }\nbool intersect(const Segment& s, const Point& p) { return ccw(s.a, s.b, p) == 0; }\nbool intersect(const Line& l, const Segment& s) { return cross(l.d(), s.a - l.a) * cross(l.d(), s.b - l.a) < EPS; }\nbool intersect(const Segment& s, const Segment& t) {\n 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;\n}\nbool intersect_onsegment(const Segment& s, const Point& p) { return intersect(s, p) && !eq(s.a, p) && !eq(s.b, p); }\n\n//distance\nReal distance(const Point& p, const Point& q) { return abs(p - q); }\nReal distance(const Line& l, const Point& p) { return distance(p, projection(l, p)); }\nReal distance(const Line& l, const Line& m) { return intersect(l, m) ? 0 : distance(l, m.a); }\nReal distance(const Segment& s, const Point& p) {\n Point q = projection(s, p);\n return intersect(s, q) ? distance(p, q) : min(distance(s.a, p), distance(s.b, p));\n}\nReal distance(const Segment& s, const Segment& t) { return intersect(s, t) ? 0 : min({distance(s, t.a), distance(s, t.b), distance(t, s.a), distance(t, s.b)}); }\nReal distance(const Line& l, const Segment& s) { return intersect(l, s) ? 0 : min(distance(l, s.a), distance(l, s.b)); }\n\n//intersect circle\nbool intersect(const Circle& c, const Point& p) { return eq(distance(c.p, p), c.r); }\nbool intersect(const Circle& c, const Line& l) { return distance(l, c.p) <= c.r + EPS; }\nint intersect(const Circle& c, const Segment& s) {\n if (!intersect(c, Line(s)))\n return 0;\n Real da = distance(c.p, s.a), db = distance(c.p, s.b);\n if (da < c.r + EPS && db < c.r + EPS)\n return 0;\n if (da > db)\n swap(da, db);\n if (da < c.r - EPS && db > c.r + EPS)\n return 1;\n if (intersect(s, projection(s, c.p)))\n return 2;\n return 0;\n}\nint intersect(Circle c1, Circle c2) {\n if (c1.r < c2.r)\n swap(c1, c2);\n Real d = distance(c1.p, c2.p);\n if (c1.r + c2.r < d)\n return 4;\n if (eq(c1.r + c2.r, d))\n return 3;\n if (c1.r - c2.r < d)\n return 2;\n if (eq(c1.r - c2.r, d))\n return 1;\n return 0;\n}\n\n//crosspoint\nPoint crosspoint(const Line& l, const Line& m) {\n Real A = cross(l.b - l.a, m.b - m.a);\n Real B = cross(l.b - l.a, l.b - m.a);\n if (eq(abs(A), 0.0) && eq(abs(B), 0.0))\n return m.a;\n return m.a + (m.b - m.a) * B / A;\n}\nPoint crosspoint(const Segment& s, const Segment& t) { return crosspoint(Line(s), Line(t)); }\npair<Point, Point> crosspoint(const Circle& c, const Line& l) {\n Point p = projection(l, c.p);\n if (eq(distance(l, c.p), c.r))\n return {p, p};\n Real len = sqrt(c.r * c.r - norm(p - c.p));\n return {p - len * l.d(), p + len * l.d()};\n}\npair<Point, Point> crosspoint(const Circle& c, const Segment& s) {\n Line l(s);\n if (intersect(c, s) == 2)\n return crosspoint(c, l);\n auto ret = crosspoint(c, l);\n if (dot(s.a - ret.first, s.b - ret.first) < 0)\n ret.second = ret.first;\n else\n ret.first = ret.second;\n return ret;\n}\npair<Point, Point> crosspoint(const Circle& c1, const Circle& c2) {\n Real d = distance(c1.p, c2.p);\n Real a = acos((c1.r * c1.r + d * d - c2.r * c2.r) / (2 * c1.r * d));\n Real t = atan2(c2.p.y - c1.p.y, c2.p.x - c1.p.x);\n Point p1 = c1.p + Point(cos(t + a) * c1.r, sin(t + a) * c1.r);\n Point p2 = c1.p + Point(cos(t - a) * c1.r, sin(t - a) * c1.r);\n return {p1, p2};\n}\n\n//tangent\npair<Point, Point> tangent(const Circle& c, const Point& p) {\n return crosspoint(c, Circle(p, sqrt(norm(c.p - p) - c.r * c.r)));\n}\nLines tangent(Circle c1, Circle c2) {\n Lines ret;\n if (c1.r < c2.r)\n swap(c1, c2);\n if (eq(c1.p, c2.p))\n return ret;\n Real g = norm(c1.p - c2.p);\n Point u = (c2.p - c1.p) / sqrt(g);\n Point v = rotate(PI / 2, u);\n for (int s : {-1, 1}) {\n Real h = (c1.r + s * c2.r) / sqrt(g);\n if (eq(1 - h * h, 0)) {\n ret.emplace_back(c1.p + u * c1.r, c1.p + (u + v) * c1.r);\n } else if (1 - h * h > 0) {\n Point uu = u * h, vv = v * sqrt(1 - h * h);\n ret.emplace_back(c1.p + (uu + vv) * c1.r, c2.p - (uu + vv) * c2.r * s);\n ret.emplace_back(c1.p + (uu - vv) * c1.r, c2.p - (uu - vv) * c2.r * s);\n }\n }\n return ret;\n}\n\n//convex\n//https://onlinejudge.u-aizu.ac.jp/courses/library/4/CGL/3/CGL_3_B\nbool is_convex(const Polygon& P) {\n int N = (int)P.size();\n for (int i = 0; i < N; i++) {\n if (ccw(P[i], P[(i + 1) % N], P[(i + 2) % N]) == -1)\n return false;\n }\n return true;\n}\n//https://onlinejudge.u-aizu.ac.jp/courses/library/4/CGL/4/CGL_4_A\nPolygon convex_hull(Points P) {\n int N = (int)P.size(), k = 0;\n if (N <= 2)\n return P;\n sort(begin(P), end(P));\n Points ret(2 * N);\n for (int i = 0; i < N; ret[k++] = P[i++]) {\n while (k >= 2 && cross(ret[k - 1] - ret[k - 2], P[i] - ret[k - 1]) < EPS)\n --k;\n }\n for (int i = N - 2, t = k + 1; i >= 0; ret[k++] = P[i--]) {\n while (k >= t && cross(ret[k - 1] - ret[k - 2], P[i] - ret[k - 1]) < EPS)\n --k;\n }\n ret.resize(k - 1);\n return ret;\n}\n//https://onlinejudge.u-aizu.ac.jp/courses/library/4/CGL/4/CGL_4_B\nReal convex_diameter(const Polygon& P) {\n int N = (int)P.size();\n int is = 0, js = 0;\n for (int i = 1; i < N; i++) {\n if (P[i].y < P[is].y)\n is = i;\n if (P[i].y > P[js].y)\n js = i;\n }\n Real maxdist = norm(P[is] - P[js]);\n int maxi, maxj, i, j;\n maxi = i = is;\n maxj = j = js;\n do {\n if (cross(P[inc(i, N)] - P[i], P[inc(j, N)] - P[j]) >= 0)\n j = inc(j, N);\n else\n i = inc(i, N);\n if (norm(P[i] - P[j]) > maxdist) {\n maxdist = norm(P[i] - P[j]);\n maxi = i;\n maxj = j;\n }\n } while (i != is || j != js);\n maxi = maxi;\n maxj = maxj;\n return sqrt(maxdist);\n}\n//https://onlinejudge.u-aizu.ac.jp/courses/library/4/CGL/4/CGL_4_C\nPolygon convex_cut(const Polygon& P, const Line& l) {\n Polygon ret;\n int N = (int)P.size();\n for (int i = 0; i < N; i++) {\n Point now = P[i], nxt = P[(inc(i, N))];\n if (ccw(l.a, l.b, now) != -1)\n ret.emplace_back(now);\n if (ccw(l.a, l.b, now) * ccw(l.a, l.b, nxt) == -1)\n ret.emplace_back(crosspoint(Line(now, nxt), l));\n }\n return ret;\n}\n\n//other\nenum\n{\n OUT,\n ON,\n IN\n};\n\n//https://onlinejudge.u-aizu.ac.jp/courses/library/4/CGL/3/CGL_3_C\nint contains(const Polygon& Q, const Point& p) {\n bool in = false;\n int N = (int)Q.size();\n for (int i = 0; i < N; i++) {\n Point a = Q[i] - p, b = Q[(i + 1) % N] - p;\n if (a.y > b.y)\n swap(a, b);\n if (a.y <= 0 && 0 < b.y && cross(a, b) < 0)\n in = !in;\n if (cross(a, b) == 0 && dot(a, b) <= 0)\n return ON;\n }\n return in ? IN : OUT;\n}\n\n//https://onlinejudge.u-aizu.ac.jp/courses/library/4/CGL/3/CGL_3_A\nReal area(const Polygon& P) {\n Real A = 0;\n for (int i = 0; i < (int)P.size(); i++) {\n A += cross(P[i], P[(i + 1) % P.size()]);\n }\n return A * 0.5;\n}\n\n//https://onlinejudge.u-aizu.ac.jp/courses/library/4/CGL/7/CGL_7_H\nReal cross_area(const Circle& c, const Point& a, const Point& b) {\n Point va = c.p - a, vb = c.p - b;\n Real f = cross(va, vb), ret = 0.0;\n if (eq(f, 0.0))\n return ret;\n if (max(abs(va), abs(vb)) < c.r + EPS)\n return f;\n if (distance(Segment(a, b), c.p) > c.r - EPS)\n return c.r * c.r * arg(vb * conj(va));\n auto u = crosspoint(c, Segment(a, b));\n vector<Point> V{a, u.first, u.second, b};\n for (int i = 0; i < (int)V.size() - 1; i++) {\n ret += cross_area(c, V[i], V[i + 1]);\n }\n return ret;\n}\nReal area(const Polygon& p, const Circle& c) {\n int N = (int)p.size();\n if (N < 3)\n return 0.0;\n Real ret = 0;\n for (int i = 0; i < N; i++) {\n ret += cross_area(c, p[i], p[inc(i, N)]);\n }\n return ret / 2.0;\n}\n\n//https://onlinejudge.u-aizu.ac.jp/problems/CGL_5_A\nReal closest_pair(Points& P, int left, int right) {\n if (right - left <= 1)\n return 1e18;\n int mid = (left + right) / 2;\n Real xx = P[mid].x;\n Real dist = min(closest_pair(P, left, mid), closest_pair(P, mid, right));\n inplace_merge(P.begin() + left, P.begin() + mid, P.begin() + right, [&](const Point& a, const Point& b) { return a.y < b.y; });\n Points memo;\n memo.reserve(right - left);\n for (int i = left; i < right; i++) {\n if (abs(P[i].x - xx) >= dist)\n continue;\n for (int j = (int)memo.size() - 1; j >= 0; j--) {\n if (P[i].y - memo[j].y >= dist)\n break;\n dist = min(dist, distance(P[i], memo[j]));\n }\n memo.emplace_back(P[i]);\n }\n return dist;\n}\nReal closest_pair(Points P) {\n sort(begin(P), end(P));\n return closest_pair(P, 0, (int)P.size());\n}\n\n//https://onlinejudge.u-aizu.ac.jp/courses/library/4/CGL/7/CGL_7_B\nCircle incircle(Polygon P) {\n assert((int)P.size() == 3);\n Real l0 = distance(P[1], P[2]), l1 = distance(P[0], P[2]), l2 = distance(P[0], P[1]);\n Circle c;\n c.p = crosspoint(Line(P[0], (P[1] * l1 + P[2] * l2) / (l1 + l2)), Line(P[1], (P[0] * l0 + P[2] * l2) / (l0 + l2)));\n c.r = distance(Line(P[0], P[1]), c.p);\n return c;\n}\n\n//https://onlinejudge.u-aizu.ac.jp/courses/library/4/CGL/7/CGL_7_C\nCircle circumcircle(Polygon P) {\n assert((int)P.size() == 3);\n Circle c;\n c.p = crosspoint(Line((P[0] + P[1]) / 2.0, (P[0] + P[1]) / 2.0 + rotate90(P[0] - P[1])),\n Line((P[0] + P[2]) / 2.0, (P[0] + P[2]) / 2.0 + rotate90(P[0] - P[2])));\n c.r = distance(c.p, P[0]);\n return c;\n}\n\nint main() {\n int N;\n while (cin >> N, N) {\n Polygon P(N);\n for (auto&& p : P) {\n cin >> p;\n }\n double ans = 0;\n for (int i = 0; i < N; i++) {\n for (int j = inc(i, N); j != i; j = inc(j, N)) {\n for (int k = inc(j, N); k != i; k = inc(k, N)) {\n Line l{P[i], P[inc(i, N)]}, m{P[j], P[inc(j, N)]}, n{P[k], P[inc(k, N)]};\n auto check = [&](Circle C) {\n if (contains(P, C.p)) {\n for (int q = 0; q < N; q++) {\n Line w{P[q], P[inc(q, N)]};\n if (C.r > distance(w, C.p) + EPS) {\n return false;\n }\n }\n return true;\n }\n return false;\n };\n if (intersect(l, m) && intersect(m, n) && intersect(l, n)) {\n Polygon Tri = {crosspoint(l, m), crosspoint(m, n), crosspoint(n, l)};\n Circle C = incircle(Tri);\n if (check(C))\n ans = max(ans, C.r);\n }\n if (parallel(l, n)) {\n Line mid{(l.a + n.b) / 2.0, (l.b + n.a) / 2.0};\n Circle C;\n C.r = distance(l, n) / 2.0;\n for (int _ = -1; _ <= 1; _ += 2) {\n C.p = crosspoint(mid, Line{m.a + m.n() * C.r * _, m.b + m.n() * C.r * _});\n if (check(C))\n ans = max(ans, C.r);\n }\n }\n }\n }\n }\n cout << fixed << setprecision(6) << ans << endl;\n }\n}", "accuracy": 1, "time_ms": 830, "memory_kb": 3600, "score_of_the_acc": -0.9608, "final_rank": 12 }, { "submission_id": "aoj_1283_5022363", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\nusing ll = long long;\nusing PII = pair<ll, ll>;\n\n#define FOR(i, a, n) for (ll i = (ll)a; i < (ll)n; ++i)\n#define REP(i, n) FOR(i, 0, n)\n#define ALL(x) x.begin(), x.end()\ntemplate <typename T> void chmin(T &a, const T &b) { a = min(a, b); }\ntemplate <typename T> void chmax(T &a, const T &b) { a = max(a, b); }\n\nconst ll INF = 1LL << 60;\n\nstruct FastIO {\n FastIO() {\n cin.tie(0);\n ios::sync_with_stdio(0);\n }\n} fastiofastio;\n\nnamespace std {\nbool operator<(const complex<long double> &a, const complex<long double> &b);\nbool operator==(const complex<long double> &a, const complex<long double> &b);\nbool cmp_y(const complex<long double> &a, const complex<long double> &b);\n} // namespace std\n\nnamespace Geometry {\nusing R = long double; // Rにmint渡せるようにする\nusing P = complex<R>;\nusing L = pair<P, P>;\nusing G = vector<P>;\nstruct C {\n P c;\n R r;\n C() {}\n C(const P &a, const R &b) : c(a), r(b) {}\n};\nstruct S : public L {\n S() {}\n S(const P &a, const P &b) : L(a, b) {}\n};\nconst R EPS = 1e-8;\nconst R PI = atan(1) * 4;\n\ninline int sgn(const R &r) { return (r > EPS) - (r < -EPS); }\ninline R dot(const P &a, const P &b) {\n return real(a) * real(b) + imag(a) * imag(b);\n}\ninline R det(const P &a, const P &b) {\n return real(a) * imag(b) - imag(a) * real(b);\n}\ninline P rotate(const P &p, const R &arg) {\n return P(cos(arg) * p.real() - sin(arg) * p.imag(),\n sin(arg) * p.real() + cos(arg) * p.imag());\n}\ninline P vec(const L &l) { return l.second - l.first; }\n\n// P,L,Sについて入力\ninline istream &operator>>(istream &is, P &p) {\n R x, y;\n is >> x >> y;\n p = P(x, y);\n return is;\n}\ninline istream &operator>>(istream &is, L &l) {\n P a, b;\n is >> a >> b;\n l = L(a, b);\n return is;\n}\ninline istream &operator>>(istream &is, S &s) {\n P a, b;\n is >> a >> b;\n s = S(a, b);\n return is;\n}\n\n// 線分abから見たcの位置\nenum CCW { LEFT = 1, RIGHT = 2, BACK = 4, FRONT = 8, ON_SEG = 16 };\nint ccw(P a, P b, P c) {\n P p = (c - a) / (b - a);\n if (sgn(imag(p)) > 0)\n return LEFT;\n if (sgn(imag(p)) < 0)\n return RIGHT;\n if (sgn(real(p)) < 0)\n return BACK;\n if (sgn(real(p) - 1) > 0)\n return FRONT;\n return ON_SEG;\n}\n\n// 射影\nP inline projection(const L &l, const P &p) {\n R t = dot(p - l.first, l.first - l.second) / norm(l.first - l.second);\n return l.first + t * (l.first - l.second);\n}\n// 反射\nP inline reflection(const L &l, const P &p) {\n return p + (R)2 * (projection(l, p) - p);\n}\n\n// 垂直,平行\ninline bool vertical(L a, L b) { return sgn(dot(vec(a), vec(b))) == 0; }\ninline bool parallel(L a, L b) { return sgn(det(vec(a), vec(b))) == 0; }\n\n// 交差判定\ntemplate <bool strict = false> inline bool intersect(const L &l1, const L &l2) {\n if (strict)\n return sgn(det(vec(l1), vec(l2))) != 0;\n return sgn(det(vec(l1), vec(l2))) != 0 || l1 == l2;\n}\ntemplate <bool strict = false> inline bool intersect(const L &l, const S &s) {\n if (strict)\n det(s.first, vec(l)) * det(s.second, vec(l)) < 0;\n return det(s.first, vec(l)) * det(s.second, vec(l)) <= 0;\n}\ntemplate <bool strict = false> inline bool intersect(const S &s1, const S &s2) {\n int ccw1 = ccw(s1.first, s1.second, s2.first) |\n ccw(s1.first, s1.second, s2.second);\n int ccw2 = ccw(s2.first, s2.second, s1.first) |\n ccw(s2.first, s2.second, s1.second);\n if (strict)\n return (ccw1 & ccw2) == (LEFT | RIGHT);\n return (ccw1 & ccw2) == (LEFT | RIGHT) || ((ccw1 | ccw2) & ON_SEG);\n}\ntemplate <bool strict = false> inline bool intersect(const S &s, const P &p) {\n return ccw(s.first, s.second, p) == ON_SEG;\n}\ntemplate <bool strict = false> inline bool intersect(const L &l, const P &p) {\n return ccw(l.first, l.second, p) == ON_SEG ||\n ccw(l.first, l.second, p) == FRONT ||\n ccw(l.first, l.second, p) == BACK;\n}\n\n// 距離\nR dist(const S &s, const P &p) {\n P q = projection(s, p);\n if (sgn(dot(s.second - s.first, p - s.first)) <= 0)\n q = s.first;\n if (sgn(dot(s.first - s.second, p - s.second)) <= 0)\n q = s.second;\n return abs(p - q);\n}\nR dist(const S &a, const S &b) {\n if (intersect(a, b))\n return 0;\n return min({dist(a, b.first), dist(a, b.second), dist(b, a.first),\n dist(b, a.second)});\n}\nR dist(const L &l, const P &p) {\n P q = projection(l, p);\n return abs(p - q);\n}\n\n// 交点 交差判定を先にすること!!!\ninline P crosspoint(const L &l1, const L &l2) {\n R ratio = det(vec(l2), l2.first - l1.first) / det(vec(l2), vec(l1));\n return l1.first + vec(l1) * ratio;\n}\n\n// 凸包 3点が一直線上に並ぶときに注意\n// 凸包のうち一番左にある頂点の中で一番下の頂点から時計回り\nG convex_hull(G ps) {\n int n = ps.size(), k = 0;\n sort(ps.begin(), ps.end());\n G r(2 * n);\n for (int i = 0; i < n; i++) {\n while (k > 1 && sgn(det(r[k - 1] - r[k - 2], ps[i] - r[k - 2])) < 0)\n k--;\n r[k++] = ps[i];\n }\n for (int i = n - 2, t = k; i >= 0; i--) {\n while (k > t && sgn(det(r[k - 1] - r[k - 2], ps[i] - r[k - 2])) < 0)\n k--;\n r[k++] = ps[i];\n }\n r.resize(k - 1);\n return r;\n}\n\n// 凸多角形polを直線lで切断して左側の多角形を返す\nG convex_cut(const G &pol, const L &l) {\n G res;\n REP(i, pol.size()) {\n P a = pol[i], b = pol[(i + 1) % pol.size()];\n int da = sgn(det(l.first - a, l.second - a)),\n db = sgn(det(l.first - b, l.second - b));\n // 点aが直線lの左側\n if (da >= 0)\n res.emplace_back(a);\n // 辺(a,b)と直線lが交わる\n if (da * db < 0)\n res.emplace_back(crosspoint(L{a, b}, l));\n }\n return res;\n}\n\n// 1直線上に3点が並んでるような部分を消去 O(p.size())\nG normalize_poligon(G p) {\n int n = p.size();\n REP(i, p.size()) {\n if (ccw(p[(i + n - 1) % n], p[i], p[(i + 1) % n]) == ON_SEG) {\n p.erase(p.begin() + i);\n i--;\n }\n }\n return p;\n}\n\n// abとacがなす角の二等分線\nL bisector_of_angle(P a, P b, P c) {\n b -= a;\n b /= abs(b);\n c -= a;\n c /= abs(c);\n P mid = (b + c) / P(2, 0);\n return L(a, a + mid);\n}\n\n// 点a,bの垂直二等分線 O(1)\nL bisector(P a, P b) {\n const P mid = (a + b) / P(2, 0);\n return L{mid, mid + (b - a) * P(0, 1)};\n}\n// 多角形polと点集合vについてボロノイ図を計算\n// 点v[s]が属する部分を返す O(pol.size * v.size())\nG voronoi_cell(G pol, G v, int s) {\n pol = normalize_poligon(pol);\n REP(i, v.size()) if (i != s) {\n pol = convex_cut(pol, bisector(v[s], v[i]));\n }\n return pol;\n}\n\n// 面積 頂点が反時計回りに並んでいること\nR area(const G &pol) {\n R ret = 0.0;\n REP(i, pol.size()) ret += det(pol[i], pol[(i + 1) % pol.size()]);\n return (ret / 2.0);\n}\n\n// 凸性の判定\nbool isConvex(const G &pol) {\n REP(i, pol.size()) {\n if (sgn(det(pol[(i + 1) % pol.size()] - pol[i],\n pol[(i + 2) % pol.size()] - pol[i])) < 0) {\n return false;\n }\n }\n return true;\n}\n\n// 多角形と点の内包\n// 2→in 1→on 0→out\nint inPolygon(const G &pol, const P &p) {\n bool in = false;\n for (int i = 0; i < pol.size(); ++i) {\n P a = pol[i] - p, b = pol[(i + 1) % pol.size()] - p;\n if (imag(a) > imag(b))\n swap(a, b);\n if (imag(a) <= 0 && 0 < imag(b) && sgn(det(a, b)) < 0) {\n in = !in;\n }\n if (sgn(det(a, b)) == 0 && sgn(dot(a, b)) <= 0)\n return 1;\n }\n return in ? 2 : 0;\n}\n\n// 3点が与えられたときに円を求める\n// 3点が直線上に並んでいるときは{0, 0, -1}を返す\nC calcCircle(R x1, R y1, R x2, R y2, R x3, R y3) {\n R a = x2 - x1, b = y2 - y1, c = x3 - x1, d = y3 - y1;\n if ((sgn(a) && sgn(d)) || (sgn(b) && sgn(c))) {\n R ox = x1 + (d * (a * a + b * b) - b * (c * c + d * d)) /\n (a * d - b * c) / 2,\n oy;\n if (b)\n oy = (a * (x1 + x2 - ox - ox) + b * (y1 + y2)) / b / 2;\n else\n oy = (c * (x1 + x3 - ox - ox) + d * (y1 + y3)) / d / 2;\n R r1 = sqrt((ox - x1) * (ox - x1) + (oy - y1) * (oy - y1)),\n r2 = sqrt((ox - x2) * (ox - x2) + (oy - y2) * (oy - y2)),\n r3 = sqrt((ox - x3) * (ox - x3) + (oy - y3) * (oy - y3)),\n r = (r1 + r2 + r3) / 3;\n return {P{ox, oy}, r};\n }\n return {P{0, 0}, -1};\n}\n// 2点p1, p2を通り、半径がrの円を2つ返す\nvector<C> calcCircle(P p1, P p2, R r) {\n if (abs(p1 - p2) > 2 * r)\n return {}; // 存在しない\n P p3 = {(p1.real() + p2.real()) / 2, (p1.imag() + p2.imag()) / 2};\n R l = abs(p1 - p3);\n P p1p2 = p2 - p1;\n R a = p1p2.real(), b = p1p2.imag();\n R dx1 = b * sqrt((r * r - l * l) / (a * a + b * b)),\n dy1 = a * sqrt((r * r - l * l) / (a * a + b * b));\n return {{{p3.real() + dx1, p3.imag() - dy1}, r},\n {{p3.real() - dx1, p3.imag() + dy1}, r}};\n}\n\n// 交差\nint intersect(const C &c, const L &l) {\n R d = dist(l, c.c);\n if (sgn(c.r - d) > 0)\n return 2; // 交差している\n else if (sgn(c.r - d) == 0)\n return 1; // 接している\n return 0; // 交差しない\n}\nint intersect(const C &a, const C &b) {\n R dist = norm(a.c - b.c), r1 = (a.r + b.r) * (a.r + b.r),\n r2 = (a.r - b.r) * (a.r - b.r);\n if (sgn(r1 - dist) < 0)\n return 4; // 円が離れている\n if (sgn(r1 - dist) == 0)\n return 3; // 外接\n if (sgn(r2 - dist) < 0 && sgn(dist - r1) < 0)\n return 2; // 交差\n if (sgn(dist - r2) == 0)\n return 1; // 内接\n return 0; // 内部に含む\n}\n\n// 交点 交差の確認を先にすること!!!\nvector<P> crosspoint(C c, L l) {\n R d = dist(l, c.c), r = c.r;\n P m = projection(l, c.c);\n P x = sqrt(r * r - d * d) * vec(l) / abs(vec(l));\n vector<P> ret(2, m);\n ret[0] -= x;\n ret[1] += x;\n\n if (ret[1] < ret[0])\n swap(ret[0], ret[1]);\n return ret;\n}\n// intersect=2 を確認すること\nvector<P> crosspoint(C a, C b) {\n R d = abs(a.c - b.c);\n R t = (a.r * a.r - b.r * b.r + d * d) / 2 / d, h = sqrt(a.r * a.r - t * t);\n P m = t / abs(b.c - a.c) * (b.c - a.c) + a.c;\n P n(-(a.c - b.c).imag() / abs(a.c - b.c),\n (a.c - b.c).real() / abs(a.c - b.c));\n vector<P> ret(2, m);\n ret[0] -= h * n;\n ret[1] += h * n;\n return ret;\n}\n\n// 点aを通る接線を返す\n// pl = ((a, 接線と円の交点), (a, 接線と円の交点))\npair<L, L> tangent(C c, P a) {\n pair<L, L> pl;\n if (sgn(abs(a - c.c) - c.r) == 0) { // 点aが円周上にあるとき 要verify\n auto normal = [](P a) {\n L vp({a * P(0, 1), a * P(0, -1)});\n return vp;\n };\n L l = normal(a - c.c); // 法線ベクトル\n l.first = a;\n l.second += a;\n pl.first = pl.second = l;\n } else if (sgn(abs(a - c.c) - c.r) > 0) { // 点aが円の外側にあるとき\n R xp = a.real() - c.c.real();\n R yp = a.imag() - c.c.imag();\n R A = sqrt(xp * xp + yp * yp - c.r * c.r);\n R B = xp * xp + yp * yp;\n P p1(c.r * (xp * c.r + yp * A) / B, c.r * (yp * c.r - xp * A) / B);\n P p2(c.r * (xp * c.r - yp * A) / B, c.r * (yp * c.r + xp * A) / B);\n pl = make_pair(L(a, p1 + c.c), L(a, p2 + c.c));\n } else { // 点 a が円の内側にあるとき\n pl.first = pl.second = L(P(INF, INF), P(INF, INF));\n }\n return pl;\n}\n\n// 2つの円の共通接線の計算に使用\n// flag=true のとき内接線, falseのとき外接線\nL common_tangent(const C &c1, const C &c2, bool flag) {\n R xp = c2.c.real() - c1.c.real();\n R yp = c2.c.imag() - c1.c.imag();\n R r = flag ? c1.r + c2.r : c1.r - c2.r;\n R A = sqrt(xp * xp + yp * yp - r * r);\n R B = xp * xp + yp * yp;\n P p1(c1.r * (xp * r + yp * A) / B, c1.r * (yp * r - xp * A) / B);\n P p2(c1.r * (xp * r - yp * A) / B, c1.r * (yp * r + xp * A) / B);\n p1 += c1.c;\n p2 += c1.c;\n return L(p1, p2);\n}\n// 2円の共通接線を返す\n// vl = {(c1の接点, c2の接点), …, (c1の接点, c2の接点)}\nvector<L> common_tangent(C c1, C c2) {\n vector<L> vl;\n int pos = intersect(c1, c2);\n // 2つの共通内接線を求める\n L pp1 = common_tangent(c1, c2, true);\n L pp2 = common_tangent(c2, c1, true);\n if (pos == 4) { // 2つの円が離れているとき\n vl.push_back(L(pp1.first, pp2.first));\n vl.push_back(L(pp1.second, pp2.second));\n } else if (pos == 3) { // 外接するとき\n vl.push_back(tangent(c1, pp1.first).first);\n }\n // 2つの共通外接線を求める\n pp1 = common_tangent(c1, c2, false);\n pp2 = common_tangent(c2, c1, false);\n if (pos >= 2) { // 2つの円が交わるとき\n vl.push_back(L(pp1.first, pp2.second));\n vl.push_back(L(pp1.second, pp2.first));\n } else if (pos == 1) { // 2つの円が内接するとき\n vl.push_back(tangent(c1, pp1.first).first);\n }\n return vl;\n}\n\n// 2円の共通面積を求める\n// https://ferin-tech.hatenablog.com/entry/2020/03/15/220024\nR common_area(C c1, C c2) {\n ll type = intersect(c1, c2);\n if (type >= 3)\n return 0;\n if (type <= 1) {\n ll r = min(c1.r, c2.r);\n return PI * r * r;\n }\n if (c1.r > c2.r)\n swap(c1, c2);\n vector<P> ps = crosspoint(c1, c2);\n R ang1 = acosl(dot(ps[0] - c1.c, ps[1] - c1.c) / (c1.r * c1.r));\n R ang2 = acosl(dot(ps[0] - c2.c, ps[1] - c2.c) / (c2.r * c2.r));\n if (ccw(ps[0], ps[1], c1.c) == ccw(ps[0], ps[1], c2.c))\n ang1 = max(ang1, 2 * PI - ang1);\n else\n ang1 = min(ang1, 2 * PI - ang1);\n ang2 = min(ang2, 2 * PI - ang2);\n return c1.r * c1.r * 0.5 * (ang1 - sinl(ang1)) +\n c2.r * c2.r * 0.5 * (ang2 - sinl(ang2));\n}\n} // namespace Geometry\n\nnamespace std {\nbool operator<(const Geometry::P &a, const Geometry::P &b) {\n return Geometry::sgn(real(a - b)) ? real(a - b) < 0\n : Geometry::sgn(imag(a - b)) < 0;\n}\nbool operator==(const Geometry::P &a, const Geometry::P &b) {\n return Geometry::sgn(real(a - b)) == 0 && Geometry::sgn(imag(a - b)) == 0;\n}\nbool cmp_y(const Geometry::P &a, const Geometry::P &b) {\n return Geometry::sgn(imag(a - b)) ? imag(a - b) < 0\n : Geometry::sgn(real(a - b)) < 0;\n}\n} // namespace std\n\nint main() {\n while (1) {\n int n;\n cin >> n;\n if (n == 0)\n break;\n Geometry::G poly;\n for (int i = 0; i < n; i++) {\n int x, y;\n cin >> x >> y;\n poly.emplace_back(x, y);\n }\n double l = 0, r = 10000;\n for (int i = 0; i < 200; i++) {\n double mid = (l + r) / 2;\n Geometry::G tmp = poly;\n for (int j = 0; j < n; j++) {\n Geometry::P v = poly[(j + 1) % n] - poly[j];\n v /= abs(v);\n v = Geometry::rotate(v, Geometry::PI / 2);\n Geometry::L l =\n Geometry::L(poly[j] + v * Geometry::P(mid, 0),\n poly[(j + 1) % n] + v * Geometry::P(mid, 0));\n tmp = Geometry::convex_cut(tmp, l);\n }\n if (tmp.size() > 0)\n l = mid;\n else\n r = mid;\n }\n cout << fixed << setprecision(6) << l << endl;\n }\n}", "accuracy": 1, "time_ms": 60, "memory_kb": 3584, "score_of_the_acc": -0.7448, "final_rank": 8 }, { "submission_id": "aoj_1283_4999973", "code_snippet": "#include<cstdio>\n#include<vector>\n#include<complex>\nusing namespace std;\n\nusing C = complex<double>;\n#define cx real()\n#define cy imag()\nconstexpr double EPS = 1e-6;\n\nint n, x, y;\nC c[101];\n\ndouble dot(C a, C b) {\n return a.cx * a.cy + b.cx * b.cy;\n}\ndouble det(C a, C b) {\n return a.cx * b.cy - a.cy * b.cx;\n}\n\ndouble distance(C a, C b, C x) {\n C d = x - a;\n C e = b - a;\n return abs(det(d, e)) / abs(e);\n}\n\nbool isPointOnLineSegment(C a, C b, C c) {\n return abs(a-c) + abs(b-c) <= abs(a-b) + EPS;\n}\n\nbool eq(C a, C b) {\n C d = a - b;\n return abs(d.cx) < EPS && abs(d.cy) < EPS;\n}\n\nC intersectionLine(C a1, C a2, C b1, C b2) {\n C a = a2 - a1, b = b2 - b1;\n return a1 + a * det(b, b1 - a1) / det(b, a);\n}\n\nbool check_point(double len, C x, C from) {\n vector<C> vc;\n for (int i = 0; i < n; i++) {\n if (distance(c[i], c[i+1], x) < len - EPS) return false;\n C tmp = intersectionLine(from, x, c[i], c[i+1]);\n if (isPointOnLineSegment(c[i], c[i+1], tmp)) vc.push_back(tmp);\n }\n for (int i = 0; i < n; i++) {\n for (int j = i+1; j < n; j++) {\n if (isPointOnLineSegment(vc[i], vc[j], x)) return true;\n }\n }\n return false;\n}\n\nbool check(double len) {\n for (int i = 0; i < n; i++) {\n for (int j = i+1; j < n; j++) if (abs(det(c[i+1]-c[i], c[j+1]-c[j])) > EPS) {\n C inter = intersectionLine(c[i+1], c[i], c[j+1], c[j]);\n C a = c[i] - inter;\n if (abs(a) <= EPS) a = c[i+1] - inter;\n a /= abs(a);\n C b = c[j] - inter;\n if (abs(b) <= EPS) b = c[j+1] - inter;\n b /= abs(b);\n C x = a + b;\n x /= abs(x);\n C p = x / abs(det(x, b));\n if (check_point(len, inter + p * len, inter)) return true;\n }\n }\n return false;\n}\n\ndouble solve() {\n double lb = 0, ub = 10000;\n while (lb + EPS < ub) {\n double mid = (lb + ub) / 2;\n if (check(mid)) lb = mid;\n else ub = mid;\n }\n return lb;\n}\n\nint main() {\n while (scanf(\"%d\", &n), n) {\n for (int i = 0; i < n; i++) scanf(\"%d%d\", &x, &y), c[i] = C(x, y);\n c[n] = c[0];\n printf(\"%.6lf\\n\", solve());\n }\n}", "accuracy": 1, "time_ms": 360, "memory_kb": 2668, "score_of_the_acc": -0.0923, "final_rank": 1 }, { "submission_id": "aoj_1283_4964685", "code_snippet": "#line 1 \"a.cpp\"\nusing namespace std;\n#line 1 \"/home/kotatsugame/library/math/point.cpp\"\n#define _USE_MATH_DEFINES\n#include<iostream>\n#include<algorithm>\n#include<vector>\n#include<cmath>\n#include<iomanip>\nconst double EPS=1e-10;\nbool eq(double a,double b){return abs(a-b)<EPS;}\nstruct Point{\n\tdouble x,y;\n\tPoint(double x_=0,double y_=0):x(x_),y(y_){}\n\tPoint operator-()const{return Point(-x,-y);}\n\tPoint operator+(const Point&p)const{return Point(x+p.x,y+p.y);}\n\tPoint operator-(const Point&p)const{return Point(x-p.x,y-p.y);}\n\tPoint operator*(const double k)const{return Point(x*k,y*k);}\n\tPoint operator/(const double k)const{return Point(x/k,y/k);}\n\tbool operator<(const Point&p)const{return eq(x,p.x)?y<p.y:x<p.x;}\n\tbool operator==(const Point&p)const{return eq(x,p.x)&&eq(y,p.y);}\n};\nistream&operator>>(istream&is,Point&p){return is>>p.x>>p.y;}\nostream&operator<<(ostream&os,const Point&p){return os<<fixed<<setprecision(9)<<p.x<<' '<<p.y;}\nstruct Line{\n\tPoint p1,p2;\n\tLine(Point p1_=Point(),Point p2_=Point()):p1(p1_),p2(p2_){}\n};\nstruct Segment:Line{\n\tSegment(Point p1_=Point(),Point p2_=Point()):Line(p1_,p2_){}\n};\nstruct Circle{\n\tPoint o;\n\tdouble r;\n\tCircle(Point o_=Point(),double r_=0):o(o_),r(r_){}\n};\nusing Polygon=vector<Point>;\n//function list begin\nPoint vec(const Line&);\ndouble norm(const Point&);\ndouble norm(const Line&);\ndouble abs(const Point&);\ndouble abs(const Line&);\ndouble arg(const Point&);\ndouble arg(const Line&);\ndouble arg(const Point&,const Point&,const Point&);//a->b->c\nint argtype(const Point&);//(-pi,0]->0,(0,pi]->1\nbool argless(const Point&,const Point&);//sorting points with arg\ndouble dot(const Point&,const Point&);\ndouble cross(const Point&,const Point&);\nPoint polar(const double,const double);\nPoint rotate(const Point&,const double);\nenum{ONLINE_FRONT=-2,CLOCKWISE=-1,ON_SEGMENT=0,COUNTER_CLOCKWISE=1,ONLINE_BACK=2};\nint ccw(const Point&,const Point&);\nint ccw(const Point&,const Point&,const Point&);\nint ccw(const Line&,const Point&);\nbool orthogonal(const Point&,const Point&);\nbool orthogonal(const Line&,const Line&);\nbool parallel(const Point&,const Point&);\nbool parallel(const Line&,const Line&);\nbool intersect(const Line&,const Point&);\nbool intersect(const Line&,const Line&);\nbool intersect(const Segment&,const Point&);\nbool intersect(const Segment&,const Segment&);\nbool intersect(const Line&,const Segment&);\nbool intersect(const Segment&,const Line&);\nbool intersect(const Circle&,const Point&);\nint intersect(const Circle&,const Line&);//count contacts\nint intersect(const Circle&,const Segment&);//count contacts\nbool intersect(const Circle&,const Circle&);\nint count_tangent(const Circle&,const Circle&);//count common tangents\ndouble distance(const Point&,const Point&);\ndouble distance(const Line&,const Point&);\ndouble distance(const Line&,const Line&);\ndouble distance(const Segment&,const Point&);\ndouble distance(const Segment&,const Segment&);\ndouble distance(const Line&,const Segment&);\ndouble distance(const Segment&,const Line&);\ndouble distance(const Circle&,const Point&);\ndouble distance(const Circle&,const Line&);\ndouble distance(const Circle&,const Segment&);\ndouble distance(const Circle&,const Circle&);\nPoint projection(const Line&,const Point&);\nPoint reflection(const Line&,const Point&);\nPoint crosspoint(const Line&,const Line&);\npair<Point,Point>crosspoint(const Circle&,const Line&);\npair<Point,Point>crosspoint(const Circle&,const Segment&);\npair<Point,Point>crosspoint(const Circle&,const Circle&);\npair<Point,Point>tangent(const Circle&,const Point&);\nvector<Line>tangent(const Circle&,const Circle&);\nbool is_convex(const Polygon&);\nPolygon convex_hull(Polygon,bool=false);\nenum{OUT,ON,IN};\nint contain(const Polygon&,const Point&);\nint contain(const Circle&,const Point&);\nint contain(const Circle&,const Segment&);\nPolygon convex_cut(const Polygon&,const Line&);\ndouble diameter(Polygon);\ndouble area(const Polygon&);\ndouble area(const Polygon&,const Line&);\ndouble area(const Polygon&,const Circle&);\n//function list end\nPoint vec(const Line&s){return s.p2-s.p1;}\ndouble norm(const Point&p){return p.x*p.x+p.y*p.y;}\ndouble norm(const Line&s){return norm(vec(s));}\ndouble abs(const Point&p){return hypot(p.x,p.y);}\ndouble abs(const Line&s){return abs(vec(s));}\ndouble arg(const Point&p){return atan2(p.y,p.x);}\ndouble arg(const Line&s){return arg(vec(s));}\ndouble arg(const Point&a,const Point&b,const Point&c){\n\tdouble A=arg(b-a),B=arg(c-b);\n\tdouble theta=abs(A-B);\n\treturn min(theta,2*M_PI-theta);\n}\nint argtype(const Point&a)\n{\n\treturn a.y<-EPS?0:a.y>EPS?1:a.x<0?1:0;\n}\nbool argless(const Point&a,const Point&b)\n{\n\tint at=argtype(a),bt=argtype(b);\n\treturn at!=bt?at<bt:ccw(a,b)>0;\n}\ndouble dot(const Point&a,const Point&b){return a.x*b.x+a.y*b.y;}\ndouble cross(const Point&a,const Point&b){return a.x*b.y-a.y*b.x;}\nPoint polar(const double r,const double theta){return Point(cos(theta),sin(theta))*r;}\nPoint rotate(const Point&p,const double theta){\n\treturn Point(p.x*cos(theta)-p.y*sin(theta),p.x*sin(theta)+p.y*cos(theta));\n}\nint ccw(const Point&a,const Point&b)\n{\n\treturn cross(a,b)>EPS?COUNTER_CLOCKWISE\n\t\t:cross(a,b)<-EPS?CLOCKWISE\n\t\t:dot(a,b)<0?ONLINE_BACK\n\t\t:norm(a)<norm(b)?ONLINE_FRONT\n\t\t:ON_SEGMENT;\n}\nint ccw(const Point&a,const Point&b,const Point&c){return ccw(b-a,c-a);}\nint ccw(const Line&s,const Point&p){return ccw(s.p1,s.p2,p);}\nbool orthogonal(const Point&a,const Point&b){return eq(dot(a,b),0);}\nbool orthogonal(const Line&s,const Line&t){return orthogonal(vec(s),vec(t));}\nbool parallel(const Point&a,const Point&b){return eq(cross(a,b),0);}\nbool parallel(const Line&s,const Line&t){return parallel(vec(s),vec(t));}\nbool intersect(const Line&s,const Point&p){return eq(cross(vec(s),p-s.p1),0);}\nbool intersect(const Line&s,const Line&t){return !parallel(s,t)||intersect(s,t.p1);}\nbool intersect(const Segment&s,const Point&p){return ccw(s,p)==ON_SEGMENT;}\nbool intersect(const Segment&s,const Segment&t){\n\treturn ccw(s,t.p1)*ccw(s,t.p2)<=0&&ccw(t,s.p1)*ccw(t,s.p2)<=0;\n}\nbool intersect(const Line&s,const Segment&t){\n\treturn cross(vec(s),t.p1-s.p1)*cross(vec(s),t.p2-s.p1)<EPS;\n}\nbool intersect(const Segment&s,const Line&t){return intersect(t,s);}\nbool intersect(const Circle&c,const Point&p){return eq(distance(c.o,p),c.r);}\nint intersect(const Circle&c,const Line&s){\n\tdouble d=distance(s,c.o);\n\treturn eq(d,c.r)?1:d<c.r?2:0;\n}\nint intersect(const Circle&c,const Segment&s){\n\tPoint h=projection(s,c.o);\n\tdouble d1=distance(c.o,s.p1),d2=distance(c.o,s.p2);\n\treturn distance(c.o,h)>c.r+EPS?0\n\t\t:d1<c.r-EPS&&d2<c.r-EPS?0\n\t\t:d1<c.r-EPS&&d2>c.r-EPS||d1>c.r-EPS&&d2<c.r-EPS?1\n\t\t:intersect(s,h)?eq(distance(c.o,h),c.r)?1:2\n\t\t:0;\n}\nbool intersect(const Circle&a,const Circle&b){\n\tint c=count_tangent(a,b);\n\treturn 1<=c&&c<=3;\n}\nint count_tangent(const Circle&a,const Circle&b){\n\tdouble d=distance(a.o,b.o);\n\treturn eq(d,a.r+b.r)?3:d>a.r+b.r?4:eq(d,abs(a.r-b.r))?1:d>abs(a.r-b.r)?2:0;\n}\ndouble distance(const Point&a,const Point&b){return abs(a-b);}\ndouble distance(const Line&s,const Point&p){return distance(p,projection(s,p));}\ndouble distance(const Line&s,const Line&t){return intersect(s,t)?0:distance(s,t.p1);}\ndouble distance(const Segment&s,const Point&p){\n\treturn distance(p,\n\t\tdot(vec(s),p-s.p1)<0?s.p1\n\t\t:dot(-vec(s),p-s.p2)<0?s.p2\n\t\t:projection(s,p)\n\t);\n}\ndouble distance(const Segment&s,const Segment&t){\n\treturn intersect(s,t)?0:min({\n\t\tdistance(s,t.p1),distance(s,t.p2),\n\t\tdistance(t,s.p1),distance(t,s.p2)\n\t});\n}\ndouble distance(const Line&s,const Segment&t){\n\treturn intersect(s,t)?0:min(distance(s,t.p1),distance(s,t.p2));\n}\ndouble distance(const Segment&s,const Line&t){return distance(t,s);}\ndouble distance(const Circle&c,const Point&p){return abs(distance(c.o,p)-c.r);}\ndouble distance(const Circle&c,const Line&s){return max(distance(s,c.o)-c.r,0.);}\ndouble distance(const Circle&c,const Segment&s){\n\treturn intersect(c,s)?0\n\t\t:contain(c,s)?c.r-max(distance(c.o,s.p1),distance(c.o,s.p2))\n\t\t:distance(s,c.o)-c.r;\n}\ndouble distance(const Circle&a,const Circle&b){return max(distance(a.o,b.o)-a.r-b.r,0.);}\nPoint projection(const Line&s,const Point&p){\n\treturn s.p1+vec(s)*dot(p-s.p1,vec(s))/norm(s);\n}\nPoint reflection(const Line&s,const Point&p){return projection(s,p)*2-p;}\nPoint crosspoint(const Line&s,const Line&t){\n\tdouble d1=cross(vec(s),t.p1-s.p1);\n\tdouble d2=-cross(vec(s),t.p2-s.p1);\n\treturn t.p1+vec(t)*(d1/(d1+d2));\n}\npair<Point,Point>crosspoint(const Circle&c,const Line&s){\n\tPoint h=projection(s,c.o);\n\tPoint e=vec(s)/abs(s)*sqrt(c.r*c.r-norm(h-c.o));\n\treturn minmax(h-e,h+e);\n}\npair<Point,Point>crosspoint(const Circle&c,const Segment&s){\n\tpair<Point,Point>p=crosspoint(c,Line(s));\n\treturn intersect(c,s)==2?p\n\t\t:intersect(s,p.first)?make_pair(p.first,p.first)\n\t\t:make_pair(p.second,p.second);\n}\npair<Point,Point>crosspoint(const Circle&a,const Circle&b){\n\tdouble d=distance(a.o,b.o);\n\tdouble alpha=acos((a.r*a.r+d*d-b.r*b.r)/(2*a.r*d));\n\tdouble theta=arg(b.o-a.o);\n\treturn minmax(a.o+polar(a.r,theta+alpha),a.o+polar(a.r,theta-alpha));\n}\npair<Point,Point>tangent(const Circle&c,const Point&p){\n\treturn crosspoint(c,Circle(p,sqrt(norm(c.o-p)-c.r*c.r)));\n}\nvector<Line>tangent(const Circle&a,const Circle&b){\n\tvector<Line>ret;\n\tdouble g=distance(a.o,b.o);\n\tif(eq(g,0))return ret;\n\tPoint u=(b.o-a.o)/g;\n\tPoint v=rotate(u,M_PI/2);\n\tfor(int s:{-1,1}){\n\t\tdouble h=(a.r+b.r*s)/g;\n\t\tif(eq(h*h,1))ret.emplace_back(a.o+(h>0?u:-u)*a.r,a.o+(h>0?u:-u)*a.r+v);\n\t\telse if(1-h*h>0){\n\t\t\tPoint U=u*h,V=v*sqrt(1-h*h);\n\t\t\tret.emplace_back(a.o+(U+V)*a.r,b.o-(U+V)*b.r*s);\n\t\t\tret.emplace_back(a.o+(U-V)*a.r,b.o-(U-V)*b.r*s);\n\t\t}\n\t}\n\treturn ret;\n}\nbool is_convex(const Polygon&P){\n\tfor(int i=0;i<P.size();i++)\n\t\tif(ccw(P[i],P[(i+1)%P.size()],P[(i+2)%P.size()])==CLOCKWISE)return false;\n\treturn true;\n}\nPolygon convex_hull(Polygon P,bool ONSEG){\n\tif(P.size()<=2)return P;\n\tsort(P.begin(),P.end());\n\tPolygon ret(2*P.size());\n\tint k=0,t;\n\tif(ONSEG){\n\t\tfor(const Point&p:P){\n\t\t\twhile(k>=2&&ccw(ret[k-2],ret[k-1],p)==CLOCKWISE)k--;\n\t\t\tret[k++]=p;\n\t\t}\n\t\tt=k;\n\t\tfor(int i=P.size()-2;i>=0;i--){\n\t\t\twhile(k>=t+1&&ccw(ret[k-2],ret[k-1],P[i])==CLOCKWISE)k--;\n\t\t\tret[k++]=P[i];\n\t\t}\n\t}\n\telse{\n\t\tfor(const Point&p:P){\n\t\t\twhile(k>=2&&ccw(ret[k-2],ret[k-1],p)!=COUNTER_CLOCKWISE)k--;\n\t\t\tret[k++]=p;\n\t\t}\n\t\tt=k;\n\t\tfor(int i=P.size()-2;i>=0;i--){\n\t\t\twhile(k>=t+1&&ccw(ret[k-2],ret[k-1],P[i])!=COUNTER_CLOCKWISE)k--;\n\t\t\tret[k++]=P[i];\n\t\t}\n\t}\n\tret.resize(k-1);\n\tint mi=0;\n\tfor(int i=1;i<k-1;i++)\n\t\tif(eq(ret[mi].y,ret[i].y)?ret[mi].x>ret[i].x:ret[mi].y>ret[i].y)mi=i;\n\trotate(ret.begin(),ret.begin()+mi,ret.end());\n\treturn ret;\n}\nint contain(const Polygon&P,const Point&p){\n\tbool in=false;\n\tfor(int i=0;i<P.size();i++){\n\t\tSegment s(P[i],P[(i+1)%P.size()]);\n\t\tif(intersect(s,p))return ON;\n\t\telse{\n\t\t\tPoint a=s.p1-p,b=s.p2-p;\n\t\t\tif(a.y>b.y)swap(a,b);\n\t\t\tif(a.y<EPS&&EPS<b.y&&cross(a,b)>EPS)in=!in;\n\t\t}\n\t}\n\treturn in?IN:OUT;\n}\nint contain(const Circle&c,const Point&p){\n\tdouble d=distance(c.o,p);\n\treturn eq(d,c.r)?ON:d<c.r?IN:OUT;\n}\nint contain(const Circle&c,const Segment&s){\n\tdouble d1=distance(c.o,s.p1),d2=distance(c.o,s.p2);\n\treturn d1<c.r+EPS&&d2<c.r+EPS?eq(d1,c.r)||eq(d2,c.r)?ON:IN:OUT;\n}\nPolygon convex_cut(const Polygon&P,const Line&s){\n\tPolygon ret;\n\tfor(int i=0;i<P.size();i++){\n\t\tSegment t(P[i],P[(i+1)%P.size()]);\n\t\tif(ccw(s,t.p1)!=CLOCKWISE)ret.push_back(t.p1);\n\t\tif(!parallel(s,t)&&!intersect(s,t.p1)\n\t\t\t&&!intersect(s,t.p2)&&intersect(s,t))ret.push_back(crosspoint(s,t));\n\t}\n\treturn ret;\n}\ndouble diameter(Polygon P){\n\tif(!is_convex(P))P=convex_hull(P);\n\tint mi=0,Mi=0;\n\tfor(int i=1;i<P.size();i++){\n\t\tif(P[i].x<P[mi].x)mi=i;\n\t\tif(P[i].x>P[Mi].x)Mi=i;\n\t}\n\tdouble ret=0;\n\tint sm=mi,sM=Mi;\n\twhile(mi!=sM||Mi!=sm){\n\t\tret=max(ret,norm(P[mi]-P[Mi]));\n\t\tif(cross(P[(mi+1)%P.size()]-P[mi],P[(Mi+1)%P.size()]-P[Mi])<0)mi=(mi+1)%P.size();\n\t\telse Mi=(Mi+1)%P.size();\n\t}\n\treturn sqrt(ret);\n}\ndouble area(const Polygon&P){\n\tdouble ret=0;\n\tfor(int i=0;i<P.size();i++)ret+=cross(P[i],P[(i+1)%P.size()]);\n\treturn ret/2;\n}\ndouble area(const Polygon&P,const Line&s){return area(convex_cut(P,s));}\ndouble area(const Polygon&P,const Circle&c){\n\tdouble ret=0;\n\tfor(int i=0;i<P.size();i++)\n\t{\n\t\tSegment s(P[i],P[(i+1)%P.size()]);\n\t\tif(contain(c,s))ret+=cross(s.p1-c.o,s.p2-c.o);\n\t\telse if(!intersect(c,s)){\n\t\t\tdouble a=arg(s.p2-c.o)-arg(s.p1-c.o);\n\t\t\tif(a>M_PI)a-=2*M_PI;\n\t\t\tif(a<-M_PI)a+=2*M_PI;\n\t\t\tret+=c.r*c.r*a;\n\t\t}\n\t\telse\n\t\t{\n\t\t\tpair<Point,Point>p=crosspoint(c,s);\n\t\t\tPoint tmp[4]={s.p1,p.first,p.second,s.p2};\n\t\t\tif(intersect(c,Segment(s.p1,p.first))==2)swap(tmp[1],tmp[2]);\n\t\t\tfor(int j=0;j<3;j++)\n\t\t\t{\n\t\t\t\tSegment t(tmp[j],tmp[j+1]);\n\t\t\t\tif(contain(c,t))ret+=cross(t.p1-c.o,t.p2-c.o);\n\t\t\t\telse{\n\t\t\t\t\tdouble a=arg(t.p2-c.o)-arg(t.p1-c.o);\n\t\t\t\t\tif(a>M_PI)a-=2*M_PI;\n\t\t\t\t\tif(a<-M_PI)a+=2*M_PI;\n\t\t\t\t\tret+=c.r*c.r*a;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n\treturn ret/2;\n}\n#line 3 \"a.cpp\"\nint N;\nPolygon P;\nmain()\n{\n\twhile(cin>>N,N)\n\t{\n\t\tP=Polygon(N);\n\t\tfor(int i=0;i<N;i++)cin>>P[i];\n\t\tdouble L=0,R=5e4;\n\t\tfor(int c=0;c<50;c++)\n\t\t{\n\t\t\tdouble r=(L+R)/2;\n\t\t\tvector<Line>A(N);\n\t\t\tfor(int i=0;i<N;i++)\n\t\t\t{\n\t\t\t\tLine s=Line(P[i],P[(i+1)%N]);\n\t\t\t\tPoint a=vec(s);\n\t\t\t\ta=a/abs(a)*r;\n\t\t\t\tA[i]=Line(s.p1+rotate(a,M_PI/2),s.p2+rotate(a,M_PI/2));\n\t\t\t}\n\t\t\tbool ok=false;\n\t\t\tfor(int i=0;i<N;i++)for(int j=i+1;j<N;j++)\n\t\t\t{\n\t\t\t\tLine a=A[i],b=A[j];\n\t\t\t\tif(!parallel(a,b))\n\t\t\t\t{\n\t\t\t\t\tPoint p=crosspoint(a,b);\n\t\t\t\t\tif(!contain(P,p))continue;\n\t\t\t\t\tbool now=true;\n\t\t\t\t\tfor(int i=0;i<N;i++)\n\t\t\t\t\t{\n\t\t\t\t\t\tSegment s(P[i],P[(i+1)%N]);\n\t\t\t\t\t\tif(distance(s,p)<r-EPS)\n\t\t\t\t\t\t{\n\t\t\t\t\t\t\tnow=false;\n\t\t\t\t\t\t\tbreak;\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t\tif(now)ok=true;\n\t\t\t\t}\n\t\t\t}\n\t\t\tif(ok)L=r;\n\t\t\telse R=r;\n\t\t}\n\t\tcout<<fixed<<setprecision(16)<<R<<endl;\n\t}\n}", "accuracy": 1, "time_ms": 1290, "memory_kb": 3516, "score_of_the_acc": -1.015, "final_rank": 16 }, { "submission_id": "aoj_1283_4951992", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\n\n#define REP(i,n) for(int i = 0; i < (n);i++)\n#define FOR(i,m,n) for(int i = (m);i<(n);i++)\n#define all(x) (x).begin(),(x).end()\n//#define int long long\ntemplate <typename T1, typename T2> inline void chmin(T1 & a, T2 b) { if (a > b) a = b; }\ntemplate <typename T1, typename T2> inline void chmax(T1& a, T2 b) { if (a < b) a = b; }\n\n//#define TEST\n\n#ifdef TEST\nvoid debug_out(){cerr<<endl;}\ntemplate<typename Head,typename... Tail>\nvoid debug_out(Head H,Tail... T){\n cerr<< \" \"<<(H);\n debug_out(T...);\n}\n#define DEBUG(...) cerr<<\"[\"<<#__VA_ARGS__<<\"]:\",debug_out(__VA_ARGS__)\n#else\n#define DEBUG(...) 42\n#endif\n\n\nconst double PI = acos(-1);\nconst double EPS = 1e-8;\n\ntypedef complex<double> point;\n\npoint operator*(const point &p, const double &d) {\n return point(real(p) * d, imag(p) * d);\n}\nnamespace std {\n bool operator<(const point &a, const point &b) {\n return real(a) != real(b) ? real(a) < real(b) : imag(a) < imag(b);\n }\n} // namespace std\n\n//直線or線分\nstruct Line : public vector<point> {\n Line(const point &a, const point &b) {\n push_back(a);\n push_back(b);\n }\n};\ntypedef vector<Line> Lines;\n\npoint direction(const point &p) { //同じ向きの単位ベクトル\n return p / abs(p);\n}\npoint orthogonal(const point &p) { //法線ベクトル\n return point(-imag(p), real(p));\n}\ndouble cross(const point &a, const point &b) {\n return real(a) * imag(b) - imag(a) * real(b);\n}\ndouble dot(const point &a, const point &b) {\n return real(a) * real(b) + imag(a) * imag(b);\n}\nint ccw(point a, point b, point c) {\n b = b - a;\n c = c - a;\n if (cross(b, c) > EPS) return +1; // counter clockwise\n if (cross(b, c) < -EPS) return -1; // clockwise\n if (dot(b, c) < -EPS) return +2; // c--a--b on line\n if (norm(b) < norm(c)) return -2; // a--b--c on line\n return 0;\n}\n\n//交差判定\nbool intersectLL(const Line &l1, const Line &l2) {\n return abs(cross(l1[1] - l1[0], l2[1] - l2[0])) > EPS || // non-parallel\n abs(cross(l1[1] - l1[0], l2[0] - l1[0])) < EPS; // same line\n}\nbool intersectLS(const Line &l, const Line &s) {\n return cross(l[1] - l[0], s[0] - l[0]) * // s[0] is left of l\n cross(l[1] - l[0], s[1] - l[0]) <\n EPS; // s[1] is right of l\n}\nbool intersectLP(const Line &l, const point &p) {\n return abs(cross(l[1] - p, l[0] - p)) < EPS;\n}\n//端点に乗ってたらfalse\nbool intersectSP_without_endpoint(const Line &s, const point &p) {\n if (abs(s[0] - p) < EPS || abs(s[1] - p) < EPS) return false;\n return abs(s[0] - p) + abs(s[1] - p) - abs(s[1] - s[0]) < EPS; // triangle inequality\n}\n\nbool intersectSP(const Line &s, const point &p) {\n // verified on 2020/04/03\n // https://onlinejudge.u-aizu.ac.jp/status/users/Mojumbo/submissions/1/1157/judge/4316978/C++14\n return abs(s[0] - p) + abs(s[1] - p) - abs(s[1] - s[0]) < EPS; // triangle inequality\n}\nbool isOnSegment(const Line &s, const Line &t) {\n if (intersectSP(s, t[0]) || intersectSP(s, t[1]) || intersectSP(t, s[0]) ||\n intersectSP(t, s[1]))\n return true;\n else\n return false;\n}\nbool isSamePoint(const point &p1, const point &p2) { return abs(p1 - p2) < EPS; }\nbool connectSS(const Line &s, const Line &t) {\n REP(i, 2) REP(j, 2) {\n if (s[i] == t[j]) return true;\n }\n return false;\n}\nbool intersectSS(const Line &s, const Line &t) {\n return ccw(s[0], s[1], t[0]) * ccw(s[0], s[1], t[1]) <= 0 &&\n ccw(t[0], t[1], s[0]) * ccw(t[0], t[1], s[1]) <= 0;\n}\n//端点が線分上に乗っている場合はfalseにする\nbool intersectSS_strict(const Line &s, const Line &t) {\n if (isOnSegment(s, t)) return false;\n return ccw(s[0], s[1], t[0]) * ccw(s[0], s[1], t[1]) <= 0 &&\n ccw(t[0], t[1], s[0]) * ccw(t[0], t[1], s[1]) <= 0;\n}\n\n//距離,交点\n//射影 直線lにpから下した垂線との交点\npoint projection(\n const Line &l,\n const point &\n p) { // verified on 2020/04/03\n // https://onlinejudge.u-aizu.ac.jp/status/users/Mojumbo/submissions/1/1157/judge/4316978/C++14\n double t = dot(p - l[0], l[0] - l[1]) / norm(l[0] - l[1]);\n return l[0] + (l[0] - l[1]) * t;\n}\n//射影 直線lを対象軸としてpと線対称にある点\npoint reflection(const Line &l, const point &p) { return p + (projection(l, p) - p) * 2; }\ndouble distancePP(const point &a, const point &b) { return sqrt(norm(a - b)); }\ndouble distanceLP(const Line &l, const point &p) { return abs(p - projection(l, p)); }\ndouble distanceLL(const Line &l, const Line &m) {\n return intersectLL(l, m) ? 0 : distanceLP(l, m[0]);\n}\ndouble distanceLS(const Line &l, const Line &s) {\n if (intersectLS(l, s)) return 0;\n return min(distanceLP(l, s[0]), distanceLP(l, s[1]));\n}\ndouble distanceSP(const Line &s, const point &p) {\n const point r = projection(s, p);\n if (intersectSP(s, r)) return abs(r - p);\n return min(abs(s[0] - p), abs(s[1] - p));\n}\ndouble distanceSS(const Line &s, const Line &t) {\n // verified on 2020/04/03\n // https://onlinejudge.u-aizu.ac.jp/status/users/Mojumbo/submissions/1/1157/judge/4316978/C++14\n if (intersectSS(s, t)) return 0;\n return min({distanceSP(s, t[0]), distanceSP(s, t[1]), distanceSP(t, s[0]),\n distanceSP(t, s[1])});\n}\npoint crosspoint(const Line &l, const Line &m) {\n double A = cross(l[1] - l[0], m[1] - m[0]);\n double B = cross(l[1] - l[0], l[1] - m[0]);\n if (abs(A) < EPS && abs(B) < EPS) return m[0]; // same line\n if (abs(A) < EPS) assert(false); // Precondition not satisfied\n return m[0] + B / A * (m[1] - m[0]);\n}\n\nstruct Polygon : public vector<point> {\n // 0->OUT, 1->ON, 2->IN\n int contains(const point &p) {\n bool in = false;\n int N = this->size();\n for (int i = 0; i < N; i++) {\n point a = this->at(i) - p;\n point b = this->at((i + 1) % N) - p;\n if (imag(a) > imag(b)) {\n std::swap(a, b);\n }\n if (imag(a) <= 0 && 0 < imag(b))\n if (cross(a, b) < 0) in = !in;\n if (cross(a, b) == 0 && dot(a, b) <= 0) return 1;\n }\n return in ? 2 : 0;\n }\n double area(){\n double A = 0;\n int N = this->size();\n REP(i,N){\n A+=cross(this->at(i),this->at((i+1)%N));\n }\n return A*0.5;\n }\n};\n\nconst double INF = 1e9;\n\nbool solve(){\n int N;cin>>N;\n if(N==0)return false;\n Polygon poly;\n vector<Line>segs;\n double minx=INF,maxx=-INF;\n REP(i,N){\n int x,y;cin>>x>>y;\n poly.emplace_back(x,y);\n chmin(minx,x);\n chmax(maxx,x);\n }\n REP(i,N){\n segs.emplace_back(poly[i],poly[(i+1)%N]);\n }\n\n auto calc=[&](point p){\n if(!poly.contains(p))return -INF;\n double ret = INF;\n for(auto &seg:segs){\n ret=min(ret,distanceSP(seg,p));\n }\n return ret;\n };\n auto ternary_search=[&](double x)->double{\n vector<double>ys;\n Line li(point(x,0),point(x,10000));\n for(auto seg:segs){\n if(intersectSS(li,seg))ys.push_back(crosspoint(li,seg).imag());\n }\n sort(all(ys));\n if(ys.size()<1)return -INF;\n double l = ys[0],r=*ys.rbegin();\n REP(_,100){\n double m1 = (l+l+r)/3,m2=(l+r+r)/3;\n point p1(x,m1),p2(x,m2);\n if(calc(p1)>calc(p2))r=m2;\n else l = m1;\n }\n return calc(point(x,l));\n };\n double ans = -INF;\n double l=minx,r=maxx;\n REP(_,100){\n double m1=(l+l+r)/3,m2=(l+r+r)/3;\n double res1=ternary_search(m1),res2=ternary_search(m2);\n DEBUG(m1,m2,res1,res2);\n if(ternary_search(m1)>ternary_search(m2))r=m2;\n else l=m1;\n }\n ans = ternary_search(l);\n cout<<setprecision(10)<<fixed<<ans<<endl;\n return true;\n}\n\nsigned main(){\n while(solve());\n}", "accuracy": 1, "time_ms": 1960, "memory_kb": 3336, "score_of_the_acc": -1.0481, "final_rank": 17 } ]
aoj_1281_cpp
Problem G: The Morning after Halloween You are working for an amusement park as an operator of an obakeyashiki , or a haunted house, in which guests walk through narrow and dark corridors. The house is proud of their lively ghosts, which are actually robots remotely controlled by the operator, hiding here and there in the corridors. One morning, you found that the ghosts are not in the positions where they are supposed to be. Ah, yesterday was Halloween. Believe or not, paranormal spirits have moved them around the corridors in the night. You have to move them into their right positions before guests come. Your manager is eager to know how long it takes to restore the ghosts. In this problem, you are asked to write a program that, given a floor map of a house, finds the smallest number of steps to move all ghosts to the positions where they are supposed to be. A floor consists of a matrix of square cells. A cell is either a wall cell where ghosts cannot move into or a corridor cell where they can. At each step, you can move any number of ghosts simultaneously. Every ghost can either stay in the current cell, or move to one of the corridor cells in its 4-neighborhood (i.e. immediately left, right, up or down), if the ghosts satisfy the following conditions: No more than one ghost occupies one position at the end of the step. No pair of ghosts exchange their positions one another in the step. For example, suppose ghosts are located as shown in the following (partial) map, where a sharp sign (' # ) represents a wall cell and 'a', 'b', and 'c' ghosts. #### ab# #c## #### The following four maps show the only possible positions of the ghosts after one step. #### #### #### #### ab# a b# acb# ab # #c## #c## # ## #c## #### #### #### #### Input The input consists of at most 10 datasets, each of which represents a floor map of a house. The format of a dataset is as follows. w h n c 11 c 12 ... c 1 w c 21 c 22 ... c 2 w . . .. . .. . . .. . c h 1 c h 2 ... c h w w , h and n in the first line are integers, separated by a space. w and h are the floor width and height of the house, respectively. n is the number of ghosts. They satisfy the following constraints. 4 ≤ w ≤ 16 4 ≤ h ≤ 16 1 ≤ n ≤ 3 Subsequent h lines of w characters are the floor map. Each of c ij is either: a ' # ' representing a wall cell, a lowercase letter representing a corridor cell which is the initial position of a ghost, an uppercase letter representing a corridor cell which is the position where the ghost corresponding to its lowercase letter is supposed to be, or a space representing a corridor cell that is none of the above. In each map, each of the fir ...(truncated)
[ { "submission_id": "aoj_1281_10851166", "code_snippet": "#include<cstdio>\n#include<cstring>\n#include<cctype>\n#include<queue>\nusing namespace std;\n\nconst int maxs = 20;\nconst int maxn = 150;\nconst int dx[]= {1,-1,0,0,0};\nconst int dy[]= {0,0,1,-1,0};\n\ninline int ID(int a, int b, int c)\n{\n return (a<<16)|(b<<8)|c;\n}\n\nint s[3], t[3];\n\nint deg[maxn], G[maxn][5];\n\ninline bool conflict(int a, int b, int a2, int b2)\n{\n return a2 == b2 || (a2 == b && b2 == a);\n}\n\nint d[maxn][maxn][maxn];\n\nint bfs()\n{\n queue<int> q;\n memset(d, -1, sizeof(d));\n q.push(ID(s[0], s[1], s[2]));\n d[s[0]][s[1]][s[2]] = 0;\n while(!q.empty())\n {\n int u = q.front();\n q.pop();\n int a = (u>>16)&0xff, b = (u>>8)&0xff, c = u&0xff;\n if(a == t[0] && b == t[1] && c == t[2]) return d[a][b][c];\n for(int i = 0; i < deg[a]; i++)\n {\n int a2 = G[a][i];\n for(int j = 0; j < deg[b]; j++)\n {\n int b2 = G[b][j];\n if(conflict(a, b, a2, b2)) continue;\n for(int k = 0; k < deg[c]; k++)\n {\n int c2 = G[c][k];\n if(conflict(a, c, a2, c2)) continue;\n if(conflict(b, c, b2, c2)) continue;\n if(d[a2][b2][c2] != -1) continue;\n d[a2][b2][c2] = d[a][b][c]+1;\n q.push(ID(a2, b2, c2));\n }\n }\n }\n }\n return -1;\n}\n\nint main()\n{\n int w, h, n;\n\n while(scanf(\"%d%d%d\\n\", &w, &h, &n) == 3 && n)\n {\n char maze[20][20];\n for(int i = 0; i < h; i++)\n fgets(maze[i], 20, stdin);\n\n int cnt, x[maxn], y[maxn], id[maxs][maxs];\n cnt = 0;\n for(int i = 0; i < h; i++)\n for(int j = 0; j < w; j++)\n if(maze[i][j] != '#')\n {\n x[cnt] = i;\n y[cnt] = j;\n id[i][j] = cnt;\n if(islower(maze[i][j])) s[maze[i][j] - 'a'] = cnt;\n else if(isupper(maze[i][j])) t[maze[i][j] - 'A'] = cnt;\n cnt++;\n }\n\n for(int i = 0; i < cnt; i++)\n {\n deg[i] = 0;\n for(int dir = 0; dir < 5; dir++)\n {\n int nx = x[i]+dx[dir], ny = y[i]+dy[dir];\n if(maze[nx][ny] != '#') G[i][deg[i]++] = id[nx][ny];\n }\n }\n\n if(n <= 2)\n {\n deg[cnt] = 1;\n G[cnt][0] = cnt;\n s[2] = t[2] = cnt++;\n }\n if(n <= 1)\n {\n deg[cnt] = 1;\n G[cnt][0] = cnt;\n s[1] = t[1] = cnt++;\n }\n\n printf(\"%d\\n\", bfs());\n }\n return 0;\n}", "accuracy": 1, "time_ms": 510, "memory_kb": 16688, "score_of_the_acc": -0.0892, "final_rank": 2 }, { "submission_id": "aoj_1281_10696783", "code_snippet": "#include<bits/stdc++.h>\n#define ll long long\nusing namespace std;\nstruct node{\n\tint x,y,p;\n};\nstruct nodenode{\n\tint x,y,xx,yy,p,typ;\n};\nstruct nodenodenode{\n\tint x,y,xx,yy,xxx,yyy,p,typ;\n};\nint n,m,x;\nchar s[55][55];\nint dx[5]={1,0,-1,0,0},\n\tdy[5]={0,1,0,-1,0};\nvoid solve1(){\n\tint sx=0,sy=0,ex=0,ey=0;\n\tfor(int i=1;i<=n;i++){\n\t\tchar c=getchar();\n\t\tfor(int j=1;j<=m;j++){\n\t\t\ts[i][j]=getchar();\n\t\t\tif(s[i][j]=='a')sx=i,sy=j;\n\t\t\tif(s[i][j]=='A')ex=i,ey=j;\n\t\t}\n\t}\n\t\n\tqueue<node>q;\n\tq.push({sx,sy,0});\n\tbool vis[22][22]={};\n\twhile(!q.empty()){\n\t\tauto qwe=q.front(); q.pop();\n\t\tint x=qwe.x,y=qwe.y,p=qwe.p;\n\t\tif(x==ex&&y==ey){\n\t\t\tcout<<p<<\"\\n\";\n\t\t\treturn ;\n\t\t}\n\t\tvis[x][y]=1;\n\t\tfor(int i=0;i<4;i++){\n\t\t\tint xx=dx[i]+x,yy=dy[i]+y;\n\t\t\tif(xx<1||xx>n||yy<1||yy>m||s[xx][yy]=='#'||vis[xx][yy])\n\t\t\t\tcontinue;\n\t\t\tvis[xx][yy]=1;\n\t\t\tq.push({xx,yy,p+1});\n\t\t}\n\t}\n}\n\nvoid solve2(){\n\tint qx=0,qy=0,wx=0,wy=0,ex=0,ey=0,fx=0,fy=0;\n\tfor(int i=1;i<=n;i++){\n\t\tchar c=getchar();\n\t\tfor(int j=1;j<=m;j++){\n\t\t\ts[i][j]=getchar();\n\t\t\tif(s[i][j]=='a')qx=i,qy=j;\n\t\t\tif(s[i][j]=='A')wx=i,wy=j;\n\t\t\tif(s[i][j]=='b')ex=i,ey=j;\n\t\t\tif(s[i][j]=='B')fx=i,fy=j;\n\t\t}\n\t}\n\t\n\tqueue<nodenode>q;\n\tq.push({qx,qy,ex,ey,0,1});\n\tq.push({wx,wy,fx,fy,0,2});\n\tshort vis[22][22][22][22]={};\n\tshort d[22][22][22][22]={};\n\tvis[qx][qy][ex][ey]=1;\n\tvis[wx][wy][fx][fy]=2;\n\twhile(!q.empty()){\n\t\tauto qwe=q.front(); q.pop();\n\t\tint x=qwe.x,y=qwe.y,xx=qwe.xx,yy=qwe.yy,p=qwe.p,typ=qwe.typ;\n\t\t\n\t\tfor(int i=0;i<5;i++){\n\t\t\tfor(int j=0;j<5;j++){\n\t\t\t\tint ax=dx[i]+x,ay=dy[i]+y;\n\t\t\t\tint bx=dx[j]+xx,by=dy[j]+yy;\n\t\t\t\tif(ax<1||ax>n||ay<1||ay>m||s[ax][ay]=='#')continue;\n\t\t\t\tif(bx<1||bx>n||by<1||by>m||s[bx][by]=='#')continue;\n\t\t\t\tif(ax==bx&&ay==by)continue;\n\t\t\t\tif(ax==xx&&ay==yy&&bx==x&&by==y)continue;\n\t\t\t\tif(vis[ax][ay][bx][by]==typ)continue;\n\t\t\t\tif(vis[ax][ay][bx][by]+typ==3){\n\t\t\t\t\tcout<<d[ax][ay][bx][by]+d[x][y][xx][yy]+1<<\"\\n\";\n\t\t\t\t\treturn ;\n\t\t\t\t}\n\t\t\t\tvis[ax][ay][bx][by]=typ;\n\t\t\t\td[ax][ay][bx][by]=d[x][y][xx][yy]+1;\n\t\t\t\tq.push({ax,ay,bx,by,p+1,typ});\n\t\t\t}\n\t\t}\n\t}\n}\n\nvoid solve3(){\n\tint ax=0,ay=0,bx=0,by=0,cx=0,cy=0,ex=0,ey=0,fx=0,fy=0,gx=0,gy=0;\n\tfor(int i=1;i<=n;i++){\n\t\tchar c=getchar();\n\t\tfor(int j=1;j<=m;j++){\n\t\t\ts[i][j]=getchar();\n\t\t\tif(s[i][j]=='a')ax=i,ay=j;\n\t\t\tif(s[i][j]=='A')bx=i,by=j;\n\t\t\tif(s[i][j]=='b')cx=i,cy=j;\n\t\t\tif(s[i][j]=='B')ex=i,ey=j;\n\t\t\tif(s[i][j]=='c')fx=i,fy=j;\n\t\t\tif(s[i][j]=='C')gx=i,gy=j;\n\t\t}\n\t}\n\t\n\tqueue<nodenodenode>q;\n\tq.push({ax,ay,cx,cy,fx,fy,0,1});\n\tq.push({bx,by,ex,ey,gx,gy,0,2});\n\tshort vis[17][17][17][17][17][17]={};\n\tshort d[17][17][17][17][17][17]={};\n\tvis[ax][ay][cx][cy][fx][fy]=1;\n\tvis[bx][by][ex][ey][gx][gy]=2;\n\twhile(!q.empty()){\n\t\tauto qwe=q.front(); q.pop();\n\t\tint x=qwe.x,y=qwe.y,xx=qwe.xx,yy=qwe.yy,\n\t\t\txxx=qwe.xxx,yyy=qwe.yyy,p=qwe.p,typ=qwe.typ;\n//\t\tcout<<x<<\" \"<<y<<\" \"<<xx<<\" \"<<yy<<\" \"<<xxx<<\" \"<<yyy<<\"\\n\";\n//\t\tcout<<d[x][y][xx][yy][xxx][yyy]<<\"\\n\";\n//\t\tcout<<x<<\" \"<<y<<\" \"<<xx<<\" \"<<yy<<\" \"<<xxx<<\" \"<<yyy<<\" \"<<p<<\"\\n\";\n//\t\tchar r[22][22]={};\n//\t\tfor(int i=1;i<=n;i++)\n//\t\t\tfor(int j=1;j<=m;j++){\n//\t\t\t\tr[i][j]=s[i][j];\n//\t\t\t\tif(r[i][j]>='A'&&r[i][j]<='z')r[i][j]=' ';\n//\t\t\t}\n//\t\tr[x][y]='a',r[xx][yy]='b',r[xxx][yyy]='c';\n//\t\tfor(int i=1;i<=n;i++){\n//\t\t\tfor(int j=1;j<=m;j++)cout<<r[i][j];\n//\t\t\tcout<<\"\\n\";\n//\t\t}\n//\t\t\n//\t\tif(x==bx&&y==by&&xx==ex&&yy==ey&&xxx==gx&&yyy==gy){\n//\t\t\tcout<<p<<\"\\n\";\n//\t\t\treturn ;\n//\t\t}\n\t\tfor(int i=0;i<5;i++){\n\t\t\tint X=dx[i]+x,Y=dy[i]+y;\n\t\t\tif(X<1||X>n||Y<1||Y>m||s[X][Y]=='#')continue;\n\t\t\tfor(int j=0;j<5;j++){\n\t\t\t\tint XX=dx[j]+xx,YY=dy[j]+yy;\n\t\t\t\tif(XX<1||XX>n||YY<1||YY>m||s[XX][YY]=='#')continue;\n\t\t\t\tif(X==xx&&Y==yy&&XX==x&&YY==y)continue;\n\t\t\t\tif(X==XX&&Y==YY)continue;\n\t\t\t\tfor(int w=0;w<5;w++){\n\t\t\t\t\tint XXX=dx[w]+xxx,YYY=dy[w]+yyy;\n\t\t\t\t\tif(XXX<1||XXX>n||YYY<1||YYY>m||s[XXX][YYY]=='#')continue;\n\t\t\t\t\tif(X==xxx&&Y==yyy&&XXX==x&&YYY==y)continue;\n\t\t\t\t\tif(XX==xxx&&YY==yyy&&XXX==xx&&YYY==yy)continue;\n\t\t\t\t\tif(XX==XXX&&YY==YYY)continue;\n\t\t\t\t\tif(X==XXX&&Y==YYY)continue;\n\t\t\t\t\tif(vis[X][Y][XX][YY][XXX][YYY]==typ)continue;\n\t\t\t\t\tif(vis[X][Y][XX][YY][XXX][YYY]+typ==3){\n\t\t\t\t\t\tcout<<d[X][Y][XX][YY][XXX][YYY]+\n\t\t\t\t\t\t\t\td[x][y][xx][yy][xxx][yyy]+1<<\"\\n\";\n\t\t\t\t\t\treturn ;\n\t\t\t\t\t}\n\t\t\t\t\tvis[X][Y][XX][YY][XXX][YYY]=typ;\n\t\t\t\t\td[X][Y][XX][YY][XXX][YYY]=\n\t\t\t\t\t\t\td[x][y][xx][yy][xxx][yyy]+1;\n\t\t\t\t\tq.push({X,Y,XX,YY,XXX,YYY,p+1,typ});\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n}\n\nsigned main(){\n//\tfreopen(\"halloween.in\",\"r\",stdin);\n//\tfreopen(\"halloween.out\",\"w\",stdout);\n//\tios::sync_with_stdio(0);\n//\tcin.tie(0);\n\twhile(1){\n\t\tcin>>m>>n>>x;\n\t\tif(n==0)return 0;\n\t\tif(x==1)solve1();\n\t\tif(x==2)solve2();\n\t\tif(x==3)solve3();\n\t}\n}", "accuracy": 1, "time_ms": 1090, "memory_kb": 99580, "score_of_the_acc": -1.0285, "final_rank": 13 }, { "submission_id": "aoj_1281_10109919", "code_snippet": "#include <cstdio>\n#include <vector>\n#include <algorithm>\n#include <array>\n#include <queue>\nusing namespace std;\nusing i64 = long long;\n#define rep(i,n) for(i64 i=0; i<(i64)(n); i++)\n\nint dist[300][300][300];\n\nint solve(int w, int z, int n, vector<int> pos0, vector<int> pos1, vector<int> G){\n rep(i,z) rep(j,z) rep(k,z) dist[i][j][k] = -1;\n int dx[5] = { 0,-1,1,-w,w };\n dist[pos0[0]][pos0[1]][pos0[2]] = 0;\n queue<array<int,3>> que; que.push({ pos0[0], pos0[1], pos0[2] });\n while(que.size()){\n auto [a,b,c] = que.front(); que.pop();\n for(int da:dx){\n int nxa = a + da;\n if(G[nxa] != 1) continue;\n for(int db:dx){\n int nxb = b + db;\n if(G[nxb] != 1) continue;\n if(n!=1) if(nxb==nxa || (a==nxb && b==nxa)) continue;\n for(int dc:dx){\n int nxc = c + dc;\n if(G[nxc] != 1) continue;\n if(dist[nxa][nxb][nxc] != -1) continue;\n if(n<=2 || (nxb!=nxc && nxc!=nxa && !(a==nxc && c==nxa) && !(c==nxb && b==nxc))){\n dist[nxa][nxb][nxc] = dist[a][b][c] + 1;\n que.push({ nxa, nxb, nxc });\n }\n }\n }\n }\n }\n return dist[pos1[0]][pos1[1]][pos1[2]];\n}\n\nvoid testcase(){\n int w, h, n; scanf(\"%d%d%d\", &w, &h, &n);\n if(w == 0) exit(0);\n vector<int> G;\n vector<int> pos0(n);\n vector<int> pos1(n);\n rep(y,h){\n scanf(\" \");\n rep(x,w){\n char c; scanf(\"%c\", &c);\n G.push_back(c == '#' ? 0 : 1);\n if('a' <= c && c <= 'c') pos1[c-'a'] = y*w+x;\n if('A' <= c && c <= 'C') pos0[c-'A'] = y*w+x;\n }\n }\n rep(ss,3-n){ pos0.push_back(h*w-w); pos1.push_back(h*w-w); }\n G.push_back(0); G[h*w-w] = 1;\n int ans = solve(w, h*w+1, n, pos0, pos1, G);\n printf(\"%d\\n\", ans);\n}\n\nint main(){\n while(true) testcase();\n return 0;\n}", "accuracy": 1, "time_ms": 1100, "memory_kb": 96508, "score_of_the_acc": -0.9987, "final_rank": 12 }, { "submission_id": "aoj_1281_9734690", "code_snippet": "#include <bits/stdc++.h>\n\nusing namespace std;\n\n#define rep(i, a, b) for (int i = (int)(a); i < (int)(b); i++)\n#define rrep(i, a, b) for (int i = (int)(b)-1; i >= (int)(a); i--)\n#define ALL(v) (v).begin(), (v).end()\n#define UNIQUE(v) sort(ALL(v)), (v).erase(unique(ALL(v)), (v).end())\n#define SZ(v) (int)v.size()\n#define MIN(v) *min_element(ALL(v))\n#define MAX(v) *max_element(ALL(v))\n#define LB(v, x) int(lower_bound(ALL(v), (x)) - (v).begin())\n#define UB(v, x) int(upper_bound(ALL(v), (x)) - (v).begin())\n\nusing uint = unsigned int;\nusing ll = long long int;\nusing ull = unsigned long long;\nusing i128 = __int128_t;\nusing u128 = __uint128_t;\nconst int inf = 0x3fffffff;\nconst ll INF = 0x1fffffffffffffff;\n\ntemplate <typename T> inline bool chmax(T &a, T b) {\n if (a < b) {\n a = b;\n return 1;\n }\n return 0;\n}\ntemplate <typename T> inline bool chmin(T &a, T b) {\n if (a > b) {\n a = b;\n return 1;\n }\n return 0;\n}\ntemplate <typename T, typename U> T ceil(T x, U y) {\n assert(y != 0);\n if (y < 0)\n x = -x, y = -y;\n return (x > 0 ? (x + y - 1) / y : x / y);\n}\ntemplate <typename T, typename U> T floor(T x, U y) {\n assert(y != 0);\n if (y < 0)\n x = -x, y = -y;\n return (x > 0 ? x / y : (x - y + 1) / y);\n}\ntemplate <typename T> int popcnt(T x) {\n return __builtin_popcountll(x);\n}\ntemplate <typename T> int topbit(T x) {\n return (x == 0 ? -1 : 63 - __builtin_clzll(x));\n}\ntemplate <typename T> int lowbit(T x) {\n return (x == 0 ? -1 : __builtin_ctzll(x));\n}\n\ntemplate <class T, class U>\nostream &operator<<(ostream &os, const pair<T, U> &p) {\n os << \"P(\" << p.first << \", \" << p.second << \")\";\n return os;\n}\ntemplate <typename T> ostream &operator<<(ostream &os, const vector<T> &vec) {\n os << \"{\";\n for (int i = 0; i < vec.size(); i++) {\n os << vec[i] << (i + 1 == vec.size() ? \"\" : \", \");\n }\n os << \"}\";\n return os;\n}\ntemplate <typename T, typename U>\nostream &operator<<(ostream &os, const map<T, U> &map_var) {\n os << \"{\";\n for (auto itr = map_var.begin(); itr != map_var.end(); itr++) {\n os << \"(\" << itr->first << \", \" << itr->second << \")\";\n itr++;\n if (itr != map_var.end())\n os << \", \";\n itr--;\n }\n os << \"}\";\n return os;\n}\ntemplate <typename T> ostream &operator<<(ostream &os, const set<T> &set_var) {\n os << \"{\";\n for (auto itr = set_var.begin(); itr != set_var.end(); itr++) {\n os << *itr;\n ++itr;\n if (itr != set_var.end())\n os << \", \";\n itr--;\n }\n os << \"}\";\n return os;\n}\n#ifdef LOCAL\n#define show(...) _show(0, #__VA_ARGS__, __VA_ARGS__)\n#else\n#define show(...) true\n#endif\ntemplate <typename T> void _show(int i, T name) {\n cerr << '\\n';\n}\ntemplate <typename T1, typename T2, typename... T3>\nvoid _show(int i, const T1 &a, const T2 &b, const T3 &...c) {\n for (; a[i] != ',' && a[i] != '\\0'; i++)\n cerr << a[i];\n cerr << \":\" << b << \" \";\n _show(i + 1, a, c...);\n}\n\n/**\n * @brief template\n */\n\nint dx[5] = {-16,-1,0,1,16};\nint encode(int X, int Y) {return X*16+Y;}\n\nint main() {\nwhile(1) {\n int N, M, K;\n cin >> M >> N >> K;\n if (N == 0) return 0;\n bool G[256] = {};\n rep(i,0,256) G[i] = false;\n vector<pair<char,int>> s, t;\n string _;\n getline(cin,_);\n rep(i,0,N) {\n getline(cin,_);\n rep(j,0,M) {\n char C = _[j];\n if (C != '#') G[encode(i,j)] = true;\n if ('a' <= C && C <= 'z') s.push_back({C,encode(i,j)});\n if ('A' <= C && C <= 'Z') t.push_back({C,encode(i,j)});\n }\n }\n sort(ALL(s)), sort(ALL(t));\n vector<int> S(K), T(K);\n rep(i,0,K) S[i] = s[i].second, T[i] = t[i].second;\n if (K == 1) {\n static int DP[256] = {};\n rep(i,0,256) DP[i] = inf;\n queue<int> Q;\n DP[S[0]] = 0, Q.push(S[0]);\n while(!Q.empty()) {\n int V = Q.front();\n Q.pop();\n rep(i,0,5) {\n int NV = V + dx[i];\n if (G[NV]) {\n if (chmin(DP[NV],DP[V]+1)) {\n Q.push(NV);\n }\n }\n }\n }\n cout << DP[T[0]] << endl;\n }\n if (K == 2) {\n static int DP[256][256] = {};\n rep(i,0,256) rep(j,0,256) DP[i][j] = inf;\n queue<pair<int,int>> Q;\n DP[S[0]][S[1]] = 0, Q.push({S[0],S[1]});\n while(!Q.empty()) {\n auto [V1,V2] = Q.front();\n Q.pop();\n rep(i,0,5) {\n int NV1 = V1 + dx[i];\n if (!G[NV1]) continue;\n rep(j,0,5) {\n int NV2 = V2 + dx[j];\n if (!G[NV2]) continue;\n if (NV1 == NV2) continue;\n if ((NV1 == V2) && (NV2 == V1)) continue;\n if (chmin(DP[NV1][NV2], DP[V1][V2] + 1)) {\n Q.push({NV1,NV2});\n }\n }\n }\n }\n cout << DP[T[0]][T[1]] << endl;\n }\n if (K == 3) {\n static int DP[256][256][256] = {};\n rep(i,0,256) rep(j,0,256) rep(k,0,256) DP[i][j][k] = inf;\n queue<tuple<int,int,int>> Q;\n DP[S[0]][S[1]][S[2]] = 0, Q.push({S[0],S[1],S[2]});\n while(!Q.empty()) {\n auto [V1,V2,V3] = Q.front();\n Q.pop();\n rep(i,0,5) {\n int NV1 = V1 + dx[i];\n if (!G[NV1]) continue;\n rep(j,0,5) {\n int NV2 = V2 + dx[j];\n if (!G[NV2]) continue;\n if (NV1 == NV2) continue;\n if ((NV1 == V2) && (NV2 == V1)) continue;\n rep(k,0,5) {\n int NV3 = V3 + dx[k];\n if (!G[NV3]) continue;\n if (NV1 == NV3) continue;\n if ((NV1 == V3) && (NV3 == V1)) continue;\n if (NV2 == NV3) continue;\n if ((NV2 == V3) && (NV3 == V2)) continue;\n if (chmin(DP[NV1][NV2][NV3],DP[V1][V2][V3]+1)) {\n Q.push({NV1,NV2,NV3});\n }\n }\n }\n }\n }\n cout << DP[T[0]][T[1]][T[2]] << endl;\n }\n}\n}", "accuracy": 1, "time_ms": 1460, "memory_kb": 71744, "score_of_the_acc": -0.8023, "final_rank": 4 }, { "submission_id": "aoj_1281_9591158", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nconst int N = 17;\n\nchar a[N][N];\nint h, w, n;\nint dist[N * N][N * N][N * N];\nvector<int> to[N * N];\n\ninline bool check(int x, int x1, int y, int y1, int z, int z1) {\n if (y == 0 && z == 0)\n return true;\n if (x1 == y1 || x1 == z1 || z1 == y1)\n return false;\n if (x == y1 && y == x1)\n return false;\n if (x == z1 && x1 == z)\n return false;\n if (y == z1 && y1 == z)\n return false;\n return true;\n}\n\nint bfs(vector<int> &start, vector<int> fin) {\n deque<vector<int>> q;\n q.emplace_back(start);\n dist[start[0]][start[1]][start[2]] = 0;\n while (!q.empty()) {\n int x = q.front()[0], y = q.front()[1], z = q.front()[2];\n q.pop_front();\n for (auto x1 : to[x]) {\n for (auto y1 : to[y]) {\n for (auto z1 : to[z]) {\n if (dist[x1][y1][z1] != -1 || (n > 1 && !check(x, x1, y, y1, z, z1)))\n continue;\n dist[x1][y1][z1] = dist[x][y][z] + 1;\n vector<int> vec;\n vec.emplace_back(x1);\n vec.emplace_back(y1);\n vec.emplace_back(z1);\n q.emplace_back(vec);\n }\n }\n }\n }\n return dist[fin[0]][fin[1]][fin[2]];\n}\n\nvoid solve() {\n string input;\n while (true) {\n getline(cin, input);\n for (int i = 0; i < N * N; i++) {\n for (int j = 0; j < N * N; j++) {\n for (int z = 0; z < N * N; z++) {\n dist[i][j][z] = -1;\n }\n }\n }\n\n for (int i = 0; i < N * N; i++) {\n to[i].clear();\n }\n\n\n string s;\n vector<int> pars;\n for (auto it : input) {\n if (it == ' ') {\n pars.emplace_back(stoi(s));\n s = \"\";\n }\n else\n s += it;\n }\n pars.emplace_back(stoi(s));\n w = pars[0], h = pars[1], n = pars[2];\n if (h == 0)\n break;\n for (int i = 0; i < h; i++) {\n getline(cin, input);\n for (int j = 0; j < w; j++) {\n a[i][j] = input[j];\n }\n }\n int cnt = 1;\n vector<vector<int>> num(h, vector<int> (w)); \n for (int i = 0; i < h; i++) {\n for (int j = 0; j < w; j++) {\n num[i][j] = cnt++;\n }\n }\n to[0] = {0};\n vector<pair<int, int>> dif;\n dif.emplace_back(-1, 0);\n dif.emplace_back(1, 0);\n dif.emplace_back(0, 0);\n dif.emplace_back(0, -1);\n dif.emplace_back(0, 1);\n for (int i = 0; i < h; i++) {\n for (int j = 0; j < w; j++) {\n for (auto it : dif) {\n int dx = it.first, dy = it.second;\n if (i + dx < 0 || i + dx >= h || j + dy < 0 || j + dy >= w || a[i + dx][j + dy] == '#')\n continue;\n to[num[i][j]].emplace_back(num[i + dx][j + dy]);\n }\n }\n }\n map<char, int> map_start, map_finish;\n for (int i = 0; i < h; i++) {\n for (int j = 0; j < w; j++) {\n if (islower(a[i][j])) {\n map_start[a[i][j]] = num[i][j];\n }\n if (isupper(a[i][j])) {\n map_finish[a[i][j]] = num[i][j];\n }\n }\n }\n vector<int> start, fin;\n for (auto it : map_start)\n start.emplace_back(it.second);\n for (auto it : map_finish)\n fin.emplace_back(it.second);\n while (start.size() < 3)\n start.emplace_back(0);\n while (fin.size() < 3)\n fin.emplace_back(0);\n cout << bfs(start, fin) << endl;\n }\n}\n\nsigned main() {\n ios::sync_with_stdio(false);\n cin.tie(nullptr);\n solve();\n}", "accuracy": 1, "time_ms": 1460, "memory_kb": 111588, "score_of_the_acc": -1.2098, "final_rank": 19 }, { "submission_id": "aoj_1281_9591145", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n#define eb emplace_back\nconst int N = 17;\n\nchar a[N][N];\nint h, w, n;\nint dist[N * N][N * N][N * N];\nvector<int> to[N * N];\n\ninline bool check(int x, int x1, int y, int y1, int z, int z1) {\n if (y == 0 && z == 0)\n return true;\n if (x1 == y1 || x1 == z1 || z1 == y1)\n return false;\n if (x == y1 && y == x1)\n return false;\n if (x == z1 && x1 == z)\n return false;\n if (y == z1 && y1 == z)\n return false;\n return true;\n}\n\nint bfs(vector<int> &start, vector<int> fin) {\n deque<vector<int>> q;\n q.eb(start);\n dist[start[0]][start[1]][start[2]] = 0;\n while (!q.empty()) {\n int x = q.front()[0], y = q.front()[1], z = q.front()[2];\n q.pop_front();\n for (auto x1 : to[x]) {\n for (auto y1 : to[y]) {\n for (auto z1 : to[z]) {\n if (dist[x1][y1][z1] != -1 || (n > 1 && !check(x, x1, y, y1, z, z1)))\n continue;\n dist[x1][y1][z1] = dist[x][y][z] + 1;\n vector<int> vec = {x1, y1, z1};\n q.eb(vec);\n }\n }\n }\n }\n return dist[fin[0]][fin[1]][fin[2]];\n}\n\nvoid solve() {\n string input;\n while (true) {\n getline(cin, input);\n for (int i = 0; i < N * N; i++) {\n for (int j = 0; j < N * N; j++) {\n for (int z = 0; z < N * N; z++) {\n dist[i][j][z] = -1;\n }\n }\n }\n\n for (int i = 0; i < N * N; i++) {\n to[i].clear();\n }\n\n\n string s;\n vector<int> pars;\n for (auto it : input) {\n if (it == ' ') {\n pars.eb(stoi(s));\n s = \"\";\n }\n else\n s += it;\n }\n pars.eb(stoi(s));\n w = pars[0], h = pars[1], n = pars[2];\n if (h == 0)\n break;\n for (int i = 0; i < h; i++) {\n getline(cin, input);\n for (int j = 0; j < w; j++) {\n a[i][j] = input[j];\n }\n }\n int cnt = 1;\n vector<vector<int>> num(h, vector<int> (w)); \n for (int i = 0; i < h; i++) {\n for (int j = 0; j < w; j++) {\n num[i][j] = cnt++;\n }\n }\n to[0] = {0};\n vector<pair<int, int>> dif = {{-1, 0}, {1, 0}, {0, 0}, {0, -1}, {0, 1}};\n for (int i = 0; i < h; i++) {\n for (int j = 0; j < w; j++) {\n for (auto it : dif) {\n int dx = it.first, dy = it.second;\n if (i + dx < 0 || i + dx >= h || j + dy < 0 || j + dy >= w || a[i + dx][j + dy] == '#')\n continue;\n to[num[i][j]].eb(num[i + dx][j + dy]);\n }\n }\n }\n map<char, int> map_start, map_finish;\n for (int i = 0; i < h; i++) {\n for (int j = 0; j < w; j++) {\n if (islower(a[i][j])) {\n map_start[a[i][j]] = num[i][j];\n }\n if (isupper(a[i][j])) {\n map_finish[a[i][j]] = num[i][j];\n }\n }\n }\n vector<int> start, fin;\n for (auto it : map_start)\n start.eb(it.second);\n for (auto it : map_finish)\n fin.eb(it.second);\n while (start.size() < 3)\n start.eb(0);\n while (fin.size() < 3)\n fin.eb(0);\n cout << bfs(start, fin) << endl;\n }\n}\n\nsigned main() {\n ios::sync_with_stdio(false);\n cin.tie(nullptr);\n solve();\n}", "accuracy": 1, "time_ms": 960, "memory_kb": 111460, "score_of_the_acc": -1.1295, "final_rank": 17 }, { "submission_id": "aoj_1281_9591120", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n#define eb emplace_back\nconst int N = 17;\n\nchar a[N][N];\nint h, w, n;\nint dist[N * N][N * N][N * N];\nvector<int> to[N * N];\n\ninline bool check(int x, int x1, int y, int y1, int z, int z1) {\n if (y == 0 && z == 0)\n return true;\n if (x1 == y1 || x1 == z1 || z1 == y1)\n return false;\n if (x == y1 && y == x1)\n return false;\n if (x == z1 && x1 == z)\n return false;\n if (y == z1 && y1 == z)\n return false;\n return true;\n}\n\nint bfs(vector<int> &start, vector<int> fin) {\n deque<vector<int>> q;\n q.eb(start);\n dist[start[0]][start[1]][start[2]] = 0;\n while (!q.empty()) {\n int x = q.front()[0], y = q.front()[1], z = q.front()[2];\n q.pop_front();\n for (auto x1 : to[x]) {\n for (auto y1 : to[y]) {\n for (auto z1 : to[z]) {\n if (dist[x1][y1][z1] != -1 || (n > 1 && !check(x, x1, y, y1, z, z1)))\n continue;\n dist[x1][y1][z1] = dist[x][y][z] + 1;\n vector<int> vec = {x1, y1, z1};\n q.eb(vec);\n }\n }\n }\n }\n return dist[fin[0]][fin[1]][fin[2]];\n}\n\nvoid solve() {\n string input;\n while (true) {\n getline(cin, input);\n for (int i = 0; i < N * N; i++) {\n for (int j = 0; j < N * N; j++) {\n for (int z = 0; z < N * N; z++) {\n dist[i][j][z] = -1;\n }\n }\n }\n\n for (int i = 0; i < N * N; i++) {\n to[i].clear();\n }\n\n\n string s;\n vector<int> pars;\n for (auto it : input) {\n if (it == ' ') {\n pars.eb(stoi(s));\n s = \"\";\n }\n else\n s += it;\n }\n pars.eb(stoi(s));\n w = pars[0], h = pars[1], n = pars[2];\n if (h == 0)\n break;\n for (int i = 0; i < h; i++) {\n getline(cin, input);\n for (int j = 0; j < w; j++) {\n a[i][j] = input[j];\n }\n }\n int cnt = 1;\n vector<vector<int>> num(h, vector<int> (w)); \n for (int i = 0; i < h; i++) {\n for (int j = 0; j < w; j++) {\n num[i][j] = cnt++;\n }\n }\n to[0] = {0};\n vector<pair<int, int>> dif = {{-1, 0}, {1, 0}, {0, 0}, {0, -1}, {0, 1}};\n for (int i = 0; i < h; i++) {\n for (int j = 0; j < w; j++) {\n for (auto [dx, dy] : dif) {\n if (i + dx < 0 || i + dx >= h || j + dy < 0 || j + dy >= w || a[i + dx][j + dy] == '#')\n continue;\n to[num[i][j]].eb(num[i + dx][j + dy]);\n }\n }\n }\n map<char, int> map_start, map_finish;\n for (int i = 0; i < h; i++) {\n for (int j = 0; j < w; j++) {\n if (islower(a[i][j])) {\n map_start[a[i][j]] = num[i][j];\n }\n if (isupper(a[i][j])) {\n map_finish[a[i][j]] = num[i][j];\n }\n }\n }\n vector<int> start, fin;\n for (auto it : map_start)\n start.eb(it.second);\n for (auto it : map_finish)\n fin.eb(it.second);\n while (start.size() < 3)\n start.eb(0);\n while (fin.size() < 3)\n fin.eb(0);\n cout << bfs(start, fin) << endl;\n }\n}\n\nsigned main() {\n ios::sync_with_stdio(false);\n cin.tie(nullptr);\n solve();\n}", "accuracy": 1, "time_ms": 960, "memory_kb": 111620, "score_of_the_acc": -1.1311, "final_rank": 18 }, { "submission_id": "aoj_1281_9485479", "code_snippet": "// #pragma GCC target(\"avx2\")\n#pragma GCC optimization(\"O3\")\n#pragma GCC optimization(\"unroll-loops\")\n\n#include <array>\n#include <bitset>\n#include <cmath>\n#include <ios>\n#include <iostream>\n#include <queue>\n\nusing namespace std;\n\nconst int inf = 2e9;\n\nint main() {\n#ifdef erytw\n freopen(\"input.txt\", \"r\", stdin);\n freopen(\"output.txt\", \"w\", stdout);\n#endif\n\n auto decode = [](int pos) {\n return array<int, 3>{pos & ((1 << 8) - 1), (pos >> 8) & ((1 << 8) - 1),\n (pos >> 16) & ((1 << 8) - 1)};\n };\n\n auto encode = [](array<int, 3> coords) {\n return coords[0] | (coords[1] << 8) | (coords[2] << 16);\n };\n\n int w, h, n;\n array<int, 1 << 24> dist;\n array<int, 3> start, finish;\n bitset<1 << 24> used, renewed;\n bitset<1 << 8> avaliable;\n\n array<int, 5> steps{0, 1, (1 << 4), -1, -(1 << 4)};\n auto stepvar = [&](int pos) {\n vector<int> step_list;\n\n return step_list;\n };\n\n string line;\n while (cin >> w >> h >> n) {\n if ((w | h | n) == 0) break;\n\n getline(cin, line);\n\n avaliable.set();\n used.reset();\n renewed.reset();\n start = finish = {0, 0, 0};\n\n for (int y = 0; y < h; ++y) {\n getline(cin, line);\n for (int x = 0; x < w; ++x) {\n if (line[x] == '#')\n avaliable[x | (y << 4)] = false;\n else if (line[x] - 'A' < 3)\n finish[line[x] - 'A'] = x | (y << 4);\n else if (line[x] - 'a' < 3)\n start[line[x] - 'a'] = x | (y << 4);\n }\n }\n\n int spos = encode(start), fpos = encode(finish);\n\n queue<int> q;\n q.push(spos);\n dist[spos] = 0;\n renewed[spos] = 1;\n\n while (!q.empty()) {\n int pos = q.front();\n q.pop();\n used[pos] = true;\n\n if (pos == fpos) break;\n\n auto [a, b, c] = decode(pos);\n\n for (int step_a : steps) {\n for (int step_b : steps) {\n for (int step_c : steps) {\n if ((step_a | (n >= 2 ? step_b : 0) | (n >= 3 ? step_c : 0)) == 0)\n continue;\n\n int n_a = a + step_a, n_b = b + (n >= 2 ? step_b : 0),\n n_c = c + (n >= 3 ? step_c : 0);\n\n if (!avaliable[n_a] || n >= 2 && !avaliable[n_b] ||\n n >= 3 && !avaliable[n_c])\n continue;\n if (n >= 2 && n_a == n_b ||\n (n >= 3 && (n_a == n_c || n_b == n_c)))\n continue;\n if ((n >= 2 && a == n_b && b == n_a) ||\n (n >= 3 &&\n ((a == n_c && c == n_a) || (b == n_c && c == n_b))))\n continue;\n\n int npos = encode({n_a, n_b, n_c});\n if (!renewed[npos]) {\n dist[npos] = inf;\n renewed[npos] = true;\n }\n if (dist[npos] > dist[pos] + 1) {\n dist[npos] = dist[pos] + 1;\n if (used[npos]) continue;\n q.push(npos);\n }\n }\n }\n }\n }\n cout << dist[fpos] << '\\n';\n }\n}", "accuracy": 1, "time_ms": 3710, "memory_kb": 68356, "score_of_the_acc": -1.1231, "final_rank": 16 }, { "submission_id": "aoj_1281_9221531", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\nint solve(int w, int h, int n) {\n vector<string> g(h, string(w, '?'));\n vector<pair<int,int>> src(n, {-1, -1}), dst(n, {-1, -1});\n cin.ignore();\n for(int i = 0; i < h; i++) {\n string s; getline(cin, s);\n assert((int)s.size() == w);\n for(int j = 0; j < w; j++) {\n if(s[j] == '#') g[i][j] = '#';\n else if(s[j] == ' ') g[i][j] = '.';\n else {\n g[i][j] = '.';\n if(isupper(s[j])) {\n src[s[j] - 'A'] = {i, j};\n } else {\n dst[s[j] - 'a'] = {i, j};\n }\n }\n }\n }\n\n auto encode = [&](vector<pair<int,int>>& state) {\n int res = 0;\n for(int i = 0; i < n; i++) res = res * h * w + (state[i].first * w + state[i].second); \n return res;\n };\n\n auto decode = [&](int v) {\n vector<pair<int,int>> state;\n for(int i = 0; i < n; i++) {\n const int q = v % w; v /= w;\n const int p = v % h; v /= h;\n state.push_back({p, q});\n }\n reverse(state.begin(), state.end());\n return state;\n };\n\n const int sz = pow(h * w, n);\n const int INF = 1e9;\n vector dist(sz, INF);\n queue<int> q;\n dist[encode(src)] = 0;\n q.push(encode(src));\n int goal = encode(dst);\n\n const int dx[] = {0, 0, +1, 0, -1};\n const int dy[] = {0, +1, 0, -1, 0};\n while(not q.empty()) {\n const int u = q.front(); q.pop();\n vector<pair<int,int>> now = decode(u);\n vector<pair<int,int>> now_ = now;\n\n auto dfs = [&](auto self, vector<pair<int,int>>& pos, int i) -> void {\n if(i == n) {\n const int v = encode(pos);\n if(dist[v] != INF) return;\n for(int k = 0; k < n; k++) {\n for(int l = k + 1; l < n; l++) {\n if(pos[k] == pos[l]) return;\n if(pos[l] == now[k] and pos[k] == now[l]) return;\n }\n }\n dist[v] = dist[u] + 1;\n q.push(v);\n return;\n }\n\n for(int d = 0; d < 5; d++) {\n if(g[pos[i].first + dx[d]][pos[i].second + dy[d]] == '.') {\n pos[i].first += dx[d];\n pos[i].second += dy[d];\n self(self, pos, i + 1);\n pos[i].first -= dx[d];\n pos[i].second -= dy[d];\n }\n }\n }; dfs(dfs, now_, 0);\n }\n return dist[encode(dst)];\n}\n\nint main() {\n while(true) {\n int w, h, n; cin >> w >> h >> n; if(make_tuple(w, h, n) == make_tuple(0, 0, 0)) return 0;\n int answer = solve(w, h, n);\n cout << answer << endl;\n }\n}", "accuracy": 1, "time_ms": 3230, "memory_kb": 69848, "score_of_the_acc": -1.0625, "final_rank": 14 }, { "submission_id": "aoj_1281_8860394", "code_snippet": "#include <cctype>\n#include <cstdio>\n#include <cstring>\n#include <queue>\nusing namespace std;\nunsigned short vis[1 << 24];\nchar c[16][20];\nbool wall[256];\nint tm0;\ninline bool check(int u, int v) {\n for (int i = 0; i < 24; i += 8) {\n int p1 = v >> i & 255;\n if (p1) {\n int j = i == 16 ? 0 : i + 8;\n int p2 = (v >> j % 24) & 255;\n int uShiftedJ = u >> j % 24 & 255;\n int uShiftedI = u >> i % 24 & 255;\n if (wall[p1] || p1 == p2 || (p1 == uShiftedJ && p2 == uShiftedI)) {\n return false;\n }\n }\n }\n return true;\n}\nint solve(int start, int goal) {\n const int dif[5] = {0, -1, 1, -16, 16};\n queue<int> q;\n q.push(start);\n q.push(-1);\n int tm = tm0 + 1;\n vis[start] = tm;\n while (1) {\n int u = q.front();\n q.pop();\n if (u < 0) {\n q.push(-1);\n ++tm;\n } else {\n for (int i1 = u & 255 ? 4 : 0; i1 >= 0; --i1)\n for (int i2 = u >> 8 & 255 ? 4 : 0; i2 >= 0; --i2)\n for (int i3 = u >> 16 ? 4 : 0; i3 >= 0; --i3) {\n int v = u + dif[i1] + (dif[i2] << 8) + (dif[i3] << 16);\n if (vis[v] <= tm0) {\n if (check(u, v)) {\n if (v == goal) {\n return tm;\n }\n vis[v] = tm;\n q.push(v);\n }\n }\n }\n }\n }\n}\nint main() {\n int w, h;\n while (scanf(\"%d%d%*d \", &w, &h), w) {\n for (int i = 0; i < h; ++i) {\n fgets(c[i], 20, stdin);\n }\n int goal = 0;\n int start = 0;\n for (int i = 0; i < h; ++i) {\n for (int j = 0; j < w; ++j) {\n if (c[i][j] == '#') {\n wall[i << 4 | j] = true;\n } else {\n wall[i << 4 | j] = false;\n if (isupper(c[i][j])) {\n goal |= (i << 4 | j) << (c[i][j] - 'A') * 8;\n } else if (islower(c[i][j])) {\n start |= (i << 4 | j) << (c[i][j] - 'a') * 8;\n }\n }\n }\n }\n int tm = solve(start, goal);\n printf(\"%d\\n\", tm - tm0);\n tm0 = tm + 1;\n if (tm0 > 55000) {\n memset(vis, 0, sizeof vis);\n tm0 = 0;\n }\n }\n}", "accuracy": 1, "time_ms": 6460, "memory_kb": 25524, "score_of_the_acc": -1.1195, "final_rank": 15 }, { "submission_id": "aoj_1281_8860391", "code_snippet": "#include <cctype>\n#include <cstdio>\n#include <cstring>\n#include <queue>\nusing namespace std;\nunsigned short vis[1 << 24];\nchar c[16][20];\nbool wall[256];\nint tm0;\ninline bool check(int u, int v) {\n for (int i = 0; i < 24; i += 8) {\n int p1 = v >> i & 255;\n if (p1) {\n int j = i == 16 ? 0 : i + 8;\n if (wall[p1]) {\n return false;\n }\n int p2 = (v >> j % 24) & 255;\n int u_shift_j = (u >> j % 24 & 255);\n int u_shift_i = (u >> i % 24 & 255);\n if (p1 == p2 || (p1 == u_shift_j && p2 == u_shift_i)) {\n return false;\n }\n }\n }\n return true;\n}\nint solve(int start, int goal) {\n const int dif[5] = {0, -1, 1, -16, 16};\n queue<int> q;\n q.push(start);\n q.push(-1);\n int tm = tm0 + 1;\n vis[start] = tm;\n while (1) {\n int u = q.front();\n q.pop();\n if (u < 0) {\n q.push(-1);\n ++tm;\n } else {\n for (int i1 = u & 255 ? 4 : 0; i1 >= 0; --i1) {\n int v_i1 = dif[i1];\n for (int i2 = u >> 8 & 255 ? 4 : 0; i2 >= 0; --i2) {\n int v_i2 = dif[i2] << 8;\n for (int i3 = u >> 16 ? 4 : 0; i3 >= 0; --i3) {\n int v = u + v_i1 + v_i2 + (dif[i3] << 16);\n if (vis[v] > tm0) {\n continue;\n }\n if (check(u, v)) {\n if (v == goal) {\n return tm;\n }\n vis[v] = tm;\n q.push(v);\n }\n }\n }\n }\n }\n }\n}\nint main() {\n int w, h;\n while (scanf(\"%d%d%*d \", &w, &h), w) {\n for (int i = 0; i < h; ++i) {\n fgets(c[i], 20, stdin);\n }\n int goal = 0;\n int start = 0;\n for (int i = 0; i < h; ++i) {\n for (int j = 0; j < w; ++j) {\n if (c[i][j] == '#') {\n wall[i << 4 | j] = true;\n } else {\n wall[i << 4 | j] = false;\n if (isupper(c[i][j])) {\n goal |= (i << 4 | j) << (c[i][j] - 'A') * 8;\n } else if (islower(c[i][j])) {\n start |= (i << 4 | j) << (c[i][j] - 'a') * 8;\n }\n }\n }\n }\n int tm = solve(start, goal);\n printf(\"%d\\n\", tm - tm0);\n tm0 = (tm0 + tm + 1) % 60000; // Avoid resetting the vis array\n }\n}", "accuracy": 1, "time_ms": 4690, "memory_kb": 25420, "score_of_the_acc": -0.8388, "final_rank": 8 }, { "submission_id": "aoj_1281_8860390", "code_snippet": "#include <cctype>\n#include <cstdio>\n#include <cstring>\n#include <deque>\nusing namespace std;\nunsigned short vis[1 << 24];\nchar c[16][20];\nbool wall[256];\nint tm0;\ninline bool check(int u, int v) {\n for (int i = 0; i < 24; i += 8) {\n int p1 = v >> i & 255;\n if (p1) {\n int j = i == 16 ? 0 : i + 8;\n if (wall[p1]) {\n return false;\n }\n int p2 = (v >> j % 24) & 255;\n if (p1 == p2) {\n return false;\n }\n if (p1 == (u >> j % 24 & 255) && p2 == (u >> i % 24 & 255)) {\n return false;\n }\n }\n }\n return true;\n}\nint solve(int start, int goal) {\n const int dif[5] = {0, -1, 1, -16, 16};\n deque<int> q;\n q.push_back(start);\n q.push_back(-1);\n int tm = tm0 + 1;\n vis[start] = tm;\n while (1) {\n int u = q.front();\n q.pop_front();\n if (u < 0) {\n q.push_back(-1);\n ++tm;\n } else {\n int u8 = u >> 8 & 255;\n int u16 = u >> 16;\n for (int i1 = u & 255 ? 4 : 0; i1 >= 0; --i1)\n for (int i2 = u8 ? 4 : 0; i2 >= 0; --i2)\n for (int i3 = u16 ? 4 : 0; i3 >= 0; --i3) {\n int v = u + dif[i1] + (dif[i2] << 8) + (dif[i3] << 16);\n if (vis[v] > tm0) {\n continue;\n }\n if (check(u, v)) {\n if (v == goal) {\n return tm;\n }\n vis[v] = tm;\n q.push_back(v);\n }\n }\n }\n }\n}\nint main() {\n int w, h;\n while (scanf(\"%d%d%*d \", &w, &h), w) {\n for (int i = 0; i < h; ++i) {\n fgets(c[i], 20, stdin);\n }\n int goal = 0;\n int start = 0;\n for (int i = 0; i < h; ++i) {\n for (int j = 0; j < w; ++j) {\n if (c[i][j] == '#') {\n wall[i << 4 | j] = true;\n } else {\n wall[i << 4 | j] = false;\n if (isupper(c[i][j])) {\n goal |= (i << 4 | j) << (c[i][j] - 'A') * 8;\n } else if (islower(c[i][j])) {\n start |= (i << 4 | j) << (c[i][j] - 'a') * 8;\n }\n }\n }\n }\n int tm = solve(start, goal);\n printf(\"%d\\n\", tm - tm0);\n tm0 = tm + 1;\n if (tm0 > 55000) {\n memset(vis, 0, sizeof vis);\n tm0 = 0;\n }\n }\n}", "accuracy": 1, "time_ms": 4710, "memory_kb": 25408, "score_of_the_acc": -0.8418, "final_rank": 10 }, { "submission_id": "aoj_1281_8821561", "code_snippet": "#include <cctype>\n#include <cstdio>\n#include <cstring>\n#include <queue>\nusing namespace std;\nunsigned short vis[1 << 24];\nchar c[16][20];\nbool wall[256];\nint tm0;\ninline bool check(int u, int v) {\n for (int i = 0; i < 24; i += 8) {\n int p1 = v >> i & 255;\n if (p1) {\n int j = i == 16 ? 0 : i + 8;\n if (wall[p1]) {\n return false;\n }\n int p2 = (v >> j) & 255;\n if (p1 == p2) {\n return false;\n }\n if (p1 == (u >> j & 255) && p2 == (u >> i & 255)) {\n return false;\n }\n }\n }\n return true;\n}\ninline int solve(int start, int goal) {\n const int dif[5] = {0, -1, 1, -16, 16};\n queue<int> q;\n q.push(start);\n q.push(-1);\n int tm = tm0 + 1;\n vis[start] = tm;\n while (1) {\n int u = q.front();\n q.pop();\n if (u < 0) {\n q.push(-1);\n ++tm;\n } else {\n for (int i1 = u & 255 ? 4 : 0; i1 >= 0; --i1)\n for (int i2 = u >> 8 & 255 ? 4 : 0; i2 >= 0; --i2)\n for (int i3 = u >> 16 ? 4 : 0; i3 >= 0; --i3) {\n int v = u + dif[i1] + (dif[i2] << 8) + (dif[i3] << 16);\n if (vis[v] > tm0) {\n continue;\n }\n if (check(u, v)) {\n if (v == goal) {\n return tm;\n }\n vis[v] = tm;\n q.push(v);\n }\n }\n }\n }\n}\nint main() {\n int w, h;\n while (scanf(\"%d%d%*d \", &w, &h), w) {\n for (int i = 0; i < h; ++i) {\n fgets(c[i], 20, stdin);\n }\n int goal = 0;\n int start = 0;\n for (int i = 0; i < h; ++i) {\n for (int j = 0; j < w; ++j) {\n if (c[i][j] == '#') {\n wall[i << 4 | j] = true;\n } else {\n wall[i << 4 | j] = false;\n if (isupper(c[i][j])) {\n goal |= (i << 4 | j) << (c[i][j] - 'A') * 8;\n } else if (islower(c[i][j])) {\n start |= (i << 4 | j) << (c[i][j] - 'a') * 8;\n }\n }\n }\n }\n int tm = solve(start, goal);\n printf(\"%d\\n\", tm - tm0);\n tm0 = tm + 1;\n if (tm0 > 55000) {\n memset(vis, 0, sizeof vis);\n tm0 = 0;\n }\n }\n}", "accuracy": 1, "time_ms": 4540, "memory_kb": 25432, "score_of_the_acc": -0.8152, "final_rank": 7 }, { "submission_id": "aoj_1281_8821556", "code_snippet": "#include <cctype>\n#include <cstdio>\n#include <cstring>\n#include <queue>\nusing namespace std;\nunsigned short vis[1 << 24];\nchar c[16][20];\nbool wall[256];\nint tm0;\ninline bool check(int u, int v) {\n for (int i = 0; i < 24; i += 8) {\n int p1 = v >> i & 255;\n if (p1) {\n int j = i == 16 ? 0 : i + 8;\n if (wall[p1]) {\n return false;\n }\n int p2 = (v >> (j & 24)) & 255;\n if (p1 == p2) {\n return false;\n }\n if (p1 == (u >> (j & 24) & 255) && p2 == (u >> (i & 24) & 255)) {\n return false;\n }\n }\n }\n return true;\n}\nint solve(int start, int goal) {\n const int dif[5] = {0, -1, 1, -16, 16};\n queue<int> q;\n q.push(start);\n q.push(-1);\n int tm = tm0 + 1;\n vis[start] = tm;\n while (1) {\n int u = q.front();\n q.pop();\n if (u < 0) {\n q.push(-1);\n ++tm;\n } else {\n for (int i1 = u & 255 ? 4 : 0; i1 >= 0; --i1)\n for (int i2 = u >> 8 & 255 ? 4 : 0; i2 >= 0; --i2)\n for (int i3 = u >> 16 ? 4 : 0; i3 >= 0; --i3) {\n int v = u + dif[i1] + (dif[i2] << 8) + (dif[i3] << 16);\n if (vis[v] > tm0) {\n continue;\n }\n if (check(u, v)) {\n if (v == goal) {\n return tm;\n }\n vis[v] = tm;\n q.push(v);\n }\n }\n }\n }\n}\nint main() {\n int w, h;\n while (scanf(\"%d%d%*d \", &w, &h), w) {\n for (int i = 0; i < h; ++i) {\n fgets(c[i], 20, stdin);\n }\n int goal = 0;\n int start = 0;\n for (int i = 0; i < h; ++i) {\n for (int j = 0; j < w; ++j) {\n char current = c[i][j];\n bool isUpper = isupper(current);\n bool isLower = islower(current);\n if (current == '#') {\n wall[i << 4 | j] = true;\n } else {\n wall[i << 4 | j] = false;\n if (isUpper) {\n goal |= (i << 4 | j) << (current - 'A') * 8;\n } else if (isLower) {\n start |= (i << 4 | j) << (current - 'a') * 8;\n }\n }\n }\n }\n int tm = solve(start, goal);\n printf(\"%d\\n\", tm - tm0);\n tm0 = tm + 1;\n if (tm0 > 55000) {\n memset(vis, 0, sizeof vis);\n tm0 = 0;\n }\n }\n}", "accuracy": 1, "time_ms": 4440, "memory_kb": 25344, "score_of_the_acc": -0.7985, "final_rank": 3 }, { "submission_id": "aoj_1281_8821554", "code_snippet": "#include <cctype>\n#include <cstdio>\n#include <cstring>\n#include <queue>\nusing namespace std;\nunsigned short vis[1 << 24];\nchar c[16][20];\nbool wall[256];\nint tm0;\ninline bool check(int u, int v) {\n for (int i = 0; i < 24; i += 8) {\n int p1 = v >> i & 255;\n if (p1) {\n int j = i == 16 ? 0 : i + 8;\n if (wall[p1]) {\n return false;\n }\n int p2 = (v >> (j == 24 ? 0 : j)) & 255;\n if (p1 == p2) {\n return false;\n }\n if (p1 == (u >> (j == 24 ? 0 : j) & 255) && p2 == (u >> i & 255)) {\n return false;\n }\n }\n }\n return true;\n}\nint solve(int start, int goal) {\n const int dif[5] = {0, -1, 1, -16, 16};\n queue<int> q;\n q.push(start);\n q.push(-1);\n int tm = tm0 + 1;\n vis[start] = tm;\n while (1) {\n int u = q.front();\n q.pop();\n if (u < 0) {\n q.push(-1);\n ++tm;\n } else {\n for (int i1 = u & 255 ? 4 : 0; i1 >= 0; --i1)\n for (int i2 = u >> 8 & 255 ? 4 : 0; i2 >= 0; --i2)\n for (int i3 = u >> 16 ? 4 : 0; i3 >= 0; --i3) {\n int v = u + dif[i1] + (dif[i2] << 8) + (dif[i3] << 16);\n if (vis[v] > tm0) {\n continue;\n }\n if (check(u, v)) {\n if (v == goal) {\n return tm;\n }\n vis[v] = tm;\n q.push(v);\n }\n }\n }\n }\n}\nint main() {\n int w, h;\n while (scanf(\"%d%d%*d \", &w, &h), w) {\n for (int i = 0; i < h; ++i) {\n fgets(c[i], 20, stdin);\n }\n int goal = 0;\n int start = 0;\n memset(wall, false, sizeof(wall));\n for (int i = 0; i < h; ++i) {\n for (int j = 0; j < w; ++j) {\n if (c[i][j] == '#') {\n wall[i << 4 | j] = true;\n } else {\n if (isupper(c[i][j])) {\n goal |= (i << 4 | j) << ((c[i][j] - 'A') << 3);\n } else if (islower(c[i][j])) {\n start |= (i << 4 | j) << ((c[i][j] - 'a') << 3);\n }\n }\n }\n }\n int tm = solve(start, goal);\n printf(\"%d\\n\", tm - tm0);\n tm0 = tm + 1;\n if (tm0 > 55000) {\n memset(vis, 0, sizeof vis);\n tm0 = 0;\n }\n }\n}", "accuracy": 1, "time_ms": 4480, "memory_kb": 25328, "score_of_the_acc": -0.8047, "final_rank": 5 }, { "submission_id": "aoj_1281_8821547", "code_snippet": "#include <cctype>\n#include <cstdio>\n#include <cstring>\n#include <queue>\n\nunsigned short vis[1 << 24];\nchar c[16][20];\nbool wall[256];\nint tm0;\n\ninline bool check(int u, int v) {\n for (int i = 0; i < 24; i += 8) {\n int p1 = v >> i & 255;\n if (p1) {\n int j = i == 16 ? 0 : i + 8;\n if (wall[p1]) {\n return false;\n }\n int p2 = (v >> j % 24) & 255;\n if (p1 == p2) {\n return false;\n }\n if (p1 == (u >> j % 24 & 255) && p2 == (u >> i % 24 & 255)) {\n return false;\n }\n }\n }\n return true;\n}\n\nint solve(int start, int goal) {\n const int dif[5] = {0, -1, 1, -16, 16};\n std::queue<int> q;\n q.push(start);\n q.push(-1);\n int tm = tm0 + 1;\n vis[start] = tm;\n while (1) {\n int u = q.front();\n q.pop();\n if (u < 0) {\n q.push(-1);\n ++tm;\n } else {\n for (int i1 = u & 255 ? 4 : 0; i1 >= 0; --i1)\n for (int i2 = u >> 8 & 255 ? 4 : 0; i2 >= 0; --i2)\n for (int i3 = u >> 16 ? 4 : 0; i3 >= 0; --i3) {\n int v = u + dif[i1] + (dif[i2] << 8) + (dif[i3] << 16);\n if (check(u, v) && vis[v] <= tm0) {\n if (v == goal) {\n return tm;\n }\n vis[v] = tm;\n q.push(v);\n }\n }\n }\n }\n}\n\nint main() {\n int w, h;\n while (scanf(\"%d%d%*d \", &w, &h), w) {\n for (int i = 0; i < h; ++i) {\n fgets(c[i], 20, stdin);\n }\n int goal = 0;\n int start = 0;\n for (int i = 0; i < h; ++i) {\n for (int j = 0; j < w; ++j) {\n int ij = i << 4 | j;\n if (c[i][j] == '#') {\n wall[ij] = true;\n } else {\n wall[ij] = false;\n if (isupper(c[i][j])) {\n goal |= ij << (c[i][j] - 'A') * 8;\n } else if (islower(c[i][j])) {\n start |= ij << (c[i][j] - 'a') * 8;\n }\n }\n }\n }\n int tm = solve(start, goal);\n printf(\"%d\\n\", tm - tm0);\n tm0 = tm + 1;\n if (tm0 > 55000) {\n memset(vis, 0, sizeof vis);\n tm0 = 0;\n }\n }\n}", "accuracy": 1, "time_ms": 4700, "memory_kb": 25476, "score_of_the_acc": -0.841, "final_rank": 9 }, { "submission_id": "aoj_1281_8508460", "code_snippet": "// UVa1601 The Morning after Halloween\n#include <bits/stdc++.h>\nusing namespace std;\n#define _for(i, a, b) for (int i = (a); i < (int)(b); ++i)\ntypedef long long LL;\nusing VI = vector<int>;\nconst int MAXV = 16 * 16 + 4;\nconst VI DX{-1, 1, 0, 0, 0}, DY{0, 0, -1, 1, 0};\nVI X(MAXV), Y(MAXV), Src(3), Dest(3);\nvector<VI> ID(16, VI(16)), G(MAXV);\n// 3个人的位置编码\ninline int enc(int a, int b, int c) { return (a << 16) + (b << 8) + c; }\n// (a,b)->(na,nb)冲突吗?\ninline bool conflict(int a, int b, int na, int nb) {\n assert(a != b);\n return (na == nb || (na == b && nb == a)); // 相等或者只是交换了位置\n}\nint bfs() { // 三个人同时走\n queue<int> Q;\n unordered_map<int, int> D;\n int st = enc(Src[0], Src[1], Src[2]); // 从起点开始bfs\n Q.push(st), D[st] = 0;\n while (!Q.empty()) { // 从x中解码出3个人的位置\n int x = Q.front(), a = x >> 16, b = (x >> 8) & 255, c = x & 255, d = D[x];\n Q.pop();\n if (a == Dest[0] && b == Dest[1] && c == Dest[2]) return d; // 到达目的地了\n for (int na : G[a]) // a的下一个位置\n for (int nb : G[b]) { // b的下一个位置\n if (conflict(a, b, na, nb)) continue; // a,b冲突\n for (int nc : G[c]) {\n int ne = enc(na, nb, nc); // 3个人的新位置编码\n if (!conflict(a, c, na, nc) && !conflict(b, c, nb, nc) &&\n !D.count(ne)) // a,c和b,c不冲突,且没有访问过\n D[ne] = d + 1, Q.push(ne); // 记录路径,入队\n }\n }\n }\n return -1;\n}\nint main() {\n ios::sync_with_stdio(false), cin.tie(0);\n for (int W, H, N, V; cin >> W >> H >> N && W;) {\n cin.ignore(), V = 0;\n vector<string> MAP(H);\n _for(r, 0, H) {\n getline(cin, MAP[r]);\n _for(c, 0, W) {\n char ch = MAP[r][c];\n if (ch == '#') continue;\n ID[r][c] = V, X[V] = r, Y[V] = c; // 每个点一个编号\n if (isupper(ch)) Dest[ch - 'A'] = V; // 目的地的编号\n if (islower(ch)) Src[ch - 'a'] = V; // 起点的编号\n ++V;\n }\n }\n _for(u, 0, V) {\n G[u].clear();\n int x = X[u], y = Y[u];\n _for(d, 0, 5) { // 考虑(x,y)的5个方向可达的点\n int nx = x + DX[d], ny = y + DY[d];\n if (0 <= nx && nx < H && 0 <= ny && ny < W && MAP[nx][ny] != '#')\n G[u].push_back(ID[nx][ny]); // 建图\n }\n }\n if (N <= 2) Src[2] = Dest[2] = V, G[V].clear(), G[V].push_back(V), ++V;\n if (N <= 1) Src[1] = Dest[1] = V, G[V].clear(), G[V].push_back(V), ++V;\n printf(\"%d\\n\", bfs());\n }\n return 0;\n}\n// 19354384 1601 The Morning after Halloween Accepted C++11 1.010 2017-05-12\n// 00:02:59", "accuracy": 1, "time_ms": 5180, "memory_kb": 59444, "score_of_the_acc": -1.2642, "final_rank": 20 }, { "submission_id": "aoj_1281_8415148", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\nstruct ghost {\n\tint start, goal;\n};\n\nint solve(int N, int K, const vector<vector<int> >& G, const vector<ghost>& H) {\n\t// step #1. calculate distances\n\tvector<vector<int> > d(N, vector<int>(N, -1));\n\tfor (int i = 0; i < N; i++) {\n\t\tvector<int> st = { i };\n\t\td[i][i] = 0;\n\t\twhile (!st.empty()) {\n\t\t\tint u = st.back();\n\t\t\tst.pop_back();\n\t\t\tfor (int j : G[u]) {\n\t\t\t\tif (d[i][j] == -1) {\n\t\t\t\t\td[i][j] = d[i][u] + 1;\n\t\t\t\t\tst.push_back(j);\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n\tvector<vector<int> > dsub(K);\n\tfor (int i = 0; i < K; i++) {\n\t\tdsub[i] = d[H[i].goal];\n\t}\n\t\n\t// step #2. preparation\n\tvector<int> power(K + 1);\n\tpower[0] = 1;\n\tfor (int i = 1; i <= K; i++) {\n\t\tpower[i] = power[i - 1] * N;\n\t}\n\tint start = 0;\n\tint goal = 0;\n\tfor (int i = 0; i < K; i++) {\n\t\tstart += H[i].start * power[i];\n\t\tgoal += H[i].goal * power[i];\n\t}\n\n\t// step #3. bfs\n\tvector<int> dist(power[K], -1);\n\tqueue<int> que;\n\tdist[start] = 0;\n\tque.push(start);\n\twhile (!que.empty()) {\n\t\tint cid = que.front();\n\t\tque.pop();\n\t\tvector<int> cs(K);\n\t\tfor (int i = 0; i < K; i++) {\n\t\t\tcs[i] = (cid / power[i]) % N;\n\t\t}\n\t\tvector<bool> flag(K, false);\n\t\tfor (int i = 0; i < K; i++) {\n\t\t\tfor (int j = i + 1; j < K; j++) {\n\t\t\t\tif (d[cs[i]][cs[j]] <= 2) {\n\t\t\t\t\tflag[i] = true;\n\t\t\t\t\tflag[j] = true;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\tvector<int> ns(K);\n\t\tauto dfs = [&](auto& self, int depth, int nid) -> void {\n\t\t\tif (depth == K) {\n\t\t\t\tbool valid = true;\n\t\t\t\tfor (int i = 0; i < K; i++) {\n\t\t\t\t\tfor (int j = i + 1; j < K; j++) {\n\t\t\t\t\t\tif (ns[i] == ns[j]) {\n\t\t\t\t\t\t\tvalid = false;\n\t\t\t\t\t\t}\n\t\t\t\t\t\tif (ns[i] == cs[j] && ns[j] == cs[i]) {\n\t\t\t\t\t\t\tvalid = false;\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\tif (valid && dist[nid] == -1) {\n\t\t\t\t\tdist[nid] = dist[cid] + 1;\n\t\t\t\t\tque.push(nid);\n\t\t\t\t}\n\t\t\t\treturn;\n\t\t\t}\n\t\t\tfor (int i : G[cs[depth]]) {\n\t\t\t\tif (flag[depth] || dsub[depth][i] == max(dsub[depth][cs[depth]] - 1, 0)) {\n\t\t\t\t\tns[depth] = i;\n\t\t\t\t\tself(self, depth + 1, nid + i * power[depth]);\n\t\t\t\t}\n\t\t\t}\n\t\t\tif (flag[depth] || dsub[depth][cs[depth]] == 0) {\n\t\t\t\tns[depth] = cs[depth];\n\t\t\t\tself(self, depth + 1, nid + cs[depth] * power[depth]);\n\t\t\t}\n\t\t};\n\t\tdfs(dfs, 0, 0);\n\t}\n\n\treturn dist[goal];\n}\n\nint main() {\n\twhile (true) {\n\t\tint H, W, N;\n\t\tcin >> W >> H >> N; cin.ignore();\n\t\tif (H == 0 && W == 0 && N == 0) {\n\t\t\tbreak;\n\t\t}\n\t\tvector<string> S(H);\n\t\tfor (int i = 0; i < H; i++) {\n\t\t\tgetline(cin, S[i]);\n\t\t}\n\t\tint vcnt = 0;\n\t\tvector<ghost> I(N);\n\t\tvector<vector<int> > v(H, vector<int>(W, -1));\n\t\tfor (int i = 0; i < H; i++) {\n\t\t\tfor (int j = 0; j < W; j++) {\n\t\t\t\tif (S[i][j] != '#') {\n\t\t\t\t\tv[i][j] = vcnt;\n\t\t\t\t\tvcnt += 1;\n\t\t\t\t}\n\t\t\t\tif ('a' <= S[i][j] && S[i][j] <= 'z') {\n\t\t\t\t\tI[S[i][j] - 'a'].start = v[i][j];\n\t\t\t\t}\n\t\t\t\tif ('A' <= S[i][j] && S[i][j] <= 'Z') {\n\t\t\t\t\tI[S[i][j] - 'A'].goal = v[i][j];\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\tconst vector<int> dx = { 1, 0, -1, 0 };\n\t\tconst vector<int> dy = { 0, 1, 0, -1 };\n\t\tauto access = [&](int x, int y) -> int {\n\t\t\treturn (0 <= x && x < H && 0 <= y && y < W ? v[x][y] : -1);\n\t\t};\n\t\tvector<vector<int> > G(vcnt);\n\t\tfor (int i = 0; i < H; i++) {\n\t\t\tfor (int j = 0; j < W; j++) {\n\t\t\t\tif (v[i][j] != -1) {\n\t\t\t\t\tfor (int k = 0; k < 4; k++) {\n\t\t\t\t\t\tint id = access(i + dx[k], j + dy[k]);\n\t\t\t\t\t\tif (id != -1) {\n\t\t\t\t\t\t\tG[v[i][j]].push_back(id);\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\tint ans = solve(vcnt, N, G, I);\n\t\tcout << ans << endl;\n\t}\n\treturn 0;\n}", "accuracy": 1, "time_ms": 130, "memory_kb": 13840, "score_of_the_acc": 0, "final_rank": 1 }, { "submission_id": "aoj_1281_8215634", "code_snippet": "#include <cctype>\n#include <cstdio>\n#include <cstring>\n#include <queue>\nusing namespace std;\nunsigned short vis[1 << 24];\nchar c[16][20];\nbool wall[256];\nint tm0;\ninline bool check(int u, int v) {\n for (int i = 0; i < 24; i += 8) {\n int p1 = v >> i & 255;\n if (p1) {\n int j = i == 16 ? 0 : i + 8;\n if (wall[p1]) {\n return false;\n }\n int p2 = (v >> j) & 255;\n if (p1 == p2) {\n return false;\n }\n if (p1 == (u >> j & 255) && p2 == (u >> i & 255)) {\n return false;\n }\n }\n }\n return true;\n}\nint solve(int start, int goal) {\n const int dif[5] = {0, -1, 1, -16, 16};\n queue<int> q;\n q.push(start);\n q.push(-1);\n int tm = tm0 + 1;\n vis[start] = tm;\n while (1) {\n int u = q.front();\n q.pop();\n if (u < 0) {\n q.push(-1);\n ++tm;\n } else {\n for (int i1 = u & 255 ? 4 : 0; i1 >= 0; --i1)\n for (int i2 = u >> 8 & 255 ? 4 : 0; i2 >= 0; --i2)\n for (int i3 = u >> 16 ? 4 : 0; i3 >= 0; --i3) {\n int v = u + dif[i1] + (dif[i2] << 8) + (dif[i3] << 16);\n if (vis[v] > tm0) {\n continue;\n }\n if (check(u, v)) {\n if (v == goal) {\n return tm;\n }\n vis[v] = tm;\n q.push(v);\n }\n }\n }\n }\n}\nint main() {\n int w, h;\n while (scanf(\"%d%d%*d \", &w, &h), w) {\n for (int i = 0; i < h; ++i) {\n fgets(c[i], 20, stdin);\n }\n int goal = 0;\n int start = 0;\n for (int i = 0; i < h; ++i) {\n for (int j = 0; j < w; ++j) {\n if (c[i][j] == '#') {\n wall[i << 4 | j] = true;\n } else {\n wall[i << 4 | j] = false;\n if (isupper(c[i][j])) {\n goal |= (i << 4 | j) << ((c[i][j] - 'A') << 3);\n } else if (islower(c[i][j])) {\n start |= (i << 4 | j) << ((c[i][j] - 'a') << 3);\n }\n }\n }\n }\n int tm = solve(start, goal);\n printf(\"%d\\n\", tm - tm0);\n tm0 = tm + 1;\n if (tm0 > 55000) {\n memset(vis, 0, sizeof vis);\n tm0 = 0;\n }\n }\n}", "accuracy": 1, "time_ms": 4470, "memory_kb": 25500, "score_of_the_acc": -0.8049, "final_rank": 6 }, { "submission_id": "aoj_1281_8215628", "code_snippet": "#include <cctype>\n#include <cstdio>\n#include <cstring>\n#include <queue>\n\nusing namespace std;\n\nunsigned short vis[1 << 24];\nchar c[16][20];\nbool wall[256];\nint tm0;\n\ninline bool check(int u, int v) {\n for (int i = 0; i < 24; i += 8) {\n int p1 = v >> i & 255;\n if (p1) {\n if (wall[p1]) {\n return false;\n }\n int p2 = (v >> ((i + 8) % 24)) & 255;\n if ((p1 == p2) || (p1 == (u >> ((i + 8) % 24) & 255) && p2 == (u >> i % 24 & 255)))\n return false;\n }\n }\n return true;\n}\n\nint solve(int start, int goal) {\n const int dif[5] = {0, -1, 1, -16, 16};\n queue<int> q;\n q.push(start);\n q.push(-1);\n int tm = tm0 + 1;\n vis[start] = tm;\n while (!q.empty()) {\n int u = q.front();\n q.pop();\n if (u < 0) {\n if (q.empty())\n break;\n q.push(-1);\n ++tm;\n } else {\n for (int i1 = (u & 255) ? 4 : 0; i1 >= 0; --i1)\n for (int i2 = (u >> 8 & 255) ? 4 : 0; i2 >= 0; --i2)\n for (int i3 = u >> 16 ? 4 : 0; i3 >= 0; --i3) {\n int v = u + dif[i1] + (dif[i2] << 8) + (dif[i3] << 16);\n if (vis[v] > tm0 || !check(u, v)) {\n continue;\n }\n if (v == goal) {\n return tm;\n }\n vis[v] = tm;\n q.push(v);\n }\n }\n }\n return -1;\n}\n\nint main() {\n int w, h;\n while (scanf(\"%d%d%*d \", &w, &h), w) {\n for (int i = 0; i < h; ++i) {\n fgets(c[i], 20, stdin);\n }\n int goal = 0, start = 0;\n for (int i = 0; i < h; ++i) {\n for (int j = 0; j < w; ++j) {\n int ij = i << 4 | j;\n wall[ij] = c[i][j] == '#';\n if (!wall[ij]) {\n if (isupper(c[i][j])) {\n goal |= ij << (c[i][j] - 'A') * 8;\n } else if (islower(c[i][j])) {\n start |= ij << (c[i][j] - 'a') * 8;\n }\n }\n }\n }\n int tm = solve(start, goal);\n printf(\"%d\\n\", tm - tm0);\n tm0 = tm + 1;\n if (tm0 > 55000) {\n memset(vis, 0, sizeof vis);\n tm0 = 0;\n }\n }\n}", "accuracy": 1, "time_ms": 4950, "memory_kb": 25500, "score_of_the_acc": -0.8807, "final_rank": 11 } ]
aoj_1287_cpp
Problem C: Stopped Watches In the middle of Tyrrhenian Sea, there is a small volcanic island called Chronus. The island is now uninhabited but it used to be a civilized island. Some historical records imply that the island was annihilated by an eruption of a volcano about 800 years ago and that most of the people in the island were killed by pyroclastic flows caused by the volcanic activity. In 2003, a European team of archaeologists launched an excavation project in Chronus Island. Since then, the project has provided many significant historic insights. In particular the discovery made in the summer of 2008 astonished the world: the project team excavated several mechanical watches worn by the victims of the disaster. This indicates that people in Chronus Island had such a highly advanced manufacturing technology. Shortly after the excavation of the watches, archaeologists in the team tried to identify what time of the day the disaster happened, but it was not successful due to several difficulties. First, the extraordinary heat of pyroclastic flows severely damaged the watches and took away the letters and numbers printed on them. Second, every watch has a perfect round form and one cannot tell where the top of the watch is. Lastly, though every watch has three hands, they have a completely identical look and therefore one cannot tell which is the hour, the minute, or the second (It is a mystery how the people in Chronus Island were distinguishing the three hands. Some archaeologists guess that the hands might be painted with different colors, but this is only a hypothesis, as the paint was lost by the heat. ). This means that we cannot decide the time indicated by a watch uniquely; there can be a number of candidates. We have to consider different rotations of the watch. Furthermore, since there are several possible interpretations of hands, we have also to consider all the permutations of hands. You are an information archaeologist invited to the project team and are asked to induce the most plausible time interval within which the disaster happened, from the set of excavated watches. In what follows, we express a time modulo 12 hours. We write a time by the notation hh : mm : ss , where hh , mm , and ss stand for the hour ( hh = 00, 01, 02, . . . , 11), the minute ( mm = 00, 01, 02, . . . , 59), and the second ( ss = 00, 01, 02, . . . , 59), respectively. The time starts from 00:00:00 and counts up every second 00:00:00, 00:00:01, 00:00:02, . . ., but it reverts to 00:00:00 every 12 hours. The watches in Chronus Island obey the following conventions of modern analog watches. A watch has three hands, i.e. the hour hand, the minute hand, and the second hand, though they look identical as mentioned above. Every hand ticks 6 degrees clockwise in a discrete manner. That is, no hand stays between ticks, and each hand returns to the same position every 60 ticks. The second hand ticks every second. The minute hand ticks every 60 seconds. The hour ha ...(truncated)
[ { "submission_id": "aoj_1287_9666489", "code_snippet": "#line 2 \"cp-library/src/cp-template.hpp\"\n#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\nusing ld = long double;\nusing uint = unsigned int;\nusing ull = unsigned long long;\nusing i32 = int;\nusing u32 = unsigned int;\nusing i64 = long long;\nusing u64 = unsigned long long;\nusing i128 = __int128_t;\ntemplate < class T > bool chmin(T& a, T b) { if(a > b) { a = b; return true; } return false; }\ntemplate < class T > bool chmax(T& a, T b) { if(a < b) { a = b; return true; } return false; }\ntemplate < class T, class U > T ceil (T x, U y) { return (x > 0 ? (x + y - 1) / y : x / y); }\ntemplate < class T, class U > T floor(T x, U y) { return (x > 0 ? x / y : (x - y + 1) / y); }\nint popcnt(i32 x) { return __builtin_popcount(x); }\nint popcnt(u32 x) { return __builtin_popcount(x); }\nint popcnt(i64 x) { return __builtin_popcountll(x); }\nint popcnt(u64 x) { return __builtin_popcountll(x); }\n\n#line 2 \"cp-library/src/utility/rep_itr.hpp\"\ntemplate < class T > struct itr_rep {\n T i, d;\n constexpr itr_rep(const T i) noexcept : i(i), d(1) {}\n constexpr itr_rep(const T i, const T d) noexcept : i(i), d(d) {}\n void operator++() noexcept { i += d; }\n constexpr int operator*() const noexcept { return i; }\n constexpr bool operator!=(const itr_rep x) const noexcept { return d > 0 ? i < x.i : i > x.i; }\n};\n\ntemplate < class T > struct rep {\n const itr_rep< T > s, t;\n constexpr rep(const T t) noexcept : s(0), t(t) {}\n constexpr rep(const T s, const T t) noexcept : s(s), t(t) {}\n constexpr rep(const T s, const T t, const T d) noexcept : s(s, d), t(t, d) {}\n constexpr auto begin() const noexcept { return s; }\n constexpr auto end () const noexcept { return t; }\n};\n\ntemplate < class T > struct revrep {\n const itr_rep < T > s, t;\n constexpr revrep(const T t) noexcept : s(t - 1, -1), t(-1, -1) {}\n constexpr revrep(const T s, const T t) noexcept : s(t - 1, -1), t(s - 1, -1) {}\n constexpr revrep(const T s, const T t, const T d) noexcept : s(t - 1, -d), t(s - 1, -d) {}\n constexpr auto begin() const noexcept { return s; }\n constexpr auto end () const noexcept { return t; }\n};\n#line 3 \"cp-library/src/utility/io.hpp\"\n\n/* 128bit integer */\nistream& operator>>(istream& is, i128& x) {\n std::string s; is >> s;\n int pm = (s[0] == '-');\n x = 0;\n for(int i : rep(pm, int(s.size()))) x = x * 10 + (s[i] - '0');\n if(pm) x *= -1;\n return is;\n}\nostream& operator<<(ostream& os, const i128& x) {\n if(x == 0) return os << '0';\n i128 y = x;\n if(y < 0) { os << '-'; y *= -1; }\n std::vector<int> ny;\n while(y > 0) { ny.push_back(y % 10); y /= 10; }\n for(int i : revrep(ny.size())) os << ny[i];\n return os;\n}\n\ntemplate < class S, class T > istream& operator>>(istream& is, std::pair< S, T >& x) { is >> x.first >> x.second; return is; }\ntemplate < class S, class T > ostream& operator<<(ostream& os, const std::pair< S, T >& x) { os << x.first << \" \" << x.second; return os; }\n\nnamespace scanner {\n struct sca {\n template < class T > operator T() {\n T s; std::cin >> s; return s;\n }\n };\n struct vec {\n int n;\n vec(int n) : n(n) {}\n template < class T > operator std::vector< T >() {\n std::vector< T > v(n);\n for(T& x : v) std::cin >> x;\n return v;\n }\n };\n struct mat {\n int h, w;\n mat(int h, int w) : h(h), w(w) {}\n template < class T > operator std::vector< std::vector< T > >() {\n std::vector m(h, std::vector< T >(w));\n for(std::vector< T >& v : m) for(T& x : v) std::cin >> x;\n return m;\n }\n };\n struct speedup {\n speedup() {\n std::cin.tie(0);\n std::ios::sync_with_stdio(0);\n }\n } speedup_instance;\n}\nscanner::sca in() { return scanner::sca(); }\nscanner::vec in(int n) { return scanner::vec(n); }\nscanner::mat in(int h, int w) { return scanner::mat(h, w); }\n\nnamespace printer {\n void precision(int d) { std::cout << std::fixed << std::setprecision(d); }\n void flush() { std::cout.flush(); }\n}\n\ntemplate < class T >\nostream& operator<<(ostream& os, const std::vector< T > a) {\n int n = a.size();\n for(int i : rep(n)) { os << a[i]; if(i != n - 1) os << ' '; }\n return os;\n}\n\nint print() { std::cout << '\\n'; return 0; }\ntemplate < class head, class... tail > int print(head&& h, tail&&... t) {\n std::cout << h; if(sizeof...(tail)) std::cout << ' ';\n return print(std::forward<tail>(t)...);\n}\ntemplate < class T > int print_n(const std::vector< T > a) {\n int n = a.size();\n for(int i : rep(n)) std::cout << a[i] << \"\\n\";\n return 0;\n}\n\n\n#line 2 \"cp-library/src/utility/key_val.hpp\"\n\ntemplate < class K, class V >\nstruct key_val {\n K key; V val;\n key_val() {}\n key_val(K key, V val) : key(key), val(val) {}\n template < std::size_t Index >\n std::tuple_element_t< Index, key_val >& get() {\n if constexpr (Index == 0) return key;\n if constexpr (Index == 1) return val;\n }\n};\n\nnamespace std {\n\ntemplate < class K, class V > struct tuple_size < key_val< K, V > > : integral_constant< size_t, 2 > {};\ntemplate < class K, class V > struct tuple_element < 0, key_val< K, V > > { using type = K; };\ntemplate < class K, class V > struct tuple_element < 1, key_val< K, V > > { using type = V; };\n\n}\n#line 2 \"cp-library/src/utility/vec_op.hpp\"\ntemplate < class T > key_val< int, T > max_of(const vector< T >& a) {\n int i = std::max_element(a.begin(), a.end()) - a.begin();\n return {i, a[i]};\n}\ntemplate < class T > key_val< int, T > min_of(const vector< T >& a) {\n int i = std::min_element(a.begin(), a.end()) - a.begin();\n return {i, a[i]};\n}\ntemplate < class S, class T > S sum_of(const vector< T >& a) {\n S sum = 0;\n for(const T x : a) sum += x;\n return sum;\n}\ntemplate < class S, class T > vector< S > freq_of(const vector< T >& a, T L, T R) {\n vector< S > res(R - L, S(0));\n for(const T x : a) res[x - L] += 1;\n return res;\n}\ntemplate < class S, class T > struct prefix_sum {\n vector< S > s;\n prefix_sum(const vector< T >& a) : s(a) {\n s.insert(s.begin(), S(0));\n for(int i : rep(a.size())) s[i + 1] += s[i];\n }\n // [L, R)\n S sum(int L, int R) { return s[R] - s[L]; }\n};\n#line 3 \"cp-library/src/utility/heap.hpp\"\n\ntemplate < class T > using heap_min = std::priority_queue< T, std::vector< T >, std::greater< T > >;\ntemplate < class T > using heap_max = std::priority_queue< T, std::vector< T >, std::less< T > >;\n\n#line 27 \"cp-library/src/cp-template.hpp\"\n\n#line 1 \"cp-library/src/algorithm/bin_search.hpp\"\ntemplate < class T, class F >\nT bin_search(T ok, T ng, F f) {\n while(abs(ng - ok) > 1) {\n T mid = (ok + ng) / 2;\n (f(mid) ? ok : ng) = mid;\n }\n return ok;\n}\n\ntemplate < class T, class F >\nT bin_search_real(T ok, T ng, F f, int step = 80) {\n while(step--) {\n T mid = (ok + ng) / 2;\n (f(mid) ? ok : ng) = mid;\n }\n return ok;\n}\n#line 2 \"cp-library/src/algorithm/argsort.hpp\"\n\ntemplate < class T > std::vector< int > argsort(const std::vector< T > &a) {\n std::vector< int > ids((int)a.size());\n std::iota(ids.begin(), ids.end(), 0);\n std::sort(ids.begin(), ids.end(), [&](int i, int j) {\n return a[i] < a[j] || (a[i] == a[j] && i < j);\n });\n return ids;\n}\n#line 1 \"macro.hpp\"\nnamespace macro {\n\nusing size_type = int;\ntemplate < class container > void sort(container& a) { std::sort(std:: begin(a), std:: end(a)); }\ntemplate < class container > void rsort(container& a) { std::sort(std::rbegin(a), std::rend(a)); }\ntemplate < class container > void reverse(container& a) { std::reverse(std::begin(a), std::end(a)); }\ntemplate < class container > void unique(container& a) {\n std::sort(std::begin(a), std::end(a));\n a.erase(std::unique(std::begin(a), std::end(a)), std::end(a));\n}\ntemplate < class container > container sorted(const container& a) { container b = a; sort(b); return std::move(b); }\ntemplate < class container > container rsorted(const container& a) { container b = a; rsort(b); return std::move(b); }\ntemplate < class container, class compare > void sort(container& a, const compare& cmp) { std::sort(std::begin(a), std::end(a), cmp); }\ntemplate < class container, class compare > container sorted(const container& a, const compare& cmp) { container b = a; sort(b, cmp); return std::move(b); }\ntemplate < class container, class value > size_type lower_bound(const container& a, const value& x) { return std::lower_bound(std::begin(a), std::end(a), x) - std::begin(a); }\ntemplate < class container, class value > size_type upper_bound(const container& a, const value& x) { return std::upper_bound(std::begin(a), std::end(a), x) - std::begin(a); }\n\nconst std::vector<std::pair<size_type, size_type>> dir4 = { {+1, 0}, {-1, 0}, { 0, +1}, { 0, -1} };\nconst std::vector<std::pair<size_type, size_type>> dir8 = { {-1, -1}, {-1, 0}, {-1, +1}, { 0, -1}, { 0, +1}, {+1, -1}, {+1, 0}, {+1, +1} };\n\n#ifdef _DEBUG\n#define debug(x) std::cout << \"[\" << __LINE__ << \"] \" << #x << \": \" << x << std::endl\n#else\n#define debug(x)\n#endif\n\ntemplate < class container > void concat(container& a, const container& b) {\n a.insert(std::end(a), std::begin(b), std::end(b));\n}\nstd::vector<size_type> iota(const size_type n) {\n std::vector<size_type> I(n);\n std::iota(std::begin(I), std::end(I), 0);\n return I;\n}\ntemplate < class container > std::vector<size_type> sort_idx(const container& a) {\n const size_type n = a.size();\n std::vector<size_type> I = iota(n);\n std::sort(std::begin(I), std::end(I), [&](size_type i, size_type j) { return a[i] < a[j] or (a[i] == a[j] and i < j); });\n return I;\n}\ntemplate < class container, class compare > std::vector<size_type> sort_idx(const container& a, const compare& cmp) {\n const size_type n = a.size();\n std::vector<size_type> I = iota(n);\n std::sort(std::begin(I), std::end(I), [&](size_type i, size_type j) { return cmp(a[i], a[j]) or (a[i] == a[j] and i < j); });\n return std::move(I);\n}\n\nstruct grid {\n using size_type = int;\n size_type H, W;\n grid(const size_type H, const size_type W) : H(H), W(W) {}\n bool contains(const size_type i, const size_type j) {\n return 0 <= i and i < H and 0 <= j and j < W;\n }\n};\n\nusing f64 = long double;\n\ntemplate < class T > vector< T >& operator++(vector< T >& a) { for(T& x : a) x++; return a; }\ntemplate < class T > vector< T >& operator--(vector< T >& a) { for(T& x : a) x--; return a; }\ntemplate < class T > vector< T > operator++(vector< T >& a, signed) { vector< T > res = a; for(T& x : a) x++; return res; }\ntemplate < class T > vector< T > operator--(vector< T >& a, signed) { vector< T > res = a; for(T& x : a) x--; return res; }\n\n} // namespace macro\n\nusing namespace macro;\n#line 3 \"A.cpp\"\n\nconst int D = 12 * 60 * 60;\nint id[61][61][61] = {};\n\nvoid init() {\n for(int a : rep(60)) for(int b : rep(60)) for(int c : rep(60)) id[a][b][c] = -1;\n int h = 0, m = 0, s = 0;\n for(int i : rep(D)) {\n id[h][m][s] = i;\n s++;\n if(s == 60) {\n m++;\n s = 0;\n if(m % 12 == 0) {\n h++;\n if(m == 60) m = 0;\n }\n }\n }\n}\n\nvoid solve(int n) {\n vector<vector<int>> a = in(n, 3);\n for(int i : rep(n)) sort(a[i]);\n\n const int INF = 1e9;\n pair<int,int> ans = {INF, INF};\n\n for(int si : rep(n)) {\n do {\n for(int add : rep(60)) {\n int sh = (a[si][0] + add) % 60;\n int sm = (a[si][1] + add) % 60;\n int ss = (a[si][2] + add) % 60;\n const int s = id[sh][sm][ss];\n if(s == -1) continue;\n int u = s;\n\n bool ok = [&] {\n for(int ti : rep(n)) if(ti != si) {\n int t_min = INF;\n do {\n for(int add_in : rep(60)) {\n int th = (a[ti][0] + add_in) % 60;\n int tm = (a[ti][1] + add_in) % 60;\n int ts = (a[ti][2] + add_in) % 60;\n const int t = id[th][tm][ts];\n if(t == -1) continue;\n if(s <= t) chmin(t_min, t);\n }\n } while(next_permutation(a[ti].begin(), a[ti].end()));\n if(t_min == INF) return false;\n chmax(u, t_min);\n }\n return true;\n }();\n\n if(ok) chmin(ans, make_pair(u - s, s));\n }\n } while(next_permutation(a[si].begin(), a[si].end()));\n }\n\n const int fr = ans.second;\n const int to = ans.second + ans.first;\n printf(\"%02d:%02d:%02d %02d:%02d:%02d\\n\", (fr/60)/60, (fr/60)%60, fr%60, (to/60)/60, (to/60)%60, to%60);\n}\n\nint main() {\n init();\n while(true) {\n int n = in();\n if(n == 0) break;\n solve(n);\n }\n}", "accuracy": 1, "time_ms": 50, "memory_kb": 4412, "score_of_the_acc": -1.005, "final_rank": 19 }, { "submission_id": "aoj_1287_6558345", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\n#define rep(i, s, t) for (int i = (int)(s); i < (int)(t); ++i)\n#define revrep(i, t, s) for (int i = (int)(t)-1; i >= (int)(s); --i)\n#define all(x) begin(x), end(x)\ntemplate <typename T> bool chmax(T& a, const T& b) { return a < b ? (a = b, 1) : 0; }\ntemplate <typename T> bool chmin(T& a, const T& b) { return a > b ? (a = b, 1) : 0; }\n\ntemplate <typename T>\nostream& operator<<(ostream& os, const vector<T>& v) {\n for (int i = 0; i < (int) v.size(); ++i) os << v[i] << \" \";\n return os;\n}\n\nint main() {\n ios_base::sync_with_stdio(false);\n cin.tie(nullptr);\n cout << fixed << setprecision(15);\n\n while (true) {\n int n;\n cin >> n;\n if (n == 0) break;\n vector<vector<int>> times(n, vector<int>(3));\n rep(i,0,n) {\n rep(j,0,3) cin >> times[i][j];\n }\n\n auto diff = [&](auto& t1, auto& t2) {\n int x1 = t1[0]/12*3600 + t1[1]*60 + t1[2];\n int x2 = t2[0]/12*3600 + t2[1]*60 + t2[2];\n int dt = x2 - x1;\n if (dt < 0) dt += 60*60*60;\n return dt;\n };\n\n auto rotate = [&](auto t, int d) {\n rep(j,0,3) t[j] = (t[j] + d) % 60;\n if (t[0]%5 != t[1]/12) return vector<int>();\n return t;\n };\n\n auto print = [&](auto& t1, auto& t2) {\n printf(\"%02d:%02d:%02d %02d:%02d:%02d\\n\", t1[0]/5, t1[1], t1[2], t2[0]/5, t2[1], t2[2]);\n };\n\n int dt = 1e9;\n vector<int> ans1(3), ans2(3);\n // fix the earliest time\n rep(i,0,n) {\n // fix the order of hands\n vector<int> ord = {0, 1, 2};\n do {\n vector<int> timei0(3);\n rep(j,0,3) timei0[j] = times[i][ord[j]];\n rep(d,0,60) {\n auto timei1 = rotate(timei0, d);\n if (timei1.empty()) continue;\n\n vector<int> timei2 = timei1;\n\n // get the time right after the timei1\n rep(k,0,n) {\n if (i == k) continue;\n vector<int> after = {10000, 10000, 10000};\n\n vector<int> ord2 = {0, 1, 2};\n do {\n vector<int> timek0(3);\n rep(j,0,3) timek0[j] = times[k][ord2[j]];\n rep(d2,0,60) {\n vector<int> timek1 = rotate(timek0, d2);\n if (!timek1.empty() && timei1 <= timek1) {\n chmin(after, timek1);\n }\n }\n } while (next_permutation(all(ord2)));\n chmax(timei2, after);\n }\n int ndt = diff(timei1, timei2);\n if (ndt < dt || (ndt == dt && timei1 < ans1)) {\n dt = ndt;\n ans1 = timei1;\n ans2 = timei2;\n }\n }\n } while (next_permutation(all(ord)));\n }\n print(ans1, ans2);\n }\n}", "accuracy": 1, "time_ms": 200, "memory_kb": 3212, "score_of_the_acc": -0.1787, "final_rank": 6 }, { "submission_id": "aoj_1287_6385728", "code_snippet": "#include <cstdio>\n#include <vector>\n#include <set>\n#include <functional>\nusing namespace std;\n\nint getID(int x, int y, int z) {\n int py = y / 12;\n if (py != x % 5) return -1;\n return z + y * 60 + (x / 5) * 60 * 60; \n}\n\nint main() {\n vector<vector<int>> pos;\n vector<int> vis(3, 0);\n vector<int> a(3);\n \n function<void(int)> dfs = [&](int p) {\n if (p == 3) {\n pos.push_back(a);\n } else {\n for (int i = 0; i < 3; i++) {\n if (!vis[i]) {\n vis[i] = 1;\n a[p] = i;\n dfs(p + 1);\n vis[i] = 0; \n }\n }\n }\n };\n dfs(0);\n \n int n;\nwhile (scanf(\"%d\", &n) == 1) {\n if (n == 0) break;\n vector<set<int>> have(n);\n for (int it = 0; it < n; it++) {\n vector<int> b(3);\n for (int i = 0; i < 3; i++) scanf(\"%d\", &b[i]);\n for (auto cur: pos) {\n int x = b[cur[0]];\n int y = b[cur[1]];\n int z= b[cur[2]];\n for (int j = 0; j < 60; j++) {\n int x0 = (x + j) % 60;\n int y0 = (y + j) % 60;\n int z0 = (z + j) % 60;\n // int id = z0 + y0 * 60 + x0 * 60 * 12;\n int id = getID(x0, y0, z0);\n if (id != -1) have[it].insert(id);\n }\n }\n }\n int ans = -1;\n int u = -1, v = -1;\n int cycle = 12 * 60 * 60;\n for (int s = 0; s < cycle; s++) {\n int low = s;\n for (int i = 0; i < n; i++) {\n auto find = have[i].lower_bound(s);\n if (find != have[i].end()) {\n int take = *find;\n low = max(low, take); \n } else {\n int take = cycle + *(have[i].begin());\n low = max(low, take);\n }\n }\n if (ans == -1 || low - s + 1 < ans) {\n ans = low - s + 1;\n u = s, v = low; \n }\n }\n auto out = [&](int u) {\n u = u % cycle;\n vector<int> c;\n for (int i = 0; i < 3; i++) {\n c.push_back(u % 60);\n u /= 60; \n }\n for (int i = (int)c.size() - 1; i >= 0; i--) {\n printf(\"%02d\", c[i]);\n if (i) printf(\":\"); \n }\n };\n /*\n for (int i = 0; i < n; i++) {\n printf(\"i = %d\\n\", i);\n for (int u: have[i]) { \n out(u); \n printf(\"\\n\");\n }\n printf(\"\\n\");\n }*/\n // printf(\"u = %d, v = %d: %d\\n\", u, v, ans);\n out(u);\n printf(\" \");\n out(v);\n printf(\"\\n\");\n}\n return 0; \n}", "accuracy": 1, "time_ms": 30, "memory_kb": 2992, "score_of_the_acc": -0.0025, "final_rank": 1 }, { "submission_id": "aoj_1287_5992996", "code_snippet": "// #pragma comment(linker, \"/stack:200000000\")\n\n#include <bits/stdc++.h>\n\n#include <limits>\n#include <type_traits>\n\nnamespace suisen {\n// ! utility\ntemplate <typename ...Types>\nusing constraints_t = std::enable_if_t<std::conjunction_v<Types...>, std::nullptr_t>;\ntemplate <bool cond_v, typename Then, typename OrElse>\nconstexpr decltype(auto) constexpr_if(Then&& then, OrElse&& or_else) {\n if constexpr (cond_v) {\n return std::forward<Then>(then);\n } else {\n return std::forward<OrElse>(or_else);\n }\n}\n\n// ! function\ntemplate <typename ReturnType, typename Callable, typename ...Args>\nusing is_same_as_invoke_result = std::is_same<std::invoke_result_t<Callable, Args...>, ReturnType>;\ntemplate <typename F, typename T>\nusing is_uni_op = is_same_as_invoke_result<T, F, T>;\ntemplate <typename F, typename T>\nusing is_bin_op = is_same_as_invoke_result<T, F, T, T>;\n\ntemplate <typename Comparator, typename T>\nusing is_comparator = std::is_same<std::invoke_result_t<Comparator, T, T>, bool>;\n\n// ! integral\ntemplate <typename T, typename = constraints_t<std::is_integral<T>>>\nconstexpr int bit_num = std::numeric_limits<std::make_unsigned_t<T>>::digits;\ntemplate <typename T, unsigned int n>\nstruct is_nbit { static constexpr bool value = bit_num<T> == n; };\ntemplate <typename T, unsigned int n>\nstatic constexpr bool is_nbit_v = is_nbit<T, n>::value;\n\n// ?\ntemplate <typename T>\nstruct safely_multipliable {};\ntemplate <>\nstruct safely_multipliable<int> { using type = long long; };\ntemplate <>\nstruct safely_multipliable<long long> { using type = __int128_t; };\ntemplate <>\nstruct safely_multipliable<float> { using type = float; };\ntemplate <>\nstruct safely_multipliable<double> { using type = double; };\ntemplate <>\nstruct safely_multipliable<long double> { using type = long double; };\ntemplate <typename T>\nusing safely_multipliable_t = typename safely_multipliable<T>::type;\n\n} // namespace suisen\n\n// ! type aliases\nusing i128 = __int128_t;\nusing u128 = __uint128_t;\nusing ll = long long;\nusing uint = unsigned int;\nusing ull = unsigned long long;\n\ntemplate <typename T> using vec = std::vector<T>;\ntemplate <typename T> using vec2 = vec<vec <T>>;\ntemplate <typename T> using vec3 = vec<vec2<T>>;\ntemplate <typename T> using vec4 = vec<vec3<T>>;\n\ntemplate <typename T>\nusing pq_greater = std::priority_queue<T, std::vector<T>, std::greater<T>>;\ntemplate <typename T, typename U>\nusing umap = std::unordered_map<T, U>;\n\n// ! macros (capital: internal macro)\n#define OVERLOAD2(_1,_2,name,...) name\n#define OVERLOAD3(_1,_2,_3,name,...) name\n#define OVERLOAD4(_1,_2,_3,_4,name,...) name\n\n#define REP4(i,l,r,s) for(std::remove_reference_t<std::remove_const_t<decltype(r)>>i=(l);i<(r);i+=(s))\n#define REP3(i,l,r) REP4(i,l,r,1)\n#define REP2(i,n) REP3(i,0,n)\n#define REPINF3(i,l,s) for(std::remove_reference_t<std::remove_const_t<decltype(l)>>i=(l);;i+=(s))\n#define REPINF2(i,l) REPINF3(i,l,1)\n#define REPINF1(i) REPINF2(i,0)\n#define RREP4(i,l,r,s) for(std::remove_reference_t<std::remove_const_t<decltype(r)>>i=(l)+fld((r)-(l)-1,s)*(s);i>=(l);i-=(s))\n#define RREP3(i,l,r) RREP4(i,l,r,1)\n#define RREP2(i,n) RREP3(i,0,n)\n\n#define rep(...) OVERLOAD4(__VA_ARGS__, REP4 , REP3 , REP2 )(__VA_ARGS__)\n#define rrep(...) OVERLOAD4(__VA_ARGS__, RREP4 , RREP3 , RREP2 )(__VA_ARGS__)\n#define repinf(...) OVERLOAD3(__VA_ARGS__, REPINF3, REPINF2, REPINF1)(__VA_ARGS__)\n\n#define CAT_I(a, b) a##b\n#define CAT(a, b) CAT_I(a, b)\n#define UNIQVAR(tag) CAT(tag, __LINE__)\n#define loop(n) for (std::remove_reference_t<std::remove_const_t<decltype(n)>> UNIQVAR(loop_variable) = n; UNIQVAR(loop_variable) --> 0;)\n\n#define all(iterable) (iterable).begin(), (iterable).end()\n#define input(type, ...) type __VA_ARGS__; read(__VA_ARGS__)\n\n// ! I/O utilities\n\n// pair\ntemplate <typename T, typename U>\nstd::ostream& operator<<(std::ostream& out, const std::pair<T, U> &a) {\n return out << a.first << ' ' << a.second;\n}\n// tuple\ntemplate <unsigned int N = 0, typename ...Args>\nstd::ostream& operator<<(std::ostream& out, const std::tuple<Args...> &a) {\n if constexpr (N >= std::tuple_size_v<std::tuple<Args...>>) {\n return out;\n } else {\n out << std::get<N>(a);\n if constexpr (N + 1 < std::tuple_size_v<std::tuple<Args...>>) {\n out << ' ';\n }\n return operator<<<N + 1>(out, a);\n }\n}\n// vector\ntemplate <typename T>\nstd::ostream& operator<<(std::ostream& out, const std::vector<T> &a) {\n for (auto it = a.begin(); it != a.end();) {\n out << *it;\n if (++it != a.end()) out << ' ';\n }\n return out;\n}\ninline void print() { std::cout << '\\n'; }\ntemplate <typename Head, typename... Tail>\ninline void print(const Head &head, const Tail &...tails) {\n std::cout << head;\n if (sizeof...(tails)) std::cout << ' ';\n print(tails...);\n}\ntemplate <typename Iterable>\nauto print_all(const Iterable& v, std::string sep = \" \", std::string end = \"\\n\") -> decltype(std::cout << *v.begin(), void()) {\n for (auto it = v.begin(); it != v.end();) {\n std::cout << *it;\n if (++it != v.end()) std::cout << sep;\n }\n std::cout << end;\n}\n\n// pair\ntemplate <typename T, typename U>\nstd::istream& operator>>(std::istream& in, std::pair<T, U> &a) {\n return in >> a.first >> a.second;\n}\n// tuple\ntemplate <unsigned int N = 0, typename ...Args>\nstd::istream& operator>>(std::istream& in, std::tuple<Args...> &a) {\n if constexpr (N >= std::tuple_size_v<std::tuple<Args...>>) {\n return in;\n } else {\n return operator>><N + 1>(in >> std::get<N>(a), a);\n }\n}\n// vector\ntemplate <typename T>\nstd::istream& operator>>(std::istream& in, std::vector<T> &a) {\n for (auto it = a.begin(); it != a.end(); ++it) in >> *it;\n return in;\n}\ntemplate <typename ...Args>\nvoid read(Args &...args) {\n ( std::cin >> ... >> args );\n}\n\n// ! integral utilities\n\n// Returns pow(-1, n)\ntemplate <typename T>\nconstexpr inline int pow_m1(T n) {\n return -(n & 1) | 1;\n}\n// Returns pow(-1, n)\ntemplate <>\nconstexpr inline int pow_m1<bool>(bool n) {\n return -int(n) | 1;\n}\n\n// Returns floor(x / y)\ntemplate <typename T>\nconstexpr inline T fld(const T x, const T y) {\n return (x ^ y) >= 0 ? x / y : (x - (y + pow_m1(y >= 0))) / y;\n}\ntemplate <typename T>\nconstexpr inline T cld(const T x, const T y) {\n return (x ^ y) <= 0 ? x / y : (x + (y + pow_m1(y >= 0))) / y;\n}\n\ntemplate <typename T, suisen::constraints_t<suisen::is_nbit<T, 32>> = nullptr>\nconstexpr inline int popcount(const T x) { return __builtin_popcount(x); }\ntemplate <typename T, suisen::constraints_t<suisen::is_nbit<T, 64>> = nullptr>\nconstexpr inline int popcount(const T x) { return __builtin_popcountll(x); }\ntemplate <typename T, suisen::constraints_t<suisen::is_nbit<T, 32>> = nullptr>\nconstexpr inline int count_lz(const T x) { return x ? __builtin_clz(x) : suisen::bit_num<T>; }\ntemplate <typename T, suisen::constraints_t<suisen::is_nbit<T, 64>> = nullptr>\nconstexpr inline int count_lz(const T x) { return x ? __builtin_clzll(x) : suisen::bit_num<T>; }\ntemplate <typename T, suisen::constraints_t<suisen::is_nbit<T, 32>> = nullptr>\nconstexpr inline int count_tz(const T x) { return x ? __builtin_ctz(x) : suisen::bit_num<T>; }\ntemplate <typename T, suisen::constraints_t<suisen::is_nbit<T, 64>> = nullptr>\nconstexpr inline int count_tz(const T x) { return x ? __builtin_ctzll(x) : suisen::bit_num<T>; }\ntemplate <typename T>\nconstexpr inline int floor_log2(const T x) { return suisen::bit_num<T> - 1 - count_lz(x); }\ntemplate <typename T>\nconstexpr inline int ceil_log2(const T x) { return floor_log2(x) + ((x & -x) != x); }\ntemplate <typename T>\nconstexpr inline int kth_bit(const T x, const unsigned int k) { return (x >> k) & 1; }\ntemplate <typename T>\nconstexpr inline int parity(const T x) { return popcount(x) & 1; }\n\nstruct all_subset {\n struct all_subset_iter {\n const int s; int t;\n constexpr all_subset_iter(int s) : s(s), t(s + 1) {}\n constexpr auto operator*() const { return t; }\n constexpr auto operator++() {}\n constexpr auto operator!=(std::nullptr_t) { return t ? (--t &= s, true) : false; }\n };\n int s;\n constexpr all_subset(int s) : s(s) {}\n constexpr auto begin() { return all_subset_iter(s); }\n constexpr auto end() { return nullptr; }\n};\n\n// ! container\n\ntemplate <typename T, typename Comparator, suisen::constraints_t<suisen::is_comparator<Comparator, T>> = nullptr>\nauto priqueue_comp(const Comparator comparator) {\n return std::priority_queue<T, std::vector<T>, Comparator>(comparator);\n}\n\ntemplate <typename Iterable>\nauto isize(const Iterable &iterable) -> decltype(int(iterable.size())) {\n return iterable.size();\n}\n\ntemplate <typename T, typename Gen, suisen::constraints_t<suisen::is_same_as_invoke_result<T, Gen, int>> = nullptr>\nauto generate_vector(int n, Gen generator) {\n std::vector<T> v(n);\n for (int i = 0; i < n; ++i) v[i] = generator(i);\n return v;\n}\n\ntemplate <typename T>\nvoid sort_unique_erase(std::vector<T> &a) {\n std::sort(a.begin(), a.end());\n a.erase(std::unique(a.begin(), a.end()), a.end());\n}\n\n// ! other utilities\n\n// x <- min(x, y). returns true iff `x` has chenged.\ntemplate <typename T>\ninline bool chmin(T &x, const T &y) {\n if (y >= x) return false;\n x = y;\n return true;\n}\n// x <- max(x, y). returns true iff `x` has chenged.\ntemplate <typename T>\ninline bool chmax(T &x, const T &y) {\n if (y <= x) return false;\n x = y;\n return true;\n}\n\nnamespace suisen {}\nusing namespace suisen;\nusing namespace std;\n\nstruct io_setup {\n io_setup(int precision = 20) {\n std::ios::sync_with_stdio(false);\n std::cin.tie(nullptr);\n std::cout << std::fixed << std::setprecision(precision);\n }\n} io_setup_{};\n\n// ! code from here\n\nconstexpr int H = 12;\nconstexpr int M = 60;\nconstexpr int S = 60;\nconstexpr int D = H * M * S;\n\nconstexpr int T = 60;\n\nstruct Time {\n int t;\n\n Time& operator++() {\n ++t;\n return *this;\n }\n operator int() { return t; }\n\n int hour() const { return t / (M * S); }\n int minute() const { return t / S % M; }\n int second() const { return t % S; }\n\n int hour_hand() const { return t / S / 12; }\n int minute_hand() const { return t / S % T; }\n int second_hand() const { return t % T; }\n\n friend ostream& operator<<(ostream& out, const Time& t) {\n return out << t.hour() << ':' << t.minute() << ':' << t.second();\n }\n};\n\nint main() {\n while (true) {\n input(int, n);\n if (n == 0) break;\n vector<array<int, 3>> watches(n);\n for (auto &[x, y, z] : watches) read(x, y, z);\n vector<int> t(D, 0);\n for (auto& hands : watches) {\n vector<int> cum_min(D, 2 * D);\n sort(all(hands));\n do {\n loop(T) {\n int m = (hands[1] - 12 * hands[0]) % 60;\n if (m < 0) m += 60;\n if (m < 12) {\n int x = hands[0] * 12 * S + m * S + hands[2];\n chmin(cum_min[x], x);\n }\n ++hands[0] %= T, ++hands[1] %= T, ++hands[2] %= T;\n }\n } while (next_permutation(all(hands)));\n rrep(s, D) {\n if (s + 1 < D) chmin(cum_min[s], cum_min[s + 1]);\n chmax(t[s], cum_min[s]);\n }\n }\n int min_interval = D - 1, aminl = 0;\n rep(s, D) {\n if (chmin(min_interval, t[s] - s)) {\n aminl = s;\n }\n }\n Time fr { aminl }, to { aminl + min_interval };\n printf(\"%02d:%02d:%02d %02d:%02d:%02d\\n\", fr.hour(), fr.minute(), fr.second(), to.hour(), to.minute(), to.second());\n }\n return 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3352, "score_of_the_acc": -0.2535, "final_rank": 8 }, { "submission_id": "aoj_1287_5992989", "code_snippet": "// #pragma comment(linker, \"/stack:200000000\")\n\n#include <bits/stdc++.h>\n\n#include <limits>\n#include <type_traits>\n\nnamespace suisen {\n// ! utility\ntemplate <typename ...Types>\nusing constraints_t = std::enable_if_t<std::conjunction_v<Types...>, std::nullptr_t>;\ntemplate <bool cond_v, typename Then, typename OrElse>\nconstexpr decltype(auto) constexpr_if(Then&& then, OrElse&& or_else) {\n if constexpr (cond_v) {\n return std::forward<Then>(then);\n } else {\n return std::forward<OrElse>(or_else);\n }\n}\n\n// ! function\ntemplate <typename ReturnType, typename Callable, typename ...Args>\nusing is_same_as_invoke_result = std::is_same<std::invoke_result_t<Callable, Args...>, ReturnType>;\ntemplate <typename F, typename T>\nusing is_uni_op = is_same_as_invoke_result<T, F, T>;\ntemplate <typename F, typename T>\nusing is_bin_op = is_same_as_invoke_result<T, F, T, T>;\n\ntemplate <typename Comparator, typename T>\nusing is_comparator = std::is_same<std::invoke_result_t<Comparator, T, T>, bool>;\n\n// ! integral\ntemplate <typename T, typename = constraints_t<std::is_integral<T>>>\nconstexpr int bit_num = std::numeric_limits<std::make_unsigned_t<T>>::digits;\ntemplate <typename T, unsigned int n>\nstruct is_nbit { static constexpr bool value = bit_num<T> == n; };\ntemplate <typename T, unsigned int n>\nstatic constexpr bool is_nbit_v = is_nbit<T, n>::value;\n\n// ?\ntemplate <typename T>\nstruct safely_multipliable {};\ntemplate <>\nstruct safely_multipliable<int> { using type = long long; };\ntemplate <>\nstruct safely_multipliable<long long> { using type = __int128_t; };\ntemplate <>\nstruct safely_multipliable<float> { using type = float; };\ntemplate <>\nstruct safely_multipliable<double> { using type = double; };\ntemplate <>\nstruct safely_multipliable<long double> { using type = long double; };\ntemplate <typename T>\nusing safely_multipliable_t = typename safely_multipliable<T>::type;\n\n} // namespace suisen\n\n// ! type aliases\nusing i128 = __int128_t;\nusing u128 = __uint128_t;\nusing ll = long long;\nusing uint = unsigned int;\nusing ull = unsigned long long;\n\ntemplate <typename T> using vec = std::vector<T>;\ntemplate <typename T> using vec2 = vec<vec <T>>;\ntemplate <typename T> using vec3 = vec<vec2<T>>;\ntemplate <typename T> using vec4 = vec<vec3<T>>;\n\ntemplate <typename T>\nusing pq_greater = std::priority_queue<T, std::vector<T>, std::greater<T>>;\ntemplate <typename T, typename U>\nusing umap = std::unordered_map<T, U>;\n\n// ! macros (capital: internal macro)\n#define OVERLOAD2(_1,_2,name,...) name\n#define OVERLOAD3(_1,_2,_3,name,...) name\n#define OVERLOAD4(_1,_2,_3,_4,name,...) name\n\n#define REP4(i,l,r,s) for(std::remove_reference_t<std::remove_const_t<decltype(r)>>i=(l);i<(r);i+=(s))\n#define REP3(i,l,r) REP4(i,l,r,1)\n#define REP2(i,n) REP3(i,0,n)\n#define REPINF3(i,l,s) for(std::remove_reference_t<std::remove_const_t<decltype(l)>>i=(l);;i+=(s))\n#define REPINF2(i,l) REPINF3(i,l,1)\n#define REPINF1(i) REPINF2(i,0)\n#define RREP4(i,l,r,s) for(std::remove_reference_t<std::remove_const_t<decltype(r)>>i=(l)+fld((r)-(l)-1,s)*(s);i>=(l);i-=(s))\n#define RREP3(i,l,r) RREP4(i,l,r,1)\n#define RREP2(i,n) RREP3(i,0,n)\n\n#define rep(...) OVERLOAD4(__VA_ARGS__, REP4 , REP3 , REP2 )(__VA_ARGS__)\n#define rrep(...) OVERLOAD4(__VA_ARGS__, RREP4 , RREP3 , RREP2 )(__VA_ARGS__)\n#define repinf(...) OVERLOAD3(__VA_ARGS__, REPINF3, REPINF2, REPINF1)(__VA_ARGS__)\n\n#define CAT_I(a, b) a##b\n#define CAT(a, b) CAT_I(a, b)\n#define UNIQVAR(tag) CAT(tag, __LINE__)\n#define loop(n) for (std::remove_reference_t<std::remove_const_t<decltype(n)>> UNIQVAR(loop_variable) = n; UNIQVAR(loop_variable) --> 0;)\n\n#define all(iterable) (iterable).begin(), (iterable).end()\n#define input(type, ...) type __VA_ARGS__; read(__VA_ARGS__)\n\n// ! I/O utilities\n\n// pair\ntemplate <typename T, typename U>\nstd::ostream& operator<<(std::ostream& out, const std::pair<T, U> &a) {\n return out << a.first << ' ' << a.second;\n}\n// tuple\ntemplate <unsigned int N = 0, typename ...Args>\nstd::ostream& operator<<(std::ostream& out, const std::tuple<Args...> &a) {\n if constexpr (N >= std::tuple_size_v<std::tuple<Args...>>) {\n return out;\n } else {\n out << std::get<N>(a);\n if constexpr (N + 1 < std::tuple_size_v<std::tuple<Args...>>) {\n out << ' ';\n }\n return operator<<<N + 1>(out, a);\n }\n}\n// vector\ntemplate <typename T>\nstd::ostream& operator<<(std::ostream& out, const std::vector<T> &a) {\n for (auto it = a.begin(); it != a.end();) {\n out << *it;\n if (++it != a.end()) out << ' ';\n }\n return out;\n}\ninline void print() { std::cout << '\\n'; }\ntemplate <typename Head, typename... Tail>\ninline void print(const Head &head, const Tail &...tails) {\n std::cout << head;\n if (sizeof...(tails)) std::cout << ' ';\n print(tails...);\n}\ntemplate <typename Iterable>\nauto print_all(const Iterable& v, std::string sep = \" \", std::string end = \"\\n\") -> decltype(std::cout << *v.begin(), void()) {\n for (auto it = v.begin(); it != v.end();) {\n std::cout << *it;\n if (++it != v.end()) std::cout << sep;\n }\n std::cout << end;\n}\n\n// pair\ntemplate <typename T, typename U>\nstd::istream& operator>>(std::istream& in, std::pair<T, U> &a) {\n return in >> a.first >> a.second;\n}\n// tuple\ntemplate <unsigned int N = 0, typename ...Args>\nstd::istream& operator>>(std::istream& in, std::tuple<Args...> &a) {\n if constexpr (N >= std::tuple_size_v<std::tuple<Args...>>) {\n return in;\n } else {\n return operator>><N + 1>(in >> std::get<N>(a), a);\n }\n}\n// vector\ntemplate <typename T>\nstd::istream& operator>>(std::istream& in, std::vector<T> &a) {\n for (auto it = a.begin(); it != a.end(); ++it) in >> *it;\n return in;\n}\ntemplate <typename ...Args>\nvoid read(Args &...args) {\n ( std::cin >> ... >> args );\n}\n\n// ! integral utilities\n\n// Returns pow(-1, n)\ntemplate <typename T>\nconstexpr inline int pow_m1(T n) {\n return -(n & 1) | 1;\n}\n// Returns pow(-1, n)\ntemplate <>\nconstexpr inline int pow_m1<bool>(bool n) {\n return -int(n) | 1;\n}\n\n// Returns floor(x / y)\ntemplate <typename T>\nconstexpr inline T fld(const T x, const T y) {\n return (x ^ y) >= 0 ? x / y : (x - (y + pow_m1(y >= 0))) / y;\n}\ntemplate <typename T>\nconstexpr inline T cld(const T x, const T y) {\n return (x ^ y) <= 0 ? x / y : (x + (y + pow_m1(y >= 0))) / y;\n}\n\ntemplate <typename T, suisen::constraints_t<suisen::is_nbit<T, 32>> = nullptr>\nconstexpr inline int popcount(const T x) { return __builtin_popcount(x); }\ntemplate <typename T, suisen::constraints_t<suisen::is_nbit<T, 64>> = nullptr>\nconstexpr inline int popcount(const T x) { return __builtin_popcountll(x); }\ntemplate <typename T, suisen::constraints_t<suisen::is_nbit<T, 32>> = nullptr>\nconstexpr inline int count_lz(const T x) { return x ? __builtin_clz(x) : suisen::bit_num<T>; }\ntemplate <typename T, suisen::constraints_t<suisen::is_nbit<T, 64>> = nullptr>\nconstexpr inline int count_lz(const T x) { return x ? __builtin_clzll(x) : suisen::bit_num<T>; }\ntemplate <typename T, suisen::constraints_t<suisen::is_nbit<T, 32>> = nullptr>\nconstexpr inline int count_tz(const T x) { return x ? __builtin_ctz(x) : suisen::bit_num<T>; }\ntemplate <typename T, suisen::constraints_t<suisen::is_nbit<T, 64>> = nullptr>\nconstexpr inline int count_tz(const T x) { return x ? __builtin_ctzll(x) : suisen::bit_num<T>; }\ntemplate <typename T>\nconstexpr inline int floor_log2(const T x) { return suisen::bit_num<T> - 1 - count_lz(x); }\ntemplate <typename T>\nconstexpr inline int ceil_log2(const T x) { return floor_log2(x) + ((x & -x) != x); }\ntemplate <typename T>\nconstexpr inline int kth_bit(const T x, const unsigned int k) { return (x >> k) & 1; }\ntemplate <typename T>\nconstexpr inline int parity(const T x) { return popcount(x) & 1; }\n\nstruct all_subset {\n struct all_subset_iter {\n const int s; int t;\n constexpr all_subset_iter(int s) : s(s), t(s + 1) {}\n constexpr auto operator*() const { return t; }\n constexpr auto operator++() {}\n constexpr auto operator!=(std::nullptr_t) { return t ? (--t &= s, true) : false; }\n };\n int s;\n constexpr all_subset(int s) : s(s) {}\n constexpr auto begin() { return all_subset_iter(s); }\n constexpr auto end() { return nullptr; }\n};\n\n// ! container\n\ntemplate <typename T, typename Comparator, suisen::constraints_t<suisen::is_comparator<Comparator, T>> = nullptr>\nauto priqueue_comp(const Comparator comparator) {\n return std::priority_queue<T, std::vector<T>, Comparator>(comparator);\n}\n\ntemplate <typename Iterable>\nauto isize(const Iterable &iterable) -> decltype(int(iterable.size())) {\n return iterable.size();\n}\n\ntemplate <typename T, typename Gen, suisen::constraints_t<suisen::is_same_as_invoke_result<T, Gen, int>> = nullptr>\nauto generate_vector(int n, Gen generator) {\n std::vector<T> v(n);\n for (int i = 0; i < n; ++i) v[i] = generator(i);\n return v;\n}\n\ntemplate <typename T>\nvoid sort_unique_erase(std::vector<T> &a) {\n std::sort(a.begin(), a.end());\n a.erase(std::unique(a.begin(), a.end()), a.end());\n}\n\n// ! other utilities\n\n// x <- min(x, y). returns true iff `x` has chenged.\ntemplate <typename T>\ninline bool chmin(T &x, const T &y) {\n if (y >= x) return false;\n x = y;\n return true;\n}\n// x <- max(x, y). returns true iff `x` has chenged.\ntemplate <typename T>\ninline bool chmax(T &x, const T &y) {\n if (y <= x) return false;\n x = y;\n return true;\n}\n\nnamespace suisen {}\nusing namespace suisen;\nusing namespace std;\n\nstruct io_setup {\n io_setup(int precision = 20) {\n std::ios::sync_with_stdio(false);\n std::cin.tie(nullptr);\n std::cout << std::fixed << std::setprecision(precision);\n }\n} io_setup_{};\n\n// ! code from here\n\nconstexpr int H = 12;\nconstexpr int M = 60;\nconstexpr int S = 60;\nconstexpr int D = H * M * S;\n\nconstexpr int T = 60;\n\nstruct Time {\n int t;\n\n Time& operator++() {\n ++t;\n return *this;\n }\n operator int() { return t; }\n\n int hour() const { return t / (M * S); }\n int minute() const { return t / S % M; }\n int second() const { return t % S; }\n\n int hour_hand() const { return t / S / 12; }\n int minute_hand() const { return t / S % T; }\n int second_hand() const { return t % T; }\n\n friend ostream& operator<<(ostream& out, const Time& t) {\n return out << t.hour() << ':' << t.minute() << ':' << t.second();\n }\n};\n\nint main() {\n while (true) {\n input(int, n);\n if (n == 0) break;\n vector<array<int, 3>> watches(n);\n for (auto &[x, y, z] : watches) read(x, y, z);\n int min_interval = D - 1;\n int aminl = 0;\n for (int s = 0; s < D; ++s) {\n int t = s;\n for (auto& hands : watches) {\n int mt = 2 * D;\n sort(all(hands));\n do {\n loop(T) {\n int m = (hands[1] - 12 * hands[0]) % 60;\n if (m < 0) m += 60;\n if (m < 12) {\n int x = hands[0] * 12 * S + m * S + hands[2];\n if (x >= s) chmin(mt, x);\n }\n ++hands[0] %= T, ++hands[1] %= T, ++hands[2] %= T;\n }\n } while (next_permutation(all(hands)));\n chmax(t, mt);\n if (t >= s + min_interval) break;\n }\n if (chmin(min_interval, t - s)) {\n aminl = s;\n }\n }\n Time fr{ aminl }, to{ aminl + min_interval };\n printf(\"%02d:%02d:%02d %02d:%02d:%02d\\n\", fr.hour(), fr.minute(), fr.second(), to.hour(), to.minute(), to.second());\n }\n return 0;\n}", "accuracy": 1, "time_ms": 4320, "memory_kb": 3536, "score_of_the_acc": -0.9225, "final_rank": 17 }, { "submission_id": "aoj_1287_5511123", "code_snippet": "#include <stdio.h>\n#include <bits/stdc++.h>\nusing namespace std;\n#define rep(i,n) for (int i = 0; i < (n); ++i)\n#define Inf 1000000001\n\nvoid Put(int t){\n\tint s = t%60;\n\tt /= 60;\n\tint m = t%60;\n\tt /= 60;\n\tint h = t;\n\t\n\tprintf(\"%02d:%02d:%02d\",h,m,s);\n}\n\nint main(){\n\t\n\tvector<array<int,3>> A(12*60*60);\n\t\n\trep(i,12*60*60){\n\t\tint ts = i%60;\n\t\tint tm = (i/60)%60;\n\t\tint th = (i/(60*12))%60;\n\t\tA[i] = {th,tm,ts};\n\t}\n\t\n\t\n\t\n\twhile(true){\n\t\tint n;\n\t\tcin>>n;\n\t\t\n\t\tif(n==0)break;\n\t\t\n\t\tvector<array<int,3>> s(n);\n\t\trep(i,n){\n\t\t\trep(j,3){\n\t\t\t\tcin>>s[i][j];\n\t\t\t}\n\t\t}\n\t\t\n\t\tvector<vector<int>> times(n);\n\t\t\n\t\trep(i,n){\n\t\t\tvector<int> tt = {0,1,2};\n\t\t\tdo{\n\t\t\t\trep(j,60){\n\t\t\t\t\tarray<int,3> B = {s[i][tt[0]],s[i][tt[1]],s[i][tt[2]]};\n\t\t\t\t\tint d = distance(A.begin(),lower_bound(A.begin(),A.end(),B));\n\t\t\t\t\tif(d!=A.size() && A[d]==B){\n\t\t\t\t\t\ttimes[i].push_back(d);\n\t\t\t\t\t}\n\t\t\t\t\trep(k,3){\n\t\t\t\t\t\ts[i][k] ++;\n\t\t\t\t\t\ts[i][k] %= 60;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t\twhile(next_permutation(tt.begin(),tt.end()));\n\t\t\t\n\t\t\tsort(times[i].begin(),times[i].end());\n\t\t\ttimes[i].erase(unique(times[i].begin(),times[i].end()),times[i].end());\n\t\t}\n\t\t\n\t\tvector<pair<int,int>> ans;\n\t\trep(i,12*60*60){\n\t\t\tint M = i;\n\t\t\trep(j,n){\n\t\t\t\tint d = distance(times[j].begin(),lower_bound(times[j].begin(),times[j].end(),i));\n\t\t\t\tif(d==times[j].size()){\n\t\t\t\t\tM = Inf;\n\t\t\t\t}\n\t\t\t\telse{\n\t\t\t\t\tM = max(M,times[j][d]);\n\t\t\t\t}\t\t\t\t\n\t\t\t}\n\t\t\tif(M==Inf)continue;\n\t\t\tans.emplace_back(i,M);\n\t\t}\n\t\t\n\t\tint ind = 0;\n\t\t\n\t\trep(i,ans.size()){\n\t\t\tif(ans[i].second-ans[i].first < ans[ind].second-ans[ind].first)ind = i;\n\t\t}\n\t\t\n\t\tPut(ans[ind].first);\n\t\tcout<<' ';\n\t\tPut(ans[ind].second);\n\t\tcout<<endl;\n\t\t\n\t}\n\t\n\treturn 0;\n}", "accuracy": 1, "time_ms": 60, "memory_kb": 4304, "score_of_the_acc": -0.9302, "final_rank": 18 }, { "submission_id": "aoj_1287_5396321", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nconst int MAX = 60 * 60 * 12, INF = 1e9;\n\nint time(int s, int t, int u, int x) { // 時針、分針、秒針がs,t,uにあって基準がxのときの時間\n int res = 0;\n s = (s + 60 - x) % 60;\n t = (t + 60 - x) % 60;\n u = (u + 60 - x) % 60;\n if (s % 5 != t / 12) return INF;\n res += s / 5 * 3600;\n res += t % 60 * 60;\n res += u % 60 * 1;\n return res;\n}\n\nint cycle[8] = {10, 6, 0, 10, 6, 0, 10, 10};\n\nstring convert(int t) {\n string res = \"\";\n for (int i = 0; i < 8; i++) {\n if (i % 3 == 2)\n res += ':';\n else {\n res += char('0' + t % cycle[i]);\n t /= cycle[i];\n }\n }\n reverse(res.begin(), res.end());\n return res;\n}\n\nvoid solve(int n) {\n vector<vector<int>> cand(n);\n for (int i = 0; i < n; i++) {\n vector<int> v(3);\n for (int j = 0; j < 3; j++) cin >> v[j];\n for (int j = 0; j < 60; j++) {\n vector<int> ord(3);\n iota(ord.begin(), ord.end(), 0);\n do {\n cand[i].emplace_back(time(v[ord[0]], v[ord[1]], v[ord[2]], j));\n } while (next_permutation(ord.begin(), ord.end()));\n }\n cand[i].emplace_back(INF);\n sort(cand[i].begin(), cand[i].end());\n }\n\n int Min = MAX, L = 0, R = MAX;\n for (int l = 0; l < MAX; l++) {\n int r = l;\n for (int j = 0; j < n; j++) {\n int nxt = *lower_bound(cand[j].begin(), cand[j].end(), l);\n r = max(r, nxt);\n }\n if (r - l < Min) {\n Min = r - l;\n L = l, R = r;\n }\n }\n\n cout << convert(L) << ' ' << convert(R) << '\\n';\n}\n\nint main() {\n cin.tie(0);\n ios::sync_with_stdio(false);\n int n;\n while (cin >> n, n) solve(n);\n return 0;\n}", "accuracy": 1, "time_ms": 40, "memory_kb": 3412, "score_of_the_acc": -0.2995, "final_rank": 10 }, { "submission_id": "aoj_1287_5396319", "code_snippet": "#define LOCAL\n#include <bits/stdc++.h>\nusing namespace std;\n#pragma region Macros\ntypedef long long ll;\ntypedef __int128_t i128;\ntypedef unsigned int uint;\ntypedef unsigned long long ull;\n#define ALL(x) (x).begin(), (x).end()\n\ntemplate <typename T> istream& operator>>(istream& is, vector<T>& v) {\n for (T& x : v) is >> x;\n return is;\n}\ntemplate <typename T> ostream& operator<<(ostream& os, const vector<T>& v) {\n for (int i = 0; i < (int)v.size(); i++) {\n os << v[i] << (i + 1 == (int)v.size() ? \"\" : \" \");\n }\n return os;\n}\ntemplate <typename T, typename U> ostream& operator<<(ostream& os, const pair<T, U>& p) {\n os << '(' << p.first << ',' << p.second << ')';\n return os;\n}\ntemplate <typename T, typename U, typename V> ostream& operator<<(ostream& os, const tuple<T, U, V>& t) {\n os << '(' << get<0>(t) << ',' << get<1>(t) << ',' << get<2>(t) << ')';\n return os;\n}\ntemplate <typename T, typename U, typename V, typename W> ostream& operator<<(ostream& os, const tuple<T, U, V, W>& t) {\n os << '(' << get<0>(t) << ',' << get<1>(t) << ',' << get<2>(t) << ',' << get<3>(t) << ')';\n return os;\n}\ntemplate <typename T, typename U> ostream& operator<<(ostream& os, const map<T, U>& m) {\n os << '{';\n for (auto itr = m.begin(); itr != m.end();) {\n os << '(' << itr->first << ',' << itr->second << ')';\n if (++itr != m.end()) os << ',';\n }\n os << '}';\n return os;\n}\ntemplate <typename T, typename U> ostream& operator<<(ostream& os, const unordered_map<T, U>& m) {\n os << '{';\n for (auto itr = m.begin(); itr != m.end();) {\n os << '(' << itr->first << ',' << itr->second << ')';\n if (++itr != m.end()) os << ',';\n }\n os << '}';\n return os;\n}\ntemplate <typename T> ostream& operator<<(ostream& os, const set<T>& s) {\n os << '{';\n for (auto itr = s.begin(); itr != s.end();) {\n os << *itr;\n if (++itr != s.end()) os << ',';\n }\n os << '}';\n return os;\n}\ntemplate <typename T> ostream& operator<<(ostream& os, const multiset<T>& s) {\n os << '{';\n for (auto itr = s.begin(); itr != s.end();) {\n os << *itr;\n if (++itr != s.end()) os << ',';\n }\n os << '}';\n return os;\n}\ntemplate <typename T> ostream& operator<<(ostream& os, const unordered_set<T>& s) {\n os << '{';\n for (auto itr = s.begin(); itr != s.end();) {\n os << *itr;\n if (++itr != s.end()) os << ',';\n }\n os << '}';\n return os;\n}\ntemplate <typename T> ostream& operator<<(ostream& os, const deque<T>& v) {\n for (int i = 0; i < (int)v.size(); i++) {\n os << v[i] << (i + 1 == (int)v.size() ? \"\" : \" \");\n }\n return os;\n}\n\nvoid debug_out() { cerr << '\\n'; }\ntemplate <class Head, class... Tail> void debug_out(Head&& head, Tail&&... tail) {\n cerr << head;\n if (sizeof...(Tail) > 0) cerr << \", \";\n debug_out(move(tail)...);\n}\n#ifdef LOCAL\n#define debug(...) \\\n cerr << \" \"; \\\n cerr << #__VA_ARGS__ << \" :[\" << __LINE__ << \":\" << __FUNCTION__ << \"]\" << '\\n'; \\\n cerr << \" \"; \\\n debug_out(__VA_ARGS__)\n#else\n#define debug(...) 42\n#endif\n\ntemplate <typename T> T gcd(T x, T y) { return y != 0 ? gcd(y, x % y) : x; }\ntemplate <typename T> T lcm(T x, T y) { return x / gcd(x, y) * y; }\n\ntemplate <class T1, class T2> inline bool chmin(T1& a, T2 b) {\n if (a > b) {\n a = b;\n return true;\n }\n return false;\n}\ntemplate <class T1, class T2> inline bool chmax(T1& a, T2 b) {\n if (a < b) {\n a = b;\n return true;\n }\n return false;\n}\n#pragma endregion\n\nconst int INF = 1e9;\nconst long long IINF = 1e18;\nconst int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};\nconst char dir[4] = {'D', 'R', 'U', 'L'};\nconst long long MOD = 1000000007;\n// const long long MOD = 998244353;\n\nconst int MAX = 60 * 60 * 12;\n\nint time(int s, int t, int u, int x) { // 時針、分針、秒針がs,t,uにあって基準がxのときの時間\n int res = 0;\n s = (s + 60 - x) % 60;\n t = (t + 60 - x) % 60;\n u = (u + 60 - x) % 60;\n if (s % 5 != t / 12) return INF;\n res += s / 5 * 3600;\n res += t % 60 * 60;\n res += u % 60 * 1;\n return res;\n}\n\nint cycle[8] = {10, 6, 0, 10, 6, 0, 10, 10};\n\nstring convert(int t) {\n string res = \"\";\n for (int i = 0; i < 8; i++) {\n if (i % 3 == 2)\n res += ':';\n else {\n res += char('0' + t % cycle[i]);\n t /= cycle[i];\n }\n }\n reverse(res.begin(), res.end());\n return res;\n}\n\nvoid solve(int n) {\n vector<vector<int>> cand(n);\n for (int i = 0; i < n; i++) {\n vector<int> v(3);\n for (int j = 0; j < 3; j++) cin >> v[j];\n for (int j = 0; j < 60; j++) {\n vector<int> ord(3);\n iota(ord.begin(), ord.end(), 0);\n do {\n cand[i].emplace_back(time(v[ord[0]], v[ord[1]], v[ord[2]], j));\n } while (next_permutation(ord.begin(), ord.end()));\n }\n cand[i].emplace_back(INF);\n sort(cand[i].begin(), cand[i].end());\n }\n\n int Min = MAX, L = 0, R = MAX;\n for (int l = 0; l < MAX; l++) {\n int r = l;\n for (int j = 0; j < n; j++) {\n int nxt = *lower_bound(cand[j].begin(), cand[j].end(), l);\n r = max(r, nxt);\n }\n if (r - l < Min) {\n Min = r - l;\n L = l, R = r;\n }\n }\n\n cout << convert(L) << ' ' << convert(R) << '\\n';\n}\n\nint main() {\n cin.tie(0);\n ios::sync_with_stdio(false);\n int n;\n while (cin >> n, n) solve(n);\n return 0;\n}", "accuracy": 1, "time_ms": 40, "memory_kb": 3180, "score_of_the_acc": -0.1361, "final_rank": 5 }, { "submission_id": "aoj_1287_5371943", "code_snippet": "#include <bits/stdc++.h>\n\n#define For(i, a, b) for (int i = (int)(a); i < (int)(b); ++i)\n#define rFor(i, a, b) for (int i = (int)(a)-1; i >= (int)(b); --i)\n#define rep(i, n) For((i), 0, (n))\n#define rrep(i, n) rFor((i), (n), 0)\n#define fi first\n#define se second\n\nusing namespace std;\n\ntypedef long long lint;\ntypedef unsigned long long ulint;\ntypedef pair<int, int> pii;\ntypedef pair<lint, lint> pll;\n\ntemplate <class T>\nbool chmax(T &a, const T &b) {\n if (a < b) {\n a = b;\n return true;\n }\n return false;\n}\ntemplate <class T>\nbool chmin(T &a, const T &b) {\n if (a > b) {\n a = b;\n return true;\n }\n return false;\n}\n\ntemplate <class T>\nT div_floor(T a, T b) {\n if (b < 0) a *= -1, b *= -1;\n return a >= 0 ? a / b : (a + 1) / b - 1;\n}\ntemplate <class T>\nT div_ceil(T a, T b) {\n if (b < 0) a *= -1, b *= -1;\n return a > 0 ? (a - 1) / b + 1 : a / b;\n}\n\ntemplate <typename T>\nstruct coord_comp {\n vector<T> v;\n bool sorted = false;\n\n coord_comp() {}\n\n int size() { return v.size(); }\n\n void add(T x) { v.push_back(x); }\n\n void build() {\n sort(v.begin(), v.end());\n v.erase(unique(v.begin(), v.end()), v.end());\n sorted = true;\n }\n\n int get_idx(T x) {\n assert(sorted);\n return lower_bound(v.begin(), v.end(), x) - v.begin();\n }\n\n T &operator[](int i) { return v[i]; }\n};\n\nconstexpr lint mod = 1000000007;\nconstexpr lint INF = mod * mod;\nconstexpr int MAX = 200010;\n\nbool check(int h, int m) {\n int x = h % 5;\n return 12 * x <= m && m < 12 * (x + 1);\n}\n\nint main() {\n int n;\n while (scanf(\"%d\", &n) && n) {\n int a[n][3];\n rep(i, n) rep(j, 3) scanf(\"%d\", &a[i][j]);\n pair<int, pii> ans = {mod, {mod, mod}};\n rep(t, n) {\n int idx[3] = {0, 1, 2};\n do {\n rep(offset, 60) {\n int h = (a[t][idx[0]] + offset) % 60,\n m = (a[t][idx[1]] + offset) % 60,\n s = (a[t][idx[2]] + offset) % 60;\n if (!check(h, m)) continue;\n int start = h / 5 * 3600 + m * 60 + s;\n int end = 0;\n rep(i, n) if (i != t) {\n int idx2[3] = {0, 1, 2};\n int min_time = mod;\n do {\n rep(offset2, 60) {\n int h2 = (a[i][idx2[0]] + offset2) % 60,\n m2 = (a[i][idx2[1]] + offset2) % 60,\n s2 = (a[i][idx2[2]] + offset2) % 60;\n if (!check(h2, m2)) continue;\n int tmp = h2 / 5 * 3600 + m2 * 60 + s2;\n if (tmp >= start) chmin(min_time, tmp);\n }\n } while (next_permutation(idx2, idx2 + 3));\n chmax(end, min_time);\n }\n chmin(ans, {end - start, {start, end}});\n }\n } while (next_permutation(idx, idx + 3));\n }\n printf(\"%02d:%02d:%02d \", ans.se.fi / 3600, ans.se.fi % 3600 / 60,\n ans.se.fi % 60);\n printf(\"%02d:%02d:%02d\\n\", ans.se.se / 3600, ans.se.se % 3600 / 60,\n ans.se.se % 60);\n }\n}", "accuracy": 1, "time_ms": 30, "memory_kb": 3396, "score_of_the_acc": -0.287, "final_rank": 9 }, { "submission_id": "aoj_1287_5033721", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\nconst int N=10;\n\nint n,s[N],t[N],u[N],last[N];\n\nint hh(int x) { return x/12/60; } // every 12 min !!!\nint mm(int x) { return x/60%60; }\nint ss(int x) { return x%60; }\n\nstring nn(int x) { string s; return s+char('0'+x/10%10)+char('0'+x%10); }\n\nint match(int s1, int t1, int u1, int s2, int t2, int u2) {\n int ds=(s1-s2+60)%60;\n int dt=(t1-t2+60)%60;\n int du=(u1-u2+60)%60;\n return ds==dt && dt==du;\n}\n\nint solve() {\n cin>>n;\n if (n==0) return 0;\n\n for (int i=0; i<n; i++) cin>>s[i]>>t[i]>>u[i];\n\n int begin,end=-1;\n memset(last,-1,sizeof(last));\n for (int i=0; i<12*60*60; i++) {\n int a=hh(i), b=mm(i), c=ss(i);\n\n int j=i;\n for (int k=0; k<n; k++) {\n if (match(s[k],t[k],u[k],a,b,c) ||\n match(s[k],t[k],u[k],a,c,b) ||\n match(s[k],t[k],u[k],b,a,c) ||\n match(s[k],t[k],u[k],b,c,a) ||\n match(s[k],t[k],u[k],c,a,b) ||\n match(s[k],t[k],u[k],c,b,a)) {\n last[k]=i;\n }\n if (last[k]<j) j=last[k];\n }\n\n if (j>=0 && (end==-1 || end-begin>i-j)) {\n end=i; begin=j;\n }\n }\n\n cout<<nn(hh(begin)/5)<<':'<<nn(mm(begin))<<':'<<nn(ss(begin))<<' '\n <<nn(hh(end)/5)<<':'<<nn(mm(end))<<':'<<nn(ss(end))<<'\\n';\n return 1;\n}\n\nint main() {\n while (solve());\n return 0;\n}", "accuracy": 1, "time_ms": 80, "memory_kb": 3132, "score_of_the_acc": -0.1074, "final_rank": 3 }, { "submission_id": "aoj_1287_5001109", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\nusing ll = long long;\nusing PII = pair<ll, ll>;\n\n#define FOR(i, a, n) for (ll i = (ll)a; i < (ll)n; ++i)\n#define REP(i, n) FOR(i, 0, n)\n#define ALL(x) x.begin(), x.end()\ntemplate <typename T> void chmin(T &a, const T &b) { a = min(a, b); }\ntemplate <typename T> void chmax(T &a, const T &b) { a = max(a, b); }\n\nconst ll INF = 1LL << 60;\n\nstruct FastIO {\n FastIO() {\n cin.tie(0);\n ios::sync_with_stdio(0);\n }\n} fastiofastio;\n\nconst int DAY = 60 * 60 * 12;\n\nbool exist[DAY][10];\n\nstring convert(int x) {\n int h = x / 3600;\n int m = (x % 3600) / 60;\n int s = x % 60;\n string ret = \"\";\n if (h < 10) {\n ret.push_back('0');\n ret.push_back(h + '0');\n } else {\n ret += to_string(h);\n }\n ret += \":\";\n if (m < 10) {\n ret.push_back('0');\n ret.push_back(m + '0');\n } else {\n ret += to_string(m);\n }\n ret += \":\";\n if (s < 10) {\n ret.push_back('0');\n ret.push_back(s + '0');\n } else {\n ret += to_string(s);\n }\n return ret;\n}\n\nint main() {\n while (1) {\n int n;\n cin >> n;\n if (n == 0)\n break;\n memset(exist, 0, sizeof(exist));\n for (int i = 0; i < n; i++) {\n int s, t, u;\n cin >> s >> t >> u;\n for (int j = 0; j < 60; j++) {\n int a[3] = {j, (j - s + t + 60) % 60, (j - s + u + 60) % 60};\n int perm[3] = {0, 1, 2};\n do {\n if (a[perm[0]] % 5 == a[perm[1]] / 12) {\n int x = a[perm[2]];\n x += (a[perm[1]] % 12) * 60;\n x += a[perm[0]] * 12 * 60;\n exist[x][i] = 1;\n }\n } while (next_permutation(perm, perm + 3));\n }\n }\n int l = 0, r = 0;\n int num[10] = {};\n int cnt = 0;\n int B = -1, E = -1;\n while (l < DAY) {\n while (r < DAY && cnt < n) {\n for (int i = 0; i < n; i++) {\n if (exist[r][i]) {\n if (num[i] == 0)\n cnt++;\n num[i]++;\n }\n }\n r++;\n }\n if (cnt < n)\n break;\n //[l, r)\n if (B == -1 || E - B > r - l) {\n B = l;\n E = r;\n }\n for (int i = 0; i < n; i++) {\n if (exist[l][i]) {\n num[i]--;\n if (num[i] == 0)\n cnt--;\n }\n }\n l++;\n }\n cout << convert(B) << \" \" << convert(E - 1) << endl;\n }\n}", "accuracy": 1, "time_ms": 20, "memory_kb": 3912, "score_of_the_acc": -0.6491, "final_rank": 14 }, { "submission_id": "aoj_1287_4789438", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nint INF = 1000000;\nstring conv(int x){\n vector<int> t = {x / 3600, x % 3600 / 60, x % 60};\n string ans;\n for (int i = 0; i < 3; i++){\n string tmp = to_string(t[i]);\n if (tmp.size() == 1){\n tmp = '0' + tmp;\n }\n ans += tmp;\n if (i < 2){\n ans += ':';\n }\n }\n return ans;\n}\nint main(){\n while (1){\n int n;\n cin >> n;\n if (n == 0){\n break;\n }\n vector<vector<int>> t(n, vector<int>(3));\n for (int i = 0; i < n; i++){\n for (int j = 0; j < 3; j++){\n cin >> t[i][j];\n }\n }\n vector<vector<int>> s(60, vector<int>(60, 0));\n for (int i = 0; i < 60; i++){\n for (int j = 0; j < 60; j++){\n for (int k = 0; k < 60; k++){\n vector<int> p = {k, (i + k) % 60, (j + k) % 60};\n sort(p.begin(), p.end());\n while (1){\n for (int l = 0; l < n; l++){\n if (t[l] == p){\n s[i][j] |= 1 << l;\n }\n }\n if (!next_permutation(p.begin(), p.end())){\n break;\n }\n }\n }\n }\n }\n vector<int> s2(86400, 0);\n vector<int> p(3, 0);\n for (int i = 0; i < 86400; i++){\n if (i > 0){\n p[0] = (p[0] + 1) % 60;\n if (i % 60 == 0){\n p[1] = (p[1] + 1) % 60;\n }\n if (i % 720 == 0){\n p[2] = (p[2] + 1) % 60;\n }\n }\n int a = (p[1] - p[0] + 60) % 60;\n int b = (p[2] - p[0] + 60) % 60;\n s2[i] = s[a][b];\n }\n int mn = INF;\n int ansL, ansR;\n int R = 0;\n vector<int> cnt(n, 0);\n set<int> st;\n for (int L = 0; L < 86400; L++){\n while (R < 86400 && st.size() < n){\n for (int i = 0; i < n; i++){\n if (s2[R] >> i & 1){\n cnt[i]++;\n st.insert(i);\n }\n }\n R++;\n if (st.size() == n){\n break;\n }\n }\n if (st.size() == n){\n if (mn > R - L){\n mn = R - L;\n ansL = L;\n ansR = R - 1;\n }\n }\n for (int i = 0; i < n; i++){\n if (s2[L] >> i & 1){\n cnt[i]--;\n assert(cnt[i] >= 0);\n if (cnt[i] == 0){\n st.erase(i);\n }\n }\n }\n }\n cout << conv(ansL) << ' ' << conv(ansR) << endl;\n }\n}", "accuracy": 1, "time_ms": 1860, "memory_kb": 3656, "score_of_the_acc": -0.6991, "final_rank": 15 }, { "submission_id": "aoj_1287_4756590", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\ntypedef long long ll;\ntemplate<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return true; } return false; }\ntemplate<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return true; } return false; }\n#define all(x) (x).begin(),(x).end()\n#define fi first\n#define se second\n#define mp make_pair\n#define si(x) int(x.size())\nconst int mod=1000000007,MAX=200005,INF=1<<30;\n\nstring S(int t){\n string res;\n int a=t/3600;\n if(a<10) res+='0';\n res+=to_string(a);\n res+=':';\n \n t%=3600;\n int b=t/60;\n if(b<10) res+='0';\n res+=to_string(b);\n res+=':';\n \n int c=t%60;\n if(c<10) res+='0';\n res+=to_string(c);\n \n return res;\n}\n\nint main(){\n \n std::ifstream in(\"text.txt\");\n std::cin.rdbuf(in.rdbuf());\n cin.tie(0);\n ios::sync_with_stdio(false);\n \n while(1){\n int N;cin>>N;\n if(N==0) break;\n vector<vector<int>> A(N);\n for(int i=0;i<N;i++){\n vector<int> t(3);\n for(int j=0;j<3;j++) cin>>t[j];\n sort(all(t));\n \n do{\n for(int j=0;j<60;j++){\n int a=(t[0]+j)%60,b=(t[1]+j)%60,c=(t[2]+j)%60;\n \n if(a%5!=b/12) continue;\n \n A[i].push_back((a/5)*3600+b*60+c);\n }\n \n }while(next_permutation(all(t)));\n \n sort(all(A[i]));\n A[i].erase(unique(all(A[i])),A[i].end());\n }\n \n pair<int,int> res={INF,INF};\n \n for(int i=0;i<N;i++){\n for(int j=0;j<N;j++){\n if(i==j) continue;\n \n for(int a:A[i]){\n for(int b:A[j]){\n if(a>b) continue;\n \n bool ok=true;\n for(int k=0;k<N;k++){\n if(k==i||k==j) continue;\n auto x=lower_bound(all(A[k]),a);\n if(x==A[k].end()||*x>b) ok=false;\n }\n if(ok){\n chmin(res,mp(b-a,a));\n }\n }\n }\n }\n }\n \n cout<<S(res.se)<<\" \"<<S(res.fi+res.se)<<endl;\n }\n}", "accuracy": 1, "time_ms": 40, "memory_kb": 3284, "score_of_the_acc": -0.2094, "final_rank": 7 }, { "submission_id": "aoj_1287_4614377", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\ntypedef long long ll;\ntypedef pair<ll, ll> l_l;\ntypedef pair<int, int> i_i;\ntemplate<class T>\ninline bool chmax(T &a, T b) {\n if(a < b) {\n a = b;\n return true;\n }\n return false;\n}\n\ntemplate<class T>\ninline bool chmin(T &a, T b) {\n if(a > b) {\n a = b;\n return true;\n }\n return false;\n}\n\nconst long double EPS = 1e-10;\nconst long long INF = 1e18;\nconst long double PI = acos(-1.0L);\nint N;\nbool can[10][24*60*60];\nint MAX = 24*60*60;\nint factor[3] = {3600, 60, 1};\nint num[10];\n\nstring f(ll x) {\n string ret;\n ll tmp = x / 3600;\n string T = to_string(tmp);\n if(T.size() == 1) T = \"0\" + T;\n ret = ret + T + \":\";\n tmp = x / 60;\n tmp %= 60;\n T = to_string(tmp);\n if(T.size() == 1) T = \"0\" + T;\n ret = ret + T + \":\";\n tmp = x;\n tmp %= 60;\n T = to_string(tmp);\n if(T.size() == 1) T = \"0\" + T;\n ret = ret + T;\n return ret;\n}\n\nvoid solve() {\n for(int i = 0; i < 10; i++) {\n assert(num[i] == 0);\n for(int j = 0; j < MAX; j++) {\n can[i][j] = false;\n }\n }\n for(int i = 0; i < N; i++) {\n vector<int> v(3);\n for(int j = 0; j < 3; j++) cin >> v[j];\n sort(v.begin(), v.end());\n do {\n for(int k = 0; k < 60; k++) {\n vector<int> w(3);\n for(int j = 0; j < 3; j++) {\n w[j] = (v[j] + k) % 60;\n }\n for(int t = 0; t < MAX; t++) {\n int a = t / 720;\n a %= 60;\n if(a != w[0]) continue;\n a = t / 60;\n a %= 60;\n if(a != w[1]) continue;\n a = t;\n a %= 60;\n if(a != w[2]) continue;\n can[i][t] = true;\n }\n }\n } while(next_permutation(v.begin(), v.end()));\n }\n int ansl = 0;\n int ansr = MAX - 1;\n int ansinterval = MAX;\n int r = -1;\n for(int l = 0; l < MAX; l++) {\n while(true) {\n if(r == MAX) break;\n bool ok = true;\n for(int j = 0; j < N; j++) {\n if(num[j] == 0) ok = false;\n }\n if(ok) break;\n r++;\n if(r == MAX) break;\n for(int j = 0; j < N; j++) {\n if(can[j][r]) num[j]++;\n }\n }\n for(int j = 0; j < N; j++) {\n if(can[j][l]) num[j]--;\n }\n if(r == MAX) continue;\n int interval = r - l;\n if(chmin(ansinterval, interval)) {\n ansl = l;\n ansr = r;\n }\n //cerr << l << \" \" << r << endl;\n }\n cout << f(ansl) << \" \" << f(ansr) << endl;\n}\n\nint main() {\n //cout.precision(10);\n cin.tie(0);\n ios::sync_with_stdio(false);\n while(cin >> N) {\n if(N == 0) break;\n solve();\n }\n return 0;\n}", "accuracy": 1, "time_ms": 8000, "memory_kb": 4152, "score_of_the_acc": -1.8169, "final_rank": 20 }, { "submission_id": "aoj_1287_4281145", "code_snippet": "#define _CRT_SECURE_NO_WARNINGS\n#define _USE_MATH_DEFINES\n\n#pragma comment (linker, \"/STACK:526000000\")\n\n#include \"bits/stdc++.h\"\n\nusing namespace std;\ntypedef string::const_iterator State;\n#define eps 1e-5L\n#define MAX_MOD 1000000007LL\n#define GYAKU 500000004LL\n#define MOD 998244353LL\n#define pb push_back\n#define mp make_pair\ntypedef long long ll;\n#define REP(a,b) for(long long (a) = 0;(a) < (b);++(a))\n#define ALL(x) (x).begin(),(x).end()\n\n// geometry library\n\ntypedef complex<long double> Point;\ntypedef pair<complex<long double>, complex<long double>> Line;\n\ntypedef struct Circle {\n complex<long double> center;\n long double r;\n}Circle;\n\nlong double dot(Point a, Point b) {\n return (a.real() * b.real() + a.imag() * b.imag());\n}\nlong double cross(Point a, Point b) {\n return (a.real() * b.imag() - a.imag() * b.real());\n}\n\nlong double Dist_Line_Point(Line a, Point b) {\n if (dot(a.second - a.first, b - a.first) < eps) return abs(b - a.first);\n if (dot(a.first - a.second, b - a.second) < eps) return abs(b - a.second);\n return abs(cross(a.second - a.first, b - a.first)) / abs(a.second - a.first);\n}\n\nint is_intersected_ls(Line a, Line b) {\n return (cross(a.second - a.first, b.first - a.first) * cross(a.second - a.first, b.second - a.first) < eps) &&\n (cross(b.second - b.first, a.first - b.first) * cross(b.second - b.first, a.second - b.first) < eps);\n}\n\nPoint intersection_l(Line a, Line b) {\n Point da = a.second - a.first;\n Point db = b.second - b.first;\n return a.first + da * cross(db, b.first - a.first) / cross(db, da);\n}\n\nlong double Dist_Line_Line(Line a, Line b) {\n if (is_intersected_ls(a, b) == 1) {\n return 0;\n }\n return min({ Dist_Line_Point(a,b.first), Dist_Line_Point(a,b.second),Dist_Line_Point(b,a.first),Dist_Line_Point(b,a.second) });\n}\n\npair<Point, Point> intersection_Circle_Circle(Circle a, Circle b) {\n long double dist = abs(a.center - b.center);\n assert(dist <= eps + a.r + b.r);\n assert(dist + eps >= abs(a.r - b.r));\n Point target = b.center - a.center;\n long double pointer = target.real() * target.real() + target.imag() * target.imag();\n long double aa = pointer + a.r * a.r - b.r * b.r;\n aa /= 2.0L;\n Point l{ (aa * target.real() + target.imag() * sqrt(pointer * a.r * a.r - aa * aa)) / pointer,\n (aa * target.imag() - target.real() * sqrt(pointer * a.r * a.r - aa * aa)) / pointer };\n Point r{ (aa * target.real() - target.imag() * sqrt(pointer * a.r * a.r - aa * aa)) / pointer,\n (aa * target.imag() + target.real() * sqrt(pointer * a.r * a.r - aa * aa)) / pointer };\n r = r + a.center;\n l = l + a.center;\n return mp(l, r);\n}\n\n//end of geometry\n\ntemplate<typename A>\nA pows(A val, ll b) {\n assert(b >= 1);\n A ans = val;\n b--;\n while (b) {\n if (b % 2) {\n ans *= val;\n }\n val *= val;\n b /= 2LL;\n }\n return ans;\n}\n\ntemplate<typename A>\nclass Compressor {\npublic:\n bool is_zipped = false;\n map<A, ll> zipper;\n map<ll, A> unzipper;\n queue<A> fetcher;\n Compressor() {\n is_zipped = false;\n zipper.clear();\n unzipper.clear();\n }\n void add(A now) {\n assert(is_zipped == false);\n zipper[now] = 1;\n fetcher.push(now);\n }\n void exec() {\n assert(is_zipped == false);\n int cnt = 0;\n for (auto i = zipper.begin(); i != zipper.end(); ++i) {\n i->second = cnt;\n unzipper[cnt] = i->first;\n cnt++;\n }\n is_zipped = true;\n }\n ll fetch() {\n assert(is_zipped == true);\n A hoge = fetcher.front();\n fetcher.pop();\n return zipper[hoge];\n }\n ll zip(A now) {\n assert(is_zipped == true);\n assert(zipper.find(now) != zipper.end());\n return zipper[now];\n }\n A unzip(ll a) {\n assert(is_zipped == true);\n assert(a < unzipper.size());\n return unzipper[a];\n }\n ll next(A now) {\n auto x = zipper.upper_bound(now);\n if (x == zipper.end()) return zipper.size();\n return (ll)((*x).second);\n }\n ll back(A now) {\n auto x = zipper.lower_bound(now);\n if (x == zipper.begin()) return -1;\n x--;\n return (ll)((*x).second);\n }\n};\n\ntemplate<typename A>\nclass Matrix {\npublic:\n vector<vector<A>> data;\n Matrix(vector<vector<A>> a) :data(a) {\n\n }\n Matrix operator + (const Matrix obj) {\n vector<vector<A>> ans;\n assert(obj.data.size() == this->data.size());\n assert(obj.data[0].size() == this->data[0].size());\n REP(i, obj.data.size()) {\n ans.push_back(vector<A>());\n REP(q, obj.data[i].size()) {\n A hoge = obj.data[i][q] + (this->data[i][q]);\n ans.back().push_back(hoge);\n }\n }\n return Matrix(ans);\n }\n Matrix operator - (const Matrix obj) {\n vector<vector<A>> ans;\n assert(obj.data.size() == this->data.size());\n assert(obj.data[0].size() == this->data[0].size());\n REP(i, obj.data.size()) {\n ans.push_back(vector<A>());\n REP(q, obj.data[i].size()) {\n A hoge = this->data[i][q] - obj.data[i][q];\n ans.back().push_back(hoge);\n }\n }\n return Matrix(ans);\n }\n Matrix operator * (const Matrix obj) {\n vector<vector<A>> ans;\n assert(obj.data.size() == this->data[0].size());\n REP(i, this -> data.size()) {\n ans.push_back(vector<A>());\n REP(q, obj.data[0].size()) {\n A hoge = (this->data[i][0]) * (obj.data[0][q]);\n for (int t = 1; t < obj.data[i].size(); ++t) {\n hoge += this->data[i][t] * obj.data[t][q];\n }\n ans.back().push_back(hoge);\n }\n }\n return Matrix(ans);\n }\n Matrix& operator *= (const Matrix obj) {\n *this = (*this * obj);\n return *this;\n }\n Matrix& operator += (const Matrix obj) {\n *this = (*this + obj);\n return *this;\n }\n Matrix& operator -= (const Matrix obj) {\n *this = (*this - obj);\n return *this;\n }\n};\n\nclass modint {\npublic:\n using u64 = std::uint_fast64_t;\n u64 value = 0;\n u64 mod;\n modint(ll a, ll b) : value(((a% b) + 2 * b) % b), mod(b) {\n\n }\n modint operator+(const modint rhs) const {\n return modint(*this) += rhs;\n }\n modint operator-(const modint rhs) const {\n return modint(*this) -= rhs;\n }\n modint operator*(const modint rhs) const {\n return modint(*this) *= rhs;\n }\n modint operator/(const modint rhs) const {\n return modint(*this) /= rhs;\n }\n modint& operator+=(const modint rhs) {\n assert(rhs.mod == mod);\n value += rhs.value;\n if (value >= mod) {\n value -= mod;\n }\n return *this;\n }\n modint& operator-=(const modint rhs) {\n assert(rhs.mod == mod);\n if (value < rhs.value) {\n value += mod;\n }\n value -= rhs.value;\n return *this;\n }\n modint& operator*=(const modint rhs) {\n assert(rhs.mod == mod);\n value = (value * rhs.value) % mod;\n return *this;\n }\n modint& operator/=(modint rhs) {\n assert(rhs.mod == mod);\n ll rem = mod - 2;\n while (rem) {\n if (rem % 2) {\n *this *= rhs;\n }\n rhs *= rhs;\n rem /= 2LL;\n }\n return *this;\n }\n bool operator <(modint rhs) const {\n return value < rhs.value;\n }\n friend ostream& operator<<(ostream& os, modint& p) {\n os << p.value;\n return (os);\n }\n};\n\nclass Dice {\npublic:\n vector<ll> vertexs;\n //Up: 0,Left: 1,Center: 2,Right: 3,Adj: 4, Down: 5\n Dice(vector<ll> init) :vertexs(init) {\n\n }\n //Look from Center\n void RtoL() {\n for (int q = 1; q < 4; ++q) {\n swap(vertexs[q], vertexs[q + 1]);\n }\n }\n void LtoR() {\n for (int q = 3; q >= 1; --q) {\n swap(vertexs[q], vertexs[q + 1]);\n }\n }\n void UtoD() {\n swap(vertexs[5], vertexs[4]);\n swap(vertexs[2], vertexs[5]);\n swap(vertexs[0], vertexs[2]);\n }\n void DtoU() {\n swap(vertexs[0], vertexs[2]);\n swap(vertexs[2], vertexs[5]);\n swap(vertexs[5], vertexs[4]);\n }\n bool ReachAble(Dice now) {\n set<Dice> hoge;\n queue<Dice> next;\n next.push(now);\n hoge.insert(now);\n while (next.empty() == false) {\n Dice seeing = next.front();\n next.pop();\n if (seeing == *this) return true;\n seeing.RtoL();\n if (hoge.count(seeing) == 0) {\n hoge.insert(seeing);\n next.push(seeing);\n }\n seeing.LtoR();\n seeing.LtoR();\n if (hoge.count(seeing) == 0) {\n hoge.insert(seeing);\n next.push(seeing);\n }\n seeing.RtoL();\n seeing.UtoD();\n if (hoge.count(seeing) == 0) {\n hoge.insert(seeing);\n next.push(seeing);\n }\n seeing.DtoU();\n seeing.DtoU();\n if (hoge.count(seeing) == 0) {\n hoge.insert(seeing);\n next.push(seeing);\n }\n }\n return false;\n }\n bool operator ==(const Dice& a) {\n for (int q = 0; q < 6; ++q) {\n if (a.vertexs[q] != (*this).vertexs[q]) {\n return false;\n }\n }\n return true;\n }\n bool operator <(const Dice& a) const {\n return (*this).vertexs < a.vertexs;\n }\n};\n\npair<Dice, Dice> TwoDimDice(int center, int up) {\n int target = 1;\n while (true) {\n if (center != target && 7 - center != target && up != target && 7 - up != target) {\n break;\n }\n target++;\n }\n return mp(Dice(vector<ll>{up, target, center, 7 - target, 7 - center, 7 - up}), Dice(vector<ll>{up, 7 - target, center, target, 7 - center, 7 - up}));\n}\n\ntuple<Dice, Dice, Dice, Dice> OneDimDice(int center) {\n int bo = min(center, 7 - center);\n pair<int, int> goa;\n if (bo == 1) {\n goa = mp(2, 3);\n }\n else if (bo == 2) {\n goa = mp(1, 3);\n }\n else if (bo == 3) {\n goa = mp(1, 2);\n }\n tuple<Dice, Dice, Dice, Dice> now = make_tuple(Dice(vector<ll>{goa.first, goa.second, center, 7 - goa.second, 7 - center, 7 - goa.first}),\n Dice(vector<ll>{goa.first, 7 - goa.second, center, goa.second, 7 - center, 7 - goa.first}),\n Dice(vector<ll>{7 - goa.first, goa.second, center, 7 - goa.second, 7 - center, goa.first}),\n Dice(vector<ll>{7 - goa.first, 7 - goa.second, center, goa.second, 7 - center, goa.first}));\n return now;\n}\n\ntemplate<typename A, typename B>\nclass Dijkstra {\npublic:\n vector<vector<pair<int, A>>> vertexs;\n B Cost_Function;\n Dijkstra(int n, B cost) : Cost_Function(cost) {\n vertexs = vector<vector<pair<int, A>>>(n, vector<pair<int, A>>{});\n }\n ~Dijkstra() {\n vertexs.clear();\n }\n void add_edge(int a, int b, A c) {\n vertexs[a].push_back(mp(b, c));\n }\n vector<ll> build_result(int StartPoint) {\n vector<ll> dist(vertexs.size(), 2e18);\n dist[StartPoint] = 0;\n priority_queue<pair<ll, int>, vector<pair<ll, int>>, greater<pair<ll, int>>> next;\n next.push(make_pair(0, StartPoint));\n while (next.empty() == false) {\n pair<ll, int> now = next.top();\n next.pop();\n if (dist[now.second] != now.first) continue;\n for (auto x : vertexs[now.second]) {\n ll now_cost = now.first + Cost_Function(x.second);\n if (dist[x.first] > now_cost) {\n dist[x.first] = now_cost;\n next.push(mp(now_cost, x.first));\n }\n }\n }\n return dist;\n }\n};\n\nclass Dinic {\npublic:\n struct edge {\n int to;\n int cap;\n int rev;\n };\n vector<vector<edge>> Graph;\n vector<int> level;\n vector<int> itr;\n Dinic(int n) {\n Graph = vector<vector<edge>>(n, vector<edge>());\n }\n void add_edge(int a, int b, int cap) {\n Graph[a].push_back(edge{ b, cap ,(int)Graph[b].size() });\n Graph[b].push_back(edge{ a,0,(int)Graph[a].size() - 1 });\n }\n void bfs(int s) {\n level = vector<int>(Graph.size(), -1);\n level[s] = 0;\n queue<int> next;\n next.push(s);\n while (next.empty() == false) {\n int now = next.front();\n next.pop();\n for (auto x : Graph[now]) {\n if (x.cap == 0) continue;\n if (level[x.to] == -1) {\n level[x.to] = level[now] + 1;\n next.push(x.to);\n }\n }\n }\n }\n int dfs(int now, int goal, int val) {\n if (goal == now) return val;\n for (int& i = itr[now]; i < (int)Graph[now].size(); ++i) {\n edge& target = Graph[now][i];\n if (target.cap > 0 && level[now] < level[target.to]) {\n int d = dfs(target.to, goal, min(val, target.cap));\n if (d > 0) {\n target.cap -= d;\n Graph[target.to][target.rev].cap += d;\n return d;\n }\n }\n }\n return 0;\n }\n int run(int s, int t) {\n int ans = 0;\n int f = 0;\n while (bfs(s), level[t] >= 0) {\n itr = vector<int>(Graph.size(), 0);\n while ((f = dfs(s, t, 1e9)) > 0) {\n ans += f;\n }\n }\n return ans;\n }\n};\n\n//by ei1333\n//https://ei1333.github.io/luzhiled/snippets/structure/segment-tree.html\ntemplate< typename Monoid >\nstruct SegmentTree {\n using F = function< Monoid(Monoid, Monoid) >;\n\n int sz;\n vector< Monoid > seg;\n\n const F f;\n const Monoid M1;\n\n SegmentTree(int n, const F f, const Monoid& M1) : f(f), M1(M1) {\n sz = 1;\n while (sz < n) sz <<= 1;\n seg.assign(2 * sz + 1, M1);\n }\n\n void set(int k, const Monoid& x) {\n seg[k + sz] = x;\n }\n\n void build() {\n for (int k = sz - 1; k > 0; k--) {\n seg[k] = f(seg[2 * k + 0], seg[2 * k + 1]);\n }\n }\n\n void update(int k, const Monoid& x) {\n k += sz;\n seg[k] = x;\n while (k >>= 1) {\n seg[k] = f(seg[2 * k + 0], seg[2 * k + 1]);\n }\n }\n\n Monoid query(int a, int b) {\n Monoid L = M1, R = M1;\n for (a += sz, b += sz; a < b; a >>= 1, b >>= 1) {\n if (a & 1) L = f(L, seg[a++]);\n if (b & 1) R = f(seg[--b], R);\n }\n return f(L, R);\n }\n\n Monoid operator[](const int& k) const {\n return seg[k + sz];\n }\n\n template< typename C >\n int find_subtree(int a, const C& check, Monoid& M, bool type) {\n while (a < sz) {\n Monoid nxt = type ? f(seg[2 * a + type], M) : f(M, seg[2 * a + type]);\n if (check(nxt)) a = 2 * a + type;\n else M = nxt, a = 2 * a + 1 - type;\n }\n return a - sz;\n }\n\n\n template< typename C >\n int find_first(int a, const C& check) {\n Monoid L = M1;\n if (a <= 0) {\n if (check(f(L, seg[1]))) return find_subtree(1, check, L, false);\n return -1;\n }\n int b = sz;\n for (a += sz, b += sz; a < b; a >>= 1, b >>= 1) {\n if (a & 1) {\n Monoid nxt = f(L, seg[a]);\n if (check(nxt)) return find_subtree(a, check, L, false);\n L = nxt;\n ++a;\n }\n }\n return -1;\n }\n\n template< typename C >\n int find_last(int b, const C& check) {\n Monoid R = M1;\n if (b >= sz) {\n if (check(f(seg[1], R))) return find_subtree(1, check, R, true);\n return -1;\n }\n int a = sz;\n for (b += sz; a < b; a >>= 1, b >>= 1) {\n if (b & 1) {\n Monoid nxt = f(seg[--b], R);\n if (check(nxt)) return find_subtree(b, check, R, true);\n R = nxt;\n }\n }\n return -1;\n }\n};\n\nunsigned long xor128() {\n static unsigned long x = 123456789, y = 362436069, z = 521288629, w = 88675123;\n unsigned long t = (x ^ (x << 11));\n x = y; y = z; z = w;\n return (w = (w ^ (w >> 19)) ^ (t ^ (t >> 8)));\n}\n\nvoid init() {\n iostream::sync_with_stdio(false);\n cin.tie(0);\n}\n\n#define int ll\nvoid solve(){\n while (true) {\n int n;\n cin >> n;\n if (n == 0) return;\n vector<vector<int>> vertexs;\n REP(i, n) {\n int a[3];\n REP(q, 3) {\n cin >> a[q];\n }\n vector<int> tmp;\n sort(a, a + 3);\n do {\n REP(q, 60) {\n a[0]++;\n a[1]++;\n a[2]++;\n a[0] %= 60;\n a[1] %= 60;\n a[2] %= 60;\n int s = a[0];\n int m = a[1];\n int h = a[2];\n if (h % 5 != m / 12) continue;\n h /= 5;\n tmp.push_back(h * 3600 + m * 60 + s);\n }\n } while (next_permutation(a, a + 3));\n sort(ALL(tmp));\n tmp.erase(std::unique(tmp.begin(), tmp.end()), tmp.end());\n vertexs.push_back(tmp);\n }\n pair<int, int> ans(0, 1e9);\n REP(i, 60 * 60 * 12) {\n int ok = 1;\n int ending = 0;\n REP(q, n) {\n auto x = lower_bound(ALL(vertexs[q]), i);\n if (x == vertexs[q].end()) {\n ok = 0;\n break;\n }\n ending = max(ending, *x);\n }\n if (ok == 0) continue;\n if (ans.second - ans.first > ending - i) {\n ans = mp(i, ending);\n }\n }\n auto output = [](int a) {\n int hoge = a / 3600;\n if (hoge < 10) {\n cout << \"0\";\n }\n cout << hoge;\n cout << \":\";\n a %= 3600;\n hoge = a / 60;\n if (hoge < 10) {\n cout << \"0\";\n }\n cout << hoge;\n cout << \":\";\n a %= 60;\n hoge = a;\n if (hoge < 10) {\n cout << \"0\";\n }\n cout << hoge;\n };\n output(ans.first);\n cout << \" \";\n output(ans.second);\n cout << endl;\n }\n}\n#undef int\nint main() {\n init();\n solve();\n}", "accuracy": 1, "time_ms": 60, "memory_kb": 3040, "score_of_the_acc": -0.0401, "final_rank": 2 }, { "submission_id": "aoj_1287_3467874", "code_snippet": "#include<iostream>\n#include<string>\n#include<cstdio>\n#include<vector>\n#include<cmath>\n#include<algorithm>\n#include<functional>\n#include<iomanip>\n#include<queue>\n#include<ciso646>\n#include<random>\n#include<map>\n#include<set>\n#include<complex>\n#include<bitset>\n#include<stack>\n#include<unordered_map>\n#include<utility>\nusing namespace std;\ntypedef long long ll;\ntypedef unsigned long long ul;\ntypedef unsigned int ui;\ntypedef long double ld;\ntypedef pair<int, int> P;\ntypedef pair<ll, ll> LP;\ntypedef pair<ld, ld> LDP;\ntypedef complex<ld> Point;\nconst ll mod = 1000000007;\nconst ll INF = mod * mod;\nconst ld eps = 1e-8;\nconst ld pi = acos(-1.0);\n#define stop char nyaa;cin>>nyaa;\n#define rep(i,n) for(int i=0;i<n;i++)\n#define per(i,n) for(int i=n-1;i>=0;i--)\n#define Rep(i,sta,n) for(int i=sta;i<n;i++)\n#define rep1(i,n) for(int i=1;i<=n;i++)\n#define per1(i,n) for(int i=n;i>=1;i--)\n#define Rep1(i,sta,n) for(int i=sta;i<=n;i++)\nint n;\nint a[10][3];\nint c[10];\n\nbool ok() {\n\trep(j, n) {\n\t\tif (c[j] == 0)return false;\n\t}\n\treturn true;\n}\nbool exi[43200][10];\n\nvoid output_as_time(int x) {\n\tstring s;\n\tif (x / 3600 < 10)s += \"0\";\n\ts += to_string(x/3600);\n\ts.push_back(':');\n\tx %= 3600;\n\tif (x / 60 < 10)s += \"0\";\n\ts += to_string(x / 60);\n\ts.push_back(':');\n\tx %= 60;\n\tif (x < 10)s += \"0\";\n\ts += to_string(x);\n\tcout << s;\n}\nvoid solve() {\n\twhile (cin >> n, n) {\n\t\trep(i, 43200) {\n\t\t\trep(j, n) {\n\t\t\t\texi[i][j] = false;\n\t\t\t}\n\t\t}\n\t\tfill(c, c + n, 0);\n\t\trep(i, n) {\n\t\t\trep(j, 3) {\n\t\t\t\tcin >> a[i][j];\n\t\t\t}\n\t\t}\n\t\trep(i, n) {\n\t\t\trep(j, 60) {\n\t\t\t\tvector<int> v;\n\t\t\t\trep(k, 3) {\n\t\t\t\t\tv.push_back((a[i][k]+j) % 60);\n\t\t\t\t}\n\t\t\t\tsort(v.begin(), v.end());\n\t\t\t\twhile (true) {\n\t\t\t\t\tif (v[0] % 5 == v[1] / 12) {\n\t\t\t\t\t\tint sum = (v[0] / 5) * 3600 + v[1] * 60 + v[2];\n\t\t\t\t\t\texi[sum][i] = true;\n\t\t\t\t\t}\n\t\t\t\t\tif (!next_permutation(v.begin(), v.end()))break;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\tint mi = 50000; int le = 0;\n\t\tint ri = 0;\n\t\trep(i, 43200) {\n\t\t\twhile (true) {\n\t\t\t\tif (ok())break;\n\t\t\t\tif (ri ==43200)break;\n\t\t\t\trep(j, n) {\n\t\t\t\t\tif (exi[ri][j])c[j]++;\n\t\t\t\t}\n\t\t\t\tri++;\n\t\t\t}\n\t\t\tif (!ok())break;\n\t\t\tif (ri - i < mi) {\n\t\t\t\tmi = ri - i; le = i;\n\t\t\t}\n\t\t\trep(j, n) {\n\t\t\t\tif (exi[i][j])c[j]--;\n\t\t\t}\n\t\t}\n\t\toutput_as_time(le);\n\t\tcout << \" \";\n\t\toutput_as_time(le + mi-1);\n\t\tcout << endl;\n\t}\n}\nint main() {\n\tsolve();\n\t//stop\n\treturn 0;\n}", "accuracy": 1, "time_ms": 30, "memory_kb": 3652, "score_of_the_acc": -0.4673, "final_rank": 11 }, { "submission_id": "aoj_1287_3325469", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\n\nbool isValid(vector<int> t){\n return t[0]%5==t[1]/12;\n}\n\nint calc(vector<int> t){\n return (t[0]/5)*60*60+t[1]*60+t[2];\n}\n\nvoid output(int t){\n cout<<setw(2);\n cout<<setfill('0');\n cout<<(t/3600)<<\":\";\n cout<<setw(2);\n cout<<setfill('0');\n cout<<(t/60%60)<<\":\";\n cout<<setw(2);\n cout<<setfill('0');\n cout<<(t%60);\n}\nint solve(int n){\n vector<vector<int>> t(n,vector<int>(3));\n for(int i=0;i<n;i++){\n for(int j=0;j<3;j++){\n cin>>t[i][j];\n }\n }\n\n vector<vector<int>> cand(n);\n for(int i=0;i<n;i++){\n for(int k=0;k<60;k++){\n for(int j=0;j<3;j++) t[i][j]=(t[i][j]+1)%60;\n for(int j=0;j<6;j++){\n if(isValid(t[i])){\n cand[i].push_back(calc(t[i]));\n cand[i].push_back(calc(t[i])+12*60*60);\n }\n next_permutation(t[i].begin(),t[i].end());\n }\n }\n cand[i].push_back(1e8);\n sort(cand[i].begin(),cand[i].end());\n }\n pair<int,int> res={1e8,1e8};\n for(int i=0;i<24*60*60;i++){\n int till=0;\n for(int j=0;j<n;j++){\n till=max(till,*lower_bound(cand[j].begin(),cand[j].end(),i));\n }\n res=min(res,{till-i,i});\n }\n output(res.second);\n cout<<\" \";\n output(res.first+res.second);\n cout<<endl;\n}\n\nint main(){\n int n;\n while(cin>>n,n){\n solve(n);\n }\n return 0;\n}", "accuracy": 1, "time_ms": 130, "memory_kb": 3124, "score_of_the_acc": -0.108, "final_rank": 4 }, { "submission_id": "aoj_1287_3224216", "code_snippet": "// g++ -std=c++11 a.cpp\n#include<iostream>\n#include<vector>\n#include<string>\n#include<algorithm>\t\n#include<map>\n#include<set>\n#include<unordered_map>\n#include<utility>\n#include<cmath>\n#include<random>\n#include<cstring>\n#include<queue>\n#include<stack>\n#include<bitset>\n#include<cstdio>\n#include<sstream>\n#include<iomanip>\n#include<assert.h>\n#include<typeinfo>\n#define loop(i,a,b) for(int i=a;i<b;i++) \n#define rep(i,a) loop(i,0,a)\n#define FOR(i,a) for(auto i:a)\n#define pb push_back\n#define all(in) in.begin(),in.end()\n#define shosu(x) fixed<<setprecision(x)\n#define show1d(v) rep(_,v.size())cout<<\" \"<<v[_];cout<<endl;\n#define show2d(v) rep(_,v.size()){rep(__,v[_].size())cout<<\" \"<<v[_][__];cout<<endl;}cout<<endl;\nusing namespace std;\n//kaewasuretyuui\ntypedef long long ll;\n#define int ll\ntypedef int Def;\ntypedef pair<Def,Def> pii;\ntypedef vector<Def> vi;\ntypedef vector<vi> vvi;\ntypedef vector<pii> vp;\ntypedef vector<vp> vvp;\ntypedef vector<string> vs;\ntypedef vector<double> vd;\ntypedef vector<vd> vvd;\ntypedef pair<Def,pii> pip;\ntypedef vector<pip>vip;\n#define mt make_tuple\ntypedef tuple<int,int,int,int> tp;\ntypedef vector<tp> vt;\ntemplate<typename A,typename B>bool cmin(A &a,const B &b){return a>b?(a=b,true):false;}\ntemplate<typename A,typename B>bool cmax(A &a,const B &b){return a<b?(a=b,true):false;}\n//template<class C>constexpr int size(const C &c){return (int)c.size();}\n//template<class T,size_t N> constexpr int size(const T (&xs)[N])noexcept{return (int)N;}\nconst double PI=acos(-1);\nconst double EPS=1e-9;\nDef inf = sizeof(Def) == sizeof(long long) ? 1e18+10 : 1e9+10;\nint dx[]={0,1,0,-1,1,1,-1,-1};\nint dy[]={1,0,-1,0,1,-1,1,-1};//RDLU\n//09062355704\nint f(int a,int b,int c){\n\tint out=b*60+c;\n\ta-=b/12;\n\tif(a<0)a+=60;\n\tif(a%5)return -1;\n\tout+=a/5*3600;\n\treturn out;\n}\nsigned main(){\n\tint n;\n\twhile(cin>>n,n){\n\t\tvvi in(n,vi(3));\n\t\trep(i,n)rep(j,3)cin>>in[i][j];\n\t\tvi dp(43200,-inf);\n\t\trep(i,n){\n\t\t\tvi t;\n\t\t\trep(j,60){\n\t\t\t\tsort(all(in[i]));\n\t\t\t\tdo{\n\t\t\t\t\tint a=f(in[i][0],in[i][1],in[i][2]);\n\t\t\t\t\tif(a+1)t.pb(a);\n\t\t\t\t}while(next_permutation(all(in[i])));\n\t\t\t\trep(k,3)(in[i][k]+=1)%=60;\n\t\t\t}\n//\t\t\tshow1d(t);\n\t\t\tvi ndp(43200,-inf);\n\t\t\trep(j,43200)rep(k,t.size())if(i==0||dp[j]+inf){\n\t\t\t\tif(i==0){\n\t\t\t\t\tndp[t[k]]=t[k];\n\t\t\t\t\tcontinue;\n\t\t\t\t}\n\t\t\t\tint mi=dp[j],ma=j;\n\t\t\t\tcmin(mi,t[k]);\n\t\t\t\tcmax(ma,t[k]);\n\t\t\t\tcmax(ndp[ma],mi);\n\t\t\t}\n\t\t\tdp=ndp;\n\t\t}\n\t\tint mi=inf,it;\n\t\trep(i,43200)if(mi>i-dp[i]){\n\t\t\tmi=i-dp[i];\n\t\t\tit=i;\n\t\t}\n\t\tprintf(\"%02d:%02d:%02d %02d:%02d:%02d\\n\",dp[it]/3600,dp[it]%3600/60,dp[it]%60,it/3600,it%3600/60,it%60);\n\t}\n}", "accuracy": 1, "time_ms": 250, "memory_kb": 3808, "score_of_the_acc": -0.6047, "final_rank": 12 }, { "submission_id": "aoj_1287_3224215", "code_snippet": "#include<iostream>\n#include<vector>\n#include<string>\n#include<algorithm>\n#include<map>\n#include<set>\n#include<unordered_map>\n#include<utility>\n#include<cmath>\n#include<random>\n#include<cstring>\n#include<queue>\n#include<stack>\n#include<bitset>\n#include<cstdio>\n#include<sstream>\n#include<iomanip>\n#include<assert.h>\n#include<typeinfo>\n#define loop(i,a,b) for(int i=a;i<b;i++)\n#define rep(i,a) loop(i,0,a)\n#define FOR(i,a) for(auto i:a)\n#define pb push_back\n#define all(in) in.begin(),in.end()\n#define shosu(x) fixed<<setprecision(x)\n#define show1d(v) rep(_,v.size())cout<<\" \"<<v[_];cout<<endl;\n#define show2d(v) rep(_,v.size()){rep(__,v[_].size())cout<<\" \"<<v[_][__];cout<<endl;}cout<<endl;\nusing namespace std;\n//kaewasuretyuui\ntypedef long long ll;\n#define int ll\ntypedef int Def;\ntypedef pair<Def,Def> pii;\ntypedef vector<Def> vi;\ntypedef vector<vi> vvi;\ntypedef vector<pii> vp;\ntypedef vector<vp> vvp;\ntypedef vector<string> vs;\ntypedef vector<double> vd;\ntypedef vector<vd> vvd;\ntypedef pair<Def,pii> pip;\ntypedef vector<pip>vip;\n//#define mt make_tuple\n//typedef tuple<int,int,int,int> tp;\n//typedef vector<tp> vt;\ntemplate<typename A,typename B>bool cmin(A &a,const B &b){return a>b?(a=b,true):false;}\ntemplate<typename A,typename B>bool cmax(A &a,const B &b){return a<b?(a=b,true):false;}\n//template<class C>constexpr int size(const C &c){return (int)c.size();}\n//template<class T,size_t N> constexpr int size(const T (&xs)[N])noexcept{return (int)N;}\nconst double PI=acos(-1);\nconst double EPS=1e-9;\nDef inf = sizeof(Def) == sizeof(long long) ? 1e18+10 : 1e9+10;\nint dx[]={0,1,0,-1,1,1,-1,-1};\nint dy[]={1,0,-1,0,1,-1,1,-1};//RDLU\n//09062355704\nint f(int a,int b,int c){\n int out=b*60+c;\n a-=b/12;\n if(a<0)a+=60;\n if(a%5)return -1;\n out+=a/5*3600;\n return out;\n}\nsigned main(){\n int n;\n while(cin>>n,n){\n vvi in(n,vi(3));\n rep(i,n)rep(j,3)cin>>in[i][j];\n vi dp(43200,-inf);\n rep(i,n){\n vi t;\n rep(j,60){\n sort(all(in[i]));\n do{\n int a=f(in[i][0],in[i][1],in[i][2]);\n if(a+1)t.pb(a);\n }while(next_permutation(all(in[i])));\n rep(k,3)(in[i][k]+=1)%=60;\n }\n //show1d(t);\n vi ndp(43200,-inf);\n rep(j,43200)rep(k,t.size())if(i==0||dp[j]+inf){\n if(i==0){\n ndp[t[k]]=t[k];\n continue;\n }\n int mi=dp[j],ma=j;\n cmin(mi,t[k]);\n cmax(ma,t[k]);\n cmax(ndp[ma],mi);\n }\n dp=ndp;\n }\n int mi=inf,it;\n rep(i,43200)if(mi>i-dp[i]){\n mi=i-dp[i];\n it=i;\n }\n// cout<<it<<\" \"<<dp[it]<<endl;\n printf(\"%02d:%02d:%02d %02d:%02d:%02d\\n\",dp[it]/3600,dp[it]%3600/60,dp[it]%60,it/3600,it%3600/60,it%60);\n }\n}", "accuracy": 1, "time_ms": 250, "memory_kb": 3808, "score_of_the_acc": -0.6047, "final_rank": 12 }, { "submission_id": "aoj_1287_3015244", "code_snippet": "#include <iostream>\n#include <vector>\n#include <algorithm>\n#include <string>\n#include <cassert>\n#include <cstring>\nusing namespace std;\n\nconst int TM_MAX = 44000;\n\nbool is_valid_time(vector<int> vec) {\n for(int i=0; i<3; i++) {\n vec[i] %= 60;\n }\n\n int h = vec[0], m = vec[1];\n int tick = h % 5;\n int lb = 12 * tick, ub = 12 * (tick + 1);\n return lb <= m && m < ub;\n}\n\nint calc_time(vector<int> vec) {\n for(int i=0; i<3; i++) {\n vec[i] %= 60;\n }\n vec[0] = vec[0] / 5 * 5;\n return vec[0] * 60 * 12 + vec[1] * 60 + vec[2];\n}\n\nstruct Segment {\n int l, r, len;\n Segment() {}\n Segment(int x, int y) {\n assert(x <= y);\n l = x, r = y;\n len = r - l + 1;\n }\n bool operator<(const Segment &s) const {\n if(len != s.len) return len < s.len;\n return l < s.l;\n }\n};\n\nstring convert_time(int tm) {\n int h = (tm / 3600) % 12;\n int m = (tm / 60) % 60;\n int s = tm % 60;\n\n char buf[30];\n sprintf(buf, \"%02d:%02d:%02d\", h, m, s);\n\n return string(buf);\n}\n\nstring convert_seg(Segment s) {\n string ret = \"\";\n ret += convert_time(s.l) + \" \";\n ret += convert_time(s.r);\n return ret;\n}\n\nint main() {\n int N;\n while(cin >> N, N) {\n vector<int> watch_list[TM_MAX];\n for(int i=0; i<N; i++) {\n vector<int> times(3);\n for(int k=0; k<3; k++) {\n cin >> times[k];\n }\n\n sort(times.begin(), times.end());\n do {\n for(int k=0; k<60; k++) {\n vector<int> tmp = times;\n for(int x=0; x<3; x++) tmp[x] += k;\n\n if(!is_valid_time(tmp)) continue;\n\n int point = calc_time(tmp);\n watch_list[point].push_back(i);\n }\n }while(next_permutation(times.begin(), times.end()));\n }\n\n int cnt = 0;\n vector<int> appeared(N);\n\n int ub = 0, lb = 0;\n vector<Segment> seg;\n while(ub < TM_MAX) {\n for(; ub < TM_MAX && cnt != N; ub++) {\n for(auto x : watch_list[ub]) {\n if(appeared[x] == 0) cnt++;\n appeared[x]++;\n }\n }\n\n for(; cnt == N; lb++) {\n for(auto x : watch_list[lb]) {\n if(appeared[x] == 1) cnt--;\n appeared[x]--;\n }\n }\n\n if(ub != TM_MAX) {\n seg.push_back(Segment(lb - 1, ub - 1));\n }\n }\n\n sort(seg.begin(), seg.end());\n cout << convert_seg(seg[0]) << endl;\n }\n return 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 4200, "score_of_the_acc": -0.8507, "final_rank": 16 } ]
aoj_1286_cpp
Problem B: Expected Allowance Hideyuki is allowed by his father Ujisato some 1000 yen bills every month for his pocket money. In the first day of every month, the number of bills is decided as follows. Ujisato prepares n pieces of m -sided dice and declares the cutback k . Hideyuki rolls these dice. The number of bills given is the sum of the spots of the rolled dice decreased by the cutback. Fortunately to Hideyuki, Ujisato promises him to give at least one bill, even if the sum of the spots does not exceed the cutback. Each of the dice has spots of 1 through m inclusive on each side, and the probability of each side is the same. In this problem, you are asked to write a program that finds the expected value of the number of given bills. For example, when n = 2, m = 6 and k = 3, the probabilities of the number of bills being 1, 2, 3, 4, 5, 6, 7, 8 and 9 are 1/36 + 2/36 + 3/36, 4/36, 5/36, 6/36, 5/36, 4/36, 3/36, 2/36 and 1/36, respectively. Therefore, the expected value is (1/36 + 2/36 + 3/36) × 1 + 4/36 × 2 + 5/36 × 3 + 6/36 × 4 + 5/36 × 5 + 4/36 × 6 + 3/36 × 7 + 2/36 × 8 + 1/36 × 9, which is approximately 4.11111111. Input The input is a sequence of lines each of which contains three integers n, m and k in this order. They satisfy the following conditions. 1 ≤ n 2 ≤ m 0 ≤ k < nm nm × m n < 100000000 (10 8 ) The end of the input is indicated by a line containing three zeros. Output The output should be comprised of lines each of which contains a single decimal fraction. It is the expected number of bills and may have an error less than 10 -7 . No other characters should occur in the output. Sample Input 2 6 0 2 6 3 3 10 9 13 3 27 1 2008 3 0 0 0 Output for the Sample Input 7.00000000 4.11111111 7.71000000 1.42902599 1001.50298805
[ { "submission_id": "aoj_1286_10895025", "code_snippet": "#include<bits/stdc++.h>\n// #include<atcoder/all>\n// #include<boost/multiprecision/cpp_int.hpp>\n\nusing namespace std;\n// using namespace atcoder;\n// using bint = boost::multiprecision::cpp_int;\nusing ll = long long;\nusing ull = unsigned long long;\nusing ld = long double;\nusing pii = pair<int,int>;\nusing pll = pair<ll,ll>;\nusing vi = vector<ll>;\nusing vvi = vector<vi>;\nusing vvvi = vector<vvi>;\nusing ve = vector<vector<int>>;\nusing vb = vector<bool>;\nusing vvb = vector<vb>;\n#define rep(i,s,n) for(ll i = (ll)s;i < (ll)n;i++)\n#define rrep(i,l,r) for(ll i = r-1;i >= l;i--)\n#define ALL(x) (x).begin(),(x).end()\n#define sz(c) ((ll)(c).size())\n#define LB(A,x) (int)(lower_bound(A.begin(),A.end(),x)-A.begin())\n#define UB(A,x) (int)(upper_bound(A.begin(),A.end(),x)-A.begin())\n// #define MOD 1000000007\n#define MOD 998244353\ntemplate<typename T>using min_priority_queue=priority_queue<T,vector<T>,greater<T>>;\ntemplate<typename T>ostream&operator<<(ostream&os,vector<T>&v){for(int i = 0;i < v.size();i++)os<<v[i]<<(i+1!=v.size()?\" \":\"\");return os;}\ntemplate<typename T>istream&operator>>(istream&is,vector<T>&v){for(T&in:v)is>>in;return is;}\ntemplate<typename T1,typename T2>ostream&operator<<(ostream&os,pair<T1,T2>&p){os<<p.first<<\" \"<<p.second;return os;}\ntemplate<typename T1,typename T2>istream&operator>>(istream&is,pair<T1,T2>&p){is>>p.first>>p.second;return is;}\ntemplate<typename T> inline bool chmax(T &a,T b){if(a < b){a = b;return true;}return false;}\ntemplate<typename T> inline bool chmin(T &a,T b){if(a > b){a = b;return true;}return false;}\nld dist(ld x1,ld y1,ld x2, ld y2){return sqrtl((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2));}\n\n\nint main(){\n \n ios_base::sync_with_stdio(0), cin.tie(0);\n while(1){\n ll n,m,K;cin >> n >> m >> K;\n if(!n)break;\n ld res = 0;\n auto dfs = [&](auto &dfs,int dep,ll sm)->void{\n if(dep == n){\n res += max(sm-K,1ll);\n return ;\n }\n rep(i,1,m+1)dfs(dfs,dep+1,sm+i);\n };\n dfs(dfs,0,0);\n ll de = 1;\n rep(i,0,n)de *= m;\n res /= de;\n cout << fixed << setprecision(20) << res << \"\\n\";\n }\n \n\n return 0;\n}", "accuracy": 1, "time_ms": 110, "memory_kb": 3464, "score_of_the_acc": -0.1434, "final_rank": 15 }, { "submission_id": "aoj_1286_7537231", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n#define rep(i, n) for (int i = 0; i < (n); i++)\n#define per(i, n) for (int i = (n)-1; i >= 0; i--)\n#define rep2(i, l, r) for (int i = (l); i < (r); i++)\n#define per2(i, l, r) for (int i = (r)-1; i >= (l); i--)\n#define each(e, v) for (auto &e : v)\n#define MM << \" \" <<\n#define pb push_back\n#define eb emplace_back\n#define all(x) begin(x), end(x)\n#define rall(x) rbegin(x), rend(x)\n#define sz(x) (int)x.size()\nusing ll = long long;\nusing pii = pair<int, int>;\nusing pil = pair<int, ll>;\nusing pli = pair<ll, int>;\nusing pll = pair<ll, ll>;\n\ntemplate <typename T>\nusing minheap = priority_queue<T, vector<T>, greater<T>>;\n\ntemplate <typename T>\nusing maxheap = priority_queue<T>;\n\ntemplate <typename T>\nbool chmax(T &x, const T &y) {\n return (x < y) ? (x = y, true) : false;\n}\n\ntemplate <typename T>\nbool chmin(T &x, const T &y) {\n return (x > y) ? (x = y, true) : false;\n}\n\ntemplate <typename T>\nint flg(T x, int i) {\n return (x >> i) & 1;\n}\n\ntemplate <typename T>\nvoid print(const vector<T> &v, T x = 0) {\n int n = v.size();\n for (int i = 0; i < n; i++) cout << v[i] + x << (i == n - 1 ? '\\n' : ' ');\n if (v.empty()) cout << '\\n';\n}\n\ntemplate <typename T>\nvoid printn(const vector<T> &v, T x = 0) {\n int n = v.size();\n for (int i = 0; i < n; i++) cout << v[i] + x << '\\n';\n}\n\ntemplate <typename T>\nint lb(const vector<T> &v, T x) {\n return lower_bound(begin(v), end(v), x) - begin(v);\n}\n\ntemplate <typename T>\nint ub(const vector<T> &v, T x) {\n return upper_bound(begin(v), end(v), x) - begin(v);\n}\n\ntemplate <typename T>\nvoid rearrange(vector<T> &v) {\n sort(begin(v), end(v));\n v.erase(unique(begin(v), end(v)), end(v));\n}\n\ntemplate <typename T>\nvector<int> id_sort(const vector<T> &v, bool greater = false) {\n int n = v.size();\n vector<int> ret(n);\n iota(begin(ret), end(ret), 0);\n sort(begin(ret), end(ret), [&](int i, int j) { return greater ? v[i] > v[j] : v[i] < v[j]; });\n return ret;\n}\n\ntemplate <typename S, typename T>\npair<S, T> operator+(const pair<S, T> &p, const pair<S, T> &q) {\n return make_pair(p.first + q.first, p.second + q.second);\n}\n\ntemplate <typename S, typename T>\npair<S, T> operator-(const pair<S, T> &p, const pair<S, T> &q) {\n return make_pair(p.first - q.first, p.second - q.second);\n}\n\ntemplate <typename S, typename T>\nistream &operator>>(istream &is, pair<S, T> &p) {\n S a;\n T b;\n is >> a >> b;\n p = make_pair(a, b);\n return is;\n}\n\ntemplate <typename S, typename T>\nostream &operator<<(ostream &os, const pair<S, T> &p) {\n return os << p.first << ' ' << p.second;\n}\n\nstruct io_setup {\n io_setup() {\n ios_base::sync_with_stdio(false);\n cin.tie(NULL);\n cout << fixed << setprecision(15);\n }\n} io_setup;\n\nconst int inf = (1 << 30) - 1;\nconst ll INF = (1LL << 60) - 1;\n// const int MOD = 1000000007;\nconst int MOD = 998244353;\n\nvoid solve() {\n int N, M, K;\n cin >> N >> M >> K;\n\n if (N == 0) exit(0);\n\n using ld = long double;\n int L = N * M;\n\n vector<ld> dp(L + 1, 0), ndp(L + 1, 0);\n dp[0] = 1;\n\n rep(i, N) {\n fill(all(ndp), 0);\n rep(j, L) {\n rep2(k, 1, M + 1) {\n if (j + k <= L) ndp[j + k] += dp[j] / M;\n }\n }\n swap(dp, ndp);\n }\n\n ld ans = 0;\n\n rep(i, L + 1) { ans += dp[i] * max(1, i - K); }\n cout << ans << '\\n';\n}\n\nint main() {\n while (true) solve();\n}", "accuracy": 1, "time_ms": 90, "memory_kb": 3556, "score_of_the_acc": -0.1225, "final_rank": 12 }, { "submission_id": "aoj_1286_6055698", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\nint n, m, k;\ndouble ans;\n\nvoid dfs(double p, int t, int tot){\n if(t == n){\n ans += p * max(1, tot - k);\n return;\n }\n for(int i = 1; i <= m; i++){\n dfs(p / m, t + 1, tot + i);\n }\n}\n\nint main(){\n cout << fixed << setprecision(12);\n while(1){\n cin >> n >> m >> k;\n if(n == 0) break;\n ans = 0;\n dfs(1.0, 0, 0);\n cout << ans << \"\\n\";\n }\n}", "accuracy": 1, "time_ms": 60, "memory_kb": 3300, "score_of_the_acc": -0.0681, "final_rank": 9 }, { "submission_id": "aoj_1286_6025865", "code_snippet": "#include <bits/stdc++.h>\n#define rep(i,n) for(int i = 0; i < (n); i++)\nusing namespace std;\ntypedef long long ll;\n\nint main(){\n\tcin.tie(0);\n\tios::sync_with_stdio(0);\n\tcout << fixed << setprecision(20);\n\t\n\twhile(true) {\n\t\tint n,m,k; cin >> n >> m >> k; if(n == 0 && m == 0 && k == 0) break;\n\t\tvector<double> dp(n * m + 1, 0.0);\n\t\tdp[0] = 1.0;\n\t\trep(i,n) {\n\t\t\tvector<double> nt(n * m + 1, 0.0);\n\t\t\trep(j,n*m)rep(k,m) nt[j + k + 1] += dp[j] / (double)m;\n\t\t\tswap(nt, dp);\n\t\t}\n\n\t\tdouble ans = 0.0;\n\t\trep(i,n*m+1) ans += (i <= k ? dp[i] : dp[i] * (i - k));\n\t\tcout << ans << endl;\n\t}\n}", "accuracy": 1, "time_ms": 80, "memory_kb": 3672, "score_of_the_acc": -0.1161, "final_rank": 11 }, { "submission_id": "aoj_1286_5964715", "code_snippet": "#define rep(i, n) for(int i=0; i<(n); ++i)\n#define rrep(i, n) for(int i=(n-1); i>=0; --i)\n#define rep2(i, s, n) for(int i=s; i<(n); ++i)\n#define ALL(v) (v).begin(), (v).end()\n#define memr(dp, val) memset(dp, val, sizeof(dp))\n#define fi first\n#define se second\ntemplate<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }\ntemplate<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }\n#include <bits/stdc++.h>\nusing namespace std;\ntemplate<typename T>\nusing min_priority_queue = priority_queue<T, vector<T>, greater<T> >;\ntypedef long long ll;\nconst int INTINF = INT_MAX >> 1;\nstatic const ll LLINF = (LLONG_MAX >> 1);\n\nint n, m, k;\nint m_pow_n;\n\nbool input(){\n\n\tcin >> n >> m >> k;\n\tif(!n && !m && !k) return false;\n\n\treturn true;\n}\n\ndouble solve(int dep, int val){\n\tdouble ret = 0;\n\tif(dep == n){\n\t\treturn double(max(val - k, 1)) / double(m_pow_n);\n\t}\n\trep2(i, 1, m+1){\n\t\tret += solve(dep + 1, val + i);\n\t}\n\treturn ret;\n}\n\nint main(){\n\tstd::cout << std::fixed << std::setprecision(15);\n\n\twhile(input()){\n\t\tm_pow_n = 1;\n\t\trep(i, n) m_pow_n *= m;\n\t\tcout << solve(0, 0) << endl; \n\t}\n\n return 0;\n}", "accuracy": 1, "time_ms": 50, "memory_kb": 3292, "score_of_the_acc": -0.0544, "final_rank": 4 }, { "submission_id": "aoj_1286_5811649", "code_snippet": "#line 1 \"/Users/nok0/Documents/Programming/nok0/cftemp.hpp\"\n#include <bits/stdc++.h>\nusing namespace std;\n\n#pragma region Macros\n// rep macro\n#define foa(v, a) for(auto &v : a)\n#define REPname(a, b, c, d, e, ...) e\n#define REP(...) REPname(__VA_ARGS__, REP3, REP2, REP1, REP0)(__VA_ARGS__)\n#define REP0(x) for(int i = 0; i < (x); ++i)\n#define REP1(i, x) for(int i = 0; i < (x); ++i)\n#define REP2(i, l, r) for(int i = (l); i < (r); ++i)\n#define REP3(i, l, r, c) for(int i = (l); i < (r); i += (c))\n#define REPSname(a, b, c, ...) c\n#define REPS(...) REPSname(__VA_ARGS__, REPS1, REPS0)(__VA_ARGS__)\n#define REPS0(x) for(int i = 1; i <= (x); ++i)\n#define REPS1(i, x) for(int i = 1; i <= (x); ++i)\n#define RREPname(a, b, c, d, e, ...) e\n#define RREP(...) RREPname(__VA_ARGS__, RREP3, RREP2, RREP1, RREP0)(__VA_ARGS__)\n#define RREP0(x) for(int i = (x)-1; i >= 0; --i)\n#define RREP1(i, x) for(int i = (x)-1; i >= 0; --i)\n#define RREP2(i, r, l) for(int i = (r)-1; i >= (l); --i)\n#define RREP3(i, r, l, c) for(int i = (r)-1; i >= (l); i -= (c))\n#define RREPSname(a, b, c, ...) c\n#define RREPS(...) RREPSname(__VA_ARGS__, RREPS1, RREPS0)(__VA_ARGS__)\n#define RREPS0(x) for(int i = (x); i >= 1; --i)\n#define RREPS1(i, x) for(int i = (x); i >= 1; --i)\n\n// name macro\n#define pb push_back\n#define eb emplace_back\n#define SZ(x) ((int)(x).size())\n#define all(x) (x).begin(), (x).end()\n#define rall(x) (x).rbegin(), (x).rend()\n#define popcnt(x) __builtin_popcountll(x)\ntemplate <class T = int>\nusing V = std::vector<T>;\ntemplate <class T = int>\nusing VV = std::vector<std::vector<T>>;\ntemplate <class T>\nusing pqup = std::priority_queue<T, std::vector<T>, std::greater<T>>;\nusing ll = long long;\nusing ld = long double;\nusing int128 = __int128_t;\nusing pii = std::pair<int, int>;\nusing pll = std::pair<long long, long long>;\n\n// input macro\ntemplate <class T, class U>\nstd::istream &operator>>(std::istream &is, std::pair<T, U> &p) {\n\tis >> p.first >> p.second;\n\treturn is;\n}\ntemplate <class T>\nstd::istream &operator>>(std::istream &is, std::vector<T> &v) {\n\tfor(T &i : v) is >> i;\n\treturn is;\n}\nstd::istream &operator>>(std::istream &is, __int128_t &a) {\n\tstd::string s;\n\tis >> s;\n\t__int128_t ret = 0;\n\tfor(int i = 0; i < s.length(); i++)\n\t\tif('0' <= s[i] and s[i] <= '9')\n\t\t\tret = 10 * ret + s[i] - '0';\n\ta = ret * (s[0] == '-' ? -1 : 1);\n\treturn is;\n}\nnamespace scanner {\nvoid scan(int &a) { std::cin >> a; }\nvoid scan(long long &a) { std::cin >> a; }\nvoid scan(std::string &a) { std::cin >> a; }\nvoid scan(char &a) { std::cin >> a; }\nvoid scan(char a[]) { std::scanf(\"%s\", a); }\nvoid scan(double &a) { std::cin >> a; }\nvoid scan(long double &a) { std::cin >> a; }\ntemplate <class T, class U>\nvoid scan(std::pair<T, U> &p) { std::cin >> p; }\ntemplate <class T>\nvoid scan(std::vector<T> &a) { std::cin >> a; }\nvoid INPUT() {}\ntemplate <class Head, class... Tail>\nvoid INPUT(Head &head, Tail &... tail) {\n\tscan(head);\n\tINPUT(tail...);\n}\n} // namespace scanner\n#define VEC(type, name, size) \\\n\tstd::vector<type> name(size); \\\n\tscanner::INPUT(name)\n#define VVEC(type, name, h, w) \\\n\tstd::vector<std::vector<type>> name(h, std::vector<type>(w)); \\\n\tscanner::INPUT(name)\n#define INT(...) \\\n\tint __VA_ARGS__; \\\n\tscanner::INPUT(__VA_ARGS__)\n#define LL(...) \\\n\tlong long __VA_ARGS__; \\\n\tscanner::INPUT(__VA_ARGS__)\n#define STR(...) \\\n\tstd::string __VA_ARGS__; \\\n\tscanner::INPUT(__VA_ARGS__)\n#define CHAR(...) \\\n\tchar __VA_ARGS__; \\\n\tscanner::INPUT(__VA_ARGS__)\n#define DOUBLE(...) \\\n\tdouble __VA_ARGS__; \\\n\tscanner::INPUT(__VA_ARGS__)\n#define LD(...) \\\n\tlong double __VA_ARGS__; \\\n\tscanner::INPUT(__VA_ARGS__)\n\n// output-macro\ntemplate <class T, class U>\nstd::ostream &operator<<(std::ostream &os, const std::pair<T, U> &p) {\n\tos << p.first << \" \" << p.second;\n\treturn os;\n}\ntemplate <class T>\nstd::ostream &operator<<(std::ostream &os, const std::vector<T> &a) {\n\tfor(int i = 0; i < int(a.size()); ++i) {\n\t\tif(i) os << \" \";\n\t\tos << a[i];\n\t}\n\treturn os;\n}\nstd::ostream &operator<<(std::ostream &dest, __int128_t &value) {\n\tstd::ostream::sentry s(dest);\n\tif(s) {\n\t\t__uint128_t tmp = value < 0 ? -value : value;\n\t\tchar buffer[128];\n\t\tchar *d = std::end(buffer);\n\t\tdo {\n\t\t\t--d;\n\t\t\t*d = \"0123456789\"[tmp % 10];\n\t\t\ttmp /= 10;\n\t\t} while(tmp != 0);\n\t\tif(value < 0) {\n\t\t\t--d;\n\t\t\t*d = '-';\n\t\t}\n\t\tint len = std::end(buffer) - d;\n\t\tif(dest.rdbuf()->sputn(d, len) != len) {\n\t\t\tdest.setstate(std::ios_base::badbit);\n\t\t}\n\t}\n\treturn dest;\n}\ntemplate <class T>\nvoid print(const T a) { std::cout << a << '\\n'; }\ntemplate <class Head, class... Tail>\nvoid print(Head H, Tail... T) {\n\tstd::cout << H << ' ';\n\tprint(T...);\n}\ntemplate <class T>\nvoid printel(const T a) { std::cout << a << '\\n'; }\ntemplate <class T>\nvoid printel(const std::vector<T> &a) {\n\tfor(const auto &v : a)\n\t\tstd::cout << v << '\\n';\n}\ntemplate <class Head, class... Tail>\nvoid printel(Head H, Tail... T) {\n\tstd::cout << H << '\\n';\n\tprintel(T...);\n}\nvoid Yes(const bool b = true) { std::cout << (b ? \"Yes\\n\" : \"No\\n\"); }\nvoid No() { std::cout << \"No\\n\"; }\nvoid YES(const bool b = true) { std::cout << (b ? \"YES\\n\" : \"NO\\n\"); }\nvoid NO() { std::cout << \"NO\\n\"; }\nvoid err(const bool b = true) {\n\tif(b) {\n\t\tstd::cout << \"-1\\n\", exit(0);\n\t}\n}\n\n//debug macro\nnamespace debugger {\ntemplate <class T>\nvoid view(const std::vector<T> &a) {\n\tstd::cerr << \"{ \";\n\tfor(const auto &v : a) {\n\t\tstd::cerr << v << \", \";\n\t}\n\tstd::cerr << \"\\b\\b }\";\n}\ntemplate <class T>\nvoid view(const std::vector<std::vector<T>> &a) {\n\tstd::cerr << \"{\\n\";\n\tfor(const auto &v : a) {\n\t\tstd::cerr << \"\\t\";\n\t\tview(v);\n\t\tstd::cerr << \"\\n\";\n\t}\n\tstd::cerr << \"}\";\n}\ntemplate <class T, class U>\nvoid view(const std::vector<std::pair<T, U>> &a) {\n\tstd::cerr << \"{\\n\";\n\tfor(const auto &p : a) std::cerr << \"\\t(\" << p.first << \", \" << p.second << \")\\n\";\n\tstd::cerr << \"}\";\n}\ntemplate <class T, class U>\nvoid view(const std::map<T, U> &m) {\n\tstd::cerr << \"{\\n\";\n\tfor(const auto &p : m) std::cerr << \"\\t[\" << p.first << \"] : \" << p.second << \"\\n\";\n\tstd::cerr << \"}\";\n}\ntemplate <class T, class U>\nvoid view(const std::pair<T, U> &p) { std::cerr << \"(\" << p.first << \", \" << p.second << \")\"; }\ntemplate <class T>\nvoid view(const std::set<T> &s) {\n\tstd::cerr << \"{ \";\n\tfor(auto &v : s) {\n\t\tview(v);\n\t\tstd::cerr << \", \";\n\t}\n\tstd::cerr << \"\\b\\b }\";\n}\n\ntemplate <class T>\nvoid view(const T &e) { std::cerr << e; }\n} // namespace debugger\n#ifdef LOCAL\nvoid debug_out() {}\ntemplate <typename Head, typename... Tail>\nvoid debug_out(Head H, Tail... T) {\n\tdebugger::view(H);\n\tstd::cerr << \", \";\n\tdebug_out(T...);\n}\n#define debug(...) \\\n\tdo { \\\n\t\tstd::cerr << __LINE__ << \" [\" << #__VA_ARGS__ << \"] : [\"; \\\n\t\tdebug_out(__VA_ARGS__); \\\n\t\tstd::cerr << \"\\b\\b]\\n\"; \\\n\t} while(false)\n#else\n#define debug(...) (void(0))\n#endif\n\n// vector macro\ntemplate <class T>\nint lb(const std::vector<T> &a, const T x) { return std::distance((a).begin(), std::lower_bound((a).begin(), (a).end(), (x))); }\ntemplate <class T>\nint ub(const std::vector<T> &a, const T x) { return std::distance((a).begin(), std::upper_bound((a).begin(), (a).end(), (x))); }\ntemplate <class T>\nvoid UNIQUE(std::vector<T> &a) {\n\tstd::sort(a.begin(), a.end());\n\ta.erase(std::unique(a.begin(), a.end()), a.end());\n}\ntemplate <class T>\nstd::vector<T> press(std::vector<T> &a) {\n\tauto res = a;\n\tUNIQUE(res);\n\tfor(auto &v : a)\n\t\tv = lb(res, v);\n\treturn res;\n}\n#define SORTname(a, b, c, ...) c\n#define SORT(...) SORTname(__VA_ARGS__, SORT1, SORT0, ...)(__VA_ARGS__)\n#define SORT0(a) std::sort((a).begin(), (a).end())\n#define SORT1(a, c) std::sort((a).begin(), (a).end(), [](const auto x, const auto y) { return x c y; })\ntemplate <class T>\nvoid ADD(std::vector<T> &a, const T x = 1) {\n\tfor(auto &v : a) v += x;\n}\ntemplate <class T>\nvoid SUB(std::vector<T> &a, const T x = 1) {\n\tfor(auto &v : a) v -= x;\n}\ntemplate <class T>\nvoid MUL(std::vector<T> &a, const T x) {\n\tfor(auto &v : a) v *= x;\n}\ntemplate <class T>\nvoid DIV(std::vector<T> &a, const T x) {\n\tfor(auto &v : a) v /= x;\n}\nstd::vector<std::pair<char, int>> rle(const string &s) {\n\tint n = s.size();\n\tstd::vector<std::pair<char, int>> ret;\n\tfor(int l = 0; l < n;) {\n\t\tint r = l + 1;\n\t\tfor(; r < n and s[l] == s[r]; r++) {}\n\t\tret.emplace_back(s[l], r - l);\n\t\tl = r;\n\t}\n\treturn ret;\n}\ntemplate <class T>\nstd::vector<std::pair<T, int>> rle(const std::vector<T> &v) {\n\tint n = v.size();\n\tstd::vector<std::pair<T, int>> ret;\n\tfor(int l = 0; l < n;) {\n\t\tint r = l + 1;\n\t\tfor(; r < n and v[l] == v[r]; r++) {}\n\t\tret.emplace_back(v[l], r - l);\n\t\tl = r;\n\t}\n\treturn ret;\n}\n\n// math macro\ntemplate <class T, class U>\ninline bool chmin(T &a, const U &b) { return a > b ? a = b, true : false; }\ntemplate <class T, class U>\ninline bool chmax(T &a, const U &b) { return a < b ? a = b, true : false; }\ntemplate <class T>\nT divup(T x, T y) { return (x + y - 1) / y; }\ntemplate <class T>\nT POW(T a, long long n) {\n\tT ret = 1;\n\twhile(n) {\n\t\tif(n & 1) ret *= a;\n\t\ta *= a;\n\t\tn >>= 1;\n\t}\n\treturn ret;\n}\n// modpow\nlong long POW(long long a, long long n, const int mod) {\n\tlong long ret = 1;\n\ta = (a % mod + mod) % mod;\n\twhile(n) {\n\t\tif(n & 1) (ret *= a) %= mod;\n\t\t(a *= a) %= mod;\n\t\tn >>= 1;\n\t}\n\treturn ret;\n}\n\n// others\nstruct fast_io {\n\tfast_io() {\n\t\tios::sync_with_stdio(false);\n\t\tcin.tie(nullptr);\n\t\tcout << fixed << setprecision(15);\n\t}\n} fast_io_;\nconst int inf = 1e9;\nconst ll INF = 1e18;\n#pragma endregion\n\nvoid main_();\n\nint main() {\n\tmain_();\n\treturn 0;\n}\n#line 2 \"a.cpp\"\n\nvoid main_() {\n\twhile(1) {\n\t\tINT(n, m, k);\n\t\tif(!n) break;\n\t\tdouble res = 0;\n\t\tauto dfs = [&](auto dfs, int i, int s) {\n\t\t\tif(i == n) {\n\t\t\t\tres += max(1, s - k);\n\t\t\t\treturn;\n\t\t\t}\n\t\t\tREPS(j, m) {\n\t\t\t\tdfs(dfs, i + 1, s + j);\n\t\t\t}\n\t\t};\n\t\tdfs(dfs, 0, 0);\n\t\tres /= pow((double)m, n);\n\t\tprint(res);\n\t}\n}", "accuracy": 1, "time_ms": 80, "memory_kb": 3888, "score_of_the_acc": -0.1287, "final_rank": 14 }, { "submission_id": "aoj_1286_5751426", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\n#define REP(i,n) for(int i=0;i<(n);i++)\n#define ALL(v) v.begin(),v.end()\n\nusing ll=long long;\nusing ld=long double;\nld ans;\n\nvoid dfs(vector<int>&v,int m,int k){\n int cnt=0;\n for(int p:v)cnt+=p;\n ans+=max(1,cnt-k);\n int now=0;\n while(++v[now]>m){\n v[now++]=1;\n if(now>=v.size())return;\n }\n dfs(v,m,k);\n}\n\nvoid F(int n,int m,int k){\n ans=0;\n vector<int> v(n,1);\n dfs(v,m,k);\n ans/=pow(m,n);\n cout<<fixed<<setprecision(12)<<ans<<endl;\n}\n\n\nint main(){\n int n,m,k;\n while(cin>>n>>m>>k,n)F(n,m,k);\n}", "accuracy": 1, "time_ms": 100, "memory_kb": 3956, "score_of_the_acc": -0.159, "final_rank": 17 }, { "submission_id": "aoj_1286_5743397", "code_snippet": "#ifdef LOCAL\n#define _GLIBCXX_DEBUG\n#endif\n \n#include <bits/stdc++.h>\nusing namespace std;\n#if __has_include(<atcoder/all>)\n#include <atcoder/all>\nusing namespace atcoder;\n#endif\n \n#pragma region Macros\n// rep macro\n#define foa(v, a) for(auto &v : a)\n#define REPname(a, b, c, d, e, ...) e\n#define REP(...) REPname(__VA_ARGS__, REP3, REP2, REP1, REP0)(__VA_ARGS__)\n#define REP0(x) for(int i = 0; i < (x); ++i)\n#define REP1(i, x) for(int i = 0; i < (x); ++i)\n#define REP2(i, l, r) for(int i = (l); i < (r); ++i)\n#define REP3(i, l, r, c) for(int i = (l); i < (r); i += (c))\n#define REPSname(a, b, c, ...) c\n#define REPS(...) REPSname(__VA_ARGS__, REPS1, REPS0)(__VA_ARGS__)\n#define REPS0(x) for(int i = 1; i <= (x); ++i)\n#define REPS1(i, x) for(int i = 1; i <= (x); ++i)\n#define RREPname(a, b, c, d, e, ...) e\n#define RREP(...) RREPname(__VA_ARGS__, RREP3, RREP2, RREP1, RREP0)(__VA_ARGS__)\n#define RREP0(x) for(int i = (x)-1; i >= 0; --i)\n#define RREP1(i, x) for(int i = (x)-1; i >= 0; --i)\n#define RREP2(i, l, r) for(int i = (r)-1; i >= (l); --i)\n#define RREP3(i, l, r, c) for(int i = (r)-1; i >= (l); i -= (c))\n#define RREPSname(a, b, c, ...) c\n#define RREPS(...) RREPSname(__VA_ARGS__, RREPS1, RREPS0)(__VA_ARGS__)\n#define RREPS0(x) for(int i = (x); i >= 1; --i)\n#define RREPS1(i, x) for(int i = (x); i >= 1; --i)\n \n// name macro\n#define fi first\n#define se second\n#define pb push_back\n#define eb emplace_back\n#define SZ(x) ((int)(x).size())\n#define all(x) (x).begin(), (x).end()\n#define rall(x) (x).rbegin(), (x).rend()\n#define popcnt(x) __builtin_popcountll(x)\ntemplate <class T = int>\nusing V = std::vector<T>;\ntemplate <class T = int>\nusing VV = std::vector<std::vector<T>>;\ntemplate <class T = int>\nusing VVV = std::vector<std::vector<std::vector<T>>>;\ntemplate <class T>\nusing pqup = std::priority_queue<T, std::vector<T>, std::greater<T>>;\nusing ll = long long;\nusing ld = long double;\nusing int128 = __int128_t;\nusing pii = std::pair<int, int>;\nusing pll = std::pair<long long, long long>;\nusing Tp = tuple<ll,ll,ll>;\n \n// input macro\ntemplate <class T, class U>\nstd::istream &operator>>(std::istream &is, std::pair<T, U> &p) {\n\tis >> p.first >> p.second;\n\treturn is;\n}\ntemplate <class T>\nstd::istream &operator>>(std::istream &is, std::vector<T> &v) {\n\tfor(T &i : v) is >> i;\n\treturn is;\n}\nstd::istream &operator>>(std::istream &is, __int128_t &a) {\n\tstd::string s;\n\tis >> s;\n\t__int128_t ret = 0;\n\tfor(int i = 0; i < s.length(); i++)\n\t\tif('0' <= s[i] and s[i] <= '9')\n\t\t\tret = 10 * ret + s[i] - '0';\n\ta = ret * (s[0] == '-' ? -1 : 1);\n\treturn is;\n}\nnamespace scanner {\nvoid scan(int &a) { std::cin >> a; }\nvoid scan(long long &a) { std::cin >> a; }\nvoid scan(std::string &a) { std::cin >> a; }\nvoid scan(char &a) { std::cin >> a; }\nvoid scan(char a[]) { std::scanf(\"%s\", a); }\nvoid scan(double &a) { std::cin >> a; }\nvoid scan(long double &a) { std::cin >> a; }\ntemplate <class T, class U>\nvoid scan(std::pair<T, U> &p) { std::cin >> p; }\ntemplate <class T>\nvoid scan(std::vector<T> &a) { std::cin >> a; }\nvoid INPUT() {}\ntemplate <class Head, class... Tail>\nvoid INPUT(Head &head, Tail &... tail) {\n\tscan(head);\n\tINPUT(tail...);\n}\n} // namespace scanner\n#define VEC(type, name, size) \\\n\tstd::vector<type> name(size); \\\n\tscanner::INPUT(name)\n#define VVEC(type, name, h, w) \\\n\tstd::vector<std::vector<type>> name(h, std::vector<type>(w)); \\\n\tscanner::INPUT(name)\n#define INT(...) \\\n\tint __VA_ARGS__; \\\n\tscanner::INPUT(__VA_ARGS__)\n#define LL(...) \\\n\tlong long __VA_ARGS__; \\\n\tscanner::INPUT(__VA_ARGS__)\n#define STR(...) \\\n\tstd::string __VA_ARGS__; \\\n\tscanner::INPUT(__VA_ARGS__)\n#define CHAR(...) \\\n\tchar __VA_ARGS__; \\\n\tscanner::INPUT(__VA_ARGS__)\n#define DOUBLE(...) \\\n\tdouble __VA_ARGS__; \\\n\tscanner::INPUT(__VA_ARGS__)\n#define LD(...) \\\n\tlong double __VA_ARGS__; \\\n\tscanner::INPUT(__VA_ARGS__)\n \n// output-macro\ntemplate <class T, class U>\nstd::ostream &operator<<(std::ostream &os, const std::pair<T, U> &p) {\n\tos << p.first << \" \" << p.second;\n\treturn os;\n}\ntemplate <class T>\nstd::ostream &operator<<(std::ostream &os, const std::vector<T> &a) {\n\tfor(int i = 0; i < int(a.size()); ++i) {\n\t\tif(i) os << \" \";\n\t\tos << a[i];\n\t}\n\treturn os;\n}\nstd::ostream &operator<<(std::ostream &dest, __int128_t &value) {\n\tstd::ostream::sentry s(dest);\n\tif(s) {\n\t\t__uint128_t tmp = value < 0 ? -value : value;\n\t\tchar buffer[128];\n\t\tchar *d = std::end(buffer);\n\t\tdo {\n\t\t\t--d;\n\t\t\t*d = \"0123456789\"[tmp % 10];\n\t\t\ttmp /= 10;\n\t\t} while(tmp != 0);\n\t\tif(value < 0) {\n\t\t\t--d;\n\t\t\t*d = '-';\n\t\t}\n\t\tint len = std::end(buffer) - d;\n\t\tif(dest.rdbuf()->sputn(d, len) != len) {\n\t\t\tdest.setstate(std::ios_base::badbit);\n\t\t}\n\t}\n\treturn dest;\n}\ntemplate <class T>\nvoid print(const T a) { std::cout << a << '\\n'; }\ntemplate <class Head, class... Tail>\nvoid print(Head H, Tail... T) {\n\tstd::cout << H << ' ';\n\tprint(T...);\n}\ntemplate <class T>\nvoid printel(const T a) { std::cout << a << '\\n'; }\ntemplate <class T>\nvoid printel(const std::vector<T> &a) {\n\tfor(const auto &v : a)\n\t\tstd::cout << v << '\\n';\n}\ntemplate <class Head, class... Tail>\nvoid printel(Head H, Tail... T) {\n\tstd::cout << H << '\\n';\n\tprintel(T...);\n}\nvoid Yes(const bool b = true) { std::cout << (b ? \"Yes\\n\" : \"No\\n\"); }\nvoid No() { std::cout << \"No\\n\"; }\nvoid YES(const bool b = true) { std::cout << (b ? \"YES\\n\" : \"NO\\n\"); }\nvoid NO() { std::cout << \"No\\n\"; }\nvoid err(const bool b = true) {\n\tif(b) {\n\t\tstd::cout << \"-1\\n\", exit(0);\n\t}\n}\n \n//debug macro\nnamespace debugger {\ntemplate <class T>\nvoid view(const std::vector<T> &a) {\n\tstd::cerr << \"{ \";\n\tfor(const auto &v : a) {\n\t\tstd::cerr << v << \", \";\n\t}\n\tstd::cerr << \"\\b\\b }\";\n}\ntemplate <class T>\nvoid view(const std::vector<std::vector<T>> &a) {\n\tstd::cerr << \"{\\n\";\n\tfor(const auto &v : a) {\n\t\tstd::cerr << \"\\t\";\n\t\tview(v);\n\t\tstd::cerr << \"\\n\";\n\t}\n\tstd::cerr << \"}\";\n}\ntemplate <class T, class U>\nvoid view(const std::vector<std::pair<T, U>> &a) {\n\tstd::cerr << \"{\\n\";\n\tfor(const auto &p : a) std::cerr << \"\\t(\" << p.first << \", \" << p.second << \")\\n\";\n\tstd::cerr << \"}\";\n}\ntemplate <class T, class U>\nvoid view(const std::map<T, U> &m) {\n\tstd::cerr << \"{\\n\";\n\tfor(const auto &p : m) std::cerr << \"\\t[\" << p.first << \"] : \" << p.second << \"\\n\";\n\tstd::cerr << \"}\";\n}\ntemplate <class T, class U>\nvoid view(const std::pair<T, U> &p) { std::cerr << \"(\" << p.first << \", \" << p.second << \")\"; }\ntemplate <class T>\nvoid view(const std::set<T> &s) {\n\tstd::cerr << \"{ \";\n\tfor(auto &v : s) {\n\t\tview(v);\n\t\tstd::cerr << \", \";\n\t}\n\tstd::cerr << \"\\b\\b }\";\n}\n \ntemplate <class T>\nvoid view(const T &e) { std::cerr << e; }\n} // namespace debugger\n#ifdef LOCAL\nvoid debug_out() {}\ntemplate <typename Head, typename... Tail>\nvoid debug_out(Head H, Tail... T) {\n\tdebugger::view(H);\n\tstd::cerr << \", \";\n\tdebug_out(T...);\n}\n#define debug(...) \\\n\tdo { \\\n\t\tstd::cerr << __LINE__ << \" [\" << #__VA_ARGS__ << \"] : [\"; \\\n\t\tdebug_out(__VA_ARGS__); \\\n\t\tstd::cerr << \"\\b\\b]\\n\"; \\\n\t} while(false)\n#else\n#define debug(...) (void(0))\n#endif\n \n// vector macro\ntemplate <class T>\nint lb(const std::vector<T> &a, const T x) { return std::distance((a).begin(), std::lower_bound((a).begin(), (a).end(), (x))); }\ntemplate <class T>\nint ub(const std::vector<T> &a, const T x) { return std::distance((a).begin(), std::upper_bound((a).begin(), (a).end(), (x))); }\ntemplate <class T>\nvoid UNIQUE(std::vector<T> &a) {\n\tstd::sort(a.begin(), a.end());\n\ta.erase(std::unique(a.begin(), a.end()), a.end());\n}\ntemplate <class T>\nstd::vector<T> press(std::vector<T> &a) {\n\tauto res = a;\n\tUNIQUE(res);\n\tfor(auto &v : a)\n\t\tv = lb(res, v);\n\treturn res;\n}\n#define SORTname(a, b, c, ...) c\n#define SORT(...) SORTname(__VA_ARGS__, SORT1, SORT0, ...)(__VA_ARGS__)\n#define SORT0(a) std::sort((a).begin(), (a).end())\n#define SORT1(a, c) std::sort((a).begin(), (a).end(), [](const auto x, const auto y) { return x c y; })\ntemplate <class T>\nvoid ADD(std::vector<T> &a, const T x) {\n\tfor(auto &v : a) v += x;\n}\ntemplate <class T>\nvoid SUB(std::vector<T> &a, const T x = 1) {\n\tfor(auto &v : a) v -= x;\n}\ntemplate <class T>\nvoid MUL(std::vector<T> &a, const T x) {\n\tfor(auto &v : a) v *= x;\n}\ntemplate <class T>\nvoid DIV(std::vector<T> &a, const T x) {\n\tfor(auto &v : a) v /= x;\n}\n \n// math macro\ntemplate <class T, class U>\ninline bool chmin(T &a, const U &b) { return a > b ? a = b, true : false; }\ntemplate <class T, class U>\ninline bool chmax(T &a, const U &b) { return a < b ? a = b, true : false; }\ntemplate <class T>\nT divup(T x, T y) { return (x + y - 1) / y; }\ntemplate <class T>\nT POW(T a, long long n) {\n\tT ret = 1;\n\twhile(n) {\n\t\tif(n & 1) ret *= a;\n\t\ta *= a;\n\t\tn >>= 1;\n\t}\n\treturn ret;\n}\n\ntemplate<typename T>\nT ADD(T a, T b){\n\tT res;\n\treturn __builtin_add_overflow(a, b, &res) ? numeric_limits<T>::max() : res;\n}\n\ntemplate<typename T>\nT MUL(T a, T b){\n\tT res;\n\treturn __builtin_mul_overflow(a, b, &res) ? numeric_limits<T>::max() : res;\n}\n\n// modpow\nlong long POW(long long a, long long n, const int mod) {\n\tlong long ret = 1;\n\twhile(n) {\n\t\tif(n & 1) (ret *= a) %= mod;\n\t\t(a *= a) %= mod;\n\t\tn >>= 1;\n\t}\n\treturn ret;\n}\n \n// others\nstruct fast_io {\n\tfast_io() {\n\t\tios::sync_with_stdio(false);\n\t\tcin.tie(nullptr);\n\t\tcout << fixed << setprecision(15);\n\t}\n} fast_io_;\n\n#pragma endregion\n\nusing R = long double;\nconstexpr R EPS=1E-11;\n\n// r の(誤差付きの)符号に従って, -1, 0, 1 を返す.\nint sgn(const R& r){ return (r > EPS) - (r < -EPS); }\n// a, b の(誤差付きの)大小比較の結果に従って, -1, 0, 1 を返す.\nint sgn(const R& a, const R &b){ return sgn(a-b); }\n// a > 0 は sgn(a) > 0\n// a < b は sgn(a, b) < 0\n// a >= b は sgn(a, b) >= 0\n// のように書く.\n// return s * 10^n\n\n//https://atcoder.jp/contests/abc191/submissions/20028529\nlong long x10(const string& s, size_t n) {\n if (s.front() == '-') return -x10(s.substr(1), n);\n auto pos = s.find('.');\n if (pos == string::npos) return stoll(s + string(n, '0'));\n return stoll(s.substr(0, pos) + s.substr(pos + 1) + string(n + pos + 1 - s.size(), '0'));\n}\n \nlong long ceildiv(long long a, long long b) {\n if (b < 0) a = -a, b = -b;\n if (a >= 0) return (a + b - 1) / b;\n else return a / b;\n}\n \nlong long floordiv(long long a, long long b) {\n if (b < 0) a = -a, b = -b;\n if (a >= 0) return a / b;\n else return (a - b + 1) / b;\n}\n \nlong long floorsqrt(long long x) {\n assert(x >= 0);\n long long ok = 0;\n long long ng = 1;\n while (ng * ng <= x) ng <<= 1;\n while (ng - ok > 1) {\n long long mid = (ng + ok) >> 1;\n if (mid * mid <= x) ok = mid;\n else ng = mid;\n }\n return ok;\n}\n\ninline int topbit(unsigned long long x){\n\treturn x?63-__builtin_clzll(x):-1;\n}\n\ninline int popcount(unsigned long long x){\n\treturn __builtin_popcountll(x);\n}\n\ninline int parity(unsigned long long x){//popcount%2\n\treturn __builtin_parity(x);\n}\n\nconst int inf = 1e9;\nconst ll INF = 1e18;\n\nint n,m,k;\n\nint sum = 0;\n\nvoid dfs(int cnt,int tot){\n if(cnt == n){\n if(tot <= k)sum += 1;\n else sum += tot - k;\n return ;\n }\n for(int i = 1;i <= m;i++){\n dfs(cnt+1,tot+i);\n }\n return ;\n}\n\nvoid main_() {\n\t\n\tcin >> n >> m >> k;\n\t\n\t\n\twhile(n != 0){\n\t int all = 1;\n\t REP(i,n)all *= m;\n\t dfs(0,0);\n\t double ans = (double)sum/all;\n\t sum = 0;\n\t cout << setprecision(10) << fixed;\n\t cout << ans << endl;\n\t cin >> n >> m >> k;\n\t}\n}\n \nint main() {\n\tint t = 1;\n\t//cin >> t;\n\twhile(t--) main_();\n\treturn 0;\n}", "accuracy": 1, "time_ms": 50, "memory_kb": 3420, "score_of_the_acc": -0.0619, "final_rank": 6 }, { "submission_id": "aoj_1286_5289827", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n#define int long long\n#define rep(i,n) for(int (i)=0;(i)<(n);(i)++)\n#define rrep(i,n) for(int (i)=((n)-1);(i)>=0;(i)--)\n#define itn int\n#define miele(v) min_element(v.begin(), v.end())\n#define maele(v) max_element(v.begin(), v.end())\n#define SUM(v) accumulate(v.begin(), v.end(), 0LL)\n#define lb(a, key) lower_bound(a.begin(),a.end(),key)\n#define ub(a, key) upper_bound(a.begin(),a.end(),key)\n#define COUNT(a, key) count(a.begin(), a.end(), key) \n#define BITCOUNT(x) __builtin_popcount(x)\n#define pb push_back\n#define all(x) (x).begin(),(x).end()\n#define F first\n#define S second\nusing P = pair <int,int>;\nusing WeightedGraph = vector<vector <P>>;\nusing UnWeightedGraph = vector<vector<int>>;\nusing Real = long double;\nusing Point = complex<Real>; //Point and Vector2d is the same!\nusing Vector2d = complex<Real>;\nconst long long INF = 1LL << 60;\nconst int MOD = 1000000007;\nconst double EPS = 1e-15;\nconst double PI=3.14159265358979323846;\ntemplate <typename T> \nint getIndexOfLowerBound(vector <T> &v, T x){\n return lower_bound(v.begin(),v.end(),x)-v.begin();\n}\ntemplate <typename T> \nint getIndexOfUpperBound(vector <T> &v, T x){\n return upper_bound(v.begin(),v.end(),x)-v.begin();\n}\ntemplate<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; }\ntemplate<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; }\n#define DUMPOUT cerr\n#define repi(itr, ds) for (auto itr = ds.begin(); itr != ds.end(); itr++)\nistream &operator>>(istream &is, Point &p) {\n Real a, b; is >> a >> b; p = Point(a, b); return is;\n}\ntemplate <typename T, typename U>\nistream &operator>>(istream &is, pair<T,U> &p_var) {\n is >> p_var.first >> p_var.second;\n return is;\n}\ntemplate <typename T>\nistream &operator>>(istream &is, vector<T> &vec) {\n for (T &x : vec) is >> x;\n return is;\n}\ntemplate <typename T, typename U>\nostream &operator<<(ostream &os, pair<T, U> &pair_var) {\n DUMPOUT<<'{';\n os << pair_var.first;\n DUMPOUT<<',';\n os << \" \"<< pair_var.second;\n DUMPOUT<<'}';\n return os;\n}\ntemplate <typename T>\nostream &operator<<(ostream &os, vector<T> &vec) {\n DUMPOUT<<'[';\n for (int i = 0; i < vec.size(); i++) \n os << vec[i] << (i + 1 == vec.size() ? \"\" : \" \");\n DUMPOUT<<']';\n return os;\n}\ntemplate <typename T>\nostream &operator<<(ostream &os, vector<vector<T>> &df) {\n for (auto& vec : df) os<<vec;\n return os;\n}\ntemplate <typename T, typename U>\nostream &operator<<(ostream &os, map<T, U> &map_var) {\n DUMPOUT << \"{\";\n repi(itr, map_var) {\n os << *itr;\n itr++;\n if (itr != map_var.end()) DUMPOUT << \", \";\n itr--;\n }\n DUMPOUT << \"}\";\n return os;\n}\ntemplate <typename T>\nostream &operator<<(ostream &os, set<T> &set_var) {\n DUMPOUT << \"{\";\n repi(itr, set_var) {\n os << *itr;\n itr++;\n if (itr != set_var.end()) DUMPOUT << \", \";\n itr--;\n }\n DUMPOUT << \"}\";\n return os;\n}\nvoid print() {cout << endl;}\ntemplate <class Head, class... Tail>\nvoid print(Head&& head, Tail&&... tail) {\n cout << head;\n if (sizeof...(tail) != 0) cout << \" \";\n print(forward<Tail>(tail)...);\n}\nvoid dump_func() {DUMPOUT << '#'<<endl;}\ntemplate <typename Head, typename... Tail>\nvoid dump_func(Head &&head, Tail &&... tail) {\n DUMPOUT << head;\n if (sizeof...(Tail) > 0) DUMPOUT << \", \";\n dump_func(std::move(tail)...);\n}\n#ifdef DEBUG_\n#define DEB\n#define dump(...) \\\n DUMPOUT << \" \" << string(#__VA_ARGS__) << \": \" \\\n << \"[\" << to_string(__LINE__) << \":\" << __FUNCTION__ << \"]\" \\\n << endl \\\n << \" \", \\\n dump_func(__VA_ARGS__)\n#else\n#define DEB if (false)\n#define dump(...)\n#endif\n\nlong double ans = 0;\nlong double n, m, k;\n\nvoid dfs(long double i, long double possibility, long double num) {\n if(i == 0) {\n ans += max(num - k, (long double)1.0) * possibility;\n }else {\n for(int j=1;j<=m;j++){\n dfs(i-1, possibility/m, num + j);\n }\n }\n}\n\nsigned main(void) { cin.tie(0); ios::sync_with_stdio(false);\n int t = 0;\n while(1) {\n cin>>n>>m>>k;\n\n if(n == 0) exit(0);\n ans = 0;\n dfs(n, 1, 0);\n\n printf(\"%.10Lf\\n\", ans);\n }\n}", "accuracy": 1, "time_ms": 290, "memory_kb": 3488, "score_of_the_acc": -0.3817, "final_rank": 18 }, { "submission_id": "aoj_1286_5248850", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n//#include <atcoder/all>\n//using namespace atcoder;\n\nusing ll = long long;\nusing ld = long double;\nusing pll = pair<ll, ll>;\nusing vll = vector<ll>;\nusing vvll = vector<vector<ll>>;\nusing vvvll = vector<vector<vector<ll>>>;\nusing vpll = vector<pair<ll, ll>>;\n#define rep(i,n) for (ll i = 0; i < (n); i++)\n#define all(v) (v).begin(), (v).end()\n#define sz(x) ((int)(x).size())\n#define fi first\n#define se second\n#define pb push_back\n#define endl '\\n'\n#define IOS ios::sync_with_stdio(false); cin.tie(nullptr);\nconst int inf = 1<<30;\nconst ll INF = 1ll<<60;\n//const ll mod = 1000000007;\nconst ll mod = 998244353;\nconst ll dx[4] = {1, 0, -1, 0};\nconst ll dy[4] = {0, 1, 0, -1};\ntemplate<class T> bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }\ntemplate<class T> bool chmin(T &a, const T &b) { if (a>b) { a=b; return 1; } return 0; }\n\ntemplate<class T> void disp1(vector<T> &array, bool blank=1) {\n rep(i, array.size()) cout << ((i&&blank) ? \" \" : \"\") << array[i];\n cout << endl;\n //cout << \" \";\n}\ntemplate<class T> void disp2(vector<vector<T>> &array, bool blank=1) {\n rep(i, array.size()) disp1(array[i], blank);\n}\n\n//using mint = modint998244353;\n\nll n, m, k, ans;\n\nvoid dfs(ll s, ll c) {\n // s = 今までの出た目の合計\n // c = 今までに振ったサイコロの個数\n if (c == n) {\n ans += max(1ll, s-k);\n return;\n }\n for (int i = 1; i <= m; i++) {\n dfs(s+i, c+1);\n }\n return;\n}\n\nint main() {\n while (cin >> n >> m >> k, n|m|k) {\n ans = 0;\n dfs(0, 0);\n ll mn = 1;\n rep(i,n) mn *= m;\n ld Ans = (ld) ans / mn;\n printf(\"%.10Lf\\n\", Ans);\n }\n return 0;\n}", "accuracy": 1, "time_ms": 50, "memory_kb": 3036, "score_of_the_acc": -0.0395, "final_rank": 2 }, { "submission_id": "aoj_1286_5229319", "code_snippet": "#line 2 \"/home/yuruhiya/programming/library/template/template.cpp\"\n#include <bits/stdc++.h>\n#line 6 \"/home/yuruhiya/programming/library/template/constants.cpp\"\n\n#define rep(i, n) for (int i = 0; i < (n); ++i)\n#define FOR(i, m, n) for (int i = (m); i < (n); ++i)\n#define rrep(i, n) for (int i = (n) - 1; i >= 0; --i)\n#define rfor(i, m, n) for (int i = (m); i >= (n); --i)\n#define unless(c) if (!(c))\n#define all(x) (x).begin(), (x).end()\n#define rall(x) (x).rbegin(), (x).rend()\n#define range_it(a, l, r) (a).begin() + (l), (a).begin() + (r)\n\nusing namespace std;\nusing ll = long long;\nusing LD = long double;\nusing VB = vector<bool>;\nusing VVB = vector<VB>;\nusing VI = vector<int>;\nusing VVI = vector<VI>;\nusing VL = vector<ll>;\nusing VVL = vector<VL>;\nusing VS = vector<string>;\nusing VD = vector<LD>;\nusing PII = pair<int, int>;\nusing VP = vector<PII>;\nusing PLL = pair<ll, ll>;\nusing VPL = vector<PLL>;\ntemplate <class T> using PQ = priority_queue<T>;\ntemplate <class T> using PQS = priority_queue<T, vector<T>, greater<T>>;\nconstexpr int inf = 1000000000;\nconstexpr long long inf_ll = 1000000000000000000ll, MOD = 1000000007;\nconstexpr long double PI = 3.14159265358979323846, EPS = 1e-12;\n#line 7 \"/home/yuruhiya/programming/library/template/Input.cpp\"\nusing namespace std;\n\n#ifdef _WIN32\n#define getchar_unlocked _getchar_nolock\n#define putchar_unlocked _putchar_nolock\n#define fwrite_unlocked fwrite\n#define fflush_unlocked fflush\n#endif\nclass Scanner {\n\tstatic int gc() {\n\t\treturn getchar_unlocked();\n\t}\n\tstatic char next_char() {\n\t\tchar c;\n\t\tread(c);\n\t\treturn c;\n\t}\n\ttemplate <class T> static void read(T& v) {\n\t\tcin >> v;\n\t}\n\tstatic void read(char& v) {\n\t\twhile (isspace(v = gc()))\n\t\t\t;\n\t}\n\tstatic void read(bool& v) {\n\t\tv = next_char() != '0';\n\t}\n\tstatic void read(string& v) {\n\t\tv.clear();\n\t\tfor (char c = next_char(); !isspace(c); c = gc()) v += c;\n\t}\n\tstatic void read(int& v) {\n\t\tv = 0;\n\t\tbool neg = false;\n\t\tchar c = next_char();\n\t\tif (c == '-') {\n\t\t\tneg = true;\n\t\t\tc = gc();\n\t\t}\n\t\tfor (; isdigit(c); c = gc()) v = v * 10 + (c - '0');\n\t\tif (neg) v = -v;\n\t}\n\tstatic void read(long long& v) {\n\t\tv = 0;\n\t\tbool neg = false;\n\t\tchar c = next_char();\n\t\tif (c == '-') {\n\t\t\tneg = true;\n\t\t\tc = gc();\n\t\t}\n\t\tfor (; isdigit(c); c = gc()) v = v * 10 + (c - '0');\n\t\tif (neg) v = -v;\n\t}\n\tstatic void read(double& v) {\n\t\tv = 0;\n\t\tdouble dp = 1;\n\t\tbool neg = false, after_dp = false;\n\t\tchar c = next_char();\n\t\tif (c == '-') {\n\t\t\tneg = true;\n\t\t\tc = gc();\n\t\t}\n\t\tfor (; isdigit(c) || c == '.'; c = gc()) {\n\t\t\tif (c == '.') {\n\t\t\t\tafter_dp = true;\n\t\t\t} else if (after_dp) {\n\t\t\t\tv += (c - '0') * (dp *= 0.1);\n\t\t\t} else {\n\t\t\t\tv = v * 10 + (c - '0');\n\t\t\t}\n\t\t}\n\t\tif (neg) v = -v;\n\t}\n\tstatic void read(long double& v) {\n\t\tv = 0;\n\t\tlong double dp = 1;\n\t\tbool neg = false, after_dp = false;\n\t\tchar c = next_char();\n\t\tif (c == '-') {\n\t\t\tneg = true;\n\t\t\tc = gc();\n\t\t}\n\t\tfor (; isdigit(c) || c == '.'; c = gc()) {\n\t\t\tif (c == '.') {\n\t\t\t\tafter_dp = true;\n\t\t\t} else if (after_dp) {\n\t\t\t\tv += (c - '0') * (dp *= 0.1);\n\t\t\t} else {\n\t\t\t\tv = v * 10 + (c - '0');\n\t\t\t}\n\t\t}\n\t\tif (neg) v = -v;\n\t}\n\ttemplate <class T, class U> static void read(pair<T, U>& v) {\n\t\tread(v.first);\n\t\tread(v.second);\n\t}\n\ttemplate <class T> static void read(vector<T>& v) {\n\t\tfor (auto& e : v) read(e);\n\t}\n\ttemplate <size_t N = 0, class T> static void read_tuple_impl(T& v) {\n\t\tif constexpr (N < tuple_size_v<T>) {\n\t\t\tread(get<N>(v));\n\t\t\tread_tuple_impl<N + 1>(v);\n\t\t}\n\t}\n\ttemplate <class... T> static void read(tuple<T...>& v) {\n\t\tread_tuple_impl(v);\n\t}\n\tstruct ReadVectorHelper {\n\t\tsize_t n;\n\t\tReadVectorHelper(size_t _n) : n(_n) {}\n\t\ttemplate <class T> operator vector<T>() {\n\t\t\tvector<T> v(n);\n\t\t\tread(v);\n\t\t\treturn v;\n\t\t}\n\t};\n\tstruct Read2DVectorHelper {\n\t\tsize_t n, m;\n\t\tRead2DVectorHelper(const pair<size_t, size_t>& nm) : n(nm.first), m(nm.second) {}\n\t\ttemplate <class T> operator vector<vector<T>>() {\n\t\t\tvector<vector<T>> v(n, vector<T>(m));\n\t\t\tread(v);\n\t\t\treturn v;\n\t\t}\n\t};\n\npublic:\n\tstring read_line() const {\n\t\tstring v;\n\t\tfor (char c = gc(); c != '\\n' && c != '\\0'; c = gc()) v += c;\n\t\treturn v;\n\t}\n\ttemplate <class T> T read() const {\n\t\tT v;\n\t\tread(v);\n\t\treturn v;\n\t}\n\ttemplate <class T> vector<T> read_vector(size_t n) const {\n\t\tvector<T> a(n);\n\t\tread(a);\n\t\treturn a;\n\t}\n\ttemplate <class T> operator T() const {\n\t\treturn read<T>();\n\t}\n\tint operator--(int) const {\n\t\treturn read<int>() - 1;\n\t}\n\tReadVectorHelper operator[](size_t n) const {\n\t\treturn ReadVectorHelper(n);\n\t}\n\tRead2DVectorHelper operator[](const pair<size_t, size_t>& nm) const {\n\t\treturn Read2DVectorHelper(nm);\n\t}\n\tvoid operator()() const {}\n\ttemplate <class H, class... T> void operator()(H&& h, T&&... t) const {\n\t\tread(h);\n\t\toperator()(forward<T>(t)...);\n\t}\n\nprivate:\n\ttemplate <template <class...> class, class...> struct Multiple;\n\ttemplate <template <class...> class V, class Head, class... Tail>\n\tstruct Multiple<V, Head, Tail...> {\n\t\ttemplate <class... Args> using vec = V<vector<Head>, Args...>;\n\t\tusing type = typename Multiple<vec, Tail...>::type;\n\t};\n\ttemplate <template <class...> class V> struct Multiple<V> { using type = V<>; };\n\ttemplate <class... T> using multiple_t = typename Multiple<tuple, T...>::type;\n\ttemplate <size_t N = 0, class T> void multiple_impl(T& t) const {\n\t\tif constexpr (N < tuple_size_v<T>) {\n\t\t\tauto& vec = get<N>(t);\n\t\t\tusing V = typename remove_reference_t<decltype(vec)>::value_type;\n\t\t\tvec.push_back(read<V>());\n\t\t\tmultiple_impl<N + 1>(t);\n\t\t}\n\t}\n\npublic:\n\ttemplate <class... T> auto multiple(size_t h) const {\n\t\tmultiple_t<T...> result;\n\t\twhile (h--) multiple_impl(result);\n\t\treturn result;\n\t}\n} in;\n#define inputs(T, ...) \\\n\tT __VA_ARGS__; \\\n\tin(__VA_ARGS__)\n#define ini(...) inputs(int, __VA_ARGS__)\n#define inl(...) inputs(long long, __VA_ARGS__)\n#define ins(...) inputs(string, __VA_ARGS__)\n#line 7 \"/home/yuruhiya/programming/library/template/Output.cpp\"\n#include <charconv>\n#line 10 \"/home/yuruhiya/programming/library/template/Output.cpp\"\nusing namespace std;\n\nstruct BoolStr {\n\tconst char *t, *f;\n\tBoolStr(const char* _t, const char* _f) : t(_t), f(_f) {}\n} Yes(\"Yes\", \"No\"), yes(\"yes\", \"no\"), YES(\"YES\", \"NO\"), Int(\"1\", \"0\");\nstruct DivStr {\n\tconst char *d, *l;\n\tDivStr(const char* _d, const char* _l) : d(_d), l(_l) {}\n} spc(\" \", \"\\n\"), no_spc(\"\", \"\\n\"), end_line(\"\\n\", \"\\n\"), comma(\",\", \"\\n\"), no_endl(\" \", \"\");\nclass Printer {\n\tBoolStr B{Yes};\n\tDivStr D{spc};\n\npublic:\n\tvoid print(int v) const {\n\t\tchar buf[12]{};\n\t\tif (auto [ptr, e] = to_chars(begin(buf), end(buf), v); e == errc{}) {\n\t\t\tfwrite(buf, sizeof(char), ptr - buf, stdout);\n\t\t} else {\n\t\t\tassert(false);\n\t\t}\n\t}\n\tvoid print(long long v) const {\n\t\tchar buf[21]{};\n\t\tif (auto [ptr, e] = to_chars(begin(buf), end(buf), v); e == errc{}) {\n\t\t\tfwrite(buf, sizeof(char), ptr - buf, stdout);\n\t\t} else {\n\t\t\tassert(false);\n\t\t}\n\t}\n\tvoid print(bool v) const {\n\t\tprint(v ? B.t : B.f);\n\t}\n\tvoid print(vector<bool>::reference v) const {\n\t\tprint(v ? B.t : B.f);\n\t}\n\tvoid print(char v) const {\n\t\tputchar_unlocked(v);\n\t}\n\tvoid print(const char* v) const {\n\t\tfwrite_unlocked(v, 1, strlen(v), stdout);\n\t}\n\tvoid print(double v) const {\n\t\tprintf(\"%.20f\", v);\n\t}\n\tvoid print(long double v) const {\n\t\tprintf(\"%.20Lf\", v);\n\t}\n\ttemplate <class T> void print(const T& v) const {\n\t\tcout << v;\n\t}\n\ttemplate <class T, class U> void print(const pair<T, U>& v) const {\n\t\tprint(v.first);\n\t\tprint(D.d);\n\t\tprint(v.second);\n\t}\n\ttemplate <class InputIterater>\n\tvoid print_range(const InputIterater& begin, const InputIterater& end) const {\n\t\tfor (InputIterater i = begin; i != end; ++i) {\n\t\t\tif (i != begin) print(D.d);\n\t\t\tprint(*i);\n\t\t}\n\t}\n\ttemplate <class T> void print(const vector<T>& v) const {\n\t\tprint_range(v.begin(), v.end());\n\t}\n\ttemplate <class T, size_t N> void print(const array<T, N>& v) const {\n\t\tprint_range(v.begin(), v.end());\n\t}\n\ttemplate <class T> void print(const vector<vector<T>>& v) const {\n\t\tfor (size_t i = 0; i < v.size(); ++i) {\n\t\t\tif (i) print(D.l);\n\t\t\tprint(v[i]);\n\t\t}\n\t}\n\n\tPrinter() = default;\n\tPrinter(const BoolStr& _boolstr, const DivStr& _divstr) : B(_boolstr), D(_divstr) {}\n\tPrinter& operator()() {\n\t\tprint(D.l);\n\t\treturn *this;\n\t}\n\ttemplate <class Head> Printer& operator()(Head&& h) {\n\t\tprint(h);\n\t\tprint(D.l);\n\t\treturn *this;\n\t}\n\ttemplate <class Head, class... Tail> Printer& operator()(Head&& h, Tail&&... t) {\n\t\tprint(h);\n\t\tprint(D.d);\n\t\treturn operator()(forward<Tail>(t)...);\n\t}\n\ttemplate <class... Args> Printer& flag(bool f, Args&&... args) {\n\t\tif (f) {\n\t\t\treturn operator()(forward<Args>(args)...);\n\t\t} else {\n\t\t\treturn *this;\n\t\t}\n\t}\n\ttemplate <class InputIterator>\n\tPrinter& range(const InputIterator& begin, const InputIterator& end) {\n\t\tprint_range(begin, end);\n\t\tprint(D.l);\n\t\treturn *this;\n\t}\n\ttemplate <class T> Printer& range(const T& a) {\n\t\trange(a.begin(), a.end());\n\t\treturn *this;\n\t}\n\ttemplate <class... T> void exit(T&&... t) {\n\t\toperator()(forward<T>(t)...);\n\t\tstd::exit(EXIT_SUCCESS);\n\t}\n\tPrinter& flush() {\n\t\tfflush_unlocked(stdout);\n\t\treturn *this;\n\t}\n\tPrinter& set(const BoolStr& b) {\n\t\tB = b;\n\t\treturn *this;\n\t}\n\tPrinter& set(const DivStr& d) {\n\t\tD = d;\n\t\treturn *this;\n\t}\n\tPrinter& set(const char* t, const char* f) {\n\t\tB = BoolStr(t, f);\n\t\treturn *this;\n\t}\n} out;\n#line 3 \"/home/yuruhiya/programming/library/template/Step.cpp\"\nusing namespace std;\n\ntemplate <class T> struct Step {\n\tusing value_type = T;\n\n\tclass iterator {\n\t\tvalue_type a, b, c;\n\n\tpublic:\n\t\tconstexpr iterator() : a(value_type()), b(value_type()), c(value_type()) {}\n\t\tconstexpr iterator(value_type _b, value_type _c, value_type _s)\n\t\t : a(_b), b(_c), c(_s) {}\n\t\tconstexpr iterator& operator++() {\n\t\t\t--b;\n\t\t\ta += c;\n\t\t\treturn *this;\n\t\t}\n\t\tconstexpr iterator operator++(int) {\n\t\t\titerator tmp = *this;\n\t\t\t--b;\n\t\t\ta += c;\n\t\t\treturn tmp;\n\t\t}\n\t\tconstexpr const value_type& operator*() const {\n\t\t\treturn a;\n\t\t}\n\t\tconstexpr const value_type* operator->() const {\n\t\t\treturn &a;\n\t\t}\n\t\tconstexpr bool operator==(const iterator& i) const {\n\t\t\treturn b == i.b;\n\t\t}\n\t\tconstexpr bool operator!=(const iterator& i) const {\n\t\t\treturn !(b == i.b);\n\t\t}\n\t\tconstexpr value_type start() const {\n\t\t\treturn a;\n\t\t}\n\t\tconstexpr value_type size() const {\n\t\t\treturn b;\n\t\t}\n\t\tconstexpr value_type step() const {\n\t\t\treturn c;\n\t\t}\n\t};\n\tconstexpr Step(value_type b, value_type c, value_type s) : be(b, c, s) {}\n\tconstexpr iterator begin() const {\n\t\treturn be;\n\t}\n\tconstexpr iterator end() const {\n\t\treturn en;\n\t}\n\tconstexpr value_type start() const {\n\t\treturn be.start();\n\t}\n\tconstexpr value_type size() const {\n\t\treturn be.size();\n\t}\n\tconstexpr value_type step() const {\n\t\treturn be.step();\n\t}\n\tconstexpr value_type sum() const {\n\t\treturn start() * size() + step() * (size() * (size() - 1) / 2);\n\t}\n\toperator vector<value_type>() const {\n\t\treturn to_a();\n\t}\n\tauto to_a() const {\n\t\tvector<value_type> result;\n\t\tresult.reserve(size());\n\t\tfor (auto i : *this) {\n\t\t\tresult.push_back(i);\n\t\t}\n\t\treturn result;\n\t}\n\nprivate:\n\titerator be, en;\n};\ntemplate <class T> constexpr auto step(T a) {\n\treturn Step<T>(0, a, 1);\n}\ntemplate <class T> constexpr auto step(T a, T b) {\n\treturn Step<T>(a, b - a, 1);\n}\ntemplate <class T> constexpr auto step(T a, T b, T c) {\n\treturn Step<T>(a, a < b ? (b - a - 1) / c + 1 : 0, c);\n}\n#line 8 \"/home/yuruhiya/programming/library/template/Ruby.cpp\"\nusing namespace std;\n\ntemplate <class F> struct Callable {\n\tF func;\n\tCallable(const F& f) : func(f) {}\n};\ntemplate <class T, class F> auto operator|(const T& v, const Callable<F>& c) {\n\treturn c.func(v);\n}\n\nstruct Sort_impl {\n\ttemplate <class F> auto operator()(F&& f) {\n\t\treturn Callable([&](auto v) {\n\t\t\tsort(begin(v), end(v), f);\n\t\t\treturn v;\n\t\t});\n\t}\n\ttemplate <class T> friend auto operator|(T v, [[maybe_unused]] const Sort_impl& c) {\n\t\tsort(begin(v), end(v));\n\t\treturn v;\n\t}\n} Sort;\nstruct SortBy_impl {\n\ttemplate <class F> auto operator()(F&& f) {\n\t\treturn Callable([&](auto v) {\n\t\t\tsort(begin(v), end(v), [&](const auto& i, const auto& j) { return f(i) < f(j); });\n\t\t\treturn v;\n\t\t});\n\t}\n} SortBy;\nstruct RSort_impl {\n\ttemplate <class F> auto operator()(F&& f) {\n\t\treturn Callable([&](auto v) {\n\t\t\tsort(rbegin(v), rend(v), f);\n\t\t\treturn v;\n\t\t});\n\t}\n\ttemplate <class T> friend auto operator|(T v, [[maybe_unused]] const RSort_impl& c) {\n\t\tsort(rbegin(v), rend(v));\n\t\treturn v;\n\t}\n} RSort;\nstruct RSortBy_impl {\n\ttemplate <class F> auto operator()(F&& f) {\n\t\treturn Callable([&](auto v) {\n\t\t\tsort(begin(v), end(v), [&](const auto& i, const auto& j) { return f(i) > f(j); });\n\t\t\treturn v;\n\t\t});\n\t}\n} RSortBy;\nstruct Reverse_impl {\n\ttemplate <class T> friend auto operator|(T v, const Reverse_impl& c) {\n\t\treverse(begin(v), end(v));\n\t\treturn v;\n\t}\n} Reverse;\nstruct Unique_impl {\n\ttemplate <class T> friend auto operator|(T v, const Unique_impl& c) {\n\t\tv.erase(unique(begin(v), end(v), end(v)));\n\t\treturn v;\n\t}\n} Unique;\nstruct Uniq_impl {\n\ttemplate <class T> friend auto operator|(T v, const Uniq_impl& c) {\n\t\tsort(begin(v), end(v));\n\t\tv.erase(unique(begin(v), end(v)), end(v));\n\t\treturn v;\n\t}\n} Uniq;\nstruct Rotate_impl {\n\tauto operator()(int&& left) {\n\t\treturn Callable([&](auto v) {\n\t\t\tint s = static_cast<int>(size(v));\n\t\t\tassert(-s <= left && left <= s);\n\t\t\tif (0 <= left) {\n\t\t\t\trotate(begin(v), begin(v) + left, end(v));\n\t\t\t} else {\n\t\t\t\trotate(begin(v), end(v) + left, end(v));\n\t\t\t}\n\t\t\treturn v;\n\t\t});\n\t}\n} Rotate;\nstruct Max_impl {\n\ttemplate <class F> auto operator()(F&& f) {\n\t\treturn Callable([&](auto v) { return *max_element(begin(v), end(v), f); });\n\t}\n\ttemplate <class T> friend auto operator|(T v, const Max_impl& c) {\n\t\treturn *max_element(begin(v), end(v));\n\t}\n} Max;\nstruct Min_impl {\n\ttemplate <class F> auto operator()(F&& f) {\n\t\treturn Callable([&](auto v) { return *min_element(begin(v), end(v), f); });\n\t}\n\ttemplate <class T> friend auto operator|(T v, const Min_impl& c) {\n\t\treturn *min_element(begin(v), end(v));\n\t}\n} Min;\nstruct MaxPos_impl {\n\ttemplate <class T> friend auto operator|(T v, const MaxPos_impl& c) {\n\t\treturn max_element(begin(v), end(v)) - begin(v);\n\t}\n} MaxPos;\nstruct MinPos_impl {\n\ttemplate <class T> friend auto operator|(T v, const MinPos_impl& c) {\n\t\treturn min_element(begin(v), end(v)) - begin(v);\n\t}\n} MinPos;\nstruct MaxBy_impl {\n\ttemplate <class F> auto operator()(F&& f) {\n\t\treturn Callable([&](auto v) {\n\t\t\tauto max_it = begin(v);\n\t\t\tauto max_val = f(*max_it);\n\t\t\tfor (auto it = next(begin(v)); it != end(v); ++it) {\n\t\t\t\tif (auto val = f(*it); max_val < val) {\n\t\t\t\t\tmax_it = it;\n\t\t\t\t\tmax_val = val;\n\t\t\t\t}\n\t\t\t}\n\t\t\treturn *max_it;\n\t\t});\n\t}\n} MaxBy;\nstruct MinBy_impl {\n\ttemplate <class F> auto operator()(F&& f) {\n\t\treturn Callable([&](auto v) {\n\t\t\tauto min_it = begin(v);\n\t\t\tauto min_val = f(*min_it);\n\t\t\tfor (auto it = next(begin(v)); it != end(v); ++it) {\n\t\t\t\tif (auto val = f(*it); min_val > val) {\n\t\t\t\t\tmin_it = it;\n\t\t\t\t\tmin_val = val;\n\t\t\t\t}\n\t\t\t}\n\t\t\treturn *min_it;\n\t\t});\n\t}\n} MinBy;\nstruct MaxOf_impl {\n\ttemplate <class F> auto operator()(F&& f) {\n\t\treturn Callable([&](auto v) {\n\t\t\tauto max_val = f(*begin(v));\n\t\t\tfor (auto it = next(begin(v)); it != end(v); ++it) {\n\t\t\t\tif (auto val = f(*it); max_val < val) {\n\t\t\t\t\tmax_val = val;\n\t\t\t\t}\n\t\t\t}\n\t\t\treturn max_val;\n\t\t});\n\t}\n} MaxOf;\nstruct MinOf_impl {\n\ttemplate <class F> auto operator()(F&& f) {\n\t\treturn Callable([&](auto v) {\n\t\t\tauto min_val = f(*begin(v));\n\t\t\tfor (auto it = next(begin(v)); it != end(v); ++it) {\n\t\t\t\tif (auto val = f(*it); min_val > val) {\n\t\t\t\t\tmin_val = val;\n\t\t\t\t}\n\t\t\t}\n\t\t\treturn min_val;\n\t\t});\n\t}\n} MinOf;\nstruct Count_impl {\n\ttemplate <class V> auto operator()(const V& val) {\n\t\treturn Callable([&](auto v) { return count(begin(v), end(v), val); });\n\t}\n} Count;\nstruct CountIf_impl {\n\ttemplate <class F> auto operator()(const F& f) {\n\t\treturn Callable([&](auto v) { return count_if(begin(v), end(v), f); });\n\t}\n} CountIf;\nstruct Index_impl {\n\ttemplate <class V> auto operator()(const V& val) {\n\t\treturn Callable([&](auto v) -> optional<int> {\n\t\t\tauto result = find(begin(v), end(v), val);\n\t\t\treturn result != end(v) ? optional(result - begin(v)) : nullopt;\n\t\t});\n\t}\n\ttemplate <class V> auto operator()(const V& val, size_t i) {\n\t\treturn Callable([&](auto v) -> optional<int> {\n\t\t\tauto result = find(next(begin(v), i), end(v), val);\n\t\t\treturn result != end(v) ? optional(result - begin(v)) : nullopt;\n\t\t});\n\t}\n} Index;\nstruct IndexIf_impl {\n\ttemplate <class F> auto operator()(const F& f) {\n\t\treturn Callable([&](auto v) -> optional<int> {\n\t\t\tauto result = find_if(begin(v), end(v), f);\n\t\t\treturn result != end(v) ? optional(result - begin(v)) : nullopt;\n\t\t});\n\t}\n} IndexIf;\nstruct FindIf_impl {\n\ttemplate <class F> auto operator()(const F& f) {\n\t\treturn Callable([&](auto v) -> optional<typename decltype(v)::value_type> {\n\t\t\tauto result = find_if(begin(v), end(v), f);\n\t\t\treturn result != end(v) ? optional(*result) : nullopt;\n\t\t});\n\t}\n} FindIf;\nstruct Sum_impl {\n\ttemplate <class F> auto operator()(F&& f) {\n\t\treturn Callable([&](auto v) {\n\t\t\treturn accumulate(next(begin(v)), end(v), f(*begin(v)),\n\t\t\t [&](const auto& a, const auto& b) { return a + f(b); });\n\t\t});\n\t}\n\ttemplate <class T> friend auto operator|(T v, [[maybe_unused]] const Sum_impl& c) {\n\t\treturn accumulate(begin(v), end(v), typename T::value_type{});\n\t}\n} Sum;\nstruct Includes {\n\ttemplate <class V> auto operator()(const V& val) {\n\t\treturn Callable([&](auto v) { return find(begin(v), end(v), val) != end(v); });\n\t}\n} Includes;\nstruct IncludesIf_impl {\n\ttemplate <class F> auto operator()(const F& f) {\n\t\treturn Callable([&](auto v) { return find_if(begin(v), end(v), f) != end(v); });\n\t}\n} IncludesIf;\nstruct RemoveIf_impl {\n\ttemplate <class F> auto operator()(const F& f) {\n\t\treturn Callable([&](auto v) {\n\t\t\tv.erase(remove_if(begin(v), end(v), f), end(v));\n\t\t\treturn v;\n\t\t});\n\t}\n} RemoveIf;\nstruct Each_impl {\n\ttemplate <class F> auto operator()(F&& f) {\n\t\treturn Callable([&](auto v) {\n\t\t\tfor (const auto& i : v) {\n\t\t\t\tf(i);\n\t\t\t}\n\t\t});\n\t}\n} Each;\nstruct EachConsPair_impl {\n\ttemplate <class T, class value_type = typename T::value_type>\n\tfriend auto operator|(const T& v, EachConsPair_impl& c) {\n\t\tvector<pair<value_type, value_type>> result;\n\t\tif (size(v) >= 2) {\n\t\t\tresult.reserve(size(v) - 1);\n\t\t\tfor (size_t i = 0; i < size(v) - 1; ++i) {\n\t\t\t\tresult.emplace_back(v[i], v[i + 1]);\n\t\t\t}\n\t\t}\n\t\treturn result;\n\t}\n} EachConsPair;\nstruct Select_impl {\n\ttemplate <class F> auto operator()(F&& f) {\n\t\treturn Callable([&](auto v) {\n\t\t\tusing value_type = typename decltype(v)::value_type;\n\t\t\tvector<value_type> result;\n\t\t\tfor (const auto& i : v) {\n\t\t\t\tif (f(i)) result.push_back(i);\n\t\t\t}\n\t\t\treturn result;\n\t\t});\n\t}\n} Select;\nstruct Map_impl {\n\ttemplate <class F> auto operator()(F&& f) {\n\t\treturn Callable([&](auto v) {\n\t\t\tusing result_type = invoke_result_t<F, typename decltype(v)::value_type>;\n\t\t\tvector<result_type> result;\n\t\t\tresult.reserve(size(v));\n\t\t\tfor (const auto& i : v) {\n\t\t\t\tresult.push_back(f(i));\n\t\t\t}\n\t\t\treturn result;\n\t\t});\n\t}\n} Map;\nstruct Indexed_impl {\n\ttemplate <class T> friend auto operator|(const T& v, Indexed_impl& c) {\n\t\tusing value_type = typename T::value_type;\n\t\tvector<pair<value_type, int>> result;\n\t\tresult.reserve(size(v));\n\t\tint index = 0;\n\t\tfor (const auto& i : v) {\n\t\t\tresult.emplace_back(i, index++);\n\t\t}\n\t\treturn result;\n\t}\n} Indexed;\nstruct AllOf_impl {\n\ttemplate <class F> auto operator()(F&& f) {\n\t\treturn Callable([&](auto v) {\n\t\t\tfor (const auto& i : v) {\n\t\t\t\tif (!f(i)) return false;\n\t\t\t}\n\t\t\treturn true;\n\t\t});\n\t}\n} AllOf;\nstruct AnyOf_impl {\n\ttemplate <class F> auto operator()(F&& f) {\n\t\treturn Callable([&](auto v) {\n\t\t\tfor (const auto& i : v) {\n\t\t\t\tif (f(i)) return true;\n\t\t\t}\n\t\t\treturn false;\n\t\t});\n\t}\n} AnyOf;\nstruct NoneOf_impl {\n\ttemplate <class F> auto operator()(F&& f) {\n\t\treturn Callable([&](auto v) {\n\t\t\tfor (const auto& i : v) {\n\t\t\t\tif (f(i)) return false;\n\t\t\t}\n\t\t\treturn true;\n\t\t});\n\t}\n} NoneOf;\n\nstruct Tally_impl {\n\ttemplate <class F> auto operator()(size_t max_val) {\n\t\treturn Callable([&](auto v) {\n\t\t\tvector<size_t> result(max_val);\n\t\t\tfor (const auto& i : v) {\n\t\t\t\tresult[static_cast<size_t>(i)]++;\n\t\t\t}\n\t\t\treturn result;\n\t\t});\n\t}\n\ttemplate <class T, class value_type = typename T::value_type>\n\tfriend auto operator|(const T& v, Tally_impl& c) {\n\t\tmap<value_type, size_t> result;\n\t\tfor (const auto& i : v) {\n\t\t\tresult[i]++;\n\t\t}\n\t\treturn result;\n\t}\n} Tally;\n\ntemplate <class T> auto operator*(const vector<T>& a, size_t n) {\n\tT result;\n\tfor (size_t i = 0; i < n; ++i) {\n\t\tresult.insert(result.end(), a.begin(), a.end());\n\t}\n\treturn result;\n}\nauto operator*(string a, size_t n) {\n\tstring result;\n\tfor (size_t i = 0; i < n; ++i) {\n\t\tresult += a;\n\t}\n\treturn result;\n}\ntemplate <class T, class U> auto& operator<<(vector<T>& a, const U& b) {\n\ta.insert(a.end(), all(b));\n\treturn a;\n}\ntemplate <class T> auto& operator<<(string& a, const T& b) {\n\ta.insert(a.end(), all(b));\n\treturn a;\n}\ntemplate <class T, class U> auto operator+(vector<T> a, const U& b) {\n\ta << b;\n\treturn a;\n}\ntemplate <class T> auto operator+(string a, const T& b) {\n\ta << b;\n\treturn a;\n}\n#line 7 \"/home/yuruhiya/programming/library/template/functions.cpp\"\nusing namespace std;\n\ntemplate <class T = long long> constexpr T TEN(size_t n) {\n\tT result = 1;\n\tfor (size_t i = 0; i < n; ++i) result *= 10;\n\treturn result;\n}\ntemplate <class T, class U,\n enable_if_t<is_integral_v<T> && is_integral_v<U>, nullptr_t> = nullptr>\nconstexpr auto div_ceil(T n, U m) {\n\treturn (n + m - 1) / m;\n}\ntemplate <class T, class U> constexpr auto div_ceil2(T n, U m) {\n\treturn div_ceil(n, m) * m;\n}\ntemplate <class T> constexpr T triangle(T n) {\n\treturn (n & 1) ? (n + 1) / 2 * n : n / 2 * (n + 1);\n}\ntemplate <class T> constexpr T nC2(T n) {\n\treturn (n & 1) ? (n - 1) / 2 * n : n / 2 * (n - 1);\n}\ntemplate <class T, class U> constexpr auto middle(const T& l, const U& r) {\n\treturn l + (r - l) / 2;\n}\ntemplate <class T, class U, class V>\nconstexpr bool in_range(const T& v, const U& lower, const V& upper) {\n\treturn lower <= v && v < upper;\n}\ntemplate <class T, enable_if_t<is_integral_v<T>, nullptr_t> = nullptr>\nconstexpr bool is_square(T n) {\n\tT s = sqrt(n);\n\treturn s * s == n || (s + 1) * (s + 1) == n;\n}\ntemplate <class T = long long> constexpr T BIT(int b) {\n\treturn T(1) << b;\n}\ntemplate <class T> constexpr int BIT(T x, int i) {\n\treturn (x & (T(1) << i)) ? 1 : 0;\n}\ntemplate <class T> constexpr int Sgn(T x) {\n\treturn (0 < x) - (0 > x);\n}\ntemplate <class T, class U, enable_if_t<is_integral_v<U>, nullptr_t> = nullptr>\nconstexpr T Pow(T a, U n) {\n\tassert(n >= 0);\n\tT result = 1;\n\twhile (n > 0) {\n\t\tif (n & 1) {\n\t\t\tresult *= a;\n\t\t\tn--;\n\t\t} else {\n\t\t\ta *= a;\n\t\t\tn >>= 1;\n\t\t}\n\t}\n\treturn result;\n}\ntemplate <class T, class U, enable_if_t<is_integral_v<U>, nullptr_t> = nullptr>\nconstexpr T Powmod(T a, U n, T mod) {\n\tassert(n >= 0);\n\tif (a > mod) a %= mod;\n\tT result = 1;\n\twhile (n > 0) {\n\t\tif (n & 1) {\n\t\t\tresult = result * a % mod;\n\t\t\tn--;\n\t\t} else {\n\t\t\ta = a * a % mod;\n\t\t\tn >>= 1;\n\t\t}\n\t}\n\treturn result;\n}\ntemplate <class T> bool chmax(T& a, const T& b) {\n\tif (a < b) {\n\t\ta = b;\n\t\treturn true;\n\t}\n\treturn false;\n}\ntemplate <class T> bool chmin(T& a, const T& b) {\n\tif (a > b) {\n\t\ta = b;\n\t\treturn true;\n\t}\n\treturn false;\n}\ntemplate <class T> int sz(const T& v) {\n\treturn v.size();\n}\ntemplate <class T, class U> int lower_index(const T& a, const U& v) {\n\treturn lower_bound(all(a), v) - a.begin();\n}\ntemplate <class T, class U> int upper_index(const T& a, const U& v) {\n\treturn upper_bound(all(a), v) - a.begin();\n}\ntemplate <class T> auto Slice(const T& v, size_t i, size_t len) {\n\treturn i < v.size() ? T(v.begin() + i, v.begin() + min(i + len, v.size())) : T();\n}\ntemplate <class T, class U = typename T::value_type> U Gcdv(const T& v) {\n\treturn accumulate(next(v.begin()), v.end(), U(*v.begin()), gcd<U, U>);\n}\ntemplate <class T, class U = typename T::value_type> U Lcmv(const T& v) {\n\treturn accumulate(next(v.begin()), v.end(), U(*v.begin()), lcm<U, U>);\n}\nnamespace internal {\n\ttemplate <class T, size_t N> auto make_vector(vector<int>& sizes, const T& init) {\n\t\tif constexpr (N == 1) {\n\t\t\treturn vector(sizes[0], init);\n\t\t} else {\n\t\t\tint size = sizes[N - 1];\n\t\t\tsizes.pop_back();\n\t\t\treturn vector(size, make_vector<T, N - 1>(sizes, init));\n\t\t}\n\t}\n} // namespace internal\ntemplate <class T, size_t N> auto make_vector(const int (&sizes)[N], const T& init = T()) {\n\tvector s(rbegin(sizes), rend(sizes));\n\treturn internal::make_vector<T, N>(s, init);\n}\n#line 9 \"/home/yuruhiya/programming/library/template/template.cpp\"\n#if __has_include(<library/dump.hpp>)\n#include <library/dump.hpp>\n#define LOCAL\n#else\n#define dump(...) ((void)0)\n#endif\n\ntemplate <class T> constexpr T oj_local(const T& oj, const T& local) {\n#ifndef LOCAL\n\treturn oj;\n#else\n\treturn local;\n#endif\n}\n#line 3 \"/home/yuruhiya/programming/library/Utility/Combination.cpp\"\nusing namespace std;\n\ntemplate <class T, class F> void each_repeated_permutation(const T& array, size_t count, F f) {\n\tusing value_type = typename T::value_type;\n\tvector<value_type> permutation(count);\n\tauto dfs = [&](auto self, size_t size) -> void {\n\t\tif (size == count) {\n\t\t\tf(permutation);\n\t\t} else {\n\t\t\tfor (const auto& i : array) {\n\t\t\t\tpermutation[size] = i;\n\t\t\t\tself(self, size + 1);\n\t\t\t}\n\t\t}\n\t};\n\tdfs(dfs, 0);\n}\n\ntemplate <class T> auto repeated_permutations(const T& array, size_t count) {\n\tusing value_type = typename T::value_type;\n\tvector<vector<value_type>> result;\n\tsize_t size = 1;\n\tfor (size_t i = 0; i < count; ++i) {\n\t\tsize *= array.size();\n\t}\n\tresult.reserve(size);\n\teach_repeated_permutation(array, count, [&](const auto& perm) { result.push_back(perm); });\n\treturn result;\n}\n#line 3 \"a.cpp\"\n\nint main() {\n\twhile (true) {\n\t\tinl(n, m, k);\n\t\tif (n + m + k == 0) break;\n\t\tll cnt = 0, sum = 0;\n\t\teach_repeated_permutation(step(1ll, m + 1), n, [&](auto p) {\n\t\t\tcnt++;\n\t\t\tsum += max(1ll, (p | Sum) - k);\n\t\t});\n\t\tout(LD(sum) / cnt);\n\t}\n}", "accuracy": 1, "time_ms": 780, "memory_kb": 3580, "score_of_the_acc": -1.0318, "final_rank": 19 }, { "submission_id": "aoj_1286_5033719", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\nint p[100000];\n\nint solve() {\n int n,m,k;\n cin>>n>>m>>k;\n if (n==0 && m==0 && k==0) return 0;\n\n memset(p,0,sizeof(p));\n p[0]=1;\n for (int d=1; d<=n; d++) {\n for (int i=d*m; i>=d; i--) {\n p[i]=0;\n for (int j=1; j<=m && j<=i; j++) p[i]+=p[i-j];\n }\n p[d-1]=0;\n }\n\n double bill=0;\n for (int i=n; i<=n*m; i++) bill+=(i>k ? i-k : 1)*p[i];\n for (int i=0; i<n; i++) bill/=m;\n\n cout<<fixed<<setprecision(8)<<bill<<endl;\n return 1;\n}\n\nint main() {\n while (solve());\n return 0;\n}", "accuracy": 1, "time_ms": 20, "memory_kb": 3632, "score_of_the_acc": -0.0348, "final_rank": 1 }, { "submission_id": "aoj_1286_4961432", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\nusing ll = long long;\n\nint cnt[300001];\n\nvoid dfs(int k, int n, int m, int s) {\n if (k == n) {\n cnt[s]++;\n return;\n }\n for (int i = 1; i <= m; i++) {\n dfs(k + 1, n, m, s + i);\n }\n}\n\nint main() {\n while (1) {\n int n, m, k;\n cin >> n >> m >> k;\n if (n == 0)\n break;\n for (int i = n; i <= n * m; i++)\n cnt[i] = 0;\n dfs(0, n, m, 0);\n double ans = 0;\n for (int i = n; i <= n * m; i++) {\n int j = max(i - k, 1);\n ans += j * cnt[i];\n }\n for (int i = 0; i < n; i++)\n ans /= m;\n printf(\"%.15lf\\n\", ans);\n }\n}", "accuracy": 1, "time_ms": 40, "memory_kb": 3352, "score_of_the_acc": -0.0448, "final_rank": 3 }, { "submission_id": "aoj_1286_4931257", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\n#define int long long\n\n// debug methods\n// usage: debug(x,y);\n#define CHOOSE(a) CHOOSE2 a\n#define CHOOSE2(a0,a1,a2,a3,a4,x,...) x\n#define debug_1(x1) cout<<#x1<<\": \"<<x1<<endl\n#define debug_2(x1,x2) cout<<#x1<<\": \"<<x1<<\", \"#x2<<\": \"<<x2<<endl\n#define debug_3(x1,x2,x3) cout<<#x1<<\": \"<<x1<<\", \"#x2<<\": \"<<x2<<\", \"#x3<<\": \"<<x3<<endl\n#define debug_4(x1,x2,x3,x4) cout<<#x1<<\": \"<<x1<<\", \"#x2<<\": \"<<x2<<\", \"#x3<<\": \"<<x3<<\", \"#x4<<\": \"<<x4<<endl\n#define debug_5(x1,x2,x3,x4,x5) cout<<#x1<<\": \"<<x1<<\", \"#x2<<\": \"<<x2<<\", \"#x3<<\": \"<<x3<<\", \"#x4<<\": \"<<x4<<\", \"#x5<<\": \"<<x5<<endl\n#ifdef _DEBUG\n#define debug(...) CHOOSE((__VA_ARGS__,debug_5,debug_4,debug_3,debug_2,debug_1,~))(__VA_ARGS__)\n#else\n#define debug(...)\n#endif\n\n#define MAX_N (1000006)\n#define INF (1LL << 60)\n#define eps 1e-9\nconst int MOD = (int)1e9 + 7;\n\nint n,m,k;\nvector<int> v;\n\nvoid rec(int sum, int depth) {\n if(depth==n){\n v.push_back(max(1LL,sum-k));\n return;\n }\n for(int i=1; i<=m; i++){\n rec(sum+i, depth+1);\n }\n}\n\nsigned main(){\n while(1){\n int nn, mm, kk;\n cin >> nn >> mm >> kk;\n if(nn==0) break;\n n = nn;\n m = mm;\n k = kk;\n v.clear();\n\n rec(0,0);\n debug(v.size());\n\n map<int,int> mp;\n for(int i=0; i<v.size(); i++) mp[v[i]]++;\n double ans = 0.0;\n for(auto it: mp){\n debug(it.first, it.second);\n ans += it.first * it.second;\n }\n printf(\"%.10f\\n\", ans/pow(m,n));\n }\n}\n\n/*\n\n\n*/", "accuracy": 1, "time_ms": 180, "memory_kb": 20152, "score_of_the_acc": -1.2105, "final_rank": 20 }, { "submission_id": "aoj_1286_4894368", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\nint n, m, k;\n\nlong long solve(long long now = 0, long long sum = 0);\n\nint main() {\n cout << fixed << setprecision(10);\n while (1) {\n cin >> n >> m >> k;\n if (n + m + k == 0) break;\n long double res = solve();\n for (int i = 0; i < n; ++i) res /= m;\n cout << res << endl;\n }\n return 0;\n}\n\nlong long solve(long long now, long long sum) {\n if (now == n) return max(1LL, sum - k);\n long long res = 0;\n for (int i = 1; i <= m; ++i) res += solve(now + 1, sum + i);\n return res;\n}", "accuracy": 1, "time_ms": 50, "memory_kb": 3324, "score_of_the_acc": -0.0563, "final_rank": 5 }, { "submission_id": "aoj_1286_4872269", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\nint solve() {\n int n, m, k;\n cin >> n >> m >> k;\n if (n == 0) return 1;\n vector<double> p(n*m + 1);\n p[0] = 1;\n for (int i = 0; i < n; ++i) {\n vector<double> nxt(n*m + 1);\n for (int j = 0; j <= n*m; ++j) {\n for (int x = 1; x <= m && j + x <= n*m; ++x) {\n nxt[j + x] += p[j] / m;\n }\n }\n p = move(nxt);\n }\n double ans = 0;\n for (int i = 0; i <= n*m; ++i) {\n ans += p[i] * max(1, i - k);\n }\n cout << fixed << setprecision(15) << ans << '\\n';\n return 0;\n}\n\nint main() {\n while (!solve());\n}", "accuracy": 1, "time_ms": 40, "memory_kb": 3696, "score_of_the_acc": -0.0649, "final_rank": 8 }, { "submission_id": "aoj_1286_4871417", "code_snippet": "#include <iostream>\n#include <iomanip>\nusing namespace std;\n\nint main()\n{\n while(true){\n int n, m, k;\n cin >> n >> m >> k;\n if(n == 0 && m == 0 && k == 0) break;\n double dp[30][10005];\n for(int i = 0; i <= n; i++){\n for(int j = 0; j <= 10000; j++) dp[i][j] = 0;\n }\n dp[0][0] = 1;\n for(int i = 1; i <= n; i++){\n for(int j = i; j <= i * m; j++){\n for(int k = 1; k <= min(j, m); k++){\n dp[i][j] += dp[i - 1][j - k];\n }\n }\n double s = 0;\n for(int j = i; j <= i * m; j++) s += dp[i][j];\n for(int j = i; j <= i * m; j++) dp[i][j] /= s;\n }\n double ans = 0;\n for(int j = n; j <= n * m; j++){\n ans += max(1, j - k) * dp[n][j];\n }\n cout << fixed << setprecision(15) << ans << endl;\n }\n}", "accuracy": 1, "time_ms": 30, "memory_kb": 4996, "score_of_the_acc": -0.1277, "final_rank": 13 }, { "submission_id": "aoj_1286_4871166", "code_snippet": "#pragma GCC optimize(\"Ofast\")\n#include <iostream> // cout, endl, cin\n#include <string> // string, to_string, stoi\n#include <vector> // vector\n#include <algorithm> // min, max, swap, sort, reverse, lower_bound, upper_bound\n#include <utility> // pair, make_pair\n#include <tuple> // tuple, make_tuple\n#include <cstdint> // int64_t, int*_t\n#include <cstdio> // printf\n#include <map> // map\n#include <queue> // queue, priority_queue\n#include <set> // set\n#include <stack> // stack\n#include <deque> // deque\n#include <unordered_map> // unordered_map\n#include <unordered_set> // unordered_set\n#include <bitset> // bitset\n#include <cctype> // isupper, islower, isdigit, toupper, tolower\n#include <iomanip> // setprecision\n#include <complex> // complex\n#include <math.h> \n#include <functional>\n#include <cassert>\nusing namespace std;\nusing ll = long long;\nusing P = pair<int,int>;\nconstexpr ll INF = 1e18;\nconstexpr int inf = 1e9;\nconstexpr double eps = 1e-9;\n// constexpr ll mod = 1000000007;\nconstexpr ll mod = 998244353;\nconst int dx[8] = {1, 0, -1, 0,1,1,-1,-1};\nconst int dy[8] = {0, 1, 0, -1,1,-1,1,-1};\ntemplate<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }\ntemplate<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }\n// --------------------------------------------------------------------------\n\n\nbool solve(){\n int N,M,K;\n cin >> N >> M >> K;\n if(N == 0) return false;\n double ans = 0;\n double sum = 0;\n function<void(int)> dfs = [&dfs,&N,&M,&sum,&K,&ans](int k){\n if(k == N){\n ans += max(1.0,sum-K);\n return;\n }else{\n for(int i=1; i<=M; i++){\n sum += i;\n dfs(k+1);\n sum -= i;\n }\n }\n };\n dfs(0);\n for(int i=0; i<N; i++) ans /= M;\n cout << fixed << setprecision(10) << ans << \"\\n\";\n return true;\n}\n\n\nint main(){\n ios::sync_with_stdio(false);\n cin.tie(nullptr);\n while(solve());\n return 0;\n}", "accuracy": 1, "time_ms": 110, "memory_kb": 3508, "score_of_the_acc": -0.146, "final_rank": 16 }, { "submission_id": "aoj_1286_4820008", "code_snippet": "#include <bits/stdc++.h>\n#define rep(i,n) for(int (i)=0;(i)<(n);++i)\n#define mkp make_pair\n#define mkt make_tuple\n#define all(v) (v).begin(),(v).end()\nusing namespace std;\ntypedef long long ll;\nconst ll MOD=1e9+7;\ntemplate<class T> void chmin(T &a,const T &b){if(a>b) a=b;}\ntemplate<class T> void chmax(T &a,const T &b){if(a<b) a=b;}\n\nll ans;\nint N,M,K;\n\nvoid rec(int now,ll val){\n if(now==0){\n ans+=max(1ll,val-K);\n return;\n }\n for(int i=1;i<=M;i++) rec(now-1,val+i);\n}\n\nvoid solve(){\n ans=0;\n ll all=1;\n rep(i,N) all*=M;\n\n rec(N,0);\n\n long double output=ans/(long double)all;\n cout<<fixed<<setprecision(10)<<output<<\"\\n\";\n}\n\nint main(){\n while(1){\n cin>>N>>M>>K;\n if(N==0) break;\n\n solve();\n }\n\n return 0;\n}", "accuracy": 1, "time_ms": 60, "memory_kb": 3208, "score_of_the_acc": -0.0627, "final_rank": 7 }, { "submission_id": "aoj_1286_4783419", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n#define modulo 998244353\n#define mod(mod_x) ((((long long)mod_x+modulo))%modulo)\n#define Inf 1000000005\ndouble ans;\n\nvoid dfs(int n,int m,int k,int now){\n\tif(n==0){\n\t\tans += (double)max(1,now-k);\n\t\treturn;\n\t}\n\t\n\tfor(int i=1;i<=m;i++){\n\t\tdfs(n-1,m,k,now+i);\n\t}\n}\n\nint main(){\n\t\n\twhile(true){\n\t\tint n,m,k;\n\t\tcin>>n>>m>>k;\n\t\tif(n==0)break;\n\t\tans=0.0;\n\t\tdfs(n,m,k,0);\n\t\tans /= pow(m,n);\n\t\tcout<<fixed<<setprecision(15)<<ans<<endl;\n\t}\n\t\n\treturn 0;\n}", "accuracy": 1, "time_ms": 60, "memory_kb": 3456, "score_of_the_acc": -0.0772, "final_rank": 10 } ]
aoj_1292_cpp
Problem H: Top Spinning Spinning tops are one of the most popular and the most traditional toys. Not only spinning them, but also making one’s own is a popular enjoyment. One of the easiest way to make a top is to cut out a certain shape from a cardboard and pierce an axis stick through its center of mass. Professionally made tops usually have three dimensional shapes, but in this problem we consider only two dimensional ones. Usually, tops have rotationally symmetric shapes, such as a circle, a rectangle (with 2-fold rotational symmetry) or a regular triangle (with 3-fold symmetry). Although such symmetries are useful in determining their centers of mass, they are not definitely required; an asymmetric top also spins quite well if its axis is properly pierced at the center of mass. When a shape of a top is given as a path to cut it out from a cardboard of uniform thickness, your task is to find its center of mass to make it spin well. Also, you have to determine whether the center of mass is on the part of the cardboard cut out. If not, you cannot pierce the axis stick, of course. Input The input consists of multiple datasets, each of which describes a counterclockwise path on a cardboard to cut out a top. A path is indicated by a sequence of command lines, each of which specifies a line segment or an arc. In the description of commands below, the current position is the position to start the next cut, if any. After executing the cut specified by a command, the current position is moved to the end position of the cut made. The commands given are one of those listed below. The command name starts from the first column of a line and the command and its arguments are separated by a space. All the command arguments are integers. start x y Specifies the start position of a path. This command itself does not specify any cutting; it only sets the current position to be ( x , y ). line x y Specifies a linear cut along a straight line from the current position to the position ( x , y ), which is not identical to the current position . arc x y r Specifies a round cut along a circular arc. The arc starts from the current position and ends at ( x , y ), which is not identical to the current position . The arc has a radius of | r |. When r is negative, the center of the circle is to the left side of the direction of this round cut; when it is positive, it is to the right side (Figure 1). The absolute value of r is greater than the half distance of the two ends of the arc. Among two arcs connecting the start and the end positions with the specified radius, the arc specified is one with its central angle less than 180 degrees. close Closes a path by making a linear cut to the initial start position and terminates a dataset. If the current position is already at the start position, this command simply indicates the end of a dataset. The figure below gives an example of a command sequence and its corresponding path. N ...(truncated)
[ { "submission_id": "aoj_1292_4783909", "code_snippet": "#include<bits/stdc++.h>\ntypedef long long int ll;\ntypedef unsigned long long int ull;\n#define BIG_NUM 2000000000\n#define HUGE_NUM 1000000000000000000\n#define MOD 1000000007\n#define EPS 0.000000001\nusing namespace std;\n\n\n\n#define LOOP 50000\n\nstruct Point{\n\tPoint(double arg_x,double arg_y){\n\t\tx = arg_x;\n\t\ty = arg_y;\n\t}\n\n\tPoint(){\n\t\tx = y = 0.0;\n\t}\n\n\tPoint operator + (Point p){ return Point(x+p.x,y+p.y); }\n\tPoint operator - (Point p){ return Point(x-p.x,y-p.y);}\n\tPoint operator * (double a){ return Point(a*x,a*y); }\n\tPoint operator / (double a){ return Point(x/a,y/a); }\n\n\tdouble abs(){ return sqrt(norm()); }\n\tdouble norm(){ return x*x + y*y; }\n\n\tbool operator<(const Point &p) const{\n\t\treturn x != p.x? x < p.x: y < p.y;\n\t}\n\n\tbool operator == (const Point &p) const{\n\t\treturn fabs(x-p.x) < EPS && fabs(y-p.y) < EPS;\n\t}\n\n\tvoid debug(){\n\n\t\tprintf(\"(%.3lf,%.3lf)\\n\",x,y);\n\t}\n\n\tdouble x,y;\n};\n\ntypedef Point Vector;\ntypedef vector<Point> Polygon;\n\nstruct Line{\n\tLine(){\n\n\t}\n\tLine(Point a,Point b){\n\t\tp[0] = a;\n\t\tp[1] = b;\n\t}\n\t/*void outPut(){\n\n\t\tprintf(\"(%.3lf,%.3lf)-(%.3lf,%.3lf)\\n\",p[0].x,p[0].y,p[1].x,p[1].y);\n\t}*/\n\tPoint p[2];\n};\n\nstruct Circle{\n\tPoint center;\n\tdouble r;\n};\n\nchar buf[10];\n\ndouble calc_dist(Point A,Point B){\n\n\treturn sqrt((A.x-B.x)*(A.x-B.x)+(A.y-B.y)*(A.y-B.y));\n}\n\ndouble norm(Vector a){\n\treturn a.x*a.x+a.y*a.y;\n}\n\ndouble abs(Vector a){\n\treturn sqrt(norm(a));\n}\n\ndouble cross(Vector a,Vector b){\n return a.x*b.y-a.y*b.x;\n}\n\ndouble dot(Vector a,Vector b){\n return a.x*b.x + a.y*b.y;\n}\n\nPoint project(Line l,Point p){\n\n\tVector base = l.p[1]-l.p[0];\n\tdouble r = dot(p-l.p[0],base)/norm(base);\n\treturn l.p[0]+base*r;\n}\n\n\n//円と直線の交点を求める関数\nvector<Point> getCrossPoints(Circle c,Line l){\n\n\tvector<Point> ret;\n\n\tVector pr = project(l,c.center);\n\tVector e = (l.p[1]-l.p[0])/abs(l.p[1]-l.p[0]);\n\n\tdouble base;\n\n\tif(fabs(c.r*c.r-norm(pr-c.center)) < EPS){\n\n\t\tbase = 0;\n\t}else{\n\t\tbase = sqrt(c.r*c.r-norm(pr-c.center));\n\t}\n\n\tret.push_back(Point(pr+e*base));\n\tret.push_back(Point(pr-e*base));\n\n\treturn ret;\n}\n\n\nPoint calc_minus(Point a,Point b){\n Point ret;\n\n ret.x = a.x-b.x;\n ret.y = a.y-b.y;\n\n return ret;\n}\n\ndouble calc_len(Vector a){\n return sqrt(a.x*a.x+a.y*a.y);\n}\n\ndouble calc_slope(Line A){\n\n\tif(fabs(A.p[0].x-A.p[1].x) < EPS){\n\n\t\treturn DBL_MAX;\n\n\t}else if(fabs(A.p[0].y-A.p[1].y) < EPS){\n\n\t\treturn 0;\n\n\t}else{\n\n\t\treturn (A.p[0].y-A.p[1].y)/(A.p[0].x-A.p[1].x);\n\t}\n}\n\n//線分ではなく直線と点の距離\ndouble getDistanceLP(Line l,Point p){\n return fabs(cross(calc_minus(l.p[1],l.p[0]),calc_minus(p,l.p[0]))/calc_len(calc_minus(l.p[1],l.p[0])));\n}\n\n//点と線分の距離\ndouble getDistanceSP(Line l,Point p){\n\tif(dot(calc_minus(l.p[1],l.p[0]),calc_minus(p,l.p[0])) < 0.0)return calc_len(calc_minus(p,l.p[0]));\n\tif(dot(calc_minus(l.p[0],l.p[1]),calc_minus(p,l.p[1])) < 0.0)return calc_len(calc_minus(p,l.p[1]));\n\treturn getDistanceLP(l,p);\n}\n\ndouble calc_S(Polygon g){\n\n\tint N = g.size();\n\tdouble ret = 0;\n\n\tfor(int i = 0; i < g.size(); i++){\n\t\tret += cross(g[i],g[(i+1)%N]);\n\t}\n\treturn ret/2.0;\n}\n\nstatic const int COUNTER_CLOCKWISE = 1;\nstatic const int CLOCKWISE = -1;\nstatic const int ONLINE_BACK = 2;\nstatic const int ONLINE_FRONT = -1;\nstatic const int ON_SEGMENT = 0;\n\nint ccw(Point p0,Point p1,Point p2){\n\n\tVector a = p1 - p0;\n\tVector b = p2 - p0;\n\n\tif(cross(a,b) > EPS)return COUNTER_CLOCKWISE;\n\tif(cross(a,b) < -EPS)return CLOCKWISE;\n\tif(dot(a,b) < -EPS)return ONLINE_BACK;\n\tif(a.norm() < b.norm())return ONLINE_FRONT;\n\n\treturn ON_SEGMENT;\n}\n\nbool isSame(Point a,Point b){\n\n\treturn abs(a-b) < EPS;\n}\n\ndouble arg(Vector p){\n\treturn atan2(p.y,p.x);\n}\n\nVector polar(double a,double r){\n\treturn Point(cos(r)*a,sin(r)*a);\n}\n\n//円と円の交点\nvector<Point> getCrossPoints(Circle c1,Circle c2){\n\n\tvector<Point> res;\n\n\tdouble d = abs(c1.center-c2.center);\n\tdouble a = acos((c1.r*c1.r+d*d-c2.r*c2.r)/(2*c1.r*d));\n\tdouble t = arg(c2.center-c1.center);\n\n\tres.push_back(Point(c1.center+polar(c1.r,t+a)));\n\tres.push_back(Point(c1.center+polar(c1.r,t-a)));\n\n\treturn res;\n}\n\n//点moveをbaseを中心にradラジアン回転させる\nPoint rotate(Point base,Point move,double rad){\n\n\tPoint ret;\n\tmove = move-base;\n\n\tret.x = move.x*cos(rad)-move.y*sin(rad);\n\tret.y = move.x*sin(rad)+move.y*cos(rad);\n\n\tret = ret+base;\n\n\treturn ret;\n}\n\n//ポリゴンの重心を求める\nPoint calc_center_of_gravity(Polygon P){\n\n\tPoint base_p = Point(0,0);\n\tint N = P.size();\n\n\tPoint ret = Point(0,0);\n\tdouble sum_S = 0;\n\n\tfor(int i = 0; i < N; i++){\n\n\t\tPolygon work;\n\t\twork.push_back(P[i]);\n\t\twork.push_back(P[(i+1)%N]);\n\t\twork.push_back(base_p);\n\n\t\tdouble tmp_S = calc_S(work);\n\n\t\tPoint tmp_center = P[i]+P[(i+1)%N]+base_p;\n\t\ttmp_center = tmp_center/3; //三角形の重心\n\n\t\ttmp_center = tmp_center*tmp_S;\n\t\tret = ret+tmp_center;\n\n\t\tsum_S += tmp_S;\n\t}\n\n\tret = ret/sum_S;\n\n\treturn ret;\n}\n\n/*\n * IN 2\n * ON 1\n * OUT 0\n *\n */\nint contains(Polygon g,Point p){\n\tint n = g.size();\n\tbool x = false;\n\tfor(int i = 0; i < n; i++){\n\t\tPoint a = g[i]-p,b = g[(i+1)%n]-p;\n\t\tif(abs(cross(a,b)) < EPS && dot(a,b) < EPS)return 1;\n\t\tif(a.y > b.y)swap(a,b);\n\t\tif(a.y < EPS && EPS < b.y && cross(a,b) > EPS) x = !x;\n\t}\n\treturn (x ? 2:0);\n}\n\n\nvoid func(){\n\n\tbool is_first = true;\n\tPolygon P;\n\n\tdouble x,y,r;\n\n\tPoint pre,next,calc_center;\n\tCircle C1,C2;\n\n\twhile(true){\n\t\tif(is_first){ //必ずstartコマンド\n\n\t\t\tscanf(\"%lf %lf\",&x,&y);\n\t\t\tP.push_back(Point(x,y));\n\t\t\tis_first = false;\n\t\t\tcontinue;\n\t\t}\n\t\tscanf(\"%s\",buf);\n\n\t\tif(buf[0] == 'l'){\n\n\t\t\tscanf(\"%lf %lf\",&x,&y);\n\t\t\tP.push_back(Point(x,y));\n\n\t\t}else if(buf[0] == 'a'){ //円弧命令\n\n\t\t\tscanf(\"%lf %lf %lf\",&x,&y,&r);\n\n\t\t\tpre = P[P.size()-1];\n\t\t\tnext = Point(x,y);\n\n\t\t\tC1.center = pre;\n\t\t\tC1.r = fabs(r);\n\n\t\t\tC2.center = next;\n\t\t\tC2.r = fabs(r);\n\n\t\t\t//pre.debug();\n\t\t\t//next.debug();\n\n\t\t\tvector<Point> crossV = getCrossPoints(C1,C2);\n\n\t\t\tif(r < 0){\n\n\t\t\t\tif(ccw(pre,next,crossV[0]) == COUNTER_CLOCKWISE){\n\n\t\t\t\t\tcalc_center = crossV[0];\n\n\t\t\t\t}else{\n\n\t\t\t\t\tcalc_center = crossV[1];\n\t\t\t\t}\n\n\t\t\t}else{ //r > 0\n\n\t\t\t\tif(ccw(pre,next,crossV[0]) == CLOCKWISE){\n\n\t\t\t\t\tcalc_center = crossV[0];\n\n\t\t\t\t}else{\n\n\t\t\t\t\tcalc_center = crossV[1];\n\t\t\t\t}\n\t\t\t}\n\n\t\t\t//printf(\"calc_center:\");\n\t\t\t//calc_center.debug();\n\n\t\t\tVector vec1 = pre-calc_center;\n\t\t\tVector vec2 = next-calc_center;\n\n\t\t\tdouble rad = acos(dot(vec1,vec2)/(abs(vec1)*abs(vec2)));\n\t\t\tdouble base_rad = rad/LOOP;\n\t\t\tif(r > 0)base_rad *= -1; //時計回り\n\n\t\t\t//点preを、calc_centerの周りにLOOP回回転させる\n\t\t\tfor(int i = 0; i < LOOP; i++){\n\n\t\t\t\tPoint tmp_p = rotate(calc_center,pre,base_rad*(i+1));\n\t\t\t\tP.push_back(tmp_p);\n\t\t\t}\n\t\t\tP.push_back(next);\n\n\t\t}else if(buf[0] == 'c'){ //close\n\n\t\t\tbreak;\n\t\t}\n\t}\n\n\tPoint center = calc_center_of_gravity(P);\n\n\tbool is_in = (contains(P,center) != 0);\n\n\tchar ch = '-';\n\tif(is_in){\n\n\t\tch = '+';\n\t}\n\n\tprintf(\"%.10lf %.10lf %c\\n\",center.x,center.y,ch);\n}\n\n\nint main(){\n\n\twhile(true){\n\n\t\tscanf(\"%s\",buf);\n\t\tif(buf[0] == 'e')break;\n\n\t\tfunc();\n\t}\n\n\treturn 0;\n}", "accuracy": 1, "time_ms": 1500, "memory_kb": 33684, "score_of_the_acc": -2, "final_rank": 9 }, { "submission_id": "aoj_1292_3236636", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\n#define rep(i,n) for(int (i)=0;(i)<(int)(n);++(i))\n#define all(x) (x).begin(),(x).end()\n#define pb push_back\n#define fi first\n#define se second\n#define dbg(x) cout<<#x\" = \"<<((x))<<endl\ntemplate<class T,class U> ostream& operator<<(ostream& o, const pair<T,U> &p){o<<\"(\"<<p.fi<<\",\"<<p.se<<\")\";return o;}\ntemplate<class T> ostream& operator<<(ostream& o, const vector<T> &v){o<<\"[\";for(T t:v){o<<t<<\",\";}o<<\"]\";return o;}\n\n/* 基本要素 */\ntypedef complex<double> Point;\ntypedef pair<Point, Point> Line;\ntypedef vector<Point> VP;\nconst double PI = acos(-1);\nconst double EPS = 1e-7; // 許容誤差^2\nconst double INF = 1e9;\n#define X real()\n#define Y imag()\n// #define LE(n,m) ((n) < (m) + EPS)\n#define LE(n,m) ((n) - (m) < EPS)\n// #define GE(n,m) ((n) + EPS > (m))\n#define GE(n,m) (EPS > (m) - (n))\n#define EQ(n,m) (abs((n)-(m)) < EPS)\n\nusing Vec = Point;\n\n// 内積 dot(a,b) = |a||b|cosθ\ndouble dot(Point a, Point b) {\n return a.X*b.X + a.Y*b.Y;\n}\n\n// 外積 cross(a,b) = |a||b|sinθ\ndouble cross(Point a, Point b) {\n return a.X*b.Y - a.Y*b.X;\n}\n\n// 点の進行方向\n// !!! 誤差に注意 !!! (掛け算したものとかなり小さいものを比べているので)\nint ccw(Point a, Point b, Point c) {\n b -= a; c -= a;\n if (cross(b,c) > EPS) return +1; // counter clockwise\n if (cross(b,c) < -EPS) return -1; // clockwise\n if (dot(b,c) < -EPS) return +2; // c--a--b on line\n if (norm(b) < norm(c)) return -2; // a--b--c on line or a==b\n return 0; // a--c--b on line or a==c or b==c\n}\n\n// 直線と線分\nbool isecLS(Point a1, Point a2, Point b1, Point b2) {\n return cross(a2-a1, b1-a1) * cross(a2-a1, b2-a1) < EPS;\n}\n\n\n// 2直線の交点\nPoint crosspointLL(Point a1, Point a2, Point b1, Point b2) {\n double d1 = cross(b2-b1, b1-a1);\n double d2 = cross(b2-b1, a2-a1);\n if (EQ(d1, 0) && EQ(d2, 0)) return a1; // same line\n assert(!EQ(d2, 0)); // 交点がない\n return a1 + d1/d2 * (a2-a1);\n}\n\n// 点pの直線aへの射影点を返す\nPoint proj(Point a1, Point a2, Point p) {\n return a1 + dot(a2-a1, p-a1)/norm(a2-a1) * (a2-a1);\n}\n\nVP crosspointLC(Point a1, Point a2, Point c, double r) {\n VP ps;\n Point ft = proj(a1, a2, c);\n if(!GE(r*r,norm(ft-c))) return ps;\n Point dir = sqrt(max(r*r - norm(ft-c), 0.0)) / abs(a2-a1) * (a2-a1);\n ps.push_back(ft + dir);\n if (!EQ(r*r, norm(ft-c))) ps.push_back(ft - dir);\n return ps;\n}\n\n// 多角形の符号付面積\ndouble area(const VP& ps) {\n double a = 0;\n rep (i, ps.size()) a += cross(ps[i], ps[(i+1) % ps.size()]);\n return a / 2;\n}\n\n// 多角形の幾何学的重心\nPoint centroid(const VP& ps) {\n int n = ps.size();\n double aSum = 0;\n Point c;\n rep (i, n) {\n double a = cross(ps[i], ps[(i+1) % n]);\n aSum += a;\n c += (ps[i] + ps[(i+1) % n]) * a;\n }\n return 1 / aSum / 3 * c;\n}\n\n// aからbへの回転角(中心(0,0))[-pi,+pi]\ndouble angle(Point a,Point b){\n double t = arg(b)-arg(a);\n while(t>+PI) t-=2*PI;\n while(t<-PI) t+=2*PI;\n return t;\n}\n\nbool on_arc(Point c, Point a, Point b, Point p){\n b -= c;\n a -= c;\n p -= c;\n c -= c;\n\n double ta = atan2(a.Y,a.X), tb = atan2(b.Y,b.X);\n while(tb - ta < 0){\n tb += 2*PI;\n }\n if(tb - ta > PI){\n swap(ta,tb);\n }\n while(tb - ta < 0){\n tb += 2*PI;\n }\n\n for(int i=-2; i<=2; ++i){\n double tmp = atan2(p.Y,p.X) + i*2*PI;\n if(ta-EPS<tmp && tmp<tb+EPS) return true;\n }\n return false;\n}\n\ndouble cb(double x){\n return x*x*x;\n}\n\nPoint READ(){\n int x,y;\n cin >>x >>y;\n return Point(x,y);\n}\n\nint main(){\n string s;\n while(cin >>s, (s==\"start\")){\n Point start = READ();\n\n VP poly;\n vector<int> arc;\n\n poly.pb(start);\n while(cin >>s){\n if(s == \"close\"){\n arc.pb(0);\n break;\n }\n\n poly.pb(READ());\n\n int rr = 0;\n if(s == \"arc\") cin >>rr;\n arc.pb(rr);\n }\n\n int n = poly.size();\n\n // calc center of mass\n Point pg = centroid(poly);\n double poly_area = area(poly);\n\n Point g = poly_area*pg;\n double mm = poly_area;\n\n VP arc_center(n);\n rep(i,n)if(arc[i]!=0){\n Point a = poly[i], b = poly[(i+1)%n];\n Point mid = (a+b)*0.5;\n\n Vec v = b-a;\n Vec h = {-v.Y, v.X};\n double hsz = abs(h);\n h = {h.X/hsz, h.Y/hsz};\n\n double chord = abs(b-a);\n double theta = asin(chord/2/abs(arc[i]));\n double len = abs(arc[i])*cos(theta);\n\n // determine the center of the arc\n Point c1 = mid + len*h;\n Point c2 = mid - len*h;\n if(arc[i]<0){\n if( angle(a-c1, b-c1) > EPS) arc_center[i] = c1;\n else arc_center[i] = c2;\n }\n else{\n if( angle(a-c1, b-c1) < -EPS) arc_center[i] = c1;\n else arc_center[i] = c2;\n }\n\n VP cross_points = crosspointLC(arc_center[i], mid, arc_center[i], abs(arc[i]));\n for(Point tmp:cross_points){\n if(on_arc(arc_center[i], a, b, tmp)){\n double A = arc[i]*arc[i]*(2*theta - sin(2*theta))/2;\n double y = 2*cb(abs(arc[i]))*cb(sin(theta))/3/A;\n Point tgp = arc_center[i] + (mid-arc_center[i])*(y/abs(mid-arc_center[i]));\n\n double mul = 1;\n if(arc[i]>0) mul = -1;\n g += tgp*(A*mul);\n mm += A*mul;\n break;\n }\n }\n }\n g *= (1.0/mm);\n\n // is center inside?\n int yct = 0;\n for(int yy=-200; yy<=200; ++yy){\n\n Point GP(200, yy);\n // dbg(GP);\n VP cps;\n rep(i,n){\n Point a = poly[i], b = poly[(i+1)%n];\n if(arc[i] == 0){\n // dbg(a);\n // dbg(b);\n if(isecLS(g,GP,a,b)){\n Point cp = crosspointLL(g,GP,a,b);\n // dbg(cp);\n if(cp.X - g.X > 0) cps.pb(cp);\n }\n }\n else{\n for(Point cp:crosspointLC(g,GP,arc_center[i],abs(arc[i]))){\n if(on_arc(arc_center[i],a,b,cp)){\n if(cp.X - g.X > 0) cps.pb(cp);\n }\n }\n }\n }\n\n VP uniq_cps;\n for(Point p:cps){\n bool ok = true;\n for(Point q:uniq_cps){\n if(abs(p-q)<EPS) ok = false;\n }\n if(ok) uniq_cps.pb(p);\n }\n // dbg(uniq_cps);\n if(uniq_cps.size()%2==0) ++yct;\n else --yct;\n }\n // dbg(yct);\n\n char inside = '+';\n if(yct>0) inside = '-';\n\n printf(\"%.10f %.10f %c\\n\", g.X, g.Y, inside);\n }\n return 0;\n}", "accuracy": 1, "time_ms": 120, "memory_kb": 3564, "score_of_the_acc": -0.1391, "final_rank": 6 }, { "submission_id": "aoj_1292_3083318", "code_snippet": "#include <iostream>\n#include <iomanip>\n#include <complex>\n#include <vector>\n#include <algorithm>\n#include <cmath>\n#include <array>\nusing namespace std;\nconst double EPS = 1e-8;\nconst double INF = 1e12;\nconst double PI = acos(-1);\n#define EQ(n,m) (abs((n)-(m)) < EPS)\n#define X real()\n#define Y imag()\n\ntypedef complex<double> P;\ntypedef vector<P> VP;\nstruct L : array<P, 2>{\n L(const P& a, const P& b){ at(0)=a; at(1)=b; }\n L(){}\n};\nstruct C{\n P p;\n double r;\n C(const P& p, const double& r) : p(p), r(r) {}\n C(){}\n};\n\nnamespace std{\n bool operator < (const P& a, const P& b){\n return !EQ(a.X,b.X) ? a.X<b.X : a.Y+EPS<b.Y;\n }\n bool operator == (const P& a, const P& b){\n return abs(a-b) < EPS;\n }\n}\n\ndouble dot(P a, P b){\n return (conj(a)*b).X;\n}\ndouble cross(P a, P b){\n return (conj(a)*b).Y;\n}\nint ccw(P a, P b, P c){\n b -= a;\n c -= a;\n if(cross(b,c) > EPS) return +1; //ccw\n if(cross(b,c) < -EPS) return -1; //cw\n if(dot(b,c) < -EPS) return +2; //c-a-b\n if(abs(c)-abs(b) > EPS) return -2; //a-b-c\n return 0; //a-c-b\n}\n\nP unit(const P &p){\n return p/abs(p);\n}\nP rotate(const P &p, double rad){\n return p *P(cos(rad), sin(rad));\n}\n\nbool intersectSP(const L& s, const P &p){\n return abs(cross(s[0]-p, s[1]-p))<EPS && dot(s[0]-p, s[1]-p)<EPS;\n}\n\nVP crosspointCC(C a, C b){\n VP ret;\n if(a.r < b.r) swap(a,b);\n double dist = abs(b.p-a.p);\n P dir = a.r*unit(b.p-a.p);\n if(EQ(dist, a.r+b.r) || EQ(dist, a.r-b.r)){\n ret.push_back(a.p +dir);\n }else if(a.r-b.r < dist && dist < a.r+b.r){\n double cos = (a.r*a.r +dist*dist -b.r*b.r)/(2*a.r*dist);\n double sin = sqrt(1 -cos*cos);\n ret.push_back(a.p +dir*P(cos, sin));\n ret.push_back(a.p +dir*P(cos, -sin));\n }\n return ret;\n}\n\nP centroid(VP &poly){\n int n = poly.size();\n P ret = P(0, 0);\n double total = 0;\n for(int i=0; i<n; i++){\n P &curr = poly[i], &next = poly[(i+1)%n];\n ret += (curr +next) *cross(curr, next);\n total += cross(curr, next);\n }\n return ret /total /3.0;\n}\n\nint in_poly(const P &p, const VP &poly){\n int n = poly.size();\n int ret = -1;\n for(int i=0; i<n; i++){\n P a = poly[i]-p;\n P b = poly[(i+1)%n]-p;\n if(a.Y > b.Y) swap(a,b);\n if(intersectSP(L(a,b), P(0,0))) return 0;\n if(a.Y<=0 && b.Y>0 && cross(a,b)<0) ret = -ret;\n }\n return ret;\n}\n\nint main(){\n while(1){\n string s;\n cin >> s;\n if(s == \"end\") break;\n\n double x,y;\n cin >> x >> y;\n VP poly;\n poly.emplace_back(x, y);\n while(1){\n string s;\n cin >> s;\n if(s == \"close\") break;\n if(s == \"line\"){\n double x,y;\n cin >> x >> y;\n poly.emplace_back(x, y);\n }\n if(s == \"arc\"){\n double x,y,r;\n cin >> x >> y >> r;\n int sign = (r > 0)? -1: 1;\n P curr = poly.back(), next = P(x, y);\n C c[2] = {C(curr, abs(r)), C(next, abs(r))};\n VP cp = crosspointCC(c[0], c[1]);\n P center = (ccw(curr, next, cp[0]) == sign)? cp[0]: cp[1];\n double angle = arg((next-center) /(curr-center));\n int split = 10000;\n for(int i=1; i<=split; i++){\n poly.push_back(center +rotate(curr -center, angle *i/split));\n }\n }\n }\n\n P center = centroid(poly);\n string in = (in_poly(center, poly) >= 0)? \"+\" :\"-\";\n cout << fixed << setprecision(5);\n cout << center.X << \" \" << center.Y << \" \" << in << endl;\n\n }\n return 0;\n}", "accuracy": 1, "time_ms": 110, "memory_kb": 10396, "score_of_the_acc": -0.3444, "final_rank": 8 }, { "submission_id": "aoj_1292_1384661", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\ntypedef complex<double> P;\ntypedef pair<P,P> L;\n\nconst double EPS = 1e-7;\n\nstruct Circle{\n P c;\n double r;\n Circle(P c=P(0,0), double r=0):c(c),r(r){}\n};\n\nstruct Arc{\n double r, theta, area;\n P begin_p, end_p, cen;\n Arc(double r, double t, P bp, P ep, P c):r(r),theta(t),begin_p(bp),end_p(ep),cen(c){}\n};\n\nbool equal(double a, double b){ return fabs(a-b) < EPS; }\n\ndouble dot(P a, P b){ return real(conj(a)*b); }\n\ndouble cross(P a, P b){ return imag(conj(a)*b); }\n\nint ccw(P a, P b, P c){\n b -= a;\n c -= a;\n if(cross(b,c) > EPS) return 1;\n if(cross(b,c) < -EPS) return -1;\n if(dot(b,c) < -EPS) return 2;\n if(norm(b) < norm(c)) return -2;\n return 0;\n}\n\ndouble area(vector<P> v){\n double sum = 0.0;\n int n = v.size();\n for(int i=0;i<n;i++) sum += (real(v[i]) - real(v[(i+1)%n])) * (imag(v[i]) + imag(v[(i+1)%n]));\n return fabs(sum) / 2;\n};\n\ndouble area(Arc ar){\n return ar.r * ar.r * (ar.theta - sin(ar.theta)) / 2.0;\n}\n\nP rotate(P p, double rad){\n double x = real(p) * cos(rad) - imag(p) * sin(rad);\n double y = real(p) * sin(rad) + imag(p) * cos(rad);\n return P(x,y);\n}\n\nbool isIntersect(L s1, L s2){\n \n if(max(real(s1.first), real(s1.second)) + EPS < min(real(s2.first), real(s2.second)) ||\n max(imag(s1.first), imag(s1.second)) + EPS < min(imag(s2.first), imag(s2.second)) ||\n max(real(s2.first), real(s2.second)) + EPS < min(real(s1.first), real(s1.second)) ||\n max(imag(s2.first), imag(s2.second)) + EPS < min(imag(s1.first), imag(s1.second))) return false;\n\n return ( ccw(s1.first,s1.second,s2.first) * ccw(s1.first,s1.second,s2.second) <= 0 &&\n ccw(s2.first,s2.second,s1.first) * ccw(s2.first,s2.second,s1.second) <= 0 );\n}\n\nenum {OUT, BORDER, IN};\nint contains(vector<P> v, P p){ \n bool in = false; \n for(int i=0;i<v.size();i++){ \n P a = v[i] - p; \n P b = v[(i+1)%v.size()] - p; \n if(imag(a) > imag(b)) swap(a,b); \n if(imag(a) <= EPS && EPS < imag(b)) \n if(cross(a,b) < -EPS) in = !in; \n if(equal(cross(a,b), 0.0) && dot(a,b) < EPS) return 1; \n } \n return in ? 2 : 0; \n}\n\nbool contains(Arc ar, P p){\n return abs(ar.cen - p) < ar.r - EPS && isIntersect(L(ar.cen, p), L(ar.begin_p, ar.end_p));\n}\n\nvector<P> getIntersectCC(Circle c1, Circle c2){\n vector<P> res;\n double r1 = c1.r, r2 = c2.r, d;\n P p1 = c1.c, p2 = c2.c;\n d = abs(p1-p2);\n\n if(d < EPS && abs(r1-r2) < EPS) return res;\n if(r1 + r2< d - EPS || d + EPS < abs(r1-r2)) return res;\n\n double a = (r1*r1 - r2*r2 + d*d) / (2*d);\n double h = sqrt(max(r1*r1 - a*a, 0.0));\n P tmp1 = p1 + a / d * (p2-p1);\n P tmp2 = h / d * (p2-p1);\n\n if(abs(tmp2) < EPS) res.push_back(tmp1);\n else {\n res.push_back(P(real(tmp1) - imag(tmp2), imag(tmp1) + real(tmp2)));\n res.push_back(P(real(tmp1) + imag(tmp2), imag(tmp1) - real(tmp2)));\n }\n return res;\n}\n\nP calc_g(vector<P> v){\n double a = 0;\n int n = v.size();\n P res = P(0, 0);\n for(int i=0;i<n;i++){\n double k = real(v[i]) * imag(v[(i+1)%n]) - real(v[(i+1)%n]) * imag(v[i]);\n res += k * (v[i] + v[(i+1)%n]);\n a += k;\n }\n //cout << res << ' ' << res / 3.0 << ' ' << res / 3.0 / a << endl;\n return res / 3.0 / a;\n}\n\nP calc_g(Arc ar){\n double A = ar.r * ar.r * (ar.theta - sin(ar.theta)) / 2.0;\n double y = 2.0 * ar.r * ar.r * ar.r * sin(ar.theta/2.0) * sin(ar.theta/2.0) * sin(ar.theta/2.0) / (3.0 * A);\n double theta = arg((ar.begin_p + ar.end_p) / 2.0 - ar.cen);\n \n return ar.cen + polar(y, theta);\n}\n\n\n\nvector<P> poly;\nvector<Arc> ar_v;\n\nvoid start(P p){\n poly.clear();\n ar_v.clear();\n poly.push_back(p);\n}\n\nvoid arc(P p, double r){\n bool assert_flg = false, rev_flg = false;\n Arc ar = Arc(abs(r), 0, poly[poly.size()-1], p, P()); \n vector<P> res_inter = getIntersectCC(Circle(ar.begin_p, ar.r), Circle(ar.end_p, ar.r));\n\n for(int i=0;i<res_inter.size();i++){\n if(ccw(ar.begin_p, ar.end_p, res_inter[i]) == -1 && r > -EPS ||\n ccw(ar.begin_p, ar.end_p, res_inter[i]) == 1 && r < EPS){\n ar.cen = res_inter[i];\n assert(!assert_flg);\n assert_flg = true;\n }\n }\n assert(assert_flg);\n\n \n ar.theta = arg(ar.end_p-ar.cen) - arg(ar.begin_p-ar.cen);\n while(ar.theta < -EPS) ar.theta += 2.0 * M_PI;\n if(ar.theta > M_PI + EPS) {\n ar.theta = 2.0 * M_PI - ar.theta;\n rev_flg = true;\n }\n\n //cout << ar.cen << endl;\n //printf(\"arg_en = %.5f, arg_be = %.5f, arg = %.5f\\n\", arg(ar.end_p-ar.cen)*180.0/M_PI, arg(ar.begin_p-ar.cen)*180.0/M_PI, ar.theta*180.0/M_PI);\n\n\n ar_v.push_back(ar);\n \n // g?????????????????£????????§?????¨????????????\n int LOOP = 3600;\n for(int i=0;i<LOOP;i++){\n if(rev_flg) poly.push_back(ar.cen + polar(ar.r, arg(ar.begin_p-ar.cen) - ar.theta * (double)i / LOOP));\n else poly.push_back(ar.cen + polar(ar.r, arg(ar.begin_p-ar.cen) + ar.theta * (double)i / LOOP));\n } \n \n poly.push_back(p);\n}\n\nvoid line(P p){\n poly.push_back(p);\n}\n\nvoid close(){\n P ans = calc_g(poly) * area(poly);\n double sum_area = area(poly);\n\n for(int i=0;i<ar_v.size();i++){\n ar_v[i].area = area(ar_v[i]);\n if(contains(poly, calc_g(ar_v[i])) == IN) ar_v[i].area *= -1.0;\n //ans += calc_g(ar_v[i]) * ar_v[i].area;\n //sum_area += ar_v[i].area;\n }\n\n ans /= sum_area;\n\n if(equal(real(ans), 0.0)) ans = P(0, imag(ans));\n if(equal(imag(ans), 0.0)) ans = P(real(ans), 0);\n \n printf(\"%.4f %.4f \", real(ans), imag(ans));\n\n /* \n for(int i=0;i<ar_v.size();i++){\n if(contains(ar_v[i], ans)){\n cout << (ar_v[i].area > EPS ? '+' : '-') << endl;\n return;\n }\n }\n */\n cout << (contains(poly, ans) == IN ? '+' : '-') << endl;\n}\n\nint main(){\n double x, y, r;\n string str;\n while(cin >> str){\n if(str == \"start\") {\n cin >> x >> y;\n start(P(x, y));\n }\n else if(str == \"line\"){\n cin >> x >> y;\n line(P(x, y));\n }\n else if(str == \"arc\"){\n cin >> x >> y >> r;\n arc(P(x, y), r);\n }\n else if(str == \"close\") close();\n else break;\n }\n}", "accuracy": 1, "time_ms": 210, "memory_kb": 5284, "score_of_the_acc": -0.2529, "final_rank": 7 }, { "submission_id": "aoj_1292_1150665", "code_snippet": "#include <cstdio>\n#include <cmath>\n#include <cstring>\n#include <cstdlib>\n#include <climits>\n#include <ctime>\n#include <queue>\n#include <stack>\n#include <algorithm>\n#include <list>\n#include <vector>\n#include <set>\n#include <map>\n#include <iostream>\n#include <deque>\n#include <complex>\n#include <string>\n#include <iomanip>\n#include <sstream>\n#include <bitset>\n#include <valarray>\n#include <unordered_map>\n#include <iterator>\n#include <assert.h>\nusing namespace std;\ntypedef long long int ll;\ntypedef unsigned int uint;\ntypedef unsigned char uchar;\ntypedef unsigned long long ull;\ntypedef pair<int, int> pii;\ntypedef pair<ll, ll> pll;\ntypedef vector<int> vi;\n\n#define REP(i,x) for(int i=0;i<(int)(x);i++)\n#define REPS(i,x) for(int i=1;i<=(int)(x);i++)\n#define RREP(i,x) for(int i=((int)(x)-1);i>=0;i--)\n#define RREPS(i,x) for(int i=((int)(x));i>0;i--)\n#define FOR(i,c) for(__typeof((c).begin())i=(c).begin();i!=(c).end();i++)\n#define RFOR(i,c) for(__typeof((c).rbegin())i=(c).rbegin();i!=(c).rend();i++)\n#define ALL(container) (container).begin(), (container).end()\n#define RALL(container) (container).rbegin(), (container).rend()\n#define SZ(container) ((int)container.size())\n#define mp(a,b) make_pair(a, b)\n#define UNIQUE(v) v.erase( unique(v.begin(), v.end()), v.end() );\n\ntemplate<class T> bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }\ntemplate<class T> bool chmin(T &a, const T &b) { if (a>b) { a=b; return 1; } return 0; }\ntemplate<class T> ostream& operator<<(ostream &os, const vector<T> &t) {\nos<<\"[\"; FOR(it,t) {if(it!=t.begin()) os<<\",\"; os<<*it;} os<<\"]\"; return os;\n}\ntemplate<class T> ostream& operator<<(ostream &os, const set<T> &t) {\nos<<\"{\"; FOR(it,t) {if(it!=t.begin()) os<<\",\"; os<<*it;} os<<\"}\"; return os;\n}\ntemplate<class S, class T> ostream& operator<<(ostream &os, const pair<S,T> &t) { return os<<\"(\"<<t.first<<\",\"<<t.second<<\")\";}\ntemplate<class S, class T> pair<S,T> operator+(const pair<S,T> &s, const pair<S,T> &t){ return pair<S,T>(s.first+t.first, s.second+t.second);}\ntemplate<class S, class T> pair<S,T> operator-(const pair<S,T> &s, const pair<S,T> &t){ return pair<S,T>(s.first-t.first, s.second-t.second);}\n\nnamespace geom{\n#define X real()\n#define Y imag()\n#define at(i) ((*this)[i])\n#define SELF (*this)\n\tenum {TRUE = 1, FALSE = 0, BORDER = -1};\n\ttypedef int BOOL;\n\ttypedef double R;\n\tconst R INF = 1e8;\n\tR EPS = 1e-6;\n\tconst R PI = 3.1415926535897932384626;\n\tinline int sig(const R &x) { return (abs(x) < EPS ? 0 : x > 0 ? 1 : -1); }\n\tinline BOOL less(const R &x, const R &y) {return sig(x-y) ? x < y : BORDER;}\n\ttypedef complex<R> P;\n\tinline R norm(const P &p){return p.X*p.X+p.Y*p.Y;}\n\tinline R inp(const P& a, const P& b){return (conj(a)*b).X;}\n\tinline R outp(const P& a, const P& b){return (conj(a)*b).Y;}\n\tinline P unit(const P& p){return p/abs(p);}\n\tinline P proj(const P &s, const P &t){return t*inp(s, t)/norm(t);}\n\tinline int ccw(const P &s, const P &t, const P &p, int adv=0){\n\t\tint res = sig(outp(t-s, p-s));\n\t\tif(res || !adv) return res;\n\t\tif(sig(inp(t-s, p-s)) < 0) return -2;\t// p-s-t\n\t\tif(sig(inp(s-t, p-t)) < 0) return 2;\t// s-t-p\n\t\treturn 0;\t\t\t\t\t\t\t\t// s-p-t\n\t}\n\t\n\t\n\tstruct L : public vector<P>{\t// line\n\t\tL(const P &p1, const P &p2){this->push_back(p1);this->push_back(p2);}\n\t\tL(){}\n\t\tP dir()const {return at(1) - at(0);}\n\t\tBOOL online(const P &p)const {return !sig(outp(p-at(0), dir()));}\n\t};\n\tstruct S : public L{\t// segment\n\t\tS(const P &p1, const P &p2):L(p1, p2){}\n\t\tS(){}\n\t\tBOOL online(const P &p)const {\n\t\t\tif(!sig(norm(p - at(0))) || !sig(norm(p - at(1)))) return BORDER;\n\t\t\treturn !sig(abs(at(0)-p) + abs(at(1) - p) - abs(at(0) - at(1)));\n\t\t}\n\t};\n\tstruct C : public P{\n\t\tC(){}\n\t\tC(const P& p, const R r):P(p), r(r){}\n\t\tR r;\n\t\tBOOL inside(const P& p)const { return less(norm(p-SELF), r*r);}\n\t};\n\tstruct F : public C{\n\t\tR s, t;\n\t\tF(const C &c, R ss, R tt):C(c), s(ss), t(tt){\n\t\t\tif(PI < s) s -= 2*PI;\n\t\t\tif(PI < t) t -= 2*PI;\n\t\t}\n\t\tBOOL inside(const P& p)const {\n\t\t\tP v = p - SELF;\n\t\t\tif(!sig(norm(v))) return BORDER;\n\t\t\tR a = arg(v);\n\t\t\tif(t < s){\n\t\t\t\tif((!less(s, a) && !less(a, t)) || !less(norm(v), r*r)) return FALSE;\n\t\t\t\treturn less(s, a) | less(a, t) | less(norm(v), r*r);\n\t\t\t}else{\n\t\t\t\tif(!less(s, a) || !less(a, t) || !less(norm(v), r*r)) return FALSE;\n\t\t\t\treturn less(s, a) | less(a, t) | less(norm(v), r*r);\n\t\t\t}\n\t\t}\n\t};\n\tP crosspoint(const L &l, const L &m);\n\tstruct G : public vector<P>{\n\t\tG(size_type size=0):vector(size){}\n\t\tS edge(int i)const {return S(at(i), at(i+1 == size() ? 0 : i+1));}\n\t\tBOOL contains(const P &p)const {\n\t\t\tR sum = .0;\n\t\t\tREP(i, size()){\n\t\t\t\tif(S(at(i), at((i+1)%size())).online(p)) return BORDER;\t// online\n\t\t\t\tsum += arg((at(i) - p) / (at((i+1)%size()) - p));\n\t\t\t}\n\t\t\treturn !!sig(sum);\n\t\t}\n\t\tR area()const {\n\t\t\tR sum = 0;\n\t\t\tREP(i, size()) sum += outp(at(i), at((i+1)%size()));\n\t\t\treturn abs(sum / 2.);\n\t\t}\n\t\tP gp()const {\n\t\t\tP r(.0, .0);\n\t\t\tREP(i, size()){\n\t\t\t\tconst S &s = edge(i);\n\t\t\t\tr += (s[0]+s[1])*outp(s[0], s[1]);\n\t\t\t}\n\t\t\treturn r / (6*area());\n\t\t}\n\t\tG convex_hull(bool online = false) {\n\t\t\tif(size() < 2) return *this;\n\t\t\tsort(ALL(*this));\n\t\t\tG r;\n\t\t\tr.resize((int)size()*2);\n\t\t\tint k=0;\n\t\t\tfor(int i=0;i<size();r[k++]=at(i++))\n\t\t\t\twhile(k>1 && ccw(r[k-2], r[k-1], at(i)) < 1-online) k--;\n\t\t\tint t = k;\n\t\t\tfor(int i=(int)size()-1;i>=0;r[k++]=at(i--))\n\t\t\t\twhile(k>t && ccw(r[k-2], r[k-1], at(i)) < 1-online) k--;\n\t\t\tr.resize(k-1);\n\t\t\treturn r;\n\t\t}\n\t\tG cut(const L &l)const {\n\t\t\tG g;\n\t\t\tREP(i, size()){\n\t\t\t\tconst S &s = edge(i);\n\t\t\t\tif(ccw(l[0], l[1], s[0], 0) >= 0) g.push_back(s[0]);\n\t\t\t\tif(ccw(l[0], l[1], s[0], 0) * ccw(l[0], l[1], s[1], 0) < 0)\n\t\t\t\t\tg.push_back(crosspoint(s, l));\n\t\t\t}\n\t\t\treturn g;\n\t\t}\n\t\tG Voronoi(const vector<P> &p, const int t)const {\n\t\t\tG g = *this;\n\t\t\tREP(i, p.size())if(i!=t){\n\t\t\t\tconst P m = (p[t]+p[i])*0.5;\n\t\t\t\tg = g.cut(L(m, m+(p[i]-p[t])*P(0, 1)));\n\t\t\t}\n\t\t\treturn g;\n\t\t}\n\t};\n\n\tinline P proj(const P &s, const L &t){return t[0] + proj(s-t[0], t[1]-t[0]);}\n\tinline P reflect(const P &s, const L &t){return 2.*proj(s, t) - s;}\n\tinline S reflect(const S &s, const L &t){return S(reflect(s[0], t), reflect(s[1], t));}\n\tBOOL intersect(const S &s, const S &t){\n\t\tconst int p = ccw(t[0], t[1], s[0], 1) * ccw(t[0], t[1], s[1], 1);\n\t\tconst int q = ccw(s[0], s[1], t[0], 1) * ccw(s[0], s[1], t[1], 1);\n\t\treturn (p>0||q>0) ? FALSE : (!p||!q) ? BORDER : TRUE;\n\t}\n\tBOOL intersect(const S &s, const L &l){\n\t\tif(l.online(s[0]) || l.online(s[1])) return BORDER;\n\t\treturn (sig(outp(l.dir(), s[0]-l[0])) * sig(outp(l.dir(), s[1]-l[0])) <= 0);\n\t}\n\tR dist2(const L &l, const P &p){return norm(outp(l.dir(), p - l[0])) / norm(l.dir());}\n\tR dist2(const S &s, const P &p){\n\t\tif(inp(p-s[0], s.dir()) < EPS) return norm(p - s[0]);\n\t\tif(inp(p-s[1], -s.dir()) < EPS) return norm(p - s[1]);\n\t\treturn dist2((const L &)s, p);\n\t}\n\tR dist2(const S &s, const L &l){\n\t\treturn intersect(s, l) ? .0 : min(dist2(l, s[0]), dist2(l, s[1]));\n\t}\n\tR dist2(const S &s, const S &t){\n\t\treturn intersect(s, t) ? .0 : min(min(dist2(s, t[0]), dist2(t, s[0])), \n\t\t\t\t\t\t\t\t\t \t min(dist2(s, t[1]), dist2(t, s[1])));\n\t}\n\ttemplate <class T> R dist2(const G &g, const T& t){ // todo: 内部に完全に含まれる場合\n\t\tR res = INF;\n\t\tREP(i, g.size()) res = min(res, dist2(g.edge(i), t));\n\t\treturn res;\n\t}\n\ttemplate<class S, class T> R dist(const S& s, const T& t){return sqrt(dist2(s, t));}\n\tinline BOOL intersect(const C &a, const C &b){\n\t\treturn less((a.r-b.r)*(a.r-b.r), norm(a-b)) + less(norm(a-b), (a.r+b.r)*(a.r+b.r)) - 1;\n\t}\n\tinline BOOL intersect(const C &c, const L &l){\n\t\treturn less(dist2(l, c), c.r*c.r);\n\t}\n\tinline BOOL intersect(const C &c, const S &s){\n\t\tint d = less(dist2(s, c), c.r*c.r);\n\t\tif(d != TRUE) return d;\n\t\tint p = c.inside(s[0]), q = c.inside(s[1]);\n\t\treturn (p<0 || q<0) ? BORDER : p&q;\n\t}\n\t\n\t/*\n\t * CCWでs[0]->s[1]にかけてが共通部分\n\t */\n\tinline S crosspoint(const C &c1, const C &c2){\n\t\tif(!intersect(c1, c2)) return S();\n\t\tR d = abs(c1 - c2);\n\t\tR x = (c1.r*c1.r - c2.r*c2.r + d*d) / (2*d);\n\t\tR h = sqrt(max(0., c1.r*c1.r - x*x));\n\t\tP u = unit(c2-c1);\n\t\treturn S(c1 + u*x + u*P(0,-1)*h, c1 + u*x + u*P(0,1)*h);\n\t}\n\tinline P crosspoint(const L &l, const L &m){\n\t\tR A = outp(l.dir(), m.dir()), B = outp(l.dir(), l[1] - m[0]);\n\t\tif(!sig(abs(A)) && !sig(abs(B))) return m[0]; // same line\n\t\tif(abs(A) < EPS) assert(false); // !!!PRECONDITION NOT SATISFIED!!!\n\t\treturn m[0] + B / A * (m[1] - m[0]);\n\t}\n\tinline S crosspoint(const C &c, const L &l){\n\t\tR d2 = dist2(l, c);\n\t\tif(c.r*c.r+EPS < d2) return S();\n\t\tP m = proj(c, l);\n\t\tP u = unit(l[1]-l[0]);\n\t\tR d = sqrt(max(.0, c.r*c.r - d2));\n\t\treturn S(m+u*d, m-u*d);\n\t}\n\tinline vector<P> crosspoint(const C &c, const S &s){\n\t\tvector<P> res = crosspoint(c, (const L&)s);\n\t\tRREP(i, res.size()){\n\t\t\tif(inp(res[i]-s[0], s.dir())*inp(res[i]-s[1], -s.dir())<EPS)\n\t\t\t\tres.erase(res.begin() + i);\n\t\t}\n\t\treturn res;\n\t}\n\tinline R commonarea(const C &a, const C &b){\n\t\tif(less(norm(a-b), (a.r-b.r)*(a.r-b.r)) == TRUE) return min(a.r*a.r, b.r*b.r)*PI;\n\t\tif(less((a.r+b.r)*(a.r+b.r), norm(a-b)) == TRUE) return .0;\n\t\tdouble d = abs(a-b);\n\t\tdouble rc = (d*d + a.r*a.r - b.r*b.r) / (2*d);\n\t\tdouble theta = acos(rc / a.r);\n\t\tdouble phi = acos((d - rc) / b.r);\n\t\treturn a.r*a.r*theta + b.r*b.r*phi - d*a.r*sin(theta);\n\t}\n\tvector<L> CommonTangent(C c1, C c2){\n\t\tif(c1.r > c2.r) swap(c1, c2);\n\t\tdouble d = abs(c1-c2);\n\t\tvector<L> res;\n\t\tif(d < EPS) return res;\n\t\tif(d + EPS > c1.r + c2.r){\n\t\t\t// 内接線\n\t\t\tP crs = (c1*c2.r + c2*c1.r) / (c1.r + c2.r);\n\t\t\tdouble rad = asin(c1.r/abs(crs-c1));\n\t\t\tres.push_back(L(crs, crs + (c1-crs)*polar(1., rad)));\n\t\t\tres.push_back(L(crs, crs + (c1-crs)*polar(1., -rad)));\n\t\t}\n\t\tif(c1.r + d + EPS > c2.r){\n\t\t\t// 外接線\n\t\t\tdouble rad = 0.5*PI+asin((c2.r-c1.r) / d);\n\t\t\tP v = unit(c2-c1)*polar(1., rad);\n\t\t\tif(c1.r + d - EPS < c2.r){\n\t\t\t\tres.push_back(L(c1+v*c1.r, c1+v*c1.r+(c1-c2)*P(0, 1)));\n\t\t\t}else{\n\t\t\t\tres.push_back(L(c1+v*c1.r, c2+v*c2.r));\n\t\t\t\tv = 2.*proj(v, c2-c1) - v;\n\t\t\t\tres.push_back(L(c1+v*c1.r, c2+v*c2.r));\n\t\t\t}\n\t\t}\n\t\treturn res;\n\t}\n\t\n\tstruct min_ball {\n\t\tP center;\n\t\tR radius2;\n\t\tmin_ball(const vector<P>& p) {\n\t\t\tFOR(it, p) ps.push_back(*it);\n\t\t}\n\t\tmin_ball& compile() {\n\t\t\tm = 0;\n\t\t\tcenter = P(0, 0);\n\t\t\tradius2 = -1;\n\t\t\tmake_ball(ps.end());\n\t\t\treturn *this;\n\t\t}\n\tprivate:\n\t\tlist<P> ps;\n\t\tlist<P>::iterator supp_end;\n\t\tint m;\n\t\tP v[3], c[3];\n\t\tR z[3], r[3];\n\t\tvoid pop() { --m; }\n\t\tvoid push(const P& p) {\n\t\t\tif (m == 0) {\n\t\t\t\tc[0] = p; r[0] = 0;\n\t\t\t} else {\n\t\t\t\tR e = norm(p-c[m-1]) - r[m-1];\n\t\t\t\tP delta = p - c[0];\n\t\t\t\tv[m] = p - c[0];\n\t\t\t\tfor (int i = 1; i < m; ++i)\n\t\t\t\t\tv[m] -= v[i] * inp(v[i], delta) / z[i];\n\t\t\t\tz[m] = inp(v[m], v[m]);\n\t\t\t\tc[m] = c[m-1] + e*v[m]/z[m]*.5;\n\t\t\t\tr[m] = r[m-1] + e*e/z[m]*.25;\n\t\t\t}\n\t\t\tcenter\t= c[m];\n\t\t\tradius2 = r[m]; ++m;\n\t\t}\n\t\tvoid make_ball(list<P>::iterator i) {\n\t\t\tsupp_end = ps.begin();\n\t\t\tif (m == 3) return;\n\t\t\tfor (list<P>::iterator k = ps.begin(); k != i; ) {\n\t\t\t\tlist<P>::iterator j = k++;\n\t\t\t\tif (norm(*j-center) > radius2) {\n\t\t\t\t\tpush(*j); make_ball(j); pop();\n\t\t\t\t\tmove_to_front(j);\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\tvoid move_to_front(list<P>::iterator j) {\n\t\t\tif (supp_end == j) ++supp_end;\n\t\t\tps.splice (ps.begin(), ps, j);\n\t\t}\n\t};\n\t\n\tstruct Arrangement{\n\t\tstruct AEdge{\n\t\t\tint u, v, t;\n\t\t\tR cost;\n\t\t\tAEdge(int u=0, int v=0, int t=0, R cost=0)\n\t\t\t\t:u(u), v(v), t(t), cost(cost){}\n\t\t};\n\t\ttypedef vector<vector<AEdge>> AGraph;\n\t\tvector<P> p;\n\t\tAGraph g;\n\t\tArrangement(){}\n\t\tArrangement(vector<S> seg){\n\t\t\tint m = seg.size();\n\t\t\tREP(i, m){\n\t\t\t\tp.push_back(seg[i][0]);\n\t\t\t\tp.push_back(seg[i][1]);\n\t\t\t\tREP(j, i) if(sig(outp(seg[i].dir(), seg[j].dir())) && intersect(seg[i], seg[j]) == TRUE)\n\t\t\t\t\tp.push_back(crosspoint(seg[i], seg[j]));\n\t\t\t}\n\t\t\tsort(ALL(p)); UNIQUE(p);\n\t\t\tint n=p.size();\n\t\t\tg.resize(n);\n\t\t\tREP(i, m){\n\t\t\t\tS &s = seg[i];\n\t\t\t\tvector<pair<R, int>> ps;\n\t\t\t\tREP(j, n) if(s.online(p[j])) ps.emplace_back(norm(p[j] - s[0]), j);\n\t\t\t\tsort(ALL(ps));\n\t\t\t\tREP(j, (int)ps.size()-1){\n\t\t\t\t\tconst int u=ps[j].second;\n\t\t\t\t\tconst int v=ps[j+1].second;\n\t\t\t\t\tg[u].emplace_back(u, v, 0, abs(p[u] - p[v]));\n\t\t\t\t\tg[v].emplace_back(v, u, 0, abs(p[u] - p[v]));\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\t\n\t\tint getIdx(P q){\n\t\t\tauto it = lower_bound(ALL(p), q);\n\t\t\tif(it == p.end() || *it != q) return -1;\n\t\t\treturn it - p.begin();\n\t\t}\n\t};\n/*\n\tstruct DualGraph{\n\t\tstruct DEdge{\n\t\t\tint u, v, f, l;\n\t\t\tR a;\n\t\t\tDEdge(int u, int v, R a):u(u),v(v),f(0),l(0){\n\t\t\t\twhile(PI < a) a -= 2*PI;\n\t\t\t\twhile(a < -PI) a += 2*PI;\n\t\t\t\tthis->a = a;\n\t\t\t}\n\t\t\tbool operator==(const DEdge &opp) const{\n\t\t\t\treturn v==opp.v;\n\t\t\t}\n\t\t\tbool operator<(const DEdge &opp) const{\n\t\t\t\treturn a>opp.a;\n\t\t\t}\n\t\t\tbool operator<(const R &opp) const{\n\t\t\t\treturn a>opp;\n\t\t\t}\n\t\t\tfriend ostream& operator<<(ostream &os, const DEdge &t) { return os<<\"(\"<<t.u<<\",\"<<t.v<<\",\"<<t.a*180/PI<<\")\";}\n\t\t};\n\t\t\n\t\tint n;\n\t\tvector<P> p;\n\t\tvector<vector<DEdge>> g;\n\t\tUnionFind uf;\n\t\tDualGraph(const vector<P> &p):p(p),g(p.size()),n(p.size()),uf(p.size()){}\n\t\tvoid add_edge(const int s, const int t){\n\t\t\tR a = arg(p[t]-p[s]);\n\t\t\tg[s].emplace_back(s, t, a);\n\t\t\tg[t].emplace_back(t, s, a > 0 ? a-PI : a+PI);\n\t\t\tuf.unionSet(s, t);\n\t\t}\n\t\tvector<G> poly;\n\t\t\n\t\tvoid add_polygon(int s, int t, R a){\n\t\t\tauto e = lower_bound(ALL(g[s]), a-EPS);\n\t\t\tif(e == g[s].end()) e = g[s].begin();\n\t\t\tif(e->f) return;\n\t\t\te->f = 1;\n\t\t\te->l = t;\n\t\t\tpoly[t].push_back(p[s]);\n\t\t\tadd_polygon(e->v, t, e->a > 0 ? e->a-PI : e->a+PI);\n\t\t}\n\t\t\n\t\tvoid toSpanningTree(){\n\t\t\tvector<pair<R, pii>> e;\n\t\t\tREP(i, n)REP(j, i)if(!uf.findSet(i, j))\n\t\t\t\te.emplace_back(norm(p[i]-p[j]), pii(i, j));\n\t\t\tsort(ALL(e));\n\t\t\tREP(ii, e.size()){\n\t\t\t\tconst int i = e[ii].second.first;\n\t\t\t\tconst int j = e[ii].second.second;\n\t\t\t\tif(uf.findSet(i, j)) continue;\n\t\t\t\tconst S s(p[i], p[j]);\n\t\t\t\tif([&](){\n\t\t\t\t\tREP(k, n)if(i!=k&&j!=k){\n\t\t\t\t\t\tFOR(it, g[k])\n\t\t\t\t\t\tif(intersect(s, S(p[k], p[it->v])) == TRUE) return 0;\n\t\t\t\t\t}\n\t\t\t\t\treturn 1;\n\t\t\t\t}()) add_edge(i, j);\n\t\t\t}\n\t\t}\n\t\t\n\t\tvector<vi> dual(){\n\t\t\ttoSpanningTree();\n\t\t\t\n\t\t\tREP(i, n){\n\t\t\t\tsort(ALL(g[i]));\n\t\t\t\tUNIQUE(g[i]);\n\t\t\t}\n\t\t\tint s = min_element(ALL(p)) - p.begin();\n\t\t\tpoly.emplace_back();\n\t\t\tadd_polygon(s, poly.size()-1, -PI*.5);\n\t\t\tREP(i, n)REP(j, g[i].size())if(!g[i][j].f){\n\t\t\t\tpoly.emplace_back();\n\t\t\t\tadd_polygon(i, poly.size()-1, g[i][j].a+2.*EPS);\n\t\t\t}\n\t\t\tint m = poly.size();\n\t\t\tvector<vi> dg(m);\n\t\t\tvector<unordered_map<int, int>> rev(n);\n\t\t\tREP(i, n)REP(j, g[i].size()){\n\t\t\t\tint u = i, v = g[i][j].v, l = g[i][j].l;\n\t\t\t\tif(u>v) swap(u, v);\n\t\t\t\tif(rev[u].find(v) != rev[u].end()){\n\t\t\t\t\tif(l != rev[u][v]){\n\t\t\t\t\t\tdg[l].push_back(rev[u][v]);\n\t\t\t\t\t\tdg[rev[u][v]].push_back(l);\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\telse rev[u][v] = l;\n\t\t\t}\n\t\t\tREP(i, m){\n\t\t\t\tsort(ALL(dg[i]));\n\t\t\t\tUNIQUE(dg[i]);\n\t\t\t}\n\t\t\treturn dg;\n\t\t}\n\t};\n*/\n#undef SELF\n#undef at\n}\n\nusing namespace geom;\n\nint f = 0;\nnamespace std{\n\tbool operator<(const P &a, const P &b){return sig(a.X-b.X) ? a.X < b.X : a.Y+EPS < b.Y;}\n\tbool operator==(const P &a, const P &b){return abs(a-b) < EPS;}\n\tistream& operator>>(istream &is, P &p){R x,y;is>>x>>y;p=P(x, y);return is;}\n\tistream& operator>>(istream &is, L &l){l.resize(2);return is >> l[0] >> l[1];}\n\tistream& operator>>(istream &is, C &c){return is >> (P &)c >> c.r;}\n\tconst double B = 200;\n\tconst double Z = 80;\n\tostream& operator<<(ostream &os, const C &c){return os << \"circle(\"<<B+Z*(c.X)<<\", \"<<1000-B-Z*(c.Y)<<\", \"<<Z*(c.r)<<\")\";}\n\tostream& operator<<(ostream &os, const P &p){return os << C(p, 2./Z);}\n\tostream& operator<<(ostream &os, const S &s){return os << \"line(\"<<B+Z*(s[0].X)<<\", \"<<1000-B-Z*(s[0].Y)<<\", \"<<B+Z*(s[1].X)<<\", \"<<1000-B-Z*(s[1].Y)<<\")\";}\n\tostream& operator<<(ostream &os, const G &g){REP(i, g.size()) cout << g.edge(i) << endl;return os;}\n\n}\n\nint n;\n\nconst int BL = 100;\n\nint main(){\n\tios::sync_with_stdio(false);\n\tstring com;\n\twhile(1){\n\t\tP prev;\n\t\tG g;\n\t\twhile(cin >> com, com != \"close\"){\n\t\t\tif(com == \"end\") return 0;\n\t\t\tif(com == \"arc\"){\n\t\t\t\tC c;\n\t\t\t\tcin >> c;\n\t\t\t\tP ort(0., c.r < 0 ? 1. : -1.);\n\t\t\t\tR r = sqrt(max(0., c.r*c.r - norm((c-prev)*.5)));\n\t\t\t\tP b = .5*(prev+c) + unit(c-prev)*ort*r;\n\t\t\t\tR s = arg(prev - b);\n\t\t\t\tR t = arg(c - b);\n\t\t\t\tif(t-s > PI) s += 2*PI;\n\t\t\t\tif(s-t > PI) t += 2*PI;\n\t\t\t\tREPS(i, BL-1){\n\t\t\t\t\tR m = (i*t + (BL-i)*s) / BL;\n\t\t\t\t\tg.push_back(b + polar(abs(c.r), m));\n\t\t\t\t}\n\t\t\t\tprev = c;\n\t\t\t}else{\n\t\t\t\tcin >> prev;\n\t\t\t}\n\t\t\tg.push_back(prev);\n\t\t}\n\t\tP gp = g.gp();\n\t\tprintf(\"%.10f %.10f %c\\n\", gp.X, gp.Y, g.contains(gp) ? '+' : '-');\n\t}\n\treturn 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 1460, "score_of_the_acc": 0, "final_rank": 1 }, { "submission_id": "aoj_1292_1150664", "code_snippet": "#include <cstdio>\n#include <cmath>\n#include <cstring>\n#include <cstdlib>\n#include <climits>\n#include <ctime>\n#include <queue>\n#include <stack>\n#include <algorithm>\n#include <list>\n#include <vector>\n#include <set>\n#include <map>\n#include <iostream>\n#include <deque>\n#include <complex>\n#include <string>\n#include <iomanip>\n#include <sstream>\n#include <bitset>\n#include <valarray>\n#include <unordered_map>\n#include <iterator>\n#include <assert.h>\nusing namespace std;\ntypedef long long int ll;\ntypedef unsigned int uint;\ntypedef unsigned char uchar;\ntypedef unsigned long long ull;\ntypedef pair<int, int> pii;\ntypedef pair<ll, ll> pll;\ntypedef vector<int> vi;\n\n#define REP(i,x) for(int i=0;i<(int)(x);i++)\n#define REPS(i,x) for(int i=1;i<=(int)(x);i++)\n#define RREP(i,x) for(int i=((int)(x)-1);i>=0;i--)\n#define RREPS(i,x) for(int i=((int)(x));i>0;i--)\n#define FOR(i,c) for(__typeof((c).begin())i=(c).begin();i!=(c).end();i++)\n#define RFOR(i,c) for(__typeof((c).rbegin())i=(c).rbegin();i!=(c).rend();i++)\n#define ALL(container) (container).begin(), (container).end()\n#define RALL(container) (container).rbegin(), (container).rend()\n#define SZ(container) ((int)container.size())\n#define mp(a,b) make_pair(a, b)\n#define UNIQUE(v) v.erase( unique(v.begin(), v.end()), v.end() );\n\ntemplate<class T> bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }\ntemplate<class T> bool chmin(T &a, const T &b) { if (a>b) { a=b; return 1; } return 0; }\ntemplate<class T> ostream& operator<<(ostream &os, const vector<T> &t) {\nos<<\"[\"; FOR(it,t) {if(it!=t.begin()) os<<\",\"; os<<*it;} os<<\"]\"; return os;\n}\ntemplate<class T> ostream& operator<<(ostream &os, const set<T> &t) {\nos<<\"{\"; FOR(it,t) {if(it!=t.begin()) os<<\",\"; os<<*it;} os<<\"}\"; return os;\n}\ntemplate<class S, class T> ostream& operator<<(ostream &os, const pair<S,T> &t) { return os<<\"(\"<<t.first<<\",\"<<t.second<<\")\";}\ntemplate<class S, class T> pair<S,T> operator+(const pair<S,T> &s, const pair<S,T> &t){ return pair<S,T>(s.first+t.first, s.second+t.second);}\ntemplate<class S, class T> pair<S,T> operator-(const pair<S,T> &s, const pair<S,T> &t){ return pair<S,T>(s.first-t.first, s.second-t.second);}\n\nnamespace geom{\n#define X real()\n#define Y imag()\n#define at(i) ((*this)[i])\n#define SELF (*this)\n\tenum {TRUE = 1, FALSE = 0, BORDER = -1};\n\ttypedef int BOOL;\n\ttypedef double R;\n\tconst R INF = 1e8;\n\tR EPS = 1e-6;\n\tconst R PI = 3.1415926535897932384626;\n\tinline int sig(const R &x) { return (abs(x) < EPS ? 0 : x > 0 ? 1 : -1); }\n\tinline BOOL less(const R &x, const R &y) {return sig(x-y) ? x < y : BORDER;}\n\ttypedef complex<R> P;\n\tinline R norm(const P &p){return p.X*p.X+p.Y*p.Y;}\n\tinline R inp(const P& a, const P& b){return (conj(a)*b).X;}\n\tinline R outp(const P& a, const P& b){return (conj(a)*b).Y;}\n\tinline P unit(const P& p){return p/abs(p);}\n\tinline P proj(const P &s, const P &t){return t*inp(s, t)/norm(t);}\n\tinline int ccw(const P &s, const P &t, const P &p, int adv=0){\n\t\tint res = sig(outp(t-s, p-s));\n\t\tif(res || !adv) return res;\n\t\tif(sig(inp(t-s, p-s)) < 0) return -2;\t// p-s-t\n\t\tif(sig(inp(s-t, p-t)) < 0) return 2;\t// s-t-p\n\t\treturn 0;\t\t\t\t\t\t\t\t// s-p-t\n\t}\n\t\n\t\n\tstruct L : public vector<P>{\t// line\n\t\tL(const P &p1, const P &p2){this->push_back(p1);this->push_back(p2);}\n\t\tL(){}\n\t\tP dir()const {return at(1) - at(0);}\n\t\tBOOL online(const P &p)const {return !sig(outp(p-at(0), dir()));}\n\t};\n\tstruct S : public L{\t// segment\n\t\tS(const P &p1, const P &p2):L(p1, p2){}\n\t\tS(){}\n\t\tBOOL online(const P &p)const {\n\t\t\tif(!sig(norm(p - at(0))) || !sig(norm(p - at(1)))) return BORDER;\n\t\t\treturn !sig(abs(at(0)-p) + abs(at(1) - p) - abs(at(0) - at(1)));\n\t\t}\n\t};\n\tstruct C : public P{\n\t\tC(){}\n\t\tC(const P& p, const R r):P(p), r(r){}\n\t\tR r;\n\t\tBOOL inside(const P& p)const { return less(norm(p-SELF), r*r);}\n\t};\n\tstruct F : public C{\n\t\tR s, t;\n\t\tF(const C &c, R ss, R tt):C(c), s(ss), t(tt){\n\t\t\tif(PI < s) s -= 2*PI;\n\t\t\tif(PI < t) t -= 2*PI;\n\t\t}\n\t\tBOOL inside(const P& p)const {\n\t\t\tP v = p - SELF;\n\t\t\tif(!sig(norm(v))) return BORDER;\n\t\t\tR a = arg(v);\n\t\t\tif(t < s){\n\t\t\t\tif((!less(s, a) && !less(a, t)) || !less(norm(v), r*r)) return FALSE;\n\t\t\t\treturn less(s, a) | less(a, t) | less(norm(v), r*r);\n\t\t\t}else{\n\t\t\t\tif(!less(s, a) || !less(a, t) || !less(norm(v), r*r)) return FALSE;\n\t\t\t\treturn less(s, a) | less(a, t) | less(norm(v), r*r);\n\t\t\t}\n\t\t}\n\t};\n\tP crosspoint(const L &l, const L &m);\n\tstruct G : public vector<P>{\n\t\tG(size_type size=0):vector(size){}\n\t\tS edge(int i)const {return S(at(i), at(i+1 == size() ? 0 : i+1));}\n\t\tBOOL contains(const P &p)const {\n\t\t\tR sum = .0;\n\t\t\tREP(i, size()){\n\t\t\t\tif(S(at(i), at((i+1)%size())).online(p)) return BORDER;\t// online\n\t\t\t\tsum += arg((at(i) - p) / (at((i+1)%size()) - p));\n\t\t\t}\n\t\t\treturn !!sig(sum);\n\t\t}\n\t\tR area()const {\n\t\t\tR sum = 0;\n\t\t\tREP(i, size()) sum += outp(at(i), at((i+1)%size()));\n\t\t\treturn abs(sum / 2.);\n\t\t}\n\t\tP gp()const {\n\t\t\tP r(.0, .0);\n\t\t\tREP(i, size()){\n\t\t\t\tconst S &s = edge(i);\n\t\t\t\tr += (s[0]+s[1])*outp(s[0], s[1]);\n\t\t\t}\n\t\t\treturn r / (6*area());\n\t\t}\n\t\tG convex_hull(bool online = false) {\n\t\t\tif(size() < 2) return *this;\n\t\t\tsort(ALL(*this));\n\t\t\tG r;\n\t\t\tr.resize((int)size()*2);\n\t\t\tint k=0;\n\t\t\tfor(int i=0;i<size();r[k++]=at(i++))\n\t\t\t\twhile(k>1 && ccw(r[k-2], r[k-1], at(i)) < 1-online) k--;\n\t\t\tint t = k;\n\t\t\tfor(int i=(int)size()-1;i>=0;r[k++]=at(i--))\n\t\t\t\twhile(k>t && ccw(r[k-2], r[k-1], at(i)) < 1-online) k--;\n\t\t\tr.resize(k-1);\n\t\t\treturn r;\n\t\t}\n\t\tG cut(const L &l)const {\n\t\t\tG g;\n\t\t\tREP(i, size()){\n\t\t\t\tconst S &s = edge(i);\n\t\t\t\tif(ccw(l[0], l[1], s[0], 0) >= 0) g.push_back(s[0]);\n\t\t\t\tif(ccw(l[0], l[1], s[0], 0) * ccw(l[0], l[1], s[1], 0) < 0)\n\t\t\t\t\tg.push_back(crosspoint(s, l));\n\t\t\t}\n\t\t\treturn g;\n\t\t}\n\t\tG Voronoi(const vector<P> &p, const int t)const {\n\t\t\tG g = *this;\n\t\t\tREP(i, p.size())if(i!=t){\n\t\t\t\tconst P m = (p[t]+p[i])*0.5;\n\t\t\t\tg = g.cut(L(m, m+(p[i]-p[t])*P(0, 1)));\n\t\t\t}\n\t\t\treturn g;\n\t\t}\n\t};\n\n\tinline P proj(const P &s, const L &t){return t[0] + proj(s-t[0], t[1]-t[0]);}\n\tinline P reflect(const P &s, const L &t){return 2.*proj(s, t) - s;}\n\tinline S reflect(const S &s, const L &t){return S(reflect(s[0], t), reflect(s[1], t));}\n\tBOOL intersect(const S &s, const S &t){\n\t\tconst int p = ccw(t[0], t[1], s[0], 1) * ccw(t[0], t[1], s[1], 1);\n\t\tconst int q = ccw(s[0], s[1], t[0], 1) * ccw(s[0], s[1], t[1], 1);\n\t\treturn (p>0||q>0) ? FALSE : (!p||!q) ? BORDER : TRUE;\n\t}\n\tBOOL intersect(const S &s, const L &l){\n\t\tif(l.online(s[0]) || l.online(s[1])) return BORDER;\n\t\treturn (sig(outp(l.dir(), s[0]-l[0])) * sig(outp(l.dir(), s[1]-l[0])) <= 0);\n\t}\n\tR dist2(const L &l, const P &p){return norm(outp(l.dir(), p - l[0])) / norm(l.dir());}\n\tR dist2(const S &s, const P &p){\n\t\tif(inp(p-s[0], s.dir()) < EPS) return norm(p - s[0]);\n\t\tif(inp(p-s[1], -s.dir()) < EPS) return norm(p - s[1]);\n\t\treturn dist2((const L &)s, p);\n\t}\n\tR dist2(const S &s, const L &l){\n\t\treturn intersect(s, l) ? .0 : min(dist2(l, s[0]), dist2(l, s[1]));\n\t}\n\tR dist2(const S &s, const S &t){\n\t\treturn intersect(s, t) ? .0 : min(min(dist2(s, t[0]), dist2(t, s[0])), \n\t\t\t\t\t\t\t\t\t \t min(dist2(s, t[1]), dist2(t, s[1])));\n\t}\n\ttemplate <class T> R dist2(const G &g, const T& t){ // todo: 内部に完全に含まれる場合\n\t\tR res = INF;\n\t\tREP(i, g.size()) res = min(res, dist2(g.edge(i), t));\n\t\treturn res;\n\t}\n\ttemplate<class S, class T> R dist(const S& s, const T& t){return sqrt(dist2(s, t));}\n\tinline BOOL intersect(const C &a, const C &b){\n\t\treturn less((a.r-b.r)*(a.r-b.r), norm(a-b)) + less(norm(a-b), (a.r+b.r)*(a.r+b.r)) - 1;\n\t}\n\tinline BOOL intersect(const C &c, const L &l){\n\t\treturn less(dist2(l, c), c.r*c.r);\n\t}\n\tinline BOOL intersect(const C &c, const S &s){\n\t\tint d = less(dist2(s, c), c.r*c.r);\n\t\tif(d != TRUE) return d;\n\t\tint p = c.inside(s[0]), q = c.inside(s[1]);\n\t\treturn (p<0 || q<0) ? BORDER : p&q;\n\t}\n\t\n\t/*\n\t * CCWでs[0]->s[1]にかけてが共通部分\n\t */\n\tinline S crosspoint(const C &c1, const C &c2){\n\t\tif(!intersect(c1, c2)) return S();\n\t\tR d = abs(c1 - c2);\n\t\tR x = (c1.r*c1.r - c2.r*c2.r + d*d) / (2*d);\n\t\tR h = sqrt(max(0., c1.r*c1.r - x*x));\n\t\tP u = unit(c2-c1);\n\t\treturn S(c1 + u*x + u*P(0,-1)*h, c1 + u*x + u*P(0,1)*h);\n\t}\n\tinline P crosspoint(const L &l, const L &m){\n\t\tR A = outp(l.dir(), m.dir()), B = outp(l.dir(), l[1] - m[0]);\n\t\tif(!sig(abs(A)) && !sig(abs(B))) return m[0]; // same line\n\t\tif(abs(A) < EPS) assert(false); // !!!PRECONDITION NOT SATISFIED!!!\n\t\treturn m[0] + B / A * (m[1] - m[0]);\n\t}\n\tinline S crosspoint(const C &c, const L &l){\n\t\tR d2 = dist2(l, c);\n\t\tif(c.r*c.r+EPS < d2) return S();\n\t\tP m = proj(c, l);\n\t\tP u = unit(l[1]-l[0]);\n\t\tR d = sqrt(max(.0, c.r*c.r - d2));\n\t\treturn S(m+u*d, m-u*d);\n\t}\n\tinline vector<P> crosspoint(const C &c, const S &s){\n\t\tvector<P> res = crosspoint(c, (const L&)s);\n\t\tRREP(i, res.size()){\n\t\t\tif(inp(res[i]-s[0], s.dir())*inp(res[i]-s[1], -s.dir())<EPS)\n\t\t\t\tres.erase(res.begin() + i);\n\t\t}\n\t\treturn res;\n\t}\n\tinline R commonarea(const C &a, const C &b){\n\t\tif(less(norm(a-b), (a.r-b.r)*(a.r-b.r)) == TRUE) return min(a.r*a.r, b.r*b.r)*PI;\n\t\tif(less((a.r+b.r)*(a.r+b.r), norm(a-b)) == TRUE) return .0;\n\t\tdouble d = abs(a-b);\n\t\tdouble rc = (d*d + a.r*a.r - b.r*b.r) / (2*d);\n\t\tdouble theta = acos(rc / a.r);\n\t\tdouble phi = acos((d - rc) / b.r);\n\t\treturn a.r*a.r*theta + b.r*b.r*phi - d*a.r*sin(theta);\n\t}\n\tvector<L> CommonTangent(C c1, C c2){\n\t\tif(c1.r > c2.r) swap(c1, c2);\n\t\tdouble d = abs(c1-c2);\n\t\tvector<L> res;\n\t\tif(d < EPS) return res;\n\t\tif(d + EPS > c1.r + c2.r){\n\t\t\t// 内接線\n\t\t\tP crs = (c1*c2.r + c2*c1.r) / (c1.r + c2.r);\n\t\t\tdouble rad = asin(c1.r/abs(crs-c1));\n\t\t\tres.push_back(L(crs, crs + (c1-crs)*polar(1., rad)));\n\t\t\tres.push_back(L(crs, crs + (c1-crs)*polar(1., -rad)));\n\t\t}\n\t\tif(c1.r + d + EPS > c2.r){\n\t\t\t// 外接線\n\t\t\tdouble rad = 0.5*PI+asin((c2.r-c1.r) / d);\n\t\t\tP v = unit(c2-c1)*polar(1., rad);\n\t\t\tif(c1.r + d - EPS < c2.r){\n\t\t\t\tres.push_back(L(c1+v*c1.r, c1+v*c1.r+(c1-c2)*P(0, 1)));\n\t\t\t}else{\n\t\t\t\tres.push_back(L(c1+v*c1.r, c2+v*c2.r));\n\t\t\t\tv = 2.*proj(v, c2-c1) - v;\n\t\t\t\tres.push_back(L(c1+v*c1.r, c2+v*c2.r));\n\t\t\t}\n\t\t}\n\t\treturn res;\n\t}\n\t\n\tstruct min_ball {\n\t\tP center;\n\t\tR radius2;\n\t\tmin_ball(const vector<P>& p) {\n\t\t\tFOR(it, p) ps.push_back(*it);\n\t\t}\n\t\tmin_ball& compile() {\n\t\t\tm = 0;\n\t\t\tcenter = P(0, 0);\n\t\t\tradius2 = -1;\n\t\t\tmake_ball(ps.end());\n\t\t\treturn *this;\n\t\t}\n\tprivate:\n\t\tlist<P> ps;\n\t\tlist<P>::iterator supp_end;\n\t\tint m;\n\t\tP v[3], c[3];\n\t\tR z[3], r[3];\n\t\tvoid pop() { --m; }\n\t\tvoid push(const P& p) {\n\t\t\tif (m == 0) {\n\t\t\t\tc[0] = p; r[0] = 0;\n\t\t\t} else {\n\t\t\t\tR e = norm(p-c[m-1]) - r[m-1];\n\t\t\t\tP delta = p - c[0];\n\t\t\t\tv[m] = p - c[0];\n\t\t\t\tfor (int i = 1; i < m; ++i)\n\t\t\t\t\tv[m] -= v[i] * inp(v[i], delta) / z[i];\n\t\t\t\tz[m] = inp(v[m], v[m]);\n\t\t\t\tc[m] = c[m-1] + e*v[m]/z[m]*.5;\n\t\t\t\tr[m] = r[m-1] + e*e/z[m]*.25;\n\t\t\t}\n\t\t\tcenter\t= c[m];\n\t\t\tradius2 = r[m]; ++m;\n\t\t}\n\t\tvoid make_ball(list<P>::iterator i) {\n\t\t\tsupp_end = ps.begin();\n\t\t\tif (m == 3) return;\n\t\t\tfor (list<P>::iterator k = ps.begin(); k != i; ) {\n\t\t\t\tlist<P>::iterator j = k++;\n\t\t\t\tif (norm(*j-center) > radius2) {\n\t\t\t\t\tpush(*j); make_ball(j); pop();\n\t\t\t\t\tmove_to_front(j);\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\tvoid move_to_front(list<P>::iterator j) {\n\t\t\tif (supp_end == j) ++supp_end;\n\t\t\tps.splice (ps.begin(), ps, j);\n\t\t}\n\t};\n\t\n\tstruct Arrangement{\n\t\tstruct AEdge{\n\t\t\tint u, v, t;\n\t\t\tR cost;\n\t\t\tAEdge(int u=0, int v=0, int t=0, R cost=0)\n\t\t\t\t:u(u), v(v), t(t), cost(cost){}\n\t\t};\n\t\ttypedef vector<vector<AEdge>> AGraph;\n\t\tvector<P> p;\n\t\tAGraph g;\n\t\tArrangement(){}\n\t\tArrangement(vector<S> seg){\n\t\t\tint m = seg.size();\n\t\t\tREP(i, m){\n\t\t\t\tp.push_back(seg[i][0]);\n\t\t\t\tp.push_back(seg[i][1]);\n\t\t\t\tREP(j, i) if(sig(outp(seg[i].dir(), seg[j].dir())) && intersect(seg[i], seg[j]) == TRUE)\n\t\t\t\t\tp.push_back(crosspoint(seg[i], seg[j]));\n\t\t\t}\n\t\t\tsort(ALL(p)); UNIQUE(p);\n\t\t\tint n=p.size();\n\t\t\tg.resize(n);\n\t\t\tREP(i, m){\n\t\t\t\tS &s = seg[i];\n\t\t\t\tvector<pair<R, int>> ps;\n\t\t\t\tREP(j, n) if(s.online(p[j])) ps.emplace_back(norm(p[j] - s[0]), j);\n\t\t\t\tsort(ALL(ps));\n\t\t\t\tREP(j, (int)ps.size()-1){\n\t\t\t\t\tconst int u=ps[j].second;\n\t\t\t\t\tconst int v=ps[j+1].second;\n\t\t\t\t\tg[u].emplace_back(u, v, 0, abs(p[u] - p[v]));\n\t\t\t\t\tg[v].emplace_back(v, u, 0, abs(p[u] - p[v]));\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\t\n\t\tint getIdx(P q){\n\t\t\tauto it = lower_bound(ALL(p), q);\n\t\t\tif(it == p.end() || *it != q) return -1;\n\t\t\treturn it - p.begin();\n\t\t}\n\t};\n/*\n\tstruct DualGraph{\n\t\tstruct DEdge{\n\t\t\tint u, v, f, l;\n\t\t\tR a;\n\t\t\tDEdge(int u, int v, R a):u(u),v(v),f(0),l(0){\n\t\t\t\twhile(PI < a) a -= 2*PI;\n\t\t\t\twhile(a < -PI) a += 2*PI;\n\t\t\t\tthis->a = a;\n\t\t\t}\n\t\t\tbool operator==(const DEdge &opp) const{\n\t\t\t\treturn v==opp.v;\n\t\t\t}\n\t\t\tbool operator<(const DEdge &opp) const{\n\t\t\t\treturn a>opp.a;\n\t\t\t}\n\t\t\tbool operator<(const R &opp) const{\n\t\t\t\treturn a>opp;\n\t\t\t}\n\t\t\tfriend ostream& operator<<(ostream &os, const DEdge &t) { return os<<\"(\"<<t.u<<\",\"<<t.v<<\",\"<<t.a*180/PI<<\")\";}\n\t\t};\n\t\t\n\t\tint n;\n\t\tvector<P> p;\n\t\tvector<vector<DEdge>> g;\n\t\tUnionFind uf;\n\t\tDualGraph(const vector<P> &p):p(p),g(p.size()),n(p.size()),uf(p.size()){}\n\t\tvoid add_edge(const int s, const int t){\n\t\t\tR a = arg(p[t]-p[s]);\n\t\t\tg[s].emplace_back(s, t, a);\n\t\t\tg[t].emplace_back(t, s, a > 0 ? a-PI : a+PI);\n\t\t\tuf.unionSet(s, t);\n\t\t}\n\t\tvector<G> poly;\n\t\t\n\t\tvoid add_polygon(int s, int t, R a){\n\t\t\tauto e = lower_bound(ALL(g[s]), a-EPS);\n\t\t\tif(e == g[s].end()) e = g[s].begin();\n\t\t\tif(e->f) return;\n\t\t\te->f = 1;\n\t\t\te->l = t;\n\t\t\tpoly[t].push_back(p[s]);\n\t\t\tadd_polygon(e->v, t, e->a > 0 ? e->a-PI : e->a+PI);\n\t\t}\n\t\t\n\t\tvoid toSpanningTree(){\n\t\t\tvector<pair<R, pii>> e;\n\t\t\tREP(i, n)REP(j, i)if(!uf.findSet(i, j))\n\t\t\t\te.emplace_back(norm(p[i]-p[j]), pii(i, j));\n\t\t\tsort(ALL(e));\n\t\t\tREP(ii, e.size()){\n\t\t\t\tconst int i = e[ii].second.first;\n\t\t\t\tconst int j = e[ii].second.second;\n\t\t\t\tif(uf.findSet(i, j)) continue;\n\t\t\t\tconst S s(p[i], p[j]);\n\t\t\t\tif([&](){\n\t\t\t\t\tREP(k, n)if(i!=k&&j!=k){\n\t\t\t\t\t\tFOR(it, g[k])\n\t\t\t\t\t\tif(intersect(s, S(p[k], p[it->v])) == TRUE) return 0;\n\t\t\t\t\t}\n\t\t\t\t\treturn 1;\n\t\t\t\t}()) add_edge(i, j);\n\t\t\t}\n\t\t}\n\t\t\n\t\tvector<vi> dual(){\n\t\t\ttoSpanningTree();\n\t\t\t\n\t\t\tREP(i, n){\n\t\t\t\tsort(ALL(g[i]));\n\t\t\t\tUNIQUE(g[i]);\n\t\t\t}\n\t\t\tint s = min_element(ALL(p)) - p.begin();\n\t\t\tpoly.emplace_back();\n\t\t\tadd_polygon(s, poly.size()-1, -PI*.5);\n\t\t\tREP(i, n)REP(j, g[i].size())if(!g[i][j].f){\n\t\t\t\tpoly.emplace_back();\n\t\t\t\tadd_polygon(i, poly.size()-1, g[i][j].a+2.*EPS);\n\t\t\t}\n\t\t\tint m = poly.size();\n\t\t\tvector<vi> dg(m);\n\t\t\tvector<unordered_map<int, int>> rev(n);\n\t\t\tREP(i, n)REP(j, g[i].size()){\n\t\t\t\tint u = i, v = g[i][j].v, l = g[i][j].l;\n\t\t\t\tif(u>v) swap(u, v);\n\t\t\t\tif(rev[u].find(v) != rev[u].end()){\n\t\t\t\t\tif(l != rev[u][v]){\n\t\t\t\t\t\tdg[l].push_back(rev[u][v]);\n\t\t\t\t\t\tdg[rev[u][v]].push_back(l);\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\telse rev[u][v] = l;\n\t\t\t}\n\t\t\tREP(i, m){\n\t\t\t\tsort(ALL(dg[i]));\n\t\t\t\tUNIQUE(dg[i]);\n\t\t\t}\n\t\t\treturn dg;\n\t\t}\n\t};\n*/\n#undef SELF\n#undef at\n}\n\nusing namespace geom;\n\nint f = 0;\nnamespace std{\n\tbool operator<(const P &a, const P &b){return sig(a.X-b.X) ? a.X < b.X : a.Y+EPS < b.Y;}\n\tbool operator==(const P &a, const P &b){return abs(a-b) < EPS;}\n\tistream& operator>>(istream &is, P &p){R x,y;is>>x>>y;p=P(x, y);return is;}\n\tistream& operator>>(istream &is, L &l){l.resize(2);return is >> l[0] >> l[1];}\n\tistream& operator>>(istream &is, C &c){return is >> (P &)c >> c.r;}\n\tconst double B = 200;\n\tconst double Z = 80;\n\tostream& operator<<(ostream &os, const C &c){return os << \"circle(\"<<B+Z*(c.X)<<\", \"<<1000-B-Z*(c.Y)<<\", \"<<Z*(c.r)<<\")\";}\n\tostream& operator<<(ostream &os, const P &p){return os << C(p, 2./Z);}\n\tostream& operator<<(ostream &os, const S &s){return os << \"line(\"<<B+Z*(s[0].X)<<\", \"<<1000-B-Z*(s[0].Y)<<\", \"<<B+Z*(s[1].X)<<\", \"<<1000-B-Z*(s[1].Y)<<\")\";}\n\tostream& operator<<(ostream &os, const G &g){REP(i, g.size()) cout << g.edge(i) << endl;return os;}\n\n}\n\nint n;\n\nconst int BL = 300;\n\nint main(){\n\tios::sync_with_stdio(false);\n\tstring com;\n\twhile(1){\n\t\tP prev;\n\t\tG g;\n\t\twhile(cin >> com, com != \"close\"){\n\t\t\tif(com == \"end\") return 0;\n\t\t\tif(com == \"arc\"){\n\t\t\t\tC c;\n\t\t\t\tcin >> c;\n\t\t\t\tP ort(0., c.r < 0 ? 1. : -1.);\n\t\t\t\tR r = sqrt(max(0., c.r*c.r - norm((c-prev)*.5)));\n\t\t\t\tP b = .5*(prev+c) + unit(c-prev)*ort*r;\n\t\t\t\tR s = arg(prev - b);\n\t\t\t\tR t = arg(c - b);\n\t\t\t\tif(t-s > PI) s += 2*PI;\n\t\t\t\tif(s-t > PI) t += 2*PI;\n\t\t\t\tREPS(i, BL-1){\n\t\t\t\t\tR m = (i*t + (BL-i)*s) / BL;\n\t\t\t\t\tg.push_back(b + polar(abs(c.r), m));\n\t\t\t\t}\n\t\t\t\tprev = c;\n\t\t\t}else{\n\t\t\t\tcin >> prev;\n\t\t\t}\n\t\t\tg.push_back(prev);\n\t\t}\n\t\tP gp = g.gp();\n\t\tprintf(\"%.10f %.10f %c\\n\", gp.X, gp.Y, g.contains(gp) ? '+' : '-');\n\t}\n\treturn 0;\n}", "accuracy": 1, "time_ms": 20, "memory_kb": 1524, "score_of_the_acc": -0.0087, "final_rank": 3 }, { "submission_id": "aoj_1292_488191", "code_snippet": "#include <iostream>\n#include <complex>\n#include <cmath>\n#include <vector>\n#include <cstdio>\nusing namespace std;\n\ntypedef complex<double> P;\n\ndouble cross(const P &a, const P &b){\n\treturn a.real() * b.imag() - a.imag() * b.real();\n}\n\nvector<P> intersect(const P &p1, const P &p2, double r){\n\tdouble d = abs(p2 - p1);\n\tdouble phi = arg(p2 - p1);\n\tdouble theta = acos(0.5 * d / r);\n\t\n\tvector<P> ret(2);\n\n\tret[0] = p1 + polar(r, phi + theta);\n\tret[1] = p1 + polar(r, phi - theta);\n\treturn ret;\n}\n\nbool contains(const vector<P> &plg, const P &pt){\n\tint cnt = 0;\n\tdouble y = imag(pt);\n\n\tfor(int i = 0; i < plg.size(); ++i){\n\t\tint j = (i + 1 == plg.size() ? 0: i + 1);\n\n\t\tdouble dyi = imag(plg[i]) - y;\n\t\tdouble dyj = y - imag(plg[j]);\n\t\tdouble tx = (dyi * real(plg[j]) + dyj * real(plg[i])) / (dyi + dyj);\n\n\t\tif(imag(plg[i]) >= y && imag(plg[j]) < y){\n\t\t\tif( tx < real(pt) ) ++cnt;\n\t\t}\n\t\telse if(imag(plg[i]) < y && imag(plg[j]) >= y){\n\t\t\tif( tx < real(pt) ) ++cnt;\n\t\t}\n\t}\n\t\n\treturn (cnt % 2 != 0);\n}\n\nP centroid(const vector<P> &p){\n\tdouble sum = 0.0;\n\tP c;\n\tint n = p.size();\n\n\tfor(int i = 0; i < n; ++i){\n\t\tint next = (i + 1) % n;\n\n\t\tdouble crs = cross(p[i], p[next]);\n\t\tc += (p[i] + p[next]) * crs;\n\t\tsum += crs;\n\t}\n\t\n\treturn c / (3.0 * sum);\n}\n\nint main(){\n\tdouble pi = acos(-1.0);\n\n\tvector<P> pt;\n\tpt.reserve(102000);\n\t\n\tstring type;\n\tdouble x, y, r;\n\n\twhile( cin >> type, type[0] != 'e' ){\n\t\tpt.clear();\n\t\t\n\t\tcin >> x >> y;\n\t\tpt.push_back( P(x, y) );\n\n\t\twhile( cin >> type, type[0] != 'c' ){\n\t\t\tif( type[0] == 'l' ){\n\t\t\t\tcin >> x >> y;\n\t\t\t\tpt.push_back( P(x, y) );\n\t\t\t}\n\t\t\telse{\n\t\t\t\tcin >> x >> y >> r;\n\t\t\t\tdouble absr = abs(r);\n\t\t\t\t\n\t\t\t\tP p(x, y);\n\t\t\t\tvector<P> intr = intersect(pt.back(), p, absr);\n\n\t\t\t\tP c;\n\t\t\t\tdouble crs = cross(p - pt.back(), intr[0] - pt.back());\n\t\t\t\tif( (r < 0) == (crs > 0) ){\n\t\t\t\t\tc = intr[0];\n\t\t\t\t}\n\t\t\t\telse{\n\t\t\t\t\tc = intr[1];\n\t\t\t\t}\n\n\t\t\t\tdouble arg1 = arg(pt.back() - c);\n\t\t\t\tdouble arg2 = arg(p - c);\n\t\t\t\tif( arg1 > arg2 + pi ){\targ2 += 2.0 * pi;\t}\n\t\t\t\telse if( arg2 > arg1 + pi ){\targ1 += 2.0 * pi;\t}\n\n\t\t\t\tdouble darg = arg2 - arg1;\n\t\t\t\tfor(int j = 1; j <= 1000; ++j){\n\t\t\t\t\tpt.push_back( c + polar(absr, arg1 + j * 0.001 * darg ) );\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\t\n\t\tP cent = centroid(pt);\n\t\tbool cont = contains(pt, cent);\n\t\t\n\t\tprintf(\"%f %f %c\\n\", cent.real(), cent.imag(), cont ? '+' : '-' );\n\t}\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 1536, "score_of_the_acc": -0.0024, "final_rank": 2 }, { "submission_id": "aoj_1292_284381", "code_snippet": "#include <stdio.h>\n#include <string.h>\n#include <algorithm>\n#include <iostream>\n#include <math.h>\n#include <assert.h>\n#include <vector>\n\nusing namespace std;\ntypedef long long ll;\ntypedef unsigned int uint;\ntypedef unsigned long long ull;\nstatic const double EPS = 1e-9;\nstatic const double PI = acos(-1.0);\n\n#define REP(i, n) for (int i = 0; i < (int)(n); i++)\n#define FOR(i, s, n) for (int i = (s); i < (int)(n); i++)\n#define FOREQ(i, s, n) for (int i = (s); i <= (int)(n); i++)\n#define FORIT(it, c) for (__typeof((c).begin())it = (c).begin(); it != (c).end(); it++)\n#define MEMSET(v, h) memset((v), h, sizeof(v))\n\n#include <complex>\n\ntypedef complex<double> Point;\ntypedef vector<Point> Polygon;\n\n//static const double EPS = 1e-8;\nstatic const double INF = 1e+10;\n\n#define CURR(P, i) (P[i])\n#define NEXT(P, i) (P[(i + 1) % P.size()])\n\nstruct Line : public vector<Point> {\n Line() {;}\n Line(Point a, Point b) { push_back(a); push_back(b); }\n};\n\nstruct Circle {\n Point p;\n double r;\n Circle() {;}\n Circle(Point p, double r) : p(p), r(r) {;}\n};\n\n\n// usage:\n// g++ -Wall -Dvisualize hoge.cpp\n// ./a.out 2> data.js\n// firefox Visualize.html\nint zoom = 10;\n#ifdef visualize\nvoid ChangeColor(int r, int g, int b) {\n fprintf(stderr, \"c.strokeStyle = 'rgb(%d, %d, %d)';\\n\", r, g, b);\n}\nvoid DrawPoint(const Point &p) {\n fprintf(stderr, \"fillRect(%d, %d, %d, %d)\\n\",\n (int)(zoom*(p.real()-2)) + 1000, 1980-(int)(zoom*(p.imag() - 2)) - 1000,\n (int)(zoom*(p.real()+2)) + 1000, 1980-(int)(zoom*(p.imag() + 2)) - 1000);\n}\nvoid DrawLine(const Line &l) {\n fprintf(stderr, \"line(%d, %d, %d, %d)\\n\",\n (int)(zoom*l[0].real()) + 1000, 1980-(int)(zoom*l[0].imag()) - 1000,\n (int)(zoom*l[1].real()) + 1000, 1980-(int)(zoom*l[1].imag()) - 1000);\n}\nvoid DrawPolygon(const Polygon &p) {\n const int n = p.size();\n for (int i = 0; i < n; i++) {\n DrawLine(Line(p[i], p[(i + 1) % n]));\n }\n}\nvoid DrawCircle(const Circle &c) {\n fprintf(stderr, \"circle(%d, %d, %d)\\n\",\n (int)(zoom * c.p.real()) + 1000, 1980 - (int)(zoom * c.p.imag()) - 1000, (int)(zoom * c.r));\n}\n#else\nvoid DrawPoint(const Point &p) {;}\nvoid ChangeColor(int r, int g, int b) {;}\nvoid DrawLine(Line l) {;}\nvoid DrawPolygon(const Polygon &p) {;}\nvoid DrawCircle(Circle c) {;}\n#endif\n//--------------------------------\n\nnamespace std {\n bool operator<(const Point &lhs, const Point &rhs) {\n return lhs.real() == rhs.real() ? lhs.imag() < rhs.imag() : lhs.real() < rhs.real();\n }\n}\n\ninline double cross(const Point &a, const Point &b) {\n return imag(conj(a) * b);\n}\n\ninline double dot(const Point &a, const Point &b) {\n return real(conj(a) * b);\n}\n\ninline int ccw(Point a, Point b, Point c) {\n b -= a;\n c -= a;\n if (cross(b, c) > 0) { return 1; }\n if (cross(b, c) < 0) { return -1; }\n if (dot(b, c) < 0) { return 2; }\n if (norm(b) < norm(c)) { return -2; }\n return 0;\n}\n\nenum { OUT, ON, IN };\nint Contains(const Polygon& P, const Point& p) {\n bool in = false;\n for (int i = 0; i < (int)P.size(); ++i) {\n Point a = CURR(P,i) - p, b = NEXT(P,i) - p;\n if (imag(a) > imag(b)) swap(a, b);\n if (imag(a) <= 0 && 0 < imag(b))\n if (cross(a, b) < 0) in = !in;\n if (cross(a, b) == 0 && dot(a, b) <= 0) return ON;\n }\n return in ? IN : OUT;\n}\n\nPoint mass(const Polygon &p) {\n const int n = p.size();\n double weight = 0.0;\n Point g = Point(0.0, 0.0);\n REP(i, n) {\n Point lg = (CURR(p, i) + NEXT(p, i)) / 3.0;\n double w = cross(CURR(p, i), NEXT(p, i));\n g += lg * w;\n weight += w;\n }\n Point ret = g / weight;\n return ret;\n}\n\n\nPolygon poly;\nchar str[1000];\n\nint main(int argc, char *argv[]) {\n if (argc > 1) { zoom = atoi(argv[1]); }\n double x, y;\n while (scanf(\"%s %lf %lf\", str, &x, &y), str[0] != 'e') {\n poly.clear();\n poly.push_back(Point(x, y));\n while (true) {\n scanf(\"%s\", str);\n if (str[0] == 'a') {\n double r;\n scanf(\"%lf %lf %lf\", &x, &y, &r);\n Point c = Point(x + poly.back().real(), y + poly.back().imag()) / 2.0;\n Point vect = Point(x - poly.back().real(), y - poly.back().imag());\n vect /= abs(vect);\n vect *= Point(0, 1);\n int s = r > 0 ? 1 : -1;\n double l = abs(c- Point(x, y));\n double d = sqrt(r * r - l * l);\n d *= -s;\n Point center = c + vect * d;\n double arg1 = arg(poly.back() - center);\n double arg2 = arg(Point(x, y) - center);\n //cout << center << endl;\n if (r < 0 && arg2 < arg1) { arg2 += 2 * PI; }\n if (r > 0 && arg2 > arg1) { arg2 -= 2 * PI; }\n for (double theta = arg1; min(arg1, arg2) <= theta && theta <= max(arg1, arg2); theta += 0.001 * -s) {\n Point p = center + Point(cos(theta), sin(theta)) * abs(r);\n poly.push_back(p);\n }\n poly.push_back(Point(x, y));\n } else if (str[0] == 'l') {\n scanf(\"%lf %lf\", &x, &y);\n poly.push_back(Point(x, y));\n } else if (str[0] == 'c') {\n break;\n }\n }\n Point p = mass(poly);\n ChangeColor(0, 0, 0);\n DrawPolygon(poly);\n ChangeColor(255, 0, 0);\n DrawPoint(p);\n if (Contains(poly, p) == OUT) {\n printf(\"%.8f %.8f -\\n\", p.real(), p.imag());\n } else {\n printf(\"%.8f %.8f +\\n\", p.real(), p.imag());\n }\n }\n}", "accuracy": 1, "time_ms": 30, "memory_kb": 1564, "score_of_the_acc": -0.0167, "final_rank": 4 }, { "submission_id": "aoj_1292_103018", "code_snippet": "#include<iostream>\n#include<cassert>\n#include<algorithm>\n#include<cmath>\n#include<complex>\n#include<vector>\nusing namespace std;\n#define REP(i,b,n) for(int i=b;i<n;i++)\n#define rep(i,n) REP(i,0,n)\n#define pb push_back\n#define mp make_pair\n\ntypedef complex<double> P;\n\nconst double eps = 1e-12;\nconst double pi = acos(-1);\n\ndouble cross(P a,P b){\n return a.real()*b.imag()-a.imag()*b.real();\n}\n\ndouble dot(P a,P b){\n return a.real()*b.real()+a.imag()*b.imag();\n}\n\npair<P,P> CC_intersection(P a,P b,double r1,double r2){\n double l,m,n;\n double d = abs(a-b);\n double x,y;\n double p,q;\n l=(d*d-r2*r2+r1*r1)/(2*d);\n \n x=a.real()+(b.real()-a.real())*l/d;\n y=a.imag()+(b.imag()-a.imag())*l/d;\n \n n=sqrt(r1*r1-l*l);\n \n p=(b.imag()-a.imag())/d*n;\n q=(b.real()-a.real())/d*n;\n \n return mp(P(x+p,y-q),P(x-p,y+q));\n}\n\nconst int CUT = 10000;\nvoid compute_arg(P cur,P next,P circle,vector<P> &gon){\n double argA=arg(cur -circle);\n double argB=arg(next-circle);\n if (max(argA,argB)-min(argA,argB)>pi){\n if (argA<0)argA+=2*pi;\n else argB+=2*pi;\n }\n double dif=(argB-argA)/(double)CUT;\n double cosx=cos(dif),sinx=sin(dif);\n P now=cur-circle;\n rep(i,CUT){\n double tx=cosx*now.real()-sinx*now.imag();\n double ty=sinx*now.real()+cosx*now.imag();\n\n now.real()=tx;\n now.imag()=ty;\n \n gon.pb(circle+now);\n }\n}\n\nP compute_centeroid(vector<P> &in,int n){\n double cx=0,cy=0;//center\n double a=0;//area of polygon\n rep(i,n){\n double cr=cross(in[i],in[(i+1)%n]);\n a+=cr;\n cx+=(in[i].real()+in[(i+1)%n].real())*cr;\n cy+=(in[i].imag()+in[(i+1)%n].imag())*cr;\n }\n a/=2.0;\n // if (a < 0)a*=-1;//may be don't need\n cx/=(6.0*a);\n cy/=(6.0*a);\n return P(cx,cy);\n}\n\ndouble distance_ls_p(P a,P b,P c){\n if (dot(b-a,c-a)<eps)return abs(c-a);\n if (dot(a-b,c-b)<eps)return abs(c-b);\n return abs(cross(b-a,c-a))/abs(b-a);\n}\n\nvoid solve(double sx,double sy){\n vector<P> gon;\n gon.pb(P(sx,sy));\n string order;\n P cur(sx,sy);\n while(true){\n cin>>order;\n if (order == \"line\"){\n P next;\n cin>>next.real()>>next.imag();\n gon.pb(next);\n cur=next;\n }else if (order == \"arc\"){\n P next;\n double r;\n cin>>next.real()>>next.imag()>>r;\n pair<P,P> tmp=CC_intersection(cur,next,fabs(r),fabs(r));\n if (r < 0){\n\tif (cross(next-cur,tmp.first-cur)>0)compute_arg(cur,next,tmp.first ,gon);\n\telse compute_arg(cur,next,tmp.second,gon);\n }else if (r > 0){\n\tif (cross(next-cur,tmp.first-cur)<0)compute_arg(cur,next,tmp.first ,gon);\n\telse compute_arg(cur,next,tmp.second,gon);\n }\n cur=next;\n }else if (order == \"close\"){\n if (abs(gon[0]-gon[gon.size()-1])<eps)gon.erase(gon.begin()+gon.size()-1);\n //else gon.pb(gon[0]);\n break;\n }\n }\n\n P ans=compute_centeroid(gon,gon.size());\n\n\n \n int index = -1;\n double dist = 1e100;\n int n=gon.size();\n rep(i,gon.size()){\n double tmp = distance_ls_p(gon[i],gon[(i+1)%n],ans);\n if (tmp < dist){\n index=i;\n dist=tmp;\n }\n }\n\n char out;\n if (cross(gon[(index+1)%n]-gon[index],ans-gon[index])<-eps)out='-';\n else out='+';\n\n printf(\"%.4lf %.4lf %c\\n\",ans.real(),ans.imag(),out);\n}\n\nmain(){\n string in;\n double sx,sy;\n while(cin>>in && in != \"end\"){\n cin>>sx>>sy;\n solve(sx,sy);\n }\n}", "accuracy": 1, "time_ms": 100, "memory_kb": 3940, "score_of_the_acc": -0.1374, "final_rank": 5 } ]
aoj_1284_cpp
Problem J: The Teacher’s Side of Math One of the tasks students routinely carry out in their mathematics classes is to solve a polynomial equation. It is, given a polynomial, say X 2 - 4 X + 1, to find its roots (2 ± √3). If the students’ task is to find the roots of a given polynomial, the teacher’s task is then to find a polynomial that has a given root. Ms. Galsone is an enthusiastic mathematics teacher who is bored with finding solutions of quadratic equations that are as simple as a + b √ c . She wanted to make higher-degree equations whose solutions are a little more complicated. As usual in problems in mathematics classes, she wants to maintain all coefficients to be integers and keep the degree of the polynomial as small as possible (provided it has the specified root). Please help her by writing a program that carries out the task of the teacher’s side. You are given a number t of the form: t = m √ a + n √ b where a and b are distinct prime numbers, and m and n are integers greater than 1. In this problem, you are asked to find t's minimal polynomial on integers , which is the polynomial F ( X ) = a d X d + a d -1 X d -1 + ... + a 1 X + a 0 satisfying the following conditions. Coefficients a 0 , ... , a d are integers and a d > 0. F ( t ) = 0. The degree d is minimum among polynomials satisfying the above two conditions. F ( X ) is primitive. That is, coefficients a 0 , ... , a d have no common divisors greater than one. For example, the minimal polynomial of √3+ √2 on integers is F ( X ) = X 4 - 10 X 2 + 1. Verifying F ( t ) = 0 is as simple as the following ( α = 3, β = 2). F ( t ) = ( α + β ) 4 - 10( α + β ) 2 + 1 = ( α 4 + 4 α 3 β + 6 α 2 β 2 + 4 αβ 3 + β 4 ) - 10( α 2 + 2 αβ + β 2 ) + 1 = 9 + 12 αβ + 36 + 8 αβ + 4 - 10(3 + 2 αβ + 2) + 1 = (9 + 36 + 4 - 50 + 1) + (12 + 8 - 20) αβ = 0 Verifying that the degree of F ( t ) is in fact minimum is a bit more difficult. Fortunately, under the condition given in this problem, which is that a and b are distinct prime numbers and m and n greater than one, the degree of the minimal polynomial is always mn . Moreover, it is always monic . That is, the coefficient of its highest-order term ( a d ) is one. Input The input consists of multiple datasets, each in the following format. a m b n This line represents m √ a + n √ b . The last dataset is followed by a single line consisting of four zeros. Numbers in a single line are separated by a single space. Every dataset satisfies the following conditions. m √ a + n √ b ≤ 4. mn ≤ 20. The coefficients of the answer a 0 , ... , a d are between (-2 31 + 1) and (2 31 - 1), inclusive. Output For each dataset, output the coefficients of its minimal polynomial on integers F ( X ) = a d X d + a d -1 X d -1 + ... + a 1 X + a 0 , in the following format. a d a d -1 ... a 1 a 0 Non-negative integers must be printed without a sign (+ or -). Numbers in a single line must be separated by a single space and no other characters or extra spaces may appear in ...(truncated)
[ { "submission_id": "aoj_1284_10851232", "code_snippet": "#include <iostream>\n#include <cstdio>\n#include <cstring>\n#include <cstdlib>\n#include <cmath>\n#include <cctype>\n#include <algorithm>\n#include <vector>\n#include <set>\n#include <map>\n#include <iomanip>\n#include <cassert>\n\nusing namespace std;\n\ntypedef long long LL;\n\n#define REP(i, n) for (int i(0); i < (int) (n); i++)\n\nvoid solve() {\n}\n\nint f[2][41][41][41];// x ^ i A ^ y * B ^ z * root ^ k\nconst long double PI=acos((long double)-1.);\n\nint a,b,m,n;\n\nlong double Cos[41][41];\nlong double Sin[41][41];\n\nint main(){\n\tfor(int i = 1; i <= 40; ++i){\n\t\tCos[i][0] = 1;\n\t\tlong double t = 2 * PI / i, tmp = 0;\n\t\tfor(int j = 1; j < i; ++j){\n\t\t\ttmp += t;\n\t\t\tCos[i][j] = cos(tmp);\n\t\t\tSin[i][j] = sin(tmp);\n\t\t}\n\t}\n\twhile(scanf(\"%d%d%d%d\",&a,&m,&b,&n)==4&&a){\n\t\tmemset(f, 0, sizeof(f));\n\t\tint n1=0,n2=1;\n\t\tf[n1][0][0][0] = 1;\n\t\tfor(int i = 0; i < m * n; ++i){\n\t\t\tint x = i / n * n;\n\t\t\tint y = i % n * m;// * x - sqrt(a, m) * e ^ x - sqrt(b, n) * e ^ y\n\t\t\tfor(int j = 0; j <= i; ++j){\n\t\t\t\tfor(int k = 0; k < n * m; ++k){\n\t\t\t\t\tint fx = k / n;\n\t\t\t\t\tint fy = k % n;\n\t\t\t\t\tfor(int l = 0; l < n * m; ++l) if(f[n1][j][k][l]){\n\t\t\t\t\t\tf[n2][j + 1][k][l] += f[n1][j][k][l]; // x\n\t\t\t\t\t\tf[n2][j][(fx + 1 == m ? 0 : fx + 1) * n + fy][l + x >= n * m ? l + x - n * m : l + x] -= f[n1][j][k][l] * (fx + 1 == m ? a : 1);\n\t\t\t\t\t\tf[n2][j][fx * n + (fy + 1 == n ? 0 : fy + 1)][l + y >= n * m ? l + y - n * m : l + y] -= f[n1][j][k][l] * (fy + 1 == n ? b : 1);\n\t\t\t\t\t\tf[n1][j][k][l] = 0;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t\tswap(n1, n2);\n\t\t}\n\t\tfor(int i = m * n; i >= 0; --i){\n//\t\t\tprintf(\"%d %c\",f[n1][i][0][0] - (n * m % 2 == 0 ? f[n1][i][0][n * m / 2] : 0), i == 0 ? '\\n' : ' ');\n\t\t\tlong double now = 0, che = 0;\n\t\t\tfor(int k = 0; k < n * m; ++k){\n\t\t\t\tnow += f[n1][i][0][k] * Cos[n * m][k];\n\t\t\t\tche += f[n1][i][0][k] * Sin[n * m][k];\n\t\t\t}\n\t\t\tLL v = now + (now<0?-0.5:0.5);\n\t\t\tprintf(\"%d%c\",(int)v,i==0?'\\n':' ');\n//\t\t\tassert(-1e-6 < che && che < 1e-6);\n\t\t}\n\t}\n\treturn 0;\n}", "accuracy": 1, "time_ms": 40, "memory_kb": 4380, "score_of_the_acc": -0.4778, "final_rank": 9 }, { "submission_id": "aoj_1284_10067814", "code_snippet": "#include <bits/stdc++.h>\n\nusing namespace std;\n\ntypedef long double dbl;\n\nconst int MAXNM = 21;\nint binom[MAXNM][MAXNM];\nconst dbl EPS = 1e-10;\n\nvoid calcbinom() {\n for (int i = 0; i < MAXNM; ++i) {\n binom[i][0] = 1;\n for (int j = 1; j <= i; ++j)\n binom[i][j] = binom[i - 1][j] + binom[i - 1][j - 1];\n }\n}\n\nvoid gauss(vector<vector<dbl>> &a) {\n int n = a.size();\n int m = a[0].size();\n for (int col = 0, row = 0; col < m && row < n; ++col) {\n for (int i = row; i < n; ++i) {\n if (abs(a[i][col]) > EPS) {\n swap(a[i], a[row]);\n break;\n }\n }\n if (abs(a[row][col]) < EPS)\n continue;\n for (int i = 0; i < n; ++i) {\n if (i != row && abs(a[i][col]) > EPS) {\n dbl val = a[i][col] / a[row][col];\n for (int j = col; j < m; ++j) {\n a[i][j] -= val * a[row][j];\n }\n }\n }\n ++row;\n }\n}\n\nvector<dbl> sle(vector<vector<dbl>> a, vector<dbl> b) {\n int n = a.size();\n int m = a[0].size();\n for (int i = 0; i < n; ++i) {\n a[i].push_back(b[i]);\n }\n gauss(a);\n vector<dbl> x(m, 0);\n for (int i = n - 1; i >= 0; --i) {\n int leftmost = m;\n for (int j = 0; j < m; ++j) {\n if (abs(a[i][j]) > EPS) {\n leftmost = j;\n break;\n }\n }\n if (leftmost == m) continue;\n dbl val = a[i].back();\n for (int j = m - 1; j > leftmost; --j) {\n val -= a[i][j] * x[j];\n }\n x[leftmost] = val / a[i][leftmost];\n }\n return x;\n}\n\nsigned main() {\n ios_base::sync_with_stdio(false);\n cin.tie(nullptr);\n calcbinom();\n while (true) {\n int a, m, b, n;\n cin >> a >> m >> b >> n;\n if (a == 0)\n break;\n vector<vector<dbl>> A(n * m, vector<dbl>(n * m + 1, 0));\n for (int k = 0; k <= n * m; ++k) {\n for (int i = 0; i <= k; ++i) {\n int j = k - i;\n A[(i % m) * n + j % n][k] += binom[k][i] * pow((dbl)a, i / m) * pow((dbl)b, j / n);\n }\n }\n A.emplace_back(n * m + 1, 0);\n A[n * m][n * m] = 1;\n vector<dbl> B(n * m + 1, 0);\n B[n * m] = 1;\n auto X = sle(A, B);\n vector<int> res(n * m + 1);\n for (int i = 0; i <= n * m; ++i)\n res[i] = round(X[i]);\n\n for (int i = n * m; i >= 0; --i) {\n cout << res[i];\n if (i)\n cout << ' ';\n }\n cout << '\\n';\n }\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3576, "score_of_the_acc": -0.3438, "final_rank": 6 }, { "submission_id": "aoj_1284_9699654", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\n\nusing LL=__int128_t;\n\nLL mod;\nll nCk[21][21];\n\nvoid init(){\n for(int i=0;i<21;i++)nCk[i][0]=1;\n for(int i=0;i<21;i++){\n for(int j=1;j<=i;j++){\n nCk[i][j]=nCk[i-1][j-1]+nCk[i-1][j];\n }\n }\n while(1){\n mod=1e10+rand()%100000;\n bool OK=1;\n for(ll p=2;p*p<=mod;p++){\n if(mod%p==0){\n OK=0;\n break;\n }\n }\n if(OK)return;\n }\n}\n\nLL pow(LL a,LL n){\n a%=mod;\n if(n==0)return 1;\n if(n%2)return (pow(a,n-1)*a)%mod;\n LL t=pow(a,n/2);\n return (t*t)%mod;\n}\nLL inv(LL a){return pow(a,mod-2);}\n\n// AX=Y\n// A is square mat\nvector<LL> LE(vector<vector<LL>> A,vector<LL> Y){\n int n=A.size();\n for(int i=0;i<n;i++){\n for(int j=i;j<n;j++){\n if(A[j][i]!=0){\n swap(A[i],A[j]);\n swap(Y[i],Y[j]);\n break;\n }\n }\n LL d=inv(A[i][i]);\n for(int j=0;j<n;j++)A[i][j]=(A[i][j]*d)%mod;\n Y[i]=(Y[i]*d)%mod;\n for(int k=0;k<n;k++){\n if(i==k)continue;\n LL v=A[k][i];\n for(int j=0;j<n;j++){\n A[k][j]=(A[k][j]-(v*A[i][j])%mod)%mod;\n if(A[k][j]<0)A[k][j]+=mod;\n }\n Y[k]=(Y[k]-(v*Y[i])%mod)%mod;\n if(Y[k]<0)Y[k]+=mod;\n }\n }\n for(auto &y:Y)if(y>(1ll<<31)+1)y-=mod;\n return Y;\n}\n\n\n\nvector<LL> pow(ll A,ll M,ll B,ll N,ll K){\n vector<LL> R(M*N);\n //(m,n)<->a*N+n\n for(int i=0;i<=K;i++){\n //A^(i/M)*B^((K-i))/M;\n int m=i%M;\n int n=(K-i)%N;\n LL u=nCk[K][i];\n for(int j=0;j<i/M;j++)u=(u*A)%mod;\n for(int j=0;j<(K-i)/N;j++)u=(u*B)%mod;\n R[m*N+n]+=u;\n R[m*N+n]%=mod;\n }\n return R;\n}\nll A,M,B,N;\nvoid solve(){\n int n=M*N;\n vector<vector<LL>> MT(n,vector<LL>(n));\n vector<LL> Y(n);\n vector<LL> D;\n for(int i=0;i<n;i++){\n D=pow(A,M,B,N,i);\n for(int k=0;k<n;k++){\n MT[n-1-k][n-1-i]=D[k];\n }\n }\n D=pow(A,M,B,N,n);\n for(int k=0;k<n;k++){\n Y[n-1-k]=-D[k];\n }\n Y=LE(MT,Y);\n cout<<1;\n for(int i=0;i<n;i++)cout<<\" \"<<ll(Y[i]);\n cout<<endl;\n}\n\n\nint main() {\n ios::sync_with_stdio(false);\n cin.tie(nullptr);\n\n\n init();\n while(cin>>A>>M>>B>>N,A!=0)solve();\n \n\n}", "accuracy": 1, "time_ms": 60, "memory_kb": 3420, "score_of_the_acc": -0.4232, "final_rank": 8 }, { "submission_id": "aoj_1284_5917807", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nvector<int> mobius = {0, 1, -1, -1, 0, -1, 1, -1, 0, 0, 1};\nint main(){\n while (true){\n int a, m, b, n;\n cin >> a >> m >> b >> n;\n if (a == 0 && m == 0 && b == 0 && n == 0){\n break;\n }\n int d = m * n;\n vector<vector<vector<vector<vector<vector<unsigned int>>>>>> dp(d + 1, vector<vector<vector<vector<vector<unsigned int>>>>>(d + 1, vector<vector<vector<vector<unsigned int>>>>(m, vector<vector<vector<unsigned int>>>(m, vector<vector<unsigned int>>(n, vector<unsigned int>(n, 0))))));\n dp[0][0][0][0][0][0] = 1;\n for (int i = 0; i < d; i++){\n int da = i / n;\n int db = i % n;\n for (int j = 0; j <= i; j++){\n for (int d1 = 0; d1 < m; d1++){\n for (int d2 = 0; d2 < m; d2++){\n for (int d3 = 0; d3 < n; d3++){\n for (int d4 = 0; d4 < n; d4++){\n dp[i + 1][j + 1][d1][d2][d3][d4] += dp[i][j][d1][d2][d3][d4];\n if (d1 < m - 1){\n dp[i + 1][j][d1 + 1][(d2 + da) % m][d3][d4] -= dp[i][j][d1][d2][d3][d4];\n } else {\n dp[i + 1][j][0][(d2 + da) % m][d3][d4] -= dp[i][j][d1][d2][d3][d4] * a;\n }\n if (d3 < n - 1){\n dp[i + 1][j][d1][d2][d3 + 1][(d4 + db) % n] -= dp[i][j][d1][d2][d3][d4];\n } else {\n dp[i + 1][j][d1][d2][0][(d4 + db) % n] -= dp[i][j][d1][d2][d3][d4] * b;\n }\n }\n }\n }\n }\n }\n }\n vector<unsigned int> ans(d + 1, 0);\n for (int i = 0; i <= d; i++){\n for (int j = 1; j <= m; j++){\n for (int k = 1; k <= n; k++){\n if (m % j == 0 && n % k == 0){\n ans[i] += dp[d][i][0][m / j % m][0][n / k % n] * mobius[j] * mobius[k];\n }\n }\n }\n }\n reverse(ans.begin(), ans.end());\n for (int i = 0; i <= d; i++){\n \tif (ans[i] >= 2147483648){\n \t ans[i] = -ans[i];\n \t cout << '-';\n \t}\n cout << ans[i];\n if (i < d){\n cout << ' ';\n }\n }\n cout << endl;\n }\n}", "accuracy": 1, "time_ms": 540, "memory_kb": 10400, "score_of_the_acc": -2, "final_rank": 10 }, { "submission_id": "aoj_1284_2994517", "code_snippet": "#include \"bits/stdc++.h\"\n#include<unordered_map>\n#include<unordered_set>\n#pragma warning(disable:4996)\nusing namespace std;\nusing ld=long double;\n\n/* verified AOJ1327 One-Dimensional Cellular Automaton\n~Matrix~\n行列の簡単な計算ができる\nvector iostream cmath cassert 必須\nMatrix a(VV), b(VV);\n行列式:a*b\nスカラ積:l*a\n和:a+b\n差:a-b\n転置:a.transport()\n余因子:a.cofactor()\n行列式:a.det()\naのi行j列目:a.get(i,j) \naのi行目j列目にkを代入:set(i,j,k)\nもしくはa[i][j] (a[i][j]=k)\naのx乗:a.pow(x)\nn*n基本行列E:Matrix(n)\nm*n 0行列:Matrix(m,n)\nm*n 全要素がpの行列:Matrix(m,n,p)\nRow型rowでの初期化(m*1行列):Matrix(row)\n**(整数不可)**\n三角化:a.triangulate()\nランク:a.rank()\n逆行列:a.inverse()\n//(逆行列のa.det()倍ならa.pre_inverse()で求まる 整数可)\nガウスの消去法:a.rowReduction()\n//連立一次方程式が解ける\n***\n*/\n\n#include <vector>\n#include <iostream>\n#include <cmath>\n#include <cassert>\nusing namespace std;\n\ntypedef ld Elem;\ntypedef vector<Elem> Row;\ntypedef vector<Row> VV;\n\ntypedef long double ld;\nconst ld EPS = 1e-11;\n\nconst bool isZero(const Elem e) {\n\treturn abs(e) < EPS;\n}\n\nstruct Matrix {\n\tVV matrix;\n\tint n, m;\n\n\tMatrix(const VV &matrix_);\n\texplicit Matrix(int n_);\n\texplicit Matrix(const Row &row);\n\tMatrix(int m_, int n_);\n\tMatrix(int m_, int n_, Elem e);\n\n\tconst Elem get(const int i, const int j) const;\n\tvoid set(const int x, const int y, const Elem k);\n\n\tconst Matrix operator + (const Matrix &rhs) const;\n\tconst Matrix operator * (const Matrix &rhs) const;\n\tconst Matrix operator - (const Matrix &rhs) const;\n\tMatrix &operator += (const Matrix &rhs);\n\tMatrix &operator *= (const Matrix &rhs);\n\tMatrix &operator -= (const Matrix &rhs);\n\n\tRow &operator[](const int x);\n\n\tconst Matrix transport() const;\n\tconst Matrix pow(int x) const;\n\tconst Matrix cofactor(int x, int y) const;\n\tconst Elem det() const;\n\n\tconst Matrix triangulate() const;\n\tconst int rank() const;\n\n\t//逆行列が存在すれば、(行列式)*(逆行列)を返す\n\t//A:matrix,return det A * A^-1\n\tconst Matrix pre_inverse() const;\n\tconst Matrix inverse() const;\n\tconst Matrix rowReduction() const;\n};\n\nconst Matrix operator * (const Elem lambda, const Matrix &rhs) {\n\tMatrix tmp(rhs);\n\tfor (int i = 0; i < rhs.m; i++)\n\t\tfor (int j = 0; j < rhs.n; j++)\n\t\t\ttmp.set(i, j, tmp.get(i, j) * lambda);\n\treturn tmp;\n}\n\nMatrix::Matrix(const VV &matrix_) : matrix(matrix_) {\n\tm = matrix_.size();\n\tif (m == 0) n = 0;\n\telse n = matrix_[0].size();\n}\nMatrix::Matrix(int n_) : m(n_), n(n_) {\n\tmatrix = VV(n, Row(n, 0));\n\tfor (int i = 0; i < n; ++i)\n\t\tset(i, i, 1);\n}\nMatrix::Matrix(const Row &row) : m(1), n(row.size()), matrix(VV(1, row)) {\n\t//sizeがmのvector<Elem>からmx1行列の生成\n\t(*this) = transport();\n}\nMatrix::Matrix(int m_, int n_) : m(m_), n(n_) {\n\tmatrix = VV(m, Row(n, 0));\n}\nMatrix::Matrix(int m_, int n_, Elem e) : m(m_), n(n_) {\n\tmatrix = VV(m, Row(n, e));\n}\n\nconst Elem Matrix::get(const int i, const int j) const {\n\tif (0 <= i && i < m && 0 <= j && j < n)\n\t\treturn matrix[i][j];\n\n\tcerr << \"get(\" << i << \",\" << j << \")is not exist.\" << endl;\n\tthrow;\n}\nvoid Matrix::set(const int i, const int j, const Elem k) {\n\tif (0 <= i && i < m && 0 <= j && j < n) {\n\t\t*(matrix[i].begin() + j) = k;\n\t\treturn;\n\t}\n\tcerr << \"set(\" << i << \",\" << j << \")is not exist.\" << endl;\n\tthrow;\n}\n\nconst Matrix Matrix::operator + (const Matrix &rhs) const {\n\tassert(m == rhs.m && n == rhs.n);\n\n\tMatrix tmp(m, n, 0);\n\tfor (int i = 0; i < m; i++) {\n\t\tfor (int j = 0; j < n; j++) {\n\t\t\ttmp.set(i, j, get(i, j) + rhs.get(i, j));\n\t\t}\n\t}\n\treturn tmp;\n}\n\nconst Matrix Matrix::operator * (const Matrix &rhs) const {\n\tassert(n == rhs.m);\n\n\tMatrix tmp(m, rhs.n, 0);\n\tElem sum;\n\tfor (int i = 0; i < m; i++)\n\t\tfor (int j = 0; j < rhs.n; j++) {\n\t\t\tsum = 0;\n\t\t\tfor (int k = 0; k < n; k++) {\n\t\t\t\tsum += get(i, k) * rhs.get(k, j);\n\t\t\t}\n\t\t\ttmp.set(i, j, sum);\n\t\t}\n\treturn tmp;\n}\n\nconst Matrix Matrix::operator - (const Matrix &rhs) const {\n\treturn *this + ((Elem)-1 * rhs);\n}\n\nMatrix &Matrix::operator += (const Matrix &rhs) {\n\treturn *this = *this + rhs;\n}\n\nMatrix &Matrix::operator *= (const Matrix &rhs) {\n\treturn *this = *this * rhs;;\n}\n\nMatrix &Matrix::operator -= (const Matrix &rhs) {\n\treturn *this = *this - rhs;\n}\n\nRow &Matrix::operator[](const int x) {\n\treturn matrix[x];\n}\n\nconst Matrix Matrix::transport() const {\n\tVV tmp;\n\tfor (int i = 0; i < n; i++) {\n\t\tRow row;\n\t\tfor (int j = 0; j < m; j++)\n\t\t\trow.push_back(get(j, i));\n\t\ttmp.push_back(row);\n\t}\n\treturn tmp;\n}\n\nconst Matrix Matrix::pow(int x) const {\n\tMatrix tmp(*this), e(m);\n\tfor (int i = 1; i <= x; i <<= 1) {\n\t\tif ((x & i) > 0)\n\t\t\te = e * tmp;\n\t\ttmp = tmp * tmp;\n\t}\n\treturn e;\n}\n\nconst Matrix Matrix::cofactor(int x, int y) const {\n\tVV tmp;\n\tfor (int i = 0; i < m; i++) {\n\t\tif (x == i) continue;\n\t\tRow row;\n\t\tfor (int j = 0; j < n; j++) {\n\t\t\tif (y == j) continue;\n\t\t\trow.push_back(get(i, j));\n\t\t}\n\t\ttmp.push_back(row);\n\t}\n\treturn Matrix(tmp);\n}\n\nconst Elem Matrix::det() const {\n\tassert(n == m);\nMatrix tri = triangulate();\nElem ans = 1;\nfor (int i = 0; i < n; ++i) {\n\tans *= tri[i][i];\n}\nreturn ans;\n\n\tif (m == 1)\n\t\treturn get(0, 0);\n\tElem sum = 0;\n\tfor (int i = 0; i < m; i++) {\n\t\tsum += ((i % 2 == 0 ? 1 : -1) * get(i, 0)) * Matrix(cofactor(i, 0)).det();\n\t}\n\treturn sum;\n}\n\nconst Matrix Matrix::triangulate() const {\n\tMatrix tmp(*this);\n\tElem e;\n\tint p = 0;\n\tfor (int i = 0; i < m && p < n; i++, p++) {\n\t\tif (isZero(tmp.get(i, p))) {\n\t\t\ttmp.set(i, p, 0);\n\t\t\tbool flag = true;\n\t\t\tfor (int j = i + 1; j < m; j++)\n\t\t\t\tif (!isZero(tmp.get(j, p))) {\n\t\t\t\t\tfor (int k = 0; k < n; k++)\n\t\t\t\t\t\ttmp.set(i, k, tmp.get(i, k) + tmp.get(j, k));\n\t\t\t\t\t//tmp[i].swap(tmp[j]);\n\t\t\t\t\tflag = false;\n\t\t\t\t\tbreak;\n\t\t\t\t}\n\t\t\tif (flag) {\n\t\t\t\ti--;\n\t\t\t\tcontinue;\n\t\t\t}\n\t\t}\n\t\tfor (int j = i + 1; j < m; j++) {\n\t\t\te = tmp.get(j, p) / tmp.get(i, p);\n\t\t\tfor (int k = 0; k < n; k++)\n\t\t\t\ttmp.set(j, k, tmp.get(j, k) - tmp.get(i, k) * e);\n\t\t}\n\t}\n\treturn tmp;\n}\n\nconst int Matrix::rank() const {\n\tMatrix tmp(triangulate());\n\tfor (int i = min(tmp.m - 1, tmp.n - 1); i >= 0; i--) {\n\t\tfor (int j = tmp.n - 1; j >= i; j--)\n\t\t\tif (isZero(tmp.get(i, j)))\n\t\t\t\tcontinue;\n\t\t\telse\n\t\t\t\treturn i + 1;\n\t}\n\treturn 0;\n}\n\nconst Matrix Matrix::pre_inverse() const {\n\tassert(m == n);\n\n\tMatrix tmp(m, n, 0);\n\tfor (int i = 0; i < m; i++)\n\t\tfor (int j = 0; j < n; j++)\n\t\t\ttmp.set(i, j, ((i + j) % 2 == 0 ? 1 : -1)*cofactor(i, j).det());\n\treturn tmp.transport();\n}\n\n/*O(n!)実装\nconst Matrix Matrix::inverse() const {\nMatrix tmp(pre_inverse());\nElem e = det();\nassert(!isZero(e));\ntmp = 1 / e * tmp;\nreturn tmp.transport();\n}*/\n\nconst Matrix Matrix::inverse() const {\n\tassert(m == n);\n\n\tMatrix tmp(m, n * 2), tmp2(m, n);\n\tfor (int i = 0; i < m; i++)\n\t\tfor (int j = 0; j < n; j++)\n\t\t\ttmp.set(i, j, get(i, j));\n\tfor (int i = 0; i < m; i++)\n\t\ttmp.set(i, i + n, 1);\n\n\ttmp = tmp.rowReduction();\n\n\t//逆行列が存在するかどうかのチェック\n\tfor (int i = 0; i < m; i++)\n\t\tassert(isZero(tmp.get(i, i) - 1));\n\n\tfor (int i = 0; i < m; i++)\n\t\tfor (int j = 0; j < n; j++)\n\t\t\ttmp2.set(i, j, tmp.get(i, j + n));\n\n\treturn tmp2;\n}\n\n/*\na b c j\nd e f k\ng h i l\n\nax+by+cz=j;\ndx+ey+fz=k;\ngx+hy+iz=l;\n\n|\nV\n\n1 0 0 x\n0 1 0 y\n0 0 1 z\n右端が答え\n*/\nconst Matrix Matrix::rowReduction() const {\n\tMatrix tmp(*this);\n\tElem e;\n\tint p = 0;\n\tfor (int i = 0; i < m && p < n; i++, p++) {\n\t\tif (isZero(tmp.get(i, p))) {\n\t\t\ttmp.set(i, p, 0);\n\t\t\tbool flag = true;\n\t\t\tfor (int j = i + 1; j < m; j++)\n\t\t\t\tif (!isZero(tmp.get(j, p))) {\n\t\t\t\t\tfor (int k = 0; k < n; k++)\n\t\t\t\t\t\ttmp.set(i, k, tmp.get(i, k) + tmp.get(j, k));\n\t\t\t\t\t//tmp[i].swap(tmp[j]);\n\t\t\t\t\tflag = false;\n\t\t\t\t\tbreak;\n\t\t\t\t}\n\t\t\tif (flag) {\n\t\t\t\ti--;\n\t\t\t\tcontinue;\n\t\t\t}\n\t\t}\n\t\te = 1 / tmp.get(i, p);\n\t\ttmp.set(i, p, 1);\n\t\tfor (int k = i + 1; k < n; k++)\n\t\t\ttmp.set(i, k, tmp.get(i, k)*e);\n\t\tfor (int j = 0; j < m; j++) {\n\t\t\tif (i == j) continue;\n\t\t\te = tmp.get(j, p);\n\t\t\tfor (int k = 0; k < n; k++)\n\t\t\t\ttmp.set(j, k, tmp.get(j, k) - tmp.get(i, k) * e);\n\t\t}\n\t}\n\treturn tmp;\n}\n\nint main()\n{\n\twhile (true) {\n\t\t//m√a + n√b ≤ 4\n\n\t\tint A, M, B, N; cin >> A >> M >> B >> N;\n\t\tif(!A)break;\n\t\tvector<vector<vector<ld>>>nums(M*N + 1, vector<vector<ld>>(M, vector<ld>(N)));\n\t\tnums[0][0][0] = 1;\n\n\t\tfor (int i = 0; i < M*N; ++i) {\n\t\t\tfor (int a = 0; a < M; ++a) {\n\t\t\t\tfor (int b = 0; b < N; ++b) {\n\t\t\t\t\t{\n\n\t\t\t\t\t\tld bai = b == N - 1 ? B : 1;\n\t\t\t\t\t\tnums[i + 1][a][(b + 1) % N] += bai*nums[i][a][b];\n\t\t\t\t\t}\n\t\t\t\t\t{\n\t\t\t\t\t\tld bai = a == M - 1 ? A : 1;\n\t\t\t\t\t\tnums[i + 1][(a + 1) % M][b] += bai*nums[i][a][b];\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\tMatrix mat(M*N + 1, M*N + 2);\n\t\tfor (int a = 0; a < M; ++a) {\n\t\t\tfor (int b = 0; b < N; ++b) {\n\t\t\t\tfor (int i = 0; i <= M*N; ++i) {\n\t\t\t\t\tmat[a*N + b][i] = nums[i][a][b];\n\t\t\t\t}\n\t\t\t\tmat[a*N + b][M*N + 1] = 0;\n\t\t\t}\n\t\t}\n\t\tmat[M*N][M*N] = 1;\n\t\tmat[M*N][M*N + 1] = 1;\n\n\t\tauto ans = mat.rowReduction();\n\n\t\tvector<long long int>anss(M*N+1);\n\t\tfor(int x=0;x<=M*N;++x){\n\t\t\tanss[x]=round(ans[x][M*N+1]);\n\t\t}\n\t\tfor (int i = 0; i <= M*N; ++i) {\n\t\t\tcout<<anss[M*N-i];\n\t\t\tif(i==M*N)cout<<endl;\n\t\t\telse cout<<\" \";\n\t\t}\n\t}\n\t\n\treturn 0;\n}", "accuracy": 1, "time_ms": 40, "memory_kb": 3332, "score_of_the_acc": -0.377, "final_rank": 7 }, { "submission_id": "aoj_1284_1427311", "code_snippet": "#include <vector>\n#include <algorithm>\n#include <cstdio>\n#include <cmath>\nusing namespace std;\n\nbool gauss(vector<vector<long double> > &a){\n\tif(a.empty())return false;\n\tint n=a.size();\n\tfor(int i=0;i<n;i++){\n\t\tif(!a[i][i]){\n\t\t\tint j=i+1;\n\t\t\tfor(;j<n;j++){\n\t\t\t\tif(a[j][i]){\n\t\t\t\t\tfor(int k=i;k<=n;k++)a[i][k]+=a[j][k];\n\t\t\t\t\tbreak;\n\t\t\t\t}\n\t\t\t}\n\t\t\tif(j==n)return false;\n\t\t}\n\t\tfor(int j=0;j<n;j++)if(i!=j){\n\t\t\tlong double r=a[j][i]/a[i][i];\n\t\t\tfor(int k=i;k<=n;k++)a[j][k]=a[j][k]-a[i][k]*r;\n\t\t}\n\t}\n\tfor(int i=0;i<n;i++){\n\t\tlong double x=a[i][i];\n\t\tfor(int j=0;j<a[i].size();j++)a[i][j]/=x;\n\t}\n\treturn true;\n}\nlong long comb(int n,int k){\n\tif(k>n-k)k=n-k;\n\tlong long r=1;\n\tfor(int i=0;i<k;i++)r=r*(n-i)/(i+1);\n\treturn r;\n}\nlong long pow_binary(long long x,long long y){\n\tlong long z=1;\n\tfor(;y;y>>=1){\n\t\tif((y&1)!=0)z=z*x;\n\t\tx=x*x;\n\t}\n\treturn z;\n}\nint main(){\n\tint A,M,B,N;\n\tfor(;scanf(\"%d%d%d%d\",&A,&M,&B,&N),A;){\n\t\tvector<vector<long double> >m(M*N+1);\n\t\tfor(int i=0;i<=M*N;i++)m[i].resize(M*N+2);\n\t\tm[0][0]=m[0][M*N+1]=1;\n\t\tfor(int i=0;i<=M*N;i++)for(int k=0;k<=i;k++){\n\t\t\tm[k%M*N+(i-k)%N+1][i<M*N ? i+1 : 0]+=(long double)comb(i,k)*pow_binary(A,k/M)*pow_binary(B,(i-k)/N);\n\t\t}\n\t\tgauss(m);\n\t\tprintf(\"1\");\n\t\tfor(int i=M*N;i>0;i--)printf(\" %d\",(int)(roundl(m[i][M*N+1])));\n\t\tputs(\"\");\n\t}\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 1064, "score_of_the_acc": -0.1023, "final_rank": 2 }, { "submission_id": "aoj_1284_1145508", "code_snippet": "#include<stdio.h>\n#include<algorithm>\n#include<vector>\n#include<math.h>\nusing namespace std;\nconst double EPS=1e-8;\ntypedef vector<double>vec;\ntypedef vector<vec>mat;\ndouble ABS(double a){return max(a,-a);}\nvec gauss_jordan(const mat &A,const vec &b){\n\tint n=A.size();\n\tmat B(n,vec(n+1));\n\tfor(int i=0;i<n;i++)\n\t\tfor(int j=0;j<n;j++)B[i][j]=A[i][j];\n\tfor(int i=0;i<n;i++)B[i][n]=b[i];\n\tfor(int i=0;i<n;i++){\n\t\tint pivot=i;\n\t\tfor(int j=i;j<n;j++){\n\t\t\tif(ABS(B[j][i])>ABS(B[pivot][i]))pivot=j;\n\t\t}\n\t\tswap(B[i],B[pivot]);\n\t\tif(ABS(B[i][i])<EPS)return vec();\n\t\tfor(int j=i+1;j<=n;j++)B[i][j]/=B[i][i];\n\t\tfor(int j=0;j<n;j++){\n\t\t\tif(i!=j){\n\t\t\t\tfor(int k=i+1;k<=n;k++)B[j][k]-=B[j][i]*B[i][k];\n\t\t\t}\n\t\t}\n\t}\n\tvec x(n);\n\tfor(int i=0;i<n;i++)x[i]=B[i][n];\n\treturn x;\n}\ndouble now[30][30][30];\nint main(){\n\tint a,m,b,n;\n\twhile(scanf(\"%d%d%d%d\",&a,&m,&b,&n),a){\n\t\tfor(int i=0;i<30;i++)for(int j=0;j<30;j++)for(int k=0;k<30;k++)\n\t\t\tnow[i][j][k]=0;\n\t\tnow[0][0][0]=1;\n\t\tfor(int i=0;i<m*n;i++){\n\t\t\tfor(int j=0;j<m;j++){\n\t\t\t\tfor(int k=0;k<n;k++){\n\t\t\t\t\tnow[i+1][j+1][k]+=now[i][j][k];\n\t\t\t\t\tnow[i+1][j][k+1]+=now[i][j][k];\n\t\t\t\t}\n\t\t\t}\n\t\t\tfor(int j=0;j<n;j++)now[i+1][0][j]+=now[i+1][m][j]*a;\n\t\t\tfor(int j=0;j<m;j++)now[i+1][j][0]+=now[i+1][j][n]*b;\n\t\t\tnow[i+1][0][0]+=now[i+1][m][n]*a*b;\n\t\t}\n\t\tmat A(m*n,vec(m*n));\n\t\tvec B(m*n);\n\t\tfor(int i=0;i<m*n;i++){\n\t\t\tfor(int j=0;j<m;j++){\n\t\t\t\tfor(int k=0;k<n;k++){\n\t\t\t\t\tA[j*n+k][i]=now[i][j][k];\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\tfor(int i=0;i<m;i++)for(int j=0;j<n;j++)\n\t\t\tB[i*n+j]-=now[n*m][i][j];\n\t\tvec x=gauss_jordan(A,B);\n\t\tprintf(\"1\");\n\t\tfor(int i=m*n-1;i>=0;i--){\n\t\t\tprintf(\" %d\",(int)(round(x[i])));\n\t\t}\n\t\tprintf(\"\\n\");\n\t}\n}", "accuracy": 1, "time_ms": 20, "memory_kb": 1292, "score_of_the_acc": -0.1431, "final_rank": 4 }, { "submission_id": "aoj_1284_1100532", "code_snippet": "#include <iostream>\n#include <cstdio>\n#include <vector>\n#include <queue>\n#include <cmath>\n#include <algorithm>\n#include <functional>\nusing namespace std;\n#define rep(i,n) for(int i=0;i<(n);++i)\n#define rep1(i,n) for(int i=1;i<=(n);++i)\n#define all(c) (c).begin(),(c).end()\n#define pb push_back\n#define fs first\n#define sc second\n#define show(x) cout << #x << \" = \" << x << endl\ntypedef long long ll;\ntypedef vector<ll> poly;\nll C[11][11];\npoly operator+(poly x,poly y){\n\tpoly ret(21,0);\n\trep(i,21){\n\t\tret[i]=x[i]+y[i];\n\t}\n\treturn ret;\n}\npoly operator-(poly x,poly y){\n\tpoly ret(21,0);\n\trep(i,21){\n\t\tret[i]=x[i]-y[i];\n\t}\n\treturn ret;\n}\npoly operator*(poly x,poly y){\n\tpoly ret(21,0);\n\trep(i,21) rep(j,21){\n\t\tif(i+j>20) continue;\n\t\tret[i+j]+=x[i]*y[j];\n\t}\n\treturn ret;\n}\npoly operator*(ll x,poly y){\n\tpoly ret(21,0);\n\trep(i,21){\n\t\tret[i]=x*y[i];\n\t}\n\treturn ret;\n}\nvoid Answer(poly p){\n\tint w=0;\n\tfor(int i=20;i>=0;i--){\n\t\tif(w!=0||p[i]!=0){\n\t\t\tif(w==0){\n\t\t\t\tif(p[i]==1) w=1;\n\t\t\t\telse w=-1;\n\t\t\t}\n\t\t\tcout<<p[i]*w;\n\t\t\tif(i==0) cout<<endl;\n\t\t\telse cout<<\" \";\n\t\t}\n\t}\n}\nint main(){\n\trep(i,11){\n\t\tC[i][0]=C[i][i]=1;\n\t\tfor(int j=1;j<i;j++) C[i][j]=C[i-1][j-1]+C[i-1][j];\n\t}\n\twhile(true){\n\t\tll a,m,b,n;\n\t\tcin>>a>>m>>b>>n;\n\t\tif(m==0) break;\n\t\tif(m>n) swap(m,n),swap(a,b);\n\t\tpoly co[4];\n\t\trep(i,4) co[i].assign(21,0);\n\t\tif(m==4){\n\t\t\tco[0][0]=-b;\n\t\t\tll now=1;\n\t\t\trep(i,n+1){\n\t\t\t\tco[i%4][n-i]+=C[n][i]*now*(i%2==0 ? 1 : -1);\n\t\t\t\tif(i%4==3) now*=a;\n\t\t\t}\n\t\t\tpoly p=co[3],q=co[2],r=co[1],s=co[0];\n\t\t\tpoly ans=a*a*a*p*p*p*p+a*a*(-4*p*p*q*s-2*p*p*r*r+4*p*q*q*r-q*q*q*q)+a*((4*p*r+2*q*q)*s*s-4*q*r*r*s+r*r*r*r)-s*s*s*s;\n\t\t\tAnswer(ans);\n\t\t}else if(m==3){\n\t\t\tco[0][0]=-b;\n\t\t\tll now=1;\n\t\t\trep(i,n+1){\n\t\t\t\tco[i%3][n-i]+=C[n][i]*now*(i%2==0 ? 1 : -1);\n\t\t\t\tif(i%3==2) now*=a;\n\t\t\t}\n\t\t\tpoly p=co[2],q=co[1],r=co[0];\n\t\t\tpoly ans=a*a*p*p*p+a*(q*q*q-3*p*q*r)+r*r*r;\n\t\t\tAnswer(ans);\n\t\t}else if(m==2){\n\t\t\tco[0][0]=-b;\n\t\t\tll now=1;\n\t\t\trep(i,n+1){\n\t\t\t\tco[i%2][n-i]+=C[n][i]*now*(i%2==0 ? 1 : -1);\n\t\t\t\tif(i%2==1) now*=a;\n\t\t\t}\n\t\t\tpoly p=co[1],q=co[0];\n\t\t\tpoly ans=a*p*p-q*q;\n\t\t\tAnswer(ans);\n\t\t}\n\t}\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 1244, "score_of_the_acc": -0.1196, "final_rank": 3 }, { "submission_id": "aoj_1284_1061830", "code_snippet": "#include <bits/stdc++.h>\n#include <iostream>\n#include <vector>\n#include <complex>\n#include <algorithm>\n#include <cstring>\n#include <map>\n#include <set>\n#include <cstdio>\n#include <queue>\n#include <cmath>\n#include <cstdlib>\n\nusing namespace std;\ntypedef long long ll;\ntypedef ll li;\ntypedef pair<int,int> PI;\n#define rep(i,n) for(int i=0;i<(int)(n);++i)\n#define REP(i, n) rep (i, n)\n#define F first\n#define S second\n#define mp(a,b) make_pair(a,b)\n#define pb(a) push_back(a)\n#define SZ(a) (int)((a).size())\n#define ALL(a) (a).begin(),(a).end()\n#define FLL(a,b) memset((a),b,sizeof(a))\n#define CLR(a) memset((a),0,sizeof(a))\n#define FOR(it,a) for(__typeof(a.begin())it=a.begin();it!=a.end();++it)\n#define FORR(it,a) for(__typeof(a.rbegin())it=a.rbegin();it!=a.rend();++it)\ntemplate<typename T,typename U> ostream& operator<< (ostream& out, const pair<T,U>& val){return out << \"(\" << val.F << \", \" << val.S << \")\";}\ntemplate<class T> ostream& operator<< (ostream& out, const vector<T>& val){out << \"{\";rep(i,SZ(val)) out << (i?\", \":\"\") << val[i];return out << \"}\";}\n#define EPS (1e-6)\n#define declare(a,it) __typeof(a) it=a\n\ntypedef vector<long double> arr;\ntypedef vector<arr> mat;\n\narr sol(mat A,arr y){//Ax=y, return x\n int n = A.size();\n mat B(n,arr(n));\n for(int i = 0; i < n; ++i)\n B[i][i] = 1;\n \n for(int i = 0; i < n; ++i){\n for(int j=i+1;j < n; ++j){\n if(abs(A[j][i]) > abs(A[i][i])){\n swap(A[j],A[i]);\n swap(B[j],B[i]);\n }\n }\n long double d = A[i][i];\n for(int j = 0; j < n; ++j){\n A[i][j] /= d;\n B[i][j] /= d;\n }\n\n for(int j = 0; j < n; ++j){\n if(i==j) continue;\n long double m = A[j][i];\n for(int k = 0; k < n; ++k){\n A[j][k] -= A[i][k] * m;\n B[j][k] -= B[i][k] * m;\n }\n }\n }\n \n arr ret(n);\n for(int i = 0; i < n; ++i)\n for(int j = 0; j < n; ++j)\n ret[i] += B[i][j] * y[j];\n \n return ret;\n}\n\nint main()\n{\n int a,m,b,n;\n while(cin >> a >> m >> b >> n && a){\n arr p(n*m);\n p[0] = 1;\n mat A(n*m,arr(n*m));\n for(int i = n*m-1; i >= 0; --i){\n for(int j = 0; j < n*m; ++j)\n A[j][i] = p[j];\n arr np(n*m);\n for(int j = 0; j < n*m; ++j){\n int an = j % m;\n int bn = j / m;\n long pp = p[j];\n if(an == m-1) np[bn*m] += pp * a;\n else np[bn*m+an+1] += pp;\n \n if(bn == n-1) np[an] += pp * b;\n else np[(bn+1)*m+an] += pp;\n }\n p = np;\n }\n for(auto & i : p) i = -i;\n auto ans = sol(A, p);\n cout << 1;\n for(auto p : ans) printf(\" %.0Lf\", abs(p)<EPS?0.0:p);\n cout << endl;\n }\n}", "accuracy": 1, "time_ms": 20, "memory_kb": 1300, "score_of_the_acc": -0.1439, "final_rank": 5 }, { "submission_id": "aoj_1284_335540", "code_snippet": "#include<cmath>\n#include<cstdio>\n#include<algorithm>\n\n#define rep(i,n) for(int i=0;i<(n);i++)\n\nusing namespace std;\n\nconst long double EPS=1e-9;\n\nconst int N_MAX=20;\nbool Gauss_Jordan(int n,const long double A[N_MAX][N_MAX],const long double *b,long double *x){\n\tstatic long double B[N_MAX][N_MAX+1];\n\trep(i,n){\n\t\trep(j,n) B[i][j]=A[i][j];\n\t\tB[i][n]=b[i];\n\t}\n\n\trep(i,n){\n\t\tint piv=i;\n\t\tfor(int j=i;j<n;j++) if(abs(B[j][i])>abs(B[piv][i])) piv=j;\n\t\trep(j,n+1) swap(B[i][j],B[piv][j]);\n\n\t\tif(abs(B[i][i])<EPS) return false;\n\n\t\tfor(int j=i+1;j<=n;j++) B[i][j]/=B[i][i];\n\t\trep(j,n) if(i!=j) for(int k=i+1;k<=n;k++) B[j][k]-=B[j][i]*B[i][k];\n\t}\n\n\trep(i,n) x[i]=B[i][n];\n\treturn true;\n}\n\nint main(){\n\tfor(int a,m,b,n;scanf(\"%d%d%d%d\",&a,&m,&b,&n),a;){\n\t\tlong double coe[21][20][20]={}; // coe[k][i][j] := (^mãa+^nãb)^k ‚Ì ^mã(a^i)*^nã(b^j) ‚ÌŒW”\n\t\tcoe[0][0][0]=1;\n\t\trep(k,m*n){\n\t\t\trep(i,m) rep(j,n) {\n\t\t\t\tif(i<m-1) coe[k+1][i+1][j]+=coe[k][i][j];\n\t\t\t\telse coe[k+1][0][j]+=a*coe[k][i][j];\n\t\t\t\tif(j<n-1) coe[k+1][i][j+1]+=coe[k][i][j];\n\t\t\t\telse coe[k+1][i][0]+=b*coe[k][i][j];\n\t\t\t}\n\t\t}\n\n\t\tlong double A[20][20],x[20],b[20];\n\t\trep(i,m) rep(j,n) {\n\t\t\tb[i*n+j]=-coe[m*n][i][j];\n\t\t\trep(k,m*n) A[i*n+j][k]=coe[k][i][j];\n\t\t}\n\t\tGauss_Jordan(m*n,A,b,x);\n\n\t\tprintf(\"1 \");\n\t\trep(k,m*n) printf(\"%.0Lf%c\",x[m*n-k-1]+EPS,k<m*n-1?' ':'\\n');\n\t}\n\n\treturn 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 0, "score_of_the_acc": 0, "final_rank": 1 } ]
aoj_1295_cpp
Problem A: Cubist Artwork International Center for Picassonian Cubism is a Spanish national museum of cubist artworks, dedicated to Pablo Picasso. The center held a competition for an artwork that will be displayed in front of the facade of the museum building. The artwork is a collection of cubes that are piled up on the ground and is intended to amuse visitors, who will be curious how the shape of the collection of cubes changes when it is seen from the front and the sides. The artwork is a collection of cubes with edges of one foot long and is built on a flat ground that is divided into a grid of one foot by one foot squares. Due to some technical reasons, cubes of the artwork must be either put on the ground, fitting into a unit square in the grid, or put on another cube in the way that the bottom face of the upper cube exactly meets the top face of the lower cube. No other way of putting cubes is possible. You are a member of the judging committee responsible for selecting one out of a plenty of artwork proposals submitted to the competition. The decision is made primarily based on artistic quality but the cost for installing the artwork is another important factor. Your task is to investigate the installation cost for each proposal. The cost is proportional to the number of cubes, so you have to figure out the minimum number of cubes needed for installation. Each design proposal of an artwork consists of the front view and the side view (the view seen from the right-hand side), as shown in Figure 1. Figure 1: An example of an artwork proposal The front view (resp., the side view) indicates the maximum heights of piles of cubes for each column line (resp., row line) of the grid. There are several ways to install this proposal of artwork, such as the following figures. In these figures, the dotted lines on the ground indicate the grid lines. The left figure makes use of 16 cubes, which is not optimal. That is, the artwork can be installed with a fewer number of cubes. Actually, the right one is optimal and only uses 13 cubes. Note that, a single pile of height three in the right figure plays the roles of two such piles in the left one. Notice that swapping columns of cubes does not change the side view. Similarly, swapping rows does not change the front view. Thus, such swaps do not change the costs of building the artworks. For example, consider the artwork proposal given in Figure 2. Figure 2: Another example of artwork proposal An optimal installation of this proposal of artwork can be achieved with 13 cubes, as shown in the following figure, which can be obtained by exchanging the rightmost two columns of the optimal installation of the artwork of Figure 1. Input The input is a sequence of datasets. The end of the input is indicated by a line containing two zeros separated by a space. Each dataset is formatted as follows. w d h 1 h 2 ... h w h' 1 h' 2 ... h' d The integers w and d separated by a space are the numbers of columns and rows o ...(truncated)
[ { "submission_id": "aoj_1295_5841742", "code_snippet": "#pragma region Macros\n#include <bits/stdc++.h>\nusing namespace std;\ntemplate <class T> inline bool chmax(T &a, T b) {\n if(a < b) {\n a = b;\n return 1;\n }\n return 0;\n}\ntemplate <class T> inline bool chmin(T &a, T b) {\n if(a > b) {\n a = b;\n return 1;\n }\n return 0;\n}\n#ifdef DEBUG\ntemplate <class T, class U>\nostream &operator<<(ostream &os, const pair<T, U> &p) {\n os << '(' << p.first << ',' << p.second << ')';\n return os;\n}\ntemplate <class T> ostream &operator<<(ostream &os, const vector<T> &v) {\n os << '{';\n for(int i = 0; i < (int)v.size(); i++) {\n if(i) { os << ','; }\n os << v[i];\n }\n os << '}';\n return os;\n}\nvoid debugg() { cerr << endl; }\ntemplate <class T, class... Args>\nvoid debugg(const T &x, const Args &... args) {\n cerr << \" \" << x;\n debugg(args...);\n}\n#define debug(...) \\\n cerr << __LINE__ << \" [\" << #__VA_ARGS__ << \"]: \", debugg(__VA_ARGS__)\n#define dump(x) cerr << __LINE__ << \" \" << #x << \" = \" << (x) << endl\n#else\n#define debug(...) (void(0))\n#define dump(x) (void(0))\n#endif\n\nstruct Setup {\n Setup() {\n cin.tie(0);\n ios::sync_with_stdio(false);\n cout << fixed << setprecision(15);\n }\n} __Setup;\n\nusing ll = long long;\n#define ALL(v) (v).begin(), (v).end()\n#define RALL(v) (v).rbegin(), (v).rend()\n#define FOR(i, a, b) for(int i = (a); i < int(b); i++)\n#define REP(i, n) FOR(i, 0, n)\nconst int INF = 1 << 30;\nconst ll LLINF = 1LL << 60;\nconstexpr int MOD = 1000000007;\nconst int dx[4] = {1, 0, -1, 0};\nconst int dy[4] = {0, 1, 0, -1};\n\nvoid Case(int i) { cout << \"Case #\" << i << \": \"; }\nint popcount(int x) { return __builtin_popcount(x); }\nll popcount(ll x) { return __builtin_popcountll(x); }\n#pragma endregion Macros\n\nint main() {\n while(1) {\n int N, M;\n cin >> N >> M;\n if(N == 0) break;\n vector<int> a(N), b(M);\n REP(i, N) cin >> a[i];\n REP(i, M) cin >> b[i];\n\n if(N < M) {\n swap(N, M);\n swap(a, b);\n } // N >= M\n int ans = INF;\n vector<int> ord(N);\n iota(ALL(ord), 0);\n\n do {\n int cost = 0;\n REP(i, M) {\n if(a[ord[i]] == b[i]) cost += b[i];\n else cost += a[ord[i]] + b[i];\n }\n FOR(i, M, N) cost += a[ord[i]];\n chmin(ans, cost);\n }while(next_permutation(ALL(ord)));\n\n cout << ans << \"\\n\";\n }\n}", "accuracy": 1, "time_ms": 840, "memory_kb": 3460, "score_of_the_acc": -1, "final_rank": 2 }, { "submission_id": "aoj_1295_4779142", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n#define modulo 1000000007\n#define mod(mod_x) ((((long long)mod_x+modulo))%modulo)\n#define Inf 2000000005\n\n\nint main(){\n\t\n\t\n\t\n\twhile(true){\n\t\tint w,d;\n\t\tcin>>w>>d;\n\t\tif(w==0)break;\n\t\t\n\t\tvector<vector<int>> dp(w+1,vector<int>(1<<d,Inf));\n\t\tdp[0][0] = 0;\n\t\t\n\t\tvector<int> hw(w),hd(d);\n\t\tfor(int i=0;i<w;i++)cin>>hw[i];\n\t\tfor(int i=0;i<d;i++)cin>>hd[i];\n\t\t\n\t\tfor(int i=0;i<w;i++){\n\t\t\tfor(int j=0;j<(1<<d);j++){\n\t\t\t\tif(dp[i][j]==Inf)continue;\n\t\t\t\t\n\t\t\t\tfor(int k=0;k<(1<<d);k++){\n\t\t\t\t\tint sum = 0;\n\t\t\t\t\tbool f = false;\n\t\t\t\t\tint mask = 0;\n\t\t\t\t\tfor(int l=0;l<d;l++){\n\t\t\t\t\t\tif((k>>l)&1){\n\t\t\t\t\t\t\tif(hw[i]>hd[l]){\n\t\t\t\t\t\t\t\tsum += hd[l];\n\t\t\t\t\t\t\t\tmask |= (1<<l);\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\telse{\n\t\t\t\t\t\t\t\tf = true;\n\t\t\t\t\t\t\t\tsum += hw[i];\n\t\t\t\t\t\t\t\tif(hw[i]==hd[l])mask |= (1<<l);\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t\tif(!f)continue;\n\t\t\t\t\tdp[i+1][j|mask] = min(dp[i+1][j|mask],dp[i][j]+sum);\n\t\t\t\t}\n\t\t\t\t\n\t\t\t}\n\t\t}\n\t\t\n\t\tcout<<dp.back().back()<<endl;\n\t\t\n\t}\n\t\n\t\n\treturn 0;\n}", "accuracy": 1, "time_ms": 3390, "memory_kb": 3184, "score_of_the_acc": -1.3625, "final_rank": 5 }, { "submission_id": "aoj_1295_3385742", "code_snippet": "#include<iomanip>\n#include<limits>\n#include<thread>\n#include<utility>\n#include<iostream>\n#include<string>\n#include<algorithm>\n#include<set>\n#include<map>\n#include<vector>\n#include<stack>\n#include<queue>\n#include<cmath>\n#include<numeric>\n#include<cassert>\n#include<random>\n#include<chrono>\n#include<unordered_set>\n#include<unordered_map>\n#include<fstream>\n#include<list>\n#include<functional>\n#include<bitset>\n#include<complex>\n#include<tuple>\nusing namespace std;\ntypedef unsigned long long int ull;\ntypedef long long int ll;\ntypedef pair<ll,ll> pll;\ntypedef pair<int,int> pi;\ntypedef pair<double,double> pd;\ntypedef pair<double,ll> pdl;\n#define F first\n#define S second\nconst ll E=1e18+7;\nconst ll MOD=1000000007;\n\n\n\n\nint main(){\n ll w,d;\n while(cin>>w>>d){\n if(w==0){break;}\n vector<ll> a(w);\n for(auto &I:a){cin>>I;}\n vector<ll> b(d);\n for(auto &I:b){cin>>I;}\n vector<vector<ll>> dp(w+1,vector<ll>(1LL<<d,MOD));\n dp[0][0]=0;\n for(int i=0;i<w;i++){\n for(int k=0;k<d;k++){\n if(a[i]>b[k]){continue;}\n for(int h=0;h<(1LL<<d);h++){\n dp[i+1][h]=min(dp[i+1][h],dp[i][h]+a[i]);\n if(a[i]==b[k]){dp[i+1][h|(1<<k)]=min(dp[i+1][h|(1<<k)],dp[i][h]+a[i]);}\n int O=((1<<d)-1)^h;\n for(int key=O;key>0;key=(key-1)&O){\n if(key>>k&1){continue;}\n ll sum=0;\n for(int p=0;p<d;p++){\n if(key>>p&1){\n if(b[p]>a[i]){sum+=MOD;}\n sum+=b[p];\n }\n }\n if(a[i]==b[k]){\n dp[i+1][h|(1<<k)|key]=min(dp[i+1][h|(1<<k)|key],dp[i][h]+sum+a[i]);\n }\n else{\n dp[i+1][h|key]=min(dp[i+1][h|key],dp[i][h]+sum+a[i]);\n }\n }\n }\n }\n }\n cout<<dp[w][(1LL<<d)-1]<<endl;\n }\n \n \n \n\n return 0;\n}", "accuracy": 1, "time_ms": 1420, "memory_kb": 3244, "score_of_the_acc": -1.0145, "final_rank": 4 }, { "submission_id": "aoj_1295_1623644", "code_snippet": "#include<stdio.h>\n#include <iostream>\n#include <math.h>\n#include <numeric>\n#include <vector>\n#include <map>\n#include <functional>\n#include <stdio.h>\n#include <array>\n#include <algorithm>\n#include <string>\n#include <string.h>\n#include <assert.h>\n#include <stdio.h>\n#include <queue>\n#include<iomanip>\n#include<bitset>\n#include<stack>\n#include<set>\n#include<limits>\n#include <complex>\nusing namespace std;\ntemplate < class BidirectionalIterator >\nbool next_combination(BidirectionalIterator first1,\n\tBidirectionalIterator last1,\n\tBidirectionalIterator first2,\n\tBidirectionalIterator last2)\n{\n\tif ((first1 == last1) || (first2 == last2)) {\n\t\treturn false;\n\t}\n\tBidirectionalIterator m1 = last1;\n\tBidirectionalIterator m2 = last2; --m2;\n\twhile (--m1 != first1 && !(*m1 < *m2)) {\n\t}\n\tbool result = (m1 == first1) && !(*first1 < *m2);\n\tif (!result) {\n\t\t// ?\n\t\twhile (first2 != m2 && !(*m1 < *first2)) {\n\t\t\t++first2;\n\t\t}\n\t\tfirst1 = m1;\n\t\tstd::iter_swap(first1, first2);\n\t\t++first1;\n\t\t++first2;\n\t}\n\tif ((first1 != last1) && (first2 != last2)) {\n\t\t// ?\n\t\tm1 = last1; m2 = first2;\n\t\twhile ((m1 != first1) && (m2 != last2)) {\n\t\t\tstd::iter_swap(--m1, m2);\n\t\t\t++m2;\n\t\t}\n\t\t// ?\n\t\tstd::reverse(first1, m1);\n\t\tstd::reverse(first1, last1);\n\t\tstd::reverse(m2, last2);\n\t\tstd::reverse(first2, last2);\n\t}\n\treturn !result;\n}\n\ntemplate < class BidirectionalIterator >\nbool next_combination(BidirectionalIterator first,\n\tBidirectionalIterator middle,\n\tBidirectionalIterator last)\n{\n\treturn next_combination(first, middle, middle, last);\n}\n\n\n\nint main() {\n\twhile (1) {\n\t\tint w, d; cin >> w >> d;\n\t\tif (!w)break;\n\t\tvector<int>ws;\n\t\tvector<int>ds;\n\t\tif (w >= d) {\n\t\t\tfor (int i = 0; i < w; ++i) {\n\t\t\t\tint a; cin >> a; ws.push_back(a);\n\t\t\t}\n\t\t\tfor (int i = 0; i <d; ++i) {\n\t\t\t\tint a; cin >> a; ds.push_back(a);\n\t\t\t}\n\t\t}\n\t\telse {\n\t\t\tfor (int i = 0; i < w; ++i) {\n\t\t\t\tint a; cin >> a; ds.push_back(a);\n\t\t\t}\n\t\t\tfor (int i = 0; i <d; ++i) {\n\t\t\t\tint a; cin >> a; ws.push_back(a);\n\t\t\t}\n\t\t}\n\t\tassert(ws.size() >= ds.size());\n\n\t\tvector<int>perm;\n\t\tfor (int i = 0; i < ws.size(); ++i) {\n\t\t\tperm.push_back(i);\n\t\t}\n\t\tint finans = 500;\n\n\n\t\tdo {\n\t\t\tint nans = 0;\n\t\t\tvector<bool>used(ws.size(), false);\n\t\t\tfor (int i = 0; i < ds.size(); ++i) {\n\t\t\t\tif (ds[i] == ws[perm[i]]) {\n\t\t\t\t\tnans += ds[i];\n\t\t\t\t}\n\t\t\t\telse {\n\t\t\t\t\tnans += ds[i] + ws[perm[i]];\n\t\t\t\t}\n\t\t\t\tused[perm[i]] = true;\n\t\t\t}\n\t\t\tfor (int i = 0; i < ws.size(); ++i) {\n\t\t\t\tif (!used[i]) {\n\t\t\t\t\tnans += ws[i];\n\t\t\t\t}\n\t\t\t}\n\t\t\tfinans = min(finans, nans);\n\n\t\t} while (next_permutation(perm.begin(), perm.end()));\n\t\tcout << finans << endl;\n\n\t}\n\n\treturn 0;\n}", "accuracy": 1, "time_ms": 5280, "memory_kb": 3116, "score_of_the_acc": -1.6916, "final_rank": 6 }, { "submission_id": "aoj_1295_1623643", "code_snippet": "#include<stdio.h>\n#include <iostream>\n#include <math.h>\n#include <numeric>\n#include <vector>\n#include <map>\n#include <functional>\n#include <stdio.h>\n#include <array>\n#include <algorithm>\n#include <string>\n#include <string.h>\n#include <assert.h>\n#include <stdio.h>\n#include <queue>\n#include<iomanip>\n#include<bitset>\n#include<stack>\n#include<set>\n#include<limits>\n#include <complex>\nusing namespace std;\ntemplate < class BidirectionalIterator >\nbool next_combination(BidirectionalIterator first1,\n\tBidirectionalIterator last1,\n\tBidirectionalIterator first2,\n\tBidirectionalIterator last2)\n{\n\tif ((first1 == last1) || (first2 == last2)) {\n\t\treturn false;\n\t}\n\tBidirectionalIterator m1 = last1;\n\tBidirectionalIterator m2 = last2; --m2;\n\twhile (--m1 != first1 && !(*m1 < *m2)) {\n\t}\n\tbool result = (m1 == first1) && !(*first1 < *m2);\n\tif (!result) {\n\t\t// ?\n\t\twhile (first2 != m2 && !(*m1 < *first2)) {\n\t\t\t++first2;\n\t\t}\n\t\tfirst1 = m1;\n\t\tstd::iter_swap(first1, first2);\n\t\t++first1;\n\t\t++first2;\n\t}\n\tif ((first1 != last1) && (first2 != last2)) {\n\t\t// ?\n\t\tm1 = last1; m2 = first2;\n\t\twhile ((m1 != first1) && (m2 != last2)) {\n\t\t\tstd::iter_swap(--m1, m2);\n\t\t\t++m2;\n\t\t}\n\t\t// ?\n\t\tstd::reverse(first1, m1);\n\t\tstd::reverse(first1, last1);\n\t\tstd::reverse(m2, last2);\n\t\tstd::reverse(first2, last2);\n\t}\n\treturn !result;\n}\n\ntemplate < class BidirectionalIterator >\nbool next_combination(BidirectionalIterator first,\n\tBidirectionalIterator middle,\n\tBidirectionalIterator last)\n{\n\treturn next_combination(first, middle, middle, last);\n}\n\n\n\nint main() {\n\twhile (1) {\n\t\tint w, d; cin >> w >> d;\n\t\tif (!w)break;\n\t\tvector<int>ws;\n\t\tvector<int>ds;\n\t\tif (w >= d) {\n\t\t\tfor (int i = 0; i < w; ++i) {\n\t\t\t\tint a; cin >> a; ws.push_back(a);\n\t\t\t}\n\t\t\tfor (int i = 0; i <d; ++i) {\n\t\t\t\tint a; cin >> a; ds.push_back(a);\n\t\t\t}\n\t\t}\n\t\telse {\n\t\t\tfor (int i = 0; i < w; ++i) {\n\t\t\t\tint a; cin >> a; ds.push_back(a);\n\t\t\t}\n\t\t\tfor (int i = 0; i <d; ++i) {\n\t\t\t\tint a; cin >> a; ws.push_back(a);\n\t\t\t}\n\t\t}\n\t\tassert(ws.size() >= ds.size());\n\t\t\n\t\t\n\t\tvector<int>perm;\n\t\tfor (int i = 0; i < ws.size(); ++i) {\n\t\t\tperm.push_back(i);\n\t\t}\n\t\tint finans = 500;\n\n\n\t\tdo {\n\t\t\tint nans = 0;\n\t\t\tfor (int i = 0; i < ds.size(); ++i) {\n\t\t\t\tif (ds[i] == ws[perm[i]]) {\n\t\t\t\t\tnans += ds[i];\n\t\t\t\t}\n\t\t\t\telse {\n\t\t\t\t\tnans += ds[i] + ws[perm[i]];\n\t\t\t\t}\n\t\t\t}\n\t\t\tfor (int i = ds.size(); i < ws.size(); ++i) {\n\t\t\t\tnans += ws[perm[i]];\n\t\t\t}\n\t\t\tfinans = min(finans, nans);\n\n\t\t} while (next_permutation(perm.begin(), perm.end()));\n\t\tcout << finans << endl;\n\n\t}\n\n\treturn 0;\n}", "accuracy": 1, "time_ms": 1070, "memory_kb": 3140, "score_of_the_acc": -0.9019, "final_rank": 1 }, { "submission_id": "aoj_1295_558103", "code_snippet": "#include<iostream>\n\nusing namespace std;\n\n#define rep(i, n) for (int i = 0; i < int(n); ++i)\n\nint dp[11][1 << 10];\n\nint main() {\n while (true) {\n int w, d, a[10], b[10];\n cin >> w >> d;\n if (w == 0 && d == 0) break;\n rep (i, w) cin >> a[i];\n rep (i, d) cin >> b[i];\n rep (i, 11) rep (j, 1 << 10) dp[i][j] = 1e9;\n dp[0][0] = 0;\n rep (i, d) {\n int mask1 = 0, mask2 = 0;\n rep (j, w) if (a[j] >= b[i]) mask1 |= 1 << j;\n rep (j, w) if (a[j] <= b[i]) mask2 |= 1 << j;\n rep (k, 1 << w) rep (l, 1 << w) if (mask1 & l) {\n\tint sum = 0;\n\trep (j, w) {\n\t if (l & 1 << j) {\n\t sum += min(a[j], b[i]);\n\t }\n\t}\n\tdp[i + 1][k | (l & mask2)] = min(dp[i + 1][k | (l & mask2)], dp[i][k] + sum);\n }\n }\n cout << dp[d][(1 << w) - 1] << endl;\n }\n return 0;\n}", "accuracy": 1, "time_ms": 6100, "memory_kb": 1204, "score_of_the_acc": -1, "final_rank": 2 } ]
aoj_1289_cpp
Problem E: Spherical Mirrors A long time ago in a galaxy, far, far away, there were N spheres with various radii. Spheres were mirrors, that is, they had reflective surfaces . . . . You are standing at the origin of the galaxy (0, 0, 0), and emit a laser ray to the direction ( u , v , w ). The ray travels in a straight line. When the laser ray from I hits the surface of a sphere at Q , let N be a point outside of the sphere on the line connecting the sphere center and Q. The reflected ray goes to the direction towards R that satisfies the following conditions: (1) R is on the plane formed by the three points I , Q and N , (2) ∠ IQN = ∠ NQR , as shown in Figure 1. Figure 1: Laser ray reflection After it is reflected several times, finally it goes beyond our observation. Your mission is to write a program that identifies the last reflection point. Input The input consists of multiple datasets, each in the following format. N u v w x 1 y 1 z 1 r 1 . . . x N y N z N r N The first line of a dataset contains a positive integer N which is the number of spheres. The next line contains three integers u , v and w separated by single spaces, where ( u , v , w ) is the direction of the laser ray initially emitted from the origin. Each of the following N lines contains four integers separated by single spaces. The i -th line corresponds to the i -th sphere, and the numbers represent the center position ( x i , y i , z i ) and the radius r i . N , u , v , w , x i , y i , z i and r i satisfy the following conditions. 1 ≤ N ≤ 100 −100 ≤ u , v , w ≤ 100 −100 ≤ x i , y i , z i ≤ 100 5 ≤ r i ≤ 30 u 2 + v 2 + w 2 > 0 You can assume that the distance between the surfaces of any two spheres is no less than 0.1. You can also assume that the origin (0, 0, 0) is located outside of any sphere, and is at least 0.1 distant from the surface of any sphere. The ray is known to be reflected by the sphere surfaces at least once, and at most five times. You can assume that the angle between the ray and the line connecting the sphere center and the reflection point, which is known as the angle of reflection (i.e. θ in Figure 1), is less than 85 degrees for each point of reflection. The last dataset is followed by a line containing a single zero. Output For each dataset in the input, you should print the x-, y- and z-coordinates of the last reflection point separated by single spaces in a line. No output line should contain extra characters. No coordinate values in the output should have an error greater than 0.01. Sample Input 3 -20 -20 -24 100 100 100 30 10 8 3 5 -70 -70 -84 5 4 0 47 84 -23 41 42 8 45 -10 14 19 -5 28 47 12 -27 68 34 14 0 Output for the Sample Input 79.0940 79.0940 94.9128 -21.8647 54.9770 34.1761
[ { "submission_id": "aoj_1289_7221053", "code_snippet": "#include <iostream>\n#include <cmath>\nusing namespace std;\n\nstruct Point3D {\n\tdouble px, py, pz;\n};\n\nPoint3D operator+(const Point3D &a1, const Point3D &a2) {\n\treturn Point3D{ a1.px + a2.px, a1.py + a2.py, a1.pz + a2.pz };\n}\n\nPoint3D operator-(const Point3D &a1, const Point3D &a2) {\n\treturn Point3D{ a1.px - a2.px, a1.py - a2.py, a1.pz - a2.pz };\n}\n\nPoint3D operator*(const Point3D &a1, const double &a2) {\n\treturn Point3D{ a1.px * a2, a1.py * a2, a1.pz * a2 };\n}\n\ndouble ABS(Point3D A1) {\n\treturn sqrt(A1.px * A1.px + A1.py * A1.py + A1.pz * A1.pz);\n}\n\ndouble dot(Point3D A1, Point3D A2) {\n\treturn A1.px * A2.px + A1.py * A2.py + A1.pz * A2.pz;\n}\n\nPoint3D Normalization(Point3D A1) {\n\tdouble LEN = ABS(A1);\n\treturn A1 * (1.0 / LEN);\n}\n\n// Get next laser vector, when reflect point is A1 and current laser vector is A2\nPoint3D NextVector(Point3D A1, Point3D A2) {\n\tdouble val = dot(A1, A2); val *= -1.0;\n\tif (abs(val) < 1e-6) return (A2 * -1.0);\n\tdouble LEN = val / ABS(A1);\n\tPoint3D Projection = A1 * (LEN / ABS(A1));\n\treturn (Projection * 2.0) + A2;\n}\nint N;\nPoint3D Laser;\nPoint3D P[109]; double R[109];\n\n// Return the index of sphere that point A is inside\nint check(Point3D A) {\n\tfor (int i = 1; i <= N; i++) {\n\t\tif (ABS(A - P[i]) <= R[i]) return i;\n\t}\n\treturn -1;\n}\n\n// Binary Search \"the hit place\"\ndouble search(Point3D Current) {\n\tdouble cl = 0.00, cr = 0.01, cm;\n\tfor (int i = 0; i < 30; i++) {\n\t\tcm = (cl + cr) / 2.0;\n\t\tPoint3D TargetPlace = Current + (Laser * cm);\n\t\tif (check(TargetPlace) == -1) { cl = cm; }\n\t\telse { cr = cm; }\n\t}\n\treturn cm;\n}\n\n// Return Answer\nPoint3D Simulation() {\n\tPoint3D Last = Point3D{ 0.0, 0.0, 0.0 };\n\tPoint3D Current = Point3D{ 0.0, 0.0, 0.0 };\n\t\n\t// Begin Simulation\n\twhile (true) {\n\t\tbool flag = false;\n\t\tfor (int i = 1; i <= 80000; i++) {\n\t\t\tPoint3D NextPlace = Current + (Laser * 0.01);\n\t\t\tint idx = check(NextPlace);\n\t\t\tif (idx == -1) {\n\t\t\t\tCurrent = NextPlace;\n\t\t\t\tcontinue;\n\t\t\t}\n\t\t\t\n\t\t\t// If NextPlace is inside sphere\n\t\t\tdouble Target = search(Current);\n\t\t\tPoint3D Reflected = Current + (Laser * Target);\n\t\t\tLast = Reflected;\n\t\t\tCurrent = Reflected;\n\t\t\tLaser = NextVector(Reflected - P[idx], Laser);\n\t\t\tLaser = Normalization(Laser);\n\t\t\tflag = true;\n\t\t\tbreak;\n\t\t}\n\t\tif (flag == false) return Last;\n\t}\n}\n\nint main() {\n\twhile (true) {\n\t\t// Input\n\t\tcin >> N; if (N == 0) break;\n\t\tcin >> Laser.px >> Laser.py >> Laser.pz;\n\t\tfor (int i = 1; i <= N; i++) cin >> P[i].px >> P[i].py >> P[i].pz >> R[i];\n\t\tLaser = Normalization(Laser);\n\t\t\n\t\t// Simulation\n\t\tPoint3D Answer = Simulation();\n\t\tprintf(\"%.12lf %.12lf %.12lf\\n\", Answer.px, Answer.py, Answer.pz);\n\t}\n\treturn 0;\n}", "accuracy": 1, "time_ms": 840, "memory_kb": 3468, "score_of_the_acc": -1.3941, "final_rank": 19 }, { "submission_id": "aoj_1289_6315107", "code_snippet": "#include<bits/stdc++.h>\n#define rep(i,a,...) for(int i = (a)*(strlen(#__VA_ARGS__)!=0);i<(int)(strlen(#__VA_ARGS__)?__VA_ARGS__:(a));++i)\n#define per(i,a,...) for(int i = (strlen(#__VA_ARGS__)?__VA_ARGS__:(a))-1;i>=(int)(strlen(#__VA_ARGS__)?(a):0);--i)\n#define foreach(i, n) for(auto &i:(n))\n#define all(x) (x).begin(), (x).end()\n#define bit(x) (1ll << (x))\n#define lambda(RES_TYPE, ...) (function<RES_TYPE(__VA_ARGS__)>)[&](__VA_ARGS__) -> RES_TYPE\n#define method(FUNC_NAME, RES_TYPE, ...) function<RES_TYPE(__VA_ARGS__)> FUNC_NAME = lambda(RES_TYPE, __VA_ARGS__)\nusing namespace std;\nusing ll = long long;\nusing pii = pair<int,int>;\nusing pll = pair<ll,ll>;\n//const ll MOD = (ll)1e9+7;\nconst ll MOD = 998244353;\nconst int INF = (ll)1e9+7;\nconst ll INFLL = (ll)1e18 + 10;\ntemplate<class t>\nusing vvector = vector<vector<t>>;\ntemplate<class t>\nusing vvvector = vector<vector<vector<t>>>;\ntemplate<class t>\nusing priority_queuer = priority_queue<t, vector<t>, greater<t>>;\ntemplate<class t, class u> bool chmax(t &a, u b){if(a<b){a=b;return true;}return false;}\ntemplate<class t, class u> bool chmin(t &a, u b){if(a>b){a=b;return true;}return false;}\n#ifdef DEBUG\n#define debug(x) cout<<\"LINE \"<<__LINE__<<\": \"<<#x<<\" = \"<<x<<endl;\n#else\n#define debug(x) (void)0\n#endif\n\nnamespace templates{\n ll modpow(ll x, ll b,ll mod=MOD){\n ll res = 1;\n while(b){\n if(b&1)res = res * x % mod;\n x = x * x % mod;\n b>>=1;\n }\n return res;\n }\n\n ll modinv(ll x){\n return modpow(x, MOD-2);\n }\n\n bool was_output = false;\n\n void print();\n template <class t> void print(const vector<t> &);\n template <class t, class u> void print(const pair<t, u> &);\n template <class t> void print(const t&);\n template <class Head, class... Tail> void print(const Head&, const Tail&...);\n\n template <class t> void println(const vector<vector<t>>&);\n template <class t> void println(const vector<t>&);\n template <class t> void println(const t&);\n template <class Head, class... Tail> void println(const Head&, const Tail&...);\n void println();\n void newline();\n\n void print(){\n return;\n }\n\n template <class t>\n void print(const vector<t>&x){\n for(const t&i:x)print(i);\n }\n template <class t, class u>\n void print(const pair<t,u>&p){\n print(p.first);\n print(p.second);\n }\n template <class t>\n void print(const t&x){\n if(was_output)cout<<\" \";\n cout<<x;\n was_output = true;\n }\n template <class Head, class... Tail>\n void print(const Head&head,const Tail&...tail){\n print(head);\n print(tail...);\n }\n\n template<class t>\n void println(const vector<vector<t>>&x){\n for(vector<t> i:x)println(i);\n }\n template<class t>\n void println(const vector<t>&x){\n for(const t&i:x)print(i);\n println();\n }\n template <class t>\n void println(const t&x){\n print(x);\n println();\n }\n void println(){\n if(was_output){\n cout << endl;\n was_output = false;\n }\n }\n template <class Head, class... Tail>\n void println(const Head&head,const Tail&...tail){\n print(head);\n println(tail...);\n }\n\n void newline(){\n was_output = true;\n println();\n }\n\n template<class t>\n istream& operator>>(istream&is, vector<t>&x){\n for(auto &i:x)is >> i;\n return is;\n }\n\n template<class t, class u>\n istream& operator>>(istream&is, pair<t, u>&x){\n is >> x.first >> x.second;\n return is;\n }\n\n template<class t>\n ostream& operator<<(ostream&os, vector<t> &v){\n os << \"{\";\n for(t &i:v){\n os << i << \", \";\n }\n os << \"}\";\n return os;\n }\n\n template<class t = long long>\n t in(){\n t res; cin >> res; return res;\n }\n\n template<class t>\n vector<t> sorted(vector<t> line,function<bool(t,t)> comp=[](t a,t b){return a<b;}){\n sort(line.begin(),line.end(),comp);\n return line;\n }\n\n template<class t>\n vector<t> reversed(vector<t> line){\n reverse(line.begin(),line.end());\n return line;\n }\n string reversed(string str){\n reverse(str.begin(),str.end());\n return str;\n }\n\n long long gcd(long long a,long long b){\n while(b){\n a %= b;\n swap(a,b);\n }\n return a;\n }\n\n long long lcm(long long a,long long b){\n return a / gcd(a,b) * b;\n }\n\n class output_initializer{\n public:\n output_initializer(){\n ios::sync_with_stdio(false);\n cin.tie(nullptr);\n cout << setprecision(20);\n }\n };output_initializer OUTPUT_INITIALIZER_INSTANCE = output_initializer();\n}\n\nusing namespace templates;\n\nusing V = vector<double>;\n\nV operator+(V a,V b){\n V res(3);\n rep(i,3){\n res[i] = a[i] + b[i];\n }\n return res;\n}\nV operator-(V a,V b){\n V res(3);\n rep(i,3){\n res[i] = a[i] - b[i];\n }\n return res;\n}\nV operator*(V a,double k){\n V res(3);\n rep(i,3){\n res[i] = a[i] * k;\n }\n return res;\n}\nV operator/(V a, double k) { return a * (1 / k); }\n\nV readV(){\n V res(3);\n foreach(i,res)i=in();\n return res;\n}\n\ndouble abs(V x){\n double res = 0;\n rep(i,3)res += x[i] * x[i];\n return sqrt(res);\n}\ndouble dot(V x,V y){\n double res = 0;\n rep(i,3)res += x[i] * y[i];\n return res;\n}\nV normal(V x){\n return x / abs(x);\n}\n\nvector<double> func(int n){\n V p(3,0);\n V dir = normal(readV());\n vector<V> spheres(n);\n vector<double> Rs(n);\n rep(i,n){\n spheres[i] = readV();\n Rs[i] = in();\n }\n V last;\n while(true){\n double dist = INF;\n int itr = -1;\n rep(i,n){\n V d = normal(spheres[i] - p);\n if(dot(dir,d) < 0)continue;\n double lp = 0;\n double rp = INF;\n rep(_,100){\n double mid1 = (lp + lp + rp) / 3;\n double mid2 = (lp + rp + rp) / 3;\n if(abs(p + dir * mid1 - spheres[i]) < abs(p + dir * mid2 - spheres[i])){\n rp = mid2;\n }else{\n lp = mid1;\n }\n }\n if(abs(p + dir * lp - spheres[i]) > Rs[i]+0.01)continue;\n lp = 0;\n rep(_,100){\n double mid = (lp + rp) / 2;\n if(abs(p + dir * mid - spheres[i]) < Rs[i]){\n rp = mid;\n }else{\n lp = mid;\n }\n }\n if(chmin(dist,lp)){\n itr = i;\n }\n }\n if(itr == -1)break;\n V at = p + dir * dist;\n last = at;\n V d = normal(at - spheres[itr]);\n dir = normal(dir - d * dot(dir,d) * 2);\n p = last;\n }\n return last;\n}\n\nint main(){\n ios::sync_with_stdio(false);\n cin.tie(nullptr);\n while(true){\n int n = in();\n if(n==0)break;\n println(func(n));\n }\n return 0;\n}", "accuracy": 1, "time_ms": 200, "memory_kb": 3432, "score_of_the_acc": -1.0235, "final_rank": 18 }, { "submission_id": "aoj_1289_4900300", "code_snippet": "#include \"iostream\"\n#include \"climits\"\n#include \"list\"\n#include \"queue\"\n#include \"stack\"\n#include \"set\"\n#include \"functional\"\n#include \"algorithm\"\n#include \"string\"\n#include \"map\"\n#include \"unordered_map\"\n#include \"unordered_set\"\n#include \"iomanip\"\n#include \"cmath\"\n#include \"random\"\n#include \"bitset\"\n#include \"cstdio\"\n#include \"numeric\"\n#include \"cassert\"\n#include \"ctime\"\n\nusing namespace std;\n\nconstexpr long long int MOD = 1000000007;\n//constexpr int MOD = 1000000007;\n//constexpr int MOD = 998244353;\n//constexpr long long int MOD = 998244353;\nconstexpr double EPS = 1e-9;\n\n//int N, M, K, T, H, W, L, R;\nlong long int N, M, K, T, H, W, L, R;\n\nstruct Point3d {\n\tlong double x, y, z;\n\tPoint3d(const long double xx = 0, const long double yy = 0, const long double zz = 0) {\n\t\tx = xx, y = yy, z = zz;\n\t}\n\tvoid Input() {\n\t\tcin >> x >> y >> z;\n\t}\n\tlong double Distance(Point3d p) {\n\t\treturn hypot(hypot(x - p.x, y - p.y), z - p.z);\n\t}\n\tPoint3d operator+(const Point3d&p)const {\n\t\treturn Point3d(x + p.x, y + p.y, z + p.z);\n\t}\n\tPoint3d operator-(const Point3d&p)const {\n\t\treturn Point3d(x - p.x, y - p.y, z - p.z);\n\t}\n\tPoint3d operator*(const long double r)const {\n\t\treturn Point3d(x*r, y*r, z*r);\n\t}\n\tvoid Output() {\n\t\tcout << fixed << setprecision(20) << x << \" \" << y << \" \" << z << endl;\n\t}\n};\n\nstruct Line {\n\tPoint3d st, ed;\n\tLine(const Point3d aa, const Point3d bb) {\n\t\tst = aa, ed = bb;\n\t}\n\tlong double Distance(Point3d p) {\n\t\tp = p - st;\n\t\tauto ced = ed - st;\n\t\tauto cst = st - st;\n\t\t{\n\t\t\tlong double rad = atan2(ced.z, ced.x);\n\t\t\tp = Point3d(hypot(p.x, p.z)*cos(atan2(p.z, p.x) - rad), p.y, hypot(p.x, p.z)*sin(atan2(p.z, p.x) - rad));\n\t\t\tced = Point3d(hypot(ced.x, ced.z), ced.y, 0);\n\t\t}\n\t\t{\n\t\t\tlong double rad = atan2(ced.y, ced.x);\n\t\t\tp = Point3d(hypot(p.x, p.y)*cos(atan2(p.y, p.x) - rad), hypot(p.x, p.y)*sin(atan2(p.y, p.x) - rad), p.z);\n\t\t\tced = Point3d(hypot(ced.x, ced.y), 0, 0);\n\t\t}\n\t\tp = Point3d(p.x, hypot(p.y, p.z), 0);\n\t\tif (0 <= p.x&&p.x <= ced.x)return p.y;\n\t\treturn hypot(min(abs(p.x), abs(p.x - ced.x)), p.y);\n\t}\n\tbool Intersect(Point3d p, long double r) {\n\t\treturn Distance(p) <= r;\n\t}\n};\n\nPoint3d reflect(Point3d a, Point3d b) {\n\tlong double radz = atan2(b.z, b.x);\n\ta = Point3d(hypot(a.x, a.z)*cos(atan2(a.z, a.x) - radz), a.y, hypot(a.x, a.z)*sin(atan2(a.z, a.x) - radz));\n\tb = Point3d(hypot(b.x, b.z), b.y, 0);\n\tlong double rady = atan2(b.y, b.x);\n\ta = Point3d(hypot(a.x, a.y)*cos(atan2(a.y, a.x) - rady), hypot(a.x, a.y)*sin(atan2(a.y, a.x) - rady), a.z);\n\tb = Point3d(hypot(b.x, b.y), 0, 0);\n\ta.x *= -1;\n\tradz *= -1, rady *= -1;\n\ta = Point3d(hypot(a.x, a.y)*cos(atan2(a.y, a.x) - rady), hypot(a.x, a.y)*sin(atan2(a.y, a.x) - rady), a.z);\n\ta = Point3d(hypot(a.x, a.z)*cos(atan2(a.z, a.x) - radz), a.y, hypot(a.x, a.z)*sin(atan2(a.z, a.x) - radz));\n\treturn a;\n}\n\nint main() {\n\tios::sync_with_stdio(false);\n\tcin.tie(0);\n\n\twhile (cin >> N, N) {\n\t\tPoint3d st, v;\n\t\tv.Input();\n\t\tvector<long double>ra(N);\n\t\tvector<Point3d>p(N);\n\t\tfor (int i = 0; i < N; i++) {\n\t\t\tp[i].Input();\n\t\t\tcin >> ra[i];\n\t\t}\n\t\tint bef = -1;\n\t\twhile (1) {\n\t\t\tint nx = -1;\n\t\t\tlong double l = 0, r = 500;\n\t\t\tfor (int loop = 0; loop < 100; loop++) {\n\t\t\t\tlong double mid = (l + r) / 2;\n\t\t\t\tbool ok = false;\n\t\t\t\tLine line(st, st + v * mid);\n\t\t\t\tfor (int i = 0; i < N; i++) {\n\t\t\t\t\tif (i == bef)continue;\n\t\t\t\t\tif (line.Intersect(p[i], ra[i])) {\n\t\t\t\t\t\tok = true;\n\t\t\t\t\t\tnx = i;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\tif (ok)r = mid;\n\t\t\t\telse l = mid;\n\t\t\t}\n\t\t//\tcout << l << \" \" << nx << endl;\n\t\t\tif (nx == -1)break;\n\t\t\tauto nv = v;\n\t\t\tbef = nx;\n\t\t\tauto nst = st + v * l;\n\t\t\tnv = reflect(v, nst - p[nx]);\n\t\t\tv = nv;\n\t\t\tst = nst;\n\t\t//\tv.Output();\n\t\t}\n\t\tst.Output();\n\t}\n}", "accuracy": 1, "time_ms": 1810, "memory_kb": 3628, "score_of_the_acc": -2, "final_rank": 20 }, { "submission_id": "aoj_1289_3228658", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\n#define rep(i,n) for(int (i)=0;(i)<(int)(n);++(i))\n#define all(x) (x).begin(),(x).end()\n#define pb push_back\n#define fi first\n#define se second\n#define dbg(x) cout<<#x\" = \"<<((x))<<endl\ntemplate<class T,class U> ostream& operator<<(ostream& o, const pair<T,U> &p){o<<\"(\"<<p.fi<<\",\"<<p.se<<\")\";return o;}\ntemplate<class T> ostream& operator<<(ostream& o, const vector<T> &v){o<<\"[\";for(T t:v){o<<t<<\",\";}o<<\"]\";return o;}\n\nstruct Point{\n double X,Y,Z;\n\n void PRINT(){\n printf(\"%.8f %.8f %.8f\\n\",X,Y,Z);\n }\n\n void ADD(Point p){\n X += p.X;\n Y += p.Y;\n Z += p.Z;\n }\n\n void MUL(double m){\n X *= m;\n Y *= m;\n Z *= m;\n }\n\n double norm(){\n return X*X + Y*Y + Z*Z;\n }\n\n double size(){\n return sqrt(norm());\n }\n\n void normalize(){\n double sz = size();\n X /= sz;\n Y /= sz;\n Z /= sz;\n }\n};\n\nusing Vec = Point;\n\ndouble dist(Point p, Point q){\n double dx = p.X-q.X;\n double dy = p.Y-q.Y;\n double dz = p.Z-q.Z;\n return sqrt(dx*dx + dy*dy + dz*dz);\n}\n\ndouble dot(Vec v, Vec u){\n return v.X*u.X + v.Y*u.Y + v.Z*u.Z;\n}\n\nPoint READ(){\n double x,y,z;\n cin >>x >>y >>z;\n return {x,y,z};\n}\n\nconst double LIM = 1000;\nconst double EPS = 1e-7;\n\nint main(){\n int n;\n while(cin >>n,n){\n Vec v = READ();\n\n vector<Point> c(n);\n vector<double> r(n);\n rep(i,n){\n c[i] = READ();\n cin >>r[i];\n }\n\n Point now = {0,0,0};\n v.normalize();\n\n // Segment to Sphere\n auto distSS = [&](Point start, Vec dir, int idx){\n double L=0, R=LIM;\n rep(loop,100){\n double m1 = (2*L+R)/3 , m2 = (L+2*R)/3;\n Vec v1(dir), v2(dir);\n v1.MUL(m1);\n v2.MUL(m2);\n\n Point p1(start), p2(start);\n p1.ADD(v1);\n p2.ADD(v2);\n\n if(dist(c[idx],p1) > dist(c[idx],p2)) L=m1;\n else R=m2;\n }\n\n Vec add(dir);\n add.MUL(R);\n Point pos(start);\n pos.ADD(add);\n\n // NOT intersect\n if( dist(pos, c[idx]) > r[idx]+EPS ) return LIM;\n\n // 交点までのパラメータを改めて求める\n L = 0;\n rep(loop,100){\n double mid = (L+R)/2;\n Vec vv(dir);\n vv.MUL(mid);\n\n Point pp(start);\n pp.ADD(vv);\n\n if(dist(c[idx],pp) < r[idx]) R = mid;\n else L = mid;\n }\n return L;\n };\n\n int prev = -1;\n while(1){\n double min_t = LIM;\n int idx = -1;\n rep(i,n)if(prev!=i){\n double tt = distSS(now,v,i);\n // dbg(i);dbg(tt);\n if(tt < min_t){\n min_t = tt;\n idx = i;\n }\n }\n\n if(idx == -1) break;\n\n Vec vv(v);\n vv.MUL(min_t);\n Point q(now);\n q.ADD(vv);\n\n // dbg(dist(c[idx],q));\n Vec qn = {q.X-c[idx].X, q.Y-c[idx].Y, q.Z-c[idx].Z};\n qn.normalize();\n\n Vec qi(v);\n qi.MUL(-1);\n\n // qn.PRINT();\n // qi.PRINT();\n\n double cos_theta = dot(qn,qi);\n // dbg(cos_theta);\n\n double D = dist(now,q);\n // dbg(D);\n D *= cos_theta;\n\n Vec hh(qn);\n hh.MUL(D);\n Point H(q);\n H.ADD(hh);\n\n Vec nh = {H.X-now.X, H.Y-now.Y, H.Z-now.Z};;\n nh.MUL(2);\n\n Point ref(now);\n ref.ADD(nh);\n\n now = q;\n v = {ref.X-q.X, ref.Y-q.Y, ref.Z-q.Z};\n v.normalize();\n // printf(\"v = \");v.PRINT();\n prev = idx;\n }\n\n now.PRINT();\n }\n return 0;\n}", "accuracy": 1, "time_ms": 50, "memory_kb": 3200, "score_of_the_acc": -0.843, "final_rank": 17 }, { "submission_id": "aoj_1289_2424194", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\n#define int long long\n\n#define EPS (1e-10)\n#define equals(a,b) (fabs((a)-(b)) < EPS)\n#define PI 3.141592653589793238\nstruct Point3D{\n double x,y,z;\n Point3D(){}\n Point3D(double x,double y,double z):x(x),y(y),z(z){}\n Point3D operator+(Point3D p) {return Point3D(x+p.x,y+p.y,z+p.z);}\n Point3D operator-(Point3D p) {return Point3D(x-p.x,y-p.y,z-p.z);}\n Point3D operator*(double k){return Point3D(x*k,y*k,z*k);}\n Point3D operator/(double k){return Point3D(x/k,y/k,z/k);}\n double norm(){return x*x+y*y+z*z;}\n double abs(){return sqrt(norm());}\n bool operator < (const Point3D &p) const{\n if(x!=p.x) return x<p.x;\n if(y!=p.y) return y<p.y;\n return z<p.z;\n }\n bool operator == (const Point3D &p) const{\n return fabs(x-p.x)<EPS && fabs(y-p.y)<EPS && fabs(z-p.z)<EPS;\n }\n};\nistream &operator >> (istream &is,Point3D &p){\n is>>p.x>>p.y>>p.z;\n return is;\n}\nostream &operator << (ostream &os,Point3D p){\n os<<fixed<<setprecision(12)<<p.x<<\" \"<<p.y<<\" \"<<p.z;\n return os;\n}\n\ntypedef Point3D Vector3D;\ntypedef vector<Point3D> Polygon3D;\n\nstruct Segment3D{\n Point3D p1,p2;\n Segment3D(){}\n Segment3D(Point3D p1, Point3D p2):p1(p1),p2(p2){}\n};\ntypedef Segment3D Line3D;\n\nistream &operator >> (istream &is,Segment3D &s){\n is>>s.p1>>s.p2;\n return is;\n}\n\nstruct Sphere{\n Point3D c;\n double r;\n Sphere(){}\n Sphere(Point3D c,double r):c(c),r(r){}\n};\n\nistream &operator >> (istream &is,Sphere &c){\n is>>c.c>>c.r;\n return is;\n}\n\ndouble norm(Vector3D a){\n return a.x*a.x+a.y*a.y+a.z*a.z;\n}\ndouble abs(Vector3D a){\n return sqrt(norm(a));\n}\ndouble dot(Vector3D a,Vector3D b){\n return a.x*b.x+a.y*b.y+a.z*b.z;\n}\nVector3D cross(Vector3D a,Vector3D b){\n return Vector3D(a.y*b.z-a.z*b.y,a.z*b.x-a.x*b.z,a.x*b.y-a.y*b.x);\n}\n\nPoint3D project(Line3D l,Point3D p){\n Point3D b=l.p2-l.p1;\n double t=dot(p-l.p1,b)/norm(b);\n return l.p1+b*t;\n}\n\ndouble getDistanceLP(Line3D l,Point3D p){\n return abs(cross(l.p2-l.p1,p-l.p1)/abs(l.p2-l.p1));\n}\n\ndouble getDistanceSP(Segment3D s,Point3D p){\n if(dot(s.p2-s.p1,p-s.p1) < 0.0 ) return abs(p-s.p1);\n if(dot(s.p1-s.p2,p-s.p2) < 0.0 ) return abs(p-s.p2);\n return getDistanceLP(s,p);\n}\n\nbool intersectSC(Segment3D s,Sphere c){\n double d=getDistanceSP(s,c.c);\n if(d>c.r) return 0;\n return !((abs(s.p1-c.c)<=c.r)&&(abs(s.p2-c.c)<=c.r));\n}\n\n\nsigned main(){\n int n;\n while(cin>>n,n){\n Vector3D v;\n cin>>v;\n Sphere s[n];\n for(int i=0;i<n;i++) cin>>s[i];\n Point3D p(0,0,0);\n int last=-1;\n while(1){\n Segment3D ss(p,p+v*1e7);\n bool f=0;\n for(int i=0;i<n;i++){\n\tif(i==last) continue;\n\t//cout<<i<<\":\"<<getDistanceSP(ss,s[i].c)<<\" \"<<s[i].r<<endl;\n\tif(getDistanceSP(ss,s[i].c)<=s[i].r) f=1;\n }\n if(!f) break;\n double l=0,r=1e7;\n for(int k=0;k<100;k++){\n\tdouble m=(l+r)/2;\n\tss=Segment3D(p,p+v*m);\n\tf=0;\n\tfor(int i=0;i<n;i++){\n\t if(i==last) continue;\n\t if(getDistanceSP(ss,s[i].c)<=s[i].r) f=1;\n\t}\n\tif(f) r=m;\n\telse l=m;\n }\n ss=Segment3D(p,p+v*r);\n f=0;\n for(int i=0;i<n;i++){\n\tif(i==last) continue;\n\tif(getDistanceSP(ss,s[i].c)<=s[i].r){\n\t last=i;\n\t break;\n\t}\n }\n p=ss.p2;\n Vector3D nm=p-s[last].c;\n nm=nm/abs(nm);\n double len=dot(nm,v);\n v=v-nm*len*2;\n }\n cout<<p<<endl;\n }\n return 0;\n}", "accuracy": 1, "time_ms": 20, "memory_kb": 3188, "score_of_the_acc": -0.8213, "final_rank": 16 }, { "submission_id": "aoj_1289_1711070", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\n\ndouble eps = 1e-8, PI = acos(-1);\n\ndouble Sqrt(double x){\n return (x<0?0:sqrt(x));\n}\n\nbool eq(double a, double b){\n return (-eps<a-b&&a-b<eps);\n}\n\nstruct Point{\n double x,y,z;\n Point (double a=0,double b=0,double c=0): x(a),y(b),z(c) {}\n Point operator + (const Point &p)const{\n return Point(x+p.x,y+p.y,z+p.z);\n }\n Point operator - (const Point &p)const{\n return Point(x-p.x,y-p.y,z-p.z);\n }\n Point operator * (const double &r)const{\n return Point(x*r,y*r,z*r);\n }\n Point operator / (const double &r)const{\n return Point(x/r,y/r,z/r);\n }\n};\n\ntypedef Point Vector;\n\ndouble dot(Point a,Point b){\n return a.x*b.x+a.y*b.y+a.z*b.z;\n}\n\nPoint cross(Point a,Point b){\n double nx=a.y*b.z-b.y*a.z;\n double ny=a.z*b.x-b.z*a.x;\n double nz=a.x*b.y-b.x*a.y;\n return Point(nx,ny,nz);\n}\n\ndouble norm(Point a){\n return dot(a,a);\n}\n\ndouble abs(Point a){\n return sqrt( norm(a) );\n}\n\nstruct Line{\n Point p;\n Vector v;\n Line(Point a=Point(),Vector b=Vector()):p(a),v(b){}\n};\n\nstruct Plane{\n Point p;\n Vector v0,v1;\n Plane(Point a=Point(),Vector b=Vector(),Vector c=Vector())\n :p(a),v0(b),v1(c){}\n};\n\nVector cross(Plane p){\n return cross(p.v0,p.v1);\n}\n\nstruct Ball{\n Point p;\n double r;\n Ball(Point a=Point(),double b=1.0):p(a),r(b){}\n};\n\nvector<Point> getCrossPoint(Ball s,Line t){\n vector<Point> res;\n Vector a=s.p-t.p;\n Vector b=t.v;\n double base=dot(a,b)/abs(b);\n double hh=norm(cross(a,b))/norm(b);\n if(s.r*s.r+eps < hh )return res;\n double w=Sqrt( s.r*s.r - hh );\n res.push_back(t.p+t.v/abs(t.v)*(base-w));\n res.push_back(t.p+t.v/abs(t.v)*(base+w));\n return res;\n}\n\nPoint reflect(Point sp,Vector sk,Point t){\n return t-sk*dot(sk,t-sp)*2.0/norm(sk);\n}\n\nint n;\nPoint v;\nBall t[100];\n\nvoid solve(){\n Line a=Line( Point() , v );\n while(1){\n Line next;\n double mini=1e9;\n for(int i=0;i<n;i++){\n vector<Point> u=getCrossPoint(t[i],a);\n for(int j=0;j<(int)u.size();j++){\n Point np=u[j];\n double dist=abs(np-a.p);\n if(dist<eps)continue;\n if(dot(np-a.p,a.v)<0)continue;\n if(dist<mini){\n mini=dist;\n next.p=np;\n next.v=reflect(np,np-t[i].p,np+a.v)-np;\n }\n }\n }\n if(mini==1e9)break;\n a=next;\n }\n printf(\"%.8f %.8f %.8f\\n\",a.p.x,a.p.y,a.p.z);\n}\n\nint main(){\n while(1){\n cin>>n;\n if(n==0)break;\n cin>>v.x>>v.y>>v.z;\n for(int i=0;i<n;i++)cin>>t[i].p.x>>t[i].p.y>>t[i].p.z>>t[i].r;\n solve();\n }\n return 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 1256, "score_of_the_acc": -0.0067, "final_rank": 4 }, { "submission_id": "aoj_1289_1572544", "code_snippet": "#include <bits/stdc++.h>\n\nusing namespace std;\n\nnamespace {\n\n typedef double real;\n real EPS = 1e-8;\n\n struct Point {\n real x, y, z;\n Point() {}\n Point(real x, real y, real z) : x(x), y(y), z(z) {}\n };\n Point operator+(const Point& a, const Point& b) { return Point(a.x + b.x, a.y + b.y, a.z + b.z); }\n Point operator-(const Point& a, const Point& b) { return Point(a.x - b.x, a.y - b.y, a.z - b.z); }\n Point operator*(const Point& a, real k) { return Point(a.x * k, a.y * k, a.z * k); }\n Point operator/(const Point& a, real k) { return a * (1.0 / k); }\n istream& operator>>(istream& is, Point& p) { return is >> p.x >> p.y >> p.z; }\n ostream& operator<<(ostream& os, const Point& p) { return os << \"Point(\" << p.x << \",\" << p.y << \",\" << p.z << \")\"; }\n real dot(const Point& a, const Point& b) { return a.x * b.x + a.y * b.y + a.z * b.z; }\n real norm(const Point& a) { return sqrt(dot(a, a)); }\n real cos(const Point& a, const Point& b) { return dot(a, b) / (norm(a) * norm(b)); }\n real dist(const Point& a, const Point& b) {\n real dx = a.x - b.x;\n real dy = a.y - b.y;\n real dz = a.z - b.z;\n return sqrt(dx * dx + dy * dy + dz * dz);\n }\n\n struct Line {\n Point a, b;\n Line(Point a, Point b) : a(a), b(b) {}\n };\n Point projection(const Line& l, const Point& p) {\n Point u = (p - l.a), v = (l.b - l.a);\n return l.a + (v / norm(v)) * (dot(u, v) / norm(v));\n }\n Point reflection(const Line& l, const Point& p) {\n Point h = projection(l, p);\n return p + (h - p) * 2;\n }\n real dist(const Line& l, const Point& p) {\n Point q = projection(l, p);\n return norm(p - q);\n }\n\n int N;\n Point U;\n vector<Point> Q; vector<real> R;\n bool input() {\n cin >> N;\n if (N == 0) return false;\n cin >> U;\n Q.clear(); Q.resize(N);\n R.clear(); R.resize(N);\n for (int i = 0; i < N; i++) {\n cin >> Q[i] >> R[i];\n }\n return true;\n }\n\n bool reflect(const Point& org, const Point& v, int sphere_index) {\n int n = sphere_index;\n Point p = Q[n] - org;\n real cos_phi = cos(v, p);\n if (cos_phi < -EPS) return false;\n return dist(Line(org, org + v), Q[n]) < R[n];\n }\n pair<Point, Point> ref(const Point& org, const Point& v, int sphere_index) {\n int n = sphere_index;\n Point p = Q[n] - org;\n real cos_phi = cos(v, p);\n real d = dist(org, Q[n]);\n real r = R[n];\n real x = d * cos_phi - sqrt( (d * cos_phi) * (d * cos_phi) - d * d + r * r );\n Point refpoint = org + v / norm(v) * x;\n Point refvector = reflection(Line(Q[n], refpoint), org) - refpoint;\n return make_pair(refpoint, refvector);\n }\n\n pair<Point, Point> next(const Point& org, const Point& v) {\n vector< pair<Point, Point> > refs;\n for (int i = 0; i < N; i++) {\n if (reflect(org, v, i)) {\n refs.push_back(ref(org, v, i));\n }\n }\n if (refs.empty()) throw \"foo\";\n pair<Point, Point> r = refs[0];\n real d = dist(org, refs[0].first);\n for (int i = 0; i < refs.size(); i++) {\n real nd = dist(org, refs[i].first);\n if (nd < d) {\n d = nd;\n r = refs[i];\n }\n }\n return r;\n }\n\n void solve() {\n Point org(0, 0, 0);\n Point u = U;\n try {\n while (true) {\n pair<Point, Point> r = next(org, u);\n org = r.first;\n u = r.second;\n }\n } catch (const char* e) {\n printf(\"%.8f %.8f %.8f\\n\", org.x, org.y, org.z);\n }\n }\n}\n\nint main() {\n while (input()) solve();\n return 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 1336, "score_of_the_acc": -0.0402, "final_rank": 15 }, { "submission_id": "aoj_1289_1420698", "code_snippet": "#include <bits/stdc++.h>\n#define rep(i,n) for(int i=0;i<(int)(n);i++)\n#define rep1(i,n) for(int i=1;i<=(int)(n);i++)\n#define all(c) c.begin(),c.end()\n#define pb push_back\n#define fs first\n#define sc second\n#define show(x) cout << #x << \" = \" << x << endl\n#define chmin(x,y) x=min(x,y)\n#define chmax(x,y) x=max(x,y)\nusing namespace std;\ntypedef double D;\nstruct P{\n\tD x,y,z;\n\tP(D a,D b,D c){x=a,y=b,z=c;}\n};\nP operator+(P a,P b){\n\treturn P(a.x+b.x,a.y+b.y,a.z+b.z);\n}\nP operator-(P a,P b){\n\treturn P(a.x-b.x,a.y-b.y,a.z-b.z);\n}\nP operator*(P a,D b){\n\treturn P(a.x*b,a.y*b,a.z*b);\n}\nP operator/(P a,D b){\n\treturn P(a.x/b,a.y/b,a.z/b);\n}\ntypedef pair<P,P> L;\nD dot(P a,P b){return a.x*b.x+a.y*b.y+a.z*b.z;}\nD norm(P a){return dot(a,a);}\nP perp(L l,P p){\n\tD t=dot(p-l.fs,l.fs-l.sc)/norm(l.fs-l.sc);\n\treturn l.fs+(l.fs-l.sc)*t;\n}\nP refl(L l,P p){\n\treturn p+(perp(l,p)-p)*2.0;\n}\ndouble x_[100],y_[100],z_[100],r_[100];\nint main(){\n\twhile(true){\n\t\tint N;\n\t\tcin>>N;\n\t\tif(N==0) break;\n\t\tdouble a=0,b=0,c=0,d,e,f;\n\t\tcin>>d>>e>>f;\n\t\trep(i,N) cin>>x_[i]>>y_[i]>>z_[i]>>r_[i];\n\t\twhile(true){\n\t\t\tint id=-1;\n\t\t\tdouble mn=1e9;\n\t\t\trep(i,N){\n\t\t\t\tdouble x=x_[i],y=y_[i],z=z_[i],r=r_[i];\n\t\t\t\tdouble A=d*d+e*e+f*f;\n\t\t\t\tdouble B=2*(d*(a-x)+e*(b-y)+f*(c-z));\n\t\t\t\tdouble C=(a-x)*(a-x)+(b-y)*(b-y)+(c-z)*(c-z)-r*r;\n\t\t\t\tdouble D=B*B-4*A*C;\n\t\t\t\tif(D<0) continue;\n\t\t\t\tdouble t=(-B-sqrt(D))/(2*A);\n\t\t\t\tif(t<1e-7) continue;\n\t\t\t\tif(t<mn) mn=t,id=i;\n\t\t\t}\n\t\t\tif(id<0){\n\t\t\t\tprintf(\"%.4f %.4f %.4f\\n\",a,b,c);\n\t\t\t\tbreak;\n\t\t\t}\n\t\t\tdouble t=mn;\n\t\t\tdouble na=a+t*d,nb=b+t*e,nc=c+t*f;\n\t\t\tP p(a,b,c);\n\t\t\tP np(na,nb,nc);\n\t\t\tL l=L(P(x_[id],y_[id],z_[id]),np);\n\t\t\tP ref=refl(l,p);\n\t\t\tP nd=ref-np;\n\t\t\tnd=nd/norm(nd);\n\t\t\ta=na,b=nb,c=nc;\n\t\t\td=nd.x,e=nd.y,f=nd.z;\n\t\t}\n\t}\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 1240, "score_of_the_acc": 0, "final_rank": 1 }, { "submission_id": "aoj_1289_1156055", "code_snippet": "#include <iostream>\n#include <iomanip>\n#include <cmath>\n#include <cstdio>\n#include <algorithm>\n#include <vector>\n#include <memory>\n#include <cstring>\n#include <cassert>\n#include <numeric>\n#include <sstream>\n#include <complex>\n#include <map>\n#include <set>\n#include <queue>\n#include <stack>\n#include <cctype>\n#include <unordered_map>\n#include <unordered_set>\nusing namespace std;\n\n#define REP2(i, m, n) for (int i = (int)(m); i < (int)(n); i++)\n#define REP(i, n) REP2(i, 0, n)\n#define ALL(S) (S).begin(), (S).end()\n\ntemplate <typename T, typename E>\nostream &operator<<(std::ostream &os, const std::pair<T, E> &p){\n return os << \"(\" << p.first << \", \" << p.second << \")\";\n}\n\ntypedef long long ll;\n\nstruct xyz_t {\n double x;\n double y;\n double z;\n friend bool operator<(const xyz_t &p, const xyz_t &q){\n return p.x != q.x ? p.x < q.x : p.y != q.y ? p.y < q.y : p.z < q.z;\n }\n friend ostream &operator<<(ostream &os, const xyz_t &p){\n os << \"(\" << p.x << \", \" << p.y << \", \" << p.z << \")\";\n return os;\n }\n};\n\ninline double dot(const xyz_t &p1, const xyz_t &p2){\n return p1.x * p2.x + p1.y * p2.y + p1.z * p2.z;\n}\n\ninline double norm(const xyz_t &p){\n return dot(p, p);\n}\n\ninline double abs(const xyz_t &p){\n return sqrt(norm(p));\n}\n\nxyz_t direction(const xyz_t &p1, const xyz_t &p2){\n xyz_t vec;\n vec.x = p2.x - p1.x;\n vec.y = p2.y - p1.y;\n vec.z = p2.z - p1.z;\n return vec;\n}\n\nvoid normalize(xyz_t &p){\n double l = abs(p);\n p.x /= l; p.y /= l; p.z /= l;\n}\n\nxyz_t operator+(const xyz_t &p1, const xyz_t &p2){\n xyz_t p;\n p.x = p1.x + p2.x;\n p.y = p1.y + p2.y;\n p.z = p1.z + p2.z;\n return p;\n}\n\nxyz_t operator-(const xyz_t &p1, const xyz_t &p2){\n xyz_t p;\n p.x = p1.x - p2.x;\n p.y = p1.y - p2.y;\n p.z = p1.z - p2.z;\n return p;\n}\n\nxyz_t operator*(const xyz_t &p1, double scale){\n xyz_t p;\n p.x = p1.x * scale;\n p.y = p1.y * scale;\n p.z = p1.z * scale;\n return p;\n}\n\n\ndouble distance(const xyz_t &p1, const xyz_t &p2){\n return abs(p2 - p1);\n}\n\nbool intersect(xyz_t curr, xyz_t dir, const pair<xyz_t, double> &C){\n normalize(dir);\n double t = dot(direction(curr, C.first), dir);\n xyz_t p = curr + dir * t;\n // cout << t << \" \" << p << endl;\n return t > 0 && distance(p, C.first) < C.second;\n}\n\nxyz_t crossing_point(xyz_t curr, xyz_t dir, const pair<xyz_t, double> &C){\n normalize(dir);\n double t = dot(direction(curr, C.first), dir);\n xyz_t p = curr + dir * t;\n double d = sqrt(C.second * C.second - norm(p - C.first));\n return curr + dir * (t - d);\n \n}\n\nint main(){\n ios::sync_with_stdio(false);\n int N;\n while (cin >> N && N){\n xyz_t vec;\n vector<pair<xyz_t, double> > cs(N);\n cin >> vec.x >> vec.y >> vec.z;\n \n REP(i, N){\n cin >> cs[i].first.x >> cs[i].first.y >> cs[i].first.z >> cs[i].second;\n }\n \n xyz_t last;\n last.x = last.y = last.z = 0;\n \n while (true){\n // cout << last << endl;\n vector<tuple<double, int, xyz_t> > ps;\n \n REP(i, N) if (intersect(last, vec, cs[i])){\n // cout << last << \" \" << vec << \" \" << cs[i].first << \" \" << cs[i].second << endl;\n xyz_t p = crossing_point(last, vec, cs[i]);\n // cout << p << endl;\n ps.push_back(make_tuple(distance(p, last), i, p));\n }\n sort(ALL(ps));\n if (ps.empty()){\n break;\n }\n\n xyz_t a = get<2>(ps[0]) - cs[get<1>(ps[0])].first;\n xyz_t v = last - get<2>(ps[0]);\n vec = a * 2 * (dot(a, v) / dot(a, a)) - v;\n normalize(vec);\n // cout << a << \" \" << v << \" \" << dot(a, v) << \" \" << get<2>(ps[0]) << \" \" << vec << endl;\n // break;\n \n last = get<2>(ps[0]);\n }\n \n printf(\"%.10lf %.10lf %.10lf\\n\", last.x, last.y, last.z);\n }\n return 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 1276, "score_of_the_acc": -0.0151, "final_rank": 11 }, { "submission_id": "aoj_1289_1133846", "code_snippet": "#include <iostream>\n#include <vector>\n#include <cmath>\n#include <cstdio>\n\nusing namespace std;\n\nclass P3 {\npublic:\n\tdouble x, y, z;\n\tP3(double x=0, double y=0, double z=0) : x(x), y(y), z(z) {}\n\tP3(const P3& p) : x(p.x), y(p.y), z(p.z) {}\n\tP3 operator + (const P3 &p) const { return P3(x+p.x, y+p.y, z+p.z); }\n\tP3 operator - (const P3 &p) const { return P3(x-p.x, y-p.y, z-p.z); }\n\tP3 operator * (double p) const { return P3(x*p, y*p, z*p); }\n\tvoid scale(double s){ x *= s, y *= s, z *= s; }\n\tdouble dot(const P3& p) const { return x*p.x + y*p.y + z*p.z; }\n\tdouble squaredLength() const { return x*x + y*y + z*z; }\n\tdouble length() const { return sqrt(squaredLength()); }\n\tdouble normalize(){ double L = length(); if(L>0) scale(1.0/L); return L; }\n};\n\nint main(){\n\tint N;\n\twhile(cin >> N && N){\n\t\tP3 pos(0, 0, 0), dir;\n\t\tcin >> dir.x >> dir.y >> dir.z;\n\t\tdir.normalize();\n\t\tvector< pair<P3, double> > vp(N);\n\t\tfor(int i=0;i<N;i++){\n\t\t\tcin >> vp[i].first.x >> vp[i].first.y >> vp[i].first.z >> vp[i].second;\n\t\t}\n\t\twhile(true){\n\t\t\tbool end = true;\n\t\t\tdouble nearest = 1e12;\n\t\t\tP3 npos, ndir;\n\t\t\tfor(int i=0;i<N;i++){\n\t\t\t\tP3 p(vp[i].first - pos);\n\t\t\t\tdouble l = dir.dot(p);\n\t\t\t\tp = p - dir*l;\n\t\t\t\tdouble dist = p.length();\n\t\t\t\tif(dist > vp[i].second - 1e-8) continue;\n\t\t\t\tdouble sub = sqrt(vp[i].second*vp[i].second - dist*dist);\n\t\t\t\tif(l-sub > 0 && nearest > l-sub){\n\t\t\t\t\tnearest = l-sub;\n\t\t\t\t\tnpos = pos + dir*(l-sub);\n\t\t\t\t\tP3 ref(npos-vp[i].first);\n\t\t\t\t\tref.normalize();\n\t\t\t\t\tndir = dir - ref*dir.dot(ref)*2.0;\n\t\t\t\t\tend = false;\n\t\t\t\t}\n\t\t\t}\n\t\t\tif(end) break;\n\t\t\tpos = npos;\n\t\t\tdir = ndir;\n\t\t}\n\t\tprintf(\"%.8lf %.8lf %.8lf\\n\", pos.x, pos.y, pos.z);\n\t}\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 1256, "score_of_the_acc": -0.0067, "final_rank": 4 }, { "submission_id": "aoj_1289_1133839", "code_snippet": "#include <iostream>\n#include <vector>\n#include <cmath>\n#include <cstdio>\n\nusing namespace std;\n\nclass P3 {\npublic:\n\tdouble x, y, z;\n\tP3(double x=0, double y=0, double z=0) : x(x), y(y), z(z) {}\n\tP3(const P3& p) : x(p.x), y(p.y), z(p.z) {}\n\tvoid set(const P3& p){ x = p.x, y = p.y, z = p.z; }\n\tvoid set(double px, double py, double pz){ x = px, y = py, z = pz; }\n\tP3 operator + (const P3 &p) const { return P3(x+p.x, y+p.y, z+p.z); }\n\tP3 operator - (const P3 &p) const { return P3(x-p.x, y-p.y, z-p.z); }\n\tP3 operator * (double p) const { return P3(x*p, y*p, z*p); }\n\tvoid add(const P3& p){ x += p.x, y += p.y, z += p.z; }\n\tvoid sub(const P3& p){ x -= p.x, y -= p.y, z -= p.z; }\n\tvoid scale(double s){ x *= s, y *= s, z *= s; }\n\tdouble dot(const P3& p) const { return x*p.x + y*p.y + z*p.z; }\n\tdouble squaredLength() const { return x*x + y*y + z*z; }\n\tdouble length() const { return sqrt(squaredLength()); }\n\tdouble normalize(){ double L = length(); if(L>0) scale(1.0/L); return L; }\n};\n\nint main(){\n\tint N;\n\twhile(cin >> N && N){\n\t\tP3 pos(0, 0, 0), dir;\n\t\tcin >> dir.x >> dir.y >> dir.z;\n\t\tdir.normalize();\n\t\tvector< pair<P3, double> > vp(N);\n\t\tfor(int i=0;i<N;i++){\n\t\t\tcin >> vp[i].first.x >> vp[i].first.y >> vp[i].first.z >> vp[i].second;\n\t\t}\n\t\twhile(true){\n\t\t\tbool end = true;\n\t\t\tdouble nearest = 1e12;\n\t\t\tP3 npos, ndir;\n\t\t\tfor(int i=0;i<N;i++){\n\t\t\t\tP3 p(vp[i].first);\n\t\t\t\tp.sub(pos);\n\t\t\t\tdouble l = dir.dot(p);\n\t\t\t\tp.sub(dir*l);\n\t\t\t\tdouble dist = p.length();\n\t\t\t\tif(dist > vp[i].second - 1e-8) continue;\n\t\t\t\tdouble sub = sqrt(vp[i].second*vp[i].second - dist*dist);\n\t\t\t\tif(l-sub > 0 && nearest > l-sub){\n\t\t\t\t\tnearest = l-sub;\n\t\t\t\t\tnpos.set(pos);\n\t\t\t\t\tnpos.add(dir*(l-sub));\n\t\t\t\t\tndir.set(dir);\n\t\t\t\t\tndir.scale(-1);\n\t\t\t\t\tP3 ref(npos-vp[i].first);\n\t\t\t\t\tref.normalize();\n\t\t\t\t\tP3 add(ndir);\n\t\t\t\t\tref.scale(ndir.dot(ref));\n\t\t\t\t\tadd.sub(ref);\n\t\t\t\t\tadd.scale(-2.0);\n\t\t\t\t\tndir.add(add);\n\t\t\t\t\tend = false;\n\t\t\t\t}\n\t\t\t}\n\t\t\tif(end) break;\n\t\t\tpos.set(npos);\n\t\t\tdir.set(ndir);\n\t\t}\n\t\tprintf(\"%.8lf %.8lf %.8lf\\n\", pos.x, pos.y, pos.z);\n\t}\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 1252, "score_of_the_acc": -0.005, "final_rank": 3 }, { "submission_id": "aoj_1289_1098679", "code_snippet": "#include <iostream>\n#include <iomanip>\n#include <vector>\n#include <cmath>\n\nusing namespace std;\nstatic const double EPS = 1e-8;\ninline int sign(double x){ return abs(x) < EPS ? 0 : (x > 0 ? 1 : -1); }\n\nstruct Point3D {\n\tdouble x, y, z;\n\tPoint3D() : x(0), y(0), z(0) { }\n\tPoint3D(double x, double y, double z) : x(x), y(y), z(z) { }\n\n\tPoint3D operator-() const { return Point3D(-x, -y, -z); }\n\tPoint3D operator+(const Point3D &p) const {\n\t\treturn Point3D(x + p.x, y + p.y, z + p.z);\n\t}\n\tPoint3D operator-(const Point3D &p) const {\n\t\treturn Point3D(x - p.x, y - p.y, z - p.z);\n\t}\n\tPoint3D operator*(const double s) const {\n\t\treturn Point3D(x * s, y * s, z * s);\n\t}\n\tPoint3D operator/(const double s) const {\n\t\treturn Point3D(x / s, y / s, z / s);\n\t}\n};\n\nstruct Line3D {\n\tPoint3D a, b;\n\tLine3D() : a(), b() { }\n\tLine3D(const Point3D &a, const Point3D &b) : a(a), b(b) { }\n};\n\nstruct Sphere {\n\tPoint3D c;\n\tdouble r;\n\tSphere() : c(), r(0) { }\n\tSphere(const Point3D &c, double r) : c(c), r(r) { }\n};\n\ninline double dot(const Point3D &a, const Point3D &b){\n\treturn a.x * b.x + a.y * b.y + a.z * b.z;\n}\ninline Point3D cross(const Point3D &a, const Point3D &b){\n\treturn Point3D(\n\t\ta.y * b.z - a.z * b.y,\n\t\ta.z * b.x - a.x * b.z,\n\t\ta.x * b.y - a.y * b.x);\n}\ninline double norm(const Point3D &p){\n\treturn p.x * p.x + p.y * p.y + p.z * p.z;\n}\ninline double abs(const Point3D &p){\n\treturn sqrt(norm(p));\n}\ninline Point3D unit(const Point3D &p){\n\treturn p / abs(p);\n}\ninline Point3D vec(const Line3D &l){\n\treturn l.b - l.a;\n}\n\ninline Point3D projection(const Line3D &l, const Point3D &p){\n\tconst Point3D v = vec(l);\n\treturn l.a + v * (dot(p - l.a, v) / norm(v));\n}\ninline Point3D reflection(const Line3D &l, const Point3D &p){\n\tconst Point3D q = projection(l, p);\n\treturn q + q - p;\n}\n\ninline bool intersectLP(const Line3D &l, const Point3D &p){\n\treturn !sign(norm(cross(p - l.a, vec(l))));\n}\ninline bool intersectSP(const Line3D &l, const Point3D &p){\n\tconst Point3D a = p - l.a, b = vec(l);\n\tif(norm(a) < EPS || norm(a - b) < EPS){ return true; }\n\tif(norm(cross(a, b)) > EPS){ return false; }\n\tif(sign(a.x * b.x) < 0){ return false; }\n\tif(sign(a.y * b.y) < 0){ return false; }\n\tif(sign(a.z * b.z) < 0){ return false; }\n\treturn norm(a) < norm(b);\n}\ninline double distanceLP(const Line3D &l, const Point3D &p){\n\tconst Point3D q = projection(l, p);\n\treturn abs(p - q);\n}\ninline double distanceSP(const Line3D &l, const Point3D &p){\n\tconst Point3D u = p - l.a;\n\tconst Point3D v = projection(Line3D(Point3D(), vec(l)), u);\n\tif(intersectSP(Line3D(Point3D(), vec(l)), v)){ return abs(u - v); }\n\treturn sqrt(min(norm(p - l.a), norm(p - l.b)));\n}\n\nvector<Point3D> crosspointCL(const Sphere &s, const Line3D &l){\n\tconst Point3D &p = projection(l, s.c);\n\tconst double d2 = norm(s.c - p);\n\tif(d2 > s.r * s.r + EPS){ return vector<Point3D>(); }\n\tif(d2 > s.r * s.r - EPS){ return vector<Point3D>(1, p); }\n\tconst double sc = sqrt(s.r * s.r - d2);\n\tvector<Point3D> ret;\n\tret.push_back(p + unit(vec(l)) * sc);\n\tret.push_back(p - unit(vec(l)) * sc);\n\treturn ret;\n}\nvector<Point3D> crosspointCS(const Sphere &s, const Line3D &l){\n\tconst vector<Point3D> ps = crosspointCL(s, l);\n\tvector<Point3D> ret;\n\tfor(int i = 0; i < ps.size(); ++i){\n\t\tif(intersectSP(l, ps[i])){ ret.push_back(ps[i]); }\n\t}\n\treturn ret;\n}\n\nbool intersectCL(const Sphere &s, const Line3D &l){\n\treturn distanceLP(l, s.c) < s.r + EPS;\n}\nbool intersectCS(const Sphere &s, const Line3D &l){\n\treturn !crosspointCS(s, l).empty();\n}\n\nint main(){\n\tios_base::sync_with_stdio(false);\n\tcout << setiosflags(ios::fixed) << setprecision(10);\n\tconst double length = 1000.0;\n\twhile(true){\n\t\tint n;\n\t\tcin >> n;\n\t\tif(n == 0){ break; }\n\t\tPoint3D init;\n\t\tcin >> init.x >> init.y >> init.z;\n\t\tvector<Sphere> spheres(n);\n\t\tfor(int i = 0; i < n; ++i){\n\t\t\tdouble x, y, z, r;\n\t\t\tcin >> x >> y >> z >> r;\n\t\t\tspheres[i] = Sphere(Point3D(x, y, z), r);\n\t\t}\n\t\tLine3D cur(Point3D(), unit(init) * length);\n\t\tPoint3D answer;\n\t\twhile(true){\n\t\t\tdouble best_length = length;\n\t\t\tLine3D next;\n\t\t\tfor(int i = 0; i < n; ++i){\n\t\t\t\tif(abs(abs(spheres[i].c - cur.a) - spheres[i].r) < EPS){\n\t\t\t\t\tcontinue;\n\t\t\t\t}\n\t\t\t\tconst vector<Point3D> ps = crosspointCS(spheres[i], cur);\n\t\t\t\tif(ps.empty()){ continue; }\n\t\t\t\tdouble l = length;\n\t\t\t\tint key = 0;\n\t\t\t\tfor(int j = 0; j < ps.size(); ++j){\n\t\t\t\t\tconst double t = abs(ps[j] - cur.a);\n\t\t\t\t\tif(t >= l){ continue; }\n\t\t\t\t\tl = t;\n\t\t\t\t\tkey = j;\n\t\t\t\t}\n\t\t\t\tif(l >= best_length){ continue; }\n\t\t\t\tbest_length = l;\n\t\t\t\tanswer = ps[key];\n\t\t\t\tconst Point3D r =\n\t\t\t\t\treflection(Line3D(spheres[i].c, answer), cur.a);\n\t\t\t\tnext = Line3D(answer, answer + unit(r - answer) * length);\n\t\t\t}\n\t\t\tif(best_length >= length){ break; }\n\t\t\tcur = next;\n\t\t}\n\t\tcout << answer.x << \" \" << answer.y << \" \" << answer.z << endl;\n\t}\n\treturn 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 1312, "score_of_the_acc": -0.0302, "final_rank": 14 }, { "submission_id": "aoj_1289_1017726", "code_snippet": "#include<iostream>\n#include<vector>\n#include<array>\n#include<cmath>\n\nusing namespace std;\n\ntypedef array<double,3> P3;\n\nP3 operator-(P3 p){\n return P3{-p[0],-p[1],-p[2]};\n}\n\nP3 operator+(P3 a,P3 b){\n return P3{a[0]+b[0],a[1]+b[1],a[2]+b[2]};\n}\n\nP3 operator-(P3 a,P3 b){\n return a+-b;\n}\n\nP3 operator-=(P3 &a,P3 b){\n return a=a-b;\n}\n \nP3 operator*(P3 p,double d){\n return P3{p[0]*d,p[1]*d,p[2]*d};\n}\n \nP3 operator/(P3 p,double d){\n return p*(1/d);\n}\n\ndouble dot(P3 a,P3 b){\n return a[0]*b[0]+a[1]*b[1]+a[2]*b[2];\n}\n\ndouble abs(P3 p){\n return sqrt(dot(p,p));\n}\n\nP3 cross(P3 a,P3 b){\n return P3{a[1]*b[2]-a[2]*b[1],a[2]*b[0]-a[0]*b[2],a[0]*b[1]-a[1]*b[0]};\n}\n \nP3 Rodrigues(P3 r,P3 base,double phy){\n P3 n=base/abs(base);\n P3 nnr=n*dot(n,r);\n return nnr+(r-nnr)*cos(phy)+cross(n,r)*sin(phy);\n}\n\nP3 closest_to_origin(P3 a,P3 b){\n P3 x=a-b;\n return b+x*-dot(b,x)/dot(x,x);\n}\n\nvector<P3> intersection(P3 p,double r,P3 a,P3 b){\n a-=p;\n b-=p;\n P3 cp=closest_to_origin(a,b);\n double d=abs(cp);\n if(d>r){\n return {};\n }else{\n P3 x=a-b;\n double f=sqrt(r*r-d*d)/abs(x);\n return {cp+x*f+p,cp+x*-f+p};\n }\n}\n\n\nint main(){\n for(int N;cin>>N,N;){\n double u,v,w;\n cin>>u>>v>>w;\n P3 p[100];\n int r[100];\n for(int i=0;i<N;i++){\n double x,y,z;\n cin>>x>>y>>z>>r[i];\n p[i]=P3{x,y,z};\n }\n P3 f{0,0,0},t{u,v,w};\n for(;;){\n double d=1e99;\n P3 nf,nt;\n for(int i=0;i<N;i++){\n\tif(dot(t-f,p[i]-f)>0){\n\t auto res=intersection(p[i],r[i],t,f);\n\t for(auto e:res){\n\t if(abs(e-f)<d){\n\t d=abs(e-f);\n\t nf=e;\n\t nt=Rodrigues(f-nf,nf-p[i],2*acos(0))+nf;\n\t }\n\t }\n\t}\n }\n if(d>1e98)break;\n f=nf;\n t=nt;\n }\n for(int i=0;i<3;i++){\n cout.precision(9);\n cout<<fixed<<f[i]<<\" \\n\"[i==2];\n }\n }\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 1296, "score_of_the_acc": -0.0235, "final_rank": 13 }, { "submission_id": "aoj_1289_1007240", "code_snippet": "#include <cstdio>\n#include <cmath>\n#include <cstring>\n#include <cstdlib>\n#include <climits>\n#include <ctime>\n#include <queue>\n#include <stack>\n#include <algorithm>\n#include <list>\n#include <vector>\n#include <set>\n#include <map>\n#include <iostream>\n#include <deque>\n#include <complex>\n#include <string>\n#include <iomanip>\n#include <sstream>\n#include <bitset>\n#include <valarray>\n#include <iterator>\nusing namespace std;\ntypedef long long int ll;\ntypedef unsigned int uint;\ntypedef unsigned char uchar;\ntypedef unsigned long long ull;\ntypedef pair<int, int> pii;\ntypedef vector<int> vi;\n\n#define REP(i,x) for(int i=0;i<(int)(x);i++)\n#define FOR(i,c) for(__typeof((c).begin())i=(c).begin();i!=(c).end();i++)\n#define RREP(i,x) for(int i=((int)(x)-1);i>=0;i--)\n#define RFOR(i,c) for(__typeof((c).rbegin())i=(c).rbegin();i!=(c).rend();i++)\n#define ALL(container) container.begin(), container.end()\n#define RALL(container) container.rbegin(), container.rend()\n#define SZ(container) ((int)container.size())\n#define mp(a,b) make_pair(a, b)\n#define UNIQUE(v) v.erase( unique(v.begin(), v.end()), v.end() );\n\ntemplate<class T> bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }\ntemplate<class T> bool chmin(T &a, const T &b) { if (a>b) { a=b; return 1; } return 0; }\ntemplate<class T> ostream& operator<<(ostream &os, const vector<T> &t) {\nos<<\"[\"; FOR(it,t) {if(it!=t.begin()) os<<\",\"; os<<*it;} os<<\"]\"; return os;\n}\ntemplate<class T> ostream& operator<<(ostream &os, const set<T> &t) {\nos<<\"{\"; FOR(it,t) {if(it!=t.begin()) os<<\",\"; os<<*it;} os<<\"}\"; return os;\n}\ntemplate<class S, class T> ostream& operator<<(ostream &os, const pair<S,T> &t) { return os<<\"(\"<<t.first<<\",\"<<t.second<<\")\";}\n\nconst int INF = 1<<28;\nconst double EPS = 1e-8;\nconst int MOD = 1000000007;\n\n\n#define EPS 1.0e-10\ninline int signum(double x) { return (abs(x) < EPS ? 0 : x > 0 ? 1 : -1); }\n\nstruct P{\n\tdouble x, y, z;\n\tP():x(0),y(0),z(0){}\n\tP(double a, double b, double c):x(a),y(b),z(c){}\n\t\n\tP operator+(const P &opp){\n\t\treturn P(x+opp.x, y+opp.y, z+opp.z);\n\t}\n\tP operator-(const P &opp){\n\t\treturn P(x-opp.x, y-opp.y, z-opp.z);\n\t}\n\tP operator/(const P &opp){\n\t\treturn P(abs(opp.x)<EPS?0:x/opp.x, abs(opp.y)<EPS?0:y/opp.y, abs(opp.z)<EPS?0:z/opp.z);\n\t}\n\tP operator/(const double &opp){\n\t\treturn P(x/opp, y/opp, z/opp);\n\t}\n\tP operator*(const double opp){\n\t\treturn P(x*opp, y*opp, z*opp);\n\t}\n\tfriend ostream& operator<<(ostream &os, const P &t) { return os<<\"(\"<<t.x<<\",\"<<t.y<<\",\"<<t.z<<\")\";}\n};\n\nstruct L{\n\tP pos, dir;\n\tL(){}\n\tL(P p, P d):pos(p),dir(d){}\n\tL(P d):pos(),dir(d){}\n};\n\nstruct C{\n\tP p;\n\tdouble r;\n\tC(){}\n\tC(P q, double s):p(q),r(s){}\n};\n\ninline double inp(P v1, P v2){\n\treturn v1.x*v2.x + v1.y*v2.y + v1.z*v2.z;\n}\n\ninline P outp(P p1, P p2){\n\treturn P(p1.y*p2.z - p1.z*p2.y, p1.z*p2.x - p1.x*p2.z, p1.x*p2.y - p1.y*p2.x);\n}\n\ninline double norm(P v){\n\treturn v.x*v.x + v.y*v.y + v.z*v.z;\n}\ninline double abs(P v){\n\treturn sqrt(norm(v));\n}\n\ninline P perf(P v1, L l){\n\treturn l.pos + l.dir*(inp(v1-l.pos, l.dir)/norm(l.dir));\n}\n\nbool lp_intersect(L l, P t){\t// 点が直線上にあるかどうか\n\treturn !signum(norm(outp(t-l.pos, l.dir)));\n}\n\nbool sp_intersect(L l, P t){\t// 点が線分上にあるかどうか\n\tP a(t-l.pos), b(l.dir);\n\tif(signum(norm(a)) == 0 || signum(norm(a-b)) == 0) return true;\t// 端点\n\tif(signum(norm(outp(a, b)))) return false;\t// 直線上にない\n\tif(signum(a.x*b.x) == -1 || signum(a.y*b.y) == -1 || signum(a.z*b.z) == -1) return false;\t// 逆方向\n\tif(norm(a) < norm(b)) return true;\n\treturn false;\t// イキスギィ!\n}\n\ndouble sp_distance(L l, P t){\n\tP v1 = t-l.pos;\n\tP v2 = perf(v1, L(l.dir));\n\tif(sp_intersect(L(l.dir), v2)) return abs(v1 - v2);\n\telse return sqrt(min(norm(t-l.pos), norm(t-(l.pos + l.dir))));\n}\n\ndouble sp_distance(P t, P p1, P p2){\n\treturn sp_distance(L(p1, p2-p1), t);\n}\n\nvector<P> cl_crosspoint(C c, L l){\n\tP p = perf(c.p, l);\n\tdouble d2 = norm(c.p-p);\n\tif(d2 > c.r*c.r + EPS) return vector<P>();\n\tif(d2 > c.r*c.r - EPS) return vector<P>(1, p);\t//境界\n\tdouble s = sqrt(c.r*c.r - d2);\n\tvector<P> ret;\n\tret.push_back(p + l.dir/abs(l.dir)*s);\n\tret.push_back(p - l.dir/abs(l.dir)*s);\n\treturn ret;\n}\n\nvector<P> cs_crosspoint(C c, L l){\n\tvector<P> ret, res = cl_crosspoint(c, l);\n\tREP(i, res.size()) if(sp_intersect(l, res[i])) ret.push_back(res[i]);\n\treturn ret;\n}\n\nbool cl_intersect(C c, L l){\n\treturn sp_distance(l, c.p) < c.r + EPS;\t// 境界を含む\n}\n\nbool cs_intersect(C c, L l){\n\treturn !cs_crosspoint(c, l).empty();\n}\n\nP reflect(P p, L l){\n\tP q = perf(p, l);\n\treturn q + (q - p);\n}\n\n\nint n;\n\nmain(){\n\tint i,j;\n\twhile(cin >> n, n){\n\t\tvector<C> c;\n\t\tdouble x, y, z, r;\n\t\tcin >> x >> y >> z;\n\t\tL s(P(x*100, y*100, z*100));\n\t\tREP(i, n){\n\t\t\tcin >> x >> y >> z >> r;\n\t\t\tc.push_back(C(P(x, y, z), r));\n\t\t}\n\t\tP ans(0, 0, 0);\n\t\twhile(1){\n\t\t\tpair<double, L> csp(1e10, L());\n//\t\t\tcout << \"s: \" << s.pos << \" -> \" << s.dir << endl;\n\t\t\tREP(i, n){\n\t\t\t\tvector<P> ret = cs_crosspoint(c[i], s);\n\t\t\t\tREP(j, ret.size()){\n\t\t\t\t\tif(norm(ret[j] - s.pos) > EPS && csp.first > norm(ret[j] - s.pos))\n\t\t\t\t\t\tcsp = pair<double, L>(norm(ret[j] - s.pos), L(ret[j], c[i].p-ret[j]));\n\t\t\t\t}\n\t\t\t}\n\t\t\tif(csp.first > 1e9) break;\n\t\t\tans = csp.second.pos;\n\t\t\tP ref = reflect(s.pos, csp.second);\n//\t\t\tcout << \"reflect: \" << ans << endl;\n//\t\t\tcout << \"d: \" << ref << endl;\n\t\t\ts = L(csp.second.pos, ref - csp.second.pos);\n\t\t\ts.dir = s.dir/abs(s.dir)*1000;\n\t\t}\n\t\tprintf(\"%.4f %.4f %.4f\\n\", ans.x, ans.y, ans.z);\n\t}\n\treturn 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 1264, "score_of_the_acc": -0.0101, "final_rank": 10 }, { "submission_id": "aoj_1289_978183", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\n#define dump(n) cout<<\"# \"<<#n<<'='<<(n)<<endl\n#define repi(i,a,b) for(int i=int(a);i<int(b);i++)\n#define peri(i,a,b) for(int i=int(b);i-->int(a);)\n#define rep(i,n) repi(i,0,n)\n#define per(i,n) peri(i,0,n)\n#define all(c) begin(c),end(c)\n#define mp make_pair\n#define mt make_tuple\n\ntypedef unsigned int uint;\ntypedef long long ll;\ntypedef unsigned long long ull;\ntypedef pair<int,int> pii;\ntypedef vector<int> vi;\ntypedef vector<vi> vvi;\ntypedef vector<ll> vl;\ntypedef vector<vl> vvl;\ntypedef vector<double> vd;\ntypedef vector<vd> vvd;\ntypedef vector<string> vs;\n\nconst int INF=1e9;\nconst int MOD=1e9+7;\nconst double EPS=1e-9;\n\ntemplate<typename T1,typename T2>\nostream& operator<<(ostream& os,const pair<T1,T2>& p){\n\treturn os<<'('<<p.first<<','<<p.second<<')';\n}\ntemplate<typename T>\nostream& operator<<(ostream& os,const vector<T>& a){\n\tos<<'[';\n\trep(i,a.size()) os<<(i?\" \":\"\")<<a[i];\n\treturn os<<']';\n}\n\nstruct Point{\n\tdouble x,y,z;\n\tPoint(){}\n\tPoint(double x,double y,double z):x(x),y(y),z(z){}\n\tPoint& operator+=(Point p){x+=p.x,y+=p.y,z+=p.z; return *this;}\n\tPoint& operator-=(Point p){x-=p.x,y-=p.y,z-=p.z; return *this;}\n\tPoint& operator*=(double c){x*=c,y*=c,z*=c; return *this;}\n\tPoint& operator/=(double c){x/=c,y/=c,z/=c; return *this;}\n};\nPoint operator+(Point a,Point b){return a+=b;}\nPoint operator-(Point a,Point b){return a-=b;}\nPoint operator*(Point a,double c){return a*=c;}\nPoint operator*(double c,Point a){return a*=c;}\nPoint operator/(Point a,double c){return a/=c;}\ndouble Dot(Point a,Point b){return a.x*b.x+a.y*b.y+a.z*b.z;}\ndouble Abs2(Point p){return Dot(p,p);}\ndouble Abs(Point p){return sqrt(Abs2(p));}\n\nstruct Line{\n\tPoint pos,dir;\n\tLine(){}\n\tLine(Point p,Point d):pos(p),dir(d){}\n};\n\nstruct Sphere{\n\tPoint center;\n\tdouble radius;\n\tSphere(){}\n\tSphere(Point c,double r):center(c),radius(r){}\n\tSphere(double x,double y,double z,double r):center(x,y,z),radius(r){}\n};\n\nPoint Proj(Line l,Point p){\n\tPoint a=p-l.pos,b=l.dir;\n\treturn l.pos+Dot(a,b)/Abs2(b)*b;\n}\ndouble DistLP(Line l,Point p){\n\treturn Abs(Proj(l,p)-p);\n}\n\npair<double,double> InterPoint(Sphere s,Line l){\n\tPoint p=l.pos-s.center,d=l.dir;\n\tdouble a=Abs2(d),b=Dot(p,d),c=Abs2(p)-pow(s.radius,2);\n\tdouble t1=(-b-sqrt(b*b-a*c))/a;\n\tdouble t2=(-b+sqrt(b*b-a*c))/a;\n\treturn mp(t1,t2);\n}\n\nostream& operator<<(ostream& os,const Point& p){\n\treturn os<<'('<<p.x<<','<<p.y<<','<<p.z<<')';\n}\n\nint main()\n{\n\tfor(int n;cin>>n && n;){\n\t\tPoint d0; cin>>d0.x>>d0.y>>d0.z;\n\t\tvector<Sphere> ss(n);\n\t\trep(i,n){\n\t\t\tdouble x,y,z,r; cin>>x>>y>>z>>r;\n\t\t\tss[i]=Sphere(x,y,z,r);\n\t\t}\n\t\t\n\t\tLine l(Point(0,0,0),d0);\n\t\tfor(;;){\n\t\t\tvd ts(n,INF);\n\t\t\trep(i,n) if(DistLP(l,ss[i].center)<ss[i].radius){\n\t\t\t\tdouble t1,t2; tie(t1,t2)=InterPoint(ss[i],l);\n\t\t\t\tts[i]=t1>EPS?t1:t2>EPS?t2:INF;\n\t\t\t}\n\t\t\t\n\t\t\tint i=min_element(all(ts))-begin(ts);\n\t\t\tif(ts[i]==INF){\n\t\t\t\tprintf(\"%.4f %.4f %.4f\\n\",l.pos.x,l.pos.y,l.pos.z);\n\t\t\t\tbreak;\n\t\t\t}\n\t\t\t\n\t\t\t// aから球面上の点bで跳ね返りcへ\n\t\t\tPoint a=l.pos,b=l.pos+l.dir*ts[i];\n\t\t\tPoint p=Proj(Line(ss[i].center,b-ss[i].center),a);\n\t\t\tPoint c=p+(p-a);\n\t\t\tl=Line(b,c-b);\n\t\t}\n\t}\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 1244, "score_of_the_acc": -0.0017, "final_rank": 2 }, { "submission_id": "aoj_1289_924998", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n \nconst double eps = 1e-8;\nconst double inf = 1e10;\n \nbool equals(double a, double b) {\n return abs(a-b) < eps;\n}\n \nstruct P {\n double x, y, z;\n P() : x(0), y(0), z(0) {}\n P(double x, double y, double z) : x(x), y(y), z(z) {}\n};\n \nP operator + (const P &a, const P &b) {\n return P(a.x+b.x, a.y+b.y, a.z+b.z);\n}\nP operator - (const P &a, const P &b) {\n return P(a.x-b.x, a.y-b.y, a.z-b.z);\n}\nP operator * (const P &p, const double &a) {\n return P(p.x*a, p.y*a, p.z*a);\n}\nP operator / (const P &p, const double &a) {\n return P(p.x/a, p.y/a, p.z/a);\n}\n \ndouble dot(P a, P b) {\n return a.x*b.x + a.y*b.y + a.z*b.z;\n}\n \ndouble norm(P a) {\n return dot(a,a);\n}\n \ndouble abs(P a) {\n return sqrt(norm(a));\n}\n \nP project(P s1, P s2, P p) {\n P base = s2 - s1;\n double t = dot(p - s1, base)/norm(base);\n return s1 + base*t;\n}\n \nP reflect(P s1, P s2, P p) {\n return p + (project(s1, s2, p) - p)*2.0;\n}\n \ndouble getDistanceLP(P s1, P s2, P p) {\n return abs(project(s1, s2, p) - p);\n}\n \nP getQ(P s1, P s2, P p, double r) {\n P m = project(s1, s2, p);\n P base = s2 - s1;\n double x = r*r - norm(m-p);\n if(equals(x, 0.0)) x = 0.0;\n else if(x < 0) return P(inf,inf,inf);\n return m - base * sqrt(x) / abs(base);\n}\n \nint main() {\n int N;\n P v;\n vector<P> p;\n vector<double> r;\n while(cin >> N && N) {\n p.resize(N);\n r.resize(N);\n cin >> v.x >> v.y >> v.z;\n for(int i = 0; i < N; ++i) {\n cin >> p[i].x >> p[i].y >> p[i].z >> r[i];\n }\n \n P s, t;\n t = v;\n while(1) {\n double mini = inf;\n int k = -1;\n for(int i = 0; i < N; ++i) {\n P q = getQ(s, t, p[i], r[i]);\n if(q.x == inf) continue;\n if(dot(t-s, q-s) < eps) continue;\n double dist = abs(q - s);\n if(mini > dist) {\n mini = dist;\n k = i;\n }\n }\n if(k == -1) break;\n P q = getQ(s, t, p[k], r[k]);\n t = reflect(p[k], q, s);\n s = q;\n }\n printf(\"%.4f %.4f %.4f\\n\", s.x, s.y, s.z);\n }\n return 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 1280, "score_of_the_acc": -0.0168, "final_rank": 12 }, { "submission_id": "aoj_1289_924853", "code_snippet": "#include<bits/stdc++.h>\n \n#define REP(i,s,n) for(int i=s;i<n;i++)\n#define rep(i,n) REP(i,0,n)\n#define IINF (INT_MAX)\n#define EPS (1e-7)\n#define equals(a,b) (fabs((a)-(b))<EPS)\n#define MAX 110\n \nusing namespace std;\n \n//テッツセツεッツスツ、テッツセツづッツスツステッツセツづヲツ?ソテッツスツヲテッツセツづヲツ個嘉ゥツ敖凖ッツスツ、テッツセツづッツスツクテッツセツづッツスツュ Verifyテッツセツεッツスツ」テッツセツ?テァツ卍づッツスツ」テッツセツ?テヲツキツ古ッツスツ」テッツセツづ」ツ?、テ」ツ?・テッツスツ」テッツセツ?テッツスツョテッツセツεッツスツ」テッツセツ?テッツスツォテッツセツεッツスツ」テッツセツ?テッツスツッVerifyテッツセツεッツスツ」テッツセツ?テッツスツィテッツセツεッツスツヲテッツセツづ・ツエツ「テッツスツクテッツセツεッツスツ」テッツセツ??テッツスツ」テッツセツ?テッツスツヲテッツセツεッツスツ」テッツセツ?テ」ツ?・テッツスツ」テッツセツづ」ツ?、?\n \n//Verify AOJ 0115\nclass Point3d{\npublic:\n double x,y,z;\n \n Point3d(double x=0,double y=0,double z=0):x(x),y(y),z(z){}\n \n Point3d operator + (const Point3d& a){\n return Point3d(x+a.x,y+a.y,z+a.z);\n }\n Point3d operator - (const Point3d& a){\n return Point3d(x-a.x,y-a.y,z-a.z);\n }\n Point3d operator * (const double& d){\n return Point3d(x*d,y*d,z*d);\n }\n Point3d operator / (const double& d){\n return Point3d(x/d,y/d,z/d);\n }\n \n bool operator < (const Point3d& p)const{\n return !equals(x,p.x)?x<p.x:((!equals(y,p.y))?y<p.y:z<p.z);\n }\n \n bool operator == (const Point3d& p)const{\n return equals(x,p.x) && equals(y,p.y) && equals(z,p.z);\n }\n \n};\n \n//Verify AOJ 0115\nstruct Segment3d{\n Point3d p[2];\n Segment3d(Point3d p1=Point3d(),Point3d p2=Point3d()){\n p[0] = p1, p[1] = p2;\n }\n bool operator == (const Segment3d& seg)const{\n return p[0] == seg.p[0] && p[1] == seg.p[1];\n }\n};\n \ntypedef Point3d Vector3d;\ntypedef Segment3d Line3d;\n \n \n \n \nostream& operator << (ostream& os,const Point3d& p){\n os << \"(\" << p.x << \",\" << p.y << \",\" << p.z << \")\";\n}\n \nostream& operator << (ostream& os,const Segment3d& seg){\n os << \"(\" << seg.p[0] << \",\" << seg.p[1] << \")\";\n}\n \n//Verify AOJ 0115\ndouble dot(const Point3d& a,const Point3d& b){\n return a.x*b.x + a.y*b.y + a.z*b.z;\n}\n \n//Verify AOJ 0115\nVector3d cross(const Point3d& a,const Point3d& b){\n return Vector3d(a.y*b.z-a.z*b.y,a.z*b.x-a.x*b.z,a.x*b.y-a.y*b.x);\n}\n \n//Verify AOJ 0115\ninline double norm(const Point3d &p){\n return p.x*p.x + p.y*p.y + p.z*p.z;\n}\n \n//Verify AOJ 0115\ninline double abs(const Point3d &p){\n return sqrt(norm(p));\n}\n \ninline double toRad(double theta){\n return theta * M_PI / 180.0;\n}\n \n \n \nbool on_line3d(Line3d line,Point3d p){\n return equals(abs(cross(p-line.p[0],line.p[1]-line.p[0])),0);\n}\n \nbool on_segment3d(Segment3d seg,Point3d p){\n if( !on_line3d(seg,p) ) return false;\n double dist[3] = { abs(seg.p[1]-seg.p[0]), abs(p-seg.p[0]), abs(p-seg.p[1]) }; \n return on_line3d(seg,p) && equals(dist[0],dist[1]+dist[2]);\n}\n \n//Verify AOJ 0115\nbool point_on_the_triangle3d(Point3d tri1,Point3d tri2,Point3d tri3,Point3d p){\n \n //テッツセツεッツスツァテッツセツづッツスツキテッツセツづ・ツ?凖ッツスツ・テッツセツづヲツ個?テッツスツ、テッツセツづッツスツクテッツセツづァツ板佚ッツスツ」テッツセツ?テッツスツォpテッツセツεッツスツ」テッツセツ?テ・ツ渉、テッツスツ」テッツセツ?テ」ツ?・テッツスツ」テッツセツ?テッツスツ」テッツセツεッツスツ」テッツセツ?テヲツキツ古ッツスツ・テッツセツ?テッツスツエテッツセツεッツスツ・テッツセツづゥツ敖津ヲツ堋療ッツスツ」テッツセツ??テッツスツ、テッツセツづッツスツクテッツセツづ・ツ伉嘉ッツスツィテッツセツづッツスツァテッツセツづヲツエツ・テッツスツ・テッツセツづッツスツステッツセツづッツスツ「テッツセツεッツスツ・テッツセツ??テッツスツ」テッツセツ?テッツスツィテッツセツεッツスツ」テッツセツ?テッツスツソテッツセツεッツスツ」テッツセツ?テッツスツェテッツセツεッツスツ」テッツセツ??\n //if( on_segment3d(Segment3d(tri1,tri2),p) ) return true;\n //if( on_segment3d(Segment3d(tri2,tri3),p) ) return true;\n //if( on_segment3d(Segment3d(tri3,tri1),p) ) return true;\n \n Vector3d v1 = tri2 - tri1;\n Vector3d v2 = tri3 - tri2;\n Vector3d v3 = tri1 - tri3;\n \n Vector3d cp[3] = { cross(v1,p-tri1), cross(v2,p-tri2), cross(v3,p-tri3) };\n double d1 = dot(cp[0],cp[1]);\n double d2 = dot(cp[0],cp[2]);\n \n // テッツセツεッツスツァテッツセツづッツスツキテッツセツづ・ツ?凖ッツスツ・テッツセツづヲツ個?テッツスツ、テッツセツづッツスツクテッツセツづァツ板佚ッツスツ」テッツセツ?テッツスツォpテッツセツεッツスツ」テッツセツ?テ・ツ渉、テッツスツ」テッツセツ?テ」ツ?・テッツスツ」テッツセツ?テッツスツ」テッツセツεッツスツ」テッツセツ?テヲツキツ古ッツスツ・テッツセツ?テッツスツエテッツセツεッツスツ・テッツセツづゥツ敖?テッツセツεッツスツ、テッツセツづッツスツクテッツセツづ・ツ伉嘉ッツスツィテッツセツづッツスツァテッツセツづヲツエツ・テッツスツ・テッツセツづッツスツステッツセツづッツスツ「テッツセツεッツスツ・テッツセツ??テッツスツ」テッツセツ?テッツスツィテッツセツεッツスツ」テッツセツ?テッツスツッテッツセツεッツスツ」テッツセツ?テッツスツソテッツセツεッツスツ」テッツセツ?テッツスツェテッツセツεッツスツ」テッツセツ?テゥツ卍崚ッツスツ」テッツセツ?テッツスツェテッツセツεッツスツ」テッツセツ??\n //if( ( !equals(d1,0.0) && d1 > 0 ) && ( !equals(d2,0.0) && d2 > 0 ) ) return true;\n \n // テッツセツεッツスツァテッツセツづッツスツキテッツセツづ・ツ?凖ッツスツ・テッツセツづヲツ個?テッツスツ、テッツセツづッツスツクテッツセツづァツ板佚ッツスツ」テッツセツ?テッツスツォpテッツセツεッツスツ」テッツセツ?テ・ツ渉、テッツスツ」テッツセツ?テ」ツ?・テッツスツ」テッツセツ?テッツスツ」テッツセツεッツスツ」テッツセツ?テヲツキツ古ッツスツ・テッツセツ?テッツスツエテッツセツεッツスツ・テッツセツづゥツ敖?テッツセツεッツスツ、テッツセツづッツスツクテッツセツづ・ツ伉嘉ッツスツィテッツセツづッツスツァテッツセツづヲツエツ・テッツスツ・テッツセツづッツスツステッツセツづッツスツ「テッツセツεッツスツ・テッツセツ??テッツスツ」テッツセツ?テッツスツィテッツセツεッツスツ」テッツセツ?テッツスツソテッツセツεッツスツ」テッツセツ?テッツスツェテッツセツεッツスツ」テッツセツ??\n if( ( equals(d1,0.0) || d1 > 0 ) && ( equals(d2,0.0) || d2 > 0 ) ) return true;\n return false;\n}\n \ninline Point3d rotateX(Point3d p,double rad){\n return Point3d(p.x,p.y*cos(rad)-p.z*sin(rad),p.y*sin(rad)+p.z*cos(rad));\n}\n \ninline Point3d rorateY(Point3d p,double rad){\n return Point3d(p.x*cos(rad)+p.z*sin(rad),p.y,-p.x*sin(rad)+p.z*cos(rad));\n}\n \ninline Point3d rorateZ(Point3d p,double rad){\n return Point3d(p.x*cos(rad)-p.y*sin(rad),p.x*sin(rad)+p.y*cos(rad),p.z);\n}\n \ninline Point3d rotateEuler(Point3d p,double alpha,double beta,double gamma){\n return Point3d( (cos(alpha)*cos(beta)*cos(gamma)-sin(alpha)*sin(gamma)) * p.x + (-cos(alpha)*cos(beta)*sin(gamma)-sin(alpha)*cos(gamma)) * p.y + (cos(alpha)*sin(beta)) * p.z,\n (sin(alpha)*cos(beta)*cos(gamma)+cos(alpha)*sin(gamma)) * p.x + (-sin(alpha)*cos(beta)*sin(gamma)+cos(alpha)*cos(gamma)) * p.y + (sin(alpha)*sin(beta)) * p.z,\n (-sin(beta)*cos(gamma)) * p.x + (sin(beta)*sin(gamma)) * p.y + (cos(beta)) * p.z);\n}\n \ninline Point3d rotateRollPitchYaw(Point3d p,double roll,double pitch,double yaw){\n return Point3d( ( cos(roll) * cos(pitch) ) * p.x + ( cos(roll) * sin(pitch) * sin(yaw) - sin(roll) * cos(yaw) ) * p.y + ( cos(roll) * sin(pitch) * cos(yaw) + sin(roll) * sin(yaw) ) * p.z,\n ( sin(roll) * cos(pitch) ) * p.x + ( sin(roll) * sin(pitch) * sin(yaw) + cos(roll) * cos(yaw) ) * p.y + ( sin(roll) * sin(pitch) * cos(yaw) - cos(roll) * sin(yaw) ) * p.z,\n -sin(pitch) * p.x + cos(pitch) * sin(yaw) * p.y + cos(pitch) * cos(yaw) * p.z);\n}\n \n \nclass Plane3d{\npublic:\n Point3d normal_vector; //テッツセツεッツスツヲテッツセツづッツスツウテッツセツづゥツ卍崚ッツスツァテッツセツづッツスツキテッツセツづ・ツ?凖ッツスツ」テッツセツづ篠エテ・ツ?姪ッツスツ」テッツセツづ」ツ?、テッツスツッテッツセツεッツスツ」テッツセツづ篠エテヲツ堋療ッツスツ」テッツセツづ篠エテッツスツォ\n double d; // テッツセツεッツスツ・テッツセツづッツスツケテッツセツづッツスツウテッツセツεッツスツゥテッツセツづヲツ閉姪ッツスツ「テッツセツεッツスツヲテッツセツづゥツウツエテッツスツケテッツセツεッツスツァテッツセツづッツスツィテッツセツづ・ツ?敕ッツスツ・テッツセツづッツスツシテッツセツ?normal_vector.x * x + normal_vector.y * y + normal_vector.z * z + d = 0\n \n Plane3d(Point3d normal_vector=Point3d(),double d=0):normal_vector(normal_vector),d(d){}\n Plane3d(Vector3d a,Vector3d b,Vector3d c){\n Vector3d v1 = b - a;\n Vector3d v2 = c - a;\n Vector3d tmp = cross(v1,v2);\n normal_vector = tmp / abs(tmp);\n set_d(a);\n }\n \n //Verify AOJ 0115\n //テッツセツεッツスツヲテッツセツづッツスツウテッツセツづゥツ卍崚ッツスツァテッツセツづッツスツキテッツセツづ・ツ?凖ッツスツ」テッツセツづ篠エテ・ツ?姪ッツスツ」テッツセツづ」ツ?、テッツスツッテッツセツεッツスツ」テッツセツづ篠エテヲツ堋療ッツスツ」テッツセツづ篠エテッツスツォnormal_vectorテッツセツεッツスツ」テッツセツ?テッツスツィテッツセツεッツスツ・テッツセツづッツスツケテッツセツづッツスツウテッツセツεッツスツゥテッツセツづヲツ閉姪ッツスツ「テッツセツεッツスツ、テッツセツづッツスツクテッツセツづァツ板佚ッツスツ」テッツセツ?テッツスツョテッツセツεッツスツッテッツセツづッツスツシテッツセツづ・ツヲツ・テッツスツァテッツセツづ」ツ?、テッツスツケテッツセツεッツスツ」テッツセツ?テ・ツ?敕ッツスツ」テッツセツづ」ツ?、テ・ツャツーテッツセツεッツスツ」テッツセツづ」ツ?、テヲツエツ・テッツスツィテッツセツづッツスツィテッツセツづヲツ堋療ッツスツァテッツセツづッツスツョテッツセツづァツ卍づッツスツ」テッツセツ?テ・ツ?姪ッツスツ」テッツセツづ」ツ?、?\n void set_d(Point3d p){\n d = dot(normal_vector,p);\n }\n \n //テッツセツεッツスツ・テッツセツづッツスツケテッツセツづッツスツウテッツセツεッツスツゥテッツセツづヲツ閉姪ッツスツ「テッツセツεッツスツ」テッツセツ?テッツスツィテッツセツεッツスツァテッツセツづ」ツ?、テッツスツケpテッツセツεッツスツ」テッツセツ?テッツスツョテッツセツεッツスツィテッツセツづッツスツキテッツセツづヲツ閉陛ッツスツゥテッツセツづ・ツエツ「テッツスツ「テッツセツεッツスツ」テッツセツづ」ツ?、テヲツエツ・テッツスツヲテッツセツづッツスツアテッツセツづ」ツ?・テッツスツ」テッツセツづ」ツ?、?テッツスツ」テッツセツづ」ツ?、?\n double distanceP(Point3d p){\n Point3d a = normal_vector * d;//テッツセツεッツスツ・テッツセツづッツスツケテッツセツづッツスツウテッツセツεッツスツゥテッツセツづヲツ閉姪ッツスツ「テッツセツεッツスツ、テッツセツづッツスツクテッツセツづァツ板佚ッツスツ」テッツセツ?テッツスツョテッツセツεッツスツゥテッツセツ?テッツスツゥテッツセツεッツスツ・テッツセツづッツスツステッツセツづァツヲツソテッツスツ」テッツセツ?テッツスツェテッツセツεッツスツァテッツセツづ」ツ?、テッツスツケテッツセツεッツスツ」テッツセツづ」ツ?、テヲツエツ・テッツスツ」テッツセツ?テッツスツ、テッツセツεッツスツ」テッツセツ?テヲツケツ佚ッツスツ」テッツセツづ」ツ?、?\n return abs( dot(p-a,normal_vector) );\n }\n \n //テッツセツεッツスツ・テッツセツづッツスツケテッツセツづッツスツウテッツセツεッツスツゥテッツセツづヲツ閉姪ッツスツ「テッツセツεッツスツ、テッツセツづッツスツクテッツセツづァツ板佚ッツスツ」テッツセツ?テッツスツァテッツセツεッツスツ」テッツセツづ」ツ?、テ」ツ?・テッツスツ」テッツセツ?テッツスツ」テッツセツεッツスツ」テッツセツ?テッツスツィテッツセツεッツスツ」テッツセツづ」ツ?、テ」ツ?・テッツスツァテッツセツづ」ツ?、テッツスツケpテッツセツεッツスツ」テッツセツ?テッツスツィテッツセツεッツスツィテッツセツづッツスツソテッツセツづ・ツヲツ・テッツスツ」テッツセツ??テッツスツァテッツセツづ」ツ?、テッツスツケテッツセツεッツスツ」テッツセツづ」ツ?、テヲツエツ・テッツスツヲテッツセツづッツスツアテッツセツづ」ツ?・テッツスツ」テッツセツづ」ツ?、?テッツスツ」テッツセツづ」ツ?、?\n Point3d nearest_point(Point3d p){\n Point3d a = normal_vector * d;\n return p - ( normal_vector * dot(p-a,normal_vector) );\n }\n \n //Verify AOJ 0115\n //テッツセツεッツスツ・テッツセツづッツスツケテッツセツづッツスツウテッツセツεッツスツゥテッツセツづヲツ閉姪ッツスツ「テッツセツεッツスツ」テッツセツ?テッツスツィテッツセツεッツスツァテッツセツづッツスツキテッツセツづ・ツ?凖ッツスツ・テッツセツづヲツ個?テッツスツ」テッツセツ?テ・ツ渉、テッツスツ、テッツセツづッツスツコテッツセツづッツスツ、テッツセツεッツスツ・テッツセツづッツスツキテッツセツづッツスツョテッツセツεッツスツ」テッツセツ?テ・ツ?姪ッツスツ」テッツセツづ」ツ?、テ・ツ?敕ッツスツ」テッツセツ??\n bool intersectS(Segment3d seg){\n Point3d a = normal_vector * d;\n double res1 = dot(a-seg.p[0],normal_vector);\n double res2 = dot(a-seg.p[1],normal_vector);\n if( res1 > res2 ) swap(res1,res2);\n //cout << res1 << \" < \" << res2 << endl;\n if( ( equals(res1,0.0) || res1 < 0 ) && ( equals(res2,0.0) || res2 > 0 ) ) return true;\n return false;\n }\n \n //Verify AOJ 0115\n //テッツセツεッツスツ・テッツセツづッツスツケテッツセツづッツスツウテッツセツεッツスツゥテッツセツづヲツ閉姪ッツスツ「テッツセツεッツスツ」テッツセツ?テッツスツィテッツセツεッツスツァテッツセツづッツスツキテッツセツづ・ツ?凖ッツスツ・テッツセツづヲツ個?テッツスツ」テッツセツ?テッツスツョテッツセツεッツスツ、テッツセツづッツスツコテッツセツづッツスツ、テッツセツεッツスツァテッツセツづ」ツ?、テッツスツケテッツセツεッツスツ」テッツセツづ」ツ?、テヲツエツ・テッツスツヲテッツセツづッツスツアテッツセツづ」ツ?・テッツスツ」テッツセツづ」ツ?、?テッツスツ」テッツセツづ」ツ?、?\n Point3d crosspointS(Segment3d seg){\n Point3d a = normal_vector * d;\n double dot_p0a = fabs(dot( seg.p[0]-a,normal_vector ));\n double dot_p1a = fabs(dot( seg.p[1]-a,normal_vector ));\n if( equals(dot_p0a+dot_p1a,0) ) return seg.p[0];\n return seg.p[0] + ( seg.p[1] - seg.p[0] ) * ( dot_p0a / ( dot_p0a + dot_p1a ) );\n }\n \n};\n \n// Verify AOJ 1289\ndouble distanceLP(Line3d line,Point3d p){\n return abs(cross(line.p[1]-line.p[0],p-line.p[0])) / abs(line.p[1]-line.p[0]);\n}\n \n// Verify AOJ 1289\ndouble distanceSP(Segment3d seg,Point3d p){\n double res = dot(seg.p[1]-seg.p[0],p-seg.p[0]);\n if( !equals(res,0.0) && res < 0.0 ) return abs( p - seg.p[0] );\n res = dot(seg.p[0]-seg.p[1],p-seg.p[1]);\n if( !equals(res,0.0) && res < 0.0 ) return abs( p - seg.p[1] );\n return distanceLP(seg,p);\n}\n \n// Verify AOJ 1289\nPoint3d project(Segment3d seg,Point3d p){\n Vector3d base = seg.p[1] - seg.p[0];\n double t = dot(p-seg.p[0],base) / norm(base);\n return seg.p[0] + base * t;\n}\n \n// Verify AOJ 1289\nPoint3d reflect(Segment3d seg,Point3d p){\n return p + (project(seg,p)-p) * 2.0;\n}\n \n//Verify AOJ 2081\nbool isParallel(Line3d l1,Line3d l2){\n Vector3d A = l1.p[0], B = l1.p[1], C = l2.p[0], D = l2.p[1];\n Vector3d AB = B - A, CD = D - C;\n Vector3d n1 = AB / abs(AB), n2 = CD / abs(CD);\n double tmp = dot(n1,n2);\n tmp = 1 - tmp*tmp;\n return equals(tmp,0.0);\n}\n \n//Verify AOJ 2081 \n//l1,l2テッツセツεッツスツ」テッツセツ?テ・ツ渉、テッツスツ・テッツセツづッツスツケテッツセツづッツスツウテッツセツεッツスツィテッツセツづッツスツ。テッツセツづ・ツ渉、テッツスツ」テッツセツ?テッツスツェテッツセツεッツスツヲテッツセツづ・ツ債催」ツ?・テッツスツ」テッツセツ?テッツスツォテッツセツεッツスツ」テッツセツ?テッツスツッテッツセツεッツスツ、テッツセツづッツスツステッツセツづッツスツソテッツセツεッツスツァテッツセツづヲツ敖ソテッツスツィテッツセツεッツスツ」テッツセツ?テッツスツァテッツセツεッツスツ」テッツセツ?テ・ツつャテッツスツ」テッツセツ?テッツスツェテッツセツεッツスツ」テッツセツ??テッツスツ」テッツセツ?テッツスツョテッツセツεッツスツ」テッツセツ?テッツスツァテッツセツεッツスツヲテッツセツづッツスツウテッツセツづッツスツィテッツセツεッツスツヲテッツセツ??\nSegment3d nearest_segmentLL(Line3d l1,Line3d l2){\n assert(!isParallel(l1,l2)); // テッツセツεッツスツ・テッツセツづッツスツケテッツセツづッツスツウテッツセツεッツスツィテッツセツづッツスツ。テッツセツづ・ツ渉、テッツスツ」テッツセツ?テッツスツェテッツセツεッツスツ・テッツセツ?テッツスツエテッツセツεッツスツ・テッツセツづゥツ敖津ヲツ堋療ッツスツ」テッツセツ?テッツスツッテッツセツεッツスツ、テッツセツづッツスツステッツセツづッツスツソテッツセツεッツスツァテッツセツづヲツ敖ソテッツスツィテッツセツεッツスツ、テッツセツづッツスツクテッツセツづ・ツつャテッツスツ・テッツセツづヲツクツ嘉ッツスツッ\n // l1.p[0] = A, l1.p[1] = B, l2.p[0] = C, l2.p[1] = D\n Vector3d AB = l1.p[1] - l1.p[0];\n Vector3d CD = l2.p[1] - l2.p[0];\n Vector3d AC = l2.p[0] - l1.p[0];\n Vector3d n1 = AB / abs(AB), n2 = CD / abs(CD);\n double d1 = (dot(n1,AC)-dot(n1,n2)*dot(n2,AC)) / (1.0-pow(dot(n1,n2),2));\n double d2 = (dot(n1,n2)*dot(n1,AC)-dot(n2,AC)) / (1.0-pow(dot(n1,n2),2));\n return Segment3d(l1.p[0]+n1*d1,l2.p[0]+n2*d2);\n}\n \n//Verify AOJ 2081\nbool intersectLL(Line3d l1,Line3d l2){\n Vector3d A = l1.p[0], B = l1.p[1], C = l2.p[0], D = l2.p[1];\n \n //テッツセツεッツスツ」テッツセツ?テヲツ閉陛ッツスツ」テッツセツづ」ツ?、テ」ツ?・テッツスツ」テッツセツ?テヲツ閉陛ッツスツ」テッツセツづ」ツ?、テッツシツュ1,l2テッツセツεッツスツ」テッツセツ?テ・ツ渉、テッツスツァテッツセツづ・ツエツ「テッツスツエテッツセツεッツスツァテッツセツづッツスツキテッツセツづ・ツ?凖ッツスツ」テッツセツ?テ、ツスツ堙ッツスツ」テッツセツづ」ツ?、テ篠オテッツスツ」テッツセツ?テッツスツェテッツセツεッツスツ」テッツセツ??\n if( equals(abs(B-A),0.0) || equals(abs(D-C),0.0) ){\n /*\n テッツセツεッツスツ」テッツセツ?テァツヲツソテッツスツ」テッツセツ?テッツスツョテッツセツεッツスツ・テッツセツ?テッツスツエテッツセツεッツスツ・テッツセツづゥツ敖津ヲツ堋療ッツスツ」テッツセツ?テッツスツッテッツセツεッツスツヲテッツセツづッツスツウテッツセツづッツスツィテッツセツεッツスツヲテッツセツ??\n テッツセツεッツスツ」テッツセツ?テヲツ閉陛ッツスツ」テッツセツづ」ツ?、テ」ツ?・テッツスツ」テッツセツ?テヲツ閉陛ッツスツ」テッツセツづ」ツ?、テ」ツ?・テッツスツ、テッツセツづッツスツクテッツセツづ・ツ?イテッツスツ」テッツセツ?テヲツ堋療ッツスツ」テッツセツづ」ツ?、テ・ツ伉嘉ッツスツ」テッツセツづ」ツ?、テ・ツ渉、テッツスツ」テッツセツ?テヲツキツ古ッツスツァテッツセツづッツスツキテッツセツづ・ツ?凖ッツスツ・テッツセツづヲツ個?テッツスツ」テッツセツ?テ・ツ渉、テッツスツァテッツセツづッツスツキテッツセツづ・ツ?凖ッツスツ・テッツセツづヲツ個?テッツスツ」テッツセツ?テッツスツォテッツセツεッツスツ」テッツセツ?テッツスツェテッツセツεッツスツ」テッツセツ?テッツスツ」テッツセツεッツスツ」テッツセツ?テッツスツヲテッツセツεッツスツ」テッツセツ??テッツスツ」テッツセツ?テッツスツェテッツセツεッツスツ」テッツセツ??テッツスツ」テッツセツ?テッツスツョテッツセツεッツスツ」テッツセツ?テッツスツァテッツセツεッツスツ」テッツセツ??テッツスツ、テッツセツづッツスツコテッツセツづッツスツ、テッツセツεッツスツ・テッツセツづッツスツキテッツセツづッツスツョテッツセツεッツスツ」テッツセツ?テ・ツ?姪ッツスツ」テッツセツづ」ツ?、テ・ツ?敕ッツスツ」テッツセツ?テ・ツ?敕ッツスツ」テッツセツ?テッツスツゥテッツセツεッツスツ」テッツセツ??テッツスツ」テッツセツ?テ・ツ?敕ッツスツ」テッツセツ?テッツスツッテッツセツεッツスツ・テッツセツづヲツ個嘉ッツスツ、テッツセツεッツスツ・テッツセツづッツスツョテッツセツづ・ツ?凖ッツスツ」テッツセツ?テッツスツァテッツセツεッツスツ」テッツセツ?テ・ツつャテッツスツ」テッツセツ?テッツスツェテッツセツεッツスツ」テッツセツ??\n */\n return false;\n }\n \n Vector3d AB = B - A, CD = D - C;\n Vector3d n1 = AB / abs(AB), n2 = CD / abs(CD);\n double tmp = dot(n1,n2);\n tmp = 1 - tmp*tmp;\n \n if( equals(tmp,0.0) ) return 0; // テッツセツεッツスツァテッツセツづ・ツエツ「テッツスツエテッツセツεッツスツァテッツセツづッツスツキテッツセツづ・ツ?凖ッツスツ」テッツセツ?テ・ツ渉、テッツスツ・テッツセツづッツスツケテッツセツづッツスツウテッツセツεッツスツィテッツセツづッツスツ。テッツセツ?\n \n Segment3d ns = nearest_segmentLL(l1,l2);\n if( ns.p[0] == ns.p[1] ) return true;\n return false;\n}\n \nbool intersectSS(Segment3d seg1,Segment3d seg2){\n if( isParallel(seg1,seg2) ) return false;\n Segment3d seg = nearest_segmentLL(seg1,seg2);\n if( !( seg.p[0] == seg.p[1] ) ) return false;\n Point3d cp = seg.p[1];\n return on_segment3d(seg1,cp) && on_segment3d(seg2,cp);\n}\n \n \nint N;\nSegment3d ururu_beam;\nPoint3d Circle[MAX];\ndouble R[MAX];\n \ninline void Input3d(Point3d &p){ cin >> p.x >> p.y >> p.z; }\n \ninline void compute(){\n int skip = -1;\n bool update = true;\n while(update){\n update = false;\n Point3d tmp_p0,tmp_p1;\n int tmp_skip = -1;\n double tmp_dist = IINF;\n Vector3d vect = ( ururu_beam.p[1] - ururu_beam.p[0] ) / abs( ururu_beam.p[1] - ururu_beam.p[0] );\n Vector3d rvect = ( ururu_beam.p[0] - ururu_beam.p[1] ) / abs( ururu_beam.p[0] - ururu_beam.p[1] );\n ururu_beam.p[1] = ururu_beam.p[0] + vect * 500;\n rep(i,N){\n if( i == skip ) continue;\n \n double dist = distanceSP(ururu_beam,Circle[i]);\n if( !equals(dist,R[i]) && dist > R[i] ) continue;\n \n Point3d on_seg = project(ururu_beam,Circle[i]);\n \n dist = sqrt(pow(R[i],2.0)-norm(on_seg-Circle[i]));\n \n Point3d cp = on_seg + rvect * dist;\n Line3d line = Line3d(Circle[i],cp);\n \n dist = abs(ururu_beam.p[0]-cp);\n if( tmp_dist > dist ){\n tmp_p0 = cp, tmp_p1 = reflect(line,ururu_beam.p[0]);\n tmp_dist = dist;\n tmp_skip = i;\n }\n update = true;\n }\n if( update ){\n ururu_beam = Segment3d(tmp_p0,tmp_p1);\n skip = tmp_skip;\n }\n }\n printf(\"%.4f %.4f %.4f\\n\",ururu_beam.p[0].x,ururu_beam.p[0].y,ururu_beam.p[0].z);\n}\n \nint main(){\n while( cin >> N, N ){\n ururu_beam.p[0] = Point3d(0,0,0);\n Input3d(ururu_beam.p[1]);\n rep(i,N){\n Input3d(Circle[i]);\n cin >> R[i];\n }\n compute();\n }\n return 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 1260, "score_of_the_acc": -0.0084, "final_rank": 6 }, { "submission_id": "aoj_1289_924852", "code_snippet": "#include<bits/stdc++.h>\n \n#define REP(i,s,n) for(int i=s;i<n;i++)\n#define rep(i,n) REP(i,0,n)\n#define IINF (INT_MAX)\n#define EPS (1e-7)\n#define equals(a,b) (fabs((a)-(b))<EPS)\n#define MAX 110\n \nusing namespace std;\n \n//テ、ツスツ愿ヲツ按静、ツクツュ Verifyテ」ツ?療」ツ?淌」ツつづ」ツ?ョテ」ツ?ォテ」ツ?ッVerifyテ」ツ?ィテヲツ崢クテ」ツ??」ツ?ヲテ」ツ?づ」ツつ?\n \n//Verify AOJ 0115\nclass Point3d{\npublic:\n double x,y,z;\n \n Point3d(double x=0,double y=0,double z=0):x(x),y(y),z(z){}\n \n Point3d operator + (const Point3d& a){\n return Point3d(x+a.x,y+a.y,z+a.z);\n }\n Point3d operator - (const Point3d& a){\n return Point3d(x-a.x,y-a.y,z-a.z);\n }\n Point3d operator * (const double& d){\n return Point3d(x*d,y*d,z*d);\n }\n Point3d operator / (const double& d){\n return Point3d(x/d,y/d,z/d);\n }\n \n bool operator < (const Point3d& p)const{\n return !equals(x,p.x)?x<p.x:((!equals(y,p.y))?y<p.y:z<p.z);\n }\n \n bool operator == (const Point3d& p)const{\n return equals(x,p.x) && equals(y,p.y) && equals(z,p.z);\n }\n \n};\n \n//Verify AOJ 0115\nstruct Segment3d{\n Point3d p[2];\n Segment3d(Point3d p1=Point3d(),Point3d p2=Point3d()){\n p[0] = p1, p[1] = p2;\n }\n bool operator == (const Segment3d& seg)const{\n return p[0] == seg.p[0] && p[1] == seg.p[1];\n }\n};\n \ntypedef Point3d Vector3d;\ntypedef Segment3d Line3d;\n \n \n \n \nostream& operator << (ostream& os,const Point3d& p){\n os << \"(\" << p.x << \",\" << p.y << \",\" << p.z << \")\";\n}\n \nostream& operator << (ostream& os,const Segment3d& seg){\n os << \"(\" << seg.p[0] << \",\" << seg.p[1] << \")\";\n}\n \n//Verify AOJ 0115\ndouble dot(const Point3d& a,const Point3d& b){\n return a.x*b.x + a.y*b.y + a.z*b.z;\n}\n \n//Verify AOJ 0115\nVector3d cross(const Point3d& a,const Point3d& b){\n return Vector3d(a.y*b.z-a.z*b.y,a.z*b.x-a.x*b.z,a.x*b.y-a.y*b.x);\n}\n \n//Verify AOJ 0115\ninline double norm(const Point3d &p){\n return p.x*p.x + p.y*p.y + p.z*p.z;\n}\n \n//Verify AOJ 0115\ninline double abs(const Point3d &p){\n return sqrt(norm(p));\n}\n \ninline double toRad(double theta){\n return theta * M_PI / 180.0;\n}\n \n \n \nbool on_line3d(Line3d line,Point3d p){\n return equals(abs(cross(p-line.p[0],line.p[1]-line.p[0])),0);\n}\n \nbool on_segment3d(Segment3d seg,Point3d p){\n if( !on_line3d(seg,p) ) return false;\n double dist[3] = { abs(seg.p[1]-seg.p[0]), abs(p-seg.p[0]), abs(p-seg.p[1]) }; \n return on_line3d(seg,p) && equals(dist[0],dist[1]+dist[2]);\n}\n \n//Verify AOJ 0115\nbool point_on_the_triangle3d(Point3d tri1,Point3d tri2,Point3d tri3,Point3d p){\n \n //テァツキツ堙・ツ按?、ツクツ甘」ツ?ォpテ」ツ?古」ツ?づ」ツ?」テ」ツ?淌・ツ?エテ・ツ青暗」ツ??、ツクツ嘉ィツァツ津・ツスツ「テ・ツ??」ツ?ィテ」ツ?ソテ」ツ?ェテ」ツ??\n //if( on_segment3d(Segment3d(tri1,tri2),p) ) return true;\n //if( on_segment3d(Segment3d(tri2,tri3),p) ) return true;\n //if( on_segment3d(Segment3d(tri3,tri1),p) ) return true;\n \n Vector3d v1 = tri2 - tri1;\n Vector3d v2 = tri3 - tri2;\n Vector3d v3 = tri1 - tri3;\n \n Vector3d cp[3] = { cross(v1,p-tri1), cross(v2,p-tri2), cross(v3,p-tri3) };\n double d1 = dot(cp[0],cp[1]);\n double d2 = dot(cp[0],cp[2]);\n \n // テァツキツ堙・ツ按?、ツクツ甘」ツ?ォpテ」ツ?古」ツ?づ」ツ?」テ」ツ?淌・ツ?エテ・ツ青?テ、ツクツ嘉ィツァツ津・ツスツ「テ・ツ??」ツ?ィテ」ツ?ッテ」ツ?ソテ」ツ?ェテ」ツ?陛」ツ?ェテ」ツ??\n //if( ( !equals(d1,0.0) && d1 > 0 ) && ( !equals(d2,0.0) && d2 > 0 ) ) return true;\n \n // テァツキツ堙・ツ按?、ツクツ甘」ツ?ォpテ」ツ?古」ツ?づ」ツ?」テ」ツ?淌・ツ?エテ・ツ青?テ、ツクツ嘉ィツァツ津・ツスツ「テ・ツ??」ツ?ィテ」ツ?ソテ」ツ?ェテ」ツ??\n if( ( equals(d1,0.0) || d1 > 0 ) && ( equals(d2,0.0) || d2 > 0 ) ) return true;\n return false;\n}\n \ninline Point3d rotateX(Point3d p,double rad){\n return Point3d(p.x,p.y*cos(rad)-p.z*sin(rad),p.y*sin(rad)+p.z*cos(rad));\n}\n \ninline Point3d rorateY(Point3d p,double rad){\n return Point3d(p.x*cos(rad)+p.z*sin(rad),p.y,-p.x*sin(rad)+p.z*cos(rad));\n}\n \ninline Point3d rorateZ(Point3d p,double rad){\n return Point3d(p.x*cos(rad)-p.y*sin(rad),p.x*sin(rad)+p.y*cos(rad),p.z);\n}\n \ninline Point3d rotateEuler(Point3d p,double alpha,double beta,double gamma){\n return Point3d( (cos(alpha)*cos(beta)*cos(gamma)-sin(alpha)*sin(gamma)) * p.x + (-cos(alpha)*cos(beta)*sin(gamma)-sin(alpha)*cos(gamma)) * p.y + (cos(alpha)*sin(beta)) * p.z,\n (sin(alpha)*cos(beta)*cos(gamma)+cos(alpha)*sin(gamma)) * p.x + (-sin(alpha)*cos(beta)*sin(gamma)+cos(alpha)*cos(gamma)) * p.y + (sin(alpha)*sin(beta)) * p.z,\n (-sin(beta)*cos(gamma)) * p.x + (sin(beta)*sin(gamma)) * p.y + (cos(beta)) * p.z);\n}\n \ninline Point3d rotateRollPitchYaw(Point3d p,double roll,double pitch,double yaw){\n return Point3d( ( cos(roll) * cos(pitch) ) * p.x + ( cos(roll) * sin(pitch) * sin(yaw) - sin(roll) * cos(yaw) ) * p.y + ( cos(roll) * sin(pitch) * cos(yaw) + sin(roll) * sin(yaw) ) * p.z,\n ( sin(roll) * cos(pitch) ) * p.x + ( sin(roll) * sin(pitch) * sin(yaw) + cos(roll) * cos(yaw) ) * p.y + ( sin(roll) * sin(pitch) * cos(yaw) - cos(roll) * sin(yaw) ) * p.z,\n -sin(pitch) * p.x + cos(pitch) * sin(yaw) * p.y + cos(pitch) * cos(yaw) * p.z);\n}\n \n \nclass Plane3d{\npublic:\n Point3d normal_vector; //テヲツウツ陛ァツキツ堙」ツδ凖」ツつッテ」ツδ暗」ツδォ\n double d; // テ・ツケツウテゥツ敖「テヲツ鳴ケテァツィツ凝・ツシツ?normal_vector.x * x + normal_vector.y * y + normal_vector.z * z + d = 0\n \n Plane3d(Point3d normal_vector=Point3d(),double d=0):normal_vector(normal_vector),d(d){}\n Plane3d(Vector3d a,Vector3d b,Vector3d c){\n Vector3d v1 = b - a;\n Vector3d v2 = c - a;\n Vector3d tmp = cross(v1,v2);\n normal_vector = tmp / abs(tmp);\n set_d(a);\n }\n \n //Verify AOJ 0115\n //テヲツウツ陛ァツキツ堙」ツδ凖」ツつッテ」ツδ暗」ツδォnormal_vectorテ」ツ?ィテ・ツケツウテゥツ敖「テ、ツクツ甘」ツ?ョテッツシツ妥ァツつケテ」ツ?凝」ツつ嬰テ」ツつ津ィツィツ暗ァツョツ療」ツ?凖」ツつ?\n void set_d(Point3d p){\n d = dot(normal_vector,p);\n }\n \n //テ・ツケツウテゥツ敖「テ」ツ?ィテァツつケpテ」ツ?ョティツキツ敕ゥツ崢「テ」ツつ津ヲツアツづ」ツつ?」ツつ?\n double distanceP(Point3d p){\n Point3d a = normal_vector * d;//テ・ツケツウテゥツ敖「テ、ツクツ甘」ツ?ョテゥツ?ゥテ・ツスツ禿」ツ?ェテァツつケテ」ツつ津」ツ?、テ」ツ?湘」ツつ?\n return abs( dot(p-a,normal_vector) );\n }\n \n //テ・ツケツウテゥツ敖「テ、ツクツ甘」ツ?ァテ」ツつづ」ツ?」テ」ツ?ィテ」ツつづァツつケpテ」ツ?ィティツソツ妥」ツ??ァツつケテ」ツつ津ヲツアツづ」ツつ?」ツつ?\n Point3d nearest_point(Point3d p){\n Point3d a = normal_vector * d;\n return p - ( normal_vector * dot(p-a,normal_vector) );\n }\n \n //Verify AOJ 0115\n //テ・ツケツウテゥツ敖「テ」ツ?ィテァツキツ堙・ツ按?」ツ?古、ツコツ、テ・ツキツョテ」ツ?凖」ツつ凝」ツ??\n bool intersectS(Segment3d seg){\n Point3d a = normal_vector * d;\n double res1 = dot(a-seg.p[0],normal_vector);\n double res2 = dot(a-seg.p[1],normal_vector);\n if( res1 > res2 ) swap(res1,res2);\n //cout << res1 << \" < \" << res2 << endl;\n if( ( equals(res1,0.0) || res1 < 0 ) && ( equals(res2,0.0) || res2 > 0 ) ) return true;\n return false;\n }\n \n //Verify AOJ 0115\n //テ・ツケツウテゥツ敖「テ」ツ?ィテァツキツ堙・ツ按?」ツ?ョテ、ツコツ、テァツつケテ」ツつ津ヲツアツづ」ツつ?」ツつ?\n Point3d crosspointS(Segment3d seg){\n Point3d a = normal_vector * d;\n double dot_p0a = fabs(dot( seg.p[0]-a,normal_vector ));\n double dot_p1a = fabs(dot( seg.p[1]-a,normal_vector ));\n if( equals(dot_p0a+dot_p1a,0) ) return seg.p[0];\n return seg.p[0] + ( seg.p[1] - seg.p[0] ) * ( dot_p0a / ( dot_p0a + dot_p1a ) );\n }\n \n};\n \n// Verify AOJ 1289\ndouble distanceLP(Line3d line,Point3d p){\n return abs(cross(line.p[1]-line.p[0],p-line.p[0])) / abs(line.p[1]-line.p[0]);\n}\n \n// Verify AOJ 1289\ndouble distanceSP(Segment3d seg,Point3d p){\n double res = dot(seg.p[1]-seg.p[0],p-seg.p[0]);\n if( !equals(res,0.0) && res < 0.0 ) return abs( p - seg.p[0] );\n res = dot(seg.p[0]-seg.p[1],p-seg.p[1]);\n if( !equals(res,0.0) && res < 0.0 ) return abs( p - seg.p[1] );\n return distanceLP(seg,p);\n}\n \n// Verify AOJ 1289\nPoint3d project(Segment3d seg,Point3d p){\n Vector3d base = seg.p[1] - seg.p[0];\n double t = dot(p-seg.p[0],base) / norm(base);\n return seg.p[0] + base * t;\n}\n \n// Verify AOJ 1289\nPoint3d reflect(Segment3d seg,Point3d p){\n return p + (project(seg,p)-p) * 2.0;\n}\n \n//Verify AOJ 2081\nbool isParallel(Line3d l1,Line3d l2){\n Vector3d A = l1.p[0], B = l1.p[1], C = l2.p[0], D = l2.p[1];\n Vector3d AB = B - A, CD = D - C;\n Vector3d n1 = AB / abs(AB), n2 = CD / abs(CD);\n double tmp = dot(n1,n2);\n tmp = 1 - tmp*tmp;\n return equals(tmp,0.0);\n}\n \n//Verify AOJ 2081 \n//l1,l2テ」ツ?古・ツケツウティツ。ツ古」ツ?ェテヲツ卍づ」ツ?ォテ」ツ?ッテ、ツスツソテァツ板ィテ」ツ?ァテ」ツ?催」ツ?ェテ」ツ??」ツ?ョテ」ツ?ァテヲツウツィテヲツ??\nSegment3d nearest_segmentLL(Line3d l1,Line3d l2){\n assert(!isParallel(l1,l2)); // テ・ツケツウティツ。ツ古」ツ?ェテ・ツ?エテ・ツ青暗」ツ?ッテ、ツスツソテァツ板ィテ、ツクツ催・ツ渉ッ\n // l1.p[0] = A, l1.p[1] = B, l2.p[0] = C, l2.p[1] = D\n Vector3d AB = l1.p[1] - l1.p[0];\n Vector3d CD = l2.p[1] - l2.p[0];\n Vector3d AC = l2.p[0] - l1.p[0];\n Vector3d n1 = AB / abs(AB), n2 = CD / abs(CD);\n double d1 = (dot(n1,AC)-dot(n1,n2)*dot(n2,AC)) / (1.0-pow(dot(n1,n2),2));\n double d2 = (dot(n1,n2)*dot(n1,AC)-dot(n2,AC)) / (1.0-pow(dot(n1,n2),2));\n return Segment3d(l1.p[0]+n1*d1,l2.p[0]+n2*d2);\n}\n \n//Verify AOJ 2081\nbool intersectLL(Line3d l1,Line3d l2){\n Vector3d A = l1.p[0], B = l1.p[1], C = l2.p[0], D = l2.p[1];\n \n //テ」ツ?敕」ツつづ」ツ?敕」ツつM1,l2テ」ツ?古ァツ崢エテァツキツ堙」ツ?佚」ツつε」ツ?ェテ」ツ??\n if( equals(abs(B-A),0.0) || equals(abs(D-C),0.0) ){\n /*\n テ」ツ?禿」ツ?ョテ・ツ?エテ・ツ青暗」ツ?ッテヲツウツィテヲツ??\n テ」ツ?敕」ツつづ」ツ?敕」ツつづ、ツクツ偲」ツ?暗」ツつ嘉」ツつ古」ツ?淌ァツキツ堙・ツ按?」ツ?古ァツキツ堙・ツ按?」ツ?ォテ」ツ?ェテ」ツ?」テ」ツ?ヲテ」ツ??」ツ?ェテ」ツ??」ツ?ョテ」ツ?ァテ」ツ??、ツコツ、テ・ツキツョテ」ツ?凖」ツつ凝」ツ?凝」ツ?ゥテ」ツ??」ツ?凝」ツ?ッテ・ツ按、テ・ツョツ堙」ツ?ァテ」ツ?催」ツ?ェテ」ツ??\n */\n return false;\n }\n \n Vector3d AB = B - A, CD = D - C;\n Vector3d n1 = AB / abs(AB), n2 = CD / abs(CD);\n double tmp = dot(n1,n2);\n tmp = 1 - tmp*tmp;\n \n if( equals(tmp,0.0) ) return 0; // テァツ崢エテァツキツ堙」ツ?古・ツケツウティツ。ツ?\n \n Segment3d ns = nearest_segmentLL(l1,l2);\n if( ns.p[0] == ns.p[1] ) return true;\n return false;\n}\n \nbool intersectSS(Segment3d seg1,Segment3d seg2){\n if( isParallel(seg1,seg2) ) return false;\n Segment3d seg = nearest_segmentLL(seg1,seg2);\n if( !( seg.p[0] == seg.p[1] ) ) return false;\n Point3d cp = seg.p[1];\n return on_segment3d(seg1,cp) && on_segment3d(seg2,cp);\n}\n \n \nint N;\nSegment3d ururu_beam;\nPoint3d Circle[MAX];\ndouble R[MAX];\n \ninline void Input3d(Point3d &p){ cin >> p.x >> p.y >> p.z; }\n \ninline void compute(){\n int skip = -1;\n bool update = true;\n while(update){\n update = false;\n Point3d tmp_p0,tmp_p1;\n int tmp_skip = -1;\n double tmp_dist = IINF;\n Vector3d vect = ( ururu_beam.p[1] - ururu_beam.p[0] ) / abs( ururu_beam.p[1] - ururu_beam.p[0] );\n Vector3d rvect = ( ururu_beam.p[0] - ururu_beam.p[1] ) / abs( ururu_beam.p[0] - ururu_beam.p[1] );\n ururu_beam.p[1] = ururu_beam.p[0] + vect * 500;\n rep(i,N){\n if( i == skip ) continue;\n \n double dist = distanceSP(ururu_beam,Circle[i]);\n if( !equals(dist,R[i]) && dist > R[i] ) continue;\n \n Point3d on_seg = project(ururu_beam,Circle[i]);\n \n dist = sqrt(pow(R[i],2.0)-norm(on_seg-Circle[i]));\n \n Point3d cp = on_seg + rvect * dist;\n Line3d line = Line3d(Circle[i],cp);\n \n dist = abs(ururu_beam.p[0]-cp);\n if( tmp_dist > dist ){\n tmp_p0 = cp, tmp_p1 = reflect(line,ururu_beam.p[0]);\n tmp_dist = dist;\n tmp_skip = i;\n }\n update = true;\n }\n if( update ){\n ururu_beam = Segment3d(tmp_p0,tmp_p1);\n skip = tmp_skip;\n }\n }\n printf(\"%.4f %.4f %.4f\\n\",ururu_beam.p[0].x,ururu_beam.p[0].y,ururu_beam.p[0].z);\n}\n \nint main(){\n while( cin >> N, N ){\n ururu_beam.p[0] = Point3d(0,0,0);\n Input3d(ururu_beam.p[1]);\n rep(i,N){\n Input3d(Circle[i]);\n cin >> R[i];\n }\n compute();\n }\n return 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 1260, "score_of_the_acc": -0.0084, "final_rank": 6 }, { "submission_id": "aoj_1289_924835", "code_snippet": "#include<bits/stdc++.h>\n \n#define REP(i,s,n) for(int i=s;i<n;i++)\n#define rep(i,n) REP(i,0,n)\n#define IINF (INT_MAX)\n#define EPS (1e-7)\n#define equals(a,b) (fabs((a)-(b))<EPS)\n#define MAX 110\n \nusing namespace std;\n \n//テ、ツスツ愿ヲツ按静、ツクツュ Verifyテ」ツ?療」ツ?淌」ツつづ」ツ?ョテ」ツ?ォテ」ツ?ッVerifyテ」ツ?ィテヲツ崢クテ」ツ??」ツ?ヲテ」ツ?づ」ツつ?\n \n//Verify AOJ 0115\nclass Point3d{\npublic:\n double x,y,z;\n \n Point3d(double x=0,double y=0,double z=0):x(x),y(y),z(z){}\n \n Point3d operator + (const Point3d& a){\n return Point3d(x+a.x,y+a.y,z+a.z);\n }\n Point3d operator - (const Point3d& a){\n return Point3d(x-a.x,y-a.y,z-a.z);\n }\n Point3d operator * (const double& d){\n return Point3d(x*d,y*d,z*d);\n }\n Point3d operator / (const double& d){\n return Point3d(x/d,y/d,z/d);\n }\n \n bool operator < (const Point3d& p)const{\n return !equals(x,p.x)?x<p.x:((!equals(y,p.y))?y<p.y:z<p.z);\n }\n \n bool operator == (const Point3d& p)const{\n return equals(x,p.x) && equals(y,p.y) && equals(z,p.z);\n }\n \n};\n \n//Verify AOJ 0115\nstruct Segment3d{\n Point3d p[2];\n Segment3d(Point3d p1=Point3d(),Point3d p2=Point3d()){\n p[0] = p1, p[1] = p2;\n }\n bool operator == (const Segment3d& seg)const{\n return p[0] == seg.p[0] && p[1] == seg.p[1];\n }\n};\n \ntypedef Point3d Vector3d;\ntypedef Segment3d Line3d;\n \n \n \n \nostream& operator << (ostream& os,const Point3d& p){\n os << \"(\" << p.x << \",\" << p.y << \",\" << p.z << \")\";\n}\n \nostream& operator << (ostream& os,const Segment3d& seg){\n os << \"(\" << seg.p[0] << \",\" << seg.p[1] << \")\";\n}\n \n//Verify AOJ 0115\ndouble dot(const Point3d& a,const Point3d& b){\n return a.x*b.x + a.y*b.y + a.z*b.z;\n}\n \n//Verify AOJ 0115\nVector3d cross(const Point3d& a,const Point3d& b){\n return Vector3d(a.y*b.z-a.z*b.y,a.z*b.x-a.x*b.z,a.x*b.y-a.y*b.x);\n}\n \n//Verify AOJ 0115\ninline double norm(const Point3d &p){\n return p.x*p.x + p.y*p.y + p.z*p.z;\n}\n \n//Verify AOJ 0115\ninline double abs(const Point3d &p){\n return sqrt(norm(p));\n}\n \ninline double toRad(double theta){\n return theta * M_PI / 180.0;\n}\n \n \n \nbool on_line3d(Line3d line,Point3d p){\n return equals(abs(cross(p-line.p[0],line.p[1]-line.p[0])),0);\n}\n \nbool on_segment3d(Segment3d seg,Point3d p){\n if( !on_line3d(seg,p) ) return false;\n double dist[3] = { abs(seg.p[1]-seg.p[0]), abs(p-seg.p[0]), abs(p-seg.p[1]) }; \n return on_line3d(seg,p) && equals(dist[0],dist[1]+dist[2]);\n}\n \n//Verify AOJ 0115\nbool point_on_the_triangle3d(Point3d tri1,Point3d tri2,Point3d tri3,Point3d p){\n \n //テァツキツ堙・ツ按?、ツクツ甘」ツ?ォpテ」ツ?古」ツ?づ」ツ?」テ」ツ?淌・ツ?エテ・ツ青暗」ツ??、ツクツ嘉ィツァツ津・ツスツ「テ・ツ??」ツ?ィテ」ツ?ソテ」ツ?ェテ」ツ??\n //if( on_segment3d(Segment3d(tri1,tri2),p) ) return true;\n //if( on_segment3d(Segment3d(tri2,tri3),p) ) return true;\n //if( on_segment3d(Segment3d(tri3,tri1),p) ) return true;\n \n Vector3d v1 = tri2 - tri1;\n Vector3d v2 = tri3 - tri2;\n Vector3d v3 = tri1 - tri3;\n \n Vector3d cp[3] = { cross(v1,p-tri1), cross(v2,p-tri2), cross(v3,p-tri3) };\n double d1 = dot(cp[0],cp[1]);\n double d2 = dot(cp[0],cp[2]);\n \n // テァツキツ堙・ツ按?、ツクツ甘」ツ?ォpテ」ツ?古」ツ?づ」ツ?」テ」ツ?淌・ツ?エテ・ツ青?テ、ツクツ嘉ィツァツ津・ツスツ「テ・ツ??」ツ?ィテ」ツ?ッテ」ツ?ソテ」ツ?ェテ」ツ?陛」ツ?ェテ」ツ??\n //if( ( !equals(d1,0.0) && d1 > 0 ) && ( !equals(d2,0.0) && d2 > 0 ) ) return true;\n \n // テァツキツ堙・ツ按?、ツクツ甘」ツ?ォpテ」ツ?古」ツ?づ」ツ?」テ」ツ?淌・ツ?エテ・ツ青?テ、ツクツ嘉ィツァツ津・ツスツ「テ・ツ??」ツ?ィテ」ツ?ソテ」ツ?ェテ」ツ??\n if( ( equals(d1,0.0) || d1 > 0 ) && ( equals(d2,0.0) || d2 > 0 ) ) return true;\n return false;\n}\n \ninline Point3d rotateX(Point3d p,double rad){\n return Point3d(p.x,p.y*cos(rad)-p.z*sin(rad),p.y*sin(rad)+p.z*cos(rad));\n}\n \ninline Point3d rorateY(Point3d p,double rad){\n return Point3d(p.x*cos(rad)+p.z*sin(rad),p.y,-p.x*sin(rad)+p.z*cos(rad));\n}\n \ninline Point3d rorateZ(Point3d p,double rad){\n return Point3d(p.x*cos(rad)-p.y*sin(rad),p.x*sin(rad)+p.y*cos(rad),p.z);\n}\n \ninline Point3d rotateEuler(Point3d p,double alpha,double beta,double gamma){\n return Point3d( (cos(alpha)*cos(beta)*cos(gamma)-sin(alpha)*sin(gamma)) * p.x + (-cos(alpha)*cos(beta)*sin(gamma)-sin(alpha)*cos(gamma)) * p.y + (cos(alpha)*sin(beta)) * p.z,\n\t\t (sin(alpha)*cos(beta)*cos(gamma)+cos(alpha)*sin(gamma)) * p.x + (-sin(alpha)*cos(beta)*sin(gamma)+cos(alpha)*cos(gamma)) * p.y + (sin(alpha)*sin(beta)) * p.z,\n\t\t (-sin(beta)*cos(gamma)) * p.x + (sin(beta)*sin(gamma)) * p.y + (cos(beta)) * p.z);\n}\n \ninline Point3d rotateRollPitchYaw(Point3d p,double roll,double pitch,double yaw){\n return Point3d( ( cos(roll) * cos(pitch) ) * p.x + ( cos(roll) * sin(pitch) * sin(yaw) - sin(roll) * cos(yaw) ) * p.y + ( cos(roll) * sin(pitch) * cos(yaw) + sin(roll) * sin(yaw) ) * p.z,\n\t\t ( sin(roll) * cos(pitch) ) * p.x + ( sin(roll) * sin(pitch) * sin(yaw) + cos(roll) * cos(yaw) ) * p.y + ( sin(roll) * sin(pitch) * cos(yaw) - cos(roll) * sin(yaw) ) * p.z,\n\t\t -sin(pitch) * p.x + cos(pitch) * sin(yaw) * p.y + cos(pitch) * cos(yaw) * p.z);\n}\n \n \nclass Plane3d{\npublic:\n Point3d normal_vector; //テヲツウツ陛ァツキツ堙」ツδ凖」ツつッテ」ツδ暗」ツδォ\n double d; // テ・ツケツウテゥツ敖「テヲツ鳴ケテァツィツ凝・ツシツ?normal_vector.x * x + normal_vector.y * y + normal_vector.z * z + d = 0\n \n Plane3d(Point3d normal_vector=Point3d(),double d=0):normal_vector(normal_vector),d(d){}\n Plane3d(Vector3d a,Vector3d b,Vector3d c){\n Vector3d v1 = b - a;\n Vector3d v2 = c - a;\n Vector3d tmp = cross(v1,v2);\n normal_vector = tmp / abs(tmp);\n set_d(a);\n }\n \n //Verify AOJ 0115\n //テヲツウツ陛ァツキツ堙」ツδ凖」ツつッテ」ツδ暗」ツδォnormal_vectorテ」ツ?ィテ・ツケツウテゥツ敖「テ、ツクツ甘」ツ?ョテッツシツ妥ァツつケテ」ツ?凝」ツつ嬰テ」ツつ津ィツィツ暗ァツョツ療」ツ?凖」ツつ?\n void set_d(Point3d p){\n d = dot(normal_vector,p);\n }\n \n //テ・ツケツウテゥツ敖「テ」ツ?ィテァツつケpテ」ツ?ョティツキツ敕ゥツ崢「テ」ツつ津ヲツアツづ」ツつ?」ツつ?\n double distanceP(Point3d p){\n Point3d a = normal_vector * d;//テ・ツケツウテゥツ敖「テ、ツクツ甘」ツ?ョテゥツ?ゥテ・ツスツ禿」ツ?ェテァツつケテ」ツつ津」ツ?、テ」ツ?湘」ツつ?\n return abs( dot(p-a,normal_vector) );\n }\n \n //テ・ツケツウテゥツ敖「テ、ツクツ甘」ツ?ァテ」ツつづ」ツ?」テ」ツ?ィテ」ツつづァツつケpテ」ツ?ィティツソツ妥」ツ??ァツつケテ」ツつ津ヲツアツづ」ツつ?」ツつ?\n Point3d nearest_point(Point3d p){\n Point3d a = normal_vector * d;\n return p - ( normal_vector * dot(p-a,normal_vector) );\n }\n \n //Verify AOJ 0115\n //テ・ツケツウテゥツ敖「テ」ツ?ィテァツキツ堙・ツ按?」ツ?古、ツコツ、テ・ツキツョテ」ツ?凖」ツつ凝」ツ??\n bool intersectS(Segment3d seg){\n Point3d a = normal_vector * d;\n double res1 = dot(a-seg.p[0],normal_vector);\n double res2 = dot(a-seg.p[1],normal_vector);\n if( res1 > res2 ) swap(res1,res2);\n //cout << res1 << \" < \" << res2 << endl;\n if( ( equals(res1,0.0) || res1 < 0 ) && ( equals(res2,0.0) || res2 > 0 ) ) return true;\n return false;\n }\n \n //Verify AOJ 0115\n //テ・ツケツウテゥツ敖「テ」ツ?ィテァツキツ堙・ツ按?」ツ?ョテ、ツコツ、テァツつケテ」ツつ津ヲツアツづ」ツつ?」ツつ?\n Point3d crosspointS(Segment3d seg){\n Point3d a = normal_vector * d;\n double dot_p0a = fabs(dot( seg.p[0]-a,normal_vector ));\n double dot_p1a = fabs(dot( seg.p[1]-a,normal_vector ));\n if( equals(dot_p0a+dot_p1a,0) ) return seg.p[0];\n return seg.p[0] + ( seg.p[1] - seg.p[0] ) * ( dot_p0a / ( dot_p0a + dot_p1a ) );\n }\n \n};\n \n// Verify AOJ 1289\ndouble distanceLP(Line3d line,Point3d p){\n return abs(cross(line.p[1]-line.p[0],p-line.p[0])) / abs(line.p[1]-line.p[0]);\n}\n\n// Verify AOJ 1289\ndouble distanceSP(Segment3d seg,Point3d p){\n double res = dot(seg.p[1]-seg.p[0],p-seg.p[0]);\n if( !equals(res,0.0) && res < 0.0 ) return abs( p - seg.p[0] );\n res = dot(seg.p[0]-seg.p[1],p-seg.p[1]);\n if( !equals(res,0.0) && res < 0.0 ) return abs( p - seg.p[1] );\n return distanceLP(seg,p);\n}\n \n// Verify AOJ 1289\nPoint3d project(Segment3d seg,Point3d p){\n Vector3d base = seg.p[1] - seg.p[0];\n double t = dot(p-seg.p[0],base) / norm(base);\n return seg.p[0] + base * t;\n}\n \n// Verify AOJ 1289\nPoint3d reflect(Segment3d seg,Point3d p){\n return p + (project(seg,p)-p) * 2.0;\n}\n\n//Verify AOJ 2081\nbool isParallel(Line3d l1,Line3d l2){\n Vector3d A = l1.p[0], B = l1.p[1], C = l2.p[0], D = l2.p[1];\n Vector3d AB = B - A, CD = D - C;\n Vector3d n1 = AB / abs(AB), n2 = CD / abs(CD);\n double tmp = dot(n1,n2);\n tmp = 1 - tmp*tmp;\n return equals(tmp,0.0);\n}\n \n//Verify AOJ 2081 \n//l1,l2テ」ツ?古・ツケツウティツ。ツ古」ツ?ェテヲツ卍づ」ツ?ォテ」ツ?ッテ、ツスツソテァツ板ィテ」ツ?ァテ」ツ?催」ツ?ェテ」ツ??」ツ?ョテ」ツ?ァテヲツウツィテヲツ??\nSegment3d nearest_segmentLL(Line3d l1,Line3d l2){\n assert(!isParallel(l1,l2)); // テ・ツケツウティツ。ツ古」ツ?ェテ・ツ?エテ・ツ青暗」ツ?ッテ、ツスツソテァツ板ィテ、ツクツ催・ツ渉ッ\n // l1.p[0] = A, l1.p[1] = B, l2.p[0] = C, l2.p[1] = D\n Vector3d AB = l1.p[1] - l1.p[0];\n Vector3d CD = l2.p[1] - l2.p[0];\n Vector3d AC = l2.p[0] - l1.p[0];\n Vector3d n1 = AB / abs(AB), n2 = CD / abs(CD);\n double d1 = (dot(n1,AC)-dot(n1,n2)*dot(n2,AC)) / (1.0-pow(dot(n1,n2),2));\n double d2 = (dot(n1,n2)*dot(n1,AC)-dot(n2,AC)) / (1.0-pow(dot(n1,n2),2));\n return Segment3d(l1.p[0]+n1*d1,l2.p[0]+n2*d2);\n}\n \n//Verify AOJ 2081\nbool intersectLL(Line3d l1,Line3d l2){\n Vector3d A = l1.p[0], B = l1.p[1], C = l2.p[0], D = l2.p[1];\n \n //テ」ツ?敕」ツつづ」ツ?敕」ツつM1,l2テ」ツ?古ァツ崢エテァツキツ堙」ツ?佚」ツつε」ツ?ェテ」ツ??\n if( equals(abs(B-A),0.0) || equals(abs(D-C),0.0) ){\n /*\n テ」ツ?禿」ツ?ョテ・ツ?エテ・ツ青暗」ツ?ッテヲツウツィテヲツ??\n テ」ツ?敕」ツつづ」ツ?敕」ツつづ、ツクツ偲」ツ?暗」ツつ嘉」ツつ古」ツ?淌ァツキツ堙・ツ按?」ツ?古ァツキツ堙・ツ按?」ツ?ォテ」ツ?ェテ」ツ?」テ」ツ?ヲテ」ツ??」ツ?ェテ」ツ??」ツ?ョテ」ツ?ァテ」ツ??、ツコツ、テ・ツキツョテ」ツ?凖」ツつ凝」ツ?凝」ツ?ゥテ」ツ??」ツ?凝」ツ?ッテ・ツ按、テ・ツョツ堙」ツ?ァテ」ツ?催」ツ?ェテ」ツ??\n */\n return false;\n }\n \n Vector3d AB = B - A, CD = D - C;\n Vector3d n1 = AB / abs(AB), n2 = CD / abs(CD);\n double tmp = dot(n1,n2);\n tmp = 1 - tmp*tmp;\n \n if( equals(tmp,0.0) ) return 0; // テァツ崢エテァツキツ堙」ツ?古・ツケツウティツ。ツ?\n \n Segment3d ns = nearest_segmentLL(l1,l2);\n if( ns.p[0] == ns.p[1] ) return true;\n return false;\n}\n\nbool intersectSS(Segment3d seg1,Segment3d seg2){\n if( isParallel(seg1,seg2) ) return false;\n Segment3d seg = nearest_segmentLL(seg1,seg2);\n if( !( seg.p[0] == seg.p[1] ) ) return false;\n Point3d cp = seg.p[1];\n return on_segment3d(seg1,cp) && on_segment3d(seg2,cp);\n}\n\n\nint N;\nSegment3d ururu_beam;\nPoint3d Circle[MAX];\ndouble R[MAX];\n\ninline void Input3d(Point3d &p){ cin >> p.x >> p.y >> p.z; }\n\ninline void compute(){\n int skip = -1;\n bool update = true;\n while(update){\n update = false;\n Point3d tmp_p0,tmp_p1;\n int tmp_skip = -1;\n double tmp_dist = IINF;\n Vector3d vect = ( ururu_beam.p[1] - ururu_beam.p[0] ) / abs( ururu_beam.p[1] - ururu_beam.p[0] );\n Vector3d rvect = ( ururu_beam.p[0] - ururu_beam.p[1] ) / abs( ururu_beam.p[0] - ururu_beam.p[1] );\n ururu_beam.p[1] = ururu_beam.p[0] + vect * 500;\n rep(i,N){\n if( i == skip ) continue;\n\n double dist = distanceSP(ururu_beam,Circle[i]);\n if( !equals(dist,R[i]) && dist > R[i] ) continue;\n\n Point3d on_seg = project(ururu_beam,Circle[i]);\n\n dist = sqrt(pow(R[i],2.0)-norm(on_seg-Circle[i]));\n double x = R[i] * R[i] - norm(on_seg-Circle[i]);\n if( x == 0 ) dist = 0;\n else if( x < 0 ) continue;\n\n Point3d cp = on_seg + rvect * dist;\n Line3d line = Line3d(Circle[i],cp);\n\n dist = abs(ururu_beam.p[0]-cp);\n if( tmp_dist > dist ){\n tmp_p0 = cp, tmp_p1 = reflect(line,ururu_beam.p[0]);\n tmp_dist = dist;\n tmp_skip = i;\n }\n update = true;\n }\n if( update ){\n ururu_beam = Segment3d(tmp_p0,tmp_p1);\n skip = tmp_skip;\n }\n }\n printf(\"%.4f %.4f %.4f\\n\",ururu_beam.p[0].x,ururu_beam.p[0].y,ururu_beam.p[0].z);\n}\n\nint main(){\n while( cin >> N, N ){\n ururu_beam.p[0] = Point3d(0,0,0);\n Input3d(ururu_beam.p[1]);\n rep(i,N){\n Input3d(Circle[i]);\n cin >> R[i];\n }\n compute();\n }\n return 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 1260, "score_of_the_acc": -0.0084, "final_rank": 6 }, { "submission_id": "aoj_1289_924834", "code_snippet": "#include<bits/stdc++.h>\n \n#define REP(i,s,n) for(int i=s;i<n;i++)\n#define rep(i,n) REP(i,0,n)\n#define IINF (INT_MAX)\n#define EPS (1e-7)\n#define equals(a,b) (fabs((a)-(b))<EPS)\n#define MAX 110\n \nusing namespace std;\n \n//作成中 VerifyしたものにはVerifyと書いてある\n \n//Verify AOJ 0115\nclass Point3d{\npublic:\n double x,y,z;\n \n Point3d(double x=0,double y=0,double z=0):x(x),y(y),z(z){}\n \n Point3d operator + (const Point3d& a){\n return Point3d(x+a.x,y+a.y,z+a.z);\n }\n Point3d operator - (const Point3d& a){\n return Point3d(x-a.x,y-a.y,z-a.z);\n }\n Point3d operator * (const double& d){\n return Point3d(x*d,y*d,z*d);\n }\n Point3d operator / (const double& d){\n return Point3d(x/d,y/d,z/d);\n }\n \n bool operator < (const Point3d& p)const{\n return !equals(x,p.x)?x<p.x:((!equals(y,p.y))?y<p.y:z<p.z);\n }\n \n bool operator == (const Point3d& p)const{\n return equals(x,p.x) && equals(y,p.y) && equals(z,p.z);\n }\n \n};\n \n//Verify AOJ 0115\nstruct Segment3d{\n Point3d p[2];\n Segment3d(Point3d p1=Point3d(),Point3d p2=Point3d()){\n p[0] = p1, p[1] = p2;\n }\n bool operator == (const Segment3d& seg)const{\n return p[0] == seg.p[0] && p[1] == seg.p[1];\n }\n};\n \ntypedef Point3d Vector3d;\ntypedef Segment3d Line3d;\n \n \n \n \nostream& operator << (ostream& os,const Point3d& p){\n os << \"(\" << p.x << \",\" << p.y << \",\" << p.z << \")\";\n}\n \nostream& operator << (ostream& os,const Segment3d& seg){\n os << \"(\" << seg.p[0] << \",\" << seg.p[1] << \")\";\n}\n \n//Verify AOJ 0115\ndouble dot(const Point3d& a,const Point3d& b){\n return a.x*b.x + a.y*b.y + a.z*b.z;\n}\n \n//Verify AOJ 0115\nVector3d cross(const Point3d& a,const Point3d& b){\n return Vector3d(a.y*b.z-a.z*b.y,a.z*b.x-a.x*b.z,a.x*b.y-a.y*b.x);\n}\n \n//Verify AOJ 0115\ninline double norm(const Point3d &p){\n return p.x*p.x + p.y*p.y + p.z*p.z;\n}\n \n//Verify AOJ 0115\ninline double abs(const Point3d &p){\n return sqrt(norm(p));\n}\n \ninline double toRad(double theta){\n return theta * M_PI / 180.0;\n}\n \n \n \nbool on_line3d(Line3d line,Point3d p){\n return equals(abs(cross(p-line.p[0],line.p[1]-line.p[0])),0);\n}\n \nbool on_segment3d(Segment3d seg,Point3d p){\n if( !on_line3d(seg,p) ) return false;\n double dist[3] = { abs(seg.p[1]-seg.p[0]), abs(p-seg.p[0]), abs(p-seg.p[1]) }; \n return on_line3d(seg,p) && equals(dist[0],dist[1]+dist[2]);\n}\n \n//Verify AOJ 0115\nbool point_on_the_triangle3d(Point3d tri1,Point3d tri2,Point3d tri3,Point3d p){\n \n //線分上にpがあった場合、三角形内とみなす\n //if( on_segment3d(Segment3d(tri1,tri2),p) ) return true;\n //if( on_segment3d(Segment3d(tri2,tri3),p) ) return true;\n //if( on_segment3d(Segment3d(tri3,tri1),p) ) return true;\n \n Vector3d v1 = tri2 - tri1;\n Vector3d v2 = tri3 - tri2;\n Vector3d v3 = tri1 - tri3;\n \n Vector3d cp[3] = { cross(v1,p-tri1), cross(v2,p-tri2), cross(v3,p-tri3) };\n double d1 = dot(cp[0],cp[1]);\n double d2 = dot(cp[0],cp[2]);\n \n // 線分上にpがあった場合,三角形内とはみなさない\n //if( ( !equals(d1,0.0) && d1 > 0 ) && ( !equals(d2,0.0) && d2 > 0 ) ) return true;\n \n // 線分上にpがあった場合,三角形内とみなす\n if( ( equals(d1,0.0) || d1 > 0 ) && ( equals(d2,0.0) || d2 > 0 ) ) return true;\n return false;\n}\n \ninline Point3d rotateX(Point3d p,double rad){\n return Point3d(p.x,p.y*cos(rad)-p.z*sin(rad),p.y*sin(rad)+p.z*cos(rad));\n}\n \ninline Point3d rorateY(Point3d p,double rad){\n return Point3d(p.x*cos(rad)+p.z*sin(rad),p.y,-p.x*sin(rad)+p.z*cos(rad));\n}\n \ninline Point3d rorateZ(Point3d p,double rad){\n return Point3d(p.x*cos(rad)-p.y*sin(rad),p.x*sin(rad)+p.y*cos(rad),p.z);\n}\n \ninline Point3d rotateEuler(Point3d p,double alpha,double beta,double gamma){\n return Point3d( (cos(alpha)*cos(beta)*cos(gamma)-sin(alpha)*sin(gamma)) * p.x + (-cos(alpha)*cos(beta)*sin(gamma)-sin(alpha)*cos(gamma)) * p.y + (cos(alpha)*sin(beta)) * p.z,\n\t\t (sin(alpha)*cos(beta)*cos(gamma)+cos(alpha)*sin(gamma)) * p.x + (-sin(alpha)*cos(beta)*sin(gamma)+cos(alpha)*cos(gamma)) * p.y + (sin(alpha)*sin(beta)) * p.z,\n\t\t (-sin(beta)*cos(gamma)) * p.x + (sin(beta)*sin(gamma)) * p.y + (cos(beta)) * p.z);\n}\n \ninline Point3d rotateRollPitchYaw(Point3d p,double roll,double pitch,double yaw){\n return Point3d( ( cos(roll) * cos(pitch) ) * p.x + ( cos(roll) * sin(pitch) * sin(yaw) - sin(roll) * cos(yaw) ) * p.y + ( cos(roll) * sin(pitch) * cos(yaw) + sin(roll) * sin(yaw) ) * p.z,\n\t\t ( sin(roll) * cos(pitch) ) * p.x + ( sin(roll) * sin(pitch) * sin(yaw) + cos(roll) * cos(yaw) ) * p.y + ( sin(roll) * sin(pitch) * cos(yaw) - cos(roll) * sin(yaw) ) * p.z,\n\t\t -sin(pitch) * p.x + cos(pitch) * sin(yaw) * p.y + cos(pitch) * cos(yaw) * p.z);\n}\n \n \nclass Plane3d{\npublic:\n Point3d normal_vector; //法線ベクトル\n double d; // 平面方程式 normal_vector.x * x + normal_vector.y * y + normal_vector.z * z + d = 0\n \n Plane3d(Point3d normal_vector=Point3d(),double d=0):normal_vector(normal_vector),d(d){}\n Plane3d(Vector3d a,Vector3d b,Vector3d c){\n Vector3d v1 = b - a;\n Vector3d v2 = c - a;\n Vector3d tmp = cross(v1,v2);\n normal_vector = tmp / abs(tmp);\n set_d(a);\n }\n \n //Verify AOJ 0115\n //法線ベクトルnormal_vectorと平面上の1点からdを計算する\n void set_d(Point3d p){\n d = dot(normal_vector,p);\n }\n \n //平面と点pの距離を求める\n double distanceP(Point3d p){\n Point3d a = normal_vector * d;//平面上の適当な点をつくる\n return abs( dot(p-a,normal_vector) );\n }\n \n //平面上でもっとも点pと近い点を求める\n Point3d nearest_point(Point3d p){\n Point3d a = normal_vector * d;\n return p - ( normal_vector * dot(p-a,normal_vector) );\n }\n \n //Verify AOJ 0115\n //平面と線分が交差するか\n bool intersectS(Segment3d seg){\n Point3d a = normal_vector * d;\n double res1 = dot(a-seg.p[0],normal_vector);\n double res2 = dot(a-seg.p[1],normal_vector);\n if( res1 > res2 ) swap(res1,res2);\n //cout << res1 << \" < \" << res2 << endl;\n if( ( equals(res1,0.0) || res1 < 0 ) && ( equals(res2,0.0) || res2 > 0 ) ) return true;\n return false;\n }\n \n //Verify AOJ 0115\n //平面と線分の交点を求める\n Point3d crosspointS(Segment3d seg){\n Point3d a = normal_vector * d;\n double dot_p0a = fabs(dot( seg.p[0]-a,normal_vector ));\n double dot_p1a = fabs(dot( seg.p[1]-a,normal_vector ));\n if( equals(dot_p0a+dot_p1a,0) ) return seg.p[0];\n return seg.p[0] + ( seg.p[1] - seg.p[0] ) * ( dot_p0a / ( dot_p0a + dot_p1a ) );\n }\n \n};\n \ndouble distanceLP(Line3d line,Point3d p){\n return abs(cross(line.p[1]-line.p[0],p-line.p[0])) / abs(line.p[1]-line.p[0]);\n}\n\ndouble distanceSP(Segment3d seg,Point3d p){\n double res = dot(seg.p[1]-seg.p[0],p-seg.p[0]);\n if( !equals(res,0.0) && res < 0.0 ) return abs( p - seg.p[0] );\n res = dot(seg.p[0]-seg.p[1],p-seg.p[1]);\n if( !equals(res,0.0) && res < 0.0 ) return abs( p - seg.p[1] );\n return distanceLP(seg,p);\n}\n \nPoint3d project(Segment3d seg,Point3d p){\n Vector3d base = seg.p[1] - seg.p[0];\n double t = dot(p-seg.p[0],base) / norm(base);\n return seg.p[0] + base * t;\n}\n \nPoint3d reflect(Segment3d seg,Point3d p){\n return p + (project(seg,p)-p) * 2.0;\n}\n\n//Verify AOJ 2081\nbool isParallel(Line3d l1,Line3d l2){\n Vector3d A = l1.p[0], B = l1.p[1], C = l2.p[0], D = l2.p[1];\n Vector3d AB = B - A, CD = D - C;\n Vector3d n1 = AB / abs(AB), n2 = CD / abs(CD);\n double tmp = dot(n1,n2);\n tmp = 1 - tmp*tmp;\n return equals(tmp,0.0);\n}\n \n//Verify AOJ 2081 \n//l1,l2が平行な時には使用できないので注意\nSegment3d nearest_segmentLL(Line3d l1,Line3d l2){\n assert(!isParallel(l1,l2)); // 平行な場合は使用不可\n // l1.p[0] = A, l1.p[1] = B, l2.p[0] = C, l2.p[1] = D\n Vector3d AB = l1.p[1] - l1.p[0];\n Vector3d CD = l2.p[1] - l2.p[0];\n Vector3d AC = l2.p[0] - l1.p[0];\n Vector3d n1 = AB / abs(AB), n2 = CD / abs(CD);\n double d1 = (dot(n1,AC)-dot(n1,n2)*dot(n2,AC)) / (1.0-pow(dot(n1,n2),2));\n double d2 = (dot(n1,n2)*dot(n1,AC)-dot(n2,AC)) / (1.0-pow(dot(n1,n2),2));\n return Segment3d(l1.p[0]+n1*d1,l2.p[0]+n2*d2);\n}\n \n//Verify AOJ 2081\nbool intersectLL(Line3d l1,Line3d l2){\n Vector3d A = l1.p[0], B = l1.p[1], C = l2.p[0], D = l2.p[1];\n \n //そもそもl1,l2が直線じゃない\n if( equals(abs(B-A),0.0) || equals(abs(D-C),0.0) ){\n /*\n この場合は注意\n そもそも与えられた線分が線分になっていないので、交差するかどうかは判定できない\n */\n return false;\n }\n \n Vector3d AB = B - A, CD = D - C;\n Vector3d n1 = AB / abs(AB), n2 = CD / abs(CD);\n double tmp = dot(n1,n2);\n tmp = 1 - tmp*tmp;\n \n if( equals(tmp,0.0) ) return 0; // 直線が平行\n \n Segment3d ns = nearest_segmentLL(l1,l2);\n if( ns.p[0] == ns.p[1] ) return true;\n return false;\n}\n\nbool intersectSS(Segment3d seg1,Segment3d seg2){\n if( isParallel(seg1,seg2) ) return false;\n Segment3d seg = nearest_segmentLL(seg1,seg2);\n if( !( seg.p[0] == seg.p[1] ) ) return false;\n Point3d cp = seg.p[1];\n return on_segment3d(seg1,cp) && on_segment3d(seg2,cp);\n}\n\n\nint N;\nSegment3d ururu_beam;\nPoint3d Circle[MAX];\ndouble R[MAX];\n\ninline void Input3d(Point3d &p){ cin >> p.x >> p.y >> p.z; }\n\ninline void compute(){\n int skip = -1;\n bool update = true;\n while(update){\n update = false;\n Point3d tmp_p0,tmp_p1;\n int tmp_skip = -1;\n double tmp_dist = IINF;\n Vector3d vect = ( ururu_beam.p[1] - ururu_beam.p[0] ) / abs( ururu_beam.p[1] - ururu_beam.p[0] );\n Vector3d rvect = ( ururu_beam.p[0] - ururu_beam.p[1] ) / abs( ururu_beam.p[0] - ururu_beam.p[1] );\n ururu_beam.p[1] = ururu_beam.p[0] + vect * 500;\n rep(i,N){\n if( i == skip ) continue;\n\n double dist = distanceSP(ururu_beam,Circle[i]);\n if( !equals(dist,R[i]) && dist > R[i] ) continue;\n\n Point3d on_seg = project(ururu_beam,Circle[i]);\n\n dist = sqrt(pow(R[i],2.0)-norm(on_seg-Circle[i]));\n double x = R[i] * R[i] - norm(on_seg-Circle[i]);\n if( x == 0 ) dist = 0;\n else if( x < 0 ) continue;\n\n Point3d cp = on_seg + rvect * dist;\n Line3d line = Line3d(Circle[i],cp);\n\n dist = abs(ururu_beam.p[0]-cp);\n if( tmp_dist > dist ){\n tmp_p0 = cp, tmp_p1 = reflect(line,ururu_beam.p[0]);\n tmp_dist = dist;\n tmp_skip = i;\n }\n update = true;\n }\n if( update ){\n ururu_beam = Segment3d(tmp_p0,tmp_p1);\n skip = tmp_skip;\n }\n }\n printf(\"%.4f %.4f %.4f\\n\",ururu_beam.p[0].x,ururu_beam.p[0].y,ururu_beam.p[0].z);\n}\n\nint main(){\n while( cin >> N, N ){\n ururu_beam.p[0] = Point3d(0,0,0);\n Input3d(ururu_beam.p[1]);\n rep(i,N){\n Input3d(Circle[i]);\n cin >> R[i];\n }\n compute();\n }\n return 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 1260, "score_of_the_acc": -0.0084, "final_rank": 6 } ]
aoj_1288_cpp
Problem D: Digits on the Floor Taro attempts to tell digits to Hanako by putting straight bars on the floor. Taro wants to express each digit by making one of the forms shown in Figure 1. Since Taro may not have bars of desired lengths, Taro cannot always make forms exactly as shown in Figure 1. Fortunately, Hanako can recognize a form as a digit if the connection relation between bars in the form is kept. Neither the lengths of bars nor the directions of forms affect Hanako’s perception as long as the connection relation remains the same. For example, Hanako can recognize all the awkward forms in Figure 2 as digits. On the other hand, Hanako cannot recognize the forms in Figure 3 as digits. For clarity, touching bars are slightly separated in Figures 1, 2 and 3. Actually, touching bars overlap exactly at one single point. Figure 1: Representation of digits Figure 2: Examples of forms recognized as digits In the forms, when a bar touches another, the touching point is an end of at least one of them. That is, bars never cross. In addition, the angle of such two bars is always a right angle. To enable Taro to represent forms with his limited set of bars, positions and lengths of bars can be changed as far as the connection relations are kept. Also, forms can be rotated. Keeping the connection relations means the following. Figure 3: Forms not recognized as digits (these kinds of forms are not contained in the dataset) Separated bars are not made to touch. Touching bars are not made separate. When one end of a bar touches another bar, that end still touches the same bar. When it touches a midpoint of the other bar, it remains to touch a midpoint of the same bar on the same side. The angle of touching two bars is kept to be the same right angle (90 degrees and -90 degrees are considered different, and forms for 2 and 5 are kept distinguished). Your task is to find how many times each digit appears on the floor. The forms of some digits always contain the forms of other digits. For example, a form for 9 always contains four forms for 1, one form for 4, and two overlapping forms for 7. In this problem, ignore the forms contained in another form and count only the digit of the “largest” form composed of all mutually connecting bars. If there is one form for 9, it should be interpreted as one appearance of 9 and no appearance of 1, 4, or 7. Input The input consists of a number of datasets. Each dataset is formatted as follows. n x 1a y 1a x 1b y 1b x 2a y 2a x 2b y 2b . . . x na y na x nb y nb In the first line, n represents the number of bars in the dataset. For the rest of the lines, one line represents one bar. Four integers x a , y a , x b , y b , delimited by single spaces, are given in each line. x a and y a are the x- and y- coordinates of one end of the bar, respectively. x b and y b are those of the other end. The coordinate system is as shown in Figure 4. You can assume 1 ≤ n ≤ 1000 and 0 ≤ x a , y a , x b , y b ≤ 1000. The end of the input is ...(truncated)
[ { "submission_id": "aoj_1288_10946400", "code_snippet": "#include <cstdio>\n#include <cstdlib>\n#include <cstring>\nusing namespace std;\nint n, xa[10010], xb[10010], ya[10010], yb[10010], v[1010][1010], xx1, xx2, q1[10010];\nint q[10010], qq[10010], r, ll, rr, in[10010], v1[10010], ans[100], c1, c2, c3;\nstruct arr{\n\tint x, y;\n}x1, x2, x3, u;\nint main()\n{\n\twhile (1)\n\t{\n\t\tmemset(v, 0, sizeof(v));\n\t\tmemset(v1, 0, sizeof(v1));\n\t\tmemset(ans, 0, sizeof(ans));\n\t\tscanf(\"%d\", &n);\n\t\tif (!n) break;\n\t\tr = 0;\n\t\tfor (int i = 1; i <= n; i++)\n\t\t{\n\t\t\tscanf(\"%d%d%d%d\", &xa[i], &ya[i], &xb[i], &yb[i]);\n\t\t\tif (!v[xa[i]][ya[i]]) q[++r] = xa[i] * 10000 + ya[i], v[xa[i]][ya[i]] = r;\n\t\t\tif (!v[xb[i]][yb[i]]) q[++r] = xb[i] * 10000 + yb[i], v[xb[i]][yb[i]] = r;\n\t\t}\n\t\tfor (int ii = 1; ii <= r; ii++)\n\t\tif (!v1[ii])\n\t\t{\n\t\t\tll = rr = 0;\n\t\t\tq1[0] = 0;\n\t\t\tc1 = c2 = c3 = 0;\n\t\t\tqq[++rr] = ii, v1[ii] = 1;\n\t\t\twhile (ll < rr)\n\t\t\t{\n\t\t\t\tu.x = q[qq[++ll]] / 10000, u.y = q[qq[ll]] % 10000;\n\t\t\t\tin[ll] = 0;\n\t\t\t\tfor (int j = 1; j <= n; j++)\n\t\t\t\t{\n\t\t\t\t\tif ((xa[j] - u.x) * (xb[j] - u.x) <= 0 && (ya[j] - u.y) * (yb[j] - u.y) <= 0 &&\n\t\t\t\t\t(xa[j] - u.x) * (yb[j] - u.y) == (xb[j] - u.x) * (ya[j] - u.y))\n\t\t\t\t\t{\n\t\t\t\t\t\tif (xb[j] != u.x || yb[j] != u.y)\n\t\t\t\t\t\t{\n\t\t\t\t\t\t\tin[ll]++;\n\t\t\t\t\t\t\tif (!v1[v[xb[j]][yb[j]]])\n\t\t\t\t\t\t\t\tqq[++rr] = v[xb[j]][yb[j]], v1[v[xb[j]][yb[j]]] = 1;\n\t\t\t\t\t\t}\n\t\t\t\t\t\tif (xa[j] != u.x || ya[j] != u.y)\n\t\t\t\t\t\t{\n\t\t\t\t\t\t\tin[ll]++;\n\t\t\t\t\t\t\tif (!v1[v[xa[j]][ya[j]]])\n\t\t\t\t\t\t\t\tqq[++rr] = v[xa[j]][ya[j]], v1[v[xa[j]][ya[j]]] = 1;\n\t\t\t\t\t\t}\n\t\t\t\t\t\tq1[++q1[0]] = j;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\tif (in[ll] == 1) c1++;\n\t\t\t\tif (in[ll] == 2) c2++;\n\t\t\t\tif (in[ll] == 3) c3++;\n\t\t\t}\n\t\t\t\n\t\t\tfor (int j = 1; j <= q1[0]; j++)\n\t\t\t\tfor (int k = 1; k <= r; k++)\n\t\t\t\t\tif (!v1[k])\n\t\t\t\t\t{\n\t\t\t\t\t\tu.x = q[k] / 10000, u.y = q[k] % 10000;\n\t\t\t\t\t\tif ((xa[q1[j]] - u.x) * (xb[q1[j]] - u.x) <= 0 && (ya[q1[j]] - u.y) * (yb[q1[j]] - u.y) <= 0 &&\n\t\t\t\t\t\t(xa[q1[j]] - u.x) * (yb[q1[j]] - u.y) == (xb[q1[j]] - u.x) * (ya[q1[j]] - u.y))\n\t\t\t\t\t\t\tqq[++rr] = k, v1[k] = 1;\n\t\t\t\t\t}\n\t\t\t\n\t\t\twhile (ll < rr)\n\t\t\t{\n\t\t\t\tu.x = q[qq[++ll]] / 10000, u.y = q[qq[ll]] % 10000;\n\t\t\t\tin[ll] = 0;\n\t\t\t\tfor (int j = 1; j <= n; j++)\n\t\t\t\t{\n\t\t\t\t\tif ((xa[j] - u.x) * (xb[j] - u.x) <= 0 && (ya[j] - u.y) * (yb[j] - u.y) <= 0 &&\n\t\t\t\t\t(xa[j] - u.x) * (yb[j] - u.y) == (xb[j] - u.x) * (ya[j] - u.y))\n\t\t\t\t\t{\n\t\t\t\t\t\tif (xb[j] != u.x || yb[j] != u.y)\n\t\t\t\t\t\t{\n\t\t\t\t\t\t\tin[ll]++;\n\t\t\t\t\t\t\tif (!v1[v[xb[j]][yb[j]]])\n\t\t\t\t\t\t\t{\n\t\t\t\t\t\t\t\tqq[++rr] = v[xb[j]][yb[j]], v1[v[xb[j]][yb[j]]] = 1;\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t}\n\t\t\t\t\t\tif (xa[j] != u.x || ya[j] != u.y)\n\t\t\t\t\t\t{\n\t\t\t\t\t\t\tin[ll]++;\n\t\t\t\t\t\t\tif (!v1[v[xa[j]][ya[j]]]) qq[++rr] = v[xa[j]][ya[j]], v1[v[xa[j]][ya[j]]] = 1;\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\tif (in[ll] == 1) c1++;\n\t\t\t\tif (in[ll] == 2) c2++;\n\t\t\t\tif (in[ll] == 3) c3++;\n\t\t\t}\n\t\t\tif (c1 == 0 && c2 == 4 && c3 == 0) ans[0]++;\n\t\t\tif (c1 == 2 && c2 == 0 && c3 == 0) ans[1]++;\n\t\t\tif (c1 == 3 && c2 == 2 && c3 == 1) ans[3]++;\n\t\t\tif (c1 == 3 && c2 == 1 && c3 == 1) ans[4]++;\n\t\t\tif (c1 == 1 && c2 == 4 && c3 == 1) ans[6]++;\n\t\t\tif (c1 == 2 && c2 == 2 && c3 == 0) ans[7]++;\n\t\t\tif (c1 == 0 && c2 == 4 && c3 == 2) ans[8]++;\n\t\t\tif (c1 == 1 && c2 == 3 && c3 == 1) ans[9]++;\n\t\t\tif (c1 == 2 && c2 == 4 && c3 == 0)\n\t\t\t{\n\t\t\t\tfor (int i = 1; i <= rr; i++)\n\t\t\t\tif (in[i] == 1)\n\t\t\t\t{\n\t\t\t\t\tx1.x = q[qq[i]] / 10000, x1.y = q[qq[i]] % 10000;\n\t\t\t\t\txx1 = qq[i];\n\t\t\t\t\tbreak;\n\t\t\t\t}\n\t\t\t\tfor (int i = 1; i <= n; i++)\n\t\t\t\t{\n\t\t\t\t\tif (v[xa[i]][ya[i]] == xx1)\n\t\t\t\t\t{\n\t\t\t\t\t\tx2.x = xb[i], x2.y = yb[i];\n\t\t\t\t\t\tbreak;\n\t\t\t\t\t}\n\t\t\t\t\tif (v[xb[i]][yb[i]] == xx1)\n\t\t\t\t\t{\n\t\t\t\t\t\tx2.x = xa[i], x2.y = ya[i];\n\t\t\t\t\t\tbreak;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\txx2 = v[x2.x][x2.y];\n\t\t\t\tfor (int i = 1; i <= n; i++)\n\t\t\t\t{\n\t\t\t\t\tif (v[xa[i]][ya[i]] == xx2 && v[xb[i]][yb[i]] != xx1)\n\t\t\t\t\t{\n\t\t\t\t\t\tx3.x = xb[i], x3.y = yb[i];\n\t\t\t\t\t\tbreak;\n\t\t\t\t\t}\n\t\t\t\t\tif (v[xa[i]][ya[i]] != xx1 && v[xb[i]][yb[i]] == xx2)\n\t\t\t\t\t{\n\t\t\t\t\t\tx3.x = xa[i], x3.y = ya[i];\n\t\t\t\t\t\tbreak;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\tif ((x2.x - x1.x) * (x3.y - x1.y) - (x3.x - x1.x) * (x2.y - x1.y) > 0) ans[5]++;\n\t\t\t\telse ans[2]++;\n\t\t\t}\n\t\t}\n\t\tfor (int i = 0; i < 9; i++) printf(\"%d \", ans[i]);\n\t\tprintf(\"%d\\n\", ans[9]);\n\t}\n\treturn 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 7072, "score_of_the_acc": -0.6027, "final_rank": 18 }, { "submission_id": "aoj_1288_9582921", "code_snippet": "#include <map>\n#include <vector>\n#include <iostream>\n#include <deque>\n#include <algorithm>\n\nusing namespace std;\n\nmap<vector<int>, int> signature;\n\nint vec(pair<int, int> a, pair<int, int> b) {\n return a.first * b.second - b.first * a.second;\n}\n\npair<int, int> operator - (pair<int, int> a, pair<int, int> b) {\n return pair<int, int>(a.first - b.first, a.second - b.second);\n}\n\nbool isInTheMidOf(pair<int, int> start, pair<int, int> end, pair<int, int> point) {\n if (point == start || point == end) return false;\n end = end - start;\n point = point - start;\n start = start - start;\n if (end.first >= 0 && !(point.first >= 0 && point.first <= end.first)) return false;\n if (end.first < 0 && !(point.first < 0 && point.first >= end.first)) return false;\n if (end.second >= 0 && !(point.second >= 0 && point.second <= end.second)) return false;\n if (end.second < 0 && !(point.second < 0 && point.second >= end.second)) return false;\n return vec(end, point) == 0;\n}\n\nbool is2(pair<int, int> start, pair<int, int> end, pair<int, int> point) {\n end = end - start;\n point = point - start;\n start = start - start;\n return vec(end, point) < 0;\n}\n\nvector<vector<int> > g;\nvector<bool> used;\ndeque<int> found;\n\nvoid dfs(int v) {\n found.push_back(v);\n used[v] = true;\n for (int i = 0; i < g[v].size(); ++i) {\n int to = g[v][i];\n if (used[to]) continue;\n dfs(to);\n }\n}\n\nint main() {\n static const int tmp0[] = {2, 2, 2, 2};\n vector<int> _sig0 (tmp0, tmp0 + sizeof(tmp0) / sizeof(tmp0[0]));\n static const int tmp1[] = {1, 1};\n vector<int> _sig1 (tmp1, tmp1 + sizeof(tmp1) / sizeof(tmp1[0]));\n static const int tmp3[] = {1, 1, 1, 2, 2, 3};\n vector<int> _sig3 (tmp3, tmp3 + sizeof(tmp3) / sizeof(tmp3[0]));\n static const int tmp4[] = {1, 1, 1, 2, 3};\n vector<int> _sig4 (tmp4, tmp4 + sizeof(tmp4) / sizeof(tmp4[0]));\n static const int tmp6[] = {1, 2, 2, 2, 2, 3};\n vector<int> _sig6 (tmp6, tmp6 + sizeof(tmp6) / sizeof(tmp6[0]));\n static const int tmp7[] = {1, 1, 2, 2};\n vector<int> _sig7 (tmp7, tmp7 + sizeof(tmp7) / sizeof(tmp7[0]));\n static const int tmp8[] = {2, 2, 2, 2, 3, 3};\n vector<int> _sig8 (tmp8, tmp8 + sizeof(tmp8) / sizeof(tmp8[0]));\n static const int tmp9[] = {1, 2, 2, 2, 3};\n vector<int> _sig9 (tmp9, tmp9 + sizeof(tmp9) / sizeof(tmp9[0]));\n signature[_sig0] = 0;\n signature[_sig1] = 1;\n signature[_sig3] = 3;\n signature[_sig4] = 4;\n signature[_sig6] = 6;\n signature[_sig7] = 7;\n signature[_sig8] = 8;\n signature[_sig9] = 9;\n int n;\n while (cin >> n) {\n if (n == 0) break;\n vector<pair<pair<int, int>, pair<int, int> > > segments(n);\n for (int i = 0; i < n; ++i) {\n pair<pair<int, int>, pair<int, int> >& e = segments[i];\n cin >> e.first.first >> e.first.second >> e.second.first >> e.second.second;\n }\n for (int i = 0; i < n; ++i) {\n for (int j = 0; j < n; ++j) {\n if (i == j) continue;\n if (isInTheMidOf(segments[i].first, segments[i].second, segments[j].first)) {\n segments.push_back(make_pair(segments[j].first, segments[i].second));\n segments[i].second = segments[j].first;\n }\n if (isInTheMidOf(segments[i].first, segments[i].second, segments[j].second)) {\n segments.push_back(make_pair(segments[j].second, segments[i].second));\n segments[i].second = segments[j].second;\n }\n }\n }\n vector<pair<int, int> > points(2*segments.size());\n for (int i = 0; i < segments.size(); ++i) {\n points[i*2] = segments[i].first;\n points[i*2+1] = segments[i].second;\n }\n sort(points.begin(), points.end());\n points.resize(unique(points.begin(), points.end()) - points.begin());\n g.assign(points.size(), vector<int>());\n for (int i = 0; i < segments.size(); ++i) {\n pair<int, int>& st = segments[i].first;\n pair<int, int>& en = segments[i].second;\n int a = int(lower_bound(points.begin(), points.end(), st) - points.begin());\n int b = int(lower_bound(points.begin(), points.end(), en) - points.begin());\n g[a].push_back(b);\n g[b].push_back(a);\n }\n used.assign(points.size(), false);\n vector<int> answer(10, 0);\n for (int i = 0; i < points.size(); ++i) {\n if (!used[i]) {\n dfs(i);\n vector<int> power;\n for (int j = 0; j < found.size(); ++j) {\n int& e = found[j];\n power.push_back(int(g[e].size()));\n }\n sort(power.begin(), power.end());\n static const int tmp[] = {1, 1, 2, 2, 2, 2};\n vector<int> _2or5 (tmp, tmp + sizeof(tmp) / sizeof(tmp[0]));\n if (power == _2or5) {\n int edge = -1, second = -1, third = -1;\n for (int j = 0; j < found.size(); ++j) {\n int& e = found[j];\n if (g[e].size() == 1) edge = e;\n }\n second = g[edge].front();\n third = g[second].front() == edge ? g[second].back() : g[second].front();\n if (is2(points[edge], points[second], points[third])) {\n answer[2]++;\n } else {\n answer[5]++;\n }\n } else {\n answer[signature[power]]++;\n }\n found.clear();\n }\n }\n for (int i = 0; i < 9; ++i) cout << answer[i] << ' ';\n cout << answer.back() << '\\n';\n }\n}", "accuracy": 1, "time_ms": 40, "memory_kb": 3252, "score_of_the_acc": -0.0399, "final_rank": 8 }, { "submission_id": "aoj_1288_5035033", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\nconst int N=1000;\nconst int M=30;\n\nstruct Point { int x,y; };\nstruct Line { Point p,q; };\n\nint operator==(const Point &p, const Point &q) {\n return p.x==q.x && p.y==q.y;\n}\n\nint cross(const Point &a, const Point &b, const Point &c) {\n return (a.x-b.x)*(c.y-b.y)-(c.x-b.x)*(a.y-b.y);\n}\n\nint cut(const Line &l, const Point &a) {\n return (l.p.x-a.x)*(a.y-l.q.y)==(l.p.y-a.y)*(a.x-l.q.x) &&\n ((l.p.x-a.x)*(l.q.x-a.x)<0 || (l.p.y-a.y)*(l.q.y-a.y)<0);\n}\n\nint touch(const Line &a, const Line &b) {\n return a.p==b.p || a.p==b.q || a.q==b.p || a.q==b.q ||\n cut(a,b.p) || cut(a,b.q) || cut(b,a.p) || cut(b,a.q);\n}\n\n\nint n,used[N],cnt[10];\nLine line[N];\nint m,g[M][M],d[M];\nPoint p[M];\n\nint addp(Point pt) {\n int i=0;\n while (i<m && !(p[i]==pt)) i++;\n if (i==m) p[m++]=pt;\n return i;\n}\n\nint recognize() {\n // split the lines\n for (int k=0; k<m; k++)\n for (int i=0; i<m; i++)\n for (int j=0; j<m; j++)\n if (i!=j && i!=k && j!=k && g[i][j]) {\n Line l = {p[i],p[j]};\n if (cut(l,p[k])) {\n g[i][j]=g[j][i]=0;\n g[i][k]=g[k][i]=g[j][k]=g[k][j]=1;\n }\n }\n\n int s=0;\n memset(d,0,sizeof(d));\n for (int i=0; i<m; i++)\n for (int j=0; j<m; j++)\n s+=g[i][j],d[i]+=g[i][j];\n s/=2;\n\n // recognize the number by features:\n // n 0 1 2 3 4 5 6 7 8 9\n // #point m 4 2 6 6 5 6 6 4 6 5\n // #line s 4 1 5 5 4 5 6 3 7 5\n if (m==4 && s==4) return 0;\n if (m==2 && s==1) return 1;\n if (m==5 && s==4) return 4;\n if (m==6 && s==6) return 6;\n if (m==4 && s==3) return 7;\n if (m==6 && s==7) return 8;\n if (m==5 && s==5) return 9;\n\n // 3 has a point that connects three lines.\n for (int i=0; i<m; i++)\n if (d[i]==3) return 3;\n\n // check 2 and 5 by line angle.\n for (int i=0; i<m; i++)\n if (d[i]==1) {\n int j=0, k=0;\n while (!g[i][j]) j++;\n while (!g[j][k] || k==i) k++;\n if (cross(p[i],p[j],p[k])>0) return 2; else return 5;\n }\n\n // error\n return 0;\n}\n\nint solve() {\n cin>>n;\n if (n==0) return 0;\n for (int i=0; i<n; i++)\n cin>>line[i].p.x>>line[i].p.y>>line[i].q.x>>line[i].q.y;\n\n\n memset(cnt,0,sizeof(cnt));\n memset(used,0,sizeof(used));\n for (int i=0; i<n; i++)\n if (!used[i]) {\n // BFS: find connected lines\n int cn=0;\n Line c[M];\n c[cn++]=line[i]; used[i]=1;\n for (int k=0; k<cn; k++)\n for (int j=i+1; j<n; j++)\n if (!used[j] && touch(c[k],line[j])) {\n c[cn++]=line[j]; used[j]=1;\n }\n\n // construct graph and recognize it\n m=0;\n memset(g,0,sizeof(g));\n for (int k=0; k<cn; k++) {\n int a=addp(c[k].p), b=addp(c[k].q);\n g[a][b]=g[b][a]=1;\n }\n cnt[recognize()]++;\n }\n\n for (int i=0; i<=9; i++)\n cout<<cnt[i]<<(i<9 ? ' ' : '\\n');\n return 1;\n}\n\nint main() {\n while (solve());\n return 0;\n}", "accuracy": 1, "time_ms": 20, "memory_kb": 3108, "score_of_the_acc": -0.0133, "final_rank": 2 }, { "submission_id": "aoj_1288_4971863", "code_snippet": "#include \"iostream\"\n#include \"climits\"\n#include \"list\"\n#include \"queue\"\n#include \"stack\"\n#include \"set\"\n#include \"functional\"\n#include \"algorithm\"\n#include \"string\"\n#include \"map\"\n#include \"unordered_map\"\n#include \"unordered_set\"\n#include \"iomanip\"\n#include \"cmath\"\n#include \"random\"\n#include \"bitset\"\n#include \"cstdio\"\n#include \"numeric\"\n#include \"cassert\"\n#include \"ctime\"\n#include \"complex\"\n\nusing namespace std;\n\nconstexpr long long int MOD = 1000000007;\n//constexpr int MOD = 1000000007;\n//constexpr int MOD = 998244353;\n//constexpr long long int MOD = 998244353;\nconstexpr double EPS = 1e-12;\n\n//int N, M, K, T, H, W, L, R;\nlong long int N, M, K, T, H, W, L, R;\n\nclass UnionFind {\n\tvector<int>parent;\n\tvector<int>rank;\npublic:\n\tUnionFind(int num) {\n\t\tnum++;\n\t\tparent.resize(num);\n\t\trank.resize(num);\n\t\tfor (int i = 0; i < num; i++) {\n\t\t\tparent[i] = i;\n\t\t\trank[i] = 0;\n\t\t}\n\t}\n\tint Find(int node) {\n\t\tif (parent[node] == node)return node;\n\t\telse return parent[node] = Find(parent[node]);\n\t}\n\tvoid Unite(int u, int v) {\n\t\tu = Find(u);\n\t\tv = Find(v);\n\t\tif (u == v)return;\n\t\tif (rank[u] < rank[v])parent[u] = v;\n\t\telse {\n\t\t\tparent[v] = u;\n\t\t\tif (rank[u] == rank[v])rank[u]++;\n\t\t}\n\t}\n\tbool Check_Same(int u, int v) {\n\t\treturn Find(u) == Find(v);\n\t}\n};\n\nstruct Point {\n\tlong double x;\n\tlong double y;\n\tPoint(long double xx, long double yy) {\n\t\tx = xx, y = yy;\n\t}\n\tPoint() {\n\t}\n\tPoint operator + (const Point& c)const {\n\t\tPoint box;\n\t\tbox.x = x + c.x;\n\t\tbox.y = y + c.y;\n\t\treturn box;\n\t}\n\tbool operator == (const Point& c)const {\n\t\treturn x == c.x&&y == c.y;\n\t}\n\tPoint operator - (const Point& c)const {\n\t\tPoint box;\n\t\tbox.x = x - c.x;\n\t\tbox.y = y - c.y;\n\t\treturn box;\n\t}\n\tPoint operator * (const long double& b)const {\n\t\tPoint box;\n\t\tbox.x = x * b;\n\t\tbox.y = y * b;\n\t\treturn box;\n\t}\n\tbool operator <(const Point& c)const {\n\t\tif (y < c.y)return true;\n\t\tif (y > c.y)return false;\n\t\tif (x < c.x)return true;\n\t\treturn false;\n\t}\n\tbool operator >(const Point& c)const {\n\t\tif (y > c.y)return true;\n\t\tif (y < c.y)return false;\n\t\tif (x > c.x)return true;\n\t\treturn false;\n\t}\n\tlong double det(Point a) {\n\t\treturn x * a.y - y * a.x;\n\t}\n};\n\nstruct Line {\n\tlong double a, b, c;\n\tLine(const int aa, const int bb, const int cc) {\n\t\ta = aa, b = bb, c = cc;\n\t}\n\tLine(const Point s, const Point t) {\n\t\tif (abs(s.x - t.x) < EPS) {\n\t\t\ta = 1;\n\t\t\tb = 0;\n\t\t}\n\t\telse {\n\t\t\tb = 1;\n\t\t\ta = (t.y - s.y) / (s.x - t.x);\n\t\t}\n\t\tc = s.x*a + s.y*b;\n\t}\n};\n\nlong double dot(Point a, Point b) {\n\treturn a.x*b.x + a.y*b.y;\n}\n\nlong double cross(Point a, Point b) {\n\treturn a.x*b.y - b.x*a.y;\n}\n\nlong double Distance(Point a, Point b) {\n\treturn sqrt((a.x - b.x)*(a.x - b.x) + (a.y - b.y)*(a.y - b.y));\n}\n\nlong double Distance(Point a, pair<Point, Point>b) {\n\tLine l = Line(b.first, b.second);\n\tif (abs(l.a*a.x + l.b*a.y - l.c) < EPS) {\n\t\tif (abs(Distance(a, b.first) + Distance(a, b.second) - Distance(b.second, b.first)) <= sqrt(EPS))return 0;\n\t\treturn min(Distance(a, b.first), Distance(a, b.second));\n\t}\n\tlong double A = pow((b.first.x - b.second.x), 2) + pow((b.first.y - b.second.y), 2);\n\tlong double B = 2 * (a.x - b.first.x)*(b.first.x - b.second.x) + 2 * (a.y - b.first.y)*(b.first.y - b.second.y);\n\tlong double r = B / 2 / A;\n\tr = -r;\n\tif (r >= 0 && r <= 1) {\n\t\treturn Distance(a, Point(b.first.x + r * (b.second.x - b.first.x), b.first.y + r * (b.second.y - b.first.y)));\n\t}\n\telse {\n\t\treturn min(Distance(a, b.first), Distance(a, b.second));\n\t}\n}\n\nbool LineCross(Point a1, Point a2, Point b1, Point b2) {\n\treturn cross(a2 - a1, b1 - a1)*cross(a2 - a1, b2 - a1) < EPS&&cross(b2 - b1, a1 - b1)*cross(b2 - b1, a2 - b1) < EPS && !(abs((a1 - a2).x*(a1 - b1).y - (a1 - a2).y*(a1 - b1).x)<EPS && abs((a1 - a2).x*(a1 - b2).y - (a1 - a2).y*(a1 - b2).x)<EPS&&abs(Distance(a1, a2) - Distance(a1, b1) - Distance(a2, b1)) > EPS&&abs(Distance(a1, a2) - Distance(a2, b2) - Distance(a1, b2)) > EPS&&abs(Distance(b1, b2) - Distance(a1, b1) - Distance(a1, b2)) > EPS&&abs(Distance(b1, b2) - Distance(a2, b1) - Distance(a2, b2)) > EPS);\n}\n\nlong double Distance(pair<Point, Point>a, pair<Point, Point>b) {\n\tif (LineCross(a.first, a.second, b.first, b.second))return 0;\n\treturn min({ Distance(a.first,b),Distance(a.second,b),Distance(b.first,a),Distance(b.second,a) });\n}\n\nPoint LineCross(Line a, Line b) {\n\tPoint ret;\n\tret.x = (a.c*b.b - a.b*b.c) / (a.a*b.b - a.b*b.a);\n\tret.y = -(a.c*b.a - a.a*b.c) / (a.a*b.b - a.b*b.a);\n\treturn ret;\n}\n\nlong double TriangleArea(Point a, Point b, Point c) {\n\tlong double A, B, C;\n\tA = Distance(a, b);\n\tB = Distance(a, c);\n\tC = Distance(b, c);\n\tlong double S = (A + B + C) / 2;\n\treturn sqrt(S*(S - A)*(S - B)*(S - C));\n}\n\nbool IsPointLeft(pair<Point, Point>a, Point b) {\n\ta.second = a.second - a.first;\n\tb = b - a.first;\n\ta.first = a.first - a.first;\n\tlong double radline = atan2(a.second.y, a.second.x);\n\tlong double radpoint = atan2(b.y, b.x) - radline;\n\tif (sin(radpoint) >= -EPS)return true;\n\telse return false;\n}\n\nbool IsPointInPolygon(Point p, vector<Point>poly) {\n\tbool ret = true;\n\tfor (int i = 0; i < poly.size(); i++) {\n\t\tif (Distance(p, { poly[i],poly[(i + 1) % poly.size()] }) <= EPS) return true;\n\t\tret &= IsPointLeft({ poly[i],poly[(i + 1) % poly.size()] }, p);\n\t}\n\treturn ret;\n}\n\nlong double Area(vector<Point>p) {\n\tlong double ret = 0;\n\tfor (int i = 2; i < p.size(); i++) {\n\t\tret += TriangleArea(p[0], p[i - 1], p[i]);\n\t}\n\treturn ret;\n}\n\nvector<vector<Point>>DividePolygonByLine(vector<Point>p, pair<Point, Point>l) {\n\tvector<int>cr;\n\tvector<Point>cp;\n\tfor (int k = 0; k < p.size(); k++) {\n\t\tint nx = k + 1;\n\t\tnx %= p.size();\n\t\tif (LineCross(p[k], p[(k + 1) % p.size()], l.first, l.second)) {\n\t\t\tauto np = LineCross(Line(p[k], p[(k + 1) % p.size()]), Line(l.first, l.second));\n\t\t\tbool flag = true;\n\t\t\tfor (auto l : cp) {\n\t\t\t\tif (Distance(l, np) <= EPS)flag = false;\n\t\t\t}\n\t\t\tif (flag) {\n\t\t\t\tcr.push_back(k);\n\t\t\t\tcp.push_back(np);\n\t\t\t}\n\t\t}\n\t}\n\tvector<Point>w;\n\tvector<Point>x;\n\tif (cr.size() != 2)return vector<vector<Point>>(1, p);\n\tint cnt = cr[0];\n\tdo {\n\t\tif (w.empty() || Distance(w.back(), p[cnt]) > EPS)w.push_back(p[cnt]);\n\t\tif (cnt == cr[0]) {\n\t\t\tif (w.empty() || Distance(w.back(), cp[0]) > EPS)w.push_back(cp[0]);\n\t\t\tif (w.empty() || Distance(w.back(), cp[1]) > EPS)w.push_back(cp[1]);\n\t\t\tcnt = cr[1] + 1;\n\t\t\tcnt %= p.size();\n\t\t}\n\t\telse if (cnt == cr[1]) {\n\t\t\tif (w.empty() || Distance(w.back(), cp[1]) > EPS)w.push_back(cp[1]);\n\t\t\tif (w.empty() || Distance(w.back(), cp[0]) > EPS)w.push_back(cp[0]);\n\t\t\tcnt = cr[0] + 1;\n\t\t\tcnt %= p.size();\n\t\t}\n\t\telse {\n\t\t\tcnt++;\n\t\t\tcnt %= p.size();\n\t\t}\n\t} while (cnt != cr[0]);\n\tcnt = cr[1];\n\tdo {\n\t\tif (x.empty() || Distance(x.back(), p[cnt]) > EPS)x.push_back(p[cnt]);\n\t\tif (cnt == cr[0]) {\n\t\t\tif (x.empty() || Distance(x.back(), cp[0]) > EPS)x.push_back(cp[0]);\n\t\t\tif (x.empty() || Distance(x.back(), cp[1]) > EPS)x.push_back(cp[1]);\n\t\t\tcnt = cr[1] + 1;\n\t\t\tcnt %= p.size();\n\t\t}\n\t\telse if (cnt == cr[1]) {\n\t\t\tif (x.empty() || Distance(x.back(), cp[1]) > EPS)x.push_back(cp[1]);\n\t\t\tif (x.empty() || Distance(x.back(), cp[0]) > EPS)x.push_back(cp[0]);\n\t\t\tcnt = cr[0] + 1;\n\t\t\tcnt %= p.size();\n\t\t}\n\t\telse {\n\t\t\tcnt++;\n\t\t\tcnt %= p.size();\n\t\t}\n\t} while (cnt != cr[1]);\n\tvector<vector<Point>>ret;\n\tret.push_back(w);\n\tret.push_back(x);\n\treturn ret;\n}\n\nstruct Circle {\n\tlong double x, y, r;\n\tvoid Input() {\n\t\tcin >> x >> y >> r;\n\t}\n\tbool operator==(const Circle&c)const {\n\t\treturn x == c.x&&y == c.y&&r == c.r;\n\t}\n};\n\nvector<Point>CircleCross(Circle a, Circle b) {\n\tvector<Point>ret;\n\tPoint ap(a.x, a.y);\n\tPoint bp(b.x, b.y);\n\tPoint dp = bp - ap;\n\tauto dis = Distance(ap, bp);\n\tif (a == b)return ret;\n\tif (a.r + b.r < Distance(ap, bp))return ret;\n\tif (abs(a.r + b.r - dis) <= EPS) {\n\t\tret.push_back(ap + dp * (a.r / (a.r + b.r)));\n\t\treturn ret;\n\t}\n\tif (abs(abs(a.r - b.r) - dis) <= EPS) {\n\t\tret.push_back(ap + dp * (a.r / (a.r - b.r)));\n\t}\n\tlong double ad;\n\tad = (a.r*a.r - b.r*b.r + dis * dis) / 2 / dis;\n\tPoint cp = ap + dp * (ad / dis);\n\tlong double amari = sqrt(a.r*a.r - ad * ad);\n\tret.push_back(cp + Point(-dp.y, dp.x)*(amari / dis));\n\tret.push_back(cp - Point(-dp.y, dp.x)*(amari / dis));\n\treturn ret;\n}\n\nvector<pair<Point, Point>>Common_Tangent(Circle a, Circle b, long double inf) {\n\tlong double pi = acos(-1);\n\tPoint ap = Point(a.x, a.y);\n\tPoint bp = Point(b.x, b.y);\n\tPoint dp = bp - ap;\n\tvector<pair<Point, Point>>ret;\n\tlong double rad = atan2(dp.y, dp.x);\n\tlong double d = hypot(dp.y, dp.x);\n\tif (a.r + b.r <= d) {\n\t\tlong double newrad = asin((a.r + b.r) / d);\n\t\t{\n\t\t\tlong double fd = hypot(inf, a.r);\n\t\t\tlong double lrad = atan2(a.r, -inf);\n\t\t\tlong double rrad = atan2(a.r, inf);\n\t\t\tpair<Point, Point>l = { Point(fd*cos(lrad + rad - newrad),fd*sin(lrad + rad - newrad)),Point(fd*cos(rrad + rad - newrad),fd*sin(rrad + rad - newrad)) };\n\t\t\tret.push_back({ l.first + ap,l.second + ap });\n\t\t}\n\t\tnewrad *= -1;\n\t\t{\n\t\t\tlong double fd = hypot(inf, -a.r);\n\t\t\tlong double lrad = atan2(-a.r, -inf);\n\t\t\tlong double rrad = atan2(-a.r, inf);\n\t\t\tpair<Point, Point>l = { Point(fd*cos(lrad + rad - newrad),fd*sin(lrad + rad - newrad)),Point(fd*cos(rrad + rad - newrad),fd*sin(rrad + rad - newrad)) };\n\t\t\tret.push_back({ l.first + ap,l.second + ap });\n\t\t}\n\t}\n\tif (abs(a.r - b.r) <= d) {\n\t\tlong double newrad = asin(abs(a.r - b.r) / d);\n\t\t{\n\t\t\tlong double fd = hypot(inf, a.r);\n\t\t\tlong double lrad = atan2(a.r, -inf);\n\t\t\tlong double rrad = atan2(a.r, inf);\n\t\t\tpair<Point, Point>l = { Point(fd*cos(lrad + rad - newrad),fd*sin(lrad + rad - newrad)),Point(fd*cos(rrad + rad - newrad),fd*sin(rrad + rad - newrad)) };\n\t\t\tret.push_back({ l.first + ap,l.second + ap });\n\t\t}\n\t\tnewrad *= -1;\n\t\t{\n\t\t\tlong double fd = hypot(inf, -a.r);\n\t\t\tlong double lrad = atan2(-a.r, -inf);\n\t\t\tlong double rrad = atan2(-a.r, inf);\n\t\t\tpair<Point, Point>l = { Point(fd*cos(lrad + rad - newrad),fd*sin(lrad + rad - newrad)),Point(fd*cos(rrad + rad - newrad),fd*sin(rrad + rad - newrad)) };\n\t\t\tret.push_back({ l.first + ap,l.second + ap });\n\t\t}\n\t}\n\treturn ret;\n}\n\nstruct Convex_hull {\n\tvector<Point>node;\n\tvector<Point>ret;\n\tbool line;\n\tConvex_hull(bool l) {\n\t\tline = l;\n\t}\n\tvoid Add_node(Point n) {\n\t\tnode.push_back(n);\n\t}\n\tvector<Point>solve() {\n\t\tsort(node.begin(), node.end());\n\t\tint index = 0;\n\t\tint num = node.size();\n\t\tret.resize(num * 2);\n\t\tfor (int i = 0; i < num; i++) {\n\t\t\tif (line) {\n\t\t\t\twhile (index > 1 && (ret[index - 1] - ret[index - 2]).det(node[i] - ret[index - 1]) < -EPS) {\n\t\t\t\t\tindex--;\n\t\t\t\t}\n\t\t\t}\n\t\t\telse {\n\t\t\t\twhile (index > 1 && (ret[index - 1] - ret[index - 2]).det(node[i] - ret[index - 1]) < EPS) {\n\t\t\t\t\tindex--;\n\t\t\t\t}\n\t\t\t}\n\t\t\tret[index++] = node[i];\n\t\t}\n\t\tint box = index;\n\t\tfor (int i = num - 2; i >= 0; i--) {\n\t\t\tif (line) {\n\t\t\t\twhile (index > box && (ret[index - 1] - ret[index - 2]).det(node[i] - ret[index - 1]) < -EPS) {\n\t\t\t\t\tindex--;\n\t\t\t\t}\n\t\t\t}\n\t\t\telse {\n\t\t\t\twhile (index > box && (ret[index - 1] - ret[index - 2]).det(node[i] - ret[index - 1]) < EPS) {\n\t\t\t\t\tindex--;\n\t\t\t\t}\n\t\t\t}\n\t\t\tret[index++] = node[i];\n\t\t}\n\t\tret.resize(index - 1);\n\t\treturn ret;\n\t}\n};\n\nint main() {\n\tios::sync_with_stdio(false);\n\tcin.tie(0);\n\n\twhile (cin >> N, N) {\n\t\tUnionFind uf(N);\n\t\tvector<Point>p(N * 2);\n\t\tfor (auto &i : p)cin >> i.x >> i.y;\n\t\tfor (int i = 0; i < N * 2; i += 2) {\n\t\t\tfor (int j = i + 2; j < N * 2; j += 2) {\n\t\t\t\tif (LineCross(p[i], p[i + 1], p[j], p[j + 1])) {\n\t\t\t\t\tuf.Unite(i / 2, j / 2);\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\tvector<int>ans(10);\n\t\tvector<int>sz(N);\n\t\tvector<int>corner(N);\n\t\tvector<int>not_corner(N);\n\t\tvector<vector<pair<Point, Point>>>vp(N);\n\t\tfor (int i = 0; i < N; i++) {\n\t\t\tsz[uf.Find(i)]++;\n\t\t}\n\t\tfor (int i = 0; i < N * 2; i += 2) {\n\t\t\tvp[uf.Find(i / 2)].push_back({ p[i], p[i + 1] });\n\t\t\tfor (int j = i + 2; j < N * 2; j += 2) {\n\t\t\t\tif (LineCross(p[i], p[i + 1], p[j], p[j + 1])) {\n\t\t\t\t\tif (Distance(p[i], p[j]) <= EPS || Distance(p[i], p[j + 1]) <= EPS || Distance(p[i + 1], p[j]) <= EPS || Distance(p[i + 1], p[j + 1]) <= EPS) {\n\t\t\t\t\t\tcorner[uf.Find(i / 2)]++;\n\t\t\t\t\t}\n\t\t\t\t\telse {\n\t\t\t\t\t\tnot_corner[uf.Find(i / 2)]++;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\tfor (int i = 0; i < N; i++) {\n\t\t\tif (sz[i] == 0)continue;\n\t\t\tif (sz[i] == 1) {\n\t\t\t\tans[1]++;\n\t\t\t\tcontinue;\n\t\t\t}\n\t\t\tif (sz[i] == 3) {\n\t\t\t\tif (corner[i] == 1)ans[4]++;\n\t\t\t\telse ans[7]++;\n\t\t\t\tcontinue;\n\t\t\t}\n\t\t\tif (sz[i] == 4) {\n\t\t\t\tif (corner[i] == 4)ans[0]++;\n\t\t\t\telse if (corner[i] == 2)ans[3]++;\n\t\t\t\telse ans[9]++;\n\t\t\t\tcontinue;\n\t\t\t}\n\t\t\tif (not_corner[i] == 1) {\n\t\t\t\tans[6]++;\n\t\t\t\tcontinue;\n\t\t\t}\n\t\t\tif (not_corner[i] == 2) {\n\t\t\t\tans[8]++;\n\t\t\t\tcontinue;\n\t\t\t}\n\t\t\tvector<vector<int>>edge(vp[i].size(),vector<int>(vp[i].size()));\n\t\t\tfor (int j = 0; j < vp[i].size(); j++) {\n\t\t\t\tint num = 0;\n\t\t\t\tint idx = 0;\n\t\t\t\tfor (int k = 0; k < vp[i].size(); k++) {\n\t\t\t\t\tif (j == k)continue;\n\t\t\t\t\tif (LineCross(vp[i][j].first, vp[i][j].second, vp[i][k].first, vp[i][k].second)) {\n\t\t\t\t\t\tedge[j][k] = 1;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t\tfor (int j = 0; j < vp[i].size(); j++) {\n\t\t\t\tif (accumulate(edge[j].begin(), edge[j].end(), 0) != 1)continue;\n\t\t\t\tint a = 0;\n\t\t\t\tfor (int k = 0; k < edge[j].size(); k++) {\n\t\t\t\t\tif (edge[j][k])a = k;\n\t\t\t\t}\n\t\t\t\tint b = 0;\n\t\t\t\tfor (int k = 0; k < edge[a].size(); k++) {\n\t\t\t\t\tif (edge[a][k] && k != j)b = k;\n\t\t\t\t}\n\t\t\t\tvector<Point>box;\n\t\t\t\tif (vp[i][a].first == vp[i][b].first) {\n\t\t\t\t\tbox.push_back(vp[i][a].second);\n\t\t\t\t\tbox.push_back(vp[i][a].first);\n\t\t\t\t\tbox.push_back(vp[i][b].second);\n\t\t\t\t}\n\t\t\t\telse if (vp[i][a].first == vp[i][b].second) {\n\t\t\t\t\tbox.push_back(vp[i][a].second);\n\t\t\t\t\tbox.push_back(vp[i][a].first);\n\t\t\t\t\tbox.push_back(vp[i][b].first);\n\t\t\t\t}\n\t\t\t\telse if (vp[i][a].second == vp[i][b].first) {\n\t\t\t\t\tbox.push_back(vp[i][a].first);\n\t\t\t\t\tbox.push_back(vp[i][a].second);\n\t\t\t\t\tbox.push_back(vp[i][b].second);\n\t\t\t\t}\n\t\t\t\telse {\n\t\t\t\t\tbox.push_back(vp[i][a].first);\n\t\t\t\t\tbox.push_back(vp[i][a].second);\n\t\t\t\t\tbox.push_back(vp[i][b].first);\n\t\t\t\t}\n\t\t\t\tlong double arad = atan2l(box[1].y - box[0].y, box[1].x - box[0].x);\n\t\t\t\tlong double brad = atan2l(box[2].y - box[1].y, box[2].x - box[1].x);\n\t\t\t\tbrad -= arad;\n\t\t\t\tlong double pi = acosl(-1);\n\t\t\t\tif (brad > pi)brad -= pi * 2;\n\t\t\t\tif (brad < -pi)brad += pi * 2;\n\t\t\t\tif (brad < 0)ans[2]++;\n\t\t\t\telse ans[5]++;\n\t\t\t\tbreak;\n\t\t\t}\n\t\t}\n\t\tfor (int i = 0; i < 10; i++) {\n\t\t\tif (i)cout << \" \";\n\t\t\tcout << ans[i];\n\t\t}\n\t\tcout << endl;\n\t}\n}", "accuracy": 1, "time_ms": 50, "memory_kb": 3644, "score_of_the_acc": -0.101, "final_rank": 14 }, { "submission_id": "aoj_1288_4962089", "code_snippet": "#line 1 \"a.cpp\"\n#include<iostream>\n#include<algorithm>\n#include<vector>\nusing namespace std;\n#line 2 \"/home/kotatsugame/library/datastructure/UF.cpp\"\nstruct UF{\n\tint n;\n\tvector<int>parent,rank;\n\tUF(int n_=0):n(n_),parent(n_),rank(n_,1)\n\t{\n\t\tfor(int i=0;i<n_;i++)parent[i]=i;\n\t}\n\tint find(int a){return parent[a]!=a?parent[a]=find(parent[a]):a;}\n\tbool same(int a,int b){return find(a)==find(b);}\n\tbool unite(int a,int b)\n\t{\n\t\ta=find(a),b=find(b);\n\t\tif(a==b)return false;\n\t\tif(rank[a]<rank[b])\n\t\t{\n\t\t\tparent[a]=b;\n\t\t\trank[b]+=rank[a];\n\t\t}\n\t\telse\n\t\t{\n\t\t\tparent[b]=a;\n\t\t\trank[a]+=rank[b];\n\t\t}\n\t\treturn true;\n\t}\n\tint size(int a){return rank[find(a)];}\n};\n#line 6 \"a.cpp\"\nint N;\nint X[1000][2],Y[1000][2];\nbool touch(int i,int ik,int j,int jk)\n{\n\treturn X[i][ik]==X[j][jk]&&Y[i][ik]==Y[j][jk];\n}\nbool touch(int i,int j)\n{\n\treturn touch(i,0,j,0)||touch(i,0,j,1)||touch(i,1,j,0)||touch(i,1,j,1);\n}\nbool meet(int x,int y,int id)\n{\n\tint a=X[id][0]-x,b=Y[id][0]-y;\n\tint c=X[id][1]-x,d=Y[id][1]-y;\n\treturn a*d==b*c&&a*c+b*d<0;\n}\nmain()\n{\n\twhile(cin>>N,N)\n\t{\n\t\tfor(int i=0;i<N;i++)for(int j=0;j<2;j++)cin>>X[i][j]>>Y[i][j];\n\t\tUF uf(N);\n\t\tfor(int i=0;i<N;i++)for(int k=0;k<2;k++)for(int j=0;j<N;j++)if(i!=j)\n\t\t{\n\t\t\tif(touch(i,j)||meet(X[i][k],Y[i][k],j))uf.unite(i,j);\n\t\t}\n\t\tvector<vector<int> >ids(N);\n\t\tfor(int i=0;i<N;i++)ids[uf.find(i)].push_back(i);\n\t\tint ans[10]={};\n\t\tfor(vector<int>id:ids)if(!id.empty())\n\t\t{\n\t\t\tvector<int>ok;\n\t\t\tbool middle=false;\n\t\t\tfor(int i:id)\n\t\t\t{\n\t\t\t\tbool now=false;\n\t\t\t\tfor(int j:id)if(i!=j)\n\t\t\t\t{\n\t\t\t\t\tif(touch(i,j))now=true;\n\t\t\t\t\telse\n\t\t\t\t\t{\n\t\t\t\t\t\tif(meet(X[i][0],Y[i][0],j)||meet(X[i][1],Y[i][1],j))middle=true;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\tif(now)ok.push_back(i);\n\t\t\t}\n\t\t\tif(middle)\n\t\t\t{\n\t\t\t\tif(ok.size()==2)ans[4]++;\n\t\t\t\telse if(ok.size()==3)ans[3]++;\n\t\t\t\telse if(ok.size()==5)ans[6]++;\n\t\t\t\telse\n\t\t\t\t{\n\t\t\t\t\tvector<int>dist;\n\t\t\t\t\tfor(int i:ok)\n\t\t\t\t\t{\n\t\t\t\t\t\tint dx=X[i][0]-X[i][1],dy=Y[i][0]-Y[i][1];\n\t\t\t\t\t\tdist.push_back(dx*dx+dy*dy);\n\t\t\t\t\t}\n\t\t\t\t\tsort(dist.begin(),dist.end());\n\t\t\t\t\tif(dist[0]==dist[1]&&dist[2]==dist[3])ans[8]++;\n\t\t\t\t\telse ans[9]++;\n\t\t\t\t}\n\t\t\t}\n\t\t\telse\n\t\t\t{\n\t\t\t\tif(ok.size()==0)ans[1]++;\n\t\t\t\telse if(ok.size()==3)ans[7]++;\n\t\t\t\telse if(ok.size()==4)ans[0]++;\n\t\t\t\telse\n\t\t\t\t{\n\t\t\t\t\tint s;\n\t\t\t\t\tfor(int i:ok)\n\t\t\t\t\t{\n\t\t\t\t\t\tint cnt=0;\n\t\t\t\t\t\tfor(int j:ok)if(i!=j&&touch(i,j))cnt++;\n\t\t\t\t\t\tif(cnt==1)\n\t\t\t\t\t\t{\n\t\t\t\t\t\t\ts=i;\n\t\t\t\t\t\t\tbreak;\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t\tint g;\n\t\t\t\t\tfor(int i:ok)if(i!=s&&touch(s,i))\n\t\t\t\t\t{\n\t\t\t\t\t\tg=i;\n\t\t\t\t\t\tbreak;\n\t\t\t\t\t}\n\t\t\t\t\tint sx,sy,gx,gy;\n\t\t\t\t\tif(touch(s,0,g,0))\n\t\t\t\t\t{\n\t\t\t\t\t\tsx=X[s][1]-X[s][0];\n\t\t\t\t\t\tsy=Y[s][1]-Y[s][0];\n\t\t\t\t\t\tgx=X[g][1]-X[g][0];\n\t\t\t\t\t\tgy=Y[g][1]-Y[g][0];\n\t\t\t\t\t}\n\t\t\t\t\telse if(touch(s,0,g,1))\n\t\t\t\t\t{\n\t\t\t\t\t\tsx=X[s][1]-X[s][0];\n\t\t\t\t\t\tsy=Y[s][1]-Y[s][0];\n\t\t\t\t\t\tgx=X[g][0]-X[g][1];\n\t\t\t\t\t\tgy=Y[g][0]-Y[g][1];\n\t\t\t\t\t}\n\t\t\t\t\telse if(touch(s,1,g,0))\n\t\t\t\t\t{\n\t\t\t\t\t\tsx=X[s][0]-X[s][1];\n\t\t\t\t\t\tsy=Y[s][0]-Y[s][1];\n\t\t\t\t\t\tgx=X[g][1]-X[g][0];\n\t\t\t\t\t\tgy=Y[g][1]-Y[g][0];\n\t\t\t\t\t}\n\t\t\t\t\telse\n\t\t\t\t\t{\n\t\t\t\t\t\tsx=X[s][0]-X[s][1];\n\t\t\t\t\t\tsy=Y[s][0]-Y[s][1];\n\t\t\t\t\t\tgx=X[g][0]-X[g][1];\n\t\t\t\t\t\tgy=Y[g][0]-Y[g][1];\n\t\t\t\t\t}\n\t\t\t\t\tif(sx*gy-sy*gx>0)ans[2]++;\n\t\t\t\t\telse ans[5]++;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\tfor(int i=0;i<10;i++)cout<<ans[i]<<(i==9?\"\\n\":\" \");\n\t}\n}", "accuracy": 1, "time_ms": 20, "memory_kb": 3220, "score_of_the_acc": -0.03, "final_rank": 5 }, { "submission_id": "aoj_1288_4930893", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\ntypedef long long ll;\ntemplate<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return true; } return false; }\ntemplate<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return true; } return false; }\n#define all(x) (x).begin(),(x).end()\n#define fi first\n#define se second\n#define mp make_pair\n#define si(x) int(x.size())\nconst int mod=1000000007,MAX=1005;\nconst int INF=1e6;\n//const ll INF=1LL<<60;\n\n//幾何ライブラリ\n\nconst double eps=1e-8;\nconst double pi=acos((long double)-1.0L);\n#define equals(a,b) (fabs((a)-(b))<eps)\n\ndouble torad(int deg) {return (double)(deg)*pi/180.0;}\ndouble todeg(double ang) {return ang*180.0/pi;}\n\nclass Point{\npublic:\n double x,y;\n \n Point(double x=0,double y=0):x(x),y(y){}\n \n Point operator + (Point p){return Point(x+p.x,y+p.y);}\n Point operator - (Point p){return Point(x-p.x,y-p.y);}\n Point operator * (double a){return Point(a*x,a*y);}\n Point operator / (double a){return Point(x/a,y/a);}\n \n double abs(){return sqrt(norm());}\n double norm(){return x*x+y*y;}\n \n bool operator < (const Point &p)const{\n return x+eps<p.x||(equals(x,p.x)&&y+eps<p.y);\n }\n \n bool operator == (const Point &p)const{\n return fabs(x-p.x)<eps&&fabs(y-p.y)<eps;\n }\n};\n\ntypedef Point Vector;\n\ndouble norm(Vector a){\n return a.x*a.x+a.y*a.y;\n}\n\ndouble abs(Vector a){\n return sqrt(norm(a));\n}\n\ndouble dot(Vector a,Vector b){\n return a.x*b.x+a.y*b.y;\n}\n\ndouble cross(Vector a,Vector b){\n return a.x*b.y-a.y*b.x;\n}\n\nstruct Segment{\n Point p1,p2;\n};\n\nbool isOrthogonal(Vector a,Vector b){\n return equals(dot(a,b),0.0);\n}\n\nbool isOrthogonal(Point a1,Point a2,Point b1,Point b2){\n return isOrthogonal(a1-a2,b1-b2);\n}\n\nbool isOrthogonal(Segment s1,Segment s2){\n return equals(dot(s1.p2-s1.p1,s2.p2-s2.p1),0.0);\n}\n\nbool isParallel(Vector a,Vector b){\n return equals(cross(a,b),0.0);\n}\n\nbool isParallel(Point a1,Point a2,Point b1,Point b2){\n return isParallel(a1-a2,b1-b2);\n}\n\nbool isParallel(Segment s1,Segment s2){\n return equals(cross(s1.p2-s1.p1,s2.p2-s2.p1),0.0);\n}\n\nPoint project(Segment s,Point p){\n Vector base=s.p2-s.p1;\n double r=dot(p-s.p1,base)/norm(base);\n return s.p1+base*r;\n}\n\nPoint reflect(Segment s,Point p){\n return p+(project(s,p)-p)*2.0;\n}\n\nPoint turn(Point p,Point c,double pi){\n double q=atan2(p.y-c.y,p.x-c.x);\n q+=pi;\n p=c+Point{cos(q)*abs(p-c),sin(q)*abs(p-c)};\n \n return p;\n}\n//pをcを中心としてpi回転させる(1周で2π)\n//p=cのときnan\n\nstatic const int counter_clockwise=1;\nstatic const int clockwise=-1;\nstatic const int online_back=2;\nstatic const int online_front=-2;\nstatic const int on_segment=0;\n\nint ccw(Point p0,Point p1,Point p2){\n Vector a=p1-p0;\n Vector b=p2-p0;\n \n if(cross(a,b)>eps) return counter_clockwise;\n if(cross(a,b)<-eps) return clockwise;\n if(dot(a,b)<-eps) return online_back;\n if(a.norm()<b.norm()) return online_front;\n \n return on_segment;\n}\n\nbool intersect(Point p1,Point p2,Point p3,Point p4){\n return(ccw(p1,p2,p3)*ccw(p1,p2,p4)<=0&&ccw(p3,p4,p1)*ccw(p3,p4,p2)<=0);\n}\n\nbool intersect(Segment s1,Segment s2){\n return intersect(s1.p1,s1.p2,s2.p1,s2.p2);\n}\n\nbool overlap(Segment s1,Segment s2){\n int a=ccw(s1.p1,s1.p2,s2.p1),b=ccw(s1.p1,s1.p2,s2.p2);\n if(a&1||b&1) return 0;\n if(a==2){\n if(b==-2||(b==0&&!(s2.p2==s1.p1))) return 1;\n else return 0;\n }\n if(a==-2){\n if(b==2||(b==0&&!(s2.p2==s1.p2))) return 1;\n else return 0;\n }\n if(a==0){\n if(s1.p1==s2.p1){\n if(b!=2) return 1;\n else return 0;\n }\n else if(s1.p2==s2.p1){\n if(b!=-2) return 1;\n else return 0;\n }\n else return 1;\n }\n return 0;\n}\n//s1とs2の共通の線分(長さ0より大きい)があるかどうか\n\ntypedef Segment Line;\n\ndouble getDistance(Point a,Point b){\n return abs(a-b);\n}\n\ndouble getDistanceLP(Line l,Point p){\n return abs(cross(l.p2-l.p1,p-l.p1)/abs(l.p2-l.p1));\n}\n\ndouble getDistanceSP(Segment s,Point p){\n if(dot(s.p2-s.p1,p-s.p1)<0.0) return abs(p-s.p1);\n if(dot(s.p1-s.p2,p-s.p2)<0.0) return abs(p-s.p2);\n return getDistanceLP(s,p);\n}\n\ndouble getDistance(Segment s1,Segment s2){\n if(intersect(s1,s2)) return 0.0;\n return min({getDistanceSP(s1,s2.p1),getDistanceSP(s1,s2.p2),getDistanceSP(s2,s1.p1),getDistanceSP(s2,s1.p2)});\n}\n\nPoint getCrossPointS(Segment s1,Segment s2){\n //if(ccw(s1.p1,s1.p2,s2.p1)==0&&ccw(s1.p1,s1.p2,s2.p2)==0) return s1.p1;\n Vector base=s2.p2-s2.p1;\n double d1=abs(cross(base,s1.p1-s2.p1));\n double d2=abs(cross(base,s1.p2-s2.p1));\n double t=d1/(d1+d2);\n return s1.p1+(s1.p2-s1.p1)*t;\n}//同じ時壊れます\n\nPoint getCrossPointL(Line l1,Line l2){\n //if(ccw(s1.p1,s1.p2,s2.p1)==0&&ccw(s1.p1,s1.p2,s2.p2)==0) return s1.p1;\n \n Vector v1=l1.p2-l1.p1,v2=l2.p2-l2.p1;\n \n return l1.p1+v1*cross(v2,l2.p1-l1.p1)/cross(v2,v1);\n}\n\nSegment ParallelSegment(Segment s,double d){\n Vector v={-(s.p2-s.p1).y,(s.p2-s.p1).x};\n v=v/abs(v);\n \n s.p1=s.p1+v*d;\n s.p2=s.p2+v*d;\n \n return s;\n}\n\nclass Circle{\npublic:\n Point c;\n double r;\n Circle(Point c=Point(),double r=0.0):c(c),r(r){}\n};\n\nPoint CircleCenter(Point a,Point b,Point c){\n Point u=a-b,v=a-c;\n double m1=(norm(a)-norm(b))/2.0,m2=(norm(a)-norm(c))/2.0;\n \n Point res;\n if(cross(u,v)==0.0){\n res.x=1e9;\n res.y=1e9;\n \n return res;\n }\n res.x=(m1*v.y-m2*u.y)/cross(u,v);\n res.y=(m1*v.x-m2*u.x)/cross(v,u);\n \n return res;\n}\n//3点を通る円の中心を返す\n\npair<Point,Point> segCrossPpoints(Circle c,Line l){\n //assert(intersect(c,l));\n Vector pr=project(l,c.c);\n Vector e=(l.p2-l.p1)/abs(l.p2-l.p1);\n double base=sqrt(c.r*c.r-norm(pr-c.c));\n return make_pair(pr+e*base,pr-e*base);\n}\n\ndouble arg(Vector p){return atan2(p.y,p.x);}\nVector polar(double a,double r){return Point(cos(r)*a,sin(r)*a);}\n\npair<Point,Point> getCrossPoints(Circle c1,Circle c2){\n //assert(intersect(c1,c2));\n double d=abs(c1.c-c2.c);\n double a=acos((c1.r*c1.r+d*d-c2.r*c2.r)/(2*c1.r*d));\n double t=arg(c2.c-c1.c);\n return make_pair(c1.c+polar(c1.r,t+a),c1.c+polar(c1.r,t-a));\n}\n\nvector<Line> Commontangent(Circle c1,Circle c2){\n vector<Line> res;\n Point p=c2.c-c1.c;\n \n if(abs(p)>=(c1.r+c2.r)){\n Point a,b;\n a.x=c1.r*(p.x*(c1.r+c2.r)+p.y*sqrt(norm(p)-(c1.r+c2.r)*(c1.r+c2.r)))/norm(p);\n a.y=c1.r*(p.y*(c1.r+c2.r)-p.x*sqrt(norm(p)-(c1.r+c2.r)*(c1.r+c2.r)))/norm(p);\n \n b.x=c1.r*(p.x*(c1.r+c2.r)-p.y*sqrt(norm(p)-(c1.r+c2.r)*(c1.r+c2.r)))/norm(p);\n b.y=c1.r*(p.y*(c1.r+c2.r)+p.x*sqrt(norm(p)-(c1.r+c2.r)*(c1.r+c2.r)))/norm(p);\n \n res.push_back(Line{a+c1.c,a+c1.c+Point{-a.y,a.x}});\n if(!(a==b)){\n res.push_back(Line{b+c1.c,b+c1.c+Point{-b.y,b.x}});\n }\n }\n \n if(abs(p)>=abs(c1.r-c2.r)){\n Point a,b;\n a.x=c1.r*(p.x*(c1.r-c2.r)+p.y*sqrt(norm(p)-(c1.r-c2.r)*(c1.r-c2.r)))/norm(p);\n a.y=c1.r*(p.y*(c1.r-c2.r)-p.x*sqrt(norm(p)-(c1.r-c2.r)*(c1.r-c2.r)))/norm(p);\n \n b.x=c1.r*(p.x*(c1.r-c2.r)-p.y*sqrt(norm(p)-(c1.r-c2.r)*(c1.r-c2.r)))/norm(p);\n b.y=c1.r*(p.y*(c1.r-c2.r)+p.x*sqrt(norm(p)-(c1.r-c2.r)*(c1.r-c2.r)))/norm(p);\n \n res.push_back(Line{a+c1.c,a+c1.c+Point{-a.y,a.x}});\n if(!(a==b)){\n res.push_back(Line{b+c1.c,b+c1.c+Point{-b.y,b.x}});\n }\n }\n \n return res;\n}\n\ntypedef vector<Point> Polygon;\n\n/*\n IN 2\n ON 1\n OUT 0\n */\n\nint contains(Polygon g,Point p){\n int n=int(g.size());\n bool x=false;\n for(int i=0;i<n;i++){\n Point a=g[i]-p,b=g[(i+1)%n]-p;\n if(a.y>b.y) swap(a,b);\n if(a.y<eps&&eps<b.y&&cross(a,b)>eps) x=!x;\n }\n return (x?2:0);\n}\n\nPolygon andrewScan(Polygon s,bool ok){\n Polygon u,l;\n sort(all(s));\n \n if(int(s.size())<3) return s;\n int n=int(s.size());\n \n u.push_back(s[0]);\n u.push_back(s[1]);\n \n l.push_back(s[n-1]);\n l.push_back(s[n-2]);\n \n if(ok){\n for(int i=2;i<n;i++){\n for(int j=int(u.size());j>=2&&ccw(u[j-2],u[j-1],s[i])==counter_clockwise;j--){\n u.pop_back();\n }\n u.push_back(s[i]);\n }\n \n for(int i=int(s.size())-3;i>=0;i--){\n for(int j=int(l.size());j>=2&&ccw(l[j-2],l[j-1],s[i])==counter_clockwise;j--){\n l.pop_back();\n }\n l.push_back(s[i]);\n }\n }\n \n if(!ok){\n for(int i=2;i<n;i++){\n for(int j=int(u.size());j>=2&&ccw(u[j-2],u[j-1],s[i])!=clockwise;j--){\n u.pop_back();\n }\n u.push_back(s[i]);\n }\n \n for(int i=int(s.size())-3;i>=0;i--){\n for(int j=int(l.size());j>=2&&ccw(l[j-2],l[j-1],s[i])!=clockwise;j--){\n l.pop_back();\n }\n l.push_back(s[i]);\n }\n }\n \n reverse(all(l));\n \n for(int i=int(u.size())-2;i>=1;i--) l.push_back(u[i]);\n \n return l;\n}//ok==1なら辺の上も含める\n\nPolygon convex_cut(const Polygon& P, const Line& l) {\n Polygon Q;\n for(int i=0;i<si(P);i++){\n Point A=P[i],B=P[(i+1)%si(P)];\n if(ccw(l.p1,l.p2,A)!=-1)Q.push_back(A);\n if(ccw(l.p1,l.p2,A)*ccw(l.p1,l.p2,B)<0) Q.push_back(getCrossPointL(Line{A,B},l));\n }\n return Q;\n}\n\ndouble area(Point a,Point b,Point c){\n b=b-a;\n c=c-a;\n return abs(b.x*c.y-b.y*c.x)/2.0;\n}\n\ndouble area(Polygon &P){\n if(si(P)==0) return 0.0;\n double res=0;\n Point c={0.0,0.0};\n for(int i=0;i<si(P);i++){\n c=c+P[i];\n }\n c=c/si(P);\n \n for(int i=0;i<si(P);i++){\n res+=area(c,P[i],P[(i+1)%si(P)]);\n }\n \n return res;\n}\n\nstruct UF{\n int n;\n vector<int> par,size,cnt;\n vector<vector<int>> ver;\n \n void init(int n_){\n n=n_;\n par.assign(n,-1);\n size.assign(n,1);\n cnt.assign(n,0);\n ver.assign(n,vector<int>());\n \n for(int i=0;i<n;i++){\n par[i]=i;\n ver[i].push_back(i);\n }\n }\n \n int root(int a){\n if(par[a]==a) return a;\n else return par[a]=root(par[a]);\n }\n \n void unite(int a,int b){\n if(root(a)!=root(b)){\n size[root(a)]+=size[root(b)];\n cnt[root(a)]+=cnt[root(b)];\n for(int x:ver[root(b)]) ver[root(a)].push_back(x);\n par[root(b)]=root(a);\n }\n }\n \n bool check(int a,int b){\n return root(a)==root(b);\n }\n};\n\nint main(){\n \n std::ifstream in(\"text.txt\");\n std::cin.rdbuf(in.rdbuf());\n cin.tie(0);\n ios::sync_with_stdio(false);\n \n while(1){\n int N;cin>>N;\n if(N==0) break;\n \n UF uf;\n uf.init(N);\n vector<int> ans(10);\n \n vector<Segment> L(N);\n for(int i=0;i<N;i++){\n cin>>L[i].p1.x>>L[i].p1.y>>L[i].p2.x>>L[i].p2.y;\n }\n \n for(int i=0;i<N;i++){\n for(int j=i+1;j<N;j++){\n if(intersect(L[i],L[j])){\n uf.unite(i,j);\n if(L[i].p1==L[j].p1) uf.cnt[uf.root(i)]++;\n if(L[i].p1==L[j].p2) uf.cnt[uf.root(i)]++;\n if(L[i].p2==L[j].p1) uf.cnt[uf.root(i)]++;\n if(L[i].p2==L[j].p2) uf.cnt[uf.root(i)]++;\n }\n }\n }\n \n for(int i=0;i<N;i++){\n if(uf.root(i)==i){\n if(uf.size[i]==1) ans[1]++;\n if(uf.size[i]==3){\n if(uf.cnt[i]==1) ans[4]++;\n if(uf.cnt[i]==2) ans[7]++;\n }\n if(uf.size[i]==4){\n if(uf.cnt[i]==4) ans[0]++;\n if(uf.cnt[i]==2) ans[3]++;\n if(uf.cnt[i]==3) ans[9]++;\n }\n if(uf.size[i]==5){\n int mu=0,aida=0;\n for(int a:uf.ver[i]){\n int cnt=0;\n for(int b:uf.ver[i]){\n if(a==b) continue;\n if(intersect(L[a],L[b])){\n if(L[a].p1==L[b].p1) cnt++;\n else if(L[a].p1==L[b].p2) cnt++;\n else if(L[a].p2==L[b].p1) cnt++;\n else if(L[a].p2==L[b].p2) cnt++;\n else aida++;\n }\n }\n if(cnt==0) mu++;\n }\n if(mu) ans[8]++;\n else if(aida) ans[6]++;\n else{\n int x=0;\n for(int a:uf.ver[i]){\n bool c=0,d=0;\n for(int b:uf.ver[i]){\n if(a==b) continue;\n if(L[a].p1==L[b].p1) c=1;\n else if(L[a].p1==L[b].p2) c=1;\n else if(L[a].p2==L[b].p1) d=1;\n else if(L[a].p2==L[b].p2) d=1;\n }\n if(!c){\n for(int b:uf.ver[i]){\n if(a==b) continue;\n if(L[a].p2==L[b].p1){\n int w=ccw(L[a].p1,L[a].p2,L[b].p2);\n if(w==1) x++;\n }\n if(L[a].p2==L[b].p2){\n int w=ccw(L[a].p1,L[a].p2,L[b].p1);\n if(w==1) x++;\n }\n }\n }else if(!d){\n for(int b:uf.ver[i]){\n if(a==b) continue;\n if(L[a].p1==L[b].p1){\n int w=ccw(L[a].p2,L[a].p1,L[b].p2);\n if(w==1) x++;\n }\n if(L[a].p1==L[b].p2){\n int w=ccw(L[a].p2,L[a].p1,L[b].p1);\n if(w==1) x++;\n }\n }\n }\n }\n \n if(x) ans[5]++;\n else ans[2]++;\n }\n }\n }\n }\n \n for(int i=0;i<10;i++){\n if(i) cout<<\" \";\n cout<<ans[i];\n }\n cout<<endl;\n }\n}", "accuracy": 1, "time_ms": 20, "memory_kb": 3592, "score_of_the_acc": -0.0856, "final_rank": 13 }, { "submission_id": "aoj_1288_3708828", "code_snippet": "#include<bits/stdc++.h>\ntypedef long long int ll;\ntypedef unsigned long long int ull;\n#define BIG_NUM 2000000000\n#define HUGE_NUM 99999999999999999\n#define MOD 1000000007\n#define EPS 0.000000001\nusing namespace std;\n\n\n#define NUM 1005\n\nstruct Point{\n\tPoint(double arg_x,double arg_y){\n\t\tx = arg_x;\n\t\ty = arg_y;\n\t}\n\n\tPoint(){\n\t\tx = y = 0.0;\n\t}\n\n\tPoint operator + (Point p){ return Point(x+p.x,y+p.y); }\n\tPoint operator - (Point p){ return Point(x-p.x,y-p.y);}\n\tPoint operator * (double a){ return Point(a*x,a*y); }\n\tPoint operator / (double a){ return Point(x/a,y/a); }\n\n\tdouble abs(){ return sqrt(norm()); }\n\tdouble norm(){ return x*x + y*y; }\n\n\tbool operator<(const Point &p) const{\n\t\treturn x != p.x? x < p.x: y < p.y;\n\t}\n\n\tbool operator == (const Point &p) const{\n\t\treturn fabs(x-p.x) < EPS && fabs(y-p.y) < EPS;\n\t}\n\n\tdouble x,y;\n};\n\nstruct Line{\npublic:\n\tPoint p[2];\n\tLine(Point p1,Point p2){\n\t\tp[0] = p1;\n\t\tp[1] = p2;\n\t}\n};\n\ntypedef Point Vector;\n\n\nint N;\nint boss[NUM],height[NUM];\nint ANS[11];\nvector<Line> LINE,GROUP[NUM];\n\nint func(double x1,double y1,double x2, double y2, double xp, double yp){\n\tdouble naiseki,norm1,norm2,gaiseki;\n\tnorm1 = sqrt((x2-x1)*(x2-x1)+(y2-y1)*(y2-y1));\n\tnorm2 = sqrt((xp-x1)*(xp-x1)+(yp-y1)*(yp-y1));\n\tnaiseki = (xp-x1)*(x2-x1)+(yp-y1)*(y2-y1);\n\tgaiseki = (x2-x1)*(yp-y1)-(xp-x1)*(y2-y1);\n\tif(gaiseki > EPS){\n\t\treturn 1;\n\t}else if(gaiseki < -EPS){\n\t\treturn -1;\n\t}\n\tif(naiseki < -EPS){\n\t\treturn 2;\n\t}\n\n\tif(norm1 < norm2){\n\t\treturn -2;\n\t}\n\treturn 0;\n}\n\nbool check_Cross(Line a,Line b){\n\n\tif(func(a.p[0].x,a.p[0].y,a.p[1].x,a.p[1].y,b.p[0].x,b.p[0].y)*\n\t\t\tfunc(a.p[0].x,a.p[0].y,a.p[1].x,a.p[1].y,b.p[1].x,b.p[1].y) <= 0 &&\n\t\t\tfunc(b.p[0].x,b.p[0].y,b.p[1].x,b.p[1].y,a.p[0].x,a.p[0].y)*\n\t\t\tfunc(b.p[0].x,b.p[0].y,b.p[1].x,b.p[1].y,a.p[1].x,a.p[1].y) <= 0){\n\t\treturn true;\n\t}\n\treturn false;\n}\n\ndouble norm(Vector a){\n\treturn a.x*a.x+a.y*a.y;\n}\n\ndouble abs(Vector a){\n\treturn sqrt(norm(a));\n}\n\ndouble abs(Line a){\n\n\treturn sqrt((a.p[0].x-a.p[1].x)*(a.p[0].x-a.p[1].x)+(a.p[0].y-a.p[1].y)*(a.p[0].y-a.p[1].y));\n}\n\ndouble cross(Vector a,Vector b){\n return a.x*b.y-a.y*b.x;\n}\n\ndouble dot(Vector a,Vector b){\n return a.x*b.x + a.y*b.y;\n}\n\n//点と直線の距離\ndouble calc_dist1(Line line,Point point){\n return abs(cross(line.p[1]-line.p[0],point-line.p[0]))/abs(line.p[1]-line.p[0]);\n}\n\ndouble calc_slope(Line A){\n\n\tif(fabs(A.p[0].x-A.p[1].x) < EPS){\n\n\t\treturn DBL_MAX;\n\n\t}else if(fabs(A.p[0].y-A.p[1].y) < EPS){\n\n\t\treturn 0;\n\n\t}else{\n\n\t\treturn (A.p[0].y-A.p[1].y)/(A.p[0].x-A.p[1].x);\n\t}\n}\n\nPoint calc_Cross_Point(double x1,double x2,double x3,double x4,double y1,double y2,double y3,double y4){\n\tPoint ret;\n\tret.x = ((x2-x1)*(y3*(x4-x3)+x3*(y3-y4))-(x4-x3)*(y1*(x2-x1)+x1*(y1-y2)))/((y2-y1)*(x4-x3)-(y4-y3)*(x2-x1));\n\tif(x1 != x2){\n\t\tret.y = ((y2-y1)*ret.x+y1*(x2-x1)+x1*(y1-y2))/(x2-x1);\n\t}else{\n\t\tret.y = ((y4-y3)*ret.x+y3*(x4-x3)+x3*(y3-y4))/(x4-x3);\n\t}\n\treturn ret;\n}\n\n//インタフェース関数\nPoint calc_Cross_Point(Point a,Point b,Point c,Point d){\n\treturn calc_Cross_Point(a.x,b.x,c.x,d.x,a.y,b.y,c.y,d.y);\n}\n\nstatic const int COUNTER_CLOCKWISE = 1;\nstatic const int CLOCKWISE = -1;\nstatic const int ONLINE_BACK = 2;\nstatic const int ONLINE_FRONT = -1;\nstatic const int ON_SEGMENT = 0;\n\nint ccw(Point p0,Point p1,Point p2){\n\n\tVector a = p1 - p0;\n\tVector b = p2 - p0;\n\n\tif(cross(a,b) > EPS)return COUNTER_CLOCKWISE;\n\tif(cross(a,b) < -EPS)return CLOCKWISE;\n\tif(dot(a,b) < -EPS)return ONLINE_BACK;\n\tif(a.norm() < b.norm())return ONLINE_FRONT;\n\n\treturn ON_SEGMENT;\n}\n\n\nint get_boss(int id){\n\tif(boss[id] == id)return id;\n\telse{\n\t\treturn boss[id] = get_boss(boss[id]);\n\t}\n}\n\nint is_same(int x,int y){\n\treturn get_boss(x) == get_boss(y);\n}\n\nvoid unite(int x,int y){\n\tint boss_x = get_boss(x);\n\tint boss_y = get_boss(y);\n\n\tif(boss_x == boss_y)return;\n\n\tif(height[x] > height[y]){\n\n\t\tboss[boss_y] = boss_x;\n\n\t}else if(height[x] < height[y]){\n\n\t\tboss[boss_x] = boss_y;\n\n\t}else{ //height[x] == height[y]\n\n\t\tboss[boss_y] = boss_x;\n\t\theight[x]++;\n\t}\n}\n\nvoid init(){\n\n\tfor(int i = 0; i < N; i++){\n\t\tboss[i] = i;\n\t\theight[i] = 0;\n\t}\n}\n\nvoid func(){\n\n\tfor(int i = 0; i <= 10; i++){\n\t\tANS[i] = 0;\n\t}\n\tLINE.clear();\n\n\tfor(int i = 0; i < N; i++){\n\n\t\tGROUP[i].clear();\n\t}\n\n\tfor(int loop = 0; loop < N; loop++){\n\n\t\tPoint a,b;\n\n\t\tscanf(\"%lf %lf %lf %lf\",&a.x,&a.y,&b.x,&b.y);\n\t\tLINE.push_back(Line(a,b));\n\t}\n\n\tinit();\n\n\t//交差する直線をuniteする\n\tfor(int i = 0; i < N-1; i++){\n\t\tfor(int k = i+1; k < N; k++){\n\t\t\tif(check_Cross(LINE[i],LINE[k])){\n\t\t\t\tunite(i,k);\n\t\t\t}\n\t\t}\n\t}\n\n\t//線分を、各グループに仕分けする\n\tfor(int i = 0; i < N; i++){\n\n\t\tGROUP[get_boss(i)].push_back(LINE[i]);\n\t}\n\n\tfor(int i = 0; i < N; i++){\n\n\t\tif(GROUP[i].size() == 0)continue;\n\n\t\t//交差の回数を数える\n\t\tint num_cross = 0;\n\n\t\tfor(int a = 0; a < GROUP[i].size()-1; a++){\n\t\t\tfor(int b = a+1; b < GROUP[i].size(); b++){\n\t\t\t\tif(check_Cross(GROUP[i][a],GROUP[i][b])){\n\t\t\t\t\tnum_cross++;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\n\t\tbool FLG;\n\n\t\tswitch(num_cross){\n\t\tcase 0:\n\n\t\t\tANS[1]++;\n\t\t\tbreak;\n\t\tcase 2: //4か7\n\n\t\t\tFLG = true;\n\t\t\t//交点が全て端点に含まれていれば7\n\t\t\tfor(int a = 0; a < 2; a++){\n\t\t\t\tfor(int b = a+1; b < 3; b++){\n\t\t\t\t\tif(!check_Cross(GROUP[i][a],GROUP[i][b]))continue;\n\n\t\t\t\t\tPoint ret = calc_Cross_Point(GROUP[i][a].p[0],GROUP[i][a].p[1],GROUP[i][b].p[0],GROUP[i][b].p[1]);\n\n\t\t\t\t\tif((ret == GROUP[i][a].p[0]|| ret == GROUP[i][a].p[1]) && (ret == GROUP[i][b].p[0]|| ret == GROUP[i][b].p[1])){\n\n\t\t\t\t\t\t//Do nothing\n\n\t\t\t\t\t}else{\n\n\t\t\t\t\t\tFLG = false;\n\t\t\t\t\t\tbreak;\n\t\t\t\t\t}\n\t\t\t\t\tif(!FLG)break;\n\t\t\t\t}\n\t\t\t}\n\n\t\t\tif(FLG){\n\n\t\t\t\tANS[7]++;\n\n\t\t\t}else{\n\n\t\t\t\tANS[4]++;\n\t\t\t}\n\n\n\t\t\tbreak;\n\t\tcase 3:\n\t\t\t;\n\t\t\tANS[3]++;\n\t\t\tbreak;\n\t\tcase 4: //0,2,5,9のいずれか\n\n\t\t\tif(GROUP[i].size() == 4){ //0か9\n\n\t\t\t\tFLG = true;\n\n\t\t\t\t//端点以外で交点を持つなら9\n\t\t\t\tfor(int a = 0; a < 3; a++){\n\t\t\t\t\tfor(int b = a+1; b < 4; b++){\n\t\t\t\t\t\tif(!check_Cross(GROUP[i][a],GROUP[i][b]))continue;\n\n\t\t\t\t\t\tif(GROUP[i][a].p[0] == GROUP[i][b].p[0] || GROUP[i][a].p[0] == GROUP[i][b].p[1] ||\n\t\t\t\t\t\t\t\tGROUP[i][a].p[1] == GROUP[i][b].p[0] || GROUP[i][a].p[1] == GROUP[i][b].p[1]){\n\n\t\t\t\t\t\t\t//Do nothing\n\n\t\t\t\t\t\t}else{\n\n\t\t\t\t\t\t\tFLG = false;\n\t\t\t\t\t\t\tbreak;\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t\tif(!FLG)break;\n\t\t\t\t}\n\n\t\t\t\tif(FLG){\n\n\t\t\t\t\tANS[0]++;\n\t\t\t\t}else{\n\n\t\t\t\t\tANS[9]++;\n\t\t\t\t}\n\n\t\t\t}else{ //2か5\n\n\t\t\t\t//交差回数が1の線分を探し、それと交差回数2の線分の、交点ではない端点との位置関係を見る\n\t\t\t\tint count,index_1,index_2;\n\n\t\t\t\tfor(int a = 0; a < 5; a++){\n\t\t\t\t\tcount = 0;\n\t\t\t\t\tfor(int b = 0; b < 5; b++){\n\t\t\t\t\t\tif(b == a)continue;\n\n\t\t\t\t\t\tif(check_Cross(GROUP[i][a],GROUP[i][b])){\n\t\t\t\t\t\t\tcount++;\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t\tif(count == 1){\n\n\t\t\t\t\t\tindex_1 = a;\n\t\t\t\t\t\tbreak;\n\t\t\t\t\t}\n\t\t\t\t}\n\n\t\t\t\tfor(int a = 0; a < 5; a++){\n\t\t\t\t\tif(a == index_1 || check_Cross(GROUP[i][index_1],GROUP[i][a]) == false)continue;\n\n\t\t\t\t\tindex_2 = a;\n\t\t\t\t\tbreak;\n\t\t\t\t}\n\n\t\t\t\tPoint A,B,C;\n\n\t\t\t\tif(GROUP[i][index_1].p[0] == GROUP[i][index_2].p[0]){\n\n\t\t\t\t\tA = GROUP[i][index_1].p[1];\n\t\t\t\t\tB = GROUP[i][index_1].p[0];\n\t\t\t\t\tC = GROUP[i][index_2].p[1];\n\n\t\t\t\t}else if(GROUP[i][index_1].p[0] == GROUP[i][index_2].p[1]){\n\n\t\t\t\t\tA = GROUP[i][index_1].p[1];\n\t\t\t\t\tB = GROUP[i][index_1].p[0];\n\t\t\t\t\tC = GROUP[i][index_2].p[0];\n\n\t\t\t\t}else if(GROUP[i][index_1].p[1] == GROUP[i][index_2].p[0]){\n\n\t\t\t\t\tA = GROUP[i][index_1].p[0];\n\t\t\t\t\tB = GROUP[i][index_1].p[1];\n\t\t\t\t\tC = GROUP[i][index_2].p[1];\n\n\n\t\t\t\t}else{ //GROUP[i][index_1].p[1] == GROUP[i][index_2].p[1]\n\n\t\t\t\t\tA = GROUP[i][index_1].p[0];\n\t\t\t\t\tB = GROUP[i][index_1].p[1];\n\t\t\t\t\tC = GROUP[i][index_2].p[0];\n\n\t\t\t\t}\n\n\t\t\t\tif(ccw(A,B,C) == CLOCKWISE){\n\n\t\t\t\t\tANS[2]++;\n\n\t\t\t\t}else{\n\n\t\t\t\t\tANS[5]++;\n\t\t\t\t}\n\t\t\t}\n\n\t\t\tbreak;\n\t\tcase 5:\n\n\t\t\tANS[6]++;\n\t\t\tbreak;\n\t\tcase 6:\n\n\t\t\tANS[8]++;\n\t\t\tbreak;\n\t\t}\n\t}\n\n\tprintf(\"%d\",ANS[0]);\n\tfor(int i = 1; i <= 9; i++){\n\n\t\tprintf(\" %d\",ANS[i]);\n\t}\n\tprintf(\"\\n\");\n}\n\nint main(){\n\n\twhile(true){\n\n\t\tscanf(\"%d\",&N);\n\t\tif(N == 0)break;\n\n\t\tfunc();\n\t}\n\n\treturn 0;\n}", "accuracy": 1, "time_ms": 30, "memory_kb": 3372, "score_of_the_acc": -0.0553, "final_rank": 11 }, { "submission_id": "aoj_1288_3707476", "code_snippet": "#include<iostream>\n#include<string>\n#include<cstdio>\n#include<vector>\n#include<cmath>\n#include<algorithm>\n#include<functional>\n#include<iomanip>\n#include<queue>\n#include<ciso646>\n#include<utility>\n#include<stack>\n#include<bitset>\n#include<numeric>\nusing namespace std;\ntypedef long long ll;\n#define rep(i,n) for(int i=0;i<n;i++)\n#define rep1(i,n) for(int i=1;i<=n;i++)\n#define Rep(i,sta,n) for(int i=sta;i<n;i++)\n#define stop char nyaa;cin>>nyaa;\nconst ll mod = 1000000007;\nconst long double eps = 1e-8;\ntypedef pair<int, int> P;\n\ntypedef long double ld;\n\n#include<complex>\ntypedef complex<ld> Point;\nconst ld pi = acos(-1.0);\nld dot(Point a, Point b) { return real(conj(a)*b); }\nld cross(Point a, Point b) { return imag(conj(a)*b); }\nnamespace std {\n\tbool operator<(const Point &lhs, const Point &rhs) {\n\t\treturn lhs.real() == rhs.real() ? lhs.imag() < rhs.imag() : lhs.real() < rhs.real();\n\t}\n}\nbool eq(ld a, ld b) {\n\treturn abs(a - b) < eps;\n}\nbool eq(Point a, Point b) {\n\treturn abs(a - b) < eps;\n}\nstruct Line {\n\tPoint a, b;\n};\nstruct Circle {\n\tPoint p; ld r;\n};\nint ccw(Point a, Point b, Point c) {\n\tb -= a; c -= a;\n\tif (cross(b, c) > eps)return 1;//counter clockwise\n\tif (cross(b, c) < -eps)return -1;//clock wise\n\tif (dot(b, c) < 0)return 2;//c--a--b on line\n\tif (norm(b) < norm(c))return -2;//a--b--c on line\n\treturn 0; //a--c--b on line\n}\n//2直線の交差判定\nbool isis_ll(Line l, Line m) {\n\treturn !eq(cross(l.b - l.a, m.b - m.a), 0);\n}\n//直線と線分の交差判定\nbool isis_ls(Line l, Line s) {\n\treturn (cross(l.b - l.a, s.a - l.a)*cross(l.b - l.a, s.b - l.a) < eps);\n}\n//点が直線上に存在するか\nbool isis_lp(Line l, Point p) {\n\treturn (abs(cross(l.b - p, l.a - p)) < eps);\n}\n//点が線分上に存在するか\nbool isis_sp(Line s, Point p) {\n\treturn (abs(s.a - p) + abs(s.b - p) - abs(s.b - s.a) < eps);\n}\n//線分と線分の交差判定\nbool isis_ss(Line s, Line t) {\n\tif (isis_sp(t, s.a) || isis_sp(t, s.b) || isis_sp(s, t.a) || isis_sp(s, t.b))return true;\n\treturn(cross(s.b - s.a, t.a - s.a)*cross(s.b - s.a, t.b - s.a) < -eps && cross(t.b - t.a, s.a - t.a)*cross(t.b - t.a, s.b - t.a) < -eps);\n}\n//点から直線への垂線の足\nPoint proj(Line l, Point p) {\n\tld t = dot(p - l.a, l.a - l.b) / norm(l.a - l.b);\n\treturn l.a + t * (l.a - l.b);\n}\n//直線と直線の交点\n//平行な2直線に対しては使うな!!!!\nPoint is_ll(Line s, Line t) {\n\tPoint sv = s.b - s.a; Point tv = t.b - t.a;\n\t//if (cross(tv, sv) == 0)return { 0,0 };\n\treturn s.a + sv * cross(tv, t.a - s.a) / cross(tv, sv);\n}\n//直線と点の距離\nld dist_lp(Line l, Point p) {\n\treturn abs(p - proj(l, p));\n}\n//直線と直線の距離\nld dist_ll(Line l, Line m) {\n\treturn isis_ll(l, m) ? 0 : dist_lp(l, m.a);\n}\n//線分と直線の距離\nld dist_ls(Line l, Line s) {\n\treturn isis_ls(l, s) ? 0 : min(dist_lp(l, s.a), dist_lp(l, s.b));\n}\n//線分と点の距離\nld dist_sp(Line s, Point p) {\n\tPoint r = proj(s, p);\n\treturn isis_sp(s, r) ? abs(p - r) : min(abs(p - s.a), abs(p - s.b));\n}\n//線分と線分の距離\nld dist_ss(Line s, Line t) {\n\tif (isis_ss(s, t))return 0;\n\treturn min({ dist_sp(s,t.a),dist_sp(s,t.b),dist_sp(t,s.a),dist_sp(t,s.b) });\n}\n\nint distingish_number(vector<Line> &v) {\n\tif (v.size() == 1)return 1;\n\tif (v.size() == 3) {\n\t\tvector<Point> p;\n\t\trep(i, 3) {\n\t\t\tRep(j, i + 1, 3) {\n\t\t\t\tLine lhs = v[i], rhs = v[j];\n\t\t\t\tif (isis_ss(lhs, rhs)) {\n\t\t\t\t\tPoint c = is_ll(lhs, rhs);\n\t\t\t\t\tp.push_back(c);\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\t//\n\t\tif (p.size() != 2)return 0;\n\t\t//\n\t\trep(i, 2) {\n\t\t\tint cnt = 0;\n\t\t\trep(j, 3) {\n\t\t\t\tLine l = v[j];\n\t\t\t\tif (eq(l.a, p[i]) || eq(l.b, p[i]))cnt++;\n\t\t\t}\n\t\t\tif (cnt != 2)return 4;\n\t\t}\n\t\treturn 7;\n\t}\n\tif (v.size() == 4) {\n\t\tvector<Point> p;\n\t\trep(i, 4) {\n\t\t\tRep(j, i + 1, 4) {\n\t\t\t\tLine lhs = v[i], rhs = v[j];\n\t\t\t\tif (isis_ss(lhs, rhs)) {\n\t\t\t\t\tPoint c = is_ll(lhs, rhs);\n\t\t\t\t\tp.push_back(c);\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\tif (p.size() == 3)return 3;\n\t\t//\n\t\tif (p.size() != 4)return 0;\n\t\t//\n\t\trep(i, 4) {\n\t\t\tint cnt = 0;\n\t\t\trep(j, 4) {\n\t\t\t\tLine l = v[j];\n\t\t\t\tif (eq(l.a, p[i]) || eq(l.b, p[i]))cnt++;\n\t\t\t}\n\t\t\tif (cnt != 2)return 9;\n\t\t}\n\t\treturn 0;\n\t}\n\tvector<Point> p;\n\trep(i, 5) {\n\t\tRep(j, i + 1, 5) {\n\t\t\tLine lhs = v[i], rhs = v[j];\n\t\t\tif (isis_ss(lhs, rhs)) {\n\t\t\t\tPoint c = is_ll(lhs, rhs);\n\t\t\t\tp.push_back(c);\n\t\t\t}\n\t\t}\n\t}\n\tif (p.size() == 6)return 8;\n\tif (p.size() == 5)return 6;\n\tif (p.size() != 4)return 0;\n\tld ma = -1;\n\tint le, ri;\n\trep(i, 4)Rep(j, i + 1, 4) {\n\t\tif (abs(p[i] - p[j]) > ma) {\n\t\t\tma = abs(p[i] - p[j]); le = i, ri = j;\n\t\t}\n\t}\n\tma = -1;\n\tint le2, ri2;\n\trep(i, 4) {\n\t\tif (i == le || i == ri)continue;\n\t\tif (abs(p[i] - p[ri]) > ma) {\n\t\t\tma = abs(p[i] - p[ri]);\n\t\t\tle2 = i;\n\t\t}\n\t}\n\tri2 = 6 - le - ri - le2;\n\tif (ccw(p[le], p[le2], p[ri2]) == -1)return 2;\n\treturn 5;\n}\n\nint n;\nvoid solve() {\n\tvector<Line> v(n);\n\tvector<bool> used(n, false);\n\trep(i, n) {\n\t\tPoint a, b;\n\t\tld x, y; cin >> x >> y; a = { x,y };\n\t\tcin >> x >> y; b = { x,y };\n\t\tv[i] = { a,b };\n\t}\n\tint cnt[10] = {};\n\trep(i, n) {\n\t\tif (used[i])continue;\n\t\tvector<Line> u; u.push_back(v[i]); used[i] = true;\n\t\trep(aa, 5) {\n\t\t\tRep(j, i + 1, n) {\n\t\t\t\tif (used[j])continue;\n\t\t\t\tbool f = false;\n\t\t\t\trep(k, u.size()) {\n\t\t\t\t\tif (isis_ss(u[k], v[j])) {\n\t\t\t\t\t\tf = true; break;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\tif (f) {\n\t\t\t\t\tu.push_back(v[j]);\n\t\t\t\t\tused[j] = true;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\t//cout <<\"???\"<< u.size() << endl;\n\t\tcnt[distingish_number(u)]++;\n\t}\n\trep(i, 10) {\n\t\tif (i > 0)cout << \" \";\n\t\tcout << cnt[i];\n\t}\n\tcout << endl;\n}\nsigned main() {\n\tcin.tie(0);\n\tios::sync_with_stdio(false);\n\twhile (cin >> n, n) {\n\t\tsolve();\n\t}\n\t//stop\n\treturn 0;\n}", "accuracy": 1, "time_ms": 3920, "memory_kb": 3388, "score_of_the_acc": -1.0526, "final_rank": 20 }, { "submission_id": "aoj_1288_3048967", "code_snippet": "#include <iostream>\n#include <iomanip>\n#include <complex>\n#include <vector>\n#include <algorithm>\n#include <cmath>\n#include <array>\n#include <queue>\nusing namespace std;\nconst double EPS = 1e-10;\nconst double INF = 1e12;\n#define EQ(n,m) (abs((n)-(m)) < EPS)\n#define X real()\n#define Y imag()\n\ntypedef complex<double> P;\ntypedef vector<P> VP;\nstruct L : array<P, 2>{\n L(const P& a, const P& b){ at(0)=a; at(1)=b; }\n L(){}\n};\nnamespace std{\n bool operator < (const P& a, const P& b){\n return !EQ(a.X,b.X)? a.X<b.X : a.Y+EPS<b.Y;\n }\n bool operator == (const P& a, const P& b){\n return abs(a-b) < EPS;\n }\n}\ndouble dot(P a, P b){\n return (conj(a)*b).X;\n}\ndouble cross(P a, P b){\n return (conj(a)*b).Y;\n}\nint ccw(P a, P b, P c){\n b -= a;\n c -= a;\n if(cross(b,c) > EPS) return +1; //ccw\n if(cross(b,c) < -EPS) return -1; //cw\n if(dot(b,c) < -EPS) return +2; //c-a-b\n if(abs(c)-abs(b) > EPS) return -2; //a-b-c\n return 0; //a-c-b\n}\n\nbool intersectSP(const L& s, const P &p){\n return abs(cross(s[0]-p, s[1]-p))<EPS && dot(s[0]-p, s[1]-p)<EPS;\n}\nbool intersectSS(const L& a, const L& b){\n return ( ccw(a[0],a[1],b[0]) *ccw(a[0],a[1],b[1]) <= 0 ) &&\n ( ccw(b[0],b[1],a[0]) *ccw(b[0],b[1],a[1]) <= 0 );\n}\n\nint main(){\n while(1){\n int n;\n cin >> n;\n if(n==0) break;\n\n vector<L> ls(n);\n for(int i=0; i<n; i++){\n int xs,ys,xt,yt;\n cin >> xs >> ys >> xt >> yt;\n ls[i] = L(P(xs, ys), P(xt, yt));\n }\n\n int ans[10] = {};\n while(!ls.empty()){\n L beg = ls.back();\n ls.pop_back();\n vector<L> cluster(1, beg);\n queue<L> wait;\n wait.push(beg);\n while(!wait.empty()){\n L curr = wait.front();\n wait.pop();\n for(int i=0; i<(int)ls.size(); i++){\n if(intersectSS(curr, ls[i])){\n wait.push(ls[i]);\n cluster.push_back(ls[i]);\n ls.erase(ls.begin() +i);\n i--;\n }\n }\n }\n\n //各クラスタについて辺を分類\n int m = cluster.size();\n vector<vector<int> > ep(m, vector<int>(2, 0)), mp(m, vector<int>(2, 0));\n int ee=0, ex=0, em=0, xm=0, mm=0;\n for(int i=0; i<m; i++){\n int endpoint=0, mid=0;\n for(int j=0; j<m; j++){\n if(i==j) continue;\n for(int d=0; d<2; d++){\n if(cluster[i][d] == cluster[j][0] || cluster[i][d] == cluster[j][1]){\n endpoint++;\n ep[i][d]++;\n continue;\n }\n if(intersectSP(cluster[j], cluster[i][d])){\n mid++;\n mp[i][d]++;\n }\n }\n }\n if(endpoint == 2){\n ee++;\n }else if(endpoint == 1){\n if(mid == 1) em++;\n else ex++;\n }else{\n if(mid == 2) mm++;\n if(mid == 1) xm++;\n }\n }\n\n if(ee==3 && ex==2){\n int num = 0;\n VP ps(3);\n for(int i=0; i<m; i++){\n for(int d=0; d<2; d++){\n if(ep[i][d] == 0){\n ps[num] = cluster[i][d];\n num++;\n ps[2] = cluster[i][1-d];\n }\n }\n }\n if(ccw(ps[0], ps[1], ps[2]) == 1){\n ans[5]++;\n }else{\n ans[2]++;\n }\n }else if(mm == 1){\n ans[8]++;\n }else if(xm == 1){\n ans[3]++;\n }else if(ee == 0){\n if(ex == 1){\n ans[4]++;\n }else{\n ans[1]++;\n }\n }else if(ee == 1){\n ans[7]++;\n }else if(ee == 2){\n ans[9]++;\n }else if(ee == 3){\n ans[6]++;\n }else{\n ans[0]++;\n }\n }\n\n for(int i=0; i<9; i++){\n cout << ans[i] << \" \";\n }\n cout << ans[9] << endl;\n }\n return 0;\n}", "accuracy": 1, "time_ms": 30, "memory_kb": 3036, "score_of_the_acc": -0.0051, "final_rank": 1 }, { "submission_id": "aoj_1288_2899209", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\n#define rep(i,n) for(int (i)=0;(i)<(int)(n);++(i))\n#define all(x) (x).begin(),(x).end()\n#define pb push_back\n#define fi first\n#define se second\n#define dbg(x) cout<<#x\" = \"<<((x))<<endl\ntemplate<class T,class U> ostream& operator<<(ostream& o, const pair<T,U> &p){o<<\"(\"<<p.fi<<\",\"<<p.se<<\")\";return o;}\ntemplate<class T> ostream& operator<<(ostream& o, const vector<T> &v){o<<\"[\";for(T t:v){o<<t<<\",\";}o<<\"]\";return o;}\n\nconst double eps = 1e-6;\n\nusing Point = pair<int,int>;\nusing Line = pair<Point,Point>;\n\ninline Line read_L(){\n Point p1,p2;\n cin >>p1.fi >>p1.se >>p2.fi >>p2.se;\n if(p1>p2) swap(p1,p2);\n return {p1,p2};\n}\n\ndouble dist(double px, double py, double qx, double qy){\n return sqrt((qx-px)*(qx-px) + (qy-py)*(qy-py));\n}\n\nbool on(Line l, Point p){\n Point q1 = l.fi, q2 = l.se;\n int dx = q2.fi - q1.fi, dy = q2.se - q1.se;\n\n double t;\n if(dx != 0) t = (double)(p.fi-q1.fi)/dx;\n else t = (double)(p.se-q1.se)/dy;\n\n double px = q1.fi + t*dx;\n double py = q1.se + t*dy;\n\n if(0<t && t<1){\n return (dist(px,py,p.fi,p.se) < eps);\n }\n return false;\n}\n\nint isec(Line l, Line m){\n if(l.fi == m.fi || l.fi == m.se || l.se == m.fi || l.se == m.se) return 1;\n if(on(l,m.fi) || on(l,m.se) || on(m,l.fi) || on(m,l.se)) return 2;\n return 0;\n}\n\nint recognize(const vector<Line> &v){\n // 棒の本数\n int V = v.size();\n\n set<Point> s;\n map<Point,int> ct;\n rep(i,V){\n s.insert(v[i].fi);\n s.insert(v[i].se);\n ++ct[v[i].fi];\n ++ct[v[i].se];\n }\n\n // 頂点の数\n int S = s.size();\n\n if(V==4 && S==4) return 0;\n if(V==1 && S==2) return 1;\n if(V==4 && S==6) return 3;\n if(V==3 && S==5) return 4;\n if(V==3 && S==4) return 7;\n if(V==4 && S==5) return 9;\n\n int two = 0;\n rep(i,V){\n if(ct[v[i].fi]==1 && ct[v[i].se]==1) ++two;\n }\n if(two==1) return 8;\n\n rep(i,V)rep(j,i){\n if(isec(v[i],v[j])==2) return 6;\n }\n\n // 2 or 5\n\n // parallel 3 lines\n map<pair<int,int>,int> d_ct;\n vector<pair<int,int>> d(V);\n rep(i,V){\n Point p1 = v[i].fi, p2 = v[i].se;\n int dx = p2.fi-p1.fi, dy = p2.se-p1.se;\n int G = __gcd(dx,dy);\n dx /= G;\n dy /= G;\n d[i] = {dx,dy};\n\n ++d_ct[d[i]];\n }\n\n // printf(\"d_ct:\\n\");\n // for(const auto &pp:d_ct) dbg(pp);\n\n vector<pair<double,Line>> p_l;\n rep(i,V)if(d_ct[d[i]]==3){\n\n // y-y1 = (y2-y1)/(x2-x1) (x-x1)\n // (y2-y1)(x-x1) - (x2-x1)(y-y1) = 0\n // (y2-y1)x - (x2-x1)y -(y2-y1)x1 + (x2-x1)y1 = 0\n int x1 = v[i].fi.fi, y1 = v[i].fi.se;\n int x2 = v[i].se.fi, y2 = v[i].se.se;\n\n int A = y2-y1, B = -(x2-x1);\n int C = -(y2-y1)*x1 + (x2-x1)*y1;\n\n if(B==0){\n p_l.pb({C/(double)A ,v[i]});\n }\n else{\n p_l.pb({-C/(double)B ,v[i]});\n }\n }\n\n sort(all(p_l));\n // dbg(p_l);\n\n Line two_checker(p_l[1].se.se, p_l[2].se.se);\n if(two_checker.fi > two_checker.se) swap(two_checker.fi, two_checker.se);\n // dbg(two_checker);\n rep(i,V)if(v[i] == two_checker) return 2;\n return 5;\n}\n\nconst int N = 1000;\nvector<int> G[N];\n\nint main(){\n int n;\n while(cin >>n,n){\n rep(i,N) G[i].clear();\n\n vector<Line> l(n);\n rep(i,n) l[i] = read_L();\n\n rep(i,n)rep(j,i){\n if(isec(l[i],l[j])!=0){\n G[i].pb(j);\n G[j].pb(i);\n }\n }\n\n int ans[10]={};\n vector<bool> vis(n);\n rep(i,n)if(!vis[i]){\n queue<int> que;\n vector<Line> cc;\n\n vis[i] = true;\n que.push(i);\n cc.pb(l[i]);\n while(!que.empty()){\n int v = que.front();\n que.pop();\n for(int nx:G[v]){\n if(!vis[nx]){\n vis[nx] = true;\n que.push(nx);\n cc.pb(l[nx]);\n }\n }\n }\n\n int idx = recognize(cc);\n ++ans[idx];\n }\n\n rep(i,10) cout << ans[i] << \" \\n\"[i==9];\n }\n return 0;\n}", "accuracy": 1, "time_ms": 40, "memory_kb": 3176, "score_of_the_acc": -0.0286, "final_rank": 4 }, { "submission_id": "aoj_1288_2794112", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\nusing ld = long double;\nusing point = complex<ld>;\n\nconstexpr ld eps = 1e-10;\n\nbool comp(point a, point b) {\n return (real(a - b) * 1.347589 + imag(a - b)) > 0;\n}\n\nld dot(point a, point b) {\n return real(conj(a) * b);\n}\n\nld cross(point a, point b) {\n return imag(conj(a) * b);\n}\n\nvector<point> unique(vector<point> ps) {\n sort(begin(ps), end(ps), comp);\n vector<point> res;\n for(auto& p : ps) {\n if(res.empty() || abs(res.back() - p) > eps) {\n res.push_back(p);\n }\n }\n return res;\n}\n\nstruct segment {\n segment(point a, point b) : a(a), b(b) {}\n bool operator<(segment other) const {\n return min(real(a), real(b)) < min(real(other.a), real(other.b));\n }\n point a, b;\n};\n\nint ccw(point a, point b, point c) {\n b -= a; c -= a;\n if(cross(b, c) > eps) return 1; // a -> b -> c : counterclockwise\n if(cross(b, c) < -eps) return -1; // a -> b -> c : clockwise\n if(dot(b, c) < 0) return 2; // c -> a -> b : line\n if(std::norm(b) < std::norm(c)) return -2; // a -> b -> c : line\n return 0; // a -> c -> b : line\n}\n\nbool isis_ss(segment s, segment t) {\n return ccw(s.a, s.b, t.a) * ccw(s.a, s.b, t.b) <= 0\n && ccw(t.a, t.b, s.a) * ccw(t.a, t.b, s.b) <= 0;\n}\n\npoint rotate(point p, ld theta) {\n const ld x = real(p), y = imag(p);\n return point(x * cos(theta) - y * sin(theta), x * sin(theta) + y * cos(theta));\n}\n\nint main() {\n int n;\n while(cin >> n, n) {\n vector<segment> ss;\n for(int i = 0; i < n; ++i) {\n ld x1, y1, x2, y2;\n cin >> x1 >> y1 >> x2 >> y2;\n ss.emplace_back(point(x1, y1), point(x2, y2));\n }\n vector<vector<int>> g(n, vector<int>(n));\n for(int i = 0; i < n; ++i) {\n for(int j = i + 1; j < n; ++j) {\n if(isis_ss(ss[i], ss[j])) {\n g[i].push_back(j);\n g[j].push_back(i);\n }\n }\n }\n vector<vector<segment>> digits;\n vector<bool> used(n);\n function<void(int, vector<segment>&)> dfs = [&](int v, vector<segment>& d) {\n used[v] = true;\n d.push_back(ss[v]);\n for(auto to : g[v]) {\n if(used[to]) continue;\n dfs(to, d);\n }\n };\n for(int i = 0; i < n; ++i) {\n if(used[i]) continue;\n vector<segment> digit;\n dfs(i, digit);\n digits.push_back(move(digit));\n }\n\n vector<int> ans(10);\n for(auto& d : digits) {\n if(d.size() == 1) {\n ans[1]++;\n } else if(d.size() == 3 || d.size() == 4) {\n vector<point> ps;\n for(auto& s : d) {\n ps.push_back(s.a);\n ps.push_back(s.b);\n }\n ps = unique(ps);\n if(ps.size() == 4) {\n ans[d.size() == 3 ? 7 : 0]++;\n } else if(ps.size() == 5) {\n ans[d.size() == 3 ? 4 : 9]++;\n } else if(ps.size() == 6) {\n ans[3]++;\n } else {\n assert(false); // !unreachable()\n }\n } else {\n int is_eight_or_six = 0;\n for(auto& s : d) {\n int cnt = 0;\n for(auto& s2 : d) {\n cnt += isis_ss(s, s2);\n }\n is_eight_or_six += cnt == 4;\n }\n if(is_eight_or_six == 2) {\n ans[8] += 1;\n } else if(is_eight_or_six == 1) {\n ans[6] += 1;\n } else {\n const ld rad = -arg(d[0].a - d[0].b);\n vector<segment> hori, vert;\n for(auto& s : d) {\n s.a = rotate(s.a, rad);\n s.b = rotate(s.b, rad);\n if(abs(real(s.a) - real(s.b)) < eps) {\n vert.push_back(s);\n } else {\n hori.push_back(s);\n }\n }\n\n if(hori.size() == 2) {\n if(hori[1] < hori[0]) swap(hori[0], hori[1]);\n if(imag(hori[0].a) > imag(hori[1].a)) {\n ans[2] += 1;\n } else {\n ans[5] += 1;\n }\n } else if(vert.size() == 2) {\n if(vert[1] < vert[0]) swap(vert[0], vert[1]);\n if(min(imag(vert[0].a), imag(vert[0].b)) > min(imag(vert[1].a), imag(vert[1].b))) {\n ans[5] += 1;\n } else {\n ans[2] += 1;\n }\n } else {\n assert(false); // !unreachable()\n }\n }\n }\n }\n for(int i = 0; i < 10; ++i) {\n cout << ans[i] << \" \\n\"[i + 1 == 10];\n }\n }\n}", "accuracy": 1, "time_ms": 180, "memory_kb": 9732, "score_of_the_acc": -1.0435, "final_rank": 19 }, { "submission_id": "aoj_1288_2565701", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\ntypedef long long ll;\n\nconst double EPS = 1e-8;\nconst double INF = 1e12;\ntypedef complex<double> P;\nnamespace std{\n\tbool operator < (const P& a, const P& b){\n\t\treturn real(a) != real(b) ? real(a) < real(b) : imag(a) < imag(b);\n\t}\n}\ndouble cross(const P& a, const P& b){\n\treturn imag(conj(a)*b);\n}\ndouble dot(const P& a, const P& b){\n\treturn real(conj(a)*b);\n}\n\nstruct L : public vector<P>{\n\tL(){}\n\tL(const P &a, const P &b){\n\t\tpush_back(a); push_back(b);\n\t}\n};\n\nstruct C{\n\tP p; double r;\n\tC(const P &p, double r) : p(p), r(r){}\n};\n\nint ccw(P a, P b, P c){\n\tb -= a; c -= a;\n\tif(cross(b, c) > 0) return +1; // counter clockwise\n\tif(cross(b, c) < 0) return -1; // clockwise\n\tif(dot(b, c) < 0) return +2; // c--a--b on line\n\tif(norm(b) < norm(c)) return -2; // a--b--c on line\n\treturn 0;\n}\n\nbool intersectLL(const L &l, const L &m){\n\treturn abs(cross(l[1] - l[0], m[1] - m[0])) > EPS || // non-parallel\n\t\tabs(cross(l[1] - l[0], m[0] - l[0])) < EPS; // same line\n}\nbool intersectLS(const L &l, const L &s){\n\treturn cross(l[1] - l[0], s[0] - l[0])* // s[0] is left of l\n\t\tcross(l[1] - l[0], s[1] - l[0]) < EPS; // s[1] is right of l\n}\nbool intersectLP(const L &l, const P &p){\n\treturn abs(cross(l[1] - p, l[0] - p)) < EPS;\n}\nbool intersectSS(const L &s, const L &t){\n\treturn ccw(s[0], s[1], t[0])*ccw(s[0], s[1], t[1]) <= 0 &&\n\t\tccw(t[0], t[1], s[0])*ccw(t[0], t[1], s[1]) <= 0;\n}\nbool intersectSP(const L &s, const P &p){\n\treturn abs(s[0] - p) + abs(s[1] - p) - abs(s[1] - s[0]) < EPS; // triangle inequality\n}\n\nstruct UnionFind{\n\tvector<int> par;\n\tint cnt;\n\tUnionFind(int size_) : par(size_, -1), cnt(size_){}\n\tvoid unite(int x, int y){\n\t\tif((x = find(x)) != (y = find(y))){\n\t\t\tif(par[y] < par[x]) swap(x, y);\n\t\t\tpar[x] += par[y]; par[y] = x; cnt--;\n\t\t}\n\t}\n\tbool same(int x, int y){ return find(x) == find(y); }\n\tint find(int x){ return par[x] < 0 ? x : par[x] = find(par[x]); }\n\tint size(int x){ return -par[find(x)]; }\n\tint size(){ return cnt; }\n};\n\nL l[1000];\nvector<int> G[1000];\nmap<vector<int>, int> m;\n\nint check(vector<int> g){\n\tif(g.size() == 0) return -1;\n\tif(g.size() == 1) return 1;\n\tif(g.size() == 2) return -1;\n\n\tfor(int i = 0; i < g.size(); i++){\n\t\tP v1 = l[g[i]][0] - l[g[i]][1];\n\t\tfor(int j = i + 1; j < g.size(); j++){\n\t\t\tif(i == j) continue;\n\t\t\tP v2 = l[g[j]][0] - l[g[j]][1];\n\t\t\tdouble d = dot(v1, v2);\n\t\t\tdouble c = cross(v1, v2);\n\t\t\tif(d < EPS || c < EPS) continue;\n\t\t\treturn -1;\n\t\t}\n\t}\n\n\tvector<int> num(6);\n\tfor(int i = 0; i < g.size(); i++){\n\t\tint same = 0, mid = 0;\n\t\tL l1 = l[g[i]];\n\t\tfor(int j = 0; j < g.size(); j++){\n\t\t\tif(i == j) continue;\n\t\t\tL l2 = l[g[j]];\n\t\t\tif(l1[0] == l2[0] || l1[1] == l2[1] || l1[0] == l2[1] || l1[1] == l2[0]){\n\t\t\t\tsame++;\n\t\t\t}\n\t\t\telse if(intersectSP(l2, l1[0]) || intersectSP(l2, l1[1])){\n\t\t\t\tmid++;\n\t\t\t}\n\t\t}\n\t\tif(same == 0 && mid == 0) num[0]++;\n\t\tif(same == 1 && mid == 0) num[1]++;\n\t\tif(same == 2 && mid == 0) num[2]++;\n\t\tif(same == 1 && mid == 1) num[3]++;\n\t\tif(same == 0 && mid == 2) num[4]++;\n\t\tif(same == 0 && mid == 1) num[5]++;\n\t}\n\n\tint ret = -1;\n\tif(m.count(num)) ret = m[num];\n\n\tif(ret == 2){\n\t\tfor(int i = 0; i < g.size(); i++){\n\t\t\tint same = 0, mid = 0;\n\t\t\tL l1 = l[g[i]];\n\t\t\tL ll;\n\t\t\tP p;\n\t\t\tfor(int j = 0; j < g.size(); j++){\n\t\t\t\tif(i == j) continue;\n\t\t\t\tL l2 = l[g[j]];\n\t\t\t\tif(l1[0] == l2[0] || l1[1] == l2[1] || l1[0] == l2[1] || l1[1] == l2[0]){\n\t\t\t\t\tsame++;\n\t\t\t\t\tif(l1[0] == l2[0] || l1[0] == l2[1]) p = l1[0];\n\t\t\t\t\telse p = l1[1];\n\t\t\t\t\tll = l2;\n\t\t\t\t}\n\t\t\t}\n\t\t\tif(same == 1 && mid == 0){\n\t\t\t\tP p1 = l1[0], p2 = l1[1];\n\t\t\t\tif(p2 == p) swap(p1, p2);\n\t\t\t\tP v1 = p2 - p1;\n\t\t\t\tp1 = ll[0], p2 = ll[1];\n\t\t\t\tif(p2 == p) swap(p1, p2);\n\t\t\t\tP v2 = p2 - p1;\n\n\t\t\t\tdouble c = cross(v1, v2);\n\t\t\t\tif(c < 0) ret = 5;\n\t\t\t}\n\t\t}\n\t}\n\treturn ret;\n}\n\nint main(){\n\tcin.tie(0);\n\tios::sync_with_stdio(false);\n#ifdef LOCAL\n\tstd::ifstream in(\"in\");\n\tstd::cin.rdbuf(in.rdbuf());\n#endif\n\n\tm[{ 0, 0, 4, 0, 0, 0 }] = 0;\n\tm[{ 1, 0, 4, 0, 0, 0 }] = 1;\n\tm[{ 0, 2, 3, 0, 0, 0 }] = 2;\n\tm[{ 0, 2, 1, 0, 0, 1 }] = 3;\n\tm[{ 1, 1, 0, 1, 0, 0 }] = 4;\n\t//m[{ 0, 2, 3, 0, 0, 0 }] = 5;\n\tm[{ 0, 1, 3, 1, 0, 0 }] = 6;\n\tm[{ 0, 2, 1, 0, 0, 0 }] = 7;\n\tm[{ 0, 0, 4, 0, 1, 0 }] = 8;\n\tm[{ 0, 1, 2, 1, 0, 0 }] = 9;\n\n\tint N;\n\twhile(cin >> N, N){\n\t\tfor(int i = 0; i < N; i++){\n\t\t\tdouble x1, y1, x2, y2;\n\t\t\tcin >> x1 >> y1 >> x2 >> y2;\n\t\t\tl[i] = L(P(x1, y1), P(x2, y2));\n\t\t}\n\n\t\tfor(int i = 0; i < N; i++){\n\t\t\tG[i].clear();\n\t\t}\n\n\t\tUnionFind uf(N);\n\t\tfor(int i = 0; i < N; i++){\n\t\t\tfor(int j = i + 1; j < N; j++){\n\t\t\t\tif(intersectSS(l[i], l[j])) uf.unite(i, j);\n\t\t\t}\n\t\t}\n\t\tfor(int i = 0; i < N; i++){\n\t\t\tint p = uf.find(i);\n\t\t\tG[p].push_back(i);\n\t\t}\n\n\t\tint ans[10] = { 0 };\n\t\tfor(int i = 0; i < N; i++){\n\t\t\tint res = check(G[i]);\n\t\t\tif(res != -1) ans[res]++;\n\t\t}\n\n\t\tfor(int i = 0; i < 10; i++){\n\t\t\tcout << ans[i] << (i != 9 ? \" \" : \"\");\n\t\t}\n\t\tcout << endl;\n\t}\n\n\n}", "accuracy": 1, "time_ms": 40, "memory_kb": 3312, "score_of_the_acc": -0.0489, "final_rank": 10 }, { "submission_id": "aoj_1288_2565698", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\ntypedef long long ll;\n\nconst double EPS = 1e-8;\nconst double INF = 1e12;\ntypedef complex<double> P;\nnamespace std{\n\tbool operator < (const P& a, const P& b){\n\t\treturn real(a) != real(b) ? real(a) < real(b) : imag(a) < imag(b);\n\t}\n}\ndouble cross(const P& a, const P& b){\n\treturn imag(conj(a)*b);\n}\ndouble dot(const P& a, const P& b){\n\treturn real(conj(a)*b);\n}\n\nstruct L : public vector<P>{\n\tL(){}\n\tL(const P &a, const P &b){\n\t\tpush_back(a); push_back(b);\n\t}\n};\n\nstruct C{\n\tP p; double r;\n\tC(const P &p, double r) : p(p), r(r){}\n};\n\nint ccw(P a, P b, P c){\n\tb -= a; c -= a;\n\tif(cross(b, c) > 0) return +1; // counter clockwise\n\tif(cross(b, c) < 0) return -1; // clockwise\n\tif(dot(b, c) < 0) return +2; // c--a--b on line\n\tif(norm(b) < norm(c)) return -2; // a--b--c on line\n\treturn 0;\n}\n\nbool intersectLL(const L &l, const L &m){\n\treturn abs(cross(l[1] - l[0], m[1] - m[0])) > EPS || // non-parallel\n\t\tabs(cross(l[1] - l[0], m[0] - l[0])) < EPS; // same line\n}\nbool intersectLS(const L &l, const L &s){\n\treturn cross(l[1] - l[0], s[0] - l[0])* // s[0] is left of l\n\t\tcross(l[1] - l[0], s[1] - l[0]) < EPS; // s[1] is right of l\n}\nbool intersectLP(const L &l, const P &p){\n\treturn abs(cross(l[1] - p, l[0] - p)) < EPS;\n}\nbool intersectSS(const L &s, const L &t){\n\treturn ccw(s[0], s[1], t[0])*ccw(s[0], s[1], t[1]) <= 0 &&\n\t\tccw(t[0], t[1], s[0])*ccw(t[0], t[1], s[1]) <= 0;\n}\nbool intersectSP(const L &s, const P &p){\n\treturn abs(s[0] - p) + abs(s[1] - p) - abs(s[1] - s[0]) < EPS; // triangle inequality\n}\n\nstruct UnionFind{\n\tvector<int> par;\n\tint cnt;\n\tUnionFind(int size_) : par(size_, -1), cnt(size_){}\n\tvoid unite(int x, int y){\n\t\tif((x = find(x)) != (y = find(y))){\n\t\t\tif(par[y] < par[x]) swap(x, y);\n\t\t\tpar[x] += par[y]; par[y] = x; cnt--;\n\t\t}\n\t}\n\tbool same(int x, int y){ return find(x) == find(y); }\n\tint find(int x){ return par[x] < 0 ? x : par[x] = find(par[x]); }\n\tint size(int x){ return -par[find(x)]; }\n\tint size(){ return cnt; }\n};\n\nL l[1000];\nvector<int> G[1000];\nmap<vector<int>, int> m;\n\nint check(vector<int> g){\n\tif(g.size() == 0) return -1;\n\tif(g.size() == 1) return 1;\n\tif(g.size() == 2) return -1;\n\n\tfor(int i = 0; i < g.size(); i++){\n\t\tP v1 = l[g[i]][0] - l[g[i]][1];\n\t\tfor(int j = i + 1; j < g.size(); j++){\n\t\t\tif(i == j) continue;\n\t\t\tP v2 = l[g[j]][0] - l[g[j]][1];\n\t\t\tdouble d = dot(v1, v2);\n\t\t\tdouble c = cross(v1, v2);\n\t\t\tif(d < EPS || c < EPS) continue;\n\t\t\treturn -1;\n\t\t}\n\t}\n\n\tvector<int> num(6);\n\tfor(int i = 0; i < g.size(); i++){\n\t\tint same = 0, mid = 0;\n\t\tL l1 = l[g[i]];\n\t\tfor(int j = 0; j < g.size(); j++){\n\t\t\tif(i == j) continue;\n\t\t\tL l2 = l[g[j]];\n\t\t\tif(l1[0] == l2[0] || l1[1] == l2[1] || l1[0] == l2[1] || l1[1] == l2[0]){\n\t\t\t\tsame++;\n\t\t\t}\n\t\t\telse if(intersectSP(l2, l1[0]) || intersectSP(l2, l1[1])){\n\t\t\t\tmid++;\n\t\t\t}\n\t\t}\n\t\tif(same == 0 && mid == 0) num[0]++;\n\t\tif(same == 1 && mid == 0) num[1]++;\n\t\tif(same == 2 && mid == 0) num[2]++;\n\t\tif(same == 1 && mid == 1) num[3]++;\n\t\tif(same == 0 && mid == 2) num[4]++;\n\t\tif(same == 0 && mid == 1) num[5]++;\n\t}\n\n\tint ret = -1;\n\tif(m.count(num)) ret = m[num];\n\n\tif(ret == 2){\n\t\tfor(int i = 0; i < g.size(); i++){\n\t\t\tint same = 0, mid = 0;\n\t\t\tL l1 = l[g[i]];\n\t\t\tL ll;\n\t\t\tP p;\n\t\t\tfor(int j = 0; j < g.size(); j++){\n\t\t\t\tif(i == j) continue;\n\t\t\t\tL l2 = l[g[j]];\n\t\t\t\tif(l1[0] == l2[0] || l1[1] == l2[1] || l1[0] == l2[1] || l1[1] == l2[0]){\n\t\t\t\t\tsame++;\n\t\t\t\t\tif(l1[0] == l2[0] || l1[0] == l2[1]) p = l1[0];\n\t\t\t\t\telse p = l1[1];\n\t\t\t\t\tll = l2;\n\t\t\t\t}\n\t\t\t\telse if(intersectSP(l2, l1[0]) || intersectSP(l2, l1[1])){\n\t\t\t\t\tmid++;\n\t\t\t\t}\n\t\t\t}\n\t\t\tif(same == 1 && mid == 0){\n\t\t\t\tP p1 = l1[0], p2 = l1[1];\n\t\t\t\tif(p2 == p) swap(p1, p2);\n\t\t\t\tP v1 = p2 - p1;\n\t\t\t\tp1 = ll[0], p2 = ll[1];\n\t\t\t\tif(p2 == p) swap(p1, p2);\n\t\t\t\tP v2 = p2 - p1;\n\n\t\t\t\tdouble c = cross(v1, v2);\n\t\t\t\tif(c < 0) ret = 5;\n\t\t\t}\n\t\t}\n\t}\n\treturn ret;\n}\n\nint main(){\n\tcin.tie(0);\n\tios::sync_with_stdio(false);\n#ifdef LOCAL\n\tstd::ifstream in(\"in\");\n\tstd::cin.rdbuf(in.rdbuf());\n#endif\n\n\tm[{ 0, 0, 4, 0, 0, 0 }] = 0;\n\tm[{ 1, 0, 4, 0, 0, 0 }] = 1;\n\tm[{ 0, 2, 3, 0, 0, 0 }] = 2;\n\tm[{ 0, 2, 1, 0, 0, 1 }] = 3;\n\tm[{ 1, 1, 0, 1, 0, 0 }] = 4;\n\t//m[{ 0, 2, 3, 0, 0, 0 }] = 5;\n\tm[{ 0, 1, 3, 1, 0, 0 }] = 6;\n\tm[{ 0, 2, 1, 0, 0, 0 }] = 7;\n\tm[{ 0, 0, 4, 0, 1, 0 }] = 8;\n\tm[{ 0, 1, 2, 1, 0, 0 }] = 9;\n\n\tint N;\n\twhile(cin >> N, N){\n\t\tfor(int i = 0; i < N; i++){\n\t\t\tdouble x1, y1, x2, y2;\n\t\t\tcin >> x1 >> y1 >> x2 >> y2;\n\t\t\tl[i] = L(P(x1, y1), P(x2, y2));\n\t\t}\n\n\t\tfor(int i = 0; i < N; i++){\n\t\t\tG[i].clear();\n\t\t}\n\n\t\tUnionFind uf(N);\n\t\tfor(int i = 0; i < N; i++){\n\t\t\tfor(int j = i + 1; j < N; j++){\n\t\t\t\tif(intersectSS(l[i], l[j])) uf.unite(i, j);\n\t\t\t}\n\t\t}\n\t\tfor(int i = 0; i < N; i++){\n\t\t\tint p = uf.find(i);\n\t\t\tG[p].push_back(i);\n\t\t}\n\n\t\tint ans[10] = { 0 };\n\t\tfor(int i = 0; i < N; i++){\n\t\t\tint res = check(G[i]);\n\t\t\tif(res != -1) ans[res]++;\n\t\t}\n\n\t\tfor(int i = 0; i < 10; i++){\n\t\t\tcout << ans[i] << (i != 9 ? \" \" : \"\");\n\t\t}\n\t\tcout << endl;\n\t}\n\n\n}", "accuracy": 1, "time_ms": 40, "memory_kb": 3436, "score_of_the_acc": -0.0674, "final_rank": 12 }, { "submission_id": "aoj_1288_2553843", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\n#define r(i,n) for(int i=0;i<n;i++)\ntypedef double D;\ntypedef complex<D> P;\ntypedef pair<P,P>L;\ntypedef vector<L> VP;\nconst D EPS = 1e-9;\n#define fi first\n#define se second\n#define mk make_pair\n#define X real()\n#define Y imag()\n#define LE(n,m) ((n) < (m) + EPS)\n#define GE(n,m) ((n) + EPS > (m))\n#define EQ(n,m) (abs((n)-(m)) < EPS)\nD dot(P a, P b) {return (conj(a)*b).X; }\nD cross(P a, P b) { return (conj(a)*b).Y;}\nint ccw(P a, P b, P c) {\n b -= a; c -= a;\n if (cross(b,c) > EPS) return +1;\n if (cross(b,c) < -EPS) return -1;\n if (dot(b,c) < -EPS) return +2;\n if (norm(b) < norm(c)) return -2; \n return 0;\n}\nbool isecLP(P a1, P a2, P b) {\n return abs(ccw(a1, a2, b)) != 1;\n}\nbool isecLL(P a1, P a2, P b1, P b2) {\n return !isecLP(a2-a1, b2-b1, 0) || isecLP(a1, b1, b2);\n}\nbool isecLS(P a1, P a2, P b1, P b2) {\n return cross(a2-a1, b1-a1) * cross(a2-a1, b2-a1) < EPS;\n}\nbool isecSS(P a1, P a2, P b1, P b2) {\n return ccw(a1, a2, b1)*ccw(a1, a2, b2) <= 0 &&\n ccw(b1, b2, a1)*ccw(b1, b2, a2) <= 0;\n}\nbool isecSP(P a1, P a2, P b) {\n return !ccw(a1, a2, b);\n // return abs(a1 - b) + abs(a2 - b) - abs(a2 - a1) < EPS; //Perfective\n}\ndouble x3,x4,y3,y4;\nVP v,t,x,cv,ch;\nint n,a[10];\nbool ch_dfs(int p){\n r(i,v.size())if(i!=p){\n if(v[p].first==v[i].first)return 1;\n if(v[p].first==v[i].second)return 1;\n if(v[p].second==v[i].first)return 1;\n if(v[p].second==v[i].second)return 1;\n }\n return 0;\n}\nvoid dfs(int d){\n r(i,cv.size()){\n r(j,v.size()){\n if(v[j].first==cv[i].first||v[j].first==cv[i].second||\n v[j].second==cv[i].first||v[j].second==cv[i].second){\n cv.push_back(v[j]);\n v.erase(v.begin()+j);\n j--;\n dfs(d+1);\n return ;\n }\n }\n }\n if(d==2){\n r(i,t.size()){\n r(j,cv.size()){\n if(isecSS(cv[j].fi,cv[j].se,t[i].fi,t[i].se)){\n t.erase(t.begin()+i);\n i--;\n }\n }\n }\n a[4]++;\n }\n else if(d==3){\n int flag=0;\n r(i,t.size()){\n r(j,cv.size()){\n if(isecSS(cv[j].fi,cv[j].se,t[i].fi,t[i].se)){\n t.erase(t.begin()+i);\n flag++;\n i--;\n }\n }\n }\n if(flag)a[3]++;\n else a[7]++;\n }\n else if(d==4){\n int flag=0;\n r(i,cv.size()){\n r(j,cv.size()){\n if(cv[i].first==cv[j].first) flag++;\n else if(cv[i].first==cv[j].second) flag++;\n else if(cv[i].second==cv[j].first) flag++;\n else if(cv[i].second==cv[j].second) flag++;\n }\n }\n if(flag==12){\n flag=0;\n r(i,t.size()){\n r(j,cv.size()){\n if(isecSS(cv[j].fi,cv[j].se,t[i].fi,t[i].se)){\n t.erase(t.begin()+i);\n flag++;\n i--;\n }\n }\n }\n if(flag) a[8]++;\n else a[0]++;\n }\n else a[9]++;\n }\n else{\n ch.clear();\n r(i,cv.size()){\n int f=0;\n r(j,cv.size()){\n if(i!=j){\n if(cv[i].first==cv[j].first) f++;\n else if(cv[i].first==cv[j].second) f++;\n else if(cv[i].second==cv[j].first) f++;\n else if(cv[i].second==cv[j].second) f++; \n }\n }\n if(f==1){\n ch.push_back(cv[i]);\n cv.erase(cv.begin()+i);\n break;\n }\n }\n r(i,4){\n r(j,cv.size()){\n if(ch[i].first==cv[j].first||ch[i].first==cv[j].second||\n ch[i].second==cv[j].first||ch[i].second==cv[j].second){\n ch.push_back(cv[j]);\n cv.erase(cv.begin()+j);\n break;\n }\n }\n }\n if(isecSS(ch[0].fi,ch[0].se,ch[3].fi,ch[3].se)||isecSS(ch[1].fi,ch[1].se,ch[4].fi,ch[4].se)){\n a[6]++;\n return ;\n }\n int fl=0;\n P p;\n if(ch[0].first==ch[1].first)p=ch[1].second,fl++;\n else if(ch[0].first==ch[1].second)p=ch[1].first,fl++;\n if(fl){\n if(ccw(ch[0].second,ch[0].first,p)==-1)a[2]++;\n else a[5]++;\n return;\n }\n if(ch[0].second==ch[1].first)p=ch[1].second;\n else if(ch[0].second==ch[1].second) p=ch[1].first;\n if(ccw(ch[0].first,ch[0].second,p)==-1)a[2]++;\n else a[5]++;\n }\n}\nint main(){\n while(cin>>n,n){\n t.clear();\n memset(a,0,sizeof(a));\n r(i,n){\n cin>>x3>>y3>>x4>>y4;\n v.push_back(mk(P(x3,y3),P(x4,y4)));\n }\n r(i,(int)v.size()){\n if(!ch_dfs(i)){\n t.push_back(v[i]);\n v.erase(v.begin()+i);\n i--;\n }\n }\n while(!v.empty()){\n cv.clear();\n cv.push_back(v[0]);\n v.erase(v.begin());\n dfs(1);\n }\n a[1]=t.size();\n r(i,10){\n if(i)cout<<' ';\n cout<<a[i];\n }\n cout<<endl;\n }\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3332, "score_of_the_acc": -0.0442, "final_rank": 9 }, { "submission_id": "aoj_1288_2520216", "code_snippet": "#include <bits/stdc++.h>\n \n#define _overload(_1,_2,_3,name,...) name\n#define _rep(i,n) _range(i,0,n)\n#define _range(i,a,b) for(int i=int(a);i<int(b);++i)\n#define rep(...) _overload(__VA_ARGS__,_range,_rep,)(__VA_ARGS__)\n \n#define _rrep(i,n) _rrange(i,n,0)\n#define _rrange(i,a,b) for(int i=int(a)-1;i>=int(b);--i)\n#define rrep(...) _overload(__VA_ARGS__,_rrange,_rrep,)(__VA_ARGS__)\n \n#define _all(arg) begin(arg),end(arg)\n#define uniq(arg) sort(_all(arg)),(arg).erase(unique(_all(arg)),end(arg))\n#define getidx(ary,key) lower_bound(_all(arg),key)-begin(arg)\n#define clr(a,b) memset((a),(b),sizeof(a))\n#define bit(n) (1LL<<(n))\n \nusing namespace std;\n \ntemplate<class T>void reg(vector<T> &ary,const T &elem){ary.emplace_back(elem);}\ntemplate<class T>bool chmin(T &a, const T &b) {return (b<a)?(a=b,1):0;}\ntemplate<class T>bool chmax(T &a, const T &b) { return (a<b)?(a=b,1):0;}\n \nusing R=long double; // __float128\nconst R EPS = 1E-11; // [-1000:1000]->EPS=1e-8 [-10000:10000]->EPS=1e-7\nconst R INF = 1E40;\nconstexpr R PI = acos(R(-1));\ninline int sgn(const R& r){ return (r > EPS) - (r < -EPS);}\ninline int sgn(const R& a, const R &b){ return sgn(a-b); }\ninline R sq(R x){return sqrt(max<R>(x,0.0));}\n \nusing P=complex<R>;\nusing VP=vector<P>;\nusing L=struct{P s,t;};\nusing VL=vector<L>;\nusing C=struct{P c;R r;};\nusing VC=vector<C>;\nusing vi=vector<int>;\n \nconstexpr P O = P(0,0);\nistream& operator >> (istream& is,P& p){ R x,y;is >> x >> y; p=P(x,y); return is;}\nostream& operator << (ostream& os,P& p){ os << real(p) << \" \" << imag(p); return os;}\n \nnamespace std{\n bool operator < (const P& a,const P& b){ return sgn(real(a-b))?real(a-b)<0:sgn(imag(a-b))<0;}\n bool operator == (const P& a,const P& b){ return sgn(real(a-b))==0 && sgn(imag(a-b))==0;}\n}\n \ninline bool cmp_x(const P& p,const P& q){return sgn(real(p-q))?real(p)<real(q):sgn(imag(p-q));}\ninline bool cmp_y(const P& a, const P& b){return sgn(imag(a-b)) ? imag(a-b)<0 : sgn(real(a-b))<0;}\ninline bool cmp_a(const P& a, const P& b){return sgn(arg(a)-arg(b)) ? arg(a)-arg(b)<0 : sgn(norm(a)-norm(b))<0;}\nbool operator < (const L& a,const L& b){ return a.s==b.s?a.t<b.t:a.s<b.s;}\nbool operator == (const L& a,const L& b){ return a.s==b.s&&a.t==b.t;}\n \n//?????? dot ?????? det\ninline R dot(P o,P a,P b){a-=o,b-=o; return real(conj(a)*b);}\ninline R det(P o,P a,P b){a-=o,b-=o; return imag(conj(a)*b);}\ninline P vec(L l){return l.t-l.s;}\n \n// ?°???± verify AOJ CGL_1_A\nP proj(P o,P a,P b){ a-=o,b-=o; return a*real(b/a);}\nP proj(L l,P p){l.t-=l.s,p-=l.s;return l.s+l.t*real(p/l.t);}\n// ????°? verify AOJ CGL_1_B\nP refl(L l,P p){ return R(2.0)*proj(l,p)-p;}\n// CCW verify AOJ CGL_1_C\nenum CCW{ LEFT = 1,RIGHT = 2,BACK = 4,FRONT = 8,ON = 16};\ninline int ccw(P o,P a, P b) {//???a??¨???b???????????????????????????\n if (sgn(det(o,a,b)) > 0) return LEFT; // counter clockwise\n if (sgn(det(o,a,b)) < 0) return RIGHT; // clockwise\n if (sgn(dot(o,a,b)) < 0) return BACK; // b--base--a on line\n if (sgn(norm(a-o)-norm(b-o)) < 0) return FRONT; // base--a--b on line\n return ON;// base--b--a on line a??¨b????????????????????????\n}\n \n// ?????´ ?????? verify AOJ CGL_2_A\nbool vertical(L a, L b) {return sgn(dot(O,vec(a),vec(b)))==0;}\nbool parallel(L a, L b) {return sgn(det(O,vec(a),vec(b)))==0;}\n\n// ???????????????verify AOJ CGL_2_B ???????????????????????´??????1,????????´??????0\nbool ill(L a,L b){ return parallel(a,b)==false;}\nbool ils(L l,L s,int end=0){ return sgn(det(l.s,l.t,s.s)*det(l.s,l.t,s.t))<=-end;}\nbool iss(L a,L b,int end=0){\n int s1=ccw(a.s,a.t,b.s)|ccw(a.s,a.t,b.t);\n int s2=ccw(b.s,b.t,a.s)|ccw(b.s,b.t,a.t);\n if(end) return (s1&s2)==(LEFT|RIGHT);\n return (s1|s2)&ON || (s1&s2)==(LEFT|RIGHT);\n}\n\nR dsp(L s,P p){\n if(sgn(dot(s.s,s.t,p))<=0) return abs(p-s.s);\n if(sgn(dot(s.t,s.s,p))<=0) return abs(p-s.t);\n return abs(det(s.s,s.t,p))/abs(s.t-s.s);\n}\n\nstruct UnionFind {\n vector<int> data;\n UnionFind(int size) : data(size, -1) { }\n bool unionSet(int x, int y) {\n x = root(x); y = root(y);\n if (x != y) {\n if (data[y] < data[x]) swap(x, y);\n data[x] += data[y]; data[y] = x;\n }\n return x != y;\n }\n bool findSet(int x, int y) {\n return root(x) == root(y);\n }\n int root(int x) {\n return data[x] < 0 ? x : data[x] = root(data[x]);\n }\n int size(int x) {\n return -data[root(x)];\n }\n};\n\ninline bool share(L& a, L& b){\n return (a.s == b.s or a.s == b.t or a.t == b.s or a.t == b.t);\n}\n\nint check(VL& ls){\n int n = ls.size();\n\n if(n == 1) return 1;\n if(n == 3 or n == 4){\n int cnt = 0;\n rep(i, n){\n rep(j, i + 1, n){\n if(share(ls[i], ls[j])){\n cnt++;\n }\n }\n }\n if(n == 3){\n if(cnt == 1) return 4;\n else if(cnt == 2) return 7;\n else assert(false);\n }\n if(n == 4){\n if(cnt == 2) return 3;\n else if(cnt == 3) return 9;\n else if(cnt == 4) return 0;\n else assert(false);\n }\n }\n if(n == 5){\n rep(i, n){\n bool ok = false;\n rep(j, n){\n if(j == i) continue;\n if(share(ls[i], ls[j])){\n ok = true;\n break;\n }\n }\n if(not ok) return 8;\n }\n\n P s;\n [&]{\n rep(i, n){\n for(P p : {ls[i].s, ls[i].t}){\n bool ok = true;\n rep(j, n){\n if(j == i) continue;\n if(dsp(ls[j], p) < EPS){\n ok = false;\n break;\n }\n }\n if(ok){\n s = p;\n return;\n }\n }\n }\n }();\n\n VP ps = {s};\n vi used(n);\n rep(loop, n){\n rep(i, n){\n if(used[i]) continue;\n if(ls[i].s == ps.back()){\n ps.emplace_back(ls[i].t);\n used[i] = true;\n break;\n }\n if(ls[i].t == ps.back()){\n ps.emplace_back(ls[i].s);\n used[i] = true;\n break;\n }\n }\n }\n vi dirs(4);\n rep(i, 4){\n dirs[i] = sgn(det(O, ps[i + 1] - ps[i], ps[i + 2] - ps[i + 1]));\n }\n if(dirs[0] == dirs[1] and dirs[1] == dirs[2] and dirs[2] == dirs[3]){\n return 6;\n }\n if(dirs[0] == 1 and dirs[1] == 1 and dirs[2] == -1 and dirs[3] == -1){\n return 5;\n }\n if(dirs[0] == -1 and dirs[1] == -1 and dirs[2] == 1 and dirs[3] == 1){\n return 2;\n }\n assert(false);\n }\n\n assert(false);\n}\n\nint main(void){\n cin.tie(0);\n ios::sync_with_stdio(false);\n\n for(int n; cin >> n, n;){\n VL ls(n);\n for(auto& e : ls) cin >> e.s >> e.t;\n\n UnionFind uf(n);\n rep(i, n){\n rep(j, i + 1, n){\n if(iss(ls[i], ls[j])){\n uf.unionSet(i, j);\n }\n }\n }\n\n vi used(n);\n vi res(10);\n rep(i, n){\n if(used[i]) continue;\n\n VL cur;\n rep(j, i, n){\n if(uf.findSet(i, j)){\n cur.emplace_back(ls[j]);\n used[j] = true;\n }\n }\n\n res[check(cur)]++;\n }\n\n rep(i, 10){\n cout << (i ? \" \":\"\") << res[i];\n }\n cout << endl;\n }\n\n return 0;\n}", "accuracy": 1, "time_ms": 240, "memory_kb": 3348, "score_of_the_acc": -0.1054, "final_rank": 15 }, { "submission_id": "aoj_1288_2191232", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\ntypedef long long ll;\ntypedef pair<int,int> P;\n#define X first\n#define Y second\nbool on(P p1,P p2,P p3){\n P v1=P(p2.X-p1.X,p2.Y-p1.Y),v2=P(p3.X-p1.X,p3.Y-p1.Y);\n if(v1.X*v2.X<0||v1.Y*v2.Y<0) return 0;\n if(!v1.X) return !v2.X&&abs(v2.Y)<abs(v1.Y);\n if(!v1.Y) return !v2.Y&&abs(v2.X)<abs(v1.X);\n if(v1.X*v1.X+v1.Y*v1.Y<v2.X*v2.X+v2.Y*v2.Y) return 0;\n return v1.X*v2.Y-v1.Y*v2.X==0;\n}\nint main(){\n int n;\n while(cin>>n,n){\n P ed[2][n];\n for(int i=0;i<n;i++)\n cin>>ed[0][i].X>>ed[0][i].Y>>ed[1][i].X>>ed[1][i].Y;\n vector<int> G[n],G2[n];\n for(int i=0;i<n;i++){\n for(int j=0;j<n;j++){\n\tif(i==j) continue;\n\tbool f=0;\n\tfor(int x=0;x<2;x++)\n\t for(int y=0;y<2;y++)\n\t f|=ed[x][i]==ed[y][j];\n\tif(f){\n\t G[i].push_back(j);\n\t continue;\n\t}\n\tfor(int x=0;x<2;x++)\n\t f|=on(ed[0][i],ed[1][i],ed[x][j]);\n\tif(f) G2[i].push_back(j),G2[j].push_back(i);\n }\n }\n int ans[10]={};\n bool used[n];\n memset(used,0,sizeof(used));\n for(int i=0;i<n;i++){\n if(used[i]) continue;\n int ec=0;\n map<P,int> mp;\n vector<int> v;\n queue<int> q;\n q.push(i);\n while(!q.empty()){\n\tint p=q.front();q.pop();\n\tif(used[p]) continue;\n\tused[p]=1;\n\tmp[ed[0][p]]++;\n\tmp[ed[1][p]]++;\n\tv.push_back(p);\n\tfor(int u:G[p]) q.push(u);\n\tfor(int u:G2[p]) q.push(u),ec++;\n }\n ec/=2;\n int m=v.size(),o=mp.size();\n if(m==1) ans[1]++;\n if(m==3){\n\tif(o==5) ans[4]++;\n\tif(o==4) ans[7]++;\n }\n if(m==4){\n\tif(o==4) ans[0]++;\n\tif(o==6) ans[3]++;\n\tif(o==5) ans[9]++;\n }\n if(m==5){\n\tif(ec==2) ans[8]++;\n\tif(ec==1) ans[6]++;\n\tif(ec==0){\n\t int a,b,c,d;\n\t for(int j=0;j<m;j++){\n\t if(mp[ed[0][v[j]]]==1){\n\t a=v[j];b=0;\n\t break;\n\t }\n\t if(mp[ed[1][v[j]]]==1){\n\t a=v[j];b=1;\n\t break;\n\t }\n\t }\n\t for(int j=0;j<m;j++){\n\t if(a==v[j]) continue; \n\t if(ed[!b][a]==ed[0][v[j]]){\n\t c=v[j];d=1;\n\t break;\n\t }\n\t if(ed[!b][a]==ed[1][v[j]]){\n\t c=v[j];d=0;\n\t break;\n\t }\n\t }\n\t P v1=P(ed[b][a].X-ed[!b][a].X,ed[b][a].Y-ed[!b][a].Y);\n\t P v2=P(ed[d][c].X-ed[!d][c].X,ed[d][c].Y-ed[!d][c].Y);\n\t \n\t if(v1.X*v2.Y-v1.Y*v2.X>0) ans[2]++;\n\t else ans[5]++;\n\t}\n }\n }\n for(int i=0;i<10;i++) cout<<ans[i]<<\" \\n\"[i==9];\n }\n return 0;\n}", "accuracy": 1, "time_ms": 40, "memory_kb": 3104, "score_of_the_acc": -0.0178, "final_rank": 3 }, { "submission_id": "aoj_1288_2009027", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n#define FOR(i,k,n) for(int i = (int)(k); i < (int)(n); i++)\n#define REP(i,n) FOR(i,0,n)\n#define ALL(a) a.begin(), a.end()\n#define MS(m,v) memset(m,v,sizeof(m))\ntypedef long long ll;\ntypedef long double ld;\ntypedef vector<int> vi;\ntypedef vector<string> vs;\ntypedef pair<int, int> pii;\nconst int MOD = 1000000007;\nconst int INF = MOD + 1;\nconst ld EPS = 1e-12;\ntemplate<class T> T &chmin(T &a, const T &b) { return a = min(a, b); }\ntemplate<class T> T &chmax(T &a, const T &b) { return a = max(a, b); }\n\n/*--------------------template--------------------*/\n\ntypedef long double ld;\nconst ld PI = acos(-1.0);\nbool eq(ld a, ld b) { return abs(a - b) < EPS; }\ntypedef complex<ld> Point;\ntypedef vector<Point> Polygon;\n\nnamespace std\n{\n\tbool operator < (const Point& a, const Point& b)\n\t{\n\t\treturn real(a) != real(b) ? real(a) < real(b) : imag(a) < imag(b);\n\t}\n}\n\nstruct Line\n{\n\tPoint a, b;\n\tLine() {};\n\tLine(Point p, Point q) :a(p), b(q) {};\n\tLine(ld x1, ld y1, ld x2, ld y2) :a(Point(x1, y1)), b(Point(x2, y2)) {};\n};\n\nld dot(Point a, Point b)\n{\n\treturn real(conj(a)*b);\n}\n\nld cross(Point a, Point b)\n{\n\treturn imag(conj(a)*b);\n}\n\nint ccw(Point a, Point b, Point c)\n{\n\tb -= a; c -= a;\n\tif (cross(b, c) > EPS) return 1; //counter cloclwise\n\tif (cross(b, c) < -EPS) return -1; //cloclwise\n\tif (dot(b, c) < 0) return 2; //c--a--b on line \n\tif (norm(b) < norm(c)) return -2; //a--b--c on line\n\treturn 0; //a--c--b on line\n}\n\nbool isis_ss(Line s, Line t)\n{\n\treturn (ccw(s.a, s.b, t.a)*ccw(s.a, s.b, t.b) <= 0 &&\n\t\tccw(t.a, t.b, s.a)*ccw(t.a, t.b, s.b) <= 0);\n}\n\nbool isis_sp(Line s, Point p)\n{\n\treturn (abs(s.a - p) + abs(s.b - p) - abs(s.b - s.a)) < EPS;\n}\n\nint label(Line s, vector<Line> segments)\n{\n\tint edge = 0, mid = 0;\n\tfor (auto i : segments)\n\t{\n\t\tif (((s.a == i.a) && (s.b == i.b)) || !isis_ss(s, i)) continue;\n\t\tif (s.a == i.a || s.a == i.b || s.b == i.a || s.b == i.b) edge++;\n\t\telse if (isis_sp(i, s.a) || isis_sp(i, s.b)) mid++;\n\t}\n\tif (edge == 2 && mid == 0) return 0;\n\tif (edge == 0 && mid == 0) return 1;\n\tif (edge == 1 && mid == 0) return 2;\n\tif (edge == 0 && mid == 1) return 3;\n\tif (edge == 1 && mid == 1) return 4;\n\tif (edge == 0 && mid == 2) return 5;\n\tassert(false);\n}\n\nint ident(vi v)\n{\n\tvector<vi> lis(10);\n\tlis[0] = { 4,0,0,0,0,0 };\n\tlis[1] = { 0,1,0,0,0,0 };\n\tlis[2] = { 3,0,2,0,0,0 };\n\tlis[3] = { 1,0,2,1,0,0 };\n\tlis[4] = { 0,1,1,0,1,0 };\n\tlis[5] = { 3,0,2,0,0,0 };\n\tlis[6] = { 3,0,1,0,1,0 };\n\tlis[7] = { 1,0,2,0,0,0 };\n\tlis[8] = { 4,0,0,0,0,1 };\n\tlis[9] = { 2,0,1,0,1,0 };\n\tREP(i, 10)\n\t{\n\t\tif (v == lis[i]) return i;\n\t}\n\tassert(false);\n}\n\nint main()\n{\n\tcin.sync_with_stdio(false); cout << fixed << setprecision(10);\n\tint n;\n\twhile (cin >> n, n)\n\t{\n\t\tvector<Line> seg;\n\t\tREP(i, n)\n\t\t{\n\t\t\tld a, b, c, d;\n\t\t\tcin >> a >> b >> c >> d;\n\t\t\tseg.emplace_back(a, b, c, d);\n\t\t}\n\t\tvector<vi> g(n);\n\t\tREP(i, n)REP(j, n)\n\t\t{\n\t\t\tif (i == j) continue;\n\t\t\tif (isis_ss(seg[i], seg[j]))\n\t\t\t{\n\t\t\t\tg[i].push_back(j);\n\t\t\t\tg[j].push_back(i);\n\t\t\t}\n\t\t}\n\t\tvector<vi> group;\n\t\tvector<bool> vis(n);\n\t\tREP(i, n)\n\t\t{\n\t\t\tif (vis[i]) continue;\n\t\t\tvi tmp;\n\t\t\tvis[i] = true;\n\t\t\tqueue<int> que;\n\t\t\tque.push(i);\n\t\t\ttmp.push_back(i);\n\t\t\twhile (!que.empty())\n\t\t\t{\n\t\t\t\tint t = que.front();\n\t\t\t\tque.pop();\n\t\t\t\tfor (auto j : g[t])\n\t\t\t\t{\n\t\t\t\t\tif (vis[j]) continue;\n\t\t\t\t\tvis[j] = true;\n\t\t\t\t\tque.push(j);\n\t\t\t\t\ttmp.push_back(j);\n\t\t\t\t}\n\t\t\t}\n\t\t\tgroup.push_back(tmp);\n\t\t}\n\t\tvi ans(10);\n\t\tfor (auto i : group)\n\t\t{\n\t\t\tvector<Line> segments;\n\t\t\tfor (auto j : i) segments.push_back(seg[j]);\n\t\t\tvi types(6);\n\t\t\tfor (auto j : segments)\n\t\t\t{\n\t\t\t\tint t = label(j, segments);\n\t\t\t\ttypes[t]++;\n\t\t\t}\n\t\t\tint res = ident(types);\n\t\t\tif (res == 2)\n\t\t\t{\n\t\t\t\tLine a, b;\n\t\t\t\tfor (auto j : segments)\n\t\t\t\t{\n\t\t\t\t\tif (label(j, segments) == 2) a = j;\n\t\t\t\t}\n\t\t\t\tfor (auto j : segments)\n\t\t\t\t{\n\t\t\t\t\tif ((a.a == j.a) && (a.b == j.b)) continue;\n\t\t\t\t\tif (isis_ss(a, j)) b = j;\n\t\t\t\t}\n\t\t\t\tPoint p, q, r;\n\t\t\t\tif (a.a == b.a)\n\t\t\t\t{\n\t\t\t\t\tp = a.b, q = a.a, r = b.b;\n\t\t\t\t}\n\t\t\t\tif (a.a == b.b)\n\t\t\t\t{\n\t\t\t\t\tp = a.b, q = a.a, r = b.a;\n\t\t\t\t}\n\t\t\t\tif (a.b == b.a)\n\t\t\t\t{\n\t\t\t\t\tp = a.a, q = a.b, r = b.b;\n\t\t\t\t}\n\t\t\t\tif (a.b == b.b)\n\t\t\t\t{\n\t\t\t\t\tp = a.a, q = a.b, r = b.a;\n\t\t\t\t}\n\t\t\t\tif (ccw(p, q, r) == 1) res = 5;\n\t\t\t\telse if (ccw(p, q, r) == -1) res = 2;\n\t\t\t}\n\t\t\tans[res]++;\n\t\t}\n\t\tREP(i, 10) cout << ans[i] << (i == 9 ? \"\\n\" : \" \");\n\t}\n\treturn 0;\n}", "accuracy": 1, "time_ms": 320, "memory_kb": 3468, "score_of_the_acc": -0.1438, "final_rank": 16 }, { "submission_id": "aoj_1288_1965110", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\ntypedef vector<int> VI;\ntypedef vector<VI> VVI;\ntypedef vector<string> VS;\ntypedef pair<int, int> PII;\ntypedef long long LL;\ntypedef pair<LL, LL> PLL;\n\n#define ALL(a) (a).begin(),(a).end()\n#define RALL(a) (a).rbegin(), (a).rend()\n#define PB push_back\n#define EB emplace_back\n#define MP make_pair\n#define SZ(a) int((a).size())\n#define EACH(i,c) for(typeof((c).begin()) i=(c).begin(); i!=(c).end(); ++i)\n#define EXIST(s,e) ((s).find(e)!=(s).end())\n#define SORT(c) sort((c).begin(),(c).end())\n\n#define FOR(i,a,b) for(int i=(a);i<(b);++i)\n#define REP(i,n) FOR(i,0,n)\n\n#define FF first\n#define SS second\ntemplate<class S, class T>\nistream& operator>>(istream& is, pair<S,T>& p){\n return is >> p.FF >> p.SS;\n}\n\nconst double EPS = 1e-10;\nconst double PI = acos(-1.0);\nconst LL MOD = 1e9+7;\n\nusing Seg = pair<PII,PII>;\nint dot(PII p1, PII p2){\n return p1.FF * p2.FF + p1.SS * p2.SS;\n}\nint cross(PII p1, PII p2){\n return p1.FF * p2.SS - p1.SS * p2.FF;\n}\nPII operator-(const PII& l, const PII& r){\n return PII(l.FF - r.FF, l.SS - r.SS);\n}\n \nbool isp(Seg s1, Seg s2){\n return s1.FF == s2.FF || s1.FF == s2.SS || s1.SS == s2.FF || s1.SS == s2.SS;\n}\nbool on(PII p, Seg s){\n return p != s.FF && p != s.SS && cross(s.FF-p, s.SS-p) == 0 && dot(s.FF-p, s.SS-p) < 0;\n}\nbool ist(Seg s1, Seg s2){\n return on(s1.FF, s2) || on(s1.SS, s2) || on(s2.FF, s1) || on(s2.SS, s1);\n}\n\nvoid dfs(int u, VVI& ps, VVI& ts, vector<bool>& use, PII& res, VI& buf){\n buf.PB(u);\n use[u] = true;\n for(int v: ps[u]){\n\t++res.FF;\n\tif(!use[v])\n\t dfs(v, ps, ts, use, res, buf);\n }\n for(int v: ts[u]){\n\t++res.SS;\n\tif(!use[v])\n\t dfs(v, ps, ts, use, res, buf);\n }\n}\n\nbool istwo(Seg s1, Seg s2){\n if(s1.SS != s2.FF) swap(s1.FF, s1.SS);\n if(s1.SS != s2.FF) swap(s2.FF, s2.SS);\n if(s1.SS != s2.FF) swap(s1.FF, s1.SS);\n return cross(s1.SS-s1.FF, s2.SS-s2.FF) < 0;\n}\n\nint main(){\n cin.tie(0);\n ios_base::sync_with_stdio(false);\n\n int N;\n while(cin>>N,N){\n\tvector<Seg> ss(N);\n\tREP(i,N) cin >> ss[i];\n\n\tVVI isps(N), ists(N);\n\tREP(i,N) REP(j,i){\n\t if(isp(ss[i], ss[j])){\n\t\tisps[i].PB(j);\n\t\tisps[j].PB(i);\n\t }\n\t else if(ist(ss[i], ss[j])){\n\t\tists[i].PB(j);\n\t\tists[j].PB(i);\n\t }\n\t}\n\n\tvector<bool> use(N);\n\tVI ans(10);\n\tREP(i,N){\n\t if(use[i]) continue;\n\t VI buf;\n\t PII tmp;\n\t dfs(i, isps, ists, use, tmp, buf);\n\t tmp.FF /= 2;\n\t tmp.SS /= 2;\n// 0 1 2 3 4 5 6 7 8 9\n//lines:\n// 4 1 5 4 3 5 5 3 5 4\n//points:\n// 4 0 4 2 1 4 4 2 4 3\n//touching:\n// 0 0 0 1 1 0 1 0 2 1\n\t switch(SZ(buf)){\n\t case 1:\n\t\tans[1]++; break;\n\t case 3:\n\t\tans[(tmp.FF==1?4:7)]++; break;\n\t case 4:\n\t\tif(tmp.FF==4) ans[0]++;\n\t\telse if(tmp.FF==2) ans[3]++;\n\t\telse ans[9]++;\n\t\tbreak;\n\t case 5:\n\t\tif(tmp.SS==1) ans[6]++;\n\t\telse if(tmp.SS==2) ans[8]++;\n\t\telse{\n\t\t int e1 = -1;\n\t\t for(int x:buf) if(SZ(isps[x]) == 1) e1 = x;\n\t\t int e2 = isps[e1][0];\n\t\t ans[(istwo(ss[e1], ss[e2])?2:5)]++;\n\t\t}\n\t\tbreak;\n\t }\n\t}\n\n\tREP(i,10) cout << (i?\" \":\"\") << ans[i]; cout << endl;\n }\n\n return 0;\n}", "accuracy": 1, "time_ms": 20, "memory_kb": 3224, "score_of_the_acc": -0.0306, "final_rank": 6 }, { "submission_id": "aoj_1288_1889147", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\n\n\ntypedef complex<double> P; //Point\ntypedef pair<P,P> L; //Line, Segment\n\nconst double EPS = 1e-8;\nconst double PI = 6.0 * asin(0.5);\n\nnamespace std {\n bool operator < (const P& a, const P& b){\n return fabs(real(a)-real(b)) < EPS ? imag(a) < imag(b) : real(a) < real(b);\n }\n bool operator == (const P&a , const P& b) {\n return abs(a-b)<EPS;\n } \n}\n\ndouble dot(P a, P b){ return a.real() * b.real() + a.imag() * b.imag(); }\ndouble cross(P a, P b){ return a.real() * b.imag() - a.imag() * b.real(); }\n\nint ccw(P a, P b, P c){\n b -= a;\n c -= a;\n if(cross(b,c) > EPS) return 1; //テ・ツ渉催ヲツ卍づィツィツ暗・ツ崢榲」ツつ?\n if(cross(b,c) < -EPS) return -1; // テヲツ卍づィツィツ暗・ツ崢榲」ツつ?\n if(dot(b,c) < -EPS) return 2; // c -- a -- b テ」ツ?ョテ、ツクツ?ァツ崢エテァツキツ?\n if(norm(b) < norm(c)) return -2; // a -- b -- c テ」ツ?ョテ、ツクツ?ァツ崢エテァツキツ?\n return 0; // a -- c -- b テ」ツ?ョテ、ツクツ?ァツ崢エテァツキツ?\n}\n\nbool isIntersect(L s1, L s2){ \n if(max(real(s1.first), real(s1.second)) + EPS < min(real(s2.first), real(s2.second)) ||\n max(imag(s1.first), imag(s1.second)) + EPS < min(imag(s2.first), imag(s2.second)) ||\n max(real(s2.first), real(s2.second)) + EPS < min(real(s1.first), real(s1.second)) ||\n max(imag(s2.first), imag(s2.second)) + EPS < min(imag(s1.first), imag(s1.second))) return false;\n\n return ( ccw(s1.first,s1.second,s2.first) * ccw(s1.first,s1.second,s2.second) <= 0 &&\n ccw(s2.first,s2.second,s1.first) * ccw(s2.first,s2.second,s1.second) <= 0 );\n}\n\nP crossPoint(L l, L m){\n double A = cross(l.second - l.first, m.second - m.first);\n double B = cross(l.second - l.first, l.second - m.first);\n if(fabs(A) < EPS && fabs(B) < EPS) return m.first;\n else if(fabs(A) >= EPS) return m.first + B / A * (m.second - m.first);\n}\n\nvector<int> G[1111];\nbool used[1111];\n\nvoid dfs(int id,vector<int> &v){\n if( used[id] ) return;\n used[id] = true;\n for( int to : G[id] ) dfs( to, v );\n v.push_back( id );\n}\n\n\n\nbool zorq(const vector<int> &v,const vector<L>& ss){\n for(int i : v ){\n for(int j : v ){\n if( i == j ) continue;\n if( isIntersect( ss[i], ss[j] ) ){\n P p = crossPoint( ss[i], ss[j] ); \n if( p != ss[i].first && p != ss[i].second ) return true;\n }\n }\n }\n return false;\n}\n\nint torf(const vector<int> &v, const vector<L>& ss ){\n for( int i : v ){\n if( G[i].size() == 1 ){\n int to = G[i][0];\n int nx = G[to][0];\n if( nx == i ) nx = G[to][1];\n\n P p1 = ss[i].first;\n P p2 = crossPoint( ss[i], ss[to] );\n P p3 = crossPoint( ss[to],ss[nx] );\n if( p1 == p2 ) p1 = ss[i].second;\n if( ccw( p1, p2, p3 ) == 1 ) return 5;\n else return 2;\n }\n }\n return -1;\n}\n\nint num(int id,const vector<L>& ss){\n vector<int> v;\n dfs( id, v );\n\n if( v.size() == 1 ) return 1;\n int sum[11]={};\n for( int i : v ){\n sum[G[i].size()]++;\n }\n\n if( sum[1] == 3 && sum[3] == 1 ) return 3;\n if( sum[1] == 1 && sum[2] == 3 && sum[3] == 1 ) return 6;\n if( sum[2] == 3 && sum[3] == 2 ) return 8;\n if( sum[2] == 4 ) {\n // 0 or 9\n if( zorq( v, ss ) ) return 9;\n return 0;\n }\n if( sum[1] == 2 && sum[2] == 1 ) {\n // 7 or 4\n if( zorq( v, ss ) ) return 4;\n return 7;\n }\n return torf(v,ss);\n \n \n}\n\nint main(){\n int n;\n while( cin >> n && n ){\n vector<L> s;\n for(int i=0;i<n;i++) G[i].clear();\n memset( used,0,sizeof(used) );\n \n for(int i=0;i<n;i++){\n int x,y,x2,y2; cin>> x >>y >> x2 >> y2;\n s.push_back( L( P(x,y) , P(x2,y2) ) ); \n }\n\n for(int i=0;i<n;i++){\n for(int j=0;j<n;j++){\n if( i==j ) continue;\n if( isIntersect( s[i], s[j] ) ) {\n G[i].push_back( j );\n }\n }\n }\n \n\n int res[11]={};\n for(int i=0;i<n;i++){\n if( used[i] ) continue;\n int st = num(i,s) ;\n res[st]++;\n }\n for(int i=0;i<10;i++) {\n if( i ) cout << \" \";\n cout << res[i];\n }\n cout << endl;\n \n }\n}", "accuracy": 1, "time_ms": 40, "memory_kb": 3244, "score_of_the_acc": -0.0387, "final_rank": 7 }, { "submission_id": "aoj_1288_1889021", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\n#define MAX_N 1005\ntypedef complex<double> P;\ndouble eps=1e-6;\n\nP calc(P a,P b,P c,P d){\n a-=d;b-=d;c-=d;\n return d+a+(b-a)*imag(a/c)/imag(a/c-b/c);\n}\n\nbool isParallel(P a,P b,P c,P d){\n return ( abs( imag( (a-b)*conj(c-d) ) ) < eps );\n}\n\nbool eq(P a,P b){\n return ( abs(a-b) < eps );\n}\n\nint n;\nP A[MAX_N],B[MAX_N];\nint G[MAX_N][MAX_N];\nint ans[10];\nbool visited[MAX_N];\nvector<int> vec;\n\nint solve(){\n if(vec.size()==1)return 1;\n int cnt1=0,cnt2=0,size=vec.size();\n for(int i=0;i<size;i++){\n for(int j=0;j<i;j++){\n if(G[vec[i]][vec[j]]==1)cnt1++;\n if(G[vec[i]][vec[j]]==2)cnt2++;\n }\n }\n\n // cout<<cnt1<<' '<<cnt2<<' '<<size<<endl;\n P g= A[vec[0]];\n for(int i=0;i<size;i++){\n int id=vec[i];\n // cout<<A[id]-g<<' '<<B[id]-g<<endl;\n }\n if(size==3){\n if(cnt1==2&&cnt2==0)return 7;\n return 4;\n \n // if(cnt1==1&&cnt2==1)return 4;\n // assert(0);\n }\n if(size==4){\n if(cnt1==4&&cnt2==0)return 0;\n if(cnt1==3&&cnt2==1)return 9;\n if(cnt1==2&&cnt2==1)return 3;\n return 3;\n // assert(0);\n }\n if(size==5){\n if(cnt1==4&&cnt2==1)return 6;\n if(cnt1==4&&cnt2==2)return 8;\n if(cnt1==4&&cnt2==0){\n for(int i=0;i<size;i++){\n int C=0,D;\n for(int j=0;j<size;j++){\n if(i!=j&&G[vec[i]][vec[j]]){\n C++;\n D=j;\n }\n }\n \n\n if(C==1){\n P cp=calc(A[vec[i]],B[vec[i]],A[vec[D]],B[vec[D]]);\n\n P from,to,prev;\n if(eq(cp,A[vec[i]]))prev=B[vec[i]],from=A[vec[i]];\n else if(eq(cp,B[vec[i]]))prev=A[vec[i]],from=B[vec[i]];\n //else assert(0);\n \n if(eq(cp,A[vec[D]]))to=B[vec[D]];\n else if(eq(cp,B[vec[D]]))to=A[vec[D]];\n // else assert(0);\n\n P base=from-prev,target=to-prev;\n if( imag(target*conj(base)) < -eps )return 2;\n else if( imag(target*conj(base)) > eps )return 5;\n // else assert(0);\n }\n }\n }\n return 2;\n assert(0);\n }\n return -1;\n}\n\nvoid dfs(int pos){\n if(visited[pos])return;\n visited[pos]=true;\n vec.push_back(pos);\n for(int i=0;i<n;i++)\n if(G[pos][i]!=0)dfs(i);\n}\nbool eq(double a,double b){\n return (-eps < a-b && a-b < eps );\n}\nbool onSegment(P a,P b,P c){\n return eq( abs(a-b) , abs(a-c) + abs(b-c) );\n}\n\nint check(int si,int ti){\n if( isParallel(A[si],B[si],A[ti],B[ti]) )return 0;\n P k=calc(A[si],B[si],A[ti],B[ti]);\n int count=3;\n if( eq(k,A[si]) || eq(k,B[si]) )count--;\n if( eq(k,A[ti]) || eq(k,B[ti]) )count--;\n if( onSegment(A[si],B[si],k) && onSegment(A[ti],B[ti],k) )return count;\n return 0;\n}\n\nint main(){\n while(1){\n cin>>n;\n if(n==0)break;\n memset(G,0,sizeof(G));\n for(int i=0;i<n;i++){\n double ax,ay,bx,by;\n cin>>ax>>ay>>bx>>by;\n A[i]=P(ax,ay);\n B[i]=P(bx,by);\n for(int j=0;j<i;j++){\n G[i][j]=G[j][i]=check(i,j);\n }\n }\n memset(ans,0,sizeof(ans));\n memset(visited,false,sizeof(visited));\n for(int i=0;i<n;i++){\n if(visited[i])continue;\n vec.clear();\n dfs(i);\n \n ans[ solve() ]++;\n }\n for(int i=0;i<10;i++){\n if(i)cout<<' ';\n cout<<ans[i];\n }\n cout<<endl;\n }\n return 0;\n}", "accuracy": 1, "time_ms": 290, "memory_kb": 5256, "score_of_the_acc": -0.4032, "final_rank": 17 } ]
aoj_1291_cpp
Problem G: Search of Concatenated Strings The amount of information on the World Wide Web is growing quite rapidly. In this information explosion age, we must survive by accessing only the Web pages containing information relevant to our own needs. One of the key technologies for this purpose is keyword search. By using well-known search engines, we can easily access those pages containing useful information about the topic we want to know. There are many variations in keyword search problems. If a single string is searched in a given text, the problem is quite easy. If the pattern to be searched consists of multiple strings, or is given by some powerful notation such as regular expressions, the task requires elaborate algorithms to accomplish efficiently. In our problem, a number of strings (element strings) are given, but they are not directly searched for. Concatenations of all the element strings in any order are the targets of the search here. For example, consider three element strings aa, b and ccc are given. In this case, the following six concatenated strings are the targets of the search, i.e. they should be searched in the text. aabccc aacccb baaccc bcccaa cccaab cccbaa The text may contain several occurrences of these strings. You are requested to count the number of occurrences of these strings, or speaking more precisely, the number of positions of occurrences in the text. Two or more concatenated strings may be identical. In such cases, it is necessary to consider subtle aspects of the above problem statement. For example, if two element strings are x and xx, the string xxx is an occurrence of both the concatenation of x and xx and that of xx and x. Since the number of positions of occurrences should be counted, this case is counted as one, not two. Two occurrences may overlap. For example, the string xxxx has occurrences of the concatenation xxx in two different positions. This case is counted as two. Input The input consists of a number of datasets, each giving a set of element strings and a text. The format of a dataset is as follows. n m e 1 e 2 . . . e n t 1 t 2 . . . t m The first line contains two integers separated by a space. n is the number of element strings. m is the number of lines used to represent the text. n is between 1 and 12, inclusive. Each of the following n lines gives an element string. The length (number of characters) of an element string is between 1 and 20, inclusive. The last m lines as a whole give the text. Since it is not desirable to have a very long line, the text is separated into m lines by newlines, but these newlines should be ignored. They are not parts of the text. The length of each of these lines (not including the newline) is between 1 and 100, inclusive. The length of the text is between 1 and 5000, inclusive. The element strings and the text do not contain characters other than lowercase letters. The end of the input is indicated by a line containing two zeros separated by a space. CAUTIO ...(truncated)
[ { "submission_id": "aoj_1291_10851194", "code_snippet": "#include<iostream>\n#include<cstdio>\n#include<cstring>\n#include<algorithm>\nusing namespace std;\n\nconst int N=12;\nint i,j,n,m;\nchar s[6000],ss[6000];\nchar c[20][50];\nbool f[5500][1<<N];\nint len,ll,l[N+3];\nbool b[5030][N+1];\n\nvoid check(int i,int j)\n{\n for (int k=0;k<l[j];++k)\n {\n if (i>len) return;\n if (s[i++]!=c[j][k]) return;\n }\n b[i-1][j]=1;\n}\n \nint main()\n{\n scanf(\"%d%d\",&n,&m);\n while (n>0 || m>0)\n {\n for (i=1;i<=n;++i)\n\t{\n\t scanf(\"%s\",c[i]);\n\t l[i]=strlen(c[i]);\n\t}\n len=0;\n for (j=1;j<=m;++j)\n\t{\n\t scanf(\"%s\",ss);\n\t ll=strlen(ss);\n\t for (i=0;i<ll;++i) s[++len]=ss[i];\n\t}\n memset(f,0,sizeof(f));\n memset(b,0,sizeof(b));\n for (i=1;i<=len;++i)\n\tfor (j=1;j<=n;++j) check(i,j); \n int maxn=1<<n;\n for (i=0;i<=len;++i) f[i][0]=1;\n for (i=1;i<=len;++i)\n\tfor (j=1;j<=n;++j)\n\t if (b[i][j])\n\t for (int k=1;k<maxn;++k)\n\t if (!f[i][k])\n\t\tif ((k|(1<<(j-1)))==k) f[i][k]|=f[i-l[j]][k^(1<<(j-1))];\n int ans=0;\n for (i=1;i<=len;++i)\n\tif (f[i][maxn-1]) ++ans;\n printf(\"%d\\n\",ans);\n scanf(\"%d%d\",&n,&m);\n }\n return 0;\n}", "accuracy": 1, "time_ms": 1030, "memory_kb": 25336, "score_of_the_acc": -0.2715, "final_rank": 4 }, { "submission_id": "aoj_1291_10702866", "code_snippet": "#include <cstdio>\n#include <cstring>\nconst int Maxn=12, Maxl=5003;\nbool g[Maxl][Maxn];\nchar a[Maxn][23],s[103],str[Maxl];\nint f[Maxl][1<<Maxn];\n\nint main()\n{\n\tint T=0, n,m,len,i,j,k,l,ans;\n\tscanf(\"%d%d\",&n,&m);\n\twhile (n)\n\t{\n\t\tfor (i=0; i<n; ++i) scanf(\"%s\",a[i]);\n\t\tlen=0;\n\t\twhile (m--)\n\t\t{\n\t\t\tscanf(\"%s\",s);\n\t\t\tl=strlen(s);\n\t\t\tfor (i=0; i<l; ++i) str[len++]=s[i];\n\t\t}\n\t\tstr[len]='\\0';\n\t\tfor (i=0; i<len; ++i)\n\t\t\tfor (j=0; j<n; ++j)\n\t\t\t{\n\t\t\t\tg[i][j]=true;\n\t\t\t\tl=strlen(a[j]);\n\t\t\t\tfor (k=0; k<l; ++k)\n\t\t\t\t\tif (str[i+k]!=a[j][k])\n\t\t\t\t\t{\n\t\t\t\t\t\tg[i][j]=false; break;\n\t\t\t\t\t}\n\t\t\t}\n\t\t++T;\n\t\tfor (i=0; i<len; ++i)\n\t\t{\n\t\t\tf[i][0]=T;\n\t\t\tfor (j=0; j<(1<<n); ++j)\n\t\t\t\tif (f[i][j]==T)\n\t\t\t\t\tfor (k=0; k<n; ++k)\n\t\t\t\t\t\tif ((j&(1<<k))==0 && g[i][k]) f[i+strlen(a[k])][j|(1<<k)]=T;\n\t\t}\n\t\tans=0;\n\t\tfor (i=0; i<=len; ++i)\n\t\t\tif (f[i][(1<<n)-1]==T) ++ans;\n\t\tprintf(\"%d\\n\",ans);\n\t\tscanf(\"%d%d\",&n,&m);\n\t}\n\treturn 0;\n}", "accuracy": 1, "time_ms": 3390, "memory_kb": 83008, "score_of_the_acc": -1.5108, "final_rank": 18 }, { "submission_id": "aoj_1291_9778375", "code_snippet": "#line 2 \"cp-library/src/cp-template.hpp\"\n#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\nusing ld = long double;\nusing uint = unsigned int;\nusing ull = unsigned long long;\nusing i32 = int;\nusing u32 = unsigned int;\nusing i64 = long long;\nusing u64 = unsigned long long;\nusing i128 = __int128_t;\ntemplate < class T > bool chmin(T& a, T b) { if(a > b) { a = b; return true; } return false; }\ntemplate < class T > bool chmax(T& a, T b) { if(a < b) { a = b; return true; } return false; }\ntemplate < class T, class U > T ceil (T x, U y) { return (x > 0 ? (x + y - 1) / y : x / y); }\ntemplate < class T, class U > T floor(T x, U y) { return (x > 0 ? x / y : (x - y + 1) / y); }\nint popcnt(i32 x) { return __builtin_popcount(x); }\nint popcnt(u32 x) { return __builtin_popcount(x); }\nint popcnt(i64 x) { return __builtin_popcountll(x); }\nint popcnt(u64 x) { return __builtin_popcountll(x); }\n\n#line 2 \"cp-library/src/utility/rep_itr.hpp\"\ntemplate < class T > struct itr_rep {\n T i, d;\n constexpr itr_rep(const T i) noexcept : i(i), d(1) {}\n constexpr itr_rep(const T i, const T d) noexcept : i(i), d(d) {}\n void operator++() noexcept { i += d; }\n constexpr int operator*() const noexcept { return i; }\n constexpr bool operator!=(const itr_rep x) const noexcept { return d > 0 ? i < x.i : i > x.i; }\n};\n\ntemplate < class T > struct rep {\n const itr_rep< T > s, t;\n constexpr rep(const T t) noexcept : s(0), t(t) {}\n constexpr rep(const T s, const T t) noexcept : s(s), t(t) {}\n constexpr rep(const T s, const T t, const T d) noexcept : s(s, d), t(t, d) {}\n constexpr auto begin() const noexcept { return s; }\n constexpr auto end () const noexcept { return t; }\n};\n\ntemplate < class T > struct revrep {\n const itr_rep < T > s, t;\n constexpr revrep(const T t) noexcept : s(t - 1, -1), t(-1, -1) {}\n constexpr revrep(const T s, const T t) noexcept : s(t - 1, -1), t(s - 1, -1) {}\n constexpr revrep(const T s, const T t, const T d) noexcept : s(t - 1, -d), t(s - 1, -d) {}\n constexpr auto begin() const noexcept { return s; }\n constexpr auto end () const noexcept { return t; }\n};\n#line 3 \"cp-library/src/utility/io.hpp\"\n\n/* 128bit integer */\nistream& operator>>(istream& is, i128& x) {\n std::string s; is >> s;\n int pm = (s[0] == '-');\n x = 0;\n for(int i : rep(pm, int(s.size()))) x = x * 10 + (s[i] - '0');\n if(pm) x *= -1;\n return is;\n}\nostream& operator<<(ostream& os, const i128& x) {\n if(x == 0) return os << '0';\n i128 y = x;\n if(y < 0) { os << '-'; y *= -1; }\n std::vector<int> ny;\n while(y > 0) { ny.push_back(y % 10); y /= 10; }\n for(int i : revrep(ny.size())) os << ny[i];\n return os;\n}\n\ntemplate < class S, class T > istream& operator>>(istream& is, std::pair< S, T >& x) { is >> x.first >> x.second; return is; }\ntemplate < class S, class T > ostream& operator<<(ostream& os, const std::pair< S, T >& x) { os << x.first << \" \" << x.second; return os; }\n\nnamespace scanner {\n struct sca {\n template < class T > operator T() {\n T s; std::cin >> s; return s;\n }\n };\n struct vec {\n int n;\n vec(int n) : n(n) {}\n template < class T > operator std::vector< T >() {\n std::vector< T > v(n);\n for(T& x : v) std::cin >> x;\n return v;\n }\n };\n struct mat {\n int h, w;\n mat(int h, int w) : h(h), w(w) {}\n template < class T > operator std::vector< std::vector< T > >() {\n std::vector m(h, std::vector< T >(w));\n for(std::vector< T >& v : m) for(T& x : v) std::cin >> x;\n return m;\n }\n };\n struct speedup {\n speedup() {\n std::cin.tie(0);\n std::ios::sync_with_stdio(0);\n }\n } speedup_instance;\n}\nscanner::sca in() { return scanner::sca(); }\nscanner::vec in(int n) { return scanner::vec(n); }\nscanner::mat in(int h, int w) { return scanner::mat(h, w); }\n\nnamespace printer {\n void precision(int d) { std::cout << std::fixed << std::setprecision(d); }\n void flush() { std::cout.flush(); }\n}\n\ntemplate < class T >\nostream& operator<<(ostream& os, const std::vector< T > a) {\n int n = a.size();\n for(int i : rep(n)) { os << a[i]; if(i != n - 1) os << ' '; }\n return os;\n}\n\nint print() { std::cout << '\\n'; return 0; }\ntemplate < class head, class... tail > int print(head&& h, tail&&... t) {\n std::cout << h; if(sizeof...(tail)) std::cout << ' ';\n return print(std::forward<tail>(t)...);\n}\ntemplate < class T > int print_n(const std::vector< T > a) {\n int n = a.size();\n for(int i : rep(n)) std::cout << a[i] << \"\\n\";\n return 0;\n}\n\n\n#line 2 \"cp-library/src/utility/key_val.hpp\"\n\ntemplate < class K, class V >\nstruct key_val {\n K key; V val;\n key_val() {}\n key_val(K key, V val) : key(key), val(val) {}\n template < std::size_t Index >\n std::tuple_element_t< Index, key_val >& get() {\n if constexpr (Index == 0) return key;\n if constexpr (Index == 1) return val;\n }\n};\n\nnamespace std {\n\ntemplate < class K, class V > struct tuple_size < key_val< K, V > > : integral_constant< size_t, 2 > {};\ntemplate < class K, class V > struct tuple_element < 0, key_val< K, V > > { using type = K; };\ntemplate < class K, class V > struct tuple_element < 1, key_val< K, V > > { using type = V; };\n\n}\n#line 2 \"cp-library/src/utility/vec_op.hpp\"\ntemplate < class T > key_val< int, T > max_of(const vector< T >& a) {\n int i = std::max_element(a.begin(), a.end()) - a.begin();\n return {i, a[i]};\n}\ntemplate < class T > key_val< int, T > min_of(const vector< T >& a) {\n int i = std::min_element(a.begin(), a.end()) - a.begin();\n return {i, a[i]};\n}\ntemplate < class S, class T > S sum_of(const vector< T >& a) {\n S sum = 0;\n for(const T x : a) sum += x;\n return sum;\n}\ntemplate < class S, class T > vector< S > freq_of(const vector< T >& a, T L, T R) {\n vector< S > res(R - L, S(0));\n for(const T x : a) res[x - L] += 1;\n return res;\n}\ntemplate < class S, class T > struct prefix_sum {\n vector< S > s;\n prefix_sum(const vector< T >& a) : s(a) {\n s.insert(s.begin(), S(0));\n for(int i : rep(a.size())) s[i + 1] += s[i];\n }\n // [L, R)\n S sum(int L, int R) { return s[R] - s[L]; }\n};\n#line 3 \"cp-library/src/utility/heap.hpp\"\n\ntemplate < class T > using heap_min = std::priority_queue< T, std::vector< T >, std::greater< T > >;\ntemplate < class T > using heap_max = std::priority_queue< T, std::vector< T >, std::less< T > >;\n\n#line 27 \"cp-library/src/cp-template.hpp\"\n\n#line 1 \"cp-library/src/algorithm/bin_search.hpp\"\ntemplate < class T, class F >\nT bin_search(T ok, T ng, F f) {\n while(abs(ng - ok) > 1) {\n T mid = (ok + ng) / 2;\n (f(mid) ? ok : ng) = mid;\n }\n return ok;\n}\n\ntemplate < class T, class F >\nT bin_search_real(T ok, T ng, F f, int step = 80) {\n while(step--) {\n T mid = (ok + ng) / 2;\n (f(mid) ? ok : ng) = mid;\n }\n return ok;\n}\n#line 2 \"cp-library/src/algorithm/argsort.hpp\"\n\ntemplate < class T > std::vector< int > argsort(const std::vector< T > &a) {\n std::vector< int > ids((int)a.size());\n std::iota(ids.begin(), ids.end(), 0);\n std::sort(ids.begin(), ids.end(), [&](int i, int j) {\n return a[i] < a[j] || (a[i] == a[j] && i < j);\n });\n return ids;\n}\n#line 1 \"macro.hpp\"\nnamespace macro {\n\nusing size_type = int;\ntemplate < class container > void sort(container& a) { std::sort(std:: begin(a), std:: end(a)); }\ntemplate < class container > void rsort(container& a) { std::sort(std::rbegin(a), std::rend(a)); }\ntemplate < class container > void reverse(container& a) { std::reverse(std::begin(a), std::end(a)); }\ntemplate < class container > void unique(container& a) {\n std::sort(std::begin(a), std::end(a));\n a.erase(std::unique(std::begin(a), std::end(a)), std::end(a));\n}\ntemplate < class container > container sorted(const container& a) { container b = a; sort(b); return std::move(b); }\ntemplate < class container > container rsorted(const container& a) { container b = a; rsort(b); return std::move(b); }\ntemplate < class container, class compare > void sort(container& a, const compare& cmp) { std::sort(std::begin(a), std::end(a), cmp); }\ntemplate < class container, class compare > container sorted(const container& a, const compare& cmp) { container b = a; sort(b, cmp); return std::move(b); }\ntemplate < class container, class value > size_type lower_bound(const container& a, const value& x) { return std::lower_bound(std::begin(a), std::end(a), x) - std::begin(a); }\ntemplate < class container, class value > size_type upper_bound(const container& a, const value& x) { return std::upper_bound(std::begin(a), std::end(a), x) - std::begin(a); }\n\nconst std::vector<std::pair<size_type, size_type>> dir4 = { {+1, 0}, {-1, 0}, { 0, +1}, { 0, -1} };\nconst std::vector<std::pair<size_type, size_type>> dir8 = { {-1, -1}, {-1, 0}, {-1, +1}, { 0, -1}, { 0, +1}, {+1, -1}, {+1, 0}, {+1, +1} };\n\n#ifdef _DEBUG\n#define debug(x) std::cout << \"[\" << __LINE__ << \"] \" << #x << \": \" << x << std::endl\n#else\n#define debug(x)\n#endif\n\ntemplate < class container > void concat(container& a, const container& b) {\n a.insert(std::end(a), std::begin(b), std::end(b));\n}\nstd::vector<size_type> iota(const size_type n) {\n std::vector<size_type> I(n);\n std::iota(std::begin(I), std::end(I), 0);\n return I;\n}\ntemplate < class container > std::vector<size_type> sort_idx(const container& a) {\n const size_type n = a.size();\n std::vector<size_type> I = iota(n);\n std::sort(std::begin(I), std::end(I), [&](size_type i, size_type j) { return a[i] < a[j] or (a[i] == a[j] and i < j); });\n return I;\n}\ntemplate < class container, class compare > std::vector<size_type> sort_idx(const container& a, const compare& cmp) {\n const size_type n = a.size();\n std::vector<size_type> I = iota(n);\n std::sort(std::begin(I), std::end(I), [&](size_type i, size_type j) { return cmp(a[i], a[j]) or (a[i] == a[j] and i < j); });\n return std::move(I);\n}\n\nstruct grid {\n using size_type = int;\n size_type H, W;\n grid(const size_type H, const size_type W) : H(H), W(W) {}\n bool contains(const size_type i, const size_type j) {\n return 0 <= i and i < H and 0 <= j and j < W;\n }\n};\n\nusing f64 = long double;\n\ntemplate < class T > vector< T >& operator++(vector< T >& a) { for(T& x : a) x++; return a; }\ntemplate < class T > vector< T >& operator--(vector< T >& a) { for(T& x : a) x--; return a; }\ntemplate < class T > vector< T > operator++(vector< T >& a, signed) { vector< T > res = a; for(T& x : a) x++; return res; }\ntemplate < class T > vector< T > operator--(vector< T >& a, signed) { vector< T > res = a; for(T& x : a) x--; return res; }\n\n} // namespace macro\n\nusing namespace macro;\n#line 3 \"cp-library/src/utility/random.hpp\"\n\nnamespace randnum {\n\nstatic uint seed;\nstatic std::mt19937 mt;\nstruct gen_seed {\n gen_seed() {\n seed = std::random_device()();\n mt = std::mt19937(seed);\n }\n} gs;\n\n// [L, R)\ntemplate < class T >\nT gen_int(T L, T R) {\n return std::uniform_int_distribution< T >(L, R - 1)(mt);\n}\n\ntemplate < class T >\nT get_real(T L, T R) {\n return std::uniform_real_distribution< T >(L, R)(mt);\n}\n\n}\n#line 3 \"cp-library/src/utility/hash.hpp\"\n\ntemplate < int num_of_mod = 2 >\nstruct hash_vector : public array<ll, num_of_mod> {\n //static constexpr ll MODS[] = {999999937, 1000000007, 1000000009, 1000000021};\n static constexpr ll MODS[] = {1000000021, 999999937, 1000000007, 1000000009, };\n static_assert(1 <= num_of_mod and num_of_mod <= 4);\n using array<ll, num_of_mod>::operator[];\n using H = hash_vector;\n static constexpr int n = num_of_mod;\n hash_vector() : array<ll,n>() {}\n hash_vector(ll x) : H() { for(int i : rep(n)) (*this)[i] = x % MODS[i]; }\n H& operator+=(const H& rhs) { for(int i : rep(n)) if(((*this)[i] += rhs[i]) >= MODS[i]) (*this)[i] -= MODS[i]; return *this; }\n H& operator-=(const H& rhs) { for(int i : rep(n)) if(((*this)[i] += MODS[i] - rhs[i]) >= MODS[i]) (*this)[i] -= MODS[i]; return *this; }\n H& operator*=(const H& rhs) { for(int i : rep(n)) (*this)[i] = (*this)[i] * rhs[i] % MODS[i]; return *this; }\n H& operator+=(const ll rhs) { for(int i : rep(n)) if(((*this)[i] += rhs % MODS[i]) >= MODS[i]) (*this)[i] -= MODS[i]; return *this; }\n H& operator-=(const ll rhs) { for(int i : rep(n)) if(((*this)[i] += MODS[i] - rhs % MODS[i]) >= MODS[i]) (*this)[i] -= MODS[i]; return *this; }\n H& operator*=(const ll rhs) { for(int i : rep(n)) (*this)[i] = (*this)[i] * (rhs % MODS[i]) % MODS[i]; return *this; }\n H operator+(const H& rhs) const { return H(*this) += rhs; }\n H operator-(const H& rhs) const { return H(*this) -= rhs; }\n H operator*(const H& rhs) const { return H(*this) *= rhs; }\n H operator+(const ll rhs) const { return H(*this) += rhs; }\n H operator-(const ll rhs) const { return H(*this) -= rhs; }\n H operator*(const ll rhs) const { return H(*this) *= rhs; }\n H operator-() const { return H().fill(0) - *this; }\n friend H operator+(ll x, const H& y) { return H(x) + y; }\n friend H operator-(ll x, const H& y) { return H(x) + y; }\n friend H operator*(ll x, const H& y) { return H(x) * y; }\n bool operator==(const H& rhs) { for(int i : rep(n)) if((*this)[i] != rhs[i]) return false; return true ; }\n bool operator!=(const H& rhs) { for(int i : rep(n)) if((*this)[i] != rhs[i]) return true ; return false; }\n};\n#line 4 \"cp-library/src/string/rolling_hash.hpp\"\n\n\nconstexpr ll BASE = 10007;\ntemplate< int num_of_mod = 2 >\nstruct rolling_hash {\n\n static const ll BASE;\n\n vector< hash_vector< num_of_mod > > pb, hs;\n rolling_hash() {}\n rolling_hash(const string& s) {\n int n = s.size();\n hs.resize(n + 1); hs[0].fill(0);\n pb.resize(n + 1); pb[0].fill(1);\n for(int i : rep(n)) {\n hs[i + 1] = hs[i] * BASE + s[i];\n pb[i + 1] = pb[i] * BASE;\n }\n }\n\n // [l, r)\n hash_vector< num_of_mod > get(int l, int r) const {\n return hs[r] - hs[l] * pb[r - l];\n }\n\n hash_vector< num_of_mod > concat(hash_vector< num_of_mod > h1, hash_vector< num_of_mod > h2, int h2_len) {\n assert(0 <= h2_len and h2_len < int(pb.size()));\n return h1 * pb[h2_len] + h2;\n }\n\n template < int n >\n static int lcp(const rolling_hash< n >& rh1, int l1, int r1, const rolling_hash< n >& rh2, int l2, int r2) {\n int lo = -1, hi = min(r1 - l1, r2 - l2) + 1;\n while(hi - lo > 1) {\n int mid = (lo + hi) / 2;\n (rh1.get(l1, l1 + mid) == rh2.get(l2, l2 + mid) ? lo : hi) = mid;\n }\n return lo;\n }\n\n template < int n >\n static int cmp(const string& s1, const rolling_hash< n >& rh1, int l1, int r1,\n const string& s2, const rolling_hash< n >& rh2, int l2, int r2) {\n int len = lcp(rh1, l1, r1, rh2, l2, r2);\n if(len == r1 - l1 && len == r2 - l2) return 0;\n if(len == r1 - l1) return -1;\n if(len == r2 - l2) return +1;\n return (s1[l1 + len] < s2[l2 + len] ? -1 : +1);\n }\n};\n\ntemplate < int num_of_mod >\nconst ll rolling_hash< num_of_mod >::BASE = randnum::gen_int<ll>(ll(0), hash_vector< num_of_mod >::MODS[0]);\n#line 4 \"A.cpp\"\nusing RH = rolling_hash< 2 >;\n\nint dp[5050][1 << 12];\n\nint solve(int n, int m) {\n vector<string> e = in(n);\n vector<RH> he(n);\n for(int i : rep(n)) he[i] = RH(e[i]);\n\n int ans = 0;\n string t = \"\";\n for(int _ : rep(m)) { string s = in(); t += s; }\n const int sz_t = t.size();\n RH ht(t);\n for(int i : rep(sz_t + 1)) for(int S : rep(1 << n)) dp[i][S] = 0;\n for(int i : rep(sz_t + 1)) dp[i][0] = 1;\n for(int i : rep(sz_t + 1)) {\n for(int S : rep(1 << n)) if(dp[i][S]) {\n for(int k : rep(n)) {\n if((S >> k & 1) == 0) {\n const int sz_e = e[k].size();\n if(i + sz_e <= sz_t and ht.get(i, i + sz_e) == he[k].get(0, sz_e)) {\n dp[i + sz_e][S | (1 << k)] = 1;\n }\n }\n }\n }\n }\n for(int i : rep(sz_t + 1)) ans += dp[i][(1 << n) - 1];\n return ans;\n}\n\nint main() {\n while(true) {\n int n = in(), m = in();\n if(make_pair(n, m) == make_pair(0, 0)) return 0;\n print(solve(n, m));\n }\n}", "accuracy": 1, "time_ms": 5520, "memory_kb": 83548, "score_of_the_acc": -1.9846, "final_rank": 19 }, { "submission_id": "aoj_1291_9778368", "code_snippet": "#line 2 \"cp-library/src/cp-template.hpp\"\n#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\nusing ld = long double;\nusing uint = unsigned int;\nusing ull = unsigned long long;\nusing i32 = int;\nusing u32 = unsigned int;\nusing i64 = long long;\nusing u64 = unsigned long long;\nusing i128 = __int128_t;\ntemplate < class T > bool chmin(T& a, T b) { if(a > b) { a = b; return true; } return false; }\ntemplate < class T > bool chmax(T& a, T b) { if(a < b) { a = b; return true; } return false; }\ntemplate < class T, class U > T ceil (T x, U y) { return (x > 0 ? (x + y - 1) / y : x / y); }\ntemplate < class T, class U > T floor(T x, U y) { return (x > 0 ? x / y : (x - y + 1) / y); }\nint popcnt(i32 x) { return __builtin_popcount(x); }\nint popcnt(u32 x) { return __builtin_popcount(x); }\nint popcnt(i64 x) { return __builtin_popcountll(x); }\nint popcnt(u64 x) { return __builtin_popcountll(x); }\n\n#line 2 \"cp-library/src/utility/rep_itr.hpp\"\ntemplate < class T > struct itr_rep {\n T i, d;\n constexpr itr_rep(const T i) noexcept : i(i), d(1) {}\n constexpr itr_rep(const T i, const T d) noexcept : i(i), d(d) {}\n void operator++() noexcept { i += d; }\n constexpr int operator*() const noexcept { return i; }\n constexpr bool operator!=(const itr_rep x) const noexcept { return d > 0 ? i < x.i : i > x.i; }\n};\n\ntemplate < class T > struct rep {\n const itr_rep< T > s, t;\n constexpr rep(const T t) noexcept : s(0), t(t) {}\n constexpr rep(const T s, const T t) noexcept : s(s), t(t) {}\n constexpr rep(const T s, const T t, const T d) noexcept : s(s, d), t(t, d) {}\n constexpr auto begin() const noexcept { return s; }\n constexpr auto end () const noexcept { return t; }\n};\n\ntemplate < class T > struct revrep {\n const itr_rep < T > s, t;\n constexpr revrep(const T t) noexcept : s(t - 1, -1), t(-1, -1) {}\n constexpr revrep(const T s, const T t) noexcept : s(t - 1, -1), t(s - 1, -1) {}\n constexpr revrep(const T s, const T t, const T d) noexcept : s(t - 1, -d), t(s - 1, -d) {}\n constexpr auto begin() const noexcept { return s; }\n constexpr auto end () const noexcept { return t; }\n};\n#line 3 \"cp-library/src/utility/io.hpp\"\n\n/* 128bit integer */\nistream& operator>>(istream& is, i128& x) {\n std::string s; is >> s;\n int pm = (s[0] == '-');\n x = 0;\n for(int i : rep(pm, int(s.size()))) x = x * 10 + (s[i] - '0');\n if(pm) x *= -1;\n return is;\n}\nostream& operator<<(ostream& os, const i128& x) {\n if(x == 0) return os << '0';\n i128 y = x;\n if(y < 0) { os << '-'; y *= -1; }\n std::vector<int> ny;\n while(y > 0) { ny.push_back(y % 10); y /= 10; }\n for(int i : revrep(ny.size())) os << ny[i];\n return os;\n}\n\ntemplate < class S, class T > istream& operator>>(istream& is, std::pair< S, T >& x) { is >> x.first >> x.second; return is; }\ntemplate < class S, class T > ostream& operator<<(ostream& os, const std::pair< S, T >& x) { os << x.first << \" \" << x.second; return os; }\n\nnamespace scanner {\n struct sca {\n template < class T > operator T() {\n T s; std::cin >> s; return s;\n }\n };\n struct vec {\n int n;\n vec(int n) : n(n) {}\n template < class T > operator std::vector< T >() {\n std::vector< T > v(n);\n for(T& x : v) std::cin >> x;\n return v;\n }\n };\n struct mat {\n int h, w;\n mat(int h, int w) : h(h), w(w) {}\n template < class T > operator std::vector< std::vector< T > >() {\n std::vector m(h, std::vector< T >(w));\n for(std::vector< T >& v : m) for(T& x : v) std::cin >> x;\n return m;\n }\n };\n struct speedup {\n speedup() {\n std::cin.tie(0);\n std::ios::sync_with_stdio(0);\n }\n } speedup_instance;\n}\nscanner::sca in() { return scanner::sca(); }\nscanner::vec in(int n) { return scanner::vec(n); }\nscanner::mat in(int h, int w) { return scanner::mat(h, w); }\n\nnamespace printer {\n void precision(int d) { std::cout << std::fixed << std::setprecision(d); }\n void flush() { std::cout.flush(); }\n}\n\ntemplate < class T >\nostream& operator<<(ostream& os, const std::vector< T > a) {\n int n = a.size();\n for(int i : rep(n)) { os << a[i]; if(i != n - 1) os << ' '; }\n return os;\n}\n\nint print() { std::cout << '\\n'; return 0; }\ntemplate < class head, class... tail > int print(head&& h, tail&&... t) {\n std::cout << h; if(sizeof...(tail)) std::cout << ' ';\n return print(std::forward<tail>(t)...);\n}\ntemplate < class T > int print_n(const std::vector< T > a) {\n int n = a.size();\n for(int i : rep(n)) std::cout << a[i] << \"\\n\";\n return 0;\n}\n\n\n#line 2 \"cp-library/src/utility/key_val.hpp\"\n\ntemplate < class K, class V >\nstruct key_val {\n K key; V val;\n key_val() {}\n key_val(K key, V val) : key(key), val(val) {}\n template < std::size_t Index >\n std::tuple_element_t< Index, key_val >& get() {\n if constexpr (Index == 0) return key;\n if constexpr (Index == 1) return val;\n }\n};\n\nnamespace std {\n\ntemplate < class K, class V > struct tuple_size < key_val< K, V > > : integral_constant< size_t, 2 > {};\ntemplate < class K, class V > struct tuple_element < 0, key_val< K, V > > { using type = K; };\ntemplate < class K, class V > struct tuple_element < 1, key_val< K, V > > { using type = V; };\n\n}\n#line 2 \"cp-library/src/utility/vec_op.hpp\"\ntemplate < class T > key_val< int, T > max_of(const vector< T >& a) {\n int i = std::max_element(a.begin(), a.end()) - a.begin();\n return {i, a[i]};\n}\ntemplate < class T > key_val< int, T > min_of(const vector< T >& a) {\n int i = std::min_element(a.begin(), a.end()) - a.begin();\n return {i, a[i]};\n}\ntemplate < class S, class T > S sum_of(const vector< T >& a) {\n S sum = 0;\n for(const T x : a) sum += x;\n return sum;\n}\ntemplate < class S, class T > vector< S > freq_of(const vector< T >& a, T L, T R) {\n vector< S > res(R - L, S(0));\n for(const T x : a) res[x - L] += 1;\n return res;\n}\ntemplate < class S, class T > struct prefix_sum {\n vector< S > s;\n prefix_sum(const vector< T >& a) : s(a) {\n s.insert(s.begin(), S(0));\n for(int i : rep(a.size())) s[i + 1] += s[i];\n }\n // [L, R)\n S sum(int L, int R) { return s[R] - s[L]; }\n};\n#line 3 \"cp-library/src/utility/heap.hpp\"\n\ntemplate < class T > using heap_min = std::priority_queue< T, std::vector< T >, std::greater< T > >;\ntemplate < class T > using heap_max = std::priority_queue< T, std::vector< T >, std::less< T > >;\n\n#line 27 \"cp-library/src/cp-template.hpp\"\n\n#line 1 \"cp-library/src/algorithm/bin_search.hpp\"\ntemplate < class T, class F >\nT bin_search(T ok, T ng, F f) {\n while(abs(ng - ok) > 1) {\n T mid = (ok + ng) / 2;\n (f(mid) ? ok : ng) = mid;\n }\n return ok;\n}\n\ntemplate < class T, class F >\nT bin_search_real(T ok, T ng, F f, int step = 80) {\n while(step--) {\n T mid = (ok + ng) / 2;\n (f(mid) ? ok : ng) = mid;\n }\n return ok;\n}\n#line 2 \"cp-library/src/algorithm/argsort.hpp\"\n\ntemplate < class T > std::vector< int > argsort(const std::vector< T > &a) {\n std::vector< int > ids((int)a.size());\n std::iota(ids.begin(), ids.end(), 0);\n std::sort(ids.begin(), ids.end(), [&](int i, int j) {\n return a[i] < a[j] || (a[i] == a[j] && i < j);\n });\n return ids;\n}\n#line 1 \"macro.hpp\"\nnamespace macro {\n\nusing size_type = int;\ntemplate < class container > void sort(container& a) { std::sort(std:: begin(a), std:: end(a)); }\ntemplate < class container > void rsort(container& a) { std::sort(std::rbegin(a), std::rend(a)); }\ntemplate < class container > void reverse(container& a) { std::reverse(std::begin(a), std::end(a)); }\ntemplate < class container > void unique(container& a) {\n std::sort(std::begin(a), std::end(a));\n a.erase(std::unique(std::begin(a), std::end(a)), std::end(a));\n}\ntemplate < class container > container sorted(const container& a) { container b = a; sort(b); return std::move(b); }\ntemplate < class container > container rsorted(const container& a) { container b = a; rsort(b); return std::move(b); }\ntemplate < class container, class compare > void sort(container& a, const compare& cmp) { std::sort(std::begin(a), std::end(a), cmp); }\ntemplate < class container, class compare > container sorted(const container& a, const compare& cmp) { container b = a; sort(b, cmp); return std::move(b); }\ntemplate < class container, class value > size_type lower_bound(const container& a, const value& x) { return std::lower_bound(std::begin(a), std::end(a), x) - std::begin(a); }\ntemplate < class container, class value > size_type upper_bound(const container& a, const value& x) { return std::upper_bound(std::begin(a), std::end(a), x) - std::begin(a); }\n\nconst std::vector<std::pair<size_type, size_type>> dir4 = { {+1, 0}, {-1, 0}, { 0, +1}, { 0, -1} };\nconst std::vector<std::pair<size_type, size_type>> dir8 = { {-1, -1}, {-1, 0}, {-1, +1}, { 0, -1}, { 0, +1}, {+1, -1}, {+1, 0}, {+1, +1} };\n\n#ifdef _DEBUG\n#define debug(x) std::cout << \"[\" << __LINE__ << \"] \" << #x << \": \" << x << std::endl\n#else\n#define debug(x)\n#endif\n\ntemplate < class container > void concat(container& a, const container& b) {\n a.insert(std::end(a), std::begin(b), std::end(b));\n}\nstd::vector<size_type> iota(const size_type n) {\n std::vector<size_type> I(n);\n std::iota(std::begin(I), std::end(I), 0);\n return I;\n}\ntemplate < class container > std::vector<size_type> sort_idx(const container& a) {\n const size_type n = a.size();\n std::vector<size_type> I = iota(n);\n std::sort(std::begin(I), std::end(I), [&](size_type i, size_type j) { return a[i] < a[j] or (a[i] == a[j] and i < j); });\n return I;\n}\ntemplate < class container, class compare > std::vector<size_type> sort_idx(const container& a, const compare& cmp) {\n const size_type n = a.size();\n std::vector<size_type> I = iota(n);\n std::sort(std::begin(I), std::end(I), [&](size_type i, size_type j) { return cmp(a[i], a[j]) or (a[i] == a[j] and i < j); });\n return std::move(I);\n}\n\nstruct grid {\n using size_type = int;\n size_type H, W;\n grid(const size_type H, const size_type W) : H(H), W(W) {}\n bool contains(const size_type i, const size_type j) {\n return 0 <= i and i < H and 0 <= j and j < W;\n }\n};\n\nusing f64 = long double;\n\ntemplate < class T > vector< T >& operator++(vector< T >& a) { for(T& x : a) x++; return a; }\ntemplate < class T > vector< T >& operator--(vector< T >& a) { for(T& x : a) x--; return a; }\ntemplate < class T > vector< T > operator++(vector< T >& a, signed) { vector< T > res = a; for(T& x : a) x++; return res; }\ntemplate < class T > vector< T > operator--(vector< T >& a, signed) { vector< T > res = a; for(T& x : a) x--; return res; }\n\n} // namespace macro\n\nusing namespace macro;\n#line 3 \"cp-library/src/utility/random.hpp\"\n\nnamespace randnum {\n\nstatic uint seed;\nstatic std::mt19937 mt;\nstruct gen_seed {\n gen_seed() {\n seed = std::random_device()();\n mt = std::mt19937(seed);\n }\n} gs;\n\n// [L, R)\ntemplate < class T >\nT gen_int(T L, T R) {\n return std::uniform_int_distribution< T >(L, R - 1)(mt);\n}\n\ntemplate < class T >\nT get_real(T L, T R) {\n return std::uniform_real_distribution< T >(L, R)(mt);\n}\n\n}\n#line 3 \"cp-library/src/utility/hash.hpp\"\n\ntemplate < int num_of_mod = 2 >\nstruct hash_vector : public array<ll, num_of_mod> {\n //static constexpr ll MODS[] = {999999937, 1000000007, 1000000009, 1000000021};\n static constexpr ll MODS[] = {1000000021, 999999937, 1000000007, 1000000009, };\n static_assert(1 <= num_of_mod and num_of_mod <= 4);\n using array<ll, num_of_mod>::operator[];\n using H = hash_vector;\n static constexpr int n = num_of_mod;\n hash_vector() : array<ll,n>() {}\n hash_vector(ll x) : H() { for(int i : rep(n)) (*this)[i] = x % MODS[i]; }\n H& operator+=(const H& rhs) { for(int i : rep(n)) if(((*this)[i] += rhs[i]) >= MODS[i]) (*this)[i] -= MODS[i]; return *this; }\n H& operator-=(const H& rhs) { for(int i : rep(n)) if(((*this)[i] += MODS[i] - rhs[i]) >= MODS[i]) (*this)[i] -= MODS[i]; return *this; }\n H& operator*=(const H& rhs) { for(int i : rep(n)) (*this)[i] = (*this)[i] * rhs[i] % MODS[i]; return *this; }\n H& operator+=(const ll rhs) { for(int i : rep(n)) if(((*this)[i] += rhs % MODS[i]) >= MODS[i]) (*this)[i] -= MODS[i]; return *this; }\n H& operator-=(const ll rhs) { for(int i : rep(n)) if(((*this)[i] += MODS[i] - rhs % MODS[i]) >= MODS[i]) (*this)[i] -= MODS[i]; return *this; }\n H& operator*=(const ll rhs) { for(int i : rep(n)) (*this)[i] = (*this)[i] * (rhs % MODS[i]) % MODS[i]; return *this; }\n H operator+(const H& rhs) const { return H(*this) += rhs; }\n H operator-(const H& rhs) const { return H(*this) -= rhs; }\n H operator*(const H& rhs) const { return H(*this) *= rhs; }\n H operator+(const ll rhs) const { return H(*this) += rhs; }\n H operator-(const ll rhs) const { return H(*this) -= rhs; }\n H operator*(const ll rhs) const { return H(*this) *= rhs; }\n H operator-() const { return H().fill(0) - *this; }\n friend H operator+(ll x, const H& y) { return H(x) + y; }\n friend H operator-(ll x, const H& y) { return H(x) + y; }\n friend H operator*(ll x, const H& y) { return H(x) * y; }\n bool operator==(const H& rhs) { for(int i : rep(n)) if((*this)[i] != rhs[i]) return false; return true ; }\n bool operator!=(const H& rhs) { for(int i : rep(n)) if((*this)[i] != rhs[i]) return true ; return false; }\n};\n#line 4 \"cp-library/src/string/rolling_hash.hpp\"\n\n\nconstexpr ll BASE = 10007;\ntemplate< int num_of_mod = 2 >\nstruct rolling_hash {\n\n static const ll BASE;\n\n vector< hash_vector< num_of_mod > > pb, hs;\n rolling_hash() {}\n rolling_hash(const string& s) {\n int n = s.size();\n hs.resize(n + 1); hs[0].fill(0);\n pb.resize(n + 1); pb[0].fill(1);\n for(int i : rep(n)) {\n hs[i + 1] = hs[i] * BASE + s[i];\n pb[i + 1] = pb[i] * BASE;\n }\n }\n\n // [l, r)\n hash_vector< num_of_mod > get(int l, int r) const {\n return hs[r] - hs[l] * pb[r - l];\n }\n\n hash_vector< num_of_mod > concat(hash_vector< num_of_mod > h1, hash_vector< num_of_mod > h2, int h2_len) {\n assert(0 <= h2_len and h2_len < int(pb.size()));\n return h1 * pb[h2_len] + h2;\n }\n\n template < int n >\n static int lcp(const rolling_hash< n >& rh1, int l1, int r1, const rolling_hash< n >& rh2, int l2, int r2) {\n int lo = -1, hi = min(r1 - l1, r2 - l2) + 1;\n while(hi - lo > 1) {\n int mid = (lo + hi) / 2;\n (rh1.get(l1, l1 + mid) == rh2.get(l2, l2 + mid) ? lo : hi) = mid;\n }\n return lo;\n }\n\n template < int n >\n static int cmp(const string& s1, const rolling_hash< n >& rh1, int l1, int r1,\n const string& s2, const rolling_hash< n >& rh2, int l2, int r2) {\n int len = lcp(rh1, l1, r1, rh2, l2, r2);\n if(len == r1 - l1 && len == r2 - l2) return 0;\n if(len == r1 - l1) return -1;\n if(len == r2 - l2) return +1;\n return (s1[l1 + len] < s2[l2 + len] ? -1 : +1);\n }\n};\n\ntemplate < int num_of_mod >\nconst ll rolling_hash< num_of_mod >::BASE = randnum::gen_int<ll>(ll(0), hash_vector< num_of_mod >::MODS[0]);\n#line 4 \"A.cpp\"\nusing RH = rolling_hash< 2 >;\n\nint solve(int n, int m) {\n vector<string> e = in(n);\n vector<RH> he(n);\n for(int i : rep(n)) he[i] = RH(e[i]);\n\n int ans = 0;\n string t = \"\";\n for(int _ : rep(m)) { string s = in(); t += s; }\n const int sz_t = t.size();\n RH ht(t);\n vector dp(sz_t + 1, vector(1 << n, int(0)));\n for(int i : rep(sz_t + 1)) dp[i][0] = 1;\n for(int i : rep(sz_t + 1)) {\n for(int S : rep(1 << n)) if(dp[i][S]) {\n for(int k : rep(n)) {\n if((S >> k & 1) == 0) {\n const int sz_e = e[k].size();\n if(i + sz_e <= sz_t and ht.get(i, i + sz_e) == he[k].get(0, sz_e)) {\n dp[i + sz_e][S | (1 << k)] = 1;\n }\n }\n }\n }\n }\n for(int i : rep(sz_t + 1)) ans += dp[i][(1 << n) - 1];\n return ans;\n}\n\nint main() {\n while(true) {\n int n = in(), m = in();\n if(make_pair(n, m) == make_pair(0, 0)) return 0;\n print(solve(n, m));\n }\n}", "accuracy": 1, "time_ms": 5590, "memory_kb": 83540, "score_of_the_acc": -1.9999, "final_rank": 20 }, { "submission_id": "aoj_1291_9758277", "code_snippet": "#include <bits/stdc++.h>\n\nusing namespace std;\n\n#define rep(i, a, b) for (int i = (int)(a); i < (int)(b); i++)\n#define rrep(i, a, b) for (int i = (int)(b)-1; i >= (int)(a); i--)\n#define ALL(v) (v).begin(), (v).end()\n#define UNIQUE(v) sort(ALL(v)), (v).erase(unique(ALL(v)), (v).end())\n#define SZ(v) (int)v.size()\n#define MIN(v) *min_element(ALL(v))\n#define MAX(v) *max_element(ALL(v))\n#define LB(v, x) int(lower_bound(ALL(v), (x)) - (v).begin())\n#define UB(v, x) int(upper_bound(ALL(v), (x)) - (v).begin())\n\nusing uint = unsigned int;\nusing ll = long long int;\nusing ull = unsigned long long;\nusing i128 = __int128_t;\nusing u128 = __uint128_t;\nconst int inf = 0x3fffffff;\nconst ll INF = 0x1fffffffffffffff;\n\ntemplate <typename T> inline bool chmax(T &a, T b) {\n if (a < b) {\n a = b;\n return 1;\n }\n return 0;\n}\ntemplate <typename T> inline bool chmin(T &a, T b) {\n if (a > b) {\n a = b;\n return 1;\n }\n return 0;\n}\ntemplate <typename T, typename U> T ceil(T x, U y) {\n assert(y != 0);\n if (y < 0)\n x = -x, y = -y;\n return (x > 0 ? (x + y - 1) / y : x / y);\n}\ntemplate <typename T, typename U> T floor(T x, U y) {\n assert(y != 0);\n if (y < 0)\n x = -x, y = -y;\n return (x > 0 ? x / y : (x - y + 1) / y);\n}\ntemplate <typename T> int popcnt(T x) {\n return __builtin_popcountll(x);\n}\ntemplate <typename T> int topbit(T x) {\n return (x == 0 ? -1 : 63 - __builtin_clzll(x));\n}\ntemplate <typename T> int lowbit(T x) {\n return (x == 0 ? -1 : __builtin_ctzll(x));\n}\n\ntemplate <class T, class U>\nostream &operator<<(ostream &os, const pair<T, U> &p) {\n os << \"P(\" << p.first << \", \" << p.second << \")\";\n return os;\n}\ntemplate <typename T> ostream &operator<<(ostream &os, const vector<T> &vec) {\n os << \"{\";\n for (int i = 0; i < vec.size(); i++) {\n os << vec[i] << (i + 1 == vec.size() ? \"\" : \", \");\n }\n os << \"}\";\n return os;\n}\ntemplate <typename T, typename U>\nostream &operator<<(ostream &os, const map<T, U> &map_var) {\n os << \"{\";\n for (auto itr = map_var.begin(); itr != map_var.end(); itr++) {\n os << \"(\" << itr->first << \", \" << itr->second << \")\";\n itr++;\n if (itr != map_var.end())\n os << \", \";\n itr--;\n }\n os << \"}\";\n return os;\n}\ntemplate <typename T> ostream &operator<<(ostream &os, const set<T> &set_var) {\n os << \"{\";\n for (auto itr = set_var.begin(); itr != set_var.end(); itr++) {\n os << *itr;\n ++itr;\n if (itr != set_var.end())\n os << \", \";\n itr--;\n }\n os << \"}\";\n return os;\n}\n#ifdef LOCAL\n#define show(...) _show(0, #__VA_ARGS__, __VA_ARGS__)\n#else\n#define show(...) true\n#endif\ntemplate <typename T> void _show(int i, T name) {\n cerr << '\\n';\n}\ntemplate <typename T1, typename T2, typename... T3>\nvoid _show(int i, const T1 &a, const T2 &b, const T3 &...c) {\n for (; a[i] != ',' && a[i] != '\\0'; i++)\n cerr << a[i];\n cerr << \":\" << b << \" \";\n _show(i + 1, a, c...);\n}\n\n/**\n * @brief template\n */\n\nint main() {\n int MAXS = 20;\nwhile(1) {\n int N, Q;\n cin >> N >> Q;\n if (N == 0) return 0;\n vector<string> S(N);\n rep(i,0,N) cin >> S[i];\n string T;\n while(Q--) {\n string U;\n cin >> U;\n T += U;\n }\n int M = T.size();\n vector DP(M+1, vector<bool>(1<<N,false));\n rep(i,0,M) {\n DP[i][0] = true;\n vector<bool> B(N,false);\n rep(j,0,N) {\n if (i+S[j].size() <= T.size() && T.substr(i,S[j].size()) == S[j]) B[j] = true;\n }\n rep(j,0,1<<N) {\n if (!DP[i][j]) continue;\n rep(k,0,N) {\n if (j & (1<<k)) continue;\n if (B[k]) DP[i+S[k].size()][j|(1<<k)] = true;\n }\n }\n }\n int ANS = 0;\n rep(i,0,M+1) if (DP[i][(1<<N)-1]) ANS++;\n cout << ANS << endl;\n}\n}", "accuracy": 1, "time_ms": 2560, "memory_kb": 5892, "score_of_the_acc": -0.3637, "final_rank": 10 }, { "submission_id": "aoj_1291_9694265", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\nusing ll=long long;\n\nint N,M;\nvoid solve(){\n vector<string> S(N);\n string T=\"\";\n for(int i=0;i<N;i++)cin>>S[i];\n int K=0;\n for(int m=0;m<M;m++){\n string t;\n cin>>t;\n T+=t;\n K+=t.size();\n }\n vector<vector<bool>> DP(K+1,vector<bool>(1<<N,0));\n DP[0][0]=1;\n for(int k=0;k<K;k++){\n vector<bool> OK(N,0);\n for(int i=0;i<N;i++)if(T.substr(k,S[i].size())==S[i])OK[i]=1;\n DP[k][0]=1;\n for(int bit=0;bit<(1<<N);bit++){\n if(!DP[k][bit])continue;\n for(int b=0;b<N;b++){\n if(bit&(1<<b))continue;\n if(!OK[b])continue;\n DP[k+S[b].size()][bit+(1<<b)]=1;\n }\n }\n }\n int an=0;\n for(int i=0;i<=K;i++)an+=DP[i][(1<<N)-1];\n cout<<an<<endl;\n\n}\n\nint main() {\n while(cin>>N>>M,N+M!=0)solve();\n \n}", "accuracy": 1, "time_ms": 2580, "memory_kb": 5660, "score_of_the_acc": -0.3652, "final_rank": 11 }, { "submission_id": "aoj_1291_8862042", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\n#define rep(i, x, y) for (int i = (x); i < (y); ++i)\n\nconst int inf = 1e9;\nconst int64_t inf64 = 1e18;\nconst double eps = 1e-9;\n\nvoid solve(int n, int m) {\n vector<string> e(n);\n stringstream ss;\n rep(i, 0, n) {\n cin >> e[i];\n }\n rep(i, 0, m) {\n string t;\n cin >> t;\n ss << t;\n }\n string ts = ss.str();\n\n vector<int> sum_len(1 << n);\n rep(i, 0, 1 << n) {\n rep(j, 0, n) {\n if (((i >> j) & 1) == 0) continue;\n sum_len[i] += e[j].size();\n }\n }\n\n vector<vector<bool>> p(5000, vector<bool>(1 << 12, false));\n vector<vector<int>> heads(ts.size());\n rep(i, 0, ts.size()) p[i][0] = true;\n rep(i, 0, ts.size()) {\n rep(j, 0, n) {\n if (i + e[j].size() > ts.size() or !equal(e[j].begin(), e[j].end(), ts.begin() + i)) continue;\n p[i][1 << j] = true;\n heads[i].push_back(j);\n }\n }\n\n int ans = 0;\n for (int i = ts.size() - 1; i >= 0; --i) {\n rep(j, 1, 1 << n) {\n if (p[i][j] or i + sum_len[j] > ts.size()) continue;\n for (int k : heads[i]) {\n if (((j >> k) & 1) == 0) continue;\n int i2 = i + e[k].size();\n if (p[i2][j & (~(1 << k))]) {\n p[i][j] = true;\n break;\n }\n }\n }\n if (p[i][(1 << n) - 1]) ++ans;\n }\n cout << ans << endl;\n}\n\nint main() {\n std::cin.tie(0);\n std::ios::sync_with_stdio(false);\n cout.setf(ios::fixed);\n cout.precision(10);\n for (;;) {\n int n, m;\n cin >> n >> m;\n if (n == 0 and m == 0) break;\n solve(n, m);\n }\n return 0;\n}", "accuracy": 1, "time_ms": 1760, "memory_kb": 6676, "score_of_the_acc": -0.1981, "final_rank": 3 }, { "submission_id": "aoj_1291_8862038", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n#define rep(i, x, y) for (int i = (x); i < (y); ++i)\n#define debug(x) #x << \"=\" << (x)\n#ifdef DEBUG\n#define _GLIBCXX_DEBUG\n#define print(x) std::cerr << debug(x) << \" (L:\" << __LINE__ << \")\" << std::endl\n#else\n#define print(x)\n#endif\nconst int inf = 1e9;\nconst int64_t inf64 = 1e18;\nconst double eps = 1e-9;\ntemplate <typename T> ostream &operator<<(ostream &os, const vector<T> &vec) {\n os << \"[\";\n for (const auto &v : vec) {\n os << v << \",\";\n }\n os << \"]\";\n return os;\n}\nvoid solve(int n, int m) {\n vector<string> e(n);\n string ts;\n int concatenated_len = 0;\n rep(i, 0, n) {\n cin >> e[i];\n concatenated_len += e[i].size();\n }\n rep(i, 0, m) {\n string t;\n cin >> t;\n ts += t;\n }\n vector<int> sum_len(1 << n);\n rep(i, 0, 1 << n) {\n for (int j = 0; j < n; ++j) {\n if (i & (1 << j)) {\n sum_len[i] += e[j].size();\n }\n }\n }\n static bool p[5000][1 << 12];\n fill_n((bool *)p, 5000 * (1 << 12), false);\n vector<vector<int>> heads(ts.size());\n rep(i, 0, ts.size()) p[i][0] = true;\n rep(i, 0, ts.size()) {\n for (int j = 0; j < n; ++j) {\n if (i + e[j].size() <= ts.size() && e[j] == ts.substr(i, e[j].size())) {\n p[i][1 << j] = true;\n heads[i].push_back(j);\n }\n }\n }\n int ans = 0;\n for (int i = ts.size() - 1; i >= 0; --i) {\n for (int j = 1; j < (1 << n); ++j) {\n if (p[i][j] || i + sum_len[j] > ts.size()) continue;\n for (int k : heads[i]) {\n if (j & (1 << k)) {\n int i2 = i + e[k].size();\n if (p[i2][j ^ (1 << k)]) { //Use bitwise XOR to get the remaining subset\n p[i][j] = true;\n break;\n }\n }\n }\n }\n if (p[i][(1 << n) - 1])\n ++ans;\n }\n cout << ans << endl;\n}\nint main() {\n std::cin.tie(0);\n std::ios::sync_with_stdio(false);\n cout.setf(ios::fixed);\n cout.precision(10);\n for (;;) {\n int n, m;\n cin >> n >> m;\n if (n == 0 and m == 0)\n break;\n solve(n, m);\n }\n return 0;\n}", "accuracy": 1, "time_ms": 1350, "memory_kb": 23896, "score_of_the_acc": -0.3237, "final_rank": 5 }, { "submission_id": "aoj_1291_8862036", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n#define rep(i, x, y) for (int i = (x); i < (y); ++i)\n#define debug(x) #x << \"=\" << (x)\n#ifdef DEBUG\n#define _GLIBCXX_DEBUG\n#define print(x) std::cerr << debug(x) << \" (L:\" << __LINE__ << \")\" << std::endl\n#else\n#define print(x)\n#endif\nconst int inf = 1e9;\nconst int64_t inf64 = 1e18;\nconst double eps = 1e-9;\ntemplate <typename T> ostream &operator<<(ostream &os, const vector<T> &vec) {\n os << \"[\";\n for (const auto &v : vec) {\n os << v << \",\";\n }\n os << \"]\";\n return os;\n}\nvoid solve(int n, int m) {\n vector<string> e(n);\n string ts;\n int concatenated_len = 0;\n rep(i, 0, n) {\n cin >> e[i];\n concatenated_len += e[i].size();\n }\n rep(i, 0, m) {\n string t;\n cin >> t;\n ts += t;\n }\n vector<int> sum_len(1 << n);\n\n vector<vector<bool>> p(ts.size(), vector<bool>(1 << n, false)); // Optimized data structure\n\n vector<vector<int>> heads(ts.size());\n rep(i, 0, ts.size()) p[i][0] = true;\n rep(i, 0, ts.size()) {\n rep(j, 0, n) {\n if (i + e[j].size() > ts.size() or e[j] != ts.substr(i, e[j].size()))\n continue;\n p[i][1 << j] = true;\n heads[i].push_back(j);\n }\n }\n int ans = 0;\n for (int i = ts.size() - 1; i >= 0; --i) {\n rep(j, 1, 1 << n) {\n if (p[i][j])\n continue;\n\n //Calculate sum_len on demand\n if (sum_len[j] == 0) {\n rep(k, 0, n) {\n if (((j >> k) & 1) != 0)\n sum_len[j] += e[k].size();\n }\n }\n\n if (i + sum_len[j] > ts.size())\n continue;\n\n for (int k : heads[i]) {\n if (((j >> k) & 1) == 0)\n continue;\n int i2 = i + e[k].size();\n if (p[i2][j & (~(1 << k))]) {\n p[i][j] = true;\n break;\n }\n }\n }\n if (p[i][(1 << n) - 1])\n ++ans;\n }\n cout << ans << endl;\n}\nint main() {\n std::cin.tie(0);\n std::ios::sync_with_stdio(false);\n cout.setf(ios::fixed);\n cout.precision(10);\n for (;;) {\n int n, m;\n cin >> n >> m;\n if (n == 0 and m == 0)\n break;\n solve(n, m);\n }\n return 0;\n}", "accuracy": 1, "time_ms": 1710, "memory_kb": 6544, "score_of_the_acc": -0.1855, "final_rank": 2 }, { "submission_id": "aoj_1291_8819805", "code_snippet": "#include <algorithm>\n#include <cassert>\n#include <cmath>\n#include <complex>\n#include <cstdio>\n#include <cstring>\n#include <iostream>\n#include <map>\n#include <numeric>\n#include <queue>\n#include <set>\n#include <stack>\n#include <vector>\nusing namespace std;\ntypedef long long ll;\ntypedef pair<int, int> PI;\n#define EPS (1e-6)\n#define rep(i, n) for (int i = 0; i < (int)(n); ++i)\n#define REP(i, n) rep(i, n)\n#define F first\n#define S second\n#define mp(a, b) make_pair(a, b)\n#define pb(a) push_back(a)\n#define min3(a, b, c) min((a), min((b), (c)))\n#define min4(a, b, c, d) min((a), min3((b), (c), (d)))\n#define SZ(a) (int)((a).size())\n#define ALL(a) (a).begin(), (a).end()\n#define RALL(a) a.rbegin(), a.rend()\n#define FLL(a, b) memset((a), b, sizeof(a))\n#define CLR(a) memset((a), 0, sizeof(a))\n#define declare(a, it) __typeof(a) it = (a)\n#define FOR(it, a) for (declare(a.begin(), it); it != a.end(); ++it)\n#define FORR(it, a) for (declare(a.rbegin(), it); it != a.rend(); ++it)\ntemplate <typename T, typename U>\nostream &operator<<(ostream &out, const pair<T, U> &val) {\n return out << \"(\" << val.F << \", \" << val.S << \")\";\n}\ntemplate <class T> ostream &operator<<(ostream &out, const vector<T> &val) {\n out << \"{\";\n rep(i, SZ(val)) out << (i ? \", \" : \"\") << val[i];\n return out << \"}\";\n}\ntypedef double FP;\ntypedef complex<FP> pt;\ntypedef pt P;\ntypedef pair<pt, pt> line;\nnamespace std {\nbool operator<(const P &a, const P &b) {\n if (abs(a.real() - b.real()) > EPS)\n return a.real() < b.real();\n return a.imag() < b.imag();\n}\n} // namespace std\nFP dot(P a, P b) { return real(conj(a) * b); }\nFP crs(P a, P b) { return imag(conj(a) * b); }\nP ortho(P a) { return P(imag(a), -real(a)); }\nP ortho(line a) { return ortho(a.S - a.F); }\nP crspt(P a, P b, P c, P d) {\n b -= a, d -= c;\n return a + b * crs(d, c - a) / crs(d, b);\n}\nP crspt(line a, line b) { return crspt(a.F, a.S, b.F, b.S); }\nbool onl(P a1, P a2, P b) {\n return abs(b - a1) + abs(b - a2) < abs(a1 - a2) + EPS;\n}\nbool onl(line a, P b) { return onl(a.F, a.S, b); }\nbool iscrs(line a, line b) {\n P c = crspt(a, b);\n return onl(a, c) && onl(b, c);\n}\nvoid pkuassert(bool t) { t = 1 / t; };\nint dx[] = {0, 1, 0, -1, 1, 1, -1, -1};\nint dy[] = {1, 0, -1, 0, -1, 1, 1, -1};\nenum { TOP, BTM, LFT, RGT, FRT, BCK };\nint dxdy2ce[] = {RGT, FRT, LFT, BCK};\nint s2i(string &a) {\n stringstream ss(a);\n int r;\n ss >> r;\n return r;\n}\ntemplate <class T> T shift(T a, int b, int c, int d, int e) {\n __typeof(a[0]) t = a[b];\n a[b] = a[c];\n a[c] = a[d];\n a[d] = a[e];\n a[e] = t;\n return a;\n}\ntemplate <class T> T rgt(T a) { return shift(a, TOP, LFT, BTM, RGT); }\ntemplate <class T> T lft(T a) { return shift(a, TOP, RGT, BTM, LFT); }\ntemplate <class T> T frt(T a) { return shift(a, TOP, BCK, BTM, FRT); }\ntemplate <class T> T bck(T a) { return shift(a, TOP, FRT, BTM, BCK); }\nline mkl(P a, P v) { return line(a, a + v); }\nFP lpdist(line a, P b) { return abs(b - crspt(a, mkl(b, ortho(a)))); }\nFP spdist(line a, P b) {\n P c(crspt(a, mkl(b, ortho(a))));\n return onl(a, c) ? abs(b - c) : min(abs(a.F - b), abs(a.S - b));\n}\nFP ssdist(line a, line b) {\n return iscrs(a, b) ? 0.\n : min4(spdist(a, b.F), spdist(a, b.S), spdist(b, a.F),\n spdist(b, a.S));\n}\nint n, m;\nll gha[6000];\nbool vis[1 << 12][100];\nll po[1000];\nvoid solve() {\n ll ha[n];\n int len[n];\n rep(i, n) {\n string a;\n cin >> a;\n ll h = 0;\n len[i] = SZ(a);\n FOR(it, a)\n h = h * 31 + *it;\n ha[i] = h;\n }\n po[0] = 1;\n rep(i, 999) po[i + 1] = po[i] * 31;\n int ans = 0;\n string text;\n rep(jjj, m) {\n string a;\n cin >> a;\n text += a;\n }\n int len_text = SZ(text);\n rep(i, len_text) gha[i + 1] = gha[i] * 31 + text[i];\n bool vis[250][1 << n];\n rep(i, 250) rep(j, 1 << n) vis[i][j] = 0;\n rep(i, 250) vis[i][0] = 1;\n rep(j, len_text + 1) {\n int cur = j % 250;\n rep(i, 1 << n) {\n if (!vis[cur][i])\n continue;\n rep(k, n) {\n ll calc = gha[j + len[k]] - gha[j] * po[len[k]];\n if ((~i & (1 << k)) && len[k] + j <= len_text && ha[k] == calc)\n vis[(j + len[k]) % 250][i | (1 << k)] = 1;\n }\n }\n ans += vis[cur][(1 << n) - 1];\n rep(i, 1 << n) vis[cur][i] = 0;\n vis[cur][0] = 1;\n }\n cout << ans << endl;\n return;\n}\nint main(int argc, char *argv[]) {\n while (cin >> n >> m, n)\n solve();\n return 0;\n}", "accuracy": 1, "time_ms": 3190, "memory_kb": 4512, "score_of_the_acc": -0.4846, "final_rank": 17 }, { "submission_id": "aoj_1291_8819801", "code_snippet": "#include <algorithm>\n#include <cassert>\n#include <cmath>\n#include <complex>\n#include <cstdio>\n#include <cstring>\n#include <iostream>\n#include <map>\n#include <numeric>\n#include <queue>\n#include <set>\n#include <stack>\n#include <vector>\n#include <sstream>\n\ntypedef long long ll;\ntypedef std::pair<int, int> PI;\n\nll gha[6000];\nll po[1000];\n\nvoid solve(int n, int m, ll *gha) {\n ll ha[n];\n int len[n];\n for (int i = 0; i < n; ++i) {\n std::string a;\n std::cin >> a;\n ll h = 0;\n len[i] = a.size();\n for (char it : a)\n h = h * 31 + it;\n ha[i] = h;\n }\n po[0] = 1;\n for (int i = 0; i < 999; i++) po[i + 1] = po[i] * 31;\n int ans = 0;\n std::string text;\n for (int jjj = 0; jjj < m; jjj++) {\n std::string a;\n std::cin >> a;\n text += a;\n }\n for (int i = 0; i < text.size(); i++) gha[i + 1] = gha[i] * 31 + text[i];\n bool vis[250][1 << n];\n for (int i = 0; i < 250; i++) for (int j = 0; j < (1 << n); j++) vis[i][j] = 0;\n for (int i = 0; i < 250; i++) vis[i][0] = 1;\n for (int j = 0; j < text.size() + 1; j++) {\n int cur = j % 250;\n for (int i = 0; i < (1 << n); i++) {\n if (!vis[cur][i])\n continue;\n for (int k = 0; k < n; k++) {\n if ((~i & (1 << k)) && len[k] + j <= text.size() && ha[k] == gha[j + len[k]] - gha[j] * po[len[k]])\n vis[(j + len[k]) % 250][i | (1 << k)] = 1;\n }\n }\n ans += vis[cur][(1 << n) - 1];\n for (int i = 0; i < (1 << n); i++) vis[cur][i] = 0;\n vis[cur][0] = 1;\n }\n std::cout << ans << std::endl;\n return;\n}\n\nint main(int argc, char *argv[]) {\n int n, m;\n while (std::cin >> n >> m, n)\n solve(n, m, gha);\n return 0;\n}", "accuracy": 1, "time_ms": 2810, "memory_kb": 4416, "score_of_the_acc": -0.4001, "final_rank": 14 }, { "submission_id": "aoj_1291_8819798", "code_snippet": "#include <algorithm>\n#include <cassert>\n#include <cmath>\n#include <complex>\n#include <cstdio>\n#include <cstring>\n#include <iostream>\n#include <map>\n#include <numeric>\n#include <queue>\n#include <set>\n#include <stack>\n#include <vector>\nusing namespace std;\ntypedef long long ll;\ntypedef pair<int, int> PI;\ntypedef double FP;\ntypedef complex<FP> pt;\ntypedef pt P;\ntypedef pair<pt, pt> line;\n\nint main(int argc, char *argv[]) {\n ll n, m;\n while (cin >> n >> m, n) {\n ll ha[n];\n int len[n];\n for(int i = 0; i < n; ++i) {\n string a;\n cin >> a;\n ll h = 0;\n len[i] = a.size();\n for(char& c : a)\n h = h * 31 + c;\n ha[i] = h;\n }\n ll po[1000];\n po[0] = 1;\n for(int i = 0; i < 999; ++i) po[i + 1] = po[i] * 31;\n int ans = 0;\n string text;\n for(int jjj = 0; jjj < m; ++jjj) {\n string a;\n cin >> a;\n text += a;\n }\n ll gha[6000];\n for(int i = 0; i < text.size(); ++i) gha[i + 1] = gha[i] * 31 + text[i];\n bool vis[250][1 << n];\n for(int i = 0; i < 250; ++i) for(int j = 0; j < 1 << n; ++j) vis[i][j] = 0;\n for(int i = 0; i < 250; ++i) vis[i][0] = 1;\n for(int j = 0; j < text.size() + 1; ++j) {\n int cur = j % 250;\n for(int i = 0; i < 1 << n; ++i) {\n if (!vis[cur][i])\n continue;\n for(int k = 0; k < n; ++k) if ((~i & (1 << k)) && len[k] + j <= text.size() &&\n ha[k] == gha[j + len[k]] - gha[j] * po[len[k]])\n vis[(j + len[k]) % 250][i | (1 << k)] = 1;\n }\n ans += vis[cur][(1 << n) - 1];\n for(int i = 0; i < 1 << n; ++i) vis[cur][i] = 0;\n vis[cur][0] = 1;\n }\n cout << ans << \"\\n\";\n }\n return 0;\n}", "accuracy": 1, "time_ms": 2490, "memory_kb": 4340, "score_of_the_acc": -0.3289, "final_rank": 6 }, { "submission_id": "aoj_1291_8819782", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n#define rep(i, x, y) for (int i = (x); i < (y); ++i)\n#define debug(x) #x << \"=\" << (x)\n#ifdef DEBUG\n#define _GLIBCXX_DEBUG\n#define print(x) std::cerr << debug(x) << \" (L:\" << __LINE__ << \")\" << std::endl\n#else\n#define print(x)\n#endif\nconst int inf = 1e9;\nconst int64_t inf64 = 1e18;\nconst double eps = 1e-9;\ntemplate <typename T> ostream &operator<<(ostream &os, const vector<T> &vec) {\n os << \"[\";\n for (const auto &v : vec) {\n os << v << \",\";\n }\n os << \"]\";\n return os;\n}\nvoid solve(int n, int m) {\n vector<string> e(n);\n string ts;\n int concatenated_len = 0;\n rep(i, 0, n) {\n cin >> e[i];\n concatenated_len += e[i].size();\n }\n ts.reserve(m); // reserve memory for string\n rep(i, 0, m) {\n string t;\n cin >> t;\n ts += t;\n }\n vector<int> sum_len(1 << n);\n rep(i, 0, 1 << n) {\n rep(j, 0, n) {\n if (((i >> j) & 1) == 0)\n continue;\n sum_len[i] += e[j].size();\n }\n }\n static bool p[5000][1 << 12];\n fill_n((bool *)p, 5000 * (1 << 12), false);\n vector<vector<int>> heads(ts.size());\n rep(i, 0, ts.size()) p[i][0] = true;\n rep(i, 0, ts.size()) {\n rep(j, 0, n) {\n if (i + e[j].size() > ts.size() or !equal(e[j].begin(), e[j].end(), ts.begin() + i))\n continue;\n p[i][1 << j] = true;\n heads[i].push_back(j);\n }\n }\n int ans = 0;\n for (int i = ts.size() - 1; i >= 0; --i) {\n rep(j, 1, 1 << n) {\n if (p[i][j] or i + sum_len[j] > ts.size())\n continue;\n for (int k : heads[i]) {\n if (((j >> k) & 1) == 0)\n continue;\n int i2 = i + e[k].size();\n if (p[i2][j & (~(1 << k))]) {\n p[i][j] = true;\n break;\n }\n }\n }\n if (p[i][(1 << n) - 1])\n ++ans;\n }\n cout << ans << endl;\n}\nint main() {\n std::cin.tie(0);\n std::ios::sync_with_stdio(false);\n cout.setf(ios::fixed);\n cout.precision(10);\n for (;;) {\n int n, m;\n cin >> n >> m;\n if (n == 0 and m == 0)\n break;\n solve(n, m);\n }\n return 0;\n}", "accuracy": 1, "time_ms": 1450, "memory_kb": 23684, "score_of_the_acc": -0.3429, "final_rank": 7 }, { "submission_id": "aoj_1291_8819778", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\n#define rep(i, x, y) for (int i = (x); i < (y); ++i)\n#define debug(x) #x << \"=\" << (x)\n\n#ifdef DEBUG\n#define _GLIBCXX_DEBUG\n#define print(x) std::cerr << debug(x) << \" (L:\" << __LINE__ << \")\" << std::endl\n#else\n#define print(x)\n#endif\n\nconst int inf = 1e9;\nconst int64_t inf64 = 1e18;\nconst double eps = 1e-9;\n\ntemplate <typename T> ostream &operator<<(ostream &os, const vector<T> &vec) {\n os << \"[\";\n for (const auto &v : vec) {\n os << v << \",\";\n }\n os << \"]\";\n return os;\n}\n\nvoid solve(int n, int m) {\n vector<string> e(n);\n string ts;\n int concatenated_len = 0;\n rep(i, 0, n) {\n cin >> e[i];\n concatenated_len += e[i].size();\n }\n rep(i, 0, m) {\n string t;\n cin >> t;\n ts += t;\n }\n vector<int> sum_len(1 << n);\n rep(i, 0, 1 << n) {\n rep(j, 0, n) {\n if (((i >> j) & 1) == 0)\n continue;\n sum_len[i] += e[j].size();\n }\n }\n static bool p[5000][1 << 12];\n fill_n((bool *)p, 5000 * (1 << 12), false);\n \n int ts_size = ts.size();\n \n vector<vector<int>> heads(ts_size);\n heads.reserve(ts_size); // preallocate memory\n \n rep(i, 0, ts_size) p[i][0] = true;\n rep(i, 0, ts_size) {\n rep(j, 0, n) {\n if (i + e[j].size() > ts_size || e[j].compare(0, e[j].size(), ts, i, e[j].size()) != 0)\n continue;\n p[i][1 << j] = true;\n heads[i].push_back(j);\n }\n }\n int ans = 0;\n for (int i = ts_size - 1; i >= 0; --i) {\n rep(j, 1, 1 << n) {\n if (p[i][j] or i + sum_len[j] > ts_size)\n continue;\n for (int k : heads[i]) {\n if (((j >> k) & 1) == 0)\n continue;\n int i2 = i + e[k].size();\n if (p[i2][j & (~(1 << k))]) {\n p[i][j] = true;\n break;\n }\n }\n }\n if (p[i][(1 << n) - 1])\n ++ans;\n }\n cout << ans << '\\n'; // use '\\n' instead of std::endl\n}\n\nint main() {\n std::cin.tie(0);\n std::ios::sync_with_stdio(false);\n cout.setf(ios::fixed);\n cout.precision(10);\n for (;;) {\n int n, m;\n cin >> n >> m;\n if (n == 0 and m == 0)\n break;\n solve(n, m);\n }\n return 0;\n}", "accuracy": 1, "time_ms": 1510, "memory_kb": 23900, "score_of_the_acc": -0.3588, "final_rank": 8 }, { "submission_id": "aoj_1291_8819771", "code_snippet": "#include <iostream>\n#include <vector>\n#include <string.h>\n\n#define rep(i, x, y) for (int i = (x); i < (y); ++i)\n#define debug(x) #x << \"=\" << (x)\n#ifdef DEBUG\n#define _GLIBCXX_DEBUG\n#define print(x) std::cerr << debug(x) << \" (L:\" << __LINE__ << \")\" << std::endl\n#else\n#define print(x)\n#endif\nconst int inf = 1e9;\nconst int64_t inf64 = 1e18;\nconst double eps = 1e-9;\n\nvoid solve(int n, int m) {\n std::vector<std::string> e(n);\n std::string ts;\n ts.reserve(m); // Reserve memory for ts\n rep(i, 0, n) {\n std::cin >> e[i];\n }\n rep(i, 0, m) {\n std::string t;\n std::cin >> t;\n ts += t;\n }\n std::vector<int> sum_len(1 << n);\n rep(i, 0, 1 << n) {\n rep(j, 0, n) {\n if (((i >> j) & 1) == 0)\n continue;\n sum_len[i] += e[j].size();\n }\n }\n static bool p[5000][1 << 12];\n memset(p, false, sizeof(p)); // Use memset to initialize the boolean array\n std::vector<std::vector<int>> heads(ts.size());\n rep(i, 0, ts.size()) p[i][0] = true;\n rep(i, 0, ts.size()) {\n rep(j, 0, n) {\n if (i + e[j].size() > ts.size() or e[j] != ts.substr(i, e[j].size()))\n continue;\n p[i][1 << j] = true;\n heads[i].push_back(j);\n }\n }\n int ans = 0;\n for (int i = ts.size() - 1; i >= 0; --i) {\n rep(j, 1, 1 << n) {\n if (p[i][j] or i + sum_len[j] > ts.size())\n continue;\n for (int k : heads[i]) {\n if (((j >> k) & 1) == 0)\n continue;\n int i2 = i + e[k].size();\n if (p[i2][j & (~(1 << k))]) {\n p[i][j] = true;\n break;\n }\n }\n }\n if (p[i][(1 << n) - 1])\n ++ans;\n }\n std::cout << ans << std::endl;\n}\nint main() {\n std::cin.tie(0);\n std::ios::sync_with_stdio(false);\n std::cout.setf(std::ios::fixed);\n std::cout.precision(10);\n for (;;) {\n int n, m;\n std::cin >> n >> m;\n if (n == 0 and m == 0)\n break;\n solve(n, m);\n }\n return 0;\n}", "accuracy": 1, "time_ms": 1610, "memory_kb": 23764, "score_of_the_acc": -0.379, "final_rank": 12 }, { "submission_id": "aoj_1291_8819768", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n#define rep(i, x, y) for (int i = (x); i < (y); ++i)\n#define debug(x) #x << \"=\" << (x)\n#ifdef DEBUG\n#define _GLIBCXX_DEBUG\n#define print(x) std::cerr << debug(x) << \" (L:\" << __LINE__ << \")\" << std::endl\n#else\n#define print(x)\n#endif\nconst int inf = 1e9;\nconst int64_t inf64 = 1e18;\nconst double eps = 1e-9;\ntemplate <typename T> ostream &operator<<(ostream &os, const vector<T> &vec) {\n os << \"[\";\n for (const auto &v : vec) {\n os << v << \",\";\n }\n os << \"]\";\n return os;\n}\nvoid solve(int n, int m) {\n vector<string> e(n);\n stringstream ssts; // replace with stringstream\n int concatenated_len = 0;\n rep(i, 0, n) {\n cin >> e[i];\n concatenated_len += e[i].size();\n }\n rep(i, 0, m) {\n string t;\n cin >> t;\n ssts << t; // use stringstream for concatenation\n }\n string ts = ssts.str(); // convert stringstream to string\n vector<int> sum_len(1 << n);\n rep(i, 0, 1 << n) {\n rep(j, 0, n) {\n if (((i >> j) & 1) == 0)\n continue;\n sum_len[i] += e[j].size();\n }\n }\n static bool p[5000][1 << 12];\n fill_n((bool *)p, 5000 * (1 << 12), false);\n vector<vector<int>> heads(ts.size());\n rep(i, 0, ts.size()) p[i][0] = true;\n rep(i, 0, ts.size()) {\n rep(j, 0, n) {\n if (i + e[j].size() > ts.size() or !equal(e[j].begin(), e[j].end(), ts.begin() + i))\n continue;\n p[i][1 << j] = true;\n heads[i].push_back(j);\n }\n }\n int ans = 0;\n for (int i = ts.size() - 1; i >= 0; --i) {\n rep(j, 1, 1 << n) {\n if (p[i][j] or i + sum_len[j] > ts.size())\n continue;\n for (int k : heads[i]) {\n if (((j >> k) & 1) == 0)\n continue;\n int i2 = i + e[k].size();\n if (p[i2][j & (~(1 << k))]) {\n p[i][j] = true;\n break;\n }\n }\n }\n if (p[i][(1 << n) - 1])\n ++ans;\n }\n cout << ans << endl;\n}\nint main() {\n std::cin.tie(0);\n std::ios::sync_with_stdio(false);\n cout.setf(ios::fixed);\n cout.precision(10);\n for (;;) {\n int n, m;\n cin >> n >> m;\n if (n == 0 and m == 0)\n break;\n solve(n, m);\n }\n return 0;\n}", "accuracy": 1, "time_ms": 1530, "memory_kb": 23812, "score_of_the_acc": -0.3621, "final_rank": 9 }, { "submission_id": "aoj_1291_8819763", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n#define rep(i, x, y) for (int i = (x); i < (y); ++i)\n#define debug(x) #x << \"=\" << (x)\n#ifdef DEBUG\n#define _GLIBCXX_DEBUG\n#define print(x) std::cerr << debug(x) << \" (L:\" << __LINE__ << \")\" << std::endl\n#else\n#define print(x)\n#endif\nconst int inf = 1e9;\nconst int64_t inf64 = 1e18;\nconst double eps = 1e-9;\ntemplate <typename T> ostream &operator<<(ostream &os, const vector<T> &vec) {\n os << \"[\";\n for (const auto &v : vec) {\n os << v << \",\";\n }\n os << \"]\";\n return os;\n}\nvoid solve(int n, int m, vector<string> &e, string &ts) {\n int concatenated_len = 0;\n rep(i, 0, n) {\n concatenated_len += e[i].size();\n }\n vector<int> sum_len(1 << n);\n static bool p[5000][1 << 12];\n fill_n((bool *)p, 5000 * (1 << 12), false);\n vector<vector<int>> heads(ts.size());\n rep(i, 0, ts.size()) p[i][0] = true;\n rep(i, 0, ts.size()) {\n rep(j, 0, n) {\n if (i + e[j].size() > ts.size() or e[j] != ts.substr(i, e[j].size()))\n continue;\n p[i][1 << j] = true;\n heads[i].push_back(j);\n }\n }\n int ans = 0;\n for (int i = ts.size() - 1; i >= 0; --i) {\n rep(j, 1, 1 << n) {\n if (p[i][j])\n continue;\n if (sum_len[j] == 0) {\n rep(k, 0, n) {\n if (((j >> k) & 1) == 0)\n continue;\n sum_len[j] += e[k].size();\n }\n }\n if (i + sum_len[j] > ts.size())\n continue;\n for (int k : heads[i]) {\n if (((j >> k) & 1) == 0)\n continue;\n int i2 = i + e[k].size();\n if (p[i2][j & (~(1 << k))]) {\n p[i][j] = true;\n break;\n }\n }\n }\n if (p[i][(1 << n) - 1])\n ++ans;\n }\n cout << ans << endl;\n}\nint main() {\n std::cin.tie(0);\n std::ios::sync_with_stdio(false);\n cout.setf(ios::fixed);\n cout.precision(10);\n for (;;) {\n int n, m;\n cin >> n >> m;\n if (n == 0 and m == 0)\n break;\n vector<string> e(n);\n string ts;\n for(auto &s : e)\n cin >> s;\n for(int i = 0; i < m; i++){\n string t;\n cin >> t;\n ts += t;\n }\n solve(n, m, e, ts);\n }\n return 0;\n}", "accuracy": 1, "time_ms": 1650, "memory_kb": 23876, "score_of_the_acc": -0.3892, "final_rank": 13 }, { "submission_id": "aoj_1291_8807710", "code_snippet": "#include <iostream>\n#include <string>\n#include <vector>\nusing namespace std;\n\nint n, m;\nvector<long long> gha;\n\nvoid solve() {\n vector<long long> ha(n);\n vector<int> len(n);\n for (int i = 0; i < n; ++i) {\n string a;\n cin >> a;\n long long h = 0;\n len[i] = a.size();\n for (char c : a) {\n h = h * 31 + c;\n }\n ha[i] = h;\n }\n\n vector<long long> po(1000);\n po[0] = 1;\n for (int i = 0; i < 999; ++i) {\n po[i + 1] = po[i] * 31;\n }\n\n int ans = 0;\n string text;\n for (int jjj = 0; jjj < m; ++jjj) {\n string a;\n cin >> a;\n text += a;\n }\n\n gha.resize(text.size() + 1);\n for (int i = 0; i < text.size(); ++i) {\n gha[i + 1] = gha[i] * 31 + text[i];\n }\n\n vector<vector<bool>> vis(250, vector<bool>(1 << n));\n for (int i = 0; i < 250; ++i) {\n fill(vis[i].begin(), vis[i].end(), false);\n vis[i][0] = true;\n }\n\n for (int j = 0; j <= text.size(); ++j) {\n int cur = j % 250;\n for (int i = 0; i < (1 << n); ++i) {\n if (!vis[cur][i]) continue;\n for (int k = 0; k < n; ++k) {\n if ((~i & (1 << k)) && len[k] + j <= text.size() &&\n ha[k] == gha[j + len[k]] - gha[j] * po[len[k]]) {\n vis[(j + len[k]) % 250][i | (1 << k)] = true;\n }\n }\n }\n ans += vis[cur][(1 << n) - 1];\n fill(vis[cur].begin(), vis[cur].end(), false);\n vis[cur][0] = true;\n }\n\n cout << ans << endl;\n}\n\nint main() {\n while (cin >> n >> m, n) {\n solve();\n }\n return 0;\n}", "accuracy": 1, "time_ms": 2970, "memory_kb": 3640, "score_of_the_acc": -0.4254, "final_rank": 16 }, { "submission_id": "aoj_1291_8807699", "code_snippet": "#include <algorithm>\n#include <iostream>\n#include <string>\n#include <vector>\nusing namespace std;\n\nint n, m;\nvector<long long> gha(6000);\nvector<long long> po(1000);\n\nvoid solve() {\n vector<long long> ha(n);\n vector<int> len(n);\n for (int i = 0; i < n; ++i) {\n string a;\n cin >> a;\n long long h = 0;\n len[i] = a.size();\n for (char ch : a)\n h = h * 31 + ch;\n ha[i] = h;\n }\n\n po[0] = 1;\n for (int i = 0; i < 999; ++i)\n po[i + 1] = po[i] * 31;\n\n int ans = 0;\n string text;\n for (int jjj = 0; jjj < m; ++jjj) {\n string a;\n cin >> a;\n text += a;\n }\n for (int i = 0; i < text.size(); ++i)\n gha[i + 1] = gha[i] * 31 + text[i];\n\n vector<vector<int>> vis(250, vector<int>(1 << n));\n\n for (int i = 0; i < 250; ++i)\n vis[i][0] = 1;\n\n for (int j = 0; j < text.size() + 1; ++j) {\n int cur = j % 250;\n for (int i = 0; i < (1 << n); ++i) {\n if (!vis[cur][i])\n continue;\n for (int k = 0; k < n; ++k) {\n if ((~i & (1 << k)) && len[k] + j <= text.size() &&\n ha[k] == gha[j + len[k]] - gha[j] * po[len[k]])\n {\n vis[(j + len[k]) % 250][i | (1 << k)] = 1;\n }\n }\n }\n ans += vis[cur][(1 << n) - 1];\n for (int i = 0; i < (1 << n); ++i)\n vis[cur][i] = 0;\n vis[cur][0] = 1;\n }\n\n cout << ans << endl;\n}\n\nint main() {\n while (cin >> n >> m, n)\n solve();\n return 0;\n}", "accuracy": 1, "time_ms": 2670, "memory_kb": 7240, "score_of_the_acc": -0.4047, "final_rank": 15 }, { "submission_id": "aoj_1291_8807689", "code_snippet": "#include <iostream>\n#include <vector>\n#include <string>\n#include <algorithm>\n\nvoid solve(int n, int m) {\n std::vector<std::string> e(n);\n std::string ts;\n int concatenated_len = 0;\n for (int i = 0; i < n; ++i) {\n std::cin >> e[i];\n concatenated_len += e[i].size();\n }\n for (int i = 0; i < m; ++i) {\n std::string t;\n std::cin >> t;\n ts += t;\n }\n std::vector<int> sum_len(1 << n);\n for (int i = 0; i < (1 << n); ++i) {\n for (int j = 0; j < n; ++j) {\n if (((i >> j) & 1) == 0)\n continue;\n sum_len[i] += e[j].size();\n }\n }\n std::vector<std::vector<int>> heads(ts.size());\n std::vector<std::vector<bool>> p(ts.size(), std::vector<bool>(1 << n, false));\n for (int i = 0; i < ts.size(); ++i) p[i][0] = true;\n for (int i = 0; i < ts.size(); ++i) {\n for (int j = 0; j < n; ++j) {\n if (i + e[j].size() > ts.size() || !std::equal(e[j].begin(), e[j].end(), ts.begin() + i))\n continue;\n p[i][1 << j] = true;\n heads[i].push_back(j);\n }\n }\n int ans = 0;\n for (int i = ts.size() - 1; i >= 0; --i) {\n for (int j = 1; j < (1 << n); ++j) {\n if (p[i][j] || i + sum_len[j] > ts.size())\n continue;\n for (int k : heads[i]) {\n if (((j >> k) & 1) == 0)\n continue;\n int i2 = i + e[k].size();\n if (p[i2][j & (~(1 << k))]) {\n p[i][j] = true;\n break;\n }\n }\n }\n if (p[i][(1 << n) - 1])\n ++ans;\n }\n std::cout << ans << \"\\n\";\n}\n\nint main() {\n std::ios::sync_with_stdio(false);\n std::cin.tie(nullptr);\n std::cout.tie(nullptr);\n std::cout.setf(std::ios::fixed);\n std::cout.precision(10);\n for (;;) {\n int n, m;\n std::cin >> n >> m;\n if (n == 0 && m == 0)\n break;\n solve(n, m);\n }\n return 0;\n}", "accuracy": 1, "time_ms": 1480, "memory_kb": 6688, "score_of_the_acc": -0.1368, "final_rank": 1 } ]
aoj_1290_cpp
Problem F: Traveling Cube On a small planet named Bandai, a landing party of the starship Tadamigawa discovered colorful cubes traveling on flat areas of the planet surface, which the landing party named beds. A cube appears at a certain position on a bed, travels on the bed for a while, and then disappears. After a longtime observation, a science officer Lt. Alyssa Ogawa of Tadamigawa found the rule how a cube travels on a bed. A bed is a rectangular area tiled with squares of the same size. One of the squares is colored red, one colored green, one colored blue, one colored cyan, one colored magenta, one colored yellow, one or more colored white, and all others, if any, colored black. Initially, a cube appears on one of the white squares. The cube’s faces are colored as follows. top red bottom cyan north green south magenta east blue west yellow The cube can roll around a side of the current square at a step and thus rolls on to an adjacent square. When the cube rolls on to a chromatically colored (red, green, blue, cyan, magenta or yellow) square, the top face of the cube after the roll should be colored the same. When the cube rolls on to a white square, there is no such restriction. The cube should never roll on to a black square. Throughout the travel, the cube can visit each of the chromatically colored squares only once, and any of the white squares arbitrarily many times. As already mentioned, the cube can never visit any of the black squares. On visit to the final chromatically colored square, the cube disappears. Somehow the order of visits to the chromatically colored squares is known to us before the travel starts. Your mission is to find the least number of steps for the cube to visit all the chromatically colored squares in the given order. Input The input is a sequence of datasets. A dataset is formatted as follows: w d c 11 . . . c w 1 . . . . . . c 1 d . . . c wd v 1 v 2 v 3 v 4 v 5 v 6 The first line is a pair of positive integers w and d separated by a space. The next d lines are w-character-long strings c 11 . . . c w 1 , . . . , c 1 d . . . c wd with no spaces. Each character cij is one of the letters r, g, b, c, m, y, w and k, which stands for red, green, blue, cyan, magenta, yellow, white and black respectively, or a sign #. Each of r, g, b, c, m, y and # occurs once and only once in a dataset. The last line is a six-character-long string v 1 v 2 v 3 v 4 v 5 v 6 which is a permutation of “rgbcmy”. The integers w and d denote the width (the length from the east end to the west end) and the depth (the length from the north end to the south end) of a bed. The unit is the length of a side of a square. You can assume that neither w nor d is greater than 30. Each character c ij shows the color of a square in the bed. The characters c 11 , c w 1 , c 1 d and c wd correspond to the north-west corner, the north-east corner, the south-west ...(truncated)
[ { "submission_id": "aoj_1290_10946409", "code_snippet": "#include <cstdio>\n#include <cstdlib>\n#include <cstring>\n#include <queue>\n#include <algorithm>\n\nusing namespace std;\n\n#define K\t0\t// Black\n#define W\t128\t// White\n#define R\t1\t// Red\n#define G\t2\t// Green\n#define B\t3\t// Blue\n#define C\t4\t// Cyan\n#define\tM\t5\t// Magenta\n#define\tY\t6\t// Yellow\n#define S\t192\t// Start Point | White\n\nunsigned char rev[256];\nint n, m;\nint sx, sy;\nunsigned char map[32][32];\nunsigned char Map[256];\nint seen[1 << 22]; // 8 * 8 * 8 * 8\nchar data[32];\n\nconst char *name[] = {\n\t\"Undefined\",\n\t\"Red\",\n\t\"Green\",\n\t\"Blue\",\n\t\"Cyan\",\n\t\"Magenta\",\n\t\"Yellow\"\n};\n\ninline int serialize(const int top, const int front, const int left, const int now) {\n\treturn (top << 9) | (front << 6) | (left << 3) | now;\n}\n\ninline void recover(const int serial, int &top, int &front, int &left, int &now) {\n\ttop = serial >> 9;\n\tfront = (serial >> 6) & 7;\n\tleft = (serial >> 3) & 7;\n\tnow = serial & 7;\n}\n\ninline bool rotateWest(int &x, int &y, int &serial) {\n\tif(!y) return false; // Can't move west.\n\tstatic int top, front, left, now, ntop, nfront, nleft;\n\tstatic int ny;\n\tny = y - 1;\n\tif (map[x][ny] == 0) return false; // Black\n\trecover(serial, top, front, left, now);\n\tntop = rev[left]; nleft = top; nfront = front;\n\tif (map[x][ny] & 128) { // White\n\t\ty = ny;\n\t\tserial = serialize(ntop, nfront, nleft, now);\n\t\treturn true;\n\t}\n\tif ( ntop != map[x][ny] || data[now] != map[x][ny] ) return false;\n\ty = ny, serial = serialize(ntop, nfront, nleft, now + 1); // Move forward\n\treturn true;\n}\n\ninline bool rotateEast(int &x, int &y, int &serial) {\n\tif(y + 1 == m) return false; // Can't move east.\n\tstatic int top, front, left, now, ntop, nfront, nleft;\n\tstatic int ny;\n\tny = y + 1;\n\tif (map[x][ny] == 0) return false; // Black\n\trecover(serial, top, front, left, now);\n\tntop = left; nleft = rev[top]; nfront = front;\n\tif (map[x][ny] & 128) { // White\n\t\ty = ny;\n\t\tserial = serialize(ntop, nfront, nleft, now);\n\t\treturn true;\n\t}\n\tif ( ntop != map[x][ny] || data[now] != map[x][ny] ) return false;\n\ty = ny, serial = serialize(ntop, nfront, nleft, now + 1); // Move forward\n\treturn true;\n}\n\ninline bool rotateNorth(int &x, int &y, int &serial) {\n\tif(!x) return false; // Can't move north.\n\tstatic int top, front, left, now, ntop, nfront, nleft;\n\tstatic int nx;\n\tnx = x - 1;\n\tif (map[nx][y] == 0) return false; // Black\n\trecover(serial, top, front, left, now);\n\tntop = front; nleft = left; nfront = rev[top];\n\tif (map[nx][y] & 128) { // White\n\t\tx = nx;\n\t\tserial = serialize(ntop, nfront, nleft, now);\n\t\treturn true;\n\t}\n\tif ( ntop != map[nx][y] || data[now] != map[nx][y] ) return false;\n\tx = nx, serial = serialize(ntop, nfront, nleft, now + 1); // Move forward\n\treturn true;\n}\n\ninline bool rotateSouth(int &x, int &y, int &serial) {\n\tif(x + 1 == n) return false; // Can't move north.\n\tstatic int top, front, left, now, ntop, nfront, nleft;\n\tstatic int nx;\n\tnx = x + 1;\n\tif (map[nx][y] == 0) return false; // Black\n\trecover(serial, top, front, left, now);\n\tntop = rev[front]; nleft = left; nfront = top;\n\tif (map[nx][y] & 128) { // White\n\t\tx = nx;\n\t\tserial = serialize(ntop, nfront, nleft, now);\n\t\treturn true;\n\t}\n\tif ( ntop != map[nx][y] || data[now] != map[nx][y] ) return false;\n\tx = nx, serial = serialize(ntop, nfront, nleft, now + 1); // Move forward\n\treturn true;\n}\n\ninline int zip(const int x, const int y, const int serial) {\n\treturn (x << 17) | (y << 12) | serial;\n}\n\ninline void unzip(const int zipped, int &x, int &y, int &serial) {\n\tx = zipped >> 17;\n\ty = (zipped >> 12) & 31;\n\tserial = zipped & 4095;\n}\n\nvoid printSerial(const int serial) {\n\tstatic int top, front, left, now;\n\trecover(serial, top, front, left, now);\n\tfprintf(stderr, \"Status: Top = %s, Front = %s, Left = %s, Now = %d\\n\", name[top], name[front], name[left], now);\n}\n\nvoid printZip(const int zipped) {\n\tstatic int x, y, serial;\n\tunzip(zipped, x, y, serial);\n\tfprintf(stderr, \"CurPos: x = %d, y = %d\\n\", x, y);\n\tprintSerial(serial);\n}\n\nint getAns() {\n\tstatic queue<int> Q;\n\twhile(Q.size()) Q.pop();\n\tmemset(seen, -1, sizeof seen);\n\tstatic int zipped, nzipped;\n\tzipped = zip(sx, sy, serialize(R, M, Y, 0));\n\tQ.push(zipped);\n\tseen[zipped] = 0;\n\twhile(Q.size()) {\n#define RECOVER tx = cx, ty = cy, ts = cs\n\t\tzipped = Q.front();\n\t\tQ.pop();\n\t\tstatic int cx, cy, cs, tx, ty, ts;\n\t\tunzip(zipped, cx, cy, cs);\n\t\tRECOVER;\n\t\tif(rotateEast(tx, ty, ts)) {\n\t\t\tif((ts & 7) == 6) return seen[zipped] + 1;\n\t\t\tnzipped = zip(tx, ty, ts);\n\t\t\tif(seen[nzipped] < 0) {\n\t\t\t\tseen[nzipped] = seen[zipped] + 1;\n\t\t\t\tQ.push(nzipped);\n\t\t\t}\n\t\t}\n\t\tRECOVER;\n\t\tif(rotateWest(tx, ty, ts)) {\n\t\t\tif((ts & 7) == 6) return seen[zipped] + 1;\n\t\t\tnzipped = zip(tx, ty, ts);\n\t\t\tif(seen[nzipped] < 0) {\n\t\t\t\tseen[nzipped] = seen[zipped] + 1;\n\t\t\t\tQ.push(nzipped);\n\t\t\t}\n\t\t}\n\t\tRECOVER;\n\t\tif(rotateNorth(tx, ty, ts)) {\n\t\t\tif((ts & 7) == 6) return seen[zipped] + 1;\n\t\t\tnzipped = zip(tx, ty, ts);\n\t\t\tif(seen[nzipped] < 0) {\n\t\t\t\tseen[nzipped] = seen[zipped] + 1;\n\t\t\t\tQ.push(nzipped);\n\t\t\t}\n\t\t}\n\t\tRECOVER;\n\t\tif(rotateSouth(tx, ty, ts)) {\n\t\t\tif((ts & 7) == 6) return seen[zipped] + 1;\n\t\t\tnzipped = zip(tx, ty, ts);\n\t\t\tif(seen[nzipped] < 0) {\n\t\t\t\tseen[nzipped] = seen[zipped] + 1;\n\t\t\t\tQ.push(nzipped);\n\t\t\t}\n\t\t}\n#undef RECOVER\n\t}\n\treturn -1;\n}\n\nint main() {\n\tmemset(Map, 0, sizeof Map);\n\tMap['#'] = S;\n\tMap['w'] = W;\n\tMap['k'] = K;\n\tMap['r'] = R;\n\tMap['g'] = G;\n\tMap['b'] = B;\n\tMap['c'] = C;\n\tMap['m'] = M;\n\tMap['y'] = Y;\n\trev[R] = C;\n\trev[C] = R;\n\trev[G] = M;\n\trev[M] = G;\n\trev[B] = Y;\n\trev[Y] = B;\n\twhile(scanf(\"%d%d\", &m, &n), n) {\n\t\tregister int i, j;\n\t\tfor(i = 0; i < n; ++ i) {\n\t\t\tscanf(\"%s\", map[i]);\n\t\t\tfor(j = 0; j < m; ++ j) {\n\t\t\t\tmap[i][j] = Map[map[i][j]];\n\t\t\t\tif(map[i][j] & 64) sx = i, sy = j;\n\t\t\t}\n\t\t}\n\t\tscanf(\"%s\", data);\n\t\tfor(i = 0; i < 6; ++ i) data[i] = Map[data[i]];\n\t\tstatic int ans;\n\t\tans = getAns();\n\t\tif (ans < 0) {\n\t\t\tputs(\"unreachable\");\n\t\t\tcontinue;\n\t\t}\n\t\tprintf(\"%d\\n\", ans);\n\t}\n}", "accuracy": 1, "time_ms": 40, "memory_kb": 19372, "score_of_the_acc": -0.2668, "final_rank": 7 }, { "submission_id": "aoj_1290_10943515", "code_snippet": "#include <cstdio>\n#include <iostream>\n#include <cstdlib>\n#include <cstring>\n#include <cmath>\n#include <string>\n#include <sstream>\n#include <vector>\n#include <map>\n#include <set>\n#include <list>\n#include <queue>\n#include <deque>\n#include <stack>\n#include <utility>\n#include <algorithm>\n\n#define\t\t\tOK\t\t\tstd::cout << \"------------\" << std::endl;\n#define\t\t\tDEBUG(x)\t\tstd::cout << #x << \" = \" << x << std::endl;\n\nusing namespace std;\n\nconst int maxn = 64;\n\nint W, D;\nchar di[maxn][maxn];\n\nconst int dx[] = {0, 0, -1, 1};\nconst int dy[] = {-1, 1, 0, 0};\n\nstruct St {\n\tint t, b, n, s, e, w;\n\n\tSt(int t=0, int b=1, int n=2, int s=3, int e=4, int w=5) \n\t: t(t),b(b),n(n),s(s),e(e),w(w) { }\n\n\tSt rot(int way) {\n\t\tswitch (way) {\n\t\t\tcase 0 : return St(e, w, n, s, b, t);\t// left\n\t\t\tcase 1 : return St(w, e, n, s, t, b);\t// right\n\t\t\tcase 2 : return St(s, n, t, b, e, w);\t// front\n\t\t\tcase 3 : return St(n, s, b, t, e, w);\t// back\n\t\t}\n\t}\n\tinline int getHash() const {\n\t\treturn t + b*6 + n*36 + s*36*6 + e*36*36 + w*36*36*6;\n\t}\n\tinline bool operator<(const St &a) const {\n\t\treturn getHash() < a.getHash();\n\t}\n};\n\ninline int getColor(char c) {\n\tswitch(c) {\n\t\tcase 'r' : return 0;\n\t\tcase 'c' : return 1;\n\t\tcase 'g' : return 2;\n\t\tcase 'm' : return 3;\n\t\tcase 'b' : return 4;\n\t\tcase 'y' : return 5;\n\t\tdefault : return -1;\n\t}\n}\n\ninline int find(int &x, int &y, char c) {\n\tfor (int i = 0;i < D;i++) {\n\t\tfor (int j = 0;j < W;j++) {\n\t\t\tif (di[i][j] == c) {\n\t\t\t\tx = i;\n\t\t\t\ty = j;\n\t\t\t\treturn 0;\n\t\t\t}\n\t\t}\n\t}\n\treturn -1;\n}\n\nconst int maxq = 100000;\nint X[maxq], Y[maxq], bt, tp;\nSt Que[maxq];\n\nmap <St,int> Map[maxn][maxn];\n\ninline void push(int x, int y, St a) {\n\tX[tp] = x; Y[tp] = y; Que[tp] = a;\n\ttp ++; tp %= maxq;\n}\n\ninline bool inBound(int x, int y) {\n\treturn 0 <= x && x < D && 0 <= y && y < W && (di[x][y] == 'w' || di[x][y] == '#');\n}\n\ninline void solve(int sx, int sy, int tx, int ty) {\n\tfor (int i = 0;i < D;i++) {\n\t\tfor (int j = 0;j < W;j++) {\n\t\t\tif (!(sx == i && sy == j))\n\t\t\t\tMap[i][j].clear();\n\t\t}\n\t}\n\tbt = tp = 0;\n\tfor (map<St,int>::iterator it = Map[sx][sy].begin();it != Map[sx][sy].end();it++) {\n\t\tpush(sx, sy, it->first);\n\t}\n\twhile (bt < tp) {\n\t\tint x = X[bt], y = Y[bt];\n\t\tSt a = Que[bt];\n\t\tbt ++; bt %= maxq;\n\n\t\tfor (int i = 0;i < 4;i++) {\n\t\t\tint xx = x + dx[i], yy = y + dy[i];\n\t\t\tSt b = a.rot(i);\n\t\t\tif (inBound(xx, yy) && (Map[xx][yy].find(b) == Map[xx][yy].end() || Map[xx][yy][b] > Map[x][y][a] + 1)) {\n\t\t\t\tMap[xx][yy][b] = Map[x][y][a] + 1;\n\t\t\t\tpush(xx, yy, b);\n\t\t\t}\n\t\t\tif (xx == tx && yy == ty && b.t == getColor(di[xx][yy]) && (Map[xx][yy].find(b) == Map[xx][yy].end() || Map[xx][yy][b] > Map[x][y][a] + 1)) {\n\t\t\t\tMap[xx][yy][b] = Map[x][y][a] + 1;\n\t\t\t}\n\t\t}\n\t}\n}\n\nint main()\n{\n\twhile (scanf(\"%d%d\\n\", &W, &D), !(W == 0 && D == 0)) {\n\t\tfor (int i = 0;i < D;i++) {\n\t\t\tfor (int j = 0;j < W;j++) {\n\t\t\t\tMap[i][j].clear();\n\t\t\t}\n\t\t}\n\t\tfor (int i = 0;i < D;i++) {\n\t\t\tscanf(\"%s\\n\", di[i]);\n\t\t}\n\t\tchar path[16];\n\t\tscanf(\"%s\\n\", path);\n\t\tint sx, sy, tx, ty;\n\t\tfind(sx, sy, '#');\n\t\tMap[sx][sy][St()] = 0;\n\t\tfor (int i = 0;i < 6;i++) {\n\t\t\tfind(tx, ty, path[i]);\n\t\t\tsolve(sx, sy, tx, ty);\n\t//\t\tcout << Map[tx][ty].size() << endl;\n\t\t\tsx = tx; sy = ty;\n\t\t}\n\t\tif (Map[tx][ty].empty()) {\n\t\t\tputs(\"unreachable\");\n\t\t} else {\n\t\t\tint ans = 100000000;\n\t\t\tfor (map<St, int>::iterator it = Map[tx][ty].begin();it != Map[tx][ty].end();it ++) {\n\t\t\t\tans = min(ans, it->second);\n\t\t\t}\n\t\t\tprintf(\"%d\\n\", ans);\n\t\t}\n\t}\n\treturn 0;\n}", "accuracy": 1, "time_ms": 250, "memory_kb": 7040, "score_of_the_acc": -0.3256, "final_rank": 10 }, { "submission_id": "aoj_1290_10212959", "code_snippet": "#include <iostream>\n#include <vector>\n#include <algorithm>\n#include <utility>\n#include <map>\n#include <queue>\nusing namespace std;\nusing ll = long long;\n#define rep(i,n) for(ll i=0; i<ll(n); i++)\n\n// up 0\n// down 1\n// left 2\n// right 3\n// front 4\n// back 5\n\n// r 0\n// u 1\n// l 2\n// d 3\n\nint face_perm[42][6];\nint face_d[42][4];\nstring colors = \"rcybmg\";\nint dx[4] = { 1,0,-1,0 };\nint dy[4] = { 0,-1,0,1 };\n\n\nvoid enum_face_perm(){\n vector<vector<int>> Q;\n Q.push_back({0,1,2,3,4,5});\n map<vector<int>, int> perms;\n perms[Q[0]] = 0;\n rep(i,Q.size()){\n //cout << i << \" : \"; for(auto a : Q[i]){ cout << a << \" \"; } cout << endl;\n rep(f,2){\n auto a = Q[i];\n auto d0 = a;\n d0[0] = a[2^f];\n d0[1] = a[3^f];\n d0[2] = a[1^f];\n d0[3] = a[0^f];\n if(perms.count(d0) == 0){\n int v = Q.size();\n perms[d0] = v;\n Q.push_back(d0);\n }\n face_d[i][0+f*2] = perms[d0];\n d0 = a;\n d0[0] = a[4^f];\n d0[1] = a[5^f];\n d0[4] = a[1^f];\n d0[5] = a[0^f];\n if(perms.count(d0) == 0){\n int v = Q.size();\n perms[d0] = v;\n Q.push_back(d0);\n }\n face_d[i][1+f*2] = perms[d0];\n }\n }\n rep(i,24) rep(j,6) face_perm[i][j] = Q[i][j];\n}\n\nint H, W;\nint idx(int y, int x){ return y*W+x; }\nint dist[2000][24][6];\n\nint main(){\n cin.tie(nullptr); ios::sync_with_stdio(false);\n enum_face_perm();\n queue<pair<pair<int,int>, int>> que;\n while(true){\n cin >> W >> H; if(H == 0) break;\n vector<string> G(H); rep(y,H) cin >> G[y];\n string g;\n rep(x,W+2) g.push_back('k');\n rep(y,H) g += \"k\" + G[y] + \"k\";\n rep(x,W+2) g.push_back('k');\n H += 2; W += 2;\n int s = g.find('#'); g[s] = 'w';\n int pt[6] = {};\n rep(i,6) pt[i] = g.find(colors[i]);\n int ord[6] = {};\n rep(i,6){ char c; cin >> c; ord[i] = colors.find(c); }\n que.push({{ s, 0 }, 0 });\n int dd[4] = {};\n rep(d,4) dd[d] = dy[d] * W + dx[d];\n rep(i,W*H) rep(j,24) rep(p,6) dist[i][j][p] = -1;\n dist[s][0][0] = 0;\n int ans = 1001001001;\n while(que.size()){\n int u = que.front().first.first;\n int x = que.front().first.second;\n int p = que.front().second;\n int w = dist[u][x][p];\n //cout << u << \" \" << x << \" \" << p << \" \" << w << endl;\n que.pop();\n rep(d,4){\n int nxu = u + dd[d];\n int nxx = face_d[x][d];\n int nxp = p;\n if(g[nxu] != 'w'){\n if(g[nxu] != colors[face_perm[nxx][0]]) continue;\n if(pt[ord[p]] != nxu) continue;\n if(p == 5){ ans = w+1; break; }\n nxp += 1;\n }\n if(dist[nxu][nxx][nxp] != -1) continue;\n dist[nxu][nxx][nxp] = w + 1;\n que.push({ {nxu,nxx}, nxp });\n }\n if(ans < 1001001001) break;\n }\n while(que.size()) que.pop();\n if(ans == 1001001001){ cout << \"unreachable\\n\"; }\n else cout << ans << \"\\n\";\n }\n return 0;\n}", "accuracy": 1, "time_ms": 20, "memory_kb": 4004, "score_of_the_acc": -0.0087, "final_rank": 1 }, { "submission_id": "aoj_1290_9809947", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n#define rep(i, n) for(int i = 0; i < (n); i++)\n#define srep(i, s, t) for(int i = (s); i < (t); i++)\n#define len(x) ((int)(x).size())\n#define all(x) (x).begin(), (x).end()\ntemplate < class T > bool chmin(T& a, T b) { if(a > b) { a = b; return true; } return false; }\ntemplate < class T > bool chmax(T& a, T b) { if(a < b) { a = b; return true; } return false; }\nusing i64 = long long;\nusing f64 = long double;\ni64 floor_div(const i64 n, const i64 d) { assert(d != 0); return n / d - static_cast<i64>((n ^ d) < 0 && n % d != 0); }\ni64 ceil_div(const i64 n, const i64 d) { assert(d != 0); return n / d + static_cast<i64>((n ^ d) >= 0 && n % d != 0); }\n\nusing face = char;\nstruct dice {\n face front, right, back, left, top, bottom;\n\n dice() {}\n dice(string s) {\n front = s[0];\n right = s[1];\n back = s[2];\n left = s[3];\n top = s[4];\n bottom = s[5];\n }\n string state() {\n return \"\"s + front + right + back + left + top + bottom;\n }\n\n face cw(face f) {\n return f;\n }\n\n dice to_front() {\n dice x;\n x.front = top;\n x.top = cw(cw(back));\n x.back = cw(cw(bottom));\n x.bottom = front;\n x.left = cw(left);\n x.right = cw(cw(cw(right)));\n return x;\n }\n dice to_back() {\n dice x;\n x.front = bottom;\n x.top = front;\n x.back = cw(cw(top));\n x.bottom = cw(cw(back));\n x.left = cw(cw(cw(left)));\n x.right = cw(right);\n return x;\n }\n dice to_right() {\n dice x;\n x.right = cw(cw(cw(top)));\n x.top = cw(cw(cw(left)));\n x.left = cw(cw(cw(bottom)));\n x.bottom = cw(cw(cw(right)));\n x.front = cw(front);\n x.back = cw(cw(cw(back)));\n return x;\n }\n dice to_left() {\n dice x;\n x.left = cw(top);\n x.top = cw(right);\n x.right = cw(bottom);\n x.bottom = cw(left);\n x.front = cw(cw(cw(front)));\n x.back = cw(back);\n return x;\n }\n};\n\nint main() {\n cin.tie(0)->sync_with_stdio(0);\n\n while(true) {\n int W, H; cin >> W >> H;\n if(W == 0) return 0;\n vector<string> S(H);\n for(auto& e : S) cin >> e;\n\n int sx, sy;\n rep(x, H) rep(y, W) if(S[x][y] == '#') {\n tie(sx, sy) = make_pair(x, y);\n S[x][y] = 'w';\n }\n\n dice sd;\n sd.top = 'r';\n sd.bottom = 'c';\n sd.back = 'g';\n sd.front = 'm';\n sd.right = 'b';\n sd.left = 'y';\n\n using state = tuple<int, int, int, string>;\n vector<state> ss;\n ss.push_back({0, sx, sy, sd.state()});\n\n const int INF = 1e9;\n\n string gs; cin >> gs;\n for(char gcol : gs) {\n priority_queue<state, vector<state>, greater<state>> q;\n vector dist(H, vector(W, map<string, int>{}));\n for(auto s : ss) {\n auto [c, x, y, d] = s;\n dist[x][y][d] = c;\n q.push(s);\n }\n while(not q.empty()) {\n auto [c, x, y, d] = q.top(); q.pop();\n if(dist[x][y].count(d) and dist[x][y][d] < c) continue;\n\n auto push = [&](int nx, int ny, string nd) {\n if(0 <= nx and nx < H and 0 <= ny and ny < W and (S[nx][ny] == 'w' or (S[nx][ny] == gcol and dice(nd).top == gcol))) {\n if(not dist[nx][ny].count(nd)) dist[nx][ny][nd] = INF;\n if(chmin(dist[nx][ny][nd], c + 1)) q.push({c + 1, nx, ny, nd});\n }\n };\n\n auto [nx, ny] = make_pair(x + 1, y);\n string nd = dice(d).to_front().state();\n push(nx, ny, nd);\n\n tie(nx, ny) = make_pair(x - 1, y);\n nd = dice(d).to_back().state();\n push(nx, ny, nd);\n\n tie(nx, ny) = make_pair(x, y + 1);\n nd = dice(d).to_right().state();\n push(nx, ny, nd);\n\n tie(nx, ny) = make_pair(x, y - 1);\n nd = dice(d).to_left().state();\n push(nx, ny, nd);\n }\n\n auto [gx, gy] = [&] {\n rep(x, H) rep(y, W) if(S[x][y] == gcol) return make_pair(x, y);\n assert(0); \n }();\n ss.clear();\n for(auto [d, c] : dist[gx][gy]) ss.push_back({c, gx, gy, d});\n }\n\n if(ss.empty()) {\n cout << \"unreachable\" << \"\\n\";\n } else {\n int ans = INF;\n for(auto s : ss) chmin(ans, get<0>(s));\n cout << ans << \"\\n\";\n }\n }\n}", "accuracy": 1, "time_ms": 630, "memory_kb": 4364, "score_of_the_acc": -0.7319, "final_rank": 18 }, { "submission_id": "aoj_1290_9735506", "code_snippet": "#include <bits/stdc++.h>\n\nusing namespace std;\n\n#define rep(i, a, b) for (int i = (int)(a); i < (int)(b); i++)\n#define rrep(i, a, b) for (int i = (int)(b)-1; i >= (int)(a); i--)\n#define ALL(v) (v).begin(), (v).end()\n#define UNIQUE(v) sort(ALL(v)), (v).erase(unique(ALL(v)), (v).end())\n#define SZ(v) (int)v.size()\n#define MIN(v) *min_element(ALL(v))\n#define MAX(v) *max_element(ALL(v))\n#define LB(v, x) int(lower_bound(ALL(v), (x)) - (v).begin())\n#define UB(v, x) int(upper_bound(ALL(v), (x)) - (v).begin())\n\nusing uint = unsigned int;\nusing ll = long long int;\nusing ull = unsigned long long;\nusing i128 = __int128_t;\nusing u128 = __uint128_t;\nconst int inf = 0x3fffffff;\nconst ll INF = 0x1fffffffffffffff;\n\ntemplate <typename T> inline bool chmax(T &a, T b) {\n if (a < b) {\n a = b;\n return 1;\n }\n return 0;\n}\ntemplate <typename T> inline bool chmin(T &a, T b) {\n if (a > b) {\n a = b;\n return 1;\n }\n return 0;\n}\ntemplate <typename T, typename U> T ceil(T x, U y) {\n assert(y != 0);\n if (y < 0)\n x = -x, y = -y;\n return (x > 0 ? (x + y - 1) / y : x / y);\n}\ntemplate <typename T, typename U> T floor(T x, U y) {\n assert(y != 0);\n if (y < 0)\n x = -x, y = -y;\n return (x > 0 ? x / y : (x - y + 1) / y);\n}\ntemplate <typename T> int popcnt(T x) {\n return __builtin_popcountll(x);\n}\ntemplate <typename T> int topbit(T x) {\n return (x == 0 ? -1 : 63 - __builtin_clzll(x));\n}\ntemplate <typename T> int lowbit(T x) {\n return (x == 0 ? -1 : __builtin_ctzll(x));\n}\n\ntemplate <class T, class U>\nostream &operator<<(ostream &os, const pair<T, U> &p) {\n os << \"P(\" << p.first << \", \" << p.second << \")\";\n return os;\n}\ntemplate <typename T> ostream &operator<<(ostream &os, const vector<T> &vec) {\n os << \"{\";\n for (int i = 0; i < vec.size(); i++) {\n os << vec[i] << (i + 1 == vec.size() ? \"\" : \", \");\n }\n os << \"}\";\n return os;\n}\ntemplate <typename T, typename U>\nostream &operator<<(ostream &os, const map<T, U> &map_var) {\n os << \"{\";\n for (auto itr = map_var.begin(); itr != map_var.end(); itr++) {\n os << \"(\" << itr->first << \", \" << itr->second << \")\";\n itr++;\n if (itr != map_var.end())\n os << \", \";\n itr--;\n }\n os << \"}\";\n return os;\n}\ntemplate <typename T> ostream &operator<<(ostream &os, const set<T> &set_var) {\n os << \"{\";\n for (auto itr = set_var.begin(); itr != set_var.end(); itr++) {\n os << *itr;\n ++itr;\n if (itr != set_var.end())\n os << \", \";\n itr--;\n }\n os << \"}\";\n return os;\n}\n#ifdef LOCAL\n#define show(...) _show(0, #__VA_ARGS__, __VA_ARGS__)\n#else\n#define show(...) true\n#endif\ntemplate <typename T> void _show(int i, T name) {\n cerr << '\\n';\n}\ntemplate <typename T1, typename T2, typename... T3>\nvoid _show(int i, const T1 &a, const T2 &b, const T3 &...c) {\n for (; a[i] != ',' && a[i] != '\\0'; i++)\n cerr << a[i];\n cerr << \":\" << b << \" \";\n _show(i + 1, a, c...);\n}\n\n/**\n * @brief template\n */\n\nint rots[4][6] = {{1,3,2,4,0,5},{5,1,0,2,4,3},{4,0,2,1,3,5},{2,1,3,5,4,0}};\nint dx[4] = {1,0,-1,0}, dy[4] = {0,1,0,-1};\n\nmap<char,int> mp;\nmap<vector<int>,int> DiceID;\nvector<vector<int>> Dices;\nint Cur = 0;\n\nstruct Dice {\n vector<int> A;\n Dice() {\n A.resize(6);\n iota(ALL(A),0);\n }\n Dice(vector<int> a) : A(a) {};\n int& operator[] (int i) {\n return A[i];\n }\n Dice rot(int dir) {\n Dice Ret;\n rep(i,0,6) Ret[i] = this->A[rots[dir][i]];\n return Ret;\n }\n int top() {\n return this->A[0];\n }\n int getID() {\n if (DiceID.count(this->A)) return DiceID[this->A];\n DiceID[this->A] = Cur;\n Dices.push_back(this->A);\n Cur++;\n return Cur-1;\n }\n};\n\nint main() {\n mp['r'] = 0;\n mp['g'] = 1;\n mp['b'] = 2;\n mp['c'] = 3;\n mp['m'] = 4;\n mp['y'] = 5;\n mp['w'] = 6;\n mp['k'] = -1;\n Dice _;\n _.getID();\nwhile(1) {\n int N, M;\n cin >> M >> N;\n if (N == 0) return 0;\n vector G(N,vector<int>(M));\n vector DP(N,vector(M,vector(24,vector<int>(7,inf))));\n queue<tuple<int,int,int,int>> Q;\n rep(i,0,N) {\n rep(j,0,M) {\n char C;\n cin >> C;\n if (C == '#') G[i][j] = 6, DP[i][j][0][0] = 0, Q.push({i,j,0,0});\n else G[i][j] = mp[C];\n }\n }\n vector<int> ord(6);\n rep(i,0,6) {\n char C;\n cin >> C;\n ord[i] = mp[C];\n }\n while(!Q.empty()) {\n auto [X,Y,ID,P] = Q.front();\n Q.pop();\n if (P == 6) break;\n Dice D = Dices[ID];\n rep(i,0,4) {\n int NX = X + dx[i], NY = Y + dy[i];\n Dice ND = D.rot(i);\n int NID = ND.getID();\n if (NX < 0 || N <= NX || NY < 0 || M <= NY) continue;\n if (G[NX][NY] < 0) continue;\n if (G[NX][NY] != 6 && G[NX][NY] != ND.top()) continue;\n if (G[NX][NY] != 6 && G[NX][NY] != ord[P]) continue;\n int NP;\n if (G[NX][NY] == ord[P]) NP = P+1;\n else NP = P;\n if (chmin(DP[NX][NY][NID][NP],DP[X][Y][ID][P]+1)) {\n Q.push({NX,NY,NID,NP});\n }\n }\n }\n int ANS = inf;\n rep(i,0,N) {\n rep(j,0,M) {\n rep(k,0,24) {\n chmin(ANS,DP[i][j][k][6]);\n }\n }\n }\n if (ANS == inf) cout << \"unreachable\" << endl;\n else cout << ANS << endl;\n}\n}", "accuracy": 1, "time_ms": 280, "memory_kb": 5128, "score_of_the_acc": -0.3318, "final_rank": 11 }, { "submission_id": "aoj_1290_9280360", "code_snippet": "#include <algorithm>\n#include <cmath>\n#include <cstdlib>\n#include <iomanip>\n#include <iostream>\n#include <list>\n#include <map>\n#include <numeric>\n#include <queue>\n#include <set>\n#include <stack>\n#include <string>\n#include <unordered_set>\n#include <vector>\n\n#define INF (1000000000)\n\nusing namespace std;\nusing ll = long long;\n\ntemplate <typename T>\nconstexpr bool chmin(T& min, const T target) {\n\tif (target < min) {\n\t\tmin = target;\n\t\treturn true;\n\t}\n\treturn false;\n}\n\n// face\n// 0: top\n// 1: bottom\n// 2: north\n// 3: south\n// 4: east\n// 5: west\n\n// direction\n// 0: north\n// 1: south\n// 2: east\n// 3: west\n\nclass Index {\n public:\n\tint phase{0}, i{0}, j{0};\n\tstring state{\"012345\"};\n};\n\nstring Roll(string state, const int direction) {\n\tconst int R[4][6]{\n\t\t{2, 3, 1, 0, 4, 5},\n\t\t{3, 2, 0, 1, 4, 5},\n\t\t{4, 5, 2, 3, 1, 0},\n\t\t{5, 4, 2, 3, 0, 1},\n\t};\n\n\tstring rolled{\"012345\"};\n\tfor (int f = 0; f < 6; f++) rolled[R[direction][f]] = state[f];\n\treturn rolled;\n}\n\nint main(void) {\n\tconst int directions[4][2]{{0, -1}, {0, 1}, {1, 0}, {-1, 0}};\n\n\twhile (true) {\n\t\tint width, depth;\n\t\tstring visits;\n\t\tcin >> width >> depth;\n\t\tif (width == 0) break;\n\t\tvector<vector<char>> bed(width, vector<char>(depth));\n\t\tIndex start{0, 0, 0, \"rcgmby\"};\n\t\tfor (int j = 0; j < depth; j++) {\n\t\t\tstring row;\n\t\t\tcin >> row;\n\t\t\tfor (int i = 0; i < width; i++) {\n\t\t\t\tbed[i][j] = row[i];\n\t\t\t\tif (bed[i][j] == '#') {\n\t\t\t\t\tstart.i = i;\n\t\t\t\t\tstart.j = j;\n\t\t\t\t\tbed[i][j] = 'w';\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\tcin >> visits;\n\n\t\tcout << flush;\n\n\t\tll min_distance_to_goal = INF;\n\t\tvector<vector<vector<map<string, ll>>>> distance(6, vector<vector<map<string, ll>>>(width, vector<map<string, ll>>(depth)));\n\t\tdistance[start.phase][start.i][start.j][start.state] = 0;\n\t\tqueue<Index> tobe_search;\n\t\ttobe_search.push(start);\n\t\twhile (!tobe_search.empty()) {\n\t\t\tIndex current = tobe_search.front();\n\t\t\ttobe_search.pop();\n\n\t\t\tfor (int d = 0; d < 4; d++) {\n\t\t\t\tIndex adjacent{\n\t\t\t\t\tcurrent.phase + (bed[current.i][current.j] == 'w' ? 0 : 1),\n\t\t\t\t\tcurrent.i + directions[d][0],\n\t\t\t\t\tcurrent.j + directions[d][1],\n\t\t\t\t\tRoll(current.state, d),\n\t\t\t\t};\n\n\t\t\t\tif (adjacent.phase == 6 ||\n\t\t\t\t\tadjacent.i < 0 || width <= adjacent.i || adjacent.j < 0 || depth <= adjacent.j ||\n\t\t\t\t\tbed[adjacent.i][adjacent.j] == 'k' ||\n\t\t\t\t\t(bed[adjacent.i][adjacent.j] != 'w' && bed[adjacent.i][adjacent.j] != visits[adjacent.phase]) ||\n\t\t\t\t\t(bed[adjacent.i][adjacent.j] != 'w' && bed[adjacent.i][adjacent.j] != adjacent.state[0]) ||\n\t\t\t\t\tdistance[adjacent.phase][adjacent.i][adjacent.j].find(adjacent.state) != distance[adjacent.phase][adjacent.i][adjacent.j].end())\n\t\t\t\t{\n\t\t\t\t\tcontinue;\n\t\t\t\t}\n\n\t\t\t\tdistance[adjacent.phase][adjacent.i][adjacent.j][adjacent.state] = distance[current.phase][current.i][current.j][current.state] + 1;\n\t\t\t\ttobe_search.push(adjacent);\n\t\t\t}\n\n\t\t\tif (bed[current.i][current.j] == visits[5]) chmin(min_distance_to_goal, distance[current.phase][current.i][current.j][current.state]);\n\t\t}\n\n\t\tif (min_distance_to_goal != INF) cout << min_distance_to_goal << endl;\n\t\telse cout << \"unreachable\" << endl;\n\t}\n\n\treturn 0;\n}", "accuracy": 1, "time_ms": 300, "memory_kb": 8764, "score_of_the_acc": -0.4108, "final_rank": 13 }, { "submission_id": "aoj_1290_9255388", "code_snippet": "#include <algorithm>\n#include <cmath>\n#include <cstdlib>\n#include <iomanip>\n#include <iostream>\n#include <list>\n#include <map>\n#include <numeric>\n#include <queue>\n#include <set>\n#include <stack>\n#include <string>\n#include <unordered_set>\n#include <vector>\n\n#define INF (1000000000)\n\nusing namespace std;\nusing ll = long long;\n\ntemplate <typename T>\nconstexpr bool chmin(T& min, const T target) {\n\tif (target < min) {\n\t\tmin = target;\n\t\treturn true;\n\t}\n\treturn false;\n}\n\n// face\n// 0: top\n// 1: bottom\n// 2: north\n// 3: south\n// 4: east\n// 5: west\n\n// direction\n// 0: north\n// 1: south\n// 2: east\n// 3: west\n\nclass Index {\n public:\n\tint phase{0}, i{0}, j{0};\n\tstring state{\"012345\"};\n};\n\nstring Roll(string state, const int direction) {\n\tconst int R[4][6]{\n\t\t{2, 3, 1, 0, 4, 5},\n\t\t{3, 2, 0, 1, 4, 5},\n\t\t{4, 5, 2, 3, 1, 0},\n\t\t{5, 4, 2, 3, 0, 1},\n\t};\n\n\tstring rolled{\"012345\"};\n\tfor (int f = 0; f < 6; f++) rolled[R[direction][f]] = state[f];\n\treturn rolled;\n}\n\nint main(void) {\n\tconst int directions[4][2]{{0, -1}, {0, 1}, {1, 0}, {-1, 0}};\n\n\twhile (true) {\n\t\tint width, depth;\n\t\tstring visits;\n\t\tcin >> width >> depth;\n\t\tif (width == 0) break;\n\t\tvector<vector<char>> bed(width, vector<char>(depth));\n\t\tIndex start{0, 0, 0, \"rcgmby\"};\n\t\tfor (int j = 0; j < depth; j++) {\n\t\t\tstring row;\n\t\t\tcin >> row;\n\t\t\tfor (int i = 0; i < width; i++) {\n\t\t\t\tbed[i][j] = row[i];\n\t\t\t\tif (bed[i][j] == '#') {\n\t\t\t\t\tstart.i = i;\n\t\t\t\t\tstart.j = j;\n\t\t\t\t\tbed[i][j] = 'w';\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\tcin >> visits;\n\n\t\tcout << flush;\n\n\t\tll min_distance_to_goal = INF;\n\t\tvector<vector<vector<map<string, ll>>>> distance(6, vector<vector<map<string, ll>>>(width, vector<map<string, ll>>(depth)));\n\t\tdistance[start.phase][start.i][start.j][start.state] = 0;\n\t\tqueue<Index> tobe_search;\n\t\ttobe_search.push(start);\n\t\twhile (!tobe_search.empty()) {\n\t\t\tIndex current = tobe_search.front();\n\t\t\ttobe_search.pop();\n\n\t\t\tfor (int d = 0; d < 4; d++) {\n\t\t\t\tIndex adjacent{\n\t\t\t\t\tcurrent.phase + (bed[current.i][current.j] == 'w' ? 0 : 1),\n\t\t\t\t\tcurrent.i + directions[d][0],\n\t\t\t\t\tcurrent.j + directions[d][1],\n\t\t\t\t\tRoll(current.state, d),\n\t\t\t\t};\n\n\t\t\t\tif (adjacent.phase == 6) continue;\n\n\t\t\t\tif (adjacent.i < 0 || width <= adjacent.i || adjacent.j < 0 || depth <= adjacent.j ||\n\t\t\t\t\tbed[adjacent.i][adjacent.j] == 'k' ||\n\t\t\t\t\t(bed[adjacent.i][adjacent.j] != 'w' && bed[adjacent.i][adjacent.j] != visits[adjacent.phase])) continue;\n\n\t\t\t\tif (bed[adjacent.i][adjacent.j] != 'w' && adjacent.state[0] != bed[adjacent.i][adjacent.j]) continue;\n\n\t\t\t\tif (distance[adjacent.phase][adjacent.i][adjacent.j].find(adjacent.state) != distance[adjacent.phase][adjacent.i][adjacent.j].end()) continue;\n\n\t\t\t\tdistance[adjacent.phase][adjacent.i][adjacent.j][adjacent.state] = distance[current.phase][current.i][current.j][current.state] + 1;\n\t\t\t\ttobe_search.push(adjacent);\n\t\t\t}\n\n\t\t\tif (bed[current.i][current.j] == visits[5]) chmin(min_distance_to_goal, distance[current.phase][current.i][current.j][current.state]);\n\n\t\t\t// cout << current.phase << endl;\n\t\t}\n\n\t\tif (min_distance_to_goal != INF) cout << min_distance_to_goal << endl;\n\t\telse cout << \"unreachable\" << endl;\n\t}\n\n\treturn 0;\n}", "accuracy": 1, "time_ms": 290, "memory_kb": 8780, "score_of_the_acc": -0.3993, "final_rank": 12 }, { "submission_id": "aoj_1290_6399050", "code_snippet": "#include \"bits/stdc++.h\"\nusing namespace std;\n\ntypedef string::const_iterator State;\n#define eps 1e-8L\n#define MAX_MOD 1000000007LL\n#define GYAKU 500000004LL\n#define MOD 998244353LL\n#define pb push_back\n#define mp make_pair\ntypedef long long ll;\ntypedef long double ld;\n#define REP(a, b) for (long long(a) = 0; (a) < (b); ++(a))\n#define ALL(x) (x).begin(), (x).end()\n\n#define int long long\nint encoding(int step, pair<int, int> x, string s)\n{\n int encoder = 0;\n REP(i, s.length())\n {\n encoder *= 26;\n encoder += s[i] - 'a';\n }\n return step + 901 * (x.first * 31 + x.second) + 100000000 * encoder;\n}\n\nstring R(string s)\n{\n string b = s;\n b[0] = s[5];\n b[1] = s[4];\n b[2] = s[2];\n b[3] = s[3];\n b[4] = s[0];\n b[5] = s[1];\n return b;\n}\nstring D(string s)\n{\n string b = s;\n b[0] = s[2];\n b[1] = s[3];\n b[2] = s[1];\n b[3] = s[0];\n b[4] = s[4];\n b[5] = s[5];\n return b;\n}\n\nvoid solve()\n{\n int r, c;\n cin >> r >> c;\n if (r == 0)\n exit(0);\n vector<string> inputs;\n REP(i, c)\n {\n string s;\n cin >> s;\n inputs.push_back(s);\n }\n string target;\n cin >> target;\n queue<tuple<pair<int, int>, pair<int, int>, string>> nexts;\n set<int> already;\n REP(i, c)\n {\n REP(q, r)\n {\n if (inputs[i][q] == '#')\n {\n nexts.push({{0, 0}, {i, q}, \"rcgmby\"});\n }\n }\n }\n while (!nexts.empty())\n {\n tuple<pair<int, int>, pair<int, int>, string> now = nexts.front();\n nexts.pop();\n pair<int, int> place = get<1>(now);\n if (inputs[place.first][place.second] != 'w' and inputs[place.first][place.second] != '#')\n {\n if (inputs[place.first][place.second] != target[get<0>(now).second])\n continue;\n if (get<2>(now)[0] != target[get<0>(now).second])\n continue;\n get<0>(now).second++;\n if (get<0>(now).second == 6)\n {\n cout << get<0>(now).first << endl;\n return;\n }\n }\n get<0>(now).first++;\n const int dx[4] = {1, -1, 0, 0};\n REP(i, 4)\n {\n int x = place.first + dx[i];\n int y = place.second + dx[3 - i];\n if (!(x >= 0 and x < c and y >= 0 and y < r))\n {\n continue;\n }\n if (inputs[x][y] == 'k')\n continue;\n string moves = get<2>(now);\n if (i == 0)\n {\n moves = D(moves);\n }\n else if (i == 1)\n {\n moves = D(D(D(moves)));\n }\n else if (i == 2)\n {\n moves = R(R(R(moves)));\n }\n else\n {\n moves = R(moves);\n }\n int target = encoding(get<0>(now).second, {x, y}, moves);\n if (already.count(target))\n continue;\n already.insert(target);\n nexts.push({get<0>(now), {x, y}, moves});\n }\n }\n cout << \"unreachable\" << endl;\n}\n\n#undef int\n\n// generated by oj-template v4.7.2\n// (https://github.com/online-judge-tools/template-generator)\nint main()\n{\n // Fasterize input/output script\n ios::sync_with_stdio(false);\n cin.tie(nullptr);\n cout << fixed << setprecision(10);\n // scanf/printf user should delete this fasterize input/output script\n\n int t = 10000;\n // cin >> t; // comment out if solving multi testcase\n for (int testCase = 1; testCase <= t; ++testCase)\n {\n solve();\n }\n return 0;\n}", "accuracy": 1, "time_ms": 450, "memory_kb": 6508, "score_of_the_acc": -0.5528, "final_rank": 14 }, { "submission_id": "aoj_1290_6315333", "code_snippet": "#include<bits/stdc++.h>\n\n#pragma GCC optimize(\"O3\")\n#pragma GCC optimize(\"unroll-loops\")\n\nusing namespace std;\n#define INFS (1LL<<28)\n#define DEKAI 1000000007\n//#define INF 1000000007\n//#define MOD 1000000007\n#define lp(i,n) for(int i=0;i<(long long)n;i++)\n#define lps(i,n) for(int i=1;i<=n;i++)\n#define all(c) begin(c), end(c)\n\n//#define int long long\n\nnamespace {\n#define __DECLARE__(C) \\\n\t template <typename T> \\\n\tstd::ostream &operator<<(std::ostream &, const C<T> &);\n\n#define __DECLAREM__(C) \\\n\t template <typename T, typename U> \\\n\tstd::ostream &operator<<(std::ostream &, const C<T, U> &);\n\n __DECLARE__(std::vector)\n __DECLARE__(std::deque)\n __DECLARE__(std::set)\n __DECLARE__(std::stack)\n __DECLARE__(std::queue)\n __DECLARE__(std::priority_queue)\n __DECLARE__(std::unordered_set)\n __DECLAREM__(std::map)\n __DECLAREM__(std::unordered_map)\n\n template <typename T, typename U>\n std::ostream &operator<<(std::ostream &, const std::pair<T, U> &);\n template <typename... T>\n std::ostream &operator<<(std::ostream &, const std::tuple<T...> &);\n template <typename T, std::size_t N>\n std::ostream &operator<<(std::ostream &, const std::array<T, N> &);\n\n template <typename Tuple, std::size_t N>\n struct __TuplePrinter__ {\n static void print(std::ostream &os, const Tuple &t) {\n __TuplePrinter__<Tuple, N - 1>::print(os, t);\n os << \", \" << std::get<N - 1>(t);\n }\n };\n\n template <typename Tuple>\n struct __TuplePrinter__<Tuple, 1> {\n static void print(std::ostream &os, const Tuple &t) { os << std::get<0>(t); }\n };\n\n template <typename... T>\n std::ostream &operator<<(std::ostream &os, const std::tuple<T...> &t) {\n os << '(';\n __TuplePrinter__<decltype(t), sizeof...(T)>::print(os, t);\n os << ')';\n return os;\n }\n\n template <typename T, typename U>\n std::ostream &operator<<(std::ostream &os, const std::pair<T, U> &v) {\n return os << '(' << v.first << \", \" << v.second << ')';\n }\n\n#define __INNER__ \\\n\tos << '['; \\\n\tfor (auto it = begin(c); it != end(c);) { \\\n\t\tos << *it; \\\n\t\tos << (++it != end(c) ? \", \" : \"\"); \\\n\t} \\\n\treturn os << ']';\n\n template <typename T, std::size_t N>\n std::ostream &operator<<(std::ostream &os, const std::array<T, N> &c) {\n __INNER__\n }\n\n#define __DEFINE__(C) \\\n\t template <typename T> \\\n\tstd::ostream &operator<<(std::ostream &os, const C<T> &c) { \\\n\t\t__INNER__ \\\n\t}\n\n#define __DEFINEM__(C) \\\n\t template <typename T, typename U> \\\n\tstd::ostream &operator<<(std::ostream &os, const C<T, U> &c) { \\\n\t\t__INNER__ \\\n\t}\n\n#define __DEFINEW__(C, M1, M2) \\\n\t template <typename T> \\\n\tstd::ostream &operator<<(std::ostream &os, const C<T> &c) { \\\n\t\tstd::deque<T> v; \\\n\t\tfor (auto d = c; !d.empty(); d.pop()) v.M1(d.M2()); \\\n\t\t\treturn os << v; \\\n\t}\n\n __DEFINE__(std::vector)\n __DEFINE__(std::deque)\n __DEFINE__(std::set)\n __DEFINEW__(std::stack, push_front, top)\n __DEFINEW__(std::queue, push_back, front)\n __DEFINEW__(std::priority_queue, push_front, top)\n __DEFINE__(std::unordered_set)\n __DEFINEM__(std::map)\n __DEFINEM__(std::unordered_map)\n}\n\n#define pii pair<int,int>\n#define ll long long\ninline ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }\ninline ll lcm(ll a, ll b) { return a / gcd(a, b) * b; }\n\n// modint\ntemplate <signed M, unsigned T>\nstruct mod_int {\n constexpr static signed MODULO = M;\n constexpr static unsigned TABLE_SIZE = T;\n\n signed x;\n\n mod_int() : x(0) {}\n\n mod_int(long long y) : x(static_cast<signed>(y >= 0 ? y % MODULO : MODULO - (-y) % MODULO)) {}\n\n mod_int(int y) : x(y >= 0 ? y % MODULO : MODULO - (-y) % MODULO) {}\n\n mod_int &operator+=(const mod_int &rhs) {\n if ((x += rhs.x) >= MODULO) x -= MODULO;\n return *this;\n }\n\n mod_int &operator-=(const mod_int &rhs) {\n if ((x += MODULO - rhs.x) >= MODULO) x -= MODULO;\n return *this;\n }\n\n mod_int &operator*=(const mod_int &rhs) {\n x = static_cast<signed>(1LL * x * rhs.x % MODULO);\n return *this;\n }\n\n mod_int &operator/=(const mod_int &rhs) {\n x = static_cast<signed>((1LL * x * rhs.inv().x) % MODULO);\n return *this;\n }\n\n mod_int operator-() const { return mod_int(-x); }\n\n mod_int operator+(const mod_int &rhs) const { return mod_int(*this) += rhs; }\n\n mod_int operator-(const mod_int &rhs) const { return mod_int(*this) -= rhs; }\n\n mod_int operator*(const mod_int &rhs) const { return mod_int(*this) *= rhs; }\n\n mod_int operator/(const mod_int &rhs) const { return mod_int(*this) /= rhs; }\n\n bool operator<(const mod_int &rhs) const { return x < rhs.x; }\n\n mod_int inv() const {\n assert(x != 0);\n if (x <= static_cast<signed>(TABLE_SIZE)) {\n if (_inv[1].x == 0) prepare();\n return _inv[x];\n } else {\n signed a = x, b = MODULO, u = 1, v = 0, t;\n while (b) {\n t = a / b;\n a -= t * b;\n std::swap(a, b);\n u -= t * v;\n std::swap(u, v);\n }\n return mod_int(u);\n }\n }\n\n mod_int pow(long long t) const {\n assert(!(x == 0 && t == 0));\n mod_int e = *this, res = mod_int(1);\n for (; t; e *= e, t >>= 1)\n if (t & 1) res *= e;\n return res;\n }\n\n mod_int fact() {\n if (_fact[0].x == 0) prepare();\n return _fact[x];\n }\n\n mod_int inv_fact() {\n if (_fact[0].x == 0) prepare();\n return _inv_fact[x];\n }\n\n mod_int choose(mod_int y) {\n assert(y.x <= x);\n return this->fact() * y.inv_fact() * mod_int(x - y.x).inv_fact();\n }\n\n static mod_int _inv[TABLE_SIZE + 1];\n\n static mod_int _fact[TABLE_SIZE + 1];\n\n static mod_int _inv_fact[TABLE_SIZE + 1];\n\n static void prepare() {\n _inv[1] = 1;\n for (int i = 2; i <= (int)TABLE_SIZE; ++i) {\n _inv[i] = 1LL * _inv[MODULO % i].x * (MODULO - MODULO / i) % MODULO;\n }\n _fact[0] = 1;\n for (unsigned i = 1; i <= TABLE_SIZE; ++i) {\n _fact[i] = _fact[i - 1] * int(i);\n }\n _inv_fact[TABLE_SIZE] = _fact[TABLE_SIZE].inv();\n for (int i = (int)TABLE_SIZE - 1; i >= 0; --i) {\n _inv_fact[i] = _inv_fact[i + 1] * (i + 1);\n }\n }\n};\n\ntemplate <int M, unsigned F>\nstd::ostream &operator<<(std::ostream &os, const mod_int<M, F> &rhs) {\n return os << rhs.x;\n}\n\ntemplate <int M, unsigned F>\nstd::istream &operator>>(std::istream &is, mod_int<M, F> &rhs) {\n long long s;\n is >> s;\n rhs = mod_int<M, F>(s);\n return is;\n}\n\ntemplate <int M, unsigned F>\nmod_int<M, F> mod_int<M, F>::_inv[TABLE_SIZE + 1];\n\ntemplate <int M, unsigned F>\nmod_int<M, F> mod_int<M, F>::_fact[TABLE_SIZE + 1];\n\ntemplate <int M, unsigned F>\nmod_int<M, F> mod_int<M, F>::_inv_fact[TABLE_SIZE + 1];\n\ntemplate <int M, unsigned F>\nbool operator==(const mod_int<M, F> &lhs, const mod_int<M, F> &rhs) {\n return lhs.x == rhs.x;\n}\n\ntemplate <int M, unsigned F>\nbool operator!=(const mod_int<M, F> &lhs, const mod_int<M, F> &rhs) {\n return !(lhs == rhs);\n}\n\nconst int MF = 1000010;\nconst int MOD = 998244353;\n\nusing mint = mod_int<MOD, MF>;\n\nmint binom(int n, int r) { return (r < 0 || r > n || n < 0) ? 0 : mint(n).choose(r); }\n\nmint fact(int n) { return mint(n).fact(); }\n\nmint inv_fact(int n) { return mint(n).inv_fact(); }\nconst ll mod = 1000000007;\n// const int MAX_N = 1024; // 4MB\n// nCr % mod\n\ninline ll gcds(ll a, ll b) { return b ? gcds(b, a % b) : a; }\ninline ll lcms(ll a, ll b) { return a / gcd(a, b) * b; }\n\ntemplate <typename Int, Int MOD, int N>\nstruct comb_util {\n std::array<Int, N + 1> fc, ifc;\n\n comb_util() {\n fc[0] = 1;\n for (int i = 1; i <= N; i++) fc[i] = fc[i - 1] * i % MOD;\n ifc[N] = inv(fc[N]);\n for (int i = N - 1; i >= 0; i--) ifc[i] = ifc[i + 1] * (i + 1) % MOD;\n }\n\n Int fact(Int n) { return fc[n]; }\n\n Int inv_fact(Int n) { return ifc[n]; }\n\n Int inv(Int n) { return pow(n, MOD - 2); }\n\n Int pow(Int n, Int a) {\n Int res = 1, exp = n;\n for (; a; a /= 2) {\n if (a & 1) res = res * exp % MOD;\n exp = exp * exp % MOD;\n }\n return res;\n }\n\n Int perm(Int n, Int r) {\n if (r < 0 || n < r)\n return 0;\n else\n return fc[n] * ifc[n - r] % MOD;\n }\n\n Int binom(Int n, Int r) {\n if (n < 0 || r < 0 || n < r) return 0;\n return fc[n] * ifc[r] % MOD * ifc[n - r] % MOD;\n }\n\n Int homo(Int n, Int r) {\n if (n < 0 || r < 0) return 0;\n return r == 0 ? 1 : binom(n + r - 1, r);\n }\n};\n\nusing comb = comb_util<long long, 1000000007, 3000000>;\n\n#define RK 200000000000\n#define LK 300000000000\n#define PL 400000000000\n#define MI 500000000000\n#define KA 600000000000\n#define PI acos(-1)\n#define int long long\n\nenum { U, B, L, F, R, D};\nstruct dice {\n int face[6];\n dice() {\n face[F] = 1;\n face[R] = 2;\n face[U] = 3;\n face[B] = 6;\n face[L] = 5;\n face[D] = 4;\n }\n int find_face(int f){\n lp(i,6){\n if(face[i]==f)return i;\n }\n return -1;\n }\n int get_hash(){\n int res = 0;\n lp(i,6){\n res*=7;\n res+=face[i];\n }\n return res;\n }\n void turn(int dir){\n switch(dir){\n case R:\n rotate(U,R,D,L);\n break;\n case B:\n rotate(U,B,D,F);\n break;\n case L:\n rotate(U,L,D,R);\n break;\n case F:\n rotate(U,F,D,B);\n break;\n case U:\n rotate(F,R,B,L);\n case D:\n rotate(F,L,B,R);\n default:\n assert(false);\n }\n }\n\n int& operator[](int n) {\n return face[n];\n }\n const int& operator[](int n) const {\n return face[n];\n }\n\n vector<dice> all_rolls() {\n vector<dice> res;\n lp(k,6){\n lp(i,4){\n res.push_back(*this);\n turn(R);\n }\n turn(k%2==1?U:F);\n }\n return res;\n }\n\n void rotate(int a,int b,int c, int d){\n int t = face[d];\n face[d]=face[c];\n face[c]=face[b];\n face[b]=face[a];\n face[a]=t;\n }\n};\n\n\nsigned main(){\n map<char,int> face;\n face['r']=3;\n face['c']=4;\n face['g']=1;\n face['m']=6;\n face['b']=2;\n face['y']=5;\n\n while(1){\n int w,d;\n cin>>w>>d;\n if(w==0)break;\n vector<string> s(d);\n lp(i,d)cin>>s[i];\n int sx,sy;\n lp(i,d){\n lp(j,w){\n if(s[i][j]=='#'){\n sx=i;\n sy=j;\n s[i][j]='w';\n }\n }\n }\n string t;\n cin>>t;\n queue<pair<pair<int,int>,pair<dice,pair<int,int>>>> q;\n set<pair<pair<int,int>,pair<int,int>>> st;\n dice def = dice();\n //x y dice cnt target\n q.push({{sx,sy},{def,{0,0}}});\n st.insert({{sx,sy},{def.get_hash(),0}});\n\n int dir[4] = {B,F,R,L};\n int dx[4] = {1, -1, 0, 0};\n int dy[4] = {0, 0, 1, -1};\n\n int ans = -1;\n while(!q.empty()&&ans==-1){\n int x = q.front().first.first;\n int y = q.front().first.second;\n dice di = q.front().second.first;\n int cnt = q.front().second.second.first;\n int tit = q.front().second.second.second;\n //cout<<x<<\" \"<<y<<\" \"<<cnt<<\" \"<<tit<<\" \"<<di.find_face(3)<<endl;\n //cout<<di[0]<<di[1]<<di[2]<<di[3]<<di[4]<<di[5]<<endl;\n q.pop();\n lp(i,4){\n int nx = dx[i]+x;\n int ny = dy[i]+y;\n if(nx<0||nx>=d||ny<0||ny>=w)continue;\n dice nd = di;\n nd.turn(dir[i]);\n\n if(s[nx][ny]=='k')continue;\n if(st.find({{nx,ny},{nd.get_hash(),tit}})!=st.end()){\n continue;\n }\n\n if(s[nx][ny]!='w'){\n if(s[nx][ny]!=t[tit])continue;\n if(nd.find_face(face[s[nx][ny]])!=U)continue;\n if(tit+1==6){\n ans=cnt+1;\n break;\n }\n q.push({{nx,ny},{nd,{cnt+1,tit+1}}});\n st.insert({{nx,ny},{nd.get_hash(),tit+1}});\n }\n else{\n q.push({{nx,ny},{nd,{cnt+1,tit}}});\n st.insert({{nx,ny},{nd.get_hash(),tit}});\n }\n }\n\n }\n if(ans==-1){\n cout<<\"unreachable\"<<endl;\n }\n else cout<<ans<<endl;\n }\n return 0;\n}", "accuracy": 1, "time_ms": 360, "memory_kb": 20184, "score_of_the_acc": -0.6556, "final_rank": 17 }, { "submission_id": "aoj_1290_5978296", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\nstruct Dice {\n std::array<int, 6> surface;\n\n Dice(int TOP = 1, int FRONT = 2) {\n assert(1 <= TOP and TOP <= 6);\n assert(1 <= FRONT and FRONT <= 6);\n assert(TOP + FRONT != 7);\n surface[0] = TOP;\n surface[1] = FRONT;\n surface[2] = dice[TOP - 1][FRONT - 1];\n surface[3] = 7 - surface[2];\n surface[4] = 7 - surface[1];\n surface[5] = 7 - surface[0];\n }\n\n Dice(const std::vector<int>& v) {\n assert(v.size() == 6);\n for (size_t i = 0; i < 6; i++) surface[i] = v[i];\n }\n\n const int& top() const { return surface[0]; }\n const int& front() const { return surface[1]; }\n const int& right() const { return surface[2]; }\n const int& left() const { return surface[3]; }\n const int& back() const { return surface[4]; }\n const int& bottom() const { return surface[5]; }\n const int& operator[](int k) const { return surface[k]; }\n\n int& top() { return surface[0]; }\n int& front() { return surface[1]; }\n int& right() { return surface[2]; }\n int& left() { return surface[3]; }\n int& back() { return surface[4]; }\n int& bottom() { return surface[5]; }\n int& operator[](int k) { return surface[k]; }\n\n bool operator==(const Dice& d) const { return surface == d.surface; }\n bool operator!=(const Dice& d) const { return surface != d.surface; }\n bool operator<(const Dice& d) const { return surface < d.surface; }\n\n int roll(int k) { // x++, x--, y++, y--, turn right, turn left\n assert(0 <= k and k < 6);\n int tmp = surface[code[k][0]];\n for (int i = 0; i < 3; i++) surface[code[k][i]] = surface[code[k][i + 1]];\n surface[code[k][3]] = tmp;\n return surface[0];\n }\n\n int rollc(char c) {\n for (int k = 0; k < 6; k++) {\n if (direction[k] != c) continue;\n return roll(k);\n }\n assert(false);\n }\n\n int hash() const {\n int res = 0;\n for (size_t i = 0; i < 6; i++) {\n assert(1 <= surface[i] and surface[i] <= 6);\n (res *= 6) += surface[i] - 1;\n }\n return res;\n }\n\n std::vector<Dice> make_all() {\n std::vector<Dice> res;\n for (int i = 0; i < 6; i++) {\n Dice d(*this);\n if (i == 1) d.roll(2);\n if (i == 2) d.roll(3);\n if (i == 3) d.roll(3), d.roll(3);\n if (i == 4) d.roll(5);\n if (i == 5) d.roll(4);\n for (int j = 0; j < 4; j++) {\n res.emplace_back(d);\n d.roll(0);\n }\n }\n return res;\n }\n\n Dice identifier() {\n auto dices = make_all();\n return *min_element(dices.begin(), dices.end());\n }\n\nprivate:\n const int dice[6][6] = {{0, 3, 5, 2, 4, 0}, {4, 0, 1, 6, 0, 3}, {2, 6, 0, 0, 1, 5},\n {5, 1, 0, 0, 6, 2}, {3, 0, 6, 1, 0, 4}, {0, 4, 2, 5, 3, 0}};\n const int code[6][4] = {{0, 3, 5, 2}, {0, 2, 5, 3}, {0, 1, 5, 4}, {0, 4, 5, 1}, {1, 2, 4, 3}, {1, 3, 4, 2}};\n const char direction[6] = {'E', 'W', 'N', 'S', 'R', 'L'};\n};\n\n/**\n * @brief Dice\n * @docs docs/util/Dice.md\n */\n\nconst int INF = 1e9;\nconst int MAX_H = 32, MAX_S = 24, MAX_C = 6;\nconst int dx[4] = {0, 0, -1, 1}, dy[4] = {1, -1, 0, 0};\nint dp[MAX_H][MAX_H][MAX_S][MAX_C + 1];\n\nint ctoi(char c) {\n if (c == 'r') return 1;\n if (c == 'c') return 6;\n if (c == 'g') return 5;\n if (c == 'm') return 2;\n if (c == 'b') return 3;\n if (c == 'y') return 4;\n if (c == 'k') return 7;\n return 8;\n}\n\nvoid solve(int w, int d) {\n vector<string> S(d);\n for (auto& s : S) cin >> s;\n string T;\n cin >> T;\n\n int sx, sy, gx, gy;\n for (int i = 0; i < d; i++) {\n for (int j = 0; j < w; j++) {\n if (S[i][j] == '#') sx = i, sy = j;\n if (S[i][j] == T.back()) gx = i, gy = j;\n for (int k = 0; k < MAX_S; k++) {\n for (int l = 0; l <= MAX_C; l++) {\n dp[i][j][k][l] = INF;\n }\n }\n }\n }\n\n Dice dice;\n auto all = dice.make_all();\n map<int, int> mp;\n for (size_t i = 0; i < all.size(); i++) mp[all[i].hash()] = i;\n queue<tuple<int, int, int, int, int>> que;\n dp[sx][sy][0][0] = 0;\n que.emplace(-dp[sx][sy][0][0], sx, sy, 0, 0);\n auto update = [&](int val, int x, int y, int s, int c) {\n if (dp[x][y][s][c] <= val) return;\n dp[x][y][s][c] = val;\n que.emplace(-val, x, y, s, c);\n };\n\n while (!que.empty()) {\n auto t = que.front();\n que.pop();\n int val, x, y, s, c;\n tie(val, x, y, s, c) = t;\n val *= -1;\n if (c == MAX_C) continue;\n auto cur = all[s];\n for (int i = 0; i < 4; i++) {\n int nx = x + dx[i], ny = y + dy[i];\n if (nx < 0 or d <= nx or ny < 0 or w <= ny) continue;\n if (S[nx][ny] == 'k') continue;\n cur.roll(i);\n int to = ctoi(S[nx][ny]);\n if (to <= 6) {\n if (to == ctoi(T[c]) and cur.top() == to) {\n update(val + 1, nx, ny, mp[cur.hash()], c + 1);\n }\n } else\n update(val + 1, nx, ny, mp[cur.hash()], c);\n cur.roll(i ^ 1);\n }\n }\n\n int ans = INF;\n for (int k = 0; k < MAX_S; k++) ans = min(ans, dp[gx][gy][k][MAX_C]);\n if (ans == INF)\n cout << \"unreachable\" << '\\n';\n else\n cout << ans << '\\n';\n}\n\nint main() {\n cin.tie(0);\n ios::sync_with_stdio(false);\n for (int w, d; cin >> w >> d, w;) solve(w, d);\n return 0;\n}", "accuracy": 1, "time_ms": 100, "memory_kb": 3820, "score_of_the_acc": -0.1, "final_rank": 4 }, { "submission_id": "aoj_1290_5978289", "code_snippet": "#define LOCAL\n#include <bits/stdc++.h>\nusing namespace std;\n#pragma region Macros\ntypedef long long ll;\ntypedef __int128_t i128;\ntypedef unsigned int uint;\ntypedef unsigned long long ull;\n#define ALL(x) (x).begin(), (x).end()\n\ntemplate <typename T> istream& operator>>(istream& is, vector<T>& v) {\n for (T& x : v) is >> x;\n return is;\n}\ntemplate <typename T> ostream& operator<<(ostream& os, const vector<T>& v) {\n for (int i = 0; i < (int)v.size(); i++) {\n os << v[i] << (i + 1 == (int)v.size() ? \"\" : \" \");\n }\n return os;\n}\ntemplate <typename T, typename U> ostream& operator<<(ostream& os, const pair<T, U>& p) {\n os << '(' << p.first << ',' << p.second << ')';\n return os;\n}\ntemplate <typename T, typename U> ostream& operator<<(ostream& os, const map<T, U>& m) {\n os << '{';\n for (auto itr = m.begin(); itr != m.end();) {\n os << '(' << itr->first << ',' << itr->second << ')';\n if (++itr != m.end()) os << ',';\n }\n os << '}';\n return os;\n}\ntemplate <typename T, typename U> ostream& operator<<(ostream& os, const unordered_map<T, U>& m) {\n os << '{';\n for (auto itr = m.begin(); itr != m.end();) {\n os << '(' << itr->first << ',' << itr->second << ')';\n if (++itr != m.end()) os << ',';\n }\n os << '}';\n return os;\n}\ntemplate <typename T> ostream& operator<<(ostream& os, const set<T>& s) {\n os << '{';\n for (auto itr = s.begin(); itr != s.end();) {\n os << *itr;\n if (++itr != s.end()) os << ',';\n }\n os << '}';\n return os;\n}\ntemplate <typename T> ostream& operator<<(ostream& os, const multiset<T>& s) {\n os << '{';\n for (auto itr = s.begin(); itr != s.end();) {\n os << *itr;\n if (++itr != s.end()) os << ',';\n }\n os << '}';\n return os;\n}\ntemplate <typename T> ostream& operator<<(ostream& os, const unordered_set<T>& s) {\n os << '{';\n for (auto itr = s.begin(); itr != s.end();) {\n os << *itr;\n if (++itr != s.end()) os << ',';\n }\n os << '}';\n return os;\n}\ntemplate <typename T> ostream& operator<<(ostream& os, const deque<T>& v) {\n for (int i = 0; i < (int)v.size(); i++) {\n os << v[i] << (i + 1 == (int)v.size() ? \"\" : \" \");\n }\n return os;\n}\n\ntemplate <int i, typename T> void print_tuple(ostream&, const T&) {}\ntemplate <int i, typename T, typename H, class... Args> void print_tuple(ostream& os, const T& t) {\n if (i) os << ',';\n os << get<i>(t);\n print_tuple<i + 1, T, Args...>(os, t);\n}\ntemplate <typename... Args> ostream& operator<<(ostream& os, const tuple<Args...>& t) {\n os << '{';\n print_tuple<0, tuple<Args...>, Args...>(os, t);\n return os << '}';\n}\n\nvoid debug_out() { cerr << '\\n'; }\ntemplate <class Head, class... Tail> void debug_out(Head&& head, Tail&&... tail) {\n cerr << head;\n if (sizeof...(Tail) > 0) cerr << \", \";\n debug_out(move(tail)...);\n}\n#ifdef LOCAL\n#define debug(...) \\\n cerr << \" \"; \\\n cerr << #__VA_ARGS__ << \" :[\" << __LINE__ << \":\" << __FUNCTION__ << \"]\" << '\\n'; \\\n cerr << \" \"; \\\n debug_out(__VA_ARGS__)\n#else\n#define debug(...) 42\n#endif\n\ntemplate <typename T> T gcd(T x, T y) { return y != 0 ? gcd(y, x % y) : x; }\ntemplate <typename T> T lcm(T x, T y) { return x / gcd(x, y) * y; }\n\nint topbit(signed t) { return t == 0 ? -1 : 31 - __builtin_clz(t); }\nint topbit(long long t) { return t == 0 ? -1 : 63 - __builtin_clzll(t); }\nint botbit(signed a) { return a == 0 ? 32 : __builtin_ctz(a); }\nint botbit(long long a) { return a == 0 ? 64 : __builtin_ctzll(a); }\nint popcount(signed t) { return __builtin_popcount(t); }\nint popcount(long long t) { return __builtin_popcountll(t); }\nbool ispow2(int i) { return i && (i & -i) == i; }\n\ntemplate <class T> T ceil(T x, T y) {\n assert(y >= 1);\n return (x > 0 ? (x + y - 1) / y : x / y);\n}\ntemplate <class T> T floor(T x, T y) {\n assert(y >= 1);\n return (x > 0 ? x / y : (x - y + 1) / y);\n}\n\ntemplate <class T1, class T2> inline bool chmin(T1& a, T2 b) {\n if (a > b) {\n a = b;\n return true;\n }\n return false;\n}\ntemplate <class T1, class T2> inline bool chmax(T1& a, T2 b) {\n if (a < b) {\n a = b;\n return true;\n }\n return false;\n}\n#pragma endregion\n\n#include <array>\n#include <cassert>\n#include <string>\n#include <vector>\n\nstruct Dice {\n std::array<int, 6> surface;\n\n Dice(int TOP = 1, int FRONT = 2) {\n assert(1 <= TOP and TOP <= 6);\n assert(1 <= FRONT and FRONT <= 6);\n assert(TOP + FRONT != 7);\n surface[0] = TOP;\n surface[1] = FRONT;\n surface[2] = dice[TOP - 1][FRONT - 1];\n surface[3] = 7 - surface[2];\n surface[4] = 7 - surface[1];\n surface[5] = 7 - surface[0];\n }\n\n Dice(const std::vector<int>& v) {\n assert(v.size() == 6);\n for (size_t i = 0; i < 6; i++) surface[i] = v[i];\n }\n\n const int& top() const { return surface[0]; }\n const int& front() const { return surface[1]; }\n const int& right() const { return surface[2]; }\n const int& left() const { return surface[3]; }\n const int& back() const { return surface[4]; }\n const int& bottom() const { return surface[5]; }\n const int& operator[](int k) const { return surface[k]; }\n\n int& top() { return surface[0]; }\n int& front() { return surface[1]; }\n int& right() { return surface[2]; }\n int& left() { return surface[3]; }\n int& back() { return surface[4]; }\n int& bottom() { return surface[5]; }\n int& operator[](int k) { return surface[k]; }\n\n bool operator==(const Dice& d) const { return surface == d.surface; }\n bool operator!=(const Dice& d) const { return surface != d.surface; }\n bool operator<(const Dice& d) const { return surface < d.surface; }\n\n int roll(int k) { // x++, x--, y++, y--, turn right, turn left\n assert(0 <= k and k < 6);\n int tmp = surface[code[k][0]];\n for (int i = 0; i < 3; i++) surface[code[k][i]] = surface[code[k][i + 1]];\n surface[code[k][3]] = tmp;\n return surface[0];\n }\n\n int rollc(char c) {\n for (int k = 0; k < 6; k++) {\n if (direction[k] != c) continue;\n return roll(k);\n }\n assert(false);\n }\n\n int hash() const {\n int res = 0;\n for (size_t i = 0; i < 6; i++) {\n assert(1 <= surface[i] and surface[i] <= 6);\n (res *= 6) += surface[i] - 1;\n }\n return res;\n }\n\n std::vector<Dice> make_all() {\n std::vector<Dice> res;\n for (int i = 0; i < 6; i++) {\n Dice d(*this);\n if (i == 1) d.roll(2);\n if (i == 2) d.roll(3);\n if (i == 3) d.roll(3), d.roll(3);\n if (i == 4) d.roll(5);\n if (i == 5) d.roll(4);\n for (int j = 0; j < 4; j++) {\n res.emplace_back(d);\n d.roll(0);\n }\n }\n return res;\n }\n\n Dice identifier() {\n auto dices = make_all();\n return *min_element(dices.begin(), dices.end());\n }\n\nprivate:\n const int dice[6][6] = {{0, 3, 5, 2, 4, 0}, {4, 0, 1, 6, 0, 3}, {2, 6, 0, 0, 1, 5},\n {5, 1, 0, 0, 6, 2}, {3, 0, 6, 1, 0, 4}, {0, 4, 2, 5, 3, 0}};\n const int code[6][4] = {{0, 3, 5, 2}, {0, 2, 5, 3}, {0, 1, 5, 4}, {0, 4, 5, 1}, {1, 2, 4, 3}, {1, 3, 4, 2}};\n const char direction[6] = {'E', 'W', 'N', 'S', 'R', 'L'};\n};\n\n/**\n * @brief Dice\n * @docs docs/util/Dice.md\n */\n\nconst int INF = 1e9;\nconst long long IINF = 1e18;\nconst char dir[4] = {'D', 'R', 'U', 'L'};\nconst long long MOD = 1000000007;\n// const long long MOD = 998244353;\n\nconst int MAX_H = 32, MAX_S = 24, MAX_C = 6;\nconst int dx[4] = {0, 0, -1, 1}, dy[4] = {1, -1, 0, 0};\nint dp[MAX_H][MAX_H][MAX_S][MAX_C + 1];\n\nint ctoi(char c) {\n if (c == 'r') return 1;\n if (c == 'c') return 6;\n if (c == 'g') return 5;\n if (c == 'm') return 2;\n if (c == 'b') return 3;\n if (c == 'y') return 4;\n if (c == 'k') return 7;\n return 8;\n}\n\nvoid solve(int w, int d) {\n vector<string> S(d);\n for (auto& s : S) cin >> s;\n string T;\n cin >> T;\n\n int sx, sy, gx, gy;\n for (int i = 0; i < d; i++) {\n for (int j = 0; j < w; j++) {\n if (S[i][j] == '#') sx = i, sy = j;\n if (S[i][j] == T.back()) gx = i, gy = j;\n for (int k = 0; k < MAX_S; k++) {\n for (int l = 0; l <= MAX_C; l++) {\n dp[i][j][k][l] = INF;\n }\n }\n }\n }\n\n Dice dice;\n auto all = dice.make_all();\n map<int, int> mp;\n for (size_t i = 0; i < all.size(); i++) mp[all[i].hash()] = i;\n queue<tuple<int, int, int, int, int>> que;\n dp[sx][sy][0][0] = 0;\n que.emplace(-dp[sx][sy][0][0], sx, sy, 0, 0);\n auto update = [&](int val, int x, int y, int s, int c) {\n if (dp[x][y][s][c] <= val) return;\n dp[x][y][s][c] = val;\n que.emplace(-val, x, y, s, c);\n };\n\n while (!que.empty()) {\n auto t = que.front();\n que.pop();\n int val, x, y, s, c;\n tie(val, x, y, s, c) = t;\n val *= -1;\n if (c == MAX_C) continue;\n auto cur = all[s];\n for (int i = 0; i < 4; i++) {\n int nx = x + dx[i], ny = y + dy[i];\n if (nx < 0 or d <= nx or ny < 0 or w <= ny) continue;\n if (S[nx][ny] == 'k') continue;\n cur.roll(i);\n int to = ctoi(S[nx][ny]);\n if (to <= 6) {\n if (to == ctoi(T[c]) and cur.top() == to) {\n update(val + 1, nx, ny, mp[cur.hash()], c + 1);\n }\n } else\n update(val + 1, nx, ny, mp[cur.hash()], c);\n cur.roll(i ^ 1);\n }\n }\n\n int ans = INF;\n for (int k = 0; k < MAX_S; k++) ans = min(ans, dp[gx][gy][k][MAX_C]);\n if (ans == INF)\n cout << \"unreachable\" << '\\n';\n else\n cout << ans << '\\n';\n}\n\nint main() {\n cin.tie(0);\n ios::sync_with_stdio(false);\n for (int w, d; cin >> w >> d, w;) solve(w, d);\n return 0;\n}", "accuracy": 1, "time_ms": 100, "memory_kb": 4056, "score_of_the_acc": -0.1036, "final_rank": 5 }, { "submission_id": "aoj_1290_5973796", "code_snippet": "#pragma GCC optimize(\"Ofast\")\n#include <iostream>\n#include <vector>\n#include <algorithm>\n#include <map>\n#include <queue>\n#include <cstdio>\n#include <ctime>\n#include <assert.h>\n#include <chrono>\n#include <random>\n#include <numeric>\n#include <set>\n#include <deque>\n#include <stack>\n#include <sstream>\n#include <utility>\n#include <cstring>\n#include <unordered_map>\n#include <unordered_set>\n#include <tuple>\n#include <array>\nusing namespace std;\ntypedef long long int ll;\ntypedef unsigned long long ull;\n\nmt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());\nll myRand(ll B) {\n return (ull)rng() % B;\n}\ninline double time() {\n return static_cast<double>(chrono::duration_cast<chrono::nanoseconds>(chrono::steady_clock::now().time_since_epoch()).count()) * 1e-9;\n}\n\nstruct state{\n int x,y;\n int idx;\n array<char, 6> dice;\n state(int _x, int _y, int _idx, array<char, 6> _dice){\n x = _x;\n y = _y;\n idx = _idx;\n dice = _dice;\n }\n};\nbool operator< (state const &a, state const &b){\n if(a.x != b.x){\n return a.x < b.x;\n }\n else if(a.y != b.y){\n return a.y < b.y;\n }\n else if(a.idx != b.idx){\n return a.idx < b.idx;\n }\n else{\n return a.dice < b.dice;\n }\n}\n\nint dx[]={-1,1,0,0};\nint dy[]={0,0,1,-1};\n\nint main(){\n cin.tie(nullptr);\n ios::sync_with_stdio(false); \n int w,h;\n array<char, 6> init_dice;\n init_dice = {'r','m','b','g','y','c'};\n while(cin >> w >> h,w){\n vector<string> s(h);\n for(int i=0;i<h;i++){\n cin >> s[i];\n }\n string per; cin >> per;\n queue<state> q;\n map<state,int> dp;\n for(int i=0;i<h;i++){\n for(int j=0;j<w;j++){\n if(s[i][j] == '#'){\n s[i][j] = 'w';\n q.push(state(i,j,0,init_dice));\n dp[state(i,j,0,init_dice)] = 0;\n }\n }\n }\n int res = -1;\n while(q.size()){\n auto p = q.front(); q.pop();\n int cost = dp[p];\n if(p.idx == 6){\n res = cost;\n break;\n }\n int x = p.x, y = p.y;\n auto dice = p.dice;\n for(int k=0;k<4;k++){\n int nx = x+dx[k];\n int ny = y+dy[k];\n auto nx_dice = dice;\n if(k == 0){\n nx_dice[3] = dice[0];\n nx_dice[0] = dice[1];\n nx_dice[1] = dice[5];\n nx_dice[5] = dice[3];\n }\n else if(k == 1){\n nx_dice[0] = dice[3];\n nx_dice[1] = dice[0];\n nx_dice[5] = dice[1];\n nx_dice[3] = dice[5];\n }\n else if(k == 2){\n nx_dice[2] = dice[0];\n nx_dice[5] = dice[2];\n nx_dice[4] = dice[5];\n nx_dice[0] = dice[4];\n }\n else{\n nx_dice[2] = dice[5];\n nx_dice[5] = dice[4];\n nx_dice[4] = dice[0];\n nx_dice[0] = dice[2];\n }\n if(0<=nx and nx<h and 0<=ny and ny<w){\n auto nx_state = state(nx,ny,p.idx,nx_dice);\n if(s[nx][ny] == 'w'){\n if(dp.find(nx_state) == dp.end()){\n dp[nx_state] = cost + 1;\n q.push(nx_state);\n }\n }\n else if(per[p.idx] == s[nx][ny]){\n if(per[p.idx] == nx_dice[0]){\n nx_state.idx++;\n if(dp.find(nx_state) == dp.end()){\n dp[nx_state] = cost + 1;\n q.push(nx_state);\n }\n }\n }\n }\n }\n }\n if(res == -1) cout << \"unreachable\" << \"\\n\";\n else cout << res << \"\\n\";\n }\n}", "accuracy": 1, "time_ms": 490, "memory_kb": 7196, "score_of_the_acc": -0.6104, "final_rank": 15 }, { "submission_id": "aoj_1290_5035101", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\nconst int N=30;\nconst int D=4;\nconst int S=24;\nconst int O=6;\n\nconst int dx[D]={-1,0,0,1};\nconst int dy[D]={0,-1,1,0};\n\nstring roll(string c, int d) {\n char t;\n switch(d) {\n case 0: // {1,0,4,3}\n t=c[1]; c[1]=c[0]; c[0]=c[4]; c[4]=c[3]; c[3]=t; break;\n case 1: // {5,0,2,3}\n t=c[5]; c[5]=c[0]; c[0]=c[2]; c[2]=c[3]; c[3]=t; break;\n case 2: // {3,2,0,5}\n t=c[3]; c[3]=c[2]; c[2]=c[0]; c[0]=c[5]; c[5]=t; break;\n case 3: // {3,4,0,1}\n t=c[3]; c[3]=c[4]; c[4]=c[0]; c[0]=c[1]; c[1]=t; break;\n }\n return c;\n}\n\n\nvector<string> cube;\nint w,d,s[N][N][S][1+O];\nstring bed[N],order;\n\nint cubeid(string s) {\n int i=0;\n while (i<cube.size() && cube[i]!=s) i++;\n return i;\n}\n\nvoid init_cubes() {\n cube.push_back(\"rgbcmy\");\n for (int i=0; i<cube.size(); i++) {\n for (int d=0; d<D; d++) {\n string s=roll(cube[i],d);\n int j=cubeid(s);\n if (j>=cube.size()) cube.push_back(s);\n }\n }\n}\n\nvoid loc(char c, int &x, int &y) {\n for (x=0; x<d; x++)\n for (y=0; y<w; y++)\n if (bed[x][y]==c) return;\n}\n\nstruct State { int x,y,cid,ord; };\n\nint solve() {\n cin>>w>>d;\n if (w==0 && d==0) return 0;\n for (int i=0; i<d; i++) cin>>bed[i];\n cin>>order;\n\n // BFS: s[x][y][cid][ord] means the min step to reach (x,y), at that time,\n // its faces are the same as cube[cid] and have visited ord colors.\n int x,y;\n queue<State> Q;\n order.insert(0,\"#\");\n memset(s,-1,sizeof(s));\n loc('#',x,y);\n s[x][y][0][0]=0;\n Q.push({x,y,0,0});\n while (!Q.empty()) {\n auto u=Q.front();\n Q.pop();\n for (int j=0; j<D; j++) {\n string cb=roll(cube[u.cid],j);\n State v = {u.x+dx[j], u.y+dy[j], cubeid(cb), u.ord};\n int su=s[u.x][u.y][u.cid][u.ord];\n\n if (0<=v.x && v.x<d && 0<=v.y && v.y<w) {\n if (bed[v.x][v.y]=='w' || bed[v.x][v.y]=='#') {\n // pass\n } else if (cb[0]==bed[v.x][v.y] && cb[0]==order[v.ord+1]) {\n v.ord+=1;\n } else {\n continue;\n }\n if (s[v.x][v.y][v.cid][v.ord]==-1) {\n s[v.x][v.y][v.cid][v.ord]=su+1;\n Q.push(v);\n }\n }\n }\n }\n\n loc(order[O],x,y);\n int k=-1;\n for (int i=0; i<S; i++) {\n int j=s[x][y][i][O];\n if (j!=-1 && (k==-1 || k>j)) k=j;\n }\n if (k==-1) {\n cout<<\"unreachable\\n\";\n } else {\n cout<<k<<'\\n';\n }\n return 1;\n}\n\nint main() {\n init_cubes();\n while (solve());\n return 0;\n}", "accuracy": 1, "time_ms": 270, "memory_kb": 3824, "score_of_the_acc": -0.3001, "final_rank": 8 }, { "submission_id": "aoj_1290_5034421", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\nusing ll = long long;\nusing PII = pair<ll, ll>;\n\n#define FOR(i, a, n) for (ll i = (ll)a; i < (ll)n; ++i)\n#define REP(i, n) FOR(i, 0, n)\n#define ALL(x) x.begin(), x.end()\n#define POPCOUNT(x) __builtin_popcount(x)\ntemplate <typename T> void chmin(T &a, const T &b) { a = min(a, b); }\ntemplate <typename T> void chmax(T &a, const T &b) { a = max(a, b); }\n\nconst ll INF = 1LL << 60;\n\nstruct FastIO {\n FastIO() {\n cin.tie(0);\n ios::sync_with_stdio(0);\n }\n} fastiofastio;\n\nvector<int> rotate_N(const vector<int> &dice) {\n vector<int> new_dice = dice;\n new_dice[0] = dice[2];\n new_dice[2] = dice[5];\n new_dice[5] = dice[4];\n new_dice[4] = dice[0];\n return new_dice;\n}\n\nvector<int> rotate_S(const vector<int> &dice) {\n vector<int> new_dice = dice;\n new_dice[2] = dice[0];\n new_dice[5] = dice[2];\n new_dice[4] = dice[5];\n new_dice[0] = dice[4];\n return new_dice;\n}\n\nvector<int> rotate_E(const vector<int> &dice) {\n vector<int> new_dice = dice;\n new_dice[3] = dice[0];\n new_dice[0] = dice[1];\n new_dice[1] = dice[5];\n new_dice[5] = dice[3];\n return new_dice;\n}\n\nvector<int> rotate_W(const vector<int> &dice) {\n vector<int> new_dice = dice;\n new_dice[3] = dice[5];\n new_dice[5] = dice[1];\n new_dice[1] = dice[0];\n new_dice[0] = dice[3];\n return new_dice;\n}\n\nusing T = tuple<int, int, int, int, int>;\n\nchar maze[30][31];\nchar v[7];\nint dist[30][30][6][6][7];\nint dx[] = {1, 0, -1, 0};\nint dy[] = {0, 1, 0, -1};\n// top front\nint r[6][6];\nint main() {\n r[0][1] = 2;\n r[0][2] = 4;\n r[0][3] = 1;\n r[0][4] = 3;\n\n r[1][0] = 3;\n r[1][2] = 0;\n r[1][3] = 5;\n r[1][5] = 2;\n\n r[2][0] = 1;\n r[2][1] = 5;\n r[2][4] = 0;\n r[2][5] = 4;\n\n r[3][0] = 4;\n r[3][1] = 0;\n r[3][4] = 5;\n r[3][5] = 1;\n\n r[4][0] = 2;\n r[4][2] = 5;\n r[4][3] = 0;\n r[4][5] = 3;\n\n r[5][1] = 3;\n r[5][2] = 1;\n r[5][3] = 4;\n r[5][4] = 2;\n\n while (1) {\n int w, d;\n cin >> w >> d;\n if (w == 0)\n break;\n for (int i = 0; i < d; i++)\n cin >> maze[i];\n cin >> v;\n memset(dist, -1, sizeof(dist));\n queue<T> que;\n for (int i = 0; i < d; i++) {\n for (int j = 0; j < w; j++) {\n if (maze[i][j] == '#') {\n que.emplace(j, i, 0, 2, 0);\n dist[j][i][0][2][0] = 0;\n maze[i][j] = 'w';\n }\n }\n }\n int ans = -1;\n while (!que.empty()) {\n T now = que.front();\n que.pop();\n int x = get<0>(now);\n int y = get<1>(now);\n int top = get<2>(now);\n int front = get<3>(now);\n int k = get<4>(now);\n if (k == 6) {\n ans = dist[x][y][top][front][k];\n break;\n }\n vector<int> dice(6);\n dice[0] = top;\n dice[2] = front;\n dice[3] = r[top][front];\n dice[5] = 5 - dice[0];\n dice[4] = 5 - dice[2];\n dice[1] = 5 - dice[3];\n for (int i = 0; i < 4; i++) {\n int nx = x + dx[i];\n int ny = y + dy[i];\n if (0 > nx || nx >= w || 0 > ny || ny >= d ||\n maze[ny][nx] == 'k')\n continue;\n if (maze[ny][nx] != 'w' && maze[ny][nx] != v[k])\n continue;\n vector<int> new_dice;\n if (i == 0)\n new_dice = rotate_E(dice);\n else if (i == 1)\n new_dice = rotate_S(dice);\n else if (i == 2)\n new_dice = rotate_W(dice);\n else\n new_dice = rotate_N(dice);\n int color = -1;\n if (maze[ny][nx] == 'r')\n color = 0;\n else if (maze[ny][nx] == 'y')\n color = 1;\n else if (maze[ny][nx] == 'm')\n color = 2;\n else if (maze[ny][nx] == 'g')\n color = 3;\n else if (maze[ny][nx] == 'b')\n color = 4;\n else if (maze[ny][nx] == 'c')\n color = 5;\n if (color != -1 && new_dice[0] != color)\n continue;\n int nk = k + (color != -1);\n if (dist[nx][ny][new_dice[0]][new_dice[2]][nk] == -1) {\n dist[nx][ny][new_dice[0]][new_dice[2]][nk] =\n dist[x][y][top][front][k] + 1;\n que.emplace(nx, ny, new_dice[0], new_dice[2], nk);\n }\n }\n }\n if (ans == -1)\n cout << \"unreachable\" << endl;\n else\n cout << ans << endl;\n }\n}", "accuracy": 1, "time_ms": 70, "memory_kb": 4256, "score_of_the_acc": -0.0714, "final_rank": 3 }, { "submission_id": "aoj_1290_5014478", "code_snippet": "#include<cstdio>\n#include<map>\n#include<queue>\nusing namespace std;\nint dx[4] = {1, 0, -1, 0};\nint dy[4] = {0, 1, 0, -1};\n\nint w, d;\nchar c[30][31], v[7];\nchar convert[8] = \"rcgmby\";\n\nstruct Dice {\n int d[6]; // (top, bottom, up, down, right, left)\n // 0:red, 1:cyan, 2:green, 3:magenta, 4:blue, 5:yellow\n Dice(int d1 = 0, int d2 = 1, int d3 = 2, int d4 = 3, int d5 = 4, int d6 = 5) {\n d[0] = d1;\n d[1] = d2;\n d[2] = d3;\n d[3] = d4;\n d[4] = d5;\n d[5] = d6;\n }\n int get() {\n int result = 0;\n for (int i = 0; i < 6; i++) result = result * 6 + d[i];\n return result;\n }\n Dice up() {\n return Dice(d[3], d[2], d[0], d[1], d[4], d[5]);\n }\n Dice down() {\n return Dice(d[2], d[3], d[1], d[0], d[4], d[5]);\n }\n Dice right() {\n return Dice(d[5], d[4], d[2], d[3], d[0], d[1]);\n }\n Dice left() {\n return Dice(d[4], d[5], d[2], d[3], d[1], d[0]);\n }\n Dice move(int num) {\n if (num == 0) return down();\n if (num == 1) return right();\n if (num == 2) return up();\n return left();\n }\n};\n\nusing P = pair<int, int>;\nusing Q = pair<int, P>;\nusing R = pair<Q, Dice>;\n\nvoid solve() {\n map<int, int> mp[6][30][30];\n queue<R> que;\n for (int i = 0; i < d; i++) for (int j = 0; j < w; j++) if (c[i][j] == '#') {\n R p = R(Q(0, P(i, j)), Dice());\n que.push(p);\n mp[0][i][j][p.second.get()] = 0;\n }\n while (!que.empty()) {\n R r = que.front(); que.pop();\n int x = r.first.second.first, y = r.first.second.second, num = r.first.first;\n Dice dice = r.second;\n for (int i = 0; i < 4; i++) {\n int num2 = num;\n int x2 = x + dx[i], y2 = y + dy[i];\n if (x2 < 0 || y2 < 0 || x2 == d || y2 == w) continue;\n Dice dice2 = dice.move(i);\n if (c[x2][y2] != 'w' && c[x2][y2] != '#') {\n if (v[num2] == c[x2][y2] && convert[dice2.d[0]] == c[x2][y2]) {\n num2++;\n } else {\n continue;\n }\n }\n if (mp[num][x2][y2].empty() || mp[num2][x2][y2].count(dice2.get()) == 0) {\n if (num2 == 6) {\n printf(\"%d\\n\", mp[num][x][y][dice.get()] + 1);\n return;\n }\n mp[num2][x2][y2][dice2.get()] = mp[num][x][y][dice.get()] + 1;\n que.push(R(Q(num2, P(x2, y2)), dice2));\n }\n }\n }\n printf(\"unreachable\\n\");\n}\n\nint main() {\n while (scanf(\"%d%d\", &w, &d), w) {\n for (int i = 0; i < d; i++) scanf(\"%s\", c[i]);\n scanf(\"%s\", v);\n solve();\n }\n}", "accuracy": 1, "time_ms": 170, "memory_kb": 6048, "score_of_the_acc": -0.2164, "final_rank": 6 }, { "submission_id": "aoj_1290_4951772", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n#define reps(i, s, n) for(ll i = (s); i < n; ++i)\n#define rep(i, n) reps(i, 0, n)\n#define ALL(v) v.begin(), v.end()\n\nusing ll = long long;\nusing vec = vector<ll>;\nusing mat = vector<vec>;\n\nint W, H;\n\nconst int base = 6;\nstruct Dice{\n int ii, jj, nxtord;\n int l, r, u, d, f, b;\n vector<int*> val;\n\n void init(){\n val[0]=&l; val[1]=&r;\n val[2]=&u; val[3]=&d;\n val[4]=&f; val[5]=&b;\n }\n\n Dice(vector<int> v) : val(6), ii(-1), jj(-1), nxtord(0){\n init();\n rep(i, 6) {\n *val[i] = v[i];\n }\n }\n\n Dice(const Dice &dice) : val(6), ii(dice.ii), jj(dice.jj), nxtord(dice.nxtord){\n init();\n rep(i, 6) {\n *val[i] = *(dice.val[i]);\n }\n }\n\n bool operator<(const Dice &dice) const{\n rep(i, 6){\n if(*val[i] < *(dice.val[i])) return true;\n if(*val[i] > *(dice.val[i])) return false;\n }\n return false;\n }\n\n void RollN(){\n int temp = u;\n u = f; f = d; d = b; b = temp;\n }\n\n void RollE(){\n int temp = u;\n u = l; l = d; d = r; r = temp;\n }\n\n void RollS(){RollN(); RollN(); RollN(); }\n void RollW(){RollE(); RollE(); RollE(); }\n //void RollL(){RollN(); RollE(); RollS();}\n //void RollR(){RollS(); RollE(); RollN();}\n\n bool roll(int i){\n if(i == 0){ --ii; RollN();}\n else if(i == 1) {++jj; RollE();}\n else if(i == 2) {++ii; RollN(); RollN(); RollN(); }\n else if(i == 3) {--jj; RollE(); RollE(); RollE(); }\n\n if(ii>=0 && ii<H && jj>=0 && jj<W) return true;\n return false;\n }\n\n void print(){\n cout<<ii<<' '<<jj<<' '<<nxtord<<endl;\n cout<<l<<' '<<r<<' '<<u<<' '<<d<<' '<<f<<' '<<b<<endl;\n rep(i, 6) cout<<*val[i]<<\" \\n\"[i == 5];\n }\n\n};\n\ntemplate<class T, class U> bool chmin(T &a, const U &b){\n if(a > b) {a = b; return true;}\n else return false;\n}\n\nvoid solve(){\n string S;\n vector<string> G(H);\n map<char, int> ord;\n map<Dice, int> dist[30][30][7];\n using P = pair<int, Dice>;\n queue<P> que;\n\n rep(i, H) cin>>G[i];\n cin>>S;\n rep(i, 6) ord[S[i]] = i;\n Dice dice({ord['y'], ord['b'], ord['r'], ord['c'], ord['m'], ord['g']});\n mat g(H, vec(W, 10));\n rep(i, H) rep(j, W){\n char c = G[i][j];\n if(c=='#'){\n dice.ii = i; dice.jj = j;\n }else if(c == 'k'){\n g[i][j] = -1;\n }else if(c != 'w'){\n g[i][j] = ord[G[i][j]];\n }\n }\n\n dist[dice.ii][dice.jj][0][dice] = -(1<<30);\n que.emplace(-(1<<30), dice);\n while(!que.empty()){\n P p = que.front(); que.pop();\n Dice now = p.second;\n if(dist[now.ii][now.jj][now.nxtord][now] < p.first) continue;\n if(now.nxtord == 6) {\n cout<<p.first + (1<<30)<<endl;\n return;\n }\n rep(k, 4){\n Dice nxt(now);\n if(nxt.roll(k)){\n if(g[nxt.ii][nxt.jj] == 10 && chmin(dist[nxt.ii][nxt.jj][nxt.nxtord][nxt], p.first + 1)){\n que.emplace(p.first + 1, nxt);\n }else if(g[nxt.ii][nxt.jj] == nxt.nxtord && nxt.u == nxt.nxtord){\n ++nxt.nxtord;\n if(chmin(dist[nxt.ii][nxt.jj][nxt.nxtord][nxt], p.first + 1)) que.emplace(p.first + 1, nxt);\n }\n }\n }\n }\n\n cout<<\"unreachable\"<<endl;\n}\n\nint main(){\n while(cin>>W>>H, W){\n solve();\n }\n}", "accuracy": 1, "time_ms": 400, "memory_kb": 14820, "score_of_the_acc": -0.6208, "final_rank": 16 }, { "submission_id": "aoj_1290_4949254", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n#define reps(i, s, n) for(ll i = (s); i < n; ++i)\n#define rep(i, n) reps(i, 0, n)\n#define ALL(v) v.begin(), v.end()\n\n#define fi first\n#define se second\n\nusing ll = long long;\nusing vec = vector<ll>;\nusing mat = vector<vec>;\n\nconst int base = 6;\nstruct Dice{\n int l, r, u, d, f, b;\n vector<int*> val;\n static map<int, int> hash_to_id;\n static vector<int> id_to_hash;\n Dice(vector<int> v) : val(6){\n val[0]=&l; val[1]=&r;\n val[2]=&u; val[3]=&d;\n val[4]=&f; val[5]=&b;\n rep(i, 6) *val[i] = v[i];\n }\n\n Dice(int id) : val(6){\n val[0]=&l; val[1]=&r;\n val[2]=&u; val[3]=&d;\n val[4]=&f; val[5]=&b;\n int hash = id_to_hash[id];\n rep(i, 6) {\n *val[5 - i] = hash % base;\n hash /= base;\n }\n }\n\n static void set_hash(vector<int*> v){\n hash_to_id.clear();\n id_to_hash.clear();\n vector<int> ord(6);\n iota(ALL(ord), 0);\n int id(0);\n do{\n int hash(0);\n rep(i, 6) (hash *= base) += *v[ord[i]];\n id_to_hash.push_back(hash);\n hash_to_id[hash] = id++;\n }while(next_permutation(ALL(ord)));\n }\n\n void RollN(){\n int temp = u;\n u = f; f = d; d = b; b = temp;\n }\n\n void RollE(){\n int temp = u;\n u = l; l = d; d = r; r = temp;\n }\n\n void RollS(){RollN(); RollN(); RollN(); }\n void RollW(){RollE(); RollE(); RollE(); }\n //void RollL(){RollN(); RollE(); RollS();}\n //void RollR(){RollS(); RollE(); RollN();}\n\n void roll(int i, int j){\n if(i == 0){\n if(j == 1) RollE();\n else RollW();\n }else{\n if(i == 1) RollS();\n else RollN();\n }\n }\n\n int id(){\n int hash(0);\n rep(i, 6) (hash *= base) += *val[i];\n if(id_to_hash[hash_to_id[hash]] != hash){\n cout<<hash<<' '<<hash_to_id[hash]<<' '<<id_to_hash[hash_to_id[hash]]<<endl;\n exit(0);\n }\n return hash_to_id[hash];\n }\n\n void print(){\n //cout<<\"Dice state\\n\";\n cout<<l<<' '<<r<<' '<<u<<' '<<d<<' '<<f<<' '<<b<<endl;\n /*rep(i, 6) cout<<*val[i]<<\" \\n\"[i == 5];\n cout<<id()<<endl;*/\n }\n\n};\n\nmap<int, int> Dice::hash_to_id;\nvector<int> Dice::id_to_hash;\n\nint W, H;\nint encode(int i, int j, int id){\n return (id * W + j) * H + i;\n}\n\nvector<int> decode(int code){\n vector<int> res(3);\n res[0] = code % H; code /= H;\n res[1] = code % W; code /= W;\n res[2] = code;\n return res;\n}\n\ntemplate<class T, class U> bool chmin(T &a, const U &b){\n if(a > b) {a = b; return true;}\n else return false;\n}\n\nint main(){\n while(cin>>W>>H, W){\n string S;\n vector<string> G(H);\n rep(i, H) cin>>G[i];\n cin>>S;\n map<char, int> ord;\n rep(i, 6) ord[S[i]] = i;\n Dice dice({ord['y'], ord['b'], ord['r'], ord['c'], ord['m'], ord['g']});\n Dice::set_hash(dice.val);\n\n mat g(H, vec(W, 10)), dist(H * W * Dice::id_to_hash.size(), vec(7, 1LL<<60));\n int si, sj;\n rep(i, H) rep(j, W){\n if(G[i][j]=='k') {\n g[i][j] = -1;\n }else if(G[i][j] == '#'){\n si = i; sj = j;\n }else if(G[i][j] != 'w'){\n g[i][j] = ord[G[i][j]];\n }\n }\n\n using P = pair<ll, ll>;\n priority_queue<P, vector<P>, greater<> > pque;\n dist[encode(si, sj, dice.id())][0] = 0;\n pque.emplace(0, encode(si, sj, dice.id()) * 7 + 0);\n vector<int> di = {-1, 0, 1, 0}, dj = {0, -1, 0, 1};\n ll res(1LL<<30);\n\n vec check(6, 1LL<<30);\n\n while(!pque.empty()){\n P p = pque.top(); pque.pop();\n vector<int> v = decode(p.se / 7);\n int i = v[0], j = v[1];\n //cout<<v[0]<<' '<<v[1]<<' '<<p.se%7<<' '<<v[2]<<endl;\n if(dist[p.se/7][p.se%7] < p.fi) continue;\n p.se %= 7;\n if(p.se == 6) {chmin(res, p.fi); continue;}\n/*\n if(g[i][j]==p.se-1) chmin(check[p.se], p.fi);\n Dice temp(v[2]);\n if(p.se == 1) {\n cout<<p.fi<<' '<<v[0]<<' '<<v[1]<<endl; \n temp.print();\n Dice test(temp.id());\n test.print();\n }\n*/\n rep(k, 4){\n int ni = i + di[k], nj = j + dj[k];\n if(ni>=0 && ni < H && nj >= 0 && nj < W && g[ni][nj] >= 0){\n Dice nxt_dice(v[2]);\n nxt_dice.roll(di[k], dj[k]);\n if(g[ni][nj] == 10 && chmin(dist[encode(ni, nj, nxt_dice.id())][p.se], p.fi + 1)){\n pque.emplace(p.fi + 1, encode(ni, nj, nxt_dice.id()) * 7 + p.se);\n }else if(g[ni][nj] == p.se && nxt_dice.u == p.se && chmin(dist[encode(ni, nj, nxt_dice.id())][p.se + 1], p.fi + 1)){\n pque.emplace(p.fi + 1, encode(ni, nj, nxt_dice.id()) * 7 + p.se + 1);\n }\n }\n }\n }\n //rep(i, 6) cout<<check[i]<<\" \\n\"[i==5];\n if(res == (1LL<<30)) cout<<\"unreachable\"<<endl;\n else cout<<res<<endl;\n }\n}", "accuracy": 1, "time_ms": 870, "memory_kb": 68964, "score_of_the_acc": -2, "final_rank": 20 }, { "submission_id": "aoj_1290_4886814", "code_snippet": "#include \"iostream\"\n#include \"climits\"\n#include \"list\"\n#include \"queue\"\n#include \"stack\"\n#include \"set\"\n#include \"functional\"\n#include \"algorithm\"\n#include \"string\"\n#include \"map\"\n#include \"unordered_map\"\n#include \"unordered_set\"\n#include \"iomanip\"\n#include \"cmath\"\n#include \"random\"\n#include \"bitset\"\n#include \"cstdio\"\n#include \"numeric\"\n#include \"cassert\"\n#include \"ctime\"\n\nusing namespace std;\n\nconstexpr long long int MOD = 1000000007;\n//constexpr int MOD = 1000000007;\n//constexpr int MOD = 998244353;\n//constexpr long long int MOD = 998244353;\nconstexpr double EPS = 1e-9;\n\n//int N, M, K, T, H, W, L, R;\nlong long int N, M, K, T, H, W, L, R;\n\nstruct Node {\n\tint y, x, dir;\n\tNode(const int yy, const int xx, const int dd) {\n\t\ty = yy, x = xx, dir = dd;\n\t}\n};\n\nint main() {\n\tios::sync_with_stdio(false);\n\tcin.tie(0);\n\n\tstring vs = { \"rmbygc\" };\n\tvector<vector<int>>dice = { {0,1,2},{0,2,4},{0,4,3},{0,3,1},{1,2,0},{1,0,3},{1,3,5},{1,5,2},{2,0,1},{2,1,5},{2,5,4},{2,4,0},{3,5,1},{3,1,0},{3,0,4},{3,4,5},{4,2,5},{4,5,3},{4,3,0},{4,0,2},{5,4,2},{5,2,1},{5,1,3},{5,3,4} };\n\tmap<vector<int>, int>mp;\n\tint cnt = 0;\n\tfor (auto i : dice) {\n\t\tmp[i] = cnt++;\n\t}\n\tint dir[] = { 1,0,-1,0,1 };\n\twhile (cin >> W >> H, H) {\n\t\tvector<string>s(H);\n\t\tvector<vector<vector<int>>>dis(H, vector<vector<int>>(W, vector<int>(24, MOD)));\n\t\tint sy, sx;\n\t\tfor (auto &i : s)cin >> i;\n\t\tfor (int i = 0; i < H; i++) {\n\t\t\tfor (int j = 0; j < W; j++) {\n\t\t\t\tif (s[i][j] == '#') {\n\t\t\t\t\tsy = i, sx = j;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\tstring t;\n\t\tcin >> t;\n\t\tdis[sy][sx][0] = 0;\n\t\tqueue<Node>Q;\n\t\tQ.push(Node(sy, sx, 0));\n\t\tint ans = MOD;\n\t\tfor (auto i : t) {\n\t\t\twhile (!Q.empty()) {\n\t\t\t\tauto node = Q.front();\n\t\t\t\tQ.pop();\n\t\t\t\tfor (int j = 0; j < 4; j++) {\n\t\t\t\t\tint ny = node.y + dir[j];\n\t\t\t\t\tint nx = node.x + dir[j + 1];\n\t\t\t\t\tif (ny < 0 || nx < 0 || ny >= H || nx >= W)continue;\n\t\t\t\t\tif (s[ny][nx] != i && s[ny][nx] != 'w'&&s[ny][nx]!='#')continue;\n\t\t\t\t\tauto ndir = dice[node.dir];\n\t\t\t\t\tauto nxdir = ndir;\n\t\t\t\t\tif (j == 0) {\n\t\t\t\t\t\tndir[0] = 5 - nxdir[1];\n\t\t\t\t\t\tndir[1] = nxdir[0];\n\t\t\t\t\t}\n\t\t\t\t\tif (j == 1) {\n\t\t\t\t\t\tndir[0] = nxdir[2];\n\t\t\t\t\t\tndir[2] = 5 -nxdir[0];\n\t\t\t\t\t}\n\t\t\t\t\tif (j == 2) {\n\t\t\t\t\t\tndir[0] = nxdir[1];\n\t\t\t\t\t\tndir[1] = 5 - nxdir[0];\n\t\t\t\t\t}\n\t\t\t\t\tif (j == 3) {\n\t\t\t\t\t\tndir[0] = 5 - nxdir[2];\n\t\t\t\t\t\tndir[2] = nxdir[0];\n\t\t\t\t\t}\n\t\t\t\t\tif (s[ny][nx] != 'w'&&s[ny][nx]!='#'&&vs[ndir[0]] != s[ny][nx])continue;\n\t\t\t\t\tauto nndir = mp[ndir];\n\t\t\t\t\tif (dis[ny][nx][nndir] > dis[node.y][node.x][node.dir] + 1) {\n\t\t\t\t\t\tdis[ny][nx][nndir] = dis[node.y][node.x][node.dir] + 1;\n\t\t\t\t\t\tQ.push(Node(ny, nx, nndir));\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t\tint ny, nx;\n\t\t\tfor (int j = 0; j < H; j++) {\n\t\t\t\tfor (int k = 0; k < W; k++) {\n\t\t\t\t\tif (s[j][k] == i) {\n\t\t\t\t\t\tny = j, nx = k;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t\tauto ndis = dis;\n\t\t\tfor (auto &j : ndis)for (auto &k : j)for (auto &l : k)l = MOD;\n\t\t\tans = MOD;\n\t\t\tfor (int j = 0; j < 24; j++) {\n\t\t\t\tndis[ny][nx][j] = dis[ny][nx][j];\n\t\t\t\tans = min(ans, dis[ny][nx][j]);\n\t\t\t\tQ.push(Node(ny, nx, j));\n\t\t\t}\n\t\t//\tcout << ans << endl;\n\t\t\tdis = ndis;\n\t\t}\n\t\tif (ans == MOD) {\n\t\t\tcout << \"unreachable\" << endl;\n\t\t}\n\t\telse {\n\t\t\tcout << ans << endl;\n\t\t}\n\t}\n}", "accuracy": 1, "time_ms": 280, "memory_kb": 3432, "score_of_the_acc": -0.3059, "final_rank": 9 }, { "submission_id": "aoj_1290_4830362", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nvector<int> dy = {1, 0, -1, 0};\nvector<int> dx = {0, 1, 0, -1};\nvector<vector<int>> r = {{2, 3, 1, 0, 4, 5}, {5, 4, 2, 3, 0, 1}, {3, 2, 0, 1, 4, 5}, {4, 5, 2, 3, 1, 0}};\nint main(){\n while (1){\n int w, d;\n cin >> w >> d;\n if (w == 0 && d == 0){\n break;\n }\n vector<vector<char>> c(d + 2, vector<char>(w + 2, 'k'));\n for (int i = 1; i <= d; i++){\n for (int j = 1; j <= w; j++){\n cin >> c[i][j];\n }\n }\n string s;\n cin >> s;\n int sy, sx;\n for (int i = 1; i <= d; i++){\n for (int j = 1; j <= w; j++){\n if (c[i][j] == '#'){\n sy = i;\n sx = j;\n }\n }\n }\n c[sy][sx] = 'w';\n tuple<array<char, 6>, int, int, int> T = make_tuple(array<char, 6>{'r', 'c', 'g', 'm', 'b', 'y'}, sy, sx, 0);\n set<tuple<array<char, 6>, int, int, int>> st;\n queue<pair<int, tuple<array<char, 6>, int, int, int>>> Q;\n st.insert(T);\n Q.push(make_pair(0, T));\n int ans = -1;\n while (!Q.empty()){\n int t = Q.front().first;\n array<char, 6> f = get<0>(Q.front().second);\n int y = get<1>(Q.front().second);\n int x = get<2>(Q.front().second);\n int p = get<3>(Q.front().second);\n Q.pop();\n if (p == 6){\n ans = t;\n break;\n }\n for (int i = 0; i < 4; i++){\n int y2 = y + dy[i];\n int x2 = x + dx[i];\n if (c[y2][x2] == 'w' || c[y2][x2] == f[r[i][0]] && c[y2][x2] == s[p]){\n array<char, 6> f2;\n for (int j = 0; j < 6; j++){\n f2[j] = f[r[i][j]];\n }\n int p2 = p;\n if (c[y2][x2] == s[p]){\n p2++;\n }\n tuple<array<char, 6>, int, int, int> next = make_tuple(f2, y2, x2, p2);\n if (!st.count(next)){\n st.insert(next);\n Q.push(make_pair(t + 1, next));\n }\n }\n }\n }\n if (ans == -1){\n cout << \"unreachable\" << endl;\n } else {\n cout << ans << endl;\n }\n }\n}", "accuracy": 1, "time_ms": 830, "memory_kb": 7040, "score_of_the_acc": -1.008, "final_rank": 19 }, { "submission_id": "aoj_1290_4789564", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\ntypedef long long ll;\ntemplate<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return true; } return false; }\ntemplate<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return true; } return false; }\n#define all(x) (x).begin(),(x).end()\n#define fi first\n#define se second\n#define mp make_pair\n#define si(x) int(x.size())\nconst int mod=1000000007,MAX=35,INF=1<<30;\n\nint ri(int u,int f){\n if(u==1){\n if(f==2) return 3;\n if(f==3) return 5;\n if(f==5) return 4;\n if(f==4) return 2;\n }\n if(u==2){\n if(f==1) return 4;\n if(f==4) return 6;\n if(f==6) return 3;\n if(f==3) return 1;\n }\n if(u==3){\n if(f==1) return 2;\n if(f==2) return 6;\n if(f==6) return 5;\n if(f==5) return 1;\n }\n if(u==4){\n if(f==1) return 5;\n if(f==5) return 6;\n if(f==6) return 2;\n if(f==2) return 1;\n }\n if(u==5){\n if(f==1) return 3;\n if(f==3) return 6;\n if(f==6) return 4;\n if(f==4) return 1;\n }\n if(u==6){\n if(f==2) return 4;\n if(f==4) return 5;\n if(f==5) return 3;\n if(f==3) return 2;\n }\n \n return -1000;\n}//uとfが与えられた時にrを返す(u==1,f==2,r==3型のサイコロ)\n\nstruct dice{\n int u,f,r,b,l,d;\n \n void init(int uu,int ff,int rr,int bb,int ll,int dd){\n u=uu;\n f=ff;\n r=rr;\n b=bb;\n l=ll;\n d=dd;\n }\n \n void init(int uu,int ff){\n u=uu;\n f=ff;\n r=ri(u,f);\n b=7-f;\n l=7-r;\n d=7-u;\n }\n \n void turnF(){\n int save=f;\n f=u;\n u=b;\n b=d;\n d=save;\n }\n \n void turnR(){\n int save=r;\n r=u;\n u=l;\n l=d;\n d=save;\n }\n \n void turnB(){\n int save=b;\n b=u;\n u=f;\n f=d;\n d=save;\n }\n \n void turnL(){\n int save=l;\n l=u;\n u=r;\n r=d;\n d=save;\n }\n \n void turn1(){\n int save=r;\n r=b;\n b=l;\n l=f;\n f=save;\n }//時計回り\n \n void turn2(){\n int save=r;\n r=f;\n f=l;\n l=b;\n b=save;\n }//反時計回り\n};\n\nstruct dat{\n int wh;\n int h;\n int w;\n int u;\n int f;\n};\n\nint H,W;\nint sh,sw;\nint S[MAX][MAX];\nint dis[7][MAX][MAX][7][7];\nint jun[7];\n\nvoid BFS(){\n for(int a=0;a<7;a++) for(int b=0;b<MAX;b++) for(int c=0;c<MAX;c++) for(int d=0;d<7;d++) for(int e=0;e<7;e++) dis[a][b][c][d][e]=INF;\n \n dis[0][sh][sw][1][2]=0;\n queue<dat> Q;\n Q.push({0,sh,sw,1,2});\n \n while(!Q.empty()){\n auto u=Q.front();Q.pop();\n if(u.wh==6) break;\n \n int d=dis[u.wh][u.h][u.w][u.u][u.f];\n \n dice X;\n X.init(u.u,u.f);\n \n int toh,tow;\n \n if(u.h>=1){\n toh=u.h-1;tow=u.w;\n if(S[toh][tow]!=-1){\n X.turnB();\n if(S[toh][tow]==0){\n if(chmin(dis[u.wh][toh][tow][X.u][X.f],d+1)) Q.push({u.wh,toh,tow,X.u,X.f});\n }\n if(S[toh][tow]==X.u&&jun[u.wh+1]==X.u){\n if(chmin(dis[u.wh+1][toh][tow][X.u][X.f],d+1)) Q.push({u.wh+1,toh,tow,X.u,X.f});\n }\n X.turnF();\n }\n }\n \n if(u.h+1<H){\n toh=u.h+1;tow=u.w;\n X.turnF();\n if(S[toh][tow]==0){\n if(chmin(dis[u.wh][toh][tow][X.u][X.f],d+1)) Q.push({u.wh,toh,tow,X.u,X.f});\n }\n if(S[toh][tow]==X.u&&jun[u.wh+1]==X.u){\n if(chmin(dis[u.wh+1][toh][tow][X.u][X.f],d+1)) Q.push({u.wh+1,toh,tow,X.u,X.f});\n }\n X.turnB();\n }\n \n if(u.w>=1){\n toh=u.h;tow=u.w-1;\n X.turnL();\n if(S[toh][tow]==0){\n if(chmin(dis[u.wh][toh][tow][X.u][X.f],d+1)) Q.push({u.wh,toh,tow,X.u,X.f});\n }\n if(S[toh][tow]==X.u&&jun[u.wh+1]==X.u){\n if(chmin(dis[u.wh+1][toh][tow][X.u][X.f],d+1)) Q.push({u.wh+1,toh,tow,X.u,X.f});\n }\n X.turnR();\n }\n \n if(u.w+1<W){\n toh=u.h;tow=u.w+1;\n X.turnR();\n if(S[toh][tow]==0){\n if(chmin(dis[u.wh][toh][tow][X.u][X.f],d+1)) Q.push({u.wh,toh,tow,X.u,X.f});\n }\n if(S[toh][tow]==X.u&&jun[u.wh+1]==X.u){\n if(chmin(dis[u.wh+1][toh][tow][X.u][X.f],d+1)) Q.push({u.wh+1,toh,tow,X.u,X.f});\n }\n X.turnL();\n }\n }\n}\n\nint main(){\n \n std::ifstream in(\"text.txt\");\n std::cin.rdbuf(in.rdbuf());\n cin.tie(0);\n ios::sync_with_stdio(false);\n \n map<char,int> MA;\n MA['r']=1;\n MA['m']=2;\n MA['b']=3;\n MA['y']=4;\n MA['g']=5;\n MA['c']=6;\n MA['#']=0;\n MA['w']=0;\n MA['k']=-1;\n \n while(1){\n cin>>W>>H;\n if(H==0) break;\n memset(S,-1,sizeof(S));\n for(int i=0;i<H;i++){\n for(int j=0;j<W;j++){\n char c;cin>>c;\n S[i][j]=MA[c];\n if(c=='#'){\n sh=i;\n sw=j;\n }\n }\n }\n string T;cin>>T;\n for(int i=0;i<6;i++) jun[i+1]=MA[T[i]];\n \n BFS();\n \n int ans=INF;\n \n for(int i=0;i<H;i++){\n for(int j=0;j<W;j++){\n if(S[i][j]==jun[6]){\n for(int a=1;a<=6;a++){\n for(int b=1;b<=6;b++){\n chmin(ans,dis[6][i][j][a][b]);\n }\n }\n }\n }\n }\n \n if(ans==INF) cout<<\"unreachable\\n\";\n else cout<<ans<<endl;\n }\n}", "accuracy": 1, "time_ms": 40, "memory_kb": 4892, "score_of_the_acc": -0.0458, "final_rank": 2 } ]
aoj_1300_cpp
Problem F: Chemist's Math You have probably learnt chemical equations (chemical reaction formulae) in your high-school days. The following are some well-known equations. 2H 2 + O 2 → 2H 2 O (1) C a (OH) 2 + CO 2 → C a CO 3 + H 2 O (2) N 2 + 3H 2 → 2NH 3 (3) While Equations (1)–(3) all have balanced left-hand sides and right-hand sides, the following ones do not. Al + O 2 → Al 2 O 3 ( wrong ) (4) C 3 H 8 + O 2 → CO 2 + H 2 O ( wrong ) (5) The equations must follow the law of conservation of mass ; the quantity of each chemical element (such as H, O, Ca, Al) should not change with chemical reactions. So we should "adjust" the numbers of molecules on the left-hand side and right-hand side: 4Al + 3O 2 → 2Al 2 O 3 ( correct ) (6) C 3 H 8 + 5O 2 → 3CO 2 + 4H 2 O (correct) (7) The coefficients of Equation (6) are (4, 3, 2) from left to right, and those of Equation (7) are (1, 5, 3, 4) from left to right. Note that the coefficient 1 may be omitted from chemical equations. The coefficients of a correct equation must satisfy the following conditions. The coefficients are all positive integers. The coefficients are relatively prime, that is, their greatest common divisor (g.c.d.) is 1. The quantities of each chemical element on the left-hand side and the right-hand side are equal. Conversely, if a chemical equation satisfies the above three conditions, we regard it as a correct equation, no matter whether the reaction or its constituent molecules can be chemically realized in the real world, and no matter whether it can be called a reaction (e.g., H 2 → H 2 is considered correct). A chemical equation satisfying Conditions 1 and 3 (but not necessarily Condition 2) is called a balanced equation. Your goal is to read in chemical equations with missing coefficients like Equation (4) and (5), line by line, and output the sequences of coefficients that make the equations correct. Note that the above three conditions do not guarantee that a correct equation is uniquely determined. For example, if we "mix" the reactions generating H 2 O and NH 3 , we would get x H 2 + y O 2 + z N 2 + u H 2 → v H 2 O + w NH 3 (8) but ( x , y , z , u , v , w ) = (2, 1, 1, 3, 2, 2) does not represent a unique correct equation; for instance, (4, 2, 1, 3, 4, 2) and (4, 2, 3, 9, 4, 6) are also "correct" according to the above definition! However, we guarantee that every chemical equation we give you will lead to a unique correct equation by adjusting their coefficients. In other words, we guarantee that (i) every chemical equation can be balanced with positive coefficients, and that (ii) all balanced equations of the original equation can be obtained by multiplying the co ...(truncated)
[ { "submission_id": "aoj_1300_1055603", "code_snippet": "#include <iostream>\n#include <cstdio>\n#include <vector>\n#include <queue>\n#include <cmath>\n#include <algorithm>\n#include <functional>\n#include <map>\n#include <cctype>\nusing namespace std;\n#define rep(i,n) for(int i=0;i<(n);++i)\n#define rep1(i,n) for(int i=1;i<=(n);++i)\n#define all(c) (c).begin(),(c).end()\n#define pb push_back\n#define fs first\n#define sc second\n#define show(x) cout << #x << \" = \" << x << endl;\ntypedef string::const_iterator State;\ntypedef map<string,int> msi;\ntypedef vector<int> vi;\ntypedef vector<vi> mat;\ntypedef pair<string,int> P;\ntypedef pair<int,int> Pa;\n\nconst int MAX_eq=26*27;\nmsi tonum;\nmat a;\nint ct;\nPa memo[MAX_eq];\nbool visited[MAX_eq]={};\nint cnt;\n\nbool isC(char c){\n\treturn'A'<=c && c<='Z';\n}\nbool isS(char c){\n\treturn'a'<=c && c<='z';\n}\nvoid mul(msi& m,int x){\n\tfor(auto& v : m){\n\t\tv.sc*=x;\n\t}\n}\nvoid add(msi& x,msi y){\n\tfor(auto& v : y){\n\t\tx[v.fs]+=v.sc;\n\t}\n}\nmsi atom(State &be){\n\t//cout << \"atmo\";\n\tmsi ret;\n\tstring a;\n\ta+=(*be);\n\tbe++;\n\tif(isS(*be)) a+=(*be),be++;\n\tret[a]++;\n\treturn ret;\n}\nint num(State &be){\n\t//cout << \"num\";\n\tint ret=0;\n\twhile(isdigit(*be)){\n\t\tret*=10;\n\t\tret+=(*be)-'0';\n\t\tbe++;\n\t}\n\treturn ret;\n}\nmsi mol(State &be){\n\tmsi ret;\n\twhile(isC(*be) || (*be)=='('){\n\t\t//cout << *be;\n\t\tmsi x;\n\t\tif(isC(*be)){\n\t\t\tx=atom(be);\n\t\t\tif(isdigit(*be)) mul(x,num(be));\n\t\t}else if(*be=='('){\n\t\t\tbe++;\n\t\t\tx=mol(be);\n\t\t\tbe++;\n\t\t\tif(isdigit(*be)) mul(x,num(be));\n\t\t}\n\t\tadd(ret,x);\n\t}\n\treturn ret;\n}\nvoid seqmol(State &be){\n\twhile(*be=='+'){\n\t\tbe++;\n\t\tmsi ret=mol(be);\n\t\t//for(auto v : ret) cout << tonum[v.fs] << \" \" << ct<<endl;\n\t\tfor(auto v : ret) a[tonum[v.fs]][ct]=v.sc;\n\t\tct++;\n\t}\n\tbe++;\n\twhile(*be!='.'){\n\t\tbe++;\n\t\tmsi ret=mol(be);\n\t\tmul(ret,-1);\n\t\t//for(auto v : ret) cout << tonum[v.fs] << \" \" << ct<<endl;\n\t\tfor(auto v : ret) a[tonum[v.fs]][ct]=v.sc;\n\t\tct++;\n\t}\n}\nint gcd(int x,int y){\n\tif(x<0) x=-x;\n\tif(y<0) y=-y;\n\tif(y==0) return x;\n\treturn gcd(y,x%y);\n}\nint lcm(int x,int y){\n\treturn x/gcd(x,y)*y;\n}\nvoid sweep(int eq, int var, mat& a){\n\tint ans=0;\n\tbool used[MAX_eq]={};\n\trep(j,var){\n\t\tint i=0;\n\t\twhile( (i < eq) && (used[i] || (a[i][j] == 0) ) ) i++;\n\t\tif(i == eq) continue;\n\t\tans++;\n\t\tused[i] = true;\n\t\trep(k,eq){\n\t\t\tif(i==k || a[k][j]==0) continue;\n\t\t\tint g=gcd(a[k][j],a[i][j]),akj=a[k][j];\n\t\t\trep(l,var){\n\t\t\t\ta[k][l] = a[k][l]*(a[i][j]/g) - a[i][l]*(akj/g);\n\t\t\t}\n\t\t}\n\t}\n}\nvoid dfs(int id,int bunshi,int bunbo){\n\tmemo[id]=Pa(bunshi,bunbo);\n//\tcout << \"id\"<<id<< \" \"<<bunshi<<\"/\"<<bunbo<<endl;\n\tvisited[id]=true;\n\trep(i,cnt){\n\t\tif(a[i][id]!=-0){\n\t\t\trep(j,ct){\n\t\t\t\tif(j!=id&&a[i][j]!=0&&!visited[j]){\n\t\t\t\t\tint nshi=bunshi*a[i][id],nbo=bunbo*a[i][j];\n\t\t\t\t\tif(nshi<0) nshi*=-1;\n\t\t\t\t\tif(nbo<0) nbo*=-1;\n\t\t\t\t\tint g=gcd(nshi,nbo);\n\t\t\t\t\tnshi/=g,nbo/=g;\n\t\t\t\t\tdfs(j,nshi,nbo);\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n}\nint main(){\n\tcnt=0;\n\trep(i,26){\n\t\tstring st(1,'A'+i);\n\t\ttonum.insert(P(st,cnt++));\n\t}\n\trep(i,26) rep(j,26){\n\t\tstring st(1,'A'+i);\n\t\tst+=('a'+j);\n\t\ttonum.insert(P(st,cnt++));\n\t}\n\twhile(true){\n\t\tstring s;\n\t\tcin>>s;\n\t\tif(s[0]=='.') break;\n\t\ts=\"+\"+s;\n\t\tState be=s.begin();\n\t\tct=0;\n\t\ta.clear();\n\t\trep(i,cnt) a.pb(vi(MAX_eq,0));\n\t\tseqmol(be);\n\t\tsweep(cnt,ct,a);\n\t\trep(i,MAX_eq) visited[i]=false;\n\t\tdfs(0,1,1);\n\t\tint lc=1;\n\t\trep(i,ct){\n\t\t\tlc=lcm(lc,memo[i].sc);\n\t\t}\n\t\trep(i,ct){\n\t\t\tprintf(\"%d\",memo[i].fs*lc/memo[i].sc);\n\t\t\tprintf((i!=ct-1 ? \" \" : \"\\n\"));\n\t\t}\n\t}\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3112, "score_of_the_acc": -1, "final_rank": 2 }, { "submission_id": "aoj_1300_566783", "code_snippet": "#include <iostream>\n#include <cstdio>\n#include <vector>\n#include <algorithm>\n#include <complex>\n#include <cstring>\n#include <cstdlib>\n#include <string>\n#include <cmath>\n#include <cassert>\n#include <queue>\n#include <set>\n#include <map>\n#include <valarray>\n#include <bitset>\n#include <stack>\n#include <iomanip>\n#include <fstream>\nusing namespace std;\n\n#define REP(i,n) for(int i=0;i<(int)(n);++i)\n#define FOR(i,c) for(__typeof((c).begin())i=(c).begin();i!=(c).end();++i)\n#define ALL(c) (c).begin(), (c).end()\n#define chmax(a,b) (a<b?(a=b,1):0)\n#define chmin(a,b) (a>b?(a=b,1):0)\n#define valid(y,x,h,w) (0<=y&&y<h&&0<=x&&x<w)\nconst int INF = 1<<29;\nconst double EPS = 1e-8;\ntypedef long long ll;\ntypedef pair<int,int> pii;\n\n\ntypedef vector<double> vec;\ntypedef vector<vec> mat;\n\nostream &operator<<(ostream &os, const vec &a) {\n FOR(it, a) {\n double a = *it;\n if (abs(*it) < EPS*EPS) a = 0;\n os << setw(3) << a << \" \";\n }\n return os;\n}\nostream &operator<<(ostream &os, const mat &a) {\n FOR(it, a) os << *it << endl;\n return os;\n}\n\nbool GaussElimination(const mat &A, const vec &b, vec &res) {\n int n = A.size();\n mat B(n, vec(n+1));\n REP(i,n) REP(j,n)\n B[i][j] = A[i][j];\n REP(i, n) B[i][n] = b[i];\n \n int nowy = 0;\n REP(x, n) {\n int pivot = -1;\n for (int j=nowy; j<n; ++j)\n if (pivot == -1 || abs(B[pivot][x]) < abs(B[j][x])) {\n pivot = j;\n }\n if (pivot == -1 || abs(B[pivot][x])<EPS) continue;\n swap(B[nowy], B[pivot]);\n\n for (int j=nowy+1; j<n; ++j) {\n double t = B[j][x] / B[nowy][x];\n for (int k=x; k<=n; ++k)\n B[j][k] = B[j][k] - B[nowy][k] * t;\n }\n nowy++;\n }\n res.clear();\n for (int y=nowy; y<n; ++y)\n if (abs(B[y][n])>EPS) // rank(A) != rank(A|b)\n return 0;\n if (nowy != n) { // rank(A) == rank(A|b) != n\n res = vec(n,1e100);\n for (int y=n-1; y>=0; --y) {\n int x;\n for (x=y; x<n; ++x) {\n if (abs(B[y][x])>EPS) break;\n }\n if (x==n) continue;\n double sum = B[y][n];\n for (int i=x+1; i<n; ++i) {\n if (res[i] == 1e100) res[i] = 0;\n sum -= res[i] * B[y][i];\n }\n res[x] = sum / B[y][x];\n }\n REP(i,n) if(res[i]==INF) res[i] = 0;\n return 0;\n }\n // ティツァツ」テ」ツ?古、ツクツ?ヲツ?湘」ツ?ォテヲツアツコテ」ツ?セテ」ツつ凝」ツ?づヲツ卍ョテゥツ?堙」ツ?ォテ・ツセツ古ゥツ??、ツサツ」テ・ツ?・\n res.resize(n);\n for (int x=n-1; x>=0; --x) {\n double sum = B[x][n];\n for (int i=n-1; i>x; --i) {\n sum -= res[i] * B[x][i]; \n }\n res[x] = sum / B[x][x];\n }\n return 1;\n}\n\n\nvector<string> split(string s, char c) {\n\tvector<string> ret;\n replace(ALL(s),c,' ');\n stringstream ss(s);\n string t;\n while(ss>>t) ret.push_back(t);\n return ret;\n}\n\nint find(const string &s, char c) {\n return s.find(c);\n}\n\ntypedef map<string, int> MP;\nchar str[100];\n\nMP group(int &id);\nMP unit_group(int &id);\nint number(int &id);\n\nMP molecule(int &id) {\n MP res;\n while(isupper(str[id])||str[id]=='(') {\n MP tmp = group(id);\n FOR(it, tmp) res[it->first] += it->second;\n }\n return res;\n}\n\nMP group(int &id) {\n MP res = unit_group(id);\n if (isdigit(str[id])) {\n int a = number(id);\n MP tmp;\n FOR(it, res) {\n tmp[it->first] = it->second*a;\n }\n res = tmp;\n }\n return res;\n}\nMP unit_group(int &id) {\n MP res;\n if (str[id] == '(') {\n id++;\n res = molecule(id);\n assert(str[id++] == ')');\n } else {\n assert(isupper(str[id]));\n int len = 1;\n if (islower(str[id+1])) len = 2;\n string tmp(len,' '); REP(i,len) tmp[i]=str[id+i];\n res[tmp] = 1;\n id+=len;\n }\n return res;\n}\n\nint number(int &id) {\n int res = 0;\n while(isdigit(str[id])) {\n res *= 10;\n res += str[id]-'0';\n id++;\n }\n return res;\n}\n\nbool integer(double x) {\n return abs(x-round(x)) < EPS;\n}\n\nint a[500][500];\n\nint main() {\n string s;\n while(cin >> s, s!=\".\") {\n s = s.substr(0,s.size()-1);\n int pos = find(s,'-');\n string s1 = s.substr(0,pos);\n string s2 = s.substr(pos+2);\n vector<string> v1 = split(s1,'+');\n vector<string> v2 = split(s2,'+');\n memset(a,0,sizeof(a));\n int kind = 0;\n map<string,int> mp;\n REP(i,v1.size()) {\n int id = 0;\n strcpy(str, v1[i].c_str());\n MP tmp = molecule(id);\n FOR(jt, tmp) {\n string s = jt->first;\n int num = jt->second;\n // cout << \"A \" << s << \" \" << num << endl;\n if (mp.count(s)==0) {\n mp[s] = kind++;\n }\n a[mp[s]][i] = num;\n }\n }\n REP(i,v2.size()) {\n int id = 0;\n strcpy(str, v2[i].c_str());\n MP tmp = molecule(id);\n FOR(jt, tmp) {\n string s = jt->first;\n int num = jt->second;\n // cout << \"B \" << s << \" \" << num << endl;\n if (mp.count(s)==0) {\n mp[s] = kind++;\n }\n a[mp[s]][v1.size()+i] = -num;\n } \n }\n a[kind++][0] = 1;\n int N = v1.size()+v2.size();\n mat A(kind,vec(kind));\n vec b(kind);\n REP(i,kind)REP(j,N) A[i][j]=a[i][j];\n // cout << A << endl; cout << endl;\n b[kind-1] = 1;\n vec x;\n GaussElimination(A,b,x);\n // cout << \"A\" << endl;\n // cout << A.size() << \" \" << A[0].size() << endl;\n // cout << b.size() << endl;\n // cout << x << endl;\n for (int i=1; i<=40000; ++i) {\n vec t(x);\n bool ok = 1;\n FOR(it, t) if (!integer(*it *= i)) {\n ok = 0;\n break;\n }\n if (ok) {\n REP(i,N) {\n if (i) cout << \" \";\n cout << t[i];\n }\n cout << endl;\n break;\n }\n }\n }\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 2336, "score_of_the_acc": -0.5698, "final_rank": 1 }, { "submission_id": "aoj_1300_520218", "code_snippet": "#include<map>\n#include<cctype>\n#include<cstdio>\n#include<string>\n#include<vector>\n#include<cassert>\n\n#define rep(i,n) for(int i=0;i<(n);i++)\n\nusing namespace std;\n\ntypedef long long ll;\n\nll gcd(ll a,ll b){ return b?gcd(b,a%b):a; }\nll lcm(ll a,ll b){ return a/gcd(a,b)*b; }\n\n/*** RATIONAL LIBRARY BEGIN ***/\ntemplate<class T>\nclass rational{\n\tT nu,de;\n\tvoid reg(){ if(de<0) nu*=-1, de*=-1; }\n\npublic:\n\t// constructor\n\trational():nu(0),de(1){}\n\trational(const T &v):nu(v),de(1){}\n\trational(const T &n,const T &d):nu(n),de(d){\n\t\tT g=gcd(nu,de);\n\t\tnu/=g;\n\t\tde/=g;\n\t\treg();\n\t}\n\n\t// getter\n\tconst T &n(){ return nu; }\n\tconst T &d(){ return de; }\n\n\t// 算術演算\n\trational &operator+=(const rational &r){\n\t\tT g=gcd(de,r.de);\n\t\tde/=g;\n\t\tnu=nu*(r.de/g)+r.nu*de;\n\t\tg=gcd(nu,g);\n\t\tnu/=g;\n\t\tde*=r.de/g;\n\t\treg();\n\t\treturn *this;\n\t}\n\n\trational &operator-=(const rational &r){\n\t\tT g=gcd(de,r.de);\n\t\tde/=g;\n\t\tnu=nu*(r.de/g)-r.nu*de;\n\t\tg=gcd(nu,g);\n\t\tnu/=g;\n\t\tde*=r.de/g;\n\t\treg();\n\t\treturn *this;\n\t}\n\n\trational &operator*=(const rational &r){\n\t\tT g1=gcd(nu,r.de),g2=gcd(de,r.nu);\n\t\tnu=(nu/g1)*(r.nu/g2);\n\t\tde=(de/g2)*(r.de/g1);\n\t\treg();\n\t\treturn *this;\n\t}\n\n\trational &operator/=(const rational &r){\n\t\tT g1=gcd(nu,r.nu),g2=gcd(de,r.de);\n\t\tnu=(nu/g1)*(r.de/g2);\n\t\tde=(de/g2)*(r.nu/g1);\n\t\treg();\n\t\treturn *this;\n\t}\n\n\trational operator+(const rational &r)const{ return rational(*this)+=r; }\n\trational operator-(const rational &r)const{ return rational(*this)-=r; }\n\trational operator*(const rational &r)const{ return rational(*this)*=r; }\n\trational operator/(const rational &r)const{ return rational(*this)/=r; }\n\n\trational operator-()const{ return rational<T>(-nu,de); }\n\n\t// 比較演算\n\tbool operator<(const rational &r)const{ return nu*r.de<r.nu*de; } // overflow 注意\n\tbool operator>(const rational &r)const{ return r<*this; }\n\tbool operator<=(const rational &r)const{ return !(r<*this); }\n\tbool operator>=(const rational &r)const{ return !(*this<r); }\n\tbool operator==(const rational &r)const{ return nu==r.nu && de==r.de; }\n\tbool operator!=(const rational &r)const{ return !(*this==r); }\n};\n/*** RATIONAL LIBRARY END ***/\n\nrational<ll> abs(const rational<ll> &r){ return r>=0?r:-r; }\n\nvoid operator+=(vector<ll> &a,const vector<ll> &b){ rep(i,80) a[i]+=b[i]; }\n\nint row;\nll A[80][80]; // 係数行列\n\nmap<string,int> f; // 原子の名前 -> id\n\nint idx;\nchar s[128];\n\nvector<ll> molecule();\n\nint number(){\n\tint num=0;\n\twhile(isdigit(s[idx])) num=num*10+(s[idx++]-'0');\n\treturn num;\n}\n\nvector<ll> chemical_element(){\n\tstring name;\n\tname+=s[idx++];\n\tif(islower(s[idx])) name+=s[idx++];\n\n\tint id;\n\tif(f.count(name)!=0) id=f[name];\n\telse{\n\t\tid=f.size();\n\t\tf[name]=id;\n\t}\n\n\tvector<ll> a(80);\n\ta[id]=1;\n\treturn a;\n}\n\nvector<ll> unit_group(){\n\tif(s[idx]!='('){\n\t\treturn chemical_element();\n\t}\n\telse{\n\t\tidx++;\n\t\tvector<ll> a=molecule();\n\t\tassert(s[idx++]==')');\n\t\treturn a;\n\t}\n}\n\nvector<ll> group(){\n\tvector<ll> a=unit_group();\n\tif(isdigit(s[idx])){\n\t\tint num=number();\n\t\trep(i,80) a[i]*=num;\n\t}\n\treturn a;\n}\n\nvector<ll> molecule(){\n\tvector<ll> a=group();\n\twhile(isupper(s[idx]) || s[idx]=='(') a+=group();\n\treturn a;\n}\n\nvoid molecule_sequence(int sign){\n\tvector<ll> a=molecule();\n\trep(i,80) A[row][i]+=sign*a[i];\n\tfor(row++;s[idx]=='+';row++){\n\t\tidx++;\n\t\ta=molecule();\n\t\trep(i,80) A[row][i]+=sign*a[i];\n\t}\n}\n\nvoid chemical_equation(){\n\tmolecule_sequence(+1);\n\tassert(s[idx++]=='-');\n\tassert(s[idx++]=='>');\n\tmolecule_sequence(-1);\n\tassert(s[idx++]=='.');\n}\n\nvoid parse(){\n\tf.clear();\n\trep(i,80) rep(j,80) A[i][j]=0;\n\tidx=row=0;\n\tchemical_equation();\n}\n\nconst int N_MAX=80;\nvoid Gauss_Jordan(int n,const rational<ll> A[N_MAX][N_MAX],const rational<ll> *b,rational<ll> *x){\n\tstatic rational<ll> B[N_MAX][N_MAX+1];\n\trep(i,n){\n\t\trep(j,n) B[i][j]=A[i][j];\n\t\tB[i][n]=b[i];\n\t}\n\n\trep(i,n){\n\t\tint piv=i;\n\t\tfor(int j=i;j<n;j++) if(abs(B[j][i])>abs(B[piv][i])) piv=j;\n\t\trep(j,n+1) swap(B[i][j],B[piv][j]);\n\n\t\tif(B[i][i]==0) break;\n\n\t\tfor(int j=i+1;j<=n;j++) B[i][j]/=B[i][i];\n\t\trep(j,n) if(i!=j) for(int k=i+1;k<=n;k++) B[j][k]-=B[j][i]*B[i][k];\n\t}\n\n\trep(i,n) x[i]=B[i][n];\n}\n\nint main(){\n\tfor(;fgets(s,128,stdin),s[0]!='.';){\n\t\tparse();\n\n\t\trational<ll> A[80][80],b[80],x[80];\n\t\trep(i,80) rep(j,80) A[j][i]=::A[i][j];\n\t\tA[79][0]=b[79]=1; // 一つめの変数の値を 1 に固定する\n\n\t\tGauss_Jordan(80,A,b,x);\n\n\t\tll L=1;\n\t\trep(i,row) L=lcm(L,x[i].d());\n\t\trep(i,row) x[i]*=L;\n\t\tll G=0;\n\t\trep(i,row) G=gcd(G,x[i].n());\n\t\trep(i,row) x[i]/=G;\n\t\trep(i,row) printf(\"%lld%c\",x[i].n(),i<row-1?' ':'\\n');\n\t}\n\n\treturn 0;\n}", "accuracy": 1, "time_ms": 140, "memory_kb": 1308, "score_of_the_acc": -1, "final_rank": 2 } ]
aoj_1294_cpp
Problem J: Zigzag Given several points on a plane, let’s try to solve a puzzle connecting them with a zigzag line. The puzzle is to find the zigzag line that passes through all the given points with the minimum number of turns. Moreover, when there are several zigzag lines with the minimum number of turns, the shortest one among them should be found. For example, consider nine points given in Figure 1. Figure 1: Given nine points A zigzag line is composed of several straight line segments. Here, the rule requests that each line segment should pass through two or more given points. A zigzag line may turn at some of the given points or anywhere else. There may be some given points passed more than once. Figure 2: Zigzag lines with three turning points. Two zigzag lines with three turning points are depicted in Figure 2 (a) and (b) for the same set of given points shown in Figure 1. The length of the zigzag line in Figure 2 (a) is shorter than that in Figure 2 (b). In fact, the length of the zigzag line in Figure 2 (a) is the shortest so that it is the solution for the nine points given in Figure 1. Another zigzag line with four turning points is depicted in Figure 3. Its length is shorter than those in Figure 2, however, the number of turning points is greater than those in Figure 2, and thus, it is not the solution. Figure 3: Zigzag line with four turning points. There are two zigzag lines that passes another set of given points depicted in Figure 4 (a) and (b). Both have the same number of turning points, and the line in (a) is longer than that in (b). However, the solution is (a), because one of the segments of the zigzag line in (b) passes only one given point, violating the rule. Figure 4: Zigzag line with two turning points (a), and not a zigzag line concerned (b). Your job is to write a program that solves this puzzle. Input The input consists of multiple datasets, followed by a line containing one zero. Each dataset has the following format. n x 1 y 1 . . . x n y n Every input item in a dataset is a non-negative integer. Items in a line are separated by a single space. n is the number of the given points. x k and y k ( k = 1, . . . , n ) indicate the position of the k -th point. The order of the points is meaningless. You can assume that 2 ≤ n ≤ 10, 0 ≤ x k ≤ 10, and 0 ≤ y k ≤ 10. Output For each dataset, the minimum number of turning points and the length of the shortest zigzag line with that number of turning points should be printed, separated by a space in a line. The length should be in a decimal fraction with an error less than 0.0001 (= 1.0 × 10 -4 ). You may assume that the minimum number of turning points is at most four, that is, the number of line segments is at most five. The example solutions for the first four datasets in the sample input are depicted in Figure 5 and 6. Sample Input 2 0 0 10 9 4 0 0 3 1 0 3 3 3 10 2 2 4 2 6 2 2 4 4 4 6 4 2 6 4 6 6 6 3 3 10 0 0 2 0 4 0 0 2 2 2 4 2 0 4 2 4 4 4 6 8 9 0 0 1 0 3 0 0 1 1 1 3 1 ...(truncated)
[ { "submission_id": "aoj_1294_10853309", "code_snippet": "#include<iostream>\n#include<cstdio>\n#include<cstdlib>\n#include<algorithm>\n#include<cstring>\n#include<cmath>\n#include<ctime>\n#include<map>\n#include<string>\n#include<vector>\n#include<set>\n\nusing namespace std;\n#define For(i,l,r) for (int i = l; i <= r; ++i)\n#define Cor(i,l,r) for (int i = l; i >= r; --i)\n#define Fill(a,b) memset(a,b,sizeof(a))\n#define FI first\n#define SE second\n#define MP make_pair\n#define PII pair<int,int>\n#define flt double\n#define INF (0x3f3f3f3f)\n#define MaxN 1020304\n#define MaxNode 1020304\n#define MD 1000000007\n#define ele(x) (1 << ((x) - 1))\n#define PFF pair<flt,flt>\n#define UPD(a,b) { a = min(a,b); }\n\nconst flt eps = 1e-7;\nint dcmp(flt x) {\n\treturn (x < -eps) ? -1 : ((x > eps) ? 1 : 0);\n}\n\nPFF operator - (PFF a,PFF b) { return MP(a.FI - b.FI,a.SE - b.SE); }\nPFF operator + (PFF a,PFF b) { return MP(a.FI + b.FI,a.SE + b.SE); }\nPFF operator * (PFF a,flt b) { return MP(a.FI * b,a.SE * b); }\nflt operator * (PFF a,PFF b) { return a.FI * b.SE - a.SE * b.FI; }\nflt operator / (PFF a,PFF b) { \n\tif (dcmp(b.FI)) return a.FI / b.FI;\n\treturn a.SE / b.SE;\n}\n\nflt dot(PFF a,PFF b) { return a.FI * b.FI + a.SE * b.SE; }\nflt mod(PFF a) {\n\treturn sqrt(a.FI * a.FI + a.SE * a.SE); \n}\n\nbool Longer(PFF a,PFF b,PFF c) {\n\treturn dcmp((c - a) / (b - a) - 1.) >= 0;\n}\n\nflt xmul(PFF a,PFF b,PFF c) {\n\treturn (c - a) * (c - b);\n}\n\nPFF GetInter(PFF a,PFF b,PFF c,PFF d) {\n\tPFF as = a, ae = a + b;\n\tPFF bs = c,be = c + d;\n\tflt u = xmul(as,ae,bs);\n\tflt v = xmul(ae,as,be);\n\tif (dcmp(u + v) == 0) return MP(-INF,-INF);\n\treturn MP(bs.FI * v + be.FI * u,bs.SE * v + be.SE * u) * (1. / (u + v));\n}\n\nint n;\nPFF a[MaxN]; pair<int,flt> f[1111][11][11];\n\nint main() {\n\t//freopen(\"input.txt\",\"r\",stdin); freopen(\"output.txt\",\"w\",stdout);\n\twhile (scanf(\"%d\",&n) != EOF && n) {\n\t\tFor(i,1,n) scanf(\"%lf%lf\",&a[i].FI,&a[i].SE);\n\t\tint All = (1 << n) - 1;\n\t\tFor(i,1,n) For(j,1,n) For(k,0,All) f[k][i][j] = MP(INF,INF);\n\t\tFor(i,1,n) For(j,1,n) if (i != j) {\n\t\t\tf[ele(i) | ele(j)][i][j] = MP(0,mod(a[i] - a[j]));\n\t\t}\n\t\tFor(S,0,All) For(i,1,n) For(j,1,n) if (f[S][i][j].FI < n) {\n\t\t\tFor(k,1,n) if (!(S & ele(k))) {\n\t\t\t\tint tInc = 1;\n\t\t\t\tif (dcmp(fabs((a[k] - a[i]) * (a[i] - a[j]))) == 0 && Longer(a[j],a[i],a[k])) tInc = 0; \n\t\t\t\tUPD(f[S | ele(k)][k][i],MP(f[S][i][j].FI + tInc,f[S][i][j].SE + mod(a[k] - a[i])));\n\t\t\t}\n\t\t\tFor(k,1,n) if (!(S & ele(k))) {\n\t\t\t\tFor(l,1,n) if (!(S & ele(l)) && k != l) {\n\t\t\t\t\tif (dcmp((a[i] - a[j]) * (a[l] - a[k])) != 0) {\n\t\t\t\t\t\tif (k == 4 && l == 3) {\n\t\t\t\t\t\t\tint z = k;\n\t\t\t\t\t\t}\n\t\t\t\t\t\tPFF tInter = GetInter(a[j],a[i] - a[j],a[k],a[l] - a[k]);\n\t\t\t\t\t\tif (Longer(a[j],a[i],tInter) && Longer(a[l],a[k],tInter)) {\n\t\t\t\t\t\t\tUPD(f[S | ele(k) | ele(l)][l][k],MP(f[S][i][j].FI + 1,f[S][i][j].SE + mod(tInter - a[i]) + mod(tInter - a[l])));\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\tpair<int,flt> ans = MP(INF,INF);\n\t\tFor(i,1,n) For(j,1,n) ans = min(ans,f[All][i][j]);\n\t\tprintf(\"%d %.12lf\\n\",ans.FI,ans.SE);\n\t}\n\treturn 0;\n}", "accuracy": 1, "time_ms": 200, "memory_kb": 7388, "score_of_the_acc": -0.0823, "final_rank": 4 }, { "submission_id": "aoj_1294_8370460", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\nstruct value {\n\tint turn; double cost;\n\tbool operator!=(const value& v) const {\n\t\treturn turn != v.turn || cost != v.cost;\n\t}\n\tbool operator<(const value& v) const {\n\t\treturn turn != v.turn ? turn < v.turn : cost < v.cost;\n\t}\n\tvalue operator+(const value& v) const {\n\t\treturn value{turn + v.turn, cost + v.cost};\n\t}\n};\n\nstruct state {\n\tint edge, bit; value cost;\n\tbool operator<(const state& s) const {\n\t\treturn s.cost < cost;\n\t}\n};\n\nint main() {\n\twhile (true) {\n\t\t// step #1. input\n\t\tint N;\n\t\tcin >> N;\n\t\tif (N == 0) {\n\t\t\tbreak;\n\t\t}\n\t\tvector<int> X(N), Y(N);\n\t\tfor (int i = 0; i < N; i++) {\n\t\t\tcin >> X[i] >> Y[i];\n\t\t}\n\n\t\t// step #2. find transitions\n\t\tauto getcross = [&](int s1, int s2, int t1, int t2) -> int {\n\t\t\treturn (X[s2] - X[s1]) * (Y[t2] - Y[t1]) - (X[t2] - X[t1]) * (Y[s2] - Y[s1]);\n\t\t};\n\t\tvector<vector<double> > subd(N, vector<double>(N));\n\t\tfor (int i = 0; i < N; i++) {\n\t\t\tfor (int j = 0; j < N; j++) {\n\t\t\t\tsubd[i][j] = hypot(X[i] - X[j], Y[i] - Y[j]);\n\t\t\t}\n\t\t}\n\t\tconst value INF = value{1012345678, 1.0e+99};\n\t\tvector<vector<value> > d(N * N, vector<value>(N * N, INF));\n\t\tfor (int i = 0; i < N * N; i++) {\n\t\t\tfor (int j = 0; j < N * N; j++) {\n\t\t\t\tint s1 = i / N, s2 = i % N;\n\t\t\t\tint t1 = j / N, t2 = j % N;\n\t\t\t\tif (s1 != s2 && t1 != t2) {\n\t\t\t\t\tint cross = getcross(s1, s2, t1, t2);\n\t\t\t\t\tif (cross == 0 && s2 == t1) {\n\t\t\t\t\t\tint dot = (X[s2] - X[s1]) * (X[t2] - X[t1]) + (Y[s2] - Y[s1]) * (Y[t2] - Y[t1]);\n\t\t\t\t\t\td[i][j] = value{dot >= 0 ? 0 : 1, subd[t1][t2]};\n\t\t\t\t\t}\n\t\t\t\t\tif (cross != 0) {\n\t\t\t\t\t\tint c1 = getcross(s2, t1, s2, t2);\n\t\t\t\t\t\tint c2 = getcross(t1, s1, t1, s2);\n\t\t\t\t\t\tint f = (cross > 0 ? +1 : -1);\n\t\t\t\t\t\tif (c1 * f >= 0 && c2 * f >= 0) {\n\t\t\t\t\t\t\td[i][j] = value{1, (c1 * subd[s1][s2] + (c2 + cross) * subd[t1][t2]) / cross};\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\n\t\t// step #3. dijkstra\n\t\tvector<vector<value> > dist(N * N, vector<value>(1 << N, INF));\n\t\tpriority_queue<state> que;\n\t\tvector<vector<bool> > vis(N * N, vector<bool>(1 << N, false));\n\t\tfor (int i = 0; i < N * N; i++) {\n\t\t\tint s1 = i / N, s2 = i % N;\n\t\t\tif (s1 != s2) {\n\t\t\t\tint bit = (1 << s1) | (1 << s2);\n\t\t\t\tdist[i][bit] = value{0, subd[s1][s2]};\n\t\t\t\tque.push(state{i, bit, dist[i][bit]});\n\t\t\t}\n\t\t}\n\t\twhile (!que.empty()) {\n\t\t\tstate u = que.top();\n\t\t\tque.pop();\n\t\t\tif (!vis[u.edge][u.bit]) {\n\t\t\t\tvis[u.edge][u.bit] = true;\n\t\t\t\tfor (int j = 0; j < N * N; j++) {\n\t\t\t\t\tint nbit = u.bit | (1 << (j / N)) | (1 << (j % N));\n\t\t\t\t\tif (d[u.edge][j] != INF && u.cost + d[u.edge][j] < dist[j][nbit]) {\n\t\t\t\t\t\tdist[j][nbit] = u.cost + d[u.edge][j];\n\t\t\t\t\t\tque.push(state{j, nbit, dist[j][nbit]});\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\n\t\t// step #4. compute answer & output\n\t\tvalue ans = INF;\n\t\tfor (int i = 0; i < N * N; i++) {\n\t\t\tans = min(ans, dist[i][(1 << N) - 1]);\n\t\t}\n\t\tcout.precision(15);\n\t\tcout << ans.turn << ' ' << fixed << ans.cost << endl;\n\t}\n\n\treturn 0;\n}", "accuracy": 1, "time_ms": 340, "memory_kb": 6356, "score_of_the_acc": -0.0809, "final_rank": 3 }, { "submission_id": "aoj_1294_7221254", "code_snippet": "#include <iostream>\n#include <cmath>\nusing namespace std;\n\nstruct Point {\n\tdouble px, py;\n};\n\nPoint operator+(const Point &a1, const Point &a2) {\n\treturn Point{ a1.px + a2.px, a1.py + a2.py };\n}\n\nPoint operator-(const Point &a1, const Point &a2) {\n\treturn Point{ a1.px - a2.px, a1.py - a2.py };\n}\n\nPoint operator*(const Point &a1, const double &a2) {\n\treturn Point{ a1.px * a2, a1.py * a2 };\n}\n\ndouble ABS(Point A1) {\n\treturn sqrt(A1.px * A1.px + A1.py * A1.py);\n}\n\ndouble crs(Point A1, Point A2) {\n\treturn A1.px * A2.py - A1.py * A2.px;\n}\n\n// Current Checkpoint is A1->A2, Next Checkpoint is A3->A4\n// What is the \"cross point\"?\npair<bool, Point> Cross(Point A1, Point A2, Point A3, Point A4) {\n\tdouble F1 = crs(A2 - A1, A3 - A1);\n\tdouble F2 = crs(A2 - A1, A4 - A1);\n\tdouble F3 = crs(A4 - A3, A1 - A3);\n\tdouble F4 = crs(A4 - A3, A2 - A3);\n\tdouble wari1 = -F1 / (F2 - F1);\n\tdouble wari2 = -F3 / (F4 - F3);\n\t\n\t// Check\n\tif (abs(F1 - F2) < 1e-6) return make_pair(false, Point{ 0.0, 0.0 });\n\tif (wari1 > 0.0) return make_pair(false, Point{ 0.0, 0.0 });\n\tif (wari2 < 1.0) return make_pair(false, Point{ 0.0, 0.0 });\n\t\n\t// Get Cross Point\n\tPoint Target = A3 + ((A4 - A3) * wari1);\n\treturn make_pair(true, Target);\n}\n\n// If A1, A2, A3 is in the same line and in order\nbool InOrder(Point A1, Point A2, Point A3) {\n\tif (abs(crs(A2 - A1, A3 - A1)) > 1e-6) return false;\n\tPoint B1 = A1 - A2;\n\tPoint B2 = A3 - A2;\n\tdouble DOT = B1.px * B2.px + B1.py * B2.py;\n\tif (DOT >= 0.0) return false;\n\treturn true;\n}\n\nint N;\nPoint P[19];\npair<int, double> DP[1024][12][12];\n\npair<int, double> solve(int mask, int pos1, int pos2) {\n\tif (mask == (1<<N)-1) return make_pair(0, 0.0);\n\tif (DP[mask][pos1][pos2].first != 1000000) return DP[mask][pos1][pos2];\n\t\n\t// Prepare\n\tpair<int, double> minx = make_pair(10000, -1.0);\n\tint bit[10];\n\tfor (int i = 0; i < N; i++) bit[i] = ((mask / (1 << i)) % 2);\n\t\n\t// Getting 1 point at once\n\tfor (int i = 0; i < N; i++) {\n\t\tif (bit[i] == 1) continue;\n\t\tpair<int, double> dst1 = make_pair(1, ABS(P[pos2] - P[i]));\n\t\tpair<int, double> dst2 = solve(mask+(1<<i),pos2,i);\n\t\tpair<int, double> dst3 = make_pair(dst1.first + dst2.first, dst1.second + dst2.second);\n\t\tif (InOrder(P[pos1], P[pos2], P[i]) == true) dst3.first -= 1;\n\t\tminx = min(minx, dst3);\n\t}\n\t\n\t// Getting 2 points at once\n\tfor (int i = 0; i < N; i++) {\n\t\tfor (int j = 0; j < N; j++) {\n\t\t\tif (i == j || bit[i] == 1 || bit[j] == 1) continue;\n\t\t\tpair<bool, Point> Target = Cross(P[pos1], P[pos2], P[i], P[j]);\n\t\t\tif (Target.first == false) continue;\n\t\t\tdouble dst1 = ABS(P[pos2] - Target.second);\n\t\t\tdouble dst2 = ABS(P[i] - Target.second);\n\t\t\tdouble dst3 = ABS(P[i] - P[j]);\n\t\t\tpair<int, double> dst4 = solve(mask+(1<<i)+(1<<j), i, j);\n\t\t\tdst4.first += 1;\n\t\t\tdst4.second += (dst1 + dst2 + dst3);\n\t\t\tminx = min(minx, dst4);\n\t\t}\n\t}\n\t\n\t// Return\n\t//cout << mask << \" \" << pos1 << \" \" << pos2 << \" \" << minx.first << \" \" << minx.second << endl;\n\tDP[mask][pos1][pos2] = minx;\n\treturn minx;\n}\n\nint main() {\n\twhile (true) {\n\t\t// Input\n\t\tcin >> N; if (N == 0) break;\n\t\tfor (int i = 0; i < N; i++) cin >> P[i].px >> P[i].py;\n\t\t\n\t\t// Initialize\n\t\tfor (int i = 0; i < (1 << N); i++) {\n\t\t\tfor (int j = 0; j < N; j++) {\n\t\t\t\tfor (int k = 0; k < N; k++) DP[i][j][k] = make_pair(1000000, -1.0);\n\t\t\t}\n\t\t}\n\t\t\n\t\t// Solve\n\t\tpair<int, double> Answer = make_pair(10000, -1.0);\n\t\tfor (int i = 0; i < N; i++) {\n\t\t\tfor (int j = 0; j < N; j++) {\n\t\t\t\tif (i == j) continue;\n\t\t\t\tpair<int, double> ret = solve((1<<i)+(1<<j), i, j);\n\t\t\t\tret.second += ABS(P[i] - P[j]);\n\t\t\t\tAnswer = min(Answer, ret);\n\t\t\t}\n\t\t}\n\t\t\n\t\t// Output\n\t\tprintf(\"%d %.12lf\\n\", Answer.first, Answer.second);\n\t}\n\treturn 0;\n}", "accuracy": 1, "time_ms": 230, "memory_kb": 5796, "score_of_the_acc": -0.0562, "final_rank": 2 }, { "submission_id": "aoj_1294_6808650", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\ntypedef long long ll;\ntemplate<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return true; } return false; }\ntemplate<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return true; } return false; }\n#define all(x) (x).begin(),(x).end()\n#define fi first\n#define se second\n#define mp make_pair\n#define si(x) int(x.size())\nconst int mod=998244353,MAX=55,INF=1<<20;\n\n//幾何ライブラリ\n// define double ll をするときは Point の < と == も書き換えよう!\n\nconst double eps=1e-8;\nconst double pi=acos((double)-1.0L);\n#define equals(a,b) (fabs((a)-(b))<eps)\n\ndouble torad(double deg) {return (double)(deg)*pi/180.0;}\ndouble todeg(double ang) {return ang*180.0/pi;}\n\nclass Point{\npublic:\n double x,y;\n \n Point(double x=0,double y=0):x(x),y(y){}\n \n Point operator + (Point p){return Point(x+p.x,y+p.y);}\n Point operator - (Point p){return Point(x-p.x,y-p.y);}\n Point operator * (double a){return Point(a*x,a*y);}\n Point operator / (double a){return Point(x/a,y/a);}\n \n double abs(){return sqrt(norm());}\n double norm(){return x*x+y*y;}\n \n bool operator < (const Point &p)const{\n return x+eps<p.x||(equals(x,p.x)&&y+eps<p.y);\n //return x<p.x||(x==p.x&&y<p.y);\n }\n \n bool operator == (const Point &p)const{\n return fabs(x-p.x)<eps/100000&&fabs(y-p.y)<eps/100000;\n //return x==p.x&&y==p.y;\n }\n};\n\ntypedef Point Vector;\n\ndouble norm(Vector a){\n return a.x*a.x+a.y*a.y;\n}\n\ndouble abs(Vector a){\n return sqrt(norm(a));\n}\n\ndouble dot(Vector a,Vector b){\n return a.x*b.x+a.y*b.y;\n}\n\ndouble cross(Vector a,Vector b){\n return a.x*b.y-a.y*b.x;\n}\n\nstruct Segment{\n Point p1,p2;\n};\n\nbool isOrthogonal(Vector a,Vector b){\n return equals(dot(a,b),0.0);\n}\n\nbool isOrthogonal(Point a1,Point a2,Point b1,Point b2){\n return isOrthogonal(a1-a2,b1-b2);\n}\n\nbool isOrthogonal(Segment s1,Segment s2){\n return equals(dot(s1.p2-s1.p1,s2.p2-s2.p1),0.0);\n}\n\nbool isParallel(Vector a,Vector b){\n return equals(cross(a,b),0.0);\n}\n\nbool isParallel(Point a1,Point a2,Point b1,Point b2){\n return isParallel(a1-a2,b1-b2);\n}\n\nbool isParallel(Segment s1,Segment s2){\n return equals(cross(s1.p2-s1.p1,s2.p2-s2.p1),0.0);\n}\n\nPoint project(Segment s,Point p){\n Vector base=s.p2-s.p1;\n double r=dot(p-s.p1,base)/norm(base);\n return s.p1+base*r;\n}\n\nPoint reflect(Segment s,Point p){\n return p+(project(s,p)-p)*2.0;\n}\n\nPoint turn(Point p,Point c,double pi){\n double q=atan2(p.y-c.y,p.x-c.x);\n q+=pi;\n p=c+Point{cos(q)*abs(p-c),sin(q)*abs(p-c)};\n \n return p;\n}\n//pをcを中心としてpi回転させる(1周で2π)\n//p=cのときnan\n\n//p0,p1,p2の順に見たときどうなるか?\n\nstatic const int counter_clockwise=1;\nstatic const int clockwise=-1;\nstatic const int online_back=2;\nstatic const int online_front=-2;\nstatic const int on_segment=0;\n\nint ccw(Point p0,Point p1,Point p2){\n Vector a=p1-p0;\n Vector b=p2-p0;\n \n if(cross(a,b)>eps) return counter_clockwise;\n if(cross(a,b)<-eps) return clockwise;\n if(dot(a,b)<-eps) return online_back;\n if(a.norm()<b.norm()) return online_front;\n \n return on_segment;\n}\n\nbool intersect(Point p1,Point p2,Point p3,Point p4){\n return(ccw(p1,p2,p3)*ccw(p1,p2,p4)<=0&&ccw(p3,p4,p1)*ccw(p3,p4,p2)<=0);\n}\n\nbool intersect(Segment s1,Segment s2){\n return intersect(s1.p1,s1.p2,s2.p1,s2.p2);\n}\n\nbool overlap(Segment s1,Segment s2){\n int a=ccw(s1.p1,s1.p2,s2.p1),b=ccw(s1.p1,s1.p2,s2.p2);\n if(a&1||b&1) return 0;\n if(a==2){\n if(b==-2||(b==0&&!(s2.p2==s1.p1))) return 1;\n else return 0;\n }\n if(a==-2){\n if(b==2||(b==0&&!(s2.p2==s1.p2))) return 1;\n else return 0;\n }\n if(a==0){\n if(s1.p1==s2.p1){\n if(b!=2) return 1;\n else return 0;\n }\n else if(s1.p2==s2.p1){\n if(b!=-2) return 1;\n else return 0;\n }\n else return 1;\n }\n return 0;\n}\n//s1とs2の共通の線分(長さ0より大きい)があるかどうか\n\ntypedef Segment Line;\n\ndouble getDistance(Point a,Point b){\n return abs(a-b);\n}\n\ndouble getDistanceLP(Line l,Point p){\n return abs(cross(l.p2-l.p1,p-l.p1)/abs(l.p2-l.p1));\n}\n\ndouble getDistanceSP(Segment s,Point p){\n if(dot(s.p2-s.p1,p-s.p1)<0.0) return abs(p-s.p1);\n if(dot(s.p1-s.p2,p-s.p2)<0.0) return abs(p-s.p2);\n return getDistanceLP(s,p);\n}\n\ndouble getDistance(Segment s1,Segment s2){\n if(intersect(s1,s2)) return 0.0;\n return min({getDistanceSP(s1,s2.p1),getDistanceSP(s1,s2.p2),getDistanceSP(s2,s1.p1),getDistanceSP(s2,s1.p2)});\n}\n\nPoint getCrossPointS(Segment s1,Segment s2){\n //if(ccw(s1.p1,s1.p2,s2.p1)==0&&ccw(s1.p1,s1.p2,s2.p2)==0) return s1.p1;\n Vector base=s2.p2-s2.p1;\n double d1=abs(cross(base,s1.p1-s2.p1));\n double d2=abs(cross(base,s1.p2-s2.p1));\n double t=d1/(d1+d2);\n return s1.p1+(s1.p2-s1.p1)*t;\n}//同じ時壊れます\n\nPoint getCrossPointL(Line l1,Line l2){\n //if(ccw(s1.p1,s1.p2,s2.p1)==0&&ccw(s1.p1,s1.p2,s2.p2)==0) return s1.p1;\n \n Vector v1=l1.p2-l1.p1,v2=l2.p2-l2.p1;\n \n return l1.p1+v1*cross(v2,l2.p1-l1.p1)/cross(v2,v1);\n}\n\nSegment ParallelSegment(Segment s,double d){\n Vector v={-(s.p2-s.p1).y,(s.p2-s.p1).x};\n v=v/abs(v);\n \n s.p1=s.p1+v*d;\n s.p2=s.p2+v*d;\n \n return s;\n}\n\nPoint naisin(Point p1,Point p2,Point p3){\n if(p1==p2&&p2==p3&&p3==p1) return p1;\n \n return (p1*abs(p2-p3)+p2*abs(p1-p3)+p3*abs(p1-p2))/(abs(p2-p3)+abs(p1-p3)+abs(p1-p2));\n}\n\nPoint naisin(Line l1,Line l2,Line l3){\n //平行でない前提\n \n Point p1=getCrossPointL(l1,l2),p2=getCrossPointL(l1,l3),p3=getCrossPointL(l2,l3);\n return naisin(p1,p2,p3);\n}\n\n//ネットの適当を書いたのであってるか全く知りません→あってそう\n\nclass Circle{\npublic:\n Point c;\n double r;\n Circle(Point c=Point(),double r=0.0):c(c),r(r){}\n};\n\nPoint CircleCenter(Point a,Point b,Point c){\n Point u=a-b,v=a-c;\n double m1=(norm(a)-norm(b))/2.0,m2=(norm(a)-norm(c))/2.0;\n \n Point res;\n if(cross(u,v)==0.0){\n res.x=1e9;\n res.y=1e9;\n \n return res;\n }\n res.x=(m1*v.y-m2*u.y)/cross(u,v);\n res.y=(m1*v.x-m2*u.x)/cross(v,u);\n \n return res;\n}\n//3点を通る円の中心を返す\n\n//交わる 0\n// c1がc2のinside 1\n// c1がc2のoutside 2\n// 交わらない 3\n\nint not_intersect(Circle c1,Circle c2){\n double d=getDistance(c1.c,c2.c);\n double r1=c1.r,r2=c2.r;\n if(r1<r2){\n if(d<(r2-r1)) return 1;\n }\n if(r1>r2){\n if(d<(r1-r2)) return 2;\n }\n if(d<=r1+r2) return 0;\n else return 3;\n}\n\npair<Point,Point> segCrossPpoints(Circle c,Line l){\n //assert(intersect(c,l));\n Vector pr=project(l,c.c);\n Vector e=(l.p2-l.p1)/abs(l.p2-l.p1);\n double base=sqrt(c.r*c.r-norm(pr-c.c));\n return make_pair(pr+e*base,pr-e*base);\n}\n\ndouble arg(Vector p){return atan2(p.y,p.x);}\nVector polar(double a,double r){return Point(cos(r)*a,sin(r)*a);}\n\n//inside(outside)\n\npair<Point,Point> getCrossPoints(Circle c1,Circle c2){\n //assert(intersect(c1,c2));\n double d=abs(c1.c-c2.c);\n double a=acos((c1.r*c1.r+d*d-c2.r*c2.r)/(2*c1.r*d));\n double t=arg(c2.c-c1.c);\n return make_pair(c1.c+polar(c1.r,t+a),c1.c+polar(c1.r,t-a));\n}\n\nvector<Line> Commontangent(Circle c1,Circle c2){\n vector<Line> res;\n Point p=c2.c-c1.c;\n \n if(abs(p)>=(c1.r+c2.r)){\n Point a,b;\n a.x=c1.r*(p.x*(c1.r+c2.r)+p.y*sqrt(norm(p)-(c1.r+c2.r)*(c1.r+c2.r)))/norm(p);\n a.y=c1.r*(p.y*(c1.r+c2.r)-p.x*sqrt(norm(p)-(c1.r+c2.r)*(c1.r+c2.r)))/norm(p);\n \n b.x=c1.r*(p.x*(c1.r+c2.r)-p.y*sqrt(norm(p)-(c1.r+c2.r)*(c1.r+c2.r)))/norm(p);\n b.y=c1.r*(p.y*(c1.r+c2.r)+p.x*sqrt(norm(p)-(c1.r+c2.r)*(c1.r+c2.r)))/norm(p);\n \n res.push_back(Line{a+c1.c,a+c1.c+Point{-a.y,a.x}});\n if(!(a==b)){\n res.push_back(Line{b+c1.c,b+c1.c+Point{-b.y,b.x}});\n }\n }\n \n if(abs(p)>=abs(c1.r-c2.r)){\n Point a,b;\n a.x=c1.r*(p.x*(c1.r-c2.r)+p.y*sqrt(norm(p)-(c1.r-c2.r)*(c1.r-c2.r)))/norm(p);\n a.y=c1.r*(p.y*(c1.r-c2.r)-p.x*sqrt(norm(p)-(c1.r-c2.r)*(c1.r-c2.r)))/norm(p);\n \n b.x=c1.r*(p.x*(c1.r-c2.r)-p.y*sqrt(norm(p)-(c1.r-c2.r)*(c1.r-c2.r)))/norm(p);\n b.y=c1.r*(p.y*(c1.r-c2.r)+p.x*sqrt(norm(p)-(c1.r-c2.r)*(c1.r-c2.r)))/norm(p);\n \n res.push_back(Line{a+c1.c,a+c1.c+Point{-a.y,a.x}});\n if(!(a==b)){\n res.push_back(Line{b+c1.c,b+c1.c+Point{-b.y,b.x}});\n }\n }\n \n return res;\n}\n\ntypedef vector<Point> Polygon;\n\n/*\n IN 2\n ON 1\n OUT 0\n */\n\nint contains(Polygon g,Point p){\n int n=int(g.size());\n bool x=false;\n for(int i=0;i<n;i++){\n Point a=g[i]-p,b=g[(i+1)%n]-p;\n if(a.y>b.y) swap(a,b);\n if(a.y<eps&&0<b.y&&cross(a,b)<0) x=!x;\n if(abs(cross(a,b))<eps&&dot(a,b)<eps) return 1;\n }\n return (x?2:0);\n}\n\nPolygon andrewScan(Polygon s,bool ok){\n Polygon u,l;\n sort(all(s));\n \n if(int(s.size())<3) return s;\n int n=int(s.size());\n \n u.push_back(s[0]);\n u.push_back(s[1]);\n \n l.push_back(s[n-1]);\n l.push_back(s[n-2]);\n \n if(ok){\n for(int i=2;i<n;i++){\n for(int j=int(u.size());j>=2&&ccw(u[j-2],u[j-1],s[i])==counter_clockwise;j--){\n u.pop_back();\n }\n u.push_back(s[i]);\n }\n \n for(int i=int(s.size())-3;i>=0;i--){\n for(int j=int(l.size());j>=2&&ccw(l[j-2],l[j-1],s[i])==counter_clockwise;j--){\n l.pop_back();\n }\n l.push_back(s[i]);\n }\n }\n \n if(!ok){\n for(int i=2;i<n;i++){\n for(int j=int(u.size());j>=2&&ccw(u[j-2],u[j-1],s[i])!=clockwise;j--){\n u.pop_back();\n }\n u.push_back(s[i]);\n }\n \n for(int i=int(s.size())-3;i>=0;i--){\n for(int j=int(l.size());j>=2&&ccw(l[j-2],l[j-1],s[i])!=clockwise;j--){\n l.pop_back();\n }\n l.push_back(s[i]);\n }\n }\n \n reverse(all(l));\n \n for(int i=int(u.size())-2;i>=1;i--) l.push_back(u[i]);\n \n return l;\n}//ok==1なら辺の上も含める\n\nPolygon convex_cut(const Polygon& P, const Line& l) {\n Polygon Q;\n for(int i=0;i<si(P);i++){\n Point A=P[i],B=P[(i+1)%si(P)];\n if(ccw(l.p1,l.p2,A)!=-1)Q.push_back(A);\n if(ccw(l.p1,l.p2,A)*ccw(l.p1,l.p2,B)<0) Q.push_back(getCrossPointL(Line{A,B},l));\n }\n return Q;\n}\n\ndouble area(Point a,Point b,Point c){\n b=b-a;\n c=c-a;\n return abs(b.x*c.y-b.y*c.x)/2.0;\n}\n\n/*\n \n ll area(Polygon P){\n ll sum=0;\n for(int i=0;i<si(P);i++){\n sum+=cross(P[i],P[(i+1)%si(P)]);\n }\n return abs(sum);\n }\n \n */\n\n// 倍\n\ndouble area(Polygon &P){\n if(si(P)==0) return 0.0;\n double res=0;\n Point c={0.0,0.0};\n for(int i=0;i<si(P);i++){\n c=c+P[i];\n }\n c=c/si(P);\n \n for(int i=0;i<si(P);i++){\n res+=area(c,P[i],P[(i+1)%si(P)]);\n }\n \n return res;\n}\n\nll gcd(ll a,ll b){\n if(b==0) return a;\n return gcd(b,a%b);\n}\n\npair<Point,Vector> perpendicular_bisector(Point a,Point b){\n Point c=(a+b)/2;\n Vector v=b-c;\n swap(v.x,v.y);\n v.x*=-1;\n \n Point p=c;\n if(v.x==0){\n v.y=1;\n p.y=0;\n }\n else if(v.y==0){\n v.x=1;\n p.x=0;\n }\n else{\n if(v.x<0){\n v.x*=-1;\n v.y*=-1;\n }\n ll g=gcd(abs(ll(v.x)),abs(ll(v.y)));\n v.x/=g;\n v.y/=g;\n if(p.x>=0){\n ll d=p.x/v.x;\n p=p-v*d;\n }else{\n ll d=abs(p.x)/v.x;\n p=p+v*d;\n \n if(p.x<0){\n p=p+v;\n }\n }\n }\n \n return mp(p,v);\n}\n//2倍するなりして整数にしておくこと\n\npair<int,double> dp[1<<10][705];\n\nvector<pair<int,int>> G[705];\n\nint main(){\n \n std::ifstream in(\"text.txt\");\n std::cin.rdbuf(in.rdbuf());\n cin.tie(0);\n ios::sync_with_stdio(false);\n \n while(1){\n int N;cin>>N;\n if(N==0) break;\n vector<Point> P(N);\n for(int i=0;i<N;i++) cin>>P[i].x>>P[i].y;\n \n vector<Point> use=P;\n \n for(int a=0;a<N;a++){\n for(int b=a+1;b<N;b++){\n for(int c=0;c<N;c++){\n for(int d=c+1;d<N;d++){\n if(isParallel(P[a],P[b],P[c],P[d])) continue;\n auto p=getCrossPointL({P[a],P[b]},{P[c],P[d]});\n use.push_back(p);\n }\n }\n }\n }\n \n sort(all(use));\n use.erase(unique(all(use)),use.end());\n \n int M=si(use);\n \n for(int bit=0;bit<(1<<N);bit++){\n for(int i=0;i<M;i++){\n dp[bit][i]=mp(INF,INF);\n }\n }\n for(int i=0;i<M;i++) G[i].clear();\n \n vector<int> idP(N),dic(M,-1);\n \n for(int i=0;i<N;i++){\n for(int j=0;j<M;j++){\n if(P[i]==use[j]){\n idP[i]=j;\n dic[j]=i;\n }\n }\n }\n \n for(int i=0;i<M;i++){\n for(int j=i+1;j<M;j++){\n int S=0;\n for(int k=0;k<N;k++){\n if((ccw(use[i],use[j],P[k]))==0) S|=(1<<k);\n }\n if(__builtin_popcount(S)>=2){\n G[i].push_back(mp(j,S));\n G[j].push_back(mp(i,S));\n }\n }\n }\n \n for(int i=0;i<N;i++){\n dp[(1<<i)][idP[i]]=mp(0,0);\n }\n \n for(int bit=0;bit<(1<<N);bit++){\n for(int i=0;i<M;i++){\n if(dp[bit][i].fi==INF) continue;\n for(auto [j,mask]:G[i]){\n chmin(dp[bit|mask][j],mp(dp[bit][i].fi+1,dp[bit][i].se+getDistance(use[i],use[j])));\n }\n }\n }\n \n pair<int,double> ans=mp(INF,INF);\n \n for(int i=0;i<M;i++){\n chmin(ans,dp[(1<<N)-1][i]);\n }\n \n cout<<fixed<<setprecision(25)<<ans.fi-1<<\" \"<<ans.se<<\"\\n\";\n }\n \n}", "accuracy": 1, "time_ms": 210, "memory_kb": 14996, "score_of_the_acc": -0.2267, "final_rank": 7 }, { "submission_id": "aoj_1294_6144641", "code_snippet": "#pragma GCC optimize(\"O3\")\n#pragma GCC optimize(\"unroll-loops\")\n#include<iostream>\n#include<string>\n#include<cstdio>\n#include<vector>\n#include<cmath>\n#include<algorithm>\n#include<functional>\n#include<iomanip>\n#include<queue>\n#include<ciso646>\n#include<random>\n#include<map>\n#include<set>\n#include<bitset>\n#include<stack>\n#include<unordered_map>\n#include<unordered_set>\n#include<utility>\n#include<cassert>\n#include<complex>\n#include<numeric>\n#include<array>\n#include<chrono>\nusing namespace std;\n\n//#define int long long\ntypedef long long ll;\n\ntypedef unsigned long long ul;\ntypedef unsigned int ui;\nconstexpr ll mod = 998244353;\nconst ll INF = mod * mod;\ntypedef pair<int, int>P;\n\n#define rep(i,n) for(int i=0;i<n;i++)\n#define per(i,n) for(int i=n-1;i>=0;i--)\n#define Rep(i,sta,n) for(int i=sta;i<n;i++)\n#define rep1(i,n) for(int i=1;i<=n;i++)\n#define per1(i,n) for(int i=n;i>=1;i--)\n#define Rep1(i,sta,n) for(int i=sta;i<=n;i++)\n#define all(v) (v).begin(),(v).end()\ntypedef pair<ll, ll> LP;\ntypedef double ld;\ntypedef pair<ld, ld> LDP;\nconst ld eps = 1e-8;\nconst ld pi = acosl(-1.0);\n\ntemplate<typename T>\nvoid chmin(T& a, T b) {\n\ta = min(a, b);\n}\ntemplate<typename T>\nvoid chmax(T& a, T b) {\n\ta = max(a, b);\n}\ntemplate<typename T>\nvoid cinarray(vector<T>& v) {\n\trep(i, v.size())cin >> v[i];\n}\ntemplate<typename T>\nvoid coutarray(vector<T>& v) {\n\trep(i, v.size()) {\n\t\tif (i > 0)cout << \" \"; cout << v[i];\n\t}\n\tcout << \"\\n\";\n}\nll mod_pow(ll x, ll n, ll m = mod) {\n\tif (n < 0) {\n\t\tll res = mod_pow(x, -n, m);\n\t\treturn mod_pow(res, m - 2, m);\n\t}\n\tif (abs(x) >= m)x %= m;\n\tif (x < 0)x += m;\n\tif (x == 0)return 0;\n\tll res = 1;\n\twhile (n) {\n\t\tif (n & 1)res = res * x % m;\n\t\tx = x * x % m; n >>= 1;\n\t}\n\treturn res;\n}\nstruct modint {\n\tint n;\n\tmodint() :n(0) { ; }\n\tmodint(ll m) {\n\t\tif (m < 0 || mod <= m) {\n\t\t\tm %= mod; if (m < 0)m += mod;\n\t\t}\n\t\tn = m;\n\t}\n\toperator int() { return n; }\n};\nbool operator==(modint a, modint b) { return a.n == b.n; }\nmodint operator+=(modint& a, modint b) { a.n += b.n; if (a.n >= mod)a.n -= mod; return a; }\nmodint operator-=(modint& a, modint b) { a.n -= b.n; if (a.n < 0)a.n += mod; return a; }\nmodint operator*=(modint& a, modint b) { a.n = ((ll)a.n * b.n) % mod; return a; }\nmodint operator+(modint a, modint b) { return a += b; }\nmodint operator-(modint a, modint b) { return a -= b; }\nmodint operator*(modint a, modint b) { return a *= b; }\nmodint operator^(modint a, ll n) {\n\tif (n == 0)return modint(1);\n\tmodint res = (a * a) ^ (n / 2);\n\tif (n % 2)res = res * a;\n\treturn res;\n}\n\nll inv(ll a, ll p) {\n\treturn (a == 1 ? 1 : (1 - p * inv(p % a, a)) / a + p);\n}\nmodint operator/(modint a, modint b) { return a * modint(inv(b, mod)); }\nmodint operator/=(modint& a, modint b) { a = a / b; return a; }\nconst int max_n = 1 << 20;\nmodint fact[max_n], factinv[max_n];\nvoid init_f() {\n\tfact[0] = modint(1);\n\tfor (int i = 0; i < max_n - 1; i++) {\n\t\tfact[i + 1] = fact[i] * modint(i + 1);\n\t}\n\tfactinv[max_n - 1] = modint(1) / fact[max_n - 1];\n\tfor (int i = max_n - 2; i >= 0; i--) {\n\t\tfactinv[i] = factinv[i + 1] * modint(i + 1);\n\t}\n}\nmodint comb(int a, int b) {\n\tif (a < 0 || b < 0 || a < b)return 0;\n\treturn fact[a] * factinv[b] * factinv[a - b];\n}\nmodint combP(int a, int b) {\n\tif (a < 0 || b < 0 || a < b)return 0;\n\treturn fact[a] * factinv[a - b];\n}\n\nll gcd(ll a, ll b) {\n\ta = abs(a); b = abs(b);\n\tif (a < b)swap(a, b);\n\twhile (b) {\n\t\tll r = a % b; a = b; b = r;\n\t}\n\treturn a;\n}\n\nint dx[4] = { 1,0,-1,0 };\nint dy[4] = { 0,1,0,-1 };\n\ntypedef complex<ld> Point;\nld dot(Point a, Point b) { return real(conj(a) * b); }\nld cross(Point a, Point b) { return imag(conj(a) * b); }\nnamespace std {\n\tbool operator<(const Point& lhs, const Point& rhs) {\n\t\treturn lhs.real() == rhs.real() ? lhs.imag() < rhs.imag() : lhs.real() < rhs.real();\n\t}\n}\nstruct Line {\n\tPoint a, b;\n};\nstruct Circle {\n\tPoint p; ld r;\n};\nint ccw(Point a, Point b, Point c) {\n\tb -= a; c -= a;\n\tif (cross(b, c) > eps)return 1;//counter clockwise\n\tif (cross(b, c) < -eps)return -1;//clock wise\n\tif (dot(b, c) < 0)return 2;//c--a--b on line\n\tif (norm(b) < norm(c))return -2;//a--b--c on line\n\treturn 0; //a--c--b on line\n}\nbool eq(ld a, ld b) {\n\treturn abs(a - b) < eps;\n}\n//2直線の交差判定\nbool isis_ll(Line l, Line m) {\n\treturn !eq(cross(l.b - l.a, m.b - m.a), 0);\n}\n//直線と線分の交差判定\nbool isis_ls(Line l, Line s) {\n\treturn (cross(l.b - l.a, s.a - l.a) * cross(l.b - l.a, s.b - l.a) < eps);\n}\n//点が直線上に存在するか\nbool isis_lp(Line l, Point p) {\n\treturn (abs(cross(l.b - p, l.a - p)) < eps);\n}\n//点が線分上に存在するか\nbool isis_sp(Line s, Point p) {\n\t//誤差がisis_lpに比べて大きいので、できるだけisis_lpを使う\n\treturn (abs(s.a - p) + abs(s.b - p) - abs(s.b - s.a) < eps);\n}\n//線分と線分の交差判定\n//bool isis_ss(Line s, Line t) {\n//\treturn(cross(s.b - s.a, t.a - s.a)*cross(s.b - s.a, t.b - s.a) < -eps && cross(t.b - t.a, s.a - t.a)*cross(t.b - t.a, s.b - t.a) < -eps);\n//}\n//線分と線分の交差判定2\nbool isis_ss(Line s, Line t) {\n\treturn 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;\n}\n//点から直線への垂線の足\nPoint proj(Line l, Point p) {\n\tld t = dot(p - l.a, l.a - l.b) / norm(l.a - l.b);\n\treturn l.a + t * (l.a - l.b);\n}\n//直線と直線の交点\n//平行な2直線に対しては使うな!!!!\nPoint is_ll(Line s, Line t) {\n\tPoint sv = s.b - s.a; Point tv = t.b - t.a;\n\treturn s.a + sv * cross(tv, t.a - s.a) / cross(tv, sv);\n}\nbool sameline(Line s, Line t) {\n\treturn abs(cross(s.b - s.a,t.b-t.a)) < eps && abs(cross(s.b-s.a,t.a-s.a)) < eps;\n}\n\nld dp[1 << 10][1 << 11];\nld cop[1 << 10][1 << 11];\n\nstruct edge {\n\tint to,id; ld cost;\n};\nint n;\nvoid solve() {\n\tvector<int> x(n), y(n);\n\trep(i, n)cin >> x[i] >> y[i];\n\tvector<Point> p(n);\n\trep(i, n)p[i] = { (ld)x[i],(ld)y[i] };\n\tvector<Line> vl;\n\trep(i, n)Rep(j, i + 1, n) {\n\t\tLine l = { p[i],p[j] };\n\t\tbool exi = false;\n\t\trep(j, vl.size())if (sameline(vl[j],l))exi = true;\n\t\tif (!exi)vl.push_back(l);\n\t}\n\tvector<Point> vcp;\n\trep(i, n) {\n\t\tvcp.push_back(p[i]);\n\t}\n\trep(i, vl.size())Rep(j, i + 1, vl.size()) {\n\t\tif (isis_ll(vl[i], vl[j])) {\n\t\t\tvcp.push_back(is_ll(vl[i], vl[j]));\n\t\t}\n\t}\n\tvector<vector<edge>> G(vcp.size());\n\t//vector<vector<ld>> dist(vcp.size(), vector<ld>(vcp.size()));\n\t//vector<vector<int>> ids(vcp.size(), vector<int>(vcp.size()));\n\trep(i, vcp.size())rep(j, vcp.size()) {\n\t\tLine s = { vcp[i],vcp[j] };\n\t\tint cnt = 0;\n\t\tint cid = 0;\n\t\trep(k, n) {\n\t\t\tif (isis_sp(s, p[k])) {\n\t\t\t\t//ids[i][j] |= (1 << k);\n\t\t\t\tcnt++;\n\t\t\t\tcid |= (1 << k);\n\t\t\t}\n\t\t}\n\t\tif (cnt >= 2) {\n\t\t\tG[i].push_back({ j,cid,abs(vcp[j] - vcp[i]) });\n\t\t}\n\t\t//if (cnt < 2)dist[i][j] = INF;\n\t\t//else dist[i][j] = abs(vcp[j] - vcp[i]);\n\t}\n\trep(i, (1 << n))rep(j, vcp.size()) {\n\t\tdp[i][j] = mod;\n\t}\n\trep(j, n)dp[0][j] = 0;\n\tfor (int c = 0;; c++) {\n\t\trep(i, (1 << n))rep(j, vcp.size())cop[i][j] = mod;\n\t\trep(i, (1 << n))rep(j, vcp.size()) {\n\t\t\tif (dp[i][j] == mod)continue;\n\t\t\tfor (edge e : G[j]) {\n\t\t\t\tint ni = i | e.id;\n\t\t\t\tint nj = e.to;\n\t\t\t\tchmin(cop[ni][nj], dp[i][j] + e.cost);\n\t\t\t}\n\t\t\t/*rep(k, vcp.size()) {\n\t\t\t\tint ni = i | ids[j][k];\n\t\t\t\tint nj = k;\n\t\t\t\tchmin(cop[ni][nj], dp[i][j] + dist[j][k]);\n\t\t\t}*/\n\t\t}\n\t\tld ans = mod;\n\t\trep(i, (1 << n))rep(j, vcp.size()) {\n\t\t\tdp[i][j] = cop[i][j];\n\t\t\tif (i == (1 << n) - 1)chmin(ans, dp[i][j]);\n\t\t}\n\t\t//cout << dp[785][8] << \"\\n\";\n\t\tif (ans < mod) {\n\t\t\tcout << c << \" \" << ans << \"\\n\";\n\t\t\tbreak;\n\t\t}\n\t}\n}\n\n\nsigned main() {\n\tios::sync_with_stdio(false);\n\tcin.tie(0);\n\tcout << fixed << setprecision(8);\n\t//init_f();\n\t//init();\n\t//while(true)\n\t//expr();\n\t//init();\n\t//int t; cin >> t; rep(i, t)\n\twhile (cin >> n, n) {\n\t\tsolve();\n\t}\n\treturn 0;\n}", "accuracy": 1, "time_ms": 3280, "memory_kb": 46648, "score_of_the_acc": -1.2162, "final_rank": 19 }, { "submission_id": "aoj_1294_5919778", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nconst double eps = 0.000001;\nconst double INF = 10000;\nstruct point{\n double x, y;\n point(){\n }\n point(double x, double y): x(x), y(y){\n }\n point operator +(point P){\n return point(x + P.x, y + P.y);\n }\n point operator -(point P){\n return point(x - P.x, y - P.y);\n }\n point operator *(double k){\n return point(x * k, y * k);\n }\n point operator /(double k){\n return point(x / k, y / k);\n }\n};\ndouble abs(point P){\n return sqrt(P.x * P.x + P.y * P.y);\n}\ndouble dist(point P, point Q){\n return abs(Q - P);\n}\ndouble dot(point P, point Q){\n return P.x * Q.x + P.y * Q.y;\n}\ndouble cross(point P, point Q){\n return P.x * Q.y - P.y * Q.x;\n}\nstruct line{\n point A, B;\n line(){\n }\n line(point A, point B): A(A), B(B){\n }\n};\npoint vec(line L){\n return L.B - L.A;\n}\nbool point_on_line(point P, line L){\n return abs(cross(P - L.A, vec(L))) < eps;\n}\ndouble pos(point P, line L){\n return dot(P - L.A, vec(L));\n}\nbool is_parallel(line L1, line L2){\n return abs(cross(vec(L1), vec(L2))) < eps;\n}\npoint line_intersection(line L1, line L2){\n return L1.A + vec(L1) * cross(L2.A - L1.A, vec(L2)) / cross(vec(L1), vec(L2));\n}\nint main(){\n cout << fixed << setprecision(20);\n while (true){\n int n;\n cin >> n;\n if (n == 0){\n break;\n }\n vector<point> P(n);\n for (int i = 0; i < n; i++){\n cin >> P[i].x >> P[i].y;\n }\n vector<line> L;\n vector<int> a, b;\n int m = 0;\n for (int i = 0; i < n; i++){\n for (int j = i + 1; j < n; j++){\n bool ok = true;\n for (int k = 0; k < m; k++){\n if (point_on_line(P[i], L[k]) && point_on_line(P[j], L[k])){\n ok = false;\n }\n }\n if (ok){\n a.push_back(i);\n b.push_back(j);\n L.push_back(line(P[i], P[j]));\n m++;\n }\n }\n }\n int cnt = 0;\n vector<point> Q;\n for (int i = 0; i < m; i++){\n for (int j = i + 1; j < m; j++){\n if (!is_parallel(L[i], L[j])){\n point X = line_intersection(L[i], L[j]);\n bool ok = true;\n for (int k = 0; k < cnt; k++){\n if (dist(Q[k], X) < eps){\n ok = false;\n }\n }\n if (ok){\n Q.push_back(X);\n cnt++;\n }\n }\n }\n }\n for (int i = 0; i < n; i++){\n bool ok = true;\n for (int j = 0; j < cnt; j++){\n if (dist(Q[j], P[i]) < eps){\n ok = false;\n }\n }\n if (ok){\n Q.push_back(P[i]);\n cnt++;\n }\n }\n vector<vector<int>> id(m);\n for (int i = 0; i < m; i++){\n for (int j = 0; j < cnt; j++){\n if (point_on_line(Q[j], L[i])){\n id[i].push_back(j);\n }\n }\n }\n vector<int> p(cnt, -1);\n for (int i = 0; i < n; i++){\n for (int j = 0; j < cnt; j++){\n if (dist(Q[j], P[i]) < eps){\n p[j] = i;\n }\n }\n }\n vector<vector<tuple<double, int, int>>> E(cnt);\n for (int i = 0; i < m; i++){\n int cnt2 = id[i].size();\n vector<pair<double, int>> S(cnt2);\n for (int j = 0; j < cnt2; j++){\n S[j] = make_pair(pos(Q[id[i][j]], L[i]), id[i][j]);\n }\n sort(S.begin(), S.end());\n for (int j = 0; j < cnt2; j++){\n for (int k = j + 1; k < cnt2; k++){\n int sum = 0;\n for (int l = j; l <= k; l++){\n if (p[S[l].second] != -1){\n sum |= 1 << p[S[l].second];\n }\n }\n if (__builtin_popcount(sum) >= 2){\n int v = S[j].second;\n int w = S[k].second;\n E[v].push_back(make_tuple(dist(Q[v], Q[w]), sum, w));\n E[w].push_back(make_tuple(dist(Q[v], Q[w]), sum, v));\n }\n }\n }\n }\n vector<vector<vector<double>>> dp(6, vector<vector<double>>(cnt, vector<double>(1 << n, INF)));\n for (int i = 0; i < cnt; i++){\n dp[0][i][0] = 0;\n if (p[i] != -1){\n dp[0][i][1 << p[i]] = 0;\n }\n }\n for (int i = 0; i < 5; i++){\n for (int j = 0; j < cnt; j++){\n for (auto e : E[j]){\n double d = get<0>(e);\n int s = get<1>(e);\n int w = get<2>(e);\n for (int k = 0; k < (1 << n); k++){\n dp[i + 1][w][k | s] = min(dp[i + 1][w][k | s], dp[i][j][k] + d);\n }\n }\n }\n }\n int ans1 = 0;\n double ans2 = INF;\n for (int i = 1; i < 6; i++){\n bool ok = false;\n for (int j = 0; j < cnt; j++){\n if (dp[i][j][(1 << n) - 1] != INF){\n ans1 = i - 1;\n ans2 = min(ans2, dp[i][j][(1 << n) - 1]);\n ok = true;\n }\n }\n if (ok){\n break;\n }\n }\n cout << ans1 << ' ' << ans2 << endl;\n }\n}", "accuracy": 1, "time_ms": 380, "memory_kb": 31476, "score_of_the_acc": -0.5585, "final_rank": 12 }, { "submission_id": "aoj_1294_5035143", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\nconst int N=10;\n\nconst double EPS=1e-8;\n\nstruct Point { double x,y; };\nstruct Line { double a,b,c; };\n\ndouble dist(Point a, Point b) {\n return sqrt((b.x-a.x)*(b.x-a.x)+(b.y-a.y)*(b.y-a.y));\n}\n\ndouble dot(Point p, Point a, Point b) {\n return (a.x-p.x)*(b.x-p.x)+(a.y-p.y)*(b.y-p.y);\n}\n\ndouble cross(Point p, Point a, Point b) {\n return (a.x-p.x)*(b.y-p.y)-(a.y-p.y)*(b.x-p.x);\n}\n\nLine makeline(Point a, Point b) {\n Line l;\n l.a=b.y-a.y;\n l.b=-(b.x-a.x);\n l.c=(b.x-a.x)*a.y-(b.y-a.y)*a.x;\n return l;\n}\n\nint intersect(Point a, Point b, Point c, Point d, Point &r) {\n Line l1=makeline(a,b),l2=makeline(c,d);\n double dd=l1.a*l2.b-l2.a*l1.b;\n if (fabs(dd)<EPS) return 0;\n r.x=(l1.b*l2.c-l2.b*l1.c)/dd;\n r.y=(l1.c*l2.a-l2.c*l1.a)/dd;\n return 1;\n}\n\nint n,z[1<<N][N][N];\ndouble len[1<<N][N][N];\nPoint p[N];\n\nint has(int mask, int i) { return (mask&(1<<i))>0; }\n\nvoid savebetter(int &z1, double &len1, int z2, double len2) {\n if (z2==-1) return;\n if (z1==-1 || z1>z2 || (z1==z2 && len1>len2)) {\n z1=z2; len1=len2;\n }\n}\n\nint turn(Point a, Point b, Point c) {\n return !(fabs(cross(a,b,c))<EPS && dot(a,b,c)>0 && dot(b,a,c)<0);\n}\n\nint solve() {\n // int i,j,k,mask,a,b,m,zz;\n // double zl;\n // Point r;\n\n cin>>n;\n if (n==0) return 0;\n for (int i=0; i<n; i++) cin>>p[i].x>>p[i].y;\n\n // DP: z[mask][i][j] is the min zigzag turns to connect a set of\n // points, and the tail of the line is point i, then j.\n memset(z,-1,sizeof(z));\n for (int i=0; i<n; i++)\n for (int j=0; j<n; j++)\n if (i!=j) {\n int m=(1<<i)+(1<<j);\n savebetter(z[m][i][j],len[m][i][j],0,dist(p[i],p[j]));\n }\n for (int mask=0; mask<(1<<n); mask++)\n for (int i=0; i<n; i++)\n for (int j=0; j<n; j++)\n if (i!=j && has(mask,i) && has(mask,j)) {\n if (z[mask][i][j]==-1) continue;\n\n // direct to one point: i==j--a\n for (int a=0; a<n; a++)\n if (!has(mask,a) && a!=i && a!=j) {\n int m=mask+(1<<a);\n int zz=z[mask][i][j]+turn(p[i],p[j],p[a]);\n double zl=len[mask][i][j]+dist(p[j],p[a]);\n savebetter(z[m][j][a],len[m][j][a],zz,zl);\n }\n\n // zigzag to two points: i==j--a--b\n for (int a=0; a<n; a++)\n if (!has(mask,a) && a!=i && a!=j)\n for (int b=0;b<n;b++)\n if (!has(mask,b) && a!=b && b!=i && b!=j) {\n Point r;\n if (intersect(p[i],p[j],p[b],p[a],r) && !turn(r,p[j],p[i]) && !turn(r,p[a],p[b])) {\n int m=mask+(1<<a)+(1<<b);\n int zz=z[mask][i][j]+1;\n double zl=len[mask][i][j]+dist(r,p[j])+dist(r,p[b]);\n savebetter(z[m][a][b],len[m][a][b],zz,zl);\n }\n }\n }\n\n int zz=-1;\n double zl=-1;\n int mask=(1<<n)-1;\n for (int i=0; i<n; i++)\n for (int j=0; j<n; j++)\n if (i!=j) savebetter(zz,zl,z[mask][i][j],len[mask][i][j]); \n\n cout<<zz<<' '<<fixed<<setprecision(5)<<zl<<'\\n';\n return 1;\n}\n\nint main() {\n while (solve());\n return 0;\n}", "accuracy": 1, "time_ms": 220, "memory_kb": 4672, "score_of_the_acc": -0.0338, "final_rank": 1 }, { "submission_id": "aoj_1294_5035136", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\nconst int N=10;\n\nconst double EPS=1e-8;\n\nstruct Point { double x,y; };\nstruct Line { double a,b,c; };\n\nint n,b[N],z[N],zmin;\ndouble len[N],lmin;\nPoint p[N],s[N];\n\ndouble dist(Point a, Point b) {\n return sqrt((b.x-a.x)*(b.x-a.x)+(b.y-a.y)*(b.y-a.y));\n}\n\ndouble dot(Point p, Point a, Point b) {\n return (a.x-p.x)*(b.x-p.x)+(a.y-p.y)*(b.y-p.y);\n}\n\ndouble cross(Point p, Point a, Point b) {\n return (a.x-p.x)*(b.y-p.y)-(a.y-p.y)*(b.x-p.x);\n}\n\nLine makeline(Point a, Point b) {\n Line l;\n l.a=b.y-a.y;\n l.b=-(b.x-a.x);\n l.c=(b.x-a.x)*a.y-(b.y-a.y)*a.x;\n return l;\n}\n\nint intersect(Point a, Point b, Point c, Point d, Point &r) {\n Line l1=makeline(a,b), l2=makeline(c,d);\n double dd=l1.a*l2.b-l2.a*l1.b;\n if (fabs(dd)<EPS) return 0;\n r.x=(l1.b*l2.c-l2.b*l1.c)/dd;\n r.y=(l1.c*l2.a-l2.c*l1.a)/dd;\n return 1;\n}\n\nint turn(Point a, Point b, Point c) {\n return !(fabs(cross(a,b,c))<EPS && dot(a,b,c)>0 && dot(b,a,c)<0);\n}\n\nvoid search(int k) {\n if (k>0 && !(z[k-1]<zmin || (z[k-1]==zmin && len[k-1]<lmin))) return;\n\n if (k==n) {\n zmin=z[n-1]; lmin=len[n-1];\n return;\n }\n\n if (k>1) {\n for (int i=0; i<n; i++)\n for (int j=0; j<n; j++)\n if (!b[i]&&!b[j]&&i!=j) {\n b[i]=b[j]=1;\n s[k]=p[i]; s[k+1]=p[j];\n Point r;\n if (intersect(s[k-2],s[k-1],s[k+1],s[k],r)) {\n if (!turn(r,s[k-1],s[k-2]) && !turn(r,s[k],s[k+1])) {\n z[k+1]=z[k-1]+1;\n len[k+1]=len[k-1]+dist(r,s[k-1])+dist(r,s[k+1]);\n search(k+2);\n }\n }\n b[i]=b[j]=0;\n }\n }\n\n for (int i=0; i<n; i++)\n if (!b[i]) {\n b[i]=1;\n s[k]=p[i];\n z[k]=(k>1 ? z[k-1]+turn(s[k-2],s[k-1],s[k]) : 0);\n len[k]=(k>0 ? len[k-1]+dist(s[k-1],s[k]) : 0);\n search(k+1);\n b[i]=0;\n }\n}\n\nint solve() {\n cin>>n;\n if (n==0) return 0;\n for (int i=0; i<n; i++) cin>>p[i].x>>p[i].y;\n\n zmin=(1<<30);\n lmin=1e30;\n memset(b,0,sizeof(b));\n search(0);\n\n cout<<zmin<<' '<<fixed<<setprecision(5)<<lmin<<'\\n';\n return 1;\n}\n\nint main() {\n while (solve());\n return 0;\n}", "accuracy": 1, "time_ms": 2360, "memory_kb": 3304, "score_of_the_acc": -0.2828, "final_rank": 8 }, { "submission_id": "aoj_1294_5027208", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\nconst int N=10;\n\nconst double EPS=1e-8;\n\nstruct Point { double x,y; };\nstruct Line { double a,b,c; };\n\nint n,b[N],z[N],zmin;\ndouble len[N],lmin;\nPoint p[N],s[N];\n\ndouble dist(Point a, Point b) {\n return sqrt((b.x-a.x)*(b.x-a.x)+(b.y-a.y)*(b.y-a.y));\n}\n\ndouble dot(Point p, Point a, Point b) {\n return (a.x-p.x)*(b.x-p.x)+(a.y-p.y)*(b.y-p.y);\n}\n\ndouble cross(Point p, Point a, Point b) {\n return (a.x-p.x)*(b.y-p.y)-(a.y-p.y)*(b.x-p.x);\n}\n\nLine makeline(Point a, Point b) {\n Line l;\n l.a=b.y-a.y;\n l.b=-(b.x-a.x);\n l.c=(b.x-a.x)*a.y-(b.y-a.y)*a.x;\n return l;\n}\n\nint intersect(Point a, Point b, Point c, Point d, Point &r) {\n Line l1=makeline(a,b),l2=makeline(c,d);\n double dd=l1.a*l2.b-l2.a*l1.b;\n if (fabs(dd)<EPS) return 0;\n r.x=(l1.b*l2.c-l2.b*l1.c)/dd;\n r.y=(l1.c*l2.a-l2.c*l1.a)/dd;\n return 1;\n}\n\nint turn(Point a, Point b, Point c) {\n return !(fabs(cross(a,b,c))<EPS && dot(a,b,c)>0 && dot(b,a,c)<0);\n}\n\nvoid search(int k) {\n int i,j,tr;\n Point r;\n\n if (k>0 && !(z[k-1]<zmin || (z[k-1]==zmin && len[k-1]<lmin))) return;\n\n if (k==n) {\n zmin=z[n-1]; lmin=len[n-1];\n return;\n }\n\n if (k>1) {\n for (i=0;i<n;i++) for (j=0;j<n;j++) if (!b[i]&&!b[j]&&i!=j) {\n b[i]=b[j]=1;\n s[k]=p[i]; s[k+1]=p[j];\n if (intersect(s[k-2],s[k-1],s[k+1],s[k],r)) {\n if (!turn(r,s[k-1],s[k-2]) && !turn(r,s[k],s[k+1])) {\n z[k+1]=z[k-1]+1;\n len[k+1]=len[k-1]+dist(r,s[k-1])+dist(r,s[k+1]);\n search(k+2);\n }\n }\n b[i]=b[j]=0;\n }\n }\n\n for (i=0;i<n;i++) if (!b[i]) {\n b[i]=1;\n s[k]=p[i];\n z[k]=(k>1 ? z[k-1]+turn(s[k-2],s[k-1],s[k]) : 0);\n len[k]=(k>0 ? len[k-1]+dist(s[k-1],s[k]) : 0);\n search(k+1);\n b[i]=0;\n }\n}\n\nint solve() {\n int i;\n\n cin>>n;\n if (n==0) return 0;\n for (i=0;i<n;i++) cin>>p[i].x>>p[i].y;\n\n zmin=(1<<30); lmin=1e30;\n memset(b,0,sizeof(b));\n search(0);\n\n cout<<zmin<<' '<<fixed<<setprecision(5)<<lmin<<endl;\n return 1;\n}\n\nint main() {\n while (solve());\n return 0;\n}", "accuracy": 1, "time_ms": 2370, "memory_kb": 3572, "score_of_the_acc": -0.2891, "final_rank": 9 }, { "submission_id": "aoj_1294_4299949", "code_snippet": "#include<bits/stdc++.h>\ntypedef long long int ll;\ntypedef unsigned long long int ull;\n#define BIG_NUM 2000000000\n#define HUGE_NUM 99999999999999999\n#define MOD 1000000007\n//#define EPS 0.000000001\nusing namespace std;\n\n\n#define EPS 0.00000001\n\n#define SIZE 15\n\nstruct Point{\n\tPoint(double arg_x,double arg_y){\n\t\tx = arg_x;\n\t\ty = arg_y;\n\t}\n\n\tPoint(){\n\t\tx = y = 0.0;\n\t}\n\n\tPoint operator + (Point p){ return Point(x+p.x,y+p.y); }\n\tPoint operator - (Point p){ return Point(x-p.x,y-p.y);}\n\tPoint operator * (double a){ return Point(a*x,a*y); }\n\tPoint operator / (double a){ return Point(x/a,y/a); }\n\n\tdouble abs(){ return sqrt(norm()); }\n\tdouble norm(){ return x*x + y*y; }\n\n\tbool operator<(const Point &p) const{\n\t\treturn x != p.x? x < p.x: y < p.y;\n\t}\n\n\tbool operator == (const Point &p) const{\n\t\treturn fabs(x-p.x) < EPS && fabs(y-p.y) < EPS;\n\t}\n\n\tdouble x,y;\n};\n\ntypedef Point Vector;\n\nstruct Line{\n\tLine(){\n\n\t}\n\tLine(Point a,Point b){\n\t\tp[0] = a;\n\t\tp[1] = b;\n\t}\n\tPoint p[2];\n};\n\nint N;\nint POW[SIZE];\nbool FLG;\ndouble NUM = 100;\ndouble dp[5][1 << 10][10][10],ans;\nPoint point[SIZE];\n\n\ndouble calc_dist(Point A,Point B){\n\n\treturn sqrt((A.x-B.x)*(A.x-B.x)+(A.y-B.y)*(A.y-B.y));\n}\n\n\nPoint calc_minus(Point a,Point b){\n Point ret;\n\n ret.x = a.x-b.x;\n ret.y = a.y-b.y;\n\n return ret;\n}\n\ndouble calc_len(Vector a){\n return sqrt(a.x*a.x+a.y*a.y);\n}\n\ndouble calc_slope(Line A){\n\n\tif(fabs(A.p[0].x-A.p[1].x) < EPS){\n\n\t\treturn DBL_MAX;\n\n\t}else if(fabs(A.p[0].y-A.p[1].y) < EPS){\n\n\t\treturn 0;\n\n\t}else{\n\n\t\treturn (A.p[1].y-A.p[0].y)/(A.p[1].x-A.p[0].x);\n\t}\n}\n\ndouble norm(Vector a){\n\treturn a.x*a.x+a.y*a.y;\n}\n\ndouble abs(Vector a){\n\treturn sqrt(norm(a));\n}\n\ndouble cross(Vector a,Vector b){\n return a.x*b.y-a.y*b.x;\n}\n\ndouble dot(Vector a,Vector b){\n return a.x*b.x + a.y*b.y;\n}\n\nint func(double x1,double y1,double x2, double y2, double xp, double yp){\n\tdouble naiseki,norm1,norm2,gaiseki;\n\tnorm1 = sqrt((x2-x1)*(x2-x1)+(y2-y1)*(y2-y1));\n\tnorm2 = sqrt((xp-x1)*(xp-x1)+(yp-y1)*(yp-y1));\n\tnaiseki = (xp-x1)*(x2-x1)+(yp-y1)*(y2-y1);\n\tgaiseki = (x2-x1)*(yp-y1)-(xp-x1)*(y2-y1);\n\tif(gaiseki > EPS){\n\t\treturn 1;\n\t}else if(gaiseki < -EPS){\n\t\treturn -1;\n\t}\n\tif(naiseki < -EPS){\n\t\treturn 2;\n\t}\n\n\tif(norm1 < norm2){\n\t\treturn -2;\n\t}\n\treturn 0;\n}\n\n\n//★★直線ではなく、線分の交差判定★★\nbool is_Cross(Line a,Line b){\n\n\tif(func(a.p[0].x,a.p[0].y,a.p[1].x,a.p[1].y,b.p[0].x,b.p[0].y)*\n\t\t\tfunc(a.p[0].x,a.p[0].y,a.p[1].x,a.p[1].y,b.p[1].x,b.p[1].y) <= 0 &&\n\t\t\tfunc(b.p[0].x,b.p[0].y,b.p[1].x,b.p[1].y,a.p[0].x,a.p[0].y)*\n\t\t\tfunc(b.p[0].x,b.p[0].y,b.p[1].x,b.p[1].y,a.p[1].x,a.p[1].y) <= 0){\n\t\treturn true;\n\t}\n\treturn false;\n}\n\n//線分ではなく直線と点の距離\ndouble getDistanceLP(Line l,Point p){\n return fabs(cross(calc_minus(l.p[1],l.p[0]),calc_minus(p,l.p[0]))/calc_len(calc_minus(l.p[1],l.p[0])));\n}\n\n//点と線分の距離\ndouble getDistanceSP(Line l,Point p){\n\tif(dot(calc_minus(l.p[1],l.p[0]),calc_minus(p,l.p[0])) < 0.0)return calc_len(calc_minus(p,l.p[0]));\n\tif(dot(calc_minus(l.p[0],l.p[1]),calc_minus(p,l.p[1])) < 0.0)return calc_len(calc_minus(p,l.p[1]));\n\treturn getDistanceLP(l,p);\n}\n\n//線分と線分の距離\ndouble getDistance(Line A,Line B){\n\tif(is_Cross(A,B))return 0.0;\n\treturn min(min(getDistanceSP(A,B.p[0]),getDistanceSP(A,B.p[1])),\n\t\t\tmin(getDistanceSP(B,A.p[0]),getDistanceSP(B,A.p[1])));\n}\n\n\n//交点を求める関数\nPoint calc_Cross_Point(double x1,double x2,double x3,double x4,double y1,double y2,double y3,double y4){\n\tPoint ret;\n\tret.x = ((x2-x1)*(y3*(x4-x3)+x3*(y3-y4))-(x4-x3)*(y1*(x2-x1)+x1*(y1-y2)))/((y2-y1)*(x4-x3)-(y4-y3)*(x2-x1));\n\tif(x1 != x2){\n\t\tret.y = ((y2-y1)*ret.x+y1*(x2-x1)+x1*(y1-y2))/(x2-x1);\n\t}else{\n\t\tret.y = ((y4-y3)*ret.x+y3*(x4-x3)+x3*(y3-y4))/(x4-x3);\n\t}\n\treturn ret;\n}\n\n//インタフェース関数\nPoint calc_Cross_Point(Point a,Point b,Point c,Point d){\n\treturn calc_Cross_Point(a.x,b.x,c.x,d.x,a.y,b.y,c.y,d.y);\n}\n\nPoint calc_Cross_Point(Line A,Line B){\n\n\tif(getDistanceSP(B,A.p[0]) < EPS){\n\n\t\treturn A.p[0];\n\n\t}else if(getDistanceSP(B,A.p[1]) < EPS){\n\n\t\treturn A.p[1];\n\n\t}else if(getDistanceSP(A,B.p[0]) < EPS){\n\n\t\treturn B.p[0];\n\n\t}else if(getDistanceSP(A,B.p[1]) < EPS){\n\n\t\treturn B.p[1];\n\t}\n\n\treturn calc_Cross_Point(A.p[0],A.p[1],B.p[0],B.p[1]);\n}\n\n\nvoid recursive(int state,int pre_pre,int pre,int num_turn,int max_turn,double sum_dist){\n\n\tif(sum_dist >= ans)return;\n\n\tif(state == POW[N]-1){\n\n\t\tans = min(ans,sum_dist);\n\n\t\tFLG = true;\n\t\treturn;\n\t}\n\tif(sum_dist > dp[num_turn][state][pre_pre][pre]+EPS){\n\n\t\treturn;\n\t}\n\n\tint next_state;\n\tdouble next_dist;\n\n\tLine from,to,PRE,NEXT;\n\n\tfrom.p[0] = point[pre_pre];\n\tfrom.p[1] = point[pre];\n\tdouble\ttmp_slope = calc_slope(from),tmp_slope2;\n\tint next_num_turn;\n\n\t//preで降り曲がる\n\tfor(int next = 0; next < N; next++){\n\t\tif(next == pre_pre || next == pre)continue;\n\n\t\tnext_dist = sum_dist+calc_dist(point[pre],point[next]);\n\t\tnext_state = state|POW[next]; //再訪あり\n\n\t\tto.p[0] = point[pre];\n\t\tto.p[1] = point[next];\n\n\t\tnext_num_turn = num_turn+1;\n\t\ttmp_slope2 = calc_slope(to);\n\t\tif(fabs(tmp_slope-tmp_slope2) < EPS){ //傾きが同じ\n\n\t\t\t//引き返しを弾く\n\t\t\tif(fabs(tmp_slope2-DBL_MAX) < EPS){\n\n\t\t\t\tif((point[pre_pre].y > point[pre].y && point[pre].y > point[next].y) ||\n\t\t\t\t\t\t(point[pre_pre].y < point[pre].y && point[pre].y < point[next].y)){\n\n\t\t\t\t\tnext_num_turn -= 1;\n\t\t\t\t}\n\n\t\t\t}else{\n\n\t\t\t\tif((point[pre_pre].x > point[pre].x && point[pre].x > point[next].x) ||\n\t\t\t\t\t\t(point[pre_pre].x < point[pre].x && point[pre].x < point[next].x)){\n\n\t\t\t\t\tnext_num_turn -= 1;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\tif(next_num_turn > max_turn)continue;\n\n\t\tif(dp[next_num_turn][next_state][pre][next] > next_dist+EPS){\n\t\t\tdp[next_num_turn][next_state][pre][next] = next_dist;\n\n\t\t\trecursive(next_state,pre,next,next_num_turn,max_turn,next_dist);\n\t\t}\n\t}\n\n\tif(num_turn == max_turn)return;\n\n\tif(fabs(tmp_slope-DBL_MAX) < EPS){ //垂直\n\n\t\tif(point[pre_pre].y > point[pre].y){\n\n\t\t\tfrom.p[1] = Point(point[pre].x,point[pre].y-NUM);\n\n\t\t}else{\n\n\t\t\tfrom.p[1] = Point(point[pre].x,point[pre].y+NUM);\n\t\t}\n\n\t}else{\n\n\t\tif(point[pre_pre].x < point[pre].x){ //右方に伸ばす\n\n\t\t\tfrom.p[1] = Point(point[pre].x+NUM,point[pre].y+NUM*tmp_slope);\n\n\t\t}else{ //左方に伸ばす\n\n\t\t\tfrom.p[1] = Point(point[pre].x-NUM,point[pre].y+(-NUM)*tmp_slope);\n\t\t}\n\n\t}\n\n\tPRE.p[0] = point[pre_pre];\n\tPRE.p[1] = point[pre];\n\n\tLine line;\n\n\t//新しい2点との交点を求める\n\tfor(int next = 0; next < N; next++){\n\t\tif(next == pre || next == pre_pre)continue;\n\t\tfor(int next_next = 0; next_next < N; next_next++){\n\t\t\tif(next_next == next || next_next == pre || next_next == pre_pre)continue;\n\n\t\t\tto.p[0] = point[next_next];\n\t\t\tto.p[1] = point[next];\n\t\t\ttmp_slope = calc_slope(to);\n\n\t\t\tif(fabs(tmp_slope-DBL_MAX) < EPS){ //垂直\n\n\t\t\t\tif(point[next_next].y > point[next].y){\n\n\t\t\t\t\tto.p[1] = Point(point[next].x,point[next].y-NUM);\n\n\t\t\t\t}else{\n\n\t\t\t\t\tto.p[1] = Point(point[next].x,point[next].y+NUM);\n\t\t\t\t}\n\n\t\t\t}else{\n\n\t\t\t\tif(point[next_next].x < point[next].x){ //右方に伸ばす\n\n\t\t\t\t\tto.p[1] = Point(point[next].x+NUM,point[next].y+NUM*tmp_slope);\n\n\t\t\t\t}else{ //左方に伸ばす\n\n\t\t\t\t\tto.p[1] = Point(point[next].x-NUM,point[next].y+(-NUM)*tmp_slope);\n\t\t\t\t}\n\t\t\t}\n\n\t\t\tNEXT.p[0] = point[next_next];\n\t\t\tNEXT.p[1] = point[next];\n\n\t\t\tPoint cross_point = calc_Cross_Point(from,to);\n\n\t\t\tif(getDistanceSP(PRE,cross_point) < EPS || getDistanceSP(NEXT,cross_point) < EPS){\n\t\t\t\tcontinue;\n\t\t\t}\n\t\t\tif(calc_dist(point[pre_pre],cross_point) < calc_dist(point[pre],cross_point) ||\n\t\t\t\t\tcalc_dist(point[next_next],cross_point) < calc_dist(point[next],cross_point)){\n\t\t\t\tcontinue;\n\t\t\t}\n\t\t\tnext_dist = sum_dist+calc_dist(cross_point,point[pre])+calc_dist(cross_point,point[next]);\n\t\t\tnext_dist += calc_dist(point[next],point[next_next]);\n\n\t\t\tnext_state = state|(POW[next]|POW[next_next]);\n\n\t\t\tif(dp[num_turn+1][next_state][next][next_next] > next_dist+EPS){\n\n\t\t\t\tdp[num_turn+1][next_state][next][next_next] = next_dist;\n\n\t\t\t\trecursive(next_state,next,next_next,num_turn+1,max_turn,next_dist);\n\t\t\t}\n\t\t}\n\t}\n}\n\nvoid init(int max_count){\n\n\tfor(int count = 0; count <= max_count; count++){\n\t\tfor(int state = 0; state < POW[N]; state++){\n\t\t\tfor(int pre_pre = 0; pre_pre < N; pre_pre++){\n\t\t\t\tfor(int pre = 0; pre < N; pre++){\n\n\t\t\t\t\tdp[count][state][pre_pre][pre] = BIG_NUM;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n}\n\n\n\nvoid func(){\n\n\tfor(int i = 0; i < N; i++){\n\n\t\tscanf(\"%lf %lf\",&point[i].x,&point[i].y);\n\t}\n\n\tint turn = -1;\n\tFLG = false;\n\tans = BIG_NUM;\n\n\tLine line;\n\n\t//設問より、turn回数は最大で4回\n\tfor(int max_turn = 0; max_turn <= 4; max_turn++){\n\n\t\tinit(max_turn);\n\n\t\t//最初の2点を選ぶ\n\t\tfor(int pre_pre = 0; pre_pre < N; pre_pre++){\n\t\t\tfor(int pre = 0; pre < N; pre++){\n\n\t\t\t\tif(pre == pre_pre)continue;\n\n\t\t\t\tint first_state = POW[pre_pre]+POW[pre];\n\t\t\t\tdouble first_dist = calc_dist(point[pre_pre],point[pre]);\n\n\t\t\t\tline.p[0] = point[pre_pre];\n\t\t\t\tline.p[1] = point[pre];\n\n\t\t\t\tdp[0][first_state][pre_pre][pre] = first_dist;\n\t\t\t\tvector<Point> vec;\n\t\t\t\tvec.push_back(point[pre_pre]);\n\t\t\t\tvec.push_back(point[pre]);\n\n\t\t\t\trecursive(first_state,pre_pre,pre,0,max_turn,first_dist);\n\t\t\t}\n\t\t}\n\n\t\tif(FLG){\n\t\t\tturn = max_turn;\n\t\t\tbreak;\n\t\t}\n\t}\n\n\tprintf(\"%d %.10lf\\n\",turn,ans);\n}\n\nint main(){\n\n\tPOW[0] = 1;\n\tfor(int i = 1; i < SIZE; i++){\n\n\t\tPOW[i] = POW[i-1]*2;\n\t}\n\n\twhile(true){\n\n\t\tscanf(\"%d\",&N);\n\t\tif(N == 0)break;\n\n\t\tfunc();\n\t}\n\treturn 0;\n}", "accuracy": 1, "time_ms": 4760, "memory_kb": 7204, "score_of_the_acc": -0.6642, "final_rank": 13 }, { "submission_id": "aoj_1294_3626599", "code_snippet": "#include<iostream>\n#include<string>\n#include<algorithm>\n#include<vector>\n#include<iomanip>\n#include<math.h>\n#include<complex>\n#include<queue>\n#include<deque>\n#include<stack>\n#include<map>\n#include<set>\n#include<bitset>\n#include<functional>\n#include<assert.h>\n#include<numeric>\nusing namespace std;\n#define REP(i,m,n) for(int i=(int)(m) ; i < (int) (n) ; ++i )\n#define rep(i,n) REP(i,0,n)\nusing ll = long long;\nconst int inf=1e9+7;\nconst ll longinf=1LL<<60 ;\nconst ll mod=1e9+7 ;\n\nusing ld = double;\nusing Point = complex<ld>;\nconst ld eps = 1e-9;\nconst ld pi = acos(-1.0);\n\nbool eq(ld a, ld b) {\n\treturn (abs(a - b) < eps);\n}\n\nbool cmp(Point x,Point y){\n\tif(eq(x.real(),y.real()))return x.imag()<y.imag();\n\treturn x.real()<y.real();\n}\n\nbool eqq(Point x,Point y){\n\treturn eq(x.real(),y.real())&&eq(x.imag(),y.imag());\n}\n//内積\nld dot(Point a, Point b) {\n\treturn real(conj(a)*b);\n}\n//外積\nld cross(Point a, Point b) {\n\treturn imag(conj(a)*b);\n}\n\n\n//線分\n//直線にするなら十分通い2点を端点とすればよい\nclass Line {\npublic:\n\tPoint a, b;\n};\n//円\nclass Circle {\npublic:\n\tPoint p;\n\tld r;\n};\n//3点の位置関係\nint ccw(Point a, Point b, Point c) {\n\tb -= a; c -= a;\n\tif (cross(b, c) > eps)return 1;//a,b,cが反時計回り\n\tif (cross(b, c) < -eps)return -1;//a,b,cが時計回り\n\tif (dot(b, c) < 0)return 2;//c,a,bの順に一直線\n\tif (norm(b) < norm(c))return -2;//a,b,cの順に一直線\n\treturn 0;//a,c,bの順に一直線\n}\n//2直線の交差判定\nbool isis_ll(Line l, Line m) {\n\treturn !eq(cross(l.b - l.a, m.b - m.a), 0);\n}\n//直線と線分の交差判定\nbool isis_ls(Line l, Line s) {\n\treturn (cross(l.b - l.a, s.a - l.a)*cross(l.b - l.a, s.b - l.a) < eps);\n}\n//点が直線上に存在するか\nbool isis_lp(Line l, Point p) {\n\treturn (abs(cross(l.b - p, l.a - p)) < eps);\n}\n//点が線分上に存在するか\nbool isis_sp(Line s, Point p) {\n\treturn (abs(s.a - p) + abs(s.b - p) - abs(s.b - s.a) < eps);\n}\nbool isin_sp(Line s, Point p) {\n if(eqq(s.a,p)||eqq(s.b, p))return false;\n\treturn (abs(s.a - p) + abs(s.b - p) - abs(s.b - s.a) < eps);\n}\n//線分と線分の交差判定\nbool isis_ss(Line s, Line t) {\n if (isis_sp(t, s.a) || isis_sp(t, s.b))return false;\n\tif (isis_sp(s, t.a) || isis_sp(s, t.b) )return false;\n\treturn(cross(s.b - s.a, t.a - s.a)*cross(s.b - s.a, t.b - s.a) < -eps && cross(t.b - t.a, s.a - t.a)*cross(t.b - t.a, s.b - t.a) < -eps);\n}\n//点から直線への垂線の足\nPoint proj(Line l, Point p) {\n\tld t = dot(p - l.a, l.a - l.b) / norm(l.a - l.b);\n\treturn l.a + t * (l.a - l.b);\n}\n\nPoint cont(Line l, Point p){\n\treturn 2.0*proj(l, p) - p;\n}\n\n//直線と直線の交点\n//平行な2直線に対しては使うな!!!!\nPoint is_ll(Line s, Line t) {\n\tPoint sv = s.b - s.a; Point tv = t.b - t.a;\n\treturn s.a + sv * cross(tv, t.a - s.a) / cross(tv, sv);\n}\n//直線と点の距離\nld dist_lp(Line l, Point p) {\n\treturn abs(p - proj(l, p));\n}\n//直線と直線の距離\nld dist_ll(Line l, Line m) {\n\treturn isis_ll(l, m) ? 0 : dist_lp(l, m.a);\n}\n//線分と直線の距離\nld dist_ls(Line l, Line s) {\n\treturn isis_ls(l, s) ? 0 : min(dist_lp(l, s.a), dist_lp(l, s.b));\n}\n//線分と点の距離\nld dist_sp(Line s, Point p) {\n\tPoint r = proj(s, p);\n\treturn isis_sp(s, r) ? abs(p - r) : min(abs(p - s.a), abs(p - s.b));\n}\n//線分と線分の距離\nld dist_ss(Line s, Line t) {\n\tif (isis_ss(s, t))return 0;\n\treturn min({ dist_sp(s,t.a),dist_sp(s,t.b),dist_sp(t,s.a),dist_sp(t,s.b) });\n}\n\nbool included(vector<Point> pol, Point p){\n int n=pol.size();\n ld sum=0;\n rep(i,n){\n Point cur=pol[i], nxt=pol[(i+1)%n];\n if(isis_sp({cur,nxt},p))return true;\n sum+=arg((nxt-p)/(cur-p));\n }\n return abs(sum)>1;\n}\n\nstruct info{\n int pre,cur,mask;\n bool operator<(const info& rhs)const{\n return make_tuple(pre,cur,mask)<make_tuple(rhs.pre,rhs.cur,rhs.mask);\n\n };\n};\nvoid solve(int n){\n Point p[n];\n rep(i,n){\n int x,y;\n cin>>x>>y;\n p[i]=Point(x,y);\n }\n int m=n*(n-1);\n vector<pair<int,int>> l;\n vector<int> mask;\n rep(i,n)rep(j,n){\n if(i==j)continue;\n l.push_back({i,j});\n int b=0;\n Line ls={p[i],p[j]};\n rep(k,n){\n if(isis_sp(ls,p[k]))b|=(1<<k);\n }\n mask.push_back(b);\n }\n pair<int,ld> dist[1<<n][n][n];\n rep(i,n)rep(j,n)rep(k,1<<n){\n dist[k][i][j]={inf,inf};\n }\n using T = pair<pair<int,ld>,info> ;\n priority_queue<T,vector<T>,greater<T>> que;\n rep(i,m){\n info in = {l[i].first, l[i].second,mask[i]};\n dist[mask[i]][l[i].first][l[i].second]={0,abs(p[l[i].second]-p[l[i].first])};\n que.push({dist[mask[i]][l[i].first][l[i].second],in});\n }\n while(que.size()){\n auto t = que.top();que.pop();\n int mas=t.second.mask, pre=t.second.pre, cur=t.second.cur;\n pair<int,ld> d = t.first;\n if(dist[mas][pre][cur]<d)continue;\n rep(i,m){\n if(abs(cross(p[pre]-p[cur],p[l[i].first]-p[l[i].second]))<eps)continue;\n Point turn = is_ll({p[pre],p[cur]},{p[l[i].first],p[l[i].second]});\n if(abs(p[cur]-turn)>eps&&ccw(p[pre],p[cur],turn)!=-2)continue;\n if(abs(p[l[i].first]-turn)>eps&&ccw(p[l[i].second],p[l[i].first],turn)!=-2)continue;\n double nd = abs(p[cur]-turn)+abs(p[l[i].second]-p[l[i].first])+abs(p[l[i].first]-turn);\n int nmask=mas|mask[i];\n if(dist[nmask][l[i].first][l[i].second] > make_pair(d.first+1, d.second+nd)){\n dist[nmask][l[i].first][l[i].second] = make_pair(d.first+1, d.second+nd);\n que.push({dist[nmask][l[i].first][l[i].second],{l[i].first, l[i].second,nmask}});\n }\n }\n }\n pair<int,ld> ans={inf,inf};\n rep(i,n)rep(j,n){\n ans=min(ans,dist[(1<<n)-1][i][j]);\n }\n cout<<ans.first<<\" \"<<ans.second<<endl;\n}\n\nint main(){\n int n;\n cout<<fixed<<setprecision(10);\n while(cin>>n,n!=0)solve(n);\n return 0;\n}", "accuracy": 1, "time_ms": 2520, "memory_kb": 5620, "score_of_the_acc": -0.3469, "final_rank": 10 }, { "submission_id": "aoj_1294_3626598", "code_snippet": "#include<iostream>\n#include<string>\n#include<algorithm>\n#include<vector>\n#include<iomanip>\n#include<math.h>\n#include<complex>\n#include<queue>\n#include<deque>\n#include<stack>\n#include<map>\n#include<set>\n#include<bitset>\n#include<functional>\n#include<assert.h>\n#include<numeric>\nusing namespace std;\n#define REP(i,m,n) for(int i=(int)(m) ; i < (int) (n) ; ++i )\n#define rep(i,n) REP(i,0,n)\nusing ll = long long;\nconst int inf=1e9+7;\nconst ll longinf=1LL<<60 ;\nconst ll mod=1e9+7 ;\n\nusing ld = double;\nusing Point = complex<ld>;\nconst ld eps = 1e-9;\nconst ld pi = acos(-1.0);\n\nbool eq(ld a, ld b) {\n\treturn (abs(a - b) < eps);\n}\n\nbool cmp(Point x,Point y){\n\tif(eq(x.real(),y.real()))return x.imag()<y.imag();\n\treturn x.real()<y.real();\n}\n\nbool eqq(Point x,Point y){\n\treturn eq(x.real(),y.real())&&eq(x.imag(),y.imag());\n}\n//内積\nld dot(Point a, Point b) {\n\treturn real(conj(a)*b);\n}\n//外積\nld cross(Point a, Point b) {\n\treturn imag(conj(a)*b);\n}\n\n\n//線分\n//直線にするなら十分通い2点を端点とすればよい\nclass Line {\npublic:\n\tPoint a, b;\n};\n//円\nclass Circle {\npublic:\n\tPoint p;\n\tld r;\n};\n//3点の位置関係\nint ccw(Point a, Point b, Point c) {\n\tb -= a; c -= a;\n\tif (cross(b, c) > eps)return 1;//a,b,cが反時計回り\n\tif (cross(b, c) < -eps)return -1;//a,b,cが時計回り\n\tif (dot(b, c) < 0)return 2;//c,a,bの順に一直線\n\tif (norm(b) < norm(c))return -2;//a,b,cの順に一直線\n\treturn 0;//a,c,bの順に一直線\n}\n//2直線の交差判定\nbool isis_ll(Line l, Line m) {\n\treturn !eq(cross(l.b - l.a, m.b - m.a), 0);\n}\n//直線と線分の交差判定\nbool isis_ls(Line l, Line s) {\n\treturn (cross(l.b - l.a, s.a - l.a)*cross(l.b - l.a, s.b - l.a) < eps);\n}\n//点が直線上に存在するか\nbool isis_lp(Line l, Point p) {\n\treturn (abs(cross(l.b - p, l.a - p)) < eps);\n}\n//点が線分上に存在するか\nbool isis_sp(Line s, Point p) {\n\treturn (abs(s.a - p) + abs(s.b - p) - abs(s.b - s.a) < eps);\n}\nbool isin_sp(Line s, Point p) {\n if(eqq(s.a,p)||eqq(s.b, p))return false;\n\treturn (abs(s.a - p) + abs(s.b - p) - abs(s.b - s.a) < eps);\n}\n//線分と線分の交差判定\nbool isis_ss(Line s, Line t) {\n if (isis_sp(t, s.a) || isis_sp(t, s.b))return false;\n\tif (isis_sp(s, t.a) || isis_sp(s, t.b) )return false;\n\treturn(cross(s.b - s.a, t.a - s.a)*cross(s.b - s.a, t.b - s.a) < -eps && cross(t.b - t.a, s.a - t.a)*cross(t.b - t.a, s.b - t.a) < -eps);\n}\n//点から直線への垂線の足\nPoint proj(Line l, Point p) {\n\tld t = dot(p - l.a, l.a - l.b) / norm(l.a - l.b);\n\treturn l.a + t * (l.a - l.b);\n}\n\nPoint cont(Line l, Point p){\n\treturn 2.0*proj(l, p) - p;\n}\n\n//直線と直線の交点\n//平行な2直線に対しては使うな!!!!\nPoint is_ll(Line s, Line t) {\n\tPoint sv = s.b - s.a; Point tv = t.b - t.a;\n\treturn s.a + sv * cross(tv, t.a - s.a) / cross(tv, sv);\n}\n//直線と点の距離\nld dist_lp(Line l, Point p) {\n\treturn abs(p - proj(l, p));\n}\n//直線と直線の距離\nld dist_ll(Line l, Line m) {\n\treturn isis_ll(l, m) ? 0 : dist_lp(l, m.a);\n}\n//線分と直線の距離\nld dist_ls(Line l, Line s) {\n\treturn isis_ls(l, s) ? 0 : min(dist_lp(l, s.a), dist_lp(l, s.b));\n}\n//線分と点の距離\nld dist_sp(Line s, Point p) {\n\tPoint r = proj(s, p);\n\treturn isis_sp(s, r) ? abs(p - r) : min(abs(p - s.a), abs(p - s.b));\n}\n//線分と線分の距離\nld dist_ss(Line s, Line t) {\n\tif (isis_ss(s, t))return 0;\n\treturn min({ dist_sp(s,t.a),dist_sp(s,t.b),dist_sp(t,s.a),dist_sp(t,s.b) });\n}\n\nbool included(vector<Point> pol, Point p){\n int n=pol.size();\n ld sum=0;\n rep(i,n){\n Point cur=pol[i], nxt=pol[(i+1)%n];\n if(isis_sp({cur,nxt},p))return true;\n sum+=arg((nxt-p)/(cur-p));\n }\n return abs(sum)>1;\n}\n\nstruct info{\n int pre,cur,mask;\n bool operator<(const info& rhs)const{\n return make_tuple(pre,cur,mask)<make_tuple(rhs.pre,rhs.cur,rhs.mask);\n\n };\n};\nvoid solve(int n){\n Point p[n];\n rep(i,n){\n int x,y;\n cin>>x>>y;\n p[i]=Point(x,y);\n }\n int m=n*(n-1);\n vector<pair<int,int>> l;\n vector<int> mask;\n rep(i,n)rep(j,n){\n if(i==j)continue;\n l.push_back({i,j});\n int b=0;\n Line ls={p[i],p[j]};\n rep(k,n){\n if(isis_sp(ls,p[k]))b|=(1<<k);\n }\n mask.push_back(b);\n }\n pair<int,ld> dist[1<<n][n][n];\n rep(i,n)rep(j,n)rep(k,1<<n){\n dist[k][i][j]={inf,inf};\n }\n using T = pair<pair<int,ld>,info> ;\n priority_queue<T,vector<T>,greater<T>> que;\n rep(i,m){\n info in = {l[i].first, l[i].second,mask[i]};\n dist[mask[i]][l[i].first][l[i].second]={0,abs(p[l[i].second]-p[l[i].first])};\n que.push({dist[mask[i]][l[i].first][l[i].second],in});\n }\n while(que.size()){\n auto t = que.top();que.pop();\n int mas=t.second.mask, pre=t.second.pre, cur=t.second.cur;\n pair<int,ld> d = t.first;\n if(dist[mas][pre][cur]<d)continue;\n rep(i,m){\n if(abs(cross(p[pre]-p[cur],p[l[i].first]-p[l[i].second]))<eps)continue;\n Point turn = is_ll({p[pre],p[cur]},{p[l[i].first],p[l[i].second]});\n if(isin_sp({p[pre],p[cur]},turn))continue;\n if(isin_sp({p[l[i].first],p[l[i].second]},turn))continue;\n if(abs(p[cur]-turn)>eps&&ccw(p[pre],p[cur],turn)!=-2)continue;\n if(abs(p[l[i].first]-turn)>eps&&ccw(p[l[i].second],p[l[i].first],turn)!=-2)continue;\n double nd = abs(p[cur]-turn)+abs(p[l[i].second]-p[l[i].first])+abs(p[l[i].first]-turn);\n int nmask=mas|mask[i];\n if(dist[nmask][l[i].first][l[i].second] > make_pair(d.first+1, d.second+nd)){\n dist[nmask][l[i].first][l[i].second] = make_pair(d.first+1, d.second+nd);\n que.push({dist[nmask][l[i].first][l[i].second],{l[i].first, l[i].second,nmask}});\n }\n }\n }\n pair<int,ld> ans={inf,inf};\n rep(i,n)rep(j,n){\n ans=min(ans,dist[(1<<n)-1][i][j]);\n }\n cout<<ans.first<<\" \"<<ans.second<<endl;\n}\n\nint main(){\n int n;\n cout<<fixed<<setprecision(10);\n while(cin>>n,n!=0)solve(n);\n return 0;\n}", "accuracy": 1, "time_ms": 3360, "memory_kb": 5576, "score_of_the_acc": -0.4539, "final_rank": 11 }, { "submission_id": "aoj_1294_3195689", "code_snippet": "#include <cstdio>\n#include <iostream>\n#include <vector>\n#include <complex>\n#include <utility>\n#include <algorithm>\n#include <numeric>\nusing namespace std;\ntypedef complex<double> P;\ntypedef pair<P,P> L;\n#define X real()\n#define Y imag()\nconst double INF = (1e15), EPS = (1e-8);\nconst pair<int,double> init(8,INF);\nP Points[10];\npair<int,double> ans, DP[2];\n\ninline double dot(P a, P b){ return a.X*b.X + a.Y*b.Y;}\ninline double cross(P a, P b){ return a.X*b.Y - a.Y*b.X;}\n\ninline P intersection(L a, L b){\n P bf = b.first, af = a.first-bf, as = a.second-bf, bs = b.second-bf;\n return af + bf + cross(bs,af)/(-cross(bs,as)+cross(bs,af))*(as-af);\n}\n\ninline bool on_line(P a, P b, P c){\n b -= a, c -= a;\n if(cross(b,c) > EPS) return false;\n if(cross(b,c) < -EPS) return false;\n if(dot(b,c) < 0) return false;\n if(norm(b) + EPS < norm(c)) return false;\n return true;\n}\n\nint main(){\n int n;\n while(cin >> n, n){\n ans = init;\n for(int i = 0; i < n; ++i){\n double x, y;\n cin >> x >> y;\n Points[i] = P(x,y);\n }\n vector<int> perm(n);\n iota(perm.begin(), perm.end(), 0);\n do{\n if(perm[0] == n-1) break;\n if(perm[0] > perm.back()) continue;\n DP[1] = init;\n DP[0] = pair<int,double>(0,abs(Points[perm[1]]-Points[perm[0]]));\n for(int i = 1; i < n-1; ++i){\n if(DP[0] >= ans && DP[1] >= ans) break;\n P prev = Points[perm[i-1]], cur = Points[perm[i]], nex = Points[perm[i+1]];\n int c = !on_line(prev, nex, cur);\n pair<int,double> val = DP[0];\n double l = abs(nex-cur);\n DP[1].second += l;\n DP[0].second += l;\n DP[0].first += c;\n DP[0] = min(DP[0],DP[1]);\n if(i == n-2) break;\n P nexx = Points[perm[i+2]];\n if(norm(cross(cur-prev,nexx-nex)) < EPS or on_line(prev,nex,cur) or on_line(cur,nexx,nex)){\n DP[1] = init;\n continue;\n }\n P cp = intersection(L(prev,cur),L(nex,nexx));\n if((!on_line(cp,nexx,nex)) or (!on_line(prev,cp,cur))){\n DP[1] = init;\n continue;\n }\n DP[1] = val;\n ++DP[1].first;\n DP[1].second += abs(cp-cur) + abs(cp-nex);\n }\n ans = min(ans, DP[0]);\n }while(next_permutation(perm.begin(), perm.end()));\n printf(\"%d %.5f\\n\", ans.first, ans.second);\n }\n return 0;\n}", "accuracy": 1, "time_ms": 7990, "memory_kb": 3168, "score_of_the_acc": -1.0029, "final_rank": 14 }, { "submission_id": "aoj_1294_3087217", "code_snippet": "#include <iostream>\n#include <iomanip>\n#include <complex>\n#include <vector>\n#include <algorithm>\n#include <cmath>\n#include <array>\n#include <map>\n#include <set>\n#include <utility>\n#include <queue>\nusing namespace std;\nconst double EPS = 1e-10;\nconst double INF = 1e12;\nconst double PI = acos(-1);\n#define EQ(n,m) (abs((n)-(m)) < EPS)\n#define X real()\n#define Y imag()\n\ntypedef complex<double> P;\ntypedef vector<P> VP;\nstruct L : array<P, 2>{\n L(const P& a, const P& b){ at(0)=a; at(1)=b; }\n L(){}\n};\n\nnamespace std{\n bool operator < (const P& a, const P& b){\n return !EQ(a.X,b.X) ? a.X<b.X : a.Y+EPS<b.Y;\n }\n bool operator == (const P& a, const P& b){\n return abs(a-b) < EPS;\n }\n}\n\ndouble dot(P a, P b){\n return (conj(a)*b).X;\n}\ndouble cross(P a, P b){\n return (conj(a)*b).Y;\n}\nint ccw(P a, P b, P c){\n b -= a;\n c -= a;\n if(cross(b,c) > EPS) return +1; //ccw\n if(cross(b,c) < -EPS) return -1; //cw\n if(dot(b,c) < -EPS) return +2; //c-a-b\n if(abs(c)-abs(b) > EPS) return -2; //a-b-c\n return 0; //a-c-b\n}\n\nbool intersectLP(const L& l, const P& p){\n return abs(cross(l[1]-p, l[0]-p)) < EPS;\n}\nbool intersectSP(const L& s, const P &p){\n return abs(cross(s[0]-p, s[1]-p))<EPS && dot(s[0]-p, s[1]-p)<EPS;\n}\nbool isParallel(const P &a, const P &b){\n return abs(cross(a,b)) < EPS;\n}\nbool isParallel(const L &a, const L &b){\n return isParallel(a[1]-a[0], b[1]-b[0]);\n}\n\nP crosspointLL(const L &l, const L &m) {\n double A = cross(l[1]-l[0], m[1]-m[0]);\n double B = cross(l[1]-l[0], l[1]-m[0]);\n return m[0] + B/A *(m[1]-m[0]);\n}\n\nstruct info{\n int pos,bit,turn;\n double dist;\n info(int p, int b, int t, double d):pos(p),bit(b),turn(t),dist(d){}\n info(){}\n bool operator <(const info &a) const{\n return (turn!=a.turn)? turn>a.turn: dist>a.dist;\n }\n};\nbool eqpos(const info &a, const info &b){\n return a.pos == b.pos;\n}\npair<vector<vector<info> >, VP> exarrangement(const vector<L> &l, const VP &p, const set<P> &ps){\n vector<VP> cp(l.size());\n VP plist = p;\n for(int i=0; i<(int)l.size(); i++){\n for(int j=i+1; j<(int)l.size(); j++){\n if(!isParallel(l[i], l[j])){\n P cpij = crosspointLL(l[i], l[j]);\n cp[i].push_back(cpij);\n cp[j].push_back(cpij);\n plist.push_back(cpij);\n }\n }\n for(int j=0; j<(int)p.size(); j++){\n if(intersectLP(l[i], p[j])){\n cp[i].push_back(p[j]);\n }\n }\n sort(cp[i].begin(), cp[i].end());\n cp[i].erase(unique(cp[i].begin(), cp[i].end()), cp[i].end());\n }\n sort(plist.begin(), plist.end());\n plist.erase(unique(plist.begin(), plist.end()), plist.end());\n\n int n = plist.size();\n map<P, int> conv;\n for(int i=0; i<n; i++){\n conv[plist[i]] = i;\n }\n vector<vector<info> > adj(n);\n for(int i=0; i<(int)cp.size(); i++){\n for(int j=0; j<(int)cp[i].size(); j++){\n for(int k=j+1; k<(int)cp[i].size(); k++){\n int jidx = conv[cp[i][j]];\n int kidx = conv[cp[i][k]];\n double dist = abs(cp[i][j] -cp[i][k]);\n int bit = 0;\n L edge(cp[i][j], cp[i][k]);\n for(int t=0; t<(int)p.size(); t++){\n if(intersectSP(edge, p[t])){\n bit |= 1<<t;\n }\n }\n adj[jidx].push_back(info(kidx, bit, 0, dist));\n adj[kidx].push_back(info(jidx, bit, 0, dist));\n }\n }\n }\n for(int i=0; i<n; i++){\n sort(adj[i].begin(), adj[i].end());\n adj[i].erase(unique(adj[i].begin(), adj[i].end(), eqpos), adj[i].end());\n }\n return make_pair(adj, plist);\n}\n\npair<vector<vector<info> >, VP> makegraph(const VP &p){\n vector<L> lines;\n for(int i=0; i<(int)p.size(); i++){\n for(int j=i+1; j<(int)p.size(); j++){\n lines.push_back(L(p[i], p[j]));\n }\n }\n set<P> ps;\n for(int i=0; i<(int)p.size(); i++){\n ps.insert(p[i]);\n }\n return exarrangement(lines, p, ps);\n}\n\nint main(){\n while(1){\n int n;\n cin >> n;\n if(n==0) break;\n\n VP p(n);\n for(int i=0; i<n; i++){\n int x,y;\n cin >> x >> y;\n p[i] = P(x, y);\n }\n \n pair<vector<vector<info> >, VP> ret = makegraph(p);\n vector<vector<info> > &adj = ret.first;\n VP &plist = ret.second;\n priority_queue<info> wait;\n vector<vector<pair<int, double> > > mincost(plist.size(), vector<pair<int, double> >(1<<n, make_pair(100, INF)));\n for(int i=0; i<n; i++){\n int pidx = lower_bound(plist.begin(), plist.end(), p[i]) -plist.begin();\n wait.push(info(pidx, 0, -1, 0));\n mincost[i][1<<i] = make_pair(-1, 0);\n }\n\n while(!wait.empty()){\n int pos = wait.top().pos;\n int bit = wait.top().bit;\n int turn = wait.top().turn;\n double dist = wait.top().dist;\n wait.pop();\n if(mincost[pos][bit] < make_pair(turn, dist)) continue;\n if(bit == (1<<n)-1) continue;\n\n for(info &next: adj[pos]){\n int npos = next.pos;\n int nbit = bit | next.bit;\n double ndist = dist +next.dist;\n pair<int, double> cost(turn+1, ndist);\n if(cost < mincost[npos][nbit]){\n wait.push(info(npos, nbit, turn+1, ndist));\n mincost[npos][nbit] = cost;\n }\n }\n }\n\n pair<int, double> ans(100, INF);\n for(int i=0; i<(int)plist.size(); i++){\n ans = min(ans, mincost[i][(1<<n)-1]);\n }\n cout << fixed << setprecision(10);\n cout << ans.first << \" \" << ans.second << endl;\n }\n return 0;\n}", "accuracy": 1, "time_ms": 5010, "memory_kb": 33000, "score_of_the_acc": -1.1816, "final_rank": 18 }, { "submission_id": "aoj_1294_2059552", "code_snippet": "#include<bits/stdc++.h>\n#define MAX 2500\n#define inf 1<<29\n#define linf 1e18\n#define eps (1e-8)\n#define mod 1000000007\n#define pi M_PI\n#define f first\n#define s second\n#define mp make_pair\n#define pb push_back\n#define all(a) (a).begin(),(a).end()\n#define pd(a) printf(\"%.10f\\n\",(double)(a))\n#define FOR(i,a,b) for(int i=(a);i<(b);i++)\n#define RFOR(i,a,b) for(int i=(a)-1;(b)<=i;i--)\n#define equals(a,b) (fabs((a)-(b))<eps)\nusing namespace std;\ntypedef long long ll;\ntypedef unsigned long long ull;\ntypedef pair<int,int> pii;\ntypedef pair<double,int> pdi;\ntypedef pair<int,double> pid;\ntypedef vector<int> vi;\ntypedef vector<pii> vpi;\nconst int dx[8]={1,0,-1,0,1,1,-1,-1};\nconst int dy[8]={0,1,0,-1,1,-1,1,-1};\n\nclass Point{\npublic:\n double x,y;\n Point(double x=0,double y=0):x(x),y(y){}\n\n Point operator+(Point p){ return Point(x+p.x,y+p.y);}\n Point operator-(Point p){ return Point(x-p.x,y-p.y);}\n Point operator*(double k){ return Point(x*k,y*k);}\n Point operator/(double k){ return Point(x/k,y/k);}\n bool operator<(Point p)const{ return (x!=p.x ? x<p.x : y<p.y);}\n bool operator==(Point p)const{ return fabs(x-p.x)<eps && fabs(y-p.y)<eps;}\n\n double abs(){ return sqrt(norm());}\n double norm(){ return (x*x+y*y);}\n};\ntypedef Point Vector;\ntypedef vector<Point> Polygon;\n\nclass Segment{\npublic:\n Point p1,p2;\n Segment(Point p1=Point(),Point p2=Point()):p1(p1),p2(p2){}\n};\ntypedef Segment Line;\n\ndouble norm(Vector a){ return (a.x*a.x+a.y*a.y);}\ndouble abs(Vector a){ return sqrt(norm(a));}\ndouble dot(Vector a,Vector b){ return (a.x*b.x+a.y*b.y);}\ndouble cross(Vector a,Vector b){ return (a.x*b.y-a.y*b.x);}\n\nbool isParallel(Vector a,Vector b){\n return equals(cross(a,b),0.0);\n}\n\nbool isParallel(Segment s,Segment t){\n return equals(cross(s.p1-s.p2,t.p1-t.p2),0.0);\n}\n\nint ccw(Point p0,Point p1,Point p2){\n Vector a=p1-p0;\n Vector b=p2-p0;\n if(cross(a,b)>eps)return 1;\n if(cross(a,b)<-eps)return -1;\n if(dot(a,b)<-eps)return 2;\n if(a.norm()<b.norm())return -2;\n return 0;\n}\n\nPoint getCrossPointLL(Line a,Line b){\n double A=cross(a.p2-a.p1,b.p2-b.p1);\n double B=cross(a.p2-a.p1,a.p2-b.p1);\n if(abs(A)<eps || abs(B)<eps)return b.p1;\n return b.p1+(b.p2-b.p1)*(B/A);\n}\n\nvector<Point> unique(vector<Point> vp){\n sort(vp.begin(),vp.end());\n vector<Point> res;\n res.push_back(vp[0]);\n for(int i=1;i<vp.size();i++){\n if(!(vp[i]==res.back()))res.push_back(vp[i]);\n }\n return res;\n}\n\nclass State{\n public:\n int index,bit,num;\n double dis;\n State(int index,int bit,int num,double dis):\n index(index),bit(bit),num(num),dis(dis){}\n bool operator<(State s)const{\n return s.num!=num ? s.num<num : s.dis-dis<-eps; \n }\n};\n\nint n;\nvector<Point> vp,sp;\nvector<pii> e[MAX];\npid d[MAX][1<<10];\n\nvoid init(){\n vp.clear();\n sp.clear();\n FOR(i,0,MAX){\n e[i].clear();\n FOR(j,0,1<<10)d[i][j]=mp(inf,inf);\n }\n}\n\nbool comp(pid a,pid b){\n return a.f!=b.f ? a.f<b.f : a.s-b.s<-eps;\n}\n\npid dijkstra(){\n priority_queue<State> pq;\n FOR(i,0,MAX)FOR(j,0,1<<n)d[i][j]=mp(inf,inf);\n FOR(i,0,sp.size()){\n d[i][0]=mp(0,0);\n pq.push(State(i,0,0,0));\n }\n\n while(pq.size()){\n State u=pq.top();\n pq.pop();\n if(u.bit==((1<<n)-1))return mp(u.num,u.dis);\n if(comp(d[u.index][u.bit],mp(u.num,u.dis)))continue; \n\n FOR(i,0,e[u.index].size()){\n if(i==u.index)continue;\n pii next=e[u.index][i];\n int nbit=(u.bit|next.s);\n double cost=abs(sp[u.index]-sp[next.f]);\n if(comp(mp(d[u.index][u.bit].f+1,d[u.index][u.bit].s+cost),\n d[next.f][nbit])){\n d[next.f][nbit]=mp(d[u.index][u.bit].f+1,\n d[u.index][u.bit].s+cost);\n pq.push(State(next.f,nbit,d[next.f][nbit].f,d[next.f][nbit].s));\n }\n }\n }\n return mp(inf,inf);\n}\n\npid solve(){\n vector<Line> vl; \n FOR(i,0,n)FOR(j,i+1,n)vl.pb(Line(vp[i],vp[j]));\n FOR(i,0,n)sp.pb(vp[i]);\n FOR(i,0,vl.size()){\n FOR(j,i+1,vl.size()){\n if(isParallel(vl[i],vl[j]))continue;\n sp.pb(getCrossPointLL(vl[i],vl[j]));\n }\n }\n sp=unique(sp);\n FOR(i,0,sp.size()){\n FOR(j,0,sp.size()){\n if(i==j)continue;\n int bits=0;\n int c=0;\n FOR(k,0,n){\n if(ccw(sp[i],sp[j],vp[k])==0){\n bits+=(1<<k);\n c++;\n }\n }\n if(c>=2)e[i].pb(mp(j,bits));\n }\n }\n return dijkstra();\n}\n\nint main()\n{\n while(1){\n cin>>n;\n init();\n if(n==0)break;\n FOR(i,0,n){\n int x,y;\n cin>>x>>y;\n vp.pb(Point(x,y));\n }\n pid ans=solve();\n cout<<ans.f-1<<\" \";\n pd(ans.s);\n }\n return 0;\n}", "accuracy": 1, "time_ms": 1580, "memory_kb": 48456, "score_of_the_acc": -1.032, "final_rank": 15 }, { "submission_id": "aoj_1294_1142830", "code_snippet": "#include<stdio.h>\n#include<algorithm>\n#include<math.h>\nusing namespace std;\nconst double EPS = 1e-10;\nconst double INF = 1e+10;\nconst double PI = acos(-1);\nint sig(double r) { return (r < -EPS) ? -1 : (r > +EPS) ? +1 : 0; }\ninline double ABS(double a){return max(a,-a);}\nstruct Pt {\n\tdouble x, y;\n\tPt() {}\n\tPt(double x, double y) : x(x), y(y) {}\n\tPt operator+(const Pt &a) const { return Pt(x + a.x, y + a.y); }\n\tPt operator-(const Pt &a) const { return Pt(x - a.x, y - a.y); }\n\tPt operator*(const Pt &a) const { return Pt(x * a.x - y * a.y, x * a.y + y * a.x); }\n\tPt operator-() const { return Pt(-x, -y); }\n\tPt operator*(const double &k) const { return Pt(x * k, y * k); }\n\tPt operator/(const double &k) const { return Pt(x / k, y / k); }\n\tdouble ABS() const { return sqrt(x * x + y * y); }\n\tdouble abs2() const { return x * x + y * y; }\n\tdouble arg() const { return atan2(y, x); }\n\tdouble dot(const Pt &a) const { return x * a.x + y * a.y; }\n\tdouble det(const Pt &a) const { return x * a.y - y * a.x; }\n};\ndouble tri(const Pt &a, const Pt &b, const Pt &c) { return (b - a).det(c - a); }\nint iSP(Pt a, Pt b, Pt c) {\n\tint s = sig((b - a).det(c - a));\n\tif (s) return s;\n\tif (sig((b - a).dot(c - a)) < 0) return -2; // c-a-b\n\tif (sig((a - b).dot(c - b)) < 0) return +2; // a-b-c\n\treturn 0;\n}\nint iLL(Pt a, Pt b, Pt c, Pt d) {\n\tif (sig((b - a).det(d - c))) return 1; // intersect\n\tif (sig((b - a).det(c - a))) return 0; // parallel\n\treturn -1; // correspond\n}\nPt pLL(Pt a, Pt b, Pt c, Pt d) {\n\tb = b - a; d = d - c; return a + b * (c - a).det(d) / b.det(d);\n}\n\npair<int,double> dp[1<<10][11][11];\nPt p[12];\nint main(){\n\tint a;\n\twhile(scanf(\"%d\",&a),a){\n\t\tfor(int i=0;i<a;i++){\n\t\t\tdouble x,y;\n\t\t\tscanf(\"%lf%lf\",&x,&y);\n\t\t\tp[i]=Pt(x,y);\n\t\t}\n\t\tfor(int i=0;i<(1<<a);i++)for(int j=0;j<a;j++)for(int k=0;k<a;k++)\n\t\t\tdp[i][j][k]=make_pair(999999999,999999999);\n\t\tfor(int i=0;i<a;i++){\n\t\t\tfor(int j=0;j<a;j++){\n\t\t\t\tif(i==j)continue;\n\t\t\t\tint to=(1<<i)+(1<<j);\n\t\t\t\tfor(int k=0;k<a;k++){\n\t\t\t\t\tif(iSP(p[i],p[k],p[j])==2)to|=(1<<k);\n\t\t\t\t}\n\t\t\t\tdp[to][i][j]=make_pair(0,(p[i]-p[j]).ABS());\n\t\t\t}\n\t\t}\n\t\tfor(int i=0;i<(1<<a);i++){\n\t\t\tfor(int j=0;j<a;j++)for(int k=0;k<a;k++){\n\t\t\t\tif(dp[i][j][k].first>99999)continue;\n\t\t\t\tfor(int l=0;l<a;l++){\n\t\t\t\t\tint to=i|(1<<l);\n\t\t\t\t\tfor(int m=0;m<a;m++){\n\t\t\t\t\t\tif(iSP(p[k],p[m],p[l])==2)to|=(1<<m);\n\t\t\t\t\t}\n\t\t\t\t\tdp[to][k][l]=min(dp[to][k][l],make_pair(dp[i][j][k].first+1,dp[i][j][k].second+(p[k]-p[l]).ABS()));\n\t\t\t\t}\n\t\t\t\tfor(int l=0;l<a;l++)for(int m=0;m<a;m++){\n\t\t\t\t\tif(l==m)continue;\n\t\t\t\t\tif(iLL(p[j],p[k],p[l],p[m])!=1)continue;\n\t\t\t\t\tPt t=pLL(p[j],p[k],p[l],p[m]);\n\t\t\t\t\tif(iSP(p[j],p[k],t)==2&&iSP(t,p[l],p[m])==2){\n\t\t\t\t\t\tint to=i|(1<<l)|(1<<m);\n\t\t\t\t\t\tfor(int n=0;n<a;n++){\n\t\t\t\t\t\t\tif(iSP(t,p[n],p[m])==2)to|=(1<<n);\n\t\t\t\t\t\t}\n\t\t\t\t\t\tdp[to][l][m]=min(dp[to][l][m],make_pair(dp[i][j][k].first+1,dp[i][j][k].second+(t-p[k]).ABS()+(p[m]-t).ABS()));\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\tpair<int,double>ret=make_pair(999999999,99999999);\n\t\tfor(int i=0;i<a;i++)for(int j=0;j<a;j++)ret=min(ret,dp[(1<<a)-1][i][j]);\n\t\tprintf(\"%d %.12f\\n\",ret.first,ret.second);\n\t}\n}", "accuracy": 1, "time_ms": 1890, "memory_kb": 3012, "score_of_the_acc": -0.2169, "final_rank": 6 }, { "submission_id": "aoj_1294_1009070", "code_snippet": "#include <cstdio>\n#include <cmath>\n#include <cstring>\n#include <cstdlib>\n#include <climits>\n#include <ctime>\n#include <queue>\n#include <stack>\n#include <algorithm>\n#include <list>\n#include <vector>\n#include <set>\n#include <map>\n#include <iostream>\n#include <deque>\n#include <complex>\n#include <string>\n#include <iomanip>\n#include <sstream>\n#include <bitset>\n#include <valarray>\n#include <iterator>\nusing namespace std;\ntypedef long long int ll;\ntypedef unsigned int uint;\ntypedef unsigned char uchar;\ntypedef unsigned long long ull;\ntypedef pair<int, int> pii;\ntypedef pair<int, double> pid;\ntypedef vector<int> vi;\n\n#define REP(i,x) for(int i=0;i<(int)(x);i++)\n#define FOR(i,c) for(__typeof((c).begin())i=(c).begin();i!=(c).end();i++)\n#define RREP(i,x) for(int i=((int)(x)-1);i>=0;i--)\n#define RFOR(i,c) for(__typeof((c).rbegin())i=(c).rbegin();i!=(c).rend();i++)\n#define ALL(container) container.begin(), container.end()\n#define RALL(container) container.rbegin(), container.rend()\n#define SZ(container) ((int)container.size())\n#define mp(a,b) make_pair(a, b)\n#define UNIQUE(v) v.erase( unique(v.begin(), v.end()), v.end() );\n\ntemplate<class T> bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }\ntemplate<class T> bool chmin(T &a, const T &b) { if (a>b) { a=b; return 1; } return 0; }\ntemplate<class T> ostream& operator<<(ostream &os, const vector<T> &t) {\nos<<\"[\"; FOR(it,t) {if(it!=t.begin()) os<<\",\"; os<<*it;} os<<\"]\"; return os;\n}\ntemplate<class T> ostream& operator<<(ostream &os, const set<T> &t) {\nos<<\"{\"; FOR(it,t) {if(it!=t.begin()) os<<\",\"; os<<*it;} os<<\"}\"; return os;\n}\ntemplate<class S, class T> ostream& operator<<(ostream &os, const pair<S,T> &t) { return os<<\"(\"<<t.first<<\",\"<<t.second<<\")\";}\n#include <complex>\n#include <cmath>\n#define EPS 1.0e-10\n#define PI 3.1415926535897932384 \n\n// 実数の符号関数\ninline int signum(double x) { return (abs(x) < EPS ? 0 : x > 0 ? 1 : -1); }\n//XY座標\n#define X real()\n#define Y imag()\n// 点\ntypedef complex<double> P;\n \n\n// 線分・半直線・直線\nstruct L { \n\tP pos, dir;\n\tL(){}\n\tL(P p, P d):pos(p),dir(d){}\n};\n \n// 多角形\ntypedef vector<P> G;\n \n// 円\nstruct C { P p; double r; };\n\n// std::norm はabs(p)*abs(p)なので遅い\ninline double norm(P p){\n\treturn p.X*p.X+p.Y*p.Y;\n}\n\n// 二つのベクトルの内積を計算する\ninline double inp(const P& a, const P& b) {\n\treturn (conj(a)*b).real();\n}\n \n// 二つのベクトルの外積を計算する\ninline double outp(const P& a, const P& b) {\n\treturn (conj(a)*b).imag();\n}\n\ninline int ccw(const P& p, const P& r, const P& s) {\n P a(r-p), b(s-p);\n int sgn = signum(outp(a, b));\n if (sgn != 0)\n return sgn;\n if (a.real()*b.real() < -EPS || a.imag()*b.imag() < -EPS)\n return -1;\n if (norm(a) < norm(b) - EPS)\n return 1;\n return 0;\n}\n\n// ベクトルpをベクトルbに射影したベクトルを計算する\ninline P proj(const P& p, const P& b) {\n\treturn b*inp(p,b)/norm(b);\n}\n \n// 点pから直線lに引いた垂線の足となる点を計算する\ninline P perf(const L& l, const P& p) {\n\tL m(l.pos - p, l.dir);\n\treturn (p + (m.pos - proj(m.pos, m.dir)));\n}\n \n// 線分sを直線bに射影した線分を計算する\ninline L proj(const L& s, const L& b) {\n\t return L(perf(b, s.pos), proj(s.dir, b.dir));\n}\n\nbool ll_intersects(const L& l, const L& m) {\n return (abs(outp(l.dir, m.dir)) > EPS || abs(outp(l.dir, m.pos-l.pos)) < EPS);\n}\nP line_cross(const L& l, const L& m) {\n double num = outp(m.dir, m.pos-l.pos);\n double denom = outp(m.dir, l.dir);\n return P(l.pos + l.dir*num/denom);\n}\nbool ls_intersects(const L& l, const L& s) {\n return (signum(outp(l.dir, s.pos-l.pos)) *\n signum(outp(l.dir, s.pos+s.dir-l.pos)) <= 0);\n}\nbool sp_intersects(const L& s, const P& p) {\n return ( abs(s.pos - p) + abs(s.pos + s.dir - p) - abs(s.dir) < EPS );\n}\nbool ss_intersects(const L& s, const L& t) {\n return (ccw(s.pos, s.pos+s.dir, t.pos) *\n ccw(s.pos, s.pos+s.dir, t.pos+t.dir) <= 0 &&\n ccw(t.pos, t.pos+t.dir, s.pos) *\n ccw(t.pos, t.pos+t.dir, s.pos+s.dir) <= 0);\n}\ndouble lp_distance(const L& l, const P& p) {\n return abs(outp(l.dir, p-l.pos) / abs(l.dir));\n}\ndouble ll_distance(const L& l, const L& m) {\n return (ll_intersects(l, m) ? 0 : lp_distance(l, m.pos));\n}\ndouble ls_distance(const L& l, const L& s) {\n if (ls_intersects(l, s))\n return 0;\n return min(lp_distance(l, s.pos), lp_distance(l, s.pos+s.dir));\n}\ndouble sp_distance(const L& s, const P& p) {\n const P r = perf(s, p);\n const double pos = ((r-s.pos)/s.dir).real();\n if (-EPS <= pos && pos <= 1 + EPS)\n return norm(r - p);\n return min(norm(s.pos - p),\n norm(s.pos+s.dir - p));\n}\ndouble ss_distance(const L& s, const L& t) {\n if (ss_intersects(s, t))\n return 0;\n return (max(sp_distance(s, t.pos), max(sp_distance(s, t.pos+t.dir), max(sp_distance(t, s.pos), sp_distance(t, s.pos+s.dir)))));\n}\n\n\nconst int INF = 1<<28;\nconst int MOD = 1000000007;\n\n\nint n;\n\ndouble dp[1<<10][11][25][25];\npii pp[11][11];\n\nmain(){\n\tint i,j;\n\twhile(cin >> n, n){\n\t\tREP(i, 1<<n)REP(j, n)REP(k, 22)REP(l, 22) dp[i][j][k][l] = 10000;\n\t\tdouble ans(10000);\n\t\tvector<P> p(n);\n\t\tREP(i, n){\n\t\t\tdouble x, y;\n\t\t\tcin >> x >> y;\n\t\t\tp[i] = P(x, y);\n\t\t}\n\t\tvector<pii> dr;\n\t\tREP(i, n)REP(j, i){\n\t\t\tP dir = p[j]-p[i];\n\t\t\tint g = __gcd((int)abs(dir.X), (int)abs(dir.Y));\n\t\t\tpp[i][j] = pii((int)dir.X/g, (int)dir.Y/g);\n\t\t\tpp[j][i] = pii(-(int)dir.X/g, -(int)dir.Y/g);\n\t\t\tdr.emplace_back((int)dir.X/g, (int)dir.Y/g);\n\t\t\tdr.emplace_back(-(int)dir.X/g, -(int)dir.Y/g);\n\t\t}\n\t\tsort(ALL(dr));\n\t\tUNIQUE(dr);\n\t\tREP(i, 1<<n)REP(j, n)REP(d, dr.size()){\n\t\t\tint dx = dr[d].first;\n\t\t\tint dy = dr[d].second;\n\t\t\tdouble &t = dp[i][j][dx+10][dy+10];\n\t\t\t\n\t\t\tif(i == (1<<j)) t = .0;\n\t\t\tif(t > 9000) continue;\n//\t\t\tprintf(\"dp[%d][%d][%d][%d] = (%d, %f)\\n\", i, j, dx, dy, t.first, t.second);\n\t\t\tif(i == (1<<n)-1){\n\t\t\t\tans = min(ans, t);\n\t\t\t\tcontinue;\n\t\t\t}\n\t\t\tL l1(p[j], P(dx*200.0, dy*200.0));\n\t\t\tREP(k, n){\n\t\t\t\tif(1&(i>>k)) continue;\n\t\t\t\tint b = i | (1<<k);\n\t\t\t\tif(signum(sp_distance(l1, p[k])) == 0){\n\t\t\t\t\tchmin(dp[b][k][dx+10][dy+10], t + abs(p[j]-p[k]));\n//\t\t\t\t\tprintf(\"!dq[%d][%d][%d][%d] = (%d, %f)\\n\", b, k, dx, dy, t.first, t.second + abs(p[j]-p[k]));\n\t\t\t\t\tcontinue;\n\t\t\t\t}\n\t\t\t\tif(chmin(dp[b][k][pp[j][k].first+10][pp[j][k].second+10], t + 1000 + abs(p[j]-p[k]))){\n//\t\t\t\t\tprintf(\"dq[%d][%d][%d][%d] = (%d, %f)\\n\", b, k, (int)dir.X/g, (int)dir.Y/g, t.first+1, t.second + abs(p[j]-p[k]));\n\t\t\t\t}\n\t\t\t\tREP(l, n){\n\t\t\t\t\tif(1&(b>>l)) continue;\n\t\t\t\t\tL l2(p[k], (p[k]-p[l])*200.0);\n\t\t\t\t\tif(!ss_intersects(l1, l2)) continue;\n\t\t\t\t\tP is = line_cross(l1, l2);\n\t\t\t\t\tchmin(dp[b][k][pp[k][l].first+10][pp[k][l].second+10], t + 1000 + abs(p[j]-is) + abs(is-p[k]));\n//\t\t\t\t\tif(chmin(dp[b][k][(int)dir.X/g+10][(int)dir.Y/g+10], pid(t.first + 1, t.second + abs(p[j]-is) + abs(is-p[k]))) && t.second + abs(p[j]-is) + abs(is-p[k]) < 18.48683298)\n//\t\t\t\t\t\tprintf(\"dq[%d][%d][%d][%d] = (%d, %f)\\n\", b, k, (int)dir.X/g, (int)dir.Y/g, t.first + 1, t.second + abs(p[j]-is) + abs(is-p[k]));\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\tint t = (int)(ans)/1000;\n\t\tprintf(\"%d %.9f\\n\", t, ans - t*1000);\n\t}\n\treturn 0;\n}", "accuracy": 1, "time_ms": 1010, "memory_kb": 56152, "score_of_the_acc": -1.1036, "final_rank": 16 }, { "submission_id": "aoj_1294_1009066", "code_snippet": "#include <cstdio>\n#include <cmath>\n#include <cstring>\n#include <cstdlib>\n#include <climits>\n#include <ctime>\n#include <queue>\n#include <stack>\n#include <algorithm>\n#include <list>\n#include <vector>\n#include <set>\n#include <map>\n#include <iostream>\n#include <deque>\n#include <complex>\n#include <string>\n#include <iomanip>\n#include <sstream>\n#include <bitset>\n#include <valarray>\n#include <iterator>\nusing namespace std;\ntypedef long long int ll;\ntypedef unsigned int uint;\ntypedef unsigned char uchar;\ntypedef unsigned long long ull;\ntypedef pair<int, int> pii;\ntypedef pair<int, double> pid;\ntypedef vector<int> vi;\n\n#define REP(i,x) for(int i=0;i<(int)(x);i++)\n#define FOR(i,c) for(__typeof((c).begin())i=(c).begin();i!=(c).end();i++)\n#define RREP(i,x) for(int i=((int)(x)-1);i>=0;i--)\n#define RFOR(i,c) for(__typeof((c).rbegin())i=(c).rbegin();i!=(c).rend();i++)\n#define ALL(container) container.begin(), container.end()\n#define RALL(container) container.rbegin(), container.rend()\n#define SZ(container) ((int)container.size())\n#define mp(a,b) make_pair(a, b)\n#define UNIQUE(v) v.erase( unique(v.begin(), v.end()), v.end() );\n\ntemplate<class T> bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }\ntemplate<class T> bool chmin(T &a, const T &b) { if (a>b) { a=b; return 1; } return 0; }\ntemplate<class T> ostream& operator<<(ostream &os, const vector<T> &t) {\nos<<\"[\"; FOR(it,t) {if(it!=t.begin()) os<<\",\"; os<<*it;} os<<\"]\"; return os;\n}\ntemplate<class T> ostream& operator<<(ostream &os, const set<T> &t) {\nos<<\"{\"; FOR(it,t) {if(it!=t.begin()) os<<\",\"; os<<*it;} os<<\"}\"; return os;\n}\ntemplate<class S, class T> ostream& operator<<(ostream &os, const pair<S,T> &t) { return os<<\"(\"<<t.first<<\",\"<<t.second<<\")\";}\n#include <complex>\n#include <cmath>\n#define EPS 1.0e-10\n#define PI 3.1415926535897932384 \n\n// 実数の符号関数\ninline int signum(double x) { return (abs(x) < EPS ? 0 : x > 0 ? 1 : -1); }\n//XY座標\n#define X real()\n#define Y imag()\n// 点\ntypedef complex<double> P;\n \n\n// 線分・半直線・直線\nstruct L { \n\tP pos, dir;\n\tL(){}\n\tL(P p, P d):pos(p),dir(d){}\n};\n \n// 多角形\ntypedef vector<P> G;\n \n// 円\nstruct C { P p; double r; };\n\n// std::norm はabs(p)*abs(p)なので遅い\ninline double norm(P p){\n\treturn p.X*p.X+p.Y*p.Y;\n}\n\n// 二つのベクトルの内積を計算する\ninline double inp(const P& a, const P& b) {\n\treturn (conj(a)*b).real();\n}\n \n// 二つのベクトルの外積を計算する\ninline double outp(const P& a, const P& b) {\n\treturn (conj(a)*b).imag();\n}\n\ninline int ccw(const P& p, const P& r, const P& s) {\n P a(r-p), b(s-p);\n int sgn = signum(outp(a, b));\n if (sgn != 0)\n return sgn;\n if (a.real()*b.real() < -EPS || a.imag()*b.imag() < -EPS)\n return -1;\n if (norm(a) < norm(b) - EPS)\n return 1;\n return 0;\n}\n\n// ベクトルpをベクトルbに射影したベクトルを計算する\ninline P proj(const P& p, const P& b) {\n\treturn b*inp(p,b)/norm(b);\n}\n \n// 点pから直線lに引いた垂線の足となる点を計算する\ninline P perf(const L& l, const P& p) {\n\tL m(l.pos - p, l.dir);\n\treturn (p + (m.pos - proj(m.pos, m.dir)));\n}\n \n// 線分sを直線bに射影した線分を計算する\ninline L proj(const L& s, const L& b) {\n\t return L(perf(b, s.pos), proj(s.dir, b.dir));\n}\n\nbool ll_intersects(const L& l, const L& m) {\n return (abs(outp(l.dir, m.dir)) > EPS || abs(outp(l.dir, m.pos-l.pos)) < EPS);\n}\nP line_cross(const L& l, const L& m) {\n double num = outp(m.dir, m.pos-l.pos);\n double denom = outp(m.dir, l.dir);\n return P(l.pos + l.dir*num/denom);\n}\nbool ls_intersects(const L& l, const L& s) {\n return (signum(outp(l.dir, s.pos-l.pos)) *\n signum(outp(l.dir, s.pos+s.dir-l.pos)) <= 0);\n}\nbool sp_intersects(const L& s, const P& p) {\n return ( abs(s.pos - p) + abs(s.pos + s.dir - p) - abs(s.dir) < EPS );\n}\nbool ss_intersects(const L& s, const L& t) {\n return (ccw(s.pos, s.pos+s.dir, t.pos) *\n ccw(s.pos, s.pos+s.dir, t.pos+t.dir) <= 0 &&\n ccw(t.pos, t.pos+t.dir, s.pos) *\n ccw(t.pos, t.pos+t.dir, s.pos+s.dir) <= 0);\n}\ndouble lp_distance(const L& l, const P& p) {\n return abs(outp(l.dir, p-l.pos) / abs(l.dir));\n}\ndouble ll_distance(const L& l, const L& m) {\n return (ll_intersects(l, m) ? 0 : lp_distance(l, m.pos));\n}\ndouble ls_distance(const L& l, const L& s) {\n if (ls_intersects(l, s))\n return 0;\n return min(lp_distance(l, s.pos), lp_distance(l, s.pos+s.dir));\n}\ndouble sp_distance(const L& s, const P& p) {\n const P r = perf(s, p);\n const double pos = ((r-s.pos)/s.dir).real();\n if (-EPS <= pos && pos <= 1 + EPS)\n return norm(r - p);\n return min(norm(s.pos - p),\n norm(s.pos+s.dir - p));\n}\ndouble ss_distance(const L& s, const L& t) {\n if (ss_intersects(s, t))\n return 0;\n return (max(sp_distance(s, t.pos), max(sp_distance(s, t.pos+t.dir), max(sp_distance(t, s.pos), sp_distance(t, s.pos+s.dir)))));\n}\n\n\nconst int INF = 1<<28;\nconst int MOD = 1000000007;\n\n\nint n;\n\ndouble dp[1<<10][11][25][25];\nmain(){\n\tint i,j;\n\twhile(cin >> n, n){\n\t\tREP(i, 1<<n)REP(j, n)REP(k, 22)REP(l, 22) dp[i][j][k][l] = 10000;\n\t\tdouble ans(10000);\n\t\tvector<P> p(n);\n\t\tREP(i, n){\n\t\t\tdouble x, y;\n\t\t\tcin >> x >> y;\n\t\t\tp[i] = P(x, y);\n\t\t}\n\t\tvector<pii> dr;\n\t\tREP(i, n)REP(j, i){\n\t\t\tP dir = p[i]-p[j];\n\t\t\tint g = __gcd((int)abs(dir.X), (int)abs(dir.Y));\n\t\t\tdr.emplace_back((int)dir.X/g, (int)dir.Y/g);\n\t\t\tdr.emplace_back(-(int)dir.X/g, (int)dir.Y/g);\n\t\t\tdr.emplace_back((int)dir.X/g, -(int)dir.Y/g);\n\t\t\tdr.emplace_back(-(int)dir.X/g, -(int)dir.Y/g);\n\t\t}\n\t\tsort(ALL(dr));\n\t\tUNIQUE(dr);\n\t\tREP(i, 1<<n)REP(j, n)REP(d, dr.size()){\n\t\t\tint dx = dr[d].first;\n\t\t\tint dy = dr[d].second;\n\t\t\tdouble &t = dp[i][j][dx+10][dy+10];\n\t\t\t\n\t\t\tif(i == (1<<j)) t = .0;\n\t\t\tif(t > 9000) continue;\n//\t\t\tprintf(\"dp[%d][%d][%d][%d] = (%d, %f)\\n\", i, j, dx, dy, t.first, t.second);\n\t\t\tif(i == (1<<n)-1){\n\t\t\t\tans = min(ans, t);\n\t\t\t\tcontinue;\n\t\t\t}\n\t\t\tL l1(p[j], P(dx*200.0, dy*200.0));\n\t\t\tREP(k, n){\n\t\t\t\tif(1&(i>>k)) continue;\n\t\t\t\tint b = i | (1<<k);\n\t\t\t\tif(signum(sp_distance(l1, p[k])) == 0){\n\t\t\t\t\tchmin(dp[b][k][dx+10][dy+10], t + abs(p[j]-p[k]));\n//\t\t\t\t\tprintf(\"!dq[%d][%d][%d][%d] = (%d, %f)\\n\", b, k, dx, dy, t.first, t.second + abs(p[j]-p[k]));\n\t\t\t\t\tcontinue;\n\t\t\t\t}\n\t\t\t\tP dir = p[k] - p[j];\n\t\t\t\tint g = __gcd((int)abs(dir.X), (int)abs(dir.Y));\n\t\t\t\tif(chmin(dp[b][k][(int)dir.X/g+10][(int)dir.Y/g+10], t + 1000 + abs(p[j]-p[k]))){\n//\t\t\t\t\tprintf(\"dq[%d][%d][%d][%d] = (%d, %f)\\n\", b, k, (int)dir.X/g, (int)dir.Y/g, t.first+1, t.second + abs(p[j]-p[k]));\n\t\t\t\t}\n\t\t\t\tREP(l, n){\n\t\t\t\t\tif(1&(b>>l)) continue;\n\t\t\t\t\tL l2(p[k], (p[k]-p[l])*200.0);\n\t\t\t\t\tif(!ss_intersects(l1, l2)) continue;\n\t\t\t\t\tP is = line_cross(l1, l2);\n\t\t\t\t\tP dir = p[l] - p[k];\n\t\t\t\t\tint g = __gcd((int)(abs(dir.X)+EPS), (int)(abs(dir.Y)+EPS));\n\t\t\t\t\tchmin(dp[b][k][(int)dir.X/g+10][(int)dir.Y/g+10], t + 1000 + abs(p[j]-is) + abs(is-p[k]));\n//\t\t\t\t\tif(chmin(dp[b][k][(int)dir.X/g+10][(int)dir.Y/g+10], pid(t.first + 1, t.second + abs(p[j]-is) + abs(is-p[k]))) && t.second + abs(p[j]-is) + abs(is-p[k]) < 18.48683298)\n//\t\t\t\t\t\tprintf(\"dq[%d][%d][%d][%d] = (%d, %f)\\n\", b, k, (int)dir.X/g, (int)dir.Y/g, t.first + 1, t.second + abs(p[j]-is) + abs(is-p[k]));\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\tint t = (int)(ans)/1000;\n\t\tprintf(\"%d %.9f\\n\", t, ans - t*1000);\n\t}\n\treturn 0;\n}", "accuracy": 1, "time_ms": 1120, "memory_kb": 56156, "score_of_the_acc": -1.1178, "final_rank": 17 }, { "submission_id": "aoj_1294_1009054", "code_snippet": "#include <cstdio>\n#include <cmath>\n#include <cstring>\n#include <cstdlib>\n#include <climits>\n#include <ctime>\n#include <queue>\n#include <stack>\n#include <algorithm>\n#include <list>\n#include <vector>\n#include <set>\n#include <map>\n#include <iostream>\n#include <deque>\n#include <complex>\n#include <string>\n#include <iomanip>\n#include <sstream>\n#include <bitset>\n#include <valarray>\n#include <iterator>\nusing namespace std;\ntypedef long long int ll;\ntypedef unsigned int uint;\ntypedef unsigned char uchar;\ntypedef unsigned long long ull;\ntypedef pair<int, int> pii;\ntypedef pair<int, double> pid;\ntypedef vector<int> vi;\n\n#define REP(i,x) for(int i=0;i<(int)(x);i++)\n#define FOR(i,c) for(__typeof((c).begin())i=(c).begin();i!=(c).end();i++)\n#define RREP(i,x) for(int i=((int)(x)-1);i>=0;i--)\n#define RFOR(i,c) for(__typeof((c).rbegin())i=(c).rbegin();i!=(c).rend();i++)\n#define ALL(container) container.begin(), container.end()\n#define RALL(container) container.rbegin(), container.rend()\n#define SZ(container) ((int)container.size())\n#define mp(a,b) make_pair(a, b)\n#define UNIQUE(v) v.erase( unique(v.begin(), v.end()), v.end() );\n\ntemplate<class T> bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }\ntemplate<class T> bool chmin(T &a, const T &b) { if (a>b) { a=b; return 1; } return 0; }\ntemplate<class T> ostream& operator<<(ostream &os, const vector<T> &t) {\nos<<\"[\"; FOR(it,t) {if(it!=t.begin()) os<<\",\"; os<<*it;} os<<\"]\"; return os;\n}\ntemplate<class T> ostream& operator<<(ostream &os, const set<T> &t) {\nos<<\"{\"; FOR(it,t) {if(it!=t.begin()) os<<\",\"; os<<*it;} os<<\"}\"; return os;\n}\ntemplate<class S, class T> ostream& operator<<(ostream &os, const pair<S,T> &t) { return os<<\"(\"<<t.first<<\",\"<<t.second<<\")\";}\n#include <complex>\n#include <cmath>\n#define EPS 1.0e-10\n#define PI 3.1415926535897932384 \n\n// 実数の符号関数\ninline int signum(double x) { return (abs(x) < EPS ? 0 : x > 0 ? 1 : -1); }\n//XY座標\n#define X real()\n#define Y imag()\n// 点\ntypedef complex<double> P;\n \n\n// 線分・半直線・直線\nstruct L { \n\tP pos, dir;\n\tL(){}\n\tL(P p, P d):pos(p),dir(d){}\n};\n \n// 多角形\ntypedef vector<P> G;\n \n// 円\nstruct C { P p; double r; };\n\n// std::norm はabs(p)*abs(p)なので遅い\ninline double norm(P p){\n\treturn p.X*p.X+p.Y*p.Y;\n}\n\n// 二つのベクトルの内積を計算する\ninline double inp(const P& a, const P& b) {\n\treturn (conj(a)*b).real();\n}\n \n// 二つのベクトルの外積を計算する\ninline double outp(const P& a, const P& b) {\n\treturn (conj(a)*b).imag();\n}\n\ninline int ccw(const P& p, const P& r, const P& s) {\n P a(r-p), b(s-p);\n int sgn = signum(outp(a, b));\n if (sgn != 0)\n return sgn;\n if (a.real()*b.real() < -EPS || a.imag()*b.imag() < -EPS)\n return -1;\n if (norm(a) < norm(b) - EPS)\n return 1;\n return 0;\n}\n\n// ベクトルpをベクトルbに射影したベクトルを計算する\ninline P proj(const P& p, const P& b) {\n\treturn b*inp(p,b)/norm(b);\n}\n \n// 点pから直線lに引いた垂線の足となる点を計算する\ninline P perf(const L& l, const P& p) {\n\tL m(l.pos - p, l.dir);\n\treturn (p + (m.pos - proj(m.pos, m.dir)));\n}\n \n// 線分sを直線bに射影した線分を計算する\ninline L proj(const L& s, const L& b) {\n\t return L(perf(b, s.pos), proj(s.dir, b.dir));\n}\n\nbool ll_intersects(const L& l, const L& m) {\n return (abs(outp(l.dir, m.dir)) > EPS || abs(outp(l.dir, m.pos-l.pos)) < EPS);\n}\nP line_cross(const L& l, const L& m) {\n double num = outp(m.dir, m.pos-l.pos);\n double denom = outp(m.dir, l.dir);\n return P(l.pos + l.dir*num/denom);\n}\nbool ls_intersects(const L& l, const L& s) {\n return (signum(outp(l.dir, s.pos-l.pos)) *\n signum(outp(l.dir, s.pos+s.dir-l.pos)) <= 0);\n}\nbool sp_intersects(const L& s, const P& p) {\n return ( abs(s.pos - p) + abs(s.pos + s.dir - p) - abs(s.dir) < EPS );\n}\nbool ss_intersects(const L& s, const L& t) {\n return (ccw(s.pos, s.pos+s.dir, t.pos) *\n ccw(s.pos, s.pos+s.dir, t.pos+t.dir) <= 0 &&\n ccw(t.pos, t.pos+t.dir, s.pos) *\n ccw(t.pos, t.pos+t.dir, s.pos+s.dir) <= 0);\n}\ndouble lp_distance(const L& l, const P& p) {\n return abs(outp(l.dir, p-l.pos) / abs(l.dir));\n}\ndouble ll_distance(const L& l, const L& m) {\n return (ll_intersects(l, m) ? 0 : lp_distance(l, m.pos));\n}\ndouble ls_distance(const L& l, const L& s) {\n if (ls_intersects(l, s))\n return 0;\n return min(lp_distance(l, s.pos), lp_distance(l, s.pos+s.dir));\n}\ndouble sp_distance(const L& s, const P& p) {\n const P r = perf(s, p);\n const double pos = ((r-s.pos)/s.dir).real();\n if (-EPS <= pos && pos <= 1 + EPS)\n return norm(r - p);\n return min(norm(s.pos - p),\n norm(s.pos+s.dir - p));\n}\ndouble ss_distance(const L& s, const L& t) {\n if (ss_intersects(s, t))\n return 0;\n return (max(sp_distance(s, t.pos), max(sp_distance(s, t.pos+t.dir), max(sp_distance(t, s.pos), sp_distance(t, s.pos+s.dir)))));\n}\n\n\nconst int INF = 1<<28;\nconst int MOD = 1000000007;\n\n\nint n;\n\ndouble dp[1<<10][11][25][25];\nmain(){\n\tint i,j;\n\twhile(cin >> n, n){\n\t\tREP(i, 1<<n)REP(j, n)REP(k, 22)REP(l, 22) dp[i][j][k][l] = 10000;\n\t\tdouble ans(10000);\n\t\tvector<P> p(n);\n\t\tREP(i, n){\n\t\t\tdouble x, y;\n\t\t\tcin >> x >> y;\n\t\t\tp[i] = P(x, y);\n\t\t}\n\t\tvector<pii> dr;\n\t\tREP(i, n)REP(j, i){\n\t\t\tP dir = p[i]-p[j];\n\t\t\tint g = __gcd((int)abs(dir.X), (int)abs(dir.Y));\n\t\t\tdr.emplace_back((int)dir.X/g, (int)dir.Y/g);\n\t\t\tdr.emplace_back(-(int)dir.X/g, (int)dir.Y/g);\n\t\t\tdr.emplace_back((int)dir.X/g, -(int)dir.Y/g);\n\t\t\tdr.emplace_back(-(int)dir.X/g, -(int)dir.Y/g);\n\t\t}\n\t\tREP(i, 1<<n)REP(j, n)REP(d, dr.size()){\n\t\t\tint dx = dr[d].first;\n\t\t\tint dy = dr[d].second;\n\t\t\tdouble &t = dp[i][j][dx+10][dy+10];\n\t\t\t\n\t\t\tif(i == (1<<j)) t = .0;\n\t\t\tif(t > 9000) continue;\n//\t\t\tprintf(\"dp[%d][%d][%d][%d] = (%d, %f)\\n\", i, j, dx, dy, t.first, t.second);\n\t\t\tif(i == (1<<n)-1){\n\t\t\t\tans = min(ans, t);\n\t\t\t\tcontinue;\n\t\t\t}\n\t\t\tL l1(p[j], P(dx*200.0, dy*200.0));\n\t\t\tREP(k, n){\n\t\t\t\tif(1&(i>>k)) continue;\n\t\t\t\tint b = i | (1<<k);\n\t\t\t\tif(signum(sp_distance(l1, p[k])) == 0){\n\t\t\t\t\tchmin(dp[b][k][dx+10][dy+10], t + abs(p[j]-p[k]));\n//\t\t\t\t\tprintf(\"!dq[%d][%d][%d][%d] = (%d, %f)\\n\", b, k, dx, dy, t.first, t.second + abs(p[j]-p[k]));\n\t\t\t\t\tcontinue;\n\t\t\t\t}\n\t\t\t\tP dir = p[k] - p[j];\n\t\t\t\tint g = __gcd((int)abs(dir.X), (int)abs(dir.Y));\n\t\t\t\tif(chmin(dp[b][k][(int)dir.X/g+10][(int)dir.Y/g+10], t + 1000 + abs(p[j]-p[k]))){\n//\t\t\t\t\tprintf(\"dq[%d][%d][%d][%d] = (%d, %f)\\n\", b, k, (int)dir.X/g, (int)dir.Y/g, t.first+1, t.second + abs(p[j]-p[k]));\n\t\t\t\t}\n\t\t\t\tREP(l, n){\n\t\t\t\t\tif(1&(b>>l)) continue;\n\t\t\t\t\tL l2(p[k], (p[k]-p[l])*200.0);\n\t\t\t\t\tif(!ss_intersects(l1, l2)) continue;\n\t\t\t\t\tP is = line_cross(l1, l2);\n\t\t\t\t\tP dir = p[l] - p[k];\n\t\t\t\t\tint g = __gcd((int)(abs(dir.X)+EPS), (int)(abs(dir.Y)+EPS));\n\t\t\t\t\tchmin(dp[b][k][(int)dir.X/g+10][(int)dir.Y/g+10], t + 1000 + abs(p[j]-is) + abs(is-p[k]));\n//\t\t\t\t\tif(chmin(dp[b][k][(int)dir.X/g+10][(int)dir.Y/g+10], pid(t.first + 1, t.second + abs(p[j]-is) + abs(is-p[k]))) && t.second + abs(p[j]-is) + abs(is-p[k]) < 18.48683298)\n//\t\t\t\t\t\tprintf(\"dq[%d][%d][%d][%d] = (%d, %f)\\n\", b, k, (int)dir.X/g, (int)dir.Y/g, t.first + 1, t.second + abs(p[j]-is) + abs(is-p[k]));\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\tint t = (int)(ans)/1000;\n\t\tprintf(\"%d %.9f\\n\", t, ans - t*1000);\n\t}\n\treturn 0;\n}", "accuracy": 1, "time_ms": 4200, "memory_kb": 56172, "score_of_the_acc": -1.5135, "final_rank": 20 }, { "submission_id": "aoj_1294_781473", "code_snippet": "#include <iostream>\n#include <cstdio>\n#include <cstring>\n#include <algorithm>\n#include <cmath>\n#include <vector>\n\nusing namespace std;\n\nstruct point_t {\n\tdouble x, y;\n\tpoint_t() { x = y = 0; }\n\tpoint_t(double tx, double ty) { x = tx, y = ty; }\n\tpoint_t operator+(const point_t &r) const {\n\t\treturn point_t(x + r.x, y + r.y);\n\t}\n\tpoint_t operator-(const point_t &r) const {\n\t\treturn point_t(x - r.x, y - r.y);\n\t}\n\tpoint_t operator*(const double &r) const {\n\t\treturn point_t(x * r, y * r);\n\t}\n\tpoint_t operator/(const double &r) const {\n\t\treturn point_t(x / r, y / r);\n\t}\n\tdouble l() const {\n\t\treturn sqrt(x * x + y * y);\n\t}\n\tvoid read() { scanf(\"%lf%lf\", &x, &y); }\n};\n\nconst double eps = 1e-8;\nint dblcmp(double x) {\n\treturn x < -eps ? -1 : x > eps;\n}\n\ndouble dot(point_t p1, point_t p2) {\n\treturn p1.x * p2.x + p1.y * p2.y;\n}\n\ndouble cross(point_t p1, point_t p2) {\n\treturn p1.x * p2.y - p1.y * p2.x;\n}\n\nbool onseg(point_t p1, point_t p2, point_t p) {\n\tif (dblcmp(cross(p - p1, p2 - p1)) != 0) return false;\n\treturn dblcmp(dot(p1 - p, p2 - p)) <= 0;\n}\n\ndouble dist(point_t p1, point_t p2) {\n\tp2 = p2 - p1;\n\treturn p2.l();\n}\n\nconst int maxn = 11;\nint n, final;\npoint_t p[maxn];\nint kill[maxn][maxn];\n\npoint_t isLL(point_t a, point_t b, point_t c, point_t d, bool &parell) {\n\tpoint_t p1 = b - a, p2 = d - c;\n\tdouble a1 = p1.y, b1 = -p1.x, c1;\n\tdouble a2 = p2.y, b2 = -p2.x, c2;\n\tif (dblcmp(a1 * b2 - a2 * b1) == 0) {\n\t\tparell = true;\n\t\treturn point_t();\n\t} else {\n\t\tparell = false;\n\t\tc1 = a1 * a.x + b1 * a.y;\n\t\tc2 = a2 * c.x + b2 * c.y;\n\t\treturn point_t((c1 * b2 - c2 * b1) / (a1 * b2 - a2 * b1), (c1 * a2 - c2 * a1) / (b1 * a2 - b2 * a1));\n\t}\n}\n\ndouble getscale(point_t v1, point_t v2) {\n\tif (dblcmp(v1.y) == 0) return v2.x / v1.x;\n\treturn v2.y / v1.y;\n}\n\npair<int, double> dp[maxn][maxn][1 << maxn];\n\nvoid work() {\n\tfor (int i = 1; i <= n; ++i) {\n\t\tfor (int j = 1; j <= n; ++j) {\n\t\t\tfor (int mask = 0; mask <= final; ++mask) {\n\t\t\t\tdp[i][j][mask].first = 100;\n\t\t\t}\n\t\t}\n\t}\n\tfor (int i = 1; i <= n; ++i) {\n\t\tfor (int j = 1; j <= n; ++j) {\n\t\t\tdp[i][j][kill[i][j]] = make_pair(0, dist(p[i], p[j]));\n\t\t}\n\t}\n\tfor (int mask = 0; mask <= final; ++mask) {\n\t\tfor (int v = 1; v <= n; ++v) {\n\t\t\tfor (int u = 1; u <= n; ++u) {\n\t\t\t\tif (dp[u][v][mask].first == 100) continue;\n\t\t\t\tpoint_t dir = p[u] - p[v];\n\t\t\t\tfor (int i = 1; i <= n; ++i) {\n\t\t\t\t\tfor (int j = 1; j <= n; ++j) {\n\t\t\t\t\t\tif (i == j) continue;\n\t\t\t\t\t\tbool parell = false;\n\t\t\t\t\t\tpoint_t is = isLL(p[u], p[u] + dir, p[i], p[j], parell);\n\t\t\t\t\t\tif (parell) continue;\n\t\t\t\t\t\tdouble len2 = dp[u][v][mask].second;\n\t\t\t\t\t\tdouble s1 = getscale(dir, is - p[u]);\n\t\t\t\t\t\tdouble s2 = getscale(p[j] - p[i], is - p[i]);\n\t\t\t\t\t\tif (dblcmp(s1) < 0 || dblcmp(s2 - 1) < 0) continue;\n\t\t\t\t\t\tlen2 += dist(is, p[u]);\n\t\t\t\t\t\tlen2 += dist(p[i], is);\n\t\t\t\t\t\tpair<int, double> &bind = dp[i][j][mask | kill[i][j]];\n\t\t\t\t\t\tbind = min(bind, make_pair(dp[u][v][mask].first + 1, len2));\t\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n}\n\nint main() {\n\twhile (scanf(\"%d\", &n) != EOF) {\n\t\tif (n == 0) break;\n\t\tfinal = (1 << n) - 1;\n\t\tfor (int i = 1; i <= n; ++i) p[i].read();\n\t\tfor (int i = 1; i <= n; ++i) {\n\t\t\tfor (int j = 1; j <= n; ++j) {\n\t\t\t\tint mask = 0;\n\t\t\t\tfor (int k = 1; k <= n; ++k) {\n\t\t\t\t\tif (onseg(p[i], p[j], p[k])) mask |= (1 << (k - 1));\n\t\t\t\t}\n\t\t\t\tkill[i][j] = mask;\n\t\t\t}\n\t\t}\n\t\twork();\n\t\tpair<int, double> ans = make_pair(100, 0);\n\t\tfor (int i = 1; i <= n; ++i) {\n\t\t\tfor (int j = 1; j <= n; ++j) {\n\t\t\t\tif (i == j) continue;\n\t\t\t\tans = min(ans, dp[i][j][final]);\n\t\t\t}\n\t\t}\n\t\tprintf(\"%d %.10lf\\n\", ans.first, ans.second);\n\t}\n\treturn 0;\n}", "accuracy": 1, "time_ms": 1180, "memory_kb": 5120, "score_of_the_acc": -0.1655, "final_rank": 5 } ]
aoj_1299_cpp
Problem E: Origami Through-Hole Origami is the traditional Japanese art of paper folding. One day, Professor Egami found the message board decorated with some pieces of origami works pinned on it, and became interested in the pinholes on the origami paper. Your mission is to simulate paper folding and pin punching on the folded sheet, and calculate the number of pinholes on the original sheet when unfolded. A sequence of folding instructions for a flat and square piece of paper and a single pinhole position are specified. As a folding instruction, two points P and Q are given. The paper should be folded so that P touches Q from above (Figure 4). To make a fold, we first divide the sheet into two segments by creasing the sheet along the folding line , i.e., the perpendicular bisector of the line segment PQ , and then turn over the segment containing P onto the other. You can ignore the thickness of the paper. Figure 4: Simple case of paper folding The original flat square piece of paper is folded into a structure consisting of layered paper segments, which are connected by linear hinges. For each instruction, we fold one or more paper segments along the specified folding line, dividing the original segments into new smaller ones. The folding operation turns over some of the paper segments (not only the new smaller segments but also some other segments that have no intersection with the folding line) to the reflective position against the folding line. That is, for a paper segment that intersects with the folding line, one of the two new segments made by dividing the original is turned over; for a paper segment that does not intersect with the folding line, the whole segment is simply turned over. The folding operation is carried out repeatedly applying the following rules, until we have no segment to turn over. Rule 1: The uppermost segment that contains P must be turned over. Rule 2: If a hinge of a segment is moved to the other side of the folding line by the operation, any segment that shares the same hinge must be turned over. Rule 3: If two paper segments overlap and the lower segment is turned over, the upper segment must be turned over too. In the examples shown in Figure 5, (a) and (c) show cases where only Rule 1 is applied. (b) shows a case where Rule 1 and 2 are applied to turn over two paper segments connected by a hinge, and (d) shows a case where Rule 1, 3 and 2 are applied to turn over three paper segments. Figure 5: Different cases of folding After processing all the folding instructions, the pinhole goes through all the layered segments of paper at that position. In the case of Figure 6, there are three pinholes on the unfolded sheet of paper. Figure 6: Number of pinholes on the unfolded sheet Input The input is a sequence of datasets. The end of the input is indicated by a line containing a zero. Each dataset is formatted as follows. k p x 1 p y 1 q x 1 q y 1 . . . p x k p y k q x k q y k h x h y For all datasets, the size of t ...(truncated)
[ { "submission_id": "aoj_1299_3228821", "code_snippet": "#include <iostream>\n#include <cstdio>\n#include <iomanip>\n#include <complex>\n#include <vector>\n#include <algorithm>\n#include <cmath>\n#include <array>\nusing namespace std;\nconst double EPS = 1e-8;\nconst double INF = 1e12;\n#define EQ(n,m) (abs((n)-(m)) < EPS)\n#define X real()\n#define Y imag()\ntypedef complex<double> P;\ntypedef vector<P> VP;\nstruct L : array<P, 2>{\n L(const P& a, const P& b){ at(0)=a; at(1)=b; }\n L(){}\n};\n\nstruct poly_connect{\n VP v;\n vector<int> c;\n};\n\ndouble dot(P a, P b){\n return (conj(a)*b).X;\n}\ndouble cross(P a, P b){\n return (conj(a)*b).Y;\n}\nint ccw(P a, P b, P c){\n b -= a;\n c -= a;\n if(cross(b,c) > EPS) return +1; //ccw\n if(cross(b,c) < -EPS) return -1; //cw\n if(dot(b,c) < -EPS) return +2; //c-a-b\n if(abs(c)-abs(b) > EPS) return -2; //a-b-c\n return 0; //a-c-b\n}\n\nbool intersectSP(const L& s, const P &p){\n return abs(cross(s[0]-p, s[1]-p))<EPS && dot(s[0]-p, s[1]-p)<EPS;\n}\nP projection(const L& l, const P& p) {\n double t = dot(p-l[0], l[0]-l[1]) / norm(l[0]-l[1]);\n return l[0] + t*(l[0]-l[1]);\n}\nP reflection(const L& l, const P& p) {\n return p + 2.0*(projection(l, p) -p);\n}\nP crosspointLL(const L &l, const L &m) {\n double A = cross(l[1]-l[0], m[1]-m[0]);\n double B = cross(l[1]-l[0], l[1]-m[0]);\n return m[0] + B/A *(m[1]-m[0]);\n}\nint in_poly(const P &p, const VP &poly){\n int n = poly.size();\n int ret = -1;\n for(int i=0; i<n; i++){\n P a = poly[i]-p;\n P b = poly[(i+1)%n]-p;\n if(a.Y > b.Y) swap(a,b);\n if(intersectSP(L(a,b), P(0,0))) return 0;\n if(a.Y<=0 && b.Y>0 && cross(a,b)<0) ret = -ret;\n }\n return ret;\n}\nbool convex_overlap(const VP &a, const VP &b){\n for(P p : a){\n if(in_poly(p, b) > 0) return true;\n }\n for(P p : b){\n if(in_poly(p, a) > 0) return true;\n }\n if(in_poly((a[0] +a[1] +a[2])/3.0, b) > 0 ||\n in_poly((b[0] +b[1] +b[2])/3.0, a) > 0){\n return true;\n }\n return false;\n}\n\npoly_connect fold_convex(int numl, int idx, poly_connect pc, const L& l){\n poly_connect ret;\n int n = pc.v.size();\n for(int i=0; i<n; i++){\n P curr = pc.v[i];\n P next = pc.v[(i+1)%n];\n if(ccw(l[0], l[1], curr) != -1){\n ret.v.push_back(curr);\n ret.c.push_back(pc.c[i]);\n }\n if(ccw(l[0], l[1], curr) *ccw(l[0], l[1], next) == -1){\n ret.v.push_back(crosspointLL(L(curr, next), l));\n if(ccw(l[0], l[1], curr) != -1){\n ret.c.push_back(2*numl-1-idx);\n }else{\n ret.c.push_back(pc.c[i]);\n }\n }\n }\n return ret;\n}\n\npoly_connect reverse_layer(int numl, poly_connect pc, const L &l){\n int n = pc.v.size();\n if(n == 0) return pc;\n for(int i=0; i<n; i++){\n pc.v[i] = reflection(l, pc.v[i]);\n if(pc.c[i] != -1){\n pc.c[i] = 2*numl -1 -pc.c[i];\n }\n }\n reverse(pc.v.begin()+1, pc.v.end());\n reverse(pc.c.begin(), pc.c.end());\n return pc;\n}\n\nvoid recursive_fold(int numl, int idx, vector<poly_connect> &ulayer, vector<bool> &used){\n if(idx >= numl || used[idx]) return;\n used[idx] = true;\n for(int connect : ulayer[idx].c){\n if(connect != -1){\n recursive_fold(numl, connect, ulayer, used);\n }\n }\n for(int i=idx+1; i<numl; i++){\n if(!ulayer[i].v.empty() && convex_overlap(ulayer[idx].v, ulayer[i].v)){\n recursive_fold(numl, i, ulayer, used);\n }\n }\n}\n\nvoid fold(int numl, vector<poly_connect> &layer, const P &p, const P &q){\n vector<poly_connect> dlayer(512), ulayer(512);\n P mid = (p +q) /2.0;\n L bisec(mid, mid +(q -p) *P(0, -1)); //残す方\n L rbisec(bisec[1], bisec[0]);\n for(int i=0; i<numl; i++){\n dlayer[i] = fold_convex(numl, i, layer[i], bisec);\n ulayer[i] = fold_convex(numl, i, layer[i], rbisec);\n }\n vector<bool> used(512, false);\n for(int i=numl-1; i>=0; i--){\n //一番上にあるlayerから再帰的に始める\n if(in_poly(p, layer[i].v) > 0){\n recursive_fold(numl, i, ulayer, used);\n break;\n }\n }\n for(int i=0; i<numl; i++){\n if(used[i]){\n layer[i] = dlayer[i];\n layer[2*numl-1-i] = reverse_layer(numl, ulayer[i], bisec);\n }\n }\n}\n\nint main(){\n while(1){\n int n;\n cin >> n;\n if(n == 0) break;\n\n VP p(n), q(n);\n for(int i=0; i<n; i++){\n int px,py, qx,qy;\n cin >> px >> py >> qx >> qy;\n p[i] = P(px, py);\n q[i] = P(qx, qy);\n }\n int hx,hy;\n cin >> hx >> hy;\n P h(hx, hy);\n\n vector<poly_connect> layer(1024);\n layer[0].v = VP{P(0, 0), P(100, 0), P(100, 100), P(0, 100)};\n layer[0].c = vector<int>{-1, -1, -1, -1};\n for(int i=0; i<n; i++){ \n fold(1<<i, layer, p[i], q[i]);\n }\n int ans = 0;\n for(int i=0; i<(1<<n); i++){\n if(in_poly(h, layer[i].v) > 0){\n ans++;\n }\n }\n cout << ans << endl;\n }\n return 0;\n}", "accuracy": 1, "time_ms": 50, "memory_kb": 3416, "score_of_the_acc": -0.0485, "final_rank": 3 }, { "submission_id": "aoj_1299_1468314", "code_snippet": "#include<cstdio>\n#include<vector>\n#include<complex>\n#include<queue>\n#include<utility>\n#include<algorithm>\n\nusing namespace std;\n\ntypedef double Real;\ntypedef complex<Real> Point;\ntypedef pair<Point,Point> Line;\ntypedef pair<Point,Point> Segment;\ntypedef vector<Point> Polygon;\n\nconst Real eps=1e-7;\nconst Point NPoint=Point(NAN,NAN);\nconst Segment NSeg=Segment(NPoint,NPoint);\n\nvoid print(Point p,char ch='\\n'){\n\t//printf(\"(%f %f)%c\",p.real(),p.imag(),ch);\n}\n\nvoid print(Polygon poly){\n\t/*if(poly.size()==0){\n\t\tprintf(\"[empty]\\n\");\n\t\treturn ;\n\t}\n\tfor(int i=0;i<poly.size();i++){\n\t\tprint(poly[i],' ');\n\t}\n\tprintf(\"\\n\");*/\n}\n\ntemplate<class T> bool eq(T a,T b){\n\treturn abs(a-b)<eps;\n}\ntemplate<class T> int sgn(T a){\n\tif(eq(a,0.0)) return 0;\n\tif(a>0) return 1;\n\treturn -1;\n}\n\nbool onSeg(Point a,Point b,Point c){\n\treturn eq(abs(a-b),abs(a-c)+abs(c-b));\n}\n\nReal doP(Point a,Point b){\n\treturn (conj(a)*b).real();\n}\n\nReal crP(Point a,Point b){\n\treturn (conj(a)*b).imag();\n}\n\nPoint proj(Point p,Point b){\n\treturn b*doP(p,b)/norm(b);\n}\n\nbool isPara_(Point a,Point b){\n\treturn eq(crP(a,b),(Real)0);\n}\n\nPoint iLL(Line l1,Line l2){\n\tif(isPara_(l1.second-l1.first,l2.second-l2.first)) return NPoint;\n\tPoint a1=l1.first,b1=l1.second;\n\tPoint a2=l2.first,b2=l2.second;\n\tReal num=crP(a2-a1,b1-a1);\n\tReal den=crP(b1-a1,b2-a2);\n\treturn a2+(b2-a2)*(num/den);\n}\n\nbool iLS_(Line l,Segment s){\n\tif(isPara_(l.second-l.first,s.second-s.first)) return false;\n\tPoint p=iLL(l,s);\n\tif(onSeg(s.first,s.second,p)) return true;\n\treturn false;\n}\n\nPoint iLS(Line l,Segment s){\n\t//重なるときは考えなくてよい \n\tPoint p=iLL(l,s);\n\tif(isnan(p.real())) return p;\n\tif(onSeg(s.first,s.second,p)) return p;\n\treturn NPoint;\n}\n\nbool iSS_(Segment s1,Segment s2){\n\tPoint p=iLL(s1,s2);\n\tif(isnan(p.real())) return false;\n\tif(!onSeg(s1.first,s1.second,p)) return false;\n\tif(!onSeg(s2.first,s2.second,p)) return false;\n\treturn true;\n}\n\nPoint iSS(Segment s1,Segment s2){\n\tif(iSS_(s1,s2)) return NPoint;\n\treturn iLL(s1,s2);\n}\n\nPoint perp(Line l,Point a){\n\tPoint p=l.first,q=l.second;\n\treturn p+proj(a-p,q-p);\n}\n\nPoint refl(Line l,Point p){\n\tPoint h=perp(l,p);\n\treturn h*(Real)2-p;\n}\n\nPolygon refl(Line l,Polygon poly){\n\tPolygon res;\n\tfor(int i=0;i<poly.size();i++){\n\t\tPoint p=refl(l,poly[i]);\n\t\tres.push_back(p);\n\t}\n\treverse(res.begin(),res.end());\n\treturn res;\n}\n\nSegment refl(Line l,Segment seg){\n\tseg.first=refl(l,seg.first);\n\tseg.second=refl(l,seg.second);\n\treturn seg;\n}\n\nLine perpBisec(Point p,Point q){\n\tPoint d=q-p;\n\tPoint rot=Point(0,1);\n\tPoint mid=(p+q)/(Real)2;\n\tPoint l1=mid+d*rot;\n\tPoint l2=mid-d*rot;\n\treturn Line(l1,l2);\n}\n\nPolygon convCut(Polygon poly,Point a,Point b){\n\tPolygon res;\n\tLine l=Line(a,b);\n\tif(poly.size()==0) return res;\n\tfor(int i=0;i+1<poly.size();i++){\n\t\tif(sgn(crP(b-a,poly[i]-a))>=0) res.push_back(poly[i]);\n\t\tSegment seg=Segment(poly[i],poly[i+1]);\n\t\tif(iLS_(l,seg)){\n\t\t\tPoint p=iLL(l,seg);\n\t\t\tif(eq(p,poly[i])) continue;\n\t\t\tif(eq(p,poly[i+1])) continue;\n\t\t\tres.push_back(iLL(l,seg));\n\t\t}\n\t}\n\tif(res.size()==0) return res;\n\tif(res.size()<=2){\n\t\tres.clear();\n\t\treturn res;\n\t}\n\tres.push_back(res[0]);\n\treturn res;\n}\n\nSegment iLPoly(Line l,Polygon poly){\n\t//1点で交わるとき注意!\n\tSegment seg=NSeg;\n\tfor(int i=0;i+1<poly.size();i++){\n\t\tSegment e=Segment(poly[i],poly[i+1]);\n\t\tPoint p=iLS(l,e);\n\t\tif(isnan(p.real())) continue;\n\t\tif(eq(p,poly[i+1])) continue;\n\t\tif(isnan(seg.first.real())) seg.first=p;\n\t\telse seg.second=p;\n\t}\n\treturn seg;\n}\n\nbool inPoly_(Polygon poly,Point p){//ON is OUT\n\tbool in=false;\n\tfor(int i=0;i+1<poly.size();i++){\n\t\tPoint a=poly[i],b=poly[i+1];\n\t\tif(onSeg(a,b,p)) return false;\n\t\ta-=p,b-=p;\n\t\tif(sgn(a.imag()-b.imag())>0) swap(a,b);\n\t\tif(sgn(a.imag())<=0&&sgn(b.imag())>0){\n\t\t\tint s=sgn(crP(a,b));\n\t\t\tif(s==-1) in=!in;\n\t\t}\n\t}\n\treturn in;\n}\n\nbool inPolyloose_(Polygon poly,Point p){//ON is IN\n\tbool in=false;\n\tfor(int i=0;i+1<poly.size();i++){\n\t\tPoint a=poly[i],b=poly[i+1];\n\t\tif(onSeg(a,b,p)) return true;\n\t\ta-=p,b-=p;\n\t\tif(sgn(a.imag()-b.imag())>0) swap(a,b);\n\t\tif(sgn(a.imag())<=0&&sgn(b.imag())>0){\n\t\t\tint s=sgn(crP(a,b));\n\t\t\tif(s==-1) in=!in;\n\t\t}\n\t}\n\treturn in;\n}\t\n/*\nbool oPolyPoly_(Polygon p1,Polygon p2){\n\t//この場合は、zero area で重なることがないのでこれでよい \n\t//よくなかった \n\t//凸性を使っている \n\tif(p1.empty()||p2.empty()) return false;\n\tif(inPoly_(p2,p1[0])) return true;\n\tif(inPoly_(p1,p2[0])) return true;/*\n\tfor(int i=0;i+1<p1.size();i++){\n\t\tfor(int j=0;j+1<p2.size();j++){\n\t\t\tSegment s1=Segment(p1[i],p1[i+1]);\n\t\t\tSegment s2=Segment(p2[j],p2[j+1]);\n\t\t\tbool flg=iSS_(s1,s2);\n\t\t\tif(flg) return true;\n\t\t}\n\t}*\n\tvector<Point> pts;\n\tfor(int i=0;i+1<p1.size();i++){\n\t\tfor(int j=0;j+1<p2.size();j++){\n\t\t\tSegment s1=Segment(p1[i],p1[i+1]);\n\t\t\tSegment s2=Segment(p2[j],p2[j+1]);\n\t\t\tPoint p=iSS(s1,s2);\n\t\t\tif(isnan(p.real())) continue;\n\t\t\tif(eq(p,s1.first)||eq(p,s1.second)) continue;\n\t\t\tif(eq(p,s2.first)||eq(p,s2.second)) continue;\n\t\t\tpts.push_back(p);\n\t\t}\n\t}\n\tfor(int i=0;i+1<p1.size();i++){\n\t\tif(inPolyloose_(p2,p1[i])) pts.push_back(p1[i]);\n\t}\n\tfor(int j=0;j+1<p2.size();j++){\n\t\tbool ok=true;\n\t\tfor(int i=0;i<p1.size();i++){\n\t\t\tif(eq(p1[i],p2[j])) ok=false;\n\t\t}\n\t\tif(!ok) continue;\n\t\tif(inPolyloose_(p1,p2[j])) pts.push_back(p2[j]);\n\t}\n\tif(pts.size()==0) return false;\n\tPoint p=0;\n\tfor(int i=0;i<pts.size();i++){\n\t\tp+=pts[i];\n\t}\n\tp/=(Real)pts.size();\n\tif(inPoly_(p1,p)==false) return false;\n\tif(inPoly_(p2,p)==false) return false;\n\treturn true;\n//\treturn false;\n}*/\n\nPolygon convCut(Polygon p1,Polygon p2){\n\tPolygon res=p1;\n\tfor(int i=0;i+1<p2.size();i++){\n\t\tif(eq(p2[i],p2[i+1])) continue;\n\t\tres=convCut(res,p2[i],p2[i+1]);\n\t}\n\treturn res;\n}\n\nbool oPolyPoly_(Polygon p1,Polygon p2){\n\t//凸のとき \n\tif(p1.size()==0||p2.size()==0) return false;\n\tPolygon p=convCut(p1,p2);\n\tReal area=0;\n\tfor(int i=0;i+1<p.size();i++){\n\t\tarea+=crP(p[i],p[i+1]);\n\t}\n\tif(eq(area,(Real)0)) return false;\n\telse return true;\n}\n\nPolygon polys[1200];\nSegment conSeg[1200][1200];\nint V;\n\n//Polygon old_polys[1200];\nPolygon new_polys[1200];\nint new_ids[1200];\nvector<int> new_papers;\nSegment new_conSeg[520][520];\n\nbool folded[1200];\n\nSegment tmp_conSeg[1200];\n\nvoid process(Point p,Point q,bool last=false){\n/*\tprintf(\"process\\n\");\n\tfor(int i=0;i<V;i++){\n\t\tfor(int j=0;j<V;j++){\n\t\t\tif(isnan(conSeg[i][j].first.real())) printf(\".\");\n\t\t\telse printf(\"#\");\n\t\t}\n\t\tprintf(\"\\n\");\n\t}*//*\n\tfor(int i=0;i<V;i++){\n\t\tprint(polys[i]);\n\t}*/\n//\tfor(int i=0;i<V;i++) old_polys[i]=polys[i];\n\tfor(int i=0;i<V;i++) new_polys[i]=polys[i];\n\tfor(int i=0;i<V;i++) for(int j=0;j<V;j++){\n\t\tif(eq(conSeg[i][j].first,conSeg[i][j].second)){\n\t\t\tconSeg[i][j]=NSeg;\n\t\t}\n\t}\n\tLine l=perpBisec(p,q);//crP(p-l.first,l.second-l.first)>0\n\tfor(int i=0;i<V;i++) tmp_conSeg[i]=iLPoly(l,polys[i]);\n\tfor(int i=0;i<V;i++) for(int j=0;j<V;j++){\n\t\tif(i==j) continue;\n\t\tif(isnan(conSeg[i][j].first.real())){\n\t\t\tconSeg[i+V][j+V]=NSeg;\n\t\t\tcontinue;\n\t\t}\n\t\tSegment seg=conSeg[i][j];\n\t\tif(iLS_(l,seg)==false){\n\t\t\tPoint tmp=seg.first;\n\t\t\tint s=sgn(crP(tmp-l.first,l.second-l.first));\n\t\t\tif(s==1){\n\t\t\t\tconSeg[i][j]=NSeg;\n\t\t\t\tconSeg[i+V][j+V]=seg;\n\t\t\t}else{\n\t\t\t\tconSeg[i][j]=seg;\n\t\t\t\tconSeg[i+V][j+V]=NSeg;\n\t\t\t}\n\t\t}else{\n\t\t\tPoint s=iLL(l,seg);\n\t\t\tPoint s1=seg.first,s2=seg.second;\n\t\t\tif(sgn(crP(s1-l.first,l.second-l.first))<0){\n\t\t\t\tswap(s1,s2);\n\t\t\t}\n\t\t\tconSeg[i+V][j+V]=Segment(s1,s);\n\t\t\tconSeg[i][j]=Segment(s,s2);\n\t\t\tif(eq(s1,s)){\n\t\t\t\tconSeg[i+V][j+V]=NSeg;\n\t\t\t}\n\t\t\tif(eq(s2,s)){\n\t\t\t\tconSeg[i][j]=NSeg;\n\t\t\t}\n\t\t}\n\t}\n\tfor(int i=0;i<V;i++){\n\t\tPolygon poly=polys[i];\n\t\tpolys[i]=convCut(poly,l.first,l.second);\n\t\tpolys[i+V]=convCut(poly,l.second,l.first);\n\t}\n//\tprintf(\"separated\\n\");\n/*\tfor(int i=0;i<V*2;i++){\n\t\tprint(polys[i]);\n\t}*/\n//\tprintf(\"separated\\n\");\n\tint st=-1;\n\tfor(int i=V*2-1;i>=V;i--){\n\t\tif(inPoly_(polys[i],p)){\n\t\t\tst=i;\n\t\t\tbreak;\n\t\t}\n\t}\n\tif(st==-1){\n\t\tprintf(\"st=-1\\n\");\n\t\texit(0);\n\t}\n\tfor(int i=0;i<V*2;i++) folded[i]=false;\n\tfolded[st]=true;\n\tqueue<int> que;\n\tque.push(st);\n\t/*for(int i=0;i<V*2;i++){\n\t\tfor(int j=0;j<V*2;j++){\n\t\t\tif(isnan(conSeg[i][j].first.real())) printf(\".\");\n\t\t\telse printf(\"#\");\n\t\t}\n\t\tprintf(\"\\n\");\n\t}*/\n\twhile(!que.empty()){\n\t\tint cur=que.front();\n\t\tque.pop();\n\t\tfor(int i=V;i<V*2;i++){\n\t\t\tif(i==cur) continue;\n\t\t\tif(folded[i]) continue;\n\t\t\tif(isnan(conSeg[i][cur].first.real())==false){\n\t\t\t\tfolded[i]=true;\n\t\t\t\tque.push(i);\n\t\t\t\tcontinue;\n\t\t\t}\n\t\t\tif(i<cur) continue;\n\t\t\tbool flg=oPolyPoly_(polys[i],polys[cur]);\n\t\t//\tif(overlap[i-V][cur-V]&&cur<i){\n\t\t\tif(flg){\n\t\t\t\tfolded[i]=true;\n\t\t\t\tque.push(i);\n\t\t\t}\n\t\t}\n\t}\n/*\tprintf(\"folded::\");\n\tfor(int i=0;i<V*2;i++){\n\t\tif(folded[i]) printf(\"%d \",i);\n\t}\n\tprintf(\"\\n\");*/\n\tnew_papers.clear();\n\tfor(int i=0;i<V;i++){\n\t//\tif(polys[i].empty()) continue;\n\t\tif(folded[i+V]==false){\n\t\t\t//new_polys[i]=old_polys[i];\n\t\t\tnew_papers.push_back(i);\n\t\t}else{\n\t\t\tif(polys[i].size()>0){\n\t\t\t\tnew_polys[i]=polys[i];\n\t\t\t\tnew_papers.push_back(i);\n\t\t\t}\n\t\t}\n\t}\n\tfor(int i=V*2-1;i>=V;i--){\n\t\tif(polys[i].empty()) continue;\n\t\tif(folded[i]){\n\t\t\tnew_polys[i]=refl(l,polys[i]);\n\t\t\tnew_papers.push_back(i);\n\t\t}\n\t}\n\tfor(int i=0;i<V*2;i++){\n\t\tnew_ids[i]=-1;\n\t}\n\tfor(int i=0;i<new_papers.size();i++){\n\t\tnew_ids[new_papers[i]]=i;\n\t}\n\tfor(int i=0;i<V;i++){\n\t\tfor(int j=0;j<V;j++){/*\n\t\t\tif(isnan(conSeg[i][j].first.real())==false&&isnan(conSeg[i+V][j+V].first.real())==false){\n\t\t\t\tif(folded[i+V]==false&&folded[j+V]==false){\n\t\t\t\t\tconSeg[i][j].first=conSeg[i+V][j+V].first;\n\t\t\t\t}\n\t\t\t}*/\n\t\t\tif(folded[i+V]==false&&folded[j+V]==false){\n\t\t\t\tif(isnan(conSeg[i+V][j+V].first.real())==false){\n\t\t\t\t\tif(conSeg[i][j].first.real()==false){\n\t\t\t\t\t\tconSeg[i][j].first=conSeg[i+V][j+V].first;\n\t\t\t\t\t}else{\n\t\t\t\t\t\tconSeg[i][j]=conSeg[i+V][j+V];\n\t\t\t\t\t}\n\t\t\t\t}else{\n\t\t\t//\t\tconSeg[i][j]=conSeg[i+V][j+V];\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n\tif(!last){\n\t\tfor(int i=0;i<new_papers.size();i++) for(int j=i+1;j<new_papers.size();j++){\n\t\t\tint id1=new_papers[i];\n\t\t\tint id2=new_papers[j];\n//\t\t\tif(id2<V||id1>=V){\n\t\t\tif(id2<V){\n\t\t\t\tnew_conSeg[i][j]=conSeg[id1][id2];\n\t\t\t\tnew_conSeg[j][i]=conSeg[id2][id1];\n\t\t\t}else if(id1>=V){\n\t\t\t\tSegment tmp=conSeg[id1][id2];\n\t\t\t\tif(isnan(tmp.first.real())==false){\n\t\t\t\t\ttmp=refl(l,tmp);\n\t\t\t\t}\n\t\t\t\tnew_conSeg[i][j]=tmp;\n\t\t\t\tnew_conSeg[j][i]=tmp;\n\t\t\t}else if(id2-id1==V){\n\t\t\t\t//Segment seg=iLPoly(l,old_polys[id1]);\n\t\t\t\tSegment seg=tmp_conSeg[id1];\n\t\t\t\tnew_conSeg[i][j]=seg;\n\t\t\t\tnew_conSeg[j][i]=seg;\n\t\t\t}else{\n\t\t\t\tnew_conSeg[i][j]=NSeg;\n\t\t\t\tnew_conSeg[j][i]=NSeg;\n\t\t\t}\n\t\t}\n\t}\n\tV=new_papers.size();\n\tfor(int i=0;i<V;i++) polys[i]=new_polys[new_papers[i]];\n\tif(!last){\n\t\tfor(int i=0;i<V;i++) for(int j=0;j<V;j++){\n\t\t\tif(i==j) conSeg[i][j]=NSeg;\n\t\t\telse conSeg[i][j]=new_conSeg[i][j];\n\t\t}\n/*\t\tfor(int i=0;i<V;i++) for(int j=0;j<V;j++){\n\t\t\tif(i==j) overlap[i][j]=false;\n\t\t\telse overlap[i][j]=oPolyPoly_(polys[i],polys[j]);\n\t\t}*/\n\t}\n}\n\nPoint ps[10],qs[10];\nPoint h;\nint N;\n\nvoid input(){\n\tscanf(\"%d\",&N);\n\tif(N==0) exit(0);\n\tfor(int i=0;i<N;i++){\n\t\tint px,py,qx,qy;\n\t\tscanf(\"%d%d%d%d\",&px,&py,&qx,&qy);\n\t\tps[i]=Point(px,py);\n\t\tqs[i]=Point(qx,qy);\n\t}\n\tint x,y;\n\tscanf(\"%d%d\",&x,&y);\n\th=Point(x,y);\n}\n\nvoid init(){\n\tV=1;\n\tPoint p1=Point(0,0);\n\tPoint p2=Point(100,0);\n\tPoint p3=Point(100,100);\n\tPoint p4=Point(0,100);\n\tfor(int i=0;i<1200;i++){\n\t\tpolys[i].clear();\n\t//\told_polys[i].clear();\n\t\tnew_polys[i].clear();\n\t\tfor(int j=0;j<1200;j++){\n\t\t\tconSeg[i][j]=NSeg;\n\t\t//\toverlap[i][j]=false;\n\t\t}\n\t}\n\tfor(int i=0;i<520;i++) for(int j=0;j<520;j++){\n\t\tnew_conSeg[i][j]=NSeg;\n\t}\n\tpolys[0].push_back(p1);\n\tpolys[0].push_back(p2);\n\tpolys[0].push_back(p3);\n\tpolys[0].push_back(p4);\n\tpolys[0].push_back(p1);\n}\n\nvoid processAll(){\n\tfor(int i=0;i<N;i++){\n\t\tPoint p=ps[i],q=qs[i];\n\t\tif(i==N-1) process(p,q,true);\n\t\telse process(p,q);\n\t}\n/*\tprintf(\"last V=%d\\n\",V);\n\tfor(int i=0;i<V;i++){\n\t\tprint(polys[i]);\n\t}*/\n}\n\nint getNum(){\n\tint res=0;\n\tfor(int i=0;i<V;i++){\n\t\tif(inPoly_(polys[i],h)) res++;\n\t}\n\treturn res;\n}\n\nint main(){\n\twhile(true){\n\t\tinput();\n\t\tinit();\n\t\tprocessAll();\n\t\tint ans=getNum();\n\t\tprintf(\"%d\\n\",ans);\n\t//\tbreak;\n\t}\n\treturn 0;\n}", "accuracy": 1, "time_ms": 160, "memory_kb": 54852, "score_of_the_acc": -1.0285, "final_rank": 4 }, { "submission_id": "aoj_1299_276331", "code_snippet": "#include <stdio.h>\n#include <assert.h>\n#include <iostream>\n#include <complex>\n#include <vector>\n#include <queue>\n#include <map>\n#include <algorithm>\nusing namespace std;\n#define rep(i, n) for(int i=0; i<(int)(n); i++)\n#define mp make_pair\ntypedef complex<double> P;\ntypedef vector<P> convex;\n#define EPS (1e-9)\n\ndouble cross(const P& a, const P& b) { return imag(conj(a)*b); }\ndouble dot(const P& a, const P& b) { return real(conj(a)*b); }\n\nint ccw(const P& a, P b, P c) {\n b -= a; c -= a;\n if(cross(b, c)>0) return 1;\n if(cross(b, c)<0) return -1;\n if(dot(b, c)<0) return 2;\n if(norm(b)<norm(c)) return -2;\n return 0;\n}\n\nP projection(const P& l0, const P& l1, const P& p) {\n const double t = dot(p-l0, l1-l0) / norm(l1-l0);\n return l0 + t*(l1-l0);\n}\n\nbool intersectSP(const P& s0, const P& s1, const P& p) {\n return abs(s0-p)+abs(s1-p)-abs(s1-s0)<EPS;\n}\n\nP crosspoint(const P& l0, const P& l1, const P& m0, const P& m1) {\n const double a = cross(l1-l0, m1-m0);\n const double b = cross(l1-l0, l1-m0);\n if(abs(a)<EPS && abs(b)<EPS) return m0;\n// if(abs(a)<EPS) throw 0;\n return m0 + b/a*(m1-m0);\n}\n\nconvex convex_cut(const convex& v, const P& l0, const P& l1) {\n convex r;\n rep(i, v.size()) {\n const P& a(v[i]), b(v[(i+1)%v.size()]);\n if(ccw(l0, l1, a)!=-1) r.push_back(a);\n if(ccw(l0, l1, a)*ccw(l0, l1, b)<0) {\n r.push_back(crosspoint(l0, l1, a, b));\n }\n }\n return r;\n}\n\nbool contains(const convex& ps, const P& p) {\n bool in = false;\n rep(i, ps.size()) {\n const int j = (i+1)%ps.size();\n P a(ps[i]-p), b(ps[j]-p);\n if(imag(a) > imag(b)) swap(a, b);\n if(imag(a) <= 0 && 0 < imag(b) && cross(a, b) < 0) in = !in;\n if(cross(a, b)==0 && dot(a, b) <= 0) return true; // on edge\n }\n return in;\n}\n\nbool intersectCVCV(const convex& a, const convex& b) {\n rep(i, a.size()) if(contains(b, a[i])) return true;\n rep(i, b.size()) if(contains(a, b[i])) return true;\n return false;\n}\n\n// convex and hinges\n// hinges are represented by (1 + the index of the connected segment)\ntypedef pair<convex, vector<int> > segment;\n\nint index(const convex& v, const P& s0, const P& s1) {\n const P& mid(0.5*(s0+s1));\n rep(i, v.size()) {\n const int j = (i+1)%v.size();\n if(intersectSP(v[i], v[j], mid)) return i;\n }\n return -1;\n}\n\nvector<segment> turnover(const vector<segment>& vs, P p, P q) {\n // first, determine which segments to turn over\n queue<int> qu;\n vector<int> is(vs.size(), 0);\n // rule 1\n for(int i=(int)vs.size()-1; i>=0; i--) if(contains(vs[i].first, p)) {\n qu.push(i);\n is[i] = 1;\n break;\n }\n P m(0.5*(p+q));\n P dir((p-q)*P(0, 1));\n while(!qu.empty()) {\n const int ix = qu.front();\n qu.pop();\n const convex& cv = vs[ix].first;\n convex a(convex_cut(cv, m, m-dir));\n // rule 2\n rep(i, a.size()) {\n const int j = (i+1)%a.size();\n const int k = index(cv, a[i], a[j]);\n if(k==-1) continue;\n const int tx = vs[ix].second[k]-1;\n if(tx!=-1 && !is[tx]) {\n qu.push(tx);\n is[tx] = 1;\n }\n }\n // rule 3\n for(int i=ix+1; i<(int)vs.size(); i++) {\n if(!is[i] && intersectCVCV(a, vs[i].first)) {\n qu.push(i);\n is[i] = 1;\n }\n }\n }\n\n // second, turn over the segments\n // in this section, hinges are left untouched, except for:\n // hinges to segment in `s` -> positive\n // hinges to segment in `t` -> negative\n vector<pair<int, segment> > s, t;\n rep(k, vs.size()) {\n if(is[k]==0) s.push_back(mp(k+1, vs[k]));\n else {\n const convex& cv = vs[k].first;\n convex a(convex_cut(cv, m, m+dir));\n if(a.size()>0) {\n vector<int> hg;\n rep(i, a.size()) {\n const int j = (i+1)%a.size();\n const int ix = index(cv, a[i], a[j]);\n hg.push_back(ix==-1 ? -(k+1) : vs[k].second[ix]);\n }\n s.push_back(mp(k+1, mp(a, hg)));\n }\n convex b(convex_cut(cv, m, m-dir));\n if(b.size()>0) {\n vector<int> hg;\n rep(i, b.size()) {\n const int j = (i-1+b.size())%b.size();\n const int ix = index(cv, b[i], b[j]);\n const P to = 2.0*projection(m, m+dir, b[i])-b[i];\n hg.push_back(ix==-1 ? k+1 : -vs[k].second[ix]);\n }\n rep(i, b.size()) b[i] = 2.0*projection(m, m+dir, b[i])-b[i];\n reverse(b.begin(), b.end());\n reverse(hg.begin(), hg.end());\n t.push_back(mp(k+1, mp(b, hg)));\n }\n }\n }\n\n // third, fix the hinges\n map<int, int> of;\n rep(i, s.size()) of[s[i].first] = i+1;\n rep(i, t.size()) of[-t[i].first] = s.size()+t.size()-i;\n vector<segment> r;\n rep(i, s.size()) r.push_back(s[i].second);\n rep(i, t.size()) r.push_back(t[t.size()-1-i].second);\n rep(i, r.size()) rep(j, r[i].second.size()) {\n r[i].second[j] = of[r[i].second[j]];\n }\n return r;\n}\n\nint N, Px[16], Py[16], Qx[16], Qy[16];\nint Hx, Hy;\n\nint main() {\n for(;;) {\n scanf(\"%d\", &N);\n if(N==0) return 0;\n rep(i, N) scanf(\"%d%d%d%d\", Px+i, Py+i, Qx+i, Qy+i);\n scanf(\"%d%d\", &Hx, &Hy);\n convex v;\n v.push_back(P(0, 0)); v.push_back(P(100, 0));\n v.push_back(P(100, 100)); v.push_back(P(0, 100));\n vector<segment> vs;\n vs.push_back(mp(v, vector<int>(v.size())));\n rep(i, N) vs = turnover(vs, P(Px[i], Py[i]), P(Qx[i], Qy[i]));\n int ans = 0;\n rep(i, vs.size()) if(contains(vs[i].first, P(Hx, Hy))) ans++;\n printf(\"%d\\n\", ans);\n }\n}", "accuracy": 1, "time_ms": 20, "memory_kb": 1140, "score_of_the_acc": 0, "final_rank": 1 }, { "submission_id": "aoj_1299_276327", "code_snippet": "#include <stdio.h>\n#include <assert.h>\n#include <iostream>\n#include <complex>\n#include <vector>\n#include <queue>\n#include <map>\n#include <algorithm>\nusing namespace std;\n#define rep(i, n) for(int i=0; i<(int)(n); i++)\n#define mp make_pair\ntypedef complex<double> P;\ntypedef vector<P> convex;\ntypedef vector<pair<P, int> > segment;\n#define EPS (1e-9)\n\n\ndouble cross(const P& a, const P& b) { return imag(conj(a)*b); }\ndouble dot(const P& a, const P& b) { return real(conj(a)*b); }\n\nint ccw(const P& a, P b, P c) {\n b -= a; c -= a;\n if(cross(b, c)>0) return 1;\n if(cross(b, c)<0) return -1;\n if(dot(b, c)<0) return 2;\n if(norm(b)<norm(c)) return -2;\n return 0;\n}\n\nP projection(const P& l0, const P& l1, const P& p) {\n const double t = dot(p-l0, l1-l0) / norm(l1-l0);\n return l0 + t*(l1-l0);\n}\n\nbool intersectSP(const P& s0, const P& s1, const P& p) {\n return abs(s0-p)+abs(s1-p)-abs(s1-s0)<EPS;\n}\n\nP crosspoint(const P& l0, const P& l1, const P& m0, const P& m1) {\n const double a = cross(l1-l0, m1-m0);\n const double b = cross(l1-l0, l1-m0);\n if(abs(a)<EPS && abs(b)<EPS) return m0;\n// if(abs(a)<EPS) throw 0;\n return m0 + b/a*(m1-m0);\n}\n\nconvex convex_cut(const convex& v, const P& l0, const P& l1) {\n convex r;\n rep(i, v.size()) {\n const P& a(v[i]), b(v[(i+1)%v.size()]);\n if(ccw(l0, l1, a)!=-1) r.push_back(a);\n if(ccw(l0, l1, a)*ccw(l0, l1, b)<0) {\n r.push_back(crosspoint(l0, l1, a, b));\n }\n }\n return r;\n}\n\nbool contains(const convex& ps, const P& p) {\n bool in = false;\n rep(i, ps.size()) {\n const int j = (i+1)%ps.size();\n P a(ps[i]-p), b(ps[j]-p);\n if(imag(a) > imag(b)) swap(a, b);\n if(imag(a) <= 0 && 0 < imag(b) && cross(a, b) < 0) in = !in;\n if(cross(a, b)==0 && dot(a, b) <= 0) return true; // on edge\n }\n return in;\n}\n\nconvex to_convex(const segment& v) {\n convex r;\n rep(i, v.size()) r.push_back(v[i].first);\n return r;\n}\n\nint index(const convex& v, const P& s0, const P& s1) {\n const P& mid(0.5*(s0+s1));\n rep(i, v.size()) {\n const int j = (i+1)%v.size();\n if(intersectSP(v[i], v[j], mid)) return i;\n }\n return -1;\n}\n\nbool intersectCVCV(const convex& a, const convex& b) {\n rep(i, a.size()) if(contains(b, a[i])) return true;\n rep(i, b.size()) if(contains(a, b[i])) return true;\n return false;\n}\n\nvector<segment> turnover(const vector<segment>& vs, P p, P q) {\n queue<int> qu;\n for(int i=(int)vs.size()-1; i>=0; i--) {\n if(contains(to_convex(vs[i]), p)) {\n qu.push(i);\n break;\n }\n }\n P m(0.5*(p+q));\n P dir((p-q)*P(0, 1));\n vector<int> is(vs.size(), 0);\n while(!qu.empty()) {\n const int ix = qu.front();\n qu.pop();\n if(is[ix]) continue;\n is[ix] = 1;\n convex cv(to_convex(vs[ix]));\n convex a(convex_cut(cv, m, m-dir));\n for(int i=ix+1; i<(int)vs.size(); i++) if(!is[i]) {\n if(intersectCVCV(a, to_convex(vs[i]))) qu.push(i);\n }\n rep(i1, a.size()) {\n const int i2 = (i1+1)%a.size();\n const int k = index(cv, a[i1], a[i2]);\n if(k!=-1 && vs[ix][k].second!=-1) qu.push(vs[ix][k].second);\n }\n }\n vector<pair<int, segment> > s, t;\n rep(k, vs.size()) {\n if(is[k]==0) {\n segment sg;\n rep(i, vs[k].size()) {\n sg.push_back(mp(vs[k][i].first, vs[k][i].second+1));\n }\n s.push_back(mp(k+1, sg));\n continue;\n }\n convex cv(to_convex(vs[k]));\n convex a(convex_cut(cv, m, m+dir));\n if(a.size()>0) {\n segment as;\n rep(i, a.size()) {\n const int j = (i+1)%a.size();\n const int ix = index(cv, a[i], a[j]);\n as.push_back(mp(a[i], ix==-1 ? -(k+1) : vs[k][ix].second+1));\n }\n s.push_back(mp(k+1, as));\n }\n convex b(convex_cut(cv, m, m-dir));\n if(b.size()>0) {\n segment bs;\n rep(i, b.size()) {\n const int j = (i+1)%b.size();\n const int ix = index(cv, b[i], b[j]);\n const P to = 2.0*projection(m, m+dir, b[i])-b[i];\n bs.push_back(mp(to, ix==-1 ? k+1 : -(vs[k][ix].second+1)));\n }\n reverse(bs.begin(), bs.end());\n int x = bs[0].second;\n rep(i, bs.size()-1) bs[i].second = bs[i+1].second;\n bs.back().second = x;\n t.push_back(mp(k+1, bs));\n }\n }\n map<int, int> of;\n rep(i, s.size()) of[s[i].first] = i+1;\n rep(i, t.size()) of[-t[i].first] = s.size()+t.size()-i;\n vector<segment> r;\n rep(i, s.size()) r.push_back(s[i].second);\n rep(i, t.size()) r.push_back(t[t.size()-1-i].second);\n rep(i, r.size()) rep(j, r[i].size()) r[i][j].second = of[r[i][j].second]-1;\n return r;\n}\n\nint N, Px[16], Py[16], Qx[16], Qy[16];\nint Hx, Hy;\n\nint main() {\n for(;;) {\n scanf(\"%d\", &N);\n if(N==0) return 0;\n rep(i, N) scanf(\"%d%d%d%d\", Px+i, Py+i, Qx+i, Qy+i);\n scanf(\"%d%d\", &Hx, &Hy);\n vector<pair<P, int> > v;\n v.push_back(mp(P(0, 0), -1));\n v.push_back(mp(P(100, 0), -1));\n v.push_back(mp(P(100, 100), -1));\n v.push_back(mp(P(0, 100), -1));\n vector<vector<pair<P, int> > > vs;\n vs.push_back(v);\n rep(i, N) vs = turnover(vs, P(Px[i], Py[i]), P(Qx[i], Qy[i]));\n int ans = 0;\n rep(i, vs.size()) if(contains(to_convex(vs[i]), P(Hx, Hy))) ans++;\n printf(\"%d\\n\", ans);\n }\n}", "accuracy": 1, "time_ms": 90, "memory_kb": 1500, "score_of_the_acc": -0.021, "final_rank": 2 }, { "submission_id": "aoj_1299_108611", "code_snippet": "#include<iostream>\n#include<queue>\n#include<vector>\n#include<complex>\nusing namespace std;\n#define REP(i,b,n) for(int i=b;i<n;i++)\n#define rep(i,n) REP(i,0,n)\n#define pb push_back\nconst double eps = 1e-10;\n\nconst int N = 2000;\n\ntypedef complex<double> P;\n\nbool adj[N][N];\nbool up[N][N];//up[i][j] = 1 i >=j \nbool cpyadj[N][N];\nbool cpyup[N][N];\n\nclass Polygon{\npublic:\n vector<P> ori,newp;\n int oriid,newid;\n int cutbit;\n vector<P> inipos;\n};\n\nvoid outputpolygon(vector<P> &a){\n cout << \"[\";\n rep(i,a.size()+1)cout << a[(i+1)%a.size()]<<\",\";\n cout << \"],\"<<endl;\n}\n\n\n#define RIGHT 1\n#define LEFT -1\n\ndouble cross(P a,P b){\n return a.real()*b.imag()-a.imag()*b.real();\n}\n\ndouble dot(P a,P b){\n return a.real()*b.real()+a.imag()*b.imag();\n}\n\nbool isp(P &a,P &b,P &c){\n return abs(a-c)+abs(b-c) < abs(a-b) + eps;\n}\n\nP intersection_ll(P &a1,P &a2,P &b1,P &b2){\n P a=a2-a1,b=b2-b1;\n return a1+ a*cross(b,b1-a1)/cross(b,a);\n}\n\nint ccw(P a,P b){\n if (cross(a,b) < -0)return RIGHT;\n else if(cross(a,b)> 0)return LEFT;\n return 2;\n}\n\nbool is_intersected_ls(P &a1,P &a2,P &b1,P &b2){\n if (isp(a1,a2,b1)||isp(a1,a2,b2)||isp(b1,b2,a1)||\n isp(b1,b2,a2))return true;\n if (cross(a2-a1,b1-a1)*cross(a2-a1,b2-a1)<-eps &&\n cross(b2-b1,a1-b1)*cross(b2-b1,a2-b1)<-eps)\n return true;\n return false;\n}\n\n//LEFT remain,RIGHT reflect\nvoid Convex_cut(vector<P> in,P &a1,P &a2,\n\t\tvector<P> &ori,vector<P> &newp){\n int n = in.size();\n ori.clear();newp.clear();\n rep(i,n){\n P now = in[i],next=in[(i+1)%n];\n if( ccw(a1-a2,now-a2) == LEFT)ori.pb(now);\n else if( ccw(a1-a2,now-a2) == RIGHT)newp.pb(now);\n if (ccw(a1-a2,now-a2)*ccw(a1-a2,next-a2)<0){\n ori.pb(intersection_ll(a1,a2,now,next));\n newp.pb(intersection_ll(a1,a2,now,next));\n }\n }\n}\n\npair<P,P> make_line(P p,P q){\n int cutpos=RIGHT;\n P tmp=(p+q);\n tmp.real()/=2;tmp.imag()/=2;\n P ret=q-tmp;\n swap(ret.real(),ret.imag());\n ret.real()*=-1;\n if ( ccw(ret,p-tmp) != cutpos)return make_pair(tmp,ret+tmp);\n return make_pair(ret+tmp,tmp);\n}\n\nbool is_in(vector<P> &in,P &a){\n int cnt=0;\n int n = in.size();\n rep(i,n){\n P cur = in[i]-a,next=in[(i+1)%n]-a;\n if (cur.imag()>next.imag())swap(cur,next);\n if (cur.imag()<0&&0<=next.imag()&&\n\tcross(next,cur)>= 0)cnt++;\n if (isp(in[i],in[(i+1)%n],a))return true;\n }\n\n if (cnt%2 == 1)return true;\n return false;\n}\n\nP reflect(P &o,P &p,P &q){\n P N=p-o,L=q-o;\n double t = abs(L);\n N/=abs(N);\n L/=abs(L);\n double prepL = dot(N,L);//theta element of vector L against N (|L|=|N|=1\n double d=2*prepL;\n P r(d*N - L);//R vector symmetric point of L\n return o+t*r;\n}\n\nint find_most_upper(int n,Polygon *in,P &p){\n int index = -1;\n rep(i,n){\n if (is_in(in[i].ori,p)){\n if (index == -1)index =i;\n else if (up[i][index])index=i;\n }\n }\n return index;\n}\n\n//a ori,b newp\nbool is_intersected_polygon(vector<P> &a,vector<P> &b){\n rep(i,a.size()){\n if (is_in(b,a[i]))return true;\n rep(j,b.size()){\n if (is_intersected_ls(a[i],a[(i+1)%a.size()],\n\t\t\t b[j],b[(j+1)%b.size()])){\n\treturn true;\n }\n }\n }\n rep(j,b.size())if (is_in(a,b[j]))return true;\n return false;\n}\n\n\ndouble distance_l_p(P a,P b,P c){\n return abs( cross(b-a,c-a))/abs(b-a);\n}\n\nbool issame(P &a,P &b){\n return fabs(a.real()-b.real())<eps && fabs(a.imag()-b.imag())<eps;\n}\n\nbool have_same_line(vector<P> &in1,vector<P> &in2,P &a,P &b,bool flag){\n rep(i,in1.size()){\n rep(j,in2.size()){\n if (isp(in1[i],in1[(i+1)%in1.size()],in2[j])\n\t ||isp(in1[i],in1[(i+1)%in1.size()],in2[(j+1)%in2.size()])\n\t )return true;\n\n if ((issame(in1[i],in2[j])&&issame(in1[(i+1)%in1.size()],in2[(j+1)%in2.size()]))\n\t||\n\t(issame(in1[i],in2[(j+1)%in2.size()])&&issame(in1[(i+1)%in1.size()],in2[j]))\n\t)return true;\n }\n }\n return false;\n}\n\nbool vis[N];\nvoid origami(int &n,int ini,Polygon in[N],P &a,P &b,\n\t pair<P,P> *line,int querry){\n int m = n;\n vector<P> dummy;\n rep(i,N){\n in[i].newp.clear(),in[i].newid=-1;//nen no tame\n }\n\n rep(i,N)vis[i]=false;\n queue<int> Q;\n Q.push(ini);\n while(!Q.empty()){\n int now = Q.front();Q.pop();\n if (vis[now])continue;\n \n vis[now]=true;\n\n Convex_cut(in[now].ori,a,b,in[now].ori,in[now].newp);\n if (in[now].newp.size() != 0){\n in[now].oriid=now;\n in[now].newid=m++;\n in[in[now].newid].cutbit=in[now].cutbit|(1<<querry);\n }\n \n \n in[now].inipos=in[now].newp;\n for(int j=querry-1;j>=0;j--){\n if((in[now].cutbit&(1<<j)) != 0){\n\trep(k,in[now].inipos.size()){\n\t in[now].inipos[k]=reflect(line[j].first,line[j].second,in[now].inipos[k]);\n\t}\n }\n }\n \n \n rep(i,n){\n if (i == now)continue;\n if (adj[now][i]){\n\tif(have_same_line(in[now].inipos,in[i].inipos,a,b,false))\n\t Q.push(i);\n }\n if (up[i][now]){\n\tif (is_intersected_polygon(in[now].newp,in[i].ori))\n\t Q.push(i);\n }\n }\n }\n \n rep(i,n){\n rep(j,in[i].newp.size()){\n in[i].newp[j]=reflect(a,b,in[i].newp[j]);\n }\n }\n \n\n rep(i,n){\n int myori=in[i].oriid,mynew=in[i].newid;\n if (in[i].newp.size() == 0)mynew=-1;\n \n if (mynew != -1){\n up[myori][mynew]=false;\n up[mynew][myori]=true;\n }\n \n rep(j,n){\n if (i == j)continue;\n int yori=in[j].oriid,yournew=in[j].newid;\n bool tmp = up[i][j];\n //in[i].ori-in[j].ori\n up[myori][yori]=up[myori][yori];\n\n //in[i].ori-in[j].newp\n if (yournew != -1){\n\tup[myori][yournew]=false;\n }\n\n //in[i].newp-in[j].ori\n if (mynew != -1){\n\tif (is_intersected_polygon(in[i].newp,in[j].ori)){\n\t up[mynew][yori]=true;\n\t}else {\n\t up[mynew][yori]=false;\n\t}\n }\n \n //in[i].newp-in[j].ori\n if (mynew != -1 && yournew != -1){\n\tif (up[yori][myori] && is_intersected_polygon(in[i].newp,in[j].newp))\n\t up[mynew][yournew]=true;\n\telse up[mynew][yournew]=false;\n }\n }\n }\n \n \n \n rep(i,n){\n if (in[i].newp.size() != 0){\n in[in[i].newid].ori=in[i].newp;\n in[in[i].newid].oriid=in[i].newid;\n in[i].newp.clear();\n }\n }\n\n n = m;\n\n rep(i,m){\n in[i].inipos.clear();\n in[i].inipos=in[i].ori;\n for(int j=querry;j>=0;j--){\n if((in[i].cutbit&(1<<j)) != 0){\n\trep(k,in[i].inipos.size()){\n\t in[i].inipos[k]=reflect(line[j].first,line[j].second,in[i].inipos[k]);\n\t}\n }\n }\n }\n\n rep(i,m){\n REP(j,i+1,m){\n if (have_same_line(in[i].inipos,in[j].inipos,a,b,true)){\n\tadj[i][j]=adj[j][i]=true;\n }else adj[i][j]=adj[j][i]=false;\n }\n }\n}\n\t \nint solve(int querry){\n int n=1;\n static Polygon in[N];\n pair<P,P> line[10];\n P pos[10];\n // initialization\n rep(i,N){\n in[i].ori.clear();\n in[i].newp.clear();\n in[i].inipos.clear();\n in[i].oriid=i;\n in[i].newid=-1;\n in[i].cutbit=0;\n }\n rep(i,N)rep(j,N)adj[i][j]=false,up[i][j]=false;\n \n in[0].ori.pb(P(0,0));\n in[0].ori.pb(P(0,100));\n in[0].ori.pb(P(100,100));\n in[0].ori.pb(P(100,0));\n in[0].inipos=in[0].ori;\n\n rep(i,querry){\n P p,q;\n cin>>p.real()>>p.imag()>>q.real()>>q.imag();\n pos[i]=p;\n pair<P,P> tmp;\n line[i] = make_line(p,q);\n }\n\n rep(i,querry){\n int index = find_most_upper(n,in,pos[i]);\n if (index == -1){\n }else origami(n,index,in,line[i].first,line[i].second,\n\t\t line,i);\n }\n\n \n int cnt=0;\n P hole;\n cin>>hole.real()>>hole.imag();\n rep(i,n){\n if (in[i].ori.size() && is_in(in[i].ori,hole))cnt++;\n }\n\n return cnt;\n}\n\nmain(){\n int q;\n while(cin>>q&&q){\n cout << solve(q) << endl; \n }\n}", "accuracy": 1, "time_ms": 4920, "memory_kb": 9588, "score_of_the_acc": -1.1552, "final_rank": 5 }, { "submission_id": "aoj_1299_108608", "code_snippet": "#include<iostream>\n#include<queue>\n#include<vector>\n#include<complex>\nusing namespace std;\n#define REP(i,b,n) for(int i=b;i<n;i++)\n#define rep(i,n) REP(i,0,n)\n#define pb push_back\nconst double eps = 1e-10;\n\nconst int N = 2000;\n\ntypedef complex<double> P;\n\nbool adj[N][N];\nbool up[N][N];//up[i][j] = 1 i >=j \nbool cpyadj[N][N];\nbool cpyup[N][N];\n\nclass Polygon{\npublic:\n vector<P> ori,newp;\n int oriid,newid;\n int cutbit;\n vector<P> inipos;\n};\n\nvoid outputpolygon(vector<P> &a){\n cout << \"[\";\n rep(i,a.size()+1)cout << a[(i+1)%a.size()]<<\",\";\n cout << \"],\"<<endl;\n}\n\n\n#define RIGHT 1\n#define LEFT -1\n\ndouble cross(P a,P b){\n return a.real()*b.imag()-a.imag()*b.real();\n}\n\ndouble dot(P a,P b){\n return a.real()*b.real()+a.imag()*b.imag();\n}\n\nbool isp(P &a,P &b,P &c){\n return abs(a-c)+abs(b-c) < abs(a-b) + eps;\n}\n\nP intersection_ll(P &a1,P &a2,P &b1,P &b2){\n P a=a2-a1,b=b2-b1;\n return a1+ a*cross(b,b1-a1)/cross(b,a);\n}\n\nint ccw(P a,P b){\n if (cross(a,b) < -0)return RIGHT;\n else if(cross(a,b)> 0)return LEFT;\n return 2;\n}\n\nbool is_intersected_ls(P &a1,P &a2,P &b1,P &b2){\n if (isp(a1,a2,b1)||isp(a1,a2,b2)||isp(b1,b2,a1)||\n isp(b1,b2,a2))return true;\n if (cross(a2-a1,b1-a1)*cross(a2-a1,b2-a1)<-eps &&\n cross(b2-b1,a1-b1)*cross(b2-b1,a2-b1)<-eps)\n return true;\n return false;\n}\n\n//LEFT remain,RIGHT reflect\nvoid Convex_cut(vector<P> in,P &a1,P &a2,\n\t\tvector<P> &ori,vector<P> &newp){\n int n = in.size();\n ori.clear();newp.clear();\n rep(i,n){\n P now = in[i],next=in[(i+1)%n];\n if( ccw(a1-a2,now-a2) == LEFT)ori.pb(now);\n else if( ccw(a1-a2,now-a2) == RIGHT)newp.pb(now);\n if (ccw(a1-a2,now-a2)*ccw(a1-a2,next-a2)<0){\n //ori.pb(intersection_ll(a1,a2,now,next));\n ori.pb(intersection_ll(now,next,a1,a2));\n newp.pb(intersection_ll(a1,a2,now,next));\n }\n }\n}\n\npair<P,P> make_line(P p,P q){\n int cutpos=RIGHT;\n P tmp=(p+q);\n tmp.real()/=2;tmp.imag()/=2;\n P ret=q-tmp;\n swap(ret.real(),ret.imag());\n ret.real()*=-1;\n if ( ccw(ret,p-tmp) != cutpos)return make_pair(tmp,ret+tmp);\n return make_pair(ret+tmp,tmp);\n}\n\nbool is_in(vector<P> &in,P &a){\n int cnt=0;\n int n = in.size();\n rep(i,n){\n P cur = in[i]-a,next=in[(i+1)%n]-a;\n if (cur.imag()>next.imag())swap(cur,next);\n if (cur.imag()<0&&0<=next.imag()&&\n\tcross(next,cur)>= 0)cnt++;\n if (isp(in[i],in[(i+1)%n],a))return true;\n }\n\n if (cnt%2 == 1)return true;\n return false;\n}\n\nP reflect(P &o,P &p,P &q){\n P N=p-o,L=q-o;\n double t = abs(L);\n N/=abs(N);\n L/=abs(L);\n double prepL = dot(N,L);//theta element of vector L against N (|L|=|N|=1\n double d=2*prepL;\n P r(d*N - L);//R vector symmetric point of L\n return o+t*r;\n}\n\nint find_most_upper(int n,Polygon *in,P &p){\n int index = -1;\n rep(i,n){\n if (is_in(in[i].ori,p)){\n if (index == -1)index =i;\n else if (up[i][index])index=i;\n }\n }\n return index;\n}\n\n//a ori,b newp\nbool is_intersected_polygon(vector<P> &a,vector<P> &b){\n rep(i,a.size()){\n if (is_in(b,a[i]))return true;\n rep(j,b.size()){\n if (is_intersected_ls(a[i],a[(i+1)%a.size()],\n\t\t\t b[j],b[(j+1)%b.size()])){\n\treturn true;\n }\n }\n }\n rep(j,b.size())if (is_in(a,b[j]))return true;\n return false;\n}\n\n\ndouble distance_l_p(P a,P b,P c){\n return abs( cross(b-a,c-a))/abs(b-a);\n}\n\nbool issame(P &a,P &b){\n return fabs(a.real()-b.real())<eps && fabs(a.imag()-b.imag())<eps;\n}\n\nbool have_same_line(vector<P> &in1,vector<P> &in2,P &a,P &b,bool flag){\n rep(i,in1.size()){\n rep(j,in2.size()){\n if (isp(in1[i],in1[(i+1)%in1.size()],in2[j])\n\t ||isp(in1[i],in1[(i+1)%in1.size()],in2[(j+1)%in2.size()])\n\t )return true;\n\n if ((issame(in1[i],in2[j])&&issame(in1[(i+1)%in1.size()],in2[(j+1)%in2.size()]))\n\t||\n\t(issame(in1[i],in2[(j+1)%in2.size()])&&issame(in1[(i+1)%in1.size()],in2[j]))\n\t)return true;\n }\n }\n return false;\n}\n\nbool vis[N];\nvoid origami(int &n,int ini,Polygon in[N],P &a,P &b,\n\t pair<P,P> *line,int querry){\n int m = n;\n vector<P> dummy;\n rep(i,N){\n in[i].newp.clear(),in[i].newid=-1;//nen no tame\n }\n\n rep(i,N)vis[i]=false;\n queue<int> Q;\n Q.push(ini);\n while(!Q.empty()){\n int now = Q.front();Q.pop();\n if (vis[now])continue;\n \n vis[now]=true;\n\n Convex_cut(in[now].ori,a,b,in[now].ori,in[now].newp);\n if (in[now].newp.size() != 0){\n in[now].oriid=now;\n in[now].newid=m++;\n in[in[now].newid].cutbit=in[now].cutbit|(1<<querry);\n }\n \n \n in[now].inipos=in[now].newp;\n for(int j=querry-1;j>=0;j--){\n if((in[now].cutbit&(1<<j)) != 0){\n\trep(k,in[now].inipos.size()){\n\t in[now].inipos[k]=reflect(line[j].first,line[j].second,in[now].inipos[k]);\n\t}\n }\n }\n \n \n rep(i,n){\n if (i == now)continue;\n if (adj[now][i]){\n\tif(have_same_line(in[now].inipos,in[i].inipos,a,b,false))\n\t Q.push(i);\n }\n if (up[i][now]){\n\tif (is_intersected_polygon(in[now].newp,in[i].ori))\n\t Q.push(i);\n }\n }\n }\n \n rep(i,n){\n rep(j,in[i].newp.size()){\n in[i].newp[j]=reflect(a,b,in[i].newp[j]);\n }\n }\n \n\n rep(i,n){\n int myori=in[i].oriid,mynew=in[i].newid;\n if (in[i].newp.size() == 0)mynew=-1;\n \n if (mynew != -1){\n up[myori][mynew]=false;\n up[mynew][myori]=true;\n }\n \n rep(j,n){\n if (i == j)continue;\n int yori=in[j].oriid,yournew=in[j].newid;\n bool tmp = up[i][j];\n //in[i].ori-in[j].ori\n up[myori][yori]=up[myori][yori];\n\n //in[i].ori-in[j].newp\n if (yournew != -1){\n\tup[myori][yournew]=false;\n }\n\n //in[i].newp-in[j].ori\n if (mynew != -1){\n\tif (is_intersected_polygon(in[i].newp,in[j].ori)){\n\t up[mynew][yori]=true;\n\t}else {\n\t up[mynew][yori]=false;\n\t}\n }\n \n //in[i].newp-in[j].ori\n if (mynew != -1 && yournew != -1){\n\tif (up[yori][myori] && is_intersected_polygon(in[i].newp,in[j].newp))\n\t up[mynew][yournew]=true;\n\telse up[mynew][yournew]=false;\n }\n }\n }\n \n \n \n rep(i,n){\n if (in[i].newp.size() != 0){\n in[in[i].newid].ori=in[i].newp;\n in[in[i].newid].oriid=in[i].newid;\n in[i].newp.clear();\n }\n }\n\n n = m;\n\n rep(i,m){\n in[i].inipos.clear();\n in[i].inipos=in[i].ori;\n for(int j=querry;j>=0;j--){\n if((in[i].cutbit&(1<<j)) != 0){\n\trep(k,in[i].inipos.size()){\n\t in[i].inipos[k]=reflect(line[j].first,line[j].second,in[i].inipos[k]);\n\t}\n }\n }\n }\n\n rep(i,m){\n REP(j,i+1,m){\n if (have_same_line(in[i].inipos,in[j].inipos,a,b,true)){\n\tadj[i][j]=adj[j][i]=true;\n }else adj[i][j]=adj[j][i]=false;\n }\n }\n}\n\t \nint solve(int querry){\n int n=1;\n static Polygon in[N];\n pair<P,P> line[10];\n P pos[10];\n // initialization\n rep(i,N){\n in[i].ori.clear();\n in[i].newp.clear();\n in[i].inipos.clear();\n in[i].oriid=i;\n in[i].newid=-1;\n in[i].cutbit=0;\n }\n rep(i,N)rep(j,N)adj[i][j]=false,up[i][j]=false;\n \n in[0].ori.pb(P(0,0));\n in[0].ori.pb(P(0,100));\n in[0].ori.pb(P(100,100));\n in[0].ori.pb(P(100,0));\n in[0].inipos=in[0].ori;\n\n rep(i,querry){\n P p,q;\n cin>>p.real()>>p.imag()>>q.real()>>q.imag();\n pos[i]=p;\n pair<P,P> tmp;\n line[i] = make_line(p,q);\n }\n\n rep(i,querry){\n int index = find_most_upper(n,in,pos[i]);\n if (index == -1){\n }else origami(n,index,in,line[i].first,line[i].second,\n\t\t line,i);\n }\n\n \n int cnt=0;\n P hole;\n cin>>hole.real()>>hole.imag();\n rep(i,n){\n if (in[i].ori.size() && is_in(in[i].ori,hole))cnt++;\n }\n\n return cnt;\n}\n\nmain(){\n int q;\n while(cin>>q&&q){\n cout << solve(q) << endl; \n }\n}", "accuracy": 1, "time_ms": 4930, "memory_kb": 9584, "score_of_the_acc": -1.1572, "final_rank": 6 }, { "submission_id": "aoj_1299_105645", "code_snippet": "#include<iostream>\n#include<queue>\n#include<vector>\n#include<complex>\nusing namespace std;\n#define REP(i,b,n) for(int i=b;i<n;i++)\n#define rep(i,n) REP(i,0,n)\n#define pb push_back\nconst double eps = 1e-10;\n\nconst int N = 2000;\n\ntypedef complex<double> P;\n\nbool adj[N][N];\nbool up[N][N];//up[i][j] = 1 i >=j \nbool cpyadj[N][N];\nbool cpyup[N][N];\n\nclass Polygon{\npublic:\n vector<P> ori,newp;\n int oriid,newid;\n int cutbit;\n vector<P> inipos;\n};\n\nvoid outputpolygon(vector<P> &a){\n cout << \"[\";\n rep(i,a.size()+1)cout << a[(i+1)%a.size()]<<\",\";\n cout << \"],\"<<endl;\n}\n\n\n#define RIGHT 1\n#define LEFT -1\n\ndouble cross(P a,P b){\n return a.real()*b.imag()-a.imag()*b.real();\n}\n\ndouble dot(P a,P b){\n return a.real()*b.real()+a.imag()*b.imag();\n}\n\nbool isp(P &a,P &b,P &c){\n return abs(a-c)+abs(b-c) < abs(a-b) + eps;\n}\n\nP intersection_ll(P &a1,P &a2,P &b1,P &b2){\n P a=a2-a1,b=b2-b1;\n return a1+ a*cross(b,b1-a1)/cross(b,a);\n}\n\nint ccw(P a,P b){\n if (cross(a,b) < -0)return RIGHT;\n else if(cross(a,b)> 0)return LEFT;\n return 2;\n}\n\nbool is_intersected_ls(P &a1,P &a2,P &b1,P &b2){\n if (isp(a1,a2,b1)||isp(a1,a2,b2)||isp(b1,b2,a1)||\n isp(b1,b2,a2))return true;\n if (cross(a2-a1,b1-a1)*cross(a2-a1,b2-a1)<-eps &&\n cross(b2-b1,a1-b1)*cross(b2-b1,a2-b1)<-eps)\n return true;\n return false;\n}\n\n//LEFT remain,RIGHT reflect\nvoid Convex_cut(vector<P> in,P &a1,P &a2,\n\t\tvector<P> &ori,vector<P> &newp){\n int n = in.size();\n ori.clear();newp.clear();\n rep(i,n){\n P now = in[i],next=in[(i+1)%n];\n if( ccw(a1-a2,now-a2) == LEFT)ori.pb(now);\n else if( ccw(a1-a2,now-a2) == RIGHT)newp.pb(now);\n if (ccw(a1-a2,now-a2)*ccw(a1-a2,next-a2)<0){\n ori.pb(intersection_ll(a1,a2,now,next));\n newp.pb(intersection_ll(a1,a2,now,next));\n }\n }\n}\n\npair<P,P> make_line(P p,P q){\n int cutpos=RIGHT;\n P tmp=(p+q);\n tmp.real()/=2;tmp.imag()/=2;\n P ret=q-tmp;\n swap(ret.real(),ret.imag());\n ret.real()*=-1;\n if ( ccw(ret,p-tmp) != cutpos)return make_pair(tmp,ret+tmp);\n return make_pair(ret+tmp,tmp);\n}\n\nbool is_in(vector<P> &in,P &a){\n int cnt=0;\n int n = in.size();\n rep(i,n){\n P cur = in[i]-a,next=in[(i+1)%n]-a;\n if (cur.imag()>next.imag())swap(cur,next);\n if (cur.imag()<0&&0<=next.imag()&&\n\tcross(next,cur)>= 0)cnt++;\n if (isp(in[i],in[(i+1)%n],a))return true;\n }\n\n if (cnt%2 == 1)return true;\n return false;\n}\n\nP reflect(P &o,P &p,P &q){\n P N=p-o,L=q-o;\n double t = abs(L);\n N/=abs(N);\n L/=abs(L);\n double prepL = dot(N,L);//theta element of vector L against N (|L|=|N|=1\n double d=2*prepL;\n P r(d*N - L);//R vector symmetric point of L\n return o+t*r;\n}\n\nint find_most_upper(int n,Polygon *in,P &p){\n int index = -1;\n rep(i,n){\n if (is_in(in[i].ori,p)){\n if (index == -1)index =i;\n else if (up[i][index])index=i;\n }\n }\n return index;\n}\n\n//a ori,b newp\nbool is_intersected_polygon(vector<P> &a,vector<P> &b){\n rep(i,a.size()){\n if (is_in(b,a[i]))return true;\n rep(j,b.size()){\n if (is_intersected_ls(a[i],a[(i+1)%a.size()],\n\t\t\t b[j],b[(j+1)%b.size()])){\n\treturn true;\n }\n }\n }\n rep(j,b.size())if (is_in(a,b[j]))return true;\n return false;\n}\n\n\ndouble distance_l_p(P a,P b,P c){\n return abs( cross(b-a,c-a))/abs(b-a);\n}\n\nbool issame(P &a,P &b){\n return fabs(a.real()-b.real())<eps && fabs(a.imag()-b.imag())<eps;\n}\n\nbool have_same_line(vector<P> &in1,vector<P> &in2,P &a,P &b,bool flag){\n rep(i,in1.size()){\n rep(j,in2.size()){\n if (isp(in1[i],in1[(i+1)%in1.size()],in2[j])\n\t ||isp(in1[i],in1[(i+1)%in1.size()],in2[(j+1)%in2.size()])\n\t )return true;\n\n if ((issame(in1[i],in2[j])&&issame(in1[(i+1)%in1.size()],in2[(j+1)%in2.size()]))\n\t||\n\t(issame(in1[i],in2[(j+1)%in2.size()])&&issame(in1[(i+1)%in1.size()],in2[j]))\n\t)return true;\n }\n }\n return false;\n}\n\nbool vis[N];\nvoid origami(int &n,int ini,Polygon in[N],P &a,P &b,\n\t pair<P,P> *line,int querry){\n int m = n;\n vector<P> dummy;\n rep(i,N){\n in[i].newp.clear(),in[i].newid=-1;//nen no tame\n }\n\n rep(i,N)vis[i]=false;\n queue<int> Q;\n Q.push(ini);\n while(!Q.empty()){\n int now = Q.front();Q.pop();\n if (vis[now])continue;\n \n vis[now]=true;\n\n Convex_cut(in[now].ori,a,b,in[now].ori,in[now].newp);\n if (in[now].newp.size() != 0){\n in[now].oriid=now;\n in[now].newid=m++;\n in[in[now].newid].cutbit=in[now].cutbit|(1<<querry);\n }\n \n \n in[now].inipos=in[now].newp;\n for(int j=querry-1;j>=0;j--){\n if((in[now].cutbit&(1<<j)) != 0){\n\trep(k,in[now].inipos.size()){\n\t in[now].inipos[k]=reflect(line[j].first,line[j].second,in[now].inipos[k]);\n\t}\n }\n }\n \n \n rep(i,n){\n if (i == now)continue;\n if (adj[now][i]){\n\tif(have_same_line(in[now].inipos,in[i].inipos,a,b,false))\n\t Q.push(i);\n }\n if (up[i][now]){\n\tif (is_intersected_polygon(in[now].newp,in[i].ori))\n\t Q.push(i);\n }\n }\n }\n \n rep(i,n){\n rep(j,in[i].newp.size()){\n in[i].newp[j]=reflect(a,b,in[i].newp[j]);\n }\n }\n \n\n rep(i,n){\n int myori=in[i].oriid,mynew=in[i].newid;\n if (in[i].newp.size() == 0)mynew=-1;\n \n if (mynew != -1){\n up[myori][mynew]=false;\n up[mynew][myori]=true;\n }\n \n rep(j,n){\n if (i == j)continue;\n int yourori=in[j].oriid,yournew=in[j].newid;\n bool tmp = up[i][j];\n //in[i].ori-in[j].ori\n up[myori][yourori]=up[myori][yourori];\n\n //in[i].ori-in[j].newp\n if (yournew != -1){\n\tup[myori][yournew]=false;\n }\n\n //in[i].newp-in[j].ori\n if (mynew != -1){\n\tif (is_intersected_polygon(in[i].newp,in[j].ori)){\n\t up[mynew][yourori]=true;\n\t}else {\n\t up[mynew][yourori]=false;\n\t}\n }\n \n //in[i].newp-in[j].ori\n if (mynew != -1 && yournew != -1){\n\tif (up[yourori][myori] && is_intersected_polygon(in[i].newp,in[j].newp))\n\t up[mynew][yournew]=true;\n\telse up[mynew][yournew]=false;\n }\n }\n }\n \n \n \n rep(i,n){\n if (in[i].newp.size() != 0){\n in[in[i].newid].ori=in[i].newp;\n in[in[i].newid].oriid=in[i].newid;\n in[i].newp.clear();\n }\n }\n\n n = m;\n\n rep(i,m){\n in[i].inipos.clear();\n in[i].inipos=in[i].ori;\n for(int j=querry;j>=0;j--){\n if((in[i].cutbit&(1<<j)) != 0){\n\trep(k,in[i].inipos.size()){\n\t in[i].inipos[k]=reflect(line[j].first,line[j].second,in[i].inipos[k]);\n\t}\n }\n }\n }\n\n rep(i,m){\n REP(j,i+1,m){\n if (have_same_line(in[i].inipos,in[j].inipos,a,b,true)){\n\tadj[i][j]=adj[j][i]=true;\n }else adj[i][j]=adj[j][i]=false;\n }\n }\n}\n\t \nint solve(int querry){\n int n=1;\n static Polygon in[N];\n pair<P,P> line[10];\n P pos[10];\n // initialization\n rep(i,N){\n in[i].ori.clear();\n in[i].newp.clear();\n in[i].inipos.clear();\n in[i].oriid=i;\n in[i].newid=-1;\n in[i].cutbit=0;\n }\n rep(i,N)rep(j,N)adj[i][j]=false,up[i][j]=false;\n \n in[0].ori.pb(P(0,0));\n in[0].ori.pb(P(0,100));\n in[0].ori.pb(P(100,100));\n in[0].ori.pb(P(100,0));\n in[0].inipos=in[0].ori;\n\n rep(i,querry){\n P p,q;\n cin>>p.real()>>p.imag()>>q.real()>>q.imag();\n pos[i]=p;\n pair<P,P> tmp;\n line[i] = make_line(p,q);\n }\n\n rep(i,querry){\n int index = find_most_upper(n,in,pos[i]);\n if (index == -1){\n }else origami(n,index,in,line[i].first,line[i].second,\n\t\t line,i);\n }\n\n \n int cnt=0;\n P hole;\n cin>>hole.real()>>hole.imag();\n rep(i,n){\n if (in[i].ori.size() && is_in(in[i].ori,hole))cnt++;\n }\n\n return cnt;\n}\n\nmain(){\n int q;\n while(cin>>q&&q){\n cout << solve(q) << endl; \n }\n}", "accuracy": 1, "time_ms": 4930, "memory_kb": 9584, "score_of_the_acc": -1.1572, "final_rank": 6 }, { "submission_id": "aoj_1299_105642", "code_snippet": "#include<iostream>\n#include<queue>\n#include<vector>\n#include<complex>\nusing namespace std;\n#define REP(i,b,n) for(int i=b;i<n;i++)\n#define rep(i,n) REP(i,0,n)\n#define pb push_back\nconst double eps = 1e-10;\n\nconst int N = 2000;\n\ntypedef complex<double> P;\n\nbool adj[N][N];\nbool up[N][N];//up[i][j] = 1 i >=j \nbool cpyadj[N][N];\nbool cpyup[N][N];\n\nclass Polygon{\npublic:\n vector<P> ori,newp;\n int oriid,newid;\n int cutbit;\n vector<P> inipos;\n};\n\nvoid outputpolygon(vector<P> &a){\n cout << \"[\";\n rep(i,a.size()+1)cout << a[(i+1)%a.size()]<<\",\";\n cout << \"],\"<<endl;\n}\n\n\n#define RIGHT 1\n#define LEFT -1\n\ndouble cross(P a,P b){\n return a.real()*b.imag()-a.imag()*b.real();\n}\n\ndouble dot(P a,P b){\n return a.real()*b.real()+a.imag()*b.imag();\n}\n\nbool isp(P &a,P &b,P &c){\n return abs(a-c)+abs(b-c) < abs(a-b) + eps;\n}\n\nP intersection_ll(P &a1,P &a2,P &b1,P &b2){\n P a=a2-a1,b=b2-b1;\n return a1+ a*cross(b,b1-a1)/cross(b,a);\n}\n\nint ccw(P a,P b){\n if (cross(a,b) < -0)return RIGHT;\n else if(cross(a,b)> 0)return LEFT;\n return 2;\n}\n\nbool is_intersected_ls(P &a1,P &a2,P &b1,P &b2){\n if (isp(a1,a2,b1)||isp(a1,a2,b2)||isp(b1,b2,a1)||\n isp(b1,b2,a2))return true;\n if (cross(a2-a1,b1-a1)*cross(a2-a1,b2-a1)<-eps &&\n cross(b2-b1,a1-b1)*cross(b2-b1,a2-b1)<-eps)\n return true;\n return false;\n}\n\n//LEFT remain,RIGHT reflect\nvoid Convex_cut(vector<P> in,P &a1,P &a2,\n\t\tvector<P> &ori,vector<P> &newp){\n int n = in.size();\n ori.clear();newp.clear();\n rep(i,n){\n P now = in[i],next=in[(i+1)%n];\n if( ccw(a1-a2,now-a2) == LEFT)ori.pb(now);\n else if( ccw(a1-a2,now-a2) == RIGHT)newp.pb(now);\n if (ccw(a1-a2,now-a2)*ccw(a1-a2,next-a2)<0){\n ori.pb(intersection_ll(a1,a2,now,next));\n newp.pb(intersection_ll(a1,a2,now,next));\n }\n }\n}\n\npair<P,P> make_line(P p,P q){\n int cutpos=RIGHT;\n P tmp=(p+q);\n tmp.real()/=2;tmp.imag()/=2;\n P ret=q-tmp;\n swap(ret.real(),ret.imag());\n ret.real()*=-1;\n if ( ccw(ret,p-tmp) != cutpos)return make_pair(tmp,ret+tmp);\n return make_pair(ret+tmp,tmp);\n}\n\nbool is_in(vector<P> &in,P &a){\n int cnt=0;\n int n = in.size();\n rep(i,n){\n P cur = in[i]-a,next=in[(i+1)%n]-a;\n if (cur.imag()>next.imag())swap(cur,next);\n if (cur.imag()<0&&0<=next.imag()&&\n\tcross(next,cur)>= 0)cnt++;\n if (isp(in[i],in[(i+1)%n],a))return true;\n }\n\n if (cnt%2 == 1)return true;\n return false;\n}\n\nP reflect(P &o,P &p,P &q){\n P N=p-o,L=q-o;\n double t = abs(L);\n N/=abs(N);\n L/=abs(L);\n double prepL = dot(N,L);//theta element of vector L against N (|L|=|N|=1\n double d=2*prepL;\n P r(d*N - L);//R vector symmetric point of L\n return o+t*r;\n}\n\nint find_most_upper(int n,Polygon *in,P &p){\n int index = -1;\n rep(i,n){\n if (is_in(in[i].ori,p)){\n if (index == -1)index =i;\n else if (up[i][index])index=i;\n }\n }\n return index;\n}\n\n//a ori,b newp\nbool is_intersected_polygon(vector<P> &a,vector<P> &b){\n rep(i,a.size()){\n if (is_in(b,a[i]))return true;\n rep(j,b.size()){\n if (is_intersected_ls(a[i],a[(i+1)%a.size()],\n\t\t\t b[j],b[(j+1)%b.size()])){\n\treturn true;\n }\n }\n }\n rep(j,b.size())if (is_in(a,b[j]))return true;\n return false;\n}\n\n\ndouble distance_l_p(P a,P b,P c){\n return abs( cross(b-a,c-a))/abs(b-a);\n}\n\nbool issame(P &a,P &b){\n return fabs(a.real()-b.real())<eps && fabs(a.imag()-b.imag())<eps;\n}\n\nbool have_same_line(vector<P> &in1,vector<P> &in2,P &a,P &b,bool flag){\n rep(i,in1.size()){\n rep(j,in2.size()){\n if (isp(in1[i],in1[(i+1)%in1.size()],in2[j])\n\t ||isp(in1[i],in1[(i+1)%in1.size()],in2[(j+1)%in2.size()])\n\t //||isp(in2[j],in2[(j+1)%in2.size()],in1[i])||\n\t //\t ||isp(in2[j],in2[(j+1)%in2.size()],in1[(i+1)%in1.size()])\n\t )return true;\n\n if ((issame(in1[i],in2[j])&&issame(in1[(i+1)%in1.size()],in2[(j+1)%in2.size()]))\n\t||\n\t(issame(in1[i],in2[(j+1)%in2.size()])&&issame(in1[(i+1)%in1.size()],in2[j]))\n\t)return true;\n }\n }\n return false;\n}\n\nbool vis[N];\nvoid origami(int &n,int ini,Polygon in[N],P &a,P &b,\n\t pair<P,P> *line,int querry){\n int m = n;\n vector<P> dummy;\n rep(i,N){\n in[i].newp.clear(),in[i].newid=-1;//nen no tame\n }\n\n rep(i,N)vis[i]=false;\n queue<int> Q;\n Q.push(ini);\n while(!Q.empty()){\n int now = Q.front();Q.pop();\n if (vis[now])continue;\n \n vis[now]=true;\n\n Convex_cut(in[now].ori,a,b,in[now].ori,in[now].newp);\n if (in[now].newp.size() != 0){\n in[now].oriid=now;\n in[now].newid=m++;\n in[in[now].newid].cutbit=in[now].cutbit|(1<<querry);\n }\n \n \n // in[now].inipos.clear();\n in[now].inipos=in[now].newp;\n for(int j=querry-1;j>=0;j--){\n if((in[now].cutbit&(1<<j)) != 0){\n\trep(k,in[now].inipos.size()){\n\t in[now].inipos[k]=reflect(line[j].first,line[j].second,in[now].inipos[k]);\n\t}\n }\n }\n \n \n rep(i,n){\n if (i == now)continue;\n if (adj[now][i]){\n\tif(have_same_line(in[now].inipos,in[i].inipos,a,b,false))\n\t Q.push(i);\n }\n if (up[i][now]){\n\tif (is_intersected_polygon(in[now].newp,in[i].ori))\n\t Q.push(i);\n }\n }\n }\n \n rep(i,n){\n rep(j,in[i].newp.size()){\n in[i].newp[j]=reflect(a,b,in[i].newp[j]);\n }\n }\n \n\n rep(i,n){\n int myori=in[i].oriid,mynew=in[i].newid;\n if (in[i].newp.size() == 0)mynew=-1;\n \n if (mynew != -1){\n up[myori][mynew]=false;\n up[mynew][myori]=true;\n }\n \n rep(j,n){\n if (i == j)continue;\n int yourori=in[j].oriid,yournew=in[j].newid;\n bool tmp = up[i][j];\n //in[i].ori-in[j].ori\n up[myori][yourori]=up[myori][yourori];\n\n //in[i].ori-in[j].newp\n if (yournew != -1){\n\tup[myori][yournew]=false;\n }\n\n //in[i].newp-in[j].ori\n if (mynew != -1){\n\tif (is_intersected_polygon(in[i].newp,in[j].ori)){\n\t up[mynew][yourori]=true;\n\t}else {\n\t up[mynew][yourori]=false;\n\t}\n }\n \n //in[i].newp-in[j].ori\n if (mynew != -1 && yournew != -1){\n\tif (up[yourori][myori] && is_intersected_polygon(in[i].newp,in[j].newp))\n\t up[mynew][yournew]=true;\n\telse up[mynew][yournew]=false;\n }\n }\n }\n \n \n \n rep(i,n){\n if (in[i].newp.size() != 0){\n in[in[i].newid].ori=in[i].newp;\n in[in[i].newid].oriid=in[i].newid;\n in[i].newp.clear();\n }\n }\n\n n = m;\n\n // cout << m << endl;\n rep(i,m){\n in[i].inipos.clear();\n in[i].inipos=in[i].ori;\n for(int j=querry;j>=0;j--){\n if((in[i].cutbit&(1<<j)) != 0){\n\trep(k,in[i].inipos.size()){\n\t in[i].inipos[k]=reflect(line[j].first,line[j].second,in[i].inipos[k]);\n\t}\n }\n }\n }\n\n rep(i,m){\n REP(j,i+1,m){\n if (have_same_line(in[i].inipos,in[j].inipos,a,b,true)){\n\tadj[i][j]=adj[j][i]=true;\n }else adj[i][j]=adj[j][i]=false;\n }\n }\n}\n\t \nint solve(int querry){\n int n=1;\n static Polygon in[N];\n pair<P,P> line[10];\n P pos[10];\n // initialization\n rep(i,N){\n in[i].ori.clear();\n in[i].newp.clear();\n in[i].inipos.clear();\n in[i].oriid=i;\n in[i].newid=-1;\n in[i].cutbit=0;\n }\n rep(i,N)rep(j,N)adj[i][j]=false,up[i][j]=false;\n \n in[0].ori.pb(P(0,0));\n in[0].ori.pb(P(0,100));\n in[0].ori.pb(P(100,100));\n in[0].ori.pb(P(100,0));\n in[0].inipos=in[0].ori;\n\n rep(i,querry){\n P p,q;\n cin>>p.real()>>p.imag()>>q.real()>>q.imag();\n pos[i]=p;\n pair<P,P> tmp;\n line[i] = make_line(p,q);\n }\n\n rep(i,querry){\n int index = find_most_upper(n,in,pos[i]);\n if (index == -1){\n }else origami(n,index,in,line[i].first,line[i].second,\n\t\t line,i);\n }\n\n \n int cnt=0;\n P hole;\n cin>>hole.real()>>hole.imag();\n rep(i,n){\n if (in[i].ori.size() && is_in(in[i].ori,hole))cnt++;\n }\n\n return cnt;\n}\n\nmain(){\n int q;\n while(cin>>q&&q){\n cout << solve(q) << endl; \n }\n}\n\n\n\n\nvoid output(int n,Polygon in[N]){\n cout <<\"output begin\" << endl;\n rep(i,n){\n if (in[i].ori.size() == 0)continue;\n outputpolygon(in[i].ori);\n\t\n //outputpolygon(in[i].inipos);\n // else cout <<\"error \" << i << endl;//outputpolygon(in[i].ori);\n }\n cout << endl;\n\n cout << \"up\"<<endl;\n rep(i,n){\n rep(j,n)cout << up[i][j];\n cout << endl;\n }\n cout << endl;\n \n \n cout << \"adj \" << endl;\n rep(i,n){\n rep(j,n)cout << adj[i][j];\n cout <<endl;\n }\n cout << endl;\n \n\n cout <<\"output end\" << endl<<endl;\n}", "accuracy": 1, "time_ms": 4930, "memory_kb": 9584, "score_of_the_acc": -1.1572, "final_rank": 6 } ]
aoj_1296_cpp
Problem B: Repeated Substitution with Sed Do you know "sed," a tool provided with Unix? Its most popular use is to substitute every occurrence of a string contained in the input string (actually each input line) with another string β . More precisely, it proceeds as follows. Within the input string, every non-overlapping (but possibly adjacent) occurrences of α are marked. If there is more than one possibility for non-overlapping matching, the leftmost one is chosen. Each of the marked occurrences is substituted with β to obtain the output string; other parts of the input string remain intact. For example, when α is " aa " and β is " bca ", an input string " aaxaaa " will produce " bcaxbcaa ", but not " aaxbcaa " nor " bcaxabca ". Further application of the same substitution to the string " bcaxbcaa " will result in " bcaxbcbca ", but this is another substitution, which is counted as the second one. In this problem, a set of substitution pairs ( α i , β i ) ( i = 1, 2, ... , n ), an initial string γ , and a final string δ are given, and you must investigate how to produce δ from γ with a minimum number of substitutions. A single substitution ( α i , β i ) here means simultaneously substituting all the non-overlapping occurrences of α i , in the sense described above, with β i . You may use a specific substitution ( α i , β i ) multiple times, including zero times. Input The input consists of multiple datasets, each in the following format. n α 1 β 1 α 2 β 2 . . . α n β n γ δ n is a positive integer indicating the number of pairs. α i and β i are separated by a single space. You may assume that 1 ≤ | α i | < | β i | ≤ 10 for any i (| s | means the length of the string s ), α i ≠ α j for any i ≠ j , n ≤ 10 and 1 ≤ | γ | < | δ | ≤ 10. All the strings consist solely of lowercase letters. The end of the input is indicated by a line containing a single zero. Output For each dataset, output the minimum number of substitutions to obtain δ from γ . If δ cannot be produced from γ with the given set of substitutions, output -1 . Sample Input 2 a bb b aa a bbbbbbbb 1 a aa a aaaaa 3 ab aab abc aadc ad dee abc deeeeeeeec 10 a abc b bai c acf d bed e abh f fag g abe h bag i aaj j bbb a abacfaabe 0 Output for the Sample Input 3 -1 7 4
[ { "submission_id": "aoj_1296_9003254", "code_snippet": "// (⁠◕⁠ᴗ⁠◕⁠✿⁠)\n\n// #pragma GCC target(\"avx2\")\n// #pragma GCC optimize(\"O3\")\n// #pragma GCC optimize(\"unroll-loops\")\n#include <bits/stdc++.h>\n#define rep(i, n) for (ll i = 0; i < (n); i++)\n#define srep(i, s, n) for (ll i = s; i < (n); i++)\n#define len(x) ((int)(x).size())\n#define all(x) (x).begin(), (x).end()\nusing namespace std;\ntemplate<typename T> using vc = vector<T>;\ntemplate<typename T> using vv = vc<vc<T>>;\ntemplate<typename T> using vvv = vv<vc<T>>;\nusing vi = vc<int>;using vvi = vv<int>; using vvvi = vv<vi>;\nusing ll = long long;using vl = vc<ll>;using vvl = vv<ll>; using vvvl = vv<vl>;\nusing ld = long double; using vld = vc<ld>; using vvld = vc<vld>; using vvvld = vc<vvld>;\nusing uint = unsigned int;\nusing ull = unsigned long long;\nconst ld pi = 3.141592653589793;\nconst int inf = 0x3f3f3f3f;\nconst ll INF = 0x3f3f3f3f3f3f3f3f;\n// const ll mod = 1000000007;\nconst ll mod = 998244353;\ninline bool inside(ll y, ll x, ll H, ll W) {return 0 <= (y) and (y) < (H) and 0 <= (x) and (x) < (W); }\n\n#define debug(var) do{std::cout << #var << \" : \\n\";view(var);}while(0)\ntemplate<typename T> void view(T e){cout << e << endl;}\ntemplate<typename T> void view(const vc<T>& v){for(const auto& e : v){ cout << e << \" \"; } cout << endl;}\ntemplate<typename T> void view(const vv<T>& vv){ for(const auto& v : vv){ view(v); } }\n\nint solve(int N){\n vc<pair<string, string>> change(N);\n rep(i, N) cin >> change[i].first >> change[i].second;\n string S, T; cin >> S >> T;\n map<string, int> dist;\n dist[S] = 0;\n dist[T] = inf;\n deque<string> q;\n q.push_back(S);\n while(!q.empty()){\n auto v = q.front(); q.pop_front();\n for (auto [a, b] : change){\n string u = \"\";\n int last = 0;\n for (auto x : v){\n u += x;\n if (len(u) - last >= len(a) && u.substr(len(u) - len(a)) == a){\n rep(i, len(a)) u.pop_back();\n u += b;\n last = len(u);\n }\n }\n if (len(u) > len(T)) continue;\n if (!dist.count(u)) dist[u] = inf;\n if (dist[u] > dist[v] + 1){\n dist[u] = dist[v] + 1;\n q.push_back(u);\n }\n }\n }\n if (dist[T] == inf) return -1;\n return dist[T];\n}\n\nint main(){\n vi ans;\n while (true){\n int N; cin >> N;\n if (N == 0) break;\n ans.push_back(solve(N));\n }\n for (auto x : ans) cout << x << endl;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3616, "score_of_the_acc": -0.3004, "final_rank": 8 }, { "submission_id": "aoj_1296_7858558", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\nbool solve() {\n int N; cin >> N;\n if (N == 0) return false;\n\n vector<pair<string, string>> P(N);\n for (auto& [a, b] : P) cin >> a >> b;\n\n auto sed = [&](string S, int i) -> string {\n const auto& [a, b] = P[i];\n reverse(S.begin(), S.end());\n string res{};\n while (S.size()) {\n if (S.size() < a.size()) {\n while (S.size()) {\n res.push_back(S.back());\n S.pop_back();\n }\n break;\n }\n bool match = true;\n for (int i = 0 ; i < (int)a.size() ; i++) {\n match &= S[S.size() - i - 1] == a[i];\n }\n if (match) {\n for (int i = 0 ; i < (int)a.size() ; i++) S.pop_back();\n for (int i = 0 ; i < (int)b.size() ; i++) res.push_back(b[i]);\n }\n else {\n res.push_back(S.back());\n S.pop_back();\n }\n }\n return res;\n };\n\n string S; cin >> S;\n string G; cin >> G;\n\n auto dfs = [&](auto dfs, string now) -> int {\n // cout << now << endl;\n if (now == G) return 0;\n int res = 1000100;\n if (now.size() > G.size()) return res;\n for (int i = 0 ; i < N ; i++) {\n string nxt = sed(now, i);\n if (now == nxt) continue;\n res = min(res, dfs(dfs, nxt) + 1);\n }\n return res;\n };\n\n int ans = dfs(dfs, S);\n cout << (ans == 1000100 ? -1 : ans) << endl;\n\n return true;\n}\n\nint main() {\n while (solve()) ;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3332, "score_of_the_acc": -0.0198, "final_rank": 2 }, { "submission_id": "aoj_1296_6784158", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\nusing vll = vector<ll>;\nusing vvll = vector<vll>;\nusing vvvll = vector<vvll>;\nusing vvvvll = vector<vvvll>;\nusing vb = vector<bool>;\nusing vvb = vector<vb>;\nusing vvvb = vector<vvb>;\nusing vd = vector<double>;\nusing vvd = vector<vd>;\nusing vvvd = vector<vvd>;\n#define all(A) A.begin(),A.end()\n#define ALL(A) A.begin(),A.end()\n#define rep(i, n) for (ll i = 0; i < (ll) (n); i++)\nusing pqr = priority_queue<pair<ll, ll>, vector<pair<ll, ll>>, greater<pair<ll, ll>>>;\n\ntemplate<class T>\nbool chmax(T& p, T q, bool C = 1) {\n if (C == 0 && p == q) {\n return 1;\n }\n if (p < q) {\n p = q;\n return 1;\n }\n else {\n return 0;\n }\n}\n\ntemplate<class T>\nbool chmin(T& p, T q, bool C = 1) {\n if (C == 0 && p == q) {\n return 1;\n }\n if (p > q) {\n p = q;\n return 1;\n }\n else {\n return 0;\n }\n}\n\nll gcd(ll(a), ll(b)) {\n if (b == 0)return a;\n ll c = a;\n while (a % b != 0) {\n c = a % b;\n a = b;\n b = c;\n }\n return b;\n}\n\nvector<ll> fact, factinv, inv;\nll mod = 1e9 + 7;\nvoid prenCkModp(ll n) {\n fact.resize(n + 5);\n factinv.resize(n + 5);\n inv.resize(n + 5);\n fact.at(0) = fact.at(1) = 1;\n factinv.at(0) = factinv.at(1) = 1;\n inv.at(1) = 1;\n for (ll i = 2; i < n + 5; i++) {\n fact.at(i) = (fact.at(i - 1) * i) % mod;\n inv.at(i) = mod - (inv.at(mod % i) * (mod / i)) % mod;\n factinv.at(i) = (factinv.at(i - 1) * inv.at(i)) % mod;\n }\n\n}\nll nCk(ll n, ll k) {\n if (k < 0)return 0;\n if (n < k) return 0;\n return fact.at(n) * (factinv.at(k) * factinv.at(n - k) % mod) % mod;\n}\nnamespace atcoder {\n\n namespace internal {\n\n // @param n `0 <= n`\n // @return minimum non-negative `x` s.t. `n <= 2**x`\n int ceil_pow2(int n) {\n int x = 0;\n while ((1U << x) < (unsigned int)(n)) x++;\n return x;\n }\n\n // @param n `1 <= n`\n // @return minimum non-negative `x` s.t. `(n & (1 << x)) != 0`\n int bsf(unsigned int n) {\n#ifdef _MSC_VER\n unsigned long index;\n _BitScanForward(&index, n);\n return index;\n#else\n return __builtin_ctz(n);\n#endif\n }\n\n } // namespace internal\n\n} // namespace atcoder\n\nnamespace atcoder {\n\n template <class S,\n S(*op)(S, S),\n S(*e)(),\n class F,\n S(*mapping)(F, S),\n F(*composition)(F, F),\n F(*id)()>\n struct lazy_segtree {\n public:\n lazy_segtree() : lazy_segtree(0) {}\n lazy_segtree(int n) : lazy_segtree(std::vector<S>(n, e())) {}\n lazy_segtree(const std::vector<S>& v) : _n(int(v.size())) {\n log = internal::ceil_pow2(_n);\n size = 1 << log;\n d = std::vector<S>(2 * size, e());\n lz = std::vector<F>(size, id());\n for (int i = 0; i < _n; i++) d[size + i] = v[i];\n for (int i = size - 1; i >= 1; i--) {\n update(i);\n }\n }\n\n void set(int p, S x) {\n //assertrt(0 <= p && p < _n);\n p += size;\n for (int i = log; i >= 1; i--) push(p >> i);\n d[p] = x;\n for (int i = 1; i <= log; i++) update(p >> i);\n }\n\n S get(int p) {\n //assertrt(0 <= p && p < _n);\n p += size;\n for (int i = log; i >= 1; i--) push(p >> i);\n return d[p];\n }\n\n S prod(int l, int r) {\n //assertrt(0 <= l && l <= r && r <= _n);\n if (l == r) return e();\n\n l += size;\n r += size;\n\n for (int i = log; i >= 1; i--) {\n if (((l >> i) << i) != l) push(l >> i);\n if (((r >> i) << i) != r) push(r >> i);\n }\n\n S sml = e(), smr = e();\n while (l < r) {\n if (l & 1) sml = op(sml, d[l++]);\n if (r & 1) smr = op(d[--r], smr);\n l >>= 1;\n r >>= 1;\n }\n\n return op(sml, smr);\n }\n\n S all_prod() { return d[1]; }\n\n void apply(int p, F f) {\n //assertrt(0 <= p && p < _n);\n p += size;\n for (int i = log; i >= 1; i--) push(p >> i);\n d[p] = mapping(f, d[p]);\n for (int i = 1; i <= log; i++) update(p >> i);\n }\n void apply(int l, int r, F f) {\n //assertrt(0 <= l && l <= r && r <= _n);\n if (l == r) return;\n\n l += size;\n r += size;\n\n for (int i = log; i >= 1; i--) {\n if (((l >> i) << i) != l) push(l >> i);\n if (((r >> i) << i) != r) push((r - 1) >> i);\n }\n\n {\n int l2 = l, r2 = r;\n while (l < r) {\n if (l & 1) all_apply(l++, f);\n if (r & 1) all_apply(--r, f);\n l >>= 1;\n r >>= 1;\n }\n l = l2;\n r = r2;\n }\n\n for (int i = 1; i <= log; i++) {\n if (((l >> i) << i) != l) update(l >> i);\n if (((r >> i) << i) != r) update((r - 1) >> i);\n }\n }\n\n template <bool (*g)(S)> int max_right(int l) {\n return max_right(l, [](S x) { return g(x); });\n }\n template <class G> int max_right(int l, G g) {\n //assertrt(0 <= l && l <= _n);\n //assertrt(g(e()));\n if (l == _n) return _n;\n l += size;\n for (int i = log; i >= 1; i--) push(l >> i);\n S sm = e();\n do {\n while (l % 2 == 0) l >>= 1;\n if (!g(op(sm, d[l]))) {\n while (l < size) {\n push(l);\n l = (2 * l);\n if (g(op(sm, d[l]))) {\n sm = op(sm, d[l]);\n l++;\n }\n }\n return l - size;\n }\n sm = op(sm, d[l]);\n l++;\n } while ((l & -l) != l);\n return _n;\n }\n\n template <bool (*g)(S)> int min_left(int r) {\n return min_left(r, [](S x) { return g(x); });\n }\n template <class G> int min_left(int r, G g) {\n //assertrt(0 <= r && r <= _n);\n //assertrt(g(e()));\n if (r == 0) return 0;\n r += size;\n for (int i = log; i >= 1; i--) push((r - 1) >> i);\n S sm = e();\n do {\n r--;\n while (r > 1 && (r % 2)) r >>= 1;\n if (!g(op(d[r], sm))) {\n while (r < size) {\n push(r);\n r = (2 * r + 1);\n if (g(op(d[r], sm))) {\n sm = op(d[r], sm);\n r--;\n }\n }\n return r + 1 - size;\n }\n sm = op(d[r], sm);\n } while ((r & -r) != r);\n return 0;\n }\n\n private:\n int _n, size, log;\n std::vector<S> d;\n std::vector<F> lz;\n\n void update(int k) { d[k] = op(d[2 * k], d[2 * k + 1]); }\n void all_apply(int k, F f) {\n d[k] = mapping(f, d[k]);\n if (k < size) lz[k] = composition(f, lz[k]);\n }\n void push(int k) {\n all_apply(2 * k, lz[k]);\n all_apply(2 * k + 1, lz[k]);\n lz[k] = id();\n }\n };\n\n} // \nusing namespace atcoder;\n\nstruct S {\n ll a;\n int size;\n};\n\nstruct F {\n ll a, b;\n};\n\nS op(S l, S r) { return S{ min(l.a,r.a), l.size + r.size }; }\n\nS e() { return S{ ll(1e18), 0 }; }\n\nS mapping(F l, S r) { return S{ r.a * l.a + r.size * l.b, r.size }; }\n\nF composition(F l, F r) { return F{ r.a * l.a, r.b * l.a + l.b }; }\n\nF id() { return F{ 1, 0 }; }\n\nint main() {\n cin.tie(nullptr);\n ios::sync_with_stdio(false);\n\n while (1) {\n ll N;\n cin >> N;\n if (N == 0)return 0;\n vector<string> A(N), B(N);\n rep(i, N)cin >> A[i] >> B[i];\n string S, T;\n cin >> S >> T;\n set<string> seen;\n map<string, ll> AN;\n AN[S] = 0;\n queue<string> que;\n que.push(S);\n while (!que.empty()) {\n string p = que.front();\n que.pop();\n if (p.size() > 10)continue;\n if (seen.count(p))continue;\n seen.insert(p);\n rep(i, N) {\n string res = \"\";\n rep(j, p.size()) {\n if (j+A[i].size()<=p.size()&&p.substr(j, A[i].size()) == A[i]) {\n res = res + B[i];\n j += A[i].size() - 1;\n }\n else {\n res.push_back(p[j]);\n }\n }\n if (!seen.count(res)) {\n if (AN.count(res) && AN[res] <= AN[p] + 1)continue;\n AN[res] = AN[p] + 1;\n que.push(res);\n }\n }\n }\n cout << (seen.count(T) ? AN[T] : -1) << endl;\n }\n\n\n\n}", "accuracy": 1, "time_ms": 20, "memory_kb": 4300, "score_of_the_acc": -1.1763, "final_rank": 16 }, { "submission_id": "aoj_1296_6728499", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\n\nint main(){\n int n;\n while(cin >> n, n){\n int ans = -1;\n vector<array<string,2>> a(n);\n for(int i = 0; i < n; i++){\n cin >> a[i][0] >> a[i][1];\n }\n string from, to;\n cin >> from >> to;\n map<string,int> mp;\n queue<string> nxt;\n mp[from] = 0;\n nxt.push(from);\n auto f = [&](string s, string t, string u){\n string res, res2;\n int tn = t.size();\n for(int i = 0; i < s.size(); i++){\n res += s[i];\n if(res.size() < tn)continue;\n if(res.substr(res.size() - tn, tn) == t){\n for(int j = 0; j < tn; j++)res.pop_back();\n res += u;\n res += \"{\";\n }\n }\n for(int i = 0; i < res.size(); i++){\n if(res[i] == '{')continue;\n res2 += res[i];\n }\n return res2;\n };\n while(!nxt.empty()){\n string s;\n s = nxt.front();\n nxt.pop();\n if(s.size() > to.size())continue;\n int d = mp[s];\n if(s == to){\n ans = d;\n break;\n }\n for(int i = 0; i < n; i++){\n string t = f(s, a[i][0], a[i][1]);\n if(mp.find(t) == mp.end()){\n mp[t] = d + 1;\n nxt.push(t);\n }\n }\n }\n cout << ans << '\\n';\n }\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3872, "score_of_the_acc": -0.5534, "final_rank": 13 }, { "submission_id": "aoj_1296_6039307", "code_snippet": "#include <bits/stdc++.h>\n#define rep(i,n) for(int i = 0; i < (n); i++)\nusing namespace std;\ntypedef long long ll;\n\nint main(){\n\tcin.tie(0);\n\tios::sync_with_stdio(0);\n\t\n\twhile(true) {\n\t\tint n; cin >> n; if(n == 0) break;\n\t\tvector<string> a(n), b(n);\n\t\trep(i,n) cin >> a[i] >> b[i];\n\t\tstring c, d; cin >> c >> d;\n\n\t\tmap<string,int> mp;\n\t\tmp[c] = 0;\n\t\tqueue<string> q;\n\t\tq.push(c);\n\n\t\twhile(!q.empty()) {\n\t\t\tstring v = q.front(); q.pop();\n\t\t\trep(i,n) {\n\t\t\t\tstring to = \"\";\n\t\t\t\trep(j,v.size()) {\n\t\t\t\t\tif(j <= v.size() - a[i].size()) {\n\t\t\t\t\t\tif(v.substr(j, a[i].size()) == a[i]) {\n\t\t\t\t\t\t\tto += b[i];\n\t\t\t\t\t\t\tj += a[i].size() - 1;\n\t\t\t\t\t\t} else {\n\t\t\t\t\t\t\tto += v[j];\n\t\t\t\t\t\t}\n\t\t\t\t\t} else {\n\t\t\t\t\t\tto += v[j];\n\t\t\t\t\t}\n\t\t\t\t}\n\n\t\t\t\tif(!mp.count(to) && to.size() <= d.size()) {\n\t\t\t\t\tmp[to] = mp[v] + 1;\n\t\t\t\t\tq.push(to);\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\n\t\tcout << (mp.count(d) ? mp[d] : -1) << '\\n';\n\t}\n}", "accuracy": 1, "time_ms": 30, "memory_kb": 3384, "score_of_the_acc": -0.4711, "final_rank": 11 }, { "submission_id": "aoj_1296_6029621", "code_snippet": "#include<bits/stdc++.h>\n#include <regex>\nusing namespace std;\nconst int INF = 1e9;\n#define keta(n, x) cout << fixed << setprecision((int)n) << (double)x << endl;\n\nint BFS(int n){\n vector<pair<string, string>> S(n);\n set<string> check;\n int ans = -1;\n for(int i = 0; i < n; i++){\n string a, b;\n cin >> a >> b;\n S[i] = make_pair(a, b);\n }\n string g, d;\n cin >> g >> d;\n queue<string> que;\n que.push(g); //初期状態\n check.insert(g);\n map<string, int> cnt; //置換回数\n cnt[g] = 0;\n int fin_size = d.size();\n\n bool ok = false;\n while(!que.empty()){\n //queの先頭を取り出す.\n string T = que.front();\n que.pop();\n\n //文字列の置換パート\n string old_T = T;\n for(int i = 0; i < n; i++){\n //文字列の置換\n string new_T = regex_replace(old_T, regex(S[i].first), S[i].second);\n //常に置換済みであればキューに入れない.\n if(!check.count(new_T)){\n check.insert(new_T);\n if(new_T == d) ok = true;\n //文字列の長さがターゲットより大きければ入れない\n if(new_T.size() <= fin_size){\n que.push(new_T);\n }else{\n continue;\n }\n }else{\n continue;\n }\n if(!cnt.count(new_T)) cnt[new_T] = cnt[old_T] + 1;\n }\n }\n if(ok){\n return cnt[d];\n }else{\n return -1;\n }\n} \n\nint main(){\n while(1){ \n int n;\n cin >> n;\n if(n == 0) break;\n cout << BFS(n) << endl;\n }\n}", "accuracy": 1, "time_ms": 50, "memory_kb": 4244, "score_of_the_acc": -1.7209, "final_rank": 18 }, { "submission_id": "aoj_1296_6029381", "code_snippet": "#include <iostream>\n#include <cstdio>\n#include <cmath>\n#include <algorithm>\n#include <string>\n#include <map>\n#include <bitset>\n#include <vector>\n#include <queue>\n#include <set>\n\nusing namespace std;\n\ntypedef long long ll;\n#define FOR(i,a,b) for(ll i = (a); i < (b); i++ )\n#define REP(i, n) FOR(i,0,n)\ntypedef pair< ll, ll > cp2;\ntypedef pair< string, cp2 > cp3;\ntypedef pair< int, string > pis;\n#define fi first\n#define se second\n#define sec se.fi\n#define thr se.se\n#define V_MAX 112345\nconst ll mod = 998244353;\n// 123456789\n\nint N;\nstring S;\nstring T;\nstring X, Y;\n\npair< string, string > A[11];\npis tmp;\npriority_queue< pis, vector<pis>, greater<pis> > pq;\n\nint main(){\n \n cin>>N;\n while( N ){\n while( !pq.empty() ) pq.pop();\n bool f = true;\n REP( i, N ){\n cin>>X>>Y;\n A[i] = make_pair( X, Y );\n }\n cin>>S>>T;\n tmp = make_pair( 0, S );\n pq.push( tmp );\n while( !pq.empty() ){\n tmp = pq.top();\n string tmpS = tmp.se;\n pq.pop();\n if( tmpS == T ){\n cout<<tmp.fi<<endl;\n f = false;\n break;\n }\n if( tmpS.size() > T.size() ) continue;\n REP( i, N ){\n string B = tmpS;\n string::size_type pos = B.find( A[i].fi );\n while( pos != string::npos ){\n B.replace( pos, A[i].fi.size(), A[i].se );\n pos = B.find( A[i].fi, pos+A[i].se.size() );\n }\n if( B != tmpS ) pq.push( make_pair(tmp.fi+1, B));\n }\n }\n if( f ) puts(\"-1\");\n cin>>N;\n }\n \n \n \n return 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3724, "score_of_the_acc": -0.4071, "final_rank": 9 }, { "submission_id": "aoj_1296_6029263", "code_snippet": "//#define _GLIBCXX_DEBUG\n#include <bits/stdc++.h>\nusing namespace std;\n// #include <atcoder/all>\n// using namespace atcoder;\n// using mint = modint1000000007;\nusing ll = long long;\nconst int MOD = (int)1e9 + 7;\nconst int INF = (int)1e9 + 1001010;\nconst ll llINF = (ll)1e18 + 11000010;\nconst double PI = 3.14159265358979;\n#define ALL(x) x.begin(),x.goal()\n#define RALL(x) x.rbegin(),x.rend()\n#define endn \"\\n\"\n\nvector<string> a, b;\nvector<int> d;\nstring start, goal;\n\ntemplate <typename T>\nbool chmax(T &a, const T &b){\n if(a < b) a = b;\n return a < b;\n}\n\ntemplate <typename T>\nbool chmin(T &a, const T &b){\n if(a > b) a = b;\n return a > b;\n}\n\nstring my_replace(string tg, string rep_from, string rep_to){\n int head = 0;\n \n while(tg.find(rep_from, head) != string::npos){\n int fi = tg.find(rep_from, head);\n tg.replace(fi, rep_from.size(), rep_to);\n head = rep_to.size() + fi;\n }\n return tg;\n}\n\n\nint dfs(string st, int count){\n // base case\n if(st == goal) return count;\n\n // recursion step\n int ret = INF;\n for(int i = 0; i < a.size(); i++){\n if(st.find(a[i]) != string::npos){\n string r = my_replace(st, a[i], b[i]);\n if(r.size() <= goal.size()){\n chmin(ret, dfs(r, count + 1));\n }\n }\n }\n\n return ret;\n}\n\nint main(){\n while(true){\n int n; cin >> n;\n if(n == 0) break;\n\n a.resize(n);\n b.resize(n);\n d.resize(n);\n for(int i = 0; i < n; i++) cin >> a[i] >> b[i];\n for(int i = 0; i < n; i++) d[i] = (int)b[i].size() - (int)a[i].size();\n\n cin >> start >> goal;\n\n int ans = dfs(start, 0);\n if(ans == INF) ans = -1;\n cout << ans << endn;\n }\n return 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3368, "score_of_the_acc": -0.0553, "final_rank": 3 }, { "submission_id": "aoj_1296_6029153", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n#define rep(i,n) for(int (i)=0;(i)<(n);(i)++)\n#define ll long long\n#define pp pair<ll,ll>\n#define ld long double\n#define all(a) (a).begin(),(a).end()\n#define mk make_pair\n#define difine define\nconstexpr int inf=1000001000;\nconstexpr ll INF=2e18;\nconstexpr ll MOD=1000000007;\nconstexpr ll mod=998244353;\nint dx[4]={1,-1,-1,1},dy[4]={1,-1,1,-1};\n\n \n// #include <atcoder/all>\n// typedef atcoder::modint1000000007 mint;\n// typedef atcoder::modint998244353 mint;\n\nint main() {\n while(true){\n int n;\n cin >> n;\n if (n==0) break;\n vector<string> a(n),b(n);\n rep(i,n) cin >> a[i] >> b[i];\n string s,t;\n cin >> s >> t;\n queue<pair<string,int>> q;\n q.push({s,0});\n int mi=inf;\n set<string> se;\n se.insert(s);\n while(!q.empty()){\n auto [f,g]=q.front();\n // cout << f << endl;\n q.pop();\n rep(i,n){\n int j=0;\n string ans;\n while(true){\n if (j+a[i].size()>f.size()){\n for (int k=j;k<f.size();k++) ans.push_back(f[k]);\n break;\n }\n bool t=true;\n rep(k,a[i].size()){\n if (f[j+k]!=a[i][k]) t=false;\n }\n if (t){\n ans=ans+b[i];\n j+=a[i].size();\n }\n else{\n ans.push_back(f[j]);\n j++;\n }\n }\n if (ans==t) mi=min(mi,g+1);\n else if (se.find(ans)==se.end() && ans.size()<=10){\n se.insert(ans);\n q.push({ans,g+1});\n }\n }\n }\n if (mi==inf) cout << -1 << endl;\n else cout << mi << endl;\n }\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3520, "score_of_the_acc": -0.2055, "final_rank": 5 }, { "submission_id": "aoj_1296_6028961", "code_snippet": "#define _GLIBCXX_DEBUG // test only\n\n#include <bits/stdc++.h>\nusing namespace std;\n\n#define INF 1020304050\n\nint main() {\n \n ios::sync_with_stdio(false);\n cin.tie(0);\n \n int n;\n cin >> n;\n while (n != 0) {\n\t\tvector<string> a(n),b(n);\n\t\tvector<int> alen(n);\n\t\tfor (int i=0;i<n;i++) {\n\t\t\tcin >> a[i] >> b[i];\n\t\t\talen[i] = a[i].size();\n\t\t}\n\t\tstring s,g;\n\t\tcin >> s >> g;\n\t\tqueue<string> q;\n\t\tq.push(s);\n\t\tmap<string,int> mp;\n\t\tmp[s] = 1;\n\t\twhile (!q.empty()) {\n\t\t\tstring now = q.front();\n\t\t\tint sz = now.size();\n\t\t\tq.pop();\n\t\t\tfor (int i=0;i<n;i++) {\n\t\t\t\tstring to = \"\";\n\t\t\t\tint j = 0;\n\t\t\t\twhile (j <= sz-alen[i]) {\n\t\t\t\t\tif (now.substr(j,alen[i]) == a[i]) {\n\t\t\t\t\t\tto += b[i];\n\t\t\t\t\t\tj += alen[i];\n\t\t\t\t\t}\n\t\t\t\t\telse if (j < now.size()) {\n\t\t\t\t\t\tto += now[j];\n\t\t\t\t\t\tj++;\n\t\t\t\t\t}\n\t\t\t\t\telse break;\n\t\t\t\t}\n\t\t\t\twhile (j < sz) {\n\t\t\t\t\tto += now[j];\n\t\t\t\t\tj++;\n\t\t\t\t}\n\t\t\t\tif (to == now) continue;\n\t\t\t\tif (mp[to] == 0) {\n\t\t\t\t\tmp[to] = mp[now]+1;\n\t\t\t\t}\n\t\t\t\tif (to.size() >= g.size()) continue;\n\t\t\t\tq.push(to);\n\t\t\t}\n\t\t}\n\t\tcout << mp[g]-1 << endl;\n cin >> n;\n }\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3500, "score_of_the_acc": -0.1858, "final_rank": 4 }, { "submission_id": "aoj_1296_6023701", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\nstring sed(string &u, string &a, string &b) {\n string res = \"\";\n for (int i = 0; i < u.size(); i++) {\n if (i + a.size() > u.size()) {\n res += u[i];\n continue;\n }\n if (u.substr(i, a.size()) == a) {\n res += b;\n i += a.size() - 1;\n }\n else {\n res += u[i];\n }\n }\n return res;\n}\n\nint main() {\n cin.tie(0);\n ios::sync_with_stdio(false);\n\n while (true) {\n int n;\n cin >> n;\n\n if (n == 0) return 0;\n\n vector<string> a(n), b(n);\n for (int i = 0; i < n; i++) cin >> a.at(i) >> b.at(i);\n\n string s, t;\n cin >> s >> t;\n\n int ans = 1e9;\n queue<pair<string, int>> q;\n q.push(make_pair(s, 0));\n while (!q.empty()) {\n auto [u, d] = q.front();\n q.pop();\n if (u.size() > t.size()) continue;\n if (u == t) {\n ans = d;\n break;\n }\n for (int i = 0; i < n; i++) {\n string u2 = sed(u, a.at(i), b.at(i));\n if (u2 == u) continue;\n //cout << u2 << endl;\n q.push(make_pair(u2, d + 1));\n }\n }\n\n if (ans == 1e9) cout << -1 << '\\n';\n else cout << ans << '\\n';\n }\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3832, "score_of_the_acc": -0.5138, "final_rank": 12 }, { "submission_id": "aoj_1296_5887059", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\nint main() {\n int N;\n while (cin >> N, N) {\n vector<pair<string, string>> V(N);\n for (auto&& [a, b] : V) {\n cin >> a >> b;\n }\n string S, T;\n cin >> S >> T;\n queue<pair<int, string>> que;\n que.push({0, S});\n map<string, int> maine;\n maine[S] = 0;\n while (!que.empty()) {\n auto [n, S] = que.front();\n que.pop();\n if (S == T)\n break;\n if (S.size() > T.size())\n continue;\n for (auto&& [a, b] : V) {\n string U = regex_replace(S, regex(a), b);\n if (U != S && !maine.count(U)) {\n maine[U] = n + 1;\n que.push({n + 1, U});\n }\n }\n }\n cout << (maine.count(T) ? maine[T] : -1) << endl;\n }\n}", "accuracy": 1, "time_ms": 30, "memory_kb": 4072, "score_of_the_acc": -1.151, "final_rank": 15 }, { "submission_id": "aoj_1296_5886116", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\nusing P = pair<string,string>;\nmap<string,int> s;\nint ans = -1;\n\nstring process(P &p, string S){\n auto[x,y] = p;\n string ret = \"\";\n int pos = 0;\n while(pos < S.size()){\n if(pos + x.size() > S.size()){\n ret += S[pos];\n pos++;\n }\n else if(S.substr(pos,x.size()) == x){\n ret += y;\n pos += x.size();\n }\n else{\n ret += S[pos];\n pos++;\n }\n }\n return ret;\n}\n\nvoid dfs(vector<P> &vec, string S, string &T, int c){\n for(int i=0; i<vec.size(); i++){\n string t = process(vec[i],S);\n if(t == T){\n if(ans == -1){\n ans = c+1;\n }\n ans = min(ans,c+1);\n }\n if(!s.count(t) && T.size() > t.size()){\n s[t] = c+1;\n dfs(vec,t,T,c+1);\n }\n else if(s[t] > c+1 && T.size() > t.size()){\n s[t] = c+1;\n dfs(vec,t,T,c+1);\n }\n }\n}\n\nvoid solve(int N){\n vector<P> vec(N);\n s.clear();\n ans = -1;\n for(int i=0; i<N; i++){\n string a, b;\n cin >> a >> b;\n vec[i] = P(a,b);\n }\n string S, T;\n cin >> S >> T;\n dfs(vec,S,T,0);\n cout << ans << endl;\n}\n\nint main(){\n while(true){\n int N;\n cin >> N;\n if(N == 0){\n break;\n }\n solve(N);\n }\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3552, "score_of_the_acc": -0.2372, "final_rank": 6 }, { "submission_id": "aoj_1296_5615160", "code_snippet": "#include <cmath>\n#include <deque>\n#include <algorithm>\n#include <iterator>\n#include <list>\n#include <tuple>\n#include <map>\n#include <unordered_map>\n#include <queue>\n#include <set>\n#include <unordered_set>\n#include <stack>\n#include <string>\n#include <vector>\n#include <fstream>\n#include <iostream>\n#include <functional>\n#include <numeric>\n#include <iomanip> \n#include <stdio.h>\n#include <assert.h>\n//eolibraries\n#define lnf 3999999999999999999\n#define inf 999999999\n#define fi first\n#define se second\n#define pb push_back\n#define ll long long\n#define ld long double\n#define all(c) (c).begin(),(c).end()\n#define sz(c) (int)(c).size()\n#define make_unique(a) sort(all(a)),a.erase(unique(all(a)),a.end())\n#define pii pair <int,int>\n#define rep(i,n) for(int i = 0 ; i < n ; i++) \n#define drep(i,n) for(int i = n-1 ; i >= 0 ; i--)\n#define crep(i,x,n) for(int i = x ; i < n ; i++)\n#define vi vector <int> \n#define vec(...) vector<__VA_ARGS__>\n#define fcin ios_base::sync_with_stdio(false),cin.tie(0),cout.tie(0);\n//eodefine\nusing namespace std;\n\nconst int max_n = 103002;\nmap<string,string> mp;\nmap<string,int> memo;\n\nvoid dfs(string a,string b,int cost){\n\tint n=sz(a),m=sz(b);\n\tif(n>m) return;\n\tif(memo.find(a)!=memo.end() and memo[a]<cost) return;\n\tmemo[a]=cost;\n\n\tfor(auto [x,y] : mp){\n\t\tint sznow=sz(x);\n\t\tvi usd(n,0);\n\t\tfor(int i=0;i<n;i++){\n\t\t\tif(a.substr(i,sznow)==x){\n\t\t\t\tusd[i]=1;\n\t\t\t\ti+=sznow-1;\n\t\t\t}\n\t\t}\n\t\tstring now=\"\";\n\t\tfor(int i=0;i<n;i++){\n\t\t\tif(usd[i]){\n\t\t\t\tnow+=y;\n\t\t\t\ti+=sznow-1;\n\t\t\t}else{\n\t\t\t\tnow+=a[i];\n\t\t\t}\n\t\t}\n\t\tdfs(now,b,cost+1);\n\t}\n\t// return;\n}\n\nvoid slv(int n){\n\tstring s;\n\tgetline(cin,s);\n\n\tmp.clear();\n\tmemo.clear();\n\t\n\trep(i,n){\n\t\tgetline(cin,s);\t\n\t\t\n\t\tint spc=s.find(' ');\n\t\tif(spc==-1){\n\t\t\tmp[s]=\"\";\n\t\t}else{\n\t\t\tmp[s.substr(0,spc)]=s.substr(spc+1);\n\t\t}\n\t}\n\t\n\tstring a,b;\n\tcin>>a>>b;\n\n\tdfs(a,b,0);\n\n\tif(memo.find(b)!=memo.end()){\n\t\tcout<<memo[b]<<\"\\n\";\n\t}else{\n\t\tcout<<\"-1\\n\";\n\t}\n\n}\n\nint main(){\nfcin;\n\twhile(true){\n\t\tint n;\n\t\tcin>>n;\n\t\tif(!n)break;\n\t\tslv(n);\n\t}\n/*\n*/\n return 0; \n}", "accuracy": 1, "time_ms": 40, "memory_kb": 3532, "score_of_the_acc": -0.8174, "final_rank": 14 }, { "submission_id": "aoj_1296_5277351", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n#define rep(i, n) for(int i = 0; i < (n); ++i)\nconst int INF = 30;\n\nstring Replace(const string &str, const string &a, const string &b) {\n string res = \"\";\n for(int i = 0; i < str.size(); ++i) {\n if(str.substr(i, a.size()) == a) {\n res += b;\n i += a.size() - 1;\n } else {\n res += str[i];\n }\n }\n return res;\n}\n\nbool solve() {\n int n; cin >> n;\n if(n == 0) return false;\n vector<pair<string, string>> sub(n);\n for(auto &[a, b] : sub) cin >> a >> b;\n string bef, aft;\n cin >> bef >> aft;\n\n auto f = [&](auto &&f, string s, int x) -> int {\n if(s.size() > aft.size()) return INF;\n if(s == aft) return x;\n int res = INF;\n for(auto &[a, b] : sub) {\n if(s == Replace(s, a, b)) continue;\n res = min(res, f(f, Replace(s, a, b), x + 1));\n }\n return res;\n };\n\n int ans = f(f, bef, 0);\n if(ans == INF) ans = -1;\n cout << ans << '\\n';\n return true;\n}\n\nint main() {\n while(solve());\n return 0;\n}", "accuracy": 1, "time_ms": 30, "memory_kb": 3376, "score_of_the_acc": -0.4632, "final_rank": 10 }, { "submission_id": "aoj_1296_5058702", "code_snippet": "#include <iostream>\n#include <queue>\n#include <map>\nusing namespace std;\n\nvoid transform(string &s, string &a, string &b){\n int i = 0;\n while(true){\n if(i + (int)a.size() > (int)s.size()) return;\n if(s.substr(i, (int)a.size()) == a){\n s = s.substr(0, i) + b + s.substr(i + (int)a.size());\n i += (int)b.size();\n }\n else i++;\n }\n}\n\nint main()\n{\n while(true){\n int n;\n cin >> n;\n if(n == 0) break;\n string a[12], b[12];\n for(int i = 0; i < n; i++) cin >> a[i] >> b[i];\n string c, d;\n cin >> c >> d;\n map<string, int> mp;\n queue<string> que;\n mp[c] = 0;\n que.push(c);\n while(que.size()){\n string s = que.front();\n que.pop();\n if((int)s.size() > 10) continue;\n for(int i = 0; i < n; i++){\n string t = s;\n transform(t, a[i], b[i]);\n if(!mp.count(t)){\n mp[t] = mp[s] + 1;\n que.push(t);\n }\n }\n }\n if(!mp.count(d)) cout << -1 << endl;\n else cout << mp[d] << endl;\n }\n}", "accuracy": 1, "time_ms": 50, "memory_kb": 4096, "score_of_the_acc": -1.5747, "final_rank": 17 }, { "submission_id": "aoj_1296_4940017", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\nint solve() {\n int n;\n cin >> n;\n if (n == 0) return 1;\n vector<string> a(n), b(n);\n for (int i = 0; i < n; ++i) cin >> a[i] >> b[i];\n string s, t;\n cin >> s >> t;\n\n set<string> st;\n auto dfs = [&](auto self, string now, int time) -> int {\n st.insert(now);\n if (now.size() == t.size()) return now == t ? time : INT_MAX;\n int res = INT_MAX;\n for (int i = 0; i < n; ++i) {\n string nxt = regex_replace(now, regex(a[i]), b[i]);\n if (nxt.size() <= t.size() && !st.count(nxt)) res = min(res, self(self, nxt, time + 1));\n }\n return res;\n };\n\n int ans = dfs(dfs, s, 0);\n cout << (ans == INT_MAX ? -1 : ans) << '\\n';\n\n return 0;\n}\n\nint main() {\n while (!solve());\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3312, "score_of_the_acc": 0, "final_rank": 1 }, { "submission_id": "aoj_1296_4918403", "code_snippet": "#include <bits/stdc++.h>\n//#include <atcoder/all>\n//using namespace atcoder;\nconst double pi = 3.141592653589793238462643383279;\nusing namespace std;\n//typedef\n//------------------------------------------\ntypedef vector<int> VI;\ntypedef vector<VI> VVI;\ntypedef vector<string> VS;\ntypedef pair<int, int> PII;\ntypedef pair<long long, long long> PLL;\ntypedef pair<int, PII> TIII;\ntypedef long long LL;\ntypedef unsigned long long ULL;\ntypedef vector<LL> VLL;\ntypedef vector<VLL> VVLL;\n\n//container util\n//------------------------------------------\n#define ALL(a) (a).begin(), (a).end()\n#define RALL(a) (a).rbegin(), (a).rend()\n#define PB push_back\n#define MP make_pair\n#define SZ(a) int((a).size())\n#define SQ(a) ((a) * (a))\n#define EACH(i, c) for (typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)\n#define EXIST(s, e) ((s).find(e) != (s).end())\n#define SORT(c) sort((c).begin(), (c).end())\n\n//repetition\n//------------------------------------------\n#define FOR(i, s, n) for (int i = s; i < (int)n; ++i)\n#define REP(i, n) FOR(i, 0, n)\n#define MOD 1000000007\n\n#define rep(i, a, b) for (int i = a; i < (b); ++i)\n#define trav(a, x) for (auto &a : x)\n#define all(x) x.begin(), x.end()\n#define sz(x) (int)(x).size()\n\ntypedef long long ll;\ntypedef pair<int, int> pii;\ntypedef vector<int> vi;\n\n#define chmin(x, y) x = min(x, y)\n#define chmax(x, y) x = max(x, y)\nconst double EPS = 1e-11, PI = acos(-1);\n//ここから編集\n\nclass KMP\n{\npublic:\n string p;\n int plen;\n vector<int> mp;\n vector<int> kmp;\n \n /* MPとKMPの構築 計算量O(logN) */\n KMP(string s) : p(s), plen((int)p.size()), mp(plen + 1), kmp(plen + 1)\n {\n mp[0] = kmp[0] = -1;\n int j = -1;\n for (int i = 0; i < plen; i++)\n {\n while (j >= 0 && p[i] != p[j])\n j = kmp[j];\n \n kmp[i + 1] = mp[i + 1] = ++j;\n \n if (i + 1 < plen && p[i + 1] == p[j])\n kmp[i + 1] = kmp[j];\n }\n }\n \n //文字列patternのS[0~i]の最小の周期長を求める\n //計算量:O(N)\n vector<ll> cycle()\n {\n vector<ll> res(plen);\n for (int i = 0; i < plen; i++)\n {\n res[i] = i - kmp[i];\n }\n return res;\n }\n \n /* MPを使った文字列検索 */\n vector<ll> search(string &text)\n {\n vector<ll> res;\n int i, j;\n int tlen = text.size();\n \n i = j = 0;\n while (j < tlen)\n {\n while (i > -1 && p[i] != text[j])\n i = kmp[i];\n \n i++;\n j++;\n if (i >= plen)\n {\n res.push_back(j - i);\n i = kmp[i];\n }\n }\n return res;\n }\n};\n\n\nint main()\n{\n cin.tie(0);\n ios::sync_with_stdio(false);\n cout << fixed << setprecision(10);\n \n int n;\n while(cin >> n, n){\n vector<string> a(n), b(n);\n REP(i,n){\n cin >> a[i] >> b[i];\n }\n\n string s, g; cin >> s >> g;\n\n map<string, int> dist;\n dist[s] = 0;\n priority_queue<pair<int, string>, vector<pair<int, string>>, greater<pair<int, string>>> q;\n q.push({0, s});\n while(q.size()){\n auto p = q.top();\n q.pop();\n \n int v = p.first;\n string w = p.second;\n\n if(w.size() > 10) continue;\n \n for(int i=0; i<n; i++){\n \n string nx = \"\";\n KMP kmp(a[i]);\n \n auto ret = kmp.search(w);\n vector<bool> used(w.size());\n\n if(ret.size() == 0) continue;\n REP(i,ret.size()) used[ret[i]] = true;\n int idx = 0;\n for(int j=0; j<w.size(); j++){\n if(used[j]){\n nx += b[i];\n j += (int)a[i].size()-1;\n idx++;\n }else{\n nx += w[j];\n }\n }\n //cout << nx << endl;\n if(w != nx){\n if(dist.find(nx) == dist.end()){\n dist[nx] = dist[w] + 1;\n q.push({dist[nx], nx});\n }\n }\n }\n }\n if(dist.find(g) == dist.end()) cout << -1 << endl;\n else cout << dist[g] << endl;\n\n }\n\n return 0;\n}", "accuracy": 1, "time_ms": 60, "memory_kb": 4144, "score_of_the_acc": -1.8221, "final_rank": 20 }, { "submission_id": "aoj_1296_4917187", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n#define rep(i, n) for (int i = 0; i < (n); i++)\n\nstring sed(string s, string before, string after)\n{\n string ret = \"\";\n int i = 0;\n while (i < s.size()) {\n if (i + before.size() > s.size()) {\n ret += s[i];\n i++;\n } else if (s.substr(i, before.size()) == before) {\n ret += after;\n i += before.size();\n } else {\n ret += s[i];\n i++;\n }\n }\n return ret;\n}\n\nvoid solve(int n)\n{\n vector<string> before(n), after(n);\n rep(i, n) cin >> before[i] >> after[i];\n string s, t;\n cin >> s >> t;\n deque<string> que;\n que.push_back(s);\n int ans = 0;\n while (!que.empty()) {\n deque<string> next;\n while (!que.empty()) {\n string cur = que.front();\n que.pop_front();\n if (cur == t) {\n cout << ans << endl;\n return;\n }\n if (cur.size() > t.size())\n continue;\n rep(i, n)\n {\n string ncur = sed(cur, before[i], after[i]);\n if (cur != ncur) {\n next.push_back(ncur);\n }\n }\n }\n que = next;\n ans++;\n }\n cout << -1 << endl;\n return;\n}\n\nint main()\n{\n int n;\n while (cin >> n, n) {\n solve(n);\n }\n return 0;\n}", "accuracy": 1, "time_ms": 50, "memory_kb": 4324, "score_of_the_acc": -1.8, "final_rank": 19 }, { "submission_id": "aoj_1296_4851755", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\nvoid solve(int n){\n vector<string> a(n),b(n);\n for (int i=0;i<n;++i) cin >> a[i] >> b[i];\n string s,t; cin >> s >> t;\n\n auto substitute=[&](string S,int x){\n string res=\"\";\n int l=a[x].size();\n for (int i=0;i<S.size();++i){\n if (i+l<=S.size()&&S.substr(i,l)==a[x]){\n res+=b[x]; i+=l-1;\n } else res+=S[i];\n }\n return res;\n };\n unordered_map<string,int> dp;\n priority_queue<pair<int,string>> pq;\n dp[s]=0; pq.emplace(-dp[s],s);\n while(!pq.empty()){\n auto p=pq.top(); pq.pop();\n string S=p.second;\n if (dp[S]<-p.first) continue;\n if (S.size()>=t.size()) continue;\n for (int i=0;i<n;++i){\n string nxt=substitute(S,i);\n if (!dp.count(nxt)||dp[S]+1<dp[nxt]){\n dp[nxt]=dp[S]+1;\n pq.emplace(-dp[nxt],nxt);\n }\n }\n }\n\n cout << (dp.count(t)?dp[t]:-1) << '\\n';\n}\n\nint main(){\n cin.tie(0);\n ios::sync_with_stdio(false);\n int n;\n while(cin >> n,n){\n solve(n);\n }\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3560, "score_of_the_acc": -0.2451, "final_rank": 7 } ]
aoj_1297_cpp
Problem C: Swimming Jam Despite urging requests of the townspeople, the municipal office cannot afford to improve many of the apparently deficient city amenities under this recession. The city swimming pool is one of the typical examples. It has only two swimming lanes. The Municipal Fitness Agency, under this circumstances, settled usage rules so that the limited facilities can be utilized fully. Two lanes are to be used for one-way swimming of different directions. Swimmers are requested to start swimming in one of the lanes, from its one end to the other, and then change the lane to swim his/her way back. When he or she reaches the original starting end, he/she should return to his/her initial lane and starts swimming again. Each swimmer has his/her own natural constant pace. Swimmers, however, are not permitted to pass other swimmers except at the ends of the pool; as the lanes are not wide enough, that might cause accidents. If a swimmer is blocked by a slower swimmer, he/she has to follow the slower swimmer at the slower pace until the end of the lane is reached. Note that the blocking swimmer’s natural pace may be faster than the blocked swimmer; the blocking swimmer might also be blocked by another swimmer ahead, whose natural pace is slower than the blocked swimmer. Blocking would have taken place whether or not a faster swimmer was between them. Swimmers can change their order if they reach the end of the lane simultaneously. They change their order so that ones with faster natural pace swim in front. When a group of two or more swimmers formed by a congestion reaches the end of the lane, they are considered to reach there simultaneously, and thus change their order there. The number of swimmers, their natural paces in times to swim from one end to the other, and the numbers of laps they plan to swim are given. Note that here one "lap" means swimming from one end to the other and then swimming back to the original end. Your task is to calculate the time required for all the swimmers to finish their plans. All the swimmers start from the same end of the pool at the same time, the faster swimmers in front. In solving this problem, you can ignore the sizes of swimmers' bodies, and also ignore the time required to change the lanes and the order in a group of swimmers at an end of the lanes. Input The input is a sequence of datasets. Each dataset is formatted as follows. n t 1 c 1 ... t n c n n is an integer (1 ≤ n ≤ 50) that represents the number of swimmers. t i and c i are integers (1 ≤ t i ≤ 300, 1 ≤ c i ≤ 250) that represent the natural pace in times to swim from one end to the other and the number of planned laps for the i -th swimmer, respectively. ti and ci are separated by a space. The end of the input is indicated by a line containing one zero. Output For each dataset, output the time required for all the swimmers to finish their plans in a line. No extra characters should occur in the output. Sample Input 2 10 30 15 20 2 10 240 15 ...(truncated)
[ { "submission_id": "aoj_1297_10853354", "code_snippet": "#include <cstdio>\n#include <cstring>\n#include <iostream>\n#include <algorithm>\n\nusing namespace std;\n\nint n, tot, tot2, ans, lamp [100], lamp2 [100], Time [100], t [100];\n\nstruct data\n{\n\tint Time, lamp;\n} a [100];\n\nbool cmpp (int x, int y)\n{return a[x].Time < a[y].Time;}\nbool cmp (data a, data b)\n{return a.Time < b.Time;}\n\nvoid work ()\n{\n\tint tx = Time[lamp[0]];\n\tans += tx;\n\tfor (int i = 0; i < tot; ++i)\n\t\tTime[lamp[i]] -= tx;\n\tfor (int i = 0; i < tot2; ++i)\n\t\tTime[lamp2[i]] -= tx;\n\tint tt = 0;\n\tfor (int i = 0; i < tot; ++i)\n\t\tif (Time[lamp[i]] <= 0)\n\t\t{\n\t\t\tt[tt++] = lamp[i];\n\t\t\tTime[lamp[i]] = a[lamp[i]].Time;\n\t\t}\n\t\telse break;\n\tsort (t, t + tt, cmpp);\n\tfor (int i = 0; i < tt; ++i)\n\t\tlamp2[tot2++] = t[i];\n\tfor (int i = 0; i < tot - tt; ++i)\n\t\tlamp[i] = lamp[i + tt];\n\ttot -= tt;\n}\n\nvoid work2 ()\n{\n\tint tx = Time[lamp2[0]];\n\tans += tx;\n\tfor (int i = 0; i < tot; ++i)\n\t\tTime[lamp[i]] -= tx;\n\tfor (int i = 0; i < tot2; ++i)\n\t\tTime[lamp2[i]] -= tx;\n\tint tt = 0;\n\tfor (int i = 0; i < tot2; ++i)\n\t\tif (Time[lamp2[i]] <= 0)\n\t\t{\n\t\t\tt[tt++] = lamp2[i];\n\t\t\tTime[lamp2[i]] = a[lamp2[i]].Time;\n\t\t\t--a[lamp2[i]].lamp;\n\t\t}\n\t\telse break;\n\tsort (t, t + tt, cmpp);\n\tfor (int i = 0; i < tt; ++i)\n\t\tif (a[t[i]].lamp) lamp[tot++] = t[i];\n\tfor (int i = 0; i < tot2 - tt; ++i)\n\t\tlamp2[i] = lamp2[i + tt];\n\ttot2 -= tt;\n}\n\nint main ()\n{\n\twhile (1)\n\t{\n\t\tscanf (\"%d\", &n);\n\t\tif (n == 0) break;\n\t\tfor (int i = 0; i < n; ++i) scanf (\"%d%d\", &a[i].Time, &a[i].lamp);\n\t\tsort (a, a + n, cmp);\n\t\tmemset (Time, 0, sizeof (Time));\n\t\ttot = n; tot2 = 0; ans = 0;\n\t\tfor (int i = 0; i < n; ++i)\n\t\t{\n\t\t\tlamp[i] = i;\n\t\t\tTime[i] = a[i].Time;\n\t\t}\n\t\twhile (1)\n\t\t{\n\t\t\tif (tot == 0) work2 ();\n\t\t\telse if (tot2 == 0) work ();\n\t\t\telse if (Time[lamp[0]] < Time[lamp2[0]]) work ();\n\t\t\telse work2 ();\n\t\t\tbool flag = 0;\n\t\t\tfor (int i = 0; i < n; ++i)\n\t\t\t\tif (a[i].lamp) flag = 1;\n\t\t\tif (!flag) break;\n\t\t}\n\t\tprintf (\"%d\\n\", ans);\n\t}\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3264, "score_of_the_acc": -0.0339, "final_rank": 2 }, { "submission_id": "aoj_1297_9778195", "code_snippet": "#line 2 \"cp-library/src/cp-template.hpp\"\n#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\nusing ld = long double;\nusing uint = unsigned int;\nusing ull = unsigned long long;\nusing i32 = int;\nusing u32 = unsigned int;\nusing i64 = long long;\nusing u64 = unsigned long long;\nusing i128 = __int128_t;\ntemplate < class T > bool chmin(T& a, T b) { if(a > b) { a = b; return true; } return false; }\ntemplate < class T > bool chmax(T& a, T b) { if(a < b) { a = b; return true; } return false; }\ntemplate < class T, class U > T ceil (T x, U y) { return (x > 0 ? (x + y - 1) / y : x / y); }\ntemplate < class T, class U > T floor(T x, U y) { return (x > 0 ? x / y : (x - y + 1) / y); }\nint popcnt(i32 x) { return __builtin_popcount(x); }\nint popcnt(u32 x) { return __builtin_popcount(x); }\nint popcnt(i64 x) { return __builtin_popcountll(x); }\nint popcnt(u64 x) { return __builtin_popcountll(x); }\n\n#line 2 \"cp-library/src/utility/rep_itr.hpp\"\ntemplate < class T > struct itr_rep {\n T i, d;\n constexpr itr_rep(const T i) noexcept : i(i), d(1) {}\n constexpr itr_rep(const T i, const T d) noexcept : i(i), d(d) {}\n void operator++() noexcept { i += d; }\n constexpr int operator*() const noexcept { return i; }\n constexpr bool operator!=(const itr_rep x) const noexcept { return d > 0 ? i < x.i : i > x.i; }\n};\n\ntemplate < class T > struct rep {\n const itr_rep< T > s, t;\n constexpr rep(const T t) noexcept : s(0), t(t) {}\n constexpr rep(const T s, const T t) noexcept : s(s), t(t) {}\n constexpr rep(const T s, const T t, const T d) noexcept : s(s, d), t(t, d) {}\n constexpr auto begin() const noexcept { return s; }\n constexpr auto end () const noexcept { return t; }\n};\n\ntemplate < class T > struct revrep {\n const itr_rep < T > s, t;\n constexpr revrep(const T t) noexcept : s(t - 1, -1), t(-1, -1) {}\n constexpr revrep(const T s, const T t) noexcept : s(t - 1, -1), t(s - 1, -1) {}\n constexpr revrep(const T s, const T t, const T d) noexcept : s(t - 1, -d), t(s - 1, -d) {}\n constexpr auto begin() const noexcept { return s; }\n constexpr auto end () const noexcept { return t; }\n};\n#line 3 \"cp-library/src/utility/io.hpp\"\n\n/* 128bit integer */\nistream& operator>>(istream& is, i128& x) {\n std::string s; is >> s;\n int pm = (s[0] == '-');\n x = 0;\n for(int i : rep(pm, int(s.size()))) x = x * 10 + (s[i] - '0');\n if(pm) x *= -1;\n return is;\n}\nostream& operator<<(ostream& os, const i128& x) {\n if(x == 0) return os << '0';\n i128 y = x;\n if(y < 0) { os << '-'; y *= -1; }\n std::vector<int> ny;\n while(y > 0) { ny.push_back(y % 10); y /= 10; }\n for(int i : revrep(ny.size())) os << ny[i];\n return os;\n}\n\ntemplate < class S, class T > istream& operator>>(istream& is, std::pair< S, T >& x) { is >> x.first >> x.second; return is; }\ntemplate < class S, class T > ostream& operator<<(ostream& os, const std::pair< S, T >& x) { os << x.first << \" \" << x.second; return os; }\n\nnamespace scanner {\n struct sca {\n template < class T > operator T() {\n T s; std::cin >> s; return s;\n }\n };\n struct vec {\n int n;\n vec(int n) : n(n) {}\n template < class T > operator std::vector< T >() {\n std::vector< T > v(n);\n for(T& x : v) std::cin >> x;\n return v;\n }\n };\n struct mat {\n int h, w;\n mat(int h, int w) : h(h), w(w) {}\n template < class T > operator std::vector< std::vector< T > >() {\n std::vector m(h, std::vector< T >(w));\n for(std::vector< T >& v : m) for(T& x : v) std::cin >> x;\n return m;\n }\n };\n struct speedup {\n speedup() {\n std::cin.tie(0);\n std::ios::sync_with_stdio(0);\n }\n } speedup_instance;\n}\nscanner::sca in() { return scanner::sca(); }\nscanner::vec in(int n) { return scanner::vec(n); }\nscanner::mat in(int h, int w) { return scanner::mat(h, w); }\n\nnamespace printer {\n void precision(int d) { std::cout << std::fixed << std::setprecision(d); }\n void flush() { std::cout.flush(); }\n}\n\ntemplate < class T >\nostream& operator<<(ostream& os, const std::vector< T > a) {\n int n = a.size();\n for(int i : rep(n)) { os << a[i]; if(i != n - 1) os << ' '; }\n return os;\n}\n\nint print() { std::cout << '\\n'; return 0; }\ntemplate < class head, class... tail > int print(head&& h, tail&&... t) {\n std::cout << h; if(sizeof...(tail)) std::cout << ' ';\n return print(std::forward<tail>(t)...);\n}\ntemplate < class T > int print_n(const std::vector< T > a) {\n int n = a.size();\n for(int i : rep(n)) std::cout << a[i] << \"\\n\";\n return 0;\n}\n\n\n#line 2 \"cp-library/src/utility/key_val.hpp\"\n\ntemplate < class K, class V >\nstruct key_val {\n K key; V val;\n key_val() {}\n key_val(K key, V val) : key(key), val(val) {}\n template < std::size_t Index >\n std::tuple_element_t< Index, key_val >& get() {\n if constexpr (Index == 0) return key;\n if constexpr (Index == 1) return val;\n }\n};\n\nnamespace std {\n\ntemplate < class K, class V > struct tuple_size < key_val< K, V > > : integral_constant< size_t, 2 > {};\ntemplate < class K, class V > struct tuple_element < 0, key_val< K, V > > { using type = K; };\ntemplate < class K, class V > struct tuple_element < 1, key_val< K, V > > { using type = V; };\n\n}\n#line 2 \"cp-library/src/utility/vec_op.hpp\"\ntemplate < class T > key_val< int, T > max_of(const vector< T >& a) {\n int i = std::max_element(a.begin(), a.end()) - a.begin();\n return {i, a[i]};\n}\ntemplate < class T > key_val< int, T > min_of(const vector< T >& a) {\n int i = std::min_element(a.begin(), a.end()) - a.begin();\n return {i, a[i]};\n}\ntemplate < class S, class T > S sum_of(const vector< T >& a) {\n S sum = 0;\n for(const T x : a) sum += x;\n return sum;\n}\ntemplate < class S, class T > vector< S > freq_of(const vector< T >& a, T L, T R) {\n vector< S > res(R - L, S(0));\n for(const T x : a) res[x - L] += 1;\n return res;\n}\ntemplate < class S, class T > struct prefix_sum {\n vector< S > s;\n prefix_sum(const vector< T >& a) : s(a) {\n s.insert(s.begin(), S(0));\n for(int i : rep(a.size())) s[i + 1] += s[i];\n }\n // [L, R)\n S sum(int L, int R) { return s[R] - s[L]; }\n};\n#line 3 \"cp-library/src/utility/heap.hpp\"\n\ntemplate < class T > using heap_min = std::priority_queue< T, std::vector< T >, std::greater< T > >;\ntemplate < class T > using heap_max = std::priority_queue< T, std::vector< T >, std::less< T > >;\n\n#line 27 \"cp-library/src/cp-template.hpp\"\n\n#line 1 \"cp-library/src/algorithm/bin_search.hpp\"\ntemplate < class T, class F >\nT bin_search(T ok, T ng, F f) {\n while(abs(ng - ok) > 1) {\n T mid = (ok + ng) / 2;\n (f(mid) ? ok : ng) = mid;\n }\n return ok;\n}\n\ntemplate < class T, class F >\nT bin_search_real(T ok, T ng, F f, int step = 80) {\n while(step--) {\n T mid = (ok + ng) / 2;\n (f(mid) ? ok : ng) = mid;\n }\n return ok;\n}\n#line 2 \"cp-library/src/algorithm/argsort.hpp\"\n\ntemplate < class T > std::vector< int > argsort(const std::vector< T > &a) {\n std::vector< int > ids((int)a.size());\n std::iota(ids.begin(), ids.end(), 0);\n std::sort(ids.begin(), ids.end(), [&](int i, int j) {\n return a[i] < a[j] || (a[i] == a[j] && i < j);\n });\n return ids;\n}\n#line 1 \"macro.hpp\"\nnamespace macro {\n\nusing size_type = int;\ntemplate < class container > void sort(container& a) { std::sort(std:: begin(a), std:: end(a)); }\ntemplate < class container > void rsort(container& a) { std::sort(std::rbegin(a), std::rend(a)); }\ntemplate < class container > void reverse(container& a) { std::reverse(std::begin(a), std::end(a)); }\ntemplate < class container > void unique(container& a) {\n std::sort(std::begin(a), std::end(a));\n a.erase(std::unique(std::begin(a), std::end(a)), std::end(a));\n}\ntemplate < class container > container sorted(const container& a) { container b = a; sort(b); return std::move(b); }\ntemplate < class container > container rsorted(const container& a) { container b = a; rsort(b); return std::move(b); }\ntemplate < class container, class compare > void sort(container& a, const compare& cmp) { std::sort(std::begin(a), std::end(a), cmp); }\ntemplate < class container, class compare > container sorted(const container& a, const compare& cmp) { container b = a; sort(b, cmp); return std::move(b); }\ntemplate < class container, class value > size_type lower_bound(const container& a, const value& x) { return std::lower_bound(std::begin(a), std::end(a), x) - std::begin(a); }\ntemplate < class container, class value > size_type upper_bound(const container& a, const value& x) { return std::upper_bound(std::begin(a), std::end(a), x) - std::begin(a); }\n\nconst std::vector<std::pair<size_type, size_type>> dir4 = { {+1, 0}, {-1, 0}, { 0, +1}, { 0, -1} };\nconst std::vector<std::pair<size_type, size_type>> dir8 = { {-1, -1}, {-1, 0}, {-1, +1}, { 0, -1}, { 0, +1}, {+1, -1}, {+1, 0}, {+1, +1} };\n\n#ifdef _DEBUG\n#define debug(x) std::cout << \"[\" << __LINE__ << \"] \" << #x << \": \" << x << std::endl\n#else\n#define debug(x)\n#endif\n\ntemplate < class container > void concat(container& a, const container& b) {\n a.insert(std::end(a), std::begin(b), std::end(b));\n}\nstd::vector<size_type> iota(const size_type n) {\n std::vector<size_type> I(n);\n std::iota(std::begin(I), std::end(I), 0);\n return I;\n}\ntemplate < class container > std::vector<size_type> sort_idx(const container& a) {\n const size_type n = a.size();\n std::vector<size_type> I = iota(n);\n std::sort(std::begin(I), std::end(I), [&](size_type i, size_type j) { return a[i] < a[j] or (a[i] == a[j] and i < j); });\n return I;\n}\ntemplate < class container, class compare > std::vector<size_type> sort_idx(const container& a, const compare& cmp) {\n const size_type n = a.size();\n std::vector<size_type> I = iota(n);\n std::sort(std::begin(I), std::end(I), [&](size_type i, size_type j) { return cmp(a[i], a[j]) or (a[i] == a[j] and i < j); });\n return std::move(I);\n}\n\nstruct grid {\n using size_type = int;\n size_type H, W;\n grid(const size_type H, const size_type W) : H(H), W(W) {}\n bool contains(const size_type i, const size_type j) {\n return 0 <= i and i < H and 0 <= j and j < W;\n }\n};\n\nusing f64 = long double;\n\ntemplate < class T > vector< T >& operator++(vector< T >& a) { for(T& x : a) x++; return a; }\ntemplate < class T > vector< T >& operator--(vector< T >& a) { for(T& x : a) x--; return a; }\ntemplate < class T > vector< T > operator++(vector< T >& a, signed) { vector< T > res = a; for(T& x : a) x++; return res; }\ntemplate < class T > vector< T > operator--(vector< T >& a, signed) { vector< T > res = a; for(T& x : a) x--; return res; }\n\n} // namespace macro\n\nusing namespace macro;\n#line 3 \"A.cpp\"\n\nint solve(int n) {\n using S = tuple<int, int, int, int>;\n priority_queue< S, vector< S >, greater< S > > pq;\n int m[2] = {0, 0};\n for(int _ : rep(n)) {\n int t = in(), c = in();\n pq.push({t, t, c, 0});\n chmax(m[0], t);\n }\n while(not pq.empty()) {\n auto [now, t, c, d] = pq.top(); pq.pop();\n if(d == 1) c--;\n if(c == 0) continue;\n const int nd = d ^ 1;\n const int nxt = max(now + t, m[nd]);\n chmax(m[nd], nxt);\n pq.push({nxt, t, c, nd});\n }\n return m[1];\n}\n\nint main() {\n while(true) {\n int n = in();\n if(n == 0) return 0;\n print(solve(n));\n }\n}", "accuracy": 1, "time_ms": 20, "memory_kb": 3400, "score_of_the_acc": -0.096, "final_rank": 12 }, { "submission_id": "aoj_1297_9778174", "code_snippet": "#line 2 \"cp-library/src/cp-template.hpp\"\n#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\nusing ld = long double;\nusing uint = unsigned int;\nusing ull = unsigned long long;\nusing i32 = int;\nusing u32 = unsigned int;\nusing i64 = long long;\nusing u64 = unsigned long long;\nusing i128 = __int128_t;\ntemplate < class T > bool chmin(T& a, T b) { if(a > b) { a = b; return true; } return false; }\ntemplate < class T > bool chmax(T& a, T b) { if(a < b) { a = b; return true; } return false; }\ntemplate < class T, class U > T ceil (T x, U y) { return (x > 0 ? (x + y - 1) / y : x / y); }\ntemplate < class T, class U > T floor(T x, U y) { return (x > 0 ? x / y : (x - y + 1) / y); }\nint popcnt(i32 x) { return __builtin_popcount(x); }\nint popcnt(u32 x) { return __builtin_popcount(x); }\nint popcnt(i64 x) { return __builtin_popcountll(x); }\nint popcnt(u64 x) { return __builtin_popcountll(x); }\n\n#line 2 \"cp-library/src/utility/rep_itr.hpp\"\ntemplate < class T > struct itr_rep {\n T i, d;\n constexpr itr_rep(const T i) noexcept : i(i), d(1) {}\n constexpr itr_rep(const T i, const T d) noexcept : i(i), d(d) {}\n void operator++() noexcept { i += d; }\n constexpr int operator*() const noexcept { return i; }\n constexpr bool operator!=(const itr_rep x) const noexcept { return d > 0 ? i < x.i : i > x.i; }\n};\n\ntemplate < class T > struct rep {\n const itr_rep< T > s, t;\n constexpr rep(const T t) noexcept : s(0), t(t) {}\n constexpr rep(const T s, const T t) noexcept : s(s), t(t) {}\n constexpr rep(const T s, const T t, const T d) noexcept : s(s, d), t(t, d) {}\n constexpr auto begin() const noexcept { return s; }\n constexpr auto end () const noexcept { return t; }\n};\n\ntemplate < class T > struct revrep {\n const itr_rep < T > s, t;\n constexpr revrep(const T t) noexcept : s(t - 1, -1), t(-1, -1) {}\n constexpr revrep(const T s, const T t) noexcept : s(t - 1, -1), t(s - 1, -1) {}\n constexpr revrep(const T s, const T t, const T d) noexcept : s(t - 1, -d), t(s - 1, -d) {}\n constexpr auto begin() const noexcept { return s; }\n constexpr auto end () const noexcept { return t; }\n};\n#line 3 \"cp-library/src/utility/io.hpp\"\n\n/* 128bit integer */\nistream& operator>>(istream& is, i128& x) {\n std::string s; is >> s;\n int pm = (s[0] == '-');\n x = 0;\n for(int i : rep(pm, int(s.size()))) x = x * 10 + (s[i] - '0');\n if(pm) x *= -1;\n return is;\n}\nostream& operator<<(ostream& os, const i128& x) {\n if(x == 0) return os << '0';\n i128 y = x;\n if(y < 0) { os << '-'; y *= -1; }\n std::vector<int> ny;\n while(y > 0) { ny.push_back(y % 10); y /= 10; }\n for(int i : revrep(ny.size())) os << ny[i];\n return os;\n}\n\ntemplate < class S, class T > istream& operator>>(istream& is, std::pair< S, T >& x) { is >> x.first >> x.second; return is; }\ntemplate < class S, class T > ostream& operator<<(ostream& os, const std::pair< S, T >& x) { os << x.first << \" \" << x.second; return os; }\n\nnamespace scanner {\n struct sca {\n template < class T > operator T() {\n T s; std::cin >> s; return s;\n }\n };\n struct vec {\n int n;\n vec(int n) : n(n) {}\n template < class T > operator std::vector< T >() {\n std::vector< T > v(n);\n for(T& x : v) std::cin >> x;\n return v;\n }\n };\n struct mat {\n int h, w;\n mat(int h, int w) : h(h), w(w) {}\n template < class T > operator std::vector< std::vector< T > >() {\n std::vector m(h, std::vector< T >(w));\n for(std::vector< T >& v : m) for(T& x : v) std::cin >> x;\n return m;\n }\n };\n struct speedup {\n speedup() {\n std::cin.tie(0);\n std::ios::sync_with_stdio(0);\n }\n } speedup_instance;\n}\nscanner::sca in() { return scanner::sca(); }\nscanner::vec in(int n) { return scanner::vec(n); }\nscanner::mat in(int h, int w) { return scanner::mat(h, w); }\n\nnamespace printer {\n void precision(int d) { std::cout << std::fixed << std::setprecision(d); }\n void flush() { std::cout.flush(); }\n}\n\ntemplate < class T >\nostream& operator<<(ostream& os, const std::vector< T > a) {\n int n = a.size();\n for(int i : rep(n)) { os << a[i]; if(i != n - 1) os << ' '; }\n return os;\n}\n\nint print() { std::cout << '\\n'; return 0; }\ntemplate < class head, class... tail > int print(head&& h, tail&&... t) {\n std::cout << h; if(sizeof...(tail)) std::cout << ' ';\n return print(std::forward<tail>(t)...);\n}\ntemplate < class T > int print_n(const std::vector< T > a) {\n int n = a.size();\n for(int i : rep(n)) std::cout << a[i] << \"\\n\";\n return 0;\n}\n\n\n#line 2 \"cp-library/src/utility/key_val.hpp\"\n\ntemplate < class K, class V >\nstruct key_val {\n K key; V val;\n key_val() {}\n key_val(K key, V val) : key(key), val(val) {}\n template < std::size_t Index >\n std::tuple_element_t< Index, key_val >& get() {\n if constexpr (Index == 0) return key;\n if constexpr (Index == 1) return val;\n }\n};\n\nnamespace std {\n\ntemplate < class K, class V > struct tuple_size < key_val< K, V > > : integral_constant< size_t, 2 > {};\ntemplate < class K, class V > struct tuple_element < 0, key_val< K, V > > { using type = K; };\ntemplate < class K, class V > struct tuple_element < 1, key_val< K, V > > { using type = V; };\n\n}\n#line 2 \"cp-library/src/utility/vec_op.hpp\"\ntemplate < class T > key_val< int, T > max_of(const vector< T >& a) {\n int i = std::max_element(a.begin(), a.end()) - a.begin();\n return {i, a[i]};\n}\ntemplate < class T > key_val< int, T > min_of(const vector< T >& a) {\n int i = std::min_element(a.begin(), a.end()) - a.begin();\n return {i, a[i]};\n}\ntemplate < class S, class T > S sum_of(const vector< T >& a) {\n S sum = 0;\n for(const T x : a) sum += x;\n return sum;\n}\ntemplate < class S, class T > vector< S > freq_of(const vector< T >& a, T L, T R) {\n vector< S > res(R - L, S(0));\n for(const T x : a) res[x - L] += 1;\n return res;\n}\ntemplate < class S, class T > struct prefix_sum {\n vector< S > s;\n prefix_sum(const vector< T >& a) : s(a) {\n s.insert(s.begin(), S(0));\n for(int i : rep(a.size())) s[i + 1] += s[i];\n }\n // [L, R)\n S sum(int L, int R) { return s[R] - s[L]; }\n};\n#line 3 \"cp-library/src/utility/heap.hpp\"\n\ntemplate < class T > using heap_min = std::priority_queue< T, std::vector< T >, std::greater< T > >;\ntemplate < class T > using heap_max = std::priority_queue< T, std::vector< T >, std::less< T > >;\n\n#line 27 \"cp-library/src/cp-template.hpp\"\n\n#line 1 \"cp-library/src/algorithm/bin_search.hpp\"\ntemplate < class T, class F >\nT bin_search(T ok, T ng, F f) {\n while(abs(ng - ok) > 1) {\n T mid = (ok + ng) / 2;\n (f(mid) ? ok : ng) = mid;\n }\n return ok;\n}\n\ntemplate < class T, class F >\nT bin_search_real(T ok, T ng, F f, int step = 80) {\n while(step--) {\n T mid = (ok + ng) / 2;\n (f(mid) ? ok : ng) = mid;\n }\n return ok;\n}\n#line 2 \"cp-library/src/algorithm/argsort.hpp\"\n\ntemplate < class T > std::vector< int > argsort(const std::vector< T > &a) {\n std::vector< int > ids((int)a.size());\n std::iota(ids.begin(), ids.end(), 0);\n std::sort(ids.begin(), ids.end(), [&](int i, int j) {\n return a[i] < a[j] || (a[i] == a[j] && i < j);\n });\n return ids;\n}\n#line 1 \"macro.hpp\"\nnamespace macro {\n\nusing size_type = int;\ntemplate < class container > void sort(container& a) { std::sort(std:: begin(a), std:: end(a)); }\ntemplate < class container > void rsort(container& a) { std::sort(std::rbegin(a), std::rend(a)); }\ntemplate < class container > void reverse(container& a) { std::reverse(std::begin(a), std::end(a)); }\ntemplate < class container > void unique(container& a) {\n std::sort(std::begin(a), std::end(a));\n a.erase(std::unique(std::begin(a), std::end(a)), std::end(a));\n}\ntemplate < class container > container sorted(const container& a) { container b = a; sort(b); return std::move(b); }\ntemplate < class container > container rsorted(const container& a) { container b = a; rsort(b); return std::move(b); }\ntemplate < class container, class compare > void sort(container& a, const compare& cmp) { std::sort(std::begin(a), std::end(a), cmp); }\ntemplate < class container, class compare > container sorted(const container& a, const compare& cmp) { container b = a; sort(b, cmp); return std::move(b); }\ntemplate < class container, class value > size_type lower_bound(const container& a, const value& x) { return std::lower_bound(std::begin(a), std::end(a), x) - std::begin(a); }\ntemplate < class container, class value > size_type upper_bound(const container& a, const value& x) { return std::upper_bound(std::begin(a), std::end(a), x) - std::begin(a); }\n\nconst std::vector<std::pair<size_type, size_type>> dir4 = { {+1, 0}, {-1, 0}, { 0, +1}, { 0, -1} };\nconst std::vector<std::pair<size_type, size_type>> dir8 = { {-1, -1}, {-1, 0}, {-1, +1}, { 0, -1}, { 0, +1}, {+1, -1}, {+1, 0}, {+1, +1} };\n\n#ifdef _DEBUG\n#define debug(x) std::cout << \"[\" << __LINE__ << \"] \" << #x << \": \" << x << std::endl\n#else\n#define debug(x)\n#endif\n\ntemplate < class container > void concat(container& a, const container& b) {\n a.insert(std::end(a), std::begin(b), std::end(b));\n}\nstd::vector<size_type> iota(const size_type n) {\n std::vector<size_type> I(n);\n std::iota(std::begin(I), std::end(I), 0);\n return I;\n}\ntemplate < class container > std::vector<size_type> sort_idx(const container& a) {\n const size_type n = a.size();\n std::vector<size_type> I = iota(n);\n std::sort(std::begin(I), std::end(I), [&](size_type i, size_type j) { return a[i] < a[j] or (a[i] == a[j] and i < j); });\n return I;\n}\ntemplate < class container, class compare > std::vector<size_type> sort_idx(const container& a, const compare& cmp) {\n const size_type n = a.size();\n std::vector<size_type> I = iota(n);\n std::sort(std::begin(I), std::end(I), [&](size_type i, size_type j) { return cmp(a[i], a[j]) or (a[i] == a[j] and i < j); });\n return std::move(I);\n}\n\nstruct grid {\n using size_type = int;\n size_type H, W;\n grid(const size_type H, const size_type W) : H(H), W(W) {}\n bool contains(const size_type i, const size_type j) {\n return 0 <= i and i < H and 0 <= j and j < W;\n }\n};\n\nusing f64 = long double;\n\ntemplate < class T > vector< T >& operator++(vector< T >& a) { for(T& x : a) x++; return a; }\ntemplate < class T > vector< T >& operator--(vector< T >& a) { for(T& x : a) x--; return a; }\ntemplate < class T > vector< T > operator++(vector< T >& a, signed) { vector< T > res = a; for(T& x : a) x++; return res; }\ntemplate < class T > vector< T > operator--(vector< T >& a, signed) { vector< T > res = a; for(T& x : a) x--; return res; }\n\n} // namespace macro\n\nusing namespace macro;\n#line 3 \"A.cpp\"\n\nconst int T = 300 * 250 * 2;\n\nint solve(int n) {\n using S = tuple<int, int, int, int>;\n priority_queue< S, vector< S >, greater< S > > pq;\n int m[2] = {0, 0};\n for(int _ : rep(n)) {\n int t = in(), c = in();\n pq.push({t, t, c, 0});\n chmax(m[0], t);\n }\n while(not pq.empty()) {\n auto [now, t, c, d] = pq.top(); pq.pop();\n if(d == 1) c--;\n if(c == 0) continue;\n const int nd = d ^ 1;\n const int nxt = max(now + t, m[nd]);\n chmax(m[nd], nxt);\n pq.push({nxt, t, c, nd});\n }\n return m[1];\n}\n\nint main() {\n while(true) {\n int n = in();\n if(n == 0) return 0;\n print(solve(n));\n }\n}", "accuracy": 1, "time_ms": 20, "memory_kb": 3372, "score_of_the_acc": -0.0835, "final_rank": 7 }, { "submission_id": "aoj_1297_9711542", "code_snippet": "#include <bits/stdc++.h>\n\nusing namespace std;\n\n#define rep(i, a, b) for (int i = (int)(a); i < (int)(b); i++)\n#define rrep(i, a, b) for (int i = (int)(b)-1; i >= (int)(a); i--)\n#define ALL(v) (v).begin(), (v).end()\n#define UNIQUE(v) sort(ALL(v)), (v).erase(unique(ALL(v)), (v).end())\n#define SZ(v) (int)v.size()\n#define MIN(v) *min_element(ALL(v))\n#define MAX(v) *max_element(ALL(v))\n#define LB(v, x) int(lower_bound(ALL(v), (x)) - (v).begin())\n#define UB(v, x) int(upper_bound(ALL(v), (x)) - (v).begin())\n\nusing uint = unsigned int;\nusing ll = long long int;\nusing ull = unsigned long long;\nusing i128 = __int128_t;\nusing u128 = __uint128_t;\nconst int inf = 0x3fffffff;\nconst ll INF = 0x1fffffffffffffff;\n\ntemplate <typename T> inline bool chmax(T &a, T b) {\n if (a < b) {\n a = b;\n return 1;\n }\n return 0;\n}\ntemplate <typename T> inline bool chmin(T &a, T b) {\n if (a > b) {\n a = b;\n return 1;\n }\n return 0;\n}\ntemplate <typename T, typename U> T ceil(T x, U y) {\n assert(y != 0);\n if (y < 0)\n x = -x, y = -y;\n return (x > 0 ? (x + y - 1) / y : x / y);\n}\ntemplate <typename T, typename U> T floor(T x, U y) {\n assert(y != 0);\n if (y < 0)\n x = -x, y = -y;\n return (x > 0 ? x / y : (x - y + 1) / y);\n}\ntemplate <typename T> int popcnt(T x) {\n return __builtin_popcountll(x);\n}\ntemplate <typename T> int topbit(T x) {\n return (x == 0 ? -1 : 63 - __builtin_clzll(x));\n}\ntemplate <typename T> int lowbit(T x) {\n return (x == 0 ? -1 : __builtin_ctzll(x));\n}\n\ntemplate <class T, class U>\nostream &operator<<(ostream &os, const pair<T, U> &p) {\n os << \"P(\" << p.first << \", \" << p.second << \")\";\n return os;\n}\ntemplate <typename T> ostream &operator<<(ostream &os, const vector<T> &vec) {\n os << \"{\";\n for (int i = 0; i < vec.size(); i++) {\n os << vec[i] << (i + 1 == vec.size() ? \"\" : \", \");\n }\n os << \"}\";\n return os;\n}\ntemplate <typename T, typename U>\nostream &operator<<(ostream &os, const map<T, U> &map_var) {\n os << \"{\";\n for (auto itr = map_var.begin(); itr != map_var.end(); itr++) {\n os << \"(\" << itr->first << \", \" << itr->second << \")\";\n itr++;\n if (itr != map_var.end())\n os << \", \";\n itr--;\n }\n os << \"}\";\n return os;\n}\ntemplate <typename T> ostream &operator<<(ostream &os, const set<T> &set_var) {\n os << \"{\";\n for (auto itr = set_var.begin(); itr != set_var.end(); itr++) {\n os << *itr;\n ++itr;\n if (itr != set_var.end())\n os << \", \";\n itr--;\n }\n os << \"}\";\n return os;\n}\n#ifdef LOCAL\n#define show(...) _show(0, #__VA_ARGS__, __VA_ARGS__)\n#else\n#define show(...) true\n#endif\ntemplate <typename T> void _show(int i, T name) {\n cerr << '\\n';\n}\ntemplate <typename T1, typename T2, typename... T3>\nvoid _show(int i, const T1 &a, const T2 &b, const T3 &...c) {\n for (; a[i] != ',' && a[i] != '\\0'; i++)\n cerr << a[i];\n cerr << \":\" << b << \" \";\n _show(i + 1, a, c...);\n}\n\n/**\n * @brief template\n */\n\nint main() {\nwhile(1) {\n int N;\n cin >> N;\n if (N == 0) return 0;\n vector<pair<int,int>> A(N);\n rep(i,0,N) cin >> A[i].first >> A[i].second, A[i].second *= 2;\n sort(ALL(A));\n reverse(ALL(A));\n vector<vector<int>> ANS(N);\n rep(i,0,N) {\n int Cur = 0;\n ANS[i].push_back(Cur);\n rep(j,0,A[i].second) {\n int Next = Cur + A[i].first;\n rep(k,0,i) {\n int l = UB(ANS[k],Cur)-1;\n if (ANS[k][l] == Cur) continue;\n if (l+1 == ANS[k].size()) continue;\n if (l % 2 != j % 2) continue;\n chmax(Next,ANS[k][l+1]);\n }\n Cur = Next;\n ANS[i].push_back(Cur);\n }\n }\n int MAX = 0;\n rep(i,0,N) chmax(MAX,ANS[i].back());\n cout << MAX << endl;\n}\n}", "accuracy": 1, "time_ms": 290, "memory_kb": 3436, "score_of_the_acc": -0.1488, "final_rank": 14 }, { "submission_id": "aoj_1297_8300961", "code_snippet": "#include <queue>\n#include <vector>\n#include <iostream>\n#include <algorithm>\nusing namespace std;\n\nint main() {\n\twhile (true) {\n\t\tint N;\n\t\tcin >> N;\n\t\tif (N == 0) {\n\t\t\tbreak;\n\t\t}\n\t\tvector<int> T(N), C(N);\n\t\tfor (int i = 0; i < N; i++) {\n\t\t\tcin >> T[i] >> C[i];\n\t\t}\n\t\tvector<int> perm(N);\n\t\tfor (int i = 0; i < N; i++) {\n\t\t\tperm[i] = i;\n\t\t}\n\t\tsort(perm.begin(), perm.end(), [&](int va, int vb) {\n\t\t\treturn T[va] < T[vb];\n\t\t});\n\t\tconst int INF = 1012345678;\n\t\tvector<priority_queue<vector<int>, vector<vector<int> >, greater<vector<int> > > > que(2);\n\t\tvector<int> last(2, -INF);\n\t\tfor (int i : perm) {\n\t\t\tque[0].push({T[i], T[i], i});\n\t\t\tlast[0] = max(last[0], T[i]);\n\t\t}\n\t\tint answer = 0;\n\t\twhile (true) {\n\t\t\tvector<int> t = {INF, INF, INF};\n\t\t\tint id = -1;\n\t\t\tfor (int i = 0; i < 2; i++) {\n\t\t\t\tif (!que[i].empty() && t > que[i].top()) {\n\t\t\t\t\tt = que[i].top();\n\t\t\t\t\tid = i;\n\t\t\t\t}\n\t\t\t}\n\t\t\tif (id == -1) {\n\t\t\t\tbreak;\n\t\t\t}\n\t\t\tanswer = t[0];\n\t\t\tque[id].pop();\n\t\t\tif (id == 1) {\n\t\t\t\tC[t[2]] -= 1;\n\t\t\t}\n\t\t\tif (C[t[2]] >= 1) {\n\t\t\t\tint pre = (!que[id ^ 1].empty() ? last[id ^ 1] : -INF);\n\t\t\t\tint nt = max(pre, t[0] + t[1]);\n\t\t\t\tque[id ^ 1].push({nt, t[1], t[2]});\n\t\t\t\tlast[id ^ 1] = nt;\n\t\t\t}\n\t\t}\n\t\tcout << answer << endl;\n\t}\n\treturn 0;\n}", "accuracy": 1, "time_ms": 50, "memory_kb": 3352, "score_of_the_acc": -0.0787, "final_rank": 5 }, { "submission_id": "aoj_1297_7195474", "code_snippet": "#include <iostream>\n#include <vector>\n#include <algorithm>\nusing namespace std;\n\nint N;\nint T[59], C[59];\nint Lap1[150009];\nint Lap2[150009];\n\nvoid solve() {\n\tfor (int i = 0; i <= 150000; i++) Lap1[i] = 0;\n\tfor (int i = 0; i <= 150000; i++) Lap2[i] = 0;\n\t\n\t// Step 1. Sorting\n\tvector<pair<int, int>> vec;\n\tfor (int i = 1; i <= N; i++) vec.push_back(make_pair(T[i], C[i]));\n\tsort(vec.begin(), vec.end());\n\treverse(vec.begin(), vec.end());\n\t\n\t// Step 2. Simulation\n\tint Answer = 0;\n\tfor (int i = 0; i < (int)vec.size(); i++) {\n\t\tint tm = 0;\n\t\tint cur1 = 0, Max1 = 0;\n\t\tint cur2 = 0, Max2 = 0;\n\t\tfor (int j = 0; j < vec[i].second; j++) {\n\t\t\tint Act0 = tm;\n\t\t\t\n\t\t\t// First Half\n\t\t\twhile (cur1 < Act0) { Max1 = max(Max1, Lap1[cur1]); cur1 += 1; }\n\t\t\tint Act1 = max(Max1, tm + vec[i].first);\n\t\t\ttm = Act1;\n\t\t\tLap1[Act0] = max(Lap1[Act0], tm);\n\t\t\t\n\t\t\t// Second Half\n\t\t\twhile (cur2 < Act1) { Max2 = max(Max2, Lap2[cur2]); cur2 += 1; }\n\t\t\tint Act2 = max(Max2, tm + vec[i].first);\n\t\t\ttm = Act2;\n\t\t\tLap2[Act1] = max(Lap2[Act1], tm);\n\t\t}\n\t\tAnswer = max(Answer, tm);\n\t}\n\t\n\t// Step 3. Output\n\tcout << Answer << endl;\n}\n\nint main() {\n\twhile (true) {\n\t\t// Input\n\t\tcin >> N; if (N == 0) break;\n\t\tfor (int i = 1; i <= N; i++) cin >> T[i] >> C[i];\n\t\t\n\t\t// Simulation\n\t\tsolve();\n\t}\n\treturn 0;\n}", "accuracy": 1, "time_ms": 140, "memory_kb": 4528, "score_of_the_acc": -0.6159, "final_rank": 18 }, { "submission_id": "aoj_1297_7150408", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\n#define rep(i, n) for(int i = 0; i < int(n); i++)\n#define per(i, n) for(int i = (n) - 1; 0 <= i; i--)\n#define rep2(i, l, r) for(int i = (l); i < int(r); i++)\n#define per2(i, l, r) for(int i = (r) -1; int(l) <= i; i--)\n#define MM << \" \" <<\n#define pb push_back\n#define eb emplace_back\n#define all(x) begin(x), end(x)\n#define rall(x) rbegin(x), rend(x)\n#define sz(x) int(x.size())\n#define each(e, x) for(auto &x : x)\n\ntemplate<class T>\nvoid print(const vector<T> &v, T x = 0) {\n int n = sz(v);\n for(int i = 0; i < n; i++) cout << v[i] + x << (i == n - 1 ? '\\n' : ' ');\n if(v.empty()) cout << '\\n';\n}\n\nusing ll = long long;\nusing pii = pair<int, int>;\nusing pll = pair<ll, ll>;\n\ntemplate<class T>\nbool chmax(T &x, const T &y) {\n return (x < y) ? (x = y, true) : false;\n}\n\ntemplate<class T>\nbool chmin(T &x, const T &y) {\n return (x > y) ? (x = y, true) : false;\n}\n\ntemplate<class T>\nusing minheap = priority_queue<T, vector<T>, greater<T>>;\n\nstruct io_setup {\n io_setup() {\n ios_base::sync_with_stdio(false);\n cin.tie(nullptr);\n cout << fixed << setprecision(15);\n }\n } io_set;\n\nll solve(int n) {\n vector<pii> da(n);\n rep(i,n) {\n cin >> da[i].first >> da[i].second;\n }\n\n vector<deque<pair<pii, vector<int>>>> q(2);\n int k[] = {0, 0};\n vector<int> tmp(n);\n iota(all(tmp), 0);\n q[0].push_back({make_pair(0, 0), tmp});\n// q[1].push_back({make_pair(0, 1), vector<int>(0)});\n int ans = 0;\n while(!q[0].empty() || !q[1].empty()) {\n decltype(q)::value_type ::value_type now;\n if(q[0].empty()) {\n now = q[1].front();\n q[1].pop_front();\n } else if(q[1].empty()) {\n now = q[0].front();\n q[0].pop_front();\n } else if(q[0].front() <= q[1].front()) {\n now = q[0].front();\n q[0].pop_front();\n } else {\n now = q[1].front();\n q[1].pop_front();\n }\n ans = now.first.first;\n// cerr << now.first.first MM now.first.second << endl;\n// if(q[0].front() == now) q[0].pop_front();\n// else q[1].pop_front();\n sort(all(now.second), [&da](int a, int b) {\n return da[a].first < da[b].first;\n });\n for(auto &i : now.second) {\n if(now.first.second == 1) da[i].second--;\n else if(da[i].second == 0) continue;\n if(chmax(k[now.first.second^1], now.first.first + da[i].first) || q[now.first.second^1].empty()) {\n q[now.first.second^1].push_back({make_pair(\n now.first.first + da[i].first,\n now.first.second^1\n ), vector<int>(1, i)});\n } else {\n q[now.first.second^1].back().second.emplace_back(i);\n }\n }\n }\n return ans;\n}\n\nint main() {\n int n;\n while(true) {\n cin >> n;\n if(n == 0) break;\n cout << solve(n) << endl;\n }\n}", "accuracy": 1, "time_ms": 20, "memory_kb": 3388, "score_of_the_acc": -0.0906, "final_rank": 8 }, { "submission_id": "aoj_1297_7132595", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\n\nvoid solve(){\n\tint n;\n\twhile(1){\n\t\tcin >> n;\n\t\tif(n == 0) break;\n\t\tvector<int> T(n), C(n);\n\t\tusing pi = pair<int, int>;\n\t\tpriority_queue<pi, vector<pi>, greater<pi>> L, R;\n\t\tint lma = 0, rma = 0;\n\t\tfor(int i = 0; i < n; i++){\n\t\t\tcin >> T[i] >> C[i];\n\t\t\tR.push({T[i], i});\n\t\t\trma = max(rma, T[i]);\n\t\t}\n\n\t\twhile(!L.empty() || !R.empty()){\n\t\t\tif(!L.empty() && !R.empty() && L.top().first == R.top().first){\n\t\t\t\tint x = L.top().first;\n\t\t\t\tint ll = lma;\n\t\t\t\tint rr = rma;\n\t\t\t\twhile(!L.empty() && L.top().first == x){\n\t\t\t\t\tint i = L.top().second;\n\t\t\t\t\tC[i]--;\n\t\t\t\t\tif(C[i] != 0){\n\t\t\t\t\t\tint t = max(T[i] + x, rr);\n\t\t\t\t\t\tR.push({t, i});\n\t\t\t\t\t\trma = max(rma, t);\n\t\t\t\t\t};\n\t\t\t\t\tL.pop();\n\t\t\t\t}\n\t\t\t\twhile(!R.empty() && R.top().first == x){\n\t\t\t\t\tint i = R.top().second;\n\t\t\t\t\tint t = max(T[i] + x, ll);\n\t\t\t\t\tL.push({t, i});\n\t\t\t\t\tlma = max(lma, t);\n\t\t\t\t\tR.pop();\n\t\t\t\t}\n\t\t\t}\n\t\t\telse if(!L.empty() && (R.empty() || L.top().first < R.top().first)){\n\t\t\t\tint x = L.top().first;\n\t\t\t\tint rr = rma;\n\t\t\t\twhile(!L.empty() && L.top().first == x){\n\t\t\t\t\tint i = L.top().second;\n\t\t\t\t\tC[i]--;\n\t\t\t\t\tif(C[i] != 0){\n\t\t\t\t\t\tint t = max(T[i] + x, rr);\n\t\t\t\t\t\tR.push({t, i});\n\t\t\t\t\t\trma = max(rma, t);\n\t\t\t\t\t};\n\t\t\t\t\tL.pop();\n\t\t\t\t}\n\t\t\t}\n\t\t\telse{\n\t\t\t\tint x = R.top().first;\n\t\t\t\tint ll = lma;\n\t\t\t\twhile(!R.empty() && R.top().first == x){\n\t\t\t\t\tint i = R.top().second;\n\t\t\t\t\tint t = max(T[i] + x, ll);\n\t\t\t\t\tL.push({t, i});\n\t\t\t\t\tlma = max(lma, t);\n\t\t\t\t\tR.pop();\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\tcout << max(lma, rma) << endl;\n\t}\n}\n\nint main(){\n\tsolve();\n}", "accuracy": 1, "time_ms": 20, "memory_kb": 3364, "score_of_the_acc": -0.0799, "final_rank": 6 }, { "submission_id": "aoj_1297_6494388", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\n#define rep(i, s, t) for (int i = (int)(s); i < (int)(t); ++i)\n#define revrep(i, t, s) for (int i = (int)(t)-1; i >= (int)(s); --i)\n#define all(x) begin(x), end(x)\ntemplate <typename T> bool chmax(T& a, const T& b) { return a < b ? (a = b, 1) : 0; }\ntemplate <typename T> bool chmin(T& a, const T& b) { return a > b ? (a = b, 1) : 0; }\n\nint main() {\n ios_base::sync_with_stdio(false);\n cin.tie(nullptr);\n cout << fixed << setprecision(15);\n\n while (1) {\n int n;\n cin >> n;\n if (n == 0) break;\n vector<int> t(n), c(n);\n rep(i,0,n) cin >> t[i] >> c[i];\n vector<int> idx(n);\n iota(all(idx), 0);\n sort(all(idx), [&](int i, int j) { return t[i] < t[j]; });\n queue<pair<int, int>> lane1, lane2;\n for (int i : idx) lane1.push({i, t[i]});\n int time1 = 0, time2 = 0;\n while (!lane1.empty() || !lane2.empty()) {\n int i, time;\n if (lane2.empty() || (!lane1.empty() && lane1.front().second < lane2.front().second)) {\n tie(i, time) = lane1.front();\n lane1.pop();\n chmax(time1, time);\n vector<int> is = {i};\n while (!lane1.empty() && lane1.front().second <= time1) {\n is.push_back(lane1.front().first);\n lane1.pop();\n }\n sort(all(is), [&](int i, int j) { return t[i] < t[j]; });\n for (int j : is) {\n lane2.push({j, time1 + t[j]});\n }\n } else {\n tie(i, time) = lane2.front();\n lane2.pop();\n chmax(time2, time);\n vector<int> is = {i};\n while (!lane2.empty() && lane2.front().second <= time2) {\n is.push_back(lane2.front().first);\n lane2.pop();\n }\n sort(all(is), [&](int i, int j) { return t[i] < t[j]; });\n for (int j : is) {\n --c[j];\n if (c[j]) {\n lane1.push({j, time2 + t[j]});\n }\n }\n }\n }\n cout << time2 << \"\\n\";\n }\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3332, "score_of_the_acc": -0.0643, "final_rank": 3 }, { "submission_id": "aoj_1297_6355065", "code_snippet": "#include <cstdio>\n#include <vector>\n#include <queue>\n#include <set>\n#include <map>\n#include <algorithm>\nusing namespace std;\n\nint main() {\nint n;\nwhile (scanf(\"%d\", &n) == 1) {\n if (n == 0) break;\n vector<int> t(n), c(n);\n for (int i = 0; i < n; i++) scanf(\"%d%d\", &t[i], &c[i]);\n map<int, vector<int>> mp[2];\n vector<int> st(2, -1);\n set<int> have;\n \n auto insert = [&](map<int, vector<int>>& u, int time, int id) {\n have.insert(time);\n if (!u.count(time)) u[time] = vector<int> ();\n u[time].push_back(id);\n };\n \n for (int i = 0; i < n; i++) {\n insert(mp[0], 0, i);\n }\n int ans = 0;\n while (have.size() != 0) {\n int u = *(have.begin());\n have.erase(u);\n for (int i = 0; i < 2; i++) {\n int j = !i;\n if (mp[i].count(u)) {\n vector<int>& all = mp[i][u];\n sort(all.begin(), all.end(), [&](auto u, auto v) {\n return t[u] < t[v];\n });\n for (int now: all) {\n if (i == 1) c[now]--;\n st[j] = max(u + t[now], st[j]);\n int rec = st[j];\n ans = max(ans, rec);\n if (c[now] != 0) {\n insert(mp[j], rec, now); \n }\n }\n }\n }\n }\n printf(\"%d\\n\", ans);\n}\n return 0; \n}", "accuracy": 1, "time_ms": 100, "memory_kb": 4436, "score_of_the_acc": -0.5694, "final_rank": 17 }, { "submission_id": "aoj_1297_5990310", "code_snippet": "// #pragma comment(linker, \"/stack:200000000\")\n\n#include <bits/stdc++.h>\n\n#include <limits>\n#include <type_traits>\n\nnamespace suisen {\n// ! utility\ntemplate <typename ...Types>\nusing constraints_t = std::enable_if_t<std::conjunction_v<Types...>, std::nullptr_t>;\ntemplate <bool cond_v, typename Then, typename OrElse>\nconstexpr decltype(auto) constexpr_if(Then&& then, OrElse&& or_else) {\n if constexpr (cond_v) {\n return std::forward<Then>(then);\n } else {\n return std::forward<OrElse>(or_else);\n }\n}\n\n// ! function\ntemplate <typename ReturnType, typename Callable, typename ...Args>\nusing is_same_as_invoke_result = std::is_same<std::invoke_result_t<Callable, Args...>, ReturnType>;\ntemplate <typename F, typename T>\nusing is_uni_op = is_same_as_invoke_result<T, F, T>;\ntemplate <typename F, typename T>\nusing is_bin_op = is_same_as_invoke_result<T, F, T, T>;\n\ntemplate <typename Comparator, typename T>\nusing is_comparator = std::is_same<std::invoke_result_t<Comparator, T, T>, bool>;\n\n// ! integral\ntemplate <typename T, typename = constraints_t<std::is_integral<T>>>\nconstexpr int bit_num = std::numeric_limits<std::make_unsigned_t<T>>::digits;\ntemplate <typename T, unsigned int n>\nstruct is_nbit { static constexpr bool value = bit_num<T> == n; };\ntemplate <typename T, unsigned int n>\nstatic constexpr bool is_nbit_v = is_nbit<T, n>::value;\n\n// ?\ntemplate <typename T>\nstruct safely_multipliable {};\ntemplate <>\nstruct safely_multipliable<int> { using type = long long; };\ntemplate <>\nstruct safely_multipliable<long long> { using type = __int128_t; };\ntemplate <>\nstruct safely_multipliable<float> { using type = float; };\ntemplate <>\nstruct safely_multipliable<double> { using type = double; };\ntemplate <>\nstruct safely_multipliable<long double> { using type = long double; };\ntemplate <typename T>\nusing safely_multipliable_t = typename safely_multipliable<T>::type;\n\n} // namespace suisen\n\n// ! type aliases\nusing i128 = __int128_t;\nusing u128 = __uint128_t;\nusing ll = long long;\nusing uint = unsigned int;\nusing ull = unsigned long long;\n\ntemplate <typename T> using vec = std::vector<T>;\ntemplate <typename T> using vec2 = vec<vec <T>>;\ntemplate <typename T> using vec3 = vec<vec2<T>>;\ntemplate <typename T> using vec4 = vec<vec3<T>>;\n\ntemplate <typename T>\nusing pq_greater = std::priority_queue<T, std::vector<T>, std::greater<T>>;\ntemplate <typename T, typename U>\nusing umap = std::unordered_map<T, U>;\n\n// ! macros (capital: internal macro)\n#define OVERLOAD2(_1,_2,name,...) name\n#define OVERLOAD3(_1,_2,_3,name,...) name\n#define OVERLOAD4(_1,_2,_3,_4,name,...) name\n\n#define REP4(i,l,r,s) for(std::remove_reference_t<std::remove_const_t<decltype(r)>>i=(l);i<(r);i+=(s))\n#define REP3(i,l,r) REP4(i,l,r,1)\n#define REP2(i,n) REP3(i,0,n)\n#define REPINF3(i,l,s) for(std::remove_reference_t<std::remove_const_t<decltype(l)>>i=(l);;i+=(s))\n#define REPINF2(i,l) REPINF3(i,l,1)\n#define REPINF1(i) REPINF2(i,0)\n#define RREP4(i,l,r,s) for(std::remove_reference_t<std::remove_const_t<decltype(r)>>i=(l)+fld((r)-(l)-1,s)*(s);i>=(l);i-=(s))\n#define RREP3(i,l,r) RREP4(i,l,r,1)\n#define RREP2(i,n) RREP3(i,0,n)\n\n#define rep(...) OVERLOAD4(__VA_ARGS__, REP4 , REP3 , REP2 )(__VA_ARGS__)\n#define rrep(...) OVERLOAD4(__VA_ARGS__, RREP4 , RREP3 , RREP2 )(__VA_ARGS__)\n#define repinf(...) OVERLOAD3(__VA_ARGS__, REPINF3, REPINF2, REPINF1)(__VA_ARGS__)\n\n#define CAT_I(a, b) a##b\n#define CAT(a, b) CAT_I(a, b)\n#define UNIQVAR(tag) CAT(tag, __LINE__)\n#define loop(n) for (std::remove_reference_t<std::remove_const_t<decltype(n)>> UNIQVAR(loop_variable) = n; UNIQVAR(loop_variable) --> 0;)\n\n#define all(iterable) (iterable).begin(), (iterable).end()\n#define input(type, ...) type __VA_ARGS__; read(__VA_ARGS__)\n\n// ! I/O utilities\n\n// pair\ntemplate <typename T, typename U>\nstd::ostream& operator<<(std::ostream& out, const std::pair<T, U> &a) {\n return out << a.first << ' ' << a.second;\n}\n// tuple\ntemplate <unsigned int N = 0, typename ...Args>\nstd::ostream& operator<<(std::ostream& out, const std::tuple<Args...> &a) {\n if constexpr (N >= std::tuple_size_v<std::tuple<Args...>>) {\n return out;\n } else {\n out << std::get<N>(a);\n if constexpr (N + 1 < std::tuple_size_v<std::tuple<Args...>>) {\n out << ' ';\n }\n return operator<<<N + 1>(out, a);\n }\n}\n// vector\ntemplate <typename T>\nstd::ostream& operator<<(std::ostream& out, const std::vector<T> &a) {\n for (auto it = a.begin(); it != a.end();) {\n out << *it;\n if (++it != a.end()) out << ' ';\n }\n return out;\n}\ninline void print() { std::cout << '\\n'; }\ntemplate <typename Head, typename... Tail>\ninline void print(const Head &head, const Tail &...tails) {\n std::cout << head;\n if (sizeof...(tails)) std::cout << ' ';\n print(tails...);\n}\ntemplate <typename Iterable>\nauto print_all(const Iterable& v, std::string sep = \" \", std::string end = \"\\n\") -> decltype(std::cout << *v.begin(), void()) {\n for (auto it = v.begin(); it != v.end();) {\n std::cout << *it;\n if (++it != v.end()) std::cout << sep;\n }\n std::cout << end;\n}\n\n// pair\ntemplate <typename T, typename U>\nstd::istream& operator>>(std::istream& in, std::pair<T, U> &a) {\n return in >> a.first >> a.second;\n}\n// tuple\ntemplate <unsigned int N = 0, typename ...Args>\nstd::istream& operator>>(std::istream& in, std::tuple<Args...> &a) {\n if constexpr (N >= std::tuple_size_v<std::tuple<Args...>>) {\n return in;\n } else {\n return operator>><N + 1>(in >> std::get<N>(a), a);\n }\n}\n// vector\ntemplate <typename T>\nstd::istream& operator>>(std::istream& in, std::vector<T> &a) {\n for (auto it = a.begin(); it != a.end(); ++it) in >> *it;\n return in;\n}\ntemplate <typename ...Args>\nvoid read(Args &...args) {\n ( std::cin >> ... >> args );\n}\n\n// ! integral utilities\n\n// Returns pow(-1, n)\ntemplate <typename T>\nconstexpr inline int pow_m1(T n) {\n return -(n & 1) | 1;\n}\n// Returns pow(-1, n)\ntemplate <>\nconstexpr inline int pow_m1<bool>(bool n) {\n return -int(n) | 1;\n}\n\n// Returns floor(x / y)\ntemplate <typename T>\nconstexpr inline T fld(const T x, const T y) {\n return (x ^ y) >= 0 ? x / y : (x - (y + pow_m1(y >= 0))) / y;\n}\ntemplate <typename T>\nconstexpr inline T cld(const T x, const T y) {\n return (x ^ y) <= 0 ? x / y : (x + (y + pow_m1(y >= 0))) / y;\n}\n\ntemplate <typename T, suisen::constraints_t<suisen::is_nbit<T, 32>> = nullptr>\nconstexpr inline int popcount(const T x) { return __builtin_popcount(x); }\ntemplate <typename T, suisen::constraints_t<suisen::is_nbit<T, 64>> = nullptr>\nconstexpr inline int popcount(const T x) { return __builtin_popcountll(x); }\ntemplate <typename T, suisen::constraints_t<suisen::is_nbit<T, 32>> = nullptr>\nconstexpr inline int count_lz(const T x) { return x ? __builtin_clz(x) : suisen::bit_num<T>; }\ntemplate <typename T, suisen::constraints_t<suisen::is_nbit<T, 64>> = nullptr>\nconstexpr inline int count_lz(const T x) { return x ? __builtin_clzll(x) : suisen::bit_num<T>; }\ntemplate <typename T, suisen::constraints_t<suisen::is_nbit<T, 32>> = nullptr>\nconstexpr inline int count_tz(const T x) { return x ? __builtin_ctz(x) : suisen::bit_num<T>; }\ntemplate <typename T, suisen::constraints_t<suisen::is_nbit<T, 64>> = nullptr>\nconstexpr inline int count_tz(const T x) { return x ? __builtin_ctzll(x) : suisen::bit_num<T>; }\ntemplate <typename T>\nconstexpr inline int floor_log2(const T x) { return suisen::bit_num<T> - 1 - count_lz(x); }\ntemplate <typename T>\nconstexpr inline int ceil_log2(const T x) { return floor_log2(x) + ((x & -x) != x); }\ntemplate <typename T>\nconstexpr inline int kth_bit(const T x, const unsigned int k) { return (x >> k) & 1; }\ntemplate <typename T>\nconstexpr inline int parity(const T x) { return popcount(x) & 1; }\n\nstruct all_subset {\n struct all_subset_iter {\n const int s; int t;\n constexpr all_subset_iter(int s) : s(s), t(s + 1) {}\n constexpr auto operator*() const { return t; }\n constexpr auto operator++() {}\n constexpr auto operator!=(std::nullptr_t) { return t ? (--t &= s, true) : false; }\n };\n int s;\n constexpr all_subset(int s) : s(s) {}\n constexpr auto begin() { return all_subset_iter(s); }\n constexpr auto end() { return nullptr; }\n};\n\n// ! container\n\ntemplate <typename T, typename Comparator, suisen::constraints_t<suisen::is_comparator<Comparator, T>> = nullptr>\nauto priqueue_comp(const Comparator comparator) {\n return std::priority_queue<T, std::vector<T>, Comparator>(comparator);\n}\n\ntemplate <typename Iterable>\nauto isize(const Iterable &iterable) -> decltype(int(iterable.size())) {\n return iterable.size();\n}\n\ntemplate <typename T, typename Gen, suisen::constraints_t<suisen::is_same_as_invoke_result<T, Gen, int>> = nullptr>\nauto generate_vector(int n, Gen generator) {\n std::vector<T> v(n);\n for (int i = 0; i < n; ++i) v[i] = generator(i);\n return v;\n}\n\ntemplate <typename T>\nvoid sort_unique_erase(std::vector<T> &a) {\n std::sort(a.begin(), a.end());\n a.erase(std::unique(a.begin(), a.end()), a.end());\n}\n\n// ! other utilities\n\n// x <- min(x, y). returns true iff `x` has chenged.\ntemplate <typename T>\ninline bool chmin(T &x, const T &y) {\n if (y >= x) return false;\n x = y;\n return true;\n}\n// x <- max(x, y). returns true iff `x` has chenged.\ntemplate <typename T>\ninline bool chmax(T &x, const T &y) {\n if (y <= x) return false;\n x = y;\n return true;\n}\n\nnamespace suisen {}\nusing namespace suisen;\nusing namespace std;\n\nstruct io_setup {\n io_setup(int precision = 20) {\n std::ios::sync_with_stdio(false);\n std::cin.tie(nullptr);\n std::cout << std::fixed << std::setprecision(precision);\n }\n} io_setup_ {};\n\n// ! code from here\n\nconstexpr int T = 300 * 500 + 10;\n\nvoid solve(vector<pair<int, int>> &&tc) {\n sort(all(tc), greater<pair<int, int>>());\n vector max_time(2, vector<int>(T, 0));\n int ans = 0;\n for (const auto &[t, c] : tc) {\n int time = 0, lane = 0;\n rep(i, 2 * c) {\n int reach_time = max(time + t, max_time[lane][time]);\n rep(k, time + 1, reach_time) {\n chmax(max_time[lane][k], reach_time);\n }\n time = reach_time;\n lane ^= 1;\n }\n chmax(ans, time);\n }\n print(ans);\n}\n\nint main() {\n while (true) {\n input(int, n);\n if (n == 0) break;\n vector<pair<int, int>> tc(n);\n read(tc);\n solve(move(tc));\n }\n return 0;\n}", "accuracy": 1, "time_ms": 150, "memory_kb": 5428, "score_of_the_acc": -1.019, "final_rank": 20 }, { "submission_id": "aoj_1297_5804849", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\nstruct swimmer {\n int t, c, nxt;\n swimmer(int t, int c, int nxt) : t(t), c(c), nxt(nxt) {}\n constexpr bool operator<(const swimmer& rhs) const { return t < rhs.t; }\n};\n\nvoid solve(int n) {\n vector<queue<swimmer>> que(2);\n for (int i = 0; i < n; i++) {\n int t, c;\n cin >> t >> c;\n que[1].emplace(t, 2 * c + 1, 0);\n }\n\n int ans = 0;\n while (!que[0].empty() || !que[1].empty()) {\n int x = (que[0].empty() || (!que[1].empty() && que[0].front().nxt > que[1].front().nxt));\n int T = que[x].front().nxt;\n ans = T;\n vector<swimmer> start;\n while (!que[x].empty() && que[x].front().nxt <= T) {\n start.emplace_back(que[x].front());\n que[x].pop();\n }\n sort(start.begin(), start.end());\n for (auto s : start) {\n if (s.c == 1) continue;\n que[x ^ 1].emplace(s.t, s.c - 1, T + s.t);\n }\n }\n\n cout << ans << '\\n';\n}\n\nint main() {\n cin.tie(0);\n ios::sync_with_stdio(false);\n int n;\n while (cin >> n, n) solve(n);\n return 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3468, "score_of_the_acc": -0.125, "final_rank": 13 }, { "submission_id": "aoj_1297_5804847", "code_snippet": "#define LOCAL\n#include <bits/stdc++.h>\nusing namespace std;\n#pragma region Macros\ntypedef long long ll;\ntypedef __int128_t i128;\ntypedef unsigned int uint;\ntypedef unsigned long long ull;\n#define ALL(x) (x).begin(), (x).end()\n\ntemplate <typename T> istream& operator>>(istream& is, vector<T>& v) {\n for (T& x : v) is >> x;\n return is;\n}\ntemplate <typename T> ostream& operator<<(ostream& os, const vector<T>& v) {\n for (int i = 0; i < (int)v.size(); i++) {\n os << v[i] << (i + 1 == (int)v.size() ? \"\" : \" \");\n }\n return os;\n}\ntemplate <typename T, typename U> ostream& operator<<(ostream& os, const pair<T, U>& p) {\n os << '(' << p.first << ',' << p.second << ')';\n return os;\n}\ntemplate <typename T, typename U> ostream& operator<<(ostream& os, const map<T, U>& m) {\n os << '{';\n for (auto itr = m.begin(); itr != m.end();) {\n os << '(' << itr->first << ',' << itr->second << ')';\n if (++itr != m.end()) os << ',';\n }\n os << '}';\n return os;\n}\ntemplate <typename T, typename U> ostream& operator<<(ostream& os, const unordered_map<T, U>& m) {\n os << '{';\n for (auto itr = m.begin(); itr != m.end();) {\n os << '(' << itr->first << ',' << itr->second << ')';\n if (++itr != m.end()) os << ',';\n }\n os << '}';\n return os;\n}\ntemplate <typename T> ostream& operator<<(ostream& os, const set<T>& s) {\n os << '{';\n for (auto itr = s.begin(); itr != s.end();) {\n os << *itr;\n if (++itr != s.end()) os << ',';\n }\n os << '}';\n return os;\n}\ntemplate <typename T> ostream& operator<<(ostream& os, const multiset<T>& s) {\n os << '{';\n for (auto itr = s.begin(); itr != s.end();) {\n os << *itr;\n if (++itr != s.end()) os << ',';\n }\n os << '}';\n return os;\n}\ntemplate <typename T> ostream& operator<<(ostream& os, const unordered_set<T>& s) {\n os << '{';\n for (auto itr = s.begin(); itr != s.end();) {\n os << *itr;\n if (++itr != s.end()) os << ',';\n }\n os << '}';\n return os;\n}\ntemplate <typename T> ostream& operator<<(ostream& os, const deque<T>& v) {\n for (int i = 0; i < (int)v.size(); i++) {\n os << v[i] << (i + 1 == (int)v.size() ? \"\" : \" \");\n }\n return os;\n}\n\ntemplate <int i, typename T> void print_tuple(ostream&, const T&) {}\ntemplate <int i, typename T, typename H, class... Args> void print_tuple(ostream& os, const T& t) {\n if (i) os << ',';\n os << get<i>(t);\n print_tuple<i + 1, T, Args...>(os, t);\n}\ntemplate <typename... Args> ostream& operator<<(ostream& os, const tuple<Args...>& t) {\n os << '{';\n print_tuple<0, tuple<Args...>, Args...>(os, t);\n return os << '}';\n}\n\nvoid debug_out() { cerr << '\\n'; }\ntemplate <class Head, class... Tail> void debug_out(Head&& head, Tail&&... tail) {\n cerr << head;\n if (sizeof...(Tail) > 0) cerr << \", \";\n debug_out(move(tail)...);\n}\n#ifdef LOCAL\n#define debug(...) \\\n cerr << \" \"; \\\n cerr << #__VA_ARGS__ << \" :[\" << __LINE__ << \":\" << __FUNCTION__ << \"]\" << '\\n'; \\\n cerr << \" \"; \\\n debug_out(__VA_ARGS__)\n#else\n#define debug(...) 42\n#endif\n\ntemplate <typename T> T gcd(T x, T y) { return y != 0 ? gcd(y, x % y) : x; }\ntemplate <typename T> T lcm(T x, T y) { return x / gcd(x, y) * y; }\n\nint topbit(signed t) { return t == 0 ? -1 : 31 - __builtin_clz(t); }\nint topbit(long long t) { return t == 0 ? -1 : 63 - __builtin_clzll(t); }\nint botbit(signed a) { return a == 0 ? 32 : __builtin_ctz(a); }\nint botbit(long long a) { return a == 0 ? 64 : __builtin_ctzll(a); }\nint popcount(signed t) { return __builtin_popcount(t); }\nint popcount(long long t) { return __builtin_popcountll(t); }\nbool ispow2(int i) { return i && (i & -i) == i; }\n\ntemplate <class T> T ceil(T x, T y) {\n assert(y >= 1);\n return (x > 0 ? (x + y - 1) / y : x / y);\n}\ntemplate <class T> T floor(T x, T y) {\n assert(y >= 1);\n return (x > 0 ? x / y : (x - y + 1) / y);\n}\n\ntemplate <class T1, class T2> inline bool chmin(T1& a, T2 b) {\n if (a > b) {\n a = b;\n return true;\n }\n return false;\n}\ntemplate <class T1, class T2> inline bool chmax(T1& a, T2 b) {\n if (a < b) {\n a = b;\n return true;\n }\n return false;\n}\n#pragma endregion\n\nconst int INF = 1e9;\nconst long long IINF = 1e18;\nconst int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};\nconst char dir[4] = {'D', 'R', 'U', 'L'};\nconst long long MOD = 1000000007;\n// const long long MOD = 998244353;\n\nstruct swimmer {\n int t, c, nxt;\n swimmer(int t, int c, int nxt) : t(t), c(c), nxt(nxt) {}\n constexpr bool operator<(const swimmer& rhs) const { return t < rhs.t; }\n};\n\nvoid solve(int n) {\n vector<queue<swimmer>> que(2);\n for (int i = 0; i < n; i++) {\n int t, c;\n cin >> t >> c;\n que[1].emplace(t, 2 * c + 1, 0);\n }\n\n int ans = 0;\n while (!que[0].empty() || !que[1].empty()) {\n int x = (que[0].empty() || (!que[1].empty() && que[0].front().nxt > que[1].front().nxt));\n int T = que[x].front().nxt;\n ans = T;\n vector<swimmer> start;\n while (!que[x].empty() && que[x].front().nxt <= T) {\n start.emplace_back(que[x].front());\n que[x].pop();\n }\n sort(start.begin(), start.end());\n for (auto s : start) {\n if (s.c == 1) continue;\n que[x ^ 1].emplace(s.t, s.c - 1, T + s.t);\n }\n }\n\n cout << ans << '\\n';\n}\n\nint main() {\n cin.tie(0);\n ios::sync_with_stdio(false);\n int n;\n while (cin >> n, n) solve(n);\n return 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3232, "score_of_the_acc": -0.0196, "final_rank": 1 }, { "submission_id": "aoj_1297_5542608", "code_snippet": "#pragma GCC target(\"avx2\")\n#pragma GCC optimize(\"Ofast\")\n#pragma GCC optimize(\"unroll-loops\")\n#include <bits/stdc++.h>\n//#include <atcoder/all>\nusing namespace std;\n//using namespace atcoder;\n#define DEBUG\n#ifdef DEBUG\ntemplate <class T, class U>\nostream &operator<<(ostream &os, const pair<T, U> &p) {\n os << '(' << p.first << ',' << p.second << ')';\n return os;\n}\ntemplate <class T> ostream &operator<<(ostream &os, const vector<T> &v) {\n os << '{';\n for(int i = 0; i < (int)v.size(); i++) {\n if(i) { os << ','; }\n os << v[i];\n }\n os << '}';\n return os;\n}\nostream &operator<<(ostream &os, const set<int> &se){\n os << '{';\n int now = 0;\n for(auto x : se){\n if(now) { os << ','; }\n os << x; \n now++;\n }\n os << '}';\n return os;\n}\nvoid debugg() { cerr << endl; }\ntemplate <class T, class... Args>\nvoid debugg(const T &x, const Args &... args) {\n cerr << \" \" << x;\n debugg(args...);\n}\n#define debug(...) \\\n cerr << __LINE__ << \" [\" << #__VA_ARGS__ << \"]: \", debugg(__VA_ARGS__)\n#define dump(x) cerr << __LINE__ << \" \" << #x << \" = \" << (x) << endl\n#else\n#define debug(...) (void(0))\n#define dump(x) (void(0))\n#endif\nusing namespace std;\ntypedef long long ll;\ntypedef vector<ll> vl;\ntypedef vector<vl> vvl;\ntypedef vector<char> vc;\ntypedef vector<string> vs;\ntypedef vector<bool> vb;\ntypedef vector<double> vd;\ntypedef pair<ll,ll> P;\ntypedef pair<int,int> pii;\ntypedef vector<P> vpl;\ntypedef tuple<ll,ll,ll> tapu;\n#define rep(i,n) for(int i=0; i<(n); i++)\n#define REP(i,a,b) for(int i=(a); i<(b); i++)\n#define all(x) (x).begin(), (x).end()\n#define rall(x) (x).rbegin(), (x).rend()\nconst int inf = (1<<30)-1;\nconst ll linf = 1LL<<61;\nconst int MAX = 510000;\nint dy[8] = {0,1,0,-1,1,-1,-1,1};\nint dx[8] = {-1,0,1,0,1,-1,1,-1};\nconst double pi = acos(-1);\nconst double eps = 1e-7;\ntemplate<typename T1,typename T2> inline bool chmin(T1 &a,T2 b){\n\tif(a>b){\n\t\ta = b; return true;\n\t}\n\telse return false;\n}\ntemplate<typename T1,typename T2> inline bool chmax(T1 &a,T2 b){\n\tif(a<b){\n\t\ta = b; return true;\n\t}\n\telse return false;\n}\ntemplate<typename T> inline void print(T &a){\n int sz = a.size();\n for(auto itr = a.begin(); itr != a.end(); itr++){\n\t\tcout << *itr;\n sz--;\n if(sz) cout << \" \";\n\t}\n cout << \"\\n\";\n}\ntemplate<typename T1,typename T2> inline void print2(T1 a, T2 b){\n\tcout << a << \" \" << b << \"\\n\";\n}\ntemplate<typename T1,typename T2,typename T3> inline void print3(T1 a, T2 b, T3 c){\n\tcout << a << \" \" << b << \" \" << c << \"\\n\";\n}\nvoid mark() {cout << \"#\" << \"\\n\";}\nll pcount(ll x) {return __builtin_popcountll(x);}\n//const int mod = 1e9 + 7;\nconst int mod = 998244353;\n\nint main(){\n while(1){\n int n; cin >> n;\n if(!n) break;\n vpl v(n);\n rep(i,n){\n cin >> v[i].first >> v[i].second;\n }\n int ans = 0;\n sort(all(v));\n vector<tapu> L,R;\n rep(i,n) L.emplace_back(v[i].first,v[i].second,i);\n while(!L.empty() || !R.empty()){\n int tl=inf,cl,idl,tr=inf,cr,idr;\n if(!L.empty()) tie(tl,cl,idl) = L[0];\n if(!R.empty()) tie(tr,cr,idr) = R[0];\n int t = min(tl,tr);\n ans += t;\n vl ls,rs;\n rep(i,L.size()) get<0>(L[i]) -= t;\n rep(i,R.size()) get<0>(R[i]) -= t;\n rep(i,L.size()){\n if(get<0>(L[i]) > 0) break;\n ls.push_back(get<2>(L[i]));\n }\n rep(i,R.size()){\n if(get<0>(R[i]) > 0) break;\n rs.push_back(get<2>(R[i]));\n }\n L.erase(L.begin(),L.begin()+(int)ls.size());\n R.erase(R.begin(),R.begin()+(int)rs.size());\n sort(all(ls)); sort(all(rs));\n for(auto x : ls){\n ll mx = -inf;\n if(!R.empty()) chmax(mx,get<0>(R.back()));\n R.emplace_back(max(mx,v[x].first),v[x].second,x);\n }\n for(auto x : rs){\n if(v[x].second == 1) continue;\n v[x].second--;\n ll mx = -inf;\n if(!L.empty()) chmax(mx,get<0>(L.back()));\n L.emplace_back(max(mx,v[x].first),v[x].second,x); \n }\n }\n cout << ans << \"\\n\";\n }\n}", "accuracy": 1, "time_ms": 20, "memory_kb": 3332, "score_of_the_acc": -0.0656, "final_rank": 4 }, { "submission_id": "aoj_1297_5509018", "code_snippet": "#include <stdio.h>\n#include <bits/stdc++.h>\nusing namespace std;\n#define rep(i,n) for (int i = 0; i < (n); ++i)\n#define Inf 1000000001\n\nstruct Data{\n\tint t,c;\n\tint remain;\n};\n\nint main(){\n\t\n\twhile(true){\n\t\tint n;\n\t\tcin>>n;\n\t\t\n\t\tif(n==0)break;\n\t\t\n\t\tvector<Data> D0(n),D1(0);\n\t\t\n\t\trep(i,n){\n\t\t\tcin>>D0[i].t>>D0[i].c;\n\t\t\tD0[i].remain = D0[i].t;\n\t\t}\n\t\t\n\t\tint ans = 0;\n\t\t\n\t\twhile(D0.size()!=0 || D1.size()!=0){\n\t\t\tans++;\n\t\t\tvector<Data> d0,d1,nd0,nd1;\n\t\t\t\n\t\t\trep(i,D0.size()){\n\t\t\t\tD0[i].remain--;\n\t\t\t\tif(D0[i].remain==0){\n\t\t\t\t\tnd1.push_back(D0[i]);\n\t\t\t\t}\n\t\t\t\telse{\n\t\t\t\t\td0.push_back(D0[i]);\n\t\t\t\t}\n\t\t\t}\n\t\t\t\n\t\t\trep(i,D1.size()){\n\t\t\t\tD1[i].remain--;\n\t\t\t\tif(D1[i].remain==0){\n\t\t\t\t\tD1[i].c--;\n\t\t\t\t\tif(D1[i].c!=0){\n\t\t\t\t\t\tnd0.push_back(D1[i]);\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\telse{\n\t\t\t\t\td1.push_back(D1[i]);\n\t\t\t\t}\n\t\t\t}\n\t\t\t\n\t\t\tint m0 = 0,m1 = 0;\n\t\t\trep(i,d0.size()){\n\t\t\t\tm0 = max(m0,d0[i].remain);\n\t\t\t}\n\t\t\trep(i,d1.size()){\n\t\t\t\tm1 = max(m1,d1[i].remain);\n\t\t\t}\n\t\t\t\n\t\t\trep(i,nd0.size()){\n\t\t\t\tnd0[i].remain = max(nd0[i].t,m0);\n\t\t\t\td0.push_back(nd0[i]);\n\t\t\t}\n\t\t\t\n\t\t\trep(i,nd1.size()){\n\t\t\t\tnd1[i].remain = max(nd1[i].t,m1);\n\t\t\t\td1.push_back(nd1[i]);\n\t\t\t}\n\t\t\t\n\t\t\tswap(d0,D0);\n\t\t\tswap(d1,D1);\n\t\t\t\n\t\t\t\n\t\t\t\n\t\t}\n\t\t\n\t\tcout<<ans<<endl;\t\t\n\t\t\n\t}\n\t\n\treturn 0;\n}", "accuracy": 1, "time_ms": 1160, "memory_kb": 3244, "score_of_the_acc": -0.1815, "final_rank": 16 }, { "submission_id": "aoj_1297_5327797", "code_snippet": "#include <iostream>\n#include <string>\n#include <algorithm>\n#include <vector>\n#include <queue>\n#include <utility>\n#include <tuple>\n#include <cmath>\n#include <numeric>\n#include <set>\n#include <map>\n#include <array>\n#include <complex>\n#include <iomanip>\n#include <cassert>\n#include <random>\n#include <chrono>\nusing ll = long long;\nusing std::cin;\nusing std::cout;\nusing std::endl;\ntemplate<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }\ntemplate<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }\nconst int inf = (int)1e9 + 7;\nconst long long INF = 1LL << 60;\n\nvoid solve()\n{\n int n; cin >> n; if(n == 0) exit(0);\n std::vector<int> t(n), c(n);\n using T = std::tuple<int, int, int>;\n std::priority_queue<T, std::vector<T>, std::greater<>> pq1, pq2;\n for (int i = 0; i < n; ++i)\n {\n cin >> t[i] >> c[i];\n pq1.emplace(0, t[i], c[i]);\n }\n\n int t1 = 0, t2 = 0;\n while(not pq1.empty() or not pq2.empty())\n {\n bool exe1 = true;\n if(pq1.empty())\n {\n exe1 = false;\n }\n else if(not pq2.empty())\n {\n int nt1 = std::get<0>(pq1.top()) + std::get<1>(pq1.top());\n int nt2 = std::get<0>(pq2.top()) + std::get<1>(pq2.top());\n if(nt1 > nt2)\n {\n exe1 = false;\n }\n else if(nt1 == nt2 and std::get<0>(pq2.top()) < std::get<0>(pq1.top()))\n {\n exe1 = false;\n }\n }\n if(exe1)\n {\n int nt1 = std::max(t1, std::get<0>(pq1.top()) + std::get<1>(pq1.top()));\n while(not pq1.empty())\n {\n auto[T, t, c] = pq1.top();\n if(t + T <= nt1)\n {\n pq2.emplace(nt1, t, c);\n pq1.pop();\n }\n else\n {\n break;\n }\n }\n t1 = nt1;\n }\n else\n {\n int nt2 = std::max(t2, std::get<0>(pq2.top()) + std::get<1>(pq2.top()));\n while(not pq2.empty())\n {\n auto[T, t, c] = pq2.top();\n if(t + T <= nt2)\n {\n if(c > 1)\n pq1.emplace(nt2, t, c - 1);\n pq2.pop();\n }\n else\n {\n break;\n }\n }\n t2 = nt2;\n }\n }\n cout << t2 << \"\\n\";\n}\nint main()\n{\n std::cin.tie(nullptr);\n std::ios::sync_with_stdio(false);\n\n\n int kkt = 89;\n //cin >> kkt;\n while(kkt)\n {\n solve();\n }\n return 0;\n}", "accuracy": 1, "time_ms": 20, "memory_kb": 3392, "score_of_the_acc": -0.0924, "final_rank": 11 }, { "submission_id": "aoj_1297_5180381", "code_snippet": "#line 2 \"/Users/tiramister/CompetitiveProgramming/Cpp/CppLibrary/Tools/heap_alias.hpp\"\n\n#include <queue>\n\ntemplate <class T>\nusing MaxHeap = std::priority_queue<T>;\ntemplate <class T>\nusing MinHeap = std::priority_queue<T, std::vector<T>, std::greater<T>>;\n#line 2 \"main.cpp\"\n#include <iostream>\n#include <vector>\n#include <tuple>\n\nusing namespace std;\n\nbool solve() {\n int n;\n cin >> n;\n if (n == 0) return false;\n\n vector<int> tmax(2, 0);\n MinHeap<tuple<int, int, int, int>> heap;\n // time, speed, lap, lane\n while (n--) {\n int s, c;\n cin >> s >> c;\n heap.emplace(s, s, c, 0);\n tmax[0] = max(tmax[0], s);\n }\n\n while (!heap.empty()) {\n auto [t, s, c, l] = heap.top();\n heap.pop();\n\n if (l == 1) --c;\n if (c == 0) continue;\n\n int nl = 1 - l;\n int nt = max(t + s, tmax[nl]);\n tmax[nl] = max(tmax[nl], nt);\n heap.emplace(nt, s, c, nl);\n }\n\n cout << tmax[1] << \"\\n\";\n return true;\n}\n\nint main() {\n cin.tie(nullptr);\n ios::sync_with_stdio(false);\n\n while (solve()) {}\n\n return 0;\n}", "accuracy": 1, "time_ms": 20, "memory_kb": 3388, "score_of_the_acc": -0.0906, "final_rank": 8 }, { "submission_id": "aoj_1297_5137026", "code_snippet": "#include <iostream>\n#include <cstdio>\n#include <cmath>\n#include <ctime>\n#include <cstdlib>\n#include <cassert>\n#include <vector>\n#include <list>\n#include <stack>\n#include <queue>\n#include <deque>\n#include <map>\n#include <set>\n#include <bitset>\n#include <string>\n#include <algorithm>\n#include <utility>\n#include <complex>\n#define rep(x, s, t) for(llint (x) = (s); (x) <= (t); (x)++)\n#define chmin(x, y) (x) = min((x), (y))\n#define chmax(x, y) (x) = max((x), (y))\n#define all(x) (x).begin(),(x).end()\n#define outl(x) cout << (x) << \"\\n\"\n#define inf 1e18\n\nusing namespace std;\n\ntypedef long long llint;\ntypedef long long ll;\ntypedef pair<int, int> P;\n\nstruct UnionFind{\n\tint size;\n\tvector<int> parent;\n\tvector<int> rank;\n\tvector<int> v, e;\n\t\n\tUnionFind(){}\n\tUnionFind(int size){\n\t\tthis->size = size;\n\t\tparent.resize(size+1);\n\t\tv.resize(size+1);\n\t\te.resize(size+1);\n\t\tinit();\n\t}\n\tvoid init(){\n\t\tfor(int i = 0; i <= size; i++){\n\t\t\tparent[i] = i;\n\t\t\tv[i] = 1, e[i] = 0;\n\t\t}\n\t}\n\tint root(int i){\n\t\tif(parent[i] == i) return i;\n\t\treturn parent[i] = root(parent[i]);\n\t}\n\tbool same(int i, int j){\n\t\treturn root(i) == root(j);\n\t}\n\tvoid merge(int i, int j){ // j will become new root\n\t\tparent[i] = j;\n\t\tv[j] += v[i];\n\t\te[j] += e[i] + 1;\n\t}\n\tvoid unite(int i, int j){\n\t\tint root_i = root(i), root_j = root(j);\n\t\tif(root_i == root_j){\n\t\t\te[root_i]++;\n\t\t\treturn;\n\t\t}\n\t\tmerge(root_i, root_j);\n\t}\n};\n\nint n;\nP p[55];\nint lane[55], pos[55];\nbool check[55];\nbool goal[55];\nUnionFind uf(55);\nconst int T = 500*300;\n\n\nint main(void)\n{\n\tios::sync_with_stdio(0);\n\tcin.tie(0);\n\t\n\twhile(1){\n\t\tcin >> n;\n\t\tif(n == 0) break;\n\t\t\n\t\tint t, c; vector<P> vec;\n\t\trep(i, 1, n){\n\t\t\tcin >> t >> c;\n\t\t\tvec.push_back(P(t, c));\n\t\t}\n\t\tsort(all(vec));\n\t\treverse(all(vec));\n\t\t\n\t\tvector<P> vec2; ll pre = -1;\n\t\tfor(auto p : vec){\n\t\t\tif(pre != p.first) vec2.push_back(p);\n\t\t\tpre = p.first;\n\t\t}\n\t\tn = vec2.size();\n\t\trep(i, 1, n) p[i] = vec2[i-1];\n\t\t\n\t\tuf.init();\n\t\trep(i, 1, n) lane[i] = pos[i] = 0, goal[i] = false;\n\t\t\n\t\trep(t, 1, T){\n\t\t\trep(i, 1, n){\n\t\t\t\tif(goal[i]) continue;\n\t\t\t\trep(j, 1, n){\n\t\t\t\t\tif(i <= j || goal[j] || lane[i] != lane[j] || uf.parent[i] != i || uf.parent[j] != j) continue;\n\t\t\t\t\tint ti = p[i].first, tj = p[j].first;\n\t\t\t\t\tif(pos[i]*tj < pos[j]*ti && (pos[i]+1)*tj >= (pos[j]+1)*ti) uf.unite(i, j);\n\t\t\t\t}\n\t\t\t}\n\t\t\t\n\t\t\t\n\t\t\trep(i, 1, n) check[i] = false;\n\t\t\trep(i, 1, n){\n\t\t\t\tuf.root(i);\n\t\t\t\tif(uf.parent[i] == i){\n\t\t\t\t\tpos[i]++;\n\t\t\t\t\tif(pos[i] == p[i].first) check[i] = true;\n\t\t\t\t}\n\t\t\t}\n\t\t\t\n\t\t\trep(i, 1, n){\n\t\t\t\tif(goal[i]) continue;\n\t\t\t\tif(check[uf.root(i)]){\n\t\t\t\t\tif(lane[i] == 1) p[i].second--;\n\t\t\t\t\tif(p[i].second == 0) goal[i] = true;\n\t\t\t\t\tpos[i] = 0, lane[i] = 1 - lane[i];\n\t\t\t\t\tuf.parent[i] = i;\n\t\t\t\t}\n\t\t\t}\n\t\t\t\n\t\t\tbool flag = true;\n\t\t\trep(i, 1, n) flag &= goal[i];\n\t\t\tif(flag){\n\t\t\t\toutl(t);\n\t\t\t\tbreak;\n\t\t\t}\n\t\t}\n\t\t\n\t}\n\t\n\tflush(cout);\n\treturn 0;\n}", "accuracy": 1, "time_ms": 7360, "memory_kb": 3188, "score_of_the_acc": -1, "final_rank": 19 }, { "submission_id": "aoj_1297_4997687", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\nusing ll = long long;\nusing PII = pair<ll, ll>;\n\n#define FOR(i, a, n) for (ll i = (ll)a; i < (ll)n; ++i)\n#define REP(i, n) FOR(i, 0, n)\n#define ALL(x) x.begin(), x.end()\ntemplate <typename T> void chmin(T &a, const T &b) { a = min(a, b); }\ntemplate <typename T> void chmax(T &a, const T &b) { a = max(a, b); }\n\nconst ll INF = 1LL << 60;\n\nstruct FastIO {\n FastIO() {\n cin.tie(0);\n ios::sync_with_stdio(0);\n }\n} fastiofastio;\n\nint minT[50][501];\n\nint main() {\n while (1) {\n int n;\n cin >> n;\n if (n == 0)\n break;\n vector<PII> vec;\n for (int i = 0; i < n; i++) {\n int t, c;\n cin >> t >> c;\n vec.emplace_back(t, c);\n }\n sort(ALL(vec));\n memset(minT, -1, sizeof(minT));\n for (int i = n - 1; i >= 0; i--) {\n int t = vec[i].first;\n int c = vec[i].second;\n minT[i][0] = 0;\n for (int j = 0; j < 2 * c; j++) {\n minT[i][j + 1] = minT[i][j] + t;\n for (int k = n - 1; k > i; k--) {\n for (int l = j - 2; l >= 0; l -= 2) {\n if (minT[k][l] < minT[i][j] &&\n minT[i][j + 1] < minT[k][l + 1]) {\n minT[i][j + 1] = minT[k][l + 1];\n break;\n }\n }\n }\n }\n }\n int ans = -1;\n for (int i = 0; i < n; i++) {\n ans = max(ans, minT[i][2 * vec[i].second]);\n }\n cout << ans << endl;\n }\n}", "accuracy": 1, "time_ms": 940, "memory_kb": 3252, "score_of_the_acc": -0.1551, "final_rank": 15 }, { "submission_id": "aoj_1297_4977137", "code_snippet": "#include <bits/stdc++.h>\n\nusing namespace std;\n\ntypedef long long ll;\ntypedef pair<int, int> P;\n\n#define eps 1e-9\n#define INF 2000000000 // 2e9\n#define LLINF 2000000000000000000ll // 2e18 (llmax:9e18)\n#define all(x) (x).begin(), (x).end()\n#define sq(x) ((x) * (x))\n\n#ifndef LOCAL\n#define dmp(...) ;\n#else\n#define dmp(...) \\\n cerr << \"[ \" << #__VA_ARGS__ << \" ] : \" << dump_str(__VA_ARGS__) << endl\n#endif\n\n// ---------------- Utility ------------------\n\ntemplate <class T>\nbool chmin(T &a, const T &b) {\n if (a <= b) return false;\n a = b;\n return true;\n}\ntemplate <class T>\nbool chmax(T &a, const T &b) {\n if (a >= b) return false;\n a = b;\n return true;\n}\n\ntemplate <class T>\nusing MaxHeap = priority_queue<T>;\ntemplate <class T>\nusing MinHeap = priority_queue<T, vector<T>, greater<T>>;\n\ntemplate <class T>\nvector<T> vect(int len, T elem) {\n return vector<T>(len, elem);\n}\n\n// ----------------- Input -------------------\n\ntemplate <class T, class U>\nistream &operator>>(istream &is, pair<T, U> &p) {\n is >> p.first >> p.second;\n return is;\n}\n\ntemplate <class T>\nistream &operator>>(istream &is, vector<T> &vec) {\n for (int i = 0; i < vec.size(); i++) is >> vec[i];\n return is;\n}\n\n// ----------------- Output ------------------\n\ntemplate <class T, class U>\nostream &operator<<(ostream &os, const pair<T, U> &p) {\n os << p.first << ',' << p.second;\n return os;\n}\n\ntemplate <class T>\nostream &operator<<(ostream &os, const vector<T> &v) {\n for (const T &e : v) os << e << \" \";\n return os;\n}\n\ntemplate <class T>\nostream &operator<<(ostream &os, const deque<T> &d) {\n for (const T &e : d) os << e << \" \";\n return os;\n}\n\ntemplate <class T>\nostream &operator<<(ostream &os, const set<T> &s) {\n os << \"{ \";\n for (const T &e : s) os << e << \" \";\n os << \"}\";\n return os;\n}\n\ntemplate <class T>\nostream &operator<<(ostream &os, const multiset<T> &s) {\n os << \"{ \";\n for (const T &e : s) os << e << \" \";\n os << \"}\";\n return os;\n}\n\ntemplate <class T, class U>\nostream &operator<<(ostream &os, const map<T, U> &m) {\n os << \"{ \";\n for (const auto &[key, val] : m) os << \"( \" << key << \" -> \" << val << \" ) \";\n os << \"}\";\n return os;\n}\n\nvoid dump_str_rec(ostringstream &) {}\n\ntemplate <class Head, class... Tail>\nvoid dump_str_rec(ostringstream &oss, Head &&head, Tail &&... tail) {\n oss << \", \" << head;\n dump_str_rec(oss, forward<Tail>(tail)...);\n}\n\ntemplate <class T, class... U>\nstring dump_str(T &&arg, U &&... args) {\n ostringstream oss;\n oss << arg;\n dump_str_rec(oss, forward<T>(args)...);\n return oss.str();\n}\n\n// --------------- Fast I/O ------------------\n\nvoid fastio() {\n cin.tie(0);\n ios::sync_with_stdio(0);\n cout << fixed << setprecision(20);\n}\n\n// ------------ End of template --------------\n\n#define endl \"\\n\"\n\nbool solve() {\n int n;\n cin >> n;\n if (n == 0) return false;\n vector<P> a(n);\n for (int i = 0; i < n; i++) cin >> a[i];\n sort(all(a));\n vector<int> lap(n, 0), start(n, 0);\n deque<int> f, b;\n for (int i = 0; i < n; i++) f.push_back(i);\n int ans = 0;\n while (!f.empty() || !b.empty()) {\n vector<int> fts;\n for (auto id : f) { fts.push_back(start[id] + a[id].first); }\n for (int i = 1; i < fts.size(); i++) chmax(fts[i], fts[i - 1]);\n\n vector<int> bts;\n for (auto id : b) { bts.push_back(start[id] + a[id].first); }\n for (int i = 1; i < bts.size(); i++) chmax(bts[i], bts[i - 1]);\n\n int nxt = -1;\n set<P> tob, tof;\n if (f.size() > 0 && (b.empty() || fts.front() <= bts.front())) {\n nxt = fts.front();\n for (int j = 0; j < f.size(); j++) {\n int id = f[j];\n if (fts[j] == nxt) {\n start[id] = nxt;\n tob.insert(P(a[id].first, id));\n }\n }\n }\n\n if (b.size() > 0 && (f.empty() || bts.front() <= fts.front())) {\n if (nxt != -1) assert(nxt == bts.front());\n nxt = bts.front();\n for (int j = 0; j < b.size(); j++) {\n int id = b[j];\n if (bts[j] == nxt) {\n start[id] = nxt;\n lap[id]++;\n tof.insert(P(a[id].first, id));\n }\n }\n }\n\n for (auto [_, id] : tob) {\n b.push_back(id);\n f.pop_front();\n }\n\n for (auto [_, id] : tof) {\n if (lap[id] < a[id].second) f.push_back(id);\n b.pop_front();\n }\n\n assert(nxt != -1);\n ans = nxt;\n // dmp(ans);\n // dmp(f);\n // dmp(b);\n // dmp(start);\n }\n cout << ans << endl;\n return true;\n}\n\nint main() {\n fastio();\n while (solve()) {}\n // int t;\n // cin >> t;\n // while (t--) solve();\n\n // int t; cin >> t;\n // for(int i=1;i<=t;i++){\n // cout << \"Case #\" << i << \": \";\n // solve();\n // }\n return 0;\n}", "accuracy": 1, "time_ms": 100, "memory_kb": 3364, "score_of_the_acc": -0.0908, "final_rank": 10 } ]
aoj_1303_cpp
Problem I: Hobby on Rails ICPC (International Connecting Points Company) starts to sell a new railway toy. It consists of a toy tramcar and many rail units on square frames of the same size. There are four types of rail units, namely, straight (S), curve (C), left-switch (L) and right-switch (R) as shown in Figure 9. A switch has three ends, namely, branch/merge-end (B/M-end), straight-end (S-end) and curve-end (C-end). A switch is either in "through" or "branching" state. When the tramcar comes from B/M-end, and if the switch is in the through-state, the tramcar goes through to S-end and the state changes to branching; if the switch is in the branching-state, it branches toward C-end and the state changes to through. When the tramcar comes from S-end or C-end, it goes out from B/M-end regardless of the state. The state does not change in this case. Kids are given rail units of various types that fill a rectangle area of w × h , as shown in Fig- ure 10(a). Rail units meeting at an edge of adjacent two frames are automatically connected. Each rail unit may be independently rotated around the center of its frame by multiples of 90 degrees in order to change the connection of rail units, but its position cannot be changed. Kids should make "valid" layouts by rotating each rail unit, such as getting Figure 10(b) from Figure 10(a). A layout is valid when all rails at three ends of every switch are directly or indirectly connected to an end of another switch or itself. A layout in Figure 10(c) is invalid as well as Figure 10(a). Invalid layouts are frowned upon. When a tramcar runs in a valid layout, it will eventually begin to repeat the same route forever. That is, we will eventually find the tramcar periodically comes to the same running condition, which is a triple of the tramcar position in the rectangle area, its direction, and the set of the states of all the switches. A periodical route is a sequence of rail units on which the tramcar starts from a rail unit with a running condition and returns to the same rail unit with the same running condition for the first time. A periodical route through a switch or switches is called the "fun route", since kids like the rattling sound the tramcar makes when it passes through a switch. The tramcar takes the same unit time to go through a rail unit, not depending on the types of the unit or the tramcar directions. After the tramcar starts on a rail unit on a “fun route”, it will come back to the same unit with the same running condition, sooner or later. The fun time T of a fun route is the number of time units that the tramcar takes for going around the route. Of course, kids better enjoy layouts with longer fun time. Given a variety of rail units placed on a rectangular area, your job is to rotate the given rail units appropriately and to find the fun route with the longest fun time in the valid layouts. For example, there is a fun route in Figure 10(b). Its fun time is 24. Let the toy tramcar start from ...(truncated)
[ { "submission_id": "aoj_1303_4937474", "code_snippet": "#include<iostream>\n#include<cassert>\nusing namespace std;\n#define REP(i,b,n) for(int i=b;i<n;i++)\n#define rep(i,n) REP(i,0,n)\n\n#define N 0\n#define E 1\n#define S 2\n#define W 3\n\nint dx[]={0,1,0,-1};\nint dy[]={-1,0,1,0};\nint curdir[]={S,W,N,E};\n\nint sd[2][4]={//S default:0 -\n -1,W,-1,E,\n S,-1,N,-1,\n};\n\nint cd[4][4]={//C default:0 -^\n W,-1,-1,N,\n E,N,-1,-1,\n -1,S,E,-1,\n -1,-1,W,S,\n};\n\nint ld[4][2][4]={//if switch == 0 ld[i][0], switch == 1 ld[i][1] \n //ST //CURVE\n W,W,-1,E, W,W,-1,N,\n S,N,N,-1, E,N,N,-1,\n -1,W,E,E, -1,S,E,E,\n S,-1,N,S, S,-1,W,S\n /*\n {W,W,-1,E},{W,W,-1,N},\n {S,N,N,-1},{E,N,N,-1},\n {-1,W,E,E},{-1,S,E,E},\n {S,-1,N,S},{S,-1,W,S}\n */\n};\n\n\nint rd[4][2][4]={//if switch == 0 ld[i][0], switch == 1 ld[i][1] \n //ST //CURVE\n -1,W,W,E, -1,W,W,S,\n S,-1,N,N, W,-1,N,N,\n E,W,-1,E, E,N,-1,E,\n S,S,N,-1, S,S,E,-1\n /*\n {-1,W,W,E},{-1,W,W,S},\n {S,-1,N,N},{W,-1,N,N},\n {E,W,-1,E},{E,N,-1,E},\n {S,S,N,-1},{S,S,E,-1}\n */\n};\n\nint switchid[6][6];\nint rot[6][6];\nchar m[6][6];\n\nint cost[6][6][4][(1<<6)];\nint visited[6][6][4][(1<<6)];\nint ans;\n\nvoid dfs(int r,int c,int y,int x,int d,int state,int visnow,int length){\n if (length != 0)d=curdir[d];\n\n if (x == -1||y == -1||x==c||y==r)return;\n\n if (visited[y][x][d][state] == -1){\n \n }else if (visited[y][x][d][state] == visnow){\n ans=max(ans,length-cost[y][x][d][state]);\n return;\n }else {\n return;\n }\n visited[y][x][d][state]=visnow;\n cost[y][x][d][state] =length;\n\n int nextd;\n int curgrid=rot[y][x];\n if (m[y][x] == 'S'){\n nextd=sd[curgrid][d];\n dfs(r,c,y+dy[nextd],x+dx[nextd],nextd,state,visnow,length+1);\n }else if( m[y][x] == 'C'){\n nextd=cd[curgrid][d];\n dfs(r,c,y+dy[nextd],x+dx[nextd],nextd,state,visnow,length+1);\n }else if (m[y][x] == 'L'){\n bool isswitch=(((1<<switchid[y][x])&state)!=0);\n nextd=ld[curgrid][isswitch][d];\n if ((rot[y][x]+3)%4 == d){\n if (isswitch)state-=(1<<switchid[y][x]);\n else state+=(1<<switchid[y][x]);\n }\n dfs(r,c,y+dy[nextd],x+dx[nextd],nextd,state,visnow,length+1);\n }else if (m[y][x] == 'R'){\n bool isswitch=(((1<<switchid[y][x])&state)!=0);\n nextd=rd[curgrid][isswitch][d];\n if ((rot[y][x]+3)%4==d){\n if (isswitch)state-=(1<<switchid[y][x]);\n else state+=(1<<switchid[y][x]);\n }\n dfs(r,c,y+dy[nextd],x+dx[nextd],nextd,state,visnow,length+1);\n }\n}\n\nvoid solve(int r,int c,int index){\n rep(i,r){\n rep(j,c){\n rep(k,4){\n\trep(l,(1<<index)){\n\t visited[i][j][k][l]=-1;\n\t}\n }\n }\n }\n \n int visnow=0;\n rep(i,r){\n rep(j,c){\n if (m[i][j] == 'R'||m[i][j] == 'L'){\n\tif (rot[i][j] == 0){\n\t dfs(r,c,i,j,W,0,visnow,0);\n\t}else if (rot[i][j] == 1){\n\t dfs(r,c,i,j,N,0,visnow,0);\n\t}else if (rot[i][j] == 2){\n\t dfs(r,c,i,j,E,0,visnow,0);\n\t}else if (rot[i][j] == 3){\n\t dfs(r,c,i,j,S,0,visnow,0);\n\t}\n\tvisnow++;\n }\n }\n }\n}\n\n\n//if rot[i][j] == -1 unvisited\nvoid search(int r,int c,int index,int now,int num,//num -> 3 position of L or R\n\t int y,int x,int d,int *posx,int *posy,bool tonext){\n\n if (tonext && num == 0 && index == now+1){\n solve(r,c,index);\n return;\n }\n\n\n if (tonext){\n \n if (num == 0){//straight\n now++;\n y = posy[now];\n x = posx[now];\n\n d = (rot[y][x]+3)%4;\n search(r,c,index,now,1,posy[now]+dy[d],posx[now]+dx[d],\n\t d,posx,posy,false);\n }else if (num == 1){\n y = posy[now];\n x = posx[now];\n d=(rot[y][x]+1)%4;\n search(r,c,index,now,2,posy[now]+dy[d],posx[now]+dx[d],\n\t d,posx,posy,false);\n }else if (num == 2){//curve\n y = posy[now];\n x = posx[now];\n if (m[y][x] == 'R'){\n\td=(rot[y][x]+2)%4;\n }else if (m[posy[now]][posx[now]]=='L'){\n\td=rot[y][x];\n }\n search(r,c,index,now,0,posy[now]+dy[d],posx[now]+dx[d],\n\t d,posx,posy,false);\n \n }\n return;\n }\n\n if (x == -1 || y == -1||x==c||y==r)return;\n d=curdir[d];\n\n if (m[y][x] == 'S'){\n if (rot[y][x] == -1){\n if (d == E || d == W)rot[y][x]=0;\n else if (d == S || d == N)rot[y][x]=1;\n search(r,c,index,now,num,y+dy[sd[rot[y][x]][d]],x+dx[sd[rot[y][x]][d]],\n\t sd[rot[y][x]][d],posx,posy,false);\n rot[y][x]=-1;\n return;\n }else if (rot[y][x] != -1){\n if (sd[rot[y][x]][d] == -1)return;\n else search(r,c,index,now,num,-1,-1,-1,posx,posy,true);\n }\n }else if(m[y][x] == 'C'){\n if (rot[y][x] == -1){\n if (d == E)rot[y][x]=1;\n else if (d == W)rot[y][x]=3;\n else if (d == S)rot[y][x]=2;\n else if (d == N)rot[y][x]=0;\n search(r,c,index,now,num,y+dy[cd[rot[y][x]][d]],x+dx[cd[rot[y][x]][d]],\n\t cd[rot[y][x]][d],posx,posy,false);\n if (d == E)rot[y][x]=2;\n else if (d == W)rot[y][x]=0;\n else if (d == S)rot[y][x]=3;\n else if (d == N)rot[y][x]=1;\n search(r,c,index,now,num,y+dy[cd[rot[y][x]][d]],x+dx[cd[rot[y][x]][d]],\n\t cd[rot[y][x]][d],posx,posy,false);\n rot[y][x]=-1;\n }else if (rot[y][x] != -1){\n if (cd[rot[y][x]][d] == -1)return;\n else search(r,c,index,now,num,-1,-1,-1,posx,posy,true);\n }\n }else if (m[y][x] == 'L'){\n if (ld[rot[y][x]][0][d] == -1)return;\n search(r,c,index,now,num,-1,-1,-1,posx,posy,true);\n }else if (m[y][x] == 'R'){\n if (rd[rot[y][x]][0][d] == -1)return;\n search(r,c,index,now,num,-1,-1,-1,posx,posy,true);\n }\n}\n\n\nvoid decide(int r,int c,int index,int now,int *posx,int *posy){\n if (now == index){\n search(r,c,index,-1,0,-1,-1,-1,posx,posy,true);\n return;\n }\n rep(i,4){\n rot[posy[now]][posx[now]]=i;\n decide(r,c,index,now+1,posx,posy);\n }\n}\n\nmain(){\n int r,c;\n while(cin>>c>>r && c){\n int index =0;\n int posx[6],posy[6];\n rep(i,r){\n rep(j,c){\n\tcin>>m[i][j];\n\trot[i][j]=-1;\n\tif (m[i][j] == 'R'||m[i][j]=='L'){\n\t switchid[i][j]=index;\n\t posy[index]=i;\n\t posx[index]=j;\n\t index++;\n\t}\n }\n }\n\n ans = 0;\n decide(r,c,index,0,posx,posy);\n cout << ans << endl;\n }\n}", "accuracy": 1, "time_ms": 30, "memory_kb": 3136, "score_of_the_acc": -0.9304, "final_rank": 4 }, { "submission_id": "aoj_1303_4864404", "code_snippet": "#include<bits/stdc++.h>\ntypedef long long int ll;\ntypedef unsigned long long int ull;\n#define BIG_NUM 2000000000\n#define HUGE_NUM 1000000000000000000\n#define MOD 1000000007\n#define EPS 0.000000001\nusing namespace std;\n\n\n\nenum Type{\n\tS,\n\tC,\n\tL,\n\tR,\n};\n\nenum DIR{\n\tNorth,\n\tEast,\n\tWest,\n\tSouth,\n\tNone,\n};\n\nstruct LOC{\n\tLOC(){\n\n\t\trow = col = 0;\n\t}\n\tLOC(int arg_row,int arg_col){\n\t\trow = arg_row;\n\t\tcol = arg_col;\n\t}\n\tint row,col;\n};\n\n\nint W,H;\nint num_state[4] = {2,4,4,4};\nint num_pos[4] = {2,2,3,3};\nint diff_row[4] = {-1,0,0,1},diff_col[4] = {0,1,-1,0};\nint num_switch;\nint ANS;\n\n//どの方向に枝が伸びているか\nDIR dir_state[4][4][4] = {{{East,West,None,None},{North,South,None,None},{None,None,None,None},{None,None,None,None}},\n\t\t\t\t {{North,West,None,None},{West,South,None,None},{East,South,None,None},{North,East,None,None}},\n\t\t\t\t {{West,North,East,None},{North,East,South,None},{East,West,South,None},{South,North,West,None}}, //★位置0がB/M★\n\t\t\t\t {{West,East,South,None},{North,West,South,None},{East,North,West,None},{South,North,East,None}}, //★位置0がB/M★\n \t \t \t \t };\n\n//next_dir[Type][state][★方位★]\nDIR next_dir[2][4][4] = {{{None,West,East,None},{South,None,None,North},{None,None,None,None},{None,None,None,None}},\n\t\t\t\t {{West,None,North,None},{None,None,South,West},{None,South,None,East},{East,North,None,None}}\n \t \t \t \t };\n\n//next_dir_switch[Lが0,Rが1][非straightなら0,straightなら1][state][★方位★]\nDIR next_dir_switch[2][2][4][4] ={\n\t\t\t\t\t\t\t\t {//L\n\t\t\t\t\t\t\t\t { //非\n\t\t\t\t\t\t\t\t {West,West,North,None},{East,North,None,North},{None,South,East,East},{South,None,South,West}\n\t\t\t\t\t\t\t\t },\n\t\t\t\t\t\t\t\t\t{ //straight\n\t\t\t\t\t\t\t\t\t\t{West,West,East,None},{South,North,None,North},{None,West,East,East},{South,None,South,North}\n\t\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t\t },\n\t\t\t\t\t\t\t\t {//R\n\t\t\t\t\t\t\t\t\t{//非\n\t\t\t\t\t\t\t\t\t\t{None,West,South,West},{West,None,North,North},{East,North,East,None},{South,South,None,East}\n\t\t\t\t\t\t\t\t\t},\n\t\t\t\t\t\t\t\t\t{ //straight\n\t\t\t\t\t\t\t\t\t {None,West,East,West},{South,None,North,North},{East,West,East,None},{South,South,None,North}\n\t\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t\t }\n\t\t\t\t\t\t\t\t};\n\n\nDIR dir_array[5] = {North,East,West,South,None},rev_dir[5] = {South,West,East,North,None};\nint POW[7];\nint index_table[10][10];\nbool is_straight[10];\nchar table[10][10];\nLOC switch_loc[10];\n\n\n\nbool rangeCheck(int row,int col){\n\n\treturn row >= 0 && row <= H-1 && col >= 0 && col <= W-1;\n}\n\nint makeCode(){\n\n\tint ret = 0;\n\n\tfor(int i = 0; i < num_switch; i++){\n\t\tif(is_straight[i]){\n\n\t\t\tret += POW[i];\n\t\t}\n\t}\n\treturn ret;\n}\n\n\nvoid calc_ans(int STATE[6][6]){\n\n\tint TIME[6][6][4][64];\n\n\tint ans = 0;\n\n\tfor(int i = 0; i < num_switch; i++){ //始点のスイッチ\n\n\t\tint base_row = switch_loc[i].row;\n\t\tint base_col = switch_loc[i].col;\n\n\t\tType type;\n\t\tif(table[base_row][base_col] == 'L'){\n\n\t\t\ttype = L;\n\t\t}else{\n\n\t\t\ttype = R;\n\t\t}\n\n\t\tint tmp_state = STATE[base_row][base_col];\n\n\t\tDIR bm_dir = dir_state[type][tmp_state][0];\n\t\tDIR dir,pre_dir;\n\n\t\tint tmp_row,tmp_col;\n\t\tint next_row,next_col;\n\n\t\tfor(int loop = 0; loop < 2; loop++){\n\n\t\t\tfor(int k = 0; k < num_switch; k++){\n\n\t\t\t\tis_straight[k] = true;\n\t\t\t}\n\n\t\t\tfor(int row = 0; row < H; row++){\n\t\t\t\tfor(int col = 0; col < W; col++){\n\t\t\t\t\tfor(int a = 0; a < 4; a++){\n\t\t\t\t\t\tfor(int state = 0; state < POW[num_switch]; state++){\n\n\t\t\t\t\t\t\tTIME[row][col][a][state] = -1;\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\n\t\t\tif(loop == 0){//最初にbmが切り替わらない方向\n\n\t\t\t\tdir = bm_dir;\n\n\t\t\t\tswitch(bm_dir){\n\t\t\t\tcase North:\n\n\t\t\t\t\ttmp_row = base_row+1;\n\t\t\t\t\ttmp_col = base_col;\n\t\t\t\t\tbreak;\n\n\t\t\t\tcase East:\n\n\t\t\t\t\ttmp_row = base_row;\n\t\t\t\t\ttmp_col = base_col-1;\n\n\t\t\t\t\tbreak;\n\n\t\t\t\tcase West:\n\n\t\t\t\t\ttmp_row = base_row;\n\t\t\t\t\ttmp_col = base_col+1;\n\n\t\t\t\t\tbreak;\n\t\t\t\tcase South:\n\n\t\t\t\t\ttmp_row = base_row-1;\n\t\t\t\t\ttmp_col = base_col;\n\t\t\t\t\tbreak;\n\t\t\t\t}\n\n\t\t\t}else{ //最初にbmを踏む方向\n\n\t\t\t\tswitch(bm_dir){\n\t\t\t\tcase North:\n\n\t\t\t\t\ttmp_row = base_row-1;\n\t\t\t\t\ttmp_col = base_col;\n\t\t\t\t\tdir = South;\n\t\t\t\t\tbreak;\n\n\t\t\t\tcase East:\n\n\t\t\t\t\ttmp_row = base_row;\n\t\t\t\t\ttmp_col = base_col+1;\n\t\t\t\t\tdir = West;\n\n\t\t\t\t\tbreak;\n\n\t\t\t\tcase West:\n\n\t\t\t\t\ttmp_row = base_row;\n\t\t\t\t\ttmp_col = base_col-1;\n\t\t\t\t\tdir = East;\n\n\t\t\t\t\tbreak;\n\t\t\t\tcase South:\n\n\t\t\t\t\ttmp_row = base_row+1;\n\t\t\t\t\ttmp_col = base_col;\n\t\t\t\t\tdir = North;\n\t\t\t\t\tbreak;\n\t\t\t\t}\n\t\t\t}\n\n\t\t\tTIME[tmp_row][tmp_col][dir][makeCode()] = 0;\n\n\t\t\tint tmp_time = 0;\n\n\t\t\twhile(true){\n\n\t\t\t\tswitch(dir){\n\t\t\t\tcase North:\n\n\t\t\t\t\tnext_row = tmp_row-1;\n\t\t\t\t\tnext_col = tmp_col;\n\t\t\t\t\tbreak;\n\t\t\t\tcase East:\n\n\t\t\t\t\tnext_row = tmp_row;\n\t\t\t\t\tnext_col = tmp_col+1;\n\t\t\t\t\tbreak;\n\t\t\t\tcase West:\n\n\t\t\t\t\tnext_row = tmp_row;\n\t\t\t\t\tnext_col = tmp_col-1;\n\t\t\t\t\tbreak;\n\t\t\t\tcase South:\n\n\t\t\t\t\tnext_row = tmp_row+1;\n\t\t\t\t\tnext_col = tmp_col;\n\t\t\t\t\tbreak;\n\t\t\t\t}\n\n\t\t\t\tType check_type;\n\t\t\t\tswitch(table[tmp_row][tmp_col]){\n\t\t\t\tcase 'S':\n\t\t\t\t\tcheck_type = S;\n\t\t\t\t\tbreak;\n\t\t\t\tcase 'C':\n\t\t\t\t\tcheck_type = C;\n\t\t\t\t\tbreak;\n\t\t\t\tcase 'L':\n\t\t\t\t\tcheck_type = L;\n\t\t\t\t\tbreak;\n\t\t\t\tcase 'R':\n\t\t\t\t\tcheck_type = R;\n\t\t\t\t\tbreak;\n\t\t\t\t}\n\n\t\t\t\tif((table[tmp_row][tmp_col] == 'L' || table[tmp_row][tmp_col] == 'R') &&\n\t\t\t\t\t\tpre_dir == rev_dir[dir_state[check_type][STATE[tmp_row][tmp_col]][0]]){ //スイッチの切り替わり\n\n\t\t\t\t\tis_straight[index_table[tmp_row][tmp_col]] = not is_straight[index_table[tmp_row][tmp_col]];\n\t\t\t\t}\n\n\t\t\t\t++tmp_time;\n\t\t\t\tint tmp_code = makeCode();\n\t\t\t\tif(TIME[next_row][next_col][dir][tmp_code] != -1){\n\n\t\t\t\t\tans = max(ans,tmp_time-TIME[next_row][next_col][dir][tmp_code]);\n\t\t\t\t\tbreak;\n\t\t\t\t}\n\t\t\t\tTIME[next_row][next_col][dir][tmp_code] = tmp_time;\n\n\t\t\t\ttmp_row = next_row;\n\t\t\t\ttmp_col = next_col;\n\n\t\t\t\tpre_dir = dir;\n\n\t\t\t\tif(table[tmp_row][tmp_col] == 'L' || table[tmp_row][tmp_col] == 'R'){\n\n\t\t\t\t\tif(table[tmp_row][tmp_col] == 'L'){\n\n\t\t\t\t\t\tdir = next_dir_switch[0][is_straight[index_table[tmp_row][tmp_col]]][STATE[tmp_row][tmp_col]][rev_dir[dir]];\n\n\t\t\t\t\t}else{\n\n\t\t\t\t\t\tdir = next_dir_switch[1][is_straight[index_table[tmp_row][tmp_col]]][STATE[tmp_row][tmp_col]][rev_dir[dir]];\n\t\t\t\t\t}\n\n\t\t\t\t}else{\n\n\t\t\t\t\tif(table[tmp_row][tmp_col] == 'S'){\n\n\t\t\t\t\t\tdir = next_dir[S][STATE[tmp_row][tmp_col]][rev_dir[dir]];\n\n\t\t\t\t\t}else{\n\n\t\t\t\t\t\tdir = next_dir[C][STATE[tmp_row][tmp_col]][rev_dir[dir]];\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n\n\tANS = max(ANS,ans);\n}\n\n\n\nvoid recursive(int base_row,int base_col,bool MUST[6][6],int STATE[6][6],bool check[6][6][4]){\n\n\tbool FLG = true;\n\n\t//埋めるべきマスが全て埋まったか調べる\n\tfor(int row = 0; row < H; row++){\n\t\tfor(int col = 0; col < W; col++){\n\t\t\tif(MUST[row][col] && STATE[row][col] == -1){\n\n\t\t\t\tFLG = false;\n\t\t\t\tbreak;\n\t\t\t}\n\t\t}\n\t\tif(!FLG)break;\n\t}\n\n\tif(FLG){\n\t\tcalc_ans(STATE);\n\t\treturn;\n\t}\n\n\tint next_row = base_row,next_col = base_col+1;\n\tif(base_col == W-1){\n\n\t\tif(base_row == H-1){\n\n\t\t\tnext_row = 0;\n\n\t\t}else{\n\n\t\t\tnext_row = base_row+1;\n\t\t}\n\n\t\tnext_col = 0;\n\n\t}\n\n\tif(!MUST[base_row][base_col] || STATE[base_row][base_col] != -1){ //現時点では決める必要なし、または決定済\n\n\t\trecursive(next_row,next_col,MUST,STATE,check);\n\t\treturn;\n\t}\n\n\tType type;\n\tswitch(table[base_row][base_col]){\n\tcase 'S':\n\t\ttype = S;\n\t\tbreak;\n\tcase 'C':\n\t\ttype = C;\n\t\tbreak;\n\tcase 'L':\n\t\ttype = L;\n\t\tbreak;\n\tcase 'R':\n\t\ttype = R;\n\t\tbreak;\n\t}\n\tvector<LOC> ADD;\n\n\tfor(int i = 0; i < num_state[type]; i++){\n\n\t\tbool next_MUST[6][6];\n\t\tint next_STATE[6][6];\n\t\tbool next_check[6][6][4];\n\n\t\t//★★★★★仮引数の配列の値をいじる場合は、その都度使い捨てにしないとバグる★★★★★\n\t\tfor(int row = 0; row < H; row++){\n\t\t\tfor(int col = 0; col < W; col++){\n\t\t\t\tnext_MUST[row][col] = MUST[row][col];\n\t\t\t\tnext_STATE[row][col] = STATE[row][col];\n\t\t\t\tfor(int a = 0; a < 4; a++){\n\n\t\t\t\t\tnext_check[row][col][a] = check[row][col][a];\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\n\t\tfor(int k = 0; k < 4; k++){\n\n\t\t\tnext_check[base_row][base_col][k] = false;\n\t\t}\n\n\t\tfor(int k = 0; k < num_pos[type]; k++){\n\n\t\t\tnext_check[base_row][base_col][dir_state[type][i][k]] = true;\n\t\t}\n\n\t\tbool tmp_FLG = true;\n\t\tADD.clear();\n\n\t\t//自分に向かう辺のチェック(相手の枝が伸びてるのに自分がfalseなら不可)\n\t\tfor(int k = 0; k < 4; k++){\n\n\t\t\tint adj_row = base_row+diff_row[k];\n\t\t\tint adj_col = base_col+diff_col[k];\n\n\t\t\tif(!rangeCheck(adj_row,adj_col)||next_STATE[adj_row][adj_col] == -1)continue;\n\n\t\t\tif(next_check[adj_row][adj_col][rev_dir[dir_array[k]]] && !next_check[base_row][base_col][dir_array[k]]){\n\n\t\t\t\ttmp_FLG = false;\n\t\t\t\tbreak;\n\t\t\t}\n\t\t}\n\t\tif(!tmp_FLG)continue;\n\n\t\t//自分から向かう辺のチェック(自分の枝が相手に伸びてるのに相手がfalseなら不可)\n\t\tfor(int k = 0; k < 4; k++){\n\n\t\t\tif(!next_check[base_row][base_col][k])continue;\n\n\t\t\tint adj_row = base_row+diff_row[k];\n\t\t\tint adj_col = base_col+diff_col[k];\n\n\t\t\tif(!rangeCheck(adj_row,adj_col)){ //外に枝が向いていたら不可\n\n\t\t\t\ttmp_FLG = false;\n\t\t\t\tbreak;\n\t\t\t}\n\t\t\tif(next_STATE[adj_row][adj_col] == -1){ //新たにMUSTに加える\n\n\t\t\t\tADD.push_back(LOC(adj_row,adj_col));\n\t\t\t\tcontinue;\n\t\t\t}\n\n\t\t\tif(next_check[base_row][base_col][dir_array[k]] && !next_check[adj_row][adj_col][rev_dir[dir_array[k]]]){\n\n\t\t\t\ttmp_FLG = false;\n\t\t\t\tbreak;\n\t\t\t}\n\t\t}\n\t\tif(!tmp_FLG)continue;\n\n\n\t\tnext_STATE[base_row][base_col] = i;\n\t\tfor(int a = 0; a < ADD.size(); a++){\n\n\t\t\tnext_MUST[ADD[a].row][ADD[a].col] = true;\n\t\t}\n\n\t\trecursive(next_row,next_col,next_MUST,next_STATE,next_check);\n\t}\n}\n\n\nvoid func(){\n\n\tnum_switch = 0;\n\tchar buf[2];\n\n\tfor(int row = 0; row < H; row++){\n\t\tfor(int col = 0; col < W; col++){\n\t\t\tscanf(\"%s\",buf);\n\t\t\ttable[row][col] = buf[0];\n\t\t\tif(buf[0] == 'L' || buf[0] == 'R'){\n\n\t\t\t\tswitch_loc[num_switch].row = row;\n\t\t\t\tswitch_loc[num_switch].col = col;\n\t\t\t\tindex_table[row][col] = num_switch++;\n\t\t\t}\n\t\t}\n\t}\n\n\tANS = 0;\n\n\tint first_STATE[6][6];\n\tbool first_MUST[6][6],first_check[6][6][4];\n\n\tfor(int row = 0; row < H; row++){\n\t\tfor(int col = 0; col < W; col++){\n\t\t\tfirst_STATE[row][col] = -1;\n\t\t\tfirst_MUST[row][col] = false;\n\t\t\tfor(int a = 0; a < 4; a++){\n\n\t\t\t\tfirst_check[row][col][a] = false;\n\t\t\t}\n\t\t}\n\t}\n\n\tfor(int i = 0; i < num_switch; i++){\n\n\t\tfirst_MUST[switch_loc[i].row][switch_loc[i].col] = true;\n\t}\n\n\trecursive(0,0,first_MUST,first_STATE,first_check);\n\n\tprintf(\"%d\\n\",ANS);\n\treturn;\n}\n\n\nint main(){\n\n\tPOW[0] = 1;\n\tfor(int i = 1; i <= 6; i++){\n\n\t\tPOW[i] = POW[i-1]*2;\n\t}\n\n\twhile(true){\n\t\tscanf(\"%d %d\",&W,&H);\n\t\tif(W == 0 && H == 0)break;\n\n\t\tfunc();\n\t}\n\n\treturn 0;\n}", "accuracy": 1, "time_ms": 60, "memory_kb": 3288, "score_of_the_acc": -1.3282, "final_rank": 6 }, { "submission_id": "aoj_1303_4864397", "code_snippet": "#include<bits/stdc++.h>\ntypedef long long int ll;\ntypedef unsigned long long int ull;\n#define BIG_NUM 2000000000\n#define HUGE_NUM 1000000000000000000\n#define MOD 1000000007\n#define EPS 0.000000001\nusing namespace std;\n\n\n\nenum Type{\n\tS,\n\tC,\n\tL,\n\tR,\n};\n\nenum DIR{\n\tNorth,\n\tEast,\n\tWest,\n\tSouth,\n\tNone,\n};\n\nstruct LOC{\n\tLOC(){\n\n\t\trow = col = 0;\n\t}\n\tLOC(int arg_row,int arg_col){\n\t\trow = arg_row;\n\t\tcol = arg_col;\n\t}\n\tint row,col;\n};\n\n\nint W,H;\nint num_state[4] = {2,4,4,4};\nint num_pos[4] = {2,2,3,3};\nint diff_row[4] = {-1,0,0,1},diff_col[4] = {0,1,-1,0};\nint num_switch;\nint ANS;\n\n//どの方向に枝が伸びているか\nDIR dir_state[4][4][4] = {{{East,West,None,None},{North,South,None,None},{None,None,None,None},{None,None,None,None}},\n\t\t\t\t {{North,West,None,None},{West,South,None,None},{East,South,None,None},{North,East,None,None}},\n\t\t\t\t {{West,North,East,None},{North,East,South,None},{East,West,South,None},{South,North,West,None}}, //★位置0がB/M★\n\t\t\t\t {{West,East,South,None},{North,West,South,None},{East,North,West,None},{South,North,East,None}}, //★位置0がB/M★\n \t \t \t \t };\n\n//next_dir[Type][state][★方位★]\nDIR next_dir[2][4][4] = {{{None,West,East,None},{South,None,None,North},{None,None,None,None},{None,None,None,None}},\n\t\t\t\t {{West,None,North,None},{None,None,South,West},{None,South,None,East},{East,North,None,None}} //★位置0がB/M★\n \t \t \t \t };\n\n//next_dir_switch[Lが0,Rが1][非straightなら0,straightなら1][state][★方位★]\nDIR next_dir_switch[2][2][4][4] ={\n\t\t\t\t\t\t\t\t {//L\n\t\t\t\t\t\t\t\t { //非\n\t\t\t\t\t\t\t\t {West,West,North,None},{East,North,None,North},{None,South,East,East},{South,None,South,West}\n\t\t\t\t\t\t\t\t },\n\t\t\t\t\t\t\t\t\t{ //straight\n\t\t\t\t\t\t\t\t\t\t{West,West,East,None},{South,North,None,North},{None,West,East,East},{South,None,South,North}\n\t\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t\t },\n\t\t\t\t\t\t\t\t {//R\n\t\t\t\t\t\t\t\t\t{//非\n\t\t\t\t\t\t\t\t\t\t{None,West,South,West},{West,None,North,North},{East,North,East,None},{South,South,None,East}\n\t\t\t\t\t\t\t\t\t},\n\t\t\t\t\t\t\t\t\t{ //straight\n\t\t\t\t\t\t\t\t\t {None,West,East,West},{South,None,North,North},{East,West,East,None},{South,South,None,North}\n\t\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t\t }\n\t\t\t\t\t\t\t\t};\n\n\nDIR dir_array[5] = {North,East,West,South,None},rev_dir[5] = {South,West,East,North,None};\nint POW[7];\nint index_table[10][10];\nbool is_straight[10];\nchar table[10][10];\nLOC switch_loc[10];\n\n\n\nbool rangeCheck(int row,int col){\n\n\treturn row >= 0 && row <= H-1 && col >= 0 && col <= W-1;\n}\n\nint makeCode(){\n\n\tint ret = 0;\n\n\tfor(int i = 0; i < num_switch; i++){\n\t\tif(is_straight[i]){\n\n\t\t\tret += POW[i];\n\t\t}\n\t}\n\treturn ret;\n}\n\n\nvoid calc_ans(int STATE[6][6]){\n\n\tint TIME[6][6][4][64];\n\n\tint ans = 0;\n\n\tfor(int i = 0; i < num_switch; i++){ //始点のスイッチ\n\n\t\tint base_row = switch_loc[i].row;\n\t\tint base_col = switch_loc[i].col;\n\n\t\tType type;\n\t\tif(table[base_row][base_col] == 'L'){\n\n\t\t\ttype = L;\n\t\t}else{\n\n\t\t\ttype = R;\n\t\t}\n\n\t\tint tmp_state = STATE[base_row][base_col];\n\n\t\tDIR bm_dir = dir_state[type][tmp_state][0];\n\t\tDIR dir,pre_dir;\n\n\t\tint tmp_row,tmp_col;\n\t\tint next_row,next_col;\n\n\t\tfor(int loop = 0; loop < 2; loop++){\n\n\t\t\tfor(int k = 0; k < num_switch; k++){\n\n\t\t\t\tis_straight[k] = true;\n\t\t\t}\n\n\t\t\tfor(int row = 0; row < H; row++){\n\t\t\t\tfor(int col = 0; col < W; col++){\n\t\t\t\t\tfor(int a = 0; a < 4; a++){\n\t\t\t\t\t\tfor(int state = 0; state < POW[num_switch]; state++){\n\n\t\t\t\t\t\t\tTIME[row][col][a][state] = -1;\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\n\t\t\tif(loop == 0){//最初にbmが切り替わらない方向\n\n\t\t\t\tdir = bm_dir;\n\n\t\t\t\tswitch(bm_dir){\n\t\t\t\tcase North:\n\n\t\t\t\t\ttmp_row = base_row+1;\n\t\t\t\t\ttmp_col = base_col;\n\t\t\t\t\tbreak;\n\n\t\t\t\tcase East:\n\n\t\t\t\t\ttmp_row = base_row;\n\t\t\t\t\ttmp_col = base_col-1;\n\n\t\t\t\t\tbreak;\n\n\t\t\t\tcase West:\n\n\t\t\t\t\ttmp_row = base_row;\n\t\t\t\t\ttmp_col = base_col+1;\n\n\t\t\t\t\tbreak;\n\t\t\t\tcase South:\n\n\t\t\t\t\ttmp_row = base_row-1;\n\t\t\t\t\ttmp_col = base_col;\n\t\t\t\t\tbreak;\n\t\t\t\t}\n\n\t\t\t}else{ //最初にbmを踏む方向\n\n\t\t\t\tswitch(bm_dir){\n\t\t\t\tcase North:\n\n\t\t\t\t\ttmp_row = base_row-1;\n\t\t\t\t\ttmp_col = base_col;\n\t\t\t\t\tdir = South;\n\t\t\t\t\tbreak;\n\n\t\t\t\tcase East:\n\n\t\t\t\t\ttmp_row = base_row;\n\t\t\t\t\ttmp_col = base_col+1;\n\t\t\t\t\tdir = West;\n\n\t\t\t\t\tbreak;\n\n\t\t\t\tcase West:\n\n\t\t\t\t\ttmp_row = base_row;\n\t\t\t\t\ttmp_col = base_col-1;\n\t\t\t\t\tdir = East;\n\n\t\t\t\t\tbreak;\n\t\t\t\tcase South:\n\n\t\t\t\t\ttmp_row = base_row+1;\n\t\t\t\t\ttmp_col = base_col;\n\t\t\t\t\tdir = North;\n\t\t\t\t\tbreak;\n\t\t\t\t}\n\t\t\t}\n\n\t\t\tTIME[tmp_row][tmp_col][dir][makeCode()] = 0;\n\n\t\t\tint tmp_time = 0;\n\n\t\t\twhile(true){\n\n\t\t\t\tswitch(dir){\n\t\t\t\tcase North:\n\t\t\t\t\t//printf(\"北\\n\");\n\t\t\t\t\tnext_row = tmp_row-1;\n\t\t\t\t\tnext_col = tmp_col;\n\t\t\t\t\tbreak;\n\t\t\t\tcase East:\n\t\t\t\t\t//printf(\"東\\n\");\n\t\t\t\t\tnext_row = tmp_row;\n\t\t\t\t\tnext_col = tmp_col+1;\n\t\t\t\t\tbreak;\n\t\t\t\tcase West:\n\t\t\t\t\t//printf(\"西\\n\");\n\t\t\t\t\tnext_row = tmp_row;\n\t\t\t\t\tnext_col = tmp_col-1;\n\t\t\t\t\tbreak;\n\t\t\t\tcase South:\n\t\t\t\t\t//printf(\"南\\n\");\n\t\t\t\t\tnext_row = tmp_row+1;\n\t\t\t\t\tnext_col = tmp_col;\n\t\t\t\t\tbreak;\n\t\t\t\t}\n\n\t\t\t\tType check_type;\n\t\t\t\tswitch(table[tmp_row][tmp_col]){\n\t\t\t\tcase 'S':\n\t\t\t\t\tcheck_type = S;\n\t\t\t\t\tbreak;\n\t\t\t\tcase 'C':\n\t\t\t\t\tcheck_type = C;\n\t\t\t\t\tbreak;\n\t\t\t\tcase 'L':\n\t\t\t\t\tcheck_type = L;\n\t\t\t\t\tbreak;\n\t\t\t\tcase 'R':\n\t\t\t\t\tcheck_type = R;\n\t\t\t\t\tbreak;\n\t\t\t\t}\n\n\t\t\t\tif((table[tmp_row][tmp_col] == 'L' || table[tmp_row][tmp_col] == 'R') &&\n\t\t\t\t\t\tpre_dir == rev_dir[dir_state[check_type][STATE[tmp_row][tmp_col]][0]]){ //スイッチの切り替わり\n\n\t\t\t\t\t//printf(\"スイッチ切り替わり\\n\");\n\t\t\t\t\tis_straight[index_table[tmp_row][tmp_col]] = not is_straight[index_table[tmp_row][tmp_col]];\n\t\t\t\t}\n\n\t\t\t\t++tmp_time;\n\t\t\t\tint tmp_code = makeCode();\n\t\t\t\tif(TIME[next_row][next_col][dir][tmp_code] != -1){\n\n\t\t\t\t\t//printf(\"ループ next_row:%d next_col:%d\\n\",next_row,next_col);\n\n\t\t\t\t\tans = max(ans,tmp_time-TIME[next_row][next_col][dir][tmp_code]);\n\t\t\t\t\tbreak;\n\t\t\t\t}\n\t\t\t\tTIME[next_row][next_col][dir][tmp_code] = tmp_time;\n\n\t\t\t\ttmp_row = next_row;\n\t\t\t\ttmp_col = next_col;\n\n\t\t\t\tpre_dir = dir;\n\n\t\t\t\tif(table[tmp_row][tmp_col] == 'L' || table[tmp_row][tmp_col] == 'R'){\n\n\t\t\t\t\tif(table[tmp_row][tmp_col] == 'L'){\n\n\t\t\t\t\t\tdir = next_dir_switch[0][is_straight[index_table[tmp_row][tmp_col]]][STATE[tmp_row][tmp_col]][rev_dir[dir]];\n\n\t\t\t\t\t}else{\n\n\t\t\t\t\t\tdir = next_dir_switch[1][is_straight[index_table[tmp_row][tmp_col]]][STATE[tmp_row][tmp_col]][rev_dir[dir]];\n\t\t\t\t\t}\n\n\t\t\t\t}else{\n\n\t\t\t\t\tif(table[tmp_row][tmp_col] == 'S'){\n\n\t\t\t\t\t\tdir = next_dir[S][STATE[tmp_row][tmp_col]][rev_dir[dir]];\n\n\t\t\t\t\t}else{\n\n\t\t\t\t\t\tdir = next_dir[C][STATE[tmp_row][tmp_col]][rev_dir[dir]];\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n\n\tANS = max(ANS,ans);\n}\n\n\n\nvoid recursive(int base_row,int base_col,bool MUST[6][6],int STATE[6][6],bool check[6][6][4]){\n\n\tbool FLG = true;\n\n\t//埋めるべきマスが全て埋まったか調べる\n\tfor(int row = 0; row < H; row++){\n\t\tfor(int col = 0; col < W; col++){\n\t\t\tif(MUST[row][col] && STATE[row][col] == -1){\n\n\t\t\t\tFLG = false;\n\t\t\t\tbreak;\n\t\t\t}\n\t\t}\n\t\tif(!FLG)break;\n\t}\n\n\tif(FLG){\n\t\tcalc_ans(STATE);\n\t\treturn;\n\t}\n\n\tint next_row = base_row,next_col = base_col+1;\n\tif(base_col == W-1){\n\n\t\tif(base_row == H-1){\n\n\t\t\tnext_row = 0;\n\n\t\t}else{\n\n\t\t\tnext_row = base_row+1;\n\t\t}\n\n\t\tnext_col = 0;\n\n\t}\n\n\tif(!MUST[base_row][base_col] || STATE[base_row][base_col] != -1){ //現時点では決める必要なし、または決定済\n\n\t\trecursive(next_row,next_col,MUST,STATE,check);\n\t\treturn;\n\t}\n\n\tType type;\n\tswitch(table[base_row][base_col]){\n\tcase 'S':\n\t\ttype = S;\n\t\tbreak;\n\tcase 'C':\n\t\ttype = C;\n\t\tbreak;\n\tcase 'L':\n\t\ttype = L;\n\t\tbreak;\n\tcase 'R':\n\t\ttype = R;\n\t\tbreak;\n\t}\n\tvector<LOC> ADD;\n\n\tfor(int i = 0; i < num_state[type]; i++){\n\n\t\tbool next_MUST[6][6];\n\t\tint next_STATE[6][6];\n\t\tbool next_check[6][6][4];\n\n\t\tfor(int row = 0; row < H; row++){\n\t\t\tfor(int col = 0; col < W; col++){\n\t\t\t\tnext_MUST[row][col] = MUST[row][col];\n\t\t\t\tnext_STATE[row][col] = STATE[row][col];\n\t\t\t\tfor(int a = 0; a < 4; a++){\n\n\t\t\t\t\tnext_check[row][col][a] = check[row][col][a];\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\n\t\tfor(int k = 0; k < 4; k++){\n\n\t\t\tnext_check[base_row][base_col][k] = false;\n\t\t}\n\n\t\tfor(int k = 0; k < num_pos[type]; k++){\n\n\t\t\tnext_check[base_row][base_col][dir_state[type][i][k]] = true;\n\t\t}\n\n\t\tbool tmp_FLG = true;\n\t\tADD.clear();\n\n\t\t//自分に向かう辺のチェック(相手の枝が伸びてるのに自分がfalseなら不可)\n\t\tfor(int k = 0; k < 4; k++){\n\n\t\t\tint adj_row = base_row+diff_row[k];\n\t\t\tint adj_col = base_col+diff_col[k];\n\n\t\t\tif(!rangeCheck(adj_row,adj_col)||next_STATE[adj_row][adj_col] == -1)continue;\n\n\t\t\tif(next_check[adj_row][adj_col][rev_dir[dir_array[k]]] && !next_check[base_row][base_col][dir_array[k]]){\n\n\t\t\t\ttmp_FLG = false;\n\t\t\t\tbreak;\n\t\t\t}\n\t\t}\n\t\tif(!tmp_FLG)continue;\n\n\t\t//自分から向かう辺のチェック(自分の枝が相手に伸びてるのに相手がfalseなら不可)\n\t\tfor(int k = 0; k < 4; k++){\n\n\t\t\tif(!next_check[base_row][base_col][k])continue;\n\n\t\t\tint adj_row = base_row+diff_row[k];\n\t\t\tint adj_col = base_col+diff_col[k];\n\n\t\t\tif(!rangeCheck(adj_row,adj_col)){ //外に枝が向いていたら不可\n\n\t\t\t\ttmp_FLG = false;\n\t\t\t\tbreak;\n\t\t\t}\n\t\t\tif(next_STATE[adj_row][adj_col] == -1){ //新たにMUSTに加える\n\n\t\t\t\tADD.push_back(LOC(adj_row,adj_col));\n\t\t\t\tcontinue;\n\t\t\t}\n\n\t\t\tif(next_check[base_row][base_col][dir_array[k]] && !next_check[adj_row][adj_col][rev_dir[dir_array[k]]]){\n\n\t\t\t\ttmp_FLG = false;\n\t\t\t\tbreak;\n\t\t\t}\n\t\t}\n\t\tif(!tmp_FLG)continue;\n\n\n\t\tnext_STATE[base_row][base_col] = i;\n\t\tfor(int a = 0; a < ADD.size(); a++){\n\n\t\t\tnext_MUST[ADD[a].row][ADD[a].col] = true;\n\t\t}\n\n\t\trecursive(next_row,next_col,next_MUST,next_STATE,next_check);\n\t}\n}\n\n\nvoid func(){\n\n\tnum_switch = 0;\n\tchar buf[2];\n\n\tfor(int row = 0; row < H; row++){\n\t\tfor(int col = 0; col < W; col++){\n\t\t\tscanf(\"%s\",buf);\n\t\t\ttable[row][col] = buf[0];\n\t\t\tif(buf[0] == 'L' || buf[0] == 'R'){\n\n\t\t\t\tswitch_loc[num_switch].row = row;\n\t\t\t\tswitch_loc[num_switch].col = col;\n\t\t\t\tindex_table[row][col] = num_switch++;\n\t\t\t}\n\t\t}\n\t}\n\n\tANS = 0;\n\n\tint first_STATE[6][6];\n\tbool first_MUST[6][6],first_check[6][6][4];\n\n\tfor(int row = 0; row < H; row++){\n\t\tfor(int col = 0; col < W; col++){\n\t\t\tfirst_STATE[row][col] = -1;\n\t\t\tfirst_MUST[row][col] = false;\n\t\t\tfor(int a = 0; a < 4; a++){\n\n\t\t\t\tfirst_check[row][col][a] = false;\n\t\t\t}\n\t\t}\n\t}\n\n\tfor(int i = 0; i < num_switch; i++){\n\n\t\tfirst_MUST[switch_loc[i].row][switch_loc[i].col] = true;\n\t}\n\n\trecursive(0,0,first_MUST,first_STATE,first_check);\n\n\tprintf(\"%d\\n\",ANS);\n\treturn;\n}\n\n\nint main(){\n\n\tPOW[0] = 1;\n\tfor(int i = 1; i <= 6; i++){\n\n\t\tPOW[i] = POW[i-1]*2;\n\t}\n\n\twhile(true){\n\t\tscanf(\"%d %d\",&W,&H);\n\t\tif(W == 0 && H == 0)break;\n\n\t\tfunc();\n\t}\n\n\treturn 0;\n}", "accuracy": 1, "time_ms": 60, "memory_kb": 3300, "score_of_the_acc": -1.3333, "final_rank": 7 }, { "submission_id": "aoj_1303_1477525", "code_snippet": "#include<stdio.h>\n#include<vector>\n#include<algorithm>\nusing namespace std;\nchar in[2];\nchar t[8][8];\nint conv[8][8];\nint H,W;\nint ret;\nint used[20][20];\nint pr[8];\nint pc[8];\nint pd[8];\nint lr[4][3]={{1,1,0},{0,2,1},{1,1,2},{2,0,1}};\nint lc[4][3]={{0,2,1},{1,1,2},{2,0,1},{1,1,0}};\nint rr[4][3]={{1,1,2},{0,2,1},{1,1,0},{2,0,1}};\nint rc[4][3]={{0,2,1},{1,1,0},{2,0,1},{1,1,2}};\nint at[8][3];\nint au[8][3];\nint al[8][3];\nint ps;\nint v[8][3][1<<6];\nvoid go(int a,int b){\n\tfor(int i=0;i<ps;i++)for(int j=0;j<3;j++)for(int k=0;k<(1<<ps);k++)v[i][j][k]=-1;\n\tint len=0;\n\tint A=a;\n\tint B=b;\n\tint bit=0;\n\twhile(1){\n\t\tv[A][B][bit]=len;\n\t\tlen+=al[A][B]+1;\n\t\tint ta=at[A][B];int tb=au[A][B];\n\t\tA=ta;\n\t\tif(tb==0){\n\t\t\tif(bit&(1<<A))B=1;\n\t\t\telse B=2;\n\t\t\tbit^=(1<<A);\n\t\t}else B=0;\n\t\tif(~v[A][B][bit])break;\n\t}\n\tret=max(ret,len-v[A][B][bit]);\n}\nvoid calc(){\n\t/*for(int i=0;i<ps;i++){\n\t\tfor(int j=0;j<3;j++){\n\t\t\tprintf(\"(%d, %d, %d)\",at[i][j],au[i][j],al[i][j]);\n\t\t}\n\t\tprintf(\"\\n\");\n\t}\n\tprintf(\"\\n\");*/\n\tfor(int i=0;i<ps;i++)for(int j=0;j<3;j++){\n\t\tgo(i,j);\n\t}\n}\nvoid dfs_route(int P1,int P2,int TR,int TC,int MR,int MC,int L){\n\tif(MR<0||MC<0||MR>=H||MC>=W)return;\n\tif(~conv[MR][MC]){\n\t\tint M=conv[MR][MC];\n\t\tbool ok=false;\n\t\tfor(int i=0;i<3;i++){\n\t\t\tif(!~at[M][i]&&((t[MR][MC]=='L'&&TR==MR*2+lr[pd[M]][i]&&TC==MC*2+lc[pd[M]][i])||(t[MR][MC]=='R'&&TR==MR*2+rr[pd[M]][i]&&TC==MC*2+rc[pd[M]][i]))){\n\t\t\t\tat[P1][P2]=M;au[P1][P2]=i;\n\t\t\t\tat[M][i]=P1;au[M][i]=P2;\n\t\t\t\tal[P1][P2]=al[M][i]=L;\n\t\t\t\tok=true;break;\n\t\t\t}\n\t\t}\n\t\tif(!ok)return;\n\t\tfor(int i=0;i<ps;i++){\n\t\t\tfor(int j=0;j<3;j++){\n\t\t\t\tif(!~at[i][j]){\n\t\t\t\t\tint sr=pr[i]*2;\n\t\t\t\t\tint sc=pc[i]*2;\n\t\t\t\t\tint kr=pr[i];int kc=pc[i];\n\t\t\t\t\tif(t[pr[i]][pc[i]]=='L'){sr+=lr[pd[i]][j];sc+=lc[pd[i]][j];\n\t\t\t\t\tif(lr[pd[i]][j]==0)kr--;if(lr[pd[i]][j]==2)kr++;\n\t\t\t\t\tif(lc[pd[i]][j]==0)kc--;if(lc[pd[i]][j]==2)kc++;}\n\t\t\t\t\tif(t[pr[i]][pc[i]]=='R'){sr+=rr[pd[i]][j];sc+=rc[pd[i]][j];\n\t\t\t\t\tif(rr[pd[i]][j]==0)kr--;if(rr[pd[i]][j]==2)kr++;\n\t\t\t\t\tif(rc[pd[i]][j]==0)kc--;if(rc[pd[i]][j]==2)kc++;}\n\t\t\t\t\tdfs_route(i,j,sr,sc,kr,kc,0);\n\t\t\t\t\tat[at[P1][P2]][au[P1][P2]]=au[at[P1][P2]][au[P1][P2]]=-1;\n\t\t\t\t\tat[P1][P2]=au[P1][P2]=-1;\n\t\t\t\t\treturn;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\tcalc();\n\t\tat[at[P1][P2]][au[P1][P2]]=au[at[P1][P2]][au[P1][P2]]=-1;\n\t\tat[P1][P2]=au[P1][P2]=-1;\n\t\treturn;\n\t}\n\tif(used[MR][MC])return;\n\tused[MR][MC]=1;\n\tif(t[MR][MC]=='S'){\n\t\tint nr,nc,mr,mc;\n\t\tif(TR%2){\n\t\t\tnr=TR;nc=MC*4+2-TC;\n\t\t\tmr=MR;mc=(nc-MC-1);\n\t\t}else{\n\t\t\tnc=TC;nr=MR*4+2-TR;\n\t\t\tmc=MC;mr=(nr-MR-1);\n\t\t}\n\t\tdfs_route(P1,P2,nr,nc,mr,mc,L+1);\n\t}else{\n\t\tif(TR%2){\n\t\t\tdfs_route(P1,P2,MR*2,MC*2+1,MR-1,MC,L+1);\n\t\t\tdfs_route(P1,P2,MR*2+2,MC*2+1,MR+1,MC,L+1);\n\t\t}else{\n\t\t\tdfs_route(P1,P2,MR*2+1,MC*2,MR,MC-1,L+1);\n\t\t\tdfs_route(P1,P2,MR*2+1,MC*2+2,MR,MC+1,L+1);\n\t\t}\n\t}\n\tused[MR][MC]=0;\n}\nvoid dfs_dir(int a){\n\tif(a==ps){\n\t\tfor(int i=0;i<ps;i++)for(int j=0;j<3;j++){\n\t\t\tat[i][j]=au[i][j]=-1;\n\t\t}\n\t\tfor(int i=0;i<H;i++)for(int j=0;j<W;j++)used[i][j]=0;\n\t\tint sr=pr[0]*2;\n\t\tint sc=pc[0]*2;\n\t\tint kr=pr[0];int kc=pc[0];\n\t\tif(t[pr[0]][pc[0]]=='L'){sr+=lr[pd[0]][0];sc+=lc[pd[0]][0];\n\t\tif(lr[pd[0]][0]==0)kr--;if(lr[pd[0]][0]==2)kr++;\n\t\tif(lc[pd[0]][0]==0)kc--;if(lc[pd[0]][0]==2)kc++;}\n\t\tif(t[pr[0]][pc[0]]=='R'){sr+=rr[pd[0]][0];sc+=rc[pd[0]][0];\n\t\tif(rr[pd[0]][0]==0)kr--;if(rr[pd[0]][0]==2)kr++;\n\t\tif(rc[pd[0]][0]==0)kc--;if(rc[pd[0]][0]==2)kc++;}\n\t\tdfs_route(0,0,sr,sc,kr,kc,0);\n\t\treturn;\n\t}\n\tfor(int i=0;i<4;i++){\n\t\tpd[a]=i;\n\t\tdfs_dir(a+1);\n\t}\n}\nint main(){\n\tint a,b;\n\twhile(scanf(\"%d%d\",&b,&a),a){\n\t\tH=a;W=b;\n\t\tfor(int i=0;i<a;i++)for(int j=0;j<b;j++){\n\t\t\tscanf(\"%s\",in);\n\t\t\tt[i][j]=in[0];\n\t\t}\n\t\tfor(int i=0;i<a;i++)for(int j=0;j<b;j++)conv[i][j]=-1;\n\t\tret=0;\n\t\tps=0;\n\t\tfor(int i=0;i<a;i++)for(int j=0;j<b;j++){\n\t\t\tif(t[i][j]=='L'||t[i][j]=='R'){\n\t\t\t\tconv[i][j]=ps;\n\t\t\t\tpr[ps]=i;pc[ps++]=j;\n\t\t\t}\n\t\t}\n\t\tif(ps%2){\n\t\t\tprintf(\"0\\n\");continue;\n\t\t}\n\t\tdfs_dir(0);\n\t\tprintf(\"%d\\n\",ret);\n\t}\n}", "accuracy": 1, "time_ms": 50, "memory_kb": 1052, "score_of_the_acc": -0.2681, "final_rank": 1 }, { "submission_id": "aoj_1303_104796", "code_snippet": "#include<iostream>\n#include<cassert>\nusing namespace std;\n#define REP(i,b,n) for(int i=b;i<n;i++)\n#define rep(i,n) REP(i,0,n)\n\n#define N 0\n#define E 1\n#define S 2\n#define W 3\n\nint dx[]={0,1,0,-1};\nint dy[]={-1,0,1,0};\nint curdir[]={S,W,N,E};\n\nint sd[2][4]={//S default:0 -\n -1,W,-1,E,\n S,-1,N,-1,\n};\n\nint cd[4][4]={//C default:0 -^\n W,-1,-1,N,\n E,N,-1,-1,\n -1,S,E,-1,\n -1,-1,W,S,\n};\n\nint ld[4][2][4]={//if switch == 0 ld[i][0], switch == 1 ld[i][1] \n //ST //CURVE\n W,W,-1,E, W,W,-1,N,\n S,N,N,-1, E,N,N,-1,\n -1,W,E,E, -1,S,E,E,\n S,-1,N,S, S,-1,W,S\n /*\n {W,W,-1,E},{W,W,-1,N},\n {S,N,N,-1},{E,N,N,-1},\n {-1,W,E,E},{-1,S,E,E},\n {S,-1,N,S},{S,-1,W,S}\n */\n};\n\n\nint rd[4][2][4]={//if switch == 0 ld[i][0], switch == 1 ld[i][1] \n //ST //CURVE\n -1,W,W,E, -1,W,W,S,\n S,-1,N,N, W,-1,N,N,\n E,W,-1,E, E,N,-1,E,\n S,S,N,-1, S,S,E,-1\n /*\n {-1,W,W,E},{-1,W,W,S},\n {S,-1,N,N},{W,-1,N,N},\n {E,W,-1,E},{E,N,-1,E},\n {S,S,N,-1},{S,S,E,-1}\n */\n};\n\nint switchid[6][6];\nint rot[6][6];\nchar m[6][6];\n\nint cost[6][6][4][(1<<6)];\nint visited[6][6][4][(1<<6)];\nint ans;\n\nvoid dfs(int r,int c,int y,int x,int d,int state,int visnow,int length){\n if (length != 0)d=curdir[d];\n\n if (x == -1||y == -1||x==c||y==r)return;\n\n if (visited[y][x][d][state] == -1){\n \n }else if (visited[y][x][d][state] == visnow){\n ans=max(ans,length-cost[y][x][d][state]);\n return;\n }else {\n return;\n }\n visited[y][x][d][state]=visnow;\n cost[y][x][d][state] =length;\n\n int nextd;\n int curgrid=rot[y][x];\n if (m[y][x] == 'S'){\n nextd=sd[curgrid][d];\n dfs(r,c,y+dy[nextd],x+dx[nextd],nextd,state,visnow,length+1);\n }else if( m[y][x] == 'C'){\n nextd=cd[curgrid][d];\n dfs(r,c,y+dy[nextd],x+dx[nextd],nextd,state,visnow,length+1);\n }else if (m[y][x] == 'L'){\n bool isswitch=(((1<<switchid[y][x])&state)!=0);\n nextd=ld[curgrid][isswitch][d];\n if ((rot[y][x]+3)%4 == d){\n if (isswitch)state-=(1<<switchid[y][x]);\n else state+=(1<<switchid[y][x]);\n }\n dfs(r,c,y+dy[nextd],x+dx[nextd],nextd,state,visnow,length+1);\n }else if (m[y][x] == 'R'){\n bool isswitch=(((1<<switchid[y][x])&state)!=0);\n nextd=rd[curgrid][isswitch][d];\n if ((rot[y][x]+3)%4==d){\n if (isswitch)state-=(1<<switchid[y][x]);\n else state+=(1<<switchid[y][x]);\n }\n dfs(r,c,y+dy[nextd],x+dx[nextd],nextd,state,visnow,length+1);\n }\n}\n\nvoid solve(int r,int c,int index){\n rep(i,r){\n rep(j,c){\n rep(k,4){\n\trep(l,(1<<index)){\n\t visited[i][j][k][l]=-1;\n\t}\n }\n }\n }\n \n int visnow=0;\n rep(i,r){\n rep(j,c){\n if (m[i][j] == 'R'||m[i][j] == 'L'){\n\tif (rot[i][j] == 0){\n\t dfs(r,c,i,j,W,0,visnow,0);\n\t}else if (rot[i][j] == 1){\n\t dfs(r,c,i,j,N,0,visnow,0);\n\t}else if (rot[i][j] == 2){\n\t dfs(r,c,i,j,E,0,visnow,0);\n\t}else if (rot[i][j] == 3){\n\t dfs(r,c,i,j,S,0,visnow,0);\n\t}\n\tvisnow++;\n }\n }\n }\n}\n\n\n//if rot[i][j] == -1 unvisited\nvoid search(int r,int c,int index,int now,int num,//num -> 3 position of L or R\n\t int y,int x,int d,int *posx,int *posy,bool tonext){\n\n if (tonext && num == 0 && index == now+1){\n solve(r,c,index);\n return;\n }\n\n\n if (tonext){\n \n if (num == 0){//straight\n now++;\n y = posy[now];\n x = posx[now];\n\n d = (rot[y][x]+3)%4;\n search(r,c,index,now,1,posy[now]+dy[d],posx[now]+dx[d],\n\t d,posx,posy,false);\n }else if (num == 1){\n y = posy[now];\n x = posx[now];\n d=(rot[y][x]+1)%4;\n search(r,c,index,now,2,posy[now]+dy[d],posx[now]+dx[d],\n\t d,posx,posy,false);\n }else if (num == 2){//curve\n y = posy[now];\n x = posx[now];\n if (m[y][x] == 'R'){\n\td=(rot[y][x]+2)%4;\n }else if (m[posy[now]][posx[now]]=='L'){\n\td=rot[y][x];\n }\n search(r,c,index,now,0,posy[now]+dy[d],posx[now]+dx[d],\n\t d,posx,posy,false);\n \n }\n return;\n }\n\n if (x == -1 || y == -1||x==c||y==r)return;\n d=curdir[d];\n\n if (m[y][x] == 'S'){\n if (rot[y][x] == -1){\n if (d == E || d == W)rot[y][x]=0;\n else if (d == S || d == N)rot[y][x]=1;\n search(r,c,index,now,num,y+dy[sd[rot[y][x]][d]],x+dx[sd[rot[y][x]][d]],\n\t sd[rot[y][x]][d],posx,posy,false);\n rot[y][x]=-1;\n return;\n }else if (rot[y][x] != -1){\n if (sd[rot[y][x]][d] == -1)return;\n else search(r,c,index,now,num,-1,-1,-1,posx,posy,true);\n }\n }else if(m[y][x] == 'C'){\n if (rot[y][x] == -1){\n if (d == E)rot[y][x]=1;\n else if (d == W)rot[y][x]=3;\n else if (d == S)rot[y][x]=2;\n else if (d == N)rot[y][x]=0;\n search(r,c,index,now,num,y+dy[cd[rot[y][x]][d]],x+dx[cd[rot[y][x]][d]],\n\t cd[rot[y][x]][d],posx,posy,false);\n if (d == E)rot[y][x]=2;\n else if (d == W)rot[y][x]=0;\n else if (d == S)rot[y][x]=3;\n else if (d == N)rot[y][x]=1;\n search(r,c,index,now,num,y+dy[cd[rot[y][x]][d]],x+dx[cd[rot[y][x]][d]],\n\t cd[rot[y][x]][d],posx,posy,false);\n rot[y][x]=-1;\n }else if (rot[y][x] != -1){\n if (cd[rot[y][x]][d] == -1)return;\n else search(r,c,index,now,num,-1,-1,-1,posx,posy,true);\n }\n }else if (m[y][x] == 'L'){\n if (ld[rot[y][x]][0][d] == -1)return;\n search(r,c,index,now,num,-1,-1,-1,posx,posy,true);\n }else if (m[y][x] == 'R'){\n if (rd[rot[y][x]][0][d] == -1)return;\n search(r,c,index,now,num,-1,-1,-1,posx,posy,true);\n }\n}\n\n\nvoid decide(int r,int c,int index,int now,int *posx,int *posy){\n if (now == index){\n search(r,c,index,-1,0,-1,-1,-1,posx,posy,true);\n return;\n }\n rep(i,4){\n rot[posy[now]][posx[now]]=i;\n decide(r,c,index,now+1,posx,posy);\n }\n}\n\nmain(){\n int r,c;\n while(cin>>c>>r && c){\n int index =0;\n int posx[6],posy[6];\n rep(i,r){\n rep(j,c){\n\tcin>>m[i][j];\n\trot[i][j]=-1;\n\tif (m[i][j] == 'R'||m[i][j]=='L'){\n\t switchid[i][j]=index;\n\t posy[index]=i;\n\t posx[index]=j;\n\t index++;\n\t}\n }\n }\n\n ans = 0;\n decide(r,c,index,0,posx,posy);\n cout << ans << endl;\n }\n}", "accuracy": 1, "time_ms": 80, "memory_kb": 944, "score_of_the_acc": -0.5556, "final_rank": 2 }, { "submission_id": "aoj_1303_104794", "code_snippet": "#include<iostream>\n#include<cassert>\nusing namespace std;\n#define REP(i,b,n) for(int i=b;i<n;i++)\n#define rep(i,n) REP(i,0,n)\n\n#define N 0\n#define E 1\n#define S 2\n#define W 3\n\nint dx[]={0,1,0,-1};\nint dy[]={-1,0,1,0};\nint curdir[]={S,W,N,E};\n\nint sd[2][4]={//S default:0 -\n -1,W,-1,E,\n S,-1,N,-1,\n};\n\nint cd[4][4]={//C default:0 -^\n W,-1,-1,N,\n E,N,-1,-1,\n -1,S,E,-1,\n -1,-1,W,S,\n};\n\nint ld[4][2][4]={//if switch == 0 ld[i][0], switch == 1 ld[i][1] \n //ST //CURVE\n W,W,-1,E, W,W,-1,N,\n S,N,N,-1, E,N,N,-1,\n -1,W,E,E, -1,S,E,E,\n S,-1,N,S, S,-1,W,S\n /*\n {W,W,-1,E},{W,W,-1,N},\n {S,N,N,-1},{E,N,N,-1},\n {-1,W,E,E},{-1,S,E,E},\n {S,-1,N,S},{S,-1,W,S}\n */\n};\n\n\nint rd[4][2][4]={//if switch == 0 ld[i][0], switch == 1 ld[i][1] \n //ST //CURVE\n -1,W,W,E, -1,W,W,S,\n S,-1,N,N, W,-1,N,N,\n E,W,-1,E, E,N,-1,E,\n S,S,N,-1, S,S,E,-1\n /*\n {-1,W,W,E},{-1,W,W,S},\n {S,-1,N,N},{W,-1,N,N},\n {E,W,-1,E},{E,N,-1,E},\n {S,S,N,-1},{S,S,E,-1}\n */\n};\n\nint switchid[6][6];\nint rot[6][6];\nchar m[6][6];\n\nint cost[6][6][4][(1<<6)];\nint visited[6][6][4][(1<<6)];\nint ans;\n\nvoid dfs(int r,int c,int y,int x,int d,int state,int visnow,int length){\n if (length != 0)d=curdir[d];\n\n if (x == -1||y == -1||x==c||y==r)return;\n\n if (visited[y][x][d][state] == -1){\n \n }else if (visited[y][x][d][state] == visnow){\n ans=max(ans,length-cost[y][x][d][state]);\n return;\n }else {\n return;\n }\n visited[y][x][d][state]=visnow;\n cost[y][x][d][state] =length;\n\n int nextd;\n int curgrid=rot[y][x];\n if (m[y][x] == 'S'){\n nextd=sd[curgrid][d];\n dfs(r,c,y+dy[nextd],x+dx[nextd],nextd,state,visnow,length+1);\n }else if( m[y][x] == 'C'){\n nextd=cd[curgrid][d];\n dfs(r,c,y+dy[nextd],x+dx[nextd],nextd,state,visnow,length+1);\n }else if (m[y][x] == 'L'){\n bool isswitch=(((1<<switchid[y][x])&state)!=0);\n nextd=ld[curgrid][isswitch][d];\n if ((rot[y][x]+3)%4 == d){\n if (isswitch)state-=(1<<switchid[y][x]);\n else state+=(1<<switchid[y][x]);\n }\n dfs(r,c,y+dy[nextd],x+dx[nextd],nextd,state,visnow,length+1);\n }else if (m[y][x] == 'R'){\n bool isswitch=(((1<<switchid[y][x])&state)!=0);\n nextd=rd[curgrid][isswitch][d];\n if ((rot[y][x]+3)%4==d){\n if (isswitch)state-=(1<<switchid[y][x]);\n else state+=(1<<switchid[y][x]);\n }\n dfs(r,c,y+dy[nextd],x+dx[nextd],nextd,state,visnow,length+1);\n }\n}\n\nvoid solve(int r,int c,int index){\n rep(i,r){\n rep(j,c){\n rep(k,4){\n\trep(l,(1<<index)){\n\t visited[i][j][k][l]=-1;\n\t}\n }\n }\n }\n \n int visnow=0;\n rep(i,r){\n rep(j,c){\n if (m[i][j] == 'R'||m[i][j] == 'L'){\n\tif (rot[i][j] == 0){\n\t dfs(r,c,i,j,W,0,visnow,0);\n\t}else if (rot[i][j] == 1){\n\t dfs(r,c,i,j,N,0,visnow,0);\n\t}else if (rot[i][j] == 2){\n\t dfs(r,c,i,j,E,0,visnow,0);\n\t}else if (rot[i][j] == 3){\n\t dfs(r,c,i,j,S,0,visnow,0);\n\t}\n\t/*\n\trep(ii,r){\n\t rep(jj,c){\n\t rep(kk,4){\n\t rep(ll,(1<<index)){\n\t\tvisited[ii][jj][kk][ll]=-1;\n\t }\n\t }\n\t }\n\t}\n\n\tif (rot[i][j] == 0){\n\t dfs(r,c,i,j,W,1<<switchid[i][j],visnow,0);\n\t}else if (rot[i][j] == 1){\n\t dfs(r,c,i,j,N,1<<switchid[i][j],visnow,0);\n\t}else if (rot[i][j] == 2){\n\t dfs(r,c,i,j,E,1<<switchid[i][j],visnow,0);\n\t}else if (rot[i][j] == 3){\n\t dfs(r,c,i,j,S,1<<switchid[i][j],visnow,0);\n\t}\n\trep(ii,r){\n\t rep(jj,c){\n\t rep(kk,4){\n\t rep(ll,(1<<index)){\n\t\tvisited[ii][jj][kk][ll]=-1;\n\t }\n\t }\n\t }\n\t}\n\t*/\n\tvisnow++;\n }\n }\n }\n}\n\n\n//if rot[i][j] == -1 unvisited\nvoid search(int r,int c,int index,int now,int num,//num -> 3 position of L or R\n\t int y,int x,int d,int *posx,int *posy,bool tonext){\n\n if (tonext && num == 0 && index == now+1){\n solve(r,c,index);\n return;\n }\n\n\n if (tonext){\n \n if (num == 0){//straight\n now++;\n y = posy[now];\n x = posx[now];\n\n d = (rot[y][x]+3)%4;\n search(r,c,index,now,1,posy[now]+dy[d],posx[now]+dx[d],\n\t d,posx,posy,false);\n }else if (num == 1){\n y = posy[now];\n x = posx[now];\n d=(rot[y][x]+1)%4;\n search(r,c,index,now,2,posy[now]+dy[d],posx[now]+dx[d],\n\t d,posx,posy,false);\n }else if (num == 2){//curve\n y = posy[now];\n x = posx[now];\n if (m[y][x] == 'R'){\n\td=(rot[y][x]+2)%4;\n }else if (m[posy[now]][posx[now]]=='L'){\n\td=rot[y][x];\n }\n search(r,c,index,now,0,posy[now]+dy[d],posx[now]+dx[d],\n\t d,posx,posy,false);\n \n }\n return;\n }\n\n if (x == -1 || y == -1||x==c||y==r)return;\n d=curdir[d];\n\n if (m[y][x] == 'S'){\n if (rot[y][x] == -1){\n if (d == E || d == W)rot[y][x]=0;\n else if (d == S || d == N)rot[y][x]=1;\n search(r,c,index,now,num,y+dy[sd[rot[y][x]][d]],x+dx[sd[rot[y][x]][d]],\n\t sd[rot[y][x]][d],posx,posy,false);\n rot[y][x]=-1;\n return;\n }else if (rot[y][x] != -1){\n if (sd[rot[y][x]][d] == -1)return;\n else search(r,c,index,now,num,-1,-1,-1,posx,posy,true);\n }\n }else if(m[y][x] == 'C'){\n if (rot[y][x] == -1){\n if (d == E)rot[y][x]=1;\n else if (d == W)rot[y][x]=3;\n else if (d == S)rot[y][x]=2;\n else if (d == N)rot[y][x]=0;\n search(r,c,index,now,num,y+dy[cd[rot[y][x]][d]],x+dx[cd[rot[y][x]][d]],\n\t cd[rot[y][x]][d],posx,posy,false);\n if (d == E)rot[y][x]=2;\n else if (d == W)rot[y][x]=0;\n else if (d == S)rot[y][x]=3;\n else if (d == N)rot[y][x]=1;\n search(r,c,index,now,num,y+dy[cd[rot[y][x]][d]],x+dx[cd[rot[y][x]][d]],\n\t cd[rot[y][x]][d],posx,posy,false);\n rot[y][x]=-1;\n }else if (rot[y][x] != -1){\n if (cd[rot[y][x]][d] == -1)return;\n else search(r,c,index,now,num,-1,-1,-1,posx,posy,true);\n }\n }else if (m[y][x] == 'L'){\n if (ld[rot[y][x]][0][d] == -1)return;\n search(r,c,index,now,num,-1,-1,-1,posx,posy,true);\n }else if (m[y][x] == 'R'){\n if (rd[rot[y][x]][0][d] == -1)return;\n search(r,c,index,now,num,-1,-1,-1,posx,posy,true);\n }\n}\n\n\nvoid decide(int r,int c,int index,int now,int *posx,int *posy){\n if (now == index){\n search(r,c,index,-1,0,-1,-1,-1,posx,posy,true);\n return;\n }\n rep(i,4){\n rot[posy[now]][posx[now]]=i;\n decide(r,c,index,now+1,posx,posy);\n }\n}\n\nmain(){\n int r,c;\n while(cin>>c>>r && c){\n int index =0;\n int posx[6],posy[6];\n rep(i,r){\n rep(j,c){\n\tcin>>m[i][j];\n\trot[i][j]=-1;\n\tif (m[i][j] == 'R'||m[i][j]=='L'){\n\t switchid[i][j]=index;\n\t posy[index]=i;\n\t posx[index]=j;\n\t index++;\n\t}\n }\n }\n\n ans = 0;\n decide(r,c,index,0,posx,posy);\n cout << ans << endl;\n }\n\n}", "accuracy": 1, "time_ms": 90, "memory_kb": 944, "score_of_the_acc": -0.6667, "final_rank": 3 }, { "submission_id": "aoj_1303_104790", "code_snippet": "#include<iostream>\n#include<cassert>\nusing namespace std;\n#define REP(i,b,n) for(int i=b;i<n;i++)\n#define rep(i,n) REP(i,0,n)\n\n#define N 0\n#define E 1\n#define S 2\n#define W 3\n\nint dx[]={0,1,0,-1};\nint dy[]={-1,0,1,0};\nint curdir[]={S,W,N,E};\n\nint sd[2][4]={//S default:0 -\n -1,W,-1,E,\n S,-1,N,-1,\n};\n\nint cd[4][4]={//C default:0 -^\n W,-1,-1,N,\n E,N,-1,-1,\n -1,S,E,-1,\n -1,-1,W,S,\n};\n\nint ld[4][2][4]={//if switch == 0 ld[i][0], switch == 1 ld[i][1] \n //ST //CURVE\n W,W,-1,E, W,W,-1,N,\n S,N,N,-1, E,N,N,-1,\n -1,W,E,E, -1,S,E,E,\n S,-1,N,S, S,-1,W,S\n /*\n {W,W,-1,E},{W,W,-1,N},\n {S,N,N,-1},{E,N,N,-1},\n {-1,W,E,E},{-1,S,E,E},\n {S,-1,N,S},{S,-1,W,S}\n */\n};\n\n\nint rd[4][2][4]={//if switch == 0 ld[i][0], switch == 1 ld[i][1] \n //ST //CURVE\n -1,W,W,E, -1,W,W,S,\n S,-1,N,N, W,-1,N,N,\n E,W,-1,E, E,N,-1,E,\n S,S,N,-1, S,S,E,-1\n /*\n {-1,W,W,E},{-1,W,W,S},\n {S,-1,N,N},{W,-1,N,N},\n {E,W,-1,E},{E,N,-1,E},\n {S,S,N,-1},{S,S,E,-1}\n */\n};\n\nint switchid[6][6];\nint rot[6][6];\nchar m[6][6];\n\nint cost[6][6][4][(1<<6)];\nint visited[6][6][4][(1<<6)];\nint ans;\n\nvoid dfs(int r,int c,int y,int x,int d,int state,int visnow,int length){\n if (length != 0)d=curdir[d];\n /*\n cout << y <<\" \" << x <<\" \" << d <<\" \" << state << \" \" << length << \" \" <<\n visnow << \" \" << visited[y][x][d][state] << endl;\n */\n\n if (x == -1||y == -1||x==c||y==r)return;\n /*\n if (m[y][x] == 'R' || m[y][x] == 'L'){\n cout<<\"tmp \" << y << \" \" <<x <<\" \" << m[y][x] <<\" \" << d <<\" \" <<\n (rot[y][x]+3)%4 << endl;\n if ((rot[y][x]+3%4)==d){\n cout << y <<\" \" << x <<\" \" << d <<\" \" << \n\t(bool)(((1<<switchid[y][x])&state)!=0) << \" \" <<state <<\" \"<< m[y][x] << \" \" << cost[y][x][d][state] <<\" \" << length << endl;\n } \n }\n */ \n if (visited[y][x][d][state] == -1){\n \n }else if (visited[y][x][d][state] == visnow){\n // cout << \"tmp \" << length - cost[y][x][d][state] << endl;\n ans=max(ans,length-cost[y][x][d][state]);\n return;\n }else {\n return;\n }\n visited[y][x][d][state]=visnow;\n cost[y][x][d][state] =length;\n\n int nextd;\n int curgrid=rot[y][x];\n if (m[y][x] == 'S'){\n nextd=sd[curgrid][d];\n dfs(r,c,y+dy[nextd],x+dx[nextd],nextd,state,visnow,length+1);\n }else if( m[y][x] == 'C'){\n nextd=cd[curgrid][d];\n dfs(r,c,y+dy[nextd],x+dx[nextd],nextd,state,visnow,length+1);\n }else if (m[y][x] == 'L'){\n bool isswitch=(((1<<switchid[y][x])&state)!=0);\n nextd=ld[curgrid][isswitch][d];\n if ((rot[y][x]+3)%4 == d){\n if (isswitch)state-=(1<<switchid[y][x]);\n else state+=(1<<switchid[y][x]);\n }\n dfs(r,c,y+dy[nextd],x+dx[nextd],nextd,state,visnow,length+1);\n }else if (m[y][x] == 'R'){\n bool isswitch=(((1<<switchid[y][x])&state)!=0);\n nextd=rd[curgrid][isswitch][d];\n if ((rot[y][x]+3)%4==d){\n if (isswitch)state-=(1<<switchid[y][x]);\n else state+=(1<<switchid[y][x]);\n }\n dfs(r,c,y+dy[nextd],x+dx[nextd],nextd,state,visnow,length+1);\n }\n}\n\nvoid solve(int r,int c,int index){\n rep(i,r){\n rep(j,c){\n rep(k,4){\n\trep(l,(1<<index)){\n\t visited[i][j][k][l]=-1;\n\t}\n }\n }\n }\n \n int visnow=0;\n rep(i,r){\n rep(j,c){\n if (m[i][j] == 'R'||m[i][j] == 'L'){\n\t//\tcout << i <<\" \" << j << endl;\n\tif (rot[i][j] == 0){\n\t dfs(r,c,i,j,W,0,visnow,0);\n\t}else if (rot[i][j] == 1){\n\t dfs(r,c,i,j,N,0,visnow,0);\n\t}else if (rot[i][j] == 2){\n\t dfs(r,c,i,j,E,0,visnow,0);\n\t}else if (rot[i][j] == 3){\n\t dfs(r,c,i,j,S,0,visnow,0);\n\t}\n\trep(ii,r){\n\t rep(jj,c){\n\t rep(kk,4){\n\t rep(ll,(1<<index)){\n\t\tvisited[ii][jj][kk][ll]=-1;\n\t }\n\t }\n\t }\n\t}\n\n\t//\tcout << i <<\" \" << j << endl;\n\tif (rot[i][j] == 0){\n\t dfs(r,c,i,j,W,1<<switchid[i][j],visnow,0);\n\t}else if (rot[i][j] == 1){\n\t dfs(r,c,i,j,N,1<<switchid[i][j],visnow,0);\n\t}else if (rot[i][j] == 2){\n\t dfs(r,c,i,j,E,1<<switchid[i][j],visnow,0);\n\t}else if (rot[i][j] == 3){\n\t dfs(r,c,i,j,S,1<<switchid[i][j],visnow,0);\n\t}\n\trep(ii,r){\n\t rep(jj,c){\n\t rep(kk,4){\n\t rep(ll,(1<<index)){\n\t\tvisited[ii][jj][kk][ll]=-1;\n\t }\n\t }\n\t }\n\t}\n\tvisnow++;\n }\n }\n }\n}\n\n\n//if rot[i][j] == -1 unvisited\nvoid search(int r,int c,int index,int now,int num,//num -> 3 position of L or R\n\t int y,int x,int d,int *posx,int *posy,bool tonext){\n\n if (tonext && num == 0 && index == now+1){\n // cout << \"try to find maximum loop on this layout\"<<endl;\n /* \n cout <<\"output\"<<endl;\n rep(i,r){\n rep(j,c){\n\tcout << m[i][j] <<\" \" << rot[i][j] <<\" \";\n }\n cout << endl;\n }\n cout << endl;\n */ \n \n solve(r,c,index);\n return;\n }\n\n\n if (tonext){\n \n //cout <<\"change \" << now <<\" \" << num << endl;\n if (num == 0){//straight\n now++;\n y = posy[now];\n x = posx[now];\n // cout <<now << \"rot \" << rot[posy[now]][posx[now]] <<endl;\n\n d = (rot[y][x]+3)%4;\n // cout <<\"dir \" << d << endl;\n search(r,c,index,now,1,posy[now]+dy[d],posx[now]+dx[d],\n\t d,posx,posy,false);\n }else if (num == 1){\n y = posy[now];\n x = posx[now];\n d=(rot[y][x]+1)%4;\n // cout <<\"dir \" << d << endl;\n search(r,c,index,now,2,posy[now]+dy[d],posx[now]+dx[d],\n\t d,posx,posy,false);\n }else if (num == 2){//curve\n y = posy[now];\n x = posx[now];\n if (m[y][x] == 'R'){\n\td=(rot[y][x]+2)%4;\n }else if (m[posy[now]][posx[now]]=='L'){\n\td=rot[y][x];\n }\n search(r,c,index,now,0,posy[now]+dy[d],posx[now]+dx[d],\n\t d,posx,posy,false);\n \n }\n return;\n }\n\n \n // cout <<\" \" << y <<\" \" <<x <<\" \" << curdir[d] <<\" \" << m[y][x] <<\" \" <<num << endl;\n\n if (x == -1 || y == -1||x==c||y==r)return;\n d=curdir[d];\n\n\n if (m[y][x] == 'S'){\n if (rot[y][x] == -1){\n if (d == E || d == W)rot[y][x]=0;\n else if (d == S || d == N)rot[y][x]=1;\n search(r,c,index,now,num,y+dy[sd[rot[y][x]][d]],x+dx[sd[rot[y][x]][d]],\n\t sd[rot[y][x]][d],posx,posy,false);\n rot[y][x]=-1;\n return;\n }else if (rot[y][x] != -1){\n if (sd[rot[y][x]][d] == -1)return;\n else search(r,c,index,now,num,-1,-1,-1,posx,posy,true);\n }\n }else if(m[y][x] == 'C'){\n if (rot[y][x] == -1){\n if (d == E)rot[y][x]=1;\n else if (d == W)rot[y][x]=3;\n else if (d == S)rot[y][x]=2;\n else if (d == N)rot[y][x]=0;\n // cout <<\"curve check \" << rot[y][x] <<\" \" << d << \" \" << y+dy[cd[rot[y][x]][d]] <<\n //\t\" \"<< x+dx[cd[rot[y][x]][d]]<<endl;\n search(r,c,index,now,num,y+dy[cd[rot[y][x]][d]],x+dx[cd[rot[y][x]][d]],\n\t cd[rot[y][x]][d],posx,posy,false);\n if (d == E)rot[y][x]=2;\n else if (d == W)rot[y][x]=0;\n else if (d == S)rot[y][x]=3;\n else if (d == N)rot[y][x]=1;\n search(r,c,index,now,num,y+dy[cd[rot[y][x]][d]],x+dx[cd[rot[y][x]][d]],\n\t cd[rot[y][x]][d],posx,posy,false);\n rot[y][x]=-1;\n }else if (rot[y][x] != -1){\n if (cd[rot[y][x]][d] == -1)return;\n else search(r,c,index,now,num,-1,-1,-1,posx,posy,true);\n }\n }else if (m[y][x] == 'L'){\n if (ld[rot[y][x]][0][d] == -1)return;\n // cout <<\"test L \" << ld[rot[y][x]][0][d] <<endl;\n search(r,c,index,now,num,-1,-1,-1,posx,posy,true);\n }else if (m[y][x] == 'R'){\n if (rd[rot[y][x]][0][d] == -1)return;\n search(r,c,index,now,num,-1,-1,-1,posx,posy,true);\n }\n}\n\n\nvoid decide(int r,int c,int index,int now,int *posx,int *posy){\n if (now == index){\n search(r,c,index,-1,0,-1,-1,-1,posx,posy,true);\n return;\n }\n rep(i,4){\n rot[posy[now]][posx[now]]=i;\n decide(r,c,index,now+1,posx,posy);\n }\n}\n\nmain(){\n int r,c;\n while(cin>>c>>r && c){\n int index =0;\n int posx[6],posy[6];\n rep(i,r){\n rep(j,c){\n\tcin>>m[i][j];\n\trot[i][j]=-1;\n\tif (m[i][j] == 'R'||m[i][j]=='L'){\n\t switchid[i][j]=index;\n\t posy[index]=i;\n\t posx[index]=j;\n\t index++;\n\t}\n }\n }\n\n ans = 0;\n decide(r,c,index,0,posx,posy);\n cout << ans << endl;\n }\n\n}", "accuracy": 1, "time_ms": 120, "memory_kb": 944, "score_of_the_acc": -1, "final_rank": 5 } ]
aoj_1298_cpp
Problem D: Separate Points Numbers of black and white points are placed on a plane. Let's imagine that a straight line of infinite length is drawn on the plane. When the line does not meet any of the points, the line divides these points into two groups. If the division by such a line results in one group consisting only of black points and the other consisting only of white points, we say that the line "separates black and white points". Let's see examples in Figure 3. In the leftmost example, you can easily find that the black and white points can be perfectly separated by the dashed line according to their colors. In the remaining three examples, there exists no such straight line that gives such a separation. Figure 3: Example planes In this problem, given a set of points with their colors and positions, you are requested to decide whether there exists a straight line that separates black and white points. Input The input is a sequence of datasets, each of which is formatted as follows. n m x 1 y 1 . . . x n y n x n +1 y n +1 . . . x n + m y n + m The first line contains two positive integers separated by a single space; n is the number of black points, and m is the number of white points. They are less than or equal to 100. Then n + m lines representing the coordinates of points follow. Each line contains two integers x i and y i separated by a space, where ( x i , y i ) represents the x -coordinate and the y -coordinate of the i -th point. The color of the i -th point is black for 1 ≤ i ≤ n , and is white for n + 1 ≤ i ≤ n + m . All the points have integral x - and y -coordinate values between 0 and 10000 inclusive. You can also assume that no two points have the same position. The end of the input is indicated by a line containing two zeros separated by a space. Output For each dataset, output " YES " if there exists a line satisfying the condition. If not, output " NO ". In either case, print it in one line for each input dataset. Sample Input 3 3 100 700 200 200 600 600 500 100 500 300 800 500 3 3 100 300 400 600 400 100 600 400 500 900 300 300 3 4 300 300 500 300 400 600 100 100 200 900 500 900 800 100 1 2 300 300 100 100 500 500 1 1 100 100 200 100 2 2 0 0 500 700 1000 1400 1500 2100 2 2 0 0 1000 1000 1000 0 0 1000 3 3 0 100 4999 102 10000 103 5001 102 10000 102 0 101 3 3 100 100 200 100 100 200 0 0 400 0 0 400 3 3 2813 1640 2583 2892 2967 1916 541 3562 9298 3686 7443 7921 0 0 Output for the Sample Input YES NO NO NO YES YES NO NO NO YES
[ { "submission_id": "aoj_1298_10522105", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n#define all(v) (v).begin(),(v).end()\n#define pb(a) push_back(a)\n#define rep(i, n) for(int i=0;i<n;i++)\n#define foa(e, v) for(auto&& e : v)\nusing ll = long long;\nconst ll MOD7 = 1000000007, MOD998 = 998244353, INF = (1LL << 60);\n#define dout(a) cout<<fixed<<setprecision(10)<<a<<endl;\n\nll cross(ll x1, ll y1, ll x2, ll y2) {\n return x1 * y2 - x2 * y1;\n}\nint main() {\n cin.tie(0);\n ios::sync_with_stdio(false);\n ll n, m; cin >> n >> m;\n ll inf = 1LL << 25;\n while(n) {\n vector<ll> ax(n), ay(n), bx(m), by(m);\n rep(i, n) cin >> ax[i] >> ay[i];\n rep(i, m) cin >> bx[i] >> by[i];\n bool ok = 0;\n rep(i, n) rep(j, m) {\n ll num = max(abs(ax[i] - bx[j]), abs(ay[i] - by[j]));\n num = inf / num;\n ll X1 = (ax[i] - bx[j]) * num + bx[j];\n ll Y1 = (ay[i] - by[j]) * num + by[j];\n num *= -1;\n ll X2 = (ax[i] - bx[j]) * num + ax[i];\n ll Y2 = (ay[i] - by[j]) * num + ay[i];\n \n // (A - B) * num + B\n // (B - A) * num + B\n assert(cross(ax[i] - X1, ay[i] - Y1, X2 - X1, Y2 - Y1) == 0LL);\n for(ll i1 = -1; i1 <= 1; i1 ++) {\n for(ll i2 = -1; i2 <= 1; i2 ++) {\n for(ll i3 = -1; i3 <= 1; i3 ++) {\n for(ll i4 = -1; i4 <= 1; i4 ++) {\n ll x1 = X1 + i1;\n ll y1 = Y1 + i2;\n ll x2 = X2 + i3;\n ll y2 = Y2 + i4;\n // if(i1 == 0 and i2 == 1 and i3 == 0 and i4 == -1) {\n // cout << x1 << \" \" << y1 << \" \" << x2 << \" \" << y2 << endl;\n // }\n {\n bool ng = 0;\n rep(k, n) {\n // if(i1 == 0 and i2 == 1 and i3 == 0 and i4 == -1) cout << cross(ax[k] - x1, ay[k] - y1, x2 - x1, y2 - y1) << \" \";\n if(cross(ax[k] - x1, ay[k] - y1, x2 - x1, y2 - y1) >= 0) {\n ng = 1;\n break;\n }\n }\n if(!ng) rep(k, m) {\n // if(i1 == 0 and i2 == 1 and i3 == 0 and i4 == -1) cout << cross(bx[k] - x1, by[k] - y1, x2 - x1, y2 - y1) << \" \";\n if(cross(bx[k] - x1, by[k] - y1, x2 - x1, y2 - y1) <= 0) {\n ng = 1;\n break;\n }\n }\n if(!ng) {\n ok = 1;\n i = n; j = m;\n i1 = 2; i2 = 2; i3 = 2; i4 = 2;\n break;\n }\n }{\n bool ng = 0;\n rep(k, n) {\n // if(i1 == 0 and i2 == 1 and i3 == 0 and i4 == -1) cout << cross(ax[k] - x1, ay[k] - y1, x2 - x1, y2 - y1) << \" \";\n if(cross(ax[k] - x1, ay[k] - y1, x2 - x1, y2 - y1) <= 0) {\n ng = 1;\n break;\n }\n }\n if(!ng) rep(k, m) {\n // if(i1 == 0 and i2 == 1 and i3 == 0 and i4 == -1) cout << cross(bx[k] - x1, by[k] - y1, x2 - x1, y2 - y1) << \" \";\n if(cross(bx[k] - x1, by[k] - y1, x2 - x1, y2 - y1) >= 0) {\n ng = 1;\n break;\n }\n }\n if(!ng) {\n ok = 1;\n i = n; j = m;\n i1 = 2; i2 = 2; i3 = 2; i4 = 2;\n break;\n }\n }\n // if(i1 == 0 and i2 == 1 and i3 == 0 and i4 == -1) cout << endl;\n \n }\n }\n }\n }\n }\n cout << (ok ? \"YES\" : \"NO\") << endl;\n cin >> n >> m;\n }\n \n \n return 0;\n}", "accuracy": 1, "time_ms": 570, "memory_kb": 3480, "score_of_the_acc": -0.645, "final_rank": 15 }, { "submission_id": "aoj_1298_10522094", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n#define all(v) (v).begin(),(v).end()\n#define pb(a) push_back(a)\n#define rep(i, n) for(int i=0;i<n;i++)\n#define foa(e, v) for(auto&& e : v)\nusing ll = long long;\nconst ll MOD7 = 1000000007, MOD998 = 998244353, INF = (1LL << 60);\n#define dout(a) cout<<fixed<<setprecision(10)<<a<<endl;\n\nll cross(ll x1, ll y1, ll x2, ll y2) {\n return x1 * y2 - x2 * y1;\n}\nint main() {\n cin.tie(0);\n ios::sync_with_stdio(false);\n ll n, m; cin >> n >> m;\n ll inf = 1LL << 20;\n while(n) {\n vector<ll> ax(n), ay(n), bx(m), by(m);\n rep(i, n) cin >> ax[i] >> ay[i];\n rep(i, m) cin >> bx[i] >> by[i];\n bool ok = 0;\n rep(i, n) rep(j, m) {\n ll num = max(abs(ax[i] - bx[j]), abs(ay[i] - by[j]));\n num = inf / num;\n ll X1 = (ax[i] - bx[j]) * num + ax[i];\n ll Y1 = (ay[i] - by[j]) * num + ay[i];\n num *= -1;\n num --;\n ll X2 = (ax[i] - bx[j]) * num + ax[i];\n ll Y2 = (ay[i] - by[j]) * num + ay[i];\n \n // (A - B) * num + B\n // (B - A) * num + B\n assert(cross(ax[i] - X1, ay[i] - Y1, X2 - X1, Y2 - Y1) == 0LL);\n for(ll i1 = -1; i1 <= 1; i1 ++) {\n for(ll i2 = -1; i2 <= 1; i2 ++) {\n for(ll i3 = -1; i3 <= 1; i3 ++) {\n for(ll i4 = -1; i4 <= 1; i4 ++) {\n ll x1 = X1 + i1;\n ll y1 = Y1 + i2;\n ll x2 = X2 + i3;\n ll y2 = Y2 + i4;\n // if(i1 == 0 and i2 == 1 and i3 == 0 and i4 == -1) {\n // cout << x1 << \" \" << y1 << \" \" << x2 << \" \" << y2 << endl;\n // }\n {\n bool ng = 0;\n rep(k, n) {\n // if(i1 == 0 and i2 == 1 and i3 == 0 and i4 == -1) cout << cross(ax[k] - x1, ay[k] - y1, x2 - x1, y2 - y1) << \" \";\n if(cross(ax[k] - x1, ay[k] - y1, x2 - x1, y2 - y1) >= 0) {\n ng = 1;\n break;\n }\n }\n if(!ng) rep(k, m) {\n // if(i1 == 0 and i2 == 1 and i3 == 0 and i4 == -1) cout << cross(bx[k] - x1, by[k] - y1, x2 - x1, y2 - y1) << \" \";\n if(cross(bx[k] - x1, by[k] - y1, x2 - x1, y2 - y1) <= 0) {\n ng = 1;\n break;\n }\n }\n if(!ng) {\n ok = 1;\n i = n; j = m;\n i1 = 2; i2 = 2; i3 = 2; i4 = 2;\n break;\n }\n }{\n bool ng = 0;\n rep(k, n) {\n // if(i1 == 0 and i2 == 1 and i3 == 0 and i4 == -1) cout << cross(ax[k] - x1, ay[k] - y1, x2 - x1, y2 - y1) << \" \";\n if(cross(ax[k] - x1, ay[k] - y1, x2 - x1, y2 - y1) <= 0) {\n ng = 1;\n break;\n }\n }\n if(!ng) rep(k, m) {\n // if(i1 == 0 and i2 == 1 and i3 == 0 and i4 == -1) cout << cross(bx[k] - x1, by[k] - y1, x2 - x1, y2 - y1) << \" \";\n if(cross(bx[k] - x1, by[k] - y1, x2 - x1, y2 - y1) >= 0) {\n ng = 1;\n break;\n }\n }\n if(!ng) {\n ok = 1;\n i = n; j = m;\n i1 = 2; i2 = 2; i3 = 2; i4 = 2;\n break;\n }\n }\n // if(i1 == 0 and i2 == 1 and i3 == 0 and i4 == -1) cout << endl;\n \n }\n }\n }\n }\n }\n cout << (ok ? \"YES\" : \"NO\") << endl;\n cin >> n >> m;\n }\n \n \n return 0;\n}", "accuracy": 1, "time_ms": 610, "memory_kb": 3312, "score_of_the_acc": -0.4292, "final_rank": 8 }, { "submission_id": "aoj_1298_10522089", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n#define all(v) (v).begin(),(v).end()\n#define pb(a) push_back(a)\n#define rep(i, n) for(int i=0;i<n;i++)\n#define foa(e, v) for(auto&& e : v)\nusing ll = long long;\nconst ll MOD7 = 1000000007, MOD998 = 998244353, INF = (1LL << 60);\n#define dout(a) cout<<fixed<<setprecision(10)<<a<<endl;\n\nll cross(ll x1, ll y1, ll x2, ll y2) {\n return x1 * y2 - x2 * y1;\n}\nint main() {\n cin.tie(0);\n ios::sync_with_stdio(false);\n ll n, m; cin >> n >> m;\n ll inf = 1LL << 27;\n while(n) {\n vector<ll> ax(n), ay(n), bx(m), by(m);\n rep(i, n) cin >> ax[i] >> ay[i];\n rep(i, m) cin >> bx[i] >> by[i];\n bool ok = 0;\n rep(i, n) rep(j, m) {\n ll num = max(abs(ax[i] - bx[j]), abs(ay[i] - by[j]));\n num = inf / num;\n ll X1 = (ax[i] - bx[j]) * num + ax[i];\n ll X2 = - (ax[i] - bx[j]) * num + bx[j];\n ll Y1 = (ay[i] - by[j]) * num + ay[i];\n ll Y2 = - (ay[i] - by[j]) * num + by[j];\n \n // (A - B) * num + B\n // (B - A) * num + B\n assert(cross(ax[i] - X1, ay[i] - Y1, X2 - X1, Y2 - Y1) == 0LL);\n for(ll i1 = -1; i1 <= 1; i1 ++) {\n for(ll i2 = -1; i2 <= 1; i2 ++) {\n for(ll i3 = -1; i3 <= 1; i3 ++) {\n for(ll i4 = -1; i4 <= 1; i4 ++) {\n ll x1 = X1 + i1;\n ll y1 = Y1 + i2;\n ll x2 = X2 + i3;\n ll y2 = Y2 + i4;\n // if(i1 == 0 and i2 == 1 and i3 == 0 and i4 == -1) {\n // cout << x1 << \" \" << y1 << \" \" << x2 << \" \" << y2 << endl;\n // }\n {\n bool ng = 0;\n rep(k, n) {\n // if(i1 == 0 and i2 == 1 and i3 == 0 and i4 == -1) cout << cross(ax[k] - x1, ay[k] - y1, x2 - x1, y2 - y1) << \" \";\n if(cross(ax[k] - x1, ay[k] - y1, x2 - x1, y2 - y1) >= 0) {\n ng = 1;\n break;\n }\n }\n if(!ng) rep(k, m) {\n // if(i1 == 0 and i2 == 1 and i3 == 0 and i4 == -1) cout << cross(bx[k] - x1, by[k] - y1, x2 - x1, y2 - y1) << \" \";\n if(cross(bx[k] - x1, by[k] - y1, x2 - x1, y2 - y1) <= 0) {\n ng = 1;\n break;\n }\n }\n if(!ng) {\n ok = 1;\n i = n; j = m;\n i1 = 2; i2 = 2; i3 = 2; i4 = 2;\n break;\n }\n }{\n bool ng = 0;\n rep(k, n) {\n // if(i1 == 0 and i2 == 1 and i3 == 0 and i4 == -1) cout << cross(ax[k] - x1, ay[k] - y1, x2 - x1, y2 - y1) << \" \";\n if(cross(ax[k] - x1, ay[k] - y1, x2 - x1, y2 - y1) <= 0) {\n ng = 1;\n break;\n }\n }\n if(!ng) rep(k, m) {\n // if(i1 == 0 and i2 == 1 and i3 == 0 and i4 == -1) cout << cross(bx[k] - x1, by[k] - y1, x2 - x1, y2 - y1) << \" \";\n if(cross(bx[k] - x1, by[k] - y1, x2 - x1, y2 - y1) >= 0) {\n ng = 1;\n break;\n }\n }\n if(!ng) {\n ok = 1;\n i = n; j = m;\n i1 = 2; i2 = 2; i3 = 2; i4 = 2;\n break;\n }\n }\n // if(i1 == 0 and i2 == 1 and i3 == 0 and i4 == -1) cout << endl;\n \n }\n }\n }\n }\n }\n cout << (ok ? \"YES\" : \"NO\") << endl;\n cin >> n >> m;\n }\n \n \n return 0;\n}", "accuracy": 1, "time_ms": 580, "memory_kb": 3308, "score_of_the_acc": -0.4163, "final_rank": 6 }, { "submission_id": "aoj_1298_10521926", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n#define all(v) (v).begin(),(v).end()\n#define pb(a) push_back(a)\n#define rep(i, n) for(int i=0;i<n;i++)\n#define foa(e, v) for(auto&& e : v)\nusing ll = long long;\nconst ll MOD7 = 1000000007, MOD998 = 998244353, INF = (1LL << 60);\n#define dout(a) cout<<fixed<<setprecision(10)<<a<<endl;\n\nll cross(ll x1, ll y1, ll x2, ll y2) {\n return x1 * y2 - x2 * y1;\n}\nint main() {\n cin.tie(0);\n ios::sync_with_stdio(false);\n ll n, m; cin >> n >> m;\n ll inf = 1LL << 28;\n while(n) {\n vector<ll> ax(n), ay(n), bx(m), by(m);\n rep(i, n) cin >> ax[i] >> ay[i];\n rep(i, m) cin >> bx[i] >> by[i];\n bool ok = 0;\n rep(i, n) rep(j, m) {\n ll num = max(abs(ax[i] - bx[j]), abs(ay[i] - by[j]));\n num = inf / num;\n ll X1 = (ax[i] - bx[j]) * num + bx[j];\n ll X2 = - (ax[i] - bx[j]) * num + ax[i];\n ll Y1 = (ay[i] - by[j]) * num + by[j];\n ll Y2 = - (ay[i] - by[j]) * num + ay[i];\n for(ll i1 = -1; i1 <= 1; i1 ++) {\n for(ll i2 = -1; i2 <= 1; i2 ++) {\n for(ll i3 = -1; i3 <= 1; i3 ++) {\n for(ll i4 = -1; i4 <= 1; i4 ++) {\n ll x1 = X1 + i1;\n ll y1 = Y1 + i2;\n ll x2 = X2 + i3;\n ll y2 = Y2 + i4;\n // if(i1 == 0 and i2 == 1 and i3 == 0 and i4 == -1) {\n // cout << x1 << \" \" << y1 << \" \" << x2 << \" \" << y2 << endl;\n // }\n {\n bool ng = 0;\n rep(k, n) {\n // if(i1 == 0 and i2 == 1 and i3 == 0 and i4 == -1) cout << cross(ax[k] - x1, ay[k] - y1, x2 - x1, y2 - y1) << \" \";\n if(cross(ax[k] - x1, ay[k] - y1, x2 - x1, y2 - y1) >= 0) {\n ng = 1;\n break;\n }\n }\n if(!ng) rep(k, m) {\n // if(i1 == 0 and i2 == 1 and i3 == 0 and i4 == -1) cout << cross(bx[k] - x1, by[k] - y1, x2 - x1, y2 - y1) << \" \";\n if(cross(bx[k] - x1, by[k] - y1, x2 - x1, y2 - y1) <= 0) {\n ng = 1;\n break;\n }\n }\n if(!ng) {\n ok = 1;\n i = n; j = m;\n i1 = 2; i2 = 2; i3 = 2; i4 = 2;\n break;\n }\n }{\n bool ng = 0;\n rep(k, n) {\n // if(i1 == 0 and i2 == 1 and i3 == 0 and i4 == -1) cout << cross(ax[k] - x1, ay[k] - y1, x2 - x1, y2 - y1) << \" \";\n if(cross(ax[k] - x1, ay[k] - y1, x2 - x1, y2 - y1) <= 0) {\n ng = 1;\n break;\n }\n }\n if(!ng) rep(k, m) {\n // if(i1 == 0 and i2 == 1 and i3 == 0 and i4 == -1) cout << cross(bx[k] - x1, by[k] - y1, x2 - x1, y2 - y1) << \" \";\n if(cross(bx[k] - x1, by[k] - y1, x2 - x1, y2 - y1) >= 0) {\n ng = 1;\n break;\n }\n }\n if(!ng) {\n ok = 1;\n i = n; j = m;\n i1 = 2; i2 = 2; i3 = 2; i4 = 2;\n break;\n }\n }\n // if(i1 == 0 and i2 == 1 and i3 == 0 and i4 == -1) cout << endl;\n \n }\n }\n }\n }\n }\n cout << (ok ? \"YES\" : \"NO\") << endl;\n cin >> n >> m;\n }\n \n \n return 0;\n}", "accuracy": 1, "time_ms": 560, "memory_kb": 3384, "score_of_the_acc": -0.5135, "final_rank": 11 }, { "submission_id": "aoj_1298_10521924", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n#define all(v) (v).begin(),(v).end()\n#define pb(a) push_back(a)\n#define rep(i, n) for(int i=0;i<n;i++)\n#define foa(e, v) for(auto&& e : v)\nusing ll = long long;\nconst ll MOD7 = 1000000007, MOD998 = 998244353, INF = (1LL << 60);\n#define dout(a) cout<<fixed<<setprecision(10)<<a<<endl;\n\nll cross(ll x1, ll y1, ll x2, ll y2) {\n return x1 * y2 - x2 * y1;\n}\nint main() {\n cin.tie(0);\n ios::sync_with_stdio(false);\n ll n, m; cin >> n >> m;\n ll inf = 1LL << 28;\n while(n) {\n vector<ll> ax(n), ay(n), bx(m), by(m);\n rep(i, n) cin >> ax[i] >> ay[i];\n rep(i, m) cin >> bx[i] >> by[i];\n bool ok = 0;\n rep(i, n) rep(j, m) {\n ll num = max(abs(ax[i] - bx[j]), abs(ay[i] - by[j]));\n num = inf / num;\n ll X1 = (ax[i] - bx[j]) * num + bx[j];\n ll X2 = -(ax[i] - bx[j]) * num + ax[i];\n ll Y1 = (ay[i] - by[j]) * num + by[j];\n ll Y2 = -(ay[i] - by[j]) * num + ay[i];\n assert(cross(ax[i] - X1, ay[i] - Y1, X2 - X1, Y2 - Y1) == 0);\n assert(cross(bx[j] - X1, by[j] - Y1, X2 - X1, Y2 - Y1) == 0);\n for(ll i1 = -1; i1 <= 1; i1 ++) {\n for(ll i2 = -1; i2 <= 1; i2 ++) {\n for(ll i3 = -1; i3 <= 1; i3 ++) {\n for(ll i4 = -1; i4 <= 1; i4 ++) {\n ll x1 = X1 + i1;\n ll y1 = Y1 + i2;\n ll x2 = X2 + i3;\n ll y2 = Y2 + i4;\n // if(i1 == 0 and i2 == 1 and i3 == 0 and i4 == -1) {\n // cout << x1 << \" \" << y1 << \" \" << x2 << \" \" << y2 << endl;\n // }\n {\n bool ng = 0;\n rep(k, n) {\n // if(i == 0 and j == 0 and i1 == -1 and i2 == 0 and i3 == 0 and i4 == -1) cout << ax[k] - x1 << \" \" << ay[k] - y1 << \" \" << x2 - x1 << \" \" << y2 - y1 << \" \" << cross(ax[k] - x1, ay[k] - y1, x2 - x1, y2 - y1) << endl;\n if(cross(ax[k] - x1, ay[k] - y1, x2 - x1, y2 - y1) <= 0) {\n ng = 1;\n break;\n }\n }\n if(!ng) rep(k, m) {\n // if(i == 0 and j == 0 and i1 == -1 and i2 == 0 and i3 == 0 and i4 == -1) cout << bx[k] - x1 << \" \" << by[k] - y1 << \" \" << x2 - x1 << \" \" << y2 - y1 << \" \" << cross(bx[k] - x1, by[k] - y1, x2 - x1, y2 - y1) << endl;\n if(cross(bx[k] - x1, by[k] - y1, x2 - x1, y2 - y1) >= 0) {\n ng = 1;\n break;\n }\n }\n if(!ng) {\n ok = 1;\n i = n; j = m;\n i1 = 2; i2 = 2; i3 = 2; i4 = 2;\n break;\n }\n }{\n bool ng = 0;\n rep(k, n) {\n if(cross(ax[k] - x1, ay[k] - y1, x2 - x1, y2 - y1) >= 0) {\n ng = 1;\n break;\n }\n }\n if(!ng) rep(k, m) {\n if(cross(bx[k] - x1, by[k] - y1, x2 - x1, y2 - y1) <= 0) {\n ng = 1;\n break;\n }\n }\n if(!ng) {\n ok = 1;\n i = n; j = m;\n i1 = 2; i2 = 2; i3 = 2; i4 = 2;\n break;\n }\n }\n \n }\n }\n }\n }\n }\n cout << (ok ? \"YES\" : \"NO\") << endl;\n cin >> n >> m;\n }\n \n \n return 0;\n}", "accuracy": 1, "time_ms": 600, "memory_kb": 3396, "score_of_the_acc": -0.5396, "final_rank": 13 }, { "submission_id": "aoj_1298_10276183", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\n#include <bits/stdc++.h>\nusing namespace std;\n\nnamespace geometry {\n\nusing Real = double;\nconstexpr Real EPS = 1e-7;\nconstexpr Real PI = numbers::pi;\n\nconstexpr int sgn(Real a) { return (a < -EPS) ? -1 : (EPS < a) ? 1 : 0; }\n\nstruct Point {\n Real x, y;\n constexpr Point(Real x=0, Real y=0) : x(x), y(y) {}\n\n constexpr Point& operator += (const Point& rhs) { return x += rhs.x, y += rhs.y, *this; }\n constexpr Point& operator -= (const Point& rhs) { return x -= rhs.x, y -= rhs.y, *this; }\n constexpr Point& operator *= (const Point& rhs) { return *this = Point(x * rhs.x - y * rhs.y, x * rhs.y + y * rhs.x); }\n constexpr Point& operator /= (const Point& rhs) { return *this *= rhs.conj() * rhs.norm2(); }\n constexpr Point& operator *= (Real k) { return x *= k, y *= k, *this; }\n constexpr Point& operator /= (Real k) { return x /= k, y /= k, *this; }\n\n constexpr Point operator + (const Point& rhs) const { return Point(*this) += rhs; }\n constexpr Point operator - (const Point& rhs) const { return Point(*this) -= rhs; }\n constexpr Point operator * (const Point& rhs) const { return Point(*this) *= rhs; }\n constexpr Point operator / (const Point& rhs) const { return Point(*this) /= rhs; }\n constexpr Point operator * (Real k) const { return Point(*this) *= k; }\n constexpr Point operator / (Real k) const { return Point(*this) /= k; }\n friend constexpr Point operator * (Real k, const Point& p) { return Point(p.x * k, p.y * k); }\n\n constexpr Point operator - () const { return Point() - *this; }\n\n constexpr bool operator < (const Point& rhs) const { return sgn(x - rhs.x) ? (x < rhs.x) : (y < rhs.y); }\n constexpr bool operator > (const Point& rhs) const { return rhs < *this; }\n constexpr bool operator == (const Point& rhs) const { return !sgn(x - rhs.x) && !sgn(y - rhs.y); }\n constexpr bool operator != (const Point& rhs) const { return !(*this == rhs); }\n\n constexpr Real norm2() const { return x * x + y * y; }\n constexpr Real norm() const { return sqrt(norm2()); }\n constexpr Real arg() const { return atan2(y, x); }\n constexpr Point rot(Real theta) const { return Point(x * cos(theta) - y * sin(theta), x * sin(theta) + y * cos(theta)); }\n constexpr Point rot90() const { return Point(-y, x); }\n constexpr Point conj() const { return Point(x, -y); }\n\n friend constexpr Real norm2(const Point& p) { return p.norm2(); }\n friend constexpr Real norm(const Point& p) { return p.norm(); }\n friend constexpr Real arg(const Point& p) { return p.arg(); }\n friend constexpr Point rot(const Point& p, Real theta) { return p.rot(theta); }\n friend constexpr Point rot90(const Point& p) { return p.rot90(); }\n friend constexpr Point conj(const Point& p) { return p.conj(); }\n\n friend istream& operator >> (istream& is, Point& p) { return is >> p.x >> p.y; }\n friend ostream& operator << (ostream& os, const Point& p) { return os << \"(\" << p.x << \", \" << p.y << \")\"; }\n};\n\nconstexpr Real dot(const Point& a, const Point& b) { return a.x * b.x + a.y * b.y; }\nconstexpr Real cross(const Point& a, const Point& b) { return a.x * b.y - a.y * b.x; }\nconstexpr Point polar(Real r, Real theta) { return Point(r * cos(theta), r * sin(theta)); }\n\n// +1 : a - b, a - c : ccw\n// -1 : a - b, a - c : cw\n// +2 : c - a - b : on_back\n// -2 : a - b - c : on_front\n// 0 : a - c - b : on_segment\nint ccw(const Point& a, Point b, Point c) {\n b -= a, c -= a;\n if(sgn(cross(b, c)) == 1) return 1;\n if(sgn(cross(b, c)) == -1) return -1;\n if(sgn(dot(b, c)) == -1) return 2;\n if(b.norm2() < c.norm2()) return -2;\n return 0; \n}\n\nstruct Line {\n Point s, t;\n constexpr Line(Point s=Point(0, 0), Point t=Point(0, 0)) : s(s), t(t) {}\n constexpr Point dir() const { return t - s; }\n constexpr Real norm() const { return dir().norm(); }\n constexpr Point normalize() const { return (t - s) / norm(); }\n\n friend constexpr Point dir(const Line& l) { return l.dir(); }\n friend constexpr Real norm(const Line& l) { return l.norm(); }\n friend constexpr Point normalize(const Line& l) { return l.normalize(); }\n};\n\nPoint projection(const Line& l, const Point& p) {\n Real t = dot(p - l.s, l.t - l.s) / norm2(l.t - l.s);\n return l.s + l.dir() * t;\n}\nPoint reflection(const Line& l, const Point& p) {\n Point x = projection(l, p);\n return 2 * x - p;\n}\n\nbool is_parallel(const Line& a, const Line& b) { return sgn(cross(a.dir(), b.dir())) == 0; }\nbool is_orthogonal(const Line& a, const Line& b) { return sgn(dot(a.dir(), b.dir())) == 0; }\nbool is_intersect(const Line& a, const Line& b) { \n return sgn(ccw(a.s, a.t, b.s)) * sgn(ccw(a.s, a.t, b.t)) <= 0 && sgn(ccw(b.s, b.t, a.s)) * sgn(ccw(b.s, b.t, a.t)) <= 0; \n}\n\nPoint cross_ll(const Line& a, const Line& b) {\n Real d1 = cross(a.dir(), b.dir());\n Real d2 = cross(a.dir(), a.t - b.s);\n return b.s + b.dir() * (d2 / d1);\n}\n\nReal dist_pp(const Point& a, const Point& b) {\n return norm(a - b);\n}\nReal dist_lp(const Line& l, const Point& p) {\n return abs(cross(l.dir(), p - l.s)) / norm(l);\n}\nReal dist_sp(const Line& l, const Point& p) {\n if(sgn(dot(l.dir(), p - l.s)) == -1) return norm(p - l.s);\n if(sgn(dot(-l.dir(), p - l.t)) == -1) return norm(p - l.t);\n return dist_lp(l, p);\n}\nReal dist_ss(const Line& a, const Line& b) {\n if(is_intersect(a, b)) return 0;\n return min({dist_sp(a, b.s), dist_sp(a, b.t), dist_sp(b, a.s), dist_sp(b, a.t)});\n}\n\nstruct Polygon : public vector<Point> {\n using vector<Point>::vector;\n Real area() const {\n int sz = size();\n Real ret = 0;\n for(int i = 0; i < sz; i++) {\n ret += cross((*this)[i], (*this)[(i + 1) % sz]);\n }\n return ret / 2;\n }\n\n bool is_convex() const {\n int sz = size();\n for(int i = 0; i < sz; i++) {\n if(ccw((*this)[i], (*this)[(i + 1) % sz], (*this)[(i + 2) % sz]) == -1) return false;\n }\n return true;\n }\n\n pair<int, int> diameter() const {\n assert(is_convex());\n int sz = size();\n int right = max_element(begin(), end()) - begin();\n int left = min_element(begin(), end()) - begin();\n Real max_dist = norm2((*this)[left] - (*this)[right]);\n pair<int, int> ret = {left, right};\n for(int i = 0; i < sz; i++) {\n Point pre = (*this)[(left + 1) % sz] - (*this)[left];\n Point nxt = (*this)[right] - (*this)[(right + 1) % sz];\n if(ccw(Point(0, 0), pre, nxt) == 1) left = (left + 1) % sz;\n else right = (right + 1) % sz;\n if(norm2((*this)[left] - (*this)[right]) > max_dist) max_dist = norm2((*this)[left] - (*this)[right]), ret = {left, right};\n }\n return ret;\n }\n\n friend Real area(const Polygon& pol) { return pol.area(); }\n friend bool is_convex(const Polygon& pol) { return pol.is_convex(); }\n friend pair<int, int> diameter(const Polygon& pol) { return pol.diameter(); } \n};\n\n// 2 : contain\n// 1 : on line\n// 0 : outside\nint contain(const Polygon& pol, const Point& p) {\n bool in = false;\n int sz = pol.size();\n for(int i = 0; i < sz; i++) {\n Point a = pol[i] - p;\n Point b = pol[(i + 1) % sz] - p;\n if(a.y > b.y) swap(a, b);\n if(sgn(a.y) <= 0 && sgn(b.y) == 1 && sgn(cross(a, b)) == -1) in ^= 1;\n if(sgn(cross(a, b)) == 0 && sgn(dot(a, b)) == -1) return 1;\n }\n return 2 * in;\n}\n\nPolygon convex_hull(Polygon pol) {\n int sz = pol.size();\n sort(pol.begin(), pol.end());\n Polygon ret;\n for(int i = 0; i < sz; i++) {\n while(ret.size() > 1 && ccw(ret[ret.size() - 2], ret.back(), pol[i]) == -1) ret.pop_back();\n ret.push_back(pol[i]);\n }\n int t = ret.size();\n for(int i = sz - 2; i >= 0; i--) {\n while(ret.size() > t && ccw(ret[ret.size() - 2], ret.back(), pol[i]) == -1) ret.pop_back();\n ret.push_back(pol[i]);\n }\n ret.pop_back();\n return ret;\n}\n\nPolygon convex_cut(const Polygon& pol, const Line& l) {\n int sz = pol.size();\n Polygon ret;\n for(int i = 0; i < pol.size(); i++) {\n int t1 = sgn(cross(l.dir(), pol[i] - l.s)), t2 = sgn(cross(l.dir(), pol[(i + 1) % sz] - l.s));\n if(t1 >= 0) ret.push_back(pol[i]); \n if(t1 * t2 < 0) ret.push_back(cross_ll(Line(pol[i], pol[(i + 1) % sz]), l));\n }\n return ret;\n}\n\nReal closest_pair(vector<Point> ps) {\n auto rec = [](auto f, vector<Point>& ps, int l, int r) -> Real {\n if(r - l < 2) return 1e100;\n int mid = (l + r) / 2;\n Real x = ps[mid].x;\n Real d = min(f(f, ps, l, mid), f(f, ps, mid, r));\n auto it = ps.begin(), itl = it + l, itm = it + mid, itr = it + r;\n inplace_merge(itl, itm, itr, [](Point a, Point b) { return a.y < b.y; });\n vector<Point> near_line;\n for(int i = l; i < r; i++) {\n if(abs(ps[i].x - x) >= d) continue;\n int sz = near_line.size();\n for(int j = sz; j--;) {\n if(ps[i].y - near_line[j].y >= d) break;\n d = min(d, dist_pp(ps[i], near_line[j]));\n }\n near_line.push_back(ps[i]);\n }\n return d;\n };\n sort(ps.begin(), ps.end());\n return rec(rec, ps, 0, ps.size());\n}\n\nstruct Circle {\n Point p;\n Real r;\n Circle() : p(Point(0, 0)), r(0) {}\n Circle(Point p, Real r) : p(p), r(r) {}\n};\n\nint intersection(const Circle& c1, const Circle& c2) {\n Real d = dist_pp(c1.p, c2.p);\n if(sgn(d - (c1.r + c2.r)) == 1) return 4;\n if(sgn(d - (c1.r + c2.r)) == 0) return 3;\n if(sgn(d - abs(c1.r - c2.r)) == 1) return 2;\n if(sgn(d - abs(c1.r - c2.r)) == 0) return 1;\n return 0;\n}\n\npair<Point, Point> cross_cl(const Circle& c, const Line& l) {\n Point proj = projection(l, c.p);\n Real h = dist_lp(l, c.p);\n Real d = sqrt(c.r * c.r - h * h);\n Point p1 = proj + l.normalize() * d;\n Point p2 = proj - l.normalize() * d;\n if(dist_pp(p1, l.s) > dist_pp(p2, l.s)) swap(p1, p2);\n return {p1, p2};\n}\n\nCircle incircle(const Point& a, const Point& b, const Point& c) {\n Real d1 = dist_pp(b, c);\n Real d2 = dist_pp(c, a);\n Real d3 = dist_pp(a, b);\n Point ip = (a * d1 + b * d2 + c * d3) / (d1 + d2 + d3);\n Real r = dist_lp(Line(a, b), ip);\n return Circle(ip, r);\n}\n\nCircle outcircle(const Point& a, const Point& b, const Point& c) {\n Line l1((a + b) / 2, (a + b) / 2 + rot90(b - a));\n Line l2((b + c) / 2, (b + c) / 2 + rot90(c - b));\n Point op = cross_ll(l1, l2);\n Real r = dist_pp(op, a);\n return Circle(op, r);\n}\n\npair<Point, Point> cross_cc(const Circle& c1, const Circle& c2) {\n Real d = dist_pp(c1.p, c2.p);\n Real theta = acos((c1.r * c1.r + d * d - c2.r * c2.r) / (2 * c1.r * d));\n Real phi = arg(c2.p - c1.p);\n Point p1 = c1.p + polar(c1.r, phi - theta);\n Point p2 = c1.p + polar(c1.r, phi + theta);\n if(p1 > p2) swap(p1, p2);\n return {p1, p2};\n}\n\npair<Point, Point> tangent_cp(const Circle& c, const Point& p) {\n return cross_cc(c, Circle(p, sqrt(norm2(c.p - p) - c.r * c.r)));\n}\n\nvector<Line> tangent_cc(const Circle& c1, const Circle& c2) {\n vector<Line> ret;\n Real d = dist_pp(c1.p, c2.p);\n if(sgn(d) == 0) return {};\n Point u = Line(c1.p, c2.p).normalize();\n Point v = rot90(u);\n for(int s : {-1, 1}) {\n Real h = (c1.r + c2.r * s) / d;\n if(sgn(h * h - 1) == 0) ret.push_back(Line(c1.p + h * u * c1.r, c1.p + h * u * c1.r + v));\n else if(sgn(h * h - 1) == -1) {\n Point U = u * h;\n Point V = v * sqrt(1 - h * h);\n ret.push_back(Line(c1.p + (U + V) * c1.r, c2.p - (U + V) * c2.r * s));\n ret.push_back(Line(c1.p + (U - V) * c1.r, c2.p - (U - V) * c2.r * s));\n } \n }\n return ret;\n}\n\nReal get_angle(const Point& a, const Point& b) {\n return (b * conj(a)).arg();\n}\n\nReal common_area_cp(const Circle& c, const Polygon& pol) {\n int sz = pol.size();\n auto cross_area = [&](const Point& a, const Point& b) -> Real {\n Point va = a - c.p, vb = b - c.p;\n Real f = cross(va, vb), ret = 0;\n if(sgn(f) == 0) return 0;\n if(sgn(c.r - max(norm(va), norm(vb))) >= 0) return f;\n if(sgn(dist_sp(Line(a, b), c.p) - c.r) >= 0) return c.r * c.r * get_angle(va, vb);\n auto [cp1, cp2] = cross_cl(c, Line(a, b));\n cp1 -= c.p, cp2 -= c.p;\n if(ccw(va, vb, cp1) != 0) cp1 = cp2;\n if(ccw(va, vb, cp2) != 0) cp2 = cp1;\n ret += (sgn(c.r - norm(va)) >= 0) ? cross(va, cp1) : c.r * c.r * get_angle(va, cp1);\n ret += cross(cp1, cp2);\n ret += (sgn(c.r - norm(vb)) >= 0) ? cross(cp2, vb) : c.r * c.r * get_angle(cp2, vb);\n return ret;\n };\n Real ret = 0;\n for(int i = 0; i < sz; i++) {\n ret += cross_area(pol[i], pol[(i + 1) % sz]);\n }\n return ret * 0.5;\n}\n\nReal common_area_cc(const Circle& c1, const Circle& c2) {\n Real d = dist_pp(c1.p, c2.p);\n if(sgn(c1.r + c2.r - d) <= 0) return 0;\n if(sgn(d - abs(c1.r - c2.r)) <= 0) {\n Real r = min(c1.r, c2.r);\n return PI * r * r;\n }\n auto area = [&](const Circle& c1, const Circle& c2) -> Real {\n Real theta = 2 * acos((d * d + c1.r * c1.r - c2.r * c2.r) / (2 * d * c1.r));\n return (theta - sin(theta)) * c1.r * c1.r * 0.5;\n };\n return area(c1, c2) + area(c2, c1);\n}\n\n} // namespace geometry\n\nusing namespace geometry;\n\n\nint main() {\n cin.tie(nullptr);\n ios::sync_with_stdio(false);\n while(true) {\n int N, M;\n cin >> N >> M;\n if(N == 0) break;\n Polygon black(N), white(M);\n for(int i = 0; i < N; i++) {\n cin >> black[i];\n }\n for(int i = 0; i < M; i++) {\n cin >> white[i];\n }\n\n auto convex_black = convex_hull(black);\n auto convex_white = convex_hull(white);\n\n bool yes = true;\n for(int i = 0; i < N; i++) {\n if(contain(convex_white, black[i])) yes = false;\n }\n for(int i = 0; i < M; i++) {\n if(contain(convex_black, white[i])) yes = false;\n }\n\n for(int i = 0; i < (int)convex_black.size(); i++) {\n for(int j = 0; j < (int)convex_white.size(); j++) {\n Line l1(convex_black[i], convex_black[(i + 1) % convex_black.size()]);\n Line l2(convex_white[j], convex_white[(j + 1) % convex_white.size()]);\n if(is_intersect(l1, l2)) yes = false;\n }\n }\n\n if(yes) cout << \"YES\\n\";\n else cout << \"NO\\n\";\n }\n\n return 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3712, "score_of_the_acc": -0.8172, "final_rank": 16 }, { "submission_id": "aoj_1298_10009879", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n#define rep(i,n) for(ll i=0;i<n;i++)\n#define rep2(i,s,t) for(ll i=s;i<t;i++)\n#define all(A) A.begin(),A.end()\nusing ll=long long;\n\n/*\n- `Point p(x, y)` で座標/ベクトルを宣言できる\n- `cout << fixed << setprecision(10);` で小数出力の桁数を指定できる\n*/\n\nusing Real = double;\nusing Point = complex<Real>;\nusing Poly = vector<Point>;\nReal eps = 1e-6;\n\nint Round(Real x) { return static_cast<int>(round(x)); }\n\n// 同じか判定\nbool equal(Point a, Point b) { return abs(a - b) < eps; }\n\nstruct Line {\n // 点 a, b を通る直線\n Point a, b;\n Line() = default;\n Line(Point a, Point b) : a(a), b(b) {}\n Line(Real A, Real B, Real C) {\n // 直線の方程式 Ax + By = C;\n if(equal(A, 0)) a = Point(0, C / B), b = Point(1, C / B);\n else if(equal(B, 0)) a = Point(C / A, 0), b = Point(C / A, 1);\n else a = Point(0, C / B), b = Point(C / A, 0);\n }\n};\n\nusing Segment = Line;\n\nstruct Circle {\n Point c; // 中心\n Real r; // 半径\n Circle() = default;\n Circle(Point c, Real r) : c(c), r(r) {}\n};\n\nReal norm(Point p) { return pow(p.real(), 2) + pow(p.imag(), 2); }\n// 単位ベクトル\nPoint unitVector(Point p) { return p / abs(p); }\n// 内積(dot product): a・b=|a||b|cosΘ\nReal dot(Point a, Point b) { return a.real() * b.real() + a.imag() * b.imag(); }\n// 外積(cross product): a×b=|a||b|sinΘ\nReal cross(Point a, Point b) {\n return a.real() * b.imag() - a.imag() * b.real();\n}\n// 点 p を反時計回りに theta(rad) 回転\nPoint rotate(Point p, Real theta) {\n Real x = cos(theta) * p.real() - sin(theta) * p.imag();\n Real y = sin(theta) * p.real() + cos(theta) * p.imag();\n return {x, y};\n}\n\n// 射影(projection): 直線 L に点 p から引いた垂線の足を求める\nPoint projection(Line L, Point p) {\n Real t = dot(p - L.a, L.a - L.b) / norm(L.a - L.b);\n return L.a + (L.a - L.b) * t;\n}\n\n// 反射(reflection): 直線 L を対称軸として点 p と線対称の位置にある点を求める\nPoint reflection(Line L, Point p) { return p + (projection(L, p) - p) * 2.0; }\n\nint ccw(Point a, Point b, Point c) {\n // a,b,c が反時計回り : 1\n // a,b,c が時計回り : -1\n // c,a,b がこの順で同一直線上 : 2\n // a,b,c がこの順で同一直線上 : -2\n // b,c,a がこの順で同一直線上 : 0\n Point p = b - a;\n Point q = c - a;\n\n if(cross(p, q) > eps) return 1;\n if(cross(p, q) < -eps) return -1;\n if(dot(p, q) < 0) return 2;\n if(norm(p) < norm(q)) return -2;\n return 0;\n}\n\n// 2 直線の直交判定\nbool isOrthiogonal(Line L1, Line L2) {\n return equal(dot(L1.b - L1.a, L2.b - L2.a), 0);\n}\n// 2 直線の平行判定\nbool isParallel(Line L1, Line L2) {\n return equal(cross(L1.b - L1.a, L2.b - L2.a), 0);\n}\n// 線分の交差判定\nbool isIntersect(Segment s, Segment t) {\n rep(i, 2) {\n if(ccw(s.a, s.b, t.a) * ccw(s.a, s.b, t.b) > 0) return false;\n swap(s, t);\n }\n return true;\n}\n\n// 2 直線の交点\nPoint crossPoint(Line s, Line t) {\n Real d1 = cross(s.b - s.a, t.b - t.a);\n Real d2 = cross(s.b - s.a, s.b - t.a);\n if(equal(abs(d1), 0.0) && equal(abs(d2), 0.0)) return t.a;\n return t.a + (t.b - t.a) * (d2 / d1);\n}\n\n// 直線 L と点 p の距離\nReal distLinePoint(Line L, Point p) {\n return abs(cross(L.b - L.a, p - L.a)) / abs(L.b - L.a);\n}\n// 線分 L と点 p の距離\nReal distSegPoint(Segment L, Point p) {\n if(dot(L.b - L.a, p - L.a) < eps) return abs(p - L.a);\n if(dot(L.a - L.b, p - L.b) < eps) return abs(p - L.b);\n return abs(cross(L.b - L.a, p - L.a)) / abs(L.b - L.a);\n}\n// 線分同士の距離\nReal distSegSeg(Segment s, Segment t) {\n if(isIntersect(s, t)) return 0;\n Real d = distSegPoint(s, t.a);\n d = min(d, distSegPoint(s, t.b));\n d = min(d, distSegPoint(t, s.a));\n d = min(d, distSegPoint(t, s.b));\n return d;\n}\n\n// 多角形の面積\nReal PolyArea(Poly p) {\n int n = p.size();\n Real res = 0;\n rep(i, n) { res += cross(p[i], p[(i + 1) % n]); }\n return res / 2;\n}\n\n// 凸判定\nbool isConvex(Poly p) {\n int n = p.size();\n rep(i, n) {\n if(ccw(p[i], p[(i + 1) % n], p[(i + 2) % n]) == -1) return false;\n }\n return true;\n}\n\n// 凸包\nPoly ConvexHull(Poly p) {\n int n = p.size();\n if(n <= 2) return p;\n sort(all(p), [&](Point a, Point b) {\n return !equal(real(a), real(b)) ? real(a) < real(b) : imag(a) < imag(b);\n });\n int sz = 0;\n Poly CH(2 * n);\n rep(i, n) {\n // 同一直線上の3点を含める -> (<-eps), 含めない -> (<eps)\n while(sz > 1 && cross(CH[sz - 1] - CH[sz - 2], p[i] - CH[sz - 1]) < -eps)\n sz--;\n CH[sz] = p[i];\n sz++;\n }\n int t = sz;\n for(int i = n - 2; i >= 0; i--) {\n while(sz > t && cross(CH[sz - 1] - CH[sz - 2], p[i] - CH[sz - 1]) < -eps)\n sz--;\n CH[sz] = p[i];\n sz++;\n }\n CH.resize(sz - 1);\n return CH;\n}\n\n// 多角形 g に点 p が含まれるか? 含む:2, 辺上:1, 含まない:0\nint isContain(Poly g, Point p) {\n int IN = 0;\n int n = g.size();\n rep(i, n) {\n Point a = g[i] - p;\n Point b = g[(i + 1) % n] - p;\n if(a.imag() > b.imag()) swap(a, b);\n if(imag(a) < eps && eps < imag(b) && cross(a, b) < -eps) IN ^= 1;\n if(equal(cross(a, b), 0) && dot(a, b) < eps) return 1;\n }\n return 2 * IN;\n}\n\n// 凸多角形の直径(最遠頂点対間距離)\nReal ConvexDiametar(Poly p) {\n int n = p.size();\n int i0 = 0, j0 = 0;\n rep2(i, 1, n) {\n if(imag(p[i]) > imag(p[i0])) i0 = i;\n if(imag(p[i]) < imag(p[j0])) j0 = i;\n }\n Real mx = abs(p[i0] - p[j0]);\n int i = i0, j = j0;\n while(i != j0 || j != i0) {\n if(cross(p[(i + 1) % n] - p[i], p[(j + 1) % n] - p[j]) >= 0)\n j = (j + 1) % n;\n else i = (i + 1) % n;\n mx = max(mx, abs(p[i] - p[j]));\n }\n return mx;\n}\n\n// 凸多角形の切断: 直線 L.a -> L.b で切断して,その左側の凸多角形を返す\nPoly ConvexCut(Poly p, Line L) {\n Poly res;\n int n = p.size();\n rep(i, n) {\n Point now = p[i], nxt = p[(i + 1) % n];\n if(ccw(L.a, L.b, now) != -1) res.push_back(now);\n if(ccw(L.a, L.b, now) * ccw(L.a, L.b, nxt) < 0)\n res.push_back(crossPoint(Line(now, nxt), L));\n }\n return res;\n}\n\nint IsIntersectCircle(Circle c1, Circle c2) {\n Real d = abs(c1.c - c2.c);\n if(d > c1.r + c2.r + eps) return 4; // 2 円が離れている\n if(equal(d, c1.r + c2.r)) return 3; // 外接\n if(equal(d, abs(c1.r - c2.r))) return 1; // 内接\n if(d < abs(c1.r - c2.r) - eps) return 0; // 内包\n return 2; // 2 交点を持つ\n}\n\n// 三角形の内接円\nCircle InscribedCircle(Point a, Point b, Point c) {\n Real A = abs(b - c), B = abs(c - a), C = abs(b - a);\n Point p(A * real(a) + B * real(b) + C * real(c),\n A * imag(a) + B * imag(b) + C * imag(c));\n p /= (A + B + C);\n Real r = distLinePoint(Line(a, b), p);\n return Circle(p, r);\n}\n\n// 三角形の外接円\nCircle CircumscribedCircle(Point a, Point b, Point c) {\n Point p = (a - b) * norm(c) + (b - c) * norm(a) + (c - a) * norm(b);\n p /= (a - b) * conj(c) + (b - c) * conj(a) + (c - a) * conj(b);\n Real r = abs(p - a);\n return Circle(p, r);\n}\n\n// 円 C と直線Lの交点\nvector<Point> CrossPointCircleLine(Circle C, Line L) {\n Real d = distLinePoint(L, C.c);\n if(d > C.r + eps) return {};\n Point h = projection(L, C.c);\n if(abs(d - C.r) < eps) return {h};\n Point e = unitVector(L.b - L.a);\n Real ph = sqrt(C.r * C.r - d * d);\n return {h - e * ph, h + e * ph};\n}\n\n// 2 円の交点\nvector<Point> CrossPointCircles(Circle C1, Circle C2) {\n int t = IsIntersectCircle(C1, C2);\n Real d = abs(C1.c - C2.c);\n if(t == 4 || t == 0) return {};\n if(t == 3) return {C1.c + (C2.c - C1.c) * (C1.r / (C1.r + C2.r))};\n if(t == 1) {\n if(C2.r < C1.r - eps) return {C1.c + (C2.c - C1.c) * (C1.r / d)};\n else return {C2.c + (C1.c - C2.c) * (C2.r / d)};\n }\n Real rc1 = (C1.r * C1.r + d * d - C2.r * C2.r) / (2 * d);\n Real rs1 = sqrt(C1.r * C1.r - rc1 * rc1);\n if(C1.r - abs(rc1) < eps) rs1 = 0;\n Point e12 = unitVector(C2.c - C1.c);\n return {C1.c + rc1 * e12 + rs1 * e12 * Point(0, 1),\n C1.c + rc1 * e12 + rs1 * e12 * Point(0, -1)};\n}\n\n// 点 p を通る円 c の接線\nvector<Point> tangentToCircle(Point p, Circle C) {\n return CrossPointCircles(C, Circle(p, sqrt(norm(C.c - p) - C.r * C.r)));\n}\n\nReal intersection_area(Circle c1, Circle c2) {\n Real d = abs(c1.c - c2.c);\n if (d > c1.r + c2.r - eps) return 0;\n if (abs(c1.r - c2.r) >= d - eps) return pow(min(c1.r, c2.r), 2) * M_PI;\n Real rd = c1.r * c1.r - c2.r * c2.r;\n long double cos1 = (d * d + rd) / (2 * d * c1.r);\n long double cos2 = (d * d - rd) / (2 * d * c2.r);\n Real ans1 = c1.r * c1.r * (acosl(cos1) - cos1 * sqrtl(1 - cos1 * cos1));\n Real ans2 = c2.r * c2.r * (acosl(cos2) - cos2 * sqrtl(1 - cos2 * cos2));\n return ans1 + ans2;\n}\n\nbool in_circle(Circle c, Point p) { return abs(c.c - p) < c.r + eps; }\n\nCircle c2(Point a, Point b) {\n return Circle((a + b) / (Real)2.0, abs(a - b) / (Real)2.0);\n}\n\n// 最小包含円\nCircle smallest_enclosing_disc(vector<Point> p) {\n if (p.size() == 1) {\n return {p[0], 0};\n }\n mt19937 rng(2024);\n int sz = p.size();\n shuffle(p.begin(), p.end(), rng);\n Circle c = c2(p[0], p[1]);\n rep2(i, 2, sz) {\n if (!in_circle(c, p[i])) {\n c = c2(p[0], p[i]);\n rep2(j, 1, i) {\n if (!in_circle(c, p[j])) {\n c = c2(p[i], p[j]);\n rep(k, j) {\n if (!in_circle(c, p[k])) {\n c = CircumscribedCircle(p[i], p[j], p[k]);\n }\n }\n }\n }\n }\n }\n return c;\n}\n\n\nvoid solve(int n,int m){\n Poly A(n),B(m);\n rep(i,n){\n double x,y;\n cin>>x>>y;\n A[i]=Point(x,y);\n }\n rep(i,m){\n double x,y;\n cin>>x>>y;\n B[i]=Point(x,y);\n }\n Poly CA=ConvexHull(A);\n Poly CB=ConvexHull(B);\n bool OK=1;\n for(auto p:CA)if(isContain(CB,p))OK=0;\n for(auto p:CB)if(isContain(CA,p))OK=0;\n int an=CA.size(),bn=CB.size();\n rep(i,an)rep(j,bn){\n Segment U(CA[i],CA[(i+1)%an]);\n Segment V(CB[j],CB[(j+1)%bn]);\n if(isIntersect(U,V))OK=0;\n }\n cout<<(OK?\"YES\":\"NO\")<<\"\\n\";\n}\n\nint main(){\n \n cin.tie(nullptr);\n ios::sync_with_stdio(false);\n\n\n int n,m;\n while(cin>>n>>m){\n if(n+m==0)return 0;\n solve(n,m);\n }\n \n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3568, "score_of_the_acc": -0.6237, "final_rank": 14 }, { "submission_id": "aoj_1298_9575222", "code_snippet": "#include <bits/stdc++.h>\n\nusing namespace std;\n\n#define rep(i, a, b) for (int i = (int)(a); i < (int)(b); i++)\n#define rrep(i, a, b) for (int i = (int)(b)-1; i >= (int)(a); i--)\n#define ALL(v) (v).begin(), (v).end()\n#define UNIQUE(v) sort(ALL(v)), (v).erase(unique(ALL(v)), (v).end())\n#define SZ(v) (int)v.size()\n#define MIN(v) *min_element(ALL(v))\n#define MAX(v) *max_element(ALL(v))\n#define LB(v, x) int(lower_bound(ALL(v), (x)) - (v).begin())\n#define UB(v, x) int(upper_bound(ALL(v), (x)) - (v).begin())\n\nusing uint = unsigned int;\nusing ll = long long int;\nusing ull = unsigned long long;\nusing i128 = __int128_t;\nusing u128 = __uint128_t;\nconst int inf = 0x3fffffff;\nconst ll INF = 0x1fffffffffffffff;\n\ntemplate <typename T> inline bool chmax(T &a, T b) {\n if (a < b) {\n a = b;\n return 1;\n }\n return 0;\n}\ntemplate <typename T> inline bool chmin(T &a, T b) {\n if (a > b) {\n a = b;\n return 1;\n }\n return 0;\n}\ntemplate <typename T, typename U> T ceil(T x, U y) {\n assert(y != 0);\n if (y < 0)\n x = -x, y = -y;\n return (x > 0 ? (x + y - 1) / y : x / y);\n}\ntemplate <typename T, typename U> T floor(T x, U y) {\n assert(y != 0);\n if (y < 0)\n x = -x, y = -y;\n return (x > 0 ? x / y : (x - y + 1) / y);\n}\ntemplate <typename T> int popcnt(T x) {\n return __builtin_popcountll(x);\n}\ntemplate <typename T> int topbit(T x) {\n return (x == 0 ? -1 : 63 - __builtin_clzll(x));\n}\ntemplate <typename T> int lowbit(T x) {\n return (x == 0 ? -1 : __builtin_ctzll(x));\n}\n\ntemplate <class T, class U>\nostream &operator<<(ostream &os, const pair<T, U> &p) {\n os << \"P(\" << p.first << \", \" << p.second << \")\";\n return os;\n}\ntemplate <typename T> ostream &operator<<(ostream &os, const vector<T> &vec) {\n os << \"{\";\n for (int i = 0; i < vec.size(); i++) {\n os << vec[i] << (i + 1 == vec.size() ? \"\" : \", \");\n }\n os << \"}\";\n return os;\n}\ntemplate <typename T, typename U>\nostream &operator<<(ostream &os, const map<T, U> &map_var) {\n os << \"{\";\n for (auto itr = map_var.begin(); itr != map_var.end(); itr++) {\n os << \"(\" << itr->first << \", \" << itr->second << \")\";\n itr++;\n if (itr != map_var.end())\n os << \", \";\n itr--;\n }\n os << \"}\";\n return os;\n}\ntemplate <typename T> ostream &operator<<(ostream &os, const set<T> &set_var) {\n os << \"{\";\n for (auto itr = set_var.begin(); itr != set_var.end(); itr++) {\n os << *itr;\n ++itr;\n if (itr != set_var.end())\n os << \", \";\n itr--;\n }\n os << \"}\";\n return os;\n}\n#ifdef LOCAL\n#define show(...) _show(0, #__VA_ARGS__, __VA_ARGS__)\n#else\n#define show(...) true\n#endif\ntemplate <typename T> void _show(int i, T name) {\n cerr << '\\n';\n}\ntemplate <typename T1, typename T2, typename... T3>\nvoid _show(int i, const T1 &a, const T2 &b, const T3 &...c) {\n for (; a[i] != ',' && a[i] != '\\0'; i++)\n cerr << a[i];\n cerr << \":\" << b << \" \";\n _show(i + 1, a, c...);\n}\n\n#include <unistd.h>\n\nnamespace fastio {\nstatic constexpr uint32_t SZ = 1 << 17;\nchar ibuf[SZ];\nchar obuf[SZ];\nchar out[100];\n// pointer of ibuf, obuf\n\n\nuint32_t pil = 0, pir = 0, por = 0;\n\nstruct Pre {\n char num[10000][4];\n constexpr Pre() : num() {\n for (int i = 0; i < 10000; i++) {\n int n = i;\n for (int j = 3; j >= 0; j--) {\n num[i][j] = n % 10 | '0';\n n /= 10;\n }\n }\n }\n} constexpr pre;\n\ninline void load() {\n memmove(ibuf, ibuf + pil, pir - pil);\n pir = pir - pil + fread(ibuf + pir - pil, 1, SZ - pir + pil, stdin);\n pil = 0;\n if (pir < SZ)\n ibuf[pir++] = '\\n';\n}\n\ninline void flush() {\n fwrite(obuf, 1, por, stdout);\n por = 0;\n}\n\nvoid rd(char &c) {\n do {\n if (pil + 1 > pir)\n load();\n c = ibuf[pil++];\n } while (isspace(c));\n}\n\nvoid rd(string &x) {\n x.clear();\n char c;\n do {\n if (pil + 1 > pir)\n load();\n c = ibuf[pil++];\n } while (isspace(c));\n do {\n x += c;\n if (pil == pir)\n load();\n c = ibuf[pil++];\n } while (!isspace(c));\n}\n\ntemplate <typename T> void rd_real(T &x) {\n string s;\n rd(s);\n x = stod(s);\n}\n\ntemplate <typename T> void rd_integer(T &x) {\n if (pil + 100 > pir)\n load();\n char c;\n do\n c = ibuf[pil++];\n while (c < '-');\n bool minus = 0;\n if constexpr (is_signed<T>::value || is_same_v<T, i128>) {\n if (c == '-') {\n minus = 1, c = ibuf[pil++];\n }\n }\n x = 0;\n while ('0' <= c) {\n x = x * 10 + (c & 15), c = ibuf[pil++];\n }\n if constexpr (is_signed<T>::value || is_same_v<T, i128>) {\n if (minus)\n x = -x;\n }\n}\n\nvoid rd(int &x) {\n rd_integer(x);\n}\nvoid rd(ll &x) {\n rd_integer(x);\n}\nvoid rd(i128 &x) {\n rd_integer(x);\n}\nvoid rd(uint &x) {\n rd_integer(x);\n}\nvoid rd(ull &x) {\n rd_integer(x);\n}\nvoid rd(u128 &x) {\n rd_integer(x);\n}\nvoid rd(double &x) {\n rd_real(x);\n}\nvoid rd(long double &x) {\n rd_real(x);\n}\n\ntemplate <class T, class U> void rd(pair<T, U> &p) {\n return rd(p.first), rd(p.second);\n}\ntemplate <size_t N = 0, typename T> void rd_tuple(T &t) {\n if constexpr (N < std::tuple_size<T>::value) {\n auto &x = std::get<N>(t);\n rd(x);\n rd_tuple<N + 1>(t);\n }\n}\ntemplate <class... T> void rd(tuple<T...> &tpl) {\n rd_tuple(tpl);\n}\n\ntemplate <size_t N = 0, typename T> void rd(array<T, N> &x) {\n for (auto &d : x)\n rd(d);\n}\ntemplate <class T> void rd(vector<T> &x) {\n for (auto &d : x)\n rd(d);\n}\n\nvoid read() {}\ntemplate <class H, class... T> void read(H &h, T &...t) {\n rd(h), read(t...);\n}\n\nvoid wt(const char c) {\n if (por == SZ)\n flush();\n obuf[por++] = c;\n}\nvoid wt(const string s) {\n for (char c : s)\n wt(c);\n}\nvoid wt(const char *s) {\n size_t len = strlen(s);\n for (size_t i = 0; i < len; i++)\n wt(s[i]);\n}\n\ntemplate <typename T> void wt_integer(T x) {\n if (por > SZ - 100)\n flush();\n if (x < 0) {\n obuf[por++] = '-', x = -x;\n }\n int outi;\n for (outi = 96; x >= 10000; outi -= 4) {\n memcpy(out + outi, pre.num[x % 10000], 4);\n x /= 10000;\n }\n if (x >= 1000) {\n memcpy(obuf + por, pre.num[x], 4);\n por += 4;\n } else if (x >= 100) {\n memcpy(obuf + por, pre.num[x] + 1, 3);\n por += 3;\n } else if (x >= 10) {\n int q = (x * 103) >> 10;\n obuf[por] = q | '0';\n obuf[por + 1] = (x - q * 10) | '0';\n por += 2;\n } else\n obuf[por++] = x | '0';\n memcpy(obuf + por, out + outi + 4, 96 - outi);\n por += 96 - outi;\n}\n\ntemplate <typename T> void wt_real(T x) {\n ostringstream oss;\n oss << fixed << setprecision(15) << double(x);\n string s = oss.str();\n wt(s);\n}\n\nvoid wt(int x) {\n wt_integer(x);\n}\nvoid wt(ll x) {\n wt_integer(x);\n}\nvoid wt(i128 x) {\n wt_integer(x);\n}\nvoid wt(uint x) {\n wt_integer(x);\n}\nvoid wt(ull x) {\n wt_integer(x);\n}\nvoid wt(u128 x) {\n wt_integer(x);\n}\nvoid wt(double x) {\n wt_real(x);\n}\nvoid wt(long double x) {\n wt_real(x);\n}\n\ntemplate <class T, class U> void wt(const pair<T, U> val) {\n wt(val.first);\n wt(' ');\n wt(val.second);\n}\ntemplate <size_t N = 0, typename T> void wt_tuple(const T t) {\n if constexpr (N < std::tuple_size<T>::value) {\n if constexpr (N > 0) {\n wt(' ');\n }\n const auto x = std::get<N>(t);\n wt(x);\n wt_tuple<N + 1>(t);\n }\n}\ntemplate <class... T> void wt(tuple<T...> tpl) {\n wt_tuple(tpl);\n}\ntemplate <class T, size_t S> void wt(const array<T, S> val) {\n auto n = val.size();\n for (size_t i = 0; i < n; i++) {\n if (i)\n wt(' ');\n wt(val[i]);\n }\n}\ntemplate <class T> void wt(const vector<T> val) {\n auto n = val.size();\n for (size_t i = 0; i < n; i++) {\n if (i)\n wt(' ');\n wt(val[i]);\n }\n}\n\nvoid print() {\n wt('\\n');\n}\ntemplate <class Head, class... Tail> void print(Head &&head, Tail &&...tail) {\n wt(head);\n if (sizeof...(Tail))\n wt(' ');\n print(forward<Tail>(tail)...);\n}\nvoid __attribute__((destructor)) _d() {\n flush();\n}\n} // namespace fastio\n\n\nusing fastio::flush;\nusing fastio::print;\nusing fastio::read;\n\ninline void first(bool i = true) {\n print(i ? \"first\" : \"second\");\n}\ninline void Alice(bool i = true) {\n print(i ? \"Alice\" : \"Bob\");\n}\ninline void Takahashi(bool i = true) {\n print(i ? \"Takahashi\" : \"Aoki\");\n}\ninline void yes(bool i = true) {\n print(i ? \"yes\" : \"no\");\n}\ninline void Yes(bool i = true) {\n print(i ? \"Yes\" : \"No\");\n}\ninline void No() {\n print(\"No\");\n}\ninline void YES(bool i = true) {\n print(i ? \"YES\" : \"NO\");\n}\ninline void NO() {\n print(\"NO\");\n}\ninline void Yay(bool i = true) {\n print(i ? \"Yay!\" : \":(\");\n}\ninline void Possible(bool i = true) {\n print(i ? \"Possible\" : \"Impossible\");\n}\ninline void POSSIBLE(bool i = true) {\n print(i ? \"POSSIBLE\" : \"IMPOSSIBLE\");\n}\n\n/**\n * @brief Fast IO\n */\n\nusing T = double;\nconst T eps = 1e-12;\nusing Point = complex<T>;\nusing Poly = vector<Point>;\n#define X real()\n#define Y imag()\ntemplate <typename T> inline bool eq(const T &a, const T &b) {\n return fabs(a - b) < eps;\n}\nbool cmp(const Point &a, const Point &b) {\n auto sub = [&](Point a) {\n return (a.Y < 0 ? -1 : (a.Y == 0 && a.X >= 0 ? 0 : 1));\n };\n if (sub(a) != sub(b))\n return sub(a) < sub(b);\n return a.Y * b.X < a.X * b.Y;\n}\nstruct Line {\n Point a, b, dir;\n Line() {}\n Line(Point _a, Point _b) : a(_a), b(_b), dir(b - a) {}\n Line(T A, T B, T C) {\n if (eq(A, .0)) {\n a = Point(0, C / B), b = Point(1 / C / B);\n } else if (eq(B, .0)) {\n a = Point(C / A, 0), b = Point(C / A, 1);\n } else {\n a = Point(0, C / B), b = Point(C / A, 0);\n }\n }\n};\nstruct Segment : Line {\n Segment() {}\n Segment(Point _a, Point _b) : Line(_a, _b) {}\n};\nstruct Circle {\n Point p;\n T r;\n Circle() {}\n Circle(Point _p, T _r) : p(_p), r(_r) {}\n};\n\nistream &operator>>(istream &is, Point &p) {\n T x, y;\n is >> x >> y;\n p = Point(x, y);\n return is;\n}\nostream &operator<<(ostream &os, Point &p) {\n os << fixed << setprecision(12) << p.X << ' ' << p.Y;\n return os;\n}\nPoint unit(const Point &a) {\n return a / abs(a);\n}\nT dot(const Point &a, const Point &b) {\n return a.X * b.X + a.Y * b.Y;\n}\nT cross(const Point &a, const Point &b) {\n return a.X * b.Y - a.Y * b.X;\n}\nPoint rot(const Point &a, const T &theta) {\n return Point(cos(theta) * a.X - sin(theta) * a.Y,\n sin(theta) * a.X + cos(theta) * a.Y);\n}\nPoint rot90(const Point &a) {\n return Point(-a.Y, a.X);\n}\nT arg(const Point &a, const Point &b, const Point &c) {\n double ret = acos(dot(a - b, c - b) / abs(a - b) / abs(c - b));\n if (cross(a - b, c - b) < 0)\n ret = -ret;\n return ret;\n}\n\nPoint Projection(const Line &l, const Point &p) {\n T t = dot(p - l.a, l.a - l.b) / norm(l.a - l.b);\n return l.a + (l.a - l.b) * t;\n}\nPoint Projection(const Segment &l, const Point &p) {\n T t = dot(p - l.a, l.a - l.b) / norm(l.a - l.b);\n return l.a + (l.a - l.b) * t;\n}\nPoint Reflection(const Line &l, const Point &p) {\n return p + (Projection(l, p) - p) * 2.;\n}\nint ccw(const Point &a, Point b, Point c) {\n b -= a;\n c -= a;\n if (cross(b, c) > eps)\n return 1; // ccw\n\n if (cross(b, c) < -eps)\n return -1; // cw\n\n if (dot(b, c) < 0)\n return 2; // c,a,b\n\n if (norm(b) < norm(c))\n return -2; // a,b,c\n\n return 0; // a,c,b\n\n}\nbool isOrthogonal(const Line &a, const Line &b) {\n return eq(dot(a.b - a.a, b.b - b.a), .0);\n}\nbool isParallel(const Line &a, const Line &b) {\n return eq(cross(a.b - a.a, b.b - b.a), .0);\n}\nbool isIntersect(const Segment &a, const Segment &b) {\n return ccw(a.a, a.b, b.a) * ccw(a.a, a.b, b.b) <= 0 and\n ccw(b.a, b.b, a.a) * ccw(b.a, b.b, a.b) <= 0;\n}\nint isIntersect(const Circle &a, const Circle &b) {\n T d = abs(a.p - b.p);\n if (d > a.r + b.r + eps)\n return 4;\n if (eq(d, a.r + b.r))\n return 3;\n if (eq(d, abs(a.r - b.r)))\n return 1;\n if (d < abs(a.r - b.r) - eps)\n return 0;\n return 2;\n}\nT Dist(const Line &a, const Point &b) {\n Point c = Projection(a, b);\n return abs(c - b);\n}\nT Dist(const Segment &a, const Point &b) {\n if (dot(a.b - a.a, b - a.a) < eps)\n return abs(b - a.a);\n if (dot(a.a - a.b, b - a.b) < eps)\n return abs(b - a.b);\n return abs(cross(a.b - a.a, b - a.a)) / abs(a.b - a.a);\n}\nT Dist(const Segment &a, const Segment &b) {\n if (isIntersect(a, b))\n return .0;\n T res = min({Dist(a, b.a), Dist(a, b.b), Dist(b, a.a), Dist(b, a.b)});\n return res;\n}\nPoint Intersection(const Line &a, const Line &b) {\n T d1 = cross(a.b - a.a, b.b - b.a);\n T d2 = cross(a.b - a.a, a.b - b.a);\n if (eq(d1, 0.) and eq(d2, 0.))\n return b.a;\n return b.a + (b.b - b.a) * (d2 / d1);\n}\nPoly Intersection(const Circle &a, const Line &b) {\n Poly res;\n T d = Dist(b, a.p);\n if (d > a.r + eps)\n return res;\n Point h = Projection(b, a.p);\n if (eq(d, a.r)) {\n res.push_back(h);\n return res;\n }\n Point e = unit(b.b - b.a);\n T ph = sqrt(a.r * a.r - d * d);\n res.push_back(h - e * ph);\n res.push_back(h + e * ph);\n return res;\n}\nPoly Intersection(const Circle &a, const Segment &b) {\n Line c(b.a, b.b);\n Poly sub = Intersection(a, c);\n double xmi = min(b.a.X, b.b.X), xma = max(b.a.X, b.b.X);\n double ymi = min(b.a.Y, b.b.Y), yma = max(b.a.Y, b.b.Y);\n Poly res;\n rep(i, 0, sub.size()) {\n if (xmi <= sub[i].X + eps and sub[i].X - eps <= xma and\n ymi <= sub[i].Y + eps and sub[i].Y - eps <= yma) {\n res.push_back(sub[i]);\n }\n }\n return res;\n}\nPoly Intersection(const Circle &a, const Circle &b) {\n Poly res;\n int mode = isIntersect(a, b);\n T d = abs(a.p - b.p);\n if (mode == 4 or mode == 0)\n return res;\n if (mode == 3) {\n T t = a.r / (a.r + b.r);\n res.push_back(a.p + (b.p - a.p) * t);\n return res;\n }\n if (mode == 1) {\n if (b.r < a.r - eps) {\n res.push_back(a.p + (b.p - a.p) * (a.r / d));\n } else {\n res.push_back(b.p + (a.p - b.p) * (b.r / d));\n }\n return res;\n }\n T rc = (a.r * a.r + d * d - b.r * b.r) / d / 2.;\n T rs = sqrt(a.r * a.r - rc * rc);\n if (a.r - abs(rc) < eps)\n rs = 0;\n Point e = unit(b.p - a.p);\n res.push_back(a.p + rc * e + rs * e * Point(0, 1));\n res.push_back(a.p + rc * e + rs * e * Point(0, -1));\n return res;\n}\nPoly HalfplaneIntersection(vector<Line> &H) {\n sort(ALL(H), [&](Line &l1, Line &l2) { return cmp(l1.dir, l2.dir); });\n auto outside = [&](Line &L, Point p) -> bool {\n return cross(L.dir, p - L.a) < -eps;\n };\n deque<Line> deq;\n int sz = 0;\n rep(i, 0, SZ(H)) {\n while (sz > 1 and\n outside(H[i], Intersection(deq[sz - 1], deq[sz - 2]))) {\n deq.pop_back();\n sz--;\n }\n while (sz > 1 and outside(H[i], Intersection(deq[0], deq[1]))) {\n deq.pop_front();\n sz--;\n }\n if (sz > 0 and fabs(cross(H[i].dir, deq[sz - 1].dir)) < eps) {\n if (dot(H[i].dir, deq[sz - 1].dir) < 0) {\n return {};\n }\n if (outside(H[i], deq[sz - 1].a)) {\n deq.pop_back();\n sz--;\n } else\n continue;\n }\n deq.push_back(H[i]);\n sz++;\n }\n\n while (sz > 2 and outside(deq[0], Intersection(deq[sz - 1], deq[sz - 2]))) {\n deq.pop_back();\n sz--;\n }\n while (sz > 2 and outside(deq[sz - 1], Intersection(deq[0], deq[1]))) {\n deq.pop_front();\n sz--;\n }\n if (sz < 3)\n return {};\n deq.push_back(deq.front());\n Poly ret;\n rep(i, 0, sz) ret.push_back(Intersection(deq[i], deq[i + 1]));\n return ret;\n}\n\nT Area(const Poly &a) {\n T res = 0;\n int n = a.size();\n rep(i, 0, n) res += cross(a[i], a[(i + 1) % n]);\n return fabs(res / 2.);\n}\nT Area(const Poly &a, const Circle &b) {\n int n = a.size();\n if (n < 3)\n return .0;\n auto rec = [&](auto self, const Circle &c, const Point &p1,\n const Point &p2) {\n Point va = c.p - p1, vb = c.p - p2;\n T f = cross(va, vb), res = .0;\n if (eq(f, .0))\n return res;\n if (max(abs(va), abs(vb)) < c.r + eps)\n return f;\n if (Dist(Segment(p1, p2), c.p) > c.r - eps)\n return c.r * c.r * arg(vb * conj(va));\n auto u = Intersection(c, Segment(p1, p2));\n Poly sub;\n sub.push_back(p1);\n for (auto &x : u)\n sub.push_back(x);\n sub.push_back(p2);\n rep(i, 0, sub.size() - 1) res += self(self, c, sub[i], sub[i + 1]);\n return res;\n };\n T res = .0;\n rep(i, 0, n) res += rec(rec, b, a[i], a[(i + 1) % n]);\n return fabs(res / 2.);\n}\nT Area(const Circle &a, const Circle &b) {\n T d = abs(a.p - b.p);\n if (d >= a.r + b.r - eps)\n return .0;\n if (d <= abs(a.r - b.r) + eps) {\n T r = min(a.r, b.r);\n return M_PI * r * r;\n }\n T ath = acos((a.r * a.r + d * d - b.r * b.r) / d / a.r / 2.);\n T res = a.r * a.r * (ath - sin(ath * 2) / 2.);\n T bth = acos((b.r * b.r + d * d - a.r * a.r) / d / b.r / 2.);\n res += b.r * b.r * (bth - sin(bth * 2) / 2.);\n return fabs(res);\n}\nbool isConvex(const Poly &a) {\n int n = a.size();\n int cur, pre, nxt;\n rep(i, 0, n) {\n pre = (i - 1 + n) % n;\n nxt = (i + 1) % n;\n cur = i;\n if (ccw(a[pre], a[cur], a[nxt]) == -1)\n return 0;\n }\n return 1;\n}\nint isContained(const Poly &a,\n const Point &b) { // 0:not contain,1:on edge,2:contain\n\n bool res = 0;\n int n = a.size();\n rep(i, 0, n) {\n Point p = a[i] - b, q = a[(i + 1) % n] - b;\n if (p.Y > q.Y)\n swap(p, q);\n if (p.Y < eps and eps < q.Y and cross(p, q) > eps)\n res ^= 1;\n if (eq(cross(p, q), .0) and dot(p, q) < eps)\n return 1;\n }\n return (res ? 2 : 0);\n}\nPoly ConvexHull(Poly &a) {\n sort(ALL(a), [](const Point &p, const Point &q) {\n return (eq(p.Y, q.Y) ? p.X < q.X : p.Y < q.Y);\n });\n a.erase(unique(ALL(a)), a.end());\n int n = a.size(), k = 0;\n Poly res(n * 2);\n for (int i = 0; i < n; res[k++] = a[i++]) {\n while (k >= 2 and\n cross(res[k - 1] - res[k - 2], a[i] - res[k - 1]) < eps)\n k--;\n }\n for (int i = n - 2, t = k + 1; i >= 0; res[k++] = a[i--]) {\n while (k >= t and\n cross(res[k - 1] - res[k - 2], a[i] - res[k - 1]) < eps)\n k--;\n }\n res.resize(k - 1);\n return res;\n}\nT Diam(const Poly &a) {\n int n = a.size();\n int x = 0, y = 0;\n rep(i, 1, n) {\n if (a[i].Y > a[x].Y)\n x = i;\n if (a[i].Y < a[y].Y)\n y = i;\n }\n T res = abs(a[x] - a[y]);\n int i = x, j = y;\n do {\n if (cross(a[(i + 1) % n] - a[i], a[(j + 1) % n] - a[j]) < 0)\n i = (i + 1) % n;\n else\n j = (j + 1) % n;\n chmax(res, abs(a[i] - a[j]));\n } while (i != x or j != y);\n return res;\n}\nPoly Cut(const Poly &a, const Line &l) {\n int n = a.size();\n Poly res;\n rep(i, 0, n) {\n Point p = a[i], q = a[(i + 1) % n];\n if (ccw(l.a, l.b, p) != -1)\n res.push_back(p);\n if (ccw(l.a, l.b, p) * ccw(l.a, l.b, q) < 0)\n res.push_back(Intersection(Line(p, q), l));\n }\n return res;\n}\n\nT Closest(Poly &a) {\n int n = a.size();\n if (n <= 1)\n return 0;\n sort(ALL(a), [&](Point a, Point b) {\n return (eq(a.X, b.X) ? a.Y < b.Y : a.X < b.X);\n });\n Poly buf(n);\n auto rec = [&](auto self, int lb, int rb) -> T {\n if (rb - lb <= 1)\n return (T)INF;\n int mid = (lb + rb) >> 1;\n auto x = a[mid].X;\n T res = min(self(self, lb, mid), self(self, mid, rb));\n inplace_merge(a.begin() + lb, a.begin() + mid, a.begin() + rb,\n [&](auto p, auto q) { return p.Y < q.Y; });\n int ptr = 0;\n rep(i, lb, rb) {\n if (abs(a[i].X - x) >= res)\n continue;\n rep(j, 0, ptr) {\n auto sub = a[i] - buf[ptr - 1 - j];\n if (sub.Y >= res)\n break;\n chmin(res, abs(sub));\n }\n buf[ptr++] = a[i];\n }\n return res;\n };\n return rec(rec, 0, n);\n}\n\nCircle Incircle(const Point &a, const Point &b, const Point &c) {\n T A = abs(b - c), B = abs(c - a), C = abs(a - b);\n Point p(A * a.X + B * b.X + C * c.X, A * a.Y + B * b.Y + C * c.Y);\n p /= (A + B + C);\n T r = Dist(Line(a, b), p);\n return Circle(p, r);\n}\nCircle Circumcircle(const Point &a, const Point &b, const Point &c) {\n Line l1((a + b) / 2., (a + b) / 2. + (b - a) * Point(0, 1));\n Line l2((b + c) / 2., (b + c) / 2. + (c - b) * Point(0, 1));\n Point p = Intersection(l1, l2);\n return Circle(p, abs(p - a));\n}\nPoly tangent(const Point &a, const Circle &b) {\n return Intersection(b, Circle(a, sqrt(norm(b.p - a) - b.r * b.r)));\n}\nvector<Line> tangent(const Circle &a, const Circle &b) {\n vector<Line> res;\n T d = abs(a.p - b.p);\n if (eq(d, 0.))\n return res;\n Point u = unit(b.p - a.p);\n Point v = u * Point(0, 1);\n for (int t : {-1, 1}) {\n T h = (a.r + b.r * t) / d;\n if (eq(h * h, 1.)) {\n res.push_back(Line(a.p + (h > 0 ? u : -u) * a.r,\n a.p + (h > 0 ? u : -u) * a.r + v));\n } else if (1 > h * h) {\n Point U = u * h, V = v * sqrt(1 - h * h);\n res.push_back(Line(a.p + (U + V) * a.r, b.p - (U + V) * (b.r * t)));\n res.push_back(Line(a.p + (U - V) * a.r, b.p - (U - V) * (b.r * t)));\n }\n }\n return res;\n}\n\n/**\n * @brief Geometry\n */\n\nint main() {\nwhile(1) {\n int N, M;\n cin >> N >> M;\n if (N == 0 && M == 0) return 0;\n Poly A(N), B(M);\n rep(i,0,N) {\n double x, y;\n cin >> x >> y;\n A[i] = Point(x,y);\n }\n rep(i,0,M) {\n double x, y;\n cin >> x >> y;\n B[i] = Point(x,y);\n }\n if (A.size() >= 2) A = ConvexHull(A);\n if (B.size() >= 2) B = ConvexHull(B);\n bool check = true;\n rep(i,0,A.size()) {\n rep(j,0,B.size()) {\n if (isIntersect(Segment(A[i],A[(i+1)%(int)A.size()]),Segment(B[j],B[(j+1)%(int)B.size()]))) check = false;\n }\n }\n rep(i,0,A.size()) {\n if (isContained(B,A[i])) check = false;\n }\n rep(i,0,B.size()) {\n if (isContained(A,B[i])) check = false;\n }\n cout << (check ? \"YES\" : \"NO\") << endl;\n}\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3432, "score_of_the_acc": -0.4409, "final_rank": 9 }, { "submission_id": "aoj_1298_8562128", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\nstruct Point {\n int x, y, color;\n Point operator-(const Point p) const { return {x - p.x, y - p.y, color}; }\n int cross(const Point p) const { return x * p.y - y * p.x; }\n int half() const { return !(y > 0 || (y == 0 && x > 0)); }\n Point to_first_half() const {\n if (half() == 0) return *this;\n return {-x, -y, color};\n }\n};\n\nbool cmp(const Point p, const Point q) {\n return p.to_first_half().cross(q.to_first_half()) > 0;\n}\n\nbool possible_aux(const vector<Point>& points) {\n int counts[2][2] = {{0, 0}, {0, 0}};\n\n for (const Point p : points) {\n counts[p.half()][p.color]++;\n }\n\n const auto ok = [&]() -> bool {\n return (counts[1][0] == 0 && counts[0][1] == 0) ||\n (counts[0][0] == 0 && counts[1][1] == 0);\n };\n\n if (ok()) return true;\n\n for (int i = 0; i < (int)points.size(); i++) {\n const Point p = points[i];\n const Point q = points[(i + 1) % points.size()];\n\n counts[!p.half()][p.color]++;\n counts[p.half()][p.color]--;\n\n if (p.cross(q) != 0 && ok()) return true;\n }\n\n return false;\n}\n\nbool possible(vector<Point> points) {\n for (int i = 0; i < (int)points.size(); i++) {\n vector<Point> centered;\n for (int j = 0; j < (int)points.size(); j++) {\n if (i == j) continue;\n centered.push_back(points[j] - points[i]);\n }\n sort(centered.begin(), centered.end(), cmp);\n if (possible_aux(centered)) return true;\n }\n\n return false;\n}\n\nint main() {\n int n, m;\n\n while (cin >> n >> m && n && m) {\n vector<Point> points(n + m);\n for (int i = 0; i < n + m; i++) {\n auto& p = points[i];\n cin >> p.x >> p.y;\n p.color = i < n;\n }\n\n cout << (possible(points) ? \"YES\" : \"NO\") << endl;\n }\n}", "accuracy": 1, "time_ms": 300, "memory_kb": 3360, "score_of_the_acc": -0.4164, "final_rank": 7 }, { "submission_id": "aoj_1298_7220918", "code_snippet": "//#define _GLIBCXX_DEBUG\n\n#include<bits/stdc++.h>\nusing namespace std;\n\n#define endl '\\n'\n#define lfs cout<<fixed<<setprecision(10)\n#define ALL(a) (a).begin(),(a).end()\n#define ALLR(a) (a).rbegin(),(a).rend()\n#define UNIQUE(a) (a).erase(unique((a).begin(),(a).end()),(a).end())\n#define spa << \" \" <<\n#define fi first\n#define se second\n#define MP make_pair\n#define MT make_tuple\n#define PB push_back\n#define EB emplace_back\n#define rep(i,n,m) for(ll i = (n); i < (ll)(m); i++)\n#define rrep(i,n,m) for(ll i = (ll)(m) - 1; i >= (ll)(n); i--)\nusing ll = long long;\nusing ld = long double;\nconst ll MOD1 = 1e9+7;\nconst ll MOD9 = 998244353;\nconst ll INF = 1e18;\nusing P = pair<ll, ll>;\ntemplate<typename T> using PQ = priority_queue<T>;\ntemplate<typename T> using QP = priority_queue<T,vector<T>,greater<T>>;\ntemplate<typename T1, typename T2>bool chmin(T1 &a,T2 b){if(a>b){a=b;return true;}else return false;}\ntemplate<typename T1, typename T2>bool chmax(T1 &a,T2 b){if(a<b){a=b;return true;}else return false;}\nll median(ll a,ll b, ll c){return a+b+c-max({a,b,c})-min({a,b,c});}\nvoid ans1(bool x){if(x) cout<<\"Yes\"<<endl;else cout<<\"No\"<<endl;}\nvoid ans2(bool x){if(x) cout<<\"YES\"<<endl;else cout<<\"NO\"<<endl;}\nvoid ans3(bool x){if(x) cout<<\"Yay!\"<<endl;else cout<<\":(\"<<endl;}\ntemplate<typename T1,typename T2>void ans(bool x,T1 y,T2 z){if(x)cout<<y<<endl;else cout<<z<<endl;}\ntemplate<typename T1,typename T2,typename T3>void anss(T1 x,T2 y,T3 z){ans(x!=y,x,z);};\ntemplate<typename T>void debug(const T &v,ll h,ll w,string sv=\" \"){for(ll i=0;i<h;i++){cout<<v[i][0];for(ll j=1;j<w;j++)cout<<sv<<v[i][j];cout<<endl;}};\ntemplate<typename T>void debug(const T &v,ll n,string sv=\" \"){if(n!=0)cout<<v[0];for(ll i=1;i<n;i++)cout<<sv<<v[i];cout<<endl;};\ntemplate<typename T>void debug(const vector<T>&v){debug(v,v.size());}\ntemplate<typename T>void debug(const vector<vector<T>>&v){for(auto &vv:v)debug(vv,vv.size());}\ntemplate<typename T>void debug(stack<T> st){while(!st.empty()){cout<<st.top()<<\" \";st.pop();}cout<<endl;}\ntemplate<typename T>void debug(queue<T> st){while(!st.empty()){cout<<st.front()<<\" \";st.pop();}cout<<endl;}\ntemplate<typename T>void debug(deque<T> st){while(!st.empty()){cout<<st.front()<<\" \";st.pop_front();}cout<<endl;}\ntemplate<typename T>void debug(PQ<T> st){while(!st.empty()){cout<<st.top()<<\" \";st.pop();}cout<<endl;}\ntemplate<typename T>void debug(QP<T> st){while(!st.empty()){cout<<st.top()<<\" \";st.pop();}cout<<endl;}\ntemplate<typename T>void debug(const set<T>&v){for(auto z:v)cout<<z<<\" \";cout<<endl;}\ntemplate<typename T>void debug(const multiset<T>&v){for(auto z:v)cout<<z<<\" \";cout<<endl;}\ntemplate<typename T,size_t size>void debug(const array<T, size> &a){for(auto z:a)cout<<z<<\" \";cout<<endl;}\ntemplate<typename T,typename V>void debug(const map<T,V>&v){for(auto z:v)cout<<\"[\"<<z.first<<\"]=\"<<z.second<<\",\";cout<<endl;}\ntemplate<typename T>vector<vector<T>>vec(ll x, ll y, T w){vector<vector<T>>v(x,vector<T>(y,w));return v;}\nll gcd(ll x,ll y){ll r;while(y!=0&&(r=x%y)!=0){x=y;y=r;}return y==0?x:y;}\n//vector<ll>dx={1,-1,0,0,1,1,-1,-1};vector<ll>dy={0,0,1,-1,1,-1,1,-1};\ntemplate<typename T>vector<T> make_v(size_t a,T b){return vector<T>(a,b);}\ntemplate<typename... Ts>auto make_v(size_t a,Ts... ts){return vector<decltype(make_v(ts...))>(a,make_v(ts...));}\ntemplate<typename T1, typename T2>ostream &operator<<(ostream &os, const pair<T1, T2>&p){return os << p.first << \" \" << p.second;}\ntemplate<typename T>ostream &operator<<(ostream &os, const vector<T> &v){for(auto &z:v)os << z << \" \";cout<<\"|\"; return os;}\ntemplate<typename T>void rearrange(vector<int>&ord, vector<T>&v){\n auto tmp = v;\n for(int i=0;i<tmp.size();i++)v[i] = tmp[ord[i]];\n}\ntemplate<typename Head, typename... Tail>void rearrange(vector<int>&ord,Head&& head, Tail&&... tail){\n rearrange(ord, head);\n rearrange(ord, tail...);\n}\ntemplate<typename T> vector<int> ascend(const vector<T>&v){\n vector<int>ord(v.size());iota(ord.begin(),ord.end(),0);\n sort(ord.begin(),ord.end(),[&](int i,int j){return v[i]<v[j];});\n return ord;\n}\ntemplate<typename T> vector<int> descend(const vector<T>&v){\n vector<int>ord(v.size());iota(ord.begin(),ord.end(),0);\n sort(ord.begin(),ord.end(),[&](int i,int j){return v[i]>v[j];});\n return ord;\n}\ntemplate<typename T> vector<T> inv_perm(const vector<T>&ord){\n vector<T>inv(ord.size());\n for(int i=0;i<ord.size();i++)inv[ord[i]] = i;\n return inv;\n}\nll FLOOR(ll n,ll div){assert(div>0);return n>=0?n/div:(n-div+1)/div;}\nll CEIL(ll n,ll div){assert(div>0);return n>=0?(n+div-1)/div:n/div;}\nll digitsum(ll n){ll ret=0;while(n){ret+=n%10;n/=10;}return ret;}\nll modulo(ll n,ll d){return (n%d+d)%d;};\ntemplate<typename T>T min(const vector<T>&v){return *min_element(v.begin(),v.end());}\ntemplate<typename T>T max(const vector<T>&v){return *max_element(v.begin(),v.end());}\ntemplate<typename T>T acc(const vector<T>&v){return accumulate(v.begin(),v.end(),T(0));};\ntemplate<typename T>T reverse(const T &v){return T(v.rbegin(),v.rend());};\n//mt19937 mt(chrono::steady_clock::now().time_since_epoch().count());\nint popcount(ll x){return __builtin_popcountll(x);};\nint poplow(ll x){return __builtin_ctzll(x);};\nint pophigh(ll x){return 63 - __builtin_clzll(x);};\ntemplate<typename T>T poll(queue<T> &q){auto ret=q.front();q.pop();return ret;};\ntemplate<typename T>T poll(priority_queue<T> &q){auto ret=q.top();q.pop();return ret;};\ntemplate<typename T>T poll(QP<T> &q){auto ret=q.top();q.pop();return ret;};\ntemplate<typename T>T poll(stack<T> &s){auto ret=s.top();s.pop();return ret;};\nll MULT(ll x,ll y){if(LLONG_MAX/x<=y)return LLONG_MAX;return x*y;}\nll POW2(ll x, ll k){ll ret=1,mul=x;while(k){if(mul==LLONG_MAX)return LLONG_MAX;if(k&1)ret=MULT(ret,mul);mul=MULT(mul,mul);k>>=1;}return ret;}\nll POW(ll x, ll k){ll ret=1;for(int i=0;i<k;i++){if(LLONG_MAX/x<=ret)return LLONG_MAX;ret*=x;}return ret;}\ntemplate< typename T = int >\nstruct edge {\n int to;\n T cost;\n int id;\n edge():id(-1){};\n edge(int to, T cost = 1, int id = -1):to(to), cost(cost), id(id){}\n operator int() const { return to; }\n};\n\ntemplate<typename T>\nusing Graph = vector<vector<edge<T>>>;\ntemplate<typename T>\nGraph<T>revgraph(const Graph<T> &g){\n Graph<T>ret(g.size());\n for(int i=0;i<g.size();i++){\n for(auto e:g[i]){\n int to = e.to;\n e.to = i;\n ret[to].push_back(e);\n }\n }\n return ret;\n}\ntemplate<typename T>\nGraph<T> readGraph(int n,int m,int indexed=1,bool directed=false,bool weighted=false){\n Graph<T> ret(n);\n for(int es = 0; es < m; es++){\n int u,v;\n T w=1;\n cin>>u>>v;u-=indexed,v-=indexed;\n if(weighted)cin>>w;\n ret[u].emplace_back(v,w,es);\n if(!directed)ret[v].emplace_back(u,w,es);\n }\n return ret;\n}\ntemplate<typename T>\nGraph<T> readParent(int n,int indexed=1,bool directed=true){\n Graph<T>ret(n);\n for(int i=1;i<n;i++){\n int p;cin>>p;\n p-=indexed;\n ret[p].emplace_back(i);\n if(!directed)ret[i].emplace_back(p);\n }\n return ret;\n}\n\ntemplate <typename F>\nclass\n#if defined(__has_cpp_attribute) && __has_cpp_attribute(nodiscard)\n [[nodiscard]]\n#endif // defined(__has_cpp_attribute) && __has_cpp_attribute(nodiscard)\nFixPoint final : private F\n{\npublic:\n template <typename G>\n explicit constexpr FixPoint(G&& g) noexcept\n : F{std::forward<G>(g)}\n {}\n\n template <typename... Args>\n constexpr decltype(auto)\n operator()(Args&&... args) const\n#if !defined(__GNUC__) || defined(__clang__) || __GNUC__ >= 9\n noexcept(noexcept(F::operator()(std::declval<FixPoint>(), std::declval<Args>()...)))\n#endif // !defined(__GNUC__) || defined(__clang__) || __GNUC__ >= 9\n {\n return F::operator()(*this, std::forward<Args>(args)...);\n }\n}; // class FixPoint\n\n\n#if defined(__cpp_deduction_guides)\ntemplate <typename F>\nFixPoint(F&&)\n-> FixPoint<std::decay_t<F>>;\n#endif // defined(__cpp_deduction_guides)\n\n\nnamespace\n{\n template <typename F>\n#if !defined(__has_cpp_attribute) || !__has_cpp_attribute(nodiscard)\n # if defined(__GNUC__) && (__GNUC__ > 3 || __GNUC__ == 3 && __GNUC_MINOR__ >= 4)\n __attribute__((warn_unused_result))\n# elif defined(_MSC_VER) && _MSC_VER >= 1700 && defined(_Check_return_)\n _Check_return_\n# endif // defined(__GNUC__) && (__GNUC__ > 3 || __GNUC__ == 3 && __GNUC_MINOR__ >= 4)\n#endif // !defined(__has_cpp_attribute) || !__has_cpp_attribute(nodiscard)\n inline constexpr decltype(auto)\n makeFixPoint(F&& f) noexcept\n {\n return FixPoint<std::decay_t<F>>{std::forward<std::decay_t<F>>(f)};\n }\n} // namespace\n\nusing Real = long double;\nusing Point = complex<Real>;\nconst Real EPS = 1e-10, PI = acos((Real)(-1.0));\n\ninline bool eq(Real a, Real b) { return fabs(b - a) < EPS; }\ninline bool eq(Point a, Point b) { return fabs(b - a) < EPS; }\n\nPoint operator*(const Point &p, const Real &d) {\n return Point(real(p) * d, imag(p) * d);\n}\n\nistream &operator>>(istream &is, Point &p) {\n Real a, b;\n is >> a >> b;\n p = Point(a, b);\n return is;\n}\n\nostream &operator<<(ostream &os, Point &p) {\n return os << fixed << setprecision(10) << p.real() << \" \" << p.imag();\n}\n\nPoint rotate(Real theta, const Point &p) {\n return Point(cos(theta) * p.real() - sin(theta) * p.imag(), sin(theta) * p.real() + cos(theta) * p.imag());\n}\n\nnamespace std {\n bool operator<(const Point& a, const Point& b) {\n return !eq(a.real(), b.real()) ? a.real() < b.real() : a.imag() < b.imag();\n }\n}\n\nstruct Line {\n Point a, b;\n Line() = default;\n Line(Point a, Point b): a(a), b(b) {}\n\n friend ostream &operator<<(ostream &os, Line &p) {\n return os << p.a << \" to \" << p.b;\n }\n\n friend istream &operator>>(istream &is, Line &a) {\n return is >> a.a >> a.b;\n }\n};\n\nstruct Circle {\n Point c;\n Real r;\n Circle() = default;\n Circle(Point c, Real r): c(c), r(r) {}\n\n friend ostream &operator<<(ostream &os, Circle &c) {\n return os << c.c << \": \" << c.r;\n }\n\n friend istream &operator>>(istream &is, Circle &c) {\n return is >> c.c >> c.r;\n }\n};\n\nReal cross(const Point &a, const Point &b) {\n return real(a) * imag(b) - real(b) * imag(a);\n}\n\nReal dot(const Point &a, const Point &b) {\n return real(a) * real(b) + imag(a) * imag(b);\n}\n\nstruct Segment : Line {\n Segment() = default;\n Segment(Point a, Point b) : Line(a, b) {}\n};\n\nint ccw(const Point &a, Point b, Point c) {\n b = b - a, c = c - a;\n if (cross(b, c) > EPS) return +1; // COUNTER_CLOCKWISE\n if (cross(b, c) < -EPS) return -1; // CLOCKWISE\n if (dot(b, c) < 0) return +2; // ONLINE_BACK\n if (norm(b) < norm(c)) return -2; // ONLINE_FRONT\n return 0; // ON_SEGMENT\n}\n\nbool intersect(const Segment &s, const Point &p) {\n return ccw(s.a, s.b, p) == 0;\n}\n\nbool intersect(const Segment &s, const Segment&t) {\n 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;\n}\n\nbool intersect(const Line &l, const Segment& s) {\n return cross(l.b - l.a, s.a - l.a) * cross(l.b - l.a, s.b - l.a) < EPS;\n}\n\nint intersect(const Circle &c, const Circle &d) {\n Real dis = fabs(c.c - d.c);\n\n if (dis > c.r + d.r + EPS) return 4;\n else if (eq(dis, c.r + d.r)) return 3;\n else if (eq(dis, abs(c.r - d.r))) return 1;\n else if (dis + min(c.r, d.r) < max(c.r, d.r) - EPS) return 0;\n else return 2;\n}\n\nbool parallel(const Line &a, const Line &b) {\n return abs(cross(a.b - a.a, b.b - b.a)) < EPS;\n}\n\nbool orthogonal(const Line &a, const Line &b) {\n return abs(dot(a.b - a.a, b.b - b.a)) < EPS;\n}\n\nPoint projection(const Line& s, const Point &p) {\n double t = dot(p - s.a, s.b - s.a) / norm(s.b - s. a);\n return s.a + (s.b - s.a) * t;\n}\n\nPoint projection(const Segment& l, const Point &p) {\n return projection(Line(l), p);\n}\n\nPoint reflection(const Line &l, const Point &p) {\n Point r = projection(l, p);\n return p + (r - p) * 2;\n}\n\nReal distance(const Line &l, const Point &p) {\n Point r = projection(l, p);\n return fabs(p - r);\n}\n\nReal distance(const Segment&s, const Point &p) {\n Point r = projection(s, p);\n if (intersect(s, r)) return abs(r - p);\n return min(abs(s.a - p), abs(s.b - p));\n}\n\nReal distance(const Segment& a, const Segment& b) {\n if (intersect(a, b)) return 0;\n return min({\n distance(a, b.a),\n distance(a, b.b),\n distance(b, a.a),\n distance(b, a.b)\n });\n}\n\nbool contains(const Circle &c, const Point &p) {\n if (fabs(c.c - p) < c.r + EPS) return true;\n return false;\n}\n\nPoint crosspoint(const Line &l, const Line &m) {\n Real A = cross(l.b - l.a, m.b - m.a);\n Real B = cross(l.b - l.a, l.b - m.a);\n if (eq(abs(A), 0.0) && eq(abs(B), 0.0)) return m.a;\n return m.a + (m.b - m.a) * B / A;\n}\n\nPoint crosspoint(const Segment& l, const Segment& m) {\n return crosspoint(Line(l), Line(m));\n}\n\nLine bisector_of_angle(const Point& a, const Point& b, const Point& c) {\n Real d_ab = fabs(a - b);\n Real d_bc = fabs(b - c);\n Point c_ = b + (c - b) * d_ab / d_bc;\n\n return Line(b, (a + c_) * 0.5);\n}\n\nLine bisector(const Point& a, const Point& b) {\n Point c = (a + b) * 0.5;\n Point d = c + rotate(PI / 2, b - c);\n\n return Line(c, d);\n}\n\nint intersect(const Circle &c, const Line &l) {\n Real d = distance(l, c.c);\n\n if (d > c.r + EPS) return 0;\n if (eq(d, c.r)) return 1;\n return 2;\n}\n\npair<Point, Point> crosspoint(const Circle& c, const Line& l) {\n Real d = distance(l, c.c);\n\n Point p = projection(l, c.c);\n if (intersect(c, l) == 1) return { p, p };\n\n Real d2 = sqrt(c.r * c.r - d * d);\n Point v = (l.a - l.b) * (1.0 / fabs(l.a - l.b));\n\n return { p + d2 * v, p - d2 * v };\n}\n\nvector<Point> crosspoint(const Circle &c, const Segment &s) {\n int isect = intersect(c, Line(s));\n if (isect == 0) return {};\n\n auto [p1, p2] = crosspoint(c, Line(s));\n\n vector<Point> res;\n if (ccw(s.a, s.b, p1) == 0) res.push_back(p1);\n if (ccw(s.a, s.b, p2) == 0) res.push_back(p2);\n\n if (res.size() == 2 && ccw(s.a, p1, p2) == 0) swap(res[0], res[1]);\n return res;\n}\n\npair<Point, Point> crosspoint(const Circle &c1, const Circle &c2) {\n Real d = fabs(c1.c - c2.c);\n Real x1 = (c1.r * c1.r + d * d - c2.r * c2.r) / 2 / d;\n Real y = sqrt(c1.r * c1.r - x1 * x1);\n\n Point base = (c2.c * x1 + c1.c * (d - x1)) * (1.0 / d);\n Point v = rotate(PI / 2, c1.c - base);\n v = v / fabs(v);\n\n return { base + v * y, base - v * y };\n}\n\npair<Point, Point> tangent(const Circle &c, const Point &p) {\n Real d = fabs(c.c - p);\n Real d2 = sqrt(d * d - c.r * c.r);\n Real a = acos(d2 / d);\n\n\n Point q = c.c - p;\n q = q / fabs(q);\n\n// cout << d << \" \" << d2 << \" \" << a << \" \" << q << endl;\n return {\n p + rotate(a, q) * d2,\n p + rotate(-a, q) * d2\n };\n}\n\nvector<Segment> common_tangent(Circle c1, Circle c2) {\n int isect = intersect(c1, c2);\n Real d = fabs(c1.c - c2.c);\n Point v = (c2.c - c1.c) / d;\n\n vector<Segment> res;\n\n if (isect == 0) {\n return {};\n } else if (isect == 1) {\n auto [p1, p2] = crosspoint(c1, Line(c1.c, c2.c));\n if (eq(fabs(p2 - c2.c), c2.r)) swap(p1, p2);\n\n Point v = p1 + rotate(PI/2, c2.c - p1);\n return { Segment(p1, v) };\n } else if (isect == 2) {\n } else if (isect == 3) {\n auto [p1, p2] = crosspoint(c1, c2);\n res.emplace_back(p1, rotate(PI / 2, c1.c - p1) + p1);\n } else {\n Real a = d * c1.r / (c1.r + c2.r);\n Point p = c1.c + v * a;\n\n Real d1 = sqrt(a * a - c1.r * c1.r), d2 = sqrt(norm(p - c2.c) - c2.r * c2.r);\n Real theta = acos(d1 / a);\n\n// cout << d << endl;\n// cout << a << \" \" << d1 << \" \" << d2 << \" \" << theta << \": \" << p << endl;\n\n Point e = (c1.c - p) * (1.0 / fabs(c1.c - p));\n Point e1 = rotate(theta, e), e2 = rotate(-theta, e);\n\n res.emplace_back(p + e1 * d1, p - e1 * d2);\n res.emplace_back(p + e2 * d1, p - e2 * d2);\n }\n\n if (eq(c1.r, c2.r)) {\n Point e = rotate(PI / 2, v);\n res.push_back(Segment(c1.c + e * c1.r, c2.c + e * c1.r));\n res.push_back(Segment(c1.c - e * c1.r, c2.c - e * c1.r));\n return res;\n }\n\n if (c1.r > c2.r) swap(c1, c2);\n\n v = c1.c - c2.c;\n Point p = v * (1.0 / (c2.r - c1.r)) * c2.r + c2.c;\n\n auto [q1, q2] = tangent(c1, p);\n res.emplace_back(q1, crosspoint(c2, Line(p, q1)).first);\n res.emplace_back(q2, crosspoint(c2, Line(p, q2)).first);\n\n return res;\n}\n\nusing Polygon = vector<Point>;\n\nistream& operator>>(istream& is, Polygon &p) {\n for (int i = 0; i < p.size(); i++) is >> p[i];\n return is;\n}\n\nostream& operator<<(ostream& os, Polygon &p) {\n for (int i = 0; i < p.size(); i++) os << p[i] << endl;\n return os;\n}\n\nReal area(const Polygon& p) {\n Real res = 0;\n for (int i = 0; i < p.size(); i++) {\n res += cross(p[i], p[(i + 1) % p.size()]);\n }\n return abs(res)/2;\n}\n\nReal arcarea(const Circle &c, const Point &p1_, const Point &p2_) {\n Point p1 = p1_ - c.c, p2 = p2_ - c.c;\n Real a2 = atan2(imag(p2), real(p2));\n Point p = rotate(-a2, p1);\n Real a1 = atan2(imag(p), real(p));\n// cout << p1 << \" \" << p2 << \" --- \" << a2 << \" \" << a1 << endl;\n return - a1 / 2 * c.r * c.r;\n}\n\nint contains(Polygon g,Point p){\n int n=g.size();\n bool x=false;\n for(int i=0;i<n;i++){\n Point a=g[i]-p,b=g[(i+1)%n]-p;\n if(fabs(cross(a,b)) < EPS and dot(a,b) < EPS) return 1;\n if(a.imag()>b.imag()) swap(a,b);\n if(a.imag() < EPS and EPS < b.imag() and cross(a,b) > EPS ) {\n// cout << a << \" --- \" << b << \": \" << cross(a, b) << endl;\n x = !x;\n }\n }\n return (x?2:0);\n}\n\nPolygon convex_hull(Polygon Q) {\n sort(ALL(Q));\n\n Polygon upper({Q[0]}), lower({Q[0]});\n\n rep(i, 1, Q.size()) {\n while (upper.size() >= 2) {\n int _ccw = ccw(upper[upper.size() - 2], upper[upper.size() - 1], Q[i]);\n if (_ccw == 1) {\n upper.pop_back();\n } else {\n break;\n }\n }\n if (upper.size() <= 1) {\n upper.push_back(Q[i]);\n } else if (upper.size() > 1) {\n int _ccw = ccw(upper[upper.size() - 2], upper[upper.size() - 1], Q[i]);\n if (_ccw == -1 || _ccw == -2) {\n upper.push_back(Q[i]);\n }\n }\n }\n rep(i, 1, Q.size()) {\n while (lower.size() >= 2) {\n int _ccw = ccw(lower[lower.size() - 2], lower[lower.size() - 1], Q[i]);\n if (_ccw == -1) {\n lower.pop_back();\n } else {\n break;\n }\n }\n\n if (lower.size() <= 1) {\n lower.push_back(Q[i]);\n } else if (lower.size() > 1) {\n int _ccw = ccw(lower[lower.size() - 2], lower[lower.size() - 1], Q[i]);\n if (_ccw == 1 || _ccw == -2) {\n lower.push_back(Q[i]);\n }\n }\n }\n\n Polygon ret;\n\n rep(i, 0, lower.size()) {\n ret.push_back(lower[i]);\n// cout << lower[i] << endl;\n }\n\n// cout << \" | \" << endl;\n\n rrep(i, 0, upper.size()) {\n if (!eq(lower[0], upper[i]) && !eq(lower.back(), upper[i]))\n ret.push_back(upper[i]);\n// cout << upper[i] << endl;\n }\n\n int start = 0;\n\n// cout << ret.size() << endl;\n rep(i, 0, ret.size()) {\n if (ret[i].imag() < ret[start].imag() || (eq(ret[i].imag(), ret[start].imag()) && ret[i].real() < ret[start].real())) {\n start = i;\n }\n }\n\n// cout << ret.size() << endl;\n// rep(i, 0, ret.size()) {\n// auto p = ret[(i + start) % ret.size()];\n// cout << (int)p.real() << \" \" << (int)p.imag() << endl;\n// }\n//\n return ret;\n}\n\nReal closest_pair(vector<Point> &pts, int l, int r) {\n int m = (l + r) / 2;\n if (r - l <= 1) {\n return INF;\n } else if (r - l == 2) {\n if (imag(pts[l]) > imag(pts[l + 1])) swap(pts[l], pts[l + 1]);\n return fabs(pts[l] - pts[l + 1]);\n }\n\n Real mid = pts[m].real();\n Real d = min(closest_pair(pts, l, m), closest_pair(pts, m, r));\n\n inplace_merge(pts.begin() + l, pts.begin() + m, pts.begin() + r, [](const Point &a, const Point& b) {\n return a.imag() < b.imag();\n });\n\n vector<Point> ret;\n\n for (int i = l; i < r; i++) {\n if (fabs(pts[i].real() - mid) >= d) continue;\n\n for (int j = ret.size() - 1; j >= 0; j--) {\n if (fabs(imag(ret[j]) - imag(pts[i])) >= d) break;\n chmin(d, fabs(ret[j] - pts[i]));\n }\n\n ret.emplace_back(pts[i]);\n }\n\n return d;\n}\n\nbool contains(const Polygon &a, const Polygon &b) {\n rep(i, 0, b.size()) {\n if (!contains(a, b[i])) return false;\n }\n return true;\n}\n\n\nReal distance(const Polygon &a, const Point &p) {\n if (contains(a, p)) return 0;\n Real res = INF;\n rep(i, 0, a.size()) {\n Segment as(a[i], a[(i + 1) % a.size()]);\n chmin(res, distance(as, p));\n }\n return res;\n}\n\nReal distance(const Polygon &a, const Polygon &b) {\n// if (contains(a, b) || contains(b, a)) return 0;\n Real res = INF;\n rep(i, 0, a.size()) chmin(res, distance(b, a[i]));\n rep(i, 0, b.size()) chmin(res, distance(a, b[i]));\n return res;\n}\n\nstruct Point3d {\n Real x, y, z;\n Point3d() {}\n Point3d(Real x, Real y, Real z): x(x), y(y), z(z) {}\n};\n\nReal distance(const Point3d &a, const Point3d &b) {\n return sqrt(pow(a.x - b.x, 2) + pow(a.y - b.y, 2) + pow(a.z - b.z, 2));\n}\n\nvoid solve(int N, int M) {\n vector<pair<Point, int>> pts(N + M);\n rep(i, 0, N) {\n cin >> pts[i].fi;\n pts[i].se = 0;\n }\n rep(i, 0, M) {\n cin >> pts[i + N].fi;\n pts[i + N].se = 1;\n }\n if (N == 0 || M == 0 || (N == 1 && M == 1)) {\n cout << \"YES\" << endl;\n return;\n }\n\n bool on_line = true;\n rep(i, 2, N + M) {\n if (abs(ccw(pts[0].fi, pts[1].fi, pts[i].fi)) == 1) {\n on_line = false;\n break;\n }\n }\n\n if (on_line) {\n sort(ALL(pts));\n int sw = 0;\n rep(i, 0, N + M - 1) {\n if (pts[i].se != pts[i + 1].se) sw++;\n }\n if (sw == 1) {\n cout << \"YES\" << endl;\n } else {\n cout << \"NO\" << endl;\n }\n return;\n }\n\n\n rep(i, 0, N + M) {\n rep(j, i + 1, N + M) {\n if (pts[i].se != pts[j].se) continue;\n\n set<int> ccw_set[2];\n\n rep(k, 0, N + M) {\n ccw_set[pts[k].se].insert(ccw(pts[i].fi, pts[j].fi, pts[k].fi));\n }\n\n if (ccw_set[1 - pts[i].se].size() != 1) {\n } else if (ccw_set[pts[i].se].find(*ccw_set[1 - pts[i].se].begin()) != ccw_set[pts[i].se].end()) {\n } else {\n cout << \"YES\" << endl;\n return;\n }\n }\n }\n cout << \"NO\" << endl;\n return;\n}\n\nint main() {\n cin.tie();\n ios::sync_with_stdio(false);\n\n cout << fixed << setprecision(10);\n int N, M;\n while (cin >> N >> M && N + M) {\n solve(N, M);\n }\n}", "accuracy": 1, "time_ms": 2110, "memory_kb": 3540, "score_of_the_acc": -1.1097, "final_rank": 20 }, { "submission_id": "aoj_1298_7166287", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\nconstexpr double EPS = 1e-8;\n\ninline int sgn(double x) { return x < -EPS ? -1 : x > EPS ? 1 : 0; }\n\ninline int compare(double a, double b) { return sgn(a - b); }\n\nstruct Point {\n double x, y;\n\n Point() {}\n\n Point(double x, double y) : x(x), y(y) {}\n\n Point& operator+=(const Point& p) {\n x += p.x, y += p.y;\n return *this;\n }\n\n Point& operator-=(const Point& p) {\n x -= p.x, y -= p.y;\n return *this;\n }\n\n Point operator+(const Point& p) const { return Point(*this) += p; }\n\n Point operator-(const Point& p) const { return Point(*this) -= p; }\n\n double norm() const { return x * x + y * y; }\n\n friend ostream& operator<<(ostream& os, const Point& p) { return os << p.x << ' ' << p.y; }\n};\n\ndouble dot(Point p, Point q) { return p.x * q.x + p.y * q.y; }\n\ndouble cross(Point p, Point q) { return p.x * q.y - p.y * q.x; }\n\nenum Position { COUNTER_CLOCKWISE = +1, CLOCKWISE = -1, ONLINE_BACK = +2, ONLINE_FRONT = -2, ON_SEGMENT = 0 };\n\nPosition ccw(Point a, Point b, Point c) {\n b -= a, c -= a;\n if (sgn(cross(b, c)) == +1) return COUNTER_CLOCKWISE;\n if (sgn(cross(b, c)) == -1) return CLOCKWISE;\n if (sgn(dot(b, c)) == -1) return ONLINE_BACK;\n if (compare(b.norm(), c.norm()) == -1) return ONLINE_FRONT;\n return ON_SEGMENT;\n}\n\nvoid solve(int n, int m) {\n vector<vector<Point>> ps(2);\n vector<Point> cand;\n for (int i = 0; i < n; i++) {\n int x, y;\n cin >> x >> y;\n ps[0].emplace_back(x, y);\n cand.emplace_back(x, y);\n }\n for (int i = 0; i < m; i++) {\n int x, y;\n cin >> x >> y;\n ps[1].emplace_back(x, y);\n cand.emplace_back(x, y);\n }\n\n auto check = [&](Point p, Point q) {\n int a = 0, b = 0;\n vector<pair<double, int>> line;\n for (int i = 0; i < 2; i++) {\n for (auto& r : ps[i]) {\n int val = ccw(p, q, r);\n if (abs(val) == 1) {\n if (i == 0) {\n if (a == 0)\n a = val;\n else if (val != a)\n return false;\n } else {\n if (b == 0)\n b = val;\n else if (val != b)\n return false;\n }\n } else\n line.emplace_back(sgn((p - q).x) != 0 ? (r - q).x / (p - q).x : (r - q).y / (p - q).y, i);\n }\n }\n if (a != 0 and b != 0 and a == b) return false;\n sort(line.begin(), line.end());\n for (size_t i = 0, cnt = 0; i + 1 < line.size(); i++) {\n if (line[i].second == line[i + 1].second) continue;\n if (++cnt == 2) return false;\n }\n return true;\n };\n for (int i = 0; i < n + m; i++) {\n for (int j = i + 1; j < n + m; j++) {\n if (check(cand[i], cand[j])) {\n cout << \"YES\" << '\\n';\n return;\n }\n }\n }\n\n cout << \"NO\" << '\\n';\n}\n\nint main() {\n cin.tie(0);\n ios::sync_with_stdio(false);\n for (int n, m; cin >> n >> m, n;) solve(n, m);\n}", "accuracy": 1, "time_ms": 130, "memory_kb": 3280, "score_of_the_acc": -0.2665, "final_rank": 2 }, { "submission_id": "aoj_1298_7140554", "code_snippet": "#include <bits/stdc++.h>\n\nusing namespace std;\n\n#define rep(i, n) for (int i = 0; i < int(n); i++)\n#define per(i, n) for (int i = (n)-1; 0 <= i; i--)\n#define rep2(i, l, r) for (int i = (l); i < int(r); i++)\n#define per2(i, l, r) for (int i = (r)-1; int(l) <= i; i--)\n#define MM << \" \" <<\n#define pb push_back\n#define eb emplace_back\n#define all(x) begin(x), end(x)\n#define rall(x) rbegin(x), rend(x)\n#define sz(x) (int)x.size()\n\ntemplate<typename T>\nvoid print(const vector<T> &v, T x = 0) {\n int n = v.size();\n for (int i = 0; i < n; i++) cout << v[i] + x << (i == n - 1 ? '\\n' : ' ');\n if (v.empty()) cout << '\\n';\n}\n\ntemplate<class T>\nusing MaxHeap = priority_queue<T>;\ntemplate<class T>\nusing MinHeap = priority_queue<T, vector<T>, greater<T>>;\n\nusing ll = long long;\nusing pii = pair<int, int>;\nusing pll = pair<ll, ll>;\n\ntemplate<typename T>\nbool chmax(T &x, const T &y) {\n return (x < y) ? (x = y, true) : false;\n}\n\ntemplate<typename T>\nbool chmin(T &x, const T &y) {\n return (x > y) ? (x = y, true) : false;\n}\n\nusing P = pair<ll, ll>;\n\nll dot(P x, P y) {\n return x.first * y.first + x.second * y.second;\n}\nll det(P x, P y) {\n return x.first * y.second - x.second * y.first;\n}\n\nP diff(const P &b, const P &a) {\n return {b.first - a.first, b.second - a.second};\n}\n\nint ccw(const P &a, P b, P c) {\n b = diff(b, a);\n c = diff(c, a);\n if(det(b, c) > 0) return +1;\n if(det(b, c) < 0) return -1;\n if(dot(b, c) < 0) return +2;\n if(c.first * c.first + c.second * c.second - b.second * b.second - c.first * c.first > 0) return -2;\n return 0;\n}\n\nbool intersect(pair<P, P> &s, pair<P, P> &t) {\n if(ccw(s.first, s.second, t.first) * ccw(s.first, s.second, t.second) > 0) return false;\n return ccw(t.first, t.second, t.first) * ccw(t.first, t.second, s.second) <= 0;\n}\n\nvector<P> convexHull(vector<P> p) {\n sort(all(p));\n p.erase(unique(all(p)), end(p));\n int n = p.size(), k = 0;\n if(n == 1) return p;\n vector<P> ch(2 * n);\n for(int i = 0; i < n; ch[k++] = p[i++]) {\n while(k >= 2 && det(diff(ch[k-1], ch[k-2]), diff(p[i], ch[k-1])) <= 0) {\n --k;\n }\n }\n for(int i = n - 2, t = k + 1; i >= 0; ch[k++] = p[i--]) {\n while(k >= t && det(diff(ch[k-1], ch[k-2]), diff(p[i], ch[k-1])) <= 0) {\n --k;\n }\n }\n ch.resize(k - 1);\n return ch;\n}\n\nbool in_poly(const vector<P> &p, P &q) {\n if(p.size() == 1) return false; \n int n = p.size();\n int ret = 0;\n rep(i,n) {\n auto a = diff(p[i], q), b = diff(p[(i + 1) % n], q);\n if(det(a, b) == 0 && dot(a, b) <= 0) return 1;\n if(a.second > b.second) swap(a, b);\n if(a.second <= 0 && b.second > 0 && det(a, b) > 0) ret ^= 2;\n }\n return ret;\n}\n\nbool onSeg(P a, P b, P c) {\n a = diff(a, c);\n b = diff(b, c);\n if(det(a, b)) return false;\n if(a.first > b.first) swap(a, b);\n return a.first <= c.first && c.first <= b.first;\n}\n\nbool solve(int n, int m) {\n vector<P> black(n), white(m);\n for(auto &i: black) cin >> i.first >> i.second;\n for(auto &i: white) cin >> i.first >> i.second;\n black = convexHull(black);\n white = convexHull(white);\n // for(auto &i : black) cout << \"(\" MM i.first MM i.second MM \")\" << endl;\n // for(auto &i : white) cout << \"(\" MM i.first MM i.second MM \")\" << endl;\n\n if(black.size() > white.size()) swap(black, white);\n\n if(black.size() == 1) {\n if(white.size() == 1) return true;\n else if(white.size() == 2) return ccw(white[0], white[1], black[0]) != 0;\n else return !in_poly(white, black[0]);\n } else if(black.size() == 2) {\n pair<P, P> c = {black[0], black[1]};\n int pp = white.size();\n rep(i,pp) {\n pair<P, P> d = {white[i], white[(i + 1) % pp]};\n if(intersect(c, d)) return false;\n }\n return true;\n } else {\n bool res = true;\n for(auto &i : black) {\n res = res & !in_poly(white, i);\n }\n for(auto &i : white) {\n res = res & !in_poly(black, i);\n }\n return res;\n }\n}\n\nint main() {\n int n, m;\n vector<bool> ans;\n while(1) {\n cin >> n >> m;\n if(n == 0 && m == 0) break;\n ans.eb(solve(n, m));\n }\n for(auto &&i : ans) cout << (i ? \"YES\" : \"NO\") << endl;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3356, "score_of_the_acc": -0.3387, "final_rank": 3 }, { "submission_id": "aoj_1298_6794635", "code_snippet": "#pragma GCC ta g(\"avx2\")\n#pragma GCC optimize(\"Ofast\")\n#pragma GCC optimize(\"unroll-loops\")\n#include <bits/stdc++.h>\nusing namespace std;\n#define DEBUG\n#ifdef DEBUG\ntemplate <class T, class U>\nostream &operator<<(ostream &os, const pair<T, U> &p) {\n os << '(' << p.first << ',' << p.second << ')';\n return os;\n}\ntemplate <class T> ostream &operator<<(ostream &os, const vector<T> &v) {\n os << '{';\n for(int i = 0; i < (int)v.size(); i++) {\n if(i) { os << ','; }\n os << v[i];\n }\n os << '}';\n return os;\n}\nvoid debugg() { cerr << endl; }\ntemplate <class T, class... Args>\nvoid debugg(const T &x, const Args &... args) {\n cerr << \" \" << x;\n debugg(args...);\n}\n#define debug(...) \\\n cerr << __LINE__ << \" [\" << #__VA_ARGS__ << \"]: \", debugg(__VA_ARGS__)\n#define dump(x) cerr << __LINE__ << \" \" << #x << \" = \" << (x) << endl\n#else\n#define debug(...) (void(0))\n#define dump(x) (void(0))\n#endif\nusing namespace std;\ntypedef long long ll;\ntypedef vector<ll> vl;\ntypedef vector<vl> vvl;\ntypedef vector<char> vc;\ntypedef vector<string> vs;\ntypedef vector<bool> vb;\ntypedef vector<double> vd;\ntypedef pair<ll,ll> P;\ntypedef pair<int,int> pii;\ntypedef vector<P> vpl;\ntypedef tuple<ll,ll,ll> tapu;\n#define rep(i,n) for(int i=0; i<(n); i++)\n#define REP(i,a,b) for(int i=(a); i<(b); i++)\n#define all(x) (x).begin(), (x).end()\n#define rall(x) (x).rbegin(), (x).rend()\ntemplate<typename T1,typename T2> inline bool chmin(T1 &a,T2 b){\n\tif(a>b){\n\t\ta = b; return true;\n\t}\n\telse return false;\n}\ntemplate<typename T1,typename T2> inline bool chmax(T1 &a,T2 b){\n\tif(a<b){\n\t\ta = b; return true;\n\t}\n\telse return false;\n}\ntemplate<typename T> inline void print(T &a){\n int sz = a.size();\n for(auto itr = a.begin(); itr != a.end(); itr++){\n\t\tcout << *itr;\n sz--;\n if(sz) cout << \" \";\n\t}\n cout << \"\\n\";\n}\ntemplate<typename T1,typename T2> inline void print2(T1 a, T2 b){\n\tcout << a << \" \" << b << \"\\n\";\n}\ntemplate<typename T1,typename T2,typename T3> inline void print3(T1 a, T2 b, T3 c){\n\tcout << a << \" \" << b << \" \" << c << \"\\n\";\n}\nvoid mark() {cout << \"#\" << \"\\n\";}\nll pcount(ll x) {return __builtin_popcountll(x);}\n//#include <atcoder/maxflow>\n//using namespace atcoder;\nconst int inf = (1<<30)-1;\nconst ll linf = 1LL<<61;\nconst int MAX = 510000;\nint dy[8] = {0,1,0,-1,1,-1,-1,1};\nint dx[8] = {-1,0,1,0,1,-1,1,-1};\nconst double pi = acos(-1);\nconst double eps = 1e-9;\n//const int mod = 1e9 + 7;\nconst int mod = 998244353;\n\nnamespace geometry {\n // Point : 複素数型を位置ベクトルとして扱う\n // 実軸(real)をx軸、挙軸(imag)をy軸として見る\n using D = long double;\n using Point = complex<D>;\n const D EPS = 1e-7;\n const D PI = acosl(-1);\n\n inline bool equal(const D &a, const D &b) { return fabs(a - b) < EPS; }\n\n // 単位ベクトル(unit vector)を求める\n Point unitVector(const Point &a) { return a / abs(a); }\n\n // 法線ベクトル(normal vector)を求める\n // 90度回転した単位ベクトルをかける\n // -90度がよければPoint(0, -1)をかける\n Point normalVector(const Point &a) { return a * Point(0, 1); }\n\n // 内積(dot product) : a・b = |a||b|cosΘ\n D dot(const Point &a, const Point &b) {\n return (a.real() * b.real() + a.imag() * b.imag());\n }\n\n // 外積(cross product) : a×b = |a||b|sinΘ\n D cross(const Point &a, const Point &b) {\n return (a.real() * b.imag() - a.imag() * b.real());\n }\n\n // 点pを反時計回りにtheta度回転\n // thetaはラジアン!!!\n Point rotate(const Point &p, const D &theta) {\n return Point(cos(theta) * p.real() - sin(theta) * p.imag(),\n sin(theta) * p.real() + cos(theta) * p.imag());\n }\n\n // ラジアン->度\n D radianToDegree(const D &radian) { return radian * 180.0 / PI; }\n\n // 度->ラジアン\n D degreeToRadian(const D &degree) { return degree * PI / 180.0; }\n\n // 点の回転方向\n // 点a, b, cの位置関係について(aが基準点)\n int ccw(const Point &a, Point b, Point c) {\n b -= a, c -= a;\n // 点a, b, c が\n // 反時計回りの時、\n if(cross(b, c) > EPS) return 1;\n // 時計回りの時、\n if(cross(b, c) < -EPS) return -1;\n // c, a, bがこの順番で同一直線上にある時、\n if(dot(b, c) < 0) return 2;\n // a, b, cがこの順番で同一直線上にある場合、\n if(norm(b) < norm(c)) return -2;\n // cが線分ab上にある場合、\n return 0;\n }\n\n // Line : 直線を表す構造体\n // b - a で直線・線分を表せる\n struct Line {\n Point a, b;\n Line() = default;\n Line(Point a, Point b) : a(a), b(b) {}\n // Ax+By=C\n Line(D A, D B, D C) {\n if(equal(A, 0)) {\n a = Point(0, C / B), b = Point(1, C / B);\n } else if(equal(B, 0)) {\n b = Point(C / A, 0), b = Point(C / A, 1);\n } else {\n a = Point(0, C / B), b = Point(C / A, 0);\n }\n }\n };\n\n // Segment : 線分を表す構造体\n // Lineと同じ\n struct Segment : Line {\n Segment() = default;\n\n Segment(Point a, Point b) : Line(a, b) {}\n };\n\n // Circle : 円を表す構造体\n // pが中心の位置ベクトル、rは半径\n struct Circle {\n Point p;\n D r;\n\n Circle() = default;\n\n Circle(Point p, D r) : p(p), r(r) {}\n };\n\n // 2直線の直交判定 : a⊥b <=> dot(a, b) = 0\n bool isOrthogonal(const Line &a, const Line &b) {\n return equal(dot(a.b - a.a, b.b - b.a), 0);\n }\n // 2直線の平行判定 : a//b <=> cross(a, b) = 0\n bool isParallel(const Line &a, const Line &b) {\n return equal(cross(a.b - a.a, b.b - b.a), 0);\n }\n\n // 点cが直線ab上にあるか\n bool isPointOnLine(const Point &a, const Point &b, const Point &c) {\n return isParallel(Line(a, b), Line(a, c));\n }\n\n // 点cが\"線分\"ab上にあるか\n bool isPointOnSegment(const Point &a, const Point &b, const Point &c) {\n // |a-c| + |c-b| <= |a-b| なら線分上\n return (abs(a - c) + abs(c - b) < abs(a - b) + EPS);\n }\n\n // 直線lと点pの距離を求める\n D distanceBetweenLineAndPoint(const Line &l, const Point &p) {\n return abs(cross(l.b - l.a, p - l.a)) / abs(l.b - l.a);\n }\n\n // 線分lと点pの距離を求める\n // 定義:点pから「線分lのどこか」への最短距離\n D distanceBetweenSegmentAndPoint(const Segment &l, const Point &p) {\n if(dot(l.b - l.a, p - l.a) < EPS) return abs(p - l.a);\n if(dot(l.a - l.b, p - l.b) < EPS) return abs(p - l.b);\n return abs(cross(l.b - l.a, p - l.a)) / abs(l.b - l.a);\n }\n\n // 直線s, tの交点の計算\n Point crossPoint(const Line &s, const Line &t) {\n D d1 = cross(s.b - s.a, t.b - t.a);\n D d2 = cross(s.b - s.a, s.b - t.a);\n if(equal(abs(d1), 0) && equal(abs(d2), 0)) return t.a;\n return t.a + (t.b - t.a) * (d2 / d1);\n }\n\n // 線分s, tの交点の計算\n Point crossPoint(const Segment &s, const Segment &t) {\n return crossPoint(Line(s), Line(t));\n }\n\n // 線分sと線分tが交差しているかどうか\n // bound:線分の端点を含むか\n bool isIntersect(const Segment &s, const Segment &t, bool bound) {\n return ccw(s.a, s.b, t.a) * ccw(s.a, s.b, t.b) < bound &&\n ccw(t.a, t.b, s.a) * ccw(t.a, t.b, s.b) < bound;\n }\n\n // 線分sとtの距離\n D distanceBetweenSegments(const Segment &s, const Segment &t) {\n if(isIntersect(s, t, 1)) return (D)(0);\n D ans = distanceBetweenSegmentAndPoint(s, t.a);\n chmin(ans, distanceBetweenSegmentAndPoint(s, t.b));\n chmin(ans, distanceBetweenSegmentAndPoint(t, s.a));\n chmin(ans, distanceBetweenSegmentAndPoint(t, s.b));\n return ans;\n }\n\n // 射影(projection)\n // 直線(線分)lに点pから引いた垂線の足を求める\n Point projection(const Line &l, const Point &p) {\n D t = dot(p - l.a, l.a - l.b) / norm(l.a - l.b);\n return l.a + (l.a - l.b) * t;\n }\n\n Point projection(const Segment &l, const Point &p) {\n D t = dot(p - l.a, l.a - l.b) / norm(l.a - l.b);\n return l.a + (l.a - l.b) * t;\n }\n\n // 反射(reflection)\n // 直線lを対称軸として点pと線対称の位置にある点を求める\n Point reflection(const Line &l, const Point &p) {\n return p + (projection(l, p) - p) * (D)2.0;\n }\n\n // 2つの円の交差判定\n // 返り値は共通接線の数\n int isIntersect(const Circle &c1, const Circle &c2) {\n D d = abs(c1.p - c2.p);\n // 2つの円が離れている場合\n if(d > c1.r + c2.r + EPS) return 4;\n // 外接している場合\n if(equal(d, c1.r + c2.r)) return 3;\n // 内接している場合\n if(equal(d, abs(c1.r - c2.r))) return 1;\n // 内包している場合\n if(d < abs(c1.r - c2.r) - EPS) return 0;\n return 2;\n }\n\n // 2つの円の交点\n vector<Point> crossPoint(const Circle &c1, const Circle &c2) {\n vector<Point> res;\n int mode = isIntersect(c1, c2);\n // 2つの中心の距離\n D d = abs(c1.p - c2.p);\n // 2円が離れている場合\n if(mode == 4) return res;\n // 1つの円がもう1つの円に内包されている場合\n if(mode == 0) return res;\n // 2円が外接する場合\n if(mode == 3) {\n D t = c1.r / (c1.r + c2.r);\n res.emplace_back(c1.p + (c2.p - c1.p) * t);\n return res;\n }\n // 内接している場合\n if(mode == 1) {\n if(c2.r < c1.r - EPS) {\n res.emplace_back(c1.p + (c2.p - c1.p) * (c1.r / d));\n } else {\n res.emplace_back(c2.p + (c1.p - c2.p) * (c2.r / d));\n }\n return res;\n }\n // 2円が重なる場合\n D rc1 = (c1.r * c1.r + d * d - c2.r * c2.r) / (2 * d);\n D rs1 = sqrt(c1.r * c1.r - rc1 * rc1);\n if(c1.r - abs(rc1) < EPS) rs1 = 0;\n Point e12 = (c2.p - c1.p) / abs(c2.p - c1.p);\n res.emplace_back(c1.p + rc1 * e12 + rs1 * e12 * Point(0, 1));\n res.emplace_back(c1.p + rc1 * e12 + rs1 * e12 * Point(0, -1));\n return res;\n }\n\n // 点pが円cの内部(円周上も含む)に入っているかどうか\n bool isInCircle(const Circle &c, const Point &p) {\n D d = abs(c.p - p);\n return (equal(d, c.r) || d < c.r - EPS);\n }\n\n // 円cと直線lの交点\n vector<Point> crossPoint(const Circle &c, const Line &l) {\n vector<Point> res;\n D d = distanceBetweenLineAndPoint(l, c.p);\n // 交点を持たない\n if(d > c.r + EPS) return res;\n // 接する\n Point h = projection(l, c.p);\n if(equal(d, c.r)) {\n res.emplace_back(h);\n return res;\n }\n Point e = unitVector(l.b - l.a);\n D ph = sqrt(c.r * c.r - d * d);\n res.emplace_back(h - e * ph);\n res.emplace_back(h + e * ph);\n return res;\n }\n\n // 点pを通る円cの接線\n // 2本あるので、接点のみを返す\n vector<Point> tangentToCircle(const Point &p, const Circle &c) {\n return crossPoint(c, Circle(p, sqrt(norm(c.p - p) - c.r * c.r)));\n }\n\n // 円の共通接線\n vector<Line> tangent(const Circle &a, const Circle &b) {\n vector<Line> ret;\n // 2円の中心間の距離\n D g = abs(a.p - b.p);\n // 円が内包されている場合\n if(equal(g, 0)) return ret;\n Point u = unitVector(b.p - a.p);\n Point v = rotate(u, PI / 2);\n for(int s : {-1, 1}) {\n D h = (a.r + b.r * s) / g;\n if(equal(h * h, 1)) {\n ret.emplace_back(a.p + (h > 0 ? u : -u) * a.r,\n a.p + (h > 0 ? u : -u) * a.r + v);\n\n } else if(1 - h * h > 0) {\n Point U = u * h, V = v * sqrt(1 - h * h);\n ret.emplace_back(a.p + (U + V) * a.r,\n b.p - (U + V) * (b.r * s));\n ret.emplace_back(a.p + (U - V) * a.r,\n b.p - (U - V) * (b.r * s));\n }\n }\n return ret;\n }\n\n // 多角形の面積を求める\n D PolygonArea(const vector<Point> &p) {\n D res = 0;\n int n = p.size();\n for(int i = 0; i < n - 1; i++) res += cross(p[i], p[i + 1]);\n res += cross(p[n - 1], p[0]);\n return res * 0.5;\n }\n\n // 凸多角形かどうか\n bool isConvex(const vector<Point> &p) {\n int n = p.size();\n int now, pre, nxt;\n for(int i = 0; i < n; i++) {\n pre = (i - 1 + n) % n;\n nxt = (i + 1) % n;\n now = i;\n if(ccw(p[pre], p[now], p[nxt]) == -1) return false;\n }\n return true;\n }\n\n // 凸包 O(NlogN)\n vector<Point> ConvexHull(vector<Point> &p) {\n int n = (int)p.size(), k = 0;\n sort(all(p), [](const Point &a, const Point &b) {\n return (real(a) != real(b) ? real(a) < real(b) : imag(a) < imag(b));\n });\n vector<Point> ch(2 * n);\n // 一直線上の3点を含める -> (< -EPS)\n // 含め無い -> (< EPS)\n for(int i = 0; i < n; ch[k++] = p[i++]) { // lower\n while(k >= 2 &&\n cross(ch[k - 1] - ch[k - 2], p[i] - ch[k - 1]) < EPS)\n --k;\n }\n for(int i = n - 2, t = k + 1; i >= 0; ch[k++] = p[i--]) { // upper\n while(k >= t &&\n cross(ch[k - 1] - ch[k - 2], p[i] - ch[k - 1]) < EPS)\n --k;\n }\n ch.resize(k - 1);\n return ch;\n }\n\n // 多角形gに点pが含まれているか?\n // 含まれる:2, 辺上にある:1, 含まれない:0\n int isContained(const vector<Point> &g, const Point &p) {\n bool in = false;\n int n = (int)g.size();\n for(int i = 0; i < n; i++) {\n Point a = g[i] - p, b = g[(i + 1) % n] - p;\n if(imag(a) > imag(b)) swap(a, b);\n if(imag(a) <= EPS && EPS < imag(b) && cross(a, b) < -EPS) in = !in;\n if(cross(a, b) == 0 && dot(a, b) <= 0) return 1;\n }\n return (in ? 2 : 0);\n }\n\n vector<Point> convex_cut(const vector<Point> &g, Line l){\n vector<Point> ret;\n for(int i=0; i<g.size(); i++){\n Point now = g[i], nxt = g[(i+1)%g.size()];\n if(ccw(l.a,l.b,now) != -1) ret.push_back(now);\n if(ccw(l.a,l.b,now) * ccw(l.a,l.b,nxt) < 0){\n ret.push_back(crossPoint(Line(now,nxt),l));\n }\n }\n return ret;\n }\n\n bool equal (const Point &a, const Point &b) {\n return equal(a.real(),b.real()) && equal(a.imag(),b.imag());\n }\n\n D circlesIntersectArea(Circle &a, Circle &b){\n double d = abs(a.p - b.p);\n if(d >= a.r + b.r - EPS) return 0; //2円が離れている\n auto R = minmax(a.r,b.r);\n if(d <= R.second - R.first + EPS) return R.first*R.first*pi; //円が円を内包する\n D theta1 = acos((d*d + a.r*a.r - b.r*b.r)/(2*a.r*d));\n D theta2 = acos((d*d + b.r*b.r - a.r*a.r)/(2*b.r*d));\n D ret = a.r*a.r*(theta1-cos(theta1)*sin(theta1)) + b.r*b.r*(theta2-cos(theta2)*sin(theta2));\n return ret;\n }\n} // namespace geometry\n\nusing namespace geometry;\n\nbool solve(){\n int n,m; cin >> n >> m;\n if(!n) return false;\n int N = n+m;\n vector<Point> a(n),b(m),c(N);\n rep(i,n){\n int x,y; cin >> x >> y;\n a[i] = Point(x,y);\n c[i] = Point(x,y);\n }\n rep(i,m){\n int x,y; cin >> x >> y;\n b[i] = Point(x,y);\n c[i+n] = Point(x,y);\n }\n auto comp = [&](pair<Point,int> a, pair<Point,int> b) -> bool {\n if(a.first.real() != b.first.real()) return a.first.real() < b.first.real();\n if(a.first.imag() != b.first.imag()) return a.first.imag() < b.first.imag();\n return a.second < b.second;\n };\n rep(i,N) rep(j,i){\n vector<pair<Point,int>> zeros;\n rep(l,2){\n bool f = true;\n rep(k,n){\n int cres = ccw(a[k],c[i],c[j]);\n if(abs(cres) == 1){\n cres = (cres + 1) / 2;\n if(l != cres) f = false;\n }else{\n zeros.emplace_back(a[k],0);\n }\n }\n rep(k,m){\n int cres = ccw(b[k],c[i],c[j]);\n if(abs(cres) == 1){\n cres = (cres + 1) / 2;\n if(1-l != cres) f = false;\n }else{\n zeros.emplace_back(b[k],1);\n }\n }\n if(!f) continue;\n sort(all(zeros),comp);\n int diff = 0;\n int sz = zeros.size();\n rep(k,sz-1){\n if(zeros[k].second != zeros[k+1].second) diff++;\n }\n if(diff <= 1){\n cout << \"YES\\n\";\n return true;\n }\n }\n }\n cout << \"NO\\n\";\n return true;\n}\n\nint main(){\n while(solve());\n}", "accuracy": 1, "time_ms": 4020, "memory_kb": 3104, "score_of_the_acc": -1, "final_rank": 17 }, { "submission_id": "aoj_1298_6583270", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\n#define rep(i, s, t) for (int i = (int)(s); i < (int)(t); ++i)\n#define revrep(i, t, s) for (int i = (int)(t)-1; i >= (int)(s); --i)\n#define all(x) begin(x), end(x)\ntemplate <typename T> bool chmax(T& a, const T& b) { return a < b ? (a = b, 1) : 0; }\ntemplate <typename T> bool chmin(T& a, const T& b) { return a > b ? (a = b, 1) : 0; }\n\nusing T = double;\nusing Vec = std::complex<T>;\n\nconst T PI = std::acos(-1);\n\nconstexpr T eps = 1e-12;\ninline bool eq(T a, T b) { return std::abs(a - b) < eps; }\ninline bool eq(Vec a, Vec b) { return std::abs(a - b) < eps; }\ninline bool lt(T a, T b) { return a < b - eps; }\ninline bool leq(T a, T b) { return a < b + eps; }\n\nstd::istream& operator>>(std::istream& is, Vec& p) {\n T x, y;\n is >> x >> y;\n p = {x, y};\n return is;\n}\n\nstruct Line {\n Vec p1, p2;\n Line() = default;\n Line(const Vec& p1, const Vec& p2) : p1(p1), p2(p2) {}\n Vec dir() const { return p2 - p1; }\n};\n\nstruct Segment {\n Vec p1, p2;\n Segment() = default;\n Segment(const Vec& p1, const Vec& p2) : p1(p1), p2(p2) {}\n Vec dir() const { return p2 - p1; }\n};\n\nstruct Circle {\n Vec c;\n T r;\n Circle() = default;\n Circle(const Vec& c, T r) : c(c), r(r) {}\n};\n\nusing Polygon = std::vector<Vec>;\n\nT dot(const Vec& a, const Vec& b) {\n return (std::conj(a) * b).real();\n}\n\nT cross(const Vec& a, const Vec& b) {\n return (std::conj(a) * b).imag();\n}\n\nVec rot(const Vec& a, T ang) {\n return a * Vec(std::cos(ang), std::sin(ang));\n}\n\nVec projection(const Line& l, const Vec& p) {\n return l.p1 + dot(p - l.p1, l.dir()) * l.dir() / std::norm(l.dir());\n}\n\nVec reflection(const Line& l, const Vec& p) {\n return T(2) * projection(l, p) - p;\n}\n\n// 0: collinear\n// 1: counter-clockwise\n// 2: clockwise\nint ccw(const Vec& a, const Vec& b, const Vec& c) {\n if (eq(cross(b - a, c - a), 0)) return 0;\n if (lt(cross(b - a, c - a), 0)) return -1;\n return 1;\n}\n\nLine bisector(const Vec& p, const Vec& q) {\n auto m = (p + q) / T(2);\n auto v = q - p;\n return Line(m, m + Vec(-v.imag(), v.real()));\n}\n\nbool intersect(const Segment& s, const Vec& p) {\n Vec u = s.p1 - p, v = s.p2 - p;\n return eq(cross(u, v), 0) && leq(dot(u, v), 0);\n}\n\n// 0: outside\n// 1: on the border\n// 2: inside\nint intersect(const Polygon& poly, const Vec& p) {\n const int n = poly.size();\n bool in = 0;\n for (int i = 0; i < n; ++i) {\n auto a = poly[i] - p, b = poly[(i+1)%n] - p;\n if (eq(cross(a, b), 0) && (lt(dot(a, b), 0) || eq(dot(a, b), 0))) return 1;\n if (a.imag() > b.imag()) std::swap(a, b);\n if (leq(a.imag(), 0) && lt(0, b.imag()) && lt(cross(a, b), 0)) in ^= 1;\n }\n return in ? 2 : 0;\n}\n\nbool intersect(const Segment& s, const Segment& t) {\n auto a = s.p1, b = s.p2;\n auto c = t.p1, d = t.p2;\n if (ccw(a, b, c) != ccw(a, b, d) && ccw(c, d, a) != ccw(c, d, b)) return 2;\n if (intersect(s, c) || intersect(s, d) || intersect(t, a) || intersect(t, b)) return 1;\n return 0;\n}\n\nbool intersect(const Polygon& poly1, const Polygon& poly2) {\n const int n = poly1.size();\n const int m = poly2.size();\n for (int i = 0; i < n; ++i) {\n for (int j = 0; j < m; ++j) {\n if (intersect(Segment(poly1[i], poly1[(i+1)%n]), Segment(poly2[j], poly2[(j+1)%n]))) {\n return true;\n }\n }\n }\n return intersect(poly1, poly2[0]) || intersect(poly2, poly1[0]);\n}\n\n\n// 0: inside\n// 1: inscribe\n// 2: intersect\n// 3: circumscribe\n// 4: outside\nint intersect(const Circle& c1, const Circle& c2) {\n T d = std::abs(c1.c - c2.c);\n if (lt(d, std::abs(c2.r - c1.r))) return 0;\n if (eq(d, std::abs(c2.r - c1.r))) return 1;\n if (eq(c1.r + c2.r, d)) return 3;\n if (lt(c1.r + c2.r, d)) return 4;\n return 2;\n}\n\nT dist(const Line& l, const Vec& p) {\n return std::abs(cross(p - l.p1, l.dir())) / std::abs(l.dir());\n}\n\nT dist(const Segment& s, const Vec& p) {\n if (lt(dot(p - s.p1, s.dir()), 0)) return std::abs(p - s.p1);\n if (lt(dot(p - s.p2, -s.dir()), 0)) return std::abs(p - s.p2);\n return std::abs(cross(p - s.p1, s.dir())) / std::abs(s.dir());\n}\n\nT dist(const Segment& s, const Segment& t) {\n if (intersect(s, t)) return T(0);\n return std::min({dist(s, t.p1), dist(s, t.p2), dist(t, s.p1), dist(t, s.p2)});\n}\n\nVec intersection(const Line& l, const Line& m) {\n Vec r = m.p1 - l.p1;\n assert(!eq(cross(l.dir(), m.dir()), 0)); // not parallel\n return l.p1 + cross(m.dir(), r) / cross(m.dir(), l.dir()) * l.dir();\n}\n\nstd::vector<Vec> intersection(const Circle& c, const Line& l) {\n T d = dist(l, c.c);\n if (lt(c.r, d)) return {}; // no intersection\n Vec e1 = l.dir() / std::abs(l.dir());\n Vec e2 = Vec(-e1.imag(), e1.real());\n if (ccw(c.c, l.p1, l.p2) == 1) e2 *= -1;\n if (eq(c.r, d)) return {c.c + d*e2}; // tangent\n T t = std::sqrt(c.r*c.r - d*d);\n return {c.c + d*e2 + t*e1, c.c + d*e2 - t*e1};\n}\n\nstd::vector<Vec> intersection(const Circle& c1, const Circle& c2) {\n T d = std::abs(c1.c - c2.c);\n if (lt(c1.r + c2.r, d)) return {}; // outside\n Vec e1 = (c2.c - c1.c) / std::abs(c2.c - c1.c);\n Vec e2 = Vec(-e1.imag(), e1.real());\n if (lt(d, std::abs(c2.r - c1.r))) return {}; // contain\n if (eq(d, std::abs(c2.r - c1.r))) return {c1.c + c1.r*e1}; // tangent\n T x = (c1.r*c1.r - c2.r*c2.r + d*d) / (2*d);\n T y = std::sqrt(c1.r*c1.r - x*x);\n return {c1.c + x*e1 + y*e2, c1.c + x*e1 - y*e2};\n}\n\nT area(const Polygon& poly) {\n const int n = poly.size();\n T res = 0;\n for (int i = 0; i < n; ++i) {\n res += cross(poly[i], poly[(i + 1) % n]);\n }\n return std::abs(res) / T(2);\n}\n\nT area_intersection(const Circle& c1, const Circle& c2) {\n T d = std::abs(c2.c - c1.c);\n if (leq(c1.r + c2.r, d)) return 0; // outside\n if (leq(d, std::abs(c2.r - c1.r))) { // inside\n T r = std::min(c1.r, c2.r);\n return PI * r * r;\n }\n T ans = 0;\n T a;\n a = std::acos((c1.r*c1.r+d*d-c2.r*c2.r)/(2*c1.r*d));\n ans += c1.r*c1.r*(a - std::sin(a)*std::cos(a));\n a = std::acos((c2.r*c2.r+d*d-c1.r*c1.r)/(2*c2.r*d));\n ans += c2.r*c2.r*(a - std::sin(a)*std::cos(a));\n return ans;\n}\n\nbool is_convex(const Polygon& poly) {\n int n = poly.size();\n for (int i = 0; i < n; ++i) {\n if (lt(cross(poly[(i+1)%n] - poly[i], poly[(i+2)%n] - poly[(i+1)%n]), 0)) {\n return false;\n }\n }\n return true;\n}\n\nstd::vector<Vec> convex_cut(const Polygon& poly, const Line& l) {\n const int n = poly.size();\n std::vector<Vec> res;\n for (int i = 0; i < n; ++i) {\n auto p = poly[i], q = poly[(i+1)%n];\n if (ccw(l.p1, l.p2, p) != -1) {\n if (res.empty() || !eq(res.back(), p)) {\n res.push_back(p);\n }\n }\n if (ccw(l.p1, l.p2, p) * ccw(l.p1, l.p2, q) < 0) {\n auto c = intersection(Line(p, q), l);\n if (res.empty() || !eq(res.back(), c)) {\n res.push_back(c);\n }\n }\n }\n return res;\n}\n\nVec centroid(const Vec& A, const Vec& B, const Vec& C) {\n assert(ccw(A, B, C) != 0);\n return (A + B + C) / T(3);\n}\n\nVec incenter(const Vec& A, const Vec& B, const Vec& C) {\n assert(ccw(A, B, C) != 0);\n T a = std::abs(B - C);\n T b = std::abs(C - A);\n T c = std::abs(A - B);\n return (a*A + b*B + c*C) / (a + b + c);\n}\n\nVec circumcenter(const Vec& A, const Vec& B, const Vec& C) {\n assert(ccw(A, B, C) != 0);\n return intersection(bisector(A, B), bisector(A, C));\n}\n\n// large error but beautiful\n// Vec circumcenter(const Vec& A, const Vec& B, const Vec& C) {\n// assert(ccw(A, B, C) != 0);\n// Vec p = C - B, q = A - C, r = B - A;\n// T a = std::norm(p) * dot(q, r);\n// T b = std::norm(q) * dot(r, p);\n// T c = std::norm(r) * dot(p, q);\n// return (a*A + b*B + c*C) / (a + b + c);\n// }\n\nstd::pair<Vec, Vec> tangent_points(const Circle& c, const Vec& p) {\n auto m = (p + c.c) / T(2);\n auto is = intersection(c, Circle(m, std::abs(p - m)));\n return {is[0], is[1]};\n}\n\n// for each l, l.p1 is a tangent point of c1\nstd::vector<Line> common_tangents(Circle c1, Circle c2) {\n assert(!eq(c1.c, c2.c) || !eq(c1.r, c2.r));\n int cnt = intersect(c1, c2); // number of common tangents\n std::vector<Line> ret;\n if (cnt == 0) {\n return ret;\n }\n\n // external\n if (eq(c1.r, c2.r)) {\n auto d = c2.c - c1.c;\n Vec e(-d.imag(), d.real());\n e = e / std::abs(e) * c1.r;\n ret.push_back(Line(c1.c + e, c1.c + e + d));\n ret.push_back(Line(c1.c - e, c1.c - e + d));\n } else {\n auto p = (-c2.r*c1.c + c1.r*c2.c) / (c1.r - c2.r);\n if (cnt == 1) {\n Vec q(-p.imag(), p.real());\n return {Line(p, q)};\n } else {\n auto [a, b] = tangent_points(c1, p);\n ret.push_back(Line(a, p));\n ret.push_back(Line(b, p));\n }\n }\n\n // internal\n auto p = (c2.r*c1.c + c1.r*c2.c) / (c1.r + c2.r);\n if (cnt == 3) {\n Vec q(-p.imag(), p.real());\n ret.push_back(Line(p, q));\n } else if (cnt == 4) {\n auto [a, b] = tangent_points(c1, p);\n ret.push_back(Line(a, p));\n ret.push_back(Line(b, p));\n }\n\n return ret;\n}\n\nvoid sort_by_arg(std::vector<Vec>& pts) {\n std::sort(pts.begin(), pts.end(), [&](auto& p, auto& q) {\n if ((p.imag() < 0) != (q.imag() < 0)) return (p.imag() < 0);\n if (cross(p, q) == 0) {\n if (p == Vec(0, 0)) return !(q.imag() < 0 || (q.imag() == 0 && q.real() > 0));\n if (q == Vec(0, 0)) return (p.imag() < 0 || (p.imag() == 0 && p.real() > 0));\n return (p.real() > q.real());\n }\n return (cross(p, q) > 0);\n });\n}\n\nstd::vector<Vec> convex_hull(std::vector<Vec>& pts) {\n int n = pts.size();\n if (n == 1) return pts;\n std::sort(pts.begin(), pts.end(), [](const Vec& v1, const Vec& v2) {\n return (v1.imag() != v2.imag()) ? (v1.imag() < v2.imag()) : (v1.real() < v2.real());\n });\n int k = 0; // the number of vertices in the convex hull\n std::vector<Vec> ch(2 * n);\n // right\n for (int i = 0; i < n; ++i) {\n while (k > 1 && lt(cross(ch[k-1] - ch[k-2], pts[i] - ch[k-1]), 0)) --k;\n ch[k++] = pts[i];\n }\n int t = k;\n // left\n for (int i = n - 2; i >= 0; --i) {\n while (k > t && lt(cross(ch[k-1] - ch[k-2], pts[i] - ch[k-1]), 0)) --k;\n ch[k++] = pts[i];\n }\n ch.resize(k - 1);\n return ch;\n}\n\ntemplate <typename T>\nostream& operator<<(ostream& os, const vector<T>& v) {\n for (int i = 0; i < (int) v.size(); ++i) os << v[i] << \" \";\n return os;\n}\n\n\nint main() {\n ios_base::sync_with_stdio(false);\n cin.tie(nullptr);\n cout << fixed << setprecision(15);\n\n while (true) {\n int n, m;\n cin >> n >> m;\n if (n == 0 && m == 0) break;\n vector<Vec> black(n), white(m);\n for (auto& p : black) cin >> p;\n for (auto& p : white) cin >> p;\n black = convex_hull(black);\n white = convex_hull(white);\n cout << (intersect(black, white) ? \"NO\" : \"YES\") << \"\\n\";\n }\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3456, "score_of_the_acc": -0.4731, "final_rank": 10 }, { "submission_id": "aoj_1298_6385868", "code_snippet": "#line 2 \"library/bits/stdc++.h\"\n\n// C\n#ifndef _GLIBCXX_NO_ASSERT\n#include <cassert>\n#endif\n#include <cctype>\n#include <cerrno>\n#include <cfloat>\n#include <ciso646>\n#include <climits>\n#include <clocale>\n#include <cmath>\n#include <csetjmp>\n#include <csignal>\n#include <cstdarg>\n#include <cstddef>\n#include <cstdio>\n#include <cstdlib>\n#include <cstring>\n#include <ctime>\n\n#if __cplusplus >= 201103L\n#include <ccomplex>\n#include <cfenv>\n#include <cinttypes>\n#include <cstdbool>\n#include <cstdint>\n#include <ctgmath>\n#include <cwchar>\n#include <cwctype>\n#endif\n\n// C++\n#include <algorithm>\n#include <bitset>\n#include <complex>\n#include <deque>\n#include <exception>\n#include <fstream>\n#include <functional>\n#include <iomanip>\n#include <ios>\n#include <iosfwd>\n#include <iostream>\n#include <istream>\n#include <iterator>\n#include <limits>\n#include <list>\n#include <locale>\n#include <map>\n#include <memory>\n#include <new>\n#include <numeric>\n#include <ostream>\n#include <queue>\n#include <set>\n#include <sstream>\n#include <stack>\n#include <stdexcept>\n#include <streambuf>\n#include <string>\n#include <typeinfo>\n#include <utility>\n#include <valarray>\n#include <vector>\n\n#if __cplusplus >= 201103L\n#include <array>\n#include <atomic>\n#include <chrono>\n#include <condition_variable>\n#include <forward_list>\n#include <future>\n#include <initializer_list>\n#include <mutex>\n#include <random>\n#include <ratio>\n#include <regex>\n#include <scoped_allocator>\n#include <system_error>\n#include <thread>\n#include <tuple>\n#include <typeindex>\n#include <type_traits>\n#include <unordered_map>\n#include <unordered_set>\n#endif\n#line 2 \"Source.cpp\"\nusing namespace std;\n\ntypedef string::const_iterator State;\n#define eps 1e-8L\n#define MAX_MOD 1000000007LL\n#define GYAKU 500000004LL\n#define MOD 998244353LL\n#define pb push_back\n#define mp make_pair\ntypedef long long ll;\ntypedef long double ld;\n#define REP(a, b) for (long long(a) = 0; (a) < (b); ++(a))\n#define ALL(x) (x).begin(), (x).end()\n\n#line 2 \"library/kotamanegi/geometry.hpp\"\n#define _USE_MATH_DEFINES\nusing namespace std;\n\ntypedef long double ld;\ntypedef complex<ld> Point;\ntypedef pair<Point, Point> Segment;\ntypedef Segment Line;\ntypedef vector<Point> Polygon;\n\nconst ld EPS = 1e-10;\n\nld abs(Line x)\n{\n return abs(x.second - x.first);\n}\n\n// dot = |a||b| cos(theta)\nld dot(Point a, Point b)\n{\n return a.real() * b.real() + a.imag() * b.imag();\n}\n\n// cross = |a||b| sin(theta)\nld cross(Point a, Point b)\n{\n return a.real() * b.imag() - a.imag() * b.real();\n}\n\n// 射影: 直線sと点pから引いた垂線の交点\nPoint projection(Line s, Point p)\n{\n Point base = s.second - s.first;\n ld r = dot(p - s.first, base) / norm(base);\n return s.first + base * r;\n}\n\n// 反射: 点pを直線sと線対称に移動した点\nPoint reflection(Line s, Point p)\n{\n return p + (projection(s, p) - p) * Point{2.0, 0.0};\n}\n\nLine vertical_bisector(Line x)\n{\n Point now = (x.first + x.second) * Point{0.5, 0};\n return Line(now, now + (x.second - x.first) * Point{0, M_PI_2});\n}\n\nenum class Position\n{\n COUNTER_CLOCKWISE, // 反時計回り\n CLOCKWISE, // 時計回り\n ON_LINE_BACK, // Lineの始点側に点がある\n ON_LINE_FRONT, // Lineの終点側に点がある\n ON_SEGMENT // Line上に点がある\n};\n\n// ccw: 線分sと点pの位置関係を示す\nPosition ccw(Line s, Point p)\n{\n Point a = s.second - s.first, b = p - s.first;\n if (cross(a, b) > eps)\n return Position::COUNTER_CLOCKWISE;\n if (cross(a, b) < -eps)\n return Position::CLOCKWISE;\n if (dot(a, b) < -eps)\n return Position::ON_LINE_BACK;\n if (norm(a) < norm(b))\n return Position::ON_LINE_FRONT;\n return Position::ON_SEGMENT;\n}\n\n// 平行判定\nbool is_parallel(Point a, Point b) { return abs(cross(a, b)) < EPS; }\nbool is_parallel(Line a, Line b) { return is_parallel(a.second - a.first, b.second - b.first); }\n\n// 直交判定\nbool is_orthogonal(Point a, Point b) { return abs(dot(a, b)) < EPS; }\nbool is_orthogonal(Line a, Line b) { return is_orthogonal(a.second - a.first, b.second - b.first); }\n\n// 交差判定\nbool is_intersect_line(Line s1, Segment s2)\n{\n if (cross(s1.second - s1.first, s2.first - s1.first) * cross(s1.second - s1.first, s2.second - s1.first) > eps)\n {\n return false;\n }\n if (ccw(s1, s2.first) == Position::ON_LINE_BACK and ccw(s1, s2.second) == Position::ON_LINE_BACK)\n {\n return false;\n }\n if (ccw(s1, s2.first) == Position::ON_LINE_FRONT and ccw(s1, s2.second) == Position::ON_LINE_FRONT)\n {\n return false;\n }\n return true;\n}\n\nbool is_intersect(Segment s1, Segment s2)\n{\n for (int i = 0; i < 2; ++i)\n {\n if (!is_intersect_line(s1, s2))\n {\n return false;\n }\n swap(s1, s2);\n }\n return true;\n}\n\nbool is_intersect(Polygon s1, Polygon s2)\n{\n for (int i = 0; i < s1.size(); ++i)\n {\n for (int q = 0; q < s2.size(); ++q)\n {\n if (is_intersect(Segment{s1[i], s1[(i + 1) % s1.size()]}, Segment{s2[q], s2[(q + 1) % s2.size()]}))\n return true;\n }\n }\n return false;\n}\n\n// 交差点\nPoint intersection_point(Line s1, Line s2)\n{\n ld s, t;\n ld deno = cross(s1.second - s1.first, s2.second - s2.first) + eps;\n s = cross(s2.first - s1.first, s2.second - s2.first) / deno;\n return s1.first + s * (s1.second - s1.first);\n}\n\n//線分と点の距離\nld distance(Segment s, Point p)\n{\n if (dot(s.second - s.first, p - s.first) < eps)\n return abs(p - s.first);\n if (dot(s.first - s.second, p - s.second) < eps)\n return abs(p - s.second);\n return abs(cross(s.second - s.first, p - s.first)) / abs(s);\n}\n\n//線分と線分の距離\nld distance(Segment s1, Segment s2)\n{\n if (is_intersect(s1, s2))\n {\n return 0;\n }\n return min({distance(s1, s2.first), distance(s1, s2.second), distance(s2, s1.first), distance(s2, s1.second)});\n}\n//多角形の面積\nld area(Polygon poly)\n{\n ld ans = 0;\n for (int i = 0; i < poly.size(); ++i)\n {\n Point a = poly[i], b = poly[(i + 1) % poly.size()];\n ans += cross(a, b);\n }\n ans /= 2.0L;\n return abs(ans);\n}\n\n//凸多角形か判定\nbool is_convex(Polygon poly)\n{\n for (int i = 0; i < poly.size(); ++i)\n {\n Line a = {poly[i], poly[(i + 1) % poly.size()]};\n if (ccw(a, poly[(i + 2) % poly.size()]) == Position::CLOCKWISE)\n return false;\n }\n return true;\n}\n\n//多角形上に点があるか判定\nbool is_on_polygon(Polygon poly, Point p)\n{\n for (int i = 0; i < poly.size(); ++i)\n {\n Line a = {poly[i], poly[(i + 1) % poly.size()]};\n if (ccw(a, p) == Position::ON_SEGMENT)\n return true;\n }\n return false;\n}\n\n//多角形内に点があるか判定\nbool is_inside_polygon(Polygon poly, Point p)\n{\n if (is_on_polygon(poly, p))\n return true;\n const int n = poly.size();\n int wn = 0;\n for (int i = 0; i < n; ++i)\n {\n ld vt = (p.imag() - poly[i].imag()) / (poly[(i + 1) % n].imag() - poly[i].imag());\n if (p.real() < (poly[i].real() + vt * (poly[(i + 1) % n].real() - poly[i].real())))\n {\n if ((poly[i].imag() <= p.imag()) and (poly[(i + 1) % n].imag() > p.imag()))\n {\n wn++;\n }\n else if ((poly[i].imag() > p.imag()) and (poly[(i + 1) % n].imag() <= p.imag()))\n {\n wn--;\n }\n }\n }\n if (wn != 0)\n return true;\n return false;\n}\n\n// 凸包\nPolygon convex_hull(Polygon poly)\n{\n if (poly.size() <= 1)\n return poly;\n sort(ALL(poly), [](auto &l, auto &r)\n {\n if(l.real() != r.real()) return l.real() < r.real();\n return l.imag() < r.imag(); });\n Polygon ans;\n ans.push_back(poly[0]);\n ans.push_back(poly[1]);\n for (int i = 2; i < poly.size(); ++i)\n {\n while (ans.size() >= 2)\n {\n Point x = ans[ans.size() - 2];\n Point y = ans[ans.size() - 1];\n if (ccw({x, y}, poly[i]) == Position::COUNTER_CLOCKWISE)\n {\n ans.pop_back();\n }\n else\n break;\n }\n ans.push_back(poly[i]);\n }\n\n Polygon reversed_ans;\n reversed_ans.push_back(poly[0]);\n reversed_ans.push_back(poly[1]);\n for (int i = 2; i < poly.size(); ++i)\n {\n while (reversed_ans.size() >= 2)\n {\n Point x = reversed_ans[reversed_ans.size() - 2];\n Point y = reversed_ans[reversed_ans.size() - 1];\n if (ccw({x, y}, poly[i]) == Position::CLOCKWISE)\n {\n reversed_ans.pop_back();\n }\n else\n break;\n }\n reversed_ans.push_back(poly[i]);\n }\n for (int q = reversed_ans.size() - 2; q >= 1; --q)\n {\n ans.push_back(reversed_ans[q]);\n }\n return ans;\n}\n\n// 凸多角形の直径\nld diameter(Polygon poly)\n{\n ld ans = 0;\n int itr = 0;\n REP(i, poly.size())\n {\n if (abs(poly[i] - poly[0]) > abs(poly[itr] - poly[0]))\n {\n itr = i;\n }\n }\n REP(i, poly.size())\n {\n while (abs(poly[(itr + 1) % poly.size()] - poly[i]) > abs(poly[itr] - poly[i]))\n {\n itr++;\n itr %= poly.size();\n }\n ans = max(ans, abs(poly[itr] - poly[i]));\n }\n return ans;\n}\n\n// 凸多角形の切断\nPolygon convex_cut(Polygon poly, Line l)\n{\n Polygon ans;\n for (int i = 0; i < poly.size(); ++i)\n {\n Point now = poly[i];\n Point nxt = poly[(i + 1) % poly.size()];\n if (ccw(l, now) != Position::CLOCKWISE)\n {\n ans.push_back(now);\n }\n if (is_intersect_line(l, {now, nxt}))\n {\n ans.push_back(intersection_point(l, {now, nxt}));\n }\n }\n return ans;\n}\n#line 17 \"Source.cpp\"\n\n#define int long long\n\nvoid solve()\n{\n int n, m;\n cin >> n >> m;\n if (n == 0)\n exit(0);\n Polygon x, y;\n REP(i, 2)\n {\n REP(q, n)\n {\n ld a, b;\n cin >> a >> b;\n x.push_back({a, b});\n }\n swap(x, y);\n swap(n, m);\n }\n\n x = convex_hull(x);\n y = convex_hull(y);\n if (is_intersect(x, y))\n {\n cout << \"NO\" << endl;\n }\n else\n {\n for (auto t : x)\n {\n if (is_inside_polygon(y, t))\n {\n cout << \"NO\" << endl;\n return;\n }\n }\n for (auto t : y)\n {\n if (is_inside_polygon(x, t))\n {\n cout << \"NO\" << endl;\n return;\n }\n }\n cout << \"YES\" << endl;\n }\n}\n\n#undef int\n\n// generated by oj-template v4.7.2\n// (https://github.com/online-judge-tools/template-generator)\nint main()\n{\n // Fasterize input/output script\n ios::sync_with_stdio(false);\n cin.tie(nullptr);\n cout << fixed << setprecision(10);\n // scanf/printf user should delete this fasterize input/output script\n\n int t = 1000;\n // cin >> t; // comment out if solving multi testcase\n for (int testCase = 1; testCase <= t; ++testCase)\n {\n solve();\n }\n return 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3492, "score_of_the_acc": -0.5215, "final_rank": 12 }, { "submission_id": "aoj_1298_6377217", "code_snippet": "#pragma GCC optimize(\"Ofast\")\n#include <iostream>\n#include <vector>\n#include <algorithm>\n#include <map>\n#include <queue>\n#include <cstdio>\n#include <ctime>\n#include <assert.h>\n#include <chrono>\n#include <random>\n#include <numeric>\n#include <set>\n#include <deque>\n#include <stack>\n#include <sstream>\n#include <utility>\n#include <cstring>\n#include <unordered_map>\n#include <unordered_set>\n#include <tuple>\n#include <array>\n#include <bitset>\nusing namespace std;\ntypedef long long int ll;\ntypedef unsigned long long ull;\n\nmt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());\nll myRand(ll B) {\n return (ull)rng() % B;\n}\ninline ll time() {\n return static_cast<long double>(chrono::duration_cast<chrono::nanoseconds>(chrono::steady_clock::now().time_since_epoch()).count()) * 1e-9;\n}\n\nconst long double eps = 1e-13;\n\nstruct point{\n ll x,y;\n point() {}\n point(ll x,ll y): x(x), y(y) {}\n point& operator-=(const point &p){\n x -= p.x; y -= p.y;\n return *this;\n }\n point& operator+=(const point &p){\n x += p.x; y += p.y;\n return *this;\n }\n point& operator*=(ll d){\n x *= d; y *= d;\n return *this;\n }\n point& operator/=(ll d){\n x /= d; y /= d;\n return *this;\n }\n const point operator+ (const point& p) const;\n const point operator- (const point& p) const;\n const point operator* (ll d) const;\n const point operator/ (ll d) const;\n};\nconst point point::operator+ (const point& p) const{\n point res(*this); return res += p;\n}\nconst point point::operator- (const point& p) const{\n point res(*this); return res -= p;\n}\nconst point point::operator* (ll d) const{\n point res(*this); return res *= d;\n}\nconst point point::operator/ (ll d) const{\n point res(*this); return res /= d;\n}\n\nll cross(point a,point b){\n return a.x*b.y-a.y*b.x;\n}\n\n// 2色の頂点群を、各領域に含まれる色が単色になるように、直線で分割できるか判定\n// (ただし、直線上に頂点は含まない)\n// 入力が格子点の場合、整数範囲で処理できる\n// vの前半は頂点の座標で、後半は色を表す(0or1)\n// O(N^3) ただ枝狩りが結構効く気がする\n// verify\n// AOJ-1298 CC-FLIPOINTS\nbool can_separate_points(vector<pair<point,bool>> &v){\n int n = v.size();\n vector<pair<point,bool>> u;\n // 2点決め打ち\n for(int i=0;i<n;i++){\n for(int j=i+1;j<n;j++){\n \n // 外積の値を見て直線上のどっち側or直線上にあるか分類\n int b = 0, w = 0;\n u.clear();\n auto p1 = v[i].first;\n auto p2 = v[j].first;\n for(int k=0;k<n;k++){\n if(i == k or j == k){\n u.push_back(v[k]);\n continue;\n }\n auto p = v[k].first;\n ll cr = cross(p2-p1, p-p1);\n if(cr == 0){\n u.push_back(v[k]);\n }\n else if(cr > 0){\n b |= (1<<v[k].second);\n }\n else if(cr < 0){\n w |= (1<<v[k].second);\n }\n // 枝狩り\n if(b == 3 or w == 3 or (b&w)) break;\n }\n if(b == 3 or w == 3 or (b&w)) continue;\n\n // 適当な基準でソートする\n sort(u.begin(), u.end(), [&](auto i, auto j){\n auto p1 = i.first;\n auto p2 = j.first;\n if(p1.x != p2.x) return p1.x < p2.x;\n else return p1.y < p2.y;\n });\n\n // 決め打った直線上の点に関して、色の入れ替わる回数が1回以下ならOK\n // 逆にそうでないなら、分割不可能(枝狩りポイント)\n int cnt = 0;\n for(int k=0;k+1<u.size();k++){\n if(u[k].second != u[k+1].second) cnt++;\n }\n return (cnt <= 1);\n }\n }\n return false;\n}\n\nint main(){\n cin.tie(nullptr);\n ios::sync_with_stdio(false);\n int n,m;\n while(cin >> n >> m, n+m){\n vector<pair<point,bool>> v(n+m);\n for(int i=0;i<n+m;i++){\n cin >> v[i].first.x >> v[i].first.y;\n v[i].second = (i<n);\n }\n if(can_separate_points(v)){\n printf(\"YES\\n\");\n }\n else{\n printf(\"NO\\n\");\n }\n }\n}", "accuracy": 1, "time_ms": 80, "memory_kb": 3168, "score_of_the_acc": -0.1035, "final_rank": 1 }, { "submission_id": "aoj_1298_6377214", "code_snippet": "#pragma GCC optimize(\"Ofast\")\n#include <iostream>\n#include <vector>\n#include <algorithm>\n#include <map>\n#include <queue>\n#include <cstdio>\n#include <ctime>\n#include <assert.h>\n#include <chrono>\n#include <random>\n#include <numeric>\n#include <set>\n#include <deque>\n#include <stack>\n#include <sstream>\n#include <utility>\n#include <cstring>\n#include <unordered_map>\n#include <unordered_set>\n#include <tuple>\n#include <array>\n#include <bitset>\nusing namespace std;\ntypedef long long int ll;\ntypedef unsigned long long ull;\n\nmt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());\nll myRand(ll B) {\n return (ull)rng() % B;\n}\ninline ll time() {\n return static_cast<long double>(chrono::duration_cast<chrono::nanoseconds>(chrono::steady_clock::now().time_since_epoch()).count()) * 1e-9;\n}\n\nconst long double eps = 1e-13;\n\nstruct point{\n ll x,y;\n point() {}\n point(ll x,ll y): x(x), y(y) {}\n point& operator-=(const point &p){\n x -= p.x; y -= p.y;\n return *this;\n }\n point& operator+=(const point &p){\n x += p.x; y += p.y;\n return *this;\n }\n point& operator*=(ll d){\n x *= d; y *= d;\n return *this;\n }\n point& operator/=(ll d){\n x /= d; y /= d;\n return *this;\n }\n const point operator+ (const point& p) const;\n const point operator- (const point& p) const;\n const point operator* (ll d) const;\n const point operator/ (ll d) const;\n};\nconst point point::operator+ (const point& p) const{\n point res(*this); return res += p;\n}\nconst point point::operator- (const point& p) const{\n point res(*this); return res -= p;\n}\nconst point point::operator* (ll d) const{\n point res(*this); return res *= d;\n}\nconst point point::operator/ (ll d) const{\n point res(*this); return res /= d;\n}\n\nll cross(point a,point b){\n return a.x*b.y-a.y*b.x;\n}\n\n// 2色の頂点群を、各領域に含まれる色が単色になるように、直線で分割できるか判定\n// (ただし、直線上に頂点は含まない)\n// 入力が格子点の場合、整数範囲で処理できる\n// vの前半は頂点の座標で、後半は色を表す(0or1)\n// O(N^3) ただ枝狩りが結構効く気がする\n// verify\n// AOJ-1298 CC-FLIPOINTS\nbool can_separate_points(vector<pair<point,bool>> &v){\n int n = v.size();\n vector<pair<point,bool>> u;\n // 2点決め打ち\n for(int i=0;i<n;i++){\n for(int j=i+1;j<n;j++){\n // 外積の値を見て直線上のどっち側or直線上にあるか分類\n int b = 0, w = 0;\n u.clear();\n auto p1 = v[i].first;\n auto p2 = v[j].first;\n for(int k=0;k<n;k++){\n if(i == k or j == k){\n u.push_back(v[k]);\n continue;\n }\n auto p = v[k].first;\n ll cr = cross(p2-p1, p-p1);\n if(cr == 0){\n u.push_back(v[k]);\n }\n else if(cr > 0){\n b |= (1<<v[k].second);\n }\n else if(cr < 0){\n w |= (1<<v[k].second);\n }\n // 枝狩り\n if(b == 3 or w == 3 or (b&w)) break;\n }\n if(b == 3 or w == 3 or (b&w)) continue;\n\n // 適当な基準でソートする\n sort(u.begin(), u.end(), [&](auto i, auto j){\n auto p1 = i.first;\n auto p2 = j.first;\n if(p1.x != p2.x) return p1.x < p2.x;\n else return p1.y < p2.y;\n });\n\n // 決め打った直線上の点に関して、色の入れ替わる回数が1回以下ならOK\n // 逆にそうでないなら、分割不可能(枝狩りポイント)\n int cnt = 0;\n for(int k=0;k+1<u.size();k++){\n if(u[k].second != u[k+1].second) cnt++;\n }\n return (cnt <= 1);\n }\n }\n return false;\n}\n\nint main(){\n cin.tie(nullptr);\n ios::sync_with_stdio(false);\n int n,m;\n while(cin >> n >> m, n+m){\n vector<pair<point,bool>> v(n+m);\n for(int i=0;i<n+m;i++){\n cin >> v[i].first.x >> v[i].first.y;\n v[i].second = (i<n);\n }\n if(can_separate_points(v)){\n printf(\"YES\\n\");\n }\n else{\n printf(\"NO\\n\");\n }\n }\n}", "accuracy": 1, "time_ms": 80, "memory_kb": 3392, "score_of_the_acc": -0.4046, "final_rank": 4 }, { "submission_id": "aoj_1298_6377212", "code_snippet": "#pragma GCC optimize(\"Ofast\")\n#include <iostream>\n#include <vector>\n#include <algorithm>\n#include <map>\n#include <queue>\n#include <cstdio>\n#include <ctime>\n#include <assert.h>\n#include <chrono>\n#include <random>\n#include <numeric>\n#include <set>\n#include <deque>\n#include <stack>\n#include <sstream>\n#include <utility>\n#include <cstring>\n#include <unordered_map>\n#include <unordered_set>\n#include <tuple>\n#include <array>\n#include <bitset>\nusing namespace std;\ntypedef long long int ll;\ntypedef unsigned long long ull;\n\nmt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());\nll myRand(ll B) {\n return (ull)rng() % B;\n}\ninline ll time() {\n return static_cast<long double>(chrono::duration_cast<chrono::nanoseconds>(chrono::steady_clock::now().time_since_epoch()).count()) * 1e-9;\n}\n\nconst long double eps = 1e-13;\n\nstruct point{\n ll x,y;\n point() {}\n point(ll x,ll y): x(x), y(y) {}\n point& operator-=(const point &p){\n x -= p.x; y -= p.y;\n return *this;\n }\n point& operator+=(const point &p){\n x += p.x; y += p.y;\n return *this;\n }\n point& operator*=(ll d){\n x *= d; y *= d;\n return *this;\n }\n point& operator/=(ll d){\n x /= d; y /= d;\n return *this;\n }\n const point operator+ (const point& p) const;\n const point operator- (const point& p) const;\n const point operator* (ll d) const;\n const point operator/ (ll d) const;\n};\nconst point point::operator+ (const point& p) const{\n point res(*this); return res += p;\n}\nconst point point::operator- (const point& p) const{\n point res(*this); return res -= p;\n}\nconst point point::operator* (ll d) const{\n point res(*this); return res *= d;\n}\nconst point point::operator/ (ll d) const{\n point res(*this); return res /= d;\n}\n\nll cross(point a,point b){\n return a.x*b.y-a.y*b.x;\n}\n\n// 2色の頂点群を、各領域に含まれる色が単色になるように、直線で分割できるか判定\n// (ただし、直線上に頂点は含まない)\n// 入力が格子点の場合、整数範囲で処理できる\n// vの前半は頂点の座標で、後半は色を表す(0or1)\n// O(N^3) ただ枝狩りが結構効く気がする\n// verify\n// AOJ-1298 CC-FLIPOINTS\nbool can_separate_points(vector<pair<point,int>> &v){\n int n = v.size();\n vector<pair<point,int>> u;\n // 2点決め打ち\n for(int i=0;i<n;i++){\n for(int j=i+1;j<n;j++){\n // 外積の値を見て直線上のどっち側or直線上にあるか分類\n int b = 0, w = 0;\n u.clear();\n auto p1 = v[i].first;\n auto p2 = v[j].first;\n for(int k=0;k<n;k++){\n if(i == k or j == k){\n u.push_back(v[k]);\n continue;\n }\n auto p = v[k].first;\n ll cr = cross(p2-p1, p-p1);\n if(cr == 0){\n u.push_back(v[k]);\n }\n else if(cr > 0){\n b |= (1<<v[k].second);\n }\n else if(cr < 0){\n w |= (1<<v[k].second);\n }\n // 枝狩り\n if(b == 3 or w == 3 or (b&w)) break;\n }\n if(b == 3 or w == 3 or (b&w)) continue;\n\n // 適当な基準でソートする\n sort(u.begin(), u.end(), [&](auto i, auto j){\n auto p1 = i.first;\n auto p2 = j.first;\n if(p1.x != p2.x) return p1.x < p2.x;\n else return p1.y < p2.y;\n });\n\n // 決め打った直線上の点に関して、色の入れ替わる回数が1回以下ならOK\n // 逆にそうでないなら、分割不可能(枝狩りポイント)\n int cnt = 0;\n for(int k=0;k+1<u.size();k++){\n if(u[k].second != u[k+1].second) cnt++;\n }\n return (cnt <= 1);\n }\n }\n return false;\n}\n\nint main(){\n cin.tie(nullptr);\n ios::sync_with_stdio(false);\n int n,m;\n while(cin >> n >> m, n+m){\n vector<pair<point,int>> v(n+m);\n for(int i=0;i<n+m;i++){\n cin >> v[i].first.x >> v[i].first.y;\n v[i].second = (i<n);\n }\n if(can_separate_points(v)){\n printf(\"YES\\n\");\n }\n else{\n printf(\"NO\\n\");\n }\n }\n}", "accuracy": 1, "time_ms": 90, "memory_kb": 3392, "score_of_the_acc": -0.407, "final_rank": 5 }, { "submission_id": "aoj_1298_6366202", "code_snippet": "#line 1 \"c.cpp\"\n/*\tauthor: Kite_kuma\n\tcreated: 2022.03.01 11:03:30 */\n\n// #ifdef LOCAL\n// #define _GLIBCXX_DEBUG\n// #endif\n#include <bits/stdc++.h>\nusing namespace std;\n\n#pragma region macros\n\n#define foa(s, v) for(auto &s : v)\n#define all(v) (v).begin(), (v).end()\n#define rall(v) (v).rbegin(), (v).rend()\n#define popcnt(n) __builtin_popcountll((long long)n)\n\n#define REPname(a, b, c, d, e, ...) e\n#define rep(...) REPname(__VA_ARGS__, REP3, REP2, REP1, REP0)(__VA_ARGS__)\n#define REP0(x) for(int Counter_in_rep_macro = 0; Counter_in_rep_macro < (x); ++Counter_in_rep_macro)\n#define REP1(i, x) for(int i = 0; i < (x); ++i)\n#define REP2(i, l, r) for(int i = (l); i < (r); ++i)\n#define REP3(i, l, r, c) for(int i = (l); i < (r); i += (c))\n\n#define DREPname(a, b, c, d, e, ...) e\n#define drep(...) DREPname(__VA_ARGS__, DREP3, DREP2, DREP1)(__VA_ARGS__)\n#define DREP1(i, x) for(int i = (x)-1; i >= 0; --i)\n#define DREP2(i, l, r) for(int i = (r)-1; i >= (l); --i)\n#define DREP3(i, l, r, c) for(int i = (r)-1; i >= (l); i -= (c))\n\n#pragma endregion\n\n#pragma region aliases\n\nusing ll = long long;\nusing ld = long double;\nusing ull = unsigned long long;\nusing vi = std::vector<int>;\nusing vvi = std::vector<std::vector<int>>;\nusing vvvi = std::vector<std::vector<std::vector<int>>>;\nusing vll = std::vector<ll>;\nusing vvll = std::vector<vll>;\nusing vvvll = std::vector<vvll>;\nusing pii = std::pair<int, int>;\nusing pll = std::pair<long long, long long>;\ntemplate <class T = ll>\nusing V = std::vector<T>;\ntemplate <class T = ll>\nusing VV = V<V<T>>;\ntemplate <class T = ll>\nusing VVV = V<V<V<T>>>;\ntemplate <class T = ll>\nusing pqup = std::priority_queue<T, std::vector<T>, std::greater<T>>;\ntemplate <class T = ll>\nusing pqdn = std::priority_queue<T>;\n\n#pragma endregion\n\n#pragma region constants\n\nconst int inf = 1e9;\nconst long long INF = 1e18;\nconst long double pi = acos(-1);\nconst char dl = '\\n';\nconst char sp = ' ';\nint dx[8] = {1, 0, -1, 0, 1, -1, -1, 1};\nint dy[8] = {0, 1, 0, -1, 1, 1, -1, -1};\n\nconst int mod_1000000007 = 1000000007;\nconst int mod_998244353 = 998244353;\n\n#pragma endregion\n\n#pragma region basic_operation\n\ntemplate <class T0, class T1, class T2>\ninline bool in_range(T0 x, T1 lef, T2 rig) {\n\treturn ((lef <= x) && (x < rig));\n}\n\ntemplate <class T>\ninline bool chmin(T &a, T b) {\n\tif(a > b) {\n\t\ta = b;\n\t\treturn true;\n\t}\n\treturn false;\n}\ntemplate <class T>\ninline bool chmax(T &a, T b) {\n\tif(a < b) {\n\t\ta = b;\n\t\treturn true;\n\t}\n\treturn false;\n}\n\nvoid Yes(bool f = 1) { std::cout << (f ? \"Yes\" : \"No\") << '\\n'; }\nvoid No() { std::cout << \"No\\n\"; }\nvoid YES(bool f = 1) { std::cout << (f ? \"YES\" : \"NO\") << '\\n'; }\nvoid NO() { std::cout << \"NO\\n\"; }\n\ntemplate <class T>\nvoid drop(T answer) {\n\tstd::cout << answer << '\\n';\n\texit(0);\n}\n\nvoid err(bool flag = true) {\n\tif(!flag) return;\n\tstd::cout << -1 << '\\n';\n\texit(0);\n}\n\ntemplate <class T>\nvoid vout(std::vector<T> const &v, bool vertically = 0) {\n\tif(vertically) {\n\t\tfor(auto const &a : v) {\n\t\t\tstd::cout << a << '\\n';\n\t\t}\n\t\treturn;\n\t}\n\tfor(auto it = v.begin(); it != v.end(); it++) {\n\t\tstd::cout << (*it);\n\t\tif(it != v.end() - 1) {\n\t\t\tstd::cout << ' ';\n\t\t}\n\t}\n\tstd::cout << '\\n';\n\treturn;\n}\n\ninline void print() { std::cout << '\\n'; }\ntemplate <class T>\ninline void print(T x) {\n\tstd::cout << x << '\\n';\n\treturn;\n}\ntemplate <typename Head, typename... Tail>\nvoid print(Head H, Tail... T) {\n\tstd::cout << H << \" \";\n\tprint(T...);\n}\n\ntemplate <class T>\nvoid add(std::vector<T> &v, T val) {\n\tfor(auto &a : v) a += val;\n\treturn;\n}\n\ntemplate <class T>\nT dup(T a, T b) {\n\tassert(b != 0);\n\treturn (a + b - 1) / b;\n}\n\ntemplate <class T>\nT greatest_lower_multiple(T x, T d) {\n\tif(d == 0) return 0;\n\tif(d < 0) d *= -1;\n\tT y = x % d;\n\tif(y < 0) y += d;\n\treturn x - y;\n}\n\ntemplate <class T>\nT least_upper_multiple(T x, T d) {\n\treturn -greatest_lower_multiple(-x, d);\n}\n\nlong long POW(long long a, long long n) {\n\tlong long res = 1;\n\twhile(n > 0) {\n\t\tif(n & 1) res = res * a;\n\t\ta = a * a;\n\t\tn >>= 1;\n\t}\n\treturn res;\n}\n\nlong long modpow(long long a, long long n, long long mod) {\t // a^n mod\n\tassert(n >= 0 && mod);\n\tif(mod == 1) return 0LL;\n\tlong long res = 1;\n\ta %= mod;\n\twhile(n > 0) {\n\t\tif(n & 1) res = res * a % mod;\n\t\ta = a * a % mod;\n\t\tn >>= 1;\n\t}\n\treturn res < 0 ? res + mod : res;\n}\n\n// return x which satisfies a * x % mod == gcd(a, mod)\n// not (mod | a)\nlong long modinv(long long a, long long mod) {\n\tlong long b = mod, u = 1, v = 0;\n\twhile(b) {\n\t\tlong long t = a / b;\n\t\ta -= t * b;\n\t\tstd::swap(a, b);\n\t\tu -= t * v;\n\t\tstd::swap(u, v);\n\t}\n\tu %= mod;\n\tif(u < 0) u += mod;\n\treturn u;\n}\n\ntemplate <class T>\nint lb(const std::vector<T> &a, const T x) {\n\treturn std::distance(a.begin(), std::lower_bound(a.begin(), a.end(), x));\n}\ntemplate <class T>\nint ub(const std::vector<T> &a, const T x) {\n\treturn std::distance(a.begin(), std::upper_bound(a.begin(), a.end(), x));\n}\ntemplate <class T>\nvoid unq_sort(std::vector<T> &a) {\n\tstd::sort(a.begin(), a.end());\n\ta.erase(std::unique(a.begin(), a.end()), a.end());\n}\ntemplate <class T>\nstd::vector<int> press(std::vector<T> &a) {\n\tauto vec = a;\n\tunq_sort(vec);\n\tstd::vector<int> ret;\n\tfor(auto &v : a) ret.push_back(lb(vec, v));\n\treturn ret;\n}\n\n#pragma endregion\n\n#pragma region input\n#define VEC(type, name, size) \\\n\tvector<type> name(size); \\\n\tscanner::INPUT(name)\n#define VVEC(type, name, h, w) \\\n\tvector<vector<type>> name(h, vector<type>(w)); \\\n\tscanner::INPUT(name)\n#define INT(...) \\\n\tint __VA_ARGS__; \\\n\tscanner::INPUT(__VA_ARGS__)\n#define LL(...) \\\n\tlong long __VA_ARGS__; \\\n\tscanner::INPUT(__VA_ARGS__)\n#define STR(...) \\\n\tstring __VA_ARGS__; \\\n\tscanner::INPUT(__VA_ARGS__)\n#define CHR(...) \\\n\tchar __VA_ARGS__; \\\n\tscanner::INPUT(__VA_ARGS__)\n#define DBL(...) \\\n\tdouble __VA_ARGS__; \\\n\tscanner::INPUT(__VA_ARGS__)\n#define LD(...) \\\n\tlong double __VA_ARGS__; \\\n\tscanner::INPUT(__VA_ARGS__)\n#define TPL3(type0, type1, type2, name) \\\n\tstd::tuple<type0, type1, type2> name; \\\n\tscanner::INPUT(name);\n#define TPL4(type0, type1, type2, type3, name) \\\n\tstd::tuple<type0, type1, type2, type3> name; \\\n\tscanner::INPUT(name);\n\nnamespace scanner {\ntemplate <class T>\nvoid scan(T &a) {\n\tstd::cin >> a;\n}\n\ntemplate <class T, class L>\nvoid scan(std::pair<T, L> &p) {\n\tscan(p.first);\n\tscan(p.second);\n}\n\ntemplate <class T0, class T1, class T2>\nvoid scan(std::tuple<T0, T1, T2> &p) {\n\tT0 t0;\n\tT1 t1;\n\tT2 t2;\n\tscan(t0);\n\tscan(t1);\n\tscan(t2);\n\tp = std::make_tuple(t0, t1, t2);\n}\n\ntemplate <class T0, class T1, class T2, class T3>\nvoid scan(std::tuple<T0, T1, T2, T3> &p) {\n\tT0 t0;\n\tT1 t1;\n\tT2 t2;\n\tT3 t3;\n\tscan(t0);\n\tscan(t1);\n\tscan(t2);\n\tscan(t3);\n\tp = std::make_tuple(t0, t1, t2, t3);\n}\n\ntemplate <class T>\nvoid scan(std::vector<T> &a) {\n\tfor(auto &i : a) scan(i);\n}\n\nvoid INPUT() {}\ntemplate <class Head, class... Tail>\nvoid INPUT(Head &head, Tail &... tail) {\n\tscan(head);\n\tINPUT(tail...);\n}\n} // namespace scanner\n\ntemplate <typename T1, typename T2>\nstd::istream &operator>>(std::istream &is, std::pair<T1, T2> &p) {\n\tis >> p.first >> p.second;\n\treturn is;\n}\n#pragma endregion\n\n#pragma region debug\n\n#pragma region output\ntemplate <typename T1, typename T2>\nstd::ostream &std::operator<<(std::ostream &os, const std::pair<T1, T2> &p) {\n\tos << p.first << \" \" << p.second;\n\treturn os;\n}\ntemplate <class T>\nstd::ostream &std::operator<<(std::ostream &os, const std::vector<T> &v) {\n\tfor(int i = 0; i < (int)v.size(); i++) {\n\t\tif(i) os << \" \";\n\t\tos << v[i];\n\t}\n\treturn os;\n}\n#pragma endregion\n\n#pragma region view\n\nnamespace viewer {\nvoid view(const long long e) {\n\tif(e == INF)\n\t\tstd::cerr << \"INF\";\n\telse if(e == -INF)\n\t\tstd::cerr << \"-INF\";\n\telse\n\t\tstd::cerr << e;\n}\n\nvoid view(const int e) {\n\tif(e == inf)\n\t\tstd::cerr << \"inf\";\n\telse if(e == -inf)\n\t\tstd::cerr << \"-inf\";\n\telse\n\t\tstd::cerr << e;\n}\n\ntemplate <typename T>\nvoid view(const T e) {\n\tstd::cerr << e;\n}\n\ntemplate <typename T, typename U>\nvoid view(const std::pair<T, U> &p) {\n\tstd::cerr << \"(\";\n\tview(p.first);\n\tstd::cerr << \", \";\n\tview(p.second);\n\tstd::cerr << \")\";\n}\n\ntemplate <class T0, class T1, class T2>\nvoid view(const std::tuple<T0, T1, T2> &p) {\n\tstd::cerr << \"(\";\n\tview(std::get<0>(p));\n\tstd::cerr << \", \";\n\tview(std::get<1>(p));\n\tstd::cerr << \", \";\n\tview(std::get<2>(p));\n\tstd::cerr << \")\";\n}\n\ntemplate <class T0, class T1, class T2, class T3>\nvoid view(const std::tuple<T0, T1, T2, T3> &p) {\n\tstd::cerr << \"(\";\n\tview(std::get<0>(p));\n\tstd::cerr << \", \";\n\tview(std::get<1>(p));\n\tstd::cerr << \", \";\n\tview(std::get<2>(p));\n\tstd::cerr << \", \";\n\tview(std::get<3>(p));\n\tstd::cerr << \")\";\n}\n\ntemplate <typename T>\nvoid view(const std::set<T> &s) {\n\tif(s.empty()) {\n\t\tstd::cerr << \"{ }\";\n\t\treturn;\n\t}\n\tstd::cerr << \"{ \";\n\tfor(auto &t : s) {\n\t\tview(t);\n\t\tstd::cerr << \", \";\n\t}\n\tstd::cerr << \"\\b\\b }\";\n}\n\ntemplate <typename T>\nvoid view(const std::unordered_set<T> &s) {\n\tif(s.empty()) {\n\t\tstd::cerr << \"{ }\";\n\t\treturn;\n\t}\n\tstd::cerr << \"{ \";\n\tfor(auto &t : s) {\n\t\tview(t);\n\t\tstd::cerr << \", \";\n\t}\n\tstd::cerr << \"\\b\\b }\";\n}\n\ntemplate <typename T>\nvoid view(const std::vector<T> &v) {\n\tif(v.empty()) {\n\t\tstd::cerr << \"{ }\";\n\t\treturn;\n\t}\n\tstd::cerr << \"{ \";\n\tfor(const auto &e : v) {\n\t\tview(e);\n\t\tstd::cerr << \", \";\n\t}\n\tstd::cerr << \"\\b\\b }\";\n}\n\ntemplate <typename T>\nvoid view(const std::vector<std::vector<T>> &vv) {\n\tstd::cerr << \"{\\n\";\n\tfor(const auto &v : vv) {\n\t\tstd::cerr << \"\\t\";\n\t\tview(v);\n\t\tstd::cerr << '\\n';\n\t}\n\tstd::cerr << \"}\";\n}\n\ntemplate <typename T, typename U>\nvoid view(const std::vector<std::pair<T, U>> &v) {\n\tstd::cerr << \"{\\n\";\n\tfor(const auto &c : v) {\n\t\tstd::cerr << \"\\t(\";\n\t\tview(c.first);\n\t\tstd::cerr << \", \";\n\t\tview(c.second);\n\t\tstd::cerr << \")\\n\";\n\t}\n\tstd::cerr << \"}\";\n}\n\ntemplate <class T0, class T1, class T2>\nvoid view(const std::vector<std::tuple<T0, T1, T2>> &v) {\n\tif(v.empty()) {\n\t\tstd::cerr << \"{ }\";\n\t\treturn;\n\t}\n\tstd::cerr << '{';\n\tfor(const auto &t : v) {\n\t\tstd::cerr << \"\\n\\t\";\n\t\tview(t);\n\t\tstd::cerr << \",\";\n\t}\n\tstd::cerr << \"\\n}\";\n}\n\ntemplate <class T0, class T1, class T2, class T3>\nvoid view(const std::vector<std::tuple<T0, T1, T2, T3>> &v) {\n\tif(v.empty()) {\n\t\tstd::cerr << \"{ }\";\n\t\treturn;\n\t}\n\tstd::cerr << '{';\n\tfor(const auto &t : v) {\n\t\tstd::cerr << \"\\n\\t\";\n\t\tview(t);\n\t\tstd::cerr << \",\";\n\t}\n\tstd::cerr << \"\\n}\";\n}\n\ntemplate <typename T, typename U>\nvoid view(const std::map<T, U> &m) {\n\tstd::cerr << \"{\\n\";\n\tfor(const auto &t : m) {\n\t\tstd::cerr << \"\\t[\";\n\t\tview(t.first);\n\t\tstd::cerr << \"] : \";\n\t\tview(t.second);\n\t\tstd::cerr << '\\n';\n\t}\n\tstd::cerr << \"}\";\n}\n\ntemplate <typename T, typename U>\nvoid view(const std::unordered_map<T, U> &m) {\n\tstd::cerr << \"{\\n\";\n\tfor(const auto &t : m) {\n\t\tstd::cerr << \"\\t[\";\n\t\tview(t.first);\n\t\tstd::cerr << \"] : \";\n\t\tview(t.second);\n\t\tstd::cerr << '\\n';\n\t}\n\tstd::cerr << \"}\";\n}\n} // namespace viewer\n\n#pragma endregion\n\n// when debugging : g++ foo.cpp -DLOCAL\n#ifdef LOCAL\nvoid debug_out() {}\ntemplate <typename Head, typename... Tail>\nvoid debug_out(Head H, Tail... T) {\n\tviewer::view(H);\n\tstd::cerr << \", \";\n\tdebug_out(T...);\n}\n#define debug(...) \\\n\tdo { \\\n\t\tstd::cerr << __LINE__ << \" [\" << #__VA_ARGS__ << \"] : [\"; \\\n\t\tdebug_out(__VA_ARGS__); \\\n\t\tstd::cerr << \"\\b\\b]\\n\"; \\\n\t} while(0)\n#define dump(x) \\\n\tdo { \\\n\t\tstd::cerr << __LINE__ << \" \" << #x << \" : \"; \\\n\t\tviewer::view(x); \\\n\t\tstd::cerr << '\\n'; \\\n\t} while(0)\n\n#else\n#define debug(...) (void(0))\n#define dump(x) (void(0))\n#endif\n\n#pragma endregion\n\n#line 8 \"Library/Math/Geometry_light.hpp\"\n\n#pragma region geometry_light\n\nusing Real = long double;\nusing R = Real;\nusing Point = std::complex<Real>;\nusing Vec = Point;\nconst Real EPS = 1e-10, PI = acos(-1);\n\ninline bool eq(const Real &a, const Real &b) { return fabs(b - a) < EPS; }\ninline bool eq(const Point &a, const Point &b) { return fabs(b - a) < EPS; }\n/*\n\t-1: a < b\n\t0 : a == b\n\t1 : a > b\n*/\ninline int compare(const Real &a, const Real &b) { return eq(a, b) ? 0 : a < b ? -1 : 1; }\ninline int sign(const Real &a) { return fabs(a) < EPS ? 0 : a < 0 ? -1 : 1; }\n\nnamespace std {\nconst Point operator*(const Point &p, const Real &d) { return Point(real(p) * d, imag(p) * d); }\n} // namespace std\n\nstd::istream &operator>>(std::istream &is, Point &p) {\n\tReal a, b;\n\tis >> a >> b;\n\tp = Point(a, b);\n\treturn is;\n}\nstd::ostream &operator<<(std::ostream &os, Point &p) { return os << std::fixed << std::setprecision(10) << p.real() << \" \" << p.imag(); }\n\n// rotate point 'p' for counter clockwise direction\nconst Point rotate(const Real &theta, const Point &p) {\n\treturn Point(cos(theta) * p.real() - sin(theta) * p.imag(), sin(theta) * p.real() + cos(theta) * p.imag());\n}\n\nconst Real radian_to_degree(const Real &r) { return (r * 180.0 / PI); }\n\nconst Real degree_to_radian(const Real &d) { return (d * PI / 180.0); }\n\n// get angle a-b-c (<pi)\nconst Real get_angle(const Point &a, const Point &b, const Point &c) {\n\tconst Point v(a - b), w(c - b);\n\tReal theta = fabs(atan2(w.imag(), w.real()) - atan2(v.imag(), v.real()));\n\treturn std::min(theta, 2 * PI - theta);\n}\n\nnamespace std {\nbool operator<(const Point &a, const Point &b) { return a.real() != b.real() ? a.real() < b.real() : a.imag() < b.imag(); }\n} // namespace std\n\nstruct Line {\n\tPoint a, b;\n\n\tLine() = default;\n\n\tLine(Point a, Point b) : a(a), b(b) {}\n\n\tLine(Real A, Real B, Real C) // Ax + By = C\n\t{\n\t\tif(eq(A, 0))\n\t\t\ta = Point(0, C / B), b = Point(1, C / B);\n\t\telse if(eq(B, 0))\n\t\t\tb = Point(C / A, 0), b = Point(C / A, 1);\n\t\telse\n\t\t\ta = Point(0, C / B), b = Point(C / A, 0);\n\t}\n\n\tfriend std::ostream &operator<<(std::ostream &os, Line &p) { return os << p.a << \" -- \" << p.b; }\n\n\tfriend std::istream &operator>>(std::istream &is, Line &a) { return is >> a.a >> a.b; }\n};\n\nstruct Segment : Line {\n\tSegment() = default;\n\n\tSegment(Point a, Point b) : Line(a, b) {}\n};\n\nstruct Circle {\n\tPoint p;\n\tReal r;\n\n\tCircle() = default;\n\n\tCircle(Point p, Real r) : p(p), r(r) {}\n};\n\nusing Points = std::vector<Point>;\nusing Polygon = std::vector<Point>;\nusing Segments = std::vector<Segment>;\nusing Lines = std::vector<Line>;\nusing Circles = std::vector<Circle>;\n\nconst Real cross(const Point &a, const Point &b) { return real(a) * imag(b) - imag(a) * real(b); }\nconst Real dot(const Point &a, const Point &b) { return real(a) * real(b) + imag(a) * imag(b); }\n\n// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_1_C\nconst int ccw(const Point &a, Point b, Point c) {\n\tb = b - a, c = c - a;\n\tif(cross(b, c) > EPS) return +1;\t\t// \"COUNTER_CLOCKWISE\"\n\tif(cross(b, c) < -EPS) return -1;\t\t// \"CLOCKWISE\"\n\tif(dot(b, c) < -EPS) return +2;\t\t\t// \"ONLINE_BACK\"\n\tif(norm(b) + EPS < norm(c)) return -2;\t// \"ONLINE_FRONT\"\n\treturn 0;\t\t\t\t\t\t\t\t// \"ON_SEGMENT\"\n}\n\n// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_2_A\nbool parallel(const Line &a, const Line &b) { return eq(cross(a.b - a.a, b.b - b.a), 0.0); }\n\n// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_2_A\nbool orthogonal(const Line &a, const Line &b) { return eq(dot(a.a - a.b, b.a - b.b), 0.0); }\n\n// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_1_A\nPoint projection(const Line &l, const Point &p) {\n\tdouble t = dot(p - l.a, l.a - l.b) / norm(l.a - l.b);\n\treturn l.a + (l.a - l.b) * t;\n}\n\nPoint projection(const Segment &l, const Point &p) {\n\tdouble t = dot(p - l.a, l.a - l.b) / norm(l.a - l.b);\n\treturn l.a + (l.a - l.b) * t;\n}\n\n// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_1_B\nPoint reflection(const Line &l, const Point &p) { return projection(l, p) * 2.0 - p; }\n\nint intersect(const Line &l, const Point &p) { return int(abs(ccw(l.a, l.b, p)) != 1); }\n\nint intersect(const Line &l, const Line &m) {\n\tif(intersect(l, m.a) && intersect(l, m.b)) return -1;\n\treturn int(abs(cross(l.b - l.a, m.b - m.a)) > EPS);\n}\n\nint intersect(const Segment &s, const Point &p) { return int(ccw(s.a, s.b, p) == 0); }\n\nint intersect(const Line &l, const Segment &s) {\n\tif(intersect(l, s.a) && intersect(l, s.b)) return -1;\n\treturn cross(l.b - l.a, s.a - l.a) * cross(l.b - l.a, s.b - l.a) < EPS;\n}\n\nReal distance(const Line &l, const Point &p);\n\nint intersect(const Circle &c, const Line &l) {\n\tReal d = c.r - distance(l, c.p);\n\treturn fabs(d) < EPS ? 1 : d > 0. ? 2 : 0;\n}\n\nint intersect(const Circle &c, const Point &p) { return int(abs(abs(p - c.p) - c.r) < EPS); }\n\n// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_2_B\nint intersect(const Segment &s, const Segment &t) {\n\tif(eq(s.a, s.b)) return intersect(t, s.a);\n\tif(intersect(Line(s), t.a) && intersect(Line(s), t.b) &&\n\t std::max(std::min(s.a, s.b), std::min(t.a, t.b)) < std::min(std::max(s.a, s.b), std::max(t.a, t.b)))\n\t\treturn -1;\n\treturn int(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);\n}\n\nint intersect(const Circle &c, const Segment &l) {\n\tconst Point h = projection(l, c.p);\n\tif(norm(h - c.p) - c.r * c.r > EPS) return 0;\n\tauto d1 = abs(c.p - l.a), d2 = abs(c.p - l.b);\n\tif(compare(d1, c.r) == -1 && compare(d2, c.r) == -1) return 0;\n\tif(d1 < c.r - EPS && d2 > c.r - EPS || d1 > c.r - EPS && d2 < c.r - EPS) return 1;\n\treturn dot(l.a - h, l.b - h) < 0 ? 2 : 0;\n}\n\nReal distance(const Point &a, const Point &b);\n\nint number_of_common_tangents(const Circle &c1, const Circle &c2) {\n\tReal r1 = std::min(c1.r, c2.r), r2 = std::max(c1.r, c2.r), d = distance(c1.p, c2.p);\n\tint com = compare(r1 + r2, d);\n\treturn com == 1 ? compare(d + r1, r2) + 1 : 3 - com;\n\t// if(c1.r < c2.r) swap(c1, c2);\n\t// Real d = abs(c1.p - c2.p);\n\t// if(compare(c1.r + c2.r, d) == -1) return 4;\n\t// if(eq(c1.r + c2.r, d)) return 3;\n\t// if(compare(c1.r - c2.r, d) == -1) return 2;\n\t// return int(eq(c1.r - c2.r, d));\n}\n\n// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_7_A&lang=jp\nint intersect(const Circle &c1, const Circle &c2) { return 2 - abs(2 - number_of_common_tangents(c1, c2)); }\n\nReal distance(const Point &a, const Point &b) { return abs(a - b); }\n\nReal distance(const Line &l, const Point &p) { return abs(p - projection(l, p)); }\n\nReal distance(const Line &l, const Line &m) { return intersect(l, m) ? 0 : distance(l, m.a); }\n\nReal distance(const Segment &s, const Point &p) {\n\tPoint r = projection(s, p);\n\treturn intersect(s, r) ? distance(r, p) : std::min(abs(s.a - p), abs(s.b - p));\n}\n\n// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_2_D\nReal distance(const Segment &a, const Segment &b) {\n\tif(intersect(a, b)) return 0;\n\treturn std::min({distance(a, b.a), distance(a, b.b), distance(b, a.a), distance(b, a.b)});\n}\n\nReal distance(const Line &l, const Segment &s) {\n\tif(intersect(l, s)) return 0;\n\treturn std::min(distance(l, s.a), distance(l, s.b));\n}\n\nPoint crosspoint(const Line &l, const Line &m) {\n\tReal A = cross(l.b - l.a, m.b - m.a);\n\tReal B = cross(l.b - l.a, l.b - m.a);\n\tif(eq(abs(A), 0.0) && eq(abs(B), 0.0)) return m.a;\n\treturn m.a + (m.b - m.a) * B / A;\n}\n\n// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_2_C\nPoint crosspoint(const Segment &l, const Segment &m) { return crosspoint(Line(l), Line(m)); }\n\n// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_7_D\nstd::pair<Point, Point> crosspoint(const Circle &c, const Line l) {\n\tPoint pr = projection(l, c.p);\n\tif(eq(distance(l, c.p), c.r)) return {pr, pr};\n\tVec v = (l.b - l.a) / abs(l.b - l.a) * sqrt(c.r * c.r - norm(pr - c.p));\n\treturn make_pair(pr - v, pr + v);\n\t// Vec e = (l.b - l.a) / abs(l.b - l.a);\n\t// double base = sqrt(c.r * c.r - norm(pr - c.p));\n\t// return {pr - e * base, pr + e * base};\n}\n\nstd::pair<Point, Point> crosspoint(const Circle &c, const Segment &l) {\n\tif(intersect(c, l) == 2) return crosspoint(c, Line(l.a, l.b));\n\tauto ret = crosspoint(c, Line(l.a, l.b));\n\tif(dot(l.a - ret.first, l.b - ret.first) < EPS)\n\t\tret.second = ret.first;\n\telse\n\t\tret.first = ret.second;\n\treturn ret;\n}\n\n// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_7_E\nstd::pair<Point, Point> crosspoint(const Circle &c1, const Circle &c2) {\n\tReal d = abs(c1.p - c2.p);\n\tReal a = acos((c1.r * c1.r + d * d - c2.r * c2.r) / (2 * c1.r * d)); // cosine theorem\n\tReal t = atan2(c2.p.imag() - c1.p.imag(), c2.p.real() - c1.p.real());\n\tPoint p1 = c1.p + Point(cos(t + a) * c1.r, sin(t + a) * c1.r);\n\tPoint p2 = c1.p + Point(cos(t - a) * c1.r, sin(t - a) * c1.r);\n\treturn make_pair(p1, p2);\n}\n\n// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_7_F\n// \"点 p を通る円 c の接線\"の接点2つ\nstd::pair<Point, Point> tangent(const Circle &c1, const Point &p2) {\n\treturn crosspoint(c1, Circle(p2, sqrt(norm(c1.p - p2) - c1.r * c1.r)));\n}\n\n// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_7_G\n// 円 c1, c2 の共通接線\nLines tangent(Circle c1, Circle c2) {\n\tLines ret;\n\tif(c1.r < c2.r) std::swap(c1, c2);\n\tReal g = norm(c1.p - c2.p);\n\tif(eq(g, 0)) return ret;\n\tVec u = (c2.p - c1.p) / Real(sqrt(g));\n\tVec v = rotate(PI * 0.5, u);\n\tfor(int s : {-1, 1}) {\n\t\tReal h = (c1.r + s * c2.r) / sqrt(g);\n\t\tif(eq(1 - h * h, 0)) {\n\t\t\tret.emplace_back(c1.p + u * c1.r, c1.p + (u + v) * c1.r);\n\t\t} else if(1 - h * h > 0) {\n\t\t\tPoint uu = u * h, vv = v * sqrt(1 - h * h);\n\t\t\tret.emplace_back(c1.p + (uu + vv) * c1.r, c2.p - (uu + vv) * c2.r * s);\n\t\t\tret.emplace_back(c1.p + (uu - vv) * c1.r, c2.p - (uu - vv) * c2.r * s);\n\t\t}\n\t}\n\treturn ret;\n}\n\n#pragma endregion\n#line 553 \"c.cpp\"\n\nbool solve(int n) {\n\tint m;\n\tcin >> m;\n\tPoints black, white;\n\tconst R alpha = 1;\n\trep(n) {\n\t\tPoint p;\n\t\tcin >> p;\n\t\tp = rotate(alpha, p);\n\t\tblack.push_back(p);\n\t}\n\trep(m) {\n\t\tPoint p;\n\t\tcin >> p;\n\t\tp = rotate(alpha, p);\n\t\twhite.push_back(p);\n\t}\n\n\tauto judge = [&](int b, int w, int blackside) {\n\t\t// int black_sign = 0; // undetermined;\n\t\t// const Line ln(black[b], white[w]);\n\t\tfoa(blackpt, black) {\n\t\t\tif(eq(blackpt, black[b])) continue;\n\t\t\tint c = (ccw(black[b], white[w], blackpt));\n\t\t\tif(c % 2 && blackside != c) return false;\n\t\t\tif(c % 2 == 0 && c != 2) return false;\n\t\t}\n\t\tfoa(whitept, white) {\n\t\t\tif(eq(whitept, white[w])) continue;\n\t\t\tint c = (ccw(black[b], white[w], whitept));\n\t\t\tif(c % 2 && blackside == c) return false;\n\t\t\tif(c % 2 == 0 && c != -2) return false;\n\t\t}\n\t\treturn true;\n\t};\n\n\tfor(int bside = -1; bside < 2; bside += 2) {\n\t\tfor(int b = 0; b < n; b++)\n\t\t\tfor(int w = 0; w < m; w++) {\n\t\t\t\tif(judge(b, w, bside)) return true;\n\t\t\t}\n\t}\n\n\treturn false;\n}\n\nint main() {\n\tstd::ios::sync_with_stdio(false);\n\tstd::cin.tie(nullptr);\n\tstd::cout << std::fixed << std::setprecision(15);\n\tsrand((unsigned)time(NULL));\n\n\tint n;\n\twhile(cin >> n && n) YES(solve(n));\n\n\treturn 0;\n}", "accuracy": 1, "time_ms": 380, "memory_kb": 3848, "score_of_the_acc": -1.0923, "final_rank": 19 }, { "submission_id": "aoj_1298_6337646", "code_snippet": "#include<bits/stdc++.h>\n#define rep(i,a,...) for(int i = (a)*(strlen(#__VA_ARGS__)!=0);i<(int)(strlen(#__VA_ARGS__)?__VA_ARGS__:(a));++i)\n#define per(i,a,...) for(int i = (strlen(#__VA_ARGS__)?__VA_ARGS__:(a))-1;i>=(int)(strlen(#__VA_ARGS__)?(a):0);--i)\n#define foreach(i, n) for(auto &i:(n))\n#define all(x) (x).begin(), (x).end()\n#define bit(x) (1ll << (x))\n#define lambda(RES_TYPE, ...) (function<RES_TYPE(__VA_ARGS__)>)[&](__VA_ARGS__) -> RES_TYPE\n#define method(FUNC_NAME, RES_TYPE, ...) function<RES_TYPE(__VA_ARGS__)> FUNC_NAME = lambda(RES_TYPE, __VA_ARGS__)\nusing namespace std;\nusing ll = long long;\nusing pii = pair<int,int>;\nusing pll = pair<ll,ll>;\n//const ll MOD = (ll)1e9+7;\nconst ll MOD = 998244353;\nconst int INF = (ll)1e9+7;\nconst ll INFLL = (ll)1e18;\ntemplate<class t>\nusing vvector = vector<vector<t>>;\ntemplate<class t>\nusing vvvector = vector<vector<vector<t>>>;\ntemplate<class t>\nusing priority_queuer = priority_queue<t, vector<t>, greater<t>>;\ntemplate<class t, class u> bool chmax(t &a, u b){if(a<b){a=b;return true;}return false;}\ntemplate<class t, class u> bool chmin(t &a, u b){if(a>b){a=b;return true;}return false;}\n#ifdef DEBUG\n#define debug(x) cout<<\"LINE \"<<__LINE__<<\": \"<<#x<<\" = \"<<x<<endl;\n#else\n#define debug(x) (void)0\n#endif\n\nnamespace templates{\n ll modpow(ll x, ll b,ll mod=MOD){\n ll res = 1;\n while(b){\n if(b&1)res = res * x % mod;\n x = x * x % mod;\n b>>=1;\n }\n return res;\n }\n\n ll modinv(ll x){\n return modpow(x, MOD-2);\n }\n\n bool was_output = false;\n\n void print();\n template <class t> void print(const vector<t> &);\n template <class t, class u> void print(const pair<t, u> &);\n template <class t> void print(const t&);\n template <class Head, class... Tail> void print(const Head&, const Tail&...);\n\n template <class t> void println(const vector<vector<t>>&);\n template <class t> void println(const vector<t>&);\n template <class t> void println(const t&);\n template <class Head, class... Tail> void println(const Head&, const Tail&...);\n void println();\n void newline();\n\n void print(){\n return;\n }\n\n template <class t>\n void print(const vector<t>&x){\n for(const t&i:x)print(i);\n }\n template <class t, class u>\n void print(const pair<t,u>&p){\n print(p.first);\n print(p.second);\n }\n template <class t>\n void print(const t&x){\n if(was_output)cout<<\" \";\n cout<<x;\n was_output = true;\n }\n template <class Head, class... Tail>\n void print(const Head&head,const Tail&...tail){\n print(head);\n print(tail...);\n }\n\n template<class t>\n void println(const vector<vector<t>>&x){\n for(vector<t> i:x)println(i);\n }\n template<class t>\n void println(const vector<t>&x){\n for(const t&i:x)print(i);\n println();\n }\n template <class t>\n void println(const t&x){\n print(x);\n println();\n }\n void println(){\n if(was_output){\n cout << endl;\n was_output = false;\n }\n }\n template <class Head, class... Tail>\n void println(const Head&head,const Tail&...tail){\n print(head);\n println(tail...);\n }\n\n void newline(){\n was_output = true;\n println();\n }\n\n template<class t>\n istream& operator>>(istream&is, vector<t>&x){\n for(auto &i:x)is >> i;\n return is;\n }\n\n template<class t, class u>\n istream& operator>>(istream&is, pair<t, u>&x){\n is >> x.first >> x.second;\n return is;\n }\n\n template<class t>\n ostream& operator<<(ostream&os, vector<t> &v){\n os << \"{\";\n for(t &i:v){\n os << i << \", \";\n }\n os << \"}\";\n return os;\n }\n\n template<class t = long long>\n t in(){\n t res; cin >> res; return res;\n }\n\n template<class t>\n vector<t> sorted(vector<t> line,function<bool(t,t)> comp=[](t a,t b){return a<b;}){\n sort(line.begin(),line.end(),comp);\n return line;\n }\n\n template<class t>\n vector<t> reversed(vector<t> line){\n reverse(line.begin(),line.end());\n return line;\n }\n string reversed(string str){\n reverse(str.begin(),str.end());\n return str;\n }\n\n long long gcd(long long a,long long b){\n while(b){\n a %= b;\n swap(a,b);\n }\n return a;\n }\n\n long long lcm(long long a,long long b){\n return a / gcd(a,b) * b;\n }\n\n class output_initializer{\n public:\n output_initializer(){\n ios::sync_with_stdio(false);\n cin.tie(nullptr);\n cout << setprecision(20);\n }\n };output_initializer OUTPUT_INITIALIZER_INSTANCE = output_initializer();\n}\n\nusing namespace templates;\n\nusing C = complex<ll>;\nusing CC = complex<double>;\n\nconst double EPS = 1e-6;\n\ntemplate<class t>\nt cross(complex<t> a,complex<t> b){\n return a.real() * b.imag() - a.imag() * b.real();\n}\ntemplate<class t>\nt dot(complex<t> a,complex<t> b){\n return a.real() * b.real() - a.imag() * b.imag();\n}\n\nCC toCC(C a){\n return CC(a.real(),a.imag());\n}\n\nvector<C> myconvex(vector<C> line){\n int n = line.size();\n if(n<=2)return line;\n vector<bool> flag(n,false);\n rep(i,n){\n rep(j,i+1,n){\n int ac = 0;\n int bc = 0;\n rep(k,n){\n if(i==k or j==k)continue;\n ll v = cross(line[i]-line[j],line[k]-line[j]);\n if(v < 0)++ac;\n if(v > 0)++bc;\n }\n if(ac==0 or bc==0){\n flag[i] = true;\n flag[j] = true;\n }\n }\n }\n CC mid;\n foreach(i,line)mid += toCC(i);\n mid /= n;\n vector<C> res;\n rep(i,n){\n if(flag[i])res.emplace_back(line[i]);\n }\n sort(all(res),lambda(bool,C a,C b){\n return arg(toCC(a)-mid) < arg(toCC(b)-mid);\n });\n return res;\n}\n\nbool func(int n,int m){\n vector<C> a;\n vector<C> b;\n rep(i,n){\n int x = in();\n int y = in();\n a.emplace_back(x,y);\n }\n rep(i,m){\n int x = in();\n int y = in();\n b.emplace_back(x,y);\n }\n a = myconvex(a);\n b = myconvex(b);\n method(check1,bool,C a,vector<C> line){\n int n = line.size();\n {\n double sum = 0;\n rep(i,n){\n sum += arg(toCC((line[(i+1)%n]-a))/toCC((line[i]-a)));\n }\n if(abs(sum) > 0.5)return true;\n }\n rep(i,n){\n C u = line[i];\n C v = line[(i+1)%n];\n if(cross(a-u,v-u)==0 and 0 <= dot(a-u,v-u) and dot(a-u,v-u) <= dot(v-u,v-u))return true;\n }\n return false;\n };\n method(check2,bool,C a,C b,C c,C d){\n if(0 <= cross(b-a,c-a) * cross(b-a,d-a))return false;\n if(0 <= cross(d-c,a-c) * cross(d-c,b-c))return false;\n return true;\n };\n n = a.size();\n m = b.size();\n if(n==1 and m==1)return true;\n foreach(i,a)if(check1(i,b))return false;\n foreach(i,b)if(check1(i,a))return false;\n rep(i,n){\n rep(j,m){\n if(check2(a[i],a[(i+1)%n],b[j],b[(j+1)%m]))return false;\n }\n }\n return true;\n}\n\nint main(){\n ios::sync_with_stdio(false);\n cin.tie(nullptr);\n while(true){\n int n = in();\n int m = in();\n if(n==0 and m==0)break;\n println(func(n,m) ? \"YES\" : \"NO\");\n }\n return 0;\n}", "accuracy": 1, "time_ms": 420, "memory_kb": 3796, "score_of_the_acc": -1.0324, "final_rank": 18 } ]
aoj_1301_cpp
Problem G: Malfatti Circles The configuration of three circles packed inside a triangle such that each circle is tangent to the other two circles and to two of the edges of the triangle has been studied by many mathematicians for more than two centuries. Existence and uniqueness of such circles for an arbitrary triangle are easy to prove. Many methods of numerical calculation or geometric construction of such circles from an arbitrarily given triangle have been discovered. Today, such circles are called the Malfatti circles . Figure 7 illustrates an example. The Malfatti circles of the triangle with the vertices (20, 80), (-40, -20) and (120, -20) are approximately the circle with the center (24.281677, 45.219486) and the radius 21.565935, the circle with the center (3.110950, 4.409005) and the radius 24.409005, and the circle with the center (54.556724, 7.107493) and the radius 27.107493. Figure 8 illustrates another example. The Malfatti circles of the triangle with the vertices (20, -20), (120, -20) and (-40, 80) are approximately the circle with the center (25.629089, −10.057956) and the radius 9.942044, the circle with the center (53.225883, −0.849435) and the radius 19.150565, and the circle with the center (19.701191, 19.203466) and the radius 19.913790. Your mission is to write a program to calculate the radii of the Malfatti circles of the given triangles. Input The input is a sequence of datasets. A dataset is a line containing six integers x 1 , y 1 , x 2 , y 2 , x 3 and y 3 in this order, separated by a space. The coordinates of the vertices of the given triangle are ( x 1 , y 1 ), ( x 2 , y 2 ) and ( x 3 , y 3 ), respectively. You can assume that the vertices form a triangle counterclockwise. You can also assume that the following two conditions hold. All of the coordinate values are greater than −1000 and less than 1000. None of the Malfatti circles of the triangle has a radius less than 0.1. The end of the input is indicated by a line containing six zeros separated by a space. Output For each input dataset, three decimal fractions r 1 , r 2 and r 3 should be printed in a line in this order separated by a space. The radii of the Malfatti circles nearest to the vertices with the coordinates ( x 1 , y 1 ), ( x 2 , y 2 ) and ( x 3 , y 3 ) should be r 1 , r 2 and r 3 , respectively. None of the output values may have an error greater than 0.0001. No extra character should appear in the output. Sample Input 20 80 -40 -20 120 -20 20 -20 120 -20 -40 80 0 0 1 0 0 1 0 0 999 1 -999 1 897 -916 847 -972 890 -925 999 999 -999 -998 -998 -999 -999 -999 999 -999 0 731 -999 -999 999 -464 -464 999 979 -436 -955 -337 157 -439 0 0 0 0 0 0 Output for the Sample Input 21.565935 24.409005 27.107493 9.942044 19.150565 19.913790 0.148847 0.207107 0.207107 0.125125 0.499750 0.499750 0.373458 0.383897 0.100456 0.706768 0.353509 0.353509 365.638023 365.638023 365.601038 378.524085 378.605339 378.605339 21.895803 22.052921 5.895714
[ { "submission_id": "aoj_1301_10852904", "code_snippet": "#include <cstdio>\n#include <cstring>\n#include <cstdlib>\n#include <algorithm>\n#include <cmath>\n#include <iostream>\nusing namespace std;\nstruct Point\n{\n\tdouble x, y;\n}Tri[10];\ndouble length(Point p)\n{\n\treturn (sqrt(p.x * p.x + p.y * p.y));\n}\ndouble cross(Point a, Point b)\n{\n\treturn a.x * b.y - a.y * b.x;\n}\nPoint dang[4];\ndouble ldang[4];\nPoint operator -(const Point &a, const Point &b)\n{\n\tPoint t;\n\tt.x = a.x - b.x;\n\tt.y = a.y - b.y;\n\treturn t;\n}\ndouble dcmp(double x)\n{\n\tif (fabs(x) < 1e-9)\n\t\treturn 0;\n\treturn x < 0 ? -1 : 1;\n}\nPoint operator +(const Point &a, const Point &v)\n{\n\tPoint t;\n\tt.x = a.x + v.x;\n\tt.y = a.y + v.y;\n\treturn t;\n}\nvoid init()\n{\n\tfor (int i = 0; i < 3; i++)\n\t\tscanf(\"%lf%lf\", &Tri[i].x, &Tri[i].y);\n\tbool flag = false;\n\tfor (int i = 0; i < 3; i++)\n\t\tif (Tri[i].x != 0 || Tri[i].y != 0)\n\t\t{\n\t\t\tflag = true;\n\t\t\tbreak;\n\t\t}\n\tif (!flag)\n\t\texit(0);\n\tPoint v1, v2;\n\tv1.x = Tri[1].x - Tri[0].x;\n\tv1.y = Tri[1].y - Tri[0].y;\n\tv2.x = Tri[2].x - Tri[0].x;\n\tv2.y = Tri[2].y - Tri[0].y;\n\tdouble d = length(v1);\n\tv1.x /= d; v1.y /= d;\n\td = length(v2);\n\tv2.x /= d; v2.y /= d;\n\tPoint e;\n\te.x = v1.x + v2.x; e.y = v1.y + v2.y;\n\tdang[0] = e;\n\tdouble L = 0.0; double R = 100000;\n\twhile (R - L > 1e-10)\n\t{\n\t\tdouble mid = (L + R) / 2.0;\n\t\tPoint v1,v2, v3;\n\t\tv1 = Tri[1] - Tri[2];\n\t\tv2 = Tri[0] - Tri[2];\n\t\tv3.x = dang[0].x * mid;\n\t\tv3.y = dang[0].y * mid;\n\t\tv3 = v3 + v2;\n\t\tif (dcmp(cross(v2, v1)) * dcmp(cross(v3, v1)) >= 0)\n\t\t\tL = mid;\n\t\telse R = mid;\n\t}\n\tldang[0] = L;\n\tv1.x = Tri[2].x - Tri[1].x;\n\tv1.y = Tri[2].y - Tri[1].y;\n\tv2.x = Tri[0].x - Tri[1].x;\n\tv2.y = Tri[0].y - Tri[1].y;\n\td = length(v1);\n\tv1.x /= d; v1.y /= d;\n\td = length(v2);\n\tv2.x /= d; v2.y /= d;\n\te.x = v1.x + v2.x; e.y = v1.y + v2.y;\n\tdang[1] = e;\n\tL = 0.0; R = 100000;\n\twhile (R - L > 1e-10)\n\t{\n\t\tdouble mid = (L + R) / 2.0;\n\t\tPoint v1,v2, v3;\n\t\tv1 = Tri[0] - Tri[2];\n\t\tv2 = Tri[1] - Tri[2];\n\t\tv3.x = dang[1].x * mid;\n\t\tv3.y = dang[1].y * mid;\n\t\tv3 = v3 + v2;\n\t\tif (dcmp(cross(v2, v1)) * dcmp(cross(v3, v1)) >= 0)\n\t\t\tL = mid;\n\t\telse R = mid;\n\t}\n\tldang[1] = L;\n\tv1.x = Tri[1].x - Tri[2].x;\n\tv1.y = Tri[1].y - Tri[2].y;\n\tv2.x = Tri[0].x - Tri[2].x;\n\tv2.y = Tri[0].y - Tri[2].y;\n\td = length(v1);\n\tv1.x /= d; v1.y /= d;\n\td = length(v2);\n\tv2.x /= d; v2.y /= d;\n\te.x = v1.x + v2.x; e.y = v1.y + v2.y;\n\tdang[2] = e;\n\tL = 0.0; R = 100000;\n\twhile (R - L > 1e-10)\n\t{\n\t\tdouble mid = (L + R) / 2.0;\n\t\tPoint v1,v2, v3;\n\t\tv1 = Tri[0] - Tri[1];\n\t\tv2 = Tri[2] - Tri[1];\n\t\tv3.x = dang[2].x * mid;\n\t\tv3.y = dang[2].y * mid;\n\t\tv3 = v3 + v2;\n\t\tif (dcmp(cross(v2, v1)) * dcmp(cross(v3, v1)) >= 0)\n\t\t\tL = mid;\n\t\telse R = mid;\n\t}\n\tldang[2] = L;\n}\nPoint operator *(const Point &a, double b)\n{\n\tPoint t = a;\n\tt.x *= b;\n\tt.y *= b;\n\treturn t;\n}\nstruct cir\n{\n\tdouble r;\n\tPoint p;\n}C[10];\nvoid solve()\n{\n\tdouble L = 0.0; double R = ldang[0];\n\twhile (R - L > 1e-10)\n\t{\n\t\tdouble mid = (L + R) / 2.0;\n\t\tC[0].p = dang[0] * mid; //xiang dui \n\t\tPoint v1 = Tri[1] - Tri[0]; \n\t\tC[0].r = fabs(C[0].p.x * v1.y - C[0].p.y * v1.x) / (length(v1));\n\t\tv1 = Tri[2] - Tri[1];\n\t\tC[0].p.x += Tri[0].x; //jue dui\n\t\tC[0].p.y += Tri[0].y;\n\t\tPoint v2 = C[0].p - Tri[1];\n\t\tdouble d1 = fabs(cross(v2,v1)) / length(v1);\n\t \tif (d1 < C[0].r)\n\t\t{\n\t\t\tR = mid;\n\t\t\tcontinue;\n\t\t}\t\n\t\t//////// \n\t\tdouble l = 0.0; double r = ldang[1];\n\t\twhile (r - l > 1e-10)\n\t\t{\n\t\t\tdouble mid = (l + r) / 2.0;\n\t\t\tC[1].p = dang[1] * mid;\n\t\t\tPoint v1 = Tri[2] - Tri[1];\n\t\t\tC[1].r = fabs(C[1].p.x * v1.y - C[1].p.y * v1.x) / (length(v1));\n\t\t\tC[1].p.x += Tri[1].x;\n\t\t\tC[1].p.y += Tri[1].y;\n\t\t\tdouble d = length(C[1].p - C[0].p);\n\t\t\tv1 = Tri[2] - Tri[0];\n\t\t\tPoint v2 = C[1].p - Tri[0];\n\t\t\tdouble d1 = fabs(cross(v1, v2))/length(v1);\n\t\t\tif (d1 < C[1].r)\n\t\t\t{\n\t\t\t\tr = mid;\n\t\t\t\tcontinue;\n\t\t\t}\n\t\t\tif (dcmp(d - C[0].r - C[1].r) == -1)\n\t\t\t\tr = mid;\n\t\t\telse\n\t\t\t\tl = mid;\n\t\t}\n\t\t//////////\n\t\tl = 0.0; r = ldang[2];\n\t\twhile (r - l > 1e-10)\n\t\t{\n\t\t\tdouble mid = (l + r) / 2.0;\n\t\t\tC[2].p = dang[2] * mid;\n\t\t\tPoint v1 = Tri[0] - Tri[2];\n\t\t\tC[2].r = fabs(C[2].p.x * v1.y - C[2].p.y * v1.x) / (length(v1));\n\t\t\tC[2].p.x += Tri[2].x;\n\t\t\tC[2].p.y += Tri[2].y;\n\t\t\tv1 = Tri[1] - Tri[0];\n\t\t\tPoint v2 = C[2].p - Tri[0];\n\t\t\tdouble d1 = fabs(cross(v1, v2))/length(v1);\n\t\t\tif (d1 < C[2].r)\n\t\t\t{\n\t\t\t\tr = mid;\n\t\t\t\tcontinue;\n\t\t\t}\n\t\t\tdouble d = length(C[2].p - C[0].p);\n\t\t\tif (dcmp(d-C[0].r - C[2].r) == -1)\n\t\t\t\tr = mid;\n\t\t\telse\n\t\t\t\tl = mid;\n\t\t}\n\t\t//////////\n\t\tdouble d = length(C[2].p - C[1].p);\n\t\tif (dcmp(d-C[2].r-C[1].r) == -1)\n\t\t\tL = mid;\n\t\telse\n\t\t\tR = mid;\n\t}\n\tprintf(\"%.6f %.6f %.6f\\n\", C[0].r ,C[1].r, C[2].r);\n}\nint main()\n{\n\twhile (1)\n\t{\n\t\tinit();\n\t\tsolve();\n\t}\n\treturn 0;\n}", "accuracy": 1, "time_ms": 140, "memory_kb": 3368, "score_of_the_acc": -0.3094, "final_rank": 2 }, { "submission_id": "aoj_1301_9797132", "code_snippet": "#include <bits/stdc++.h>\n\nusing namespace std;\n\n#define rep(i, a, b) for (int i = (int)(a); i < (int)(b); i++)\n#define rrep(i, a, b) for (int i = (int)(b)-1; i >= (int)(a); i--)\n#define ALL(v) (v).begin(), (v).end()\n#define UNIQUE(v) sort(ALL(v)), (v).erase(unique(ALL(v)), (v).end())\n#define SZ(v) (int)v.size()\n#define MIN(v) *min_element(ALL(v))\n#define MAX(v) *max_element(ALL(v))\n#define LB(v, x) int(lower_bound(ALL(v), (x)) - (v).begin())\n#define UB(v, x) int(upper_bound(ALL(v), (x)) - (v).begin())\n\nusing uint = unsigned int;\nusing ll = long long int;\nusing ull = unsigned long long;\nusing i128 = __int128_t;\nusing u128 = __uint128_t;\nconst int inf = 0x3fffffff;\nconst ll INF = 0x1fffffffffffffff;\n\ntemplate <typename T> inline bool chmax(T &a, T b) {\n if (a < b) {\n a = b;\n return 1;\n }\n return 0;\n}\ntemplate <typename T> inline bool chmin(T &a, T b) {\n if (a > b) {\n a = b;\n return 1;\n }\n return 0;\n}\ntemplate <typename T, typename U> T ceil(T x, U y) {\n assert(y != 0);\n if (y < 0)\n x = -x, y = -y;\n return (x > 0 ? (x + y - 1) / y : x / y);\n}\ntemplate <typename T, typename U> T floor(T x, U y) {\n assert(y != 0);\n if (y < 0)\n x = -x, y = -y;\n return (x > 0 ? x / y : (x - y + 1) / y);\n}\ntemplate <typename T> int popcnt(T x) {\n return __builtin_popcountll(x);\n}\ntemplate <typename T> int topbit(T x) {\n return (x == 0 ? -1 : 63 - __builtin_clzll(x));\n}\ntemplate <typename T> int lowbit(T x) {\n return (x == 0 ? -1 : __builtin_ctzll(x));\n}\n\ntemplate <class T, class U>\nostream &operator<<(ostream &os, const pair<T, U> &p) {\n os << \"P(\" << p.first << \", \" << p.second << \")\";\n return os;\n}\ntemplate <typename T> ostream &operator<<(ostream &os, const vector<T> &vec) {\n os << \"{\";\n for (int i = 0; i < vec.size(); i++) {\n os << vec[i] << (i + 1 == vec.size() ? \"\" : \", \");\n }\n os << \"}\";\n return os;\n}\ntemplate <typename T, typename U>\nostream &operator<<(ostream &os, const map<T, U> &map_var) {\n os << \"{\";\n for (auto itr = map_var.begin(); itr != map_var.end(); itr++) {\n os << \"(\" << itr->first << \", \" << itr->second << \")\";\n itr++;\n if (itr != map_var.end())\n os << \", \";\n itr--;\n }\n os << \"}\";\n return os;\n}\ntemplate <typename T> ostream &operator<<(ostream &os, const set<T> &set_var) {\n os << \"{\";\n for (auto itr = set_var.begin(); itr != set_var.end(); itr++) {\n os << *itr;\n ++itr;\n if (itr != set_var.end())\n os << \", \";\n itr--;\n }\n os << \"}\";\n return os;\n}\n#ifdef LOCAL\n#define show(...) _show(0, #__VA_ARGS__, __VA_ARGS__)\n#else\n#define show(...) true\n#endif\ntemplate <typename T> void _show(int i, T name) {\n cerr << '\\n';\n}\ntemplate <typename T1, typename T2, typename... T3>\nvoid _show(int i, const T1 &a, const T2 &b, const T3 &...c) {\n for (; a[i] != ',' && a[i] != '\\0'; i++)\n cerr << a[i];\n cerr << \":\" << b << \" \";\n _show(i + 1, a, c...);\n}\n\n/**\n * @brief template\n */\n\nusing T = long double;\nconst T eps = 1e-8;\nusing Point = complex<T>;\nusing Poly = vector<Point>;\n#define X real()\n#define Y imag()\ntemplate <typename T> inline bool eq(const T &a, const T &b) {\n return fabs(a - b) < eps;\n}\nbool cmp(const Point &a, const Point &b) {\n auto sub = [&](Point a) {\n return (a.Y < 0 ? -1 : (a.Y == 0 && a.X >= 0 ? 0 : 1));\n };\n if (sub(a) != sub(b))\n return sub(a) < sub(b);\n return a.Y * b.X < a.X * b.Y;\n}\nstruct Line {\n Point a, b, dir;\n Line() {}\n Line(Point _a, Point _b) : a(_a), b(_b), dir(b - a) {}\n Line(T A, T B, T C) {\n if (eq(A, (T).0)) {\n a = Point(0, C / B), b = Point(1 / C / B);\n } else if (eq(B, (T).0)) {\n a = Point(C / A, 0), b = Point(C / A, 1);\n } else {\n a = Point(0, C / B), b = Point(C / A, 0);\n }\n }\n};\nstruct Segment : Line {\n Segment() {}\n Segment(Point _a, Point _b) : Line(_a, _b) {}\n};\nstruct Circle {\n Point p;\n T r;\n Circle() {}\n Circle(Point _p, T _r) : p(_p), r(_r) {}\n};\n\nistream &operator>>(istream &is, Point &p) {\n T x, y;\n is >> x >> y;\n p = Point(x, y);\n return is;\n}\nostream &operator<<(ostream &os, Point &p) {\n os << fixed << setprecision(12) << p.X << ' ' << p.Y;\n return os;\n}\nPoint unit(const Point &a) {\n return a / abs(a);\n}\nT dot(const Point &a, const Point &b) {\n return a.X * b.X + a.Y * b.Y;\n}\nT cross(const Point &a, const Point &b) {\n return a.X * b.Y - a.Y * b.X;\n}\nPoint rot(const Point &a, const T &theta) {\n return Point(cos(theta) * a.X - sin(theta) * a.Y,\n sin(theta) * a.X + cos(theta) * a.Y);\n}\nPoint rot90(const Point &a) {\n return Point(-a.Y, a.X);\n}\nT arg(const Point &a, const Point &b, const Point &c) {\n double ret = acos(dot(a - b, c - b) / abs(a - b) / abs(c - b));\n if (cross(a - b, c - b) < 0)\n ret = -ret;\n return ret;\n}\n\nPoint Projection(const Line &l, const Point &p) {\n T t = dot(p - l.a, l.a - l.b) / norm(l.a - l.b);\n return l.a + (l.a - l.b) * t;\n}\nPoint Projection(const Segment &l, const Point &p) {\n T t = dot(p - l.a, l.a - l.b) / norm(l.a - l.b);\n return l.a + (l.a - l.b) * t;\n}\nPoint Reflection(const Line &l, const Point &p) {\n return p + (Projection(l, p) - p) * (T)2.;\n}\nint ccw(const Point &a, Point b, Point c) {\n b -= a;\n c -= a;\n if (cross(b, c) > eps)\n return 1; // ccw\n\n if (cross(b, c) < -eps)\n return -1; // cw\n\n if (dot(b, c) < 0)\n return 2; // c,a,b\n\n if (norm(b) < norm(c))\n return -2; // a,b,c\n\n return 0; // a,c,b\n\n}\nbool isOrthogonal(const Line &a, const Line &b) {\n return eq(dot(a.b - a.a, b.b - b.a), (T).0);\n}\nbool isParallel(const Line &a, const Line &b) {\n return eq(cross(a.b - a.a, b.b - b.a), (T).0);\n}\nbool isIntersect(const Segment &a, const Segment &b) {\n return ccw(a.a, a.b, b.a) * ccw(a.a, a.b, b.b) <= 0 and\n ccw(b.a, b.b, a.a) * ccw(b.a, b.b, a.b) <= 0;\n}\nint isIntersect(const Circle &a, const Circle &b) {\n T d = abs(a.p - b.p);\n if (d > a.r + b.r + eps)\n return 4;\n if (eq(d, a.r + b.r))\n return 3;\n if (eq(d, abs(a.r - b.r)))\n return 1;\n if (d < abs(a.r - b.r) - eps)\n return 0;\n return 2;\n}\nT Dist(const Line &a, const Point &b) {\n Point c = Projection(a, b);\n return abs(c - b);\n}\nT Dist(const Segment &a, const Point &b) {\n if (dot(a.b - a.a, b - a.a) < eps)\n return abs(b - a.a);\n if (dot(a.a - a.b, b - a.b) < eps)\n return abs(b - a.b);\n return abs(cross(a.b - a.a, b - a.a)) / abs(a.b - a.a);\n}\nT Dist(const Segment &a, const Segment &b) {\n if (isIntersect(a, b))\n return (T).0;\n T res = min({Dist(a, b.a), Dist(a, b.b), Dist(b, a.a), Dist(b, a.b)});\n return res;\n}\nPoint Intersection(const Line &a, const Line &b) {\n T d1 = cross(a.b - a.a, b.b - b.a);\n T d2 = cross(a.b - a.a, a.b - b.a);\n if (eq(d1, (T)0.) and eq(d2, (T)0.))\n return b.a;\n return b.a + (b.b - b.a) * (d2 / d1);\n}\nPoly Intersection(const Circle &a, const Line &b) {\n Poly res;\n T d = Dist(b, a.p);\n if (d > a.r + eps)\n return res;\n Point h = Projection(b, a.p);\n if (eq(d, a.r)) {\n res.push_back(h);\n return res;\n }\n Point e = unit(b.b - b.a);\n T ph = sqrt(a.r * a.r - d * d);\n res.push_back(h - e * ph);\n res.push_back(h + e * ph);\n return res;\n}\nPoly Intersection(const Circle &a, const Segment &b) {\n Line c(b.a, b.b);\n Poly sub = Intersection(a, c);\n double xmi = min(b.a.X, b.b.X), xma = max(b.a.X, b.b.X);\n double ymi = min(b.a.Y, b.b.Y), yma = max(b.a.Y, b.b.Y);\n Poly res;\n rep(i, 0, sub.size()) {\n if (xmi <= sub[i].X + eps and sub[i].X - eps <= xma and\n ymi <= sub[i].Y + eps and sub[i].Y - eps <= yma) {\n res.push_back(sub[i]);\n }\n }\n return res;\n}\nPoly Intersection(const Circle &a, const Circle &b) {\n Poly res;\n int mode = isIntersect(a, b);\n T d = abs(a.p - b.p);\n if (mode == 4 or mode == 0)\n return res;\n if (mode == 3) {\n T t = a.r / (a.r + b.r);\n res.push_back(a.p + (b.p - a.p) * t);\n return res;\n }\n if (mode == 1) {\n if (b.r < a.r - eps) {\n res.push_back(a.p + (b.p - a.p) * (a.r / d));\n } else {\n res.push_back(b.p + (a.p - b.p) * (b.r / d));\n }\n return res;\n }\n T rc = (a.r * a.r + d * d - b.r * b.r) / d / (T)2.;\n T rs = sqrt(a.r * a.r - rc * rc);\n if (a.r - abs(rc) < eps)\n rs = 0;\n Point e = unit(b.p - a.p);\n res.push_back(a.p + rc * e + rs * e * Point(0, 1));\n res.push_back(a.p + rc * e + rs * e * Point(0, -1));\n return res;\n}\nPoly HalfplaneIntersection(vector<Line> &H) {\n sort(ALL(H), [&](Line &l1, Line &l2) { return cmp(l1.dir, l2.dir); });\n auto outside = [&](Line &L, Point p) -> bool {\n return cross(L.dir, p - L.a) < -eps;\n };\n deque<Line> deq;\n int sz = 0;\n rep(i, 0, SZ(H)) {\n while (sz > 1 and\n outside(H[i], Intersection(deq[sz - 1], deq[sz - 2]))) {\n deq.pop_back();\n sz--;\n }\n while (sz > 1 and outside(H[i], Intersection(deq[0], deq[1]))) {\n deq.pop_front();\n sz--;\n }\n if (sz > 0 and fabs(cross(H[i].dir, deq[sz - 1].dir)) < eps) {\n if (dot(H[i].dir, deq[sz - 1].dir) < 0) {\n return {};\n }\n if (outside(H[i], deq[sz - 1].a)) {\n deq.pop_back();\n sz--;\n } else\n continue;\n }\n deq.push_back(H[i]);\n sz++;\n }\n\n while (sz > 2 and outside(deq[0], Intersection(deq[sz - 1], deq[sz - 2]))) {\n deq.pop_back();\n sz--;\n }\n while (sz > 2 and outside(deq[sz - 1], Intersection(deq[0], deq[1]))) {\n deq.pop_front();\n sz--;\n }\n if (sz < 3)\n return {};\n deq.push_back(deq.front());\n Poly ret;\n rep(i, 0, sz) ret.push_back(Intersection(deq[i], deq[i + 1]));\n return ret;\n}\n\nT Area(const Poly &a) {\n T res = 0;\n int n = a.size();\n rep(i, 0, n) res += cross(a[i], a[(i + 1) % n]);\n return fabs(res / (T)2.);\n}\nT Area(const Poly &a, const Circle &b) {\n int n = a.size();\n if (n < 3)\n return (T).0;\n auto rec = [&](auto self, const Circle &c, const Point &p1,\n const Point &p2) {\n Point va = c.p - p1, vb = c.p - p2;\n T f = cross(va, vb), res = (T).0;\n if (eq(f, (T).0))\n return res;\n if (max(abs(va), abs(vb)) < c.r + eps)\n return f;\n if (Dist(Segment(p1, p2), c.p) > c.r - eps)\n return c.r * c.r * arg(vb * conj(va));\n auto u = Intersection(c, Segment(p1, p2));\n Poly sub;\n sub.push_back(p1);\n for (auto &x : u)\n sub.push_back(x);\n sub.push_back(p2);\n rep(i, 0, sub.size() - 1) res += self(self, c, sub[i], sub[i + 1]);\n return res;\n };\n T res = (T).0;\n rep(i, 0, n) res += rec(rec, b, a[i], a[(i + 1) % n]);\n return fabs(res / (T)2.);\n}\nT Area(const Circle &a, const Circle &b) {\n T d = abs(a.p - b.p);\n if (d >= a.r + b.r - eps)\n return (T).0;\n if (d <= abs(a.r - b.r) + eps) {\n T r = min(a.r, b.r);\n return M_PI * r * r;\n }\n T ath = acos((a.r * a.r + d * d - b.r * b.r) / d / a.r / (T)2.);\n T res = a.r * a.r * (ath - sin(ath * 2) / (T)2.);\n T bth = acos((b.r * b.r + d * d - a.r * a.r) / d / b.r / (T)2.);\n res += b.r * b.r * (bth - sin(bth * 2) / (T)2.);\n return fabs(res);\n}\nbool isConvex(const Poly &a) {\n int n = a.size();\n int cur, pre, nxt;\n rep(i, 0, n) {\n pre = (i - 1 + n) % n;\n nxt = (i + 1) % n;\n cur = i;\n if (ccw(a[pre], a[cur], a[nxt]) == -1)\n return 0;\n }\n return 1;\n}\nint isContained(const Poly &a,\n const Point &b) { // 0:not contain,1:on edge,2:contain\n\n bool res = 0;\n int n = a.size();\n rep(i, 0, n) {\n Point p = a[i] - b, q = a[(i + 1) % n] - b;\n if (p.Y > q.Y)\n swap(p, q);\n if (p.Y < eps and eps < q.Y and cross(p, q) > eps)\n res ^= 1;\n if (eq(cross(p, q), (T).0) and dot(p, q) < eps)\n return 1;\n }\n return (res ? 2 : 0);\n}\nPoly ConvexHull(Poly &a) {\n sort(ALL(a), [](const Point &p, const Point &q) {\n return (eq(p.Y, q.Y) ? p.X < q.X : p.Y < q.Y);\n });\n a.erase(unique(ALL(a)), a.end());\n int n = a.size(), k = 0;\n Poly res(n * 2);\n for (int i = 0; i < n; res[k++] = a[i++]) {\n while (k >= 2 and\n cross(res[k - 1] - res[k - 2], a[i] - res[k - 1]) < eps)\n k--;\n }\n for (int i = n - 2, t = k + 1; i >= 0; res[k++] = a[i--]) {\n while (k >= t and\n cross(res[k - 1] - res[k - 2], a[i] - res[k - 1]) < eps)\n k--;\n }\n res.resize(k - 1);\n return res;\n}\nT Diam(const Poly &a) {\n int n = a.size();\n int x = 0, y = 0;\n rep(i, 1, n) {\n if (a[i].Y > a[x].Y)\n x = i;\n if (a[i].Y < a[y].Y)\n y = i;\n }\n T res = abs(a[x] - a[y]);\n int i = x, j = y;\n do {\n if (cross(a[(i + 1) % n] - a[i], a[(j + 1) % n] - a[j]) < 0)\n i = (i + 1) % n;\n else\n j = (j + 1) % n;\n chmax(res, abs(a[i] - a[j]));\n } while (i != x or j != y);\n return res;\n}\nPoly Cut(const Poly &a, const Line &l) {\n int n = a.size();\n Poly res;\n rep(i, 0, n) {\n Point p = a[i], q = a[(i + 1) % n];\n if (ccw(l.a, l.b, p) != -1)\n res.push_back(p);\n if (ccw(l.a, l.b, p) * ccw(l.a, l.b, q) < 0)\n res.push_back(Intersection(Line(p, q), l));\n }\n return res;\n}\n\nT Closest(Poly &a) {\n int n = a.size();\n if (n <= 1)\n return 0;\n sort(ALL(a), [&](Point a, Point b) {\n return (eq(a.X, b.X) ? a.Y < b.Y : a.X < b.X);\n });\n Poly buf(n);\n auto rec = [&](auto self, int lb, int rb) -> T {\n if (rb - lb <= 1)\n return (T)INF;\n int mid = (lb + rb) >> 1;\n auto x = a[mid].X;\n T res = min(self(self, lb, mid), self(self, mid, rb));\n inplace_merge(a.begin() + lb, a.begin() + mid, a.begin() + rb,\n [&](auto p, auto q) { return p.Y < q.Y; });\n int ptr = 0;\n rep(i, lb, rb) {\n if (abs(a[i].X - x) >= res)\n continue;\n rep(j, 0, ptr) {\n auto sub = a[i] - buf[ptr - 1 - j];\n if (sub.Y >= res)\n break;\n chmin(res, abs(sub));\n }\n buf[ptr++] = a[i];\n }\n return res;\n };\n return rec(rec, 0, n);\n}\n\nCircle Incircle(const Point &a, const Point &b, const Point &c) {\n T A = abs(b - c), B = abs(c - a), C = abs(a - b);\n Point p(A * a.X + B * b.X + C * c.X, A * a.Y + B * b.Y + C * c.Y);\n p /= (A + B + C);\n T r = Dist(Line(a, b), p);\n return Circle(p, r);\n}\nCircle Circumcircle(const Point &a, const Point &b, const Point &c) {\n Line l1((a + b) / (T)2., (a + b) / (T)2. + (b - a) * Point(0, 1));\n Line l2((b + c) / (T)2., (b + c) / (T)2. + (c - b) * Point(0, 1));\n Point p = Intersection(l1, l2);\n return Circle(p, abs(p - a));\n}\nPoly tangent(const Point &a, const Circle &b) {\n return Intersection(b, Circle(a, sqrt(norm(b.p - a) - b.r * b.r)));\n}\nvector<Line> tangent(const Circle &a, const Circle &b) {\n vector<Line> res;\n T d = abs(a.p - b.p);\n if (eq(d, (T)0.))\n return res;\n Point u = unit(b.p - a.p);\n Point v = u * Point(0, 1);\n for (int t : {-1, 1}) {\n T h = (a.r + b.r * t) / d;\n if (eq(h * h, (T)1.)) {\n res.push_back(Line(a.p + (h > 0 ? u : -u) * a.r,\n a.p + (h > 0 ? u : -u) * a.r + v));\n } else if (1 > h * h) {\n Point U = u * h, V = v * sqrt(1 - h * h);\n res.push_back(Line(a.p + (U + V) * a.r, b.p - (U + V) * (b.r * t)));\n res.push_back(Line(a.p + (U - V) * a.r, b.p - (U - V) * (b.r * t)));\n }\n }\n return res;\n}\n\n/**\n * @brief Geometry\n */\n\nint main() {\nwhile(1) {\n long double x1, y1, x2, y2, x3, y3;\n cin >> x1 >> y1 >> x2 >> y2 >> x3 >> y3;\n if (x1 == 0.0L && y1 == 0.0L && x2 == 0.0L && y2 == 0.0L && x3 == 0.0L && y3 == 0.0L) return 0;\n Point A(x1,y1), B(x2,y2), C(x3,y3);\n Line a(B,C), b(C,A), c(A,B);\n Point I = (abs(B-C)*A+abs(C-A)*B+abs(A-B)*C)/(abs(B-C)+abs(C-A)+abs(A-B));\n long double IR = Dist(a,I);\n auto Judge = [&](long double D) -> pair<bool,pair<long double,long double>> {\n long double OK = 0.0L, NG = 1.0L;\n Point CA = A + (I-A) * D;\n long double RA = IR * D;\n rep(_,0,60) {\n long double MID = (OK + NG) / 2.0L;\n Point CB = B + (I-B) * MID;\n long double RB = IR * MID;\n if (abs(CA-CB) >= RA + RB) OK = MID;\n else NG = MID;\n }\n long double DB = OK;\n OK = 0.0L, NG = 1.0L;\n rep(_,0,60) {\n long double MID = (OK + NG) / 2.0L;\n Point CC = C + (I-C) * MID;\n long double RC = IR * MID;\n if (abs(CA-CC) >= RA + RC) OK = MID;\n else NG = MID;\n }\n long double DC = OK;\n {\n Point CB = B + (I-B) * DB, CC = C + (I-C) * DC;\n if (abs(CB-CC) >= IR * (DB+DC)) return {true,{DB,DC}};\n else return {false,{DB,DC}};\n }\n };\n {\n long double NG = 0.0L, OK = 1.0L;\n rep(_,0,60) {\n long double MID = (NG + OK) / 2.0L;\n if (Judge(MID).first) OK = MID;\n else NG = MID;\n }\n auto ANS = Judge(OK).second;\n printf(\"%.12Lf %.12Lf %.12Lf\\n\", IR*OK, IR*ANS.first, IR*ANS.second);\n }\n}\n}", "accuracy": 1, "time_ms": 630, "memory_kb": 3580, "score_of_the_acc": -0.6539, "final_rank": 13 }, { "submission_id": "aoj_1301_9561853", "code_snippet": "#include <algorithm>\n#include <cmath>\n#include <cstdlib>\n#include <iomanip>\n#include <iostream>\n#include <list>\n#include <map>\n#include <numeric>\n#include <queue>\n#include <set>\n#include <stack>\n#include <string>\n#include <unordered_set>\n#include <vector>\n\nusing namespace std;\nusing ll = long long;\n\nclass Vec2 {\n public:\n\tdouble x, y;\n\n\tVec2()\n\t\t: x(0), y(0) {};\n\n\tVec2(const double& x, const double& y)\n\t\t: x(x), y(y) {};\n\n\tdouble Mag() const {\n\t\treturn sqrt(this->x * this->x + this->y * this->y);\n\t}\n\n\tVec2 operator+(const Vec2& t) const {\n\t\treturn Vec2{this->x + t.x, this->y + t.y};\n\t}\n\n\tVec2 operator-(const Vec2& t) const {\n\t\treturn Vec2{this->x - t.x, this->y - t.y};\n\t}\n\n\tVec2 operator/(const double& t) const {\n\t\treturn Vec2{this->x / t, this->y / t};\n\t}\n\n\tfriend Vec2 operator*(const double& t, const Vec2 vec2) {\n\t\treturn Vec2{t * vec2.x, t * vec2.y};\n\t}\n\n\tbool operator==(const Vec2 t) const {\n\t\treturn (this->x == t.x and this->y == t.y);\n\t}\n};\n\nclass Circle {\n public:\n\tVec2 center;\n\tdouble radius;\n\n\tCircle()\n\t\t: center(Vec2{0.0, 0.0}), radius(0.0) {};\n\n\tCircle(const Vec2& center, const double& radius)\n\t\t: center(center), radius(radius) {};\n};\n\nCircle Incircle(const array<Vec2, 3>& vertices) {\n\tCircle incircle;\n\n\tdouble m12 = (vertices[1] - vertices[2]).Mag();\n\tdouble m20 = (vertices[2] - vertices[0]).Mag();\n\tdouble m01 = (vertices[0] - vertices[1]).Mag();\n\tincircle.center = (m12 * vertices[0] + m20 * vertices[1] + m01 * vertices[2]) / (m12 + m20 + m01);\n\tdouble s = (m12 + m20 + m01) / 2.0;\n\tincircle.radius = sqrt(s * (s - m12) * (s - m20) * (s - m01)) / s;\n\n\treturn incircle;\n}\n\nCircle AnotherCircle(const Vec2& vertice, const Circle& incircle, const Vec2& v_to_i, const Circle& circle0) {\n\tCircle circle;\n\n\tdouble l{0.0}, r{1.0};\n\tconst double threshold{0.0000001};\n\twhile (true) {\n\t\tdouble m = (l + r) / 2.0;\n\n\t\tcircle = Circle{vertice + m * v_to_i, m * incircle.radius};\n\n\t\tif ((circle0.radius + circle.radius) > (circle0.center - circle.center).Mag()) r = m;\n\t\telse l = m;\n\n\t\tif ((r - l) * incircle.radius < threshold) break;\n\t}\n\n\treturn circle;\n}\n\nbool solve() {\n\tarray<Vec2, 3> vertices;\n\tfor (Vec2& vertice : vertices) cin >> vertice.x >> vertice.y;\n\tif (vertices[0] == vertices[1]) return false;\n\n\tCircle incircle = Incircle(vertices);\n\tarray<Vec2, 3> v_to_i{incircle.center - vertices[0], incircle.center - vertices[1], incircle.center - vertices[2]};\n\tarray<Circle, 3> circles;\n\n\tdouble l{0.0}, r{1.0};\n\tconst double threshold{0.0000001};\n\twhile (true) {\n\t\tdouble m = (l + r) / 2.0;\n\n\t\tcircles[0] = Circle{vertices[0] + m * v_to_i[0], m * incircle.radius};\n\t\tcircles[1] = AnotherCircle(vertices[1], incircle, v_to_i[1], circles[0]);\n\t\tcircles[2] = AnotherCircle(vertices[2], incircle, v_to_i[2], circles[0]);\n\n\t\tif ((circles[1].radius + circles[2].radius) > (circles[1].center - circles[2].center).Mag()) l = m;\n\t\telse r = m;\n\n\t\tif ((r - l) * incircle.radius < threshold) break;\n\t}\n\n\tcout << fixed << setprecision(6);\n\tcout << circles[0].radius << \" \" << circles[1].radius << \" \" << circles[2].radius << endl;\n\n\treturn true;\n}\n\nint main() {\n\twhile (solve());\n\treturn 0;\n}", "accuracy": 1, "time_ms": 60, "memory_kb": 3516, "score_of_the_acc": -0.4849, "final_rank": 9 }, { "submission_id": "aoj_1301_9561849", "code_snippet": "#include <algorithm>\n#include <cmath>\n#include <cstdlib>\n#include <iomanip>\n#include <iostream>\n#include <list>\n#include <map>\n#include <numeric>\n#include <queue>\n#include <set>\n#include <stack>\n#include <string>\n#include <unordered_set>\n#include <vector>\n\nusing namespace std;\nusing ll = long long;\n\nclass Vec2 {\n public:\n\tdouble x, y;\n\n\tVec2()\n\t\t: x(0), y(0) {};\n\n\tVec2(const double& x, const double& y)\n\t\t: x(x), y(y) {};\n\n\tdouble Mag() const {\n\t\treturn sqrt(this->x * this->x + this->y * this->y);\n\t}\n\n\tVec2 operator+(const Vec2& t) const {\n\t\treturn Vec2{this->x + t.x, this->y + t.y};\n\t}\n\n\tVec2 operator-(const Vec2& t) const {\n\t\treturn Vec2{this->x - t.x, this->y - t.y};\n\t}\n\n\tVec2 operator/(const double& t) const {\n\t\treturn Vec2{this->x / t, this->y / t};\n\t}\n\n\tfriend Vec2 operator*(const double& t, const Vec2 vec2) {\n\t\treturn Vec2{t * vec2.x, t * vec2.y};\n\t}\n\n\tbool operator==(const Vec2 t) const {\n\t\treturn (this->x == t.x and this->y == t.y);\n\t}\n};\n\nclass Circle {\n public:\n\tVec2 center;\n\tdouble radius;\n\n\tCircle()\n\t\t: center(Vec2{0.0, 0.0}), radius(0.0) {};\n\n\tCircle(const Vec2& center, const double& radius)\n\t\t: center(center), radius(radius) {};\n};\n\nCircle Incircle(const array<Vec2, 3>& vertices) {\n\tCircle incircle;\n\n\tdouble m12 = (vertices[1] - vertices[2]).Mag();\n\tdouble m20 = (vertices[2] - vertices[0]).Mag();\n\tdouble m01 = (vertices[0] - vertices[1]).Mag();\n\tincircle.center = (m12 * vertices[0] + m20 * vertices[1] + m01 * vertices[2]) / (m12 + m20 + m01);\n\tdouble s = (m12 + m20 + m01) / 2.0;\n\tincircle.radius = sqrt(s * (s - m12) * (s - m20) * (s - m01)) / s;\n\n\treturn incircle;\n}\n\nCircle AnotherCircle(const Vec2& vertice, const Circle& incircle, const Vec2& v_to_i, const Circle& circle0) {\n\tCircle circle;\n\n\tdouble l{0.0}, r{1.0};\n\tconst double threshold{0.0000001};\n\twhile (true) {\n\t\tdouble m = (l + r) / 2.0;\n\n\t\tcircle = Circle{vertice + m * v_to_i, m * incircle.radius};\n\n\t\tif ((circle0.radius + circle.radius) > (circle0.center - circle.center).Mag()) r = m;\n\t\telse l = m;\n\n\t\tif ((r - l) * incircle.radius < threshold) break;\n\t}\n\n\treturn circle;\n}\n\nbool solve() {\n\tarray<Vec2, 3> vertices;\n\tfor (Vec2& vertice : vertices) cin >> vertice.x >> vertice.y;\n\tif (vertices[0] == vertices[1]) return false;\n\n\tCircle incircle = Incircle(vertices);\n\tarray<Vec2, 3> v_to_i{incircle.center - vertices[0], incircle.center - vertices[1], incircle.center - vertices[2]};\n\tarray<Circle, 3> circles;\n\n\tdouble l{0.0}, r{1.0};\n\tconst double threshold{0.0000001};\n\twhile (true) {\n\t\tdouble m = (l + r) / 2.0;\n\n\t\tcircles[0] = Circle{vertices[0] + m * v_to_i[0], m * incircle.radius};\n\t\tcircles[1] = AnotherCircle(vertices[1], incircle, v_to_i[1], circles[0]);\n\t\tcircles[2] = AnotherCircle(vertices[2], incircle, v_to_i[2], circles[0]);\n\n\t\tif ((circles[1].radius + circles[2].radius) > (circles[1].center - circles[2].center).Mag()) l = m;\n\t\telse r = m;\n\t\tif ((r - l) * incircle.radius < threshold) break;\n\t}\n\n\tcout << fixed << setprecision(6);\n\tcout << circles[0].radius << \" \" << circles[1].radius << \" \" << circles[2].radius << endl;\n\n\treturn true;\n}\n\nint main() {\n\twhile (solve());\n\treturn 0;\n}", "accuracy": 1, "time_ms": 60, "memory_kb": 3392, "score_of_the_acc": -0.3275, "final_rank": 3 }, { "submission_id": "aoj_1301_9561848", "code_snippet": "#include <algorithm>\n#include <cmath>\n#include <cstdlib>\n#include <iomanip>\n#include <iostream>\n#include <list>\n#include <map>\n#include <numeric>\n#include <queue>\n#include <set>\n#include <stack>\n#include <string>\n#include <unordered_set>\n#include <vector>\n\nusing namespace std;\nusing ll = long long;\n\nclass Vec2 {\n public:\n\tdouble x, y;\n\n\tVec2()\n\t\t: x(0), y(0) {};\n\n\tVec2(const double& x, const double& y)\n\t\t: x(x), y(y) {};\n\n\tdouble Mag() const {\n\t\treturn sqrt(this->x * this->x + this->y * this->y);\n\t}\n\n\tVec2 operator+(const Vec2& t) const {\n\t\treturn Vec2{this->x + t.x, this->y + t.y};\n\t}\n\n\tVec2 operator-(const Vec2& t) const {\n\t\treturn Vec2{this->x - t.x, this->y - t.y};\n\t}\n\n\tVec2 operator/(const double& t) const {\n\t\treturn Vec2{this->x / t, this->y / t};\n\t}\n\n\tfriend Vec2 operator*(const double& t, const Vec2 vec2) {\n\t\treturn Vec2{t * vec2.x, t * vec2.y};\n\t}\n\n\tbool operator==(const Vec2 t) const {\n\t\treturn (this->x == t.x and this->y == t.y);\n\t}\n};\n\nclass Circle {\n public:\n\tVec2 center;\n\tdouble radius;\n\n\tCircle()\n\t\t: center(Vec2{0.0, 0.0}), radius(0.0) {};\n\n\tCircle(const Vec2& center, const double& radius)\n\t\t: center(center), radius(radius) {};\n};\n\nCircle Incircle(const array<Vec2, 3>& vertices) {\n\tCircle incircle;\n\n\tdouble m12 = (vertices[1] - vertices[2]).Mag();\n\tdouble m20 = (vertices[2] - vertices[0]).Mag();\n\tdouble m01 = (vertices[0] - vertices[1]).Mag();\n\tincircle.center = (m12 * vertices[0] + m20 * vertices[1] + m01 * vertices[2]) / (m12 + m20 + m01);\n\tdouble s = (m12 + m20 + m01) / 2.0;\n\tincircle.radius = sqrt(s * (s - m12) * (s - m20) * (s - m01)) / s;\n\n\treturn incircle;\n}\n\nCircle AnotherCircle(const Vec2& vertice, const Circle& incircle, const Vec2& v_to_i, const Circle& circle0) {\n\tCircle circle;\n\n\tdouble l{0.0}, r{1.0};\n\tconst double threshold{0.0000001};\n\twhile (true) {\n\t\tdouble m = (l + r) / 2.0;\n\t\tcircle = Circle{vertice + m * v_to_i, m * incircle.radius};\n\n\t\tdouble d = (circle0.radius + circle.radius) - (circle0.center - circle.center).Mag();\n\t\t// printf(\"l %10.6f m %10.6f r %10.6f d %+10.6f\\n\", l, m, r, d);\n\t\tif ((r - l) * incircle.radius < threshold) break;\n\t\telse if (d > threshold) r = m;\n\t\telse if (d < -threshold) l = m;\n\t\telse break;\n\t}\n\n\treturn circle;\n}\n\nbool solve() {\n\tarray<Vec2, 3> vertices;\n\tfor (Vec2& vertice : vertices) cin >> vertice.x >> vertice.y;\n\tif (vertices[0] == vertices[1]) return false;\n\n\tCircle incircle = Incircle(vertices);\n\tarray<Vec2, 3> v_to_i{incircle.center - vertices[0], incircle.center - vertices[1], incircle.center - vertices[2]};\n\tarray<Circle, 3> circles;\n\n\tdouble l{0.0}, r{1.0};\n\tconst double threshold{0.0000001};\n\twhile (true) {\n\t\tdouble m = (l + r) / 2.0;\n\t\tcircles[0] = Circle{vertices[0] + m * v_to_i[0], m * incircle.radius};\n\t\tcircles[1] = AnotherCircle(vertices[1], incircle, v_to_i[1], circles[0]);\n\t\tcircles[2] = AnotherCircle(vertices[2], incircle, v_to_i[2], circles[0]);\n\n\t\tdouble d = (circles[1].radius + circles[2].radius) - (circles[1].center - circles[2].center).Mag();\n\t\t// printf(\"l %10.6f m %10.6f r %10.6f d %+10.6f\\n\", l, m, r, d);\n\t\tif ((r - l) * incircle.radius < threshold) break;\n\t\telse if (d > threshold) l = m;\n\t\telse if (d < -threshold) r = m;\n\t\telse break;\n\t}\n\n\tcout << fixed << setprecision(6);\n\tcout << circles[0].radius << \" \" << circles[1].radius << \" \" << circles[2].radius << endl;\n\n\treturn true;\n}\n\nint main() {\n\twhile (solve());\n\treturn 0;\n}", "accuracy": 1, "time_ms": 40, "memory_kb": 3464, "score_of_the_acc": -0.4158, "final_rank": 7 }, { "submission_id": "aoj_1301_8533119", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nusing ld = long double;\n\nconst ld EPS = 1e-10;\nconst ld Pi = acos(-1);\n\nusing Point = complex<ld>;\n\n//struct{\n// ld x, y;\n// Point() = default;\n// Point(ld a, ld b) : x(a), y(b) {}\n//};\n\n//iostream& operator>>(iostream& is, Point& P){\n// is >> P.x >> P.y;\n// return is;\n//}\n\nstruct Circle{\n ld r;\n Point c;\n Circle() = default;\n Circle(ld a, Point b) : r(a), c(b) {}\n};\n\nld cross(Point a, Point b){\n return real(a) * imag(b) - real(b) * imag(a);\n}\n\nPoint rotate(Point o, Point p, ld theta){\n return (p - o) * Point(cos(theta), sin(theta)) + o;\n}\n\nld findArg(Point a, Point o, Point b){\n return arg((b - o) / (a - o));\n}\n\nbool lineOver(Point p, Point a, Point b){\n return cross(a - p, b - p) < -EPS;\n}\n\nPoint input(){\n ld x, y; cin >> x >> y;\n return Point(x, y);\n}\n\nint main(){\n cin.tie(nullptr); ios_base::sync_with_stdio(false);\n cout << fixed << setprecision(6);\n\n auto solve = [&]() -> bool {\n Point A = input(), B = input(), C = input();\n if(norm(A) < EPS && norm(B) < EPS && norm(C) < EPS) return false;\n\n ld argBAC = findArg(B, A, C);\n ld argCBA = findArg(C, B, A);\n ld argACB = findArg(A, C, B);\n Point Asct = 0;\n {\n Point V = C - B;\n V /= abs(V);\n Asct = V * abs(C - B) * abs(B - A) / (abs(B - A) + abs(C - A)) + B;\n }\n Point Bsct = 0;\n {\n Point V = A - C;\n V /= abs(V);\n Bsct = V * abs(A - C) * abs(C - B) / (abs(C - B) + abs(A - B)) + C;\n }\n Point Va = rotate(0, B - A, argBAC / 2);\n Point Vb = rotate(0, C - B, argCBA / 2);\n Point Vc = rotate(0, A - C, argACB / 2);\n Va /= abs(Va);\n Vb /= abs(Vb);\n Vc /= abs(Vc);\n assert(abs(norm(Va)) < 1 + EPS);\n assert(abs(norm(Vb)) < 1 + EPS);\n assert(abs(norm(Vc)) < 1 + EPS);\n // cout << argBAC << \" \" << argCBA << \" \" << argACB << '\\n';\n // cout << Va << \" \" << Vb << \" \" << Vc << endl;\n //cout << Bsct << '\\n';\n ld Lta = 0, Rta = 1000000000;\n Circle P, Q, R;\n while(abs(Rta - Lta) > EPS){\n ld ta = (Rta + Lta) / 2;\n P.c = Va * ta + A;\n P.r = ta * sin(argBAC / 2);\n //cout << P.c << \" \" << P.r << '\\n';\n if(lineOver(P.c, B, Bsct)){\n Rta = ta;\n continue;\n }\n\n // B\n {\n ld Ltb = 0, Rtb = 10000000000;\n while(abs(Rtb - Ltb) > EPS){\n ld tb = (Rtb + Ltb) / 2;\n Q.c = Vb * tb + B;\n Q.r = tb * sin(argCBA / 2);\n if(lineOver(Q.c, Asct, A)){\n Rtb = tb;\n continue;\n }\n if(Q.r + P.r > abs(Q.c - P.c)) Rtb = tb;\n else Ltb = tb;\n }\n }\n\n // C\n {\n ld Ltc = 0, Rtc = 100000000;\n while(abs(Rtc - Ltc) > EPS){\n ld tc = (Rtc + Ltc) / 2;\n R.c = Vc * tc + C;\n R.r = tc * sin(argACB / 2);\n if(lineOver(R.c, A, Asct)){\n Rtc = tc;\n continue;\n }\n if(R.r + P.r > abs(R.c - P.c)) Rtc = tc;\n else Ltc = tc;\n }\n }\n\n //cout << P.r << \" \" << Q.r << \" \" << R.r << '\\n';\n //cout << P.c << \" \" << Q.c << \" \" << R.c << '\\n';\n if(Q.r + R.r > abs(Q.c - R.c)) Lta = ta;\n else Rta = ta;\n }\n\n cout << P.r << \" \" << Q.r << \" \" << R.r << '\\n';\n return true;\n };\n while(solve());\n}", "accuracy": 1, "time_ms": 410, "memory_kb": 3928, "score_of_the_acc": -1.0616, "final_rank": 15 }, { "submission_id": "aoj_1301_6670737", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\n#define rep(i, s, t) for (int i = (int)(s); i < (int)(t); ++i)\n#define revrep(i, t, s) for (int i = (int)(t)-1; i >= (int)(s); --i)\n#define all(x) begin(x), end(x)\ntemplate <typename T> bool chmax(T& a, const T& b) { return a < b ? (a = b, 1) : 0; }\ntemplate <typename T> bool chmin(T& a, const T& b) { return a > b ? (a = b, 1) : 0; }\n\nusing T = double;\nusing Vec = std::complex<T>;\n\nconst T PI = std::acos(-1);\n\nconstexpr T eps = 1e-12;\ninline bool eq(T a, T b) { return std::abs(a - b) <= eps; }\ninline bool eq(Vec a, Vec b) { return std::abs(a - b) <= eps; }\ninline bool lt(T a, T b) { return a < b - eps; }\ninline bool leq(T a, T b) { return a <= b + eps; }\n\nstd::istream& operator>>(std::istream& is, Vec& p) {\n T x, y;\n is >> x >> y;\n p = {x, y};\n return is;\n}\n\nstruct Line {\n Vec p1, p2;\n Line() = default;\n Line(const Vec& p1, const Vec& p2) : p1(p1), p2(p2) {}\n Vec dir() const { return p2 - p1; }\n};\n\nstruct Segment {\n Vec p1, p2;\n Segment() = default;\n Segment(const Vec& p1, const Vec& p2) : p1(p1), p2(p2) {}\n Vec dir() const { return p2 - p1; }\n};\n\nstruct Circle {\n Vec c;\n T r;\n Circle() = default;\n Circle(const Vec& c, T r) : c(c), r(r) {}\n};\n\nusing Polygon = std::vector<Vec>;\n\nT dot(const Vec& a, const Vec& b) {\n return (std::conj(a) * b).real();\n}\n\nT cross(const Vec& a, const Vec& b) {\n return (std::conj(a) * b).imag();\n}\n\nVec rot(const Vec& a, T ang) {\n return a * Vec(std::cos(ang), std::sin(ang));\n}\n\nVec perp(const Vec& a) {\n return Vec(-a.imag(), a.real());\n}\n\nVec projection(const Line& l, const Vec& p) {\n return l.p1 + dot(p - l.p1, l.dir()) * l.dir() / std::norm(l.dir());\n}\n\nVec reflection(const Line& l, const Vec& p) {\n return T(2) * projection(l, p) - p;\n}\n\n// 0: collinear\n// 1: counter-clockwise\n// 2: clockwise\nint ccw(const Vec& a, const Vec& b, const Vec& c) {\n if (eq(cross(b - a, c - a), 0)) return 0;\n if (lt(cross(b - a, c - a), 0)) return -1;\n return 1;\n}\n\nvoid sort_by_arg(std::vector<Vec>& pts) {\n std::sort(pts.begin(), pts.end(), [&](auto& p, auto& q) {\n if ((p.imag() < 0) != (q.imag() < 0)) return (p.imag() < 0);\n if (cross(p, q) == 0) {\n if (p == Vec(0, 0)) return !(q.imag() < 0 || (q.imag() == 0 && q.real() > 0));\n if (q == Vec(0, 0)) return (p.imag() < 0 || (p.imag() == 0 && p.real() > 0));\n return (p.real() > q.real());\n }\n return (cross(p, q) > 0);\n });\n}\n\nT dist(const Line& l, const Vec& p) {\n return std::abs(cross(p - l.p1, l.dir())) / std::abs(l.dir());\n}\n\n// 0: inside\n// 1: inscribe\n// 2: intersect\n// 3: circumscribe\n// 4: outside\nint intersect(const Circle& c1, const Circle& c2) {\n T d = std::abs(c1.c - c2.c);\n if (lt(d, std::abs(c2.r - c1.r))) return 0;\n if (eq(d, std::abs(c2.r - c1.r))) return 1;\n if (eq(c1.r + c2.r, d)) return 3;\n if (lt(c1.r + c2.r, d)) return 4;\n return 2;\n}\n\nVec incenter(const Vec& A, const Vec& B, const Vec& C) {\n assert(ccw(A, B, C) != 0);\n T a = std::abs(B - C);\n T b = std::abs(C - A);\n T c = std::abs(A - B);\n return (a*A + b*B + c*C) / (a + b + c);\n}\n\nint main() {\n ios_base::sync_with_stdio(false);\n cin.tie(nullptr);\n cout << fixed << setprecision(15);\n\n while (true) {\n Vec a, b, c;\n cin >> a >> b >> c;\n if (eq(abs(a)+abs(b)+abs(c), 0)) break;\n Line ab(a, b), bc(b, c), ca(c, a);\n auto i = incenter(a, b, c);\n\n auto calc = [&](Vec& b, Vec& i, Line& bc, Circle& c1) {\n double lb = 0, ub = 1;\n rep(_,0,100) {\n double m = (lb + ub) / 2;\n auto x2 = b + (i - b) * m;\n double r2 = dist(bc, x2);\n\n Circle c2(x2, r2);\n\n if (intersect(c2, c1) == 4) lb = m; // outside\n else ub = m;\n }\n auto x2 = b + (i - b) * ub;\n double r2 = dist(bc, x2);\n return Circle(x2, r2);\n };\n\n double lb = 0, ub = 1;\n rep(_,0,100) {\n double m = (lb + ub) / 2;\n auto x1 = a + (i - a) * m;\n double r1 = dist(ab, x1);\n Circle c1(x1, r1);\n\n auto c2 = calc(b, i, bc, c1);\n auto c3 = calc(c, i, bc, c1);\n if (intersect(c2, c3) == 4) ub = m;\n else lb = m;\n }\n\n auto x1 = a + (i - a) * ub;\n double r1 = dist(ab, x1);\n Circle c1(x1, r1);\n\n auto c2 = calc(b, i, bc, c1);\n auto c3 = calc(c, i, bc, c1);\n\n cout << c1.r << \" \" << c2.r << \" \" << c3.r << endl;\n }\n}", "accuracy": 1, "time_ms": 1250, "memory_kb": 3444, "score_of_the_acc": -0.5768, "final_rank": 12 }, { "submission_id": "aoj_1301_6371613", "code_snippet": "#line 1 \"c.cpp\"\n/*\tauthor: Kite_kuma\n\tcreated: 2022.03.03 20:01:20 */\n\n// #ifdef LOCAL\n// #define _GLIBCXX_DEBUG\n// #endif\n#include <bits/stdc++.h>\nusing namespace std;\n\n#pragma region macros\n\n#define foa(s, v) for(auto &s : v)\n#define all(v) (v).begin(), (v).end()\n#define rall(v) (v).rbegin(), (v).rend()\n#define popcnt(n) __builtin_popcountll((long long)n)\n\n#define REPname(a, b, c, d, e, ...) e\n#define rep(...) REPname(__VA_ARGS__, REP3, REP2, REP1, REP0)(__VA_ARGS__)\n#define REP0(x) for(int Counter_in_rep_macro = 0; Counter_in_rep_macro < (x); ++Counter_in_rep_macro)\n#define REP1(i, x) for(int i = 0; i < (x); ++i)\n#define REP2(i, l, r) for(int i = (l); i < (r); ++i)\n#define REP3(i, l, r, c) for(int i = (l); i < (r); i += (c))\n\n#define DREPname(a, b, c, d, e, ...) e\n#define drep(...) DREPname(__VA_ARGS__, DREP3, DREP2, DREP1)(__VA_ARGS__)\n#define DREP1(i, x) for(int i = (x)-1; i >= 0; --i)\n#define DREP2(i, l, r) for(int i = (r)-1; i >= (l); --i)\n#define DREP3(i, l, r, c) for(int i = (r)-1; i >= (l); i -= (c))\n\n#pragma endregion\n\n#pragma region aliases\n\nusing ll = long long;\nusing ld = long double;\nusing ull = unsigned long long;\nusing vi = std::vector<int>;\nusing vvi = std::vector<std::vector<int>>;\nusing vvvi = std::vector<std::vector<std::vector<int>>>;\nusing vll = std::vector<ll>;\nusing vvll = std::vector<vll>;\nusing vvvll = std::vector<vvll>;\nusing pii = std::pair<int, int>;\nusing pll = std::pair<long long, long long>;\ntemplate <class T = ll>\nusing V = std::vector<T>;\ntemplate <class T = ll>\nusing VV = V<V<T>>;\ntemplate <class T = ll>\nusing VVV = V<V<V<T>>>;\ntemplate <class T = ll>\nusing pqup = std::priority_queue<T, std::vector<T>, std::greater<T>>;\ntemplate <class T = ll>\nusing pqdn = std::priority_queue<T>;\n\n#pragma endregion\n\n#pragma region constants\n\nconst int inf = 1e9;\nconst long long INF = 1e18;\nconst long double pi = acos(-1);\nconst char dl = '\\n';\nconst char sp = ' ';\nint dx[8] = {1, 0, -1, 0, 1, -1, -1, 1};\nint dy[8] = {0, 1, 0, -1, 1, 1, -1, -1};\n\nconst int mod_1000000007 = 1000000007;\nconst int mod_998244353 = 998244353;\n\n#pragma endregion\n\n#pragma region basic_operation\n\ntemplate <class T0, class T1, class T2>\ninline bool in_range(T0 x, T1 lef, T2 rig) {\n\treturn ((lef <= x) && (x < rig));\n}\n\ntemplate <class T>\ninline bool chmin(T &a, T b) {\n\tif(a > b) {\n\t\ta = b;\n\t\treturn true;\n\t}\n\treturn false;\n}\ntemplate <class T>\ninline bool chmax(T &a, T b) {\n\tif(a < b) {\n\t\ta = b;\n\t\treturn true;\n\t}\n\treturn false;\n}\n\nvoid Yes(bool f = 1) { std::cout << (f ? \"Yes\" : \"No\") << '\\n'; }\nvoid No() { std::cout << \"No\\n\"; }\nvoid YES(bool f = 1) { std::cout << (f ? \"YES\" : \"NO\") << '\\n'; }\nvoid NO() { std::cout << \"NO\\n\"; }\n\ntemplate <class T>\nvoid drop(T answer) {\n\tstd::cout << answer << '\\n';\n\texit(0);\n}\n\nvoid err(bool flag = true) {\n\tif(!flag) return;\n\tstd::cout << -1 << '\\n';\n\texit(0);\n}\n\ntemplate <class T>\nvoid vout(std::vector<T> const &v, bool vertically = 0) {\n\tif(vertically) {\n\t\tfor(auto const &a : v) {\n\t\t\tstd::cout << a << '\\n';\n\t\t}\n\t\treturn;\n\t}\n\tfor(auto it = v.begin(); it != v.end(); it++) {\n\t\tstd::cout << (*it);\n\t\tif(it != v.end() - 1) {\n\t\t\tstd::cout << ' ';\n\t\t}\n\t}\n\tstd::cout << '\\n';\n\treturn;\n}\n\ninline void print() { std::cout << '\\n'; }\ntemplate <class T>\ninline void print(T x) {\n\tstd::cout << x << '\\n';\n\treturn;\n}\ntemplate <typename Head, typename... Tail>\nvoid print(Head H, Tail... T) {\n\tstd::cout << H << \" \";\n\tprint(T...);\n}\n\ntemplate <class T>\nvoid add(std::vector<T> &v, T val) {\n\tfor(auto &a : v) a += val;\n\treturn;\n}\n\ntemplate <class T>\nT dup(T a, T b) {\n\tassert(b != 0);\n\treturn (a + b - 1) / b;\n}\n\ntemplate <class T>\nT greatest_lower_multiple(T x, T d) {\n\tif(d == 0) return 0;\n\tif(d < 0) d *= -1;\n\tT y = x % d;\n\tif(y < 0) y += d;\n\treturn x - y;\n}\n\ntemplate <class T>\nT least_upper_multiple(T x, T d) {\n\treturn -greatest_lower_multiple(-x, d);\n}\n\nlong long POW(long long a, long long n) {\n\tlong long res = 1;\n\twhile(n > 0) {\n\t\tif(n & 1) res = res * a;\n\t\ta = a * a;\n\t\tn >>= 1;\n\t}\n\treturn res;\n}\n\nlong long modpow(long long a, long long n, long long mod) {\t // a^n mod\n\tassert(n >= 0 && mod);\n\tif(mod == 1) return 0LL;\n\tlong long res = 1;\n\ta %= mod;\n\twhile(n > 0) {\n\t\tif(n & 1) res = res * a % mod;\n\t\ta = a * a % mod;\n\t\tn >>= 1;\n\t}\n\treturn res < 0 ? res + mod : res;\n}\n\n// return x which satisfies a * x % mod == gcd(a, mod)\n// not (mod | a)\nlong long modinv(long long a, long long mod) {\n\tlong long b = mod, u = 1, v = 0;\n\twhile(b) {\n\t\tlong long t = a / b;\n\t\ta -= t * b;\n\t\tstd::swap(a, b);\n\t\tu -= t * v;\n\t\tstd::swap(u, v);\n\t}\n\tu %= mod;\n\tif(u < 0) u += mod;\n\treturn u;\n}\n\ntemplate <class T>\nint lb(const std::vector<T> &a, const T x) {\n\treturn std::distance(a.begin(), std::lower_bound(a.begin(), a.end(), x));\n}\ntemplate <class T>\nint ub(const std::vector<T> &a, const T x) {\n\treturn std::distance(a.begin(), std::upper_bound(a.begin(), a.end(), x));\n}\ntemplate <class T>\nvoid unq_sort(std::vector<T> &a) {\n\tstd::sort(a.begin(), a.end());\n\ta.erase(std::unique(a.begin(), a.end()), a.end());\n}\ntemplate <class T>\nstd::vector<int> press(std::vector<T> &a) {\n\tauto vec = a;\n\tunq_sort(vec);\n\tstd::vector<int> ret;\n\tfor(auto &v : a) ret.push_back(lb(vec, v));\n\treturn ret;\n}\n\n#pragma endregion\n\n#pragma region input\n#define VEC(type, name, size) \\\n\tvector<type> name(size); \\\n\tscanner::INPUT(name)\n#define VVEC(type, name, h, w) \\\n\tvector<vector<type>> name(h, vector<type>(w)); \\\n\tscanner::INPUT(name)\n#define INT(...) \\\n\tint __VA_ARGS__; \\\n\tscanner::INPUT(__VA_ARGS__)\n#define LL(...) \\\n\tlong long __VA_ARGS__; \\\n\tscanner::INPUT(__VA_ARGS__)\n#define STR(...) \\\n\tstring __VA_ARGS__; \\\n\tscanner::INPUT(__VA_ARGS__)\n#define CHR(...) \\\n\tchar __VA_ARGS__; \\\n\tscanner::INPUT(__VA_ARGS__)\n#define DBL(...) \\\n\tdouble __VA_ARGS__; \\\n\tscanner::INPUT(__VA_ARGS__)\n#define LD(...) \\\n\tlong double __VA_ARGS__; \\\n\tscanner::INPUT(__VA_ARGS__)\n#define TPL3(type0, type1, type2, name) \\\n\tstd::tuple<type0, type1, type2> name; \\\n\tscanner::INPUT(name);\n#define TPL4(type0, type1, type2, type3, name) \\\n\tstd::tuple<type0, type1, type2, type3> name; \\\n\tscanner::INPUT(name);\n\nnamespace scanner {\ntemplate <class T>\nvoid scan(T &a) {\n\tstd::cin >> a;\n}\n\ntemplate <class T, class L>\nvoid scan(std::pair<T, L> &p) {\n\tscan(p.first);\n\tscan(p.second);\n}\n\ntemplate <class T0, class T1, class T2>\nvoid scan(std::tuple<T0, T1, T2> &p) {\n\tT0 t0;\n\tT1 t1;\n\tT2 t2;\n\tscan(t0);\n\tscan(t1);\n\tscan(t2);\n\tp = std::make_tuple(t0, t1, t2);\n}\n\ntemplate <class T0, class T1, class T2, class T3>\nvoid scan(std::tuple<T0, T1, T2, T3> &p) {\n\tT0 t0;\n\tT1 t1;\n\tT2 t2;\n\tT3 t3;\n\tscan(t0);\n\tscan(t1);\n\tscan(t2);\n\tscan(t3);\n\tp = std::make_tuple(t0, t1, t2, t3);\n}\n\ntemplate <class T>\nvoid scan(std::vector<T> &a) {\n\tfor(auto &i : a) scan(i);\n}\n\nvoid INPUT() {}\ntemplate <class Head, class... Tail>\nvoid INPUT(Head &head, Tail &... tail) {\n\tscan(head);\n\tINPUT(tail...);\n}\n} // namespace scanner\n\ntemplate <typename T1, typename T2>\nstd::istream &operator>>(std::istream &is, std::pair<T1, T2> &p) {\n\tis >> p.first >> p.second;\n\treturn is;\n}\n#pragma endregion\n\n#pragma region debug\n\n#pragma region output\ntemplate <typename T1, typename T2>\nstd::ostream &std::operator<<(std::ostream &os, const std::pair<T1, T2> &p) {\n\tos << p.first << \" \" << p.second;\n\treturn os;\n}\ntemplate <class T>\nstd::ostream &std::operator<<(std::ostream &os, const std::vector<T> &v) {\n\tfor(int i = 0; i < (int)v.size(); i++) {\n\t\tif(i) os << \" \";\n\t\tos << v[i];\n\t}\n\treturn os;\n}\n#pragma endregion\n\n#pragma region view\n\nnamespace viewer {\nvoid view(const long long e) {\n\tif(e == INF)\n\t\tstd::cerr << \"INF\";\n\telse if(e == -INF)\n\t\tstd::cerr << \"-INF\";\n\telse\n\t\tstd::cerr << e;\n}\n\nvoid view(const int e) {\n\tif(e == inf)\n\t\tstd::cerr << \"inf\";\n\telse if(e == -inf)\n\t\tstd::cerr << \"-inf\";\n\telse\n\t\tstd::cerr << e;\n}\n\ntemplate <typename T>\nvoid view(const T e) {\n\tstd::cerr << e;\n}\n\ntemplate <typename T, typename U>\nvoid view(const std::pair<T, U> &p) {\n\tstd::cerr << \"(\";\n\tview(p.first);\n\tstd::cerr << \", \";\n\tview(p.second);\n\tstd::cerr << \")\";\n}\n\ntemplate <class T0, class T1, class T2>\nvoid view(const std::tuple<T0, T1, T2> &p) {\n\tstd::cerr << \"(\";\n\tview(std::get<0>(p));\n\tstd::cerr << \", \";\n\tview(std::get<1>(p));\n\tstd::cerr << \", \";\n\tview(std::get<2>(p));\n\tstd::cerr << \")\";\n}\n\ntemplate <class T0, class T1, class T2, class T3>\nvoid view(const std::tuple<T0, T1, T2, T3> &p) {\n\tstd::cerr << \"(\";\n\tview(std::get<0>(p));\n\tstd::cerr << \", \";\n\tview(std::get<1>(p));\n\tstd::cerr << \", \";\n\tview(std::get<2>(p));\n\tstd::cerr << \", \";\n\tview(std::get<3>(p));\n\tstd::cerr << \")\";\n}\n\ntemplate <typename T>\nvoid view(const std::set<T> &s) {\n\tif(s.empty()) {\n\t\tstd::cerr << \"{ }\";\n\t\treturn;\n\t}\n\tstd::cerr << \"{ \";\n\tfor(auto &t : s) {\n\t\tview(t);\n\t\tstd::cerr << \", \";\n\t}\n\tstd::cerr << \"\\b\\b }\";\n}\n\ntemplate <typename T>\nvoid view(const std::unordered_set<T> &s) {\n\tif(s.empty()) {\n\t\tstd::cerr << \"{ }\";\n\t\treturn;\n\t}\n\tstd::cerr << \"{ \";\n\tfor(auto &t : s) {\n\t\tview(t);\n\t\tstd::cerr << \", \";\n\t}\n\tstd::cerr << \"\\b\\b }\";\n}\n\ntemplate <typename T>\nvoid view(const std::vector<T> &v) {\n\tif(v.empty()) {\n\t\tstd::cerr << \"{ }\";\n\t\treturn;\n\t}\n\tstd::cerr << \"{ \";\n\tfor(const auto &e : v) {\n\t\tview(e);\n\t\tstd::cerr << \", \";\n\t}\n\tstd::cerr << \"\\b\\b }\";\n}\n\ntemplate <typename T>\nvoid view(const std::vector<std::vector<T>> &vv) {\n\tstd::cerr << \"{\\n\";\n\tfor(const auto &v : vv) {\n\t\tstd::cerr << \"\\t\";\n\t\tview(v);\n\t\tstd::cerr << '\\n';\n\t}\n\tstd::cerr << \"}\";\n}\n\ntemplate <typename T, typename U>\nvoid view(const std::vector<std::pair<T, U>> &v) {\n\tstd::cerr << \"{\\n\";\n\tfor(const auto &c : v) {\n\t\tstd::cerr << \"\\t(\";\n\t\tview(c.first);\n\t\tstd::cerr << \", \";\n\t\tview(c.second);\n\t\tstd::cerr << \")\\n\";\n\t}\n\tstd::cerr << \"}\";\n}\n\ntemplate <class T0, class T1, class T2>\nvoid view(const std::vector<std::tuple<T0, T1, T2>> &v) {\n\tif(v.empty()) {\n\t\tstd::cerr << \"{ }\";\n\t\treturn;\n\t}\n\tstd::cerr << '{';\n\tfor(const auto &t : v) {\n\t\tstd::cerr << \"\\n\\t\";\n\t\tview(t);\n\t\tstd::cerr << \",\";\n\t}\n\tstd::cerr << \"\\n}\";\n}\n\ntemplate <class T0, class T1, class T2, class T3>\nvoid view(const std::vector<std::tuple<T0, T1, T2, T3>> &v) {\n\tif(v.empty()) {\n\t\tstd::cerr << \"{ }\";\n\t\treturn;\n\t}\n\tstd::cerr << '{';\n\tfor(const auto &t : v) {\n\t\tstd::cerr << \"\\n\\t\";\n\t\tview(t);\n\t\tstd::cerr << \",\";\n\t}\n\tstd::cerr << \"\\n}\";\n}\n\ntemplate <typename T, typename U>\nvoid view(const std::map<T, U> &m) {\n\tstd::cerr << \"{\\n\";\n\tfor(const auto &t : m) {\n\t\tstd::cerr << \"\\t[\";\n\t\tview(t.first);\n\t\tstd::cerr << \"] : \";\n\t\tview(t.second);\n\t\tstd::cerr << '\\n';\n\t}\n\tstd::cerr << \"}\";\n}\n\ntemplate <typename T, typename U>\nvoid view(const std::unordered_map<T, U> &m) {\n\tstd::cerr << \"{\\n\";\n\tfor(const auto &t : m) {\n\t\tstd::cerr << \"\\t[\";\n\t\tview(t.first);\n\t\tstd::cerr << \"] : \";\n\t\tview(t.second);\n\t\tstd::cerr << '\\n';\n\t}\n\tstd::cerr << \"}\";\n}\n} // namespace viewer\n\n#pragma endregion\n\n// when debugging : g++ foo.cpp -DLOCAL\n#ifdef LOCAL\nvoid debug_out() {}\ntemplate <typename Head, typename... Tail>\nvoid debug_out(Head H, Tail... T) {\n\tviewer::view(H);\n\tstd::cerr << \", \";\n\tdebug_out(T...);\n}\n#define debug(...) \\\n\tdo { \\\n\t\tstd::cerr << __LINE__ << \" [\" << #__VA_ARGS__ << \"] : [\"; \\\n\t\tdebug_out(__VA_ARGS__); \\\n\t\tstd::cerr << \"\\b\\b]\\n\"; \\\n\t} while(0)\n#define dump(x) \\\n\tdo { \\\n\t\tstd::cerr << __LINE__ << \" \" << #x << \" : \"; \\\n\t\tviewer::view(x); \\\n\t\tstd::cerr << '\\n'; \\\n\t} while(0)\n\n#else\n#define debug(...) (void(0))\n#define dump(x) (void(0))\n#endif\n\n#pragma endregion\n#line 8 \"SPJ-Library/geometry/geometry.hpp\"\n\n#pragma region geometry\n\nusing Real = long double;\nusing R = Real;\nusing Point = std::complex<Real>;\nusing Vec = Point;\nconst Real EPS = 1e-10, PI = acos(-1);\n\ninline bool eq(const Real &a, const Real &b) { return fabs(b - a) < EPS; }\ninline bool eq(const Point &a, const Point &b) { return fabs(b - a) < EPS; }\n/*\n\t-1: a < b\n\t0 : a == b\n\t1 : a > b\n*/\ninline int compare(const Real &a, const Real &b) { return eq(a, b) ? 0 : a < b ? -1 : 1; }\n\nnamespace std {\nconst Point operator*(const Point &p, const Real &d) { return Point(real(p) * d, imag(p) * d); }\n} // namespace std\n\nstd::istream &operator>>(std::istream &is, Point &p) {\n\tReal a, b;\n\tis >> a >> b;\n\tp = Point(a, b);\n\treturn is;\n}\nstd::ostream &operator<<(std::ostream &os, Point &p) { return os << std::fixed << std::setprecision(10) << p.real() << \" \" << p.imag(); }\n\n// rotate point 'p' for counter clockwise direction\nconst Point rotate(const Real &theta, const Point &p) {\n\treturn Point(cos(theta) * p.real() - sin(theta) * p.imag(), sin(theta) * p.real() + cos(theta) * p.imag());\n}\n\nconst Real radian_to_degree(const Real &r) { return (r * 180.0 / PI); }\n\nconst Real degree_to_radian(const Real &d) { return (d * PI / 180.0); }\n\n// get angle a-b-c (<pi)\nconst Real get_angle(const Point &a, const Point &b, const Point &c) {\n\tconst Point v(a - b), w(c - b);\n\tReal theta = fabs(atan2(w.imag(), w.real()) - atan2(v.imag(), v.real()));\n\treturn std::min(theta, 2 * PI - theta);\n}\n\nnamespace std {\nbool operator<(const Point &a, const Point &b) { return a.real() != b.real() ? a.real() < b.real() : a.imag() < b.imag(); }\n} // namespace std\n\nstruct Line {\n\tPoint a, b;\n\n\tLine() = default;\n\n\tLine(Point a, Point b) : a(a), b(b) {}\n\n\tLine(Real A, Real B, Real C) // Ax + By = C\n\t{\n\t\tif(eq(A, 0))\n\t\t\ta = Point(0, C / B), b = Point(1, C / B);\n\t\telse if(eq(B, 0))\n\t\t\tb = Point(C / A, 0), b = Point(C / A, 1);\n\t\telse\n\t\t\ta = Point(0, C / B), b = Point(C / A, 0);\n\t}\n\n\tfriend std::ostream &operator<<(std::ostream &os, Line &p) { return os << p.a << \" -- \" << p.b; }\n\n\tfriend std::istream &operator>>(std::istream &is, Line &a) { return is >> a.a >> a.b; }\n};\n\nstruct Segment : Line {\n\tSegment() = default;\n\n\tSegment(Point a, Point b) : Line(a, b) {}\n};\n\nstruct Circle {\n\tPoint p;\n\tReal r;\n\n\tCircle() = default;\n\n\tCircle(Point p, Real r) : p(p), r(r) {}\n};\n\nusing Points = std::vector<Point>;\nusing Polygon = std::vector<Point>;\nusing Segments = std::vector<Segment>;\nusing Lines = std::vector<Line>;\nusing Circles = std::vector<Circle>;\n\nconst Real cross(const Point &a, const Point &b) { return real(a) * imag(b) - imag(a) * real(b); }\nconst Real dot(const Point &a, const Point &b) { return real(a) * real(b) + imag(a) * imag(b); }\n\n// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_1_C\nconst int ccw(const Point &a, Point b, Point c) {\n\tb = b - a, c = c - a;\n\tif(cross(b, c) > EPS) return +1;\t\t// \"COUNTER_CLOCKWISE\"\n\tif(cross(b, c) < -EPS) return -1;\t\t// \"CLOCKWISE\"\n\tif(dot(b, c) < -EPS) return +2;\t\t\t// \"ONLINE_BACK\"\n\tif(norm(b) + EPS < norm(c)) return -2;\t// \"ONLINE_FRONT\"\n\treturn 0;\t\t\t\t\t\t\t\t// \"ON_SEGMENT\"\n}\n\n// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_2_A\nbool parallel(const Line &a, const Line &b) { return eq(cross(a.b - a.a, b.b - b.a), 0.0); }\n\n// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_2_A\nbool orthogonal(const Line &a, const Line &b) { return eq(dot(a.a - a.b, b.a - b.b), 0.0); }\n\n// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_1_A\nPoint projection(const Line &l, const Point &p) {\n\tdouble t = dot(p - l.a, l.a - l.b) / norm(l.a - l.b);\n\treturn l.a + (l.a - l.b) * t;\n}\n\nPoint projection(const Segment &l, const Point &p) {\n\tdouble t = dot(p - l.a, l.a - l.b) / norm(l.a - l.b);\n\treturn l.a + (l.a - l.b) * t;\n}\n\n// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_1_B\nPoint reflection(const Line &l, const Point &p) { return projection(l, p) * 2.0 - p; }\n\nint intersect(const Line &l, const Point &p) { return int(abs(ccw(l.a, l.b, p)) != 1); }\n\nint intersect(const Line &l, const Line &m) {\n\tif(intersect(l, m.a) && intersect(l, m.b)) return -1;\n\treturn int(abs(cross(l.b - l.a, m.b - m.a)) > EPS);\n}\n\nint intersect(const Segment &s, const Point &p) { return int(ccw(s.a, s.b, p) == 0); }\n\nint intersect(const Line &l, const Segment &s) {\n\tif(intersect(l, s.a) && intersect(l, s.b)) return -1;\n\treturn cross(l.b - l.a, s.a - l.a) * cross(l.b - l.a, s.b - l.a) < EPS;\n}\n\nReal distance(const Line &l, const Point &p);\n\nint intersect(const Circle &c, const Line &l) {\n\tReal d = c.r - distance(l, c.p);\n\treturn fabs(d) < EPS ? 1 : d > 0. ? 2 : 0;\n}\n\nint intersect(const Circle &c, const Point &p) { return int(abs(abs(p - c.p) - c.r) < EPS); }\n\n// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_2_B\nint intersect(const Segment &s, const Segment &t) {\n\tif(eq(s.a, s.b)) return intersect(t, s.a);\n\tif(intersect(Line(s), t.a) && intersect(Line(s), t.b) &&\n\t std::max(std::min(s.a, s.b), std::min(t.a, t.b)) < std::min(std::max(s.a, s.b), std::max(t.a, t.b)))\n\t\treturn -1;\n\treturn int(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);\n}\n\nint intersect(const Circle &c, const Segment &l) {\n\tconst Point h = projection(l, c.p);\n\tif(norm(h - c.p) - c.r * c.r > EPS) return 0;\n\tauto d1 = abs(c.p - l.a), d2 = abs(c.p - l.b);\n\tif(compare(d1, c.r) == -1 && compare(d2, c.r) == -1) return 0;\n\tif(d1 < c.r - EPS && d2 > c.r - EPS || d1 > c.r - EPS && d2 < c.r - EPS) return 1;\n\treturn dot(l.a - h, l.b - h) < 0 ? 2 : 0;\n}\n\nReal distance(const Point &a, const Point &b);\n\nint number_of_common_tangents(const Circle &c1, const Circle &c2) {\n\tReal r1 = std::min(c1.r, c2.r), r2 = std::max(c1.r, c2.r), d = distance(c1.p, c2.p);\n\tint com = compare(r1 + r2, d);\n\treturn com == 1 ? compare(d + r1, r2) + 1 : 3 - com;\n\t// if(c1.r < c2.r) swap(c1, c2);\n\t// Real d = abs(c1.p - c2.p);\n\t// if(compare(c1.r + c2.r, d) == -1) return 4;\n\t// if(eq(c1.r + c2.r, d)) return 3;\n\t// if(compare(c1.r - c2.r, d) == -1) return 2;\n\t// return int(eq(c1.r - c2.r, d));\n}\n\n// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_7_A&lang=jp\nint intersect(const Circle &c1, const Circle &c2) { return 2 - abs(2 - number_of_common_tangents(c1, c2)); }\n\nReal distance(const Point &a, const Point &b) { return abs(a - b); }\n\nReal distance(const Line &l, const Point &p) { return abs(p - projection(l, p)); }\n\nReal distance(const Line &l, const Line &m) { return intersect(l, m) ? 0 : distance(l, m.a); }\n\nReal distance(const Segment &s, const Point &p) {\n\tPoint r = projection(s, p);\n\treturn intersect(s, r) ? distance(r, p) : std::min(abs(s.a - p), abs(s.b - p));\n}\n\n// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_2_D\nReal distance(const Segment &a, const Segment &b) {\n\tif(intersect(a, b)) return 0;\n\treturn std::min({distance(a, b.a), distance(a, b.b), distance(b, a.a), distance(b, a.b)});\n}\n\nReal distance(const Line &l, const Segment &s) {\n\tif(intersect(l, s)) return 0;\n\treturn std::min(distance(l, s.a), distance(l, s.b));\n}\n\nPoint crosspoint(const Line &l, const Line &m) {\n\tReal A = cross(l.b - l.a, m.b - m.a);\n\tReal B = cross(l.b - l.a, l.b - m.a);\n\tif(eq(abs(A), 0.0) && eq(abs(B), 0.0)) return m.a;\n\treturn m.a + (m.b - m.a) * B / A;\n}\n\n// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_2_C\nPoint crosspoint(const Segment &l, const Segment &m) { return crosspoint(Line(l), Line(m)); }\n\n// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_7_D\nstd::pair<Point, Point> crosspoint(const Circle &c, const Line l) {\n\tPoint pr = projection(l, c.p);\n\tif(eq(distance(l, c.p), c.r)) return {pr, pr};\n\tVec v = (l.b - l.a) / abs(l.b - l.a) * sqrt(c.r * c.r - norm(pr - c.p));\n\treturn make_pair(pr - v, pr + v);\n\t// Vec e = (l.b - l.a) / abs(l.b - l.a);\n\t// double base = sqrt(c.r * c.r - norm(pr - c.p));\n\t// return {pr - e * base, pr + e * base};\n}\n\nstd::pair<Point, Point> crosspoint(const Circle &c, const Segment &l) {\n\tif(intersect(c, l) == 2) return crosspoint(c, Line(l.a, l.b));\n\tauto ret = crosspoint(c, Line(l.a, l.b));\n\tif(dot(l.a - ret.first, l.b - ret.first) < EPS)\n\t\tret.second = ret.first;\n\telse\n\t\tret.first = ret.second;\n\treturn ret;\n}\n\n// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_7_E\nstd::pair<Point, Point> crosspoint(const Circle &c1, const Circle &c2) {\n\tReal d = abs(c1.p - c2.p);\n\tReal a = acos((c1.r * c1.r + d * d - c2.r * c2.r) / (2 * c1.r * d)); // cosine theorem\n\tReal t = atan2(c2.p.imag() - c1.p.imag(), c2.p.real() - c1.p.real());\n\tPoint p1 = c1.p + Point(cos(t + a) * c1.r, sin(t + a) * c1.r);\n\tPoint p2 = c1.p + Point(cos(t - a) * c1.r, sin(t - a) * c1.r);\n\treturn make_pair(p1, p2);\n}\n\n// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_7_F\n// \"点 p を通る円 c の接線\"の接点2つ\nstd::pair<Point, Point> tangent(const Circle &c1, const Point &p2) {\n\treturn crosspoint(c1, Circle(p2, sqrt(norm(c1.p - p2) - c1.r * c1.r)));\n}\n\n// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_7_G\n// 円 c1, c2 の共通接線\nLines tangent(Circle c1, Circle c2) {\n\tLines ret;\n\tif(c1.r < c2.r) std::swap(c1, c2);\n\tReal g = norm(c1.p - c2.p);\n\tif(eq(g, 0)) return ret;\n\tVec u = (c2.p - c1.p) / Real(sqrt(g));\n\tVec v = rotate(PI * 0.5, u);\n\tfor(int s : {-1, 1}) {\n\t\tReal h = (c1.r + s * c2.r) / sqrt(g);\n\t\tif(eq(1 - h * h, 0)) {\n\t\t\tret.emplace_back(c1.p + u * c1.r, c1.p + (u + v) * c1.r);\n\t\t} else if(1 - h * h > 0) {\n\t\t\tPoint uu = u * h, vv = v * sqrt(1 - h * h);\n\t\t\tret.emplace_back(c1.p + (uu + vv) * c1.r, c2.p - (uu + vv) * c2.r * s);\n\t\t\tret.emplace_back(c1.p + (uu - vv) * c1.r, c2.p - (uu - vv) * c2.r * s);\n\t\t}\n\t}\n\treturn ret;\n}\n\n// bisection of angle A-B-C\n// https://onlinejudge.u-aizu.ac.jp/status/users/Kite_kuma/submissions/1/CGL_7_B/judge/5763316/C++17\nLine bisection_of_angle(const Point &a, const Point &b, const Point &c) {\n\tReal da = distance(a, b);\n\tReal dc = distance(b, c);\n\treturn Line(b, (a * dc + c * da) / (da + dc));\n}\n\n// https://onlinejudge.u-aizu.ac.jp/status/users/Kite_kuma/submissions/1/CGL_7_B/judge/5763316/C++17\nCircle incircle_of_triangle(const Point &a, const Point &b, const Point &c) {\n\tPoint center = crosspoint(bisection_of_angle(a, b, c), bisection_of_angle(b, c, a));\n\treturn Circle(center, distance(Line(a, b), center));\n}\nCircle incircle_of_triangle(const Polygon &p) { return incircle_of_triangle(p[0], p[1], p[2]); }\n\n#pragma endregion\n#line 552 \"c.cpp\"\n\nR radius_ub;\nPoint otherside(Polygon p, int idx) {\n\trotate(p.begin(), p.begin() + idx, p.end());\n\tR beta = distance(p[1], p.front());\n\tR gamma = distance(p[2], p.front());\n\treturn (p[1] * gamma + p[2] * beta) / (beta + gamma);\n}\n\nCircle cornercircle(Polygon p, int idx, R radius, Point leg) {\n\trotate(p.begin(), p.begin() + idx, p.end());\n\tR ang = get_angle(p[1], p.front(), p.back());\n\tVec unitvec = leg - p.front();\n\tunitvec /= fabs(unitvec);\n\tPoint center = p.front() + unitvec * radius / sin(ang / 2);\n\treturn Circle(center, radius);\n}\n\nR good_size(Polygon p, int idx, Circle c, Point leg) {\n\tR low = 0;\n\tR high = radius_ub;\n\trep(30) {\n\t\tR mid = (low + high) / 2;\n\t\tCircle circ = cornercircle(p, idx, mid, leg);\n\t\tif(number_of_common_tangents(c, circ) == 4) {\n\t\t\tlow = mid;\n\t\t} else\n\t\t\thigh = mid;\n\t}\n\treturn low;\n}\n\nvector<R> radius(Polygon p) {\n\tPoints on_edge;\n\trep(i, 3) on_edge.emplace_back(otherside(p, i));\n\n\tCircle inc = incircle_of_triangle(p);\n\tradius_ub = inc.r;\n\n\tauto is_large = [&](R r) -> bool {\n\t\tif(r > radius_ub) return true;\n\t\tCircle a = cornercircle(p, 0, r, on_edge.front());\n\t\tCircles cs;\n\t\trep(idx, 1, 3) {\n\t\t\tR rad = good_size(p, idx, a, on_edge[idx]);\n\t\t\tcs.push_back(cornercircle(p, idx, rad, on_edge[idx]));\n\t\t}\n\t\treturn number_of_common_tangents(cs.front(), cs.back()) == 4;\n\t};\n\n\tR low = 0;\n\tR high = radius_ub;\n\trep(30) {\n\t\tR mid = (low + high) / 2;\n\t\t(is_large(mid) ? high : low) = mid;\n\t}\n\n\tV<R> rads;\n\trads.push_back(low);\n\tCircle c = cornercircle(p, 0, rads.front(), on_edge.front());\n\trep(idx, 1, 3) { rads.push_back(good_size(p, idx, c, on_edge[idx])); }\n\treturn rads;\n}\nint main() {\n\tstd::ios::sync_with_stdio(false);\n\tstd::cin.tie(nullptr);\n\tstd::cout << std::fixed << std::setprecision(15);\n\tsrand((unsigned)time(NULL));\n\n\tvector<int> vec(6);\n\n\twhile(1) {\n\t\tll sum = 0LL;\n\t\tfoa(t, vec) {\n\t\t\tcin >> t;\n\t\t\tsum += t;\n\t\t}\n\t\tif(!sum) break;\n\t\tPolygon p;\n\t\tp.emplace_back(R(vec[0]), R(vec[1]));\n\t\tp.emplace_back(R(vec[2]), R(vec[3]));\n\t\tp.emplace_back(R(vec[4]), R(vec[5]));\n\t\tvector<R> ret = radius(p);\n\t\tvout(ret);\n\t}\n\n\treturn 0;\n}", "accuracy": 1, "time_ms": 1230, "memory_kb": 3724, "score_of_the_acc": -0.9291, "final_rank": 14 }, { "submission_id": "aoj_1301_6270267", "code_snippet": "#include<bits/stdc++.h>\n\nusing namespace std;\n\n#define _ ios_base::sync_with_stdio(0);cin.tie(0);\n#define endl '\\n'\n\ntypedef long long ll;\n\nconst int INF = 0x3f3f3f3f;\nconst ll LINF = 0x3f3f3f3f3f3f3f3f;\n\ntypedef long double ld;\nconst ld DINF = 1e18;\nconst ld pi = acos(-1.0);\nconst ld eps = 1e-8;\n\n#define sq(x) ((x)*(x))\n\nbool eq(ld a, ld b) {\n\treturn abs(a-b) <= eps;\t\n}\n\nstruct pt {\n\tld x, y;\n\tpt(ld x_ = 0, ld y_ = 0) : x(x_), y(y_) {}\n\t\n\tpt operator + (const pt p) const { return pt(x+p.x, y + p.y); }\n\tpt operator - (const pt p) const { return pt(x-p.x, y - p.y); }\n\tpt operator * (const ld c) const { return pt(x*c, y*c); }\n\tpt operator / (const ld c) const { return pt(x/c, y/c); }\n\tld operator * (const pt p) const { return x*p.x + y*p.y; }\n\tld operator ^ (const pt p) const { return x*p.y - y*p.x; }\n\tfriend istream& operator >> (istream& in, pt& p) {\n\t\treturn in >> p.x >> p.y;\t\n\t}\n};\n\nstruct line {\n\tpt p, q;\n\tline() {}\n\tline (pt p_, pt q_) : p(p_), q(q_) {}\n};\n\nstruct circ{\n\tpt c;\n\tld r;\n\tcirc() {}\n\tcirc (pt c_, ld r_) : c(c_), r(r_) {}\n};\n\nld dist(pt p, pt q) { // distancia\n\treturn hypot(p.y - q.y, p.x - q.x);\n}\n\nld norm(pt v) { // norma do vetor\n \treturn dist(pt(0, 0), v);\n}\n\nld sarea(pt p, pt q, pt r) { // area com sinal\n\treturn ((q-p)^(r-q))/2;\n}\n\nbool ccw(pt p, pt q, pt r) { // se p, q, r sao ccw\n\treturn sarea(p, q, r) > eps;\n}\n\npt rotate90(pt p) { // rotaciona 90 graus\n\treturn pt(-p.y, p.x);\n}\n\nbool isinseg(pt p, line r) { // se p pertence ao seg de r\n\tpt a = r.p - p, b = r.q - p;\n\t\treturn eq((a ^ b), 0) and (a * b) < eps;\n}\n\nbool interseg(line r, line s) { // se o seg de r intersecta o seg de s\n\tif (isinseg(r.p, s) or isinseg(r.q, s)\n\t\t\t\tor isinseg(s.p, r) or isinseg(s.q, r)) return 1;\n\n\t\t\t\t\treturn ccw(r.p, r.q, s.p) != ccw(r.p, r.q, s.q) and\n\t\t\t\t\t\t\t\tccw(s.p, s.q, r.p) != ccw(s.p, s.q, r.q);\n}\n\nld disttoline(pt p, line r) { // distancia do ponto a reta\n\treturn 2 * abs(sarea(p, r.p, r.q)) / dist(r.p, r.q);\n}\n\nbool circ_line_inter(pt a, pt b, circ C1) { // intersecao da circunf (c, r) e reta ab\n\tpt c = C1.c;\n\tld r = C1.r;\n\tvector<pt> ret;\n\tb = b-a, a = a-c;\n\tld A = b*b;\n\tld B = a*b;\n\tld C = a*a - r*r;\n\tld D = B*B - A*C;\n\tif (D < -eps) return false;\n\tret.push_back(c+a+b*(-B+sqrt(D+eps))/A);\n\tif (D > eps) ret.push_back(c+a+b*(-B-sqrt(D))/A);\n\treturn true;\n}\n\nvector<pt> circ_inter(circ c1, circ c2) { // intersecao da circunf (a, r) e (b, R)\n\tpt a = c1.c, b = c2.c;\n\tld r = c1.r, R = c2.r;\n\tvector<pt> ret;\n\tld d = dist(a, b);\n\tif (d > r+R or d+min(r, R) < max(r, R)) return ret;\n\tld x = (d*d-R*R+r*r)/(2*d);\n\tld y = sqrt(r*r-x*x);\n\tpt v = (b-a)/d;\n\tret.push_back(a+v*x + rotate90(v)*y);\n\tif (y > 0) ret.push_back(a+v*x - rotate90(v)*y);\n\treturn ret;\n}\n\n\n// bissetriz no ponto a em relação e b e c\npt bisse(pt a, pt b, pt c) {\n\tpt p1 = b-a, p2 = c-a;\n\tp1 = p1 / norm(p1);\n\tp2 = p2 / norm(p2);\n\tpt bic = (p1+p2)/2;\n\tbic = bic / norm(bic);\n\treturn bic;\n}\n\ncirc solve(vector<pt> v, circ c) {\n\tpt bic = bisse(v[0], v[1], v[2]);\n\tld l = 0.1, r = 1e4;\n\tcirc C;\n\twhile (abs(r-l) > eps) {\n\t\tld m = (l+r) / 2;\t\n\t\tpt c1 = v[0] + bic * m;\n\t\tld r1 = disttoline(c1, line(v[0], v[1]));\n\t\tC = circ(c1, r1);\n\t\tif (interseg(line(v[1], v[2]), line(v[0], c1)) or \n\t\t\tcirc_line_inter(v[1], v[2], C)) {\n\t\t\tr = m;\n\t\t\tcontinue;\n\t\t}\n\n\t\tint sz = circ_inter(C, c).size();\n\t\t//cout << \"second sz \" << m << \" \"<< sz << endl;\n\t\tif (sz == 2) r = m;\n\t\telse if (sz == 0) l = m;\n\t\telse break;\n\t}\n\treturn C;\n}\n\nint main() { _\n\tvector<pt> v(3);\n\twhile (cin >> v[0] >> v[1] >> v[2]) {\n\t\tif (v[0].x == 0 and v[0].y == 0 and v[1].x == 0 and v[1].y == 0)\n\t\t\tbreak;\n\n\t\tpt bic = bisse(v[0], v[1], v[2]);\n\t\tld l = 0.1, r = 1e4;\n\n\t\tcirc C1, C2, C3;\n\t\twhile (abs(r-l) > eps) {\n\t\t\tld m = (l+r) / 2;\t\n\t\t\tpt c1 = v[0] + bic * m;\n\t\t\tld r1 = disttoline(c1, line(v[0], v[1]));\n\t\t\tC1 = circ(c1, r1);\n\t\t\tif (interseg(line(v[1], v[2]), line(v[0], c1)) or \n\t\t\t\t\tcirc_line_inter(v[1], v[2], C1)) {\n\t\t\t\tr = m;\n\t\t\t\tcontinue;\n\t\t\t}\n\n\t\t\tC1 = circ(c1, r1);\n\t\t\trotate(v.begin(), v.begin()+1, v.end());\n\t\t\tC2 = solve(v, C1);\n\t\t\trotate(v.begin(), v.begin()+1, v.end());\n\t\t\tC3 = solve(v, C1);\n\t\t\trotate(v.begin(), v.begin()+1, v.end());\n\n\t\t\tint sz = circ_inter(C2, C3).size();\n\t\t\t//cout << m << \" \" << sz << endl;\n\t\t\tif (sz == 2) l = m;\n\t\t\telse if (sz == 0) r = m;\n\t\t\telse break;\n\t\t}\n\t\tcout << setprecision(6) << fixed;\t\t\n\t\tcout << C1.r << \" \" << C2.r << \" \" << C3.r << endl;\n\n\t}\n\n\texit(0);\n}", "accuracy": 1, "time_ms": 1090, "memory_kb": 3344, "score_of_the_acc": -0.4253, "final_rank": 8 }, { "submission_id": "aoj_1301_6265984", "code_snippet": "#include <bits/stdc++.h>\n#define debug(x) cout << #x << \" = \" << x << endl\n#define REP(i, n) for (Long i = 0; i < (Long)n; i++)\nusing namespace std;\n\ntypedef long long Long;\ntypedef long double Double;\nconst Double EPS = 1e-9;\nconst Double PI = acos(-1);\n\nbool near(Double a, Double b) {\n\treturn fabs(a - b) < EPS;\n}\n\nDouble square(Double x) {\n\treturn x * x;\n}\n\nint getSgn(Double x) {\n\tif (near(x, 0)) return 0;\n\tif (x > 0) return 1;\n\treturn -1;\n}\n\nstruct Point{\n\tDouble x, y;\n\tPoint(Double x = 0, Double y = 0): x(x), y(y) {}\n\tPoint operator +(const Point &other) const {\n\t\treturn Point(x + other.x, y + other.y);\n\t}\n\tPoint operator -(const Point &other) const {\n\t\treturn Point(x - other.x, y - other.y);\n\t}\n\tPoint operator *(Double t) const {\n\t\treturn Point(x * t, y * t);\n\t}\n\tPoint operator /(Double t) const {\n\t\tassert(!near(t, 0));\n\t\treturn Point(x / t, y / t);\n\t}\n\tPoint operator +=(const Point &other) {\n\t\t*this = *this + other;\n\t\treturn *this;\n\t}\n\tPoint operator -=(const Point &other) {\n\t\t*this = *this - other;\n\t\treturn *this;\n\t}\n\tPoint operator *=(Double t) {\n\t\t*this = *this * t;\n\t\treturn *this;\n\t}\n\tPoint operator /=(Double t) {\n\t\t*this = *this / t;\n\t\treturn *this;\n\t}\n\tbool operator ==(const Point &P) const {\n\t\treturn near(x, P.x) && near(y, P.y);\n\t}\n\tDouble abs2() {\n\t\treturn x * x + y * y;\n\t}\n\tDouble abs() {\n\t\treturn sqrt(abs2());\n\t}\n\tPoint ort() {\n\t\treturn Point(-y, x);\n\t}\n\tDouble cross(const Point &other) const {\n\t\treturn x * other.y - y * other.x;\n\t}\n\tDouble cross(const Point &A, const Point &B) const {\n\t\t//Given the current point P, this calculate PA x PB\n\t\treturn (A - *this).cross(B - *this);\n\t}\n};\n\ntypedef Point Vector;\n\n//dot and cross products\nDouble dot(Point A, Point B) {\n\treturn A.x * B.x + A.y * B.y;\n}\nDouble cross(Point A, Point B) {\n\treturn A.x * B.y - A.y * B.x;\n}\n\n//additional point operators with double\nPoint operator *(Double a, Point b) {\n\treturn b * a;\n}\n\nPoint operator /(Double a , Point b) {\n\treturn b / a;\n}\n\nDouble dist(Point A, Point B) {\n\treturn sqrt(square(B.x - A.x) + square(B.y - A.y));\n}\n\nVector bisect(Vector A, Vector B) {\n\treturn A * B.abs() + B * A.abs();\n}\n\nistream & operator >> (istream &in, Point &A) {\n\tin >> A.x >> A.y;\n\treturn in;\n}\n\nostream & operator << (ostream &out, const Point &A) {\n\tout << A.x << \" \" << A.y;\n\treturn out;\n}\n\nconst int IT = 1e4;\n\npair<Double, Double> getRadious(Double r1, Double a, Double b, Double c, Double d1, Double d2) {\n\tDouble disc2 = r1 - b * (a * r1 - d1);\n\tassert(disc2 >= -EPS);\n\tdisc2 += EPS;\n\tDouble r2 = (sqrt(disc2) - sqrt(r1)) / b;\n\tr2 *= r2;\n\t\n\tDouble disc3 = r1 - c * (a * r1 - d2);\n\tassert(disc3 >= -EPS);\n\tdisc3 += EPS;\n\tDouble r3 = (sqrt(disc3) - sqrt(r1)) / c;\n\tr3 *= r3;\n\tassert(r2 >= -EPS);\n\tassert(r3 >= -EPS);\n\treturn {r2, r3};\n}\nint main() {\n\tios_base::sync_with_stdio(false);\n\tcin.tie(0);\n\tcout << fixed << setprecision(9);\n\t\n\tLong xA, yA, xB, yB, xC, yC;\n\twhile (cin >> xA >> yA >> xB >> yB >> xC >> yC) {\n\t\tif (xA == 0 && yA == 0 && xB == 0 && yB == 0 && xC == 0 && yC == 0) {\n\t\t\tbreak;\n\t\t}\n\t\tPoint A(xA, yA), B(xB, yB), C(xC, yC);\n\t\tDouble a = dot(B - A, bisect(B - A, C - A)) / cross(B - A, bisect(B - A, C - A));\n\t\tDouble b = dot(C - B, bisect(C - B, A - B)) / cross(C - B, bisect(C - B, A - B));\n\t\tDouble c = dot(A - C, bisect(A - C, B - C)) / cross(A - C, bisect(A - C, B - C));\n\t\tassert(a >= -EPS);\n\t\tassert(b >= -EPS);\n\t\tassert(c >= -EPS);\n\t\tDouble d1 = dist(A, B);\n\t\tDouble d2 = dist(A, C);\n\t\tDouble d3 = dist(B, C);\n\t\t//a * r1 + b * r2 + 2 * sqrt(r1 * r2) = d1\n\t\t//a * r1 + c * r3 + 2 * sqrt(r1 * r3) = d2\n\t\t//b * r2 + c * r3 + 2 * sqrt(r2 * r3) = d3\n\t\tDouble low = 0.1;\n\t\tDouble high = min({b * d1 / (a * b - 1), d1 / a , c * d2 / (a * c - 1), d2 / a});\n\t\tfor (int i = 0; i < IT; i++) {\n\t\t\tDouble r1 = (low + high) / 2;\n\t\t\t\n\t\t\tauto radii = getRadious(r1, a, b, c, d1, d2);\n\t\t\tDouble r2 = radii.first;\n\t\t\tDouble r3 = radii.second;\n\t\t\t\n\t\t\tDouble val = b * r2 + c * r3 + 2 * sqrt(r2 * r3);\n\t\t\tif (val < d3) {\n\t\t\t\thigh = r1;\n\t\t\t} else {\n\t\t\t\tlow = r1;\n\t\t\t}\n\t\t}\n\t\tDouble r1 = (low + high) / 2.0;\n\t\tauto radii = getRadious(r1, a, b, c, d1, d2);\n\t\tDouble r2 = radii.first;\n\t\tDouble r3 = radii.second;\n\t\t\n\t\tcout << r1 << \" \" << r2 << \" \" << r3 << \"\\n\";\n\t}\n\t\n\treturn 0;\n}", "accuracy": 1, "time_ms": 780, "memory_kb": 3344, "score_of_the_acc": -0.3775, "final_rank": 5 }, { "submission_id": "aoj_1301_6052367", "code_snippet": "#include <iostream>\n#include <iomanip>\n\n#include <algorithm>\n#include <cassert>\n#include <complex>\n#include <iostream>\n#include <optional>\n#include <tuple>\n#include <variant>\n#include <vector>\n\nnamespace suisen {\nnamespace geometry {\n\n using coordinate_t = long double;\n using Point = std::complex<coordinate_t>;\n\n // operator\n\n Point operator+(const Point &p, coordinate_t real) { return Point(p) + Point(real, 0); }\n Point operator-(const Point &p, coordinate_t real) { return Point(p) - Point(real, 0); }\n Point operator*(const Point &p, coordinate_t real) { return Point(p) * Point(real, 0); }\n Point operator/(const Point &p, coordinate_t real) { return Point(p) / Point(real, 0); }\n Point operator+(coordinate_t real, const Point &p) { return Point(real, 0) + Point(p); }\n Point operator-(coordinate_t real, const Point &p) { return Point(real, 0) - Point(p); }\n Point operator*(coordinate_t real, const Point &p) { return Point(real, 0) * Point(p); }\n Point operator/(coordinate_t real, const Point &p) { return Point(real, 0) / Point(p); }\n\n std::istream& operator>>(std::istream &in, Point &p) {\n coordinate_t x, y;\n in >> x >> y;\n p = Point(x, y);\n return in;\n }\n std::ostream& operator<<(std::ostream &out, const Point &p) {\n return out << p.real() << ' ' << p.imag();\n }\n\n // relations between three points X, Y, Z.\n\n struct ISP {\n static constexpr int L_CURVE = +1; // +---------------+ Z is in 'a' => ISP = +1\n static constexpr int R_CURVE = -1; // |aaaaaaaaaaaaaaa| Z is in 'b' => ISP = -1\n static constexpr int FRONT = +2; // |ddd X eee Y ccc| Z is in 'c' => ISP = +2\n static constexpr int BACK = -2; // |bbbbbbbbbbbbbbb| Z is in 'd' => ISP = -2\n static constexpr int MIDDLE = 0; // +---------------+ Z is in 'e' => ISP = 0\n };\n\n struct Sign {\n static constexpr int NEGATIVE = -1;\n static constexpr int ZERO = 0;\n static constexpr int POSITIVE = +1;\n };\n\n enum class Containment {\n OUT, ON, IN\n };\n\n constexpr Point ZERO = Point(0, 0);\n constexpr Point ONE = Point(1, 0);\n constexpr Point I = Point(0, 1);\n constexpr coordinate_t EPS = 1e-9;\n constexpr coordinate_t PI = 3.14159265358979323846264338327950288419716939937510L;\n constexpr coordinate_t E = 2.71828182845904523536028747135266249775724709369995L;\n\n constexpr auto XY_COMPARATOR = [](const Point &p, const Point &q) {\n return p.real() == q.real() ? p.imag() < q.imag() : p.real() < q.real();\n };\n constexpr auto XY_COMPARATOR_GREATER = [](const Point &p, const Point &q) {\n return p.real() == q.real() ? p.imag() > q.imag() : p.real() > q.real();\n };\n constexpr auto YX_COMPARATOR = [](const Point &p, const Point &q) {\n return p.imag() == q.imag() ? p.real() < q.real() : p.imag() < q.imag();\n };\n constexpr auto YX_COMPARATOR_GREATER = [](const Point &p, const Point &q) {\n return p.imag() == q.imag() ? p.real() > q.real() : p.imag() > q.imag();\n };\n\n int sgn(coordinate_t x) {\n return x > EPS ? Sign::POSITIVE : x < -EPS ? Sign::NEGATIVE : Sign::ZERO;\n }\n int compare(coordinate_t x, coordinate_t y) {\n return sgn(x - y);\n }\n\n auto cartesian(const coordinate_t real, const coordinate_t imag) {\n return Point(real, imag);\n }\n auto polar(const coordinate_t rho, const coordinate_t theta) {\n return Point(rho * std::cos(theta), rho * std::sin(theta));\n }\n auto cis(const coordinate_t theta) {\n return Point(std::cos(theta), std::sin(theta));\n }\n auto conj(const Point &z) {\n return Point(z.real(), -z.imag());\n }\n auto arg(const Point &z) {\n return std::atan2(z.imag(), z.real());\n }\n auto square_abs(const Point &z) {\n return z.real() * z.real() + z.imag() * z.imag();\n }\n auto abs(const Point &z) {\n return std::sqrt(square_abs(z));\n }\n auto rot(const Point &z, const coordinate_t theta) {\n return cis(theta) * z;\n }\n auto dot(const Point &a, const Point &b) {\n return a.real() * b.real() + a.imag() * b.imag();\n }\n auto det(const Point &a, const Point &b) {\n return a.real() * b.imag() - a.imag() * b.real();\n }\n bool equals(const Point &a, const Point &b) {\n return sgn(a.real() - b.real()) == Sign::ZERO and sgn(a.imag() - b.imag()) == Sign::ZERO;\n }\n bool equals(coordinate_t a, coordinate_t b) {\n return compare(a, b) == 0;\n }\n \n // https://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_1_C\n int isp(const Point &a, const Point &b, const Point &c) {\n Point ab = b - a, ac = c - a;\n int s = sgn(det(ab, ac));\n if (s == Sign::POSITIVE) return ISP::L_CURVE;\n if (s == Sign::NEGATIVE) return ISP::R_CURVE;\n if (sgn(dot(ab, ac)) == Sign::NEGATIVE) return ISP::BACK;\n Point ba = a - b, bc = c - b;\n if (sgn(dot(ba, bc)) == Sign::NEGATIVE) return ISP::FRONT;\n return ISP::MIDDLE;\n }\n\n struct Line {\n Point a, b;\n Line() : Line(ZERO, ZERO) {}\n Line(const Point &from, const Point &to) : a(from), b(to) {}\n };\n struct Ray {\n Point a, b;\n Ray() : Ray(ZERO, ZERO) {}\n Ray(const Point &from, const Point &to) : a(from), b(to) {}\n };\n struct Segment {\n Point a, b;\n Segment() : Segment(ZERO, ZERO) {}\n Segment(const Point &from, const Point &to) : a(from), b(to) {}\n };\n struct Circle {\n Point center;\n coordinate_t radius;\n Circle() : Circle(ZERO, 0) {}\n Circle(const Point &c, const coordinate_t &r) : center(c), radius(r) {}\n };\n\n // Triangle\n \n coordinate_t signed_area(const Point &a, const Point &b, const Point &c) {\n return det(b - a, c - a) / 2;\n }\n coordinate_t area(const Point &a, const Point &b, const Point &c) {\n return std::abs(signed_area(a, b, c));\n }\n Point pG(const Point &a, const Point &b, const Point &c) {\n return (a + b + c) / 3;\n }\n // https://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_7_B\n Circle pI(const Point &a, const Point &b, const Point &c) {\n auto la = std::abs(b - c), lb = std::abs(c - a), lc = std::abs(a - b);\n auto l = la + lb + lc;\n la /= l, lb /= l, lc /= l;\n Point center = la * a + lb * b + lc * c;\n auto radius = 2. * area(a, b, c) / l;\n return Circle(center, radius);\n }\n // https://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_7_C\n Circle pO(const Point &a, const Point &b, const Point &c) {\n Point ab = b - a, bc = c - b, ca = a - c;\n auto la = square_abs(bc), lb = square_abs(ca), lc = square_abs(ab);\n auto s = la * (lb + lc - la), t = lb * (lc + la - lb), u = lc * (la + lb - lc);\n auto l = s + t + u;\n s /= l, t /= l, u /= l;\n Point center = a * s + b * t + c * u;\n return Circle(center, std::abs(center - a));\n }\n Point pH(const Point &a, const Point &b, const Point &c) {\n return a + b + c - 2 * pO(a, b, c).center;\n }\n auto pIabc(const Point &a, const Point &b, const Point &c) {\n return std::make_tuple(pI(-a, b, c), pI(a, -b, c), pI(a, b, -c));\n }\n\n // Line\n\n // https://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_2_A\n template <typename line_t_1, typename line_t_2>\n auto is_parallel(const line_t_1 &l1, const line_t_2 &l2) -> decltype(l1.a, l1.b, l2.a, l2.b, bool()) {\n return sgn(det(l1.b - l1.a, l2.b - l2.a)) == Sign::ZERO;\n }\n // https://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_2_A\n template <typename line_t_1, typename line_t_2>\n auto is_orthogonal(const line_t_1 &l1, const line_t_2 &l2) -> decltype(l1.a, l1.b, l2.a, l2.b, bool()) {\n return sgn(dot(l1.b - l1.a, l2.b - l2.a)) == Sign::ZERO;\n }\n template <typename line_t_1, typename line_t_2>\n auto on_the_same_line(const line_t_1 &l1, const line_t_2 &l2) -> decltype(l1.a, l1.b, l2.a, l2.b, bool()) {\n return is_parallel(l1, l2) and sgn(det(l1.b - l1.a, l2.a - l1.a)) == Sign::ZERO;\n }\n\n // https://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_1_A\n template <typename line_t>\n Point projection(const Point &p, const line_t &line) {\n Point a = p - line.a;\n Point b = line.b - line.a;\n return line.a + dot(a, b) / square_abs(b) * b;\n }\n\n // https://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_1_B\n template <typename line_t>\n Point reflection(const Point &p, const line_t &line) {\n Point h = projection(p, line);\n return p + (h - p) * 2;\n }\n\n // https://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_2_D\n coordinate_t square_distance(const Point &p, const Segment &l) {\n Point h = projection(p, l);\n if (isp(l.a, l.b, h) == ISP::MIDDLE) {\n return square_abs(h - p);\n } else {\n return std::min(square_abs(p - l.a), square_abs(p - l.b));\n }\n }\n coordinate_t square_distance(const Segment &l, const Point &p) {\n return square_distance(p, l);\n }\n coordinate_t square_distance(const Point &p, const Ray &l) {\n Point h = projection(p, l);\n int dir = isp(l.a, l.b, h);\n return dir == ISP::MIDDLE or dir == ISP::FRONT ? square_abs(h - p) : std::min(square_abs(p - l.a), square_abs(p - l.b));\n }\n coordinate_t square_distance(const Ray &l, const Point &p) {\n return square_distance(p, l);\n }\n coordinate_t square_distance(const Point &p, const Line &l) {\n return square_abs(projection(p, l) - p);\n }\n coordinate_t square_distance(const Line &l, const Point &p) {\n return square_distance(p, l);\n }\n // https://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_2_D\n coordinate_t distance(const Point &p, const Segment &l) {\n return std::sqrt(square_distance(p, l));\n }\n // https://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_2_D\n coordinate_t distance(const Segment &l, const Point &p) {\n return distance(p, l);\n }\n coordinate_t distance(const Point &p, const Ray &l) {\n return std::sqrt(square_distance(p, l));\n }\n coordinate_t distance(const Ray &l, const Point &p) {\n return distance(p, l);\n }\n coordinate_t distance(const Point &p, const Line &l) {\n return std::sqrt(square_distance(p, l));\n }\n coordinate_t distance(const Line &l, const Point &p) {\n return distance(p, l);\n }\n\n Containment contains(const Segment &l, const Point &p) {\n return sgn(distance(l, p)) == 0 ? Containment::ON : Containment::OUT;\n }\n Containment contains(const Ray &l, const Point &p) {\n return sgn(distance(l, p)) == 0 ? Containment::ON : Containment::OUT;\n }\n Containment contains(const Line &l, const Point &p) {\n return sgn(distance(l, p)) == 0 ? Containment::ON : Containment::OUT;\n }\n\n bool equals(const Line &l, const Line &m) {\n return on_the_same_line(l, m);\n }\n bool equals(const Ray &l, const Ray &m) {\n return on_the_same_line(l, m) and equals(l.a, m.a);\n }\n bool equals(const Segment &l, const Segment &m) {\n return (equals(l.a, m.a) and equals(l.b, m.b)) or (equals(l.a, m.b) and equals(l.b, m.a));\n }\n\n // \"https://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_2_B\"\n bool has_common_point(const Segment &l1, const Segment &l2) {\n int isp_1a = isp(l1.a, l1.b, l2.a), isp_1b = isp(l1.a, l1.b, l2.b);\n if (isp_1a * isp_1b > 0) return false;\n int isp_2a = isp(l2.a, l2.b, l1.a), isp_2b = isp(l2.a, l2.b, l1.b);\n if (isp_2a * isp_2b > 0) return false;\n return true;\n }\n\n namespace internal {\n template <typename line_t_1, typename line_t_2>\n Point cross_point(const line_t_1 &l1, const line_t_2 &l2) {\n assert(not is_parallel(l1, l2));\n Point u = l1.b - l1.a, v = l2.a - l2.b, c = l2.a - l1.a;\n return l2.a - det(u, c) / det(u, v) * v;\n }\n }\n\n // \"https://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_2_C\"\n std::variant<std::nullptr_t, Point, Segment> common_point(const Segment &l1, const Segment &l2) {\n if (not has_common_point(l1, l2)) return nullptr;\n if (not is_parallel(l1, l2)) return internal::cross_point(l1, l2);\n std::vector<Point> ps { l1.a, l1.b, l2.a, l2.b };\n for (int i = 0; i <= 2; ++i) for (int j = 2; j >= i; --j) {\n if (XY_COMPARATOR(ps[j + 1], ps[j])) std::swap(ps[j], ps[j + 1]);\n }\n if (equals(ps[1], ps[2])) return ps[1];\n return Segment(ps[1], ps[2]);\n }\n\n // https://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_2_D\n coordinate_t square_distance(const Segment &l1, const Segment &l2) {\n if (has_common_point(l1, l2)) return 0;\n return std::min({ square_distance(l1, l2.a), square_distance(l1, l2.b), square_distance(l1.a, l2), square_distance(l1.b, l2) });\n }\n // https://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_2_D\n coordinate_t distance(const Segment &l1, const Segment &l2) {\n return std::sqrt(square_distance(l1, l2));\n }\n\n // Polygon\n\n using Polygon = std::vector<Point>;\n\n // https://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_3_A\n coordinate_t signed_area(const Polygon &poly) {\n coordinate_t res = 0;\n int sz = poly.size();\n for (int i = 0; i < sz; ++i) {\n int j = i + 1;\n if (j == sz) j = 0;\n res += signed_area(ZERO, poly[i], poly[j]);\n }\n return res;\n }\n // https://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_3_C\n Containment contains(const Polygon &poly, const Point &p) {\n bool in = false;\n int sz = poly.size();\n for (int i = 0; i < sz; ++i) {\n int j = i + 1;\n if (j == sz) j -= sz;\n Point a = poly[i] - p, b = poly[j] - p;\n if (a.imag() > b.imag()) std::swap(a, b);\n if (sgn(a.imag()) <= 0 and sgn(b.imag()) > 0 and sgn(det(a, b)) < 0) in = not in;\n if (sgn(det(a, b)) == 0 and sgn(dot(a, b)) <= 0) return Containment::ON;\n }\n return in ? Containment::IN : Containment::OUT;\n }\n\n // Circle\n\n // https://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_7_A\n int tangent_num(const Circle &c1, const Circle &c2) {\n coordinate_t r1 = c1.radius, r2 = c2.radius;\n if (r1 > r2) return tangent_num(c2, c1);\n coordinate_t d = abs(c1.center - c2.center);\n int cp = compare(d, r1 + r2);\n if (cp > 0) return 4;\n if (cp == 0) return 3;\n int cn = compare(d, r2 - r1);\n if (cn > 0) return 2;\n if (cn == 0) return 1;\n return 0;\n }\n}\n} // namespace suisen\n\nusing namespace suisen::geometry;\n\nbool solve() {\n coordinate_t x0, y0, x1, y1, x2, y2;\n std::cin >> x0 >> y0 >> x1 >> y1 >> x2 >> y2;\n if (x0 == 0 and y0 == 0 and x1 == 0 and y1 == 0 and x2 == 0 and y2 == 0) return false;\n Point A { x0, y0 }, B { x1, y1 }, C { x2, y2 };\n Polygon ABC { A, B, C };\n Point O = pI(A, B, C).center;\n\n Point dA = (O - A) / abs(O - A);\n Point dB = (O - B) / abs(O - B);\n Point dC = (O - C) / abs(O - C);\n\n coordinate_t sA = abs(projection(A + dA, Line { A, B }) - (A + dA));\n coordinate_t sB = abs(projection(B + dB, Line { B, C }) - (B + dB));\n coordinate_t sC = abs(projection(C + dC, Line { C, A }) - (C + dC));\n\n coordinate_t cA = sqrtl(std::max(coordinate_t(0), 1 - sA * sA));\n coordinate_t cB = sqrtl(std::max(coordinate_t(0), 1 - sB * sB));\n coordinate_t cC = sqrtl(std::max(coordinate_t(0), 1 - sC * sC));\n\n coordinate_t AB = abs(B - A);\n coordinate_t BC = abs(C - B);\n coordinate_t CA = abs(A - C);\n\n coordinate_t ans_x = 0, ans_y = 0, ans_z = 0;\n\n coordinate_t lx = 0, rx = std::min(coordinate_t(5000), std::min(CA, AB) / cA);\n for (int loop_x = 0; loop_x < 128; ++loop_x) {\n coordinate_t x = (lx + rx) / 2;\n Point X = A + x * dA;\n coordinate_t hX = x * sA;\n if (contains(ABC, X + dA * hX) == Containment::OUT) {\n rx = x;\n continue;\n }\n Circle cX { X, hX };\n\n coordinate_t ly = 0, ry = std::min(coordinate_t(5000), std::min(AB, BC) / cB);\n for (int loop_y = 0; loop_y < 128; ++loop_y) {\n coordinate_t y = (ly + ry) / 2;\n Point Y = B + y * dB;\n if (contains(ABC, Y) == Containment::OUT) {\n ry = y;\n continue;\n }\n coordinate_t hY = y * sB;\n Circle cY { Y, hY };\n if (tangent_num(cX, cY) == 4) {\n ly = y;\n } else {\n ry = y;\n }\n }\n coordinate_t y = ry;\n Point Y = B + y * dB;\n coordinate_t hY = y * sB;\n Circle cY { Y, hY };\n if (tangent_num(cX, cY) < 3) {\n lx = x;\n continue;\n }\n\n coordinate_t lz = 0, rz = std::min(coordinate_t(5000), std::min(BC, CA) / cC);\n for (int loop_z = 0; loop_z < 128; ++loop_z) {\n coordinate_t z = (lz + rz) / 2;\n Point Z = C + z * dC;\n if (contains(ABC, Z) == Containment::OUT) {\n rz = z;\n continue;\n }\n coordinate_t hZ = z * sC;\n Circle cZ { Z, hZ };\n if (tangent_num(cX, cZ) == 4) {\n lz = z;\n } else {\n rz = z;\n }\n }\n\n coordinate_t z = rz;\n Point Z = C + z * dC;\n coordinate_t hZ = z * sC;\n Circle cZ { Z, hZ };\n if (tangent_num(cX, cZ) < 3) {\n lx = x;\n continue;\n }\n\n if (tangent_num(cY, cZ) == 4) {\n rx = x;\n } else {\n lx = x;\n }\n\n ans_x = hX, ans_y = hY, ans_z = hZ;\n }\n std::cout << ans_x << ' ' << ans_y << ' ' << ans_z << std::endl;\n return true;\n}\n\nint main() {\n std::ios::sync_with_stdio(false);\n std::cin.tie(nullptr);\n std::cout << std::fixed << std::setprecision(6);\n\n while (solve());\n return 0;\n}", "accuracy": 1, "time_ms": 3650, "memory_kb": 3608, "score_of_the_acc": -1.1548, "final_rank": 19 }, { "submission_id": "aoj_1301_5550393", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\n//geometry library by maine\n\n//basic\nusing Real = double;\nusing Point = complex<Real>;\n#define x real()\n#define y imag()\nconst Real EPS = 1e-8, PI = acos(-1), INF = 1e10;\n\nistream& operator>>(istream& is, Point& p) {\n Real a, b;\n is >> a >> b;\n p = Point(a, b);\n return is;\n}\nostream& operator<<(ostream& os, Point p) {\n return os << fixed << setprecision(10) << p.x << \" \" << p.y;\n}\n\nPoint operator*(const Point& p, const Real& d) { return Point(p.x * d, p.y * d); }\n\nnamespace std {\nbool operator<(const Point& a, const Point& b) {\n return a.x != b.x ? a.x < b.x : a.y < b.y;\n}\n} // namespace std\n\nReal get_angle(const Point& a, const Point& b, const Point& c) {\n return arg((c - a) / (b - a));\n}\n\n//unit vector\nPoint e(const Point& p) { return p / abs(p); }\n\n//angle transformation\nReal radian_to_degree(Real r) { return r * 180.0 / PI; }\nReal degree_to_radian(Real d) { return d * PI / 180.0; }\n\n//basic functions\nbool eq(const Real& r, const Real& s) { return abs(r - s) < EPS; }\nbool eq(const Point& p, const Point& q) { return abs(p - q) < EPS; }\nReal dot(const Point& p, const Point& q) { return p.x * q.x + p.y * q.y; }\nReal cross(const Point& p, const Point& q) { return p.x * q.y - p.y * q.x; }\nPoint rotate(const Real& theta, const Point& p) {\n return {cos(theta) * p.x - sin(theta) * p.y, sin(theta) * p.x + cos(theta) * p.y};\n}\n\n//structs : Line, Segment, Circle\nstruct Line {\n Point a, b;\n Line() = default;\n Line(Point a, Point b) : a(a), b(b) {}\n Point d() const { return e(b - a); } //direction vector\n Point n() const {\n Point dd = d();\n return {dd.y, -dd.x};\n } //normal vector\n};\nstruct Segment : Line {\n Segment() = default;\n Segment(Point a, Point b) : Line(a, b) {}\n};\nstruct Circle {\n Point p;\n Real r;\n Circle() = default;\n Circle(Point p, Real r) : p(p), r(r){};\n};\nusing Points = vector<Point>;\nusing Polygon = vector<Point>;\nusing Polygons = vector<Polygon>;\nusing Lines = vector<Line>;\nusing Segments = vector<Segment>;\nusing Circles = vector<Circle>;\n\n//happy functions\n//https://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_1_C\nint ccw(const Point& a, const Point& bb, const Point& cc) {\n Point b = bb - a, c = cc - a;\n if (cross(b, c) > EPS)\n return 1; //COUNTER_CLOCKWISE\n if (cross(b, c) < -EPS)\n return -1; //CLOCKWISE\n if (dot(b, c) < 0)\n return 2; //ONLINE_BACK c-a-b\n if (norm(b) < norm(c))\n return -2; //ONLINE_FRONT a-b-c\n return 0; //ON_SEGMENT a-c-b\n}\n\nbool parallel(const Line& l, const Line& m) { return eq(cross(l.d(), m.d()), 0.0); }\nbool orthogonal(const Line& l, const Line& m) { return eq(dot(l.d(), m.d()), 0.0); }\nPoint projection(const Line& l, const Point& p) { return p + l.n() * dot(l.a - p, l.n()); }\nPoint reflection(const Line& l, const Point& p) { return p + l.n() * 2.0 * dot(l.a - p, l.n()); }\n\n//intersect\nbool intersect(const Line& l, const Point& p) { return abs(ccw(l.a, l.b, p)) != 1; }\nbool intersect(const Line& l, const Line& m) { return !parallel(l, m) || (parallel(l, m) && intersect(l, m.a)); }\nbool intersect(const Segment& s, const Point& p) { return ccw(s.a, s.b, p) == 0; }\nbool intersect(const Line& l, const Segment& s) { return cross(l.d(), s.a - l.a) * cross(l.d(), s.b - l.a) < EPS; }\nbool intersect(const Segment& s, const Segment& t) {\n 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;\n}\n\n//distance\nReal distance(const Point& p, const Point& q) { return abs(p - q); }\nReal distance(const Line& l, const Point& p) { return distance(p, projection(l, p)); }\nReal distance(const Line& l, const Line& m) { return intersect(l, m) ? 0 : distance(l, m.a); }\nReal distance(const Segment& s, const Point& p) {\n Point q = projection(s, p);\n return intersect(s, q) ? distance(p, q) : min(distance(s.a, p), distance(s.b, p));\n}\nReal distance(const Segment& s, const Segment& t) { return intersect(s, t) ? 0 : min({distance(s, t.a), distance(s, t.b), distance(t, s.a), distance(t, s.b)}); }\nReal distance(const Line& l, const Segment& s) { return intersect(l, s) ? 0 : min(distance(l, s.a), distance(l, s.b)); }\n\n//intersect circle\nbool intersect(const Circle& c, const Point& p) { return eq(distance(c.p, p), c.r); }\nbool intersect(const Circle& c, const Line& l) { return distance(l, c.p) <= c.r + EPS; }\nint intersect(const Circle& c, const Segment& s) {\n if (!intersect(c, Line(s)))\n return 0;\n Real da = distance(c.p, s.a), db = distance(c.p, s.b);\n if (da < c.r + EPS && db < c.r + EPS)\n return 0;\n if (da > db)\n swap(da, db);\n if (da < c.r - EPS && db > c.r + EPS)\n return 1;\n if (intersect(s, projection(s, c.p)))\n return 2;\n return 0;\n}\nint intersect(Circle c1, Circle c2) {\n if (c1.r < c2.r)\n swap(c1, c2);\n Real d = distance(c1.p, c2.p);\n if (c1.r + c2.r < d)\n return 4;\n if (eq(c1.r + c2.r, d))\n return 3;\n if (c1.r - c2.r < d)\n return 2;\n if (eq(c1.r - c2.r, d))\n return 1;\n return 0;\n}\n\n//crosspoint\nPoint crosspoint(const Line& l, const Line& m) {\n Real A = cross(m.a - l.a, m.d()), B = cross(l.d(), m.d());\n if (eq(A, 0.0) && eq(B, 0.0))\n return m.a;\n return l.a + l.d() * A / B;\n}\nPoint crosspoint(const Segment& s, const Segment& t) { return crosspoint(Line(s), Line(t)); }\npair<Point, Point> crosspoint(const Circle& c, const Line& l) {\n Point p = projection(l, c.p);\n if (eq(distance(l, c.p), c.r))\n return {p, p};\n Real len = sqrt(c.r * c.r - norm(p - c.p));\n return {p + len * l.d(), p - len * l.d()};\n}\npair<Point, Point> crosspoint(const Circle& c, const Segment& s) {\n Line l(s);\n if (intersect(c, s) == 2)\n return crosspoint(c, l);\n auto ret = crosspoint(c, l);\n if (dot(s.a - ret.first, s.b - ret.first) < 0)\n ret.second = ret.first;\n else\n ret.first = ret.second;\n return ret;\n}\npair<Point, Point> crosspoint(const Circle& c1, const Circle& c2) {\n Real d = distance(c1.p, c2.p);\n Real a = acos((c1.r * c1.r + d * d - c2.r * c2.r) / (2 * c1.r * d));\n Real t = atan2(c2.p.y - c1.p.y, c2.p.x - c1.p.x);\n Point p1 = c1.p + Point(cos(t + a) * c1.r, sin(t + a) * c1.r);\n Point p2 = c1.p + Point(cos(t - a) * c1.r, sin(t - a) * c1.r);\n return {p1, p2};\n}\n\n//tangent\npair<Point, Point> tangent(const Circle& c, const Point& p) {\n return crosspoint(c, Circle(p, sqrt(norm(c.p - p) - c.r * c.r)));\n}\nLines tangent(Circle c1, Circle c2) {\n Lines ret;\n if (c1.r < c2.r)\n swap(c1, c2);\n if (eq(c1.p, c2.p))\n return ret;\n Real g = norm(c1.p - c2.p);\n Point u = (c2.p - c1.p) / sqrt(g);\n Point v = rotate(PI / 2, u);\n for (int s : {-1, 1}) {\n Real h = (c1.r + s * c2.r) / sqrt(g);\n if (eq(1 - h * h, 0)) {\n ret.emplace_back(c1.p + u * c1.r, c1.p + (u + v) * c1.r);\n } else if (1 - h * h > 0) {\n Point uu = u * h, vv = v * sqrt(1 - h * h);\n ret.emplace_back(c1.p + (uu + vv) * c1.r, c2.p - (uu + vv) * c2.r * s);\n ret.emplace_back(c1.p + (uu - vv) * c1.r, c2.p - (uu - vv) * c2.r * s);\n }\n }\n return ret;\n}\n\n//convex\nbool is_convex(const Polygon& P) {\n int N = (int)P.size();\n for (int i = 0; i < N; i++) {\n if (ccw(P[i], P[(i + 1) % N], P[(i + 2) % N]) == -1)\n return false;\n }\n return true;\n}\nPolygon convex_hull(Points P) {\n int N = (int)P.size(), k = 0;\n if (N <= 2)\n return P;\n sort(begin(P), end(P));\n Points ret(2 * N);\n for (int i = 0; i < N; ret[k++] = P[i++]) {\n while (k >= 2 && cross(ret[k - 1] - ret[k - 2], P[i] - ret[k - 1]) < EPS)\n --k;\n }\n for (int i = N - 2, t = k + 1; i >= 0; ret[k++] = P[i--]) {\n while (k >= t && cross(ret[k - 1] - ret[k - 2], P[i] - ret[k - 1]) < EPS)\n --k;\n }\n ret.resize(k - 1);\n return ret;\n}\n\n//other\nenum\n{\n OUT,\n ON,\n IN\n};\n\nint contains(const Polygon& Q, const Point& p) {\n bool in = false;\n int N = (int)Q.size();\n for (int i = 0; i < N; i++) {\n Point a = Q[i] - p, b = Q[(i + 1) % N] - p;\n if (a.y > b.y)\n swap(a, b);\n if (a.y <= 0 && 0 < b.y && cross(a, b) < 0)\n in = !in;\n if (cross(a, b) == 0 && dot(a, b) <= 0)\n return ON;\n }\n return in ? IN : OUT;\n}\n\nReal area(const Polygon& P) {\n Real A = 0;\n for (int i = 0; i < (int)P.size(); i++) {\n A += cross(P[i], P[(i + 1) % P.size()]);\n }\n return A * 0.5;\n}\n\n\nint main() {\n Point A, B, C;\n while (cin >> A >> B >> C, A != B) {\n Segment a{B, C}, b{A, C}, c{A, B};\n Circle Ca, Cb, Cc;\n\n auto check = [&](double len) {\n double angle_A = get_angle(A, B, C);\n Point D{A + rotate(angle_A / 2, B - A)};\n Line d{A, D};\n Ca.p = A + (D - A) / abs(D - A) * len;\n Ca.r = distance(Line{c}, Ca.p);\n if (intersect(Ca, Line{a}) || intersect(a, Segment{A, Ca.p}))\n return false;\n\n\n auto calc_circle = [&](Point A, Point B, Point C, Segment a, Segment b, Segment c, Circle Ca) {\n Circle ret;\n double angle_B = arg((A - B) / (C - B));\n Point E{B + rotate(angle_B / 2, C - B)};\n Line e{B, E};\n auto check2 = [&](double len) {\n ret.p = B + (E - B) / abs(E - B) * len;\n ret.r = distance(Line{a}, ret.p);\n if (intersect(ret, Line{b}) || intersect(b, Segment{B, ret.p}))\n return false;\n return intersect(ret, Ca) >= 3;\n };\n\n double ok = 0;\n double ng = 20000;\n while (abs(ok - ng) > EPS) {\n double mid = (ok + ng) / 2;\n if (check2(mid)) {\n ok = mid;\n } else {\n ng = mid;\n }\n }\n\n return ret;\n };\n\n Cb = calc_circle(A, B, C, a, b, c, Ca);\n Cc = calc_circle(B, C, A, b, c, a, Ca);\n\n return intersect(Cb, Cc) < 3;\n };\n\n double ok = 0;\n double ng = 3000;\n while (abs(ok - ng) > EPS) {\n double mid = (ok + ng) / 2;\n if (check(mid)) {\n ok = mid;\n } else {\n ng = mid;\n }\n }\n cout << fixed << setprecision(6) << Ca.r << \" \" << Cb.r << \" \" << Cc.r << endl;\n }\n}", "accuracy": 1, "time_ms": 650, "memory_kb": 3908, "score_of_the_acc": -1.0732, "final_rank": 16 }, { "submission_id": "aoj_1301_5349532", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nstruct point{\n double x, y;\n point(){\n }\n point(double x, double y): x(x), y(y){\n }\n point operator +(point P){\n return point(x + P.x, y + P.y);\n }\n point operator -(point P){\n return point(x - P.x, y - P.y);\n }\n point operator *(double k){\n return point(x * k, y * k);\n }\n point operator /(double k){\n return point(x / k, y / k);\n }\n};\ndouble abs(point P){\n return sqrt(P.x * P.x + P.y * P.y);\n}\ndouble dist(point P, point Q){\n return abs(Q - P);\n}\ndouble dot(point P, point Q){\n return P.x * Q.x + P.y * Q.y;\n}\ndouble cross(point P, point Q){\n return P.x * Q.y - P.y * Q.x;\n}\ndouble area(point P1, point P2, point P3){\n return abs(cross(P2 - P1, P3 - P1)) / 2;\n}\ndouble angle(point P, point Q, point R){\n return acos(dot(P - Q, R - Q) / abs(P - Q) / abs(R - Q)); \n}\npoint incenter(point P1, point P2, point P3){\n double d1 = dist(P2, P3);\n double d2 = dist(P3, P1);\n double d3 = dist(P1, P2);\n return (P1 * d1 + P2 * d2 + P3 * d3) / (d1 + d2 + d3);\n}\ndouble inscribled_circle(point P1, point P2, point P3){\n return area(P1, P2, P3) * 2 / (dist(P1, P2) + dist(P2, P3) + dist(P3, P1));\n}\nint main(){\n cout << fixed << setprecision(10);\n while (true){\n point P1, P2, P3;\n cin >> P1.x >> P1.y >> P2.x >> P2.y >> P3.x >> P3.y;\n if (P1.x == 0 && P1.y == 0 && P2.x == 0 && P2.y == 0 && P3.x == 0 && P3.y == 0){\n break;\n }\n double t1 = angle(P3, P1, P2), t2 = angle(P1, P2, P3), t3 = angle(P2, P3, P1);\n point I = incenter(P1, P2, P3);\n double r = inscribled_circle(P1, P2, P3);\n double r1, r2, r3;\n double tv = 0, fv = dist(P1, I);\n for (int i = 0; i < 50; i++){\n double mid = (tv + fv) / 2;\n r1 = mid * sin(t1 / 2);\n point C1 = P1 + (I - P1) / abs(I - P1) * mid;\n point C2;\n double tv2 = 0, fv2 = dist(P2, I);\n for (int j = 0; j < 50; j++){\n double mid2 = (tv2 + fv2) / 2;\n r2 = mid2 * sin(t2 / 2);\n C2 = P2 + (I - P2) / abs(I - P2) * mid2;\n if (dist(C1, C2) > r1 + r2){\n tv2 = mid2;\n } else {\n fv2 = mid2;\n }\n }\n point C3;\n double tv3 = 0, fv3 = dist(P3, I);\n for (int j = 0; j < 50; j++){\n double mid3 = (tv3 + fv3) / 2;\n r3 = mid3 * sin(t3 / 2);\n C3 = P3 + (I - P3) / abs(I - P3) * mid3;\n if (dist(C1, C3) > r1 + r3){\n tv3 = mid3;\n } else {\n fv3 = mid3;\n }\n }\n if (dist(C2, C3) < r2 + r3){\n tv = mid;\n } else {\n fv = mid;\n }\n }\n cout << r1 << ' ' << r2 << ' ' << r3 << endl;\n }\n}", "accuracy": 1, "time_ms": 200, "memory_kb": 3532, "score_of_the_acc": -0.5267, "final_rank": 11 }, { "submission_id": "aoj_1301_4891547", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\nusing ll = long long;\nusing ull = unsigned long long;\nusing ld = long double;\nusing P = pair<int,int>;\nusing vi = vector<int>;\nusing vvi = vector<vi>;\nusing vvvi = vector<vvi>;\nusing vll = vector<ll>;\nusing vvll = vector<vll>;\n\n#define REP(i,n) for (int i=0; i<(n); ++i)\n#define RREP(i,n) for (int i=(int)(n)-1; i>=0; --i)\n#define FOR(i,a,n) for (int i=(a); i<(n); ++i)\n#define RFOR(i,a,n) for (int i=(int)(n)-1; i>=(a); --i)\n\n#define SZ(x) ((int)(x).size())\n#define ALL(x) (x).begin(),(x).end()\n\n#define DUMP(x) cerr<<#x<<\" = \"<<(x)<<endl\n#define DEBUG(x) cerr<<#x<<\" = \"<<(x)<<\" (L\"<<__LINE__<<\")\"<<endl;\n\ntemplate<typename T>\nostream& operator<<(ostream& os, const vector<T>& v) {\n os << \"[\";\n REP (i, SZ(v)) {\n if (i) os << \", \";\n os << v[i];\n }\n return os << \"]\";\n}\n\ntemplate<typename T, typename U>\nostream& operator<<(ostream& os, const pair<T, U>& p) {\n return os << \"(\" << p.first << \" \" << p.second << \")\";\n}\n\ntemplate<class T>\nbool chmax(T& a, const T& b) {\n if (a < b) { a = b; return true; }\n return false;\n}\ntemplate<class T>\nbool chmin(T& a, const T& b) {\n if (b < a) { a = b; return true; }\n return false;\n}\n\nconst ll MOD = 1e9+7;\nconst ld eps = 1e-9;\nconst int INF = INT_MAX / 2;\nconst ll LINF = LLONG_MAX / 2;\n\ntemplate<typename T>\nstruct edge {\n int src, to;\n T cost;\n\n friend ostream &operator<<(ostream &os, const edge &e) {\n return os << \"(\" << e.src << \"->\" << e.to << \":\" << e.cost << \")\";\n }\n};\n\ntemplate<typename T>\nusing Graph = vector<vector<edge<T>>>;\n\n\ntemplate<int64_t mod>\nclass modint {\n int64_t x;\n\npublic:\n modint(int64_t x = 0) : x(x < 0 ? ((x % mod) + mod) % mod : x % mod) {}\n\n const modint operator-() const { return x ? mod - x : 0; }\n\n modint &operator+=(const modint &rhs) {\n if ((x += rhs.x) >= mod) x -= mod;\n return *this;\n }\n\n modint &operator-=(const modint &rhs) {\n return *this += -rhs;\n }\n\n modint &operator*=(const modint &rhs) {\n (x *= rhs.x) %= mod;\n return *this;\n }\n\n modint &operator/=(const modint &rhs) {\n return *this *= rhs.pow(mod - 2);\n }\n\n friend const modint operator+(modint lhs, const modint &rhs) {\n return lhs += rhs;\n }\n\n friend const modint operator-(modint lhs, const modint &rhs) {\n return lhs -= rhs;\n }\n\n friend const modint operator*(modint lhs, const modint &rhs) {\n return lhs *= rhs;\n }\n\n friend const modint operator/(modint lhs, const modint &rhs) {\n return lhs /= rhs;\n }\n\n const modint pow(int64_t n) const {\n modint ret = 1, tmp = *this;\n while (n) {\n if (n & 1) ret *= tmp;\n tmp *= tmp;\n n >>= 1;\n }\n return ret;\n }\n\n friend bool operator==(const modint &lhs, const modint &rhs) {\n return lhs.x == rhs.x;\n }\n\n friend bool operator!=(const modint &lhs, const modint &rhs) {\n return !(lhs == rhs);\n }\n\n friend ostream &operator<<(ostream &os, const modint &a) {\n return os << a.x;\n }\n\n friend istream &operator>>(istream &is, modint &a) {\n int64_t tmp;\n is >> tmp;\n a = tmp;\n return is;\n }\n};\n\n\n/**\n * @brief\n * 二次元幾何\n * @author habara-k\n * @date 2020/05/05\n */\n\nusing Real = double;\nconst Real PI = acos(-1);\n\nusing Point = complex<Real>;\nnamespace std {\n bool operator<(const Point &a, const Point &b) {\n if (a.real() == b.real()) return a.imag() < b.imag();\n return a.real() < b.real();\n }\n}\n\nstruct Line {\n Point a, b;\n\n Line() {}\n\n Line(const Point &a, const Point &b) : a(a), b(b) {}\n\n friend ostream &operator<<(ostream &os, const Line &l) {\n return os << \"[\" << l.a << \",\" << l.b << \"]\";\n }\n};\n\nstruct Segment : Line {\n Segment() {}\n\n Segment(const Point &a, const Point &b) : Line(a, b) {}\n};\n\ninline bool eq(Real a, Real b) { return abs(b - a) < eps; }\n\nReal radian_to_degree(Real r) {\n return r * 180.0 / PI;\n}\n\nReal degree_to_radian(Real d) {\n return d * PI / 180.0;\n}\n\nPoint rotate(const Point &p, Real theta) {\n return p * polar((Real) 1.0, theta);\n}\n\nReal cross(const Point &a, const Point &b) {\n return a.real() * b.imag() - a.imag() * b.real();\n}\n\nReal dot(const Point &a, const Point &b) {\n return a.real() * b.real() + a.imag() * b.imag();\n}\n\n\n/**\n* @brief 点p の直線l への射影を求める.\n*/\nPoint projection(const Line &l, const Point &p) {\n Real A = dot(l.b - l.a, p - l.a),\n B = dot(l.a - l.b, p - l.b);\n return (A * l.b + B * l.a) / (A + B);\n}\n\n/**\n* @brief 2直線の並行判定\n*/\nbool parallel(const Line &l1, const Line &l2) {\n return eq(cross(l1.a - l1.b, l2.a - l2.b), 0.0);\n}\n\n/**\n* @brief 2直線の直行判定\n*/\nbool orthogonal(const Line &l1, const Line &l2) {\n return eq(dot(l1.a - l1.b, l2.a - l2.b), 0.0);\n}\n\n\n/**\n* @brief 有向線分と点の位置関係\n* @param[in] a, b, c: 線分a->b, 点c\n* @return 線分a->b からみて, 点c がどこにあるか.\n*/\nconst int COUNTER_CLOCKWISE = 1,\n CLOCKWISE = -1,\n ONLINE_BACK = 2,\n ONLINE_FRONT = -2,\n ON_SEGMENT = 0;\n\nint ccw(const Point &a, Point b, Point c) {\n b = b - a, c = c - a;\n if (cross(b, c) > eps) return COUNTER_CLOCKWISE;\n if (cross(b, c) < -eps) return CLOCKWISE;\n if (dot(b, c) < 0) return ONLINE_BACK;\n if (norm(b) < norm(c)) return ONLINE_FRONT;\n return ON_SEGMENT;\n}\n\n\n/**\n* @brief 直線と点の交差判定\n*/\nbool intersected(const Line &l, const Point &p) {\n return abs(ccw(l.a, l.b, p)) != 1;\n}\n\n/**\n* @brief 線分と点の交差判定\n*/\nbool intersected(const Segment &s, const Point &p) {\n return ccw(s.a, s.b, p) == 0;\n}\n\n/**\n* @brief 直線と線分の交差判定\n*/\nbool intersected(const Line &l, const Segment &s) {\n return cross(l.b - l.a, s.a - l.a) * cross(l.b - l.a, s.b - l.a) < eps;\n}\n\n/**\n* @brief 2つの線分の交差判定\n*/\nbool intersected(const Segment &s1, const Segment &s2) {\n return ccw(s1.a, s1.b, s2.a) * ccw(s1.a, s1.b, s2.b) <= 0 and\n ccw(s2.a, s2.b, s1.a) * ccw(s2.a, s2.b, s1.b) <= 0;\n}\n\n\n/**\n* @brief 2直線の交点\n*/\nPoint crosspoint(const Line &l1, const Line &l2) {\n Real A = cross(l2.a - l1.a, l2.b - l1.a),\n B = cross(l2.b - l1.b, l2.a - l1.b);\n return (A * l1.b + B * l1.a) / (A + B);\n}\n\n\n/**\n* @brief 直線と点の距離\n*/\nReal distance(const Line &l, const Point &p) {\n return abs(p - projection(l, p));\n}\n\n/**\n* @brief 線分と点の距離\n*/\nReal distance(const Segment &s, const Point &p) {\n Point r = projection(s, p);\n if (intersected(s, r)) return abs(r - p);\n return min(abs(s.a - p), abs(s.b - p));\n}\n\n/**\n* @brief 直線と線分の距離\n*/\nReal distance(const Line &l, const Segment &s) {\n if (intersected(l, s)) return 0;\n return min(distance(l, s.a), distance(l, s.b));\n}\n\n/**\n* @brief 2つの線分の距離\n*/\nReal distance(const Segment &s1, const Segment &s2) {\n if (intersected(s1, s2)) return 0.0;\n return min({distance(s1, s2.a), distance(s1, s2.b),\n distance(s2, s1.a), distance(s2, s1.b)});\n}\n\n\nstruct Circle {\n Point p;\n Real r;\n\n Circle() {}\n\n Circle(const Point &p, Real r) : p(p), r(r) {}\n};\n\n\n/**\n* @brief 2つの円の交点の数\n*/\nint intersected(Circle c1, Circle c2) {\n if (c1.r < c2.r) swap(c1, c2);\n Real d = abs(c1.p - c2.p);\n if (c1.r + c2.r < d) return 4;\n if (eq(c1.r + c2.r, d)) return 3;\n if (c1.r - c2.r < d) return 2;\n if (eq(c1.r - c2.r, d)) return 1;\n return 0;\n}\n\n/**\n* @brief 円と直線の交点のペア\n* @details 交差することを確認してから呼ぶこと.\n*/\npair<Point, Point> crosspoint(const Circle &c, const Line &l) {\n Real h = distance(l, c.p);\n Point p = projection(l, c.p);\n if (eq(h, c.r)) return {p, p};\n Point u = l.a - l.b;\n u /= abs(u);\n Real d = sqrt(c.r * c.r - h * h);\n return {p + u * d, p - u * d};\n}\n\n/**\n* @brief 2つの円の交点のペア\n* @details 交差することを確認してから呼ぶこと.\n*/\npair<Point, Point> crosspoint(const Circle &c1, const Circle &c2) {\n Real d = abs(c2.p - c1.p), t = arg(c2.p - c1.p);\n Real a = acos((c1.r * c1.r + d * d - c2.r * c2.r) / (2 * c1.r * d));\n return {c1.p + polar(c1.r, t + a),\n c1.p + polar(c1.r, t - a)};\n}\n\n/**\n* @brief 点p, 円c に対し, pを通るcの接線を返す(c上の2点のペアで返す).\n* @details 点p が円c の外側にあることを確認してから呼ぶこと.\n*/\npair<Point, Point> tangent(const Point &p, const Circle &c) {\n return crosspoint(c, Circle(p, sqrt(norm(p - c.p) - c.r * c.r)));\n};\n\n/**\n* @brief 2つの円に共通する接線を返す(最大4本).\n* @details 点p が円c の外側にあることを確認してから呼ぶこと.\n*/\nvector<Line> common_tangent(const Circle &c1, const Circle &c2) {\n vector<Line> lines;\n Point u = c2.p - c1.p;\n Real d = abs(u);\n if (eq(d, 0.0)) return lines;\n u /= d;\n for (Real s : {-1, 1}) {\n // s = -1: 同じ側に2つの円があるとき.\n // s = 1: 反対側に2つの円があるとき.\n Real h = (c1.r + s * c2.r) / d;\n if (eq(abs(h), 1.0)) {\n // 2つの円が接しているとき.\n lines.emplace_back(\n c1.p + u * h * c1.r,\n c1.p + u * h * c1.r + rotate(u, PI / 2.0));\n } else if (abs(h) < 1) {\n // 2本の接線が引けるとき.\n Real a = acos(h);\n lines.emplace_back(\n c1.p + u * polar(c1.r, a),\n c2.p - s * u * polar(c2.r, a));\n lines.emplace_back(\n c1.p + u * polar(c1.r, -a),\n c2.p - s * u * polar(c2.r, -a));\n }\n }\n return lines;\n}\n\n\nbool solve() {\n vector<Point> ps(3);\n vector<Line> ls(3), cs(3);\n REP(i, 3) {\n Real x, y; cin >> x >> y;\n ps[i] = {x, y};\n }\n if (abs(ps[0]) + abs(ps[1]) + abs(ps[2]) < eps) return false;\n REP(i, 3) {\n ls[i] = {ps[(i+1) % 3], ps[(i+2) % 3]};\n Point a = ps[(i+1) % 3] - ps[i];\n Point b = ps[(i+2) % 3] - ps[i];\n a /= abs(a);\n b /= abs(b);\n Point d = a + b;\n cs[i] = {ps[i], ps[i] + d};\n }\n\n Point c = crosspoint(cs[0], cs[1]);\n\n Real r0, r1, r2;\n auto check = [&](Point q0) {\n r0 = distance(ls[1], q0);\n\n Point p1, p2;\n {\n auto check1 = [&](Point q1) {\n r1 = distance(ls[0], q1);\n return abs(q0 - q1) > r0 + r1;\n };\n Point lb = ps[1], ub = c;\n while (abs(ub - lb) > eps) {\n Point mid = (ub + lb) / 2.0;\n (check1(mid) ? lb : ub) = mid;\n }\n p1 = lb;\n }\n {\n auto check2 = [&](Point q2) {\n r2 = distance(ls[0], q2);\n return abs(q0 - q2) > r0 + r2;\n };\n Point lb = ps[2], ub = c;\n while (abs(ub - lb) > eps) {\n Point mid = (ub + lb) / 2.0;\n (check2(mid) ? lb : ub) = mid;\n }\n p2 = lb;\n }\n\n return abs(p1 - p2) < r1 + r2;\n\n };\n\n Point lb = ps[0], ub = c;\n while (abs(ub - lb) > eps) {\n Point mid = (ub + lb) / 2.0;\n (check(mid) ? lb : ub) = mid;\n }\n\n cout << r0 << ' ' << r1 << ' ' << r2 << endl;\n\n return true;\n}\n\nint main() {\n cin.tie(0);\n ios::sync_with_stdio(false);\n cout << fixed << setprecision(10);\n\n while (solve()) {}\n\n return 0;\n}", "accuracy": 1, "time_ms": 340, "memory_kb": 3384, "score_of_the_acc": -0.3605, "final_rank": 4 }, { "submission_id": "aoj_1301_4806757", "code_snippet": "#include <bits/stdc++.h>\n\nusing namespace std;\n\n#define SZ(x) (int)(x.size())\n#define REP(i, n) for(int i=0;i<(n);++i)\n#define FOR(i, a, b) for(int i=(a);i<(b);++i)\n#define RREP(i, n) for(int i=(int)(n);i>=0;--i)\n#define RFOR(i, a, b) for(int i=(int)(a);i>=(int)(b);--i)\n#define ALL(a) (a).begin(),(a).end()\n#define DUMP(x) cerr<<#x<<\" = \"<<(x)<<endl\n#define DEBUG(x) cerr<<#x<<\" = \"<<(x)<<\" (L\"<<__LINE__<<\")\"<< endl;\n#define double long double\n\nusing ll = long long;\nusing vi = vector<int>;\nusing vvi = vector<vi>;\nusing vll = vector<ll>;\nusing vvll = vector<vll>;\nusing P = pair<int, int>;\n\nconst double eps = 1e-8;\nconst ll MOD = 1000000007;\nconst int INF = INT_MAX / 2;\nconst ll LINF = LLONG_MAX / 2;\n\ntemplate <typename T1, typename T2>\nbool chmax(T1 &a, const T2 &b) {\n if(a < b) {a = b; return true;}\n return false;\n}\n\ntemplate <typename T1, typename T2>\nbool chmin(T1 &a, const T2 &b) {\n if(a > b) {a = b; return true;}\n return false;\n}\n\ntemplate<typename T1, typename T2>\nostream& operator<<(ostream &os, const pair<T1, T2> p) {\n os << p.first << \":\" << p.second;\n return os;\n}\n\ntemplate<class T>\nostream &operator<<(ostream &os, const vector<T> &v) {\n REP(i, SZ(v)) {\n if(i) os << \" \";\n os << v[i];\n }\n return os;\n}\n\ndouble len(double r1, double r2, double t1, double t2) {\n double val1 = r1 * abs(tan(acos(-1) / 2.0 - t1 / 2.0));\n double val2 = r2 * abs(tan(acos(-1) / 2.0 - t2 / 2.0));\n double val3 = 2 * sqrt(r1 * r2);\n return val1 + val2 + val3;\n}\n\nbool solve() {\n vector<double> x(3), y(3);\n bool end = true;\n REP(i, 3) {\n int u, v; cin >> u >> v;\n end &= u == 0 && v == 0;\n x[i] = u;\n y[i] = v;\n }\n if(end) return false;\n\n double t1 = abs(arg(complex<double>(x[1] - x[0], y[1] - y[0])) - arg(complex<double>(x[2] - x[0], y[2] - y[0])));\n double t2 = abs(arg(complex<double>(x[2] - x[1], y[2] - y[1])) - arg(complex<double>(x[0] - x[1], y[0] - y[1])));\n double t3 = abs(arg(complex<double>(x[0] - x[2], y[0] - y[2])) - arg(complex<double>(x[1] - x[2], y[1] - y[2])));\n chmin(t1, 2 * acos(-1) - t1);\n chmin(t2, 2 * acos(-1) - t2);\n chmin(t3, 2 * acos(-1) - t3);\n double len1 = abs(complex<double>(x[0], y[0]) - complex<double>(x[1], y[1]));\n double len2 = abs(complex<double>(x[1], y[1]) - complex<double>(x[2], y[2]));\n double len3 = abs(complex<double>(x[2], y[2]) - complex<double>(x[0], y[0]));\n\n\n double l1 = 0, r1 = 100000;\n REP(_, 60) {\n double mid1 = (l1 + r1) / 2.0;\n\n double l2 = 0, r2 = 100000;\n REP(_, 60) {\n double mid2 = (l2 + r2) / 2.0;\n double l = len(mid1, mid2, t1, t2);\n if(l < len1) {\n l2 = mid2;\n } else {\n r2 = mid2;\n }\n }\n\n double l3 = 0, r3 = 100000;\n REP(_, 60) {\n double mid3 = (l3 + r3) / 2.0;\n double l = len(mid1, mid3, t1, t3);\n if(l < len3) {\n l3 = mid3;\n } else {\n r3 = mid3;\n }\n }\n\n double l = len(l2, l3, t2, t3);\n if(l < len2) {\n r1 = mid1;\n } else {\n l1 = mid1;\n }\n }\n\n double l2 = 0, r2 = 100000;\n REP(_, 60) {\n double mid2 = (l2 + r2) / 2.0;\n double l = len(l1, mid2, t1, t2);\n if(l < len1) {\n l2 = mid2;\n } else {\n r2 = mid2;\n }\n }\n\n double l3 = 0, r3 = 100000;\n REP(_, 60) {\n double mid3 = (l3 + r3) / 2.0;\n double l = len(l1, mid3, t1, t3);\n if(l < len3) {\n l3 = mid3;\n } else {\n r3 = mid3;\n }\n }\n\n cout << l1 << \" \" << l2 << \" \" << l3 << endl;\n\n\n return true;\n}\n\nint main() {\n cin.tie(0);\n ios::sync_with_stdio(false);\n cout << fixed << setprecision(10);\n\n while(solve()) {}\n\n return 0;\n\n}", "accuracy": 1, "time_ms": 3420, "memory_kb": 3596, "score_of_the_acc": -1.1041, "final_rank": 17 }, { "submission_id": "aoj_1301_3711008", "code_snippet": "#include<iostream>\n#include<string>\n#include<cstdio>\n#include<vector>\n#include<cmath>\n#include<algorithm>\n#include<functional>\n#include<iomanip>\n#include<queue>\n#include<ciso646>\n#include<utility>\n#include<stack>\n#include<bitset>\n#include<numeric>\nusing namespace std;\ntypedef long long ll;\n#define rep(i,n) for(int i=0;i<n;i++)\n#define rep1(i,n) for(int i=1;i<=n;i++)\n#define Rep(i,sta,n) for(int i=sta;i<n;i++)\n#define stop char nyaa;cin>>nyaa;\nconst ll mod = 1000000007;\nconst long double eps = 1e-8;\ntypedef pair<int, int> P;\n\ntypedef long double ld;\n\n#include<complex>\ntypedef complex<ld> Point;\nconst ld pi = acos(-1.0);\nld dot(Point a, Point b) { return real(conj(a)*b); }\nld cross(Point a, Point b) { return imag(conj(a)*b); }\nnamespace std {\n\tbool operator<(const Point &lhs, const Point &rhs) {\n\t\treturn lhs.real() == rhs.real() ? lhs.imag() < rhs.imag() : lhs.real() < rhs.real();\n\t}\n}\nstruct Line {\n\tPoint a, b;\n};\nstruct Circle {\n\tPoint p; ld r;\n};\nint ccw(Point a, Point b, Point c) {\n\tb -= a; c -= a;\n\tif (cross(b, c) > eps)return 1;//counter clockwise\n\tif (cross(b, c) < -eps)return -1;//clock wise\n\tif (dot(b, c) < 0)return 2;//c--a--b on line\n\tif (norm(b) < norm(c))return -2;//a--b--c on line\n\treturn 0; //a--c--b on line\n}\nPoint proj(Line l, Point p) {\n\tld t = dot(p - l.a, l.a - l.b) / norm(l.a - l.b);\n\treturn l.a + t * (l.a - l.b);\n}\nld dist_lp(Line l, Point p) {\n\treturn abs(p - proj(l, p));\n}\nbool isis_ss(Line s, Line t) {\n\treturn(cross(s.b - s.a, t.a - s.a)*cross(s.b - s.a, t.b - s.a) < -eps && cross(t.b - t.a, s.a - t.a)*cross(t.b - t.a, s.b - t.a) < -eps);\n}\nvector<Point> is_cc(Circle c1, Circle c2) {\n\tvector<Point> res;\n\tld d = abs(c1.p - c2.p);\n\tld rc = (d*d + c1.r*c1.r - c2.r*c2.r) / (2 * d);\n\tld dfr = c1.r*c1.r - rc * rc;\n\tif (abs(dfr) < eps)dfr = 0.0;\n\tif (dfr < 0.0)return res;\n\tld rs = sqrt(dfr);\n\tPoint diff = (c2.p - c1.p) / d;\n\tres.push_back(c1.p + diff * Point(rc, rs));\n\tif (dfr != 0.0)res.push_back(c1.p + diff * Point(rc, -rs));\n\treturn res;\n}\nvector<Point> is_lc(Circle c, Line l) {\n\tvector<Point> res;\n\tld d = dist_lp(l, c.p);\n\tif (d > c.r + eps)return res;\n\tld len = (d > c.r) ? 0.0 : sqrt(c.r*c.r - d * d);\n\tPoint nor = (l.a - l.b) / abs(l.a - l.b);\n\tres.push_back(proj(l, c.p) + len * nor);\n\tres.push_back(proj(l, c.p) - len * nor);\n\treturn res;\n}\nvector<Point> is_sc(Circle c, Line s) {\n\tvector<Point> res, cop; cop = is_lc(c, s);\n\tint len = cop.size();\n\trep(k, len) {\n\t\tif (ccw(s.a, cop[k], s.b) == -2) {\n\t\t\tres.push_back(cop[k]);\n\t\t}\n\t}\n\treturn res;\n}\nbool isis_sp(Line s, Point p) {\n\treturn (abs(s.a - p) + abs(s.b - p) - abs(s.b - s.a) < eps);\n}\nld dist_sp(Line s, Point p) {\n\tPoint r = proj(s, p);\n\treturn isis_sp(s, r) ? abs(p - r) : min(abs(p - s.a), abs(p - s.b));\n}\n\nPoint p[3];\nld dist(int i, int j) {\n\tld dx = real(p[j]) - real(p[i]), dy = imag(p[j]) - imag(p[i]);\n\treturn sqrt(dx*dx + dy * dy);\n}\nld calc_cos(ld d1, ld d2, ld d3) {\n\treturn (d1*d1 + d2 * d2 - d3 * d3) / (2 * d1*d2);\n}\n\n\nvoid solve() {\n\tld thec[3];\n\tthec[0] = calc_cos(dist(0, 1), dist(0, 2), dist(1, 2));\n\tthec[1] = calc_cos(dist(1, 0), dist(1, 2), dist(0, 2));\n\tthec[2] = calc_cos(dist(2, 1), dist(0, 2), dist(1, 0));\n\trep(i, 3) {\n\t\tthec[i] = acos(thec[i]);\n\t}\n\tPoint d[3];\n\trep(i, 3) {\n\t\tint j = (i + 1) % 3, k = (i + 2) % 3;\n\t\tPoint l = p[j] - p[i], r = p[k] - p[i];\n\t\tl = l / abs(l); r = r / abs(r);\n\t\tPoint mid = l + r;\n\t\tmid = mid / abs(mid);\n\t\td[i] = mid;\n\t}\n\tCircle c1, c2, c3;\n\tld le = 0, ri = 3000;\n\trep(aa, 50) {\n\t\tld mid = (le + ri)/2.0;\n\t\tc1.p = p[0] + mid * d[0];\n\t\tc1.r = mid * sin(thec[0] / 2.0);\n\t\tif (isis_ss({ p[1],p[2] }, { p[0],c1.p })) {\n\t\t\tri = mid; continue;\n\t\t}\n\t\tif (dist_sp({ p[1],p[2] }, c1.p)<c1.r) {\n\t\t\tri = mid; continue;\n\t\t}\n\t\t//cout << \"hello\" << endl;\n\t\tbool f = false;\n\t\tld cle = 0, cri = 3000;\n\t\trep(bb, 50) {\n\t\t\tld cmid = (cle + cri) / 2.0;\n\t\t\tc2.p = p[1] + cmid * d[1];\n\t\t\tc2.r = cmid * sin(thec[1] / 2.0);\n\t\t\tf = true;\n\t\t\tif (isis_ss({ p[0],p[2] }, { p[1],c2.p })) {\n\t\t\t\tcri = cmid; continue;\n\t\t\t}\n\t\t\tif (dist_sp({ p[0],p[2] }, c2.p) < c2.r) {\n\t\t\t\tcri = cmid; continue;\n\t\t\t}\n\t\t\tf = false;\n\t\t\tif (is_cc(c1, c2).size() > 0) {\n\t\t\t\tcri = cmid;\n\t\t\t}\n\t\t\telse cle = cmid;\n\t\t}\n\t\tif (f) {\n\t\t\tle = mid; continue;\n\t\t}\n\t\tf = false;\n\t\tcle = 0, cri = 3000;\n\t\trep(bb, 50) {\n\t\t\tld cmid = (cle + cri) / 2.0;\n\t\t\tc3.p = p[2] + cmid * d[2];\n\t\t\tc3.r = cmid * sin(thec[2] / 2.0); \n\t\t\tf = true;\n\t\t\tif (isis_ss({ p[0],p[1] }, { p[2],c3.p })) {\n\t\t\t\tcri = cmid; continue;\n\t\t\t}\n\t\t\tif (dist_sp({ p[1],p[0] }, c3.p)<c3.r) {\n\t\t\t\tcri = cmid; continue;\n\t\t\t}\n\t\t\tf = false;\n\t\t\tif (is_cc(c1, c3).size() > 0) {\n\t\t\t\tcri = cmid;\n\t\t\t}\n\t\t\telse cle = cmid;\n\t\t}\n\t\tif (f) {\n\t\t\tle = mid; continue;\n\t\t}\n\t\t//cout << c1.r<<\" \"<<c2.r << \" \" << c3.r << endl;\n\t\tif (is_cc(c2, c3).size() > 0) {\n\t\t\tle = mid;\n\t\t}\n\t\telse {\n\t\t\tri = mid;\n\t\t}\n\t}\n\tcout << c1.r << \" \" << c2.r << \" \" << c3.r << endl;\n}\nsigned main() {\n\tcin.tie(0);\n\tios::sync_with_stdio(false);\n\tcout << fixed << setprecision(10);\n\twhile (true) {\n\t\tbool f = true;\n\t\trep(i, 3) {\n\t\t\tld x, y; cin >> x >> y;\n\t\t\tp[i] = { x,y };\n\t\t\tif (x || y)f = false;\n\t\t}\n\t\tif (f)break;\n\t\tsolve();\n\t}\n\t//stop\n\t\treturn 0;\n}", "accuracy": 1, "time_ms": 5340, "memory_kb": 3552, "score_of_the_acc": -1.3441, "final_rank": 20 }, { "submission_id": "aoj_1301_3574793", "code_snippet": "#include <bits/stdc++.h>\n#define rep(i,n) for(int i = 0; i < n; i++)\nusing namespace std;\ntypedef long long ll;\ntypedef pair<ll,ll> P;\ntypedef complex<double> C;\n \nconst double EPS = 1e-10;\n#define EQ(a,b) (abs((a)-(b)) < EPS)\n#define EQV(a,b) ( EQ((a).real(), (b).real()) && EQ((a).imag(), (b).imag()) )\nconst ll INF = 100000000;\nconst ll MOD = 1000000007;\nC p[3];\n\ndouble cross(C a, C b) {\n return (a.real() * b.imag() - a.imag() * b.real());\n}\n\nC intersection_l(C a1, C a2, C b1, C b2) {\n C a = a2 - a1; C b = b2 - b1;\n return a1 + a * cross(b, b1-a1) / cross(b, a);\n}\n\nvoid solve() {\n double l = min(abs(p[0]-p[1]), abs(p[0]-p[2]));\n\n double lef = 0.0, rig = l;\n C r1, r2, r3;\n double d1, d2, d3;\n rep(_,100) {\n \n double mid = (lef + rig) / 2.0;\n C mp1 = p[0] + (p[1] - p[0]) / abs(p[1]-p[0]) * mid;\n C mp2 = p[0] + (p[2] - p[0]) / abs(p[2]-p[0]) * mid;\n C mp1t = mp1 + (p[1]-p[0])*C(0,1);\n C mp2t = mp2 + (p[2]-p[0])*C(0,1);\n r1 = intersection_l(mp1, mp1t, mp2, mp2t);\n d1 = abs(r1 - mp1);\n\n double l_2 = min(abs(p[1]-p[0]), abs(p[1]-p[2]));\n double lef_2 = 0.0, rig_2 = l_2;\n rep(__,100) {\n double mid_2 = (lef_2 + rig_2) / 2.0;\n C mp1_2 = p[1] + (p[0]-p[1]) / abs(p[0]-p[1]) * mid_2;\n C mp2_2 = p[1] + (p[2]-p[1]) / abs(p[2]-p[1]) * mid_2;\n C mp1t_2 = mp1_2 + (p[0]-p[1])*C(0,1);\n C mp2t_2 = mp2_2 + (p[2]-p[1])*C(0,1);\n r2 = intersection_l(mp1_2, mp1t_2, mp2_2, mp2t_2);\n d2 = abs(r2 - mp1_2);\n if (abs(r1-r2) < d1+d2) {\n rig_2 = mid_2;\n } else {\n lef_2 = mid_2;\n }\n }\n\n l_2 = min(abs(p[2]-p[0]), abs(p[2]-p[1]));\n lef_2 = 0.0, rig_2 = l_2;\n rep(__,100) {\n double mid_2 = (lef_2 + rig_2) / 2.0;\n C mp1_2 = p[2] + (p[0]-p[2]) / abs(p[0]-p[2]) * mid_2;\n C mp2_2 = p[2] + (p[1]-p[2]) / abs(p[1]-p[2]) * mid_2;\n C mp1t_2 = mp1_2 + (p[0]-p[2])*C(0,1);\n C mp2t_2 = mp2_2 + (p[1]-p[2])*C(0,1);\n r3 = intersection_l(mp1_2, mp1t_2, mp2_2, mp2t_2);\n d3 = abs(r3 - mp1_2);\n if (abs(r1-r3) < d1+d3) {\n rig_2 = mid_2;\n } else {\n lef_2 = mid_2;\n }\n }\n if (abs(r2-r3) < d2+d3) {\n lef = mid;\n } else {\n rig = mid;\n }\n }\n printf(\"%.9f %.9f %.9f\\n\", d1, d2, d3);\n}\n\nint main() {\n int _x[3], _y[3];\n while (cin >> _x[0] >> _y[0] >> _x[1] >> _y[1] >> _x[2] >> _y[2]) {\n bool end = true;\n rep(i,3) {\n if (_x[i] || _y[i]) end = false;\n }\n if (end) break;\n rep(i,3) p[i] = C(_x[i], _y[i]);\n solve();\n }\n}", "accuracy": 1, "time_ms": 3310, "memory_kb": 3140, "score_of_the_acc": -0.5085, "final_rank": 10 }, { "submission_id": "aoj_1301_2926557", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\n#define repl(i,a,b) for(int i=(int)(a);i<(int)(b);i++)\n#define rep(i,n) repl(i,0,n)\n#define all(x) (x).begin(),(x).end()\n#define dbg(x) cout<<#x\"=\"<<x<<endl\n#define mmax(x,y) (x>y?x:y)\n#define mmin(x,y) (x<y?x:y)\n#define maxch(x,y) x=mmax(x,y)\n#define minch(x,y) x=mmin(x,y)\n#define uni(x) x.erase(unique(all(x)),x.end())\n#define exist(x,y) (find(all(x),y)!=x.end())\n#define bcnt __builtin_popcount\n\ntypedef long double D;\ntypedef complex<D> P;\n\n#define X real()\n#define Y imag()\n\nconst D eps=1e-8;\nconst D inf=1e12;\nconst D PI=acos(-1);\n\nD cross(P a,P b){ return (conj(a)*b).Y; }\nstruct L : public vector<P> { // line and segment\n L(const P& a,const P &b){\n push_back(a);\n push_back(b);\n }\n};\nP projection(L l,P p){\n P b=l[1]-l[0],c=p-l[0];\n return l[0]+b*(c/b).X;\n}\nD distanceLP(L l,P p) {\n return abs(p-projection(l,p));\n}\nP crosspoint(L l,L m) {\n D A=cross(l[1]-l[0],m[1]-m[0]);\n D B=cross(l[1]-l[0],l[1]-m[0]);\n if (abs(A)<eps&&abs(B)<eps) return m[0]; // same line\n if (abs(A)<eps) assert(false); // !!!PRECONDITION NOT SATISFIED!!!\n return m[0]+B/A*(m[1]-m[0]);\n}\n\nP p[3],q[3],e[3],x[3];\nD r[3];\nP g;\nP cp;\n\nbool ok(){\n r[0]=distanceLP(L(p[0],p[1]),x[0]);\n repl(i,1,3){\n D lb=0,ub=abs(cp-p[i]);\n rep(hoge,80){\n D mid=(ub+lb)/2.0;\n x[i]=p[i]+e[i]*mid;\n r[i]=distanceLP(L(p[i],p[(i+1)%3]),x[i]);\n D d0i=abs(x[i]-x[0]);\n if(d0i>r[0]+r[i])lb=mid;\n else ub=mid;\n }\n }\n D d12=abs(x[1]-x[2]);\n return d12>r[1]+r[2];\n}\n\nint main(){\n while(1){\n bool done=true;\n rep(i,3){\n int x,y;\n cin>>x>>y;\n if(x!=0||y!=0)done=false;\n p[i]=P(x,y);\n }\n if(done)break;\n\n D absA=abs(p[2]-p[1]); D absB=abs(p[2]-p[0]); D absC=abs(p[1]-p[0]);\n q[0]=(p[1]*absB+p[2]*absC)/(absB+absC);\n q[1]=(p[0]*absA+p[2]*absC)/(absA+absC);\n q[2]=(p[0]*absA+p[1]*absB)/(absA+absB);\n cp=crosspoint(L(p[1],q[1]),L(p[2],q[2]));\n rep(i,3)e[i]=(q[i]-p[i])/abs(q[i]-p[i]);\n D lb=0,ub=abs(cp-p[0]);\n rep(hoge,80){\n D mid=(lb+ub)/2.0;\n x[0]=p[0]+e[0]*mid;\n if(ok())ub=mid;\n else lb=mid;\n }\n printf(\"%.5Lf %.5Lf %.5Lf\\n\", r[0], r[1], r[2]);\n }\n return 0;\n}", "accuracy": 1, "time_ms": 6500, "memory_kb": 3256, "score_of_the_acc": -1.1472, "final_rank": 18 }, { "submission_id": "aoj_1301_2899956", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nusing ld = long double;\nusing P = pair<ld, ld>;\ninline ld dist(const P& p1, const P& p2) { return hypot(p1.first - p2.first, p1.second - p2.second); }\nint main()\n{\n while (true) {\n P p1, p2, p3;\n cin >> p1.first >> p1.second >> p2.first >> p2.second >> p3.first >> p3.second;\n if (p1 == P{0, 0} and p2 == P{0, 0} and p3 == P{0, 0}) { break; }\n const ld a = dist(p1, p2), b = dist(p2, p3), c = dist(p3, p1), s = (a + b + c) / 2;\n const ld r = sqrt((s - a) * (s - b) * (s - c) / s);\n const ld d = hypot(s - a, r), e = hypot(s - b, r), f = hypot(s - c, r);\n const ld r1 = r / 2 / (s - a) * (s + d - r - e - f), r2 = r / 2 / (s - b) * (s + e - r - d - f), r3 = r / 2 / (s - c) * (s + f - r - d - e);\n cout << fixed << setprecision(15) << r2 << \" \" << r3 << \" \" << r1 << endl;\n }\n return 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3240, "score_of_the_acc": -0.1269, "final_rank": 1 }, { "submission_id": "aoj_1301_2845681", "code_snippet": "#include <iostream>\n#include <math.h>\n#include <cmath>\n#include <iomanip>\nusing namespace std;\n\nlong double x[3], y[3], l[3], a[3], r[3];\n\t\nbool check() {\n\tfor (int i = 0; i < 3; i++) {\n\t\tif (x[i] != 0) {\n\t\t\treturn true;\n\t\t}\n\t\tif (y[i] != 0) {\n\t\t\treturn true;\n\t\t}\n\t}\n\treturn false;\n}\n\n\nint main() {\n\tcout << fixed;\n\tcout << setprecision(6);\n\n\tint tmp;\n\tbool first = true;\n\tlong double value;\n\tlong double opp, hyp, adj;\n\tlong double dist;\n\tlong double diffx[2], diffy[2];\n\tlong double dirx, diry;\n\n\tfor (int i = 0; i < 3; i++) {\n\t\tcin >> x[i];\n\t\tcin >> y[i];\n\t}\n\n\tint itr = 0;\n\twhile (check()) {\n\t\t//cout << itr++ << endl;\n\t\tfor (int i = 0; i < 3; i++) {\n\t\t\ttmp = (i+1) % 3;\n\n\t\t\t//cout << pow(x[i] - x[tmp], 2) << \" \" << pow(y[i] - y[tmp], 2) << endl;\n\t\t\tl[i] = sqrtl(pow(x[i] - x[tmp], 2) + pow(y[i] - y[tmp], 2));\n\t\t}\n\n\t\tfor (int i = 0; i < 3; i++) {\n\t\t\t/*\n\t\t\thyp = 2*l[i]*l[(i+2)%3];\n\t\t\tadj = pow(l[i], 2) + pow(l[(i+2)%3], 2) - pow(l[(i+1)%3], 2);\n\t\t\topp = sqrtl(pow(hyp,2)-pow(adj,2));\n\n\t\t\ta[i] = (hyp - adj) / opp;\n\t\t\t*/\n\n\t\t\ta[i] = pow(l[i],2) + pow(l[(i+2)%3],2) - pow(l[(i+1)%3],2);\n\t\t\ta[i] /= 2*l[i]*l[(i+2)%3];\n\t\t\ta[i] = acos(a[i]);\n\t\t\ta[i] = tan(a[i]/2);\n\t\t\t//cout << a[i] << endl;\n\t\t}\n\n\n\t\tlong double diff = max(l[0],l[2]);\n\t\tlong double diff2;\n\t\tr[0] = 0.1;\n\n\t\twhile (diff > 1e-10) {\n\t\t\tr[0] += diff;\n\t\t\t//cout << \"START: \" << r[0] << endl;\n\n\t\t\tr[1] = 0.1;\n\t\t\tdiff2 = l[0];\n\t\t\twhile (diff2 > 1e-10) {\n\t\t\t\tr[1] += diff2;\n\n\t\t\t\tif (r[0]/a[0] + 2*sqrtl(r[0]*r[1]) + r[1]/a[1] > l[0]) {\n\t\t\t\t\tr[1] -= diff2;\n\t\t\t\t\tdiff2 /= 2;\n\t\t\t\t}\n\t\t\t} \n\n\t\t\tr[2] = 0.1;\n\t\t\tdiff2 = l[2];\n\t\t\twhile (diff2 > 1e-10) {\n\t\t\t\tr[2] += diff2;\n\n\t\t\t\tif (r[0]/a[0] + 2*sqrtl(r[0]*r[2]) + r[2]/a[2] > l[2]) {\n\t\t\t\t\tr[2] -= diff2;\n\t\t\t\t\tdiff2 /= 2;\n\t\t\t\t}\n\t\t\t} \n\t\t\t\n\t\t\tvalue = r[1]/a[1] + 2*sqrtl(r[1]*r[2]) + r[2]/a[2];\n\n\t\t\tif (value < l[1]) {\n\t\t\t\tr[0] -= diff;\n\t\t\t\tdiff /= 2;\n\t\t\t} \n\t\t}\n\n\t\tif (first) {\n\t\t\tfirst = false;\n\t\t} else {\n\t\t\tcout << endl;\n\t\t}\n\n\t\tcout << r[0];\n\t\tfor (int i = 1; i < 3; i++) {\n\t\t\tcout << \" \" << r[i];\n\t\t}\n\n\t\tfor (int i = 0; i < 3; i++) {\n\t\t\tcin >> x[i];\n\t\t\tcin >> y[i];\n\t\t}\n\t}\n\n\treturn 0;\n}", "accuracy": 1, "time_ms": 310, "memory_kb": 3412, "score_of_the_acc": -0.3914, "final_rank": 6 } ]
aoj_1305_cpp
Problem A: Membership Management Peter is a senior manager of Agile Change Management (ACM) Inc., where each employee is a member of one or more task groups. Since ACM is agile, task groups are often reorganized and their members frequently change, so membership management is his constant headache. Peter updates the membership information whenever any changes occur: for instance, the following line written by him means that Carol and Alice are the members of the Design Group. design:carol,alice. The name preceding the colon is the group name and the names following it specify its members. A smaller task group may be included in a larger one. So, a group name can appear as a member of another group, for instance, as follows. development:alice,bob,design,eve. Simply unfolding the design above gives the following membership specification, which is equivalent to the original. development:alice,bob,carol,alice,eve. In this case, however, alice occurs twice. After removing one of the duplicates, we have the following more concise specification. development:alice,bob,carol,eve. Your mission in this problem is to write a program that, given group specifications, identifies group members. Note that Peter's specifications can include deeply nested groups. In the following, for instance, the group one contains a single member dave. one:another. another:yetanother. yetanother:dave. Input The input is a sequence of datasets, each being in the following format. n group 1 : member 1,1 ,..., member 1, m 1 . . . . group i : member i ,1 ,..., member i , m i . . . . group n : member n ,1 ,..., member n , m n . The first line contains n, which represents the number of groups and is a positive integer no more than 100. Each of the following n lines contains the membership information of a group: group i (1 ≤ i ≤ n ) is the name of the i -th task group and is followed by a colon ( : ) and then the list of its m i member s that are delimited by a comma ( , ) and terminated by a period ( . ). Those group names are mutually different. Each m i (1 ≤ i ≤ n ) is between 1 and 10, inclusive. A member is another group name if it is one of group 1 , group 2 ,..., or groupn. Otherwise it is an employee name. There are no circular (or recursive) definitions of group(s). You may assume that m i member names of a group are mutually different. Each group or employee name is a non-empty character string of length between 1 and 15, inclusive, and consists of lowercase letters. The end of the input is indicated by a line containing a zero. Output For each dataset, output the number of employees included in the first group of the dataset, that is group 1 , in a line. No extra characters should occur in the output. Sample Input 2 development:alice,bob,design,eve. design:carol,alice. 3 one:another. another:yetanother. yetanother:dave. 3 friends:alice,bob,bestfriends,carol,fran,badcompany. bestfriends:eve,alice. badcompany:dave,carol. 5 a:b,c,d,e. b:c,d,e,f. c:d,e,f,g. d:e,f,g,h. e:f,g,h, ...(truncated)
[ { "submission_id": "aoj_1305_10739683", "code_snippet": "#include<stdio.h>\n#include<string.h>\n#include<string>\n#include<vector>\n#include<stdlib.h>\n#include<algorithm>\n#include<iostream>\nusing namespace std;\n\nvector<string> q[111];\nint n;\nchar s[300];\n\nint main()\n{\n //freopen(\"x.in\",\"r\",stdin);\n while(scanf(\"%d\",&n)&&n)\n {\n int i,j,k;\n scanf(\"%s\",s);\n string t;\n q[0].clear();\n for(i=0;s[i]!=':';i++);\n for(i+=1;s[i];i++)\n {\n if(s[i]==','||s[i]=='.')\n {\n //cout<<t<<endl;\n q[0].push_back(t);\n t=\"\";\n }\n else t+=s[i];\n }\n //if(t!=\"\") q[0].push_back(t);\n //cout<<endl;\n for(i=1;i<n;i++)\n {\n scanf(\"%s\",s);t=\"\";q[i].clear();\n for(j=0;s[j]!=':';j++) t+=s[j];\n //cout<<t<<endl;\n q[i].push_back(t);t=\"\";\n for(j+=1;s[j];j++)\n {\n if(s[j]==','||s[j]=='.')\n {\n //cout<<t<<endl;\n q[i].push_back(t);t=\"\";\n }\n else t+=s[j];\n }\n //if(t!=\"\") q[i].push_back(t);\n //cout<<t<<endl<<endl;\n }\n sort(q[0].begin(),q[0].end());\n for(i=1;i<q[0].size();)\n {\n if(q[0][i]==q[0][i-1])\n {\n swap(q[0][i],q[0][q[0].size()-1]);\n q[0].pop_back();\n }\n else i++;\n }\n //vector<string>::iterator it;\n bool ok=true;\n while(ok)\n {\n ok=false;\n for(i=0;i<q[0].size();i++)\n {\n for(j=1;j<n;j++)\n {\n if(q[0][i]==q[j][0])\n {\n for(k=1;k<q[j].size();k++)\n {\n q[0].push_back(q[j][k]);\n }\n ok=true;\n swap(q[0][i],q[0][q[0].size()-1]);\n q[0].pop_back();\n sort(q[0].begin(),q[0].end());\n for(j=1;j<q[0].size();)\n {\n if(q[0][j]==q[0][j-1])\n {\n q[0].erase(q[0].begin()+j);\n }\n else j++;\n }\n }\n }\n }\n }\n printf(\"%d\\n\",q[0].size());\n }\n return 0;\n}", "accuracy": 1, "time_ms": 360, "memory_kb": 3456, "score_of_the_acc": -1.0549, "final_rank": 20 }, { "submission_id": "aoj_1305_10035727", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\n#define all(a) a.begin(),a.end()\n#define reps(i, a, n) for (int i = (a); i < (int)(n); i++)\n#define rep(i, n) reps(i, 0, n)\n#define rreps(i, a, n) for (int i = (a); i > (int)(n); i--)\nconst long long mod = 1000000007;\nconst long long INF = 1e18;\nll myceil(ll a, ll b) {return (a+b-1)/b;}\n\n\nint n;\nmap<string,set<string>> group;\nvector<string> qwe(150);\nset<string> opened;\n\n\nvoid open(string key) {\n for (auto s : group[key]) {\n if (group[s].size() != 0) {\n if (opened.count(s) == 0) {\n open(s);\n }\n opened.insert(s);\n for (auto nm : group[s]) {\n group[key].insert(nm);\n }\n }\n }\n return;\n}\n\nvoid solve() {\n group.clear();\n opened.clear();\n string s;\n rep(i,n) {\n cin >> s;\n int si = 0;\n int ei = 0;\n string gn;\n while (true) {\n ei++;\n if (s[ei] == ':') {\n gn = s.substr(si,ei-si);\n qwe[i] = gn;\n si = ei+1;\n }\n if (s[ei] == ',') {\n group[gn].insert(s.substr(si,ei-si));\n si = ei+1;\n }\n if (s[ei] == '.') {\n group[gn].insert(s.substr(si,ei-si));\n break;\n }\n }\n }\n\n\n open(qwe[0]);\n reps(i,1,n) {\n if (group[qwe[0]].count(qwe[i])) {\n group[qwe[0]].erase(qwe[i]);\n }\n }\n cout << group[qwe[0]].size() << endl;\n return;\n}\n\nint main() {\n cin.tie(nullptr);\n ios_base::sync_with_stdio(false);\n while (cin >> n && n != 0) {\n solve();\n }\n}", "accuracy": 1, "time_ms": 130, "memory_kb": 3964, "score_of_the_acc": -0.6091, "final_rank": 2 }, { "submission_id": "aoj_1305_8860872", "code_snippet": "#include <iostream>\n#include <unordered_map>\n#include <set>\n#include <string>\n#include <vector>\nusing namespace std;\n\nvector<vector<int>> involve;\n\nvoid dfs2(int now, vector<bool> &used, set<int> &ans) {\n if (used[now]) return;\n used[now] = true;\n \n for (auto i : involve[now]) {\n if (involve[i].size()) {\n dfs2(i, used, ans);\n } else {\n ans.insert(i);\n }\n }\n}\n\nint main() {\n std::ios::sync_with_stdio(false);\n cin.tie(0);\n\n while (1) {\n int num = 0;\n unordered_map<string, int> mp;\n int N;\n cin >> N;\n if (!N) break;\n \n involve.clear();\n involve.resize(100000);\n \n for (int i = 0; i < N; ++i) {\n string st;\n cin >> st;\n string groupname;\n vector<string> membername;\n int pre;\n for (int j = 0; j < st.size(); ++j) {\n if (st[j] == ':') {\n groupname = st.substr(0, j);\n pre = j;\n } else if (st[j] == ',' || st[j] == '.') {\n membername.push_back(st.substr(pre + 1, j - pre - 1));\n pre = j;\n }\n }\n if (!mp.count(groupname)) {\n mp[groupname] = num++;\n }\n for (int i = 0; i < membername.size(); ++i) {\n if (!(mp.count(membername[i]))) {\n mp[membername[i]] = num++;\n }\n }\n for (int i = 0; i < membername.size(); ++i) {\n involve[mp[groupname]].push_back(mp[membername[i]]);\n }\n }\n vector<bool> used(100001, false);\n set<int> allnames;\n dfs2(0, used, allnames);\n cout << allnames.size() << endl;\n }\n return 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 5728, "score_of_the_acc": -1, "final_rank": 19 }, { "submission_id": "aoj_1305_8860868", "code_snippet": "#include <algorithm>\n#include <array>\n#include <cassert>\n#include <functional>\n#include <iomanip>\n#include <iostream>\n#include <limits>\n#include <queue>\n#include <stack>\n#include <stdio.h>\n#include <string.h>\n#include <string>\n#include <unordered_map>\n#include <unordered_set>\n#include <vector>\nusing namespace std;\n\nvector<vector<int>> involve;\n\nvoid dfs2(int now, vector<bool> &used, vector<int> &ans) {\n if (used[now]) return;\n used[now] = true;\n for (auto i : involve[now]) {\n if (involve[i].size()) {\n dfs2(i, used, ans);\n } else {\n ans.push_back(i);\n }\n }\n}\n\nint main() {\n while (1) {\n int num = 0;\n unordered_map<string, int> mp;\n unordered_set<int> ed;\n int N;\n cin >> N;\n if (!N) break;\n involve.clear();\n involve.resize(100000);\n for (int i = 0; i < N; ++i) {\n string st;\n cin >> st;\n string groupname;\n vector<string> membername;\n int pre;\n for (int j = 0; j < st.size(); ++j) {\n if (st[j] == ':') {\n groupname = st.substr(0, j);\n pre = j;\n } else if (st[j] == ',' || st[j] == '.') {\n membername.push_back(st.substr(pre + 1, j - pre - 1));\n pre = j;\n }\n }\n if (!mp.count(groupname)) {\n mp[groupname] = num++;\n }\n for (int i = 0; i < membername.size(); ++i) {\n if (!(mp.count(membername[i]))) {\n mp[membername[i]] = num++;\n }\n }\n for (int i = 0; i < membername.size(); ++i) {\n involve[mp[groupname]].push_back(mp[membername[i]]);\n ed.emplace(mp[membername[i]]);\n }\n }\n vector<bool> used(100001, false);\n vector<int> allnames;\n dfs2(0, used, allnames);\n sort(allnames.begin(), allnames.end());\n const int ans(unique(allnames.begin(), allnames.end()) - allnames.begin());\n cout << ans << endl;\n }\n return 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 5328, "score_of_the_acc": -0.8336, "final_rank": 6 }, { "submission_id": "aoj_1305_8860866", "code_snippet": "#include <algorithm>\n#include <array>\n#include <assert.h>\n#include <bitset>\n#include <complex>\n#include <cstdlib>\n#include <functional>\n#include <iomanip>\n#include <iostream>\n#include <limits>\n#include <unordered_map>\n#include <math.h>\n#include <numeric>\n#include <queue>\n#include <set>\n#include <stack>\n#include <stdio.h>\n#include <stdio.h>\n#include <string.h>\n#include <string>\n#include <vector>\nusing namespace std;\n\nbool isso(long long int a) {\n if (a == 1 || a == 0)\n return false;\n for (long long int i = 2; i * i <= a; i += (i > 2 ? 2 : 1)) {\n if (a % i == 0)\n return false;\n }\n return true;\n}\n\nlong long int powint(long long int a, long long int b) {\n assert(b >= 0);\n long long int res = 1;\n while (b > 0) {\n if (b & 1)\n res = res * a;\n a = a * a;\n b >>= 1;\n }\n return res;\n}\n\nvector<vector<int>> involve;\nvoid dfs2(const int now, vector<bool> &used, vector<int> &ans) {\n if (used[now])\n return;\n used[now] = true;\n for (auto i : involve[now]) {\n if (involve[i].size())\n dfs2(i, used, ans);\n else\n ans.push_back(i);\n }\n}\n\nint main() {\n while (1) {\n int num = 0;\n unordered_map<string, int> mp;\n set<int> ed;\n int N;\n cin >> N;\n if (!N)\n break;\n involve.clear();\n involve.resize(100000);\n for (int i = 0; i < N; ++i) {\n string st;\n cin >> st;\n string groupname;\n vector<string> membername;\n int pre;\n for (int j = 0; j < st.size(); ++j) {\n if (st[j] == ':') {\n groupname = st.substr(0, j);\n pre = j;\n } else if (st[j] == ',' || st[j] == '.') {\n membername.push_back(st.substr(pre + 1, j - pre - 1));\n pre = j;\n }\n }\n if (!mp.count(groupname)) {\n mp[groupname] = num++;\n }\n for (int i = 0; i < membername.size(); ++i) {\n if (!(mp.count(membername[i]))) {\n mp[membername[i]] = num++;\n }\n }\n for (int i = 0; i < membername.size(); ++i) {\n involve[mp[groupname]].push_back(mp[membername[i]]);\n ed.emplace(mp[membername[i]]);\n }\n }\n vector<bool> used(100001, false);\n vector<int> allnames;\n dfs2(0, used, allnames);\n sort(allnames.begin(), allnames.end());\n const int ans(unique(allnames.begin(), allnames.end()) - allnames.begin());\n cout << ans << endl;\n }\n return 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 5324, "score_of_the_acc": -0.8319, "final_rank": 5 }, { "submission_id": "aoj_1305_8835825", "code_snippet": "#include <algorithm>\n#include <array>\n#include <assert.h>\n#include <bitset>\n#include <cmath>\n#include <cstdlib>\n#include <functional>\n#include <iomanip>\n#include <iostream>\n#include <limits>\n#include <map>\n#include <math.h>\n#include <numeric>\n#include <queue>\n#include <set>\n#include <stack>\n#include <stdio.h>\n#include <stdio.h>\n#include <string.h>\n#include <string>\n#include <unordered_map>\n#include <vector>\nusing namespace std;\n\nvector<bool> primeSieve(int n) {\n vector<bool> isPrime(n + 1, true);\n isPrime[0] = isPrime[1] = false;\n\n for (int i = 2; i * i <= n; ++i) {\n if (isPrime[i]) {\n for (int j = i * i; j <= n; j += i) {\n isPrime[j] = false;\n }\n }\n }\n\n return isPrime;\n}\n\nstruct edge {\n int from;\n int to;\n int cost;\n};\n\nstruct aa {\n int now;\n int fuel;\n int cost;\n};\n\nclass Compare {\npublic:\n bool operator()(const aa& l, const aa& r) { return l.cost > r.cost; }\n};\n\nvector<vector<int>> involve;\nvector<int> dfs2(const int now, vector<bool>& used) {\n if (used[now])\n return vector<int>(0);\n else {\n used[now] = true;\n }\n vector<int> ans;\n for (auto i : involve[now]) {\n if (involve[i].size()) {\n vector<int> a(dfs2(i, used));\n ans.insert(ans.end(), a.begin(), a.end());\n }\n else {\n ans.push_back(i);\n }\n }\n return ans;\n}\n\nint main() {\n while (1) {\n int num = 0;\n unordered_map<string, int> mp;\n set<int> ed;\n int N;\n cin >> N;\n if (!N)\n break;\n involve.clear();\n involve.resize(100000);\n for (int i = 0; i < N; ++i) {\n string st;\n cin >> st;\n string groupname;\n vector<string> membername;\n int pre;\n for (int j = 0; j < st.size(); ++j) {\n if (st[j] == ':') {\n groupname = st.substr(0, j);\n pre = j;\n }\n else if (st[j] == ',' || st[j] == '.') {\n membername.push_back(st.substr(pre + 1, j - pre - 1));\n pre = j;\n }\n }\n if (!mp.count(groupname)) {\n mp[groupname] = num++;\n }\n for (int i = 0; i < membername.size(); ++i) {\n if (!(mp.count(membername[i]))) {\n mp[membername[i]] = num++;\n }\n }\n for (int i = 0; i < membername.size(); ++i) {\n involve[mp[groupname]].push_back(mp[membername[i]]);\n ed.emplace(mp[membername[i]]);\n }\n }\n vector<bool> used(100001, false);\n vector<int> allnames(dfs2(0, used));\n sort(allnames.begin(), allnames.end());\n const int ans(unique(allnames.begin(), allnames.end()) - allnames.begin());\n cout << ans << endl;\n }\n return 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 5512, "score_of_the_acc": -0.9101, "final_rank": 9 }, { "submission_id": "aoj_1305_8835823", "code_snippet": "#include <algorithm>\n#include <array>\n#include <assert.h>\n#include <bitset>\n#include <complex>\n#include <cstdlib>\n#include <functional>\n#include <iomanip>\n#include <iostream>\n#include <limits>\n#include <map>\n#include <cmath>\n#include <numeric>\n#include <queue>\n#include <set>\n#include <stack>\n#include <stdio.h>\n#include <stdio.h>\n#include <string.h>\n#include <string>\n#include <unordered_map>\n#include <vector>\nusing namespace std;\n\nlong long int powint(long long int a, long long int b) {\n assert(b >= 0);\n if (b == 0)\n return 1;\n if (b == 1)\n return a;\n long long int ans = 1;\n long long int aa = pow(a, b / 2);\n ans *= aa * aa;\n if (b % 2)\n ans *= a;\n return ans;\n}\n\nstruct edge {\n int from;\n int to;\n int cost;\n};\n\nstruct aa {\n int now;\n int fuel;\n int cost;\n};\n\nclass Compare {\npublic:\n bool operator()(const aa &l, const aa &r) { return l.cost > r.cost; }\n};\n\nvector<vector<int>> involve;\nvector<int> dfs2(const int now, vector<bool> &used) {\n if (used[now])\n return vector<int>(0);\n else {\n used[now] = true;\n }\n vector<int> ans;\n stack<int> stk;\n stk.push(now);\n while (!stk.empty()) {\n int curr = stk.top();\n stk.pop();\n for (auto i : involve[curr]) {\n if (!used[i]) {\n used[i] = true;\n stk.push(i);\n if (involve[i].empty()) {\n ans.push_back(i);\n }\n }\n }\n }\n return ans;\n}\n\nint main() {\n while (1) {\n int num = 0;\n unordered_map<string, int> mp;\n vector<bool> ed(100000, false);\n int N;\n cin >> N;\n if (!N)\n break;\n involve.clear();\n involve.resize(100000);\n for (int i = 0; i < N; ++i) {\n string st;\n cin >> st;\n string groupname;\n vector<string> membername;\n int pre;\n for (int j = 0; j < st.size(); ++j) {\n if (st[j] == ':') {\n groupname = st.substr(0, j);\n pre = j;\n } else if (st[j] == ',' || st[j] == '.') {\n membername.push_back(st.substr(pre + 1, j - pre - 1));\n pre = j;\n }\n }\n if (!mp.count(groupname)) {\n mp[groupname] = num++;\n }\n for (int i = 0; i < membername.size(); ++i) {\n if (!(mp.count(membername[i]))) {\n mp[membername[i]] = num++;\n }\n }\n for (int i = 0; i < membername.size(); ++i) {\n involve[mp[groupname]].push_back(mp[membername[i]]);\n ed[mp[membername[i]]] = true;\n }\n }\n vector<bool> used(100001, false);\n vector<int> allnames(dfs2(0, used));\n sort(allnames.begin(), allnames.end());\n const int ans(unique(allnames.begin(), allnames.end()) - allnames.begin());\n cout << ans << endl;\n }\n return 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 5544, "score_of_the_acc": -0.9235, "final_rank": 10 }, { "submission_id": "aoj_1305_8822503", "code_snippet": "#include <algorithm>\n#include <array>\n#include <assert.h>\n#include <cstdlib>\n#include <functional>\n#include <iostream>\n#include <map>\n#include <set>\n#include <string>\n#include <vector>\nusing namespace std;\n\nvector<vector<int>> involve;\n\nvector<int> dfs2(const int now, vector<bool> &used) {\n if (used[now]) return vector<int>(0);\n \n used[now] = true;\n vector<int> ans;\n ans.reserve(involve[now].size());\n\n for (auto i : involve[now]) {\n if (involve[i].size()) {\n vector<int> a(dfs2(i, used));\n ans.insert(ans.end(), make_move_iterator(a.begin()), make_move_iterator(a.end()));\n } else {\n ans.push_back(i);\n }\n }\n return ans;\n}\n\nint main() {\n while (1) {\n int num = 0;\n map<string, int> mp;\n set<int> ed;\n int N;\n cin >> N;\n if (!N) break;\n\n involve.clear();\n involve.resize(100000);\n for (int i = 0; i < N; ++i) {\n string st;\n cin >> st;\n string groupname;\n vector<string> membername;\n int pre;\n for (int j = 0; j < st.size(); ++j) {\n if (st[j] == ':') {\n groupname = st.substr(0, j);\n pre = j;\n } else if (st[j] == ',' || st[j] == '.') {\n membername.push_back(st.substr(pre + 1, j - pre - 1));\n pre = j;\n }\n }\n if (!mp.count(groupname)) {\n mp[groupname] = num++;\n }\n for (int i = 0; i < membername.size(); ++i) {\n auto it = mp.find(membername[i]);\n if (it == mp.end()) {\n mp[membername[i]] = num++;\n }\n }\n for (int i = 0; i < membername.size(); ++i) {\n involve[mp[groupname]].push_back(mp[membername[i]]);\n ed.emplace(mp[membername[i]]);\n }\n }\n vector<bool> used(100001, false);\n vector<int> allnames(dfs2(0, used));\n sort(allnames.begin(), allnames.end());\n const int ans(unique(allnames.begin(), allnames.end()) - allnames.begin());\n cout << ans << endl;\n }\n return 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 5544, "score_of_the_acc": -0.9235, "final_rank": 10 }, { "submission_id": "aoj_1305_8822488", "code_snippet": "#include <algorithm>\n#include <map>\n#include <set>\n#include <iostream>\n#include <vector>\nusing namespace std;\n\nvector<vector<int>> involve;\n\nvector<int> dfs2(const int now, vector<bool> &used) {\n if (used[now])\n return vector<int>(0);\n else {\n used[now] = true;\n }\n vector<int> ans;\n for (auto i : involve[now]) {\n if (involve[i].size()) {\n vector<int> a(dfs2(i, used));\n ans.insert(ans.end(), a.begin(), a.end());\n } else {\n ans.push_back(i);\n }\n }\n return ans;\n}\n\nint main() {\n std::ios::sync_with_stdio(false);\n while (1) {\n int num = 0;\n map<string, int> mp;\n set<int> ed;\n int N;\n cin >> N;\n if (!N)\n break;\n involve.clear();\n involve.resize(100000);\n for (int i = 0; i < N; ++i) {\n string st;\n cin >> st;\n string groupname;\n vector<string> membername;\n membername.reserve(st.size()); // preallocate memory\n int pre;\n for (int j = 0; j < st.size(); ++j) {\n if (st[j] == ':') {\n groupname = st.substr(0, j);\n pre = j;\n } else if (st[j] == ',' || st[j] == '.') {\n membername.push_back(st.substr(pre + 1, j - pre - 1));\n pre = j;\n }\n }\n if (!mp.count(groupname)) {\n mp[groupname] = num++;\n }\n for (int i = 0; i < membername.size(); ++i) {\n if (!(mp.count(membername[i]))) {\n mp[membername[i]] = num++;\n }\n }\n for (int i = 0; i < membername.size(); ++i) {\n involve[mp[groupname]].emplace_back(mp[membername[i]]);\n ed.emplace(mp[membername[i]]);\n }\n }\n vector<bool> used(100001, false);\n vector<int> allnames(dfs2(0, used));\n sort(allnames.begin(), allnames.end());\n const int ans(unique(allnames.begin(), allnames.end()) - allnames.begin());\n cout << ans << endl;\n }\n return 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 5612, "score_of_the_acc": -0.9517, "final_rank": 14 }, { "submission_id": "aoj_1305_8809589", "code_snippet": "#include <algorithm>\n#include <array>\n#include <assert.h>\n#include <bitset>\n#include <complex>\n#include <cstdlib>\n#include <functional>\n#include <iomanip>\n#include <iostream>\n#include <limits>\n#include <map>\n#include <math.h>\n#include <numeric>\n#include <queue>\n#include <set>\n#include <stack>\n#include <stdio.h>\n#include <stdio.h>\n#include <string.h>\n#include <string>\n#include <vector>\nusing namespace std;\nbool isso(long long int a) {\n if (a == 1 || a == 0)\n return false;\n for (long long int i = 2; i * i <= a; ++i) {\n if ((a % i)) {\n } else {\n return false;\n }\n }\n return true;\n}\nlong long int powint(long long int a, long long int b) {\n assert(b >= 0);\n if (b == 0)\n return 1;\n if (b == 1)\n return a;\n long long int ans = 1;\n long long int aa = powint(a, b / 2);\n ans *= aa * aa;\n if (b % 2)\n ans *= a;\n return ans;\n}\nstruct edge {\n int from;\n int to;\n int cost;\n};\nstruct aa {\n int now;\n int fuel;\n int cost;\n};\nclass Compare {\npublic:\n bool operator()(const aa &l, const aa &r) { return l.cost > r.cost; }\n};\nvector<vector<int>> involve;\nvoid dfs2(const int now, vector<bool> &used, vector<int> &ans) {\n if (used[now])\n return;\n else {\n used[now] = true;\n }\n for (auto i : involve[now]) {\n if (involve[i].size()) {\n dfs2(i, used, ans);\n } else {\n ans.push_back(i);\n }\n }\n}\nint main() {\n while (1) {\n int num = 0;\n map<string, int> mp;\n set<int> ed;\n int N;\n cin >> N;\n if (!N)\n break;\n involve.clear();\n involve.resize(100000);\n for (int i = 0; i < N; ++i) {\n string st;\n cin >> st;\n string groupname;\n vector<string> membername;\n int pre;\n for (int j = 0; j < st.size(); ++j) {\n if (st[j] == ':') {\n groupname = st.substr(0, j);\n pre = j;\n } else if (st[j] == ',' || st[j] == '.') {\n membername.push_back(st.substr(pre + 1, j - pre - 1));\n pre = j;\n }\n }\n if (!mp.count(groupname)) {\n mp[groupname] = num++;\n }\n for (int i = 0; i < membername.size(); ++i) {\n if (!(mp.count(membername[i]))) {\n mp[membername[i]] = num++;\n }\n }\n for (int i = 0; i < membername.size(); ++i) {\n involve[mp[groupname]].push_back(mp[membername[i]]);\n ed.emplace(mp[membername[i]]);\n }\n }\n vector<bool> used(100001, false);\n vector<int> allnames;\n dfs2(0, used, allnames);\n sort(allnames.begin(), allnames.end());\n const int ans(unique(allnames.begin(), allnames.end()) - allnames.begin());\n cout << ans << endl;\n }\n return 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 5580, "score_of_the_acc": -0.9384, "final_rank": 13 }, { "submission_id": "aoj_1305_8215695", "code_snippet": "#include <iostream>\n#include <vector>\n#include <unordered_map>\n#include <unordered_set>\n#include <algorithm>\n\nusing namespace std;\n\nvector<vector<int>> involve;\nvector<int> dfs2(const int now, vector<bool> &used) {\n if (used[now])\n return {};\n used[now] = true;\n vector<int> ans;\n for (const auto &i : involve[now]) {\n if (!involve[i].empty()) {\n vector<int> a = dfs2(i, used);\n ans.insert(ans.end(), make_move_iterator(a.begin()), make_move_iterator(a.end()));\n } else {\n ans.push_back(i);\n }\n }\n return ans;\n}\n\nint main() {\n ios_base::sync_with_stdio(false);\n cin.tie(nullptr);\n while (1) {\n int num = 0;\n unordered_map<string, int> mp;\n unordered_set<int> ed;\n int N;\n cin >> N;\n if (!N)\n break;\n involve.clear();\n involve.resize(100000);\n for (int i = 0; i < N; ++i) {\n string st;\n cin >> st;\n string groupname;\n vector<string> membername;\n int pre;\n for (int j = 0; j < st.size(); ++j) {\n if (st[j] == ':') {\n groupname = st.substr(0, j);\n pre = j;\n } else if (st[j] == ',' || st[j] == '.') {\n membername.push_back(st.substr(pre + 1, j - pre - 1));\n pre = j;\n }\n }\n if (!mp.count(groupname)) {\n mp[groupname] = num++;\n }\n for (const auto &name : membername) {\n if (!mp.count(name)) {\n mp[name] = num++;\n }\n }\n for (const auto &name : membername) {\n involve[mp[groupname]].push_back(mp[name]);\n ed.emplace(mp[name]);\n }\n }\n vector<bool> used(100001, false);\n vector<int> allnames = dfs2(0, used);\n sort(allnames.begin(), allnames.end());\n const int ans = distance(allnames.begin(), unique(allnames.begin(), allnames.end()));\n cout << ans << \"\\n\";\n }\n return 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 5504, "score_of_the_acc": -0.9068, "final_rank": 7 }, { "submission_id": "aoj_1305_8215691", "code_snippet": "#include <algorithm>\n#include <array>\n#include <assert.h>\n#include <bitset>\n#include <complex>\n#include <cstdlib>\n#include <functional>\n#include <iomanip>\n#include <iostream>\n#include <limits>\n#include <map>\n#include <math.h>\n#include <numeric>\n#include <queue>\n#include <set>\n#include <stack>\n#include <stdio.h>\n#include <stdio.h>\n#include <string.h>\n#include <string>\n#include <vector>\nusing namespace std;\nbool isso(long long int a) {\n if (a == 1 || a == 0)\n return false;\n for (long long int i = 2; i * i <= a; ++i) {\n if ((a % i)) {\n } else {\n return false;\n }\n }\n return true;\n}\nlong long int powint(long long int a, long long int b) {\n assert(b >= 0);\n if (b == 0)\n return 1;\n if (b == 1)\n return a;\n long long int ans = 1;\n long long int aa = powint(a, b / 2);\n ans *= aa * aa;\n if (b % 2)\n ans *= a;\n return ans;\n}\nstruct edge {\n int from;\n int to;\n int cost;\n};\nstruct aa {\n int now;\n int fuel;\n int cost;\n};\nclass Compare {\npublic:\n bool operator()(const aa &l, const aa &r) { return l.cost > r.cost; }\n};\nvector<vector<int>> involve;\nvector<int> dfs2(const int now, vector<bool> &used) {\n if (used[now])\n return vector<int>(0);\n else {\n used[now] = true;\n }\n vector<int> ans;\n for (auto i : involve[now]) {\n if (involve[i].size()) {\n vector<int> a(dfs2(i, used));\n ans.insert(ans.end(), a.begin(), a.end());\n } else {\n ans.push_back(i);\n }\n }\n return ans;\n}\nint main() {\n while (1) {\n int num = 0;\n map<string, int> mp;\n set<int> ed;\n int N;\n cin >> N;\n if (!N)\n break;\n involve.clear();\n involve.resize(1100);\n for (int i = 0; i < N; ++i) {\n string st;\n cin >> st;\n string groupname;\n vector<string> membername;\n int pre;\n for (int j = 0; j < st.size(); ++j) {\n if (st[j] == ':') {\n groupname = st.substr(0, j);\n pre = j;\n } else if (st[j] == ',' || st[j] == '.') {\n membername.push_back(st.substr(pre + 1, j - pre - 1));\n pre = j;\n }\n }\n if (!mp.count(groupname)) {\n mp[groupname] = num++;\n }\n for (int i = 0; i < membername.size(); ++i) {\n if (!(mp.count(membername[i]))) {\n mp[membername[i]] = num++;\n }\n }\n for (int i = 0; i < membername.size(); ++i) {\n involve[mp[groupname]].push_back(mp[membername[i]]);\n ed.emplace(mp[membername[i]]);\n }\n }\n vector<bool> used(1100, false);\n vector<int> allnames(dfs2(0, used));\n sort(allnames.begin(), allnames.end());\n const int ans(unique(allnames.begin(), allnames.end()) - allnames.begin());\n cout << ans << endl;\n }\n return 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3324, "score_of_the_acc": 0, "final_rank": 1 }, { "submission_id": "aoj_1305_8215690", "code_snippet": "#include <algorithm>\n#include <array>\n#include <assert.h>\n#include <bitset>\n#include <complex>\n#include <cstdlib>\n#include <functional>\n#include <iomanip>\n#include <iostream>\n#include <limits>\n#include <map>\n#include <math.h>\n#include <numeric>\n#include <queue>\n#include <set>\n#include <stack>\n#include <stdio.h>\n#include <stdio.h>\n#include <string.h>\n#include <string>\n#include <vector>\nusing namespace std;\nbool isso(long long int a) {\n if (a == 1 || a == 0)\n return false;\n for (long long int i = 2; i * i <= a; ++i) {\n if ((a % i)) {\n } else {\n return false;\n }\n }\n return true;\n}\nlong long int powint(long long int a, long long int b) {\n assert(b >= 0);\n if (b == 0)\n return 1;\n if (b == 1)\n return a;\n long long int ans = 1;\n long long int aa = powint(a, b / 2);\n ans *= aa * aa;\n if (b % 2)\n ans *= a;\n return ans;\n}\nstruct edge {\n int from;\n int to;\n int cost;\n};\nstruct aa {\n int now;\n int fuel;\n int cost;\n};\nclass Compare {\npublic:\n bool operator()(const aa &l, const aa &r) { return l.cost > r.cost; }\n};\nvector<vector<int>> involve;\nvector<int> dfs2(const int now, vector<bool> &used) {\n if (used[now])\n return vector<int>(0);\n else {\n used[now] = true;\n }\n vector<int> ans;\n for (auto i : involve[now]) {\n if (involve[i].size()) {\n vector<int> a(dfs2(i, used));\n ans.insert(ans.end(), a.begin(), a.end());\n } else {\n ans.push_back(i);\n }\n }\n return ans;\n}\nint main() {\n while (1) {\n int num = 0;\n map<string, int> mp;\n set<int> ed;\n int N;\n cin >> N;\n if (!N)\n break;\n involve.clear();\n involve.resize(100000);\n for (int i = 0; i < N; ++i) {\n string st;\n cin >> st;\n string groupname;\n vector<string> membername;\n int pre;\n for (int j = 0; j < st.size(); ++j) {\n if (st[j] == ':') {\n groupname = st.substr(0, j);\n pre = j;\n } else if (st[j] == ',' || st[j] == '.') {\n membername.push_back(st.substr(pre + 1, j - pre - 1));\n pre = j;\n }\n }\n if (!mp.count(groupname)) {\n mp[groupname] = num++;\n }\n for (int i = 0; i < membername.size(); ++i) {\n if (!(mp.count(membername[i]))) {\n mp[membername[i]] = num++;\n }\n }\n for (int i = 0; i < membername.size(); ++i) {\n involve[mp[groupname]].push_back(mp[membername[i]]);\n ed.emplace(mp[membername[i]]);\n }\n }\n vector<bool> used(100001, false);\n vector<int> allnames(dfs2(0, used));\n sort(allnames.begin(), allnames.end());\n const int ans(unique(allnames.begin(), allnames.end()) - allnames.begin());\n cout << ans << endl;\n }\n return 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 5680, "score_of_the_acc": -0.98, "final_rank": 18 }, { "submission_id": "aoj_1305_8116057", "code_snippet": "#include <algorithm>\n#include <array>\n#include <assert.h>\n#include <bitset>\n#include <complex>\n#include <cstdlib>\n#include <functional>\n#include <iomanip>\n#include <iostream>\n#include <limits>\n#include <map>\n#include <math.h>\n#include <numeric>\n#include <queue>\n#include <set>\n#include <stack>\n#include <stdio.h>\n#include <stdio.h>\n#include <string.h>\n#include <string>\n#include <vector>\nusing namespace std;\n\nbool is_prime(long long n) {\n if (n <= 1) {\n return false;\n }\n for (long long i = 2; i * i <= n; ++i) {\n if (n % i == 0) {\n return false;\n }\n }\n return true;\n}\n\nlong long pow_int(long long a, long long b) {\n assert(b >= 0);\n if (b == 0) {\n return 1;\n }\n if (b == 1) {\n return a;\n }\n long long ans = 1;\n long long aa = pow_int(a, b / 2);\n ans *= aa * aa;\n if (b % 2) {\n ans *= a;\n }\n return ans;\n}\n\nstruct Edge {\n int from;\n int to;\n int cost;\n};\n\nstruct AA {\n int now;\n int fuel;\n int cost;\n};\n\nclass Compare {\npublic:\n bool operator()(const AA& l, const AA& r) {\n return l.cost > r.cost;\n }\n};\n\nvector<vector<int>> involve;\n\nvector<int> dfs(int now, vector<bool>& used) {\n if (used[now]) {\n return vector<int>();\n }\n used[now] = true;\n vector<int> ans;\n for (auto i : involve[now]) {\n if (involve[i].size()) {\n vector<int> a(dfs(i, used));\n ans.insert(ans.end(), a.begin(), a.end());\n } else {\n ans.push_back(i);\n }\n }\n return ans;\n}\n\nint main() {\n while (1) {\n int num = 0;\n map<string, int> mp;\n set<int> ed;\n int N;\n cin >> N;\n if (!N) {\n break;\n }\n involve.clear();\n involve.resize(100000);\n for (int i = 0; i < N; ++i) {\n string st;\n cin >> st;\n string groupname;\n vector<string> membername;\n int pre = -1;\n for (int j = 0; j < st.size(); ++j) {\n if (st[j] == ':') {\n groupname = st.substr(0, j);\n pre = j;\n } else if (st[j] == ',' || st[j] == '.') {\n membername.push_back(st.substr(pre + 1, j - pre - 1));\n pre = j;\n }\n }\n if (!mp.count(groupname)) {\n mp[groupname] = num++;\n }\n for (int i = 0; i < membername.size(); ++i) {\n if (!(mp.count(membername[i]))) {\n mp[membername[i]] = num++;\n }\n }\n for (int i = 0; i < membername.size(); ++i) {\n involve[mp[groupname]].push_back(mp[membername[i]]);\n ed.emplace(mp[membername[i]]);\n }\n }\n vector<bool> used(100001, false);\n vector<int> allnames(dfs(0, used));\n sort(allnames.begin(), allnames.end());\n const int ans(unique(allnames.begin(), allnames.end()) - allnames.begin());\n cout << ans << endl;\n }\n return 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 5508, "score_of_the_acc": -0.9085, "final_rank": 8 }, { "submission_id": "aoj_1305_8116056", "code_snippet": "#include <algorithm>\n#include <array>\n#include <assert.h>\n#include <bitset>\n#include <complex>\n#include <cstdlib>\n#include <functional>\n#include <iomanip>\n#include <iostream>\n#include <limits>\n#include <map>\n#include <math.h>\n#include <numeric>\n#include <queue>\n#include <set>\n#include <stack>\n#include <stdio.h>\n#include <stdio.h>\n#include <string.h>\n#include <string>\n#include <vector>\nusing namespace std;\n\nbool is_prime(long long int a) {\n if (a == 1 || a == 0)\n return false;\n for (long long int i = 2; i * i <= a; ++i) {\n if (a % i == 0) {\n return false;\n }\n }\n return true;\n}\n\nlong long int pow_int(long long int a, long long int b) {\n assert(b >= 0);\n if (b == 0)\n return 1;\n if (b == 1)\n return a;\n long long int ans = 1;\n long long int aa = pow_int(a, b / 2);\n ans *= aa * aa;\n if (b % 2)\n ans *= a;\n return ans;\n}\n\nstruct edge {\n int from;\n int to;\n int cost;\n};\n\nstruct aa {\n int now;\n int fuel;\n int cost;\n};\n\nclass Compare {\npublic:\n bool operator()(const aa &l, const aa &r) { return l.cost > r.cost; }\n};\n\nvector<vector<int>> involve;\n\nvector<int> dfs2(const int now, vector<bool> &used) {\n if (used[now])\n return vector<int>(0);\n else {\n used[now] = true;\n }\n vector<int> ans;\n for (auto i : involve[now]) {\n if (involve[i].size()) {\n vector<int> a(dfs2(i, used));\n ans.insert(ans.end(), a.begin(), a.end());\n } else {\n ans.push_back(i);\n }\n }\n return ans;\n}\n\nint main() {\n ios_base::sync_with_stdio(false);\n cin.tie(NULL);\n\n while (1) {\n int num = 0;\n map<string, int> mp;\n set<int> ed;\n int N;\n cin >> N;\n if (!N)\n break;\n involve.clear();\n involve.resize(100000);\n for (int i = 0; i < N; ++i) {\n string st;\n cin >> st;\n string groupname;\n vector<string> membername;\n int pre;\n for (int j = 0; j < st.size(); ++j) {\n if (st[j] == ':') {\n groupname = st.substr(0, j);\n pre = j;\n } else if (st[j] == ',' || st[j] == '.') {\n membername.push_back(st.substr(pre + 1, j - pre - 1));\n pre = j;\n }\n }\n if (!mp.count(groupname)) {\n mp[groupname] = num++;\n }\n for (int i = 0; i < membername.size(); ++i) {\n if (!(mp.count(membername[i]))) {\n mp[membername[i]] = num++;\n }\n }\n for (int i = 0; i < membername.size(); ++i) {\n involve[mp[groupname]].push_back(mp[membername[i]]);\n ed.emplace(mp[membername[i]]);\n }\n }\n vector<bool> used(100001, false);\n vector<int> allnames(dfs2(0, used));\n sort(allnames.begin(), allnames.end());\n const int ans(unique(allnames.begin(), allnames.end()) - allnames.begin());\n cout << ans << endl;\n }\n return 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 5632, "score_of_the_acc": -0.9601, "final_rank": 16 }, { "submission_id": "aoj_1305_8116052", "code_snippet": "#include <algorithm>\n#include <array>\n#include <assert.h>\n#include <bitset>\n#include <complex>\n#include <cstdlib>\n#include <functional>\n#include <iomanip>\n#include <iostream>\n#include <limits>\n#include <map>\n#include <math.h>\n#include <numeric>\n#include <queue>\n#include <set>\n#include <stack>\n#include <stdio.h>\n#include <stdio.h>\n#include <string.h>\n#include <string>\n#include <vector>\nusing namespace std;\nbool is_prime(long long int a) {\n if (a == 1 || a == 0)\n return false;\n for (long long int i = 2; i * i <= a; ++i) {\n if ((a % i)) {\n } else {\n return false;\n }\n }\n return true;\n}\nlong long int pow_int(long long int a, long long int b) {\n assert(b >= 0);\n if (b == 0) {\n return 1;\n }\n if (b == 1) {\n return a;\n }\n long long int ans = 1;\n long long int aa = pow_int(a, b / 2);\n ans *= aa * aa;\n if (b % 2) {\n ans *= a;\n }\n return ans;\n}\nstruct edge {\n int from;\n int to;\n int cost;\n};\nstruct aa {\n int now;\n int fuel;\n int cost;\n};\nclass Compare {\npublic:\n bool operator()(const aa &l, const aa &r) { return l.cost > r.cost; }\n};\nvector<vector<int>> involve;\nvector<int> dfs2(const int now, vector<bool> &used) {\n if (used[now]) {\n return vector<int>(0);\n } else {\n used[now] = true;\n }\n vector<int> ans;\n for (auto i : involve[now]) {\n if (involve[i].size()) {\n vector<int> a(dfs2(i, used));\n ans.insert(ans.end(), a.begin(), a.end());\n } else {\n ans.push_back(i);\n }\n }\n return ans;\n}\nint main() {\n while (1) {\n int num = 0;\n map<string, int> mp;\n set<int> ed;\n int N;\n cin >> N;\n if (!N) {\n break;\n }\n involve.clear();\n involve.resize(100000);\n for (int i = 0; i < N; ++i) {\n string st;\n cin >> st;\n string groupname;\n vector<string> membername;\n int pre;\n for (int j = 0; j < st.size(); ++j) {\n if (st[j] == ':') {\n groupname = st.substr(0, j);\n pre = j;\n } else if (st[j] == ',' || st[j] == '.') {\n membername.push_back(st.substr(pre + 1, j - pre - 1));\n pre = j;\n }\n }\n if (!mp.count(groupname)) {\n mp[groupname] = num++;\n }\n for (int i = 0; i < membername.size(); ++i) {\n if (!(mp.count(membername[i]))) {\n mp[membername[i]] = num++;\n }\n }\n for (int i = 0; i < membername.size(); ++i) {\n involve[mp[groupname]].push_back(mp[membername[i]]);\n ed.emplace(mp[membername[i]]);\n }\n }\n vector<bool> used(num + 1, false);\n vector<int> allnames(dfs2(0, used));\n sort(allnames.begin(), allnames.end());\n const int ans(unique(allnames.begin(), allnames.end()) - allnames.begin());\n cout << ans << endl;\n }\n return 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 5288, "score_of_the_acc": -0.817, "final_rank": 4 }, { "submission_id": "aoj_1305_8116049", "code_snippet": "#include <algorithm>\n#include <array>\n#include <bitset>\n#include <cassert>\n#include <complex>\n#include <cstdio>\n#include <cstdlib>\n#include <cstring>\n#include <functional>\n#include <iomanip>\n#include <iostream>\n#include <limits>\n#include <map>\n#include <math.h>\n#include <numeric>\n#include <queue>\n#include <set>\n#include <stack>\n#include <string>\n#include <vector>\nusing namespace std;\n\nbool is_prime(long long int a) {\n if (a < 2) {\n return false;\n }\n for (long long int i = 2; i * i <= a; ++i) {\n if (a % i == 0) {\n return false;\n }\n }\n return true;\n}\n\nlong long int powint(long long int a, long long int b) {\n assert(b >= 0);\n if (b == 0) {\n return 1;\n }\n if (b == 1) {\n return a;\n }\n long long int ans = 1;\n long long int aa = powint(a, b / 2);\n ans *= aa * aa;\n if (b % 2) {\n ans *= a;\n }\n return ans;\n}\n\nstruct edge {\n int from;\n int to;\n int cost;\n};\n\nstruct aa {\n int now;\n int fuel;\n int cost;\n};\n\nclass Compare {\npublic:\n bool operator()(const aa &l, const aa &r) { return l.cost > r.cost; }\n};\n\nvector<vector<int>> involve;\n\nvector<int> dfs2(const int now, vector<bool> &used) {\n if (used[now]) {\n return vector<int>(0);\n } else {\n used[now] = true;\n }\n vector<int> ans;\n for (auto i : involve[now]) {\n if (involve[i].size()) {\n vector<int> a(dfs2(i, used));\n ans.insert(ans.end(), a.begin(), a.end());\n } else {\n ans.push_back(i);\n }\n }\n return ans;\n}\n\nint main() {\n ios_base::sync_with_stdio(false);\n cin.tie(nullptr);\n cout.tie(nullptr);\n\n while (true) {\n int num = 0;\n map<string, int> mp;\n set<int> ed;\n int N;\n cin >> N;\n if (!N) {\n break;\n }\n involve.clear();\n involve.resize(100000);\n for (int i = 0; i < N; ++i) {\n string st;\n cin >> st;\n string groupname;\n vector<string> membername;\n int pre = -1;\n int idx = 0;\n while (idx < st.size()) {\n if (st[idx] == ':') {\n groupname = st.substr(0, idx);\n pre = idx;\n } else if (st[idx] == ',' || st[idx] == '.') {\n membername.push_back(st.substr(pre + 1, idx - pre - 1));\n pre = idx;\n }\n ++idx;\n }\n if (!mp.count(groupname)) {\n mp[groupname] = num++;\n }\n for (int i = 0; i < membername.size(); ++i) {\n if (!(mp.count(membername[i]))) {\n mp[membername[i]] = num++;\n }\n }\n for (int i = 0; i < membername.size(); ++i) {\n involve[mp[groupname]].push_back(mp[membername[i]]);\n ed.emplace(mp[membername[i]]);\n }\n }\n vector<bool> used(100001, false);\n vector<int> allnames(dfs2(0, used));\n sort(allnames.begin(), allnames.end());\n const int ans(unique(allnames.begin(), allnames.end()) - allnames.begin());\n cout << ans << endl;\n }\n return 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 5676, "score_of_the_acc": -0.9784, "final_rank": 17 }, { "submission_id": "aoj_1305_8116048", "code_snippet": "#include <algorithm>\n#include <array>\n#include <bitset>\n#include <iostream>\n#include <map>\n#include <queue>\n#include <set>\n#include <vector>\nusing namespace std;\n\nbool is_prime(long long int a) {\n if (a == 1 || a == 0)\n return false;\n for (long long int i = 2; i * i <= a; ++i) {\n if ((a % i) == 0) {\n return false;\n }\n }\n return true;\n}\n\nlong long int powint(long long int a, long long int b) {\n if (b == 0)\n return 1;\n if (b == 1)\n return a;\n long long int ans = powint(a * a, b / 2);\n if (b % 2)\n ans *= a;\n return ans;\n}\n\nstruct edge {\n int from;\n int to;\n int cost;\n};\n\nstruct aa {\n int now;\n int fuel;\n int cost;\n};\n\nclass Compare {\npublic:\n bool operator()(const aa &l, const aa &r) { return l.cost > r.cost; }\n};\n\nvector<vector<int>> involve;\nvector<int> dfs2(const int now, vector<bool> &used) {\n if (used[now])\n return vector<int>(0);\n else {\n used[now] = true;\n }\n vector<int> ans;\n for (auto i : involve[now]) {\n if (involve[i].size()) {\n vector<int> a(dfs2(i, used));\n ans.insert(ans.end(), a.begin(), a.end());\n } else {\n ans.push_back(i);\n }\n }\n return ans;\n}\n\nint main() {\n ios_base::sync_with_stdio(false);\n cin.tie(nullptr);\n cout.tie(nullptr);\n\n while (true) {\n int num = 0;\n map<string, int> mp;\n set<int> ed;\n int N;\n cin >> N;\n if (!N)\n break;\n involve.clear();\n involve.resize(100000);\n for (int i = 0; i < N; ++i) {\n string st;\n cin >> st;\n string groupname;\n vector<string> membername;\n int pre = -1;\n for (int j = 0; j < st.size(); ++j) {\n if (st[j] == ':') {\n groupname = st.substr(0, j);\n pre = j;\n } else if (st[j] == ',' || st[j] == '.') {\n membername.push_back(st.substr(pre + 1, j - pre - 1));\n pre = j;\n }\n }\n if (!mp.count(groupname)) {\n mp[groupname] = num++;\n }\n for (int i = 0; i < membername.size(); ++i) {\n if (!(mp.count(membername[i]))) {\n mp[membername[i]] = num++;\n }\n }\n for (int i = 0; i < membername.size(); ++i) {\n involve[mp[groupname]].push_back(mp[membername[i]]);\n ed.emplace(mp[membername[i]]);\n }\n }\n vector<bool> used(num, false);\n vector<int> allnames(dfs2(0, used));\n sort(allnames.begin(), allnames.end());\n cout << unique(allnames.begin(), allnames.end()) - allnames.begin() << endl;\n }\n return 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 5576, "score_of_the_acc": -0.9368, "final_rank": 12 }, { "submission_id": "aoj_1305_8116046", "code_snippet": "#include <algorithm>\n#include <array>\n#include <assert.h>\n#include <bitset>\n#include <complex>\n#include <cstdlib>\n#include <functional>\n#include <iomanip>\n#include <iostream>\n#include <limits>\n#include <map>\n#include <math.h>\n#include <numeric>\n#include <queue>\n#include <set>\n#include <stack>\n#include <stdio.h>\n#include <stdio.h>\n#include <string.h>\n#include <string>\n#include <vector>\nusing namespace std;\nbool isprime(int a) {\n if (a == 1 || a == 0)\n return false;\n for (int i = 2; i * i <= a; ++i) {\n if (a % i == 0) {\n return false;\n }\n }\n return true;\n}\nint powint(int a, int b) {\n assert(b >= 0);\n if (b == 0)\n return 1;\n if (b == 1)\n return a;\n int ans = 1;\n int aa = powint(a, b / 2);\n ans *= aa * aa;\n if (b % 2)\n ans *= a;\n return ans;\n}\nstruct edge {\n int from;\n int to;\n int cost;\n};\nstruct aa {\n int now;\n int fuel;\n int cost;\n};\nclass Compare {\npublic:\n bool operator()(const aa &l, const aa &r) { return l.cost > r.cost; }\n};\nvector<vector<int>> involve;\nvector<int> dfs2(const int now, vector<bool> &used) {\n if (used[now])\n return vector<int>(0);\n else {\n used[now] = true;\n }\n vector<int> ans;\n for (auto i : involve[now]) {\n if (involve[i].size()) {\n vector<int> a(dfs2(i, used));\n ans.insert(ans.end(), a.begin(), a.end());\n } else {\n ans.push_back(i);\n }\n }\n return ans;\n}\nint main() {\n while (1) {\n int num = 0;\n map<string, int> mp;\n set<int> ed;\n int N;\n cin >> N;\n if (!N)\n break;\n involve.clear();\n involve.resize(100000);\n for (int i = 0; i < N; ++i) {\n string st;\n cin >> st;\n string groupname;\n vector<string> membername;\n int pre;\n for (int j = 0; j < st.size(); ++j) {\n if (st[j] == ':') {\n groupname = st.substr(0, j);\n pre = j;\n } else if (st[j] == ',' || st[j] == '.') {\n membername.push_back(st.substr(pre + 1, j - pre - 1));\n pre = j;\n }\n }\n if (!mp.count(groupname)) {\n mp[groupname] = num++;\n }\n for (int i = 0; i < membername.size(); ++i) {\n if (!(mp.count(membername[i]))) {\n mp[membername[i]] = num++;\n }\n }\n for (int i = 0; i < membername.size(); ++i) {\n involve[mp[groupname]].push_back(mp[membername[i]]);\n ed.emplace(mp[membername[i]]);\n }\n }\n vector<bool> used(num, false);\n vector<int> allnames(dfs2(0, used));\n sort(allnames.begin(), allnames.end());\n const int ans(unique(allnames.begin(), allnames.end()) - allnames.begin());\n cout << ans << endl;\n }\n return 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 5624, "score_of_the_acc": -0.9567, "final_rank": 15 }, { "submission_id": "aoj_1305_8116045", "code_snippet": "#include <algorithm>\n#include <array>\n#include <bitset>\n#include <cassert>\n#include <complex>\n#include <cstdlib>\n#include <functional>\n#include <iomanip>\n#include <iostream>\n#include <limits>\n#include <map>\n#include <math.h>\n#include <numeric>\n#include <queue>\n#include <set>\n#include <stack>\n#include <stdio.h>\n#include <string.h>\n#include <string>\n#include <vector>\n\nusing namespace std;\n\nbool is_prime(long long int a) {\n if (a <= 1)\n return false;\n for (long long int i = 2; i * i <= a; ++i) {\n if (a % i == 0) {\n return false;\n }\n }\n return true;\n}\n\nlong long int pow_int(long long int a, long long int b) {\n assert(b >= 0);\n if (b == 0)\n return 1;\n if (b == 1)\n return a;\n long long int ans = 1;\n long long int aa = pow_int(a, b / 2);\n ans *= aa * aa;\n if (b % 2)\n ans *= a;\n return ans;\n}\n\nstruct edge {\n int from;\n int to;\n int cost;\n};\n\nstruct aa {\n int now;\n int fuel;\n int cost;\n};\n\nclass Compare {\npublic:\n bool operator()(const aa &l, const aa &r) { return l.cost > r.cost; }\n};\n\nvector<vector<int>> involve;\nvector<int> dfs2(const int now, vector<bool> &used) {\n if (used[now])\n return vector<int>(0);\n else {\n used[now] = true;\n }\n vector<int> ans;\n for (auto i : involve[now]) {\n if (involve[i].size()) {\n vector<int> a(dfs2(i, used));\n ans.insert(ans.end(), a.begin(), a.end());\n } else {\n ans.push_back(i);\n }\n }\n return ans;\n}\n\nint main() {\n while (1) {\n int num = 0;\n map<string, int> mp;\n set<int> ed;\n int N;\n cin >> N;\n if (!N)\n break;\n involve.clear();\n involve.resize(100000);\n for (int i = 0; i < N; ++i) {\n string st;\n cin >> st;\n string groupname;\n vector<string> membername;\n int pre;\n for (int j = 0; j < st.size(); ++j) {\n if (st[j] == ':') {\n groupname = st.substr(0, j);\n pre = j;\n } else if (st[j] == ',' || st[j] == '.') {\n membername.push_back(st.substr(pre + 1, j - pre - 1));\n pre = j;\n }\n }\n if (!mp.count(groupname)) {\n mp[groupname] = num++;\n }\n for (int i = 0; i < membername.size(); ++i) {\n if (!(mp.count(membername[i]))) {\n mp[membername[i]] = num++;\n }\n }\n for (int i = 0; i < membername.size(); ++i) {\n involve[mp[groupname]].push_back(mp[membername[i]]);\n ed.emplace(mp[membername[i]]);\n }\n }\n vector<bool> used(num, false);\n vector<int> allnames(dfs2(0, used));\n sort(allnames.begin(), allnames.end());\n const int ans(unique(allnames.begin(), allnames.end()) - allnames.begin());\n cout << ans << endl;\n }\n return 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 5260, "score_of_the_acc": -0.8053, "final_rank": 3 } ]
aoj_1307_cpp
Problem C: Towns along a Highway There are several towns on a highway. The highway has no forks. Given the distances between the neighboring towns, we can calculate all distances from any town to others. For example, given five towns (A, B, C, D and E) and the distances between neighboring towns like in Figure C.1, we can calculate the distance matrix as an upper triangular matrix containing the distances between all pairs of the towns, as shown in Figure C.2. Figure C.1: An example of towns Figure C.2: The distance matrix for Figure C.1 In this problem, you must solve the inverse problem. You know only the distances between all pairs of the towns, that is, N ( N - 1)/2 numbers in the above matrix. Then, you should recover the order of the towns and the distances from each town to the next. Input The input is a sequence of datasets. Each dataset is formatted as follows. N d 1 d 2 ... d N ( N -1)/2 The first line contains an integer N (2 ≤ N ≤ 20), which is the number of the towns on the highway. The following lines contain N ( N -1)/2 integers separated by a single space or a newline, which are the distances between all pairs of the towns. Numbers are given in descending order without any information where they appear in the matrix. You can assume that each distance is between 1 and 400, inclusive. Notice that the longest distance is the distance from the leftmost town to the rightmost. The end of the input is indicated by a line containing a zero. Output For each dataset, output N - 1 integers in a line, each of which is the distance from a town to the next in the order of the towns. Numbers in a line must be separated by a single space. If the answer is not unique, output multiple lines ordered by the lexicographical order as a sequence of integers, each of which corresponds to an answer. For example, ' 2 10 ' should precede ' 10 2 '. If no answer exists, output nothing. At the end of the output for each dataset, a line containing five minus symbols ' ----- ' should be printed for human readability. No extra characters should occur in the output. For example, extra spaces at the end of a line are not allowed. Sample Input 2 1 3 5 3 2 3 6 3 2 5 9 8 7 6 6 4 3 2 2 1 6 9 8 8 7 6 6 5 5 3 3 3 2 2 1 1 6 11 10 9 8 7 6 6 5 5 4 3 2 2 1 1 7 72 65 55 51 48 45 40 38 34 32 27 25 24 23 21 17 14 13 11 10 7 20 190 189 188 187 186 185 184 183 182 181 180 179 178 177 176 175 174 173 172 171 170 169 168 167 166 165 164 163 162 161 160 159 158 157 156 155 154 153 152 151 150 149 148 147 146 145 144 143 142 141 140 139 138 137 136 135 134 133 132 131 130 129 128 127 126 125 124 123 122 121 120 119 118 117 116 115 114 113 112 111 110 109 108 107 106 105 104 103 102 101 100 99 98 97 96 95 94 93 92 91 90 89 88 87 86 85 84 83 82 81 80 79 78 77 76 75 74 73 72 71 70 69 68 67 66 65 64 63 62 61 60 59 58 57 56 55 54 53 52 51 50 49 48 47 46 45 44 43 42 41 40 39 38 37 36 35 34 33 32 31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 19 60 59 58 ...(truncated)
[ { "submission_id": "aoj_1307_10850484", "code_snippet": "#include <cstdio>\n#include <cstring>\n#include <algorithm>\n#include <map>\n#include <iostream>\n#include <vector>\nusing namespace std;\nint d[500];\nint sum[100];\nint n;\nvector<int>ans[1148576];\nint cnt;\nbool vis[1148576];\nint dis[500];\nint num[500];\nbool check(int l,int r,int id,int val)\n{\n\tbool flag=true;\n\tdis[id]=val;\n\tfor(int i=1;i<=l;i++)\n\t{\n\t num[dis[id]-dis[i]]--;\n if(num[dis[id]-dis[i]]<0)\n {\n flag=false;\n }\n\t}\n\tfor(int i=r;i<=n;i++)\n {\n num[dis[i]-dis[id]]--;\n if(num[dis[i]-dis[id]]<0)\n {\n flag=false;\n }\n }\n\tif(flag) return true;\n\treturn false;\n}\nvoid dfs(int l,int r)\n{\n if(l==r-1)\n {\n for(int i=1;i<=n;i++)\n {\n ans[cnt].push_back(dis[i]);\n }\n cnt++;\n return ;\n }\n int k=1;\n\twhile(num[d[k]]==0) k++;\n\tif(check(l,r,l+1,dis[n]-d[k]))dfs(l+1,r);\n\tfor(int i=1;i<=l;i++)\n {\n num[dis[l+1]-dis[i]]++;\n }\n\tfor(int i=r;i<=n;i++)\n {\n num[dis[i]-dis[l+1]]++;\n }\n\tif(check(l,r,r-1,d[k]))dfs(l,r-1);\n\tfor(int i=1;i<=l;i++)\n\t{\n\t num[dis[r-1]-dis[i]]++;\n\t}\n\tfor(int i=r;i<=n;i++)\n {\n num[dis[i]-dis[r-1]]++;\n }\n}\nint main()\n{\n while(~scanf(\"%d\",&n))\n {\n if(n==0)break;\n cnt=0;\n memset(vis,0,sizeof(vis));\n memset(num,0,sizeof(num));\n for(int i=0;i<300000;i++)\n {\n ans[i].clear();\n }\n for(int i=0;i<n*(n-1)/2;i++)\n {\n scanf(\"%d\",d+i);\n num[d[i]]++;\n }\n dis[n]=d[0];\n num[d[0]]--;\n memset(sum,0,sizeof(sum));\n dfs(1,n);\n for(int i=0;i<cnt;i++)\n {\n if(vis[i])continue;\n for(int j=i+1;j<cnt;j++)\n {\n int len=ans[i].size();\n int k;\n for(k=0;k<len;k++)\n {\n if(ans[i][k]!=ans[j][k])break;\n }\n if(k==len)\n {\n vis[j]=1;\n }\n\n }\n }\n // cout<<\"~~\"<<cnt<<endl;\n for(int i=0;i<cnt;i++)\n {\n if(vis[i])continue;\n int len=ans[i].size();\n for(int k=1;k<len;k++)\n {\n if(k==len-1)\n printf(\"%d\\n\",ans[i][k]-ans[i][k-1]);\n else\n printf(\"%d \",ans[i][k]-ans[i][k-1]);\n }\n }\n printf(\"-----\\n\");\n }\n}", "accuracy": 1, "time_ms": 20, "memory_kb": 31440, "score_of_the_acc": -1.0031, "final_rank": 19 }, { "submission_id": "aoj_1307_8326614", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\n#define rep(i, n) for (int i = 0; i < (n); i++)\n#define per(i, n) for (int i = (n)-1; i >= 0; i--)\n#define rep2(i, l, r) for (int i = (l); i < (r); i++)\n#define per2(i, l, r) for (int i = (r)-1; i >= (l); i--)\n#define each(e, x) for (auto &e : x)\n#define sz(x) (int)x.size()\n#define all(x) begin(x), end(x)\n#define rall(x) rbegin(x), rend(x)\n#define eb emplace_back\n#define MM << ' ' <<\n#define TT template <typename T>\nusing ll = long long;\nusing pii = pair<int, int>;\nusing pll = pair<ll, ll>;\n\nTT bool chmin(T &x, T y) { return x > y ? (x = y, true) : false; }\nTT bool chmax(T &x, T y) { return x < y ? (x = y, true) : false; }\n\nTT using minheap = priority_queue<T, vector<T>, greater<T>>;\nTT using maxheap = priority_queue<T>;\n\nTT void print(vector<T> v, T x = 0) {\n int n = sz(v);\n rep(i, n) cout << v[i] + x << (i == n - 1 ? '\\n' : ' ');\n if (n == 0) cout << '\\n';\n}\n\nTT void printn(vector<T> v, T x = 0) {\n int n = sz(v);\n rep(i, n) cout << v[i] + x << '\\n';\n}\n\nTT void rearrange(vector<T> &v) {\n sort(all(v));\n v.erase(unique(all(v)), end(v));\n}\n\nTT int lb(vector<T> &v, T x) { return lower_bound(all(v), x) - begin(v); }\nTT int ub(vector<T> &v, T x) { return upper_bound(all(v), x) - begin(v); }\n\nstruct io_setup {\n io_setup() {\n ios_base::sync_with_stdio(false);\n cin.tie(NULL);\n cout << fixed << setprecision(15);\n }\n} io_setup;\n\nint main() {\n while (true) {\n int N;\n cin >> N;\n if (N == 0) break;\n\n int D;\n cin >> D;\n\n vector<int> c(D + 1, 0);\n rep(i, N * (N - 1) / 2 - 1) {\n int x;\n cin >> x;\n c[x]++;\n }\n\n auto solve = [&](int S) -> vector<int> {\n int ptr = D;\n vector<int> d(N);\n d[0] = 0, d[N - 1] = D;\n auto c2 = c;\n int l = 1, r = N - 2;\n rep(i, N - 2) {\n while (ptr > 0 && c2[ptr] == 0) ptr--;\n if ((S >> i) & 1) {\n d[r] = d[0] + ptr;\n rep(j, l) {\n int x = d[r] - d[j];\n if (x < 0 || c2[x]-- == 0) return {};\n }\n rep2(j, r + 1, N) {\n int x = d[j] - d[r];\n if (x < 0 || c2[x]-- == 0) return {};\n }\n r--;\n } else {\n d[l] = d[N - 1] - ptr;\n rep(j, l) {\n int x = d[l] - d[j];\n if (x < 0 || c2[x]-- == 0) return {};\n }\n rep2(j, r + 1, N) {\n int x = d[j] - d[l];\n if (x < 0 || c2[x]-- == 0) return {};\n }\n l++;\n }\n }\n vector<int> ret(N - 1);\n rep(i, N - 1) ret[i] = d[i + 1] - d[i];\n return ret;\n };\n\n vector<vector<int>> ans;\n rep(S, 1 << (N - 2)) {\n auto v = solve(S);\n if (!empty(v)) ans.eb(v);\n }\n\n rearrange(ans);\n\n each(e, ans) print(e);\n cout << \"-----\\n\";\n }\n}", "accuracy": 1, "time_ms": 1210, "memory_kb": 3208, "score_of_the_acc": -0.3837, "final_rank": 16 }, { "submission_id": "aoj_1307_7235078", "code_snippet": "#include \"bits/stdc++.h\"\nusing namespace std;\nusing namespace std::chrono;\ntypedef long long int llint;\ntypedef long double lldo;\n#define mp make_pair\n#define mt make_tuple\n#define pub push_back\n#define puf push_front\n#define pob pop_back\n#define pof pop_front\n#define fir first\n#define sec second\n#define res resize\n#define ins insert\n#define era erase\n#define REP(i, n) for(int i = 0;i < (n);i++)\n/*cout<<fixed<<setprecision(20);cin.tie(0);ios::sync_with_stdio(false);*/\nconst int mod=1000000007;\nconst llint inf=2.19e15+1;\nconst long double pai=3.141592653589793238462643383279502884197;\nconst long double eps=1e-10;\ntemplate <class T,class U>bool chmin(T& a,U b){if(a>b){a=b;return true;}return false;}\ntemplate <class T,class U>bool chmax(T& a,U b){if(a<b){a=b;return true;}return false;}\nllint gcd(llint a,llint b){if(a%b==0){return b;}else return gcd(b,a%b);}\nllint lcm(llint a,llint b){if(a==0){return b;}return a/gcd(a,b)*b;}\ntemplate<class T> void SO(T& ve){sort(ve.begin(),ve.end());}\ntemplate<class T> void REV(T& ve){reverse(ve.begin(),ve.end());}\ntemplate<class T>int LBI(const vector<T>&ar,T in){return lower_bound(ar.begin(),ar.end(),in)-ar.begin();}\ntemplate<class T>int UBI(const vector<T>&ar,T in){return upper_bound(ar.begin(),ar.end(),in)-ar.begin();}\nvector<vector<int>>ans;\nvoid solve(multiset<int>kyo,vector<int>La,vector<int>Ra,int n,int S){\n\t//Sが両端の長さ\n\tint i;\n\tif(n==0){\n\t\tvector<int> now;\n\t\tfor(i=1;i<La.size();i++){\n\t\t\tnow.pub(La[i]-La[i-1]);\n\t\t}\n\t\tnow.pub(S-La.back()-Ra.back());\n\t\tfor(i=Ra.size()-1;i>0;i--){\n\t\t\tnow.pub(Ra[i]-Ra[i-1]);\n\t\t}\n\t\tans.pub(now);\n\t\treturn;\n\t}\n\tint X=S-*prev(kyo.end());\n\t{\n\t\tauto kyob=kyo;\n\t\tif(La.back()>=X){goto LEND;}\n\t\tfor(i=0;i<La.size();i++){\n\t\t\tauto itr=kyob.lower_bound(X-La[i]);\n\t\t\tif(itr==kyob.end()||*itr!=X-La[i]){goto LEND;}\n\t\t\tkyob.erase(itr);\n\t\t}\n\t\tfor(i=0;i<Ra.size();i++){\n\t\t\tauto itr=kyob.lower_bound(S-Ra[i]-X);\n\t\t\tif(itr==kyob.end()||*itr!=S-Ra[i]-X){goto LEND;}\n\t\t\tkyob.erase(itr);\n\t\t}\n\t\tauto Lb=La;\n\t\tLb.pub(X);\n\t\tsolve(kyob,move(Lb),Ra,n-1,S);\n\t}\n\tLEND:\n\t{\n\t\tauto kyob=kyo;\n\t\tif(Ra.back()>=X){goto REND;}\n\t\tfor(i=0;i<Ra.size();i++){\n\t\t\tauto itr=kyob.lower_bound(X-Ra[i]);\n\t\t\tif(itr==kyob.end()||*itr!=X-Ra[i]){goto REND;}\n\t\t\tkyob.erase(itr);\n\t\t}\n\t\tfor(i=0;i<La.size();i++){\n\t\t\tauto itr=kyob.lower_bound(S-La[i]-X);\n\t\t\tif(itr==kyob.end()||*itr!=S-La[i]-X){goto REND;}\n\t\t\tkyob.erase(itr);\n\t\t}\n\t\tauto Rb=Ra;\n\t\tRb.pub(X);\n\t\tsolve(kyob,La,move(Rb),n-1,S);\n\t}\n\tREND:\n\treturn;\n}\nint main(void){\n\twhile(-1){\n\t\tint n,i,j,S;cin>>n;\n\t\tif(n==0){return 0;}\n\t\tcin>>S;\n\t\tmultiset<int>kyo;\n\t\tfor(i=1;i<n*(n-1)/2;i++){\n\t\t\tint x;cin>>x;kyo.ins(x);\n\t\t}\n\t\tsolve(kyo,{0},{0},n-2,S);\n\t\tSO(ans);\n\t\tfor(i=0;i<ans.size();i++){\n\t\t\tif(i>0&&ans[i]==ans[i-1]){continue;}\n\t\t\tfor(j=0;j<ans[i].size();j++){\n\t\t\t\tcout<<ans[i][j];\n\t\t\t\tif(j+1!=ans[i].size()){cout<<\" \";}\n\t\t\t}\n\t\t\tcout<<endl;\n\t\t}\n\t\tcout<<\"-----\"<<endl;\n\t\tans.clear();\n\t}\n\treturn 0;\n}", "accuracy": 1, "time_ms": 40, "memory_kb": 3484, "score_of_the_acc": -0.0322, "final_rank": 14 }, { "submission_id": "aoj_1307_7218711", "code_snippet": "#include <iostream>\n#include <vector>\n#include <algorithm>\nusing namespace std;\n\nint N;\nint D[409], Count1[409], Count2[409];\nvector<vector<int>> Answer;\n\nvoid Initialize() {\n\tfor (int i = 0; i < 409; i++) Count1[i] = 0;\n\tfor (int i = 0; i < 409; i++) Count2[i] = 0;\n\tAnswer.clear();\n}\n\nbool check(vector<int> List) {\n\tvector<int> Distance_List;\n\tfor (int i = 0; i < (int)List.size(); i++) {\n\t\tint sum = 0;\n\t\tfor (int j = i; j < (int)List.size(); j++) {\n\t\t\tsum += List[j];\n\t\t\tDistance_List.push_back(sum);\n\t\t}\n\t}\n\tsort(Distance_List.begin(), Distance_List.end());\n\treverse(Distance_List.begin(), Distance_List.end());\n\tfor (int i = 0; i < N*(N-1)/2; i++) {\n\t\tif (D[i] != Distance_List[i]) return false;\n\t}\n\treturn true;\n}\n\nvoid dfs(int cur_dist, vector<int> V1, vector<int> V2) {\n\tint new_dist = cur_dist;\n\tint Left = V1[V1.size() - 1];\n\tint Right = V2[V2.size() - 1];\n\twhile (new_dist >= 0) {\n\t\tif (Count1[new_dist] < Count2[new_dist]) return;\n\t\tif (Count1[new_dist] > Count2[new_dist]) break;\n\t\tnew_dist -= 1;\n\t}\n\t/*cout << \"> cur_dist = \" << cur_dist << endl;\n\tcout << \"> new_dist = \" << new_dist << endl;\n\tcout << \"> Count2[1] = \" << Count2[1] << endl;\n\tcout << \"> V1 = {\"; for (int i = 0; i < (int)V1.size(); i++) { if (i) cout << \", \"; cout << V1[i]; } cout << \"}\" << endl;\n\tcout << \"> V2 = {\"; for (int i = 0; i < (int)V2.size(); i++) { if (i) cout << \", \"; cout << V2[i]; } cout << \"}\" << endl;\n\tcout << endl;*/\n\t\n\t// When All Completed\n\tif (new_dist == -1) {\n\t\tvector<int> Final;\n\t\tfor (int i = 1; i < (int)V1.size(); i++) Final.push_back(V1[i] - V1[i-1]);\n\t\tFinal.push_back(D[0] - Left - Right);\n\t\tfor (int i = (int)V2.size() - 1; i >= 1; i--) Final.push_back(V2[i] - V2[i-1]);\n\t\tif (check(Final) == true) {\n\t\t\tAnswer.push_back(Final);\n\t\t}\n\t\treturn;\n\t}\n\t\n\t// Adding Left\n\tint new_left = D[0] - new_dist;\n\tif (new_left + Right < D[0] && new_left > Left) {\n\t\tvector<int> V3 = V1;\n\t\tfor (int i : V1) Count2[new_left - i] += 1;\n\t\tfor (int i : V2) Count2[D[0] - new_left - i] += 1;\n\t\tV3.push_back(new_left);\n\t\tdfs(new_dist, V3, V2);\n\t\tfor (int i : V1) Count2[new_left - i] -= 1;\n\t\tfor (int i : V2) Count2[D[0] - new_left - i] -= 1;\n\t}\n\t\n\t// Adding Right\n\tint new_right = D[0] - new_dist;\n\tif (new_right + Left < D[0] && new_right > Right) {\n\t\tvector<int> V3 = V2;\n\t\tfor (int i : V1) Count2[D[0] - new_right - i] += 1;\n\t\tfor (int i : V2) Count2[new_right - i] += 1;\n\t\tV3.push_back(new_right);\n\t\tdfs(new_dist, V1, V3);\n\t\tfor (int i : V1) Count2[D[0] - new_right - i] -= 1;\n\t\tfor (int i : V2) Count2[new_right - i] -= 1;\n\t}\n}\n\nint main() {\n\twhile (true) {\n\t\t// Input\n\t\tInitialize();\n\t\tcin >> N; if (N == 0) break;\n\t\tfor (int i = 0; i < N * (N-1) / 2; i++) cin >> D[i];\n\t\tfor (int i = 0; i < N * (N-1) / 2; i++) Count1[D[i]] += 1;\n\t\t\n\t\t// Solve\n\t\tvector<int> ZERO = {0};\n\t\tCount2[D[0]] += 1;\n\t\tdfs(400, ZERO, ZERO);\n\t\tsort(Answer.begin(), Answer.end());\n\t\tAnswer.erase(unique(Answer.begin(), Answer.end()), Answer.end());\n\t\t\n\t\t// Output\n\t\tfor (int i = 0; i < (int)Answer.size(); i++) {\n\t\t\tfor (int j = 0; j < N - 1; j++) {\n\t\t\t\tif (j >= 1) cout << \" \";\n\t\t\t\tcout << Answer[i][j];\n\t\t\t}\n\t\t\tcout << endl;\n\t\t}\n\t\tcout << \"-----\" << endl;\n\t}\n\treturn 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3540, "score_of_the_acc": -0.0249, "final_rank": 9 }, { "submission_id": "aoj_1307_7132580", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\ntypedef long long ll;\ntemplate<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return true; } return false; }\ntemplate<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return true; } return false; }\n#define all(x) (x).begin(),(x).end()\n#define fi first\n#define se second\n#define mp make_pair\n#define si(x) int(x.size())\nconst int mod=998244353,MAX=300005,INF=1<<30;\n\nint main(){\n \n std::ifstream in(\"text.txt\");\n std::cin.rdbuf(in.rdbuf());\n cin.tie(0);\n ios::sync_with_stdio(false);\n \n while(1){\n int N;cin>>N;\n if(N==0) break;\n set<vector<int>> ans;\n vector<int> cnsv(801);\n int ma=-1;\n for(int i=0;i<N*(N-1)/2;i++){\n int x;cin>>x;\n chmax(ma,x);\n cnsv[x]++;\n }\n cnsv[ma]--;\n \n for(int bit=0;bit<(1<<(N-2));bit++){\n int L=0,R=N-2;\n vector<int> kaku;\n for(int i=0;i<N-2;i++){\n if(bit&(1<<i)){\n kaku.push_back(R);\n R--;\n }else{\n kaku.push_back(L);\n L++;\n }\n }\n L=0;R=N-2;\n \n vector<int> cn=cnsv;\n vector<int> dis(N-1,-1);\n bool ok=true;\n int la=ma;\n for(int t=0;t<si(kaku);t++){\n while(cn[la]==0) la--;\n int x=kaku[t];\n \n if(L==x){\n int rem=ma;\n for(int i=0;i<L;i++) rem-=dis[i];\n rem-=la;\n if(rem<0||cn[rem]==0){\n ok=false;\n break;\n }\n dis[x]=rem;\n \n rem=ma;\n for(int i=0;i<=L;i++) rem-=dis[i];\n \n for(int i=N-2;i>=R;i--){\n if(rem<0||cn[rem]==0){\n ok=false;\n break;\n }\n cn[rem]--;\n rem-=dis[i];\n }\n if(!ok) break;\n \n rem=0;\n for(int i=L;i>=0;i--){\n rem+=dis[i];\n if(rem<0||cn[rem]==0){\n ok=false;\n break;\n }\n cn[rem]--;\n }\n if(!ok) break;\n \n L++;\n }else{\n int rem=ma;\n for(int i=R+1;i<N-1;i++) rem-=dis[i];\n rem-=la;\n if(rem<0||cn[rem]==0){\n ok=false;\n break;\n }\n dis[x]=rem;\n \n rem=ma;\n for(int i=R;i<N-1;i++) rem-=dis[i];\n \n for(int i=0;i<=L;i++){\n if(rem<0||cn[rem]==0){\n ok=false;\n break;\n }\n cn[rem]--;\n rem-=dis[i];\n }\n if(!ok) break;\n \n rem=0;\n for(int i=R;i<N-1;i++){\n rem+=dis[i];\n if(rem<0||cn[rem]==0){\n ok=false;\n break;\n }\n cn[rem]--;\n }\n if(!ok) break;\n \n R--;\n }\n }\n \n if(ok){\n int z=-1,sum=ma;\n for(int i=0;i<N-1;i++){\n if(dis[i]==-1){\n z=i;\n }else{\n sum-=dis[i];\n }\n }\n dis[z]=sum;\n ans.insert(dis);\n }\n }\n \n for(auto a:ans){\n for(int i=0;i<N-1;i++){\n if(i) cout<<\" \";\n cout<<a[i];\n }\n cout<<\"\\n\";\n }\n cout<<\"-----\\n\";\n }\n}", "accuracy": 1, "time_ms": 3250, "memory_kb": 3364, "score_of_the_acc": -1.0187, "final_rank": 20 }, { "submission_id": "aoj_1307_6962812", "code_snippet": "#line 1 \"e.cpp\"\n// cSpell:disable\n/*\tauthor: Kite_kuma\n\tcreated: 2022.09.14 00:17:26 */\n\n#line 1 \"SPJ-Library/data_structure/counter.hpp\"\n#include <cassert>\n#include <iostream>\n#include <map>\n\ntemplate <typename key_type, typename amount_type>\nstruct counter : public std::map<key_type, amount_type> {\n\tvoid increase(key_type key, amount_type amount = 1) {\n\t\tif(this->count(key)) {\n\t\t\tthis->operator[](key) += amount;\n\t\t\tif(!this->operator[](key)) this->erase(key);\n\t\t\treturn;\n\t\t}\n\t\tif(!amount) return;\n\t\tthis->operator[](key) = amount;\n\t}\n\n\tvoid decrease(key_type key, amount_type amount = 1) { increase(key, -amount); }\n\n\tamount_type amount(key_type key) { return this->count(key) ? this->at(key) : 0; }\n};\n\ntemplate <typename key_type, typename amount_type>\nstd::ostream &operator<<(std::ostream &os, const counter<key_type, amount_type> &ctr) {\n\tos << \"{\\n\";\n\tfor(typename counter<key_type, amount_type>::const_iterator it = ctr.begin(); it != ctr.end(); it++) {\n\t\tos << \"\\t[\" << it->first << \"] : \" << it->second << '\\n';\n\t}\n\tos << \"}\";\n\treturn os;\n}\n#line 2 \"SPJ-Library/template/kuma.hpp\"\n\n#line 2 \"SPJ-Library/template/basic_func.hpp\"\n\n#include <algorithm>\n#line 6 \"SPJ-Library/template/basic_func.hpp\"\n#include <vector>\n\ntemplate <class T0, class T1, class T2>\ninline bool in_range(T0 x, T1 lef, T2 rig) {\n\treturn ((lef <= x) && (x < rig));\n}\n\ntemplate <class T>\ninline bool chmin(T &a, T b) {\n\tif(a > b) {\n\t\ta = b;\n\t\treturn true;\n\t}\n\treturn false;\n}\ntemplate <class T>\ninline bool chmax(T &a, T b) {\n\tif(a < b) {\n\t\ta = b;\n\t\treturn true;\n\t}\n\treturn false;\n}\n\nvoid Yes(bool flag = true) { std::cout << (flag ? \"Yes\" : \"No\") << '\\n'; }\nvoid YES(bool flag = true) { std::cout << (flag ? \"YES\" : \"NO\") << '\\n'; }\n\ntemplate <class T>\nvoid drop(T answer) {\n\tstd::cout << answer << '\\n';\n\texit(0);\n}\n\ntemplate <class T>\nvoid vout(std::vector<T> const &v, bool vertically = 0) {\n\tif(vertically) {\n\t\tfor(auto const &a : v) {\n\t\t\tstd::cout << a << '\\n';\n\t\t}\n\t\treturn;\n\t}\n\tfor(auto it = v.begin(); it != v.end(); it++) {\n\t\tstd::cout << (*it);\n\t\tif(it != v.end() - 1) {\n\t\t\tstd::cout << ' ';\n\t\t}\n\t}\n\tstd::cout << '\\n';\n\treturn;\n}\n\ninline void print() { std::cout << '\\n'; }\ntemplate <class T>\ninline void print(const T &x) {\n\tstd::cout << x << '\\n';\n\treturn;\n}\ntemplate <typename Head, typename... Tail>\nvoid print(const Head &H, const Tail &... T) {\n\tstd::cout << H << \" \";\n\tprint(T...);\n}\n\ntemplate <class T>\nvoid add(std::vector<T> &v, T value) {\n\tfor(auto &a : v) a += value;\n\treturn;\n}\n\ntemplate <class T>\nT greatest_lower_multiple(T x, T d) {\n\tif(d == 0) return 0;\n\tif(d < 0) d *= -1;\n\tT y = x % d;\n\tif(y < 0) y += d;\n\treturn x - y;\n}\n\ntemplate <class T>\nT least_upper_multiple(T x, T d) {\n\treturn -greatest_lower_multiple(-x, d);\n}\n\n// ceil(a / b);\ntemplate <class T>\nT dup(T a, T b) {\n\tassert(b);\n\tif(b < 0) {\n\t\ta *= -1;\n\t\tb *= -1;\n\t}\n\treturn least_upper_multiple(a, b) / b;\n}\n\nlong long pow_ll(long long a, long long n) {\n\tassert(n >= 0LL);\n\tif(n == 0) return 1LL;\n\tif(a == 0) return 0LL;\n\tif(a == 1) return 1LL;\n\tif(a == -1) return (n & 1LL) ? -1LL : 1LL;\n\tlong long res = 1;\n\twhile(n > 1LL) {\n\t\tif(n & 1LL) res *= a;\n\t\ta *= a;\n\t\tn >>= 1;\n\t}\n\treturn res * a;\n}\n\nlong long modpow(long long a, long long n, long long mod) {\t // a^n mod\n\tassert(n >= 0 && mod);\n\tif(mod == 1) return 0LL;\n\tlong long res = 1;\n\ta %= mod;\n\twhile(n > 0) {\n\t\tif(n & 1) res = res * a % mod;\n\t\ta = a * a % mod;\n\t\tn >>= 1;\n\t}\n\treturn res < 0 ? res + mod : res;\n}\n\n// return x which satisfies a * x % mod == gcd(a, mod)\n// not (mod | a)\nlong long modinv(long long a, const long long mod) {\n\tlong long b = mod, u = 1, v = 0;\n\twhile(b) {\n\t\tlong long t = a / b;\n\t\ta -= t * b;\n\t\tstd::swap(a, b);\n\t\tu -= t * v;\n\t\tstd::swap(u, v);\n\t}\n\tu %= mod;\n\tif(u < 0) u += mod;\n\treturn u;\n}\n\ntemplate <class T>\nint lb(const std::vector<T> &a, const T x) {\n\treturn (int)std::distance(a.begin(), std::lower_bound(a.begin(), a.end(), x));\n}\ntemplate <class T>\nint ub(const std::vector<T> &a, const T x) {\n\treturn (int)std::distance(a.begin(), std::upper_bound(a.begin(), a.end(), x));\n}\ntemplate <class T>\nvoid unq_sort(std::vector<T> &a) {\n\tstd::sort(a.begin(), a.end());\n\ta.erase(std::unique(a.begin(), a.end()), a.end());\n}\ntemplate <class T>\nstd::vector<int> press(const std::vector<T> &a) {\n\tstd::vector<T> vec = a;\n\tunq_sort(vec);\n\tstd::vector<int> ret;\n\tfor(const auto &v : a) ret.push_back(lb(vec, v));\n\treturn ret;\n}\n#line 1 \"SPJ-Library/template/header.hpp\"\n#include <bits/stdc++.h>\n#line 2 \"SPJ-Library/template/io.hpp\"\n\n#line 4 \"SPJ-Library/template/io.hpp\"\n\n#line 8 \"SPJ-Library/template/debug.hpp\"\n\n#line 3 \"SPJ-Library/template/constants.hpp\"\n\nconstexpr int inf = 1000'000'000;\nconstexpr long long INF = 1'000'000'000'000'000'000LL;\nconstexpr int mod_1000000007 = 1000000007;\nconstexpr int mod_998244353 = 998244353;\nconst long double pi = acosl(-1.);\nconstexpr int dx[8] = {1, 0, -1, 0, 1, -1, -1, 1};\nconstexpr int dy[8] = {0, 1, 0, -1, 1, 1, -1, -1};\n#line 10 \"SPJ-Library/template/debug.hpp\"\n\nnamespace viewer {\n\ntemplate <typename T, typename U>\nvoid view(const std::pair<T, U> &p);\n\ntemplate <class T0, class T1, class T2>\nvoid view(const std::tuple<T0, T1, T2> &p);\n\ntemplate <class T0, class T1, class T2, class T3>\nvoid view(const std::tuple<T0, T1, T2, T3> &p);\n\nvoid view(const long long &e);\n\nvoid view(const int &e);\n\ntemplate <typename T>\nvoid view(const T &e);\n\ntemplate <typename T>\nvoid view(const std::set<T> &s);\n\ntemplate <typename T>\nvoid view(const std::unordered_set<T> &s);\n\ntemplate <typename T>\nvoid view(const std::multiset<T> &s);\n\ntemplate <typename T>\nvoid view(const std::unordered_multiset<T> &s);\n\ntemplate <typename T>\nvoid view(const std::vector<T> &v);\n\ntemplate <typename T, std::size_t ary_size>\nvoid view(const std::array<T, ary_size> &v);\n\ntemplate <typename T>\nvoid view(const std::vector<std::vector<T>> &vv);\n\ntemplate <typename T, typename U>\nvoid view(const std::vector<std::pair<T, U>> &v);\n\ntemplate <class T0, class T1, class T2>\nvoid view(const std::vector<std::tuple<T0, T1, T2>> &v);\n\ntemplate <class T0, class T1, class T2, class T3>\nvoid view(const std::vector<std::tuple<T0, T1, T2, T3>> &v);\n\ntemplate <typename map_type>\nvoid view_map_container(const map_type &m);\n\ntemplate <typename T, typename U>\nvoid view(const std::map<T, U> &m);\n\ntemplate <typename T, typename U>\nvoid view(const std::unordered_map<T, U> &m);\n\ntemplate <typename container_type>\nvoid view_container(const container_type &c, bool vertically = false) {\n\ttypename container_type::const_iterator begin = c.begin();\n\tconst typename container_type::const_iterator end = c.end();\n\tif(vertically) {\n\t\tstd::cerr << \"{\\n\";\n\t\twhile(begin != end) {\n\t\t\tstd::cerr << '\\t';\n\t\t\tview(*(begin++));\n\t\t\tif(begin != end) std::cerr << ',';\n\t\t\tstd::cerr << '\\n';\n\t\t}\n\t\tstd::cerr << '}';\n\t\treturn;\n\t}\n\tstd::cerr << \"{ \";\n\twhile(begin != end) {\n\t\tview(*(begin++));\n\t\tif(begin != end) std::cerr << ',';\n\t\tstd::cerr << ' ';\n\t}\n\tstd::cerr << '}';\n}\n\ntemplate <typename T, typename U>\nvoid view(const std::pair<T, U> &p) {\n\tstd::cerr << '(';\n\tview(p.first);\n\tstd::cerr << \", \";\n\tview(p.second);\n\tstd::cerr << ')';\n}\n\ntemplate <class T0, class T1, class T2>\nvoid view(const std::tuple<T0, T1, T2> &p) {\n\tstd::cerr << '(';\n\tview(std::get<0>(p));\n\tstd::cerr << \", \";\n\tview(std::get<1>(p));\n\tstd::cerr << \", \";\n\tview(std::get<2>(p));\n\tstd::cerr << ')';\n}\n\ntemplate <class T0, class T1, class T2, class T3>\nvoid view(const std::tuple<T0, T1, T2, T3> &p) {\n\tstd::cerr << '(';\n\tview(std::get<0>(p));\n\tstd::cerr << \", \";\n\tview(std::get<1>(p));\n\tstd::cerr << \", \";\n\tview(std::get<2>(p));\n\tstd::cerr << \", \";\n\tview(std::get<3>(p));\n\tstd::cerr << ')';\n}\n\nvoid view(const long long &e) {\n\tif(e == INF)\n\t\tstd::cerr << \"INF\";\n\telse if(e == -INF)\n\t\tstd::cerr << \"-INF\";\n\telse\n\t\tstd::cerr << e;\n}\n\nvoid view(const int &e) {\n\tif(e == inf)\n\t\tstd::cerr << \"inf\";\n\telse if(e == -inf)\n\t\tstd::cerr << \"-inf\";\n\telse\n\t\tstd::cerr << e;\n}\n\ntemplate <typename T>\nvoid view(const T &e) {\n\tstd::cerr << e;\n}\n\ntemplate <typename T>\nvoid view(const std::set<T> &s) {\n\tview_container(s);\n}\n\ntemplate <typename T>\nvoid view(const std::unordered_set<T> &s) {\n\tview_container(s);\n}\n\ntemplate <typename T>\nvoid view(const std::multiset<T> &s) {\n\tview_container(s);\n}\n\ntemplate <typename T>\nvoid view(const std::unordered_multiset<T> &s) {\n\tview_container(s);\n}\n\ntemplate <typename T>\nvoid view(const std::vector<T> &v) {\n\tview_container(v);\n}\n\ntemplate <typename T, std::size_t ary_size>\nvoid view(const std::array<T, ary_size> &v) {\n\tview_container(v);\n}\n\ntemplate <typename T>\nvoid view(const std::vector<std::vector<T>> &vv) {\n\tview_container(vv, true);\n}\n\ntemplate <typename T, typename U>\nvoid view(const std::vector<std::pair<T, U>> &v) {\n\tview_container(v, true);\n}\n\ntemplate <class T0, class T1, class T2>\nvoid view(const std::vector<std::tuple<T0, T1, T2>> &v) {\n\tview_container(v, true);\n}\n\ntemplate <class T0, class T1, class T2, class T3>\nvoid view(const std::vector<std::tuple<T0, T1, T2, T3>> &v) {\n\tview_container(v, true);\n}\n\ntemplate <typename map_type>\nvoid view_map_container(const map_type &m) {\n\tstd::cerr << \"{\\n\";\n\tfor(typename map_type::const_iterator it = m.begin(); it != m.end(); it++) {\n\t\tstd::cerr << \"\\t[\";\n\t\tview(it->first);\n\t\tstd::cerr << \"] : \";\n\t\tview(it->second);\n\t\tstd::cerr << '\\n';\n\t}\n\tstd::cerr << \"}\";\n}\n\ntemplate <typename T, typename U>\nvoid view(const std::map<T, U> &m) {\n\tview_map_container(m);\n}\n\ntemplate <typename T, typename U>\nvoid view(const std::unordered_map<T, U> &m) {\n\tview_map_container(m);\n}\n\n} // namespace viewer\n\n// when compiling : g++ foo.cpp -DLOCAL\n#ifdef LOCAL\nvoid debug_out() {}\ntemplate <typename T>\nvoid debug_out(const T &x) {\n\tviewer::view(x);\n}\ntemplate <typename Head, typename... Tail>\nvoid debug_out(const Head &H, const Tail &... T) {\n\tviewer::view(H);\n\tstd::cerr << \", \";\n\tdebug_out(T...);\n}\n#define debug(...) \\\n\tdo { \\\n\t\tstd::cerr << __LINE__ << \" [\" << #__VA_ARGS__ << \"] : [\"; \\\n\t\tdebug_out(__VA_ARGS__); \\\n\t\tstd::cerr << \"]\\n\"; \\\n\t} while(0)\n#else\n#define debug(...) (void(0))\n#endif\n#line 2 \"SPJ-Library/template/scanner.hpp\"\n\n#line 6 \"SPJ-Library/template/scanner.hpp\"\n\nnamespace scanner {\ntemplate <class T>\nvoid scan(T &a) {\n\tstd::cin >> a;\n}\n\ntemplate <class T, class L>\nvoid scan(std::pair<T, L> &p) {\n\tscan(p.first);\n\tscan(p.second);\n}\n\ntemplate <class T0, class T1, class T2>\nvoid scan(std::tuple<T0, T1, T2> &p) {\n\tT0 t0;\n\tT1 t1;\n\tT2 t2;\n\tscan(t0);\n\tscan(t1);\n\tscan(t2);\n\tp = std::make_tuple(t0, t1, t2);\n}\n\ntemplate <class T0, class T1, class T2, class T3>\nvoid scan(std::tuple<T0, T1, T2, T3> &p) {\n\tT0 t0;\n\tT1 t1;\n\tT2 t2;\n\tT3 t3;\n\tscan(t0);\n\tscan(t1);\n\tscan(t2);\n\tscan(t3);\n\tp = std::make_tuple(t0, t1, t2, t3);\n}\n\ntemplate <class T>\nvoid scan(std::vector<T> &a) {\n\tfor(auto &i : a) scan(i);\n}\n\nvoid INPUT() {}\ntemplate <class Head, class... Tail>\nvoid INPUT(Head &head, Tail &... tail) {\n\tscan(head);\n\tINPUT(tail...);\n}\n} // namespace scanner\n#line 7 \"SPJ-Library/template/io.hpp\"\n\n#define VEC(type, name, size) \\\n\tvector<type> name(size); \\\n\tscanner::INPUT(name)\n#define VVEC(type, name, h, w) \\\n\tvector<vector<type>> name(h, vector<type>(w)); \\\n\tscanner::INPUT(name)\n#define INT(...) \\\n\tint __VA_ARGS__; \\\n\tscanner::INPUT(__VA_ARGS__)\n#define LL(...) \\\n\tlong long __VA_ARGS__; \\\n\tscanner::INPUT(__VA_ARGS__)\n#define STR(...) \\\n\tstring __VA_ARGS__; \\\n\tscanner::INPUT(__VA_ARGS__)\n#define CHR(...) \\\n\tchar __VA_ARGS__; \\\n\tscanner::INPUT(__VA_ARGS__)\n#define DBL(...) \\\n\tdouble __VA_ARGS__; \\\n\tscanner::INPUT(__VA_ARGS__)\n#define LD(...) \\\n\tlong double __VA_ARGS__; \\\n\tscanner::INPUT(__VA_ARGS__)\n#define TPL3(type0, type1, type2, name) \\\n\tstd::tuple<type0, type1, type2> name; \\\n\tscanner::INPUT(name);\n#define TPL4(type0, type1, type2, type3, name) \\\n\tstd::tuple<type0, type1, type2, type3> name; \\\n\tscanner::INPUT(name);\n\ntemplate <typename T1, typename T2>\nstd::istream &operator>>(std::istream &is, std::pair<T1, T2> &p) {\n\tis >> p.first >> p.second;\n\treturn is;\n}\n\ntemplate <typename T1, typename T2>\nstd::ostream &operator<<(std::ostream &os, const std::pair<T1, T2> &p) {\n\tos << p.first << ' ' << p.second;\n\treturn os;\n}\n\nstruct fast_io {\n\tfast_io() {\n\t\tstd::ios::sync_with_stdio(false);\n\t\tstd::cin.tie(nullptr);\n\t\tstd::cout << std::fixed << std::setprecision(15);\n\t\tsrand((unsigned)time(NULL));\n\t}\n} fast_io_;\n#line 2 \"SPJ-Library/template/macros.hpp\"\n\n#define foa(s, v) for(auto &s : v)\n#define all(v) (v).begin(), (v).end()\n#define rall(v) (v).rbegin(), (v).rend()\n#define pcnt(n) __builtin_popcountll((long long)n)\n\n#define REPname(a, b, c, d, e, ...) e\n#define rep(...) REPname(__VA_ARGS__, REP3, REP2, REP1, REP0)(__VA_ARGS__)\n#define REP0(x) for(int Counter_in_rep_macro = 0; Counter_in_rep_macro < (x); ++Counter_in_rep_macro)\n#define REP1(i, x) for(int i = 0; i < (x); ++i)\n#define REP2(i, l, r) for(int i = (l); i < (r); ++i)\n#define REP3(i, l, r, c) for(int i = (l); i < (r); i += (c))\n#define DREPname(a, b, c, d, e, ...) e\n#define drep(...) DREPname(__VA_ARGS__, DREP3, DREP2, DREP1)(__VA_ARGS__)\n#define DREP1(i, x) for(int i = (x)-1; i >= 0; --i)\n#define DREP2(i, l, r) for(int i = (r)-1; i >= (l); --i)\n#define DREP3(i, l, r, c) for(int i = (r)-1; i >= (l); i -= (c))\n\nusing ll = long long;\nusing ld = long double;\nusing ull = unsigned long long;\nusing vi = std::vector<int>;\nusing vvi = std::vector<std::vector<int>>;\nusing vvvi = std::vector<std::vector<std::vector<int>>>;\nusing vll = std::vector<ll>;\nusing vvll = std::vector<vll>;\nusing vvvll = std::vector<vvll>;\ntemplate <class T = ll>\nusing V = std::vector<T>;\ntemplate <class T = ll>\nusing VV = V<V<T>>;\ntemplate <class T = ll>\nusing VVV = V<V<V<T>>>;\ntemplate <class T = ll>\nusing pqup = std::priority_queue<T, std::vector<T>, std::greater<T>>;\ntemplate <class T = ll>\nusing pqdn = std::priority_queue<T>;\n#line 7 \"SPJ-Library/template/kuma.hpp\"\n\nusing namespace std;\n#line 7 \"e.cpp\"\n\nvoid solve(int n) {\n\tint pair_count = n * (n - 1) / 2;\n\tVEC(int, lens, pair_count);\n\tif(n == 2) {\n\t\tprint(lens.front());\n\t\treturn;\n\t}\n\tsort(all(lens));\n\tconst int longest = lens.back();\n\n\tcounter<int, int> c;\n\tfor(auto len : lens) c.increase(len, 1);\n\n\tc.decrease(longest, 1);\n\n\tvector<int> slices = {0, longest};\n\n\tvvi answers;\n\n\tauto dfs = [&](auto self) -> void {\n\t\tif(c.empty()) {\n\t\t\tauto slices_copy = slices;\n\t\t\tsort(all(slices));\n\t\t\trep(i, n - 1) { slices[i] = slices[i + 1] - slices[i]; }\n\t\t\tslices.pop_back();\n\t\t\tanswers.push_back(slices);\n\t\t\tswap(slices_copy, slices);\n\t\t\treturn;\n\t\t}\n\n\t\tconst int tmp_longest = prev(c.end())->first;\n\n\t\tauto add_slice = [&](int from_left) -> bool {\n\t\t\tslices.push_back(from_left);\n\t\t\tbool ret = true;\n\t\t\trep(i, int(slices.size()) - 1) {\n\t\t\t\tint diff = abs(from_left - slices[i]);\n\t\t\t\tif(c.amount(diff) == 0) {\n\t\t\t\t\tret = false;\n\t\t\t\t}\n\t\t\t\tc.decrease(diff, 1);\n\t\t\t}\n\t\t\treturn ret;\n\t\t};\n\n\t\tauto rem_slice = [&]() -> void {\n\t\t\tconst int from_left = slices.back();\n\t\t\trep(i, int(slices.size()) - 1) {\n\t\t\t\tint diff = abs(from_left - slices[i]);\n\t\t\t\tc.increase(diff, 1);\n\t\t\t}\n\t\t\tslices.pop_back();\n\t\t\treturn;\n\t\t};\n\n\t\tauto chk = [&](int from_left) -> void {\n\t\t\tif(add_slice(from_left)) self(self);\n\t\t\trem_slice();\n\t\t\treturn;\n\t\t};\n\n\t\tchk(tmp_longest);\n\t\tchk(longest - tmp_longest);\n\n\t\treturn;\n\t};\n\n\tdfs(dfs);\n\n\tunq_sort(answers);\n\tfoa(vec, answers) { vout(vec, 0); }\n\treturn;\n}\nint main() {\n\tint n;\n\twhile(cin >> n && n) {\n\t\t// if(n > 12) break;\n\t\tsolve(n);\n\t\trep(5) cout << '-';\n\t\tcout << '\\n';\n\t}\n\treturn 0;\n}", "accuracy": 1, "time_ms": 30, "memory_kb": 3228, "score_of_the_acc": -0.0202, "final_rank": 7 }, { "submission_id": "aoj_1307_6813582", "code_snippet": "#pragma region Macros\n#if defined(noimi) && defined(_GLIBCXX_DEBUG) && defined(_GLIBCXX_DEBUG_PEDANTIC)\n// #pragma comment(linker, \"/stack:200000000\")\n#include <stdc++.h>\n#pragma GCC optimize(\"O3\")\n#else\n#pragma GCC optimize(\"Ofast\")\n// #pragma GCC optimize(\"unroll-loops\")\n// #pragma GCC target(\"popcnt\")\n// #pragma GCC target(\"sse,sse2,sse3,ssse3,sse4,popcnt,fma,abm,mmx,avx,avx2,tune=native\")\n// #pragma GCC target(\"sse,sse2,sse3,ssse3,sse4,popcnt,fma,abm,mmx,avx,avx2\")\n// #pragma GCC target(\"avx2\")\n// #include <bits/stdc++.h>\n#include <bits/stdc++.h>\n#endif\n#include <cstdint>\n#include <immintrin.h>\n#include <variant>\n\n#ifdef noimi\n#define oj_local(a, b) b\n#else\n#define oj_local(a, b) a\n#endif\n\n#define LOCAL if(oj_local(0, 1))\n#define OJ if(oj_local(1, 0))\n\nusing namespace std;\nusing ll = long long;\nusing ull = unsigned long long int;\nusing i128 = __int128_t;\nusing pii = pair<int, int>;\nusing pll = pair<ll, ll>;\nusing ld = long double;\ntemplate <typename T> using vc = vector<T>;\ntemplate <typename T> using vvc = vector<vc<T>>;\ntemplate <typename T> using vvvc = vector<vvc<T>>;\nusing vi = vc<int>;\nusing vl = vc<ll>;\nusing vpi = vc<pii>;\nusing vpl = vc<pll>;\ntemplate <class T> using pq = priority_queue<T>;\ntemplate <class T> using pqg = priority_queue<T, vector<T>, greater<T>>;\ntemplate <typename T> int si(const T &x) { return x.size(); }\ntemplate <class T, class S> inline bool chmax(T &a, const S &b) { return (a < b ? a = b, 1 : 0); }\ntemplate <class T, class S> inline bool chmin(T &a, const S &b) { return (a > b ? a = b, 1 : 0); }\nvi iota(int n) {\n vi a(n);\n return iota(a.begin(), a.end(), 0), a;\n}\ntemplate <typename T> vi iota(const vector<T> &a, bool greater = false) {\n vi res(a.size());\n iota(res.begin(), res.end(), 0);\n sort(res.begin(), res.end(), [&](int i, int j) {\n if(greater) return a[i] > a[j];\n return a[i] < a[j];\n });\n return res;\n}\n\n// macros\n#define overload5(a, b, c, d, e, name, ...) name\n#define overload4(a, b, c, d, name, ...) name\n#define endl '\\n'\n#define REP0(n) for(ll jidlsjf = 0; jidlsjf < n; ++jidlsjf)\n#define REP1(i, n) for(ll i = 0; i < (n); ++i)\n#define REP2(i, a, b) for(ll i = (a); i < (b); ++i)\n#define REP3(i, a, b, c) for(ll i = (a); i < (b); i += (c))\n#define rep(...) overload4(__VA_ARGS__, REP3, REP2, REP1, REP0)(__VA_ARGS__)\n#define per0(n) for(int jidlsjf = 0; jidlsjf < (n); ++jidlsjf)\n#define per1(i, n) for(ll i = (n)-1; i >= 0; --i)\n#define per2(i, a, b) for(ll i = (a)-1; i >= b; --i)\n#define per3(i, a, b, c) for(ll i = (a)-1; i >= (b); i -= (c))\n#define per(...) overload4(__VA_ARGS__, per3, per2, per1, per0)(__VA_ARGS__)\n#define fore0(a) rep(a.size())\n#define fore1(i, a) for(auto &&i : a)\n#define fore2(a, b, v) for(auto &&[a, b] : v)\n#define fore3(a, b, c, v) for(auto &&[a, b, c] : v)\n#define fore4(a, b, c, d, v) for(auto &&[a, b, c, d] : v)\n#define fore(...) overload5(__VA_ARGS__, fore4, fore3, fore2, fore1, fore0)(__VA_ARGS__)\n#define fi first\n#define se second\n#define pb push_back\n#define ppb pop_back\n#define ppf pop_front\n#define eb emplace_back\n#define drop(s) cout << #s << endl, exit(0)\n#define si(c) (int)(c).size()\n#define lb(c, x) distance((c).begin(), lower_bound(all(c), (x)))\n#define ub(c, x) distance((c).begin(), upper_bound(all(c), (x)))\n#define rng(v, l, r) v.begin() + l, v.begin() + r\n#define all(c) begin(c), end(c)\n#define rall(c) rbegin(c), rend(c)\n#define SORT(v) sort(all(v))\n#define REV(v) reverse(all(v))\n#define UNIQUE(x) SORT(x), x.erase(unique(all(x)), x.end())\ntemplate <typename T = ll, typename S> T SUM(const S &v) { return accumulate(all(v), T(0)); }\n#define MIN(v) *min_element(all(v))\n#define MAX(v) *max_element(all(v))\n#define overload2(_1, _2, name, ...) name\n#define vec(type, name, ...) vector<type> name(__VA_ARGS__)\n#define vv(type, name, h, ...) vector<vector<type>> name(h, vector<type>(__VA_ARGS__))\n#define vvv(type, name, h, w, ...) vector<vector<vector<type>>> name(h, vector<vector<type>>(w, vector<type>(__VA_ARGS__)))\n#define vvvv(type, name, a, b, c, ...) \\\n vector<vector<vector<vector<type>>>> name(a, vector<vector<vector<type>>>(b, vector<vector<type>>(c, vector<type>(__VA_ARGS__))))\nconstexpr pii dx4[4] = {pii{1, 0}, pii{0, 1}, pii{-1, 0}, pii{0, -1}};\nconstexpr pii dx8[8] = {{1, 0}, {1, 1}, {0, 1}, {-1, 1}, {-1, 0}, {-1, -1}, {0, -1}, {1, -1}};\n\nnamespace yesno_impl {\nconst string YESNO[2] = {\"NO\", \"YES\"};\nconst string YesNo[2] = {\"No\", \"Yes\"};\nconst string yesno[2] = {\"no\", \"yes\"};\nconst string firstsecond[2] = {\"second\", \"first\"};\nconst string FirstSecond[2] = {\"Second\", \"First\"};\nconst string possiblestr[2] = {\"impossible\", \"possible\"};\nvoid YES(bool t = 1) { cout << YESNO[t] << endl; }\nvoid NO(bool t = 1) { YES(!t); }\nvoid Yes(bool t = 1) { cout << YesNo[t] << endl; }\nvoid No(bool t = 1) { Yes(!t); }\nvoid yes(bool t = 1) { cout << yesno[t] << endl; }\nvoid no(bool t = 1) { yes(!t); }\nvoid first(bool t = 1) { cout << firstsecond[t] << endl; }\nvoid First(bool t = 1) { cout << FirstSecond[t] << endl; }\nvoid possible(bool t = 1) { cout << possiblestr[t] << endl; }\n}; // namespace yesno_impl\nusing namespace yesno_impl;\n\n#define INT(...) \\\n int __VA_ARGS__; \\\n IN(__VA_ARGS__)\n#define LL(...) \\\n ll __VA_ARGS__; \\\n IN(__VA_ARGS__)\n#define STR(...) \\\n string __VA_ARGS__; \\\n IN(__VA_ARGS__)\n#define CHR(...) \\\n char __VA_ARGS__; \\\n IN(__VA_ARGS__)\n#define DBL(...) \\\n double __VA_ARGS__; \\\n IN(__VA_ARGS__)\n#define VEC(type, name, size) \\\n vector<type> name(size); \\\n IN(name)\n#define VEC2(type, name1, name2, size) \\\n vector<type> name1(size), name2(size); \\\n for(int i = 0; i < size; i++) IN(name1[i], name2[i])\n#define VEC3(type, name1, name2, name3, size) \\\n vector<type> name1(size), name2(size), name3(size); \\\n for(int i = 0; i < size; i++) IN(name1[i], name2[i], name3[i])\n#define VEC4(type, name1, name2, name3, name4, size) \\\n vector<type> name1(size), name2(size), name3(size), name4(size); \\\n for(int i = 0; i < size; i++) IN(name1[i], name2[i], name3[i], name4[i]);\n#define VV(type, name, h, w) \\\n vector<vector<type>> name(h, vector<type>(w)); \\\n IN(name)\nint scan() { return getchar(); }\nvoid scan(int &a) { cin >> a; }\nvoid scan(long long &a) { cin >> a; }\nvoid scan(char &a) { cin >> a; }\nvoid scan(double &a) { cin >> a; }\nvoid scan(string &a) { cin >> a; }\ntemplate <class T, class S> void scan(pair<T, S> &p) { scan(p.first), scan(p.second); }\ntemplate <class T> void scan(vector<T> &);\ntemplate <class T> void scan(vector<T> &a) {\n for(auto &i : a) scan(i);\n}\ntemplate <class T> void scan(T &a) { cin >> a; }\nvoid IN() {}\ntemplate <class Head, class... Tail> void IN(Head &head, Tail &...tail) {\n scan(head);\n IN(tail...);\n}\n\ntemplate <typename T, typename S> T ceil(T x, S y) {\n assert(y);\n return (y < 0 ? ceil(-x, -y) : (x > 0 ? (x + y - 1) / y : x / y));\n}\n\ntemplate <typename T, typename S> T floor(T x, S y) {\n assert(y);\n return (y < 0 ? floor(-x, -y) : (x > 0 ? x / y : x / y - (x % y == 0 ? 0 : 1)));\n}\ntemplate <class T> T POW(T x, int n) {\n T res = 1;\n for(; n; n >>= 1, x *= x)\n if(n & 1) res *= x;\n return res;\n}\ntemplate <class T, class S> T POW(T x, S n, const ll &mod) {\n T res = 1;\n x %= mod;\n for(; n; n >>= 1, x = x * x % mod)\n if(n & 1) res = res * x % mod;\n return res;\n}\nvector<pll> factor(ll x) {\n vector<pll> ans;\n for(ll i = 2; i * i <= x; i++)\n if(x % i == 0) {\n ans.push_back({i, 1});\n while((x /= i) % i == 0) ans.back().second++;\n }\n if(x != 1) ans.push_back({x, 1});\n return ans;\n}\ntemplate <class T> vector<T> divisor(T x) {\n vector<T> ans;\n for(T i = 1; i * i <= x; i++)\n if(x % i == 0) {\n ans.pb(i);\n if(i * i != x) ans.pb(x / i);\n }\n return ans;\n}\ntemplate <typename T> void zip(vector<T> &x) {\n vector<T> y = x;\n UNIQUE(y);\n for(int i = 0; i < x.size(); ++i) { x[i] = lb(y, x[i]); }\n}\ntemplate <class S> void fold_in(vector<S> &v) {}\ntemplate <typename Head, typename... Tail, class S> void fold_in(vector<S> &v, Head &&a, Tail &&...tail) {\n for(auto e : a) v.emplace_back(e);\n fold_in(v, tail...);\n}\ntemplate <class S> void renumber(vector<S> &v) {}\ntemplate <typename Head, typename... Tail, class S> void renumber(vector<S> &v, Head &&a, Tail &&...tail) {\n for(auto &&e : a) e = lb(v, e);\n renumber(v, tail...);\n}\ntemplate <class S, class... Args> vector<S> zip(vector<S> &head, Args &&...args) {\n vector<S> v;\n fold_in(v, head, args...);\n sort(all(v)), v.erase(unique(all(v)), v.end());\n renumber(v, head, args...);\n return v;\n}\ntemplate <typename T> vector<T> RUI(const vector<T> &v) {\n vector<T> res(v.size() + 1);\n for(int i = 0; i < v.size(); i++) res[i + 1] = res[i] + v[i];\n return res;\n}\n// 反時計周りに 90 度回転\ntemplate <typename T> void rot(vector<vector<T>> &v) {\n if(empty(v)) return;\n int n = v.size(), m = v[0].size();\n vector res(m, vector<T>(n));\n rep(i, n) rep(j, m) res[m - 1 - j][i] = v[i][j];\n v.swap(res);\n}\n// x in [l, r)\ntemplate <class T, class S> bool inc(const T &x, const S &l, const S &r) { return l <= x and x < r; }\n\n// 便利関数\nconstexpr ll ten(int n) { return n == 0 ? 1 : ten(n - 1) * 10; }\nconstexpr ll tri(ll n) { return n * (n + 1) / 2; }\n// l + ... + r\nconstexpr ll tri(ll l, ll r) { return (l + r) * (r - l + 1) / 2; }\nll max(int x, ll y) { return max((ll)x, y); }\nll max(ll x, int y) { return max(x, (ll)y); }\nint min(int x, ll y) { return min((ll)x, y); }\nint min(ll x, int y) { return min(x, (ll)y); }\n// bit 演算系\nll pow2(int i) { return 1LL << i; }\nint topbit(signed t) { return t == 0 ? -1 : 31 - __builtin_clz(t); }\nint topbit(ll t) { return t == 0 ? -1 : 63 - __builtin_clzll(t); }\nint lowbit(signed a) { return a == 0 ? 32 : __builtin_ctz(a); }\nint lowbit(ll a) { return a == 0 ? 64 : __builtin_ctzll(a); }\n// int allbit(int n) { return (1 << n) - 1; }\nconstexpr ll mask(int n) { return (1LL << n) - 1; }\n// int popcount(signed t) { return __builtin_popcount(t); }\n// int popcount(ll t) { return __builtin_popcountll(t); }\nint popcount(uint64_t t) { return __builtin_popcountll(t); }\nstatic inline uint64_t popcount64(uint64_t x) {\n uint64_t m1 = 0x5555555555555555ll;\n uint64_t m2 = 0x3333333333333333ll;\n uint64_t m4 = 0x0F0F0F0F0F0F0F0Fll;\n uint64_t h01 = 0x0101010101010101ll;\n\n x -= (x >> 1) & m1;\n x = (x & m2) + ((x >> 2) & m2);\n x = (x + (x >> 4)) & m4;\n\n return (x * h01) >> 56;\n}\nbool ispow2(int i) { return i && (i & -i) == i; }\n\nll rnd(ll l, ll r) { //[l, r)\n#ifdef noimi\n static mt19937_64 gen;\n#else\n static mt19937_64 gen(chrono::steady_clock::now().time_since_epoch().count());\n#endif\n return uniform_int_distribution<ll>(l, r - 1)(gen);\n}\nll rnd(ll n) { return rnd(0, n); }\n\ntemplate <class t> void random_shuffle(vc<t> &a) { rep(i, si(a)) swap(a[i], a[rnd(0, i + 1)]); }\n\nint in() {\n int x;\n cin >> x;\n return x;\n}\nll lin() {\n unsigned long long x;\n cin >> x;\n return x;\n}\n\ntemplate <class T, class S> pair<T, S> operator-(const pair<T, S> &x) { return pair<T, S>(-x.first, -x.second); }\ntemplate <class T, class S> pair<T, S> operator-(const pair<T, S> &x, const pair<T, S> &y) { return pair<T, S>(x.fi - y.fi, x.se - y.se); }\ntemplate <class T, class S> pair<T, S> operator+(const pair<T, S> &x, const pair<T, S> &y) { return pair<T, S>(x.fi + y.fi, x.se + y.se); }\ntemplate <class T> pair<T, T> operator&(const pair<T, T> &l, const pair<T, T> &r) { return pair<T, T>(max(l.fi, r.fi), min(l.se, r.se)); }\ntemplate <class T, class S> pair<T, S> operator+=(pair<T, S> &l, const pair<T, S> &r) { return l = l + r; }\ntemplate <class T, class S> pair<T, S> operator-=(pair<T, S> &l, const pair<T, S> &r) { return l = l - r; }\ntemplate <class T> bool intersect(const pair<T, T> &l, const pair<T, T> &r) { return (l.se < r.se ? r.fi < l.se : l.fi < r.se); }\n\ntemplate <typename T> struct edge {\n int from, to;\n T cost;\n int id;\n edge(int to, T cost) : from(-1), to(to), cost(cost) {}\n edge(int from, int to, T cost) : from(from), to(to), cost(cost) {}\n edge(int from, int to, T cost, int id) : from(from), to(to), cost(cost), id(id) {}\n constexpr bool operator<(const edge<T> &rhs) const noexcept { return cost < rhs.cost; }\n edge &operator=(const int &x) {\n to = x;\n return *this;\n }\n operator int() const { return to; }\n friend ostream operator<<(ostream &os, edge &e) { return os << e.to; }\n};\ntemplate <typename T> using Edges = vector<edge<T>>;\n\nusing Tree = vector<vector<int>>;\nusing Graph = vector<vector<int>>;\ntemplate <class T> using Wgraph = vector<vector<edge<T>>>;\nGraph getG(int n, int m = -1, bool directed = false, int margin = 1) {\n Tree res(n);\n if(m == -1) m = n - 1;\n while(m--) {\n int a, b;\n cin >> a >> b;\n a -= margin, b -= margin;\n res[a].emplace_back(b);\n if(!directed) res[b].emplace_back(a);\n }\n return res;\n}\nGraph getTreeFromPar(int n, int margin = 1) {\n Graph res(n);\n for(int i = 1; i < n; i++) {\n int a;\n cin >> a;\n res[a - margin].emplace_back(i);\n }\n return res;\n}\ntemplate <class T> Wgraph<T> getWg(int n, int m = -1, bool directed = false, int margin = 1) {\n Wgraph<T> res(n);\n if(m == -1) m = n - 1;\n while(m--) {\n int a, b;\n T c;\n scan(a), scan(b), scan(c);\n a -= margin, b -= margin;\n res[a].emplace_back(b, c);\n if(!directed) res[b].emplace_back(a, c);\n }\n return res;\n}\nvoid add(Graph &G, int x, int y) { G[x].eb(y), G[y].eb(x); }\ntemplate <class S, class T> void add(Wgraph<S> &G, int x, int y, T c) { G[x].eb(y, c), G[y].eb(x, c); }\n\n#define TEST \\\n INT(testcases); \\\n while(testcases--)\n\nistream &operator>>(istream &is, i128 &v) {\n string s;\n is >> s;\n v = 0;\n for(int i = 0; i < (int)s.size(); i++) {\n if(isdigit(s[i])) { v = v * 10 + s[i] - '0'; }\n }\n if(s[0] == '-') { v *= -1; }\n return is;\n}\n\nostream &operator<<(ostream &os, const i128 &v) {\n if(v == 0) { return (os << \"0\"); }\n i128 num = v;\n if(v < 0) {\n os << '-';\n num = -num;\n }\n string s;\n for(; num > 0; num /= 10) { s.push_back((char)(num % 10) + '0'); }\n reverse(s.begin(), s.end());\n return (os << s);\n}\nnamespace aux {\ntemplate <typename T, unsigned N, unsigned L> struct tp {\n static void output(std::ostream &os, const T &v) {\n os << std::get<N>(v) << (&os == &cerr ? \", \" : \" \");\n tp<T, N + 1, L>::output(os, v);\n }\n};\ntemplate <typename T, unsigned N> struct tp<T, N, N> {\n static void output(std::ostream &os, const T &v) { os << std::get<N>(v); }\n};\n} // namespace aux\ntemplate <typename... Ts> std::ostream &operator<<(std::ostream &os, const std::tuple<Ts...> &t) {\n if(&os == &cerr) { os << '('; }\n aux::tp<std::tuple<Ts...>, 0, sizeof...(Ts) - 1>::output(os, t);\n if(&os == &cerr) { os << ')'; }\n return os;\n}\ntemplate <class T, class S> ostream &operator<<(ostream &os, const pair<T, S> &p) {\n if(&os == &cerr) { return os << \"(\" << p.first << \", \" << p.second << \")\"; }\n return os << p.first << \" \" << p.second;\n}\ntemplate <class Ch, class Tr, class Container> std::basic_ostream<Ch, Tr> &operator<<(std::basic_ostream<Ch, Tr> &os, const Container &x) {\n bool f = true;\n if(&os == &cerr) os << \"[\";\n for(auto &y : x) {\n if(&os == &cerr)\n os << (f ? \"\" : \", \") << y;\n else\n os << (f ? \"\" : \" \") << y;\n f = false;\n }\n if(&os == &cerr) os << \"]\";\n return os;\n}\n\n#ifdef noimi\n#undef endl\nvoid debug() { cerr << endl; }\nvoid debug(bool) { cerr << endl; }\ntemplate <class Head, class... Tail> void debug(bool is_front, Head head, Tail... tail) {\n if(!is_front) cerr << \", \";\n cerr << head;\n debug(false, tail...);\n}\n\n#define dump(args...) \\\n { \\\n vector<string> _debug = _split(#args, ','); \\\n err(true, begin(_debug), args); \\\n }\n\nvector<string> _split(const string &s, char c) {\n vector<string> v;\n stringstream ss(s);\n string x;\n while(getline(ss, x, c)) {\n if(empty(v))\n v.eb(x);\n else {\n bool flag = false;\n for(auto [c, d] : {pair('(', ')'), pair('[', ']'), pair('{', '}')}) {\n if(count(all(v.back()), c) != count(all(v.back()), d)) flag = true;\n }\n if(flag)\n v.back() += \",\" + x;\n else\n v.eb(x);\n }\n }\n return move(v);\n}\n\nvoid err(bool, vector<string>::iterator) { cerr << endl; }\ntemplate <typename T, typename... Args> void err(bool is_front, vector<string>::iterator it, T a, Args... args) {\n if(!is_front) cerr << \", \";\n cerr << it->substr((*it)[0] == ' ', (*it).size()) << \" = \" << a, err(false, ++it, args...);\n}\n\n// #define dump(...) cerr << #__VA_ARGS__ << \" : \", debug(true, __VA_ARGS__)\n#else\n#define dump(...) static_cast<void>(0)\n#define dbg(...) static_cast<void>(0)\n#endif\nvoid OUT() { cout << endl; }\ntemplate <class Head, class... Tail> void OUT(const Head &head, const Tail &...tail) {\n cout << head;\n if(sizeof...(tail)) cout << ' ';\n OUT(tail...);\n}\n\ntemplate <typename T> static constexpr T inf = numeric_limits<T>::max() / 2;\ntemplate <class T, class S> constexpr pair<T, S> inf<pair<T, S>> = {inf<T>, inf<S>};\n\ntemplate <class F> struct REC {\n F f;\n REC(F &&f_) : f(std::forward<F>(f_)) {}\n template <class... Args> auto operator()(Args &&...args) const { return f(*this, std::forward<Args>(args)...); }\n};\n\ntemplate <class S> vector<pair<S, int>> runLength(const vector<S> &v) {\n vector<pair<S, int>> res;\n for(auto &e : v) {\n if(res.empty() or res.back().fi != e)\n res.eb(e, 1);\n else\n res.back().se++;\n }\n return res;\n}\nvector<pair<char, int>> runLength(const string &v) {\n vector<pair<char, int>> res;\n for(auto &e : v) {\n if(res.empty() or res.back().fi != e)\n res.eb(e, 1);\n else\n res.back().se++;\n }\n return res;\n}\n\nint toint(const char &c, const char start = 'a') { return c - start; }\nint toint(const char &c, const string &chars) { return find(all(chars), c) - begin(chars); }\nint alphabets_to_int(const char &c) { return (islower(c) ? c - 'a' : c - 'A' + 26); }\ntemplate <typename T> auto toint(const T &v, const char &start = 'a') {\n vector<decltype(toint(v[0]))> ret;\n ret.reserve(v.size());\n for(auto &&e : v) ret.emplace_back(toint(e, start));\n return ret;\n}\ntemplate <typename T> auto toint(const T &v, const string &start) {\n vector<decltype(toint(v[0]))> ret;\n ret.reserve(v.size());\n for(auto &&e : v) ret.emplace_back(toint(e, start));\n return ret;\n}\n// a -> 0, A -> 26\ntemplate <typename T> auto alphabets_to_int(const T &s) {\n vector<decltype(alphabets_to_int(s[0]))> res;\n res.reserve(s.size());\n for(auto &&e : s) { res.emplace_back(alphabets_to_int(e)); }\n return res;\n}\n\ntemplate <class T, class F> T bin_search(T ok, T ng, const F &f) {\n while(abs(ok - ng) > 1) {\n T mid = ok + ng >> 1;\n (f(mid) ? ok : ng) = mid;\n }\n return ok;\n}\ntemplate <class T, class F> T bin_search_double(T ok, T ng, const F &f, int iter = 80) {\n while(iter--) {\n T mid = (ok + ng) / 2;\n (f(mid) ? ok : ng) = mid;\n }\n return ok;\n}\n\nstruct Setup_io {\n Setup_io() {\n ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);\n cout << fixed << setprecision(11);\n }\n} setup_io;\n\n#pragma endregion\n\nint main() {\n rep(100) {\n INT(n);\n if(!n) exit(0);\n VEC(int, d, n *(n - 1) / 2);\n multiset<int> s;\n fore(e, d) s.emplace(e);\n\n vvc<int> ans;\n vi v{0, *rbegin(s)};\n s.erase(--end(s));\n\n REC([&](auto &&f, int i, vi &v) -> void {\n // dump(i, v, s);\n if(i == n) {\n ans.eb(v);\n return;\n }\n int d = *rbegin(s);\n\n vi w;\n for(auto D : {d, v[1] - d}) {\n bool flag = true;\n fore(e, v) {\n int k = abs(D - e);\n auto it = s.lower_bound(k);\n if(it != end(s) and *it == k) {\n w.eb(k);\n s.erase(it);\n } else {\n flag = false;\n break;\n }\n }\n if(flag) {\n v.eb(D);\n f(i + 1, v);\n fore(e, w) s.emplace(e);\n v.pop_back();\n } else {\n fore(e, w) s.emplace(e);\n }\n w.clear();\n }\n })\n (2, v);\n fore(e, ans) SORT(e);\n UNIQUE(ans);\n fore(e, ans) {\n SORT(e);\n vi w;\n rep(i, n - 1) w.eb(e[i + 1] - e[i]);\n OUT(w);\n }\n cout << \"-----\" << endl;\n }\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3560, "score_of_the_acc": -0.0256, "final_rank": 10 }, { "submission_id": "aoj_1307_6009914", "code_snippet": "//#pragma GCC optimize(\"O3\")\n//#pragma GCC optimize(\"unroll-loops\")\n#include<iostream>\n#include<string>\n#include<cstdio>\n#include<vector>\n#include<cmath>\n#include<algorithm>\n#include<functional>\n#include<iomanip>\n#include<queue>\n#include<ciso646>\n#include<random>\n#include<map>\n#include<set>\n#include<bitset>\n#include<stack>\n#include<unordered_map>\n#include<unordered_set>\n#include<utility>\n#include<cassert>\n#include<complex>\n#include<numeric>\n#include<array>\nusing namespace std;\n\n//#define int long long\ntypedef long long ll;\n\ntypedef unsigned long long ul;\ntypedef unsigned int ui;\nconstexpr ll mod = 998244353;\nconst ll INF = mod * mod;\ntypedef pair<int, int>P;\n\n#define rep(i,n) for(int i=0;i<n;i++)\n#define per(i,n) for(int i=n-1;i>=0;i--)\n#define Rep(i,sta,n) for(int i=sta;i<n;i++)\n#define rep1(i,n) for(int i=1;i<=n;i++)\n#define per1(i,n) for(int i=n;i>=1;i--)\n#define Rep1(i,sta,n) for(int i=sta;i<=n;i++)\n#define all(v) (v).begin(),(v).end()\ntypedef pair<ll, ll> LP;\ntypedef long double ld;\ntypedef pair<ld, ld> LDP;\nconst ld eps = 1e-8;\nconst ld pi = acosl(-1.0);\n\nll mod_pow(ll x, ll n, ll m = mod) {\n\tif (n < 0) {\n\t\tll res = mod_pow(x, -n, m);\n\t\treturn mod_pow(res, m - 2, m);\n\t}\n\tif (abs(x) >= m)x %= m;\n\tif (x < 0)x += m;\n\tll res = 1;\n\twhile (n) {\n\t\tif (n & 1)res = res * x % m;\n\t\tx = x * x % m; n >>= 1;\n\t}\n\treturn res;\n}\nstruct modint {\n\tll n;\n\tmodint() :n(0) { ; }\n\tmodint(ll m) :n(m) {\n\t\tif (n >= mod)n %= mod;\n\t\telse if (n < 0)n = (n % mod + mod) % mod;\n\t}\n\toperator int() { return n; }\n};\nbool operator==(modint a, modint b) { return a.n == b.n; }\nmodint operator+=(modint& a, modint b) { a.n += b.n; if (a.n >= mod)a.n -= mod; return a; }\nmodint operator-=(modint& a, modint b) { a.n -= b.n; if (a.n < 0)a.n += mod; return a; }\nmodint operator*=(modint& a, modint b) { a.n = ((ll)a.n * b.n) % mod; return a; }\nmodint operator+(modint a, modint b) { return a += b; }\nmodint operator-(modint a, modint b) { return a -= b; }\nmodint operator*(modint a, modint b) { return a *= b; }\nmodint operator^(modint a, ll n) {\n\tif (n == 0)return modint(1);\n\tmodint res = (a * a) ^ (n / 2);\n\tif (n % 2)res = res * a;\n\treturn res;\n}\n\nll inv(ll a, ll p) {\n\treturn (a == 1 ? 1 : (1 - p * inv(p % a, a)) / a + p);\n}\nmodint operator/(modint a, modint b) { return a * modint(inv(b, mod)); }\nmodint operator/=(modint& a, modint b) { a = a / b; return a; }\nconst int max_n = 1 << 2;\nmodint fact[max_n], factinv[max_n];\nvoid init_f() {\n\tfact[0] = modint(1);\n\tfor (int i = 0; i < max_n - 1; i++) {\n\t\tfact[i + 1] = fact[i] * modint(i + 1);\n\t}\n\tfactinv[max_n - 1] = modint(1) / fact[max_n - 1];\n\tfor (int i = max_n - 2; i >= 0; i--) {\n\t\tfactinv[i] = factinv[i + 1] * modint(i + 1);\n\t}\n}\nmodint comb(int a, int b) {\n\tif (a < 0 || b < 0 || a < b)return 0;\n\treturn fact[a] * factinv[b] * factinv[a - b];\n}\nmodint combP(int a, int b) {\n\tif (a < 0 || b < 0 || a < b)return 0;\n\treturn fact[a] * factinv[a - b];\n}\n\nint dx[4] = { 1,0,-1,0 };\nint dy[4] = { 0,-1,0,1 };\n\nll gcd(ll a, ll b) {\n\twhile (b) {\n\t\tll r = a % b; a = b; b = r;\n\t}\n\treturn a;\n}\n\nint n;\nvoid solve() {\n\tvector<int> d;\n\tint m = n * (n - 1) / 2;\n\trep(i, n * (n - 1) / 2) {\n\t\tint x; cin >> x; d.push_back(x);\n\t}\n\tsort(all(d));\n\t/*if (n == 2) {\n\t\tcout << d[0] << \"\\n\";\n\t\tcout << \"-----\\n\";\n\t\treturn;\n\t}*/\n\tvector<int> ori(401);\n\tfor (int dd : d)ori[dd]++;\n\tvector<vector<int>> ans;\n\t\n\trep(i, (1 << (n - 2))) {\n\t\tvector<int> cnt = ori;\n\t\tint tmp = 400;\n\t\tauto takema = [&]() {\n\t\t\twhile (cnt[tmp] == 0)tmp--;\n\t\t\treturn tmp;\n\t\t};\n\t\tint al = takema(); cnt[al]--;\n\t\tvector<int> cur(n-1, -1);\n\t\tint locl = 0, locr = n - 2;\n\t\tint sl = 0, sr = 0;\n\t\tbool valid = true;\n\t\trep(j, n - 2) {\n\t\t\tint ma = takema(); cnt[ma]--;\n\t\t\tif (i & (1 << j)) {\n\t\t\t\tint val = al - ma - sr;\n\t\t\t\tcur[locr] = val; locr--; sr += val;\n\t\t\t\tfor (int k = 0; k < locl; k++) {\n\t\t\t\t\tma -= cur[k];\n\t\t\t\t\tif (ma<0||cnt[ma] == 0) {\n\t\t\t\t\t\tvalid = false; break;\n\t\t\t\t\t}\n\t\t\t\t\telse {\n\t\t\t\t\t\tcnt[ma]--;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t\telse {\n\t\t\t\tint val = al - ma - sl;\n\t\t\t\tcur[locl] = val; locl++; sl += val;\n\t\t\t\tfor (int k = n - 2; k > locr; k--) {\n\t\t\t\t\tma -= cur[k];\n\t\t\t\t\tif (ma<0||cnt[ma] == 0) {\n\t\t\t\t\t\tvalid = false; break;\n\t\t\t\t\t}\n\t\t\t\t\telse {\n\t\t\t\t\t\tcnt[ma]--;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t\tif (!valid)break;\n\t\t}\n\t\tif (!valid)continue;\n\t\tint r = al;\n\t\trep(i, n - 1)if (cur[i] >= 0)r -= cur[i];\n\t\trep(i, n - 1)if (cur[i] < 0)cur[i] = r;\n\t\tcnt = ori;\n\t\trep(i, n - 1) {\n\t\t\tint sum = 0;\n\t\t\tRep(j, i, n - 1) {\n\t\t\t\tsum += cur[j];\n\t\t\t\tif (cnt[sum] == 0) {\n\t\t\t\t\tvalid = false;\n\t\t\t\t}\n\t\t\t\tcnt[sum]--;\n\t\t\t}\n\t\t}\n\t\tif (valid)ans.push_back(cur);\n\t}\n\t\n\t//cout << \"ans is \" << \"\\n\";\n\tsort(all(ans));\n\tans.erase(unique(all(ans)), ans.end());\n\trep(i, ans.size()) {\n\t\trep(j, n - 1) {\n\t\t\tif (j > 0)cout << \" \";\n\t\t\tcout << ans[i][j];\n\t\t}\n\t\tcout << \"\\n\";\n\t}\n\tcout << \"-----\\n\";\n}\n\n\nsigned main() {\n\tios::sync_with_stdio(false);\n\tcin.tie(0);\n\tcout << fixed << setprecision(8);\n\t//init_f();\n\t//init();\n\t//int t; cin >> t; rep(i, t)\n\twhile(cin>>n,n)\n\t\tsolve();\n\treturn 0;\n}", "accuracy": 1, "time_ms": 1930, "memory_kb": 3428, "score_of_the_acc": -0.6136, "final_rank": 17 }, { "submission_id": "aoj_1307_5007686", "code_snippet": "#include<cstdio>\n#include<vector>\n#include<set>\n#include<string>\n#include<algorithm>\n#include<functional>\nusing namespace std;\n\nint n, m, d[190], ans[20], len, skip[401], counter[401];\nset<string> double_count;\n\nbool check() {\n vector<int> sum(n, 0), tmp;\n for (int i = 0; i < n-1; i++) sum[i+1] = sum[i] + ans[i];\n for (int i = 0; i < n-1; i++) {\n for (int j = i+1; j < n; j++) {\n tmp.push_back(sum[j] - sum[i]);\n }\n }\n sort(tmp.begin(), tmp.end(), greater<int>());\n for (int i = 0; i < m; i++) if (tmp[i] != d[i]) return false;\n string s = \"\";\n for (int i = 0; i < n-1; i++) s += to_string(ans[i]) + \" \";\n if (double_count.count(s)) return false;\n double_count.insert(s);\n return true;\n}\n\nvoid dfs(int l, int r, int num, int lsum, int rsum) {\n if (l == r) {\n ans[l] = len - lsum - rsum;\n if (ans[l] > 0 && check()) {\n for (int i = 0; i < n-1; i++) printf(\"%d%c\", ans[i], i == n-2 ? '\\n' : ' ');\n }\n return;\n }\n if (skip[d[num]] > 0) {\n skip[d[num]]--;\n dfs(l, r, num+1, lsum, rsum);\n skip[d[num]]++;\n return;\n }\n counter[d[num]]--;\n ans[l] = len - d[num] - lsum;\n if (ans[l] > 0) { // 重複???\n bool ok = true;\n for (int r2 = r+1, s_tmp = len - lsum - rsum - ans[l]; r2 <= n-2 && s_tmp >= 0 && s_tmp <= d[num]; s_tmp += ans[r2], r2++) {\n skip[s_tmp]++;\n if (skip[s_tmp] > counter[s_tmp]) ok = false;\n }\n if (ok) dfs(l+1, r, num+1, lsum + ans[l], rsum);\n for (int r2 = r+1, s_tmp = len - lsum - rsum - ans[l]; r2 <= n-2 && s_tmp >= 0 && s_tmp <= d[num]; s_tmp += ans[r2], r2++) {\n skip[s_tmp]--;\n }\n }\n ans[r] = len - d[num] - rsum;\n if (ans[r] > 0) {\n bool ok = true;\n for (int l2 = l-1, s_tmp = len - lsum - rsum - ans[r]; l2 >= 0 && s_tmp >= 0 && s_tmp <= d[num]; s_tmp += ans[l2], l2--) {\n skip[s_tmp]++;\n if (skip[s_tmp] > counter[s_tmp]) ok = false;\n }\n if (ok) dfs(l, r-1, num+1, lsum, rsum + ans[r]);\n for (int l2 = l-1, s_tmp = len - lsum - rsum - ans[r]; l2 >= 0 && s_tmp >= 0 && s_tmp <= d[num]; s_tmp += ans[l2], l2--) {\n skip[s_tmp]--;\n }\n }\n counter[d[num]]++;\n}\n\nint main() {\n while (scanf(\"%d\", &n), n) {\n double_count.clear();\n m = n * (n-1) / 2;\n for (int i = 0; i <= 400; i++) counter[i] = 0;\n for (int i = 0; i < m; i++) scanf(\"%d\", &d[i]), counter[d[i]]++;\n len = d[0];\n if (n == 2) {\n printf(\"%d\\n\", d[0]);\n } else {\n dfs(0, n-2, 1, 0, 0);\n }\n printf(\"-----\\n\");\n }\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 2828, "score_of_the_acc": 0, "final_rank": 1 }, { "submission_id": "aoj_1307_4926392", "code_snippet": "#include <bits/stdc++.h>\n\nusing namespace std;\n\n#define REP(i, n) for (int i=0; i<(n); ++i)\n#define RREP(i, n) for (int i=(int)(n)-1; i>=0; --i)\n#define FOR(i, a, n) for (int i=(a); i<(n); ++i)\n#define RFOR(i, a, n) for (int i=(int)(n)-1; i>=(a); --i)\n\n#define SZ(x) ((int)(x).size())\n#define ALL(x) (x).begin(),(x).end()\n\n#define DUMP(x) cerr<<#x<<\" = \"<<(x)<<endl\n#define DEBUG(x) cerr<<#x<<\" = \"<<(x)<<\" (L\"<<__LINE__<<\")\"<<endl;\n\ntemplate<class T>\nostream &operator<<(ostream &os, const vector<T> &v) {\n os << \"[\";\n REP(i, SZ(v)) {\n if (i) os << \", \";\n os << v[i];\n }\n return os << \"]\";\n}\n\ntemplate<class T, class U>\nostream &operator<<(ostream &os, const pair<T, U> &p) {\n return os << \"(\" << p.first << \" \" << p.second << \")\";\n}\n\ntemplate<class T>\nbool chmax(T &a, const T &b) {\n if (a < b) {\n a = b;\n return true;\n }\n return false;\n}\n\ntemplate<class T>\nbool chmin(T &a, const T &b) {\n if (b < a) {\n a = b;\n return true;\n }\n return false;\n}\n\nusing ll = long long;\nusing ull = unsigned long long;\nusing ld = long double;\nusing P = pair<int, int>;\nusing vi = vector<int>;\nusing vll = vector<ll>;\nusing vvi = vector<vi>;\nusing vvll = vector<vll>;\n\nconst ll MOD = 1e9 + 7;\nconst int INF = INT_MAX / 2;\nconst ll LINF = LLONG_MAX / 2;\nconst ld eps = 1e-9;\n\nvoid sch(int n, int k, int l, multiset<int> &st, vi &ans, set<vi> &res) {\n if(k > l) {\n vi v2(n-1);\n REP(i, SZ(ans)-1) {\n v2[i] = ans[i+1] - ans[i];\n }\n res.insert(v2);\n return;\n }\n\n multiset<int> deleted;\n multiset<int> use;\n auto it = --st.end();\n int val = *it;\n st.erase(it);\n\n // 1\n bool ok = true;\n ans[k] = ans[n-1] - val;\n REP(i, k) {\n use.insert(ans[k] - ans[i]);\n }\n for(int i=n-2;i>l;--i) {\n use.insert(ans[i] - ans[k]);\n }\n for(auto &e: use) {\n it = st.find(e);\n if(it == st.end()) {\n ok = false;\n break;\n }\n st.erase(it);\n deleted.insert(e);\n }\n if(ok) {\n sch(n, k+1, l, st, ans, res);\n }\n st.merge(deleted);\n use.clear();\n ans[k] = -1;\n\n // 2\n ok = true;\n ans[l] = ans[0] + val;\n FOR(i, 1, k) {\n use.insert(ans[l] - ans[i]);\n }\n for(int i=n-1;i>l;--i) {\n use.insert(ans[i] - ans[l]);\n }\n for(auto &e: use) {\n it = st.find(e);\n if(it == st.end()) {\n ok = false;\n break;\n }\n st.erase(it);\n deleted.insert(e);\n }\n if(ok) {\n sch(n, k, l-1, st, ans, res);\n }\n ans[l] = -1;\n st.merge(deleted);\n use.clear();\n\n st.insert(val);\n}\n\nbool solve() {\n int n;\n cin >> n;\n if (n == 0) return false;\n int sz = n * (n - 1) / 2;\n multiset<int> st;\n REP(i, sz) {\n int v;\n cin >> v;\n st.insert(v);\n }\n\n vi ans(n, -1);\n ans[0] = 0;\n auto it = --st.end();\n int val = *it;\n st.erase(it);\n ans[n-1] = val;\n set<vi> res;\n sch(n, 1, n-2, st, ans, res);\n\n for (auto &v: res) {\n REP(i, SZ(v)) {\n cout << v[i];\n if (i == SZ(v) - 1) cout << \"\\n\";\n else cout << \" \";\n }\n }\n cout << \"-----\" << endl;\n return true;\n}\n\nint main() {\n cin.tie(0);\n ios::sync_with_stdio(false);\n cout << fixed << setprecision(10);\n\n while (solve()) {}\n\n return 0;\n}", "accuracy": 1, "time_ms": 20, "memory_kb": 3408, "score_of_the_acc": -0.0234, "final_rank": 8 }, { "submission_id": "aoj_1307_4440938", "code_snippet": "#include <bits/stdc++.h>\n\nusing namespace std;\n\nclass Solution {\npublic:\n\tvoid solve() {\n\t\twhile (1) {\n\t\t\tint n;\n\t\t\tcin >> n;\n\t\t\tif (n == 0) return;\n\t\t\tvector<int> d((n * (n - 1)) / 2, 0);\n\t\t\tfor (int i = 0; i < (n * (n - 1)) / 2; i++) cin >> d[i];\n\t\t\tvector<int> _cnt(d[0] + 1);\n\t\t\tvector<int> _x(n, -1);\n\t\t\tfor (int i = 0; i < (n * (n - 1)) / 2; i++) _cnt[d[i]]++;\n\t\t\t_x[0] = 0;\n\t\t\t_x[n - 1] = d[0];\n\t\t\t_cnt[d[0]]--;\n\t\t\tvector<vector<int> > ans;\n\t\t\tfor (int i = 0; i < (1 << (n - 2)); i++) {\n\t\t\t\tbool ok = true;\n\t\t\t\tvector<int> x(_x.begin(), _x.end());\n\t\t\t\tvector<int> cnt(_cnt.begin(), _cnt.end());\n\t\t\t\tint l = 1, r = n - 2;\n\t\t\t\tint sh = n - 3;\n\t\t\t\tfor (int j = d[0]; j > 0; j--) {\n\t\t\t\t\twhile (cnt[j] > 0 && sh >= 0) {\n\t\t\t\t\t\tint id;\n\t\t\t\t\t\tif ((i >> sh) & 1) {\n\t\t\t\t\t\t\tid = r;\n\t\t\t\t\t\t\tx[r--] = j;\n\t\t\t\t\t\t} else {\n\t\t\t\t\t\t\tid = l;\n\t\t\t\t\t\t\tx[l++] = d[0] - j;\n\t\t\t\t\t\t}\n\t\t\t\t\t\tfor (int k = 0; k < n; k++) {\n\t\t\t\t\t\t\tif (id == k || x[k] == -1) continue;\n\t\t\t\t\t\t\tif (!cnt[abs(x[id] - x[k])]--) {\n\t\t\t\t\t\t\t\tok = false;\n\t\t\t\t\t\t\t\tbreak;\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t}\n\t\t\t\t\t\tsh--;\n\t\t\t\t\t\tif (!ok) break;\n\t\t\t\t\t}\n\t\t\t\t\tif (!ok) break;\n\t\t\t\t}\n\t\t\t\tif (!ok) continue;\n\t\t\t\tans.push_back(x);\n\t\t\t}\n\t\t\tsort(ans.begin(), ans.end());\n\t\t\tans.erase(unique(ans.begin(), ans.end()), ans.end());\n\t\t\tfor (int i = 0; i < ans.size(); i++) {\n\t\t\t\tfor (int j = 1; j < n; j++) {\n\t\t\t\t\tif (j == 1)\n\t\t\t\t\t\tcout << ans[i][j] - ans[i][j - 1];\n\t\t\t\t\telse\n\t\t\t\t\t\tcout << \" \" << ans[i][j] - ans[i][j - 1];\n\t\t\t\t}\n\t\t\t\tcout << \"\\n\";\n\t\t\t}\n\t\t\tcout << \"-----\\n\";\n\t\t}\n\t\treturn;\n\t};\n};\n\nint main() {\n\tios_base::sync_with_stdio(false);\n\tcin.tie(0);\n\tSolution solution;\n\tsolution.solve();\n\treturn 0;\n}", "accuracy": 1, "time_ms": 2300, "memory_kb": 3164, "score_of_the_acc": -0.7185, "final_rank": 18 }, { "submission_id": "aoj_1307_3978569", "code_snippet": "#include <algorithm>\n#include <iostream>\n#include <vector>\n#include <set>\n#include <map>\nusing namespace std;\n\n// http://koturn.hatenablog.com/entry/2018/06/10/060000\ntemplate<typename F> struct FixPoint : F {\n FixPoint(F&& f) : F(forward<F>(f)) { }\n template<typename... Args> decltype(auto) operator()(Args&&... args) {\n return F::operator()(*this, forward<Args>(args)...);\n }\n};\ntemplate<typename F> decltype(auto) MFP(F&& f) { // makeFixPoint\n return FixPoint<F>{forward<F>(f)};\n}\n\nint main() {\n int n;\n while (cin >> n, n) {\n int nC2 = n * (n - 1) / 2;\n vector<int> ds(nC2); for (auto &d: ds) cin >> d;\n multiset<int> ms; for (auto &d: ds) ms.emplace(d);\n int ma = ds[0]; ms.erase(ms.find(ma));\n vector<int> acc({0, ma});\n map<int, int> cnt; for (auto &d: ds) cnt[d]++;\n set<vector<int>> res;\n MFP([&](auto dfs) -> void {\n if (ms.empty()) {\n auto tmp(acc); sort(tmp.begin(), tmp.end());\n res.emplace(tmp);\n return ;\n }\n for (int nxt: {*ms.rbegin(), ma - *ms.rbegin()}) {\n bool flg = true;\n for (auto x: acc) flg &= --cnt[abs(x - nxt)] >= 0;\n if (flg) {\n for (auto x: acc) ms.erase(ms.find(abs(x - nxt)));\n acc.emplace_back(nxt);\n dfs();\n acc.pop_back();\n for (auto x: acc) ms.emplace(abs(x - nxt));\n }\n for (auto x: acc) ++cnt[abs(x - nxt)];\n }\n })();\n for (auto &v: res) {\n for (int i = 0; i + 1 < n; i++) cout << v[i + 1] - v[i] << \" \\n\"[i == n - 2];\n }\n cout << string(5, '-') << endl;\n }\n return 0;\n}", "accuracy": 1, "time_ms": 20, "memory_kb": 3068, "score_of_the_acc": -0.0115, "final_rank": 3 }, { "submission_id": "aoj_1307_3655932", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\nint main() {\n int n;\n while(cin >> n, n) {\n multiset<int, greater<>> d;\n for(int i = 0; i < n * (n - 1) / 2; ++i) {\n int x; cin >> x;\n d.insert(x);\n }\n\n vector<int> cur = {0, *d.begin()}; // x position\n d.erase(d.begin());\n vector<vector<int>> ans;\n function<void(multiset<int, greater<>>&)> dfs = [&] (multiset<int, greater<>>& s) {\n if((int)cur.size() == n) { // ok\n ans.push_back({});\n for(int i = 1; i < n; ++i) {\n ans.back().push_back(cur[i] - cur[i - 1]);\n }\n return;\n }\n auto attempt = [&] (int p) {\n vector<int> rm;\n for(auto x : cur) {\n const auto it = s.lower_bound(abs(p - x));\n if(it == end(s) || *it != abs(p - x)) break;\n rm.push_back(*it);\n s.erase(it);\n }\n if(rm.size() == cur.size()) {\n cur.insert(lower_bound(begin(cur), end(cur), p), p);\n dfs(s);\n cur.erase(find(begin(cur), end(cur), p));\n }\n s.insert(begin(rm), end(rm));\n };\n attempt(*s.begin());\n attempt(cur.back() - *s.begin());\n };\n\n dfs(d);\n sort(begin(ans), end(ans));\n ans.erase(unique(begin(ans), end(ans)), end(ans));\n for(auto const& v : ans) {\n for(int i = 0; i < n - 1; ++i) {\n cout << v[i] << \" \\n\"[i + 1 == n - 1];\n }\n }\n cout << \"-----\" << endl;\n }\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3204, "score_of_the_acc": -0.0131, "final_rank": 6 }, { "submission_id": "aoj_1307_3625993", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\nusing Int = long long;\ntemplate<typename T1,typename T2> inline void chmin(T1 &a,T2 b){if(a>b) a=b;}\ntemplate<typename T1,typename T2> inline void chmax(T1 &a,T2 b){if(a<b) a=b;}\n\n\ntemplate<typename F>\nstruct FixPoint : F{\n FixPoint(F&& f):F(forward<F>(f)){}\n template<typename... Args>\n decltype(auto) operator()(Args&&... args) const{\n return F::operator()(*this,forward<Args>(args)...);\n } \n};\ntemplate<typename F>\ninline decltype(auto) MFP(F&& f){\n return FixPoint<F>{forward<F>(f)};\n}\n\n//INSERT ABOVE HERE\nsigned main(){\n int n;\n while(cin>>n,n){\n n--;\n int m=n*(n+1)/2;\n vector<int> ds(m);\n for(int i=0;i<m;i++) cin>>ds[i];\n\n multiset<int> ms;\n for(int d:ds) ms.emplace(d);\n \n int len=ds[0];\n ms.erase(ms.find(len));\n vector<int> vs({0,len});\n \n map<int, int> cnt;\n for(int x:ms) cnt[x]++; \n \n set<vector<int>> ans;\n // [l, r]\n MFP([&](auto dfs)->void{ \n if(ms.empty()){\n auto us(vs);\n sort(us.begin(),us.end()); \n ans.emplace(us);\n return;\n } \n for(int k=0;k<2;k++){\n int nxt=k?*ms.rbegin():len-*ms.rbegin(); \n int flg=1;\n for(int v:vs){\n cnt[abs(v-nxt)]--;\n flg&=cnt[abs(v-nxt)]>=0;\n }\n if(flg){\n for(int v:vs)\n ms.erase(ms.find(abs(v-nxt)));\n vs.emplace_back(nxt);\n dfs();\n vs.pop_back();\n for(int v:vs)\n ms.emplace(abs(v-nxt)); \n } \n for(int v:vs)\n cnt[abs(v-nxt)]++;\n } \n })();\n \n for(auto vs:ans){\n for(int i=0;i<n;i++){\n if(i) cout<<\" \";\n cout<<vs[i+1]-vs[i];\n }\n cout<<endl;\n }\n cout<<\"-----\"<<endl;\n }\n return 0;\n}", "accuracy": 1, "time_ms": 20, "memory_kb": 3084, "score_of_the_acc": -0.012, "final_rank": 5 }, { "submission_id": "aoj_1307_3597313", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\ntypedef long long int ll;\ntypedef pair<ll,ll> pll;\ntypedef long double D;\n#define F first\n#define S second\nconst ll MOD=1000000007;\nconst ll E=1e18;\n\nset<vector<ll>> ans;\nmultiset<ll> cost;\nset<ll> point;\nll n;\nll l,r;\n\nvoid dfs(ll d){\n if(d==n){\n ll L=l;\n auto I=point.begin(); ++I;\n vector<ll> ret;\n for(;I!=point.end();++I){\n ret.push_back((*I)-L);\n L=*I;\n }\n ans.insert(ret);\n return;\n }\n auto I=cost.end(); I--;\n ll dist=*I;\n ll w=dist+l;\n vector<ll> used;\n bool j=true;\n for(auto I=point.begin();I!=point.end();++I){\n auto T=cost.find(abs(*I-w));\n if(T!=cost.end()){\n used.push_back(abs(*I-w));\n cost.erase(T);\n }\n else{\n j=false;\n break;\n }\n }\n if(j && point.count(w)==0){\n point.insert(w);\n dfs(d+1);\n point.erase(w);\n }\n for(auto &I:used){cost.insert(I);}\n used.clear();\n \n w=r-dist;\n j=true;\n for(auto I=point.begin();I!=point.end();++I){\n auto T=cost.find(abs(*I-w));\n if(T!=cost.end()){\n used.push_back(abs(*I-w));\n cost.erase(T);\n }\n else{\n j=false;\n break;\n }\n }\n if(j && point.count(w)==0){\n point.insert(w);\n dfs(d+1);\n point.erase(w);\n }\n for(auto &I:used){cost.insert(I);}\n}\n\n\n\n\nint main(){\n while(cin>>n){\n if(n==0){break;}\n //for(auto &I:ans){I.clear();}\n ans.clear();\n cost.clear();\n point.clear();\n l=0; cin>>r;\n for(int i=1;i<n*(n-1)/2;i++){\n ll d;\n cin>>d;\n cost.insert(d);\n }\n point.insert(l);\n point.insert(r);\n dfs(2);\n for(auto &I:ans){\n for(int i=0;i+1<n;i++){cout<<I[i]<<(i+2==n?\"\\n\":\" \");}\n }\n cout<<\"-----\"<<endl;\n }\n\n return 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3152, "score_of_the_acc": -0.0113, "final_rank": 2 }, { "submission_id": "aoj_1307_3159134", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\ntypedef long long ll;\ntypedef unsigned long long ull;\ntypedef pair<ll, ll> P;\n\n#define fi first\n#define se second\n#define repl(i,a,b) for(ll i=(ll)(a);i<(ll)(b);i++)\n#define rep(i,n) repl(i,0,n)\n#define all(x) (x).begin(),(x).end()\n#define dbg(x) cout<<#x\"=\"<<x<<endl\n#define mmax(x,y) (x>y?x:y)\n#define mmin(x,y) (x<y?x:y)\n#define maxch(x,y) x=mmax(x,y)\n#define minch(x,y) x=mmin(x,y)\n#define uni(x) x.erase(unique(all(x)),x.end())\n#define exist(x,y) (find(all(x),y)!=x.end())\n#define bcnt __builtin_popcountll\n\n#define INF 1e16\n#define mod 1000000007\n\nint N,M;\nint d[410],cnt[410];\nint x[22];\nset<vector<int> > res;\n\nvoid dfs(int l,int r){\n //dbg(l); dbg(r);\n //rep(i,10)dbg(cnt[i]);\n if(l+1>=r){\n vector<int> tmp;\n rep(j,N-1)tmp.push_back(x[j+1]-x[j]);\n res.insert(tmp); return ;\n }\n int mx=-1;\n rep(i,401)if(cnt[i]>0)maxch(mx,i);\n if(mx==-1)return ;\n //dbg(mx);\n\n // left\n x[r-1]=x[0]+mx;\n bool ok=true;\n vector<int> sav;\n repl(i,r,N){\n int crtd=x[i]-x[r-1];\n if(crtd<0||cnt[crtd]==0){\n ok=false; break;\n }\n cnt[crtd]--;\n sav.push_back(crtd);\n }\n repl(i,0,l+1){\n int crtd=x[r-1]-x[i];\n if(crtd<0||cnt[crtd]==0){\n ok=false; break;\n }\n cnt[crtd]--;\n sav.push_back(crtd);\n }\n if(ok)dfs(l,r-1);\n rep(i,sav.size())cnt[sav[i]]++;\n\n // left\n x[r-1]=x[N-1]-mx;\n ok=true;\n sav.clear();\n repl(i,r,N){\n int crtd=x[i]-x[r-1];\n if(crtd<0||cnt[crtd]==0){\n ok=false; break;\n }\n cnt[crtd]--;\n sav.push_back(crtd);\n }\n repl(i,0,l+1){\n int crtd=x[r-1]-x[i];\n if(crtd<0||cnt[crtd]==0){\n ok=false; break;\n }\n cnt[crtd]--;\n sav.push_back(crtd);\n }\n if(ok)dfs(l,r-1);\n rep(i,sav.size())cnt[sav[i]]++;\n\n // right\n x[l+1]=x[N-1]-mx;\n ok=true;\n sav.clear();\n repl(i,0,l+1){\n int crtd=x[l+1]-x[i];\n if(crtd<0||cnt[crtd]==0){\n ok=false; break;\n }\n cnt[crtd]--;\n sav.push_back(crtd);\n }\n repl(i,r,N){\n int crtd=x[i]-x[l+1];\n if(crtd<0||cnt[crtd]==0){\n ok=false; break;\n }\n cnt[crtd]--;\n sav.push_back(crtd);\n }\n if(ok)dfs(l+1,r);\n rep(i,sav.size())cnt[sav[i]]++;\n\n // right\n x[l+1]=mx-x[0];\n ok=true;\n sav.clear();\n repl(i,0,l+1){\n int crtd=x[l+1]-x[i];\n if(crtd<0||cnt[crtd]==0){\n ok=false; break;\n }\n cnt[crtd]--;\n sav.push_back(crtd);\n }\n repl(i,r,N){\n int crtd=x[i]-x[l+1];\n if(crtd<0||cnt[crtd]==0){\n ok=false; break;\n }\n cnt[crtd]--;\n sav.push_back(crtd);\n }\n if(ok)dfs(l+1,r);\n rep(i,sav.size())cnt[sav[i]]++;\n\n}\n\nint main(){\n cin.tie(0);\n ios::sync_with_stdio(false);\n while(1){\n cin>>N;\n if(N==0)break;\n int mx=0;\n M=N*(N-1)/2;\n memset(cnt,0,sizeof(cnt));\n rep(i,M){\n cin>>d[i];\n cnt[d[i]]++;\n }\n sort(d,d+M);\n x[0]=0;\n x[N-1]=d[M-1]; cnt[d[M-1]]--; M--;\n res.clear();\n dfs(0,N-1);\n for(vector<int> ans : res){\n cout<<ans[0];\n repl(j,1,ans.size())cout<<\" \"<<ans[j];\n cout<<endl;\n }\n cout<<\"-----\"<<endl;\n }\n return 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3160, "score_of_the_acc": -0.0116, "final_rank": 4 }, { "submission_id": "aoj_1307_3020039", "code_snippet": "#include<bits/stdc++.h>\ntypedef long long int ll;\ntypedef unsigned long long int ull;\n#define BIG_NUM 2000000000\n#define MOD 1000000007\n#define EPS 0.000000001\nusing namespace std;\n\n\n#define NUM 20\n\nenum Type{\n\tL,\n\tR,\n};\n\nstruct Info{\n\tbool operator<(const struct Info &arg) const{\n\t\treturn dist > arg.dist;\n\t}\n\tint dist,count;\n};\n\nstruct Answer{\n\n\tvoid set(int arg_length,int arg_table[NUM]){\n\t\tlength = arg_length;\n\t\tfor(int i = 0; i < length; i++){\n\t\t\ttable[i] = arg_table[i];\n\t\t}\n\t}\n\tbool operator<(const struct Answer &arg) const{\n\t\tfor(int i = 0; i < length; i++){\n\t\t\tif(table[i] != arg.table[i]){\n\t\t\t\treturn table[i] < arg.table[i];\n\t\t\t}\n\t\t}\n\t\treturn true;\n\t}\n\n\tbool operator==(const struct Answer &arg) const{\n\n\t\tbool FLG = true;\n\t\tfor(int i = 0; i < length; i++){\n\t\t\tif(table[i] != arg.table[i]){\n\t\t\t\tFLG = false;\n\t\t\t}\n\t\t}\n\t\treturn FLG;\n\t}\n\n\tint table[NUM],length;\n};\n\nint N;\nint info_index;\nInfo info[(NUM*(NUM-1))/2];\nmap<string,bool> MAP;\nmap<int,int> LOC;\nvector<Answer> ANS;\n\nstring make_str(int dist_array[NUM]){\n\n\tstring ret;\n\n\tfor(int i = 0; i < info_index; i++){\n\t\tret.append(to_string(dist_array[i])).append(\"@\");\n\t}\n\n\treturn ret;\n}\n\nint get_index(int rest_num[(NUM*(NUM-1))/2]){\n\n\tfor(int i = 0; i < info_index; i++){\n\t\tif(rest_num[i] > 0){\n\t\t\treturn i;\n\t\t}\n\t}\n\treturn -1; //Error\n}\n\nvoid recursive(int dist_array[NUM],int rest_num[(NUM*(NUM-1))/2],int index,int num_decided){\n\n\tif(num_decided == N){\n\n\t\tint work[NUM];\n\t\tAnswer ans;\n\n\t\tfor(int i = 0; i < N-1; i++){\n\t\t\twork[i] = dist_array[i+1]-dist_array[i];\n\t\t}\n\t\tans.set(N-1,work);\n\n\t\tANS.push_back(ans);\n\n\t\treturn;\n\t}\n\n\tint tmp_dist = info[index].dist;\n\trest_num[index]--;\n\n\tint next_dist_array[2][NUM],next_rest_num[2][(NUM*(NUM-1))/2];\n\n\tfor(int i = 0; i < 2; i++){\n\t\tfor(int k = 0; k < N; k++){\n\t\t\tnext_dist_array[i][k] = dist_array[k];\n\t\t}\n\t\tfor(int k = 0; k < info_index; k++){\n\t\t\tnext_rest_num[i][k] = rest_num[k];\n\t\t}\n\t}\n\n\tint tmp_pos;\n\n\tfor(int i = N-2; i >= 0; i--){\n\t\tif(next_dist_array[L][i] == -1){\n\t\t\tnext_dist_array[L][i] = tmp_dist;\n\t\t\ttmp_pos = i;\n\t\t\tbreak;\n\t\t}\n\t}\n\n\tint tmp_diff,tmp_loc;\n\tbool FLG = true;\n\n\tfor(int i = 1; i < N; i++){\n\t\tif(next_dist_array[L][i] == -1 || i == tmp_pos)continue;\n\n\t\ttmp_diff = abs(next_dist_array[L][i]-tmp_dist);\n\n\t\tauto at = LOC.find(tmp_diff);\n\n\t\tif(at == LOC.end()){\n\t\t\tFLG = false;\n\t\t\tbreak;\n\t\t}\n\n\t\ttmp_loc = LOC[tmp_diff];\n\n\t\tif(next_rest_num[L][tmp_loc] == 0){\n\t\t\tFLG = false;\n\t\t\tbreak;\n\t\t}\n\t\tnext_rest_num[L][tmp_loc]--;\n\t}\n\n\tif(FLG){\n\n\t\tstring tmp_str = make_str(next_dist_array[L]);\n\n\t\tauto ch = MAP.find(tmp_str);\n\n\t\tif(ch == MAP.end()){\n\n\t\t\tMAP[tmp_str] = true;\n\t\t\tint next_index = get_index(next_rest_num[L]);\n\t\t\trecursive(next_dist_array[L],next_rest_num[L],next_index,num_decided+1);\n\t\t}\n\t}\n\n\ttmp_dist = dist_array[N-1]-tmp_dist;\n\n\tauto a_r = LOC.find(tmp_dist);\n\tif(a_r == LOC.end() || rest_num[LOC[tmp_dist]] == 0)return;\n\n\tfor(int i = 0; i < N; i++){\n\t\tif(next_dist_array[R][i] == -1){\n\t\t\tnext_dist_array[R][i] = tmp_dist;\n\t\t\ttmp_pos = i;\n\t\t\tbreak;\n\t\t}\n\t}\n\n\t FLG = true;\n\n\tfor(int i = 0; i < N-1; i++){\n\t\tif(next_dist_array[R][i] == -1 || i == tmp_pos)continue;\n\n\t\ttmp_diff = abs(next_dist_array[R][i]-tmp_dist);\n\n\t\tauto at = LOC.find(tmp_diff);\n\n\t\tif(at == LOC.end()){\n\t\t\tFLG = false;\n\t\t\tbreak;\n\t\t}\n\n\t\ttmp_loc = LOC[tmp_diff];\n\n\t\tif(next_rest_num[R][tmp_loc] == 0){\n\t\t\tFLG = false;\n\t\t\tbreak;\n\t\t}\n\t\tnext_rest_num[R][tmp_loc]--;\n\t}\n\n\tif(FLG){\n\n\t\tstring tmp_str = make_str(next_dist_array[R]);\n\n\t\tauto ch = MAP.find(tmp_str);\n\n\t\tif(ch == MAP.end()){\n\n\t\t\tMAP[tmp_str] = true;\n\t\t\tint next_index = get_index(next_rest_num[R]);\n\t\t\trecursive(next_dist_array[R],next_rest_num[R],next_index,num_decided+1);\n\t\t}\n\t}\n}\n\nvoid func(){\n\n\tMAP.clear();\n\tLOC.clear();\n\tANS.clear();\n\tinfo_index = 0;\n\n\tint tmp;\n\tbool FLG;\n\n\tfor(int i = 0; i < (N*(N-1))/2; i++){\n\n\t\tscanf(\"%d\",&tmp);\n\n\t\tFLG = false;\n\n\t\tfor(int k = 0;k < info_index; k++){\n\t\t\tif(info[k].dist == tmp){\n\t\t\t\tinfo[k].count++;\n\t\t\t\tFLG = true;\n\t\t\t\tbreak;\n\t\t\t}\n\t\t}\n\n\t\tif(FLG)continue;\n\n\t\tinfo[info_index].dist = tmp;\n\t\tinfo[info_index].count = 1;\n\t\tinfo_index++;\n\t}\n\n\tfor(int i = 0; i < info_index; i++){\n\t\tLOC[info[i].dist] = i;\n\t}\n\n\tif(info[0].count != 1){\n\t\tprintf(\"-----\\n\");\n\t\treturn;\n\t}\n\n\tint first_dist_array[NUM];\n\tint first_rest_num[(NUM*(NUM-1))/2];\n\n\tfirst_dist_array[0] = 0;\n\tfor(int i = 1; i <= N-2; i++)first_dist_array[i] = -1;\n\tfirst_dist_array[N-1] = info[0].dist;\n\n\tfirst_rest_num[0] = 0;\n\tfor(int i = 1; i < info_index; i++)first_rest_num[i] = info[i].count;\n\n\trecursive(first_dist_array,first_rest_num,1,2);\n\n\tif(ANS.size() > 0){\n\t\tsort(ANS.begin(),ANS.end());\n\t\tANS.erase(unique(ANS.begin(),ANS.end()),ANS.end());\n\t}\n\n\tfor(int i = 0; i < ANS.size(); i++){\n\t\tprintf(\"%d\",ANS[i].table[0]);\n\t\tfor(int k = 1; k < N-1; k++){\n\t\t\tprintf(\" %d\",ANS[i].table[k]);\n\t\t}\n\t\tprintf(\"\\n\");\n\t}\n\n\tprintf(\"-----\\n\");\n}\n\nint main(){\n\n\twhile(true){\n\t\tscanf(\"%d\",&N);\n\t\tif(N == 0)break;\n\n\t\tfunc();\n\t}\n\n\treturn 0;\n}", "accuracy": 1, "time_ms": 30, "memory_kb": 3464, "score_of_the_acc": -0.0284, "final_rank": 11 }, { "submission_id": "aoj_1307_3020035", "code_snippet": "#include<bits/stdc++.h>\ntypedef long long int ll;\ntypedef unsigned long long int ull;\n#define BIG_NUM 2000000000\n#define MOD 1000000007\n#define EPS 0.000000001\nusing namespace std;\n\n\n#define NUM 20\n\nenum Type{\n\tL,\n\tR,\n};\n\nstruct Info{\n\tbool operator<(const struct Info &arg) const{ //距離の降順\n\t\treturn dist > arg.dist;\n\t}\n\tint dist,count;\n};\n\nstruct Answer{\n\n\tvoid set(int arg_length,int arg_table[NUM]){\n\t\tlength = arg_length;\n\t\tfor(int i = 0; i < length; i++){\n\t\t\ttable[i] = arg_table[i];\n\t\t}\n\t}\n\tbool operator<(const struct Answer &arg) const{\n\t\tfor(int i = 0; i < length; i++){\n\t\t\tif(table[i] != arg.table[i]){\n\t\t\t\treturn table[i] < arg.table[i];\n\t\t\t}\n\t\t}\n\t\treturn true;\n\t}\n\n\tbool operator==(const struct Answer &arg) const{\n\n\t\tbool FLG = true;\n\t\tfor(int i = 0; i < length; i++){\n\t\t\tif(table[i] != arg.table[i]){\n\t\t\t\tFLG = false;\n\t\t\t}\n\t\t}\n\t\treturn FLG;\n\t}\n\n\tint table[NUM],length;\n};\n\nint N;\nint info_index;\nInfo info[(NUM*(NUM-1))/2];\nmap<string,bool> MAP;\nmap<int,int> LOC;\nvector<Answer> ANS;\n\nstring make_str(int dist_array[NUM]){\n\n\tstring ret;\n\n\tfor(int i = 0; i < info_index; i++){\n\t\tret.append(to_string(dist_array[i])).append(\"@\");\n\t}\n\n\treturn ret;\n}\n\n//0でない最初の場所を返却する関数\nint get_index(int rest_num[(NUM*(NUM-1))/2]){\n\n\tfor(int i = 0; i < info_index; i++){\n\t\tif(rest_num[i] > 0){\n\t\t\treturn i;\n\t\t}\n\t}\n\treturn -1; //Error\n}\n\n//index:rest_numの、非0の最小インデックス\nvoid recursive(int dist_array[NUM],int rest_num[(NUM*(NUM-1))/2],int index,int num_decided){\n\n\tif(num_decided == N){\n\n\t\t//町同士の距離をstring配列化して、ANSに格納\n\t\tint work[NUM];\n\t\tAnswer ans;\n\n\t\tfor(int i = 0; i < N-1; i++){\n\t\t\twork[i] = dist_array[i+1]-dist_array[i];\n\t\t}\n\t\tans.set(N-1,work);\n\n\t\tANS.push_back(ans);\n\n\t\treturn;\n\t}\n\n\t//今回処理する距離\n\tint tmp_dist = info[index].dist;\n\trest_num[index]--;\n\n\tint next_dist_array[2][NUM],next_rest_num[2][(NUM*(NUM-1))/2];\n\n\tfor(int i = 0; i < 2; i++){\n\t\tfor(int k = 0; k < N; k++){\n\t\t\tnext_dist_array[i][k] = dist_array[k];\n\t\t}\n\t\tfor(int k = 0; k < info_index; k++){\n\t\t\tnext_rest_num[i][k] = rest_num[k];\n\t\t}\n\t}\n\n\t/*★★残っている最大の距離を、左端からの距離と解釈する場合★★*/\n\tint tmp_pos;\n\n\t//右端から数えて、一番近い空きスペースを探す\n\tfor(int i = N-2; i >= 0; i--){\n\t\tif(next_dist_array[L][i] == -1){\n\t\t\tnext_dist_array[L][i] = tmp_dist;\n\t\t\ttmp_pos = i;\n\t\t\tbreak;\n\t\t}\n\t}\n\n\tint tmp_diff,tmp_loc;\n\tbool FLG = true;\n\n\t//既に配置されている他の距離と矛盾がないか調べる\n\tfor(int i = 1; i < N; i++){ //★i == 0からやると、tmp_distをデクリメントしているのでfalseになってしまう★\n\t\tif(next_dist_array[L][i] == -1 || i == tmp_pos)continue;\n\n\t\ttmp_diff = abs(next_dist_array[L][i]-tmp_dist);\n\n\t\tauto at = LOC.find(tmp_diff);\n\n\t\tif(at == LOC.end()){ //入力にない距離なら不適\n\t\t\tFLG = false;\n\t\t\tbreak;\n\t\t}\n\n\t\ttmp_loc = LOC[tmp_diff];\n\n\t\tif(next_rest_num[L][tmp_loc] == 0){ //距離がもう残っていない場合は不適\n\t\t\tFLG = false;\n\t\t\tbreak;\n\t\t}\n\t\tnext_rest_num[L][tmp_loc]--;\n\t}\n\n\tif(FLG){ //計算済みでないか調べる\n\n\t\tstring tmp_str = make_str(next_dist_array[L]);\n\n\t\tauto ch = MAP.find(tmp_str);\n\n\t\tif(ch == MAP.end()){\n\n\t\t\tMAP[tmp_str] = true;\n\t\t\tint next_index = get_index(next_rest_num[L]);\n\t\t\trecursive(next_dist_array[L],next_rest_num[L],next_index,num_decided+1);\n\t\t}\n\t}\n\n\t/*★残っている最大の距離を、右端からの距離と解釈する場合★*/\n\n\ttmp_dist = dist_array[N-1]-tmp_dist; //★★左端からの距離にする(右端からの距離 == tmp_dist)★★\n\n\tauto a_r = LOC.find(tmp_dist);\n\tif(a_r == LOC.end() || rest_num[LOC[tmp_dist]] == 0)return;\n\n\t//左端から数えて、一番近い空きスペースを探す\n\tfor(int i = 0; i < N; i++){\n\t\tif(next_dist_array[R][i] == -1){\n\t\t\tnext_dist_array[R][i] = tmp_dist;\n\t\t\ttmp_pos = i;\n\t\t\tbreak;\n\t\t}\n\t}\n\n\t FLG = true;\n\n\t//既に配置されている他の距離と矛盾がないか調べる\n\tfor(int i = 0; i < N-1; i++){ //★i == N-1までやると、tmp_distをデクリメントしているのでfalseになってしまう★\n\t\tif(next_dist_array[R][i] == -1 || i == tmp_pos)continue;\n\n\t\ttmp_diff = abs(next_dist_array[R][i]-tmp_dist);\n\n\t\tauto at = LOC.find(tmp_diff);\n\n\t\tif(at == LOC.end()){ //入力にない距離なら不適\n\t\t\tFLG = false;\n\t\t\tbreak;\n\t\t}\n\n\t\ttmp_loc = LOC[tmp_diff];\n\n\t\tif(next_rest_num[R][tmp_loc] == 0){ //距離がもう残っていない場合は不適\n\t\t\tFLG = false;\n\t\t\tbreak;\n\t\t}\n\t\tnext_rest_num[R][tmp_loc]--;\n\t}\n\n\tif(FLG){ //計算済みでないか調べる\n\n\t\tstring tmp_str = make_str(next_dist_array[R]);\n\n\t\tauto ch = MAP.find(tmp_str);\n\n\t\tif(ch == MAP.end()){\n\n\t\t\tMAP[tmp_str] = true;\n\t\t\tint next_index = get_index(next_rest_num[R]);\n\t\t\trecursive(next_dist_array[R],next_rest_num[R],next_index,num_decided+1);\n\t\t}\n\t}\n}\n\nvoid func(){\n\n\tMAP.clear();\n\tLOC.clear();\n\tANS.clear();\n\tinfo_index = 0;\n\n\tint tmp;\n\tbool FLG;\n\n\t//距離の入力\n\tfor(int i = 0; i < (N*(N-1))/2; i++){\n\n\t\tscanf(\"%d\",&tmp);\n\n\t\tFLG = false;\n\n\t\t//既に登場した距離か調べる\n\t\tfor(int k = 0;k < info_index; k++){\n\t\t\tif(info[k].dist == tmp){\n\t\t\t\tinfo[k].count++;\n\t\t\t\tFLG = true;\n\t\t\t\tbreak;\n\t\t\t}\n\t\t}\n\n\t\tif(FLG)continue;\n\n\t\tinfo[info_index].dist = tmp;\n\t\tinfo[info_index].count = 1;\n\t\tinfo_index++;\n\t}\n\n\tfor(int i = 0; i < info_index; i++){ //数字の位置を記録\n\t\tLOC[info[i].dist] = i;\n\t}\n\n\tif(info[0].count != 1){ //最長距離が1つでないならreturn\n\t\t//printf(\"not 1\\n\");\n\t\tprintf(\"-----\\n\");\n\t\treturn;\n\t}\n\n\tint first_dist_array[NUM]; //左端の町からの相対距離配列\n\tint first_rest_num[(NUM*(NUM-1))/2]; //距離の使用数の残数配列<★infoの並びに対応★>\n\n\tfirst_dist_array[0] = 0;\n\tfor(int i = 1; i <= N-2; i++)first_dist_array[i] = -1;\n\tfirst_dist_array[N-1] = info[0].dist;\n\n\tfirst_rest_num[0] = 0;\n\tfor(int i = 1; i < info_index; i++)first_rest_num[i] = info[i].count;\n\n\trecursive(first_dist_array,first_rest_num,1,2);\n\n\tif(ANS.size() > 0){\n\t\tsort(ANS.begin(),ANS.end());\n\t\tANS.erase(unique(ANS.begin(),ANS.end()),ANS.end());\n\t}\n\n\tfor(int i = 0; i < ANS.size(); i++){\n\t\tprintf(\"%d\",ANS[i].table[0]);\n\t\tfor(int k = 1; k < N-1; k++){\n\t\t\tprintf(\" %d\",ANS[i].table[k]);\n\t\t}\n\t\tprintf(\"\\n\");\n\t}\n\n\tprintf(\"-----\\n\");\n}\n\nint main(){\n\n\twhile(true){\n\t\tscanf(\"%d\",&N);\n\t\tif(N == 0)break;\n\n\t\tfunc();\n\t}\n\n\treturn 0;\n}", "accuracy": 1, "time_ms": 30, "memory_kb": 3500, "score_of_the_acc": -0.0297, "final_rank": 12 }, { "submission_id": "aoj_1307_2988850", "code_snippet": "#include \"bits/stdc++.h\"\n#include<unordered_map>\n#include<unordered_set>\n#pragma warning(disable:4996)\nusing namespace std;\n\nbool try_insert(map<int,int>&mp,const vector<int>&anss, int place,int rest, int l, int r) {\n\tvector<int>del_nums;\n\n\tif(l==place){\n\t\tint sum=anss[place];\n\t\tdel_nums.push_back(sum);\n\t\tfor (int x = l - 1; x >= 0; --x) {\n\t\t\tsum+=anss[x];\n\t\t\tdel_nums.push_back(sum);\n\t\t}\n\n\t\tsum=rest-anss[place];\n\t\tdel_nums.push_back(sum);\n\t\tfor (int x = r; x < anss.size(); ++x) {\n\t\t\tsum+=anss[x];\n\t\t\tdel_nums.push_back(sum);\n\t\t}\n\t}\n\telse {\n\t\tint sum = rest-anss[place];\n\t\tdel_nums.push_back(sum);\n\t\tfor (int x = l - 1; x >= 0; --x) {\n\t\t\tsum += anss[x];\n\t\t\tdel_nums.push_back(sum);\n\t\t}\n\n\t\tsum = anss[place];\n\t\tdel_nums.push_back(sum);\n\t\tfor (int x = r; x < anss.size(); ++x) {\n\t\t\tsum+=anss[x];\n\t\t\tdel_nums.push_back(sum);\n\t\t}\n\t}\n\tfor (auto del_num : del_nums) {\n\t\tauto m=mp.find(del_num);\n\t\tif(m==mp.end())return false;\n\t\telse {\n\t\t\tmp[del_num]--;\n\t\t\tif(mp[del_num]==0)mp.erase(m);\n\t\t}\n\t}\n\treturn true;\n}\n\nvector<vector<int>>answers;\n\nvoid dfs(const vector<int>&anss, int rest,map<int,int>&v,int l,int r) {\n\tif (l + 1 == r) {\n\t\tassert(v.empty());\n\t\t{\n\t\t\tauto n_anss(anss);\n\t\t\tn_anss[l]=rest;\n\t\t\tanswers.push_back(n_anss);\n\t\t}\n\t\treturn ;\n\t}\n\telse {\n\t\tint max_num = prev(v.end())->first;\n\t\t{\n\t\t\tint l_num = max_num - accumulate(anss.begin(), anss.begin() + l, 0);\n\t\t\tif (l_num <= 0) {\n\t\t\t}\n\t\t\telse {\n\t\t\t\tmap<int, int>next_v(v);\n\t\t\t\tauto n_anss(anss);\n\t\t\t\tn_anss[l] = l_num;\n\t\t\t\tif (try_insert(next_v, n_anss, l, rest, l, r)) {\n\t\t\t\t\tdfs(n_anss, rest - l_num, next_v, l + 1, r);\n\t\t\t\t}\n\t\t\t}\n\n\n\t\t}\n\t\t{\n\t\t\tint l_num = (rest + accumulate(anss.begin() + r, anss.end(), 0)) - max_num;\n\t\t\tif (l_num <= 0) {\n\t\t\t}\n\t\t\telse {\n\t\t\t\tmap<int, int>next_v(v);\n\t\t\t\tauto n_anss(anss);\n\t\t\t\tn_anss[l] = l_num;\n\t\t\t\tif (try_insert(next_v, n_anss, l, rest, l, r)) {\n\t\t\t\t\tdfs(n_anss, rest - l_num, next_v, l + 1, r);\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\t{\n\t\t\tint r_num = max_num - accumulate(anss.begin() + r, anss.end(), 0);\n\t\t\tif (r_num <= 0) {\n\t\t\t}\n\t\t\telse {\n\t\t\t\tmap<int, int>next_v(v);\n\t\t\t\tauto n_anss(anss);\n\t\t\t\tn_anss[r - 1] = r_num;\n\t\t\t\tif (try_insert(next_v, n_anss, r - 1, rest, l, r)) {\n\t\t\t\t\tdfs(n_anss, rest - r_num, next_v, l, r - 1);\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\t{\n\t\t\tint r_num = (rest + accumulate(anss.begin(), anss.begin() + l, 0)) - max_num;\n\t\t\tif (r_num <= 0) {\n\t\t\t}\n\t\t\telse {\n\t\t\t\tmap<int, int>next_v(v);\n\t\t\t\tauto n_anss(anss);\n\t\t\t\tn_anss[r - 1] = r_num;\n\t\t\t\tif (try_insert(next_v, n_anss, r - 1, rest, l, r)) {\n\t\t\t\t\tdfs(n_anss, rest - r_num, next_v, l, r - 1);\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\treturn ;\n\t}\n\t\n}\n\nvoid solve(int N) {\n\tvector<int>v(N*(N-1)/2);\n\tmap<int,int>mp;\n\tfor(int i=0;i<(N*(N-1))/2;++i)cin>>v[i];\n\tfor(int i=1;i<(N*(N-1))/2;++i)mp[v[i]]++;\n\n\tif (N ==2) {\n\t\tanswers=(vector<vector<int>>{vector<int>{1}});\n\t}\n\telse {\n\n\t\tvector<int>anss(N - 1, -1);\n\t\tdfs(anss, v[0], mp, 0, N - 1);\n\t}\n}\n\nbool operator <(const vector<int>&l, const vector<int>&r) {\n\tfor (int i = 0; i < l.size(); ++i) {\n\t\tif(l[i]<r[i])return true;\n\t\telse if(l[i]>r[i])return false;\n\t}\n\treturn false;\n}\n\nint main()\n{\n\tint N;\n\twhile (cin >> N) {\n\t\tif(!N)break;\n\t\tanswers.clear();\n\t\tsolve(N);\n\t\tsort(answers.begin(),answers.end());\n\t\tanswers.erase(unique(answers.begin(),answers.end()),answers.end());\n\t\tfor (auto answer : answers) {\n\t\t\tfor (int k = 0; k < N-1; ++k) {\n\t\t\t\tcout<<answer[k];\n\t\t\t\tif(k==N-2)cout<<endl;\n\t\t\t\telse cout<<\" \";\n\t\t\t}\n\t\t}\n\t\tcout<<\"-----\"<<endl;\n\t}\n\n\treturn 0;\n}", "accuracy": 1, "time_ms": 60, "memory_kb": 3284, "score_of_the_acc": -0.0314, "final_rank": 13 }, { "submission_id": "aoj_1307_2610427", "code_snippet": "#include<bits/stdc++.h>\n\nusing namespace std;\n\n\n\n#define FOR(i,bg,ed) for(int i=bg;i<ed;i++)\n#define REP(i,n) FOR(i,0,n)\n#define ALL(v) (v).begin(),(v).end()\n\n#define fi first\n#define se second\n#define pb push_back\ntypedef long long LL;\ntypedef vector<LL> V;\ntypedef vector<V> VV;\n\nset<VV> memo; \nset<V> ret;\n\nbool g(VV& dist,int l,int r,map<LL,int>& cnt){\n FOR(k,l+1,r){\n if(dist[k][l]==-1&&dist[r][k]==-1)continue;\n if(dist[k][l]==-1){\n dist[k][l]=dist[r][l]-dist[r][k];\n if(cnt.count(dist[k][l])==0)return false;\n if(--cnt[dist[k][l]]==0)cnt.erase(dist[k][l]);\n if(g(dist,l,k,cnt)==false)return false;\n }\n if(dist[r][k]==-1){\n dist[r][k]=dist[r][l]-dist[k][l];\n if(cnt.count(dist[r][k])==0)return false;\n if(--cnt[dist[r][k]]==0)cnt.erase(dist[r][k]);\n if(g(dist,k,r,cnt)==false)return false;\n }\n }\n return true;\n}\n\nvoid f(int N,VV dist,int l,int r,map<LL,int> m){\n if(memo.count(dist))return;\n memo.insert(dist);\n if(m.size()==0){\n V r;\n REP(i,N-1){\n r.pb(dist[i+1][i]);\n }\n ret.insert(r);\n return;\n }\n const int bg=0;\n const int ed=N-1;\n {\n VV nxt = dist;\n auto cp = m;\n LL x = cp.rbegin()->fi;\n nxt[r][bg]=x;\n if(--cp[x] == 0)cp.erase(x);\n LL y =nxt[ed][bg]-x;\n nxt[ed][r]=y;\n if(cp.count(y)==1){\n if(--cp[y]==0)cp.erase(y);\n if(g(nxt,bg,r,cp)&&g(nxt,r,ed,cp))f(N,nxt,l,r-1,cp);\n }\n }\n\n {\n VV nxt = dist;\n auto cp = m;\n LL x = cp.rbegin()->fi;\n nxt[ed][l]=x;\n if(--cp[x] == 0)cp.erase(x);\n LL y =nxt[ed][bg]-x;\n nxt[l][bg]=y;\n if(cp.count(y)==1){\n if(--cp[y] == 0)cp.erase(y);\n if(g(nxt,bg,l,cp)&&g(nxt,l,ed,cp))\n f(N,nxt,l+1,r,cp);\n }\n }\n\n}\nvoid solve(int N){\n if(N>=2){ \n memo.clear();\n ret.clear();\n map<LL,int> d;\n int M = N*(N-1) /2;\n VV dist(N);\n REP(i,N)dist[i].assign(i,-1);\n REP(i,M){\n LL x;\n cin>>x;\n if(i==0){\n dist[N-1][0]=x;\n }\n else{\n d[x]++;\n }\n }\n \n f(N,dist,1,N-2,d);\n for(auto &v:ret){\n REP(i,N-1)cout<<\" \"+(!i)<<v[i];cout<<endl;\n }\n }\n cout<<\"-----\"<<endl;\n}\n\nint main(){\n int N;\n while(cin>>N,N)solve(N);\n}", "accuracy": 1, "time_ms": 30, "memory_kb": 4116, "score_of_the_acc": -0.0512, "final_rank": 15 } ]
aoj_1304_cpp
Problem J: Infected Land The earth is under an attack of a deadly virus. Luckily, prompt actions of the Ministry of Health against this emergency successfully confined the spread of the infection within a square grid of areas. Recently, public health specialists found an interesting pattern with regard to the transition of infected areas. At each step in time, every area in the grid changes its infection state according to infection states of its directly (horizontally, vertically, and diagonally) adjacent areas. An infected area continues to be infected if it has two or three adjacent infected areas. An uninfected area becomes infected if it has exactly three adjacent infected areas. An area becomes free of the virus, otherwise. Your mission is to fight against the virus and disinfect all the areas. The Ministry of Health lets an anti-virus vehicle prototype under your command. The functionality of the vehicle is summarized as follows. At the beginning of each time step, you move the vehicle to one of the eight adjacent areas. The vehicle is not allowed to move to an infected area (to protect its operators from the virus). It is not allowed to stay in the same area. Following vehicle motion, all the areas, except for the area where the vehicle is in, change their infection states according to the transition rules described above. Special functionality of the vehicle protects its area from virus infection even if the area is adjacent to exactly three infected areas. Unfortunately, this virus-protection capability of the vehicle does not last. Once the vehicle leaves the area, depending on the infection states of the adjacent areas, the area can be infected. The area where the vehicle is in, which is uninfected , has the same effect to its adjacent areas as an infected area as far as the transition rules are concerned. The following series of figures illustrate a sample scenario that successfully achieves the goal. Initially, your vehicle denoted by @ is found at (1, 5) in a 5 × 5-grid of areas, and you see some infected areas which are denoted by # 's. Firstly, at the beginning of time step 1, you move your vehicle diagonally to the southwest, that is, to the area (2, 4). Note that this vehicle motion was possible because this area was not infected at the start of time step 1. Following this vehicle motion, infection state of each area changes according to the transition rules. The column "1-end" of the figure illustrates the result of such changes at the end of time step 1. Note that the area (3, 3) becomes infected because there were two adjacent infected areas and the vehicle was also in an adjacent area, three areas in total. In time step 2, you move your vehicle to the west and position it at (2, 3). Then infection states of other areas change. Note that even if your vehicle had exactly three infected adjacent areas (west, southwest, and south), the area that is being visited by the vehicle is not in ...(truncated)
[ { "submission_id": "aoj_1304_10946407", "code_snippet": "#include <cstdio>\n#include <cstdlib>\n#include <cstring>\n#include <cmath>\n#include <algorithm>\nusing namespace std;\n#define rep(i,n) for (int i = 0; i < (n); i++)\nconst int dx[8] = {1,1,1,-1,-1,-1,0,0};\nconst int dy[8] = {1,-1,0,1,-1,0,1,-1};\nstruct data\n{\n\tint mask,x,y;\n};\nint n;\ndata next_state(int mask, int x, int y)\n{\n\tint T = 0;\n\tmask |= (1 << (x*n+y));\n\trep(i,n)\n\t\trep(j,n)\n\t\t{\n\t\t\tint cnt = 0;\n\t\t\trep(k,8)\n\t\t\t{\n\t\t\t\tint xx = i + dx[k];\n\t\t\t\tint yy = j + dy[k];\n\t\t\t\tif (xx < 0 || xx >= n || yy < 0 || yy >= n)\n\t\t\t\t\tcontinue;\n\t\t\t\tif (mask & (1 << (xx*n+yy)))\n\t\t\t\t\tcnt++;\n\t\t\t}\n\t\t\tif (mask & (1 << (i * n + j)))\n\t\t\t{\n\t\t\t\tif (cnt == 3 || cnt == 2)\n\t\t\t\t\tT |= 1 << (i * n + j);\n\t\t\t}\n\t\t\telse\n\t\t\t{\n\t\t\t\tif (cnt == 3)\n\t\t\t\t\tT |= 1 << (i*n+j);\n\t\t\t}\n\t\t}\n\tT &= ~(1 << (x * n + y));\n\treturn (data){T, x, y};\n}\nbool dfs(data s, int dep)\n{\n\tif (s.mask == 0)\n\t\treturn true;\n\tif (dep == 0)\n\t\treturn false;\n\trep(i,8)\n\t{\n\t\tint xx = s.x + dx[i];\n\t\tint yy = s.y + dy[i];\n\t\tif (xx < 0 || xx >= n || yy < 0 || yy >= n) continue;\n\t\tif (s.mask & (1 << (xx*n+yy)))\n\t\t\tcontinue;\n\t\tif (dfs(next_state(s.mask,xx,yy),dep-1))\n\t\t\treturn true;\n\t}\n\treturn false;\n}\nchar c[10];\nint main()\n{\n\tfor (; scanf(\"%d\", &n), n;)\n\t{\n\t\tdata s = {};\n\t\trep(i,n)\n\t\t{\n\t\t\tscanf(\"%s\", c);\n\t\t\trep(j,n)\n\t\t\t{\n\t\t\t\tswitch(c[j])\n\t\t\t\t{\n\t\t\t\t\tcase '@': s.x = i, s.y = j; break;\n\t\t\t\t\tcase '#': s.mask |= 1 << (i * n + j);break;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\tint ans = -1;\n\t\trep(t,11)\n\t\t\tif (dfs(s, t))\n\t\t\t{\n\t\t\t\tans = t;\n\t\t\t\tbreak;\n\t\t\t}\n\t\tprintf(\"%d\\n\", ans);\n\t}\n\treturn 0;\n}", "accuracy": 1, "time_ms": 210, "memory_kb": 2920, "score_of_the_acc": -0.0118, "final_rank": 1 }, { "submission_id": "aoj_1304_9504768", "code_snippet": "#include <iostream>\n#include <cstdio>\n\n#define rep(i,n) for(int i=0;i<(n);i++)\n\nusing namespace std;\n\nconst int dx[]={1,1,0,-1,-1,-1,0,1},dy[]={0,-1,-1,-1,0,1,1,1};\n\nstruct data1 {\n int S;\n int x,y;\n};\n\nint n;\n\ndata1 next_state(int S, int x, int y) {\n S |= 1 << (y * n + x);\n int T = 0;\n rep(i, n) rep(j, n) {\n int cnt = 0;\n rep(k, 8) {\n int yy = i + dy[k], xx = j + dx[k];\n if (0 <= yy && yy < n && 0 <= xx && xx < n && (S & (1 << (yy * n + xx)))) cnt++;\n }\n if (S & (1 << (i * n + j))) {\n if (cnt == 2 || cnt == 3) {\n T |= 1 << (i * n + j);\n }\n } else {\n if (cnt == 3) {\n T |= 1 << (i * n + j);\n }\n }\n }\n if (T & (1 << (y * n + x))) T -= (1 << (y * n + x));\n data1 result = {T, x, y};\n return result;\n}\n\nbool dfs(data1 D, int t) {\n if (D.S == 0) return true;\n if (t == 0) return false;\n rep(k, 8) {\n int yy = D.y + dy[k], xx = D.x + dx[k];\n if (0 <= yy && yy < n && 0 <= xx && xx < n && (D.S & (1 << (yy * n + xx))) == 0) {\n if (dfs(next_state(D.S, xx, yy), t - 1)) return true;\n }\n }\n return false;\n}\n\nint main() {\n while (scanf(\"%d\", &n), n) {\n data1 ini = {0, 0, 0};\n rep(i, n) {\n char s[6]; \n scanf(\"%s\", s);\n rep(j, n) {\n if (s[j] == '#') ini.S |= 1 << (i * n + j);\n if (s[j] == '@') {\n ini.x = j; \n ini.y = i;\n }\n }\n }\n\n int ans = -1;\n rep(t, 12) {\n if (dfs(ini, t)) { \n ans = t; \n break; \n }\n }\n printf(\"%d\\n\", ans);\n }\n\n return 0;\n}", "accuracy": 1, "time_ms": 460, "memory_kb": 3180, "score_of_the_acc": -0.0952, "final_rank": 7 }, { "submission_id": "aoj_1304_9504703", "code_snippet": "#include <iostream>\n#include <string>\n#include <algorithm>\n#include <queue>\n#include <vector>\n#include <set>\n#include <bitset>\n\nusing namespace std;\n\ntypedef vector<vector<char> > vvchar;\n\nconst int dy[] = {-1, -1, -1, 0, 0, 1, 1, 1};\nconst int dx[] = {-1, 0, 1, -1, 1, -1, 0, 1};\n\nint solve(int n, const string *a) {\n vvchar def(n + 2, vector<char>(n + 2));\n vvchar v1(n + 2, vector<char>(n + 2));\n int at = -1;\n int st = 0;\n int bit = 1;\n\n // Initialize the grid and find the starting positions\n for (int y = 0; y < n; ++y) {\n for (int x = 0; x < n; ++x) {\n if (a[y][x] == '@') {\n at = ((y + 1) << 3) | (x + 1);\n } else if (a[y][x] == '#') {\n v1[y + 1][x + 1] = 1;\n st |= bit;\n }\n bit <<= 1;\n }\n }\n\n if (st == 0) return 0;\n\n queue<int> q;\n q.push(st | (at << 25));\n q.push(-1);\n\n set<int> visit;\n visit.insert(q.front());\n\n int t = 1;\n while (q.size() > 1) {\n st = q.front();\n q.pop();\n\n if (st < 0) {\n ++t;\n q.push(-1);\n continue;\n }\n\n // Reconstruct v1 from the state\n int atx = (st >> 25) & 7;\n int aty = (st >> 28) & 7;\n int mask = 1;\n fill(v1.begin(), v1.end(), vector<char>(n + 2, 0));\n for (int y = 1; y <= n; ++y) {\n for (int x = 1; x <= n; ++x) {\n if (st & mask) {\n v1[y][x] = 1;\n }\n mask <<= 1;\n }\n }\n\n for (int i = 0; i < 8; ++i) {\n int ny = aty + dy[i];\n int nx = atx + dx[i];\n if (ny <= 0 || ny > n || nx <= 0 || nx > n) continue;\n if (v1[ny][nx] != 0) continue;\n\n v1[ny][nx] = 2;\n int nst = 0;\n bit = 1;\n for (int y = 1; y <= n; ++y) {\n for (int x = 1; x <= n; ++x) {\n int cnt = 0;\n for (int j = 0; j < 8; ++j) {\n if (v1[y + dy[j]][x + dx[j]]) {\n ++cnt;\n }\n }\n if (v1[y][x] == 0) {\n if (cnt == 3) {\n nst |= bit;\n }\n } else if (v1[y][x] == 1) {\n if (cnt == 2 || cnt == 3) {\n nst |= bit;\n }\n }\n bit <<= 1;\n }\n }\n v1[ny][nx] = 0;\n\n if (nst == 0) return t;\n\n nst |= (ny << 28) | (nx << 25);\n if (visit.insert(nst).second) {\n q.push(nst);\n }\n }\n }\n return -1;\n}\n\nint main() {\n int n;\n string a[5];\n\n while (cin >> n, n != 0) {\n for (int i = 0; i < n; ++i) {\n cin >> a[i];\n }\n cout << solve(n, a) << '\\n';\n }\n\n return 0;\n}", "accuracy": 1, "time_ms": 160, "memory_kb": 3372, "score_of_the_acc": -0.0422, "final_rank": 3 }, { "submission_id": "aoj_1304_9504689", "code_snippet": "#include <iostream>\n#include <string>\n#include <algorithm>\n#include <queue>\n#include <vector>\n#include <set>\n#include <bitset>\nusing namespace std;\n\ntypedef vector<vector<char> > vvchar;\n\nint dy[] = {-1, -1, -1, 0, 0, 1, 1, 1};\nint dx[] = {-1, 0, 1, -1, 1, -1, 0, 1};\n\nint solve(int n, const string *a) {\n vvchar def(n + 2, vector<char>(n + 2));\n vvchar v1 = def;\n\n int at = -1;\n int st = 0;\n int bit = 1;\n for (int y = 0; y < n; ++y) {\n for (int x = 0; x < n; ++x) {\n if (a[y][x] == '@') {\n at = (y + 1) << 3 | (x + 1);\n } else if (a[y][x] == '#') {\n v1[y + 1][x + 1] = 1;\n st |= bit;\n }\n\n bit <<= 1;\n }\n }\n\n if (st == 0) {\n return 0;\n }\n\n queue<int> q;\n q.push(st | (at << 25));\n q.push(-1);\n\n set<int> visit;\n visit.insert(q.front());\n\n int t = 1;\n while (q.size() > 1) {\n st = q.front();\n q.pop();\n\n if (st < 0) {\n ++t;\n q.push(-1);\n } else {\n v1 = def;\n int atx = (st >> 25) & 7;\n int aty = (st >> 28) & 7;\n\n int mask = 1;\n for (int y = 1; y <= n; ++y) {\n for (int x = 1; x <= n; ++x) {\n if (st & mask) {\n v1[y][x] = 1;\n }\n mask <<= 1;\n }\n }\n\n for (int i = 0; i < 8; ++i) {\n int ny = aty + dy[i];\n int nx = atx + dx[i];\n if (ny <= 0 || ny > n || nx <= 0 || nx > n) {\n continue;\n }\n if (v1[ny][nx] != 0) {\n continue;\n }\n\n v1[ny][nx] = 2;\n\n int nst = 0;\n bit = 1;\n for (int y = 1; y <= n; ++y)\n for (int x = 1; x <= n; ++x) {\n int cnt = 0;\n\n for (int j = 0; j < 8; ++j) {\n if (v1[y + dy[j]][x + dx[j]]) {\n ++cnt;\n }\n }\n\n if (v1[y][x] == 0) {\n if (cnt == 3) {\n nst |= bit;\n }\n } else if (v1[y][x] == 1) {\n if (cnt == 2 || cnt == 3) {\n nst |= bit;\n }\n }\n\n bit <<= 1;\n }\n\n v1[ny][nx] = 0;\n\n if (nst == 0) {\n return t;\n }\n\n nst |= ny << 28 | nx << 25;\n if (visit.insert(nst).second) {\n q.push(nst);\n }\n }\n }\n }\n\n return -1;\n}\n\nint main() {\n int n;\n string a[5];\n\n while (cin >> n, n != 0) {\n for (int i = 0; i < n; ++i) {\n cin >> a[i];\n }\n\n cout << solve(n, a) << '\\n';\n }\n\n return 0;\n}", "accuracy": 1, "time_ms": 160, "memory_kb": 3352, "score_of_the_acc": -0.0403, "final_rank": 2 }, { "submission_id": "aoj_1304_9504023", "code_snippet": "#include <iostream>\n#include <vector>\n#include <set>\n#include <queue>\nusing namespace std;\n\nstruct dat {\n int m, x, y, t;\n};\n\nint n;\n\ndat upd(int m, int tx, int ty, int t) {\n dat tm;\n tm.m = 0;\n m |= (1 << (tx * n + ty));\n for (int x = 0;x < n;x++) {\n for (int y = 0;y < n;y++) {\n int cnt = 0;\n for (int i = -1;i <= 1;i++) {\n for (int j = -1;j <= 1;j++) {\n if (i == 0 && j == 0) continue;\n int nx = x + i, ny = y + j;\n if (0 <= nx && 0 <= ny && nx < n && ny < n && (m & (1 << (nx * n + ny)))) {\n cnt++;\n }\n }\n }\n if (m & (1 << (n * x + y))) {\n if (cnt == 2 || cnt == 3) tm.m |= (1 << (n * x + y));\n } else if (cnt == 3) tm.m |= (1 << (n * x + y));\n }\n }\n if (tm.m & (1 << (tx * n + ty))) tm.m -= (1 << (tx * n + ty));\n tm.t = t;\n tm.x = tx;\n tm.y = ty;\n return tm;\n}\n\nint main() {\n while (true) {\n cin >> n;\n if (!n) break;\n dat s;\n s.m = 0;\n for (int i = 0;i < n;i++) {\n for (int j = 0;j < n;j++) {\n char c; cin >> c;\n if (c == '#') s.m |= (1 << (i * n + j));\n if (c == '@') s.x = i, s.y = j;\n }\n }\n queue<dat> q;\n q.push(s);\n set<vector<int>> st;\n int ans = -1;\n while (!q.empty()) {\n dat v = q.front();\n q.pop();\n if (!v.m) {\n ans = v.t;\n break;\n }\n if (st.find({v.m, v.x, v.y}) != st.end()) continue;\n st.insert({v.m, v.x, v.y});\n for (int i = -1;i <= 1;i++) {\n for (int j = -1;j <= 1;j++) {\n if (i == 0 && j == 0) continue;\n int nx = v.x + i, ny = v.y + j;\n if (0 <= nx && 0 <= ny && nx < n && ny < n && !(v.m & (1 << (nx * n + ny)))) {\n q.push(upd(v.m, nx, ny, v.t + 1));\n }\n }\n }\n }\n cout << ans << \"\\n\";\n }\n}", "accuracy": 1, "time_ms": 690, "memory_kb": 4060, "score_of_the_acc": -0.2316, "final_rank": 8 }, { "submission_id": "aoj_1304_9504014", "code_snippet": "#include<iostream>\n#include<vector>\n#include<set>\n#include<algorithm>\n#include<queue>\n\nusing namespace std;\n\nstruct S{\n int t;\n vector<char> v;\n};\n\nint main(){\n for(int n;cin>>n,n;){\n S is;\n is.t=0;\n is.v.resize(n*n);\n for(int i=0;i<n*n;i++){\n cin>>is.v[i];\n }\n queue<S> que;\n que.push(is);\n set<vector<char> > p;\n while(!que.empty()){\n S c=que.front();\n if(find(c.v.begin(),c.v.end(),'#')==c.v.end())break;\n que.pop();\n if(!p.insert(c.v).second)continue;\n int ps=find(c.v.begin(),c.v.end(),'@')-c.v.begin();\n int x=ps%n;\n int y=ps/n;\n for(int i=-1;i<=1;i++){\n\tfor(int j=-1;j<=1;j++){\n\t int nx=x+j;\n\t int ny=y+i;\n\t if(0<=ny&&ny<n&&0<=nx&&nx<n&&c.v[ny*n+nx]=='.'){\n\t vector<char> nv(n*n);\n\t for(int k=0;k<n;k++){\n\t for(int l=0;l<n;l++){\n\t\tif(k==ny&&l==nx){\n\t\t nv[ny*n+nx]='@';\n\t\t}else{\n\t\t int ni=0;\n\t\t for(int m=max(0,k-1);m<=min(k+1,n-1);m++){\n\t\t for(int o=max(0,l-1);o<=min(l+1,n-1);o++){\n\t\t ni+=c.v[m*n+o]=='#'||ny==m&&nx==o;\n\t\t }\n\t\t }\n\t\t nv[k*n+l]=((c.v[k*n+l]=='#'&&(ni==3||ni==4))\n\t\t\t ||(c.v[k*n+l]!='#'&&ni==3))?'#':'.';\n\t\t}\n\t }\n\t }\n\t S ns={c.t+1,nv};\n\t que.push(ns);\n\t }\n\t}\n }\n }\n cout<<(que.empty()?-1:que.front().t)<<endl;\n }\n return 0;\n}", "accuracy": 1, "time_ms": 950, "memory_kb": 4860, "score_of_the_acc": -0.3677, "final_rank": 12 }, { "submission_id": "aoj_1304_9503966", "code_snippet": "#include <iostream>\n#include <cstdio>\n\n#define rep(i,n) for(int i=0;i<(n);i++)\n\nusing namespace std;\n\nconst int dx[]={1,1,0,-1,-1,-1,0,1},dy[]={0,-1,-1,-1,0,1,1,1};\n\nstruct data1 {\n int S;\n int x,y;\n};\n\nint n;\n\ndata1 next_state(int S, int x, int y) {\n S |= 1 << (y * n + x);\n int T = 0;\n rep(i, n) rep(j, n) {\n int cnt = 0;\n rep(k, 8) {\n int yy = i + dy[k], xx = j + dx[k];\n if (0 <= yy && yy < n && 0 <= xx && xx < n && (S & (1 << (yy * n + xx)))) cnt++;\n }\n if (S & (1 << (i * n + j))) {\n if (cnt == 2 || cnt == 3) {\n T |= 1 << (i * n + j);\n }\n } else {\n if (cnt == 3) {\n T |= 1 << (i * n + j);\n }\n }\n }\n T &= ~(1 << (y * n + x));\n data1 result = {T, x, y};\n return result;\n}\n\nbool dfs(data1 D, int t) {\n if (D.S == 0) return true;\n if (t == 0) return false;\n rep(k, 8) {\n int yy = D.y + dy[k], xx = D.x + dx[k];\n if (0 <= yy && yy < n && 0 <= xx && xx < n && (D.S & (1 << (yy * n + xx))) == 0) {\n if (dfs(next_state(D.S, xx, yy), t - 1)) return true;\n }\n }\n return false;\n}\n\nint main() {\n while (scanf(\"%d\", &n), n) {\n data1 ini = {0, 0, 0};\n rep(i, n) {\n char s[6]; \n scanf(\"%s\", s);\n rep(j, n) {\n if (s[j] == '#') ini.S |= 1 << (i * n + j);\n if (s[j] == '@') {\n ini.x = j; \n ini.y = i;\n }\n }\n }\n\n int ans = -1;\n rep(t, 11) {\n if (dfs(ini, t)) { \n ans = t; \n break; \n }\n }\n printf(\"%d\\n\", ans);\n }\n\n return 0;\n}", "accuracy": 1, "time_ms": 300, "memory_kb": 3168, "score_of_the_acc": -0.0562, "final_rank": 4 }, { "submission_id": "aoj_1304_9503963", "code_snippet": "#include<iostream>\n#include<cstdio>\n#include<cstring>\n#include<vector>\n#include<set>\n#include<algorithm>\n\nusing namespace std;\n\n#define FOR(I, A, B) for (int I = int(A); I < int(B); ++I)\n\nconst int Q(2000010), N(6);\nconst int dir[8][2] = {{1, 1}, {-1, -1}, {-1, 1}, {1, -1}, {1, 0}, {0, 1}, {-1, 0}, {0, -1}};\n\nint n, t, x, y, s;\nint q[Q], d[Q];\nchar str[N][N];\nset<int> ha;\n\nint state(int s, int x, int y) {\n return s * 25 + x * 5 + y;\n}\nvoid add(int s, int _d) {\n if (ha.count(s)) return;\n d[t] = _d; q[t++] = s; ha.insert(s);\n}\nint p(int x, int y) {return x * n + y;}\nint bit(int x, int y) {\n return (x >> y) & 1;\n}\nint cnt(int ss, int x, int y) {\n int ret = 0;\n FOR(i, 0, 8) {\n int tx = x + dir[i][0], ty = y + dir[i][1];\n if (tx == -1 || ty == -1 || tx == n || ty == n) continue;\n ret += bit(ss, p(tx, ty));\n }\n return ret;\n}\nint bfs() {\n if (s == 0) return 0;\n FOR(h, 0, t) {\n y = q[h] % 5; x = q[h] % 25 / 5; s = q[h] / 25;\n FOR(i, 0, 8) {\n int tx = x + dir[i][0], ty = y + dir[i][1];\n if (tx == -1 || ty == -1 || tx == n || ty == n || bit(s, p(tx, ty))) continue;\n int ss = s | (1 << p(tx, ty)), ts = 0;\n FOR(j, 0, n)\n FOR(k, 0, n) {\n int _cnt = cnt(ss, j, k);\n if (bit(ss, p(j, k)) == 0) {\n if (_cnt == 3) ts |= 1 << p(j, k);\n }\n else {\n if (_cnt == 2 || _cnt == 3) ts |= 1 << p(j, k);\n }\n }\n if (bit(ts, p(tx, ty))) ts -= 1 << p(tx, ty);\n if (ts == 0) return d[h] + 1;\n add(state(ts, tx, ty), d[h] + 1);\n }\n }\n return -1;\n}\nint main() {\n while (scanf(\"%d\", &n), n) {\n ha.clear();\n FOR(i, 0, n)\n scanf(\"%s\", str[i]);\n t = s = 0;\n FOR(i, 0, n)\n FOR(j, 0, n)\n if (str[i][j] == '@') {\n x = i; y = j;\n }\n else if (str[i][j] == '#') s |= 1 << p(i, j);\n add(state(s, x, y), 0);\n printf(\"%d\\n\", bfs());\n }\n return 0;\n}", "accuracy": 1, "time_ms": 180, "memory_kb": 3484, "score_of_the_acc": -0.0573, "final_rank": 5 }, { "submission_id": "aoj_1304_4885528", "code_snippet": "#include \"iostream\"\n#include \"climits\"\n#include \"list\"\n#include \"queue\"\n#include \"stack\"\n#include \"set\"\n#include \"functional\"\n#include \"algorithm\"\n#include \"string\"\n#include \"map\"\n#include \"unordered_map\"\n#include \"unordered_set\"\n#include \"iomanip\"\n#include \"cmath\"\n#include \"random\"\n#include \"bitset\"\n#include \"cstdio\"\n#include \"numeric\"\n#include \"cassert\"\n#include \"ctime\"\n\nusing namespace std;\n\nconstexpr long long int MOD = 1000000007;\n//constexpr int MOD = 1000000007;\n//constexpr int MOD = 998244353;\n//constexpr long long int MOD = 998244353;\nconstexpr double EPS = 1e-12;\n\n//int N, M, K, T, H, W, L, R;\nlong long int N, M, K, T, H, W, L, R;\n\nstruct Node {\n\tint bit, x, y;\n\tNode(const int bb, const int xx, const int yy) {\n\t\tbit = bb, x = xx, y = yy;\n\t}\n\tbool operator<(const Node&n)const {\n\t\treturn make_pair(bit, make_pair(x, y)) < make_pair(n.bit, make_pair(n.x, n.y));\n\t}\n};\n\nint dx[] = { 1,1,1,0,-1,-1,-1,0 };\nint dy[] = { 1,0,-1,-1,-1,0,1,1 };\n\nvector<vector<int>>Field(Node num) {\n\tvector<vector<int>>field(N, vector<int>(N));\n\tfor (int i = N - 1; i >= 0; i--) {\n\t\tfor (int j = N - 1; j >= 0; j--) {\n\t\t\tfield[i][j] = num.bit&1;\n\t\t\tnum.bit >>= 1;\n\t\t}\n\t}\n\treturn field;\n}\n\nNode Next(Node num) {\n\tauto field = Field(num);\n//\tfor (auto i : field) {\n//\t\tfor (auto j : i)cout << j;\n//\t\tcout << endl;\n//\t}\n//\tcout << num.y << \" \" << num.x << endl;\n\tauto nfield = field;\n\tfor (int i = 0; i < N; i++) {\n\t\tfor (int j = 0; j < N; j++) {\n\t\t\tif (i == num.y&&j == num.x)continue;\n\t\t\tint cnt = 0;\n\t\t\tfor (int k = 0; k < 8; k++) {\n\t\t\t\tint ny = i + dy[k];\n\t\t\t\tint nx = j + dx[k];\n\t\t\t\tif (ny < 0 || nx < 0 || ny >= N || nx >= N)continue;\n\t\t\t\tcnt += field[ny][nx] || (num.y == ny && num.x == nx);\n\t\t\t}\n\t\t\tif (field[i][j]) {\n\t\t\t\tif (cnt == 2 || cnt == 3)nfield[i][j] = 1;\n\t\t\t\telse nfield[i][j] = 0;\n\t\t\t}\n\t\t\telse {\n\t\t\t\tif (cnt == 3)nfield[i][j] = 1;\n\t\t\t\telse nfield[i][j] = 0;\n\t\t\t}\n\t\t}\n\t}\n//\tfor (auto i : nfield) {\n//\t\tfor (auto j : i)cout << j;\n//\t\tcout << endl;\n//\t}\n//\tcout << num.y << \" \" << num.x << endl;\n\tnum.bit = 0;\n\tfor (int i = 0; i < N; i++) {\n\t\tfor (int j = 0; j < N; j++) {\n\t\t\tnum.bit <<= 1;\n\t\t\tnum.bit += nfield[i][j];\n\t\t}\n\t}\n\treturn num;\n}\n\nint main() {\n\tios::sync_with_stdio(false);\n\tcin.tie(0);\n\n\twhile (cin >> N, N) {\n\t\tvector<string>s(N);\n\t\tfor (auto &i : s)cin >> i;\n\t\tint y, x;\n\t\tqueue<Node>Q;\n\t\tint b = 0;\n\t\tfor (int i = 0; i < N; i++) {\n\t\t\tfor (int j = 0; j < N; j++) {\n\t\t\t\tb <<= 1;\n\t\t\t\tif (s[i][j] == '@') {\n\t\t\t\t\ty = i, x = j;\n\t\t\t\t}\n\t\t\t\tb |= s[i][j] == '#';\n\t\t\t}\n\t\t}\n\t\tQ.push(Node(b, x, y));\n\t\tmap<Node, int>mp;\n\t\tmp[Node(b, x, y)] = 0;\n\t\tint ans = -1;\n\t\twhile (!Q.empty()) {\n\t\t\tauto cn = Q.front();\n\t\t\tQ.pop();\n\t\t\tif (!cn.bit) {\n\t\t\t\tans = mp[cn];\n\t\t\t\tbreak;\n\t\t\t}\n\t\t\tauto field = Field(cn);\n\t\t\tfor (int i = 0; i < 8; i++) {\n\t\t\t\tint ny = cn.y + dy[i];\n\t\t\t\tint nx = cn.x + dx[i];\n\t\t\t\tif (ny < 0 || nx < 0 || ny >= N || nx >= N || field[ny][nx])continue;\n\t\t\t\tauto nnode = Next(Node(cn.bit, nx, ny));\n\t\t\t\tif (mp.find(nnode) == mp.end()) {\n\t\t\t\t\tmp[nnode] = mp[cn] + 1;\n\t\t\t\t\tQ.push(nnode);\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\tcout << ans << endl;\n\t}\n}", "accuracy": 1, "time_ms": 1810, "memory_kb": 4712, "score_of_the_acc": -0.5572, "final_rank": 14 }, { "submission_id": "aoj_1304_4830287", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\ntypedef long long ll;\ntypedef pair<ll, ll> l_l;\ntypedef pair<int, int> i_i;\ntemplate<class T>\ninline bool chmax(T &a, T b) {\n if(a < b) {\n a = b;\n return true;\n }\n return false;\n}\n\ntemplate<class T>\ninline bool chmin(T &a, T b) {\n if(a > b) {\n a = b;\n return true;\n }\n return false;\n}\n\nconst long double EPS = 1e-10;\nconst long long INF = 1e18;\nconst long double PI = acos(-1.0L);\n//const ll mod = 1000000007;\nint N;\n\nvector<string> f(vector<string> S) {\n auto ret = S;\n for(int h = 0; h < N; h++) {\n for(int w = 0; w < N; w++) {\n if(S[h][w] == '@') continue;\n int cnt = 0;\n for(int newh = h - 1; newh <= h + 1; newh++) {\n for(int neww = w-1; neww <= w+1; neww++) {\n if(newh==h and neww==w) continue;\n if(newh < 0) continue;\n if(newh >= N) continue;\n if(neww < 0) continue;\n if(neww >= N) continue;\n if(S[newh][neww] != '.') cnt++;\n }\n }\n if(S[h][w] == '#') {\n if(cnt == 2 or cnt == 3) {\n ret[h][w] = '#';\n } else {\n ret[h][w] = '.';\n }\n } else {\n if(cnt == 3) {\n ret[h][w] = '#';\n } else {\n ret[h][w] = '.';\n }\n }\n }\n }\n return ret;\n}\n\nvoid print(vector<string> S) {\n for(auto tmp : S) cout << tmp << endl;\n}\n\nvoid solve() {\n vector<string> S(N);\n for(int i = 0; i < N; i++) {\n cin >> S[i];\n }\n map<vector<string>, ll> mp;\n queue<vector<string>> que;\n mp[S] = 0;\n que.push(S);\n while(!que.empty()) {\n auto S = que.front();\n que.pop();\n //cerr << mp[S] << endl;\n //print(S);\n int H, W;\n bool infected = false;\n for(int h = 0; h < N; h++) {\n for(int w = 0; w < N; w++) {\n if(S[h][w] == '@') {\n H = h;\n W = w;\n }\n if(S[h][w] == '#') {\n infected = true;\n }\n }\n }\n if(!infected) {\n cout << mp[S] << endl;\n return;\n }\n for(int h = H - 1; h <= H + 1; h++) {\n for(int w = W - 1; w <= W + 1; w++) {\n if(h == H and w == W) continue;\n if(h < 0) continue;\n if(h >= N) continue;\n if(w < 0) continue;\n if(w >= N) continue;\n if(S[h][w] == '#') continue;\n auto A = S;\n A[H][W] = '.';\n A[h][w] = '@';\n A = f(A);\n if(mp.count(A)) continue;\n mp[A] = mp[S] + 1;\n que.push(A);\n }\n }\n }\n cout << -1 << endl;\n}\n\nint main() {\n while(cin >> N) {\n if(N == 0) break;\n solve();\n }\n return 0;\n}", "accuracy": 1, "time_ms": 4390, "memory_kb": 13632, "score_of_the_acc": -1.9993, "final_rank": 20 }, { "submission_id": "aoj_1304_4827911", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nvector<int> dy = {1, 0, -1, 0, 1, 1, -1, -1};\nvector<int> dx = {0, 1, 0, -1, 1, -1, -1, 1};\nint next(int S, int n){\n int S2 = 0;\n for (int i = 0; i < n; i++){\n for (int j = 0; j < n; j++){\n int cnt = 0;\n for (int k = 0; k < 8; k++){\n int y2 = i + dy[k];\n int x2 = j + dx[k];\n if (0 <= y2 && y2 < n && 0 <= x2 && x2 < n){\n int id = y2 * n + x2;\n if (S >> id & 1){\n cnt++;\n }\n }\n }\n bool flg = false;\n if (S >> (i * n + j) & 1){\n flg = true;\n }\n if (flg && (cnt == 2 || cnt == 3)){\n S2 |= 1 << (i * n + j);\n }\n if (!flg && cnt == 3){\n S2 |= 1 << (i * n + j);\n }\n }\n }\n return S2;\n}\nint main(){\n while (1){\n int n;\n cin >> n;\n if (n == 0){\n break;\n }\n vector<vector<char>> a(n, vector<char>(n));\n for (int i = 0; i < n; i++){\n for (int j = 0; j < n; j++){\n cin >> a[i][j];\n }\n }\n int S = 0;\n int p = 0;\n for (int i = 0; i < n; i++){\n for (int j = 0; j < n; j++){\n if (a[i][j] == '@'){\n p = i * n + j;\n }\n if (a[i][j] == '#'){\n S += 1 << (i * n + j);\n }\n }\n }\n set<pair<int, int>> used;\n used.insert(make_pair(S, p));\n int ans = -1;\n queue<tuple<int, int, int>> Q;\n Q.push(make_tuple(0, S, p));\n while (!Q.empty()){\n int d = get<0>(Q.front());\n int s = get<1>(Q.front());\n int c = get<2>(Q.front());\n Q.pop();\n if (s == 0){\n ans = d;\n break;\n }\n int y = c / n;\n int x = c % n;\n for (int i = 0; i < 8; i++){\n int y2 = y + dy[i];\n int x2 = x + dx[i];\n if (0 <= y2 && y2 < n && 0 <= x2 && x2 < n){\n int c2 = y2 * n + x2;\n if (!(s >> c2 & 1)){\n int s2 = s | (1 << c2);\n s2 = next(s2, n);\n s2 &= ~(1 << c2);\n if (!used.count(make_pair(s2, c2))){\n used.insert(make_pair(s2, c2));\n Q.push(make_tuple(d + 1, s2, c2));\n }\n }\n }\n }\n }\n cout << ans << endl;\n }\n}", "accuracy": 1, "time_ms": 670, "memory_kb": 4656, "score_of_the_acc": -0.2825, "final_rank": 10 }, { "submission_id": "aoj_1304_4784162", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\ntypedef long long ll;\ntemplate<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return true; } return false; }\ntemplate<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return true; } return false; }\n#define all(x) (x).begin(),(x).end()\n#define fi first\n#define se second\n#define mp make_pair\n#define si(x) int(x.size())\nconst int mod=1000000007,MAX=105,INF=1<<27;\nmap<pair<int,int>,int> MA;\nint N;\n\nvoid BFS(int bit,int s){\n queue<pair<int,int>> Q;\n Q.push(mp(bit,s));\n MA[mp(bit,s)]=0;\n \n while(!Q.empty()){\n auto u=Q.front();Q.pop();\n int h=u.se/N,w=u.se%N;\n int bi=u.fi;\n \n bool check=false;\n \n for(int dh=-1;dh<=1;dh++){\n for(int dw=-1;dw<=1;dw++){\n if(dh==0&&dw==0) continue;\n if(h+dh<0||h+dh>=N||w+dw<0||w+dw>=N) continue;\n if(bi&(1<<((h+dh)*N+(w+dw)))) continue;\n \n pair<int,int> to;\n to.se=(h+dh)*N+(w+dw);\n \n bi+=(1<<to.se);\n \n for(int i=0;i<N;i++){\n for(int j=0;j<N;j++){\n if(i*N+j==to.se) continue;\n int cnt=0;\n for(int ddh=-1;ddh<=1;ddh++){\n for(int ddw=-1;ddw<=1;ddw++){\n if(ddh==0&&ddw==0) continue;\n if(i+ddh<0||i+ddh>=N||j+ddw<0||j+ddw>=N) continue;\n \n if(bi&(1<<((i+ddh)*N+(j+ddw)))) cnt++;\n }\n }\n if(bi&(1<<(i*N+j))){\n if(cnt==2||cnt==3) to.fi+=(1<<(i*N+j));\n }else{\n if(cnt==3) to.fi+=(1<<(i*N+j));\n }\n }\n }\n \n bi-=(1<<to.se);\n \n if(MA.count(to)==0){\n MA[to]=MA[u]+1;\n Q.push(to);\n }\n \n if(to.fi==0) check=true;\n }\n }\n \n if(check) break;\n }\n}\n\nint main(){\n \n std::ifstream in(\"text.txt\");\n std::cin.rdbuf(in.rdbuf());\n cin.tie(0);\n ios::sync_with_stdio(false);\n \n while(1){\n cin>>N;\n if(N==0) break;\n MA.clear();\n \n int s=0,bit=0;\n \n for(int i=0;i<N;i++){\n for(int j=0;j<N;j++){\n char c;cin>>c;\n if(c=='@') s=i*N+j;\n if(c=='#') bit+=1<<(i*N+j);\n }\n }\n \n BFS(bit,s);\n \n int ans=INF;\n \n for(int i=0;i<N*N;i++) if(MA.count(mp(0,i))) chmin(ans,MA[mp(0,i)]);\n \n if(ans==INF) ans=-1;\n \n cout<<ans<<endl;\n }\n}", "accuracy": 1, "time_ms": 210, "memory_kb": 3572, "score_of_the_acc": -0.0726, "final_rank": 6 }, { "submission_id": "aoj_1304_4397772", "code_snippet": "#include <bits/stdc++.h>\n\nusing namespace std;\n\n#define REP(i, n) for (int i=0; i<(n); ++i)\n#define RREP(i, n) for (int i=(int)(n)-1; i>=0; --i)\n#define FOR(i, a, n) for (int i=(a); i<(n); ++i)\n#define RFOR(i, a, n) for (int i=(int)(n)-1; i>=(a); --i)\n\n#define SZ(x) ((int)(x).size())\n#define all(x) begin(x),end(x)\n\n#define dump(x) cerr<<#x<<\" = \"<<(x)<<endl\n#define debug(x) cerr<<#x<<\" = \"<<(x)<<\" (L\"<<__LINE__<<\")\"<<endl;\n\ntemplate<class T>\nostream &operator<<(ostream &os, const vector <T> &v) {\n os << \"[\";\n REP(i, SZ(v)) {\n if (i) os << \", \";\n os << v[i];\n }\n return os << \"]\";\n}\n\ntemplate<class T, class U>\nostream &operator<<(ostream &os, const pair <T, U> &p) {\n return os << \"(\" << p.first << \" \" << p.second << \")\";\n}\n\ntemplate<class T>\nbool chmax(T &a, const T &b) {\n if (a < b) {\n a = b;\n return true;\n }\n return false;\n}\n\ntemplate<class T>\nbool chmin(T &a, const T &b) {\n if (b < a) {\n a = b;\n return true;\n }\n return false;\n}\n\nusing ll = long long;\nusing ull = unsigned long long;\nusing ld = long double;\nusing P = pair<int, int>;\nusing vi = vector<int>;\nusing vll = vector<ll>;\nusing vvi = vector<vi>;\nusing vvll = vector<vll>;\n\nconst ll MOD = 1e9 + 7;\nconst int INF = INT_MAX / 2;\nconst ll LINF = LLONG_MAX / 2;\nconst ld eps = 1e-9;\n\n\nint main() {\n cin.tie(0);\n ios::sync_with_stdio(false);\n cout << fixed << setprecision(10);\n\n const int dy[] = {1, 0, -1, 0, 1, 1, -1, -1},\n dx[] = {0, 1, 0, -1, 1, -1, 1, -1};\n\n for (;;) {\n int n; cin >> n;\n if (n == 0) break;\n\n string a;\n int si, sj;\n REP(i, n) {\n string s; cin >> s;\n a += s;\n REP(j, n) {\n if (s[j] == '@') {\n si = i, sj = j;\n }\n }\n }\n\n queue<tuple<string,int,int>> que;\n map<tuple<string,int,int>,int> dist;\n que.emplace(a, si, sj);\n dist[make_tuple(a, si, sj)] = 0;\n\n int ans = -1;\n\n while (!que.empty()) {\n string board;\n int si, sj;\n int d = dist[que.front()];\n tie(board, si, sj) = que.front();\n que.pop();\n\n bool finish = true;\n REP(i, n*n) {\n if (board[i] == '#') {\n finish = false;\n break;\n }\n }\n if (finish) {\n ans = d;\n break;\n }\n\n REP(k, 8) {\n string tmp = board;\n int y = si + dy[k], x = sj + dx[k];\n if (y < 0 or x < 0 or n <= y or n <= x or\n tmp[y*n + x] == '#') continue;\n tmp[y*n + x] = '@';\n tmp[si*n + sj] = '.';\n string nxt;\n REP(i, n) {\n REP(j, n) {\n if (i == y and j == x) {\n nxt += '@';\n continue;\n }\n int cnt = 0;\n REP(t, 8) {\n int y = i + dy[t], x = j + dx[t];\n if (y < 0 or x < 0 or n <= y or n <= x) continue;\n cnt += tmp[y*n + x] != '.';\n }\n if (tmp[i*n + j] == '#' and (cnt == 2 or cnt == 3)) {\n nxt += '#';\n } else if (tmp[i*n + j] == '.' and cnt == 3) {\n nxt += '#';\n } else {\n nxt += '.';\n }\n }\n }\n if (dist.count(make_tuple(nxt, y, x)) == 0) {\n dist[make_tuple(nxt, y, x)] = d + 1;\n que.emplace(nxt, y, x);\n } else if (chmin(dist[make_tuple(nxt, y, x)], d + 1)){\n que.emplace(nxt, y, x);\n }\n }\n }\n\n cout << ans << endl;\n }\n\n return 0;\n}", "accuracy": 1, "time_ms": 1990, "memory_kb": 7080, "score_of_the_acc": -0.8207, "final_rank": 16 }, { "submission_id": "aoj_1304_4396280", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\nusing vi = vector<int>;\nusing vvi = vector<vi>;\nusing vll = vector<ll>;\nusing vvll = vector<vll>;\nusing P = pair<int, int>;\nconst double eps = 1e-8;\nconst ll MOD = 1000000007;\nconst int INF = INT_MAX / 2;\nconst ll LINF = LLONG_MAX / 2;\ntemplate<typename T1, typename T2>\nvoid chmax(T1 &a, const T2 &b) {\n if (a < b) a = b;\n}\ntemplate<typename T1, typename T2>\nvoid chmin(T1 &a, const T2 &b) {\n if (a > b) a = b;\n}\ntemplate<typename T>\nvoid printv(const vector<T> &s) {\n for (int i = 0; i < (int) (s.size()); ++i) {\n cout << s[i];\n if (i != (int) (s.size()) - 1) cout << \" \";\n }\n cout << \"\\n\";\n}\ntemplate<typename T1, typename T2>\nostream &operator<<(ostream &os, const pair<T1, T2> p) {\n os << p.first << \":\" << p.second;\n return os;\n}\nstruct RollingHash {\n const int base = 9973;\n const int mod[2] = {999999937, 1000000007};\n vector<int> s;\n vector<ll> hash[2], pow[2];\n template<class S>\n RollingHash(const S &s) {\n int n = s.size();\n for (int id = 0; id < 2; ++id) {\n hash[id].assign(n+1, 0);\n pow[id].assign(n+1, 1);\n for (int i = 0; i < n; ++i) {\n hash[id][i+1] = (hash[id][i] * base + s[i]) % mod[id];\n pow[id][i+1] = pow[id][i] * base % mod[id];\n }\n }\n }\n pair<ll,ll> get(int l, int r) {\n ll ret[2];\n for (int id = 0; id < 2; ++id) {\n ret[id] = hash[id][r] - hash[id][l] * pow[id][r - l] % mod[id];\n if (ret[id] < 0) ret[id] += mod[id];\n }\n return { ret[0], ret[1] };\n }\n};\nstruct Elm {\n int d, r, c;\n ll hash;\n vector<string> v;\n};\nbool operator<(const Elm &e1, const Elm &e2) {\n return e1.d > e2.d;\n}\nbool solve() {\n int n;\n cin >> n;\n if (n == 0) return false;\n vector<string> in(n);\n int r = -1, c = -1;\n int cnt = 0;\n for(int i=0;i<n;++i) {\n cin >> in[i];\n for(int j=0;j<n;++j) {\n if (in[i][j] == '@') r = i, c = j;\n cnt += in[i][j] == '#';\n }\n }\n if(cnt == 0) {\n cout << 0 << endl;\n return true;\n }\n map<ll, int> mp;\n queue<Elm> pq;\n ll hash = 0;\n ll now = 1;\n for(int i=0;i<n;++i) {\n for(int j=0;j<n;++j) {\n hash += now * in[i][j];\n hash %= MOD;\n now *= 998244353;\n now %= MOD;\n }\n }\n mp[hash] = 0;\n pq.push({0, r, c, hash, in});\n vi dr = {-1, 1, 0, 0}, dc = {0, 0, -1, 1};\n vi dr2 = {1, 1, 0, -1, -1, -1, 0, 1}, dc2 = {0, -1, -1, -1, 0, 1, 1, 1};\n ll ans = -1;\n while (!pq.empty()) {\n auto now = pq.front();\n pq.pop();\n int d = now.d, r = now.r, c = now.c;\n ll hash = now.hash;\n auto v = now.v;\n if (mp.find(hash) != mp.end() && mp[hash] < d) continue;\n for(int i=0;i<8;++i) {\n if(ans != -1) break;\n vector<string> nxt(n);\n for(int j=0;j<n;++j) {\n nxt[j].assign(n, '.');\n }\n int nr = r + dr2[i], nc = c + dc2[i];\n if (!(0 <= nr && nr < n && 0 <= nc && nc < n) || v[nr][nc] == '#') continue;\n nxt[nr][nc] = '@';\n int cnt_sharp = 0;\n for(int j=0;j<n;++j) {\n for(int k=0;k<n;++k) {\n int cnt = 0;\n for(int l=0;l<8;++l) {\n int adjr = j + dr2[l], adjc = k + dc2[l];\n if (0 <= adjr && adjr < n && 0 <= adjc && adjc < n) {\n cnt += v[adjr][adjc] == '#' || nxt[adjr][adjc] == '@';\n }\n }\n if ((v[j][k] == '#' && (cnt == 2 || cnt == 3)) ||\n ((v[j][k] == '.' || v[j][k] == '@') && nxt[j][k] != '@' && cnt == 3)) {\n nxt[j][k] = '#';\n cnt_sharp++;\n }\n }\n }\n ll nxthash = 0;\n ll nowmul = 1;\n for(int i=0;i<n;++i) {\n for(int j=0;j<n;++j) {\n nxthash += nowmul * nxt[i][j];\n nxthash %= MOD;\n nowmul *= 998244353;\n nowmul %= MOD;\n }\n }\n if (mp.find(nxthash) == mp.end() || mp[nxthash] > d + 1) {\n if(cnt_sharp == 0) {\n ans = d+1;\n break;\n }\n mp[nxthash] = d + 1;\n pq.push({d + 1, nr, nc, nxthash, nxt});\n }\n }\n }\n cout << (ans == LINF ? -1 : ans) << endl;\n return true;\n}\nint main() {\n cin.tie(0);\n ios::sync_with_stdio(false);\n cout << fixed << setprecision(10);\n while (1) {\n if (!solve()) break;\n }\n}", "accuracy": 1, "time_ms": 700, "memory_kb": 4536, "score_of_the_acc": -0.2784, "final_rank": 9 }, { "submission_id": "aoj_1304_4286161", "code_snippet": "#define _CRT_SECURE_NO_WARNINGS\n#define _USE_MATH_DEFINES\n\n#pragma comment (linker, \"/STACK:526000000\")\n\n#include \"bits/stdc++.h\"\n\nusing namespace std;\ntypedef string::const_iterator State;\n#define eps 1e-5L\n#define MAX_MOD 1000000007LL\n#define GYAKU 500000004LL\n#define MOD 998244353LL\n#define pb push_back\n#define mp make_pair\ntypedef long long ll;\n#define REP(a,b) for(long long (a) = 0;(a) < (b);++(a))\n#define ALL(x) (x).begin(),(x).end()\n\n// geometry library\n\ntypedef complex<long double> Point;\ntypedef pair<complex<long double>, complex<long double>> Line;\n\ntypedef struct Circle {\n complex<long double> center;\n long double r;\n}Circle;\n\nlong double dot(Point a, Point b) {\n return (a.real() * b.real() + a.imag() * b.imag());\n}\nlong double cross(Point a, Point b) {\n return (a.real() * b.imag() - a.imag() * b.real());\n}\n\nlong double Dist_Line_Point(Line a, Point b) {\n if (dot(a.second - a.first, b - a.first) < eps) return abs(b - a.first);\n if (dot(a.first - a.second, b - a.second) < eps) return abs(b - a.second);\n return abs(cross(a.second - a.first, b - a.first)) / abs(a.second - a.first);\n}\n\nint is_intersected_ls(Line a, Line b) {\n return (cross(a.second - a.first, b.first - a.first) * cross(a.second - a.first, b.second - a.first) < eps) &&\n (cross(b.second - b.first, a.first - b.first) * cross(b.second - b.first, a.second - b.first) < eps);\n}\n\nPoint intersection_l(Line a, Line b) {\n Point da = a.second - a.first;\n Point db = b.second - b.first;\n return a.first + da * cross(db, b.first - a.first) / cross(db, da);\n}\n\nlong double Dist_Line_Line(Line a, Line b) {\n if (is_intersected_ls(a, b) == 1) {\n return 0;\n }\n return min({ Dist_Line_Point(a,b.first), Dist_Line_Point(a,b.second),Dist_Line_Point(b,a.first),Dist_Line_Point(b,a.second) });\n}\n\npair<Point, Point> intersection_Circle_Circle(Circle a, Circle b) {\n long double dist = abs(a.center - b.center);\n assert(dist <= eps + a.r + b.r);\n assert(dist + eps >= abs(a.r - b.r));\n Point target = b.center - a.center;\n long double pointer = target.real() * target.real() + target.imag() * target.imag();\n long double aa = pointer + a.r * a.r - b.r * b.r;\n aa /= 2.0L;\n Point l{ (aa * target.real() + target.imag() * sqrt(pointer * a.r * a.r - aa * aa)) / pointer,\n (aa * target.imag() - target.real() * sqrt(pointer * a.r * a.r - aa * aa)) / pointer };\n Point r{ (aa * target.real() - target.imag() * sqrt(pointer * a.r * a.r - aa * aa)) / pointer,\n (aa * target.imag() + target.real() * sqrt(pointer * a.r * a.r - aa * aa)) / pointer };\n r = r + a.center;\n l = l + a.center;\n return mp(l, r);\n}\n\n//end of geometry\n\ntemplate<typename A>\nA pows(A val, ll b) {\n assert(b >= 1);\n A ans = val;\n b--;\n while (b) {\n if (b % 2) {\n ans *= val;\n }\n val *= val;\n b /= 2LL;\n }\n return ans;\n}\n\ntemplate<typename A>\nclass Compressor {\npublic:\n bool is_zipped = false;\n map<A, ll> zipper;\n map<ll, A> unzipper;\n queue<A> fetcher;\n Compressor() {\n is_zipped = false;\n zipper.clear();\n unzipper.clear();\n }\n void add(A now) {\n assert(is_zipped == false);\n zipper[now] = 1;\n fetcher.push(now);\n }\n void exec() {\n assert(is_zipped == false);\n int cnt = 0;\n for (auto i = zipper.begin(); i != zipper.end(); ++i) {\n i->second = cnt;\n unzipper[cnt] = i->first;\n cnt++;\n }\n is_zipped = true;\n }\n ll fetch() {\n assert(is_zipped == true);\n A hoge = fetcher.front();\n fetcher.pop();\n return zipper[hoge];\n }\n ll zip(A now) {\n assert(is_zipped == true);\n assert(zipper.find(now) != zipper.end());\n return zipper[now];\n }\n A unzip(ll a) {\n assert(is_zipped == true);\n assert(a < unzipper.size());\n return unzipper[a];\n }\n ll next(A now) {\n auto x = zipper.upper_bound(now);\n if (x == zipper.end()) return zipper.size();\n return (ll)((*x).second);\n }\n ll back(A now) {\n auto x = zipper.lower_bound(now);\n if (x == zipper.begin()) return -1;\n x--;\n return (ll)((*x).second);\n }\n};\n\ntemplate<typename A>\nclass Matrix {\npublic:\n vector<vector<A>> data;\n Matrix(vector<vector<A>> a) :data(a) {\n\n }\n Matrix operator + (const Matrix obj) {\n vector<vector<A>> ans;\n assert(obj.data.size() == this->data.size());\n assert(obj.data[0].size() == this->data[0].size());\n REP(i, obj.data.size()) {\n ans.push_back(vector<A>());\n REP(q, obj.data[i].size()) {\n A hoge = obj.data[i][q] + (this->data[i][q]);\n ans.back().push_back(hoge);\n }\n }\n return Matrix(ans);\n }\n Matrix operator - (const Matrix obj) {\n vector<vector<A>> ans;\n assert(obj.data.size() == this->data.size());\n assert(obj.data[0].size() == this->data[0].size());\n REP(i, obj.data.size()) {\n ans.push_back(vector<A>());\n REP(q, obj.data[i].size()) {\n A hoge = this->data[i][q] - obj.data[i][q];\n ans.back().push_back(hoge);\n }\n }\n return Matrix(ans);\n }\n Matrix operator * (const Matrix obj) {\n vector<vector<A>> ans;\n assert(obj.data.size() == this->data[0].size());\n REP(i, this -> data.size()) {\n ans.push_back(vector<A>());\n REP(q, obj.data[0].size()) {\n A hoge = (this->data[i][0]) * (obj.data[0][q]);\n for (int t = 1; t < obj.data[i].size(); ++t) {\n hoge += this->data[i][t] * obj.data[t][q];\n }\n ans.back().push_back(hoge);\n }\n }\n return Matrix(ans);\n }\n Matrix& operator *= (const Matrix obj) {\n *this = (*this * obj);\n return *this;\n }\n Matrix& operator += (const Matrix obj) {\n *this = (*this + obj);\n return *this;\n }\n Matrix& operator -= (const Matrix obj) {\n *this = (*this - obj);\n return *this;\n }\n};\n\nclass modint {\npublic:\n using u64 = std::uint_fast64_t;\n u64 value = 0;\n u64 mod;\n modint(ll a, ll b) : value(((a% b) + 2 * b) % b), mod(b) {\n\n }\n modint operator+(const modint rhs) const {\n return modint(*this) += rhs;\n }\n modint operator-(const modint rhs) const {\n return modint(*this) -= rhs;\n }\n modint operator*(const modint rhs) const {\n return modint(*this) *= rhs;\n }\n modint operator/(const modint rhs) const {\n return modint(*this) /= rhs;\n }\n modint& operator+=(const modint rhs) {\n assert(rhs.mod == mod);\n value += rhs.value;\n if (value >= mod) {\n value -= mod;\n }\n return *this;\n }\n modint& operator-=(const modint rhs) {\n assert(rhs.mod == mod);\n if (value < rhs.value) {\n value += mod;\n }\n value -= rhs.value;\n return *this;\n }\n modint& operator*=(const modint rhs) {\n assert(rhs.mod == mod);\n value = (value * rhs.value) % mod;\n return *this;\n }\n modint& operator/=(modint rhs) {\n assert(rhs.mod == mod);\n ll rem = mod - 2;\n while (rem) {\n if (rem % 2) {\n *this *= rhs;\n }\n rhs *= rhs;\n rem /= 2LL;\n }\n return *this;\n }\n bool operator <(modint rhs) const {\n return value < rhs.value;\n }\n friend ostream& operator<<(ostream& os, modint& p) {\n os << p.value;\n return (os);\n }\n};\n\nclass Dice {\npublic:\n vector<ll> vertexs;\n //Up: 0,Left: 1,Center: 2,Right: 3,Adj: 4, Down: 5\n Dice(vector<ll> init) :vertexs(init) {\n\n }\n //Look from Center\n void RtoL() {\n for (int q = 1; q < 4; ++q) {\n swap(vertexs[q], vertexs[q + 1]);\n }\n }\n void LtoR() {\n for (int q = 3; q >= 1; --q) {\n swap(vertexs[q], vertexs[q + 1]);\n }\n }\n void UtoD() {\n swap(vertexs[5], vertexs[4]);\n swap(vertexs[2], vertexs[5]);\n swap(vertexs[0], vertexs[2]);\n }\n void DtoU() {\n swap(vertexs[0], vertexs[2]);\n swap(vertexs[2], vertexs[5]);\n swap(vertexs[5], vertexs[4]);\n }\n bool ReachAble(Dice now) {\n set<Dice> hoge;\n queue<Dice> next;\n next.push(now);\n hoge.insert(now);\n while (next.empty() == false) {\n Dice seeing = next.front();\n next.pop();\n if (seeing == *this) return true;\n seeing.RtoL();\n if (hoge.count(seeing) == 0) {\n hoge.insert(seeing);\n next.push(seeing);\n }\n seeing.LtoR();\n seeing.LtoR();\n if (hoge.count(seeing) == 0) {\n hoge.insert(seeing);\n next.push(seeing);\n }\n seeing.RtoL();\n seeing.UtoD();\n if (hoge.count(seeing) == 0) {\n hoge.insert(seeing);\n next.push(seeing);\n }\n seeing.DtoU();\n seeing.DtoU();\n if (hoge.count(seeing) == 0) {\n hoge.insert(seeing);\n next.push(seeing);\n }\n }\n return false;\n }\n bool operator ==(const Dice& a) {\n for (int q = 0; q < 6; ++q) {\n if (a.vertexs[q] != (*this).vertexs[q]) {\n return false;\n }\n }\n return true;\n }\n bool operator <(const Dice& a) const {\n return (*this).vertexs < a.vertexs;\n }\n};\n\npair<Dice, Dice> TwoDimDice(int center, int up) {\n int target = 1;\n while (true) {\n if (center != target && 7 - center != target && up != target && 7 - up != target) {\n break;\n }\n target++;\n }\n return mp(Dice(vector<ll>{up, target, center, 7 - target, 7 - center, 7 - up}), Dice(vector<ll>{up, 7 - target, center, target, 7 - center, 7 - up}));\n}\n\ntuple<Dice, Dice, Dice, Dice> OneDimDice(int center) {\n int bo = min(center, 7 - center);\n pair<int, int> goa;\n if (bo == 1) {\n goa = mp(2, 3);\n }\n else if (bo == 2) {\n goa = mp(1, 3);\n }\n else if (bo == 3) {\n goa = mp(1, 2);\n }\n tuple<Dice, Dice, Dice, Dice> now = make_tuple(Dice(vector<ll>{goa.first, goa.second, center, 7 - goa.second, 7 - center, 7 - goa.first}),\n Dice(vector<ll>{goa.first, 7 - goa.second, center, goa.second, 7 - center, 7 - goa.first}),\n Dice(vector<ll>{7 - goa.first, goa.second, center, 7 - goa.second, 7 - center, goa.first}),\n Dice(vector<ll>{7 - goa.first, 7 - goa.second, center, goa.second, 7 - center, goa.first}));\n return now;\n}\n\ntemplate<typename A, typename B>\nclass Dijkstra {\npublic:\n vector<vector<pair<int, A>>> vertexs;\n B Cost_Function;\n Dijkstra(int n, B cost) : Cost_Function(cost) {\n vertexs = vector<vector<pair<int, A>>>(n, vector<pair<int, A>>{});\n }\n ~Dijkstra() {\n vertexs.clear();\n }\n void add_edge(int a, int b, A c) {\n vertexs[a].push_back(mp(b, c));\n }\n vector<ll> build_result(int StartPoint) {\n vector<ll> dist(vertexs.size(), 2e18);\n dist[StartPoint] = 0;\n priority_queue<pair<ll, int>, vector<pair<ll, int>>, greater<pair<ll, int>>> next;\n next.push(make_pair(0, StartPoint));\n while (next.empty() == false) {\n pair<ll, int> now = next.top();\n next.pop();\n if (dist[now.second] != now.first) continue;\n for (auto x : vertexs[now.second]) {\n ll now_cost = now.first + Cost_Function(x.second);\n if (dist[x.first] > now_cost) {\n dist[x.first] = now_cost;\n next.push(mp(now_cost, x.first));\n }\n }\n }\n return dist;\n }\n};\n\nclass Dinic {\npublic:\n struct edge {\n int to;\n int cap;\n int rev;\n };\n vector<vector<edge>> Graph;\n vector<int> level;\n vector<int> itr;\n Dinic(int n) {\n Graph = vector<vector<edge>>(n, vector<edge>());\n }\n void add_edge(int a, int b, int cap) {\n Graph[a].push_back(edge{ b, cap ,(int)Graph[b].size() });\n Graph[b].push_back(edge{ a,0,(int)Graph[a].size() - 1 });\n }\n void bfs(int s) {\n level = vector<int>(Graph.size(), -1);\n level[s] = 0;\n queue<int> next;\n next.push(s);\n while (next.empty() == false) {\n int now = next.front();\n next.pop();\n for (auto x : Graph[now]) {\n if (x.cap == 0) continue;\n if (level[x.to] == -1) {\n level[x.to] = level[now] + 1;\n next.push(x.to);\n }\n }\n }\n }\n int dfs(int now, int goal, int val) {\n if (goal == now) return val;\n for (int& i = itr[now]; i < (int)Graph[now].size(); ++i) {\n edge& target = Graph[now][i];\n if (target.cap > 0 && level[now] < level[target.to]) {\n int d = dfs(target.to, goal, min(val, target.cap));\n if (d > 0) {\n target.cap -= d;\n Graph[target.to][target.rev].cap += d;\n return d;\n }\n }\n }\n return 0;\n }\n int run(int s, int t) {\n int ans = 0;\n int f = 0;\n while (bfs(s), level[t] >= 0) {\n itr = vector<int>(Graph.size(), 0);\n while ((f = dfs(s, t, 1e9)) > 0) {\n ans += f;\n }\n }\n return ans;\n }\n};\n\n//by ei1333\n//https://ei1333.github.io/luzhiled/snippets/structure/segment-tree.html\ntemplate< typename Monoid >\nstruct SegmentTree {\n using F = function< Monoid(Monoid, Monoid) >;\n\n int sz;\n vector< Monoid > seg;\n\n const F f;\n const Monoid M1;\n\n SegmentTree(int n, const F f, const Monoid& M1) : f(f), M1(M1) {\n sz = 1;\n while (sz < n) sz <<= 1;\n seg.assign(2 * sz + 1, M1);\n }\n\n void set(int k, const Monoid& x) {\n seg[k + sz] = x;\n }\n\n void build() {\n for (int k = sz - 1; k > 0; k--) {\n seg[k] = f(seg[2 * k + 0], seg[2 * k + 1]);\n }\n }\n\n void update(int k, const Monoid& x) {\n k += sz;\n seg[k] = x;\n while (k >>= 1) {\n seg[k] = f(seg[2 * k + 0], seg[2 * k + 1]);\n }\n }\n\n Monoid query(int a, int b) {\n Monoid L = M1, R = M1;\n for (a += sz, b += sz; a < b; a >>= 1, b >>= 1) {\n if (a & 1) L = f(L, seg[a++]);\n if (b & 1) R = f(seg[--b], R);\n }\n return f(L, R);\n }\n\n Monoid operator[](const int& k) const {\n return seg[k + sz];\n }\n\n template< typename C >\n int find_subtree(int a, const C& check, Monoid& M, bool type) {\n while (a < sz) {\n Monoid nxt = type ? f(seg[2 * a + type], M) : f(M, seg[2 * a + type]);\n if (check(nxt)) a = 2 * a + type;\n else M = nxt, a = 2 * a + 1 - type;\n }\n return a - sz;\n }\n\n\n template< typename C >\n int find_first(int a, const C& check) {\n Monoid L = M1;\n if (a <= 0) {\n if (check(f(L, seg[1]))) return find_subtree(1, check, L, false);\n return -1;\n }\n int b = sz;\n for (a += sz, b += sz; a < b; a >>= 1, b >>= 1) {\n if (a & 1) {\n Monoid nxt = f(L, seg[a]);\n if (check(nxt)) return find_subtree(a, check, L, false);\n L = nxt;\n ++a;\n }\n }\n return -1;\n }\n\n template< typename C >\n int find_last(int b, const C& check) {\n Monoid R = M1;\n if (b >= sz) {\n if (check(f(seg[1], R))) return find_subtree(1, check, R, true);\n return -1;\n }\n int a = sz;\n for (b += sz; a < b; a >>= 1, b >>= 1) {\n if (b & 1) {\n Monoid nxt = f(seg[--b], R);\n if (check(nxt)) return find_subtree(b, check, R, true);\n R = nxt;\n }\n }\n return -1;\n }\n};\n\nunsigned long xor128() {\n static unsigned long x = 123456789, y = 362436069, z = 521288629, w = 88675123;\n unsigned long t = (x ^ (x << 11));\n x = y; y = z; z = w;\n return (w = (w ^ (w >> 19)) ^ (t ^ (t >> 8)));\n}\n\nvoid init() {\n iostream::sync_with_stdio(false);\n cout << fixed << setprecision(20);\n}\n\n#define int long long\n\nvoid solve() {\n while (true) {\n int n;\n cin >> n;\n if (n == 0)return;\n int cnt = 0;\n REP(i, n) {\n string s;\n cin >> s;\n REP(q, n) {\n if (s[q] == '#') {\n cnt += (1 << (i * n + q + 5));\n }\n else if (s[q] == '@') {\n cnt += i * n + q;\n }\n }\n }\n set<int> gogo;\n gogo.insert(cnt);\n queue<pair<int, int>> next;\n next.push(mp(cnt, 0));\n int ans = -1;\n while (next.empty() == false) {\n pair<int, int> now = next.front();\n next.pop();\n int grid[5][5] = {};\n int cnt = 0;\n REP(i, n) {\n REP(q, n) {\n if ((1LL << (i * n + q + 5)) & now.first) {\n grid[i][q] = 1;\n cnt++;\n }\n }\n }\n if (cnt == 0) {\n ans = now.second;\n break;\n }\n const int dx[8] = { 1,1,1,0,0,-1,-1,-1 };\n const int dy[8] = { 1,0,-1,1,-1,1,0,-1 };\n pair<int, int> place = mp((now.first % (1 << 5)) / n, (now.first % (1 << 5)) % n);\n REP(q, 8) {\n int x = place.first + dx[q];\n int y = place.second + dy[q];\n if (x >= 0 && x < n && y >= 0 && y < n&&grid[x][y] == 0) {\n grid[x][y] = 1;\n int tmp = x * n + y;\n REP(j, n) {\n REP(t, n) {\n if (j == x && t == y) continue;\n int cnter = 0;\n REP(p, 8) {\n int next_x = j + dx[p];\n int next_y = t + dy[p];\n if (next_x >= 0 && next_x < n && next_y >= 0 && next_y < n) {\n cnter += grid[next_x][next_y];\n }\n }\n int doing = 0;\n if (grid[j][t] == 1) {\n if (cnter >= 2 && cnter <= 3) {\n doing = 1;\n }\n }\n else {\n if (cnter == 3) {\n doing = 1;\n }\n }\n tmp += doing * (1LL << (5 + j * n + t));\n }\n }\n if (gogo.count(tmp) == 0) {\n gogo.insert(tmp);\n next.push(mp(tmp, now.second + 1));\n }\n grid[x][y] = 0;\n }\n }\n }\n cout << ans << endl;\n }\n}\n#undef int\nint main() {\n init();\n solve();\n}", "accuracy": 1, "time_ms": 750, "memory_kb": 4540, "score_of_the_acc": -0.2906, "final_rank": 11 }, { "submission_id": "aoj_1304_4175913", "code_snippet": "#include<iostream>\n#include<queue>\n#include<map>\nusing namespace std;\nint N;\nint dx[8]={0,1,0,-1,1,1,-1,-1};\nint dy[8]={1,0,-1,0,1,-1,1,-1};\nmain()\n{\n\twhile(cin>>N,N)\n\t{\n\t\tstring start=\"\";\n\t\tfor(int i=0;i<N;i++)\n\t\t{\n\t\t\tstring input;cin>>input;start+=input;\n\t\t}\n\t\tmap<string,int>M;\n\t\tM[start]=0;\n\t\tqueue<string>P;\n\t\tP.push(start);\n\t\tint ans=-1;\n\t\twhile(!P.empty())\n\t\t{\n\t\t\tstring now=P.front();P.pop();\n\t\t\tint cost=M[now];\n\t\t\tint sx,sy;\n\t\t\tint cnt=0;\n\t\t\tfor(int i=0;i<N*N;i++)\n\t\t\t{\n\t\t\t\tif(now[i]=='@')sx=i/N,sy=i%N;\n\t\t\t\tif(now[i]=='#')cnt++;\n\t\t\t}\n\t\t\tif(cnt==0)\n\t\t\t{\n\t\t\t\tans=cost;\n\t\t\t\tbreak;\n\t\t\t}\n\t\t\tfor(int r=0;r<8;r++)\n\t\t\t{\n\t\t\t\tint tx=sx+dx[r],ty=sy+dy[r];\n\t\t\t\tif(tx<0||ty<0||tx>=N||ty>=N||now[tx*N+ty]=='#')continue;\n\t\t\t\tstring tmp=now;\n\t\t\t\ttmp[sx*N+sy]='.';\n\t\t\t\ttmp[tx*N+ty]='@';\n\t\t\t\tstring nxt=tmp;\n\t\t\t\tfor(int x=0;x<N;x++)for(int y=0;y<N;y++)\n\t\t\t\t{\n\t\t\t\t\tif(nxt[x*N+y]=='@')continue;\n\t\t\t\t\tint cnt=0;\n\t\t\t\t\tfor(int r=0;r<8;r++)\n\t\t\t\t\t{\n\t\t\t\t\t\tint xx=x+dx[r],yy=y+dy[r];\n\t\t\t\t\t\tif(0<=xx&&xx<N&&0<=yy&&yy<N&&tmp[xx*N+yy]!='.')cnt++;\n\t\t\t\t\t}\n\t\t\t\t\tif(tmp[x*N+y]=='#'?2<=cnt&&cnt<=3:cnt==3)nxt[x*N+y]='#';\n\t\t\t\t\telse nxt[x*N+y]='.';\n\t\t\t\t}\n\t\t\t\tif(M.find(nxt)==M.end())\n\t\t\t\t{\n\t\t\t\t\tM[nxt]=cost+1;\n\t\t\t\t\tP.push(nxt);\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\tcout<<ans<<endl;\n\t}\n}", "accuracy": 1, "time_ms": 1590, "memory_kb": 7184, "score_of_the_acc": -0.7358, "final_rank": 15 }, { "submission_id": "aoj_1304_3989183", "code_snippet": "#include <vector>\n#include <algorithm>\n#include <map>\n#include <string>\n#include <queue>\n#include <iostream>\nusing namespace std;\n\nconst int dx[] = {-1, -1, -1, 0, 0, 1, 1, 1};\nconst int dy[] = {-1, 0, 1, -1, 1, -1, 0, 1};\n\nconst int INF = 1 << 29;\n\nvector<string> get_next_board(const vector<string> &board) {\n int N = board.size();\n vector<string> res(N, string(N, '.'));\n\n // vector<string> sample = {\".....\", \"##.@.\", \"#....\", \"...#.\", \"##.##\"};\n \n for(int i=0; i<N; i++) {\n for(int j=0; j<N; j++) {\n int cnt = 0; \n for(int k=0; k<8; k++) {\n int ni = i + dx[k], nj = j + dy[k];\n if(ni < 0 or ni >= N or nj < 0 or nj >= N) continue;\n cnt += (board[ni][nj] == '#' or board[ni][nj] == '@');\n }\n\n /*\n if(sample == board) {\n fprintf(stderr, \"i = %d, j = %d, cnt = %d\\n\", i, j, cnt);\n }\n */\n\n if(board[i][j] == '#') {\n if(cnt == 2 or cnt == 3) res[i][j] = '#';\n else res[i][j] = '.';\n }\n else if(board[i][j] == '@') {\n res[i][j] = '@';\n }\n else {\n if(cnt == 3) res[i][j] = '#';\n else res[i][j] = '.';\n }\n }\n }\n\n /*\n if(sample == board) {\n for(int i=0; i<N; i++) {\n cerr << \"# \" << res[i] << endl;\n }\n for(int i=0; i<N; i++) {\n cerr << \"? \" << board[i] << endl;\n }\n }\n */\n return res;\n}\n\nint bfs(const vector<string> &board, int ax, int ay) {\n map< vector<string>, int > state;\n int N = board.size();\n state[board] = 0;\n\n queue< tuple< vector<string>, int, int> > que;\n que.emplace(board, ax, ay);\n\n int res = INF;\n while(que.size()) {\n vector<string> B; int x, y;\n tie(B, x, y) = que.front(); que.pop();\n int cost = state[B];\n \n int cnt = 0;\n for(int i=0; i<N; i++) {\n for(int j=0; j<N; j++) {\n if(B[i][j] == '#') {\n cnt = 1;\n break;\n }\n }\n }\n if(cnt == 0) {\n res = cost;\n break;\n }\n \n for(int k=0; k<8; k++) {\n int nx = x + dx[k], ny = y + dy[k];\n if(nx < 0 or nx >= N or ny < 0 or ny >= N) continue;\n if(B[nx][ny] == '#') continue;\n swap(B[x][y], B[nx][ny]);\n vector<string> NB = get_next_board(B);\n if(!state.count(NB)) {\n state[NB] = cost + 1;\n que.emplace(NB, nx, ny);\n }\n swap(B[x][y], B[nx][ny]);\n }\n }\n return res;\n}\n\nint solve_testcase() {\n int N; cin >> N;\n if(N == 0) return 1;\n\n vector<string> board(N);\n int ax = -1, ay = -1;\n for(int i=0; i<N; i++) {\n cin >> board[i];\n for(int j=0; j<N; j++) {\n if(board[i][j] == '@') {\n ax = i, ay = j;\n }\n }\n }\n\n int ans = bfs(board, ax, ay);\n cout << (ans == INF ? -1 : ans) << endl;\n return 0;\n}\n\nint main() {\n while(!solve_testcase());\n return 0;\n}", "accuracy": 1, "time_ms": 2880, "memory_kb": 13584, "score_of_the_acc": -1.6378, "final_rank": 18 }, { "submission_id": "aoj_1304_3963563", "code_snippet": "#include <vector>\n#include <algorithm>\n#include <map>\n#include <string>\n#include <queue>\n#include <iostream>\nusing namespace std;\n\nconst int dx[] = {-1, -1, -1, 0, 0, 1, 1, 1};\nconst int dy[] = {-1, 0, 1, -1, 1, -1, 0, 1};\n\nconst int INF = 1 << 29;\n\nvector<string> get_next_board(const vector<string> &board) {\n int N = board.size();\n vector<string> res(N, string(N, '.'));\n\n // vector<string> sample = {\".....\", \"##.@.\", \"#....\", \"...#.\", \"##.##\"};\n \n for(int i=0; i<N; i++) {\n for(int j=0; j<N; j++) {\n int cnt = 0; \n for(int k=0; k<8; k++) {\n int ni = i + dx[k], nj = j + dy[k];\n if(ni < 0 or ni >= N or nj < 0 or nj >= N) continue;\n cnt += (board[ni][nj] == '#' or board[ni][nj] == '@');\n }\n\n /*\n if(sample == board) {\n fprintf(stderr, \"i = %d, j = %d, cnt = %d\\n\", i, j, cnt);\n }\n */\n\n if(board[i][j] == '#') {\n if(cnt == 2 or cnt == 3) res[i][j] = '#';\n else res[i][j] = '.';\n }\n else if(board[i][j] == '@') {\n res[i][j] = '@';\n }\n else {\n if(cnt == 3) res[i][j] = '#';\n else res[i][j] = '.';\n }\n }\n }\n\n /*\n if(sample == board) {\n for(int i=0; i<N; i++) {\n cerr << \"# \" << res[i] << endl;\n }\n for(int i=0; i<N; i++) {\n cerr << \"? \" << board[i] << endl;\n }\n }\n */\n return res;\n}\n\nint bfs(const vector<string> &board, int ax, int ay) {\n map< vector<string>, int > state;\n int N = board.size();\n state[board] = 0;\n\n queue< tuple< vector<string>, int, int> > que;\n que.emplace(board, ax, ay);\n\n int res = INF;\n while(que.size()) {\n vector<string> B; int x, y;\n tie(B, x, y) = que.front(); que.pop();\n int cost = state[B];\n \n int cnt = 0;\n for(int i=0; i<N; i++) {\n for(int j=0; j<N; j++) {\n if(B[i][j] == '#') {\n cnt = 1;\n break;\n }\n }\n }\n if(cnt == 0) {\n res = cost;\n break;\n }\n \n for(int k=0; k<8; k++) {\n int nx = x + dx[k], ny = y + dy[k];\n if(nx < 0 or nx >= N or ny < 0 or ny >= N) continue;\n if(B[nx][ny] == '#') continue;\n swap(B[x][y], B[nx][ny]);\n vector<string> NB = get_next_board(B);\n if(!state.count(NB)) {\n state[NB] = cost + 1;\n que.emplace(NB, nx, ny);\n }\n swap(B[x][y], B[nx][ny]);\n }\n }\n return res;\n}\n\nint solve_testcase() {\n int N; cin >> N;\n if(N == 0) return 1;\n\n vector<string> board(N);\n int ax = -1, ay = -1;\n for(int i=0; i<N; i++) {\n cin >> board[i];\n for(int j=0; j<N; j++) {\n if(board[i][j] == '@') {\n ax = i, ay = j;\n }\n }\n }\n\n int ans = bfs(board, ax, ay);\n cout << (ans == INF ? -1 : ans) << endl;\n return 0;\n}\n\nint main() {\n while(!solve_testcase());\n return 0;\n}", "accuracy": 1, "time_ms": 2890, "memory_kb": 13640, "score_of_the_acc": -1.6454, "final_rank": 19 }, { "submission_id": "aoj_1304_3924075", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\nusing Int = long long;\ntemplate<typename T1,typename T2> inline void chmin(T1 &a,T2 b){if(a>b) a=b;}\ntemplate<typename T1,typename T2> inline void chmax(T1 &a,T2 b){if(a<b) a=b;}\n\n//INSERT ABOVE HERE\n\nint dy[]={-1,-1,-1,0,1,1,1,0};\nint dx[]={-1,0,1,1,1,0,-1,-1};\nstruct State{\n vector<string> st;\n State(int n):st(n){}\n\n int in(int y,int x) const{\n return 0<=y&&y<(int)st.size()&&0<=x&&x<(int)st[0].size();\n }\n\n State next() const{\n State res(*this);\n\n int n=st.size();\n for(int i=0;i<n;i++){\n for(int j=0;j<n;j++){\n if(st[i][j]=='@') continue;\n\n int cnt=0;\n for(int k=0;k<8;k++){\n int ny=i+dy[k],nx=j+dx[k];\n if(!in(ny,nx)) continue;\n if(st[ny][nx]!='.') cnt++;\n }\n\n res.st[i][j]='.';\n if(st[i][j]=='#'){\n if(cnt==2) res.st[i][j]='#';\n if(cnt==3) res.st[i][j]='#';\n }\n if(st[i][j]=='.'){\n if(cnt==3) res.st[i][j]='#';\n }\n }\n }\n return res;\n }\n\n vector<State> move() const{\n int n=st.size();\n int y=-1,x=-1;\n for(int i=0;i<n;i++)\n for(int j=0;j<n;j++)\n if(st[i][j]=='@') y=i,x=j;\n\n vector<State> adj;\n for(int k=0;k<8;k++){\n int ny=y+dy[k],nx=x+dx[k];\n if(!in(ny,nx)) continue;\n if(st[ny][nx]=='#') continue;\n State res(*this);\n swap(res.st[y][x],res.st[ny][nx]);\n adj.emplace_back(res.next());\n }\n return adj;\n }\n};\n\nsigned main(){\n int n;\n while(cin>>n,n){\n State init(n);\n for(int i=0;i<n;i++) cin>>init.st[i];\n\n map<vector<string>, int> dist;\n queue<State> que;\n dist[init.st]=0;\n que.emplace(init);\n\n int ans=-1;\n while(!que.empty()){\n auto v=que.front();que.pop();\n\n int cnt=0;\n for(int i=0;i<n;i++)\n for(int j=0;j<n;j++)\n if(v.st[i][j]=='#') cnt++;\n if(cnt==0){\n ans=dist[v.st];\n break;\n }\n\n auto adj=v.move();\n for(auto u:adj){\n if(dist.count(u.st)) continue;\n dist[u.st]=dist[v.st]+1;\n que.emplace(u);\n }\n }\n cout<<ans<<endl;\n }\n return 0;\n}", "accuracy": 1, "time_ms": 3330, "memory_kb": 10792, "score_of_the_acc": -1.4837, "final_rank": 17 }, { "submission_id": "aoj_1304_3593305", "code_snippet": "#include<iostream>\n#include<string>\n#include<cstdio>\n#include<vector>\n#include<cmath>\n#include<algorithm>\n#include<functional>\n#include<iomanip>\n#include<queue>\n#include<ciso646>\n#include<random>\n#include<map>\n#include<set>\n#include<complex>\n#include<bitset>\n#include<stack>\n#include<unordered_map>\n#include<utility>\nusing namespace std;\ntypedef long long ll;\ntypedef unsigned long long ul;\ntypedef unsigned int ui;\nconst ll mod = 1000000007;\ntypedef double ld;\ntypedef complex<ld> Point;\nconst ll INF = mod * mod;\ntypedef pair<int, int> P;\n#define stop char nyaa;cin>>nyaa;\n#define rep(i,n) for(int i=0;i<n;i++)\n#define per(i,n) for(int i=n-1;i>=0;i--)\n#define Rep(i,sta,n) for(int i=sta;i<n;i++)\n#define rep1(i,n) for(int i=1;i<=n;i++)\n#define per1(i,n) for(int i=n;i>=1;i--)\n#define Rep1(i,sta,n) for(int i=sta;i<=n;i++)\nconst ld eps = 1e-6;\nconst ld pi = acos(-1.0);\ntypedef pair<ld, ld> LDP;\ntypedef pair<ll, ll> LP;\n\nint dx[8] = { 1,1,1,0,-1,-1,-1,0 };\nint dy[8] = { 1,0,-1,-1,-1,0,1,1 };\n\nint n;\nmap<P, bool> used;\n//map<P, int> memo;\nchar mp[5][5];\n\nvector<P> nexc(int loc, int s) {\n\t//cout << loc << \" \" << s << endl;\n\t//if (s == 0)return 0;\n\tvector<P> ret;\n\t//if (used[{loc, s}])return memo[{loc, s}];\n\t//used[{loc, s}] = true;\n\tvector<vector<bool>> g(n);\n\trep(i, n) {\n\t\tg[i].resize(n);\n\t\trep(j, n) {\n\t\t\tint z = i * n + j;\n\t\t\tif (s&(1 << z))g[i][j] = true;\n\t\t\telse g[i][j] = false;\n\t\t}\n\t}\n\tint x = loc / n, y = loc % n;\n\trep(k, 8) {\n\t\tint nx = x + dx[k], ny = y + dy[k];\n\t\tif (nx < 0 || ny < 0 || nx >= n || ny >= n)continue;\n\t\tif (g[nx][ny])continue;\n\t\tint nloc = nx * n + ny;\n\t\tint nex = 0;\n\t\trep(i, n) {\n\t\t\trep(j, n) {\n\t\t\t\tint z = i * n + j;\n\t\t\t\tif (z == nloc)continue;\n\t\t\t\t\n\t\t\t\tint cnt = 0;\n\t\t\t\trep(l, 8) {\n\t\t\t\t\tint sx = i + dx[l], sy = j + dy[l];\n\t\t\t\t\tif (sx < 0 || sy < 0 || sx >= n || sy >= n)continue;\n\t\t\t\t\tif (g[sx][sy]||sx*n+sy==nloc)cnt++;\n\t\t\t\t}\n\t\t\t\tif (g[i][j]) {\n\t\t\t\t\tif (cnt == 3 || cnt == 2)nex += (1 << z);\n\t\t\t\t}\n\t\t\t\telse {\n\t\t\t\t\tif (cnt == 3)nex += (1 << z);\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\tif (!used[{nloc, nex}]) {\n\t\t\tused[{nloc, nex}] = true;\n\t\t\tret.push_back({ nloc,nex });\n\t\t}\n\t}\n\treturn ret;\n}\n\nvoid solve(){\n\tused.clear(); \n\t//memo.clear();\n\tint s = 0;\n\tint x = 0;\n\trep(i, n) {\n\t\trep(j, n) {\n\t\t\tcin >> mp[i][j];\n\t\t\tint z = i * n + j;\n\t\t\tif (mp[i][j] == '#') {\n\t\t\t\ts += (1 << z);\n\t\t\t}\n\t\t\telse if (mp[i][j] == '@') {\n\t\t\t\tx = z;\n\t\t\t}\n\t\t}\n\t}\n\tqueue<P> q; q.push({ x,s }); used[{x, s}] = true;\n\tint tmp = 0;\n\twhile (!q.empty()) {\n\t\tint len = q.size();\n\t\trep(aa, len) {\n\t\t\tP p = q.front(); q.pop();\n\t\t\tx = p.first, s = p.second;\n\t\t\tif (s == 0) {\n\t\t\t\tcout << tmp << endl; return;\n\t\t\t}\n\t\t\tvector<P> c = nexc(x, s);\n\t\t\trep(i, c.size())q.push(c[i]);\n\t\t}\n\t\ttmp++;\n\t}\n\tcout << -1 << endl;\n}\n\nint main() {\n\tios::sync_with_stdio(false);\n\tcin.tie(0);\n\twhile (cin >> n, n) {\n\t\tsolve();\n\t}\n\t//stop\n\t\treturn 0;\n}", "accuracy": 1, "time_ms": 1430, "memory_kb": 4292, "score_of_the_acc": -0.4282, "final_rank": 13 } ]
aoj_1309_cpp
Problem E: The Two Men of the Japanese Alps Two experienced climbers are planning a first-ever attempt: they start at two points of the equal altitudes on a mountain range, move back and forth on a single route keeping their altitudes equal, and finally meet with each other at a point on the route. A wise man told them that if a route has no point lower than the start points (of the equal altitudes) there is at least a way to achieve the attempt. This is the reason why the two climbers dare to start planning this fancy attempt. The two climbers already obtained altimeters (devices that indicate altitude) and communication devices that are needed for keeping their altitudes equal. They also picked up a candidate route for the attempt: the route consists of consequent line segments without branches; the two starting points are at the two ends of the route; there is no point lower than the two starting points of the equal altitudes. An illustration of the route is given in Figure E.1 (this figure corresponds to the first dataset of the sample input). Figure E.1: An illustration of a route The attempt should be possible for the route as the wise man said. The two climbers, however, could not find a pair of move sequences to achieve the attempt, because they cannot keep their altitudes equal without a complex combination of both forward and backward moves. For example, for the route illustrated above: a climber starting at p 1 (say A ) moves to s , and the other climber (say B ) moves from p 6 to p 5 ; then A moves back to t while B moves to p 4 ; finally A arrives at p 3 and at the same time B also arrives at p 3 . Things can be much more complicated and thus they asked you to write a program to find a pair of move sequences for them. There may exist more than one possible pair of move sequences, and thus you are requested to find the pair of move sequences with the shortest length sum. Here, we measure the length along the route surface, i.e., an uphill path from (0, 0) to (3, 4) has the length of 5. Input The input is a sequence of datasets. The first line of each dataset has an integer indicating the number of points N (2 ≤ N ≤ 100) on the route. Each of the following N lines has the coordinates ( x i , y i ) ( i = 1, 2, ... , N ) of the points: the two start points are ( x 1 , y 1 ) and ( x N , y N ); the line segments of the route connect ( x i , y i ) and ( x i +1 , y i +1 ) for i = 1, 2, ... , N - 1. Here, x i is the horizontal distance along the route from the start point x 1 , and y i is the altitude relative to the start point y 1 . All the coordinates are non-negative integers smaller than 1000, and inequality x i < x i +1 holds for i = 1, 2, .. , N - 1, and 0 = y 1 = y N ≤ y i for i = 2, 3, ... , N - 1. The end of the input is indicated by a line containing a zero. Output For each dataset, output the minimum sum of lengths (along the route) of move sequences until the two climbers meet with each other at a point on the route. Each output ...(truncated)
[ { "submission_id": "aoj_1309_10852715", "code_snippet": "#define PII pair<int,int>\n#define PDD pair<double,double>\n#include <bits/stdc++.h>\n\nusing namespace std;\n\nconst int N=10240;\nconst double EPS=1E-7;\n\nvector<PDD> v;\nint n,cnt,x[N],y[N],f[N],h[N],nextbase[N],prvbase[N],nxt[102][N],prv[102][N];\n\nqueue<PII> q;\nbool vis[102][N];\ndouble dp[102][N];\n\ndouble Dis(int x,int y)\n{\n\treturn sqrt(pow(v[x].first-v[y].first,2)+pow(v[x].second-v[y].second,2));\n}\n\nint dcmp(double x,double y)\n{\n\treturn fabs(x-y)<EPS?0:(x>y?1:-1);\n}\n\nbool Inside(int x,int l,int r)\n{\n\tif(l>r) swap(l,r);\n\treturn l<x && x<r;\n}\n\nvoid Move(int x,int y,int drx,int dry,double d)\n{\n\tint nx,ny;\n\tif(drx)\n\t\tnx=nextbase[x];\n\telse\n\t\tnx=prvbase[x];\n\tif(nx==-1) return;\n\n\tif(dry)\n\t\tny=nxt[nx][y];\n\telse\n\t\tny=prv[nx][y];\n\n\tif(ny==-1) return;\n\n\tassert(!dcmp(v[f[nx]].second,v[ny].second));\n\n\td+=Dis(x,f[nx])+Dis(y,ny);\n\n\tif(dcmp(dp[nx][ny],d)==1)\n\t{\n\t\tdp[nx][ny]=d;\n\t\tif(!vis[nx][ny])\n\t\t\tvis[nx][ny]=1,q.push(PII(nx,ny));\n\t}\n}\n\ndouble SPFA()\n{\n\tmemset(vis,0,sizeof(vis));\n\tfor(int i=1;i<=n;i++)\n\t\tfor(int j=0;j<cnt;j++)\n\t\t\tdp[i][j]=1E14;\n\n\tdp[1][cnt-1]=0,vis[1][cnt-1]=1;\n\tfor(q.push(PII(1,cnt-1));!q.empty();q.pop())\n\t{\n\t\tPII u=q.front();\n\t\tint x=u.first,y=u.second; \n\t\tdouble d=dp[x][y]; x=f[x];\n\n\t\tif(x==y)\n\t\t{\n\t\t\tvis[u.first][y]=0;\n\t\t\tcontinue;\n\t\t}\n\t\tassert(!dcmp(v[x].second,v[y].second));\n\n\t\tfor(int i=0;i<2;i++)\n\t\t\tfor(int j=0;j<2;j++)\n\t\t\t\tMove(x,y,i,j,d),Move(y,x,i,j,d);\n\t\tvis[u.first][y]=0;\n\t}\n\n\tdouble res=1E14;\n\tfor(int i=1;i<=n;i++)\n\t{\n\t\tint u=f[i];\n\t\tres=min(res,dp[i][u]);\n\t}\n\treturn res;\n}\n\nint main()\n{\n\tfor(;cin>>n,n;)\n\t{\n\t\tv.clear();\n\t\tmemset(f,0,sizeof(f));\n\t\tmemset(h,0,sizeof(h));\n\t\tfor(int i=1;i<=n;i++)\n\t\t\tscanf(\"%d%d\",x+i,y+i);\n\n\t\tfor(int i=1;i<n;i++)\n\t\t{\n\t\t\tbool flag=1;\n\t\t\tfor(int j=1;j<i;j++)\n\t\t\t\tif(y[i]==y[j])\n\t\t\t\t{\n\t\t\t\t\tflag=0;\n\t\t\t\t\tbreak;\n\t\t\t\t}\n\t\t\tif(!flag)\n\t\t\t\tcontinue;\n\n\t\t\tfor(int j=1;j<n;j++)\n\t\t\t\tif(Inside(y[i],y[j],y[j+1]))\n\t\t\t\t\tv.push_back(PDD(double(x[j+1]-x[j])/(y[j+1]-y[j])*(y[i]-y[j])+x[j],y[i]));\n\t\t}\n\t\tfor(int i=1;i<=n;i++)\n\t\t\tv.push_back(PDD(x[i],y[i]));\n\t\tsort(v.begin(),v.end()),cnt=v.size();\n\n/*\n\t\tfor(int i=0;i<cnt;i++)\n\t\t\tcout<<v[i].first<<\" \"<<v[i].second<<endl;\n*/\n\n\t\tfor(int i=1;i<=n;i++)\n\t\t\tfor(int j=0;j<cnt;j++)\n\t\t\t\tif(!dcmp(v[j].first,x[i]))\n\t\t\t\t{\n\t\t\t\t\tf[i]=j,h[j]=i;\n\t\t\t\t\tbreak;\n\t\t\t\t}\n\n\t\tfor(int i=0,k=-1;i<cnt;i++)\n\t\t{\n\t\t\tprvbase[i]=k;\n\t\t\tif(h[i]) k=h[i];\n\t\t}\n\n\t\tfor(int i=cnt-1,k=-1;i>=0;i--)\n\t\t{\n\t\t\tnextbase[i]=k;\n\t\t\tif(h[i]) k=h[i];\n\t\t}\n\n\t\t/*for(int i=0;i<cnt;i++)\n\t\t\tcout<<i<<\" \"<<nextbase[i]<<endl;*/\n\n\t\tfor(int i=1;i<=n;i++)\n\t\t\tfor(int j=0,k=-1;j<cnt;j++)\n\t\t\t{\n\t\t\t\tif(!dcmp(v[j].second,y[i]))\n\t\t\t\t\tk=j;\n\t\t\t\tprv[i][j]=k;\n\n\t\t\t\tif(dcmp(v[j].second,y[i]) && h[j])\n\t\t\t\t\tk=-1;\n\t\t\t}\n\n\t\tfor(int i=n;i>=1;i--)\n\t\t\tfor(int j=cnt-1,k=-1;j>=0;j--)\n\t\t\t{\n\t\t\t\tif(!dcmp(v[j].second,y[i]))\n\t\t\t\t\tk=j;\n\t\t\t\tnxt[i][j]=k;\n\n\t\t\t\tif(dcmp(v[j].second,y[i]) && h[j])\n\t\t\t\t\tk=-1;\n\t\t\t}\n\n\t\tprintf(\"%.9f\\n\",SPFA());\n\t}\n}", "accuracy": 1, "time_ms": 50, "memory_kb": 19596, "score_of_the_acc": -0.1344, "final_rank": 11 }, { "submission_id": "aoj_1309_9812927", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n#define rep(i, n) for(int i = 0; i < (n); i++)\n#define srep(i, s, t) for(int i = (s); i < (t); i++)\n#define len(x) ((int)(x).size())\n#define all(x) (x).begin(), (x).end()\ntemplate < class T > bool chmin(T& a, T b) { if(a > b) { a = b; return true; } return false; }\ntemplate < class T > bool chmax(T& a, T b) { if(a < b) { a = b; return true; } return false; }\nusing i64 = long long;\nusing f64 = double;\ni64 floor_div(const i64 n, const i64 d) { assert(d != 0); return n / d - static_cast<i64>((n ^ d) < 0 && n % d != 0); }\ni64 ceil_div(const i64 n, const i64 d) { assert(d != 0); return n / d + static_cast<i64>((n ^ d) >= 0 && n % d != 0); }\n\nf64 solve(int n) {\n vector<f64> ax(n), ay(n);\n rep(i, n) cin >> ax[i] >> ay[i];\n\n vector<f64> vy = ay;\n sort(vy.begin(), vy.end());\n vy.erase(unique(vy.begin(), vy.end()), vy.end());\n const int m = vy.size();\n\n vector<f64> x, y;\n rep(i, n) {\n x.push_back(ax[i]);\n y.push_back(ay[i]);\n if(i + 1 < n) {\n if(ay[i] < ay[i + 1]) {\n rep(k, m) if(ay[i] < vy[k] and vy[k] < ay[i + 1]) {\n x.push_back(ax[i] + (ax[i + 1] - ax[i]) * (vy[k] - ay[i]) / (ay[i + 1] - ay[i]));\n y.push_back(vy[k]);\n }\n } else {\n for(int k = m - 1; k >= 0; k--) if(ay[i + 1] < vy[k] and vy[k] < ay[i]) {\n x.push_back(ax[i] + (ax[i + 1] - ax[i]) * (vy[k] - ay[i]) / (ay[i + 1] - ay[i]));\n y.push_back(vy[k]);\n }\n }\n }\n }\n\n const int sz = x.size();\n const f64 INF = 1e18;\n map<pair<int, int>, f64> dp;\n queue<pair<int, int>> q;\n dp[{0, sz - 1}] = 0;\n q.push({0, sz - 1});\n while(not q.empty()) {\n auto [i, j] = q.front(); q.pop();\n for(int ni : {i - 1, i, i + 1}) {\n for(int nj : {j - 1, j, j + 1}) {\n if(0 <= ni and ni <= nj and nj < sz and y[ni] == y[nj]) {\n const f64 ci = hypot(x[i] - x[ni], y[i] - y[ni]);\n const f64 cj = hypot(x[j] - x[nj], y[j] - y[nj]);\n if(not dp.count({ni, nj})) dp[{ni, nj}] = INF;\n if(chmin(dp[{ni, nj}], dp[{i, j}] + ci + cj)) q.push({ni, nj});\n }\n }\n }\n }\n f64 ans = INF;\n rep(i, sz) chmin(ans, dp[{i, i}]);\n return ans;\n}\n\nint main() {\n cin.tie(0)->sync_with_stdio(0);\n \n while(true) {\n int n; cin >> n; if(n == 0) return 0;\n cout << fixed << setprecision(20) << solve(n) << \"\\n\";\n }\n}", "accuracy": 1, "time_ms": 960, "memory_kb": 6340, "score_of_the_acc": -0.1597, "final_rank": 12 }, { "submission_id": "aoj_1309_9737219", "code_snippet": "#include <bits/stdc++.h>\n\nusing namespace std;\n\n#define rep(i, a, b) for (int i = (int)(a); i < (int)(b); i++)\n#define rrep(i, a, b) for (int i = (int)(b)-1; i >= (int)(a); i--)\n#define ALL(v) (v).begin(), (v).end()\n#define UNIQUE(v) sort(ALL(v)), (v).erase(unique(ALL(v)), (v).end())\n#define SZ(v) (int)v.size()\n#define MIN(v) *min_element(ALL(v))\n#define MAX(v) *max_element(ALL(v))\n#define LB(v, x) int(lower_bound(ALL(v), (x)) - (v).begin())\n#define UB(v, x) int(upper_bound(ALL(v), (x)) - (v).begin())\n\nusing uint = unsigned int;\nusing ll = long long int;\nusing ull = unsigned long long;\nusing i128 = __int128_t;\nusing u128 = __uint128_t;\nconst int inf = 0x3fffffff;\nconst ll INF = 0x1fffffffffffffff;\n\ntemplate <typename T> inline bool chmax(T &a, T b) {\n if (a < b) {\n a = b;\n return 1;\n }\n return 0;\n}\ntemplate <typename T> inline bool chmin(T &a, T b) {\n if (a > b) {\n a = b;\n return 1;\n }\n return 0;\n}\ntemplate <typename T, typename U> T ceil(T x, U y) {\n assert(y != 0);\n if (y < 0)\n x = -x, y = -y;\n return (x > 0 ? (x + y - 1) / y : x / y);\n}\ntemplate <typename T, typename U> T floor(T x, U y) {\n assert(y != 0);\n if (y < 0)\n x = -x, y = -y;\n return (x > 0 ? x / y : (x - y + 1) / y);\n}\ntemplate <typename T> int popcnt(T x) {\n return __builtin_popcountll(x);\n}\ntemplate <typename T> int topbit(T x) {\n return (x == 0 ? -1 : 63 - __builtin_clzll(x));\n}\ntemplate <typename T> int lowbit(T x) {\n return (x == 0 ? -1 : __builtin_ctzll(x));\n}\n\ntemplate <class T, class U>\nostream &operator<<(ostream &os, const pair<T, U> &p) {\n os << \"P(\" << p.first << \", \" << p.second << \")\";\n return os;\n}\ntemplate <typename T> ostream &operator<<(ostream &os, const vector<T> &vec) {\n os << \"{\";\n for (int i = 0; i < vec.size(); i++) {\n os << vec[i] << (i + 1 == vec.size() ? \"\" : \", \");\n }\n os << \"}\";\n return os;\n}\ntemplate <typename T, typename U>\nostream &operator<<(ostream &os, const map<T, U> &map_var) {\n os << \"{\";\n for (auto itr = map_var.begin(); itr != map_var.end(); itr++) {\n os << \"(\" << itr->first << \", \" << itr->second << \")\";\n itr++;\n if (itr != map_var.end())\n os << \", \";\n itr--;\n }\n os << \"}\";\n return os;\n}\ntemplate <typename T> ostream &operator<<(ostream &os, const set<T> &set_var) {\n os << \"{\";\n for (auto itr = set_var.begin(); itr != set_var.end(); itr++) {\n os << *itr;\n ++itr;\n if (itr != set_var.end())\n os << \", \";\n itr--;\n }\n os << \"}\";\n return os;\n}\n#ifdef LOCAL\n#define show(...) _show(0, #__VA_ARGS__, __VA_ARGS__)\n#else\n#define show(...) true\n#endif\ntemplate <typename T> void _show(int i, T name) {\n cerr << '\\n';\n}\ntemplate <typename T1, typename T2, typename... T3>\nvoid _show(int i, const T1 &a, const T2 &b, const T3 &...c) {\n for (; a[i] != ',' && a[i] != '\\0'; i++)\n cerr << a[i];\n cerr << \":\" << b << \" \";\n _show(i + 1, a, c...);\n}\n\n/**\n * @brief template\n */\n\nint main() {\nwhile(1) {\n int N;\n cin >> N;\n if (N == 0) return 0;\n vector<int> X(N), Y(N), YS;\n rep(i,0,N) cin >> X[i] >> Y[i], YS.push_back(Y[i]);\n UNIQUE(YS);\n int M = YS.size();\n N--;\n auto in = [&](int A, int H) -> bool {\n if (Y[A] <= Y[A+1]) return Y[A] <= YS[H] && YS[H] <= Y[A+1];\n else return Y[A+1] <= YS[H] && YS[H] <= Y[A];\n };\n auto Dist = [&](int A, double H) -> double {\n if (Y[A] == Y[A+1]) return (double)(X[A+1]-X[A]);\n H = abs(H);\n double DX = (double)(X[A+1]-X[A]), DY = (double)(abs(Y[A+1]-Y[A]));\n return sqrt(DX*DX+DY*DY)*H/DY;\n };\n vector DP(N,vector(N,vector<double>(M,inf)));\n priority_queue<tuple<double,int,int,int>,vector<tuple<double,int,int,int>>,greater<tuple<double,int,int,int>>> PQ;\n DP[0][N-1][0] = 0;\n PQ.push({0,0,N-1,0});\n while(!PQ.empty()) {\n auto [D,A,B,H] = PQ.top();\n PQ.pop();\n if (D > DP[A][B][H]) continue;\n double ND;\n int NA, NB, NH;\n if (A != 0 && Y[A] == YS[H]) {\n ND = D + Dist(A,0.0);\n NA = A-1;\n NB = B;\n NH = H;\n if (chmin(DP[NA][NB][NH],ND)) PQ.push({ND,NA,NB,NH});\n }\n if (A != N-1 && Y[A+1] == YS[H]) {\n ND = D + Dist(A,0.0);\n NA = A+1;\n NB = B;\n NH = H;\n if (chmin(DP[NA][NB][NH],ND)) PQ.push({ND,NA,NB,NH});\n }\n if (B != 0 && Y[B] == YS[H]) {\n ND = D + Dist(B,0.0);\n NA = A;\n NB = B-1;\n NH = H;\n if (chmin(DP[NA][NB][NH],ND)) PQ.push({ND,NA,NB,NH});\n }\n if (B != N-1 && Y[B+1] == YS[H]) {\n ND = D + Dist(B,0.0);\n NA = A;\n NB = B+1;\n NH = H;\n if (chmin(DP[NA][NB][NH],ND)) PQ.push({ND,NA,NB,NH});\n }\n if (H != 0 && in(A,H-1) && in(B,H-1)) {\n ND = D + Dist(A,YS[H]-YS[H-1]) + Dist(B,YS[H]-YS[H-1]);\n NA = A;\n NB = B;\n NH = H-1;\n if (chmin(DP[NA][NB][NH],ND)) PQ.push({ND,NA,NB,NH});\n }\n if (H != M-1 && in(A,H+1) && in(B,H+1)) {\n ND = D + Dist(A,YS[H+1]-YS[H]) + Dist(B,YS[H+1]-YS[H]);\n NA = A;\n NB = B;\n NH = H+1;\n if (chmin(DP[NA][NB][NH],ND)) PQ.push({ND,NA,NB,NH});\n }\n }\n double ANS = inf;\n rep(i,0,N) {\n rep(j,0,M) {\n if (DP[i][i][j] < inf) chmin(ANS,DP[i][i][j]+Dist(i,0.0));\n }\n }\n printf(\"%.12f\\n\",ANS);\n}\n}", "accuracy": 1, "time_ms": 300, "memory_kb": 11360, "score_of_the_acc": -0.1002, "final_rank": 6 }, { "submission_id": "aoj_1309_8520718", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\n#define rep(i, n) for (int i = 0; i < (n); i++)\n#define per(i, n) for (int i = (n)-1; i >= 0; i--)\n#define rep2(i, l, r) for (int i = (l); i < (r); i++)\n#define per2(i, l, r) for (int i = (r)-1; i >= (l); i--)\n#define each(e, x) for (auto &e : x)\n#define sz(x) (int)x.size()\n#define all(x) begin(x), end(x)\n#define rall(x) rbegin(x), rend(x)\n#define eb emplace_back\n#define MM << ' ' <<\n#define TT template <typename T>\nusing ll = long long;\nusing pii = pair<int, int>;\nusing pll = pair<ll, ll>;\n\nTT bool chmin(T &x, T y) { return x > y ? (x = y, true) : false; }\nTT bool chmax(T &x, T y) { return x < y ? (x = y, true) : false; }\n\nTT using minheap = priority_queue<T, vector<T>, greater<T>>;\nTT using maxheap = priority_queue<T>;\n\nTT void print(vector<T> v, T x = 0) {\n int n = sz(v);\n rep(i, n) cout << v[i] + x << (i == n - 1 ? '\\n' : ' ');\n if (n == 0) cout << '\\n';\n}\n\nTT void printn(vector<T> v, T x = 0) {\n int n = sz(v);\n rep(i, n) cout << v[i] + x << '\\n';\n}\n\nTT void rearrange(vector<T> &v) {\n sort(all(v));\n v.erase(unique(all(v)), end(v));\n}\n\nTT int lb(vector<T> &v, T x) { return lower_bound(all(v), x) - begin(v); }\nTT int ub(vector<T> &v, T x) { return upper_bound(all(v), x) - begin(v); }\n\nconst int inf = (1 << 30) - 1;\nconst ll INF = (1LL << 60) - 1;\n\nstruct io_setup {\n io_setup() {\n ios_base::sync_with_stdio(false);\n cin.tie(NULL);\n cout << fixed << setprecision(15);\n }\n} io_setup;\n\nint main() {\n while (1) {\n int n;\n cin >> n;\n if (!n)\n break;\n vector<int> x(n), y(n);\n rep(i, n) cin >> x[i] >> y[i];\n vector<int> ys = y;\n rearrange(ys);\n int m = sz(ys);\n rep(i, n) y[i] = lb(ys, y[i]);\n y.eb(0);\n using Real = double;\n vector dp(n, vector(n, vector(m, (Real)1e9)));\n minheap<pair<Real, pair<int, pii>>> que;\n auto in = [&](int idx, int h) {\n return (min(y[idx], y[idx + 1]) <= h && h <= max(y[idx], y[idx + 1]));\n };\n rep(i, n - 1) rep(j, m) if (in(i, j)) dp[i][i][j] = 0, que.emplace(0, pair(j, pair(i, i)));\n auto dist = [&](int idx, int y1, int y2) {\n double dy = abs(ys[y1] - ys[y2]);\n double dx = (x[idx + 1] - x[idx]) * (dy / abs(ys[y[idx]] - ys[y[idx + 1]]));\n return sqrt(dx * dx + dy * dy);\n };\n while (sz(que)) {\n auto [nd, huv] = que.top();\n auto [nh, uv] = huv;\n auto [u, v] = uv;\n que.pop();\n if (nd > dp[u][v][nh] + 1e-6)\n continue;\n rep(h, m) {\n if (h != nh && in(u, h) && in(v, h)) {\n Real ned = nd + dist(u, nh, h) + dist(v, nh, h);\n if (chmin(dp[u][v][h], ned))\n que.emplace(ned, pair(h, pair(u, v)));\n }\n }\n if (u > 0) {\n int cost = (y[u - 1] == y[u] ? x[u] - x[u - 1] : 0);\n if (nh == y[u] && chmin(dp[u - 1][v][nh], nd + cost))\n que.emplace(nd + cost, pair(nh, pair(u - 1, v)));\n }\n if (u < n - 1) {\n int cost = (y[u] == y[u + 1] ? x[u + 1] - x[u] : 0);\n if (nh == y[u + 1] && chmin(dp[u + 1][v][nh], nd + cost))\n que.emplace(nd + cost, pair(nh, pair(u + 1, v)));\n }\n if (v > 0) {\n int cost = (y[v - 1] == y[v] ? x[v] - x[v - 1] : 0);\n if (nh == y[v] && chmin(dp[u][v - 1][nh], nd + cost))\n que.emplace(nd + cost, pair(nh, pair(u, v - 1)));\n }\n if (v < n - 1) {\n int cost = (y[v] == y[v + 1] ? x[v + 1] - x[v] : 0);\n if (nh == y[v + 1] && chmin(dp[u][v + 1][nh], nd + cost))\n que.emplace(nd + cost, pair(nh, pair(u, v + 1)));\n } \n }\n cout << dp[0][n - 1][0] << endl;\n }\n}", "accuracy": 1, "time_ms": 6350, "memory_kb": 12048, "score_of_the_acc": -1.0651, "final_rank": 16 }, { "submission_id": "aoj_1309_8291258", "code_snippet": "#include <iostream>\n#include <cmath>\n#include <queue>\n#include <tuple>\n#include <vector>\n#include <algorithm>\nusing namespace std;\n\n// MLE がきつい…\nint N;\nint X[10009], Y[10009];\nint Height[10009];\nint Henkan[10009];\ndouble Dist[109][109][109];\ndouble Len[10009];\nvector<pair<double, double>> List;\nvector<int> ID[10009];\n\nvoid Initialize() {\n\tList.clear();\n\tfor (int i = 0; i < 10009; i++) {\n\t\tX[i] = 0; Y[i] = 0; Height[i] = 0; Henkan[i] = 0;\n\t\tLen[i] = 0; ID[i].clear();\n\t}\n}\n\nint main() {\n\twhile (true) {\n\t\t// Step 1. Input\n\t\tInitialize();\n\t\tcin >> N; if (N == 0) break;\n\t\tfor (int i = 1; i <= N; i++) cin >> X[i] >> Y[i];\n\n\t\t// Step 2. Sorting\n\t\tvector<int> vec;\n\t\tfor (int i = 1; i <= N; i++) vec.push_back(Y[i]);\n\t\tsort(vec.begin(), vec.end());\n\t\tvec.erase(unique(vec.begin(), vec.end()), vec.end());\n\n\t\t// Step 3. Enumerate Checkpoint\n\t\tfor (int i = 1; i <= N - 1; i++) {\n\t\t\tint pos1 = lower_bound(vec.begin(), vec.end(), Y[i + 0]) - vec.begin();\n\t\t\tint pos2 = lower_bound(vec.begin(), vec.end(), Y[i + 1]) - vec.begin();\n\t\t\tif (pos1 < pos2) {\n\t\t\t\tfor (int j = pos1 + 1; j <= pos2 - 1; j++) {\n\t\t\t\t\tdouble py = vec[j];\n\t\t\t\t\tdouble px = 1.0 * X[i + 0] + 1.0 * (X[i + 1] - X[i + 0]) * (py - Y[i + 0]) / (Y[i + 1] - Y[i + 0]);\n\t\t\t\t\tList.push_back(make_pair(px, py));\n\t\t\t\t}\n\t\t\t}\n\t\t\tif (pos1 > pos2) {\n\t\t\t\tfor (int j = pos1 - 1; j >= pos2 + 1; j--) {\n\t\t\t\t\tdouble py = vec[j];\n\t\t\t\t\tdouble px = 1.0 * X[i + 0] + 1.0 * (X[i + 1] - X[i + 0]) * (Y[i + 0] - py) / (Y[i + 0] - Y[i + 1]);\n\t\t\t\t\tList.push_back(make_pair(px, py));\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\tfor (int i = 1; i <= N; i++) List.push_back(make_pair(1.0 * X[i], 1.0 * Y[i]));\n\t\tsort(List.begin(), List.end());\n\t\tfor (int i = 0; i < List.size() - 1; i++) {\n\t\t\tdouble diff_x = abs(List[i + 1].first - List[i].first);\n\t\t\tdouble diff_y = abs(List[i + 1].second - List[i].second);\n\t\t\tLen[i] = sqrt(diff_x * diff_x + diff_y * diff_y);\n\t\t}\n\n\t\t// Step 4. Enumerate CheckPoint 2\n\t\tfor (int i = 0; i < List.size(); i++) {\n\t\t\tint pos1 = lower_bound(vec.begin(), vec.end(), List[i].second) - vec.begin();\n\t\t\tHenkan[i] = ID[pos1].size();\n\t\t\tHeight[i] = pos1;\n\t\t\tID[pos1].push_back(i);\n\t\t}\n\t\t\n\t\t// Step 5. Dijkstra\n\t\tpriority_queue<tuple<double, int, int, int>, vector<tuple<double, int, int, int>>, greater<tuple<double, int, int, int>>> Q;\n\t\tfor (int i = 0; i <= N; i++) {\n\t\t\tfor (int j = 0; j <= N; j++) {\n\t\t\t\tfor (int k = 0; k <= N; k++) Dist[i][j][k] = 1e9;\n\t\t\t}\n\t\t}\n\t\tDist[0][Henkan[0]][Henkan[List.size() - 1]] = 0;\n\t\tQ.push(make_tuple(0, 0, Henkan[0], Henkan[List.size() - 1]));\n\n\t\t// Step 6. Dijkstra Main\n\t\twhile (!Q.empty()) {\n\t\t\tint val1 = get<1>(Q.top());\n\t\t\tint val2 = get<2>(Q.top());\n\t\t\tint val3 = get<3>(Q.top());\n\t\t\tint pos1 = ID[val1][val2];\n\t\t\tint pos2 = ID[val1][val3];\n\t\t\tQ.pop();\n\t\t\tfor (int j = -1; j <= 1; j++) {\n\t\t\t\tfor (int k = -1; k <= 1; k++) {\n\t\t\t\t\tint nex1 = pos1 + j; if (nex1 < 0 || nex1 >= List.size()) continue;\n\t\t\t\t\tint nex2 = pos2 + k; if (nex2 < 0 || nex2 >= List.size()) continue;\n\t\t\t\t\tif (List[nex1].second != List[nex2].second) continue;\n\t\t\t\t\tdouble cost = 0;\n\t\t\t\t\tif (j != 0) cost += Len[min(pos1, nex1)];\n\t\t\t\t\tif (k != 0) cost += Len[min(pos2, nex2)];\n\t\t\t\t\tint cur1 = Height[nex1];\n\t\t\t\t\tint cur2 = Henkan[nex1];\n\t\t\t\t\tint cur3 = Henkan[nex2];\n\t\t\t\t\tif (Dist[cur1][cur2][cur3] > Dist[val1][val2][val3] + cost) {\n\t\t\t\t\t\tDist[cur1][cur2][cur3] = Dist[val1][val2][val3] + cost;\n\t\t\t\t\t\tQ.push(make_tuple(Dist[cur1][cur2][cur3], cur1, cur2, cur3));\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\n\t\t// Step 7. Output\n\t\tdouble Answer = 1e9;\n\t\tfor (int i = 0; i < List.size(); i++) Answer = min(Answer, (double)Dist[Height[i]][Henkan[i]][Henkan[i]]);\n\t\tprintf(\"%.12lf\\n\", Answer);\n\t}\n\treturn 0;\n}", "accuracy": 1, "time_ms": 210, "memory_kb": 13944, "score_of_the_acc": -0.1091, "final_rank": 7 }, { "submission_id": "aoj_1309_6402864", "code_snippet": "#include \"bits/stdc++.h\"\nusing namespace std;\n\ntypedef string::const_iterator State;\n#define eps 1e-8L\n#define MAX_MOD 1000000007LL\n#define GYAKU 500000004LL\n#define MOD 998244353LL\n#define pb push_back\n#define mp make_pair\ntypedef long long ll;\ntypedef long double ld;\n#define REP(a, b) for (long long(a) = 0; (a) < (b); ++(a))\n#define ALL(x) (x).begin(), (x).end()\n\n#define int long long\n\nstruct info\n{\n ld d;\n int l, r;\n info(ld d, int l, int r) : d(d), l(l), r(r) {}\n info() {}\n bool operator<(const info &a) const\n {\n return d > a.d;\n }\n};\n\nvoid solve()\n{\n int n;\n cin >> n;\n if (n == 0)\n exit(0);\n vector<pair<ld, ld>> inputs;\n REP(i, n)\n {\n ld a, b;\n cin >> a >> b;\n inputs.push_back({a, b});\n }\n REP(i, n)\n {\n REP(j, n - 1)\n {\n if ((inputs[j].second < inputs[i].second and inputs[i].second < inputs[j + 1].second) || (inputs[j + 1].second < inputs[i].second and inputs[i].second < inputs[j].second))\n {\n double rate = (inputs[i].second - inputs[j].second) / (inputs[j + 1].second - inputs[j].second);\n inputs.push_back(make_pair(inputs[j].first + rate * (inputs[j + 1].first - inputs[j].first), inputs[i].second));\n }\n }\n }\n sort(ALL(inputs));\n inputs.erase(unique(inputs.begin(), inputs.end()), inputs.end());\n\n int m = inputs.size();\n priority_queue<info> wait;\n wait.push(info(0, 0, m - 1));\n map<pair<int, int>, ld> costs;\n costs[{0, m - 1}] = 0;\n ld ans = 1e18;\n while (!wait.empty())\n {\n ld d = wait.top().d;\n int l = wait.top().l;\n int r = wait.top().r;\n wait.pop();\n if (d > costs[{l, r}] + eps)\n continue;\n if (l == r)\n ans = min(ans, d);\n for (int i = -1; i <= 1; i += 1)\n {\n for (int j = -1; j <= 1; j += 1)\n {\n if (l + i < 0 or m <= l + i)\n continue;\n if (r + j < 0 or m <= r + j)\n continue;\n if (abs(inputs[l + i].second - inputs[r + j].second) < eps)\n {\n ld x1 = inputs[l].first - inputs[l + i].first;\n ld x2 = inputs[r].first - inputs[r + j].first;\n ld y1 = inputs[l].second - inputs[l+i].second;\n ld y2 = inputs[r].second - inputs[r+j].second;\n ld nd = d + sqrt(x1 * x1 + y1 * y1) + sqrt(x2 * x2 + y2 * y2);\n pair<double, double> nlr(l + i, r + j);\n if(costs.count(nlr) == 0 or nd + eps < costs[nlr]){\n costs[nlr] = nd;\n wait.push(info(nd, l + i, r + j));\n }\n }\n }\n }\n }\n cout << ans << endl;\n}\n#undef int\n\n// generated by oj-template v4.7.2\n// (https://github.com/online-judge-tools/template-generator)\nint main()\n{\n // Fasterize input/output script\n ios::sync_with_stdio(false);\n cin.tie(nullptr);\n cout << fixed << setprecision(10);\n // scanf/printf user should delete this fasterize input/output script\n\n int t = 10000;\n // cin >> t; // comment out if solving multi testcase\n for (int testCase = 1; testCase <= t; ++testCase)\n {\n solve();\n }\n return 0;\n}", "accuracy": 1, "time_ms": 1780, "memory_kb": 10792, "score_of_the_acc": -0.3296, "final_rank": 15 }, { "submission_id": "aoj_1309_6029320", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\n#define rep(i, s, t) for (int i = (int)(s); i < (int)(t); ++i)\n\nusing T = tuple<double, int, int>;\n\ntemplate <typename T>\nostream& operator<<(ostream& os, const vector<T>& v) {\n for (int i = 0; i < (int) v.size(); ++i) {\n os << v[i];\n if (i < (int) v.size() - 1) os << \" \";\n }\n return os;\n}\n\nconstexpr int INF = 1e9;\n\nint main() {\n ios_base::sync_with_stdio(false);\n cin.tie(nullptr);\n cout << fixed << setprecision(15);\n\n while (1) {\n int N;\n cin >> N;\n if (N == 0) break;\n vector<int> x(N), y(N), ys;\n rep(i,0,N) {\n cin >> x[i] >> y[i];\n ys.push_back(y[i]);\n }\n sort(ys.begin(), ys.end());\n ys.erase(unique(ys.begin(), ys.end()), ys.end());\n vector<double> xx;\n vector<int> yy;\n rep(i,0,N-1) {\n vector<pair<double, int>> t;\n t.push_back({x[i], y[i]});\n for (int v : ys) {\n if (v <= min(y[i], y[i+1]) || max(y[i], y[i+1]) <= v) continue;\n t.push_back({x[i]+1.0*(x[i+1]-x[i])*(v-y[i])/(y[i+1]-y[i]), v});\n }\n sort(t.begin(), t.end());\n for (auto [u, v] : t) xx.push_back(u), yy.push_back(v);\n }\n xx.push_back(x[N-1]);\n yy.push_back(0);\n int n = xx.size();\n set<pair<int, int>> done;\n\n priority_queue<T, vector<T>, greater<>> pq;\n pq.push({0, 0, n-1});\n while (!pq.empty()) {\n auto [d, i, j] = pq.top(); pq.pop();\n if (i == j) {\n cout << d << \"\\n\";\n break;\n }\n if (done.count({i,j})) continue;\n done.insert({i,j});\n rep(di,-1,2) rep(dj,-1,2) {\n if (di == 0 && dj == 0) continue;\n int ni = i+di, nj = j+dj;\n if (ni > nj || ni < 0 || nj >= n || yy[ni] != yy[nj]) continue;\n double nd = d;\n nd += sqrt(pow(xx[ni]-xx[i], 2)+pow(yy[ni]-yy[i], 2));\n nd += sqrt(pow(xx[nj]-xx[j], 2)+pow(yy[nj]-yy[j], 2));\n pq.push({nd, ni, nj});\n }\n }\n }\n}", "accuracy": 1, "time_ms": 130, "memory_kb": 4788, "score_of_the_acc": -0.0143, "final_rank": 2 }, { "submission_id": "aoj_1309_6023915", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\nstruct node {\n double x, y;\n node(double x, double y) : x(x), y(y) {}\n};\n\ndouble dist(const node& a, const node& b) { return sqrt((a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y)); }\n\nbool solve() {\n int N;\n cin >> N;\n if (N == 0) return false;\n vector<int> x(N), y(N);\n for (int i = 0; i < N; i++) cin >> x[i] >> y[i];\n\n vector<int> ys = y;\n sort(ys.begin(), ys.end());\n ys.erase(unique(ys.begin(), ys.end()), ys.end());\n vector<node> vs;\n int sz = ys.size();\n for (int i = 0; i < N; i++) {\n vs.emplace_back(x[i], y[i]);\n if (i == N - 1) break;\n double d = double(x[i + 1] - x[i]) / abs(y[i + 1] - y[i]);\n if (y[i] < y[i + 1]) {\n for (int j = 0; j < sz; j++) {\n if (y[i] < ys[j] and ys[j] < y[i + 1]) {\n vs.emplace_back(x[i] + d * (ys[j] - y[i]), ys[j]);\n }\n }\n } else {\n for (int j = sz - 1; j >= 0; j--) {\n if (ys[j] < y[i] and y[i + 1] < ys[j]) {\n vs.emplace_back(x[i] + d * (y[i] - ys[j]), ys[j]);\n }\n }\n }\n }\n\n int n = vs.size();\n vector<map<int, double>> dp(n);\n priority_queue<tuple<double, int, int>> pq;\n dp[0][n - 1] = 0;\n pq.emplace(-dp[0][n - 1], 0, n - 1);\n\n while (!pq.empty()) {\n auto t = pq.top();\n pq.pop();\n double val;\n int a, b;\n tie(val, a, b) = t;\n val *= -1;\n if (dp[a][b] < val) continue;\n if (a == b) {\n cout << val << '\\n';\n return true;\n }\n for (int da : {-1, 0, 1}) {\n for (int db : {-1, 0, 1}) {\n int na = a + da, nb = b + db;\n if (na < 0 or n <= na or nb < 0 or n <= nb) continue;\n if (vs[na].y != vs[nb].y) continue;\n double cost = val + dist(vs[a], vs[na]) + dist(vs[b], vs[nb]);\n if (!dp[na].count(nb) or cost < dp[na][nb]) {\n dp[na][nb] = cost;\n pq.emplace(-cost, na, nb);\n }\n }\n }\n }\n\n return true;\n}\n\nint main() {\n cin.tie(0);\n ios::sync_with_stdio(false);\n cout << fixed << setprecision(10);\n while (solve())\n ;\n return 0;\n}", "accuracy": 1, "time_ms": 60, "memory_kb": 5528, "score_of_the_acc": -0.0098, "final_rank": 1 }, { "submission_id": "aoj_1309_6023914", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\nstruct node {\n double x, y;\n node(double x, double y) : x(x), y(y) {}\n};\n\ndouble dist(const node& a, const node& b) { return sqrt((a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y)); }\n\nbool solve() {\n int N;\n cin >> N;\n if (N == 0) return false;\n vector<int> x(N), y(N);\n for (int i = 0; i < N; i++) cin >> x[i] >> y[i];\n\n vector<int> ys = y;\n sort(ys.begin(), ys.end());\n ys.erase(unique(ys.begin(), ys.end()), ys.end());\n vector<node> vs;\n int sz = ys.size();\n for (int i = 0; i < N; i++) {\n vs.emplace_back(x[i], y[i]);\n if (i == N - 1) break;\n double d = double(x[i + 1] - x[i]) / abs(y[i + 1] - y[i]);\n if (y[i] < y[i + 1]) {\n for (int j = 0; j < sz; j++) {\n if (y[i] < ys[j] and ys[j] < y[i + 1]) {\n vs.emplace_back(x[i] + d * (ys[j] - y[i]), ys[j]);\n }\n }\n } else {\n for (int j = sz - 1; j >= 0; j--) {\n if (ys[j] < y[i] and y[i + 1] < ys[j]) {\n vs.emplace_back(x[i] + d * (y[i] - ys[j]), ys[j]);\n }\n }\n }\n }\n\n int n = vs.size();\n vector<map<int, double>> dp(n);\n priority_queue<tuple<double, int, int>> pq;\n dp[0][n - 1] = 0;\n pq.emplace(-dp[0][n - 1], 0, n - 1);\n\n while (!pq.empty()) {\n auto t = pq.top();\n pq.pop();\n double val;\n int a, b;\n tie(val, a, b) = t;\n val *= -1;\n if (dp[a][b] < val) continue;\n for (int da : {-1, 0, 1}) {\n for (int db : {-1, 0, 1}) {\n int na = a + da, nb = b + db;\n if (na < 0 or n <= na or nb < 0 or n <= nb) continue;\n if (vs[na].y != vs[nb].y) continue;\n double cost = val + dist(vs[a], vs[na]) + dist(vs[b], vs[nb]);\n if (!dp[na].count(nb) or cost < dp[na][nb]) {\n dp[na][nb] = cost;\n pq.emplace(-cost, na, nb);\n }\n }\n }\n }\n\n double ans = 1e9;\n for (int i = 0; i < n; i++) {\n if (dp[i].count(i)) {\n ans = min(ans, dp[i][i]);\n }\n }\n\n cout << ans << '\\n';\n return true;\n}\n\nint main() {\n cin.tie(0);\n ios::sync_with_stdio(false);\n cout << fixed << setprecision(10);\n while (solve())\n ;\n return 0;\n}", "accuracy": 1, "time_ms": 590, "memory_kb": 9364, "score_of_the_acc": -0.1282, "final_rank": 10 }, { "submission_id": "aoj_1309_6023909", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\nstruct vertex {\n int l, r;\n double x;\n int y, id;\n vertex(int l, int r, double x, int y, int id) : l(l), r(r), x(x), y(y), id(id) {}\n};\n\ndouble dist(const vertex& a, const vertex& b) { return sqrt((a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y)); }\n\nbool solve() {\n int N;\n cin >> N;\n if (N == 0) return false;\n vector<int> x(N), y(N);\n for (int i = 0; i < N; i++) cin >> x[i] >> y[i];\n\n vector<int> ys = y;\n sort(ys.begin(), ys.end());\n ys.erase(unique(ys.begin(), ys.end()), ys.end());\n vector<vertex> vs;\n int sz = ys.size(), n = 0;\n for (int i = 0; i < N; i++) {\n vs.emplace_back(i, i, x[i], y[i], n++);\n if (i == N - 1) break;\n double d = double(x[i + 1] - x[i]) / abs(y[i + 1] - y[i]);\n if (y[i] < y[i + 1]) {\n for (int j = 0; j < sz; j++) {\n if (y[i] < ys[j] and ys[j] < y[i + 1]) {\n vs.emplace_back(i, i + 1, x[i] + d * (ys[j] - y[i]), ys[j], n++);\n }\n }\n } else {\n for (int j = sz - 1; j >= 0; j--) {\n if (ys[j] < y[i] and y[i + 1] < ys[j]) {\n vs.emplace_back(i, i + 1, x[i] + d * (y[i] - ys[j]), ys[j], n++);\n }\n }\n }\n }\n\n vector<map<int, double>> dp(n);\n priority_queue<tuple<double, int, int>> pq;\n dp[0][n - 1] = 0;\n pq.emplace(-dp[0][n - 1], 0, n - 1);\n\n while (!pq.empty()) {\n auto t = pq.top();\n pq.pop();\n double val;\n int a, b;\n tie(val, a, b) = t;\n val *= -1;\n if (dp[a][b] < val) continue;\n for (int da : {-1, 0, 1}) {\n for (int db : {-1, 0, 1}) {\n int na = a + da, nb = b + db;\n if (na < 0 or n <= na or nb < 0 or n <= nb) continue;\n if (vs[na].y != vs[nb].y) continue;\n double cost = val + dist(vs[a], vs[na]) + dist(vs[b], vs[nb]);\n if (!dp[na].count(nb) or cost < dp[na][nb]) {\n dp[na][nb] = cost;\n pq.emplace(-cost, na, nb);\n }\n }\n }\n }\n\n double ans = 1e9;\n for (int i = 0; i < n; i++) {\n if (dp[i].count(i)) {\n ans = min(ans, dp[i][i]);\n }\n }\n\n cout << ans << '\\n';\n return true;\n}\n\nint main() {\n cin.tie(0);\n ios::sync_with_stdio(false);\n cout << fixed << setprecision(10);\n while (solve())\n ;\n return 0;\n}", "accuracy": 1, "time_ms": 560, "memory_kb": 9432, "score_of_the_acc": -0.1241, "final_rank": 8 }, { "submission_id": "aoj_1309_6023908", "code_snippet": "#define LOCAL\n#include <bits/stdc++.h>\nusing namespace std;\n#pragma region Macros\ntypedef long long ll;\ntypedef __int128_t i128;\ntypedef unsigned int uint;\ntypedef unsigned long long ull;\n#define ALL(x) (x).begin(), (x).end()\n\ntemplate <typename T> istream& operator>>(istream& is, vector<T>& v) {\n for (T& x : v) is >> x;\n return is;\n}\ntemplate <typename T> ostream& operator<<(ostream& os, const vector<T>& v) {\n for (size_t i = 0; i < v.size(); i++) {\n os << v[i] << (i + 1 == v.size() ? \"\" : \" \");\n }\n return os;\n}\ntemplate <typename T, typename U> ostream& operator<<(ostream& os, const pair<T, U>& p) {\n os << '(' << p.first << ',' << p.second << ')';\n return os;\n}\ntemplate <typename T, typename U> ostream& operator<<(ostream& os, const map<T, U>& m) {\n os << '{';\n for (auto itr = m.begin(); itr != m.end();) {\n os << '(' << itr->first << ',' << itr->second << ')';\n if (++itr != m.end()) os << ',';\n }\n os << '}';\n return os;\n}\ntemplate <typename T, typename U> ostream& operator<<(ostream& os, const unordered_map<T, U>& m) {\n os << '{';\n for (auto itr = m.begin(); itr != m.end();) {\n os << '(' << itr->first << ',' << itr->second << ')';\n if (++itr != m.end()) os << ',';\n }\n os << '}';\n return os;\n}\ntemplate <typename T> ostream& operator<<(ostream& os, const set<T>& s) {\n os << '{';\n for (auto itr = s.begin(); itr != s.end();) {\n os << *itr;\n if (++itr != s.end()) os << ',';\n }\n os << '}';\n return os;\n}\ntemplate <typename T> ostream& operator<<(ostream& os, const multiset<T>& s) {\n os << '{';\n for (auto itr = s.begin(); itr != s.end();) {\n os << *itr;\n if (++itr != s.end()) os << ',';\n }\n os << '}';\n return os;\n}\ntemplate <typename T> ostream& operator<<(ostream& os, const unordered_set<T>& s) {\n os << '{';\n for (auto itr = s.begin(); itr != s.end();) {\n os << *itr;\n if (++itr != s.end()) os << ',';\n }\n os << '}';\n return os;\n}\ntemplate <typename T> ostream& operator<<(ostream& os, const deque<T>& v) {\n for (size_t i = 0; i < v.size(); i++) {\n os << v[i] << (i + 1 == v.size() ? \"\" : \" \");\n }\n return os;\n}\ntemplate <typename T, size_t N> ostream& operator<<(ostream& os, const array<T, N>& v) {\n for (size_t i = 0; i < N; i++) {\n os << v[i] << (i + 1 == N ? \"\" : \" \");\n }\n return os;\n}\n\ntemplate <int i, typename T> void print_tuple(ostream&, const T&) {}\ntemplate <int i, typename T, typename H, class... Args> void print_tuple(ostream& os, const T& t) {\n if (i) os << ',';\n os << get<i>(t);\n print_tuple<i + 1, T, Args...>(os, t);\n}\ntemplate <typename... Args> ostream& operator<<(ostream& os, const tuple<Args...>& t) {\n os << '{';\n print_tuple<0, tuple<Args...>, Args...>(os, t);\n return os << '}';\n}\n\nvoid debug_out() { cerr << '\\n'; }\ntemplate <class Head, class... Tail> void debug_out(Head&& head, Tail&&... tail) {\n cerr << head;\n if (sizeof...(Tail) > 0) cerr << \", \";\n debug_out(move(tail)...);\n}\n#ifdef LOCAL\n#define debug(...) \\\n cerr << \" \"; \\\n cerr << #__VA_ARGS__ << \" :[\" << __LINE__ << \":\" << __FUNCTION__ << \"]\" << '\\n'; \\\n cerr << \" \"; \\\n debug_out(__VA_ARGS__)\n#else\n#define debug(...) void(0)\n#endif\n\ntemplate <typename T> T gcd(T x, T y) { return y != 0 ? gcd(y, x % y) : x; }\ntemplate <typename T> T lcm(T x, T y) { return x / gcd(x, y) * y; }\n\nint topbit(signed t) { return t == 0 ? -1 : 31 - __builtin_clz(t); }\nint topbit(long long t) { return t == 0 ? -1 : 63 - __builtin_clzll(t); }\nint botbit(signed a) { return a == 0 ? 32 : __builtin_ctz(a); }\nint botbit(long long a) { return a == 0 ? 64 : __builtin_ctzll(a); }\nint popcount(signed t) { return __builtin_popcount(t); }\nint popcount(long long t) { return __builtin_popcountll(t); }\nbool ispow2(int i) { return i && (i & -i) == i; }\nlong long MSK(int n) { return (1LL << n) - 1; }\n\ntemplate <class T> T ceil(T x, T y) {\n assert(y >= 1);\n return (x > 0 ? (x + y - 1) / y : x / y);\n}\ntemplate <class T> T floor(T x, T y) {\n assert(y >= 1);\n return (x > 0 ? x / y : (x - y + 1) / y);\n}\n\ntemplate <class T1, class T2> inline bool chmin(T1& a, T2 b) {\n if (a > b) {\n a = b;\n return true;\n }\n return false;\n}\ntemplate <class T1, class T2> inline bool chmax(T1& a, T2 b) {\n if (a < b) {\n a = b;\n return true;\n }\n return false;\n}\n\ntemplate <typename T> void mkuni(vector<T>& v) {\n sort(v.begin(), v.end());\n v.erase(unique(v.begin(), v.end()), v.end());\n}\ntemplate <typename T> int lwb(const vector<T>& v, const T& x) { return lower_bound(v.begin(), v.end(), x) - v.begin(); }\n#pragma endregion\n\nconst int INF = 1e9;\nconst long long IINF = 1e18;\nconst int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};\nconst char dir[4] = {'D', 'R', 'U', 'L'};\nconst long long MOD = 1000000007;\n// const long long MOD = 998244353;\n\nstruct vertex {\n int l, r;\n double x;\n int y, id;\n vertex(int l, int r, double x, int y, int id) : l(l), r(r), x(x), y(y), id(id) {}\n};\n\ndouble dist(const vertex& a, const vertex& b) { return sqrt((a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y)); }\n\nbool solve() {\n int N;\n cin >> N;\n if (N == 0) return false;\n vector<int> x(N), y(N);\n for (int i = 0; i < N; i++) cin >> x[i] >> y[i];\n\n vector<int> ys = y;\n sort(ys.begin(), ys.end());\n ys.erase(unique(ys.begin(), ys.end()), ys.end());\n vector<vertex> vs;\n int sz = ys.size(), n = 0;\n for (int i = 0; i < N; i++) {\n vs.emplace_back(i, i, x[i], y[i], n++);\n if (i == N - 1) break;\n double d = double(x[i + 1] - x[i]) / abs(y[i + 1] - y[i]);\n if (y[i] < y[i + 1]) {\n for (int j = 0; j < sz; j++) {\n if (y[i] < ys[j] and ys[j] < y[i + 1]) {\n vs.emplace_back(i, i + 1, x[i] + d * (ys[j] - y[i]), ys[j], n++);\n }\n }\n } else {\n for (int j = sz - 1; j >= 0; j--) {\n if (ys[j] < y[i] and y[i + 1] < ys[j]) {\n vs.emplace_back(i, i + 1, x[i] + d * (y[i] - ys[j]), ys[j], n++);\n }\n }\n }\n }\n\n vector<map<int, double>> dp(n);\n priority_queue<tuple<double, int, int>> pq;\n dp[0][n - 1] = 0;\n pq.emplace(-dp[0][n - 1], 0, n - 1);\n\n while (!pq.empty()) {\n auto t = pq.top();\n pq.pop();\n double val;\n int a, b;\n tie(val, a, b) = t;\n val *= -1;\n if (dp[a][b] < val) continue;\n for (int da : {-1, 0, 1}) {\n for (int db : {-1, 0, 1}) {\n int na = a + da, nb = b + db;\n if (na < 0 or n <= na or nb < 0 or n <= nb) continue;\n if (vs[na].y != vs[nb].y) continue;\n double cost = val + dist(vs[a], vs[na]) + dist(vs[b], vs[nb]);\n if (!dp[na].count(nb) or cost < dp[na][nb]) {\n dp[na][nb] = cost;\n pq.emplace(-cost, na, nb);\n }\n }\n }\n }\n\n double ans = 1e9;\n for (int i = 0; i < n; i++) {\n if (dp[i].count(i)) {\n ans = min(ans, dp[i][i]);\n }\n }\n\n cout << ans << '\\n';\n return true;\n}\n\nint main() {\n cin.tie(0);\n ios::sync_with_stdio(false);\n cout << fixed << setprecision(10);\n while (solve())\n ;\n return 0;\n}", "accuracy": 1, "time_ms": 570, "memory_kb": 9544, "score_of_the_acc": -0.1267, "final_rank": 9 }, { "submission_id": "aoj_1309_5979131", "code_snippet": "#pragma GCC ta g(\"avx2\")\n#pragma GCC optimize(\"Ofast\")\n#pragma GCC optimize(\"unroll-loops\")\n#include <bits/stdc++.h>\nusing namespace std;\n#define DEBUG\n#ifdef DEBUG\ntemplate <class T, class U>\nostream &operator<<(ostream &os, const pair<T, U> &p) {\n os << '(' << p.first << ',' << p.second << ')';\n return os;\n}\ntemplate <class T> ostream &operator<<(ostream &os, const vector<T> &v) {\n os << '{';\n for(int i = 0; i < (int)v.size(); i++) {\n if(i) { os << ','; }\n os << v[i];\n }\n os << '}';\n return os;\n}\nvoid debugg() { cerr << endl; }\ntemplate <class T, class... Args>\nvoid debugg(const T &x, const Args &... args) {\n cerr << \" \" << x;\n debugg(args...);\n}\n#define debug(...) \\\n cerr << __LINE__ << \" [\" << #__VA_ARGS__ << \"]: \", debugg(__VA_ARGS__)\n#define dump(x) cerr << __LINE__ << \" \" << #x << \" = \" << (x) << endl\n#else\n#define debug(...) (void(0))\n#define dump(x) (void(0))\n#endif\nusing namespace std;\ntypedef long long ll;\ntypedef vector<ll> vl;\ntypedef vector<vl> vvl;\ntypedef vector<char> vc;\ntypedef vector<string> vs;\ntypedef vector<bool> vb;\ntypedef vector<double> vd;\ntypedef pair<ll,ll> P;\ntypedef pair<int,int> pii;\ntypedef vector<P> vpl;\ntypedef tuple<ll,ll,ll> tapu;\n#define rep(i,n) for(int i=0; i<(n); i++)\n#define REP(i,a,b) for(int i=(a); i<(b); i++)\n#define all(x) (x).begin(), (x).end()\n#define rall(x) (x).rbegin(), (x).rend()\nconst int inf = (1<<30)-1;\nconst ll linf = 1LL<<61;\nconst int MAX = 510000;\nint dy[8] = {0,1,0,-1,1,-1,-1,1};\nint dx[8] = {-1,0,1,0,1,-1,1,-1};\nconst double pi = acos(-1);\nconst double eps = 1e-7;\ntemplate<typename T1,typename T2> inline bool chmin(T1 &a,T2 b){\n\tif(a>b){\n\t\ta = b; return true;\n\t}\n\telse return false;\n}\ntemplate<typename T1,typename T2> inline bool chmax(T1 &a,T2 b){\n\tif(a<b){\n\t\ta = b; return true;\n\t}\n\telse return false;\n}\ntemplate<typename T> inline void print(T &a){\n int sz = a.size();\n for(auto itr = a.begin(); itr != a.end(); itr++){\n\t\tcout << *itr;\n sz--;\n if(sz) cout << \" \";\n\t}\n cout << \"\\n\";\n}\ntemplate<typename T1,typename T2> inline void print2(T1 a, T2 b){\n\tcout << a << \" \" << b << \"\\n\";\n}\ntemplate<typename T1,typename T2,typename T3> inline void print3(T1 a, T2 b, T3 c){\n\tcout << a << \" \" << b << \" \" << c << \"\\n\";\n}\nvoid mark() {cout << \"#\" << \"\\n\";}\nll pcount(ll x) {return __builtin_popcountll(x);}\n//#include <atcoder/mincostflow>\n//using namespace atcoder;\n//const int mod = 1e9 + 7;\nconst int mod = 998244353;\n\nbool in(ll y, ll x, ll h, ll w){\n return 0 <= y && y < h && 0 <= x && x < w;\n}\n\nusing tu = tuple<double,int,int>;\n\nint main(){\n while(1){\n int n; cin >> n;\n if(!n) break;\n vl x(n),y(n);\n set<int> se;\n rep(i,n) cin >> x[i] >> y[i], se.insert(y[i]);\n vd X;\n vl Y;\n rep(i,n){\n X.push_back(x[i]); Y.push_back(y[i]);\n if(i == n-1) break;\n int sgn = y[i+1]>y[i] ? 1 : -1;\n double dx = y[i+1]==y[i] ? 0 : (double)(x[i+1]-x[i]) / abs(y[i+1]-y[i]);\n for(int j=1; j<abs(y[i+1]-y[i]); j++){\n if(!se.count(y[i]+sgn*j)) continue;\n X.push_back(x[i]+dx*j);\n Y.push_back(y[i]+sgn*j);\n }\n }\n int N = X.size();\n map<pii,double> dp;\n priority_queue<tu,vector<tu>,greater<tu>> pq;\n dp[{0,N-1}] = 0;\n pq.emplace(0,0,N-1);\n auto dist = [&](double x1, int y1, double x2, int y2) -> double {\n return sqrt((y1-y2)*(y1-y2) + (x1-x2)*(x1-x2));\n };\n while(!pq.empty()){\n ll d,l,r;\n tie(d,l,r) = pq.top(); pq.pop();\n int cy = Y[l];\n double cxl = X[l], cxr = X[r];\n for(int i=l-1; i<=l+1; i++) for(int j=r-1; j<=r+1; j++){\n if(i<0||i>=N||j<0||j>=N) continue;\n if(i>j) continue;\n if(Y[i] != Y[j]) continue;\n double cost = dist(cxl,cy,X[i],Y[i]) + dist(cxr,cy,X[j],Y[j]);\n if(!dp.count({i,j})) dp[{i,j}] = dp[{l,r}] + cost, pq.emplace(dp[{i,j}],i,j);\n else if(chmin(dp[{i,j}], dp[{l,r}] + cost)) pq.emplace(dp[{i,j}],i,j); \n }\n }\n double ans = inf;\n rep(i,N) if(dp.count({i,i})) chmin(ans, dp[{i,i}]);\n printf(\"%.10f\\n\",ans);\n }\n}", "accuracy": 1, "time_ms": 1000, "memory_kb": 6220, "score_of_the_acc": -0.165, "final_rank": 13 }, { "submission_id": "aoj_1309_5810276", "code_snippet": "#pragma GCC optimize(\"Ofast\")\n#include <iostream>\n#include <vector>\n#include <algorithm>\n#include <map>\n#include <queue>\n#include <cstdio>\n#include <ctime>\n#include <assert.h>\n#include <chrono>\n#include <random>\n#include <numeric>\n#include <set>\n#include <deque>\n#include <stack>\n#include <sstream>\n#include <utility>\nusing namespace std;\ntypedef long long int ll;\ntypedef unsigned long long ull;\n\nmt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());\nll myRand(ll B) {\n return (ull)rng() % B;\n}\n\ndouble solve(int n){\n double res = 1e18;\n vector<int> x(n),y(n);\n vector<int> v;\n for(int i=0;i<n;i++){\n cin >> x[i] >> y[i];\n }\n v = y;\n sort(v.begin(), v.end());\n v.erase(unique(v.begin(), v.end()),v.end());\n vector<pair<double,int>> point;\n point.push_back({x[0],y[0]});\n int j = 0;\n for(int i=0;i+1<n;i++){\n int ad;\n if(y[i] < y[i+1]){\n ad = 1;\n }\n else if(y[i] > y[i+1]){\n ad = -1;\n }\n else{\n point.push_back({x[i+1],y[i+1]});\n continue;\n }\n while(y[i+1]!=v[j]){\n j+=ad;\n double nx = x[i];\n nx += (double)(x[i+1]-x[i]) * (double)abs(v[j]-y[i]) / (double)abs(y[i]-y[i+1]);\n point.push_back({nx,v[j]});\n }\n }\n n = point.size();\n vector<vector<bool>> dp(n,vector<bool>(n));\n priority_queue<pair<double,pair<int,int>>,vector<pair<double,pair<int,int>>>,greater<pair<double,pair<int,int>>>> pq;\n pq.push({0,{0,n-1}});\n while(pq.size()){\n auto p = pq.top(); pq.pop();\n int s = p.second.first, t = p.second.second;\n if(t == s){\n res = p.first;\n break;\n }\n if(dp[s][t])continue;\n dp[s][t] = 1; \n for(int i=-1;i<=1;i++){\n for(int j=-1;j<=1;j++){\n int si = s+i;\n int ti = t+j;\n if(0<=si and si<n and 0<=ti and ti<n and point[si].second == point[ti].second and !dp[si][ti]){\n double cost = p.first \n + sqrt((point[s].first-point[si].first)*(point[s].first-point[si].first) + (point[s].second-point[si].second)*(point[s].second-point[si].second))\n + sqrt((point[t].first-point[ti].first)*(point[t].first-point[ti].first) + (point[t].second-point[ti].second)*(point[t].second-point[ti].second));\n pq.push({cost,{si,ti}});\n }\n }\n }\n }\n return res;\n}\n\nint main(){\n cin.tie(nullptr);\n ios::sync_with_stdio(false);\n int n;\n while(cin >> n,n){\n printf(\"%.9f\\n\",solve(n));\n }\n}", "accuracy": 1, "time_ms": 40, "memory_kb": 6416, "score_of_the_acc": -0.0146, "final_rank": 3 }, { "submission_id": "aoj_1309_5244797", "code_snippet": "#include<iostream>\n#include<iomanip>\n#include<algorithm>\n#include<vector>\n#include<queue>\n#include<cmath>\nusing namespace std;\nint N;\ndouble dp[5050][5050];\nmain()\n{\n\twhile(cin>>N,N)\n\t{\n\t\tvector<pair<int,int> >A(N);\n\t\tvector<int>hs(N);\n\t\tfor(int i=0;i<N;i++)\n\t\t{\n\t\t\tcin>>A[i].first>>A[i].second;\n\t\t\ths[i]=A[i].second;\n\t\t}\n\t\tsort(hs.begin(),hs.end());\n\t\ths.erase(unique(hs.begin(),hs.end()),hs.end());\n\t\tvector<pair<double,int> >X;\n\t\tX.push_back(A[0]);\n\t\tfor(int i=1;i<N;i++)\n\t\t{\n\t\t\tint dx=A[i].first-A[i-1].first;\n\t\t\tint dy=A[i].second-A[i-1].second;\n\t\t\tif(dy==0)\n\t\t\t{\n\t\t\t\tX.push_back(A[i]);\n\t\t\t}\n\t\t\telse if(dy>0)\n\t\t\t{\n\t\t\t\tint lo=lower_bound(hs.begin(),hs.end(),A[i-1].second)-hs.begin();\n\t\t\t\tint hi=lower_bound(hs.begin(),hs.end(),A[i].second)-hs.begin();\n\t\t\t\tfor(int j=lo+1;j<=hi;j++)\n\t\t\t\t{\n\t\t\t\t\tint ny=hs[j]-A[i-1].second;\n\t\t\t\t\tX.push_back(make_pair(A[i-1].first+dx*ny/(double)dy,hs[j]));\n\t\t\t\t}\n\t\t\t}\n\t\t\telse\n\t\t\t{\n\t\t\t\tint hi=lower_bound(hs.begin(),hs.end(),A[i-1].second)-hs.begin();\n\t\t\t\tint lo=lower_bound(hs.begin(),hs.end(),A[i].second)-hs.begin();\n\t\t\t\tfor(int j=hi-1;j>=lo;j--)\n\t\t\t\t{\n\t\t\t\t\tint ny=hs[j]-A[i-1].second;\n\t\t\t\t\tX.push_back(make_pair(A[i-1].first+dx*ny/(double)dy,hs[j]));\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\tN=X.size();\n\t\tfor(int i=0;i<N;i++)for(int j=i;j<N;j++)dp[i][j]=1e150;\n\t\tdp[0][N-1]=0;\n\t\tpriority_queue<pair<double,pair<int,int> > >P;\n\t\tP.push(make_pair(0,make_pair(0,N-1)));\n\t\twhile(!P.empty())\n\t\t{\n\t\t\tint i=P.top().second.first,j=P.top().second.second;\n\t\t\tdouble cost=-P.top().first;\n\t\t\tP.pop();\n\t\t\tif(i==j)continue;\n\t\t\t//if(i==j||dp[i][j]<cost)continue;\n\t\t\tint dyi=X[i+1].second-X[i].second;\n\t\t\tint dyj=X[j-1].second-X[j].second;\n\t\t\tif(dyi==0)\n\t\t\t{\n\t\t\t\tdouble nxt=cost+X[i+1].first-X[i].first;\n\t\t\t\tif(dp[i+1][j]>nxt)\n\t\t\t\t{\n\t\t\t\t\tdp[i+1][j]=nxt;\n\t\t\t\t\tP.push(make_pair(-nxt,make_pair(i+1,j)));\n\t\t\t\t}\n\t\t\t}\n\t\t\tif(dyj==0)\n\t\t\t{\n\t\t\t\tdouble nxt=cost+X[j].first-X[j-1].first;\n\t\t\t\tif(dp[i][j-1]>nxt)\n\t\t\t\t{\n\t\t\t\t\tdp[i][j-1]=nxt;\n\t\t\t\t\tP.push(make_pair(-nxt,make_pair(i,j-1)));\n\t\t\t\t}\n\t\t\t}\n\t\t\tif(dyi==dyj)\n\t\t\t{\n\t\t\t\tdouble nxt=cost+hypot(X[i+1].first-X[i].first,dyi)+hypot(X[j].first-X[j-1].first,dyj);\n\t\t\t\tif(dp[i+1][j-1]>nxt)\n\t\t\t\t{\n\t\t\t\t\tdp[i+1][j-1]=nxt;\n\t\t\t\t\tP.push(make_pair(-nxt,make_pair(i+1,j-1)));\n\t\t\t\t}\n\t\t\t}\n\t\t\tif(j<N-1)\n\t\t\t{\n\t\t\t\tint ndyj=X[j+1].second-X[j].second;\n\t\t\t\tif(ndyj==0)\n\t\t\t\t{\n\t\t\t\t\tdouble nxt=cost+X[j+1].first-X[j].first;\n\t\t\t\t\tif(dp[i][j+1]>nxt)\n\t\t\t\t\t{\n\t\t\t\t\t\tdp[i][j+1]=nxt;\n\t\t\t\t\t\tP.push(make_pair(-nxt,make_pair(i,j+1)));\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\telse if(dyi==ndyj)\n\t\t\t\t{\n\t\t\t\t\tdouble nxt=cost+hypot(X[i+1].first-X[i].first,dyi)+hypot(X[j+1].first-X[j].first,ndyj);\n\t\t\t\t\tif(dp[i+1][j+1]>nxt)\n\t\t\t\t\t{\n\t\t\t\t\t\tdp[i+1][j+1]=nxt;\n\t\t\t\t\t\tP.push(make_pair(-nxt,make_pair(i+1,j+1)));\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t\tif(i>0)\n\t\t\t{\n\t\t\t\tint ndyi=X[i-1].second-X[i].second;\n\t\t\t\tif(ndyi==0)\n\t\t\t\t{\n\t\t\t\t\tdouble nxt=cost+X[i].first-X[i-1].first;\n\t\t\t\t\tif(dp[i-1][j]>nxt)\n\t\t\t\t\t{\n\t\t\t\t\t\tdp[i-1][j]=nxt;\n\t\t\t\t\t\tP.push(make_pair(-nxt,make_pair(i-1,j)));\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\telse if(ndyi==dyj)\n\t\t\t\t{\n\t\t\t\t\tdouble nxt=cost+hypot(X[i].first-X[i-1].first,ndyi)+hypot(X[j].first-X[j-1].first,dyj);\n\t\t\t\t\tif(dp[i-1][j-1]>nxt)\n\t\t\t\t\t{\n\t\t\t\t\t\tdp[i-1][j-1]=nxt;\n\t\t\t\t\t\tP.push(make_pair(-nxt,make_pair(i-1,j-1)));\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t\tif(i>0&&j<N-1)\n\t\t\t{\n\t\t\t\tint ndyi=X[i-1].second-X[i].second;\n\t\t\t\tint ndyj=X[j+1].second-X[j].second;\n\t\t\t\tif(ndyi==ndyj)\n\t\t\t\t{\n\t\t\t\t\tdouble nxt=cost+hypot(X[i].first-X[i-1].first,ndyi)+hypot(X[j+1].first-X[j].first,ndyj);\n\t\t\t\t\tif(dp[i-1][j+1]>nxt)\n\t\t\t\t\t{\n\t\t\t\t\t\tdp[i-1][j+1]=nxt;\n\t\t\t\t\t\tP.push(make_pair(-nxt,make_pair(i-1,j+1)));\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\tdouble ans=1e150;\n\t\tfor(int i=0;i<N;i++)ans=min(ans,dp[i][i]);\n\t\tcout<<fixed<<setprecision(16)<<ans<<endl;\n\t}\n}", "accuracy": 1, "time_ms": 530, "memory_kb": 116280, "score_of_the_acc": -1.0777, "final_rank": 19 }, { "submission_id": "aoj_1309_5035250", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\nusing ll = long long;\nusing PII = pair<ll, ll>;\n\n#define FOR(i, a, n) for (ll i = (ll)a; i < (ll)n; ++i)\n#define REP(i, n) FOR(i, 0, n)\n#define ALL(x) x.begin(), x.end()\n#define POPCOUNT(x) __builtin_popcount(x)\ntemplate <typename T> void chmin(T &a, const T &b) { a = min(a, b); }\ntemplate <typename T> void chmax(T &a, const T &b) { a = max(a, b); }\n\nconst ll INF = 1LL << 60;\n\nstruct FastIO {\n FastIO() {\n cin.tie(0);\n ios::sync_with_stdio(0);\n }\n} fastiofastio;\n\nusing State = tuple<double, int, int, int>;\nusing P = pair<int, double>;\n\ndouble dist[100][100][1000];\nint x[100], y[100];\nint N;\n\nint main() {\n while (1) {\n cin >> N;\n if (N == 0)\n break;\n for (int i = 0; i < N; i++)\n cin >> x[i] >> y[i];\n fill((double *)dist, (double *)(dist + N), 1e9);\n dist[0][N - 1][0] = 0;\n priority_queue<State, vector<State>, greater<State>> que;\n que.emplace(0, 0, N - 1, 0);\n while (!que.empty()) {\n State now = que.top();\n que.pop();\n double d = get<0>(now);\n int A = get<1>(now);\n int B = get<2>(now);\n int H = get<3>(now);\n if (dist[A][B][H] < d)\n continue;\n if (A == N - 1 || B == 0)\n continue;\n vector<P> upA;\n if (y[A] > H || y[A + 1] > H) {\n double X = x[A + 1] - x[A];\n double Y = abs(y[A + 1] - y[A]);\n if (y[A + 1] == H + 1)\n upA.emplace_back(A + 1, sqrt(1 + pow(X / Y, 2)));\n else\n upA.emplace_back(A, sqrt(1 + pow(X / Y, 2)));\n }\n if (A > 0 && y[A] == H && y[A - 1] > H) {\n double X = x[A] - x[A - 1];\n double Y = y[A] - y[A - 1];\n upA.emplace_back(A - 1, sqrt(1 + pow(X / Y, 2)));\n }\n vector<P> upB;\n if (y[B] > H || y[B - 1] > H) {\n double X = x[B] - x[B - 1];\n double Y = abs(y[B] - y[B - 1]);\n if (y[B - 1] == H + 1)\n upB.emplace_back(B - 1, sqrt(1 + pow(X / Y, 2)));\n else\n upB.emplace_back(B, sqrt(1 + pow(X / Y, 2)));\n }\n if (B + 1 < N && y[B] == H && y[B + 1] > H) {\n double X = x[B + 1] - x[B];\n double Y = y[B + 1] - y[B];\n upB.emplace_back(B + 1, sqrt(1 + pow(X / Y, 2)));\n }\n vector<P> downA;\n if (y[A] < H || y[A + 1] < H) {\n double X = x[A + 1] - x[A];\n double Y = abs(y[A + 1] - y[A]);\n if (y[A + 1] == H - 1)\n downA.emplace_back(A + 1, sqrt(1 + pow(X / Y, 2)));\n else\n downA.emplace_back(A, sqrt(1 + pow(X / Y, 2)));\n }\n if (A > 0 && y[A] == H && y[A - 1] < H) {\n double X = x[A] - x[A - 1];\n double Y = y[A] - y[A - 1];\n downA.emplace_back(A - 1, sqrt(1 + pow(X / Y, 2)));\n }\n vector<P> downB;\n if (y[B] < H || y[B - 1] < H) {\n double X = x[B] - x[B - 1];\n double Y = abs(y[B] - y[B - 1]);\n if (y[B - 1] == H - 1)\n downB.emplace_back(B - 1, sqrt(1 + pow(X / Y, 2)));\n else\n downB.emplace_back(B, sqrt(1 + pow(X / Y, 2)));\n }\n if (B + 1 < N && y[B] == H && y[B + 1] < H) {\n double X = x[B + 1] - x[B];\n double Y = y[B + 1] - y[B];\n downB.emplace_back(B + 1, sqrt(1 + pow(X / Y, 2)));\n }\n for (P p1 : upA) {\n for (P p2 : upB) {\n if (dist[p1.first][p2.first][H + 1] >\n d + p1.second + p2.second) {\n dist[p1.first][p2.first][H + 1] =\n d + p1.second + p2.second;\n que.emplace(dist[p1.first][p2.first][H + 1], p1.first,\n p2.first, H + 1);\n }\n }\n }\n for (P p1 : downA) {\n for (P p2 : downB) {\n if (dist[p1.first][p2.first][H - 1] >\n d + p1.second + p2.second) {\n dist[p1.first][p2.first][H - 1] =\n d + p1.second + p2.second;\n que.emplace(dist[p1.first][p2.first][H - 1], p1.first,\n p2.first, H - 1);\n }\n }\n }\n if (A > 0 && y[A] == H && y[A - 1] == H) {\n double len = x[A] - x[A - 1];\n if (dist[A - 1][B][H] > d + len) {\n dist[A - 1][B][H] = d + len;\n que.emplace(dist[A - 1][B][H], A - 1, B, H);\n }\n }\n if (y[A + 1] == H) {\n double len = x[A + 1] - x[A];\n if (dist[A + 1][B][H] > d + len) {\n dist[A + 1][B][H] = d + len;\n que.emplace(dist[A + 1][B][H], A + 1, B, H);\n }\n }\n if (B + 1 < N && y[B] == H && y[B + 1] == H) {\n double len = x[B + 1] - x[B];\n if (dist[A][B + 1][H] > d + len) {\n dist[A][B + 1][H] = d + len;\n que.emplace(dist[A][B + 1][H], A, B + 1, H);\n }\n }\n if (y[B - 1] == H) {\n double len = x[B] - x[B - 1];\n if (dist[A][B - 1][H] > d + len) {\n dist[A][B - 1][H] = d + len;\n que.emplace(dist[A][B - 1][H], A, B - 1, H);\n }\n }\n }\n double ans = 1e9;\n for (int i = 1; i + 1 < N; i++) {\n ans = min(ans, dist[i][i][y[i]]);\n }\n for (int i = 0; i + 1 < N; i++) {\n if (y[i] == y[i + 1]) {\n ans = min(ans, dist[i][i + 1][y[i]] + (x[i + 1] - x[i]));\n }\n }\n cout << fixed << setprecision(15) << ans << endl;\n }\n}", "accuracy": 1, "time_ms": 3640, "memory_kb": 81648, "score_of_the_acc": -1.2599, "final_rank": 20 }, { "submission_id": "aoj_1309_4889485", "code_snippet": "#include \"iostream\"\n#include \"climits\"\n#include \"list\"\n#include \"queue\"\n#include \"stack\"\n#include \"set\"\n#include \"functional\"\n#include \"algorithm\"\n#include \"string\"\n#include \"map\"\n#include \"unordered_map\"\n#include \"unordered_set\"\n#include \"iomanip\"\n#include \"cmath\"\n#include \"random\"\n#include \"bitset\"\n#include \"cstdio\"\n#include \"numeric\"\n#include \"cassert\"\n#include \"ctime\"\n\nusing namespace std;\n\nconstexpr long long int MOD = 1000000007;\n//constexpr int MOD = 1000000007;\n//constexpr int MOD = 998244353;\n//constexpr long long int MOD = 998244353;\nconstexpr double EPS = 1e-9;\n\n//int N, M, K, T, H, W, L, R;\nlong long int N, M, K, T, H, W, L, R;\n\nstruct Node {\n\tint l, r;\n\tlong double cost;\n\tNode(const int ll, const int rr, const long double cc) {\n\t\tl = ll, r = rr, cost = cc;\n\t}\n\tbool operator<(const Node&n)const {\n\t\treturn cost > n.cost;\n\t}\n};\n\nstruct Point {\n\tlong double x, y;\n\tPoint(const long double xx, const long double yy) {\n\t\tx = xx, y = yy;\n\t}\n\tbool operator<(const Point&p)const {\n\t\treturn x < p.x;\n\t}\n};\n\nvector<Point>func() {\n\tvector<long double>x(N);\n\tvector<long double>y(N);\n\tset<int>st;\n\tfor (int i = 0; i < N; i++) {\n\t\tcin >> x[i] >> y[i];\n\t\tst.insert(y[i]);\n\t}\n\tvector<Point>ret;\n\tfor (int i = 0; i < N; i++) {\n\t\tret.push_back(Point(x[i], y[i]));\n\t}\n\tfor (int i = 1; i < N; i++) {\n\t\tfor (auto j : st) {\n\t\t\tif (y[i] > j&&j > y[i - 1]) {\n\t\t\t\tret.push_back(Point(x[i] + (y[i] - j) / (y[i] - y[i - 1])*(x[i - 1] - x[i]), j));\n\t\t\t}\n\t\t\tif (y[i] < j&&j < y[i - 1]) {\n\t\t\t\tret.push_back(Point(x[i] + (y[i] - j) / (y[i] - y[i - 1])*(x[i - 1] - x[i]), j));\n\t\t\t}\n\t\t}\n\t}\n\tsort(ret.begin(), ret.end());\n\treturn ret;\n}\n\nint main() {\n\tios::sync_with_stdio(false);\n\tcin.tie(0);\n\n\twhile (cin >> N, N) {\n\t\tauto p = func();\n\t\tlong double ans = MOD;\n\t\t//vector<vector<long double>>dp(p.size(), vector<long double>(p.size(), MOD));\n\t\tvector<unordered_map<int, long double>>dp(p.size());\n\t\tpriority_queue<Node>PQ;\n\t\tPQ.push(Node(0, p.size() - 1, 0));\n\t\twhile (!PQ.empty()) {\n\t\t\tauto box = PQ.top();\n\t\t\tPQ.pop();\n\t\t\tfor (int i = box.l - 1; i <= box.l + 1; i++) {\n\t\t\t\tif (i < 0 || i >= p.size())continue;\n\t\t\t\tfor (int j = box.r - 1; j <= box.r + 1; j++) {\n\t\t\t\t\tif (j < 0 || j >= p.size())continue;\n\t\t\t\t\tif (p[i].y != p[j].y)continue;\n\t\t\t\t\tlong double add = hypot(p[i].x - p[box.l].x, p[i].y - p[box.l].y) + hypot(p[j].x - p[box.r].x, p[j].y - p[box.r].y);\n\t\t\t\t\tif (dp[i].find(j) == dp[i].end()) {\n\t\t\t\t\t\tdp[i][j] = box.cost + add;\n\t\t\t\t\t\tPQ.push(Node(i, j, dp[i][j]));\n\t\t\t\t\t}\n\t\t\t\t\telse if (dp[i][j] > box.cost + add) {\n\t\t\t\t\t\tdp[i][j] = box.cost + add;\n\t\t\t\t\t\tPQ.push(Node(i, j, dp[i][j]));\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\tfor (int i = 0; i < dp.size(); i++) {\n\t\t\tif(dp[i].find(i)!=dp[i].end())ans = min(ans, dp[i][i]);\n\t\t}\n\t\tcout << fixed << setprecision(20) << ans << endl;\n\t}\n}", "accuracy": 1, "time_ms": 1410, "memory_kb": 10512, "score_of_the_acc": -0.2685, "final_rank": 14 }, { "submission_id": "aoj_1309_4834746", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nint main(){\n cout << fixed << setprecision(10);\n while (1){\n int N;\n cin >> N;\n if (N == 0){\n break;\n }\n vector<int> x(N), y(N);\n for (int i = 0; i < N; i++){\n cin >> x[i] >> y[i];\n }\n vector<int> h = y;\n sort(h.begin(), h.end());\n h.erase(unique(h.begin(), h.end()), h.end());\n vector<double> x2;\n vector<int> y2;\n x2.push_back(x[0]);\n y2.push_back(y[0]);\n int M = 1;\n for (int i = 1; i < N; i++){\n auto itr1 = lower_bound(h.begin(), h.end(), y[i - 1]);\n auto itr2 = lower_bound(h.begin(), h.end(), y[i]);\n if (itr1 < itr2){\n itr1++;\n while (itr1 < itr2){\n x2.push_back(x[i - 1] + (double) (x[i] - x[i - 1]) * (*itr1 - y[i - 1]) / (y[i] - y[i - 1]));\n y2.push_back(*itr1);\n M++;\n itr1++;\n }\n }\n if (itr1 > itr2){\n itr1--;\n while (itr1 > itr2){\n x2.push_back(x[i - 1] + (double) (x[i] - x[i - 1]) * (y[i - 1] - *itr1) / (y[i - 1] - y[i]));\n y2.push_back(*itr1);\n M++;\n itr1--;\n }\n }\n x2.push_back(x[i]);\n y2.push_back(y[i]);\n M++;\n }\n vector<double> d(M - 1);\n for (int i = 0; i < M - 1; i++){\n d[i] = sqrt(pow(x2[i + 1] - x2[i], 2) + pow(y2[i + 1] - y2[i], 2));\n }\n vector<vector<bool>> used(M, vector<bool>(M, false));\n priority_queue<pair<double, pair<int, int>>, vector<pair<double, pair<int, int>>>, greater<pair<double, pair<int, int>>>> pq;\n pq.push(make_pair(0, make_pair(0, M - 1)));\n double ans = -1;\n while (!pq.empty()){\n double s = pq.top().first;\n int a = pq.top().second.first;\n int b = pq.top().second.second;\n pq.pop();\n if (a == b){\n ans = s;\n break;\n }\n if (!used[a][b]){\n used[a][b] = true;\n for (int da = -1; da <= 1; da++){\n for (int db = -1; db <= 1; db++){\n int a2 = a + da;\n int b2 = b + db;\n if (0 <= a2 && a2 < M && 0 <= b2 && b2 < M){\n if (y2[a2] == y2[b2] && !used[a2][b2]){\n double s2 = s;\n if (a != a2){\n s2 += d[min(a, a2)];\n }\n if (b != b2){\n s2 += d[min(b, b2)];\n }\n pq.push(make_pair(s2, make_pair(a2, b2)));\n }\n }\n }\n }\n }\n }\n cout << ans << endl;\n }\n}", "accuracy": 1, "time_ms": 40, "memory_kb": 6428, "score_of_the_acc": -0.0147, "final_rank": 4 }, { "submission_id": "aoj_1309_4811307", "code_snippet": "#include <bits/stdc++.h>\n\nusing namespace std;\n\n#define SZ(x) (int)(x.size())\n#define REP(i, n) for(int i=0;i<(n);++i)\n#define FOR(i, a, b) for(int i=(a);i<(b);++i)\n#define RREP(i, n) for(int i=(int)(n);i>=0;--i)\n#define RFOR(i, a, b) for(int i=(int)(a);i>=(int)(b);--i)\n#define ALL(a) (a).begin(),(a).end()\n#define DUMP(x) cerr<<#x<<\" = \"<<(x)<<endl\n#define DEBUG(x) cerr<<#x<<\" = \"<<(x)<<\" (L\"<<__LINE__<<\")\"<< endl;\n\nusing ll = long long;\nusing vi = vector<int>;\nusing vvi = vector<vi>;\nusing vll = vector<ll>;\nusing vvll = vector<vll>;\nusing P = pair<int, int>;\n\nconst double eps = 1e-8;\nconst ll MOD = 1000000007;\nconst int INF = INT_MAX / 2;\nconst ll LINF = LLONG_MAX / 2;\n\ntemplate <typename T1, typename T2>\nbool chmax(T1 &a, const T2 &b) {\n if(a < b) {a = b; return true;}\n return false;\n}\n\ntemplate <typename T1, typename T2>\nbool chmin(T1 &a, const T2 &b) {\n if(a > b) {a = b; return true;}\n return false;\n}\n\ntemplate<typename T1, typename T2>\nostream& operator<<(ostream &os, const pair<T1, T2> p) {\n os << p.first << \":\" << p.second;\n return os;\n}\n\ntemplate<class T>\nostream &operator<<(ostream &os, const vector<T> &v) {\n REP(i, SZ(v)) {\n if(i) os << \" \";\n os << v[i];\n }\n return os;\n}\n\ndouble cross(double x1, double y1, double x2, double y2, double y) {\n if(abs(y1 - y) < eps || abs(y2 - y) < eps) return -1;\n if(!((y1 < y && y < y2) || (y2 < y && y < y1))) {\n return -1;\n }\n\n return x1 + (x2 - x1) * (y - y1) / (y2 - y1);\n}\n\ndouble kyori(pair<double, double> p1, pair<double, double> p2) {\n return sqrt((p1.first - p2.first) * (p1.first - p2.first) + (p1.second - p2.second) * (p1.second - p2.second));\n}\n\nbool solve() {\n int n; cin >> n;\n if(n == 0) return false;\n vi x(n), y(n);\n int ma = 0;\n set<int> sty;\n REP(i, n) {\n cin >> x[i] >> y[i];\n chmax(ma, y[i]);\n sty.insert(y[i]);\n }\n\n\n vector<pair<double, double>> v;\n REP(i, n) {\n v.emplace_back(x[i], y[i]);\n }\n for(auto &e: sty) {\n REP(i, n-1) {\n double nowx = cross(x[i], y[i], x[i+1], y[i+1], e);\n if(abs(nowx + 1) < eps) continue;\n v.emplace_back(nowx, e);\n }\n }\n sty.clear();\n sort(ALL(v));\n int sz = SZ(v);\n vector<map<int, double>> d(sz);\n using elm = pair<double, pair<int, int>>;\n priority_queue<elm, vector<elm>, greater<elm>> pq;\n\n d[0][sz-1] = 0.0;\n pq.push({0.0, {0, sz-1}});\n double ans = INF;\n while(!pq.empty()) {\n auto now = pq.top(); pq.pop();\n double dist = now.first;\n int i1 = now.second.first;\n int i2 = now.second.second;\n if(i1 == i2) {\n ans = dist;\n break;\n }\n\n //cout << d[i1][i2] << \" \" << v[i1] << \" \" << v[i2] << endl;\n\n double nowy = v[i1].second;\n\n vector<bool> same(4);\n\n if(i1 != 0 && abs(nowy - v[i1-1].second) < eps) {\n if(d[i1-1].find(i2) == d[i1-1].end() || d[i1-1][i2] > d[i1][i2] + kyori(v[i1-1], v[i1])) {\n d[i1-1][i2] = d[i1][i2] + kyori(v[i1-1], v[i1]);\n pq.push({d[i1-1][i2], {i1-1, i2}});\n }\n same[0] = true;\n }\n\n if(i1 != sz-1 && abs(nowy - v[i1+1].second) < eps) {\n if(d[i1+1].find(i2) == d[i1+1].end() || d[i1+1][i2] > d[i1][i2] + kyori(v[i1+1], v[i1])) {\n d[i1+1][i2] = d[i1][i2] + kyori(v[i1+1], v[i1]);\n pq.push({d[i1+1][i2], {i1+1, i2}});\n }\n same[1] = true;\n }\n\n if(i1 != 0 && abs(nowy - v[i2-1].second) < eps) {\n if(d[i1].find(i2-1) == d[i1].end() || d[i1][i2-1] > d[i1][i2] + kyori(v[i2-1], v[i2])) {\n d[i1][i2-1] = d[i1][i2] + kyori(v[i2-1], v[i2]);\n pq.push({d[i1][i2-1], {i1, i2-1}});\n }\n same[2] = true;\n }\n\n if(i1 != 0 && abs(nowy - v[i2+1].second) < eps) {\n if(d[i1].find(i2+1) == d[i1].end() || d[i1][i2+1] > d[i1][i2] + kyori(v[i2+1], v[i2])) {\n d[i1][i2+1] = d[i1][i2] + kyori(v[i2+1], v[i2]);\n pq.push({d[i1][i2+1], {i1, i2+1}});\n }\n same[3] = true;\n }\n\n\n\n if(i1 != 0 && i2 != 0 && v[i1-1].second < nowy == v[i2-1].second < nowy && !same[0] && !same[2]) {\n double diff = 0.0;\n diff += kyori(v[i1], v[i1-1]);\n diff += kyori(v[i2], v[i2-1]);\n if(d[i1-1].find(i2-1) == d[i1-1].end() || d[i1-1][i2-1] > d[i1][i2] + diff) {\n d[i1-1][i2-1] = d[i1][i2] + diff;\n pq.push({d[i1-1][i2-1], {i1-1, i2-1}});\n }\n }\n\n if(i1 != 0 && i2 != sz-1 && v[i1-1].second < nowy == v[i2+1].second < nowy && !same[0] && !same[3]) {\n double diff = 0.0;\n diff += kyori(v[i1], v[i1-1]);\n diff += kyori(v[i2], v[i2+1]);\n if(d[i1-1].find(i2+1) == d[i1-1].end() || d[i1-1][i2+1] > d[i1][i2] + diff) {\n d[i1-1][i2+1] = d[i1][i2] + diff;\n pq.push({d[i1-1][i2+1], {i1-1, i2+1}});\n }\n }\n\n if(i1 != sz-1 && i2 != 0 && v[i1+1].second < nowy == v[i2-1].second < nowy && !same[1] && !same[2] ) {\n double diff = 0.0;\n diff += kyori(v[i1], v[i1+1]);\n diff += kyori(v[i2], v[i2-1]);\n if(d[i1+1].find(i2-1) == d[i1+1].end() || d[i1+1][i2-1] > d[i1][i2] + diff) {\n d[i1+1][i2-1] = d[i1][i2] + diff;\n pq.push({d[i1+1][i2-1], {i1+1, i2-1}});\n }\n }\n\n if(i1 != sz-1 && i2 != sz-1 && v[i1+1].second < nowy == v[i2+1].second < nowy && !same[1] && !same[3]) {\n double diff = 0.0;\n diff += kyori(v[i1], v[i1+1]);\n diff += kyori(v[i2], v[i2+1]);\n if(d[i1+1].find(i2+1) == d[i1+1].end() || d[i1+1][i2+1] > d[i1][i2] + diff) {\n d[i1+1][i2+1] = d[i1][i2] + diff;\n pq.push({d[i1+1][i2+1], {i1+1, i2+1}});\n }\n }\n }\n\n /*\n double ans = INF;\n REP(i, sz) {\n if(abs(d[i][i] - INF) > eps) chmin(ans, d[i][i]);\n }\n */\n cout << ans << endl;\n\n return true;\n}\n\nint main() {\n cin.tie(0);\n ios::sync_with_stdio(false);\n cout << fixed << setprecision(10);\n\n while(solve()) {}\n\n return 0;\n}", "accuracy": 1, "time_ms": 120, "memory_kb": 5860, "score_of_the_acc": -0.0223, "final_rank": 5 }, { "submission_id": "aoj_1309_4796501", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\ntypedef long long ll;\ntemplate<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return true; } return false; }\ntemplate<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return true; } return false; }\n#define all(x) (x).begin(),(x).end()\n#define fi first\n#define se second\n#define mp make_pair\n#define si(x) int(x.size())\nconst int mod=1000000007,MAX=5005,INF=1<<30;\nint id=0;\ndouble x[MAX],y[MAX],dis[MAX][MAX];\n\nvoid dijkstra(){\n for(int i=0;i<id;i++) for(int j=i;j<id;j++) dis[i][j]=INF;\n dis[0][id-1]=0;\n priority_queue<pair<double,pair<int,int>>,vector<pair<double,pair<int,int>>>,greater<pair<double,pair<int,int>>>> PQ;\n PQ.push(mp(0,mp(0,id-1)));\n \n while(!PQ.empty()){\n auto u=PQ.top();PQ.pop();\n if(u.se.fi==u.se.se) break;\n \n double d=dis[u.se.fi][u.se.se];\n \n if(u.fi>d) continue;\n \n for(int a=-1;a<=1;a++){\n for(int b=-1;b<=1;b++){\n if(a==0&&b==0) continue;\n \n if(u.se.fi+a<0||u.se.fi+a>=id||u.se.se+b<0||u.se.se+b>=id) continue;\n \n int toa=u.se.fi+a,tob=u.se.se+b;\n \n if(y[toa]==y[tob]){\n if(chmin(dis[toa][tob],d+hypot(x[toa-a]-x[toa],y[toa-a]-y[toa])+hypot(x[tob-b]-x[tob],y[tob-b]-y[tob]))){\n PQ.push(mp(dis[toa][tob],mp(toa,tob)));\n }\n }\n }\n }\n }\n \n}\n\nint main(){\n \n std::ifstream in(\"text.txt\");\n std::cin.rdbuf(in.rdbuf());\n cin.tie(0);\n ios::sync_with_stdio(false);\n \n while(1){\n int N;cin>>N;\n if(N==0) break;\n memset(x,0,sizeof(x));\n memset(y,0,sizeof(y));\n vector<double> px,py;\n set<double> se,se2;\n for(int i=0;i<N;i++){\n double a,b;cin>>a>>b;\n px.push_back(a);\n py.push_back(b);\n se.insert(b);\n se2.insert(-b);\n }\n \n if(N==2){\n cout<<px[1]<<endl;\n continue;\n }\n \n id=0;\n \n for(int i=0;i+1<N;i++){\n if(py[i]<py[i+1]){\n for(double yy:se){\n if(py[i]<=yy&&yy<=py[i+1]){\n if(yy==py[i+1]&&i+1!=N-1) continue;\n double d=(abs(yy-py[i])/abs(py[i+1]-py[i])*(px[i+1]-px[i])+px[i]);\n x[id]=d;\n y[id]=yy;\n id++;\n }\n }\n }else if(py[i]==py[i+1]){\n x[id]=px[i];\n y[id]=py[i];\n id++;\n \n if(i+1==N-1){\n x[id]=px[i+1];\n y[id]=py[i+1];\n id++;\n }\n }else{\n for(double yy2:se2){\n double yy=-yy2;\n if(py[i]>=yy&&yy>=py[i+1]){\n if(yy==py[i+1]&&i+1!=N-1) continue;\n double d=(abs(yy-py[i])/abs(py[i+1]-py[i])*(px[i+1]-px[i])+px[i]);\n x[id]=d;\n y[id]=yy;\n id++;\n }\n }\n }\n }\n \n dijkstra();\n \n double ans=INF;\n for(int i=0;i<id;i++) chmin(ans,dis[i][i]);\n \n cout<<setprecision(25)<<ans<<endl;\n }\n \n}", "accuracy": 1, "time_ms": 530, "memory_kb": 116016, "score_of_the_acc": -1.0753, "final_rank": 17 }, { "submission_id": "aoj_1309_4796411", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\ntypedef long long ll;\ntemplate<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return true; } return false; }\ntemplate<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return true; } return false; }\n#define all(x) (x).begin(),(x).end()\n#define fi first\n#define se second\n#define mp make_pair\n#define si(x) int(x.size())\nconst int mod=1000000007,MAX=5005,INF=1<<30;\nint id=0;\ndouble x[MAX],y[MAX],dis[MAX][MAX];\n\nvoid dijkstra(){\n for(int i=0;i<id;i++) for(int j=0;j<=i;j++) dis[i][j]=INF;\n dis[id-1][0]=0;\n priority_queue<pair<double,pair<int,int>>,vector<pair<double,pair<int,int>>>,greater<pair<double,pair<int,int>>>> PQ;\n PQ.push(mp(0,mp(0,id-1)));\n \n while(!PQ.empty()){\n auto u=PQ.top();PQ.pop();\n if(u.se.fi==u.se.se) break;\n \n double d=dis[u.se.se][u.se.fi];\n \n if(u.fi>d) continue;\n \n for(int a=-1;a<=1;a++){\n for(int b=-1;b<=1;b++){\n if(a==0&&b==0) continue;\n \n if(u.se.fi+a<0||u.se.fi+a>=id||u.se.se+b<0||u.se.se+b>=id) continue;\n \n int toa=u.se.fi+a,tob=u.se.se+b;\n \n if(y[toa]==y[tob]){\n if(chmin(dis[tob][toa],d+hypot(x[toa-a]-x[toa],y[toa-a]-y[toa])+hypot(x[tob-b]-x[tob],y[tob-b]-y[tob]))){\n PQ.push(mp(dis[tob][toa],mp(toa,tob)));\n }\n }\n }\n }\n }\n \n}\n\nint main(){\n \n std::ifstream in(\"text.txt\");\n std::cin.rdbuf(in.rdbuf());\n cin.tie(0);\n ios::sync_with_stdio(false);\n \n while(1){\n int N;cin>>N;\n if(N==0) break;\n memset(x,0,sizeof(x));\n memset(y,0,sizeof(y));\n vector<double> px,py;\n set<double> se,se2;\n for(int i=0;i<N;i++){\n double a,b;cin>>a>>b;\n px.push_back(a);\n py.push_back(b);\n se.insert(b);\n se2.insert(-b);\n }\n \n if(N==2){\n cout<<px[1]<<endl;\n continue;\n }\n \n id=0;\n \n for(int i=0;i+1<N;i++){\n if(py[i]<py[i+1]){\n for(double yy:se){\n if(py[i]<=yy&&yy<=py[i+1]){\n if(yy==py[i+1]&&i+1!=N-1) continue;\n double d=(abs(yy-py[i])/abs(py[i+1]-py[i])*(px[i+1]-px[i])+px[i]);\n x[id]=d;\n y[id]=yy;\n id++;\n }\n }\n }else if(py[i]==py[i+1]){\n x[id]=px[i];\n y[id]=py[i];\n id++;\n \n if(i+1==N-1){\n x[id]=px[i+1];\n y[id]=py[i+1];\n id++;\n }\n }else{\n for(double yy2:se2){\n double yy=-yy2;\n if(py[i]>=yy&&yy>=py[i+1]){\n if(yy==py[i+1]&&i+1!=N-1) continue;\n double d=(abs(yy-py[i])/abs(py[i+1]-py[i])*(px[i+1]-px[i])+px[i]);\n x[id]=d;\n y[id]=yy;\n id++;\n }\n }\n }\n }\n \n dijkstra();\n \n double ans=INF;\n for(int i=0;i<id;i++) chmin(ans,dis[i][i]);\n \n cout<<setprecision(25)<<ans<<endl;\n }\n \n}", "accuracy": 1, "time_ms": 520, "memory_kb": 116252, "score_of_the_acc": -1.0758, "final_rank": 18 } ]
aoj_1306_cpp
Problem B: Balloon Collecting "Balloons should be captured efficiently", the game designer says. He is designing an oldfashioned game with two dimensional graphics. In the game, balloons fall onto the ground one after another, and the player manipulates a robot vehicle on the ground to capture the balloons. The player can control the vehicle to move left or right, or simply stay. When one of the balloons reaches the ground, the vehicle and the balloon must reside at the same position, otherwise the balloon will burst and the game ends. Figure B.1: Robot vehicle and falling balloons The goal of the game is to store all the balloons into the house at the left end on the game field. The vehicle can carry at most three balloons at a time, but its speed changes according to the number of the carrying balloons. When the vehicle carries k balloons ( k = 0, 1, 2, 3), it takes k +1 units of time to move one unit distance. The player will get higher score when the total moving distance of the vehicle is shorter. Your mission is to help the game designer check game data consisting of a set of balloons. Given a landing position (as the distance from the house) and a landing time of each balloon, you must judge whether a player can capture all the balloons, and answer the minimum moving distance needed to capture and store all the balloons. The vehicle starts from the house. If the player cannot capture all the balloons, you must identify the first balloon that the player cannot capture. Input The input is a sequence of datasets. Each dataset is formatted as follows. n p 1 t 1 . . . p n t n The first line contains an integer n, which represents the number of balloons (0 < n ≤ 40). Each of the following n lines contains two integers p i and t i (1 ≤ i ≤ n ) separated by a space. p i and t i represent the position and the time when the i -th balloon reaches the ground (0 < p i ≤ 100, 0 < t i ≤ 50000). You can assume t i < t j for i < j . The position of the house is 0, and the game starts from the time 0. The sizes of the vehicle, the house, and the balloons are small enough, and should be ignored. The vehicle needs 0 time for catching the balloons or storing them into the house. The vehicle can start moving immediately after these operations. The end of the input is indicated by a line containing a zero. Output For each dataset, output one word and one integer in a line separated by a space. No extra characters should occur in the output. If the player can capture all the balloons, output "OK" and an integer that represents the minimum moving distance of the vehicle to capture and store all the balloons. If it is impossible for the player to capture all the balloons, output "NG" and an integer k such that the k -th balloon in the dataset is the first balloon that the player cannot capture. Sample Input 2 10 100 100 270 2 10 100 100 280 3 100 150 10 360 40 450 3 100 150 10 360 40 440 2 100 10 50 200 2 100 100 50 110 1 15 10 4 1 10 2 20 3 100 90 200 0 Output for ...(truncated)
[ { "submission_id": "aoj_1306_6728978", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\n\narray<array<array<int,4>,101>,50501> dp;\n\nint main(){\n int n, INF = 1 << 30, r = 50500;\n while(cin >> n, n){\n int ans = -1;\n for(int t = 0; t <= r; t++){\n for(int p = 0; p <= 100; p++){\n for(int j = 0; j < 4; j++){\n dp[t][p][j] = INF;\n }\n }\n }\n vector<array<int,2>> bl(n);\n for(int i = 0; i < n; i++){\n cin >> bl[i][0] >> bl[i][1];\n }\n bool flag = true;\n dp[0][0][0] = 0;\n for(int t = 0, bi = 0; t < r; t++){\n if(bi < n && t == bl[bi][1]){\n int pos = bl[bi][0];\n for(int nt = t; nt <= t + 3 && nt <= r; nt++){\n for(int p = 0; p <= 100; p++){\n if(nt == t && p == pos)continue;\n for(int j = 0; j < 4; j++){\n dp[nt][p][j] = INF;\n }\n }\n }\n for(int j = 2; j >= 0; j--)dp[t][pos][j+1] = dp[t][pos][j];\n dp[t][pos][0] = INF;\n int minv = *min_element(dp[t][pos].begin(), dp[t][pos].end());\n if(minv == INF){\n flag = false;\n cout << \"NG \" << bi + 1 << '\\n';\n break;\n }\n bi++;\n }\n dp[t][0][0] = *min_element(dp[t][0].begin(), dp[t][0].end());\n for(int p = 0; p <= 100; p++){\n for(int j = 0; j <= 3 && t + j + 1 <= r; j++){\n dp[t+1][p][j] = min(dp[t+1][p][j], dp[t][p][j]);\n if(p >= 1){\n dp[t+j+1][p-1][j] = min(dp[t+j+1][p-1][j], dp[t][p][j] + 1);\n }\n if(p + 1 <= 100){\n dp[t+j+1][p+1][j] = min(dp[t+j+1][p+1][j], dp[t][p][j] + 1);\n }\n }\n }\n }\n if(flag){\n cout << \"OK \" << *min_element(dp[r][0].begin(), dp[r][0].end()) << '\\n';\n }\n }\n}", "accuracy": 1, "time_ms": 1380, "memory_kb": 83024, "score_of_the_acc": -1.2997, "final_rank": 17 }, { "submission_id": "aoj_1306_4811352", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n#define modulo 1000000007\n#define mod(mod_x) ((((long long)mod_x+modulo))%modulo)\n#define Inf 100000001\n\n\n\nint main(){\n\n\twhile(true){\n\t\tint n;\n\t\tcin>>n;\n\t\tif(n==0)break;\n\t\tvector<int> p(n+1,0),t(n+1,0);\n\t\tfor(int i=1;i<=n;i++)cin>>p[i]>>t[i];\n\t\t\n\t\tvector<vector<vector<int>>> dp(n+1,vector<vector<int>>(4,vector<int>(10000,Inf)));\n\t\tdp[0][0][0] = 0;\n\t\tstring S = \"\";\n\t\tint ans = -1;\n\t\tfor(int i=0;i<n;i++){\n\t\t\tint T = t[i+1] - t[i];\n\t\t\tint P = abs(p[i+1] - p[i]);\n\t\t\tfor(int j=0;j<=3;j++){\n\t\t\t\tfor(int x=0;x<10000;x++){\n\t\t\t\t\tif(dp[i][j][x]==Inf)continue;\n\t\t\t\t\tint k = (j+1);\n\t\t\t\t\tif(x+p[i]+p[i+1]<10000)dp[i+1][1][x+p[i]+p[i+1]] = min(dp[i+1][1][x+p[i]+p[i+1]],dp[i][j][x]+p[i]*k + p[i+1]);\n\t\t\t\t\tif(j!=3){\n\t\t\t\t\t\tif(x+P<10000)dp[i+1][j+1][x+P] = min(dp[i+1][j+1][x+P],dp[i][j][x]+P*k);\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t\tS = \"NG\";\n\t\t\tfor(int j=0;j<=3;j++){\n\t\t\t\tfor(int k=0;k<10000;k++){\n\t\t\t\t\tif(dp[i+1][j][k]<=t[i+1]){\n\t\t\t\t\t\tS = \"\";\n\t\t\t\t\t\tdp[i+1][j][k] = t[i+1];\n\t\t\t\t\t}\n\t\t\t\t\telse dp[i+1][j][k] = Inf;\n\t\t\t\t}\n\t\t\t}\n\t\t\tif(S==\"NG\"){\n\t\t\t\tans = i+1;\n\t\t\t\tbreak;\n\t\t\t}\n\t\t}\n\t\tif(S!=\"NG\"){\n\t\t\tS = \"OK\";\n\t\t\tans = Inf;\n\t\t\tfor(int i=0;i<10000;i++){\n\t\t\t\tfor(int j=0;j<=3;j++){\n\t\t\t\t\tif(dp.back()[j][i] <= t.back()){\n\t\t\t\t\t\tans = min(ans,i + p.back());\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\tcout<<S<<' '<<ans<<endl;\n\t}\n\t\n\treturn 0;\n}", "accuracy": 1, "time_ms": 70, "memory_kb": 9608, "score_of_the_acc": -0.0992, "final_rank": 5 }, { "submission_id": "aoj_1306_4260605", "code_snippet": "#include<iostream>\n#include<vector>\n#include<algorithm>\n#include<cmath>\n#include<ctime>\n\nusing namespace std;\nusing ll = long long;\n\n#define INF 1e9\n\nint main(){\n cin.tie(0);\n ios::sync_with_stdio(false);\n int ti = clock();\n // start-----------------------------------------------\n int n;\n while(cin >> n && n){\n vector<vector<vector<int>>> dp(n+1, vector<vector<int>>(50001, vector<int>(4, INF)));\n vector<int> point(n+1);\n vector<int> times(n+1);\n point[0] = 0;\n times[0] = 0;\n dp[0][0][0] = 0;\n int i, p, t;\n bool flag;\n for(i = 0; i < n; i++){\n cin >> p >> t;\n point[i+1] = p;\n times[i+1] = t;\n flag = false;\n for(int j = 0; j < 4; j++){\n if(times[i] + point[i]*(j+1) + p <= t && dp[i][times[i]][j] != INF){\n flag = true;\n dp[i+1][t][1] = min(dp[i+1][t][1], dp[i][times[i]][j] + point[i] + p);\n }\n }\n for(int j = 0; j < 3; j++){\n if(times[i] + abs(p - point[i]) * (j+1) <= t && dp[i][times[i]][j] != INF){\n flag = true;\n dp[i+1][t][j+1] = min(dp[i+1][t][j+1], dp[i][times[i]][j] + abs(p - point[i]));\n }\n }\n if(!flag) break;\n }\n\n if(!flag){\n i++;\n cout << \"NG\" << ' ' << i << endl;\n for(i; i < n; i++) cin >> p >> t;\n }\n else{\n int ans = dp[n][times[n]][0];\n for(i = 1; i < 4; i++) ans = min(ans, dp[n][times[n]][i] + point[n]);\n cout << \"OK\" << ' ' << ans << endl;\n }\n }\n // end-----------------------------------------------\n // cerr << 1.0 * (clock() - ti) / CLOCKS_PER_SEC << endl;\n}", "accuracy": 1, "time_ms": 2300, "memory_kb": 117760, "score_of_the_acc": -2, "final_rank": 19 }, { "submission_id": "aoj_1306_4208993", "code_snippet": "#define _CRT_SECURE_NO_WARNINGS\n#pragma comment (linker, \"/STACK:526000000\")\n\n#include \"bits/stdc++.h\"\n\nusing namespace std;\ntypedef string::const_iterator State;\n#define eps 1e-11L\n#define MAX_MOD 1000000007LL\n#define GYAKU 500000004LL\n\n#define MOD 998244353LL\n#define seg_size 262144*2LL\n#define pb push_back\n#define mp make_pair\ntypedef long long ll;\n#define REP(a,b) for(long long (a) = 0;(a) < (b);++(a))\n#define ALL(x) (x).begin(),(x).end()\n\nunsigned long xor128() {\n\tstatic unsigned long x = 123456789, y = 362436069, z = 521288629, w = time(NULL);\n\tunsigned long t = (x ^ (x << 11));\n\tx = y; y = z; z = w;\n\treturn (w = (w ^ (w >> 19)) ^ (t ^ (t >> 8)));\n}\n\nvoid init() {\n\tiostream::sync_with_stdio(false);\n\tcout << fixed << setprecision(20);\n}\n\nint dp[4][101][51001];\n\nvoid solve(){\n\twhile (true) {\n\t\tint n;\n\t\tcin >> n;\n\t\tif (n == 0) return;\n\t\tvector<pair<int, int>> inputs;\n\t\tREP(i, n) {\n\t\t\tint a, b;\n\t\t\tcin >> a >> b;\n\t\t\tinputs.push_back(mp(a, b));\n\t\t}\n\t\tinputs.push_back(mp(0, 1e9));\n\t\treverse(ALL(inputs));\n\t\tREP(t, 4) {\n\t\t\tREP(i, 101) {\n\t\t\t\tREP(q, 51001) {\n\t\t\t\t\tdp[t][i][q] = 1e9;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\tdp[0][0][0] = 0;\n\t\tint ans = -1;\n\t\tint tmp = 1;\n\t\tfor (int i = 0; i <= 50500; ++i) {\n\t\t\tif (inputs.back().second == i) {\n\t\t\t\tfor (int t = 1; t <= 3; ++t) {\n\t\t\t\t\tREP(q, 101) {\n\t\t\t\t\t\tREP(j, 4) {\n\t\t\t\t\t\t\tdp[j][q][i + t] = 1e9;\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\tREP(q, 101) {\n\t\t\t\t\tREP(j, 4) {\n\t\t\t\t\t\tif (q != inputs.back().first) {\n\t\t\t\t\t\t\tdp[j][q][i] = 1e9;\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\tfor (int j = 3; j >= 1;--j) {\n\t\t\t\t\tswap(dp[j][inputs.back().first][i], dp[j - 1][inputs.back().first][i]);\n\t\t\t\t}\n\t\t\t\tdp[0][inputs.back().first][i] = 1e9;\n\t\t\t\tint ok = 0;\n\t\t\t\tREP(j, 4) {\n\t\t\t\t\tif (dp[j][inputs.back().first][i] != 1e9) ok = 1;\n\t\t\t\t}\n\t\t\t\t\n\t\t\t\tif (ok == 0) {\n\t\t\t\t\tans = tmp;\n\t\t\t\t\tbreak;\n\t\t\t\t}\n\t\t\t\ttmp++;\n\t\t\t\tinputs.pop_back();\n\t\t\t}\n\t\t\tREP(t, 4) {\n\t\t\t\tfor (int q = 0; q < 101; ++q) {\n\t\t\t\t\tif (q != 100) {\n\t\t\t\t\t\tdp[t][q + 1][i + t + 1] = min(dp[t][q + 1][i + t + 1], dp[t][q][i] + 1);\n\t\t\t\t\t}\n\t\t\t\t\tif (q != 0) {\n\t\t\t\t\t\tif(q == 1){\n\t\t\t\t\t\t\tdp[0][q - 1][i + t + 1] = min(dp[0][q - 1][i + t + 1], dp[t][q][i] + 1);\n\t\t\t\t\t\t}\n\t\t\t\t\t\tdp[t][q - 1][i + t + 1] = min(dp[t][q - 1][i + t + 1], dp[t][q][i] + 1);\n\t\t\t\t\t}\n\t\t\t\t\tdp[t][q][i + 1] = min(dp[t][q][i + 1], dp[t][q][i]);\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\tif (ans != -1) {\n\t\t\tcout << \"NG \" << ans << endl;\n\t\t\tcontinue;\n\t\t}\n\t\tcout << \"OK \" << dp[0][0][50500] << endl;\n\t}\n}\n\n#undef int\nint main() {\n\tinit();\n\tsolve();\n}", "accuracy": 1, "time_ms": 2130, "memory_kb": 83632, "score_of_the_acc": -1.6353, "final_rank": 18 }, { "submission_id": "aoj_1306_3693534", "code_snippet": "#include <iostream>\n#include <vector>\n#include <cmath>\nusing namespace std;\n\nint dp[41][60001][3] = {};\nint main(){\n int N;\n int inf = 1e5;\n while(cin >> N && N){\n vector<int> P(N+1,0),T(N+1,0);\n for(int i=0;i<=N;i++) for(int j=0;j<=60000;j++) for(int k=0;k<=2;k++) dp[i][j][k] = inf;\n for(int i=1;i<=N;i++) cin >> P[i] >> T[i];\n dp[0][0][0] = 0;\n for(int i=1;i<=N;i++){\n for(int j=0;j<=T[i];j++){\n if(j+P[i]<=T[i]){\n dp[i][T[i]+2*P[i]][0] = min(dp[i][T[i]+2*P[i]][0],dp[i-1][j][0]+2*P[i]);\n dp[i][T[i]][1] = min(dp[i][T[i]][1],dp[i-1][j][0]+P[i]);\n }\n for(int k=1;k<=2;k++){\n if(j+(k+1)*abs(P[i]-P[i-1])<=T[i]){\n dp[i][T[i]+(k+2)*P[i]][0] = min(dp[i][T[i]+(k+2)*P[i]][0],dp[i-1][j][k]+abs(P[i]-P[i-1])+P[i]);\n if(k==1) dp[i][T[i]][k+1] = min(dp[i][T[i]][k+1],dp[i-1][j][k]+abs(P[i]-P[i-1]));\n }\n }\n }\n }\n int ans = inf;\n for(int j=0;j<=60000;j++) ans = min(ans,dp[N][j][0]);\n if(ans!=inf){\n cout << \"OK \" << ans << endl;\n continue;\n }\n cout << \"NG \";\n for(int i=1;i<=N;i++){\n bool ok = true;\n for(int j=0;j<=60000;j++) if(dp[i][j][0]!=inf) ok = false;\n if(ok){\n cout << i << endl;\n break;\n }\n }\n }\n}", "accuracy": 1, "time_ms": 180, "memory_kb": 31904, "score_of_the_acc": -0.337, "final_rank": 11 }, { "submission_id": "aoj_1306_3085164", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\nint n, dp[2][2][3][8001];\n\nvoid update(int& a, int t) {\n\tif (a == -1) a = t;\n\telse a = min(a, t);\n}\n\nvoid solve(int n) {\n\tmemset(dp, -1, sizeof(dp));\n\tdp[0][0][0][0] = 0;\n\t\n\tint prv = 0, ng_i = -1;\n\tfor (int i=0; i<n; ++i) {\n\t\tint p, t;\n\t\tcin >> p >> t;\n\t\t\n\t\tmemset(dp[1], -1, sizeof(dp[1]));\n\t\t\n\t\tbool ng = true;\n\t\tfor (int j=0; j<2; ++j) for (int k=0; k<3; ++k) for (int d=0; d<8001; ++d) {\n\t\t\tint cur_t = dp[0][j][k][d];\n\t\t\tif (cur_t == -1) continue;\n\t\t\t\n\t\t\tint nxt_t = cur_t + (k+1) * abs(prv * j - p);\n\t\t\tif (nxt_t > t) continue;\n\t\t\t\n\t\t\tint nxt_d = d + abs(prv * j - p);\n\t\t\tif (k + 1 < 3) update(dp[1][1][k+1][nxt_d], t);\n\n\t\t\tint back_t = t + (k+2) * p, back_d = nxt_d + p;\n\t\t\tupdate(dp[1][0][0][back_d], back_t);\n\t\t\t\n\t\t\tng = false;\n\t\t}\n\t\t\n\t\tprv = p;\n\t\tswap(dp[0], dp[1]);\n\t\t\n\t\tif (ng && ng_i == -1) ng_i = i+1;\n\t}\n\t\n\tif (ng_i == -1) {\n\t\tint ans = -1;\n\t\tfor (int d=8000; d>=0; --d) if (dp[0][0][0][d] != -1) ans = d;\n\t\tcout << \"OK \" << ans << endl;\n\t}\n\telse {\n\t\tcout << \"NG \" << ng_i << endl;\n\t}\n}\n\nint main() {\n\twhile (cin >> n, n) {\n\t\tsolve(n);\n\t}\n}", "accuracy": 1, "time_ms": 40, "memory_kb": 3400, "score_of_the_acc": -0.0333, "final_rank": 1 }, { "submission_id": "aoj_1306_2831280", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\n// よこ, たて\nvector<pair<int, int>> v;\nint ok=100000000;\nint ng=-1;\nvoid msearch(int i, int p, int bn, int t, int x, int move){\n if(move<ok){\n if(i==v.size()){\n if(ok>move+x){\n ok=move+x;\n }\n }else if(x==0){\n if(t+v[i].first>v[i].second){\n if(ng<p){\n ng=p;\n }\n }else{\n t=v[i].second;\n x=v[i].first;\n move+=v[i].first;\n msearch(i+1, p+1, bn+1, t, x, move);\n }\n }else{\n //cout<<\"x: \"<<x<<\" bn: \"<<bn<<\" vfront: \"<<v[i].first<<\" \"<<v[i].second<<\" t: \"<<t<<endl;\n if(abs(x-v[i].first)*(bn+1)+t<=v[i].second && bn<3){\n msearch(i+1, p+1, bn+1, v[i].second, v[i].first, move+abs(x-v[i].first));\n }\n if(t+x*(bn+1)+v[i].first>v[i].second){\n if(ng<p){\n ng=p;\n }\n }else{\n msearch(i+1, p+1, 1, v[i].second, v[i].first, move+x+v[i].first);\n }\n }\n }\n}\n\nint main(void){\n // Your code here!\n int n;\n while(cin>>n,n){\n v.resize(n);\n for(auto& a:v){\n cin>>a.first>>a.second;\n }\n msearch(0, 1, 0, 0, 0, (unsigned long long)0);\n if(ok==100000000){\n cout<<\"NG \"<<ng<<endl;\n }else{\n cout<<\"OK \"<<ok<<endl;\n }\n v.clear();\n ok=100000000;\n ng=-1;\n }\n}", "accuracy": 1, "time_ms": 410, "memory_kb": 2992, "score_of_the_acc": -0.1928, "final_rank": 8 }, { "submission_id": "aoj_1306_2831080", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nusing ll=long long;\nusing vi=vector<int>;\nusing vvi=vector<vi>;\nusing pii=pair<int,int>;\n#define rep(i,n) for(int i=0;i<n;i++)\n#define range(i,a,n) for(int i=a;i<n;i++)\n#define all(a) a.begin(),a.end()\n#define rall(a) a.rbegin(),a.rend()\n#define INF 1e9\n#define EPS 1e-9\n#define MOD (1e9+7)\nvoid put(string d){}template<class H,class...T>void put(string d,H&h,T&...t){cout<<h;if(sizeof...(t))cout<<d;put(d,t...);}\ntemplate<class T>void puti(T&a,string d=\" \"){bool f=1;for(auto&_:a)cout<<(exchange(f,0)?\"\":d)<<_;cout<<endl;}\ntemplate<class T>void putii(T&a,string d=\" \"){for(auto&_:a)puti(_,d);}\n\nvector<pii> b;\nusing data=tuple<int,int,int,int,int>;\nmap<data,pair<bool,int>> memo;\n\npair<bool,int> solve(int pos, int index, int lefttime, int carry, int totalDist){\n\tdata d=make_tuple(pos, index, lefttime, carry, totalDist);\n\tif(memo.find(d)!=memo.end()) return memo[d];\n\t//cout<<pos<<\" \"<<index<<\" \"<<lefttime<<\" \"<<carry<<\" \"<<totalDist<<endl;\n\tif(index==b.size()) return memo[d]=make_pair(true, totalDist+pos);\n\tint bpos=b[index].first;\n\tint bhigh=b[index].second-lefttime;\n\tint dist=abs(pos-bpos);\n\tint need=dist*(carry+1);\n\tif(bhigh<need){\n\t\treturn memo[d]=make_pair(false,index+1);\t//目標を取れない\n\t}else{\n\t\tlefttime+=bhigh;\n\t}\n\tif(carry==2){\t//3つめを持った\n\t\treturn memo[d]=solve(0, index+1,lefttime+bpos*(carry+2), 0, totalDist+dist+bpos);\n\t}\n\tauto ans_next=solve(bpos, index+1, lefttime, carry+1, totalDist+dist);\n\tneed=bpos*(carry+2);\n\tauto ans_home=solve(0, index+1,lefttime+need, 0, totalDist+dist+bpos);\n\tif(ans_home.first==false and ans_next.first==false) {\n\t\tif(ans_home.second>ans_next.second){\n\t\t\treturn memo[d]=ans_home;\n\t\t}else{\n\t\t\treturn memo[d]=ans_next;\t\t\t\n\t\t}\n\t}\n\tif(ans_home.first==false) return memo[d]=ans_next;\n\tif(ans_next.first==false) return memo[d]=ans_home;\n\tif(ans_next.second > ans_home.second) return memo[d]=ans_home;\n\telse return memo[d]=ans_next;\n}\n\n\nint main(){\n\tint n;\n\twhile(cin>>n,n){\n\t\tmemo.clear();\n\t\tb.resize(n);\n\t\tfor(auto&i:b)cin>>i.first>>i.second;\n\t\tauto res=solve(0,0,0,0,0);\n\t\tcout<<(res.first?\"OK \":\"NG \")<<res.second<<endl;\n\t}\n\treturn 0;\n}", "accuracy": 1, "time_ms": 90, "memory_kb": 7436, "score_of_the_acc": -0.0896, "final_rank": 4 }, { "submission_id": "aoj_1306_2316724", "code_snippet": "#include<bits/stdc++.h>\n#define range(i,a,b) for(int i = (a); i < (b); i++)\n#define rep(i,b) for(int i = 0; i < (b); i++)\n#define all(a) (a).begin(), (a).end()\n#define show(x) cerr << #x << \" = \" << (x) << endl;\nusing namespace std;\n\nint g_dis;\nint ng;\nint n;\npair<int, int> p[50]; //???????????????\n\nvoid dfs(int i, int d, int k){\n if(k > 3) return;\n// show(t)\n// show(i)\n int c = i == 0 ? 0 : p[i - 1].first;\n int t = i == 0 ? 0 : p[i - 1].second;\n if(g_dis < d) return;\n if(i == n){\n g_dis = min(g_dis, d + c);\n return;\n }\n\n //show(p[i].second)\n //show(dist * (k + 1))\n //show(c * (k + 1) + p[i].first)\n\n int dist = abs(p[i].first - c);\n ng = max(ng, i);\n if(p[i].second >= t + dist * (k + 1)) dfs(i + 1, d + dist, k + 1);\n if(p[i].second >= t + c * (k + 1) + p[i].first) dfs(i + 1, d + c + p[i].first, 1);\n}\n\nint main(){\n cin.tie(0);\n ios::sync_with_stdio(false);\n\n while(cin >> n,n){\n g_dis = 1e8;\n ng = 0;\n rep(i,n) cin >> p[i].first >> p[i].second;\n dfs(0,0,0);\n if(g_dis == 1e8) cout << \"NG \" << ng + 1 << endl;\n else cout << \"OK \" << g_dis << endl;\n }\n}", "accuracy": 1, "time_ms": 950, "memory_kb": 3100, "score_of_the_acc": -0.4316, "final_rank": 13 }, { "submission_id": "aoj_1306_2054700", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\ntypedef long long ll;\n#define rep(i,n) for(int (i)=0;(i)<(int)(n);++(i))\n#define each(itr,c) for(__typeof(c.begin()) itr=c.begin(); itr!=c.end(); ++itr)\n#define all(x) (x).begin(),(x).end()\n#define pb push_back\n#define fi first\n#define se second\n\nconst int INF=123456789;\n\nint dp[5][4][101];\nint nx[5][4][101];\nint b[50500];\n\ninline void init()\n{\n memset(b,-1,sizeof(b));\n rep(i,5)rep(j,4)rep(k,101) dp[i][j][k]=INF;\n}\n\nint main()\n{\n int n;\n while(cin >>n,n)\n {\n init();\n\n map<int,int> num;\n vector<int> P(n), T(n);\n rep(i,n)\n {\n scanf(\" %d %d\", &P[i], &T[i]);\n b[T[i]]=P[i];\n num[T[i]]=i;\n }\n\n int ans=-1;\n bool fail=false;\n dp[1][0][0]=0;\n for(int t=1; t<50500; ++t)\n {\n rep(w,4)\n {\n if(b[t]==-1)\n {\n bool ok=(t-(w+1)>=0);\n if(ok)\n {\n for(int i=t; i>t-(w+1); --i) if(b[i]!=-1) ok=false;\n }\n\n if(ok) dp[0][0][0]=min(dp[0][0][0],dp[w+1][w][1]+1);\n\n for(int pos=1; pos<=100; ++pos)\n {\n dp[0][w][pos]=min(dp[0][w][pos],dp[1][w][pos]);\n if(ok)\n {\n dp[0][w][pos]=min(dp[0][w][pos],dp[w+1][w][pos-1]+1);\n if(pos+1<=100) dp[0][w][pos]=min(dp[0][w][pos],dp[w+1][w][pos+1]+1);\n }\n }\n }\n else\n {\n int b_idx=num[t];\n int pos=b[t];\n if(w<3)\n {\n dp[0][w+1][pos]=min(dp[0][w+1][pos],dp[1][w][pos]);\n\n if( t-(w+1)>=0 && (b_idx==0 ||(b_idx>0 && t-(w+1)>=T[b_idx-1])) )\n {\n if(pos-1>=0) dp[0][w+1][pos]=min(dp[0][w+1][pos],dp[w+1][w][pos-1]+1);\n if(pos+1<=100) dp[0][w+1][pos]=min(dp[0][w+1][pos],dp[w+1][w][pos+1]+1);\n }\n }\n }\n }\n\n if(b[t]!=-1)\n {\n // printf(\" %d th balloon, (%d,%d)\\n\", num[t],P[num[t]],T[num[t]]);\n bool c=false;\n rep(j,4)\n {\n // printf(\" %d, %d\\n\",j,dp[0][j][b[t]] );\n if(dp[0][j][b[t]]<INF) c=true;\n }\n if(!c)\n {\n fail=true;\n ans=num[t]+1;\n break;\n }\n }\n\n for(int i=4; i>0; --i)rep(j,4)rep(k,101) dp[i][j][k]=dp[i-1][j][k];\n rep(i,4)rep(j,101) dp[0][i][j]=INF;\n }\n\n if(fail) printf(\"NG %d\\n\", ans);\n else printf(\"OK %d\\n\", dp[1][0][0]);\n }\n return 0;\n}", "accuracy": 1, "time_ms": 2120, "memory_kb": 3276, "score_of_the_acc": -0.9485, "final_rank": 16 }, { "submission_id": "aoj_1306_2053830", "code_snippet": "#include \"bits/stdc++.h\"\nusing namespace std;\ntypedef long long ll;\ntypedef pair<int,int> pii;\n#define rep(i,n) for(ll i=0;i<(ll)(n);i++)\n#define all(a) (a).begin(),(a).end()\n#define pb push_back\n#define INF (1e9+1)\n//#define INF (1LL<<59)\n\nint main(){\n\tint n;\n\twhile(cin>>n&&n){\n\t\tstatic int dp[50][5][10000];\n\t\trep(i,50)rep(j,5)rep(k,10000)dp[i][j][k] = INF;\n\t\tdp[0][0][0] = 0;\n\t\t\n\t\tint bef = 0;\n\t\tbool endf = false;\n\t\tint p,t;\n\t\trep(i,n){\n\t\t\tcin>>p>>t;\n\t\t\tif(endf)continue;\n\t\t\trep(j,3){\n\t\t\t\trep(k,10000){\n\t\t\t\t\tif(k+abs(p-bef)>=10000)continue;\n\t\t\t\t\tint res = dp[i][j][k] + (j+1)*abs(p-bef);\n\t\t\t\t\tif(res<=t)\n\t\t\t\t\t\tdp[i+1][j+1][k+abs(p-bef)] = t;\n\t\t\t\t}\n\t\t\t}\n\t\t\tfor(int j=1;j<=3;j++){\n\t\t\t\trep(k,10000){\n\t\t\t\t\tif(k+bef+p>=10000)continue;\n\t\t\t\t\tint res = dp[i][j][k]+(j+1)*bef+p;\n\t\t\t\t\tif(res<=t)\n\t\t\t\t\t\tdp[i+1][1][k+bef+p] = t;\n\t\t\t\t}\n\t\t\t}\n\t\t\t\n\t\t\tbool f=false;\n\t\t\trep(j,4){\n\t\t\t\trep(k,10000){\n\t\t\t\t\tif(dp[i+1][j][k]<=t)f=true;\n\t\t\t\t}\n\t\t\t}\n\t\t\tif(!f){\n\t\t\t\tcout<<\"NG \"<<i+1<<endl;\n\t\t\t\tendf = true;\n\t\t\t}\n\t\t\t\n\t\t\tbef = p;\n\t\t}\n\t\tif(!endf){\n\t\t\tint ans = INF;\n\t\t\trep(i,4){\n\t\t\t\trep(j,10000){\n\t\t\t\t\tif(dp[n][i][j]<=t){\n\t\t\t\t\t\tans = min<ll>(ans,j+p);\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t\tcout<<\"OK \"<<ans<<endl;\n\t\t}\n\t}\n\t\n}", "accuracy": 1, "time_ms": 90, "memory_kb": 12780, "score_of_the_acc": -0.135, "final_rank": 7 }, { "submission_id": "aoj_1306_1642944", "code_snippet": "#include <iostream>\n#include <algorithm>\nusing namespace std;\nint n,dis[41],t[41],maxball,ans;\n\nvoid saiki(int x,int now,int ball,int sum,int kai){\n if(ans<sum)return;\n maxball=max(maxball,kai);\n if(kai==n&&!ball)ans=min(ans,sum);\n if(ball)saiki(0,now+x*(1+ball),0,sum+x,kai);\n if(ball==3 || kai==n) return;\n \n int nx=dis[kai];\n int nsum=sum+abs(x-dis[kai]);\n int nnow=now+abs(x-dis[kai])*(1+ball);\n if(nnow<=t[kai])saiki(nx,t[kai],ball+1,nsum,kai+1);\n}\n\nint main(){\n while(1){\n cin>>n;\n if(!n)break;\n for(int i=0;i<n;i++)cin >>dis[i]>>t[i];\n sort(t,t+n);\n maxball=0,ans=100000000;\n saiki(0,0,0,0,0);\n if(maxball==n)cout <<\"OK \"<<ans<<endl;\n else cout<<\"NG \"<<maxball+1<<endl;\n }\n return 0;\n}", "accuracy": 1, "time_ms": 1020, "memory_kb": 1180, "score_of_the_acc": -0.4461, "final_rank": 14 }, { "submission_id": "aoj_1306_1175259", "code_snippet": "// AOJ 1306\n#include <bits/stdc++.h>\nusing namespace std;\n#define rep(i,n) for(int i = 0 ;i < n ; i++)\n\nint p[100],t[100];\nint dp[100][55010];\nint N;\n\nint main(){\n\twhile( cin >> N && N ){\n\t\trep(i,N) cin >> p[i] >> t[i];\n\t\trep(i,41)rep(j,55010) dp[i][j] = 1e9;\n\t\tdp[0][0] = 0;\n\t\trep(i,N){\n\t\t\trep(k,50010){\n\t\t\t\tint s = 0;\n\t\t\t\tint w = 0;\n\t\t\t\tint pr = 0;\n\t\t\t\tint maxi = k;\n\t\t\t\trep(j,3){\n\t\t\t\t\tif( i+j >= N ) break;\n\t\t\t\t\ts = (j+1)*abs(pr-p[i+j]); \n\t\t\t\t\tw += abs(pr-p[i+j]);\n\t\t\t\t\tif( maxi + s > t[i+j] ) break;\n\t\t\t\t\tmaxi = t[i+j];\n\t\t\t\t\tint next = t[i+j]+(j+2)*p[i+j];\n\t\t\t\t\tdp[i+j+1][next] = min(dp[i+j+1][next],dp[i][k]+w+p[i+j]);\n\t\t\t\t\tpr = p[i+j];\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\tint fail = 0;\n\t\trep(i,N+1){\n\t\t\tint ok = 0;\n\t\t\trep(j,55010)\n\t\t\t\tif( dp[i][j] != 1e9 ) ok = 1;\n\t\t\tif(!ok){ cout << \"NG \" << i << endl; fail = 1; break; }\n\t\t}\n\t\tif( fail ) continue;\n\t\tcout << \"OK \" << *min_element(dp[N],dp[N]+55010) << endl;\n\t}\n}", "accuracy": 1, "time_ms": 290, "memory_kb": 11400, "score_of_the_acc": -0.2113, "final_rank": 9 }, { "submission_id": "aoj_1306_986034", "code_snippet": "#include<stdio.h>\n#include<algorithm>\nusing namespace std;\nint dp[50][4][8100];\nint x[41];\nint t[41];\ninline int ABS(int a){return max(a,-a);}\nint main(){\n\tint a;\n\twhile(scanf(\"%d\",&a),a){\n\t\tint m=0;\n\t\tfor(int i=0;i<a;i++){\n\t\t\tscanf(\"%d%d\",x+i,t+i);\n\t\t\tm=max(m,t[i]);\n\t\t}\n\t\tfor(int i=0;i<a+1;i++){\n\t\t\tfor(int j=0;j<4;j++){\n\t\t\t\tfor(int k=0;k<200*a+10;k++)\n\t\t\t\t\tdp[i][j][k]=99999999;\n\t\t\t}\n\t\t}\n\t\tdp[0][0][0]=0;\n\t\tfor(int i=0;i<a;i++){\n\t\t\tfor(int j=0;j<4;j++){\n\t\t\t\tfor(int k=0;k<200*a+10;k++){\n\t\t\t\t\tif(dp[i][j][k]>9999999)continue;\n\t\t\t\t\tint t1=(i?x[i-1]:0)*(j+1)+x[i];\n\t\t\t\t\t//printf(\"1: %d %d %d %d\\n\",i,j,k,dp[i][j][k]+t1);\n\t\t\t\t\tif(dp[i][j][k]+t1<=t[i]){\n\t\t\t\t\t\tdp[i+1][1][k+x[i-1]+x[i]]=min(dp[i+1][1][k+x[i-1]+x[i]],t[i]);\n\t\t\t\t\t}\n\t\t\t\t\tif(j<3){\n\t\t\t\t\t\tint t2=ABS((i?x[i-1]:0)-x[i])*(j+1);\n\t\t\t\t\t//\tprintf(\"2: %d %d %d %d\\n\",i,j,k,dp[i][j][k]+t2);\n\t\t\t\t\t\tif(dp[i][j][k]+t2<=t[i]){\n\t\t\t\t\t\t\tdp[i+1][j+1][k+ABS((i?x[i-1]:0)-x[i])]=min(dp[i+1][j+1][k+ABS((i?x[i-1]:0)-x[i])],t[i]);\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\tfor(int i=a;i>=0;i--){\n\t\t\tint ret=99999999;\n\t\t\tfor(int j=0;j<4;j++){\n\t\t\t\tfor(int k=0;k<200*a+10;k++){\n\t\t\t\t\tif(dp[i][j][k]<9999999)ret=min(ret,k+x[a-1]);\n\t\t\t\t}\n\t\t\t}\n\t\t\tif(ret<999999){\n\t\t\t\tif(i==a)printf(\"OK %d\\n\",ret);\n\t\t\t\telse printf(\"NG %d\\n\",i+1);\n\t\t\t\tbreak;\n\t\t\t}\n\t\t}\n\t}\n}", "accuracy": 1, "time_ms": 30, "memory_kb": 7168, "score_of_the_acc": -0.0609, "final_rank": 2 }, { "submission_id": "aoj_1306_523677", "code_snippet": "#include <iostream>\n#include <algorithm>\n#include <vector>\n#include <map>\n#include <cmath>\nusing namespace std;\n\nconst int MAX_K = 4;\nconst int MAX_X = 10500;\nconst int MAX_N = 41;\nconst int INF = 1e+8;\nint n, p[MAX_N], t[MAX_N];\nint ans, get;\nint dp[MAX_N][MAX_K][MAX_X];\n\nvoid solve(){\n\tfor(int i=0 ; i < MAX_N ; i++ ){\n\t\tfor(int j=0 ; j < MAX_K ; j++ ){\n\t\t\tfor(int k=0 ; k < MAX_X ; k++ ){\n\t\t\t\tdp[i][j][k] = INF;\n\t\t\t}\n\t\t}\n\t}\n\t\n\t// dp[i][k][x] := sum distance\n\tdp[0][0][0] = 0;\n\tfor(int i=0 ; i < n ; i++ ){\n\t\tfor(int k=0 ; k <= 3 ; k++ ){\n\t\t\tfor(int x=0 ; x < MAX_X ; x++ ){\n\t\t\t\tif( dp[i][k][x] != INF ){\n\t\t\t\t\tif( k != 0 ){ // go house\n\t\t\t\t\t\tint dist = x;\n\t\t\t\t\t\tdouble next_time = t[i-1] + (double)dist / (1.0 / (k+1));\n\t\t\t\t\t\tdist += p[i];\n\t\t\t\t\t\tnext_time += (double)p[i];\n\t\t\t\t\t\t\n\t\t\t\t\t\tif( next_time <= t[i] ){\n\t\t\t\t\t\t\tdp[i+1][1][p[i]] = min(dp[i+1][1][p[i]], dp[i][k][x] + dist );\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t\tif( k != 3 ){\n\t\t\t\t\t\tint dist = abs(x - p[i]);\n\t\t\t\t\t\tdouble prev_time = i? t[i-1] : 0;\n\t\t\t\t\t\tdouble next_time = prev_time + (double)dist / (1.0 / (k+1));\n\t\t\t\t\t\t\n\t\t\t\t\t\tif( next_time <= t[i] ){\n\t\t\t\t\t\t\tdp[i+1][k+1][p[i]] = min(dp[i+1][k+1][p[i]], dp[i][k][x] + dist );\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n\tget = 0;\n\tans = INF;\n\tfor(int i=0 ; i <= n ; i++ ){\n\t\tfor(int k=0 ; k <= 3 ; k++ ){\n\t\t\tfor(int x=0 ; x < MAX_X ; x++ ){\n\t\t\t\tif( dp[i][k][x] != INF ){\n\t\t\t\t\tget = max(get, i);\n\t\t\t\t\tif( i == n ){\n\t\t\t\t\t\tans = min(ans, dp[i][k][x] + p[i-1]);\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n}\n\nint main(){\n\twhile( cin >> n , n ){\n\t\tfor(int i=0 ; i < n ; i++ ){\n\t\t\tcin >> p[i] >> t[i];\n\t\t}\n\t\tans = INF;\n\t\tsolve();\n\t\tif( ans != INF ){\n\t\t\tcout << \"OK \" << ans << endl;\n\t\t}else{\n\t\t\tcout << \"NG \" << get+1 << endl;\n\t\t}\n\t}\n}", "accuracy": 1, "time_ms": 70, "memory_kb": 7888, "score_of_the_acc": -0.0846, "final_rank": 3 }, { "submission_id": "aoj_1306_522896", "code_snippet": "#include <iostream>\n#include <algorithm>\n#include <vector>\n#include <map>\n#include <cmath>\nusing namespace std;\n\nconst int MAX_K = 5;\nconst int MAX_X = 10500;\nconst int MAX_N = 41;\nconst int INF = 1e+8;\nint n, p[MAX_N], t[MAX_N];\nint ans, get;\nint dp[MAX_N][MAX_K][MAX_X];\n\nvoid solve(){\n\tfor(int i=0 ; i < MAX_N ; i++ ){\n\t\tfor(int j=0 ; j < MAX_K ; j++ ){\n\t\t\tfor(int k=0 ; k < MAX_X ; k++ ){\n\t\t\t\tdp[i][j][k] = INF;\n\t\t\t}\n\t\t}\n\t}\n\t\n\t// dp[i][k][x] := sum distance\n\tdp[0][0][0] = 0;\n\tfor(int i=0 ; i < n ; i++ ){\n\t\tfor(int k=0 ; k <= 3 ; k++ ){\n\t\t\tfor(int x=0 ; x < MAX_X ; x++ ){\n\t\t\t\tif( dp[i][k][x] != INF ){\n\t\t\t\t\tif( k != 0 ){ // go house\n\t\t\t\t\t\tint dist = x;\n\t\t\t\t\t\tdouble next_time = t[i-1] + (double)dist / (1.0 / (k+1));\n\t\t\t\t\t\t\n\t\t\t\t\t\tdist += p[i];\n\t\t\t\t\t\tnext_time += (double)p[i];\n\t\t\t\t\t\t//cout << \"time:\" << next_time << \" t[i]:\" << t[i] << endl;\n\t\t\t\t\t\t//cout << \"dist:\" << dp[i][k][x] + dist << endl;\n\t\t\t\t\t\tif( next_time <= t[i] ){\n\t\t\t\t\t\t\t//cout << \"time:\" << next_time << \" t[i]:\" << t[i] << endl;\n\t\t\t\t\t\t\tdp[i+1][1][p[i]] = min(dp[i+1][1][p[i]], dp[i][k][x] + dist );\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t\tif( k != 3 ){\n\t\t\t\t\t\tint dist = abs(x - p[i]);\n\t\t\t\t\t\tdouble prev_time = i? t[i-1] : 0;\n\t\t\t\t\t\tdouble next_time = prev_time + (double)dist / (1.0 / (k+1));\n\t\t\t\t\t\t//cout << \"time:\" << next_time << \" t[i]:\" << t[i] << endl;\n\t\t\t\t\t\tif( next_time <= t[i] ){\n\t\t\t\t\t\t\tdp[i+1][k+1][p[i]] = min(dp[i+1][k+1][p[i]], dp[i][k][x] + dist );\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n\tget = 0;\n\tans = INF;\n\tfor(int i=0 ; i <= n ; i++ ){\n\t\tfor(int k=0 ; k <= 3 ; k++ ){\n\t\t\tfor(int x=0 ; x < MAX_X ; x++ ){\n\t\t\t\tif( dp[i][k][x] != INF ){\n\t\t\t\t\t//cout << \"i:\" << i << \" k:\" << k << \" x:\" << x << \" dist:\" << dp[i][k][x] << endl;\n\t\t\t\t\tget = max(get, i);\n\t\t\t\t\tif( i == n ){\n\t\t\t\t\t\tans = min(ans, dp[i][k][x] + p[i-1]);\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n}\n\nint main(){\n\twhile( cin >> n , n ){\n\t\tfor(int i=0; i < n ; i++ ){\n\t\t\tcin >> p[i] >> t[i];\n\t\t}\n\t\tans = INF;\n\t\tsolve();\n\t\tif( ans != INF ){\n\t\t\tcout << \"OK \" << ans << endl;\n\t\t}else{\n\t\t\tcout << \"NG \" << get+1 << endl;\n\t\t}\n\t}\n}", "accuracy": 1, "time_ms": 80, "memory_kb": 9568, "score_of_the_acc": -0.1033, "final_rank": 6 }, { "submission_id": "aoj_1306_466746", "code_snippet": "#include<iostream>\n#include<algorithm>\n#include<vector>\n#include<cmath>\n#define F first\n#define S second\nusing namespace std;\ntypedef pair<int,int> P;\nvector<P> info;//position,time\n\n//再帰で全てのパターンをためす\n//といっても本当に全てのパターンやったら終わらないので\n//現在の距離が答えの距離より大きくなったら最短にはなりえないのでおわらせる\n\n\nbool iscan;\nint ans;\nint failedPosition;\n\nvoid rec(int time,int k,int distance,int position){\n if(distance >= ans)return;\n\n if(position >= info.size()-1){\n ans = min(ans,distance+info[position].F);\n //cout << \"ans = \" << ans << endl;\n return;\n }\n\n\n int time1,time2;//time1 -> そのまま次へ, time2 -> 一旦家に帰る\n time1 = time+(k+1)*abs(info[position+1].F-info[position].F);\n time2 = time+(k+1)*abs(info[position].F-info[0].F)+info[position+1].F;\n \n if(k == 3){//vechicle can carry at most three balloons at a time. <- importance\n if(time2 <= info[position+1].S){\n rec(info[position+1].S,1,distance+abs(info[position].F-info[0].F)+info[position+1].F,position+1);\n return; // <- dont forget\n }\n else{\n iscan = false;\n failedPosition = position+1;\n return;\n }\n \n }\n\n\n\n \n if(time1 <=info[position+1].S && time2 <=info[position+1].S ){\n rec(info[position+1].S,k+1,distance+abs(info[position+1].F-info[position].F),position+1);\n if(position!=0)rec(info[position+1].S,1,distance+abs(info[position].F-info[0].F)+info[position+1].F,position+1);\n }\n else if(time1<=info[position+1].S){\n rec(info[position+1].S,k+1,distance+abs(info[position+1].F-info[position].F),position+1);\n }\n else if(time2<=info[position+1].S){\n rec(info[position+1].S,1,distance+abs(info[position].F-info[0].F)+info[position+1].F,position+1);\n }\n else {\n iscan = false;\n failedPosition = position+1;\n return;\n } \n\n\n\n}\n\n\nint main(){\n int n,p,t;\n const int INF=999999999;\n\n while(cin >> n && n){\n ans = INF;\n info.clear();\n iscan = true;\n\n info.push_back(P(0,0));//Home \n for(int i=0;i<n;i++){\n cin >> p >> t;\n info.push_back(P(p,t)); // t_i < t_(i+1)\n }\n \n rec(0,0,0,0);\n\n if(!iscan && ans == INF){\n cout << \"NG \" << failedPosition << endl;\n }\n else{\n cout << \"OK \" << ans << endl;\n }\n\n }\n return 0;\n}", "accuracy": 1, "time_ms": 540, "memory_kb": 1204, "score_of_the_acc": -0.2349, "final_rank": 10 }, { "submission_id": "aoj_1306_466742", "code_snippet": "#include<iostream>\n#include<algorithm>\n#include<vector>\n#include<cmath>\n#define F first\n#define S second\nusing namespace std;\ntypedef pair<int,int> P;\nvector<P> info;//position,time\n\nbool iscan;\nint ans;\nint failedPosition;\n\nvoid rec(int time,int k,int distance,int position){\n if(distance >= ans)return;\n\n if(position >= info.size()-1){\n ans = min(ans,distance+info[position].F);\n //cout << \"ans = \" << ans << endl;\n return;\n }\n\n\n int time1,time2;//time1 -> そのまま次へ, time2 -> 一旦家に帰る\n time1 = time+(k+1)*abs(info[position+1].F-info[position].F);\n time2 = time+(k+1)*abs(info[position].F-info[0].F)+info[position+1].F;\n \n if(k == 3){\n if(time2 <= info[position+1].S){\n rec(info[position+1].S,1,distance+abs(info[position].F-info[0].F)+info[position+1].F,position+1);\n return;\n }\n else{\n iscan = false;\n failedPosition = position+1;\n return;\n }\n \n }\n\n\n\n \n if(time1 <=info[position+1].S && time2 <=info[position+1].S ){\n rec(info[position+1].S,k+1,distance+abs(info[position+1].F-info[position].F),position+1);\n rec(info[position+1].S,1,distance+abs(info[position].F-info[0].F)+info[position+1].F,position+1);\n }\n else if(time1<=info[position+1].S){\n rec(info[position+1].S,k+1,distance+abs(info[position+1].F-info[position].F),position+1);\n }\n else if(time2<=info[position+1].S){\n rec(info[position+1].S,1,distance+abs(info[position].F-info[0].F)+info[position+1].F,position+1);\n }\n else {\n iscan = false;\n failedPosition = position+1;\n return;\n } \n\n\n\n}\n\n\nint main(){\n int n,p,t;\n const int INF=999999999;\n\n while(cin >> n && n){\n ans = INF;\n info.clear();\n iscan = true;\n\n info.push_back(P(0,0));\n for(int i=0;i<n;i++){\n cin >> p >> t;\n info.push_back(P(p,t));\n }\n \n rec(0,0,0,0);\n\n if(!iscan && ans == INF){\n cout << \"NG \" << failedPosition << endl;\n }\n else{\n cout << \"OK \" << ans << endl;\n }\n\n }\n return 0;\n}", "accuracy": 1, "time_ms": 940, "memory_kb": 1204, "score_of_the_acc": -0.4111, "final_rank": 12 }, { "submission_id": "aoj_1306_274466", "code_snippet": "#include<iostream>\n#include<vector>\n#include<stdlib.h>\nusing namespace std;\n\n\nstruct bal{\n\tint time,loc;\n};\n\nint n;\nint m_depth;\nint res=100000000;\nvector<bal> V;\n\nvoid dfs(int depth,int k,int total){\n\tm_depth=max(m_depth,depth);\n\tif(total>=res)\n\t\treturn;\n\tif(depth==n){\n\t\ttotal+=V[n-1].loc;\n\t\tres=min(res,total);\n\t\treturn;\n\t}\n\tint n_loc=0;\n\tint room=V[0].time;\n\tif(depth!=0){\n\t\tn_loc=V[depth-1].loc;\n\t\troom=V[depth].time-V[depth-1].time;\n\t}\n\n\tint cost=0;\n\tif(k!=0){\n\t\tcost+=n_loc*(k+1);\n\t\tcost+=V[depth].loc*1;\n\t\tif(cost<=room)\n\t\t\tdfs(depth+1,1,total+n_loc+V[depth].loc);\n\t}\n\tcost=0;\n\tif(k!=3){\n\t\tcost+=(abs(V[depth].loc-n_loc))*(k+1);\n\t\tif(cost<=room)\n\t\t\tdfs(depth+1,k+1,total+abs(V[depth].loc-n_loc));\n\t}\n\n}\n\nvoid solve(){\n\tres=100000000;\n\tV.clear();\n\tm_depth=0;\n\tfor(int i=0;i<n;i++){\n\t\tbal now;\n\t\tcin>>now.loc>>now.time;\n\t\tV.push_back(now);\n\t}\n\tm_depth;\n\tdfs(0,0,0);\n\tif(m_depth!=n)\n\t\tcout<<\"NG \"<<m_depth+1<<endl;\n\telse cout<<\"OK \"<<res<<endl;\n}\n\n\nint main()\n{\n\twhile(cin>>n){\n\t\tif(n==0) break;\n\t\tsolve();\n\t}\n\treturn 0;\n}", "accuracy": 1, "time_ms": 1980, "memory_kb": 0, "score_of_the_acc": -0.859, "final_rank": 15 } ]
aoj_1308_cpp
Problem D: Awkward Lights You are working as a night watchman in an office building. Your task is to check whether all the lights in the building are turned off after all the office workers in the building have left the office. If there are lights that are turned on, you must turn off those lights. This task is not as easy as it sounds to be because of the strange behavior of the lighting system of the building as described below. Although an electrical engineer checked the lighting system carefully, he could not determine the cause of this behavior. So you have no option but to continue to rely on this system for the time being. Each floor in the building consists of a grid of square rooms. Every room is equipped with one light and one toggle switch. A toggle switch has two positions but they do not mean fixed ON/OFF. When the position of the toggle switch of a room is changed to the other position, in addition to that room, the ON/OFF states of the lights of the rooms at a certain Manhattan distance from that room are reversed. The Manhattan distance between the room at ( x 1 , y 1 ) of a grid of rooms and the room at ( x 2 , y 2 ) is given by | x 1 - x 2 | + | y 1 - y 2 |. For example, if the position of the toggle switch of the room at (2, 2) of a 4 × 4 grid of rooms is changed and the given Manhattan distance is two, the ON/OFF states of the lights of the rooms at (1, 1), (1, 3), (2, 4), (3, 1), (3, 3), and (4, 2) as well as at (2, 2) are reversed as shown in Figure D.1, where black and white squares represent the ON/OFF states of the lights. Figure D.1: An example behavior of the lighting system Your mission is to write a program that answer whether all the lights on a floor can be turned off. Input The input is a sequence of datasets. Each dataset is formatted as follows. m n d S 11 S 12 S 13 ... S 1 m S 21 S 22 S 23 ... S 2 m ... S n 1 S n 2 S n 3 ... S nm The first line of a dataset contains three integers. m and n (1 ≤ m ≤ 25, 1 ≤ n ≤ 25) are the numbers of columns and rows of the grid, respectively. d (1 ≤ d ≤ m + n ) indicates the Manhattan distance. Subsequent n lines of m integers are the initial ON/OFF states. Each S ij (1 ≤ i ≤ n , 1 ≤ j ≤ m ) indicates the initial ON/OFF state of the light of the room at ( i , j ): '0' for OFF and '1' for ON. The end of the input is indicated by a line containing three zeros. Output For each dataset, output '1' if all the lights can be turned off. If not, output '0'. In either case, print it in one line for each input dataset. Sample Input 1 1 1 1 2 2 1 1 1 1 1 3 2 1 1 0 1 0 1 0 3 3 1 1 0 1 0 1 0 1 0 1 4 4 2 1 1 0 1 0 0 0 1 1 0 1 1 1 0 0 0 5 5 1 1 1 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 5 5 2 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 11 11 3 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 ...(truncated)
[ { "submission_id": "aoj_1308_10853874", "code_snippet": "#include <stdio.h>\n#include <string.h>\n#include <algorithm>\n#include <iostream>\n#include <queue>\n#include <map>\n#include <set>\n#include <vector>\n#include <math.h>\n#include <string>\nusing namespace std;\n\nconst int MAXN = 1000;\nint equ,var;\nint a[MAXN][MAXN];\nint x[MAXN];\n\nint Gauss()\n{\n\tint max_r,col,k;\n\tfor(k = 0,col = 0;k < equ && col < var;k++,col++)\n\t{\n\t\tmax_r = k;\n\t\tfor(int i = k+1;i < equ;i++)\n\t\t{\n\t\t\tif(abs(a[i][col]) > abs(a[max_r][k]))\n\t\t\t\tmax_r = i;\n\t\t}\n\t\tif(a[max_r][col] == 0)\n\t\t{\n\t\t\tk--;\n\t\t\tcontinue;\n\t\t}\n\t\tif(max_r != k)\n\t\t{\n\t\t\tfor(int j = col;j < var+1;j++)\n\t\t\t\tswap(a[k][j],a[max_r][j]);\n\t\t}\n\t\tfor(int i = k+1;i < equ;i++)\n\t\t{\n\t\t\tif(a[i][col] != 0)\n\t\t\t{\n\t\t\t\tfor(int j = col;j < var+1;j++)\n\t\t\t\t\ta[i][j] ^= a[k][j];\n\t\t\t}\n\t\t}\n\t}\n\tfor(int i = k;i < equ;i++)\n\t\tif(a[i][col] != 0)\n\t\t\treturn -1;\n\tif(k < var)return var-k;\n\treturn 0;\n}\n\nint main()\n{\n\t//freopen(\"in.txt\",\"r\",stdin);\n\t//freopen(\"out.txt\",\"w\",stdout);\n\tint n,m,d;\n\twhile(scanf(\"%d%d%d\",&m,&n,&d) == 3)\n\t{\n\t\tif(n == 0 && m == 0 && d == 0)break;\n\t\tequ = var = n*m;\n\t\tmemset(a,0,sizeof(a));\n\t\tfor(int i = 0;i < n*m;i++)\n\t\t\tscanf(\"%d\",&a[i][n*m]);\n\t\tfor(int j = 0;j < n*m;j++)\n\t\t{\n\t\t\ta[j][j] = 1;\n\t\t\tint x = j/m;\n\t\t\tint y = j%m;\n\t\t\tfor(int i= -d;i <= d;i++)\n\t\t\t{\n\t\t\t\tint xx = x+i;\n\t\t\t\tint yy = y - (d-abs(i));\n\t\t\t\tif(xx < 0 || xx >= n || yy < 0 || yy >= m)continue;\n\t\t\t\ta[xx*m + yy][j] = 1;\n\t\t\t}\n\t\t\tfor(int i= -d;i <= d;i++)\n\t\t\t{\n\t\t\t\tint xx = x+i;\n\t\t\t\tint yy = y + (d-abs(i));\n\t\t\t\tif(xx < 0 || xx >= n || yy < 0 || yy >= m)continue;\n\t\t\t\ta[xx*m + yy][j] = 1;\n\t\t\t}\n\t\t}\n\t\tif(Gauss() >= 0)printf(\"1\\n\");\n\t\telse printf(\"0\\n\");\n\t}\n\treturn 0;\n}", "accuracy": 1, "time_ms": 130, "memory_kb": 7088, "score_of_the_acc": -0.5077, "final_rank": 16 }, { "submission_id": "aoj_1308_10689536", "code_snippet": "#include<cstdio>\n#include<iostream>\n#include<queue>\n#include<algorithm>\n#include<map>\n#include<vector>\n#include<cstring>\n#include<cmath>\ninline int Input(){\n\tint ret=0;bool isN=0;char c=getchar();\n\twhile(c<'0' || c>'9'){\n\t\tif(c=='-') isN=1;\n\t\tc=getchar();\n\t}\n\twhile(c>='0' && c<='9'){\n\t\tret=ret*10+c-'0';\n\t\tc=getchar();\n\t}\n\treturn isN?-ret:ret;\n}\n\ninline void Output(long long x){\n if(x<0){\n putchar('-');x=-x;\n }\n int len=0,data[20];\n while(x){\n data[len++]=x%10;x/=10;\n }\n if(!len) data[len++]=0;\n while(len--)\n putchar(data[len]+48);\n putchar('\\n');\n}\n#pragma comment(linker,\"/STACK:124000000,124000000\")\n#include<map>\n#include<vector>\n#include<queue>\n#define eps 1e-10\n#define INF 2000000009\n#define maxn 150050\n#define maxm 2500010\n#define mod 1000000\n#define llINF 1LL<<60\nusing namespace std;\n\nconst int MAXN=1000;\n\n\n\nint a[MAXN][MAXN];//增广矩阵\nint x[MAXN];//解集\n//bool free_x[MAXN];//标记是否是不确定的变元\n\n\n\n/*\nvoid Debug(void)\n{\n int i, j;\n for (i = 0; i < equ; i++)\n {\n for (j = 0; j < var + 1; j++)\n {\n cout << a[i][j] << \" \";\n }\n cout << endl;\n }\n cout << endl;\n}\n*/\n\n\ninline int gcd(int a,int b)\n{\n int t;\n while(b!=0)\n {\n t=b;\n b=a%b;\n a=t;\n }\n return a;\n}\ninline int lcm(int a,int b)\n{\n return a/gcd(a,b)*b;//先除后乘防溢出\n}\n\n// 高斯消元法解方程组(Gauss-Jordan elimination).(-2表示有浮点数解,但无整数解,\n//-1表示无解,0表示唯一解,大于0表示无穷解,并返回自由变元的个数)\n//有equ个方程,var个变元。增广矩阵行数为equ,分别为0到equ-1,列数为var+1,分别为0到var.\nint Gauss(int equ,int var)\n{\n int i,j,k;\n int max_r;// 当前这列绝对值最大的行.\n int col;//当前处理的列\n int ta,tb;\n int LCM;\n int temp;\n int free_x_num;\n int free_index;\n bool free_x[MAXN];\n for(int i=0;i<=var;i++)\n {\n x[i]=0;\n free_x[i]=true;\n }\n\n //转换为阶梯阵.\n col=0; // 当前处理的列\n for(k = 0;k < equ && col < var;k++,col++)\n {// 枚举当前处理的行.\n// 找到该col列元素绝对值最大的那行与第k行交换.(为了在除法时减小误差)\n max_r=k;\n for(i=k+1;i<equ;i++)\n {\n if(abs(a[i][col])>abs(a[max_r][col])) max_r=i;\n }\n if(max_r!=k)\n {// 与第k行交换.\n for(j=k;j<var+1;j++) swap(a[k][j],a[max_r][j]);\n }\n if(a[k][col]==0)\n {// 说明该col列第k行以下全是0了,则处理当前行的下一列.\n k--;\n continue;\n }\n for(i=k+1;i<equ;i++)\n {// 枚举要删去的行.\n if(a[i][col]!=0)\n {\n LCM = lcm(abs(a[i][col]),abs(a[k][col]));\n ta = LCM/abs(a[i][col]);\n tb = LCM/abs(a[k][col]);\n if(a[i][col]*a[k][col]<0)tb=-tb;//异号的情况是相加\n for(j=col;j<var+1;j++)\n {\n a[i][j] = abs(a[i][j]*ta-a[k][j]*tb)%2;\n }\n }\n }\n }\n\n // Debug();\n\n // 1. 无解的情况: 化简的增广阵中存在(0, 0, ..., a)这样的行(a != 0).\n for (i = k; i < equ; i++)\n { // 对于无穷解来说,如果要判断哪些是自由变元,那么初等行变换中的交换就会影响,则要记录交换.\n if(((a[i][col]+2)%2)!= 0) return -1;\n }\n return 0;\n // 2. 无穷解的情况: 在var * (var + 1)的增广阵中出现(0, 0, ..., 0)这样的行,即说明没有形成严格的上三角阵.\n // 且出现的行数即为自由变元的个数.\n if (k < var)\n {\n // 首先,自由变元有var - k个,即不确定的变元至少有var - k个.\n for (i = k - 1; i >= 0; i--)\n {\n // 第i行一定不会是(0, 0, ..., 0)的情况,因为这样的行是在第k行到第equ行.\n // 同样,第i行一定不会是(0, 0, ..., a), a != 0的情况,这样的无解的.\n free_x_num = 0; // 用于判断该行中的不确定的变元的个数,如果超过1个,则无法求解,它们仍然为不确定的变元.\n for (j = 0; j < var; j++)\n {\n if (a[i][j] != 0 && free_x[j]) free_x_num++, free_index = j;\n }\n if (free_x_num > 1) continue; // 无法求解出确定的变元.\n // 说明就只有一个不确定的变元free_index,那么可以求解出该变元,且该变元是确定的.\n temp = a[i][var];\n for (j = 0; j < var; j++)\n {\n if (a[i][j] != 0 && j != free_index) temp -= a[i][j] * x[j];\n }\n x[free_index] = temp / a[i][free_index]; // 求出该变元.\n free_x[free_index] = 0; // 该变元是确定的.\n }\n return var - k; // 自由变元有var - k个.\n }\n // 3. 唯一解的情况: 在var * (var + 1)的增广阵中形成严格的上三角阵.\n // 计算出Xn-1, Xn-2 ... X0.\n for (i = var - 1; i >= 0; i--)\n {\n temp = a[i][var];\n for (j = i + 1; j < var; j++)\n {\n if (a[i][j] != 0) temp -= a[i][j] * x[j];\n }\n if (temp % a[i][i] != 0) return -2; // 说明有浮点数解,但无整数解.\n x[i] = temp / a[i][i];\n }\n return 0;\n}\nint main()\n{\n int n,m,d;\n while(scanf(\"%d%d%d\",&m,&n,&d))\n {\n if(n==0&&m==0&&d==0) break;\n int t=n*m;\n memset(a,0,sizeof(a));\n for(int i=0;i<n;i++)\n {\n for(int j=0;j<m;j++)\n {\n scanf(\"%d\",&a[i*m+j][t]);\n }\n }\n for(int i=0;i<n;i++)\n {\n for(int j=0;j<m;j++)\n {\n a[i*m+j][i*m+j]=1;\n for(int x=0;x<n;x++)\n {\n for(int y=0;y<m;y++)\n {\n if(abs(x-i)+abs(y-j)==d)\n {\n a[i*m+j][x*m+y]+=1;\n }\n }\n }\n }\n }\n// for(int i=0;i<t;i++)\n// {\n// for(int j=0;j<=t;j++)\n// printf(\"%d \",a[i][j]);\n// printf(\"\\n\");\n// }\n int ans=Gauss(t,t);\n if(ans>=0) printf(\"1\\n\");\n else printf(\"0\\n\");\n// for(int i=0;i<t;i++)\n// printf(\"%d \",x[i]);\n }\n return 0;\n}", "accuracy": 1, "time_ms": 230, "memory_kb": 7308, "score_of_the_acc": -0.5665, "final_rank": 17 }, { "submission_id": "aoj_1308_10689535", "code_snippet": "#include<cstdio>\n#include<iostream>\n#include<queue>\n#include<algorithm>\n#include<map>\n#include<vector>\n#include<cstring>\n#include<cmath>\ninline int Input(){\n\tint ret=0;bool isN=0;char c=getchar();\n\twhile(c<'0' || c>'9'){\n\t\tif(c=='-') isN=1;\n\t\tc=getchar();\n\t}\n\twhile(c>='0' && c<='9'){\n\t\tret=ret*10+c-'0';\n\t\tc=getchar();\n\t}\n\treturn isN?-ret:ret;\n}\n\ninline void Output(long long x){\n if(x<0){\n putchar('-');x=-x;\n }\n int len=0,data[20];\n while(x){\n data[len++]=x%10;x/=10;\n }\n if(!len) data[len++]=0;\n while(len--)\n putchar(data[len]+48);\n putchar('\\n');\n}\n#pragma comment(linker,\"/STACK:124000000,124000000\")\n#include<map>\n#include<vector>\n#include<queue>\n#define eps 1e-10\n#define INF 2000000009\n#define maxn 150050\n#define maxm 2500010\n#define mod 1000000\n#define llINF 1LL<<60\nusing namespace std;\n\nconst int MAXN=1000;\n\n\n\nint a[MAXN][MAXN];//增广矩阵\nint x[MAXN];//解集\n//bool free_x[MAXN];//标记是否是不确定的变元\n\n\n\n/*\nvoid Debug(void)\n{\n int i, j;\n for (i = 0; i < equ; i++)\n {\n for (j = 0; j < var + 1; j++)\n {\n cout << a[i][j] << \" \";\n }\n cout << endl;\n }\n cout << endl;\n}\n*/\n\n\ninline int gcd(int a,int b)\n{\n int t;\n while(b!=0)\n {\n t=b;\n b=a%b;\n a=t;\n }\n return a;\n}\ninline int lcm(int a,int b)\n{\n return a/gcd(a,b)*b;//先除后乘防溢出\n}\n\n// 高斯消元法解方程组(Gauss-Jordan elimination).(-2表示有浮点数解,但无整数解,\n//-1表示无解,0表示唯一解,大于0表示无穷解,并返回自由变元的个数)\n//有equ个方程,var个变元。增广矩阵行数为equ,分别为0到equ-1,列数为var+1,分别为0到var.\nint Gauss(int equ,int var)\n{\n int i,j,k;\n int max_r;// 当前这列绝对值最大的行.\n int col;//当前处理的列\n int ta,tb;\n int LCM;\n int temp;\n int free_x_num;\n int free_index;\n bool free_x[MAXN];\n for(int i=0;i<=var;i++)\n {\n x[i]=0;\n free_x[i]=true;\n }\n\n //转换为阶梯阵.\n col=0; // 当前处理的列\n for(k = 0;k < equ && col < var;k++,col++)\n {// 枚举当前处理的行.\n// 找到该col列元素绝对值最大的那行与第k行交换.(为了在除法时减小误差)\n max_r=k;\n for(i=k+1;i<equ;i++)\n {\n if(abs(a[i][col])>abs(a[max_r][col])) max_r=i;\n }\n if(max_r!=k)\n {// 与第k行交换.\n for(j=k;j<var+1;j++) swap(a[k][j],a[max_r][j]);\n }\n if(a[k][col]==0)\n {// 说明该col列第k行以下全是0了,则处理当前行的下一列.\n k--;\n continue;\n }\n for(i=k+1;i<equ;i++)\n {// 枚举要删去的行.\n if(a[i][col]!=0)\n {\n LCM = lcm(abs(a[i][col]),abs(a[k][col]));\n ta = LCM/abs(a[i][col]);\n tb = LCM/abs(a[k][col]);\n if(a[i][col]*a[k][col]<0)tb=-tb;//异号的情况是相加\n for(j=col;j<var+1;j++)\n {\n a[i][j] = abs(a[i][j]*ta-a[k][j]*tb)%2;\n }\n }\n }\n }\n\n // Debug();\n\n // 1. 无解的情况: 化简的增广阵中存在(0, 0, ..., a)这样的行(a != 0).\n for (i = k; i < equ; i++)\n { // 对于无穷解来说,如果要判断哪些是自由变元,那么初等行变换中的交换就会影响,则要记录交换.\n if(((a[i][col]+2)%2)!= 0) return -1;\n }\n return 0;\n // 2. 无穷解的情况: 在var * (var + 1)的增广阵中出现(0, 0, ..., 0)这样的行,即说明没有形成严格的上三角阵.\n // 且出现的行数即为自由变元的个数.\n if (k < var)\n {\n // 首先,自由变元有var - k个,即不确定的变元至少有var - k个.\n for (i = k - 1; i >= 0; i--)\n {\n // 第i行一定不会是(0, 0, ..., 0)的情况,因为这样的行是在第k行到第equ行.\n // 同样,第i行一定不会是(0, 0, ..., a), a != 0的情况,这样的无解的.\n free_x_num = 0; // 用于判断该行中的不确定的变元的个数,如果超过1个,则无法求解,它们仍然为不确定的变元.\n for (j = 0; j < var; j++)\n {\n if (a[i][j] != 0 && free_x[j]) free_x_num++, free_index = j;\n }\n if (free_x_num > 1) continue; // 无法求解出确定的变元.\n // 说明就只有一个不确定的变元free_index,那么可以求解出该变元,且该变元是确定的.\n temp = a[i][var];\n for (j = 0; j < var; j++)\n {\n if (a[i][j] != 0 && j != free_index) temp -= a[i][j] * x[j];\n }\n x[free_index] = temp / a[i][free_index]; // 求出该变元.\n free_x[free_index] = 0; // 该变元是确定的.\n }\n return var - k; // 自由变元有var - k个.\n }\n // 3. 唯一解的情况: 在var * (var + 1)的增广阵中形成严格的上三角阵.\n // 计算出Xn-1, Xn-2 ... X0.\n for (i = var - 1; i >= 0; i--)\n {\n temp = a[i][var];\n for (j = i + 1; j < var; j++)\n {\n if (a[i][j] != 0) temp -= a[i][j] * x[j];\n }\n if (temp % a[i][i] != 0) return -2; // 说明有浮点数解,但无整数解.\n x[i] = temp / a[i][i];\n }\n return 0;\n}\nint main()\n{\n int n,m,d;\n while(scanf(\"%d%d%d\",&m,&n,&d))\n {\n if(n==0&&m==0&&d==0) break;\n int t=n*m;\n memset(a,0,sizeof(a));\n for(int i=0;i<n;i++)\n {\n for(int j=0;j<m;j++)\n {\n scanf(\"%d\",&a[i*m+j][t]);\n }\n }\n for(int i=0;i<n;i++)\n {\n for(int j=0;j<m;j++)\n {\n a[i*m+j][i*m+j]=1;\n for(int x=0;x<n;x++)\n {\n for(int y=0;y<m;y++)\n {\n if(abs(x-i)+abs(y-j)==d)\n {\n a[i*m+j][x*m+y]=1;\n }\n }\n }\n }\n }\n// for(int i=0;i<t;i++)\n// {\n// for(int j=0;j<=t;j++)\n// printf(\"%d \",a[i][j]);\n// printf(\"\\n\");\n// }\n int ans=Gauss(t,t);\n if(ans>=0) printf(\"1\\n\");\n else printf(\"0\\n\");\n// for(int i=0;i<t;i++)\n// printf(\"%d \",x[i]);\n }\n return 0;\n}", "accuracy": 1, "time_ms": 230, "memory_kb": 7408, "score_of_the_acc": -0.5799, "final_rank": 18 }, { "submission_id": "aoj_1308_10689532", "code_snippet": "#include<cstdio>\n#include<iostream>\n#include<queue>\n#include<algorithm>\n#include<map>\n#include<vector>\n#include<cstring>\n#include<cmath>\ninline int Input(){\n\tint ret=0;bool isN=0;char c=getchar();\n\twhile(c<'0' || c>'9'){\n\t\tif(c=='-') isN=1;\n\t\tc=getchar();\n\t}\n\twhile(c>='0' && c<='9'){\n\t\tret=ret*10+c-'0';\n\t\tc=getchar();\n\t}\n\treturn isN?-ret:ret;\n}\n\ninline void Output(long long x){\n if(x<0){\n putchar('-');x=-x;\n }\n int len=0,data[20];\n while(x){\n data[len++]=x%10;x/=10;\n }\n if(!len) data[len++]=0;\n while(len--)\n putchar(data[len]+48);\n putchar('\\n');\n}\n#pragma comment(linker,\"/STACK:124000000,124000000\")\n#include<map>\n#include<vector>\n#include<queue>\n#define eps 1e-10\n#define INF 2000000009\n#define maxn 150050\n#define maxm 2500010\n#define mod 1000000\n#define llINF 1LL<<60\nusing namespace std;\n\nconst int MAXN=1000;\n\n\n\nint a[MAXN][MAXN];//增广矩阵\nint x[MAXN];//解集\n//bool free_x[MAXN];//标记是否是不确定的变元\n\n\n\n/*\nvoid Debug(void)\n{\n int i, j;\n for (i = 0; i < equ; i++)\n {\n for (j = 0; j < var + 1; j++)\n {\n cout << a[i][j] << \" \";\n }\n cout << endl;\n }\n cout << endl;\n}\n*/\n\n\ninline int gcd(int a,int b)\n{\n int t;\n while(b!=0)\n {\n t=b;\n b=a%b;\n a=t;\n }\n return a;\n}\ninline int lcm(int a,int b)\n{\n return a/gcd(a,b)*b;//先除后乘防溢出\n}\n\n// 高斯消元法解方程组(Gauss-Jordan elimination).(-2表示有浮点数解,但无整数解,\n//-1表示无解,0表示唯一解,大于0表示无穷解,并返回自由变元的个数)\n//有equ个方程,var个变元。增广矩阵行数为equ,分别为0到equ-1,列数为var+1,分别为0到var.\nint Gauss(int equ,int var)\n{\n int i,j,k;\n int max_r;// 当前这列绝对值最大的行.\n int col;//当前处理的列\n int ta,tb;\n int LCM;\n int temp;\n int free_x_num;\n int free_index;\n bool free_x[MAXN];\n for(int i=0;i<=var;i++)\n {\n x[i]=0;\n free_x[i]=true;\n }\n\n //转换为阶梯阵.\n col=0; // 当前处理的列\n for(k = 0;k < equ && col < var;k++,col++)\n {// 枚举当前处理的行.\n// 找到该col列元素绝对值最大的那行与第k行交换.(为了在除法时减小误差)\n max_r=k;\n for(i=k+1;i<equ;i++)\n {\n if(abs(a[i][col])>abs(a[max_r][col])) max_r=i;\n }\n if(max_r!=k)\n {// 与第k行交换.\n for(j=k;j<var+1;j++) swap(a[k][j],a[max_r][j]);\n }\n if(a[k][col]==0)\n {// 说明该col列第k行以下全是0了,则处理当前行的下一列.\n k--;\n continue;\n }\n for(i=k+1;i<equ;i++)\n {// 枚举要删去的行.\n if(a[i][col]!=0)\n {\n LCM = lcm(abs(a[i][col]),abs(a[k][col]));\n ta = LCM/abs(a[i][col]);\n tb = LCM/abs(a[k][col]);\n if(a[i][col]*a[k][col]<0)tb=-tb;//异号的情况是相加\n for(j=col;j<var+1;j++)\n {\n a[i][j] = abs(a[i][j]*ta-a[k][j]*tb)%2;\n }\n }\n }\n }\n\n // Debug();\n\n // 1. 无解的情况: 化简的增广阵中存在(0, 0, ..., a)这样的行(a != 0).\n for (i = k; i < equ; i++)\n { // 对于无穷解来说,如果要判断哪些是自由变元,那么初等行变换中的交换就会影响,则要记录交换.\n if(((a[i][col]+2)%2)!= 0) return -1;\n }\n return 0;\n // 2. 无穷解的情况: 在var * (var + 1)的增广阵中出现(0, 0, ..., 0)这样的行,即说明没有形成严格的上三角阵.\n // 且出现的行数即为自由变元的个数.\n if (k < var)\n {\n // 首先,自由变元有var - k个,即不确定的变元至少有var - k个.\n for (i = k - 1; i >= 0; i--)\n {\n // 第i行一定不会是(0, 0, ..., 0)的情况,因为这样的行是在第k行到第equ行.\n // 同样,第i行一定不会是(0, 0, ..., a), a != 0的情况,这样的无解的.\n free_x_num = 0; // 用于判断该行中的不确定的变元的个数,如果超过1个,则无法求解,它们仍然为不确定的变元.\n for (j = 0; j < var; j++)\n {\n if (a[i][j] != 0 && free_x[j]) free_x_num++, free_index = j;\n }\n if (free_x_num > 1) continue; // 无法求解出确定的变元.\n // 说明就只有一个不确定的变元free_index,那么可以求解出该变元,且该变元是确定的.\n temp = a[i][var];\n for (j = 0; j < var; j++)\n {\n if (a[i][j] != 0 && j != free_index) temp -= a[i][j] * x[j];\n }\n x[free_index] = temp / a[i][free_index]; // 求出该变元.\n free_x[free_index] = 0; // 该变元是确定的.\n }\n return var - k; // 自由变元有var - k个.\n }\n // 3. 唯一解的情况: 在var * (var + 1)的增广阵中形成严格的上三角阵.\n // 计算出Xn-1, Xn-2 ... X0.\n for (i = var - 1; i >= 0; i--)\n {\n temp = a[i][var];\n for (j = i + 1; j < var; j++)\n {\n if (a[i][j] != 0) temp -= a[i][j] * x[j];\n }\n if (temp % a[i][i] != 0) return -2; // 说明有浮点数解,但无整数解.\n x[i] = temp / a[i][i];\n }\n return 0;\n}\nint main()\n{\n int n,m,d;\n while(scanf(\"%d%d%d\",&m,&n,&d)&&n)\n {\n int t=n*m;\n memset(a,0,sizeof(a));\n for(int i=0;i<n;i++)\n {\n for(int j=0;j<m;j++)\n {\n scanf(\"%d\",&a[i*m+j][t]);\n }\n }\n for(int i=0;i<n;i++)\n {\n for(int j=0;j<m;j++)\n {\n a[i*m+j][i*m+j]=1;\n for(int x=0;x<n;x++)\n {\n for(int y=0;y<m;y++)\n {\n if(abs(x-i)+abs(y-j)==d)\n {\n a[i*m+j][x*m+y]=1;\n }\n }\n }\n }\n }\n// for(int i=0;i<t;i++)\n// {\n// for(int j=0;j<=t;j++)\n// printf(\"%d \",a[i][j]);\n// printf(\"\\n\");\n// }\n int ans=Gauss(t,t);\n if(ans>=0) printf(\"1\\n\");\n else printf(\"0\\n\");\n// for(int i=0;i<t;i++)\n// printf(\"%d \",x[i]);\n }\n return 0;\n}", "accuracy": 1, "time_ms": 240, "memory_kb": 7404, "score_of_the_acc": -0.5823, "final_rank": 19 }, { "submission_id": "aoj_1308_10689530", "code_snippet": "#include<cstdio>\n#include<iostream>\n#include<queue>\n#include<algorithm>\n#include<map>\n#include<vector>\n#include<cstring>\n#include<cmath>\ninline int Input(){\n\tint ret=0;bool isN=0;char c=getchar();\n\twhile(c<'0' || c>'9'){\n\t\tif(c=='-') isN=1;\n\t\tc=getchar();\n\t}\n\twhile(c>='0' && c<='9'){\n\t\tret=ret*10+c-'0';\n\t\tc=getchar();\n\t}\n\treturn isN?-ret:ret;\n}\n\ninline void Output(long long x){\n if(x<0){\n putchar('-');x=-x;\n }\n int len=0,data[20];\n while(x){\n data[len++]=x%10;x/=10;\n }\n if(!len) data[len++]=0;\n while(len--)\n putchar(data[len]+48);\n putchar('\\n');\n}\n#pragma comment(linker,\"/STACK:124000000,124000000\")\n#include<map>\n#include<vector>\n#include<queue>\n#define eps 1e-10\n#define INF 2000000009\n#define maxn 150050\n#define maxm 2500010\n#define mod 1000000\n#define llINF 1LL<<60\nusing namespace std;\n\nconst int MAXN=900;\n\n\n\nint a[MAXN][MAXN];//增广矩阵\nint x[MAXN];//解集\n//bool free_x[MAXN];//标记是否是不确定的变元\n\n\n\n/*\nvoid Debug(void)\n{\n int i, j;\n for (i = 0; i < equ; i++)\n {\n for (j = 0; j < var + 1; j++)\n {\n cout << a[i][j] << \" \";\n }\n cout << endl;\n }\n cout << endl;\n}\n*/\n\n\ninline int gcd(int a,int b)\n{\n int t;\n while(b!=0)\n {\n t=b;\n b=a%b;\n a=t;\n }\n return a;\n}\ninline int lcm(int a,int b)\n{\n return a/gcd(a,b)*b;//先除后乘防溢出\n}\n\n// 高斯消元法解方程组(Gauss-Jordan elimination).(-2表示有浮点数解,但无整数解,\n//-1表示无解,0表示唯一解,大于0表示无穷解,并返回自由变元的个数)\n//有equ个方程,var个变元。增广矩阵行数为equ,分别为0到equ-1,列数为var+1,分别为0到var.\nint Gauss(int equ,int var)\n{\n int i,j,k;\n int max_r;// 当前这列绝对值最大的行.\n int col;//当前处理的列\n int ta,tb;\n int LCM;\n int temp;\n int free_x_num;\n int free_index;\n bool free_x[MAXN];\n for(int i=0;i<=var;i++)\n {\n x[i]=0;\n free_x[i]=true;\n }\n\n //转换为阶梯阵.\n col=0; // 当前处理的列\n for(k = 0;k < equ && col < var;k++,col++)\n {// 枚举当前处理的行.\n// 找到该col列元素绝对值最大的那行与第k行交换.(为了在除法时减小误差)\n max_r=k;\n for(i=k+1;i<equ;i++)\n {\n if(abs(a[i][col])>abs(a[max_r][col])) max_r=i;\n }\n if(max_r!=k)\n {// 与第k行交换.\n for(j=k;j<var+1;j++) swap(a[k][j],a[max_r][j]);\n }\n if(a[k][col]==0)\n {// 说明该col列第k行以下全是0了,则处理当前行的下一列.\n k--;\n continue;\n }\n for(i=k+1;i<equ;i++)\n {// 枚举要删去的行.\n if(a[i][col]!=0)\n {\n LCM = lcm(abs(a[i][col]),abs(a[k][col]));\n ta = LCM/abs(a[i][col]);\n tb = LCM/abs(a[k][col]);\n if(a[i][col]*a[k][col]<0)tb=-tb;//异号的情况是相加\n for(j=col;j<var+1;j++)\n {\n a[i][j] = abs(a[i][j]*ta-a[k][j]*tb)%2;\n }\n }\n }\n }\n\n // Debug();\n\n // 1. 无解的情况: 化简的增广阵中存在(0, 0, ..., a)这样的行(a != 0).\n for (i = k; i < equ; i++)\n { // 对于无穷解来说,如果要判断哪些是自由变元,那么初等行变换中的交换就会影响,则要记录交换.\n if((a[i][col]%2)!= 0) return -1;\n }\n return 0;\n // 2. 无穷解的情况: 在var * (var + 1)的增广阵中出现(0, 0, ..., 0)这样的行,即说明没有形成严格的上三角阵.\n // 且出现的行数即为自由变元的个数.\n if (k < var)\n {\n // 首先,自由变元有var - k个,即不确定的变元至少有var - k个.\n for (i = k - 1; i >= 0; i--)\n {\n // 第i行一定不会是(0, 0, ..., 0)的情况,因为这样的行是在第k行到第equ行.\n // 同样,第i行一定不会是(0, 0, ..., a), a != 0的情况,这样的无解的.\n free_x_num = 0; // 用于判断该行中的不确定的变元的个数,如果超过1个,则无法求解,它们仍然为不确定的变元.\n for (j = 0; j < var; j++)\n {\n if (a[i][j] != 0 && free_x[j]) free_x_num++, free_index = j;\n }\n if (free_x_num > 1) continue; // 无法求解出确定的变元.\n // 说明就只有一个不确定的变元free_index,那么可以求解出该变元,且该变元是确定的.\n temp = a[i][var];\n for (j = 0; j < var; j++)\n {\n if (a[i][j] != 0 && j != free_index) temp -= a[i][j] * x[j];\n }\n x[free_index] = temp / a[i][free_index]; // 求出该变元.\n free_x[free_index] = 0; // 该变元是确定的.\n }\n return var - k; // 自由变元有var - k个.\n }\n // 3. 唯一解的情况: 在var * (var + 1)的增广阵中形成严格的上三角阵.\n // 计算出Xn-1, Xn-2 ... X0.\n for (i = var - 1; i >= 0; i--)\n {\n temp = a[i][var];\n for (j = i + 1; j < var; j++)\n {\n if (a[i][j] != 0) temp -= a[i][j] * x[j];\n }\n if (temp % a[i][i] != 0) return -2; // 说明有浮点数解,但无整数解.\n x[i] = temp / a[i][i];\n }\n return 0;\n}\nint main()\n{\n int n,m,d;\n while(scanf(\"%d%d%d\",&m,&n,&d)&&n)\n {\n int t=n*m;\n memset(a,0,sizeof(a));\n for(int i=0;i<n;i++)\n {\n for(int j=0;j<m;j++)\n {\n scanf(\"%d\",&a[i*m+j][t]);\n }\n }\n for(int i=0;i<n;i++)\n {\n for(int j=0;j<m;j++)\n {\n a[i*m+j][i*m+j]=1;\n for(int x=0;x<n;x++)\n {\n for(int y=0;y<m;y++)\n {\n if(abs(x-i)+abs(y-j)==d)\n {\n a[i*m+j][x*m+y]=1;\n }\n }\n }\n }\n }\n// for(int i=0;i<t;i++)\n// {\n// for(int j=0;j<=t;j++)\n// printf(\"%d \",a[i][j]);\n// printf(\"\\n\");\n// }\n int ans=Gauss(t,t);\n if(ans>=0) printf(\"1\\n\");\n else printf(\"0\\n\");\n// for(int i=0;i<t;i++)\n// printf(\"%d \",x[i]);\n }\n return 0;\n}", "accuracy": 1, "time_ms": 230, "memory_kb": 6416, "score_of_the_acc": -0.4465, "final_rank": 12 }, { "submission_id": "aoj_1308_10689529", "code_snippet": "#include<cstdio>\n#include<iostream>\n#include<queue>\n#include<algorithm>\n#include<map>\n#include<vector>\n#include<cstring>\n#include<cmath>\ninline int Input(){\n\tint ret=0;bool isN=0;char c=getchar();\n\twhile(c<'0' || c>'9'){\n\t\tif(c=='-') isN=1;\n\t\tc=getchar();\n\t}\n\twhile(c>='0' && c<='9'){\n\t\tret=ret*10+c-'0';\n\t\tc=getchar();\n\t}\n\treturn isN?-ret:ret;\n}\n\ninline void Output(long long x){\n if(x<0){\n putchar('-');x=-x;\n }\n int len=0,data[20];\n while(x){\n data[len++]=x%10;x/=10;\n }\n if(!len) data[len++]=0;\n while(len--)\n putchar(data[len]+48);\n putchar('\\n');\n}\n#pragma comment(linker,\"/STACK:124000000,124000000\")\n#include<map>\n#include<vector>\n#include<queue>\n#define eps 1e-10\n#define INF 2000000009\n#define maxn 150050\n#define maxm 2500010\n#define mod 1000000\n#define llINF 1LL<<60\nusing namespace std;\n\nconst int MAXN=900;\n\n\n\nint a[MAXN][MAXN];//增广矩阵\nint x[MAXN];//解集\n//bool free_x[MAXN];//标记是否是不确定的变元\n\n\n\n/*\nvoid Debug(void)\n{\n int i, j;\n for (i = 0; i < equ; i++)\n {\n for (j = 0; j < var + 1; j++)\n {\n cout << a[i][j] << \" \";\n }\n cout << endl;\n }\n cout << endl;\n}\n*/\n\n\ninline int gcd(int a,int b)\n{\n int t;\n while(b!=0)\n {\n t=b;\n b=a%b;\n a=t;\n }\n return a;\n}\ninline int lcm(int a,int b)\n{\n return a/gcd(a,b)*b;//先除后乘防溢出\n}\n\n// 高斯消元法解方程组(Gauss-Jordan elimination).(-2表示有浮点数解,但无整数解,\n//-1表示无解,0表示唯一解,大于0表示无穷解,并返回自由变元的个数)\n//有equ个方程,var个变元。增广矩阵行数为equ,分别为0到equ-1,列数为var+1,分别为0到var.\nint Gauss(int equ,int var)\n{\n int i,j,k;\n int max_r;// 当前这列绝对值最大的行.\n int col;//当前处理的列\n int ta,tb;\n int LCM;\n int temp;\n int free_x_num;\n int free_index;\n bool free_x[MAXN];\n for(int i=0;i<=var;i++)\n {\n x[i]=0;\n free_x[i]=true;\n }\n\n //转换为阶梯阵.\n col=0; // 当前处理的列\n for(k = 0;k < equ && col < var;k++,col++)\n {// 枚举当前处理的行.\n// 找到该col列元素绝对值最大的那行与第k行交换.(为了在除法时减小误差)\n max_r=k;\n for(i=k+1;i<equ;i++)\n {\n if(abs(a[i][col])>abs(a[max_r][col])) max_r=i;\n }\n if(max_r!=k)\n {// 与第k行交换.\n for(j=k;j<var+1;j++) swap(a[k][j],a[max_r][j]);\n }\n if(a[k][col]==0)\n {// 说明该col列第k行以下全是0了,则处理当前行的下一列.\n k--;\n continue;\n }\n for(i=k+1;i<equ;i++)\n {// 枚举要删去的行.\n if(a[i][col]!=0)\n {\n LCM = lcm(abs(a[i][col]),abs(a[k][col]));\n ta = LCM/abs(a[i][col]);\n tb = LCM/abs(a[k][col]);\n if(a[i][col]*a[k][col]<0)tb=-tb;//异号的情况是相加\n for(j=col;j<var+1;j++)\n {\n a[i][j] = abs(a[i][j]*ta-a[k][j]*tb)%2;\n }\n }\n }\n }\n\n // Debug();\n\n // 1. 无解的情况: 化简的增广阵中存在(0, 0, ..., a)这样的行(a != 0).\n for (i = k; i < equ; i++)\n { // 对于无穷解来说,如果要判断哪些是自由变元,那么初等行变换中的交换就会影响,则要记录交换.\n if (a[i][col]!= 0) return -1;\n }\n return 0;\n // 2. 无穷解的情况: 在var * (var + 1)的增广阵中出现(0, 0, ..., 0)这样的行,即说明没有形成严格的上三角阵.\n // 且出现的行数即为自由变元的个数.\n if (k < var)\n {\n // 首先,自由变元有var - k个,即不确定的变元至少有var - k个.\n for (i = k - 1; i >= 0; i--)\n {\n // 第i行一定不会是(0, 0, ..., 0)的情况,因为这样的行是在第k行到第equ行.\n // 同样,第i行一定不会是(0, 0, ..., a), a != 0的情况,这样的无解的.\n free_x_num = 0; // 用于判断该行中的不确定的变元的个数,如果超过1个,则无法求解,它们仍然为不确定的变元.\n for (j = 0; j < var; j++)\n {\n if (a[i][j] != 0 && free_x[j]) free_x_num++, free_index = j;\n }\n if (free_x_num > 1) continue; // 无法求解出确定的变元.\n // 说明就只有一个不确定的变元free_index,那么可以求解出该变元,且该变元是确定的.\n temp = a[i][var];\n for (j = 0; j < var; j++)\n {\n if (a[i][j] != 0 && j != free_index) temp -= a[i][j] * x[j];\n }\n x[free_index] = temp / a[i][free_index]; // 求出该变元.\n free_x[free_index] = 0; // 该变元是确定的.\n }\n return var - k; // 自由变元有var - k个.\n }\n // 3. 唯一解的情况: 在var * (var + 1)的增广阵中形成严格的上三角阵.\n // 计算出Xn-1, Xn-2 ... X0.\n for (i = var - 1; i >= 0; i--)\n {\n temp = a[i][var];\n for (j = i + 1; j < var; j++)\n {\n if (a[i][j] != 0) temp -= a[i][j] * x[j];\n }\n if (temp % a[i][i] != 0) return -2; // 说明有浮点数解,但无整数解.\n x[i] = temp / a[i][i];\n }\n return 0;\n}\nint main()\n{\n int n,m,d;\n while(scanf(\"%d%d%d\",&m,&n,&d)&&n)\n {\n int t=n*m;\n memset(a,0,sizeof(a));\n for(int i=0;i<n;i++)\n {\n for(int j=0;j<m;j++)\n {\n scanf(\"%d\",&a[i*m+j][t]);\n }\n }\n for(int i=0;i<n;i++)\n {\n for(int j=0;j<m;j++)\n {\n a[i*m+j][i*m+j]=1;\n for(int x=0;x<n;x++)\n {\n for(int y=0;y<m;y++)\n {\n if(abs(x-i)+abs(y-j)==d)\n {\n a[i*m+j][x*m+y]=1;\n }\n }\n }\n }\n }\n// for(int i=0;i<t;i++)\n// {\n// for(int j=0;j<=t;j++)\n// printf(\"%d \",a[i][j]);\n// printf(\"\\n\");\n// }\n int ans=Gauss(t,t);\n if(ans>=0) printf(\"1\\n\");\n else printf(\"0\\n\");\n// for(int i=0;i<t;i++)\n// printf(\"%d \",x[i]);\n }\n return 0;\n}", "accuracy": 1, "time_ms": 230, "memory_kb": 6420, "score_of_the_acc": -0.447, "final_rank": 13 }, { "submission_id": "aoj_1308_10689528", "code_snippet": "#include<cstdio>\n#include<iostream>\n#include<queue>\n#include<algorithm>\n#include<map>\n#include<vector>\n#include<cstring>\n#include<cmath>\ninline int Input(){\n\tint ret=0;bool isN=0;char c=getchar();\n\twhile(c<'0' || c>'9'){\n\t\tif(c=='-') isN=1;\n\t\tc=getchar();\n\t}\n\twhile(c>='0' && c<='9'){\n\t\tret=ret*10+c-'0';\n\t\tc=getchar();\n\t}\n\treturn isN?-ret:ret;\n}\n\ninline void Output(long long x){\n if(x<0){\n putchar('-');x=-x;\n }\n int len=0,data[20];\n while(x){\n data[len++]=x%10;x/=10;\n }\n if(!len) data[len++]=0;\n while(len--)\n putchar(data[len]+48);\n putchar('\\n');\n}\n#pragma comment(linker,\"/STACK:124000000,124000000\")\n#include<map>\n#include<vector>\n#include<queue>\n#define eps 1e-10\n#define INF 2000000009\n#define maxn 150050\n#define maxm 2500010\n#define mod 1000000\n#define llINF 1LL<<60\nusing namespace std;\n\nconst int MAXN=900;\n\n\n\nint a[MAXN][MAXN];//增广矩阵\nint x[MAXN];//解集\n//bool free_x[MAXN];//标记是否是不确定的变元\n\n\n\n/*\nvoid Debug(void)\n{\n int i, j;\n for (i = 0; i < equ; i++)\n {\n for (j = 0; j < var + 1; j++)\n {\n cout << a[i][j] << \" \";\n }\n cout << endl;\n }\n cout << endl;\n}\n*/\n\n\ninline int gcd(int a,int b)\n{\n int t;\n while(b!=0)\n {\n t=b;\n b=a%b;\n a=t;\n }\n return a;\n}\ninline int lcm(int a,int b)\n{\n return a/gcd(a,b)*b;//先除后乘防溢出\n}\n\n// 高斯消元法解方程组(Gauss-Jordan elimination).(-2表示有浮点数解,但无整数解,\n//-1表示无解,0表示唯一解,大于0表示无穷解,并返回自由变元的个数)\n//有equ个方程,var个变元。增广矩阵行数为equ,分别为0到equ-1,列数为var+1,分别为0到var.\nint Gauss(int equ,int var)\n{\n int i,j,k;\n int max_r;// 当前这列绝对值最大的行.\n int col;//当前处理的列\n int ta,tb;\n int LCM;\n int temp;\n int free_x_num;\n int free_index;\n bool free_x[MAXN];\n for(int i=0;i<=var;i++)\n {\n x[i]=0;\n free_x[i]=true;\n }\n\n //转换为阶梯阵.\n col=0; // 当前处理的列\n for(k = 0;k < equ && col < var;k++,col++)\n {// 枚举当前处理的行.\n// 找到该col列元素绝对值最大的那行与第k行交换.(为了在除法时减小误差)\n max_r=k;\n for(i=k+1;i<equ;i++)\n {\n if(abs(a[i][col])>abs(a[max_r][col])) max_r=i;\n }\n if(max_r!=k)\n {// 与第k行交换.\n for(j=k;j<var+1;j++) swap(a[k][j],a[max_r][j]);\n }\n if(a[k][col]==0)\n {// 说明该col列第k行以下全是0了,则处理当前行的下一列.\n k--;\n continue;\n }\n for(i=k+1;i<equ;i++)\n {// 枚举要删去的行.\n if(a[i][col]!=0)\n {\n LCM = lcm(abs(a[i][col]),abs(a[k][col]));\n ta = LCM/abs(a[i][col]);\n tb = LCM/abs(a[k][col]);\n if(a[i][col]*a[k][col]<0)tb=-tb;//异号的情况是相加\n for(j=col;j<var+1;j++)\n {\n a[i][j] = (a[i][j]*ta-a[k][j]*tb)%2;\n }\n }\n }\n }\n\n // Debug();\n\n // 1. 无解的情况: 化简的增广阵中存在(0, 0, ..., a)这样的行(a != 0).\n for (i = k; i < equ; i++)\n { // 对于无穷解来说,如果要判断哪些是自由变元,那么初等行变换中的交换就会影响,则要记录交换.\n if (a[i][col] != 0) return -1;\n }\n return 0;\n // 2. 无穷解的情况: 在var * (var + 1)的增广阵中出现(0, 0, ..., 0)这样的行,即说明没有形成严格的上三角阵.\n // 且出现的行数即为自由变元的个数.\n if (k < var)\n {\n // 首先,自由变元有var - k个,即不确定的变元至少有var - k个.\n for (i = k - 1; i >= 0; i--)\n {\n // 第i行一定不会是(0, 0, ..., 0)的情况,因为这样的行是在第k行到第equ行.\n // 同样,第i行一定不会是(0, 0, ..., a), a != 0的情况,这样的无解的.\n free_x_num = 0; // 用于判断该行中的不确定的变元的个数,如果超过1个,则无法求解,它们仍然为不确定的变元.\n for (j = 0; j < var; j++)\n {\n if (a[i][j] != 0 && free_x[j]) free_x_num++, free_index = j;\n }\n if (free_x_num > 1) continue; // 无法求解出确定的变元.\n // 说明就只有一个不确定的变元free_index,那么可以求解出该变元,且该变元是确定的.\n temp = a[i][var];\n for (j = 0; j < var; j++)\n {\n if (a[i][j] != 0 && j != free_index) temp -= a[i][j] * x[j];\n }\n x[free_index] = temp / a[i][free_index]; // 求出该变元.\n free_x[free_index] = 0; // 该变元是确定的.\n }\n return var - k; // 自由变元有var - k个.\n }\n // 3. 唯一解的情况: 在var * (var + 1)的增广阵中形成严格的上三角阵.\n // 计算出Xn-1, Xn-2 ... X0.\n for (i = var - 1; i >= 0; i--)\n {\n temp = a[i][var];\n for (j = i + 1; j < var; j++)\n {\n if (a[i][j] != 0) temp -= a[i][j] * x[j];\n }\n if (temp % a[i][i] != 0) return -2; // 说明有浮点数解,但无整数解.\n x[i] = temp / a[i][i];\n }\n return 0;\n}\nint main()\n{\n int n,m,d;\n while(scanf(\"%d%d%d\",&m,&n,&d)&&n)\n {\n int t=n*m;\n memset(a,0,sizeof(a));\n for(int i=0;i<n;i++)\n {\n for(int j=0;j<m;j++)\n {\n scanf(\"%d\",&a[i*m+j][t]);\n }\n }\n for(int i=0;i<n;i++)\n {\n for(int j=0;j<m;j++)\n {\n a[i*m+j][i*m+j]=1;\n for(int x=0;x<n;x++)\n {\n for(int y=0;y<m;y++)\n {\n if(abs(x-i)+abs(y-j)==d)\n {\n a[i*m+j][x*m+y]=1;\n }\n }\n }\n }\n }\n// for(int i=0;i<t;i++)\n// {\n// for(int j=0;j<=t;j++)\n// printf(\"%d \",a[i][j]);\n// printf(\"\\n\");\n// }\n int ans=Gauss(t,t);\n if(ans>=0) printf(\"1\\n\");\n else printf(\"0\\n\");\n// for(int i=0;i<t;i++)\n// printf(\"%d \",x[i]);\n }\n return 0;\n}", "accuracy": 1, "time_ms": 230, "memory_kb": 6404, "score_of_the_acc": -0.4448, "final_rank": 11 }, { "submission_id": "aoj_1308_10689527", "code_snippet": "#include<cstdio>\n#include<iostream>\n#include<queue>\n#include<algorithm>\n#include<map>\n#include<vector>\n#include<cstring>\n#include<cmath>\ninline int Input(){\n\tint ret=0;bool isN=0;char c=getchar();\n\twhile(c<'0' || c>'9'){\n\t\tif(c=='-') isN=1;\n\t\tc=getchar();\n\t}\n\twhile(c>='0' && c<='9'){\n\t\tret=ret*10+c-'0';\n\t\tc=getchar();\n\t}\n\treturn isN?-ret:ret;\n}\n\ninline void Output(long long x){\n if(x<0){\n putchar('-');x=-x;\n }\n int len=0,data[20];\n while(x){\n data[len++]=x%10;x/=10;\n }\n if(!len) data[len++]=0;\n while(len--)\n putchar(data[len]+48);\n putchar('\\n');\n}\n#pragma comment(linker,\"/STACK:124000000,124000000\")\n#include<map>\n#include<vector>\n#include<queue>\n#define eps 1e-10\n#define INF 2000000009\n#define maxn 150050\n#define maxm 2500010\n#define mod 1000000\n#define llINF 1LL<<60\nusing namespace std;\n\nconst int MAXN=900;\n\n\n\nint a[MAXN][MAXN];//增广矩阵\nint x[MAXN];//解集\nbool free_x[MAXN];//标记是否是不确定的变元\n\n\n\n/*\nvoid Debug(void)\n{\n int i, j;\n for (i = 0; i < equ; i++)\n {\n for (j = 0; j < var + 1; j++)\n {\n cout << a[i][j] << \" \";\n }\n cout << endl;\n }\n cout << endl;\n}\n*/\n\n\ninline int gcd(int a,int b)\n{\n int t;\n while(b!=0)\n {\n t=b;\n b=a%b;\n a=t;\n }\n return a;\n}\ninline int lcm(int a,int b)\n{\n return a/gcd(a,b)*b;//先除后乘防溢出\n}\n\n// 高斯消元法解方程组(Gauss-Jordan elimination).(-2表示有浮点数解,但无整数解,\n//-1表示无解,0表示唯一解,大于0表示无穷解,并返回自由变元的个数)\n//有equ个方程,var个变元。增广矩阵行数为equ,分别为0到equ-1,列数为var+1,分别为0到var.\nint Gauss(int equ,int var)\n{\n int i,j,k;\n int max_r;// 当前这列绝对值最大的行.\n int col;//当前处理的列\n int ta,tb;\n int LCM;\n int temp;\n int free_x_num;\n int free_index;\n\n for(int i=0;i<=var;i++)\n {\n x[i]=0;\n free_x[i]=true;\n }\n\n //转换为阶梯阵.\n col=0; // 当前处理的列\n for(k = 0;k < equ && col < var;k++,col++)\n {// 枚举当前处理的行.\n// 找到该col列元素绝对值最大的那行与第k行交换.(为了在除法时减小误差)\n max_r=k;\n for(i=k+1;i<equ;i++)\n {\n if(abs(a[i][col])>abs(a[max_r][col])) max_r=i;\n }\n if(max_r!=k)\n {// 与第k行交换.\n for(j=k;j<var+1;j++) swap(a[k][j],a[max_r][j]);\n }\n if(a[k][col]==0)\n {// 说明该col列第k行以下全是0了,则处理当前行的下一列.\n k--;\n continue;\n }\n for(i=k+1;i<equ;i++)\n {// 枚举要删去的行.\n if(a[i][col]!=0)\n {\n LCM = lcm(abs(a[i][col]),abs(a[k][col]));\n ta = LCM/abs(a[i][col]);\n tb = LCM/abs(a[k][col]);\n if(a[i][col]*a[k][col]<0)tb=-tb;//异号的情况是相加\n for(j=col;j<var+1;j++)\n {\n a[i][j] = (a[i][j]*ta-a[k][j]*tb)%2;\n }\n }\n }\n }\n\n // Debug();\n\n // 1. 无解的情况: 化简的增广阵中存在(0, 0, ..., a)这样的行(a != 0).\n for (i = k; i < equ; i++)\n { // 对于无穷解来说,如果要判断哪些是自由变元,那么初等行变换中的交换就会影响,则要记录交换.\n if (a[i][col] != 0) return -1;\n }\n return 0;\n // 2. 无穷解的情况: 在var * (var + 1)的增广阵中出现(0, 0, ..., 0)这样的行,即说明没有形成严格的上三角阵.\n // 且出现的行数即为自由变元的个数.\n if (k < var)\n {\n // 首先,自由变元有var - k个,即不确定的变元至少有var - k个.\n for (i = k - 1; i >= 0; i--)\n {\n // 第i行一定不会是(0, 0, ..., 0)的情况,因为这样的行是在第k行到第equ行.\n // 同样,第i行一定不会是(0, 0, ..., a), a != 0的情况,这样的无解的.\n free_x_num = 0; // 用于判断该行中的不确定的变元的个数,如果超过1个,则无法求解,它们仍然为不确定的变元.\n for (j = 0; j < var; j++)\n {\n if (a[i][j] != 0 && free_x[j]) free_x_num++, free_index = j;\n }\n if (free_x_num > 1) continue; // 无法求解出确定的变元.\n // 说明就只有一个不确定的变元free_index,那么可以求解出该变元,且该变元是确定的.\n temp = a[i][var];\n for (j = 0; j < var; j++)\n {\n if (a[i][j] != 0 && j != free_index) temp -= a[i][j] * x[j];\n }\n x[free_index] = temp / a[i][free_index]; // 求出该变元.\n free_x[free_index] = 0; // 该变元是确定的.\n }\n return var - k; // 自由变元有var - k个.\n }\n // 3. 唯一解的情况: 在var * (var + 1)的增广阵中形成严格的上三角阵.\n // 计算出Xn-1, Xn-2 ... X0.\n for (i = var - 1; i >= 0; i--)\n {\n temp = a[i][var];\n for (j = i + 1; j < var; j++)\n {\n if (a[i][j] != 0) temp -= a[i][j] * x[j];\n }\n if (temp % a[i][i] != 0) return -2; // 说明有浮点数解,但无整数解.\n x[i] = temp / a[i][i];\n }\n return 0;\n}\nint main()\n{\n int n,m,d;\n while(scanf(\"%d%d%d\",&m,&n,&d)&&n)\n {\n int t=n*m;\n memset(a,0,sizeof(a));\n for(int i=0;i<n;i++)\n {\n for(int j=0;j<m;j++)\n {\n scanf(\"%d\",&a[i*m+j][t]);\n }\n }\n for(int i=0;i<n;i++)\n {\n for(int j=0;j<m;j++)\n {\n a[i*m+j][i*m+j]=1;\n for(int x=0;x<n;x++)\n {\n for(int y=0;y<m;y++)\n {\n if(abs(x-i)+abs(y-j)==d)\n {\n a[i*m+j][x*m+y]=1;\n }\n }\n }\n }\n }\n// for(int i=0;i<t;i++)\n// {\n// for(int j=0;j<=t;j++)\n// printf(\"%d \",a[i][j]);\n// printf(\"\\n\");\n// }\n int ans=Gauss(t,t);\n if(ans>=0) printf(\"1\\n\");\n else printf(\"0\\n\");\n// for(int i=0;i<t;i++)\n// printf(\"%d \",x[i]);\n }\n return 0;\n}", "accuracy": 1, "time_ms": 270, "memory_kb": 6672, "score_of_the_acc": -0.4926, "final_rank": 15 }, { "submission_id": "aoj_1308_10689522", "code_snippet": "#include<cstdio>\n#include<iostream>\n#include<queue>\n#include<algorithm>\n#include<map>\n#include<vector>\n#include<cstring>\n#include<cmath>\ninline int Input(){\n\tint ret=0;bool isN=0;char c=getchar();\n\twhile(c<'0' || c>'9'){\n\t\tif(c=='-') isN=1;\n\t\tc=getchar();\n\t}\n\twhile(c>='0' && c<='9'){\n\t\tret=ret*10+c-'0';\n\t\tc=getchar();\n\t}\n\treturn isN?-ret:ret;\n}\n\ninline void Output(long long x){\n if(x<0){\n putchar('-');x=-x;\n }\n int len=0,data[20];\n while(x){\n data[len++]=x%10;x/=10;\n }\n if(!len) data[len++]=0;\n while(len--)\n putchar(data[len]+48);\n putchar('\\n');\n}\n#pragma comment(linker,\"/STACK:124000000,124000000\")\n#include<map>\n#include<vector>\n#include<queue>\n#define eps 1e-10\n#define INF 2000000009\n#define maxn 150050\n#define maxm 2500010\n#define mod 1000000\n#define llINF 1LL<<60\nusing namespace std;\nconst int MAXN=900;\n\n\n\nint a[MAXN][MAXN];//增广矩阵\nint x[MAXN];//解集\nbool free_x[MAXN];//标记是否是不确定的变元\n\n\n\n/*\nvoid Debug(void)\n{\n int i, j;\n for (i = 0; i < equ; i++)\n {\n for (j = 0; j < var + 1; j++)\n {\n cout << a[i][j] << \" \";\n }\n cout << endl;\n }\n cout << endl;\n}\n*/\n\n\ninline int gcd(int a,int b)\n{\n int t;\n while(b!=0)\n {\n t=b;\n b=a%b;\n a=t;\n }\n return a;\n}\ninline int lcm(int a,int b)\n{\n return a/gcd(a,b)*b;//先除后乘防溢出\n}\n\n// 高斯消元法解方程组(Gauss-Jordan elimination).(-2表示有浮点数解,但无整数解,\n//-1表示无解,0表示唯一解,大于0表示无穷解,并返回自由变元的个数)\n//有equ个方程,var个变元。增广矩阵行数为equ,分别为0到equ-1,列数为var+1,分别为0到var.\nint Gauss(int equ,int var)\n{\n\n int i,j,k;\n int max_r;// 当前这列绝对值最大的行.\n int col;//当前处理的列\n int ta,tb;\n int LCM;\n int temp;\n int free_x_num;\n int free_index;\n\n for(int i=0;i<=var;i++)\n {\n x[i]=0;\n free_x[i]=true;\n }\n\n //转换为阶梯阵.\n col=0; // 当前处理的列\n for(k = 0;k < equ && col < var;k++,col++)\n {// 枚举当前处理的行.\n// 找到该col列元素绝对值最大的那行与第k行交换.(为了在除法时减小误差)\n max_r=k;\n for(i=k+1;i<equ;i++)\n {\n if(abs(a[i][col])>abs(a[max_r][col])) max_r=i;\n }\n if(max_r!=k)\n {// 与第k行交换.\n for(j=k;j<var+1;j++) swap(a[k][j],a[max_r][j]);\n }\n if(a[k][col]==0)\n {// 说明该col列第k行以下全是0了,则处理当前行的下一列.\n k--;\n continue;\n }\n for(i=k+1;i<equ;i++)\n {// 枚举要删去的行.\n if(a[i][col]!=0)\n {\n LCM = lcm(abs(a[i][col]),abs(a[k][col]));\n ta = LCM/abs(a[i][col]);\n tb = LCM/abs(a[k][col]);\n if(a[i][col]*a[k][col]<0)tb=-tb;//异号的情况是相加\n for(j=col;j<var+1;j++)\n {\n a[i][j] = (a[i][j]*ta-a[k][j]*tb)%2;\n }\n }\n }\n }\n// for(int i=0;i<equ;i++)\n// {\n// for(int j=0;j<=equ;j++)\n// printf(\"%d \",a[i][j]);\n// printf(\"\\n\");\n// }\n// Debug();\n//\n// 1. 无解的情况: 化简的增广阵中存在(0, 0, ..., a)这样的行(a != 0).\n// printf(\"%d\\n\",k);\n for (i = k; i < equ; i++)\n { // 对于无穷解来说,如果要判断哪些是自由变元,那么初等行变换中的交换就会影响,则要记录交换.\n if (a[i][col] %2!= 0) return -1;\n }\n return 0;\n// for(int i=0;i<equ;i++)\n// printf(\"%d \",a[i][var]);\n // 2. 无穷解的情况: 在var * (var + 1)的增广阵中出现(0, 0, ..., 0)这样的行,即说明没有形成严格的上三角阵.\n // 且出现的行数即为自由变元的个数.\n if (k < var)\n {\n // 首先,自由变元有var - k个,即不确定的变元至少有var - k个.\n for (i = k - 1; i >= 0; i--)\n {\n // 第i行一定不会是(0, 0, ..., 0)的情况,因为这样的行是在第k行到第equ行.\n // 同样,第i行一定不会是(0, 0, ..., a), a != 0的情况,这样的无解的.\n free_x_num = 0; // 用于判断该行中的不确定的变元的个数,如果超过1个,则无法求解,它们仍然为不确定的变元.\n for (j = 0; j < var; j++)\n {\n if (a[i][j] != 0 && free_x[j]) free_x_num++, free_index = j;\n }\n if (free_x_num > 1) continue; // 无法求解出确定的变元.\n // 说明就只有一个不确定的变元free_index,那么可以求解出该变元,且该变元是确定的.\n temp = a[i][var];\n for (j = 0; j < var; j++)\n {\n if (a[i][j] != 0 && j != free_index) temp -= a[i][j] * x[j];\n }\n x[free_index] = temp / a[i][free_index]; // 求出该变元.\n free_x[free_index] = 0; // 该变元是确定的.\n }\n return var - k; // 自由变元有var - k个.\n }\n // 3. 唯一解的情况: 在var * (var + 1)的增广阵中形成严格的上三角阵.\n // 计算出Xn-1, Xn-2 ... X0.\n for (i = var - 1; i >= 0; i--)\n {\n temp = a[i][var];\n for (j = i + 1; j < var; j++)\n {\n if (a[i][j] != 0) temp -= a[i][j] * x[j];\n }\n if (temp % a[i][i] != 0) return -2; // 说明有浮点数解,但无整数解.\n x[i] = temp / a[i][i];\n }\n return 0;\n}\nint main()\n{\n int n,m,d;\n while(scanf(\"%d%d%d\",&m,&n,&d)&&n)\n {\n int t=n*m;\n memset(a,0,sizeof(a));\n for(int i=0;i<n;i++)\n {\n for(int j=0;j<m;j++)\n {\n scanf(\"%d\",&a[i*m+j][t]);\n }\n }\n for(int i=0;i<n;i++)\n {\n for(int j=0;j<m;j++)\n {\n a[i*m+j][i*m+j]=1;\n for(int x=0;x<n;x++)\n {\n for(int y=0;y<m;y++)\n {\n if(abs(x-i)+abs(y-j)==d)\n {\n a[i*m+j][x*m+y]=1;\n }\n }\n }\n }\n }\n// for(int i=0;i<t;i++)\n// {\n// for(int j=0;j<=t;j++)\n// printf(\"%d \",a[i][j]);\n// printf(\"\\n\");\n// }\n int ans=Gauss(t,t);\n if(ans>=0) printf(\"1\\n\");\n else printf(\"0\\n\");\n// for(int i=0;i<t;i++)\n// printf(\"%d \",x[i]);\n }\n return 0;\n}", "accuracy": 1, "time_ms": 270, "memory_kb": 6664, "score_of_the_acc": -0.4915, "final_rank": 14 }, { "submission_id": "aoj_1308_10299609", "code_snippet": "#include<bits/stdc++.h>\n// #include<atcoder/all>\n// #include<boost/multiprecision/cpp_int.hpp>\n\nusing namespace std;\n// using namespace atcoder;\n// using bint = boost::multiprecision::cpp_int;\nusing ll = long long;\nusing ull = unsigned long long;\nusing ld = long double;\nusing pii = pair<int,int>;\nusing pll = pair<ll,ll>;\nusing vi = vector<ll>;\nusing vvi = vector<vi>;\nusing vvvi = vector<vvi>;\nusing ve = vector<vector<int>>;\nusing vb = vector<bool>;\nusing vvb = vector<vb>;\n#define rep(i,s,n) for(ll i = (ll)s;i < (ll)n;i++)\n#define rrep(i,l,r) for(ll i = r-1;i >= l;i--)\n#define ALL(x) (x).begin(),(x).end()\n#define sz(c) ((ll)(c).size())\n#define LB(A,x) (int)(lower_bound(A.begin(),A.end(),x)-A.begin())\n#define UB(A,x) (int)(upper_bound(A.begin(),A.end(),x)-A.begin())\n// #define MOD 1000000007\n#define MOD 998244353\ntemplate<typename T>using min_priority_queue=priority_queue<T,vector<T>,greater<T>>;\ntemplate<typename T>ostream&operator<<(ostream&os,vector<T>&v){for(int i = 0;i < v.size();i++)os<<v[i]<<(i+1!=v.size()?\" \":\"\");return os;}\ntemplate<typename T>istream&operator>>(istream&is,vector<T>&v){for(T&in:v)is>>in;return is;}\ntemplate<typename T1,typename T2>ostream&operator<<(ostream&os,pair<T1,T2>&p){os<<p.first<<\" \"<<p.second;return os;}\ntemplate<typename T1,typename T2>istream&operator>>(istream&is,pair<T1,T2>&p){is>>p.first>>p.second;return is;}\ntemplate<typename T> inline bool chmax(T &a,T b){if(a < b){a = b;return true;}return false;}\ntemplate<typename T> inline bool chmin(T &a,T b){if(a > b){a = b;return true;}return false;}\nld dist(ld x1,ld y1,ld x2, ld y2){return sqrtl((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2));}\n\n\n// F_2係数 N×M行列 掃き出しO(N^2 M)\n// これはAx = bを解くとき等はis = true.これにより最右列がrankの計算などに絡まないようにする.\n// return {idx,rank}\npair<vector<int>,int> Gauss_Jordan(vector<vector<int>> &a,bool is = false){\n assert(!a.empty());\n int n = a.size(),m = a[0].size();\n // idx[j] = j列目に対応する元々の番号\n vector<int> idx(m);iota(idx.begin(),idx.end(),0);\n int rank = 0;\n for(int _ = 0;_ < n;_++){\n for(int i = _;i < n;i++)for(int j = _;j + is < m;j++){\n if(a[i][j]){\n swap(idx[_],idx[j]);\n for(int k = 0;k < n;k++)swap(a[k][_],a[k][j]);\n for(int k = 0;k < m;k++)swap(a[_][k],a[i][k]);\n rank++;\n goto kasu;\n }\n }\n kasu:\n if(rank != _ + 1)break;\n for(int i = 0;i < n;i++){\n if(i == _ || !a[i][_])continue;\n for(int j = 0;j < m;j++)a[i][j] = a[i][j] ^ a[_][j];\n }\n }\n // 必要ならばidxを参照することを忘れない\n // ex.) res[idx[i]] = hoge;等\n return {idx,rank};\n};\n// 時間が厳しいならbitset高速化\n\nint main(){\n \n ios_base::sync_with_stdio(0), cin.tie(0);\n while(1){\n int n,m,d;cin >> m >> n >> d;\n if(!n)break;\n vvi v(n,vi(m));cin >> v;\n vector<vector<int>> a(n*m,vector<int>(n*m + 1));\n rep(i,0,n)rep(j,0,m)if(v[i][j] == 1)a[i*m + j].back() = 1;\n rep(i,0,n)rep(j,0,m){\n a[i*m+j][i*m+j] = 1;\n rep(ni,0,n)rep(nj,0,m){\n if(abs(i-ni) + abs(j-nj) == d)a[i*m + j][ni*m + nj] = 1;\n }\n }\n auto [idx,rnk] = Gauss_Jordan(a,true);\n bool is = true;\n rep(i,rnk,n*m){\n if(a[i].back())is = false;\n }\n cout << is << \"\\n\";\n }\n \n \n \n\n return 0;\n}", "accuracy": 1, "time_ms": 600, "memory_kb": 4596, "score_of_the_acc": -0.3094, "final_rank": 10 }, { "submission_id": "aoj_1308_9770779", "code_snippet": "#line 2 \"cp-library/src/cp-template.hpp\"\n#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\nusing ld = long double;\nusing uint = unsigned int;\nusing ull = unsigned long long;\nusing i32 = int;\nusing u32 = unsigned int;\nusing i64 = long long;\nusing u64 = unsigned long long;\nusing i128 = __int128_t;\ntemplate < class T > bool chmin(T& a, T b) { if(a > b) { a = b; return true; } return false; }\ntemplate < class T > bool chmax(T& a, T b) { if(a < b) { a = b; return true; } return false; }\ntemplate < class T, class U > T ceil (T x, U y) { return (x > 0 ? (x + y - 1) / y : x / y); }\ntemplate < class T, class U > T floor(T x, U y) { return (x > 0 ? x / y : (x - y + 1) / y); }\nint popcnt(i32 x) { return __builtin_popcount(x); }\nint popcnt(u32 x) { return __builtin_popcount(x); }\nint popcnt(i64 x) { return __builtin_popcountll(x); }\nint popcnt(u64 x) { return __builtin_popcountll(x); }\n\n#line 2 \"cp-library/src/utility/rep_itr.hpp\"\ntemplate < class T > struct itr_rep {\n T i, d;\n constexpr itr_rep(const T i) noexcept : i(i), d(1) {}\n constexpr itr_rep(const T i, const T d) noexcept : i(i), d(d) {}\n void operator++() noexcept { i += d; }\n constexpr int operator*() const noexcept { return i; }\n constexpr bool operator!=(const itr_rep x) const noexcept { return d > 0 ? i < x.i : i > x.i; }\n};\n\ntemplate < class T > struct rep {\n const itr_rep< T > s, t;\n constexpr rep(const T t) noexcept : s(0), t(t) {}\n constexpr rep(const T s, const T t) noexcept : s(s), t(t) {}\n constexpr rep(const T s, const T t, const T d) noexcept : s(s, d), t(t, d) {}\n constexpr auto begin() const noexcept { return s; }\n constexpr auto end () const noexcept { return t; }\n};\n\ntemplate < class T > struct revrep {\n const itr_rep < T > s, t;\n constexpr revrep(const T t) noexcept : s(t - 1, -1), t(-1, -1) {}\n constexpr revrep(const T s, const T t) noexcept : s(t - 1, -1), t(s - 1, -1) {}\n constexpr revrep(const T s, const T t, const T d) noexcept : s(t - 1, -d), t(s - 1, -d) {}\n constexpr auto begin() const noexcept { return s; }\n constexpr auto end () const noexcept { return t; }\n};\n#line 3 \"cp-library/src/utility/io.hpp\"\n\n/* 128bit integer */\nistream& operator>>(istream& is, i128& x) {\n std::string s; is >> s;\n int pm = (s[0] == '-');\n x = 0;\n for(int i : rep(pm, int(s.size()))) x = x * 10 + (s[i] - '0');\n if(pm) x *= -1;\n return is;\n}\nostream& operator<<(ostream& os, const i128& x) {\n if(x == 0) return os << '0';\n i128 y = x;\n if(y < 0) { os << '-'; y *= -1; }\n std::vector<int> ny;\n while(y > 0) { ny.push_back(y % 10); y /= 10; }\n for(int i : revrep(ny.size())) os << ny[i];\n return os;\n}\n\ntemplate < class S, class T > istream& operator>>(istream& is, std::pair< S, T >& x) { is >> x.first >> x.second; return is; }\ntemplate < class S, class T > ostream& operator<<(ostream& os, const std::pair< S, T >& x) { os << x.first << \" \" << x.second; return os; }\n\nnamespace scanner {\n struct sca {\n template < class T > operator T() {\n T s; std::cin >> s; return s;\n }\n };\n struct vec {\n int n;\n vec(int n) : n(n) {}\n template < class T > operator std::vector< T >() {\n std::vector< T > v(n);\n for(T& x : v) std::cin >> x;\n return v;\n }\n };\n struct mat {\n int h, w;\n mat(int h, int w) : h(h), w(w) {}\n template < class T > operator std::vector< std::vector< T > >() {\n std::vector m(h, std::vector< T >(w));\n for(std::vector< T >& v : m) for(T& x : v) std::cin >> x;\n return m;\n }\n };\n struct speedup {\n speedup() {\n std::cin.tie(0);\n std::ios::sync_with_stdio(0);\n }\n } speedup_instance;\n}\nscanner::sca in() { return scanner::sca(); }\nscanner::vec in(int n) { return scanner::vec(n); }\nscanner::mat in(int h, int w) { return scanner::mat(h, w); }\n\nnamespace printer {\n void precision(int d) { std::cout << std::fixed << std::setprecision(d); }\n void flush() { std::cout.flush(); }\n}\n\ntemplate < class T >\nostream& operator<<(ostream& os, const std::vector< T > a) {\n int n = a.size();\n for(int i : rep(n)) { os << a[i]; if(i != n - 1) os << ' '; }\n return os;\n}\n\nint print() { std::cout << '\\n'; return 0; }\ntemplate < class head, class... tail > int print(head&& h, tail&&... t) {\n std::cout << h; if(sizeof...(tail)) std::cout << ' ';\n return print(std::forward<tail>(t)...);\n}\ntemplate < class T > int print_n(const std::vector< T > a) {\n int n = a.size();\n for(int i : rep(n)) std::cout << a[i] << \"\\n\";\n return 0;\n}\n\n\n#line 2 \"cp-library/src/utility/key_val.hpp\"\n\ntemplate < class K, class V >\nstruct key_val {\n K key; V val;\n key_val() {}\n key_val(K key, V val) : key(key), val(val) {}\n template < std::size_t Index >\n std::tuple_element_t< Index, key_val >& get() {\n if constexpr (Index == 0) return key;\n if constexpr (Index == 1) return val;\n }\n};\n\nnamespace std {\n\ntemplate < class K, class V > struct tuple_size < key_val< K, V > > : integral_constant< size_t, 2 > {};\ntemplate < class K, class V > struct tuple_element < 0, key_val< K, V > > { using type = K; };\ntemplate < class K, class V > struct tuple_element < 1, key_val< K, V > > { using type = V; };\n\n}\n#line 2 \"cp-library/src/utility/vec_op.hpp\"\ntemplate < class T > key_val< int, T > max_of(const vector< T >& a) {\n int i = std::max_element(a.begin(), a.end()) - a.begin();\n return {i, a[i]};\n}\ntemplate < class T > key_val< int, T > min_of(const vector< T >& a) {\n int i = std::min_element(a.begin(), a.end()) - a.begin();\n return {i, a[i]};\n}\ntemplate < class S, class T > S sum_of(const vector< T >& a) {\n S sum = 0;\n for(const T x : a) sum += x;\n return sum;\n}\ntemplate < class S, class T > vector< S > freq_of(const vector< T >& a, T L, T R) {\n vector< S > res(R - L, S(0));\n for(const T x : a) res[x - L] += 1;\n return res;\n}\ntemplate < class S, class T > struct prefix_sum {\n vector< S > s;\n prefix_sum(const vector< T >& a) : s(a) {\n s.insert(s.begin(), S(0));\n for(int i : rep(a.size())) s[i + 1] += s[i];\n }\n // [L, R)\n S sum(int L, int R) { return s[R] - s[L]; }\n};\n#line 3 \"cp-library/src/utility/heap.hpp\"\n\ntemplate < class T > using heap_min = std::priority_queue< T, std::vector< T >, std::greater< T > >;\ntemplate < class T > using heap_max = std::priority_queue< T, std::vector< T >, std::less< T > >;\n\n#line 27 \"cp-library/src/cp-template.hpp\"\n\n#line 1 \"cp-library/src/algorithm/bin_search.hpp\"\ntemplate < class T, class F >\nT bin_search(T ok, T ng, F f) {\n while(abs(ng - ok) > 1) {\n T mid = (ok + ng) / 2;\n (f(mid) ? ok : ng) = mid;\n }\n return ok;\n}\n\ntemplate < class T, class F >\nT bin_search_real(T ok, T ng, F f, int step = 80) {\n while(step--) {\n T mid = (ok + ng) / 2;\n (f(mid) ? ok : ng) = mid;\n }\n return ok;\n}\n#line 2 \"cp-library/src/algorithm/argsort.hpp\"\n\ntemplate < class T > std::vector< int > argsort(const std::vector< T > &a) {\n std::vector< int > ids((int)a.size());\n std::iota(ids.begin(), ids.end(), 0);\n std::sort(ids.begin(), ids.end(), [&](int i, int j) {\n return a[i] < a[j] || (a[i] == a[j] && i < j);\n });\n return ids;\n}\n#line 1 \"macro.hpp\"\nnamespace macro {\n\nusing size_type = int;\ntemplate < class container > void sort(container& a) { std::sort(std:: begin(a), std:: end(a)); }\ntemplate < class container > void rsort(container& a) { std::sort(std::rbegin(a), std::rend(a)); }\ntemplate < class container > void reverse(container& a) { std::reverse(std::begin(a), std::end(a)); }\ntemplate < class container > void unique(container& a) {\n std::sort(std::begin(a), std::end(a));\n a.erase(std::unique(std::begin(a), std::end(a)), std::end(a));\n}\ntemplate < class container > container sorted(const container& a) { container b = a; sort(b); return std::move(b); }\ntemplate < class container > container rsorted(const container& a) { container b = a; rsort(b); return std::move(b); }\ntemplate < class container, class compare > void sort(container& a, const compare& cmp) { std::sort(std::begin(a), std::end(a), cmp); }\ntemplate < class container, class compare > container sorted(const container& a, const compare& cmp) { container b = a; sort(b, cmp); return std::move(b); }\ntemplate < class container, class value > size_type lower_bound(const container& a, const value& x) { return std::lower_bound(std::begin(a), std::end(a), x) - std::begin(a); }\ntemplate < class container, class value > size_type upper_bound(const container& a, const value& x) { return std::upper_bound(std::begin(a), std::end(a), x) - std::begin(a); }\n\nconst std::vector<std::pair<size_type, size_type>> dir4 = { {+1, 0}, {-1, 0}, { 0, +1}, { 0, -1} };\nconst std::vector<std::pair<size_type, size_type>> dir8 = { {-1, -1}, {-1, 0}, {-1, +1}, { 0, -1}, { 0, +1}, {+1, -1}, {+1, 0}, {+1, +1} };\n\n#ifdef _DEBUG\n#define debug(x) std::cout << \"[\" << __LINE__ << \"] \" << #x << \": \" << x << std::endl\n#else\n#define debug(x)\n#endif\n\ntemplate < class container > void concat(container& a, const container& b) {\n a.insert(std::end(a), std::begin(b), std::end(b));\n}\nstd::vector<size_type> iota(const size_type n) {\n std::vector<size_type> I(n);\n std::iota(std::begin(I), std::end(I), 0);\n return I;\n}\ntemplate < class container > std::vector<size_type> sort_idx(const container& a) {\n const size_type n = a.size();\n std::vector<size_type> I = iota(n);\n std::sort(std::begin(I), std::end(I), [&](size_type i, size_type j) { return a[i] < a[j] or (a[i] == a[j] and i < j); });\n return I;\n}\ntemplate < class container, class compare > std::vector<size_type> sort_idx(const container& a, const compare& cmp) {\n const size_type n = a.size();\n std::vector<size_type> I = iota(n);\n std::sort(std::begin(I), std::end(I), [&](size_type i, size_type j) { return cmp(a[i], a[j]) or (a[i] == a[j] and i < j); });\n return std::move(I);\n}\n\nstruct grid {\n using size_type = int;\n size_type H, W;\n grid(const size_type H, const size_type W) : H(H), W(W) {}\n bool contains(const size_type i, const size_type j) {\n return 0 <= i and i < H and 0 <= j and j < W;\n }\n};\n\nusing f64 = long double;\n\ntemplate < class T > vector< T >& operator++(vector< T >& a) { for(T& x : a) x++; return a; }\ntemplate < class T > vector< T >& operator--(vector< T >& a) { for(T& x : a) x--; return a; }\ntemplate < class T > vector< T > operator++(vector< T >& a, signed) { vector< T > res = a; for(T& x : a) x++; return res; }\ntemplate < class T > vector< T > operator--(vector< T >& a, signed) { vector< T > res = a; for(T& x : a) x--; return res; }\n\n} // namespace macro\n\nusing namespace macro;\n#line 3 \"A.cpp\"\n\nconst int MAX_ROW = 700; // to be set appropriately\nconst int MAX_COL = 700; // to be set appropriately\nstruct BitMatrix {\n int H, W;\n bitset<MAX_COL> val[MAX_ROW];\n BitMatrix(int m = 1, int n = 1) : H(m), W(n) {}\n inline bitset<MAX_COL>& operator [] (int i) {return val[i];}\n};\n\nint GaussJordan(BitMatrix &A, bool is_extended = false) {\n int rank = 0;\n for (int col = 0; col < A.W; ++col) {\n if (is_extended && col == A.W - 1) break;\n int pivot = -1;\n for (int row = rank; row < A.H; ++row) {\n if (A[row][col]) {\n pivot = row;\n break;\n }\n }\n if (pivot == -1) continue;\n swap(A[pivot], A[rank]);\n for (int row = 0; row < A.H; ++row) {\n if (row != rank && A[row][col]) A[row] ^= A[rank];\n }\n ++rank;\n }\n return rank;\n}\n\nint linear_equation(BitMatrix A, vector<int> b, vector<int> &res) {\n int m = A.H, n = A.W;\n BitMatrix M(m, n + 1);\n for (int i = 0; i < m; ++i) {\n for (int j = 0; j < n; ++j) M[i][j] = A[i][j];\n M[i][n] = b[i];\n }\n int rank = GaussJordan(M, true);\n\n // check if it has no solution\n for (int row = rank; row < m; ++row) if (M[row][n]) return -1;\n\n // answer\n res.assign(n, 0);\n for (int i = 0; i < rank; ++i) res[i] = M[i][n];\n return rank;\n}\n\nint solve(int w, int h, int d) {\n vector<vector<int>> s = in(h, w);\n\n BitMatrix A(h * w, h * w);\n for(int i : rep(h)) for(int j : rep(w)) {\n for(int x : rep(h)) for(int y : rep(w)) {\n if(abs(i - x) + abs(j - y) == d or make_pair(i, j) == make_pair(x, y)) {\n A[x * w + y][i * w + j] = 1;\n }\n }\n }\n\n vector<int> b(h * w, 0);\n for(int i : rep(h)) for(int j : rep(w)) {\n b[i * w + j] = s[i][j];\n }\n\n vector<int> res;\n return linear_equation(A, b, res) != -1;\n}\n\nint main() {\n while(true) {\n int w = in(), h = in(), d = in();\n if(make_tuple(w, h, d) == make_tuple(0, 0, 0)) return 0;\n cout << solve(w, h, d) << \"\\n\";\n }\n}", "accuracy": 1, "time_ms": 60, "memory_kb": 3580, "score_of_the_acc": -0.0153, "final_rank": 2 }, { "submission_id": "aoj_1308_9770777", "code_snippet": "#line 2 \"cp-library/src/cp-template.hpp\"\n#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\nusing ld = long double;\nusing uint = unsigned int;\nusing ull = unsigned long long;\nusing i32 = int;\nusing u32 = unsigned int;\nusing i64 = long long;\nusing u64 = unsigned long long;\nusing i128 = __int128_t;\ntemplate < class T > bool chmin(T& a, T b) { if(a > b) { a = b; return true; } return false; }\ntemplate < class T > bool chmax(T& a, T b) { if(a < b) { a = b; return true; } return false; }\ntemplate < class T, class U > T ceil (T x, U y) { return (x > 0 ? (x + y - 1) / y : x / y); }\ntemplate < class T, class U > T floor(T x, U y) { return (x > 0 ? x / y : (x - y + 1) / y); }\nint popcnt(i32 x) { return __builtin_popcount(x); }\nint popcnt(u32 x) { return __builtin_popcount(x); }\nint popcnt(i64 x) { return __builtin_popcountll(x); }\nint popcnt(u64 x) { return __builtin_popcountll(x); }\n\n#line 2 \"cp-library/src/utility/rep_itr.hpp\"\ntemplate < class T > struct itr_rep {\n T i, d;\n constexpr itr_rep(const T i) noexcept : i(i), d(1) {}\n constexpr itr_rep(const T i, const T d) noexcept : i(i), d(d) {}\n void operator++() noexcept { i += d; }\n constexpr int operator*() const noexcept { return i; }\n constexpr bool operator!=(const itr_rep x) const noexcept { return d > 0 ? i < x.i : i > x.i; }\n};\n\ntemplate < class T > struct rep {\n const itr_rep< T > s, t;\n constexpr rep(const T t) noexcept : s(0), t(t) {}\n constexpr rep(const T s, const T t) noexcept : s(s), t(t) {}\n constexpr rep(const T s, const T t, const T d) noexcept : s(s, d), t(t, d) {}\n constexpr auto begin() const noexcept { return s; }\n constexpr auto end () const noexcept { return t; }\n};\n\ntemplate < class T > struct revrep {\n const itr_rep < T > s, t;\n constexpr revrep(const T t) noexcept : s(t - 1, -1), t(-1, -1) {}\n constexpr revrep(const T s, const T t) noexcept : s(t - 1, -1), t(s - 1, -1) {}\n constexpr revrep(const T s, const T t, const T d) noexcept : s(t - 1, -d), t(s - 1, -d) {}\n constexpr auto begin() const noexcept { return s; }\n constexpr auto end () const noexcept { return t; }\n};\n#line 3 \"cp-library/src/utility/io.hpp\"\n\n/* 128bit integer */\nistream& operator>>(istream& is, i128& x) {\n std::string s; is >> s;\n int pm = (s[0] == '-');\n x = 0;\n for(int i : rep(pm, int(s.size()))) x = x * 10 + (s[i] - '0');\n if(pm) x *= -1;\n return is;\n}\nostream& operator<<(ostream& os, const i128& x) {\n if(x == 0) return os << '0';\n i128 y = x;\n if(y < 0) { os << '-'; y *= -1; }\n std::vector<int> ny;\n while(y > 0) { ny.push_back(y % 10); y /= 10; }\n for(int i : revrep(ny.size())) os << ny[i];\n return os;\n}\n\ntemplate < class S, class T > istream& operator>>(istream& is, std::pair< S, T >& x) { is >> x.first >> x.second; return is; }\ntemplate < class S, class T > ostream& operator<<(ostream& os, const std::pair< S, T >& x) { os << x.first << \" \" << x.second; return os; }\n\nnamespace scanner {\n struct sca {\n template < class T > operator T() {\n T s; std::cin >> s; return s;\n }\n };\n struct vec {\n int n;\n vec(int n) : n(n) {}\n template < class T > operator std::vector< T >() {\n std::vector< T > v(n);\n for(T& x : v) std::cin >> x;\n return v;\n }\n };\n struct mat {\n int h, w;\n mat(int h, int w) : h(h), w(w) {}\n template < class T > operator std::vector< std::vector< T > >() {\n std::vector m(h, std::vector< T >(w));\n for(std::vector< T >& v : m) for(T& x : v) std::cin >> x;\n return m;\n }\n };\n struct speedup {\n speedup() {\n std::cin.tie(0);\n std::ios::sync_with_stdio(0);\n }\n } speedup_instance;\n}\nscanner::sca in() { return scanner::sca(); }\nscanner::vec in(int n) { return scanner::vec(n); }\nscanner::mat in(int h, int w) { return scanner::mat(h, w); }\n\nnamespace printer {\n void precision(int d) { std::cout << std::fixed << std::setprecision(d); }\n void flush() { std::cout.flush(); }\n}\n\ntemplate < class T >\nostream& operator<<(ostream& os, const std::vector< T > a) {\n int n = a.size();\n for(int i : rep(n)) { os << a[i]; if(i != n - 1) os << ' '; }\n return os;\n}\n\nint print() { std::cout << '\\n'; return 0; }\ntemplate < class head, class... tail > int print(head&& h, tail&&... t) {\n std::cout << h; if(sizeof...(tail)) std::cout << ' ';\n return print(std::forward<tail>(t)...);\n}\ntemplate < class T > int print_n(const std::vector< T > a) {\n int n = a.size();\n for(int i : rep(n)) std::cout << a[i] << \"\\n\";\n return 0;\n}\n\n\n#line 2 \"cp-library/src/utility/key_val.hpp\"\n\ntemplate < class K, class V >\nstruct key_val {\n K key; V val;\n key_val() {}\n key_val(K key, V val) : key(key), val(val) {}\n template < std::size_t Index >\n std::tuple_element_t< Index, key_val >& get() {\n if constexpr (Index == 0) return key;\n if constexpr (Index == 1) return val;\n }\n};\n\nnamespace std {\n\ntemplate < class K, class V > struct tuple_size < key_val< K, V > > : integral_constant< size_t, 2 > {};\ntemplate < class K, class V > struct tuple_element < 0, key_val< K, V > > { using type = K; };\ntemplate < class K, class V > struct tuple_element < 1, key_val< K, V > > { using type = V; };\n\n}\n#line 2 \"cp-library/src/utility/vec_op.hpp\"\ntemplate < class T > key_val< int, T > max_of(const vector< T >& a) {\n int i = std::max_element(a.begin(), a.end()) - a.begin();\n return {i, a[i]};\n}\ntemplate < class T > key_val< int, T > min_of(const vector< T >& a) {\n int i = std::min_element(a.begin(), a.end()) - a.begin();\n return {i, a[i]};\n}\ntemplate < class S, class T > S sum_of(const vector< T >& a) {\n S sum = 0;\n for(const T x : a) sum += x;\n return sum;\n}\ntemplate < class S, class T > vector< S > freq_of(const vector< T >& a, T L, T R) {\n vector< S > res(R - L, S(0));\n for(const T x : a) res[x - L] += 1;\n return res;\n}\ntemplate < class S, class T > struct prefix_sum {\n vector< S > s;\n prefix_sum(const vector< T >& a) : s(a) {\n s.insert(s.begin(), S(0));\n for(int i : rep(a.size())) s[i + 1] += s[i];\n }\n // [L, R)\n S sum(int L, int R) { return s[R] - s[L]; }\n};\n#line 3 \"cp-library/src/utility/heap.hpp\"\n\ntemplate < class T > using heap_min = std::priority_queue< T, std::vector< T >, std::greater< T > >;\ntemplate < class T > using heap_max = std::priority_queue< T, std::vector< T >, std::less< T > >;\n\n#line 27 \"cp-library/src/cp-template.hpp\"\n\n#line 1 \"cp-library/src/algorithm/bin_search.hpp\"\ntemplate < class T, class F >\nT bin_search(T ok, T ng, F f) {\n while(abs(ng - ok) > 1) {\n T mid = (ok + ng) / 2;\n (f(mid) ? ok : ng) = mid;\n }\n return ok;\n}\n\ntemplate < class T, class F >\nT bin_search_real(T ok, T ng, F f, int step = 80) {\n while(step--) {\n T mid = (ok + ng) / 2;\n (f(mid) ? ok : ng) = mid;\n }\n return ok;\n}\n#line 2 \"cp-library/src/algorithm/argsort.hpp\"\n\ntemplate < class T > std::vector< int > argsort(const std::vector< T > &a) {\n std::vector< int > ids((int)a.size());\n std::iota(ids.begin(), ids.end(), 0);\n std::sort(ids.begin(), ids.end(), [&](int i, int j) {\n return a[i] < a[j] || (a[i] == a[j] && i < j);\n });\n return ids;\n}\n#line 1 \"macro.hpp\"\nnamespace macro {\n\nusing size_type = int;\ntemplate < class container > void sort(container& a) { std::sort(std:: begin(a), std:: end(a)); }\ntemplate < class container > void rsort(container& a) { std::sort(std::rbegin(a), std::rend(a)); }\ntemplate < class container > void reverse(container& a) { std::reverse(std::begin(a), std::end(a)); }\ntemplate < class container > void unique(container& a) {\n std::sort(std::begin(a), std::end(a));\n a.erase(std::unique(std::begin(a), std::end(a)), std::end(a));\n}\ntemplate < class container > container sorted(const container& a) { container b = a; sort(b); return std::move(b); }\ntemplate < class container > container rsorted(const container& a) { container b = a; rsort(b); return std::move(b); }\ntemplate < class container, class compare > void sort(container& a, const compare& cmp) { std::sort(std::begin(a), std::end(a), cmp); }\ntemplate < class container, class compare > container sorted(const container& a, const compare& cmp) { container b = a; sort(b, cmp); return std::move(b); }\ntemplate < class container, class value > size_type lower_bound(const container& a, const value& x) { return std::lower_bound(std::begin(a), std::end(a), x) - std::begin(a); }\ntemplate < class container, class value > size_type upper_bound(const container& a, const value& x) { return std::upper_bound(std::begin(a), std::end(a), x) - std::begin(a); }\n\nconst std::vector<std::pair<size_type, size_type>> dir4 = { {+1, 0}, {-1, 0}, { 0, +1}, { 0, -1} };\nconst std::vector<std::pair<size_type, size_type>> dir8 = { {-1, -1}, {-1, 0}, {-1, +1}, { 0, -1}, { 0, +1}, {+1, -1}, {+1, 0}, {+1, +1} };\n\n#ifdef _DEBUG\n#define debug(x) std::cout << \"[\" << __LINE__ << \"] \" << #x << \": \" << x << std::endl\n#else\n#define debug(x)\n#endif\n\ntemplate < class container > void concat(container& a, const container& b) {\n a.insert(std::end(a), std::begin(b), std::end(b));\n}\nstd::vector<size_type> iota(const size_type n) {\n std::vector<size_type> I(n);\n std::iota(std::begin(I), std::end(I), 0);\n return I;\n}\ntemplate < class container > std::vector<size_type> sort_idx(const container& a) {\n const size_type n = a.size();\n std::vector<size_type> I = iota(n);\n std::sort(std::begin(I), std::end(I), [&](size_type i, size_type j) { return a[i] < a[j] or (a[i] == a[j] and i < j); });\n return I;\n}\ntemplate < class container, class compare > std::vector<size_type> sort_idx(const container& a, const compare& cmp) {\n const size_type n = a.size();\n std::vector<size_type> I = iota(n);\n std::sort(std::begin(I), std::end(I), [&](size_type i, size_type j) { return cmp(a[i], a[j]) or (a[i] == a[j] and i < j); });\n return std::move(I);\n}\n\nstruct grid {\n using size_type = int;\n size_type H, W;\n grid(const size_type H, const size_type W) : H(H), W(W) {}\n bool contains(const size_type i, const size_type j) {\n return 0 <= i and i < H and 0 <= j and j < W;\n }\n};\n\nusing f64 = long double;\n\ntemplate < class T > vector< T >& operator++(vector< T >& a) { for(T& x : a) x++; return a; }\ntemplate < class T > vector< T >& operator--(vector< T >& a) { for(T& x : a) x--; return a; }\ntemplate < class T > vector< T > operator++(vector< T >& a, signed) { vector< T > res = a; for(T& x : a) x++; return res; }\ntemplate < class T > vector< T > operator--(vector< T >& a, signed) { vector< T > res = a; for(T& x : a) x--; return res; }\n\n} // namespace macro\n\nusing namespace macro;\n#line 3 \"A.cpp\"\n\nconst int MAX_ROW = 1000; // to be set appropriately\nconst int MAX_COL = 1000; // to be set appropriately\nstruct BitMatrix {\n int H, W;\n bitset<MAX_COL> val[MAX_ROW];\n BitMatrix(int m = 1, int n = 1) : H(m), W(n) {}\n inline bitset<MAX_COL>& operator [] (int i) {return val[i];}\n};\n\nint GaussJordan(BitMatrix &A, bool is_extended = false) {\n int rank = 0;\n for (int col = 0; col < A.W; ++col) {\n if (is_extended && col == A.W - 1) break;\n int pivot = -1;\n for (int row = rank; row < A.H; ++row) {\n if (A[row][col]) {\n pivot = row;\n break;\n }\n }\n if (pivot == -1) continue;\n swap(A[pivot], A[rank]);\n for (int row = 0; row < A.H; ++row) {\n if (row != rank && A[row][col]) A[row] ^= A[rank];\n }\n ++rank;\n }\n return rank;\n}\n\nint linear_equation(BitMatrix A, vector<int> b, vector<int> &res) {\n int m = A.H, n = A.W;\n BitMatrix M(m, n + 1);\n for (int i = 0; i < m; ++i) {\n for (int j = 0; j < n; ++j) M[i][j] = A[i][j];\n M[i][n] = b[i];\n }\n int rank = GaussJordan(M, true);\n\n // check if it has no solution\n for (int row = rank; row < m; ++row) if (M[row][n]) return -1;\n\n // answer\n res.assign(n, 0);\n for (int i = 0; i < rank; ++i) res[i] = M[i][n];\n return rank;\n}\n\nint solve(int w, int h, int d) {\n vector<vector<int>> s = in(h, w);\n\n BitMatrix A(h * w, h * w);\n for(int i : rep(h)) for(int j : rep(w)) {\n for(int x : rep(h)) for(int y : rep(w)) {\n if(abs(i - x) + abs(j - y) == d or make_pair(i, j) == make_pair(x, y)) {\n A[x * w + y][i * w + j] = 1;\n }\n }\n }\n\n vector<int> b(h * w, 0);\n for(int i : rep(h)) for(int j : rep(w)) {\n b[i * w + j] = s[i][j];\n }\n\n vector<int> res;\n return linear_equation(A, b, res) != -1;\n}\n\nint main() {\n while(true) {\n int w = in(), h = in(), d = in();\n if(make_tuple(w, h, d) == make_tuple(0, 0, 0)) return 0;\n cout << solve(w, h, d) << \"\\n\";\n }\n}", "accuracy": 1, "time_ms": 70, "memory_kb": 3732, "score_of_the_acc": -0.0387, "final_rank": 4 }, { "submission_id": "aoj_1308_9744358", "code_snippet": "#include <bits/stdc++.h>\n\nusing namespace std;\n\n#define rep(i, a, b) for (int i = (int)(a); i < (int)(b); i++)\n#define rrep(i, a, b) for (int i = (int)(b)-1; i >= (int)(a); i--)\n#define ALL(v) (v).begin(), (v).end()\n#define UNIQUE(v) sort(ALL(v)), (v).erase(unique(ALL(v)), (v).end())\n#define SZ(v) (int)v.size()\n#define MIN(v) *min_element(ALL(v))\n#define MAX(v) *max_element(ALL(v))\n#define LB(v, x) int(lower_bound(ALL(v), (x)) - (v).begin())\n#define UB(v, x) int(upper_bound(ALL(v), (x)) - (v).begin())\n\nusing uint = unsigned int;\nusing ll = long long int;\nusing ull = unsigned long long;\nusing i128 = __int128_t;\nusing u128 = __uint128_t;\nconst int inf = 0x3fffffff;\nconst ll INF = 0x1fffffffffffffff;\n\ntemplate <typename T> inline bool chmax(T &a, T b) {\n if (a < b) {\n a = b;\n return 1;\n }\n return 0;\n}\ntemplate <typename T> inline bool chmin(T &a, T b) {\n if (a > b) {\n a = b;\n return 1;\n }\n return 0;\n}\ntemplate <typename T, typename U> T ceil(T x, U y) {\n assert(y != 0);\n if (y < 0)\n x = -x, y = -y;\n return (x > 0 ? (x + y - 1) / y : x / y);\n}\ntemplate <typename T, typename U> T floor(T x, U y) {\n assert(y != 0);\n if (y < 0)\n x = -x, y = -y;\n return (x > 0 ? x / y : (x - y + 1) / y);\n}\ntemplate <typename T> int popcnt(T x) {\n return __builtin_popcountll(x);\n}\ntemplate <typename T> int topbit(T x) {\n return (x == 0 ? -1 : 63 - __builtin_clzll(x));\n}\ntemplate <typename T> int lowbit(T x) {\n return (x == 0 ? -1 : __builtin_ctzll(x));\n}\n\ntemplate <class T, class U>\nostream &operator<<(ostream &os, const pair<T, U> &p) {\n os << \"P(\" << p.first << \", \" << p.second << \")\";\n return os;\n}\ntemplate <typename T> ostream &operator<<(ostream &os, const vector<T> &vec) {\n os << \"{\";\n for (int i = 0; i < vec.size(); i++) {\n os << vec[i] << (i + 1 == vec.size() ? \"\" : \", \");\n }\n os << \"}\";\n return os;\n}\ntemplate <typename T, typename U>\nostream &operator<<(ostream &os, const map<T, U> &map_var) {\n os << \"{\";\n for (auto itr = map_var.begin(); itr != map_var.end(); itr++) {\n os << \"(\" << itr->first << \", \" << itr->second << \")\";\n itr++;\n if (itr != map_var.end())\n os << \", \";\n itr--;\n }\n os << \"}\";\n return os;\n}\ntemplate <typename T> ostream &operator<<(ostream &os, const set<T> &set_var) {\n os << \"{\";\n for (auto itr = set_var.begin(); itr != set_var.end(); itr++) {\n os << *itr;\n ++itr;\n if (itr != set_var.end())\n os << \", \";\n itr--;\n }\n os << \"}\";\n return os;\n}\n#ifdef LOCAL\n#define show(...) _show(0, #__VA_ARGS__, __VA_ARGS__)\n#else\n#define show(...) true\n#endif\ntemplate <typename T> void _show(int i, T name) {\n cerr << '\\n';\n}\ntemplate <typename T1, typename T2, typename... T3>\nvoid _show(int i, const T1 &a, const T2 &b, const T3 &...c) {\n for (; a[i] != ',' && a[i] != '\\0'; i++)\n cerr << a[i];\n cerr << \":\" << b << \" \";\n _show(i + 1, a, c...);\n}\n\n/**\n * @brief template\n */\n\ntemplate <unsigned mod = 1000000007> struct fp {\n unsigned v;\n static constexpr int get_mod() {\n return mod;\n }\n constexpr unsigned inv() const {\n assert(v != 0);\n int x = v, y = mod, p = 1, q = 0, t = 0, tmp = 0;\n while (y > 0) {\n t = x / y;\n x -= t * y, p -= t * q;\n tmp = x, x = y, y = tmp;\n tmp = p, p = q, q = tmp;\n }\n if (p < 0)\n p += mod;\n return p;\n }\n constexpr fp(ll x = 0) : v(x >= 0 ? x % mod : (mod - (-x) % mod) % mod) {}\n fp operator-() const {\n return fp() - *this;\n }\n fp pow(ull t) {\n fp res = 1, b = *this;\n while (t) {\n if (t & 1)\n res *= b;\n b *= b;\n t >>= 1;\n }\n return res;\n }\n fp &operator+=(const fp &x) {\n if ((v += x.v) >= mod)\n v -= mod;\n return *this;\n }\n fp &operator-=(const fp &x) {\n if ((v += mod - x.v) >= mod)\n v -= mod;\n return *this;\n }\n fp &operator*=(const fp &x) {\n v = ull(v) * x.v % mod;\n return *this;\n }\n fp &operator/=(const fp &x) {\n v = ull(v) * x.inv() % mod;\n return *this;\n }\n fp operator+(const fp &x) const {\n return fp(*this) += x;\n }\n fp operator-(const fp &x) const {\n return fp(*this) -= x;\n }\n fp operator*(const fp &x) const {\n return fp(*this) *= x;\n }\n fp operator/(const fp &x) const {\n return fp(*this) /= x;\n }\n bool operator==(const fp &x) const {\n return v == x.v;\n }\n bool operator!=(const fp &x) const {\n return v != x.v;\n }\n friend istream &operator>>(istream &is, fp &x) {\n return is >> x.v;\n }\n friend ostream &operator<<(ostream &os, const fp &x) {\n return os << x.v;\n }\n};\n\ntemplate <typename T> T Inv(ll n) {\n static const int md = T::get_mod();\n static vector<T> buf({0, 1});\n assert(n > 0);\n n %= md;\n while (SZ(buf) <= n) {\n int k = SZ(buf), q = (md + k - 1) / k;\n buf.push_back(buf[k * q - md] * q);\n }\n return buf[n];\n}\n\ntemplate <typename T> T Fact(ll n, bool inv = 0) {\n static const int md = T::get_mod();\n static vector<T> buf({1, 1}), ibuf({1, 1});\n assert(n >= 0 and n < md);\n while (SZ(buf) <= n) {\n buf.push_back(buf.back() * SZ(buf));\n ibuf.push_back(ibuf.back() * Inv<T>(SZ(ibuf)));\n }\n return inv ? ibuf[n] : buf[n];\n}\n\ntemplate <typename T> T nPr(int n, int r, bool inv = 0) {\n if (n < 0 || n < r || r < 0)\n return 0;\n return Fact<T>(n, inv) * Fact<T>(n - r, inv ^ 1);\n}\ntemplate <typename T> T nCr(int n, int r, bool inv = 0) {\n if (n < 0 || n < r || r < 0)\n return 0;\n return Fact<T>(n, inv) * Fact<T>(r, inv ^ 1) * Fact<T>(n - r, inv ^ 1);\n}\ntemplate <typename T> T nHr(int n, int r, bool inv = 0) {\n return nCr<T>(n + r - 1, r, inv);\n}\n\n/**\n * @brief Modint\n */\n\ntemplate <class T> struct Matrix {\n int h, w;\n vector<vector<T>> val;\n T det;\n Matrix() {}\n Matrix(int n) : h(n), w(n), val(vector<vector<T>>(n, vector<T>(n))) {}\n Matrix(int n, int m)\n : h(n), w(m), val(vector<vector<T>>(n, vector<T>(m))) {}\n vector<T> &operator[](const int i) {\n return val[i];\n }\n Matrix &operator+=(const Matrix &m) {\n assert(h == m.h and w == m.w);\n rep(i, 0, h) rep(j, 0, w) val[i][j] += m.val[i][j];\n return *this;\n }\n Matrix &operator-=(const Matrix &m) {\n assert(h == m.h and w == m.w);\n rep(i, 0, h) rep(j, 0, w) val[i][j] -= m.val[i][j];\n return *this;\n }\n Matrix &operator*=(const Matrix &m) {\n assert(w == m.h);\n Matrix<T> res(h, m.w);\n rep(i, 0, h) rep(j, 0, m.w) rep(k, 0, w) res.val[i][j] +=\n val[i][k] * m.val[k][j];\n *this = res;\n return *this;\n }\n Matrix operator+(const Matrix &m) const {\n return Matrix(*this) += m;\n }\n Matrix operator-(const Matrix &m) const {\n return Matrix(*this) -= m;\n }\n Matrix operator*(const Matrix &m) const {\n return Matrix(*this) *= m;\n }\n Matrix pow(ll k) {\n Matrix<T> res(h, h), c = *this;\n rep(i, 0, h) res.val[i][i] = 1;\n while (k) {\n if (k & 1)\n res *= c;\n c *= c;\n k >>= 1;\n }\n return res;\n }\n vector<int> gauss(int c = -1) {\n det = 1;\n if (val.empty())\n return {};\n if (c == -1)\n c = w;\n int cur = 0;\n vector<int> res;\n rep(i, 0, c) {\n if (cur == h)\n break;\n rep(j, cur, h) if (val[j][i] != 0) {\n swap(val[cur], val[j]);\n if (cur != j)\n det *= -1;\n break;\n }\n det *= val[cur][i];\n if (val[cur][i] == 0)\n continue;\n rep(j, 0, h) if (j != cur) {\n T z = val[j][i] / val[cur][i];\n rep(k, i, w) val[j][k] -= val[cur][k] * z;\n }\n res.push_back(i);\n cur++;\n }\n return res;\n }\n Matrix inv() {\n assert(h == w);\n Matrix base(h, h * 2), res(h, h);\n rep(i, 0, h) rep(j, 0, h) base[i][j] = val[i][j];\n rep(i, 0, h) base[i][h + i] = 1;\n base.gauss(h);\n det = base.det;\n rep(i, 0, h) rep(j, 0, h) res[i][j] = base[i][h + j] / base[i][i];\n return res;\n }\n bool operator==(const Matrix &m) {\n assert(h == m.h and w == m.w);\n rep(i, 0, h) rep(j, 0, w) if (val[i][j] != m.val[i][j]) return false;\n return true;\n }\n bool operator!=(const Matrix &m) {\n assert(h == m.h and w == m.w);\n rep(i, 0, h) rep(j, 0, w) if (val[i][j] == m.val[i][j]) return false;\n return true;\n }\n friend istream &operator>>(istream &is, Matrix &m) {\n rep(i, 0, m.h) rep(j, 0, m.w) is >> m[i][j];\n return is;\n }\n friend ostream &operator<<(ostream &os, Matrix &m) {\n rep(i, 0, m.h) {\n rep(j, 0, m.w) os << m[i][j]\n << (j == m.w - 1 and i != m.h - 1 ? '\\n' : ' ');\n }\n return os;\n }\n};\n\n/**\n * @brief Matrix\n */\n\ntemplate<typename T>pair<vector<T>,Matrix<T>> LinearEquation(Matrix<T> a,vector<T> b){\n int h=a.h,w=a.w;\n rep(i,0,h)a[i].push_back(b[i]);\n a.w++;\n vector<int> idx=a.gauss(w);\n rep(i,idx.size(),h)if(a[i][w]!=0)return {{},{}};\n vector<T> res(w);\n rep(i,0,idx.size())res[idx[i]]=a[i][w]/a[i][idx[i]];\n Matrix<T> d(w,h+w);\n rep(i,0,h)rep(j,0,w)d[j][i]=a[i][j];\n rep(i,0,w)d[i][h+i]=1;\n int r=d.gauss(h).size();\n Matrix<T> basis(w-r,w);\n rep(i,r,w)basis[i-r]={d[i].begin()+h,d[i].end()};\n return {res,basis};\n}\n\n/**\n * @brief Linear Equation\n */\n\nusing Fp = fp<2>;\n\nint main() {\nwhile(1) {\n int N, M, D;\n cin >> M >> N >> D;\n if (N == 0) return 0;\n int K = N*M;\n vector A(N,vector<int>(M));\n rep(i,0,N) rep(j,0,M) cin >> A[i][j];\n Matrix<Fp> X(K);\n rep(i,0,N) {\n rep(j,0,M) {\n X[i*M+j][i*M+j] = 1;\n rep(k,0,N) {\n rep(l,0,M) {\n if (abs(i-k)+abs(j-l) == D) {\n X[i*M+j][k*M+l] = 1;\n }\n }\n }\n }\n }\n vector<Fp> Y(K);\n rep(i,0,N) rep(j,0,M) Y[i*M+j] = A[i][j];\n auto Ret = LinearEquation(X,Y);\n if (Ret.first.empty()) cout << 0 << endl;\n else cout << 1 << endl;\n}\n}", "accuracy": 1, "time_ms": 3480, "memory_kb": 10920, "score_of_the_acc": -2, "final_rank": 20 }, { "submission_id": "aoj_1308_8525269", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\n#define rep(i, n) for (int i = 0; i < (n); i++)\n#define per(i, n) for (int i = (n)-1; i >= 0; i--)\n#define rep2(i, l, r) for (int i = (l); i < (r); i++)\n#define per2(i, l, r) for (int i = (r)-1; i >= (l); i--)\n#define each(e, x) for (auto &e : x)\n#define sz(x) (int)x.size()\n#define all(x) begin(x), end(x)\n#define rall(x) rbegin(x), rend(x)\n#define eb emplace_back\n#define MM << ' ' <<\n#define TT template <typename T>\nusing ll = long long;\nusing pii = pair<int, int>;\nusing pll = pair<ll, ll>;\n\nTT bool chmin(T &x, T y) { return x > y ? (x = y, true) : false; }\nTT bool chmax(T &x, T y) { return x < y ? (x = y, true) : false; }\n\nTT using minheap = priority_queue<T, vector<T>, greater<T>>;\nTT using maxheap = priority_queue<T>;\n\nTT void print(vector<T> v, T x = 0) {\n int n = sz(v);\n rep(i, n) cout << v[i] + x << (i == n - 1 ? '\\n' : ' ');\n if (n == 0) cout << '\\n';\n}\n\nTT void printn(vector<T> v, T x = 0) {\n int n = sz(v);\n rep(i, n) cout << v[i] + x << '\\n';\n}\n\nTT void rearrange(vector<T> &v) {\n sort(all(v));\n v.erase(unique(all(v)), end(v));\n}\n\nTT int lb(vector<T> &v, T x) { return lower_bound(all(v), x) - begin(v); }\nTT int ub(vector<T> &v, T x) { return upper_bound(all(v), x) - begin(v); }\n\nconst int inf = (1 << 30) - 1;\nconst ll INF = (1LL << 60) - 1;\n\nstruct io_setup {\n io_setup() {\n ios_base::sync_with_stdio(false);\n cin.tie(NULL);\n cout << fixed << setprecision(15);\n }\n} io_setup;\n\nint main() {\n while (1) {\n int m, n, d;\n cin >> m >> n >> d;\n if (!m)\n break;\n vector<vector<int>> s(n, vector<int>(m));\n rep(i, n) rep(j, m) cin >> s[i][j];\n auto XOR = [&](vector<int> x, const vector<int> &y) {\n rep(i, n) x[i] ^= y[i];\n return x;\n };\n vector<vector<int>> xs;\n rep(i, n) rep(j, m) {\n vector<int> x(n, 0);\n x[i] |= 1 << j;\n rep(k, n) rep(l, m) if (abs(i - k) + abs(j - l) == d) x[k] |= 1 << l;\n each(y, xs) chmin(x, XOR(x, y));\n if (*max_element(all(x)) > 0)\n xs.eb(x);\n }\n vector<int> x(n, 0);\n rep(i, n) rep(j, m) x[i] |= s[i][j] << j;\n each(y, xs) chmin(x, XOR(x, y));\n cout << (*max_element(all(x)) > 0 ? 0 : 1) << '\\n';\n }\n}", "accuracy": 1, "time_ms": 210, "memory_kb": 3496, "score_of_the_acc": -0.0477, "final_rank": 6 }, { "submission_id": "aoj_1308_7992086", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\nusing vl = vector<ll>;\nusing vvl = vector<vl>;\nusing pl = pair<ll,ll>;\nusing vp = vector<pl>;\n#define rep(i,n) for(ll i=0;i<(ll)(n);++i)\n#define reps(i,s,n) for(ll i=(s);i<(ll)(n);++i)\n#define rep1(i,n) for(ll i=1;i<=(ll)(n);++i)\ntemplate<typename T>inline bool chmax(T &a,T b){return a<b?a=b,true:false;}\ntemplate<typename T>inline bool chmin(T &a,T b){return a>b?a=b,true:false;}\n#define fi first\n#define se second\n#define pb push_back\n#define eb emplace_back\n#define be(v) (v).begin(),(v).end()\nconst long long INF = 1e18;\n#ifdef DEBUG\n#include <debug.hpp>\nrandom_device rd;\nmt19937 gen(rd());\nll random(ll l,ll h){\n\tuniform_int_distribution<ll> dist(l,h);\n\treturn dist(gen);\n}\n#endif\n\nconst int MAX_ROW = 25*25; // to be set appropriately\nconst int MAX_COL = 25*25; // to be set appropriately\nstruct BitMatrix {\n\tint H, W;\n\tbitset<MAX_COL> val[MAX_ROW];\n\tBitMatrix(int m = 1, int n = 1) : H(m), W(n) {}\n\tinline bitset<MAX_COL>& operator [] (int i) {return val[i];}\n\tvoid show(){\n\t\trep(h,H)rep(w,W)cout<<val[h][w]<<\" \";\n\t\tcout<<endl;\n\t}\n};\n\nint GaussJordan(BitMatrix &A, bool is_extended = false) {\n\tint rank = 0;\n\tfor (int col = 0; col < A.W; ++col) {\n\t\tif (is_extended && col == A.W - 1) break;\n\t\tint pivot = -1;\n\t\tfor (int row = rank; row < A.H; ++row) {\n\t\t\tif (A[row][col]) {\n\t\t\t\tpivot = row;\n\t\t\t\tbreak;\n\t\t\t}\n\t\t}\n\t\tif (pivot == -1) continue;\n\t\tswap(A[pivot], A[rank]);\n\t\tfor (int row = 0; row < A.H; ++row) {\n\t\t\tif (row != rank && A[row][col]) A[row] ^= A[rank];\n\t\t}\n\t\t++rank;\n\t}\n\treturn rank;\n}\n\nint linear_equation(BitMatrix A, vector<int> b, vector<int> &res) {\n\tint m = A.H, n = A.W;\n\tBitMatrix M(m, n + 1);\n\tfor (int i = 0; i < m; ++i) {\n\t\tfor (int j = 0; j < n; ++j) M[i][j] = A[i][j];\n\t\tM[i][n] = b[i];\n\t}\n\tint rank = GaussJordan(M, true);\n\n\t// check if it has no solution\n\tfor (int row = rank; row < m; ++row) if (M[row][n]) return -1;\n\n\t// answer\n\tres.assign(n, 0);\n\tfor (int i = 0; i < rank; ++i) res[i] = M[i][n];\n\treturn rank;\n}\nll M,N,D;\nvector<int> B;\nbool input(){\n\tcin>>M>>N>>D;\n\tif(M==0)return false;\n\tB.assign(N*M,0);\n\trep(n,N*M){\n\t\tll x;cin>>x;\n\t\tB.at(n) = x;\n\t}\n\treturn true;\n}\n#ifdef DEBUG\nvoid showall(){\n\tshow(M,N,D,B);\n}\n#endif\nvp manhattan(ll y,ll x,ll d){\n\tll H=N,W=M;\n\tvp ret;\n\treps(nx,x-d,x+d+1){\n\t\tfor(ll j=-1;j<=1;j+=2){\n\t\t\tll ny = y + j * (d - abs(nx - x));\n\t\t\tif(ny >= 0 && ny < H && nx >= 0 && nx < W){\n\t\t\t\tret.eb(ny,nx);\n\t\t\t}\n\t\t}\n\t}\n\treturn ret;\n}\nvoid logic(){\n\tBitMatrix A(N*M,N*M);\n\trep(n,N)rep(m,M){\n\t\tA[n*M+m][n*M+m] = 1;\n\t\tfor(auto e:manhattan(n,m,D)){\n\t\t\tA[n*M+m][e.fi*M+e.se] = 1;\n\t\t}\n\t}\n\tvector<int> res(N*M);\n\tll rank = linear_equation(A,B,res);\n\t#ifdef DEBUG\n\tshow(\"A\");A.show();\n\tshow(\"rank res\",rank);show(res);\n\t#endif\n\tcout<< (rank>=0?1:0) <<endl;\n}\n#ifdef DEBUG\nll naive(){\n\treturn 1;\n}\n#endif\nint main(){\n\twhile(input()){\n\t#ifdef DEBUG\n\t\tshowall();\n\t#endif\n\t\tlogic();\n\t}\n\t#ifdef DEBUG\n\t//show(\"naive=\",naive());\n\t#endif\n\t#ifdef DEBUG\n\t/*\n\trep(q,100){\n\t\tN=random(1LL,1000LL);\n\t\tll lans=logic(),nans=naive();\n\t\tshowall();\n\t\tif(lans!=nans){ show(\" WA\",lans,nans); break; }\n\t\telse{ show(\" AC\",lans,nans); }\n\t}\n\t*/\n\t#endif\n\treturn 0;\n}\n//cout << fixed << setprecision(9);", "accuracy": 1, "time_ms": 50, "memory_kb": 3488, "score_of_the_acc": 0, "final_rank": 1 }, { "submission_id": "aoj_1308_7339989", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std ;\n#define fast_io ios::sync_with_stdio(false); cin.tie(nullptr);\n#pragma GCC optimize(\"Ofast,no-stack-protector,unroll-loops,fast-math\")\ntypedef long long ll ;\ntypedef long double ld ;\ntypedef pair<ll,ll> P ;\ntypedef tuple<ll,ll,ll> TP ;\n#define chmin(a,b) a = min(a,b)\n#define chmax(a,b) a = max(a,b)\n#define bit_count(x) __builtin_popcountll(x)\n#define gcd(a,b) __gcd(a,b)\n#define lcm(a,b) a / gcd(a,b) * b\n#define rep(i,n) for(int i = 0 ; i < n ; i++)\n#define rrep(i,a,b) for(int i = a ; i < b ; i++)\n#define repi(it,S) for(auto it = S.begin() ; it != S.end() ; it++)\n#define pt(a) cout << a << endl\n#define DEBUG(...) ; cout << #__VA_ARGS__ << endl ; for(auto x : {__VA_ARGS__}) cout << x << \" \" ; cout << endl ;\n#define DEBUG_LIST(...) cout << #__VA_ARGS__ << endl ; DEBUG_REP(__VA_ARGS__) ;\n#define DEBUG_REP(V) cout << \"{ \" ; repi(itr,V) cout << *itr << \", \" ; cout << \"}\" << endl ;\n#define debug(a) cout << #a << \" \" << a << endl\n#define all(a) a.begin(), a.end()\n#define endl \"\\n\"\n\ntemplate<int MAX_COL, typename T=bool> struct BitMatrix{\n\n private:\n int row, col;\n vector<bitset<MAX_COL>> mat;\n\n void init_(vector<vector<T>> A){\n row = A.size();\n col = A[0].size();\n mat.resize(row);\n for(int i = 0; i < row; i++){\n for(int j = 0; j < col; j++){\n if(A[i][j] != 0) mat[i][j] = 1;\n else mat[i][j] = 0;\n }\n }\n }\n \n void init_(vector<T> A, bool row_matrix = false){\n if(row_matrix) {\n col = (int)A.size();\n row = 1;\n mat.resize(1);\n for(int i = 0; i < col; i++){\n if(A[i] != 0) mat[0][i] = 1;\n else mat[0][i] = 0;\n }\n }\n else {\n col = 1;\n row = (int)A.size();\n mat.resize(row);\n for(int i = 0; i < row; i++){\n if(A[i] != 0) mat[i][0] = 1;\n else mat[i][0] = 0;\n }\n }\n }\n\n void transpose_() {\n vector<bitset<MAX_COL>> res(col);\n rep(i,row) rep(j,col) res[j][i] = mat[i][j];\n mat = res;\n swap(row,col);\n }\n \n void flip_() {\n rep(i,row) rep(j,col) mat[i][j].flip();\n }\n\n void concat_col_(vector<T> &Y) {\n BitMatrix X(Y);\n concat_col_(X);\n }\n void concat_col_(vector<vector<T>> &Y) {\n BitMatrix X(Y);\n concat_col_(X);\n }\n void concat_col_(BitMatrix &Y) {\n assert((int)Y.row == row);\n rep(i,row) {\n rep(j,Y.col) mat[i][j+col] = (Y.mat[i][j]);\n }\n col += Y.col;\n }\n\n void concat_row_(vector<T> &Y) {\n BitMatrix X(Y,true);\n concat_row_(X);\n }\n void concat_row_(vector<vector<T>> &Y) {\n BitMatrix X(Y);\n concat_row_(X);\n }\n void concat_row_(BitMatrix &Y) {\n assert((int)Y.col == col);\n row += Y.row;\n rep(i,Y.row) mat.push_back(Y.mat[i]);\n }\n\n void print_() {\n rep(i,row){\n rep(j,col) cout << mat[i][j]; cout << endl;\n }\n }\n\n public:\n\n inline BitMatrix &operator&=(const BitMatrix Y) {\n rep(i,row) mat[i] &= Y.mat[i];\n return *this ;\n }\n\n inline BitMatrix &operator|=(const BitMatrix Y) {\n rep(i,row) mat[i] |= Y.mat[i];\n return *this ;\n }\n \n inline BitMatrix &operator^=(const BitMatrix Y) {\n rep(i,row) mat[i] ^= Y.mat[i];\n return *this ;\n }\n\n inline BitMatrix operator&(const BitMatrix Y) const { return BitMatrix(*this) &= Y; }\n\n inline BitMatrix operator|(const BitMatrix Y) const { return BitMatrix(*this) |= Y; }\n\n inline BitMatrix operator^(const BitMatrix Y) const { return BitMatrix(*this) += Y; }\n\n inline bool operator==(const BitMatrix Y) const { return mat == Y.mat; }\n\n inline bool operator!=(const BitMatrix Y) const { return mat != Y.mat; }\n\n inline bitset<MAX_COL>&operator[] (int i) {return mat[i]; }\n\n BitMatrix(int n): row(n), col(0) { mat.resize(row); }\n BitMatrix(vector<T> A, bool row_matrix = false) { init_(A, row_matrix); }\n BitMatrix(vector<vector<T>> A){ init_(A); }\n void init(vector<T> A, bool row_matrix = false) { init_(A, row_matrix); }\n void init(vector<vector<T>> A) { init_(A); }\n size_t row_size() { return row; }\n size_t col_size() { return col; }\n void transpose() { transpose_(); }\n void flip() { flip_(); }\n void concat_col(vector<vector<T>> &Y) { concat_col_(Y); }\n void concat_col(vector<T> &Y) { concat_col_(Y); }\n void concat_col(BitMatrix &Y) { concat_col_(Y); }\n void concat_row(vector<vector<T>> &Y) { concat_row_(Y); }\n void concat_row(vector<T> &Y) { concat_row_(Y); }\n void concat_row(BitMatrix &Y) { concat_row_(Y); }\n void print() { print_(); }\n};\n\nconst int MAX_COL = 2510;\nusing Matrix = BitMatrix<MAX_COL, bool>;\n\ntemplate<typename T=bool> struct GaussJordan{\n private:\n int rank;\n vector<bool> solution;\n\n int sweep_out_(Matrix &A , bool is_extended = false){\n rank = 0 ;\n for(int col = 0 ; col < A.col_size() ; col++){\n if(is_extended && col == A.col_size() - 1) break ;\n\n int pivot = -1 ;\n for(int row = rank ; row < A.col_size() ; row++){\n if(A[row][col]){\n pivot = row ;\n break ;\n }\n }\n\n if(pivot == -1) continue ;\n swap(A[pivot] , A[rank]) ;\n for(int row = 0 ; row < A.row_size() ; row++){\n if(row != rank && A[row][col]) A[row] ^= A[rank] ;\n }\n rank++ ;\n }\n return rank ;\n }\n \n vector<bool> solve_simultaneous_equation_(vector<vector<T>> &A , vector<T> &b){\n Matrix X(A), Y(b);\n return solve_simultaneous_equation_(X, Y);\n }\n vector<bool> solve_simultaneous_equation_(Matrix &A , vector<T> &b){\n Matrix Y(b);\n return solve_simultaneous_equation_(A, Y);\n }\n vector<bool> solve_simultaneous_equation_(vector<vector<T>> &A , Matrix &b){\n Matrix X(A);\n return solve_simultaneous_equation_(X, b);\n }\n vector<bool> solve_simultaneous_equation_(Matrix &A , Matrix &b){\n A.concat_col(b);\n return solve_simultaneous_equation_(A);\n }\n vector<bool> solve_simultaneous_equation_(vector<vector<T>> &A){\n return solve_simultaneous_equation_(to_matrix(A));\n }\n vector<bool> solve_simultaneous_equation_(Matrix &M){\n\n int n = M.row_size() , m = M.col_size();\n\n rank = sweep_out_(M,true);\n\n for(int row = rank ; row < n ; row++) if(M[row][n]) return {};\n\n vector<bool> res;\n res.resize(rank);\n for(int i = 0 ; i < rank; i++) res[i] = M[i][m];\n\n return solution = res;\n }\n\n public:\n GaussJordan(){}\n int sweep_out(Matrix &A) { return sweep_out_(A, false); }\n int sweep_out(Matrix &A, bool is_extended) { return sweep_out_(A, is_extended); }\n vector<bool> solve_simultaneous_equation(Matrix &A , Matrix &b) { return solve_simultaneous_equation_(A, b); }\n vector<bool> solve_simultaneous_equation(Matrix &A , vector<T> &b) { return solve_simultaneous_equation_(A, b); }\n vector<bool> solve_simultaneous_equation(vector<vector<T>> &A , vector<T> &b) { return solve_simultaneous_equation_(A, b); }\n vector<bool> solve_simultaneous_equation(vector<vector<T>> &A , Matrix &b) { return solve_simultaneous_equation_(A, b); }\n vector<bool> solve_simultaneous_equation(vector<vector<T>> &A) { return solve_simultaneous_equation_(A); }\n vector<bool> solve_simultaneous_equation(Matrix A) { return solve_simultaneous_equation_(A); }\n int get_rank() { return rank; }\n vector<T> get_solution() { return solution; }\n};\n\nint n, m, d;\nvector<vector<char>> S;\nvector<vector<bool>> A;\n\nbool solve(){\n S.clear();\n A.clear();\n cin >> m >> n >> d;\n if(m == 0 && n == 0 && d == 0) return false;\n S.resize(n, vector<char>(m));\n A.resize(n, vector<bool>(m));\n rep(i,n) rep(j,m) cin >> S[i][j], A[i][j] = S[i][j] - '0';\n vector<vector<bool>> M(n*m,vector<bool>(n*m+1,0));\n vector<bool> res(n*m);\n rep(i,n) rep(j,m){\n M[i*m+j][i*m+j] = 1;\n rep(x,n) rep(y,m){\n int dist = abs(i - x) + abs(j - y);\n if(dist == d) {\n M[i*m+j][x*m+y] = 1;\n }\n }\n if(!A[i][j]) M[i*m+j][n*m] = 1;\n }\n GaussJordan gj;\n\n auto ans = gj.solve_simultaneous_equation(Matrix(M));\n if(ans.empty()) cout << 0 << endl;\n else cout << 1 << endl;\n return 1;\n}\n\nint main(){\n fast_io\n while(1){\n if(!solve()) {\n return 0;\n }\n }\n}", "accuracy": 1, "time_ms": 70, "memory_kb": 3876, "score_of_the_acc": -0.058, "final_rank": 9 }, { "submission_id": "aoj_1308_7339969", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std ;\n#define fast_io ios::sync_with_stdio(false); cin.tie(nullptr);\n#pragma GCC optimize(\"Ofast,no-stack-protector,unroll-loops,fast-math\")\ntypedef long long ll ;\ntypedef long double ld ;\ntypedef pair<ll,ll> P ;\ntypedef tuple<ll,ll,ll> TP ;\n#define chmin(a,b) a = min(a,b)\n#define chmax(a,b) a = max(a,b)\n#define bit_count(x) __builtin_popcountll(x)\n#define gcd(a,b) __gcd(a,b)\n#define lcm(a,b) a / gcd(a,b) * b\n#define rep(i,n) for(int i = 0 ; i < n ; i++)\n#define rrep(i,a,b) for(int i = a ; i < b ; i++)\n#define repi(it,S) for(auto it = S.begin() ; it != S.end() ; it++)\n#define pt(a) cout << a << endl\n#define DEBUG(...) ; cout << #__VA_ARGS__ << endl ; for(auto x : {__VA_ARGS__}) cout << x << \" \" ; cout << endl ;\n#define DEBUG_LIST(...) cout << #__VA_ARGS__ << endl ; DEBUG_REP(__VA_ARGS__) ;\n#define DEBUG_REP(V) cout << \"{ \" ; repi(itr,V) cout << *itr << \", \" ; cout << \"}\" << endl ;\n#define debug(a) cout << #a << \" \" << a << endl\n#define all(a) a.begin(), a.end()\n#define endl \"\\n\"\n\ntemplate<int MAX_COL, typename T=bool> struct BitMatrix{\n\n private:\n int row, col;\n vector<bitset<MAX_COL>> mat;\n\n void init_(vector<vector<T>> A){\n row = A.size();\n col = A[0].size();\n mat.resize(row);\n for(int i = 0; i < row; i++){\n for(int j = 0; j < col; j++){\n if(A[i][j] != 0) mat[i][j] = 1;\n else mat[i][j] = 0;\n }\n }\n }\n \n void init_(vector<T> A, bool row_matrix = false){\n if(row_matrix) {\n col = (int)A.size();\n row = 1;\n mat.resize(1);\n for(int i = 0; i < col; i++){\n if(A[i] != 0) mat[0][i] = 1;\n else mat[0][i] = 0;\n }\n }\n else {\n col = 1;\n row = (int)A.size();\n mat.resize(row);\n for(int i = 0; i < row; i++){\n if(A[i] != 0) mat[i][0] = 1;\n else mat[i][0] = 0;\n }\n }\n }\n\n void transpose_() {\n vector<bitset<MAX_COL>> res(col);\n rep(i,row) rep(j,col) res[j][i] = mat[i][j];\n mat = res;\n swap(row,col);\n }\n \n void flip_() {\n rep(i,row) rep(j,col) mat[i][j].flip();\n }\n\n void concat_col_(vector<T> &Y) {\n BitMatrix X(Y);\n concat_col_(X);\n }\n void concat_col_(vector<vector<T>> &Y) {\n BitMatrix X(Y);\n concat_col_(X);\n }\n void concat_col_(BitMatrix &Y) {\n assert((int)Y.row == row);\n rep(i,row) {\n rep(j,Y.col) mat[i][j+col] = (Y.mat[i][j]);\n }\n col += Y.col;\n }\n\n void concat_row_(vector<T> &Y) {\n BitMatrix X(Y,true);\n concat_row_(X);\n }\n void concat_row_(vector<vector<T>> &Y) {\n BitMatrix X(Y);\n concat_row_(X);\n }\n void concat_row_(BitMatrix &Y) {\n assert((int)Y.col == col);\n row += Y.row;\n rep(i,Y.row) mat.push_back(Y.mat[i]);\n }\n\n void print_() {\n rep(i,row){\n rep(j,col) cout << mat[i][j]; cout << endl;\n }\n }\n\n public:\n\n inline BitMatrix &operator&=(const BitMatrix Y) {\n rep(i,row) mat[i] &= Y.mat[i];\n return *this ;\n }\n\n inline BitMatrix &operator|=(const BitMatrix Y) {\n rep(i,row) mat[i] |= Y.mat[i];\n return *this ;\n }\n \n inline BitMatrix &operator^=(const BitMatrix Y) {\n rep(i,row) mat[i] ^= Y.mat[i];\n return *this ;\n }\n\n inline BitMatrix operator&(const BitMatrix Y) const { return BitMatrix(*this) &= Y; }\n\n inline BitMatrix operator|(const BitMatrix Y) const { return BitMatrix(*this) |= Y; }\n\n inline BitMatrix operator^(const BitMatrix Y) const { return BitMatrix(*this) += Y; }\n\n inline bool operator==(const BitMatrix Y) const { return mat == Y.mat; }\n\n inline bool operator!=(const BitMatrix Y) const { return mat != Y.mat; }\n\n inline bitset<MAX_COL>&operator[] (int i) {return mat[i]; }\n\n BitMatrix(int n): row(n), col(0) { mat.resize(row); }\n BitMatrix(vector<T> A, bool row_matrix = false) { init_(A, row_matrix); }\n BitMatrix(vector<vector<T>> A){ init_(A); }\n void init(vector<T> A, bool row_matrix = false) { init_(A, row_matrix); }\n void init(vector<vector<T>> A) { init_(A); }\n size_t row_size() { return row; }\n size_t col_size() { return col; }\n void transpose() { transpose_(); }\n void flip() { flip_(); }\n void concat_col(vector<vector<T>> &Y) { concat_col_(Y); }\n void concat_col(vector<T> &Y) { concat_col_(Y); }\n void concat_col(BitMatrix &Y) { concat_col_(Y); }\n void concat_row(vector<vector<T>> &Y) { concat_row_(Y); }\n void concat_row(vector<T> &Y) { concat_row_(Y); }\n void concat_row(BitMatrix &Y) { concat_row_(Y); }\n void print() { print_(); }\n};\n\nconst int MAX_COL = 2510;\nusing Matrix = BitMatrix<MAX_COL, bool>;\n\ntemplate<typename T=bool> struct GaussJordan{\n private:\n int rank;\n vector<bool> solution;\n\n int sweep_out_(Matrix &A , bool is_extended = false){\n rank = 0 ;\n for(int col = 0 ; col < A.col_size() ; col++){\n if(is_extended && col == A.col_size() - 1) break ;\n\n int pivot = -1 ;\n for(int row = rank ; row < A.col_size() ; row++){\n if(A[row][col]){\n pivot = row ;\n break ;\n }\n }\n\n if(pivot == -1) continue ;\n swap(A[pivot] , A[rank]) ;\n for(int row = 0 ; row < A.row_size() ; row++){\n if(row != rank && A[row][col]) A[row] ^= A[rank] ;\n }\n rank++ ;\n }\n return rank ;\n }\n \n vector<bool> solve_simultaneous_equation_(vector<vector<T>> &A , vector<T> &b){\n Matrix X(A), Y(b);\n return solve_simultaneous_equation_(X, Y);\n }\n vector<bool> solve_simultaneous_equation_(Matrix &A , vector<T> &b){\n Matrix Y(b);\n return solve_simultaneous_equation_(A, Y);\n }\n vector<bool> solve_simultaneous_equation_(vector<vector<T>> &A , Matrix &b){\n Matrix X(A);\n return solve_simultaneous_equation_(X, b);\n }\n vector<bool> solve_simultaneous_equation_(Matrix &A , Matrix &b){\n A.concat_col(b);\n return solve_simultaneous_equation_(A);\n }\n vector<bool> solve_simultaneous_equation_(vector<vector<T>> &A){\n return solve_simultaneous_equation_(to_matrix(A));\n }\n vector<bool> solve_simultaneous_equation_(Matrix &M){\n\n int n = M.row_size() , m = M.col_size();\n\n rank = sweep_out_(M,true);\n\n for(int row = rank ; row < n ; row++) if(M[row][n]) return {};\n\n vector<bool> res;\n res.resize(rank);\n for(int i = 0 ; i < rank; i++) res[i] = M[i][m];\n\n return solution = res;\n }\n\n public:\n GaussJordan(){}\n int sweep_out(Matrix &A) { return sweep_out_(A, false); }\n int sweep_out(Matrix &A, bool is_extended) { return sweep_out_(A, is_extended); }\n vector<bool> solve_simultaneous_equation(Matrix &A , Matrix &b) { return solve_simultaneous_equation_(A, b); }\n vector<bool> solve_simultaneous_equation(Matrix &A , vector<T> &b) { return solve_simultaneous_equation_(A, b); }\n vector<bool> solve_simultaneous_equation(vector<vector<T>> &A , vector<T> &b) { return solve_simultaneous_equation_(A, b); }\n vector<bool> solve_simultaneous_equation(vector<vector<T>> &A , Matrix &b) { return solve_simultaneous_equation_(A, b); }\n vector<bool> solve_simultaneous_equation(vector<vector<T>> &A) { return solve_simultaneous_equation_(A); }\n vector<bool> solve_simultaneous_equation(Matrix A) { return solve_simultaneous_equation_(A); }\n int get_rank() { return rank; }\n vector<T> get_solution() { return solution; }\n};\n\nint n, m, d;\nvector<vector<char>> S;\nvector<vector<bool>> A;\n\nbool solve(){\n S.clear();\n A.clear();\n cin >> m >> n >> d;\n if(m == 0 && n == 0 && d == 0) return false;\n S.resize(n, vector<char>(m));\n A.resize(n, vector<bool>(m));\n rep(i,n) rep(j,m) cin >> S[i][j], A[i][j] = S[i][j] - '0';\n vector<vector<bool>> M(n*m,vector<bool>(n*m,0));\n vector<bool> res(n*m);\n rep(i,n) rep(j,m){\n M[i*m+j][i*m+j] = 1;\n rep(x,n) rep(y,m){\n int dist = abs(i - x) + abs(j - y);\n if(dist == d) {\n M[i*m+j][x*m+y] = 1;\n }\n }\n if(!A[i][j]) res[i*m+j] = 1;\n }\n GaussJordan gj;\n auto ans = gj.solve_simultaneous_equation(M,res);\n if(ans.empty()) cout << 0 << endl;\n else cout << 1 << endl;\n return 1;\n}\n\nint main(){\n fast_io\n while(1){\n if(!solve()) {\n return 0;\n }\n }\n}", "accuracy": 1, "time_ms": 70, "memory_kb": 3852, "score_of_the_acc": -0.0548, "final_rank": 8 }, { "submission_id": "aoj_1308_7339747", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std ;\n#define fast_io ios::sync_with_stdio(false); cin.tie(nullptr);\n#pragma GCC optimize(\"Ofast,no-stack-protector,unroll-loops,fast-math\")\ntypedef long long ll ;\ntypedef long double ld ;\ntypedef pair<ll,ll> P ;\ntypedef tuple<ll,ll,ll> TP ;\n#define chmin(a,b) a = min(a,b)\n#define chmax(a,b) a = max(a,b)\n#define bit_count(x) __builtin_popcountll(x)\n#define gcd(a,b) __gcd(a,b)\n#define lcm(a,b) a / gcd(a,b) * b\n#define rep(i,n) for(int i = 0 ; i < n ; i++)\n#define rrep(i,a,b) for(int i = a ; i < b ; i++)\n#define repi(it,S) for(auto it = S.begin() ; it != S.end() ; it++)\n#define pt(a) cout << a << endl\n#define DEBUG(...) ; cout << #__VA_ARGS__ << endl ; for(auto x : {__VA_ARGS__}) cout << x << \" \" ; cout << endl ;\n#define DEBUG_LIST(...) cout << #__VA_ARGS__ << endl ; DEBUG_REP(__VA_ARGS__) ;\n#define DEBUG_REP(V) cout << \"{ \" ; repi(itr,V) cout << *itr << \", \" ; cout << \"}\" << endl ;\n#define debug(a) cout << #a << \" \" << a << endl\n#define all(a) a.begin(), a.end()\n#define endl \"\\n\"\n\ntemplate<int MAX_COL, typename T=bool> struct BitMatrix{\n\n private:\n int row, col;\n vector<bitset<MAX_COL>> mat;\n\n void init_(vector<vector<T>> A){\n row = A.size();\n col = A[0].size();\n mat.resize(row);\n for(int i = 0; i < row; i++){\n for(int j = 0; j < col; j++){\n if(A[i][j] != 0) mat[i][j] = 1;\n else mat[i][j] = 0;\n }\n }\n }\n \n void init_(vector<T> A, bool row_matrix = false){\n if(row_matrix) {\n col = (int)A.size();\n row = 1;\n mat.resize(1);\n for(int i = 0; i < col; i++){\n if(A[i] != 0) mat[0][i] = 1;\n else mat[0][i] = 0;\n }\n }\n else {\n col = 1;\n row = (int)A.size();\n mat.resize(row);\n for(int i = 0; i < row; i++){\n if(A[i] != 0) mat[i][0] = 1;\n else mat[i][0] = 0;\n }\n }\n }\n\n void transpose_() {\n vector<bitset<MAX_COL>> res(col);\n rep(i,row) rep(j,col) res[j][i] = mat[i][j];\n mat = res;\n swap(row,col);\n }\n \n void flip_() {\n rep(i,row) rep(j,col) mat[i][j].flip();\n }\n\n void concat_col_(vector<T> &Y) {\n BitMatrix X(Y);\n concat_col_(X);\n }\n void concat_col_(vector<vector<T>> &Y) {\n BitMatrix X(Y);\n concat_col_(X);\n }\n void concat_col_(BitMatrix &Y) {\n assert((int)Y.row == row);\n rep(i,row) {\n rep(j,Y.col) mat[i][j+col] = (Y.mat[i][j]);\n }\n col += Y.col;\n }\n\n void concat_row_(vector<T> &Y) {\n BitMatrix X(Y,true);\n concat_row_(X);\n }\n void concat_row_(vector<vector<T>> &Y) {\n BitMatrix X(Y);\n concat_row_(X);\n }\n void concat_row_(BitMatrix &Y) {\n assert((int)Y.col == col);\n row += Y.row;\n rep(i,Y.row) mat.push_back(Y.mat[i]);\n }\n\n void print_() {\n rep(i,row){\n rep(j,col) cout << mat[i][j]; cout << endl;\n }\n }\n\n public:\n\n inline BitMatrix &operator&=(const BitMatrix Y) {\n rep(i,row) mat[i] &= Y.mat[i];\n return *this ;\n }\n\n inline BitMatrix &operator|=(const BitMatrix Y) {\n rep(i,row) mat[i] |= Y.mat[i];\n return *this ;\n }\n \n inline BitMatrix &operator^=(const BitMatrix Y) {\n rep(i,row) mat[i] ^= Y.mat[i];\n return *this ;\n }\n\n inline BitMatrix operator&(const BitMatrix Y) const { return BitMatrix(*this) &= Y; }\n\n inline BitMatrix operator|(const BitMatrix Y) const { return BitMatrix(*this) |= Y; }\n\n inline BitMatrix operator^(const BitMatrix Y) const { return BitMatrix(*this) += Y; }\n\n inline bool operator==(const BitMatrix Y) const { return mat == Y.mat; }\n\n inline bool operator!=(const BitMatrix Y) const { return mat != Y.mat; }\n\n inline bitset<MAX_COL>&operator[] (int i) {return mat[i]; }\n\n BitMatrix(int n): row(n), col(0) { mat.resize(row); }\n BitMatrix(vector<T> A, bool row_matrix = false) { init_(A, row_matrix); }\n BitMatrix(vector<vector<T>> A){ init_(A); }\n void init(vector<T> A, bool row_matrix = false) { init_(A, row_matrix); }\n void init(vector<vector<T>> A) { init_(A); }\n size_t row_size() { return row; }\n size_t col_size() { return col; }\n void transpose() { transpose_(); }\n void flip() { flip_(); }\n void concat_col(vector<vector<T>> &Y) { concat_col_(Y); }\n void concat_col(vector<T> &Y) { concat_col_(Y); }\n void concat_col(BitMatrix &Y) { concat_col_(Y); }\n void concat_row(vector<vector<T>> &Y) { concat_row_(Y); }\n void concat_row(vector<T> &Y) { concat_row_(Y); }\n void concat_row(BitMatrix &Y) { concat_row_(Y); }\n void print() { print_(); }\n};\n\nconst int MAX_COL = 2510;\nusing Matrix = BitMatrix<MAX_COL, bool>;\n\ntemplate<typename T=bool> struct GaussJordan{\n private:\n int rank;\n vector<bool> solution;\n \n int sweep_out_(Matrix &A , bool is_extended = false){\n rank = 0 ;\n for(int col = 0 ; col < A.col_size() ; col++){\n if(is_extended && col == A.col_size() - 1) break ;\n\n int pivot = -1 ;\n for(int row = rank ; row < A.col_size() ; row++){\n if(A[row][col]){\n pivot = row ;\n break ;\n }\n }\n\n if(pivot == -1) continue ;\n swap(A[pivot] , A[rank]) ;\n for(int row = 0 ; row < A.row_size() ; row++){\n if(row != rank && A[row][col]) A[row] ^= A[rank] ;\n }\n rank++ ;\n }\n return rank ;\n }\n \n vector<bool> solve_simultaneous_equation_(vector<vector<T>> &A , vector<T> &b){\n Matrix X(A), Y(b);\n return solve_simultaneous_equation_(X,Y);\n }\n vector<bool> solve_simultaneous_equation_(Matrix &A , vector<T> &b){\n return solve_simultaneous_equation_(A, Matrix(b));\n }\n vector<bool> solve_simultaneous_equation_(vector<vector<T>> &A , Matrix &b){\n return solve_simultaneous_equation_(Matrix(A), b);\n }\n vector<bool> solve_simultaneous_equation_(Matrix &A , Matrix &b){\n A.concat_col(b);\n return solve_simultaneous_equation_(A);\n }\n vector<bool> solve_simultaneous_equation_(vector<vector<T>> &A){\n Matrix M(A);\n return solve_simultaneous_equation_(M);\n }\n vector<bool> solve_simultaneous_equation_(Matrix &M){\n\n int n = M.row_size() , m = M.col_size();\n\n rank = sweep_out_(M,true);\n\n for(int row = rank ; row < n ; row++) if(M[row][n]) return {};\n\n vector<bool> res;\n res.resize(rank);\n for(int i = 0 ; i < rank; i++) res[i] = M[i][m];\n\n return solution = res;\n }\n\n public:\n GaussJordan(){}\n int sweep_out(Matrix &A) { return sweep_out_(A, false); }\n int sweep_out(Matrix &A, bool is_extended) { return sweep_out_(A, is_extended); }\n vector<bool> solve_simultaneous_equation(Matrix &A , Matrix &b) { return solve_simultaneous_equation_(A, b); }\n vector<bool> solve_simultaneous_equation(Matrix &A , vector<T> &b) { return solve_simultaneous_equation_(A, b); }\n vector<bool> solve_simultaneous_equation(vector<vector<T>> &A , vector<T> &b) { return solve_simultaneous_equation_(A, b); }\n vector<bool> solve_simultaneous_equation(vector<vector<T>> &A , Matrix &b) { return solve_simultaneous_equation_(A, b); }\n vector<bool> solve_simultaneous_equation(vector<vector<T>> &A) { return solve_simultaneous_equation_(A); }\n vector<bool> solve_simultaneous_equation(Matrix A) { return solve_simultaneous_equation_(A); }\n int get_rank() { return rank; }\n vector<T> get_solution() { return solution; }\n};\n\nint n, m, d;\nvector<vector<char>> S;\nvector<vector<bool>> A;\n\nbool solve(){\n S.clear();\n A.clear();\n cin >> m >> n >> d;\n if(m == 0 && n == 0 && d == 0) return false;\n S.resize(n, vector<char>(m));\n A.resize(n, vector<bool>(m));\n rep(i,n) rep(j,m) cin >> S[i][j], A[i][j] = S[i][j] - '0';\n vector<vector<bool>> M(n*m,vector<bool>(n*m,0));\n vector<bool> res(n*m);\n rep(i,n) rep(j,m){\n M[i*m+j][i*m+j] = 1;\n rep(x,n) rep(y,m){\n int dist = abs(i - x) + abs(j - y);\n if(dist == d) {\n M[i*m+j][x*m+y] = 1;\n }\n }\n if(!A[i][j]) res[i*m+j] = 1;\n }\n GaussJordan gj;\n auto ans = gj.solve_simultaneous_equation(M,res);\n if(ans.empty()) cout << 0 << endl;\n else cout << 1 << endl;\n return 1;\n}\n\nint main(){\n fast_io\n while(1){\n if(!solve()) {\n return 0;\n }\n }\n}", "accuracy": 1, "time_ms": 70, "memory_kb": 3748, "score_of_the_acc": -0.0408, "final_rank": 5 }, { "submission_id": "aoj_1308_7339729", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std ;\n#define fast_io ios::sync_with_stdio(false); cin.tie(nullptr);\n#pragma GCC optimize(\"Ofast,no-stack-protector,unroll-loops,fast-math\")\ntypedef long long ll ;\ntypedef long double ld ;\ntypedef pair<ll,ll> P ;\ntypedef tuple<ll,ll,ll> TP ;\n#define chmin(a,b) a = min(a,b)\n#define chmax(a,b) a = max(a,b)\n#define bit_count(x) __builtin_popcountll(x)\n#define gcd(a,b) __gcd(a,b)\n#define lcm(a,b) a / gcd(a,b) * b\n#define rep(i,n) for(int i = 0 ; i < n ; i++)\n#define rrep(i,a,b) for(int i = a ; i < b ; i++)\n#define repi(it,S) for(auto it = S.begin() ; it != S.end() ; it++)\n#define pt(a) cout << a << endl\n#define DEBUG(...) ; cout << #__VA_ARGS__ << endl ; for(auto x : {__VA_ARGS__}) cout << x << \" \" ; cout << endl ;\n#define DEBUG_LIST(...) cout << #__VA_ARGS__ << endl ; DEBUG_REP(__VA_ARGS__) ;\n#define DEBUG_REP(V) cout << \"{ \" ; repi(itr,V) cout << *itr << \", \" ; cout << \"}\" << endl ;\n#define debug(a) cout << #a << \" \" << a << endl\n#define all(a) a.begin(), a.end()\n#define endl \"\\n\"\n\ntemplate<int MAX_COL, typename T=bool> struct BitMatrix{\n\n private:\n int row, col;\n vector<bitset<MAX_COL>> mat;\n\n void init_(vector<vector<T>> A){\n row = A.size();\n col = A[0].size();\n mat.resize(row);\n for(int i = 0; i < row; i++){\n for(int j = 0; j < col; j++){\n if(A[i][j] != 0) mat[i][j] = 1;\n else mat[i][j] = 0;\n }\n }\n }\n \n void init_(vector<T> A, bool row_matrix = false){\n if(row_matrix) {\n col = (int)A.size();\n row = 1;\n mat.resize(1);\n for(int i = 0; i < col; i++){\n if(A[i] != 0) mat[0][i] = 1;\n else mat[0][i] = 0;\n }\n }\n else {\n col = 1;\n row = (int)A.size();\n mat.resize(row);\n for(int i = 0; i < row; i++){\n if(A[i] != 0) mat[i][0] = 1;\n else mat[i][0] = 0;\n }\n }\n }\n\n void transpose_() {\n vector<bitset<MAX_COL>> res(col);\n rep(i,row) rep(j,col) res[j][i] = mat[i][j];\n mat = res;\n swap(row,col);\n }\n \n void flip_() {\n rep(i,row) rep(j,col) mat[i][j].flip();\n }\n\n void concat_col_(vector<T> &Y) {\n BitMatrix X(Y);\n concat_col_(X);\n }\n void concat_col_(vector<vector<T>> &Y) {\n BitMatrix X(Y);\n concat_col_(X);\n }\n void concat_col_(BitMatrix &Y) {\n assert((int)Y.row == row);\n rep(i,row) {\n rep(j,Y.col) mat[i][j+col] = (Y.mat[i][j]);\n }\n col += Y.col;\n }\n\n void concat_row_(vector<T> &Y) {\n BitMatrix X(Y,true);\n concat_row_(X);\n }\n void concat_row_(vector<vector<T>> &Y) {\n BitMatrix X(Y);\n concat_row_(X);\n }\n void concat_row_(BitMatrix &Y) {\n assert((int)Y.col == col);\n row += Y.row;\n rep(i,Y.row) mat.push_back(Y.mat[i]);\n }\n\n void print_() {\n rep(i,row){\n rep(j,col) cout << mat[i][j]; cout << endl;\n }\n }\n\n public:\n\n inline BitMatrix &operator&=(const BitMatrix Y) {\n rep(i,row) mat[i] &= Y.mat[i];\n return *this ;\n }\n\n inline BitMatrix &operator|=(const BitMatrix Y) {\n rep(i,row) mat[i] |= Y.mat[i];\n return *this ;\n }\n \n inline BitMatrix &operator^=(const BitMatrix Y) {\n rep(i,row) mat[i] ^= Y.mat[i];\n return *this ;\n }\n\n inline BitMatrix operator&(const BitMatrix Y) const { return BitMatrix(*this) &= Y; }\n\n inline BitMatrix operator|(const BitMatrix Y) const { return BitMatrix(*this) |= Y; }\n\n inline BitMatrix operator^(const BitMatrix Y) const { return BitMatrix(*this) += Y; }\n\n inline bool operator==(const BitMatrix Y) const { return mat == Y.mat; }\n\n inline bool operator!=(const BitMatrix Y) const { return mat != Y.mat; }\n\n inline bitset<MAX_COL>&operator[] (int i) {return mat[i]; }\n\n BitMatrix(int n): row(n), col(0) { mat.resize(row); }\n BitMatrix(vector<T> A, bool row_matrix = false) { init_(A, row_matrix); }\n BitMatrix(vector<vector<T>> A){ init_(A); }\n void init(vector<T> A, bool row_matrix = false) { init_(A, row_matrix); }\n void init(vector<vector<T>> A) { init_(A); }\n size_t row_size() { return row; }\n size_t col_size() { return col; }\n void transpose() { transpose_(); }\n void flip() { flip_(); }\n void concat_col(vector<vector<T>> &Y) { concat_col_(Y); }\n void concat_col(vector<T> &Y) { concat_col_(Y); }\n void concat_col(BitMatrix &Y) { concat_col_(Y); }\n void concat_row(vector<vector<T>> &Y) { concat_row_(Y); }\n void concat_row(vector<T> &Y) { concat_row_(Y); }\n void concat_row(BitMatrix &Y) { concat_row_(Y); }\n void print() { print_(); }\n};\n\nconst int MAX_COL = 2510;\nusing Matrix = BitMatrix<MAX_COL, bool>;\n\ntemplate<typename T=bool> struct GaussJordan{\n private:\n int rank;\n vector<bool> solution;\n \n int sweep_out_(Matrix &A , bool is_extended = false){\n rank = 0 ;\n for(int col = 0 ; col < A.col_size() ; col++){\n if(is_extended && col == A.col_size() - 1) break ;\n\n int pivot = -1 ;\n for(int row = rank ; row < A.col_size() ; row++){\n if(A[row][col]){\n pivot = row ;\n break ;\n }\n }\n\n if(pivot == -1) continue ;\n swap(A[pivot] , A[rank]) ;\n for(int row = 0 ; row < A.row_size() ; row++){\n if(row != rank && A[row][col]) A[row] ^= A[rank] ;\n }\n rank++ ;\n }\n return rank ;\n }\n \n vector<bool> solve_simultaneous_equation_(vector<vector<T>> &A , vector<T> &b){\n return solve_simultaneous_equation_(Matrix(A), Matrix(b));\n }\n vector<bool> solve_simultaneous_equation_(Matrix &A , vector<T> &b){\n return solve_simultaneous_equation_(A, Matrix(b));\n }\n vector<bool> solve_simultaneous_equation_(vector<vector<T>> &A , Matrix &b){\n return solve_simultaneous_equation_(Matrix(A), b);\n }\n vector<bool> solve_simultaneous_equation_(Matrix &A , Matrix &b){\n return solve_simultaneous_equation_(A.concat_col(b));\n }\n vector<bool> solve_simultaneous_equation_(vector<vector<T>> &A){\n Matrix M(A);\n return solve_simultaneous_equation_(M);\n }\n vector<bool> solve_simultaneous_equation_(Matrix &M){\n\n int n = M.row_size() , m = M.col_size();\n\n rank = sweep_out_(M,true);\n\n for(int row = rank ; row < n ; row++) if(M[row][n]) return {};\n\n vector<bool> res;\n res.resize(rank);\n for(int i = 0 ; i < rank; i++) res[i] = M[i][m];\n\n return solution = res;\n }\n\n public:\n GaussJordan(){}\n int sweep_out(Matrix &A) { return sweep_out_(A, false); }\n int sweep_out(Matrix &A, bool is_extended) { return sweep_out_(A, is_extended); }\n vector<bool> solve_simultaneous_equation(Matrix &A , Matrix &b) { return solve_simultaneous_equation_(A, b); }\n vector<bool> solve_simultaneous_equation(Matrix &A , vector<T> &b) { return solve_simultaneous_equation_(A, b); }\n vector<bool> solve_simultaneous_equation(vector<vector<T>> &A , vector<T> &b) { return solve_simultaneous_equation_(A, b); }\n vector<bool> solve_simultaneous_equation(vector<vector<T>> &A , Matrix &b) { return solve_simultaneous_equation_(A, b); }\n vector<bool> solve_simultaneous_equation(vector<vector<T>> &A) { return solve_simultaneous_equation_(A); }\n vector<bool> solve_simultaneous_equation(Matrix A) { return solve_simultaneous_equation_(A); }\n int get_rank() { return rank; }\n vector<T> get_solution() { return solution; }\n};\n\nint n, m, d;\nvector<vector<char>> S;\nvector<vector<bool>> A;\n\nbool solve(){\n S.clear();\n A.clear();\n cin >> m >> n >> d;\n if(m == 0 && n == 0 && d == 0) return false;\n S.resize(n, vector<char>(m));\n A.resize(n, vector<bool>(m));\n rep(i,n) rep(j,m) cin >> S[i][j], A[i][j] = S[i][j] - '0';\n vector<vector<bool>> M(n*m,vector<bool>(n*m+1,0));\n rep(i,n) rep(j,m){\n M[i*m+j][i*m+j] = 1;\n rep(x,n) rep(y,m){\n int dist = abs(i - x) + abs(j - y);\n if(dist == d) {\n M[i*m+j][x*m+y] = 1;\n }\n }\n if(!A[i][j]) M[i*m+j][n*m] = 1;\n }\n GaussJordan gj;\n auto res = gj.solve_simultaneous_equation(M);\n if(res.empty()) cout << 0 << endl;\n else cout << 1 << endl;\n return 1;\n}\n\nint main(){\n fast_io\n while(1){\n if(!solve()) {\n return 0;\n }\n }\n}", "accuracy": 1, "time_ms": 70, "memory_kb": 3828, "score_of_the_acc": -0.0516, "final_rank": 7 }, { "submission_id": "aoj_1308_7339652", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std ;\n#define fast_io ios::sync_with_stdio(false); cin.tie(nullptr);\n#pragma GCC optimize(\"Ofast,no-stack-protector,unroll-loops,fast-math\")\ntypedef long long ll ;\ntypedef long double ld ;\ntypedef pair<ll,ll> P ;\ntypedef tuple<ll,ll,ll> TP ;\n#define chmin(a,b) a = min(a,b)\n#define chmax(a,b) a = max(a,b)\n#define bit_count(x) __builtin_popcountll(x)\n#define gcd(a,b) __gcd(a,b)\n#define lcm(a,b) a / gcd(a,b) * b\n#define rep(i,n) for(int i = 0 ; i < n ; i++)\n#define rrep(i,a,b) for(int i = a ; i < b ; i++)\n#define repi(it,S) for(auto it = S.begin() ; it != S.end() ; it++)\n#define pt(a) cout << a << endl\n#define DEBUG(...) ; cout << #__VA_ARGS__ << endl ; for(auto x : {__VA_ARGS__}) cout << x << \" \" ; cout << endl ;\n#define DEBUG_LIST(...) cout << #__VA_ARGS__ << endl ; DEBUG_REP(__VA_ARGS__) ;\n#define DEBUG_REP(V) cout << \"{ \" ; repi(itr,V) cout << *itr << \", \" ; cout << \"}\" << endl ;\n#define debug(a) cout << #a << \" \" << a << endl\n#define all(a) a.begin(), a.end()\n#define endl \"\\n\"\n\ntemplate<int MAX_COL, typename T=bool> struct BitMatrix{\n\n private:\n int row, col;\n vector<bitset<MAX_COL>> mat;\n\n void init_(vector<vector<T>> A){\n row = A.size();\n col = A[0].size();\n mat.resize(row);\n for(int i = 0; i < row; i++){\n for(int j = 0; j < col; j++){\n if(A[i][j] != 0) mat[i][j] = 1;\n else mat[i][j] = 0;\n }\n }\n }\n \n void init_(vector<T> A, bool row_matrix = false){\n if(row_matrix) {\n col = (int)A.size();\n row = 1;\n mat.resize(1);\n for(int i = 0; i < col; i++){\n if(A[i] != 0) mat[0][i] = 1;\n else mat[0][i] = 0;\n }\n }\n else {\n col = 1;\n row = (int)A.size();\n mat.resize(row);\n for(int i = 0; i < row; i++){\n if(A[i] != 0) mat[i][0] = 1;\n else mat[i][0] = 0;\n }\n }\n }\n\n void transpose_() {\n vector<bitset<MAX_COL>> res(col);\n rep(i,row) rep(j,col) res[j][i] = mat[i][j];\n mat = res;\n swap(row,col);\n }\n \n void flip_() {\n rep(i,row) rep(j,col) mat[i][j].flip();\n }\n\n void concat_col_(vector<T> &Y) {\n BitMatrix X(Y);\n concat_col_(X);\n }\n void concat_col_(vector<vector<T>> &Y) {\n BitMatrix X(Y);\n concat_col_(X);\n }\n void concat_col_(BitMatrix &Y) {\n assert((int)Y.row == row);\n rep(i,row) {\n rep(j,Y.col) mat[i][j+col] = (Y.mat[i][j]);\n }\n col += Y.col;\n }\n\n void concat_row_(vector<T> &Y) {\n BitMatrix X(Y,true);\n concat_row_(X);\n }\n void concat_row_(vector<vector<T>> &Y) {\n BitMatrix X(Y);\n concat_row_(X);\n }\n void concat_row_(BitMatrix &Y) {\n assert((int)Y.col == col);\n row += Y.row;\n rep(i,Y.row) mat.push_back(Y.mat[i]);\n }\n\n void print_() {\n rep(i,row){\n rep(j,col) cout << mat[i][j]; cout << endl;\n }\n }\n\n public:\n\n inline BitMatrix &operator&=(const BitMatrix Y) {\n rep(i,row) mat[i] &= Y.mat[i];\n return *this ;\n }\n\n inline BitMatrix &operator|=(const BitMatrix Y) {\n rep(i,row) mat[i] |= Y.mat[i];\n return *this ;\n }\n \n inline BitMatrix &operator^=(const BitMatrix Y) {\n rep(i,row) mat[i] ^= Y.mat[i];\n return *this ;\n }\n\n inline BitMatrix operator&(const BitMatrix Y) const { return BitMatrix(*this) &= Y; }\n\n inline BitMatrix operator|(const BitMatrix Y) const { return BitMatrix(*this) |= Y; }\n\n inline BitMatrix operator^(const BitMatrix Y) const { return BitMatrix(*this) += Y; }\n\n inline bool operator==(const BitMatrix Y) const { return mat == Y.mat; }\n\n inline bool operator!=(const BitMatrix Y) const { return mat != Y.mat; }\n\n inline bitset<MAX_COL>&operator[] (int i) {return mat[i]; }\n\n BitMatrix(int n): row(n), col(0) { mat.resize(row); }\n BitMatrix(vector<T> A, bool row_matrix = false) { init_(A, row_matrix); }\n BitMatrix(vector<vector<T>> A){ init_(A); }\n void init(vector<T> A, bool row_matrix = false) { init_(A, row_matrix); }\n void init(vector<vector<T>> A) { init_(A); }\n size_t row_size() { return row; }\n size_t col_size() { return col; }\n void transpose() { transpose_(); }\n void flip() { flip_(); }\n void concat_col(vector<vector<T>> &Y) { concat_col_(Y); }\n void concat_col(vector<T> &Y) { concat_col_(Y); }\n void concat_col(BitMatrix &Y) { concat_col_(Y); }\n void concat_row(vector<vector<T>> &Y) { concat_row_(Y); }\n void concat_row(vector<T> &Y) { concat_row_(Y); }\n void concat_row(BitMatrix &Y) { concat_row_(Y); }\n void print() { print_(); }\n};\n\nconst int MAX_COL = 2510;\nusing Matrix = BitMatrix<MAX_COL, bool>;\n\ntemplate<typename T=bool> struct GaussJordan{\n private:\n int rank;\n vector<bool> solution;\n \n int sweep_out_(Matrix &A , bool is_extended = false){\n rank = 0 ;\n for(int col = 0 ; col < A.col_size() ; col++){\n if(is_extended && col == A.col_size() - 1) break ;\n\n int pivot = -1 ;\n for(int row = rank ; row < A.col_size() ; row++){\n if(A[row][col]){\n pivot = row ;\n break ;\n }\n }\n\n if(pivot == -1) continue ;\n swap(A[pivot] , A[rank]) ;\n for(int row = 0 ; row < A.row_size() ; row++){\n if(row != rank && A[row][col]) A[row] ^= A[rank] ;\n }\n rank++ ;\n }\n return rank ;\n }\n \n vector<bool> solve_simultaneous_equation_(vector<vector<T>> &A , vector<T> &b){\n return solve_simultaneous_equation_(Matrix(A), Matrix(b));\n }\n vector<bool> solve_simultaneous_equation_(Matrix &A , vector<T> &b){\n return solve_simultaneous_equation_(A, Matrix(b));\n }\n vector<bool> solve_simultaneous_equation_(vector<vector<T>> &A , Matrix &b){\n return solve_simultaneous_equation_(Matrix(A), b);\n }\n vector<bool> solve_simultaneous_equation_(Matrix &A , Matrix &b){\n return solve_simultaneous_equation_(A.concat_col(b));\n }\n vector<bool> solve_simultaneous_equation_(Matrix &M){\n\n int n = M.row_size() , m = M.col_size();\n\n rank = sweep_out_(M,true);\n\n for(int row = rank ; row < n ; row++) if(M[row][n]) return {};\n\n vector<bool> res;\n res.resize(rank);\n for(int i = 0 ; i < rank; i++) res[i] = M[i][m];\n\n return solution = res;\n }\n\n public:\n GaussJordan(){}\n int sweep_out(Matrix &A) { return sweep_out_(A, false); }\n int sweep_out(Matrix &A, bool is_extended) { return sweep_out_(A, is_extended); }\n vector<bool> solve_simultaneous_equation(Matrix &A , Matrix &b) { return solve_simultaneous_equation_(A, b); }\n vector<bool> solve_simultaneous_equation(Matrix &A , vector<T> &b) { return solve_simultaneous_equation_(A, b); }\n vector<bool> solve_simultaneous_equation(vector<vector<T>> &A , vector<T> &b) { return solve_simultaneous_equation_(A, b); }\n vector<bool> solve_simultaneous_equation(vector<vector<T>> &A , Matrix &b) { return solve_simultaneous_equation_(A, b); }\n vector<bool> solve_simultaneous_equation(Matrix A) { return solve_simultaneous_equation_(A); }\n int get_rank() { return rank; }\n vector<T> get_solution() { return solution; }\n};\n\nint n, m, d;\nvector<vector<char>> S;\nvector<vector<bool>> A;\n\nbool solve(){\n S.clear();\n A.clear();\n cin >> m >> n >> d;\n if(m == 0 && n == 0 && d == 0) return false;\n S.resize(n, vector<char>(m));\n A.resize(n, vector<bool>(m));\n rep(i,n) rep(j,m) cin >> S[i][j], A[i][j] = S[i][j] - '0';\n vector<vector<bool>> M(n*m,vector<bool>(n*m+1,0));\n rep(i,n) rep(j,m){\n M[i*m+j][i*m+j] = 1;\n rep(x,n) rep(y,m){\n int dist = abs(i - x) + abs(j - y);\n if(dist == d) {\n M[i*m+j][x*m+y] = 1;\n }\n }\n if(!A[i][j]) M[i*m+j][n*m] = 1;\n }\n GaussJordan gj;\n auto res = gj.solve_simultaneous_equation(Matrix(M));\n if(res.empty()) cout << 0 << endl;\n else cout << 1 << endl;\n return 1;\n}\n\nint main(){\n fast_io\n while(1){\n if(!solve()) {\n return 0;\n }\n }\n}", "accuracy": 1, "time_ms": 60, "memory_kb": 3632, "score_of_the_acc": -0.0223, "final_rank": 3 } ]
aoj_1310_cpp
Problem F: Find the Multiples You are given a sequence a 0 a 1 ... a N -1 digits and a prime number Q . For each i ≤ j with a i ≠ 0, the subsequence a i a i +1 ... a j can be read as a decimal representation of a positive integer. Subsequences with leading zeros are not considered. Your task is to count the number of pairs ( i , j ) such that the corresponding subsequence is a multiple of Q . Input The input consists of at most 50 datasets. Each dataset is represented by a line containing four integers N , S , W , and Q , separated by spaces, where 1 ≤ N ≤ 10 5 , 1 ≤ S ≤ 10 9 , 1 ≤ W ≤ 10 9 , and Q is a prime number less than 10 8 . The sequence a 0 ... a N -1 of length N is generated by the following code, in which ai is written as a[i] . int g = S; for(int i=0; i<N; i++) { a[i] = (g/7) % 10; if( g%2 == 0 ) { g = (g/2); } else { g = (g/2) ^ W; } } Note: the operators / , % , and ^ are the integer division, the modulo, and the bitwise exclusiveor, respectively. The above code is meant to be a random number generator. The intended solution does not rely on the way how the sequence is generated. The end of the input is indicated by a line containing four zeros separated by spaces. Output For each dataset, output the answer in a line. You may assume that the answer is less than 2 30 . Sample Input 3 32 64 7 4 35 89 5 5 555 442 3 5 777 465 11 100000 666 701622763 65537 0 0 0 0 Output for the Sample Input 2 4 6 3 68530 In the first dataset, the sequence is 421. We can find two multiples of Q = 7, namely, 42 and 21. In the second dataset, the sequence is 5052, from which we can find 5, 50, 505, and 5 being the multiples of Q = 5. Notice that we don't count 0 or 05 since they are not a valid representation of positive integers. Also notice that we count 5 twice, because it occurs twice in different positions. In the third and fourth datasets, the sequences are 95073 and 12221, respectively.
[ { "submission_id": "aoj_1310_10851362", "code_snippet": "#include <cstdio>\n#include <map>\n#include <iostream>\n#include <cstring>\nusing namespace std;\nusing std::map;\nint a[100010];\nmap<long long,int> mp;\nlong long pow_mod(long long a,long long b,long long p)\n{\n\tlong long ans = 1;\n\twhile(b) {\n\t\tif(b&1) ans = ans * a % p;\n\t\tb >>= 1; a = a * a % p;\n\t}\n\treturn ans;\n}\n\nint main()\n{\n\tlong long N,S,W,Q;\n\twhile(std::cin>>N>>S>>W>>Q)\n\t{\n\t\tif(!N) break;\n\t\tmp.clear();\n\t\tint g = S;\n\t\ta[0] = 0;\n\t\tfor(int i = 1; i <= N; i++)\n\t\t{\n\t\t\ta[i] = (g/7) % 10;\n\t\t\tif(g%2==0) g /= 2;\n\t\t\telse g = (g/2) ^ W;\n\t\t}\n\t\tif(Q == 5 || Q == 2)\n\t\t{\n\t\t\tint ans = 0;\n\t\t\tint cnt = 0;\n\t\t\tfor(int i = 1;i <= N;i++)\n\t\t\t{\n\t\t\t\tif(a[i] != 0)cnt++;\n\t\t\t\tif(a[i] % Q == 0)ans += cnt;\n\t\t\t}\n\t\t\tprintf(\"%d\\n\",ans);\n\t\t\tcontinue;\n\t\t}\n\t\tlong long ans = 0;\n\t\tlong long sum = 0;\n\t\tlong long S = 1;\n\t\tfor(int i = N; i >= 1; i--)\n\t\t{\n\t\t\tsum = (S*a[i] + sum) % Q;\n\t if(a[i] > 0 && !sum) ans++;\n\t\t\tif(a[i] > 0) ans += mp[sum] ;\n\t\t\tmp[sum]++;\n\t\t\tS *= 10;\n\t\t\tS %= Q;\n\t\t}\n\t\tstd::cout<<ans<<std::endl;\n\t}\n\treturn 0;\n}", "accuracy": 1, "time_ms": 390, "memory_kb": 9440, "score_of_the_acc": -0.9644, "final_rank": 14 }, { "submission_id": "aoj_1310_9730559", "code_snippet": "#include <bits/stdc++.h>\n\nusing namespace std;\n\n#define rep(i, a, b) for (int i = (int)(a); i < (int)(b); i++)\n#define rrep(i, a, b) for (int i = (int)(b)-1; i >= (int)(a); i--)\n#define ALL(v) (v).begin(), (v).end()\n#define UNIQUE(v) sort(ALL(v)), (v).erase(unique(ALL(v)), (v).end())\n#define SZ(v) (int)v.size()\n#define MIN(v) *min_element(ALL(v))\n#define MAX(v) *max_element(ALL(v))\n#define LB(v, x) int(lower_bound(ALL(v), (x)) - (v).begin())\n#define UB(v, x) int(upper_bound(ALL(v), (x)) - (v).begin())\n\nusing uint = unsigned int;\nusing ll = long long int;\nusing ull = unsigned long long;\nusing i128 = __int128_t;\nusing u128 = __uint128_t;\nconst int inf = 0x3fffffff;\nconst ll INF = 0x1fffffffffffffff;\n\ntemplate <typename T> inline bool chmax(T &a, T b) {\n if (a < b) {\n a = b;\n return 1;\n }\n return 0;\n}\ntemplate <typename T> inline bool chmin(T &a, T b) {\n if (a > b) {\n a = b;\n return 1;\n }\n return 0;\n}\ntemplate <typename T, typename U> T ceil(T x, U y) {\n assert(y != 0);\n if (y < 0)\n x = -x, y = -y;\n return (x > 0 ? (x + y - 1) / y : x / y);\n}\ntemplate <typename T, typename U> T floor(T x, U y) {\n assert(y != 0);\n if (y < 0)\n x = -x, y = -y;\n return (x > 0 ? x / y : (x - y + 1) / y);\n}\ntemplate <typename T> int popcnt(T x) {\n return __builtin_popcountll(x);\n}\ntemplate <typename T> int topbit(T x) {\n return (x == 0 ? -1 : 63 - __builtin_clzll(x));\n}\ntemplate <typename T> int lowbit(T x) {\n return (x == 0 ? -1 : __builtin_ctzll(x));\n}\n\ntemplate <class T, class U>\nostream &operator<<(ostream &os, const pair<T, U> &p) {\n os << \"P(\" << p.first << \", \" << p.second << \")\";\n return os;\n}\ntemplate <typename T> ostream &operator<<(ostream &os, const vector<T> &vec) {\n os << \"{\";\n for (int i = 0; i < vec.size(); i++) {\n os << vec[i] << (i + 1 == vec.size() ? \"\" : \", \");\n }\n os << \"}\";\n return os;\n}\ntemplate <typename T, typename U>\nostream &operator<<(ostream &os, const map<T, U> &map_var) {\n os << \"{\";\n for (auto itr = map_var.begin(); itr != map_var.end(); itr++) {\n os << \"(\" << itr->first << \", \" << itr->second << \")\";\n itr++;\n if (itr != map_var.end())\n os << \", \";\n itr--;\n }\n os << \"}\";\n return os;\n}\ntemplate <typename T> ostream &operator<<(ostream &os, const set<T> &set_var) {\n os << \"{\";\n for (auto itr = set_var.begin(); itr != set_var.end(); itr++) {\n os << *itr;\n ++itr;\n if (itr != set_var.end())\n os << \", \";\n itr--;\n }\n os << \"}\";\n return os;\n}\n#ifdef LOCAL\n#define show(...) _show(0, #__VA_ARGS__, __VA_ARGS__)\n#else\n#define show(...) true\n#endif\ntemplate <typename T> void _show(int i, T name) {\n cerr << '\\n';\n}\ntemplate <typename T1, typename T2, typename... T3>\nvoid _show(int i, const T1 &a, const T2 &b, const T3 &...c) {\n for (; a[i] != ',' && a[i] != '\\0'; i++)\n cerr << a[i];\n cerr << \":\" << b << \" \";\n _show(i + 1, a, c...);\n}\n\n/**\n * @brief template\n */\n\nint main() {\nwhile(1) {\n ll N, S, W, Q;\n cin >> N >> S >> W >> Q;\n if (N == 0) return 0;\n ll G = S;\n vector<ll> A(N);\n rep(i,0,N) {\n A[i] = (G/7) % 10;\n if (G % 2 == 0) G /= 2;\n else G = (G/2) ^ W;\n }\n reverse(ALL(A));\n if (Q == 2) {\n ll ANS = 0;\n ll COUNT = 0;\n rep(i,0,N) {\n if (A[i] % 2 == 0) COUNT++;\n if (A[i] != 0) ANS += COUNT;\n }\n cout << ANS << endl;\n continue;\n }\n if (Q == 5) {\n ll ANS = 0;\n ll COUNT = 0;\n rep(i,0,N) {\n if (A[i] % 5 == 0) COUNT++;\n if (A[i] != 0) ANS += COUNT;\n }\n cout << ANS << endl;\n continue;\n }\n ll D = 1, Cur = 0;\n ll ANS = 0;\n map<ll,int> mp;\n mp[0]++;\n rep(i,0,N) {\n Cur += (D * A[i]);\n D *= 10;\n Cur %= Q;\n D %= Q;\n if (A[i] != 0) ANS += mp[Cur];\n mp[Cur]++;\n }\n cout << ANS << endl;\n}\n}", "accuracy": 1, "time_ms": 380, "memory_kb": 9848, "score_of_the_acc": -0.9499, "final_rank": 12 }, { "submission_id": "aoj_1310_8998678", "code_snippet": "#line 2 \"cp-library/src/cp-template.hpp\"\n#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\nusing ld = long double;\nusing uint = unsigned int;\nusing ull = unsigned long long;\nusing i32 = int;\nusing u32 = unsigned int;\nusing i64 = long long;\nusing u64 = unsigned long long;\nusing i128 = __int128_t;\ntemplate < class T > bool chmin(T& a, T b) { if(a > b) { a = b; return true; } return false; }\ntemplate < class T > bool chmax(T& a, T b) { if(a < b) { a = b; return true; } return false; }\ntemplate < class T, class U > T ceil (T x, U y) { return (x > 0 ? (x + y - 1) / y : x / y); }\ntemplate < class T, class U > T floor(T x, U y) { return (x > 0 ? x / y : (x - y + 1) / y); }\nint popcnt(i32 x) { return __builtin_popcount(x); }\nint popcnt(u32 x) { return __builtin_popcount(x); }\nint popcnt(i64 x) { return __builtin_popcountll(x); }\nint popcnt(u64 x) { return __builtin_popcountll(x); }\n\n#line 2 \"cp-library/src/utility/rep_itr.hpp\"\ntemplate < class T > struct itr_rep {\n T i, d;\n constexpr itr_rep(const T i) noexcept : i(i), d(1) {}\n constexpr itr_rep(const T i, const T d) noexcept : i(i), d(d) {}\n void operator++() noexcept { i += d; }\n constexpr int operator*() const noexcept { return i; }\n constexpr bool operator!=(const itr_rep x) const noexcept { return d > 0 ? i < x.i : i > x.i; }\n};\n\ntemplate < class T > struct rep {\n const itr_rep< T > s, t;\n constexpr rep(const T t) noexcept : s(0), t(t) {}\n constexpr rep(const T s, const T t) noexcept : s(s), t(t) {}\n constexpr rep(const T s, const T t, const T d) noexcept : s(s, d), t(t, d) {}\n constexpr auto begin() const noexcept { return s; }\n constexpr auto end () const noexcept { return t; }\n};\n\ntemplate < class T > struct revrep {\n const itr_rep < T > s, t;\n constexpr revrep(const T t) noexcept : s(t - 1, -1), t(-1, -1) {}\n constexpr revrep(const T s, const T t) noexcept : s(t - 1, -1), t(s - 1, -1) {}\n constexpr revrep(const T s, const T t, const T d) noexcept : s(t - 1, -d), t(s - 1, -d) {}\n constexpr auto begin() const noexcept { return s; }\n constexpr auto end () const noexcept { return t; }\n};\n#line 3 \"cp-library/src/utility/io.hpp\"\n\n/* 128bit integer */\nistream& operator>>(istream& is, i128& x) {\n std::string s; is >> s;\n int pm = (s[0] == '-');\n x = 0;\n for(int i : rep(pm, int(s.size()))) x = x * 10 + (s[i] - '0');\n if(pm) x *= -1;\n return is;\n}\nostream& operator<<(ostream& os, const i128& x) {\n if(x == 0) return os << '0';\n i128 y = x;\n if(y < 0) { os << '-'; y *= -1; }\n std::vector<int> ny;\n while(y > 0) { ny.push_back(y % 10); y /= 10; }\n for(int i : revrep(ny.size())) os << ny[i];\n return os;\n}\n\ntemplate < class S, class T > istream& operator>>(istream& is, std::pair< S, T >& x) { is >> x.first >> x.second; return is; }\ntemplate < class S, class T > ostream& operator<<(ostream& os, const std::pair< S, T >& x) { os << x.first << \" \" << x.second; return os; }\n\nnamespace scanner {\n struct sca {\n template < class T > operator T() {\n T s; std::cin >> s; return s;\n }\n };\n struct vec {\n int n;\n vec(int n) : n(n) {}\n template < class T > operator std::vector< T >() {\n std::vector< T > v(n);\n for(T& x : v) std::cin >> x;\n return v;\n }\n };\n struct mat {\n int h, w;\n mat(int h, int w) : h(h), w(w) {}\n template < class T > operator std::vector< std::vector< T > >() {\n std::vector m(h, std::vector< T >(w));\n for(std::vector< T >& v : m) for(T& x : v) std::cin >> x;\n return m;\n }\n };\n struct speedup {\n speedup() {\n std::cin.tie(0);\n std::ios::sync_with_stdio(0);\n }\n } speedup_instance;\n}\nscanner::sca in() { return scanner::sca(); }\nscanner::vec in(int n) { return scanner::vec(n); }\nscanner::mat in(int h, int w) { return scanner::mat(h, w); }\n\nnamespace printer {\n void precision(int d) { std::cout << std::fixed << std::setprecision(d); }\n void flush() { std::cout.flush(); }\n}\n\ntemplate < class T >\nostream& operator<<(ostream& os, const std::vector< T > a) {\n int n = a.size();\n for(int i : rep(n)) { os << a[i]; if(i != n - 1) os << ' '; }\n return os;\n}\n\nint print() { std::cout << '\\n'; return 0; }\ntemplate < class head, class... tail > int print(head&& h, tail&&... t) {\n std::cout << h; if(sizeof...(tail)) std::cout << ' ';\n return print(std::forward<tail>(t)...);\n}\ntemplate < class T > int print_n(const std::vector< T > a) {\n int n = a.size();\n for(int i : rep(n)) std::cout << a[i] << \"\\n\";\n return 0;\n}\n\n\n#line 2 \"cp-library/src/utility/key_val.hpp\"\n\ntemplate < class K, class V >\nstruct key_val {\n K key; V val;\n key_val() {}\n key_val(K key, V val) : key(key), val(val) {}\n template < std::size_t Index >\n std::tuple_element_t< Index, key_val >& get() {\n if constexpr (Index == 0) return key;\n if constexpr (Index == 1) return val;\n }\n};\n\nnamespace std {\n\ntemplate < class K, class V > struct tuple_size < key_val< K, V > > : integral_constant< size_t, 2 > {};\ntemplate < class K, class V > struct tuple_element < 0, key_val< K, V > > { using type = K; };\ntemplate < class K, class V > struct tuple_element < 1, key_val< K, V > > { using type = V; };\n\n}\n#line 2 \"cp-library/src/utility/vec_op.hpp\"\ntemplate < class T > key_val< int, T > max_of(const vector< T >& a) {\n int i = std::max_element(a.begin(), a.end()) - a.begin();\n return {i, a[i]};\n}\ntemplate < class T > key_val< int, T > min_of(const vector< T >& a) {\n int i = std::min_element(a.begin(), a.end()) - a.begin();\n return {i, a[i]};\n}\ntemplate < class S, class T > S sum_of(const vector< T >& a) {\n S sum = 0;\n for(const T x : a) sum += x;\n return sum;\n}\ntemplate < class S, class T > vector< S > freq_of(const vector< T >& a, T L, T R) {\n vector< S > res(R - L, S(0));\n for(const T x : a) res[x - L] += 1;\n return res;\n}\ntemplate < class S, class T > struct prefix_sum {\n vector< S > s;\n prefix_sum(const vector< T >& a) : s(a) {\n s.insert(s.begin(), S(0));\n for(int i : rep(a.size())) s[i + 1] += s[i];\n }\n // [L, R)\n S sum(int L, int R) { return s[R] - s[L]; }\n};\n#line 3 \"cp-library/src/utility/heap.hpp\"\n\ntemplate < class T > using heap_min = std::priority_queue< T, std::vector< T >, std::greater< T > >;\ntemplate < class T > using heap_max = std::priority_queue< T, std::vector< T >, std::less< T > >;\n\n#line 27 \"cp-library/src/cp-template.hpp\"\n\n#line 1 \"cp-library/src/algorithm/bin_search.hpp\"\ntemplate < class T, class F >\nT bin_search(T ok, T ng, F f) {\n while(abs(ng - ok) > 1) {\n T mid = (ok + ng) / 2;\n (f(mid) ? ok : ng) = mid;\n }\n return ok;\n}\n\ntemplate < class T, class F >\nT bin_search_real(T ok, T ng, F f, int step = 80) {\n while(step--) {\n T mid = (ok + ng) / 2;\n (f(mid) ? ok : ng) = mid;\n }\n return ok;\n}\n#line 2 \"cp-library/src/algorithm/argsort.hpp\"\n\ntemplate < class T > std::vector< int > argsort(const std::vector< T > &a) {\n std::vector< int > ids((int)a.size());\n std::iota(ids.begin(), ids.end(), 0);\n std::sort(ids.begin(), ids.end(), [&](int i, int j) {\n return a[i] < a[j] || (a[i] == a[j] && i < j);\n });\n return ids;\n}\n#line 1 \"macro.hpp\"\nnamespace macro {\n\nusing size_type = int;\ntemplate < class container > void sort(container& a) { std::sort(std:: begin(a), std:: end(a)); }\ntemplate < class container > void rsort(container& a) { std::sort(std::rbegin(a), std::rend(a)); }\ntemplate < class container > void reverse(container& a) { std::reverse(std::begin(a), std::end(a)); }\ntemplate < class container > void unique(container& a) {\n std::sort(std::begin(a), std::end(a));\n a.erase(std::unique(std::begin(a), std::end(a)), std::end(a));\n}\ntemplate < class container > container sorted(const container& a) { container b = a; sort(b); return std::move(b); }\ntemplate < class container > container rsorted(const container& a) { container b = a; rsort(b); return std::move(b); }\ntemplate < class container, class compare > void sort(container& a, const compare& cmp) { std::sort(std::begin(a), std::end(a), cmp); }\ntemplate < class container, class compare > container sorted(const container& a, const compare& cmp) { container b = a; sort(b, cmp); return std::move(b); }\ntemplate < class container, class value > size_type lower_bound(const container& a, const value& x) { return std::lower_bound(std::begin(a), std::end(a), x) - std::begin(a); }\ntemplate < class container, class value > size_type upper_bound(const container& a, const value& x) { return std::upper_bound(std::begin(a), std::end(a), x) - std::begin(a); }\n\nconst std::vector<std::pair<size_type, size_type>> dir4 = { {+1, 0}, {-1, 0}, { 0, +1}, { 0, -1} };\nconst std::vector<std::pair<size_type, size_type>> dir8 = { {-1, -1}, {-1, 0}, {-1, +1}, { 0, -1}, { 0, +1}, {+1, -1}, {+1, 0}, {+1, +1} };\n\n#ifdef _DEBUG\n#define debug(x) std::cout << \"[\" << __LINE__ << \"] \" << #x << \": \" << x << std::endl\n#else\n#define debug(x)\n#endif\n\ntemplate < class container > void concat(container& a, const container& b) {\n a.insert(std::end(a), std::begin(b), std::end(b));\n}\nstd::vector<size_type> iota(const size_type n) {\n std::vector<size_type> I(n);\n std::iota(std::begin(I), std::end(I), 0);\n return I;\n}\ntemplate < class container > std::vector<size_type> sort_idx(const container& a) {\n const size_type n = a.size();\n std::vector<size_type> I = iota(n);\n std::sort(std::begin(I), std::end(I), [&](size_type i, size_type j) { return a[i] < a[j] or (a[i] == a[j] and i < j); });\n return I;\n}\ntemplate < class container, class compare > std::vector<size_type> sort_idx(const container& a, const compare& cmp) {\n const size_type n = a.size();\n std::vector<size_type> I = iota(n);\n std::sort(std::begin(I), std::end(I), [&](size_type i, size_type j) { return cmp(a[i], a[j]) or (a[i] == a[j] and i < j); });\n return std::move(I);\n}\n\nstruct grid {\n using size_type = int;\n size_type H, W;\n grid(const size_type H, const size_type W) : H(H), W(W) {}\n bool contains(const size_type i, const size_type j) {\n return 0 <= i and i < H and 0 <= j and j < W;\n }\n};\n\nusing f64 = long double;\n\n} // namespace macro\n\nusing namespace macro;\n#line 3 \"A.cpp\"\n\nint solve2(int N, vector<i64> a, int Q) {\n int ans = 0, zero = 0;\n for(int i : rep(N)) {\n if(a[i] % Q == 0) ans += i + 1 - zero;\n if(a[i] == 0) zero++;\n }\n return ans - zero;\n}\n\nint solve(int N, int S, int W, int Q) {\n vector<i64> a(N);\n {\n int g = S;\n for(int i = 0; i < N; i++) {\n a[i] = (g / 7) % 10;\n if(g % 2 == 0) { g = (g / 2); }\n else {g = (g / 2) ^ W; }\n }\n }\n\n if(Q == 2 or Q == 5) return solve2(N, a, Q);\n\n i64 pow10 = 1;\n vector<i64> b(N);\n for(int i : revrep(N)) {\n b[i] = (a[i] * pow10) % Q;\n pow10 = (pow10 * 10) % Q;\n }\n\n vector<i64> s(N + 1, 0);\n for(int i : revrep(N)) s[i] = (s[i + 1] + b[i]) % Q;\n int ans = 0;\n map<i64, int> mp;\n mp[0] = 1;\n for(int i : revrep(N)) {\n if(a[i] != 0) ans += mp[s[i]];\n mp[s[i]]++;\n }\n return ans;\n}\n\nint main() {\n while(true) {\n int N = in(), S = in(), W = in(), Q = in();\n if(make_tuple(N, S, W, Q) == make_tuple(0, 0, 0, 0)) return 0;\n print(solve(N, S, W, Q));\n }\n}", "accuracy": 1, "time_ms": 410, "memory_kb": 10956, "score_of_the_acc": -1.625, "final_rank": 20 }, { "submission_id": "aoj_1310_8505599", "code_snippet": "#include<bits/stdc++.h>\n\nusing namespace std;\n\n#define rep(i, n) for(int i = 0; i < (n); i++)\n#define per(i, n) for(int i = (n)-1; i >= 0; i--)\n#define rep2(i, l, r) for(int i = (l); i < (r); i++)\n#define per2(i, l, r) for(int i = (r)-1; i >= (l); i--)\n#define each(e, x) for(auto &e : x)\n#define sz(x) (int)x.size()\n#define all(x) begin(x), end(x)\n#define rall(x) rbegin(x), rend(x)\n#define eb emplace_back\n#define MM << ' ' <<\n#define TT template<typename T>\nusing ll = long long;\nusing pii = pair<int, int>;\nusing pll = pair<ll, ll>;\n\nTT bool chmin(T &x, T y) { return x > y ? (x = y, true) : false; }\n\nTT bool chmax(T &x, T y) { return x < y ? (x = y, true) : false; }\n\nTT using minheap = priority_queue<T, vector<T>, greater<T>>;\nTT using maxheap = priority_queue<T>;\n\nTT void print(vector<T> v, T x = 0) {\n int n = sz(v);\n rep(i, n) cout << v[i] + x << (i == n - 1 ? '\\n' : ' ');\n if (n == 0) cout << '\\n';\n}\n\nTT void printn(vector<T> v, T x = 0) {\n int n = sz(v);\n rep(i, n) cout << v[i] + x << '\\n';\n}\n\nTT void rearrange(vector<T> &v) {\n sort(all(v));\n v.erase(unique(all(v)), end(v));\n}\n\nTT int lb(vector<T> &v, T x) { return lower_bound(all(v), x) - begin(v); }\n\nTT int ub(vector<T> &v, T x) { return upper_bound(all(v), x) - begin(v); }\n\nconst int inf = (1 << 30) - 1;\nconst ll INF = (1LL << 60) - 1;\n\nstruct io_setup {\n io_setup() {\n ios_base::sync_with_stdio(false);\n cin.tie(NULL);\n cout << fixed << setprecision(15);\n }\n} io_setup;\n\nint main() {\n while (1) {\n int n, s, w, p;\n cin >> n >> s >> w >> p;\n if (!n)\n break;\n vector<int> a(n);\n int g = s;\n rep(i, n) {\n a[i] = g / 7 % 10;\n g = (g % 2 == 0 ? g / 2 : (g / 2) ^ w);\n }\n ll ans = 0;\n if (p == 2 || p == 5) {\n ll cnt = 0;\n rep(i, n) {\n if (a[i] % p == 0) {\n if (a[i]) ans++;\n ans += cnt;\n }\n if (a[i]) cnt++;\n }\n }\n else {\n map<ll, ll> m;\n ll val = 0, t = 1;\n m[0]++;\n per(i, n) {\n val += t * a[i];\n val %= p;\n if (a[i]) ans += m[val];\n m[val]++;\n t *= 10, t %= p;\n }\n }\n cout << ans << '\\n';\n }\n}", "accuracy": 1, "time_ms": 400, "memory_kb": 9400, "score_of_the_acc": -1.0785, "final_rank": 16 }, { "submission_id": "aoj_1310_6761294", "code_snippet": "/**\n * author: otera\n**/\n#include<bits/stdc++.h>\nusing namespace std;\n\n// #define int long long\n\nusing ll = long long;\nusing ld = long double;\nusing ull = unsigned long long;\nusing int128_t = __int128_t;\n#define repa(i, n) for(int i = 0; i < n; ++ i)\n#define repb(i, a, b) for(int i = a; i < b; ++ i)\n#define repc(i, a, b, c) for(int i = a; i < b; i += c)\n#define overload4(a, b, c, d, e, ...) e\n#define overload3(a, b, c, d, ...) d\n#define rep(...) overload4(__VA_ARGS__, repc, repb, repa)(__VA_ARGS__)\n#define rep1a(i, n) for(int i = 0; i <= n; ++ i)\n#define rep1b(i, a, b) for(int i = a; i <= b; ++ i)\n#define rep1c(i, a, b, c) for(int i = a; i <= b; i += c)\n#define rep1(...) overload4(__VA_ARGS__, rep1c, rep1b, rep1a)(__VA_ARGS__)\n#define rev_repa(i, n) for(int i=n-1;i>=0;i--)\n#define rev_repb(i, a, b) assert(a > b);for(int i=a;i>b;i--)\n#define rev_rep(...) overload3(__VA_ARGS__, rev_repb, rev_repa)(__VA_ARGS__)\n#define rev_rep1a(i, n) for(int i=n;i>=1;i--)\n#define rev_rep1b(i, a, b) assert(a >= b);for(int i=a;i>=b;i--)\n#define rev_rep1(...) overload3(__VA_ARGS__, rev_rep1b, rev_rep1a)(__VA_ARGS__)\ntypedef pair<int, int> P;\ntypedef pair<ll, ll> LP;\n#define pb push_back\n#define pf push_front\n#define ppb pop_back\n#define ppf pop_front\n#define eb emplace_back\n#define fr first\n#define sc second\n#define all(c) c.begin(),c.end()\n#define rall(c) c.rbegin(), c.rend()\n#define lb(c, x) distance((c).begin(), lower_bound(all(c), (x)))\n#define ub(c, x) distance((c).begin(), upper_bound(all(c), (x)))\n#define Sort(a) sort(all(a))\n#define Rev(a) reverse(all(a))\n#define Uniq(a) sort(all(a));a.erase(unique(all(a)),end(a))\n#define si(c) (int)(c).size()\ninline ll popcnt(ull a){ return __builtin_popcountll(a); }\n#define kth_bit(x, k) ((x>>k)&1)\n#define unless(A) if(!(A))\nll intpow(ll a, ll b){ ll ans = 1; while(b){ if(b & 1) ans *= a; a *= a; b /= 2; } return ans; }\nll intpow(ll a, ll b, ll m) {ll ans = 1; while(b){ if(b & 1) (ans *= a) %= m; (a *= a) %= m; b /= 2; } return ans; }\ntemplate<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }\ntemplate<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }\n#define INT(...) int __VA_ARGS__;in(__VA_ARGS__)\n#define LL(...) ll __VA_ARGS__;in(__VA_ARGS__)\n#define ULL(...) ull __VA_ARGS__;in(__VA_ARGS__)\n#define STR(...) string __VA_ARGS__;in(__VA_ARGS__)\n#define CHR(...) char __VA_ARGS__;in(__VA_ARGS__)\n#define DBL(...) double __VA_ARGS__;in(__VA_ARGS__)\n#define LD(...) ld __VA_ARGS__;in(__VA_ARGS__)\n#define vec(type,name,...) vector<type>name(__VA_ARGS__)\n#define VEC(type,name,size) vector<type>name(size);in(name)\n#define vv(type,name,h,...) vector<vector<type>>name(h,vector<type>(__VA_ARGS__))\n#define VV(type,name,h,w) vector<vector<type>>name(h,vector<type>(w));in(name)\n#define vvv(type,name,h,w,...) vector<vector<vector<type>>>name(h,vector<vector<type>>(w,vector<type>(__VA_ARGS__)))\ntemplate <class T> using vc = vector<T>;\ntemplate <class T> using vvc = vector<vc<T>>;\ntemplate <class T> using vvvc = vector<vvc<T>>;\ntemplate <class T> using vvvvc = vector<vvvc<T>>;\ntemplate <class T> using pq = priority_queue<T>;\ntemplate <class T> using pqg = priority_queue<T, vector<T>, greater<T>>;\ntemplate <class T, class U> using umap = unordered_map<T, U>;\ntemplate<class T> void scan(T& a){ cin >> a; }\ntemplate<class T> void scan(vector<T>& a){ for(auto&& i : a) scan(i); }\nvoid in(){}\ntemplate <class Head, class... Tail> void in(Head& head, Tail&... tail){ scan(head); in(tail...); }\nvoid print(){ cout << ' '; }\ntemplate<class T> void print(const T& a){ cout << a; }\ntemplate<class T> void print(const vector<T>& a){ if(a.empty()) return; print(a[0]); for(auto i = a.begin(); ++i != a.end(); ){ cout << ' '; print(*i); } }\nint out(){ cout << '\\n'; return 0; }\ntemplate<class T> int out(const T& t){ print(t); cout << '\\n'; return 0; }\ntemplate<class Head, class... Tail> int out(const Head& head, const Tail&... tail){ print(head); cout << ' '; out(tail...); return 0; }\n#define CHOOSE(a) CHOOSE2 a\n#define CHOOSE2(a0,a1,a2,a3,a4,x,...) x\n#define debug_1(x1) cout<<#x1<<\": \"<<x1<<endl\n#define debug_2(x1,x2) cout<<#x1<<\": \"<<x1<<\", \"#x2<<\": \"<<x2<<endl\n#define debug_3(x1,x2,x3) cout<<#x1<<\": \"<<x1<<\", \"#x2<<\": \"<<x2<<\", \"#x3<<\": \"<<x3<<endl\n#define debug_4(x1,x2,x3,x4) cout<<#x1<<\": \"<<x1<<\", \"#x2<<\": \"<<x2<<\", \"#x3<<\": \"<<x3<<\", \"#x4<<\": \"<<x4<<endl\n#define debug_5(x1,x2,x3,x4,x5) cout<<#x1<<\": \"<<x1<<\", \"#x2<<\": \"<<x2<<\", \"#x3<<\": \"<<x3<<\", \"#x4<<\": \"<<x4<<\", \"#x5<<\": \"<<x5<<endl\n#ifdef DEBUG\n#define debug(...) CHOOSE((__VA_ARGS__,debug_5,debug_4,debug_3,debug_2,debug_1,~))(__VA_ARGS__)\n#define dump(...) { print(#__VA_ARGS__); print(\":\"); out(__VA_ARGS__); }\n#else\n#define debug(...)\n#define dump(...)\n#endif\n\nstruct io_setup {\n io_setup(int precision = 20) {\n ios::sync_with_stdio(false);\n cin.tie(0);\n cout << fixed << setprecision(precision);\n }\n} io_setup_ {};\n\nvoid solve() {\n int n, s, w, q;\n while(cin >> n >> s >> w >> q) {\n if(n == 0) break;\n int g = s;\n vc<int> a(n);\n rep(i, n) {\n a[i] = (g / 7) % 10;\n if(g % 2 == 0) {\n g = g / 2;\n } else {\n g = (g / 2) ^ w;\n }\n }\n if(q == 2 || q == 5) {\n int cnt = 0;\n ll ans = 0;\n rev_rep(i, n) {\n if(a[i] % q == 0) ++ cnt;\n if(a[i] != 0) ans += cnt;\n }\n out(ans);\n continue;\n }\n if(n <= 10) dump(a);\n ll ans = 0;\n map<int, int> mp;\n mp[0] ++;\n int cur = 0;\n int pw = 1;\n for(int i = n - 1; i >= 0; -- i) {\n (cur += a[i] * pw % q) %= q;\n (pw *= 10) %= q;\n if(n <= 10) debug(cur);\n if(a[i] != 0) ans += (ll)mp[cur];\n mp[cur] ++;\n }\n // vc<int> sum(n + 1, 0);\n // sum[n] = n;\n // pw = 1;\n // for(int i = n - 1; i >= 0; -- i) {\n // sum[i] = sum[i + 1] + a[i] * pw;\n // sum[i] %= q;\n // (pw *= 10) %= q;\n // }\n // rep(i, n) {\n // rep(j, i + 1, n + 1) {\n // if(a[i] != 0 && sum[i] == sum[j]) ++ ans;\n // }\n // }\n out(ans);\n } \n}\n\nsigned main() {\n int testcase = 1;\n // in(testcase);\n while(testcase--) solve();\n return 0;\n}", "accuracy": 1, "time_ms": 360, "memory_kb": 7800, "score_of_the_acc": -0.1452, "final_rank": 3 }, { "submission_id": "aoj_1310_6677476", "code_snippet": "#include <bits/stdc++.h>\n#pragma GCC optimize(\"Ofast\")\n#define _GLIBCXX_DEBUG\nusing namespace std;\nusing std::cout;\nusing std::cin;\nusing std::endl;\nusing ll=long long;\nusing ld=long double;\nll ILL=2167167167167167167;\nconst int INF=2100000000;\nconst ll mod=998244353;\n#define rep(i,a) for (int i=0;i<a;i++)\n#define all(p) p.begin(),p.end()\ntemplate<class T> using _pq = priority_queue<T, vector<T>, greater<T>>;\ntemplate<class T> ll LB(vector<T> &v,T a){return lower_bound(v.begin(),v.end(),a)-v.begin();}\ntemplate<class T> ll UB(vector<T> &v,T a){return upper_bound(v.begin(),v.end(),a)-v.begin();}\ntemplate<class T> bool chmin(T &a,const T &b){if(a>b){a=b;return 1;}else return 0;}\ntemplate<class T> bool chmax(T &a,const T &b){if(a<b){a=b;return 1;}else return 0;}\ntemplate<class T> void So(vector<T> &v) {sort(v.begin(),v.end());}\ntemplate<class T> void Sore(vector<T> &v) {sort(v.begin(),v.end(),[](T x,T y){return x>y;});}\nvoid yneos(bool a){if(a) cout<<\"Yes\\n\"; else cout<<\"No\\n\";}\ntemplate<class T> void vec_out(vector<T> &p){for(int i=0;i<(int)(p.size());i++){if(i) cout<<\" \";cout<<p[i];}cout<<\"\\n\";}\n\nint t=INF;\nvoid solve();\n// oddloop\nint main() {\n\tios::sync_with_stdio(false);\n\tcin.tie(nullptr);\n\t\n\t//cin>>t;\n\trep(i,t) solve();\n}\n\nvoid solve(){\n\tint N,S,W,Q;\n\tcin>>N>>S>>W>>Q;\n\tif(N==0){\n\t\tt=0;\n\t\treturn;\n\t}\n\tif(Q==2||Q==5){\n\t\tll ans=0,C=0;\n\t\trep(i,N){\n\t\t\tint tmp=(S/7)%10;\n\t\t\tif(tmp%Q==0){\n\t\t\t\tans+=C;\n\t\t\t\tif(tmp!=0) ans++;\n\t\t\t}if(tmp!=0) C++;\n\t\t\tif(S%2==0) S/=2;\n\t\t\telse S=((S/2)^W);\n\t\t}\n\t\tcout<<ans<<\"\\n\";\n\t\treturn;\n\t}\n\tmap<int,int> m;\n\tll ans=0;\n\tll R=1,A=10,P=Q-2;\n\twhile(P){\n\t\tif(P&1){\n\t\t\tR=(R*A)%Q;\n\t\t}\n\t\tA=(A*A)%Q;\n\t\tP>>=1;\n\t}\n\tll v=0,D=1;\n\trep(i,N){\n\t\tll tmp=(S/7)%10;\n\t\tif(tmp!=0) m[v]++;\n\t\tv=(v+tmp*D)%Q;\n\t\tD=(R*D)%Q;\n\t\tans+=m[v];\n\t\tif(S%2==0) S/=2;\n\t\telse S=((S/2)^W);\n\t}\n\tcout<<ans<<\"\\n\";\n}", "accuracy": 1, "time_ms": 390, "memory_kb": 7568, "score_of_the_acc": -0.4573, "final_rank": 8 }, { "submission_id": "aoj_1310_6583669", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\n#define rep(i, s, t) for (int i = (int)(s); i < (int)(t); ++i)\n#define revrep(i, t, s) for (int i = (int)(t)-1; i >= (int)(s); --i)\n#define all(x) begin(x), end(x)\ntemplate <typename T> bool chmax(T& a, const T& b) { return a < b ? (a = b, 1) : 0; }\ntemplate <typename T> bool chmin(T& a, const T& b) { return a > b ? (a = b, 1) : 0; }\n\ntemplate <typename T>\nostream& operator<<(ostream& os, const vector<T>& v) {\n for (int i = 0; i < (int) v.size(); ++i) os << v[i] << \" \";\n return os;\n}\n\nint main() {\n ios_base::sync_with_stdio(false);\n cin.tie(nullptr);\n cout << fixed << setprecision(15);\n\n while (true) {\n ll N, S, W, Q;\n cin >> N >> S >> W >> Q;\n if (N == 0) break;\n\n int g = S;\n vector<ll> a(N);\n rep(i,0,N) {\n a[i] = (g/7) % 10;\n if (g % 2 == 0) g = g / 2;\n else g = (g / 2) ^ W;\n }\n\n ll ans = 0;\n if (Q == 2 || Q == 5) {\n ll cnt = 0;\n rep(i,0,N) {\n if (a[i] != 0) ++cnt;\n if (a[i] % Q == 0) ans += cnt;\n }\n } else {\n map<ll, ll> cnt;\n cnt[0] = 1;\n reverse(all(a));\n ll x = 0;\n ll p = 1;\n rep(i,0,N) {\n x = (x + a[i]*p) % Q;\n p = p * 10 % Q;\n if (a[i] != 0) ans += cnt[x];\n ++cnt[x];\n }\n }\n cout << ans << \"\\n\";\n }\n}", "accuracy": 1, "time_ms": 370, "memory_kb": 9768, "score_of_the_acc": -0.8032, "final_rank": 11 }, { "submission_id": "aoj_1310_6337167", "code_snippet": "#include<bits/stdc++.h>\nusing ll = long long;\n#define var auto\nconst char newl = '\\n';\n\nusing namespace std;\n\ntemplate<typename T1, typename T2> inline void chmin(T1& a, T2 b) { if (a > b) a = b; }\ntemplate<typename T1, typename T2> inline void chmax(T1& a, T2 b) { if (a < b) a = b; }\n\nbool solve() {\n int n, s, w, q;\n cin >> n >> s >> w >> q;\n if (n == 0) return false;\n\n vector<int> a(n);\n {\n int g = s;\n for (int i = 0; i < n; i++) {\n a[i] = (g / 7) % 10;\n if (g % 2 == 0) { g = (g / 2); }\n else { g = (g / 2) ^ w; }\n }\n }\n\n ll res = 0;\n if (q == 2 || q == 5) {\n int tail_cnt = 0;\n for (int i = n - 1; i >= 0; i--) {\n if (a[i] % q == 0) tail_cnt++;\n if (a[i] != 0) res += tail_cnt;\n }\n }\n else {\n map<ll, int> tail_cnts;\n ll cur_mul = 0;\n ll cur_base = 1;\n tail_cnts[cur_mul]++;\n for (int i = n - 1; i >= 0; i--) {\n cur_mul = (a[i] * cur_base + cur_mul) % q;\n if (a[i] != 0) res += tail_cnts[cur_mul];\n tail_cnts[cur_mul]++;\n cur_base = (cur_base * 10) % q;\n }\n }\n\n cout << res << endl;\n return true;\n}\n\nint main() {\n ios::sync_with_stdio(false);\n cin.tie(nullptr);\n while (solve());\n}", "accuracy": 1, "time_ms": 400, "memory_kb": 9324, "score_of_the_acc": -1.058, "final_rank": 15 }, { "submission_id": "aoj_1310_6214711", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\n#define all(A) A.begin(),A.end()\nusing vll = vector<ll>;\n#define rep(i, n) for (long long i = 0; i < (long long)(n); i++)\nstruct F {\n ll L;\n ll R;\n\n};\nll d(F f) {\n return f.R - f.L + 1;\n}\n\nint main() {\n while (1) {\n ll N, S, W, Q;\n cin >> N >> S >> W >> Q;\n if (N == 0)return 0;\n\n vll A(N);\n ll g = S;\n for (int i = 0; i < N; i++) {\n A[i] = (g / 7) % 10;\n if (g % 2 == 0) { g = (g / 2); }\n else { g = (g / 2) ^ W; }\n }\n reverse(all(A));\n ll an = 0;\n map<ll, ll> M;\n M[0]++;\n ll k = 0;\n ll t = 1;\n if (Q == 2) {\n vll E(2, 0);\n //E[0]++;\n rep(i, N) {\n E[A[i] % 2]++;\n if (A[i] != 0)an += E[0];\n \n }\n cout << an << endl;\n continue;\n }\n else if (Q == 5) {\n vll E(5, 0);\n //E[0]++;\n rep(i, N) {\n E[A[i] % 5]++;\n if (A[i] != 0)an += E[0];\n \n }\n cout << an << endl;\n continue;\n }\n rep(i, N) {\n k += A[i] * t;\n k %= Q;\n M[k]++;\n t *= 10;\n t %= Q;\n\n if (A[i] != 0) {\n an += (M[k] - 1);\n }\n }\n cout << an << endl;\n }\n}", "accuracy": 1, "time_ms": 380, "memory_kb": 9888, "score_of_the_acc": -0.9607, "final_rank": 13 }, { "submission_id": "aoj_1310_6026504", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\nbool solve() {\n int N, S, W, Q;\n cin >> N >> S >> W >> Q;\n if (N == 0 and S == 0 and W == 0 and W == 0) return false;\n\n vector<int> a(N);\n int g = S;\n for (int i = 0; i < N; i++) {\n a[i] = (g / 7) % 10;\n if (g % 2 == 0) {\n g = (g / 2);\n } else {\n g = (g / 2) ^ W;\n }\n }\n\n long long ans = 0;\n if (Q == 2 or Q == 5) {\n int nonzero = 0;\n for (int i = 0; i < N; i++) {\n nonzero += (a[i] != 0);\n if (a[i] % Q == 0) ans += nonzero;\n }\n cout << ans << '\\n';\n return true;\n }\n\n int suf = 0;\n map<int, int> mp;\n mp[suf]++;\n for (int i = N - 1, power = 1; i >= 0; i--, (power *= 10) %= Q) {\n suf = (suf + a[i] * power) % Q;\n if (a[i] != 0) ans += mp[suf];\n mp[suf]++;\n }\n\n cout << ans << '\\n';\n return true;\n}\n\nint main() {\n cin.tie(0);\n ios::sync_with_stdio(false);\n while (solve())\n ;\n return 0;\n}", "accuracy": 1, "time_ms": 360, "memory_kb": 7756, "score_of_the_acc": -0.1333, "final_rank": 2 }, { "submission_id": "aoj_1310_6026503", "code_snippet": "#define LOCAL\n#include <bits/stdc++.h>\nusing namespace std;\n#pragma region Macros\ntypedef long long ll;\ntypedef __int128_t i128;\ntypedef unsigned int uint;\ntypedef unsigned long long ull;\n#define ALL(x) (x).begin(), (x).end()\n\ntemplate <typename T> istream& operator>>(istream& is, vector<T>& v) {\n for (T& x : v) is >> x;\n return is;\n}\ntemplate <typename T> ostream& operator<<(ostream& os, const vector<T>& v) {\n for (size_t i = 0; i < v.size(); i++) {\n os << v[i] << (i + 1 == v.size() ? \"\" : \" \");\n }\n return os;\n}\ntemplate <typename T, typename U> ostream& operator<<(ostream& os, const pair<T, U>& p) {\n os << '(' << p.first << ',' << p.second << ')';\n return os;\n}\ntemplate <typename T, typename U> ostream& operator<<(ostream& os, const map<T, U>& m) {\n os << '{';\n for (auto itr = m.begin(); itr != m.end();) {\n os << '(' << itr->first << ',' << itr->second << ')';\n if (++itr != m.end()) os << ',';\n }\n os << '}';\n return os;\n}\ntemplate <typename T, typename U> ostream& operator<<(ostream& os, const unordered_map<T, U>& m) {\n os << '{';\n for (auto itr = m.begin(); itr != m.end();) {\n os << '(' << itr->first << ',' << itr->second << ')';\n if (++itr != m.end()) os << ',';\n }\n os << '}';\n return os;\n}\ntemplate <typename T> ostream& operator<<(ostream& os, const set<T>& s) {\n os << '{';\n for (auto itr = s.begin(); itr != s.end();) {\n os << *itr;\n if (++itr != s.end()) os << ',';\n }\n os << '}';\n return os;\n}\ntemplate <typename T> ostream& operator<<(ostream& os, const multiset<T>& s) {\n os << '{';\n for (auto itr = s.begin(); itr != s.end();) {\n os << *itr;\n if (++itr != s.end()) os << ',';\n }\n os << '}';\n return os;\n}\ntemplate <typename T> ostream& operator<<(ostream& os, const unordered_set<T>& s) {\n os << '{';\n for (auto itr = s.begin(); itr != s.end();) {\n os << *itr;\n if (++itr != s.end()) os << ',';\n }\n os << '}';\n return os;\n}\ntemplate <typename T> ostream& operator<<(ostream& os, const deque<T>& v) {\n for (size_t i = 0; i < v.size(); i++) {\n os << v[i] << (i + 1 == v.size() ? \"\" : \" \");\n }\n return os;\n}\ntemplate <typename T, size_t N> ostream& operator<<(ostream& os, const array<T, N>& v) {\n for (size_t i = 0; i < N; i++) {\n os << v[i] << (i + 1 == N ? \"\" : \" \");\n }\n return os;\n}\n\ntemplate <int i, typename T> void print_tuple(ostream&, const T&) {}\ntemplate <int i, typename T, typename H, class... Args> void print_tuple(ostream& os, const T& t) {\n if (i) os << ',';\n os << get<i>(t);\n print_tuple<i + 1, T, Args...>(os, t);\n}\ntemplate <typename... Args> ostream& operator<<(ostream& os, const tuple<Args...>& t) {\n os << '{';\n print_tuple<0, tuple<Args...>, Args...>(os, t);\n return os << '}';\n}\n\nvoid debug_out() { cerr << '\\n'; }\ntemplate <class Head, class... Tail> void debug_out(Head&& head, Tail&&... tail) {\n cerr << head;\n if (sizeof...(Tail) > 0) cerr << \", \";\n debug_out(move(tail)...);\n}\n#ifdef LOCAL\n#define debug(...) \\\n cerr << \" \"; \\\n cerr << #__VA_ARGS__ << \" :[\" << __LINE__ << \":\" << __FUNCTION__ << \"]\" << '\\n'; \\\n cerr << \" \"; \\\n debug_out(__VA_ARGS__)\n#else\n#define debug(...) void(0)\n#endif\n\ntemplate <typename T> T gcd(T x, T y) { return y != 0 ? gcd(y, x % y) : x; }\ntemplate <typename T> T lcm(T x, T y) { return x / gcd(x, y) * y; }\n\nint topbit(signed t) { return t == 0 ? -1 : 31 - __builtin_clz(t); }\nint topbit(long long t) { return t == 0 ? -1 : 63 - __builtin_clzll(t); }\nint botbit(signed a) { return a == 0 ? 32 : __builtin_ctz(a); }\nint botbit(long long a) { return a == 0 ? 64 : __builtin_ctzll(a); }\nint popcount(signed t) { return __builtin_popcount(t); }\nint popcount(long long t) { return __builtin_popcountll(t); }\nbool ispow2(int i) { return i && (i & -i) == i; }\nlong long MSK(int n) { return (1LL << n) - 1; }\n\ntemplate <class T> T ceil(T x, T y) {\n assert(y >= 1);\n return (x > 0 ? (x + y - 1) / y : x / y);\n}\ntemplate <class T> T floor(T x, T y) {\n assert(y >= 1);\n return (x > 0 ? x / y : (x - y + 1) / y);\n}\n\ntemplate <class T1, class T2> inline bool chmin(T1& a, T2 b) {\n if (a > b) {\n a = b;\n return true;\n }\n return false;\n}\ntemplate <class T1, class T2> inline bool chmax(T1& a, T2 b) {\n if (a < b) {\n a = b;\n return true;\n }\n return false;\n}\n\ntemplate <typename T> void mkuni(vector<T>& v) {\n sort(v.begin(), v.end());\n v.erase(unique(v.begin(), v.end()), v.end());\n}\ntemplate <typename T> int lwb(const vector<T>& v, const T& x) { return lower_bound(v.begin(), v.end(), x) - v.begin(); }\n#pragma endregion\n\nconst int INF = 1e9;\nconst long long IINF = 1e18;\nconst int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};\nconst char dir[4] = {'D', 'R', 'U', 'L'};\nconst long long MOD = 1000000007;\n// const long long MOD = 998244353;\n\nbool solve() {\n int N, S, W, Q;\n cin >> N >> S >> W >> Q;\n if (N == 0 and S == 0 and W == 0 and W == 0) return false;\n\n vector<int> a(N);\n int g = S;\n for (int i = 0; i < N; i++) {\n a[i] = (g / 7) % 10;\n if (g % 2 == 0) {\n g = (g / 2);\n } else {\n g = (g / 2) ^ W;\n }\n }\n\n ll ans = 0;\n if (Q == 2 or Q == 5) {\n int nonzero = 0;\n for (int i = 0; i < N; i++) {\n nonzero += (a[i] != 0);\n if (a[i] % Q == 0) ans += nonzero;\n }\n cout << ans << '\\n';\n return true;\n }\n\n int suf = 0;\n map<int, int> mp;\n mp[suf]++;\n for (int i = N - 1, power = 1; i >= 0; i--, (power *= 10) %= Q) {\n suf = (suf + a[i] * power) % Q;\n if (a[i] != 0) ans += mp[suf];\n mp[suf]++;\n }\n\n cout << ans << '\\n';\n return true;\n}\n\nint main() {\n cin.tie(0);\n ios::sync_with_stdio(false);\n while (solve())\n ;\n return 0;\n}", "accuracy": 1, "time_ms": 370, "memory_kb": 7580, "score_of_the_acc": -0.2106, "final_rank": 4 }, { "submission_id": "aoj_1310_6014367", "code_snippet": "#pragma GCC optimize(\"Ofast\")\n#include <iostream>\n#include <vector>\n#include <algorithm>\n#include <map>\n#include <queue>\n#include <cstdio>\n#include <ctime>\n#include <assert.h>\n#include <chrono>\n#include <random>\n#include <numeric>\n#include <set>\n#include <deque>\n#include <stack>\n#include <sstream>\n#include <utility>\n#include <cstring>\n#include <unordered_map>\n#include <unordered_set>\n#include <tuple>\n#include <array>\n#include <bitset>\nusing namespace std;\ntypedef long long int ll;\ntypedef unsigned long long ull;\n\nmt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());\nll myRand(ll B) {\n return (ull)rng() % B;\n}\ninline double time() {\n return static_cast<double>(chrono::duration_cast<chrono::nanoseconds>(chrono::steady_clock::now().time_since_epoch()).count()) * 1e-9;\n}\n\nconstexpr ll mod = 1e9+7;\n\nint main(){\n cin.tie(nullptr);\n ios::sync_with_stdio(false);\n int n,s,w,q;\n while(cin >> n >> s >> w >> q,n){\n ll g = s;\n vector<int> a(n);\n for(int i=0;i<n;i++){\n a[i] = (g/7) % 10;\n if(g&1) g = (g>>1)^w;\n else g = (g>>1);\n }\n // 場合分け\n if(q == 2 or q == 5){\n ll res = 0;\n ll pre = 0;\n for(int i=0;i<n;i++){\n if(a[i]) pre++;\n if(q == 2){\n if(!(a[i]&1)) res += pre;\n }\n else{\n if(a[i] == 0 or a[i] == 5) res += pre;\n }\n }\n cout << res << \"\\n\";\n }\n else{\n vector<ll> sum(n+1);\n ll po = 1;\n for(int i=n-1;i>=0;i--){\n sum[i] = (sum[i+1] + a[i]*po) % q;\n po *= 10; if(po >= q) po %= q;\n }\n map<ll,ll> mp;\n ll res = 0;\n for(int i=0;i<n;i++){\n if(a[i]) mp[sum[i]]++;\n res += mp[sum[i+1]];\n }\n cout << res << \"\\n\";\n }\n }\n}", "accuracy": 1, "time_ms": 400, "memory_kb": 10168, "score_of_the_acc": -1.2866, "final_rank": 17 }, { "submission_id": "aoj_1310_6006874", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\nint main() {\n cin.tie(0);\n ios::sync_with_stdio(false);\n\n while (true) {\n int n, s, w, q;\n cin >> n >> s >> w >> q;\n if (n == 0) return 0;\n\n vector<int> a(n);\n int g = s;\n for (int i = 0; i < n; i++) {\n a.at(i) = (g / 7) % 10;\n if (g % 2 == 0) g = g / 2;\n else g = ((g / 2) ^ w);\n }\n\n if (q == 2 || q == 5) {\n int ans = 0, count = 0;\n for (int i = n - 1; i >= 0; i--) {\n if (a.at(i) % q == 0) count++;\n if (a.at(i) != 0) ans += count;\n }\n cout << ans << '\\n';\n continue;\n }\n\n map<int, int> mp;\n mp[0]++;\n int now = 0, now10 = 1, ans = 0;\n for (int i = n - 1; i >= 0; i--) {\n now += now10 * a.at(i);\n now %= q;\n now10 = (now10 * 10) % q;\n if (a.at(i) != 0) ans += mp[now];\n mp[now]++;\n }\n\n cout << ans << '\\n';\n }\n}", "accuracy": 1, "time_ms": 360, "memory_kb": 7720, "score_of_the_acc": -0.1235, "final_rank": 1 }, { "submission_id": "aoj_1310_6004979", "code_snippet": "#line 1 \"/Users/nok0/Documents/Programming/nok0/cftemp.hpp\"\n#include <bits/stdc++.h>\nusing namespace std;\n\n#pragma region Macros\n// rep macro\n#define foa(v, a) for(auto &v : a)\n#define REPname(a, b, c, d, e, ...) e\n#define REP(...) REPname(__VA_ARGS__, REP3, REP2, REP1, REP0)(__VA_ARGS__)\n#define REP0(x) for(int i = 0; i < (x); ++i)\n#define REP1(i, x) for(int i = 0; i < (x); ++i)\n#define REP2(i, l, r) for(int i = (l); i < (r); ++i)\n#define REP3(i, l, r, c) for(int i = (l); i < (r); i += (c))\n#define REPSname(a, b, c, ...) c\n#define REPS(...) REPSname(__VA_ARGS__, REPS1, REPS0)(__VA_ARGS__)\n#define REPS0(x) for(int i = 1; i <= (x); ++i)\n#define REPS1(i, x) for(int i = 1; i <= (x); ++i)\n#define RREPname(a, b, c, d, e, ...) e\n#define RREP(...) RREPname(__VA_ARGS__, RREP3, RREP2, RREP1, RREP0)(__VA_ARGS__)\n#define RREP0(x) for(int i = (x)-1; i >= 0; --i)\n#define RREP1(i, x) for(int i = (x)-1; i >= 0; --i)\n#define RREP2(i, r, l) for(int i = (r)-1; i >= (l); --i)\n#define RREP3(i, r, l, c) for(int i = (r)-1; i >= (l); i -= (c))\n#define RREPSname(a, b, c, ...) c\n#define RREPS(...) RREPSname(__VA_ARGS__, RREPS1, RREPS0)(__VA_ARGS__)\n#define RREPS0(x) for(int i = (x); i >= 1; --i)\n#define RREPS1(i, x) for(int i = (x); i >= 1; --i)\n\n// name macro\n#define pb push_back\n#define eb emplace_back\n#define SZ(x) ((int)(x).size())\n#define all(x) (x).begin(), (x).end()\n#define rall(x) (x).rbegin(), (x).rend()\n#define popcnt(x) __builtin_popcountll(x)\ntemplate <class T = int>\nusing V = std::vector<T>;\ntemplate <class T = int>\nusing VV = std::vector<std::vector<T>>;\ntemplate <class T>\nusing pqup = std::priority_queue<T, std::vector<T>, std::greater<T>>;\nusing ll = long long;\nusing ld = long double;\nusing int128 = __int128_t;\nusing pii = std::pair<int, int>;\nusing pll = std::pair<long long, long long>;\n\n// input macro\ntemplate <class T, class U>\nstd::istream &operator>>(std::istream &is, std::pair<T, U> &p) {\n\tis >> p.first >> p.second;\n\treturn is;\n}\ntemplate <class T>\nstd::istream &operator>>(std::istream &is, std::vector<T> &v) {\n\tfor(T &i : v) is >> i;\n\treturn is;\n}\nstd::istream &operator>>(std::istream &is, __int128_t &a) {\n\tstd::string s;\n\tis >> s;\n\t__int128_t ret = 0;\n\tfor(int i = 0; i < s.length(); i++)\n\t\tif('0' <= s[i] and s[i] <= '9')\n\t\t\tret = 10 * ret + s[i] - '0';\n\ta = ret * (s[0] == '-' ? -1 : 1);\n\treturn is;\n}\nnamespace scanner {\nvoid scan(int &a) { std::cin >> a; }\nvoid scan(long long &a) { std::cin >> a; }\nvoid scan(std::string &a) { std::cin >> a; }\nvoid scan(char &a) { std::cin >> a; }\nvoid scan(char a[]) { std::scanf(\"%s\", a); }\nvoid scan(double &a) { std::cin >> a; }\nvoid scan(long double &a) { std::cin >> a; }\ntemplate <class T, class U>\nvoid scan(std::pair<T, U> &p) { std::cin >> p; }\ntemplate <class T>\nvoid scan(std::vector<T> &a) { std::cin >> a; }\nvoid INPUT() {}\ntemplate <class Head, class... Tail>\nvoid INPUT(Head &head, Tail &...tail) {\n\tscan(head);\n\tINPUT(tail...);\n}\n} // namespace scanner\n#define VEC(type, name, size) \\\n\tstd::vector<type> name(size); \\\n\tscanner::INPUT(name)\n#define VVEC(type, name, h, w) \\\n\tstd::vector<std::vector<type>> name(h, std::vector<type>(w)); \\\n\tscanner::INPUT(name)\n#define INT(...) \\\n\tint __VA_ARGS__; \\\n\tscanner::INPUT(__VA_ARGS__)\n#define LL(...) \\\n\tlong long __VA_ARGS__; \\\n\tscanner::INPUT(__VA_ARGS__)\n#define STR(...) \\\n\tstd::string __VA_ARGS__; \\\n\tscanner::INPUT(__VA_ARGS__)\n#define CHAR(...) \\\n\tchar __VA_ARGS__; \\\n\tscanner::INPUT(__VA_ARGS__)\n#define DOUBLE(...) \\\n\tdouble __VA_ARGS__; \\\n\tscanner::INPUT(__VA_ARGS__)\n#define LD(...) \\\n\tlong double __VA_ARGS__; \\\n\tscanner::INPUT(__VA_ARGS__)\n\n// output-macro\ntemplate <class T, class U>\nstd::ostream &operator<<(std::ostream &os, const std::pair<T, U> &p) {\n\tos << p.first << \" \" << p.second;\n\treturn os;\n}\ntemplate <class T>\nstd::ostream &operator<<(std::ostream &os, const std::vector<T> &a) {\n\tfor(int i = 0; i < int(a.size()); ++i) {\n\t\tif(i) os << \" \";\n\t\tos << a[i];\n\t}\n\treturn os;\n}\nstd::ostream &operator<<(std::ostream &dest, __int128_t &value) {\n\tstd::ostream::sentry s(dest);\n\tif(s) {\n\t\t__uint128_t tmp = value < 0 ? -value : value;\n\t\tchar buffer[128];\n\t\tchar *d = std::end(buffer);\n\t\tdo {\n\t\t\t--d;\n\t\t\t*d = \"0123456789\"[tmp % 10];\n\t\t\ttmp /= 10;\n\t\t} while(tmp != 0);\n\t\tif(value < 0) {\n\t\t\t--d;\n\t\t\t*d = '-';\n\t\t}\n\t\tint len = std::end(buffer) - d;\n\t\tif(dest.rdbuf()->sputn(d, len) != len) {\n\t\t\tdest.setstate(std::ios_base::badbit);\n\t\t}\n\t}\n\treturn dest;\n}\ntemplate <class T>\nvoid print(const T a) { std::cout << a << '\\n'; }\ntemplate <class Head, class... Tail>\nvoid print(Head H, Tail... T) {\n\tstd::cout << H << ' ';\n\tprint(T...);\n}\ntemplate <class T>\nvoid printel(const T a) { std::cout << a << '\\n'; }\ntemplate <class T>\nvoid printel(const std::vector<T> &a) {\n\tfor(const auto &v : a)\n\t\tstd::cout << v << '\\n';\n}\ntemplate <class Head, class... Tail>\nvoid printel(Head H, Tail... T) {\n\tstd::cout << H << '\\n';\n\tprintel(T...);\n}\nvoid Yes(const bool b = true) { std::cout << (b ? \"Yes\\n\" : \"No\\n\"); }\nvoid No() { std::cout << \"No\\n\"; }\nvoid YES(const bool b = true) { std::cout << (b ? \"YES\\n\" : \"NO\\n\"); }\nvoid NO() { std::cout << \"NO\\n\"; }\nvoid err(const bool b = true) {\n\tif(b) {\n\t\tstd::cout << \"-1\\n\", exit(0);\n\t}\n}\n\n//debug macro\nnamespace debugger {\ntemplate <class T>\nvoid view(const std::vector<T> &a) {\n\tstd::cerr << \"{ \";\n\tfor(const auto &v : a) {\n\t\tstd::cerr << v << \", \";\n\t}\n\tstd::cerr << \"\\b\\b }\";\n}\ntemplate <class T>\nvoid view(const std::vector<std::vector<T>> &a) {\n\tstd::cerr << \"{\\n\";\n\tfor(const auto &v : a) {\n\t\tstd::cerr << \"\\t\";\n\t\tview(v);\n\t\tstd::cerr << \"\\n\";\n\t}\n\tstd::cerr << \"}\";\n}\ntemplate <class T, class U>\nvoid view(const std::vector<std::pair<T, U>> &a) {\n\tstd::cerr << \"{\\n\";\n\tfor(const auto &p : a) std::cerr << \"\\t(\" << p.first << \", \" << p.second << \")\\n\";\n\tstd::cerr << \"}\";\n}\ntemplate <class T, class U>\nvoid view(const std::map<T, U> &m) {\n\tstd::cerr << \"{\\n\";\n\tfor(const auto &p : m) std::cerr << \"\\t[\" << p.first << \"] : \" << p.second << \"\\n\";\n\tstd::cerr << \"}\";\n}\ntemplate <class T, class U>\nvoid view(const std::pair<T, U> &p) { std::cerr << \"(\" << p.first << \", \" << p.second << \")\"; }\ntemplate <class T>\nvoid view(const std::set<T> &s) {\n\tstd::cerr << \"{ \";\n\tfor(auto &v : s) {\n\t\tview(v);\n\t\tstd::cerr << \", \";\n\t}\n\tstd::cerr << \"\\b\\b }\";\n}\n\ntemplate <class T>\nvoid view(const T &e) { std::cerr << e; }\n} // namespace debugger\n#ifdef LOCAL\nvoid debug_out() {}\ntemplate <typename Head, typename... Tail>\nvoid debug_out(Head H, Tail... T) {\n\tdebugger::view(H);\n\tstd::cerr << \", \";\n\tdebug_out(T...);\n}\n#define debug(...) \\\n\tdo { \\\n\t\tstd::cerr << __LINE__ << \" [\" << #__VA_ARGS__ << \"] : [\"; \\\n\t\tdebug_out(__VA_ARGS__); \\\n\t\tstd::cerr << \"\\b\\b]\\n\"; \\\n\t} while(false)\n#else\n#define debug(...) (void(0))\n#endif\n\n// vector macro\ntemplate <class T>\nint lb(const std::vector<T> &a, const T x) { return std::distance((a).begin(), std::lower_bound((a).begin(), (a).end(), (x))); }\ntemplate <class T>\nint ub(const std::vector<T> &a, const T x) { return std::distance((a).begin(), std::upper_bound((a).begin(), (a).end(), (x))); }\ntemplate <class T>\nvoid UNIQUE(std::vector<T> &a) {\n\tstd::sort(a.begin(), a.end());\n\ta.erase(std::unique(a.begin(), a.end()), a.end());\n}\ntemplate <class T>\nstd::vector<T> press(std::vector<T> &a) {\n\tauto res = a;\n\tUNIQUE(res);\n\tfor(auto &v : a)\n\t\tv = lb(res, v);\n\treturn res;\n}\n#define SORTname(a, b, c, ...) c\n#define SORT(...) SORTname(__VA_ARGS__, SORT1, SORT0, ...)(__VA_ARGS__)\n#define SORT0(a) std::sort((a).begin(), (a).end())\n#define SORT1(a, c) std::sort((a).begin(), (a).end(), [](const auto x, const auto y) { return x c y; })\ntemplate <class T>\nvoid ADD(std::vector<T> &a, const T x = 1) {\n\tfor(auto &v : a) v += x;\n}\ntemplate <class T>\nvoid SUB(std::vector<T> &a, const T x = 1) {\n\tfor(auto &v : a) v -= x;\n}\nstd::vector<std::pair<char, int>> rle(const string &s) {\n\tint n = s.size();\n\tstd::vector<std::pair<char, int>> ret;\n\tfor(int l = 0; l < n;) {\n\t\tint r = l + 1;\n\t\tfor(; r < n and s[l] == s[r]; r++) {}\n\t\tret.emplace_back(s[l], r - l);\n\t\tl = r;\n\t}\n\treturn ret;\n}\ntemplate <class T>\nstd::vector<std::pair<T, int>> rle(const std::vector<T> &v) {\n\tint n = v.size();\n\tstd::vector<std::pair<T, int>> ret;\n\tfor(int l = 0; l < n;) {\n\t\tint r = l + 1;\n\t\tfor(; r < n and v[l] == v[r]; r++) {}\n\t\tret.emplace_back(v[l], r - l);\n\t\tl = r;\n\t}\n\treturn ret;\n}\nstd::vector<int> iota(int n) {\n\tstd::vector<int> p(n);\n\tstd::iota(p.begin(), p.end(), 0);\n\treturn p;\n}\ntemplate <class T>\nstruct cum_vector {\npublic:\n\tcum_vector() = default;\n\ttemplate <class U>\n\tcum_vector(const std::vector<U> &vec) : cum((int)vec.size() + 1) {\n\t\tfor(int i = 0; i < (int)vec.size(); i++)\n\t\t\tcum[i + 1] = cum[i] + vec[i];\n\t}\n\tT prod(int l, int r) {\n\t\treturn cum[r] - cum[l];\n\t}\n\nprivate:\n\tstd::vector<T> cum;\n};\n\n// math macro\ntemplate <class T, class U>\ninline bool chmin(T &a, const U &b) { return a > b ? a = b, true : false; }\ntemplate <class T, class U>\ninline bool chmax(T &a, const U &b) { return a < b ? a = b, true : false; }\ntemplate <class T>\nT divup(T x, T y) { return (x + y - 1) / y; }\ntemplate <class T>\nT POW(T a, long long n) {\n\tT ret = 1;\n\twhile(n) {\n\t\tif(n & 1) ret *= a;\n\t\ta *= a;\n\t\tn >>= 1;\n\t}\n\treturn ret;\n}\n// modpow\nlong long POW(long long a, long long n, const int mod) {\n\tlong long ret = 1;\n\ta = (a % mod + mod) % mod;\n\twhile(n) {\n\t\tif(n & 1) (ret *= a) %= mod;\n\t\t(a *= a) %= mod;\n\t\tn >>= 1;\n\t}\n\treturn ret;\n}\ntemplate <class T, class F>\nT bin_search(T ok, T ng, const F &f) {\n\twhile(abs(ok - ng) > 1) {\n\t\tT mid = (ok + ng) >> 1;\n\t\t(f(mid) ? ok : ng) = mid;\n\t}\n\treturn ok;\n}\ntemplate <class T, class F>\nT bin_search(T ok, T ng, const F &f, int loop) {\n\tfor(int i = 0; i < loop; i++) {\n\t\tT mid = (ok + ng) / 2;\n\t\t(f(mid) ? ok : ng) = mid;\n\t}\n\treturn ok;\n}\n\n// others\nstruct fast_io {\n\tfast_io() {\n\t\tios::sync_with_stdio(false);\n\t\tcin.tie(nullptr);\n\t\tcout << fixed << setprecision(15);\n\t}\n} fast_io_;\nconst int inf = 1e9;\nconst ll INF = 1e18;\n#pragma endregion\n\nvoid main_();\n\nint main() {\n\tmain_();\n\treturn 0;\n}\n#line 2 \"a.cpp\"\n\nvector<int> gen(int n, int s, int w) {\n\tint g = s;\n\tvector<int> a(n);\n\tfor(int i = 0; i < n; i++) {\n\t\ta[i] = (g / 7) % 10;\n\t\tif(g % 2 == 0)\n\t\t\tg /= 2;\n\t\telse\n\t\t\tg = (g / 2) ^ w;\n\t}\n\treturn a;\n}\n\nvoid main_() {\n\twhile(1) {\n\t\tINT(n, s, w, q);\n\t\tif(!n) break;\n\t\tauto a = gen(n, s, w);\n\t\tif(q == 2 or q == 5) {\n\t\t\tV<> valid(n);\n\t\t\tREP(i, n) { valid[i] = !!a[i]; }\n\t\t\tcum_vector<int> cum(valid);\n\t\t\tint res = 0;\n\t\t\tRREP(i, n) {\n\t\t\t\tif(a[i] % q) continue;\n\t\t\t\tres += cum.prod(0, i + 1);\n\t\t\t}\n\t\t\tprint(res);\n\t\t\tcontinue;\n\t\t}\n\t\tmap<int, int> memo;\n\t\tll val = 0, base = 1, res = 0;\n\t\tmemo[0] = 1;\n\t\tRREP(i, n) {\n\t\t\tval += (ll)a[i] * base;\n\t\t\tval %= q;\n\t\t\tbase *= 10;\n\t\t\tbase %= q;\n\t\t\tif(a[i]) res += memo[val];\n\t\t\tmemo[val]++;\n\t\t}\n\t\tprint(res);\n\t}\n}", "accuracy": 1, "time_ms": 380, "memory_kb": 7840, "score_of_the_acc": -0.406, "final_rank": 7 }, { "submission_id": "aoj_1310_5995675", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\nusing ll = long long;\nusing PII = pair<ll, ll>;\n\n#define FOR(i, a, n) for (ll i = (ll)a; i < (ll)n; ++i)\n#define REP(i, n) FOR(i, 0, n)\n#define ALL(x) x.begin(), x.end()\n#define POPCOUNT(x) __builtin_popcount(x)\ntemplate <typename T> void chmin(T &a, const T &b) { a = min(a, b); }\ntemplate <typename T> void chmax(T &a, const T &b) { a = max(a, b); }\n\nconst ll INF = 1LL << 60;\n\nstruct FastIO {\n FastIO() {\n cin.tie(0);\n ios::sync_with_stdio(0);\n }\n} fastiofastio;\n\nconst ll MOD = 1e9 + 7;\n\n// BEGIN CUT\nll modpow(ll x, ll y, ll m) {\n ll a = 1, p = x;\n while (y > 0) {\n if (y % 2 == 0) {\n p = (p * p) % m;\n y /= 2;\n } else {\n a = (a * p) % m;\n y--;\n }\n }\n return a;\n}\n// END CUT\n\nll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }\nll lcm(ll a, ll b) { return a / gcd(a, b) * b; }\n\nint A[100000];\nll B[100001];\nll mul[100000];\n\nint main() {\n int N, S, W, Q;\n while (cin >> N >> S >> W >> Q, N) {\n int g = S;\n for (int i = 0; i < N; i++) {\n A[i] = (g / 7) % 10;\n if (g % 2 == 0) {\n g = (g / 2);\n } else {\n g = (g / 2) ^ W;\n }\n }\n if (Q == 2 || Q == 5) {\n int ans = 0;\n int k = 0;\n for (int i = 0; i < N; i++) {\n if (A[i] == 0) {\n ans += k;\n } else {\n if (A[i] % Q == 0)\n ans += k + 1;\n k++;\n }\n }\n cout << ans << '\\n';\n continue;\n }\n mul[N - 1] = 1;\n for (int i = N - 2; i >= 0; i--) {\n mul[i] = (mul[i + 1] * 10) % Q;\n }\n memset(B, 0, sizeof(B));\n for (int i = 0; i < N; i++) {\n if (i > 0)\n B[i] = B[i - 1];\n (B[i] += A[i] * mul[i] % Q) %= Q;\n }\n map<int, int> cnt;\n int prev = 0;\n int ans = 0;\n for (int i = 0; i < N; i++) {\n ans += cnt[B[i]];\n if (A[i] != 0) {\n cnt[prev]++;\n }\n if (A[i] != 0 && A[i] % Q == 0)\n ans++;\n prev = B[i];\n }\n cout << ans << '\\n';\n }\n}", "accuracy": 1, "time_ms": 440, "memory_kb": 9388, "score_of_the_acc": -1.5753, "final_rank": 19 }, { "submission_id": "aoj_1310_5960866", "code_snippet": "#pragma GCC tag(\"avx2\")\n#pragma GCC optimize(\"Ofast\")\n#pragma GCC optimize(\"unroll-loops\")\n#include <bits/stdc++.h>\nusing namespace std;\n#define DEBUG\n#ifdef DEBUG\ntemplate <class T, class U>\nostream &operator<<(ostream &os, const pair<T, U> &p) {\n os << '(' << p.first << ',' << p.second << ')';\n return os;\n}\ntemplate <class T> ostream &operator<<(ostream &os, const vector<T> &v) {\n os << '{';\n for(int i = 0; i < (int)v.size(); i++) {\n if(i) { os << ','; }\n os << v[i];\n }\n os << '}';\n return os;\n}\nvoid debugg() { cerr << endl; }\ntemplate <class T, class... Args>\nvoid debugg(const T &x, const Args &... args) {\n cerr << \" \" << x;\n debugg(args...);\n}\n#define debug(...) \\\n cerr << __LINE__ << \" [\" << #__VA_ARGS__ << \"]: \", debugg(__VA_ARGS__)\n#define dump(x) cerr << __LINE__ << \" \" << #x << \" = \" << (x) << endl\n#else\n#define debug(...) (void(0))\n#define dump(x) (void(0))\n#endif\nusing namespace std;\ntypedef long long ll;\ntypedef vector<ll> vl;\ntypedef vector<vl> vvl;\ntypedef vector<char> vc;\ntypedef vector<string> vs;\ntypedef vector<bool> vb;\ntypedef vector<double> vd;\ntypedef pair<ll,ll> P;\ntypedef pair<int,int> pii;\ntypedef vector<P> vpl;\ntypedef tuple<ll,ll,ll> tapu;\n#define rep(i,n) for(int i=0; i<(n); i++)\n#define REP(i,a,b) for(int i=(a); i<(b); i++)\n#define all(x) (x).begin(), (x).end()\n#define rall(x) (x).rbegin(), (x).rend()\ntemplate<typename T1,typename T2> inline bool chmin(T1 &a,T2 b){\n\tif(a>b){\n\t\ta = b; return true;\n\t}\n\telse return false;\n}\ntemplate<typename T1,typename T2> inline bool chmax(T1 &a,T2 b){\n\tif(a<b){\n\t\ta = b; return true;\n\t}\n\telse return false;\n}\ntemplate<typename T> inline void print(T &a){\n int sz = a.size();\n for(auto itr = a.begin(); itr != a.end(); itr++){\n\t\tcout << *itr;\n sz--;\n if(sz) cout << \" \";\n\t}\n cout << \"\\n\";\n}\ntemplate<typename T1,typename T2> inline void print2(T1 a, T2 b){\n\tcout << a << \" \" << b << \"\\n\";\n}\ntemplate<typename T1,typename T2,typename T3> inline void print3(T1 a, T2 b, T3 c){\n\tcout << a << \" \" << b << \" \" << c << \"\\n\";\n}\nvoid mark() {cout << \"#\" << \"\\n\";}\nll pcount(ll x) {return __builtin_popcountll(x);}\n//#include <atcoder/maxflow>\n//using namespace atcoder;\nconst int inf = (1<<30)-1;\nconst ll linf = 1LL<<61;\nconst int MAX = 5050000;\nint dy[8] = {0,1,0,-1,1,-1,-1,1};\nint dx[8] = {-1,0,1,0,1,-1,1,-1};\nconst double pi = acos(-1);\nconst double eps = 1e-9;\nconst int mod = 1e9 + 7;\n//const int mod = 998244353;\n\nll modpow(ll x, ll n, ll mod){\n ll res = 1;\n while(n > 0){\n if(n & 1) res = res * x % mod;\n x = x * x % mod;\n n >>= 1;\n }\n return res;\n}\n\nint main(){\n while(1){\n ll n,s,w,p; cin >> n >> s >> w >> p;\n if(!n) break;\n vl a(n);\n ll g = s;\n rep(i,n){\n a[i] = (g/7) % 10;\n if(g % 2 == 0) g = g/2;\n else g = (g/2) ^ w;\n }\n ll inv10 = modpow(10,p-2,p);\n ll ans = 0;\n if(p == 2 || p == 5){\n ll cnt = 0;\n rep(i,n){\n if(a[i] == 0) ans += cnt;\n else{\n cnt++;\n if(a[i] % p == 0) ans += cnt;\n }\n }\n }else{\n map<ll,ll> mp;\n ll now = 0;\n ll mul = 1;\n mp[0]++;\n rep(i,n){\n if(a[i] == 0) ans--;\n now = (now * 10 + a[i]) % p;\n ll tmp = now * mul % p;\n if(mp.count(tmp)) ans += mp[tmp];\n if(a[i] != 0) mp[tmp]++;\n mul = mul * inv10 % p;\n }\n }\n cout << ans << \"\\n\";\n }\n}", "accuracy": 1, "time_ms": 430, "memory_kb": 9288, "score_of_the_acc": -1.4232, "final_rank": 18 }, { "submission_id": "aoj_1310_5537799", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\n\nint main() {\n int N, S, W, Q;\n while (cin >> N >> S >> W >> Q, N) {\n vector<int> a(N);\n int g = S;\n for (int i = 0; i < N; i++) {\n a[i] = (g / 7) % 10;\n if (g % 2 == 0) {\n g = (g / 2);\n } else {\n g = (g / 2) ^ W;\n }\n }\n int ans = 0;\n if (10 % Q == 0) {\n int maine = 0;\n for (int i = N - 1; i >= 0; i--) {\n maine += (a[i] % Q == 0);\n if (a[i])\n ans += maine;\n }\n } else {\n map<int, int> maine;\n maine[0]++;\n int book = 0, tens = 1;\n for (int i = N - 1; i >= 0; i--) {\n (tens *= 10) %= Q;\n (book += tens * a[i]) %= Q;\n if (a[i] != 0)\n ans += maine[book];\n maine[book]++;\n }\n }\n cout << ans << endl;\n }\n}", "accuracy": 1, "time_ms": 380, "memory_kb": 7668, "score_of_the_acc": -0.3594, "final_rank": 6 }, { "submission_id": "aoj_1310_5252898", "code_snippet": "#include <iostream>\n#include <algorithm>\n#include <vector>\n#include <map>\n\nusing namespace std;\n\nbool solve() {\n int n;\n cin >> n;\n if (n == 0) return false;\n\n vector<int> ds(n);\n {\n int s, w;\n cin >> s >> w;\n\n for (auto& d : ds) {\n d = (s / 7) % 10;\n\n if (s % 2 == 0) {\n s = s / 2;\n } else {\n s = (s / 2) ^ w;\n }\n }\n }\n\n int p;\n cin >> p;\n\n int ans = 0;\n if (p == 2 || p == 5) {\n int acc = 0;\n for (auto d : ds) {\n if (d != 0) ++acc;\n if (d % p == 0) ans += acc;\n }\n } else {\n reverse(ds.begin(), ds.end());\n map<int, int> cnt;\n\n int sum = 0, deg = 1;\n for (auto d : ds) {\n ++cnt[sum];\n (sum += d * deg) %= p;\n (deg *= 10) %= p;\n\n if (d != 0 && cnt.count(sum)) ans += cnt[sum];\n }\n }\n\n cout << ans << \"\\n\";\n return true;\n}\n\nint main() {\n cin.tie(nullptr);\n ios::sync_with_stdio(false);\n\n while (solve()) {}\n\n return 0;\n}", "accuracy": 1, "time_ms": 370, "memory_kb": 7692, "score_of_the_acc": -0.2409, "final_rank": 5 }, { "submission_id": "aoj_1310_4956348", "code_snippet": "#include<iostream>\n#include<map>\nusing namespace std;\nlong modpow(long a,long b,long mod){return b?modpow(a*a%mod,b/2,mod)*(b%2?a:1)%mod:1%mod;}\nint N,S,W,Q;\nmain()\n{\n\twhile(cin>>N>>S>>W>>Q,N)\n\t{\n\t\tlong inv=modpow(10,Q-2,Q),pinv=1;\n\t\tlong ans=0,now=0,cnt=0;\n\t\tmap<int,int>mp;\n\t\tfor(int i=0;i<N;i++)\n\t\t{\n\t\t\tint a=S/7%10;\n\t\t\tif(a!=0)\n\t\t\t{\n\t\t\t\tmp[now]++;\n\t\t\t\tcnt++;\n\t\t\t}\n\t\t\tnow=(now+pinv*a)%Q;\n\t\t\tpinv=pinv*inv%Q;\n\t\t\tif(Q==2||Q==5)\n\t\t\t{\n\t\t\t\tif(a%Q==0)ans+=cnt;\n\t\t\t}\n\t\t\telse ans+=mp[now];\n\t\t\tif(S%2==0)S/=2;\n\t\t\telse S=S/2^W;\n\t\t}\n\t\tcout<<ans<<endl;\n\t}\n}", "accuracy": 1, "time_ms": 410, "memory_kb": 7304, "score_of_the_acc": -0.6358, "final_rank": 10 }, { "submission_id": "aoj_1310_4941064", "code_snippet": "#include \"iostream\"\n#include \"climits\"\n#include \"list\"\n#include \"queue\"\n#include \"stack\"\n#include \"set\"\n#include \"functional\"\n#include \"algorithm\"\n#include \"string\"\n#include \"map\"\n#include \"unordered_map\"\n#include \"unordered_set\"\n#include \"iomanip\"\n#include \"cmath\"\n#include \"random\"\n#include \"bitset\"\n#include \"cstdio\"\n#include \"numeric\"\n#include \"cassert\"\n#include \"ctime\"\n\nusing namespace std;\n\n//constexpr long long int MOD = 1000000007;\n//constexpr int MOD = 1000000007;\n//constexpr int MOD = 998244353;\nconstexpr long long int MOD = 998244353;\nconstexpr double EPS = 1e-12;\n\n//int N, M, K, T, H, W, L, R;\nlong long int N, M, K, T, H, W, L, R;\n\n\nint main() {\n\tios::sync_with_stdio(false);\n\tcin.tie(0);\n\n\twhile (cin >> N >> L >> M >> K, N) {\n\t\tvector<int>v(N + 1);\n\t\tfor (int i = 1; i <= N; i++) {\n\t\t\tv[i] = (L / 7) % 10;\n\t\t\tif (L & 1)L = (L / 2) ^ M;\n\t\t\telse L >>= 1;\n\t\t}\n\t\tif (K == 2 || K == 5) {\n\t\t\tlong long int ans = 0;\n\t\t\tint num = 0;\n\t\t\tfor (int i = 1; i <= N; i++) {\n\t\t\t//\tcout << v[i];\n\t\t\t\tif (v[i]) {\n\t\t\t\t\tnum++;\n\t\t\t\t\tif (v[i] % K == 0) {\n\t\t\t\t\t\tans += num;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\telse {\n\t\t\t\t\tans += num;\n\t\t\t\t}\n\t\t\t}\n\t\t\tcout << ans << endl;\n\t\t\tcontinue;\n\t\t}\n\t\tmap<int, int>mp;\n\t\tlong long int ans = 0;\n\t\tmp[0]++;\n\t\tlong long int num = 0;\n\t\tlong long int by = 1;\n\t\tfor (int i = N; i > 0; i--) {\n\t\t\tnum += by * v[i];\n\t\t\tnum %= K;\n\t\t\tif (v[i]) {\n\t\t\t\tans += mp[num];\n\t\t\t}\n\t\t\tmp[num]++;\n\t\t\tby *= 10;\n\t\t\tby %= K;\n\t\t}\n\t\tcout << ans << endl;\n\t}\n}", "accuracy": 1, "time_ms": 410, "memory_kb": 7264, "score_of_the_acc": -0.625, "final_rank": 9 } ]
aoj_1302_cpp
Problem H: Twenty Questions Consider a closed world and a set of features that are defined for all the objects in the world. Each feature can be answered with "yes" or "no". Using those features, we can identify any object from the rest of the objects in the world. In other words, each object can be represented as a fixed-length sequence of booleans. Any object is different from other objects by at least one feature. You would like to identify an object from others. For this purpose, you can ask a series of questions to someone who knows what the object is. Every question you can ask is about one of the features. He/she immediately answers each question with "yes" or "no" correctly. You can choose the next question after you get the answer to the previous question. You kindly pay the answerer 100 yen as a tip for each question. Because you don' t have surplus money, it is necessary to minimize the number of questions in the worst case. You don’t know what is the correct answer, but fortunately know all the objects in the world. Therefore, you can plan an optimal strategy before you start questioning. The problem you have to solve is: given a set of boolean-encoded objects, minimize the maximum number of questions by which every object in the set is identifiable. Input The input is a sequence of multiple datasets. Each dataset begins with a line which consists of two integers, m and n : the number of features, and the number of objects, respectively. You can assume 0 < m ≤ 11 and 0 < n ≤ 128. It is followed by n lines, each of which corresponds to an object. Each line includes a binary string of length m which represent the value ("yes" or "no") of features. There are no two identical objects. The end of the input is indicated by a line containing two zeros. There are at most 100 datasets. Output For each dataset, minimize the maximum number of questions by which every object is identifiable and output the result. Sample Input 8 1 11010101 11 4 00111001100 01001101011 01010000011 01100110001 11 16 01000101111 01011000000 01011111001 01101101001 01110010111 01110100111 10000001010 10010001000 10010110100 10100010100 10101010110 10110100010 11001010011 11011001001 11111000111 11111011101 11 12 10000000000 01000000000 00100000000 00010000000 00001000000 00000100000 00000010000 00000001000 00000000100 00000000010 00000000001 00000000000 9 32 001000000 000100000 000010000 000001000 000000100 000000010 000000001 000000000 011000000 010100000 010010000 010001000 010000100 010000010 010000001 010000000 101000000 100100000 100010000 100001000 100000100 100000010 100000001 100000000 111000000 110100000 110010000 110001000 110000100 110000010 110000001 110000000 0 0 Output for the Sample Input 0 2 4 11 9
[ { "submission_id": "aoj_1302_10848269", "code_snippet": "#include<iostream>\n#include<fstream>\n#include<iomanip>\n#include<cstdio>\n#include<cstdlib>\n#include<cstring>\n#include<cmath>\n#include<iomanip>\n#include<algorithm>\n#include<list>\n#include<map>\n#include<set>\n#include<queue>\n#include<deque>\n#include<vector>\n#include<functional>\n#include<string>\n#define INF 1000000007\n#define ll long long\n#define rep(i,k) for(int i=g.start[k];i!=INF;i=g.next[i])\n\nusing namespace std;\nint n,m,p[130];\nint cnt[1<<11][1<<11],dp[1<<11][1<<11];\n\nvoid Init()\n{\n\tstring t;\n\tmemset(cnt,0,sizeof(cnt));\n\tmemset(dp,-1,sizeof(dp));\n\tfor(int i=0;i<n;i++){\n\t\tcin>>t;\n\t\tfor(int j=0;j<t.length();j++)\n\t\t\tp[i]=(p[i]<<1)+t[j]-'0';\n\t}\n\tfor(int i=0;i<(1<<m);i++)\n\t\tfor(int j=0;j<n;j++)\n\t\t\tcnt[i][i&p[j]]++;\n}\nint DP(int mask1,int mask2)\n{\n\tif(dp[mask1][mask2]!=-1)return dp[mask1][mask2];\n\tif(cnt[mask1][mask2]<=1)return dp[mask1][mask2]=0;\n\tdp[mask1][mask2]=INF;\n\tfor(int i=0;i<m;i++)\n\t{\n\t\tif(mask1&(1<<i))continue;\n\t\tint& ans=dp[mask1][mask2];\n\t\tint tmp=mask1^(1<<i);\n\t\tans=min(ans,max(DP(tmp,mask2),DP(tmp,mask2^(1<<i)))+1);\n\t}\n\treturn dp[mask1][mask2];\n}\nint main()\n{\n\tios::sync_with_stdio(false);\n\t\n\twhile(cin>>m>>n&&(n||m))\n\t{\n\t\tInit();\n\t\tcout<<DP(0,0)<<endl;\n\t}\n\t\n\treturn 0;\n}", "accuracy": 1, "time_ms": 70, "memory_kb": 36156, "score_of_the_acc": -0.9968, "final_rank": 17 }, { "submission_id": "aoj_1302_10002395", "code_snippet": "#pragma GCC target(\"avx2\")\n#pragma GCC optimize(\"O3\")\n#pragma GCC optimize(\"unroll-loops\")\n\n#include <bits/stdc++.h>\nusing namespace std;\n#define all(v) (v).begin(),(v).end()\n#define pb(a) push_back(a)\n#define rep(i, n) for(int i=0;i<n;i++)\n#define foa(e, v) for(auto&& e : v)\nusing ll = long long;\nconst ll MOD7 = 1000000007, MOD998 = 998244353, INF = (1LL << 60);\n#define dout(a) cout<<fixed<<setprecision(10)<<a<<endl;\n\nint main() {\n cin.tie(0);\n ios::sync_with_stdio(false);\n int m, n;\n cin >> m >> n;\n while(m) {\n vector<string> t(n);\n rep(i, n) cin >> t[i];\n vector<string> s(m, string(n, '0'));\n rep(i, n) rep(j, m) s[j][i] = t[i][j];\n map<pair<vector<int>, int>, int> mp;\n auto dfs = [&](auto &&dfs, vector<int> v, int bit) -> int {\n if((int)v.size() <= 1) return 0;\n if(mp.count({v, bit})) return mp[{v, bit}];\n int ans = m;\n \n for(int i = 0; i < m; i ++) {\n if(bit >> i & 1) continue;\n vector<int> nx1, nx2;\n foa(j, v) {\n if(s[i][j] == '1') nx1.pb(j);\n else nx2.pb(j);\n }\n if(nx1.empty() or nx2.empty()) continue;\n ans = min(ans, max(dfs(dfs, nx1, bit | (1 << i)), dfs(dfs, nx2, bit | (1 << i))) + 1);\n }\n \n return mp[{v, bit}] = ans;\n };\n\n vector<int> base(n);\n iota(all(base), 0);\n cout << dfs(dfs, base, 0) << endl;\n cin >> m >> n;\n }\n return 0;\n}", "accuracy": 1, "time_ms": 600, "memory_kb": 9224, "score_of_the_acc": -0.5596, "final_rank": 7 }, { "submission_id": "aoj_1302_9729469", "code_snippet": "#include <bits/stdc++.h>\n\nusing namespace std;\n\n#define rep(i, a, b) for (int i = (int)(a); i < (int)(b); i++)\n#define rrep(i, a, b) for (int i = (int)(b)-1; i >= (int)(a); i--)\n#define ALL(v) (v).begin(), (v).end()\n#define UNIQUE(v) sort(ALL(v)), (v).erase(unique(ALL(v)), (v).end())\n#define SZ(v) (int)v.size()\n#define MIN(v) *min_element(ALL(v))\n#define MAX(v) *max_element(ALL(v))\n#define LB(v, x) int(lower_bound(ALL(v), (x)) - (v).begin())\n#define UB(v, x) int(upper_bound(ALL(v), (x)) - (v).begin())\n\nusing uint = unsigned int;\nusing ll = long long int;\nusing ull = unsigned long long;\nusing i128 = __int128_t;\nusing u128 = __uint128_t;\nconst int inf = 0x3fffffff;\nconst ll INF = 0x1fffffffffffffff;\n\ntemplate <typename T> inline bool chmax(T &a, T b) {\n if (a < b) {\n a = b;\n return 1;\n }\n return 0;\n}\ntemplate <typename T> inline bool chmin(T &a, T b) {\n if (a > b) {\n a = b;\n return 1;\n }\n return 0;\n}\ntemplate <typename T, typename U> T ceil(T x, U y) {\n assert(y != 0);\n if (y < 0)\n x = -x, y = -y;\n return (x > 0 ? (x + y - 1) / y : x / y);\n}\ntemplate <typename T, typename U> T floor(T x, U y) {\n assert(y != 0);\n if (y < 0)\n x = -x, y = -y;\n return (x > 0 ? x / y : (x - y + 1) / y);\n}\ntemplate <typename T> int popcnt(T x) {\n return __builtin_popcountll(x);\n}\ntemplate <typename T> int topbit(T x) {\n return (x == 0 ? -1 : 63 - __builtin_clzll(x));\n}\ntemplate <typename T> int lowbit(T x) {\n return (x == 0 ? -1 : __builtin_ctzll(x));\n}\n\ntemplate <class T, class U>\nostream &operator<<(ostream &os, const pair<T, U> &p) {\n os << \"P(\" << p.first << \", \" << p.second << \")\";\n return os;\n}\ntemplate <typename T> ostream &operator<<(ostream &os, const vector<T> &vec) {\n os << \"{\";\n for (int i = 0; i < vec.size(); i++) {\n os << vec[i] << (i + 1 == vec.size() ? \"\" : \", \");\n }\n os << \"}\";\n return os;\n}\ntemplate <typename T, typename U>\nostream &operator<<(ostream &os, const map<T, U> &map_var) {\n os << \"{\";\n for (auto itr = map_var.begin(); itr != map_var.end(); itr++) {\n os << \"(\" << itr->first << \", \" << itr->second << \")\";\n itr++;\n if (itr != map_var.end())\n os << \", \";\n itr--;\n }\n os << \"}\";\n return os;\n}\ntemplate <typename T> ostream &operator<<(ostream &os, const set<T> &set_var) {\n os << \"{\";\n for (auto itr = set_var.begin(); itr != set_var.end(); itr++) {\n os << *itr;\n ++itr;\n if (itr != set_var.end())\n os << \", \";\n itr--;\n }\n os << \"}\";\n return os;\n}\n#ifdef LOCAL\n#define show(...) _show(0, #__VA_ARGS__, __VA_ARGS__)\n#else\n#define show(...) true\n#endif\ntemplate <typename T> void _show(int i, T name) {\n cerr << '\\n';\n}\ntemplate <typename T1, typename T2, typename... T3>\nvoid _show(int i, const T1 &a, const T2 &b, const T3 &...c) {\n for (; a[i] != ',' && a[i] != '\\0'; i++)\n cerr << a[i];\n cerr << \":\" << b << \" \";\n _show(i + 1, a, c...);\n}\n\n/**\n * @brief template\n */\n\nint main() {\nwhile(1) {\n int N, M;\n cin >> N >> M;\n if (N == 0) return 0;\n vector<vector<int>> A(M,vector<int>(N));\n rep(i,0,M) {\n rep(j,0,N) {\n char C;\n cin >> C;\n A[i][j] = C - '0';\n }\n }\n vector<int> DP((int)pow(3,N),inf);\n rep(i,0,(int)pow(3,N)) {\n vector<int> V(N);\n int I = i;\n rep(j,0,N) {\n V[j] = I % 3;\n I /= 3;\n }\n vector<bool> B(M,true);\n rep(j,0,N) {\n if (V[j] == 2) continue;\n rep(k,0,M) {\n if (A[k][j] != V[j]) B[k] = false;\n }\n }\n int COUNT = 0;\n rep(j,0,M) if (B[j]) COUNT++;\n if (COUNT <= 1) {\n DP[i] = 0;\n continue;\n }\n rep(j,0,N) {\n if (V[j] == 2) {\n int MAX = 0;\n rep(k,0,2) {\n V[j] = k;\n int X = 0;\n rrep(l,0,N) {\n X *= 3;\n X += V[l];\n }\n chmax(MAX,DP[X]);\n V[j] = 2;\n }\n chmin(DP[i],MAX+1);\n }\n }\n }\n cout << DP[pow(3,N)-1] << endl;\n}\n}", "accuracy": 1, "time_ms": 1450, "memory_kb": 4452, "score_of_the_acc": -1.03, "final_rank": 19 }, { "submission_id": "aoj_1302_8992741", "code_snippet": "#line 2 \"cp-library/src/cp-template.hpp\"\n#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\nusing ld = long double;\nusing uint = unsigned int;\nusing ull = unsigned long long;\nusing i32 = int;\nusing u32 = unsigned int;\nusing i64 = long long;\nusing u64 = unsigned long long;\nusing i128 = __int128_t;\ntemplate < class T > bool chmin(T& a, T b) { if(a > b) { a = b; return true; } return false; }\ntemplate < class T > bool chmax(T& a, T b) { if(a < b) { a = b; return true; } return false; }\ntemplate < class T, class U > T ceil (T x, U y) { return (x > 0 ? (x + y - 1) / y : x / y); }\ntemplate < class T, class U > T floor(T x, U y) { return (x > 0 ? x / y : (x - y + 1) / y); }\nint popcnt(i32 x) { return __builtin_popcount(x); }\nint popcnt(u32 x) { return __builtin_popcount(x); }\nint popcnt(i64 x) { return __builtin_popcountll(x); }\nint popcnt(u64 x) { return __builtin_popcountll(x); }\n\n#line 2 \"cp-library/src/utility/rep_itr.hpp\"\ntemplate < class T > struct itr_rep {\n T i, d;\n constexpr itr_rep(const T i) noexcept : i(i), d(1) {}\n constexpr itr_rep(const T i, const T d) noexcept : i(i), d(d) {}\n void operator++() noexcept { i += d; }\n constexpr int operator*() const noexcept { return i; }\n constexpr bool operator!=(const itr_rep x) const noexcept { return d > 0 ? i < x.i : i > x.i; }\n};\n\ntemplate < class T > struct rep {\n const itr_rep< T > s, t;\n constexpr rep(const T t) noexcept : s(0), t(t) {}\n constexpr rep(const T s, const T t) noexcept : s(s), t(t) {}\n constexpr rep(const T s, const T t, const T d) noexcept : s(s, d), t(t, d) {}\n constexpr auto begin() const noexcept { return s; }\n constexpr auto end () const noexcept { return t; }\n};\n\ntemplate < class T > struct revrep {\n const itr_rep < T > s, t;\n constexpr revrep(const T t) noexcept : s(t - 1, -1), t(-1, -1) {}\n constexpr revrep(const T s, const T t) noexcept : s(t - 1, -1), t(s - 1, -1) {}\n constexpr revrep(const T s, const T t, const T d) noexcept : s(t - 1, -d), t(s - 1, -d) {}\n constexpr auto begin() const noexcept { return s; }\n constexpr auto end () const noexcept { return t; }\n};\n#line 3 \"cp-library/src/utility/io.hpp\"\n\n/* 128bit integer */\nistream& operator>>(istream& is, i128& x) {\n std::string s; is >> s;\n int pm = (s[0] == '-');\n x = 0;\n for(int i : rep(pm, int(s.size()))) x = x * 10 + (s[i] - '0');\n if(pm) x *= -1;\n return is;\n}\nostream& operator<<(ostream& os, const i128& x) {\n if(x == 0) return os << '0';\n i128 y = x;\n if(y < 0) { os << '-'; y *= -1; }\n std::vector<int> ny;\n while(y > 0) { ny.push_back(y % 10); y /= 10; }\n for(int i : revrep(ny.size())) os << ny[i];\n return os;\n}\n\ntemplate < class S, class T > istream& operator>>(istream& is, std::pair< S, T >& x) { is >> x.first >> x.second; return is; }\ntemplate < class S, class T > ostream& operator<<(ostream& os, const std::pair< S, T >& x) { os << x.first << \" \" << x.second; return os; }\n\nnamespace scanner {\n struct sca {\n template < class T > operator T() {\n T s; std::cin >> s; return s;\n }\n };\n struct vec {\n int n;\n vec(int n) : n(n) {}\n template < class T > operator std::vector< T >() {\n std::vector< T > v(n);\n for(T& x : v) std::cin >> x;\n return v;\n }\n };\n struct mat {\n int h, w;\n mat(int h, int w) : h(h), w(w) {}\n template < class T > operator std::vector< std::vector< T > >() {\n std::vector m(h, std::vector< T >(w));\n for(std::vector< T >& v : m) for(T& x : v) std::cin >> x;\n return m;\n }\n };\n struct speedup {\n speedup() {\n std::cin.tie(0);\n std::ios::sync_with_stdio(0);\n }\n } speedup_instance;\n}\nscanner::sca in() { return scanner::sca(); }\nscanner::vec in(int n) { return scanner::vec(n); }\nscanner::mat in(int h, int w) { return scanner::mat(h, w); }\n\nnamespace printer {\n void precision(int d) { std::cout << std::fixed << std::setprecision(d); }\n void flush() { std::cout.flush(); }\n}\n\ntemplate < class T >\nostream& operator<<(ostream& os, const std::vector< T > a) {\n int n = a.size();\n for(int i : rep(n)) { os << a[i]; if(i != n - 1) os << ' '; }\n return os;\n}\n\nint print() { std::cout << '\\n'; return 0; }\ntemplate < class head, class... tail > int print(head&& h, tail&&... t) {\n std::cout << h; if(sizeof...(tail)) std::cout << ' ';\n return print(std::forward<tail>(t)...);\n}\ntemplate < class T > int print_n(const std::vector< T > a) {\n int n = a.size();\n for(int i : rep(n)) std::cout << a[i] << \"\\n\";\n return 0;\n}\n\n\n#line 2 \"cp-library/src/utility/key_val.hpp\"\n\ntemplate < class K, class V >\nstruct key_val {\n K key; V val;\n key_val() {}\n key_val(K key, V val) : key(key), val(val) {}\n template < std::size_t Index >\n std::tuple_element_t< Index, key_val >& get() {\n if constexpr (Index == 0) return key;\n if constexpr (Index == 1) return val;\n }\n};\n\nnamespace std {\n\ntemplate < class K, class V > struct tuple_size < key_val< K, V > > : integral_constant< size_t, 2 > {};\ntemplate < class K, class V > struct tuple_element < 0, key_val< K, V > > { using type = K; };\ntemplate < class K, class V > struct tuple_element < 1, key_val< K, V > > { using type = V; };\n\n}\n#line 2 \"cp-library/src/utility/vec_op.hpp\"\ntemplate < class T > key_val< int, T > max_of(const vector< T >& a) {\n int i = std::max_element(a.begin(), a.end()) - a.begin();\n return {i, a[i]};\n}\ntemplate < class T > key_val< int, T > min_of(const vector< T >& a) {\n int i = std::min_element(a.begin(), a.end()) - a.begin();\n return {i, a[i]};\n}\ntemplate < class S, class T > S sum_of(const vector< T >& a) {\n S sum = 0;\n for(const T x : a) sum += x;\n return sum;\n}\ntemplate < class S, class T > vector< S > freq_of(const vector< T >& a, T L, T R) {\n vector< S > res(R - L, S(0));\n for(const T x : a) res[x - L] += 1;\n return res;\n}\ntemplate < class S, class T > struct prefix_sum {\n vector< S > s;\n prefix_sum(const vector< T >& a) : s(a) {\n s.insert(s.begin(), S(0));\n for(int i : rep(a.size())) s[i + 1] += s[i];\n }\n // [L, R)\n S sum(int L, int R) { return s[R] - s[L]; }\n};\n#line 3 \"cp-library/src/utility/heap.hpp\"\n\ntemplate < class T > using heap_min = std::priority_queue< T, std::vector< T >, std::greater< T > >;\ntemplate < class T > using heap_max = std::priority_queue< T, std::vector< T >, std::less< T > >;\n\n#line 27 \"cp-library/src/cp-template.hpp\"\n\n#line 1 \"cp-library/src/algorithm/bin_search.hpp\"\ntemplate < class T, class F >\nT bin_search(T ok, T ng, F f) {\n while(abs(ng - ok) > 1) {\n T mid = (ok + ng) / 2;\n (f(mid) ? ok : ng) = mid;\n }\n return ok;\n}\n\ntemplate < class T, class F >\nT bin_search_real(T ok, T ng, F f, int step = 80) {\n while(step--) {\n T mid = (ok + ng) / 2;\n (f(mid) ? ok : ng) = mid;\n }\n return ok;\n}\n#line 2 \"cp-library/src/algorithm/argsort.hpp\"\n\ntemplate < class T > std::vector< int > argsort(const std::vector< T > &a) {\n std::vector< int > ids((int)a.size());\n std::iota(ids.begin(), ids.end(), 0);\n std::sort(ids.begin(), ids.end(), [&](int i, int j) {\n return a[i] < a[j] || (a[i] == a[j] && i < j);\n });\n return ids;\n}\n#line 1 \"macro.hpp\"\nnamespace macro {\n\nusing size_type = int;\ntemplate < class container > void sort(container& a) { std::sort(std:: begin(a), std:: end(a)); }\ntemplate < class container > void rsort(container& a) { std::sort(std::rbegin(a), std::rend(a)); }\ntemplate < class container > void reverse(container& a) { std::reverse(std::begin(a), std::end(a)); }\ntemplate < class container > void unique(container& a) {\n std::sort(std::begin(a), std::end(a));\n a.erase(std::unique(std::begin(a), std::end(a)), std::end(a));\n}\ntemplate < class container > container sorted(const container& a) { container b = a; sort(b); return std::move(b); }\ntemplate < class container > container rsorted(const container& a) { container b = a; rsort(b); return std::move(b); }\ntemplate < class container, class compare > void sort(container& a, const compare& cmp) { std::sort(std::begin(a), std::end(a), cmp); }\ntemplate < class container, class compare > container sorted(const container& a, const compare& cmp) { container b = a; sort(b, cmp); return std::move(b); }\ntemplate < class container, class value > size_type lower_bound(const container& a, const value& x) { return std::lower_bound(std::begin(a), std::end(a), x) - std::begin(a); }\ntemplate < class container, class value > size_type upper_bound(const container& a, const value& x) { return std::upper_bound(std::begin(a), std::end(a), x) - std::begin(a); }\n\nconst std::vector<std::pair<size_type, size_type>> dir4 = { {+1, 0}, {-1, 0}, { 0, +1}, { 0, -1} };\nconst std::vector<std::pair<size_type, size_type>> dir8 = { {-1, -1}, {-1, 0}, {-1, +1}, { 0, -1}, { 0, +1}, {+1, -1}, {+1, 0}, {+1, +1} };\n\n#ifdef _DEBUG\n#define debug(x) std::cout << \"[\" << __LINE__ << \"] \" << #x << \": \" << x << std::endl\n#else\n#define debug(x)\n#endif\n\ntemplate < class container > void concat(container& a, const container& b) {\n a.insert(std::end(a), std::begin(b), std::end(b));\n}\nstd::vector<size_type> iota(const size_type n) {\n std::vector<size_type> I(n);\n std::iota(std::begin(I), std::end(I), 0);\n return I;\n}\ntemplate < class container > std::vector<size_type> sort_idx(const container& a) {\n const size_type n = a.size();\n std::vector<size_type> I = iota(n);\n std::sort(std::begin(I), std::end(I), [&](size_type i, size_type j) { return a[i] < a[j] or (a[i] == a[j] and i < j); });\n return I;\n}\ntemplate < class container, class compare > std::vector<size_type> sort_idx(const container& a, const compare& cmp) {\n const size_type n = a.size();\n std::vector<size_type> I = iota(n);\n std::sort(std::begin(I), std::end(I), [&](size_type i, size_type j) { return cmp(a[i], a[j]) or (a[i] == a[j] and i < j); });\n return std::move(I);\n}\n\nstruct grid {\n using size_type = int;\n size_type H, W;\n grid(const size_type H, const size_type W) : H(H), W(W) {}\n bool contains(const size_type i, const size_type j) {\n return 0 <= i and i < H and 0 <= j and j < W;\n }\n};\n\n} // namespace macro\n\nusing namespace macro;\n#line 3 \"A.cpp\"\n\nint solve(int m, int n) {\n vector<string> s = in(n);\n\n map<vector<int>, int> memo;\n vector<int> used(m, 0);\n auto dfs = [&](auto self, vector<int> a) -> int {\n if(a.size() <= 1) return 0;\n if(memo.count(a)) return memo[a];\n\n int ans = 1e9;\n for(int i : rep(m)) if(not used[i]) {\n vector<int> a0, a1;\n for(int id : a) {\n (s[id][i] == '0' ? a0 : a1).push_back(id);\n }\n if(a0.size() == 0 or a1.size() == 0) continue;\n used[i] = 1;\n chmin(ans, 1 + max(self(self, a0), self(self, a1)));\n used[i] = 0;\n }\n return memo[a] = ans;\n };\n\n return dfs(dfs, iota(n));\n}\n\nint main() {\n while(true) {\n int m = in(), n = in();\n if(make_pair(m, n) == make_pair(0, 0)) return 0;\n print(solve(m, n));\n }\n}", "accuracy": 1, "time_ms": 150, "memory_kb": 5500, "score_of_the_acc": -0.1199, "final_rank": 3 }, { "submission_id": "aoj_1302_8820636", "code_snippet": "#include <iostream>\n#include <map>\n#include <string>\n#include <vector>\nusing namespace std;\n\nmap<pair<vector<int>, int>, int> mp;\nstring qes[128];\n\nint search(vector<int> &vi, int depth, int used, int m, int n) {\n pair<vector<int>, int> p = make_pair(vi, used);\n if (mp.count(p))\n return mp[p];\n if (vi.size() > (1 << (m - depth)))\n return 100;\n if (vi.size() == 1)\n return depth;\n int res = 100;\n vector<int> a, b;\n for (int i = 0; i < m; i++) {\n if ((used >> i) & 1)\n continue;\n a.clear(); b.clear();\n for (int j = 0; j < vi.size(); j++) {\n if (qes[vi[j]][i] == '0')\n a.push_back(vi[j]);\n else\n b.push_back(vi[j]);\n }\n if (a.empty() || b.empty())\n continue;\n res = min(res, max(search(a, depth + 1, used | (1 << i), m, n),\n search(b, depth + 1, used | (1 << i), m, n)));\n }\n return mp[p] = res;\n}\n\nint main() {\n int m, n;\n while (cin >> m >> n, m) {\n for (int i = 0; i < n; i++)\n cin >> qes[i];\n vector<int> vi(n);\n for (int i = 0; i < n; i++)\n vi[i] = i;\n mp.clear();\n cout << search(vi, 0, 0, m, n) << endl;\n }\n}", "accuracy": 1, "time_ms": 780, "memory_kb": 8648, "score_of_the_acc": -0.6725, "final_rank": 10 }, { "submission_id": "aoj_1302_8808449", "code_snippet": "#include <iostream>\n#include <map>\n#include <string>\n#include <vector>\nusing namespace std;\n\nmap<pair<vector<int>, int>, int> mp;\nstring qes[128];\n\nint search(vector<int> &vi, int depth, int used, int m, int n) {\n if (mp.count(make_pair(vi, used)))\n return mp[make_pair(vi, used)];\n\n if (vi.size() > (1 << (m - depth)))\n return 100;\n\n if (vi.size() == 1)\n return depth;\n\n int res = 100;\n\n vector<int> a(n), b(n); // Initialize a and b with the maximum possible size\n\n for (int i = 0; i < m; i++) {\n if ((used >> i) & 1)\n continue;\n\n a.clear();\n b.clear();\n\n for (int j = 0; j < vi.size(); j++) {\n if (qes[vi[j]][i] == '0')\n a.push_back(vi[j]);\n else\n b.push_back(vi[j]);\n }\n\n if (a.empty() || b.empty())\n continue;\n\n res = min(res, max(search(a, depth + 1, used | (1 << i), m, n),\n search(b, depth + 1, used | (1 << i), m, n)));\n }\n\n mp[make_pair(vi, used)] = res; // Store the result in mp\n\n return res;\n}\n\nint main() {\n int m, n;\n\n while (cin >> m >> n, m) {\n for (int i = 0; i < n; i++)\n cin >> qes[i];\n\n vector<int> vi(n);\n\n for (int i = 0; i < n; i++)\n vi[i] = i;\n\n mp.clear();\n\n cout << search(vi, 0, 0, m, n) << endl;\n }\n}", "accuracy": 1, "time_ms": 830, "memory_kb": 9064, "score_of_the_acc": -0.7214, "final_rank": 11 }, { "submission_id": "aoj_1302_8808444", "code_snippet": "#include <iostream>\n#include <map>\n#include <string>\n#include <vector>\nusing namespace std;\n\nmap<pair<vector<int>, int>, int> mp;\nstring qes[128];\n\nint search(vector<int> &vi, int depth, int used, int m, int n) {\n if (mp.count(make_pair(vi, used)))\n return mp[make_pair(vi, used)];\n\n if (vi.size() > (1 << (m - depth)))\n return 100;\n\n if (vi.size() == 1)\n return depth;\n\n int res = 100;\n for (int i = 0; i < m; i++) {\n if ((used >> i) & 1)\n continue;\n\n vector<int> a, b;\n for (int j = 0; j < vi.size(); j++) {\n if (qes[vi[j]][i] == '0')\n a.push_back(vi[j]);\n else\n b.push_back(vi[j]);\n }\n\n if (a.empty() || b.empty())\n continue;\n\n res = min(res, max(search(a, depth + 1, used | (1 << i), m, n),\n search(b, depth + 1, used | (1 << i), m, n)));\n }\n\n return mp[make_pair(vi, used)] = res;\n}\n\nint main() {\n int m, n;\n while (cin >> m >> n, m) {\n for (int i = 0; i < n; i++)\n cin >> qes[i];\n\n vector<int> vi(n);\n for (int i = 0; i < n; i++)\n vi[i] = i;\n\n mp.clear();\n cout << search(vi, 0, 0, m, n) << endl;\n }\n}", "accuracy": 1, "time_ms": 920, "memory_kb": 8696, "score_of_the_acc": -0.7754, "final_rank": 15 }, { "submission_id": "aoj_1302_8496704", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\ntypedef vector<int> Vi;\nconst int INF = 1<<29;\nint m, n, obj[130];\nmap<Vi, int> mp;\nint dfs(Vi vi){\n if(vi.size()<2) return 0;\n if(mp.count(vi)>0) return mp[vi];\n int res = INF;\n for(int i=0;i<m;i++){\n Vi v0, v1;\n for(int j=0;j<vi.size();j++){\n int num = vi[j];\n if(obj[num]&1<<i) v1.push_back(num);\n else v0.push_back(num);\n }\n if(v0.size()<1 || v1.size()<1) continue;\n res = min(res, max(dfs(v0), dfs(v1))+1);\n }\n return mp[vi] = res;\n}\nint main(){\n string s;\n while(cin >> m >> n, m){\n Vi vi;\n for(int i=0;i<n;i++){\n cin >> s;\n int ob = 0;\n for(int j=0;j<m;j++){\n if(s[j]=='1') ob |= 1<<j;\n }\n obj[i] = ob;\n vi.push_back(i);\n }\n cout << dfs(vi) << endl;\n mp.clear();\n }\n return(0);\n}", "accuracy": 1, "time_ms": 260, "memory_kb": 5392, "score_of_the_acc": -0.1964, "final_rank": 4 }, { "submission_id": "aoj_1302_8321504", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\nint main() {\n\twhile (true) {\n\t\tint M, N;\n\t\tcin >> M >> N;\n\t\tif (M == 0 && N == 0) {\n\t\t\tbreak;\n\t\t}\n\t\tvector<string> S(N);\n\t\tfor (int i = 0; i < N; i++) {\n\t\t\tcin >> S[i];\n\t\t}\n\t\tvector<int> power(M + 1);\n\t\tpower[0] = 1;\n\t\tfor (int i = 1; i <= M; i++) {\n\t\t\tpower[i] = power[i - 1] * 3;\n\t\t}\n\t\tvector<int> cnt(power[M]);\n\t\tfor (int i = 0; i < N; i++) {\n\t\t\tfor (int j = 0; j < (1 << M); j++) {\n\t\t\t\tint code = 0;\n\t\t\t\tfor (int k = 0; k < M; k++) {\n\t\t\t\t\tcode += ((j >> k) & 1 ? 2 : int(S[i][k] - '0')) * power[k];\n\t\t\t\t}\n\t\t\t\tcnt[code] += 1;\n\t\t\t}\n\t\t}\n\t\tconst int INF = 1012345678;\n\t\tvector<int> dp(power[M]);\n\t\tfor (int i = 0; i < power[M]; i++) {\n\t\t\tif (cnt[i] <= 1) {\n\t\t\t\tdp[i] = 0;\n\t\t\t}\n\t\t\telse {\n\t\t\t\tdp[i] = INF;\n\t\t\t\tfor (int j = 0; j < M; j++) {\n\t\t\t\t\tint z = i / power[j] % 3;\n\t\t\t\t\tif (z == 2) {\n\t\t\t\t\t\tint resl = dp[i - 2 * power[j]];\n\t\t\t\t\t\tint resr = dp[i - 1 * power[j]];\n\t\t\t\t\t\tdp[i] = min(dp[i], max(resl, resr) + 1);\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\tcout << dp[power[M] - 1] << endl;\n\t}\n\treturn 0;\n}", "accuracy": 1, "time_ms": 80, "memory_kb": 4324, "score_of_the_acc": -0.0334, "final_rank": 1 }, { "submission_id": "aoj_1302_8302614", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\ntemplate<class T>\nbool chmin(T &a, const T &b) {if (a > b) {a = b; return 1;} return 0;}\n\nbool is_end = false;\n\nconst int INF = 1e9;\nint dp[1<<11][1<<11];\n\nvoid init()\n{\n\tfor (int i = 0; i < (1<<11); ++i)\n\t{\n\t\tfor (int j = 0; j < (1<<11); ++j)\n\t\t{\n\t\t\tdp[i][j] = INF;\n\t\t}\n\t}\n\treturn;\n}\n\nvoid calc()\n{\n\tint M, N; cin >> M >> N;\n\t\n\tif (M == 0)\n\t{\n\t\tis_end = true;\n\t\treturn;\n\t}\n\t\n\tvector<int> quest(N);\n\tfor (int i = 0; i < N; ++i)\n\t{\n\t\tstring S; cin >> S;\n\t\tbitset<11> bs(S);\n\t\tquest[i] = bs.to_ullong();\n\t}\n\t\n\tinit();\n\t\n\tfor (int bit = (1<<M)-1; bit >= 0; --bit)\n\t{\n\t\tfor (int sbit = bit; sbit >= 0; sbit = bit & (sbit-1))\n\t\t{\n\t\t\tint sum = 0;\n\t\t\tfor (int i = 0; i < N; ++i)\n\t\t\t{\n\t\t\t\tif ((quest[i] & bit) == sbit) {sum += 1;}\n\t\t\t}\n\t\t\t\n\t\t\tif (sum <= 1)\n\t\t\t{\n\t\t\t\tdp[bit][sbit] = 0;\n\t\t\t}\n\t\t\telse\n\t\t\t{\n\t\t\t\tfor (int i = 0; i < M; ++i)\n\t\t\t\t{\n\t\t\t\t\tif ((bit & (1<<i))) {continue;}\n\t\t\t\t\tint nbit = 1<<i;\n\t\t\t\t\tchmin(dp[bit][sbit], 1 + max(dp[bit^nbit][sbit], dp[bit^nbit][sbit^nbit]));\n\t\t\t\t}\n\t\t\t}\n\t\t\t\n\t\t\tif (sbit == 0) {break;}\n\t\t}\n\t}\n\t\n\tint res = dp[0][0];\n\tcout << res << endl;\n\t\n\treturn;\n}\n\nint main()\n{\n\twhile (!is_end)\n\t{\n\t\tcalc();\n\t}\n\t\n\treturn 0;\n}", "accuracy": 1, "time_ms": 130, "memory_kb": 19580, "score_of_the_acc": -0.5348, "final_rank": 6 }, { "submission_id": "aoj_1302_8215672", "code_snippet": "#include <fstream>\n#include <iostream>\n#include <map>\n#include <string>\n#include <vector>\nusing namespace std;\nmap<pair<vector<int>, int>, int> mp;\nstring qes[128];\nint search(vector<int> &vi, int depth, int used, int m, int n) {\n if (mp.count(make_pair(vi, used)))\n return mp[make_pair(vi, used)];\n if (vi.size() > (1 << (m - depth)))\n return 100;\n if (vi.size() == 1)\n return depth;\n int res = 100;\n for (int i = 0; i < m; i++) {\n if ((used >> i) & 1)\n continue;\n vector<int> a, b;\n for (int j = 0; j < vi.size(); j++) {\n if (qes[vi[j]][i] == '0')\n a.push_back(vi[j]);\n else\n b.push_back(vi[j]);\n }\n if (a.empty() || b.empty())\n continue;\n res = min(res, max(search(a, depth + 1, used | (1 << i), m, n),\n search(b, depth + 1, used | (1 << i), m, n)));\n }\n return mp[make_pair(vi, used)] = res;\n}\nint main() {\n int m, n;\n while (cin >> m >> n, m) {\n for (int i = 0; i < n; i++)\n cin >> qes[i];\n vector<int> vi(n);\n for (int i = 0; i < n; i++)\n vi[i] = i;\n mp.clear();\n cout << search(vi, 0, 0, m, n) << endl;\n }\n}", "accuracy": 1, "time_ms": 920, "memory_kb": 8572, "score_of_the_acc": -0.7716, "final_rank": 13 }, { "submission_id": "aoj_1302_8215669", "code_snippet": "#include <iostream>\n#include <map>\n#include <string>\n#include <vector>\nusing namespace std;\nmap<pair<vector<int>, int>, int> mp;\nstring qes[128];\nint search(vector<int> &vi, int depth, int used, int m, int n) {\n if (mp.count(make_pair(vi, used)))\n return mp[make_pair(vi, used)];\n if (vi.size() > (1 << (m - depth)))\n return 100;\n if (vi.size() == 1)\n return depth;\n int res = 100;\n for (int i = 0; i < m; i++) {\n if ((used >> i) & 1)\n continue;\n vector<int> a, b;\n for (int j = 0; j < vi.size(); j++) {\n if (qes[vi[j]][i] == '0')\n a.push_back(vi[j]);\n else\n b.push_back(vi[j]);\n }\n if (a.empty() || b.empty())\n continue;\n res = min(res, max(search(a, depth + 1, used | (1 << i), m, n),\n search(b, depth + 1, used | (1 << i), m, n)));\n }\n return mp[make_pair(vi, used)] = res;\n}\nint main() {\n int m, n;\n while (cin >> m >> n, m) {\n for (int i = 0; i < n; i++)\n cin >> qes[i];\n vector<int> vi(n);\n for (int i = 0; i < n; i++)\n vi[i] = i;\n mp.clear();\n cout << search(vi, 0, 0, m, n) << endl;\n }\n}", "accuracy": 1, "time_ms": 920, "memory_kb": 8668, "score_of_the_acc": -0.7745, "final_rank": 14 }, { "submission_id": "aoj_1302_8116035", "code_snippet": "#include <iostream>\n#include <map>\n#include <string>\n#include <vector>\nusing namespace std;\nmap<pair<vector<int>, int>, int> mp;\nvector<string> qes(128);\nint search(vector<int> &vi, int depth, int used, int m, int n) {\n if (mp.count({vi, used}))\n return mp[{vi, used}];\n if (vi.size() > (1 << (m - depth)))\n return 100;\n if (vi.size() == 1)\n return depth;\n int res = 100;\n for (int i = 0; i < m; i++) {\n if ((used >> i) & 1)\n continue;\n vector<int> a, b;\n for (int j = 0; j < vi.size(); j++) {\n if (qes[vi[j]][i] == '0')\n a.push_back(vi[j]);\n else\n b.push_back(vi[j]);\n }\n if (a.empty() || b.empty())\n continue;\n res = min(res, max(search(a, depth + 1, used | (1 << i), m, n),\n search(b, depth + 1, used | (1 << i), m, n)));\n }\n return mp[{vi, used}] = res;\n}\nint main() {\n ios_base::sync_with_stdio(false);\n cin.tie(NULL);\n int m, n;\n while (cin >> m >> n, m) {\n for (int i = 0; i < n; i++)\n cin >> qes[i];\n vector<int> vi(n);\n for (int i = 0; i < n; i++)\n vi[i] = i;\n mp.clear();\n cout << search(vi, 0, 0, m, n) << endl;\n }\n}", "accuracy": 1, "time_ms": 870, "memory_kb": 8664, "score_of_the_acc": -0.7382, "final_rank": 12 }, { "submission_id": "aoj_1302_8051593", "code_snippet": "#include <iostream>\n#include <map>\n#include <string>\n#include <vector>\n\nusing namespace std;\n\nmap<pair<vector<int>, int>, int> mp;\nstring qes[128];\n\nint search(const vector<int>& vi, int depth, int used, int m) {\n if (mp.count(make_pair(vi, used)))\n return mp[make_pair(vi, used)];\n if (vi.size() > (1 << (m - depth)))\n return 100;\n if (vi.size() == 1)\n return depth;\n int res = 100;\n for (int i = 0; i < m; i++) {\n if ((used >> i) & 1)\n continue;\n vector<int> a, b;\n for (int j = 0; j < vi.size(); j++) {\n if (qes[vi[j]][i] == '0')\n a.push_back(vi[j]);\n else\n b.push_back(vi[j]);\n }\n if (a.empty() || b.empty())\n continue;\n int a_used = used | (1 << i);\n int res_a, res_b;\n if (mp.count(make_pair(a, a_used))) {\n res_a = mp[make_pair(a, a_used)];\n } else {\n res_a = search(a, depth + 1, a_used, m);\n mp[make_pair(a, a_used)] = res_a;\n }\n int b_used = a_used;\n if (mp.count(make_pair(b, b_used))) {\n res_b = mp[make_pair(b, b_used)];\n } else {\n res_b = search(b, depth + 1, b_used, m);\n mp[make_pair(b, b_used)] = res_b;\n }\n res = min(res, max(res_a, res_b));\n }\n return mp[make_pair(vi, used)] = res;\n}\n\nint main() {\n ios::sync_with_stdio(false);\n cin.tie(nullptr);\n\n int m, n;\n while (cin >> m >> n, m) {\n for (int i = 0; i < n; i++)\n cin >> qes[i];\n vector<int> vi(n);\n for (int i = 0; i < n; i++)\n vi[i] = i;\n mp.clear();\n cout << search(vi, 0, 0, m) << '\\n';\n }\n}", "accuracy": 1, "time_ms": 960, "memory_kb": 14144, "score_of_the_acc": -0.9705, "final_rank": 16 }, { "submission_id": "aoj_1302_8051587", "code_snippet": "#include <iostream>\n#include <map>\n#include <string>\n#include <vector>\nusing namespace std;\nmap<pair<int, int>, int> mp;\nstring qes[128];\nint search(vector<int> &vi, int depth, int used, int m, int n) {\n auto key = make_pair(vi.size(), used);\n if (mp.count(key))\n return mp[key];\n if (vi.size() > (1 << (m - depth)))\n return 100;\n if (vi.size() == 1)\n return depth;\n int res = 100;\n for (int i = 0; i < m; i++) {\n if ((used >> i) & 1)\n continue;\n vector<int> a, b;\n for (int j = 0; j < vi.size(); j++) {\n if (qes[vi[j]][i] == '0')\n a.push_back(vi[j]);\n else\n b.push_back(vi[j]);\n }\n if (a.empty() || b.empty())\n continue;\n res = min(res, max(search(a, depth + 1, used | (1 << i), m, n),\n search(b, depth + 1, used | (1 << i), m, n)));\n }\n return mp[key] = res;\n}\nint main() {\n int m, n;\n while (cin >> m >> n, m) {\n for (int i = 0; i < n; i++)\n cin >> qes[i];\n vector<int> vi(n);\n for (int i = 0; i < n; i++)\n vi[i] = i;\n mp.clear();\n cout << search(vi, 0, 0, m, n) << endl;\n }\n}", "accuracy": 1, "time_ms": 110, "memory_kb": 4044, "score_of_the_acc": -0.0466, "final_rank": 2 }, { "submission_id": "aoj_1302_8051585", "code_snippet": "#include <iostream>\n#include <map>\n#include <string>\n#include <vector>\nusing namespace std;\nmap<pair<vector<int>, int>, int> mp;\nstring qes[128];\nint search(vector<int> &vi, int depth, int used, int m, int n) {\n if (mp.count(make_pair(vi, used)))\n return mp[make_pair(vi, used)];\n if (vi.size() > (1 << (m - depth)))\n return 100;\n if (vi.size() == 1)\n return depth;\n int res = 100;\n for (int i = 0; i < m; i++) {\n if ((used >> i) & 1)\n continue;\n vector<int> a, b;\n for (int j = 0; j < vi.size(); j++) {\n if (qes[vi[j]][i] == '0')\n a.push_back(vi[j]);\n else\n b.push_back(vi[j]);\n }\n if (a.empty() || b.empty())\n continue;\n int a_res = search(a, depth + 1, used | (1 << i), m, n);\n int b_res = search(b, depth + 1, used | (1 << i), m, n);\n res = min(res, max(a_res, b_res));\n }\n return mp[make_pair(vi, used)] = res;\n}\nint main() {\n ios_base::sync_with_stdio(false);\n cin.tie(nullptr);\n cout.tie(nullptr);\n int m, n;\n while (cin >> m >> n, m) {\n for (int i = 0; i < n; i++)\n cin >> qes[i];\n vector<int> vi(n);\n for (int i = 0; i < n; i++)\n vi[i] = i;\n mp.clear();\n cout << search(vi, 0, 0, m, n) << '\\n';\n }\n}", "accuracy": 1, "time_ms": 630, "memory_kb": 9300, "score_of_the_acc": -0.5836, "final_rank": 8 }, { "submission_id": "aoj_1302_7787815", "code_snippet": "#include <stdio.h>\n\n#include <algorithm>\n#include <vector>\nusing namespace std;\nchar str[150][13];\nint dp[180000];\nint pow3[13];\nint m, n;\nint calc(int a) {\n if (~dp[a]) return dp[a];\n vector<int> c;\n int key[13];\n for (int i = 0; i < m; i++) key[i] = a % pow3[i + 1] / pow3[i];\n for (int i = 0; i < n; i++) {\n bool ok = true;\n for (int j = 0; j < m; j++) {\n if (key[j] && str[i][j] - '0' + 1 != key[j]) ok = false;\n }\n if (ok) c.push_back(i);\n }\n if (c.size() <= 1) return dp[a] = 0;\n int ret = 99999999;\n for (int i = 0; i < m; i++) {\n if (key[i]) continue;\n int L = calc(a + pow3[i]);\n int R = calc(a + pow3[i] * 2);\n ret = min(ret, max(L, R) + 1);\n }\n return dp[a] = ret;\n}\nint main() {\n int a, b;\n pow3[0] = 1;\n for (int i = 1; i < 13; i++) pow3[i] = pow3[i - 1] * 3;\n while (scanf(\"%d%d\", &a, &b), a) {\n n = b;\n m = a;\n for (int i = 0; i < b; i++) scanf(\"%s\", str[i]);\n for (int i = 0; i < pow3[a]; i++) dp[i] = -1;\n printf(\"%d\\n\", calc(0));\n }\n}", "accuracy": 1, "time_ms": 930, "memory_kb": 3468, "score_of_the_acc": -0.6232, "final_rank": 9 }, { "submission_id": "aoj_1302_7191475", "code_snippet": "#include <iostream>\n#include <vector>\n#include <string>\n#include <algorithm>\n#include <map>\nusing namespace std;\n\nstruct Solver{\n int n, m;\n vector<int> a;\n map<pair<int, vector<int>>, int> mem;\n Solver(int n, int m): n(n), m(m), a(vector<int>(n)){\n for(int i = 0; i < n; i++){\n string line; cin >> line;\n for(int j = 0; j < m; j++){\n if(line[j] == '1') a[i] |= 1 << j;\n }\n }\n }\n\n int f(int used, vector<int> a){\n if(mem.count({used, a}) == 1) return mem[{used, a}];\n if(a.size() <= 1) return 0;\n if(a.size() == 2) return 1;\n int res = 1 << 30;\n for(int i = 0; i < m; i++){\n if((used >> i) & 1) continue;\n vector<int> u, v;\n for(auto &it: a){\n if((it >> i) & 1) u.emplace_back(it);\n else v.emplace_back(it);\n }\n res = min(res, max(f(used | 1 << i, u), f(used | 1 << i, v))+1);\n }\n mem[{used, a}] = res;\n return res;\n }\n\n void solve(){\n cout << f(0, a) << '\\n';\n }\n};\n\nint main(){\n while(true){\n int m, n;\n cin >> m >> n;\n if(m == 0 && n == 0) break;\n \n Solver sol(n, m);\n sol.solve();\n }\n return 0;\n}", "accuracy": 1, "time_ms": 520, "memory_kb": 6932, "score_of_the_acc": -0.4317, "final_rank": 5 }, { "submission_id": "aoj_1302_6842375", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\ntemplate<class T> inline T read(T&x){\n T data=0;\n int w=1;\n char ch=getchar();\n while(ch!='-'&&!isdigit(ch))\n ch=getchar();\n if(ch=='-')\n w=-1,ch=getchar();\n while(isdigit(ch))\n data=10*data+ch-'0',ch=getchar();\n return x=data*w;\n}\nconst int maxn=150;\nint m,n;\nint feature[maxn],cnt[(1<<11)+10][(1<<11)+10];\nint d[(1<<11)+10][(1<<11)+10];\n\nint dp(int s,int a){\n if(d[s][a]!=-1)return d[s][a];\n if(cnt[s][a]<=1)return d[s][a]=0;\n if(cnt[s][a]==2)return d[s][a]==1;\n int ans=20;\n for(int i=0;i<m;++i)\n if(!(s&(1<<i)))ans=min(ans, max(dp(s|(1<<i),a),dp(s|(1<<i),a|(1<<i)))+1 );\n return d[s][a]=ans;\n}\nint main(){\n while(read(m)&&read(n)){\n memset(feature,0,sizeof(feature));\n char s[20];\n for(int i=1;i<=n;++i){\n scanf(\"%s\",s);\n for(int j=0;j<m;++j)\n feature[i]|=((s[j]-'0')<<j);\n }\n memset(cnt,0,sizeof(cnt));\n for(int i=0;i<(1<<m);++i)\n for(int j=1;j<=n;++j)\n ++cnt[i][i&feature[j]];\n memset(d,-1,sizeof(d));\n printf(\"%d\\n\",!dp(0,0)?0:dp(0,0)+1);\n }\n return 0;\n}", "accuracy": 1, "time_ms": 70, "memory_kb": 36260, "score_of_the_acc": -1, "final_rank": 18 } ]
aoj_1311_cpp
Problem G: Test Case Tweaking You are a judge of a programming contest. You are preparing a dataset for a graph problem to seek for the cost of the minimum cost path. You've generated some random cases, but they are not interesting. You want to produce a dataset whose answer is a desired value such as the number representing this year 2010. So you will tweak (which means 'adjust') the cost of the minimum cost path to a given value by changing the costs of some edges. The number of changes should be made as few as possible. A non-negative integer c and a directed graph G are given. Each edge of G is associated with a cost of a non-negative integer. Given a path from one node of G to another, we can define the cost of the path as the sum of the costs of edges constituting the path. Given a pair of nodes in G , we can associate it with a non-negative cost which is the minimum of the costs of paths connecting them. Given a graph and a pair of nodes in it, you are asked to adjust the costs of edges so that the minimum cost path from one node to the other will be the given target cost c . You can assume that c is smaller than the cost of the minimum cost path between the given nodes in the original graph. For example, in Figure G.1, the minimum cost of the path from node 1 to node 3 in the given graph is 6. In order to adjust this minimum cost to 2, we can change the cost of the edge from node 1 to node 3 to 2. This direct edge becomes the minimum cost path after the change. For another example, in Figure G.2, the minimum cost of the path from node 1 to node 12 in the given graph is 4022. In order to adjust this minimum cost to 2010, we can change the cost of the edge from node 6 to node 12 and one of the six edges in the right half of the graph. There are many possibilities of edge modification, but the minimum number of modified edges is 2. Input The input is a sequence of datasets. Each dataset is formatted as follows. n m c f 1 t 1 c 1 f 2 t 2 c 2 . . . f m t m c m Figure G.1: Example 1 of graph Figure G.2: Example 2 of graph The integers n , m and c are the number of the nodes, the number of the edges, and the target cost, respectively, each separated by a single space, where 2 ≤ n ≤ 100, 1 ≤ m ≤ 1000 and 0 ≤ c ≤ 100000. Each node in the graph is represented by an integer 1 through n . The following m lines represent edges: the integers f i , t i and c i (1 ≤ i ≤ m ) are the originating node, the destination node and the associated cost of the i -th edge, each separated by a single space. They satisfy 1 ≤ f i , t i ≤ n and 0 ≤ c i ≤ 10000. You can assume that f i ≠ t i and ( f i , t i ) ≠ ( f j , t j ) when i ≠ j . You can assume that, for each dataset, there is at least one path from node 1 to node n , and that the cost of the minimum cost path from node 1 to node n of the given graph is greater than c . The end of the input is indicated by a line containing three zeros separated by single spaces. Output For each dataset, output a line containing ...(truncated)
[ { "submission_id": "aoj_1311_10848586", "code_snippet": "/* ***********************************************\nAuthor :kuangbin\nCreated Time :2013/11/15 16:30:39\nFile Name :G.cpp\n************************************************ */\n\n#include <stdio.h>\n#include <string.h>\n#include <iostream>\n#include <algorithm>\n#include <vector>\n#include <queue>\n#include <set>\n#include <map>\n#include <string>\n#include <math.h>\n#include <stdlib.h>\n#include <time.h>\nusing namespace std;\nconst int INF = 0x3f3f3f3f;\nstruct Edge\n{\n\tint to,next;\n\tint w;\n}edge[20010];\nint head[110];\nint tot;\nvoid addedge(int u,int v,int w)\n{\n\tedge[tot].to = v;\n\tedge[tot].next = head[u];\n\tedge[tot].w = w;\n\thead[u] = tot++;\n}\n\nvoid init()\n{\n\ttot = 0;\n\tmemset(head,-1,sizeof(head));\n}\nint dp[110][1010];\nint n,m;\nbool vis[110][1010];\nvoid SPFA(int s)\n{\n\tqueue<pair<int,int> >q;\n\tq.push(make_pair(s,0));\n\tfor(int i = 1;i <= n;i++)\n\t\tfor(int j = 0;j < 1010;j++)\n\t\t\tdp[i][j] = INF;\n\tmemset(vis,false,sizeof(vis));\n\tdp[s][0] = 0;\n\tvis[s][0] = true;\n\twhile(!q.empty())\n\t{\n\t\tpair<int,int> tmp = q.front();\n\t\tint u = tmp.first;\n\t\tint k = tmp.second;\n\t\t//printf(\"%d %d\\n\",u,k);\n\t\tq.pop();\n\t\tvis[u][k] = false;\n\t\tfor(int i = head[u];i != -1;i = edge[i].next)\n\t\t{\n\t\t\tint v = edge[i].to;\n\t\t\t//cout<<v<<endl;\n\t\t//\tprintf(\"%d %d\\n\",dp[v][k],dp[u][k] + edge[i].w);\n\n\t\t\tif(dp[v][k] > dp[u][k] + edge[i].w)\n\t\t\t{\n\t\t\t//\tcout<<\"*\"<<endl;\n\t\t\t\tdp[v][k] = dp[u][k] + edge[i].w;\n\t\t\t\tif(!vis[v][k])\n\t\t\t\t{\n\t\t\t\t\tq.push(make_pair(v,k));\n\t\t\t\t\tvis[v][k] = true;\n\t\t\t\t}\n\t\t\t}\n\n\t\t\tif(k < m && dp[v][k+1] > dp[u][k] )\n\t\t\t{\n\t\t\t\tdp[v][k+1] = dp[u][k] ;\n\t\t\t\tif(!vis[v][k+1])\n\t\t\t\t{\n\t\t\t\t\tq.push(make_pair(v,k+1));\n\t\t\t\t\tvis[v][k+1] = true;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n}\nint main()\n{\n //freopen(\"in.txt\",\"r\",stdin);\n //freopen(\"out.txt\",\"w\",stdout);\n int c;\n\twhile(scanf(\"%d%d%d\",&n,&m,&c) == 3)\n\t{\n\t\tif(n == 0 && m == 0 && c == 0)break;\n\t\tinit();\n\t\tint u,v,w;\n\t\tfor(int i = 0;i < m;i++)\n\t\t{\n\t\t\tscanf(\"%d%d%d\",&u,&v,&w);\n\t\t\taddedge(u,v,w);\n\t\t}\n\t\tSPFA(1);\n\t//\tfor(int i = 1;i <= n;i++)\n\t//\t\tfor(int j = 0;j <= m;j++)\n\t//\t\t\tprintf(\" %d %d %d\\n\",i,j,dp[i][j]);\n\t\tint ans = m ;\n\t\tfor(int i = 0;i <= m;i++)\n\t\t\tif(dp[n][i] <= c)\n\t\t\t{\n\t\t\t\tans = i;\n\t\t\t\tbreak;\n\t\t\t}\n\t\tcout<<ans<<endl;\n\t}\n return 0;\n}", "accuracy": 1, "time_ms": 40, "memory_kb": 3916, "score_of_the_acc": -0.0325, "final_rank": 7 }, { "submission_id": "aoj_1311_10005516", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n#define all(v) (v).begin(),(v).end()\n#define pb(a) push_back(a)\n#define rep(i, n) for(int i=0;i<n;i++)\n#define foa(e, v) for(auto&& e : v)\nusing ll = long long;\nconst ll MOD7 = 1000000007, MOD998 = 998244353, INF = (1LL << 60);\n#define dout(a) cout<<fixed<<setprecision(10)<<a<<endl;\n\nint main() {\n cin.tie(0);\n ios::sync_with_stdio(false);\n\n int n, m, C;\n cin >> n >> m >> C;\n while(n) {\n int inf = 1 << 30;\n vector dis(n, vector(m + 1, inf));\n vector<vector<pair<int, int>>> v(n);\n rep(i, m) {\n int a, b, c; cin >> a >> b >> c;\n a --; b --;\n v[a].push_back({b, c});\n }\n dis[0][0] = 0;\n multiset<tuple<int, int, int>> ms;\n ms.insert({0, 0, 0});\n while(!ms.empty()) {\n auto [d, now, k] = *ms.begin();\n ms.erase(ms.begin());\n for(auto [to, cost] : v[now]) {\n if(dis[to][k] > d + cost) {\n dis[to][k] = d + cost;\n ms.insert({dis[to][k], to, k});\n }\n if(k < m and dis[to][k + 1] > d) {\n dis[to][k + 1] = d;\n ms.insert({dis[to][k + 1], to, k + 1});\n }\n }\n }\n int ans = inf;\n rep(i, m + 1) if(dis[n - 1][i] <= C) ans = min(ans, i);\n cout << ans << endl; \n cin >> n >> m >> C;\n }\n\n return 0;\n}", "accuracy": 1, "time_ms": 230, "memory_kb": 10088, "score_of_the_acc": -0.3954, "final_rank": 15 }, { "submission_id": "aoj_1311_9729492", "code_snippet": "#include <bits/stdc++.h>\n\nusing namespace std;\n\n#define rep(i, a, b) for (int i = (int)(a); i < (int)(b); i++)\n#define rrep(i, a, b) for (int i = (int)(b)-1; i >= (int)(a); i--)\n#define ALL(v) (v).begin(), (v).end()\n#define UNIQUE(v) sort(ALL(v)), (v).erase(unique(ALL(v)), (v).end())\n#define SZ(v) (int)v.size()\n#define MIN(v) *min_element(ALL(v))\n#define MAX(v) *max_element(ALL(v))\n#define LB(v, x) int(lower_bound(ALL(v), (x)) - (v).begin())\n#define UB(v, x) int(upper_bound(ALL(v), (x)) - (v).begin())\n\nusing uint = unsigned int;\nusing ll = long long int;\nusing ull = unsigned long long;\nusing i128 = __int128_t;\nusing u128 = __uint128_t;\nconst int inf = 0x3fffffff;\nconst ll INF = 0x1fffffffffffffff;\n\ntemplate <typename T> inline bool chmax(T &a, T b) {\n if (a < b) {\n a = b;\n return 1;\n }\n return 0;\n}\ntemplate <typename T> inline bool chmin(T &a, T b) {\n if (a > b) {\n a = b;\n return 1;\n }\n return 0;\n}\ntemplate <typename T, typename U> T ceil(T x, U y) {\n assert(y != 0);\n if (y < 0)\n x = -x, y = -y;\n return (x > 0 ? (x + y - 1) / y : x / y);\n}\ntemplate <typename T, typename U> T floor(T x, U y) {\n assert(y != 0);\n if (y < 0)\n x = -x, y = -y;\n return (x > 0 ? x / y : (x - y + 1) / y);\n}\ntemplate <typename T> int popcnt(T x) {\n return __builtin_popcountll(x);\n}\ntemplate <typename T> int topbit(T x) {\n return (x == 0 ? -1 : 63 - __builtin_clzll(x));\n}\ntemplate <typename T> int lowbit(T x) {\n return (x == 0 ? -1 : __builtin_ctzll(x));\n}\n\ntemplate <class T, class U>\nostream &operator<<(ostream &os, const pair<T, U> &p) {\n os << \"P(\" << p.first << \", \" << p.second << \")\";\n return os;\n}\ntemplate <typename T> ostream &operator<<(ostream &os, const vector<T> &vec) {\n os << \"{\";\n for (int i = 0; i < vec.size(); i++) {\n os << vec[i] << (i + 1 == vec.size() ? \"\" : \", \");\n }\n os << \"}\";\n return os;\n}\ntemplate <typename T, typename U>\nostream &operator<<(ostream &os, const map<T, U> &map_var) {\n os << \"{\";\n for (auto itr = map_var.begin(); itr != map_var.end(); itr++) {\n os << \"(\" << itr->first << \", \" << itr->second << \")\";\n itr++;\n if (itr != map_var.end())\n os << \", \";\n itr--;\n }\n os << \"}\";\n return os;\n}\ntemplate <typename T> ostream &operator<<(ostream &os, const set<T> &set_var) {\n os << \"{\";\n for (auto itr = set_var.begin(); itr != set_var.end(); itr++) {\n os << *itr;\n ++itr;\n if (itr != set_var.end())\n os << \", \";\n itr--;\n }\n os << \"}\";\n return os;\n}\n#ifdef LOCAL\n#define show(...) _show(0, #__VA_ARGS__, __VA_ARGS__)\n#else\n#define show(...) true\n#endif\ntemplate <typename T> void _show(int i, T name) {\n cerr << '\\n';\n}\ntemplate <typename T1, typename T2, typename... T3>\nvoid _show(int i, const T1 &a, const T2 &b, const T3 &...c) {\n for (; a[i] != ',' && a[i] != '\\0'; i++)\n cerr << a[i];\n cerr << \":\" << b << \" \";\n _show(i + 1, a, c...);\n}\n\n/**\n * @brief template\n */\n\nint main() {\nwhile(1) {\n int N, M, K;\n cin >> N >> M >> K;\n if (N == 0) return 0;\n vector<vector<int>> G(N);\n vector<int> A(M), B(M), C(M);\n rep(i,0,M) {\n cin >> A[i] >> B[i] >> C[i];\n A[i]--, B[i]--;\n G[A[i]].push_back(i);\n }\n vector<vector<int>> DP(N,vector<int>(N+1,inf));\n priority_queue<pair<int,pair<int,int>>,vector<pair<int,pair<int,int>>>,greater<pair<int,pair<int,int>>>> PQ;\n DP[0][0] = 0;\n PQ.push({0,{0,0}});\n while(!PQ.empty()) {\n auto [D,P] = PQ.top();\n auto [V,X] = P;\n PQ.pop();\n if (D > DP[V][X]) continue;\n for (int i : G[V]) {\n int NV = B[i], Cost = C[i];\n if (chmin(DP[NV][X],DP[V][X]+Cost)) {\n PQ.push({DP[NV][X],{NV,X}});\n }\n if (X < N) {\n if (chmin(DP[NV][X+1],DP[V][X])) {\n PQ.push({DP[NV][X+1],{NV,X+1}});\n }\n }\n }\n }\n int ANS = inf;\n rep(i,0,N+1) {\n if (DP[N-1][i] <= K) chmin(ANS,i);\n }\n cout << ANS << endl;\n}\n}", "accuracy": 1, "time_ms": 40, "memory_kb": 3676, "score_of_the_acc": -0.0281, "final_rank": 6 }, { "submission_id": "aoj_1311_9006540", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\nusing P = pair<ll, ll>;\n#define rep(i, a, b) for(ll i = a; i < b; ++i)\n#define rrep(i, a, b) for(ll i = a; i >= b; --i)\nconstexpr ll inf = 4e18;\nstruct SetupIO {\n SetupIO() {\n ios::sync_with_stdio(0);\n cin.tie(0);\n cout << fixed << setprecision(30);\n }\n} setup_io;\ntemplate <typename T = int>\nstruct Edge {\n int from, to;\n T cost;\n int idx;\n Edge()\n : from(-1), to(-1), cost(-1), idx(-1) {}\n Edge(int from, int to, T cost = 1, int idx = -1)\n : from(from), to(to), cost(cost), idx(idx) {}\n operator int() const {\n return to;\n }\n};\ntemplate <typename T = int>\nstruct Graph {\n Graph(int N)\n : n(N), es(0), g(N) {}\n int size() const {\n return n;\n }\n int edge_size() const {\n return es;\n }\n void add_edge(int from, int to, T cost = 1) {\n assert(0 <= from and from < n);\n assert(0 <= to and to < n);\n g[from].emplace_back(from, to, cost, es);\n g[to].emplace_back(to, from, cost, es++);\n }\n void add_directed_edge(int from, int to, T cost = 1) {\n assert(0 <= from and from < n);\n assert(0 <= to and to < n);\n g[from].emplace_back(from, to, cost, es++);\n }\n inline vector<Edge<T>>& operator[](const int& k) {\n assert(0 <= k and k < n);\n return g[k];\n }\n inline const vector<Edge<T>>& operator[](const int& k) const {\n assert(0 <= k and k < n);\n return g[k];\n }\n\n private:\n int n, es;\n vector<vector<Edge<T>>> g;\n};\ntemplate <typename T = int>\nusing Edges = vector<Edge<T>>;\ntemplate <typename T>\nvector<pair<T, int>> dijkstra(const Graph<T>& g, const int s = 0) {\n int n = g.size();\n assert(0 <= s and s < n);\n vector<pair<T, int>> d(n, {numeric_limits<T>::max(), -1});\n priority_queue<pair<T, int>, vector<pair<T, int>>, greater<pair<T, int>>> pq;\n d[s] = {0, -1};\n pq.push({0, s});\n while(!pq.empty()) {\n pair<T, int> p = pq.top();\n pq.pop();\n T dist = p.first;\n int cur = p.second;\n if(d[cur].first < dist) continue;\n for(const Edge<T>& edge : g[cur]) {\n if(d[edge.to].first > d[cur].first + edge.cost) {\n d[edge.to] = {d[cur].first + edge.cost, cur};\n pq.push({d[edge.to].first, edge.to});\n }\n }\n }\n return d;\n}\nint main(void) {\n while(true) {\n int n, m, c;\n cin >> n >> m >> c;\n if(n == 0 and m == 0 and c == 0) break;\n Graph<int> g(n * (m + 1));\n rep(i, 0, m) {\n int f, t, w;\n cin >> f >> t >> w;\n f--;\n t--;\n rep(j, 0, m) {\n g.add_directed_edge(f * (m + 1) + j, t * (m + 1) + j + 1, 0);\n }\n rep(j, 0, m + 1) {\n g.add_directed_edge(f * (m + 1) + j, t * (m + 1) + j, w);\n }\n }\n vector<pair<int, int>> d = dijkstra(g);\n int ans = m;\n rep(i, 0, m + 1) {\n if(d[(n - 1) * (m + 1) + i].first <= c) {\n ans = i;\n break;\n }\n }\n cout << ans << '\\n';\n }\n}", "accuracy": 1, "time_ms": 790, "memory_kb": 56420, "score_of_the_acc": -1.9802, "final_rank": 20 }, { "submission_id": "aoj_1311_8998572", "code_snippet": "#line 2 \"cp-library/src/cp-template.hpp\"\n#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\nusing ld = long double;\nusing uint = unsigned int;\nusing ull = unsigned long long;\nusing i32 = int;\nusing u32 = unsigned int;\nusing i64 = long long;\nusing u64 = unsigned long long;\nusing i128 = __int128_t;\ntemplate < class T > bool chmin(T& a, T b) { if(a > b) { a = b; return true; } return false; }\ntemplate < class T > bool chmax(T& a, T b) { if(a < b) { a = b; return true; } return false; }\ntemplate < class T, class U > T ceil (T x, U y) { return (x > 0 ? (x + y - 1) / y : x / y); }\ntemplate < class T, class U > T floor(T x, U y) { return (x > 0 ? x / y : (x - y + 1) / y); }\nint popcnt(i32 x) { return __builtin_popcount(x); }\nint popcnt(u32 x) { return __builtin_popcount(x); }\nint popcnt(i64 x) { return __builtin_popcountll(x); }\nint popcnt(u64 x) { return __builtin_popcountll(x); }\n\n#line 2 \"cp-library/src/utility/rep_itr.hpp\"\ntemplate < class T > struct itr_rep {\n T i, d;\n constexpr itr_rep(const T i) noexcept : i(i), d(1) {}\n constexpr itr_rep(const T i, const T d) noexcept : i(i), d(d) {}\n void operator++() noexcept { i += d; }\n constexpr int operator*() const noexcept { return i; }\n constexpr bool operator!=(const itr_rep x) const noexcept { return d > 0 ? i < x.i : i > x.i; }\n};\n\ntemplate < class T > struct rep {\n const itr_rep< T > s, t;\n constexpr rep(const T t) noexcept : s(0), t(t) {}\n constexpr rep(const T s, const T t) noexcept : s(s), t(t) {}\n constexpr rep(const T s, const T t, const T d) noexcept : s(s, d), t(t, d) {}\n constexpr auto begin() const noexcept { return s; }\n constexpr auto end () const noexcept { return t; }\n};\n\ntemplate < class T > struct revrep {\n const itr_rep < T > s, t;\n constexpr revrep(const T t) noexcept : s(t - 1, -1), t(-1, -1) {}\n constexpr revrep(const T s, const T t) noexcept : s(t - 1, -1), t(s - 1, -1) {}\n constexpr revrep(const T s, const T t, const T d) noexcept : s(t - 1, -d), t(s - 1, -d) {}\n constexpr auto begin() const noexcept { return s; }\n constexpr auto end () const noexcept { return t; }\n};\n#line 3 \"cp-library/src/utility/io.hpp\"\n\n/* 128bit integer */\nistream& operator>>(istream& is, i128& x) {\n std::string s; is >> s;\n int pm = (s[0] == '-');\n x = 0;\n for(int i : rep(pm, int(s.size()))) x = x * 10 + (s[i] - '0');\n if(pm) x *= -1;\n return is;\n}\nostream& operator<<(ostream& os, const i128& x) {\n if(x == 0) return os << '0';\n i128 y = x;\n if(y < 0) { os << '-'; y *= -1; }\n std::vector<int> ny;\n while(y > 0) { ny.push_back(y % 10); y /= 10; }\n for(int i : revrep(ny.size())) os << ny[i];\n return os;\n}\n\ntemplate < class S, class T > istream& operator>>(istream& is, std::pair< S, T >& x) { is >> x.first >> x.second; return is; }\ntemplate < class S, class T > ostream& operator<<(ostream& os, const std::pair< S, T >& x) { os << x.first << \" \" << x.second; return os; }\n\nnamespace scanner {\n struct sca {\n template < class T > operator T() {\n T s; std::cin >> s; return s;\n }\n };\n struct vec {\n int n;\n vec(int n) : n(n) {}\n template < class T > operator std::vector< T >() {\n std::vector< T > v(n);\n for(T& x : v) std::cin >> x;\n return v;\n }\n };\n struct mat {\n int h, w;\n mat(int h, int w) : h(h), w(w) {}\n template < class T > operator std::vector< std::vector< T > >() {\n std::vector m(h, std::vector< T >(w));\n for(std::vector< T >& v : m) for(T& x : v) std::cin >> x;\n return m;\n }\n };\n struct speedup {\n speedup() {\n std::cin.tie(0);\n std::ios::sync_with_stdio(0);\n }\n } speedup_instance;\n}\nscanner::sca in() { return scanner::sca(); }\nscanner::vec in(int n) { return scanner::vec(n); }\nscanner::mat in(int h, int w) { return scanner::mat(h, w); }\n\nnamespace printer {\n void precision(int d) { std::cout << std::fixed << std::setprecision(d); }\n void flush() { std::cout.flush(); }\n}\n\ntemplate < class T >\nostream& operator<<(ostream& os, const std::vector< T > a) {\n int n = a.size();\n for(int i : rep(n)) { os << a[i]; if(i != n - 1) os << ' '; }\n return os;\n}\n\nint print() { std::cout << '\\n'; return 0; }\ntemplate < class head, class... tail > int print(head&& h, tail&&... t) {\n std::cout << h; if(sizeof...(tail)) std::cout << ' ';\n return print(std::forward<tail>(t)...);\n}\ntemplate < class T > int print_n(const std::vector< T > a) {\n int n = a.size();\n for(int i : rep(n)) std::cout << a[i] << \"\\n\";\n return 0;\n}\n\n\n#line 2 \"cp-library/src/utility/key_val.hpp\"\n\ntemplate < class K, class V >\nstruct key_val {\n K key; V val;\n key_val() {}\n key_val(K key, V val) : key(key), val(val) {}\n template < std::size_t Index >\n std::tuple_element_t< Index, key_val >& get() {\n if constexpr (Index == 0) return key;\n if constexpr (Index == 1) return val;\n }\n};\n\nnamespace std {\n\ntemplate < class K, class V > struct tuple_size < key_val< K, V > > : integral_constant< size_t, 2 > {};\ntemplate < class K, class V > struct tuple_element < 0, key_val< K, V > > { using type = K; };\ntemplate < class K, class V > struct tuple_element < 1, key_val< K, V > > { using type = V; };\n\n}\n#line 2 \"cp-library/src/utility/vec_op.hpp\"\ntemplate < class T > key_val< int, T > max_of(const vector< T >& a) {\n int i = std::max_element(a.begin(), a.end()) - a.begin();\n return {i, a[i]};\n}\ntemplate < class T > key_val< int, T > min_of(const vector< T >& a) {\n int i = std::min_element(a.begin(), a.end()) - a.begin();\n return {i, a[i]};\n}\ntemplate < class S, class T > S sum_of(const vector< T >& a) {\n S sum = 0;\n for(const T x : a) sum += x;\n return sum;\n}\ntemplate < class S, class T > vector< S > freq_of(const vector< T >& a, T L, T R) {\n vector< S > res(R - L, S(0));\n for(const T x : a) res[x - L] += 1;\n return res;\n}\ntemplate < class S, class T > struct prefix_sum {\n vector< S > s;\n prefix_sum(const vector< T >& a) : s(a) {\n s.insert(s.begin(), S(0));\n for(int i : rep(a.size())) s[i + 1] += s[i];\n }\n // [L, R)\n S sum(int L, int R) { return s[R] - s[L]; }\n};\n#line 3 \"cp-library/src/utility/heap.hpp\"\n\ntemplate < class T > using heap_min = std::priority_queue< T, std::vector< T >, std::greater< T > >;\ntemplate < class T > using heap_max = std::priority_queue< T, std::vector< T >, std::less< T > >;\n\n#line 27 \"cp-library/src/cp-template.hpp\"\n\n#line 1 \"cp-library/src/algorithm/bin_search.hpp\"\ntemplate < class T, class F >\nT bin_search(T ok, T ng, F f) {\n while(abs(ng - ok) > 1) {\n T mid = (ok + ng) / 2;\n (f(mid) ? ok : ng) = mid;\n }\n return ok;\n}\n\ntemplate < class T, class F >\nT bin_search_real(T ok, T ng, F f, int step = 80) {\n while(step--) {\n T mid = (ok + ng) / 2;\n (f(mid) ? ok : ng) = mid;\n }\n return ok;\n}\n#line 2 \"cp-library/src/algorithm/argsort.hpp\"\n\ntemplate < class T > std::vector< int > argsort(const std::vector< T > &a) {\n std::vector< int > ids((int)a.size());\n std::iota(ids.begin(), ids.end(), 0);\n std::sort(ids.begin(), ids.end(), [&](int i, int j) {\n return a[i] < a[j] || (a[i] == a[j] && i < j);\n });\n return ids;\n}\n#line 1 \"macro.hpp\"\nnamespace macro {\n\nusing size_type = int;\ntemplate < class container > void sort(container& a) { std::sort(std:: begin(a), std:: end(a)); }\ntemplate < class container > void rsort(container& a) { std::sort(std::rbegin(a), std::rend(a)); }\ntemplate < class container > void reverse(container& a) { std::reverse(std::begin(a), std::end(a)); }\ntemplate < class container > void unique(container& a) {\n std::sort(std::begin(a), std::end(a));\n a.erase(std::unique(std::begin(a), std::end(a)), std::end(a));\n}\ntemplate < class container > container sorted(const container& a) { container b = a; sort(b); return std::move(b); }\ntemplate < class container > container rsorted(const container& a) { container b = a; rsort(b); return std::move(b); }\ntemplate < class container, class compare > void sort(container& a, const compare& cmp) { std::sort(std::begin(a), std::end(a), cmp); }\ntemplate < class container, class compare > container sorted(const container& a, const compare& cmp) { container b = a; sort(b, cmp); return std::move(b); }\ntemplate < class container, class value > size_type lower_bound(const container& a, const value& x) { return std::lower_bound(std::begin(a), std::end(a), x) - std::begin(a); }\ntemplate < class container, class value > size_type upper_bound(const container& a, const value& x) { return std::upper_bound(std::begin(a), std::end(a), x) - std::begin(a); }\n\nconst std::vector<std::pair<size_type, size_type>> dir4 = { {+1, 0}, {-1, 0}, { 0, +1}, { 0, -1} };\nconst std::vector<std::pair<size_type, size_type>> dir8 = { {-1, -1}, {-1, 0}, {-1, +1}, { 0, -1}, { 0, +1}, {+1, -1}, {+1, 0}, {+1, +1} };\n\n#ifdef _DEBUG\n#define debug(x) std::cout << \"[\" << __LINE__ << \"] \" << #x << \": \" << x << std::endl\n#else\n#define debug(x)\n#endif\n\ntemplate < class container > void concat(container& a, const container& b) {\n a.insert(std::end(a), std::begin(b), std::end(b));\n}\nstd::vector<size_type> iota(const size_type n) {\n std::vector<size_type> I(n);\n std::iota(std::begin(I), std::end(I), 0);\n return I;\n}\ntemplate < class container > std::vector<size_type> sort_idx(const container& a) {\n const size_type n = a.size();\n std::vector<size_type> I = iota(n);\n std::sort(std::begin(I), std::end(I), [&](size_type i, size_type j) { return a[i] < a[j] or (a[i] == a[j] and i < j); });\n return I;\n}\ntemplate < class container, class compare > std::vector<size_type> sort_idx(const container& a, const compare& cmp) {\n const size_type n = a.size();\n std::vector<size_type> I = iota(n);\n std::sort(std::begin(I), std::end(I), [&](size_type i, size_type j) { return cmp(a[i], a[j]) or (a[i] == a[j] and i < j); });\n return std::move(I);\n}\n\nstruct grid {\n using size_type = int;\n size_type H, W;\n grid(const size_type H, const size_type W) : H(H), W(W) {}\n bool contains(const size_type i, const size_type j) {\n return 0 <= i and i < H and 0 <= j and j < W;\n }\n};\n\nusing f64 = long double;\n\n} // namespace macro\n\nusing namespace macro;\n#line 1 \"cp-library/src/graph/shortest_path.hpp\"\n// g <- pair < v , cost > \ntemplate < class T >\nvector< T > dijkstra(vector<vector<pair<int, T>>> &graph, int s) {\n T INF = numeric_limits< T >::max();\n vector<T> dist(graph.size(), INF);\n priority_queue<pair<T,int>, vector<pair<T,int>>, greater<pair<T,int>>> q;\n q.push({dist[s] = T(0), s});\n while(!q.empty()){\n auto [uc, ui] = q.top(); q.pop();\n if(uc != dist[ui]) continue;\n for(auto [vi, vc] : graph[ui]) if(dist[vi] > uc + vc) \n q.push({dist[vi] = uc + vc, vi});\n }\n return dist;\n}\n\n// g <- pair < v , cost > \ntemplate < class T >\nvector< T > dijkstra(vector<vector<pair<int, T>>> &graph, vector<int> &starts) {\n T INF = numeric_limits< T >::max();\n vector<T> dist(graph.size(), INF);\n priority_queue<pair<T,int>, vector<pair<T,int>>, greater<pair<T,int>>> q;\n for(int s : starts) q.push({dist[s] = T(0), s});\n while(!q.empty()){\n auto [uc, ui] = q.top(); q.pop();\n if(uc != dist[ui]) continue;\n for(auto [vi, vc] : graph[ui]) if(dist[vi] > uc + vc) \n q.push({dist[vi] = uc + vc, vi});\n }\n return dist;\n}\n\n// g <- pair < v , cost > \ntemplate < class T >\npair< T, vector<int> > shortest_path(vector<vector<pair<int, T>>> &graph, int s, int t) {\n T INF = numeric_limits< T >::max();\n vector<T> dist(graph.size(), INF);\n vector<int> prev(graph.size(), -1);\n priority_queue<pair<T,int>, vector<pair<T,int>>, greater<pair<T,int>>> q;\n q.push({dist[s] = T(0), s});\n while(!q.empty()){\n auto [uc, ui] = q.top(); q.pop();\n if(uc != dist[ui]) continue;\n for(auto [vi, vc] : graph[ui]) if(dist[vi] > uc + vc) \n q.push({dist[vi] = uc + vc, vi}), prev[vi] = ui;\n }\n\n vector<int> path;\n if(dist[t] != INF) {\n for(int v = t; v != -1; v = prev[v]) path.push_back(v);\n reverse(path.begin(), path.end());\n }\n return {dist[t], path};\n}\n#line 4 \"A.cpp\"\n\nint solve(int n, int m, int target_cost) {\n vector<vector<pair<int,int>>> g(n * (m + 1));\n auto h = [&](int v, int e) { return v * (m + 1) + e; };\n for(int _ : rep(m)) {\n int f = in(), t = in(), c = in(); f--, t--;\n for(int e = 0; e <= m; e++) {\n g[h(f, e)].push_back({h(t, e), c});\n if(e + 1 <= m)\n g[h(f, e)].push_back({h(t, e + 1), 0});\n }\n }\n\n vector<int> dist = dijkstra(g, h(0, 0));\n for(int e = 0; e <= m; e++) {\n if(dist[h(n - 1, e)] <= target_cost) {\n return e;\n }\n }\n\n assert(0);\n}\n\nint main() {\n while(true) {\n int n = in(), m = in(), c = in();\n if(make_tuple(n, m, c) == make_tuple(0, 0, 0)) return 0;\n print(solve(n, m, c));\n }\n}", "accuracy": 1, "time_ms": 600, "memory_kb": 33568, "score_of_the_acc": -1.312, "final_rank": 17 }, { "submission_id": "aoj_1311_8505587", "code_snippet": "#include<bits/stdc++.h>\n\nusing namespace std;\n\n#define rep(i, n) for(int i = 0; i < (n); i++)\n#define per(i, n) for(int i = (n)-1; i >= 0; i--)\n#define rep2(i, l, r) for(int i = (l); i < (r); i++)\n#define per2(i, l, r) for(int i = (r)-1; i >= (l); i--)\n#define each(e, x) for(auto &e : x)\n#define sz(x) (int)x.size()\n#define all(x) begin(x), end(x)\n#define rall(x) rbegin(x), rend(x)\n#define eb emplace_back\n#define MM << ' ' <<\n#define TT template<typename T>\nusing ll = long long;\nusing pii = pair<int, int>;\nusing pll = pair<ll, ll>;\n\nTT bool chmin(T &x, T y) { return x > y ? (x = y, true) : false; }\n\nTT bool chmax(T &x, T y) { return x < y ? (x = y, true) : false; }\n\nTT using minheap = priority_queue<T, vector<T>, greater<T>>;\nTT using maxheap = priority_queue<T>;\n\nTT void print(vector<T> v, T x = 0) {\n int n = sz(v);\n rep(i, n) cout << v[i] + x << (i == n - 1 ? '\\n' : ' ');\n if (n == 0) cout << '\\n';\n}\n\nTT void printn(vector<T> v, T x = 0) {\n int n = sz(v);\n rep(i, n) cout << v[i] + x << '\\n';\n}\n\nTT void rearrange(vector<T> &v) {\n sort(all(v));\n v.erase(unique(all(v)), end(v));\n}\n\nTT int lb(vector<T> &v, T x) { return lower_bound(all(v), x) - begin(v); }\n\nTT int ub(vector<T> &v, T x) { return upper_bound(all(v), x) - begin(v); }\n\nconst int inf = (1 << 30) - 1;\nconst ll INF = (1LL << 60) - 1;\n\nstruct io_setup {\n io_setup() {\n ios_base::sync_with_stdio(false);\n cin.tie(NULL);\n cout << fixed << setprecision(15);\n }\n} io_setup;\n\nint main() {\n while (1) {\n int n, m, c;\n cin >> n >> m >> c;\n if (!n)\n break;\n vector<vector<pii>> g(n);\n rep(i, m) {\n int u, v, w;\n cin >> u >> v >> w;\n u--, v--;\n g[u].eb(v, w);\n }\n vector<vector<int>> d(n, vector<int>(n, 1e9));\n minheap<pair<int, pii>> que;\n d[0][0] = 0, que.emplace(0, pair(0, 0));\n while (sz(que)) {\n auto [nd, ij] = que.top();\n que.pop();\n auto [ni, nj] = ij;\n if (nd != d[ni][nj])\n continue;\n for (auto [e, cost] : g[ni]) {\n if (chmin(d[e][nj], nd + cost))\n que.emplace(nd + cost, pair(e, nj));\n if (nj + 1 < n && chmin(d[e][nj + 1], nd))\n que.emplace(nd, pair(e, nj + 1));\n }\n }\n rep(j, n) {\n if (d[n - 1][j] <= c) {\n cout << j << '\\n';\n break;\n }\n }\n }\n}", "accuracy": 1, "time_ms": 30, "memory_kb": 3440, "score_of_the_acc": -0.0106, "final_rank": 2 }, { "submission_id": "aoj_1311_8162785", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\nusing ll = long long;\n\ntemplate<class T>\nbool chmin(T &a, const T b) {if (a > b) {a = b; return 1;} return 0;}\n\ntemplate<class T>\nbool chmax(T &a, const T b) {if (a < b) {a = b; return 1;} return 0;}\n\nbool is_end = false;\n\nstruct edge\n{\n\tll to, cost;\n\t\n\tedge() = default;\n\tedge(ll t, ll c) : to(t), cost(c) { }\n};\n\nvoid calc()\n{\n\tll N, M, C; cin >> N >> M >> C;\n\t\n\tif (N == 0)\n\t{\n\t\tis_end = true;\n\t\treturn;\n\t}\n\t\n\tvector<vector<edge>> G(N);\n\tfor (int i = 0; i < M; ++i)\n\t{\n\t\tll f, t, c; cin >> f >> t >> c;\n\t\tf--, t--;\n\t\tG[f].emplace_back(edge(t, c));\n\t}\n\t\n\tconst ll INF = 1e18;\n\tvector<vector<ll>> dist(N, vector<ll>(M+1, INF));\n\tdist[0][0] = 0;\n\tpriority_queue<tuple<ll, ll, ll>, vector<tuple<ll, ll, ll>>, greater<tuple<ll, ll, ll>>> pq;\n\tpq.push({dist[0][0], 0, 0});\n\t\n\twhile (!pq.empty())\n\t{\n\t\tauto [d, v, m] = pq.top();\n\t\tpq.pop();\n\t\t\n\t\tif (dist[v][m] < d) {continue;}\n\t\t\t\t\n\t\tfor (auto e : G[v])\n\t\t{\n\t\t\tll nv = e.to, cst = e.cost;\n\t\t\tif (chmin(dist[nv][m], dist[v][m] + cst))\n\t\t\t{\n\t\t\t\tpq.push({dist[nv][m], nv, m});\n\t\t\t}\n\t\t\tif (m == M) {continue;}\n\t\t\tif (chmin(dist[nv][m+1], dist[v][m] + 0))\n\t\t\t{\n\t\t\t\tpq.push({dist[nv][m+1], nv, m+1});\n\t\t\t}\n\t\t}\n\t\t\n\t}\n\t\n\t// for (int v = 0; v < N; ++v)\n\t// {\n\t// \tcout << endl;\n\t// \tfor (int i = 0; i <= M; ++i)\n\t// \t{\n\t// \t\tif (dist[v][i] < INF)\n\t// \t\t{\n\t// \t\t\tcout << v << \" \" << i << \" \" << dist[v][i] << endl;\n\t// \t\t}\n\t// \t}\n\t// }\n\t\n\tll res = M;\n\tfor (ll i = 0; i <= M; ++i)\n\t{\n\t\tif (dist[N-1][i] <= C) {chmin(res, i);}\n\t}\n\tcout << res << endl;\n\t\n\t\n\treturn;\n}\n\nint main()\n{\n\twhile (!is_end)\n\t{\n\t\tcalc();\n\t}\n\t\n\treturn 0;\n}", "accuracy": 1, "time_ms": 210, "memory_kb": 9320, "score_of_the_acc": -0.3551, "final_rank": 14 }, { "submission_id": "aoj_1311_6783569", "code_snippet": "#line 1 \"c.cpp\"\n/**\n *\tauthor: nok0\n *\tcreated: 2022.07.05 15:55:33\n**/\n#line 1 \"/Users/nok0/Documents/Programming/nok0/cftemp.hpp\"\n#include <bits/stdc++.h>\nusing namespace std;\n\n#pragma region Macros\n// rep macro\n#define foa(v, a) for(auto &v : a)\n#define repname(a, b, c, d, e, ...) e\n#define rep(...) repname(__VA_ARGS__, rep3, rep2, rep1, rep0)(__VA_ARGS__)\n#define rep0(x) for(int i = 0; i < (x); ++i)\n#define rep1(i, x) for(int i = 0; i < (x); ++i)\n#define rep2(i, l, r) for(int i = (l); i < (r); ++i)\n#define rep3(i, l, r, c) for(int i = (l); i < (r); i += (c))\n#define repsname(a, b, c, ...) c\n#define reps(...) repsname(__VA_ARGS__, reps1, reps0)(__VA_ARGS__)\n#define reps0(x) for(int i = 1; i <= (x); ++i)\n#define reps1(i, x) for(int i = 1; i <= (x); ++i)\n#define rrepname(a, b, c, d, e, ...) e\n#define rrep(...) rrepname(__VA_ARGS__, rrep3, rrep2, rrep1, rrep0)(__VA_ARGS__)\n#define rrep0(x) for(int i = (x)-1; i >= 0; --i)\n#define rrep1(i, x) for(int i = (x)-1; i >= 0; --i)\n#define rrep2(i, r, l) for(int i = (r)-1; i >= (l); --i)\n#define rrep3(i, r, l, c) for(int i = (r)-1; i >= (l); i -= (c))\n#define rrepsname(a, b, c, ...) c\n#define rreps(...) rrepsname(__VA_ARGS__, rreps1, rreps0)(__VA_ARGS__)\n#define rreps0(x) for(int i = (x); i >= 1; --i)\n#define rreps1(i, x) for(int i = (x); i >= 1; --i)\n\n// name macro\n#define pb push_back\n#define eb emplace_back\n#define SZ(x) ((int)(x).size())\n#define all(x) (x).begin(), (x).end()\n#define rall(x) (x).rbegin(), (x).rend()\n#define popcnt(x) __builtin_popcountll(x)\ntemplate <class T = int>\nusing V = std::vector<T>;\ntemplate <class T = int>\nusing VV = std::vector<std::vector<T>>;\ntemplate <class T>\nusing pqup = std::priority_queue<T, std::vector<T>, std::greater<T>>;\nusing ll = long long;\nusing ld = long double;\nusing int128 = __int128_t;\nusing pii = std::pair<int, int>;\nusing pll = std::pair<long long, long long>;\n\n// input macro\ntemplate <class T, class U>\nstd::istream &operator>>(std::istream &is, std::pair<T, U> &p) {\n\tis >> p.first >> p.second;\n\treturn is;\n}\ntemplate <class T>\nstd::istream &operator>>(std::istream &is, std::vector<T> &v) {\n\tfor(T &i : v) is >> i;\n\treturn is;\n}\nstd::istream &operator>>(std::istream &is, __int128_t &a) {\n\tstd::string s;\n\tis >> s;\n\t__int128_t ret = 0;\n\tfor(int i = 0; i < s.length(); i++)\n\t\tif('0' <= s[i] and s[i] <= '9')\n\t\t\tret = 10 * ret + s[i] - '0';\n\ta = ret * (s[0] == '-' ? -1 : 1);\n\treturn is;\n}\nnamespace scanner {\nvoid scan(int &a) { std::cin >> a; }\nvoid scan(long long &a) { std::cin >> a; }\nvoid scan(std::string &a) { std::cin >> a; }\nvoid scan(char &a) { std::cin >> a; }\nvoid scan(char a[]) { std::scanf(\"%s\", a); }\nvoid scan(double &a) { std::cin >> a; }\nvoid scan(long double &a) { std::cin >> a; }\ntemplate <class T, class U>\nvoid scan(std::pair<T, U> &p) { std::cin >> p; }\ntemplate <class T>\nvoid scan(std::vector<T> &a) { std::cin >> a; }\nvoid INPUT() {}\ntemplate <class Head, class... Tail>\nvoid INPUT(Head &head, Tail &...tail) {\n\tscan(head);\n\tINPUT(tail...);\n}\n} // namespace scanner\n#define VEC(type, name, size) \\\n\tstd::vector<type> name(size); \\\n\tscanner::INPUT(name)\n#define VVEC(type, name, h, w) \\\n\tstd::vector<std::vector<type>> name(h, std::vector<type>(w)); \\\n\tscanner::INPUT(name)\n#define INT(...) \\\n\tint __VA_ARGS__; \\\n\tscanner::INPUT(__VA_ARGS__)\n#define LL(...) \\\n\tlong long __VA_ARGS__; \\\n\tscanner::INPUT(__VA_ARGS__)\n#define STR(...) \\\n\tstd::string __VA_ARGS__; \\\n\tscanner::INPUT(__VA_ARGS__)\n#define CHAR(...) \\\n\tchar __VA_ARGS__; \\\n\tscanner::INPUT(__VA_ARGS__)\n#define DOUBLE(...) \\\n\tdouble __VA_ARGS__; \\\n\tscanner::INPUT(__VA_ARGS__)\n#define LD(...) \\\n\tlong double __VA_ARGS__; \\\n\tscanner::INPUT(__VA_ARGS__)\n\n// output-macro\ntemplate <class T, class U>\nstd::ostream &operator<<(std::ostream &os, const std::pair<T, U> &p) {\n\tos << p.first << \" \" << p.second;\n\treturn os;\n}\ntemplate <class T>\nstd::ostream &operator<<(std::ostream &os, const std::vector<T> &a) {\n\tfor(int i = 0; i < int(a.size()); ++i) {\n\t\tif(i) os << \" \";\n\t\tos << a[i];\n\t}\n\treturn os;\n}\nstd::ostream &operator<<(std::ostream &dest, __int128_t &value) {\n\tstd::ostream::sentry s(dest);\n\tif(s) {\n\t\t__uint128_t tmp = value < 0 ? -value : value;\n\t\tchar buffer[128];\n\t\tchar *d = std::end(buffer);\n\t\tdo {\n\t\t\t--d;\n\t\t\t*d = \"0123456789\"[tmp % 10];\n\t\t\ttmp /= 10;\n\t\t} while(tmp != 0);\n\t\tif(value < 0) {\n\t\t\t--d;\n\t\t\t*d = '-';\n\t\t}\n\t\tint len = std::end(buffer) - d;\n\t\tif(dest.rdbuf()->sputn(d, len) != len) {\n\t\t\tdest.setstate(std::ios_base::badbit);\n\t\t}\n\t}\n\treturn dest;\n}\ntemplate <class T>\nvoid print(const T a) { std::cout << a << '\\n'; }\ntemplate <class Head, class... Tail>\nvoid print(Head H, Tail... T) {\n\tstd::cout << H << ' ';\n\tprint(T...);\n}\ntemplate <class T>\nvoid printel(const T a) { std::cout << a << '\\n'; }\ntemplate <class T>\nvoid printel(const std::vector<T> &a) {\n\tfor(const auto &v : a)\n\t\tstd::cout << v << '\\n';\n}\ntemplate <class Head, class... Tail>\nvoid printel(Head H, Tail... T) {\n\tstd::cout << H << '\\n';\n\tprintel(T...);\n}\nvoid Yes(const bool b = true) { std::cout << (b ? \"Yes\\n\" : \"No\\n\"); }\nvoid No() { std::cout << \"No\\n\"; }\nvoid YES(const bool b = true) { std::cout << (b ? \"YES\\n\" : \"NO\\n\"); }\nvoid NO() { std::cout << \"NO\\n\"; }\nvoid err(const bool b = true) {\n\tif(b) {\n\t\tstd::cout << \"-1\\n\", exit(0);\n\t}\n}\n\n//debug macro\nnamespace debugger {\ntemplate <class T>\nvoid view(const std::vector<T> &a) {\n\tstd::cerr << \"{ \";\n\tfor(const auto &v : a) {\n\t\tstd::cerr << v << \", \";\n\t}\n\tstd::cerr << \"\\b\\b }\";\n}\ntemplate <class T>\nvoid view(const std::vector<std::vector<T>> &a) {\n\tstd::cerr << \"{\\n\";\n\tfor(const auto &v : a) {\n\t\tstd::cerr << \"\\t\";\n\t\tview(v);\n\t\tstd::cerr << \"\\n\";\n\t}\n\tstd::cerr << \"}\";\n}\ntemplate <class T, class U>\nvoid view(const std::vector<std::pair<T, U>> &a) {\n\tstd::cerr << \"{\\n\";\n\tfor(const auto &p : a) std::cerr << \"\\t(\" << p.first << \", \" << p.second << \")\\n\";\n\tstd::cerr << \"}\";\n}\ntemplate <class T, class U>\nvoid view(const std::map<T, U> &m) {\n\tstd::cerr << \"{\\n\";\n\tfor(const auto &p : m) std::cerr << \"\\t[\" << p.first << \"] : \" << p.second << \"\\n\";\n\tstd::cerr << \"}\";\n}\ntemplate <class T, class U>\nvoid view(const std::pair<T, U> &p) { std::cerr << \"(\" << p.first << \", \" << p.second << \")\"; }\ntemplate <class T>\nvoid view(const std::set<T> &s) {\n\tstd::cerr << \"{ \";\n\tfor(auto &v : s) {\n\t\tview(v);\n\t\tstd::cerr << \", \";\n\t}\n\tstd::cerr << \"\\b\\b }\";\n}\n\ntemplate <class T>\nvoid view(const T &e) { std::cerr << e; }\n} // namespace debugger\n#ifdef LOCAL\nvoid debug_out() {}\ntemplate <typename Head, typename... Tail>\nvoid debug_out(Head H, Tail... T) {\n\tdebugger::view(H);\n\tstd::cerr << \", \";\n\tdebug_out(T...);\n}\n#define debug(...) \\\n\tdo { \\\n\t\tstd::cerr << __LINE__ << \" [\" << #__VA_ARGS__ << \"] : [\"; \\\n\t\tdebug_out(__VA_ARGS__); \\\n\t\tstd::cerr << \"\\b\\b]\\n\"; \\\n\t} while(false)\n#else\n#define debug(...) (void(0))\n#endif\n\n// vector macro\ntemplate <class T>\nint lb(const std::vector<T> &a, const T x) { return std::distance((a).begin(), std::lower_bound((a).begin(), (a).end(), (x))); }\ntemplate <class T>\nint ub(const std::vector<T> &a, const T x) { return std::distance((a).begin(), std::upper_bound((a).begin(), (a).end(), (x))); }\ntemplate <class T>\nvoid UNIQUE(std::vector<T> &a) {\n\tstd::sort(a.begin(), a.end());\n\ta.erase(std::unique(a.begin(), a.end()), a.end());\n}\ntemplate <class T>\nstd::vector<T> press(std::vector<T> &a) {\n\tauto res = a;\n\tUNIQUE(res);\n\tfor(auto &v : a)\n\t\tv = lb(res, v);\n\treturn res;\n}\n#define SORTname(a, b, c, ...) c\n#define SORT(...) SORTname(__VA_ARGS__, SORT1, SORT0, ...)(__VA_ARGS__)\n#define SORT0(a) std::sort((a).begin(), (a).end())\n#define SORT1(a, c) std::sort((a).begin(), (a).end(), [](const auto x, const auto y) { return x c y; })\ntemplate <class T>\nvoid ADD(std::vector<T> &a, const T x = 1) {\n\tfor(auto &v : a) v += x;\n}\ntemplate <class T>\nvoid SUB(std::vector<T> &a, const T x = 1) {\n\tfor(auto &v : a) v -= x;\n}\nstd::vector<std::pair<char, int>> rle(const string &s) {\n\tint n = s.size();\n\tstd::vector<std::pair<char, int>> ret;\n\tfor(int l = 0; l < n;) {\n\t\tint r = l + 1;\n\t\tfor(; r < n and s[l] == s[r]; r++) {}\n\t\tret.emplace_back(s[l], r - l);\n\t\tl = r;\n\t}\n\treturn ret;\n}\ntemplate <class T>\nstd::vector<std::pair<T, int>> rle(const std::vector<T> &v) {\n\tint n = v.size();\n\tstd::vector<std::pair<T, int>> ret;\n\tfor(int l = 0; l < n;) {\n\t\tint r = l + 1;\n\t\tfor(; r < n and v[l] == v[r]; r++) {}\n\t\tret.emplace_back(v[l], r - l);\n\t\tl = r;\n\t}\n\treturn ret;\n}\nstd::vector<int> iota(int n) {\n\tstd::vector<int> p(n);\n\tstd::iota(p.begin(), p.end(), 0);\n\treturn p;\n}\ntemplate <class T>\nstruct cum_vector {\npublic:\n\tcum_vector() = default;\n\ttemplate <class U>\n\tcum_vector(const std::vector<U> &vec) : cum((int)vec.size() + 1) {\n\t\tfor(int i = 0; i < (int)vec.size(); i++)\n\t\t\tcum[i + 1] = cum[i] + vec[i];\n\t}\n\tT prod(int l, int r) {\n\t\treturn cum[r] - cum[l];\n\t}\n\nprivate:\n\tstd::vector<T> cum;\n};\n\n// math macro\ntemplate <class T, class U>\ninline bool chmin(T &a, const U &b) { return a > b ? a = b, true : false; }\ntemplate <class T, class U>\ninline bool chmax(T &a, const U &b) { return a < b ? a = b, true : false; }\ntemplate <class T>\nT divup(T x, T y) { return (x + y - 1) / y; }\ntemplate <class T>\nT POW(T a, long long n) {\n\tT ret = 1;\n\twhile(n) {\n\t\tif(n & 1) ret *= a;\n\t\ta *= a;\n\t\tn >>= 1;\n\t}\n\treturn ret;\n}\n// modpow\nlong long POW(long long a, long long n, const int mod) {\n\tlong long ret = 1;\n\ta = (a % mod + mod) % mod;\n\twhile(n) {\n\t\tif(n & 1) (ret *= a) %= mod;\n\t\t(a *= a) %= mod;\n\t\tn >>= 1;\n\t}\n\treturn ret;\n}\ntemplate <class T, class F>\nT bin_search(T ok, T ng, const F &f) {\n\twhile(abs(ok - ng) > 1) {\n\t\tT mid = (ok + ng) >> 1;\n\t\t(f(mid) ? ok : ng) = mid;\n\t}\n\treturn ok;\n}\ntemplate <class T, class F>\nT bin_search(T ok, T ng, const F &f, int loop) {\n\tfor(int i = 0; i < loop; i++) {\n\t\tT mid = (ok + ng) / 2;\n\t\t(f(mid) ? ok : ng) = mid;\n\t}\n\treturn ok;\n}\n\n// others\nstruct fast_io {\n\tfast_io() {\n\t\tios::sync_with_stdio(false);\n\t\tcin.tie(nullptr);\n\t\tcout << fixed << setprecision(15);\n\t}\n} fast_io_;\nconst int inf = 1e9;\nconst ll INF = 1e18;\n#pragma endregion\n\nvoid main_();\n\nint main() {\n\tmain_();\n\treturn 0;\n}\n#line 10 \"/Users/nok0/Documents/Programming/nok0/graph/graph.hpp\"\n\nstruct Edge {\n\tint to;\n\tlong long cost;\n\tEdge() = default;\n\tEdge(int to_, long long cost_) : to(to_), cost(cost_) {}\n\tbool operator<(const Edge &a) const { return cost < a.cost; }\n\tbool operator>(const Edge &a) const { return cost > a.cost; }\n\tfriend std::ostream &operator<<(std::ostream &s, Edge &a) {\n\t\ts << \"to: \" << a.to << \", cost: \" << a.cost;\n\t\treturn s;\n\t}\n};\n\nclass graph {\n\tstd::vector<std::vector<Edge>> edges;\n\n\ttemplate <class F>\n\tstruct rec_lambda {\n\t\tF f;\n\t\trec_lambda(F &&f_) : f(std::forward<F>(f_)) {}\n\t\ttemplate <class... Args>\n\t\tauto operator()(Args &&...args) const {\n\t\t\treturn f(*this, std::forward<Args>(args)...);\n\t\t}\n\t};\n\npublic:\n\tinline const std::vector<Edge> &operator[](int k) const { return edges[k]; }\n\tinline std::vector<Edge> &operator[](int k) { return edges[k]; }\n\n\tint size() const { return edges.size(); }\n\tvoid resize(const int n) { edges.resize(n); }\n\n\tgraph() = default;\n\tgraph(int n) : edges(n) {}\n\tgraph(int n, int e, bool weight = 0, bool directed = 0, int idx = 1) : edges(n) { input(e, weight, directed, idx); }\n\tconst long long INF = 3e18;\n\n\tvoid input(int e = -1, bool weight = 0, bool directed = false, int idx = 1) {\n\t\tif(e == -1) e = size() - 1;\n\t\twhile(e--) {\n\t\t\tint u, v;\n\t\t\tlong long cost = 1;\n\t\t\tstd::cin >> u >> v;\n\t\t\tif(weight) std::cin >> cost;\n\t\t\tu -= idx, v -= idx;\n\t\t\tedges[u].emplace_back(v, cost);\n\t\t\tif(!directed) edges[v].emplace_back(u, cost);\n\t\t}\n\t}\n\n\tvoid add_edge(int u, int v, long long cost = 1, bool directed = false, int idx = 0) {\n\t\tu -= idx, v -= idx;\n\t\tedges[u].emplace_back(v, cost);\n\t\tif(!directed) edges[v].emplace_back(u, cost);\n\t}\n\n\t// Ο(V+E)\n\tstd::vector<long long> bfs(int s) {\n\t\tstd::vector<long long> dist(size(), INF);\n\t\tstd::queue<int> que;\n\t\tdist[s] = 0;\n\t\tque.push(s);\n\t\twhile(!que.empty()) {\n\t\t\tint v = que.front();\n\t\t\tque.pop();\n\t\t\tfor(auto &e : edges[v]) {\n\t\t\t\tif(dist[e.to] != INF) continue;\n\t\t\t\tdist[e.to] = dist[v] + e.cost;\n\t\t\t\tque.push(e.to);\n\t\t\t}\n\t\t}\n\t\treturn dist;\n\t}\n\n\t// Ο(V+E)\n\t// constraint: cost of each edge is zero or one\n\tstd::vector<long long> zero_one_bfs(int s) {\n\t\tstd::vector<long long> dist(size(), INF);\n\t\tstd::deque<int> deq;\n\t\tdist[s] = 0;\n\t\tdeq.push_back(s);\n\t\twhile(!deq.empty()) {\n\t\t\tint v = deq.front();\n\t\t\tdeq.pop_front();\n\t\t\tfor(auto &e : edges[v]) {\n\t\t\t\tassert(0LL <= e.cost and e.cost < 2LL);\n\t\t\t\tif(e.cost and dist[e.to] > dist[v] + 1) {\n\t\t\t\t\tdist[e.to] = dist[v] + 1;\n\t\t\t\t\tdeq.push_back(e.to);\n\t\t\t\t} else if(!e.cost and dist[e.to] > dist[v]) {\n\t\t\t\t\tdist[e.to] = dist[v];\n\t\t\t\t\tdeq.push_front(e.to);\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\treturn dist;\n\t}\n\n\t// Ο((E+V)logV)\n\t// cannot reach: INF\n\tstd::vector<long long> dijkstra(int s) { // verified\n\t\tstd::vector<long long> dist(size(), INF);\n\t\tconst auto compare = [](const std::pair<long long, int> &a, const std::pair<long long, int> &b) { return a.first > b.first; };\n\t\tstd::priority_queue<std::pair<long long, int>, std::vector<std::pair<long long, int>>, decltype(compare)> que{compare};\n\t\tdist[s] = 0;\n\t\tque.emplace(0, s);\n\t\twhile(!que.empty()) {\n\t\t\tstd::pair<long long, int> p = que.top();\n\t\t\tque.pop();\n\t\t\tint v = p.second;\n\t\t\tif(dist[v] < p.first) continue;\n\t\t\tfor(auto &e : edges[v]) {\n\t\t\t\tif(dist[e.to] > dist[v] + e.cost) {\n\t\t\t\t\tdist[e.to] = dist[v] + e.cost;\n\t\t\t\t\tque.emplace(dist[e.to], e.to);\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\treturn dist;\n\t}\n\n\t// Ο(VE)\n\t// cannot reach: INF\n\t// negative cycle: -INF\n\tstd::vector<long long> bellman_ford(int s) { // verified\n\t\tint n = size();\n\t\tstd::vector<long long> res(n, INF);\n\t\tres[s] = 0;\n\t\tfor(int loop = 0; loop < n - 1; loop++) {\n\t\t\tfor(int v = 0; v < n; v++) {\n\t\t\t\tif(res[v] == INF) continue;\n\t\t\t\tfor(auto &e : edges[v]) {\n\t\t\t\t\tres[e.to] = std::min(res[e.to], res[v] + e.cost);\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\tstd::queue<int> que;\n\t\tstd::vector<int> chk(n);\n\t\tfor(int v = 0; v < n; v++) {\n\t\t\tif(res[v] == INF) continue;\n\t\t\tfor(auto &e : edges[v]) {\n\t\t\t\tif(res[e.to] > res[v] + e.cost and !chk[e.to]) {\n\t\t\t\t\tque.push(e.to);\n\t\t\t\t\tchk[e.to] = 1;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\twhile(!que.empty()) {\n\t\t\tint now = que.front();\n\t\t\tque.pop();\n\t\t\tfor(auto &e : edges[now]) {\n\t\t\t\tif(!chk[e.to]) {\n\t\t\t\t\tchk[e.to] = 1;\n\t\t\t\t\tque.push(e.to);\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\tfor(int i = 0; i < n; i++)\n\t\t\tif(chk[i]) res[i] = -INF;\n\t\treturn res;\n\t}\n\n\t// Ο(V^3)\n\tstd::vector<std::vector<long long>> warshall_floyd() { // verified\n\t\tint n = size();\n\t\tstd::vector<std::vector<long long>> dist(n, std::vector<long long>(n, INF));\n\t\tfor(int i = 0; i < n; i++) dist[i][i] = 0;\n\t\tfor(int i = 0; i < n; i++)\n\t\t\tfor(auto &e : edges[i]) dist[i][e.to] = std::min(dist[i][e.to], e.cost);\n\t\tfor(int k = 0; k < n; k++)\n\t\t\tfor(int i = 0; i < n; i++) {\n\t\t\t\tif(dist[i][k] == INF) continue;\n\t\t\t\tfor(int j = 0; j < n; j++) {\n\t\t\t\t\tif(dist[k][j] == INF) continue;\n\t\t\t\t\tdist[i][j] = std::min(dist[i][j], dist[i][k] + dist[k][j]);\n\t\t\t\t}\n\t\t\t}\n\t\treturn dist;\n\t}\n\n\t// Ο(V) (using DFS)\n\t// if a directed cycle exists, return {}\n\tstd::vector<int> topological_sort() { // verified\n\t\tstd::vector<int> res;\n\t\tint n = size();\n\t\tstd::vector<int> used(n, 0);\n\t\tbool not_DAG = false;\n\t\tfor(int i = 0; i < n; i++) {\n\t\t\trec_lambda([&](auto &&dfs, int k) -> void {\n\t\t\t\tif(not_DAG) return;\n\t\t\t\tif(used[k]) {\n\t\t\t\t\tif(used[k] == 1) not_DAG = true;\n\t\t\t\t\treturn;\n\t\t\t\t}\n\t\t\t\tused[k] = 1;\n\t\t\t\tfor(auto &e : edges[k]) dfs(e.to);\n\t\t\t\tused[k] = 2;\n\t\t\t\tres.push_back(k);\n\t\t\t})(i);\n\t\t}\n\t\tif(not_DAG) return std::vector<int>{};\n\t\tstd::reverse(res.begin(), res.end());\n\t\treturn res;\n\t}\n\n\tbool is_DAG() { return !topological_sort().empty(); } // verified\n\n\t// Ο(V)\n\t// array of the distance from each vertex to the most distant vertex\n\tstd::vector<long long> height() { // verified\n\t\tauto vec1 = bfs(0);\n\t\tint v1 = -1, v2 = -1;\n\t\tlong long dia = -1;\n\t\tfor(int i = 0; i < int(size()); i++)\n\t\t\tif(dia < vec1[i]) dia = vec1[i], v1 = i;\n\t\tvec1 = bfs(v1);\n\t\tdia = -1;\n\t\tfor(int i = 0; i < int(size()); i++)\n\t\t\tif(dia < vec1[i]) dia = vec1[i], v2 = i;\n\t\tauto vec2 = bfs(v2);\n\t\tfor(int i = 0; i < int(size()); i++)\n\t\t\tif(vec1[i] < vec2[i]) vec1[i] = vec2[i];\n\t\treturn vec1;\n\t}\n\n\t// O(V+E)\n\t// vector<(int)(0 or 1)>\n\t// if it is not bipartite, return {}\n\tstd::vector<int> bipartite_grouping() {\n\t\tstd::vector<int> colors(size(), -1);\n\t\tauto dfs = [&](auto self, int now, int col) -> bool {\n\t\t\tcolors[now] = col;\n\t\t\tfor(auto &e : edges[now]) {\n\t\t\t\tif(col == colors[e.to]) return false;\n\t\t\t\tif(colors[e.to] == -1 and !self(self, e.to, !col)) return false;\n\t\t\t}\n\t\t\treturn true;\n\t\t};\n\t\tfor(int i = 0; i < int(size()); i++)\n\t\t\tif(colors[i] == -1 and !dfs(dfs, i, 0)) return std::vector<int>{};\n\t\treturn colors;\n\t}\n\n\tbool is_bipartite() { return !bipartite_grouping().empty(); }\n\n\t// Ο(V+E)\n\t// ((v1, v2), diameter)\n\tstd::pair<std::pair<int, int>, long long> diameter() { // verified\n\t\tauto vec = bfs(0);\n\t\tint v1 = -1, v2 = -1;\n\t\tlong long dia = -1;\n\t\tfor(int i = 0; i < int(size()); i++)\n\t\t\tif(dia < vec[i]) dia = vec[i], v1 = i;\n\t\tvec = bfs(v1);\n\t\tdia = -1;\n\t\tfor(int i = 0; i < int(size()); i++)\n\t\t\tif(dia < vec[i]) dia = vec[i], v2 = i;\n\t\tstd::pair<std::pair<int, int>, long long> res = {{v1, v2}, dia};\n\t\treturn res;\n\t}\n\n\t// Ο(V+E)\n\t// return {s, v1, v2, ... t}\n\tstd::vector<int> diameter_path() {\n\t\tauto vec = bfs(0);\n\t\tint v1 = -1, v2 = -1;\n\t\tlong long dia = -1;\n\t\tfor(int i = 0; i < int(size()); i++)\n\t\t\tif(dia < vec[i]) dia = vec[i], v1 = i;\n\t\tauto vec2 = bfs(v1);\n\t\tdia = -1;\n\t\tfor(int i = 0; i < int(size()); i++)\n\t\t\tif(dia < vec2[i]) dia = vec2[i], v2 = i;\n\t\tvec = bfs(v2);\n\t\tstd::vector<int> ret;\n\t\tauto dfs = [&](auto self, int now, int p) -> void {\n\t\t\tret.emplace_back(now);\n\t\t\tif(now == v2) return;\n\t\t\tfor(auto &[to, cost] : (*this)[now]) {\n\t\t\t\tif(vec[to] + vec2[to] == dia and to != p)\n\t\t\t\t\tself(self, to, now);\n\t\t\t}\n\t\t};\n\t\tdfs(dfs, v1, -1);\n\t\treturn ret;\n\t}\n\n\t// Ο(V+E)\n\t// return subtree_size, root = root\n\tstd::vector<int> subtree_size(const int root) {\n\t\tint n = size();\n\t\tstd::vector<int> ret(n, 1);\n\t\trec_lambda([&](auto &&dfs, int now, int p) -> void {\n\t\t\tfor(auto &[to, cost] : (*this)[now]) {\n\t\t\t\tif(to == p) continue;\n\t\t\t\tdfs(to, now);\n\t\t\t\tret[now] += ret[to];\n\t\t\t}\n\t\t})(root, -1);\n\t\treturn ret;\n\t}\n\n\t// Ο(ElogV)\n\tlong long prim() { // verified\n\t\tlong long res = 0;\n\t\tstd::priority_queue<Edge, std::vector<Edge>, std::greater<Edge>> que;\n\t\tfor(auto &e : edges[0]) que.push(e);\n\t\tstd::vector<int> chk(size());\n\t\tchk[0] = 1;\n\t\tint cnt = 1;\n\t\twhile(cnt < size()) {\n\t\t\tauto e = que.top();\n\t\t\tque.pop();\n\t\t\tif(chk[e.to]) continue;\n\t\t\tcnt++;\n\t\t\tres += e.cost;\n\t\t\tchk[e.to] = 1;\n\t\t\tfor(auto &e2 : edges[e.to]) que.push(e2);\n\t\t}\n\t\treturn res;\n\t}\n\n\t// Ο(ElogE)\n\tlong long kruskal() { // verified\n\t\tstd::vector<std::tuple<int, int, long long>> Edges;\n\t\tfor(int i = 0; i < int(size()); i++)\n\t\t\tfor(auto &e : edges[i]) Edges.emplace_back(i, e.to, e.cost);\n\t\tstd::sort(Edges.begin(), Edges.end(), [](const std::tuple<int, int, long long> &a, const std::tuple<int, int, long long> &b) {\n\t\t\treturn std::get<2>(a) < std::get<2>(b);\n\t\t});\n\t\tstd::vector<int> uf_data(size(), -1);\n\t\tauto root = [&uf_data](auto self, int x) -> int {\n\t\t\tif(uf_data[x] < 0) return x;\n\t\t\treturn uf_data[x] = self(self, uf_data[x]);\n\t\t};\n\t\tauto unite = [&uf_data, &root](int u, int v) -> bool {\n\t\t\tu = root(root, u), v = root(root, v);\n\t\t\tif(u == v) return false;\n\t\t\tif(uf_data[u] > uf_data[v]) std::swap(u, v);\n\t\t\tuf_data[u] += uf_data[v];\n\t\t\tuf_data[v] = u;\n\t\t\treturn true;\n\t\t};\n\t\tlong long ret = 0;\n\t\tfor(auto &e : Edges)\n\t\t\tif(unite(std::get<0>(e), std::get<1>(e))) ret += std::get<2>(e);\n\t\treturn ret;\n\t}\n\n\tgraph build_mst() {\n\t\tstd::vector<std::tuple<int, int, long long>> Edges;\n\t\tfor(int i = 0; i < int(size()); i++)\n\t\t\tfor(auto &e : edges[i]) Edges.emplace_back(i, e.to, e.cost);\n\t\tstd::sort(Edges.begin(), Edges.end(), [](const std::tuple<int, int, long long> &a, const std::tuple<int, int, long long> &b) {\n\t\t\treturn std::get<2>(a) < std::get<2>(b);\n\t\t});\n\t\tstd::vector<int> uf_data(size(), -1);\n\t\tauto root = [&uf_data](auto self, int x) -> int {\n\t\t\tif(uf_data[x] < 0) return x;\n\t\t\treturn uf_data[x] = self(self, uf_data[x]);\n\t\t};\n\t\tauto unite = [&uf_data, &root](int u, int v) -> bool {\n\t\t\tu = root(root, u), v = root(root, v);\n\t\t\tif(u == v) return false;\n\t\t\tif(uf_data[u] > uf_data[v]) std::swap(u, v);\n\t\t\tuf_data[u] += uf_data[v];\n\t\t\tuf_data[v] = u;\n\t\t\treturn true;\n\t\t};\n\t\tgraph g(this->size());\n\t\tfor(auto &e : Edges)\n\t\t\tif(unite(std::get<0>(e), std::get<1>(e))) {\n\t\t\t\tg.add_edge(std::get<0>(e), std::get<1>(e), std::get<2>(e));\n\t\t\t}\n\t\treturn g;\n\t}\n\n\t// O(V)\n\tstd::vector<int> centroid() {\n\t\tint n = size();\n\t\tstd::vector<int> centroid, sz(n);\n\t\tauto dfs = [&](auto self, int now, int per) -> void {\n\t\t\tsz[now] = 1;\n\t\t\tbool is_centroid = true;\n\t\t\tfor(auto &e : edges[now]) {\n\t\t\t\tif(e.to != per) {\n\t\t\t\t\tself(self, e.to, now);\n\t\t\t\t\tsz[now] += sz[e.to];\n\t\t\t\t\tif(sz[e.to] > n / 2) is_centroid = false;\n\t\t\t\t}\n\t\t\t}\n\t\t\tif(n - sz[now] > n / 2) is_centroid = false;\n\t\t\tif(is_centroid) centroid.push_back(now);\n\t\t};\n\t\tdfs(dfs, 0, -1);\n\t\treturn centroid;\n\t}\n\n\t// Ο(V+E)\n\t// directed graph from root to leaf\n\tgraph root_to_leaf(int root = 0) {\n\t\tgraph res(size());\n\t\tstd::vector<int> chk(size(), 0);\n\t\tchk[root] = 1;\n\t\tauto dfs = [&](auto self, int now) -> void {\n\t\t\tfor(auto &e : edges[now]) {\n\t\t\t\tif(chk[e.to] == 1) continue;\n\t\t\t\tchk[e.to] = 1;\n\t\t\t\tres.add_edge(now, e.to, e.cost, 1, 0);\n\t\t\t\tself(self, e.to);\n\t\t\t}\n\t\t};\n\t\tdfs(dfs, root);\n\t\treturn res;\n\t}\n\n\t// Ο(V+E)\n\t// directed graph from leaf to root\n\tgraph leaf_to_root(int root = 0) {\n\t\tgraph res(size());\n\t\tstd::vector<int> chk(size(), 0);\n\t\tchk[root] = 1;\n\t\tauto dfs = [&](auto self, int now) -> void {\n\t\t\tfor(auto &e : edges[now]) {\n\t\t\t\tif(chk[e.to] == 1) continue;\n\t\t\t\tchk[e.to] = 1;\n\t\t\t\tres.add_edge(e.to, now, e.cost, 1, 0);\n\t\t\t\tself(self, e.to);\n\t\t\t}\n\t\t};\n\t\tdfs(dfs, root);\n\t\treturn res;\n\t}\n\n\t// long long Chu_Liu_Edmonds(int root = 0) {}\n};\n#line 7 \"c.cpp\"\n\nvoid solve() {\n\tINT(n, m, c);\n\tif(!n) exit(0);\n\tgraph g(n, m, 1, 1);\n\tgraph h(n * (m + 1));\n\trep(i, n) {\n\t\trep(j, m + 1) {\n\t\t\tfor(auto [to, cost] : g[i]) {\n\t\t\t\t//skip する場合\n\t\t\t\tif(j + 1 <= m) h.add_edge(i * (m + 1) + j, to * (m + 1) + j + 1, 0, 1);\n\t\t\t\th.add_edge(i * (m + 1) + j, to * (m + 1) + j, cost, 1);\n\t\t\t}\n\t\t}\n\t}\n\tauto d = h.dijkstra(0);\n\trep(i, m + 1) {\n\t\tif(d[(n - 1) * (m + 1) + i] <= c) {\n\t\t\tprint(i);\n\t\t\treturn;\n\t\t}\n\t}\n}\n\nvoid main_() {\n\twhile(1) solve();\n}", "accuracy": 1, "time_ms": 590, "memory_kb": 57500, "score_of_the_acc": -1.7368, "final_rank": 19 }, { "submission_id": "aoj_1311_6501233", "code_snippet": "#include <bits/stdc++.h>\n#pragma GCC optimize(\"Ofast\")\n#define _GLIBCXX_DEBUG\nusing namespace std;\nusing std::cout;\nusing std::cin;\nusing std::endl;\nusing ll=long long;\nusing ld=long double;\nll ILL=1167167167167167167;\nconst int INF=1050000000;\nconst ll mod=998244353;\n#define rep(i,a) for (ll i=0;i<a;i++)\n#define all(p) p.begin(),p.end()\ntemplate<class T> using _pq = priority_queue<T, vector<T>, greater<T>>;\ntemplate<class T> ll LB(vector<T> &v,T a){return lower_bound(v.begin(),v.end(),a)-v.begin();}\ntemplate<class T> ll UB(vector<T> &v,T a){return upper_bound(v.begin(),v.end(),a)-v.begin();}\ntemplate<class T> bool chmin(T &a,const T &b){if(a>b){a=b;return 1;}else return 0;}\ntemplate<class T> bool chmax(T &a,const T &b){if(a<b){a=b;return 1;}else return 0;}\ntemplate<class T> void So(vector<T> &v) {sort(v.begin(),v.end());}\ntemplate<class T> void Sore(vector<T> &v) {sort(v.begin(),v.end(),[](T x,T y){return x>y;});}\nvoid yneos(bool a){if(a) cout<<\"Yes\\n\"; else cout<<\"No\\n\";}\ntemplate<class T> void vec_out(vector<T> &p){for(int i=0;i<(int)(p.size());i++){if(i) cout<<\" \";cout<<p[i];}cout<<\"\\n\";}\n\nint N,M,C;\n\nvoid solve();\n// oddloop\nint main() {\n\tios::sync_with_stdio(false);\n\tcin.tie(nullptr);\n\t\n\twhile(cin>>N>>M>>C,N) solve();\n}\n\nvoid solve(){\n\tvector<vector<pair<int,int>>> G(N*(M+1));\n\trep(i,M){\n\t\tint f,t,w;\n\t\tcin>>f>>t>>w;\n\t\tf--,t--;\n\t\trep(j,M){\n\t\t\tG[f+j*N].push_back({t+j*N,w});\n\t\t\tG[f+j*N].push_back({t+j*N+N,0});\n\t\t}\n\t}\n\tvector<int> dp(N*(M+1),INF);\n\t_pq<pair<int,int>> pq;\n\tdp[0]=0;\n\tpq.push({0,0});\n\twhile(!pq.empty()){\n\t\tint ind,time;\n\t\ttie(time,ind)=pq.top();\n\t\tpq.pop();\n\t\tif(dp[ind]>time) continue;\n\t\tfor(auto x:G[ind]){\n\t\t\tif(chmin(dp[x.first],time+x.second)){\n\t\t\t\tpq.push({dp[x.first],x.first});\n\t\t\t}\n\t\t}\n\t}\n\trep(i,M){\n\t\tif(dp[i*N+N-1]>C&&C>=dp[N*(i+2)-1]){\n\t\t\tcout<<i+1<<\"\\n\";\n\t\t\treturn;\n\t\t}\n\t}\n}", "accuracy": 1, "time_ms": 600, "memory_kb": 31504, "score_of_the_acc": -1.2742, "final_rank": 16 }, { "submission_id": "aoj_1311_6023641", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\nconst int INF = 1 << 30;\n\nbool solve() {\n int n, m, c;\n cin >> n >> m >> c;\n if (n == 0 and m == 0 and c == 0) return false;\n vector<vector<pair<int, int>>> G(n);\n for (; m--;) {\n int f, t, x;\n cin >> f >> t >> x;\n G[--f].emplace_back(--t, x);\n }\n\n vector<vector<int>> dp(n, vector<int>(n, INF));\n priority_queue<tuple<int, int, int>> pq;\n dp[0][0] = 0;\n pq.emplace(-dp[0][0], 0, 0);\n\n while (!pq.empty()) {\n auto t = pq.top();\n pq.pop();\n int val, v, x;\n tie(val, v, x) = t;\n val *= -1;\n if (dp[v][x] < val) continue;\n for (auto& e : G[v]) {\n int u = e.first;\n if (val + e.second < dp[u][x]) {\n dp[u][x] = val + e.second;\n pq.emplace(-dp[u][x], u, x);\n }\n if (x + 1 < n and val < dp[u][x + 1]) {\n dp[u][x + 1] = val;\n pq.emplace(-dp[u][x + 1], u, x + 1);\n }\n }\n }\n\n int ans = n;\n for (int j = n - 1; j >= 0; j--) {\n if (dp[n - 1][j] <= c) {\n ans = j;\n }\n }\n\n cout << ans << '\\n';\n return true;\n}\n\nint main() {\n cin.tie(0);\n ios::sync_with_stdio(false);\n while (solve())\n ;\n return 0;\n}", "accuracy": 1, "time_ms": 60, "memory_kb": 3436, "score_of_the_acc": -0.05, "final_rank": 9 }, { "submission_id": "aoj_1311_6023637", "code_snippet": "#define LOCAL\n#include <bits/stdc++.h>\nusing namespace std;\n#pragma region Macros\ntypedef long long ll;\ntypedef __int128_t i128;\ntypedef unsigned int uint;\ntypedef unsigned long long ull;\n#define ALL(x) (x).begin(), (x).end()\n\ntemplate <typename T> istream& operator>>(istream& is, vector<T>& v) {\n for (T& x : v) is >> x;\n return is;\n}\ntemplate <typename T> ostream& operator<<(ostream& os, const vector<T>& v) {\n for (size_t i = 0; i < v.size(); i++) {\n os << v[i] << (i + 1 == v.size() ? \"\" : \" \");\n }\n return os;\n}\ntemplate <typename T, typename U> ostream& operator<<(ostream& os, const pair<T, U>& p) {\n os << '(' << p.first << ',' << p.second << ')';\n return os;\n}\ntemplate <typename T, typename U> ostream& operator<<(ostream& os, const map<T, U>& m) {\n os << '{';\n for (auto itr = m.begin(); itr != m.end();) {\n os << '(' << itr->first << ',' << itr->second << ')';\n if (++itr != m.end()) os << ',';\n }\n os << '}';\n return os;\n}\ntemplate <typename T, typename U> ostream& operator<<(ostream& os, const unordered_map<T, U>& m) {\n os << '{';\n for (auto itr = m.begin(); itr != m.end();) {\n os << '(' << itr->first << ',' << itr->second << ')';\n if (++itr != m.end()) os << ',';\n }\n os << '}';\n return os;\n}\ntemplate <typename T> ostream& operator<<(ostream& os, const set<T>& s) {\n os << '{';\n for (auto itr = s.begin(); itr != s.end();) {\n os << *itr;\n if (++itr != s.end()) os << ',';\n }\n os << '}';\n return os;\n}\ntemplate <typename T> ostream& operator<<(ostream& os, const multiset<T>& s) {\n os << '{';\n for (auto itr = s.begin(); itr != s.end();) {\n os << *itr;\n if (++itr != s.end()) os << ',';\n }\n os << '}';\n return os;\n}\ntemplate <typename T> ostream& operator<<(ostream& os, const unordered_set<T>& s) {\n os << '{';\n for (auto itr = s.begin(); itr != s.end();) {\n os << *itr;\n if (++itr != s.end()) os << ',';\n }\n os << '}';\n return os;\n}\ntemplate <typename T> ostream& operator<<(ostream& os, const deque<T>& v) {\n for (size_t i = 0; i < v.size(); i++) {\n os << v[i] << (i + 1 == v.size() ? \"\" : \" \");\n }\n return os;\n}\ntemplate <typename T, size_t N> ostream& operator<<(ostream& os, const array<T, N>& v) {\n for (size_t i = 0; i < N; i++) {\n os << v[i] << (i + 1 == N ? \"\" : \" \");\n }\n return os;\n}\n\ntemplate <int i, typename T> void print_tuple(ostream&, const T&) {}\ntemplate <int i, typename T, typename H, class... Args> void print_tuple(ostream& os, const T& t) {\n if (i) os << ',';\n os << get<i>(t);\n print_tuple<i + 1, T, Args...>(os, t);\n}\ntemplate <typename... Args> ostream& operator<<(ostream& os, const tuple<Args...>& t) {\n os << '{';\n print_tuple<0, tuple<Args...>, Args...>(os, t);\n return os << '}';\n}\n\nvoid debug_out() { cerr << '\\n'; }\ntemplate <class Head, class... Tail> void debug_out(Head&& head, Tail&&... tail) {\n cerr << head;\n if (sizeof...(Tail) > 0) cerr << \", \";\n debug_out(move(tail)...);\n}\n#ifdef LOCAL\n#define debug(...) \\\n cerr << \" \"; \\\n cerr << #__VA_ARGS__ << \" :[\" << __LINE__ << \":\" << __FUNCTION__ << \"]\" << '\\n'; \\\n cerr << \" \"; \\\n debug_out(__VA_ARGS__)\n#else\n#define debug(...) void(0)\n#endif\n\ntemplate <typename T> T gcd(T x, T y) { return y != 0 ? gcd(y, x % y) : x; }\ntemplate <typename T> T lcm(T x, T y) { return x / gcd(x, y) * y; }\n\nint topbit(signed t) { return t == 0 ? -1 : 31 - __builtin_clz(t); }\nint topbit(long long t) { return t == 0 ? -1 : 63 - __builtin_clzll(t); }\nint botbit(signed a) { return a == 0 ? 32 : __builtin_ctz(a); }\nint botbit(long long a) { return a == 0 ? 64 : __builtin_ctzll(a); }\nint popcount(signed t) { return __builtin_popcount(t); }\nint popcount(long long t) { return __builtin_popcountll(t); }\nbool ispow2(int i) { return i && (i & -i) == i; }\nlong long MSK(int n) { return (1LL << n) - 1; }\n\ntemplate <class T> T ceil(T x, T y) {\n assert(y >= 1);\n return (x > 0 ? (x + y - 1) / y : x / y);\n}\ntemplate <class T> T floor(T x, T y) {\n assert(y >= 1);\n return (x > 0 ? x / y : (x - y + 1) / y);\n}\n\ntemplate <class T1, class T2> inline bool chmin(T1& a, T2 b) {\n if (a > b) {\n a = b;\n return true;\n }\n return false;\n}\ntemplate <class T1, class T2> inline bool chmax(T1& a, T2 b) {\n if (a < b) {\n a = b;\n return true;\n }\n return false;\n}\n\ntemplate <typename T> void mkuni(vector<T>& v) {\n sort(v.begin(), v.end());\n v.erase(unique(v.begin(), v.end()), v.end());\n}\ntemplate <typename T> int lwb(const vector<T>& v, const T& x) { return lower_bound(v.begin(), v.end(), x) - v.begin(); }\n#pragma endregion\n\nconst int INF = 1e9;\nconst long long IINF = 1e18;\nconst int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};\nconst char dir[4] = {'D', 'R', 'U', 'L'};\nconst long long MOD = 1000000007;\n// const long long MOD = 998244353;\n\nbool solve() {\n int n, m, c;\n cin >> n >> m >> c;\n if (n == 0 and m == 0 and c == 0) return false;\n vector<vector<pair<int, int>>> G(n);\n for (; m--;) {\n int f, t, x;\n cin >> f >> t >> x;\n G[--f].emplace_back(--t, x);\n }\n\n vector<vector<int>> dp(n, vector<int>(n, INF));\n priority_queue<tuple<int, int, int>> pq;\n dp[0][0] = 0;\n pq.emplace(-dp[0][0], 0, 0);\n\n while (!pq.empty()) {\n auto t = pq.top();\n pq.pop();\n int val, v, x;\n tie(val, v, x) = t;\n val *= -1;\n if (dp[v][x] < val) continue;\n for (auto& e : G[v]) {\n int u = e.first;\n if (val + e.second < dp[u][x]) {\n dp[u][x] = val + e.second;\n pq.emplace(-dp[u][x], u, x);\n }\n if (x + 1 < n and val < dp[u][x + 1]) {\n dp[u][x + 1] = val;\n pq.emplace(-dp[u][x + 1], u, x + 1);\n }\n }\n }\n\n int ans = n;\n for (int j = n - 1; j >= 0; j--) {\n if (dp[n - 1][j] <= c) {\n ans = j;\n }\n }\n\n cout << ans << '\\n';\n return true;\n}\n\nint main() {\n cin.tie(0);\n ios::sync_with_stdio(false);\n while (solve())\n ;\n return 0;\n}", "accuracy": 1, "time_ms": 60, "memory_kb": 3560, "score_of_the_acc": -0.0523, "final_rank": 10 }, { "submission_id": "aoj_1311_6020851", "code_snippet": "#include<bits/stdc++.h> \nusing namespace std;\nusing ll=long long int;\n//using Int=__int128;\n#define ALL(A) A.begin(),A.end()\ntemplate<typename T1,typename T2> bool chmin(T1 &a,T2 b){if(a<=b)return 0; a=b; return 1;}\ntemplate<typename T1,typename T2> bool chmax(T1 &a,T2 b){if(a>=b)return 0; a=b; return 1;}\ntemplate<typename T> constexpr int bitUP(T x,int a){return (x>>a)&1;}\n//→ ↓ ← ↑ \nint dh[4]={0,1,0,-1}, dw[4]={1,0,-1,0};\n//右上から時計回り\n//int dh[8]={-1,0,1,1,1,0,-1,-1}, dw[8]={1,1,1,0,-1,-1,-1,0};\nlong double EPS = 1e-6;\nlong double PI = acos(-1);\nconst ll INF=(1LL<<62);\nconst int MAX=(1<<30);\nconstexpr ll MOD=1000000000+7;\n//constexpr ll MOD=998244353;\n\n\ninline void bin101(){\n ios::sync_with_stdio(false);\n cin.tie(0);\n cout << fixed << setprecision(20);\n}\n\nusing pii=pair<int,int>;\nusing pil=pair<int,ll>;\nusing pli=pair<ll,int>;\nusing pll=pair<ll,ll>;\nusing psi=pair<string,int>;\nusing pis=pair<int,string>;\nusing psl=pair<string,ll>;\nusing pls=pair<ll,string>;\nusing pss=pair<string,string>;\n\nusing Graph=vector<vector<int>>;\n\ntemplate<typename T >\nstruct edge {\n\tint to;\n\tT cost;\n\tedge()=default;\n\tedge(int to, T cost) : to(to), cost(cost) {}\n\n};\ntemplate<typename T>\nusing WeightGraph=vector<vector<edge<T>>>;\n\ntemplate<typename T>\nvoid CinGraph(int M,WeightGraph<T> &g,bool directed=false,bool index1=true){\n while(M--){\n int s,t;\n T cost;\n cin>>s>>t>>cost;\n if(index1) s--,t--;\n g[s].emplace_back(t,cost);\n if(not directed) g[t].emplace_back(s,cost);\n }\n}\n\nvoid CinGraph(int M,Graph &g,bool directed=false,bool index1=true){\n while(M--){\n int s,t;\n cin>>s>>t;\n if(index1) s--,t--;\n g[s].push_back(t);\n if(not directed) g[t].push_back(s);\n }\n}\n\n\n//0-indexed vector cin\ntemplate<typename T>\ninline istream &operator>>(istream &is,vector<T> &v) {\n for(size_t i=0;i<v.size();i++) is>>v[i];\n\treturn is;\n}\n \n//0-indexed vector cin\ntemplate<typename T>\ninline istream &operator>>(istream &is,vector<vector<T>> &v) {\n for(size_t i=0;i<v.size();i++){\n is>>v[i];\n }\n return is;\n}\n//vector cout\ntemplate<typename T>\ninline ostream &operator<<(ostream &os,const vector<T> &v) {\n for(size_t i=0;i<v.size();i++){\n if(i) os<<\" \";\n os<<v[i];\n }\n return os;\n}\n//vector<vector> cout\ntemplate<typename T>\ninline ostream &operator<<(ostream &os,const vector<vector<T>> &v) {\n for(size_t i=0;i<v.size();i++){\n os<<v[i];\n if(i+1!=v.size()) os<<\"\\n\";\n }\n return os;\n}\n\n//Graph out\ntemplate<typename T>\ninline ostream &operator<<(ostream &os,const Graph &g) {\n for(size_t i=0;i<g.size();i++){\n for(int to:g[i]){\n os<<i<<\"->\"<<to<<\" \";\n }\n os<<endl;\n }\n return os;\n}\n\n//WeightGraph out\ntemplate<typename T>\ninline ostream &operator<<(ostream &os,const WeightGraph<T> &g) {\n for(size_t i=0;i<g.size();i++){\n for(auto e:g[i]){\n os<<i<<\"->\"<<e.to<<\"(\"<<e.cost<<\") \";\n }\n os<<endl;\n }\n return os;\n}\n\n\n//要素数n 初期値x\ntemplate<typename T>\ninline vector<T> vmake(size_t n,T x){\n\treturn vector<T>(n,x);\n}\n\n//a,b,c,x data[a][b][c] 初期値x\ntemplate<typename... Args>\nauto vmake(size_t n,Args... args){\n\tauto v=vmake(args...);\n\treturn vector<decltype(v)>(n,move(v));\n}\ntemplate<typename V,typename T>\nvoid fill(V &v,const T value){\n v=value;\n}\n\ntemplate<typename V,typename T>\nvoid fill(vector<V> &vec,const T value){\n for(auto &v:vec) fill(v,value);\n}\n\n//pair cout\ntemplate<typename T, typename U>\ninline ostream &operator<<(ostream &os,const pair<T,U> &p) {\n\tos<<p.first<<\" \"<<p.second;\n\treturn os;\n}\n \n//pair cin\ntemplate<typename T, typename U>\ninline istream &operator>>(istream &is,pair<T,U> &p) {\n\tis>>p.first>>p.second;\n\treturn is;\n}\n \n//ソート\ntemplate<typename T>\ninline void vsort(vector<T> &v){\n sort(v.begin(),v.end());\n}\n \n//逆順ソート\ntemplate<typename T>\ninline void rvsort(vector<T> &v){\n\tsort(v.rbegin(),v.rend());\n}\n\n//1ビットの数を返す\ninline int popcount(int x){\n\treturn __builtin_popcount(x);\n}\n//1ビットの数を返す\ninline int popcount(ll x){\n\treturn __builtin_popcountll(x);\n}\ntemplate<typename T>\ninline void Compress(vector<T> &C){\n sort(C.begin(),C.end());\n C.erase(unique(C.begin(),C.end()),C.end());\n}\ntemplate<typename T>\ninline int lower_idx(const vector<T> &C,T value){\n return lower_bound(C.begin(),C.end(),value)-C.begin();\n}\ntemplate<typename T>\ninline int upper_idx(const vector<T> &C,T value){\n return upper_bound(C.begin(),C.end(),value)-C.begin();\n}\n//時計回りに90度回転\ntemplate<typename T>\ninline void rotate90(vector<vector<T>> &C){\n vector<vector<T>> D(C[0].size(),vector<T>(C.size()));\n for(int h=0;h<C.size();h++){\n for(int w=0;w<C[h].size();w++){\n D[w][C.size()-1-h]=C[h][w];\n }\n }\n C=D;\n}\n//補グラフを返す\n//i→iのような辺は加えない\nGraph ComplementGraph(const Graph &g){\n size_t sz=g.size();\n bool used[sz][sz];\n fill(used[0],used[sz],false);\n for(size_t i=0;i<sz;i++){\n for(int to:g[i]){\n used[i][to]=true;\n }\n }\n Graph ret(sz);\n for(size_t i=0;i<sz;i++){\n for(size_t j=0;j<sz;j++){\n if(used[i][j]) continue;\n if(i==j) continue;\n ret[i].push_back(j);\n }\n }\n return ret;\n}\n//グラフの分解 secondはある頂点がどこに対応するか id[i]={2,3}のとき,頂点iはret[2][3]に対応\n//無効グラフのみに対応\npair<vector<Graph>,vector<pair<int,int>>> GraphDecomposition(const Graph &g){\n vector<pair<int,int>> id(g.size(),pair<int,int>(-1,-1));\n vector<Graph> ret;\n vector<int> now;\n for(size_t i=0;i<g.size();i++){\n if(id[i].first!=-1) continue;\n id[i].first=ret.size();\n id[i].second=0;\n now.push_back(i);\n for(size_t j=0;j<now.size();j++){\n for(int to:g[now[j]]){\n if(id[to].first==-1){\n id[to].first=ret.size();\n id[to].second=now.size();\n now.push_back(to);\n }\n }\n }\n Graph r(now.size());\n for(size_t j=0;j<now.size();j++){\n r[j]=g[now[j]];\n for(int &to:r[j]){\n to=id[to].second;\n }\n }\n ret.push_back(r);\n now.clear();\n }\n return make_pair(ret,id);\n}\n//0indexを想定\nbool OutGrid(ll h,ll w,ll H,ll W){\n return (h>=H or w>=W or h<0 or w<0);\n}\n\nvoid NO(){\n cout<<\"NO\"<<\"\\n\";\n}\nvoid YES(){\n cout<<\"YES\"<<\"\\n\";\n}\nvoid No(){\n cout<<\"No\"<<\"\\n\";\n}\nvoid Yes(){\n cout<<\"Yes\"<<\"\\n\";\n}\nnamespace overflow{\n template<typename T>\n T max(){\n return numeric_limits<T>::max();\n }\n template<typename T>\n T ADD(T a,T b){\n T res;\n return __builtin_add_overflow(a,b,&res)?max<T>():res;\n }\n template<typename T>\n T MUL(T a,T b){\n T res;\n return __builtin_mul_overflow(a,b,&res)?max<T>():res;\n }\n};\nusing namespace overflow;\nstruct ModInt{\n using u64=uint_fast64_t;\n u64 a;\n constexpr ModInt() :a(0){}\n constexpr ModInt(ll x) :a((x>=0)?(x%MOD):(x%MOD+MOD) ) {}\n\n inline constexpr ModInt operator+(const ModInt rhs)const noexcept{\n return ModInt(*this)+=rhs;\n }\n inline constexpr ModInt operator-(const ModInt rhs)const noexcept{\n return ModInt(*this)-=rhs;\n }\n inline constexpr ModInt operator*(const ModInt rhs)const noexcept{\n return ModInt(*this)*=rhs;\n }\n inline constexpr ModInt operator/(const ModInt rhs)const noexcept{\n return ModInt(*this)/=rhs;\n }\n inline constexpr ModInt operator+(const ll rhs) const noexcept{\n return ModInt(*this)+=ModInt(rhs);\n }\n inline constexpr ModInt operator-(const ll rhs)const noexcept{\n return ModInt(*this)-=ModInt(rhs);\n }\n inline constexpr ModInt operator*(const ll rhs)const noexcept{\n return ModInt(*this)*=ModInt(rhs);\n }\n inline constexpr ModInt operator/(const ll rhs)const noexcept{\n return ModInt(*this)/=ModInt(rhs);\n }\n\n inline constexpr ModInt &operator+=(const ModInt rhs)noexcept{\n a+=rhs.a;\n if(a>=MOD) a-=MOD;\n return *this;\n }\n inline constexpr ModInt &operator-=(const ModInt rhs)noexcept{\n if(rhs.a>a) a+=MOD;\n a-=rhs.a;\n return *this;\n }\n inline constexpr ModInt &operator*=(const ModInt rhs)noexcept{\n a=(a*rhs.a)%MOD;\n return *this;\n }\n inline constexpr ModInt &operator/=(ModInt rhs)noexcept{\n a=(a*rhs.inverse().a)%MOD;\n return *this;\n }\n inline constexpr ModInt &operator+=(const ll rhs)noexcept{\n return *this+=ModInt(rhs);\n }\n inline constexpr ModInt &operator-=(const ll rhs)noexcept{\n return *this-=ModInt(rhs);\n }\n inline constexpr ModInt &operator*=(const ll rhs)noexcept{\n return *this*=ModInt(rhs);\n }\n inline constexpr ModInt &operator/=(const ll rhs)noexcept{\n return *this/=ModInt(rhs);\n }\n\n inline constexpr ModInt operator=(const ll x)noexcept{\n a=(x>=0)?(x%MOD):(x%MOD+MOD);\n return *this;\n }\n\n inline constexpr bool operator==(const ModInt p)const noexcept{\n return a==p.a;\n }\n\n inline constexpr bool operator!=(const ModInt p)const noexcept{\n return a!=p.a;\n }\n\n inline constexpr ModInt pow(ll N) const noexcept{\n ModInt ans(1LL),p(a);\n while(N>0){\n if(bitUP(N,0)){\n ans*=p;\n }\n p*=p;\n N>>=1;\n }\n return ans;\n }\n inline constexpr ModInt inverse() const noexcept{\n return pow(MOD-2);\n }\n\n};\ninline constexpr ModInt operator+(const ll &a,const ModInt &b)noexcept{\n return ModInt(a)+=b;\n}\ninline constexpr ModInt operator-(const ll &a,const ModInt &b)noexcept{\n return ModInt(a)-=b;\n}\ninline constexpr ModInt operator*(const ll &a,const ModInt &b)noexcept{\n return ModInt(a)*=b;\n}\ninline constexpr ModInt operator/(const ll &a,const ModInt &b)noexcept{\n return ModInt(a)/=b;\n}\n//cout\ninline ostream &operator<<(ostream &os,const ModInt &p) {\n return os<<p.a;\n}\n\n//cin\ninline istream &operator>>(istream &is,ModInt &p) {\n ll t;\n is>>t;\n p=t;\n return is;\n}\n\nstruct Binominal{\n vector<ModInt> fac,finv,inv; //fac[n]:n! finv:(n!)の逆元\n int sz;\n Binominal(int n=10) :sz(1){\n if(n<=0) n=10;\n init(n);\n }\n inline void init(int n){\n fac.resize(n+1,1);\n finv.resize(n+1,1);\n inv.resize(n+1,1);\n for(int i=sz+1;i<=n;i++){\n fac[i]=fac[i-1]*i;\n inv[i]=MOD-inv[MOD%i]*(MOD/i);\n finv[i]=finv[i-1]*inv[i];\n }\n sz=n;\n }\n //nCk(n,k<=N) をO(1)で求める\n inline ModInt com(int n,int k){\n if(n<k) return ModInt(0);\n if(n<0 || k<0) return ModInt(0);\n if(n>sz) init(n);\n return fac[n]*finv[k]*finv[n-k];\n }\n //nCk(k<=N) をO(k) で求める \n inline ModInt rcom(ll n,int k){\n if(n<0 || k<0 || n<k) return ModInt(0);\n if(k>sz) init(k);\n ModInt ret(1);\n for(int i=0;i<k;i++){\n ret*=n-i;\n }\n ret*=finv[k];\n return ret;\n }\n\n //重複組み合わせ n種類のものから重複を許してk個を選ぶ\n //〇がn個,|がk個\n inline ModInt h(int n,int k){\n return com(n+k-1,k);\n }\n};\nvector<int> Subset(int S,bool zero=false,bool full=false){\n vector<int> ret;\n int now=(S-1)&S;\n if(full and S){\n ret.push_back(S);\n }\n do{\n ret.push_back(now);\n now=(now-1)&S;\n }while(now!=0);\n if(zero){\n ret.push_back(0);\n }\n return ret;\n}\ntemplate<typename T>\nT SUM(const vector<T> &v,int s,int t){\n chmax(s,0);\n chmin(t,int(v.size())-1);\n if(s>t) return 0;\n if(s==0) return v[t];\n else return v[t]-v[s-1];\n}\ntemplate<typename T>\nvoid buildSUM(vector<T> &v){\n for(size_t i=1;i<v.size();i++){\n v[i]+=v[i-1];\n }\n return;\n}\n \n\nvoid solve(){\nwhile(true){\n int N,M,C;\n cin>>N>>M>>C;\n if(N==0) return;\n WeightGraph<int> G(N);\n for(int i=0;i<M;i++){\n int a,b,c; cin>>a>>b>>c;\n a--; b--;\n G[a].emplace_back(b,c);\n }\n auto dp=vmake(N,N+1,MAX);\n using T=tuple<int,int,int>;\n priority_queue<T,vector<T>,greater<T>> que;\n que.emplace(0,0,0);\n dp[0][0]=0;\n while(que.size()){\n int c,now,used;\n tie(c,now,used)=que.top(); que.pop();\n if(c>dp[now][used]) continue;\n\n for(auto e:G[now]){\n \n if(used<N and chmin(dp[e.to][used+1],c)){\n que.emplace(c,e.to,used+1);\n }\n if(chmin(dp[e.to][used],c+e.cost)){\n que.emplace(c+e.cost,e.to,used);\n }\n }\n }\n for(int used=0;used<=N;used++){\n if(dp[N-1][used]<=C){\n cout<<used<<endl;\n break;\n }\n }\n}\n}\n\nint main(){\n bin101();\n int T=1;\n //cin>>T;\n while(T--) solve();\n}", "accuracy": 1, "time_ms": 30, "memory_kb": 3676, "score_of_the_acc": -0.0149, "final_rank": 5 }, { "submission_id": "aoj_1311_5972710", "code_snippet": "#include <bits/stdc++.h>\n//#include <atcoder/all>\n//using namespace atcoder;\n#pragma GCC target (\"avx2\")\n#pragma GCC optimization (\"O3\")\n#pragma GCC optimization (\"unroll-loops\")\nusing namespace std;\n \ntypedef vector<int> VI;\ntypedef vector<VI> VVI;\ntypedef vector<string> VS;\ntypedef pair<int, int> PII;\ntypedef pair<int, int> pii;\ntypedef pair<long long, long long> PLL;\ntypedef pair<int, PII> TIII;\n \ntypedef long long ll;\ntypedef long double ld;\ntypedef unsigned long long ull;\n \n \n#define FOR(i, s, n) for (int i = s; i < (int)n; ++i)\n#define REP(i, n) FOR(i, 0, n)\n#define rep(i, a, b) for (int i = a; i < (b); ++i)\n#define trav(a, x) for (auto &a : x)\n#define all(x) x.begin(), x.end()\n \n#define MOD 1000000007\n \ntemplate<class T1, class T2> inline bool chmax(T1 &a, T2 b) {if (a < b) {a = b; return true;} return false;}\ntemplate<class T1, class T2> inline bool chmin(T1 &a, T2 b) {if (a > b) {a = b; return true;} return false;}\nconst double EPS = 1e-8, PI = acos(-1);\nconst double pi = 3.141592653589793238462643383279;\n//ここから編集 \ntypedef string::const_iterator State;\nll GCD(ll a, ll b){\n return (b==0)?a:GCD(b, a%b);\n}\nll LCM(ll a, ll b){\n return a/GCD(a, b) * b;\n}\ntemplate< int mod >\nstruct ModInt {\n int x;\n \n ModInt() : x(0) {}\n \n ModInt(int64_t y) : x(y >= 0 ? y % mod : (mod - (-y) % mod) % mod) {}\n \n ModInt &operator+=(const ModInt &p) {\n if((x += p.x) >= mod) x -= mod;\n return *this;\n }\n \n ModInt &operator-=(const ModInt &p) {\n if((x += mod - p.x) >= mod) x -= mod;\n return *this;\n }\n \n ModInt &operator*=(const ModInt &p) {\n x = (int) (1LL * x * p.x % mod);\n return *this;\n }\n \n ModInt &operator/=(const ModInt &p) {\n *this *= p.inverse();\n return *this;\n }\n \n ModInt operator-() const { return ModInt(-x); }\n \n ModInt operator+(const ModInt &p) const { return ModInt(*this) += p; }\n \n ModInt operator-(const ModInt &p) const { return ModInt(*this) -= p; }\n \n ModInt operator*(const ModInt &p) const { return ModInt(*this) *= p; }\n \n ModInt operator/(const ModInt &p) const { return ModInt(*this) /= p; }\n \n bool operator==(const ModInt &p) const { return x == p.x; }\n \n bool operator!=(const ModInt &p) const { return x != p.x; }\n \n ModInt inverse() const {\n int a = x, b = mod, u = 1, v = 0, t;\n while(b > 0) {\n t = a / b;\n swap(a -= t * b, b);\n swap(u -= t * v, v);\n }\n return ModInt(u);\n }\n \n ModInt pow(int64_t n) const {\n ModInt ret(1), mul(x);\n while(n > 0) {\n if(n & 1) ret *= mul;\n mul *= mul;\n n >>= 1;\n }\n return ret;\n }\n \n friend ostream &operator<<(ostream &os, const ModInt &p) {\n return os << p.x;\n }\n \n friend istream &operator>>(istream &is, ModInt &a) {\n int64_t t;\n is >> t;\n a = ModInt< mod >(t);\n return (is);\n }\n \n static int get_mod() { return mod; }\n};\n \nusing modint = ModInt< 1000000007 >;\ntemplate< typename T >\nstruct Combination {\n vector< T > _fact, _rfact, _inv;\n \n Combination(int sz) : _fact(sz + 1), _rfact(sz + 1), _inv(sz + 1) {\n _fact[0] = _rfact[sz] = _inv[0] = 1;\n for(int i = 1; i <= sz; i++) _fact[i] = _fact[i - 1] * i;\n _rfact[sz] /= _fact[sz];\n for(int i = sz - 1; i >= 0; i--) _rfact[i] = _rfact[i + 1] * (i + 1);\n for(int i = 1; i <= sz; i++) _inv[i] = _rfact[i] * _fact[i - 1];\n }\n \n inline T fact(int k) const { return _fact[k]; }\n \n inline T rfact(int k) const { return _rfact[k]; }\n \n inline T inv(int k) const { return _inv[k]; }\n \n T P(int n, int r) const {\n if(r < 0 || n < r) return 0;\n return fact(n) * rfact(n - r);\n }\n \n T C(int p, int q) const {\n if(q < 0 || p < q) return 0;\n return fact(p) * rfact(q) * rfact(p - q);\n }\n \n T H(int n, int r) const {\n if(n < 0 || r < 0) return (0);\n return r == 0 ? 1 : C(n + r - 1, r);\n }\n};\n \nll modpow(ll x, ll n, ll mod) {\n ll res = 1;\n x %= mod;\n if(x == 0) return 0;\n while(n) {\n if(n&1) res = (res * x) % mod;\n x = (x * x) % mod;\n n >>= 1;\n }\n return res;\n} \ninline long long mod(long long a, long long m) {\n return (a % m + m) % m;\n}\ntemplate<typename T> \nstruct BIT{\n int N;\n std::vector<T> node;\n BIT(){}\n BIT(int n){\n N = n;\n node.resize(N+10);\n }\n void build(int n) {\n N = n;\n node.resize(N+10);\n }\n /* a: 1-indexed */\n void add(int a, T x){\n for(int i=a; i<(int)node.size(); i += i & -i) node[i] += x;\n }\n\n /* [1, a] */\n T sum(int a){\n T res = 0;\n for(int x=a; x>0; x -= x & -x) res += node[x];\n return res;\n }\n\n /* [l, r] */\n T rangesum(int l, int r){\n return sum(r) - sum(l-1);\n }\n\n /* \n a1+a2+...+aw >= valとなるような最小のwを返す\n @verify https://codeforces.com/contest/992/problem/E\n */\n int lower_bound(T val) {\n if(val < 0) return 0;\n\n int res = 0;\n int n = 1; \n while (n < N) n *= 2;\n\n T tv=0;\n for (int k = n; k > 0; k /= 2) {\n if(res + k < N && node[res + k] < val){\n val -= node[res+k];\n res += k;\n }\n }\n return res+1; \n }\n};\nstruct UnionFind{\n std::vector<int> par;\n std::vector<int> siz;\n\n UnionFind(int sz_): par(sz_), siz(sz_) {\n for(int i=0; i<sz_; ++i) par[i] = i, siz[i] = 1;\n }\n\n void init(int sz_){\n par.resize(sz_);\n siz.resize(sz_);\n for(int i=0; i<sz_; ++i) par[i] = i, siz[i] = 1;\n }\n\n int root(int x){\n if(x == par[x]) return x;\n return par[x] = root(par[x]);\n }\n\n bool merge(int x, int y){\n x = root(x), y = root(y);\n if(x == y) return false;\n if(siz[x] < siz[y]) std::swap(x, y);\n siz[x] += siz[y];\n par[y] = x;\n return true;\n }\n\n bool issame(int x, int y){\n return root(x) == root(y);\n }\n\n int size(int x){\n return siz[root(x)];\n }\n};\nstruct RollingHash{\n\n using ull = unsigned long long;\n const ull mod = (1ULL << 61) - 1;\n const ull MASK30 = (1ULL << 30) - 1;\n const ull MASK31 = (1ULL << 31) - 1;\n\n const ull MASK61 = mod;\n\n ull base;\n int n;\n vector<ull> hash, pow;\n\n RollingHash(const string &s)\n {\n random_device rnd;\n mt19937_64 mt(rnd());\n base = 1001;\n \n n = (int)s.size();\n hash.assign(n+1, 0);\n pow.assign(n+1, 1);\n \n for(int i=0; i<n; i++){\n hash[i+1] = calc_mod(mul(hash[i], base) + s[i]);\n pow[i+1] = calc_mod(mul(pow[i], base));\n }\n }\n\n ull calc_mod(ull x){\n ull xu = x >> 61;\n ull xd = x & MASK61;\n ull res = xu + xd;\n if(res >= mod) res -= mod;\n return res;\n }\n\n ull mul(ull a, ull b){\n ull au = a >> 31;\n ull ad = a & MASK31;\n ull bu = b >> 31;\n ull bd = b & MASK31;\n ull mid = ad * bu + au * bd;\n ull midu = mid >> 30;\n ull midd = mid & MASK30;\n return calc_mod(au * bu * 2 + midu + (midd << 31) + ad * bd);\n }\n\n //[l,r)のハッシュ値\n inline ull get(int l, int r){\n ull res = calc_mod(hash[r] + mod*3-mul(hash[l], pow[r-l]));\n return res;\n }\n //string tのハッシュ値\n inline ull get(string t){\n ull res = 0;\n for(int i=0; i<t.size(); i++){\n res = calc_mod(mul(res, base)+t[i]);\n }\n return res;\n }\n //string s中のtの数をカウント\n inline int count(string t) {\n if(t.size() > n) return 0;\n auto hs = get(t);\n int res = 0;\n for(int i=0; i<n-t.size()+1; i++){\n if(get(i, i+t.size()) == hs) res++; \n }\n return res;\n }\n\n /* \n concat \n @verify https://codeforces.com/problemset/problem/514/C\n */\n inline ull concat(ull h1, ull h2, int h2len){\n return calc_mod(h2 + mul(h1, pow[h2len]));\n }\n\n // LCPを求める S[a:] T[b:]\n inline int LCP(int a, int b){\n int len = min((int)hash.size()-a, (int)hash.size()-b);\n \n int lb = -1, ub = len;\n while(ub-lb>1){\n int mid = (lb+ub)/2;\n\n if(get(a, a+mid) == get(b, b+mid)) lb = mid;\n else ub = mid;\n }\n return lb;\n }\n};\nint dist[110][1010];\n\nint main() { \n cin.tie(0);\n ios::sync_with_stdio(false);\n cout << fixed << setprecision(12);\n \n int n, m, c;\n while(cin >> n >> m >> c) {\n if(n == 0 && m == 0 && c == 0) break;\n vector<vector<pair<int, int>>> g(n);\n REP(i,m) {\n int a, b, d; cin >> a >> b >> d;\n a--; b--;\n g[a].push_back({b, d});\n }\n REP(i,n) REP(j,m+1) dist[i][j] = 1e9;\n // iにいて変更回数がj回, これまでのコストがkであるとき\n priority_queue<pair<ll,pair<ll,ll>>, vector<pair<ll,pair<ll,ll>>>, greater<pair<ll,pair<ll,ll>>>> pq;\n pq.push({0, {0,0}});\n dist[0][0] = 0;\n while(pq.size()) {\n auto[d, p] = pq.top(); pq.pop();\n auto[t, v] = p;\n if(dist[v][t] < d) continue;\n for(auto[u, cost]: g[v]) {\n if(dist[u][t] > dist[v][t] + cost) {\n dist[u][t] = dist[v][t] + cost;\n pq.push({dist[u][t], {t, u}});\n }\n if(dist[u][t+1] > dist[v][t]) {\n dist[u][t+1] = dist[v][t];\n pq.push({dist[u][t+1], {t+1, u}});\n }\n }\n\n }\n int ans = 1e9;\n REP(i,m+1) if(dist[n-1][i] <= c) {\n ans = i;\n break;\n }\n cout << ans << endl;\n }\n return 0;\n}", "accuracy": 1, "time_ms": 140, "memory_kb": 3968, "score_of_the_acc": -0.165, "final_rank": 12 }, { "submission_id": "aoj_1311_5964333", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\nusing T = tuple<int, int, int>;\n\nint main() {\n ios_base::sync_with_stdio(false);\n cin.tie(nullptr);\n cout << fixed << setprecision(15);\n\n while (true) {\n int n, m, c;\n cin >> n >> m >> c;\n if (n == 0 && m == 0 && c == 0) break;\n vector<vector<pair<int, int>>> G(n);\n for (int i = 0; i < m; ++i) {\n int f, t, ci;\n cin >> f >> t >> ci;\n --f, --t;\n G[f].push_back({t, ci});\n }\n vector<vector<int>> dist(n, vector<int>(n, 1e9)); // minimum dist to i after removing j edges\n dist[0][0] = 0;\n priority_queue<T, vector<T>, greater<>> pq;\n pq.push({0, 0, 0});\n while (!pq.empty()) {\n auto [d, v, k] = pq.top();\n pq.pop();\n if (d > dist[v][k]) continue;\n for (auto [u, w] : G[v]) {\n if (k + 1 < n && d < dist[u][k+1]) {\n dist[u][k+1] = d;\n pq.push({d, u, k + 1});\n }\n if (d + w < dist[u][k]) {\n dist[u][k] = d + w;\n pq.push({d + w, u, k});\n }\n }\n }\n for (int j = 0; j < n; ++j) {\n if (dist[n-1][j] <= c) {\n cout << j << \"\\n\";\n break;\n }\n }\n }\n}", "accuracy": 1, "time_ms": 30, "memory_kb": 3472, "score_of_the_acc": -0.0112, "final_rank": 3 }, { "submission_id": "aoj_1311_5959190", "code_snippet": "#pragma GCC ta g(\"avx2\")\n#pragma GCC optimize(\"Ofast\")\n#pragma GCC optimize(\"unroll-loops\")\n#include <bits/stdc++.h>\nusing namespace std;\n#define DEBUG\n#ifdef DEBUG\ntemplate <class T, class U>\nostream &operator<<(ostream &os, const pair<T, U> &p) {\n os << '(' << p.first << ',' << p.second << ')';\n return os;\n}\ntemplate <class T> ostream &operator<<(ostream &os, const vector<T> &v) {\n os << '{';\n for(int i = 0; i < (int)v.size(); i++) {\n if(i) { os << ','; }\n os << v[i];\n }\n os << '}';\n return os;\n}\nvoid debugg() { cerr << endl; }\ntemplate <class T, class... Args>\nvoid debugg(const T &x, const Args &... args) {\n cerr << \" \" << x;\n debugg(args...);\n}\n#define debug(...) \\\n cerr << __LINE__ << \" [\" << #__VA_ARGS__ << \"]: \", debugg(__VA_ARGS__)\n#define dump(x) cerr << __LINE__ << \" \" << #x << \" = \" << (x) << endl\n#else\n#define debug(...) (void(0))\n#define dump(x) (void(0))\n#endif\nusing namespace std;\ntypedef long long ll;\ntypedef vector<ll> vl;\ntypedef vector<vl> vvl;\ntypedef vector<char> vc;\ntypedef vector<string> vs;\ntypedef vector<bool> vb;\ntypedef vector<double> vd;\ntypedef pair<ll,ll> P;\ntypedef pair<int,int> pii;\ntypedef vector<P> vpl;\ntypedef tuple<ll,ll,ll> tapu;\n#define rep(i,n) for(int i=0; i<(n); i++)\n#define REP(i,a,b) for(int i=(a); i<(b); i++)\n#define all(x) (x).begin(), (x).end()\n#define rall(x) (x).rbegin(), (x).rend()\ntemplate<typename T1,typename T2> inline bool chmin(T1 &a,T2 b){\n\tif(a>b){\n\t\ta = b; return true;\n\t}\n\telse return false;\n}\ntemplate<typename T1,typename T2> inline bool chmax(T1 &a,T2 b){\n\tif(a<b){\n\t\ta = b; return true;\n\t}\n\telse return false;\n}\ntemplate<typename T> inline void print(T &a){\n int sz = a.size();\n for(auto itr = a.begin(); itr != a.end(); itr++){\n\t\tcout << *itr;\n sz--;\n if(sz) cout << \" \";\n\t}\n cout << \"\\n\";\n}\ntemplate<typename T1,typename T2> inline void print2(T1 a, T2 b){\n\tcout << a << \" \" << b << \"\\n\";\n}\ntemplate<typename T1,typename T2,typename T3> inline void print3(T1 a, T2 b, T3 c){\n\tcout << a << \" \" << b << \" \" << c << \"\\n\";\n}\nvoid mark() {cout << \"#\" << \"\\n\";}\nll pcount(ll x) {return __builtin_popcountll(x);}\n//#include <atcoder/maxflow>\n//using namespace atcoder;\nconst int inf = (1<<30)-1;\nconst ll linf = 1LL<<61;\nconst int MAX = 5050000;\nint dy[8] = {0,1,0,-1,1,-1,-1,1};\nint dx[8] = {-1,0,1,0,1,-1,1,-1};\nconst double pi = acos(-1);\nconst double eps = 1e-9;\nconst int mod = 1e9 + 7;\n//const int mod = 998244353;\n\nint main(){\n while(1){\n int n,m,C; cin >> n >> m >> C;\n if(!n) break;\n vector<vpl> G(n);\n rep(i,m){\n int u,v,c; cin >> u >> v >> c; u--; v--;\n G[u].emplace_back(v,c);\n }\n int ok = m, ng = 0;\n vvl d(n,vl(m+1,inf));\n while(ok-ng > 1){\n int mid = (ok+ng) / 2;\n rep(i,n) rep(j,m+1) d[i][j] = inf;\n d[0][0] = 0;\n priority_queue<tapu,vector<tapu>,greater<tapu>> pq;\n pq.emplace(0,0,0);\n while(!pq.empty()){\n int c,u,t;\n tie(c,u,t) = pq.top(); pq.pop();\n if(d[u][t] < c) continue;\n for(auto v : G[u]){\n if(chmin(d[v.first][t], c + v.second)){\n pq.emplace(d[v.first][t], v.first, t);\n }\n if(t < mid && chmin(d[v.first][t+1], c)){\n pq.emplace(d[v.first][t+1], v.first, t+1);\n }\n }\n }\n int res = *min_element(all(d[n-1]));\n if(res <= C) ok = mid;\n else ng = mid;\n }\n cout << ok << \"\\n\";\n }\n}", "accuracy": 1, "time_ms": 240, "memory_kb": 6456, "score_of_the_acc": -0.3421, "final_rank": 13 }, { "submission_id": "aoj_1311_5434873", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\n#define ALL(x) begin(x),end(x)\n#define rep(i,n) for(int i=0;i<(n);i++)\n#define debug(v) cout<<#v<<\":\";for(auto x:v){cout<<x<<' ';}cout<<endl;\n#define mod 998244353\nusing ll=long long;\nconst int INF=1000000000;\nconst ll LINF=1001002003004005006ll;\nint dx[]={1,0,-1,0},dy[]={0,1,0,-1};\ntemplate<class T>bool chmax(T &a,const T &b){if(a<b){a=b;return true;}return false;}\ntemplate<class T>bool chmin(T &a,const T &b){if(b<a){a=b;return true;}return false;}\n\nstruct IOSetup{\n IOSetup(){\n cin.tie(0);\n ios::sync_with_stdio(0);\n cout<<fixed<<setprecision(12);\n }\n} iosetup;\n \ntemplate<typename T>\nostream &operator<<(ostream &os,const vector<T>&v){\n for(int i=0;i<(int)v.size();i++) os<<v[i]<<(i+1==(int)v.size()?\"\":\" \");\n return os;\n}\ntemplate<typename T>\nistream &operator>>(istream &is,vector<T>&v){\n for(T &x:v)is>>x;\n return is;\n}\n\nint N,M,C;\n\n// graph template\n// ref : https://ei1333.github.io/library/graph/graph-template.cpp\ntemplate<typename T=int>\nstruct Edge{\n int from,to;\n T w;\n int idx;\n Edge()=default;\n Edge(int from,int to,T w=1,int idx=-1):from(from),to(to),w(w),idx(idx){}\n operator int() const{return to;}\n};\n\ntemplate<typename T=int>\nstruct Graph{\n vector<vector<Edge<T>>> g;\n int V,E;\n Graph()=default;\n Graph(int n):g(n),V(n),E(0){}\n\n size_t size(){\n return g.size();\n }\n void resize(int k){\n g.resize(k);\n }\n inline const vector<Edge<T>> &operator[](int k)const{\n return (g.at(k));\n }\n inline vector<Edge<T>> &operator[](int k){\n return (g.at(k));\n }\n void add_directed_edge(int from,int to,T cost=1){\n g[from].emplace_back(from,to,cost,E++);\n }\n void add_edge(int from,int to,T cost=1){\n g[from].emplace_back(from,to,cost,E);\n g[to].emplace_back(to,from,cost,E++);\n }\n void read(int m,int pad=-1,bool weighted=false,bool directed=false){\n for(int i=0;i<m;i++){\n int u,v;cin>>u>>v;\n u+=pad,v+=pad;\n T w=T(1);\n if(weighted) cin>>w;\n if(directed) add_directed_edge(u,v,w);\n else add_edge(u,v,w);\n }\n }\n};\n\nvoid solve(){\n Graph<int> G(N);\n G.read(M,-1,true,true);\n\n vector<vector<int>> dp(N,vector<int>(N+1,INF));\n dp[0][0]=0;\n using P=pair<int,int>;\n\n priority_queue<pair<int,P>,vector<pair<int,P>>,greater<pair<int,P>>> que;\n que.emplace(0,P(0,0));\n\n while(!que.empty()){\n auto [d,p]=que.top();que.pop();\n\n if(dp[p.first][p.second]<d) continue;\n\n for(auto &e:G[p.first]){\n if(p.second<N)if(chmin(dp[e][p.second+1],d)) que.emplace(d,P(e,p.second+1));\n if(chmin(dp[e][p.second],d+e.w)) que.emplace(d+e.w,P(e,p.second));\n }\n }\n\n for(int k=0;k<=N;k++){\n if(dp[N-1][k]<=C){\n cout<<k<<endl;\n return ;\n }\n }\n cout<<-1<<endl;\n return ;\n}\n\nsigned main(){\n while(cin>>N>>M>>C,N and M) solve();\n return 0;\n}", "accuracy": 1, "time_ms": 30, "memory_kb": 3628, "score_of_the_acc": -0.0141, "final_rank": 4 }, { "submission_id": "aoj_1311_5434869", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\n#define ALL(x) begin(x),end(x)\n#define rep(i,n) for(int i=0;i<(n);i++)\n#define debug(v) cout<<#v<<\":\";for(auto x:v){cout<<x<<' ';}cout<<endl;\n#define mod 998244353\nusing ll=long long;\nconst int INF=1000000000;\nconst ll LINF=1001002003004005006ll;\nint dx[]={1,0,-1,0},dy[]={0,1,0,-1};\ntemplate<class T>bool chmax(T &a,const T &b){if(a<b){a=b;return true;}return false;}\ntemplate<class T>bool chmin(T &a,const T &b){if(b<a){a=b;return true;}return false;}\n\nstruct IOSetup{\n IOSetup(){\n cin.tie(0);\n ios::sync_with_stdio(0);\n cout<<fixed<<setprecision(12);\n }\n} iosetup;\n \ntemplate<typename T>\nostream &operator<<(ostream &os,const vector<T>&v){\n for(int i=0;i<(int)v.size();i++) os<<v[i]<<(i+1==(int)v.size()?\"\":\" \");\n return os;\n}\ntemplate<typename T>\nistream &operator>>(istream &is,vector<T>&v){\n for(T &x:v)is>>x;\n return is;\n}\n\nint N,M,C;\n\n// graph template\n// ref : https://ei1333.github.io/library/graph/graph-template.cpp\ntemplate<typename T=int>\nstruct Edge{\n int from,to;\n T w;\n int idx;\n Edge()=default;\n Edge(int from,int to,T w=1,int idx=-1):from(from),to(to),w(w),idx(idx){}\n operator int() const{return to;}\n};\n\ntemplate<typename T=int>\nstruct Graph{\n vector<vector<Edge<T>>> g;\n int V,E;\n Graph()=default;\n Graph(int n):g(n),V(n),E(0){}\n\n size_t size(){\n return g.size();\n }\n void resize(int k){\n g.resize(k);\n }\n inline const vector<Edge<T>> &operator[](int k)const{\n return (g.at(k));\n }\n inline vector<Edge<T>> &operator[](int k){\n return (g.at(k));\n }\n void add_directed_edge(int from,int to,T cost=1){\n g[from].emplace_back(from,to,cost,E++);\n }\n void add_edge(int from,int to,T cost=1){\n g[from].emplace_back(from,to,cost,E);\n g[to].emplace_back(to,from,cost,E++);\n }\n void read(int m,int pad=-1,bool weighted=false,bool directed=false){\n for(int i=0;i<m;i++){\n int u,v;cin>>u>>v;\n u+=pad,v+=pad;\n T w=T(1);\n if(weighted) cin>>w;\n if(directed) add_directed_edge(u,v,w);\n else add_edge(u,v,w);\n }\n }\n};\n\ntemplate<typename T>\nvector<vector<T>> WarshallFloyd(vector<vector<T>> mat, T inf=1000000100){\n int n=(int)mat.size();\n for(int k=0;k<n;k++)for(int i=0;i<n;i++)for(int j=0;j<n;j++)\n if(mat[i][k]<inf and mat[k][j]<inf) mat[i][j]=min(mat[i][j],mat[i][k]+mat[k][j]);\n return mat;\n}\n\nvoid solve(){\n vector<Edge<int>> E;\n vector<vector<int>> W(N,vector<int>(N,INF));\n rep(i,M){\n int u,v,c;cin>>u>>v>>c;\n u--,v--;\n E.emplace_back(u,v,c,0);\n chmin(W[u][v],c);\n }\n\n sort(ALL(E),[](Edge<int> a,Edge<int> b){return a.w<b.w;});\n reverse(ALL(E));\n\n W=WarshallFloyd(W);\n\n\n for(auto &e:E){\n chmin(W[e.from][e.to],0);\n e.idx=1;\n\n for(int i=0;i<N;i++)for(int j=0;j<N;j++)\n chmin(W[i][j],W[i][e.from]+W[e.to][j]);\n\n if(W[0][N-1]<=C) break;\n }\n \n Graph<int> G(N);\n for(auto &e:E) G.g[e.from].push_back(e);\n \n\n vector<vector<int>> dp(N,vector<int>(N+1,INF));\n dp[0][0]=0;\n using P=pair<int,int>;\n\n priority_queue<pair<int,P>,vector<pair<int,P>>,greater<pair<int,P>>> que;\n que.emplace(0,P(0,0));\n\n while(!que.empty()){\n auto [d,p]=que.top();que.pop();\n\n if(dp[p.first][p.second]<d) continue;\n\n for(auto &e:G[p.first]){\n if(p.second<N)if(chmin(dp[e][p.second+1],d)) que.emplace(d,P(e,p.second+1));\n if(chmin(dp[e][p.second],d+e.w)) que.emplace(d+e.w,P(e,p.second));\n }\n }\n\n for(int k=0;k<=N;k++){\n if(dp[N-1][k]<=C){\n cout<<k<<endl;\n return ;\n }\n }\n cout<<-1<<endl;\n return ;\n}\n\nsigned main(){\n while(cin>>N>>M>>C,N and M) solve();\n return 0;\n}", "accuracy": 1, "time_ms": 120, "memory_kb": 3664, "score_of_the_acc": -0.1331, "final_rank": 11 }, { "submission_id": "aoj_1311_5312019", "code_snippet": "#include <bits/stdc++.h>\n\nusing namespace std;\n\n#define eps 1e-9\n#define INF 2000000000 // 2e9\n#define LLINF 2000000000000000000ll // 2e18 (llmax:9e18)\n#define all(x) (x).begin(), (x).end()\n#define sq(x) ((x) * (x))\n\n#define rep(i, x) for (int i = 0; i < (int)(x); i++)\n#define drep(i, x) for (int i = ((int)(x)-1); i >= 0; i--)\n\n#define popcount __builtin_popcount\n#define next __next\n#define prev __prev\n\n#ifndef LOCAL\n#define dmp(...) ;\n#else\n#define dmp(...) \\\n cerr << \"[ \" << #__VA_ARGS__ << \" ] : \" << dump_str(__VA_ARGS__) << endl\n#endif\n\n// ---------------- Utility ------------------\n\ntemplate <class T>\nbool chmin(T &a, const T &b) {\n if (a <= b) return false;\n a = b;\n return true;\n}\ntemplate <class T>\nbool chmax(T &a, const T &b) {\n if (a >= b) return false;\n a = b;\n return true;\n}\n\ntemplate <class T>\nusing MaxHeap = priority_queue<T>;\ntemplate <class T>\nusing MinHeap = priority_queue<T, vector<T>, greater<T>>;\n\ntemplate <class T>\nvector<T> vect(int len, T elem) {\n return vector<T>(len, elem);\n}\n\n// ----------------- Input -------------------\n\ntemplate <class T, class U>\nistream &operator>>(istream &is, pair<T, U> &p) {\n return is >> p.first >> p.second;\n}\n\ntemplate <class T>\nistream &operator>>(istream &is, vector<T> &vec) {\n for (int i = 0; i < vec.size(); i++) is >> vec[i];\n return is;\n}\n\n// ----------------- Output ------------------\n\ntemplate <class T, class U>\nostream &operator<<(ostream &os, const pair<T, U> &p) {\n return os << p.first << ',' << p.second;\n}\n\ntemplate <class T>\nostream &operator<<(ostream &os, const vector<T> &v) {\n for (const T &e : v) os << e << \" \";\n return os;\n}\n\ntemplate <class T>\nostream &operator<<(ostream &os, const deque<T> &d) {\n for (const T &e : d) os << e << \" \";\n return os;\n}\n\ntemplate <class T>\nostream &operator<<(ostream &os, const set<T> &s) {\n os << \"{ \";\n for (const T &e : s) os << e << \" \";\n return os << \"}\";\n}\n\ntemplate <class T, class U>\nostream &operator<<(ostream &os, const map<T, U> &m) {\n os << \"{ \";\n for (const auto &[key, val] : m) os << \"( \" << key << \" -> \" << val << \" ) \";\n return os << \"}\";\n}\n\ntemplate <class TupleTy, size_t... I>\nvoid dump_tuple(ostream &os, const TupleTy t, std::index_sequence<I...>) {\n (..., (os << (I == 0 ? \"\" : \",\") << std::get<I>(t)));\n}\n\ntemplate <class... Args>\nostream &operator<<(ostream &os, const tuple<Args...> &t) {\n os << \"(\";\n dump_tuple(os, t, std::make_index_sequence<sizeof...(Args)>());\n return os << \")\";\n}\n\nvoid dump_str_rec(ostringstream &) {}\n\ntemplate <class Head, class... Tail>\nvoid dump_str_rec(ostringstream &oss, Head &&head, Tail &&... tail) {\n oss << \", \" << head;\n dump_str_rec(oss, forward<Tail>(tail)...);\n}\n\ntemplate <class T, class... U>\nstring dump_str(T &&arg, U &&... args) {\n ostringstream oss;\n oss << arg;\n dump_str_rec(oss, forward<U>(args)...);\n return oss.str();\n}\n\n// --------------- Fast I/O ------------------\n\nvoid fastio() {\n cin.tie(0);\n ios::sync_with_stdio(0);\n cout << fixed << setprecision(20);\n}\n\n// ------------------ ACL --------------------\n\n// #include <atcoder/modint>\n// constexpr int MOD = 998244353;\n// constexpr int MOD = 1000000007;\n// using mint = atcoder::static_modint<MOD>;\n// ostream &operator<<(ostream &os, const mint &v) {\n// return os << v.val();\n// }\n\n// ------------ End of template --------------\n\n#define endl \"\\n\"\nusing ll = long long;\nusing pii = pair<int, int>;\n\ntemplate <class Cost = int>\nstruct edge {\n int from, to;\n Cost cost;\n edge() {}\n edge(int from, int to, Cost cost) : from(from), to(to), cost(cost) {}\n\n // for debug\n friend ostream &operator<<(ostream &os, const edge &e) {\n os << e.from << \" -> \" << e.to << \" : \" << e.cost << \", \";\n return os;\n }\n};\n\ntemplate <class Cost = int>\nstruct Graph {\n int V;\n vector<vector<edge<Cost>>> G;\n Graph() {}\n Graph(int V) : V(V) { G.resize(V); }\n void add_edge(int a, int b, Cost cost = Cost(0)) {\n G[a].push_back(edge<Cost>(a, b, cost));\n }\n void add_edge_undirected(int a, int b, Cost cost = Cost(0)) {\n G[a].push_back(edge<Cost>(a, b, cost));\n G[b].push_back(edge<Cost>(b, a, cost));\n }\n size_t size() const { return G.size(); }\n const vector<edge<Cost>> &operator[](int id) const { return G[id]; }\n\n // for debug\n friend ostream &operator<<(ostream &os, const Graph g) {\n for (int i = 0; i < g.size(); i++) {\n os << g[i];\n if (i + 1 < g.size()) os << endl;\n }\n return os;\n }\n};\n\n// verified\n// https://atcoder.jp/contests/dwacon6th-final-open/submissions/10023433\ntemplate <class Cost>\nvector<Cost> dijkstra(Graph<Cost> &g, int s) {\n assert(0 <= s && s < g.size());\n vector<Cost> dist;\n dist.assign(g.size(), numeric_limits<Cost>::max());\n dist[s] = Cost(0);\n MinHeap<pair<Cost, int>> q;\n q.push(make_pair(Cost(0), s));\n while (!q.empty()) {\n auto a = q.top();\n q.pop();\n int v = a.second;\n if (dist[v] < a.first) continue;\n for (auto e : g[v]) {\n if (dist[e.to] > dist[v] + e.cost) {\n dist[e.to] = dist[v] + e.cost;\n q.push(make_pair(dist[e.to], e.to));\n }\n }\n }\n return dist;\n}\n\nbool solve() {\n int n, m, c;\n cin >> n >> m >> c;\n if (n + m + c == 0) return false;\n Graph<int> g(n * (m + 1));\n for (int i = 0; i < m; i++) {\n int a, b, cost;\n cin >> a >> b >> cost;\n a--;\n b--;\n for (int j = 0; j < m; j++) {\n g.add_edge(j * n + a, j * n + b, cost);\n g.add_edge(j * n + a, (j + 1) * n + b, 0);\n }\n }\n auto dist = dijkstra(g, 0);\n for (int i = 0; i <= m; i++) {\n if (dist[i * n + n - 1] <= c) {\n cout << i << endl;\n return true;\n }\n }\n assert(false);\n return true;\n}\n\nint main() {\n fastio();\n // solve();\n while (solve()) {}\n // int t; cin >> t; while(t--)solve();\n\n // int t; cin >> t;\n // for(int i=1;i<=t;i++){\n // cout << \"Case #\" << i << \": \";\n // solve();\n // }\n return 0;\n}", "accuracy": 1, "time_ms": 680, "memory_kb": 43084, "score_of_the_acc": -1.5914, "final_rank": 18 }, { "submission_id": "aoj_1311_5136684", "code_snippet": "//#pragma GCC optimize(\"Ofast\")\n#include <bits/stdc++.h>\nusing namespace std;\ntypedef long long int ll;\ntypedef unsigned long long ull;\n \nmt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());\nll myRand(ll B) {\n\treturn (ull)rng() % B;\n}\n\nint main(){\n cin.tie(nullptr);\n ios::sync_with_stdio(false);\n int n,m,c;\n while(cin >> n >> m >> c,n+m+c){\n vector<vector<pair<int,int>>> g(n);\n for(int i=0;i<m;i++){\n int x,y,w; cin >> x >> y >> w;\n x--; y--;\n g[x].push_back({y,w});\n }\n vector<vector<int>> d(n,vector<int>(n+1,1e9));\n d[0][0]=0;\n priority_queue<pair<int,pair<int,int>>> pq;\n pq.push(make_pair(0,make_pair(0,0)));\n while(pq.size()){\n auto p=pq.top(); pq.pop();\n if(-p.first!=d[p.second.first][p.second.second])continue;\n for(auto to:g[p.second.first]){\n int t=to.first;\n if(d[t][p.second.second]>-p.first+to.second){\n d[t][p.second.second]=-p.first+to.second;\n pq.push(make_pair(-d[t][p.second.second],make_pair(t,p.second.second)));\n }\n if(p.second.second+1>n)continue;\n if(d[t][p.second.second+1]>-p.first){\n d[t][p.second.second+1]=-p.first;\n pq.push(make_pair(p.first,make_pair(t,p.second.second+1)));\n }\n }\n }\n for(int i=0;i<=n;i++){\n if(c>=d[n-1][i]){\n cout << i << endl;\n break;\n }\n }\n } \n}", "accuracy": 1, "time_ms": 60, "memory_kb": 3272, "score_of_the_acc": -0.047, "final_rank": 8 }, { "submission_id": "aoj_1311_4996007", "code_snippet": "#include<cstdio>\n#include<vector>\n#include<queue>\n#include<map>\n#include<algorithm>\nusing namespace std;\nusing P = pair<int, int>;\nusing Q = pair<int, P>;\nconstexpr int INF = 1000000000;\n\nint n, m, c, x, y, z;\nvector<P> e[100];\nint mn[100][101];\n\nint main() {\n while (scanf(\"%d%d%d\", &n, &m, &c), n) {\n for (int i = 0; i < n; i++) {\n e[i].clear();\n for (int j = 0; j <= n; j++) mn[i][j] = INF;\n }\n for (int i = 0; i < m; i++) {\n scanf(\"%d%d%d\", &x, &y, &z);\n x--; y--;\n e[x].push_back(P(y, z));\n }\n priority_queue<Q, vector<Q>, greater<Q>> pq; // (cost, P(num, minas))\n pq.push(Q(0, P(0, 0)));\n for (int i = 0; i <= n; i++) mn[0][i] = 0;\n while (!pq.empty()) {\n Q tmp = pq.top(); pq.pop();\n int num = tmp.second.first, minas = tmp.second.second;\n if (mn[num][minas] < tmp.first) continue;\n for (P p : e[num]) {\n if (mn[p.first][minas] > mn[num][minas] + p.second) {\n mn[p.first][minas] = mn[num][minas] + p.second;\n pq.push(Q(mn[p.first][minas], P(p.first, minas)));\n }\n if (minas < n && mn[p.first][minas+1] > mn[num][minas]) {\n if (mn[num][minas] >= mn[p.first][minas]) continue;\n mn[p.first][minas+1] = mn[num][minas];\n pq.push(Q(mn[p.first][minas+1], P(p.first, minas+1)));\n }\n }\n }\n int ans = 0;\n while (mn[n-1][ans] > c) ans++;\n printf(\"%d\\n\", ans);\n }\n}", "accuracy": 1, "time_ms": 30, "memory_kb": 2860, "score_of_the_acc": 0, "final_rank": 1 } ]
aoj_1315_cpp
Problem A: Gift from the Goddess of Programming The goddess of programming is reviewing a thick logbook, which is a yearly record of visitors to her holy altar of programming. The logbook also records her visits at the altar. The altar attracts programmers from all over the world because one visitor is chosen every year and endowed with a gift of miracle programming power by the goddess. The endowed programmer is chosen from those programmers who spent the longest time at the altar during the goddess's presence. There have been enthusiastic visitors who spent very long time at the altar but failed to receive the gift because the goddess was absent during their visits. Now, your mission is to write a program that finds how long the programmer to be endowed stayed at the altar during the goddess's presence. Input The input is a sequence of datasets. The number of datasets is less than 100. Each dataset is formatted as follows. n M 1 /D 1 h 1 : m 1 e 1 p 1 M 2 /D 2 h 2 : m 2 e 2 p 2 . . . M n /D n h n : m n e n p n The first line of a dataset contains a positive even integer, n ≤ 1000, which denotes the number of lines of the logbook. This line is followed by n lines of space-separated data, where M i /D i identifies the month and the day of the visit, h i : m i represents the time of either the entrance to or exit from the altar, e i is either I for entrance, or O for exit, and p i identifies the visitor. All the lines in the logbook are formatted in a fixed-column format. Both the month and the day in the month are represented by two digits. Therefore April 1 is represented by 04/01 and not by 4/1. The time is described in the 24-hour system, taking two digits for the hour, followed by a colon and two digits for minutes, 09:13 for instance and not like 9:13. A programmer is identified by an ID, a unique number using three digits. The same format is used to indicate entrance and exit of the goddess, whose ID is 000. All the lines in the logbook are sorted in ascending order with respect to date and time. Because the altar is closed at midnight, the altar is emptied at 00:00. You may assume that each time in the input is between 00:01 and 23:59, inclusive. A programmer may leave the altar just after entering it. In this case, the entrance and exit time are the same and the length of such a visit is considered 0 minute. You may assume for such entrance and exit records, the line that corresponds to the entrance appears earlier in the input than the line that corresponds to the exit. You may assume that at least one programmer appears in the logbook. The end of the input is indicated by a line containing a single zero. Output For each dataset, output the total sum of the blessed time of the endowed programmer. The blessed time of a programmer is the length of his/her stay at the altar during the presence of the goddess. The endowed programmer is the one whose total blessed time is the longest among all the programmers. The output should be represented ...(truncated)
[ { "submission_id": "aoj_1315_4851862", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nconst vector<int> convert={1440,60,1};\nvector<int> month={0,31,28,31,30,31,30,31,31,30,31,30};\n\nint time(string s,string t){\n int M=(s[0]-'0')*10+(s[1]-'0')-1;\n int D=(s[3]-'0')*10+(s[4]-'0')-1;\n int h=(t[0]-'0')*10+(t[1]-'0');\n int m=(t[3]-'0')*10+(t[4]-'0');\n return convert[0]*(month[M]+D)+convert[1]*h+convert[2]*m;\n}\n\nconst int MAX_T=6e5+10,MAX_N=1010;\nint imos[MAX_T];\n\nvoid solve(int n){\n vector<string> S(n),T(n);\n vector<char> IO(n);\n vector<int> id(n);\n for (int i=0;i<n;++i){\n string ID; cin >> S[i] >> T[i] >> IO[i] >> ID;\n id[i]=stoi(ID);\n }\n\n vector<int> sum(MAX_N,0),pre(MAX_N,0);\n for (int i=0;i<MAX_T;++i) imos[i]=0;\n for (int i=0;i<n;++i){\n if (id[i]) continue;\n int t=time(S[i],T[i]);\n if (IO[i]=='I') ++imos[t+1];\n else --imos[t+1];\n }\n for (int i=0;i<MAX_T-1;++i) imos[i+1]+=imos[i];\n for (int i=0;i<MAX_T-1;++i) imos[i+1]+=imos[i];\n\n for (int i=0;i<n;++i){\n if (!id[i]) continue;\n int t=time(S[i],T[i]);\n if (IO[i]=='I') pre[id[i]]=t;\n else sum[id[i]]+=imos[t]-imos[pre[id[i]]];\n }\n\n int ans=0;\n for (int i=0;i<MAX_N;++i) ans=max(ans,sum[i]);\n\n cout << ans << '\\n';\n}\n\nint main(){\n cin.tie(0);\n ios::sync_with_stdio(false);\n for (int i=0;i<11;++i) month[i+1]+=month[i];\n int n;\n while(cin >> n,n){\n solve(n);\n }\n}", "accuracy": 1, "time_ms": 60, "memory_kb": 5644, "score_of_the_acc": -0.3098, "final_rank": 14 }, { "submission_id": "aoj_1315_4851861", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n#define LOCAL\n#pragma region Macros\ntypedef long long ll;\n#define ALL(x) (x).begin(),(x).end()\nconst long long MOD=1000000007;\n// const long long MOD=998244353;\nconst int INF=1e9;\nconst long long IINF=1e18;\nconst int dx[4]={1,0,-1,0},dy[4]={0,1,0,-1};\nconst char dir[4]={'D','R','U','L'};\n\ntemplate<typename T>\nistream &operator>>(istream &is,vector<T> &v){\n for (T &x:v) is >> x;\n return is;\n}\ntemplate<typename T>\nostream &operator<<(ostream &os,const vector<T> &v){\n for (int i=0;i<v.size();++i){\n os << v[i] << (i+1==v.size()?\"\": \" \");\n }\n return os;\n}\ntemplate<typename T,typename U>\nostream &operator<<(ostream &os,const pair<T,U> &p){\n os << '(' << p.first << ',' << p.second << ')';\n return os;\n}\ntemplate<typename T,typename U>\nostream &operator<<(ostream &os,const map<T,U> &m){\n os << '{';\n for (auto itr=m.begin();itr!=m.end();++itr){\n os << '(' << itr->first << ',' << itr->second << ')';\n if (++itr!=m.end()) os << ',';\n --itr;\n }\n os << '}';\n return os;\n}\ntemplate<typename T>\nostream &operator<<(ostream &os,const set<T> &s){\n os << '{';\n for (auto itr=s.begin();itr!=s.end();++itr){\n os << *itr;\n if (++itr!=s.end()) os << ',';\n --itr;\n }\n os << '}';\n return os;\n}\n\nvoid debug_out(){cerr << '\\n';}\ntemplate<class Head,class... Tail>\nvoid debug_out(Head&& head,Tail&&... tail){\n cerr << head;\n if (sizeof...(Tail)>0) cerr << \", \";\n debug_out(move(tail)...);\n}\n#ifdef LOCAL\n#define debug(...) cerr << \" \";\\\ncerr << #__VA_ARGS__ << \" :[\" << __LINE__ << \":\" << __FUNCTION__ << \"]\" << '\\n';\\\ncerr << \" \";\\\ndebug_out(__VA_ARGS__)\n#else\n#define debug(...) 42\n#endif\n\ntemplate<typename T> T gcd(T x,T y){return y!=0?gcd(y,x%y):x;}\ntemplate<typename T> T lcm(T x,T y){return x/gcd(x,y)*y;}\n\ntemplate<class T1,class T2> inline bool chmin(T1 &a,T2 b){\n if (a>b){a=b; return true;} return false;\n}\ntemplate<class T1,class T2> inline bool chmax(T1 &a,T2 b){\n if (a<b){a=b; return true;} return false;\n}\n#pragma endregion\n\nconst vector<int> convert={1440,60,1};\nvector<int> month={0,31,28,31,30,31,30,31,31,30,31,30};\n\nint time(string s,string t){\n int M=(s[0]-'0')*10+(s[1]-'0')-1;\n int D=(s[3]-'0')*10+(s[4]-'0')-1;\n int h=(t[0]-'0')*10+(t[1]-'0');\n int m=(t[3]-'0')*10+(t[4]-'0');\n return convert[0]*(month[M]+D)+convert[1]*h+convert[2]*m;\n}\n\nconst int MAX_T=6e5+10,MAX_N=1010;\nint imos[MAX_T];\n\nvoid solve(int n){\n vector<string> S(n),T(n);\n vector<char> IO(n);\n vector<int> id(n);\n for (int i=0;i<n;++i){\n string ID; cin >> S[i] >> T[i] >> IO[i] >> ID;\n id[i]=stoi(ID);\n }\n\n vector<int> sum(MAX_N,0),pre(MAX_N,0);\n for (int i=0;i<MAX_T;++i) imos[i]=0;\n for (int i=0;i<n;++i){\n if (id[i]) continue;\n int t=time(S[i],T[i]);\n if (IO[i]=='I') ++imos[t+1];\n else --imos[t+1];\n }\n for (int i=0;i<MAX_T-1;++i) imos[i+1]+=imos[i];\n for (int i=0;i<MAX_T-1;++i) imos[i+1]+=imos[i];\n\n for (int i=0;i<n;++i){\n if (!id[i]) continue;\n int t=time(S[i],T[i]);\n if (IO[i]=='I') pre[id[i]]=t;\n else sum[id[i]]+=imos[t]-imos[pre[id[i]]];\n }\n\n int ans=0;\n for (int i=0;i<MAX_N;++i) ans=max(ans,sum[i]);\n\n cout << ans << '\\n';\n}\n\nint main(){\n cin.tie(0);\n ios::sync_with_stdio(false);\n for (int i=0;i<11;++i) month[i+1]+=month[i];\n int n;\n while(cin >> n,n){\n solve(n);\n }\n}", "accuracy": 1, "time_ms": 60, "memory_kb": 5612, "score_of_the_acc": -0.3068, "final_rank": 13 }, { "submission_id": "aoj_1315_4813376", "code_snippet": "#include <iostream>\n#include <string>\n#include <cstdlib>\n#include <cmath>\n#include <vector>\n#include <unordered_map>\n#include <map>\n#include <set>\n#include <algorithm>\n#include <queue>\n#include <stack>\n#include <functional>\n#include <bitset>\n#include <assert.h>\n#include <unordered_map>\n#include <fstream>\n#include <ctime>\n#include <complex>\nusing namespace std;\ntypedef long long ll;\ntypedef vector<ll> vl;\ntypedef vector<vl> vvl;\ntypedef vector<char> vc;\ntypedef vector<string> vs;\ntypedef vector<bool> vb;\ntypedef vector<double> vd;\ntypedef pair<ll,ll> P;\ntypedef pair<int,int> pii;\ntypedef vector<P> vpl;\ntypedef tuple<ll,ll,ll> tapu;\n#define rep(i,n) for(int i=0; i<(n); i++)\n#define REP(i,a,b) for(int i=(a); i<(b); i++)\n#define all(x) (x).begin(), (x).end()\n#define rall(x) (x).rbegin(), (x).rend()\nconst int inf = 1<<30;\nconst ll linf = 1LL<<62;\nconst int MAX = 1020000;\nll dy[8] = {1,-1,0,0,1,-1,1,-1};\nll dx[8] = {0,0,1,-1,1,-1,-1,1};\nconst double pi = acos(-1);\nconst double eps = 1e-7;\ntemplate<typename T1,typename T2> inline bool chmin(T1 &a,T2 b){\n\tif(a>b){\n\t\ta = b; return true;\n\t}\n\telse return false;\n}\ntemplate<typename T1,typename T2> inline bool chmax(T1 &a,T2 b){\n\tif(a<b){\n\t\ta = b; return true;\n\t}\n\telse return false;\n}\ntemplate<typename T> inline void print(T &a){\n for(auto itr = a.begin(); itr != a.end(); itr++){\n\t\tcout << *itr << \" \";\n\t}\n cout << \"\\n\";\n}\ntemplate<typename T1,typename T2> inline void print2(T1 a, T2 b){\n\tcout << \"debug: \" << a << \" \" << b << \"\\n\";\n}\ntemplate<typename T1,typename T2,typename T3> inline void print3(T1 a, T2 b, T3 c){\n\tcout << \"debug: \" << a << \" \" << b << \" \" << c << \"\\n\";\n}\nvoid mark() {cout << \"#\" << \"\\n\";}\nll pcount(ll x) {return __builtin_popcountll(x);}\nconst int mod = 1e9 + 7;\n//const int mod = 998244353;\n\nint main(){\n\twhile(1){\n\t\tint n; cin >> n;\n\t\tif(!n) break;\n\t\tmap<int,map<int,vector<pii>>> mp;\n\t\trep(i,n){\n\t\t\tint a,b,c,d,e; char f;\n\t\t\tscanf(\"%d/%d %d:%d %c %d\",&a,&b,&c,&d,&f,&e);\n\t\t\tint s = f == 'I';\n\t\t\tmp[a*31+b][c*60+d].emplace_back(s, e);\n\t\t}\n\t\tvector<int> t(n,0);\n\t\tset<int> se;\n\t\tfor(auto i : mp){\n\t\t\trep(j,1440){\n\t\t\t\tfor(auto k : i.second[j]){\n\t\t\t\t\tif(k.first) se.insert(k.second);\n\t\t\t\t\telse se.erase(k.second);\n\t\t\t\t}\n\t\t\t\tif(se.count(0) == 1){\n\t\t\t\t\tfor(auto l : se) t[l]++;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\tt[0] = 0;\n\t\tcout << *max_element(all(t)) << \"\\n\";\n\t}\n}", "accuracy": 1, "time_ms": 30, "memory_kb": 3424, "score_of_the_acc": -0.0596, "final_rank": 10 }, { "submission_id": "aoj_1315_4773668", "code_snippet": "#include \"iostream\"\n#include \"climits\"\n#include \"list\"\n#include \"queue\"\n#include \"stack\"\n#include \"set\"\n#include \"functional\"\n#include \"algorithm\"\n#include \"string\"\n#include \"map\"\n#include \"unordered_map\"\n#include \"unordered_set\"\n#include \"iomanip\"\n#include \"cmath\"\n#include \"random\"\n#include \"bitset\"\n#include \"cstdio\"\n#include \"numeric\"\n#include \"cassert\"\n#include \"ctime\"\n\nusing namespace std;\n\nconstexpr long long int MOD = 1000000007;\n//constexpr int MOD = 1000000007;\n//constexpr int MOD = 998244353;\n//constexpr long long int MOD = 998244353;\nconstexpr double EPS = 1e-12;\n\n//int N, M, K, T, H, W, L, R;\nlong long int N, M, K, T, H, W, L, R;\n\nint second(string s) {\n\tfor (auto &i : s)i -= '0';\n\tint ret = s[0];\n\tret *= 10;\n\tret += s[1];\n\tret *= 6;\n\tret += s[3];\n\tret *= 10;\n\tret += s[4];\n\treturn ret;\n}\n\nint main() {\n\tios::sync_with_stdio(false);\n\tcin.tie(0);\n\n\twhile (cin >> N, N) {\n\t\tmap<string, vector<int>>mp;\n\t\tvector<string>a(N);\n\t\tvector<string>b(N);\n\t\tvector<char>c(N);\n\t\tvector<string>d(N);\n\t\tfor (int i = 0; i < N; i++) {\n\t\t\tcin >> a[i] >> b[i] >> c[i] >> d[i];\n\t\t\tmp[a[i]].push_back(i);\n\t\t}\n\t\tmap<string, int>score;\n\t\tfor (auto i : mp) {\n\t\t\tvector<int>v(1441);\n\t\t\tfor (auto j : i.second) {\n\t\t\t\tif (d[j] == \"000\") {\n\t\t\t\t\tif (c[j] == 'I') {\n\t\t\t\t\t\tv[second(b[j])]++;\n\t\t\t\t\t}\n\t\t\t\t\telse {\n\t\t\t\t\t\tv[second(b[j])]--;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t\tfor (int j = 1; j <= 1440; j++) {\n\t\t\t\tv[j] += v[j - 1];\n\t\t\t}\n\t\t\tfor (auto j : i.second) {\n\t\t\t\tif (d[j] != \"000\") {\n\t\t\t\t\tif (c[j] == 'I') {\n\t\t\t\t\t\tfor (int k = second(b[j]); k <= 1440; k++) {\n\t\t\t\t\t\t\tscore[d[j]] += v[k];\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t\telse {\n\t\t\t\t\t\tfor (int k = second(b[j]); k <= 1440; k++) {\n\t\t\t\t\t\t\tscore[d[j]] -= v[k];\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\tint ans = 0;\n\t\tfor (auto i : score) {\n\t\t\tans = max(ans, i.second);\n\t\t}\n\t\tcout << ans << endl;\n\t}\n}", "accuracy": 1, "time_ms": 80, "memory_kb": 3380, "score_of_the_acc": -0.1269, "final_rank": 11 }, { "submission_id": "aoj_1315_4745873", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nint main(){\n while (1){\n int n;\n cin >> n;\n if (n == 0){\n break;\n }\n vector<int> M(n), D(n), h(n), m(n);\n vector<char> e(n);\n vector<int> p(n);\n for (int i = 0; i < n; i++){\n char c1, c2;\n cin >> M[i] >> c1 >> D[i] >> h[i] >> c2 >> m[i] >> e[i] >> p[i];\n }\n vector<int> time(n);\n for (int i = 0; i < n; i++){\n time[i] = ((M[i] * 40 + D[i]) * 24 + h[i]) * 60 + m[i];\n }\n vector<int> t(1000, 0);\n vector<bool> c(1000, false);\n for (int i = 0; i < n; i++){\n if (e[i] == 'I'){\n c[p[i]] = true;\n }\n if (e[i] == 'O'){\n c[p[i]] = false;\n }\n if (i < n - 1 && c[0]){\n int dt = time[i + 1] - time[i];\n for (int j = 1; j < 1000; j++){\n if (c[j]){\n t[j] += dt;\n }\n }\n }\n }\n int ans = 0;\n for (int j = 1; j < 1000; j++){\n ans = max(ans, t[j]);\n }\n cout << ans << endl;\n }\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3092, "score_of_the_acc": 0, "final_rank": 1 }, { "submission_id": "aoj_1315_4120552", "code_snippet": "#include <iostream>\n#include <cstdio>\n#include <string>\n#include <cstring>\n#include <deque>\n#include <list>\n#include <queue>\n#include <stack>\n#include <vector>\n#include <utility>\n#include <algorithm>\n#include <map>\n#include <set>\n#include <complex>\n#include <cmath>\n#include <limits>\n#include <climits>\n#include <ctime>\n#include <cassert>\n#include <numeric>\n#include <functional>\n#include <bitset>\n\nusing namespace std;\nusing lint = long long int;\nlong long int INF = 1001001001001001LL;\nint inf = 1000000007;\nlong long int MOD = 1000000007LL;\ndouble PI = 3.1415926535897932;\n\ntemplate<typename T1,typename T2>inline void chmin(T1 &a,const T2 &b){if(a>b) a=b;}\ntemplate<typename T1,typename T2>inline void chmax(T1 &a,const T2 &b){if(a<b) a=b;}\n\n#define ALL(a) a.begin(),a.end()\n#define RALL(a) a.rbegin(),a.rend()\n\n/* do your best */\n\n\n// quoted from beet-aizu\ntemplate <typename T, typename E, typename F, typename G>\nstruct SegmentTree{\n // using F = function<T(T, T)>\n // using G = function<T(T, E)>\n int n;\n F f;\n G g;\n T ti;\n vector<T> dat;\n SegmentTree(){};\n SegmentTree(F f,G g,T ti):f(f),g(g),ti(ti){}\n void init(int n_){ \n n=1;\n while(n<n_) n<<=1;\n dat.assign(n<<1,ti);\n }\n void build(const vector<T> &v){\n int n_=v.size();\n init(n_);\n for(int i=0;i<n_;i++) dat[n+i]=v[i];\n for(int i=n-1;i;i--)\n dat[i]=f(dat[(i<<1)|0],dat[(i<<1)|1]);\n }\n void update(int k,const E &x){\n k += n;\n dat[k] = g(dat[k], x);\n while(k>>=1)\n dat[k]=f(dat[(k<<1)|0],dat[(k<<1)|1]); \n }\n T operator [](int k) const { return dat[k+n]; }\n T query(int a,int b) const {\n T vl=ti,vr=ti;\n for(int l=a+n,r=b+n;l<r;l>>=1,r>>=1) {\n if(l&1) vl=f(vl,dat[l++]);\n if(r&1) vr=f(dat[--r],vr);\n }\n return f(vl,vr);\n }\n\n /* TODO わからない 聞く \n template<typename C>\n int find(int a,int b,C &check,int k,int l,int r){\n if(!check(dat[k])||r<=a||b<=l) return -1;\n if(k>=n) return k-n;\n int m=(l+r)>>1;\n int vl=find(a,b,check,(k<<1)|0,l,m);\n if(~vl) return vl;\n return find(a,b,check,(k<<1)|1,m,r);\n }\n template<typename C>\n int find(int a,int b,C &check){\n return find(a,b,check,1,0,n);\n }*/\n \n};\n\n\n/** テンプレ\nint main(){\n**/\n\nbool solve() {\n int n; cin >> n;\n if(n == 0) return false;\n vector<int> id(n);\n vector<int> t(n);\n vector<char> mode(n);\n \n for(int i = 0; i < n; i++) {\n string a, b, c, ID; cin >> a >> b >> c >> ID;\n // cerr << a.substr(3, 2) << \" \" << b.substr(0, 2) << \" \" << b.substr(3, 2) << endl;\n int M = stoi(a.substr(0, 2));\n int d = stoi(a.substr(3, 2));\n int h = stoi(b.substr(0, 2));\n int m = stoi(b.substr(3, 2));\n assert(M <= 12);\n assert(d <= 31);\n assert(h <= 24);\n assert(m <= 60);\n\n t[i] = M * 32 * 24 * 60 + d * 24 * 60 + h * 60 + m;\n id[i] = stoi(ID);\n mode[i] = c[0];\n }\n\n vector<vector<int>> l(1000);\n vector<vector<int>> r(1000);\n\n for(int i = 0; i < n; i++) {\n if(mode[i] == 'I') {\n l[id[i]].push_back(t[i]);\n } else {\n r[id[i]].push_back(t[i]);\n }\n }\n \n vector<int> dat(13 * 32 * 25 * 61, 0);\n \n assert(l[0].size() == r[0].size());\n for(int j = 0; j < l[0].size(); j++) {\n int left = l[0][j];\n int right = r[0][j];\n for(int i = left; i < right; i++) dat[i] = 1;\n }\n\n using T = int; // type T\n using E = int; // type E\n auto f = [](T a, T b){ // return type T value\n return a + b;\n };\n auto g = [](T a, E b){ // return type T value\n return b; // return b;\n };\n T ti = 0; // identify element\n SegmentTree<T, E, decltype(f), decltype(g)> sg(f, g, ti); // don't change\n sg.build(dat); // 初期配列を代入\n \n vector<int> cnt(1000, 0);\n for(int i = 1; i < 1000; i++) {\n assert(l[i].size() == r[i].size());\n for(int j = 0; j < l[i].size(); j++) {\n int left = l[i][j];\n int right = r[i][j];\n cnt[i] += sg.query(left, right);\n }\n }\n\n int ma = 0;\n for(int i = 0; i < 1000; i++) ma = max(ma, cnt[i]);\n cout << ma << endl;\n \n return true;\n}\n\nsigned main() {\n \n while(solve()) {}\n return 0;\n}", "accuracy": 1, "time_ms": 260, "memory_kb": 13800, "score_of_the_acc": -1.3571, "final_rank": 18 }, { "submission_id": "aoj_1315_4120415", "code_snippet": "// #define _GLIBCXX_DEBUG // for STL debug (optional)\n#include <iostream>\n#include <iomanip>\n#include <cstdio>\n#include <string>\n#include <cstring>\n#include <deque>\n#include <list>\n#include <queue>\n#include <stack>\n#include <vector>\n#include <utility>\n#include <algorithm>\n#include <map>\n#include <set>\n#include <complex>\n#include <cmath>\n#include <limits>\n#include <cfloat>\n#include <climits>\n#include <ctime>\n#include <cassert>\n#include <numeric>\n#include <fstream>\n#include <functional>\n#include <bitset>\nusing namespace std;\nusing ll = long long int;\nusing int64 = long long int;\n \ntemplate<typename T> void chmax(T &a, T b) {a = max(a, b);}\ntemplate<typename T> void chmin(T &a, T b) {a = min(a, b);}\ntemplate<typename T> void chadd(T &a, T b) {a = a + b;}\n \nint dx[] = {0, 0, 1, -1};\nint dy[] = {1, -1, 0, 0};\nconst int INF = 1LL << 29;\nconst ll LONGINF = 1LL << 60;\nconst ll MOD = 1000000007LL;\n\nint solve_testcase() {\n int N; cin >> N;\n if(N == 0) return 1;\n\n vector< vector< tuple<int, int, int> > > info(31 * 12);\n for(int i=0; i<N; i++) {\n int day = 0, time = 0, inout = 0, id = -1;\n {\n int m, d; scanf(\"%d/%d\", &m, &d); m--, d--;\n day = 31 * m + d;\n }\n {\n int h, m; scanf(\"%d:%d\", &h, &m);\n time = h * 60 + m;\n }\n {\n char c; scanf(\" %c\", &c);\n inout = (c == 'I' ? 1 : 0);\n }\n {\n scanf(\" %d\", &id);\n }\n info[day].emplace_back(time, inout, id);\n }\n\n // id, 時間\n map<int, int> rec;\n const int MAX_TIME = 3600;\n for(int d=0; d<31 * 12; d++) {\n vector<bool> god(MAX_TIME);\n\n // 誰がいまいるかの集合\n set<int> S;\n \n int ptr = 0;\n for(int t=0; t<MAX_TIME; t++) {\n while(ptr < info[d].size() and get<0>(info[d][ptr]) == t) {\n int inout, id; tie(std::ignore, inout, id) = info[d][ptr];\n if(inout == 1) S.emplace(id);\n if(inout == 0) S.erase(id);\n ptr++;\n }\n\n god[t] = S.count(0);\n for(auto p : S) {\n if(p == 0) continue;\n if(god[t]) rec[p]++;\n }\n }\n }\n\n int ans = 0;\n for(auto e : rec) ans = max(ans, e.second);\n printf(\"%d\\n\", ans);\n return 0;\n}\n\nint main() {\n while(!solve_testcase());\n return 0;\n}", "accuracy": 1, "time_ms": 300, "memory_kb": 3260, "score_of_the_acc": -0.43, "final_rank": 15 }, { "submission_id": "aoj_1315_4120397", "code_snippet": "#include <bits/stdc++.h>\n\nusing namespace std;\n\nint timetom(string s){\n int h=stoi(s.substr(0,2));\n int m=stoi(s.substr(3,2));\n return h*60+m;\n}\n\nbool solve(){\n int n;\n cin>>n;\n if(n==0)return false;\n //vector<pair<pair<int,bool>,int>> d(n);//<time,io>,id\n vector<vector<int>> d;\n vector<int> tm(n);\n map<string,int> mp;\n int godind=-1;\n for (int i = 0; i < n; ++i) {\n string s,t;\n cin>>s>>t;\n tm[i]=timetom(t);\n char c;\n cin>>c;\n string id;\n cin>>id;\n if(mp.find(id)==mp.end()){\n mp[id]=d.size();\n if(id==\"000\")godind=d.size();\n d.emplace_back(vector<int>(n+1,0));\n }\n if(c=='I')++d[mp[id]][i];\n else --d[mp[id]][i];\n }\n for(auto &i:d){\n for (int j = 0; j < n; ++j) {\n i[j+1]+=i[j];\n }\n }\n if(godind==-1){\n cout<<0<<endl;\n return true;\n }\n int ans=0;\n for (int i = 0; i < d.size(); ++i) {\n if(i==godind)continue;\n int tans=0;\n for (int j = 0; j < n-1; ++j) {\n if(d[i][j]==1&&d[godind][j]==1){\n tans+=tm[j+1]-tm[j];\n }\n }\n ans=max(ans,tans);\n }\n cout<<ans<<endl;\n return true;\n}\n\nint main(){\n while(solve());\n return 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 4888, "score_of_the_acc": -0.1677, "final_rank": 12 }, { "submission_id": "aoj_1315_3904078", "code_snippet": "#include \"bits/stdc++.h\"\nusing namespace std;\nconst int year1=372;\nconst int day1=1440;\nclass logdata\n{\n\tpublic:\n\tint day;\n\tint date;\n\tint visitor;\n\tlogdata()\n\t{}\n\tlogdata(string m,string t,string v)\n\t{\n\t\tvisitor=stoi(v);\n\t\tint big=(m[0]-'0')*10+(m[1]-'0');\n\t\tint little=(m[3]-'0')*10+(m[4]-'0');\n\t\tday=(big-1)*31+little;\n\t\tbig=(t[0]-'0')*10+(t[1]-'0');\n\t\tlittle=(t[3]-'0')*10+(t[4]-'0');\n\t\tdate=big*60+little;\n\t}\n};\nlogdata IO[1000];\nbool inroom[1000];//誰が部屋にいるか\nint during[1000];//goddessと一緒にいた時間\nstring indata[4];\n\n//IO[index-1]からIO[index]までの経過時間を求める\nint keika(int index)\n{\n\tint diffday=IO[index].day-IO[index-1].day;\n\tint difftime=IO[index].date-IO[index-1].date;\n\treturn diffday*day1+difftime;\n}\n\nint main()\n{\n\tint n;\n\twhile(1)\n\t{\n\t\tcin>>n;\n\t\tif(n==0)\n\t\t\tbreak;\n\t\tfor(int i=0;i<n;i++)\n\t\t{\n\t\t\tfor(int j=0;j<4;j++)\n\t\t\t\tcin>>indata[j];\n\t\t\tIO[i]=logdata(indata[0],indata[1],indata[3]);\n\t\t\tif(i>0)\n\t\t\t{\n\t\t\t\tif(IO[i].day<IO[i-1].day)\n\t\t\t\t\tIO[i].day+=((IO[i-1].day-IO[i].day)/year1+1)*year1;\n\t\t\t}\n\t\t}\n\t\t//input end\n\t\tfill(during,during+1000,0);\n\t\tfill(inroom,inroom+1000,false);\n\t\tfor(int i=0;i<n;i++)\n\t\t{\n\t\t\t//goddessがいるなら部屋にいる人の時間を加算する\n\t\t\tif(inroom[0])\n\t\t\t{\n\t\t\t\tint k=keika(i);\n\t\t\t\tfor(int j=1;j<1000;j++)\n\t\t\t\t{\n\t\t\t\t\tif(inroom[j])\n\t\t\t\t\t\tduring[j]+=k;\n\t\t\t\t}\n\t\t\t}\n\t\t\t//部屋にいる人\n\t\t\tinroom[IO[i].visitor]^=true;//反転\n\t\t}\n\t\tsort(during,during+1000);\n\t\treverse(during,during+1000);\n\t\tcout<<during[0]<<endl;\n\t}\n\treturn 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3156, "score_of_the_acc": -0.006, "final_rank": 5 }, { "submission_id": "aoj_1315_3695170", "code_snippet": "#include <bits/stdc++.h>\n \nusing namespace std;\n \ntypedef long long ll;\ntypedef unsigned long long ull;\ntypedef pair<ll, ll> P;\ntypedef vector<ll> V;\ntypedef complex<double> Point;\n \n#define PI acos(-1.0)\n#define EPS 1e-10\nconst ll INF = 1e16;\nconst ll MOD = 1e9 + 7;\n \n#define FOR(i,a,b) for(int i=(a);i<(b);i++)\n#define rep(i,N) for(int i=0;i<(N);i++)\n#define ALL(s) (s).begin(),(s).end()\n#define EQ(a,b) (abs((a)-(b))<EPS)\n#define EQV(a,b) ( EQ((a).real(), (b).real()) && EQ((a).imag(), (b).imag()) )\n#define fi first\n#define se second\n#define N_SIZE (1LL << 20)\n#define NIL -1\n \nll n;\nll sum[600][60*30];\nll ans[600];\n \nvoid solve(){\n // ll res = 0;\n rep(i,600)rep(j,60*30-1)sum[i][j+1] += sum[i][j];\n FOR(i,1,600){\n ll cnt = 0;\n rep(j,60*30){\n if((sum[i][j] > 0) && (sum[0][j] > 0))cnt++;\n // else cnt = 0;\n // res = max(cnt,res);\n }\n\t\tans[i] += cnt;\n }\n // return res;\n}\n \nvoid init(){\n rep(i,600)rep(j,60*30)sum[i][j] = 0;\n}\n \nll calc(string s){\n return ((s[0] - '0')*10+(s[1] - '0'))*60 + (s[3] - '0')*10+(s[4] - '0');\n}\n \nint main(){\n while(cin >> n && n){\n\t\tfill(ans,ans+600,0);\n string pre = \"\";\n ll aans = 0;\n rep(i,n){\n string s,hm,e;\n ll p;\n cin >> s >> hm >> e >> p;\n if(pre != s){\n pre = s;\n\t\t\t\tsolve();\n // ans = max(ans,solve());\n\t\t\t\t// cout << \"!!\" << pre << \" \" << ans << endl;\n init();\n }\n ll num = calc(hm);\n // cout << p << endl;\n if(e == \"I\")sum[p][num] = 1;\n else sum[p][num] = -1;\n }\n // ans = max(ans,solve());\n\t\tsolve();\n\t\tinit();\n\t\tFOR(i,1,600)aans = max(aans,ans[i]);\n\t\t// cout << \"!!\" << pre << \" \" << ans << endl;\n cout << aans << endl;\n }\n}", "accuracy": 1, "time_ms": 710, "memory_kb": 11500, "score_of_the_acc": -1.7852, "final_rank": 20 }, { "submission_id": "aoj_1315_3595576", "code_snippet": "#include \"bits/stdc++.h\"\nusing namespace std;\ntypedef long long ll;\ntypedef pair<int, int> pii;\ntypedef pair<ll, ll> pll;\nconst int INF = 1e9;\nconst ll LINF = 1e18;\n\n\nint timesum[1001];\nint entry[1001];\nint main(void) {\n // cin.tie(0); ios::sync_with_stdio(false);\n int n;\n while(cin >> n,n){\n vector<int> M(n),D(n),h(n),m(n);\n vector<string> e(n);\n vector<int> p(n);\n char c;\n for(int i = 0; i < n;i++){\n cin >> M[i] >> c >> D[i] >> h[i] >> c >> m[i] >> e[i] >> p[i];\n }\n fill(timesum,timesum+1001,0);\n fill(entry,entry+1001,-1);\n \n for(int i = 0; i < n;i++){\n \n if(e[i] == \"I\"){\n entry[p[i]] = h[i] * 60 + m[i];\n }else{\n int time = h[i] * 60 + m[i];\n \n if(p[i] == 0){\n for(int j = 1; j <= 1000; j++){\n if(entry[j] == -1) continue;\n timesum[j] += time-max(entry[0],entry[j]);\n }\n \n }else{\n if(entry[0] != -1 && entry[p[i]] != -1){\n timesum[p[i]] += time - max(entry[0],entry[p[i]]);\n }\n }\n entry[p[i]] = -1;\n }\n }\n cout << *max_element(timesum, timesum+101) << endl;\n }\n return 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3208, "score_of_the_acc": -0.0108, "final_rank": 7 }, { "submission_id": "aoj_1315_3372181", "code_snippet": "#include<iostream>\n#include<algorithm>\n#include<cstdio>\nusing namespace std;\nint n;\nint f()\n{\n\tint a,b;scanf(\"%d:%d\",&a,&b);return a*60+b;\n}\nmain()\n{\n\twhile(cin>>n,n)\n\t{\n\t\tint a[1000]={};\n\t\tint t[1000]={};\n\t\tint ans=0;\n\t\tbool flag=false;\n\t\tfor(;n--;)\n\t\t{\n\t\t\tstring s;cin>>s;\n\t\t\tint X=f();\n\t\t\tstring io;int id;cin>>io>>id;\n\t\t\tif(io==\"I\")\n\t\t\t{\n\t\t\t\tif(id)t[id]=X;\n\t\t\t\telse\n\t\t\t\t{\n\t\t\t\t\tfor(int j=1;j<1000;j++)if(t[j])t[j]=X;\n\t\t\t\t\tflag=true;\n\t\t\t\t}\n\t\t\t}\n\t\t\telse\n\t\t\t{\n\t\t\t\tif(id)\n\t\t\t\t{\n\t\t\t\t\tif(flag)\n\t\t\t\t\t{\n\t\t\t\t\t\ta[id]+=X-t[id];\n\t\t\t\t\t\tans=max(ans,a[id]);\n\t\t\t\t\t}\n\t\t\t\t\tt[id]=0;\n\t\t\t\t}\n\t\t\t\telse\n\t\t\t\t{\n\t\t\t\t\tfor(int j=1;j<1000;j++)\n\t\t\t\t\t{\n\t\t\t\t\t\tif(t[j])\n\t\t\t\t\t\t{\n\t\t\t\t\t\t\ta[j]+=X-t[j];\n\t\t\t\t\t\t\tans=max(ans,a[j]);\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t\tflag=false;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\tcout<<ans<<endl;\n\t}\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3196, "score_of_the_acc": -0.0097, "final_rank": 6 }, { "submission_id": "aoj_1315_3314957", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\n\nint convert(char x,char y){\n return (x-'0')*10+y-'0';\n}\nint solve(int n){\n vector<string> date(n);\n vector<int> t(n);\n vector<char> op(n);\n vector<string> id(n);\n for(int i=0;i<n;i++){\n string buf;\n cin>>date[i]>>buf>>op[i]>>id[i];\n t[i]=convert(buf[0],buf[1])*60+convert(buf[3],buf[4]);\n }\n\n map<string,int> res;\n map<string,vector<int>> events;\n for(int i=0;i<n;i++) events[date[i]].push_back(i);\n\n for(auto &kvp:events){\n auto event=kvp.second;\n stack<tuple<string,int,int>> outer;\n map<string,int> inner;\n int gin=0;\n for(int idx:event){\n if(id[idx]==\"000\"){\n if(op[idx]=='I'){\n gin=t[idx];\n }\n else{\n for(auto &e:inner){\n res[e.first]+=t[idx]-max(gin,e.second);\n }\n while(!outer.empty()){\n auto tmp=outer.top(); outer.pop();\n res[get<0>(tmp)]+=max(get<2>(tmp)-max(get<1>(tmp),gin),0);\n }\n }\n }\n else{\n if(op[idx]=='I'){\n inner[id[idx]]=t[idx];\n }\n else{\n outer.push(make_tuple(id[idx],inner[id[idx]],t[idx]));\n inner.erase(id[idx]);\n }\n }\n } \n }\n int ret=0;\n for(auto &e:res) if(e.first!=\"000\") ret=max(ret,e.second);\n return ret;\n}\n \nint main(){\n int n;\n while(cin>>n,n){\n cout<<solve(n)<<endl;\n }\n return 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3332, "score_of_the_acc": -0.0224, "final_rank": 8 }, { "submission_id": "aoj_1315_3274069", "code_snippet": "#include <iostream>\n#include <algorithm>\nusing namespace std;\n\n#define REP(i,n) for (int i=0; i<n; i++)\n\nint imos[1003][1500], staytime[1003];\n\nint main(){\n while(1) {\n int n, mon, d, h, min, p, prevdate = 0;\n char e;\n cin >> n;\n\n if (n==0){\n break;\n }\n\n REP(i,1000){\n REP(j,1500){\n imos[i][j] = 0;\n }\n staytime[i] = 0;\n }\n\n REP(i, n) {\n scanf(\"%d/%d %d:%d %c %d\\n\", &mon, &d, &h, &min, &e, &p);\n// printf(\"%d/%d %d:%d %c %d\\n\", mon, d, h, min, e, p);\n if (mon * 12 + d != prevdate) {\n// printf(\"prevdate: %d\\n\", prevdate);\n REP(j, 1000) {\n REP(k, 1499){\n imos[j][k+1] += imos[j][k];\n }\n }\n REP(k, 1500){\n if (imos[0][k]>0){\n REP(j,1000){\n if (imos[j][k]>0){\n staytime[j] += 1;\n }\n }\n }\n }\n REP(j,1000){\n REP(k,1500){\n imos[j][k] = 0;\n }\n }\n prevdate = mon * 12 + d;\n }\n if (e == 'I') {\n imos[p][60 * h + min] += 1;\n } else {\n imos[p][60 * h + min] -= 1;\n }\n }\n\n REP(j, 1000) {\n REP(k, 1499){\n imos[j][k+1] += imos[j][k];\n }\n }\n REP(k, 1500){\n if (imos[0][k]>0){\n REP(j,1000){\n if (imos[j][k]>0){\n staytime[j] += 1;\n }\n }\n }\n }\n\n int ans=0;\n for (int i=1; i<1000; i++){ // 直す\n ans = max(ans, staytime[i]);\n// printf(\"staytime[%d] = %d\\n\", i, staytime[i]);\n }\n\n cout << ans << endl;\n\n }\n\n\n return 0;\n}", "accuracy": 1, "time_ms": 620, "memory_kb": 8984, "score_of_the_acc": -1.4217, "final_rank": 19 }, { "submission_id": "aoj_1315_3252243", "code_snippet": "#include<iostream>\n#include<algorithm>\n#include<cstdio>\n#include<cmath>\n#include<cctype>\n#include<math.h>\n#include<string>\n#include<string.h>\n#include<stack>\n#include<queue>\n#include<vector>\n#include<utility>\n#include<set>\n#include<map>\n#include<stdlib.h>\n#include<iomanip>\n\nusing namespace std;\n\n#define ll long long\n#define ld long double\n#define EPS 0.0000000001\n#define INF 1e9\n#define MOD 1000000007\n#define rep(i,n) for(int i=0;i<(n);i++)\n#define loop(i,a,n) for(i=a;i<(n);i++)\n#define all(in) in.begin(),in.end()\n#define shosu(x) fixed<<setprecision(x)\n\ntypedef vector<int> vi;\ntypedef vector<string> vs;\ntypedef pair<int,int> pii;\ntypedef vector<pii> vp;\n\nint gcd(int a, int b){\n if(b==0) return a;\n return gcd(b,a%b);\n}\nint lcm(int a, int b){\n return a*b/gcd(a,b);\n}\n\nint toi(string s){\n int ret = 0;\n rep(i,s.size()){\n ret *= 10;\n ret += s[i]-'0';\n }\n return ret;\n}\n\n#define M 60*24+3\n\nint main(void) {\n int i,j;\n int n;\n int days[13] = {0,31,28,31,30,31,30,31,31,30,31,30,31};\n rep(i,12)days[i+1] += days[i];\n while(cin >> n, n){\n vector<vector<vp> > v(365,vector<vp>(1000));\n rep(i,n){\n string a,b,io,id;\n cin >> a >> b >> io >> id;\n int day = days[toi(a.substr(0,2))-1] + toi(a.substr(3,2));\n int t = toi(b.substr(0,2))*60 + toi(b.substr(3,2));\n v[day][toi(id)].push_back(pii(t,int(io==\"O\")));\n }\n vi sum(1000,0);\n rep(i,365){\n int god[M] = {};\n rep(k,v[i][0].size()){\n if(v[i][0][k].second) god[v[i][0][k].first]--;\n else god[v[i][0][k].first]++;\n }\n rep(k,M-1)god[k+1] += god[k];\n loop(j,1,1000)if(v[i][j].size()){\n int t[M] = {};\n rep(k,v[i][j].size()){\n if(v[i][j][k].second) t[v[i][j][k].first]--;\n else t[v[i][j][k].first]++;\n }\n rep(k,M-1)t[k+1] += t[k];\n rep(k,M)if(god[k]&&t[k])sum[j]++;\n }\n }\n sort(all(sum));\n cout << sum[999] << endl;\n }\n}", "accuracy": 1, "time_ms": 390, "memory_kb": 11476, "score_of_the_acc": -1.3258, "final_rank": 17 }, { "submission_id": "aoj_1315_3219599", "code_snippet": "//include\n//------------------------------------------\n#include <vector>\n#include <list>\n#include <map>\n#include <climits>\n#include <set>\n#include <deque>\n#include <stack>\n#include <bitset>\n#include <algorithm>\n#include <functional>\n#include <numeric>\n#include <utility>\n#include <sstream>\n#include <iostream>\n#include <iomanip>\n#include <cstdio>\n#include <cmath>\n#include <cstdlib>\n#include <cctype>\n#include <string>\n#include <cstring>\n#include <ctime>\n#include <queue>\n#include <random>\n#include <cctype>\n#include <complex>\n#include <regex>\n\nusing namespace std;\n\n#define C_MAX(a, b) ((a)>(b)?(a):(b))\n#define SHOW_VECTOR(v) {std::cerr << #v << \"\\t:\";for(const auto& xxx : v){std::cerr << xxx << \" \";}std::cerr << \"\\n\";}\n#define SHOW_MAP(v){std::cerr << #v << endl; for(const auto& xxx: v){std::cerr << xxx.first << \" \" << xxx.second << \"\\n\";}}\n\nstruct Data {\n int time;\n string which;\n string id;\n};\n\nstruct Per {\n int start;\n int end;\n string id;\n};\n\nint getTime(string s) {\n int hour = stoi(s.substr(0, 2));\n int minute = stoi(s.substr(3, 2));\n int time = hour * 60 + minute;\n return time;\n}\n\nint main() {\n\n while (true) {\n\n int N;\n cin >> N;\n\n if (N == 0) break;\n\n string pre = \"\";\n vector<vector<Data>> datas;\n for (int i = 0; i < N; i++) {\n string date, ti, which, id;\n cin >> date >> ti >> which >> id;\n int time = getTime(ti);\n if (i == 0) {\n datas.push_back(vector<Data>());\n datas.back().push_back((Data) {time, which, id});\n pre = date;\n continue;\n }\n if (pre == date) datas.back().push_back((Data) {time, which, id});\n else {\n datas.push_back(vector<Data>());\n datas.back().push_back((Data) {time, which, id});\n pre = date;\n }\n }\n\n map<string, int> M;\n for (int i = 0; i < datas.size(); i++) {\n vector<Per> god;\n vector<Per> other;\n int have = -1;\n for (int j = 0; j < datas[i].size(); j++) {\n auto data = datas[i][j];\n if (data.id == \"000\") {\n if (have == -1) have = data.time;\n else {\n god.push_back((Per) {have, data.time, \"000\"});\n have = -1;\n }\n }\n }\n map<string, int> has;\n for (int j = 0; j < datas[i].size(); j++) has[datas[i][j].id] = -1;\n for (int j = 0; j < datas[i].size(); j++) {\n auto data = datas[i][j];\n if (data.id != \"000\") {\n if (has[data.id] == -1) has[data.id] = data.time;\n else {\n other.push_back((Per) {has[data.id], data.time, data.id});\n has[data.id] = -1;\n }\n }\n }\n for (auto go: god) {\n for (auto ot: other) {\n int minV = max(go.start, ot.start);\n int maxV = min(go.end, ot.end);\n int d = maxV - minV;\n M[ot.id] += max(0, d);\n }\n }\n }\n\n int ansV = 0;\n for (auto x: M) ansV = max(ansV, x.second);\n\n cout << ansV << endl;\n\n }\n\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3344, "score_of_the_acc": -0.0235, "final_rank": 9 }, { "submission_id": "aoj_1315_3048256", "code_snippet": "#include <iostream>\n#include <map>\nusing namespace std;\nconst int INF = (1e9);\n\nint main(){\n int N;\n const string g = \"000\";\n while(cin >> N, N){\n string d, t, e, id, D;\n map<string, int> T, I, O;\n for(int i = 0; i < N; ++i){\n cin >> d >> t >> e >> id;\n if(D != d){\n D = d;\n for(auto itr = I.begin(); itr != I.end(); ++itr) itr->second = INF;\n for(auto itr = O.begin(); itr != O.end(); ++itr) itr->second = -1;\n }\n int m = stoi(t.substr(0,2))*60 + stoi(t.substr(3,2));\n if(e == \"I\"){\n I[id] = m;\n O[id] = INF;\n }else{\n O[id] = m;\n if(id != g && O[g] > m){\n T[id] += max(0, m - max(I[g],I[id]));\n }else if(id == g){\n for(auto itr = O.begin(); itr != O.end(); ++itr){\n id = itr->first;\n int o = itr->second;\n if(o > m){\n T[id] += max(0,m - max(I[id],I[g]));\n }\n }\n }\n }\n }\n int ans = 0;\n for(auto itr = T.begin(); itr != T.end(); ++itr){\n if(itr->first == g) continue;\n //cout << itr->first << \" \" << itr->second << endl;\n ans = max(ans, itr->second);\n }\n cout << ans << endl;\n }\n return 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3152, "score_of_the_acc": -0.0056, "final_rank": 4 }, { "submission_id": "aoj_1315_2737477", "code_snippet": "#include<iostream>\n#include<vector>\n#include<stdlib.h>\n#include<string>\n#define f first\n#define s second\nusing namespace std;\n \nint T(string s){\n int time=0;\n string s1=s.substr(0,2);\n string s2=s.substr(3,2);\n time+=(60)*atoi(s1.c_str());\n time+=atoi(s2.c_str());\n return time;\n}\n \nint main()\n{\n int N,n;\n string s1,s2;\n char c;\n \n while(1){\n cin>>N;\n if(N==0)break;\n int S[1000]={};\n vector<vector<pair<string,pair<int,int> > > > V(1000);\n for(int i=0;i<N;i++){\n cin>>s1>>s2>>c>>n;\n if(c=='I')S[n]=T(s2);\n else {\n V[n].push_back(make_pair(s1,make_pair(S[n],T(s2))));\n S[n]=0;\n }\n }\n for(int i=0;i<1000;i++)S[i]=0;\n for(int k=1;k<1000;k++){\n if(V[k].empty())continue;\n for(int i=0;i<V[0].size();i++){\n for(int j=0;j<V[k].size();j++){\n if(V[0][i].f!=V[k][j].f)continue;\n //0 1 1 0\n if(V[0][i].s.f<=V[k][j].s.f && V[k][j].s.s<=V[0][i].s.s){\n S[k]+=(V[k][j].s.s-V[k][j].s.f);\n }\n //1 0 0 1\n else if(V[k][j].s.f<=V[0][i].s.f && V[0][i].s.s<=V[k][j].s.s){\n S[k]+=(V[0][i].s.s-V[0][i].s.f);\n }\n //0 1 0 1\n else if(V[0][i].s.f<=V[k][j].s.f && V[k][j].s.f <=V[0][i].s.s){\n S[k]+=(V[0][i].s.s-V[k][j].s.f);\n }\n //1 0 1 0\n else if(V[0][i].s.f<=V[k][j].s.s && V[k][j].s.s <=V[0][i].s.s){\n S[k]+=(V[k][j].s.s-V[0][i].s.f);\n }\n }\n }\n }\n int ans=0;\n for(int i=1;i<1000;i++)ans=max(ans,S[i]);\n cout<<ans<<endl;\n/*\n for(int k=0;k<5;k++){\n for(int i=0;i<V[k].size();i++)cout<<V[k][i].f<<\" \"<<V[k][i].s.f<<\" \"<<V[k][i].s.s<<\" \"<<k<<endl;\n }*/\n }\n return 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3124, "score_of_the_acc": -0.003, "final_rank": 3 }, { "submission_id": "aoj_1315_2639658", "code_snippet": "#include <cstdio>\n#include <iostream>\n#include <vector>\n#include <cassert>\n#include <utility>\n#include <algorithm>\nusing namespace std;\n\nint N;\nconst int S = 1000;\n\nstruct Elem {\n bool in_room;\n int month, day, t;\n Elem() : in_room(false) {};\n Elem(int mo, int da, int h, int m) : month(mo), day(da), t(60*h+m) {\n in_room = true;\n }\n};\n\nint seg_val(int xl, int xr, int yl, int yr) {\n // x := user, y := goddess\n if(xr <= yl || yr <= xl) return 0;\n if(xl <= yl && yr <= xr) return yr - yl;\n if(yl <= xl && xr <= yr) return xr - xl;\n if(xl <= yl && yl <= xr) return xr - yl;\n return yr - xl;\n}\n\nint main() {\n while(cin >> N, N) {\n vector< pair<int, int> > seg[S][12][31];\n vector<Elem> es(S);\n for(int i=0; i<N; i++) {\n int month, day, h, m, id;\n char io;\n scanf(\"%02d/%02d %02d:%02d %c %03d\", &month, &day, &h, &m, &io, &id);\n month--; day--;\n if(io == 'I') {\n es[id] = Elem(month, day, h, m);\n }\n else {\n assert(es[id].month == month && es[id].day == day);\n int cur_time = 60*h + m;\n // ?????????\n seg[id][month][day].push_back(make_pair(es[id].t, cur_time));\n }\n }\n\n vector<int> sum_time(S);\n for(int id=1; id<1000; id++) {\n for(int mo=0; mo<12; mo++) {\n for(int da=0; da<31; da++) {\n for(auto x : seg[id][mo][da]) {\n for(auto y : seg[0][mo][da]) {\n int xl = x.first, xr = x.second;\n int yl = y.first, yr = y.second;\n sum_time[id] += seg_val(xl, xr, yl, yr);\n }\n }\n }\n }\n }\n cout << *max_element(sum_time.begin(), sum_time.end()) << endl;\n }\n return 0;\n}", "accuracy": 1, "time_ms": 160, "memory_kb": 11992, "score_of_the_acc": -1.0454, "final_rank": 16 }, { "submission_id": "aoj_1315_2573865", "code_snippet": "#include <iostream>\n#include <vector>\n#include <algorithm>\n#include <string>\n#include <cstdlib>\nusing namespace std;\n\nint main(){\n\twhile(1){\n\t\tint n;\n\t\tcin >> n;\n\t\tif(n==0) break;\n\t\t\n\t\tvector<vector<int> > in(1000), out(1000);\n\t\tint idmax=0;\n\t\tfor(int i=0; i<n; i++){\n\t\t\tstring date,time,io,id;\n\t\t\tcin >> date >> time >> io >> id;\n\t\t\tint min = stoi(date.substr(0,2))*50000 +stoi(date.substr(3,2))*1440\n\t\t\t\t\t\t+stoi(time.substr(0,2))*60 +stoi(time.substr(3,2));\n\t\t\tif(io==\"I\"){\n\t\t\t\tin[stoi(id)].push_back(min);\n\t\t\t}else{\n\t\t\t\tout[stoi(id)].push_back(min);\n\t\t\t}\n\t\t\tidmax = max(idmax, stoi(id));\n\t\t}\n\t\t\n\t\tint ans=0;\n\t\tfor(int i=1; i<=idmax; i++){\n\t\t\tint sum=0;\n\t\t\tfor(int j=0; j<(int)in[0].size(); j++){\n\t\t\t\tfor(int k=0; k<(int)in[i].size(); k++){\n\t\t\t\t\tif((in[0][j]<=in[i][k] && in[i][k]<=out[0][j]) || (in[i][k]<=in[0][j] && in[0][j]<=out[i][k])){\n\t\t\t\t\t\tsum += abs(max(in[0][j], in[i][k]) -min(out[0][j], out[i][k]));\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t\tans = max(ans, sum);\n\t\t}\n\t\tcout << ans << endl;\n\t}\n\treturn 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3116, "score_of_the_acc": -0.0022, "final_rank": 2 } ]
aoj_1313_cpp
Problem I: Intersection of Two Prisms Suppose that P 1 is an infinite-height prism whose axis is parallel to the z -axis, and P 2 is also an infinite-height prism whose axis is parallel to the y -axis. P 1 is defined by the polygon C 1 which is the cross section of P 1 and the xy -plane, and P 2 is also defined by the polygon C 2 which is the cross section of P 2 and the x z-plane. Figure I.1 shows two cross sections which appear as the first dataset in the sample input, and Figure I.2 shows the relationship between the prisms and their cross sections. Figure I.1: Cross sections of Prisms Figure I.2: Prisms and their cross sections Figure I.3: Intersection of two prisms Figure I.3 shows the intersection of two prisms in Figure I.2, namely, P 1 and P 2 . Write a program which calculates the volume of the intersection of two prisms. Input The input is a sequence of datasets. The number of datasets is less than 200. Each dataset is formatted as follows. m n x 11 y 11 x 12 y 12 . . . x 1 m y 1 m x 21 z 21 x 22 z 22 . . . x 2 n z 2 n m and n are integers (3 ≤ m ≤ 100, 3 ≤ n ≤ 100) which represent the numbers of the vertices of the polygons, C 1 and C 2 , respectively. x 1 i , y 1 i , x 2 j and z 2 j are integers between -100 and 100, inclusive. ( x 1 i , y 1 i ) and ( x 2 j , z 2 j ) mean the i -th and j -th vertices' positions of C 1 and C 2 respectively. The sequences of these vertex positions are given in the counterclockwise order either on the xy -plane or the xz -plane as in Figure I.1. You may assume that all the polygons are convex , that is, all the interior angles of the polygons are less than 180 degrees. You may also assume that all the polygons are simple , that is, each polygon's boundary does not cross nor touch itself. The end of the input is indicated by a line containing two zeros. Output For each dataset, output the volume of the intersection of the two prisms, P 1 and P 2 , with a decimal representation in a line. None of the output values may have an error greater than 0.001. The output should not contain any other extra characters. Sample Input 4 3 7 2 3 3 0 2 3 1 4 2 0 1 8 1 4 4 30 2 30 12 2 12 2 2 15 2 30 8 13 14 2 8 8 5 13 5 21 7 21 9 18 15 11 15 6 10 6 8 8 5 10 12 5 9 15 6 20 10 18 12 3 3 5 5 10 3 10 10 20 8 10 15 10 8 4 4 -98 99 -99 -99 99 -98 99 97 -99 99 -98 -98 99 -99 96 99 0 0 Output for the Sample Input 4.708333333333333 1680.0000000000005 491.1500000000007 0.0 7600258.4847715655
[ { "submission_id": "aoj_1313_6066782", "code_snippet": "#include <bits/stdc++.h>\n\nusing namespace std;\n\n#define INF 2000000000 // 2e9\n#define LLINF 2000000000000000000ll // 2e18 (llmax:9e18)\n#define all(x) (x).begin(), (x).end()\n#define sq(x) ((x) * (x))\n\n#define rep(i, x) for (int i = 0; i < (int)(x); i++)\n#define drep(i, x) for (int i = ((int)(x)-1); i >= 0; i--)\n\n#define popcount __builtin_popcount\n#define next __next\n#define prev __prev\n\n#ifndef LOCAL\n#define dmp(...) ;\n#else\n#define dmp(...) \\\n cerr << \"[ \" << #__VA_ARGS__ << \" ] : \" << dump_str(__VA_ARGS__) << endl\n#endif\n\n// ---------------- Utility ------------------\n\ntemplate <class T>\nbool chmin(T &a, const T &b) {\n if (a <= b) return false;\n a = b;\n return true;\n}\n\ntemplate <class T>\nbool chmax(T &a, const T &b) {\n if (a >= b) return false;\n a = b;\n return true;\n}\n\ntemplate <class T>\nusing MaxHeap = priority_queue<T>;\n\ntemplate <class T>\nusing MinHeap = priority_queue<T, vector<T>, greater<T>>;\n\ntemplate <class T>\nvector<T> vect(int len, T elem) {\n return vector<T>(len, elem);\n}\n\n// ----------------- Input -------------------\n\ntemplate <class T, class U>\nistream &operator>>(istream &is, pair<T, U> &p) {\n return is >> p.first >> p.second;\n}\n\ntemplate <class T>\nistream &operator>>(istream &is, vector<T> &vec) {\n for (int i = 0; i < vec.size(); i++) is >> vec[i];\n return is;\n}\n\n// ----------------- Output ------------------\n\ntemplate <class T, class U>\nostream &operator<<(ostream &os, const pair<T, U> &p) {\n return os << p.first << ',' << p.second;\n}\n\ntemplate <class T>\nostream &operator<<(ostream &os, const vector<T> &v) {\n for (const T &e : v) os << e << \" \";\n return os;\n}\n\ntemplate <class T>\nostream &operator<<(ostream &os, const deque<T> &d) {\n for (const T &e : d) os << e << \" \";\n return os;\n}\n\ntemplate <class T>\nostream &operator<<(ostream &os, const set<T> &s) {\n os << \"{ \";\n for (const T &e : s) os << e << \" \";\n return os << \"}\";\n}\n\ntemplate <class T, class U>\nostream &operator<<(ostream &os, const map<T, U> &m) {\n os << \"{ \";\n for (const auto &[key, val] : m) os << \"( \" << key << \" -> \" << val << \" ) \";\n return os << \"}\";\n}\n\ntemplate <class TupleTy, size_t... I>\nvoid dump_tuple(ostream &os, const TupleTy t, std::index_sequence<I...>) {\n (..., (os << (I == 0 ? \"\" : \",\") << std::get<I>(t)));\n}\n\ntemplate <class... Args>\nostream &operator<<(ostream &os, const tuple<Args...> &t) {\n os << \"(\";\n dump_tuple(os, t, std::make_index_sequence<sizeof...(Args)>());\n return os << \")\";\n}\n\nvoid dump_str_rec(ostringstream &) {}\n\ntemplate <class Head, class... Tail>\nvoid dump_str_rec(ostringstream &oss, Head &&head, Tail &&... tail) {\n oss << \", \" << head;\n dump_str_rec(oss, forward<Tail>(tail)...);\n}\n\ntemplate <class T, class... U>\nstring dump_str(T &&arg, U &&... args) {\n ostringstream oss;\n oss << arg;\n dump_str_rec(oss, forward<U>(args)...);\n return oss.str();\n}\n\n// --------------- Fast I/O ------------------\n\nvoid fastio() {\n cin.tie(0);\n ios::sync_with_stdio(0);\n cout << fixed << setprecision(20);\n}\n\n// ------------------ ACL --------------------\n\n// #include <atcoder/modint>\n// constexpr int MOD = 998244353;\n// constexpr int MOD = 1000000007;\n// using mint = atcoder::static_modint<MOD>;\n// ostream &operator<<(ostream &os, const mint &v) {\n// return os << v.val();\n// }\n\n// ------------ End of template --------------\n\n#define endl \"\\n\"\nusing ll = long long;\nusing pii = pair<int, int>;\n\nconst bool multitest = false;\n\n#define double long double\n\nconst double eps = 1e-9;\n\n// Geometry Library\n// written by okuraofvegetable\n\n#define pb push_back\n#define DOUBLE_INF 1e50\n#define Points vector<Point>\n\n#define EQ(a, b) (abs((a) - (b)) < eps)\n#define GT(a, b) ((a) > (b))\n#define GE(a, b) (GT(a, b) || EQ(a, b))\n#define LT(a, b) ((a) < (b))\n#define LE(a, b) (LT(a, b) || EQ(a, b))\n\ninline double add(double a, double b) {\n if (abs(a + b) < eps * (abs(a) + abs(b))) return 0;\n return a + b;\n}\n\n// --------------------- Point -----------------------\n\n#define Vector Point\nstruct Point {\n double x, y;\n Point() {}\n Point(double x, double y) : x(x), y(y) {}\n Point operator+(Point p) { return Point(add(x, p.x), add(y, p.y)); }\n Point operator-(Point p) { return Point(add(x, -p.x), add(y, -p.y)); }\n Point operator*(double d) { return Point(x * d, y * d); }\n Point operator/(double d) { return Point(x / d, y / d); }\n double dot(Point p) { return add(x * p.x, y * p.y); }\n double det(Point p) { return add(x * p.y, -y * p.x); }\n double norm() { return sqrt(x * x + y * y); }\n double norm2() { return x * x + y * y; }\n double dist(Point p) { return ((*this) - p).norm(); }\n double dist2(Point p) { return sq(x - p.x) + sq(y - p.y); }\n double arg() { return atan2(y, x); } // [-PI,PI]\n double arg(Vector v) { return v.arg() - arg(); } // henkaku\n double angle(Vector v) { // [0,PI]\n return acos(cos(arg(v)));\n }\n Vector normalize() { return (*this) * (1.0 / norm()); }\n Point vert() { return Point(y, -x); }\n\n // Signed area of triange (0,0) (x,y) (p.x,p.y)\n double area(Point p) { return (x * p.y - p.x * y) / 2.0; }\n friend istream &operator>>(istream &is, Point &p) { return is >> p.x >> p.y; }\n friend ostream &operator<<(ostream &os, const Point &p) {\n return os << '(' << p.x << ',' << p.y << ')';\n }\n};\n\nPoint divide_rate(Point a, Point b, double A, double B) {\n assert(!EQ(A + B, 0.0));\n return (a * B + b * A) * (1.0 / (A + B));\n}\n\nVector polar(double len, double arg) {\n return Vector(cos(arg) * len, sin(arg) * len);\n}\n\n// Direction a -> b -> c\n// verified AOJ CGL_1_C\nenum { COUNTER_CLOCKWISE, CLOCKWISE, ONLINE_BACK, ONLINE_FRONT, ON_SEGMENT };\nint ccw(Point a, Point b, Point c) {\n Vector p = b - a;\n Vector q = c - a;\n if (p.det(q) > 0.0) return COUNTER_CLOCKWISE; // counter clockwise\n if (p.det(q) < 0.0) return CLOCKWISE; // clockwise\n if (p.dot(q) < 0.0) return ONLINE_BACK; // c--a--b online_back\n if (p.norm() < q.norm()) return ONLINE_FRONT; // a--b--c online_front\n return ON_SEGMENT; // a--c--b on_segment\n}\n\n// --------------------- Line ------------------------\nstruct Line {\n Point a, b;\n Line() {}\n Line(Point a, Point b) : a(a), b(b) {}\n bool on(Point q) { return EQ((a - q).det(b - q), 0.0); }\n // following 2 functions verified AOJ CGL_2_A\n bool is_parallel(Line l) { return EQ((a - b).det(l.a - l.b), 0.0); }\n bool is_orthogonal(Line l) { return EQ((a - b).dot(l.a - l.b), 0.0); }\n Point intersection(Line l) {\n assert(!is_parallel(l));\n return a + (b - a) * ((l.b - l.a).det(l.a - a) / (l.b - l.a).det(b - a));\n }\n // Projection of p to this line\n // verified AOJ CGL_1_A\n Point projection(Point p) {\n return (b - a) * ((b - a).dot(p - a) / (b - a).norm2()) + a;\n }\n double distance(Point p) {\n Point q = projection(p);\n return p.dist(q);\n }\n // Reflection point of p onto this line\n // verified AOJ CGL_1_B\n Point refl(Point p) {\n Point proj = projection(p);\n return p + ((proj - p) * 2.0);\n }\n bool left(Point p) {\n if ((p - a).det(b - a) > 0.0)\n return true;\n else\n return false;\n }\n friend istream &operator>>(istream &is, Line &l) { return is >> l.a >> l.b; }\n friend ostream &operator<<(ostream &os, const Line &l) {\n return os << l.a << \" -> \" << l.b;\n }\n};\n\n// ------------------- Segment ----------------------\nstruct Segment {\n Point a, b;\n Segment() {}\n Segment(Point a, Point b) : a(a), b(b) {}\n Line line() { return Line(a, b); }\n bool on(Point q) {\n return (EQ((a - q).det(b - q), 0.0) && LE((a - q).dot(b - q), 0.0));\n }\n // verified AOJ CGL_2_B\n bool is_intersect(Segment s) {\n if (line().is_parallel(s.line())) {\n if (on(s.a) || on(s.b)) return true;\n if (s.on(a) || s.on(b)) return true;\n return false;\n }\n Point p = line().intersection(s.line());\n if (on(p) && s.on(p))\n return true;\n else\n return false;\n }\n bool is_intersect(Line l) {\n if (line().is_parallel(l)) {\n if (l.on(a) || l.on(b))\n return true;\n else\n return false;\n }\n Point p = line().intersection(l);\n if (on(p))\n return true;\n else\n return false;\n }\n // following 2 distance functions are verified at AOJ CGL_2_D\n double distance(Point p) {\n double res = DOUBLE_INF;\n Point q = line().projection(p);\n if (on(q)) res = min(res, p.dist(q));\n res = min(res, min(p.dist(a), p.dist(b)));\n return res;\n }\n double distance(Segment s) {\n if (is_intersect(s)) return 0.0;\n double res = DOUBLE_INF;\n res = min(res, s.distance(a));\n res = min(res, s.distance(b));\n res = min(res, this->distance(s.a));\n res = min(res, this->distance(s.b));\n return res;\n }\n friend istream &operator>>(istream &is, Segment &s) {\n return is >> s.a >> s.b;\n }\n friend ostream &operator<<(ostream &os, const Segment &s) {\n return os << s.a << \" -> \" << s.b;\n }\n};\n\ntypedef vector<Point> Polygon;\n\nbool solve() {\n int m, n;\n cin >> m >> n;\n if (n == 0 && m == 0) return false;\n Polygon a(m), b(n);\n cin >> a >> b;\n\n double a_x_max = -200, b_x_max = -200;\n double a_x_min = 200, b_x_min = 200;\n\n for (int i = 0; i < m; i++) {\n chmax(a_x_max, a[i].x);\n chmin(a_x_min, a[i].x);\n }\n\n for (int i = 0; i < n; i++) {\n chmax(b_x_max, b[i].x);\n chmin(b_x_min, b[i].x);\n }\n\n // if (a_x_max - eps < b_x_min || b_x_max - eps < a_x_min) {\n // cout << 0.0 << endl;\n // return true;\n // }\n\n auto intersection_length = [&](const Polygon &pol, const Line &l) {\n vector<Point> ps;\n for (int i = 0; i < pol.size(); i++) {\n Segment seg(pol[i], pol[(i + 1) % pol.size()]);\n if (seg.a.x < l.a.x && seg.b.x < l.a.x) continue;\n if (seg.a.x > l.a.x && seg.b.x > l.a.x) continue;\n // if (seg.line().is_parallel(l) && EQ(pol[i].x, l.a.x)) return -1.0;\n if (seg.line().is_parallel(l)) continue;\n if (!seg.is_intersect(l)) continue;\n Point inter = seg.line().intersection(l);\n bool is_new = true;\n for (auto &p : ps) {\n if ((p - inter).norm() < eps) is_new = false;\n }\n if (is_new) ps.push_back(inter);\n }\n assert(ps.size() <= 2);\n dmp(ps);\n if (ps.size() == 2) return (ps[0] - ps[1]).norm();\n return (double)0.0;\n };\n\n auto f = [&](double x) {\n // cerr << fixed << setprecision(20) << x << endl;\n Line l(Point(x, 0), Point(x, 1));\n auto len_a = intersection_length(a, l);\n if (EQ(len_a, 0.0)) return (double)0.0;\n auto len_b = intersection_length(b, l);\n if (EQ(len_b, 0.0)) return (double)0.0;\n dmp(x, len_a, len_b);\n return len_a * len_b;\n };\n\n double ans = 0.0;\n double s = -100, step = 1.0;\n while (s + step <= 105) {\n double mid = s + step / 2.0;\n if (a_x_min <= mid && mid <= a_x_max && b_x_min <= mid && mid <= b_x_max) {\n double p = f(s), q = f(mid), r = f(s + step);\n ans += (p + 4.0 * q + r) * step / 6.0;\n }\n s += step;\n }\n cout << ans << endl;\n\n return true;\n}\n\nint main() {\n fastio();\n if (!multitest) {\n while (solve()) {}\n } else {\n cerr << \"[Warning] Multi testcase mode on\" << endl;\n int t;\n cin >> t;\n while (t--) solve();\n }\n return 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3440, "score_of_the_acc": -0.9988, "final_rank": 13 }, { "submission_id": "aoj_1313_6066758", "code_snippet": "#include <bits/stdc++.h>\n\nusing namespace std;\n\n#define INF 2000000000 // 2e9\n#define LLINF 2000000000000000000ll // 2e18 (llmax:9e18)\n#define all(x) (x).begin(), (x).end()\n#define sq(x) ((x) * (x))\n\n#define rep(i, x) for (int i = 0; i < (int)(x); i++)\n#define drep(i, x) for (int i = ((int)(x)-1); i >= 0; i--)\n\n#define popcount __builtin_popcount\n#define next __next\n#define prev __prev\n\n#ifndef LOCAL\n#define dmp(...) ;\n#else\n#define dmp(...) \\\n cerr << \"[ \" << #__VA_ARGS__ << \" ] : \" << dump_str(__VA_ARGS__) << endl\n#endif\n\n// ---------------- Utility ------------------\n\ntemplate <class T>\nbool chmin(T &a, const T &b) {\n if (a <= b) return false;\n a = b;\n return true;\n}\n\ntemplate <class T>\nbool chmax(T &a, const T &b) {\n if (a >= b) return false;\n a = b;\n return true;\n}\n\ntemplate <class T>\nusing MaxHeap = priority_queue<T>;\n\ntemplate <class T>\nusing MinHeap = priority_queue<T, vector<T>, greater<T>>;\n\ntemplate <class T>\nvector<T> vect(int len, T elem) {\n return vector<T>(len, elem);\n}\n\n// ----------------- Input -------------------\n\ntemplate <class T, class U>\nistream &operator>>(istream &is, pair<T, U> &p) {\n return is >> p.first >> p.second;\n}\n\ntemplate <class T>\nistream &operator>>(istream &is, vector<T> &vec) {\n for (int i = 0; i < vec.size(); i++) is >> vec[i];\n return is;\n}\n\n// ----------------- Output ------------------\n\ntemplate <class T, class U>\nostream &operator<<(ostream &os, const pair<T, U> &p) {\n return os << p.first << ',' << p.second;\n}\n\ntemplate <class T>\nostream &operator<<(ostream &os, const vector<T> &v) {\n for (const T &e : v) os << e << \" \";\n return os;\n}\n\ntemplate <class T>\nostream &operator<<(ostream &os, const deque<T> &d) {\n for (const T &e : d) os << e << \" \";\n return os;\n}\n\ntemplate <class T>\nostream &operator<<(ostream &os, const set<T> &s) {\n os << \"{ \";\n for (const T &e : s) os << e << \" \";\n return os << \"}\";\n}\n\ntemplate <class T, class U>\nostream &operator<<(ostream &os, const map<T, U> &m) {\n os << \"{ \";\n for (const auto &[key, val] : m) os << \"( \" << key << \" -> \" << val << \" ) \";\n return os << \"}\";\n}\n\ntemplate <class TupleTy, size_t... I>\nvoid dump_tuple(ostream &os, const TupleTy t, std::index_sequence<I...>) {\n (..., (os << (I == 0 ? \"\" : \",\") << std::get<I>(t)));\n}\n\ntemplate <class... Args>\nostream &operator<<(ostream &os, const tuple<Args...> &t) {\n os << \"(\";\n dump_tuple(os, t, std::make_index_sequence<sizeof...(Args)>());\n return os << \")\";\n}\n\nvoid dump_str_rec(ostringstream &) {}\n\ntemplate <class Head, class... Tail>\nvoid dump_str_rec(ostringstream &oss, Head &&head, Tail &&... tail) {\n oss << \", \" << head;\n dump_str_rec(oss, forward<Tail>(tail)...);\n}\n\ntemplate <class T, class... U>\nstring dump_str(T &&arg, U &&... args) {\n ostringstream oss;\n oss << arg;\n dump_str_rec(oss, forward<U>(args)...);\n return oss.str();\n}\n\n// --------------- Fast I/O ------------------\n\nvoid fastio() {\n cin.tie(0);\n ios::sync_with_stdio(0);\n cout << fixed << setprecision(20);\n}\n\n// ------------------ ACL --------------------\n\n// #include <atcoder/modint>\n// constexpr int MOD = 998244353;\n// constexpr int MOD = 1000000007;\n// using mint = atcoder::static_modint<MOD>;\n// ostream &operator<<(ostream &os, const mint &v) {\n// return os << v.val();\n// }\n\n// ------------ End of template --------------\n\n#define endl \"\\n\"\nusing ll = long long;\nusing pii = pair<int, int>;\n\nconst bool multitest = false;\n\n#define double long double\n\nconst double eps = 1e-9;\n\n// Geometry Library\n// written by okuraofvegetable\n\n#define pb push_back\n#define DOUBLE_INF 1e50\n#define Points vector<Point>\n\n#define EQ(a, b) (abs((a) - (b)) < eps)\n#define GT(a, b) ((a) > (b))\n#define GE(a, b) (GT(a, b) || EQ(a, b))\n#define LT(a, b) ((a) < (b))\n#define LE(a, b) (LT(a, b) || EQ(a, b))\n\ninline double add(double a, double b) {\n if (abs(a + b) < eps * (abs(a) + abs(b))) return 0;\n return a + b;\n}\n\n// --------------------- Point -----------------------\n\n#define Vector Point\nstruct Point {\n double x, y;\n Point() {}\n Point(double x, double y) : x(x), y(y) {}\n Point operator+(Point p) { return Point(add(x, p.x), add(y, p.y)); }\n Point operator-(Point p) { return Point(add(x, -p.x), add(y, -p.y)); }\n Point operator*(double d) { return Point(x * d, y * d); }\n Point operator/(double d) { return Point(x / d, y / d); }\n double dot(Point p) { return add(x * p.x, y * p.y); }\n double det(Point p) { return add(x * p.y, -y * p.x); }\n double norm() { return sqrt(x * x + y * y); }\n double norm2() { return x * x + y * y; }\n double dist(Point p) { return ((*this) - p).norm(); }\n double dist2(Point p) { return sq(x - p.x) + sq(y - p.y); }\n double arg() { return atan2(y, x); } // [-PI,PI]\n double arg(Vector v) { return v.arg() - arg(); } // henkaku\n double angle(Vector v) { // [0,PI]\n return acos(cos(arg(v)));\n }\n Vector normalize() { return (*this) * (1.0 / norm()); }\n Point vert() { return Point(y, -x); }\n\n // Signed area of triange (0,0) (x,y) (p.x,p.y)\n double area(Point p) { return (x * p.y - p.x * y) / 2.0; }\n friend istream &operator>>(istream &is, Point &p) { return is >> p.x >> p.y; }\n friend ostream &operator<<(ostream &os, const Point &p) {\n return os << '(' << p.x << ',' << p.y << ')';\n }\n};\n\nPoint divide_rate(Point a, Point b, double A, double B) {\n assert(!EQ(A + B, 0.0));\n return (a * B + b * A) * (1.0 / (A + B));\n}\n\nVector polar(double len, double arg) {\n return Vector(cos(arg) * len, sin(arg) * len);\n}\n\n// Direction a -> b -> c\n// verified AOJ CGL_1_C\nenum { COUNTER_CLOCKWISE, CLOCKWISE, ONLINE_BACK, ONLINE_FRONT, ON_SEGMENT };\nint ccw(Point a, Point b, Point c) {\n Vector p = b - a;\n Vector q = c - a;\n if (p.det(q) > 0.0) return COUNTER_CLOCKWISE; // counter clockwise\n if (p.det(q) < 0.0) return CLOCKWISE; // clockwise\n if (p.dot(q) < 0.0) return ONLINE_BACK; // c--a--b online_back\n if (p.norm() < q.norm()) return ONLINE_FRONT; // a--b--c online_front\n return ON_SEGMENT; // a--c--b on_segment\n}\n\n// --------------------- Line ------------------------\nstruct Line {\n Point a, b;\n Line() {}\n Line(Point a, Point b) : a(a), b(b) {}\n bool on(Point q) { return EQ((a - q).det(b - q), 0.0); }\n // following 2 functions verified AOJ CGL_2_A\n bool is_parallel(Line l) { return EQ((a - b).det(l.a - l.b), 0.0); }\n bool is_orthogonal(Line l) { return EQ((a - b).dot(l.a - l.b), 0.0); }\n Point intersection(Line l) {\n assert(!is_parallel(l));\n return a + (b - a) * ((l.b - l.a).det(l.a - a) / (l.b - l.a).det(b - a));\n }\n // Projection of p to this line\n // verified AOJ CGL_1_A\n Point projection(Point p) {\n return (b - a) * ((b - a).dot(p - a) / (b - a).norm2()) + a;\n }\n double distance(Point p) {\n Point q = projection(p);\n return p.dist(q);\n }\n // Reflection point of p onto this line\n // verified AOJ CGL_1_B\n Point refl(Point p) {\n Point proj = projection(p);\n return p + ((proj - p) * 2.0);\n }\n bool left(Point p) {\n if ((p - a).det(b - a) > 0.0)\n return true;\n else\n return false;\n }\n friend istream &operator>>(istream &is, Line &l) { return is >> l.a >> l.b; }\n friend ostream &operator<<(ostream &os, const Line &l) {\n return os << l.a << \" -> \" << l.b;\n }\n};\n\n// ------------------- Segment ----------------------\nstruct Segment {\n Point a, b;\n Segment() {}\n Segment(Point a, Point b) : a(a), b(b) {}\n Line line() { return Line(a, b); }\n bool on(Point q) {\n return (EQ((a - q).det(b - q), 0.0) && LE((a - q).dot(b - q), 0.0));\n }\n // verified AOJ CGL_2_B\n bool is_intersect(Segment s) {\n if (line().is_parallel(s.line())) {\n if (on(s.a) || on(s.b)) return true;\n if (s.on(a) || s.on(b)) return true;\n return false;\n }\n Point p = line().intersection(s.line());\n if (on(p) && s.on(p))\n return true;\n else\n return false;\n }\n bool is_intersect(Line l) {\n if (line().is_parallel(l)) {\n if (l.on(a) || l.on(b))\n return true;\n else\n return false;\n }\n Point p = line().intersection(l);\n if (on(p))\n return true;\n else\n return false;\n }\n // following 2 distance functions are verified at AOJ CGL_2_D\n double distance(Point p) {\n double res = DOUBLE_INF;\n Point q = line().projection(p);\n if (on(q)) res = min(res, p.dist(q));\n res = min(res, min(p.dist(a), p.dist(b)));\n return res;\n }\n double distance(Segment s) {\n if (is_intersect(s)) return 0.0;\n double res = DOUBLE_INF;\n res = min(res, s.distance(a));\n res = min(res, s.distance(b));\n res = min(res, this->distance(s.a));\n res = min(res, this->distance(s.b));\n return res;\n }\n friend istream &operator>>(istream &is, Segment &s) {\n return is >> s.a >> s.b;\n }\n friend ostream &operator<<(ostream &os, const Segment &s) {\n return os << s.a << \" -> \" << s.b;\n }\n};\n\ntypedef vector<Point> Polygon;\n\nbool solve() {\n int m, n;\n cin >> m >> n;\n if (n == 0 && m == 0) return false;\n Polygon a(m), b(n);\n cin >> a >> b;\n\n double a_x_max = -200, b_x_max = -200;\n double a_x_min = 200, b_x_min = 200;\n\n for (int i = 0; i < m; i++) {\n chmax(a_x_max, a[i].x);\n chmin(a_x_min, a[i].x);\n }\n\n for (int i = 0; i < n; i++) {\n chmax(b_x_max, b[i].x);\n chmin(b_x_min, b[i].x);\n }\n\n // if (a_x_max - eps < b_x_min || b_x_max - eps < a_x_min) {\n // cout << 0.0 << endl;\n // return true;\n // }\n\n auto intersection_length = [&](const Polygon &pol, const Line &l) {\n vector<Point> ps;\n for (int i = 0; i < pol.size(); i++) {\n Segment seg(pol[i], pol[(i + 1) % pol.size()]);\n if (seg.a.x < l.a.x && seg.b.x < l.a.x) continue;\n if (seg.a.x > l.a.x && seg.b.x > l.a.x) continue;\n // if (seg.line().is_parallel(l) && EQ(pol[i].x, l.a.x)) return -1.0;\n if (seg.line().is_parallel(l)) continue;\n if (!seg.is_intersect(l)) continue;\n Point inter = seg.line().intersection(l);\n bool is_new = true;\n for (auto &p : ps) {\n if ((p - inter).norm() < eps) is_new = false;\n }\n if (is_new) ps.push_back(inter);\n }\n assert(ps.size() <= 2);\n dmp(ps);\n if (ps.size() == 2) return (ps[0] - ps[1]).norm();\n return (double)0.0;\n };\n\n auto f = [&](double x) {\n // cerr << fixed << setprecision(20) << x << endl;\n Line l(Point(x, 0), Point(x, 1));\n auto len_a = intersection_length(a, l);\n if (EQ(len_a, 0.0)) return (double)0.0;\n auto len_b = intersection_length(b, l);\n if (EQ(len_b, 0.0)) return (double)0.0;\n dmp(x, len_a, len_b);\n return len_a * len_b;\n };\n\n double ans = 0.0;\n double s = -100, step = 1.0;\n while (s + step <= 105) {\n double mid = s + step / 2.0;\n if (a_x_min <= mid && mid <= a_x_max && b_x_min <= mid && mid <= b_x_max) {\n double p = f(s), q = f(s + step / 2.0), r = f(s + step);\n ans += (p + 4.0 * q + r) * step / 6.0;\n }\n s += step;\n }\n cout << ans << endl;\n\n return true;\n}\n\nint main() {\n fastio();\n if (!multitest) {\n while (solve()) {}\n } else {\n cerr << \"[Warning] Multi testcase mode on\" << endl;\n int t;\n cin >> t;\n while (t--) solve();\n }\n return 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3444, "score_of_the_acc": -1, "final_rank": 14 }, { "submission_id": "aoj_1313_3978161", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\ntemplate<typename F>\nstruct Point {\n F x, y;\n Point(F x = 0, F y = 0) : x(x), y(y) {}\n bool operator<(const Point &oth) const {\n return x == oth.x ? y < oth.y : x < oth.x;\n }\n Point operator-(const Point &oth) const {\n return Point(x - oth.x, y - oth.y);\n }\n};\n\ntemplate<typename F>\nusing Polygon = vector<Point<F>>;\n\ntemplate<typename F>\nPolygon<F> upperConvexHull(Polygon<F> P) {\n reverse(P.begin(), P.end());\n rotate(P.begin(), min_element(P.begin(), P.end(), [](Point<F> a, Point<F> b) {\n return a.x == b.x ? a.y > b.y : a.x < b.x;\n }), P.end());\n int n = P.size();\n Polygon<F> ret = {P[0]};\n for (int i = 1; i < n; ++i) {\n auto dir = P[i] - P[i - 1];\n if (dir.x > 0) ret.push_back(P[i]);\n }\n return ret;\n}\n\ntemplate<typename F>\nPolygon<F> lowerConvexHull(Polygon<F> P) {\n rotate(P.begin(), min_element(P.begin(), P.end()), P.end());\n int n = P.size();\n Polygon<F> ret = {P[0]};\n for (int i = 1; i < n; ++i) {\n auto dir = P[i] - P[i - 1];\n if (dir.x > 0) ret.push_back(P[i]);\n }\n return ret;\n}\n\ntemplate<typename F>\ndouble getLen(F x, Polygon<F> U, Polygon<F> L) {\n int i = upper_bound(U.begin(), U.end(), Point<F>(x, -1e9)) - U.begin();\n int j = upper_bound(L.begin(), L.end(), Point<F>(x, -1e9)) - L.begin();\n if (i == U.size() or j == L.size()) return 0;\n if (i == 0 and U[i].x > x) return 0;\n if (j == 0 and L[j].x > x) return 0;\n auto getY = [&](double x, int i, Polygon<F> P) -> double {\n if (i == 0) return P[i].y;\n return P[i].y - (double) (P[i].y - P[i - 1].y) / \n (P[i].x - P[i - 1].x) * \n (P[i].x - x);\n };\n return getY(x, i, U) - getY(x, j, L);\n}\n\n\ntemplate<typename Double>\nclass Integration {\n Double ALPHA = sqrt((5 - sqrt(40. / 7))) / 3, WA = (322 + sqrt(11830)) / 900;\n Double W0 = 128. / 225.;\n Double BETA = sqrt((5 + sqrt(40. / 7))) / 3, WB = (322 - sqrt(11830)) / 900;\n function<Double(Double)> f;\n Double quadrature(Double l, Double r) {\n auto m = (l + r) / 2, len = r - m;\n return (f(m - ALPHA * len) * WA + f(m - BETA * len) * WB + f(m) * W0 +\n f(m + ALPHA * len) * WA + f(m + BETA * len) * WB) * len;\n }\n Double askArea(Double l, Double r, Double exceptArea) {\n Double m = (l + r) / 2, L = quadrature(l, m), R = quadrature(m, r);\n if (abs(L + R - exceptArea) < 1e-10)\n return L + R;\n else return askArea(l, m, L) + askArea(m, r, R);\n }\npublic:\n Integration(function<Double(Double)> func) : f(func) {}\n Double intergal(Double l, Double r, int piece = 10) {\n Double ans = 0;\n for (Double dx = (r - l) / piece, i = 0; i < piece; ++i) {\n auto cur = l + dx * i;\n ans += askArea(cur, cur + dx, quadrature(cur, cur + dx));\n }\n return ans;\n }\n};\n\nint main() {\n ios_base::sync_with_stdio(false); cin.tie(0);\n int n, m; \n while (cin >> n >> m and n and m) {\n Polygon<double> Z, Y;\n vector<int> Xs;\n for (int i = 0; i < n; ++i) {\n int x, y; cin >> x >> y;\n Z.emplace_back(x, y);\n Xs.emplace_back(x);\n }\n for (int i = 0; i < m; ++i) {\n int x, y; cin >> x >> y;\n Y.emplace_back(x, y);\n Xs.emplace_back(x);\n }\n sort(Xs.begin(), Xs.end());\n Xs.erase(unique(Xs.begin(), Xs.end()), Xs.end());\n Polygon<double> UZ = upperConvexHull(Z);\n Polygon<double> LZ = lowerConvexHull(Z);\n Polygon<double> UY = upperConvexHull(Y);\n Polygon<double> LY = lowerConvexHull(Y);\n double preX = Xs[0];\n double ans = 0;\n Integration<double> formula([=](double x) {\n return getLen(x, UZ, LZ) * getLen(x, UY, LY);\n });\n for (int x : Xs) {\n ans += formula.intergal(preX, x);\n preX = x;\n }\n cout << fixed << setprecision(10) << ans << '\\n';\n }\n}", "accuracy": 1, "time_ms": 70, "memory_kb": 3284, "score_of_the_acc": -0.9642, "final_rank": 12 }, { "submission_id": "aoj_1313_3042345", "code_snippet": "#include <iostream>\n#include <iomanip>\n#include <complex>\n#include <vector>\n#include <algorithm>\n#include <cmath>\n#include <array>\nusing namespace std;\nconst double EPS = 1e-10;\nconst double INF = 1e12;\n#define EQ(n,m) (abs((n)-(m)) < EPS)\n#define X real()\n#define Y imag()\n\ntypedef complex<double> P;\ntypedef vector<P> VP;\nstruct L : array<P, 2>{\n L(const P& a, const P& b){ at(0)=a; at(1)=b; }\n L(){}\n};\n\nnamespace std{\n bool operator < (const P& a, const P& b){\n return (a.X!=b.X) ? a.X<b.X : a.Y<b.Y;\n }\n bool operator == (const P& a, const P& b){\n return abs(a-b) < EPS;\n }\n}\n\ndouble dot(P a, P b){\n return (conj(a)*b).X;\n}\ndouble cross(P a, P b){\n return (conj(a)*b).Y;\n}\nint ccw(P a, P b, P c){\n b -= a;\n c -= a;\n if(cross(b,c) > EPS) return +1; //ccw\n if(cross(b,c) < -EPS) return -1; //cw\n if(dot(b,c) < -EPS) return +2; //c-a-b\n if(abs(c)-abs(b) > EPS) return -2; //a-b-c\n return 0; //a-c-b\n}\n\nbool intersectLS(const L& l, const L& s){\n return cross(l[1]-l[0], s[0]-l[0])*\n cross(l[1]-l[0], s[1]-l[0]) < EPS;\n}\n\nbool isParallel(const P &a, const P &b){\n return abs(cross(a,b)) < EPS;\n}\nbool isParallel(const L &a, const L &b){\n return isParallel(a[1]-a[0], b[1]-b[0]);\n}\nP crosspointLL(const L &l, const L &m) {\n double A = cross(l[1]-l[0], m[1]-m[0]);\n double B = cross(l[1]-l[0], l[1]-m[0]);\n return m[0] + B/A *(m[1]-m[0]);\n}\n\ndouble getarea(const VP &poly){\n double ret = 0;\n for (int i=0; i<(int)poly.size(); i++){ \n ret += cross(poly[i], poly[(i+1)%poly.size()]);\n }\n return ret*0.5;\n}\n\nVP convex_cut(const VP& p, const L& l){\n VP ret;\n int n = p.size();\n for(int i=0; i<n; i++){\n P curr = p[i];\n P next = p[(i+1)%n];\n if(ccw(l[0], l[1], curr) != -1) ret.push_back(curr);\n if(ccw(l[0], l[1], curr) *ccw(l[0], l[1], next) == -1){\n ret.push_back(crosspointLL(L(curr, next), l));\n }\n }\n return ret;\n}\n\nstruct c2cut{\n double z, x[2];\n c2cut(double z, double xmin, double xmax):z(z),x{xmin, xmax}{}\n c2cut(){}\n bool operator <(const c2cut &a){\n return z < a.z;\n }\n bool operator ==(const c2cut &a){\n return z == a.z;\n }\n};\n\nint main(){\n while(1){\n int m,n;\n cin >> m >> n;\n if(n==0) break;\n \n VP c1(m), c2(n);\n for(int i=0; i<m; i++){\n double x,y;\n cin >> x >> y;\n c1[i] = P(x, y);\n }\n for(int i=0; i<n; i++){\n double x,z;\n cin >> x >> z;\n c2[i] = P(x, z);\n }\n \n vector<c2cut> cz(n);\n for(int i=0; i<n; i++){\n double z = c2[i].Y;\n L cutline(P(0, z), P(1, z));\n vector<double> cpx;\n for(int i=0; i<n; i++){\n L edge(c2[i], c2[(i+1)%n]);\n if(!isParallel(cutline, edge) && intersectLS(cutline, edge)){\n cpx.push_back((crosspointLL(cutline, edge)).X);\n }\n }\n double xmin = *min_element(cpx.begin(), cpx.end());\n double xmax = *max_element(cpx.begin(), cpx.end());\n cz[i] = c2cut(z, xmin, xmax);\n }\n sort(cz.begin(), cz.end());\n cz.erase(unique(cz.begin(), cz.end()), cz.end());\n\n double ans = 0;\n for(int i=0; i<(int)cz.size()-1; i++){\n vector<double> zdiff;\n for(int d=0; d<2; d++){\n double x[2] = {cz[i].x[d], cz[i+1].x[d]};\n if(abs(x[1]-x[0]) < EPS) continue;\n if(x[0] > x[1]) swap(x[0], x[1]);\n L cut[2] = {L(P(x[1], 0), P(x[1], 1)), L(P(x[0], 1), P(x[0], 0))};\n VP p = convex_cut(c1, cut[0]);\n p = convex_cut(p, cut[1]);\n for(int j=0; j<(int)p.size(); j++){\n double xdiff = abs(p[j].X -cz[i].x[d]);\n zdiff.push_back((cz[i+1].z -cz[i].z) *xdiff/(x[1]-x[0]));\n }\n }\n for(int d=0; d<2; d++){\n int xmax = cz[i+d].x[1];\n int xmin = cz[i+d].x[0];\n L cut[2] = {L(P(xmax, 0), P(xmax, 1)), L(P(xmin, 1), P(xmin, 0))};\n VP p = convex_cut(c1, cut[0]);\n p = convex_cut(p, cut[1]);\n if(!p.empty()){\n zdiff.push_back((cz[i+1].z -cz[i].z) *d);\n }\n }\n sort(zdiff.begin(), zdiff.end());\n zdiff.erase(unique(zdiff.begin(), zdiff.end()), zdiff.end());\n\n for(int j=0; j<(int)zdiff.size()-1; j++){\n double z[3];\n z[0] = cz[i].z +zdiff[j];\n z[2] = cz[i].z +zdiff[j+1];\n z[1] = (z[0] +z[2])/2;\n double area[3];\n for(int k=0; k<3; k++){\n L cutline(P(0, z[k]), P(1, z[k]));\n vector<double> cpx;\n for(int i=0; i<n; i++){\n L edge(c2[i], c2[(i+1)%n]);\n if(!isParallel(cutline, edge) && intersectLS(cutline, edge)){\n cpx.push_back((crosspointLL(cutline, edge)).X);\n }\n }\n double xmin = *min_element(cpx.begin(), cpx.end());\n double xmax = *max_element(cpx.begin(), cpx.end());\n L cut[2] = {L(P(xmax, 0), P(xmax, 1)), L(P(xmin, 1), P(xmin, 0))};\n VP con = convex_cut(c1, cut[0]);\n con = convex_cut(con, cut[1]);\n area[k] = getarea(con);\n }\n ans += (z[2] -z[0])/6 *(area[0] +4*area[1] +area[2]);\n }\n }\n\n cout << fixed << setprecision(10);\n cout << ans << endl;\n }\n return 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3268, "score_of_the_acc": -0.9489, "final_rank": 11 }, { "submission_id": "aoj_1313_2423083", "code_snippet": "#include <bits/stdc++.h>\n#define ll long long\n#define INF 1000000005\n#define MOD 1000000007\n#define EPS 1e-10\n#define rep(i,n) for(int i=0;i<(int)n;++i)\n#define each(a, b) for(auto (a): (b))\n#define all(v) (v).begin(),(v).end()\n#define fi first\n#define se second\n#define pb push_back\n#define show(x) cout <<#x<<\" = \"<<(x)<<endl\n#define spair(p) cout <<#p<<\": \"<<p.fi<<\" \"<<p.se<<endl\n#define svec(v) cout<<#v<<\":\";rep(kbrni,v.size())cout<<\" \"<<v[kbrni];cout<<endl\n#define sset(s) cout<<#s<<\":\";each(kbrni,s)cout <<\" \"<<kbrni;cout<<endl\n\nusing namespace std;\n\ntypedef complex<double> C;\ntypedef pair<int,int>P;\n\nnamespace std\n{\n bool operator < (const C a, const C b) {\n return a.real() != b.real() ? a.real() < b.real() : a.imag() < b.imag();\n }\n}\n\nstruct L : public vector<C>\n{\n L(const C a, const C b) {\n push_back(a); push_back(b);\n }\n};\n\nbool eq(double a,double b)\n{\n return (-EPS<a-b&&a-b<EPS);\n}\n\nbool eq(C c1,C c2)\n{\n return (eq(c1.real(),c2.real()) && eq(c1.imag(),c2.imag()));\n}\n\n//?????¶??????sqrt\ndouble Sqrt(double x)\n{\n if(x<0) return 0;\n else return sqrt(x);\n}\n\n//??£??????\nC normalize(C c)\n{\n return c / abs(c);\n}\n\n//?§????(rad)\ndouble getarg(C a,C b){\n return arg(b*conj(a));\n}\n\n//??????\ndouble cross(const C a, const C b)\n{\n return imag(conj(a)*b);\n}\n//??????\ndouble dot(const C a, const C b)\n{\n return real(conj(a)*b);\n}\n\nint ccw(C a, C b, C c)\n{\n b -= a; c -= a;\n if (cross(b, c) > 0) return +1; // counter clockwise\n if (cross(b, c) < 0) return -1; // clockwise\n if (dot(b, c) < 0) return +2; // c--a--b on line\n if (norm(b) < norm(c)) return -2; // a--b--c on line\n return 0;\n}\n//??´???????????????????????????(????????´??????True)\nbool intersectLL(const L &l, const L &m)\n{\n return abs(cross(l[1]-l[0], m[1]-m[0])) > EPS || abs(cross(l[1]-l[0], m[0]-l[0])) < EPS;\n}\n//??´?????¨?????????????????????(????????±??????????????¨??????)\nbool intersectLS(const L &l, const L &s)\n{\n return cross(l[1]-l[0], s[0]-l[0]) * cross(l[1]-l[0], s[1]-l[0]) < EPS;\n}\n//??´?????¨????????????(??±???)??????\nbool intersectLP(const L &l, const C p)\n{\n return abs(cross(l[1]-p, l[0]-p)) < EPS;\n}\n//??????????????????????????????(????????±??????????????¨??????)\nbool intersectSS(const L &s, const L &t)\n{\n return ccw(s[0],s[1],t[0])*ccw(s[0],s[1],t[1]) <= 0 && ccw(t[0],t[1],s[0])*ccw(t[0],t[1],s[1]) <= 0;\n}\n//????????¨????????????(??±???)??????\nbool intersectSP(const L &s, const C p)\n{\n return abs(s[0]-p)+abs(s[1]-p)-abs(s[1]-s[0]) < EPS;\n}\n//???p?????´???l???????°???±\nC projection(const L &l, const C p)\n{\n double t = dot(p-l[0], l[0]-l[1]) / norm(l[0]-l[1]);\n return l[0] + t*(l[0]-l[1]);\n}\n//???p?????´???l????????¨??????????§°?§????\nC reflection(const L &l, const C p)\n{\n return p + (projection(l, p) - p)*2.0;\n}\n//?????¨??´???????????¢\ndouble distanceLP(const L &l, const C p)\n{\n return abs(p - projection(l, p));\n}\n//??´?????¨??´???????????¢\ndouble distanceLL(const L &l, const L &m)\n{\n return intersectLL(l, m) ? 0 : distanceLP(l, m[0]);\n}\n//??´?????¨??????????????¢\ndouble distanceLS(const L &l, const L &s)\n{\n if (intersectLS(l, s)) return 0;\n return min(distanceLP(l, s[0]), distanceLP(l, s[1]));\n}\n//????????¨???????????¢\ndouble distanceSP(const L &s, const C p)\n{\n const C r = projection(s, p);\n if (intersectSP(s, r)) return abs(r - p);\n return min(abs(s[0] - p), abs(s[1] - p));\n}\n//????????¨??????????????¢\ndouble distanceSS(const L &s, const L &t)\n{\n if (intersectSS(s, t)) return 0;\n return min(min(distanceSP(s, t[0]), distanceSP(s, t[1])),min(distanceSP(t, s[0]), distanceSP(t, s[1])));\n}\n//??´???????????????????????????\nC crosspointLL(const L &l, const L &m)\n{\n double A = cross(l[1] - l[0], m[1] - m[0]);\n double B = cross(l[1] - l[0], l[1] - m[0]);\n //????????´????????¨???\n if(abs(A) < EPS && abs(B) < EPS){\n return m[0];\n }\n return m[0] + B / A * (m[1] - m[0]);\n}\n//?????¨??´????????????\ndouble gettime(C c1,C c2)\n{\n return (dot(c1,c2) < 0 ? -1.0 : 1.0 ) * abs(c2) / abs(c1);\n}\n//?????¨??´????????????\nvector<C> crosspointCL(C c1,double r1,L l)\n{\n C a=l[0], b=l[1];\n vector<C> res;\n C base=b-a, target=projection(L(a,b),c1);\n double length=abs(base), h=abs(c1-target);\n base/=length;\n if(r1+EPS<h) return res;\n double w=Sqrt(r1*r1-h*h);\n double LL=gettime(normalize(b-a),target-a)-w,RR=LL+w*2.0;\n res.push_back(a+base*LL);\n if(eq(LL,RR)) return res;\n res.push_back(a+base*RR);\n return res;\n}\n//?????¨???????????????\nvector<C> crosspointCS(C c1,double r1,L s)\n{\n vector<C> tmp=crosspointCL(c1,r1,s);\n vector<C> res;\n rep(i,tmp.size()){\n if(eq(abs(s[1]-s[0]),abs(s[0]-tmp[i])+abs(s[1]-tmp[i]))){\n res.push_back(tmp[i]);\n }\n }\n return res;\n}\n//?????????????????????\nL crosspointCC(const C c1, const double r1, const C c2, const double r2)\n{\n C a = conj(c2-c1), b = (r2*r2-r1*r1-(c2-c1)*conj(c2-c1)), c = r1*r1*(c2-c1);\n C d = b*b-4.0*a*c;\n C z1 = (-b+sqrt(d))/(2.0*a)+c1, z2 = (-b-sqrt(d))/(2.0*a)+c1;\n return L(z1, z2);\n}\n//?????¨????§???¢?????±?????¨????????¢???\ndouble getarea(C c1,double r1,C a,C b)\n{\n C va=c1-a,vb=c1-b;\n double A=abs(va),B=abs(vb);\n double f=cross(va,vb),d=distanceSP(L(a,b),c1),res=0;\n if(eq(f,0.0)) return 0;\n if(A < r1+EPS && B < r1+EPS) return f*0.5;\n if(d>r1-EPS) return r1*r1*M_PI*getarg(va,vb)/(2.0*M_PI);\n vector<C> u=crosspointCS(c1,r1,L(a,b));\n u.insert(u.begin(),a),u.push_back(b);\n for(int i=0;i+1<(int)u.size();i++){\n res+=getarea(c1,r1,u[i],u[i+1]);\n }\n return res;\n}\ndouble getcrossarea(vector<C> t,C c1,double r1)\n{\n int n=t.size();\n if(n<3) return 0;\n double res=0;\n rep(i,n){\n C a=t[i], b=t[(i+1)%n];\n res += getarea(c1,r1,a,b);\n }\n return res;\n}\n//??????????±???????\nvector<C> convex_hull(vector<C> ps)\n{\n int n = ps.size(), k = 0;\n sort(ps.begin(), ps.end());\n vector<C> ch(2*n);\n for (int i = 0; i < n; ch[k++] = ps[i++]){\n while (k >= 2 && ccw(ch[k-2], ch[k-1], ps[i]) <= 0) k--;\n }\n for (int i = n-2, t = k+1; i >= 0; ch[k++] = ps[i--]){\n while (k >= t && ccw(ch[k-2], ch[k-1], ps[i]) <= 0) k--;\n }\n ch.resize(k-1);\n return ch;\n}\n//?????§??????\nbool isconvex(const vector<C> &ps)\n{\n rep(i,ps.size()){\n if (ccw(ps[(i+ps.size()-1) % ps.size()],ps[i],ps[(i+1) % ps.size()])) return false;\n }\n return true;\n}\n//????§???¢?????¢???\ndouble area(const vector<C> &ps)\n{\n double A = 0;\n rep(i,ps.size()){\n A += cross(ps[i],ps[(i+1) % ps.size()]);\n }\n return A / 2.0;\n}\n//???????§???¢?????´?????§???????????????????????´????????¢\nvector<C> convex_cut(const vector<C> &ps, const L &l)\n{\n vector<C> Q;\n rep(i,ps.size()){\n C A = ps[i], B = ps[(i+1)%ps.size()];\n if (ccw(l[0], l[1], A) != -1) Q.push_back(A);\n if (ccw(l[0], l[1], A)*ccw(l[0], l[1], B) < 0)\n Q.push_back(crosspointLL(L(A, B),l));\n }\n return Q;\n}\n//??????????§???¢???????????????????????????(0??????????????????,1?????????,2???????????????)\nint contains(const vector<C>& ps, const C p)\n{\n bool flag = false;\n rep(i,ps.size()) {\n C a = ps[i] - p, b = ps[(i+1)%ps.size()] - p;\n if (imag(a) > imag(b)) swap(a, b);\n if (imag(a) <= 0 && 0 < imag(b)){\n if (cross(a, b) < 0) flag = !flag;\n }\n if (cross(a, b) == 0 && dot(a, b) <= 0) return 1;\n }\n return flag ? 2 : 0;\n}\n//???????§???¢?????´???????±???????(?????£????????????)\n//maxi,maxj?????????????????¨??????\ndouble convex_diameter(const vector<C> &ps)\n{\n const int n = ps.size();\n int is = 0, js = 0;\n for (int i = 1; i < n; ++i) {\n if (imag(ps[i]) > imag(ps[is])) is = i;\n if (imag(ps[i]) < imag(ps[js])) js = i;\n }\n double maxd = abs(ps[is]-ps[js]);\n int i, maxi, j, maxj;\n i = maxi = is;\n j = maxj = js;\n do{\n if (cross(ps[(i+1)%ps.size()]-ps[i],ps[(j+1)%ps.size()]-ps[j]) >= 0) j = (j+1) % n;\n else i = (i+1) % n;\n if (abs(ps[i]-ps[j]) > maxd) {\n maxd = abs(ps[i]-ps[j]);\n maxi = i; maxj = j;\n }\n } while (i != is || j != js);\n return maxd;\n}\n\nbool compyx(C c1,C c2)\n{\n return c1.imag() != c2.imag() ? c1.imag() < c2.imag() : c1.real() < c2.real();\n}\n\n//????????????????±???????\ndouble closest_pair(C *a, int n)\n{\n if(n<=1) return INF;\n int m=n/2;\n double x=a[m].real();\n double d=min(closest_pair(a,m),closest_pair(a+m,n-m));\n inplace_merge(a,a+m,a+n,compyx);\n vector<C> b;\n rep(i,n){\n if(abs(x-a[i].real())>=d) continue;\n rep(j,b.size()){\n C dp=a[i]-b[b.size()-1-j];\n if(dp.imag()>=d) break;\n d=min(d,abs(dp));\n }\n b.push_back(a[i]);\n }\n return d;\n}\ndouble compute_shortest(C *a,int n)\n{\n sort(a,a+n);\n return closest_pair(a,n);\n}\n//2??????????????¢????????????(????????????2??????????????±?????\\????????°)\nint getstateCC(C c1,double r1,C c2,double r2)\n{\n double d=abs(c1-c2);\n if(d>r1+r2+EPS)return 4;\n if(d>r1+r2-EPS)return 3;\n if(d>abs(r1-r2)+EPS)return 2;\n if(d>abs(r1-r2)-EPS)return 1;\n return 0;\n}\n//?????????????????\\???????????????????????\\???\nC gettangentCP_(C c1,double r1,C p,int flg){\n C base=c1-p;\n double w=Sqrt(norm(base)-r1*r1);\n C s=p+base*C(w,r1 * flg)/norm(base)*w;\n return s;\n}\n//????????????????????\\???\nvector<L> gettangentCP(C c1,double r1,C p){\n vector<L> res;\n C s=gettangentCP_(c1,r1,p,1);\n C t=gettangentCP_(c1,r1,p,-1);\n //??????????????¨??????????????´???\n if(eq(s,t)){\n res.push_back(L(s,s+(c1-p)*C(0,1)));\n }else{\n res.push_back(L(p,s));\n res.push_back(L(p,t));\n }\n return res;\n}\n\n//2????????±????????\\???????±???????\nL getintangent(C c1,double r1,C c2,double r2,double flg)\n{\n C base=c2-c1;\n double w=r1+r2;\n double h=Sqrt(norm(base)-w*w);\n C k=base*C(w,h*flg)/norm(base);\n return L(c1+k*r1,c2-k*r2);\n}\n//2????????±????????\\???????±???????\nL getouttangent(C c1,double r1,C c2,double r2,double flg)\n{\n C base=c2-c1;\n double h=r2-r1;\n double w=Sqrt(norm(base)-h*h);\n C k=base*C(w,h*flg)/norm(base)*C(0,flg);\n return L(c1+k*r1,c2+k*r2);\n}\n//2????????±?????\\???????±???????(?????´??????????????????????????????????????\\???)\nvector<L> gettangentCC(C c1,double r1,C c2,double r2)\n{\n vector<L> res;\n double d=abs(c1-c2);\n if(d>r1+r2+EPS) res.push_back(getintangent(c1,r1,c2,r2,1));\n if(d>r1+r2-EPS) res.push_back(getintangent(c1,r1,c2,r2,-1));\n if(d>abs(r1-r2)+EPS) res.push_back(getouttangent(c1,r1,c2,r2,1));\n if(d>abs(r1-r2)-EPS) res.push_back(getouttangent(c1,r1,c2,r2,-1));\n return res;\n}\n\ndouble width(vector<P> vec,int n,double x)\n{\n double lb = INF,ub = -INF;\n rep(i,n){\n L hoge(C(vec[i].fi,vec[i].se),C(vec[(i+1)%n].fi,vec[(i+1)%n].se));\n L cri(C(x,101),C(x,-101));\n if(intersectLS(cri,hoge)){\n C res = crosspointLL(cri,hoge);\n lb = min(lb,res.imag());\n ub = max(ub,res.imag());\n }\n }\n return max(0.0,ub-lb);\n}\n\nint main()\n{\n while(1){\n int n,m;\n cin >> m >> n;\n if(n == 0 && m == 0){\n break;\n }\n vector<P> v1,v2;\n vector<int> ax;\n rep(i,m){\n int x,y;\n cin >> x >> y;\n v1.pb(P(x,y));\n ax.pb(x);\n }\n rep(i,n){\n int x,y;\n cin >> x >> y;\n v2.pb(P(x,y));\n ax.pb(x);\n }\n sort(all(ax));\n ax.erase(unique(all(ax)),ax.end());\n int mn1 = (*min_element(all(v1))).fi;\n int mx1 = (*max_element(all(v1))).fi;\n int mn2 = (*min_element(all(v2))).fi;\n int mx2 = (*max_element(all(v2))).fi;\n double res = 0.0;\n rep(i,ax.size()-1){\n double a = ax[i],b = ax[i+1],c = (a+b)/2;\n if(mn1 <= c && c <= mx1 && mn2 <= c && c <= mx2){\n double fa = width(v1,m,a) * width(v2,n,a);\n double fb = width(v1,m,b) * width(v2,n,b);\n double fc = width(v1,m,c) * width(v2,n,c);\n res += (b - a) / 6 * (fa + 4*fc + fb);\n }\n }\n printf(\"%.10f\\n\",res);\n }\n return 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3176, "score_of_the_acc": -0.9222, "final_rank": 10 }, { "submission_id": "aoj_1313_2040628", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\ntypedef double dbl;\n#define double long double\ntypedef complex<double> P;\ntypedef vector<P> G;\n#define REP(i,n) for(int i = 0 ; i < n ; i++)\ndouble eps = 1e-11;\ndouble EPS = eps;\n\nbool eq(double a,double b){\n\treturn fabs(a-b) < eps;\n}\n\ndouble cross(P a,P b) { return imag(conj(a)*b); }\ndouble dot(P a,P b) { return real(conj(a)*b);}\n\nvector<P> ps1,ps2;\n//??´???\nstruct L : public vector<P> {\n\tL(P a,P b) {\n\t\tpush_back(a); push_back(b);\n\t}\n\t\n};\nint ccw(P a, P b, P c) {\n\tb -= a; c -= a;\n\tif (cross(b, c) > 0) return +1;\t// a ??? b ??§???????¨??????????????????? b ??? c(???)\n\tif (cross(b, c) < 0) return -1;\t// a ??? b ??§????¨??????????????????? b ??? c(???)\n\tif (dot(b, c) < 0) return +2; \t// a???b??§????????????a??????????¶???????b???c(c--a--b)\n\tif (norm(b) < norm(c)) return -2;\t// a???b??§????????????b???c(a--b--c)\n\treturn 0;\t\t\t\t\t\t\t// a???b??§????????????b???c(????????? b == c)\n}\n \n\n\nP crosspoint(const L &l, const L &m) {\n\tdouble A = cross(l[1] - l[0], m[1] - m[0]);\n\tdouble B = cross(l[1] - l[0], l[1] - m[0]);\n\tif (abs(A) < EPS && abs(B) < EPS) return m[0];\n\treturn m[0] + B / A * (m[1] - m[0]);\n}\n \n \n// ???????§???¢???????????????????§???¢???????????´?????§????????????\n//???????????´?????????????????´?????????????????´?????????????????¨?????????????????¨?????????????????????????????¨????????????????????????\n#define curr(P, i) P[(i) % P.size()]\n#define next(P, i) P[(i+1)%P.size()]\n#define prev(P, i) P[(i+P.size()-1) % P.size()]\nG convex_cut(const G& poly, const L& l) {\n G Q;\n for (int i = 0; i < poly.size(); ++i) {\n P A = curr(poly, i), B = next(poly, i);\n if (ccw(l[0], l[1], A) != -1) Q.push_back(A);\n if (ccw(l[0], l[1], A)*ccw(l[0], l[1], B) < 0)\n Q.push_back(crosspoint(L(A, B), l));\n }\n return Q;\n}\n\n// ????´?????§???¢?????¢??????\"2???\"????±??????? O(n)\ndouble area2(const G& poly) {\n\tdouble A = 0;\n\tREP(i,poly.size()) A += cross(curr(poly, i), next(poly, i));\n\treturn abs(A);\n}\n\n\nvector<double> ux;\n\nP in(){\n\tdouble x,y;\n\tcin >> x >> y;\n\t\n\treturn P(x,y);\n}\npair<double,double> seg(const vector<P> &ps,double x){\n\tdouble mi = 1e9, ma = -1e9;\n\tauto reflesh = [&mi,&ma](double y){\n\t\tmi = min(mi,y);\n\t\tma = max(ma,y);\n\t};\n\tfor(int i = 0 ; i < ps.size() ; i++){\n\t\tP a = ps[i];\n\t\tP b = ps[(i+1)%ps.size()];\n\t\tif( a.real() > b.real() ) swap(a,b);\n\t\tif( eq(a.real(),x) ) reflesh(a.imag());\n\t\tif( eq(b.real(),x) ) reflesh(b.imag());\n\t\tif( a.real() + eps < x and x < b.real() - eps ){\n\t\t\treflesh(a.imag() + (x - a.real()) * (b.imag() - a.imag()) / (b.real()-a.real()));\n\t\t}\n\t}\n\tif( ma < mi ) return {0.0,0.0};\n\treturn {mi,ma};\n}\n\ndouble S(double l,double r){\n\tif( r <= l ) return 0;\n\tG g1 = convex_cut(ps1,L(P(l,0),P(l,-1)));\n\tG g2 = convex_cut(g1,L(P(r,0),P(r,+1)));\n\t\n\treturn area2(g2) / 2;\n\t\n}\n\ndouble Vf(double z){\n\tauto p = seg(ps2,z);\n\treturn S(p.first,p.second);\n}\n\ndouble Vs(double l,double r){\n\treturn (r-l)/6*(Vf(l)+4*Vf((l+r)/2)+Vf(r));\n}\n\ndouble V(double l,double r,int k=6){\n\tif( l >= r ) return 0;\n\tdouble m = (l+r)/2;\n\tdouble a = Vs(l,m);\n\tdouble b = Vs(m,r);\n\tdouble c = Vs(l,r);\n\tif( ( k <= 0) and eq(a+b,c) ){\n\t\treturn c;\n\t}else{\n\t\treturn V(l,m,k-1) + V(m,r,k-1);\n\t}\t\n}\n\nint main(){\n\tint m,n;\n\twhile(cin >> m >> n && m ){\n\t\t\n\t\tps1.clear();\n\t\tps2.clear();\n\t\tux.clear();\n\t\tfor(int i = 0 ; i < m ; i++){\n\t\t\tps1.push_back(in());\n\t\t\tux.push_back(ps1.back().real());\n\t\t}\n\n\t\tvector<double> uz;\n\t\tfor(int i = 0 ; i < n ; i++){\n\t\t\tps2.push_back(in());\n\t\t\tps2.back() = P(ps2.back().imag(),ps2.back().real());\n\t\t\tuz.push_back(ps2.back().real());\n\n\t\t}\n\t\t\n\t\tsort(uz.begin(),uz.end());\n\t\tsort(ux.begin(),ux.end());\n\t\tdouble vans = 0;\n\t\t//for( auto z : uz ) cout << z << \" \"; cout << endl;\n\t\t//for( auto x : ux ) cout << x << \" \"; cout << endl;\n\t\tfor(int i = 0 ; i+1 < uz.size() ; i++)\n\t\t\tif( uz[i] != uz[i+1] )\n\t\t\t\tvans += V(uz[i],uz[i+1]);\n\t\tprintf(\"%.8lf\\n\",(dbl)vans);\n\t}\n}", "accuracy": 1, "time_ms": 5650, "memory_kb": 3240, "score_of_the_acc": -1.9408, "final_rank": 16 }, { "submission_id": "aoj_1313_1195725", "code_snippet": "#include <iostream>\n#include <cstdio>\n#include <vector>\n#include <set>\n#include <map>\n#include <queue>\n#include <deque>\n#include <stack>\n#include <algorithm>\n#include <cstring>\n#include <functional>\n#include <cmath>\n#include <complex>\n#include <cassert>\nusing namespace std;\n#define rep(i,n) for(int i=0;i<(n);++i)\n#define rep1(i,n) for(int i=1;i<=(n);++i)\n#define all(c) (c).begin(),(c).end()\n#define fs first\n#define sc second\n#define pb push_back\n#define show(x) cout << #x << \" \" << x << endl\ntypedef long double D;\ntypedef complex<D> P;\ntypedef pair<P,P> L;\ntypedef vector<P> Pol;\nD eps=1e-11,Eps=1e-9;\nD cro(P a,P b){return imag(conj(a)*b);}\nint ccw(P a,P b,P c){\n\tif(cro(b-a,c-a)>eps) return 1;\n\tif(cro(b-a,c-a)<-eps) return -1;\n\tif(abs(abs(a-c)+abs(c-b)-abs(a-b))<eps) return 0;\n\tif(abs(abs(a-b)+abs(c-b)-abs(a-c))<eps) return -2;\n\treturn 2;\n}\nbool iLS(L l,L s){\n\tif(abs(cro(l.fs-l.sc,s.fs-s.sc))<eps) return 0;\n\treturn cro(l.sc-l.fs,s.fs-l.fs)*cro(l.sc-l.fs,s.sc-l.fs)<eps;\n}\nP intLL(L a,L b){\n\tD t=cro(a.sc-a.fs,a.sc-b.fs)/cro(a.sc-a.fs,b.sc-b.fs);\n\treturn b.fs+t*(b.sc-b.fs);\n}\nint M,N;\nPol a,b;\nD rec(D x){\n\tL l=L(P(x,0),P(x,1));\n\tPol inters;\n\trep(j,M){\n\t\tL e=L(a[j],a[(j+1)%M]);\n\t\tif(iLS(l,e)) inters.pb(intLL(l,e));\n\t}\n\tif(inters.size()<=1) return -1;\n\tD leny=0;\n\tfor(P p:inters) for(P q:inters) leny=max(leny,abs(p-q));\n\tinters.clear();\n\trep(j,N){\n\t\tL e=L(b[j],b[(j+1)%N]);\n\t\tif(iLS(l,e)) inters.pb(intLL(l,e));\n\t}\n\tif(inters.size()<=1) return -1;\n\tD lenz=0;\n\tfor(P p:inters) for(P q:inters) lenz=max(lenz,abs(p-q));\n\treturn leny*lenz;\n}\nint main(){\n\twhile(true){\n\t\tcin>>M>>N;\n\t\tif(M==0) break;\n\t\ta.clear(),b.clear();\n\t\tvector<D> xs;\n\t\tD ans=0;\n\t\trep(i,M){\n\t\t\tD x,y;\n\t\t\tcin>>x>>y;\n\t\t\ta.pb(P(x,y));\n\t\t\txs.pb(x-Eps);\n\t\t\txs.pb(x+Eps);\n\t\t}\n\t\trep(i,N){\n\t\t\tD x,y;\n\t\t\tcin>>x>>y;\n\t\t\tb.pb(P(x,y));\n\t\t\txs.pb(x-Eps);\n\t\t\txs.pb(x+Eps);\n\t\t}\n\t\tsort(all(xs));\n\t\trep(i,xs.size()-1){\n\t\t\tif(rec(xs[i])<-eps||rec(xs[i])<-eps) continue;\n\t\t\tans+=(xs[i+1]-xs[i])/6.0*(rec(xs[i])+4.0*rec((xs[i+1]+xs[i])/2)+rec(xs[i+1]));\n\t\t}\n\t\tprintf(\"%.4f\\n\",(double)ans);\n\t}\n}", "accuracy": 1, "time_ms": 110, "memory_kb": 1308, "score_of_the_acc": -0.3975, "final_rank": 7 }, { "submission_id": "aoj_1313_1156580", "code_snippet": "#include <cstdio>\n#include <cmath>\n#include <cstring>\n#include <cstdlib>\n#include <climits>\n#include <ctime>\n#include <queue>\n#include <stack>\n#include <algorithm>\n#include <list>\n#include <vector>\n#include <set>\n#include <map>\n#include <iostream>\n#include <deque>\n#include <complex>\n#include <string>\n#include <iomanip>\n#include <sstream>\n#include <bitset>\n#include <valarray>\n#include <unordered_map>\n#include <iterator>\n#include <assert.h>\nusing namespace std;\ntypedef long long int ll;\ntypedef unsigned int uint;\ntypedef unsigned char uchar;\ntypedef unsigned long long ull;\ntypedef pair<int, int> pii;\ntypedef pair<ll, ll> pll;\ntypedef vector<int> vi;\n\n#define REP(i,x) for(int i=0;i<(int)(x);i++)\n#define REPS(i,x) for(int i=1;i<=(int)(x);i++)\n#define RREP(i,x) for(int i=((int)(x)-1);i>=0;i--)\n#define RREPS(i,x) for(int i=((int)(x));i>0;i--)\n#define FOR(i,c) for(__typeof((c).begin())i=(c).begin();i!=(c).end();i++)\n#define RFOR(i,c) for(__typeof((c).rbegin())i=(c).rbegin();i!=(c).rend();i++)\n#define ALL(container) (container).begin(), (container).end()\n#define RALL(container) (container).rbegin(), (container).rend()\n#define SZ(container) ((int)container.size())\n#define mp(a,b) make_pair(a, b)\n#define UNIQUE(v) v.erase( unique(v.begin(), v.end()), v.end() );\n\ntemplate<class T> bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }\ntemplate<class T> bool chmin(T &a, const T &b) { if (a>b) { a=b; return 1; } return 0; }\ntemplate<class T> ostream& operator<<(ostream &os, const vector<T> &t) {\nos<<\"[\"; FOR(it,t) {if(it!=t.begin()) os<<\",\"; os<<*it;} os<<\"]\"; return os;\n}\ntemplate<class T> ostream& operator<<(ostream &os, const set<T> &t) {\nos<<\"{\"; FOR(it,t) {if(it!=t.begin()) os<<\",\"; os<<*it;} os<<\"}\"; return os;\n}\ntemplate<class S, class T> ostream& operator<<(ostream &os, const pair<S,T> &t) { return os<<\"(\"<<t.first<<\",\"<<t.second<<\")\";}\ntemplate<class S, class T> pair<S,T> operator+(const pair<S,T> &s, const pair<S,T> &t){ return pair<S,T>(s.first+t.first, s.second+t.second);}\ntemplate<class S, class T> pair<S,T> operator-(const pair<S,T> &s, const pair<S,T> &t){ return pair<S,T>(s.first-t.first, s.second-t.second);}\n\nnamespace geom{\n#define X real()\n#define Y imag()\n#define at(i) ((*this)[i])\n#define SELF (*this)\n\tenum {TRUE = 1, FALSE = 0, BORDER = -1};\n\ttypedef int BOOL;\n\ttypedef long double R;\n\tconst R INF = 1e8;\n\tR EPS = 1e-12;\n\tconst R PI = 3.1415926535897932384626;\n\tinline int sig(const R &x) { return (abs(x) < EPS ? 0 : x > 0 ? 1 : -1); }\n\tinline BOOL less(const R &x, const R &y) {return sig(x-y) ? x < y : BORDER;}\n\ttypedef complex<R> P;\n\tinline R norm(const P &p){return p.X*p.X+p.Y*p.Y;}\n\tinline R inp(const P& a, const P& b){return (conj(a)*b).X;}\n\tinline R outp(const P& a, const P& b){return (conj(a)*b).Y;}\n\tinline P unit(const P& p){return p/abs(p);}\n\tinline P proj(const P &s, const P &t){return t*inp(s, t)/norm(t);}\n\tinline int ccw(const P &s, const P &t, const P &p, int adv=0){\n\t\tint res = sig(outp(t-s, p-s));\n\t\tif(res || !adv) return res;\n\t\tif(sig(inp(t-s, p-s)) < 0) return -2;\t// p-s-t\n\t\tif(sig(inp(s-t, p-t)) < 0) return 2;\t// s-t-p\n\t\treturn 0;\t\t\t\t\t\t\t\t// s-p-t\n\t}\n\t\n\t\n\tstruct L : public vector<P>{\t// line\n\t\tL(const P &p1, const P &p2){this->push_back(p1);this->push_back(p2);}\n\t\tL(){}\n\t\tP dir()const {return at(1) - at(0);}\n\t};\n\tstruct S : public L{\t// segment\n\t\tS(const P &p1, const P &p2):L(p1, p2){}\n\t\tS(){}\n\t};\n\tstruct G : public vector<P>{\n\t\tG(size_type size=0):vector(size){}\n\t\tS edge(int i)const {return S(at(i), at(i+1 == size() ? 0 : i+1));}\n\t};\n\tBOOL intersect(const S &s, const S &t){\n\t\tconst int p = ccw(t[0], t[1], s[0], 1) * ccw(t[0], t[1], s[1], 1);\n\t\tconst int q = ccw(s[0], s[1], t[0], 1) * ccw(s[0], s[1], t[1], 1);\n\t\treturn (p>0||q>0) ? FALSE : (!p||!q) ? BORDER : TRUE;\n\t}\n\tBOOL intersect(const S &s, const L &l){\n\t\treturn (sig(outp(l.dir(), s[0]-l[0])) * sig(outp(l.dir(), s[1]-l[0])) <= 0);\n\t}\n\tinline P crosspoint(const L &l, const L &m){\n\t\tR A = outp(l.dir(), m.dir()), B = outp(l.dir(), l[1] - m[0]);\n\t\tif(!sig(abs(A)) && !sig(abs(B))) return m[0]; // same line\n\t\tif(abs(A) < EPS) assert(false); // !!!PRECONDITION NOT SATISFIED!!!\n\t\treturn m[0] + B / A * (m[1] - m[0]);\n\t}\n\n\ttemplate<class Func> R simpson(R s, R t, Func func){\n\t\treturn (t-s)/6*(func(s+2*EPS) + func(t-2*EPS) + 4*func((s+t)*(R).5));\n\t}\n#undef SELF\n#undef at\n}\n\nusing namespace geom;\n\nint f = 0;\nnamespace std{\n\tbool operator<(const P &a, const P &b){return sig(a.X-b.X) ? a.X < b.X : a.Y+EPS < b.Y;}\n\tbool operator==(const P &a, const P &b){return abs(a-b) < EPS;}\n\tistream& operator>>(istream &is, P &p){R x,y;is>>x>>y;p=P(x, y);return is;}\n\tistream& operator>>(istream &is, L &l){l.resize(2);return is >> l[0] >> l[1];}\n}\n\nint n, m;\n\nint main(){\n\tios::sync_with_stdio(false);\n\twhile(cin >> m >> n, n){\n\t\tG g1(m), g2(n);\n\t\tvector<R> x;\n\t\tREP(i, m){\n\t\t\tcin >> g1[i];\n\t\t\tx.push_back(g1[i].X);\n\t\t}\n\t\tREP(i, n){\n\t\t\tcin >> g2[i];\n\t\t\tx.push_back(g2[i].X);\n\t\t}\n\t\tsort(ALL(x));UNIQUE(x);\n\t\t\n\t\tauto width = [&](const G &g, R x){\n\t\t\tL l(P(x, 0), P(x, 1));\n\t\t\tR b = INF, u = -INF;\n\t\t\tREP(i, g.size()){\n\t\t\t\tif(sig(g.edge(i).dir().X) && intersect(g.edge(i), l)){\n\t\t\t\t\tP p = crosspoint(g.edge(i), l);\n\t\t\t\t\tb = min(b, p.Y);\n\t\t\t\t\tu = max(u, p.Y);\n\t\t\t\t}\n\t\t\t}\n\t\t\treturn max<R>(0, u-b);\n\t\t};\n\t\tauto area = [&](R x){\n\t\t\treturn width(g1, x) * width(g2, x);\n\t\t};\n\t\t\n\t\tR ans = 0;\n\t\tREP(i, (int)x.size()-1){\n\t\t\tans += simpson(x[i], x[i+1], area);\n\t\t}\n\t\tprintf(\"%.10f\\n\", (double)ans);\n\t}\n\treturn 0;\n}", "accuracy": 1, "time_ms": 40, "memory_kb": 1308, "score_of_the_acc": -0.3851, "final_rank": 6 }, { "submission_id": "aoj_1313_1113794", "code_snippet": "#include<bits/stdc++.h>\n#define rep(i,n) for(int i=0;i<(int)(n);i++)\n#define all(a) (a).begin(),(a).end()\n#define EQ(a,b) (abs((a)-(b)) < EPS)\n#define fs first\n#define sc second\nusing namespace std;\ntypedef double D;\ntypedef complex<D> P;\ntypedef pair<P,P> L;\n\nconst D EPS = 1e-8;\n\nnamespace std{\n bool operator<(const P &a,const P &b){\n return EQ(real(a),real(b))?imag(a)<imag(b):real(a)<real(b);\n }\n bool operator==(const P &a, const P &b){return EQ(a,b);}\n}\n\ninline D dot(P x, P y){return real(conj(x)*y);}\ninline D cross(P x, P y){return imag(conj(x)*y);}\n\ninline int ccw(P a,P b,P c){\n b -= a;c -= a;\n if (cross(b,c)>EPS) return 1; //counter clockwise\n if (cross(b,c)<-EPS) return -1; //clockwise\n if (dot(b, c)<-EPS) return 2; //c--a--b on line\n if (abs(b)+EPS<abs(c)) return -2; //a--b--c on line\n return 0; //on segment\n}\n\ninline bool para(L a,L b){return abs(cross(a.fs-a.sc,b.fs-b.sc))<EPS;}\n\ninline bool is_cp(L a,L b){\n if(para(a,b))return false;\n if(ccw(a.fs,a.sc,b.fs)*ccw(a.fs,a.sc,b.sc)<=0)\n if(ccw(b.fs,b.sc,a.fs)*ccw(b.fs,b.sc,a.sc)<=0)return true;\n return false;\n}\n\ninline P line_cp(L a,L b){\n return a.fs+(a.sc-a.fs)*cross(b.sc-b.fs,b.fs-a.fs)/cross(b.sc-b.fs,a.sc-a.fs);\n}\n\nvector<P> p[2];\ninline D len(D x, int id){\n L l = L( P(x,-1000), P(x,1000) );\n vector<P> res;\n rep(i,p[id].size()){\n L a = L( p[id][i], p[id][(i+1)%p[id].size()] );\n if(is_cp(a,l)){\n P cp = line_cp(a,l);\n if(!EQ(cp,p[id][i]))res.push_back(cp);\n }else if(para(a,l) && ccw(l.fs,l.sc,a.sc)==0){\n res.push_back(a.sc);\n }\n }\n\n if(res.size()<2)return 0;\n return abs(res[0]-res[1]);\n}\n\ninline D f(D x){\n return len(x/2,0) * len(x/2,1);\n}\n\nint main(){\n int m,n;\n while(cin >> m >> n, m){\n int minx[2], maxx[2];\n p[0].resize(m); p[1].resize(n);\n minx[0] = minx[1] = 1000;\n maxx[0] = maxx[1] = -1000;\n\n rep(i,m){\n int x,y;\n cin >> x >> y;\n p[0][i] = P(x,y);\n minx[0]=min(minx[0],x);\n maxx[0]=max(maxx[0],x);\n }\n\n rep(i,n){\n int x,z;\n cin >> x >> z;\n p[1][i] = P(x,z);\n minx[1]=min(minx[1],x);\n maxx[1]=max(maxx[1],x);\n }\n\n D ans = 0;\n for(D i=-199;i<200;i+=2){\n if(2*minx[0]<=i && i<=2*maxx[0]){\n\tif(2*minx[1]<=i && i<=2*maxx[1]){\n\t ans += f(i-1)+4*f(i)+f(i+1);\n\t}\n }\n }\n cout << fixed << setprecision(9) << ans/6 << endl;\n }\n}", "accuracy": 1, "time_ms": 20, "memory_kb": 1288, "score_of_the_acc": -0.3758, "final_rank": 3 }, { "submission_id": "aoj_1313_1113792", "code_snippet": "#include<bits/stdc++.h>\n#define rep(i,n) for(int i=0;i<(int)(n);i++)\n#define all(a) (a).begin(),(a).end()\n#define EQ(a,b) (abs((a)-(b)) < EPS)\n#define fs first\n#define sc second\nusing namespace std;\ntypedef double D;\ntypedef complex<D> P;\ntypedef pair<P,P> L;\n\nconst D EPS = 1e-8;\n\nnamespace std{\n bool operator<(const P &a,const P &b){\n return EQ(real(a),real(b))?imag(a)<imag(b):real(a)<real(b);\n }\n bool operator==(const P &a, const P &b){return EQ(a,b);}\n}\n\ninline D dot(P x, P y){return real(conj(x)*y);}\ninline D cross(P x, P y){return imag(conj(x)*y);}\n\nint ccw(P a,P b,P c){\n b -= a;c -= a;\n if (cross(b,c)>EPS) return 1; //counter clockwise\n if (cross(b,c)<-EPS) return -1; //clockwise\n if (dot(b, c)<-EPS) return 2; //c--a--b on line\n if (abs(b)+EPS<abs(c)) return -2; //a--b--c on line\n return 0; //on segment\n}\n\ninline bool para(L a,L b){return abs(cross(a.fs-a.sc,b.fs-b.sc))<EPS;}\n\ninline bool is_cp(L a,L b){\n if(para(a,b))return false;\n if(ccw(a.fs,a.sc,b.fs)*ccw(a.fs,a.sc,b.sc)<=0)\n if(ccw(b.fs,b.sc,a.fs)*ccw(b.fs,b.sc,a.sc)<=0)return true;\n return false;\n}\n\ninline P line_cp(L a,L b){\n return a.fs+(a.sc-a.fs)*cross(b.sc-b.fs,b.fs-a.fs)/cross(b.sc-b.fs,a.sc-a.fs);\n}\n\nvector<P> p[2];\ninline D len(D x, int id){\n L l = L( P(x,-1000), P(x,1000) );\n vector<P> res;\n rep(i,p[id].size()){\n L a = L( p[id][i], p[id][(i+1)%p[id].size()] );\n if(is_cp(a,l)){\n P cp = line_cp(a,l);\n if(!EQ(cp,p[id][i]))res.push_back(cp);\n }else if(para(a,l) && ccw(l.fs,l.sc,a.sc)==0){\n res.push_back(a.sc);\n }\n }\n\n if(res.size()<2)return 0;\n return abs(res[0]-res[1]);\n}\n\ninline D f(D x){\n return len(x/2,0) * len(x/2,1);\n}\n\nint main(){\n int m,n;\n while(cin >> m >> n, m){\n int minx[2], maxx[2];\n p[0].resize(m); p[1].resize(n);\n minx[0] = minx[1] = 1000;\n maxx[0] = maxx[1] = -1000;\n\n rep(i,m){\n int x,y;\n cin >> x >> y;\n p[0][i] = P(x,y);\n minx[0]=min(minx[0],x);\n maxx[0]=max(maxx[0],x);\n }\n\n rep(i,n){\n int x,z;\n cin >> x >> z;\n p[1][i] = P(x,z);\n minx[1]=min(minx[1],x);\n maxx[1]=max(maxx[1],x);\n }\n\n D ans = 0;\n for(D i=-199;i<200;i+=2){\n if(2*minx[0]<=i && i<=2*maxx[0]){\n\tif(2*minx[1]<=i && i<=2*maxx[1]){\n\t ans += f(i-1)+4*f(i)+f(i+1);\n\t}\n }\n }\n cout << fixed << setprecision(9) << ans/6 << endl;\n }\n}", "accuracy": 1, "time_ms": 30, "memory_kb": 1284, "score_of_the_acc": -0.3764, "final_rank": 4 }, { "submission_id": "aoj_1313_1113791", "code_snippet": "#include<bits/stdc++.h>\n#define rep(i,n) for(int i=0;i<(int)(n);i++)\n#define all(a) (a).begin(),(a).end()\n#define EQ(a,b) (abs((a)-(b)) < EPS)\n#define fs first\n#define sc second\nusing namespace std;\ntypedef double D;\ntypedef complex<D> P;\ntypedef pair<P,P> L;\n\nconst D EPS = 1e-8;\n\nnamespace std{\n bool operator<(const P &a,const P &b){\n return EQ(real(a),real(b))?imag(a)<imag(b):real(a)<real(b);\n }\n bool operator==(const P &a, const P &b){return EQ(a,b);}\n}\n\ninline D dot(P x, P y){return real(conj(x)*y);}\ninline D cross(P x, P y){return imag(conj(x)*y);}\n\nint ccw(P a,P b,P c){\n b -= a;c -= a;\n if (cross(b,c)>EPS) return 1; //counter clockwise\n if (cross(b,c)<-EPS) return -1; //clockwise\n if (dot(b, c)<-EPS) return 2; //c--a--b on line\n if (abs(b)+EPS<abs(c)) return -2; //a--b--c on line\n return 0; //on segment\n}\n\ninline bool para(L a,L b){return abs(cross(a.fs-a.sc,b.fs-b.sc))<EPS;}\n\ninline bool is_cp(L a,L b){\n if(para(a,b))return false;\n if(ccw(a.fs,a.sc,b.fs)*ccw(a.fs,a.sc,b.sc)<=0)\n if(ccw(b.fs,b.sc,a.fs)*ccw(b.fs,b.sc,a.sc)<=0)return true;\n return false;\n}\n\ninline P line_cp(L a,L b){\n return a.fs+(a.sc-a.fs)*cross(b.sc-b.fs,b.fs-a.fs)/cross(b.sc-b.fs,a.sc-a.fs);\n}\n\nvector<P> p[2];\ninline D len(D x, int id){\n L l = L( P(x,-1000), P(x,1000) );\n vector<P> res;\n rep(i,p[id].size()){\n L a = L( p[id][i], p[id][(i+1)%p[id].size()] );\n if(is_cp(a,l)){\n P cp = line_cp(a,l);\n if(!EQ(cp,p[id][i]))res.push_back(cp);\n }else if(para(a,l) && ccw(l.fs,l.sc,a.sc)==0){\n res.push_back(a.sc);\n }\n }\n\n if(res.size()<2)return 0;\n return abs(res[0]-res[1]);\n}\n\ninline D f(D x){\n return len(x,0) * len(x,1);\n}\n\nint main(){\n int m,n;\n while(cin >> m >> n, m){\n D minx[2], maxx[2];\n p[0].resize(m); p[1].resize(n);\n minx[0] = minx[1] = 1000;\n maxx[0] = maxx[1] = -1000;\n\n rep(i,m){\n D x,y;\n cin >> x >> y;\n p[0][i] = P(x,y);\n minx[0]=min(minx[0],x);\n maxx[0]=max(maxx[0],x);\n }\n\n rep(i,n){\n D x,z;\n cin >> x >> z;\n p[1][i] = P(x,z);\n minx[1]=min(minx[1],x);\n maxx[1]=max(maxx[1],x);\n }\n\n D ans = 0;\n for(D i=-100;i<100;i+=1){\n if(minx[0]<=i+0.5 && i+0.5<=maxx[0]){\n\tif(minx[1]<=i+0.5 && i+0.5<=maxx[1]){\n\t ans += f(i)+4*f(i+0.5)+f(i+1);\n\t}\n }\n }\n cout << fixed << setprecision(9) << ans/6 << endl;\n }\n}", "accuracy": 1, "time_ms": 20, "memory_kb": 1320, "score_of_the_acc": -0.385, "final_rank": 5 }, { "submission_id": "aoj_1313_1113790", "code_snippet": "#include<bits/stdc++.h>\n#define rep(i,n) for(int i=0;i<(int)(n);i++)\n#define all(a) (a).begin(),(a).end()\n#define EQ(a,b) (abs((a)-(b)) < EPS)\n#define fs first\n#define sc second\nusing namespace std;\ntypedef long double D;\ntypedef complex<D> P;\ntypedef pair<P,P> L;\n\nconst D EPS = 1e-8;\n\nnamespace std{\n bool operator<(const P &a,const P &b){\n return EQ(real(a),real(b))?imag(a)<imag(b):real(a)<real(b);\n }\n bool operator==(const P &a, const P &b){return EQ(a,b);}\n}\n\ninline D dot(P x, P y){return real(conj(x)*y);}\ninline D cross(P x, P y){return imag(conj(x)*y);}\n\nint ccw(P a,P b,P c){\n b -= a;c -= a;\n if (cross(b,c)>EPS) return 1; //counter clockwise\n if (cross(b,c)<-EPS) return -1; //clockwise\n if (dot(b, c)<-EPS) return 2; //c--a--b on line\n if (abs(b)+EPS<abs(c)) return -2; //a--b--c on line\n return 0; //on segment\n}\n\ninline bool para(L a,L b){return abs(cross(a.fs-a.sc,b.fs-b.sc))<EPS;}\n\ninline bool is_cp(L a,L b){\n if(para(a,b))return false;\n if(ccw(a.fs,a.sc,b.fs)*ccw(a.fs,a.sc,b.sc)<=0)\n if(ccw(b.fs,b.sc,a.fs)*ccw(b.fs,b.sc,a.sc)<=0)return true;\n return false;\n}\n\ninline P line_cp(L a,L b){\n return a.fs+(a.sc-a.fs)*cross(b.sc-b.fs,b.fs-a.fs)/cross(b.sc-b.fs,a.sc-a.fs);\n}\n\nvector<P> p[2];\ninline D len(D x, int id){\n L l = L( P(x,-1000), P(x,1000) );\n vector<P> res;\n rep(i,p[id].size()){\n L a = L( p[id][i], p[id][(i+1)%p[id].size()] );\n if(is_cp(a,l)){\n P cp = line_cp(a,l);\n if(!EQ(cp,p[id][i]))res.push_back(cp);\n }else if(para(a,l) && ccw(l.fs,l.sc,a.sc)==0){\n res.push_back(a.sc);\n }\n }\n\n if(res.size()<2)return 0;\n return abs(res[0]-res[1]);\n}\n\ninline D f(D x){\n return len(x,0) * len(x,1);\n}\n\nint main(){\n int m,n;\n while(cin >> m >> n, m){\n D minx[2], maxx[2];\n p[0].resize(m); p[1].resize(n);\n minx[0] = minx[1] = 1000;\n maxx[0] = maxx[1] = -1000;\n\n rep(i,m){\n D x,y;\n cin >> x >> y;\n p[0][i] = P(x,y);\n minx[0]=min(minx[0],x);\n maxx[0]=max(maxx[0],x);\n }\n\n rep(i,n){\n D x,z;\n cin >> x >> z;\n p[1][i] = P(x,z);\n minx[1]=min(minx[1],x);\n maxx[1]=max(maxx[1],x);\n }\n\n D ans = 0;\n for(D i=-100;i<100;i+=1){\n if(minx[0]<=i+0.5 && i+0.5<=maxx[0]){\n\tif(minx[1]<=i+0.5 && i+0.5<=maxx[1]){\n\t ans += f(i)+4*f(i+0.5)+f(i+1);\n\t}\n }\n }\n cout << fixed << setprecision(9) << ans/6 << endl;\n }\n}", "accuracy": 1, "time_ms": 90, "memory_kb": 1328, "score_of_the_acc": -0.3998, "final_rank": 8 }, { "submission_id": "aoj_1313_990797", "code_snippet": "#include <bits/stdc++.h>\n\nusing namespace std;\n\n#define rep(i, n) for (int i = 0; i < int(n); ++i)\n\nconst int DIV = 500000;\n\nint m, n;\ndouble x1[100], y[100], x2[100], z[100];\ndouble mnx, mxx;\n\ndouble solve(double x) {\n double y1 = 0, y2 = 0, z1 = 0, z2 = 0;\n static int px11 = 0, px12 = 0, px21 = 0, px22 = 0;\n rep (ii, m) {\n int i = (px11 + ii) % m;\n if (x1[i] != x1[(i + 1) % m] && x1[i] <= x && x <= x1[(i + 1) % m]) {\n y1 = y[i] + (y[(i + 1) % m] - y[i]) / (x1[(i + 1) % m] - x1[i]) * (x - x1[i]);\n px11 = i;\n break;\n }\n }\n rep (ii, m) {\n int i = (px12 - ii + m) % m;\n if (x1[i] != x1[(i + 1) % m] && x1[(i + 1) % m] <= x && x <= x1[i]) {\n y2 = y[i] + (y[(i + 1) % m] - y[i]) / (x1[(i + 1) % m] - x1[i]) * (x - x1[i]);\n px12 = i;\n break;\n }\n }\n rep (ii, n) {\n int i = (px21 + ii) % n;\n if (x2[i] != x2[(i + 1) % n] && x2[i] <= x && x <= x2[(i + 1) % n]) {\n z1 = z[i] + (z[(i + 1) % n] - z[i]) / (x2[(i + 1) % n] - x2[i]) * (x - x2[i]);\n px21 = i;\n break;\n }\n }\n rep (ii, n) {\n int i = (px22 - ii + n) % n;\n if (x2[i] != x2[(i + 1) % n] && x2[(i + 1) % n] <= x && x <= x2[i]) {\n z2 = z[i] + (z[(i + 1) % n] - z[i]) / (x2[(i + 1) % n] - x2[i]) * (x - x2[i]);\n px22 = i;\n break;\n }\n }\n return (y2 - y1) * (z2 - z1);\n}\n\ndouble simpson(double l, double r, double f(double), int k = 1){\n double h = (r - l) / (2 * k);\n double fo = 0, fe = 0;\n for(int i = 1; i <= 2 * k - 3; i += 2){\n fo += f(l + h * i);\n fe += f(l + h * (i + 1));\n }\n return (f(l) + f(r) + 4 * (fo + f(r - h)) + 2 * fe) * h / 3;\n}\n\nint main() {\n while (true) {\n cin >> m >> n;\n if (m == 0 && n == 0) break;\n rep (i, m) cin >> x1[i] >> y[i];\n rep (i, n) cin >> x2[i] >> z[i];\n double mnx = max(*min_element(x1, x1 + m), *min_element(x2, x2 + n));\n double mxx = min(*max_element(x1, x1 + m), *max_element(x2, x2 + n));\n printf(\"%.12lf\\n\", simpson(mnx, mxx, solve, DIV));\n }\n}", "accuracy": 1, "time_ms": 5610, "memory_kb": 1248, "score_of_the_acc": -1.3553, "final_rank": 15 }, { "submission_id": "aoj_1313_802145", "code_snippet": "#include <bits/stdc++.h>\n#define REP(i,n) for(int i=0; i<(int)(n); ++i)\n\nusing namespace std;\n\nconst double EPS = 1e-8;\ntypedef pair<long double, long double> P;\nvector<P> vp1;\nvector<P> vp2;\n\nlong double width(long double x, const vector<P>& v){\n long double top = -1e8, bottom = 1e8;\n REP(i, v.size()){\n P p1 = v[i], p2 = v[(i + 1) % v.size()];\n if(abs(p1.first - p2.first) < EPS){\n if(abs(p1.first - x) < EPS){\n top = max(top, p1.second);\n top = max(top, p2.second);\n bottom = min(bottom, p1.second);\n bottom = min(bottom, p2.second);\n }\n continue;\n }\n if(p1.first > p2.first) swap(p1, p2);\n if(p1.first - EPS < x && x < p2.first + EPS){\n long double ratio = (x - p1.first) / (p2.first - p1.first);\n long double y = p1.second + ratio * (p2.second - p1.second);\n top = max(top, y);\n bottom = min(bottom, y);\n }\n }\n return max(0.0L, top - bottom);\n}\nlong double f(long double x){\n long double w = width(x, vp1) * width(x, vp2);\n return w;\n}\n\nlong double simpson(long double l, long double r, int N){\n long double h = (r - l) / (2 * N); \n long double S = f(l) + f(r);\n for(int i = 1; i < 2 * N; i += 2){\n S += 4.0 * f(l + h * i);\n }\n for(int i = 2; i < 2 * N; i += 2){\n S += 2.0 * f(l + h * i);\n }\n return S * h / 3.0;\n}\n\n\nint main(){\n int M, N;\n while(cin >> M >> N && M){\n vp1.resize(M);\n vp2.resize(N);\n REP(i, M) cin >> vp1[i].first >> vp1[i].second;\n REP(i, N) cin >> vp2[i].first >> vp2[i].second;\n long double left1 = vp1[0].first, right1 = vp1[0].first;\n long double left2 = vp2[0].first, right2 = vp2[0].first;\n REP(i, M) left1 = min(left1, vp1[i].first);\n REP(i, N) left2 = min(left2, vp2[i].first);\n REP(i, M) right1 = max(right1, vp1[i].first);\n REP(i, N) right2 = max(right2, vp2[i].first);\n vector<long double> vx;\n long double left = max(left1, left2), right = min(right1, right2);\n vx.push_back(left); vx.push_back(right);\n REP(i, M) if(left < vp1[i].first && vp1[i].first < right) vx.push_back(vp1[i].first);\n REP(i, N) if(left < vp2[i].first && vp2[i].first < right) vx.push_back(vp2[i].first);\n long double S = 0;\n sort(vx.begin(), vx.end());\n REP(i, vx.size() - 1){\n S += simpson(vx[i], vx[i + 1], 100);\n }\n printf(\"%.16Lf\\n\", S);\n }\n return 0;\n}", "accuracy": 1, "time_ms": 300, "memory_kb": 1272, "score_of_the_acc": -0.4208, "final_rank": 9 }, { "submission_id": "aoj_1313_350388", "code_snippet": "#include <iostream>\n#include <iomanip>\n#include <sstream>\n#include <cstdio>\n#include <string>\n#include <vector>\n#include <algorithm>\n#include <complex>\n#include <cstring>\n#include <cstdlib>\n#include <cmath>\n#include <cassert>\n#include <climits>\n#include <queue>\n#include <set>\n#include <map>\n#include <valarray>\n#include <bitset>\n#include <stack>\nusing namespace std;\n\n#define REP(i,n) for(int i=0;i<(int)n;++i)\n#define FOR(i,c) for(__typeof((c).begin())i=(c).begin();i!=(c).end();++i)\n#define ALL(c) (c).begin(), (c).end()\ntypedef long long ll;\ntypedef pair<int,int> pii;\nconst int INF = 1<<29;\nconst double PI = acos(-1);\nconst double EPS = 1e-8;\ntypedef pair<double, double> pdd;\n\ntypedef complex<double> P;\nnamespace std {\n bool operator < (const P& a, const P& b) {\n return real(a) != real(b) ? real(a) < real(b) : imag(a) < imag(b);\n }\n}\ndouble cross(const P& a, const P& b) {\n return imag(conj(a)*b);\n}\ndouble dot(const P& a, const P& b) {\n return real(conj(a)*b);\n}\nstruct L : public vector<P> {\n L(const P &a, const P &b) {\n push_back(a); push_back(b);\n }\n};\ntypedef vector<P> G;\n#define curr(P, i) P[i]\n#define next(P, i) P[(i+1)%P.size()]\nstruct C {\n P p; double r;\n C(const P &p, double r) : p(p), r(r) { }\n};\n\nint ccw(P a, P b, P c) {\n b -= a; c -= a;\n if (cross(b, c) > 0) return +1; // counter clockwise\n if (cross(b, c) < 0) return -1; // clockwise\n if (dot(b, c) < 0) return +2; // c--a--b on line\n if (norm(b) < norm(c)) return -2; // a--b--c on line\n return 0;\n}\nbool intersectLS(const L &l, const L &s) {\n return cross(l[1]-l[0], s[0]-l[0])* // s[0] is left of l\n cross(l[1]-l[0], s[1]-l[0]) < EPS; // s[1] is right of l\n}\nP crosspoint(const L &l, const L &m) {\n double A = cross(l[1] - l[0], m[1] - m[0]);\n double B = cross(l[1] - l[0], l[1] - m[0]);\n if (abs(A) < EPS && abs(B) < EPS) return m[0]; // same line\n if (abs(A) < EPS) assert(false); // !!!PRECONDITION NOT SATISFIED!!!\n return m[0] + B / A * (m[1] - m[0]);\n}\n\ndouble width(int *X, int *Y, int n, double x) {\n double lb = INF, ub = -INF;\n L l = L(P(x,0), P(x,1));\n REP(i, n) {\n L s = L(P(X[i], Y[i]), P(X[(i+1)%n], Y[(i+1)%n]));\n if (intersectLS(l, s)) {\n P p = crosspoint(l, s);\n double y = p.imag();\n lb = min(lb, y);\n ub = max(ub, y);\n }\n }\n return max(0.0, ub-lb);\n}\nint main() {\n int m, n;\n while(cin >> m >> n, m||n) {\n int X1[m], Y1[m], X2[n], Z2[n];\n REP(i, m) cin >> X1[i] >> Y1[i];\n REP(i, n) cin >> X2[i] >> Z2[i];\n int min1 = *min_element(X1, X1+m), max1 = *max_element(X1, X1+m);\n int min2 = *min_element(X2, X2+n), max2 = *max_element(X2, X2+n);\n vector<int> xs;\n REP(i, m) xs.push_back(X1[i]);\n REP(i, n) xs.push_back(X2[i]);\n sort(ALL(xs));\n\n double res = 0;\n REP(i, xs.size()-1) {\n double a = xs[i], b = xs[i+1], c = (a+b)/2;\n if (min1 <= c && c <= max1 && min2 <= c && c<= max2) {\n double fa = width(X1, Y1, m, a) * width(X2, Z2, n, a);\n double fb = width(X1, Y1, m, b) * width(X2, Z2, n, b);\n double fc = width(X1, Y1, m, c) * width(X2, Z2, n, c);\n res += (b-a) / 6 * (fa + 4*fc + fb);\n }\n }\n printf(\"%.10f\\n\", res);\n }\n}", "accuracy": 1, "time_ms": 50, "memory_kb": 968, "score_of_the_acc": -0.2882, "final_rank": 2 }, { "submission_id": "aoj_1313_152809", "code_snippet": "#include<complex>\n#include<vector>\n#include<iostream>\n#include<stack>\n#include<cmath>\n#include<map>\n\nenum CCW{FRONT=0x01,RIGHT=0x02,BACK=0x04,LEFT=0x08,OVER=0x10};\nenum CIRCLE_RELATION{CIRCLE_SAME=0x01,CIRCLE_CONTAIN=0x02,\n\t\t CIRCLE_NO_CROSS=0x04,CIRCLE_ONE_CROSS=0x08,\n\t\t CIRCLE_ONE_INSIDE_CROSS=0x10,CIRCLE_TWO_CROSS=0x20};\n#define sc second\n#define fr first\n\n#define REP(i,n) for(int i = 0; i < (int)(n); ++i)\n\nusing namespace std;\n\ntypedef double elem;\ntypedef complex<elem> point, vec;\ntypedef pair<point, point> line, hline, seg, pp;\n\nconst double infty = 1e40;\nconst double eps = 1.0e-8;\nconst double pi = acos(-1.0);\npoint base(0,0);\n\n// ”’l‰‰ŽZ\ninline elem sq(elem a){ return a*a; }\ninline elem cq(elem a){ return a*a*a; }\n\n// Šp“x•ÏŠ·\nelem rad(elem deg){ return (deg/180)*pi; }\nelem deg(elem rad){ return (rad*180)/pi; }\n\n// •‚“®¬”“_‚ÌŽô‚¢A‚È‚Ç\nbool eq(elem a, elem b){ return abs(a-b) < eps; }\nbool eqv(vec a, vec b){ return eq( abs(b-a),0); }\n\n// “_ƒIƒyƒŒ[ƒ^\nbool far(point a, point b){ return abs(b-a)>0; }\nbool near(point a, point b){ return abs(b-a)<=0; }\nelem dot(vec a, vec b){ return (a.real() * b.real() + a.imag() * b.imag() ); }\nelem cross(vec a, vec b){ return ( a.real() * b.imag() - a.imag() * b.real() ); }\n\n// a‚©‚çb‚܂ŎžŒv‰ñ‚è‚ÌŠp“xA“àŠpA“]‰ñ“]\nelem varg(vec a, vec b){\n elem ret=arg(a)-arg(b);\n if(ret<0)ret+=2*pi;\n if(ret>2*pi)ret-=2*pi;\n if(eq(ret,2*pi))ret=0;\n return ret;\n}\nelem varg2(vec a, vec b){\n elem ret = varg(a,b);\n if(ret>pi)return 2*pi-ret;\n return ret;\n}\nelem arg(vec a, vec b){ return acos( dot(a,b) / ( abs(a) * abs(b) ) ); }\npoint rot(point p, elem theta){ return p * polar((elem)1.0, theta); }\npoint rotdeg(point p, elem deg){ return p * polar((elem)1.0, rad(deg)); }\npoint proj(line l, point p){\n double t=dot(p-l.first,l.first-l.second)/abs(l.first-l.second);\n return l.first + t*(l.first-l.second);\n}\npoint reflect(line l, point p){ return p+2.0*(proj(l,p)-p); }\n\n// “ñ“_ŠÔ‹——£A’¼ü‚Æ“_‚̍ŒZ‹——£Aü•ª‚Æ“_‚̍ŒZ‹——£\nelem dist(point a, point b){ return abs(a-b); }\nelem dist_l(line l, point x){ return abs(cross(l.sc-l.fr,x-l.fr)) / abs(l.sc-l.fr); }\nelem dist_seg(seg s, point x)\n{\n if( dot(s.sc-s.fr,x-s.fr)<0 ) return abs(x-s.fr);\n if( dot(s.fr-s.sc,x-s.sc)<0 ) return abs(x-s.sc);\n return dist_l(s,x);\n}\n\n// ’PˆÊƒxƒNƒgƒ‹A–@üƒxƒNƒgƒ‹A’PˆÊ–@üƒxƒNƒgƒ‹\nvec uvec(vec a){ return a / abs(a); }\nvec nmr(vec a){ return a * vec(0,-1); }\nvec nml(vec a){ return a * vec(0,1); }\nvec unmr(vec a){ return uvec( nmr(a) ); }\nvec unml(vec a){ return uvec( nml(a) ); }\n\n// ’¼ŒðA•½s”»’è\nbool orth(point a1, point a2, point b1, point b2){ return eq( dot( a2 - a1, b2 - b1 ), 0 ); }\nbool orth(vec v1, vec v2){ return eq( dot(v1, v2), 0 ); }\nbool prll(point a1, point a2, point b1, point b2){ return eq( cross( a2 - a1, b2 - b1 ), 0 ); }\nbool prll(vec v1, vec v2){ return eq( cross(v1, v2), 0 ); }\n\n// CCW ƒƒoƒXƒg‚¾‚ªA¸“x‚É‚æ‚é\ninline int ccw(const point &a, point b, point x){\n b -= a;\n x -= a;\n if( eq(cross(b,x),0.0) && dot(b,x) < 0 ) return BACK;\n if( eq(cross(b,x),0.0) && abs(b) < abs(x) ) return FRONT;\n if( eq(cross(b,x),0.0) ) return OVER;\n if( cross(b,x) > 0 ) return LEFT;\n if( cross(b,x) < 0 ) return RIGHT;\n}\n\n// ü•ªŠg’£\nline expandLine(line l, elem mag){\n line ret = l;\n vec vf(l.first - l.second);\n vec vs(l.second - l.first);\n ret.first = l.second + mag * vf;\n ret.second = l.first + mag * vs;\n return ret;\n}\n\n// ü•ª‚ÌŒð·”»’è\ninline bool intersectedSS(const seg &a, const seg &b)\n{\n int cwaf=ccw(a.fr,a.sc,b.fr);\n int cwbf=ccw(b.fr,b.sc,a.fr);\n int cwas=ccw(a.fr,a.sc,b.sc);\n int cwbs=ccw(b.fr,b.sc,a.sc);\n if( cwaf==OVER || cwas==OVER || cwbf==OVER || cwbs==OVER ) return true;\n return ( cwaf | cwas ) == (LEFT|RIGHT) && ( cwbf | cwbs ) == (LEFT|RIGHT);\n}\n\n// ’¼ü‚ÌŒð·”»’è\nbool intersectedLL(line a, line b){ return !eq( cross(a.sc-a.fr,b.sc-b.fr), 0.0 ); }\n\n// Œð“_ŒvŽZ\npoint intersectionSS(seg a, seg b)\n{\n elem d1 = dist_l(b,a.fr);\n elem d2 = dist_l(b,a.sc);\n return a.fr + ( d1 / (d1 + d2 ) ) * (a.sc-a.fr);\n}\npoint intersectionLL(line a, line b)\n{\n vec va = a.sc - a.fr;\n vec vb = b.sc - b.fr;\n return a.fr + va * ( cross(vb, b.fr - a.fr) / cross(vb,va) );\n}\n\n// ü•ªŒð“_ˆêЇ”Å\nbool intersectionLL(line a, line b, point &ret){\n return intersectedLL( a, b ) ? ret = intersectionLL( a, b ), true : false;\n}\nbool intersectionLH(line a, hline b, point &ret){\n point tmp;\n return intersectionLL(a,b,tmp) ? ( ccw(b.fr,b.sc,tmp)&(OVER|FRONT) ? ret=tmp, true : false ) : false;\n}\nbool intersectionLS(line l, seg s, point &ret){\n point tmp;\n return intersectionLL(l,s,tmp) ? ( ccw(s.fr,s.sc,tmp)&OVER ? ret=tmp, true : false ) : false;\n}\nbool intersectionHH(hline a, hline b, point &ret){\n point tmp;\n return intersectionLL(a,b,tmp) ? ( ccw(a.fr,a.sc,tmp)&(OVER|FRONT)&&ccw(b.fr,b.sc,tmp)&(OVER|FRONT) ? ret = tmp, true : false ) : false;\n}\nbool intersectionHS(hline a, seg s, point &ret){\n point tmp;\n return intersectionLS(a,s,tmp) ? ( ccw(a.fr,a.sc,tmp)&(OVER|FRONT) ? ret = tmp, true : false ) : false;\n}\nbool intersectionSS(seg a, seg b, point &ret){\n return intersectedSS(a,b) ? ret = intersectionSS(a,b), true : false;\n}\n\npair<int, pair<double,double> > getLH(double x, const vector<point> &v)\n{\n point a,b;\n int cnt=0;\n line l(point(x,0),point(x,1));\n for(int i = 0; i < (int)v.size(); ++i){\n point is;\n if( intersectionLS( l, seg(v[i],v[(i+1)%v.size()]), is ) ){\n if(cnt==0){\n\ta = is;\n\t++cnt;\n }else if( far(is,a) ){\n\tb = is;\n\t++cnt;\n }\n }\n }\n if(cnt==1)b=a;\n if( a.imag() > b.imag() ) swap( a, b );\n return make_pair( cnt, make_pair(a.imag(), b.imag() ) );\n}\n\nstruct point3{\n double x,y,z;\n point3(double x, double y, double z):x(x),y(y),z(z){}\n point3 operator-(const point3 &t){\n return point3(x-t.x,y-t.y,z-t.z);\n }\n point3 operator+(const point3 &t){\n return point3(x+t.x,y+t.y,z+t.z);\n }\n};\n\nostream &operator<<(ostream &oss, const point3 &t)\n{\n oss << '('<<t.x <<','<< t.y<<','<<t.z<<')';\n return oss;\n}\n\ndouble dot3(point3 a, point3 b){\n return a.x*b.x + a.y*b.y + a.z*b.z;\n}\npoint3 cross3(point3 a, point3 b){\n return point3(a.y*b.z-b.y*a.z, a.z*b.x-b.z*a.x, a.x*b.y-b.x*a.y);\n}\n\ndouble tetrahedronVolume(point3 a, point3 b, point3 c, point3 d)\n{\n point3 A=b-a;\n point3 B=c-a;\n point3 C=d-a;\n return abs(dot3(A,cross3(B,C))/6);\n}\ndouble getVolume( const vector<point3> &V1, const vector<point3> &V2 )\n{\n double ret=0;\n vector<point3> v1 = V1;\n vector<point3> v2 = V2;\n if( v2.size() < v1.size() ){\n swap( v1, v2 );\n }\n if(v1.size()==1){\n if(v2.size()==2){\n ret = 0;\n }else if(v2.size()==4){\n ret+=tetrahedronVolume(v1[0],v2[0],v2[1],v2[2]);\n ret+=tetrahedronVolume(v1[0],v2[2],v2[3],v2[0]);\n }\n }else if(v1.size()==2){\n if(v2.size()==2){\n ret = tetrahedronVolume(v1[0],v1[1],v2[0],v2[1]);\n }else{\n if(eq(v1[0].y,v1[1].y)){\n\tret += tetrahedronVolume( v1[1],v1[0],v2[1],v2[2] );\n\tret += tetrahedronVolume( v1[0],v2[0],v2[1],v2[2] );\n\tret += tetrahedronVolume( v1[0],v2[2],v2[3],v2[0] );\n }else{\n\tret += tetrahedronVolume( v1[1],v1[0],v2[2],v2[3] );\n\tret += tetrahedronVolume( v1[0],v2[0],v2[1],v2[2] );\n\tret += tetrahedronVolume( v1[0],v2[2],v2[3],v2[0] );\n }\n }\n }else{\n ret += tetrahedronVolume( v1[1],v1[0],v2[1],v1[2] );\n ret += tetrahedronVolume( v2[0],v1[0],v2[3],v2[1] );\n ret += tetrahedronVolume( v1[3],v1[0],v1[2],v2[3] );\n\n ret += tetrahedronVolume( v2[2],v2[3],v2[1],v1[2] );\n ret += tetrahedronVolume( v1[0],v1[2],v2[1],v2[3] );\n }\n return ret;\n}\n\nint main()\n{\n int m,n;\n while(scanf(\"%d%d\", &m, &n) && m && n){\n double x,y;\n double res = 0;\n vector<point> P1;\n vector<point> P2;\n int minx1=1000,maxx1=-1000;\n int minx2=1000,maxx2=-1000;\n map<int,pair<double,double> > vx;\n for(int i = 0; i < m; ++i){\n scanf(\"%lf%lf\", &x, &y);\n P1.push_back(point(x,y));\n vx[(int)x]=make_pair(0,0);\n minx1 = min( (int)x, minx1 );\n maxx1 = max( (int)x, maxx1 );\n }\n for(int i = 0; i < n; ++i){\n scanf(\"%lf%lf\", &x, &y);\n P2.push_back(point(x,y));\n minx2 = min( (int)x, minx2 );\n maxx2 = max( (int)x, maxx2 );\n vx[(int)x]=make_pair(0,0);\n }\n retry:;\n for(map<int,pair<double,double> >::iterator itm = vx.begin(); itm != vx.end(); ++itm){\n pair<int, pair<double,double > > LH = getLH( itm->first, P1);\n if( !( minx1 <= itm->first && itm->first <= maxx1 &&\n\t minx2 <= itm->first && itm->first <= maxx2 ) ){\n\tvx.erase(itm);\n\tgoto retry;\n }\n if( LH.first == 0 ){\n\tvx.erase(itm);\n\tgoto retry;\n }else{\n\titm->second = LH.second;\n }\n }\n vector< vector<point3> > V;\n for(map<int,pair<double,double> >::iterator itm = vx.begin(); itm != vx.end(); ++itm){\n vector<point3> vpx;\n double x = itm->first;\n double y1 = itm->second.first;\n double y2 = itm->second.second;\n\n if( eq(y1,y2) ){\n\tpair<int,pair<double,double> > Z = getLH( x, P2 );\n\tdouble z1 = Z.second.first;\n\tdouble z2 = Z.second.second;\n\tif( eq(z1,z2) ){\n\t vpx.push_back( point3( x,y1,z1 ) );\n\t}else{\n\t vpx.push_back( point3( x,y1,z1 ) );\n\t vpx.push_back( point3( x,y1,z2 ) );\n\t}\n }else{\n\tpair<int,pair<double,double> > Z = getLH( x, P2 );\n\tdouble z1 = Z.second.first;\n\tdouble z2 = Z.second.second;\n\tif( eq(z1,z2) ){\n\t vpx.push_back( point3( x,y1,z1 ) );\n\t vpx.push_back( point3( x,y2,z1 ) );\n\t}else{\n\t vpx.push_back( point3( x,y1,z1 ) );\n\t vpx.push_back( point3( x,y1,z2 ) );\n\t vpx.push_back( point3( x,y2,z2 ) );\n\t vpx.push_back( point3( x,y2,z1 ) );\n\t}\n }\n \n /*\n for(int i = 0; i < (int)vpx.size(); ++i){\n\tcout << vpx[i] << ' ';\n }\n cout << endl;\n */\n\n V.push_back( vpx );\n }\n for(int i = 0; i < (int)V.size()-1; ++i){\n //cout << i << ' ' << i+1 << endl;\n res += getVolume(V[i], V[i+1]);\n }\n printf(\"%.13lf\\n\", res);\n //return 0;\n }\n return 0;\n}", "accuracy": 1, "time_ms": 20, "memory_kb": 0, "score_of_the_acc": -0.0018, "final_rank": 1 } ]
aoj_1316_cpp
Problem B: The Sorcerer's Donut Your master went to the town for a day. You could have a relaxed day without hearing his scolding. But he ordered you to make donuts dough by the evening. Loving donuts so much, he can't live without eating tens of donuts everyday. What a chore for such a beautiful day. But last week, you overheard a magic spell that your master was using. It was the time to try. You casted the spell on a broomstick sitting on a corner of the kitchen. With a flash of lights, the broom sprouted two arms and two legs, and became alive. You ordered him, then he brought flour from the storage, and started kneading dough. The spell worked, and how fast he kneaded it! A few minutes later, there was a tall pile of dough on the kitchen table. That was enough for the next week. \OK, stop now." You ordered. But he didn't stop. Help! You didn't know the spell to stop him! Soon the kitchen table was filled with hundreds of pieces of dough, and he still worked as fast as he could. If you could not stop him now, you would be choked in the kitchen filled with pieces of dough. Wait, didn't your master write his spells on his notebooks? You went to his den, and found the notebook that recorded the spell of cessation. But it was not the end of the story. The spell written in the notebook is not easily read by others. He used a plastic model of a donut as a notebook for recording the spell. He split the surface of the donut-shaped model into square mesh (Figure B.1), and filled with the letters (Figure B.2). He hid the spell so carefully that the pattern on the surface looked meaningless. But you knew that he wrote the pattern so that the spell "appears" more than once (see the next paragraph for the precise conditions). The spell was not necessarily written in the left-to-right direction, but any of the 8 directions, namely left-to-right, right-to-left, top-down, bottom-up, and the 4 diagonal directions. You should be able to find the spell as the longest string that appears more than once. Here, a string is considered to appear more than once if there are square sequences having the string on the donut that satisfy the following conditions. Each square sequence does not overlap itself. (Two square sequences can share some squares.) The square sequences start from different squares, and/or go to different directions. Figure B.1: The Sorcerer's Donut Before Filled with Letters, Showing the Mesh and 8 Possible Spell Directions Figure B.2: The Sorcerer's Donut After Filled with Letters Note that a palindrome (i.e., a string that is the same whether you read it backwards or forwards) that satisfies the first condition "appears" twice. The pattern on the donut is given as a matrix of letters as follows. ABCD EFGH IJKL Note that the surface of the donut has no ends; the top and bottom sides, and the left and right sides of the pattern are respectively connected. There can be square sequences longer than both the vertical and horizontal lengths of the patt ...(truncated)
[ { "submission_id": "aoj_1316_11047140", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\nusing a2 = array<int, 2>;\nusing a3 = array<ll, 3>;\n\nbool chmin(auto& a, const auto& b) { return a > b ? a = b, 1 : 0; }\nbool chmax(auto& a, const auto& b) { return a < b ? a = b, 1 : 0; }\n\nll mod = 998244353;\nconst ll INF = 1e18;\n\nifstream in;\nofstream out;\n\nint main(int argc, char** argv) {\n ios::sync_with_stdio(false);\n cin.tie(0);\n\n if(argc > 2) {\n in.open(argv[1]);\n cin.rdbuf(in.rdbuf());\n out.open(argv[2]);\n cout.rdbuf(out.rdbuf());\n }\n\n while(1) {\n ll h, w;\n cin >> h >> w;\n if(h == 0) break;\n\n vector<string> v(h);\n for(auto& x : v) cin >> x;\n\n map<string, ll> mp;\n\n vector<a2> dir = {\n {1, 0}, {-1, 0}, {0, 1}, {0, -1},\n {1, 1}, {1, -1}, {-1, 1}, {-1, -1},\n };\n for(int i = 0; i < h; i++) {\n for(int j = 0; j < w; j++) {\n for(auto [dx, dy] : dir) {\n ll p = (i + h + dx) % h, q = (j + w + dy) % w;\n string s(1, v[i][j]);\n while(p != i || q != j) {\n s.push_back(v[p][q]);\n mp[s]++;\n p = (p + h + dx) % h, q = (q + w + dy) % w;\n }\n }\n }\n }\n\n ll len = 0;\n string ans;\n for(auto [s, cnt] : mp) {\n if(cnt > 1) {\n if(chmax(len, s.size())) {\n ans = s;\n }\n }\n }\n\n if(len == 0) {\n cout << 0 << endl;\n } else {\n cout << ans << endl;\n }\n }\n\n return 0;\n}", "accuracy": 1, "time_ms": 250, "memory_kb": 30080, "score_of_the_acc": -0.9452, "final_rank": 5 }, { "submission_id": "aoj_1316_10946406", "code_snippet": "#include <iostream>\n#include <cstdio>\n#include <cstdlib>\n#include <string>\n#include <map>\n#include <vector>\n#include <algorithm>\n\nusing namespace std;\n\nconst int maxn = 40;\nconst int dx[] = {1, -1, 0, 0 , 1, 1, -1, -1},\n\t \t dy[] = {0, 0, 1, -1, 1, -1, 1, -1};\n\nint n, m, ml, cnt;\nchar donut[maxn][maxn];\nstring ans[2000];\nmap<string, int> mp;\n\nint main()\n{\n#ifdef LOCAL\n\tfreopen(\"B.in\", \"r\", stdin);\n#endif\n\twhile(scanf(\"%d%d\", &n, &m) != EOF) {\n\t\tif(n == 0 && m == 0)\tbreak;\n\t\tml = cnt = 0;\n\t\tmp.clear();\n\t\tfor(int i = 0; i < n; i++)\n\t\t\tscanf(\"%s\", donut[i]);\n\t\tfor(int i = 0; i < n; i++)\n\t\t\tfor(int j = 0; j < m; j++) \n\t\t\t\tfor(int k = 0; k < 8; k++) {\n\t\t\t\t\tstring tmp = \"\";\n\t\t\t\t\tint nx = i, ny = j;\n\t\t\t\t\tdo{\n\t\t\t\t\t\ttmp += donut[nx][ny];\n\t\t\t\t\t\tif(tmp.size() >= 2 && tmp.size() >= ml) {\n\t\t\t\t\t\t\tif(!mp[tmp])\tmp.insert(pair <string, int> (tmp, 1));\t\n\t\t\t\t\t\t\telse if(tmp.size() == ml && mp[tmp] == 1) \n\t\t\t\t\t\t\t\tans[cnt++] = tmp;\n\t\t\t\t\t\t\telse if(tmp.size() > ml) {\n\t\t\t\t\t\t\t\tml = tmp.size();\n\t\t\t\t\t\t\t\tcnt = 0;\n\t\t\t\t\t\t\t\tans[cnt++] = tmp;\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t}\n\t\t\t\t\t\tmp[tmp]++;\t\t\t\t\t\t\n\t\t\t\t\t\tnx = (nx+dx[k]+n)%n;\n\t\t\t\t\t\tny = (ny+dy[k]+m)%m;\n\t\t\t\t\t}while(nx != i || ny != j);\t\n//\t\t\t\t\tcout << tmp << endl;\t\n\t\t\t\t\t/*\n\t\t\t\t\tif(!mp[tmp])\tmp.insert(pair <string, int> (tmp, 1));\t\n\t\t\t\t\telse {\n\t\t\t\t\t\tif(tmp.size() == ml && mp[tmp] == 1) {\n\t\t\t\t\t\t\tans[cnt++] = tmp;\n\t\t\t\t\t\t}\n\t\t\t\t\t\telse if(tmp.size() > ml) {\n\t\t\t\t\t\t\tml = tmp.size();\n\t\t\t\t\t\t\tcnt = 0;\n\t\t\t\t\t\t\tans[cnt++] = tmp;\n\t\t\t\t\t\t}\n\t\t\t\t\t\tmp[tmp]++;\n\t\t\t\t\t}\n\t\t\t\t\t*/\n\t\t\t\t}\n\t\tsort(ans, ans+cnt);\n\t\tif(cnt)\tprintf(\"%s\\n\", ans[0].c_str());\n\t\telse\tprintf(\"0\\n\");\n\t}\n\treturn 0;\n}", "accuracy": 1, "time_ms": 410, "memory_kb": 30120, "score_of_the_acc": -1.0171, "final_rank": 16 }, { "submission_id": "aoj_1316_10885614", "code_snippet": "#include<bits/stdc++.h>\n// #include<atcoder/all>\n// #include<boost/multiprecision/cpp_int.hpp>\n\nusing namespace std;\n// using namespace atcoder;\n// using bint = boost::multiprecision::cpp_int;\nusing ll = long long;\nusing ull = unsigned long long;\nusing ld = long double;\nusing pii = pair<int,int>;\nusing pll = pair<ll,ll>;\nusing vi = vector<ll>;\nusing vvi = vector<vi>;\nusing vvvi = vector<vvi>;\nusing ve = vector<vector<int>>;\nusing vb = vector<bool>;\nusing vvb = vector<vb>;\n#define rep(i,s,n) for(ll i = (ll)s;i < (ll)n;i++)\n#define rrep(i,l,r) for(ll i = r-1;i >= l;i--)\n#define ALL(x) (x).begin(),(x).end()\n#define sz(c) ((ll)(c).size())\n#define LB(A,x) (int)(lower_bound(A.begin(),A.end(),x)-A.begin())\n#define UB(A,x) (int)(upper_bound(A.begin(),A.end(),x)-A.begin())\n// #define MOD 1000000007\n#define MOD 998244353\ntemplate<typename T>using min_priority_queue=priority_queue<T,vector<T>,greater<T>>;\ntemplate<typename T>ostream&operator<<(ostream&os,vector<T>&v){for(int i = 0;i < v.size();i++)os<<v[i]<<(i+1!=v.size()?\" \":\"\");return os;}\ntemplate<typename T>istream&operator>>(istream&is,vector<T>&v){for(T&in:v)is>>in;return is;}\ntemplate<typename T1,typename T2>ostream&operator<<(ostream&os,pair<T1,T2>&p){os<<p.first<<\" \"<<p.second;return os;}\ntemplate<typename T1,typename T2>istream&operator>>(istream&is,pair<T1,T2>&p){is>>p.first>>p.second;return is;}\ntemplate<typename T> inline bool chmax(T &a,T b){if(a < b){a = b;return true;}return false;}\ntemplate<typename T> inline bool chmin(T &a,T b){if(a > b){a = b;return true;}return false;}\nld dist(ld x1,ld y1,ld x2, ld y2){return sqrtl((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2));}\n\nint dx[] = {-1,-1,-1,0,0,1,1,1};\nint dy[] = {-1,0,1,-1,1,-1,0,1};\n\nint main(){\n \n ios_base::sync_with_stdio(0), cin.tie(0);\n while(1){\n int h,w;cin >> h >> w;\n if(!h)break;\n vector<string> s(h);cin >> s;\n string res = \"0\";\n unordered_set<string> st;\n rep(i,0,h)rep(j,0,w)rep(dir,0,8){\n string t = \"\";\n t += s[i][j];\n int ni = (i+dy[dir]+h)%h,nj = (j+dx[dir]+w)%w;\n while(ni != i || nj != j){\n t += s[ni][nj];\n if(st.count(t)){\n if(sz(t) > sz(res))res = t;\n else if(sz(t) == sz(res)){\n if(t < res)res = t;\n }\n }else st.insert(t);\n ni = (ni+dy[dir]+h)%h,nj = (nj+dx[dir]+w)%w;\n }\n }\n cout << res << \"\\n\";\n }\n \n\n return 0;\n}", "accuracy": 1, "time_ms": 290, "memory_kb": 29480, "score_of_the_acc": -0.9423, "final_rank": 4 }, { "submission_id": "aoj_1316_10538817", "code_snippet": "#include <iostream>\n#include <iomanip>\n#include <cassert>\n#include <vector>\n#include <algorithm>\n#include <utility>\n#include <numeric>\n#include <tuple>\n#include <ranges>\nnamespace ranges = std::ranges;\nnamespace views = std::views;\n// #include \"Src/Number/IntegerDivision.hpp\"\n// #include \"Src/Utility/BinarySearch.hpp\"\n// #include \"Src/Sequence/CompressedSequence.hpp\"\n// #include \"Src/Sequence/RunLengthEncoding.hpp\"\n// #include \"Src/Algebra/Group/AdditiveGroup.hpp\"\n// #include \"Src/DataStructure/FenwickTree/FenwickTree.hpp\"\n// #include \"Src/DataStructure/SegmentTree/SegmentTree.hpp\"\n// using namespace zawa;\n// #include \"atcoder/modint\"\n// using mint = atcoder::modint998244353;\n#include <set>\nint H, W;\nstd::string S[10];\nbool solve() {\n std::cin >> H >> W;\n if (H == 0 and W == 0) return false;\n for (int i = 0 ; i < H ; i++) std::cin >> S[i];\n std::vector<std::string> app;\n for (int sy = 0 ; sy < H ; sy++) for (int sx = 0 ; sx < W ; sx++) {\n for (int dy = -1 ; dy <= 1 ; dy++) for (int dx = -1 ; dx <= 1 ; dx++) if (dy != 0 or dx != 0) {\n std::set<std::pair<int, int>> vis;\n std::string T;\n for (int d = 0 ; ; d++) {\n const int py = ((sy + d * dy) % H + H) % H;\n const int px = ((sx + d * dx) % W + W) % W;\n if (vis.contains(std::pair{py, px})) break;\n vis.insert({py, px});\n T += S[py][px];\n if (T.size() >= 2u) app.push_back(T);\n }\n }\n }\n ranges::sort(app);\n std::vector<std::string> multiple;\n for (int i = 0, j = 0 ; i < std::ssize(app) ; i = j) {\n while (j < std::ssize(app) and app[i] == app[j]) j++;\n if (j - i >= 2) multiple.push_back(app[i]);\n }\n std::vector<std::string> ans;\n for (int i = 0 ; i < std::ssize(multiple) ; i++) {\n auto it = ranges::lower_bound(multiple.begin() + i + 1, multiple.end(), multiple[i]);\n if (it == multiple.end() or it->size() < multiple[i].size() or it->substr(0, multiple[i].size()) != multiple[i])\n ans.push_back(multiple[i]);\n }\n if (ans.empty()) std::cout << 0 << '\\n';\n else {\n int idx = 0;\n for (int i = 1 ; i < std::ssize(ans) ; i++) if (ans[idx].size() < ans[i].size()) idx = i;\n std::cout << ans[idx] << '\\n';\n }\n return true;\n}\nint main() {\n std::cin.tie(nullptr);\n std::cout.tie(nullptr);\n std::ios::sync_with_stdio(false);\n while (solve()) std::cout.flush();\n}", "accuracy": 1, "time_ms": 290, "memory_kb": 31208, "score_of_the_acc": -1.0015, "final_rank": 14 }, { "submission_id": "aoj_1316_9599420", "code_snippet": "#include <bits/stdc++.h>\n\nusing namespace std;\n\n#define rep(i, a, b) for (int i = (int)(a); i < (int)(b); i++)\n#define rrep(i, a, b) for (int i = (int)(b)-1; i >= (int)(a); i--)\n#define ALL(v) (v).begin(), (v).end()\n#define UNIQUE(v) sort(ALL(v)), (v).erase(unique(ALL(v)), (v).end())\n#define SZ(v) (int)v.size()\n#define MIN(v) *min_element(ALL(v))\n#define MAX(v) *max_element(ALL(v))\n#define LB(v, x) int(lower_bound(ALL(v), (x)) - (v).begin())\n#define UB(v, x) int(upper_bound(ALL(v), (x)) - (v).begin())\n\nusing uint = unsigned int;\nusing ll = long long int;\nusing ull = unsigned long long;\nusing i128 = __int128_t;\nusing u128 = __uint128_t;\nconst int inf = 0x3fffffff;\nconst ll INF = 0x1fffffffffffffff;\n\ntemplate <typename T> inline bool chmax(T &a, T b) {\n if (a < b) {\n a = b;\n return 1;\n }\n return 0;\n}\ntemplate <typename T> inline bool chmin(T &a, T b) {\n if (a > b) {\n a = b;\n return 1;\n }\n return 0;\n}\ntemplate <typename T, typename U> T ceil(T x, U y) {\n assert(y != 0);\n if (y < 0)\n x = -x, y = -y;\n return (x > 0 ? (x + y - 1) / y : x / y);\n}\ntemplate <typename T, typename U> T floor(T x, U y) {\n assert(y != 0);\n if (y < 0)\n x = -x, y = -y;\n return (x > 0 ? x / y : (x - y + 1) / y);\n}\ntemplate <typename T> int popcnt(T x) {\n return __builtin_popcountll(x);\n}\ntemplate <typename T> int topbit(T x) {\n return (x == 0 ? -1 : 63 - __builtin_clzll(x));\n}\ntemplate <typename T> int lowbit(T x) {\n return (x == 0 ? -1 : __builtin_ctzll(x));\n}\n\ntemplate <class T, class U>\nostream &operator<<(ostream &os, const pair<T, U> &p) {\n os << \"P(\" << p.first << \", \" << p.second << \")\";\n return os;\n}\ntemplate <typename T> ostream &operator<<(ostream &os, const vector<T> &vec) {\n os << \"{\";\n for (int i = 0; i < vec.size(); i++) {\n os << vec[i] << (i + 1 == vec.size() ? \"\" : \", \");\n }\n os << \"}\";\n return os;\n}\ntemplate <typename T, typename U>\nostream &operator<<(ostream &os, const map<T, U> &map_var) {\n os << \"{\";\n for (auto itr = map_var.begin(); itr != map_var.end(); itr++) {\n os << \"(\" << itr->first << \", \" << itr->second << \")\";\n itr++;\n if (itr != map_var.end())\n os << \", \";\n itr--;\n }\n os << \"}\";\n return os;\n}\ntemplate <typename T> ostream &operator<<(ostream &os, const set<T> &set_var) {\n os << \"{\";\n for (auto itr = set_var.begin(); itr != set_var.end(); itr++) {\n os << *itr;\n ++itr;\n if (itr != set_var.end())\n os << \", \";\n itr--;\n }\n os << \"}\";\n return os;\n}\n#ifdef LOCAL\n#define show(...) _show(0, #__VA_ARGS__, __VA_ARGS__)\n#else\n#define show(...) true\n#endif\ntemplate <typename T> void _show(int i, T name) {\n cerr << '\\n';\n}\ntemplate <typename T1, typename T2, typename... T3>\nvoid _show(int i, const T1 &a, const T2 &b, const T3 &...c) {\n for (; a[i] != ',' && a[i] != '\\0'; i++)\n cerr << a[i];\n cerr << \":\" << b << \" \";\n _show(i + 1, a, c...);\n}\n\n#include <unistd.h>\n\nnamespace fastio {\nstatic constexpr uint32_t SZ = 1 << 17;\nchar ibuf[SZ];\nchar obuf[SZ];\nchar out[100];\n// pointer of ibuf, obuf\n\n\nuint32_t pil = 0, pir = 0, por = 0;\n\nstruct Pre {\n char num[10000][4];\n constexpr Pre() : num() {\n for (int i = 0; i < 10000; i++) {\n int n = i;\n for (int j = 3; j >= 0; j--) {\n num[i][j] = n % 10 | '0';\n n /= 10;\n }\n }\n }\n} constexpr pre;\n\ninline void load() {\n memmove(ibuf, ibuf + pil, pir - pil);\n pir = pir - pil + fread(ibuf + pir - pil, 1, SZ - pir + pil, stdin);\n pil = 0;\n if (pir < SZ)\n ibuf[pir++] = '\\n';\n}\n\ninline void flush() {\n fwrite(obuf, 1, por, stdout);\n por = 0;\n}\n\nvoid rd(char &c) {\n do {\n if (pil + 1 > pir)\n load();\n c = ibuf[pil++];\n } while (isspace(c));\n}\n\nvoid rd(string &x) {\n x.clear();\n char c;\n do {\n if (pil + 1 > pir)\n load();\n c = ibuf[pil++];\n } while (isspace(c));\n do {\n x += c;\n if (pil == pir)\n load();\n c = ibuf[pil++];\n } while (!isspace(c));\n}\n\ntemplate <typename T> void rd_real(T &x) {\n string s;\n rd(s);\n x = stod(s);\n}\n\ntemplate <typename T> void rd_integer(T &x) {\n if (pil + 100 > pir)\n load();\n char c;\n do\n c = ibuf[pil++];\n while (c < '-');\n bool minus = 0;\n if constexpr (is_signed<T>::value || is_same_v<T, i128>) {\n if (c == '-') {\n minus = 1, c = ibuf[pil++];\n }\n }\n x = 0;\n while ('0' <= c) {\n x = x * 10 + (c & 15), c = ibuf[pil++];\n }\n if constexpr (is_signed<T>::value || is_same_v<T, i128>) {\n if (minus)\n x = -x;\n }\n}\n\nvoid rd(int &x) {\n rd_integer(x);\n}\nvoid rd(ll &x) {\n rd_integer(x);\n}\nvoid rd(i128 &x) {\n rd_integer(x);\n}\nvoid rd(uint &x) {\n rd_integer(x);\n}\nvoid rd(ull &x) {\n rd_integer(x);\n}\nvoid rd(u128 &x) {\n rd_integer(x);\n}\nvoid rd(double &x) {\n rd_real(x);\n}\nvoid rd(long double &x) {\n rd_real(x);\n}\n\ntemplate <class T, class U> void rd(pair<T, U> &p) {\n return rd(p.first), rd(p.second);\n}\ntemplate <size_t N = 0, typename T> void rd_tuple(T &t) {\n if constexpr (N < std::tuple_size<T>::value) {\n auto &x = std::get<N>(t);\n rd(x);\n rd_tuple<N + 1>(t);\n }\n}\ntemplate <class... T> void rd(tuple<T...> &tpl) {\n rd_tuple(tpl);\n}\n\ntemplate <size_t N = 0, typename T> void rd(array<T, N> &x) {\n for (auto &d : x)\n rd(d);\n}\ntemplate <class T> void rd(vector<T> &x) {\n for (auto &d : x)\n rd(d);\n}\n\nvoid read() {}\ntemplate <class H, class... T> void read(H &h, T &...t) {\n rd(h), read(t...);\n}\n\nvoid wt(const char c) {\n if (por == SZ)\n flush();\n obuf[por++] = c;\n}\nvoid wt(const string s) {\n for (char c : s)\n wt(c);\n}\nvoid wt(const char *s) {\n size_t len = strlen(s);\n for (size_t i = 0; i < len; i++)\n wt(s[i]);\n}\n\ntemplate <typename T> void wt_integer(T x) {\n if (por > SZ - 100)\n flush();\n if (x < 0) {\n obuf[por++] = '-', x = -x;\n }\n int outi;\n for (outi = 96; x >= 10000; outi -= 4) {\n memcpy(out + outi, pre.num[x % 10000], 4);\n x /= 10000;\n }\n if (x >= 1000) {\n memcpy(obuf + por, pre.num[x], 4);\n por += 4;\n } else if (x >= 100) {\n memcpy(obuf + por, pre.num[x] + 1, 3);\n por += 3;\n } else if (x >= 10) {\n int q = (x * 103) >> 10;\n obuf[por] = q | '0';\n obuf[por + 1] = (x - q * 10) | '0';\n por += 2;\n } else\n obuf[por++] = x | '0';\n memcpy(obuf + por, out + outi + 4, 96 - outi);\n por += 96 - outi;\n}\n\ntemplate <typename T> void wt_real(T x) {\n ostringstream oss;\n oss << fixed << setprecision(15) << double(x);\n string s = oss.str();\n wt(s);\n}\n\nvoid wt(int x) {\n wt_integer(x);\n}\nvoid wt(ll x) {\n wt_integer(x);\n}\nvoid wt(i128 x) {\n wt_integer(x);\n}\nvoid wt(uint x) {\n wt_integer(x);\n}\nvoid wt(ull x) {\n wt_integer(x);\n}\nvoid wt(u128 x) {\n wt_integer(x);\n}\nvoid wt(double x) {\n wt_real(x);\n}\nvoid wt(long double x) {\n wt_real(x);\n}\n\ntemplate <class T, class U> void wt(const pair<T, U> val) {\n wt(val.first);\n wt(' ');\n wt(val.second);\n}\ntemplate <size_t N = 0, typename T> void wt_tuple(const T t) {\n if constexpr (N < std::tuple_size<T>::value) {\n if constexpr (N > 0) {\n wt(' ');\n }\n const auto x = std::get<N>(t);\n wt(x);\n wt_tuple<N + 1>(t);\n }\n}\ntemplate <class... T> void wt(tuple<T...> tpl) {\n wt_tuple(tpl);\n}\ntemplate <class T, size_t S> void wt(const array<T, S> val) {\n auto n = val.size();\n for (size_t i = 0; i < n; i++) {\n if (i)\n wt(' ');\n wt(val[i]);\n }\n}\ntemplate <class T> void wt(const vector<T> val) {\n auto n = val.size();\n for (size_t i = 0; i < n; i++) {\n if (i)\n wt(' ');\n wt(val[i]);\n }\n}\n\nvoid print() {\n wt('\\n');\n}\ntemplate <class Head, class... Tail> void print(Head &&head, Tail &&...tail) {\n wt(head);\n if (sizeof...(Tail))\n wt(' ');\n print(forward<Tail>(tail)...);\n}\nvoid __attribute__((destructor)) _d() {\n flush();\n}\n} // namespace fastio\n\n\nusing fastio::flush;\nusing fastio::print;\nusing fastio::read;\n\ninline void first(bool i = true) {\n print(i ? \"first\" : \"second\");\n}\ninline void Alice(bool i = true) {\n print(i ? \"Alice\" : \"Bob\");\n}\ninline void Takahashi(bool i = true) {\n print(i ? \"Takahashi\" : \"Aoki\");\n}\ninline void yes(bool i = true) {\n print(i ? \"yes\" : \"no\");\n}\ninline void Yes(bool i = true) {\n print(i ? \"Yes\" : \"No\");\n}\ninline void No() {\n print(\"No\");\n}\ninline void YES(bool i = true) {\n print(i ? \"YES\" : \"NO\");\n}\ninline void NO() {\n print(\"NO\");\n}\ninline void Yay(bool i = true) {\n print(i ? \"Yay!\" : \":(\");\n}\ninline void Possible(bool i = true) {\n print(i ? \"Possible\" : \"Impossible\");\n}\ninline void POSSIBLE(bool i = true) {\n print(i ? \"POSSIBLE\" : \"IMPOSSIBLE\");\n}\n\n/**\n * @brief Fast IO\n */\n\nint main() {\nwhile(1) {\n int N, M;\n cin >> N >> M;\n if (N == 0 && M == 0) return 0;\n vector<vector<char>> C(N,vector<char>(M));\n rep(i,0,N) rep(j,0,M) cin >> C[i][j];\\\n map<string,int> mp;\n rep(i,0,N) {\n rep(j,0,M) {\n rep(dx,-1,2) {\n rep(dy,-1,2) {\n if (dx==0 && dy==0) continue;\n vector<vector<bool>> used(N,vector<bool>(M,false));\n int PX = i, PY = j;\n string Cur = \"\";\n while(1) {\n if (used[PX][PY]) break;\n Cur += C[PX][PY];\n used[PX][PY] = true;\n PX = (PX + N + dx) % N;\n PY = (PY + M + dy) % M;\n }\n for (int k = 2; k <= Cur.size(); k++) {\n mp[Cur.substr(0,k)]++;\n }\n }\n }\n }\n }\n vector<string> S;\n for (auto it = mp.begin(); it != mp.end(); it++) {\n if (it->second >= 2) S.push_back(it->first);\n }\n string ANS = \"\";\n rep(i,0,S.size()) {\n if (ANS.size() < S[i].size()) ANS = S[i];\n else if (ANS.size() == S[i].size() && ANS > S[i]) ANS = S[i];\n }\n if (ANS == \"\") cout << 0 << endl;\n else cout << ANS << endl;\n}\n}", "accuracy": 1, "time_ms": 290, "memory_kb": 32496, "score_of_the_acc": -1.0456, "final_rank": 19 }, { "submission_id": "aoj_1316_7909539", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\nbool solve() {\n int H, W; cin >> H >> W;\n if (H == 0 and W == 0) return false;\n vector<vector<char>> G(H, vector<char>(W));\n for (auto& g : G) for (auto& x : g) cin >> x;\n\n auto get = [&](int sy, int sx, int dy, int dx) -> string {\n vector<vector<bool>> used(H, vector<bool>(W, false));\n int ny = sy, nx = sx;\n string res{};\n while (!used.at(ny).at(nx)) {\n res += G[ny][nx];\n used[ny][nx] = true;\n ny += dy; ny = (ny + H) % H;\n nx += dx; nx = (nx + W) % W;\n }\n return res;\n };\n \n /*\n cout << get(4, 5, -1, 1) << endl;\n exit(0);\n */\n\n map<string, int> mp{};\n\n for (int sy = 0 ; sy < H ; sy++) {\n for (int sx = 0 ; sx < W ; sx++) {\n for (int dy = -1 ; dy <= 1 ; dy++) {\n for (int dx = -1 ; dx <= 1 ; dx++) {\n if (dy == 0 and dx == 0) continue;\n string s = get(sy, sx, dy, dx);\n for (size_t len = 2 ; len <= s.size() ; len++) {\n mp[s.substr(0, len)]++;\n }\n }\n }\n }\n }\n\n string ans{};\n for (const auto& [s, cnt] : mp) {\n if (cnt < 2) continue;\n if (ans.size() > s.size()) continue;\n if (ans.size() == s.size()) {\n ans = min(ans, s);\n }\n else {\n ans = s;\n }\n }\n\n if (ans.empty()) {\n cout << 0 << endl;\n }\n else {\n cout << ans << endl;\n }\n\n return true;\n}\n \n\nint main() {\n while (solve()) ;\n}", "accuracy": 1, "time_ms": 300, "memory_kb": 29820, "score_of_the_acc": -0.9584, "final_rank": 8 }, { "submission_id": "aoj_1316_7136181", "code_snippet": "// author: hanyu\n#include <bits/stdc++.h>\nusing namespace std;\n\nvector<int> di = {0, -1, -1, -1, 0, 1, 1, 1}, dj = {1, 1, 0, -1, -1, -1, 0, 1};\n\nint main() {\n cin.tie(0);\n ios::sync_with_stdio(false);\n\n while (true) {\n int h, w;\n cin >> h >> w;\n if (h + w == 0) break;\n\n vector<string> x(h);\n for (int i = 0; i < h; i++) cin >> x[i];\n\n map<string, int> mp;\n for (int i = 0; i < h; i++) {\n for (int j = 0; j < w; j++) {\n for (int way = 0; way < 8; way++) {\n string s = \"\";\n int nowi = i, nowj = j;\n vector<vector<int>> visit(h, vector<int>(w, 0));\n while (true) {\n if (visit[nowi][nowj] == 1) break;\n visit[nowi][nowj] = 1;\n s += x[nowi][nowj];\n if (s.size() >= 2) mp[s]++;\n nowi += di[way];\n nowj += dj[way];\n if (nowi < 0) nowi += h;\n if (nowi >= h) nowi -= h;\n if (nowj < 0) nowj += w;\n if (nowj >= w) nowj -= w;\n }\n }\n }\n }\n\n string answer = \"\";\n for (auto i : mp) {\n if (i.second <= 1) continue;\n if (i.first.size() > answer.size()) answer = i.first;\n }\n\n if (answer == \"\") cout << 0 << endl;\n else cout << answer << endl;\n }\n}", "accuracy": 1, "time_ms": 300, "memory_kb": 29768, "score_of_the_acc": -0.9566, "final_rank": 6 }, { "submission_id": "aoj_1316_6779082", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\ntypedef long long ll;\ntypedef pair<ll,ll> pll;\ntypedef vector<ll> vll;\ntypedef vector<pll> vpll;\n#define mp make_pair\n#define pb push_back\n#define rep(i,n) for(int i=0;i<n;i++)\n#define per(i,n) for(int i=n-1;i>=0;i--)\n#define rrep(i,l,r) for(int i=l;i<=r;i++)\n#define chmin(a,b) a=min(a,b)\n#define chmax(a,b) a=max(a,b)\n#define all(x) x.begin(),x.end()\n#define unq(x) sort(all(x));x.erase(unique(all(x)),x.end())\n//#define mod 1000000007\n#define mod 998244353\n//ll mod;\n#define ad(a,b) a=(a+b)%mod;\n#define mul(a,b) a=a*b%mod;\nvoid readv(vector<ll> &a,ll n){\n\trep(i,n){\n\t\tll x;\n\t\tcin>>x;\n\t\ta.push_back(x);\n\t}\n}\nvoid outv(vector<ll> &a,ll n){\n\trep(i,n){\n\t\tif(i>0)cout<<\" \";\n\t\tcout<<a[i];\n\t}\n\tcout<<\"\\n\";\n}\nll po(ll x,ll y){\n\tll res=1;\n\tfor(;y;y>>=1){\n\t\tif(y&1)res=res*x%mod;\n\t\tx=x*x%mod;\n\t}\n\treturn res;\n}\nll gcd(ll a,ll b){\n\treturn (b?gcd(b,a%b):a);\n}\n#define FACMAX 200010\nll fac[FACMAX],inv[FACMAX],ivf[FACMAX];\nvoid initfac(){\n\tfac[0]=ivf[0]=inv[1]=1;\n\tfor(ll i=1;i<FACMAX;i++)fac[i]=fac[i-1]*i%mod;\n\tfor(ll i=1;i<FACMAX;i++){\n\t\tif(i>1)inv[i]=(mod-mod/i*inv[mod%i]%mod)%mod;\n\t\tivf[i]=po(fac[i],mod-2);\n\t}\n}\nll P(ll n,ll k){\n\tif(n<0||n<k)return 0;\n\treturn fac[n]*ivf[n-k]%mod;\n}\nll C(ll n,ll k){\n\tif(k<0||n<k)return 0;\n\treturn fac[n]*ivf[n-k]%mod*ivf[k]%mod;\n}\nll H(ll n,ll k){\n\treturn C(n+k-1,k);\n}\n\nll dx[]={1,1,1,0,0,-1,-1,-1};\nll dy[]={1,0,-1,1,-1,1,0,-1};\nll h,w;\nchar c[10][20];\nvoid solve(){\n\tvector<string> v;\n\trep(i,h)rep(j,w){\n\t\trep(dir,8){\n\t\t\tbool vis[10][20];\n\t\t\trep(i,h)rep(j,w)vis[i][j]=0;\n\t\t\tstring s;\n\t\t\tll x=i,y=j;\n\t\t\twhile(!vis[x][y]){\n\t\t\t\tvis[x][y]=1;\n\t\t\t\ts.pb(c[x][y]);\n\t\t\t\tv.pb(s);\n\t\t\t\tx=(x+dx[dir]+h)%h;\n\t\t\t\ty=(y+dy[dir]+w)%w;\n\t\t\t}\n\t\t}\n\t}\n\tsort(all(v));\n\tstring res=\"0\";\n\trep(i,v.size()-1){\n\t\tif(v[i]==v[i+1]){\n\t\t\tbool ok=0;\n\t\t\tif(res.size()<v[i].size())ok=1;\n\t\t\tif(res.size()==v[i].size()&&res>v[i])ok=1;\n\t\t\tif(ok)res=v[i];\n\t\t}\n\t}\n\tcout<<res<<endl;\n}\nint main(){\n\tcin.tie(0);\n\tios::sync_with_stdio(0);\n\twhile(1){\n\t\tcin>>h>>w;\n\t\tif(h==0&&w==0)break;\n\t\trep(i,h)rep(j,w)cin>>c[i][j];\n\t\tsolve();\n\t}\n}", "accuracy": 1, "time_ms": 230, "memory_kb": 32580, "score_of_the_acc": -1.022, "final_rank": 17 }, { "submission_id": "aoj_1316_6385527", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\ntypedef long long int ll;\ntypedef long double ld;\ntypedef pair<ll, ll> pint;\n#define rep(i, n) for(ll i = 0; i < (ll)n; i++)\n#define ALL(v) (v).begin(), (v).end()\nconst ll MOD = 1000000007;\nconst ll INF = 1e16;\nll dx[8] = {-1, 0, 1, -1, 1, -1, 0, 1};\nll dy[8] = {-1, -1, -1, 0, 0, 1, 1, 1};\n\nint main() {\n while(1) {\n ll H, W; cin >> H >> W;\n if(H == 0 && W == 0) break;\n vector<string> V(H);\n rep(i, H) cin >> V[i];\n map<string, ll> mp;\n rep(i, H) {\n rep(j, W) {\n rep(k, 8) {\n ll nx = j;\n ll ny = i;\n string S = \"\";\n S += V[ny][nx];\n while(1) {\n nx = (nx+dx[k]+W)%W;\n ny = (ny+dy[k]+H)%H;\n if(i == ny && j == nx) break;\n S += V[ny][nx];\n mp[S]++;\n }\n }\n }\n }\n vector<string> ans;\n ll sz = 0;\n for(auto c: mp) if(c.second > 1) sz = max(sz, (ll)c.first.size());\n for(auto c: mp) if(c.second > 1 && sz == (ll)c.first.size()) ans.push_back(c.first);\n if(ans.size() == 0) cout << 0 << \"\\n\";\n else {\n \n sort(ALL(ans));\n cout << ans[0] << \"\\n\";\n }\n }\n}", "accuracy": 1, "time_ms": 380, "memory_kb": 29996, "score_of_the_acc": -0.9996, "final_rank": 12 }, { "submission_id": "aoj_1316_6331349", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\n\nint main(){\n\tint h, w;\n\twhile(cin >> h >> w){\n\t\tif(!h)return 0;\n\t\tvector<string> A(h);\n\t\tfor(auto &&s:A)cin >> s;\n\t\tmap<string,int> mp;\n\t\tauto safe=[&](int &y,int &x){\n\t\t\tif(y>=h)y-=h;\n\t\t\tif(x>=w)x-=w;\n\t\t\tif(y<0)y+=h;\n\t\t\tif(x<0)x+=w;\n\t\t};\n\t\tauto f=[&](int y,int x,int dy,int dx){\n\t\t\tstring ans;\n\t\t\tans+=A[y][x];\n\t\t\tint cy=y+dy,cx=x+dx;\n\t\t\tsafe(cy,cx);\n\t\t\twhile(cy!=y||cx!=x){\n\t\t\t\tans+=A[cy][cx];\n\t\t\t\tcy+=dy,cx+=dx;\n\t\t\t\tsafe(cy,cx);\n mp[ans]++;\n\t\t\t}\n\t\t};\n\t\tfor(int y=0;y<h;y++){\n\t\t\tfor(int x=0;x<w;x++){\n\t\t\t\tfor(int dy=-1;dy<=1;dy++){\n\t\t\t\t\tfor(int dx=-1;dx<=1;dx++){\n\t\t\t\t\t\tif(dy==0&&dx==0)continue;\n\t\t\t\t\t\tf(y,x,dy,dx);\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\tint maxv=0;\n\t\tstring ans;\n\t\tfor(auto p:mp){\n if(p.second==1)continue;\n\t\t\tif(p.first.size()>maxv){\n\t\t\t\tmaxv=p.first.size();\n\t\t\t\tans=p.first;\n\t\t\t}else if(p.first.size()==maxv){\n\t\t\t\tans=min(ans,p.first);\n\t\t\t}\n\t\t}\n if(ans.empty())ans=\"0\";\n\t\tcout << ans << '\\n';\n\t}\n}", "accuracy": 1, "time_ms": 300, "memory_kb": 29972, "score_of_the_acc": -0.9636, "final_rank": 9 }, { "submission_id": "aoj_1316_6219734", "code_snippet": "#include <stdio.h>\n#include <string>\n#include <unordered_map>\nusing namespace std;\n\nchar pat[16][32], vis[16][32];\nint dxy[8][2] = { {-1, 0}, {-1, 1}, {0, 1}, {1, 1}, {1, 0}, {1, -1}, {0, -1}, {-1, -1} };\nunordered_map<string, int> u;\n\nvoid clearmat(char* s, int h, int w, int ww) {\n for (int i = 0; i < h; i++) {\n for (int j = 0; j < w; j++) s[i * ww + j] = 0;\n }\n}\n\nint main(void) {\n int h, w, x, y;\n string s;\n while (1) {\n scanf(\"%d %d\", &h, &w);\n if (h == 0) break;\n for (int i = 0; i < h; i++) {\n scanf(\"%s\", pat[i]);\n }\n for (int i = 0; i < h; i++) {\n for (int j = 0; j < w; j++) {\n for (int d = 0; d < 8; d++) {\n x = i, y = j;\n s = \"\";\n while (!vis[x][y]) {\n vis[x][y] = 1;\n s += pat[x][y];\n if (s.length() > 1) {\n if (u.find(s) == u.end()) u.insert(pair<string, int>(s, 1));\n else u[s]++;\n }\n x += dxy[d][0];\n y += dxy[d][1];\n if (x < 0) x += h;\n if (x >= h) x -= h;\n if (y < 0) y += w;\n if (y >= w) y -= w;\n }\n clearmat((char*)vis, h, w, 32);\n }\n }\n }\n\n s = \"\";\n for (auto i : u) {\n if (i.second > 1) {\n if (i.first.length() > s.length()) s = i.first;\n else if (i.first.length() == s.length() && s.compare(i.first) > 0) s = i.first;\n }\n }\n u.clear();\n if (s.length() < 2) printf(\"0\\n\");\n else printf(\"%s\\n\", s.c_str());\n }\n return 0;\n}", "accuracy": 1, "time_ms": 300, "memory_kb": 28644, "score_of_the_acc": -0.9181, "final_rank": 2 }, { "submission_id": "aoj_1316_6196047", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\nusing ll = long long;\n#define rep(i, n) for (int i = 0; i < (n); i++)\n#define ALL(x) x.begin(), x.end()\n\nint dx[8] = {1, 1, 0, -1, -1, -1, 0, 1};\nint dy[8] = {0, 1, 1, 1, 0, -1, -1, -1};\n\nint main() {\n int H, W;\n while (cin >> H >> W) {\n if (H == 0 && W == 0) break;\n vector<string> G(H);\n rep(i, H) cin >> G[i];\n string ans = \"\";\n rep(x1, H) rep(y1, W) rep(x2, H) rep(y2, W) {\n if (G[x1][y1] != G[x2][y2]) continue;\n rep(k1, 8) rep(k2, 8) {\n if (x1 == x2 && y1 == y2 && k1 == k2) continue;\n int nx1 = (x1 + dx[k1] + H) % H, ny1 = (y1 + dy[k1] + W) % W;\n int nx2 = (x2 + dx[k2] + H) % H, ny2 = (y2 + dy[k2] + W) % W;\n if (G[nx1][ny1] != G[nx2][ny2]) continue;\n string s = \"\";\n s += G[x1][y1];\n vector<vector<int>> seen1(H, vector<int>(W)), seen2(H, vector<int>(W));\n seen1[x1][y1] = 1;\n seen2[x2][y2] = 1;\n while (G[nx1][ny1] == G[nx2][ny2] && seen1[nx1][ny1] == 0 && seen2[nx2][ny2] == 0) {\n s += G[nx1][ny1];\n seen1[nx1][ny1] = 1;\n seen2[nx2][ny2] = 1;\n nx1 = (nx1 + dx[k1] + H) % H, ny1 = (ny1 + dy[k1] + W) % W;\n nx2 = (nx2 + dx[k2] + H) % H, ny2 = (ny2 + dy[k2] + W) % W;\n }\n if (ans.size() == 0)\n ans = s;\n else if (ans.size() < s.size() || (ans.size() == s.size() && ans > s))\n ans = s;\n }\n }\n if (ans.size() == 0)\n cout << 0 << '\\n';\n else\n cout << ans << '\\n';\n }\n return 0;\n}", "accuracy": 1, "time_ms": 2450, "memory_kb": 3372, "score_of_the_acc": -1, "final_rank": 13 }, { "submission_id": "aoj_1316_6029149", "code_snippet": "#include <bits/stdc++.h>\n#define rep(i, a, n) for(int i = a; i < (n); i++)\nusing namespace std;\nusing ll = long long;\nusing P = pair<int, int>;\nconst int INF = 1001001001;\nconst ll LINF = 1001002003004005006ll;\n//const int mod = 1000000007;\n//const int mod = 998244353;\nconst int di[] = {1,1,0,-1,-1,-1,0,1};\nconst int dj[] = {0,1,1,1,0,-1,-1,-1};\n\nint main()\n{\n while(true){\n int h, w;\n cin >> h >> w;\n if(h == 0 && w == 0) return 0;\n vector<string> s(h);\n rep(i, 0, h) cin >> s[i];\n map<string, int> mp;\n string ans;\n auto dfs = [&](auto& f, int si, int sj, int i, int j, int dir, string t) -> void{\n int ni = (i + di[dir] + h)%h;\n int nj = (j + dj[dir] + w)%w;\n mp[t]++;\n if(si == ni && sj == nj) return;\n else f(f, si, sj, ni, nj, dir, t + s[ni][nj]);\n };\n rep(i, 0, h){\n rep(j, 0, w){\n rep(dir, 0, 8){\n string t;\n t += s[i][j];\n dfs(dfs, i, j, i, j, dir, t);\n }\n }\n }\n for(auto i : mp){\n if(i.second >= 2){\n if(ans.size() == i.first.size()) ans = min(ans, i.first);\n else ans = (ans.size() > i.first.size()) ? ans : i.first;\n }\n }\n if(ans.size() <= 1) cout << 0 << endl;\n else cout << ans << endl;\n }\n}", "accuracy": 1, "time_ms": 380, "memory_kb": 30216, "score_of_the_acc": -1.0072, "final_rank": 15 }, { "submission_id": "aoj_1316_6029083", "code_snippet": "// #include \"atcoder/all\"\n#include <iostream> // cout, endl, cin\n#include <string> // string, to_string, stoi\n#include <vector> // vector\n#include <algorithm> // min, max, swap, sort, reverse, lower_bound, upper_bound\n#include <utility> // pair, make_pair\n#include <tuple> // tuple, make_tuple\n#include <cstdint> // int64_t, int*_t\n#include <cstdio> // printf\n#include <map> // map\n#include <queue> // queue, priority_queue\n#include <set> // set\n#include <stack> // stack\n#include <deque> // deque\n#include <unordered_map> // unordered_map\n#include <unordered_set> // unordered_set\n#include <bitset> // bitset\n#include <cctype> // isupper, islower, isdigit, toupper, tolower\n#include <iomanip> // setprecision\n#include <complex> // complex\n#include <math.h>\n#include <functional>\n#include <cassert>\nusing namespace std;\n// using namespace atcoder;\nusing ll = long long;\nusing P = pair<ll,ll>;\nconstexpr ll INF = 1e18;\nconstexpr ll LLMAX = 9223372036854775807;\nconstexpr int inf = 1e9;\nconstexpr ll mod = 1000000007;\n// constexpr ll mod = 998244353;\n// 右下左上\nconst int dx[8] = {1, 0, -1, 0,1,1,-1,-1};\nconst int dy[8] = {0, 1, 0, -1,1,-1,1,-1};\ntemplate<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }\ntemplate<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }\n#define eol endl\n// ---------------------------------------------------------------------------\n\nbool solve(){\n int H,W;\n cin >> H >> W;\n if(H == 0) return false;\n vector<string> G(H);\n for(int i=0; i<H; i++){\n cin >> G[i];\n }\n set<string> mp;\n int ansL = 0;\n string ansS = \"\";\n for(int i=0; i<H; i++){\n for(int j=0; j<W; j++){\n for(int k=0; k<8; k++){\n int y = i;\n int x = j;\n string now = \"\";\n now += G[y][x];\n y += dy[k];\n x += dx[k];\n y += H;\n x += W;\n y %= H;\n x %= W;\n while(y != i || x != j){\n now += G[y][x];\n if(mp.count(now)){\n if(ansL < now.size()){\n ansL = now.size();\n ansS = now;\n }else if(ansL == now.size()){\n chmin(ansS,now);\n }\n }else{\n mp.emplace(now);\n }\n y += dy[k];\n x += dx[k];\n y += H;\n x += W;\n y %= H;\n x %= W;\n }\n }\n }\n }\n if(ansL <= 1){\n cout << 0 << endl;\n }else{\n cout << ansS << endl;\n }\n return true;\n}\n\nint main(){\n ios::sync_with_stdio(false);\n cin.tie(nullptr);\n while(solve());\n return 0;\n}", "accuracy": 1, "time_ms": 300, "memory_kb": 29788, "score_of_the_acc": -0.9573, "final_rank": 7 }, { "submission_id": "aoj_1316_6029006", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\n\nusing ll = long long;\nconst int INF = 1e9;\nconst ll inf = 1LL<<60;\n\nvector<int> dx = {0, 1, 1, 1, 0, -1, -1, -1};\nvector<int> dy = {1, 1, 0, -1, -1, -1, 0, 1};\n\nbool solve() {\n int h, w; cin >> h >> w;\n if (h == 0) return false;\n vector<string> s(h);\n for (int i=0; i<h; i++) cin >> s[i];\n vector<string> S;\n for (int i=0; i<h; i++) for (int j=0; j<w; j++) {\n for (int k=0; k<8; k++) {\n string t = \"\";\n t += s[i][j];\n int nx = i, ny = j;\n while (1) {\n nx = (nx + dx[k]);\n ny = (ny + dy[k]);\n if (nx < 0) nx = h-1;\n if (ny < 0) ny = w-1;\n if (nx >= h) nx = 0;\n if (ny >= w) ny = 0;\n if (i == nx && j == ny) break;\n t += s[nx][ny];\n }\n if (t.size() == 1) continue;\n S.push_back(t);\n }\n }\n auto calc = [](string &a, string &b) -> string {\n string res = \"\";\n for (int i=0; i<min(a.size(), b.size()); i++) {\n if (a[i] != b[i]) break;\n res += a[i];\n }\n return res;\n };\n int ma = 2;\n set<string> P;\n for (int i=0; i<S.size(); i++) for (int j=i+1; j<S.size(); j++) {\n string k = calc(S[i], S[j]);\n if (k.size() < ma) continue;\n P.insert(k);\n ma = max(ma, (int)k.size());\n }\n for (auto c : P) if (c.size() == ma) {\n cout << c << endl;\n return true;\n }\n cout << 0 << endl;\n return true;\n}\n\nint main() {\n while (solve());\n}", "accuracy": 1, "time_ms": 180, "memory_kb": 3592, "score_of_the_acc": -0.0075, "final_rank": 1 }, { "submission_id": "aoj_1316_6026945", "code_snippet": "#include <iostream>\n#include <algorithm>\n#include <vector>\n#include <string>\n#include <utility>\n#include <map>\n\nusing namespace std;\n\nint dx[] = { -1, -1, -1, 0, 0, 0, 1, 1, 1 };\nint dy[] = { -1, 0, 1, -1, 0, 1, -1, 0, 1 };\n\nint main() {\n\tint h, w;\n\twhile (cin >> h >> w, h || w) {\n\t\tvector<string> vs(h);\n\t\tfor (auto& s : vs) cin >> s;\n\t\tvector<map<string, int> > mps(h * w + 1);\n\t\tint ans_len = 0;\n\t\tfor (int x = 0; x < h; ++x) {\n\t\t\tfor (int y = 0; y < w; ++y) {\n\t\t\t\tfor (int i = 0; i < 9; ++i) {\n\t\t\t\t\tstring tmp { vs[x][y] };\n\t\t\t\t\tint nx = x, ny = y;\n\t\t\t\t\twhile (true) {\n\t\t\t\t\t\tnx = (nx + dx[i] + h) % h;\n\t\t\t\t\t\tny = (ny + dy[i] + w) % w;\n\t\t\t\t\t\tif (x == nx && y == ny) break;\n\t\t\t\t\t\ttmp += vs[nx][ny];\n\t\t\t\t\t\t++mps[tmp.length()][tmp];\n\t\t\t\t\t\tif (mps[tmp.length()][tmp] > 1)\n\t\t\t\t\t\t\tans_len = max(ans_len, (int)tmp.length());\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\tif (ans_len) {\n\t\t\tstring ans(ans_len, 'Z');\n\t\t\tfor (auto& [s, count] : mps[ans_len])\n\t\t\t\tif (count > 1 && ans > s) ans = s;\n\t\t\tcout << ans << endl;\n\t\t} else cout << 0 << endl;\n\t}\n\n\treturn 0;\n}", "accuracy": 1, "time_ms": 220, "memory_kb": 29704, "score_of_the_acc": -0.9192, "final_rank": 3 }, { "submission_id": "aoj_1316_6025991", "code_snippet": "#include <bits/stdc++.h>\n#define rep(i,n) for(int i = 0; i < (n); i++)\nusing namespace std;\ntypedef long long ll;\n\nint main(){\n\tcin.tie(0);\n\tios::sync_with_stdio(0);\n\n\tint di[] = {-1, -1, -1, 0, 0, +1, +1, +1};\n\tint dj[] = {-1, 0, +1, -1, +1, -1, 0, +1};\n\t\n\twhile(true) {\n\t\tint h,w; cin >> h >> w; if(h == 0) break;\n\t\tvector< string > s(h);\n\t\trep(i,h) cin >> s[i];\n\n\t\tmap<string,int> mp;\n\t\trep(d,8) {\n\t\t\tvector<vector<bool>> used(h, vector<bool> (w, false));\n\t\t\trep(i,h)rep(j,w) {\n\t\t\t\tif(used[i][j]) continue;\n\t\t\t\tint ci = i, cj = j;\n\t\t\t\tstring t = \"\";\n\t\t\t\twhile(!used[ci][cj]) {\n\t\t\t\t\tused[ci][cj] = true;\n\t\t\t\t\tt += s[ci][cj];\n\t\t\t\t\tci = (ci + di[d] + h) % h;\n\t\t\t\t\tcj = (cj + dj[d] + w) % w;\n\t\t\t\t}\n\t\t\t\t\n\t\t\t\tint N = t.size();\n\t\t\t\tt += t;\n\t\t\t\trep(k,N)for(int c = 2; c <= N; c++)\n\t\t\t\t\tmp[t.substr(k, c)]++;\n\t\t\t}\n\t\t}\n\n\t\tstring ans = \"\";\n\t\tfor(auto e : mp) if(e.second >= 2) {\n\t\t\tstring t = e.first;\n\t\t\tif(t.size() > ans.size()) ans = t;\n\t\t\telse if(t.size() == ans.size()) ans = min(ans, t);\n\t\t}\n\t\tcout << (ans.size() == 0 ? \"0\" : ans) << endl;\n\t}\n}", "accuracy": 1, "time_ms": 320, "memory_kb": 29936, "score_of_the_acc": -0.9712, "final_rank": 11 }, { "submission_id": "aoj_1316_5997941", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\n#define all(A) A.begin(),A.end()\nusing vll = vector<ll>;\n#define rep(i, n) for (long long i = 0; i < (long long)(n); i++)\n\n\nvll dx = { 1,-1,0 };\nvll dy = { 1,-1,0 };\nint main() {\n while (1) {\n ll H, W;\n cin >> H >> W;\n if (H == 0)return 0;\n vector<vector<char>> V(H, vector<char>(W));\n rep(h, H) {\n string S;\n cin >> S;\n rep(w, W)V[h][w] = S[w];\n }\n map<string, ll> M;\n rep(h, H) {\n rep(w, W) {\n rep(i, 8) {\n ll x = h; ll y = w;\n string K = \"\";\n while (1) {\n K += V[x][y];\n x += dx[i / 3]+H;\n y += dy[i % 3]+W;\n x %= H; y %= W;\n M[K]++;\n if (x == h && y == w) {\n \n break;\n }\n }\n }\n }\n }\n ll m = 0;\n string an = \"0\";\n for (auto p : M) {\n if (p.second > 1) {\n if (p.first.size() > an.size()) {\n an = p.first;\n }\n else if (p.first.size() == an.size()) {\n if (p.first < an) {\n an = p.first;\n }\n }\n }\n }\n cout << an << endl;\n }\n}", "accuracy": 1, "time_ms": 310, "memory_kb": 30056, "score_of_the_acc": -0.9709, "final_rank": 10 }, { "submission_id": "aoj_1316_5879279", "code_snippet": "#pragma region Macros\n#include <bits/stdc++.h>\nusing namespace std;\ntemplate <class T> inline bool chmax(T &a, T b) {\n if(a < b) {\n a = b;\n return 1;\n }\n return 0;\n}\ntemplate <class T> inline bool chmin(T &a, T b) {\n if(a > b) {\n a = b;\n return 1;\n }\n return 0;\n}\n#ifdef DEBUG\ntemplate <class T, class U>\nostream &operator<<(ostream &os, const pair<T, U> &p) {\n os << '(' << p.first << ',' << p.second << ')';\n return os;\n}\ntemplate <class T> ostream &operator<<(ostream &os, const vector<T> &v) {\n os << '{';\n for(int i = 0; i < (int)v.size(); i++) {\n if(i) { os << ','; }\n os << v[i];\n }\n os << '}';\n return os;\n}\nvoid debugg() { cerr << endl; }\ntemplate <class T, class... Args>\nvoid debugg(const T &x, const Args &... args) {\n cerr << \" \" << x;\n debugg(args...);\n}\n#define debug(...) \\\n cerr << __LINE__ << \" [\" << #__VA_ARGS__ << \"]: \", debugg(__VA_ARGS__)\n#define dump(x) cerr << __LINE__ << \" \" << #x << \" = \" << (x) << endl\n#else\n#define debug(...) (void(0))\n#define dump(x) (void(0))\n#endif\n\nstruct Setup {\n Setup() {\n cin.tie(0);\n ios::sync_with_stdio(false);\n cout << fixed << setprecision(15);\n }\n} __Setup;\n\nusing ll = long long;\n#define ALL(v) (v).begin(), (v).end()\n#define RALL(v) (v).rbegin(), (v).rend()\n#define FOR(i, a, b) for(int i = (a); i < int(b); i++)\n#define REP(i, n) FOR(i, 0, n)\nconst int INF = 1 << 30;\nconst ll LLINF = 1LL << 60;\nconstexpr int MOD = 1000000007;\n\nvoid Case(int i) { cout << \"Case #\" << i << \": \"; }\nint popcount(int x) { return __builtin_popcount(x); }\nll popcount(ll x) { return __builtin_popcountll(x); }\n#pragma endregion Macros\n\nconst int di[8] = {-1, -1, 0, 1, 1, 1, 0, -1};\nconst int dj[8] = {0, 1, 1, 1, 0, -1, -1, -1};\n\nbool solve() {\n int N, M;\n cin >> N >> M;\n if(N == 0) return false;\n vector<string> G(N);\n REP(i, N) cin >> G[i];\n\n map<string, int> cnt;\n REP(si, N) REP(sj, M) {\n set<string> se;\n REP(dir, 8) {\n string s = \"\";\n int ni = si, nj = sj;\n while(1) {\n s += G[ni][nj];\n se.emplace(s);\n ni = (ni + di[dir] + N) % N;\n nj = (nj + dj[dir] + M) % M;\n if(ni == si and nj == sj) break;\n }\n }\n for(auto s : se) cnt[s] += 1;\n }\n string ans = \"\";\n for(const auto& [s, num] : cnt) {\n if(num <= 1) continue;\n if(ans.size() < s.size()) ans = s;\n else if(ans > s) ans = s;\n }\n\n if((int)ans.size() <= 1) cout << \"0\\n\";\n else cout << ans << \"\\n\";\n return true;\n}\n\nint main() {\n while(solve());\n}", "accuracy": 1, "time_ms": 490, "memory_kb": 30216, "score_of_the_acc": -1.0556, "final_rank": 20 }, { "submission_id": "aoj_1316_5876556", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\nusing P = pair<int,int>;\n\nvoid solve(int H, int W){\n vector<string> vec(H);\n for(int i=0; i<H; i++){\n cin >> vec[i];\n }\n set<string> S;\n string ans = \"\";\n for(int i=0; i<H; i++){\n for(int j=0; j<W; j++){\n for(int a=-1; a<=1; a++){\n for(int b=-1; b<=1; b++){\n if(a == 0 && b == 0){\n continue;\n }\n string add = \"\";\n set<P> p;\n int x = i;\n int y = j;\n while(true){\n if(p.count(P(x,y))){\n break;\n }\n add += vec[x][y];\n p.insert(P(x,y));\n x = (x+a+H)%H;\n y = (y+b+W)%W; \n if(S.count(add)){\n if(add.size() > ans.size()){\n ans = add;\n }\n if(add.size() == ans.size() && add < ans){\n ans = add;\n }\n }\n S.insert(add);\n }\n }\n }\n }\n }\n if(ans.size() <= 1){\n cout << 0 << endl;\n }\n else{\n cout << ans << endl;\n }\n}\n\nint main(){\n while(true){\n int H, W;\n cin >> H >> W;\n if(H == 0){\n break;\n }\n solve(H,W);\n }\n}", "accuracy": 1, "time_ms": 480, "memory_kb": 29904, "score_of_the_acc": -1.0405, "final_rank": 18 } ]
aoj_1314_cpp
Problem J: Matrix Calculator Dr. Jimbo, an applied mathematician, needs to calculate matrices all day for solving his own problems. In his laboratory, he uses an excellent application program for manipulating matrix expressions, however, he cannot use it outside his laboratory because the software consumes much of resources. He wants to manipulate matrices outside, so he needs a small program similar to the excellent application for his handheld computer. Your job is to provide him a program that computes expressions of matrices. Expressions of matrices are described in a simple language. Its syntax is shown in Table J.1. Note that even a space and a newline have meaningful roles in the syntax. The start symbol of this syntax is program that is defined as a sequence of assignments in Table J.1. Each assignment has a variable on the left hand side of an equal symbol (" = ") and an expression of matrices on the right hand side followed by a period and a newline ( NL ). It denotes an assignment of the value of the expression to the variable. The variable (var in Table J.1) is indicated by an uppercase Roman letter. The value of the expression ( expr ) is a matrix or a scalar, whose elements are integers. Here, a scalar integer and a 1 × 1 matrix whose only element is the same integer can be used interchangeably. An expression is one or more terms connected by " + " or " - " symbols. A term is one or more factors connected by " * " symbol. These operators (" + ", " - ", " * ") are left associative. A factor is either a primary expression (primary) or a " - " symbol followed by a factor. This unary operator " - " is right associative. The meaning of operators are the same as those in the ordinary arithmetic on matrices: Denoting matrices by A and B , A + B , A - B , A * B , and -A are defined as the matrix sum, difference, product, and negation. The sizes of A and B should be the same for addition and subtraction. The number of columns of A and the number of rows of B should be the same for multiplication. Note that all the operators +, -, * and unary - represent computations of addition, subtraction, multiplication and negation modulo M = 2 15 = 32768, respectively. Thus all the values are nonnegative integers between 0 and 32767, inclusive. For example, the result of an expression 2 - 3 should be 32767, instead of -1. inum is a non-negative decimal integer less than M . var represents the matrix that is assigned to the variable var in the most recent preceding assignment statement of the same variable. matrix represents a mathematical matrix similar to a 2-dimensional array whose elements are integers. It is denoted by a row-seq with a pair of enclosing square brackets. row-seq represents a sequence of rows, adjacent two of which are separated by a semicolon. row represents a sequence of expressions, adjacent two of which are separated by a space character. For example, [1 2 3;4 5 6] represents a matrix . The first row has three integers separated b ...(truncated)
[ { "submission_id": "aoj_1314_10908049", "code_snippet": "#include <bits/stdc++.h>\n\nusing namespace std;\n\nconst int MOD = 32768;\ntypedef vector<int> List;\ntypedef vector<vector<int>> Matrix;\nMatrix variables[26];\n\nMatrix parseExpr(const char*& p);\n\nvoid skip(const char *&p)\n{\n while ( p && *p == ' ' ) p++;\n}\nbool consume(const char *&p, char c)\n{\n if ( *p && *p == c )\n {\n p++;\n return true;\n }\n return false;\n}\nMatrix operator +(const Matrix &A, const Matrix &B)\n{\n int H = A.size(), W = A[0].size();\n Matrix ret(H, List(W));\n for ( int y = 0 ; y < H ; y++ )\n for ( int x = 0 ; x < W ; x++ )\n ret[y][x] = (A[y][x] + B[y][x]) % MOD;\n return ret;\n}\nMatrix operator *(const Matrix &A, const Matrix &B)\n{\n int Ha = A.size(), Wa = A[0].size();\n int Hb = B.size(), Wb = B[0].size();\n\n if ( Ha == 1 && Wa == 1 )\n {\n Matrix ret(Hb, List(Wb));\n for ( int y = 0 ; y < Hb ; y++ )\n for ( int x = 0 ; x < Wb ; x++ )\n ret[y][x] = (A[0][0] * B[y][x]) % MOD;\n return ret;\n }\n if ( Hb == 1 && Wb == 1 )\n {\n Matrix ret(Ha, List(Wa));\n for ( int y = 0 ; y < Ha ; y++ )\n for ( int x = 0 ; x < Wa ; x++ )\n ret[y][x] = (A[y][x] * B[0][0]) % MOD;\n return ret;\n }\n\n Matrix ret(Ha, List(Wb));\n for ( int y = 0 ; y < Ha ; y++ )\n for ( int x = 0 ; x < Wb ; x++ )\n {\n ret[y][x] = 0;\n for ( int t = 0 ; t < Wa ; t ++ )\n ret[y][x] = (ret[y][x] + A[y][t] * B[t][x]) % MOD;\n }\n return ret;\n}\nMatrix operator -(const Matrix &A, const Matrix &B)\n{\n int H = A.size(), W = A[0].size();\n Matrix ret(H, List(W));\n for ( int y = 0 ; y < H ; y++ )\n for ( int x = 0 ; x < W ; x++ )\n ret[y][x] = (A[y][x] - B[y][x]) % MOD;\n return ret;\n}\nMatrix operator -(const Matrix &A)\n{\n int H = A.size(), W = A[0].size();\n Matrix ret(H, List(W));\n for ( int y = 0 ; y < H ; y++ )\n for ( int x = 0 ; x < W ; x++ )\n ret[y][x] = (-A[y][x] + MOD) % MOD;\n return ret;\n}\nMatrix parseInum(const char*& p)\n{\n int val = 0;\n for ( ; isdigit(*p); p++)\n {\n val = val * 10 + (*p - '0');\n val %= MOD;\n }\n return Matrix(1, List(1, val));\n}\nMatrix parseRow(const char*& p)\n{\n Matrix ret = parseExpr(p);\n while ( *p )\n {\n skip(p);\n if ( *p == ']' || *p == ';' ) break;\n auto nxt = parseExpr(p);\n for ( int y = 0 ; y < ret.size() ; y ++ )\n for ( auto n: nxt[y] )\n ret[y].push_back(n);\n }\n return ret;\n}\nMatrix parseRowSeq(const char*& p)\n{\n Matrix ret = parseRow(p);\n while ( *p )\n {\n skip(p);\n if ( *p == ']') break;\n consume(p, ';');\n auto nxt = parseRow(p);\n for (auto &row: nxt) ret.emplace_back(row);\n }\n return ret;\n}\nMatrix Transpose(const Matrix &A)\n{\n int H = A.size(), W = A[0].size();\n Matrix ret(W, List(H));\n for ( int y = 0 ; y < H ; y++ )\n for ( int x = 0 ; x < W ; x++ )\n ret[x][y] = A[y][x];\n return ret;\n}\nMatrix parsePrimary(const char*& p) {\n Matrix ret;\n skip(p);\n if (isdigit(*p))\n ret = parseInum(p);\n else if (isupper(*p))\n ret = variables[*p++ - 'A'];\n else if ( consume(p, '[') )\n ret = parseRowSeq(p), consume(p, ']');\n else if ( consume(p, '(') )\n ret = parseExpr(p), consume(p, ')');\n\n while ( true )\n {\n if (consume(p, '(') ) // INDEXED_PRIMARY\n {\n auto A = parseExpr(p);\n consume(p, ',');\n auto B = parseExpr(p);\n consume(p, ')');\n\n Matrix nxt(A[0].size(), List(B[0].size()));\n for ( int y = 0 ; y < A[0].size() ; y++ )\n for ( int x = 0 ; x < B[0].size() ; x++ )\n nxt[y][x] = ret[ A[0][y] - 1 ][ B[0][x] - 1 ];\n swap(ret, nxt);\n }\n else if ( *p == '\\'') // TRANSPOSED_PRIMARY\n {\n bool isTrans = false;\n while ( consume(p, '\\'') ) isTrans = !isTrans;\n if ( isTrans ) ret = Transpose(ret);\n }\n else break;\n }\n return ret;\n}\n\nMatrix parseFactor(const char*& p) {\n bool neg = false;\n while ( consume(p, '-') ) neg = !neg;\n auto ret = parsePrimary(p);\n return neg ? -ret : ret;\n}\n\nMatrix parseTerm(const char*& p) {\n auto ret = parseFactor(p);\n while (consume(p, '*') ) ret = ret * parseFactor(p);\n return ret;\n}\n\nMatrix parseExpr(const char*& p) {\n auto ret = parseTerm(p);\n while (true) {\n if ( consume(p, '+') ) ret = ret + parseTerm(p);\n else if ( consume(p, '-') ) ret = ret - parseTerm(p);\n else break;\n }\n return ret;\n}\nvoid print(const Matrix &m)\n{\n int H = m.size();\n int W = m[0].size();\n for ( int y = 0 ; y < H ; y++ )\n for ( int x = 0 ; x < W ; x++ )\n cout << ( m[y][x] % MOD + MOD ) % MOD << \" \\n\"[x+1 == W];\n}\nvoid sol(int N) {\n for ( int i = 0 ; i < 26 ; i ++ ) variables[i].clear();\n\n string s;\n for (int i = 0; i < N; i++) {\n getline(cin, s);\n int varIndex = s[0] - 'A';\n while ( !s.empty() && ( s.back() == ' ' || s.back() == '.' ) ) s.pop_back();\n const char* p = s.c_str() + s.find('=') + 1;\n variables[varIndex] = parseExpr(p);\n print(variables[varIndex]);\n }\n}\n\nint main() {\n#ifdef AJAVA_DEBUG\nfreopen(\"output.txt\", \"wt\", stdout);\n#endif\n cin.tie(nullptr)->sync_with_stdio(false);\n int N;\n for ( int i = 0 ; cin >> N && N != 0 ; i ++ )\n {\n cin.ignore();\n sol(N);\n cout << \"-----\\n\";\n }\n return 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3548, "score_of_the_acc": -0.1001, "final_rank": 3 }, { "submission_id": "aoj_1314_9417335", "code_snippet": "#line 1 \"main.cpp\"\n#include<bits/stdc++.h>\n//#pragma GCC target(\"avx,avx2\")\n//#pragma GCC optimize(\"O3\")\n//#pragma GCC optimize(\"unroll-loops\")\n#ifdef LOCAL\n#include <debug.hpp>\n#define debug(...) debug_print::multi_print(#__VA_ARGS__, __VA_ARGS__)\n#else\n#define debug(...) (static_cast<void>(0))\n#endif\nusing namespace std;\nusing ll = long long;\nusing ull = unsigned long long;\nusing ld = long double;\nusing pll = pair<ll,ll>;\nusing pii = pair<int,int>;\nusing vi = vector<int>;\nusing vvi = vector<vi>;\nusing vvvi = vector<vvi>;\nusing vl = vector<ll>;\nusing vvl = vector<vl>;\nusing vvvl = vector<vvl>;\nusing vul = vector<ull>;\nusing vpii = vector<pii>;\nusing vvpii = vector<vpii>;\nusing vpll = vector<pll>;\nusing vs = vector<string>;\ntemplate<class T> using pq = priority_queue<T,vector<T>, greater<T>>;\n#define overload4(_1, _2, _3, _4, name, ...) name\n#define overload3(a,b,c,name,...) name\n#define rep1(n) for (ll UNUSED_NUMBER = 0; UNUSED_NUMBER < (n); ++UNUSED_NUMBER)\n#define rep2(i, n) for (ll i = 0; i < (n); ++i)\n#define rep3(i, a, b) for (ll i = (a); i < (b); ++i)\n#define rep4(i, a, b, c) for (ll i = (a); i < (b); i += (c))\n#define rep(...) overload4(__VA_ARGS__, rep4, rep3, rep2, rep1)(__VA_ARGS__)\n#define rrep1(n) for(ll i = (n) - 1;i >= 0;i--)\n#define rrep2(i,n) for(ll i = (n) - 1;i >= 0;i--)\n#define rrep3(i,a,b) for(ll i = (b) - 1;i >= (a);i--)\n#define rrep4(i,a,b,c) for(ll i = (a) + ((b)-(a)-1) / (c) * (c);i >= (a);i -= c)\n#define rrep(...) overload4(__VA_ARGS__, rrep4, rrep3, rrep2, rrep1)(__VA_ARGS__)\n#define all1(i) begin(i) , end(i)\n#define all2(i,a) begin(i) , begin(i) + a\n#define all3(i,a,b) begin(i) + a , begin(i) + b\n#define all(...) overload3(__VA_ARGS__, all3, all2, all1)(__VA_ARGS__)\n#define sum(...) accumulate(all(__VA_ARGS__),0LL)\ntemplate<class T> bool chmin(T &a, const T &b){ if(a > b){ a = b; return 1; } else return 0; }\ntemplate<class T> bool chmax(T &a, const T &b){ if(a < b){ a = b; return 1; } else return 0; }\ntemplate<class T> auto min(const T& a){return *min_element(all(a));}\ntemplate<class T> auto max(const T& a){return *max_element(all(a));}\ntemplate<class... Ts> void in(Ts&... t);\n#define INT(...) int __VA_ARGS__; in(__VA_ARGS__)\n#define LL(...) ll __VA_ARGS__; in(__VA_ARGS__)\n#define STR(...) string __VA_ARGS__; in(__VA_ARGS__)\n#define CHR(...) char __VA_ARGS__; in(__VA_ARGS__)\n#define DBL(...) double __VA_ARGS__; in(__VA_ARGS__)\n#define LD(...) ld __VA_ARGS__; in(__VA_ARGS__)\n#define VEC(type, name, size) vector<type> name(size); in(name)\n#define VV(type, name, h, w) vector<vector<type>> name(h, vector<type>(w)); in(name)\nll intpow(ll a, ll b){ ll ans = 1; while(b){if(b & 1) ans *= a; a *= a; b /= 2;} return ans;}\nll modpow(ll a, ll b, ll p){ ll ans = 1; a %= p;if(a < 0) a += p;while(b){ if(b & 1) (ans *= a) %= p; (a *= a) %= p; b /= 2; } return ans; }\nbool is_clamp(ll val,ll low,ll high) {return low <= val && val < high;}\nvoid Yes() {cout << \"Yes\\n\";return;}\nvoid No() {cout << \"No\\n\";return;}\nvoid YES() {cout << \"YES\\n\";return;}\nvoid NO() {cout << \"NO\\n\";return;}\ntemplate <typename T>\nT floor(T a, T b) {return a / b - (a % b && (a ^ b) < 0);}\ntemplate <typename T>\nT ceil(T x, T y) {return floor(x + y - 1, y);}\ntemplate <typename T>\nT bmod(T x, T y) {return x - y * floor(x, y);}\ntemplate <typename T>\npair<T, T> divmod(T x, T y) {T q = floor(x, y);return {q, x - q * y};}\nnamespace IO{\n#define VOID(a) decltype(void(a))\nstruct setting{ setting(){cin.tie(nullptr); ios::sync_with_stdio(false);fixed(cout); cout.precision(12);}} setting;\ntemplate<int I> struct P : P<I-1>{};\ntemplate<> struct P<0>{};\ntemplate<class T> void i(T& t){ i(t, P<3>{}); }\nvoid i(vector<bool>::reference t, P<3>){ int a; i(a); t = a; }\ntemplate<class T> auto i(T& t, P<2>) -> VOID(cin >> t){ cin >> t; }\ntemplate<class T> auto i(T& t, P<1>) -> VOID(begin(t)){ for(auto&& x : t) i(x); }\ntemplate<class T, size_t... idx> void ituple(T& t, index_sequence<idx...>){in(get<idx>(t)...);}\ntemplate<class T> auto i(T& t, P<0>) -> VOID(tuple_size<T>{}){ituple(t, make_index_sequence<tuple_size<T>::value>{});} \n#undef VOID\n}\n#define unpack(a) (void)initializer_list<int>{(a, 0)...}\ntemplate<class... Ts> void in(Ts&... t){ unpack(IO :: i(t)); }\n#undef unpack\nconstexpr long double PI = 3.141592653589793238462643383279L;\ntemplate <class F> struct REC {\n F f;\n REC(F &&f_) : f(forward<F>(f_)) {}\n template <class... Args> auto operator()(Args &&...args) const { return f(*this, forward<Args>(args)...); }};\n\n//constexpr int mod = 998244353;\nconstexpr int mod = 1000000007;\ntemplate <typename T> \nstruct Edge {\n\tint from, to;\n\tT cost;\n\tEdge() = default;\n\tEdge(int _to, T _cost) : from(-1), to(_to), cost(_cost) {}\n\tEdge(int _from, int _to, T _cost) : from(_from), to(_to), cost(_cost) {}\n\tbool operator < (const Edge &a) const { return cost < a.cost; }\n\tbool operator > (const Edge &a) const { return cost > a.cost; }\n Edge &operator = (const int &x) {\n to = x;\n return *this;\n }\n operator int() const { return to; }\n friend ostream operator<<(ostream &os, Edge &edge) { return os << edge.to; }\n};\ntemplate <typename T>\nusing Edges = vector<Edge<T>>;\ntemplate <typename T>\nusing Wgraph = vector<Edges<T>>;\nusing Ugraph = vector<vector<int>>;\nUgraph uinput(int N, int M = -1, bool is_directed = false, int origin = 1) {\n Ugraph g(N);\n if (M == -1) M = N - 1;\n while(M--) {\n int a,b;\n cin >> a >> b;\n a -= origin, b -= origin;\n g[a].push_back(b);\n if(!is_directed) g[b].push_back(a);\n }\n return g;\n}\ntemplate <typename T>\nWgraph<T> winput(int N, int M = -1, bool is_directed = false,int origin = 1) {\n Wgraph<T> g(N);\n if (M == -1) M = N - 1;\n while(M--) {\n int a,b;\n T c;\n cin >> a >> b >> c;\n a -= origin, b -= origin;\n g[a].emplace_back(b,c);\n if(!is_directed) g[b].emplace_back(a,c);\n }\n return g;\n}\n#line 2 \"library/modint/barrett-reduction.hpp\"\nstruct Barrett {\n using u32 = unsigned int;\n using i64 = long long;\n using u64 = unsigned long long;\n u32 m;\n u64 im;\n Barrett() : m(), im() {}\n Barrett(int n) : m(n), im(u64(-1) / m + 1) {}\n constexpr inline i64 quo(u64 n) {\n u64 x = u64((__uint128_t(n) * im) >> 64);\n u32 r = n - x * m;\n return m <= r ? x - 1 : x;\n }\n constexpr inline i64 rem(u64 n) {\n u64 x = u64((__uint128_t(n) * im) >> 64);\n u32 r = n - x * m;\n return m <= r ? r + m : r;\n }\n constexpr inline pair<i64, int> quorem(u64 n) {\n u64 x = u64((__uint128_t(n) * im) >> 64);\n u32 r = n - x * m;\n if (m <= r) return {x - 1, r + m};\n return {x, r};\n }\n constexpr inline i64 pow(u64 n, i64 p) {\n u32 a = rem(n), r = m == 1 ? 0 : 1;\n while (p) {\n if (p & 1) r = rem(u64(r) * a);\n a = rem(u64(a) * a);\n p >>= 1;\n }\n return r;\n }\n};\n#line 3 \"library/modint/ArbitaryModint.hpp\"\nstruct ArbitraryModint {\n int x;\n ArbitraryModint():x(0) {}\n ArbitraryModint(int64_t y) {\n int z = y % get_mod();\n if(z < 0) z += get_mod();\n x = z;\n }\n ArbitraryModint &operator+=(const ArbitraryModint &p) {\n if((x += p.x) >= get_mod()) x -= get_mod();\n return *this;\n }\n ArbitraryModint &operator-=(const ArbitraryModint &p) {\n if((x += get_mod() - p.x) >= get_mod()) x -= get_mod();\n return *this;\n }\n ArbitraryModint &operator*=(const ArbitraryModint &p) {\n x = rem((unsigned long long)x * p.x);\n return *this;\n }\n ArbitraryModint &operator/=(const ArbitraryModint &p) {\n *this *= p.inverse();\n return *this;\n }\n ArbitraryModint operator-() const {return ArbitraryModint(-x);};\n ArbitraryModint operator+(const ArbitraryModint &p) const{\n return ArbitraryModint(*this) += p;\n }\n ArbitraryModint operator-(const ArbitraryModint &p) const{\n return ArbitraryModint(*this) -= p;\n }\n ArbitraryModint operator*(const ArbitraryModint &p) const{\n return ArbitraryModint(*this) *= p;\n }\n ArbitraryModint operator/(const ArbitraryModint &p) const {\n return ArbitraryModint(*this) /= p;\n }\n bool operator==(const ArbitraryModint &p) {return x == p.x;}\n bool operator!=(const ArbitraryModint &p) {return x != p.x;}\n ArbitraryModint inverse() const {\n int a = x,b = get_mod(),u = 1,v = 0,t;\n while(b > 0) {\n t = a / b;\n swap(a -= t * b,b);\n swap(u -= t * v,v);\n }\n return ArbitraryModint(u);\n }\n ArbitraryModint pow(int64_t n) const {\n ArbitraryModint ret(1),mul(x);\n while(n > 0) {\n if(n & 1) ret *= mul;\n mul *= mul;\n n >>= 1;\n }\n return ret;\n }\n friend ostream &operator<<(ostream &os,const ArbitraryModint &p) {\n return os << p.x;\n }\n friend istream &operator>>(istream &is,ArbitraryModint &a) {\n int64_t t;\n is >> t;\n a = ArbitraryModint(t);\n return (is);\n }\n int get() const {return x;}\n inline unsigned int rem(unsigned long long p) {return barrett().rem(p);};\n static inline Barrett &barrett() {\n static Barrett b;\n return b;\n }\n static inline int &get_mod() {\n static int mod = 0;\n return mod;\n }\n static void set_mod(int md) {\n assert(0 < md && md <= (1LL << 30) - 1);\n get_mod() = md;\n barrett() = Barrett(md);\n }\n};\n#line 146 \"main.cpp\"\nusing mint = ArbitraryModint;\nusing vm = vector<mint>;\nusing vvm = vector<vm>;\nusing vvvm = vector<vvm>;\n#line 2 \"library/matrix/matrix.hpp\"\ntemplate <class T>\nstruct Matrix {\n vector<vector<T>> A;\n Matrix() = default;\n Matrix(int n, int m) : A(n, vector<T>(m, T())) {}\n Matrix(int n) : A(n, vector<T>(n, T())){};\n int H() const { return A.size(); }\n int W() const { return A[0].size(); }\n int size() const { return A.size(); }\n inline const vector<T> &operator[](int k) const { return A[k]; }\n inline vector<T> &operator[](int k) { return A[k]; }\n static Matrix I(int n) {\n Matrix mat(n);\n for (int i = 0; i < n; i++) mat[i][i] = 1;\n return (mat);\n }\n Matrix &operator+=(const Matrix &B) {\n int n = H(), m = W();\n assert(n == B.H() && m == B.W());\n for (int i = 0; i < n; i++) for (int j = 0; j < m; j++) (*this)[i][j] += B[i][j];\n return (*this);\n }\n Matrix &operator-=(const Matrix &B) {\n int n = H(), m = W();\n assert(n == B.H() && m == B.W());\n for (int i = 0; i < n; i++) for (int j = 0; j < m; j++) (*this)[i][j] -= B[i][j];\n return (*this);\n }\n Matrix &operator*=(const Matrix &B) {\n int n = H(), m = B.W(), p = W();\n assert(p == B.H());\n vector<vector<T>> C(n, vector<T>(m, T{}));\n for (int i = 0; i < n; i++) {\n for (int k = 0; k < p; k++) {\n if((*this)[i][k] == 0) continue;\n for (int j = 0; j < m; j++) C[i][j] += (*this)[i][k] * B[k][j];\n }\n }\n A.swap(C);\n return (*this);\n }\n Matrix &operator^=(long long k) {\n Matrix B = Matrix::I(H());\n while (k > 0) {\n if (k & 1) B *= *this;\n *this *= *this;\n k >>= 1LL;\n }\n A.swap(B.A);\n return (*this);\n }\n Matrix operator+(const Matrix &B) const { return (Matrix(*this) += B); }\n Matrix operator-(const Matrix &B) const { return (Matrix(*this) -= B); }\n Matrix operator*(const Matrix &B) const { return (Matrix(*this) *= B); }\n Matrix operator^(const long long k) const { return (Matrix(*this) ^= k); }\n bool operator==(const Matrix &B) const {\n assert(H() == B.H() && W() == B.W());\n for (int i = 0; i < H(); i++) for (int j = 0; j < W(); j++) {\n if (A[i][j] != B[i][j]) return false;\n }\n return true;\n }\n bool operator!=(const Matrix &B) const {\n assert(H() == B.H() && W() == B.W());\n for (int i = 0; i < H(); i++) for (int j = 0; j < W(); j++) {\n if (A[i][j] != B[i][j]) return true;\n }\n return false;\n }\n friend ostream &operator<<(ostream &os, const Matrix &p) {\n int n = p.H(), m = p.W();\n for (int i = 0; i < n; i++) {\n os << (i ? \" \" : \"\") << \"[\";\n for (int j = 0; j < m; j++) {\n os << p[i][j] << (j + 1 == m ? \"]\\n\" : \",\");\n }\n }\n return (os);\n }\n T determinant() const {\n Matrix B(*this);\n assert(H() == W());\n T ret = 1;\n for (int i = 0; i < H(); i++) {\n int idx = -1;\n for (int j = i; j < W(); j++) {\n if (B[j][i] != 0) {\n idx = j;\n break;\n }\n }\n if (idx == -1) return 0;\n if (i != idx) {\n ret *= T(-1);\n swap(B[i], B[idx]);\n }\n ret *= B[i][i];\n T inv = T(1) / B[i][i];\n for (int j = 0; j < W(); j++) {\n B[i][j] *= inv;\n }\n for (int j = i + 1; j < H(); j++) {\n T a = B[j][i];\n if (a == 0) continue;\n for (int k = i; k < W(); k++) {\n B[j][k] -= B[i][k] * a;\n }\n }\n }\n return ret;\n }\n};\n#line 151 \"main.cpp\"\nusing T = Matrix<mint>;\nvector<Matrix<mint>> VAR(26);\nT term(string &s,int &id);\nT expression(string &s,int &id);\nT factor(string &s,int &id);\nT primary(string &s,int &id);\nT matrix(string &s,int &id);\nT row_seq(string &s,int &id);\nT row(string &s,int &id);\nmint number(string &s,int &id) {\n mint ret = 0;\n while(isdigit(s[id])) {\n ret *= 10;\n ret += s[id++] - '0';\n }\n return ret;\n}\nT row(string &s,int &id) {\n T ret = expression(s,id);\n //debug(ret,id,s[id],s);\n while(1) {\n if(s[id] == ' ') {\n id++;\n T ret2 = expression(s,id);\n assert(ret.H() == ret2.H());\n T ret3(ret.H(),ret.W() + ret2.W());\n rep(i,ret.H()) {\n rep(j,ret.W()) {\n ret3[i][j] = ret[i][j];\n }\n }\n rep(i,ret2.H()) {\n rep(j,ret2.W()) {\n ret3[i][j + ret.W()] = ret2[i][j];\n }\n }\n swap(ret,ret3);\n }\n else break;\n }\n return ret;\n}\nT row_seq(string &s,int &id) {\n T ret = row(s,id);\n while(1) {\n if(s[id] == ';') {\n id++;\n T ret2 = row(s,id);\n assert(ret.W() == ret2.W());\n T ret3(ret.H() + ret2.H(),ret.W());\n rep(i,ret.H()) {\n rep(j,ret.W()) ret3[i][j] = ret[i][j];\n }\n rep(i,ret2.H()) {\n rep(j,ret2.W()) ret3[i+ret.H()][j] = ret2[i][j];\n }\n swap(ret,ret3);\n }\n else break;\n }\n return ret;\n}\nT matrix(string &s,int &id) {\n assert(s[id] == '[');\n id++;\n T ret = row_seq(s,id);\n //debug(ret);\n assert(s[id] == ']');\n id++;\n return ret;\n}\nT primary(string &s,int &id) {\n T ret;\n\tif(isdigit(s[id])) {\n mint res = number(s,id);\n ret = Matrix<mint>(1);\n ret[0][0] = res;\n }\n else if('A' <= s[id] && s[id] <= 'Z') {\n ret = VAR[s[id] - 'A'];\n id++;\n }\n else if(s[id] == '[') {\n ret = matrix(s,id);\n }\n else if(s[id] == '(') {\n id++;\n ret = expression(s,id);\n assert(s[id] == ')');\n id++;\n }\n while(1) {\n if(s[id] == '(') {\n id++;\n T ret2 = expression(s,id);\n assert(s[id] == ',');\n id++;\n T ret3 = expression(s,id);\n assert(s[id] == ')');\n id++;\n T ret4(ret2.W(),ret3.W());\n rep(i,ret2.W()) {\n rep(j,ret3.W()) {\n ret4[i][j] = ret[ret2[0][i].get() - 1][ret3[0][j].get() - 1];\n }\n }\n swap(ret,ret4);\n }\n else if(s[id] == '\\'') {\n id++;\n T ret2(ret.W(),ret.H());\n rep(i,ret.H()) {\n rep(j,ret.W()) {\n ret2[j][i] = ret[i][j];\n }\n }\n swap(ret,ret2);\n }\n else break;\n }\n return ret;\n}\nT factor(string &s,int &id) {\n\tint co = 1;\n\tT ret;\n\tif(s[id] == '-') {\n\t\tco = -1;\n\t\tid++;\n\t\tret = factor(s,id);\n\t}\n\telse ret = primary(s,id);\n\trep(i,ret.H()) {\n\t\trep(j,ret.W()) {\n\t\t\tret[i][j] *= co;\n\t\t}\n\t}\n\treturn ret;\n}\nT term(string &s,int &id) {\n T ret = factor(s,id);\n while(1) {\n if(s[id] == '*') {\n id++;\n T ret2 = factor(s,id);\n //debug(ret,ret2);\n if(ret.H() == ret.W() && ret.H() == 1) {\n rep(i,ret2.H()) {\n rep(j,ret2.W()) {\n ret2[i][j] *= ret[0][0];\n }\n }\n swap(ret,ret2);\n }\n else if(ret2.H() == ret2.W() && ret2.H() == 1) {\n rep(i,ret.H()) {\n rep(j,ret.W()) {\n ret[i][j] *= ret2[0][0];\n }\n }\n }\n else ret *= ret2;\n }\n else break;\n }\n return ret;\n}\nT expression(string &s,int &id) {\n T ret = term(s,id);\n\twhile(1) {\n\t\tif(s[id] == '+') {\n\t\t\tid++;\n\t\t\tT ret2 = term(s,id);\n\t\t\tret += ret2;\n\t\t}\n\t\telse if(s[id] == '-') {\n\t\t\tid++;\n\t\t\tT ret2 = term(s,id);\n\t\t\tret -= ret2;\n\t\t}\n\t\telse break;\n\t}\n return ret;\n}\n\nint main() {\n\tmint::set_mod(32768);\n string s,t;\n\twhile(1) {\n\t\tgetline(cin,t);\n int x = stoi(t);\n\t\tif(x == 0) break;\n\t\twhile(x--) {\n\t\t\tgetline(cin,s);\n\t\t\tint p = s[0] - 'A';\n\t\t\tint id = 2;\n T ret = expression(s,id);\n VAR[p] = ret;\n rep(i,ret.H()) {\n rep(j,ret.W()) {\n cout << ret[i][j] << \" \\n\"[j == ret.W() - 1];\n }\n }\n\t\t}\n cout << \"-----\\n\";\n\t}\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3760, "score_of_the_acc": -0.1329, "final_rank": 11 }, { "submission_id": "aoj_1314_8458258", "code_snippet": "#include <bits/stdc++.h>\n\nstruct Matrix {\n static constexpr int MOD = 32768;\n std::vector<std::vector<int>> data;\n\n Matrix() {}\n explicit Matrix(int value): data(1, std::vector<int>(1, value)) {}\n explicit Matrix(int n, int m): data(n, std::vector<int>(m)) {}\n\n std::pair<int, int> size() const {\n return {data.size(), data.front().size()};\n }\n\n friend std::ostream& operator<<(std::ostream& os, const Matrix& mat) {\n auto [n, m] = mat.size();\n for (int i = 0; i < n; i++) {\n for (int j = 0; j < m; j++) {\n os << mat.data[i][j];\n if (j + 1 != m) {\n os << ' ';\n }\n }\n if (i + 1 != n) {\n os << '\\n';\n }\n }\n return os;\n }\n\n bool is_scalar() const {\n return data.size() == 1 && data.front().size() == 1;\n }\n\n friend Matrix operator+(const Matrix& lhs, const Matrix& rhs) {\n if (lhs.is_scalar() ^ rhs.is_scalar()) {\n if (lhs.is_scalar()) {\n return lhs.data.front().front() + rhs;\n } else {\n return rhs.data.front().front() + lhs;\n }\n }\n\n auto [n, m] = lhs.size();\n\n Matrix res(lhs);\n for (int i = 0; i < n; i++) {\n for (int j = 0; j < m; j++) {\n res.data[i][j] += rhs.data[i][j];\n res.data[i][j] %= MOD;\n }\n }\n\n return res;\n }\n\n friend Matrix operator-(const Matrix& lhs, const Matrix& rhs) {\n if (lhs.is_scalar() ^ rhs.is_scalar()) {\n if (lhs.is_scalar()) {\n return lhs.data.front().front() - rhs;\n } else {\n return rhs.data.front().front() - lhs;\n }\n }\n\n auto [n, m] = lhs.size();\n\n Matrix res(lhs);\n for (int i = 0; i < n; i++) {\n for (int j = 0; j < m; j++) {\n res.data[i][j] -= rhs.data[i][j];\n res.data[i][j] = (res.data[i][j] + MOD) % MOD;\n }\n }\n\n return res;\n }\n\n friend Matrix operator*(const Matrix& lhs, const Matrix& rhs) {\n if (lhs.is_scalar() ^ rhs.is_scalar()) {\n if (lhs.is_scalar()) {\n return lhs.data.front().front() * rhs;\n } else {\n return rhs.data.front().front() * lhs;\n }\n }\n\n auto [n, m] = lhs.size();\n int l = rhs.data.front().size();\n\n Matrix res(n, l);\n for (int i = 0; i < n; i++) {\n for (int j = 0; j < m; j++) {\n for (int k = 0; k < l; k++) {\n res.data[i][k] += lhs.data[i][j] * rhs.data[j][k];\n res.data[i][k] %= MOD;\n }\n }\n }\n\n return res;\n }\n\n friend Matrix operator+(int value, const Matrix& rhs) {\n auto [n, m] = rhs.size();\n auto res(rhs);\n for (int i = 0; i < n; i++) {\n for (int j = 0; j < m; j++) {\n res.data[i][j] += value;\n res.data[i][j] %= MOD;\n }\n }\n return res;\n }\n\n friend Matrix operator-(int value, const Matrix& rhs) {\n auto [n, m] = rhs.size();\n auto res(rhs);\n for (int i = 0; i < n; i++) {\n for (int j = 0; j < m; j++) {\n res.data[i][j] -= value;\n res.data[i][j] = (res.data[i][j] + MOD) % MOD;\n }\n }\n return res;\n }\n\n friend Matrix operator*(int value, const Matrix& rhs) {\n auto [n, m] = rhs.size();\n auto res(rhs);\n for (int i = 0; i < n; i++) {\n for (int j = 0; j < m; j++) {\n res.data[i][j] *= value;\n res.data[i][j] %= MOD;\n }\n }\n return res;\n }\n\n Matrix operator-() {\n auto [n, m] = size();\n auto res(*this);\n for (int i = 0; i < n; i++) {\n for (int j = 0; j < m; j++) {\n res.data[i][j] = (MOD - res.data[i][j]) % MOD;\n }\n }\n return res;\n }\n\n Matrix transpose() {\n auto [n, m] = size();\n Matrix mat(m, n);\n for (int i = 0; i < n; i++) {\n for (int j = 0; j < m; j++) {\n mat.data[j][i] = data[i][j];\n }\n }\n return mat;\n }\n\n Matrix operator()(const Matrix& lhs, const Matrix& rhs) {\n auto [n, m] = size();\n auto [n1, m1] = lhs.size();\n auto [n2, m2] = rhs.size();\n Matrix mat(m1, m2);\n\n for (int i = 0; i < n1; i++) {\n for (int j = 0; j < m1; j++) {}\n }\n\n for (int i = 0; i < n2; i++) {\n for (int j = 0; j < m2; j++) {}\n }\n\n for (int i = 0; i < m1; i++) {\n for (int j = 0; j < m2; j++) {\n mat.data[i][j] = data[lhs.data[0][i] - 1][rhs.data[0][j] - 1];\n }\n }\n\n return mat;\n }\n\n Matrix add_row(const Matrix& mat) {\n auto [n1, m1] = size();\n auto [n2, m2] = mat.size();\n\n Matrix res(n1 + n2, m1);\n for (int i = 0; i < n1; i++) {\n for (int j = 0; j < m1; j++) {\n res.data[i][j] = data[i][j];\n }\n }\n\n for (int i = 0; i < n2; i++) {\n for (int j = 0; j < m1; j++) {\n res.data[i + n1][j] = mat.data[i][j];\n }\n }\n\n return res;\n }\n\n Matrix add_column(const Matrix& mat) {\n auto [n1, m1] = size();\n auto [n2, m2] = mat.size();\n Matrix res(n1, m1 + m2);\n for (int i = 0; i < n1; i++) {\n for (int j = 0; j < m1; j++) {\n res.data[i][j] = data[i][j];\n }\n }\n\n for (int i = 0; i < n2; i++) {\n for (int j = 0; j < m2; j++) {\n res.data[i][j + m1] = mat.data[i][j];\n }\n }\n\n return res;\n }\n};\n\nstruct Parser {\n using Iter = std::string::const_iterator&;\n std::vector<std::string> lines;\n std::string line;\n std::map<char, Matrix> database;\n\n void consume(Iter it, char c) {\n it++;\n }\n\n Parser(const std::vector<std::string>& s): lines(s) {}\n\n std::vector<Matrix> parse() {\n std::vector<Matrix> mats;\n for (int i = 0; i < (int)lines.size(); i++) {\n line = lines[i];\n auto it = line.cbegin();\n auto [var, mat] = assignment(it);\n\n mats.push_back(mat);\n database[var] = mat;\n }\n return mats;\n }\n\n std::pair<char, Matrix> assignment(Iter it) {\n char assignment_var = *it;\n consume(it, assignment_var);\n consume(it, '=');\n Matrix res = expr(it);\n consume(it, '.');\n return {assignment_var, res};\n }\n\n Matrix var(Iter it) {\n char c = *it;\n consume(it, c);\n return database[c];\n }\n\n Matrix expr(Iter it) {\n Matrix res = term(it);\n while (it != line.end() && (*it == '+' || *it == '-')) {\n if (*it == '+') {\n consume(it, '+');\n res = res + term(it);\n } else {\n consume(it, '-');\n res = res - term(it);\n }\n }\n return res;\n }\n\n Matrix term(Iter it) {\n Matrix res = factor(it);\n while (it != line.end() && *it == '*') {\n consume(it, '*');\n res = res * factor(it);\n }\n return res;\n }\n\n Matrix factor(Iter it) {\n bool inv = false;\n while (it != line.end() && *it == '-') {\n consume(it, '-');\n inv ^= true;\n }\n Matrix res = primary(it);\n if (inv) {\n res = -res;\n }\n return res;\n }\n\n Matrix primary(Iter it) {\n Matrix res;\n if (std::isdigit(*it)) {\n res = inum(it);\n } else if (std::isupper(*it)) {\n res = var(it);\n } else if (*it == '[') {\n res = matrix(it);\n } else if (*it == '(') {\n consume(it, '(');\n res = expr(it);\n consume(it, ')');\n }\n\n while (it != line.end() && (*it == '(' || *it == '\\'')) {\n if (*it == '(') {\n res = indexed_primary(it, res);\n } else if (*it == '\\'') {\n res = transposed_primary(it, res);\n }\n }\n\n return res;\n }\n\n Matrix indexed_primary(Iter it, Matrix mat) {\n consume(it, '(');\n Matrix lhs = expr(it);\n consume(it, ',');\n Matrix rhs = expr(it);\n consume(it, ')');\n\n return mat(lhs, rhs);\n }\n\n Matrix transposed_primary(Iter it, Matrix mat) {\n consume(it, '\\'');\n return mat.transpose();\n }\n\n Matrix matrix(Iter it) {\n consume(it, '[');\n Matrix res = row_seq(it);\n consume(it, ']');\n return res;\n }\n\n Matrix row_seq(Iter it) {\n Matrix res = row(it);\n while (it != line.end() && *it == ';') {\n consume(it, ';');\n res = res.add_row(row(it));\n }\n return res;\n }\n\n Matrix row(Iter it) {\n Matrix res = expr(it);\n while (it != line.end() && *it == ' ') {\n consume(it, ' ');\n res = res.add_column(expr(it));\n }\n return res;\n }\n\n Matrix inum(Iter it) {\n int res = digit(it);\n while (it != line.end() && std::isdigit(*it)) {\n res = 10 * res + digit(it);\n }\n return Matrix(res);\n }\n\n int digit(Iter it) {\n char res = *it;\n consume(it, res);\n return res - '0';\n }\n};\n\nint main() {\n int n;\n while (std::cin >> n) {\n if (n == 0) break;\n std::cin.ignore();\n std::vector<std::string> lines(n);\n for (int i = 0; i < n; i++) {\n std::getline(std::cin, lines[i]);\n }\n Parser parser(lines);\n auto mats = parser.parse();\n for (auto mat: mats) {\n std::cout << mat << std::endl;\n }\n std::cout << \"-----\" << std::endl;\n }\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3664, "score_of_the_acc": -0.118, "final_rank": 6 }, { "submission_id": "aoj_1314_8033955", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\ntypedef long long ll;\ntypedef __int128_t lll;\ntypedef long double ld;\ntypedef pair<ll, ll> pl;\ntypedef tuple<ll, ll, ll> tt;\ntypedef vector<ll> vl;\ntypedef vector<vl> vvl;\ntypedef vector<vvl> vvvl;\ntypedef vector<double> vd;\ntypedef vector<vd> vvd;\ntypedef vector<vvd> vvvd;\ntypedef vector<bool> vb;\ntypedef vector<vb> vvb;\ntypedef vector<vvb> vvvb;\ntypedef vector<char> vc;\ntypedef vector<vc> vvc;\ntypedef vector<vvc> vvvc;\ntypedef vector<pl> vp;\ntypedef vector<tt> vt;\ntypedef vector<string> vs;\n\nconst ll INF = 1000000010;\nconst ll INFL = INF * INF;\nconst ld epsilon = 1e-10;\n\n#define ovl(a, b, c, d, e, ...) e\n#define pb push_back\n#define eb emplace_back\n#define MP make_pair\n#define DEBUG(...) DEBUG_(#__VA_ARGS__, __VA_ARGS__)\n#define REP1(i, r) for (int i = 0; i < (r); i++)\n#define REP2(i, l, r) for (int i = (l); i < (r); i++)\n#define REP3(i, l, r, d) for (int i = (l); i < (r); i += (d))\n#define REP(...) ovl(__VA_ARGS__, REP3, REP2, REP1)(__VA_ARGS__)\n#define RREP1(i, r) for (int i = (r)-1; i >= 0; i--)\n#define RREP2(i, l, r) for (int i = (r)-1; i >= (l); i--)\n#define RREP3(i, l, r, d) for (int i = (r)-1; i >= (l); i -= (d))\n#define RREP(...) ovl(__VA_ARGS__, RREP3, RREP2, RREP1)(__VA_ARGS__)\n#define EACH1(x, a) for (auto &x : a)\n#define EACH2(x, y, a) for (auto &[x, y] : a)\n#define EACH3(x, y, z, a) for (auto &[x, y, z] : a)\n#define EACH(...) ovl(__VA_ARGS__, EACH3, EACH2, EACH1)(__VA_ARGS__)\n#define ALL(x) begin(x), end(x)\n#define RALL(x) (x).rbegin(), (x).rend()\n#define sz(x) (ll) x.size()\n#define LB(a, x) (lower_bound(ALL(a), x) - a.begin())\n#define UB(a, x) (upper_bound(ALL(a), x) - a.begin())\n#define FLG(x, i) (((x) >> (i)) & 1)\n#define CNTBIT(x) __builtin_popcountll(x)\n#define IN(x, a, b) ((a) <= (x) && (x) < (b))\n#define int ll\n\ntemplate <class T>\nvoid DEBUG_(string_view name, const T &a) {\n cerr << name << \": \" << a << endl;\n}\n\ntemplate <class T>\nvoid DEBUG_(string_view name, const vector<T> &a) {\n cerr << name << \": \";\n REP(i, sz(a)) cerr << a[i] << \" \";\n cerr << endl;\n}\n\ntemplate <class T, class U>\nbool chmax(T &x, const U &y) {\n return x < y ? x = y, 1 : 0;\n}\ntemplate <class T, class U>\nbool chmin(T &x, const U &y) {\n return x > y ? x = y, 1 : 0;\n}\n\n//-----------------------------------------------------------------------\n\ntypedef string::const_iterator State;\ntypedef vector<vector<int>> Mat;\n\nconstexpr int MOD = 1 << 15;\n\nMat matadd(const Mat &m1, const Mat &m2) {\n assert(m1.size() == m2.size() && m1[0].size() == m2[0].size());\n Mat m3 = m1;\n REP(h, (int)m1.size())\n REP(w, (int)m1[0].size()) {\n m3[h][w] += m2[h][w];\n m3[h][w] %= MOD;\n }\n return m3;\n}\n\nMat matsub(const Mat &m1, const Mat &m2) {\n assert(m1.size() == m2.size() && m1[0].size() == m2[0].size());\n Mat m3 = m1;\n REP(h, (int)m1.size())\n REP(w, (int)m1[0].size()) {\n m3[h][w] += MOD - m2[h][w] % MOD;\n m3[h][w] %= MOD;\n }\n return m3;\n}\n\nMat matmul(const Mat &m1, const Mat &m2) {\n if (int(m1.size()) == 1 && int(m1[0].size()) == 1) {\n Mat m3 = m2;\n REP(h, int(m3.size()))\n REP(w, int(m3[0].size())) {\n m3[h][w] *= m1[0][0];\n m3[h][w] %= MOD;\n }\n return m3;\n } else if (int(m2.size()) == 1 && int(m2[0].size()) == 1) {\n Mat m3 = m1;\n REP(h, int(m3.size()))\n REP(w, int(m3[0].size())) {\n m3[h][w] *= m2[0][0];\n m3[h][w] %= MOD;\n }\n return m3;\n } else {\n assert(m1[0].size() == m2.size());\n Mat m3(m1.size(), vl(m2[0].size(), 0));\n REP(i, (int)m1.size())\n REP(j, (int)m2[0].size())\n REP(k, (int)m1[0].size()) {\n m3[i][j] += m1[i][k] * m2[k][j] % MOD;\n m3[i][j] %= MOD;\n }\n return m3;\n }\n}\n\nMat unary_minus(const Mat &m) {\n Mat m3 = m;\n REP(h, (int)m.size())\n REP(w, (int)m[0].size()) { m3[h][w] = MOD - m[h][w]; }\n return m3;\n}\n\nMat mattranspose(const Mat &m) {\n Mat m3(m[0].size(), vl(m.size()));\n REP(h, (int)m.size())\n REP(w, (int)m[0].size()) { m3[w][h] = m[h][w]; }\n return m3;\n}\n\nMat matindex(const Mat &p, const Mat &m1, const Mat &m2) {\n assert((int)m1.size() == 1 && (int)m2.size() == 1);\n Mat m3(m1[0].size(), vl(m2[0].size()));\n REP(h, (int)m1[0].size())\n REP(w, (int)m2[0].size()) { m3[h][w] = p[m1[0][h] - 1][m2[0][w] - 1]; }\n return m3;\n}\n\nmap<string, Mat> store;\n\nMat assignment(State &bein);\nstring var(State &begin);\nMat expr(State &begin);\nMat term(State &begin);\nMat factor(State &begin);\nMat primary(State &begin);\nMat matrix(State &begin);\n\nMat assignment(State &begin) {\n string X = var(begin);\n assert(*begin == '=');\n begin++;\n Mat mat = expr(begin);\n store[X] = mat;\n return mat;\n}\n\nstring var(State &begin) {\n assert('A' <= *begin && *begin <= 'Z');\n string ret(1, *begin);\n begin++;\n return ret;\n}\n\nMat expr(State &begin) {\n Mat mat = term(begin);\n while (*begin != '.') {\n if (*begin == '+') {\n begin++;\n Mat mat2 = term(begin);\n mat = matadd(mat, mat2);\n } else if (*begin == '-') {\n begin++;\n Mat mat2 = term(begin);\n mat = matsub(mat, mat2);\n } else {\n break;\n }\n }\n return mat;\n}\n\nMat term(State &begin) {\n Mat mat = factor(begin);\n while (*begin == '*') {\n begin++;\n Mat mat2 = factor(begin);\n mat = matmul(mat, mat2);\n }\n return mat;\n}\n\nMat factor(State &begin) {\n if (*begin == '-') {\n begin++;\n return unary_minus(factor(begin));\n } else {\n return primary(begin);\n }\n}\n\nint inum(State &begin) {\n int ret = 0;\n while (isdigit(*begin)) {\n ret *= 10;\n ret %= MOD;\n ret += *begin - '0';\n ret %= MOD;\n begin++;\n }\n // DEBUG(*begin);\n return ret;\n}\n\nMat row(State &begin) {\n // DEBUG(\"row\");\n Mat mat = expr(begin);\n // DEBUG(*begin);\n while (*begin == ' ') {\n begin++;\n Mat mat2 = expr(begin);\n // DEBUG(mat2);\n assert(mat.size() == mat2.size());\n REP(h, int(mat.size())) {\n EACH(e, mat2[h]) { mat[h].pb(e); }\n }\n }\n return mat;\n}\n\nMat rowseq(State &begin) {\n Mat mat = row(begin);\n while (*begin == ';') {\n begin++;\n Mat mat2 = row(begin);\n EACH(r, mat2) { mat.pb(r); }\n }\n return mat;\n}\n\nMat primary_sub(State &begin) {\n if (isdigit(*begin)) {\n return Mat(1, vl(1, inum(begin)));\n }\n if ('A' <= *begin && *begin <= 'Z') {\n Mat ret = store[string(1, *begin)];\n begin++;\n return ret;\n }\n if (*begin == '[') {\n begin++;\n Mat mat = rowseq(begin);\n assert(*begin == ']');\n begin++;\n return mat;\n }\n if (*begin == '(') {\n begin++;\n Mat mat = expr(begin);\n assert(*begin == ')');\n begin++;\n return mat;\n }\n assert(false);\n}\n\nMat primary(State &begin) {\n Mat p = primary_sub(begin);\n while (*begin == '(' || *begin == '\\'') {\n if (*begin == '(') {\n begin++;\n Mat mat1 = expr(begin);\n assert(*begin == ',');\n begin++;\n Mat mat2 = expr(begin);\n assert(*begin == ')');\n begin++;\n p = matindex(p, mat1, mat2);\n } else {\n begin++;\n p = mattranspose(p);\n }\n }\n return p;\n}\n\nvector<Mat> solve(int N) {\n store.clear();\n vector<Mat> mats;\n REP(i, N) {\n string S;\n getline(cin, S);\n // DEBUG(S);\n State begin = S.begin();\n Mat mat = assignment(begin);\n // DEBUG(\"mat\");\n // EACH(m, mat) { DEBUG(m); }\n mats.pb(mat);\n }\n return mats;\n}\n\nsigned main() {\n ios::sync_with_stdio(false);\n cin.tie(0);\n cout << fixed << setprecision(15);\n\n vector<vector<Mat>> answers;\n while (true) {\n string n;\n getline(cin, n);\n State begin = n.begin();\n int N = inum(begin);\n if (N == 0) break;\n // DEBUG(N);\n vector<Mat> answer = solve(N);\n answers.pb(answer);\n }\n\n EACH(as, answers) {\n EACH(a, as) {\n REP(h, (int)a.size()) {\n REP(w, (int)a[h].size()) {\n cout << a[h][w] << (w == int(a[h].size()) - 1 ? \"\" : \" \");\n }\n cout << endl;\n }\n }\n cout << \"-----\" << endl;\n }\n\n return 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 4312, "score_of_the_acc": -0.2182, "final_rank": 19 }, { "submission_id": "aoj_1314_8019671", "code_snippet": "#pragma region ngng628_library\n\n#include <cassert>\n#include <numeric>\n#include <type_traits>\n#include <utility>\n\nnamespace atcoder {\n\nnamespace internal {\n\nconstexpr long long safe_mod(long long x, long long m) {\n x %= m;\n if (x < 0) x += m;\n return x;\n}\n\nstruct barrett {\n unsigned int _m;\n unsigned long long im;\n\n explicit barrett(unsigned int m) : _m(m), im((unsigned long long)(-1) / m + 1) {}\n\n unsigned int umod() const { return _m; }\n\n unsigned int mul(unsigned int a, unsigned int b) const {\n\n unsigned long long z = a;\n z *= b;\n unsigned long long x =\n (unsigned long long)(((unsigned __int128)(z)*im) >> 64);\n unsigned int v = (unsigned int)(z - x * _m);\n if (_m <= v) v += _m;\n return v;\n }\n};\n\nconstexpr long long pow_mod_constexpr(long long x, long long n, int m) {\n if (m == 1) return 0;\n unsigned int _m = (unsigned int)(m);\n unsigned long long r = 1;\n unsigned long long y = safe_mod(x, m);\n while (n) {\n if (n & 1) r = (r * y) % _m;\n y = (y * y) % _m;\n n >>= 1;\n }\n return r;\n}\n\nconstexpr bool is_prime_constexpr(int n) {\n if (n <= 1) return false;\n if (n == 2 || n == 7 || n == 61) return true;\n if (n % 2 == 0) return false;\n long long d = n - 1;\n while (d % 2 == 0) d /= 2;\n constexpr long long bases[3] = {2, 7, 61};\n for (long long a : bases) {\n long long t = d;\n long long y = pow_mod_constexpr(a, t, n);\n while (t != n - 1 && y != 1 && y != n - 1) {\n y = y * y % n;\n t <<= 1;\n }\n if (y != n - 1 && t % 2 == 0) {\n return false;\n }\n }\n return true;\n}\ntemplate <int n> constexpr bool is_prime = is_prime_constexpr(n);\n\nconstexpr std::pair<long long, long long> inv_gcd(long long a, long long b) {\n a = safe_mod(a, b);\n if (a == 0) return {b, 0};\n\n long long s = b, t = a;\n long long m0 = 0, m1 = 1;\n\n while (t) {\n long long u = s / t;\n s -= t * u;\n m0 -= m1 * u; // |m1 * u| <= |m1| * s <= b\n\n\n auto tmp = s;\n s = t;\n t = tmp;\n tmp = m0;\n m0 = m1;\n m1 = tmp;\n }\n if (m0 < 0) m0 += b / s;\n return {s, m0};\n}\n\nconstexpr int primitive_root_constexpr(int m) {\n if (m == 2) return 1;\n if (m == 167772161) return 3;\n if (m == 469762049) return 3;\n if (m == 754974721) return 11;\n if (m == 998244353) return 3;\n int divs[20] = {};\n divs[0] = 2;\n int cnt = 1;\n int x = (m - 1) / 2;\n while (x % 2 == 0) x /= 2;\n for (int i = 3; (long long)(i)*i <= x; i += 2) {\n if (x % i == 0) {\n divs[cnt++] = i;\n while (x % i == 0) {\n x /= i;\n }\n }\n }\n if (x > 1) {\n divs[cnt++] = x;\n }\n for (int g = 2;; g++) {\n bool ok = true;\n for (int i = 0; i < cnt; i++) {\n if (pow_mod_constexpr(g, (m - 1) / divs[i], m) == 1) {\n ok = false;\n break;\n }\n }\n if (ok) return g;\n }\n}\ntemplate <int m> constexpr int primitive_root = primitive_root_constexpr(m);\n\nunsigned long long floor_sum_unsigned(unsigned long long n,\n unsigned long long m,\n unsigned long long a,\n unsigned long long b) {\n unsigned long long ans = 0;\n while (true) {\n if (a >= m) {\n ans += n * (n - 1) / 2 * (a / m);\n a %= m;\n }\n if (b >= m) {\n ans += n * (b / m);\n b %= m;\n }\n\n unsigned long long y_max = a * n + b;\n if (y_max < m) break;\n n = (unsigned long long)(y_max / m);\n b = (unsigned long long)(y_max % m);\n std::swap(m, a);\n }\n return ans;\n}\n\n} // namespace internal\n\n} // namespace atcoder\n\n\n#include <cassert>\n#include <numeric>\n#include <type_traits>\n\nnamespace atcoder {\n\nnamespace internal {\n\ntemplate <class T>\nusing is_signed_int128 =\n typename std::conditional<std::is_same<T, __int128_t>::value ||\n std::is_same<T, __int128>::value,\n std::true_type,\n std::false_type>::type;\n\ntemplate <class T>\nusing is_unsigned_int128 =\n typename std::conditional<std::is_same<T, __uint128_t>::value ||\n std::is_same<T, unsigned __int128>::value,\n std::true_type,\n std::false_type>::type;\n\ntemplate <class T>\nusing make_unsigned_int128 =\n typename std::conditional<std::is_same<T, __int128_t>::value,\n __uint128_t,\n unsigned __int128>;\n\ntemplate <class T>\nusing is_integral = typename std::conditional<std::is_integral<T>::value ||\n is_signed_int128<T>::value ||\n is_unsigned_int128<T>::value,\n std::true_type,\n std::false_type>::type;\n\ntemplate <class T>\nusing is_signed_int = typename std::conditional<(is_integral<T>::value &&\n std::is_signed<T>::value) ||\n is_signed_int128<T>::value,\n std::true_type,\n std::false_type>::type;\n\ntemplate <class T>\nusing is_unsigned_int =\n typename std::conditional<(is_integral<T>::value &&\n std::is_unsigned<T>::value) ||\n is_unsigned_int128<T>::value,\n std::true_type,\n std::false_type>::type;\n\ntemplate <class T>\nusing to_unsigned = typename std::conditional<\n is_signed_int128<T>::value,\n make_unsigned_int128<T>,\n typename std::conditional<std::is_signed<T>::value,\n std::make_unsigned<T>,\n std::common_type<T>>::type>::type;\n\ntemplate <class T>\nusing is_signed_int_t = std::enable_if_t<is_signed_int<T>::value>;\n\ntemplate <class T>\nusing is_unsigned_int_t = std::enable_if_t<is_unsigned_int<T>::value>;\n\ntemplate <class T> using to_unsigned_t = typename to_unsigned<T>::type;\n\n} // namespace internal\n\n} // namespace atcoder\n\n\nnamespace atcoder {\n\nnamespace internal {\n\nstruct modint_base {};\nstruct static_modint_base : modint_base {};\n\ntemplate <class T> using is_modint = std::is_base_of<modint_base, T>;\ntemplate <class T> using is_modint_t = std::enable_if_t<is_modint<T>::value>;\n\n} // namespace internal\n\ntemplate <int m, std::enable_if_t<(1 <= m)>* = nullptr>\nstruct static_modint : internal::static_modint_base {\n using mint = static_modint;\n\n public:\n static constexpr int mod() { return m; }\n static mint raw(int v) {\n mint x;\n x._v = v;\n return x;\n }\n\n static_modint() : _v(0) {}\n template <class T, internal::is_signed_int_t<T>* = nullptr>\n static_modint(T v) {\n long long x = (long long)(v % (long long)(umod()));\n if (x < 0) x += umod();\n _v = (unsigned int)(x);\n }\n template <class T, internal::is_unsigned_int_t<T>* = nullptr>\n static_modint(T v) {\n _v = (unsigned int)(v % umod());\n }\n\n unsigned int val() const { return _v; }\n\n mint& operator++() {\n _v++;\n if (_v == umod()) _v = 0;\n return *this;\n }\n mint& operator--() {\n if (_v == 0) _v = umod();\n _v--;\n return *this;\n }\n mint operator++(int) {\n mint result = *this;\n ++*this;\n return result;\n }\n mint operator--(int) {\n mint result = *this;\n --*this;\n return result;\n }\n\n mint& operator+=(const mint& rhs) {\n _v += rhs._v;\n if (_v >= umod()) _v -= umod();\n return *this;\n }\n mint& operator-=(const mint& rhs) {\n _v -= rhs._v;\n if (_v >= umod()) _v += umod();\n return *this;\n }\n mint& operator*=(const mint& rhs) {\n unsigned long long z = _v;\n z *= rhs._v;\n _v = (unsigned int)(z % umod());\n return *this;\n }\n mint& operator/=(const mint& rhs) { return *this = *this * rhs.inv(); }\n\n mint operator+() const { return *this; }\n mint operator-() const { return mint() - *this; }\n\n mint pow(long long n) const {\n assert(0 <= n);\n mint x = *this, r = 1;\n while (n) {\n if (n & 1) r *= x;\n x *= x;\n n >>= 1;\n }\n return r;\n }\n mint inv() const {\n if (prime) {\n assert(_v);\n return pow(umod() - 2);\n } else {\n auto eg = internal::inv_gcd(_v, m);\n assert(eg.first == 1);\n return eg.second;\n }\n }\n\n friend mint operator+(const mint& lhs, const mint& rhs) {\n return mint(lhs) += rhs;\n }\n friend mint operator-(const mint& lhs, const mint& rhs) {\n return mint(lhs) -= rhs;\n }\n friend mint operator*(const mint& lhs, const mint& rhs) {\n return mint(lhs) *= rhs;\n }\n friend mint operator/(const mint& lhs, const mint& rhs) {\n return mint(lhs) /= rhs;\n }\n friend bool operator==(const mint& lhs, const mint& rhs) {\n return lhs._v == rhs._v;\n }\n friend bool operator!=(const mint& lhs, const mint& rhs) {\n return lhs._v != rhs._v;\n }\n\n private:\n unsigned int _v;\n static constexpr unsigned int umod() { return m; }\n static constexpr bool prime = internal::is_prime<m>;\n};\n\ntemplate <int id> struct dynamic_modint : internal::modint_base {\n using mint = dynamic_modint;\n\n public:\n static int mod() { return (int)(bt.umod()); }\n static void set_mod(int m) {\n assert(1 <= m);\n bt = internal::barrett(m);\n }\n static mint raw(int v) {\n mint x;\n x._v = v;\n return x;\n }\n\n dynamic_modint() : _v(0) {}\n template <class T, internal::is_signed_int_t<T>* = nullptr>\n dynamic_modint(T v) {\n long long x = (long long)(v % (long long)(mod()));\n if (x < 0) x += mod();\n _v = (unsigned int)(x);\n }\n template <class T, internal::is_unsigned_int_t<T>* = nullptr>\n dynamic_modint(T v) {\n _v = (unsigned int)(v % mod());\n }\n\n unsigned int val() const { return _v; }\n\n mint& operator++() {\n _v++;\n if (_v == umod()) _v = 0;\n return *this;\n }\n mint& operator--() {\n if (_v == 0) _v = umod();\n _v--;\n return *this;\n }\n mint operator++(int) {\n mint result = *this;\n ++*this;\n return result;\n }\n mint operator--(int) {\n mint result = *this;\n --*this;\n return result;\n }\n\n mint& operator+=(const mint& rhs) {\n _v += rhs._v;\n if (_v >= umod()) _v -= umod();\n return *this;\n }\n mint& operator-=(const mint& rhs) {\n _v += mod() - rhs._v;\n if (_v >= umod()) _v -= umod();\n return *this;\n }\n mint& operator*=(const mint& rhs) {\n _v = bt.mul(_v, rhs._v);\n return *this;\n }\n mint& operator/=(const mint& rhs) { return *this = *this * rhs.inv(); }\n\n mint operator+() const { return *this; }\n mint operator-() const { return mint() - *this; }\n\n mint pow(long long n) const {\n assert(0 <= n);\n mint x = *this, r = 1;\n while (n) {\n if (n & 1) r *= x;\n x *= x;\n n >>= 1;\n }\n return r;\n }\n mint inv() const {\n auto eg = internal::inv_gcd(_v, mod());\n assert(eg.first == 1);\n return eg.second;\n }\n\n friend mint operator+(const mint& lhs, const mint& rhs) {\n return mint(lhs) += rhs;\n }\n friend mint operator-(const mint& lhs, const mint& rhs) {\n return mint(lhs) -= rhs;\n }\n friend mint operator*(const mint& lhs, const mint& rhs) {\n return mint(lhs) *= rhs;\n }\n friend mint operator/(const mint& lhs, const mint& rhs) {\n return mint(lhs) /= rhs;\n }\n friend bool operator==(const mint& lhs, const mint& rhs) {\n return lhs._v == rhs._v;\n }\n friend bool operator!=(const mint& lhs, const mint& rhs) {\n return lhs._v != rhs._v;\n }\n\n private:\n unsigned int _v;\n static internal::barrett bt;\n static unsigned int umod() { return bt.umod(); }\n};\ntemplate <int id> internal::barrett dynamic_modint<id>::bt(998244353);\n\nusing modint998244353 = static_modint<998244353>;\nusing modint1000000007 = static_modint<1000000007>;\nusing modint = dynamic_modint<-1>;\n\nnamespace internal {\n\ntemplate <class T>\nusing is_static_modint = std::is_base_of<internal::static_modint_base, T>;\n\ntemplate <class T>\nusing is_static_modint_t = std::enable_if_t<is_static_modint<T>::value>;\n\ntemplate <class> struct is_dynamic_modint : public std::false_type {};\ntemplate <int id>\nstruct is_dynamic_modint<dynamic_modint<id>> : public std::true_type {};\n\ntemplate <class T>\nusing is_dynamic_modint_t = std::enable_if_t<is_dynamic_modint<T>::value>;\n\n} // namespace internal\n\n} // namespace atcoder\n\nusing mint = atcoder::static_modint<32768>;\n#include <bits/stdc++.h>\n#define int Int\n#define float Float\n#define overload3(_1,_2,_3,name,...) name\n#define _step(n) _rep(_,n)\n#define _rep(i,n) _repr(i,0,n)\n#define _repr(i,b,e) for(int i=(b), i##_len=(e); i<i##_len; ++i)\n#define rep(...) overload3(__VA_ARGS__, _repr, _rep, _step)(__VA_ARGS__)\n#define _reps(i,n) _reprs(i,1,n)\n#define _reprs(i,b,e) for(int i=(b), i##_len=(e); i<=i##_len; ++i)\n#define reps(...) overload3(__VA_ARGS__, _reprs, _reps)(__VA_ARGS__)\n#define rrep(i,n) for(int i=(int)(n)-1; i>=0; --i)\n#define rreps(i,n) for(int i=(n); i>0; --i)\n#define all(v) std::begin(v), std::end(v)\n#define rall(v) std::rbegin(v), std::rend(v)\n#define pb push_back\n#define eb emplace_back\n#define len(v) ssize(std::size(v))\n#define eprintf(...) fprintf(stderr, __VA_ARGS__)\nusing namespace std;\nusing Int = long long;\nusing Float = long double;\nusing int32 = int32_t;\nusing int64 = int64_t;\nusing uint32 = uint32_t;\nusing uint64 = uint64_t;\nusing usize = size_t;\nusing ssize = ptrdiff_t;\ntemplate<class T> using vec = vector<T>;\ntemplate<class T> using MaxHeap = priority_queue<T>;\ntemplate<class T> using MinHeap = priority_queue<T, vec<T>, greater<T>>;\nusing pii = pair<int, int>;\nusing vi = vec<int>;\nusing vvi = vec<vi>;\nusing vvvi = vec<vvi>;\nusing vb = basic_string<bool>;\nusing vvb = vec<vb>;\nusing vvvb = vec<vvb>;\nconstexpr int OO = (1LL<<62)-(1LL<<31);\ntemplate<class T> string join(const vec<T>& v){ stringstream s; for (T t : v) s << ' ' << t; return s.str().substr(1); }\ntemplate<class T> ostream& operator <<(ostream& os, const vec<T>& v){ if (len(v)) os << join(v); return os; }\ntemplate<class T> ostream& operator <<(ostream& os, const vec<vec<T>>& v){\n rep (i, len(v)) if (len(v[i])) os << join(v[i]) << (i-len(v)+1 ? \"\\n\" : \"\");\n return os;\n}\ntemplate<class T, class U> ostream& operator <<(ostream& os, const pair<T, U>& p){ return os << p.first << ' ' << p.second; }\ntemplate<class T, class U, class V>\nostream& operator <<(ostream& os, const tuple<T, U, V>& t){ return os << get<0>(t) << \" \" << get<1>(t) << \" \" << get<2>(t); }\nstruct Scanner {\n Scanner() = default;\n int nextInt(int o = 0) const {\n char c = skip();\n int r = c - '0', sgn = 1;\n if (c == '-') sgn = -1, r = gc() & 0xf;\n else if (c == '+') r = gc() & 0xf;\n while (!isspace(c = gc())) r = 10 * r + (c & 0xf);\n return sgn * r + o;\n }\n string nextLine() const { char c; string r; while ((c = gc()) != '\\n') r.pb(c); return r; }\nprivate:\n char skip() const { char c; while (isspace(c = getchar())) {} return c; }\n inline char gc() const { return getchar(); }\n} sc;\nistream& operator >>(istream& is, mint& r){ int t; is >> t; r = t; return is; }\nostream& operator <<(ostream& os, const mint& r){ return os << r.val(); }\nmint operator\"\" _mod_m(unsigned long long n) { return n; }\nusing vm = vec<mint>;\nusing vvm = vec<vm>;\nusing vvvm = vec<vvm>;\n# pragma endregion ngng628_library\n\ntemplate <class T>\nstruct Matrix {\n // コンストラクタ\n Matrix() : m(vec<vec<T>>()) {}\n Matrix(int n) : m(vec<vec<T>>(n, vec<T>(n))) {}\n Matrix(int h, int w) : m(vec<vec<T>>(h, vec<T>(w, 0))) {}\n Matrix(int h, int w, T d) : m(vec<vec<T>>(h, vec<T>(w, d))){}\n Matrix(const vec<vec<T>>& _m) : m(_m){}\n\n // 単位行列\n const static Matrix Identity(const int n) {\n Matrix mat(n);\n rep (i, n) mat[i][i] = T(1);\n return mat;\n }\n\n // 要素アクセス\n vec<T> operator[](const int i) const { return m[i]; }\n vec<T>& operator[](const int i) { return m[i]; }\n T operator ()(const int i, const int j) const { return m[i][j]; }\n T& operator ()(const int i, const int j) { return m[i][j]; }\n\n // ゲッター\n [[nodiscard]] constexpr int n_rows() const { return len(m); }\n [[nodiscard]] constexpr int n_cols() const { return len(m[0]); }\n\n // 行・列の拡張\n void resize(const int h, const int w) { m.resize(h, w, T()); }\n void push_back_row(const vec<T>& v) { m.push_back(v); }\n void push_back_col(const vec<T>& v) { rep (i, n_rows()) m[i].push_back(v[i]); }\n void pop_back_row() { m.pop_back(); }\n void pop_back_col() { rep (i, n_rows()) m[i].pop_back(); }\n\n // 四則演算とべき乗\n Matrix& operator +=(const Matrix& other) {\n const int r = n_rows();\n const int c = n_cols();\n assert(r == other.n_rows() and c == other.n_cols());\n rep (i, r) rep (j, c) (*this)(i, j) += other(i, j);\n return *this;\n }\n Matrix& operator -=(const Matrix& other) {\n const int r = n_rows();\n const int c = n_cols();\n assert(r == other.n_rows() and c == other.n_cols());\n rep (i, r) rep (j, c) (*this)(i, j) -= other(i, j);\n return *this;\n }\n Matrix& operator *=(const Matrix& other) {\n const int r = n_rows(), c = other.n_cols(), p = n_cols();\n assert(p == other.n_rows());\n vec<vec<T>> v(r, vec<T>(c, T(0)));\n rep (i, r) rep (j, c) rep (k, p) v[i][j] = (v[i][j] + (*this)(i, k) * other(k, j));\n m.swap(v);\n return *this;\n }\n Matrix& operator ^=(int n) {\n Matrix res = Matrix::Identity(n_rows());\n Matrix x = *this;\n while (n > 0) {\n if (n & 1) res *= x;\n x *= x;\n n >>= 1;\n }\n swap(m, res.m);\n return *this;\n }\n\n friend Matrix operator +(const Matrix& a, const Matrix& b) { return Matrix(a) += b; }\n friend Matrix operator -(const Matrix& a, const Matrix& b) { return Matrix(a) -= b; }\n friend Matrix operator *(const Matrix& a, const Matrix& b) { return Matrix(a) *= b; }\n friend Matrix operator ^(const Matrix& a, const int n) { return Matrix(a) ^= n; }\n\n // スカラー倍\n Matrix& operator *=(T k) { rep (i, n_rows()) rep (j, n_cols()) (*this)(i, j) *= k; return *this; }\n Matrix& operator /=(T k) { rep (i, n_rows()) rep (j, n_cols()) (*this)(i, j) /= k; return *this; }\n friend Matrix operator *(const Matrix& a, const T b) { return Matrix(a) *= b; }\n friend Matrix operator *(const T a, const Matrix& b) { return Matrix(b) *= a; }\n friend Matrix operator /(const Matrix& a, const T b) { return Matrix(a) /= b; }\n\n // 単項演算子\n Matrix operator -() const { Matrix mat(n_rows(), n_cols()); rep (i, n_rows()) rep (j, n_cols()) mat(i, j) = -this->m[i][j]; return mat; }\n Matrix operator +() const { return *this; }\n\n // 累乗\n Matrix pow(int n) { return (*this) ^ n; }\n friend Matrix pow(const Matrix& mat, int n) { return mat.pow(n); }\n\n // 行列式 (T型が体であるときのみ対応)\n T det() {\n assert(n_rows() == n_cols());\n Matrix mat(*this);\n T ret = 1;\n rep (i, n_cols()) {\n int idx = -1;\n rep (j, i, n_cols()) {\n if (mat(j, i) != T(0)) idx = j;\n }\n if (idx == -1) return 0;\n if (i != idx) {\n ret *= -1;\n swap(mat[i], mat[idx]);\n }\n ret *= mat(i, i);\n T v = mat(i, i);\n rep (j, n_cols()) mat(i, j) /= v;\n rep (j, i + 1, n_cols()) {\n T a = mat(j, i);\n rep (k, n_cols()) mat(j, k) -= mat(i, k) * a;\n }\n }\n return ret;\n }\n friend T det(const Matrix& mat) { return mat.det(); }\n\n // 転置行列\n Matrix transposed() const {\n Matrix mat(n_cols(), n_rows());\n rep (i, n_rows()) {\n rep (j, n_cols()) {\n mat(j, i) = m[i][j];\n }\n }\n return mat;\n }\n friend T transposed(const Matrix& mat) { return mat.transposed(); }\n\n // 行のスライス\n Matrix selected_rows(const vector<int>& indices) {\n vector<vector<T>> acc;\n for (int i : indices) {\n acc.push_back(m[i]);\n }\n return Matrix(acc);\n }\n\n // 列のスライス\n Matrix selected_cols(const vector<int>& indices) {\n vector<vector<T>> acc;\n Matrix mt = transposed();\n for (int j : indices) {\n acc.push_back(mt[j]);\n }\n return Matrix(acc).transposed();\n }\n\n // 行列を横に合体させる\n //\n // ```\n // [1, 2; 3, 4].row_concat([5, 6, 7; 8, 9, 10]) # => [1, 2, 5, 6; 3, 4, 8, 9, 10]\n // ```\n Matrix row_concat(const Matrix& other) {\n assert(n_rows() == other.n_rows());\n Matrix ret(n_rows(), n_cols() + other.n_cols());\n const int w = n_cols();\n rep (i, n_rows()) {\n rep (j, w) ret(i, j) = m[i][j];\n rep (j, other.n_cols()) ret(i, w + j) = other[i][j];\n }\n return ret;\n }\n\n // 行列を縦に合体させる\n //\n // ```\n // [1, 2; 3, 4].row_concat([5, 6; 7, 8; 9, 10]) # => [1, 2; 3, 4; 5, 6; 7, 8; 9, 10]\n // ```\n Matrix col_concat(const Matrix& other) {\n assert(n_cols() == other.n_cols());\n Matrix ret(n_rows() + other.n_rows(), n_cols());\n const int h = n_rows();\n rep (i, h) {\n rep (j, n_cols()) ret(i, j) = m[i][j];\n }\n rep (i, other.n_rows()) {\n rep (j, other.n_cols()) ret(h + i, j) = other[i][j];\n }\n return ret;\n }\n\n // 入出力\n friend ostream& operator <<(ostream& os, const Matrix& mat){\n rep (i, mat.n_rows()) {\n rep (j, mat.n_cols()) {\n if (j != 0) os << ' ';\n os << mat(i, j);\n }\n if (i != mat.n_rows() - 1) os << '\\n';\n }\n return os;\n }\n\nprivate:\n vec<vec<T>> m;\n};\n\nusing M = Matrix<mint>;\n\nstruct Parser {\n map<char, M> mp;\n vector<string> s;\n int line;\n string::const_iterator it;\n\n Parser() = default;\n\n Parser(const vector<string>& _s) : s(_s) {\n }\n\n void parse() {\n const int n = len(s);\n rep (i, 0, n) {\n line = i;\n it = s[i].begin();\n program();\n }\n }\n\n void program() {\n char var = *it++;\n assert(*it++ == '=');\n\n M res = expr();\n assert(*it++ == '.');\n\n mp[var] = res;\n cout << res << endl;\n }\n\n M expr() {\n M acc = term();\n while (*it == '+' || *it == '-') {\n char op = *it++;\n M other = term();\n if (op == '+') {\n acc += other;\n }\n else {\n acc -= other;\n }\n }\n return acc;\n }\n\n M term() {\n M acc = factor();\n while (*it == '*') {\n assert(*it++ == '*');\n M other = factor();\n if (isnumber(acc) && isnumber(other)) {\n acc *= other;\n }\n else if (isnumber(acc) && not isnumber(other)) {\n acc = acc(0, 0) * other;\n }\n else if (not isnumber(acc) && isnumber(other)) {\n acc *= other(0, 0);\n }\n else {\n assert(not isnumber(acc) && not isnumber(other));\n acc *= other;\n }\n }\n return acc;\n }\n\n M factor() {\n if (*it == '-') {\n assert(*it++ == '-');\n M ret = factor();\n return -ret;\n }\n return primary();\n }\n\n M primary() {\n M ret;\n if (isdigit(*it)) {\n mint val = inum();\n ret = M(1, 1, val);\n }\n else if (isalpha(*it)) {\n ret = mp[*it++];\n }\n else if (*it == '(') {\n assert(*it++ == '(');\n ret = expr();\n assert(*it++ == ')');\n }\n else {\n assert(*it == '[');\n ret = matrix();\n }\n\n while (*it == '(' || *it == '\\'') {\n if (*it == '(') {\n assert(*it++ == '(');\n M is = expr();\n assert(*it++ == ',');\n M js = expr();\n assert(*it++ == ')');\n assert(is.n_rows() == 1);\n assert(js.n_rows() == 1);\n {\n vector<int> indices;\n rep (j, 0, is.n_cols()) {\n indices.push_back(is(0, j).val() - 1);\n }\n ret = ret.selected_rows(indices);\n }\n {\n vector<int> indices;\n rep (j, 0, js.n_cols()) {\n indices.push_back(js(0, j).val() - 1);\n }\n ret = ret.selected_cols(indices);\n }\n }\n else if (*it == '\\'') {\n assert(*it++ == '\\'');\n ret = ret.transposed();\n }\n }\n return ret;\n }\n\n M matrix() {\n assert(*it++ == '[');\n M row = row_seq();\n assert(*it++ == ']');\n return row;\n }\n\n M row_seq() {\n M acc = row();\n while (*it == ';') {\n assert(*it++ == ';');\n M other = row();\n acc = acc.col_concat(other);\n }\n return acc;\n }\n\n M row() {\n M acc = expr();\n while (*it == ' ') {\n assert(*it++ == ' ');\n M other = expr();\n acc = acc.row_concat(other);\n }\n return acc;\n }\n\n bool isnumber(const M& mat) {\n return mat.n_rows() == 1 && mat.n_cols() == 1;\n }\n\n mint inum() {\n mint ret = 0;\n for (; isdigit(*it); ++it) {\n ret = 10*ret + (*it - '0');\n }\n return ret;\n }\n};\n\nint32 main() {\n while (true) {\n auto n = sc.nextInt();\n if (!n) break;\n vector<string> s(n);\n for (string& si : s) si = sc.nextLine();\n Parser(s).parse();\n cout << \"-----\" << endl;\n }\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3608, "score_of_the_acc": -0.1094, "final_rank": 4 }, { "submission_id": "aoj_1314_7983042", "code_snippet": "#include <string.h>\n\n#include <algorithm>\n#include <array>\n#include <bitset>\n#include <cassert>\n#include <cfloat>\n#include <chrono>\n#include <ciso646>\n#include <climits>\n#include <cmath>\n#include <complex>\n#include <cstdio>\n#include <functional>\n#include <iomanip>\n#include <iostream>\n#include <map>\n#include <numeric>\n#include <optional>\n#include <queue>\n#include <random>\n#include <set>\n#include <stack>\n#include <string>\n#include <unordered_map>\n#include <unordered_set>\n#include <utility>\n#include <vector>\n\n#define REP_OVERLOAD(arg1, arg2, arg3, arg4, NAME, ...) NAME\n#define REP3(i, l, r, s) \\\n for (int i = int(l), rep3_r = int(r), rep3_s = int(s); i < rep3_r; \\\n i += rep3_s)\n#define REP2(i, l, r) REP3(i, l, r, 1)\n#define REP1(i, n) REP2(i, 0, n)\n#define rep(...) REP_OVERLOAD(__VA_ARGS__, REP3, REP2, REP1, )(__VA_ARGS__)\n#define repin(i, l, r) for (int i = int(l), repin_r = int(r); i <= repin_r; ++i)\n\n#define RREP_OVERLOAD(arg1, arg2, arg3, arg4, NAME, ...) NAME\n#define RREP3(i, l, r, s) \\\n for (int i = int(r) - 1, rrep3_l = int(l), rrep3_s = int(s); i >= rrep3_l; \\\n i -= rrep3_s)\n#define RREP2(i, l, r) RREP3(i, l, r, 1)\n#define RREP1(i, n) RREP2(i, 0, n)\n#define rrep(...) RREP_OVERLOAD(__VA_ARGS__, RREP3, RREP2, RREP1, )(__VA_ARGS__)\n#define rrepin(i, l, r) \\\n for (int i = int(r), rrepin_l = int(l); i >= rrepin_l; --i)\n\n\n#include <algorithm>\n#include <cassert>\n#include <vector>\n\nnamespace rklib {\n\n// a <- max(a, b)\ntemplate <class T>\nbool chmax(T &a, const T &b) {\n if (a < b) {\n a = b;\n return true;\n }\n return false;\n}\n\n// a <- min(a, b)\ntemplate <class T>\nbool chmin(T &a, const T &b) {\n if (a > b) {\n a = b;\n return true;\n }\n return false;\n}\n\n// if a < 0: a <- b\n// else: a <- min(a, b)\ntemplate <class T>\nbool chmin_non_negative(T &a, const T &b) {\n if (a < 0 || a > b) {\n a = b;\n return true;\n }\n return false;\n}\n\n// floor(num / den)\ntemplate <class T>\nT div_floor(T num, T den) {\n if (den < 0) num = -num, den = -den;\n return num >= 0 ? num / den : (num + 1) / den - 1;\n}\n\n// ceil(num / den)\ntemplate <class T>\nT div_ceil(T num, T den) {\n if (den < 0) num = -num, den = -den;\n return num <= 0 ? num / den : (num - 1) / den + 1;\n}\n\nnamespace internal {\n\ntemplate <class T>\nT remainder_count(T r, T b, T m) {\n return r / m * b + std::min(b, r % m);\n}\n\n} // namespace internal\n\n// Number of integer x s.t.\n// - x in [l, r)\n// - x mod m in [a, b)\ntemplate <class T>\nT remainder_count(T l, T r, T a, T b, T m) {\n assert(l >= 0);\n assert(m >= 1);\n\n if (l >= r || a >= b) return 0;\n if (m <= a || b < 0) return 0;\n chmax(a, T(0));\n chmin(b, m);\n\n auto res = internal::remainder_count(r, b, m);\n if (l >= 1) res -= internal::remainder_count(l, b, m);\n if (a >= 1) res -= internal::remainder_count(r, a, m);\n if (l >= 1 && a >= 1) res += internal::remainder_count(l, a, m);\n\n return res;\n}\n\n} // namespace rklib\n\n\nusing namespace std;\nusing namespace rklib;\n\nusing lint = long long;\nusing ulint = unsigned long long;\nusing pii = pair<int, int>;\nusing pll = pair<lint, lint>;\n\n\n\n#include <utility>\n\nnamespace atcoder {\n\nnamespace internal {\n\n// @param m `1 <= m`\n// @return x mod m\nconstexpr long long safe_mod(long long x, long long m) {\n x %= m;\n if (x < 0) x += m;\n return x;\n}\n\n// Fast modular multiplication by barrett reduction\n// Reference: https://en.wikipedia.org/wiki/Barrett_reduction\n// NOTE: reconsider after Ice Lake\nstruct barrett {\n unsigned int _m;\n unsigned long long im;\n\n // @param m `1 <= m < 2^31`\n barrett(unsigned int m) : _m(m), im((unsigned long long)(-1) / m + 1) {}\n\n // @return m\n unsigned int umod() const { return _m; }\n\n // @param a `0 <= a < m`\n // @param b `0 <= b < m`\n // @return `a * b % m`\n unsigned int mul(unsigned int a, unsigned int b) const {\n // [1] m = 1\n // a = b = im = 0, so okay\n\n // [2] m >= 2\n // im = ceil(2^64 / m)\n // -> im * m = 2^64 + r (0 <= r < m)\n // let z = a*b = c*m + d (0 <= c, d < m)\n // a*b * im = (c*m + d) * im = c*(im*m) + d*im = c*2^64 + c*r + d*im\n // c*r + d*im < m * m + m * im < m * m + 2^64 + m <= 2^64 + m * (m + 1) < 2^64 * 2\n // ((ab * im) >> 64) == c or c + 1\n unsigned long long z = a;\n z *= b;\n#ifdef _MSC_VER\n unsigned long long x;\n _umul128(z, im, &x);\n#else\n unsigned long long x =\n (unsigned long long)(((unsigned __int128)(z)*im) >> 64);\n#endif\n unsigned int v = (unsigned int)(z - x * _m);\n if (_m <= v) v += _m;\n return v;\n }\n};\n\n// @param n `0 <= n`\n// @param m `1 <= m`\n// @return `(x ** n) % m`\nconstexpr long long pow_mod_constexpr(long long x, long long n, int m) {\n if (m == 1) return 0;\n unsigned int _m = (unsigned int)(m);\n unsigned long long r = 1;\n unsigned long long y = safe_mod(x, m);\n while (n) {\n if (n & 1) r = (r * y) % _m;\n y = (y * y) % _m;\n n >>= 1;\n }\n return r;\n}\n\n// Reference:\n// M. Forisek and J. Jancina,\n// Fast Primality Testing for Integers That Fit into a Machine Word\n// @param n `0 <= n`\nconstexpr bool is_prime_constexpr(int n) {\n if (n <= 1) return false;\n if (n == 2 || n == 7 || n == 61) return true;\n if (n % 2 == 0) return false;\n long long d = n - 1;\n while (d % 2 == 0) d /= 2;\n constexpr long long bases[3] = {2, 7, 61};\n for (long long a : bases) {\n long long t = d;\n long long y = pow_mod_constexpr(a, t, n);\n while (t != n - 1 && y != 1 && y != n - 1) {\n y = y * y % n;\n t <<= 1;\n }\n if (y != n - 1 && t % 2 == 0) {\n return false;\n }\n }\n return true;\n}\ntemplate <int n> constexpr bool is_prime = is_prime_constexpr(n);\n\n// @param b `1 <= b`\n// @return pair(g, x) s.t. g = gcd(a, b), xa = g (mod b), 0 <= x < b/g\nconstexpr std::pair<long long, long long> inv_gcd(long long a, long long b) {\n a = safe_mod(a, b);\n if (a == 0) return {b, 0};\n\n // Contracts:\n // [1] s - m0 * a = 0 (mod b)\n // [2] t - m1 * a = 0 (mod b)\n // [3] s * |m1| + t * |m0| <= b\n long long s = b, t = a;\n long long m0 = 0, m1 = 1;\n\n while (t) {\n long long u = s / t;\n s -= t * u;\n m0 -= m1 * u; // |m1 * u| <= |m1| * s <= b\n\n // [3]:\n // (s - t * u) * |m1| + t * |m0 - m1 * u|\n // <= s * |m1| - t * u * |m1| + t * (|m0| + |m1| * u)\n // = s * |m1| + t * |m0| <= b\n\n auto tmp = s;\n s = t;\n t = tmp;\n tmp = m0;\n m0 = m1;\n m1 = tmp;\n }\n // by [3]: |m0| <= b/g\n // by g != b: |m0| < b/g\n if (m0 < 0) m0 += b / s;\n return {s, m0};\n}\n\n// Compile time primitive root\n// @param m must be prime\n// @return primitive root (and minimum in now)\nconstexpr int primitive_root_constexpr(int m) {\n if (m == 2) return 1;\n if (m == 167772161) return 3;\n if (m == 469762049) return 3;\n if (m == 754974721) return 11;\n if (m == 998244353) return 3;\n int divs[20] = {};\n divs[0] = 2;\n int cnt = 1;\n int x = (m - 1) / 2;\n while (x % 2 == 0) x /= 2;\n for (int i = 3; (long long)(i)*i <= x; i += 2) {\n if (x % i == 0) {\n divs[cnt++] = i;\n while (x % i == 0) {\n x /= i;\n }\n }\n }\n if (x > 1) {\n divs[cnt++] = x;\n }\n for (int g = 2;; g++) {\n bool ok = true;\n for (int i = 0; i < cnt; i++) {\n if (pow_mod_constexpr(g, (m - 1) / divs[i], m) == 1) {\n ok = false;\n break;\n }\n }\n if (ok) return g;\n }\n}\ntemplate <int m> constexpr int primitive_root = primitive_root_constexpr(m);\n\n} // namespace internal\n\n} // namespace atcoder\n\n\n#include <cassert>\n#include <numeric>\n#include <type_traits>\n\nnamespace atcoder {\n\nnamespace internal {\n\n#ifndef _MSC_VER\ntemplate <class T>\nusing is_signed_int128 =\n typename std::conditional<std::is_same<T, __int128_t>::value ||\n std::is_same<T, __int128>::value,\n std::true_type,\n std::false_type>::type;\n\ntemplate <class T>\nusing is_unsigned_int128 =\n typename std::conditional<std::is_same<T, __uint128_t>::value ||\n std::is_same<T, unsigned __int128>::value,\n std::true_type,\n std::false_type>::type;\n\ntemplate <class T>\nusing make_unsigned_int128 =\n typename std::conditional<std::is_same<T, __int128_t>::value,\n __uint128_t,\n unsigned __int128>;\n\ntemplate <class T>\nusing is_integral = typename std::conditional<std::is_integral<T>::value ||\n is_signed_int128<T>::value ||\n is_unsigned_int128<T>::value,\n std::true_type,\n std::false_type>::type;\n\ntemplate <class T>\nusing is_signed_int = typename std::conditional<(is_integral<T>::value &&\n std::is_signed<T>::value) ||\n is_signed_int128<T>::value,\n std::true_type,\n std::false_type>::type;\n\ntemplate <class T>\nusing is_unsigned_int =\n typename std::conditional<(is_integral<T>::value &&\n std::is_unsigned<T>::value) ||\n is_unsigned_int128<T>::value,\n std::true_type,\n std::false_type>::type;\n\ntemplate <class T>\nusing to_unsigned = typename std::conditional<\n is_signed_int128<T>::value,\n make_unsigned_int128<T>,\n typename std::conditional<std::is_signed<T>::value,\n std::make_unsigned<T>,\n std::common_type<T>>::type>::type;\n\n#else\n\ntemplate <class T> using is_integral = typename std::is_integral<T>;\n\ntemplate <class T>\nusing is_signed_int =\n typename std::conditional<is_integral<T>::value && std::is_signed<T>::value,\n std::true_type,\n std::false_type>::type;\n\ntemplate <class T>\nusing is_unsigned_int =\n typename std::conditional<is_integral<T>::value &&\n std::is_unsigned<T>::value,\n std::true_type,\n std::false_type>::type;\n\ntemplate <class T>\nusing to_unsigned = typename std::conditional<is_signed_int<T>::value,\n std::make_unsigned<T>,\n std::common_type<T>>::type;\n\n#endif\n\ntemplate <class T>\nusing is_signed_int_t = std::enable_if_t<is_signed_int<T>::value>;\n\ntemplate <class T>\nusing is_unsigned_int_t = std::enable_if_t<is_unsigned_int<T>::value>;\n\ntemplate <class T> using to_unsigned_t = typename to_unsigned<T>::type;\n\n} // namespace internal\n\n} // namespace atcoder\n\n#include <cassert>\n#include <numeric>\n#include <type_traits>\n\n#ifdef _MSC_VER\n#include <intrin.h>\n#endif\n\nnamespace atcoder {\n\nnamespace internal {\n\nstruct modint_base {};\nstruct static_modint_base : modint_base {};\n\ntemplate <class T> using is_modint = std::is_base_of<modint_base, T>;\ntemplate <class T> using is_modint_t = std::enable_if_t<is_modint<T>::value>;\n\n} // namespace internal\n\ntemplate <int m, std::enable_if_t<(1 <= m)>* = nullptr>\nstruct static_modint : internal::static_modint_base {\n using mint = static_modint;\n\n public:\n static constexpr int mod() { return m; }\n static mint raw(int v) {\n mint x;\n x._v = v;\n return x;\n }\n\n static_modint() : _v(0) {}\n template <class T, internal::is_signed_int_t<T>* = nullptr>\n static_modint(T v) {\n long long x = (long long)(v % (long long)(umod()));\n if (x < 0) x += umod();\n _v = (unsigned int)(x);\n }\n template <class T, internal::is_unsigned_int_t<T>* = nullptr>\n static_modint(T v) {\n _v = (unsigned int)(v % umod());\n }\n static_modint(bool v) { _v = ((unsigned int)(v) % umod()); }\n\n unsigned int val() const { return _v; }\n\n mint& operator++() {\n _v++;\n if (_v == umod()) _v = 0;\n return *this;\n }\n mint& operator--() {\n if (_v == 0) _v = umod();\n _v--;\n return *this;\n }\n mint operator++(int) {\n mint result = *this;\n ++*this;\n return result;\n }\n mint operator--(int) {\n mint result = *this;\n --*this;\n return result;\n }\n\n mint& operator+=(const mint& rhs) {\n _v += rhs._v;\n if (_v >= umod()) _v -= umod();\n return *this;\n }\n mint& operator-=(const mint& rhs) {\n _v -= rhs._v;\n if (_v >= umod()) _v += umod();\n return *this;\n }\n mint& operator*=(const mint& rhs) {\n unsigned long long z = _v;\n z *= rhs._v;\n _v = (unsigned int)(z % umod());\n return *this;\n }\n mint& operator/=(const mint& rhs) { return *this = *this * rhs.inv(); }\n\n mint operator+() const { return *this; }\n mint operator-() const { return mint() - *this; }\n\n mint pow(long long n) const {\n assert(0 <= n);\n mint x = *this, r = 1;\n while (n) {\n if (n & 1) r *= x;\n x *= x;\n n >>= 1;\n }\n return r;\n }\n mint inv() const {\n if (prime) {\n assert(_v);\n return pow(umod() - 2);\n } else {\n auto eg = internal::inv_gcd(_v, m);\n assert(eg.first == 1);\n return eg.second;\n }\n }\n\n friend mint operator+(const mint& lhs, const mint& rhs) {\n return mint(lhs) += rhs;\n }\n friend mint operator-(const mint& lhs, const mint& rhs) {\n return mint(lhs) -= rhs;\n }\n friend mint operator*(const mint& lhs, const mint& rhs) {\n return mint(lhs) *= rhs;\n }\n friend mint operator/(const mint& lhs, const mint& rhs) {\n return mint(lhs) /= rhs;\n }\n friend bool operator==(const mint& lhs, const mint& rhs) {\n return lhs._v == rhs._v;\n }\n friend bool operator!=(const mint& lhs, const mint& rhs) {\n return lhs._v != rhs._v;\n }\n\n private:\n unsigned int _v;\n static constexpr unsigned int umod() { return m; }\n static constexpr bool prime = internal::is_prime<m>;\n};\n\ntemplate <int id> struct dynamic_modint : internal::modint_base {\n using mint = dynamic_modint;\n\n public:\n static int mod() { return (int)(bt.umod()); }\n static void set_mod(int m) {\n assert(1 <= m);\n bt = internal::barrett(m);\n }\n static mint raw(int v) {\n mint x;\n x._v = v;\n return x;\n }\n\n dynamic_modint() : _v(0) {}\n template <class T, internal::is_signed_int_t<T>* = nullptr>\n dynamic_modint(T v) {\n long long x = (long long)(v % (long long)(mod()));\n if (x < 0) x += mod();\n _v = (unsigned int)(x);\n }\n template <class T, internal::is_unsigned_int_t<T>* = nullptr>\n dynamic_modint(T v) {\n _v = (unsigned int)(v % mod());\n }\n dynamic_modint(bool v) { _v = ((unsigned int)(v) % mod()); }\n\n unsigned int val() const { return _v; }\n\n mint& operator++() {\n _v++;\n if (_v == umod()) _v = 0;\n return *this;\n }\n mint& operator--() {\n if (_v == 0) _v = umod();\n _v--;\n return *this;\n }\n mint operator++(int) {\n mint result = *this;\n ++*this;\n return result;\n }\n mint operator--(int) {\n mint result = *this;\n --*this;\n return result;\n }\n\n mint& operator+=(const mint& rhs) {\n _v += rhs._v;\n if (_v >= umod()) _v -= umod();\n return *this;\n }\n mint& operator-=(const mint& rhs) {\n _v += mod() - rhs._v;\n if (_v >= umod()) _v -= umod();\n return *this;\n }\n mint& operator*=(const mint& rhs) {\n _v = bt.mul(_v, rhs._v);\n return *this;\n }\n mint& operator/=(const mint& rhs) { return *this = *this * rhs.inv(); }\n\n mint operator+() const { return *this; }\n mint operator-() const { return mint() - *this; }\n\n mint pow(long long n) const {\n assert(0 <= n);\n mint x = *this, r = 1;\n while (n) {\n if (n & 1) r *= x;\n x *= x;\n n >>= 1;\n }\n return r;\n }\n mint inv() const {\n auto eg = internal::inv_gcd(_v, mod());\n assert(eg.first == 1);\n return eg.second;\n }\n\n friend mint operator+(const mint& lhs, const mint& rhs) {\n return mint(lhs) += rhs;\n }\n friend mint operator-(const mint& lhs, const mint& rhs) {\n return mint(lhs) -= rhs;\n }\n friend mint operator*(const mint& lhs, const mint& rhs) {\n return mint(lhs) *= rhs;\n }\n friend mint operator/(const mint& lhs, const mint& rhs) {\n return mint(lhs) /= rhs;\n }\n friend bool operator==(const mint& lhs, const mint& rhs) {\n return lhs._v == rhs._v;\n }\n friend bool operator!=(const mint& lhs, const mint& rhs) {\n return lhs._v != rhs._v;\n }\n\n private:\n unsigned int _v;\n static internal::barrett bt;\n static unsigned int umod() { return bt.umod(); }\n};\ntemplate <int id> internal::barrett dynamic_modint<id>::bt = 998244353;\n\nusing modint998244353 = static_modint<998244353>;\nusing modint1000000007 = static_modint<1000000007>;\nusing modint = dynamic_modint<-1>;\n\nnamespace internal {\n\ntemplate <class T>\nusing is_static_modint = std::is_base_of<internal::static_modint_base, T>;\n\ntemplate <class T>\nusing is_static_modint_t = std::enable_if_t<is_static_modint<T>::value>;\n\ntemplate <class> struct is_dynamic_modint : public std::false_type {};\ntemplate <int id>\nstruct is_dynamic_modint<dynamic_modint<id>> : public std::true_type {};\n\ntemplate <class T>\nusing is_dynamic_modint_t = std::enable_if_t<is_dynamic_modint<T>::value>;\n\n} // namespace internal\n\n} // namespace atcoder\n\n\n#include <assert.h>\n\n#include <vector>\n\nnamespace rklib {\n\ntemplate <class T>\nstruct Matrix {\n std::vector<std::vector<T>> a;\n\n Matrix() : a(1, std::vector<T>(1, 0)) {}\n Matrix(int n, int m) : a(n, std::vector<T>(m, 0)) {}\n Matrix(int n) : a(n, std::vector<T>(n, 0)) {}\n\n int height() const { return a.size(); }\n int width() const { return a[0].size(); }\n\n inline const std::vector<T> &operator[](int i) const { return a[i]; }\n inline std::vector<T> &operator[](int i) { return a[i]; }\n\n static Matrix id(int n) {\n Matrix res(n);\n for (int i = 0; i < n; i++) res[i][i] = 1;\n return res;\n }\n\n Matrix &operator*=(const Matrix &b) {\n assert(width() == b.height());\n std::vector<std::vector<T>> c(height(), std::vector<T>(b.width()));\n for (int i = 0; i < height(); ++i) {\n for (int k = 0; k < b.height(); ++k) {\n for (int j = 0; j < b.width(); ++j) {\n c[i][j] += a[i][k] * b[k][j];\n }\n }\n }\n a.swap(c);\n return *this;\n }\n\n Matrix pow(long long n) const {\n auto x = (*this), res = id(height());\n while (n) {\n if (n & 1) {\n res *= x;\n --n;\n } else {\n x *= x;\n n >>= 1;\n }\n }\n return res;\n }\n\n Matrix operator*(const Matrix &b) const { return Matrix(*this) *= b; }\n\n std::vector<T> operator*(const std::vector<T> &v) {\n assert(width() == (int)v.size());\n std::vector<T> res(height(), 0);\n for (int i = 0; i < height(); ++i) {\n for (int j = 0; j < width(); ++j) {\n res[i] += a[i][j] * v[j];\n }\n }\n return res;\n }\n\n Matrix transposed() {\n Matrix res(this->width(), this->height());\n for (size_t i = 0; i < this->height(); i++) {\n for (size_t j = 0; j < this->width(); j++) {\n res[j][i] = this->a[i][j];\n }\n }\n return res;\n }\n};\n\ntemplate <class T>\nstd::pair<int, bool> gauss_jordan(Matrix<T> &a) {\n int rnk = 0;\n bool swp = false;\n for (int j = 0; j < a.width(); ++j) {\n int pivot = -1;\n for (int i = rnk; i < a.height(); ++i) {\n if (a[i][j] != 0) {\n pivot = i;\n break;\n }\n }\n if (pivot < 0) continue;\n swap(a[pivot], a[rnk]);\n if (pivot != rnk) swp ^= true;\n for (int i = 0; i < a.height(); ++i) {\n if (i != rnk && a[i][j] != 0) {\n auto coef = a[i][j] / a[rnk][j];\n for (int k = j; k < a.width(); ++k) {\n a[i][k] -= a[rnk][k] * coef;\n }\n }\n }\n ++rnk;\n }\n return {rnk, swp};\n}\n\ntemplate <class T>\nT determinant(Matrix<T> a) {\n auto [rnk, swp] = gauss_jordan(a);\n if (rnk < a.height()) return 0;\n T res = 1;\n for (int i = 0; i < a.height(); ++i) res *= a[i][i];\n if (swp) res = -res;\n return res;\n}\n\ntemplate <class T>\nstd::pair<std::vector<T>, std::vector<std::vector<T>>>\nsystem_of_linear_equations(Matrix<T> a, const std::vector<T> &b) {\n assert(a.height() == (int)b.size());\n Matrix<T> aug(a.height(), a.width() + 1);\n for (int i = 0; i < a.height(); ++i) {\n for (int j = 0; j < a.width(); ++j) {\n aug[i][j] = a[i][j];\n }\n aug[i][a.width()] = b[i];\n }\n\n auto rnk = gauss_jordan(a).first, rnk_aug = gauss_jordan(aug).first;\n\n std::vector<T> solution;\n std::vector<std::vector<T>> kernel;\n if (rnk < rnk_aug) return {solution, kernel};\n\n solution.resize(a.width(), 0);\n std::vector<bool> used(a.width(), false);\n std::vector<int> pos(rnk);\n for (int i = 0; i < rnk; ++i) {\n for (int j = 0; j < a.width(); ++j) {\n if (aug[i][j] != 0) {\n solution[j] = aug[i][a.width()] / aug[i][j];\n used[j] = true;\n pos[i] = j;\n break;\n }\n }\n }\n for (int j = 0; j < a.width(); ++j) {\n if (!used[j]) {\n std::vector<T> v(a.width(), 0);\n v[j] = 1;\n for (int i = 0; i < rnk; ++i) {\n v[pos[i]] = -aug[i][j] / aug[i][pos[i]];\n }\n kernel.push_back(v);\n }\n }\n return {solution, kernel};\n}\n\n} // namespace rklib\n\n\nusing mint = atcoder::modint;\nusing mat = Matrix<mint>;\n\nmat var(string &s, int &idx);\nmat expr(string &s, int &idx);\nmat term(string &s, int &idx);\nmat factor(string &s, int &idx);\nmat primary(string &s, int &idx);\nmat matrix(string &s, int &idx);\nmat row_seq(string &s, int &idx);\nmat row(string &s, int &idx);\nmat inum(string &s, int &idx);\n\nmat vars[26];\n\nmat var(string &s, int &idx) {\n auto res = vars[s[idx] - 'A'];\n ++idx;\n return res;\n}\nmat expr(string &s, int &idx) {\n auto res = term(s, idx);\n while (idx < int(s.size()) && (s[idx] == '+' || s[idx] == '-')) {\n auto op = s[idx];\n ++idx;\n auto tmp = term(s, idx);\n if (op == '+') {\n rep(i, res.height()) rep(j, res.width()) res[i][j] += tmp[i][j];\n } else {\n rep(i, res.height()) rep(j, res.width()) res[i][j] -= tmp[i][j];\n }\n }\n return res;\n}\nmat term(string &s, int &idx) {\n auto res = factor(s, idx);\n while (idx < int(s.size()) && s[idx] == '*') {\n ++idx;\n auto tmp = factor(s, idx);\n if (res.height() == 1 && res.width() == 1) {\n auto coef = res[0][0];\n res = tmp;\n rep(i, res.height()) rep(j, res.width()) res[i][j] *= coef;\n } else if (tmp.height() == 1 && tmp.width() == 1) {\n auto coef = tmp[0][0];\n rep(i, res.height()) rep(j, res.width()) res[i][j] *= coef;\n } else {\n res = res * tmp;\n }\n }\n return res;\n}\nmat factor(string &s, int &idx) {\n if (s[idx] == '-') {\n ++idx;\n auto res = factor(s, idx);\n rep(i, res.height()) rep(j, res.width()) res[i][j] = -res[i][j];\n return res;\n }\n return primary(s, idx);\n}\nmat primary(string &s, int &idx) {\n mat res;\n if (isdigit(s[idx])) {\n res = inum(s, idx);\n } else if ('A' <= s[idx] && s[idx] <= 'Z') {\n res = var(s, idx);\n } else if (s[idx] == '[') {\n res = matrix(s, idx);\n } else {\n ++idx;\n res = expr(s, idx);\n ++idx;\n }\n while (idx < s.size() && (s[idx] == '\\'' || s[idx] == '(')) {\n if (s[idx] == '\\'') {\n ++idx;\n res = res.transposed();\n } else {\n ++idx;\n auto A = expr(s, idx);\n ++idx;\n auto B = expr(s, idx);\n ++idx;\n mat tmp(A.width(), B.width());\n rep(i, A.width()) rep(j, B.width()) {\n tmp[i][j] = res[A[0][i].val() - 1][B[0][j].val() - 1];\n }\n res = tmp;\n }\n }\n return res;\n}\nmat matrix(string &s, int &idx) {\n ++idx;\n auto res = row_seq(s, idx);\n ++idx;\n return res;\n}\nmat row_seq(string &s, int &idx) {\n auto res = row(s, idx);\n while (idx < int(s.size()) && s[idx] == ';') {\n ++idx;\n auto tmp = row(s, idx);\n mat res2(res.height() + tmp.height(), res.width());\n rep(j, res.width()) {\n rep(i, res.height()) res2[i][j] = res[i][j];\n rep(i, tmp.height()) res2[i + res.height()][j] = tmp[i][j];\n }\n res = res2;\n }\n return res;\n}\nmat row(string &s, int &idx) {\n auto res = expr(s, idx);\n while (idx < int(s.size()) && s[idx] == ' ') {\n ++idx;\n auto tmp = expr(s, idx);\n mat res2(res.height(), res.width() + tmp.width());\n rep(i, res.height()) {\n rep(j, res.width()) res2[i][j] = res[i][j];\n rep(j, tmp.width()) res2[i][j + res.width()] = tmp[i][j];\n }\n res = res2;\n }\n return res;\n}\nmat inum(string &s, int &idx) {\n mat res(1, 1);\n mint tmp = 0;\n while (idx < int(s.size()) && isdigit(s[idx])) {\n tmp = tmp * 10 + (s[idx] - '0');\n ++idx;\n }\n\n res[0][0] = tmp;\n return res;\n}\n\nint main() {\n mint::set_mod(32768);\n\n while (true) {\n string s;\n getline(cin, s);\n int n = stoi(s);\n if (n == 0) break;\n rep(_, n) {\n getline(cin, s);\n int idx = 0;\n\n auto c = s[idx];\n idx += 2;\n auto ans = expr(s, idx);\n vars[c - 'A'] = ans;\n\n rep(i, ans.height()) {\n rep(j, ans.width()) {\n cout << ans[i][j].val();\n if (j == ans.width() - 1) {\n cout << \"\\n\";\n } else {\n cout << \" \";\n }\n }\n }\n }\n puts(\"-----\");\n }\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3776, "score_of_the_acc": -0.1354, "final_rank": 13 }, { "submission_id": "aoj_1314_6027674", "code_snippet": "#include <bits/stdc++.h>\n#define _overload3(_1, _2, _3, name, ...) name\n#define _rep(i, n) repi(i, 0, n)\n#define repi(i, a, b) for (int i = (a); i < (b); ++i)\n#define rep(...) _overload3(__VA_ARGS__, repi, _rep, )(__VA_ARGS__)\n#define ALL(x) x.begin(), x.end()\n#define chmax(x, y) x = max(x, y)\n#define chmin(x, y) x = min(x, y)\nusing namespace std;\nrandom_device rnd;\nmt19937 mt(rnd());\nusing ll = long long;\nusing lld = long double;\nusing VI = vector<int>;\nusing VVI = vector<VI>;\nusing VL = vector<ll>;\nusing VVL = vector<VL>;\nusing PII = pair<int, int>;\nconst int IINF = 1 << 30;\nconst ll INF = 1ll << 60;\nconst ll MOD = 32768;\n\nusing Matrix = vector<vector<int>>;\n\nstring code;\n\nvoid program(int &pos);\nvoid assign(int &pos);\nMatrix expr(int &pos);\nMatrix term(int &pos);\nMatrix factor(int &pos);\nMatrix primary(int &pos);\nMatrix indexed_primary(int &pos);\nMatrix transposed_primary(int &pos);\nMatrix matrix(int &pos);\nMatrix row_seq(int &pos);\nMatrix inum(int &pos);\nint digit(int &pos);\nmap<char, Matrix> var;\n\nvoid debug(int pos)\n{\n cerr << pos << \" \" << code.size() << endl;\n rep(i, pos, code.size())\n {\n cout << code[i];\n }\n cout << endl;\n}\nvoid print(Matrix &mat)\n{\n for (auto r : mat)\n {\n rep(j, r.size())\n {\n auto elem = r[j];\n cout << elem << (j == r.size() - 1 ? \"\" : \" \");\n }\n cout << endl;\n }\n}\n\nvoid addsub(Matrix &a, Matrix &b, char op)\n{\n assert(a.size() == b.size());\n assert(a[0].size() == b[0].size());\n rep(i, a.size())\n {\n rep(j, a[0].size())\n {\n\n a[i][j] += (op == '+' ? 1 : -1) * b[i][j];\n a[i][j] += MOD;\n a[i][j] %= MOD;\n }\n }\n}\nMatrix trans(Matrix &a)\n{\n Matrix ret(a[0].size(), vector<int>(a.size()));\n rep(i, a.size()) rep(j, a[0].size())\n {\n ret[j][i] = a[i][j];\n }\n return ret;\n}\nMatrix mul(Matrix a, Matrix b)\n{\n\n if (a[0].size() == b.size())\n {\n Matrix c(a.size(), vector<int>(b[0].size(), 0));\n rep(i, a.size())\n {\n rep(j, b[0].size())\n {\n rep(k, a[0].size())\n {\n c[i][j] += a[i][k] * b[k][j];\n c[i][j] %= MOD;\n }\n }\n }\n return c;\n }\n else if (a.size() == 1 && a[0].size() == 1)\n {\n Matrix c(b.size(), vector<int>(b[0].size(), 0));\n rep(i, b.size())\n {\n rep(j, b[0].size())\n {\n c[i][j] = b[i][j] * a[0][0];\n c[i][j] %= MOD;\n }\n }\n return c;\n }\n else\n {\n assert(b.size() == 1 && b[0].size() == 1);\n Matrix c(a.size(), vector<int>(a[0].size(), 0));\n rep(i, a.size())\n {\n rep(j, a[0].size())\n {\n c[i][j] = a[i][j] * b[0][0];\n c[i][j] %= MOD;\n }\n }\n return c;\n }\n}\n\nint digit(int &pos)\n{\n assert(isdigit(code[pos]));\n return code[pos++] - '0';\n}\n\nMatrix inum(int &pos)\n{\n Matrix ret(1);\n int num = 0;\n while (isdigit(code[pos]))\n {\n num = num * 10 + digit(pos);\n }\n ret[0].push_back(num);\n return ret;\n}\n\nvoid fill(Matrix &ret, Matrix mat, int r, int c)\n{\n rep(i, mat.size())\n {\n rep(j, mat[0].size())\n {\n if (mat[i][j] != -1)\n {\n ret[r + i][c + j] = mat[i][j];\n }\n }\n }\n}\n\nvoid shrink(Matrix &mat)\n{\n assert(!mat.back().empty());\n while (mat.back()[0] == -1)\n {\n mat.pop_back();\n assert(!mat.empty());\n assert(!mat.back().empty());\n }\n rep(i, mat.size())\n {\n assert(!mat[i].empty());\n while (mat[i].back() == -1)\n {\n mat[i].pop_back();\n assert(!mat[i].empty());\n }\n }\n}\n\nMatrix row_seq(int &pos)\n{\n Matrix ret(128, vector<int>(128, -1));\n // debug(pos);\n Matrix m = expr(pos);\n // debug(pos);\n int r = 0, c = 0;\n int width = -1;\n fill(ret, m, r, c);\n while (ret[r][c] != -1)\n c++;\n // debug(pos);\n\n while (code[pos] == ';' || code[pos] == ' ')\n {\n if (code[pos] == ' ')\n {\n pos++;\n // m = expr(pos);\n // fill(ret, m, r, c);\n // while (ret[r][c] != -1)\n // c++;\n }\n else\n {\n pos++;\n if (width == -1)\n width = c;\n else\n assert(width == c);\n c = 0;\n r++;\n while (ret[r][c] != -1)\n {\n c++;\n if (c == width)\n {\n c = 0;\n r++;\n }\n }\n }\n\n m = expr(pos);\n fill(ret, m, r, c);\n while (ret[r][c] != -1)\n c++;\n }\n // print(ret);\n shrink(ret);\n // print(ret);\n return ret;\n}\n\nMatrix matrix(int &pos)\n{\n assert(code[pos] == '[');\n pos++;\n auto ret = row_seq(pos);\n // cerr << code[pos] << endl;\n assert(code[pos] == ']');\n pos++;\n return ret;\n}\n\nMatrix primary(int &pos)\n{\n Matrix ret;\n if (isdigit(code[pos]))\n {\n ret = inum(pos);\n }\n else if (code[pos] == '[')\n {\n ret = matrix(pos);\n }\n else if (code[pos] == '(')\n {\n pos++;\n ret = expr(pos);\n assert(code[pos] == ')');\n pos++;\n }\n else\n {\n ret = var[code[pos]];\n pos++;\n }\n while (code[pos] == '(' || code[pos] == '\\'')\n {\n\n if (code[pos] == '(')\n {\n pos++;\n auto rows = expr(pos);\n assert(code[pos] == ',');\n pos++;\n auto cols = expr(pos);\n assert(rows.size() == 1);\n assert(cols.size() == 1);\n assert(code[pos] == ')');\n pos++;\n Matrix tmp(128, vector<int>(128, -1));\n rep(i, rows[0].size())\n {\n rep(j, cols[0].size())\n {\n tmp[i][j] = ret[rows[0][i] - 1][cols[0][j] - 1];\n }\n }\n ret = tmp;\n shrink(ret);\n }\n else if (code[pos] == '\\'')\n {\n pos++;\n ret = trans(ret);\n }\n }\n\n return ret;\n}\n\nMatrix expr(int &pos)\n{\n Matrix ret = term(pos);\n while (code[pos] == '+' || code[pos] == '-')\n {\n char op = code[pos];\n pos++;\n Matrix m = term(pos);\n addsub(ret, m, op);\n }\n return ret;\n}\n\nMatrix term(int &pos)\n{\n Matrix ret = factor(pos);\n while (code[pos] == '*')\n {\n pos++;\n Matrix m = factor(pos);\n ret = mul(ret, m);\n }\n return ret;\n}\n\nMatrix factor(int &pos)\n{\n if (code[pos] == '-')\n {\n pos++;\n Matrix ret = factor(pos);\n rep(i, ret.size())\n {\n rep(j, ret[0].size())\n {\n ret[i][j] = MOD - ret[i][j];\n }\n }\n return ret;\n }\n return primary(pos);\n}\n\nvoid program(int &pos)\n{\n while (pos < code.size())\n {\n assign(pos);\n }\n}\n\nvoid assign(int &pos)\n{\n char v = code[pos];\n pos++;\n assert(code[pos] == '=');\n pos++;\n var[v] = expr(pos);\n print(var[v]);\n // debug(pos);\n assert(code[pos] == '.');\n pos++;\n}\n\nvoid solve(int n)\n{\n code = \"\";\n rep(i, n)\n {\n string s;\n getline(cin, s);\n code += s;\n }\n // cerr << code << endl;\n int pos = 0;\n program(pos);\n cout << \"-----\" << endl;\n}\n\nint main()\n{\n int n;\n while (cin >> n, cin.ignore(), n)\n {\n solve(n);\n }\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3988, "score_of_the_acc": -0.1681, "final_rank": 16 }, { "submission_id": "aoj_1314_6025039", "code_snippet": "/*\n   ∫ ∫ ∫\n   ノヽ\n  (_  )\n (_    )\n(______ )\n ヽ(´・ω・)ノ \n   |  /\n   UU\n*/\n#pragma region macro\n#include <iostream>\n#include <string>\n#include <algorithm>\n#include <cctype>\n#include <vector>\n#include <cassert>\n#include <cmath>\n#include <utility>\n#include <tuple>\n#include <map>\n#include <set>\n#include <deque>\n#include <queue>\ntypedef long long int64;\nusing namespace std;\ntypedef vector<int> vi;\nconst int MOD = 1<<15;\nconst int64 INF = 1LL << 62;\nconst int inf = 1<<30;\nconst char bn = '\\n';\ntemplate<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }\ntemplate<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return 1; } return 0; }\n#define REP(i, n) for (int i = 0; i < (n); i++)\n#define ALL(obj) (obj).begin(), (obj).end() //コンテナじゃないと使えない!!\n#define debug(x) cerr << #x << \": \" << (x) << \"\\n\";\n#define mp make_pair\ntemplate <typename T>\nostream& operator<<(ostream& os, const vector<T> &V){\n int N = V.size();\n REP(i,N){\n os << V[i];\n if (i!=N-1) os << \" \";\n }\n os << \"\\n\";\n return os;\n}\ntemplate <typename T,typename S>\nostream& operator<<(ostream& os, pair<T,S> const&P){\n os << \"(\";\n os << P.first;\n os << \" , \";\n os << P.second;\n os << \")\";\n return os;\n}\ntemplate <typename T,typename S,typename U>\nostream& operator<<(ostream& os, tuple<T,S,U> const& P){\n os << \"(\";\n os << get<0>(P);\n os << \", \";\n os << get<1>(P);\n os << \", \";\n os << get<2>(P);\n os << \")\";\n return os;\n}\ntemplate <typename T>\nostream& operator<<(ostream& os, set<T> &S){\n auto it=S.begin();\n while(it!=S.end()){\n os << *it;\n os << \" \";\n it++;\n }\n os << \"\\n\";\n return os;\n}\ntemplate <typename T>\nostream& operator<<(ostream& os, deque<T> &q){\n for(auto it=q.begin();it<q.end();it++){\n os<<*it;\n os<<\" \";\n }\n os<<endl;\n return os;\n}\ntemplate <typename T,typename S>\nostream& operator<<(ostream& os, map<T,S> const&M){\n for(auto e:M){\n os<<e;\n }\n os<<endl;\n return os;\n}\nvector<pair<int,int>> dxdy = {mp(0,1),mp(1,0),mp(-1,0),mp(0,-1)};\n#pragma endregion\n//fixed<<setprecision(10)<<ans<<endl;\n\n\nint64 pow(int a,int b,int mod){\n vector<bool> bit;\n for(b=b;b>0;b>>=1){\n bit.push_back(b&1);\n }\n vector<int64> fac(bit.size()); fac[0] = a;\n int64 res = 1;\n for(int i=1;i<bit.size();i++){\n fac[i] = (fac[i-1] * fac[i-1])%mod;\n }\n for(int i=0;i<bit.size();i++){\n if(bit[i]) res*=fac[i];\n res%=mod;\n }\n return res;\n}\n\n\n//mint\nstruct mint {\n int64 x;\n mint(int64 x=0):x((x+2*MOD)%MOD){}\n mint& operator+=(const mint a) {\n if ((x += a.x) >= MOD) x -= MOD;\n return *this;\n }\n mint& operator-=(const mint a) {\n if ((x += MOD-a.x) >= MOD) x -= MOD;\n return *this;\n }\n mint& operator*=(const mint a) {\n (x *= a.x) %= MOD;\n return *this;\n }\n mint operator+(const mint a) const {\n mint res(*this);\n return res+=a;\n }\n mint operator-(const mint a) const {\n mint res(*this);\n return res-=a;\n }\n mint operator*(const mint a) const {\n mint res(*this);\n return res*=a;\n }\n mint pow(int64 t) const {\n if (!t) return 1;\n mint a = pow(t>>1);\n a *= a;\n if (t&1) a *= *this;\n return a;\n }\n\n // for prime MOD\n mint inv() const {\n return pow(MOD-2);\n }\n mint& operator/=(const mint a) {\n return (*this) *= a.inv();\n }\n mint operator/(const mint a) const {\n mint res(*this);\n return res/=a;\n }\n};\nostream& operator<<(ostream& os, mint a){\n os << a.x;\n return os;\n}\n\ntemplate <typename T>\nstruct Matrix{\n vector<vector<T>> matrix_data;\n Matrix(){}\n Matrix(const vector<vector<T>>& matrix_data):matrix_data(matrix_data){}\n Matrix(const T& val):matrix_data(vector<vector<T>>(1, vector<T>(1, val))){} //1*1の行列\n\n //単位行列\n Matrix<T> Identity(size_t N){\n vector<vector<T>> res(N,vector<T>(N,0));\n for(size_t i=0; i<N; i++) res[i][i]=1;\n return Matrix<T>(res);\n }\n\n //累乗O(N^3 log k)\n Matrix<T> power(int64 k){\n Matrix<T> res = Matrix::Identity(matrix_data.size());\n Matrix<T> tmp = *this;\n\n while(k){\n if(k&1){res *= tmp;}\n tmp *= tmp;\n k >>= 1LL;\n }\n return res;\n }\n\n void add_row(Matrix<T> const& A){\n assert(A.matrix_data[0].size() == matrix_data[0].size());\n \n for(auto row:A.matrix_data){\n matrix_data.emplace_back(row);\n }\n }\n\n void add_column(Matrix<T> const& A){\n assert(A.matrix_data.size() == matrix_data.size());\n \n REP(i, A.matrix_data.size()){\n for(auto v:A.matrix_data[i]){\n matrix_data[i].emplace_back(v);\n } \n }\n }\n\n Matrix<T> transpose(){\n vector<vector<T>> res(matrix_data[0].size(), vector<T>(matrix_data.size()));\n REP(i, matrix_data.size()){\n REP(j, matrix_data[0].size()){\n res[j][i] = matrix_data[i][j];\n }\n }\n\n return Matrix<T>(res);\n }\n\n\n};\n\ntemplate <typename T>\nMatrix<T> operator+(const Matrix<T>& mat_a, const Matrix<T>& mat_b){\n assert(mat_a.matrix_data.size() == mat_b.matrix_data.size());\n assert(mat_a.matrix_data[0].size() == mat_b.matrix_data[0].size());\n size_t A = mat_a.matrix_data.size();\n size_t B = mat_b.matrix_data[0].size();\n\n Matrix<T> res = mat_a;\n \n for(size_t i=0; i<A; i++){\n for(size_t j=0; j<B; j++){\n res.matrix_data[i][j] += mat_b.matrix_data[i][j];\n }\n }\n\n return res;\n}\n\ntemplate <typename T>\nMatrix<T>& operator+=(Matrix<T>& mat_a, Matrix<T>& mat_b){\n mat_a = mat_a+mat_b;\n return mat_a;\n}\n\ntemplate <typename T>\nMatrix<T> operator-(const Matrix<T>& mat_a, const Matrix<T>& mat_b){\n assert(mat_a.matrix_data.size() == mat_b.matrix_data.size());\n assert(mat_a.matrix_data[0].size() == mat_b.matrix_data[0].size());\n size_t A = mat_a.matrix_data.size();\n size_t B = mat_b.matrix_data[0].size();\n\n Matrix<T> res = mat_a;\n \n for(size_t i=0; i<A; i++){\n for(size_t j=0; j<B; j++){\n res.matrix_data[i][j] -= mat_b.matrix_data[i][j];\n }\n }\n\n return res;\n}\n\ntemplate <typename T>\nMatrix<T>& operator-=(Matrix<T>& mat_a, Matrix<T>& mat_b){\n mat_a = mat_a-mat_b;\n return mat_a;\n}\n\ntemplate <typename T>\nbool is_scalar_(Matrix<T> const& A){\n return A.matrix_data.size() == 1 and A.matrix_data[0].size() == 1;\n}\n\ntemplate <typename T>\nMatrix<T> operator*(const Matrix<T>& mat_a, const Matrix<T>& mat_b){\n if( is_scalar_(mat_a) ){\n return mat_a.matrix_data[0][0].x * mat_b;\n }else if( is_scalar_(mat_b) ){\n return mat_b.matrix_data[0][0].x * mat_a;\n\n }\n size_t A = mat_a.matrix_data.size();\n size_t B = mat_b.matrix_data.size();\n assert(B > 0);\n size_t C = mat_b.matrix_data[0].size();\n\n Matrix<T> res(vector<vector<T>>(A,vector<T>(C,0)));\n \n for(size_t i=0; i<A; i++){\n assert(mat_a.matrix_data[i].size() == B);\n for(size_t j=0; j<C; j++){\n for(size_t k=0; k<B; k++){\n res.matrix_data[i][j] += mat_a.matrix_data[i][k] * mat_b.matrix_data[k][j];\n }\n }\n }\n\n return res;\n}\n\ntemplate <typename T>\nMatrix<T> operator*(const int& val, const Matrix<T>& mat_a){\n Matrix<T> res = mat_a;\n for(auto&& row: res.matrix_data){\n for(auto&& a:row) a *= val;\n }\n \n return res;\n}\n\ntemplate <typename T>\nMatrix<T>& operator*=(Matrix<T>& mat_a, Matrix<T>& mat_b){\n mat_a = mat_a*mat_b;\n return mat_a;\n}\n\ntemplate <typename T>\nostream& operator<<(ostream& os, const Matrix<T>& M){\n REP(i,M.matrix_data.size()){\n REP(j,M.matrix_data[0].size()){\n os << M.matrix_data[i][j];\n if(j!=M.matrix_data[0].size()-1) os<<\" \";\n }\n os << \"\\n\";\n }\n return os;\n}\n\n\n\nmap<char, Matrix<mint>> matrix_dict;\n\n// 構文解析\n/*\n 構文解析でよくあるバグは次の二つです。\n - begin++をし忘れる\n - getlineとcinの混合\n*/\n\n\ntypedef string::const_iterator State;\nclass ParseError {};\n\n// beginがexpectedを指していたらbeginを一つ進める。\nvoid consume(State &begin, char expected) {\n if (*begin == expected) {\n begin++;\n } else {\n cerr << \"Expected '\" << expected << \"' but got '\" << *begin << \"'\"\n << endl;\n cerr << \"Rest string is '\";\n while (*begin) {\n cerr << *begin++;\n }\n cerr << \"'\" << endl;\n throw ParseError();\n }\n}\nvoid print_rest_string(State begin){\n cerr << \"Rest string is '\";\n while (*begin) {\n cerr << *begin++;\n }\n cerr << \"'\" << endl;\n}\n\nbool is_capital(char C){\n return 'A' <= C and C <= 'Z';\n}\n\nMatrix<mint> expression(State &begin);\nMatrix<mint> term(State &begin);\nMatrix<mint> factor(State &begin);\nMatrix<mint> primary(State &begin);\nMatrix<mint> matrix(State &begin);\nMatrix<mint> row(State &begin);\nMatrix<mint> number(State &begin);\n\n// 四則演算の式をパースして、その評価結果を返す。\nMatrix<mint> expression(State &begin){\n Matrix<mint> res = term(begin);\n\n while(true){\n if(*begin == '+'){\n consume(begin, '+');\n auto tmp = term(begin);\n res += tmp;\n }else if(*begin == '-'){\n consume(begin, '-');\n auto tmp = term(begin);\n res -= tmp;\n }else{\n break;\n }\n }\n\n return res;\n}\n\n// 乗算除算の式をパースして、その評価結果を返す。\nMatrix<mint> term(State &begin){\n Matrix<mint> res = factor(begin);\n \n while(true){\n if(*begin == '*'){\n consume(begin, '*');\n auto tmp = factor(begin);\n res *= tmp;\n }else{\n break;\n }\n }\n\n return res;\n}\n\nMatrix<mint> factor(State &begin){\n Matrix<mint> res;\n if(*begin == '-'){\n consume(begin, '-');\n auto tmp = factor(begin);\n res = (-1) * tmp ;\n }else{\n res = primary(begin);\n }\n return res;\n}\n\nMatrix<mint> primary(State &begin){\n Matrix<mint> res;\n if( isdigit(*begin) ){\n res = number(begin);\n }else if( is_capital(*begin) ){\n res = matrix_dict[*begin++];\n }else if( *begin == '[' ){\n res = matrix(begin);\n }else if( *begin == '(' ){\n consume(begin, '(');\n res = expression(begin);\n consume(begin, ')');\n }else{\n assert(false);\n }\n\n while(true){\n if(*begin == '('){\n consume(begin, '(');\n Matrix<mint> expr1 = expression(begin);\n consume(begin, ',');\n Matrix<mint> expr2 = expression(begin);\n consume(begin, ')');\n\n assert(expr1.matrix_data.size()==1);\n assert(expr2.matrix_data.size()==1);\n\n vector<vector<mint>> res_vec(expr1.matrix_data[0].size(), vector<mint>(expr2.matrix_data[0].size(), 0 ) );\n REP(i, expr1.matrix_data[0].size()){\n REP(j, expr2.matrix_data[0].size()){\n res_vec[i][j] = res.matrix_data[ expr1.matrix_data[0][i].x-1 ][ expr2.matrix_data[0][j].x-1 ];\n }\n }\n res = Matrix<mint>( res_vec );\n }else if(*begin == '\\''){\n consume(begin, '\\'');\n res = res.transpose();\n }else break;\n\n }\n return res;\n}\n\nMatrix<mint> matrix(State &begin){\n consume(begin, '[');\n\n Matrix<mint> res = row(begin);\n\n while(true){\n if(*begin == ';'){\n consume(begin, ';');\n res.add_row( row(begin) );\n }else break;\n }\n\n consume(begin, ']');\n return res;\n}\n\nMatrix<mint> row(State &begin){\n Matrix<mint> res = expression(begin);\n\n while(true){\n if(*begin == ' '){\n consume(begin, ' ');\n res.add_column( expression(begin) );\n }else break;\n }\n\n return res;\n}\n\n// 数字の列をパースして、その数を返す。\nMatrix<mint> number(State &begin){\n int val = 0;\n while(isdigit(*begin)){\n val *= 10;\n val += *begin - '0';\n begin++;\n }\n\n Matrix<mint> res(val);\n return res;\n}\n\nvoid solve(string S){\n State begin = S.begin();\n char var = *begin++;\n consume(begin, '=');\n\n Matrix<mint> ans = expression(begin);\n consume(begin, '.');\n matrix_dict[var] = ans;\n cout << ans;\n}\n\n\nint main(){\n cin.tie(nullptr);\n ios::sync_with_stdio(false);\n int N;\n string tmp;\n while(getline(cin, tmp)){\n N = stoi(tmp);\n if(N==0) break;\n REP(_, N){\n string S; getline(cin,S);\n // debug(S)\n solve(S);\n }\n cout << \"-----\" << endl;\n }\n \n}", "accuracy": 1, "time_ms": 10, "memory_kb": 4008, "score_of_the_acc": -0.1712, "final_rank": 17 }, { "submission_id": "aoj_1314_6021621", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\ntypedef long long ll;\ntemplate<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return true; } return false; }\ntemplate<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return true; } return false; }\n#define all(x) (x).begin(),(x).end()\n#define fi first\n#define se second\n#define mp make_pair\n#define si(x) int(x.size())\nconst int mod=998244353,MAX=205,INF=1<<30;\n\n//modintのみ\n\n// from: https://gist.github.com/yosupo06/ddd51afb727600fd95d9d8ad6c3c80c9\n// (based on AtCoder STL)\n\n#include <cassert>\n#include <numeric>\n#include <type_traits>\n\nnamespace atcoder {\n \n namespace internal {\n \n#ifndef _MSC_VER\n template <class T>\n using is_signed_int128 =\n typename std::conditional<std::is_same<T, __int128_t>::value ||\n std::is_same<T, __int128>::value,\n std::true_type,\n std::false_type>::type;\n \n template <class T>\n using is_unsigned_int128 =\n typename std::conditional<std::is_same<T, __uint128_t>::value ||\n std::is_same<T, unsigned __int128>::value,\n std::true_type,\n std::false_type>::type;\n \n template <class T>\n using make_unsigned_int128 =\n typename std::conditional<std::is_same<T, __int128_t>::value,\n __uint128_t,\n unsigned __int128>;\n \n template <class T>\n using is_integral = typename std::conditional<std::is_integral<T>::value ||\n is_signed_int128<T>::value ||\n is_unsigned_int128<T>::value,\n std::true_type,\n std::false_type>::type;\n \n template <class T>\n using is_signed_int = typename std::conditional<(is_integral<T>::value &&\n std::is_signed<T>::value) ||\n is_signed_int128<T>::value,\n std::true_type,\n std::false_type>::type;\n \n template <class T>\n using is_unsigned_int =\n typename std::conditional<(is_integral<T>::value &&\n std::is_unsigned<T>::value) ||\n is_unsigned_int128<T>::value,\n std::true_type,\n std::false_type>::type;\n \n template <class T>\n using to_unsigned = typename std::conditional<\n is_signed_int128<T>::value,\n make_unsigned_int128<T>,\n typename std::conditional<std::is_signed<T>::value,\n std::make_unsigned<T>,\n std::common_type<T>>::type>::type;\n \n#else\n \n template <class T> using is_integral = typename std::is_integral<T>;\n \n template <class T>\n using is_signed_int =\n typename std::conditional<is_integral<T>::value && std::is_signed<T>::value,\n std::true_type,\n std::false_type>::type;\n \n template <class T>\n using is_unsigned_int =\n typename std::conditional<is_integral<T>::value &&\n std::is_unsigned<T>::value,\n std::true_type,\n std::false_type>::type;\n \n template <class T>\n using to_unsigned = typename std::conditional<is_signed_int<T>::value,\n std::make_unsigned<T>,\n std::common_type<T>>::type;\n \n#endif\n \n template <class T>\n using is_signed_int_t = std::enable_if_t<is_signed_int<T>::value>;\n \n template <class T>\n using is_unsigned_int_t = std::enable_if_t<is_unsigned_int<T>::value>;\n \n template <class T> using to_unsigned_t = typename to_unsigned<T>::type;\n \n } // namespace internal\n \n} // namespace atcoder\n\n#include <utility>\n\nnamespace atcoder {\n \n namespace internal {\n \n constexpr long long safe_mod(long long x, long long m) {\n x %= m;\n if (x < 0) x += m;\n return x;\n }\n \n struct barrett {\n unsigned int _m;\n unsigned long long im;\n \n barrett(unsigned int m) : _m(m), im((unsigned long long)(-1) / m + 1) {}\n \n unsigned int umod() const { return _m; }\n \n unsigned int mul(unsigned int a, unsigned int b) const {\n \n unsigned long long z = a;\n z *= b;\n#ifdef _MSC_VER\n unsigned long long x;\n _umul128(z, im, &x);\n#else\n unsigned long long x =\n (unsigned long long)(((unsigned __int128)(z)*im) >> 64);\n#endif\n unsigned int v = (unsigned int)(z - x * _m);\n if (_m <= v) v += _m;\n return v;\n }\n };\n \n constexpr long long pow_mod_constexpr(long long x, long long n, int m) {\n if (m == 1) return 0;\n unsigned int _m = (unsigned int)(m);\n unsigned long long r = 1;\n unsigned long long y = safe_mod(x, m);\n while (n) {\n if (n & 1) r = (r * y) % _m;\n y = (y * y) % _m;\n n >>= 1;\n }\n return r;\n }\n \n constexpr bool is_prime_constexpr(int n) {\n if (n <= 1) return false;\n if (n == 2 || n == 7 || n == 61) return true;\n if (n % 2 == 0) return false;\n long long d = n - 1;\n while (d % 2 == 0) d /= 2;\n for (long long a : {2, 7, 61}) {\n long long t = d;\n long long y = pow_mod_constexpr(a, t, n);\n while (t != n - 1 && y != 1 && y != n - 1) {\n y = y * y % n;\n t <<= 1;\n }\n if (y != n - 1 && t % 2 == 0) {\n return false;\n }\n }\n return true;\n }\n template <int n> constexpr bool is_prime = is_prime_constexpr(n);\n \n constexpr std::pair<long long, long long> inv_gcd(long long a, long long b) {\n a = safe_mod(a, b);\n if (a == 0) return {b, 0};\n \n long long s = b, t = a;\n long long m0 = 0, m1 = 1;\n \n while (t) {\n long long u = s / t;\n s -= t * u;\n m0 -= m1 * u; // |m1 * u| <= |m1| * s <= b\n \n \n auto tmp = s;\n s = t;\n t = tmp;\n tmp = m0;\n m0 = m1;\n m1 = tmp;\n }\n if (m0 < 0) m0 += b / s;\n return {s, m0};\n }\n \n constexpr int primitive_root_constexpr(int m) {\n if (m == 2) return 1;\n if (m == 167772161) return 3;\n if (m == 469762049) return 3;\n if (m == 754974721) return 11;\n if (m == 998244353) return 3;\n int divs[20] = {};\n divs[0] = 2;\n int cnt = 1;\n int x = (m - 1) / 2;\n while (x % 2 == 0) x /= 2;\n for (int i = 3; (long long)(i)*i <= x; i += 2) {\n if (x % i == 0) {\n divs[cnt++] = i;\n while (x % i == 0) {\n x /= i;\n }\n }\n }\n if (x > 1) {\n divs[cnt++] = x;\n }\n for (int g = 2;; g++) {\n bool ok = true;\n for (int i = 0; i < cnt; i++) {\n if (pow_mod_constexpr(g, (m - 1) / divs[i], m) == 1) {\n ok = false;\n break;\n }\n }\n if (ok) return g;\n }\n }\n template <int m> constexpr int primitive_root = primitive_root_constexpr(m);\n \n } // namespace internal\n \n} // namespace atcoder\n\n#include <cassert>\n#include <numeric>\n#include <type_traits>\n\n#ifdef _MSC_VER\n#include <intrin.h>\n#endif\n\nnamespace atcoder {\n \n namespace internal {\n \n struct modint_base {};\n struct static_modint_base : modint_base {};\n \n template <class T> using is_modint = std::is_base_of<modint_base, T>;\n template <class T> using is_modint_t = std::enable_if_t<is_modint<T>::value>;\n \n } // namespace internal\n \n template <int m, std::enable_if_t<(1 <= m)>* = nullptr>\n struct static_modint : internal::static_modint_base {\n using mint = static_modint;\n \n public:\n static constexpr int mod() { return m; }\n static mint raw(int v) {\n mint x;\n x._v = v;\n return x;\n }\n \n static_modint() : _v(0) {}\n template <class T, internal::is_signed_int_t<T>* = nullptr>\n static_modint(T v) {\n long long x = (long long)(v % (long long)(umod()));\n if (x < 0) x += umod();\n _v = (unsigned int)(x);\n }\n template <class T, internal::is_unsigned_int_t<T>* = nullptr>\n static_modint(T v) {\n _v = (unsigned int)(v % umod());\n }\n static_modint(bool v) { _v = ((unsigned int)(v) % umod()); }\n \n unsigned int val() const { return _v; }\n \n mint& operator++() {\n _v++;\n if (_v == umod()) _v = 0;\n return *this;\n }\n mint& operator--() {\n if (_v == 0) _v = umod();\n _v--;\n return *this;\n }\n mint operator++(int) {\n mint result = *this;\n ++*this;\n return result;\n }\n mint operator--(int) {\n mint result = *this;\n --*this;\n return result;\n }\n \n mint& operator+=(const mint& rhs) {\n _v += rhs._v;\n if (_v >= umod()) _v -= umod();\n return *this;\n }\n mint& operator-=(const mint& rhs) {\n _v -= rhs._v;\n if (_v >= umod()) _v += umod();\n return *this;\n }\n mint& operator*=(const mint& rhs) {\n unsigned long long z = _v;\n z *= rhs._v;\n _v = (unsigned int)(z % umod());\n return *this;\n }\n mint& operator/=(const mint& rhs) { return *this = *this * rhs.inv(); }\n \n mint operator+() const { return *this; }\n mint operator-() const { return mint() - *this; }\n \n mint pow(long long n) const {\n assert(0 <= n);\n mint x = *this, r = 1;\n while (n) {\n if (n & 1) r *= x;\n x *= x;\n n >>= 1;\n }\n return r;\n }\n mint inv() const {\n if (prime) {\n assert(_v);\n return pow(umod() - 2);\n } else {\n auto eg = internal::inv_gcd(_v, m);\n assert(eg.first == 1);\n return eg.second;\n }\n }\n \n friend mint operator+(const mint& lhs, const mint& rhs) {\n return mint(lhs) += rhs;\n }\n friend mint operator-(const mint& lhs, const mint& rhs) {\n return mint(lhs) -= rhs;\n }\n friend mint operator*(const mint& lhs, const mint& rhs) {\n return mint(lhs) *= rhs;\n }\n friend mint operator/(const mint& lhs, const mint& rhs) {\n return mint(lhs) /= rhs;\n }\n friend bool operator==(const mint& lhs, const mint& rhs) {\n return lhs._v == rhs._v;\n }\n friend bool operator!=(const mint& lhs, const mint& rhs) {\n return lhs._v != rhs._v;\n }\n \n private:\n unsigned int _v;\n static constexpr unsigned int umod() { return m; }\n static constexpr bool prime = internal::is_prime<m>;\n };\n \n template <int id> struct dynamic_modint : internal::modint_base {\n using mint = dynamic_modint;\n \n public:\n static int mod() { return (int)(bt.umod()); }\n static void set_mod(int m) {\n assert(1 <= m);\n bt = internal::barrett(m);\n }\n static mint raw(int v) {\n mint x;\n x._v = v;\n return x;\n }\n \n dynamic_modint() : _v(0) {}\n template <class T, internal::is_signed_int_t<T>* = nullptr>\n dynamic_modint(T v) {\n long long x = (long long)(v % (long long)(mod()));\n if (x < 0) x += mod();\n _v = (unsigned int)(x);\n }\n template <class T, internal::is_unsigned_int_t<T>* = nullptr>\n dynamic_modint(T v) {\n _v = (unsigned int)(v % mod());\n }\n dynamic_modint(bool v) { _v = ((unsigned int)(v) % mod()); }\n \n unsigned int val() const { return _v; }\n \n mint& operator++() {\n _v++;\n if (_v == umod()) _v = 0;\n return *this;\n }\n mint& operator--() {\n if (_v == 0) _v = umod();\n _v--;\n return *this;\n }\n mint operator++(int) {\n mint result = *this;\n ++*this;\n return result;\n }\n mint operator--(int) {\n mint result = *this;\n --*this;\n return result;\n }\n \n mint& operator+=(const mint& rhs) {\n _v += rhs._v;\n if (_v >= umod()) _v -= umod();\n return *this;\n }\n mint& operator-=(const mint& rhs) {\n _v += mod() - rhs._v;\n if (_v >= umod()) _v -= umod();\n return *this;\n }\n mint& operator*=(const mint& rhs) {\n _v = bt.mul(_v, rhs._v);\n return *this;\n }\n mint& operator/=(const mint& rhs) { return *this = *this * rhs.inv(); }\n \n mint operator+() const { return *this; }\n mint operator-() const { return mint() - *this; }\n \n mint pow(long long n) const {\n assert(0 <= n);\n mint x = *this, r = 1;\n while (n) {\n if (n & 1) r *= x;\n x *= x;\n n >>= 1;\n }\n return r;\n }\n mint inv() const {\n auto eg = internal::inv_gcd(_v, mod());\n assert(eg.first == 1);\n return eg.second;\n }\n \n friend mint operator+(const mint& lhs, const mint& rhs) {\n return mint(lhs) += rhs;\n }\n friend mint operator-(const mint& lhs, const mint& rhs) {\n return mint(lhs) -= rhs;\n }\n friend mint operator*(const mint& lhs, const mint& rhs) {\n return mint(lhs) *= rhs;\n }\n friend mint operator/(const mint& lhs, const mint& rhs) {\n return mint(lhs) /= rhs;\n }\n friend bool operator==(const mint& lhs, const mint& rhs) {\n return lhs._v == rhs._v;\n }\n friend bool operator!=(const mint& lhs, const mint& rhs) {\n return lhs._v != rhs._v;\n }\n \n private:\n unsigned int _v;\n static internal::barrett bt;\n static unsigned int umod() { return bt.umod(); }\n };\n template <int id> internal::barrett dynamic_modint<id>::bt = 998244353;\n \n using modint998244353 = static_modint<998244353>;\n using modint1000000007 = static_modint<1000000007>;\n using modint = dynamic_modint<-1>;\n \n namespace internal {\n \n template <class T>\n using is_static_modint = std::is_base_of<internal::static_modint_base, T>;\n \n template <class T>\n using is_static_modint_t = std::enable_if_t<is_static_modint<T>::value>;\n \n template <class> struct is_dynamic_modint : public std::false_type {};\n template <int id>\n struct is_dynamic_modint<dynamic_modint<id>> : public std::true_type {};\n \n template <class T>\n using is_dynamic_modint_t = std::enable_if_t<is_dynamic_modint<T>::value>;\n \n } // namespace internal\n \n} // namespace atcoder\n\nusing mint=atcoder::modint;\n\nusing mat=vector<vector<mint>>;\nusing vec=vector<mint>;\n\nmat TEN;\n\nmat add(mat &a,mat &b){\n assert(si(a)==si(b));\n assert(si(a[0])==si(b[0]));\n mat c(si(a),vec(si(a[0])));\n for(int i=0;i<si(c);i++){\n for(int j=0;j<si(c[0]);j++){\n c[i][j]=a[i][j]+b[i][j];\n }\n }\n return c;\n}\n\nmat sub(mat &a,mat &b){\n assert(si(a)==si(b));\n assert(si(a[0])==si(b[0]));\n mat c(si(a),vec(si(a[0])));\n for(int i=0;i<si(c);i++){\n for(int j=0;j<si(c[0]);j++){\n c[i][j]=a[i][j]-b[i][j];\n }\n }\n return c;\n}\n\nmat mul(mat &a,mat &b){\n if(si(a[0])==si(b)){\n mat c(si(a),vec(si(b[0])));\n for(int i=0;i<si(c);i++){\n for(int j=0;j<si(c[0]);j++){\n for(int k=0;k<si(a[0]);k++){\n c[i][j]+=a[i][k]*b[k][j];\n }\n }\n }\n return c;\n }else if(si(b)==1&&si(b[0])==1){\n mat c(si(a),vec(si(a[0])));\n for(int i=0;i<si(c);i++){\n for(int j=0;j<si(c[0]);j++){\n c[i][j]=a[i][j]*b[0][0];\n }\n }\n return c;\n }else if(si(a)==1&&si(a[0])==1){\n mat c(si(b),vec(si(b[0])));\n for(int i=0;i<si(c);i++){\n for(int j=0;j<si(c[0]);j++){\n c[i][j]=b[i][j]*a[0][0];\n }\n }\n return c;\n }else{\n assert(false);\n return TEN;\n }\n}\n\nmat neg(mat &a){\n mat c(si(a),vec(si(a[0])));\n for(int i=0;i<si(c);i++){\n for(int j=0;j<si(c[0]);j++){\n c[i][j]=-a[i][j];\n }\n }\n return c;\n}\n\nmap<char,mat> MA;\nstring S;\n\nmat assignment(int &j);\nchar var(int &j);\nmat var2(int &j);\nmat expr(int &j);\nmat term(int &j);\nmat factor(int &j);\nmat primary(int &j);\nmat primary_sub(int &j);\nmat matrix(int &j);\nmat row_seq(int &j);\nmat row(int &j);\nmat inum(int &j);\nmat digit(int &j);\n\nmat assignment(int &j){\n char c=var(j);\n assert(S[j]=='=');\n j++;\n auto res=expr(j);\n assert(S[j]=='.');\n return MA[c]=res;\n}\n\nchar var(int &j){\n char res=S[j];\n j++;\n return res;\n}\n\nmat var2(int &j){\n char c=S[j];\n assert(MA.count(c));\n j++;\n return MA[c];\n}\n\nmat expr(int &j){\n auto res=term(j);\n while(j<si(S)){\n char c=S[j];\n if(c=='+'){\n j++;\n auto X=term(j);\n res=add(res,X);\n }else if(c=='-'){\n j++;\n auto X=term(j);\n res=sub(res,X);\n }else{\n break;\n }\n }\n return res;\n}\n\nmat term(int &j){\n auto res=factor(j);\n while(j<si(S)){\n char c=S[j];\n if(c=='*'){\n j++;\n auto X=factor(j);\n res=mul(res,X);\n }else{\n break;\n }\n }\n return res;\n}\n\nmat factor(int &j){\n if(S[j]=='-'){\n j++;\n auto res=factor(j);\n return neg(res);\n }else{\n return primary(j);\n }\n}\n\nmat primary(int &j){\n auto res=primary_sub(j);\n while(j<si(S)){\n if(S[j]=='('){\n j++;\n auto H=expr(j);\n assert(S[j]==',');\n j++;\n auto W=expr(j);\n assert(S[j]==')');\n j++;\n mat X(si(H[0]),vec(si(W[0])));\n for(int h=0;h<si(H[0]);h++){\n for(int w=0;w<si(W[0]);w++){\n X[h][w]=res[H[0][h].val()-1][W[0][w].val()-1];\n }\n }\n res=X;\n }else if(S[j]=='\\''){\n j++;\n mat X(si(res[0]),vec(si(res)));\n for(int h=0;h<si(res);h++){\n for(int w=0;w<si(res[0]);w++){\n X[w][h]=res[h][w];\n }\n }\n res=X;\n }else{\n break;\n }\n }\n \n return res;\n}\n\nmat primary_sub(int &j){\n if('0'<=S[j]&&S[j]<='9'){\n return inum(j);\n }else if('A'<=S[j]&&S[j]<='Z'){\n return var2(j);\n }else if(S[j]=='['){\n return matrix(j);\n }else if(S[j]=='('){\n j++;\n auto res=expr(j);\n assert(S[j]==')');\n j++;\n return res;\n }else{\n assert(false);\n return TEN;\n }\n}\n\nmat matrix(int &j){\n j++;\n auto res=row_seq(j);\n assert(S[j]==']');\n j++;\n return res;\n}\n\nmat row_seq(int &j){\n auto res=row(j);\n while(j<si(S)){\n if(S[j]==';'){\n j++;\n auto X=row(j);\n for(int h=0;h<si(X);h++) res.push_back(X[h]);\n }else break;\n }\n return res;\n}\n\nmat row(int &j){\n auto res=expr(j);\n while(j<si(S)){\n if(S[j]==' '){\n j++;\n auto X=expr(j);\n for(int h=0;h<si(X);h++){\n for(int w=0;w<si(X[h]);w++){\n res[h].push_back(X[h][w]);\n }\n }\n }else{\n break;\n }\n }\n return res;\n}\n\nmat inum(int &j){\n mat res=digit(j);\n while(j<si(S)){\n if('0'<=S[j]&&S[j]<='9'){\n res=mul(res,TEN);\n auto X=digit(j);\n res=add(res,X);\n }else{\n break;\n }\n }\n return res;\n}\n\nmat digit(int &j){\n mint a=S[j]-'0';\n mat res={{a}};\n j++;\n return res;\n}\n\nint main() {\n \n std::ifstream in(\"text.txt\");\n std::cin.rdbuf(in.rdbuf());\n cin.tie(0);\n ios::sync_with_stdio(false);\n \n mint::set_mod(32768);\n \n TEN.push_back({});\n TEN.back().push_back(10);\n \n while(1){\n string N;\n getline(cin,N);\n if(N==\"0\") break;\n int NN=stoi(N);\n MA.clear();\n for(int i=0;i<NN;i++){\n getline(cin,S);\n int j=0;\n auto res=assignment(j);\n for(int h=0;h<si(res);h++){\n for(int w=0;w<si(res[h]);w++){\n if(w) cout<<\" \";\n cout<<res[h][w].val();\n }\n cout<<\"\\n\";\n }\n }\n \n cout<<\"-----\\n\";\n }\n \n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3672, "score_of_the_acc": -0.1193, "final_rank": 7 }, { "submission_id": "aoj_1314_5998895", "code_snippet": "#include <bits/stdc++.h>\n\nusing namespace std;\n\ntemplate< class T >\nstruct Matrix {\n vector< vector< T > > A;\n\n Matrix() {}\n\n Matrix(size_t n, size_t m) : A(n, vector< T >(m, 0)) {}\n\n Matrix(size_t n) : A(n, vector< T >(n, 0)) {};\n\n size_t height() const {\n return (A.size());\n }\n\n size_t width() const {\n return (A[0].size());\n }\n\n inline const vector< T > &operator[](int k) const {\n return (A.at(k));\n }\n\n inline vector< T > &operator[](int k) {\n return (A.at(k));\n }\n\n static Matrix I(size_t n) {\n Matrix mat(n);\n for(int i = 0; i < n; i++) mat[i][i] = 1;\n return (mat);\n }\n\n Matrix &operator+=(const Matrix &B) {\n size_t n = height(), m = width();\n assert(n == B.height() && m == B.width());\n for(int i = 0; i < n; i++)\n for(int j = 0; j < m; j++)\n (*this)[i][j] += B[i][j];\n return (*this);\n }\n\n Matrix &operator-=(const Matrix &B) {\n size_t n = height(), m = width();\n assert(n == B.height() && m == B.width());\n for(int i = 0; i < n; i++)\n for(int j = 0; j < m; j++)\n (*this)[i][j] -= B[i][j];\n return (*this);\n }\n\n Matrix &operator*=(const Matrix &B) {\n if (B.height() == 1 and B.width() == 1){\n auto res = *this * B[0][0];\n this->A = res.A;\n return (*this);\n }\n\n if (this->height() == 1 and this->width() == 1){\n auto res = (*this)[0][0] * B;\n this->A = res.A;\n return (*this);\n }\n\n size_t n = height(), m = B.width(), p = width();\n assert(p == B.height());\n vector< vector< T > > C(n, vector< T >(m, 0));\n for(int i = 0; i < n; i++)\n for(int j = 0; j < m; j++)\n for(int k = 0; k < p; k++)\n C[i][j] = (C[i][j] + (*this)[i][k] * B[k][j]);\n A.swap(C);\n return (*this);\n }\n\n Matrix &operator^=(long long k) {\n Matrix B = Matrix::I(height());\n while(k > 0) {\n if(k & 1) B *= *this;\n *this *= *this;\n k >>= 1LL;\n }\n A.swap(B.A);\n return (*this);\n }\n\n Matrix operator+(const Matrix &B) const {\n return (Matrix(*this) += B);\n }\n\n Matrix operator-(const Matrix &B) const {\n return (Matrix(*this) -= B);\n }\n\n Matrix operator*(const Matrix &B) const {\n if (this->height() == 1 and this->width() == 1)\n return (*this)[0][0] * B;\n return (Matrix(*this) *= B);\n }\n\n Matrix operator^(const long long k) const {\n return (Matrix(*this) ^= k);\n }\n\n Matrix operator*(const T &k) const {\n auto res = Matrix(*this);\n for (int i = 0; i < res.height(); i++){\n for (int j = 0; j < res.width(); j++)\n res[i][j] *= k;\n }\n return res;\n }\n\n friend Matrix operator*(const T &k, const Matrix &p) {\n auto res = Matrix(p);\n for (int i = 0; i < res.height(); i++){\n for (int j = 0; j < res.width(); j++)\n res[i][j] *= k;\n }\n return res;\n }\n\n friend Matrix concat_v(const Matrix &lhs, const Matrix &rhs){\n assert(lhs.width() == rhs.width());\n Matrix res(lhs.height() + rhs.height(), lhs.width());\n for (int i = 0; i < lhs.height(); i++){\n for (int j = 0; j < lhs.width(); j++)\n res[i][j] = lhs[i][j];\n }\n for (int i = 0; i < rhs.height(); i++){\n for (int j = 0; j < rhs.width(); j++)\n res[lhs.height() + i][j] = rhs[i][j];\n }\n\n return res;\n }\n\n friend Matrix concat_w(const Matrix &lhs, const Matrix &rhs){\n assert(lhs.height() == rhs.height());\n Matrix res(lhs.height(), lhs.width() + rhs.width());\n for (int i = 0; i < lhs.height(); i++){\n for (int j = 0; j < lhs.width(); j++)\n res[i][j] = lhs[i][j];\n }\n for (int i = 0; i < rhs.height(); i++){\n for (int j = 0; j < rhs.width(); j++)\n res[i][lhs.width() + j] = rhs[i][j];\n }\n\n return res;\n }\n\n Matrix t(){\n Matrix res(this->width(), this->height());\n for (int i = 0; i < this->height(); i++){\n for (int j = 0; j < this->width(); j++)\n res[j][i] = (*this)[i][j];\n }\n\n return res;\n }\n\n friend ostream &operator<<(ostream &os, Matrix &p) {\n size_t n = p.height(), m = p.width();\n for(int i = 0; i < n; i++) {\n for(int j = 0; j < m; j++) {\n os << p[i][j] << (j + 1 == m ? \"\\n\" : \" \");\n }\n }\n return (os);\n }\n\n\n T determinant() {\n Matrix B(*this);\n assert(width() == height());\n T ret = 1;\n for(int i = 0; i < width(); i++) {\n int idx = -1;\n for(int j = i; j < width(); j++) {\n if(B[j][i] != 0) idx = j;\n }\n if(idx == -1) return (0);\n if(i != idx) {\n ret *= -1;\n swap(B[i], B[idx]);\n }\n ret *= B[i][i];\n T vv = B[i][i];\n for(int j = 0; j < width(); j++) {\n B[i][j] /= vv;\n }\n for(int j = i + 1; j < width(); j++) {\n T a = B[j][i];\n for(int k = 0; k < width(); k++) {\n B[j][k] -= B[i][k] * a;\n }\n }\n }\n return (ret);\n }\n};\n\ntemplate <std::uint_fast64_t Modulus> class modint {\n using u64 = std::uint_fast64_t;\n u64 a;\n\n public:\n\n template <class INT>\n constexpr modint(const INT x = 0) noexcept : a(x >= 0 ? x % Modulus : x % int_fast64_t(Modulus) + Modulus) {}\n constexpr modint(const u64 x = 0) noexcept : a(x % Modulus) {}\n constexpr u64 &value() noexcept { return a; }\n constexpr const u64 &value() const noexcept { return a; }\n constexpr modint operator+(const modint rhs) const noexcept {\n return modint(*this) += rhs;\n }\n constexpr modint operator-(const modint rhs) const noexcept {\n return modint(*this) -= rhs;\n }\n constexpr modint operator*(const modint rhs) const noexcept {\n return modint(*this) *= rhs;\n }\n constexpr modint operator/(const modint rhs) const noexcept {\n return modint(*this) /= rhs;\n }\n constexpr modint &operator+=(const modint rhs) noexcept {\n a += rhs.a;\n if (a >= Modulus) {\n a -= Modulus;\n }\n return *this;\n }\n constexpr modint &operator-=(const modint rhs) noexcept {\n if (a < rhs.a) {\n a += Modulus;\n }\n a -= rhs.a;\n return *this;\n }\n constexpr modint &operator*=(const modint rhs) noexcept {\n a = a * rhs.a % Modulus;\n return *this;\n }\n constexpr modint &operator/=(modint rhs) noexcept {\n u64 exp = Modulus - 2;\n while (exp) {\n if (exp % 2) {\n *this *= rhs;\n }\n rhs *= rhs;\n exp /= 2;\n }\n return *this;\n }\n\n constexpr bool operator<(const modint& rhs) const noexcept {return this->a < rhs.a;}\n constexpr bool operator>(const modint& rhs) const noexcept {return rhs < *this;}\n constexpr bool operator<=(const modint& rhs) const noexcept {return !(*this > rhs);}\n constexpr bool operator>=(const modint& rhs) const noexcept {return !(*this < rhs);}\n constexpr bool operator==(const modint& rhs) const noexcept {return this->a == rhs.a;}\n constexpr bool operator!=(const modint& rhs) const noexcept {return !(*this == rhs);}\n\n constexpr modint& operator++() noexcept {\n *this += modint(1);\n return *this;\n }\n constexpr modint operator++(int) noexcept {\n modint tmp(*this);\n ++(*this);\n return tmp;\n }\n constexpr modint& operator--() noexcept {\n *this -= modint(1);\n return *this;\n }\n constexpr modint operator--(int) noexcept {\n modint tmp(*this);\n --(*this);\n return tmp;\n }\n constexpr modint operator-() const noexcept {\n return modint(0) - *this;\n }\n\n template <std::uint_fast64_t M>\n friend constexpr std::ostream& operator<<(std::ostream& os, const modint<M>& rhs) noexcept {\n os << rhs.a;\n return os;\n }\n template <std::uint_fast64_t M>\n friend constexpr std::istream& operator>>(std::istream& is, modint<M>& rhs) noexcept {\n int64_t tmp;\n is >> tmp;\n rhs = modint(tmp);\n return is;\n }\n\n constexpr modint pow(const u64 k) const noexcept {\n if (k == 0)\n return 1;\n if (k % 2 == 0){\n modint res = pow(k / 2);\n return res * res;\n }\n return pow(k - 1) * modint(*this);\n }\n\n template <typename T>\n operator const T (){return a;}\n\n};\n\nusing mint = modint<32768>;\n\nclass Parser{\n string S;\n int pos = 0;\n map<char, Matrix<mint>> vars;\n public:\n Parser(string T) : S(T){}\n\n void set_s(string s){\n S = s;\n pos = 0;\n }\n\n char peek(){\n // if (pos < S.size())\n // cerr << S[pos] << endl;\n return pos < S.size() ? S[pos] : 'a';\n }\n\n bool is_number(char c){\n return '0' <= c and c <= '9';\n }\n\n bool is_A_to_Z(char c){\n return 'A' <= c and c <= 'Z';\n }\n\n void skip_spaces(){\n while (peek() == ' ')\n pos++;\n }\n\n void next(bool state){\n assert(state);\n pos++;\n }\n\n Matrix<mint> assignment(){\n char var_c = var();\n next(peek() == '=');\n vars[var_c] = expr();\n next(peek() == '.');\n \n return vars[var_c];\n }\n\n char var(){\n char res = peek();\n next('A' <= peek() and peek() <= 'Z');\n return res;\n }\n\n Matrix<mint> expr(){\n Matrix<mint> res = term();\n while (peek() == '+' or peek() == '-'){\n if (peek() == '+'){\n pos++;\n res += term();\n } else if (peek() == '-'){\n pos++;\n res -= term();\n }\n }\n return res;\n }\n\n Matrix<mint> term(){\n Matrix<mint> res = factor();\n while (peek() == '*'){\n pos++;\n res *= factor();\n }\n\n return res;\n }\n\n Matrix<mint> factor(){\n if (peek() == '-'){\n pos++;\n return mint(-1) * factor();\n }\n\n return primary();\n }\n\n Matrix<mint> primary(){\n auto res = Matrix<mint>(1, 1);\n res[0][0] = -1;\n if (is_number(peek())){\n res[0][0] = number();\n } else if (is_A_to_Z(peek())){\n auto v = var();\n res = vars[v];\n } else if (peek() == '['){\n res = matrix();\n } else if (peek() == '('){\n pos++;\n res = expr();\n next(peek() == ')');\n }\n\n while (is_index() or peek() == '\\''){\n if (peek() == '\\''){\n pos++;\n res = res.t();\n } else if (is_index()){\n next(peek() == '(');\n auto left = expr();\n next(peek() == ',');\n auto right = expr();\n next(peek() == ')');\n Matrix<mint> res2(left.width(), right.width());\n for (int i = 0; i < left.width(); i++){\n for (int j = 0; j < right.width(); j++)\n res2[i][j] = res[left[0][i] - 1][right[0][j] - 1];\n }\n res = res2;\n }\n }\n\n return res;\n }\n\n bool is_index(){\n if (peek() != '(')\n return false;\n int c = 0;\n for (int i = pos; i < S.size(); i++){\n if (S[i] == '(')\n c++;\n if (S[i] == ')'){\n c--;\n if (c == 0)\n break;\n }\n if (S[i] == ',' and c == 1)\n return true;\n }\n return false;\n }\n\n Matrix<mint> matrix(){\n next(peek() == '[');\n auto res = row_seq();\n next(peek() == ']');\n return res;\n }\n\n Matrix<mint> row_seq(){\n auto res = row();\n while (peek() == ';'){\n pos++;\n res = concat_v(res, row());\n }\n return res;\n }\n\n Matrix<mint> row(){\n auto res = expr();\n while (peek() == ' '){\n pos++;\n res = concat_w(res, expr());\n }\n return res;\n }\n\n mint number(){\n mint res = 0;\n while (is_number(peek())){\n res = res * 10 + peek() - '0';\n pos++;\n }\n return res;\n }\n};\n\nvoid solve(int n){\n string stash;\n getline(cin, stash);\n Parser parser(\"\");\n for (int i = 0; i < n; i++){\n string s;\n getline(cin, s);\n parser.set_s(s);\n auto res = parser.assignment();\n cout << res;\n }\n puts(\"-----\");\n}\n\nint main(){\n int n;\n while (true){\n cin >> n;\n if (n == 0)\n break;\n solve(n);\n }\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3952, "score_of_the_acc": -0.1625, "final_rank": 15 }, { "submission_id": "aoj_1314_5998887", "code_snippet": "#include <bits/stdc++.h>\n\nusing namespace std;\n\ntemplate< class T >\nstruct Matrix {\n vector< vector< T > > A;\n\n Matrix() {}\n\n Matrix(size_t n, size_t m) : A(n, vector< T >(m, 0)) {}\n\n Matrix(size_t n) : A(n, vector< T >(n, 0)) {};\n\n size_t height() const {\n return (A.size());\n }\n\n size_t width() const {\n return (A[0].size());\n }\n\n inline const vector< T > &operator[](int k) const {\n return (A.at(k));\n }\n\n inline vector< T > &operator[](int k) {\n return (A.at(k));\n }\n\n static Matrix I(size_t n) {\n Matrix mat(n);\n for(int i = 0; i < n; i++) mat[i][i] = 1;\n return (mat);\n }\n\n Matrix &operator+=(const Matrix &B) {\n size_t n = height(), m = width();\n assert(n == B.height() && m == B.width());\n for(int i = 0; i < n; i++)\n for(int j = 0; j < m; j++)\n (*this)[i][j] += B[i][j];\n return (*this);\n }\n\n Matrix &operator-=(const Matrix &B) {\n size_t n = height(), m = width();\n assert(n == B.height() && m == B.width());\n for(int i = 0; i < n; i++)\n for(int j = 0; j < m; j++)\n (*this)[i][j] -= B[i][j];\n return (*this);\n }\n\n Matrix &operator*=(const Matrix &B) {\n if (B.height() == 1 and B.width() == 1){\n auto res = *this * B[0][0];\n this->A = res.A;\n return (*this);\n }\n\n if (this->height() == 1 and this->width() == 1){\n auto res = (*this)[0][0] * B;\n this->A = res.A;\n return (*this);\n }\n\n size_t n = height(), m = B.width(), p = width();\n assert(p == B.height());\n vector< vector< T > > C(n, vector< T >(m, 0));\n for(int i = 0; i < n; i++)\n for(int j = 0; j < m; j++)\n for(int k = 0; k < p; k++)\n C[i][j] = (C[i][j] + (*this)[i][k] * B[k][j]);\n A.swap(C);\n return (*this);\n }\n\n Matrix &operator^=(long long k) {\n Matrix B = Matrix::I(height());\n while(k > 0) {\n if(k & 1) B *= *this;\n *this *= *this;\n k >>= 1LL;\n }\n A.swap(B.A);\n return (*this);\n }\n\n Matrix operator+(const Matrix &B) const {\n return (Matrix(*this) += B);\n }\n\n Matrix operator-(const Matrix &B) const {\n return (Matrix(*this) -= B);\n }\n\n Matrix operator*(const Matrix &B) const {\n if (this->height() == 1 and this->width() == 1)\n return (*this)[0][0] * B;\n return (Matrix(*this) *= B);\n }\n\n Matrix operator^(const long long k) const {\n return (Matrix(*this) ^= k);\n }\n\n Matrix operator*(const T &k) const {\n auto res = Matrix(*this);\n for (int i = 0; i < res.height(); i++){\n for (int j = 0; j < res.width(); j++)\n res[i][j] *= k;\n }\n return res;\n }\n\n friend Matrix operator*(const T &k, const Matrix &p) {\n auto res = Matrix(p);\n for (int i = 0; i < res.height(); i++){\n for (int j = 0; j < res.width(); j++)\n res[i][j] *= k;\n }\n return res;\n }\n\n friend Matrix concat_v(const Matrix &lhs, const Matrix &rhs){\n assert(lhs.width() == rhs.width());\n Matrix res(lhs.height() + rhs.height(), lhs.width());\n for (int i = 0; i < lhs.height(); i++){\n for (int j = 0; j < lhs.width(); j++)\n res[i][j] = lhs[i][j];\n }\n for (int i = 0; i < rhs.height(); i++){\n for (int j = 0; j < rhs.width(); j++)\n res[lhs.height() + i][j] = rhs[i][j];\n }\n\n return res;\n }\n\n friend Matrix concat_w(const Matrix &lhs, const Matrix &rhs){\n assert(lhs.height() == rhs.height());\n Matrix res(lhs.height(), lhs.width() + rhs.width());\n for (int i = 0; i < lhs.height(); i++){\n for (int j = 0; j < lhs.width(); j++)\n res[i][j] = lhs[i][j];\n }\n for (int i = 0; i < rhs.height(); i++){\n for (int j = 0; j < rhs.width(); j++)\n res[i][lhs.width() + j] = rhs[i][j];\n }\n\n return res;\n }\n\n Matrix t(){\n Matrix res(this->width(), this->height());\n for (int i = 0; i < this->height(); i++){\n for (int j = 0; j < this->width(); j++)\n res[j][i] = (*this)[i][j];\n }\n\n return res;\n }\n\n friend ostream &operator<<(ostream &os, Matrix &p) {\n size_t n = p.height(), m = p.width();\n for(int i = 0; i < n; i++) {\n for(int j = 0; j < m; j++) {\n os << p[i][j] << (j + 1 == m ? \"\\n\" : \" \");\n }\n }\n return (os);\n }\n\n\n T determinant() {\n Matrix B(*this);\n assert(width() == height());\n T ret = 1;\n for(int i = 0; i < width(); i++) {\n int idx = -1;\n for(int j = i; j < width(); j++) {\n if(B[j][i] != 0) idx = j;\n }\n if(idx == -1) return (0);\n if(i != idx) {\n ret *= -1;\n swap(B[i], B[idx]);\n }\n ret *= B[i][i];\n T vv = B[i][i];\n for(int j = 0; j < width(); j++) {\n B[i][j] /= vv;\n }\n for(int j = i + 1; j < width(); j++) {\n T a = B[j][i];\n for(int k = 0; k < width(); k++) {\n B[j][k] -= B[i][k] * a;\n }\n }\n }\n return (ret);\n }\n};\n\ntemplate <std::uint_fast64_t Modulus> class modint {\n using u64 = std::uint_fast64_t;\n u64 a;\n\n public:\n\n template <class INT>\n constexpr modint(const INT x = 0) noexcept : a(x >= 0 ? x % Modulus : x % int_fast64_t(Modulus) + Modulus) {}\n constexpr modint(const u64 x = 0) noexcept : a(x % Modulus) {}\n constexpr u64 &value() noexcept { return a; }\n constexpr const u64 &value() const noexcept { return a; }\n constexpr modint operator+(const modint rhs) const noexcept {\n return modint(*this) += rhs;\n }\n constexpr modint operator-(const modint rhs) const noexcept {\n return modint(*this) -= rhs;\n }\n constexpr modint operator*(const modint rhs) const noexcept {\n return modint(*this) *= rhs;\n }\n constexpr modint operator/(const modint rhs) const noexcept {\n return modint(*this) /= rhs;\n }\n constexpr modint &operator+=(const modint rhs) noexcept {\n a += rhs.a;\n if (a >= Modulus) {\n a -= Modulus;\n }\n return *this;\n }\n constexpr modint &operator-=(const modint rhs) noexcept {\n if (a < rhs.a) {\n a += Modulus;\n }\n a -= rhs.a;\n return *this;\n }\n constexpr modint &operator*=(const modint rhs) noexcept {\n a = a * rhs.a % Modulus;\n return *this;\n }\n constexpr modint &operator/=(modint rhs) noexcept {\n u64 exp = Modulus - 2;\n while (exp) {\n if (exp % 2) {\n *this *= rhs;\n }\n rhs *= rhs;\n exp /= 2;\n }\n return *this;\n }\n\n constexpr bool operator<(const modint& rhs) const noexcept {return this->a < rhs.a;}\n constexpr bool operator>(const modint& rhs) const noexcept {return rhs < *this;}\n constexpr bool operator<=(const modint& rhs) const noexcept {return !(*this > rhs);}\n constexpr bool operator>=(const modint& rhs) const noexcept {return !(*this < rhs);}\n constexpr bool operator==(const modint& rhs) const noexcept {return this->a == rhs.a;}\n constexpr bool operator!=(const modint& rhs) const noexcept {return !(*this == rhs);}\n\n constexpr modint& operator++() noexcept {\n *this += modint(1);\n return *this;\n }\n constexpr modint operator++(int) noexcept {\n modint tmp(*this);\n ++(*this);\n return tmp;\n }\n constexpr modint& operator--() noexcept {\n *this -= modint(1);\n return *this;\n }\n constexpr modint operator--(int) noexcept {\n modint tmp(*this);\n --(*this);\n return tmp;\n }\n constexpr modint operator-() const noexcept {\n return modint(0) - *this;\n }\n\n template <std::uint_fast64_t M>\n friend constexpr std::ostream& operator<<(std::ostream& os, const modint<M>& rhs) noexcept {\n os << rhs.a;\n return os;\n }\n template <std::uint_fast64_t M>\n friend constexpr std::istream& operator>>(std::istream& is, modint<M>& rhs) noexcept {\n int64_t tmp;\n is >> tmp;\n rhs = modint(tmp);\n return is;\n }\n\n constexpr modint pow(const u64 k) const noexcept {\n if (k == 0)\n return 1;\n if (k % 2 == 0){\n modint res = pow(k / 2);\n return res * res;\n }\n return pow(k - 1) * modint(*this);\n }\n\n template <typename T>\n operator const T (){return a;}\n\n};\n\nusing mint = modint<32768>;\n\nclass Parser{\n string S;\n int pos = 0;\n map<char, Matrix<mint>> vars;\n public:\n Parser(string T) : S(T){}\n\n void set_s(string s){\n S = s;\n pos = 0;\n }\n\n char peek(){\n // if (pos < S.size())\n // cerr << S[pos] << endl;\n return pos < S.size() ? S[pos] : 'a';\n }\n\n bool is_number(char c){\n return '0' <= c and c <= '9';\n }\n\n bool is_A_to_Z(char c){\n return 'A' <= c and c <= 'Z';\n }\n\n void skip_spaces(){\n while (peek() == ' ')\n pos++;\n }\n\n Matrix<mint> assignment(){\n char var_c = var();\n assert(peek() == '=');\n pos++;\n vars[var_c] = expr();\n assert(peek() == '.');\n pos++;\n\n return vars[var_c];\n }\n\n char var(){\n assert('A' <= peek() and peek() <= 'Z');\n char res = peek();\n pos++;\n return res;\n }\n\n Matrix<mint> expr(){\n Matrix<mint> res = term();\n while (peek() == '+' or peek() == '-'){\n if (peek() == '+'){\n pos++;\n res += term();\n } else if (peek() == '-'){\n pos++;\n res -= term();\n }\n }\n return res;\n }\n\n Matrix<mint> term(){\n Matrix<mint> res = factor();\n while (peek() == '*'){\n pos++;\n res *= factor();\n }\n\n return res;\n }\n\n Matrix<mint> factor(){\n if (peek() == '-'){\n pos++;\n return mint(-1) * factor();\n }\n\n return primary();\n }\n\n Matrix<mint> primary(){\n auto res = Matrix<mint>(1, 1);\n res[0][0] = -1;\n if (is_number(peek())){\n res[0][0] = number();\n } else if (is_A_to_Z(peek())){\n auto v = var();\n res = vars[v];\n } else if (peek() == '['){\n res = matrix();\n } else if (peek() == '('){\n pos++;\n res = expr();\n assert(peek() == ')');\n pos++;\n }\n\n while (is_index() or peek() == '\\''){\n if (peek() == '\\''){\n pos++;\n res = res.t();\n } else if (is_index()){\n assert(peek() == '(');\n pos++;\n auto left = expr();\n assert(peek() == ',');\n pos++;\n auto right = expr();\n assert(peek() == ')');\n pos++;\n Matrix<mint> res2(left.width(), right.width());\n for (int i = 0; i < left.width(); i++){\n for (int j = 0; j < right.width(); j++)\n res2[i][j] = res[left[0][i] - 1][right[0][j] - 1];\n }\n res = res2;\n }\n }\n\n return res;\n }\n\n bool is_index(){\n if (peek() != '(')\n return false;\n int c = 0;\n for (int i = pos; i < S.size(); i++){\n if (S[i] == '(')\n c++;\n if (S[i] == ')'){\n c--;\n if (c == 0)\n break;\n }\n if (S[i] == ',' and c == 1)\n return true;\n }\n return false;\n }\n\n Matrix<mint> matrix(){\n assert(peek() == '[');\n pos++;\n auto res = row_seq();\n assert(peek() == ']');\n pos++;\n return res;\n }\n\n Matrix<mint> row_seq(){\n auto res = row();\n while (peek() == ';'){\n pos++;\n res = concat_v(res, row());\n }\n return res;\n }\n\n Matrix<mint> row(){\n auto res = expr();\n while (peek() == ' '){\n pos++;\n res = concat_w(res, expr());\n }\n return res;\n }\n\n mint number(){\n mint res = 0;\n while (is_number(peek())){\n res = res * 10 + peek() - '0';\n pos++;\n }\n return res;\n }\n};\n\nvoid solve(int n){\n string stash;\n getline(cin, stash);\n Parser parser(\"\");\n for (int i = 0; i < n; i++){\n string s;\n getline(cin, s);\n parser.set_s(s);\n auto res = parser.assignment();\n cout << res;\n }\n puts(\"-----\");\n}\n\nint main(){\n int n;\n while (true){\n cin >> n;\n if (n == 0)\n break;\n solve(n);\n }\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3784, "score_of_the_acc": -0.1366, "final_rank": 14 }, { "submission_id": "aoj_1314_5998884", "code_snippet": "#include <bits/stdc++.h>\n\nusing namespace std;\n\ntemplate< class T >\nstruct Matrix {\n vector< vector< T > > A;\n\n Matrix() {}\n\n Matrix(size_t n, size_t m) : A(n, vector< T >(m, 0)) {}\n\n Matrix(size_t n) : A(n, vector< T >(n, 0)) {};\n\n size_t height() const {\n return (A.size());\n }\n\n size_t width() const {\n return (A[0].size());\n }\n\n inline const vector< T > &operator[](int k) const {\n return (A.at(k));\n }\n\n inline vector< T > &operator[](int k) {\n return (A.at(k));\n }\n\n static Matrix I(size_t n) {\n Matrix mat(n);\n for(int i = 0; i < n; i++) mat[i][i] = 1;\n return (mat);\n }\n\n Matrix &operator+=(const Matrix &B) {\n size_t n = height(), m = width();\n assert(n == B.height() && m == B.width());\n for(int i = 0; i < n; i++)\n for(int j = 0; j < m; j++)\n (*this)[i][j] += B[i][j];\n return (*this);\n }\n\n Matrix &operator-=(const Matrix &B) {\n size_t n = height(), m = width();\n assert(n == B.height() && m == B.width());\n for(int i = 0; i < n; i++)\n for(int j = 0; j < m; j++)\n (*this)[i][j] -= B[i][j];\n return (*this);\n }\n\n Matrix &operator*=(const Matrix &B) {\n if (B.height() == 1 and B.width() == 1){\n auto res = *this * B[0][0];\n this->A = res.A;\n return (*this);\n }\n\n if (this->height() == 1 and this->width() == 1){\n auto res = (*this)[0][0] * B;\n this->A = res.A;\n return (*this);\n }\n\n size_t n = height(), m = B.width(), p = width();\n assert(p == B.height());\n vector< vector< T > > C(n, vector< T >(m, 0));\n for(int i = 0; i < n; i++)\n for(int j = 0; j < m; j++)\n for(int k = 0; k < p; k++)\n C[i][j] = (C[i][j] + (*this)[i][k] * B[k][j]);\n A.swap(C);\n return (*this);\n }\n\n Matrix &operator^=(long long k) {\n Matrix B = Matrix::I(height());\n while(k > 0) {\n if(k & 1) B *= *this;\n *this *= *this;\n k >>= 1LL;\n }\n A.swap(B.A);\n return (*this);\n }\n\n Matrix operator+(const Matrix &B) const {\n return (Matrix(*this) += B);\n }\n\n Matrix operator-(const Matrix &B) const {\n return (Matrix(*this) -= B);\n }\n\n Matrix operator*(const Matrix &B) const {\n if (this->height() == 1 and this->width() == 1)\n return (*this)[0][0] * B;\n return (Matrix(*this) *= B);\n }\n\n Matrix operator^(const long long k) const {\n return (Matrix(*this) ^= k);\n }\n\n Matrix operator*(const T &k) const {\n auto res = Matrix(*this);\n for (int i = 0; i < res.height(); i++){\n for (int j = 0; j < res.width(); j++)\n res[i][j] *= k;\n }\n return res;\n }\n\n friend Matrix operator*(const T &k, const Matrix &p) {\n auto res = Matrix(p);\n for (int i = 0; i < res.height(); i++){\n for (int j = 0; j < res.width(); j++)\n res[i][j] *= k;\n }\n return res;\n }\n\n friend Matrix concat_v(const Matrix &lhs, const Matrix &rhs){\n assert(lhs.width() == rhs.width());\n Matrix res(lhs.height() + rhs.height(), lhs.width());\n for (int i = 0; i < lhs.height(); i++){\n for (int j = 0; j < lhs.width(); j++)\n res[i][j] = lhs[i][j];\n }\n for (int i = 0; i < rhs.height(); i++){\n for (int j = 0; j < rhs.width(); j++)\n res[lhs.height() + i][j] = rhs[i][j];\n }\n\n return res;\n }\n\n friend Matrix concat_w(const Matrix &lhs, const Matrix &rhs){\n assert(lhs.height() == rhs.height());\n Matrix res(lhs.height(), lhs.width() + rhs.width());\n for (int i = 0; i < lhs.height(); i++){\n for (int j = 0; j < lhs.width(); j++)\n res[i][j] = lhs[i][j];\n }\n for (int i = 0; i < rhs.height(); i++){\n for (int j = 0; j < rhs.width(); j++)\n res[i][lhs.width() + j] = rhs[i][j];\n }\n\n return res;\n }\n\n Matrix t(){\n Matrix res(this->width(), this->height());\n for (int i = 0; i < this->height(); i++){\n for (int j = 0; j < this->width(); j++)\n res[j][i] = (*this)[i][j];\n }\n\n return res;\n }\n\n friend ostream &operator<<(ostream &os, Matrix &p) {\n size_t n = p.height(), m = p.width();\n for(int i = 0; i < n; i++) {\n for(int j = 0; j < m; j++) {\n os << p[i][j] << (j + 1 == m ? \"\\n\" : \" \");\n }\n }\n return (os);\n }\n\n\n T determinant() {\n Matrix B(*this);\n assert(width() == height());\n T ret = 1;\n for(int i = 0; i < width(); i++) {\n int idx = -1;\n for(int j = i; j < width(); j++) {\n if(B[j][i] != 0) idx = j;\n }\n if(idx == -1) return (0);\n if(i != idx) {\n ret *= -1;\n swap(B[i], B[idx]);\n }\n ret *= B[i][i];\n T vv = B[i][i];\n for(int j = 0; j < width(); j++) {\n B[i][j] /= vv;\n }\n for(int j = i + 1; j < width(); j++) {\n T a = B[j][i];\n for(int k = 0; k < width(); k++) {\n B[j][k] -= B[i][k] * a;\n }\n }\n }\n return (ret);\n }\n};\n\ntemplate <std::uint_fast64_t Modulus> class modint {\n using u64 = std::uint_fast64_t;\n u64 a;\n\n public:\n\n template <class INT>\n constexpr modint(const INT x = 0) noexcept : a(x >= 0 ? x % Modulus : x % int_fast64_t(Modulus) + Modulus) {}\n constexpr modint(const u64 x = 0) noexcept : a(x % Modulus) {}\n constexpr u64 &value() noexcept { return a; }\n constexpr const u64 &value() const noexcept { return a; }\n constexpr modint operator+(const modint rhs) const noexcept {\n return modint(*this) += rhs;\n }\n constexpr modint operator-(const modint rhs) const noexcept {\n return modint(*this) -= rhs;\n }\n constexpr modint operator*(const modint rhs) const noexcept {\n return modint(*this) *= rhs;\n }\n constexpr modint operator/(const modint rhs) const noexcept {\n return modint(*this) /= rhs;\n }\n constexpr modint &operator+=(const modint rhs) noexcept {\n a += rhs.a;\n if (a >= Modulus) {\n a -= Modulus;\n }\n return *this;\n }\n constexpr modint &operator-=(const modint rhs) noexcept {\n if (a < rhs.a) {\n a += Modulus;\n }\n a -= rhs.a;\n return *this;\n }\n constexpr modint &operator*=(const modint rhs) noexcept {\n a = a * rhs.a % Modulus;\n return *this;\n }\n constexpr modint &operator/=(modint rhs) noexcept {\n u64 exp = Modulus - 2;\n while (exp) {\n if (exp % 2) {\n *this *= rhs;\n }\n rhs *= rhs;\n exp /= 2;\n }\n return *this;\n }\n\n constexpr bool operator<(const modint& rhs) const noexcept {return this->a < rhs.a;}\n constexpr bool operator>(const modint& rhs) const noexcept {return rhs < *this;}\n constexpr bool operator<=(const modint& rhs) const noexcept {return !(*this > rhs);}\n constexpr bool operator>=(const modint& rhs) const noexcept {return !(*this < rhs);}\n constexpr bool operator==(const modint& rhs) const noexcept {return this->a == rhs.a;}\n constexpr bool operator!=(const modint& rhs) const noexcept {return !(*this == rhs);}\n\n constexpr modint& operator++() noexcept {\n *this += modint(1);\n return *this;\n }\n constexpr modint operator++(int) noexcept {\n modint tmp(*this);\n ++(*this);\n return tmp;\n }\n constexpr modint& operator--() noexcept {\n *this -= modint(1);\n return *this;\n }\n constexpr modint operator--(int) noexcept {\n modint tmp(*this);\n --(*this);\n return tmp;\n }\n constexpr modint operator-() const noexcept {\n return modint(0) - *this;\n }\n\n template <std::uint_fast64_t M>\n friend constexpr std::ostream& operator<<(std::ostream& os, const modint<M>& rhs) noexcept {\n os << rhs.a;\n return os;\n }\n template <std::uint_fast64_t M>\n friend constexpr std::istream& operator>>(std::istream& is, modint<M>& rhs) noexcept {\n int64_t tmp;\n is >> tmp;\n rhs = modint(tmp);\n return is;\n }\n\n constexpr modint pow(const u64 k) const noexcept {\n if (k == 0)\n return 1;\n if (k % 2 == 0){\n modint res = pow(k / 2);\n return res * res;\n }\n return pow(k - 1) * modint(*this);\n }\n\n template <typename T>\n operator const T (){return a;}\n\n};\n\nusing mint = modint<32768>;\n\nclass Parser{\n string S;\n int pos = 0;\n map<char, Matrix<mint>> vars;\n public:\n Parser(string T) : S(T){}\n\n void set_s(string s){\n S = s;\n pos = 0;\n }\n\n char peek(){\n // if (pos < S.size())\n // cerr << S[pos] << endl;\n return pos < S.size() ? S[pos] : 'a';\n }\n\n bool is_number(char c){\n return '0' <= c and c <= '9';\n }\n\n bool is_A_to_Z(char c){\n return 'A' <= c and c <= 'Z';\n }\n\n void skip_spaces(){\n while (peek() == ' ')\n pos++;\n }\n\n Matrix<mint> assignment(){\n char var_c = var();\n assert(peek() == '=');\n pos++;\n vars[var_c] = expr();\n assert(peek() == '.');\n pos++;\n\n return vars[var_c];\n }\n\n char var(){\n assert('A' <= peek() and peek() <= 'Z');\n char res = peek();\n pos++;\n return res;\n }\n\n Matrix<mint> expr(){\n Matrix<mint> res = term();\n while (peek() == '+' or peek() == '-'){\n if (peek() == '+'){\n pos++;\n res += term();\n } else if (peek() == '-'){\n pos++;\n res -= term();\n }\n }\n return res;\n }\n\n Matrix<mint> term(){\n Matrix<mint> res = factor();\n while (peek() == '*'){\n pos++;\n res *= factor();\n }\n\n return res;\n }\n\n Matrix<mint> factor(){\n if (peek() == '-'){\n pos++;\n return mint(-1) * factor();\n }\n\n return primary();\n }\n\n Matrix<mint> primary(){\n auto res = Matrix<mint>(1, 1);\n res[0][0] = -1;\n if (is_number(peek())){\n res[0][0] = number();\n } else if (is_A_to_Z(peek())){\n auto v = var();\n res = vars[v];\n } else if (peek() == '['){\n res = matrix();\n }\n\n if (!is_index() and peek() == '('){\n pos++;\n res = expr();\n assert(peek() == ')');\n pos++;\n }\n\n while (is_index() or peek() == '\\''){\n if (peek() == '\\''){\n pos++;\n res = res.t();\n } else if (is_index()){\n assert(peek() == '(');\n pos++;\n auto left = expr();\n assert(peek() == ',');\n pos++;\n auto right = expr();\n assert(peek() == ')');\n pos++;\n Matrix<mint> res2(left.width(), right.width());\n for (int i = 0; i < left.width(); i++){\n for (int j = 0; j < right.width(); j++)\n res2[i][j] = res[left[0][i] - 1][right[0][j] - 1];\n }\n res = res2;\n }\n }\n\n return res;\n }\n\n bool is_index(){\n if (peek() != '(')\n return false;\n int c = 0;\n for (int i = pos; i < S.size(); i++){\n if (S[i] == '(')\n c++;\n if (S[i] == ')'){\n c--;\n if (c == 0)\n break;\n }\n if (S[i] == ',' and c == 1)\n return true;\n }\n return false;\n }\n\n Matrix<mint> matrix(){\n assert(peek() == '[');\n pos++;\n auto res = row_seq();\n assert(peek() == ']');\n pos++;\n return res;\n }\n\n Matrix<mint> row_seq(){\n auto res = row();\n while (peek() == ';'){\n pos++;\n res = concat_v(res, row());\n }\n return res;\n }\n\n Matrix<mint> row(){\n auto res = expr();\n while (peek() == ' '){\n pos++;\n res = concat_w(res, expr());\n }\n return res;\n }\n\n mint number(){\n mint res = 0;\n while (is_number(peek())){\n res = res * 10 + peek() - '0';\n pos++;\n }\n return res;\n }\n};\n\nvoid solve(int n){\n string stash;\n getline(cin, stash);\n Parser parser(\"\");\n for (int i = 0; i < n; i++){\n string s;\n getline(cin, s);\n parser.set_s(s);\n auto res = parser.assignment();\n cout << res;\n }\n puts(\"-----\");\n}\n\nint main(){\n int n;\n while (true){\n cin >> n;\n if (n == 0)\n break;\n solve(n);\n }\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3708, "score_of_the_acc": -0.1248, "final_rank": 9 }, { "submission_id": "aoj_1314_5955269", "code_snippet": "/*\tauthor: Kite_kuma\n\tcreated: 2021.10.10 10:33:56 */\n\n#ifdef LOCAL\n#define _GLIBCXX_DEBUG\n#endif\n#include <bits/stdc++.h>\nusing namespace std;\n\n#pragma region macros\n\n#define foa(s, v) for(auto &s : v)\n#define all(v) (v).begin(), (v).end()\n#define rall(v) (v).rbegin(), (v).rend()\n#define popcnt(n) __builtin_popcountll((long long)n)\n\n#define REPname(a, b, c, d, e, ...) e\n#define rep(...) REPname(__VA_ARGS__, REP3, REP2, REP1, REP0)(__VA_ARGS__)\n#define REP0(x) for(int Counter_in_rep_macro = 0; Counter_in_rep_macro < (x); ++Counter_in_rep_macro)\n#define REP1(i, x) for(int i = 0; i < (x); ++i)\n#define REP2(i, l, r) for(int i = (l); i < (r); ++i)\n#define REP3(i, l, r, c) for(int i = (l); i < (r); i += (c))\n\n#define DREPname(a, b, c, d, e, ...) e\n#define drep(...) DREPname(__VA_ARGS__, DREP3, DREP2, DREP1)(__VA_ARGS__)\n#define DREP1(i, x) for(int i = (x)-1; i >= 0; --i)\n#define DREP2(i, l, r) for(int i = (r)-1; i >= (l); --i)\n#define DREP3(i, l, r, c) for(int i = (r)-1; i >= (l); i -= (c))\n\n#pragma endregion\n\n#pragma region aliases\n\nusing ll = long long;\nusing ld = long double;\nusing ull = unsigned long long;\nusing vi = std::vector<int>;\nusing vvi = std::vector<std::vector<int>>;\nusing vvvi = std::vector<std::vector<std::vector<int>>>;\nusing vll = std::vector<ll>;\nusing vvll = std::vector<vll>;\nusing vvvll = std::vector<vvll>;\nusing pii = std::pair<int, int>;\nusing pll = std::pair<long long, long long>;\ntemplate <class T = ll>\nusing V = std::vector<T>;\ntemplate <class T = ll>\nusing VV = V<V<T>>;\ntemplate <class T = ll>\nusing VVV = V<V<V<T>>>;\ntemplate <class T = ll>\nusing pqup = std::priority_queue<T, std::vector<T>, std::greater<T>>;\ntemplate <class T = ll>\nusing pqdn = std::priority_queue<T>;\n\n#pragma endregion\n\n#pragma region constants\n\nconst int inf = 1e9;\nconst long long INF = 1e18;\nconst long double pi = acos(-1);\nconst char dl = '\\n';\nconst char sp = ' ';\nint dx[8] = {1, 0, -1, 0, 1, -1, -1, 1};\nint dy[8] = {0, 1, 0, -1, 1, 1, -1, -1};\n\nconst int mod_1000000007 = 1000000007;\nconst int mod_998244353 = 998244353;\n\n#pragma endregion\n\n#pragma region basic_operation\n\ntemplate <class T0, class T1, class T2>\ninline bool in_range(T0 x, T1 lef, T2 rig) {\n\treturn ((lef <= x) && (x < rig));\n}\n\ntemplate <class T>\ninline bool chmin(T &a, T b) {\n\tif(a > b) {\n\t\ta = b;\n\t\treturn true;\n\t}\n\treturn false;\n}\ntemplate <class T>\ninline bool chmax(T &a, T b) {\n\tif(a < b) {\n\t\ta = b;\n\t\treturn true;\n\t}\n\treturn false;\n}\n\nvoid Yes(bool f = 1) { std::cout << (f ? \"Yes\" : \"No\") << '\\n'; }\nvoid No() { std::cout << \"No\\n\"; }\nvoid YES(bool f = 1) { std::cout << (f ? \"YES\" : \"NO\") << '\\n'; }\nvoid NO() { std::cout << \"NO\\n\"; }\n\ntemplate <class T>\nvoid drop(T answer) {\n\tstd::cout << answer << '\\n';\n\texit(0);\n}\n\nvoid err(bool flag = true) {\n\tif(!flag) return;\n\tstd::cout << -1 << '\\n';\n\texit(0);\n}\n\ntemplate <class T>\nvoid vout(std::vector<T> const &v, bool vertically = 0) {\n\tif(vertically) {\n\t\tfor(auto const &a : v) {\n\t\t\tstd::cout << a << '\\n';\n\t\t}\n\t\treturn;\n\t}\n\tfor(auto it = v.begin(); it != v.end(); it++) {\n\t\tstd::cout << (*it);\n\t\tif(it != v.end() - 1) {\n\t\t\tstd::cout << ' ';\n\t\t}\n\t}\n\tstd::cout << '\\n';\n\treturn;\n}\n\ninline void print() { std::cout << '\\n'; }\ntemplate <class T>\ninline void print(T x) {\n\tstd::cout << x << '\\n';\n\treturn;\n}\ntemplate <typename Head, typename... Tail>\nvoid print(Head H, Tail... T) {\n\tstd::cout << H << \" \";\n\tprint(T...);\n}\n\ntemplate <class T>\nvoid add(std::vector<T> &v, T val) {\n\tfor(auto &a : v) a += val;\n\treturn;\n}\n\ntemplate <class T>\nT dup(T a, T b) {\n\tassert(b != 0);\n\treturn (a + b - 1) / b;\n}\n\ntemplate <class T>\nT greatest_lower_multiple(T x, T d) {\n\tif(d == 0) return 0;\n\tif(d < 0) d *= -1;\n\tT y = x % d;\n\tif(y < 0) y += d;\n\treturn x - y;\n}\n\ntemplate <class T>\nT least_upper_multiple(T x, T d) {\n\treturn -greatest_lower_multiple(-x, d);\n}\n\nlong long POW(long long a, long long n) {\n\tlong long res = 1;\n\twhile(n > 0) {\n\t\tif(n & 1) res = res * a;\n\t\ta = a * a;\n\t\tn >>= 1;\n\t}\n\treturn res;\n}\n\nlong long modpow(long long a, long long n, long long mod) {\t // a^n mod\n\tassert(n >= 0 && mod);\n\tif(mod == 1) return 0LL;\n\tlong long res = 1;\n\ta %= mod;\n\twhile(n > 0) {\n\t\tif(n & 1) res = res * a % mod;\n\t\ta = a * a % mod;\n\t\tn >>= 1;\n\t}\n\treturn res < 0 ? res + mod : res;\n}\n\n// return x which satisfies a * x % mod == gcd(a, mod)\n// not (mod | a)\nlong long modinv(long long a, long long mod) {\n\tlong long b = mod, u = 1, v = 0;\n\twhile(b) {\n\t\tlong long t = a / b;\n\t\ta -= t * b;\n\t\tstd::swap(a, b);\n\t\tu -= t * v;\n\t\tstd::swap(u, v);\n\t}\n\tu %= mod;\n\tif(u < 0) u += mod;\n\treturn u;\n}\n\ntemplate <class T>\nint lb(const std::vector<T> &a, const T x) {\n\treturn std::distance(a.begin(), std::lower_bound(a.begin(), a.end(), x));\n}\ntemplate <class T>\nint ub(const std::vector<T> &a, const T x) {\n\treturn std::distance(a.begin(), std::upper_bound(a.begin(), a.end(), x));\n}\ntemplate <class T>\nvoid unq_sort(std::vector<T> &a) {\n\tstd::sort(a.begin(), a.end());\n\ta.erase(std::unique(a.begin(), a.end()), a.end());\n}\ntemplate <class T>\nstd::vector<int> press(std::vector<T> &a) {\n\tauto vec = a;\n\tunq_sort(vec);\n\tstd::vector<int> ret;\n\tfor(auto &v : a) ret.push_back(lb(vec, v));\n\treturn ret;\n}\n\n#pragma endregion\n\n#pragma region input\n#define VEC(type, name, size) \\\n\tvector<type> name(size); \\\n\tscanner::INPUT(name)\n#define VVEC(type, name, h, w) \\\n\tvector<vector<type>> name(h, vector<type>(w)); \\\n\tscanner::INPUT(name)\n#define INT(...) \\\n\tint __VA_ARGS__; \\\n\tscanner::INPUT(__VA_ARGS__)\n#define LL(...) \\\n\tlong long __VA_ARGS__; \\\n\tscanner::INPUT(__VA_ARGS__)\n#define STR(...) \\\n\tstring __VA_ARGS__; \\\n\tscanner::INPUT(__VA_ARGS__)\n#define CHR(...) \\\n\tchar __VA_ARGS__; \\\n\tscanner::INPUT(__VA_ARGS__)\n#define DBL(...) \\\n\tdouble __VA_ARGS__; \\\n\tscanner::INPUT(__VA_ARGS__)\n#define LD(...) \\\n\tlong double __VA_ARGS__; \\\n\tscanner::INPUT(__VA_ARGS__)\n#define TPL3(type0, type1, type2, name) \\\n\tstd::tuple<type0, type1, type2> name; \\\n\tscanner::INPUT(name);\n#define TPL4(type0, type1, type2, type3, name) \\\n\tstd::tuple<type0, type1, type2, type3> name; \\\n\tscanner::INPUT(name);\n\nnamespace scanner {\ntemplate <class T>\nvoid scan(T &a) {\n\tstd::cin >> a;\n}\n\ntemplate <class T, class L>\nvoid scan(std::pair<T, L> &p) {\n\tscan(p.first);\n\tscan(p.second);\n}\n\ntemplate <class T0, class T1, class T2>\nvoid scan(std::tuple<T0, T1, T2> &p) {\n\tT0 t0;\n\tT1 t1;\n\tT2 t2;\n\tscan(t0);\n\tscan(t1);\n\tscan(t2);\n\tp = std::make_tuple(t0, t1, t2);\n}\n\ntemplate <class T0, class T1, class T2, class T3>\nvoid scan(std::tuple<T0, T1, T2, T3> &p) {\n\tT0 t0;\n\tT1 t1;\n\tT2 t2;\n\tT3 t3;\n\tscan(t0);\n\tscan(t1);\n\tscan(t2);\n\tscan(t3);\n\tp = std::make_tuple(t0, t1, t2, t3);\n}\n\ntemplate <class T>\nvoid scan(std::vector<T> &a) {\n\tfor(auto &i : a) scan(i);\n}\n\nvoid INPUT() {}\ntemplate <class Head, class... Tail>\nvoid INPUT(Head &head, Tail &... tail) {\n\tscan(head);\n\tINPUT(tail...);\n}\n} // namespace scanner\n\ntemplate <typename T1, typename T2>\nstd::istream &operator>>(std::istream &is, std::pair<T1, T2> &p) {\n\tis >> p.first >> p.second;\n\treturn is;\n}\n#pragma endregion\n\n#pragma region debug\n\n#pragma region output\ntemplate <typename T1, typename T2>\nstd::ostream &std::operator<<(std::ostream &os, const std::pair<T1, T2> &p) {\n\tos << p.first << \" \" << p.second;\n\treturn os;\n}\ntemplate <class T>\nstd::ostream &std::operator<<(std::ostream &os, const std::vector<T> &v) {\n\tfor(int i = 0; i < (int)v.size(); i++) {\n\t\tif(i) os << \" \";\n\t\tos << v[i];\n\t}\n\treturn os;\n}\n#pragma endregion\n\n#pragma region view\n\nnamespace viewer {\nvoid view(const long long e) {\n\tif(e == INF)\n\t\tstd::cerr << \"INF\";\n\telse if(e == -INF)\n\t\tstd::cerr << \"-INF\";\n\telse\n\t\tstd::cerr << e;\n}\n\nvoid view(const int e) {\n\tif(e == inf)\n\t\tstd::cerr << \"inf\";\n\telse if(e == -inf)\n\t\tstd::cerr << \"-inf\";\n\telse\n\t\tstd::cerr << e;\n}\n\ntemplate <typename T>\nvoid view(const T e) {\n\tstd::cerr << e;\n}\n\ntemplate <typename T, typename U>\nvoid view(const std::pair<T, U> &p) {\n\tstd::cerr << \"(\";\n\tview(p.first);\n\tstd::cerr << \", \";\n\tview(p.second);\n\tstd::cerr << \")\";\n}\n\ntemplate <class T0, class T1, class T2>\nvoid view(const std::tuple<T0, T1, T2> &p) {\n\tstd::cerr << \"(\";\n\tview(std::get<0>(p));\n\tstd::cerr << \", \";\n\tview(std::get<1>(p));\n\tstd::cerr << \", \";\n\tview(std::get<2>(p));\n\tstd::cerr << \")\";\n}\n\ntemplate <class T0, class T1, class T2, class T3>\nvoid view(const std::tuple<T0, T1, T2, T3> &p) {\n\tstd::cerr << \"(\";\n\tview(std::get<0>(p));\n\tstd::cerr << \", \";\n\tview(std::get<1>(p));\n\tstd::cerr << \", \";\n\tview(std::get<2>(p));\n\tstd::cerr << \", \";\n\tview(std::get<3>(p));\n\tstd::cerr << \")\";\n}\n\ntemplate <typename T>\nvoid view(const std::set<T> &s) {\n\tif(s.empty()) {\n\t\tstd::cerr << \"{ }\";\n\t\treturn;\n\t}\n\tstd::cerr << \"{ \";\n\tfor(auto &t : s) {\n\t\tview(t);\n\t\tstd::cerr << \", \";\n\t}\n\tstd::cerr << \"\\b\\b }\";\n}\n\ntemplate <typename T>\nvoid view(const std::unordered_set<T> &s) {\n\tif(s.empty()) {\n\t\tstd::cerr << \"{ }\";\n\t\treturn;\n\t}\n\tstd::cerr << \"{ \";\n\tfor(auto &t : s) {\n\t\tview(t);\n\t\tstd::cerr << \", \";\n\t}\n\tstd::cerr << \"\\b\\b }\";\n}\n\ntemplate <typename T>\nvoid view(const std::vector<T> &v) {\n\tif(v.empty()) {\n\t\tstd::cerr << \"{ }\";\n\t\treturn;\n\t}\n\tstd::cerr << \"{ \";\n\tfor(const auto &e : v) {\n\t\tview(e);\n\t\tstd::cerr << \", \";\n\t}\n\tstd::cerr << \"\\b\\b }\";\n}\n\ntemplate <typename T>\nvoid view(const std::vector<std::vector<T>> &vv) {\n\tstd::cerr << \"{\\n\";\n\tfor(const auto &v : vv) {\n\t\tstd::cerr << \"\\t\";\n\t\tview(v);\n\t\tstd::cerr << '\\n';\n\t}\n\tstd::cerr << \"}\";\n}\n\ntemplate <typename T, typename U>\nvoid view(const std::vector<std::pair<T, U>> &v) {\n\tstd::cerr << \"{\\n\";\n\tfor(const auto &c : v) {\n\t\tstd::cerr << \"\\t(\";\n\t\tview(c.first);\n\t\tstd::cerr << \", \";\n\t\tview(c.second);\n\t\tstd::cerr << \")\\n\";\n\t}\n\tstd::cerr << \"}\";\n}\n\ntemplate <class T0, class T1, class T2>\nvoid view(const std::vector<std::tuple<T0, T1, T2>> &v) {\n\tif(v.empty()) {\n\t\tstd::cerr << \"{ }\";\n\t\treturn;\n\t}\n\tstd::cerr << '{';\n\tfor(const auto &t : v) {\n\t\tstd::cerr << \"\\n\\t\";\n\t\tview(t);\n\t\tstd::cerr << \",\";\n\t}\n\tstd::cerr << \"\\n}\";\n}\n\ntemplate <class T0, class T1, class T2, class T3>\nvoid view(const std::vector<std::tuple<T0, T1, T2, T3>> &v) {\n\tif(v.empty()) {\n\t\tstd::cerr << \"{ }\";\n\t\treturn;\n\t}\n\tstd::cerr << '{';\n\tfor(const auto &t : v) {\n\t\tstd::cerr << \"\\n\\t\";\n\t\tview(t);\n\t\tstd::cerr << \",\";\n\t}\n\tstd::cerr << \"\\n}\";\n}\n\ntemplate <typename T, typename U>\nvoid view(const std::map<T, U> &m) {\n\tstd::cerr << \"{\\n\";\n\tfor(const auto &t : m) {\n\t\tstd::cerr << \"\\t[\";\n\t\tview(t.first);\n\t\tstd::cerr << \"] : \";\n\t\tview(t.second);\n\t\tstd::cerr << '\\n';\n\t}\n\tstd::cerr << \"}\";\n}\n\ntemplate <typename T, typename U>\nvoid view(const std::unordered_map<T, U> &m) {\n\tstd::cerr << \"{\\n\";\n\tfor(const auto &t : m) {\n\t\tstd::cerr << \"\\t[\";\n\t\tview(t.first);\n\t\tstd::cerr << \"] : \";\n\t\tview(t.second);\n\t\tstd::cerr << '\\n';\n\t}\n\tstd::cerr << \"}\";\n}\n} // namespace viewer\n\n#pragma endregion\n\n// when debugging : g++ foo.cpp -DLOCAL\n#ifdef LOCAL\nvoid debug_out() {}\ntemplate <typename Head, typename... Tail>\nvoid debug_out(Head H, Tail... T) {\n\tviewer::view(H);\n\tstd::cerr << \", \";\n\tdebug_out(T...);\n}\n#define debug(...) \\\n\tdo { \\\n\t\tstd::cerr << __LINE__ << \" [\" << #__VA_ARGS__ << \"] : [\"; \\\n\t\tdebug_out(__VA_ARGS__); \\\n\t\tstd::cerr << \"\\b\\b]\\n\"; \\\n\t} while(0)\n#define dump(x) \\\n\tdo { \\\n\t\tstd::cerr << __LINE__ << \" \" << #x << \" : \"; \\\n\t\tviewer::view(x); \\\n\t\tstd::cerr << '\\n'; \\\n\t} while(0)\n\n#else\n#define debug(...) (void(0))\n#define dump(x) (void(0))\n#endif\n\n#pragma endregion\n\n#pragma region modint\n\nconst int mod = (1 << 15);\n\ntemplate <int mod>\nstruct ModInt {\n\tint x;\n\tModInt() : x(0) {}\n\tModInt(long long y) : x(y >= 0 ? y % mod : (mod - (-y) % mod) % mod) {}\n\tModInt &operator+=(const ModInt &p) {\n\t\tif((x += p.x) >= mod) x -= mod;\n\t\treturn *this;\n\t}\n\tModInt &operator-=(const ModInt &p) {\n\t\tif((x += mod - p.x) >= mod) x -= mod;\n\t\treturn *this;\n\t}\n\tModInt &operator*=(const ModInt &p) {\n\t\tx = (int)(1LL * x * p.x % mod);\n\t\treturn *this;\n\t}\n\tModInt &operator/=(const ModInt &p) {\n\t\t*this *= p.inverse();\n\t\treturn *this;\n\t}\n\tModInt operator-() const { return ModInt(-x); }\n\tModInt operator+(const ModInt &p) const { return ModInt(*this) += p; }\n\tModInt operator-(const ModInt &p) const { return ModInt(*this) -= p; }\n\tModInt operator*(const ModInt &p) const { return ModInt(*this) *= p; }\n\tModInt operator/(const ModInt &p) const { return ModInt(*this) /= p; }\n\tbool operator==(const ModInt &p) const { return x == p.x; }\n\tbool operator!=(const ModInt &p) const { return x != p.x; }\n\tModInt inverse() const {\n\t\tint a = x, b = mod, u = 1, v = 0, t;\n\t\twhile(b > 0) {\n\t\t\tt = a / b;\n\t\t\tswap(a -= t * b, b);\n\t\t\tswap(u -= t * v, v);\n\t\t}\n\t\treturn ModInt(u);\n\t}\n\tModInt pow(long long n) const {\n\t\tModInt ret(1), mul(x);\n\t\twhile(n > 0) {\n\t\t\tif(n & 1) ret *= mul;\n\t\t\tmul *= mul;\n\t\t\tn >>= 1;\n\t\t}\n\t\treturn ret;\n\t}\n\tfriend ostream &operator<<(ostream &os, const ModInt &p) { return os << p.x; }\n\tfriend istream &operator>>(istream &is, ModInt &a) {\n\t\tlong long t;\n\t\tis >> t;\n\t\ta = ModInt<mod>(t);\n\t\treturn (is);\n\t}\n\tstatic int get_mod() { return mod; }\n};\nusing mint = ModInt<mod>;\n\n#pragma endregion\n\nstruct matrix {\n\tint row_size, col_size;\n\tVV<mint> mat;\n\n\tmatrix() = default;\n\tmatrix(int h, int w, mint value = 0) : row_size(h), col_size(w), mat(h, V<mint>(w, value)) {}\n\tmatrix(mint val) : row_size(1), col_size(1), mat(1, V<mint>(1, val)) {}\n\tmatrix(VV<mint> m) : row_size(m.size()), col_size(m[0].size()), mat(m) {}\n\n\tV<mint> &operator[](int idx) { return mat[idx]; }\n\n\tmatrix operator+(matrix right) {\n\t\tassert(row_size == right.row_size && col_size == right.col_size);\n\t\tauto ret = mat;\n\t\trep(i, row_size) rep(j, col_size) { ret[i][j] += right[i][j]; }\n\t\treturn ret;\n\t}\n\n\tmatrix operator-(matrix right) {\n\t\tassert(row_size == right.row_size && col_size == right.col_size);\n\t\tauto ret = mat;\n\t\trep(i, row_size) rep(j, col_size) { ret[i][j] -= right[i][j]; }\n\t\treturn ret;\n\t}\n\n\tmatrix operator*(mint right) {\n\t\tauto ret = mat;\n\t\trep(i, row_size) rep(j, col_size) { ret[i][j] *= right; }\n\t\treturn ret;\n\t}\n\n\tmatrix operator*(matrix right) {\n\t\tif(right.row_size + right.col_size == 2) return *(this) * right[0][0];\n\t\tif(row_size + col_size == 2) return right * mat[0][0];\n\t\tassert(col_size == right.row_size);\n\t\tmatrix ret(row_size, right.col_size);\n\t\trep(i, row_size) rep(k, col_size) rep(j, right.col_size) { ret[i][j] += mat[i][k] * right[k][j]; }\n\t\treturn ret;\n\t}\n\n\tmatrix conjugate_side(matrix right) {\n\t\tassert(row_size == right.row_size);\n\t\tVV<mint> ret = mat;\n\t\trep(r, row_size) {\n\t\t\trep(c, right.col_size) { ret[r].push_back(right[r][c]); }\n\t\t}\n\t\treturn matrix(ret);\n\t}\n\n\tmatrix conjugate_bottom(matrix under) {\n\t\tif(col_size != under.col_size) {\n\t\t\tdebug(mat);\n\t\t\tdebug(under.mat);\n\t\t\tdebug(col_size, under.col_size);\n\t\t\tassert(false);\n\t\t}\n\t\tVV<mint> ret = mat;\n\t\tfoa(v, under.mat) { ret.push_back(v); }\n\t\treturn ret;\n\t}\n\n\tmatrix transpose() {\n\t\tmatrix ret(col_size, row_size);\n\t\trep(i, row_size) rep(j, col_size) ret[j][i] = mat[i][j];\n\t\treturn ret;\n\t}\n\n\tvoid print() {\n\t\trep(i, row_size) {\n\t\t\trep(j, col_size) {\n\t\t\t\tcout << mat[i][j];\n\t\t\t\tif(j == col_size - 1) {\n\t\t\t\t\tcout << dl;\n\t\t\t\t} else\n\t\t\t\t\tcout << sp;\n\t\t\t}\n\t\t}\n\t}\n};\n\nstring s;\nstring::iterator it;\nV<matrix> vars(26);\nstruct parser {\n\tparser() { it = s.begin(); }\n\tvoid consume(const char &c) {\n\t\tif(*it == c) {\n\t\t\tit++;\n\t\t\treturn;\n\t\t}\n\t\tdebug(s);\n\t\tdebug(it - s.begin());\n\t\tdebug(\"consumed:\", c);\n\t\tif(it != s.end())\n\t\t\tcerr << \"but in this case\" << '\\'' << *it << '\\'';\n\t\telse\n\t\t\tcerr << \"but in this case, program has ended\";\n\t\tcerr << dl;\n\t\texit(0);\n\t}\n\tmatrix assignment() {\n\t\tint v = var();\n\t\tconsume('=');\n\t\tdebug(\"eq\", v, it - s.begin());\n\t\tmatrix e = expr();\n\t\tdebug(e.mat);\n\t\tconsume('.');\n\t\tassert(it == s.end());\n\t\tvars[v] = e;\n\t\treturn e;\n\t}\n\tmint dig() { return *it++ - '0'; }\n\tmint inum() {\n\t\tmint ret = 0;\n\t\tassert(isdigit(*it));\n\t\twhile(isdigit(*it)) {\n\t\t\tret *= 10;\n\t\t\tret += dig();\n\t\t}\n\t\treturn ret;\n\t}\n\n\tint var() {\n\t\tassert(isalpha(*it));\n\t\treturn *it++ - 'A';\n\t}\n\tmatrix expr() {\n\t\tmatrix ret = term();\n\t\twhile(it != s.end()) {\n\t\t\tif(*it == '+') {\n\t\t\t\tconsume('+');\n\t\t\t\tret = ret + term();\n\t\t\t} else if(*it == '-') {\n\t\t\t\tconsume('-');\n\t\t\t\tret = ret - term();\n\t\t\t} else {\n\t\t\t\tbreak;\n\t\t\t}\n\t\t}\n\t\tdebug(ret.mat, it - s.begin());\n\t\treturn ret;\n\t}\n\tmatrix term() {\n\t\tmatrix ret = factor();\n\t\twhile(it != s.end() && *it == '*') {\n\t\t\tconsume('*');\n\t\t\tret = ret * factor();\n\t\t}\n\t\tdebug(ret.mat, it - s.begin());\n\t\treturn ret;\n\t}\n\tmatrix factor() {\n\t\tif(*it == '-') {\n\t\t\tconsume('-');\n\t\t\treturn factor() * mint(-1);\n\t\t}\n\t\treturn primary();\n\t}\n\tmatrix primary() {\n\t\tmatrix ret;\n\t\tif(isalpha(*it))\n\t\t\tret = matrix(vars[var()]);\n\t\telse if(isdigit(*it))\n\t\t\tret = inum();\n\t\telse if(*it == '(') {\n\t\t\tconsume('(');\n\t\t\tret = expr();\n\t\t\tconsume(')');\n\t\t} else {\n\t\t\tconsume('[');\n\t\t\tret = row_seq();\n\t\t\tconsume(']');\n\t\t}\n\t\twhile(it != s.end()) {\n\t\t\tif(*it == '\\'') {\n\t\t\t\tconsume('\\'');\n\t\t\t\tret = ret.transpose();\n\t\t\t} else if(*it == '(') {\n\t\t\t\tconsume('(');\n\t\t\t\tmatrix rows = expr();\n\t\t\t\tconsume(',');\n\t\t\t\tmatrix cols = expr();\n\t\t\t\tconsume(')');\n\t\t\t\tassert(rows.row_size == 1);\n\t\t\t\tassert(cols.row_size == 1);\n\t\t\t\tmatrix nxt(rows.col_size, cols.col_size);\n\t\t\t\trep(i, rows.col_size) rep(j, cols.col_size) { nxt[i][j] = ret[(rows[0][i] - 1).x][(cols[0][j] - 1).x]; }\n\t\t\t\tswap(nxt, ret);\n\t\t\t} else {\n\t\t\t\tbreak;\n\t\t\t}\n\t\t}\n\t\treturn ret;\n\t}\n\t// mat indexed_primary() {}\n\t// mat transposed_primary() {}\n\tmatrix row_seq() {\n\t\tmatrix ret = row();\n\t\tdebug(ret.mat);\n\t\twhile(it != s.end() && *it == ';') {\n\t\t\tconsume(';');\n\t\t\tret = ret.conjugate_bottom(row());\n\t\t\tdebug(ret.mat);\n\t\t}\n\t\tdebug(ret.mat);\n\t\treturn ret;\n\t}\n\tmatrix row() {\n\t\tmatrix ret = expr();\n\t\twhile(it != s.end() && *it == ' ') {\n\t\t\tconsume(' ');\n\t\t\tret = ret.conjugate_side(expr());\n\t\t}\n\t\tdebug(ret.mat);\n\t\treturn ret;\n\t}\n};\n\nvoid solve(int n) {\n\tcin.ignore();\n\trep(n) {\n\t\tdebug(n);\n\t\tgetline(cin, s);\n\t\tdebug(s);\n\t\tparser ps;\n\t\tps.assignment().print();\n\t}\n}\n\nint main() {\n\tstd::ios::sync_with_stdio(false);\n\tstd::cin.tie(nullptr);\n\tstd::cout << std::fixed << std::setprecision(15);\n\tsrand((unsigned)time(NULL));\n\n\tint n;\n\twhile(cin >> n && n) {\n\t\tsolve(n);\n\t\tcout << \"-----\" << dl;\n\t}\n\n\treturn 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3764, "score_of_the_acc": -0.1335, "final_rank": 12 }, { "submission_id": "aoj_1314_5953886", "code_snippet": "#define MOD_TYPE 1\n\n#include <bits/stdc++.h>\nusing namespace std;\n\n//#include <atcoder/all>\n\n\n#include <utility>\n\nnamespace atcoder {\n\nnamespace internal {\n\n// @param m `1 <= m`\n// @return x mod m\nconstexpr long long safe_mod(long long x, long long m) {\n x %= m;\n if (x < 0) x += m;\n return x;\n}\n\n// Fast modular multiplication by barrett reduction\n// Reference: https://en.wikipedia.org/wiki/Barrett_reduction\n// NOTE: reconsider after Ice Lake\nstruct barrett {\n unsigned int _m;\n unsigned long long im;\n\n // @param m `1 <= m < 2^31`\n barrett(unsigned int m) : _m(m), im((unsigned long long)(-1) / m + 1) {}\n\n // @return m\n unsigned int umod() const { return _m; }\n\n // @param a `0 <= a < m`\n // @param b `0 <= b < m`\n // @return `a * b % m`\n unsigned int mul(unsigned int a, unsigned int b) const {\n // [1] m = 1\n // a = b = im = 0, so okay\n\n // [2] m >= 2\n // im = ceil(2^64 / m)\n // -> im * m = 2^64 + r (0 <= r < m)\n // let z = a*b = c*m + d (0 <= c, d < m)\n // a*b * im = (c*m + d) * im = c*(im*m) + d*im = c*2^64 + c*r + d*im\n // c*r + d*im < m * m + m * im < m * m + 2^64 + m <= 2^64 + m * (m + 1) < 2^64 * 2\n // ((ab * im) >> 64) == c or c + 1\n unsigned long long z = a;\n z *= b;\n#ifdef _MSC_VER\n unsigned long long x;\n _umul128(z, im, &x);\n#else\n unsigned long long x =\n (unsigned long long)(((unsigned __int128)(z)*im) >> 64);\n#endif\n unsigned int v = (unsigned int)(z - x * _m);\n if (_m <= v) v += _m;\n return v;\n }\n};\n\n// @param n `0 <= n`\n// @param m `1 <= m`\n// @return `(x ** n) % m`\nconstexpr long long pow_mod_constexpr(long long x, long long n, int m) {\n if (m == 1) return 0;\n unsigned int _m = (unsigned int)(m);\n unsigned long long r = 1;\n unsigned long long y = safe_mod(x, m);\n while (n) {\n if (n & 1) r = (r * y) % _m;\n y = (y * y) % _m;\n n >>= 1;\n }\n return r;\n}\n\n// Reference:\n// M. Forisek and J. Jancina,\n// Fast Primality Testing for Integers That Fit into a Machine Word\n// @param n `0 <= n`\nconstexpr bool is_prime_constexpr(int n) {\n if (n <= 1) return false;\n if (n == 2 || n == 7 || n == 61) return true;\n if (n % 2 == 0) return false;\n long long d = n - 1;\n while (d % 2 == 0) d /= 2;\n constexpr long long bases[3] = {2, 7, 61};\n for (long long a : bases) {\n long long t = d;\n long long y = pow_mod_constexpr(a, t, n);\n while (t != n - 1 && y != 1 && y != n - 1) {\n y = y * y % n;\n t <<= 1;\n }\n if (y != n - 1 && t % 2 == 0) {\n return false;\n }\n }\n return true;\n}\ntemplate <int n> constexpr bool is_prime = is_prime_constexpr(n);\n\n// @param b `1 <= b`\n// @return pair(g, x) s.t. g = gcd(a, b), xa = g (mod b), 0 <= x < b/g\nconstexpr std::pair<long long, long long> inv_gcd(long long a, long long b) {\n a = safe_mod(a, b);\n if (a == 0) return {b, 0};\n\n // Contracts:\n // [1] s - m0 * a = 0 (mod b)\n // [2] t - m1 * a = 0 (mod b)\n // [3] s * |m1| + t * |m0| <= b\n long long s = b, t = a;\n long long m0 = 0, m1 = 1;\n\n while (t) {\n long long u = s / t;\n s -= t * u;\n m0 -= m1 * u; // |m1 * u| <= |m1| * s <= b\n\n // [3]:\n // (s - t * u) * |m1| + t * |m0 - m1 * u|\n // <= s * |m1| - t * u * |m1| + t * (|m0| + |m1| * u)\n // = s * |m1| + t * |m0| <= b\n\n auto tmp = s;\n s = t;\n t = tmp;\n tmp = m0;\n m0 = m1;\n m1 = tmp;\n }\n // by [3]: |m0| <= b/g\n // by g != b: |m0| < b/g\n if (m0 < 0) m0 += b / s;\n return {s, m0};\n}\n\n// Compile time primitive root\n// @param m must be prime\n// @return primitive root (and minimum in now)\nconstexpr int primitive_root_constexpr(int m) {\n if (m == 2) return 1;\n if (m == 167772161) return 3;\n if (m == 469762049) return 3;\n if (m == 754974721) return 11;\n if (m == 998244353) return 3;\n int divs[20] = {};\n divs[0] = 2;\n int cnt = 1;\n int x = (m - 1) / 2;\n while (x % 2 == 0) x /= 2;\n for (int i = 3; (long long)(i)*i <= x; i += 2) {\n if (x % i == 0) {\n divs[cnt++] = i;\n while (x % i == 0) {\n x /= i;\n }\n }\n }\n if (x > 1) {\n divs[cnt++] = x;\n }\n for (int g = 2;; g++) {\n bool ok = true;\n for (int i = 0; i < cnt; i++) {\n if (pow_mod_constexpr(g, (m - 1) / divs[i], m) == 1) {\n ok = false;\n break;\n }\n }\n if (ok) return g;\n }\n}\ntemplate <int m> constexpr int primitive_root = primitive_root_constexpr(m);\n\n} // namespace internal\n\n} // namespace atcoder\n\n\n#include <cassert>\n#include <numeric>\n#include <type_traits>\n\nnamespace atcoder {\n\nnamespace internal {\n\n#ifndef _MSC_VER\ntemplate <class T>\nusing is_signed_int128 =\n typename std::conditional<std::is_same<T, __int128_t>::value ||\n std::is_same<T, __int128>::value,\n std::true_type,\n std::false_type>::type;\n\ntemplate <class T>\nusing is_unsigned_int128 =\n typename std::conditional<std::is_same<T, __uint128_t>::value ||\n std::is_same<T, unsigned __int128>::value,\n std::true_type,\n std::false_type>::type;\n\ntemplate <class T>\nusing make_unsigned_int128 =\n typename std::conditional<std::is_same<T, __int128_t>::value,\n __uint128_t,\n unsigned __int128>;\n\ntemplate <class T>\nusing is_integral = typename std::conditional<std::is_integral<T>::value ||\n is_signed_int128<T>::value ||\n is_unsigned_int128<T>::value,\n std::true_type,\n std::false_type>::type;\n\ntemplate <class T>\nusing is_signed_int = typename std::conditional<(is_integral<T>::value &&\n std::is_signed<T>::value) ||\n is_signed_int128<T>::value,\n std::true_type,\n std::false_type>::type;\n\ntemplate <class T>\nusing is_unsigned_int =\n typename std::conditional<(is_integral<T>::value &&\n std::is_unsigned<T>::value) ||\n is_unsigned_int128<T>::value,\n std::true_type,\n std::false_type>::type;\n\ntemplate <class T>\nusing to_unsigned = typename std::conditional<\n is_signed_int128<T>::value,\n make_unsigned_int128<T>,\n typename std::conditional<std::is_signed<T>::value,\n std::make_unsigned<T>,\n std::common_type<T>>::type>::type;\n\n#else\n\ntemplate <class T> using is_integral = typename std::is_integral<T>;\n\ntemplate <class T>\nusing is_signed_int =\n typename std::conditional<is_integral<T>::value && std::is_signed<T>::value,\n std::true_type,\n std::false_type>::type;\n\ntemplate <class T>\nusing is_unsigned_int =\n typename std::conditional<is_integral<T>::value &&\n std::is_unsigned<T>::value,\n std::true_type,\n std::false_type>::type;\n\ntemplate <class T>\nusing to_unsigned = typename std::conditional<is_signed_int<T>::value,\n std::make_unsigned<T>,\n std::common_type<T>>::type;\n\n#endif\n\ntemplate <class T>\nusing is_signed_int_t = std::enable_if_t<is_signed_int<T>::value>;\n\ntemplate <class T>\nusing is_unsigned_int_t = std::enable_if_t<is_unsigned_int<T>::value>;\n\ntemplate <class T> using to_unsigned_t = typename to_unsigned<T>::type;\n\n} // namespace internal\n\n} // namespace atcoder\n\n#include <cassert>\n#include <numeric>\n#include <type_traits>\n\n#ifdef _MSC_VER\n#include <intrin.h>\n#endif\n\nnamespace atcoder {\n\nnamespace internal {\n\nstruct modint_base {};\nstruct static_modint_base : modint_base {};\n\ntemplate <class T> using is_modint = std::is_base_of<modint_base, T>;\ntemplate <class T> using is_modint_t = std::enable_if_t<is_modint<T>::value>;\n\n} // namespace internal\n\ntemplate <int m, std::enable_if_t<(1 <= m)>* = nullptr>\nstruct static_modint : internal::static_modint_base {\n using mint = static_modint;\n\n public:\n static constexpr int mod() { return m; }\n static mint raw(int v) {\n mint x;\n x._v = v;\n return x;\n }\n\n static_modint() : _v(0) {}\n template <class T, internal::is_signed_int_t<T>* = nullptr>\n static_modint(T v) {\n long long x = (long long)(v % (long long)(umod()));\n if (x < 0) x += umod();\n _v = (unsigned int)(x);\n }\n template <class T, internal::is_unsigned_int_t<T>* = nullptr>\n static_modint(T v) {\n _v = (unsigned int)(v % umod());\n }\n static_modint(bool v) { _v = ((unsigned int)(v) % umod()); }\n\n unsigned int val() const { return _v; }\n\n mint& operator++() {\n _v++;\n if (_v == umod()) _v = 0;\n return *this;\n }\n mint& operator--() {\n if (_v == 0) _v = umod();\n _v--;\n return *this;\n }\n mint operator++(int) {\n mint result = *this;\n ++*this;\n return result;\n }\n mint operator--(int) {\n mint result = *this;\n --*this;\n return result;\n }\n\n mint& operator+=(const mint& rhs) {\n _v += rhs._v;\n if (_v >= umod()) _v -= umod();\n return *this;\n }\n mint& operator-=(const mint& rhs) {\n _v -= rhs._v;\n if (_v >= umod()) _v += umod();\n return *this;\n }\n mint& operator*=(const mint& rhs) {\n unsigned long long z = _v;\n z *= rhs._v;\n _v = (unsigned int)(z % umod());\n return *this;\n }\n mint& operator/=(const mint& rhs) { return *this = *this * rhs.inv(); }\n\n mint operator+() const { return *this; }\n mint operator-() const { return mint() - *this; }\n\n mint pow(long long n) const {\n assert(0 <= n);\n mint x = *this, r = 1;\n while (n) {\n if (n & 1) r *= x;\n x *= x;\n n >>= 1;\n }\n return r;\n }\n mint inv() const {\n if (prime) {\n assert(_v);\n return pow(umod() - 2);\n } else {\n auto eg = internal::inv_gcd(_v, m);\n assert(eg.first == 1);\n return eg.second;\n }\n }\n\n friend mint operator+(const mint& lhs, const mint& rhs) {\n return mint(lhs) += rhs;\n }\n friend mint operator-(const mint& lhs, const mint& rhs) {\n return mint(lhs) -= rhs;\n }\n friend mint operator*(const mint& lhs, const mint& rhs) {\n return mint(lhs) *= rhs;\n }\n friend mint operator/(const mint& lhs, const mint& rhs) {\n return mint(lhs) /= rhs;\n }\n friend bool operator==(const mint& lhs, const mint& rhs) {\n return lhs._v == rhs._v;\n }\n friend bool operator!=(const mint& lhs, const mint& rhs) {\n return lhs._v != rhs._v;\n }\n\n private:\n unsigned int _v;\n static constexpr unsigned int umod() { return m; }\n static constexpr bool prime = internal::is_prime<m>;\n};\n\ntemplate <int id> struct dynamic_modint : internal::modint_base {\n using mint = dynamic_modint;\n\n public:\n static int mod() { return (int)(bt.umod()); }\n static void set_mod(int m) {\n assert(1 <= m);\n bt = internal::barrett(m);\n }\n static mint raw(int v) {\n mint x;\n x._v = v;\n return x;\n }\n\n dynamic_modint() : _v(0) {}\n template <class T, internal::is_signed_int_t<T>* = nullptr>\n dynamic_modint(T v) {\n long long x = (long long)(v % (long long)(mod()));\n if (x < 0) x += mod();\n _v = (unsigned int)(x);\n }\n template <class T, internal::is_unsigned_int_t<T>* = nullptr>\n dynamic_modint(T v) {\n _v = (unsigned int)(v % mod());\n }\n dynamic_modint(bool v) { _v = ((unsigned int)(v) % mod()); }\n\n unsigned int val() const { return _v; }\n\n mint& operator++() {\n _v++;\n if (_v == umod()) _v = 0;\n return *this;\n }\n mint& operator--() {\n if (_v == 0) _v = umod();\n _v--;\n return *this;\n }\n mint operator++(int) {\n mint result = *this;\n ++*this;\n return result;\n }\n mint operator--(int) {\n mint result = *this;\n --*this;\n return result;\n }\n\n mint& operator+=(const mint& rhs) {\n _v += rhs._v;\n if (_v >= umod()) _v -= umod();\n return *this;\n }\n mint& operator-=(const mint& rhs) {\n _v += mod() - rhs._v;\n if (_v >= umod()) _v -= umod();\n return *this;\n }\n mint& operator*=(const mint& rhs) {\n _v = bt.mul(_v, rhs._v);\n return *this;\n }\n mint& operator/=(const mint& rhs) { return *this = *this * rhs.inv(); }\n\n mint operator+() const { return *this; }\n mint operator-() const { return mint() - *this; }\n\n mint pow(long long n) const {\n assert(0 <= n);\n mint x = *this, r = 1;\n while (n) {\n if (n & 1) r *= x;\n x *= x;\n n >>= 1;\n }\n return r;\n }\n mint inv() const {\n auto eg = internal::inv_gcd(_v, mod());\n assert(eg.first == 1);\n return eg.second;\n }\n\n friend mint operator+(const mint& lhs, const mint& rhs) {\n return mint(lhs) += rhs;\n }\n friend mint operator-(const mint& lhs, const mint& rhs) {\n return mint(lhs) -= rhs;\n }\n friend mint operator*(const mint& lhs, const mint& rhs) {\n return mint(lhs) *= rhs;\n }\n friend mint operator/(const mint& lhs, const mint& rhs) {\n return mint(lhs) /= rhs;\n }\n friend bool operator==(const mint& lhs, const mint& rhs) {\n return lhs._v == rhs._v;\n }\n friend bool operator!=(const mint& lhs, const mint& rhs) {\n return lhs._v != rhs._v;\n }\n\n private:\n unsigned int _v;\n static internal::barrett bt;\n static unsigned int umod() { return bt.umod(); }\n};\ntemplate <int id> internal::barrett dynamic_modint<id>::bt = 998244353;\n\nusing modint998244353 = static_modint<998244353>;\nusing modint1000000007 = static_modint<1000000007>;\nusing modint = dynamic_modint<-1>;\n\nnamespace internal {\n\ntemplate <class T>\nusing is_static_modint = std::is_base_of<internal::static_modint_base, T>;\n\ntemplate <class T>\nusing is_static_modint_t = std::enable_if_t<is_static_modint<T>::value>;\n\ntemplate <class> struct is_dynamic_modint : public std::false_type {};\ntemplate <int id>\nstruct is_dynamic_modint<dynamic_modint<id>> : public std::true_type {};\n\ntemplate <class T>\nusing is_dynamic_modint_t = std::enable_if_t<is_dynamic_modint<T>::value>;\n\n} // namespace internal\n\n} // namespace atcoder\n\nusing namespace atcoder;\n\n#if 0\n#include <boost/multiprecision/cpp_dec_float.hpp>\n#include <boost/multiprecision/cpp_int.hpp>\nusing Int = boost::multiprecision::cpp_int;\nusing lld = boost::multiprecision::cpp_dec_float_100;\n#endif\n\n#if 1\n#pragma GCC target(\"avx2\")\n#pragma GCC optimize(\"O3\")\n#pragma GCC optimize(\"unroll-loops\")\n#endif\n\n#pragma region Macros\n\nusing ll = long long int;\nusing ld = long double;\nusing pii = pair<int, int>;\nusing pll = pair<ll, ll>;\nusing pld = pair<ld, ld>;\ntemplate <typename Q_type>\nusing smaller_queue = priority_queue<Q_type, vector<Q_type>, greater<Q_type>>;\n\n#if MOD_TYPE == 1\nconstexpr ll MOD = ll(1e9 + 7);\n#else\n#if MOD_TYPE == 2\nconstexpr ll MOD = 998244353;\n#else\nconstexpr ll MOD = 1000003;\n#endif\n#endif\n\nusing mint = static_modint<MOD>;\nconstexpr int INF = (int)1e9 + 10;\nconstexpr ll LINF = (ll)4e18;\nconstexpr double PI = acos(-1.0);\nconstexpr double EPS = 1e-11;\nconstexpr int Dx[] = {0, 0, -1, 1, -1, 1, -1, 1, 0};\nconstexpr int Dy[] = {1, -1, 0, 0, -1, -1, 1, 1, 0};\n\n#define REP(i, m, n) for (ll i = m; i < (ll)(n); ++i)\n#define rep(i, n) REP(i, 0, n)\n#define REPI(i, m, n) for (int i = m; i < (int)(n); ++i)\n#define repi(i, n) REPI(i, 0, n)\n#define MP make_pair\n#define MT make_tuple\n#define YES(n) cout << ((n) ? \"YES\" : \"NO\") << \"\\n\"\n#define Yes(n) cout << ((n) ? \"Yes\" : \"No\") << \"\\n\"\n#define possible(n) cout << ((n) ? \"possible\" : \"impossible\") << \"\\n\"\n#define Possible(n) cout << ((n) ? \"Possible\" : \"Impossible\") << \"\\n\"\n#define Yay(n) cout << ((n) ? \"Yay!\" : \":(\") << \"\\n\"\n#define all(v) v.begin(), v.end()\n#define NP(v) next_permutation(all(v))\n#define dbg(x) cerr << #x << \":\" << x << \"\\n\";\n#define UNIQUE(v) v.erase(unique(all(v)), v.end())\n\nstruct io_init {\n io_init() {\n cin.tie(nullptr);\n ios::sync_with_stdio(false);\n cout << setprecision(30) << setiosflags(ios::fixed);\n };\n} io_init;\ntemplate <typename T>\ninline bool chmin(T& a, T b) {\n if (a > b) {\n a = b;\n return true;\n }\n return false;\n}\ntemplate <typename T>\ninline bool chmax(T& a, T b) {\n if (a < b) {\n a = b;\n return true;\n }\n return false;\n}\ninline ll CEIL(ll a, ll b) { return (a + b - 1) / b; }\ntemplate <typename A, size_t N, typename T>\ninline void Fill(A (&array)[N], const T& val) {\n fill((T*)array, (T*)(array + N), val);\n}\ntemplate <typename T>\nvector<T> compress(vector<T>& v) {\n vector<T> val = v;\n sort(all(val)), val.erase(unique(all(val)), val.end());\n for (auto&& vi : v) vi = lower_bound(all(val), vi) - val.begin();\n return val;\n}\ntemplate <typename T, typename U>\nconstexpr istream& operator>>(istream& is, pair<T, U>& p) noexcept {\n is >> p.first >> p.second;\n return is;\n}\ntemplate <typename T, typename U>\nconstexpr ostream& operator<<(ostream& os, pair<T, U> p) noexcept {\n os << p.first << \" \" << p.second;\n return os;\n}\nostream& operator<<(ostream& os, mint m) {\n os << m.val();\n return os;\n}\nostream& operator<<(ostream& os, modint m) {\n os << m.val();\n return os;\n}\n\nrandom_device seed_gen;\nmt19937_64 engine(seed_gen());\n\nstruct BiCoef {\n vector<mint> fact_, inv_, finv_;\n BiCoef(int n) noexcept : fact_(n, 1), inv_(n, 1), finv_(n, 1) {\n fact_.assign(n, 1), inv_.assign(n, 1), finv_.assign(n, 1);\n for (int i = 2; i < n; i++) {\n fact_[i] = fact_[i - 1] * i;\n inv_[i] = -inv_[MOD % i] * (MOD / i);\n finv_[i] = finv_[i - 1] * inv_[i];\n }\n }\n mint c(ll n, ll k) const noexcept {\n if (n < k || n < 0 || k < 0) return 0;\n return fact_[n] * finv_[k] * finv_[n - k];\n }\n mint P(ll n, ll k) const noexcept { return c(n, k) * fact_[k]; }\n mint H(ll n, ll k) const noexcept { return c(n + k - 1, k); }\n mint Ch1(ll n, ll k) const noexcept {\n if (n < 0 || k < 0) return 0;\n mint res = 0;\n for (int i = 0; i < n; i++)\n res += c(n, i) * mint(n - i).pow(k) * (i & 1 ? -1 : 1);\n return res;\n }\n mint fact(ll n) const noexcept {\n if (n < 0) return 0;\n return fact_[n];\n }\n mint inv(ll n) const noexcept {\n if (n < 0) return 0;\n return inv_[n];\n }\n mint finv(ll n) const noexcept {\n if (n < 0) return 0;\n return finv_[n];\n }\n};\n\nBiCoef bc(500010);\n\n#pragma endregion\n\ntemplate <typename T>\nvoid icpc(T a) {\n if (!a) exit(0);\n}\ntemplate <typename T, typename U>\nvoid icpc(T a, U end) {\n if (a == end) exit(0);\n}\n\n#pragma region Matrix\n\n// 参考・引用:https://qiita.com/gnbrganchan/items/47118d45b3af9d5ae9a4\n\nusing Type = modint;\n\nstruct Matrix {\n // 行列A\n vector<vector<Type>> A;\n // コンストラクタ:第1引数⇒行数、第2引数⇒列数、第3引数⇒初期値\n Matrix() : A() {}\n Matrix(int h, int w) : A(vector<vector<Type>>(h, vector<Type>(w))) {}\n Matrix(int h, int w, Type d)\n : A(vector<vector<Type>>(h, vector<Type>(w, d))) {}\n Matrix(vector<vector<Type>> A) : A(A) {}\n Matrix(initializer_list<initializer_list<Type>> A) : A(A.begin(), A.end()) {}\n // 添え字アクセス\n vector<Type> operator[](const int i) const { return A[i]; }\n vector<Type>& operator[](const int i) { return A[i]; }\n // 行数・列数\n int r = A.size();\n int c = (A.empty() ? 0 : A[0].size());\n // 行列同士の演算\n Matrix& operator+=(const Matrix& B) {\n assert(c == B.c && r == B.r);\n rep(i, r) rep(j, c) A[i][j] += B[i][j];\n return *this;\n }\n Matrix& operator-=(const Matrix& B) {\n assert(c == B.c && r == B.r);\n rep(i, r) rep(j, c) A[i][j] -= B[i][j];\n return *this;\n }\n Matrix& operator*=(const Matrix& B) {\n if (B.r == 1 and B.c == 1) {\n return (*this) *= B[0][0];\n } else if (r == 1 and c == 1) {\n Type k = A[0][0];\n (*this) = B;\n return (*this) *= k;\n }\n assert(c == B.r);\n Matrix m2(r, B.c, 0);\n rep(i, r) rep(j, B.c) rep(k, c) m2[i][j] += A[i][k] * B[k][j];\n c = B.c;\n rep(i, r) A[i].resize(c);\n rep(i, r) rep(j, c) A[i][j] = m2[i][j];\n return *this;\n }\n Matrix operator+(const Matrix& B) const { return Matrix(*this) += B; }\n Matrix operator-(const Matrix& B) const { return Matrix(*this) -= B; }\n Matrix operator*(const Matrix& B) const { return Matrix(*this) *= B; }\n vector<Type> operator*(const vector<Type>& b) const {\n assert(c == b.size());\n vector<Type> res(r);\n rep(i, r) rep(k, c) res[i] += A[i][k] * b[k];\n return res;\n }\n bool operator==(const Matrix& B) {\n assert(c == B.c && r == B.r);\n bool flg = true;\n rep(i, r) rep(j, c) if (A[i][j] != B[i][j]) flg = false;\n return flg;\n }\n // 行列とスカラの演算\n Matrix& operator+=(const Type& k) {\n rep(i, r) rep(j, c) A[i][j] += k;\n return *this;\n }\n Matrix& operator-=(const Type& k) {\n rep(i, r) rep(j, c) A[i][j] -= k;\n return *this;\n }\n Matrix& operator*=(const Type& k) {\n rep(i, r) rep(j, c) A[i][j] *= k;\n return *this;\n }\n Matrix& operator/=(const Type& k) {\n rep(i, r) rep(j, c) A[i][j] /= k;\n return *this;\n }\n Matrix operator+(const Type& k) const { return Matrix(*this) += k; }\n Matrix operator-(const Type& k) const { return Matrix(*this) -= k; }\n Matrix operator*(const Type& k) const { return Matrix(*this) *= k; }\n Matrix operator/(const Type& k) const { return Matrix(*this) /= k; }\n Matrix operator-() const { return Matrix(*this) *= -1; }\n // 回転(degの数だけ時計回りに90度回転)\n Matrix& rot(int deg) {\n Matrix m2(c, r);\n if (deg == 1 || deg == 3) {\n if (deg == 1) rep(i, r) rep(j, c) m2[j][r - i - 1] = A[i][j];\n if (deg == 3) rep(i, r) rep(j, c) m2[c - j - 1][i] = A[i][j];\n swap(c, r); // 列数と行数を入れ替える\n A.resize(r);\n rep(i, r) A[i].resize(c); //リサイズ\n }\n if (deg == 2) rep(i, r) rep(j, c) m2[r - i - 1][c - j - 1] = A[i][j];\n rep(i, r) rep(j, c) A[i][j] = m2[i][j];\n return *this;\n }\n // 転置\n Matrix& tran() {\n Matrix m2(c, r);\n rep(i, r) rep(j, c) m2[j][i] = A[i][j];\n (*this) = m2;\n return *this;\n }\n // 単位行列\n static Matrix E(int n) {\n Matrix res(n, n);\n rep(i, n) rep(j, n) res[i][j] = i == j;\n return res;\n }\n // 累乗\n Matrix pow(ll n) {\n assert(n >= 0);\n Matrix res = E(r);\n Matrix P = (*this);\n while (n > 0) {\n if (n & 1) res *= P;\n P *= P;\n n >>= 1;\n }\n return res;\n }\n};\n// 出力\nostream& operator<<(ostream& os, Matrix A) noexcept {\n rep(i, A.r) {\n if (i > 0) cout << \"\\n\";\n rep(j, A.c) { cout << A[i][j] << (j + 1 == A.c ? \"\" : \" \"); }\n }\n return os;\n}\n\n#pragma endregion\n\nMatrix A[26];\n\nMatrix var(string&, int&);\nMatrix expr(string&, int&);\nMatrix term(string&, int&);\nMatrix factor(string&, int&);\nMatrix primary(string&, int&);\nMatrix matrix(string&, int&);\nMatrix row_seq(string&, int&);\nMatrix row(string&, int&);\nMatrix inum(string&, int&);\n\nMatrix var(string& s, int& i) {\n assert(isupper(s[i]));\n return A[s[i++] - 'A'];\n}\n\nMatrix expr(string& s, int& i) {\n Matrix S = term(s, i);\n while (i < s.length() and (s[i] == '+' or s[i] == '-')) {\n if (s[i] == '+') {\n i++;\n S += term(s, i);\n } else {\n i++;\n S -= term(s, i);\n }\n }\n return S;\n}\n\nMatrix term(string& s, int& i) {\n Matrix P = factor(s, i);\n while (i < s.length() and s[i] == '*') {\n i++;\n P *= factor(s, i);\n }\n return P;\n}\n\nMatrix factor(string& s, int& i) {\n if (s[i] == '-') {\n i++;\n return -factor(s, i);\n }\n return primary(s, i);\n}\n\n// idnexed, transposed を兼ねる\nMatrix primary(string& s, int& i) {\n Matrix P;\n if (isdigit(s[i])) {\n P = inum(s, i);\n } else if (isupper(s[i])) {\n P = var(s, i);\n } else if (s[i] == '[') {\n P = matrix(s, i);\n } else if (s[i] == '(') {\n i++;\n P = expr(s, i);\n i++;\n }\n while (s[i] == '(' or s[i] == '\\'') {\n if (s[i] == '(') {\n i++;\n Matrix I = expr(s, i);\n assert(s[i++] == ',');\n Matrix J = expr(s, i);\n assert(s[i++] == ')');\n assert(I.r == 1);\n assert(J.r == 1);\n vector<vector<modint>> v;\n Matrix res(I.c, J.c);\n rep(i, I.c) rep(j, J.c) {\n res[i][j] = P.A[I[0][i].val() - 1][J[0][j].val() - 1];\n }\n P = res;\n } else if (s[i] == '\\'') {\n i++;\n P.tran();\n }\n }\n return P;\n}\n\nMatrix matrix(string& s, int& i) {\n assert(s[i++] == '[');\n Matrix res = row_seq(s, i);\n assert(s[i++] == ']');\n return res;\n}\n\nMatrix row_seq(string& s, int& i) {\n Matrix P = row(s, i);\n vector<vector<modint>> v;\n rep(i, P.r) v.push_back(P.A[i]);\n while (i < s.length() and s[i] == ';') {\n i++;\n P = row(s, i);\n assert(v[0].size() == P.c);\n rep(i, P.r) v.push_back(P.A[i]);\n }\n return Matrix(v);\n}\n\nMatrix row(string& s, int& i) {\n Matrix P = expr(s, i);\n vector<vector<modint>> v = P.A;\n while (i < s.length() and s[i] == ' ') {\n i++;\n P = expr(s, i);\n assert(v.size() == P.r);\n rep(i, P.r) {\n rep(j, P.c) { v[i].push_back(P[i][j]); }\n }\n }\n return Matrix(v);\n}\n\n// digit を兼ねる\nMatrix inum(string& s, int& i) {\n modint val = 0;\n while (i < s.length() and isdigit(s[i])) {\n val = val * 10 + s[i] - '0';\n i++;\n }\n return Matrix(1, 1, val);\n}\n\nvoid solve() {\n int n;\n cin >> n;\n cin.ignore();\n icpc(n);\n\n rep(i, n) {\n string s;\n getline(cin, s);\n int num = s[0] - 'A';\n s = s.substr(2);\n s.pop_back();\n int tmp = 0;\n A[num] = expr(s, tmp);\n cout << A[num] << \"\\n\";\n }\n cout << \"-----\\n\";\n}\n\nint main() {\n modint::set_mod(32768);\n while (1) solve();\n}", "accuracy": 1, "time_ms": 100, "memory_kb": 9372, "score_of_the_acc": -2, "final_rank": 20 }, { "submission_id": "aoj_1314_5444999", "code_snippet": "#include <iostream>\n#include <vector>\n#include <queue>\n#include <cassert>\nusing namespace std;\nusing ll = long long;\nusing ull = unsigned long long;\n\nvector<int> getchar_buffer;\nint my_getchar(){\n if(getchar_buffer.size()){\n int res = getchar_buffer.back();\n getchar_buffer.pop_back();\n return res;\n }\n return getchar();\n}\n\nint readInt(){\n int n = 0;\n while(true){\n int c = my_getchar();\n if(c < '0' || '9' < c){\n getchar_buffer.push_back(c);\n break;\n }\n n = n * 10 + (c-'0');\n }\n return n;\n}\n\n\nconst int modmask = 0x7fff;\n\n\nstruct Matrix{\n int H,W;\n vector<int> G;\n Matrix(int h,int w){\n H = h;\n W = w;\n G.assign(H*W,0);\n }\n int* operator[](int y){ return G.data() + (y*W); }\n const int* operator[](int y) const { return G.data() + (y*W); }\n\n void print() const {\n for(int y=0; y<H; y++){\n for(int x=0; x<W; x++){\n if(x) printf(\" \");\n printf(\"%d\",operator[](y)[x]);\n }\n printf(\"\\n\");\n }\n }\n};\n\nMatrix operator+(const Matrix& l, const Matrix& r){\n assert(l.H == r.H);\n assert(l.W == r.W);\n Matrix res(l.H,l.W);\n for(int y=0; y<l.H; y++) for(int x=0; x<l.W; x++) res[y][x] = (l[y][x] + r[y][x]) & modmask;\n return move(res);\n}\nMatrix operator-(const Matrix& l, const Matrix& r){\n assert(l.H == r.H);\n assert(l.W == r.W);\n Matrix res(l.H,l.W);\n for(int y=0; y<l.H; y++) for(int x=0; x<l.W; x++) res[y][x] = (l[y][x] + (modmask+1) - r[y][x]) & modmask;\n return move(res);\n}\nMatrix operator-(const Matrix& r){\n Matrix res(r.H,r.W);\n for(int y=0; y<r.H; y++) for(int x=0; x<r.W; x++) res[y][x] = ((modmask+1) - r[y][x]) & modmask;\n return move(res);\n}\nMatrix operator*(const Matrix& l, const Matrix& r){\n if(l.W == 1 && l.H == 1){\n Matrix res(r.H,r.W);\n for(int y=0; y<r.H; y++) for(int x=0; x<r.W; x++) res[y][x] = (l[0][0] * r[y][x]) & modmask;\n return move(res);\n }\n if(r.W == 1 && r.H == 1){\n Matrix res(l.H,l.W);\n for(int y=0; y<l.H; y++) for(int x=0; x<l.W; x++) res[y][x] = (l[y][x] * r[0][0]) & modmask;\n return move(res);\n }\n assert(l.W == r.H);\n Matrix res(l.H,r.W);\n for(int y=0; y<l.H; y++) for(int j=0; j<l.W; j++) for(int x=0; x<r.W; x++)\n res[y][x] = (res[y][x] + l[y][j] * r[j][x]) & modmask;\n return move(res);\n}\nMatrix matrix_catW(const Matrix& l, const Matrix& r){\n assert(l.H == r.H);\n Matrix res(l.H,l.W+r.W);\n for(int y=0; y<l.H; y++) for(int x=0; x<l.W; x++) res[y][x] = l[y][x];\n for(int y=0; y<r.H; y++) for(int x=0; x<r.W; x++) res[y][x+l.W] = r[y][x];\n return move(res);\n}\nMatrix matrix_catH(const Matrix& l, const Matrix& r){\n assert(l.W == r.W);\n Matrix res(l.H+r.H,l.W);\n for(int y=0; y<l.H; y++) for(int x=0; x<l.W; x++) res[y][x] = l[y][x];\n for(int y=0; y<r.H; y++) for(int x=0; x<r.W; x++) res[y+l.H][x] = r[y][x];\n return move(res);\n}\nMatrix matrix_trans(const Matrix& l){\n Matrix res(l.W,l.H);\n for(int y=0; y<l.W; y++) for(int x=0; x<l.H; x++) res[y][x] = l[x][y];\n return move(res);\n}\nMatrix matrix_index_primary(const Matrix& l, const Matrix& ipy, const Matrix& ipx){\n assert(ipy.H == 1);\n assert(ipx.H == 1);\n Matrix res(ipy.W,ipx.W);\n for(int y=0; y<ipy.W; y++) for(int x=0; x<ipx.W; x++) res[y][x] = l[ipy[0][y]-1][ipx[0][x]-1];\n return move(res);\n}\n\n\n\nMatrix readExp(const vector<Matrix>& A){\n vector<Matrix> st_operands;\n vector<int> st_operators;\n vector<int> st_primary_state = {0};\n bool prev_primary = false;\n\n auto reduce_operator = [&](int op)->bool{\n if(st_operators.empty()) return false;\n if(st_operators.back() != op) return false;\n st_operators.pop_back();\n //printf(\"reduce_operator(%c)\\n\",(char)op);\n do /* while(0) */ {\n if(op == '['){ break; }\n if(op == '('){ st_primary_state.pop_back(); break; }\n auto r = move(st_operands.back()); st_operands.pop_back();\n if(op == '_'){ st_operands.push_back(-r); continue; }\n auto l = move(st_operands.back()); st_operands.pop_back();\n if(op == ' '){ st_operands.push_back(matrix_catW(l,r)); break; }\n if(op == ';'){ st_operands.push_back(matrix_catH(l,r)); break; }\n if(op == '+'){ st_operands.push_back(l+r); break; }\n if(op == '-'){ st_operands.push_back(l-r); break; }\n if(op == '*'){ st_operands.push_back(l*r); break; }\n if(op == ','){ st_operands.push_back(l*r); break; }\n } while(0);\n return true;\n };\n\n bool done = false;\n while(!done){\n int nxc = my_getchar();\n assert(nxc != '\\n');\n if(nxc == '.'){\n while(reduce_operator('_'));\n while(reduce_operator('*'));\n while(reduce_operator('+') || reduce_operator('-'));\n done = true;\n }\n else if(nxc == '('){\n if(prev_primary){\n //printf(\"index primary begins\\n\");\n st_primary_state.push_back(1);\n st_operators.push_back(nxc);\n }\n else{\n st_primary_state.push_back(0);\n st_operators.push_back(nxc);\n }\n prev_primary = false;\n }\n else if(nxc == ','){\n while(reduce_operator('_'));\n while(reduce_operator('*'));\n while(reduce_operator('+') || reduce_operator('-'));\n st_primary_state.back() = 2;\n prev_primary = false;\n }\n else if(nxc == ')'){\n if(st_primary_state.back() == 2){\n while(reduce_operator('_'));\n while(reduce_operator('*'));\n while(reduce_operator('+') || reduce_operator('-'));\n reduce_operator('(');\n Matrix ipx = move(st_operands.back()); st_operands.pop_back();\n Matrix ipy = move(st_operands.back()); st_operands.pop_back();\n st_operands.back() = matrix_index_primary(st_operands.back(), ipy, ipx);\n prev_primary = true;\n }\n else{\n while(reduce_operator('_'));\n while(reduce_operator('*'));\n while(reduce_operator('+') || reduce_operator('-'));\n reduce_operator('(');\n }\n }\n else if(nxc == ' '){\n while(reduce_operator('_'));\n while(reduce_operator('*'));\n while(reduce_operator('+') || reduce_operator('-'));\n while(reduce_operator(' '));\n st_operators.push_back(nxc);\n prev_primary = false;\n }\n else if(nxc == ';'){\n while(reduce_operator('_'));\n while(reduce_operator('*'));\n while(reduce_operator('+') || reduce_operator('-'));\n while(reduce_operator(' '));\n while(reduce_operator(';'));\n st_operators.push_back(nxc);\n prev_primary = false;\n }\n else if(nxc == '[') st_operators.push_back(nxc);\n else if(nxc == ']'){\n while(reduce_operator('_'));\n while(reduce_operator('*'));\n while(reduce_operator('+') || reduce_operator('-'));\n while(reduce_operator(' '));\n while(reduce_operator(';'));\n reduce_operator('[');\n prev_primary = true;\n }\n else if(nxc == '\\''){\n st_operands.back() = matrix_trans(st_operands.back());\n prev_primary = true;\n }\n else if(nxc == '+'){\n while(reduce_operator('_'));\n while(reduce_operator('*'));\n while(reduce_operator('+') || reduce_operator('-'));\n st_operators.push_back(nxc);\n prev_primary = false;\n }\n else if(nxc == '-'){\n if(prev_primary){\n while(reduce_operator('_'));\n while(reduce_operator('*'));\n while(reduce_operator('+') || reduce_operator('-'));\n st_operators.push_back(nxc);\n }\n else{\n st_operators.push_back('_');\n }\n prev_primary = false;\n }\n else if(nxc == '*'){\n while(reduce_operator('_'));\n while(reduce_operator('*'));\n st_operators.push_back(nxc);\n prev_primary = false;\n }\n else if('0' <= nxc && nxc <= '9'){\n getchar_buffer.push_back(nxc);\n int nxval = readInt();\n st_operands.push_back(Matrix(1,1));\n st_operands.back()[0][0] = nxval;\n prev_primary = true;\n }\n else if('A' <= nxc && nxc <= 'Z'){\n st_operands.push_back(A[nxc-'A']);\n prev_primary = true;\n }\n /*\n printf(\"############################\\n\");\n for(auto a : st_operands){\n a.print();\n printf(\"- - - - - - - - - - - \\n\");\n }\n for(auto a : st_operators){\n printf(\"%c\",(char)a);\n }\n printf(\"\\n- - - - - - - - - - - \\n\");\n fflush(stdout);\n */\n }\n // printf(\"############################\\n\");\n return move(st_operands.back());\n}\n\n\n\nbool testcase(){\n int n = readInt();\n int newline_after_n = my_getchar(); assert(newline_after_n == '\\n');\n if(n == 0) return false;\n vector<Matrix> A(26,Matrix(1,1));\n for(int i=0; i<n; i++){\n int assign_dst = my_getchar() - 'A';\n int assign_equal = my_getchar(); assert(assign_equal == '=');\n A[assign_dst] = readExp(A);\n A[assign_dst].print();\n int assign_newline = my_getchar(); assert(assign_newline == '\\n');\n }\n printf(\"-----\\n\");\n return true;\n}\n\n\n\nint main(){\n while(testcase());\n return 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3608, "score_of_the_acc": -0.1094, "final_rank": 4 }, { "submission_id": "aoj_1314_5379472", "code_snippet": "#pragma GCC target(\"avx2\")\n#pragma GCC optimize(\"O3\")\n#pragma GCC optimize(\"unroll-loops\")\n\n#include<iostream>\n#include<string>\n#include<cstdio>\n#include<vector>\n#include<cmath>\n#include<algorithm>\n#include<functional>\n#include<iomanip>\n#include<queue>\n#include<ciso646>\n#include<random>\n#include<map>\n#include<set>\n#include<bitset>\n#include<stack>\n#include<unordered_map>\n#include<unordered_set>\n#include<utility>\n#include<cassert>\n#include<complex>\n#include<numeric>\n#include<array>\nusing namespace std;\n\n//#define int long long\ntypedef long long ll;\n\ntypedef unsigned long long ul;\ntypedef unsigned int ui;\nconstexpr ll mod = 32768;\nconst ll INF = mod * mod;\ntypedef pair<int, int>P;\n#define stop char nyaa;cin>>nyaa;\n#define rep(i,n) for(int i=0;i<n;i++)\n#define per(i,n) for(int i=n-1;i>=0;i--)\n#define Rep(i,sta,n) for(int i=sta;i<n;i++)\n#define rep1(i,n) for(int i=1;i<=n;i++)\n#define per1(i,n) for(int i=n;i>=1;i--)\n#define Rep1(i,sta,n) for(int i=sta;i<=n;i++)\n#define all(v) (v).begin(),(v).end()\ntypedef pair<ll, ll> LP;\ntypedef long double ld;\ntypedef pair<ld, ld> LDP;\nconst ld eps = 1e-12;\nconst ld pi = acosl(-1.0);\n\nll mod_pow(ll x, ll n, ll m = mod) {\n\tif (n < 0) {\n\t\tll res = mod_pow(x, -n, m);\n\t\treturn mod_pow(res, m - 2, m);\n\t}\n\tif (abs(x) >= m)x %= m;\n\tif (x < 0)x += m;\n\tll res = 1;\n\twhile (n) {\n\t\tif (n & 1)res = res * x % m;\n\t\tx = x * x % m; n >>= 1;\n\t}\n\treturn res;\n}\nstruct modint {\n\tll n;\n\tmodint() :n(0) { ; }\n\tmodint(ll m) :n(m) {\n\t\tif (n >= mod)n %= mod;\n\t\telse if (n < 0)n = (n % mod + mod) % mod;\n\t}\n\toperator int() { return n; }\n};\nbool operator==(modint a, modint b) { return a.n == b.n; }\nmodint operator+=(modint& a, modint b) { a.n += b.n; if (a.n >= mod)a.n -= mod; return a; }\nmodint operator-=(modint& a, modint b) { a.n -= b.n; if (a.n < 0)a.n += mod; return a; }\nmodint operator*=(modint& a, modint b) { a.n = ((ll)a.n * b.n) % mod; return a; }\nmodint operator+(modint a, modint b) { return a += b; }\nmodint operator-(modint a, modint b) { return a -= b; }\nmodint operator*(modint a, modint b) { return a *= b; }\nmodint operator^(modint a, ll n) {\n\tif (n == 0)return modint(1);\n\tmodint res = (a * a) ^ (n / 2);\n\tif (n % 2)res = res * a;\n\treturn res;\n}\n\nll inv(ll a, ll p) {\n\treturn (a == 1 ? 1 : (1 - p * inv(p % a, a)) / a + p);\n}\nmodint operator/(modint a, modint b) { return a * modint(inv(b, mod)); }\nmodint operator/=(modint& a, modint b) { a = a / b; return a; }\nconst int max_n = 1 << 2;\nmodint fact[max_n], factinv[max_n];\nvoid init_f() {\n\tfact[0] = modint(1);\n\tfor (int i = 0; i < max_n - 1; i++) {\n\t\tfact[i + 1] = fact[i] * modint(i + 1);\n\t}\n\tfactinv[max_n - 1] = modint(1) / fact[max_n - 1];\n\tfor (int i = max_n - 2; i >= 0; i--) {\n\t\tfactinv[i] = factinv[i + 1] * modint(i + 1);\n\t}\n}\nmodint comb(int a, int b) {\n\tif (a < 0 || b < 0 || a < b)return 0;\n\treturn fact[a] * factinv[b] * factinv[a - b];\n}\nmodint combP(int a, int b) {\n\tif (a < 0 || b < 0 || a < b)return 0;\n\treturn fact[a] * factinv[a - b];\n}\n\ntypedef complex<ld> Point;\nld dot(Point a, Point b) { return real(conj(a) * b); }\nld cross(Point a, Point b) { return imag(conj(a) * b); }\nnamespace std {\n\tbool operator<(const Point& lhs, const Point& rhs) {\n\t\treturn lhs.real() == rhs.real() ? lhs.imag() < rhs.imag() : lhs.real() < rhs.real();\n\t}\n}\nstruct Line {\n\tPoint a, b;\n};\nstruct Circle {\n\tPoint p; ld r;\n};\nint ccw(Point a, Point b, Point c) {\n\tb -= a; c -= a;\n\tif (cross(b, c) > eps)return 1;//counter clockwise\n\tif (cross(b, c) < -eps)return -1;//clock wise\n\tif (dot(b, c) < 0)return 2;//c--a--b on line\n\tif (norm(b) < norm(c))return -2;//a--b--c on line\n\treturn 0; //a--c--b on line\n}\nbool isis_ss(Line s, Line t) {\n\treturn 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;\n}\nPoint proj(Line l, Point p) {\n\tld t = dot(p - l.a, l.a - l.b) / norm(l.a - l.b);\n\treturn l.a + t * (l.a - l.b);\n}\n\n//点を直線で反転\nPoint rev_lp(Line l, Point p) {\n\tPoint h = proj(l, p);\n\treturn h + h - p;\n}\nPoint is_ll(Line s, Line t) {\n\tPoint sv = s.b - s.a; Point tv = t.b - t.a;\n\treturn s.a + sv * cross(tv, t.a - s.a) / cross(tv, sv);\n}\n\nusing mat = vector<vector<modint>>;\n\nmat memo[26];\n\nmat expr(string& s, int& i);\nmat term(string& s, int& i);\nmat factor(string& s, int& i);\nmat primary(string& s, int& i);\nmat inum(string& s, int& i);\nmat matrix(string& s, int& i);\nmat row_seq(string& s, int& i);\nmat row(string& s, int& i);\n\nmat expr(string& s, int& i) {\n\tmat res = term(s, i);\n\twhile (s[i] == '+' || s[i] == '-') {\n\t\tmodint c = 1; if (s[i] == '-')c = -1;\n\t\ti++;\n\t\tmat nex = term(s, i);\n\t\tassert(res.size() == nex.size() && res[0].size() == nex[0].size());\n\t\trep(i, res.size())rep(j, res[i].size())res[i][j] += c * nex[i][j];\n\t}\n\treturn res;\n}\nmat term(string& s, int& i) {\n\tmat res = factor(s, i);\n\twhile (s[i] == '*') {\n\t\ti++;\n\t\tmat nex = factor(s, i);\n\t\tif (res[0].size() != nex.size()) {\n\t\t\tif (res.size() == 1 && res[0].size() == 1) {\n\t\t\t\tswap(res, nex);\n\t\t\t\trep(i, res.size())rep(j, res[i].size()) {\n\t\t\t\t\tres[i][j] *= nex[0][0];\n\t\t\t\t}\n\t\t\t}\n\t\t\telse if (nex.size() == 1 && nex[0].size() == 1) {\n\t\t\t\trep(i, res.size())rep(j, res[i].size()) {\n\t\t\t\t\tres[i][j] *= nex[0][0];\n\t\t\t\t}\n\t\t\t}\n\t\t\telse {\n\t\t\t\tassert(false);\n\t\t\t}\n\t\t}\n\t\telse {\n\t\t\tint n = res.size(), m = nex[0].size();\n\t\t\tmat nres(n, vector<modint>(m));\n\t\t\trep(i, n)rep(j, m) {\n\t\t\t\trep(k, res[i].size()) {\n\t\t\t\t\tnres[i][j] += res[i][k] * nex[k][j];\n\t\t\t\t}\n\t\t\t}\n\t\t\tswap(res, nres);\n\t\t}\n\t}\n\treturn res;\n}\nmat factor(string& s, int& i) {\n\tif (s[i] == '-') {\n\t\ti++;\n\t\tmat res = factor(s, i);\n\t\trep(i, res.size())rep(j, res[i].size())res[i][j] *= -1;\n\t\treturn res;\n\t}\n\treturn primary(s, i);\n}\nmat primary(string& s, int& i) {\n\tmat res;\n\tif ('0' <= s[i] && s[i] <= '9') {\n\t\tres = inum(s, i);\n\t}\n\telse if ('A' <= s[i] && s[i] <= 'Z') {\n\t\tres = memo[s[i] - 'A']; i++;\n\t}\n\telse if (s[i] == '[') {\n\t\tres = matrix(s, i);\n\t}\n\telse if (s[i] == '(') {\n\t\ti++;\n\t\tres = expr(s, i);\n\t\ti++;\n\t}\n\telse {\n\t\tcout << s << \" \" << i << \"\\n\";\n\t\tassert(false);\n\t}\n\twhile (s[i] == '(' || s[i] == '\\'') {\n\t\tif (s[i] == '(') {\n\t\t\ti++;\n\t\t\tmat n1 = expr(s, i);\n\t\t\ti++;\n\t\t\tmat n2 = expr(s, i);\n\t\t\ti++;\n\t\t\tassert(n1.size() == 1 && n2.size() == 1);\n\t\t\tint n = n1[0].size();\n\t\t\tint m = n2[0].size();\n\t\t\tmat nres(n, vector<modint>(m));\n\t\t\trep(i, n)rep(j, m) {\n\t\t\t\tint x = n1[0][i] - (modint)1;\n\t\t\t\tint y = n2[0][j] - (modint)1;\n\t\t\t\tassert(x < res.size() && y < res[0].size());\n\t\t\t\tnres[i][j] = res[n1[0][i] - (modint)1][n2[0][j] - (modint)1];\n\t\t\t}\n\t\t\tswap(res, nres);\n\t\t}\n\t\telse {\n\t\t\ti++;\n\t\t\tint n = res.size();\n\t\t\tint m = res[0].size();\n\t\t\tmat nres(m, vector<modint>(n));\n\t\t\trep(i, m)rep(j, n) {\n\t\t\t\tnres[i][j] = res[j][i];\n\t\t\t}\n\t\t\tswap(res, nres);\n\t\t}\n\t}\n\treturn res;\n}\n\n\nmat matrix(string& s, int& i) {\n\ti++;\n\tmat res = row_seq(s, i);\n\ti++;\n\treturn res;\n}\nmat row_seq(string& s, int& i) {\n\tmat res = row(s, i);\n\twhile (s[i] == ';') {\n\t\ti++;\n\t\tmat nex = row(s, i);\n\t\tassert(res[0].size() == nex[0].size());\n\t\trep(j, nex.size())res.push_back(nex[j]);\n\t}\n\treturn res;\n}\nmat row(string& s, int& i) {\n\tmat res = expr(s, i);\n\twhile (s[i] == ' ') {\n\t\ti++;\n\t\tmat nex = expr(s, i);\n\t\tassert(res.size() == nex.size());\n\t\trep(i, res.size()) {\n\t\t\trep(j, nex[i].size()) {\n\t\t\t\tres[i].push_back(nex[i][j]);\n\t\t\t}\n\t\t}\n\t}\n\treturn res;\n}\nmat inum(string& s, int& i) {\n\tmodint val = 0;\n\twhile ('0' <= s[i] && s[i] <= '9') {\n\t\tval = val * (modint)10 + (modint)(s[i] - '0');\n\t\ti++;\n\t}\n\tmat res = { {val} };\n\treturn res;\n}\n\nint n;\nvoid solve() {\n\tstring s;\n\tgetline(cin, s);\n\trep(_, n) {\n\t\tgetline(cin, s);\n\t\tint c = s[0] - 'A';\n\t\ts.erase(s.begin(), s.begin() + 2);\n\t\tint i = 0;\n\t\tmat ans = expr(s, i);\n\t\tmemo[c] = ans;\n\t\trep(i, ans.size()) {\n\t\t\trep(j, ans[i].size()) {\n\t\t\t\tif (j > 0)cout << \" \";\n\t\t\t\tcout << ans[i][j];\n\t\t\t}\n\t\t\tcout << \"\\n\";\n\t\t}\n\t}\n\tcout << \"-----\\n\";\n}\n\nsigned main() {\n\tios::sync_with_stdio(false);\n\tcin.tie(0);\n\t//cout << fixed << setprecision(10);\n\t//init_f();\n\t//init();\n\t//expr();\n\t//int t; cin >> t;rep(i,t)\n\twhile (cin >> n, n)\n\t\tsolve();\n\treturn 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 4048, "score_of_the_acc": -0.1774, "final_rank": 18 }, { "submission_id": "aoj_1314_4973727", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\nusing ll = long long;\n#define var auto\n\nconst char newl = '\\n';\n\ntemplate<typename T1, typename T2>\ninline bool chmin(T1 &a, T2 b) { bool c = a > b; if (c) a = b; return c; }\ntemplate<typename T1, typename T2>\ninline bool chmax(T1 &a, T2 b) { bool c = a < b; if (c) a = b; return c; }\n\n//BEGIN CUT HERE\ntemplate<typename T, T MOD = 1000000007>\nstruct Mint{\n static constexpr T mod = MOD;\n T v;\n Mint():v(0){}\n Mint(signed v):v(v){}\n Mint(long long t){v=t%MOD;if(v<0) v+=MOD;}\n\n Mint pow(long long k){\n Mint res(1),tmp(v);\n while(k){\n if(k&1) res*=tmp;\n tmp*=tmp;\n k>>=1;\n }\n return res;\n }\n\n static Mint add_identity(){return Mint(0);}\n static Mint mul_identity(){return Mint(1);}\n\n Mint inv(){return pow(MOD-2);}\n\n Mint& operator+=(Mint a){v+=a.v;if(v>=MOD)v-=MOD;return *this;}\n Mint& operator-=(Mint a){v+=MOD-a.v;if(v>=MOD)v-=MOD;return *this;}\n Mint& operator*=(Mint a){v=1LL*v*a.v%MOD;return *this;}\n Mint& operator/=(Mint a){return (*this)*=a.inv();}\n\n Mint operator+(Mint a) const{return Mint(v)+=a;}\n Mint operator-(Mint a) const{return Mint(v)-=a;}\n Mint operator*(Mint a) const{return Mint(v)*=a;}\n Mint operator/(Mint a) const{return Mint(v)/=a;}\n\n Mint operator-() const{return v?Mint(MOD-v):Mint(v);}\n\n bool operator==(const Mint a)const{return v==a.v;}\n bool operator!=(const Mint a)const{return v!=a.v;}\n bool operator <(const Mint a)const{return v <a.v;}\n\n static Mint comb(long long n,int k){\n Mint num(1),dom(1);\n for(int i=0;i<k;i++){\n num*=Mint(n-i);\n dom*=Mint(i+1);\n }\n return num/dom;\n }\n};\ntemplate<typename T, T MOD> constexpr T Mint<T, MOD>::mod;\ntemplate<typename T, T MOD>\nostream& operator<<(ostream &os,Mint<T, MOD> m){os<<m.v;return os;}\n//END CUT HERE\n\n//BEGIN CUT HERE\ntemplate<typename K>\nstruct Matrix{\n typedef vector<K> arr;\n typedef vector<arr> mat;\n size_t r, c;\n mat dat;\n\n Matrix(){}\n Matrix(size_t r,size_t c):dat(r,arr(c,K())),r(r),c(c){}\n Matrix(mat dat):dat(dat),r(dat.size()),c(dat[0].size()){}\n\n size_t size() const{return dat.size();}\n bool empty() const{return size()==0;}\n arr& operator[](size_t k){return dat[k];}\n const arr& operator[](size_t k) const {return dat[k];}\n\n const void operator+=(Matrix<K>& b){\n for (int i = 0; i < r; i++){\n for (int j = 0; j < c; j++){\n dat[i][j] += b[i][j];\n }\n }\n }\n const Matrix<K> operator+(Matrix<K>& b){\n var res = Matrix<K>(dat);\n res += b;\n return res;\n }\n const void operator-=(Matrix<K>& b){\n for (int i = 0; i < r; i++){\n for (int j = 0; j < c; j++){\n dat[i][j] -= b[i][j];\n }\n }\n }\n const Matrix<K> operator-(Matrix<K>& b){\n var res = Matrix<K>(dat);\n res -= b;\n return res;\n }\n\n const void operator*=(K& b){\n for (int i = 0; i < r; i++){\n for (int j = 0; j < c; j++){\n dat[i][j] *= b;\n }\n }\n }\n\n static Matrix cross(const Matrix &A,const Matrix &B){\n Matrix res(A.size(),B[0].size());\n for(int i=0;i<(int)A.size();i++)\n for(int j=0;j<(int)B[0].size();j++)\n for(int k=0;k<(int)B.size();k++)\n res[i][j]+=A[i][k]*B[k][j];\n return res;\n }\n\n\n\n Matrix<K> transpose(){\n Matrix<K> res(c, r);\n for (int i = 0; i < r; i++){\n for (int j = 0; j < c; j++){\n res[j][i] = dat[i][j];\n }\n }\n return res;\n }\n};\n//END CUT HERE\n\nusing M = Mint<int, 32768>;\nusing Mat = Matrix<M>;\n\nmap<char, Mat> mats;\n\n//A = expr\npair<char, Mat> assignment(string&, int&);\n//term+term-term+...\nMat expr(string&, int&);\n//primary*primary*...\nMat term(string&, int&);\n//---...\n//num/variable/matrix\n//(transposed or indexed)...\nMat primary(string&, int&);\nMat matrix(string&, int&);\n\npair<char, Mat> assignment(string& s, int& i){\n assert(isalpha(s[i]));\n var variable = s[i];\n i++;\n assert(s[i] == '=');\n i++;\n var value = expr(s, i);\n return pair<char, Mat>(variable, value);\n}\n\nMat expr(string& s, int& i){\n var res = term(s, i);\n while (i < s.size() && (s[i] == '+' || s[i] == '-')){\n var op = s[i];\n i++;\n var nxt = term(s, i);\n res = op == '+' ? res + nxt : res - nxt;\n }\n return res;\n}\n\nMat term(string& s, int& i){\n var res = primary(s, i);\n while (i < s.size() && s[i] == '*'){\n i++;\n var nxt = primary(s, i);\n if (res.r == 1 && res.c == 1){\n nxt *= res.dat[0][0];\n res = nxt;\n }\n else if (nxt.r == 1 && nxt.c == 1){\n res *= nxt.dat[0][0];\n }\n else res = Mat::cross(res, nxt);\n }\n return res;\n}\n\nMat primary(string& s, int& i){\n ll sign = 1;\n while (s[i] == '-'){\n sign *= -1;\n i++;\n }\n Mat res;\n if (isdigit(s[i])){\n ll num = 0;\n while (i < s.size() && isdigit(s[i])){\n num = num * 10 + s[i] - '0';\n i++;\n }\n res = Mat(1, 1);\n res[0][0] = M(num);\n }\n else if (isalpha(s[i])){\n res = mats[s[i]];\n i++;\n }\n else if (s[i] == '('){\n i++;\n res = expr(s, i);\n assert(s[i] == ')'); i++;\n }\n else{\n res = matrix(s, i);\n }\n while (i < s.size()){\n //O(nm) 100*100\n if (s[i] == '\\''){\n res = res.transpose();\n i++;\n continue;\n }\n if (s[i] == '('){\n i++;\n var a = expr(s, i);\n assert(s[i] == ','); i++;\n var b = expr(s, i);\n assert(s[i] == ')'); i++;\n assert(a.r == 1 && b.r == 1);\n\n Mat k(a.c, b.c);\n for (int i = 0; i < k.r; i++){\n for (int j = 0; j < k.c; j++){\n k[i][j] = res[a[0][i].v - 1][b[0][j].v - 1];\n }\n }\n\n res = k;\n continue;\n }\n break;\n }\n for (int i = 0; i < res.r; i++){\n for (int j = 0; j < res.c; j++){\n res[i][j] = res[i][j] * M(sign);\n }\n }\n return res;\n}\n\nMat matrix(string& s, int& i){\n assert(s[i] == '['); i++;\n vector<vector<M>> res;\n while (true){\n int baserow = -1;\n bool isfirst = true;\n while (true){\n var row = expr(s, i);\n if (isfirst){\n baserow = res.size();\n for (int i = 0; i < row.r; i++){\n res.push_back(vector<M>{});\n }\n isfirst = false;\n }\n for (int i = 0; i < row.r; i++){\n for (int j = 0; j < row.c; j++){\n res[baserow + i].emplace_back(row[i][j]);\n }\n }\n if (s[i] == ' '){\n i++;\n continue;\n }\n else break;\n }\n if (s[i] == ';'){\n i++;\n continue;\n }\n else break;\n }\n assert(s[i] == ']'); i++;\n return res;\n}\n\nvoid dump(Mat m){\n for (int i = 0; i < m.r; i++){\n for (int j = 0; j < m.c; j++){\n if (j != 0) cout << \" \";\n cout << m[i][j];\n }\n cout << endl;\n }\n}\n\nsigned main() {\n cin.tie(0);\n ios::sync_with_stdio(0);\n int n;\n while (cin >> n, n){\n cin.ignore();\n for (int iter = 0; iter < n; iter++){\n string s;\n getline(cin, s);\n int i = 0;\n var [variable, value] = assignment(s, i);\n dump(value);\n mats[variable] = value;\n }\n cout << \"-----\" << endl;\n }\n return 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3672, "score_of_the_acc": -0.1193, "final_rank": 7 }, { "submission_id": "aoj_1314_3179239", "code_snippet": "#include <bits/stdc++.h>\n#define MOD 32768LL\nusing namespace std;\ntypedef long long ll;\ntypedef pair<int,int> P;\ntypedef vector<ll> vec;\ntypedef vector<vec> mat;\n\nclass val{\npublic:\n\tmat m;\n\tll v;\n\tint t;\n\tval(){\n\t\tt=0;\n\t\tv=0;\n\t}\n\n\tval(int tt,ll vv){\n\t\tt=tt;\n\t\tv=vv;\n\t}\n\n\tval(mat mm){\n\t\tt=1;\n\t\tm=mm;\n\t\tfor(int i=0;i<m.size();i++){\n\t\t\tfor(int j=0;j<m[0].size();j++){\n\t\t\t\tm[i][j]%=MOD;\n\t\t\t\tif(m[i][j]<0LL)m[i][j]+=MOD;\n\t\t\t}\n\t\t}\n\t}\n\tval(ll vv){\n\t\tv=vv;\n\t\tv%=MOD;\n\t\tif(v<0LL){\n\t\t\tv+=MOD;\n\t\t}\n\t\tt=0;\n\t}\n\tval operator+ (val va){\n\t\tif(va.t==1 && va.m.size()==1 && va.m[0].size()==1){\n\t\t\tva.t=0;\n\t\t\tva.v=va.m[0][0];\n\t\t}\n\t\tif(t==1 && m.size()==1 && m[0].size()==1){\n\t\t\tt=0;\n\t\t\tv=m[0][0];\n\t\t}\n\t\tif(t==0 && va.t==0){\n\t\t\treturn val(va.v+v);\n\t\t}\n\t\tif(t==0 && va.t==1){\n\t\t\tfor(int i=0;i<va.m.size();i++){\n\t\t\t\tfor(int j=0;j<va.m[0].size();j++){\n\t\t\t\t\tva.m[i][j]+=v;\n\t\t\t\t}\n\t\t\t}\n\t\t\treturn mat(va.m);\n\t\t}\n\t\tif(t==1 && va.t==0){\n\t\t\tfor(int i=0;i<m.size();i++){\n\t\t\t\tfor(int j=0;j<m[0].size();j++){\n\t\t\t\t\tm[i][j]+=va.v;\n\t\t\t\t}\n\t\t\t}\n\t\t\treturn mat(m);\n\t\t}\n\t\tif(t==1 && va.t==1){\n\t\t\tfor(int i=0;i<va.m.size();i++){\n\t\t\t\tfor(int j=0;j<va.m[0].size();j++){\n\t\t\t\t\tm[i][j]+=va.m[i][j];\n\t\t\t\t}\n\t\t\t}\n\t\t\treturn mat(m);\n\t\t}\n\t}\n\tval operator- (val va){\n\t\tif(va.t==1 && va.m.size()==1 && va.m[0].size()==1){\n\t\t\tva.t=0;\n\t\t\tva.v=va.m[0][0];\n\t\t}\n\t\tif(t==1 && m.size()==1 && m[0].size()==1){\n\t\t\tt=0;\n\t\t\tv=m[0][0];\n\t\t}\n\t\tif(t==0 && va.t==0){\n\t\t\treturn val(v-va.v);\n\t\t}\n\t\tif(t==0 && va.t==1){\n\t\t\tfor(int i=0;i<va.m.size();i++){\n\t\t\t\tfor(int j=0;j<va.m[0].size();j++){\n\t\t\t\t\tva.m[i][j]=v-va.m[i][j];\n\t\t\t\t}\n\t\t\t}\n\t\t\treturn mat(va.m);\n\t\t}\n\t\tif(t==1 && va.t==0){\n\t\t\tfor(int i=0;i<m.size();i++){\n\t\t\t\tfor(int j=0;j<m[0].size();j++){\n\t\t\t\t\tm[i][j]-=va.v;\n\t\t\t\t}\n\t\t\t}\n\t\t\treturn mat(m);\n\t\t}\n\t\tif(t==1 && va.t==1){\n\t\t\tfor(int i=0;i<va.m.size();i++){\n\t\t\t\tfor(int j=0;j<va.m[0].size();j++){\n\t\t\t\t\tm[i][j]-=va.m[i][j];\n\t\t\t\t}\n\t\t\t}\n\t\t\treturn mat(m);\n\t\t}\n\t}\n\tval operator* (val va){\n\t\tif(va.t==1 && va.m.size()==1 && va.m[0].size()==1){\n\t\t\tva.t=0;\n\t\t\tva.v=va.m[0][0];\n\t\t}\n\t\tif(t==1 && m.size()==1 && m[0].size()==1){\n\t\t\tt=0;\n\t\t\tv=m[0][0];\n\t\t}\n\t\tif(t==0 && va.t==0){\n\t\t\treturn val(v*va.v);\n\t\t}\n\t\tif(t==0 && va.t==1){\n\t\t\tfor(int i=0;i<va.m.size();i++){\n\t\t\t\tfor(int j=0;j<va.m[0].size();j++){\n\t\t\t\t\tva.m[i][j]=v*va.m[i][j];\n\t\t\t\t}\n\t\t\t}\n\t\t\treturn mat(va.m);\n\t\t}\n\t\tif(t==1 && va.t==0){\n\t\t\tfor(int i=0;i<m.size();i++){\n\t\t\t\tfor(int j=0;j<m[0].size();j++){\n\t\t\t\t\tm[i][j]*=va.v;\n\t\t\t\t}\n\t\t\t}\n\t\t\treturn mat(m);\n\t\t}\n\t\tif(t==1 && va.t==1){\n\t\t\tmat C(m.size(),vec(va.m[0].size()));\n\t\t\tfor(int i=0;i<m.size();i++){\n\t\t\t\tfor(int k=0;k<va.m.size();k++){\n\t\t\t\t\tfor(int j=0;j<va.m[0].size();j++){\n\t\t\t\t\t\tC[i][j]=(C[i][j]+(m[i][k]*va.m[k][j]%MOD))%MOD;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t\treturn mat(C);\n\t\t}\n\t}\n};\n\nval v[35];\n\ntypedef string::const_iterator State;\nval expression(State &begin);\n\nint number(State &begin){\n\tint ret=0;\n\twhile(isdigit(*begin)){\n\t\tret*=10;\n\t\tret+=*begin-'0';\n\t\tret%=32768LL;\n\t\tbegin++;\n\t}\n\treturn ret;\n}\n\nval row(State &begin){\n\tval ret=expression(begin);\n\tif(*begin==' '){\n\t\tbegin++;\n\t\tval v2=row(begin);\n\t\t//printf(\"%d %d\\n\",ret.t,v2.t);\n\t\tif(v2.t==0 && ret.t==0){\n\t\t\tret.t=1;\n\t\t\tret.m=mat(1,vec(2));\n\t\t\tret.m[0][0]=ret.v;\n\t\t\tret.m[0][1]=v2.v;\n\t\t}else if(v2.t==0){\n\t\t\tret.m[0].push_back(v2.v);\n\t\t}else if(ret.t==0){\n\t\t\tret.t=1;\n\t\t\tret.m=mat(1,vec(1));\n\t\t\tret.m[0][0]=ret.v;\n\t\t\tfor(int i=0;i<v2.m[0].size();i++){\n\t\t\t\tret.m[0].push_back(v2.m[0][i]);\n\t\t\t}\n\t\t}else if(ret.t==1 && v2.t==1){\n\t\t\t//printf(\"%d %d %d %d\\n\",ret.m.size(),ret.m[0].size(),v2.m.size(),v2.m[0].size());\n\t\t\tfor(int i=0;i<v2.m.size();i++){\n\t\t\t\tfor(int j=0;j<v2.m[i].size();j++){\n\t\t\t\t\tret.m[i].push_back(v2.m[i][j]);\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\treturn ret;\n\t}else{\n\t\treturn ret;\n\t}\n}\n\nval rowsqr(State &begin){\n\tval ret=row(begin);\n\twhile(1){\n\t\t//cout << \"rs \" << *begin << endl;\n\t\tif(*begin==';'){\n\t\t\tbegin++;\n\t\t\tval v2=rowsqr(begin);\n\t\t\tif(ret.t==0){\n\t\t\t\tret.t=1;\n\t\t\t\tret.m=mat(1,vec(1));\n\t\t\t\tret.m[0][0]=ret.v;\n\t\t\t}\n\t\t\tif(v2.t==0){\n\t\t\t\tret.m.push_back(vec(1,v2.v));\n\t\t\t}else{\n\t\t\t\tfor(int i=0;i<v2.m.size();i++){\n\t\t\t\t\tret.m.push_back(v2.m[i]);\n\t\t\t\t}\n\t\t\t}\n\t\t\t//printf(\"%d %d\\n\",ret.m.size(),ret.m[0].size());\n\t\t}else{\n\t\t\treturn ret;\n\t\t}\n\t}\n\t\n}\n\nval primary(State &begin){\n\tval ret=val(-1,0);\n\twhile(1){\n\t\tif(*begin=='['){\n\t\t\tbegin++;\n\t\t\tret=rowsqr(begin);\n\t\t\tbegin++;\n\t\t}else if((*begin)>='A' && (*begin)<='Z'){\n\t\t\tret=v[(*begin)-'A'];\n\t\t\tbegin++;\n\t\t}else if(*begin=='('){\n\t\t\tbegin++;\n\t\t\tval v1=expression(begin);\n\t\t\tif(*begin==')'){\n\t\t\t\tbegin++;\n\t\t\t\tret=v1;\n\t\t\t\tcontinue;\n\t\t\t}\n\t\t\tbegin++;\n\t\t\tval v2=expression(begin);\n\t\t\tbegin++;\n\t\t\tval ret2;\n\t\t\tret2.t=1;\n\t\t\tif(ret.t==0){\n\t\t\t\tret.m=mat(1,vec(1,ret.v));\n\t\t\t}\n\t\t\tif(v1.t==0){\n\t\t\t\tv1.m=mat(1,vec(1,v1.v));\n\t\t\t}\n\t\t\tif(v2.t==0){\n\t\t\t\tv2.m=mat(1,vec(1,v2.v));\n\t\t\t}\n\t\t\tret2.m=mat(v1.m[0].size(),vec(v2.m[0].size()));\n\t\t\tfor(int i=0;i<v1.m[0].size();i++){\n\t\t\t\tfor(int j=0;j<v2.m[0].size();j++){\n\t\t\t\t\tret2.m[i][j]=ret.m[v1.m[0][i]-1][v2.m[0][j]-1];\n\t\t\t\t}\n\t\t\t}\n\t\t\tret=ret2;\n\t\t}else if(*begin=='\\''){\n\t\t\tbegin++;\n\t\t\tval ret2;\n\t\t\tret2.t=ret.t;\n\t\t\tif(ret.t==0){\n\t\t\t\tret2.v=ret.v;\n\t\t\t}else if(ret.t==1){\n\t\t\t\tret2.m=mat(ret.m[0].size(),vec(ret.m.size()));\n\t\t\t\tfor(int i=0;i<ret.m.size();i++){\n\t\t\t\t\tfor(int j=0;j<ret.m[0].size();j++){\n\t\t\t\t\t\tret2.m[j][i]=ret.m[i][j];\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t\tret=ret2;\n\t\t}else if(isdigit(*begin)){\n\t\t\tret=val(number(begin));\n\t\t}else{\n\t\t\tbreak;\n\t\t}\n\t}\n\treturn ret;\n}\n\nval negation(val ret){\n\tif(ret.t==0){\n\t\tret.v=MOD-ret.v;\n\t\treturn ret;\n\t}else{\n\t\tfor(int i=0;i<ret.m.size();i++){\n\t\t\tfor(int j=0;j<ret.m[i].size();j++){\n\t\t\t\tret.m[i][j]=MOD-ret.m[i][j];\n\t\t\t}\n\t\t}\n\t\treturn ret;\n\t}\n}\n\nval factor(State &begin){\n\tif(*begin=='-'){\n\t\tbegin++;\n\t\tval ret=factor(begin);\n\t\treturn negation(ret);\n\t}else{\n\t\treturn primary(begin);\n\t}\n}\n\nval term(State &begin){\n\tval ret=factor(begin);\n\twhile(1){\n\t\tif(*begin=='*'){\n\t\t\tbegin++;\n\t\t\tret=ret*factor(begin);\n\t\t}else{\n\t\t\tbreak;\n\t\t}\n\t}\n\treturn ret;\n}\n\nval expression(State &begin){\n\tval ret=term(begin);\n\twhile(1){\n\t\tif(*begin=='+'){\n\t\t\tbegin++;\n\t\t\tret=ret+term(begin);\n\t\t}else if(*begin=='-'){\n\t\t\tbegin++;\n\t\t\tret=ret-term(begin);\n\t\t}else{\n\t\t\tbreak;\n\t\t}\n\t}\n\treturn ret;\n}\n\nint n;\n\nvoid solve(){\n\tfor(int i=0;i<26;i++){\n\t\tv[i].m.clear();\n\t\tv[i].t=0;\n\t}\n\tfor(int i=0;i<n;i++){\n\t\tstring str;\n\t\tgetline(cin,str);\n\t\tState b=str.begin();\n\t\tb++;\n\t\tb++;\n\t\tint pos=str[0]-'A';\n\t\tv[pos]=expression(b);\n\t\tif(v[pos].t==0){\n\t\t\tprintf(\"%lld\\n\",v[pos].v);\n\t\t}else{\n\t\t\tfor(int i=0;i<v[pos].m.size();i++){\n\t\t\t\tfor(int j=0;j<v[pos].m[0].size();j++){\n\t\t\t\t\tprintf(\"%lld%c\",v[pos].m[i][j],j+1==v[pos].m[0].size()?'\\n':' ');\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n\tprintf(\"-----\\n\");\n}\n\nint main(void){\n\twhile(1){\n\t\tscanf(\"%d%*c\",&n);\n\t\tif(n==0)break;\n\t\tsolve();\n\t}\n\treturn 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3732, "score_of_the_acc": -0.1286, "final_rank": 10 }, { "submission_id": "aoj_1314_3174459", "code_snippet": "#include<bits/stdc++.h>\ntypedef long long int ll;\ntypedef unsigned long long int ull;\n#define BIG_NUM 2000000000\n#define HUGE_NUM 99999999999999999\n//#define MOD 1000000007\n#define EPS 0.000000001\nusing namespace std;\n\n#define MOD 32768\ntypedef vector<int> V;\ntypedef vector<V> MATRIX;\n\nint N;\nint global_index;\nchar buf[105];\nMATRIX TABLE[26];\n\nMATRIX expr();\n\nMATRIX operator+(const MATRIX &A,const MATRIX &B){\n\n\tint num_row = A.size();\n\tint num_col = A[0].size();\n\n\tMATRIX RET(num_row,V(num_col));\n\n\tfor(int row = 0; row < num_row; row++){\n\t\tfor(int col = 0; col < num_col; col++){\n\t\t\tRET[row][col] = A[row][col]+B[row][col];\n\t\t\tRET[row][col] %= MOD;\n\t\t}\n\t}\n\n\treturn RET;\n}\n\nMATRIX operator-(const MATRIX &A,const MATRIX &B){\n\n\tint num_row = A.size();\n\tint num_col = A[0].size();\n\n\tMATRIX RET(num_row,V(num_col));\n\n\tfor(int row = 0; row < num_row; row++){\n\t\tfor(int col = 0; col < num_col; col++){\n\t\t\tRET[row][col] = A[row][col]-B[row][col];\n\t\t\tRET[row][col] = (RET[row][col]+MOD)%MOD;\n\t\t}\n\t}\n\n\treturn RET;\n}\n\nMATRIX operator*(const MATRIX &A, const MATRIX &B){\n\n\tint num_rowA = A.size();\n\tint num_colA = A[0].size();\n\n\tint num_rowB = B.size();\n\tint num_colB = B[0].size();\n\n\tif(num_rowA == 1 && num_colA == 1){ //Aがスカラー\n\n\t\tMATRIX RET(num_rowB,V(num_colB));\n\n\t\tfor(int row = 0; row < num_rowB; row++){\n\t\t\tfor(int col = 0; col < num_colB; col++){\n\t\t\t\tRET[row][col] = B[row][col]*A[0][0];\n\t\t\t\tRET[row][col] %= MOD;\n\t\t\t}\n\t\t}\n\n\t\treturn RET;\n\n\t}else if(num_rowB == 1 && num_colB == 1){ //Bがスカラー\n\n\t\tMATRIX RET(num_rowA,V(num_colA));\n\n\t\tfor(int row = 0; row < num_rowA; row++){\n\t\t\tfor(int col = 0; col < num_colA; col++){\n\t\t\t\tRET[row][col] = A[row][col]*B[0][0];\n\t\t\t\tRET[row][col] %= MOD;\n\t\t\t}\n\t\t}\n\n\t\treturn RET;\n\n\t}else{\n\n\t\tMATRIX RET(num_rowA,V(num_colB));\n\n\t\tfor(int row = 0; row < num_rowA; row++){\n\t\t\tfor(int col = 0; col < num_colB; col++){\n\t\t\t\tRET[row][col] = 0;\n\t\t\t\tfor(int a = 0; a < num_colA; a++){\n\t\t\t\t\tRET[row][col] += A[row][a]*B[a][col];\n\t\t\t\t\tRET[row][col] %= MOD;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\n\t\treturn RET;\n\t}\n}\n\nMATRIX operator-(const MATRIX &A){\n\n\n\tint num_rowA = A.size();\n\tint num_colA = A[0].size();\n\tMATRIX RET(num_rowA,V(num_colA));\n\n\tfor(int row = 0; row < num_rowA; row++){\n\t\tfor(int col = 0; col < num_colA; col++){\n\t\t\tRET[row][col] = (-A[row][col]+MOD)%MOD;\n\t\t}\n\t}\n\treturn RET;\n}\n\nMATRIX transpose(MATRIX A){\n\n\tint num_row = A[0].size();\n\tint num_col = A.size();\n\n\tMATRIX RET(num_row,V(num_col));\n\n\tfor(int row = 0; row < A.size(); row++){\n\t\tfor(int col = 0; col < A[0].size(); col++){\n\t\t\tRET[col][row] = A[row][col];\n\t\t}\n\t}\n\n\treturn RET;\n}\n\nMATRIX get_row(){\n\n\tMATRIX A = expr();\n\n\twhile(buf[global_index] == ' '){\n\t\tglobal_index++;\n\t\tMATRIX B = expr();\n\t\tfor(int a = 0; a < A.size(); a++){\n\t\t\tA[a].insert(A[a].end(),B[a].begin(),B[a].end());\n\t\t}\n\t}\n\treturn A;\n}\n\n\nMATRIX row_seq(){\n\tMATRIX A = get_row();\n\n\twhile(buf[global_index] == ';'){\n\t\tglobal_index++;\n\t\tMATRIX B = get_row();\n\t\tA.insert(A.end(),B.begin(),B.end());\n\t}\n\n\treturn A;\n}\n\nMATRIX matrix(){\n\n\tglobal_index++;\n\tMATRIX A = row_seq();\n\tglobal_index++;\n\n\treturn A;\n}\n\nMATRIX get_var(){\n\n\tMATRIX RET = TABLE[buf[global_index]-'A'];\n\tglobal_index++;\n\n\treturn RET;\n}\n\nMATRIX inum(){\n\n\tint tmp = 0;\n\n\twhile(buf[global_index] >= '0' && buf[global_index] <= '9'){\n\t\ttmp = 10*tmp+(buf[global_index]-'0');\n\t\ttmp %= MOD;\n\t\tglobal_index++;\n\t}\n\n\tMATRIX RET(1,V(1));\n\n\tRET[0][0] = tmp;\n\n\treturn RET;\n}\n\n\n\nMATRIX primary(){\n\n\tMATRIX A;\n\n\tif(buf[global_index] >= '0' && buf[global_index] <= '9'){\n\n\t\tA = inum();\n\n\t}else if(buf[global_index] >= 'A' && buf[global_index] <= 'Z'){\n\n\t\tA = get_var();\n\n\t}else if(buf[global_index] == '['){\n\n\t\tA = matrix();\n\n\t}else if(buf[global_index] == '('){\n\t\tglobal_index++;\n\t\tA = expr();\n\t\tglobal_index++;\n\t}\n\n\tbool FLG;\n\n\twhile(true){\n\t\tFLG = false;\n\n\t\tif(buf[global_index] == '('){\n\n\t\t\tFLG = true;\n\t\t\tglobal_index++;\n\t\t\tMATRIX B = expr();\n\t\t\tglobal_index++;\n\t\t\tMATRIX C = expr();\n\t\t\tglobal_index++;\n\n\t\t\tint num_row = B[0].size();\n\t\t\tint num_col = C[0].size();\n\n\t\t\tMATRIX TMP(num_row,V(num_col));\n\n\t\t\tfor(int row = 0; row < num_row; row++){\n\t\t\t\tfor(int col = 0; col < num_col; col++){\n\t\t\t\t\tTMP[row][col] = A[B[0][row]-1][C[0][col]-1];\n\t\t\t\t}\n\t\t\t}\n\t\t\tA = TMP;\n\n\t\t}else if(buf[global_index] == '\\''){\n\t\t\tFLG = true;\n\t\t\tglobal_index++;\n\t\t\tA = transpose(A);\n\t\t}\n\n\t\tif(!FLG)break;\n\t}\n\treturn A;\n}\n\n\nMATRIX factor(){\n\n\tif(buf[global_index] == '-'){\n\n\t\tglobal_index++;\n\t\treturn -factor();\n\n\t}else{\n\n\t\treturn primary();\n\t}\n}\n\nMATRIX term(){\n\n\tMATRIX A = factor();\n\n\twhile(buf[global_index] == '*'){\n\t\tglobal_index++;\n\t\tA = A*factor();\n\t}\n\treturn A;\n}\n\nMATRIX expr(){\n\n\tMATRIX A = term();\n\tchar op;\n\n\twhile(buf[global_index] == '+' || buf[global_index] == '-'){\n\n\t\top = buf[global_index++];\n\n\t\tif(op == '+'){\n\n\t\t\tA = A+term();\n\n\t\t}else{\n\n\t\t\tA = A-term();\n\t\t}\n\t}\n\n\treturn A;\n}\n\nvoid assignment(){\n\n\tint type = buf[0]-'A';\n\tglobal_index = 2;\n\tTABLE[type] = expr();\n\n\tglobal_index++;\n}\n\nvoid func(){\n\n\tgetchar();\n\tfor(int loop = 0; loop < N; loop++){\n\t\tfgets(buf,100,stdin);\n\n\t\tassignment();\n\n\t\tMATRIX A = TABLE[buf[0]-'A'];\n\t\tint num_row = A.size();\n\t\tint num_col = A[0].size();\n\n\t\tfor(int row = 0; row < num_row; row++){\n\t\t\tprintf(\"%d\",A[row][0]);\n\t\t\tfor(int col = 1; col < num_col; col++){\n\t\t\t\tprintf(\" %d\",A[row][col]);\n\t\t\t}\n\t\t\tprintf(\"\\n\");\n\t\t}\n\t}\n\tprintf(\"-----\\n\");\n}\n\n\nint main(){\n\n\twhile(true){\n\t\tscanf(\"%d\",&N);\n\t\tif(N == 0)break;\n\n\t\tfunc();\n\t}\n\n\treturn 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3380, "score_of_the_acc": -0.0742, "final_rank": 2 }, { "submission_id": "aoj_1314_2998151", "code_snippet": "#include <cstdio>\n#include <cstdint>\n#include <cctype>\n#include <cassert>\n#include <vector>\n#include <algorithm>\n#include <string>\n#include <map>\n\n#define fprintf(...) void(0)\n\nconst int M=32768;\n\nclass Matrix {\n size_t r=0, c=0;\n std::vector<std::vector<int>> entity;\n \npublic:\n Matrix(): entity(0) {}\n Matrix(int x): r(1), c(1), entity(1, std::vector<int>(1, x)) {}\n Matrix(size_t r, size_t c): r(r), c(c), entity(r, std::vector<int>(c)) {}\n Matrix(const Matrix &oth): r(oth.r), c(oth.c), entity(oth.entity) {}\n Matrix(Matrix &&oth): r(oth.r), c(oth.c), entity(std::move(oth.entity)) {}\n\n Matrix &operator =(const Matrix &oth) {\n r = oth.r;\n c = oth.c;\n entity = oth.entity;\n return *this;\n }\n\n Matrix &operator =(Matrix &&oth) {\n r = oth.r;\n c = oth.c;\n entity = std::move(oth.entity);\n return *this;\n }\n\n Matrix &operator +=(const Matrix &oth) {\n assert(r == oth.r && c == oth.c);\n for (size_t i=0; i<r; ++i)\n for (size_t j=0; j<c; ++j) {\n entity[i][j] += oth[i][j];\n if (entity[i][j] >= M) entity[i][j] -= M;\n }\n\n return *this;\n }\n\n Matrix operator +(const Matrix &oth) const {\n return Matrix(*this) += oth;\n }\n\n Matrix &operator -=(const Matrix &oth) {\n assert(r == oth.r && c == oth.c);\n for (size_t i=0; i<r; ++i)\n for (size_t j=0; j<c; ++j) {\n entity[i][j] -= oth[i][j];\n if (entity[i][j] < 0) entity[i][j] += M;\n }\n\n return *this;\n }\n\n Matrix operator -(const Matrix &oth) const {\n return Matrix(*this) -= oth;\n }\n\n Matrix operator *(const Matrix &oth) const {\n if (oth.r == 1 && oth.c == 1) {\n Matrix res(*this);\n for (size_t i=0; i<r; ++i)\n for (size_t j=0; j<c; ++j)\n res[i][j] = res[i][j]*oth[0][0] % M;\n\n return res;\n }\n if (r == 1 && c == 1) {\n Matrix res(oth);\n for (size_t i=0; i<oth.r; ++i)\n for (size_t j=0; j<oth.c; ++j)\n res[i][j] = res[i][j]*entity[0][0] % M;\n\n return res;\n }\n assert(c == oth.r);\n Matrix res(r, oth.c);\n for (size_t i=0; i<r; ++i)\n for (size_t j=0; j<c; ++j)\n for (size_t k=0; k<oth.c; ++k) {\n res[i][k] += entity[i][j] * oth[j][k];\n if ((res[i][k] %= M) < 0) res[i][k] += M;\n }\n\n return res;\n }\n\n Matrix &operator *=(const Matrix &oth) {\n Matrix rhs=(*this)*oth;\n return (*this = rhs);\n }\n\n const std::vector<int> &operator [](size_t i) const {\n return entity[i];\n }\n\n std::vector<int> &operator [](size_t i) {\n return entity[i];\n }\n\n Matrix operator [](\n const std::pair<std::vector<int>, std::vector<int>> &ind) const {\n\n size_t ir=ind.first.size(), ic=ind.second.size();\n Matrix res(ir, ic);\n for (size_t i=0; i<ir; ++i)\n for (size_t j=0; j<ic; ++j)\n res[i][j] = entity[ind.first[i]-1][ind.second[j]-1];\n\n return res;\n }\n\n Matrix operator -() const {\n Matrix res(r, c);\n for (size_t i=0; i<r; ++i)\n for (size_t j=0; j<c; ++j)\n if (entity[i][j])\n res[i][j] = M-entity[i][j];\n\n return res;\n }\n\n Matrix operator ~() const {\n Matrix res(c, r);\n for (size_t i=0; i<r; ++i)\n for (size_t j=0; j<c; ++j)\n res[j][i] = entity[i][j];\n\n return res;\n }\n\n size_t row() const {\n return entity.size();\n }\n\n size_t col() const {\n return entity[0].size();\n }\n\n void add_row() {\n entity.emplace_back();\n ++r;\n }\n\n void add_col(size_t i, int x) {\n entity[i].push_back(x);\n if (c < entity[i].size())\n c = entity[i].size();\n }\n\n void output() const {\n for (size_t i=0; i<r; ++i)\n for (size_t j=0; j<c; ++j)\n printf(\"%d%c\", entity[i][j], j+1<c? ' ':'\\n');\n }\n};\n\nMatrix parse(\n const std::string &s, size_t &i, const std::map<char, Matrix> &vars,\n size_t preced=0) {\n\n static const std::vector<std::string> &ops={\"+-\", \"*\"};\n\n fprintf(stderr, \"s[%zu]: %c #%zu\\n\", i, s[i], preced);\n if (preced == ops.size()) {\n if (s[i] == '-') {\n return -parse(s, ++i, vars, preced);\n }\n\n Matrix res;\n if (isdigit(s[i])) {\n int scl=s[i]-'0';\n while (++i < s.length() && isdigit(s[i]))\n scl = scl*10+s[i]-'0';\n if ((scl %= M) < 0) scl += M;\n res = Matrix(scl);\n } else if (isupper(s[i])) {\n res = vars.at(s[i++]);\n } else if (s[i] == '(') {\n res = parse(s, ++i, vars, 0);\n assert(s[i] == ')');\n ++i;\n } else if (s[i] == '[') {\n // tsurai\n ++i;\n size_t r=0, c=0;\n while (s[i] != ']') {\n Matrix tmp=parse(s, i, vars, 0);\n for (size_t ir=0; ir<tmp.row(); ++ir) {\n if (r+ir >= res.row()) res.add_row();\n for (size_t ic=0; ic<tmp.col(); ++ic) {\n // fprintf(stderr, \"r+ir: %zu\\n\", r+ir);\n if (c+ic < res[r+ir].size()) {\n res[r+ir][c+ic] = tmp[ir][ic];\n } else {\n res.add_col(r+ir, tmp[ir][ic]);\n }\n }\n }\n // fprintf(stderr, \"> s[%zu]\\n\", i);\n if (s[i] == ' ') {\n c += tmp.col();\n ++i;\n } else if (s[i] == ';') {\n r = res.row();\n c = 0;\n ++i;\n } else {\n assert(s[i] == ']');\n }\n }\n ++i;\n } else {\n assert(false);\n }\n\n while (s[i] == '\\'' || s[i] == '(') {\n char ch=s[i++];\n if (ch == '\\'') {\n res = ~res;\n } else /* if (ch == '(') */ {\n const std::vector<int> fst=parse(s, i, vars, 0)[0];\n assert(s[i] == ',');\n const std::vector<int> snd=parse(s, ++i, vars, 0)[0];\n assert(s[i] == ')');\n ++i;\n res = res[std::make_pair(fst, snd)];\n }\n }\n // fprintf(stderr, \">> s[%zu]\\n\", i);\n return res;\n }\n\n Matrix lhs=parse(s, i, vars, preced+1);\n while (i < s.length()) {\n char op=s[i];\n if (!std::count(ops[preced].begin(), ops[preced].end(), op)) break;\n Matrix rhs=parse(s, ++i, vars, preced+1);\n if (op == '+') {\n lhs += rhs;\n } else if (op == '-') {\n lhs -= rhs;\n } else if (op == '*') {\n lhs *= rhs;\n }\n }\n return lhs;\n}\n\nint testcase_ends() {\n int n;\n scanf(\"%d\", &n);\n if (n == 0) return 1;\n\n std::map<char, Matrix> vars;\n for (int i=0; i<n; ++i) {\n char buf[128];\n scanf(\" %[^\\n]\", buf);\n std::string s=buf;\n fprintf(stderr, \"%s\\n\", s.c_str());\n s.pop_back();\n char lhs=s[0];\n size_t j=2;\n Matrix rhs=parse(s, j, vars);\n //assert(s[j] == '.');\n rhs.output();\n vars[lhs] = rhs;\n }\n printf(\"-----\\n\");\n return 0;\n}\n\nint main() {\n while (!testcase_ends()) {}\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 2900, "score_of_the_acc": 0, "final_rank": 1 } ]
aoj_1318_cpp
Problem D: Long Distance Taxi A taxi driver, Nakamura, was so delighted because he got a passenger who wanted to go to a city thousands of kilometers away. However, he had a problem. As you may know, most taxis in Japan run on liquefied petroleum gas (LPG) because it is cheaper than gasoline. There are more than 50,000 gas stations in the country, but less than one percent of them sell LPG. Although the LPG tank of his car was full, the tank capacity is limited and his car runs 10 kilometer per liter, so he may not be able to get to the destination without filling the tank on the way. He knew all the locations of LPG stations. Your task is to write a program that finds the best way from the current location to the destination without running out of gas. Input The input consists of several datasets, and each dataset is in the following format. N M cap src dest c 1,1 c 1,2 d 1 c 2,1 c 2,2 d 2 . . . c N,1 c N,2 d N s 1 s 2 . . . s M The first line of a dataset contains three integers ( N, M, cap ), where N is the number of roads (1 ≤ N ≤ 3000), M is the number of LPG stations (1≤ M ≤ 300), and cap is the tank capacity (1 ≤ cap ≤ 200) in liter. The next line contains the name of the current city ( src ) and the name of the destination city ( dest ). The destination city is always different from the current city. The following N lines describe roads that connect cities. The road i (1 ≤ i ≤ N) connects two different cities c i,1 and c i,2 with an integer distance d i (0 < d i ≤ 2000) in kilometer, and he can go from either city to the other. You can assume that no two different roads connect the same pair of cities. The columns are separated by a single space. The next M lines ( s 1 , s 2 ,..., s M ) indicate the names of the cities with LPG station. You can assume that a city with LPG station has at least one road. The name of a city has no more than 15 characters. Only English alphabet ('A' to 'Z' and 'a' to 'z', case sensitive) is allowed for the name. A line with three zeros terminates the input. Output For each dataset, output a line containing the length (in kilometer) of the shortest possible journey from the current city to the destination city. If Nakamura cannot reach the destination, output "-1" (without quotation marks). You must not output any other characters. The actual tank capacity is usually a little bit larger than that on the specification sheet, so you can assume that he can reach a city even when the remaining amount of the gas becomes exactly zero. In addition, you can always fill the tank at the destination so you do not have to worry about the return trip. Sample Input 6 3 34 Tokyo Kyoto Tokyo Niigata 335 Tokyo Shizuoka 174 Shizuoka Nagoya 176 Nagoya Kyoto 195 Toyama Niigata 215 Toyama Kyoto 296 Nagoya Niigata Toyama 6 3 30 Tokyo Kyoto Tokyo Niigata 335 Tokyo Shizuoka 174 Shizuoka Nagoya 176 Nagoya Kyoto 195 Toyama Niigata 215 Toyama Kyoto 296 Nagoya Niigata Toyama 0 0 0 Output for the Sample Input 846 -1
[ { "submission_id": "aoj_1318_10849725", "code_snippet": "#include <iostream>\n#include <cstdio>\n#include <cstdlib>\n#include <vector>\n#include <queue>\n#include <map>\n#include <string>\n#include <cstring>\n#include <algorithm>\n\nusing namespace std;\n\nconst int maxn = 6000;\nconst int maxm = 6090;\nconst int inf = ~0u >> 2;\n\nstruct Edge{\n\tint v, w;\n\tEdge() {}\n\tEdge(int v, int w) {\n\t\tthis->v = v;\tthis->w = w;\n\t}\n};\n\nint n, m, gas, ncnt;\nvector<int> gast;\nvector<Edge> g1[maxn], g2[maxm];\nmap<string, int> mp;\n\nvoid addNode(string s)\n{\n\tif(!mp[s])\tmp[s] = ++ncnt;\n}\n\nvoid addEdge(int u, int v, int w)\n{\n\tg1[u].push_back(Edge(v, w));\n\tg1[v].push_back(Edge(u, w));\n}\n\nvoid spfa(int x, int dis[], vector<Edge> g[])\n{\n\tqueue<int> q;\n\tbool inqueue[maxn];\n\tmemset(inqueue, 0, sizeof(inqueue));\n\tinqueue[x] = 1;\tq.push(x); dis[x] = 0;\n\twhile(!q.empty()) {\n\t\tint tmp = q.front();\tq.pop();\tinqueue[tmp] = false;\n\t\tfor(int i = 0; i < g[tmp].size(); i++) {\n\t\t\tint v = g[tmp][i].v, w = g[tmp][i].w;\n\t\t\tif(dis[tmp]+w < dis[v]) {\n\t\t\t\tdis[v] = dis[tmp]+w;\n\t\t\t\tif(!inqueue[v]) {\n\t\t\t\t\tq.push(v);\n\t\t\t\t\tinqueue[v] = true;;\n\t\t\t\t}\n\t\t\t}\t\n\t\t}\n\t}\t\n}\n\nint main()\n{\n#ifdef LOCAL\n\tfreopen(\"D.in\", \"r\", stdin);\n#endif\n\twhile(scanf(\"%d%d%d\", &m, &n, &gas) != EOF) {\n\t\tif(m == 0 && n == 0)\tbreak;\n\t\tncnt = 0;\n\t\tchar stmp[maxm], estmp[maxm]; \n\t\tgast.clear();\n\t\tmp.clear();\n\t\tfor(int i = 0; i < maxn; i++)\tg1[i].clear();\n\t\tfor(int i = 0; i < maxm; i++)\tg2[i].clear();\n\t\tscanf(\"%s%s\", stmp, estmp);\n\t\taddNode(stmp);\taddNode(estmp);\n\t\tfor(int i = 0; i < m; i++) {\n\t\t\tint w;\n\t\t\tscanf(\"%s%s%d\", stmp, estmp, &w);\n\t\t\taddNode(stmp);\taddNode(estmp);\n\t\t\taddEdge(mp[stmp], mp[estmp], w);\n\t\t}\n\t\tgast.push_back(1);\tgast.push_back(2);\n\t\tfor(int i = 1; i <= n; i++) {\n\t\t//if one vertex is unique, ignore it\n\t\t\tscanf(\"%s\", stmp);\n\t\t\tif(mp[stmp])\tgast.push_back(mp[stmp]);\n\t\t}\n\t\t//calc single-point shortest path for every gas station and set-up point\n\t\t//rebuild a graph with vertices including stations and beginning and end point\n\t\tfor(int i = 0; i < gast.size(); i++) {\n\t\t\tint x = gast[i];\n\t\t\tint dis[maxn];\n\t\t\tfill(dis, dis+ncnt+1, inf);\n\t\t\tspfa(x, dis, g1);\n\t\t\tfor(int j = 0; j < gast.size(); j++)\n\t\t\t\tif(dis[gast[j]] <= gas*10) \tg2[i].push_back(Edge(j, dis[gast[j]]));\n\t\t}\n\t\tint dis[maxm];\n\t\tfill(dis, dis+gast.size()+1, inf);\n\t\tspfa(0, dis, g2);\t\n\t\tprintf(\"%d\\n\", dis[1] != inf ? dis[1] : -1);\n\t}\n\treturn 0;\n}", "accuracy": 1, "time_ms": 150, "memory_kb": 5488, "score_of_the_acc": -0.0165, "final_rank": 1 }, { "submission_id": "aoj_1318_10730467", "code_snippet": "#include<iostream>\n#include<cstdio>\n#include<cstring>\n#include<string>\n#include<algorithm>\n#include<map>\n#include<queue>\n#include<stack>\n#include<cmath>\n#include<vector>\n#define inf 0x3f3f3f3f\n#define Inf 0x3FFFFFFFFFFFFFFFLL\n#define eps 1e-9\n#define pi acos(-1.0)\nusing namespace std;\ntypedef long long ll;\nconst int maxn=3000+10;\nmap<string,int>mp;\nstruct Edge\n{\n int v,w,next;\n Edge(){}\n Edge(int v,int w,int next):v(v),w(w),next(next){}\n}edges[maxn<<2];\nint head[maxn<<1],d[maxn<<1][2010],nEdge;\nbool flag[maxn<<1],inq[maxn<<1][2010];\nvoid AddEdges(int u,int v,int w)\n{\n edges[++nEdge]=Edge(v,w,head[u]);\n head[u]=nEdge;\n edges[++nEdge]=Edge(u,w,head[v]);\n head[v]=nEdge;\n}\nint spfa(int s,int t,int cap)\n{\n memset(d,0x3f,sizeof(d));\n memset(inq,0,sizeof(inq));\n cap*=10;\n d[s][cap]=0;\n queue<pair<int,int> >q;\n q.push(make_pair(s,cap));\n pair<int,int>pii;\n int u,st;\n while(!q.empty())\n {\n pii=q.front();q.pop();\n u=pii.first;st=pii.second;\n inq[u][st]=false;\n for(int k=head[u];k!=-1;k=edges[k].next)\n {\n int v=edges[k].v;\n int cost=edges[k].w;\n if(st>=cost&&d[v][st-cost]>d[u][st]+cost)\n {\n d[v][st-cost]=d[u][st]+cost;\n if(!inq[v][st-cost]) {inq[v][st-cost]=true;q.push(make_pair(v,st-cost));}\n }\n if(flag[u])\n {\n if(cap>=cost&&d[v][cap-cost]>d[u][st]+cost)\n {\n d[v][cap-cost]=d[u][st]+cost;\n if(!inq[v][cap-cost]) {inq[v][cap-cost]=true;q.push(make_pair(v,cap-cost));}\n }\n }\n }\n }\n int res=inf;\n for(int i=0;i<=cap;++i)\n res=min(res,d[t][i]);\n return res==inf?-1:res;\n}\nint main()\n{\n //freopen(\"in.txt\",\"r\",stdin);\n //freopen(\"out.txt\",\"w\",stdout);\n char str[30],str2[30];\n int n,m,s,t,cap,N;\n while(~scanf(\"%d%d%d\",&n,&m,&cap))\n {\n if(n==0&&m==0&&cap==0) break;\n mp.clear();\n N=0;\n scanf(\"%s\",str);\n s=mp[(string)str]=++N;\n scanf(\"%s\",str);\n t=mp[(string)str]=++N;\n memset(head,0xff,sizeof(head));\n nEdge=-1;\n memset(flag,0,sizeof(flag));\n int u,v,w;\n for(int i=0;i<n;++i)\n {\n scanf(\"%s%s%d\",str,str2,&w);\n if(mp[(string)str]) u=mp[(string)str];\n else u=mp[(string)str]=++N;\n if(mp[(string)str2]) v=mp[(string)str2];\n else v=mp[(string)str2]=++N;\n AddEdges(u,v,w);\n }\n for(int i=0;i<m;++i)\n {\n scanf(\"%s\",str);\n flag[mp[(string)str]]=true;\n }\n int ans=spfa(s,t,cap);\n printf(\"%d\\n\",ans);\n }\n return 0;\n}", "accuracy": 1, "time_ms": 420, "memory_kb": 75740, "score_of_the_acc": -0.7621, "final_rank": 11 }, { "submission_id": "aoj_1318_10730466", "code_snippet": "#include <map>\n#include <set>\n#include <queue>\n#include <stack>\n#include <cmath>\n#include <string>\n#include <cctype>\n#include <vector>\n#include <cstdio>\n#include <climits>\n#include <cstdlib>\n#include <cstring>\n#include <iostream>\n#include <algorithm>\n\nusing namespace std;\n\ntypedef long long LL;\n\n\nconst int oo=INT_MAX>>1;\nstruct node{\n\tint u,x,w;\n\tnode(int _u,int _x,int _w){\n\t\tu=_u; x=_x; w=_w;\n\t}\n\tbool operator<(const node &t)const{\n\t\treturn w>t.w;\n\t}\n};\npriority_queue<node> Q;\nvector< pair<int,int> > a[13000];\nint dis[6007][2007],N;\nbool vis[6007][2007],b[6100];\nmap<string,int> mm;\n\nint get(string s){\n\tif (mm[s]) return mm[s];\n\tmm[s]=++N; a[N].clear();\n\treturn N;\n}\n\nint main(){\n\tint M,c,lim,u,v,d,i,j;\n\twhile (scanf(\"%d%d%d\",&M,&c,&lim), M || c || lim){\n\t\tlim*=10;\n\t\tchar s[100],t[100];\n\t\tmm.clear(); N=0;\n\t\tscanf(\"%s%s\",s,t); get(s); get(t);\n\t\twhile (M--){\n\t\t\tscanf(\"%s%s%d\",s,t,&d);\n\t\t\tu=get(s); v=get(t);\n\t\t\ta[u].push_back(make_pair(v,d)); a[v].push_back(make_pair(u,d));\n\t\t}\n\t\tfor (i=1;i<=N;i++){\n\t\t\tb[i]=0;\n\t\t\tfor (j=0;j<=lim;j++){\n\t\t\t\tdis[i][j]=oo; vis[i][j]=0;\n\t\t\t}\n\t\t}\n\t\twhile (c--){\n\t\t\tscanf(\"%s\",s); b[get(s)]=1;\n\t\t}\n\t\tdis[1][lim]=0;\n\t\tQ.push(node(1,lim,0));\n\t\tint x,y;\n\t\twhile (!Q.empty()){\n\t\t\tu=Q.top().u; x=Q.top().x; Q.pop();\n\t\t\tif (vis[u][x]) continue;\n\t\t\tvis[u][x]=1;\n\t\t\tfor (vector< pair<int,int> >::iterator ii=a[u].begin();ii!=a[u].end();ii++){\n\t\t\t\tv=ii->first; d=ii->second;\n\t\t\t\tif (d>x) continue;\n\t\t\t\ty=b[v]?lim:x-d;\n\t\t\t\tif (dis[u][x]+d<dis[v][y]){\n\t\t\t\t\tdis[v][y]=dis[u][x]+d;\n\t\t\t\t\tQ.push(node(v,y,dis[v][y]));\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\tint ans=oo;\n\t\tfor (i=0;i<=lim;i++){\n\t\t\tans=min(ans,dis[2][i]);\n\t\t}\n\t\tprintf(\"%d\\n\",ans==oo?-1:ans);\n\t}\n}", "accuracy": 1, "time_ms": 610, "memory_kb": 88728, "score_of_the_acc": -0.923, "final_rank": 14 }, { "submission_id": "aoj_1318_10730415", "code_snippet": "#include <iostream>\n#include <cstdio>\n#include <cstring>\n#include <cstdlib>\n#include <cmath>\n#include <algorithm>\n#include <queue>\n#include <map>\n#include <set>\n#include <vector>\n#include <string>\n#include <stack>\n#include <bitset>\n#define INF 0x3f3f3f3f\n#define eps 1e-8\n#define FI first\n#define SE second\nusing namespace std;\ntypedef long long LL;\nconst int maxn=6005;\nint n,m,C;\nmap <string,int> mp;\nmap <string,int>::iterator it;\nint head[maxn];\nstruct Edge\n{\n int next,to,len;\n}ed[maxn*2];\nint ecnt,label;\n\ninline void init()\n{\n ecnt=label=0;\n memset(head,-1,sizeof(head));\n mp.clear();\n}\n\ninline void addedge(int u,int v,int l)\n{\n ed[ecnt].next=head[u];\n ed[ecnt].to=v;\n ed[ecnt].len=l;\n head[u]=ecnt++;\n}\n\ninline int add(char *s)\n{\n it=mp.find(s);\n if(it!=mp.end()) return it->second;\n mp.insert(make_pair(s,label));\n return label++;\n}\n\nchar s1[25],s2[25];\nstruct Node\n{\n int u,c,dis;\n inline Node() {}\n inline Node(int _u,int _c,int _dis)\n {\n u=_u; c=_c; dis=_dis;\n }\n bool operator <(const Node &a)const\n {\n return dis>a.dis;\n }\n};\npriority_queue <Node> Q;\n\nint dp[6005][2005];\nbool done[6005][2005];\nbool LPG[6005];\ninline int Dijkstra(int S,int T)\n{\n while(!Q.empty()) Q.pop();\n C*=10;\n for(int i=0;i<label;++i) for(int j=0;j<=C;++j)\n {\n dp[i][j]=INF;\n done[i][j]=0;\n }\n dp[S][C]=0;\n Q.push(Node(S,C,0));\n Node t;\n while(!Q.empty())\n {\n t=Q.top(); Q.pop();\n if(t.u==T) return t.dis;\n if(done[t.u][t.c]) continue;\n for(int e=head[t.u];~e;e=ed[e].next)\n {\n int v=ed[e].to;\n if(t.c<ed[e].len) continue;\n int nc=t.c-ed[e].len;\n if(LPG[v]) nc=C;\n if(dp[v][nc]>t.dis+ed[e].len)\n {\n dp[v][nc]=t.dis+ed[e].len;\n Q.push(Node(v,nc,dp[v][nc]));\n }\n }\n }\n return -1;\n}\n\nint main()\n{\n while(scanf(\"%d%d%d\",&n,&m,&C)&&n)\n {\n init();\n scanf(\"%s%s\",s1,s2);\n int S=add(s1),T=add(s2);\n for(int i=0;i<n;++i)\n {\n int d;\n scanf(\"%s%s%d\",s1,s2,&d);\n if(d>C*10) continue;\n int u=add(s1),v=add(s2);\n addedge(u,v,d);\n addedge(v,u,d);\n }\n memset(LPG,0,sizeof(LPG));\n for(int i=0;i<m;++i)\n {\n scanf(\"%s\",s1);\n LPG[add(s1)]=1;\n }\n printf(\"%d\\n\",Dijkstra(S,T));\n }\n return 0;\n}", "accuracy": 1, "time_ms": 540, "memory_kb": 87140, "score_of_the_acc": -0.8956, "final_rank": 13 }, { "submission_id": "aoj_1318_10584241", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nconst long long INF = 4e18;\n\nint main() {\n while (true) {\n int N, M, cap;\n if (!(cin >> N >> M >> cap) || (N == 0 && M == 0 && cap == 0)) break;\n string srcName, dstName; cin >> srcName >> dstName;\n\n unordered_map<string, int> id;\n vector<vector<pair<int,int>>> g;\n\n auto getID = [&](const string& s) -> int {\n auto it = id.find(s);\n if (it != id.end()) return it->second;\n int idx = id.size();\n id[s] = idx;\n g.emplace_back();\n return idx;\n };\n\n for (int i = 0; i < N; ++i) {\n string a, b; int d; cin >> a >> b >> d;\n int u = getID(a), v = getID(b);\n g[u].push_back({v,d});\n g[v].push_back({u,d});\n }\n\n vector<int> station;\n station.reserve(M+2);\n station.push_back(getID(srcName));\n int dstID = getID(dstName);\n if (dstID != station[0]) station.push_back(dstID);\n\n for (int i = 0; i < M; ++i) {\n string s; cin >> s;\n int idS = getID(s);\n if (idS != station[0] && idS != dstID) station.push_back(idS);\n }\n\n int R = cap * 10;\n int K = station.size();\n vector<vector<long long>> meta(K, vector<long long>(K, INF));\n\n auto dijkstra_cut = [&](int start){\n vector<long long> dist(g.size(), INF);\n using P=pair<long long,int>; priority_queue<P, vector<P>, greater<P>> pq;\n dist[start]=0; pq.push({0,start});\n while(!pq.empty()){\n auto [d,u]=pq.top(); pq.pop();\n if(d!=dist[u] || d>R) continue;\n for(auto [v,w]:g[u]){\n if(d+w<dist[v] && d+w<=R){\n dist[v]=d+w; pq.push({dist[v],v});\n }\n }\n }\n return dist;\n };\n\n vector<vector<long long>> allDist;\n for(int i=0;i<K;++i) allDist.push_back(dijkstra_cut(station[i]));\n\n for(int i=0;i<K;++i)\n for(int j=0;j<K;++j)\n if(allDist[i][station[j]]<=R)\n meta[i][j]=allDist[i][station[j]];\n\n vector<long long> dmeta(K,INF);\n using P=pair<long long,int>; priority_queue<P,vector<P>,greater<P>> pq;\n dmeta[0]=0; pq.push({0,0});\n while(!pq.empty()){\n auto [d,u]=pq.top(); pq.pop();\n if(d!=dmeta[u]) continue;\n for(int v=0;v<K;++v)\n if(meta[u][v]<INF && d+meta[u][v]<dmeta[v]){\n dmeta[v]=d+meta[u][v];\n pq.push({dmeta[v],v});\n }\n }\n\n long long ans = dmeta[1];\n cout << (ans==INF?-1:ans) << '\\n';\n }\n return 0;\n}", "accuracy": 1, "time_ms": 180, "memory_kb": 11340, "score_of_the_acc": -0.0798, "final_rank": 5 }, { "submission_id": "aoj_1318_10584197", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\nconst ll INF = 4e18;\n\nint main() {\n ios::sync_with_stdio(false);\n cin.tie(nullptr);\n\n while (true) {\n int N, M, cap;\n if (!(cin >> N >> M >> cap) || (N == 0 && M == 0 && cap == 0)) break;\n string srcName, dstName; cin >> srcName >> dstName;\n\n unordered_map<string, int> id;\n vector<vector<pair<int,int>>> g; // id -> {next, dist}\n\n auto getID = [&](const string& s) -> int {\n auto it = id.find(s);\n if (it != id.end()) return it->second;\n int idx = id.size();\n id[s] = idx;\n g.emplace_back();\n return idx;\n };\n\n for (int i = 0; i < N; ++i) {\n string a, b; int d; cin >> a >> b >> d;\n int u = getID(a), v = getID(b);\n g[u].push_back({v,d});\n g[v].push_back({u,d});\n }\n\n vector<int> station;\n station.reserve(M+2);\n station.push_back(getID(srcName));\n int dstID = getID(dstName);\n if (dstID != station[0]) station.push_back(dstID);\n\n for (int i = 0; i < M; ++i) {\n string s; cin >> s;\n int idS = getID(s);\n if (idS != station[0] && idS != dstID) station.push_back(idS);\n }\n\n int R = cap * 10;\n int K = station.size();\n vector<vector<ll>> meta(K, vector<ll>(K, INF));\n\n auto dijkstra_cut = [&](int start){\n vector<ll> dist(g.size(), INF);\n using P=pair<ll,int>; priority_queue<P, vector<P>, greater<P>> pq;\n dist[start]=0; pq.push({0,start});\n while(!pq.empty()){\n auto [d,u]=pq.top(); pq.pop();\n if(d!=dist[u] || d>R) continue;\n for(auto [v,w]:g[u]){\n if(d+w<dist[v] && d+w<=R){\n dist[v]=d+w; pq.push({dist[v],v});\n }\n }\n }\n return dist;\n };\n\n vector<vector<ll>> allDist;\n for(int i=0;i<K;++i) allDist.push_back(dijkstra_cut(station[i]));\n\n for(int i=0;i<K;++i)\n for(int j=0;j<K;++j)\n if(allDist[i][station[j]]<=R)\n meta[i][j]=allDist[i][station[j]];\n\n vector<ll> dmeta(K,INF);\n using P=pair<ll,int>; priority_queue<P,vector<P>,greater<P>> pq;\n dmeta[0]=0; pq.push({0,0});\n while(!pq.empty()){\n auto [d,u]=pq.top(); pq.pop();\n if(d!=dmeta[u]) continue;\n for(int v=0;v<K;++v)\n if(meta[u][v]<INF && d+meta[u][v]<dmeta[v]){\n dmeta[v]=d+meta[u][v];\n pq.push({dmeta[v],v});\n }\n }\n\n ll ans = dmeta[1];\n cout << (ans==INF?-1:ans) << '\\n';\n }\n return 0;\n}", "accuracy": 1, "time_ms": 140, "memory_kb": 11464, "score_of_the_acc": -0.0744, "final_rank": 4 }, { "submission_id": "aoj_1318_10292082", "code_snippet": "#include \"bits/stdc++.h\"\nusing namespace std;\nusing ll = long long;\nusing ld = long double;\nusing ull = unsigned long long;\n#define rep(i, m, n) for (ll i = (ll)m; i < (ll)n; i++)\n#define drep(i, m, n) for (ll i = m - 1; i >= n; i--)\n#define Endl endl\n#define all(a) a.begin(), a.end()\n#define pr(i, j) make_pair(i, j)\n#define isin(x, l, r) (l <= x && x < r)\n#define chmin(a, b) a = min(a, b)\n#define chmax(a, b) a = max(a, b)\n#define srt(ar) sort(ar.begin(), ar.end())\n#define rev(ar) reverse(ar.begin(), ar.end())\n#define jge(f, s, t) cout << (f ? s : t) << endl\n#pragma GCC target(\"avx2\")\n#pragma GCC optimize(\"O3\")\n#pragma GCC optimize(\"unroll-loops\")\n\n#define printar(ar) \\\n do \\\n { \\\n for (auto dbg : ar) \\\n { \\\n cout << dbg << \" \"; \\\n } \\\n cout << endl; \\\n } while (0)\nconst ll inf = 1e18;\nconst ld pi = 3.14159265358979;\nconst ld eps = 1e-9;\ntemplate <class T, ll n, ll idx = 0>\nauto make_vec(const ll (&d)[n], const T &init) noexcept\n{\n if constexpr (idx < n)\n return std::vector(d[idx], make_vec<T, n, idx + 1>(d, init));\n else\n return init;\n}\n\ntemplate <class T, ll n>\nauto make_vec(const ll (&d)[n]) noexcept\n{\n return make_vec(d, T{});\n}\n//////////////// 以下を貼る ////////////////\ntemplate <class T>\nsize_t HashCombine(const size_t seed, const T &v)\n{\n return seed ^ (std::hash<T>()(v) + 0x9e3779b9 + (seed << 6) + (seed >> 2));\n}\n/* pair用 */\ntemplate <class T, class S>\nstruct std::hash<std::pair<T, S>>\n{\n size_t operator()(const std::pair<T, S> &keyval) const noexcept\n {\n return HashCombine(std::hash<T>()(keyval.first), keyval.second);\n }\n};\n////////////////////////////////////////////\nint main()\n{\n ios::sync_with_stdio(false);\n cin.tie(nullptr);\n while (true)\n {\n ll n, m, cap;\n cin >> n >> m >> cap;\n if (n == 0)\n {\n break;\n }\n string S, G;\n cin >> S >> G;\n set<string> st;\n st.insert(S);\n st.insert(G);\n vector<string> a(n), b(n);\n vector<ll> c(n);\n rep(i, 0, n)\n {\n cin >> a[i] >> b[i] >> c[i];\n st.insert(a[i]);\n st.insert(b[i]);\n }\n ll C = 0;\n map<string, ll> mp;\n for (auto i : st)\n {\n mp[i] = C;\n C++;\n }\n vector<vector<pair<ll, ll>>> g(C);\n rep(i, 0, n)\n {\n g[mp[a[i]]].push_back(pr(mp[b[i]], c[i]));\n g[mp[b[i]]].push_back(pr(mp[a[i]], c[i]));\n }\n vector<bool> sta(C);\n rep(i, 0, m)\n {\n string x;\n cin >> x;\n sta[mp[x]] = true;\n }\n vector<vector<ll>> d(C, vector<ll>(cap * 10 + 1, inf));\n d[mp[S]][cap * 10] = 0;\n using T = pair<ll, pair<ll, ll>>;\n priority_queue<T, vector<T>, greater<T>> que;\n que.push(pr(0, pr(mp[S], cap * 10)));\n while (!que.empty())\n {\n ll X = que.top().first;\n ll Y = que.top().second.first;\n ll Z = que.top().second.second;\n que.pop();\n if (d[Y][Z] != X)\n {\n continue;\n }\n rep(i, 0, g[Y].size())\n {\n ll nxt = g[Y][i].first;\n ll cost = g[Y][i].second;\n if (Z >= cost)\n {\n if (sta[nxt])\n {\n if (d[nxt][cap * 10] > X + cost)\n {\n d[nxt][cap * 10] = X + cost;\n que.push(pr(d[nxt][cap * 10], pr(nxt, cap * 10)));\n }\n }\n else\n {\n if (d[nxt][Z - cost] > X + cost)\n {\n d[nxt][Z - cost] = X + cost;\n que.push(pr(d[nxt][Z - cost], pr(nxt, Z - cost)));\n }\n }\n }\n }\n }\n ll ans = inf;\n rep(i, 0, cap * 10 + 1)\n {\n chmin(ans, d[mp[G]][i]);\n }\n if (ans == inf)\n {\n cout << -1 << endl;\n }\n else\n {\n cout << ans << endl;\n }\n }\n}", "accuracy": 1, "time_ms": 1160, "memory_kb": 101768, "score_of_the_acc": -1.1439, "final_rank": 16 }, { "submission_id": "aoj_1318_10060827", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\n\nusing ll = long long;\n#define rep(i,n) for(int i=0;i<n;i++)\n\nmap<string,int> MP;\nvector<vector<array<int,2>>> G;\nint rt(string S){\n if(!MP.count(S)){\n MP[S]=MP.size();\n G.push_back({});\n }\n return MP[S];\n}\n\nvoid solve(int N,int M,int C){\n MP.clear();\n G.clear();\n string s,t;\n cin>>s>>t;\n int S=rt(s),T=rt(t);\n rep(i,N){\n cin>>s>>t;\n int U=rt(s),V=rt(t),d;\n cin>>d;\n G[U].push_back({V,d});\n G[V].push_back({U,d});\n }\n set<int> LP;\n rep(i,M){\n string s;\n cin>>s;\n LP.insert(rt(s));\n }\n\n N=MP.size();\n vector<vector<ll>> D(N,vector<ll>(C*10+1,1e18));\n D[S][0]=0;\n priority_queue<array<ll,3>,vector<array<ll,3>>,greater<array<ll,3>>> Q;\n Q.push({0,S,0});\n vector<vector<bool>> seen(N,vector<bool>(C*10+1,0));\n ll an=1e18;\n while(!Q.empty()){\n auto p=Q.top();\n Q.pop();\n if(seen[p[1]][p[2]])continue;\n seen[p[1]][p[2]]=1;\n if(p[1]==T)an=min(an,p[0]);\n for(auto [v,d]:G[p[1]]){\n ll nc=p[2]+d,nd=p[0]+d;\n if(nc>C*10)continue;\n if(LP.count(v))nc=0;\n if(seen[v][nc])continue;\n if(D[v][nc]>nd){\n D[v][nc]=nd;\n Q.push({nd,v,nc});\n }\n }\n }\n cout<<(an<1e17?an:-1)<<endl;\n}\n\nint main() {\n cin.tie(nullptr);\n ios::sync_with_stdio(false);\n int N,M,C;\n while(cin>>N>>M>>C,N+M+C!=0)solve(N,M,C);\n}", "accuracy": 1, "time_ms": 1380, "memory_kb": 101432, "score_of_the_acc": -1.1769, "final_rank": 18 }, { "submission_id": "aoj_1318_10060819", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\n\nusing ll = long long;\n#define rep(i,n) for(int i=0;i<n;i++)\n#define rep2(i,s,t) for(int i=s;i<t;i++)\n\nusing vi = vector<int>;\n\nint rt(map<string,int> &MP,string S,vector<vector<array<int,2>>> &G){\n if(!MP.count(S)){\n MP[S]=MP.size();\n G.push_back({});\n }\n return MP[S];\n}\n\nvoid solve(int N,int M,int C){\n map<string,int> MP;\n vector<vector<array<int,2>>> G;\n string s,t;\n cin>>s>>t;\n int S=rt(MP,s,G),T=rt(MP,t,G);\n rep(i,N){\n string u,v;\n cin>>u>>v;\n int U=rt(MP,u,G),V=rt(MP,v,G),d;\n cin>>d;\n G[U].push_back({V,d});\n G[V].push_back({U,d});\n }\n set<int> LP;\n rep(i,M){\n string s;\n cin>>s;\n LP.insert(rt(MP,s,G));\n }\n\n N=MP.size();\n vector<vector<ll>> D(N,vector<ll>(C*10+1,1e18));\n D[S][0]=0;\n priority_queue<array<ll,3>,vector<array<ll,3>>,greater<array<ll,3>>> Q;\n Q.push({0,S,0});\n vector<vector<bool>> seen(N,vector<bool>(C*10+1,0));\n while(!Q.empty()){\n auto p=Q.top();\n Q.pop();\n if(seen[p[1]][p[2]])continue;\n seen[p[1]][p[2]]=1;\n for(auto [v,d]:G[p[1]]){\n ll nc=p[2]+d;\n if(nc>C*10)continue;\n ll nd=p[0]+d;\n if(LP.count(v))nc=0;\n if(seen[v][nc])continue;\n if(D[v][nc]>nd){\n D[v][nc]=nd;\n Q.push({nd,v,nc});\n }\n }\n }\n ll an=1e18;\n rep(i,C*10+1)an=min(an,D[T][i]);\n cout<<(an<1e17?an:-1)<<endl;\n}\n\nint main() {\n\n cin.tie(nullptr);\n ios::sync_with_stdio(false);\n\n int N,M,C;\n while(cin>>N>>M>>C,N+M+C!=0)solve(N,M,C);\n}", "accuracy": 1, "time_ms": 1400, "memory_kb": 102336, "score_of_the_acc": -1.1892, "final_rank": 19 }, { "submission_id": "aoj_1318_9684304", "code_snippet": "#include <bits/stdc++.h>\n\nusing namespace std;\n\n#define rep(i, a, b) for (int i = (int)(a); i < (int)(b); i++)\n#define rrep(i, a, b) for (int i = (int)(b)-1; i >= (int)(a); i--)\n#define ALL(v) (v).begin(), (v).end()\n#define UNIQUE(v) sort(ALL(v)), (v).erase(unique(ALL(v)), (v).end())\n#define SZ(v) (int)v.size()\n#define MIN(v) *min_element(ALL(v))\n#define MAX(v) *max_element(ALL(v))\n#define LB(v, x) int(lower_bound(ALL(v), (x)) - (v).begin())\n#define UB(v, x) int(upper_bound(ALL(v), (x)) - (v).begin())\n\nusing uint = unsigned int;\nusing ll = long long int;\nusing ull = unsigned long long;\nusing i128 = __int128_t;\nusing u128 = __uint128_t;\nconst int inf = 0x3fffffff;\nconst ll INF = 0x1fffffffffffffff;\n\ntemplate <typename T> inline bool chmax(T &a, T b) {\n if (a < b) {\n a = b;\n return 1;\n }\n return 0;\n}\ntemplate <typename T> inline bool chmin(T &a, T b) {\n if (a > b) {\n a = b;\n return 1;\n }\n return 0;\n}\ntemplate <typename T, typename U> T ceil(T x, U y) {\n assert(y != 0);\n if (y < 0)\n x = -x, y = -y;\n return (x > 0 ? (x + y - 1) / y : x / y);\n}\ntemplate <typename T, typename U> T floor(T x, U y) {\n assert(y != 0);\n if (y < 0)\n x = -x, y = -y;\n return (x > 0 ? x / y : (x - y + 1) / y);\n}\ntemplate <typename T> int popcnt(T x) {\n return __builtin_popcountll(x);\n}\ntemplate <typename T> int topbit(T x) {\n return (x == 0 ? -1 : 63 - __builtin_clzll(x));\n}\ntemplate <typename T> int lowbit(T x) {\n return (x == 0 ? -1 : __builtin_ctzll(x));\n}\n\ntemplate <class T, class U>\nostream &operator<<(ostream &os, const pair<T, U> &p) {\n os << \"P(\" << p.first << \", \" << p.second << \")\";\n return os;\n}\ntemplate <typename T> ostream &operator<<(ostream &os, const vector<T> &vec) {\n os << \"{\";\n for (int i = 0; i < vec.size(); i++) {\n os << vec[i] << (i + 1 == vec.size() ? \"\" : \", \");\n }\n os << \"}\";\n return os;\n}\ntemplate <typename T, typename U>\nostream &operator<<(ostream &os, const map<T, U> &map_var) {\n os << \"{\";\n for (auto itr = map_var.begin(); itr != map_var.end(); itr++) {\n os << \"(\" << itr->first << \", \" << itr->second << \")\";\n itr++;\n if (itr != map_var.end())\n os << \", \";\n itr--;\n }\n os << \"}\";\n return os;\n}\ntemplate <typename T> ostream &operator<<(ostream &os, const set<T> &set_var) {\n os << \"{\";\n for (auto itr = set_var.begin(); itr != set_var.end(); itr++) {\n os << *itr;\n ++itr;\n if (itr != set_var.end())\n os << \", \";\n itr--;\n }\n os << \"}\";\n return os;\n}\n#ifdef LOCAL\n#define show(...) _show(0, #__VA_ARGS__, __VA_ARGS__)\n#else\n#define show(...) true\n#endif\ntemplate <typename T> void _show(int i, T name) {\n cerr << '\\n';\n}\ntemplate <typename T1, typename T2, typename... T3>\nvoid _show(int i, const T1 &a, const T2 &b, const T3 &...c) {\n for (; a[i] != ',' && a[i] != '\\0'; i++)\n cerr << a[i];\n cerr << \":\" << b << \" \";\n _show(i + 1, a, c...);\n}\n\n/**\n * @brief template\n */\n\nstruct UnionFind{\n vector<int> par; int n;\n UnionFind(){}\n UnionFind(int _n):par(_n,-1),n(_n){}\n int root(int x){return par[x]<0?x:par[x]=root(par[x]);}\n bool same(int x,int y){return root(x)==root(y);}\n int size(int x){return -par[root(x)];}\n bool unite(int x,int y){\n x=root(x),y=root(y); if(x==y)return false;\n if(size(x)>size(y))swap(x,y);\n par[y]+=par[x]; par[x]=y; n--; return true;\n }\n};\n\n/**\n * @brief Union Find\n */\n\nvector<int> MakeDist(vector<vector<pair<int,int>>>& G, int S) {\n int N = G.size();\n vector<int> DP(N,inf);\n priority_queue<pair<int,int>,vector<pair<int,int>>,greater<pair<int,int>>> PQ;\n DP[S] = 0;\n PQ.push({0,S});\n while(!PQ.empty()) {\n auto [D,P] = PQ.top();\n PQ.pop();\n if (DP[P] < D) continue;\n for (auto E : G[P]) {\n int NP = E.first, ND = E.second + D;\n if (chmin(DP[NP],ND)) {\n PQ.push({ND,NP});\n }\n }\n }\n return DP;\n}\n\nint main() {\nwhile(1) {\n int M, K, L;\n cin >> M >> K >> L;\n if (M == 0) return 0;\n L *= 10;\n map<string,int> mp;\n int Cur = 0;\n string S;\n cin >> S;\n mp[S] = 0;\n cin >> S;\n mp[S] = 1;\n Cur = 2;\n vector<int> A(M), B(M), D(M);\n rep(i,0,M) {\n string T;\n cin >> S >> T >> D[i];\n if (mp.count(S)) A[i] = mp[S];\n else mp[S] = A[i] = Cur, Cur++;\n if (mp.count(T)) B[i] = mp[T];\n else mp[T] = B[i] = Cur, Cur++;\n }\n vector<int> C(K+2);\n rep(i,0,K) {\n cin >> S;\n if (mp.count(S)) C[i] = mp[S];\n else mp[S] = C[i] = Cur, Cur++;\n }\n C[K] = 0,C[K+1] = 1;\n UNIQUE(C);\n int N = Cur;\n K = C.size();\n vector<vector<pair<int,int>>> G(N);\n rep(i,0,M) G[A[i]].push_back({B[i],D[i]}), G[B[i]].push_back({A[i],D[i]});\n vector<vector<int>> Dist(K,vector<int>(K,inf));\n rep(i,0,K) {\n vector<int> DP = MakeDist(G,C[i]);\n rep(j,0,K) {\n Dist[i][j] = DP[C[j]];\n }\n }\n rep(i,0,K) {\n rep(j,0,K) {\n if (Dist[i][j] > L) Dist[i][j] = inf;\n }\n }\n rep(k,0,K) {\n rep(i,0,K) {\n rep(j,0,K) {\n chmin(Dist[i][j], Dist[i][k] + Dist[k][j]);\n }\n }\n }\n cout << (Dist[0][1] == inf ? -1 : Dist[0][1]) << endl;\n}\n}", "accuracy": 1, "time_ms": 640, "memory_kb": 4004, "score_of_the_acc": -0.0825, "final_rank": 6 }, { "submission_id": "aoj_1318_9563459", "code_snippet": "#include <algorithm>\n#include <cmath>\n#include <cstdlib>\n#include <iomanip>\n#include <iostream>\n#include <list>\n#include <map>\n#include <numeric>\n#include <queue>\n#include <set>\n#include <stack>\n#include <string>\n#include <unordered_set>\n#include <vector>\n\nusing namespace std;\nusing ll = long long;\n\nclass Node {\n public:\n\tstring city;\n\tint gas; // [deciliter]\n\tint dist;\n\n\tNode(const string& city, const int& gas, const int& dist)\n\t\t: city(city), gas(gas), dist(dist) {};\n\n\tbool operator<(const Node& b) const {\n\t\treturn (this->dist > b.dist);\n\t}\n};\n\n// cap [deciliter]\nll ShortestWay(const int& cap, const string& src, const string& dest, map<string, map<string, int>>& adjacents, map<string, bool>& has_station) {\n\tll ans = -1;\n\n\tmap<string, map<int, ll>> dist;\n\tdist[src][cap] = 0;\n\tmap<string, map<int, bool>> done;\n\tpriority_queue<Node> que;\n\tque.emplace(src, cap, 0);\n\n\twhile (!que.empty()) {\n\t\tNode node = que.top();\n\t\tque.pop();\n\t\tif (node.city == dest) {\n\t\t\tans = dist[node.city][node.gas];\n\t\t\tbreak;\n\t\t}\n\t\tdone[node.city][node.gas] = true;\n\n\t\tfor (const auto& adjacent : adjacents[node.city]) {\n\t\t\tint adjacent_gas = node.gas - adjacent.second;\n\t\t\tif (adjacent_gas < 0) continue;\n\t\t\tif (has_station[adjacent.first]) adjacent_gas = cap;\n\t\t\tif (done[adjacent.first][adjacent_gas]) continue;\n\t\t\tll adjacent_dist = node.dist + adjacent.second;\n\n\t\t\tbool dismiss = false;\n\t\t\tfor (const auto& gas_and_dist : dist[adjacent.first]) {\n\t\t\t\tif (adjacent_gas <= gas_and_dist.first and adjacent_dist >= gas_and_dist.second) dismiss = true;\n\t\t\t}\n\t\t\tif (dismiss) continue;\n\n\t\t\tdist[adjacent.first][adjacent_gas] = adjacent_dist;\n\t\t\tque.emplace(adjacent.first, adjacent_gas, adjacent_dist);\n\t\t}\n\t}\n\n\treturn ans;\n}\n\nbool solve() {\n\tint n, m, cap;\n\tcin >> n >> m >> cap;\n\tif (n == 0) return false;\n\tstring src, dest;\n\tcin >> src >> dest;\n\tmap<string, map<string, int>> adjacents;\n\tfor (int i = 0; i < n; i++) {\n\t\tstring c1, c2;\n\t\tint d;\n\t\tcin >> c1 >> c2 >> d;\n\t\tadjacents[c1][c2] = adjacents[c2][c1] = d;\n\t}\n\tmap<string, bool> has_station;\n\tfor (int i = 0; i < m; i++) {\n\t\tstring s;\n\t\tcin >> s;\n\t\thas_station[s] = true;\n\t}\n\n\tcout << ShortestWay(cap * 10, src, dest, adjacents, has_station) << endl;\n\n\treturn true;\n}\n\nint main(void) {\n\twhile (solve());\n\treturn 0;\n}", "accuracy": 1, "time_ms": 6200, "memory_kb": 104212, "score_of_the_acc": -2, "final_rank": 20 }, { "submission_id": "aoj_1318_8994680", "code_snippet": "#line 2 \"cp-library/src/cp-template.hpp\"\n#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\nusing ld = long double;\nusing uint = unsigned int;\nusing ull = unsigned long long;\nusing i32 = int;\nusing u32 = unsigned int;\nusing i64 = long long;\nusing u64 = unsigned long long;\nusing i128 = __int128_t;\ntemplate < class T > bool chmin(T& a, T b) { if(a > b) { a = b; return true; } return false; }\ntemplate < class T > bool chmax(T& a, T b) { if(a < b) { a = b; return true; } return false; }\ntemplate < class T, class U > T ceil (T x, U y) { return (x > 0 ? (x + y - 1) / y : x / y); }\ntemplate < class T, class U > T floor(T x, U y) { return (x > 0 ? x / y : (x - y + 1) / y); }\nint popcnt(i32 x) { return __builtin_popcount(x); }\nint popcnt(u32 x) { return __builtin_popcount(x); }\nint popcnt(i64 x) { return __builtin_popcountll(x); }\nint popcnt(u64 x) { return __builtin_popcountll(x); }\n\n#line 2 \"cp-library/src/utility/rep_itr.hpp\"\ntemplate < class T > struct itr_rep {\n T i, d;\n constexpr itr_rep(const T i) noexcept : i(i), d(1) {}\n constexpr itr_rep(const T i, const T d) noexcept : i(i), d(d) {}\n void operator++() noexcept { i += d; }\n constexpr int operator*() const noexcept { return i; }\n constexpr bool operator!=(const itr_rep x) const noexcept { return d > 0 ? i < x.i : i > x.i; }\n};\n\ntemplate < class T > struct rep {\n const itr_rep< T > s, t;\n constexpr rep(const T t) noexcept : s(0), t(t) {}\n constexpr rep(const T s, const T t) noexcept : s(s), t(t) {}\n constexpr rep(const T s, const T t, const T d) noexcept : s(s, d), t(t, d) {}\n constexpr auto begin() const noexcept { return s; }\n constexpr auto end () const noexcept { return t; }\n};\n\ntemplate < class T > struct revrep {\n const itr_rep < T > s, t;\n constexpr revrep(const T t) noexcept : s(t - 1, -1), t(-1, -1) {}\n constexpr revrep(const T s, const T t) noexcept : s(t - 1, -1), t(s - 1, -1) {}\n constexpr revrep(const T s, const T t, const T d) noexcept : s(t - 1, -d), t(s - 1, -d) {}\n constexpr auto begin() const noexcept { return s; }\n constexpr auto end () const noexcept { return t; }\n};\n#line 3 \"cp-library/src/utility/io.hpp\"\n\n/* 128bit integer */\nistream& operator>>(istream& is, i128& x) {\n std::string s; is >> s;\n int pm = (s[0] == '-');\n x = 0;\n for(int i : rep(pm, int(s.size()))) x = x * 10 + (s[i] - '0');\n if(pm) x *= -1;\n return is;\n}\nostream& operator<<(ostream& os, const i128& x) {\n if(x == 0) return os << '0';\n i128 y = x;\n if(y < 0) { os << '-'; y *= -1; }\n std::vector<int> ny;\n while(y > 0) { ny.push_back(y % 10); y /= 10; }\n for(int i : revrep(ny.size())) os << ny[i];\n return os;\n}\n\ntemplate < class S, class T > istream& operator>>(istream& is, std::pair< S, T >& x) { is >> x.first >> x.second; return is; }\ntemplate < class S, class T > ostream& operator<<(ostream& os, const std::pair< S, T >& x) { os << x.first << \" \" << x.second; return os; }\n\nnamespace scanner {\n struct sca {\n template < class T > operator T() {\n T s; std::cin >> s; return s;\n }\n };\n struct vec {\n int n;\n vec(int n) : n(n) {}\n template < class T > operator std::vector< T >() {\n std::vector< T > v(n);\n for(T& x : v) std::cin >> x;\n return v;\n }\n };\n struct mat {\n int h, w;\n mat(int h, int w) : h(h), w(w) {}\n template < class T > operator std::vector< std::vector< T > >() {\n std::vector m(h, std::vector< T >(w));\n for(std::vector< T >& v : m) for(T& x : v) std::cin >> x;\n return m;\n }\n };\n struct speedup {\n speedup() {\n std::cin.tie(0);\n std::ios::sync_with_stdio(0);\n }\n } speedup_instance;\n}\nscanner::sca in() { return scanner::sca(); }\nscanner::vec in(int n) { return scanner::vec(n); }\nscanner::mat in(int h, int w) { return scanner::mat(h, w); }\n\nnamespace printer {\n void precision(int d) { std::cout << std::fixed << std::setprecision(d); }\n void flush() { std::cout.flush(); }\n}\n\ntemplate < class T >\nostream& operator<<(ostream& os, const std::vector< T > a) {\n int n = a.size();\n for(int i : rep(n)) { os << a[i]; if(i != n - 1) os << ' '; }\n return os;\n}\n\nint print() { std::cout << '\\n'; return 0; }\ntemplate < class head, class... tail > int print(head&& h, tail&&... t) {\n std::cout << h; if(sizeof...(tail)) std::cout << ' ';\n return print(std::forward<tail>(t)...);\n}\ntemplate < class T > int print_n(const std::vector< T > a) {\n int n = a.size();\n for(int i : rep(n)) std::cout << a[i] << \"\\n\";\n return 0;\n}\n\n\n#line 2 \"cp-library/src/utility/key_val.hpp\"\n\ntemplate < class K, class V >\nstruct key_val {\n K key; V val;\n key_val() {}\n key_val(K key, V val) : key(key), val(val) {}\n template < std::size_t Index >\n std::tuple_element_t< Index, key_val >& get() {\n if constexpr (Index == 0) return key;\n if constexpr (Index == 1) return val;\n }\n};\n\nnamespace std {\n\ntemplate < class K, class V > struct tuple_size < key_val< K, V > > : integral_constant< size_t, 2 > {};\ntemplate < class K, class V > struct tuple_element < 0, key_val< K, V > > { using type = K; };\ntemplate < class K, class V > struct tuple_element < 1, key_val< K, V > > { using type = V; };\n\n}\n#line 2 \"cp-library/src/utility/vec_op.hpp\"\ntemplate < class T > key_val< int, T > max_of(const vector< T >& a) {\n int i = std::max_element(a.begin(), a.end()) - a.begin();\n return {i, a[i]};\n}\ntemplate < class T > key_val< int, T > min_of(const vector< T >& a) {\n int i = std::min_element(a.begin(), a.end()) - a.begin();\n return {i, a[i]};\n}\ntemplate < class S, class T > S sum_of(const vector< T >& a) {\n S sum = 0;\n for(const T x : a) sum += x;\n return sum;\n}\ntemplate < class S, class T > vector< S > freq_of(const vector< T >& a, T L, T R) {\n vector< S > res(R - L, S(0));\n for(const T x : a) res[x - L] += 1;\n return res;\n}\ntemplate < class S, class T > struct prefix_sum {\n vector< S > s;\n prefix_sum(const vector< T >& a) : s(a) {\n s.insert(s.begin(), S(0));\n for(int i : rep(a.size())) s[i + 1] += s[i];\n }\n // [L, R)\n S sum(int L, int R) { return s[R] - s[L]; }\n};\n#line 3 \"cp-library/src/utility/heap.hpp\"\n\ntemplate < class T > using heap_min = std::priority_queue< T, std::vector< T >, std::greater< T > >;\ntemplate < class T > using heap_max = std::priority_queue< T, std::vector< T >, std::less< T > >;\n\n#line 27 \"cp-library/src/cp-template.hpp\"\n\n#line 1 \"cp-library/src/algorithm/bin_search.hpp\"\ntemplate < class T, class F >\nT bin_search(T ok, T ng, F f) {\n while(abs(ng - ok) > 1) {\n T mid = (ok + ng) / 2;\n (f(mid) ? ok : ng) = mid;\n }\n return ok;\n}\n\ntemplate < class T, class F >\nT bin_search_real(T ok, T ng, F f, int step = 80) {\n while(step--) {\n T mid = (ok + ng) / 2;\n (f(mid) ? ok : ng) = mid;\n }\n return ok;\n}\n#line 2 \"cp-library/src/algorithm/argsort.hpp\"\n\ntemplate < class T > std::vector< int > argsort(const std::vector< T > &a) {\n std::vector< int > ids((int)a.size());\n std::iota(ids.begin(), ids.end(), 0);\n std::sort(ids.begin(), ids.end(), [&](int i, int j) {\n return a[i] < a[j] || (a[i] == a[j] && i < j);\n });\n return ids;\n}\n#line 1 \"macro.hpp\"\nnamespace macro {\n\nusing size_type = int;\ntemplate < class container > void sort(container& a) { std::sort(std:: begin(a), std:: end(a)); }\ntemplate < class container > void rsort(container& a) { std::sort(std::rbegin(a), std::rend(a)); }\ntemplate < class container > void reverse(container& a) { std::reverse(std::begin(a), std::end(a)); }\ntemplate < class container > void unique(container& a) {\n std::sort(std::begin(a), std::end(a));\n a.erase(std::unique(std::begin(a), std::end(a)), std::end(a));\n}\ntemplate < class container > container sorted(const container& a) { container b = a; sort(b); return std::move(b); }\ntemplate < class container > container rsorted(const container& a) { container b = a; rsort(b); return std::move(b); }\ntemplate < class container, class compare > void sort(container& a, const compare& cmp) { std::sort(std::begin(a), std::end(a), cmp); }\ntemplate < class container, class compare > container sorted(const container& a, const compare& cmp) { container b = a; sort(b, cmp); return std::move(b); }\ntemplate < class container, class value > size_type lower_bound(const container& a, const value& x) { return std::lower_bound(std::begin(a), std::end(a), x) - std::begin(a); }\ntemplate < class container, class value > size_type upper_bound(const container& a, const value& x) { return std::upper_bound(std::begin(a), std::end(a), x) - std::begin(a); }\n\nconst std::vector<std::pair<size_type, size_type>> dir4 = { {+1, 0}, {-1, 0}, { 0, +1}, { 0, -1} };\nconst std::vector<std::pair<size_type, size_type>> dir8 = { {-1, -1}, {-1, 0}, {-1, +1}, { 0, -1}, { 0, +1}, {+1, -1}, {+1, 0}, {+1, +1} };\n\n#ifdef _DEBUG\n#define debug(x) std::cout << \"[\" << __LINE__ << \"] \" << #x << \": \" << x << std::endl\n#else\n#define debug(x)\n#endif\n\ntemplate < class container > void concat(container& a, const container& b) {\n a.insert(std::end(a), std::begin(b), std::end(b));\n}\nstd::vector<size_type> iota(const size_type n) {\n std::vector<size_type> I(n);\n std::iota(std::begin(I), std::end(I), 0);\n return I;\n}\ntemplate < class container > std::vector<size_type> sort_idx(const container& a) {\n const size_type n = a.size();\n std::vector<size_type> I = iota(n);\n std::sort(std::begin(I), std::end(I), [&](size_type i, size_type j) { return a[i] < a[j] or (a[i] == a[j] and i < j); });\n return I;\n}\ntemplate < class container, class compare > std::vector<size_type> sort_idx(const container& a, const compare& cmp) {\n const size_type n = a.size();\n std::vector<size_type> I = iota(n);\n std::sort(std::begin(I), std::end(I), [&](size_type i, size_type j) { return cmp(a[i], a[j]) or (a[i] == a[j] and i < j); });\n return std::move(I);\n}\n\nstruct grid {\n using size_type = int;\n size_type H, W;\n grid(const size_type H, const size_type W) : H(H), W(W) {}\n bool contains(const size_type i, const size_type j) {\n return 0 <= i and i < H and 0 <= j and j < W;\n }\n};\n\n} // namespace macro\n\nusing namespace macro;\n#line 3 \"A.cpp\"\n\n// g <- pair < v , cost > \ntemplate < class T >\nvector< T > dijkstra(vector<vector<pair<int, T>>> &graph, int s) {\n T INF = numeric_limits< T >::max();\n vector<T> dist(graph.size(), INF);\n priority_queue<pair<T,int>, vector<pair<T,int>>, greater<pair<T,int>>> q;\n q.push({dist[s] = T(0), s});\n while(!q.empty()){\n auto [uc, ui] = q.top(); q.pop();\n if(uc != dist[ui]) continue;\n for(auto [vi, vc] : graph[ui]) if(dist[vi] > uc + vc) \n q.push({dist[vi] = uc + vc, vi});\n }\n return dist;\n}\n\nint solve(int N, int M, int cap) {\n cap *= 10;\n\n map<string, int> mp;\n int sz = 0;\n auto ID = [&](string s) {\n if(!mp.count(s)) mp[s] = sz++;\n return mp[s];\n };\n int src = [&] { string s = in(); return ID(s); }();\n int dst = [&] { string s = in(); return ID(s); }();\n vector<vector<pair<int,int>>> G(N + N);\n for(int i : rep(N)) {\n string s = in(), t = in(); int d = in();\n int u = ID(s), v = ID(t);\n G[u].push_back({v, d});\n G[v].push_back({u, d});\n }\n vector<bool> station(N + N, false);\n for(int i : rep(M)) {\n string s = in();\n station[ID(s)] = true;\n }\n\n const int INF = 1e9;\n vector dist(N + N, vector(cap + 1, INF));\n heap_min<tuple<int,int,int>> q;\n q.push({dist[src][cap] = 0, src, cap});\n while(not q.empty()) {\n auto [ud, ui, uc] = q.top(); q.pop();\n if(dist[ui][uc] < ud) continue;\n for(auto [vi, d] : G[ui]) {\n if(0 <= uc - d) {\n int vc = station[vi] ? cap : uc - d;\n if(chmin(dist[vi][vc], ud + d)) q.push({ud + d, vi, vc});\n }\n }\n }\n\n int ans = INF;\n for(int c = 0; c <= cap; c++) chmin(ans, dist[dst][c]);\n return ans < INF ? ans : -1;\n}\n\nint main() {\n while(true) {\n int N = in(), M = in(), cap = in();\n if(make_tuple(N, M, cap) == make_tuple(0, 0, 0)) return 0;\n print(solve(N, M, cap));\n }\n}", "accuracy": 1, "time_ms": 1100, "memory_kb": 77356, "score_of_the_acc": -0.8904, "final_rank": 12 }, { "submission_id": "aoj_1318_8475146", "code_snippet": "// #define _GLIBCXX_DEBUG\n#pragma GCC optimize(\"O2,no-stack-protector,unroll-loops,fast-math\")\n#include <bits/stdc++.h>\nusing namespace std;\n#define rep(i, n) for (int i = 0; i < int(n); i++)\n#define per(i, n) for (int i = (n)-1; 0 <= i; i--)\n#define rep2(i, l, r) for (int i = (l); i < int(r); i++)\n#define per2(i, l, r) for (int i = (r)-1; int(l) <= i; i--)\n#define each(e, v) for (auto& e : v)\n#define MM << \" \" <<\n#define pb push_back\n#define eb emplace_back\n#define all(x) begin(x), end(x)\n#define rall(x) rbegin(x), rend(x)\n#define sz(x) (int)x.size()\ntemplate <typename T> void print(const vector<T>& v, T x = 0) {\n int n = v.size();\n for (int i = 0; i < n; i++) cout << v[i] + x << (i == n - 1 ? '\\n' : ' ');\n if (v.empty()) cout << '\\n';\n}\nusing ll = long long;\nusing pii = pair<int, int>;\nusing pll = pair<ll, ll>;\ntemplate <typename T> bool chmax(T& x, const T& y) {\n return (x < y) ? (x = y, true) : false;\n}\ntemplate <typename T> bool chmin(T& x, const T& y) {\n return (x > y) ? (x = y, true) : false;\n}\ntemplate <class T>\nusing minheap = std::priority_queue<T, std::vector<T>, std::greater<T>>;\ntemplate <class T> using maxheap = std::priority_queue<T>;\ntemplate <typename T> int lb(const vector<T>& v, T x) {\n return lower_bound(begin(v), end(v), x) - begin(v);\n}\ntemplate <typename T> int ub(const vector<T>& v, T x) {\n return upper_bound(begin(v), end(v), x) - begin(v);\n}\ntemplate <typename T> void rearrange(vector<T>& v) {\n sort(begin(v), end(v));\n v.erase(unique(begin(v), end(v)), end(v));\n}\n\n// __int128_t gcd(__int128_t a, __int128_t b) {\n// if (a == 0)\n// return b;\n// if (b == 0)\n// return a;\n// __int128_t cnt = a % b;\n// while (cnt != 0) {\n// a = b;\n// b = cnt;\n// cnt = a % b;\n// }\n// return b;\n// }\n\nstruct Union_Find_Tree {\n vector<int> data;\n const int n;\n int cnt;\n \n Union_Find_Tree(int n) : data(n, -1), n(n), cnt(n) {}\n \n int root(int x) {\n if (data[x] < 0) return x;\n return data[x] = root(data[x]);\n }\n \n int operator[](int i) { return root(i); }\n \n bool unite(int x, int y) {\n x = root(x), y = root(y);\n if (x == y) return false;\n if (data[x] > data[y]) swap(x, y);\n data[x] += data[y], data[y] = x;\n cnt--;\n return true;\n }\n \n int size(int x) { return -data[root(x)]; }\n \n int count() { return cnt; };\n \n bool same(int x, int y) { return root(x) == root(y); }\n \n void clear() {\n cnt = n;\n fill(begin(data), end(data), -1);\n }\n};\n\ntemplate <int mod> struct Mod_Int {\n int x;\n\n Mod_Int() : x(0) {}\n\n Mod_Int(long long y) : x(y >= 0 ? y % mod : (mod - (-y) % mod) % mod) {}\n\n static int get_mod() { return mod; }\n\n Mod_Int& operator+=(const Mod_Int& p) {\n if ((x += p.x) >= mod) x -= mod;\n return *this;\n }\n\n Mod_Int& operator-=(const Mod_Int& p) {\n if ((x += mod - p.x) >= mod) x -= mod;\n return *this;\n }\n\n Mod_Int& operator*=(const Mod_Int& p) {\n x = (int)(1LL * x * p.x % mod);\n return *this;\n }\n\n Mod_Int& operator/=(const Mod_Int& p) {\n *this *= p.inverse();\n return *this;\n }\n\n Mod_Int& operator++() { return *this += Mod_Int(1); }\n\n Mod_Int operator++(int) {\n Mod_Int tmp = *this;\n ++*this;\n return tmp;\n }\n\n Mod_Int& operator--() { return *this -= Mod_Int(1); }\n\n Mod_Int operator--(int) {\n Mod_Int tmp = *this;\n --*this;\n return tmp;\n }\n\n Mod_Int operator-() const { return Mod_Int(-x); }\n\n Mod_Int operator+(const Mod_Int& p) const { return Mod_Int(*this) += p; }\n\n Mod_Int operator-(const Mod_Int& p) const { return Mod_Int(*this) -= p; }\n\n Mod_Int operator*(const Mod_Int& p) const { return Mod_Int(*this) *= p; }\n\n Mod_Int operator/(const Mod_Int& p) const { return Mod_Int(*this) /= p; }\n\n bool operator==(const Mod_Int& p) const { return x == p.x; }\n\n bool operator!=(const Mod_Int& p) const { return x != p.x; }\n\n Mod_Int inverse() const {\n assert(*this != Mod_Int(0));\n return pow(mod - 2);\n }\n\n Mod_Int pow(long long k) const {\n Mod_Int now = *this, ret = 1;\n for (; k > 0; k >>= 1, now *= now) {\n if (k & 1) ret *= now;\n }\n return ret;\n }\n\n friend ostream& operator<<(ostream& os, const Mod_Int& p) {\n return os << p.x;\n }\n\n friend istream& operator>>(istream& is, Mod_Int& p) {\n long long a;\n is >> a;\n p = Mod_Int<mod>(a);\n return is;\n }\n};\n\nll mpow2(ll x, ll n, ll mod) {\n ll ans = 1;\n x %= mod;\n while (n != 0) {\n if (n & 1) ans = ans * x % mod;\n x = x * x % mod;\n n = n >> 1;\n }\n ans %= mod;\n return ans;\n}\n\ntemplate <typename T> T modinv(T a, const T& m) {\n T b = m, u = 1, v = 0;\n while (b > 0) {\n T t = a / b;\n swap(a -= t * b, b);\n swap(u -= t * v, v);\n }\n return u >= 0 ? u % m : (m - (-u) % m) % m;\n}\n\nll divide_int(ll a, ll b) {\n if (b < 0) a = -a, b = -b;\n return (a >= 0 ? a / b : (a - b + 1) / b);\n}\n\n// const int MOD = 1000000007;\nconst int MOD = 998244353;\nusing mint = Mod_Int<MOD>;\n\n// ----- library -------\n// ----- library -------\n\nint main() {\n ios::sync_with_stdio(false);\n std::cin.tie(nullptr);\n cout << fixed << setprecision(15);\n\n while (1) {\n int m, k, c;\n cin >> m >> k >> c;\n c *= 10;\n if (!m)\n break;\n string sa, sb;\n cin >> sa >> sb;\n vector<string> su(m), sv(m), ss(k);\n vector<int> d(m);\n rep(i, m) cin >> su[i] >> sv[i] >> d[i];\n rep(i, k) cin >> ss[i];\n vector<string> ls{sa, sb};\n rep(i, m) ls.eb(su[i]), ls.eb(sv[i]);\n rep(i, k) ls.eb(ss[i]);\n rearrange(ls);\n int a, b;\n vector<int> u(m), v(m), s(k);\n a = lb(ls, sa), b = lb(ls, sb);\n rep(i, m) u[i] = lb(ls, su[i]), v[i] = lb(ls, sv[i]);\n rep(i, k) s[i] = lb(ls, ss[i]);\n int n = sz(ls);\n vector<vector<pii>> g(n);\n vector<int> f(n, 0);\n rep(i, m) g[u[i]].eb(v[i], d[i]), g[v[i]].eb(u[i], d[i]);\n rep(i, k) f[s[i]] = 1;\n minheap<pair<int, pii>> que;\n vector<vector<int>> mi(n, vector<int>(c + 1, 1e9));\n mi[a][c] = 0, que.emplace(0, pair(a, c));\n while (sz(que)) {\n auto [nd, nij] = que.top();\n que.pop();\n auto [ni, nj] = nij;\n if (mi[ni][nj] != nd)\n continue;\n for (auto [nv, cost] : g[ni]) {\n if (nj < cost)\n continue;\n int rest = (f[nv] ? c : nj - cost);\n if (chmin(mi[nv][rest], nd + cost))\n que.emplace(nd + cost, pair(nv, rest));\n }\n }\n int ans = 1e9;\n rep(j, c + 1) chmin(ans, mi[b][j]);\n cout << (ans < 1e8 ? ans : -1) << '\\n';\n }\n}", "accuracy": 1, "time_ms": 1010, "memory_kb": 52020, "score_of_the_acc": -0.6227, "final_rank": 10 }, { "submission_id": "aoj_1318_8390116", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\nconst int INF = 1012345678;\n\nstruct edge {\n\tint to, cost;\n};\n\nstruct state {\n\tint pos, rem, cost;\n\tbool operator<(const state& s) const {\n\t\treturn cost > s.cost;\n\t}\n};\n\nint solve(int N, int M, int C, const string& src, const string& dst, const vector<string>& A, const vector<string>& B, const vector<int>& D, const vector<string>& S) {\n\tvector<string> cities;\n\tcities.push_back(src);\n\tcities.push_back(dst);\n\tcities.insert(cities.end(), A.begin(), A.end());\n\tcities.insert(cities.end(), B.begin(), B.end());\n\tcities.insert(cities.end(), S.begin(), S.end());\n\tsort(cities.begin(), cities.end());\n\tcities.erase(unique(cities.begin(), cities.end()), cities.end());\n\tint V = cities.size();\n\tvector<vector<edge> > G(V);\n\tfor (int i = 0; i < N; i++) {\n\t\tint va = lower_bound(cities.begin(), cities.end(), A[i]) - cities.begin();\n\t\tint vb = lower_bound(cities.begin(), cities.end(), B[i]) - cities.begin();\n\t\tG[va].push_back(edge{vb, D[i]});\n\t\tG[vb].push_back(edge{va, D[i]});\n\t}\n\tint vs = lower_bound(cities.begin(), cities.end(), src) - cities.begin();\n\tint vg = lower_bound(cities.begin(), cities.end(), dst) - cities.begin();\n\tvector<bool> station(V, false);\n\tfor (int i = 0; i < M; i++) {\n\t\tint pos = lower_bound(cities.begin(), cities.end(), S[i]) - cities.begin();\n\t\tstation[pos] = true;\n\t}\n\tvector<vector<int> > dist(V, vector<int>(C + 1, INF));\n\tpriority_queue<state> que;\n\tvector<vector<bool> > vis(V, vector<bool>(C + 1, false));\n\tdist[vs][C] = 0;\n\tque.push(state{vs, C, 0});\n\twhile (!que.empty()) {\n\t\tstate s = que.top();\n\t\tque.pop();\n\t\tif (!vis[s.pos][s.rem]) {\n\t\t\tvis[s.pos][s.rem] = true;\n\t\t\tfor (edge e : G[s.pos]) {\n\t\t\t\tint nrem = s.rem - e.cost;\n\t\t\t\tif (nrem >= 0) {\n\t\t\t\t\tif (station[e.to]) {\n\t\t\t\t\t\tnrem = C;\n\t\t\t\t\t}\n\t\t\t\t\tif (dist[e.to][nrem] > s.cost + e.cost) {\n\t\t\t\t\t\tdist[e.to][nrem] = s.cost + e.cost;\n\t\t\t\t\t\tque.push(state{e.to, nrem, dist[e.to][nrem]});\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n\tint ans = *min_element(dist[vg].begin(), dist[vg].end());\n\treturn ans;\n}\n\nint main() {\n\tcin.tie(0);\n\tios::sync_with_stdio(false);\n\twhile (true) {\n\t\tint N, M, C;\n\t\tcin >> N >> M >> C;\n\t\tif (N == 0 && M == 0 && C == 0) {\n\t\t\tbreak;\n\t\t}\n\t\tC *= 10;\n\t\tstring src, dst;\n\t\tcin >> src >> dst;\n\t\tvector<string> A(N), B(N);\n\t\tvector<int> D(N);\n\t\tfor (int i = 0; i < N; i++) {\n\t\t\tcin >> A[i] >> B[i] >> D[i];\n\t\t}\n\t\tvector<string> S(M);\n\t\tfor (int i = 0; i < M; i++) {\n\t\t\tcin >> S[i];\n\t\t}\n\t\tint ans = solve(N, M, C, src, dst, A, B, D, S);\n\t\tcout << (ans != INF ? ans : -1) << endl;\n\t}\n\treturn 0;\n}", "accuracy": 1, "time_ms": 660, "memory_kb": 54632, "score_of_the_acc": -0.591, "final_rank": 8 }, { "submission_id": "aoj_1318_6736548", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\n#define rep(i, s, t) for (int i = (int)(s); i < (int)(t); ++i)\n#define revrep(i, t, s) for (int i = (int)(t)-1; i >= (int)(s); --i)\n#define all(x) begin(x), end(x)\ntemplate <typename T> bool chmax(T& a, const T& b) { return a < b ? (a = b, 1) : 0; }\ntemplate <typename T> bool chmin(T& a, const T& b) { return a > b ? (a = b, 1) : 0; }\n\ntemplate <typename T>\nstruct Edge {\n int from, to;\n T weight;\n Edge() = default;\n Edge(int to, T weight) : from(-1), to(to), weight(weight) {}\n Edge(int from, int to, T weight) : from(from), to(to), weight(weight) {}\n};\n\n\ntemplate <typename T>\nstd::vector<T> dijkstra(const std::vector<std::vector<Edge<T>>>& G, int s) {\n std::vector<T> dist(G.size(), std::numeric_limits<T>::max());\n dist[s] = 0;\n using P = std::pair<T, int>;\n std::priority_queue<P, std::vector<P>, std::greater<P>> pq;\n pq.emplace(0, s);\n\n while (!pq.empty()) {\n T d;\n int v;\n std::tie(d, v) = pq.top();\n pq.pop();\n if (dist[v] < d) continue;\n for (auto& e : G[v]) {\n if (dist[e.to] > d + e.weight) {\n dist[e.to] = d + e.weight;\n pq.emplace(dist[e.to], e.to);\n }\n }\n }\n\n return dist;\n}\n\n\nint main() {\n ios_base::sync_with_stdio(false);\n cin.tie(nullptr);\n cout << fixed << setprecision(15);\n\n while (true) {\n int N, M, cap;\n cin >> N >> M >> cap;\n if (N == 0) break;\n string src, dest;\n cin >> src >> dest;\n vector<vector<Edge<int>>> G;\n map<string, int> idx;\n rep(_,0,N) {\n string su, sv;\n int d;\n cin >> su >> sv >> d;\n int u, v;\n if (idx.count(su)) u = idx[su];\n else {\n u = idx.size();\n idx[su] = u;\n G.emplace_back();\n }\n if (idx.count(sv)) v = idx[sv];\n else {\n v = idx.size();\n idx[sv] = v;\n G.emplace_back();\n }\n G[u].push_back({v, d});\n G[v].push_back({u, d});\n }\n int V = G.size();\n if (!idx.count(src) || !idx.count(dest)) {\n cout << -1 << endl;\n continue;\n }\n vector<int> stations;\n rep(i,0,M) {\n string s;\n cin >> s;\n stations.push_back(idx[s]);\n }\n stations.push_back(idx[src]);\n stations.push_back(idx[dest]);\n vector<vector<Edge<int>>> G2(V);\n for (int s : stations) {\n auto dist = dijkstra(G, s);\n for (int t : stations) {\n if (dist[t] <= cap * 10) G2[s].push_back({t, dist[t]});\n }\n }\n auto dist = dijkstra(G2, idx[src]);\n int ans = dist[idx[dest]];\n cout << (ans < 1e9 ? ans : -1) << endl;\n }\n}", "accuracy": 1, "time_ms": 280, "memory_kb": 5496, "score_of_the_acc": -0.038, "final_rank": 3 }, { "submission_id": "aoj_1318_6722793", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\n#ifdef _RUTHEN\n#include \"debug.hpp\"\n#else\n#define show(...) true\n#endif\n\nusing ll = long long;\n#define rep(i, n) for (int i = 0; i < (n); i++)\ntemplate <class T> using V = vector<T>;\n\nint main() {\n ios::sync_with_stdio(false);\n cin.tie(0);\n int N, M, cap;\n while (cin >> N >> M >> cap, !(N == 0 and M == 0 and cap == 0)) {\n V<string> SS;\n string src, dst;\n cin >> src >> dst;\n SS.push_back(src);\n SS.push_back(dst);\n V<string> c1(N), c2(N);\n V<int> d(N);\n rep(i, N) {\n cin >> c1[i] >> c2[i] >> d[i];\n SS.push_back(c1[i]);\n SS.push_back(c2[i]);\n }\n V<string> S(M);\n rep(i, M) {\n cin >> S[i];\n SS.push_back(S[i]);\n }\n sort(SS.begin(), SS.end());\n SS.erase(unique(SS.begin(), SS.end()), SS.end());\n int NV = SS.size();\n V<V<pair<int, ll>>> G(NV);\n rep(i, N) {\n int a = lower_bound(SS.begin(), SS.end(), c1[i]) - SS.begin();\n int b = lower_bound(SS.begin(), SS.end(), c2[i]) - SS.begin();\n G[a].push_back({b, d[i]});\n G[b].push_back({a, d[i]});\n }\n V<int> gas(NV, 0);\n rep(i, M) {\n int a = lower_bound(SS.begin(), SS.end(), S[i]) - SS.begin();\n gas[a] = 1;\n }\n const ll INF = 1LL << 60;\n cap *= 10;\n V<V<ll>> dist(NV, V<ll>(cap + 1, INF));\n int s = lower_bound(SS.begin(), SS.end(), src) - SS.begin();\n int g = lower_bound(SS.begin(), SS.end(), dst) - SS.begin();\n dist[s][cap] = 0;\n priority_queue<tuple<ll, int, int>> que;\n que.push({0, s, cap});\n bool ok = false;\n while (!que.empty()) {\n auto [d, cv, ccap] = que.top();\n que.pop();\n d = -d;\n if (cv == g) {\n cout << d << '\\n';\n ok = true;\n break;\n }\n if (dist[cv][ccap] != d) continue;\n if (gas[cv] and ccap < cap) {\n dist[cv][cap] = d;\n que.push({-d, cv, cap});\n }\n for (auto [nex, cost] : G[cv]) {\n if (ccap >= cost) {\n if (dist[nex][ccap - cost] > d + cost) {\n dist[nex][ccap - cost] = d + cost;\n que.push({-(d + cost), nex, ccap - cost});\n }\n }\n }\n }\n if (!ok) cout << -1 << '\\n';\n }\n return 0;\n}", "accuracy": 1, "time_ms": 1430, "memory_kb": 97808, "score_of_the_acc": -1.149, "final_rank": 17 }, { "submission_id": "aoj_1318_6673027", "code_snippet": "#include <bits/stdc++.h>\n\n#ifdef _MSC_VER\n# include <intrin.h>\n#else\n# include <x86intrin.h>\n#endif\n\n#include <limits>\n#include <type_traits>\n\nnamespace suisen {\n// ! utility\ntemplate <typename ...Types>\nusing constraints_t = std::enable_if_t<std::conjunction_v<Types...>, std::nullptr_t>;\ntemplate <bool cond_v, typename Then, typename OrElse>\nconstexpr decltype(auto) constexpr_if(Then&& then, OrElse&& or_else) {\n if constexpr (cond_v) {\n return std::forward<Then>(then);\n } else {\n return std::forward<OrElse>(or_else);\n }\n}\n\n// ! function\ntemplate <typename ReturnType, typename Callable, typename ...Args>\nusing is_same_as_invoke_result = std::is_same<std::invoke_result_t<Callable, Args...>, ReturnType>;\ntemplate <typename F, typename T>\nusing is_uni_op = is_same_as_invoke_result<T, F, T>;\ntemplate <typename F, typename T>\nusing is_bin_op = is_same_as_invoke_result<T, F, T, T>;\n\ntemplate <typename Comparator, typename T>\nusing is_comparator = std::is_same<std::invoke_result_t<Comparator, T, T>, bool>;\n\n// ! integral\ntemplate <typename T, typename = constraints_t<std::is_integral<T>>>\nconstexpr int bit_num = std::numeric_limits<std::make_unsigned_t<T>>::digits;\ntemplate <typename T, unsigned int n>\nstruct is_nbit { static constexpr bool value = bit_num<T> == n; };\ntemplate <typename T, unsigned int n>\nstatic constexpr bool is_nbit_v = is_nbit<T, n>::value;\n\n// ?\ntemplate <typename T>\nstruct safely_multipliable {};\ntemplate <>\nstruct safely_multipliable<int> { using type = long long; };\ntemplate <>\nstruct safely_multipliable<long long> { using type = __int128_t; };\ntemplate <>\nstruct safely_multipliable<unsigned int> { using type = unsigned long long; };\ntemplate <>\nstruct safely_multipliable<unsigned long int> { using type = __uint128_t; };\ntemplate <>\nstruct safely_multipliable<unsigned long long> { using type = __uint128_t; };\ntemplate <>\nstruct safely_multipliable<float> { using type = float; };\ntemplate <>\nstruct safely_multipliable<double> { using type = double; };\ntemplate <>\nstruct safely_multipliable<long double> { using type = long double; };\ntemplate <typename T>\nusing safely_multipliable_t = typename safely_multipliable<T>::type;\n\ntemplate <typename T, typename = void>\nstruct rec_value_type {\n using type = T;\n};\ntemplate <typename T>\nstruct rec_value_type<T, std::void_t<typename T::value_type>> {\n using type = typename rec_value_type<typename T::value_type>::type;\n};\ntemplate <typename T>\nusing rec_value_type_t = typename rec_value_type<T>::type;\n\n} // namespace suisen\n\n// ! type aliases\nusing i128 = __int128_t;\nusing u128 = __uint128_t;\n\ntemplate <typename T>\nusing pq_greater = std::priority_queue<T, std::vector<T>, std::greater<T>>;\ntemplate <typename T, typename U>\nusing umap = std::unordered_map<T, U>;\n\n// ! macros (capital: internal macro)\n#define OVERLOAD2(_1,_2,name,...) name\n#define OVERLOAD3(_1,_2,_3,name,...) name\n#define OVERLOAD4(_1,_2,_3,_4,name,...) name\n\n#define REP4(i,l,r,s) for(std::remove_reference_t<std::remove_const_t<decltype(r)>>i=(l);i<(r);i+=(s))\n#define REP3(i,l,r) REP4(i,l,r,1)\n#define REP2(i,n) REP3(i,0,n)\n#define REPINF3(i,l,s) for(std::remove_reference_t<std::remove_const_t<decltype(l)>>i=(l);;i+=(s))\n#define REPINF2(i,l) REPINF3(i,l,1)\n#define REPINF1(i) REPINF2(i,0)\n#define RREP4(i,l,r,s) for(std::remove_reference_t<std::remove_const_t<decltype(r)>>i=(l)+fld((r)-(l)-1,s)*(s);i>=(l);i-=(s))\n#define RREP3(i,l,r) RREP4(i,l,r,1)\n#define RREP2(i,n) RREP3(i,0,n)\n\n#define rep(...) OVERLOAD4(__VA_ARGS__, REP4 , REP3 , REP2 )(__VA_ARGS__)\n#define rrep(...) OVERLOAD4(__VA_ARGS__, RREP4 , RREP3 , RREP2 )(__VA_ARGS__)\n#define repinf(...) OVERLOAD3(__VA_ARGS__, REPINF3, REPINF2, REPINF1)(__VA_ARGS__)\n\n#define CAT_I(a, b) a##b\n#define CAT(a, b) CAT_I(a, b)\n#define UNIQVAR(tag) CAT(tag, __LINE__)\n#define loop(n) for (std::remove_reference_t<std::remove_const_t<decltype(n)>> UNIQVAR(loop_variable) = n; UNIQVAR(loop_variable) --> 0;)\n\n#define all(iterable) std::begin(iterable), std::end(iterable)\n#define input(type, ...) type __VA_ARGS__; read(__VA_ARGS__)\n\n#ifdef LOCAL\n# define debug(...) debug_internal(#__VA_ARGS__, __VA_ARGS__)\n\ntemplate <class T, class... Args>\nvoid debug_internal(const char* s, T&& first, Args&&... args) {\n constexpr const char* prefix = \"[\\033[32mDEBUG\\033[m] \";\n constexpr const char* open_brakets = sizeof...(args) == 0 ? \"\" : \"(\";\n constexpr const char* close_brakets = sizeof...(args) == 0 ? \"\" : \")\";\n std::cerr << prefix << open_brakets << s << close_brakets << \": \" << open_brakets << std::forward<T>(first);\n ((std::cerr << \", \" << std::forward<Args>(args)), ...);\n std::cerr << close_brakets << \"\\n\";\n}\n\n#else\n# define debug(...) void(0)\n#endif\n\n// ! I/O utilities\n\n// __int128_t\nstd::ostream& operator<<(std::ostream& dest, __int128_t value) {\n std::ostream::sentry s(dest);\n if (s) {\n __uint128_t tmp = value < 0 ? -value : value;\n char buffer[128];\n char* d = std::end(buffer);\n do {\n --d;\n *d = \"0123456789\"[tmp % 10];\n tmp /= 10;\n } while (tmp != 0);\n if (value < 0) {\n --d;\n *d = '-';\n }\n int len = std::end(buffer) - d;\n if (dest.rdbuf()->sputn(d, len) != len) {\n dest.setstate(std::ios_base::badbit);\n }\n }\n return dest;\n}\n// __uint128_t\nstd::ostream& operator<<(std::ostream& dest, __uint128_t value) {\n std::ostream::sentry s(dest);\n if (s) {\n char buffer[128];\n char* d = std::end(buffer);\n do {\n --d;\n *d = \"0123456789\"[value % 10];\n value /= 10;\n } while (value != 0);\n int len = std::end(buffer) - d;\n if (dest.rdbuf()->sputn(d, len) != len) {\n dest.setstate(std::ios_base::badbit);\n }\n }\n return dest;\n}\n\n// pair\ntemplate <typename T, typename U>\nstd::ostream& operator<<(std::ostream& out, const std::pair<T, U>& a) {\n return out << a.first << ' ' << a.second;\n}\n// tuple\ntemplate <unsigned int N = 0, typename ...Args>\nstd::ostream& operator<<(std::ostream& out, const std::tuple<Args...>& a) {\n if constexpr (N >= std::tuple_size_v<std::tuple<Args...>>) {\n return out;\n } else {\n out << std::get<N>(a);\n if constexpr (N + 1 < std::tuple_size_v<std::tuple<Args...>>) {\n out << ' ';\n }\n return operator<<<N + 1>(out, a);\n }\n}\n// vector\ntemplate <typename T>\nstd::ostream& operator<<(std::ostream& out, const std::vector<T>& a) {\n for (auto it = a.begin(); it != a.end();) {\n out << *it;\n if (++it != a.end()) out << ' ';\n }\n return out;\n}\n// array\ntemplate <typename T, size_t N>\nstd::ostream& operator<<(std::ostream& out, const std::array<T, N>& a) {\n for (auto it = a.begin(); it != a.end();) {\n out << *it;\n if (++it != a.end()) out << ' ';\n }\n return out;\n}\ninline void print() { std::cout << '\\n'; }\ntemplate <typename Head, typename... Tail>\ninline void print(const Head& head, const Tail &...tails) {\n std::cout << head;\n if (sizeof...(tails)) std::cout << ' ';\n print(tails...);\n}\ntemplate <typename Iterable>\nauto print_all(const Iterable& v, std::string sep = \" \", std::string end = \"\\n\") -> decltype(std::cout << *v.begin(), void()) {\n for (auto it = v.begin(); it != v.end();) {\n std::cout << *it;\n if (++it != v.end()) std::cout << sep;\n }\n std::cout << end;\n}\n\n__int128_t parse_i128(std::string& s) {\n __int128_t ret = 0;\n for (int i = 0; i < int(s.size()); i++) if ('0' <= s[i] and s[i] <= '9') ret = 10 * ret + s[i] - '0';\n if (s[0] == '-') ret = -ret;\n return ret;\n}\n__uint128_t parse_u128(std::string& s) {\n __uint128_t ret = 0;\n for (int i = 0; i < int(s.size()); i++) if ('0' <= s[i] and s[i] <= '9') ret = 10 * ret + s[i] - '0';\n return ret;\n}\n// __int128_t\nstd::istream& operator>>(std::istream& in, __int128_t& v) {\n std::string s;\n in >> s;\n v = parse_i128(s);\n return in;\n}\n// __uint128_t\nstd::istream& operator>>(std::istream& in, __uint128_t& v) {\n std::string s;\n in >> s;\n v = parse_u128(s);\n return in;\n}\n// pair\ntemplate <typename T, typename U>\nstd::istream& operator>>(std::istream& in, std::pair<T, U>& a) {\n return in >> a.first >> a.second;\n}\n// tuple\ntemplate <unsigned int N = 0, typename ...Args>\nstd::istream& operator>>(std::istream& in, std::tuple<Args...>& a) {\n if constexpr (N >= std::tuple_size_v<std::tuple<Args...>>) {\n return in;\n } else {\n return operator>><N + 1>(in >> std::get<N>(a), a);\n }\n}\n// vector\ntemplate <typename T>\nstd::istream& operator>>(std::istream& in, std::vector<T>& a) {\n for (auto it = a.begin(); it != a.end(); ++it) in >> *it;\n return in;\n}\n// array\ntemplate <typename T, size_t N>\nstd::istream& operator>>(std::istream& in, std::array<T, N>& a) {\n for (auto it = a.begin(); it != a.end(); ++it) in >> *it;\n return in;\n}\ntemplate <typename ...Args>\nvoid read(Args &...args) {\n (std::cin >> ... >> args);\n}\n\n// ! integral utilities\n\n// Returns pow(-1, n)\ntemplate <typename T>\nconstexpr inline int pow_m1(T n) {\n return -(n & 1) | 1;\n}\n// Returns pow(-1, n)\ntemplate <>\nconstexpr inline int pow_m1<bool>(bool n) {\n return -int(n) | 1;\n}\n\n// Returns floor(x / y)\ntemplate <typename T>\nconstexpr inline T fld(const T x, const T y) {\n return (x ^ y) >= 0 ? x / y : (x - (y + pow_m1(y >= 0))) / y;\n}\ntemplate <typename T>\nconstexpr inline T cld(const T x, const T y) {\n return (x ^ y) <= 0 ? x / y : (x + (y + pow_m1(y >= 0))) / y;\n}\n\ntemplate <typename T, suisen::constraints_t<suisen::is_nbit<T, 16>> = nullptr>\n__attribute__((target(\"popcnt\"))) constexpr inline int popcount(const T x) { return _mm_popcnt_u32(x); }\ntemplate <typename T, suisen::constraints_t<suisen::is_nbit<T, 32>> = nullptr>\n__attribute__((target(\"popcnt\"))) constexpr inline int popcount(const T x) { return _mm_popcnt_u32(x); }\ntemplate <typename T, suisen::constraints_t<suisen::is_nbit<T, 64>> = nullptr>\n__attribute__((target(\"popcnt\"))) constexpr inline int popcount(const T x) { return _mm_popcnt_u64(x); }\ntemplate <typename T, suisen::constraints_t<suisen::is_nbit<T, 16>> = nullptr>\nconstexpr inline int count_lz(const T x) { return x ? __builtin_clz(x) : suisen::bit_num<T>; }\ntemplate <typename T, suisen::constraints_t<suisen::is_nbit<T, 32>> = nullptr>\nconstexpr inline int count_lz(const T x) { return x ? __builtin_clz(x) : suisen::bit_num<T>; }\ntemplate <typename T, suisen::constraints_t<suisen::is_nbit<T, 64>> = nullptr>\nconstexpr inline int count_lz(const T x) { return x ? __builtin_clzll(x) : suisen::bit_num<T>; }\ntemplate <typename T, suisen::constraints_t<suisen::is_nbit<T, 16>> = nullptr>\nconstexpr inline int count_tz(const T x) { return x ? __builtin_ctz(x) : suisen::bit_num<T>; }\ntemplate <typename T, suisen::constraints_t<suisen::is_nbit<T, 32>> = nullptr>\nconstexpr inline int count_tz(const T x) { return x ? __builtin_ctz(x) : suisen::bit_num<T>; }\ntemplate <typename T, suisen::constraints_t<suisen::is_nbit<T, 64>> = nullptr>\nconstexpr inline int count_tz(const T x) { return x ? __builtin_ctzll(x) : suisen::bit_num<T>; }\ntemplate <typename T>\nconstexpr inline int floor_log2(const T x) { return suisen::bit_num<T> -1 - count_lz(x); }\ntemplate <typename T>\nconstexpr inline int ceil_log2(const T x) { return floor_log2(x) + ((x & -x) != x); }\ntemplate <typename T>\nconstexpr inline int kth_bit(const T x, const unsigned int k) { return (x >> k) & 1; }\ntemplate <typename T>\nconstexpr inline int parity(const T x) { return popcount(x) & 1; }\n\n// ! container\n\ntemplate <typename T, typename Comparator, suisen::constraints_t<suisen::is_comparator<Comparator, T>> = nullptr>\nauto priqueue_comp(const Comparator comparator) {\n return std::priority_queue<T, std::vector<T>, Comparator>(comparator);\n}\n\ntemplate <typename Iterable>\nauto isize(const Iterable& iterable) -> decltype(int(iterable.size())) {\n return iterable.size();\n}\n\ntemplate <typename T, typename Gen, suisen::constraints_t<suisen::is_same_as_invoke_result<T, Gen, int>> = nullptr>\nauto generate_vector(int n, Gen generator) {\n std::vector<T> v(n);\n for (int i = 0; i < n; ++i) v[i] = generator(i);\n return v;\n}\ntemplate <typename T>\nauto generate_range_vector(T l, T r) {\n return generate_vector(r - l, [l](int i) { return l + i; });\n}\ntemplate <typename T>\nauto generate_range_vector(T n) {\n return generate_range_vector(0, n);\n}\n\ntemplate <typename T>\nvoid sort_unique_erase(std::vector<T>& a) {\n std::sort(a.begin(), a.end());\n a.erase(std::unique(a.begin(), a.end()), a.end());\n}\n\ntemplate <typename InputIterator, typename BiConsumer>\nauto foreach_adjacent_values(InputIterator first, InputIterator last, BiConsumer f) -> decltype(f(*first++, *last), void()) {\n if (first != last) for (auto itr = first, itl = itr++; itr != last; itl = itr++) f(*itl, *itr);\n}\ntemplate <typename Container, typename BiConsumer>\nauto foreach_adjacent_values(Container c, BiConsumer f) -> decltype(c.begin(), c.end(), void()) {\n foreach_adjacent_values(c.begin(), c.end(), f);\n}\n\n// ! other utilities\n\n// x <- min(x, y). returns true iff `x` has chenged.\ntemplate <typename T>\ninline bool chmin(T& x, const T& y) {\n if (y >= x) return false;\n x = y;\n return true;\n}\n// x <- max(x, y). returns true iff `x` has chenged.\ntemplate <typename T>\ninline bool chmax(T& x, const T& y) {\n if (y <= x) return false;\n x = y;\n return true;\n}\n\ntemplate <typename T, std::enable_if_t<std::is_integral_v<T>, std::nullptr_t> = nullptr>\nstd::string bin(T val, int bit_num = -1) {\n std::string res;\n if (bit_num >= 0) {\n for (int bit = bit_num; bit-- > 0;) res += '0' + ((val >> bit) & 1);\n } else {\n for (; val; val >>= 1) res += '0' + (val & 1);\n std::reverse(res.begin(), res.end());\n }\n return res;\n}\n\ntemplate <typename T, std::enable_if_t<std::is_integral_v<T>, std::nullptr_t> = nullptr>\nstd::vector<T> digits_low_to_high(T val, T base = 10) {\n std::vector<T> res;\n for (; val; val /= base) res.push_back(val % base);\n if (res.empty()) res.push_back(T{ 0 });\n return res;\n}\ntemplate <typename T, std::enable_if_t<std::is_integral_v<T>, std::nullptr_t> = nullptr>\nstd::vector<T> digits_high_to_low(T val, T base = 10) {\n auto res = digits_low_to_high(val, base);\n std::reverse(res.begin(), res.end());\n return res;\n}\n\ntemplate <typename T>\nstd::string join(const std::vector<T>& v, const std::string& sep, const std::string& end) {\n std::ostringstream ss;\n for (auto it = v.begin(); it != v.end();) {\n ss << *it;\n if (++it != v.end()) ss << sep;\n }\n ss << end;\n return ss.str();\n}\n\nnamespace suisen {}\nusing namespace suisen;\nusing namespace std;\n\nstruct io_setup {\n io_setup(int precision = 20) {\n std::ios::sync_with_stdio(false);\n std::cin.tie(nullptr);\n std::cout << std::fixed << std::setprecision(precision);\n }\n} io_setup_{};\n\n// ! code from here\n\n#include <algorithm>\n#include <cassert>\n#include <vector>\n\nnamespace suisen {\ntemplate <typename T>\nclass CoordinateCompressorBuilder {\n public:\n struct Compressor {\n public:\n static constexpr int absent = -1;\n\n // default constructor\n Compressor() : _xs(std::vector<T>{}) {}\n // Construct from strictly sorted vector\n Compressor(const std::vector<T> &xs) : _xs(xs) {\n assert(is_strictly_sorted(xs));\n }\n\n // Return the number of distinct keys.\n int size() const {\n return _xs.size();\n }\n // Check if the element is registered.\n bool has_key(const T &e) const {\n return std::binary_search(_xs.begin(), _xs.end(), e);\n }\n // Compress the element. if not registered, returns `default_value`. (default: Compressor::absent)\n int comp(const T &e, int default_value = absent) const {\n const int res = min_geq_index(e);\n return res != size() and _xs[res] == e ? res : default_value;\n }\n // Restore the element from the index.\n T decomp(const int compressed_index) const {\n return _xs[compressed_index];\n }\n // Compress the element. Equivalent to call `comp(e)`\n int operator[](const T &e) const {\n return comp(e);\n }\n // Return the minimum registered value greater than `e`. if not exists, return `default_value`.\n T min_gt(const T &e, const T &default_value) const {\n auto it = std::upper_bound(_xs.begin(), _xs.end(), e);\n return it == _xs.end() ? default_value : *it;\n }\n // Return the minimum registered value greater than or equal to `e`. if not exists, return `default_value`.\n T min_geq(const T &e, const T &default_value) const {\n auto it = std::lower_bound(_xs.begin(), _xs.end(), e);\n return it == _xs.end() ? default_value : *it;\n }\n // Return the maximum registered value less than `e`. if not exists, return `default_value`\n T max_lt(const T &e, const T &default_value) const {\n auto it = std::upper_bound(_xs.rbegin(), _xs.rend(), e, std::greater<T>());\n return it == _xs.rend() ? default_value : *it;\n }\n // Return the maximum registered value less than or equal to `e`. if not exists, return `default_value`\n T max_leq(const T &e, const T &default_value) const {\n auto it = std::lower_bound(_xs.rbegin(), _xs.rend(), e, std::greater<T>());\n return it == _xs.rend() ? default_value : *it;\n }\n // Return the compressed index of the minimum registered value greater than `e`. if not exists, return `compressor.size()`.\n int min_gt_index(const T &e) const {\n return std::upper_bound(_xs.begin(), _xs.end(), e) - _xs.begin();\n }\n // Return the compressed index of the minimum registered value greater than or equal to `e`. if not exists, return `compressor.size()`.\n int min_geq_index(const T &e) const {\n return std::lower_bound(_xs.begin(), _xs.end(), e) - _xs.begin();\n }\n // Return the compressed index of the maximum registered value less than `e`. if not exists, return -1.\n int max_lt_index(const T &e) const {\n return int(_xs.rend() - std::upper_bound(_xs.rbegin(), _xs.rend(), e, std::greater<T>())) - 1;\n }\n // Return the compressed index of the maximum registered value less than or equal to `e`. if not exists, return -1.\n int max_leq_index(const T &e) const {\n return int(_xs.rend() - std::lower_bound(_xs.rbegin(), _xs.rend(), e, std::greater<T>())) - 1;\n }\n private:\n std::vector<T> _xs;\n static bool is_strictly_sorted(const std::vector<T> &v) {\n return std::adjacent_find(v.begin(), v.end(), std::greater_equal<T>()) == v.end();\n }\n };\n CoordinateCompressorBuilder() : _xs(std::vector<T>{}) {}\n explicit CoordinateCompressorBuilder(const std::vector<T> &xs) : _xs(xs) {}\n explicit CoordinateCompressorBuilder(std::vector<T> &&xs) : _xs(std::move(xs)) {}\n template <typename Gen, constraints_t<is_same_as_invoke_result<T, Gen, int>> = nullptr>\n CoordinateCompressorBuilder(const int n, Gen generator) {\n reserve(n);\n for (int i = 0; i < n; ++i) push(generator(i));\n }\n // Attempt to preallocate enough memory for specified number of elements.\n void reserve(int n) {\n _xs.reserve(n);\n }\n // Add data.\n void push(const T &first) {\n _xs.push_back(first);\n }\n // Add data.\n void push(T &&first) {\n _xs.push_back(std::move(first));\n }\n // Add data in the range of [first, last). \n template <typename Iterator>\n auto push(const Iterator &first, const Iterator &last) -> decltype(std::vector<T>{}.push_back(*first), void()) {\n for (auto it = first; it != last; ++it) _xs.push_back(*it);\n }\n // Add all data in the container. Equivalent to `push(iterable.begin(), iterable.end())`.\n template <typename Iterable>\n auto push(const Iterable &iterable) -> decltype(std::vector<T>{}.push_back(*iterable.begin()), void()) {\n push(iterable.begin(), iterable.end());\n }\n // Add data.\n template <typename ...Args>\n void emplace(Args &&...args) {\n _xs.emplace_back(std::forward<Args>(args)...);\n }\n // Build compressor.\n auto build() {\n std::sort(_xs.begin(), _xs.end()), _xs.erase(std::unique(_xs.begin(), _xs.end()), _xs.end());\n return Compressor {_xs};\n }\n // Build compressor from vector.\n static auto build(const std::vector<T> &xs) {\n return CoordinateCompressorBuilder(xs).build();\n }\n // Build compressor from vector.\n static auto build(std::vector<T> &&xs) {\n return CoordinateCompressorBuilder(std::move(xs)).build();\n }\n // Build compressor from generator.\n template <typename Gen, constraints_t<is_same_as_invoke_result<T, Gen, int>> = nullptr>\n static auto build(const int n, Gen generator) {\n return CoordinateCompressorBuilder<T>(n, generator).build();\n }\n private:\n std::vector<T> _xs;\n};\n\n} // namespace suisen\n\nlong long solve(int n, int c, int s, int t, vector<tuple<int, int, int>> edges, vector<int> st) {\n vector<int8_t> gs(n, false);\n for (auto e : st) {\n gs[e] = true;\n }\n\n vector<vector<pair<int, int>>> g(n);\n for (auto [u, v, w] : edges) {\n g[u].emplace_back(v, w);\n g[v].emplace_back(u, w);\n }\n\n vector d(n, vector<long long>(c + 1, numeric_limits<long long>::max()));\n pq_greater<tuple<long long, int, int>> pq;\n pq.emplace(d[s][c] = 0, s, c);\n while (pq.size()) {\n auto [dvx, v, x] = pq.top();\n pq.pop();\n if (dvx != d[v][x]) continue;\n for (auto [nv, w] : g[v]) if (w <= x) {\n int nx = gs[nv] ? c : x - w;\n if (chmin(d[nv][nx], dvx + w)) {\n pq.emplace(d[nv][nx], nv, nx);\n }\n }\n }\n\n long long ans = *min_element(all(d[t]));\n if (ans == numeric_limits<long long>::max()) return -1;\n return ans;\n}\n\nint main() {\n while (true) {\n input(int, n, m, c);\n if (n == 0 and m == 0 and c == 0) break;;\n\n c *= 10;\n input(string, s, t);\n vector<tuple<string, string, int>> edges(n);\n read(edges);\n vector<string> st(m);\n read(st);\n\n CoordinateCompressorBuilder<string> builder;\n builder.push(s);\n builder.push(t);\n\n for (auto& [u, v, w] : edges) {\n builder.push(u);\n builder.push(v);\n }\n for (auto& x : st) {\n builder.push(x);\n }\n auto cmp = builder.build();\n\n int si = cmp[s], ti = cmp[t];\n vector<tuple<int, int, int>> es;\n for (auto& [u, v, w] : edges) {\n es.emplace_back(cmp[u], cmp[v], w);\n }\n vector<int> sts;\n for (auto& x : st) {\n sts.push_back(cmp[x]);\n }\n print(solve(cmp.size(), c, si, ti, es, sts));\n }\n\n return 0;\n}", "accuracy": 1, "time_ms": 1200, "memory_kb": 98244, "score_of_the_acc": -1.1154, "final_rank": 15 }, { "submission_id": "aoj_1318_6605348", "code_snippet": "#include <stdio.h>\n#include <string>\n#include <vector>\n#include <unordered_map>\n#include <unordered_set>\n#include <queue>\n#define INF 1012345678\nusing namespace std;\n\nchar buff1[32], buff2[32];\nunordered_map<string, vector<pair<string, int>>> ed;\nvector<pair<int, int>> edn[6144], edn2[6144];\nunordered_map<string, int> nodenm;\nint distn[6144], visn[6144];\nunordered_set<string> city, lpg;\npriority_queue<pair<int, int>, vector<pair<int, int>>, greater<>> pq;\n\nvoid dijk(int src, int n, int m, int cap) {\n int node, d;\n for (int i = 1; i <= n; i++) {\n distn[i] = cap + 1;\n visn[i] = 0;\n }\n \n while (pq.size()) pq.pop();\n pq.push({ 0, src });\n while (pq.size()) {\n node = pq.top().second;\n d = pq.top().first;\n pq.pop();\n if (visn[node]) continue;\n else visn[node] = 1;\n for (auto& i : edn[node]) {\n if (d + i.second < distn[i.first]) {\n distn[i.first] = d + i.second;\n pq.push({ distn[i.first], i.first });\n }\n }\n }\n\n for (int i = 1; i <= m; i++) {\n if (distn[i] <= cap) {\n edn2[src].push_back({ i, distn[i] });\n }\n }\n}\n\nint dijk2(int src, int dest, int n) {\n int node;\n int d;\n for (int i = 1; i <= n; i++) {\n distn[i] = INF;\n visn[i] = 0;\n }\n\n while (pq.size()) pq.pop();\n pq.push({ 0, src });\n while (pq.size()) {\n node = pq.top().second;\n d = pq.top().first;\n pq.pop();\n if (node == dest) return d;\n if (visn[node]) continue;\n else visn[node] = 1;\n for (auto& i : edn2[node]) {\n if (d + i.second < distn[i.first]) {\n distn[i.first] = d + i.second;\n pq.push({ distn[i.first], i.first });\n }\n }\n }\n\n return -1;\n}\n\nint main(void) {\n string src, dest;\n int n, m, cap, x;\n while (1) {\n scanf(\"%d %d %d\", &n, &m, &cap);\n if (n == 0 && m == 0 && cap == 0) break;\n else cap *= 10;\n scanf(\"%s %s\", buff1, buff2);\n src = buff1, dest = buff2;\n for (int i = 0; i < n; i++) {\n scanf(\"%s %s %d\", buff1, buff2, &x);\n if (ed.find(buff1) == ed.end()) ed.insert({ buff1, {{buff2, x}} });\n else ed[buff1].push_back({ buff2, x });\n if (ed.find(buff2) == ed.end()) ed.insert({ buff2, {{buff1, x}} });\n else ed[buff2].push_back({ buff1, x });\n if (city.find(buff1) == city.end()) city.insert(buff1);\n if (city.find(buff2) == city.end()) city.insert(buff2);\n }\n for (int i = 0; i < m; i++) {\n scanf(\"%s\", buff1);\n lpg.insert(buff1);\n }\n lpg.insert(src);\n lpg.insert(dest);\n nodenm.insert({ src, 1 });\n nodenm.insert({ dest, 2 });\n m = 2;\n for (auto& i : lpg) {\n if (i == src || i == dest) continue;\n nodenm.insert({ i, ++m });\n }\n n = m;\n for (auto& i : city) {\n if (lpg.find(i) != lpg.end()) continue;\n nodenm.insert({ i, ++n });\n }\n for (auto& i : ed) {\n for (auto& j : i.second) {\n edn[nodenm[i.first]].push_back({nodenm[j.first], j.second});\n }\n }\n\n\n for (int i = 1; i <= m; i++) {\n dijk(i, n, m, cap);\n }\n printf(\"%d\\n\", dijk2(1, 2, m));\n\n\n city.clear();\n lpg.clear();\n ed.clear();\n for (int i = 1; i <= n; i++) {\n edn[i].clear();\n edn2[i].clear();\n }\n nodenm.clear();\n }\n return 0;\n}", "accuracy": 1, "time_ms": 190, "memory_kb": 6128, "score_of_the_acc": -0.0294, "final_rank": 2 }, { "submission_id": "aoj_1318_6605184", "code_snippet": "#include <stdio.h>\n#include <string>\n#include <vector>\n#include <unordered_map>\n#include <unordered_set>\n#include <queue>\n#define INF 1012345678\nusing namespace std;\n\nchar buff1[32], buff2[32];\nunordered_map<string, vector<pair<string, int>>> ed, ed2;\nunordered_map<string, int> dist;\nunordered_set<string> city, lpg, vis;\npriority_queue<pair<int, string>, vector<pair<int, string>>, greater<>> pq;\n\nvoid dijk(string src, string node, int cap) {\n int d;\n dist.clear();\n vis.clear();\n for (auto& i : city) dist.insert({ i, cap + 1 });\n pq.push({ 0, src });\n while (pq.size()) {\n node = pq.top().second;\n d = pq.top().first;\n pq.pop();\n if (vis.find(node) != vis.end()) continue;\n else vis.insert(node);\n for (auto& i : ed[node]) {\n if (d + i.second < dist[i.first]) {\n dist[i.first] = d + i.second;\n pq.push({ dist[i.first], i.first });\n }\n }\n }\n\n for (auto& i : dist) {\n if (i.second <= cap && lpg.find(i.first) != lpg.end()) {\n if (ed2.find(src) == ed2.end()) ed2.insert({ src, {{i.first, i.second}} });\n else ed2[src].push_back({ i.first, i.second });\n }\n }\n}\n\nint dijk2(string src, string dest) {\n string node;\n int d;\n dist.clear();\n vis.clear();\n for (auto& i : lpg) dist.insert({ i, INF });\n pq.push({ 0, src });\n while (pq.size()) {\n node = pq.top().second;\n d = pq.top().first;\n pq.pop();\n if (node == dest) return d;\n if (vis.find(node) != vis.end()) continue;\n else vis.insert(node);\n for (auto& i : ed2[node]) {\n if (d + i.second < dist[i.first]) {\n dist[i.first] = d + i.second;\n pq.push({ dist[i.first], i.first });\n }\n }\n }\n\n return -1;\n}\n\nint main(void) {\n string src, dest;\n int n, m, cap, x;\n while (1) {\n scanf(\"%d %d %d\", &n, &m, &cap);\n if (n == 0 && m == 0 && cap == 0) break;\n else cap *= 10;\n scanf(\"%s %s\", buff1, buff2);\n src = buff1, dest = buff2;\n for (int i = 0; i < n; i++) {\n scanf(\"%s %s %d\", buff1, buff2, &x);\n if (ed.find(buff1) == ed.end()) ed.insert({ buff1, {{buff2, x}} });\n else ed[buff1].push_back({ buff2, x });\n if (ed.find(buff2) == ed.end()) ed.insert({ buff2, {{buff1, x}} });\n else ed[buff2].push_back({ buff1, x });\n if (city.find(buff1) == city.end()) city.insert(buff1);\n if (city.find(buff2) == city.end()) city.insert(buff2);\n }\n for (int i = 0; i < m; i++) {\n scanf(\"%s\", buff1);\n lpg.insert(buff1);\n }\n lpg.insert(src);\n lpg.insert(dest);\n for (auto& i : lpg) {\n dijk(i, i, cap);\n }\n printf(\"%d\\n\", dijk2(src, dest));\n\n\n city.clear();\n lpg.clear();\n dist.clear();\n vis.clear();\n ed.clear();\n ed2.clear();\n }\n return 0;\n}", "accuracy": 1, "time_ms": 1790, "memory_kb": 9008, "score_of_the_acc": -0.3222, "final_rank": 7 }, { "submission_id": "aoj_1318_6176860", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nconst int inf = 1 << 30;\n\nstruct abc{\n string a;\n string b;\n int c;\n};\n\nstruct nc{\n int npos;\n int c;\n};\n\nstruct HQ{\n int d;\n int pos;\n int cap;\n \n bool operator<(const HQ& right) const{\n return d > right.d;\n }\n};\n\nint main(void){\n int n, m, cap;\n while(1){\n cin >> n >> m >> cap;\n if(n == 0) break;\n string s, g;\n cin >> s >> g;\n set<string> city;\n city.insert(s);\n city.insert(g);\n vector<abc> E(n);\n for(int i = 0; i < n; i++){\n cin >> E[i].a >> E[i].b >> E[i].c;\n city.insert(E[i].a);\n city.insert(E[i].b);\n }\n map<string, int> mp;\n int x = 0;\n for(auto c: city){\n mp[c] = x;\n x++;\n }\n set<int> se;\n for(int i = 0; i < m; i++){\n string tmp;\n cin >> tmp;\n se.insert(mp[tmp]);\n }\n int ss = mp[s];\n int gg = mp[g];\n se.insert(gg);\n int l = city.size();\n vector<vector<nc>> edges(l, vector<nc>());\n for(auto tmp: E){\n edges[mp[tmp.a]].push_back({mp[tmp.b], tmp.c});\n edges[mp[tmp.b]].push_back({mp[tmp.a], tmp.c});\n }\n cap *= 10;\n vector<vector<int>> dist(l, vector<int>(cap + 1, inf));\n dist[ss][cap] = 0;\n priority_queue<HQ> hq;\n hq.push({0, ss, cap});\n int d, pos, cc, npos, c, nc, nd;\n while(!hq.empty()){\n auto tmp = hq.top();\n d = tmp.d;\n pos = tmp.pos;\n cc = tmp.cap;\n hq.pop();\n if(se.count(pos) && cc != cap){\n if(dist[pos][cap] > d){\n dist[pos][cap] = d;\n hq.push({d, pos, cap});\n }\n continue;\n }\n \n for(auto tmp2: edges[pos]){\n npos = tmp2.npos;\n c = tmp2.c;\n if(c > cc) continue;\n nc = cc - c;\n nd = d + c;\n if(dist[npos][nc] > nd){\n dist[npos][nc] = nd;\n hq.push({nd, npos, nc});\n }\n }\n }\n if(dist[gg][cap] == inf) cout << \"-1\\n\";\n else cout << dist[gg][cap] << \"\\n\";\n }\n \n}", "accuracy": 1, "time_ms": 960, "memory_kb": 52472, "score_of_the_acc": -0.619, "final_rank": 9 } ]
aoj_1317_cpp
Problem C: Weaker than Planned The committee members of the Kitoshima programming contest had decided to use crypto-graphic software for their secret communication. They had asked a company, Kodai Software, to develop cryptographic software that employed a cipher based on highly sophisticated mathematics. According to reports on IT projects, many projects are not delivered on time, on budget, with required features and functions. This applied to this case. Kodai Software failed to implement the cipher by the appointed date of delivery, and asked to use a simpler version that employed a type of substitution cipher for the moment. The committee members got angry and strongly requested to deliver the full specification product, but they unwillingly decided to use this inferior product for the moment. In what follows, we call the text before encryption, plaintext, and the text after encryption, ciphertext . This simple cipher substitutes letters in the plaintext, and its substitution rule is specified with a set of pairs. A pair consists of two letters and is unordered, that is, the order of the letters in the pair does not matter. A pair (A, B) and a pair (B, A) have the same meaning. In one substitution rule, one letter can appear in at most one single pair. When a letter in a pair appears in the plaintext, the letter is replaced with the other letter in the pair. Letters not specified in any pairs are left as they are. For example, by substituting the plaintext ABCDEFGHIJKLMNOPQRSTUVWXYZ with the substitution rule {(A, Z), (B, Y)} results in the following ciphertext. ZYCDEFGHIJKLMNOPQRSTUVWXBA This may be a big chance for us, because the substitution rule seems weak against cracking. We may be able to know communications between committee members. The mission here is to develop a deciphering program that finds the plaintext messages from given ciphertext messages. A ciphertext message is composed of one or more ciphertext words. A ciphertext word is generated from a plaintext word with a substitution rule. You have a list of candidate words containing the words that can appear in the plaintext; no other words may appear. Some words in the list may not actually be used in the plaintext. There always exists at least one sequence of candidate words from which the given ciphertext is obtained by some substitution rule. There may be cases where it is impossible to uniquely identify the plaintext from a given ciphertext and the list of candidate words. Input The input consists of multiple datasets, each of which contains a ciphertext message and a list of candidate words in the following format. n word 1 . . . word n sequence n in the first line is a positive integer, representing the number of candidate words. Each of the next n lines represents one of the candidate words. The last line, sequence, is a sequence of one or more ciphertext words separated by a single space and terminated with a period. You may assume the number of characters in each sequ ...(truncated)
[ { "submission_id": "aoj_1317_7228208", "code_snippet": "#ifdef LOCAL\n#define _GLIBCXX_DEBUG\n#endif\n#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\nusing vi = vector<int>;\ntemplate <class T = int>\nusing V = vector<T>;\n\n\n#define repname(a, b, c, d, ...) d\n#define rep(...) repname(__VA_ARGS__, REP2, REP1, REP0)(__VA_ARGS__)\n#define REP2(i, l, r) for(int i = (l); i < (r); i++)\n#define REP1(i, x) REP2(i, 0, x)\n#define REP0(x) REP1(SPJ, x)\n#define all(v) (v).begin(), (v).end()\n#define sz(v) (int)(v).size()\n\nconstexpr int inf = 1e9;\nconstexpr ll INF = 1e18;\n\ntemplate <class T>\nbool chmin(T &a, const T b) {\n\treturn a > b ? a = b, 1 : 0;\n}\n\ntemplate <class T>\nbool chmax(T &a, const T b) {\n\treturn a < b ? a = b, 1 : 0;\n}\n\nvoid solve(int n) {\n\tvector<string> w(n), c;\n\trep(i, n) {\n\t\tcin >> w[i];\n\t}\n\twhile(1) {\n\t\tstring s;\n\t\tcin >> s;\n\t\tc.push_back(s);\n\t\tif(c.back().back() == '.') {\n\t\t\tc.back().pop_back();\n\t\t\tbreak;\n\t\t}\n\t}\n\tint m = sz(c);\n\tvector<vector<string>> lef(21), rig(21);\n\trep(i, n) lef[sz(w[i])].push_back(w[i]);\n\trep(i, m) rig[sz(c[i])].push_back(c[i]);\n\n\n\tvector<vi> res;\n\tauto dfs = [&](auto dfs, vi &used, vi &f) -> void {\n\t\tif(sz(res) > 1) return;\n\t\tint mx = -1, nx = -1;\n\t\trep(i, m) {\n\t\t\tif(used[i]) continue;\n\t\t\tset<char> st;\n\t\t\trep(j, sz(c[i])) {\n\t\t\t\tif(f[c[i][j] - 'A'] == -1) {\n\t\t\t\t\tst.insert(c[i][j]);\n\t\t\t\t}\n\t\t\t}\n\t\t\tif(chmax(mx, sz(st))) {\n\t\t\t\tnx = i;\n\t\t\t}\n\t\t}\n\t\tif(nx == -1) {\n\t\t\tres.push_back(f);\n\t\t\treturn;\n\t\t}\n\t\tauto str = c[nx];\n\t\trep(j, sz(lef[sz(str)])) {\n\t\t\tif(sz(res) > 1) return;\n\t\t\tbool ok = 1;\n\t\t\tauto nf = f;\n\t\t\trep(k, sz(str)) {\n\t\t\t\tauto L = lef[sz(str)][j][k] - 'A';\n\t\t\t\tauto R = str[k] - 'A';\n\t\t\t\tif(nf[L] != -1 and nf[L] != R) {\n\t\t\t\t\tok = 0;\n\t\t\t\t\tbreak;\n\t\t\t\t}\n\t\t\t\tif(nf[R] != -1 and nf[R] != L) {\n\t\t\t\t\tok = 0;\n\t\t\t\t\tbreak;\n\t\t\t\t}\n\t\t\t\tnf[R] = L;\n\t\t\t\tnf[L] = R;\n\t\t\t}\n\t\t\tif(ok) {\n\t\t\t\tauto nu = used;\n\t\t\t\tnu[nx] = 1;\n\t\t\t\tdfs(dfs, nu, nf);\n\t\t\t}\n\t\t}\n\t\treturn;\n\t};\n\n\tvi f(26, -1), used(m);\n\n\tdfs(dfs, used, f);\n\n\tif(sz(res) > 1) {\n\t\tcout << \"-.\\n\";\n\t\treturn;\n\t}\n\tf = res[0];\n\trep(i, m) {\n\t\trep(j, sz(c[i])) {\n\t\t\tauto C = c[i][j] - 'A';\n\t\t\tc[i][j] = 'A' + f[C];\n\t\t}\n\t\tcout << c[i];\n\t\tif(i != m - 1)\n\t\t\tcout << ' ';\n\t\telse\n\t\t\tcout << \".\\n\";\n\t}\n}\n\nint main() {\n\tint n;\n\twhile(cin >> n, n) solve(n);\n}", "accuracy": 1, "time_ms": 7740, "memory_kb": 3320, "score_of_the_acc": -1.3226, "final_rank": 19 }, { "submission_id": "aoj_1317_7221184", "code_snippet": "#ifdef LOCAL\n#define _GLIBCXX_DEBUG\n#endif\n#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\nusing vi = vector<int>;\ntemplate <class T = int>\nusing V = vector<T>;\n\n\n#define repname(a, b, c, d, ...) d\n#define rep(...) repname(__VA_ARGS__, REP2, REP1, REP0)(__VA_ARGS__)\n#define REP2(i, l, r) for(int i = (l); i < (r); i++)\n#define REP1(i, x) REP2(i, 0, x)\n#define REP0(x) REP1(SPJ, x)\n#define all(v) (v).begin(), (v).end()\n#define sz(v) (int)(v).size()\n\nconstexpr int inf = 1e9;\nconstexpr ll INF = 1e18;\n\ntemplate <class T>\nbool chmin(T &a, const T b) {\n\treturn a > b ? a = b, 1 : 0;\n}\n\ntemplate <class T>\nbool chmax(T &a, const T b) {\n\treturn a < b ? a = b, 1 : 0;\n}\n\nvoid solve(int n) {\n\tvector<string> w(n), c;\n\trep(i, n) {\n\t\tcin >> w[i];\n\t}\n\twhile(1) {\n\t\tstring s;\n\t\tcin >> s;\n\t\tc.push_back(s);\n\t\tif(c.back().back() == '.') {\n\t\t\tc.back().pop_back();\n\t\t\tbreak;\n\t\t}\n\t}\n\tint m = sz(c);\n\tvector<vector<string>> lef(21), rig(21);\n\trep(i, n) lef[sz(w[i])].push_back(w[i]);\n\trep(i, m) rig[sz(c[i])].push_back(c[i]);\n\n\n\tvector<vi> res;\n\tauto dfs = [&](auto dfs, vi &used, vi &f) -> void {\n\t\tif(sz(res) > 1) return;\n\t\tint mx = -1, nx = -1;\n\t\trep(i, m) {\n\t\t\tif(used[i]) continue;\n\t\t\tset<char> st;\n\t\t\trep(j, sz(c[i])) {\n\t\t\t\tif(f[c[i][j] - 'A'] == -1) {\n\t\t\t\t\tst.insert(c[i][j]);\n\t\t\t\t}\n\t\t\t}\n\t\t\tif(chmax(mx, sz(st))) {\n\t\t\t\tnx = i;\n\t\t\t}\n\t\t}\n\t\tif(nx == -1) {\n\t\t\tres.push_back(f);\n\t\t\treturn;\n\t\t}\n\t\tauto str = c[nx];\n\t\trep(j, sz(lef[sz(str)])) {\n\t\t\tif(sz(res) > 1) return;\n\t\t\tbool ok = 1;\n\t\t\tauto nf = f;\n\t\t\trep(k, sz(str)) {\n\t\t\t\tauto L = lef[sz(str)][j][k] - 'A';\n\t\t\t\tauto R = str[k] - 'A';\n\t\t\t\tif(nf[L] != -1 and nf[L] != R) {\n\t\t\t\t\tok = 0;\n\t\t\t\t\tbreak;\n\t\t\t\t}\n\t\t\t\tif(nf[R] != -1 and nf[R] != L) {\n\t\t\t\t\tok = 0;\n\t\t\t\t\tbreak;\n\t\t\t\t}\n\t\t\t\tnf[R] = L;\n\t\t\t\tnf[L] = R;\n\t\t\t}\n\t\t\tif(ok) {\n\t\t\t\tauto nu = used;\n\t\t\t\tnu[nx] = 1;\n\t\t\t\tdfs(dfs, nu, nf);\n\t\t\t}\n\t\t}\n\t\treturn;\n\t};\n\n\tvi f(26, -1), used(m);\n\n\tdfs(dfs, used, f);\n\n\tif(sz(res) > 1) {\n\t\tcout << \"-.\\n\";\n\t\treturn;\n\t}\n\tf = res[0];\n\trep(i, m) {\n\t\trep(j, sz(c[i])) {\n\t\t\tauto C = c[i][j] - 'A';\n\t\t\tc[i][j] = 'A' + f[C];\n\t\t}\n\t\tcout << c[i];\n\t\tif(i != m - 1)\n\t\t\tcout << ' ';\n\t\telse\n\t\t\tcout << \".\\n\";\n\t}\n}\n\nint main() {\n\tint n;\n\twhile(cin >> n, n) solve(n);\n}", "accuracy": 1, "time_ms": 7700, "memory_kb": 3392, "score_of_the_acc": -1.3276, "final_rank": 20 }, { "submission_id": "aoj_1317_6774206", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\ntypedef long long ll;\ntemplate<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return true; } return false; }\ntemplate<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return true; } return false; }\n#define all(x) (x).begin(),(x).end()\n#define fi first\n#define se second\n#define mp make_pair\n#define si(x) int(x.size())\nconst int mod=998244353,MAX=65;\nconst ll INF=1LL<<62;\n\nvector<string> S,T,use;\nvector<int> syuS,syuT;\nvector<int> pos[MAX];\nvector<string> now,ans;\nmap<char,char> dic;\nbool two=false;\nint rem;\n\nvoid solve(int u){\n if(two) return;\n \n if(si(S)-u<rem) return;\n \n if(u==si(S)){\n for(auto a:now){\n if(si(a)==0) return;\n }\n if(si(ans)){\n two=true;\n }else{\n ans=now;\n }\n \n return;\n }\n \n for(int i=0;i<si(use);i++){\n if(syuS[u]<syuT[i]&&si(now[pos[i][0]])==0) return;\n }\n \n for(int i=0;i<si(use);i++){\n if(si(S[u])!=si(use[i])) continue;\n if(syuS[u]!=syuT[i]) continue;\n if(si(now[pos[i][0]])) continue;\n map<char,char> MA;\n bool ok=true;\n for(int j=0;j<si(S[u]);j++){\n if(MA.count(S[u][j])){\n if(MA[S[u][j]]!=use[i][j]) ok=false;\n }\n if(dic.count(S[u][j])){\n if(dic[S[u][j]]!=use[i][j]) ok=false;\n }else{\n MA[S[u][j]]=use[i][j];\n }\n \n if(MA.count(use[i][j])){\n if(MA[use[i][j]]!=S[u][j]) ok=false;\n }\n if(dic.count(use[i][j])){\n if(dic[use[i][j]]!=S[u][j]) ok=false;\n }else{\n MA[use[i][j]]=S[u][j];\n }\n \n if(!ok) break;\n }\n if(!ok) continue;\n \n //cout<<u<<\" \"<<i<<endl;\n \n for(auto x:MA){\n assert(!dic.count(x.fi));\n dic[x.fi]=x.se;\n //cout<<x.fi<<\" \"<<x.se<<endl;\n }\n \n for(int k:pos[i]){\n now[k]=S[u];\n }\n rem--;\n solve(u+1);\n rem++;\n for(int k:pos[i]){\n now[k]=\"\";\n }\n \n for(auto x:MA){\n dic.erase(x.fi);\n }\n }\n \n solve(u+1);\n}\n\nint main(){\n \n std::ifstream in(\"text.txt\");\n std::cin.rdbuf(in.rdbuf());\n cin.tie(0);\n ios::sync_with_stdio(false);\n \n while(1){\n int N;cin>>N;\n if(N==0) break;\n S.clear();S.resize(N);\n for(int i=0;i<N;i++) cin>>S[i];\n cin.ignore();\n string U;\n getline(cin,U);\n T.clear();T.push_back(\"\");\n for(char c:U){\n if(c==' '){\n T.push_back(\"\");\n }else if('A'<=c&&c<='Z'){\n T.back()+=c;\n }\n }\n \n use=T;\n sort(all(use));\n use.erase(unique(all(use)),use.end());\n \n for(int i=0;i<MAX;i++) pos[i].clear();\n for(int i=0;i<si(T);i++){\n for(int j=0;j<si(use);j++){\n if(T[i]==use[j]) pos[j].push_back(i);\n }\n }\n \n rem=si(use);\n \n sort(all(S),[](auto a,auto b){\n set<char> c1,c2;\n for(char c:a) c1.insert(c);\n for(char c:b) c2.insert(c);\n return si(c1)>si(c2);\n });\n \n two=false;\n ans.clear();\n now.clear();now.resize(si(T));\n dic.clear();\n \n syuS.clear();\n syuT.clear();\n for(int i=0;i<si(S);i++){\n set<char> c1;\n for(char c:S[i]) c1.insert(c);\n syuS.push_back(si(c1));\n }\n for(int i=0;i<si(use);i++){\n set<char> c1;\n for(char c:use[i]) c1.insert(c);\n syuT.push_back(si(c1));\n }\n \n solve(0);\n \n if(two) cout<<\"-.\\n\";\n else{\n for(int i=0;i<si(ans);i++){\n if(i) cout<<\" \";\n cout<<ans[i];\n }\n cout<<\".\\n\";\n }\n \n /*\n for(auto a:S) cout<<a<<\" \";\n cout<<endl;\n for(auto a:use) cout<<a<<\" \";\n cout<<endl;\n for(auto a:T) cout<<a<<\" \";\n cout<<endl;\n for(int a:syuS) cout<<a<<\" \";\n cout<<endl;\n for(int a:syuT) cout<<a<<\" \";\n cout<<endl;\n for(int i=0;i<si(use);i++){\n for(int a:pos[i]) cout<<a<<\" \";\n cout<<endl;\n }\n */\n }\n}", "accuracy": 1, "time_ms": 150, "memory_kb": 3332, "score_of_the_acc": -0.3424, "final_rank": 14 }, { "submission_id": "aoj_1317_6390650", "code_snippet": "#include<iostream>\n#include<cstdio>\n#include<cstring>\n#include<string>\n#include<vector>\n#include<cmath>\n#include<algorithm>\n#include<map>\n#include<queue>\n#include<deque>\n#include<iomanip>\n#include<tuple>\n#include<cassert>\n#include<set>\n#include<complex>\n#include<numeric>\n#include<functional>\n#include<unordered_map>\n#include<unordered_set>\nusing namespace std;\ntypedef long long int LL;\ntypedef pair<int,int> P;\ntypedef pair<LL,LL> LP;\nconst int INF=1<<30;\nconst LL MAX=1e9+7;\n\nvoid array_show(int *array,int array_n,char middle=' '){\n for(int i=0;i<array_n;i++)printf(\"%d%c\",array[i],(i!=array_n-1?middle:'\\n'));\n}\nvoid array_show(LL *array,int array_n,char middle=' '){\n for(int i=0;i<array_n;i++)printf(\"%lld%c\",array[i],(i!=array_n-1?middle:'\\n'));\n}\nvoid array_show(vector<int> &vec_s,int vec_n=-1,char middle=' '){\n if(vec_n==-1)vec_n=vec_s.size();\n for(int i=0;i<vec_n;i++)printf(\"%d%c\",vec_s[i],(i!=vec_n-1?middle:'\\n'));\n}\nvoid array_show(vector<LL> &vec_s,int vec_n=-1,char middle=' '){\n if(vec_n==-1)vec_n=vec_s.size();\n for(int i=0;i<vec_n;i++)printf(\"%lld%c\",vec_s[i],(i!=vec_n-1?middle:'\\n'));\n}\ntemplate<typename T> ostream& operator<<(ostream& os,const vector<T>& v1){\n int n=v1.size();\n for(int i=0;i<n;i++){\n if(i)os<<\" \";\n os<<v1[i];\n }\n return os;\n}\ntemplate<typename T1,typename T2> ostream& operator<<(ostream& os,const pair<T1,T2>& p){\n os<<p.first<<\" \"<<p.second;\n return os;\n}\ntemplate<typename T> istream& operator>>(istream& is,vector<T>& v1){\n int n=v1.size();\n for(int i=0;i<n;i++)is>>v1[i];\n return is;\n}\ntemplate<typename T1,typename T2> istream& operator>>(istream& is,pair<T1,T2>& p){\n is>>p.first>>p.second;\n return is;\n}\n\nnamespace sol{\n const int N=26;\n vector<int> vt;\n vector<string> v1,v2;\n vector<char> used;\n vector<int> used2;\n\n bool check(string sa,string sb,int p){\n if(sa.size()!=sb.size())return false;\n for(int i=0;i<sa.size();i++){\n int a=sa[i]-'A',b=sb[i]-'A';\n if(vt[a]==-1 && vt[b]==-1){\n vt[a]=b,vt[b]=a;\n used2[a]=p,used2[b]=p;\n }\n if(vt[a]!=b || vt[b]!=a)return false;\n }\n return true;\n }\n\n void check_rev(int p){\n for(int i=0;i<N;i++){\n if(used2[i]==p)used2[i]=-1,vt[i]=-1;\n }\n }\n\n int dfs(int pos,int max_cnt){\n if(pos==v2.size())return 1;\n int s=0;\n for(int i=0;i<v1.size();i++){\n if(used[i])continue;\n if(!check(v1[i],v2[pos],pos)){\n check_rev(pos);\n continue;\n }\n s+=dfs(pos+1,max_cnt);\n if(s>=max_cnt)return s;\n check_rev(pos);\n }\n return s;\n }\n\n void solve(){\n int n,m;\n int i,j,k;\n int a,b,c;\n while(cin>>n){\n if(n==0)return;\n v1.resize(n);\n cin>>v1;\n string sa,sb;\n vector<int> va;\n vector<string> vs;\n bool flag=false;\n v2.clear();\n while(cin>>sa){\n if(sa.back()=='.'){\n sa.pop_back();\n flag=true;\n }\n vs.push_back(sa);\n for(j=0;j<v2.size();j++){\n if(v2[j]==sa){\n break;\n }\n }\n if(j==v2.size())v2.push_back(sa);\n if(flag)break;\n }\n auto cnt=[](string s1){\n vector<char> v(N,0);\n int s=0;\n for(int i=0;i<s1.size();i++){\n int a=s1[i]-'A';\n if(!v[a])v[a]=1,s++;\n }\n return s;\n };\n sort(v2.begin(),v2.end(),[&](string s1,string s2){\n return cnt(s1)>cnt(s2);\n });\n used.assign(v1.size(),0);\n used2.assign(N,-1);\n vt.assign(N,-1);\n a=dfs(0,2);\n if(a>=2){\n cout<<\"-.\"<<endl;\n continue;\n }\n dfs(0,1);\n for(auto& s:vs){\n for(auto& ca:s){\n a=ca-'A';\n ca='A'+vt[a];\n }\n }\n cout<<vs<<\".\"<<endl;\n }\n }\n}\n\nint main(){\n cin.tie(0);\n ios::sync_with_stdio(false);\n sol::solve();\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3392, "score_of_the_acc": -0.3328, "final_rank": 13 }, { "submission_id": "aoj_1317_5452988", "code_snippet": "#pragma GCC optimize(\"O3\")\n#pragma GCC optimize(\"unroll-loops\")\n#include<iostream>\n#include<string>\n#include<cstdio>\n#include<vector>\n#include<cmath>\n#include<algorithm>\n#include<functional>\n#include<iomanip>\n#include<queue>\n#include<ciso646>\n#include<random>\n#include<map>\n#include<set>\n#include<bitset>\n#include<stack>\n#include<unordered_map>\n#include<unordered_set>\n#include<utility>\n#include<cassert>\n#include<complex>\n#include<numeric>\n#include<array>\nusing namespace std;\n\n//#define int long long\ntypedef long long ll;\n\ntypedef unsigned long long ul;\ntypedef unsigned int ui;\nconstexpr ll mod = 1000000007;\nconst ll INF = mod * mod;\ntypedef pair<int, int>P;\n#define stop char nyaa;cin>>nyaa;\n#define rep(i,n) for(int i=0;i<n;i++)\n#define per(i,n) for(int i=n-1;i>=0;i--)\n#define Rep(i,sta,n) for(int i=sta;i<n;i++)\n#define rep1(i,n) for(int i=1;i<=n;i++)\n#define per1(i,n) for(int i=n;i>=1;i--)\n#define Rep1(i,sta,n) for(int i=sta;i<=n;i++)\n#define all(v) (v).begin(),(v).end()\ntypedef pair<ll, ll> LP;\ntypedef long double ld;\ntypedef pair<ld, ld> LDP;\nconst ld eps = 1e-12;\nconst ld pi = acosl(-1.0);\n\nll mod_pow(ll x, ll n, ll m = mod) {\n\tif (n < 0) {\n\t\tll res = mod_pow(x, -n, m);\n\t\treturn mod_pow(res, m - 2, m);\n\t}\n\tif (abs(x) >= m)x %= m;\n\tif (x < 0)x += m;\n\tll res = 1;\n\twhile (n) {\n\t\tif (n & 1)res = res * x % m;\n\t\tx = x * x % m; n >>= 1;\n\t}\n\treturn res;\n}\nstruct modint {\n\tll n;\n\tmodint() :n(0) { ; }\n\tmodint(ll m) :n(m) {\n\t\tif (n >= mod)n %= mod;\n\t\telse if (n < 0)n = (n % mod + mod) % mod;\n\t}\n\toperator int() { return n; }\n};\nbool operator==(modint a, modint b) { return a.n == b.n; }\nmodint operator+=(modint& a, modint b) { a.n += b.n; if (a.n >= mod)a.n -= mod; return a; }\nmodint operator-=(modint& a, modint b) { a.n -= b.n; if (a.n < 0)a.n += mod; return a; }\nmodint operator*=(modint& a, modint b) { a.n = ((ll)a.n * b.n) % mod; return a; }\nmodint operator+(modint a, modint b) { return a += b; }\nmodint operator-(modint a, modint b) { return a -= b; }\nmodint operator*(modint a, modint b) { return a *= b; }\nmodint operator^(modint a, ll n) {\n\tif (n == 0)return modint(1);\n\tmodint res = (a * a) ^ (n / 2);\n\tif (n % 2)res = res * a;\n\treturn res;\n}\n\nll inv(ll a, ll p) {\n\treturn (a == 1 ? 1 : (1 - p * inv(p % a, a)) / a + p);\n}\nmodint operator/(modint a, modint b) { return a * modint(inv(b, mod)); }\nmodint operator/=(modint& a, modint b) { a = a / b; return a; }\nconst int max_n = 1 << 1;\nmodint fact[max_n], factinv[max_n];\nvoid init_f() {\n\tfact[0] = modint(1);\n\tfor (int i = 0; i < max_n - 1; i++) {\n\t\tfact[i + 1] = fact[i] * modint(i + 1);\n\t}\n\tfactinv[max_n - 1] = modint(1) / fact[max_n - 1];\n\tfor (int i = max_n - 2; i >= 0; i--) {\n\t\tfactinv[i] = factinv[i + 1] * modint(i + 1);\n\t}\n}\nmodint comb(int a, int b) {\n\tif (a < 0 || b < 0 || a < b)return 0;\n\treturn fact[a] * factinv[b] * factinv[a - b];\n}\nmodint combP(int a, int b) {\n\tif (a < 0 || b < 0 || a < b)return 0;\n\treturn fact[a] * factinv[a - b];\n}\n\nint n;\nvoid solve() {\n\tvector<string> s(n);\n\trep(i, n)cin >> s[i];\n\tvector<string> t;\n\twhile (true) {\n\t\tstring x; cin >> x;\n\t\tif (x.back() == '.') {\n\t\t\tx.pop_back();\n\t\t\tt.push_back(x); break;\n\t\t}\n\t\tt.push_back(x);\n\t}\n\tauto calc = [&](int i, int j)->vector<int> {\n\t\tvector<int> res(26, -1);\n\t\trep(c, s[j].size()) {\n\t\t\tres[s[j][c] - 'A'] = t[i][c] - 'A';\n\t\t\tres[t[i][c] - 'A'] = s[j][c] - 'A';\n\t\t}\n\t\treturn res;\n\t};\n\tint m = t.size();\n\tvector<vector<vector<int>>> b;\n\tb.resize(m);\n\trep(i, m) {\n\t\tb[i].resize(n);\n\t\trep(j, n) {\n\t\t\tb[i][j].resize(m);\n\t\t\tif (s[j].size() != t[i].size())continue;\n\t\t\trep(k, m) {\n\t\t\t\trep(l, n) {\n\t\t\t\t\tbool valid = true;\n\t\t\t\t\tif (t[k].size() != s[l].size())valid = false;\n\t\t\t\t\telse {\n\t\t\t\t\t\tvector<int> vl = calc(i, j);\n\t\t\t\t\t\tvector<int> vr = calc(k, l);\n\t\t\t\t\t\trep(c, 26) {\n\t\t\t\t\t\t\tif (vl[c] >= 0 && vr[c] >= 0 && vl[c] != vr[c])valid = false;\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t\tif (valid)b[i][j][k] |= (1 << l);\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n\tvector<int> ori(m);\n\trep(i, m)rep(j, n) {\n\t\tbool valid = true;\n\t\tif (t[i].size() != s[j].size())valid = false;\n\t\telse {\n\t\t\tint alf[26]; rep(c, 26)alf[c] = -1;\n\t\t\trep(c, t[i].size()) {\n\t\t\t\tint a = s[j][c] - 'A';\n\t\t\t\tint b = t[i][c] - 'A';\n\t\t\t\tif (alf[a] >= 0 && alf[a] != b)valid = false;\n\t\t\t\tif (alf[b] >= 0 && alf[b] != a)valid = false;\n\t\t\t\talf[a] = b; alf[b] = a;\n\t\t\t}\n\t\t}\n\t\tif (valid)ori[i] |= (1 << j);\n\t}\n\tvector<int> cur;\n\tstring ans;\n\tfunction<bool(vector<int>, int)> dfs = [&](vector<int> vs, int dep)->bool {\n\t\tif (dep == m) {\n\t\t\tif (ans.empty()) {\n\t\t\t\trep(i, m) {\n\t\t\t\t\tif (i > 0)ans.push_back(' ');\n\t\t\t\t\tans += s[cur[i]];\n\t\t\t\t\tif (i == m - 1)ans.push_back('.');\n\t\t\t\t}\n\t\t\t\treturn false;\n\t\t\t}\n\t\t\telse {\n\t\t\t\tans = \"-.\";\n\t\t\t\treturn true;\n\t\t\t}\n\t\t}\n\t\telse {\n\t\t\trep(i, n) {\n\t\t\t\tif (vs[dep] & (1 << i)) {\n\t\t\t\t\tvector<int> cop = vs;\n\t\t\t\t\tbool valid = true;\n\t\t\t\t\tfor (int j = dep + 1; j < m; j++) {\n\t\t\t\t\t\tcop[j] &= b[dep][i][j];\n\t\t\t\t\t\tif (cop[j] == 0)valid = false;\n\t\t\t\t\t}\n\t\t\t\t\tif (valid) {\n\t\t\t\t\t\tcur.push_back(i);\n\t\t\t\t\t\tif (dfs(cop, dep + 1))return true;\n\t\t\t\t\t\tcur.pop_back();\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\treturn false;\n\t};\n\tdfs(ori, 0);\n\t//cout << \"ans is \";\n\tcout << ans << \"\\n\";\n}\n\n\nsigned main() {\n\tios::sync_with_stdio(false);\n\tcin.tie(0);\n\t//cout << fixed << setprecision(12);\n\t//init_f();\n\t//init();\n\t//expr();\n\t//int t; cin >> t;rep(i,t)\n\twhile (cin >> n, n) {\n\t\tsolve();\n\t}\n\treturn 0;\n}", "accuracy": 1, "time_ms": 220, "memory_kb": 3632, "score_of_the_acc": -0.3939, "final_rank": 15 }, { "submission_id": "aoj_1317_5068086", "code_snippet": "#include<iostream>\n#include<string>\n#include<set>\n#include<algorithm>\nusing namespace std;\n\nint n, m;\nstring words[20], words2[40], words3[40], ans;\nbool same[40], selected[20];\nchar r[26];\n\nint dfs(int x) {\n if (x == m) {\n ans = \"\";\n for (int i = 0; i < m; i++) {\n for (char c : words3[i]) ans += r[c-'A'];\n if (i == m-1) ans += \".\";\n else ans += \" \";\n }\n return 1;\n }\n if (same[x]) return dfs(x+1);\n char copy[26];\n for (int i = 0; i < 26; i++) copy[i] = r[i];\n int counter = 0;\n for (int i = 0; i < n; i++) if (words[i].length() == words2[x].length()) {\n bool ok = true;\n for (int j = 0; j < words[i].length(); j++) {\n if (r[words[i][j]-'A'] != 0 && r[words[i][j]-'A'] != words2[x][j]) {\n ok = false;\n break;\n }\n if (r[words2[x][j]-'A'] != 0 && r[words2[x][j]-'A'] != words[i][j]) {\n ok = false;\n break;\n }\n r[words[i][j]-'A'] = words2[x][j];\n r[words2[x][j]-'A'] = words[i][j];\n }\n if (ok) {\n selected[i] = true;\n if (x >= 2) {\n for (int p = x+1; p < m; p++) {\n bool ok2 = true;\n for (int k = 0; k < n; k++) if (!selected[k] && words[k].length() == words2[p].length()) {\n bool tmp = true;\n for (int j = 0; j < words[k].length(); j++) {\n if (r[words[k][j]-'A'] != 0 && r[words[k][j]-'A'] != words2[p][j]) {\n tmp = false;\n break;\n }\n if (r[words2[p][j]-'A'] != 0 && r[words2[p][j]-'A'] != words[k][j]) {\n tmp = false;\n break;\n }\n }\n if (tmp) {\n ok2 = true;\n break;\n }\n }\n if (!ok2) {\n ok = false;\n break;\n }\n }\n }\n counter += dfs(x+1);\n if (counter >= 2) break;\n selected[i] = false;\n }\n for (int j = 0; j < 26; j++) r[j] = copy[j];\n }\n for (int j = 0; j < 26; j++) r[j] = copy[j];\n return counter;\n}\n\nbool comp(const string& a, const string& b) {\n set<char> as, bs;\n for (char x : a) as.insert(x);\n for (char x : b) bs.insert(x);\n return as.size() > bs.size();\n}\n\nint main() {\n while (cin >> n, n) {\n for (int i = 0; i < n; i++) cin >> words[i];\n for (m = 0;; m++) {\n cin >> words2[m];\n if (words2[m][words2[m].length() - 1] == '.') {\n words2[m] = words2[m].substr(0, words2[m].length() - 1);\n m++;\n break;\n }\n }\n for (int i = 0; i < m; i++) words3[i] = words2[i];\n sort(words2, words2 + m, comp);\n for (int i = 0; i < m; i++) {\n same[i] = false;\n for (int j = 0; j < i; j++) if (words2[i] == words2[j]) {\n same[i] = true;\n break;\n }\n }\n if (dfs(0) >= 2) cout << \"-.\" << endl;\n else cout << ans << endl;\n }\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3000, "score_of_the_acc": -0.2772, "final_rank": 7 }, { "submission_id": "aoj_1317_3655967", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\nint main() {\n int n;\n while(cin >> n, n) {\n vector<string> ws(n);\n for(auto& w : ws) cin >> w;\n vector<string> ss = {\"\"};\n while(cin >> ss.back()) {\n if(ss.back().back() == '.') break;\n ss.push_back(\"\");\n }\n ss.back().pop_back(); // period\n const int ss_sz = [&] () { // unique\n set<string> sss(ss.begin(), ss.end());\n return sss.size();\n }();\n\n // long string has strong constraints\n sort(begin(ws), end(ws), [] (string s1, string s2) {\n set<char> ss1(begin(s1), end(s1));\n set<char> ss2(begin(s2), end(s2));\n return make_pair(s1.size(), ss1.size()) > make_pair(s2.size(), ss2.size());\n });\n\n vector<char> m(26, '*'); // map\n vector<int> mcnt(26);\n auto conv = [&] (string s) {\n for(auto& c : s) {\n c = m[c - 'A'];\n }\n return s;\n };\n string ans;\n function<void(int, int)> dfs = [&] (int i, int remain) {\n if(ans == \"-.\") return;\n if(i == n) {\n string tans = conv(ss[0]);\n for(int j = 1; j < (int)ss.size(); ++j) {\n tans += \" \" + conv(ss[j]);\n }\n tans += \".\";\n if(tans.find('*') != string::npos) {\n return;\n } else if(!ans.empty() && ans != tans) {\n ans = \"-.\";\n } else {\n ans = tans;\n }\n return;\n }\n if(n - i - 1 >= remain) {\n dfs(i + 1, remain); // no use\n }\n const auto& w = ws[i];\n for(auto const& s : ss) {\n if(s.size() != w.size()) continue;\n int it = 0;\n for(; it < (int)w.size(); ++it) {\n if((m[s[it] - 'A'] == w[it] && m[w[it] - 'A'] == s[it]) || (m[s[it] - 'A'] == '*' && m[w[it] - 'A'] == '*')) {\n m[s[it] - 'A'] = w[it];\n m[w[it] - 'A'] = s[it];\n ++mcnt[s[it] - 'A'];\n ++mcnt[w[it] - 'A'];\n } else {\n break;\n }\n }\n if(it == (int)w.size()) { // ok\n dfs(i + 1, remain - 1);\n }\n for(int j = 0; j < it; ++j) { // restore\n --mcnt[s[j] - 'A'];\n --mcnt[w[j] - 'A'];\n if(mcnt[s[j] - 'A'] == 0) {\n m[s[j] - 'A'] = m[w[j] - 'A'] = '*';\n }\n }\n }\n };\n dfs(0, ss_sz);\n\n cout << ans << endl;\n }\n}", "accuracy": 1, "time_ms": 1010, "memory_kb": 3228, "score_of_the_acc": -0.4389, "final_rank": 16 }, { "submission_id": "aoj_1317_3158931", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\ntypedef long long ll;\ntypedef unsigned long long ull;\ntypedef pair<char, char> P;\n\n#define fi first\n#define se second\n#define repl(i,a,b) for(ll i=(ll)(a);i<(ll)(b);i++)\n#define rep(i,n) repl(i,0,n)\n#define all(x) (x).begin(),(x).end()\n#define dbg(x) cout<<#x\"=\"<<x<<endl\n#define mmax(x,y) (x>y?x:y)\n#define mmin(x,y) (x<y?x:y)\n#define maxch(x,y) x=mmax(x,y)\n#define minch(x,y) x=mmin(x,y)\n#define uni(x) x.erase(unique(all(x)),x.end())\n#define exist(x,y) (find(all(x),y)!=x.end())\n#define bcnt __builtin_popcountll\n\n#define INF 1e16\n#define mod 1000000007\n\nint n,m;\nstring w[20];\nvector<string> s;\nvector<int> ord;\nint res=0;\nset<P> role[2];\nbool used[26];\nint ccnt[100];\n\nvoid dfs(int i){\n if(i==m){\n res++;\n if(res<2){\n role[res]=role[res-1];\n }\n return ;\n }\n int j=ord[i];\n rep(k,n){\n if(w[k].size()>s[j].size())break;\n if(s[j].size()!=w[k].size())continue;\n bool ok=true;\n vector<P> add;\n rep(l,s[j].size()){\n char c1=s[j][l],c2=w[k][l];\n if(used[c1-'A']!=used[c2-'A']){\n ok=false; break;\n }\n if(used[c1-'A']&&used[c2-'A']&&role[res].find(P(c1,c2))==role[res].end()){\n ok=false; break;\n }\n if(role[res].find(P(c1,c2))==role[res].end()){\n role[res].insert(P(c1,c2)); role[res].insert(P(c2,c1));\n used[c1-'A']=used[c2-'A']=true;\n add.push_back(P(c1,c2));\n }\n }\n if(!ok){\n rep(l,add.size()){\n role[res].erase(P(add[l].fi,add[l].se));\n role[res].erase(P(add[l].se,add[l].fi));\n used[add[l].fi-'A']=false; used[add[l].se-'A']=false;\n }\n continue;\n }\n dfs(i+1);\n if(res==2)return;\n rep(l,add.size()){\n role[res].erase(P(add[l].fi,add[l].se));\n role[res].erase(P(add[l].se,add[l].fi));\n used[add[l].fi-'A']=false; used[add[l].se-'A']=false;\n }\n }\n}\n\nint main(){\n cin.tie(0);\n ios::sync_with_stdio(false);\n while(1){\n cin>>n;\n if(n==0)break;\n s.clear();\n m=0; ord.clear();\n rep(i,n)cin>>w[i];\n sort(w,w+n,[](const string& a,const string& b){ return a.size() < b.size(); });\n while(1){\n ord.push_back(m++);\n string tmp;\n cin>>tmp;\n if(tmp[tmp.size()-1]=='.'){\n s.push_back(tmp.substr(0,tmp.size()-1));\n break;\n }\n s.push_back(tmp);\n }\n rep(i,m){\n set<char> st;\n rep(j,s[i].size())st.insert(s[i][j]);\n ccnt[i]=st.size();\n }\n sort(all(ord),[=](const int& a,const int& b){ return ccnt[a] > ccnt[b]; });\n res=0;\n rep(i,2)role[i].clear();\n memset(used,0,sizeof(used));\n dfs(0);\n if(res==2)cout<<\"-.\"<<endl;\n else{\n string ans=s[0];\n repl(i,1,m)ans+=\" \"+s[i];\n ans+=\".\";\n rep(i,ans.size()){\n for(P p : role[0]){\n if(ans[i]==p.fi){\n ans[i]=p.se;\n break;\n }\n }\n }\n cout<<ans<<endl;\n }\n }\n return 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3176, "score_of_the_acc": -0.3022, "final_rank": 10 }, { "submission_id": "aoj_1317_2461570", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\n#define int long long\nint n;\nvector<string> w,v;\nvector<string> xyz[30];\nstring s;\nvector<char> vc,ans;\ntypedef pair<int,int> P;\nvector<P> vp;\nvector<int> used;\nint valid;\nvector<int> wc1,wc2;\nvoid dfs(){\n for(int i=0;i<(int)v.size();i++){\n string& t=v[i];\n bool flg=0;\n for(int j=0;j<(int)xyz[t.size()].size();j++){\n string& u=xyz[t.size()][j];\n bool ok=1;\n for(int k=0;k<(int)t.size();k++){\n if(!used[t[k]-'A']) continue;\n if(!used[u[k]-'A']){\n ok=0;\n break;\n }\n ok&=vc[t[k]-'A']==u[k]-'A';\n if(!ok) break;\n }\n //cout<<t<<\" \"<<u<<\":\"<<ok<<endl;\n if(ok){\n flg=1;\n break;\n }\n }\n if(!flg) return;\n }\n int cnt=0;\n for(int i=0;i<26;i++) if(used[vp[i].second]) cnt++;\n //cout<<cnt<<endl;\n //if(cnt>1) return;\n if(cnt==26){\n valid++;\n ans=vc;\n return;\n }\n int x=0;\n while(used[vp[x].second]) x++;\n //cout<<x<<endl;\n if(vp[x].first){\n for(int k=0;k<26;k++){\n if(used[vp[k].second]) continue;\n used[vp[x].second]=used[vp[k].second]=1;\n vc[vp[x].second]=vp[k].second;\n vc[vp[k].second]=vp[x].second;\n //cout<<x<<\" \"<<k<<endl;\n //cout<<(char)(vp[x].second+'A')<<\"<->\"<<(char)(vp[k].second+'A')<<endl;\n dfs();\n if(valid>1) return; \n used[vp[x].second]=used[vp[k].second]=0;\n vc[vp[x].second]=vc[vp[k].second]=0; \n }\n }else{\n used[vp[x].second]=1;\n vc[vp[x].second]=vp[x].second;\n dfs();\n used[vp[x].second]=0;\n vc[vp[x].second]=0;\n }\n}\nsigned main(){\n while(cin>>n,n){\n for(int i=0;i<30;i++) xyz[i].clear(); \n w.resize(n);\n for(int i=0;i<n;i++) cin>>w[i],xyz[w[i].size()].push_back(w[i]);\n cin.ignore();\n getline(cin,s);\n assert(s.back()=='.');\n s.pop_back();\n v.clear();\n for(int i=0;i<(int)s.size();i++){\n string t;\n while(i<(int)s.size()&&s[i]!=' ') t+=s[i++];\n //cout<<t<<endl;\n v.push_back(t);\n }\n vc.clear();\n vc.resize(26,0);\n wc2.clear();\n wc2.resize(26,0);\n for(int i=0;i<(int)v.size();i++)\n for(int j=0;j<(int)v[i].size();j++)\n wc2[v[i][j]-'A']++;\n vp.resize(26);\n for(int i=0;i<26;i++) vp[i]=P(wc2[i],i);\n sort(vp.rbegin(),vp.rend());\n used.clear();\n used.resize(26,0);\n valid=0;\n dfs();\n //cout<<valid<<endl;\n if(valid!=1){\n cout<<\"-.\"<<endl;\n continue;\n }\n //puts(\"found\");\n for(int i=0;i<(int)v.size();i++){\n if(i) cout<<\" \";\n for(int j=0;j<(int)v[i].size();j++){\n //cout<<v[i][j]-'A'<<endl;\n cout<<(char)(ans[v[i][j]-'A']+'A');\n }\n }\n cout<<\".\"<<endl;\n }\n return 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3164, "score_of_the_acc": -0.3005, "final_rank": 9 }, { "submission_id": "aoj_1317_2461536", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\n#define int long long\nint n;\nvector<string> w,v;\nvector<string> xyz[30];\nstring s;\nvector<char> vc,ans;\ntypedef pair<int,int> P;\nvector<P> vp;\nvector<int> used;\nint valid;\nvector<int> wc1,wc2;\nvoid dfs(){\n for(int i=0;i<(int)v.size();i++){\n string& t=v[i];\n bool flg=0;\n for(int j=0;j<(int)xyz[t.size()].size();j++){\n string& u=xyz[t.size()][j];\n bool ok=1;\n for(int k=0;k<(int)t.size();k++){\n\tif(!used[t[k]-'A']) continue;\n\tif(!used[u[k]-'A']){\n\t ok=0;\n\t break;\n\t}\n\tok&=vc[t[k]-'A']==u[k]-'A';\n\tif(!ok) break;\n }\n //cout<<t<<\" \"<<u<<\":\"<<ok<<endl;\n if(ok){\n\tflg=1;\n\tbreak;\n }\n }\n if(!flg) return;\n }\n int cnt=0;\n for(int i=0;i<26;i++) if(used[vp[i].second]) cnt++;\n //cout<<cnt<<endl;\n //if(cnt>1) return;\n if(cnt==26){\n valid++;\n ans=vc;\n return;\n }\n int x=0;\n while(used[vp[x].second]) x++;\n //cout<<x<<endl;\n if(vp[x].first){\n for(int k=0;k<26;k++){\n if(used[vp[k].second]) continue;\n used[vp[x].second]=used[vp[k].second]=1;\n vc[vp[x].second]=vp[k].second;\n vc[vp[k].second]=vp[x].second;\n //cout<<x<<\" \"<<k<<endl;\n //cout<<(char)(vp[x].second+'A')<<\"<->\"<<(char)(vp[k].second+'A')<<endl;\n dfs();\n if(valid>1) return; \n used[vp[x].second]=used[vp[k].second]=0;\n vc[vp[x].second]=vc[vp[k].second]=0; \n }\n }else{\n used[vp[x].second]=1;\n vc[vp[x].second]=vp[x].second;\n dfs();\n used[vp[x].second]=0;\n vc[vp[x].second]=0;\n }\n}\nsigned main(){\n while(cin>>n,n){\n for(int i=0;i<30;i++) xyz[i].clear(); \n w.resize(n);\n for(int i=0;i<n;i++) cin>>w[i],xyz[w[i].size()].push_back(w[i]);\n cin.ignore();\n getline(cin,s);\n assert(s.back()=='.');\n s.pop_back();\n v.clear();\n for(int i=0;i<(int)s.size();i++){\n string t;\n while(i<(int)s.size()&&s[i]!=' ') t+=s[i++];\n //cout<<t<<endl;\n v.push_back(t);\n }\n vc.clear();\n vc.resize(26,0);\n wc2.clear();\n wc2.resize(26,0);\n for(int i=0;i<(int)v.size();i++)\n for(int j=0;j<(int)v[i].size();j++)\n\twc2[v[i][j]-'A']++;\n vp.resize(26);\n for(int i=0;i<26;i++) vp[i]=P(wc2[i],i);\n sort(vp.rbegin(),vp.rend());\n used.clear();\n used.resize(26,0);\n valid=0;\n dfs();\n //cout<<valid<<endl;\n if(valid!=1){\n cout<<\"-.\"<<endl;\n continue;\n }\n //puts(\"found\");\n for(int i=0;i<(int)v.size();i++){\n if(i) cout<<\" \";\n for(int j=0;j<(int)v[i].size();j++){\n\t//cout<<v[i][j]-'A'<<endl;\n\tcout<<(char)(ans[v[i][j]-'A']+'A');\n }\n }\n cout<<\".\"<<endl;\n }\n return 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3180, "score_of_the_acc": -0.3027, "final_rank": 11 }, { "submission_id": "aoj_1317_2290534", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\n#define int long long\n\n#define rep(i, n) for(int i = 0; i < (int)(n); i++)\n\nint n;\nvector<string> word;\nvector<string> seq;\nmap<char, int> mp;\nmap<char, char> conv;\nvector< pair<int, char> > ord;\nstring ans;\nint num;\n\nbool check() {\n rep(i, seq.size()) {\n bool flag = false;\n rep(j, word.size()) {\n if(word[j].size() != seq[i].size()) continue;\n bool flag2 = true;\n rep(k, seq[i].size()) {\n\tif(conv[seq[i][k]] == '$') continue;\n\tif(conv[seq[i][k]] != word[j][k]) flag2 = false;\n }\n if(flag2) flag = true;\n }\n if(!flag) return false;\n }\n return true;\n}\n\nvoid dfs(int idx) {\n //for(auto p : conv) {\n //cout << p.first << \" \" << p.second << endl;\n //}\n //cout << check() << endl; \n if(!check()) return;\n if(idx == ord.size()) {\n string tmp = \"\";\n rep(i, seq.size()) {\n if(i) tmp += \" \";\n rep(j, seq[i].size()) {\n\ttmp += conv[seq[i][j]];\n }\n }\n ans = tmp;\n num++;\n return;\n }\n if(conv[ord[idx].second] != '$') {\n dfs(idx+1);\n return;\n }\n ///cout << ord[idx].second << endl;\n rep(i, 26) {\n if(conv[(char)('A'+i)] != '$') continue;\n conv[ord[idx].second] = (char)('A'+i);\n conv[(char)('A'+i)] = ord[idx].second;\n dfs(idx+1);\n if(num > 1) return;\n conv[ord[idx].second] = '$';\n conv[(char)('A'+i)] = '$';\n }\n}\n\nsigned main(){\n while(cin >> n, n) {\n word.clear();\n word.resize(n);\n rep(i, n) cin >> word[i];\n seq.clear();\n while(1) {\n string s;\n cin >> s;\n seq.push_back(s);\n if(s.back() == '.') {\n\tseq[seq.size()-1].pop_back();\n\tbreak;\n }\n }\n ord.clear();\n mp.clear();\n conv.clear();\n rep(i, seq.size()) rep(j, seq[i].size()) {\n if(seq[i][j] == '.') continue;\n mp[seq[i][j]]++;\n }\n for(auto p : mp) {\n ord.push_back(make_pair(p.second, p.first));\n }\n sort(ord.begin(), ord.end(), greater<pair<int, int> >());\n ans = \"-\"; \n rep(i, 26) conv[(char)('A'+i)] = '$';\n conv['.'] = '.';\n num = 0;\n dfs(0);\n //cout << num << endl;\n if(num != 1) ans = \"-\";\n cout << ans + \".\" << endl;\n }\n return 0;\n}", "accuracy": 1, "time_ms": 80, "memory_kb": 3196, "score_of_the_acc": -0.314, "final_rank": 12 }, { "submission_id": "aoj_1317_2123640", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\ntypedef pair<int,int> P;\ntypedef pair<int,string> PP;\n\nset<string> se;\nmap<string,int> m;\nvector<PP> a;\nvector<string> v;\nint ans[26],u[26],uu[100],n,c;\n\nvoid dfs(int k) {\n if(k==a.size()||c>1) {\n for(int i=0; i<26; i++) {\n if(u[i]==-1) continue;\n if(u[u[i]]!=i) return;\n }\n c++;\n for(int i=0; i<26; i++) ans[i]=u[i];\n return;\n }\n string r=a[k].second;\n for(int i=0; i<n; i++) {\n string t=v[i];\n if(uu[i]||r.size()!=t.size()) continue;\n for(int j=0; j<t.size(); j++) {\n if(u[t[j]-'A']!=-1&&u[t[j]-'A']!=r[j]-'A') goto next;\n if(u[r[j]-'A']!=-1&&u[r[j]-'A']!=t[j]-'A') goto next;\n }\n int u2[26];\n for(int j=0; j<26; j++) u2[j]=u[j];\n for(int j=0; j<t.size(); j++) {\n u[t[j]-'A']=r[j]-'A';\n u[r[j]-'A']=t[j]-'A';\n }\n uu[i]=1;\n dfs(k+1);\n uu[i]=0;\n for(int j=0; j<26; j++) u[j]=u2[j];\n next:;\n }\n}\nstring s;\n\nint main() {\n while(cin >> n && n) {\n se.clear();a.clear();m.clear();v.clear();\n for(int i=0; i<n; i++) {\n cin >> s;\n v.push_back(s);\n se.insert(s);\n }\n getline(cin,s);\n getline(cin,s);\n stringstream ss;\n ss << s;\n string r;\n while(ss>>r) {\n if(r[r.size()-1]=='.') r=r.substr(0,r.size()-1);\n m[r]++;\n }\n for(map<string,int>::iterator it=m.begin();it!=m.end();it++) {\n int d[26],cnt=0;memset(d,0,sizeof(d));\n r=it->first;\n for(int i=0; i<r.size(); i++) d[r[i]-'A']++;\n for(int i=0; i<26; i++) if(d[i]) cnt++;\n a.push_back(PP(cnt,it->first));\n }\n sort(a.begin(),a.end(),greater<PP>());\n memset(u,-1,sizeof(u));\n memset(uu,0,sizeof(uu));\n c=0;\n dfs(0);\n if(c==1) {\n for(int i=0; i<s.size(); i++) {\n if(isalpha(s[i])) cout << (char)(ans[s[i]-'A']+'A');\n else cout << s[i];\n }\n cout << endl;\n } else cout << \"-.\" << endl;\n }\n return 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3144, "score_of_the_acc": -0.2976, "final_rank": 8 }, { "submission_id": "aoj_1317_1563938", "code_snippet": "#include<iostream>\n#include<string>\n#include<vector>\n#include<utility>\n#include<algorithm>\n\nusing namespace std;\n\nint ord(char c){\n return c-'A';\n}\n\nvector<pair<char,char> > rec(vector<string> seq,vector<string> word,vector<pair<char,char> > rules,int u){\n if(seq.empty())return rules;\n int mx=-1,x;\n for(int i=0;i<seq.size();i++){\n int t=u;\n int cu=0;\n for(auto e:seq[i]){\n int xx=e-'A';\n if(!(t>>xx&1)){\n\tcu++;\n\tt|=1<<xx;\n }\n }\n if(cu>mx){\n mx=cu;\n x=i;\n }\n }\n vector<pair<char,char> > res;\n for(int j=0;j<word.size();j++){\n auto e=word[j];\n if(seq[x].size()!=e.size())continue;\n vector<pair<char,char> > nr=rules;\n int nu=u;\n for(int i=0;i<e.size();i++){\n int sr=nu>>ord(seq[x][i])&1;\n int wr=nu>>ord(e[i])&1;\n if(sr^wr)goto next;\n if(!sr){\n\tnu|=1<<ord(seq[x][i])|1<<ord(e[i]);\n\tchar l=seq[x][i],h=e[i];\n\tif(h<l){\n\t swap(l,h);\n\t}\n\tnr.emplace_back(l,h);\n }else{\n\tchar l=min(seq[x][i],e[i]);\n\tchar h=max(seq[x][i],e[i]);\n\tif(find(begin(nr),end(nr),make_pair(l,h))==end(nr))goto next;\n }\n }\n {\n vector<string> nseq(begin(seq),begin(seq)+x),nword(begin(word),begin(word)+j);\n nseq.insert(end(nseq),begin(seq)+x+1,end(seq));\n nword.insert(end(nword),begin(word)+j+1,end(word));\n auto r=rec(nseq,nword,nr,nu);\n if(!res.empty()&&!r.empty())throw 0;\n if(!r.empty()){\n\tres=r;\n }\n }\n next:\n ;\n }\n return res;\n}\n\nint main(){\n for(int n;cin>>n,n;){\n vector<string> word(n);\n for(int i=0;i<n;i++){\n cin>>word[i];\n }\n vector<string> seq;\n for(string s;;){\n cin>>s;\n if(s.back()!='.'){\n\tseq.push_back(s);\n }else{\n\tseq.push_back(s.substr(0,s.size()-1));\n\tbreak;\n }\n }\n vector<string> useq=seq,uword=word;\n sort(begin(useq),end(useq));\n sort(begin(uword),end(uword));\n useq.erase(unique(begin(useq),end(useq)),end(useq));\n uword.erase(unique(begin(uword),end(uword)),end(uword));\n try{\n auto r=rec(useq,word,{},0);\n char table[256];\n for(auto e:r){\n\ttable[e.first]=e.second;\n\ttable[e.second]=e.first;\n }\n bool ns=false;\n for(auto e:seq){\n\tif(ns++){\n\t cout<<' ';\n\t}\n\tfor(auto f:e){\n\t cout<<table[f];\n\t}\n }\n cout<<'.'<<endl;\n }catch(...){\n cout<<\"-.\"<<endl;\n }\n }\n}", "accuracy": 1, "time_ms": 5600, "memory_kb": 1332, "score_of_the_acc": -0.764, "final_rank": 17 }, { "submission_id": "aoj_1317_1487088", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n#define rep(i,n) for(int i=0;i<(int)(n);++i)\n#define rep1(i,n) for(int i=1;i<=(n);++i)\n#define all(c) (c).begin(),(c).end()\n#define fs first\n#define sc second\n#define pb push_back\n#define show(x) cout << #x << \" \" << x << endl\ntypedef pair<string,int> Psi;\ntypedef vector<int> Vi;\nint N;\nvector<string> ss,words[21];\nvector<Psi> vpsi;\nVi ans;\nbool app[26];\nvoid init(){\n\tans.clear();\n\tss.clear();\n\trep(i,21) words[i].clear();\n\tvpsi.clear();\n\trep(i,26) app[i]=0;\n}\nconst bool lencomp(Psi x,Psi y){\n\tstring s=x.fs,t=y.fs;\n\tbool a[26]={},b[26]={};\n\trep(i,s.size()) a[s[i]-'A']=1;\n\trep(i,t.size()) b[t[i]-'A']=1;\n\tint c=0,d=0;\n\trep(i,26) if(a[i]) c++;\n\trep(i,26) if(b[i]) d++;\n\treturn c>d;\n}\ninline bool eq(Vi& a,Vi& b){\n\trep(i,26) if(app[i]&&a[i]!=b[i]) return 0;\n\treturn 1;\n}\nbool dfs(int x,Vi vi){\n\tif(x==ss.size()){\n\t\tif(!ans.empty()&&!eq(ans,vi)) return false;\n\t\tans=vi;\n\t\treturn true;\n\t}\n\tstring s=ss[x];\n\tint L=s.size();\n\tfor(string w:words[L]){\n\t\tVi nvi=vi;\n\t\tbool ok=1;\n\t\trep(i,L){\n\t\t\tif(nvi[s[i]-'A']>=0&&nvi[s[i]-'A']+'A'!=w[i]){\n\t\t\t\tok=0;\n\t\t\t\tbreak;\n\t\t\t}\n\t\t\tif(nvi[w[i]-'A']>=0&&nvi[w[i]-'A']+'A'!=s[i]){\n\t\t\t\tok=0;\n\t\t\t\tbreak;\n\t\t\t}\n\t\t\tnvi[s[i]-'A']=w[i]-'A';\n\t\t\tnvi[w[i]-'A']=s[i]-'A';\n\t\t}\n\t\tif(!ok) continue;\n\t\tif(!dfs(x+1,nvi)) return false;\n\t}\n\treturn true;\n}\nint main(){\n\twhile(true){\n\t\tcin>>N;\n\t\tif(N==0) break;\n\t\tinit();\n\t\trep(i,N){\n\t\t\tstring s;\n\t\t\tcin>>s;\n\t\t\twords[s.size()].pb(s);\n\t\t}\n\t\tint I=0;\n\t\twhile(true){\n\t\t\tstring s;\n\t\t\tcin>>s;\n\t\t\trep(i,s.size()) if(s[i]!='.') app[s[i]-'A']=1;\n\t\t\tif(s.back()=='.'){\n\t\t\t\tvpsi.pb(Psi(s.substr(0,s.size()-1),I++));\n\t\t\t\tbreak;\n\t\t\t}\n\t\t\tvpsi.pb(Psi(s,I++));\n\t\t}\n\t\tsort(all(vpsi),lencomp);\n\t\tfor(Psi psi:vpsi) ss.pb(psi.fs);\n\t\tif(dfs(0,Vi(26,-1))&&!ans.empty()){\n\t\t\tvector<string> vst(ss.size());\n\t\t\trep(i,ss.size()) vst[vpsi[i].sc]=ss[i];\n\t\t\trep(i,vst.size()){\n\t\t\t\trep(j,vst[i].size()){\n\t\t\t\t\tcout<<(char)(ans[vst[i][j]-'A']+'A');\n\t\t\t\t}\n\t\t\t\tcout<<(i+1==vst.size()?\".\\n\":\" \");\n\t\t\t}\n\t\t}else{\n\t\t\tputs(\"-.\");\n\t\t}\n\t}\n}", "accuracy": 1, "time_ms": 20, "memory_kb": 1240, "score_of_the_acc": -0.0291, "final_rank": 1 }, { "submission_id": "aoj_1317_1411895", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\nconst int MAXN = 21;\nint N;\nstring word[MAXN];\nstring line;\nvector<string> V;\nvector<int> F;\nmap<char, char> ans;\n\nbool ok(const string &x, const string &w, map<char, char> m) {\n if(x.size() != w.size()) return false;\n for(int i = 0; i < x.size(); ++i) {\n if(m.count(x[i]) && m[x[i]] != w[i]) return false;\n if(m.count(w[i]) && m[w[i]] != x[i]) return false;\n m[x[i]] = w[i];\n m[w[i]] = x[i];\n }\n return true;\n}\n\nvoid reg(const string &x, const string &w, map<char, char> &m) {\n for(int i = 0; i < x.size(); ++i) {\n m[x[i]] = w[i];\n m[w[i]] = x[i];\n }\n}\n\nbool update(int k, map<char, char> m) {\n F = vector<int>(V.size());\n for(int i = k; i < V.size(); ++i) {\n for(int j = 0; j < N; ++j) {\n if(ok(V[i], word[j], m)) {\n F[i] |= 1 << j;\n }\n }\n if(!F[i]) return false;\n }\n return true;\n}\n\nvoid rec(int k, map<char, char> m) {\n if(k == V.size()) {\n if(ans.size()) throw 0;\n ans = m;\n return;\n }\n for(int i = 0; i < N; ++i) {\n if(F[k] >> i & 1); else continue;\n map<char, char> nm = m;\n reg(V[k], word[i], nm);\n vector<int> undo = F;\n if(update(k+1, nm)) {\n rec(k+1, nm);\n }\n F = undo;\n }\n}\n\nint main() {\n while(cin >> N && N) {\n for(int i = 0; i < N; ++i) {\n cin >> word[i];\n }\n cin.ignore();\n getline(cin, line);\n stringstream ss(line.substr(0, line.size()-1));\n set<string> ws;\n for(string s; ss >> s; ) ws.insert(s);\n V.clear();\n for(set<string>::iterator it = ws.begin();\n it != ws.end(); ++it) {\n V.push_back(*it);\n }\n update(0, map<char, char>());\n ans = map<char, char>();\n try {\n rec(0, map<char, char>());\n if(ans.empty()) throw 0;\n for(int i = 0; i < line.size(); ++i) {\n if(isalpha(line[i])) cout << ans[line[i]];\n else cout << line[i];\n }\n cout << endl;\n } catch(...) {\n cout << \"-.\" << endl;\n }\n }\n return 0;\n}", "accuracy": 1, "time_ms": 200, "memory_kb": 1368, "score_of_the_acc": -0.0705, "final_rank": 5 }, { "submission_id": "aoj_1317_1097826", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\ntypedef vector<int> vi;\ntypedef vector<vi> vvi;\ntypedef vector<string> vs;\ntypedef pair<int, int> pii;\ntypedef long long ll;\n#define ALL(a) (a).begin(),(a).end()\n#define RALL(a) (a).rbegin(),(a).rend()\n#define EXIST(s,e) ((s).find(e)!=(s).end())\n#define FOR(i,a,b) for(int i=(a); i<(b); ++i)\n#define REP(i,n) FOR(i,0,n)\n\nvs dfs(vs &words,vs &c_words,map<char,char> &m,int cur,vs &orig_c_words){\n vs ret;\n if(cur==orig_c_words.size()){\n REP(i,orig_c_words.size()){\n string s;\n REP(j,orig_c_words[i].size()){\n s.push_back(m[orig_c_words[i][j]]);\n }\n ret.push_back(s);\n }\n return ret;\n }\n\n REP(i,words.size()){\n if(words[i].size()==c_words[cur].size()){\n map<char,char> nm(m);\n bool ok=true;\n REP(j,words[i].size()){\n map<char,char>::iterator it_i=nm.find(words[i][j]);\n map<char,char>::iterator it_c=nm.find(c_words[cur][j]);\n bool ei=it_i!=nm.end();\n bool ec=it_c!=nm.end();\n if(ei&&ec){\n if(it_i->second!=c_words[cur][j]||it_c->second!=words[i][j]){\n ok=false;\n break;\n }\n }else if(ei||ec){\n ok=false;\n break;\n }else{\n nm[words[i][j]]=c_words[cur][j];\n nm[c_words[cur][j]]=words[i][j];\n }\n }\n if(ok){\n //cerr<<\"cur:\"<<cur<<endl;\n vs res=dfs(words,c_words,nm,cur+1,orig_c_words);\n //cerr<<\"res:\"<<res.size()<<endl;\n if(res.size()>0){\n if(res.size()==100){\n return vs(100);\n }\n if(ret.size()){\n return vs(100);\n }else{\n ret=res;\n }\n }\n }\n }\n }\n return ret;\n\n}\nbool pred(const string &l,const string &r){\n if(l.size()==r.size()){\n return l>r;\n }else{\n return l.size()>r.size();\n }\n}\nint main(){\n int n;\n while(cin>>n,n){\n vs words(n);\n REP(i,n){\n cin>>words[i];\n }\n string sequence;\n getline(cin,sequence);\n getline(cin,sequence);\n vs c_words;\n int prev=0;\n REP(i,sequence.size()){\n switch(sequence[i]){\n case ' ':\n case '.':\n c_words.push_back(sequence.substr(prev,i-prev));\n prev=i+1;\n break;\n }\n }\n vs orig_c_words(c_words);\n sort(ALL(c_words),pred);\n map<char,char> m;\n vs res=dfs(words,c_words,m,0,orig_c_words);\n if(res.size()&&res.size()<100){\n REP(i,res.size()){\n cout<<res[i]<<(i<res.size()-1?\" \":\"\");\n }\n cout<<\".\"<<endl;\n }else{\n cout<<\"-.\"<<endl;\n }\n }\n}", "accuracy": 1, "time_ms": 160, "memory_kb": 1280, "score_of_the_acc": -0.0529, "final_rank": 4 }, { "submission_id": "aoj_1317_1091493", "code_snippet": "#include <algorithm>\n#include <array>\n#include <cstdlib>\n#include <functional>\n#include <iostream>\n#include <sstream>\n#include <vector>\nusing namespace std;\n\nconstexpr int MAX_M = 50;\nconstexpr int MAX_LENGTH = 20;\n\nint n, m;\nstring target;\nvector<string> candidates[MAX_LENGTH + 1];\npair<int, string> words[MAX_M];\nvector<char> assign(128, -1);\n\nint cnt;\n\nbool dfs(int idx = 0) {\n\tif(idx == m) {\n\t\tif(++cnt == 1) {\n\t\t\tfor(auto &c : target) {\n\t\t\t\tif(c != ' ') c = assign[c];\n\t\t\t}\n\t\t\treturn true;\n\t\t}\n\t\telse {\n\t\t\treturn false;\n\t\t}\n\t}\n\n\tconst auto &s = words[idx].second;\n\n\tfor(const auto &t : candidates[s.size()]) {\n\t\tauto tmp(assign);\n\n\t\tfor(int i = 0; i < static_cast<int>(s.size()); ++i) {\n\t\t\tif(!(tmp[s[i]] == t[i] || (tmp[s[i]] == -1 && tmp[t[i]] == -1))) {\n\t\t\t\tgoto next;\n\t\t\t}\n\n\t\t\ttmp[s[i]] = t[i];\n\t\t\ttmp[t[i]] = s[i];\n\t\t}\n\n\t\tassign.swap(tmp);\n\t\tif(!dfs(idx + 1)) return false;\n\t\tassign = move(tmp);\n\t\t\n\tnext:;\n\t}\n\n\treturn true;\n}\n\nint main() {\n\tcin.tie(nullptr);\n\tios::sync_with_stdio(false);\n\n\twhile(cin >> n && n) {\n\t\tfor_each(begin(candidates), end(candidates), [](vector<string> &v) { v.clear(); });\n\n\t\tfor(int i = 0; i < n; ++i) {\n\t\t\tstring in;\n\t\t\tcin >> in;\n\n\t\t\tconst int len = in.size();\n\t\t\tcandidates[len].emplace_back(move(in));\n\t\t}\n\n\t\tcin.ignore();\n\t\tgetline(cin, target);\n\t\ttarget = target.substr(0, target.size() - 1);\n\n\t\t{\n\t\t\tstring in;\n\t\t\tistringstream iss(target);\n\n\t\t\tfor(m = 0; iss >> in; ++m) {\n\t\t\t\twords[m].second = in;\n\t\t\t\tin.erase(unique(in.begin(), in.end()), in.end());\n\t\t\t\twords[m].first = in.size();\n\t\t\t}\n\t\t}\n\n\t\tsort(words, words + m, greater<pair<int, string>>());\n\n\t\tcnt = 0;\n\t\tfill(begin(assign), end(assign), -1);\n\t\tdfs();\n\n\t\tcout << (cnt == 1 ? target : \"-\") << \".\\n\";\n\t}\n\n\treturn EXIT_SUCCESS;\n}", "accuracy": 1, "time_ms": 20, "memory_kb": 1260, "score_of_the_acc": -0.0319, "final_rank": 2 }, { "submission_id": "aoj_1317_930173", "code_snippet": "#include<bits/stdc++.h>\n\n#define REP(i,s,n) for(int i=s;i<n;i++)\n#define rep(i,n) REP(i,0,n)\n\nusing namespace std;\n\nconst int IINF = INT_MAX;\n\nint n,found,len;\nvector<string> buf[30],seq;\nstring tmp,sequence,answer;\nchar transfer[26];\n\nbool comp(const string& a,const string& b) {\n set<char> A,B;\n rep(i,a.size()) A.insert(a[i]);\n rep(i,b.size()) B.insert(b[i]);\n return A.size() > B.size();\n}\n\nvoid dfs(int cur){\n\n if( found >= 2 ) return;\n\n if( cur >= len ){\n ++found;\n rep(i,answer.size()) if( isupper(answer[i]) ) answer[i] = transfer[answer[i]-'A'];\n return;\n }\n\n int word_size = seq[cur].size();\n\n rep(i,(int)buf[word_size].size()){\n string word = buf[word_size][i];\n\n int *backtrack = new int[word_size+1];\n backtrack[0] = -1; // sentinel\n bool error = false;\n rep(j,word_size){\n if( transfer[word[j]-'A'] == '?' && transfer[seq[cur][j]-'A'] == '?' ) {\n backtrack[j] = 1, backtrack[j+1] = -1;\n transfer[word[j]-'A'] = seq[cur][j];\n transfer[seq[cur][j]-'A'] = word[j];\n } else if( transfer[word[j]-'A'] == seq[cur][j] && transfer[seq[cur][j]-'A'] == word[j] ) {\n backtrack[j] = 0;\n } else {\n backtrack[j] = -1;\n error = true;\n break;\n }\n }\n\n if( !error ) dfs( cur+1 );\n\n int j = 0;\n while( backtrack[j] != -1 && j < word_size) {\n if( backtrack[j] == 1 ) transfer[word[j]-'A'] = transfer[seq[cur][j]-'A'] = '?';\n ++j;\n }\n\n }\n\n}\n\nint main(){\n while( cin >> n, n ){\n seq.clear();\n rep(i,30) buf[i].clear();\n rep(i,n) {\n cin >> tmp;\n buf[(int)tmp.size()].push_back(tmp);\n }\n cin.ignore();\n getline(cin,sequence);\n stringstream ss(sequence.substr(0,(int)sequence.size()-1));\n while( !( ss >> tmp ).fail() ) seq.push_back(tmp);\n sort(seq.begin(),seq.end(),comp);\n\n answer = sequence;\n found = 0, len = (int)seq.size();\n rep(i,26) transfer[i] = '?';\n dfs(0);\n\n cout << ((found==1)?answer:\"-.\") << endl;\n\n }\n return 0;\n}", "accuracy": 1, "time_ms": 30, "memory_kb": 8100, "score_of_the_acc": -1.0026, "final_rank": 18 }, { "submission_id": "aoj_1317_800555", "code_snippet": "#include <cstdio>\n#include <cstring>\n#include <algorithm>\nusing namespace std;\n\nchar w1[22][110];\nchar w2[44][110];\nint w1_cnt, w2_cnt;\nint cw[28];\nint aw[28];\nbool ok[44][22];\nint ok_cnt[44];\nbool vis[44];\nint ans;\n\nbool cal_ok(int i, int j) {\n if (strlen(w2[i]) != strlen(w1[j])) return false;\n int tw[28];\n memcpy(tw, cw, sizeof(tw));\n for (int k = 0; w2[i][k]; k++) {\n int c = w2[i][k] - 'A';\n int d = w1[j][k] - 'A';\n if (tw[c] != -1 && tw[c] != w1[j][k]) return false;\n if (tw[d] != -1 && tw[d] != w2[i][k]) return false;\n tw[c] = w1[j][k];\n tw[d] = w2[i][k];\n }\n return true;\n}\nvoid get_ok() {\n memset(ok, 0, sizeof(ok));\n for (int i = 0; i < w2_cnt; i++)\n for (int j = 0; j < w1_cnt; j++) {\n ok[i][j] = cal_ok(i, j);\n if (ok[i][j]) ok_cnt[i]++;\n }\n}\nvoid print_ok() {\n\tfor (int i = 0; i < w2_cnt; i++) {\n\t\tfor (int j = 0; j < w1_cnt; j++) {\n\t\t\tprintf(\"%d \", ok[i][j]);\n\t\t}\n\t\tputs(\"\");\n\t}\n}\n\nint find() {\n int p = -1;\n for (int i = 0; i < w2_cnt; i++) if (!vis[i])\n if (p == -1 || ok_cnt[i] < ok_cnt[p]) p = i;\n return p;\n}\n\nvoid dfs(int u) {\n if (ans > 1) return;\n if (u == -1) {\n ans++;\n memcpy(aw, cw, sizeof(aw));\n return;\n }\n// bool tmp[44][22];\n// int tmp_cnt[44];\n int tw[28];\n// memcpy(tmp, ok, sizeof(tmp));\n// memcpy(tmp_cnt, ok_cnt, sizeof(ok_cnt));\n memcpy(tw, cw, sizeof(tw));\n get_ok();\n for (int i = 0; i < w1_cnt; i++) if (ok[u][i]) {\n vis[u] = true;\n for (int j = 0; w2[u][j]; j++) {\n int c = w2[u][j] - 'A';\n int d = w1[i][j] - 'A';\n cw[c] = w1[i][j];\n cw[d] = w2[u][j];\n }\n dfs(find());\n memcpy(cw, tw, sizeof(tw));\n vis[u] = false;\n get_ok();\n// memcpy(ok_cnt, tmp_cnt, sizeof(tmp_cnt));\n// memcpy(ok, tmp, sizeof(tmp));\n }\n}\n\nvoid work() {\n for (int i = 0; i < w1_cnt; i++)\n scanf(\"%s\", w1[i]);\n w2_cnt = 0;\n while (true) {\n char str[100];\n scanf(\"%s\", str);\n int len = strlen(str);\n if (str[len - 1] == '.') {\n str[--len] = 0;\n strcpy(w2[w2_cnt], str);\n w2_cnt++;\n break;\n } else {\n strcpy(w2[w2_cnt], str);\n w2_cnt++;\n }\n }\n//for (int i = 0; i < w2_cnt; i++) printf(\"%s \", w2[i]); puts(\".....\");\n memset(cw, -1, sizeof(cw));\n get_ok();\n//print_ok();\n memset(vis, 0, sizeof(vis));\n int p = find();\n//printf(\"%d\\n\", p);\n ans = 0;\n dfs(p);\n if (ans > 1) {\n puts(\"-.\");\n return;\n }\n//for (int i = 0; i < 26; i++) printf(\"%d \", aw[i]); puts(\"?\");\n for (int i = 0; i < w2_cnt; i++) {\n for (int j = 0; w2[i][j]; j++) {\n int d = w2[i][j] - 'A';\n printf(\"%c\", aw[d]);\n }\n if (i < w2_cnt - 1) printf(\" \");\n }\n puts(\".\");\n}\n\nint main() {\n while (scanf(\"%d\", &w1_cnt), w1_cnt) {\n work();\n }\n return 0;\n}", "accuracy": 1, "time_ms": 810, "memory_kb": 1044, "score_of_the_acc": -0.1035, "final_rank": 6 }, { "submission_id": "aoj_1317_791955", "code_snippet": "#include <iostream>\n#include <algorithm>\n#include <vector>\n#include <set>\n#include <cstring>\n#include <sstream>\nusing namespace std;\n\nint n, m;\nstring cand[22];\nvector<string> words;\n\nvector<int> match[82];\nint matchBit[82];\n\nvector<char> ansKey;\nint ansFlg;\n\nbool matches(string a, string b){\n if(a.size() != b.size()) return false;\n char trans[128];\n memset(trans, -1, sizeof(trans));\n for(int i = 0; i < a.size(); i++){\n if(trans[a[i]] != -1 && b[i] != trans[a[i]]) return false;\n if(trans[b[i]] != -1 && a[i] != trans[b[i]]) return false;\n trans[a[i]] = b[i];\n trans[b[i]] = a[i];\n }\n return true;\n}\n\nbool mycmp(const string &a, const string &b){\n set<char> st1;\n for(int i = 0; i < a.size(); i++) st1.insert(a[i]);\n set<char> st2;\n for(int i = 0; i < b.size(); i++) st2.insert(b[i]);\n\n return st1.size() > st2.size();\n}\n\nbool setKey(const string &a, const string &b, vector<char> &key){\n for(int i = 0; i < a.size(); i++){\n if(key[a[i]] != -1 && key[a[i]] != b[i] ||\n key[b[i]] != -1 && key[b[i]] != a[i]){\n return false;\n }\n key[a[i]] = b[i];\n key[b[i]] = a[i];\n }\n return true;\n}\n\nvoid dfs(int idx, int used, vector<char> key){\n if(idx == m){\n if(ansFlg == 1){\n ansFlg = -1;\n }\n else{\n ansFlg = 1;\n ansKey = key;\n }\n return;\n }\n\n for(int i = idx; i < m; i++){\n if((matchBit[i] & used) == matchBit[i]){\n return;\n }\n }\n\n for(int i = 0; i < match[idx].size(); i++){\n int j = match[idx][i];\n if(used & (1 << j)) continue;\n\n vector<char> nextKey = key;\n if(setKey(words[idx], cand[j], nextKey)){\n dfs(idx + 1, used | (1 << j), nextKey);\n if(ansFlg == -1) return;\n }\n }\n}\n\nvoid solve(){\n sort(words.begin(), words.end(), mycmp);\n\n for(int i = 0; i < m; i++){\n match[i].clear();\n matchBit[i] = 0;\n\n for(int j = 0; j < n; j++){\n if(matches(words[i], cand[j])){\n match[i].push_back(j);\n matchBit[i] |= (1 << j);\n }\n }\n }\n\n vector<char> key(128, -1);\n ansFlg = 0;\n dfs(0, 0, key);\n}\n\nint main(){\n while(cin >> n, n){\n for(int i = 0; i < n; i++){\n cin >> cand[i];\n\n for(int j = 0; j < i; j++){\n if(cand[j] == cand[i]){\n i--;\n n--;\n break;\n }\n }\n }\n\n string s;\n getline(cin, s);\n getline(cin, s);\n for(int i = 0; i < s.size(); i++) if(s[i] == '.') s[i] = ' ';\n\n vector<string> raw;\n stringstream ss(s);\n words.clear();\n\n while(ss >> s){\n raw.push_back(s);\n\n bool pushFlg = true;\n for(int i = 0; i < words.size(); i++){\n if(words[i] == s){\n pushFlg = false;\n break;\n }\n }\n if(pushFlg) words.push_back(s);\n }\n m = words.size();\n\n solve();\n\n if(ansFlg == 1){\n for(int i = 0; i < raw.size(); i++){\n if(i != 0) cout << \" \";\n for(int j = 0; j < raw[i].size(); j++){\n cout << ansKey[raw[i][j]];\n }\n }\n cout << \".\" << endl;\n }\n else{\n cout << \"-.\" << endl;\n }\n }\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 1272, "score_of_the_acc": -0.0323, "final_rank": 3 } ]
aoj_1322_cpp
Problem H: ASCII Expression Mathematical expressions appearing in old papers and old technical articles are printed with typewriter in several lines, where a fixed-width or monospaced font is required to print characters (digits, symbols and spaces). Let us consider the following mathematical expression. It is printed in the following four lines: 4 2 ( 1 - ---- ) * - 5 + 6 2 3 where “- 5” indicates unary minus followed by 5. We call such an expression of lines “ASCII expression”. For helping those who want to evaluate ASCII expressions obtained through optical character recognition (OCR) from old papers, your job is to write a program that recognizes the structure of ASCII expressions and computes their values. For the sake of simplicity, you may assume that ASCII expressions are constructed by the following rules. Its syntax is shown in Table H.1. (1) Terminal symbols are ‘ 0 ’, ‘ 1 ’, ‘ 2 ’, ‘ 3 ’, ‘ 4 ’, ‘ 5 ’, ‘ 6 ’, ‘ 7 ’, ‘ 8 ’, ‘ 9 ’, ‘ + ’, ‘ - ’, ‘ * ’, ‘ ( ’, ‘ ) ’, and ‘ ’. (2) Nonterminal symbols are expr , term , factor , powexpr , primary , fraction and digit . The start symbol is expr . (3) A “cell” is a rectangular region of characters that corresponds to a terminal or nonterminal symbol (Figure H.1). In the cell, there are no redundant rows and columns that consist only of space characters. A cell corresponding to a terminal symbol consists of a single character. A cell corresponding to a nonterminal symbol contains cell(s) corresponding to its descendant(s) but never partially overlaps others. (4) Each cell has a base-line, a top-line, and a bottom-line. The base-lines of child cells of the right-hand side of rules I, II, III, and V should be aligned. Their vertical position defines the base-line position of their left-hand side cell. Table H.1: Rules for constructing ASCII expressions (similar to Backus-Naur Form) The box indicates the cell of the terminal or nonterminal symbol that corresponds to a rectan- gular region of characters. Note that each syntactically-needed space character is explicitly indicated by the period character denoted by, here. (5) powexpr consists of a primary and an optional digit . The digit is placed one line above the base-line of the primary cell. They are horizontally adjacent to each other. The base-line of a powexpr is that of the primary . (6) fraction is indicated by three or more consecutive hyphens called “vinculum”. Its dividend expr is placed just above the vinculum, and its divisor expr is placed just beneath it. The number of the hyphens of the vinculum, denoted by w h , is equal to 2 + max( w 1 , w 2 ), where w 1 and w 2 indicate the width of the cell of the dividend and that of the divisor, respectively. These cells are centered, where there are ⌈( w h − w k )/2⌉ space characters to the left and ⌊( w h − w k )/2⌋ space characters to the right, ( k = 1, 2). The base-line of a fraction is at the position of the vinculum. (7) digit consists of one character ...(truncated)
[ { "submission_id": "aoj_1322_5951862", "code_snippet": "#define MOD_TYPE 1\n\n#pragma region Macros\n\n#include <bits/stdc++.h>\nusing namespace std;\n\n//#include <atcoder/all>\n\n\n#include <utility>\n\nnamespace atcoder {\n\nnamespace internal {\n\n// @param m `1 <= m`\n// @return x mod m\nconstexpr long long safe_mod(long long x, long long m) {\n x %= m;\n if (x < 0) x += m;\n return x;\n}\n\n// Fast modular multiplication by barrett reduction\n// Reference: https://en.wikipedia.org/wiki/Barrett_reduction\n// NOTE: reconsider after Ice Lake\nstruct barrett {\n unsigned int _m;\n unsigned long long im;\n\n // @param m `1 <= m < 2^31`\n barrett(unsigned int m) : _m(m), im((unsigned long long)(-1) / m + 1) {}\n\n // @return m\n unsigned int umod() const { return _m; }\n\n // @param a `0 <= a < m`\n // @param b `0 <= b < m`\n // @return `a * b % m`\n unsigned int mul(unsigned int a, unsigned int b) const {\n // [1] m = 1\n // a = b = im = 0, so okay\n\n // [2] m >= 2\n // im = ceil(2^64 / m)\n // -> im * m = 2^64 + r (0 <= r < m)\n // let z = a*b = c*m + d (0 <= c, d < m)\n // a*b * im = (c*m + d) * im = c*(im*m) + d*im = c*2^64 + c*r + d*im\n // c*r + d*im < m * m + m * im < m * m + 2^64 + m <= 2^64 + m * (m + 1) < 2^64 * 2\n // ((ab * im) >> 64) == c or c + 1\n unsigned long long z = a;\n z *= b;\n#ifdef _MSC_VER\n unsigned long long x;\n _umul128(z, im, &x);\n#else\n unsigned long long x =\n (unsigned long long)(((unsigned __int128)(z)*im) >> 64);\n#endif\n unsigned int v = (unsigned int)(z - x * _m);\n if (_m <= v) v += _m;\n return v;\n }\n};\n\n// @param n `0 <= n`\n// @param m `1 <= m`\n// @return `(x ** n) % m`\nconstexpr long long pow_mod_constexpr(long long x, long long n, int m) {\n if (m == 1) return 0;\n unsigned int _m = (unsigned int)(m);\n unsigned long long r = 1;\n unsigned long long y = safe_mod(x, m);\n while (n) {\n if (n & 1) r = (r * y) % _m;\n y = (y * y) % _m;\n n >>= 1;\n }\n return r;\n}\n\n// Reference:\n// M. Forisek and J. Jancina,\n// Fast Primality Testing for Integers That Fit into a Machine Word\n// @param n `0 <= n`\nconstexpr bool is_prime_constexpr(int n) {\n if (n <= 1) return false;\n if (n == 2 || n == 7 || n == 61) return true;\n if (n % 2 == 0) return false;\n long long d = n - 1;\n while (d % 2 == 0) d /= 2;\n constexpr long long bases[3] = {2, 7, 61};\n for (long long a : bases) {\n long long t = d;\n long long y = pow_mod_constexpr(a, t, n);\n while (t != n - 1 && y != 1 && y != n - 1) {\n y = y * y % n;\n t <<= 1;\n }\n if (y != n - 1 && t % 2 == 0) {\n return false;\n }\n }\n return true;\n}\ntemplate <int n> constexpr bool is_prime = is_prime_constexpr(n);\n\n// @param b `1 <= b`\n// @return pair(g, x) s.t. g = gcd(a, b), xa = g (mod b), 0 <= x < b/g\nconstexpr std::pair<long long, long long> inv_gcd(long long a, long long b) {\n a = safe_mod(a, b);\n if (a == 0) return {b, 0};\n\n // Contracts:\n // [1] s - m0 * a = 0 (mod b)\n // [2] t - m1 * a = 0 (mod b)\n // [3] s * |m1| + t * |m0| <= b\n long long s = b, t = a;\n long long m0 = 0, m1 = 1;\n\n while (t) {\n long long u = s / t;\n s -= t * u;\n m0 -= m1 * u; // |m1 * u| <= |m1| * s <= b\n\n // [3]:\n // (s - t * u) * |m1| + t * |m0 - m1 * u|\n // <= s * |m1| - t * u * |m1| + t * (|m0| + |m1| * u)\n // = s * |m1| + t * |m0| <= b\n\n auto tmp = s;\n s = t;\n t = tmp;\n tmp = m0;\n m0 = m1;\n m1 = tmp;\n }\n // by [3]: |m0| <= b/g\n // by g != b: |m0| < b/g\n if (m0 < 0) m0 += b / s;\n return {s, m0};\n}\n\n// Compile time primitive root\n// @param m must be prime\n// @return primitive root (and minimum in now)\nconstexpr int primitive_root_constexpr(int m) {\n if (m == 2) return 1;\n if (m == 167772161) return 3;\n if (m == 469762049) return 3;\n if (m == 754974721) return 11;\n if (m == 998244353) return 3;\n int divs[20] = {};\n divs[0] = 2;\n int cnt = 1;\n int x = (m - 1) / 2;\n while (x % 2 == 0) x /= 2;\n for (int i = 3; (long long)(i)*i <= x; i += 2) {\n if (x % i == 0) {\n divs[cnt++] = i;\n while (x % i == 0) {\n x /= i;\n }\n }\n }\n if (x > 1) {\n divs[cnt++] = x;\n }\n for (int g = 2;; g++) {\n bool ok = true;\n for (int i = 0; i < cnt; i++) {\n if (pow_mod_constexpr(g, (m - 1) / divs[i], m) == 1) {\n ok = false;\n break;\n }\n }\n if (ok) return g;\n }\n}\ntemplate <int m> constexpr int primitive_root = primitive_root_constexpr(m);\n\n} // namespace internal\n\n} // namespace atcoder\n\n\n#include <cassert>\n#include <numeric>\n#include <type_traits>\n\nnamespace atcoder {\n\nnamespace internal {\n\n#ifndef _MSC_VER\ntemplate <class T>\nusing is_signed_int128 =\n typename std::conditional<std::is_same<T, __int128_t>::value ||\n std::is_same<T, __int128>::value,\n std::true_type,\n std::false_type>::type;\n\ntemplate <class T>\nusing is_unsigned_int128 =\n typename std::conditional<std::is_same<T, __uint128_t>::value ||\n std::is_same<T, unsigned __int128>::value,\n std::true_type,\n std::false_type>::type;\n\ntemplate <class T>\nusing make_unsigned_int128 =\n typename std::conditional<std::is_same<T, __int128_t>::value,\n __uint128_t,\n unsigned __int128>;\n\ntemplate <class T>\nusing is_integral = typename std::conditional<std::is_integral<T>::value ||\n is_signed_int128<T>::value ||\n is_unsigned_int128<T>::value,\n std::true_type,\n std::false_type>::type;\n\ntemplate <class T>\nusing is_signed_int = typename std::conditional<(is_integral<T>::value &&\n std::is_signed<T>::value) ||\n is_signed_int128<T>::value,\n std::true_type,\n std::false_type>::type;\n\ntemplate <class T>\nusing is_unsigned_int =\n typename std::conditional<(is_integral<T>::value &&\n std::is_unsigned<T>::value) ||\n is_unsigned_int128<T>::value,\n std::true_type,\n std::false_type>::type;\n\ntemplate <class T>\nusing to_unsigned = typename std::conditional<\n is_signed_int128<T>::value,\n make_unsigned_int128<T>,\n typename std::conditional<std::is_signed<T>::value,\n std::make_unsigned<T>,\n std::common_type<T>>::type>::type;\n\n#else\n\ntemplate <class T> using is_integral = typename std::is_integral<T>;\n\ntemplate <class T>\nusing is_signed_int =\n typename std::conditional<is_integral<T>::value && std::is_signed<T>::value,\n std::true_type,\n std::false_type>::type;\n\ntemplate <class T>\nusing is_unsigned_int =\n typename std::conditional<is_integral<T>::value &&\n std::is_unsigned<T>::value,\n std::true_type,\n std::false_type>::type;\n\ntemplate <class T>\nusing to_unsigned = typename std::conditional<is_signed_int<T>::value,\n std::make_unsigned<T>,\n std::common_type<T>>::type;\n\n#endif\n\ntemplate <class T>\nusing is_signed_int_t = std::enable_if_t<is_signed_int<T>::value>;\n\ntemplate <class T>\nusing is_unsigned_int_t = std::enable_if_t<is_unsigned_int<T>::value>;\n\ntemplate <class T> using to_unsigned_t = typename to_unsigned<T>::type;\n\n} // namespace internal\n\n} // namespace atcoder\n\n#include <cassert>\n#include <numeric>\n#include <type_traits>\n\n#ifdef _MSC_VER\n#include <intrin.h>\n#endif\n\nnamespace atcoder {\n\nnamespace internal {\n\nstruct modint_base {};\nstruct static_modint_base : modint_base {};\n\ntemplate <class T> using is_modint = std::is_base_of<modint_base, T>;\ntemplate <class T> using is_modint_t = std::enable_if_t<is_modint<T>::value>;\n\n} // namespace internal\n\ntemplate <int m, std::enable_if_t<(1 <= m)>* = nullptr>\nstruct static_modint : internal::static_modint_base {\n using mint = static_modint;\n\n public:\n static constexpr int mod() { return m; }\n static mint raw(int v) {\n mint x;\n x._v = v;\n return x;\n }\n\n static_modint() : _v(0) {}\n template <class T, internal::is_signed_int_t<T>* = nullptr>\n static_modint(T v) {\n long long x = (long long)(v % (long long)(umod()));\n if (x < 0) x += umod();\n _v = (unsigned int)(x);\n }\n template <class T, internal::is_unsigned_int_t<T>* = nullptr>\n static_modint(T v) {\n _v = (unsigned int)(v % umod());\n }\n static_modint(bool v) { _v = ((unsigned int)(v) % umod()); }\n\n unsigned int val() const { return _v; }\n\n mint& operator++() {\n _v++;\n if (_v == umod()) _v = 0;\n return *this;\n }\n mint& operator--() {\n if (_v == 0) _v = umod();\n _v--;\n return *this;\n }\n mint operator++(int) {\n mint result = *this;\n ++*this;\n return result;\n }\n mint operator--(int) {\n mint result = *this;\n --*this;\n return result;\n }\n\n mint& operator+=(const mint& rhs) {\n _v += rhs._v;\n if (_v >= umod()) _v -= umod();\n return *this;\n }\n mint& operator-=(const mint& rhs) {\n _v -= rhs._v;\n if (_v >= umod()) _v += umod();\n return *this;\n }\n mint& operator*=(const mint& rhs) {\n unsigned long long z = _v;\n z *= rhs._v;\n _v = (unsigned int)(z % umod());\n return *this;\n }\n mint& operator/=(const mint& rhs) { return *this = *this * rhs.inv(); }\n\n mint operator+() const { return *this; }\n mint operator-() const { return mint() - *this; }\n\n mint pow(long long n) const {\n assert(0 <= n);\n mint x = *this, r = 1;\n while (n) {\n if (n & 1) r *= x;\n x *= x;\n n >>= 1;\n }\n return r;\n }\n mint inv() const {\n if (prime) {\n assert(_v);\n return pow(umod() - 2);\n } else {\n auto eg = internal::inv_gcd(_v, m);\n assert(eg.first == 1);\n return eg.second;\n }\n }\n\n friend mint operator+(const mint& lhs, const mint& rhs) {\n return mint(lhs) += rhs;\n }\n friend mint operator-(const mint& lhs, const mint& rhs) {\n return mint(lhs) -= rhs;\n }\n friend mint operator*(const mint& lhs, const mint& rhs) {\n return mint(lhs) *= rhs;\n }\n friend mint operator/(const mint& lhs, const mint& rhs) {\n return mint(lhs) /= rhs;\n }\n friend bool operator==(const mint& lhs, const mint& rhs) {\n return lhs._v == rhs._v;\n }\n friend bool operator!=(const mint& lhs, const mint& rhs) {\n return lhs._v != rhs._v;\n }\n\n private:\n unsigned int _v;\n static constexpr unsigned int umod() { return m; }\n static constexpr bool prime = internal::is_prime<m>;\n};\n\ntemplate <int id> struct dynamic_modint : internal::modint_base {\n using mint = dynamic_modint;\n\n public:\n static int mod() { return (int)(bt.umod()); }\n static void set_mod(int m) {\n assert(1 <= m);\n bt = internal::barrett(m);\n }\n static mint raw(int v) {\n mint x;\n x._v = v;\n return x;\n }\n\n dynamic_modint() : _v(0) {}\n template <class T, internal::is_signed_int_t<T>* = nullptr>\n dynamic_modint(T v) {\n long long x = (long long)(v % (long long)(mod()));\n if (x < 0) x += mod();\n _v = (unsigned int)(x);\n }\n template <class T, internal::is_unsigned_int_t<T>* = nullptr>\n dynamic_modint(T v) {\n _v = (unsigned int)(v % mod());\n }\n dynamic_modint(bool v) { _v = ((unsigned int)(v) % mod()); }\n\n unsigned int val() const { return _v; }\n\n mint& operator++() {\n _v++;\n if (_v == umod()) _v = 0;\n return *this;\n }\n mint& operator--() {\n if (_v == 0) _v = umod();\n _v--;\n return *this;\n }\n mint operator++(int) {\n mint result = *this;\n ++*this;\n return result;\n }\n mint operator--(int) {\n mint result = *this;\n --*this;\n return result;\n }\n\n mint& operator+=(const mint& rhs) {\n _v += rhs._v;\n if (_v >= umod()) _v -= umod();\n return *this;\n }\n mint& operator-=(const mint& rhs) {\n _v += mod() - rhs._v;\n if (_v >= umod()) _v -= umod();\n return *this;\n }\n mint& operator*=(const mint& rhs) {\n _v = bt.mul(_v, rhs._v);\n return *this;\n }\n mint& operator/=(const mint& rhs) { return *this = *this * rhs.inv(); }\n\n mint operator+() const { return *this; }\n mint operator-() const { return mint() - *this; }\n\n mint pow(long long n) const {\n assert(0 <= n);\n mint x = *this, r = 1;\n while (n) {\n if (n & 1) r *= x;\n x *= x;\n n >>= 1;\n }\n return r;\n }\n mint inv() const {\n auto eg = internal::inv_gcd(_v, mod());\n assert(eg.first == 1);\n return eg.second;\n }\n\n friend mint operator+(const mint& lhs, const mint& rhs) {\n return mint(lhs) += rhs;\n }\n friend mint operator-(const mint& lhs, const mint& rhs) {\n return mint(lhs) -= rhs;\n }\n friend mint operator*(const mint& lhs, const mint& rhs) {\n return mint(lhs) *= rhs;\n }\n friend mint operator/(const mint& lhs, const mint& rhs) {\n return mint(lhs) /= rhs;\n }\n friend bool operator==(const mint& lhs, const mint& rhs) {\n return lhs._v == rhs._v;\n }\n friend bool operator!=(const mint& lhs, const mint& rhs) {\n return lhs._v != rhs._v;\n }\n\n private:\n unsigned int _v;\n static internal::barrett bt;\n static unsigned int umod() { return bt.umod(); }\n};\ntemplate <int id> internal::barrett dynamic_modint<id>::bt = 998244353;\n\nusing modint998244353 = static_modint<998244353>;\nusing modint1000000007 = static_modint<1000000007>;\nusing modint = dynamic_modint<-1>;\n\nnamespace internal {\n\ntemplate <class T>\nusing is_static_modint = std::is_base_of<internal::static_modint_base, T>;\n\ntemplate <class T>\nusing is_static_modint_t = std::enable_if_t<is_static_modint<T>::value>;\n\ntemplate <class> struct is_dynamic_modint : public std::false_type {};\ntemplate <int id>\nstruct is_dynamic_modint<dynamic_modint<id>> : public std::true_type {};\n\ntemplate <class T>\nusing is_dynamic_modint_t = std::enable_if_t<is_dynamic_modint<T>::value>;\n\n} // namespace internal\n\n} // namespace atcoder\n\nusing namespace atcoder;\n\n#if 0\n#include <boost/multiprecision/cpp_dec_float.hpp>\n#include <boost/multiprecision/cpp_int.hpp>\nusing Int = boost::multiprecision::cpp_int;\nusing lld = boost::multiprecision::cpp_dec_float_100;\n#endif\n\n#if 1\n#pragma GCC target(\"avx2\")\n#pragma GCC optimize(\"O3\")\n#pragma GCC optimize(\"unroll-loops\")\n#endif\n\nusing ll = long long int;\nusing ld = long double;\nusing pii = pair<int, int>;\nusing pll = pair<ll, ll>;\nusing pld = pair<ld, ld>;\ntemplate <typename Q_type>\nusing smaller_queue = priority_queue<Q_type, vector<Q_type>, greater<Q_type>>;\n\n#if MOD_TYPE == 1\nconstexpr ll MOD = ll(1e9 + 7);\n#else\n#if MOD_TYPE == 2\nconstexpr ll MOD = 998244353;\n#else\nconstexpr ll MOD = 1000003;\n#endif\n#endif\n\nusing mint = static_modint<MOD>;\nconstexpr int INF = (int)1e9 + 10;\nconstexpr ll LINF = (ll)4e18;\nconstexpr double PI = acos(-1.0);\nconstexpr double EPS = 1e-11;\nconstexpr int Dx[] = {0, 0, -1, 1, -1, 1, -1, 1, 0};\nconstexpr int Dy[] = {1, -1, 0, 0, -1, -1, 1, 1, 0};\n\n#define REP(i, m, n) for (ll i = m; i < (ll)(n); ++i)\n#define rep(i, n) REP(i, 0, n)\n#define REPI(i, m, n) for (int i = m; i < (int)(n); ++i)\n#define repi(i, n) REPI(i, 0, n)\n#define MP make_pair\n#define MT make_tuple\n#define YES(n) cout << ((n) ? \"YES\" : \"NO\") << \"\\n\"\n#define Yes(n) cout << ((n) ? \"Yes\" : \"No\") << \"\\n\"\n#define possible(n) cout << ((n) ? \"possible\" : \"impossible\") << \"\\n\"\n#define Possible(n) cout << ((n) ? \"Possible\" : \"Impossible\") << \"\\n\"\n#define Yay(n) cout << ((n) ? \"Yay!\" : \":(\") << \"\\n\"\n#define all(v) v.begin(), v.end()\n#define NP(v) next_permutation(all(v))\n#define dbg(x) cerr << #x << \":\" << x << \"\\n\";\n#define UNIQUE(v) v.erase(unique(all(v)), v.end())\n\nstruct io_init {\n io_init() {\n cin.tie(nullptr);\n ios::sync_with_stdio(false);\n cout << setprecision(30) << setiosflags(ios::fixed);\n };\n} io_init;\ntemplate <typename T>\ninline bool chmin(T &a, T b) {\n if (a > b) {\n a = b;\n return true;\n }\n return false;\n}\ntemplate <typename T>\ninline bool chmax(T &a, T b) {\n if (a < b) {\n a = b;\n return true;\n }\n return false;\n}\ninline ll CEIL(ll a, ll b) { return (a + b - 1) / b; }\ntemplate <typename A, size_t N, typename T>\ninline void Fill(A (&array)[N], const T &val) {\n fill((T *)array, (T *)(array + N), val);\n}\ntemplate <typename T>\nvector<T> compress(vector<T> &v) {\n vector<T> val = v;\n sort(all(val)), val.erase(unique(all(val)), val.end());\n for (auto &&vi : v) vi = lower_bound(all(val), vi) - val.begin();\n return val;\n}\ntemplate <typename T, typename U>\nconstexpr istream &operator>>(istream &is, pair<T, U> &p) noexcept {\n is >> p.first >> p.second;\n return is;\n}\ntemplate <typename T, typename U>\nconstexpr ostream &operator<<(ostream &os, pair<T, U> p) noexcept {\n os << p.first << \" \" << p.second;\n return os;\n}\nostream &operator<<(ostream &os, mint m) {\n os << m.val();\n return os;\n}\n\nrandom_device seed_gen;\nmt19937_64 engine(seed_gen());\n\nstruct BiCoef {\n vector<mint> fact_, inv_, finv_;\n BiCoef(int n) noexcept : fact_(n, 1), inv_(n, 1), finv_(n, 1) {\n fact_.assign(n, 1), inv_.assign(n, 1), finv_.assign(n, 1);\n for (int i = 2; i < n; i++) {\n fact_[i] = fact_[i - 1] * i;\n inv_[i] = -inv_[MOD % i] * (MOD / i);\n finv_[i] = finv_[i - 1] * inv_[i];\n }\n }\n mint C(ll n, ll k) const noexcept {\n if (n < k || n < 0 || k < 0) return 0;\n return fact_[n] * finv_[k] * finv_[n - k];\n }\n mint P(ll n, ll k) const noexcept { return C(n, k) * fact_[k]; }\n mint H(ll n, ll k) const noexcept { return C(n + k - 1, k); }\n mint Ch1(ll n, ll k) const noexcept {\n if (n < 0 || k < 0) return 0;\n mint res = 0;\n for (int i = 0; i < n; i++)\n res += C(n, i) * mint(n - i).pow(k) * (i & 1 ? -1 : 1);\n return res;\n }\n mint fact(ll n) const noexcept {\n if (n < 0) return 0;\n return fact_[n];\n }\n mint inv(ll n) const noexcept {\n if (n < 0) return 0;\n return inv_[n];\n }\n mint finv(ll n) const noexcept {\n if (n < 0) return 0;\n return finv_[n];\n }\n};\n\nBiCoef bc(500010);\n\n#pragma endregion\n\ntemplate <typename T>\nvoid icpc(T a) {\n if (!a) exit(0);\n}\ntemplate <typename T, typename U>\nvoid icpc(T a, U end) {\n if (a == end) exit(0);\n}\n\n// 端のスペースを削除\nvoid clean(vector<string> &s) {\n int n = s.size(), m = s[0].size();\n vector<int> erase_i(n, 0), erase_j(m, 0);\n rep(i, n) {\n bool allspace = true;\n rep(j, m) {\n if (s[i][j] != '.') {\n allspace = false;\n break;\n }\n }\n if (allspace)\n erase_i[i] = 1;\n else\n break;\n }\n for (int i = n - 1; i >= 0; i--) {\n bool allspace = true;\n rep(j, m) {\n if (s[i][j] != '.') {\n allspace = false;\n break;\n }\n }\n if (allspace)\n erase_i[i] = 1;\n else\n break;\n }\n rep(j, m) {\n bool allspace = true;\n rep(i, n) {\n if (s[i][j] != '.') {\n allspace = false;\n break;\n }\n }\n if (allspace)\n erase_j[j] = 1;\n else\n break;\n }\n for (int j = m - 1; j >= 0; j--) {\n bool allspace = true;\n rep(i, n) {\n if (s[i][j] != '.') {\n allspace = false;\n break;\n }\n }\n if (allspace)\n erase_j[j] = 1;\n else\n break;\n }\n\n vector<string> res;\n rep(i, n) {\n if (erase_i[i]) continue;\n res.emplace_back(\"\");\n rep(j, m) {\n if (erase_j[j]) continue;\n res.back().push_back(s[i][j]);\n }\n }\n s = res;\n}\n\n// base の位置 (存在しない場合は-1)\nint find_base(vector<string> s) {\n int n = s.size(), m = s[0].size();\n int Max = 0;\n int base = -1;\n rep(i, n) {\n int Maxi = -1;\n int cnt = 0;\n rep(j, m) {\n if (s[i][j] == '-')\n cnt++;\n else\n cnt = 0;\n if (cnt >= 2) {\n chmax(Maxi, cnt);\n }\n }\n if (chmax(Max, Maxi)) {\n base = i;\n }\n }\n return base;\n}\n\n// 分割\nvector<vector<string>> split(vector<string> &s, vector<int> &pos) {\n int n = s.size(), m = s[0].size();\n vector<vector<string>> res(pos.size() + 1, vector<string>(n));\n rep(i, n) {\n int pi = 0;\n rep(j, m) {\n if (pi < pos.size() and j == pos[pi]) {\n pi++;\n } else {\n res[pi][i].push_back(s[i][j]);\n }\n }\n }\n for (auto &&r : res) clean(r);\n return res;\n}\n\nmodint expr(vector<string>);\nmodint factor(vector<string>);\nmodint powexpr(vector<string>);\nmodint primary(vector<string>);\nmodint fraction(vector<string>);\nmodint digit(vector<string>);\n\nmodint expr_sub(vector<pair<char, modint>> &p) {\n modint prod = 1;\n modint res = 0;\n int sgn = 1;\n for (auto [op, val] : p) {\n if (op == 'n') {\n prod *= val;\n } else if (op == '+') {\n res += prod * sgn;\n prod = 1;\n sgn = 1;\n } else if (op == '-') {\n res += prod * sgn;\n prod = 1;\n sgn = -1;\n }\n }\n res += prod * sgn;\n return res;\n}\n\n// term も兼ねる\nmodint expr(vector<string> s) {\n clean(s);\n int n = s.size(), m = s[0].size();\n\n int base = find_base(s);\n if (base == -1) base = n - 1;\n vector<int> pos = {};\n int blank = 0;\n rep(j, m) {\n bool allspace = true;\n rep(i, n) {\n if (s[i][j] != '.') {\n allspace = false;\n }\n if (s[i][j] == '(') {\n blank++;\n } else if (s[i][j] == ')') {\n blank--;\n }\n }\n if (allspace and blank == 0) pos.emplace_back(j);\n }\n vector<vector<string>> v = split(s, pos);\n\n vector<pair<char, modint>> p(v.size());\n rep(i, v.size()) {\n char op = 'n';\n modint val = 0;\n if (v[i].size() == 1 and v[i][0].size() == 1) { // 演算子 or 数字\n char c = v[i][0][0];\n if (isdigit(c)) {\n val = c - '0';\n } else {\n op = c;\n }\n } else {\n val = factor(v[i]);\n }\n p[i] = {op, val};\n }\n\n // *- と始めの - を圧縮\n vector<pair<char, modint>> q;\n bool minus = false;\n rep(i, p.size()) {\n if (i > 0 and p[i].first == '-' and p[i - 1].first != 'n' or\n i == 0 and p[i].first == '-') {\n minus ^= 1;\n } else {\n if (minus) {\n assert(p[i].first == 'n');\n p[i].second *= -1;\n minus = false;\n }\n q.push_back(p[i]);\n }\n }\n return expr_sub(q);\n}\n\n// 先頭に - が付くものは expr でケアする\nmodint factor(vector<string> s) {\n int n = s.size(), m = s[0].size();\n int base = find_base(s);\n if (base == -1) base = n - 1;\n if (s[base][m - 2] != '-') {\n return powexpr(s);\n }\n return fraction(s);\n}\n\nmodint powexpr(vector<string> s) {\n clean(s);\n int n = s.size(), m = s[0].size();\n\n if (n == 2 and m == 2) {\n modint a = s[1][0] - '0';\n int b = s[0][1] - '0';\n if (b == 0)\n return 1;\n else\n return a.pow(b);\n } else if (n == 1 and m == 1) {\n return s[0][0] - '0';\n }\n int base = find_base(s);\n if (base == -1) base = n - 1;\n if (s[base].back() == ')') {\n return primary(s);\n }\n assert(s[base][m - 2] == ')');\n assert(isdigit(s[base - 1][m - 1]));\n int b = s[base - 1][m - 1] - '0';\n rep(i, n) s[i].pop_back();\n modint a = primary(s);\n if (b == 0)\n return 1;\n else\n return a.pow(b);\n}\n\nmodint primary(vector<string> s) {\n clean(s);\n int n = s.size(), m = s[0].size();\n\n if (n == 1 and m == 1) return digit(s);\n int base = find_base(s);\n if (base == -1) base = n - 1;\n assert(s[base][0] == '(');\n assert(s[base].back() == ')');\n assert(m >= 5);\n vector<string> t(n);\n rep(i, n) { t[i] = s[i].substr(2, m - 4); }\n return expr(t);\n}\n\nmodint fraction(vector<string> s) {\n clean(s);\n int base = find_base(s);\n assert(base != -1);\n vector<string> U, D;\n rep(i, s.size()) {\n if (i < base)\n U.push_back(s[i]);\n else if (i > base)\n D.push_back(s[i]);\n }\n return expr(U) / expr(D);\n}\n\nmodint digit(vector<string> s) {\n clean(s);\n assert(s.size() == 1 and s[0].size() == 1);\n return s[0][0] - '0';\n}\n\nvoid solve() {\n int n;\n cin >> n;\n icpc(n);\n vector<string> s(n);\n rep(i, n) cin >> s[i];\n cout << expr(s).val() << \"\\n\";\n}\n\nint main() {\n modint::set_mod(2011);\n while (1) solve();\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 9176, "score_of_the_acc": -0.2647, "final_rank": 7 }, { "submission_id": "aoj_1322_4972605", "code_snippet": "#pragma region template\n#include <bits/stdc++.h>\n//#include <boost/multiprecision/cpp_int.hpp>\n// using cpp_int = boost::multiprecision::cpp_int;\nusing namespace std;\nusing ll = long long;\nusing ull = unsigned long long;\nusing ld = long double;\nusing vi = vector<int>;\nusing vvi = vector<vi>;\nusing vvvi = vector<vvi>;\nusing vll = vector<ll>;\nusing vvll = vector<vll>;\nusing vvvll = vector<vvll>;\nusing vld = vector<ld>;\nusing vvld = vector<vld>;\nusing vvvld = vector<vvld>;\nusing vs = vector<string>;\nusing pll = pair<ll, ll>;\nusing vp = vector<pll>;\ntemplate <typename T>\nusing pqrev = priority_queue<T, vector<T>, greater<T>>;\n#define rep(i, n) for (ll i = 0, i##_end = (n); i < i##_end; i++)\n#define repb(i, n) for (ll i = (n)-1; i >= 0; i--)\n#define repr(i, a, b) for (ll i = (a), i##_end = (b); i < i##_end; i++)\n#define reprb(i, a, b) for (ll i = (b)-1, i##_end = (a); i >= i##_end; i--)\n#define ALL(a) (a).begin(), (a).end()\n#define SZ(x) ((ll)(x).size())\n#ifdef OJ_LOCAL\n#include \"dump.hpp\"\n#else\n#define dump(...) ((void)0)\n#endif\nconstexpr ll INF = 1e+18;\nconstexpr ld EPS = 1e-12L;\nconstexpr ld PI = 3.14159265358979323846L;\ntemplate <typename T>\nconstexpr T local([[maybe_unused]] const T &lcl, [[maybe_unused]] const T &oj) {\n#ifdef OJ_LOCAL\n return lcl;\n#else\n return oj;\n#endif\n}\ntemplate <typename S, typename T>\nconstexpr bool chmax(S &a, const T &b) {\n if (a < b) {\n a = b;\n return 1;\n }\n return 0;\n}\ntemplate <typename S, typename T>\nconstexpr bool chmin(S &a, const T &b) {\n if (b < a) {\n a = b;\n return 1;\n }\n return 0;\n}\ntemplate <typename T>\nT max(const vector<T> &x) {\n return *max_element(ALL(x));\n}\ntemplate <typename T>\nT min(const vector<T> &x) {\n return *min_element(ALL(x));\n}\ntemplate <typename T>\npair<T, int> argmax(const vector<T> &x) {\n int idx = 0;\n T m = x[0];\n repr(i, 1, SZ(x)) {\n if (chmax(m, x[i])) idx = i;\n }\n return {m, idx};\n}\ntemplate <typename T>\npair<T, int> argmin(const vector<T> &x) {\n int idx = 0;\n T m = x[0];\n repr(i, 1, SZ(x)) {\n if (chmin(m, x[i])) idx = i;\n }\n return {m, idx};\n}\ntemplate <typename T>\nT sum(const vector<T> &x) {\n return accumulate(ALL(x), T(0));\n}\n// last param -> T\ntemplate <typename T>\nvector<T> makev(size_t a, T b) {\n return vector<T>(a, b);\n}\ntemplate <typename... Args>\nauto makev(size_t sz, Args... args) {\n return vector<decltype(makev(args...))>(sz, makev(args...));\n}\n\ntemplate <typename T>\nbool print_(const T &a) {\n cout << a;\n return true;\n}\ntemplate <typename T>\nbool print_(const vector<T> &vec) {\n for (auto &a : vec) {\n cout << a;\n if (&a != &vec.back()) cout << \" \";\n }\n return false;\n}\ntemplate <typename T>\nbool print_(const vector<vector<T>> &vv) {\n for (auto &v : vv) {\n for (auto &a : v) {\n cout << a;\n if (&a != &v.back()) cout << \" \";\n }\n if (&v != &vv.back()) cout << \"\\n\";\n }\n return false;\n}\nvoid print() { cout << \"\\n\"; }\ntemplate <typename Head, typename... Tail>\nvoid print(Head &&head, Tail &&... tail) {\n bool f = print_(head);\n if (sizeof...(tail) != 0) cout << (f ? \" \" : \"\\n\");\n print(forward<Tail>(tail)...);\n}\n#pragma endregion\n\n//*\nconstexpr ll MOD = 1e9 + 7;\n/*/\nconstexpr ll MOD = 998244353;\n//*/\n\n#define PR(f) \\\n do { \\\n cout << ((f) ? \"Yes\" : \"No\") << \"\\n\"; \\\n return; \\\n } while (0)\n\nint n, m;\nvs s;\n\nvoid reduce(int &u, int &l, int &d, int &r){\n //dump(\"reduce\", u, l, d, r);\n repr(i, u, d){\n bool f = 1;\n repr(j, l, r){\n if(s[i][j]!='.'){\n f = 0;\n break;\n }\n }\n if(f) u++;\n else break;\n }\n reprb(i, u, d){\n bool f = 1;\n repr(j, l, r){\n if(s[i][j]!='.'){\n f = 0;\n break;\n }\n }\n if(f) d--;\n else break;\n }\n repr(j, l, r){\n bool f = 1;\n repr(i, u, d){\n if(s[i][j]!='.'){\n f = 0;\n break;\n }\n }\n if(f) l++;\n else break;\n }\n reprb(j, l, r){\n bool f = 1;\n repr(i, u, d){\n if(s[i][j]!='.'){\n f = 0;\n break;\n }\n }\n if(f) r--;\n else break;\n }\n}\n\nconst int M = 2011;\nint inv[M];\nunordered_set<string> endexpr;\n\nint expr(int u, int l, int d, int r, int base);\nint term(int u, int l, int d, int r, int base);\n\nint digit(int u, int l){\n dump(\"digit\", u, l, s[u][l]-'0');\n return s[u][l]-'0';\n}\n\nint primary(int u, int l, int d, int r, int base){\n reduce(u, l, d, r);\n dump(\"primary\", u, l, d, r);\n int ans = 0;\n if(s[base][l]=='('){\n ans = expr(u, l+2, d, r-2, base);\n }else{\n ans = digit(base, l);\n }\n ans += M;\n ans %= M;\n dump(\"primary\", u, l, d, r, ans);\n return ans;\n}\n\nint powexpr(int u, int l, int d, int r, int base){\n reduce(u, l, d, r);\n dump(\"powexpr\", u, l, d, r);\n /*\n if(br(base, l, r)){\n return expr(u, l+2, d, r-2, base);\n }\n */\n int ans = 0;\n if(s[base][r-1]=='.'){\n int b = primary(u, l, d, r-1, base);\n int c = digit(base-1, r-1);\n ans = 1;\n rep(t, c){\n ans *= b;\n ans %= M;\n }\n }else{\n ans = primary(u, l, d, r, base);\n }\n ans += M;\n ans %= M;\n dump(\"powexpr\", u, l, d, r, ans);\n return ans;\n}\n\nint fraction(int u, int l, int d, int r, int base){\n reduce(u, l, d, r);\n dump(\"fraction\", u, l, d, r, base);\n /*\n if(br(base, l, r)){\n return expr(u, l+2, d, r-2, base);\n }\n */\n int ans = 0;\n int nu = u, nl = l+1, nd = base, nr = r-1;\n reduce(nu, nl, nd, nr);\n repr(i, nu, nd){\n if(s[i][nl]!='.'){\n ans = expr(nu, nl, nd, nr, i);\n break;\n }\n }\n nu = base+1, nl = l+1, nd = d, nr = r-1;\n reduce(nu, nl, nd, nr);\n repr(i, nu, nd){\n if(s[i][nl]!='.'){\n ans *= inv[expr(nu, nl, nd, nr, i)];\n break;\n }\n }\n ans += M;\n ans %= M;\n dump(\"fraction\", u, l, d, r, base, ans);\n return ans;\n}\n\nint factor(int u, int l, int d, int r, int base){\n reduce(u, l, d, r);\n dump(\"factor\", u, l, d, r, base);\n /*\n if(br(base, l, r)){\n return expr(u, l+2, d, r-2, base);\n }\n */\n int ans = 0;\n int stk = 0;\n repr(j, l, r){\n if(s[base][j]=='(') stk++;\n if(s[base][j]==')') stk--;\n if(s[base].substr(j, 3)==\".*.\" && !stk){\n ans = term(u, l, d, j, base) * factor(u, j+3, d, r, base);\n break;\n }\n }\n if(s[base].substr(l, 2)==\"--\"){\n ans = fraction(u, l, d, r, base);\n }else if(s[base].substr(l, 2)==\"-.\"){\n ans = -factor(u, l+2, d, r, base);\n }else{\n ans = powexpr(u, l, d, r, base);\n }\n ans += M;\n ans %= M;\n dump(\"factor\", u, l, d, r, ans);\n return ans;\n}\n\nint term(int u, int l, int d, int r, int base){\n reduce(u, l, d, r);\n dump(\"term\", u, l, d, r);\n /*\n if(br(base, l, r)){\n return expr(u, l+2, d, r-2, base);\n }\n */\n int ans = 0;\n int stk = 0;\n repr(j, l, r){\n if(s[base][j]=='(') stk++;\n if(s[base][j]==')') stk--;\n if(s[base].substr(j, 3)==\".*.\" && !stk){\n ans = factor(u, l, d, j, base) * term(u, j+3, d, r, base);\n break;\n }\n if(j==r-1){\n ans = factor(u, l, d, r, base);\n }\n }\n ans += M;\n ans %= M;\n dump(\"term\", u, l, d, r, ans);\n return ans;\n}\n\nint expr(int u, int l, int d, int r, int base){\n assert(l<r);\n reduce(u, l, d, r);\n dump(\"expr\", u, l, d, r);\n /*\n if(br(base, l, r)){\n return expr(u, l+2, d, r-2, base);\n }\n */\n int ans = 0;\n int stk = 0;\n reprb(j, l, r){\n if(s[base][j]==')') stk++;\n if(s[base][j]=='(') stk--;\n if(s[base].substr(j, 3)==\".+.\" && !stk){\n ans = expr(u, l, d, j, base) + term(u, j+3, d, r, base);\n break;\n }\n bool f = 0;\n if(j-2 >= l && endexpr.count(s[base].substr(j-2, 2))){\n f = 1;\n }\n if(j-1 == l && j+1 < r && '0' <= s[base][j-1] && s[base][j-1] <= '9'){\n f = 1;\n }\n //dump(j, f);\n if(s[base].substr(j, 3)==\".-.\" && f && !stk){\n ans = expr(u, l, d, j, base) - term(u, j+3, d, r, base);\n break;\n }\n if(j==l){\n ans = term(u, l, d, r, base);\n }\n }\n ans += M;\n ans %= M;\n dump(\"expr\", u, l, d, r, ans);\n return ans;\n}\n\nvoid solve() {\n endexpr.emplace(\"--\");\n endexpr.emplace(\".0\");\n endexpr.emplace(\".1\");\n endexpr.emplace(\".2\");\n endexpr.emplace(\".3\");\n endexpr.emplace(\".4\");\n endexpr.emplace(\".5\");\n endexpr.emplace(\".6\");\n endexpr.emplace(\".7\");\n endexpr.emplace(\".8\");\n endexpr.emplace(\".9\");\n endexpr.emplace(\".)\");\n endexpr.emplace(\").\");\n endexpr.emplace(\"0.\");\n endexpr.emplace(\"1.\");\n endexpr.emplace(\"2.\");\n endexpr.emplace(\"3.\");\n endexpr.emplace(\"4.\");\n endexpr.emplace(\"5.\");\n endexpr.emplace(\"6.\");\n endexpr.emplace(\"7.\");\n endexpr.emplace(\"8.\");\n endexpr.emplace(\"9.\");\n cin >> n;\n if(!n) exit(0);\n s.resize(n);\n rep(i, n) cin >> s[i];\n m = SZ(s[0]);\n repr(i, 1, M){\n repr(j, 1, M){\n if((i*j)%M==1) inv[i] = j;\n }\n }\n int base = -1;\n rep(i, n){\n if(s[i][0]!='.') base = i;\n }\n print(expr(0, 0, n, m, base));\n}\n\nint main() {\n cin.tie(0);\n ios::sync_with_stdio(false);\n cout << fixed << setprecision(20);\n //*\n //solve();\n /*/\n ll _cases;\n cin >> _cases;\n while (_cases--) solve();\n //*/\n while(1) solve();\n}", "accuracy": 1, "time_ms": 420, "memory_kb": 3376, "score_of_the_acc": -1.0974, "final_rank": 8 }, { "submission_id": "aoj_1322_3349880", "code_snippet": "#include <iostream>\n#include <vector>\n#include <string>\n#include <algorithm>\nusing namespace std;\n\nint power[2011][2011], divis[2011][2011], mod = 2011;\n\nvoid init() {\n\tfor (int i = 0; i < 2011; i++) {\n\t\tpower[i][0] = 1; for (int j = 1; j < 2011; j++) power[i][j] = (power[i][j - 1] * i) % mod;\n\t}\n\tfor (int i = 0; i < 2011 * 2011; i++) divis[i / 2011][i % 2011] = -1;\n\tfor (int i = 0; i < 2011; i++) {\n\t\tfor (int j = 0; j < 2011; j++) {\n\t\t\tif (divis[(i * j) % mod][i] == -1) divis[(i * j) % mod][i] = j;\n\t\t}\n\t}\n}\n\nint modpow(int a, int b) { return power[a][b]; }\nint Div(int a, int b) { return divis[a][b]; }\n\nint solve(vector<string> A) {\n\t/*cout << endl;\n\tfor (int i = 0; i < A.size(); i++) cout << A[i] << endl;\n\tcout << endl;*/\n\n\t// ---------------------------------------- Step 1: 式を圧縮する ---------------------------------\n\tint px = (1 << 30), py = (1 << 30), qx = 0, qy = 0;\n\tfor (int i = 0; i < A.size(); i++) {\n\t\tfor (int j = 0; j < A[0].size(); j++) {\n\t\t\tif (A[i][j] == '.') continue;\n\t\t\tpx = min(px, i); py = min(py, j);\n\t\t\tqx = max(qx, i); qy = max(qy, j);\n\t\t}\n\t}\n\n\t// ---------------------------------------- Step 2: パースする -----------------------------------\n\tvector<string>B;\n\tfor (int i = px; i <= qx; i++) B.push_back(A[i].substr(py, qy - py + 1));\n\n\tint BASE = 0, H = B.size(), W = B[0].size();\n\tfor (int i = 0; i < H; i++) {\n\t\tif (B[i][0] != '.') { BASE = i; }\n\t}\n\n\tif (H == 1 && W == 1) return (B[0][0] - '0');\n\n\tvector<pair<int, int>>vec; int depth = 0; bool IsDiv = false;\n\tfor (int i = 0; i < W; i++) {\n\t\tif (B[BASE][i] == '(') depth++;\n\t\tif (B[BASE][i] == ')') depth--;\n\t\tif (depth == 0) {\n\t\t\tif (B[BASE][i] == '+') vec.push_back(make_pair(i, 1));\n\t\t\tif (B[BASE][i] == '-' && (i != W - 1 && B[BASE][i + 1] != '-') && (i != 0 && B[BASE][i - 1] != '-')) {\n\t\t\t\tbool ok = false;\n\t\t\t\tif (i == 0) ok = true;\n\t\t\t\tif (i >= 2) {\n\t\t\t\t\tif (B[BASE][i - 2] == '+') ok = true;\n\t\t\t\t\tif (B[BASE][i - 2] == '*') ok = true;\n\t\t\t\t\tif (B[BASE][i - 2] == '-' && (i == 2 || B[BASE][i - 3] != '-')) ok = true;\n\t\t\t\t}\n\t\t\t\tif (ok == false) vec.push_back(make_pair(i, 2));\n\t\t\t}\n\t\t\tif (B[BASE][i] == '-') IsDiv = true;\n\t\t}\n\t}\n\n\t// ---------------------------------------- Step 3: '+', '-' が存在する場合 ----------------------\n\tif (vec.size() >= 1) {\n\t\tvector<pair<int, int>>vec3;\n\t\tvec3.push_back(make_pair(-1, 1));\n\t\tfor (int i = 0; i < vec.size(); i++) vec3.push_back(vec[i]);\n\t\tvec3.push_back(make_pair(W, 1));\n\n\t\tbool ok = false; int res = 0;\n\t\tfor (int i = 1; i < vec3.size(); i++) {\n\t\t\tif (vec3[i].first - vec3[i - 1].first == 2) {\n\t\t\t\tif (vec3[i].second == 2) ok ^= true;\n\t\t\t}\n\t\t\telse {\n\t\t\t\tvector<string>A3;\n\t\t\t\tfor (int j = 0; j < H; j++) A3.push_back(B[j].substr(vec3[i - 1].first + 1, (vec3[i].first - 1) - (vec3[i - 1].first + 1) + 1));\n\n\t\t\t\tint rem = solve(A3);\n\t\t\t\tif (ok == false) res += rem; else res -= rem;\n\t\t\t\tres += mod; res %= mod;\n\n\t\t\t\tif (vec3[i].second == 1) ok = false;\n\t\t\t\telse ok = true;\n\t\t\t}\n\t\t}\n\t\treturn res;\n\t}\n\n\t// ---------------------------------------- Step 3.5: マイナスの処理 -----------------------------\n\tif (B[BASE][0] == '-' && (W >= 2 && B[BASE][1] == '.')) {\n\t\tvector<string>A4;\n\t\tfor (int i = 0; i < H; i++) A4.push_back(B[i].substr(1, W - 1));\n\t\tint p1 = solve(A4);\n\t\treturn (mod - p1) % mod;\n\t}\n\n\t// ---------------------------------------- Step 4: パース 2 個目 --------------------------------\n\tvector<int>vec2; depth = 0;\n\tfor (int i = 0; i < W; i++) {\n\t\tif (B[BASE][i] == '(') depth++;\n\t\tif (B[BASE][i] == ')') depth--;\n\t\tif (B[BASE][i] == '*' && depth == 0) vec2.push_back(i);\n\t}\n\n\t// ---------------------------------------- Step 5: '*' が存在する場合 ---------------------------\n\tif (vec2.size() >= 1) {\n\t\tvector<int>vec3; vec3.push_back(-1);\n\t\tfor (int i = 0; i < vec2.size(); i++) vec3.push_back(vec2[i]);\n\t\tvec3.push_back(W);\n\n\t\tint muls = 1;\n\t\tfor (int i = 0; i < vec3.size() - 1; i++) {\n\t\t\tvector<string>A5;\n\t\t\tfor (int j = 0; j < H; j++) A5.push_back(B[j].substr(vec3[i] + 1, (vec3[i + 1] - 1) - (vec3[i] + 1) + 1));\n\t\t\tint ret = solve(A5); muls *= ret; muls %= mod;\n\t\t}\n\t\treturn muls;\n\t}\n\n\t// ---------------------------------------- Step 6: '/' が存在する場合 ---------------------------\n\tif (IsDiv == true) {\n\t\tvector<string>A6a, A6b;\n\t\tfor (int i = 0; i < BASE; i++) A6a.push_back(B[i]);\n\t\tfor (int i = BASE + 1; i < H; i++) A6b.push_back(B[i]);\n\n\t\tint v1 = solve(A6a), v2 = solve(A6b);\n\t\tint rets = Div(v1, v2);\n\t\treturn rets;\n\t}\n\n\t// ---------------------------------------- Step 7: '(' が存在する場合 ---------------------------\n\tif (B[BASE][0] == '(' && B[BASE][W - 1] == ')') {\n\t\tvector<string> A7;\n\t\tfor (int i = 0; i < H; i++) A7.push_back(B[i].substr(1, W - 2));\n\t\treturn solve(A7);\n\t}\n\n\t// ---------------------------------------- Step 8: その他の場合(累乗) -------------------------\n\tint pows = (B[BASE - 1][W - 1] - '0');\n\tvector<string>A8;\n\tfor (int i = 0; i < H; i++) A8.push_back(B[i].substr(0, W - 1));\n\tint C8 = solve(A8);\n\treturn modpow(C8, pows);\n}\n\nint main() {\n\tinit();\n\twhile (true) {\n\t\tint n; cin >> n; if (n == 0) break;\n\t\tvector<string>vec;\n\t\tfor (int i = 0; i < n; i++) { string str; cin >> str; vec.push_back(str); }\n\n\t\tcout << solve(vec) << endl;\n\t}\n\treturn 0;\n}", "accuracy": 1, "time_ms": 60, "memory_kb": 34660, "score_of_the_acc": -1.122, "final_rank": 9 }, { "submission_id": "aoj_1322_1060386", "code_snippet": "#include<iostream>\n#include<cstring>\n\nusing namespace std;\n\nconst int M=2011;\nint inv[M+1];\nchar g[20][82];\nint cur;\n\nint expr(int,int,int,int);\n\nchar gcic(int c,int t,int b=99){\n for(;t<b;t++){\n if(g[t][c]!='.')return g[t][c];\n }\n return '.';\n}\n\nint sexpr(int t,int l,int b,int r){\n while(gcic(l,t,b)=='.'){\n l++;\n }\n cur=l;\n while(gcic(r-1,t,b)=='.'){\n r--;\n }\n return expr(t,l,b,r);\n}\n\nint factor(int t,int l,int b,int r){\n if(gcic(l,t)=='-'){\n if(gcic(l+1,t,b)=='.'){\n return (M-factor(t,l+2,b,r))%M;\n }else{\n int v;\n for(v=t;g[v][l]!='-';v++);\n int vr;\n for(vr=l;g[v][vr]=='-';vr++);\n int rv=(sexpr(t,l,v,vr)*inv[sexpr(v+1,l,b,vr)])%M;\n cur=vr;\n return rv;\n }\n }else{\n char c=gcic(l,t);\n int prm;\n if(c=='('){\n prm=expr(t,l+2,b,r-2);\n cur+=2;\n }else{\n prm=c-'0';\n cur=l+1;\n }\n char pc=gcic(cur,t,b);\n if(pc=='.'){\n return prm;\n }else{\n int rv=1;\n int p=pc-'0';\n cur++;\n while(p--){\n\trv=rv*prm%M;\n }\n return rv;\n }\n }\n}\n\nint term(int t,int l,int b,int r){\n int rv=factor(t,l,b,r);\n while(gcic(cur+1,t,b)=='*'){\n rv=(rv*factor(t,cur+3,b,r))%M;\n }\n return rv;\n}\n\nint expr(int t,int l,int b,int r){\n int rv=term(t,l,b,r);\n for(;;){\n char op=gcic(cur+1,t,b);\n if(op!='+'&&op!='-')break;\n int f=(op=='+')?1:-1;\n rv=(rv+f*term(t,cur+3,b,r)+M)%M;\n }\n return rv;\n}\n\nint main(){\n for(int i=1;i<=M;i++){\n for(int j=0;j<=M;j++){\n if(i*j%M==1){\n\tinv[i]=j;\n }\n }\n }\n int len;\n for(int n;cin>>n,n;){\n for(int i=0;i<n;i++){\n cin>>g[i];\n len=strlen(g[i]);\n for(int j=len;j<82;j++){\n\tg[i][j]='.';\n }\n }\n cur=0;\n cout<<expr(0,0,n,len)<<endl;\n }\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 1180, "score_of_the_acc": -0.034, "final_rank": 4 }, { "submission_id": "aoj_1322_1013452", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\nstring S[111];\nint id;\nint N;\nint L;\n\n#define MOD (2011)\n\nint Y[2012];\nvoid init(){\n for(int i=0;i<2011;i++){\n for(int j=0;j<2011;j++){\n if( i * j % MOD == 1 ) Y[i] = j;\n }\n }\n}\nint expr(int top,int bottom);\nint term(int top,int bottom);\nint factor(int top,int bottom);\nint powexpr(int top,int bottom);\nint primary(int top,int bottom);\nint fraction(int top,int bottom);\nint digit(int top,int bottom);\n\nint mod(int a,int m){\n //if( abs(a) < m ) return a;\n int ret = a%m;\n return ret<0?MOD+ret:ret;\n}\n\nint maxpow(int a,int b){\n int x = a;\n int ret = 1;\n while( b > 0 ){\n if( b & 1 ) ret = mod(ret*x,MOD);\n x = mod(x*x,MOD);\n b>>=1;\n }\n return ret;\n}\n\n\n\nint check(int top,int bottom,int index){\n while( index < L ){\n id = index;\n for(int i=top;i<=bottom;i++){\n if( S[i][index] != '.' ) return i;\n }\n ++index;\n }\n return -1;\n}\n\nvoid print(string s,int top,int bottom,int base){\n printf(\"%s top( %d ), bottom( %d ), base( %d ), id ( %d ), S[base][id]= %c \\n\\n\",\n\t s.c_str(),top,bottom,base,id,base==-1?'e':S[base][id]);\n}\n\nint expr(int top,int bottom) {\n int base = check(top,bottom,id); \n //print(\"expr\",top,bottom,base);\n if( base == -1 ) return 2012;\n int t = term(top,bottom);\n while( id < L && S[base][id] == '.' ){\n int tmp_id = id;\n id++;\n if( id < L && (S[base][id] == '+' || S[base][id] == '-') ){\n char op = S[base][id];\n id++;\n if( id < L && S[base][id] == '.' ){\n\tid++;\n\tint r = term(top,bottom);\n\t//print(\"expr return\",top,bottom,base);\n\t//printf(\" t = %d , r = %d\\n\",t,r);\n\tif( op == '+' ) t += r;\n\tif( op == '-' ) t -= r;\n\tt = mod(t,MOD);\n } else {\n\tid = tmp_id;\n\tbreak;\n }\n } else {\n id = tmp_id;\n break;\n }\n }\n return t;\n}\nint term(int top,int bottom) {\n int base = check(top,bottom,id);\n //print(\"term\",top,bottom,base);\n if( base == -1 ) return 2012;\n int t = factor(top,bottom);\n //print(\"term retrun\",top,bottom,base);\n //printf(\"t = %d\\n\",t);\n while( id < L && S[base][id] == '.' ){\n int tmp_id = id;\n id++;\n if( id < L && S[base][id] == '*' ){\n id++;\n if( id < L && S[base][id] == '.' ){\n\tid++;\n\tint r = factor(top,bottom);\n\t//print(\"term multi retrun\",top,bottom,base);\n\t//printf(\"t = %d\\n\",t);\n\tt = mod(t * r , MOD);\n\t//printf(\"r = %d t = %d\\n\",r,t);\n } else {\n\tid = tmp_id;\n\tbreak;\n }\n } else {\n id = tmp_id;\n break;\n }\n }\n return t;\n}\n\nint factor(int top,int bottom){\n int base = check(top,bottom,id);\n //print(\"factor\",top,bottom,base);\n if( base == -1 ) return 2012;\n int tmp_id;\n int t=2012;\n bool f = false;\n tmp_id = id;\n t = powexpr(top,bottom);\n if( t > 2011 ) {\n id = tmp_id;\n t = fraction(top,bottom);\n }\n if( t > 2011 ) {\n id = tmp_id; \n if( id+1 < L && S[base][id] == '-' && S[base][id+1] == '.' ) {\n id +=2;\n f = true;\n t = factor(top,bottom);\n } \n }\n if( f ) t *= -1;\n return mod(t,MOD); \n}\nint powexpr(int top,int bottom){\n int base = check(top,bottom,id);\n //print(\"powexpr\",top,bottom,base);\n if( base == -1 ) return 2012;\n int t = 2012;\n t = primary(top,bottom);\n if ( t == 2012 ) return 2012;\n if( id < L && base >0 && isdigit(S[base-1][id]) ){\n int r = digit(base-1,base-1);\n t = maxpow(t,r);\n //print(\"powexpr return \",top,bottom,base);\n //printf(\"t = %d\\n\",t);\n }\n return t;\n}\n\nint primary(int top,int bottom){\n int base = check(top,bottom,id);\n //print(\"primary\",top,bottom,base);\n if( base == -1 ) return 2012;\n int tmp_id = id;\n int t = digit(top,bottom);\n if ( t == 2012 ){\n id = tmp_id;\n if( id+1 < L && S[base][id] == '(' && S[base][id+1] == '.' ){\n id += 2;\n t = expr(top,bottom);\n if( t == 2012 ) return 2012;\n //print(\"primary return\",top,bottom,base);\n //printf(\"t = %d\\n\",t); \n id +=2;\n } else {\n return t;\n }\n }\n return t;\n}\n\nint fraction(int top,int bottom){\n int base = check(top,bottom,id);\n //print(\"fraction\",top,bottom,base);\n if( base == -1 ) return 2012;\n int t = 2012;\n if( id+2 < L && S[base][id] == '-' && S[base][id+1] == '-' && S[base][id+2] == '-' ){\n id++;\n int tmp_id = id;\n int p = expr(top,base-1);\n id = tmp_id;\n if( p == 2012 ) return 2012;\n int q = expr(base+1,bottom);\n if( q == 2012 ) return 2012;\n id = max(id,tmp_id);\n while( id < L && S[base][id] == '-' ) id++;\n //print(\"fraction return\",top,bottom,base);\n //printf(\"%d * Y[%d] = %d\\n\",p,q,Y[abs(q)]);\n t = mod(p*Y[abs(q)]*(q<0?-1:1),MOD);\n }\n return t;\n}\nint digit(int top,int bottom){\n int base = check( top,bottom,id );\n //print(\"digit\",top,bottom,base);\n if( base == -1 ) return 2012;\n int t=0;\n bool f = false;\n while( id < L && isdigit(S[base][id]) ){\n f = true;\n t *= 10;\n t += S[base][id]-'0';\n id++;\n }\n if( f ) return mod(t,MOD);\n return 2012;\n}\n\n\n\nint main(){\n init();\n while( cin >> N && N ){\n for(int i=0;i<N;i++){\n cin >> S[i];\n }\n id = 0;\n L = (int)S[0].size();\n int res = mod(expr(0,N-1),MOD);\n if( res < 0 ) res = 2011+res;\n cout << res << endl;\n }\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 1220, "score_of_the_acc": -0.0352, "final_rank": 5 }, { "submission_id": "aoj_1322_657899", "code_snippet": "#include <iostream>\n#include <cstdio>\n#include <vector>\n#include <complex>\n#include <algorithm>\n#include <set>\n#include <map>\n#include <queue>\n#include <string>\n#include <cstring>\n#include <stack>\n#include <cmath>\n#include <iomanip>\n#include <sstream>\n#include <cassert>\n\nusing namespace std;\n\ntypedef long long ll;\ntypedef ll li;\ntypedef pair<int,int> PI;\n#define EPS (1e-10L)\n#define rep(i,n) for(int i=0;i<(int)(n);++i)\n#define REP(i, n) rep (i, n)\n#define F first\n#define S second\n#define mp(a,b) make_pair(a,b)\n#define pb(a) push_back(a)\n#define SZ(a) (int)((a).size())\n#define ALL(a) a.begin(),a.end()\n#define CLR(a) memset((a),0,sizeof(a))\n#define FOR(it,a) for(__typeof(a.begin())it=a.begin();it!=a.end();++it)\ntemplate<typename T,typename U> ostream& operator<< (ostream& out, const pair<T,U>& val){return out << \"(\" << val.F << \", \" << val.S << \")\";}\ntemplate<class T> ostream& operator<< (ostream& out, const vector<T>& val){out << \"{\";rep(i,SZ(val)) out << (i?\", \":\"\") << val[i];return out << \"}\";}\nvoid pkuassert(bool t){t=1/t;};\nint dx[]={0,1,0,-1,1,1,-1,-1};\nint dy[]={1,0,-1,0,-1,1,1,-1};\n\nint n;\nstring in[20];\nint inv[3000];\n\nvoid normal(int &x1,int &y1,int &x2,int &y2){\n set<int> x,y;\n for(int i=x1;i<x2;++i)\n for(int j=y1;j<y2;++j){\n if(in[i][j]!='.'){\n x.insert(i);\n y.insert(j);\n }\n }\n x1 = *x.begin();\n x2 = *x.rbegin()+1;\n y1 = *y.begin();\n y2 = *y.rbegin()+1;\n}\n\nint expr(int,int,int,int);\nint term(int,int,int,int);\nint factor(int,int,int,int);\nint powexpr(int,int,int,int);\nint primary(int,int,int,int);\n\nint digit(int x1,int y1,int x2,int y2){\n normal(x1,y1,x2,y2);\n //cout << \"digit \" << x1 << ' ' << y1 << ' ' << x2 << ' ' << y2 << endl; \n int ret=0;\n while(y1<y2 && isdigit(in[x1][y1]))\n ret=(ret*10+in[x1][y1++]-'0')%2011;\n return ret;\n}\n\nint primary(int x1,int y1,int x2,int y2){\n normal(x1,y1,x2,y2);\n //cout << \"primary \" << x1 << ' ' << y1 << ' ' << x2 << ' ' << y2 << endl;\n int base;\n for(int j=x1;j<x2;++j)\n if(in[j][y1]!='.') base=j;\n if(in[base][y1]=='(')\n return expr(x1,y1+1,x2,y2-1);\n return digit(x1,y1,x2,y2);\n}\n\nint fraction(int x1,int y1,int x2,int y2){\n normal(x1,y1,x2,y2);\n int base;\n for(int j=x1;j<x2;++j)\n if(in[j][y1]!='.') base=j;\n int p=expr(base+1,y1,x2,y2);\n int c=expr(x1,y1,base,y2);\n return c*inv[p]%2011;\n}\n\nint powexpr(int x1,int y1,int x2,int y2){\n normal(x1,y1,x2,y2);\n //cout << \"powexpr \" << x1 << ' ' << y1 << ' ' << x2 << ' ' << y2 << endl;\n int base;\n int base2=-1;\n for(int j=x1;j<x2;++j)\n if(in[j][y1]!='.') base=j;\n for(int j=x1;j<x2;++j)\n if(in[j][y2-1]!='.') base2=j;\n //cout << base << ' ' << base2 << endl;\n if(base2==base) return primary(x1,y1,x2,y2);\n\n int idx=y2-1;\n while(idx>=y1 && isdigit(in[base2][idx])) --idx;\n ++idx;\n int di=digit(base2,idx,base2+1,y2);\n int pr=primary(x1,y1,x2,idx);\n //cout << \"pr di \" << pr << ' ' << di << endl;\n int ret=1;\n rep(i,di) ret=(ret*pr)%2011;\n return ret;\n}\n\nint factor(int x1,int y1,int x2,int y2){\n normal(x1,y1,x2,y2);\n //cout << \"factor \" << x1 << ' ' << y1 << ' ' << x2 << ' ' << y2 << endl; \n int ret;\n int base;\n for(int j=x1;j<x2;++j)\n if(in[j][y1]!='.') base=j;\n\n if(in[base][y1]=='-' &&\n y1+1<y2 && in[base][y1+1]=='.')\n return ((-factor(x1,y1+2,x2,y2))%2011+2011)%2011;\n\n if(in[base][y1]=='-' &&\n y1+1<y2 && in[base][y1+1]=='-')\n return fraction(x1,y1,x2,y2);\n\n return powexpr(x1,y1,x2,y2);\n}\n\nint term(int x1,int y1,int x2,int y2){\n normal(x1,y1,x2,y2);\n int ret;\n int base;\n //cout << \"term \" << x1 << ' ' << y1 << ' ' << x2 << ' ' << y2 << endl;\n for(int j=x1;j<x2;++j)\n if(in[j][y1]!='.') base=j;\n int idx=-1;\n int pa=0;\n for(int j=y1;j<y2;++j){\n if(in[base][j]=='(') ++pa;\n if(in[base][j]==')') --pa;\n if(j-1>=y1 && j+1<y2 && in[base][j-1]=='.' &&\n in[base][j]=='*' &&\n in[base][j+1]=='.' && pa==0){\n idx=j;\n break; \n }\n }\n \n if(idx!=-1) ret=factor(x1,y1,x2,idx-1);\n else return factor(x1,y1,x2,y2);\n y1 = idx+1;\n while(true){\n\n normal(x1,y1,x2,y2);\n idx=-1;\n int pa=0;\n \n for(int j=y1;j<y2;++j){\n if(in[base][j]=='(') ++pa;\n if(in[base][j]==')') --pa;\n if(j-1>=y1 && j+1<y2 && in[base][j-1]=='.' &&\n in[base][j]=='*' &&\n in[base][j+1]=='.' && pa==0){\n idx=j;\n break;\n }\n }\n \n if(idx!=-1){\n int t=factor(x1,y1,x2,idx-1);\n ret=(ret*t)%2011;\n }else{\n int t=factor(x1,y1,x2,y2);\n ret=(ret*t)%2011;\n return ret;\n }\n y1 = idx+1;\n }\n return ret;\n}\n\nint expr(int x1,int y1,int x2,int y2){\n normal(x1,y1,x2,y2);\n int ret;\n int base;\n /*\n cout << \"expr \" << x1 << ' ' << y1 << ' ' << x2 << ' ' << y2 << endl;\n for(int i=x1;i<x2;++i){\n for(int j=y1;j<y2;++j) cout << in[i][j];\n cout << endl;\n }\n */\n for(int j=x1;j<x2;++j)\n if(in[j][y1]!='.') base=j;\n int idx=-1;\n int pa=0;\n for(int j=y1;j<y2;++j){\n if(in[base][j]=='(') ++pa;\n if(in[base][j]==')') --pa;\n if(j-1>=y1 && j+1<y2 && in[base][j-1]=='.' &&\n (in[base][j]=='+' ||\n (in[base][j]=='-' &&\n (j-2>=0 && in[base][j-2]!='*' &&\n in[base][j-2]!='+' &&\n (in[base][j-2]!='-' || (j-3>=0 && in[base][j-3]=='-'))))) &&\n in[base][j+1]=='.' && pa==0){\n idx=j;\n break;\n }\n }\n \n if(idx!=-1) ret=term(x1,y1,x2,idx-1);\n else return term(x1,y1,x2,y2);\n y1 = idx+1;\n while(true){\n normal(x1,y1,x2,y2);\n char op=in[base][idx];\n idx=-1;\n int pa=0;\n for(int j=y1;j<y2;++j){\n if(in[base][j]=='(') ++pa;\n if(in[base][j]==')') --pa;\n if(j-1>=y1 && j+1<y2 && in[base][j-1]=='.' &&\n (in[base][j]=='+' ||\n (in[base][j]=='-' &&\n (j-2>=0 && in[base][j-2]!='*' &&\n in[base][j-2]!='+' &&\n (in[base][j-2]!='-' || (j-3>=0 && in[base][j-3]=='-'))))) &&\n in[base][j+1]=='.' && pa==0){\n idx=j;\n break; \n }\n }\n \n if(idx!=-1){\n int t=term(x1,y1,x2,idx-1);\n if(op=='+') ret=(ret+t)%2011;\n else ret=((ret-t)%2011+2011)%2011;\n }\n else{\n int t=term(x1,y1,x2,y2);\n //cout << op << ' ' << t << endl;\n if(op=='+') ret=(ret+t)%2011;\n else ret=((ret-t)%2011+2011)%2011;\n return ret;\n }\n y1 = idx+1; \n }\n return ret;\n}\n\nvoid solve(){\n rep(i, n) cin >> in[i];\n //rep(i, n) cout << in[i] << endl;\n cout << expr(0,0,n,SZ(in[0])) << endl;\n}\n\nint main(int argc, char *argv[])\n{\n rep(i,2011)rep(j,2011) if(i*j%2011==1) inv[i]=j;\n //cout << 4*inv[9] << endl;\n while(cin >> n && n) solve();\n return 0;\n}", "accuracy": 1, "time_ms": 20, "memory_kb": 1232, "score_of_the_acc": -0.0599, "final_rank": 6 }, { "submission_id": "aoj_1322_423240", "code_snippet": "#include <iostream>\n#include <cstring>\n#include <string>\nusing namespace std;\n\nconst int MOD = 2011;\nint inv[2022];\nchar AA[100][100];\nbool visited[100][100];\n\nclass Result\n{\npublic:\n\tint x,y,v;\n\tResult()\n\t{}\n\n\tResult(int x, int y, int v)\n\t\t:x(x),y(y),v(v)\n\t{}\n};\n\nint findexpr(int x, int y, int d)\n{\n\twhile(1) {\n\t\tif(y < 0 || AA[y][x] == '!') break;\n\t\tif(visited[y][x]) break;\n\t\tif(AA[y][x] !='.') return y;\n\n\t\ty += d;\n\t}\n\n\treturn -1;\n}\n\nResult expr(int x, int y);\nResult term(int x, int y);\nResult factor(int x, int y);\nResult powexpr(int x, int y);\nResult primary(int x, int y);\nResult fraction(int x, int y);\nResult digit(int x, int y);\n\nResult expr(int x, int y)\n{\n\tvisited[y][x] = true;\n\tResult a = term(x,y);\n\tx = a.x; y = a.y;\n\n\twhile(AA[y][x+1] == '+' || AA[y][x+1] == '-') {\n\t\tvisited[y][x+1] = true;\n\t\tchar op = AA[y][x+1];\n\n\t\tResult b = term(x + 3, y);\n\t\tx = b.x; y = b.y;\n\t\t\n\t\tif(op == '+') a.v = (a.v + b.v) % MOD;\n\t\tif(op == '-') a.v = (a.v - b.v + MOD) % MOD;\n\t}\n\n\treturn Result(x,y,a.v);\n}\n\nResult term(int x, int y)\n{\n\tvisited[y][x] = true;\n\n\tResult a = factor(x, y);\n\tx = a.x; y = a.y;\n\n\twhile(AA[y][x+1] == '*') {\n\t\tvisited[y][x+1] = true;\n\n\t\tResult b = factor(x + 3, y);\n\t\tx = b.x; y = b.y;\n\t\ta.v *= b.v;\n\t\ta.v %= MOD;\n\t}\n\n\treturn Result(x,y,a.v);\n}\n\nResult factor(int x, int y)\n{\n\tvisited[y][x] = true;\n\n\tResult a;\n\tif(AA[y][x] == '-') {\n\t\tif(AA[y][x+1] == '-') {\n\t\t\ta = fraction(x, y);\n\t\t}\n\t\telse {\n\t\t\ta = factor(x + 2, y);\n\t\t\ta.v = (-a.v + MOD) % MOD;\n\t\t}\n\t}\n\telse {\n\t\ta = powexpr(x, y);\n\t}\n\n\treturn a;\n}\n\nResult powexpr(int x, int y)\n{\n\tvisited[y][x] = true;\n\n\tResult a = primary(x,y);\n\tx = a.x; y = a.y;\n\n\tif(y!=0 && isdigit(AA[y-1][x])) {\n\t\tint pw = AA[y-1][x] - '0';\n\t\tint p = a.v;\n\t\tint res = 1;\n\t\tfor(int i=0; i<pw; i++) {\n\t\t\tres *= p;\n\t\t\tres %= MOD;\n\t\t}\n\n\t\ta.v = res % MOD;\n\t\ta.x += 1;\n\t}\n\n\treturn a;\n}\n\nResult primary(int x, int y)\n{\n\tvisited[y][x] = true;\n\n\tResult a;\n\tif(isdigit(AA[y][x])) {\n\t\ta.v = AA[y][x] - '0';\n\t\ta.x = x + 1;\n\t\ta.y = y;\n\t}\n\telse {\n\t\ta = expr(x + 2,y);\n\t\ta.x += 2;\n\t}\n\n\treturn a;\n}\n\nResult fraction(int x, int y)\n{\n\tbool findTop=false, findBtm=false;\n\tResult t, b;\n\n\tint tx = x;\n\twhile(AA[y][x] == '-') {\n\t\tvisited[y][x] = true;\n\t\tx++;\n\t}\n\n\tx = tx;\n\twhile(AA[y][x] == '-') {\n\t\tif(!findTop) {\n\t\t\tint ty = findexpr(x,y-1,-1);\n\t\t\tif(ty != -1) {\n\t\t\t\tt = expr(x,ty);\n\t\t\t\tfindTop = true;\n\t\t\t}\n\t\t}\n\t\tif(!findBtm) {\n\t\t\tint by = findexpr(x,y+1,1);\n\t\t\tif(by != -1) {\n\t\t\t\tb = expr(x,by);\n\t\t\t\tfindBtm = true;\n\t\t\t}\n\t\t}\n\n\t\tx++;\n\t}\n\n\tResult res;\n\tres.x = x;\n\tres.y = y;\n\tres.v = t.v * inv[b.v] % MOD;\n\n\treturn res;\n}\n\nint main()\n{\n\tfor(int i=1; i<MOD; i++)\n\tfor(int j=1; j<MOD; j++) \n\t\tif(j * i % MOD == 1) inv[i] = j;\n\n\tint N;\n\twhile(cin >> N, N) {\n\t\tmemset(visited, 0, sizeof(visited));\n\t\tfor(int i=0; i<100; i++)\n\t\tfor(int j=0; j<100; j++)\n\t\t\tAA[j][i] = '!';\n\n\t\tfor(int i=0; i<N; i++)\n\t\t\tcin >> AA[i];\n\n\t\tcout << expr(0, findexpr(0,0,1)).v << endl;\n\t}\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 0, "score_of_the_acc": 0, "final_rank": 1 }, { "submission_id": "aoj_1322_413544", "code_snippet": "#include <iostream>\n#include <string>\n#include <vector>\n\nusing namespace std;\n\nstatic int INV[2011] = { 0 };\n\nint mod(int x){\n\twhile(x < 0){ x += 2011; }\n\treturn x % 2011;\n}\n\nstruct Result {\n\tint p, v;\n};\n\n#define PARSER_DECL(name) \\\n\tResult name ( \\\n\t\tconst vector<string> &s, int left, int top, int right, int bottom)\n\nPARSER_DECL(expr);\nPARSER_DECL(term);\nPARSER_DECL(factor);\nPARSER_DECL(powexpr);\nPARSER_DECL(primary);\nPARSER_DECL(fraction);\nPARSER_DECL(digit);\n\nint calculateBase(\n\tconst vector<string> &s, int left, int top, int right, int bottom)\n{\n\tfor(int i = left; i < right; ++i){\n\t\tfor(int j = top; j < bottom; ++j){\n\t\t\tif(s[j][i] != '.'){ return j; }\n\t\t}\n\t}\n\treturn -1;\n}\n\nPARSER_DECL(expr){\n\tint base = calculateBase(s, left, top, right, bottom);\n\tResult r = term(s, left, top, right, bottom);\n\tint next = r.p + 1;\n\twhile(next < right && (s[base][next] == '+' || s[base][next] == '-')){\n\t\tr.p += 3;\n\t\tResult q = term(s, r.p, top, right, bottom);\n\t\tif(s[base][next] == '+'){\n\t\t\tr.v = mod(r.v + q.v);\n\t\t}else{\n\t\t\tr.v = mod(r.v - q.v);\n\t\t}\n\t\tr.p = q.p;\n\t\tnext = q.p + 1;\n\t}\n\treturn r;\n}\nPARSER_DECL(term){\n\tint base = calculateBase(s, left, top, right, bottom);\n\tResult r = factor(s, left, top, right, bottom);\n\tint next = r.p + 1;\n\twhile(next < right && s[base][next] == '*'){\n\t\tr.p += 3;\n\t\tResult q = factor(s, r.p, top, right, bottom);\n\t\tr.v = mod(r.v * q.v);\n\t\tr.p = q.p;\n\t\tnext = q.p + 1;\n\t}\n\treturn r;\n}\nPARSER_DECL(factor){\n\tint base = calculateBase(s, left, top, right, bottom);\n\tint numNeg = 0, p = left;\n\twhile(s[base][p] == '-' && s[base][p + 1] == '.'){\n\t\t++numNeg;\n\t\tp += 2;\n\t}\n\tif(s[base][p] == '-'){\n\t\tResult r = fraction(s, p, top, right, bottom);\n\t\tif(numNeg % 2 == 1){ r.v = mod(-r.v); }\n\t\treturn r;\n\t}else{\n\t\tResult r = powexpr(s, p, top, right, bottom);\n\t\tif(numNeg % 2 == 1){ r.v = mod(-r.v); }\n\t\treturn r;\n\t}\n}\nPARSER_DECL(powexpr){\n\tint base = calculateBase(s, left, top, right, bottom);\n\tResult r = primary(s, left, top, right, bottom);\n\tif(base > 0 && r.p < right && isdigit(s[base - 1][r.p])){\n\t\tResult q = digit(s, r.p, top, right, bottom);\n\t\tint v = 1;\n\t\tfor(int i = 0; i < q.v; ++i){ v = mod(v * r.v); }\n\t\tr.v = v;\n\t\tr.p = q.p;\n\t}\n\treturn r;\n}\nPARSER_DECL(primary){\n\tint base = calculateBase(s, left, top, right, bottom);\n\tif(isdigit(s[base][left])){\n\t\tResult r = digit(s, left, top, right, bottom);\n\t\treturn r;\n\t}else{\n\t\tResult r = expr(s, left + 2, top, right, bottom);\n\t\tr.p += 2;\n\t\treturn r;\n\t}\n}\nPARSER_DECL(fraction){\n\tint base = calculateBase(s, left, top, right, bottom);\n\tint length = 0;\n\tfor(int i = left; i < right; ++i, ++length){\n\t\tif(s[base][i] != '-'){ break; }\n\t}\n\tint upperBegin = left;\n\tfor(int i = 0; i < length; ++i, ++upperBegin){\n\t\tbool flag = false;\n\t\tfor(int j = top; j < base; ++j){\n\t\t\tflag = flag || (s[j][upperBegin] != '.');\n\t\t}\n\t\tif(flag){ break; }\n\t}\n\tint lowerBegin = left;\n\tfor(int i = 0; i < length; ++i, ++lowerBegin){\n\t\tbool flag = false;\n\t\tfor(int j = base + 1; j < bottom; ++j){\n\t\t\tflag = flag || (s[j][lowerBegin] != '.');\n\t\t}\n\t\tif(flag){ break; }\n\t}\n\tResult upper = expr(s, upperBegin, top, right, base);\n\tResult lower = expr(s, lowerBegin, base + 1, right, bottom);\n\tResult r;\n\tr.v = mod(upper.v * INV[lower.v]);\n\tr.p = left + length;\n\treturn r;\n}\nPARSER_DECL(digit){\n\tint base = calculateBase(s, left, top, right, bottom);\n\tResult r;\n\tr.v = s[base][left] - '0';\n\tr.p = left + 1;\n\treturn r;\n}\n\nint main(){\n\tfor(int y = 1; y < 2011; ++y){\n\t\tfor(int z = 1; z < 2011; ++z){\n\t\t\tif(z * y % 2011 == 1){ INV[y] = z; }\n\t\t}\n\t}\n\twhile(true){\n\t\tint n;\n\t\tcin >> n;\n\t\tif(n == 0){ break; }\n\t\tvector<string> s(n);\n\t\tfor(int i = 0; i < n; ++i){ cin >> s[i]; }\n\t\tResult r = expr(s, 0, 0, s[0].size(), n);\n\t\tcout << r.v << endl;\n\t}\n\treturn 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 0, "score_of_the_acc": 0, "final_rank": 1 }, { "submission_id": "aoj_1322_304384", "code_snippet": "#include<cctype>\n#include<cstdio>\n#include<cassert>\n#include<cstring>\n\n#define rep(i,n) for(int i=0;i<(n);i++)\n\nusing namespace std;\n\nint inv[2011];\nchar E[20][81];\n\nint parse(int t,int l,int b,int r);\nint expr(int &idx,int y,int y1,int y2);\nint term(int &idx,int y,int y1,int y2);\nint factor(int &idx,int y,int y1,int y2);\nint powexpr(int &idx,int y,int y1,int y2);\nint primary(int &idx,int y,int y1,int y2);\nint fraction(int &idx,int y,int y1,int y2);\n\nint expr(int &idx,int y,int y1,int y2){\n\tint a=term(idx,y,y1,y2);\n\twhile(1){\n\t\tchar c=E[y][idx];\n\t\tif(c!='+' && c!='-') break;\n\t\tidx+=2;\n\t\tint b=term(idx,y,y1,y2);\n\t\tif(c=='+') a=(a+b)%2011;\n\t\tif(c=='-') a=(a-b+2011)%2011;\n\t}\n\treturn a;\n}\n\nint term(int &idx,int y,int y1,int y2){\n\tint a=factor(idx,y,y1,y2);\n\twhile(1){\n\t\tchar c=E[y][idx];\n\t\tif(c!='*') break;\n\n\t\tidx+=2;\n\t\tint b=factor(idx,y,y1,y2);\n\t\ta=a*b%2011;\n\t}\n\treturn a;\n}\n\nint factor(int &idx,int y,int y1,int y2){\n\tif(E[y][idx]=='-' && E[y][idx+1]!='-'){\n\t\tidx+=2;\n\t\treturn (-factor(idx,y,y1,y2)+2011)%2011;\n\t}\n\n\tif(E[y][idx]=='-' && E[y][idx+1]=='-'){\n\t\treturn fraction(idx,y,y1,y2);\n\t}\n\n\treturn powexpr(idx,y,y1,y2);\n}\n\nint powexpr(int &idx,int y,int y1,int y2){\n\tint a=primary(idx,y,y1,y2);\n\tif(y>0 && isdigit(E[y-1][idx-1])){\n\t\tint b=E[y-1][idx-1]-'0',c=1;\n\t\trep(i,b) c=c*a%2011;\n\t\ta=c;\n\t\tidx++;\n\t}\n\treturn a;\n}\n\nint primary(int &idx,int y,int y1,int y2){\n\tint a;\n\tif(isdigit(E[y][idx])){\n\t\ta=E[y][idx]-'0';\n\t}\n\telse{\n\t\tidx+=2;\n\t\ta=expr(idx,y,y1,y2);\n\t}\n\tidx+=2;\n\treturn a;\n}\n\nint fraction(int &idx,int y,int y1,int y2){\n\tint x1=idx+1;\n\twhile(E[y][idx]=='-') idx++;\n\tint x2=idx-1;\n\tidx++;\n\treturn parse(y1,x1,y,x2)*inv[parse(y+1,x1,y2,x2)]%2011;\n}\n\nint find_base_line(int t,int l,int b,int r){\n\tfor(int j=l;j<r;j++) for(int i=t;i<b;i++) if(E[i][j]!='.') return i;\n\tassert(0);\n}\n\nint parse(int t,int l,int b,int r){\n\tint y=find_base_line(t,l,b,r);\n\tint idx=l;\n\twhile(E[y][idx]=='.') idx++;\n\treturn expr(idx,y,t,b);\n}\n\nint main(){\n\trep(i,2011) rep(j,2011) if(i*j%2011==1) inv[i]=j;\n\n\tfor(int h;scanf(\"%d\",&h),h;){\n\t\trep(i,h){\n\t\t\trep(j,81) E[i][j]='\\0';\n\t\t\tscanf(\"%s\",E[i]);\n\t\t}\n\t\tprintf(\"%d\\n\",parse(0,0,h,strlen(E[0])));\n\t}\n\n\treturn 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 0, "score_of_the_acc": 0, "final_rank": 1 } ]
aoj_1320_cpp
Problem F: City Merger Recent improvements in information and communication technology have made it possible to provide municipal service to a wider area more quickly and with less costs. Stimulated by this, and probably for saving their not sufficient funds, mayors of many cities started to discuss on mergers of their cities. There are, of course, many obstacles to actually put the planned mergers in practice. Each city has its own culture of which citizens are proud. One of the largest sources of friction is with the name of the new city. All citizens would insist that the name of the new city should have the original name of their own city at least as a part of it. Simply concatenating all the original names would, however, make the name too long for everyday use. You are asked by a group of mayors to write a program that finds the shortest possible name for the new city that includes all the original names of the merged cities. If two or more cities have common parts, they can be overlapped. For example, if "FUKUOKA", "OKAYAMA", and "YAMAGUCHI" cities are to be merged, "FUKUOKAYAMAGUCHI" is such a name that include all three of the original city names. Although this includes all the characters of the city name "FUKUYAMA" in this order, it does not appear as a consecutive substring, and thus "FUKUYAMA" is not considered to be included in the name. Input The input is a sequence of datasets. Each dataset begins with a line containing a positive integer n ( n ≤ 14), which denotes the number of cities to be merged. The following n lines contain the names of the cities in uppercase alphabetical letters, one in each line. You may assume that none of the original city names has more than 20 characters. Of course, no two cities have the same name. The end of the input is indicated by a line consisting of a zero. Output For each dataset, output the length of the shortest possible name of the new city in one line. The output should not contain any other characters. Sample Input 3 FUKUOKA OKAYAMA YAMAGUCHI 3 FUKUOKA FUKUYAMA OKAYAMA 2 ABCDE EDCBA 4 GA DEFG CDDE ABCD 2 ABCDE C 14 AAAAA BBBBB CCCCC DDDDD EEEEE FFFFF GGGGG HHHHH IIIII JJJJJ KKKKK LLLLL MMMMM NNNNN 0 Output for the Sample Input 16 19 9 9 5 70
[ { "submission_id": "aoj_1320_10954681", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\nint n;\nvector<int> ls;\nvector<vector<int>> v; // v[i][j] = overlap length a[i] suffix vs a[j] prefix\nunordered_map<long long,int> cac; // memo for sediv\nunordered_map<int,int> bp; // single-bit -> index\n\nconst int INF = 1000000000;\n\nlong long make_key(int st, int en, int tp) {\n // replicate Python: cachenm = st + en*n + tp*n*n\n long long nn = (long long)n * (long long)n;\n return (long long)tp * nn + (long long)en * n + st;\n}\n\nint sediv(int st, int en, int tp) {\n long long key = make_key(st,en,tp);\n auto it = cac.find(key);\n if (it != cac.end()) return it->second;\n\n if (tp == 0) {\n int tmp = ls[st] + ls[en] - v[st][en];\n cac[key] = tmp;\n return tmp;\n }\n auto itbp = bp.find(tp);\n if (itbp != bp.end()) {\n int a = itbp->second;\n int tmp = ls[st] + ls[a] + ls[en] - v[st][a] - v[a][en];\n cac[key] = tmp;\n return tmp;\n }\n\n int mn = INF;\n for (int i = 0; i < n-1; ++i) {\n if (!(tp & (1<<i))) continue;\n for (int j = i+1; j < n; ++j) {\n if (!(tp & (1<<j))) continue;\n int tp2 = tp & ~( (1<<i) | (1<<j) );\n int tmp1 = sediv(i,j,tp2) - v[st][i] - v[j][en];\n int tmp2 = sediv(j,i,tp2) - v[st][j] - v[i][en];\n int cur = min(tmp1, tmp2);\n if (cur < mn) mn = cur;\n }\n }\n mn += ls[st] + ls[en];\n cac[key] = mn;\n return mn;\n}\n\n// overlap function same as Python sc\nint sc_str_overlap(const string &a, const string &b) {\n int lna = (int)a.size();\n int lnb = (int)b.size();\n for (int i = 0; i < (int)a.size(); ++i) {\n if (lnb + i <= lna) continue;\n int len = lna - i;\n // compare a[i:] with b[:len]\n bool ok = true;\n for (int k = 0; k < len; ++k) {\n if (a[i+k] != b[k]) { ok = false; break; }\n }\n if (ok) return len;\n }\n return 0;\n}\n\n// generate next combination in lexicographic order\n// a: current indices (size k), n: range, returns false if no next\nbool next_combination(vector<int> &a, int n, int k) {\n for (int i = k-1; i >= 0; --i) {\n if (a[i] != i + n - k) {\n ++a[i];\n for (int j = i+1; j < k; ++j) a[j] = a[j-1] + 1;\n return true;\n }\n }\n return false;\n}\n\nint main(){\n ios::sync_with_stdio(false);\n cin.tie(nullptr);\n // prepare bp\n for (int i = 0; i < 15; ++i) bp[1<<i] = i;\n\n while (true) {\n if (!(cin >> n)) return 0;\n if (n == 0) return 0;\n vector<string> s;\n s.reserve(n);\n for (int i = 0; i < n; ++i) {\n string t; cin >> t;\n s.push_back(t);\n }\n\n // dpl: remove strings fully contained in others\n {\n vector<char> removed(n, 0);\n for (int i = 0; i < n; ++i) {\n if (removed[i]) continue;\n for (int j = 0; j < n; ++j) {\n if (i == j || removed[j]) continue;\n if (s[i].size() <= s[j].size()) {\n // check if s[i] is substring of s[j]\n if (s[j].find(s[i]) != string::npos) {\n removed[i] = 1;\n break;\n }\n }\n }\n }\n vector<string> ns;\n for (int i = 0; i < n; ++i) if (!removed[i]) ns.push_back(s[i]);\n s.swap(ns);\n }\n\n n = (int)s.size();\n if (n == 0) { cout << 0 << '\\n'; continue; }\n\n // build v\n v.assign(n, vector<int>(n,0));\n if (n == 1) {\n cout << s[0].size() << '\\n';\n continue;\n }\n for (int i = 0; i < n; ++i) {\n for (int j = 0; j < n; ++j) {\n if (i == j) continue;\n v[i][j] = sc_str_overlap(s[i], s[j]);\n }\n }\n ls.assign(n,0);\n for (int i = 0; i < n; ++i) ls[i] = (int)s[i].size();\n\n if (n <= 3) {\n // brute force permutations to get maximum overlaps sum\n vector<int> perm(n);\n for (int i = 0; i < n; ++i) perm[i] = i;\n int best = 0;\n do {\n int sum = 0;\n for (int i = 0; i+1 < n; ++i) sum += v[perm[i]][perm[i+1]];\n best = max(best, sum);\n } while (next_permutation(perm.begin(), perm.end()));\n int total_len = 0;\n for (int x : ls) total_len += x;\n cout << (total_len - best) << '\\n';\n continue;\n }\n\n cac.clear();\n\n int div2 = n/2;\n int div3 = n - div2;\n int sm = (1<<n);\n\n // prepare initial combination [0,1,...,div2-1]\n vector<int> comb(div2);\n for (int i = 0; i < div2; ++i) comb[i] = i;\n\n int ans = INF;\n\n bool first = true;\n while (true) {\n // comb is current combination in lexicographic order\n if (!first) {\n // already advanced\n }\n first = false;\n\n // replicate Python's: if i[0] != 0: break\n if (comb[0] != 0) break;\n\n int gr0 = 0;\n for (int x : comb) gr0 |= (1<<x);\n int gr1 = (sm - 1) ^ gr0; // complement within n bits\n\n // lga, lgb: store sediv results for start-end pairs\n // use 2D vectors filled with INF\n vector<vector<int>> lga(n, vector<int>(n, INF));\n vector<vector<int>> lgb(n, vector<int>(n, INF));\n\n // for gr0: iterate all pairs i<j that exist in gr0\n for (int i = 0; i < n-1; ++i) {\n if (!(gr0 & (1<<i))) continue;\n for (int j = i+1; j < n; ++j) {\n if (!(gr0 & (1<<j))) continue;\n int msk = gr0 & ~( (1<<i) | (1<<j) );\n lga[i][j] = sediv(i,j,msk);\n lga[j][i] = sediv(j,i,msk);\n }\n }\n\n // for gr1: same\n for (int i = 0; i < n-1; ++i) {\n if (!(gr1 & (1<<i))) continue;\n for (int j = i+1; j < n; ++j) {\n if (!(gr1 & (1<<j))) continue;\n int msk = gr1 & ~( (1<<i) | (1<<j) );\n lgb[i][j] = sediv(i,j,msk);\n lgb[j][i] = sediv(j,i,msk);\n }\n }\n\n int mn = INF;\n // iterate over valid (sta,ena) in lga and (stb,enb) in lgb\n for (int sta = 0; sta < n; ++sta) {\n for (int ena = 0; ena < n; ++ena) {\n if (lga[sta][ena] == INF) continue;\n for (int stb = 0; stb < n; ++stb) {\n for (int enb = 0; enb < n; ++enb) {\n if (lgb[stb][enb] == INF) continue;\n // two combinations as in python\n int tmp = lga[sta][ena] + lgb[stb][enb] - v[ena][stb];\n if (tmp < mn) mn = tmp;\n tmp = lga[sta][ena] + lgb[stb][enb] - v[enb][sta];\n if (tmp < mn) mn = tmp;\n }\n }\n }\n }\n if (mn < ans) ans = mn;\n\n // advance combination\n if (!next_combination(comb, n, div2)) break;\n }\n\n cout << ans << '\\n';\n }\n\n return 0;\n}", "accuracy": 1, "time_ms": 3510, "memory_kb": 10704, "score_of_the_acc": -0.5784, "final_rank": 17 }, { "submission_id": "aoj_1320_10856460", "code_snippet": "#include <iostream>\n#include <cstdio>\n#include <cstdlib>\n#include <cstring>\n#include <vector>\n\nusing namespace std;\n\nconst int maxn = 30;\n\nint n;\nint extra[maxn][maxn], f[1<<15][maxn];\nchar word[maxn][maxn];\nbool substr[maxn];\nvector<char*> wd;\n\nint main()\n{\n#ifdef LOCAL\n\tfreopen(\"F.in\", \"r\", stdin);\n#endif\n\twhile(scanf(\"%d\", &n) != EOF) {\n\t\tif(n == 0)\tbreak;\n\t\tmemset(substr, 0, n*sizeof(char));\n\t\twd.clear();\n\t\tfor(int i = 0; i < n; i++) {\n\t\t\tscanf(\"%s\", word+i);\n\t\t\tfor(int j = 0; j < i; j++) {\n\t\t\t\tif(strstr(word[i], word[j]) != NULL)\tsubstr[j] = true;\n\t\t\t\telse if(strstr(word[j], word[i]) != NULL)\tsubstr[i] = true;\n\t\t\t}\n\t\t}\n\t\tfor(int i = 0; i < n; i++)\n\t\t\tif(!substr[i])\twd.push_back(word[i]);\n\t\tfor(int i = 0; i < wd.size(); i++)\n\t\t\tfor(int j = 0; j < wd.size(); j++) {\n\t\t\t\tint plen = strlen(wd[i]), nlen = strlen(wd[j]), match = 0;\n\t\t\t\tfor(int k = min(nlen, plen); k > 0; k--)\n\t\t\t\t\tif(strncmp(wd[j], wd[i]+plen-k, k) == 0)\t{\n\t\t\t\t\t\tmatch = k;\n\t\t\t\t\t\tbreak;\n\t\t\t\t\t}\n\t\t\t\textra[i][j] = nlen-match;\n\t\t\t}\n\t\tint cnt = wd.size();\n\t\tmemset(f, 0, sizeof(f));\n\t\tfor(int i = 0; i < cnt; i++)\n\t\t\tf[1<<i][i] = strlen(wd[i]);\n\t\tfor(int i = 1; i < (1 << cnt); i++) \n\t\t\tfor(int j = 0; j < cnt; j++) {\n\t\t\t\tif(((1<<j)&i) == 0)\tcontinue; \n\t\t\t\tfor(int k = 0; k < cnt; k++) {\n\t\t\t\t\tif((1<<k)&i)\tcontinue; \n\t\t\t\t\tint now = i|(1<<k); \n\t\t\t\t\tf[now][k] = f[now][k]? min(f[now][k], f[i][j]+extra[j][k]):f[i][j]+extra[j][k]; \n//\t\t\t\t\tif(i == 13)\n//\t\t\t\t\tcout << \"Insert \" << k << \" after \" << j << ' ' << f[now][k] << endl; \n\t\t\t\t} \n\t\t\t} \n\t\tint ans = cnt*maxn; \n\t\tfor(int i = 0; i < cnt; i++) \n\t\t\tans = min(ans, f[(1<<cnt)-1][i]); \n\t\tprintf(\"%d\\n\", ans); \n\t}\n\treturn 0; \n}", "accuracy": 1, "time_ms": 150, "memory_kb": 7416, "score_of_the_acc": -0.0338, "final_rank": 14 }, { "submission_id": "aoj_1320_9680190", "code_snippet": "#include <bits/stdc++.h>\n\nusing namespace std;\n\n#define rep(i, a, b) for (int i = (int)(a); i < (int)(b); i++)\n#define rrep(i, a, b) for (int i = (int)(b)-1; i >= (int)(a); i--)\n#define ALL(v) (v).begin(), (v).end()\n#define UNIQUE(v) sort(ALL(v)), (v).erase(unique(ALL(v)), (v).end())\n#define SZ(v) (int)v.size()\n#define MIN(v) *min_element(ALL(v))\n#define MAX(v) *max_element(ALL(v))\n#define LB(v, x) int(lower_bound(ALL(v), (x)) - (v).begin())\n#define UB(v, x) int(upper_bound(ALL(v), (x)) - (v).begin())\n\nusing uint = unsigned int;\nusing ll = long long int;\nusing ull = unsigned long long;\nusing i128 = __int128_t;\nusing u128 = __uint128_t;\nconst int inf = 0x3fffffff;\nconst ll INF = 0x1fffffffffffffff;\n\ntemplate <typename T> inline bool chmax(T &a, T b) {\n if (a < b) {\n a = b;\n return 1;\n }\n return 0;\n}\ntemplate <typename T> inline bool chmin(T &a, T b) {\n if (a > b) {\n a = b;\n return 1;\n }\n return 0;\n}\ntemplate <typename T, typename U> T ceil(T x, U y) {\n assert(y != 0);\n if (y < 0)\n x = -x, y = -y;\n return (x > 0 ? (x + y - 1) / y : x / y);\n}\ntemplate <typename T, typename U> T floor(T x, U y) {\n assert(y != 0);\n if (y < 0)\n x = -x, y = -y;\n return (x > 0 ? x / y : (x - y + 1) / y);\n}\ntemplate <typename T> int popcnt(T x) {\n return __builtin_popcountll(x);\n}\ntemplate <typename T> int topbit(T x) {\n return (x == 0 ? -1 : 63 - __builtin_clzll(x));\n}\ntemplate <typename T> int lowbit(T x) {\n return (x == 0 ? -1 : __builtin_ctzll(x));\n}\n\ntemplate <class T, class U>\nostream &operator<<(ostream &os, const pair<T, U> &p) {\n os << \"P(\" << p.first << \", \" << p.second << \")\";\n return os;\n}\ntemplate <typename T> ostream &operator<<(ostream &os, const vector<T> &vec) {\n os << \"{\";\n for (int i = 0; i < vec.size(); i++) {\n os << vec[i] << (i + 1 == vec.size() ? \"\" : \", \");\n }\n os << \"}\";\n return os;\n}\ntemplate <typename T, typename U>\nostream &operator<<(ostream &os, const map<T, U> &map_var) {\n os << \"{\";\n for (auto itr = map_var.begin(); itr != map_var.end(); itr++) {\n os << \"(\" << itr->first << \", \" << itr->second << \")\";\n itr++;\n if (itr != map_var.end())\n os << \", \";\n itr--;\n }\n os << \"}\";\n return os;\n}\ntemplate <typename T> ostream &operator<<(ostream &os, const set<T> &set_var) {\n os << \"{\";\n for (auto itr = set_var.begin(); itr != set_var.end(); itr++) {\n os << *itr;\n ++itr;\n if (itr != set_var.end())\n os << \", \";\n itr--;\n }\n os << \"}\";\n return os;\n}\n#ifdef LOCAL\n#define show(...) _show(0, #__VA_ARGS__, __VA_ARGS__)\n#else\n#define show(...) true\n#endif\ntemplate <typename T> void _show(int i, T name) {\n cerr << '\\n';\n}\ntemplate <typename T1, typename T2, typename... T3>\nvoid _show(int i, const T1 &a, const T2 &b, const T3 &...c) {\n for (; a[i] != ',' && a[i] != '\\0'; i++)\n cerr << a[i];\n cerr << \":\" << b << \" \";\n _show(i + 1, a, c...);\n}\n\n/**\n * @brief template\n */\n\nint main() {\nwhile(1) {\n int N;\n cin >> N;\n if (N == 0) return 0;\n vector<string> T(N);\n rep(i,0,N) cin >> T[i];\n vector<string> S;\n rep(i,0,N) {\n bool check = true;\n rep(j,0,N) {\n if (i==j) continue;\n if (T[i].size() > T[j].size()) continue;\n rep(k,0,T[j].size()) {\n if (k+(int)T[i].size() > (int)T[j].size()) break;\n if (T[j].substr(k,T[i].size()) == T[i]) check = false;\n }\n }\n if (check) S.push_back(T[i]);\n }\n N = S.size();\n vector<vector<int>> G(N,vector<int>(N,inf));\n rep(i,0,N) {\n rep(j,0,N) {\n if (i == j) {\n G[i][j] = 0;\n continue;\n }\n rep(k,0,min(S[i].size(),S[j].size())) {\n if (S[i].substr(S[i].size()-k,k) == S[j].substr(0,k)) G[i][j] = (int)S[j].size() - k;\n }\n }\n }\n vector DP(1<<N, vector<int>(N,inf));\n rep(i,0,N) DP[1<<i][i] = S[i].size();\n rep(i,1,1<<N) {\n rep(j,0,N) {\n if (DP[i][j] == inf) continue;\n rep(k,0,N) {\n if (i & (1<<k)) continue;\n chmin(DP[i|(1<<k)][k],DP[i][j]+G[j][k]);\n }\n }\n }\n int ANS = inf;\n rep(i,0,N) chmin(ANS, DP[(1<<N)-1][i]);\n cout << ANS << endl;\n}\n}", "accuracy": 1, "time_ms": 190, "memory_kb": 4740, "score_of_the_acc": -0.0123, "final_rank": 7 }, { "submission_id": "aoj_1320_9205868", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\n#define rep(i, n) for(int i = 0; i < n; i++)\n\nint calcdif(string s, string t) { // 文字列tの長さ - 文字列sと文字列tの共通部分の長さ\n int sz = s.size();\n int tz = t.size();\n int comlen = 0;\n for (int i = 0; i < sz; i++) { // sのiからszまでとtの0からlenまでの共通部分の長さを計算する\n for (int len = 1; len <= tz; len++) {\n string ss = s.substr(i, sz);\n string tt = t.substr(0, len);\n if (ss == tt) {\n comlen = max(comlen, len);\n }\n }\n }\n\n return tz - comlen;\n}\n\nint main() {\n int n;\n while(cin >> n && n) {\n vector<string> cities(n);\n rep(i, n) cin >> cities[i];\n\n vector<int> unIncludedCities; // 互いに部分文字列になっていない都市のリスト\n\n rep(i, n) { // 都市iが他の都市の部分文字列になってないなら、unIncludedCitiesに追加\n bool isInclude = false;\n rep(j, n) {\n if(i == j) continue;\n if(cities[j].find(cities[i]) != string::npos) {\n isInclude = true;\n break;\n }\n }\n if(!isInclude) unIncludedCities.push_back(i);\n }\n\n int m = unIncludedCities.size();\n\n vector<vector<int>> dif(m, vector<int>(m)); // dif[i][j] := 都市jの長さ - 都市iの末尾と都市jの先頭の重複する部分の長さ\n rep(i, m) rep(j, m) {\n dif[i][j] = calcdif(cities[unIncludedCities[i]], cities[unIncludedCities[j]]);\n }\n\n vector<vector<int>> dp((1 << m), vector<int>(m, 1e9)); // dp[i][j] := 集合iを訪れて都市jにいるときの統合された文字列の最小の長さ\n rep(i, m) dp[1 << i][i] = cities[unIncludedCities[i]].size(); // 初期値が都市iのとき、都市iの長さを格納\n\n rep(i, (1 << m)) rep(j, m) {\n if(!(i & (1 << j))) continue;\n rep(k, m) {\n if(i & (1 << k)) continue;\n dp[i | (1 << k)][k] = min(dp[i | (1 << k)][k], dp[i][j] + dif[j][k]);\n }\n }\n\n cout << *min_element(dp[(1 << m) - 1].begin(), dp[(1 << m) - 1].end()) << endl;\n }\n}", "accuracy": 1, "time_ms": 180, "memory_kb": 4620, "score_of_the_acc": -0.0095, "final_rank": 4 }, { "submission_id": "aoj_1320_9205813", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\nusing ld = long double;\nusing ull = unsigned long long;\nusing uint = unsigned;\nusing pii = pair<int, int>;\nusing pll = pair<ll, ll>;\nusing pdd = pair<ld, ld>;\nusing tuplis = array<ll, 3>;\ntemplate<class T> using pq = priority_queue<T, vector<T>, greater<T>>;\nconst ll LINF=0x1fffffffffffffff;\nconst ll MINF=0x7fffffffffff;\nconst int INF=0x3fffffff;\nconst int MOD=1000000007;\nconst int MODD=998244353;\nconst ld DINF=numeric_limits<ld>::infinity();\nconst ld EPS=1e-9;\nconst ld PI=3.14159265358979323846;\nconst ll dx[] = {0, 1, 0, -1, 1, -1, 1, -1};\nconst ll dy[] = {1, 0, -1, 0, 1, 1, -1, -1};\n#define overload5(a,b,c,d,e,name,...) name\n#define overload4(a,b,c,d,name,...) name\n#define overload3(a,b,c,name,...) name\n#define rep1(n) for(ll i=0;i<n;++i)\n#define rep2(i,n) for(ll i=0;i<n;++i)\n#define rep3(i,a,b) for(ll i=a;i<b;++i)\n#define rep4(i,a,b,c) for(ll i=a;i<b;i+=c)\n#define rep(...) overload4(__VA_ARGS__,rep4,rep3,rep2,rep1)(__VA_ARGS__)\n#define rrep1(n) for(ll i=n;i--;)\n#define rrep2(i,n) for(ll i=n;i--;)\n#define rrep3(i,a,b) for(ll i=b;i-->(a);)\n#define rrep4(i,a,b,c) for(ll i=(a)+((b)-(a)-1)/(c)*(c);i>=(a);i-=c)\n#define rrep(...) overload4(__VA_ARGS__,rrep4,rrep3,rrep2,rrep1)(__VA_ARGS__)\n#define each1(i,a) for(auto&&i:a)\n#define each2(x,y,a) for(auto&&[x,y]:a)\n#define each3(x,y,z,a) for(auto&&[x,y,z]:a)\n#define each4(w,x,y,z,a) for(auto&&[w,x,y,z]:a)\n#define each(...) overload5(__VA_ARGS__,each4,each3,each2,each1)(__VA_ARGS__)\n#define all1(i) begin(i),end(i)\n#define all2(i,a) begin(i),begin(i)+a\n#define all3(i,a,b) begin(i)+a,begin(i)+b\n#define all(...) overload3(__VA_ARGS__,all3,all2,all1)(__VA_ARGS__)\n#define rall1(i) rbegin(i),rend(i)\n#define rall2(i,a) rbegin(i),rbegin(i)+a\n#define rall3(i,a,b) rbegin(i)+a,rbegin(i)+b\n#define rall(...) overload3(__VA_ARGS__,rall3,rall2,rall1)(__VA_ARGS__)\n#define sum(...) accumulate(all(__VA_ARGS__),0LL)\n#define dsum(...) accumulate(all(__VA_ARGS__),0.0L)\n#define Msum(...) accumulate(all(__VA_ARGS__),mint{})\n#define elif else if\n#define INT(...) int __VA_ARGS__;in(__VA_ARGS__)\n#define LL(...) ll __VA_ARGS__;in(__VA_ARGS__)\n#define ULL(...) ull __VA_ARGS__;in(__VA_ARGS__)\n#define STR(...) string __VA_ARGS__;in(__VA_ARGS__)\n#define CHR(...) char __VA_ARGS__;in(__VA_ARGS__)\n#define DBL(...) double __VA_ARGS__;in(__VA_ARGS__)\n#define LD(...) ld __VA_ARGS__;in(__VA_ARGS__)\n#define vec(type,name,...) vector<type>name(__VA_ARGS__)\n#define VEC(type,name,size) vector<type>name(size);in(name)\n#define vv(type,name,h,...) vector name(h,vector<type>(__VA_ARGS__))\n#define VV(type,name,h,w) vector name(h,vector<type>(w));in(name)\n#define vvv(type,name,h,w,...) vector name(h,vector(w,vector<type>(__VA_ARGS__)))\ntemplate<class T> ll sz(const T& a){ return size(a); }\ntemplate<class T> auto min(const T& a){ return *min_element(all(a)); }\ntemplate<class T> auto max(const T& a){ return *max_element(all(a)); }\ntemplate<class T, class U> ll count(const T& a, const U& b){ return count(all(a), b); }\ntemplate<class T, class F> ll count_if(const T& a, F b){ return count_if(all(a), b); }\ntemplate<class T, class F> void filter(T& a, F b){ a.erase(remove_if(all(a), not_fn(b)), a.end()); }\ntemplate<class T, class F = less<>> void sor(T& a, F b = F{}){ sort(all(a), b); }\ntemplate<class T> void rev(T& a){ reverse(all(a)); }\ntemplate<class T> void uniq(T& a){ sor(a); a.erase(unique(all(a)), end(a)); }\nll popcnt(ull a){ return __builtin_popcountll(a); }\nll intpow(ll a, ll b){ ll ans = 1; while(b){ if(b & 1) ans *= a; a *= a; b /= 2; } return ans; }\nll modpow(ll a, ll b, ll p){ ll ans = 1; while(b){ if(b & 1) (ans *= a) %= p; (a *= a) %= p; b /= 2; } return ans; }\ntemplate<class T> T div_floor(T a, T b) { return a / b - ((a ^ b) < 0 && a % b); }\ntemplate<class T> T div_ceil(T a, T b) { return a / b + ((a ^ b) > 0 && a % b); }\ntemplate<class T> bool chmin(T& a, const T& b){ if(a <= b) return 0; a = b; return 1; }\ntemplate<class T> bool chmax(T& a, const T& b){ if(a >= b) return 0; a = b; return 1; }\ntemplate<class T, class U> bool chmin(T& a, const U& b){ return chmin(a, (T)b); }\ntemplate<class T, class U> bool chmax(T& a, const U& b){ return chmax(a, (T)b); }\nvector<ll> iota(ll n, ll begin = 0){ vector<ll> a(n); iota(a.begin(), a.end(), begin); return a; }\nvector<pll> factor(ull x){ vector<pll> ans; for(ull i = 2; i * i <= x; i++) if(x % i == 0){ ans.push_back({i, 1}); while((x /= i) % i == 0) ans.back().second++; } if(x != 1) ans.push_back({x, 1}); return ans; }\nvector<ll> divisor(ull x){ vector<ll> ans; for(ull i = 1; i * i <= x; i++) if(x % i == 0) ans.push_back(i); rrep(ans.size() - (ans.back() * ans.back() == x)) ans.push_back(x / ans[i]); return ans; }\ntemplate<class T> unordered_map<T, ll> press(vector<T> a){ uniq(a); unordered_map<T, ll> ans; rep(a.size()) ans[a[i]] = i; return ans; }\ntemplate<class T> auto run_press(const T& a){ vector<pair<decay_t<decltype(a[0])>, ll>> ans; each(x, a){ if(ans.empty() || ans.back().first != x) ans.emplace_back(x, 1); else ans.back().second++; } return ans; }\ntemplate<class... Ts> void in(Ts&... t);\n[[maybe_unused]] void print(){}\ntemplate<class T, class... Ts> void print(const T& t, const Ts&... ts);\ntemplate<class... Ts> void out(const Ts&... ts){ print(ts...); cout << '\\n'; }\nnamespace IO{\n#define VOID(a) decltype(void(a))\nstruct S{ S(){ cin.tie(nullptr)->sync_with_stdio(0); fixed(cout).precision(12); } }S;\ntemplate<int I> struct P : P<I-1>{};\ntemplate<> struct P<0>{};\ntemplate<class T> void i(T& t){ i(t, P<3>{}); }\nvoid i(vector<bool>::reference t, P<3>){ int a; i(a); t = a; }\ntemplate<class T> auto i(T& t, P<2>) -> VOID(cin >> t){ cin >> t; }\ntemplate<class T> auto i(T& t, P<1>) -> VOID(begin(t)){ for(auto&& x : t) i(x); }\ntemplate<class T, size_t... idx> void ituple(T& t, index_sequence<idx...>){ in(get<idx>(t)...); }\ntemplate<class T> auto i(T& t, P<0>) -> VOID(tuple_size<T>{}){ ituple(t, make_index_sequence<tuple_size<T>::value>{}); }\ntemplate<class T> void o(const T& t){ o(t, P<4>{}); }\ntemplate<size_t N> void o(const char (&t)[N], P<4>){ cout << t; }\ntemplate<class T, size_t N> void o(const T (&t)[N], P<3>){ o(t[0]); for(size_t i = 1; i < N; i++){ o(' '); o(t[i]); } }\ntemplate<class T> auto o(const T& t, P<2>) -> VOID(cout << t){ cout << t; }\ntemplate<class T> auto o(const T& t, P<1>) -> VOID(begin(t)){ bool first = 1; for(auto&& x : t) { if(first) first = 0; else o(' '); o(x); } }\ntemplate<class T, size_t... idx> void otuple(const T& t, index_sequence<idx...>){ print(get<idx>(t)...); }\ntemplate<class T> auto o(T& t, P<0>) -> VOID(tuple_size<T>{}){ otuple(t, make_index_sequence<tuple_size<T>::value>{}); }\n#undef VOID\n}\n#define unpack(a) (void)(0 + ... + (a, 0))\ntemplate<class... Ts> void in(Ts&... t){ unpack(IO::i(t)); }\ntemplate<class T, class... Ts> void print(const T& t, const Ts&... ts){ IO::o(t); unpack(IO::o((cout << ' ', ts))); }\n#undef unpack\n#ifdef DEBUG\nll __lg(ull x){ return 63 - __builtin_clzll(x); }\n#define debug(...) { print(#__VA_ARGS__); print(\n#else\n#define debug(...) void(0)\n#endif\n#define YESNO(yes,no) void yes(bool i = 1){ out(i?#yes:#no); } void no(){ out(#no); }\nYESNO(first, second)\nYESNO(First, Second)\nYESNO(Yes, No)\nYESNO(YES, NO)\nYESNO(possible, impossible)\nYESNO(Possible, Impossible)\nYESNO(POSSIBLE, IMPOSSIBLE)\n\n\n\ntemplate<class T> using P = pair<T, T>;\n#define x first\n#define y second\ntemplate<class T> P<T> operator+(const P<T>& a, const P<T>& b) { return {a.x + b.x, a.y + b.y}; }\ntemplate<class T> P<T> operator-(const P<T>& a, const P<T>& b) { return {a.x - b.x, a.y - b.y}; }\ntemplate<class T> P<T> operator-(const P<T>& a) { return {-a.x, -a.y}; }\ntemplate<class T, class U> P<T> operator*(const P<T>& a, const U& b) { return {a.x * b, a.y * b}; }\ntemplate<class T, class U> P<T> operator/(const P<T>& a, const U& b) { return {a.x / b, a.y / b}; }\ntemplate<class T> P<T>& operator+=(P<T>& a, const P<T>& b) { return a = a + b; }\ntemplate<class T> P<T>& operator-=(P<T>& a, const P<T>& b) { return a = a - b; }\ntemplate<class T, class U> P<T>& operator*=(P<T>& a, const U& b) { return a = a * b; }\ntemplate<class T, class U> P<T>& operator/=(P<T>& a, const U& b) { return a = a / b; }\ntemplate<class T> P<T> rotate(const P<T>& a) { return {-a.y, a.x}; } // 90 degree ccw\ntemplate<class T> T dot(const P<T>& a, const P<T>& b) { return a.x * b.x + a.y * b.y; }\ntemplate<class T> T cross(const P<T>& a, const P<T>& b) { return dot(rotate(a), b); }\ntemplate<class T> T square(const P<T>& a) { return dot(a, a); }\ntemplate<class T> ld abs(const P<T>& a) { return hypotl(a.x, a.y); }\ntemplate<class T> T gcd(const P<T>& a) { return gcd(a.x, a.y); }\ntemplate<class T> P<T> normalize(P<T> a) {\n if(a == P<T>{}) return a;\n a /= gcd(a);\n if(a < P<T>{}) a = -a;\n return a;\n}\n\n// 文字列tの長さ - 文字列sと文字列tの共通部分の長さ\nint calcdif(string s, string t) {\n int sz = s.size();\n int tz = t.size();\n int comlen = 0;\n for (int i = 0; i < sz; i++) { // sのiからszまでとtの0からlenまでの共通部分の長さを計算する\n for (int len = 1; len <= tz; len++) {\n string ss = s.substr(i, sz);\n string tt = t.substr(0, len);\n if (ss == tt) {\n comlen = max(comlen, len);\n }\n }\n }\n\n // // tがsに含まれている場合、末尾に付け足す文字列の長さは0\n // for (int len = 1; len <= sz; len++) {\n // for (int i = 0; i < sz; i++) {\n // string ss = s.substr(i, len);\n // string tt = t.substr(0, tz);\n // if (ss == tt) {\n // return 0;\n // }\n // }\n // }\n\n return tz - comlen;\n}\n\nint main() {\n int n;\n while(cin >> n && n) {\n vector<string> str(n+1);\n for (int i = 1; i <= n; i++) cin >> str[i];\n\n vector<string> rms;\n // 他の文字列に含まれていない文字列を抽出\n for (int i = 1; i <= n; i++) {\n bool flag = false;\n for (int j = 1; j <= n; j++) {\n if (i == j) continue;\n for (int len = 1; len <= str[j].size(); len++) {\n for (int k = 0; k + len <= str[j].size(); k++) {\n if (str[j].substr(k, len) == str[i]) {\n flag = true;\n break;\n }\n }\n }\n }\n if (!flag) rms.push_back(str[i]);\n }\n // out(rms);\n \n int m = rms.size();\n // dif[i][j] 文字列jの長さ - 文字列iと文字列jの共通部分の長さ\n vector<vector<int>> dif(n+1, vector<int>(n+1, 0));\n for (int i = 0; i < m; i++) {\n for (int j = 0; j < m; j++) {\n if (i == j) continue;\n dif[i][j] = calcdif(rms[i], rms[j]);\n }\n }\n\n // rep(i, m) {\n // rep(j, m) {\n // cout << dif[i][j] << \" \";\n // }\n // cout << endl;\n // }\n\n // // dp[i][j] すでに状態iなる文字列を使っていて、現在位置がjであるときの併合した文字列の長さの最小値\n vector<vector<int>> dp((1<<14)+1, vector<int>(n+1, INF));\n for (int i = 0; i < m; i++) dp[(1<<i)][i] = rms[i].size();\n for (int i = 1; i < (1<<m); i++) { // すでに使っている文字列の集合\n for (int j = 0; j <= m; j++) { // 現在位置\n if (dp[i][j] == INF) continue;\n for (int k = 0; k < m; k++) { // 併合する文字列を選ぶ\n if (i & (1<<(k))) continue; // すでに使っている文字列は使えない\n chmin(dp[i|(1<<(k))][k], dp[i][j] + dif[j][k]);\n }\n }\n }\n out(*min_element(all(dp[(1<<m)-1])));\n }\n}", "accuracy": 1, "time_ms": 290, "memory_kb": 4748, "score_of_the_acc": -0.0276, "final_rank": 12 }, { "submission_id": "aoj_1320_8992038", "code_snippet": "#line 2 \"cp-library/src/cp-template.hpp\"\n#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\nusing ld = long double;\nusing uint = unsigned int;\nusing ull = unsigned long long;\nusing i32 = int;\nusing u32 = unsigned int;\nusing i64 = long long;\nusing u64 = unsigned long long;\nusing i128 = __int128_t;\ntemplate < class T > bool chmin(T& a, T b) { if(a > b) { a = b; return true; } return false; }\ntemplate < class T > bool chmax(T& a, T b) { if(a < b) { a = b; return true; } return false; }\ntemplate < class T, class U > T ceil (T x, U y) { return (x > 0 ? (x + y - 1) / y : x / y); }\ntemplate < class T, class U > T floor(T x, U y) { return (x > 0 ? x / y : (x - y + 1) / y); }\nint popcnt(i32 x) { return __builtin_popcount(x); }\nint popcnt(u32 x) { return __builtin_popcount(x); }\nint popcnt(i64 x) { return __builtin_popcountll(x); }\nint popcnt(u64 x) { return __builtin_popcountll(x); }\n\n#line 2 \"cp-library/src/utility/rep_itr.hpp\"\ntemplate < class T > struct itr_rep {\n T i, d;\n constexpr itr_rep(const T i) noexcept : i(i), d(1) {}\n constexpr itr_rep(const T i, const T d) noexcept : i(i), d(d) {}\n void operator++() noexcept { i += d; }\n constexpr int operator*() const noexcept { return i; }\n constexpr bool operator!=(const itr_rep x) const noexcept { return d > 0 ? i < x.i : i > x.i; }\n};\n\ntemplate < class T > struct rep {\n const itr_rep< T > s, t;\n constexpr rep(const T t) noexcept : s(0), t(t) {}\n constexpr rep(const T s, const T t) noexcept : s(s), t(t) {}\n constexpr rep(const T s, const T t, const T d) noexcept : s(s, d), t(t, d) {}\n constexpr auto begin() const noexcept { return s; }\n constexpr auto end () const noexcept { return t; }\n};\n\ntemplate < class T > struct revrep {\n const itr_rep < T > s, t;\n constexpr revrep(const T t) noexcept : s(t - 1, -1), t(-1, -1) {}\n constexpr revrep(const T s, const T t) noexcept : s(t - 1, -1), t(s - 1, -1) {}\n constexpr revrep(const T s, const T t, const T d) noexcept : s(t - 1, -d), t(s - 1, -d) {}\n constexpr auto begin() const noexcept { return s; }\n constexpr auto end () const noexcept { return t; }\n};\n#line 3 \"cp-library/src/utility/io.hpp\"\n\n/* 128bit integer */\nistream& operator>>(istream& is, i128& x) {\n std::string s; is >> s;\n int pm = (s[0] == '-');\n x = 0;\n for(int i : rep(pm, int(s.size()))) x = x * 10 + (s[i] - '0');\n if(pm) x *= -1;\n return is;\n}\nostream& operator<<(ostream& os, const i128& x) {\n if(x == 0) return os << '0';\n i128 y = x;\n if(y < 0) { os << '-'; y *= -1; }\n std::vector<int> ny;\n while(y > 0) { ny.push_back(y % 10); y /= 10; }\n for(int i : revrep(ny.size())) os << ny[i];\n return os;\n}\n\ntemplate < class S, class T > istream& operator>>(istream& is, std::pair< S, T >& x) { is >> x.first >> x.second; return is; }\ntemplate < class S, class T > ostream& operator<<(ostream& os, const std::pair< S, T >& x) { os << x.first << \" \" << x.second; return os; }\n\nnamespace scanner {\n struct sca {\n template < class T > operator T() {\n T s; std::cin >> s; return s;\n }\n };\n struct vec {\n int n;\n vec(int n) : n(n) {}\n template < class T > operator std::vector< T >() {\n std::vector< T > v(n);\n for(T& x : v) std::cin >> x;\n return v;\n }\n };\n struct mat {\n int h, w;\n mat(int h, int w) : h(h), w(w) {}\n template < class T > operator std::vector< std::vector< T > >() {\n std::vector m(h, std::vector< T >(w));\n for(std::vector< T >& v : m) for(T& x : v) std::cin >> x;\n return m;\n }\n };\n struct speedup {\n speedup() {\n std::cin.tie(0);\n std::ios::sync_with_stdio(0);\n }\n } speedup_instance;\n}\nscanner::sca in() { return scanner::sca(); }\nscanner::vec in(int n) { return scanner::vec(n); }\nscanner::mat in(int h, int w) { return scanner::mat(h, w); }\n\nnamespace printer {\n void precision(int d) { std::cout << std::fixed << std::setprecision(d); }\n void flush() { std::cout.flush(); }\n}\n\ntemplate < class T >\nostream& operator<<(ostream& os, const std::vector< T > a) {\n int n = a.size();\n for(int i : rep(n)) { os << a[i]; if(i != n - 1) os << ' '; }\n return os;\n}\n\nint print() { std::cout << '\\n'; return 0; }\ntemplate < class head, class... tail > int print(head&& h, tail&&... t) {\n std::cout << h; if(sizeof...(tail)) std::cout << ' ';\n return print(std::forward<tail>(t)...);\n}\ntemplate < class T > int print_n(const std::vector< T > a) {\n int n = a.size();\n for(int i : rep(n)) std::cout << a[i] << \"\\n\";\n return 0;\n}\n\n\n#line 2 \"cp-library/src/utility/key_val.hpp\"\n\ntemplate < class K, class V >\nstruct key_val {\n K key; V val;\n key_val() {}\n key_val(K key, V val) : key(key), val(val) {}\n template < std::size_t Index >\n std::tuple_element_t< Index, key_val >& get() {\n if constexpr (Index == 0) return key;\n if constexpr (Index == 1) return val;\n }\n};\n\nnamespace std {\n\ntemplate < class K, class V > struct tuple_size < key_val< K, V > > : integral_constant< size_t, 2 > {};\ntemplate < class K, class V > struct tuple_element < 0, key_val< K, V > > { using type = K; };\ntemplate < class K, class V > struct tuple_element < 1, key_val< K, V > > { using type = V; };\n\n}\n#line 2 \"cp-library/src/utility/vec_op.hpp\"\ntemplate < class T > key_val< int, T > max_of(const vector< T >& a) {\n int i = std::max_element(a.begin(), a.end()) - a.begin();\n return {i, a[i]};\n}\ntemplate < class T > key_val< int, T > min_of(const vector< T >& a) {\n int i = std::min_element(a.begin(), a.end()) - a.begin();\n return {i, a[i]};\n}\ntemplate < class S, class T > S sum_of(const vector< T >& a) {\n S sum = 0;\n for(const T x : a) sum += x;\n return sum;\n}\ntemplate < class S, class T > vector< S > freq_of(const vector< T >& a, T L, T R) {\n vector< S > res(R - L, S(0));\n for(const T x : a) res[x - L] += 1;\n return res;\n}\ntemplate < class S, class T > struct prefix_sum {\n vector< S > s;\n prefix_sum(const vector< T >& a) : s(a) {\n s.insert(s.begin(), S(0));\n for(int i : rep(a.size())) s[i + 1] += s[i];\n }\n // [L, R)\n S sum(int L, int R) { return s[R] - s[L]; }\n};\n#line 3 \"cp-library/src/utility/heap.hpp\"\n\ntemplate < class T > using heap_min = std::priority_queue< T, std::vector< T >, std::greater< T > >;\ntemplate < class T > using heap_max = std::priority_queue< T, std::vector< T >, std::less< T > >;\n\n#line 27 \"cp-library/src/cp-template.hpp\"\n\n#line 1 \"cp-library/src/algorithm/bin_search.hpp\"\ntemplate < class T, class F >\nT bin_search(T ok, T ng, F f) {\n while(abs(ng - ok) > 1) {\n T mid = (ok + ng) / 2;\n (f(mid) ? ok : ng) = mid;\n }\n return ok;\n}\n\ntemplate < class T, class F >\nT bin_search_real(T ok, T ng, F f, int step = 80) {\n while(step--) {\n T mid = (ok + ng) / 2;\n (f(mid) ? ok : ng) = mid;\n }\n return ok;\n}\n#line 2 \"cp-library/src/algorithm/argsort.hpp\"\n\ntemplate < class T > std::vector< int > argsort(const std::vector< T > &a) {\n std::vector< int > ids((int)a.size());\n std::iota(ids.begin(), ids.end(), 0);\n std::sort(ids.begin(), ids.end(), [&](int i, int j) {\n return a[i] < a[j] || (a[i] == a[j] && i < j);\n });\n return ids;\n}\n#line 1 \"macro.hpp\"\nnamespace macro {\n\nusing size_type = int;\ntemplate < class container > void sort(container& a) { std::sort(std:: begin(a), std:: end(a)); }\ntemplate < class container > void rsort(container& a) { std::sort(std::rbegin(a), std::rend(a)); }\ntemplate < class container > void reverse(container& a) { std::reverse(std::begin(a), std::end(a)); }\ntemplate < class container > void unique(container& a) {\n std::sort(std::begin(a), std::end(a));\n a.erase(std::unique(std::begin(a), std::end(a)), std::end(a));\n}\ntemplate < class container > container sorted(const container& a) { container b = a; sort(b); return std::move(b); }\ntemplate < class container > container rsorted(const container& a) { container b = a; rsort(b); return std::move(b); }\ntemplate < class container, class compare > void sort(container& a, const compare& cmp) { std::sort(std::begin(a), std::end(a), cmp); }\ntemplate < class container, class compare > container sorted(const container& a, const compare& cmp) { container b = a; sort(b, cmp); return std::move(b); }\ntemplate < class container, class value > size_type lower_bound(const container& a, const value& x) { return std::lower_bound(std::begin(a), std::end(a), x) - std::begin(a); }\ntemplate < class container, class value > size_type upper_bound(const container& a, const value& x) { return std::upper_bound(std::begin(a), std::end(a), x) - std::begin(a); }\n\nconst std::vector<std::pair<size_type, size_type>> dir4 = { {+1, 0}, {-1, 0}, { 0, +1}, { 0, -1} };\nconst std::vector<std::pair<size_type, size_type>> dir8 = { {-1, -1}, {-1, 0}, {-1, +1}, { 0, -1}, { 0, +1}, {+1, -1}, {+1, 0}, {+1, +1} };\n\n#ifdef _DEBUG\n#define debug(x) std::cout << \"[\" << __LINE__ << \"] \" << #x << \": \" << x << std::endl\n#else\n#define debug(x)\n#endif\n\ntemplate < class container > void concat(container& a, const container& b) {\n a.insert(std::end(a), std::begin(b), std::end(b));\n}\nstd::vector<size_type> iota(const size_type n) {\n std::vector<size_type> I(n);\n std::iota(std::begin(I), std::end(I), 0);\n return I;\n}\ntemplate < class container > std::vector<size_type> sort_idx(const container& a) {\n const size_type n = a.size();\n std::vector<size_type> I = iota(n);\n std::sort(std::begin(I), std::end(I), [&](size_type i, size_type j) { return a[i] < a[j] or (a[i] == a[j] and i < j); });\n return I;\n}\ntemplate < class container, class compare > std::vector<size_type> sort_idx(const container& a, const compare& cmp) {\n const size_type n = a.size();\n std::vector<size_type> I = iota(n);\n std::sort(std::begin(I), std::end(I), [&](size_type i, size_type j) { return cmp(a[i], a[j]) or (a[i] == a[j] and i < j); });\n return std::move(I);\n}\n\nstruct grid {\n using size_type = int;\n size_type H, W;\n grid(const size_type H, const size_type W) : H(H), W(W) {}\n bool contains(const size_type i, const size_type j) {\n return 0 <= i and i < H and 0 <= j and j < W;\n }\n};\n\n} // namespace macro\n\nusing namespace macro;\n#line 3 \"A.cpp\"\n\nint solve(int n) {\n vector<string> s = in(n);\n {\n vector<string> ns;\n for(int i : rep(n)) {\n bool substr = false;\n for(int j : rep(n)) if(j != i and s[i].size() <= s[j].size()) {\n for(int k : rep(s[j].size())) {\n if(s[j].substr(k, s[i].size()) == s[i]) {\n substr = true;\n }\n }\n }\n if(!substr) ns.push_back(s[i]);\n }\n s = move(ns);\n n = s.size();\n }\n\n vector dp(1 << n, vector(n, int(1e9)));\n for(int i : rep(n)) dp[1 << i][i] = s[i].size();\n\n vector c(n, vector(n, int(1e9)));\n for(int i : rep(n)) for(int j : rep(n)) if(i != j) {\n for(int k : revrep(int(s[i].size()))) {\n if(s[i].substr(s[i].size() - k, k) == s[j].substr(0, k)) {\n c[i][j] = s[j].size() - k;\n break;\n }\n }\n }\n\n for(int S : rep(1 << n)) if(S) {\n for(int i : rep(n)) if(S & (1 << i)) {\n for(int j : rep(n)) if(!(S & (1 << j))) {\n chmin(dp[S | (1 << j)][j], dp[S][i] + c[i][j]);\n }\n }\n }\n\n int ans = 1e9;\n for(int i : rep(n)) chmin(ans, dp[(1 << n) - 1][i]);\n return ans;\n}\n\nint main() {\n while(true) {\n int n = in(); if(n == 0) break;\n print(solve(n));\n }\n}", "accuracy": 1, "time_ms": 170, "memory_kb": 4884, "score_of_the_acc": -0.0107, "final_rank": 5 }, { "submission_id": "aoj_1320_8992028", "code_snippet": "#line 2 \"cp-library/src/cp-template.hpp\"\n#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\nusing ld = long double;\nusing uint = unsigned int;\nusing ull = unsigned long long;\nusing i32 = int;\nusing u32 = unsigned int;\nusing i64 = long long;\nusing u64 = unsigned long long;\nusing i128 = __int128_t;\ntemplate < class T > bool chmin(T& a, T b) { if(a > b) { a = b; return true; } return false; }\ntemplate < class T > bool chmax(T& a, T b) { if(a < b) { a = b; return true; } return false; }\ntemplate < class T, class U > T ceil (T x, U y) { return (x > 0 ? (x + y - 1) / y : x / y); }\ntemplate < class T, class U > T floor(T x, U y) { return (x > 0 ? x / y : (x - y + 1) / y); }\nint popcnt(i32 x) { return __builtin_popcount(x); }\nint popcnt(u32 x) { return __builtin_popcount(x); }\nint popcnt(i64 x) { return __builtin_popcountll(x); }\nint popcnt(u64 x) { return __builtin_popcountll(x); }\n\n#line 2 \"cp-library/src/utility/rep_itr.hpp\"\ntemplate < class T > struct itr_rep {\n T i, d;\n constexpr itr_rep(const T i) noexcept : i(i), d(1) {}\n constexpr itr_rep(const T i, const T d) noexcept : i(i), d(d) {}\n void operator++() noexcept { i += d; }\n constexpr int operator*() const noexcept { return i; }\n constexpr bool operator!=(const itr_rep x) const noexcept { return d > 0 ? i < x.i : i > x.i; }\n};\n\ntemplate < class T > struct rep {\n const itr_rep< T > s, t;\n constexpr rep(const T t) noexcept : s(0), t(t) {}\n constexpr rep(const T s, const T t) noexcept : s(s), t(t) {}\n constexpr rep(const T s, const T t, const T d) noexcept : s(s, d), t(t, d) {}\n constexpr auto begin() const noexcept { return s; }\n constexpr auto end () const noexcept { return t; }\n};\n\ntemplate < class T > struct revrep {\n const itr_rep < T > s, t;\n constexpr revrep(const T t) noexcept : s(t - 1, -1), t(-1, -1) {}\n constexpr revrep(const T s, const T t) noexcept : s(t - 1, -1), t(s - 1, -1) {}\n constexpr revrep(const T s, const T t, const T d) noexcept : s(t - 1, -d), t(s - 1, -d) {}\n constexpr auto begin() const noexcept { return s; }\n constexpr auto end () const noexcept { return t; }\n};\n#line 3 \"cp-library/src/utility/io.hpp\"\n\n/* 128bit integer */\nistream& operator>>(istream& is, i128& x) {\n std::string s; is >> s;\n int pm = (s[0] == '-');\n x = 0;\n for(int i : rep(pm, int(s.size()))) x = x * 10 + (s[i] - '0');\n if(pm) x *= -1;\n return is;\n}\nostream& operator<<(ostream& os, const i128& x) {\n if(x == 0) return os << '0';\n i128 y = x;\n if(y < 0) { os << '-'; y *= -1; }\n std::vector<int> ny;\n while(y > 0) { ny.push_back(y % 10); y /= 10; }\n for(int i : revrep(ny.size())) os << ny[i];\n return os;\n}\n\ntemplate < class S, class T > istream& operator>>(istream& is, std::pair< S, T >& x) { is >> x.first >> x.second; return is; }\ntemplate < class S, class T > ostream& operator<<(ostream& os, const std::pair< S, T >& x) { os << x.first << \" \" << x.second; return os; }\n\nnamespace scanner {\n struct sca {\n template < class T > operator T() {\n T s; std::cin >> s; return s;\n }\n };\n struct vec {\n int n;\n vec(int n) : n(n) {}\n template < class T > operator std::vector< T >() {\n std::vector< T > v(n);\n for(T& x : v) std::cin >> x;\n return v;\n }\n };\n struct mat {\n int h, w;\n mat(int h, int w) : h(h), w(w) {}\n template < class T > operator std::vector< std::vector< T > >() {\n std::vector m(h, std::vector< T >(w));\n for(std::vector< T >& v : m) for(T& x : v) std::cin >> x;\n return m;\n }\n };\n struct speedup {\n speedup() {\n std::cin.tie(0);\n std::ios::sync_with_stdio(0);\n }\n } speedup_instance;\n}\nscanner::sca in() { return scanner::sca(); }\nscanner::vec in(int n) { return scanner::vec(n); }\nscanner::mat in(int h, int w) { return scanner::mat(h, w); }\n\nnamespace printer {\n void precision(int d) { std::cout << std::fixed << std::setprecision(d); }\n void flush() { std::cout.flush(); }\n}\n\ntemplate < class T >\nostream& operator<<(ostream& os, const std::vector< T > a) {\n int n = a.size();\n for(int i : rep(n)) { os << a[i]; if(i != n - 1) os << ' '; }\n return os;\n}\n\nint print() { std::cout << '\\n'; return 0; }\ntemplate < class head, class... tail > int print(head&& h, tail&&... t) {\n std::cout << h; if(sizeof...(tail)) std::cout << ' ';\n return print(std::forward<tail>(t)...);\n}\ntemplate < class T > int print_n(const std::vector< T > a) {\n int n = a.size();\n for(int i : rep(n)) std::cout << a[i] << \"\\n\";\n return 0;\n}\n\n\n#line 2 \"cp-library/src/utility/key_val.hpp\"\n\ntemplate < class K, class V >\nstruct key_val {\n K key; V val;\n key_val() {}\n key_val(K key, V val) : key(key), val(val) {}\n template < std::size_t Index >\n std::tuple_element_t< Index, key_val >& get() {\n if constexpr (Index == 0) return key;\n if constexpr (Index == 1) return val;\n }\n};\n\nnamespace std {\n\ntemplate < class K, class V > struct tuple_size < key_val< K, V > > : integral_constant< size_t, 2 > {};\ntemplate < class K, class V > struct tuple_element < 0, key_val< K, V > > { using type = K; };\ntemplate < class K, class V > struct tuple_element < 1, key_val< K, V > > { using type = V; };\n\n}\n#line 2 \"cp-library/src/utility/vec_op.hpp\"\ntemplate < class T > key_val< int, T > max_of(const vector< T >& a) {\n int i = std::max_element(a.begin(), a.end()) - a.begin();\n return {i, a[i]};\n}\ntemplate < class T > key_val< int, T > min_of(const vector< T >& a) {\n int i = std::min_element(a.begin(), a.end()) - a.begin();\n return {i, a[i]};\n}\ntemplate < class S, class T > S sum_of(const vector< T >& a) {\n S sum = 0;\n for(const T x : a) sum += x;\n return sum;\n}\ntemplate < class S, class T > vector< S > freq_of(const vector< T >& a, T L, T R) {\n vector< S > res(R - L, S(0));\n for(const T x : a) res[x - L] += 1;\n return res;\n}\ntemplate < class S, class T > struct prefix_sum {\n vector< S > s;\n prefix_sum(const vector< T >& a) : s(a) {\n s.insert(s.begin(), S(0));\n for(int i : rep(a.size())) s[i + 1] += s[i];\n }\n // [L, R)\n S sum(int L, int R) { return s[R] - s[L]; }\n};\n#line 3 \"cp-library/src/utility/heap.hpp\"\n\ntemplate < class T > using heap_min = std::priority_queue< T, std::vector< T >, std::greater< T > >;\ntemplate < class T > using heap_max = std::priority_queue< T, std::vector< T >, std::less< T > >;\n\n#line 27 \"cp-library/src/cp-template.hpp\"\n\n#line 1 \"cp-library/src/algorithm/bin_search.hpp\"\ntemplate < class T, class F >\nT bin_search(T ok, T ng, F f) {\n while(abs(ng - ok) > 1) {\n T mid = (ok + ng) / 2;\n (f(mid) ? ok : ng) = mid;\n }\n return ok;\n}\n\ntemplate < class T, class F >\nT bin_search_real(T ok, T ng, F f, int step = 80) {\n while(step--) {\n T mid = (ok + ng) / 2;\n (f(mid) ? ok : ng) = mid;\n }\n return ok;\n}\n#line 2 \"cp-library/src/algorithm/argsort.hpp\"\n\ntemplate < class T > std::vector< int > argsort(const std::vector< T > &a) {\n std::vector< int > ids((int)a.size());\n std::iota(ids.begin(), ids.end(), 0);\n std::sort(ids.begin(), ids.end(), [&](int i, int j) {\n return a[i] < a[j] || (a[i] == a[j] && i < j);\n });\n return ids;\n}\n#line 1 \"macro.hpp\"\nnamespace macro {\n\nusing size_type = int;\ntemplate < class container > void sort(container& a) { std::sort(std:: begin(a), std:: end(a)); }\ntemplate < class container > void rsort(container& a) { std::sort(std::rbegin(a), std::rend(a)); }\ntemplate < class container > void reverse(container& a) { std::reverse(std::begin(a), std::end(a)); }\ntemplate < class container > void unique(container& a) {\n std::sort(std::begin(a), std::end(a));\n a.erase(std::unique(std::begin(a), std::end(a)), std::end(a));\n}\ntemplate < class container > container sorted(const container& a) { container b = a; sort(b); return std::move(b); }\ntemplate < class container > container rsorted(const container& a) { container b = a; rsort(b); return std::move(b); }\ntemplate < class container, class compare > void sort(container& a, const compare& cmp) { std::sort(std::begin(a), std::end(a), cmp); }\ntemplate < class container, class compare > container sorted(const container& a, const compare& cmp) { container b = a; sort(b, cmp); return std::move(b); }\ntemplate < class container, class value > size_type lower_bound(const container& a, const value& x) { return std::lower_bound(std::begin(a), std::end(a), x) - std::begin(a); }\ntemplate < class container, class value > size_type upper_bound(const container& a, const value& x) { return std::upper_bound(std::begin(a), std::end(a), x) - std::begin(a); }\n\nconst std::vector<std::pair<size_type, size_type>> dir4 = { {+1, 0}, {-1, 0}, { 0, +1}, { 0, -1} };\nconst std::vector<std::pair<size_type, size_type>> dir8 = { {-1, -1}, {-1, 0}, {-1, +1}, { 0, -1}, { 0, +1}, {+1, -1}, {+1, 0}, {+1, +1} };\n\n#ifdef _DEBUG\n#define debug(x) std::cout << \"[\" << __LINE__ << \"] \" << #x << \": \" << x << std::endl\n#else\n#define debug(x)\n#endif\n\ntemplate < class container > void concat(container& a, const container& b) {\n a.insert(std::end(a), std::begin(b), std::end(b));\n}\nstd::vector<size_type> iota(const size_type n) {\n std::vector<size_type> I(n);\n std::iota(std::begin(I), std::end(I), 0);\n return I;\n}\ntemplate < class container > std::vector<size_type> sort_idx(const container& a) {\n const size_type n = a.size();\n std::vector<size_type> I = iota(n);\n std::sort(std::begin(I), std::end(I), [&](size_type i, size_type j) { return a[i] < a[j] or (a[i] == a[j] and i < j); });\n return I;\n}\ntemplate < class container, class compare > std::vector<size_type> sort_idx(const container& a, const compare& cmp) {\n const size_type n = a.size();\n std::vector<size_type> I = iota(n);\n std::sort(std::begin(I), std::end(I), [&](size_type i, size_type j) { return cmp(a[i], a[j]) or (a[i] == a[j] and i < j); });\n return std::move(I);\n}\n\nstruct grid {\n using size_type = int;\n size_type H, W;\n grid(const size_type H, const size_type W) : H(H), W(W) {}\n bool contains(const size_type i, const size_type j) {\n return 0 <= i and i < H and 0 <= j and j < W;\n }\n};\n\n} // namespace macro\n\nusing namespace macro;\n#line 3 \"A.cpp\"\n\nint solve(int n) {\n vector<string> s = in(n);\n {\n vector<string> ns;\n for(int i : rep(n)) {\n bool substr = false;\n for(int j : rep(n)) if(j != i and s[i].size() <= s[j].size()) {\n for(int k : rep(s[j].size())) {\n if(s[j].substr(k, s[i].size()) == s[i]) {\n substr = true;\n }\n }\n }\n if(!substr) ns.push_back(s[i]);\n }\n s = move(ns);\n n = s.size();\n }\n\n vector dp(1 << n, vector(n, int(1e9)));\n for(int i : rep(n)) dp[1 << i][i] = s[i].size();\n\n for(int S : rep(1 << n)) if(S) {\n for(int i : rep(n)) if(S & (1 << i)) {\n for(int j : rep(n)) if(!(S & (1 << j))) {\n for(int k : revrep(int(s[i].size()))) {\n if(s[i].substr(s[i].size() - k, k) == s[j].substr(0, k)) {\n chmin(dp[S | (1 << j)][j], dp[S][i] + int(s[j].size() - k));\n break;\n }\n }\n }\n }\n }\n\n int ans = 1e9;\n for(int i : rep(n)) chmin(ans, dp[(1 << n) - 1][i]);\n return ans;\n}\n\nint main() {\n while(true) {\n int n = in(); if(n == 0) break;\n print(solve(n));\n }\n}", "accuracy": 1, "time_ms": 6670, "memory_kb": 4804, "score_of_the_acc": -0.9977, "final_rank": 18 }, { "submission_id": "aoj_1320_8586452", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\nconst int INF = 1<<29;\nint n, fnt[15][15];\nstring s[15];\nint memo[1<<15][15];\nint dp(int bit, int rht, int cnt){\n if(cnt>=n) return 0;\n if(memo[bit][rht]!=-1) return memo[bit][rht];\n int res = INF;\n for(int i=0;i<n;i++){\n if((bit&1<<i)==0) res = min(res, dp((bit|1<<i), i, cnt+1)+fnt[rht][i]);\n }\n return memo[bit][rht] = res;\n}\nint main(){\n while(cin >> n, n){\n for(int i=0;i<n;i++) cin >> s[i];\n \n for(int i=0;i<n;i++){\n fnt[n][i] = s[i].size();\n for(int j=0;j<n;j++){\n if(i==j) continue;\n fnt[i][j] = s[j].size();\n for(int k=0;k<s[i].size();k++){\n int sel = s[i].size()-k;\n if(s[i].substr(0, k)+s[j].substr(0, sel) == s[i]) fnt[i][j] = min(fnt[i][j], (int)s[j].size()-sel);\n }\n }\n }\n \n int bit=0, cnt=0;\n for(int i=0;i<n;i++){\n bool ok = false;\n for(int j=0;j<n;j++){\n if(i==j) continue;\n if(s[j].find(s[i])!=string::npos) ok = true;\n }\n if(ok){\n bit |= 1<<i;\n cnt++;\n }\n }\n memset(memo, -1, sizeof(memo));\n cout << dp(bit, n, cnt) << endl;\n \n }\n return(0);\n}", "accuracy": 1, "time_ms": 240, "memory_kb": 4980, "score_of_the_acc": -0.0223, "final_rank": 10 }, { "submission_id": "aoj_1320_8474991", "code_snippet": "// #define _GLIBCXX_DEBUG\n#pragma GCC optimize(\"O2,no-stack-protector,unroll-loops,fast-math\")\n#include <bits/stdc++.h>\nusing namespace std;\n#define rep(i, n) for (int i = 0; i < int(n); i++)\n#define per(i, n) for (int i = (n)-1; 0 <= i; i--)\n#define rep2(i, l, r) for (int i = (l); i < int(r); i++)\n#define per2(i, l, r) for (int i = (r)-1; int(l) <= i; i--)\n#define each(e, v) for (auto& e : v)\n#define MM << \" \" <<\n#define pb push_back\n#define eb emplace_back\n#define all(x) begin(x), end(x)\n#define rall(x) rbegin(x), rend(x)\n#define sz(x) (int)x.size()\ntemplate <typename T> void print(const vector<T>& v, T x = 0) {\n int n = v.size();\n for (int i = 0; i < n; i++) cout << v[i] + x << (i == n - 1 ? '\\n' : ' ');\n if (v.empty()) cout << '\\n';\n}\nusing ll = long long;\nusing pii = pair<int, int>;\nusing pll = pair<ll, ll>;\ntemplate <typename T> bool chmax(T& x, const T& y) {\n return (x < y) ? (x = y, true) : false;\n}\ntemplate <typename T> bool chmin(T& x, const T& y) {\n return (x > y) ? (x = y, true) : false;\n}\ntemplate <class T>\nusing minheap = std::priority_queue<T, std::vector<T>, std::greater<T>>;\ntemplate <class T> using maxheap = std::priority_queue<T>;\ntemplate <typename T> int lb(const vector<T>& v, T x) {\n return lower_bound(begin(v), end(v), x) - begin(v);\n}\ntemplate <typename T> int ub(const vector<T>& v, T x) {\n return upper_bound(begin(v), end(v), x) - begin(v);\n}\ntemplate <typename T> void rearrange(vector<T>& v) {\n sort(begin(v), end(v));\n v.erase(unique(begin(v), end(v)), end(v));\n}\n\n// __int128_t gcd(__int128_t a, __int128_t b) {\n// if (a == 0)\n// return b;\n// if (b == 0)\n// return a;\n// __int128_t cnt = a % b;\n// while (cnt != 0) {\n// a = b;\n// b = cnt;\n// cnt = a % b;\n// }\n// return b;\n// }\n\nstruct Union_Find_Tree {\n vector<int> data;\n const int n;\n int cnt;\n \n Union_Find_Tree(int n) : data(n, -1), n(n), cnt(n) {}\n \n int root(int x) {\n if (data[x] < 0) return x;\n return data[x] = root(data[x]);\n }\n \n int operator[](int i) { return root(i); }\n \n bool unite(int x, int y) {\n x = root(x), y = root(y);\n if (x == y) return false;\n if (data[x] > data[y]) swap(x, y);\n data[x] += data[y], data[y] = x;\n cnt--;\n return true;\n }\n \n int size(int x) { return -data[root(x)]; }\n \n int count() { return cnt; };\n \n bool same(int x, int y) { return root(x) == root(y); }\n \n void clear() {\n cnt = n;\n fill(begin(data), end(data), -1);\n }\n};\n\ntemplate <int mod> struct Mod_Int {\n int x;\n\n Mod_Int() : x(0) {}\n\n Mod_Int(long long y) : x(y >= 0 ? y % mod : (mod - (-y) % mod) % mod) {}\n\n static int get_mod() { return mod; }\n\n Mod_Int& operator+=(const Mod_Int& p) {\n if ((x += p.x) >= mod) x -= mod;\n return *this;\n }\n\n Mod_Int& operator-=(const Mod_Int& p) {\n if ((x += mod - p.x) >= mod) x -= mod;\n return *this;\n }\n\n Mod_Int& operator*=(const Mod_Int& p) {\n x = (int)(1LL * x * p.x % mod);\n return *this;\n }\n\n Mod_Int& operator/=(const Mod_Int& p) {\n *this *= p.inverse();\n return *this;\n }\n\n Mod_Int& operator++() { return *this += Mod_Int(1); }\n\n Mod_Int operator++(int) {\n Mod_Int tmp = *this;\n ++*this;\n return tmp;\n }\n\n Mod_Int& operator--() { return *this -= Mod_Int(1); }\n\n Mod_Int operator--(int) {\n Mod_Int tmp = *this;\n --*this;\n return tmp;\n }\n\n Mod_Int operator-() const { return Mod_Int(-x); }\n\n Mod_Int operator+(const Mod_Int& p) const { return Mod_Int(*this) += p; }\n\n Mod_Int operator-(const Mod_Int& p) const { return Mod_Int(*this) -= p; }\n\n Mod_Int operator*(const Mod_Int& p) const { return Mod_Int(*this) *= p; }\n\n Mod_Int operator/(const Mod_Int& p) const { return Mod_Int(*this) /= p; }\n\n bool operator==(const Mod_Int& p) const { return x == p.x; }\n\n bool operator!=(const Mod_Int& p) const { return x != p.x; }\n\n Mod_Int inverse() const {\n assert(*this != Mod_Int(0));\n return pow(mod - 2);\n }\n\n Mod_Int pow(long long k) const {\n Mod_Int now = *this, ret = 1;\n for (; k > 0; k >>= 1, now *= now) {\n if (k & 1) ret *= now;\n }\n return ret;\n }\n\n friend ostream& operator<<(ostream& os, const Mod_Int& p) {\n return os << p.x;\n }\n\n friend istream& operator>>(istream& is, Mod_Int& p) {\n long long a;\n is >> a;\n p = Mod_Int<mod>(a);\n return is;\n }\n};\n\nll mpow2(ll x, ll n, ll mod) {\n ll ans = 1;\n x %= mod;\n while (n != 0) {\n if (n & 1) ans = ans * x % mod;\n x = x * x % mod;\n n = n >> 1;\n }\n ans %= mod;\n return ans;\n}\n\ntemplate <typename T> T modinv(T a, const T& m) {\n T b = m, u = 1, v = 0;\n while (b > 0) {\n T t = a / b;\n swap(a -= t * b, b);\n swap(u -= t * v, v);\n }\n return u >= 0 ? u % m : (m - (-u) % m) % m;\n}\n\nll divide_int(ll a, ll b) {\n if (b < 0) a = -a, b = -b;\n return (a >= 0 ? a / b : (a - b + 1) / b);\n}\n\n// const int MOD = 1000000007;\nconst int MOD = 998244353;\nusing mint = Mod_Int<MOD>;\n\n// ----- library -------\n// ----- library -------\n\nint main() {\n ios::sync_with_stdio(false);\n std::cin.tie(nullptr);\n cout << fixed << setprecision(15);\n\n while (1) {\n int n;\n cin >> n;\n if (!n)\n break;\n vector<string> s(n);\n rep(i, n) cin >> s[i];\n vector<vector<int>> c(n, vector<int>(n));\n rep(i, n) rep(j, n) {\n int li = sz(s[i]), lj = sz(s[j]);\n c[i][j] = lj;\n rep(k, li) {\n bool ok = true;\n int val = lj;\n rep(l, min(lj, li - k)) {\n if (s[i][k + l] != s[j][l]) {\n ok = false;\n break;\n }\n val--;\n }\n if (ok)\n chmin(c[i][j], val);\n }\n }\n vector<vector<int>> dp(1 << n, vector<int>(n, 1e9));\n rep(i, n) dp[1 << i][i] = sz(s[i]);\n rep(t, 1 << n) rep(i, n) rep(j, n) {\n if (t >> j & 1)\n continue;\n chmin(dp[t | (1 << j)][c[i][j] == 0 ? i : j], dp[t][i] + c[i][j]);\n }\n cout << *min_element(all(dp[(1 << n) - 1])) << '\\n';\n }\n}", "accuracy": 1, "time_ms": 350, "memory_kb": 4748, "score_of_the_acc": -0.0367, "final_rank": 15 }, { "submission_id": "aoj_1320_8331841", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\nconst int INF = 1012345678;\n\nint calc_extra(const string& S, const string& T) {\n\tint ans = T.size();\n\tfor (int i = 1; i <= S.size() && i <= T.size(); i++) {\n\t\tif (S.substr(S.size() - i) == T.substr(0, i)) {\n\t\t\tans = T.size() - i;\n\t\t}\n\t}\n\treturn ans;\n}\n\nint solve_sequence(int N, const vector<string>& S) {\n\tvector<vector<int> > extras(N, vector<int>(N));\n\tfor (int i = 0; i < N; i++) {\n\t\tfor (int j = 0; j < N; j++) {\n\t\t\textras[i][j] = calc_extra(S[i], S[j]);\n\t\t}\n\t}\n\tvector<vector<int> > dp(1 << N, vector<int>(N, INF));\n\tfor (int i = 0; i < N; i++) {\n\t\tdp[1 << i][i] = S[i].size();\n\t}\n\tfor (int i = 1; i < (1 << N); i++) {\n\t\tif ((i & (i - 1)) == 0) {\n\t\t\tcontinue; // i = 2^k\n\t\t}\n\t\tfor (int j = 0; j < N; j++) {\n\t\t\tif ((i >> j) & 1) {\n\t\t\t\tint sub = i - (1 << j);\n\t\t\t\tfor (int k = 0; k < N; k++) {\n\t\t\t\t\tif ((sub >> k) & 1) {\n\t\t\t\t\t\tdp[i][j] = min(dp[i][j], dp[sub][k] + extras[k][j]);\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n\tint ans = *min_element(dp[(1 << N) - 1].begin(), dp[(1 << N) - 1].end());\n\treturn ans;\n}\n\nint solve(int N, const vector<string>& S) {\n\tvector<string> T;\n\tfor (int i = 0; i < N; i++) {\n\t\tbool flag = true;\n\t\tfor (int j = 0; j < N; j++) {\n\t\t\tif ((S[j] == S[i] && j < i) || (S[j] != S[i] && S[j].find(S[i]) != string::npos)) {\n\t\t\t\tflag = false;\n\t\t\t}\n\t\t}\n\t\tif (flag) {\n\t\t\tT.push_back(S[i]);\n\t\t}\n\t}\n\treturn solve_sequence(T.size(), T);\n}\n\nint main() {\n\twhile (true) {\n\t\tint N;\n\t\tcin >> N;\n\t\tif (N == 0) {\n\t\t\tbreak;\n\t\t}\n\t\tvector<string> S(N);\n\t\tfor (int i = 0; i < N; i++) {\n\t\t\tcin >> S[i];\n\t\t}\n\t\tint ans = solve(N, S);\n\t\tcout << ans << endl;\n\t}\n\treturn 0;\n}", "accuracy": 1, "time_ms": 180, "memory_kb": 4528, "score_of_the_acc": -0.0086, "final_rank": 3 }, { "submission_id": "aoj_1320_6775480", "code_snippet": "// I SELL YOU...! \n#include<iostream>\n#include<vector>\n#include<algorithm>\n#include<functional>\n#include<queue>\n#include<chrono>\n#include<iomanip>\n#include<map>\n#include<set>\n#include<bitset>\nusing namespace std;\nusing ll = long long;\nusing P = pair<ll,ll>;\nusing TP = tuple<ll,ll,ll>;\nvoid init_io(){\n cin.tie(0);\n ios::sync_with_stdio(false);\n cout << fixed << setprecision(18);\n}\nconst ll INF = 1e15;\nll calc_dp(ll s, ll i, const vector<vector<ll>> &pat, vector<vector<ll>> &dp) {\n ll n = pat.size();\n ll k = 0;\n if (dp[s][i] != -1) return dp[s][i];\n for (int i=0;i<n;i++) {\n if ((1ll << i) & s) k++;\n }\n if (k==1) {\n return dp[s][i] = pat[i][i];\n }\n ll prev_s = s ^ (1ll << i);\n ll res = INF;\n for (int j=0;j<n;j++) {\n if (i==j || ((prev_s & (1ll << j)) == 0)) continue;\n res = min(res, calc_dp(prev_s, j, pat, dp) + pat[i][i] - pat[j][i]);\n }\n // cout << bitset<14>(s) <<\" \"<<i <<\" \"<<res << endl;\n return dp[s][i] = res;\n};\n\nll max_match(const string &s, const string &t) {\n ll n = s.size(), m = t.size();\n ll res = 0;\n for (int i=1;i<=min(n, m);i++) {\n bool can = true;\n for (int j=0;j<i;j++) {\n if (s[(n-i)+j] != t[j]) {\n can = false;\n break;\n }\n }\n if (can) {\n res = i;\n }\n }\n return res;\n};\nvoid solve() {\n ll n;\n cin >> n;\n if (n==0) return;\n vector<string> s(n);\n for(int i=0;i<n;i++) {\n cin >> s[i];\n }\n for (auto itr=s.begin();itr!=s.end();) {\n ll c = 0;\n for (auto t: s) {\n if (t == *itr) {\n if (c==0) {\n c++;\n continue;\n }\n }\n if (t.find(*itr) != string::npos) {\n c = -1;\n break;\n }\n }\n if (c==-1) {\n itr = s.erase(itr);\n } else {\n itr++;\n }\n }\n n = s.size();\n vector<vector<ll>> dp((1ll<<n), vector<ll>(n, -1));\n vector<vector<ll>> pat(n, vector<ll>(n, 0));\n for (int i=0;i<n;i++) {\n for (int j=0;j<n;j++) {\n if (i==j) {\n pat[i][i] = s[i].size();\n } else {\n pat[i][j] = max_match(s[i], s[j]);\n }\n }\n }\n ll ans = 1e15;\n for (int i=0;i<n;i++) {\n ans = min(ans, calc_dp((1ll<<n) -1, i,pat, dp));\n }\n cout << ans << endl;\n solve();\n}\nsigned main(){\n init_io();\n solve();\n}", "accuracy": 1, "time_ms": 320, "memory_kb": 101052, "score_of_the_acc": -1.0258, "final_rank": 20 }, { "submission_id": "aoj_1320_6754394", "code_snippet": "#include <bits/stdc++.h>\n#include <chrono>\n#include <thread>\n////#include <atcoder/all>\n\n//using namespace atcoder;\nusing namespace std;\nusing ll = long long;\nusing vll = vector<ll>;\nusing vvll = vector<vll>;\nusing vvvll = vector<vvll>;\nusing vb = vector<bool>;\nusing vvb = vector<vb>;\nusing vvvb = vector<vvb>;\nusing vd = vector<double>;\nusing vvd = vector<vd>;\nusing vvvd = vector<vvd>;\n#define all(A) A.begin(),A.end()\n#define ALL(A) A.begin(),A.end()\n#define rep(i, n) for (ll i = 0; i < (ll) (n); i++)\nusing pqr = priority_queue<pair<ll, ll>, vector<pair<ll, ll>>, greater<pair<ll, ll>>>;\n\ntemplate<class T>\nbool chmax(T& p, T q) {\n if (p < q) {\n p = q;\n return 1;\n }\n else {\n return 0;\n }\n}\ntemplate<class T>\nbool chmin(T& p, T q) {\n if (p > q) {\n p = q;\n return 1;\n }\n else {\n return 0;\n }\n}\n\nll gcd(ll(a), ll(b)) {\n if (b == 0)return a;\n ll c = a;\n while (a % b != 0) {\n c = a % b;\n a = b;\n b = c;\n }\n return b;\n}\n\nll Eulers_phi(ll n) {\n ll res = 0;\n ll N = n;\n vll P;\n for (ll p = 2; p * p <= N; p++) {\n if (N % p == 0) {\n P.push_back(p);\n while (N % p == 0)N /= p;\n }\n }\n if (N != 1)P.push_back(N);\n\n N = P.size();\n rep(bit, (1 << N)) {\n ll k = 1;\n ll t = 0;\n rep(i, N) {\n if (bit & (1 << i)) {\n k *= P[i];\n t++;\n }\n }\n res += (t % 2 == 0 ? 1 : -1) * n / k;\n }\n return res;\n\n}\n\nmap<string, ll> M;\n\nbool OK = 1;\nll dfs(string S) {\n ll res = 0;\n ll N = S.size();\n rep(i, N) {\n ll d = 0;\n ll R = i;\n if (S[i] <= 'Z' && S[i] >= 'A') {\n string G = \"\";\n if (S.size() > 1 && (S[i + 1] <= 'z' && S[i + 1] > 'a')) {\n G = S.substr(i, 2);\n R = i + 1;\n }\n else {\n G = S.substr(i, 1);\n R = i;\n }\n if (!M.count(G)) {\n OK = 0;\n return res;\n }\n d = M[G];\n }\n else if (S[i] == '(') {\n ll L = i;\n ll k = 0;\n for (ll j = i; j < N; j++) {\n if (S[j] == '(')k++;\n else if (S[j] == ')') {\n k--;\n if (k == 0) {\n d = dfs(S.substr(L + 1, j - L - 1));\n R = j;\n break;\n }\n }\n }\n }\n else continue;\n ll b = 1;\n if (S.size() > R + 1) {\n if ('0' <= S[R + 1] && S[R + 1] <= '9') {\n b = S[R + 1] - '0';\n R++;\n if (S.size() > R + 1) {\n if ('0' <= S[R + 1] && S[R + 1] <= '9') {\n b = b * 10 + S[R + 1] - '0';\n R++;\n }\n }\n }\n }\n res += d * b;\n i = R;\n }\n\n\n return res;\n}\n\n\nint main() {\n cin.tie(nullptr);\n ios::sync_with_stdio(false);\n\n\n while (1) {\n ll N;\n cin >> N;\n if (N == 0)return 0;\n vector<string> S(N);\n rep(i, N)cin >> S[i];\n vector<bool> D(N, 1);\n rep(i, N) {\n rep(j, N) {\n if (i == j)continue;\n for (ll k = 0; k < ll(S[j].size()) - ll(S[i].size())+1; k++) {\n if (S[j].substr(k, S[i].size()) == S[i]) {\n D[i] = 0;\n }\n }\n }\n }\n vector<string> T;\n rep(i, N) {\n if (D[i])T.push_back(S[i]);\n }\n N = T.size();\n S = T;\n vvll DP((1 << N), vll(N, 1e18));\n rep(i, N) {\n DP[(1 << i)][i] = T[i].size();\n }\n\n vvll Z(N, vll(N,0));\n rep(i, N)rep(j, N) {\n rep(k, S[j].size()) {\n if (k == 0)continue;\n if (S[i].size()<k)break;\n string P = S[i].substr(S[i].size() - k, k);\n string Q = S[j].substr(0, k);\n if (P == Q) {\n Z[i][j] = k;\n }\n }\n }\n rep(bit, (1 << N)) {\n if (bit == 0)continue;\n rep(j, N) {\n if (!(bit & (1 << j)))continue;\n rep(n, N) {\n if (bit & (1 << n))continue;\n chmin(DP[bit + (1 << n)][n], DP[bit][j]+ll(T[n].size()) - Z[j][n]);\n }\n }\n }\n ll an = 1e18;\n rep(i, N)chmin(an, DP[(1 << N) - 1][i]);\n cout << an << endl;\n }\n\n}", "accuracy": 1, "time_ms": 180, "memory_kb": 5812, "score_of_the_acc": -0.0218, "final_rank": 9 }, { "submission_id": "aoj_1320_6710683", "code_snippet": "#include <algorithm>\n#include <bitset>\n#include <cassert>\n#include <chrono>\n#include <climits>\n#include <cmath>\n#include <cstddef>\n#include <cstdint>\n#include <cstdlib>\n#include <functional>\n#include <iomanip>\n#include <iostream>\n#include <limits>\n#include <map>\n#include <memory>\n#include <numeric>\n#include <optional>\n#include <queue>\n#include <random>\n#include <set>\n#include <stack>\n#include <string>\n#include <tuple>\n#include <type_traits>\n#include <unordered_map>\n#include <unordered_set>\n#include <utility>\n#include <vector>\n\n/* macro */\n\n#define rep(i, a, n) for (int i = (int)(a); i < (int)(n); i++)\n#define rrep(i, a, n) for (int i = ((int)(n - 1)); i >= (int)(a); i--)\n#define Rep(i, a, n) for (i64 i = (i64)(a); i < (i64)(n); i++)\n#define RRep(i, a, n) for (i64 i = ((i64)(n - i64(1))); i >= (i64)(a); i--)\n#define all(v) (v).begin(), (v).end()\n#define rall(v) (v).rbegin(), (v).rend()\n#define Bit(n) (1LL << (n))\n\n/* macro end */\n\n/* template */\n\nnamespace ebi {\n\n#ifdef LOCAL\n#define debug(...) \\\n std::cerr << \"LINE: \" << __LINE__ << \" [\" << #__VA_ARGS__ << \"]:\", \\\n debug_out(__VA_ARGS__)\n#else\n#define debug(...)\n#endif\n\nvoid debug_out() { std::cerr << std::endl; }\n\ntemplate <typename Head, typename... Tail>\nvoid debug_out(Head h, Tail... t) {\n std::cerr << \" \" << h;\n if (sizeof...(t) > 0) std::cout << \" :\";\n debug_out(t...);\n}\n\ntemplate <typename T1, typename T2>\nstd::ostream &operator<<(std::ostream &os, const std::pair<T1, T2> &pa) {\n return os << pa.first << \" \" << pa.second;\n}\n\ntemplate <typename T1, typename T2>\nstd::istream &operator>>(std::istream &os, std::pair<T1, T2> &pa) {\n return os >> pa.first >> pa.second;\n}\n\ntemplate <typename T>\nstd::ostream &operator<<(std::ostream &os, const std::vector<T> &vec) {\n for (std::size_t i = 0; i < vec.size(); i++)\n os << vec[i] << (i + 1 == vec.size() ? \"\" : \" \");\n return os;\n}\n\ntemplate <typename T>\nstd::istream &operator>>(std::istream &os, std::vector<T> &vec) {\n for (T &e : vec) std::cin >> e;\n return os;\n}\n\ntemplate <typename T>\nstd::ostream &operator<<(std::ostream &os, const std::optional<T> &opt) {\n if (opt) {\n os << opt.value();\n } else {\n os << \"invalid value\";\n }\n return os;\n}\n\nusing std::size_t;\nusing i32 = std::int32_t;\nusing u32 = std::uint32_t;\nusing i64 = std::int64_t;\nusing u64 = std::uint64_t;\n\ntemplate <class T, T init>\nauto make_vector(int n) {\n return std::vector<T>(n, init);\n}\n\ntemplate <class T, T init, typename Head, typename... Tail>\nauto make_vector(Head n, Tail... ts) {\n return std::vector(n, make_vector<T, init>(ts...));\n}\n\ntemplate <class T>\ninline bool chmin(T &a, T b) {\n if (a > b) {\n a = b;\n return true;\n }\n return false;\n}\n\ntemplate <class T>\ninline bool chmax(T &a, T b) {\n if (a < b) {\n a = b;\n return true;\n }\n return false;\n}\n\ntemplate <class T>\nT pow(T x, i64 n) {\n T res = 1;\n while (n > 0) {\n if (n & 1) res = res * x;\n x = x * x;\n n >>= 1;\n }\n return res;\n}\n\ntemplate <class T>\nT mod_pow(T x, i64 n, i64 mod) {\n T res = 1;\n while (n > 0) {\n if (n & 1) res = (res * x) % mod;\n x = (x * x) % mod;\n n >>= 1;\n }\n return res;\n}\n\ntemplate <class T>\nT scan() {\n T val;\n std::cin >> val;\n return val;\n}\n\ntemplate <class T>\nstruct Edge {\n int to;\n T cost;\n Edge(int _to, T _cost = 1) : to(_to), cost(_cost) {}\n};\n\ntemplate <class T>\nstruct Graph : std::vector<std::vector<Edge<T>>> {\n using std::vector<std::vector<Edge<T>>>::vector;\n void add_edge(int u, int v, T w, bool directed = false) {\n (*this)[u].emplace_back(v, w);\n if (directed) return;\n (*this)[v].emplace_back(u, w);\n }\n};\n\nstruct graph : std::vector<std::vector<int>> {\n using std::vector<std::vector<int>>::vector;\n void add_edge(int u, int v, bool directed = false) {\n (*this)[u].emplace_back(v);\n if (directed) return;\n (*this)[v].emplace_back(u);\n }\n};\n\nconstexpr i64 LNF = std::numeric_limits<i64>::max() / 4;\n\nconstexpr int INF = std::numeric_limits<int>::max() / 2;\n\nconst std::vector<int> dy = {1, 0, -1, 0, 1, 1, -1, -1};\nconst std::vector<int> dx = {0, 1, 0, -1, 1, -1, 1, -1};\n\n} // namespace ebi\n\nnamespace ebi {\n\nvoid main_() {\n int n;\n while (std::cin >> n, n != 0) {\n std::vector<std::string> s(n);\n std::cin >> s;\n {\n std::vector<std::string> cp;\n rep(i, 0, n) {\n bool flag = false;\n rep(j, 0, n) {\n if (flag) break;\n if (i == j) continue;\n int isz = s[i].size();\n int jsz = s[j].size();\n if (isz >= jsz) continue;\n rep(k, 0, jsz) {\n if (k + isz > jsz) break;\n if (s[j].substr(k, isz) == s[i]) {\n flag = true;\n break;\n }\n }\n }\n if (flag) continue;\n cp.emplace_back(s[i]);\n }\n s = cp;\n }\n n = s.size();\n std::vector dp(1 << n, std::vector<int>(n, INF));\n std::vector duplicates(n, std::vector<int>(n, 0));\n rep(i,0,n) rep(j,0,n) {\n int isz = s[i].size();\n int jsz = s[j].size();\n rep(k,0,isz) {\n bool flag = true;\n rep(l,0,isz-k) {\n if(l >= jsz || s[i][k+l] != s[j][l]) {\n flag = false;\n break;\n }\n }\n if(flag) {\n duplicates[i][j] = isz - k;\n break;\n }\n }\n }\n auto f = [&](auto &&self, int bit, int back) -> int {\n if(dp[bit][back] != INF) return dp[bit][back];\n if(bit == (1<<back)) return dp[bit][back] = s[back].size();\n int prev = bit ^ (1<<back);\n rep(i,0,n) if((prev >> i) & 1) {\n chmin(dp[bit][back], self(self, prev, i) + (int)s[back].size() - duplicates[i][back]);\n }\n return dp[bit][back];\n };\n int ans = INF;\n rep(i, 0, n) chmin(ans, f(f, (1 << n) - 1, i));\n std::cout << ans << '\\n';\n }\n}\n\n} // namespace ebi\n\nint main() {\n std::cout << std::fixed << std::setprecision(15);\n std::cin.tie(nullptr);\n std::ios::sync_with_stdio(false);\n int t = 1;\n // std::cin >> t;\n while (t--) {\n ebi::main_();\n }\n return 0;\n}", "accuracy": 1, "time_ms": 260, "memory_kb": 4792, "score_of_the_acc": -0.0234, "final_rank": 11 }, { "submission_id": "aoj_1320_6706859", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\n#ifdef _RUTHEN\n#include \"debug.hpp\"\n#else\n#define show(...) true\n#endif\n\nusing ll = long long;\n#define rep(i, n) for (int i = 0; i < (n); i++)\ntemplate <class T> using V = vector<T>;\n\nint solve2(V<string>& T) {\n const int INF = 1 << 30;\n int N = T.size();\n int N2 = 1 << N;\n V<V<int>> cost(N, V<int>(N, INF));\n rep(las, N) {\n rep(nex, N) {\n if (las == nex) continue;\n int l2 = T[nex].size();\n for (int sz = l2 - 1; sz >= 0; sz--) {\n // S[nex].substr(0, sz)\n if (T[las].size() < sz) continue;\n if (T[las].substr(T[las].size() - sz) == T[nex].substr(0, sz)) {\n cost[las][nex] = l2 - sz;\n break;\n }\n }\n }\n }\n V<V<int>> dp(N2, V<int>(N, INF));\n rep(i, N) dp[1 << i][i] = T[i].size();\n for (int s = 1; s < N2; s++) {\n for (int las = 0; las < N; las++) {\n if ((s >> las & 1) == 0) continue;\n if (dp[s][las] == INF) continue;\n for (int nex = 0; nex < N; nex++) {\n if (s >> nex & 1) continue;\n dp[s | (1 << nex)][nex] = min(dp[s | (1 << nex)][nex], dp[s][las] + cost[las][nex]);\n }\n }\n }\n int ans = *min_element(dp[N2 - 1].begin(), dp[N2 - 1].end());\n return ans;\n}\n\nvoid solve(int N) {\n V<string> S(N);\n rep(i, N) cin >> S[i];\n V<int> cont(N, 0);\n rep(i, N) {\n // check S[i] in S[j]\n int lsi = S[i].size();\n rep(j, N) {\n if (i == j) continue;\n int lsj = S[j].size();\n if (lsi > lsj) continue;\n rep(k, lsj - lsi + 1) {\n if (S[i] == S[j].substr(k, lsi)) {\n cont[i] = 1;\n break;\n }\n }\n if (cont[i]) break;\n }\n }\n V<string> T;\n rep(i, N) if (cont[i] == 0) T.push_back(S[i]);\n cout << solve2(T) << '\\n';\n return;\n}\n\nint main() {\n ios::sync_with_stdio(false);\n cin.tie(0);\n int N;\n while (cin >> N, N != 0) solve(N);\n return 0;\n}", "accuracy": 1, "time_ms": 150, "memory_kb": 4572, "score_of_the_acc": -0.0045, "final_rank": 2 }, { "submission_id": "aoj_1320_6706850", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\n#ifdef _RUTHEN\n#include \"debug.hpp\"\n#else\n#define show(...) true\n#endif\n\nusing ll = long long;\n#define rep(i, n) for (int i = 0; i < (n); i++)\ntemplate <class T> using V = vector<T>;\n\nint solve2(V<string>& T) {\n const int INF = 1 << 30;\n int N = T.size();\n int N2 = 1 << N;\n V<V<int>> dp(N2, V<int>(N, INF));\n rep(i, N) dp[1 << i][i] = T[i].size();\n for (int s = 1; s < N2; s++) {\n for (int las = 0; las < N; las++) {\n if ((s >> las & 1) == 0) continue;\n if (dp[s][las] == INF) continue;\n for (int nex = 0; nex < N; nex++) {\n if (s >> nex & 1) continue;\n int l2 = T[nex].size();\n for (int sz = l2 - 1; sz >= 0; sz--) {\n // S[nex].substr(0, sz)\n if (T[las].size() < sz) continue;\n if (T[las].substr(T[las].size() - sz) == T[nex].substr(0, sz)) {\n dp[s | (1 << nex)][nex] = min(dp[s | (1 << nex)][nex], dp[s][las] + l2 - sz);\n break;\n }\n }\n }\n }\n }\n int ans = *min_element(dp[N2 - 1].begin(), dp[N2 - 1].end());\n return ans;\n}\n\nvoid solve(int N) {\n V<string> S(N);\n rep(i, N) cin >> S[i];\n V<int> cont(N, 0);\n rep(i, N) {\n // check S[i] in S[j]\n int lsi = S[i].size();\n rep(j, N) {\n if (i == j) continue;\n int lsj = S[j].size();\n if (lsi > lsj) continue;\n rep(k, lsj - lsi + 1) {\n if (S[i] == S[j].substr(k, lsi)) {\n cont[i] = 1;\n break;\n }\n }\n if (cont[i]) break;\n }\n }\n show(cont);\n V<string> T;\n rep(i, N) if (cont[i] == 0) T.push_back(S[i]);\n cout << solve2(T) << '\\n';\n return;\n}\n\nint main() {\n ios::sync_with_stdio(false);\n cin.tie(0);\n int N;\n while (cin >> N, N != 0) solve(N);\n return 0;\n}", "accuracy": 1, "time_ms": 6730, "memory_kb": 4676, "score_of_the_acc": -1.0055, "final_rank": 19 }, { "submission_id": "aoj_1320_6179467", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\n\nint main(void){\n while(1){\n int n;\n cin >> n;\n if(n == 0) break;\n vector<string> S(n);\n for(int i = 0; i < n; i++) cin >> S[i];\n vector<string> T;\n for(int i = 0; i < n; i++){\n bool ok = true;\n for(int j = 0; j < n; j++){\n if(i == j) continue;\n int found = S[j].find(S[i]);\n if(found != string::npos){\n ok = false;\n break;\n }\n }\n if(ok) T.push_back(S[i]);\n }\n S = T;\n n = S.size();\n vector<vector<int>> common(n, vector<int>(n, 0));\n for(int i = 0; i < n; i++){\n for(int j = 0; j < n; j++){\n if(i == j) continue;\n int li = S[i].size();\n int lj = S[j].size();\n int mi = min(li, lj);\n for(int k = 1; k < mi; k++){\n bool ok = true;\n for(int l = 0; l < k; l++){\n if(S[i][li - k + l] != S[j][l]){\n ok = false;\n break;\n }\n }\n if(ok) common[i][j] = k;\n }\n }\n }\n vector<vector<int>> dp(1 << n, vector<int>(n, 0));\n for(int bit = 0; bit < (1 << n); bit++){\n for(int j = 0; j < n; j++){\n if(bit >> j & 1){\n for(int k = 0; k < n; k++){\n if(j != k && (bit >> k & 1)){\n dp[bit][j] = max(dp[bit][j], dp[bit ^ (1 << j)][k] + common[j][k]);\n }\n }\n }\n }\n }\n int ans = 0;\n for(auto s: S) ans += s.size();\n int max_ = 0;\n for(int i = 0; i < n; i++) max_ = max(max_, dp[(1 << n) - 1][i]);\n cout << ans - max_ << \"\\n\";\n \n \n }\n}", "accuracy": 1, "time_ms": 210, "memory_kb": 4764, "score_of_the_acc": -0.0156, "final_rank": 8 }, { "submission_id": "aoj_1320_6000659", "code_snippet": "#define rep(i, n) for(int i=0; i<(n); ++i)\n#define rrep(i, n) for(int i=(n-1); i>=0; --i)\n#define rep2(i, s, n) for(int i=s; i<(n); ++i)\n#define ALL(v) (v).begin(), (v).end()\n#define memr(dp, val) memset(dp, val, sizeof(dp))\n#define fi first\n#define se second\ntemplate<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }\ntemplate<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }\n#include <bits/stdc++.h>\nusing namespace std;\ntemplate<typename T>\nusing min_priority_queue = priority_queue<T, vector<T>, greater<T> >;\ntypedef long long ll;\nconst int INTINF = INT_MAX >> 1;\nstatic const ll LLINF = (LLONG_MAX >> 1);\n\nint n;\nstring s[15];\nstring ss[15]; // 包含しているsを除いた入力\nint dp[1 << 15][15][15];\n\nbool a_has_b(string a, string b){\n\tif(a.size() < b.size()) return false;\n\n\trep(i, a.size()){\n\t\tif(i + b.size() - 1 >= a.size()) break;\n\t\tbool ok = true;\n\t\trep(j, b.size()){\n\t\t\tif(a[i+j] != b[j]) ok = false;\n\t\t}\n\t\tif(ok) return true;\n\t}\n\treturn false;\n}\n\nbool input(){\n\n\tcin >> n;\n\tif(!n) return false;\n\trep(i, n) cin >> s[i];\n\n\tbool inclusions[15];\n\tmemr(inclusions, 0);\n\trep(i, n){\n\t\trep2(j, i+1, n){\n\t\t\tif(a_has_b(s[i], s[j])) inclusions[j] = true; \n\t\t\tif(a_has_b(s[j], s[i])) inclusions[i] = true; \n\t\t}\n\t}\n\n\tss[0] = \"\";\n\tint cnt = 1;\n\trep(i, n){\n\t\tif(!inclusions[i]){\n\t\t\tss[cnt] = s[i];\n\t\t\tcnt++;\n\t\t}\n\t}\n\n\tn = cnt - 1;\n\n\treturn true;\n}\n\nint cost[15][15];\n\nint calc(string a, string b){\n\tint ret = 0;\n\trep(i, a.size()){\n\t\tint cnt = 0;\n\t\trep(j, b.size()){\n\t\t\tif(i + j >= a.size()) break;\n\t\t\tif(a[i+j] == b[j]) {\n\t\t\t\tcnt++;\n\t\t\t}\n\t\t\telse{\n\t\t\t\tcnt = 0;\n\t\t\t\tbreak;\n\t\t\t}\n\t\t}\n\t\tchmax(ret, cnt);\n\t}\n\treturn ret;\n}\n\nint main(){\n std::cout << std::fixed << std::setprecision(15);\n\n\twhile(input()){\n\t\trep(i, 1 << n) rep(j, 15) rep(k, 15) dp[i][j][k] = INTINF;\n\n\t\trep(i, 15) rep(j, 15) cost[i][j] = INTINF;\n\t\trep(i, n+1){\n\t\t\trep(j, n+1){\n\t\t\t\tchmin(cost[i][j], calc(ss[i], ss[j]));\n\t\t\t\tchmin(cost[j][i], calc(ss[j], ss[i]));\n\t\t\t}\n\t\t}\n\t\t\n\t\tqueue<tuple<int, int, int> > q;\n\t\tq.push({0, 0, 0});\n\t\tdp[0][0][0] = 0;\n\n\t\twhile(q.size()){\n\t\t\tint S, front, back;\n\t\t\tauto f = q.front(); q.pop();\n\t\t\ttie(S, front, back) = f;\n\t\t\trep(i, n){\n\t\t\t\tif(!(S >> i & 1)){\n\t\t\t\t\tif(dp[S | 1 << i][i+1][back] == INTINF) q.push({S | 1 << i, i + 1, back});\n\t\t\t\t\tif(dp[S | 1 << i][front][i+1] == INTINF) q.push({S | 1 << i, front, i + 1});\n\t\t\t\t\tchmin(dp[S | 1 << i][i+1][back], dp[S][front][back] + int(ss[i+1].size()) - cost[i+1][front]);\n\t\t\t\t\tchmin(dp[S | 1 << i][front][i+1], dp[S][front][back] + int(ss[i+1].size()) - cost[back][i+1]);\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\n\t\tint ans = INTINF;\n\t\trep(i, n+1) rep(j, n+1) {\n\t\t\tchmin(ans, dp[(1 << n) - 1][i][j]);\n\t\t}\n\t\tcout << ans << endl;\n\t}\n return 0;\n}", "accuracy": 1, "time_ms": 2380, "memory_kb": 22356, "score_of_the_acc": -0.5269, "final_rank": 16 }, { "submission_id": "aoj_1320_5971021", "code_snippet": "#pragma GCC optimize(\"Ofast\")\n#include <bits/stdc++.h>\n#define rep(i,n) for(int i=0;i<n;i++)\n#define cinf(n,x) for(int i=0;i<(n);i++)cin>>x[i];\n#define ft first\n#define sc second\n#define pb push_back\n#define lb lower_bound\n#define ub upper_bound\n#define all(v) (v).begin(),(v).end()\n#define LB(a,x) lb(all(a),x)-a.begin()\n#define UB(a,x) ub(all(a),x)-a.begin()\n#define mod 1000000007\n//#define mod 998244353\n#define FS fixed<<setprecision(15)\nusing namespace std;\ntypedef long long ll;\nconst double pi=3.141592653589793;\ntemplate<class T> using V=vector<T>;\nusing P=pair<int,int>;\ntypedef unsigned long long ull;\ntypedef long double ldouble;\ntemplate<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; }\ntemplate<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; }\ntemplate<class T> inline void out(T a){ cout << a << '\\n'; }\nvoid YN(bool ok){if(ok) cout << \"Yes\" << endl; else cout << \"No\" << endl;}\n//void YN(bool ok){if(ok) cout << \"YES\" << endl; else cout << \"NO\" << endl;}\n\n\nconst ll INF=1e18;\nconst int mx=200005;\n\nint dp[(1<<14)+10][20];\n\nint main(){\n //オーバーフローは大丈夫ですか??\n cin.tie(0);ios::sync_with_stdio(false);\n int cnt=0;\n while(true){\n cnt++;\n int n;\n cin>>n;\n if(n==0) break;\n V<string> s(n);\n cinf(n,s);\n \n int MX=1000000;\n V<int> lst((1<<n),-1);\n V<V<int>> cost(n,V<int>(n,MX));\n rep(i,n){\n int bit=(1<<i);\n for(int j=0;j<n;j++){\n if(j==i) continue;\n bool ok=0;\n rep(L,s[i].size()){\n for(int R=L;R<s[i].size();R++){\n if(s[i].substr(L,R-L+1)==s[j]){\n ok=1;\n }\n }\n }\n if(ok) cost[i][j]=0;\n }\n \n }\n rep(i,n){\n rep(j,n){\n if(i==j) cost[i][j]=0;\n else{\n chmin(cost[i][j],(int)s[j].size());\n rep(k,s[i].size()){\n int len=s[i].size()-k;\n if(len>s[j].size()) continue;\n if(s[i].substr(k,len)==s[j].substr(0,len)){\n chmin(cost[i][j],(int)s[j].size()-len);\n }\n }\n }\n }\n }\n rep(i,(1<<n))rep(j,n)dp[i][j]=MX;\n rep(i,n){\n dp[(1<<i)][i]=s[i].size();\n }\n rep(i,(1<<n)){\n rep(j,n){\n if(dp[i][j]==MX||!(i>>j&1)) continue;\n rep(k,n){\n if(i>>k&1) continue;\n int to=i|(1<<k);\n \n if(cost[j][k]==0) chmin(dp[to][j],dp[i][j]);\n else chmin(dp[to][k],dp[i][j]+cost[j][k]);\n }\n }\n }\n int ans=MX;\n rep(i,n) chmin(ans,dp[(1<<n)-1][i]);\n out(ans);\n }\n}", "accuracy": 1, "time_ms": 310, "memory_kb": 4792, "score_of_the_acc": -0.031, "final_rank": 13 }, { "submission_id": "aoj_1320_5962479", "code_snippet": "// #include \"atcoder/all\"\n#include <iostream> // cout, endl, cin\n#include <string> // string, to_string, stoi\n#include <vector> // vector\n#include <algorithm> // min, max, swap, sort, reverse, lower_bound, upper_bound\n#include <utility> // pair, make_pair\n#include <tuple> // tuple, make_tuple\n#include <cstdint> // int64_t, int*_t\n#include <cstdio> // printf\n#include <map> // map\n#include <queue> // queue, priority_queue\n#include <set> // set\n#include <stack> // stack\n#include <deque> // deque\n#include <unordered_map> // unordered_map\n#include <unordered_set> // unordered_set\n#include <bitset> // bitset\n#include <cctype> // isupper, islower, isdigit, toupper, tolower\n#include <iomanip> // setprecision\n#include <complex> // complex\n#include <math.h>\n#include <functional>\n#include <cassert>\nusing namespace std;\n// using namespace atcoder;\nusing ll = long long;\nusing P = pair<ll,ll>;\nconstexpr ll INF = 1e15;\nconstexpr ll LLMAX = 9223372036854775807;\nconstexpr int inf = 1e9;\n// constexpr ll mod = 1000000007;\nconstexpr ll mod = 998244353;\n// 右下左上\nconst int dx[8] = {1, 0, -1, 0,1,1,-1,-1};\nconst int dy[8] = {0, 1, 0, -1,1,-1,1,-1};\ntemplate<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }\ntemplate<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }\n#define eol \"\\n\"\n// ---------------------------------------------------------------------------\n\nbool solve(){\n int N;\n cin >> N;\n if(N == 0) return false;\n vector<string> S(N);\n for(int i=0; i<N; i++){\n cin >> S[i];\n }\n vector<int> lost(N,false);\n for(int i=0; i<N; i++){\n for(int j=0; j<N; j++){\n if(i == j) continue;\n for(int l=0; l<S[j].size(); l++){\n if(l+S[i].size() > S[j].size()) continue;\n if(S[i] == S[j].substr(l,S[i].size())){\n lost[i] = true;\n }\n }\n }\n }\n int cnt = 0;\n map<int,int> pos;\n for(int i=0; i<N; i++){\n if(lost[i]) continue;\n pos[cnt] = i;\n cnt++;\n }\n vector<vector<int>> dist(cnt,vector<int>(cnt));\n for(int i=0; i<cnt; i++){\n for(int j=0; j<cnt; j++){\n string s = S[pos[i]];\n string t = S[pos[j]];\n int mx = 0;\n for(int l=0; l<s.size(); l++){\n int len = s.size()-l;\n if(s.substr(l) == t.substr(0,len)){\n mx = len;\n break;\n }\n }\n dist[i][j] = t.size()-mx;\n }\n }\n vector<vector<int>> dp(cnt,vector<int>((1LL<<cnt),inf));\n for(int i=0; i<cnt; i++){\n dp[i][(1LL<<i)] = S[pos[i]].size();\n }\n for(int bit=0; bit<(1LL<<cnt); bit++){\n for(int i=0; i<cnt; i++){\n if(!(bit & (1LL<<i))) continue;\n for(int nxt=0; nxt<cnt; nxt++){\n if(bit & (1LL<<nxt)) continue;\n if(chmin(dp[nxt][bit+(1LL<<nxt)],dp[i][bit]+dist[i][nxt]));\n }\n }\n }\n int ans = inf;\n for(int i=0; i<cnt; i++){\n chmin(ans, dp[i][(1LL<<cnt)-1]);\n }\n // for(int i=0; i<cnt; i++){\n // for(int j=0; j<cnt ;j++){\n // cout << dist[i][j] << \" \\n\"[j+1==cnt];\n // }\n // }\n cout << ans << endl;\n return true;\n}\n\nint main(){\n ios::sync_with_stdio(false);\n cin.tie(nullptr);\n while(solve());\n return 0;\n}", "accuracy": 1, "time_ms": 160, "memory_kb": 4140, "score_of_the_acc": -0.0015, "final_rank": 1 }, { "submission_id": "aoj_1320_5950425", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\nint main() {\n while (true) {\n int n;\n cin >> n;\n if(n == 0) {\n return 0;\n }\n vector<string>S(n);\n for(int i = 0; i < n; i++) {\n cin >> S[i];\n }\n vector<string>T;\n for(int i = 0; i < n; i++) {\n bool flag = true;\n for(int j = 0; j < n; j++) {\n if(i == j || S[i].size() >= S[j].size()) {\n continue;\n }\n for(int k = 0; k < S[j].size()-S[i].size()+1; k++) {\n if(S[i] == S[j].substr(k,S[i].size())) {\n flag = false;\n }\n }\n }\n if(flag) {\n T.push_back(S[i]);\n }\n }\n vector<vector<int>>tmp(T.size(),vector<int>(T.size()));\n vector<vector<int>>dp(1 << T.size(),vector<int>(T.size(),1001001001));\n for(int i = 0; i < T.size(); i++) {\n dp[(1 << i)][i] = T[i].size();\n for(int j = 0; j < T.size(); j++) {\n for(int k = 0; k < min(T[i].size(),T[j].size()); k++) {\n if(T[i].substr(T[i].size()-k,k) == T[j].substr(0,k)) {\n tmp[i][j] = T[j].size()-k;\n }\n }\n }\n }\n for(int i = 0; i < (1 << T.size()); i++) {\n for(int j = 0; j < T.size(); j++) {\n for(int k = 0; k < T.size(); k++) {\n if(!(1 & (i >> k))) {\n dp[i|(1 << k)][k] = min(dp[i|(1 << k)][k],dp[i][j]+tmp[j][k]);\n }\n }\n }\n }\n cout << *min_element(dp[(1 << T.size())-1].begin(),dp[(1 << T.size())-1].end()) << endl;\n }\n}", "accuracy": 1, "time_ms": 180, "memory_kb": 4848, "score_of_the_acc": -0.0119, "final_rank": 6 } ]
aoj_1312_cpp
Problem H: Where's Wally Do you know the famous series of children's books named "Where's Wally"? Each of the books contains a variety of pictures of hundreds of people. Readers are challenged to find a person called Wally in the crowd. We can consider "Where's Wally" as a kind of pattern matching of two-dimensional graphical images. Wally's figure is to be looked for in the picture. It would be interesting to write a computer program to solve "Where's Wally", but this is not an easy task since Wally in the pictures may be slightly different in his appearances. We give up the idea, and make the problem much easier to solve. You are requested to solve an easier version of the graphical pattern matching problem. An image and a pattern are given. Both are rectangular matrices of bits (in fact, the pattern is always square-shaped). 0 means white, and 1 black. The problem here is to count the number of occurrences of the pattern in the image, i.e. the number of squares in the image exactly matching the pattern. Patterns appearing rotated by any multiples of 90 degrees and/or turned over forming a mirror image should also be taken into account. Input The input is a sequence of datasets each in the following format. w h p image data pattern data The first line of a dataset consists of three positive integers w , h and p . w is the width of the image and h is the height of the image. Both are counted in numbers of bits. p is the width and height of the pattern. The pattern is always square-shaped. You may assume 1 ≤ w ≤ 1000, 1 ≤ h ≤ 1000, and 1 ≤ p ≤ 100. The following h lines give the image. Each line consists of ⌈ w /6⌉ (which is equal to &⌊( w +5)/6⌋) characters, and corresponds to a horizontal line of the image. Each of these characters represents six bits on the image line, from left to right, in a variant of the BASE64 encoding format. The encoding rule is given in the following table. The most significant bit of the value in the table corresponds to the leftmost bit in the image. The last character may also represent a few bits beyond the width of the image; these bits should be ignored. character value (six bits) A-Z 0-25 a-z 26-51 0-9 52-61 + 62 / 63 The last p lines give the pattern. Each line consists of ⌈ p /6⌉ characters, and is encoded in the same way as the image. A line containing three zeros indicates the end of the input. The total size of the input does not exceed two megabytes. Output For each dataset in the input, one line containing the number of matched squares in the image should be output. An output line should not contain extra characters. Two or more matching squares may be mutually overlapping. In such a case, they are counted separately. On the other hand, a single square is never counted twice or more, even if it matches both the original pattern and its rotation, for example. Sample Input 48 3 3 gAY4I4wA gIIgIIgg w4IAYAg4 g g w 153 3 3 kkkkkkkkkkkkkkkkkkkkkkkkkg SSSSSSSSSSSSSSSSSSSSSSSSSQ JJJJJJJJJJJJJJJJJJJJJJJJJI g Q I 1 ...(truncated)
[ { "submission_id": "aoj_1312_10849705", "code_snippet": "#include <cstdio>\n#include <algorithm>\n#include <vector>\n#include <map>\n#include <set>\n#include <queue>\n#include <stack>\n#include <list>\n#include <cctype>\n#include <functional>\n#include <iterator>\n#include <cstring>\n#include <cmath>\n#include <iostream>\n#include <string>\n\nusing namespace std;\ntypedef long long ll;\ntypedef unsigned long long ull;\nint n, m, p;\nchar code[1010][1010];\nbool ma[1010][1010];\nbool pattern[1010][1010];\n\null ha[1010][1010], tmp[1010][1010];\null mh[1010][1010];\nvoid compute_hash(bool a[1010][1010], int n, int m) {\n const ull B1 = 9983;\n const ull B2 = 100000007;\n ull t1 = 1;\n for (int j = 0; j < p; j++) t1 *= B1;\n\n for (int i = 0; i < n; i++) {\n ull e = 0;\n for (int j = 0; j < p; j++) e = e * B1 + a[i][j];\n\n for (int j = 0; j + p <= m; j++) {\n tmp[i][j] = e;\n if (j + p < m) e = e * B1 - t1 * a[i][j] + a[i][j + p];\n }\n }\n\n ull t2 = 1;\n for (int i = 0; i < p; i++) t2 *= B2;\n\n for (int j = 0; j + p <= m; j++) {\n ull e = 0;\n for (int i = 0; i < p; i++) e = e * B2 + tmp[i][j];\n\n for (int i = 0; i + p <= n; i++) {\n ha[i][j] = e;\n if (i + p < n) e = e * B2 - t2 * tmp[i][j] + tmp[i + p][j];\n }\n }\n \n}\n\nint getbit(char ch) {\n if (ch >= 'A' && ch <= 'Z') return 0 + ch - 'A';\n if (ch >= 'a' && ch <= 'z') return 26 + ch - 'a';\n if (ch >= '0' && ch <= '9') return 52 + ch - '0';\n if (ch == '+') return 62;\n return 63;\n}\n\nvoid decode(bool mat[1010][1010], int h, int w) {\n for (int i = 0; i < h; i++) {\n for (int j = 0; j < (w + 5) / 6; j++) {\n int bit = getbit(code[i][j]);\n for (int k = 0; k < 6; k++) {\n mat[i][j*6+k] = ((bit >> (5 - k)) & 1) == 1;\n }\n }\n }\n}\n\nvoid init() {\n for (int i = 0; i < n; i++) scanf(\"%s\", code[i]);\n // printf(\"text:\\n\");\n // for (int i = 0; i < n; i++) puts(code[i]);\n // printf(\"\\n\");\n decode(ma, n, m);\n // for (int i = 0; i < n; i++) {\n // for (int j = 0; j < m; j++) {\n // printf(\"%d\", ma[i][j]);\n // }\n // printf(\"\\n\");\n // }\n // printf(\"\\n\");\n for (int i = 0; i < p; i++) scanf(\"%s\", code[i]);\n // printf(\"pattern:\\n\");\n // for (int i = 0; i < p; i++) puts(code[i]);\n // printf(\"\\n\");\n \n decode(pattern, p, p);\n // for (int i = 0; i < p; i++) {\n // for (int j = 0; j < p; j++) {\n // printf(\"%d\", pattern[i][j]);\n // }\n // printf(\"\\n\");\n // }\n // printf(\"\\n\");\n}\n\nvoid showp() {\n for (int i = 0; i < p; i++) {\n for (int j = 0; j < p; j++) {\n printf(\"%d\", pattern[i][j]);\n }\n printf(\"\\n\");\n }\n}\n\nbool tm[1010][1010];\nvoid rotate() {\n for (int i = 0; i < p; i++) {\n for (int j = 0; j < p; j++) {\n tm[i][j] = pattern[j][p-1-i];\n }\n }\n for (int i = 0; i < p; i++) for (int j = 0; j < p; j++)\n pattern[i][j] = tm[i][j];\n}\n\nvoid mirror() {\n for (int i = 0; i < p; i++) {\n for (int j = 0; j < p; j++) {\n tm[i][j] = pattern[i][p-1-j];\n }\n }\n for (int i = 0; i < p; i++) for (int j = 0; j < p; j++)\n pattern[i][j] = tm[i][j];\n}\n\n\nset<ull> seen;\n\nvoid solve() {\n seen.clear();\n int ans = 0;\n compute_hash(pattern, p, p);\n ull hp = ha[0][0];\n seen.insert(hp);\n compute_hash(ma, n, m);\n// printf(\"hp:%llu ha[0][0]:%llu\\n\", hp, ha[0][0]);\n for (int i = 0; i + p <= n; i++) {\n for (int j = 0; j + p<= m; j++) {\n if (ha[i][j] == hp) ans++;\n mh[i][j] = ha[i][j];\n }\n }\n// printf(\"ans = %d\\n\", ans);\n for (int k = 0; k < 7; k++) {\n if (k == 3) mirror();\n else rotate();\n compute_hash(pattern, p, p);\n hp = ha[0][0];\n// printf(\"hash: %llu\\n\", hp);\n if (seen.find(hp) != seen.end()) continue;\n// showp();\n\n seen.insert(hp);\n for (int i = 0; i + p <= n; i++) {\n for (int j = 0; j + p <= m; j++) {\n if (mh[i][j] == hp) ans++;\n }\n }\n }\n printf(\"%d\\n\", ans);\n}\n\nint main() {\n for (;;) {\n scanf(\"%d%d%d\", &m, &n, &p);\n if (n == 0 && m == 0 && p == 0) break;\n init();\n solve();\n }\n return 0;\n}", "accuracy": 1, "time_ms": 70, "memory_kb": 30436, "score_of_the_acc": -1, "final_rank": 11 }, { "submission_id": "aoj_1312_9733154", "code_snippet": "#include <bits/stdc++.h>\n\nusing namespace std;\n\n#define rep(i, a, b) for (int i = (int)(a); i < (int)(b); i++)\n#define rrep(i, a, b) for (int i = (int)(b)-1; i >= (int)(a); i--)\n#define ALL(v) (v).begin(), (v).end()\n#define UNIQUE(v) sort(ALL(v)), (v).erase(unique(ALL(v)), (v).end())\n#define SZ(v) (int)v.size()\n#define MIN(v) *min_element(ALL(v))\n#define MAX(v) *max_element(ALL(v))\n#define LB(v, x) int(lower_bound(ALL(v), (x)) - (v).begin())\n#define UB(v, x) int(upper_bound(ALL(v), (x)) - (v).begin())\n\nusing uint = unsigned int;\nusing ll = long long int;\nusing ull = unsigned long long;\nusing i128 = __int128_t;\nusing u128 = __uint128_t;\nconst int inf = 0x3fffffff;\nconst ll INF = 0x1fffffffffffffff;\n\ntemplate <typename T> inline bool chmax(T &a, T b) {\n if (a < b) {\n a = b;\n return 1;\n }\n return 0;\n}\ntemplate <typename T> inline bool chmin(T &a, T b) {\n if (a > b) {\n a = b;\n return 1;\n }\n return 0;\n}\ntemplate <typename T, typename U> T ceil(T x, U y) {\n assert(y != 0);\n if (y < 0)\n x = -x, y = -y;\n return (x > 0 ? (x + y - 1) / y : x / y);\n}\ntemplate <typename T, typename U> T floor(T x, U y) {\n assert(y != 0);\n if (y < 0)\n x = -x, y = -y;\n return (x > 0 ? x / y : (x - y + 1) / y);\n}\ntemplate <typename T> int popcnt(T x) {\n return __builtin_popcountll(x);\n}\ntemplate <typename T> int topbit(T x) {\n return (x == 0 ? -1 : 63 - __builtin_clzll(x));\n}\ntemplate <typename T> int lowbit(T x) {\n return (x == 0 ? -1 : __builtin_ctzll(x));\n}\n\ntemplate <class T, class U>\nostream &operator<<(ostream &os, const pair<T, U> &p) {\n os << \"P(\" << p.first << \", \" << p.second << \")\";\n return os;\n}\ntemplate <typename T> ostream &operator<<(ostream &os, const vector<T> &vec) {\n os << \"{\";\n for (int i = 0; i < vec.size(); i++) {\n os << vec[i] << (i + 1 == vec.size() ? \"\" : \", \");\n }\n os << \"}\";\n return os;\n}\ntemplate <typename T, typename U>\nostream &operator<<(ostream &os, const map<T, U> &map_var) {\n os << \"{\";\n for (auto itr = map_var.begin(); itr != map_var.end(); itr++) {\n os << \"(\" << itr->first << \", \" << itr->second << \")\";\n itr++;\n if (itr != map_var.end())\n os << \", \";\n itr--;\n }\n os << \"}\";\n return os;\n}\ntemplate <typename T> ostream &operator<<(ostream &os, const set<T> &set_var) {\n os << \"{\";\n for (auto itr = set_var.begin(); itr != set_var.end(); itr++) {\n os << *itr;\n ++itr;\n if (itr != set_var.end())\n os << \", \";\n itr--;\n }\n os << \"}\";\n return os;\n}\n#ifdef LOCAL\n#define show(...) _show(0, #__VA_ARGS__, __VA_ARGS__)\n#else\n#define show(...) true\n#endif\ntemplate <typename T> void _show(int i, T name) {\n cerr << '\\n';\n}\ntemplate <typename T1, typename T2, typename... T3>\nvoid _show(int i, const T1 &a, const T2 &b, const T3 &...c) {\n for (; a[i] != ',' && a[i] != '\\0'; i++)\n cerr << a[i];\n cerr << \":\" << b << \" \";\n _show(i + 1, a, c...);\n}\n\n/**\n * @brief template\n */\n\nnamespace Random {\nmt19937_64 randgen(chrono::steady_clock::now().time_since_epoch().count());\nusing u64 = unsigned long long;\nu64 get() {\n return randgen();\n}\ntemplate <typename T> T get(T L) { // [0,L]\n\n return get() % (L + 1);\n}\ntemplate <typename T> T get(T L, T R) { // [L,R]\n\n return get(R - L) + L;\n}\n}; // namespace Random\n\n/**\n * @brief Random\n */\n\null MOD = 0x1fffffffffffffff;\n\ninline ull mul(ull a, ull b) {\n __uint128_t ans = __uint128_t(a) * b;\n ans = (ans >> 61) + (ans & MOD);\n if (ans >= MOD)\n ans -= MOD;\n return ans;\n}\n\nstatic inline ull genbase() {\n return Random::get(ull(0x1fffffffffffffff));\n}\n\nvector<vector<int>> rot90(vector<vector<int>> A) {\n int N = A.size();\n vector<vector<int>> Ret(N,vector<int>(N));\n rep(i,0,N) {\n rep(j,0,N) {\n Ret[N-1-j][i] = A[i][j];\n }\n }\n return Ret;\n}\n\nvector<vector<int>> Millor(vector<vector<int>> A) {\n int N = A.size();\n vector<vector<int>> Ret(N,vector<int>(N));\n rep(i,0,N) {\n rep(j,0,N) {\n Ret[i][N-1-j] = A[i][j];\n }\n }\n return Ret;\n}\n\n\n\nvoid Solve(int N, int M, int K) {\n int mm = (M+5)/6;\n vector<vector<int>> A(N,vector<int>(mm*6));\n rep(i,0,N) {\n rep(j,0,mm) {\n char C;\n cin >> C;\n int X;\n if ('A' <= C && C <= 'Z') X = C - 'A';\n if ('a' <= C && C <= 'z') X = C - 'a' + 26;\n if (isdigit(C)) X = C - '0' + 52;\n if (C == '+') X = 62;\n if (C == '/') X = 63;\n rep(k,0,6) {\n if (X & (1<<k)) A[i][j*6+5-k] = 1;\n else A[i][j*6+5-k] = 0;\n }\n }\n }\n rep(i,0,N) A[i].resize(M);\n int kk = (K+5)/6;\n vector<vector<int>> B(K,vector<int>(kk*6));\n rep(i,0,K) {\n rep(j,0,kk) {\n char C;\n cin >> C;\n int X;\n if ('A' <= C && C <= 'Z') X = C - 'A';\n if ('a' <= C && C <= 'z') X = C - 'a' + 26;\n if (isdigit(C)) X = C - '0' + 52;\n if (C == '+') X = 62;\n if (C == '/') X = 63;\n rep(k,0,6) {\n if (X & (1<<k)) B[i][j*6+5-k] = 1;\n else B[i][j*6+5-k] = 0;\n }\n }\n }\n rep(i,0,K) B[i].resize(K);\n const ull base[2] = {genbase(), genbase()};\n int L = max(N,max(M,K));\n vector<vector<ull>> power(2,vector<ull>(L+1,0));\n power[0][0] = power[1][0] = 1;\n rep(i,0,2) rep(j,0,L) power[i][j+1] = mul(power[i][j],base[i]);\n vector<vector<ull>> hash(N+1,vector<ull>(M+1,0));\n rep(i,0,N) {\n rep(j,0,M) {\n hash[i+1][j+1] += A[i][j];\n hash[i+1][j+1] += mul(hash[i][j+1], base[0]);\n hash[i+1][j+1] += mul(hash[i+1][j], base[1]);\n hash[i+1][j+1] += MOD - mul(mul(hash[i][j],base[0]),base[1]);\n while(hash[i+1][j+1] >= MOD) hash[i+1][j+1] -= MOD;\n }\n }\n auto get = [&](int l1, int r1, int l2, int r2) -> ull {\n ull Ret = hash[r1][r2];\n Ret += MOD - mul(hash[l1][r2], power[0][r1-l1]);\n Ret += MOD - mul(hash[r1][l2], power[1][r2-l2]);\n Ret += mul(mul(hash[l1][l2],power[0][r1-l1]),power[1][r2-l2]);\n while(Ret >= MOD) Ret -= MOD;\n return Ret;\n };\n map<ull,bool> mp;\n rep(i,0,4) {\n ull X = 0;\n rep(j,0,K) {\n rep(k,0,K) {\n X += mul(mul(B[j][k],power[0][K-1-j]),power[1][K-1-k]);\n while (X >= MOD) X -= MOD;\n }\n }\n mp[X] = true;\n B = rot90(B);\n }\n B = Millor(B);\n rep(i,0,4) {\n ull X = 0;\n rep(j,0,K) {\n rep(k,0,K) {\n X += mul(mul(B[j][k],power[0][K-1-j]),power[1][K-1-k]);\n while (X >= MOD) X -= MOD;\n }\n }\n mp[X] = true;\n B = rot90(B);\n }\n int ANS = 0;\n rep(i,0,N) {\n if (i+K > N) break;\n rep(j,0,M) {\n if (j+K > M) break;\n if (mp.count(get(i,i+K,j,j+K))) ANS++;\n }\n }\n cout << ANS << endl;\n}\n\nint main() {\nwhile(1) {\n int N, M, K;\n cin >> M >> N >> K;\n if (N == 0) return 0;\n Solve(N,M,K);\n}\n}", "accuracy": 1, "time_ms": 310, "memory_kb": 15064, "score_of_the_acc": -0.4473, "final_rank": 10 }, { "submission_id": "aoj_1312_8520219", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\n#define rep(i, n) for (int i = 0; i < (n); i++)\n#define per(i, n) for (int i = (n)-1; i >= 0; i--)\n#define rep2(i, l, r) for (int i = (l); i < (r); i++)\n#define per2(i, l, r) for (int i = (r)-1; i >= (l); i--)\n#define each(e, x) for (auto &e : x)\n#define sz(x) (int)x.size()\n#define all(x) begin(x), end(x)\n#define rall(x) rbegin(x), rend(x)\n#define eb emplace_back\n#define MM << ' ' <<\n#define TT template <typename T>\nusing ll = long long;\nusing pii = pair<int, int>;\nusing pll = pair<ll, ll>;\n\nTT bool chmin(T &x, T y) { return x > y ? (x = y, true) : false; }\nTT bool chmax(T &x, T y) { return x < y ? (x = y, true) : false; }\n\nTT using minheap = priority_queue<T, vector<T>, greater<T>>;\nTT using maxheap = priority_queue<T>;\n\nTT void print(vector<T> v, T x = 0) {\n int n = sz(v);\n rep(i, n) cout << v[i] + x << (i == n - 1 ? '\\n' : ' ');\n if (n == 0) cout << '\\n';\n}\n\nTT void printn(vector<T> v, T x = 0) {\n int n = sz(v);\n rep(i, n) cout << v[i] + x << '\\n';\n}\n\nTT void rearrange(vector<T> &v) {\n sort(all(v));\n v.erase(unique(all(v)), end(v));\n}\n\nTT int lb(vector<T> &v, T x) { return lower_bound(all(v), x) - begin(v); }\nTT int ub(vector<T> &v, T x) { return upper_bound(all(v), x) - begin(v); }\n\nconst int inf = (1 << 30) - 1;\nconst ll INF = (1LL << 60) - 1;\n\nstruct io_setup {\n io_setup() {\n ios_base::sync_with_stdio(false);\n cin.tie(NULL);\n cout << fixed << setprecision(15);\n }\n} io_setup;\n\nusing ull = unsigned long long;\nconst ull mod = (1LL << 61) - 1;\n\nnamespace Hash {\null getmod(ull x) {\n ull ret = (x >> 61) + (x & mod);\n if (ret >= mod) ret -= mod;\n return ret;\n}\n\null mul(ull x, ull y) {\n x = getmod(x), y = getmod(y);\n ull x1 = x >> 31;\n ull x2 = x & ((1LL << 31) - 1);\n ull y1 = y >> 31;\n ull y2 = y & ((1LL << 31) - 1);\n ull z = x1 * y2 + x2 * y1;\n ull z1 = z >> 30;\n ull z2 = z & ((1LL << 30) - 1);\n return getmod(x1 * y1 * 2 + x2 * y2 + z1 + (z2 << 31));\n}\n} // namespace Hash\n\n// 0 が含まれないように\ntemplate <typename T>\nstruct RollingHash {\n const int n;\n const ull b; // 基数\n vector<ull> h, p;\n\n // b = 887'446'835'055'281'585\n RollingHash(const T &s, ull b = 887446835055281585) : n(sz(s)), b(b) {\n h.assign(n + 1, 0), p.assign(n + 1, 1);\n rep(i, n) {\n p[i + 1] = Hash::mul(p[i], b);\n h[i + 1] = Hash::mul(h[i], b) + s[i];\n if (h[i + 1] >= mod) h[i + 1] -= mod;\n }\n }\n\n // 文字列の [l,r) の部分のハッシュ値\n ull get_hash(int l, int r) const {\n ull ret = h[r] + mod - Hash::mul(h[l], p[r - l]);\n return ret - (ret >= mod ? mod : 0);\n }\n};\n\nint main() {\n while (1) {\n int w, h, p;\n cin >> w >> h >> p;\n if (!w)\n break;\n vector<string> img(h), pat(p);\n rep(i, h) cin >> img[i];\n rep(i, p) cin >> pat[i];\n vector<vector<int>> vs(h), vt(p);\n auto val = [](char c) {\n if (isupper(c))\n return c - 'A';\n else if (islower(c))\n return c - 'a' + 26;\n else if (isdigit(c))\n return c - '0' + 52;\n else\n return (c == '+' ? 62 : 63);\n };\n rep(i, h) rep(j, sz(img[i])) vs[i].eb(val(img[i][j]));\n rep(i, p) rep(j, sz(pat[i])) vt[i].eb(val(pat[i][j]));\n vector<vector<int>> s(h, vector<int>(w)), t(p, vector<int>(p));\n rep(i, h) rep(j, w) s[i][j] = vs[i][j / 6] >> (5 - j % 6) & 1;\n rep(i, p) rep(j, p) t[i][j] = vt[i][j / 6] >> (5 - j % 6) & 1;\n vector<vector<ull>> hs(w, vector<ull>(h));\n rep(i, h) {\n RollingHash rh(s[i]);\n rep(j, w - p + 1) hs[j][i] = rh.get_hash(j, j + p);\n }\n vector<vector<int>> f(h, vector<int>(w, 0));\n auto rotate = [](vector<vector<int>> t) {\n int n = sz(t);\n vector<vector<int>> ret(n, vector<int>(n));\n rep(i, n) rep(j, n) ret[j][n - 1 - i] = t[i][j];\n return ret;\n };\n auto rev = [](vector<vector<int>> t) {\n int n = sz(t);\n vector<vector<int>> ret(n, vector<int>(n));\n rep(i, n) rep(j, n) ret[i][n - 1 - j] = t[i][j];\n return ret;\n };\n rep(side, 2) {\n rep(rot, 4) {\n vector<int> ct(p * p);\n rep(i, p) rep(j, p) ct[i * p + j] = t[i][j];\n RollingHash rht(ct);\n ull tar = rht.get_hash(0, p * p);\n ull b = 887446835055281585, base = 1;\n rep(t, p) base = Hash::mul(base, b);\n rep(j, w - p + 1) {\n RollingHash rhs(hs[j], base);\n rep(i, h - p + 1) f[i][j] |= rhs.get_hash(i, i + p) == tar;\n }\n t = rotate(t);\n }\n t = rev(t);\n }\n int ans = 0;\n rep(i, h) rep(j, w) ans += f[i][j];\n cout << ans << '\\n';\n }\n}", "accuracy": 1, "time_ms": 940, "memory_kb": 20788, "score_of_the_acc": -1.4799, "final_rank": 12 }, { "submission_id": "aoj_1312_8215714", "code_snippet": "#include <bits/stdc++.h>\n#define N 3\nusing namespace std;\ntypedef unsigned long long ull;\nbool im[1010][1010], pa[110][110];\nint w, h, p, ans;\nvector<ull> used;\nvoid ppp() {\n ull a = 0;\n ull roll[1001][1001] = {};\n for (int i = 0; i < p; i++)\n for (int j = 0; j < p; j++)\n a = a * N + pa[i][j];\n for (int i = 0; i < used.size(); i++)\n if (used[i] == a)\n return;\n used.push_back(a);\n for (int j = 0; j < h; j++) {\n ull b = 0, g = 1;\n for (int i = 0; i < p; i++)\n b = b * N + im[j][i], g *= N;\n roll[j][p - 1] = b;\n for (int i = p; i < w; i++) {\n b = b * N + im[j][i] - im[j][i - p] * g;\n roll[j][i] = b;\n }\n }\n for (int j = p - 1; j < w; j++) {\n ull b = 0, g = 1, n = 1;\n for (int i = 0; i < p; i++)\n n *= N;\n for (int i = 0; i < p; i++)\n b = b * n + roll[i][j], g *= n;\n if (b == a)\n ans++;\n for (int i = p; i < h; i++) {\n b = b * n + roll[i][j] - roll[i - p][j] * g;\n if (b == a)\n ans++;\n }\n }\n}\nvoid ch() {\n bool npa[110][110];\n for (int i = 0; i < p; i++)\n for (int j = 0; j < p; j++)\n npa[i][j] = pa[j][p - i - 1];\n for (int i = 0; i < p; i++)\n for (int j = 0; j < p; j++)\n pa[i][j] = npa[i][j];\n}\nvoid ch1() {\n for (int i = 0; i < p; i++)\n for (int j = 0; j < p / 2; j++)\n swap(pa[i][j], pa[i][p - j - 1]);\n}\nint main() {\n int D[256];\n for (int i = 'A'; i <= 'Z'; i++)\n D[i] = i - 'A';\n for (int i = 'a'; i <= 'z'; i++)\n D[i] = i - 'a' + 26;\n for (int i = '0'; i <= '9'; i++)\n D[i] = i - '0' + 52;\n D['+'] = 62, D['/'] = 63;\n while (cin >> w >> h >> p, w) {\n string s;\n used.clear();\n for (int i = 0; i < h; i++) {\n cin >> s;\n for (int j = 0; j < s.size(); j++) {\n int t = D[s[j]], a = 6;\n while (a--)\n im[i][j * 6 + a] = t % 2, t /= 2;\n }\n }\n for (int i = 0; i < p; i++) {\n cin >> s;\n for (int j = 0; j < s.size(); j++) {\n int t = D[s[j]], a = 6;\n while (a--)\n pa[i][j * 6 + a] = t % 2, t /= 2;\n }\n }\n ans = 0;\n for (int i = 0; i < 4; i++) {\n ppp();\n ch1();\n ppp();\n ch1();\n ch();\n }\n cout << ans << endl;\n }\n return 0;\n}", "accuracy": 1, "time_ms": 260, "memory_kb": 11960, "score_of_the_acc": -0.2225, "final_rank": 7 }, { "submission_id": "aoj_1312_8215713", "code_snippet": "#include <bits/stdc++.h>\n#define N 3\nusing namespace std;\ntypedef unsigned long long ull;\nbool im[1010][1010], pa[110][110];\nint w, h, p, ans;\nvector<ull> used;\nvoid ppp() {\n ull a = 0;\n ull roll[1001][1001] = {};\n for (int i = 0; i < p; i++)\n for (int j = 0; j < p; j++)\n a = a * N + pa[i][j];\n for (int i = 0; i < used.size(); i++)\n if (used[i] == a)\n return;\n used.push_back(a);\n for (int j = 0; j < h; j++) {\n ull b = 0, g = 1;\n for (int i = 0; i < p; i++)\n b = b * N + im[j][i], g *= N;\n roll[j][p - 1] = b;\n for (int i = p; i < w; i++) {\n b = b * N + im[j][i] - im[j][i - p] * g;\n roll[j][i] = b;\n }\n }\n for (int j = p - 1; j < w; j++) {\n ull b = 0, g = 1, n = 1;\n for (int i = 0; i < p; i++)\n n *= N;\n for (int i = 0; i < p; i++)\n b = b * n + roll[i][j], g *= n;\n if (b == a)\n ans++;\n for (int i = p; i < h; i++) {\n b = b * n + roll[i][j] - roll[i - p][j] * g;\n if (b == a)\n ans++;\n }\n }\n}\nvoid ch() {\n bool npa[110][110];\n for (int i = 0; i < p; i++)\n for (int j = 0; j < p; j++)\n npa[i][j] = pa[j][p - i - 1];\n for (int i = 0; i < p; i++)\n for (int j = 0; j < p; j++)\n pa[i][j] = npa[i][j];\n}\nvoid ch1() {\n for (int i = 0; i < p; i++)\n for (int j = 0; j < p / 2; j++)\n swap(pa[i][j], pa[i][p - j - 1]);\n}\nint main() {\n int D[256];\n for (int i = 'A'; i <= 'Z'; i++)\n D[i] = i - 'A';\n for (int i = 'a'; i <= 'z'; i++)\n D[i] = i - 'a' + 26;\n for (int i = '0'; i <= '9'; i++)\n D[i] = i - '0' + 52;\n D['+'] = 62, D['/'] = 63;\n while (cin >> w >> h >> p, w) {\n string s;\n used.clear();\n for (int i = 0; i < h; i++) {\n cin >> s;\n for (int j = 0; j < s.size(); j++) {\n int t = D[s[j]];\n int a = 6;\n while (a--)\n im[i][j * 6 + a] = t % 2, t /= 2;\n }\n }\n for (int i = 0; i < p; i++) {\n cin >> s;\n for (int j = 0; j < s.size(); j++) {\n int t = D[s[j]];\n int a = 6;\n while (a--)\n pa[i][j * 6 + a] = t % 2, t /= 2;\n }\n }\n ans = 0;\n for (int i = 0; i < 4; i++) {\n ppp();\n ch1();\n ppp();\n ch1();\n ch();\n }\n cout << ans << endl;\n }\n return 0;\n}", "accuracy": 1, "time_ms": 250, "memory_kb": 11972, "score_of_the_acc": -0.2116, "final_rank": 6 }, { "submission_id": "aoj_1312_8116063", "code_snippet": "#include <bits/stdc++.h>\n#define N 3\nusing namespace std;\n\ntypedef unsigned long long ull;\n\nbool im[1010][1010], pa[110][110];\nint w, h, p, ans;\nvector<ull> used;\n\nvoid ppp() {\n ull a = 0;\n ull roll[1001][1001] = {};\n for (int i = 0; i < p; i++) {\n for (int j = 0; j < p; j++) {\n a = a * N + pa[i][j];\n }\n }\n for (int i = 0; i < used.size(); i++) {\n if (used[i] == a) {\n return;\n }\n }\n used.push_back(a);\n for (int j = 0; j < h; j++) {\n ull b = 0, g = 1;\n for (int i = 0; i < p; i++) {\n b = b * N + im[j][i];\n g *= N;\n }\n roll[j][p - 1] = b;\n for (int i = p; i < w; i++) {\n b = b * N + im[j][i] - im[j][i - p] * g;\n roll[j][i] = b;\n }\n }\n for (int j = p - 1; j < w; j++) {\n ull b = 0, g = 1, n = 1;\n for (int i = 0; i < p; i++) {\n n *= N;\n }\n for (int i = 0; i < p; i++) {\n b = b * n + roll[i][j];\n g *= n;\n }\n if (b == a) {\n ans++;\n }\n for (int i = p; i < h; i++) {\n b = b * n + roll[i][j] - roll[i - p][j] * g;\n if (b == a) {\n ans++;\n }\n }\n }\n}\n\nvoid ch() {\n bool npa[110][110];\n for (int i = 0; i < p; i++) {\n for (int j = 0; j < p; j++) {\n npa[i][j] = pa[j][p - i - 1];\n }\n }\n for (int i = 0; i < p; i++) {\n for (int j = 0; j < p; j++) {\n pa[i][j] = npa[i][j];\n }\n }\n}\n\nvoid ch1() {\n for (int i = 0; i < p; i++) {\n for (int j = 0; j < p / 2; j++) {\n swap(pa[i][j], pa[i][p - j - 1]);\n }\n }\n}\n\nint main() {\n int D[256];\n for (int i = 'A'; i <= 'Z'; i++) {\n D[i] = i - 'A';\n }\n for (int i = 'a'; i <= 'z'; i++) {\n D[i] = i - 'a' + 26;\n }\n for (int i = '0'; i <= '9'; i++) {\n D[i] = i - '0' + 52;\n }\n D['+'] = 62, D['/'] = 63;\n while (cin >> w >> h >> p, w) {\n string s;\n used.clear();\n for (int i = 0; i < h; i++) {\n cin >> s;\n for (int j = 0; j < s.size(); j++) {\n int t = D[s[j]];\n int a = 6;\n while (a--) {\n im[i][j * 6 + a] = t % 2;\n t /= 2;\n }\n }\n }\n for (int i = 0; i < p; i++) {\n cin >> s;\n for (int j = 0; j < s.size(); j++) {\n int t = D[s[j]];\n int a = 6;\n while (a--) {\n pa[i][j * 6 + a] = t % 2;\n t /= 2;\n }\n }\n }\n ans = 0;\n for (int i = 0; i < 4; i++) {\n ppp();\n ch1();\n ppp();\n ch1();\n ch();\n }\n cout << ans << endl;\n }\n return 0;\n}", "accuracy": 1, "time_ms": 280, "memory_kb": 11884, "score_of_the_acc": -0.2414, "final_rank": 9 }, { "submission_id": "aoj_1312_8113057", "code_snippet": "#include <bits/stdc++.h>\n#define N 3\nusing namespace std;\ntypedef long long ull;\nbool im[1010][1010], pa[110][110];\nint w, h, p, ans;\nvector<ull> used;\nvoid ppp() {\n ull a = 0;\n ull roll[1001][1001] = {};\n for (int i = 0; i < p; i++)\n for (int j = 0; j < p; j++)\n a = a * N + pa[i][j];\n for (int i = 0; i < used.size(); i++)\n if (used[i] == a)\n return;\n used.push_back(a);\n for (int j = 0; j < h; j++) {\n ull b = 0, g = 1;\n for (int i = 0; i < p; i++)\n b = b * N + im[j][i], g *= N;\n roll[j][p - 1] = b;\n for (int i = p; i < w; i++) {\n b = b * N + im[j][i] - im[j][i - p] * g;\n roll[j][i] = b;\n }\n }\n for (int j = p - 1; j < w; j++) {\n ull b = 0, g = 1, n = 1;\n for (int i = 0; i < p; i++)\n n *= N;\n for (int i = 0; i < p; i++)\n b = b * n + roll[i][j], g *= n;\n if (b == a)\n ans++;\n for (int i = p; i < h; i++) {\n b = b * n + roll[i][j] - roll[i - p][j] * g;\n if (b == a)\n ans++;\n }\n }\n}\nvoid ch() {\n bool npa[110][110];\n for (int i = 0; i < p; i++)\n for (int j = 0; j < p; j++)\n npa[i][j] = pa[j][p - i - 1];\n for (int i = 0; i < p; i++)\n for (int j = 0; j < p; j++)\n pa[i][j] = npa[i][j];\n}\nvoid ch1() {\n for (int i = 0; i < p; i++)\n for (int j = 0; j < p / 2; j++)\n swap(pa[i][j], pa[i][p - j - 1]);\n}\nint main() {\n int D[256];\n for (int i = 'A'; i <= 'Z'; i++)\n D[i] = i - 'A';\n for (int i = 'a'; i <= 'z'; i++)\n D[i] = i - 'a' + 26;\n for (int i = '0'; i <= '9'; i++)\n D[i] = i - '0' + 52;\n D['+'] = 62, D['/'] = 63;\n while (cin >> w >> h >> p, w) {\n string s;\n used.clear();\n for (int i = 0; i < h; i++) {\n cin >> s;\n for (int j = 0; j < s.size(); j++) {\n int t = D[s[j]];\n int a = 6;\n while (a--)\n im[i][j * 6 + a] = t % 2, t /= 2;\n }\n }\n for (int i = 0; i < p; i++) {\n cin >> s;\n for (int j = 0; j < s.size(); j++) {\n int t = D[s[j]];\n int a = 6;\n while (a--)\n pa[i][j * 6 + a] = t % 2, t /= 2;\n }\n }\n ans = 0;\n for (int i = 0; i < 4; i++) {\n ppp();\n ch1();\n ppp();\n ch1();\n ch();\n }\n cout << ans << endl;\n }\n return 0;\n}", "accuracy": 1, "time_ms": 260, "memory_kb": 12008, "score_of_the_acc": -0.2251, "final_rank": 8 }, { "submission_id": "aoj_1312_8051625", "code_snippet": "#include <bits/stdc++.h>\n#define N 3\nusing namespace std;\ntypedef unsigned long long ull;\nbool im[1010][1010], pa[110][110];\nint w, h, p, ans;\nunordered_set<ull> used;\nvoid ppp() {\n ull a = 0;\n ull roll[1001][1001] = {};\n for (int i = 0; i < p; i++)\n for (int j = 0; j < p; j++)\n a = a * N + pa[i][j];\n if (used.find(a) != used.end()) return;\n used.insert(a);\n for (int j = 0; j < h; j++) {\n ull b = 0, g = 1;\n for (int i = 0; i < p; i++)\n b = b * N + im[j][i], g *= N;\n roll[j][p - 1] = b;\n for (int i = p; i < w; i++) {\n b = b * N + im[j][i] - im[j][i - p] * g;\n roll[j][i] = b;\n }\n }\n for (int j = p - 1; j < w; j++) {\n ull b = 0, g = 1, n = 1;\n for (int i = 0; i < p; i++)\n n *= N;\n for (int i = 0; i < p; i++)\n b = b * n + roll[i][j], g *= n;\n if (b == a)\n ans++;\n for (int i = p; i < h; i++) {\n b = b * n + roll[i][j] - roll[i - p][j] * g;\n if (b == a)\n ans++;\n }\n }\n}\nvoid ch() {\n bool npa[110][110];\n for (int i = 0; i < p; i++)\n for (int j = 0; j < p; j++)\n npa[i][j] = pa[j][p - i - 1];\n for (int i = 0; i < p; i++)\n for (int j = 0; j < p; j++)\n pa[i][j] = npa[i][j];\n}\nvoid ch1() {\n for (int i = 0; i < p; i++)\n for (int j = 0; j < p / 2; j++)\n swap(pa[i][j], pa[i][p - j - 1]);\n}\nint main() {\n int D[256];\n for (int i = 'A'; i <= 'Z'; i++)\n D[i] = i - 'A';\n for (int i = 'a'; i <= 'z'; i++)\n D[i] = i - 'a' + 26;\n for (int i = '0'; i <= '9'; i++)\n D[i] = i - '0' + 52;\n D['+'] = 62, D['/'] = 63;\n while (cin >> w >> h >> p, w) {\n string s;\n used.clear();\n for (int i = 0; i < h; i++) {\n cin >> s;\n for (int j = 0; j < s.size(); j++) {\n int t = D[s[j]];\n int a = 6;\n while (a--)\n im[i][j * 6 + a] = t % 2, t /= 2;\n }\n }\n for (int i = 0; i < p; i++) {\n cin >> s;\n for (int j = 0; j < s.size(); j++) {\n int t = D[s[j]];\n int a = 6;\n while (a--)\n pa[i][j * 6 + a] = t % 2, t /= 2;\n }\n }\n ans = 0;\n for (int i = 0; i < 4; i++) {\n ppp();\n ch1();\n ppp();\n ch1();\n ch();\n }\n cout << ans << endl;\n }\n return 0;\n}", "accuracy": 1, "time_ms": 180, "memory_kb": 12152, "score_of_the_acc": -0.1409, "final_rank": 4 }, { "submission_id": "aoj_1312_8051622", "code_snippet": "#include <bits/stdc++.h>\n#define N 3\nusing namespace std;\ntypedef unsigned long long ull;\nbool im[1010][1010], pa[110][110];\nint w, h, p, ans;\nunordered_set<ull> used;\nvoid ppp() {\n ull a = 0;\n ull roll[1001][1001] = {};\n for (int i = 0; i < p; i++)\n for (int j = 0; j < p; j++)\n a = a * N + pa[i][j];\n if (used.count(a))\n return;\n used.insert(a);\n for (int j = 0; j < h; j++) {\n ull b = 0, g = 1;\n for (int i = 0; i < p; i++)\n b = b * N + im[j][i], g *= N;\n roll[j][p - 1] = b;\n for (int i = p; i < w; i++) {\n b = b * N + im[j][i] - im[j][i - p] * g;\n roll[j][i] = b;\n }\n }\n for (int j = p - 1; j < w; j++) {\n ull b = 0, g = 1, n = 1;\n for (int i = 0; i < p; i++)\n n *= N;\n for (int i = 0; i < p; i++)\n b = b * n + roll[i][j], g *= n;\n if (b == a)\n ans++;\n for (int i = p; i < h; i++) {\n b = b * n + roll[i][j] - roll[i - p][j] * g;\n if (b == a)\n ans++;\n }\n }\n}\nvoid ch() {\n bool npa[110][110];\n for (int i = 0; i < p; i++)\n for (int j = 0; j < p; j++)\n npa[i][j] = pa[j][p - i - 1];\n for (int i = 0; i < p; i++)\n for (int j = 0; j < p; j++)\n pa[i][j] = npa[i][j];\n}\nvoid ch1() {\n for (int i = 0; i < p; i++)\n for (int j = 0; j < p / 2; j++)\n swap(pa[i][j], pa[i][p - j - 1]);\n}\nint main() {\n int D[256];\n for (int i = 'A'; i <= 'Z'; i++)\n D[i] = i - 'A';\n for (int i = 'a'; i <= 'z'; i++)\n D[i] = i - 'a' + 26;\n for (int i = '0'; i <= '9'; i++)\n D[i] = i - '0' + 52;\n D['+'] = 62, D['/'] = 63;\n while (cin >> w >> h >> p, w) {\n string s;\n used.clear();\n for (int i = 0; i < h; i++) {\n cin >> s;\n for (int j = 0; j < s.size(); j++) {\n int t = D[s[j]];\n int a = 6;\n while (a--)\n im[i][j * 6 + a] = t % 2, t /= 2;\n }\n }\n for (int i = 0; i < p; i++) {\n cin >> s;\n for (int j = 0; j < s.size(); j++) {\n int t = D[s[j]];\n int a = 6;\n while (a--)\n pa[i][j * 6 + a] = t % 2, t /= 2;\n }\n }\n ans = 0;\n for (int i = 0; i < 4; i++) {\n ppp();\n ch1();\n ppp();\n ch1();\n ch();\n }\n cout << ans << endl;\n }\n return 0;\n}", "accuracy": 1, "time_ms": 150, "memory_kb": 12172, "score_of_the_acc": -0.1075, "final_rank": 2 }, { "submission_id": "aoj_1312_8051619", "code_snippet": "#include <iostream>\n#include <vector>\n#include <unordered_set>\n#include <string>\n#include <cstring>\n\nusing namespace std;\n\n#define N 3\n#define MAX 1010\n\ntypedef unsigned long long ull;\n\nbool im[MAX][MAX], pa[MAX][MAX], npa[MAX][MAX];\nint w, h, p, ans;\nvector<ull> used;\nunordered_set<ull> used_set;\n\nvoid ppp() {\n ull a = 0;\n ull roll[MAX][MAX] = {};\n for (int i = 0; i < p; i++)\n for (int j = 0; j < p; j++)\n a = a * N + pa[i][j];\n \n if (used_set.count(a)) {\n return;\n }\n used_set.insert(a);\n \n for (int j = 0; j < h; j++) {\n ull b = 0, g = 1;\n for (int i = 0; i < p; i++)\n b = b * N + im[j][i], g *= N;\n roll[j][p - 1] = b;\n for (int i = p; i < w; i++) {\n b = b * N + im[j][i] - im[j][i - p] * g;\n roll[j][i] = b;\n }\n }\n for (int j = p - 1; j < w; j++) {\n ull b = 0, g = 1, n = 1;\n for (int i = 0; i < p; i++)\n n *= N;\n for (int i = 0; i < p; i++)\n b = b * n + roll[i][j], g *= n;\n if (b == a)\n ans++;\n for (int i = p; i < h; i++) {\n b = b * n + roll[i][j] - roll[i - p][j] * g;\n if (b == a)\n ans++;\n }\n }\n}\n\nvoid ch() {\n for (int i = 0; i < p; i++)\n for (int j = 0; j < p; j++)\n npa[i][j] = pa[j][p - i - 1];\n memcpy(pa, npa, sizeof(npa));\n}\n\nvoid ch1() {\n for (int i = 0; i < p; i++)\n for (int j = 0; j < p / 2; j++)\n swap(pa[i][j], pa[i][p - j - 1]);\n}\n\nint main() {\n int D[256];\n for (int i = 'A'; i <= 'Z'; i++)\n D[i] = i - 'A';\n for (int i = 'a'; i <= 'z'; i++)\n D[i] = i - 'a' + 26;\n for (int i = '0'; i <= '9'; i++)\n D[i] = i - '0' + 52;\n D['+'] = 62, D['/'] = 63;\n while (cin >> w >> h >> p, w) {\n string s;\n used.clear();\n used_set.clear();\n \n for (int i = 0; i < h; i++) {\n cin >> s;\n for (int j = 0; j < s.size(); j++) {\n int t = D[s[j]];\n int a = 6;\n while (a--)\n im[i][j * 6 + a] = t % 2, t /= 2;\n }\n }\n for (int i = 0; i < p; i++) {\n cin >> s;\n for (int j = 0; j < s.size(); j++) {\n int t = D[s[j]];\n int a = 6;\n while (a--)\n pa[i][j * 6 + a] = t % 2, t /= 2;\n }\n }\n \n ans = 0;\n for (int i = 0; i < 4; i++) {\n ppp();\n ch1();\n ppp();\n ch1();\n ch();\n }\n cout << ans << endl;\n }\n return 0;\n}", "accuracy": 1, "time_ms": 160, "memory_kb": 13376, "score_of_the_acc": -0.1839, "final_rank": 5 }, { "submission_id": "aoj_1312_8051616", "code_snippet": "#include <bits/stdc++.h>\n#define N 3\nusing namespace std;\ntypedef unsigned long long ull;\nbool im[1010][1010], pa[110][110];\nint w, h, p, ans;\nunordered_set<ull> used;\nvoid ppp() {\n ull a = 0;\n ull roll[1001][1001] = {};\n for (int i = 0; i < p; i++)\n for (int j = 0; j < p; j++)\n a = a * N + pa[i][j];\n if (used.count(a))\n return;\n used.insert(a);\n for (int j = 0; j < h; j++) {\n ull b = 0, g = 1;\n for (int i = 0; i < p; i++)\n b = b * N + im[j][i], g *= N;\n roll[j][p - 1] = b;\n for (int i = p; i < w; i++) {\n b = b * N + im[j][i] - im[j][i - p] * g;\n roll[j][i] = b;\n }\n }\n for (int j = p - 1; j < w; j++) {\n ull b = 0, g = 1, n = 1;\n for (int i = 0; i < p; i++)\n n *= N;\n for (int i = 0; i < p; i++)\n b = b * n + roll[i][j], g *= n;\n if (b == a)\n ans++;\n for (int i = p; i < h; i++) {\n b = b * n + roll[i][j] - roll[i - p][j] * g;\n if (b == a)\n ans++;\n }\n }\n}\nvoid ch() {\n bool npa[110][110];\n for (int i = 0; i < p; i++)\n for (int j = 0; j < p; j++)\n npa[i][j] = pa[j][p - i - 1];\n for (int i = 0; i < p; i++)\n for (int j = 0; j < p; j++)\n pa[i][j] = npa[i][j];\n}\nvoid ch1() {\n for (int i = 0; i < p; i++)\n for (int j = 0; j < p / 2; j++)\n swap(pa[i][j], pa[i][p - j - 1]);\n}\nint main() {\n int D[256];\n for (int i = 'A'; i <= 'Z'; i++)\n D[i] = i - 'A';\n for (int i = 'a'; i <= 'z'; i++)\n D[i] = i - 'a' + 26;\n for (int i = '0'; i <= '9'; i++)\n D[i] = i - '0' + 52;\n D['+'] = 62, D['/'] = 63;\n while (cin >> w >> h >> p, w) {\n string s;\n used.clear();\n for (int i = 0; i < h; i++) {\n cin >> s;\n for (int j = 0; j < s.size(); j++) {\n int t = D[s[j]];\n int a = 6;\n while (a--)\n im[i][j * 6 + a] = t & 1, t >>= 1;\n }\n }\n for (int i = 0; i < p; i++) {\n cin >> s;\n for (int j = 0; j < s.size(); j++) {\n int t = D[s[j]];\n int a = 6;\n while (a--)\n pa[i][j * 6 + a] = t & 1, t >>= 1;\n }\n }\n ans = 0;\n for (int i = 0; i < 4; i++) {\n ppp();\n ch1();\n ppp();\n ch1();\n ch();\n }\n cout << ans << endl;\n }\n return 0;\n}", "accuracy": 1, "time_ms": 150, "memory_kb": 12152, "score_of_the_acc": -0.1064, "final_rank": 1 }, { "submission_id": "aoj_1312_8051615", "code_snippet": "#include <bits/stdc++.h>\n#define N 3\nusing namespace std;\ntypedef unsigned long long ull;\nbool im[1010][1010], pa[110][110];\nint w, h, p, ans;\nunordered_set<ull> used; // use unordered_set instead of vector to check for duplicates\nvoid ppp() {\n ull a = 0;\n ull roll[1001][1001] = {};\n for (int i = 0; i < p; i++)\n for (int j = 0; j < p; j++)\n a = a * N + pa[i][j];\n if (used.count(a)) // use unordered_set's count() function to check for duplicates\n return;\n used.insert(a);\n for (int j = 0; j < h; j++) {\n ull b = 0, g = 1;\n for (int i = 0; i < p; i++)\n b = b * N + im[j][i], g *= N;\n roll[j][p - 1] = b;\n for (int i = p; i < w; i++) {\n b = b * N + im[j][i] - im[j][i - p] * g;\n roll[j][i] = b;\n }\n }\n for (int j = p - 1; j < w; j++) {\n ull b = 0, g = 1, n = 1;\n for (int i = 0; i < p; i++)\n n *= N;\n for (int i = 0; i < p; i++)\n b = b * n + roll[i][j], g *= n;\n if (b == a)\n ans++;\n for (int i = p; i < h; i++) {\n b = b * n + roll[i][j] - roll[i - p][j] * g;\n if (b == a)\n ans++;\n }\n }\n}\nvoid ch() {\n bool npa[110][110];\n for (int i = 0; i < p; i++)\n for (int j = 0; j < p; j++)\n npa[i][j] = pa[j][p - i - 1];\n for (int i = 0; i < p; i++)\n for (int j = 0; j < p; j++)\n pa[i][j] = npa[i][j];\n}\nvoid ch1() {\n for (int i = 0; i < p; i++)\n for (int j = 0; j < p / 2; j++)\n swap(pa[i][j], pa[i][p - j - 1]);\n}\nint main() {\n int D[256];\n for (int i = 'A'; i <= 'Z'; i++)\n D[i] = i - 'A';\n for (int i = 'a'; i <= 'z'; i++)\n D[i] = i - 'a' + 26;\n for (int i = '0'; i <= '9'; i++)\n D[i] = i - '0' + 52;\n D['+'] = 62, D['/'] = 63;\n while (cin >> w >> h >> p, w) {\n string s;\n used.clear();\n for (int i = 0; i < h; i++) {\n cin >> s;\n for (int j = 0; j < s.size(); j++) {\n int t = D[s[j]];\n int a = 6;\n while (a--)\n im[i][j * 6 + a] = t % 2, t /= 2;\n }\n }\n for (int i = 0; i < p; i++) {\n cin >> s;\n for (int j = 0; j < s.size(); j++) {\n int t = D[s[j]];\n int a = 6;\n while (a--)\n pa[i][j * 6 + a] = t % 2, t /= 2;\n }\n }\n ans = 0;\n for (int i = 0; i < 4; i++) {\n ppp();\n ch1();\n ppp();\n ch1();\n ch();\n }\n cout << ans << endl;\n }\n return 0;\n}", "accuracy": 1, "time_ms": 150, "memory_kb": 12264, "score_of_the_acc": -0.1124, "final_rank": 3 } ]
aoj_1323_cpp
Problem I: Encircling Circles You are given a set of circles C of a variety of radii (radiuses) placed at a variety of positions, possibly overlapping one another. Given a circle with radius r , that circle may be placed so that it encircles all of the circles in the set C if r is large enough. There may be more than one possible position of the circle of radius r to encircle all the member circles of C . We define the region U as the union of the areas of encircling circles at all such positions. In other words, for each point in U , there exists a circle of radius r that encircles that point and all the members of C . Your task is to calculate the length of the periphery of that region U . Figure I.1 shows an example of the set of circles C and the region U . In the figure, three circles contained in C are expressed by circles of solid circumference, some possible positions of the encircling circles are expressed by circles of dashed circumference, and the area U is expressed by a thick dashed closed curve. Input The input is a sequence of datasets. The number of datasets is less than 100. Each dataset is formatted as follows. n r x 1 y 1 r 1 x 2 y 2 r 2 . . . x n y n r n The first line of a dataset contains two positive integers, n and r , separated by a single space. n means the number of the circles in the set C and does not exceed 100. r means the radius of the encircling circle and does not exceed 1000. Each of the n lines following the first line contains three integers separated by a single space. ( x i , y i ) means the center position of the i -th circle of the set C and r i means its radius. You may assume −500≤ x i ≤500, −500≤ y i ≤500, and 1≤ r i ≤500. The end of the input is indicated by a line containing two zeros separated by a single space. Output For each dataset, output a line containing a decimal fraction which means the length of the periphery (circumferential length) of the region U . The output should not contain an error greater than 0.01. You can assume that, when r changes by ε (|ε| < 0.0000001), the length of the periphery of the region U will not change more than 0.001. If r is too small to cover all of the circles in C , output a line containing only 0.0. No other characters should be contained in the output. Sample Input 1 10 5 5 7 2 12 5 5 7 8 6 3 3 10 3 11 2 2 1 1 2 16 3 3 15 -5 2 5 9 2 9 5 8 6 3 38 -25 -10 8 30 5 7 -3 35 11 3 39 -25 -10 8 30 5 7 -3 35 11 3 800 -400 400 2 300 300 1 300 302 1 3 800 400 -400 2 300 300 1 307 300 3 8 147 130 80 12 130 -40 12 -110 80 12 -110 -40 12 70 140 12 70 -100 12 -50 140 12 -50 -100 12 3 493 345 154 10 291 111 75 -275 -301 46 4 55 54 0 1 40 30 5 27 36 10 0 48 7 3 30 0 3 3 -3 0 4 400 0 3 3 7 2 3 2 -5 -4 2 -4 3 2 3 10 -5 -4 5 2 3 5 -4 3 5 4 6 4 6 1 5 5 1 1 7 1 0 1 1 3 493 345 154 10 291 111 75 -275 -301 46 5 20 -9 12 5 0 15 5 3 -3 3 12 9 5 -12 9 5 0 0 Output for the Sample Input 81.68140899333463 106.81415022205297 74.11215318612639 108.92086846105579 0.0 254.85616536128433 8576.93 ...(truncated)
[ { "submission_id": "aoj_1323_6159804", "code_snippet": "#pragma GCC optimize(\"O3\")\n#pragma GCC optimize(\"unroll-loops\")\n#include<iostream>\n#include<string>\n#include<cstdio>\n#include<vector>\n#include<cmath>\n#include<algorithm>\n#include<functional>\n#include<iomanip>\n#include<queue>\n#include<ciso646>\n#include<random>\n#include<map>\n#include<set>\n#include<bitset>\n#include<stack>\n#include<unordered_map>\n#include<unordered_set>\n#include<utility>\n#include<cassert>\n#include<complex>\n#include<numeric>\n#include<array>\n#include<chrono>\nusing namespace std;\n\n//#define int long long\ntypedef long long ll;\n\ntypedef unsigned long long ul;\ntypedef unsigned int ui;\nconstexpr ll mod = 1000000007;\nconst ll INF = mod * mod;\ntypedef pair<int, int>P;\n\n#define rep(i,n) for(int i=0;i<n;i++)\n#define per(i,n) for(int i=n-1;i>=0;i--)\n#define Rep(i,sta,n) for(int i=sta;i<n;i++)\n#define rep1(i,n) for(int i=1;i<=n;i++)\n#define per1(i,n) for(int i=n;i>=1;i--)\n#define Rep1(i,sta,n) for(int i=sta;i<=n;i++)\n#define all(v) (v).begin(),(v).end()\ntypedef pair<ll, ll> LP;\n\ntemplate<typename T>\nvoid chmin(T& a, T b) {\n\ta = min(a, b);\n}\ntemplate<typename T>\nvoid chmax(T& a, T b) {\n\ta = max(a, b);\n}\ntemplate<typename T>\nvoid cinarray(vector<T>& v) {\n\trep(i, v.size())cin >> v[i];\n}\ntemplate<typename T>\nvoid coutarray(vector<T>& v) {\n\trep(i, v.size()) {\n\t\tif (i > 0)cout << \" \"; cout << v[i];\n\t}\n\tcout << \"\\n\";\n}\nll mod_pow(ll x, ll n, ll m = mod) {\n\tif (n < 0) {\n\t\tll res = mod_pow(x, -n, m);\n\t\treturn mod_pow(res, m - 2, m);\n\t}\n\tif (abs(x) >= m)x %= m;\n\tif (x < 0)x += m;\n\tif (x == 0)return 0;\n\tll res = 1;\n\twhile (n) {\n\t\tif (n & 1)res = res * x % m;\n\t\tx = x * x % m; n >>= 1;\n\t}\n\treturn res;\n}\nstruct modint {\n\tint n;\n\tmodint() :n(0) { ; }\n\tmodint(ll m) {\n\t\tif (m < 0 || mod <= m) {\n\t\t\tm %= mod; if (m < 0)m += mod;\n\t\t}\n\t\tn = m;\n\t}\n\toperator int() { return n; }\n};\nbool operator==(modint a, modint b) { return a.n == b.n; }\nmodint operator+=(modint& a, modint b) { a.n += b.n; if (a.n >= mod)a.n -= mod; return a; }\nmodint operator-=(modint& a, modint b) { a.n -= b.n; if (a.n < 0)a.n += mod; return a; }\nmodint operator*=(modint& a, modint b) { a.n = ((ll)a.n * b.n) % mod; return a; }\nmodint operator+(modint a, modint b) { return a += b; }\nmodint operator-(modint a, modint b) { return a -= b; }\nmodint operator*(modint a, modint b) { return a *= b; }\nmodint operator^(modint a, ll n) {\n\tif (n == 0)return modint(1);\n\tmodint res = (a * a) ^ (n / 2);\n\tif (n % 2)res = res * a;\n\treturn res;\n}\n\nll inv(ll a, ll p) {\n\treturn (a == 1 ? 1 : (1 - p * inv(p % a, a)) / a + p);\n}\nmodint operator/(modint a, modint b) { return a * modint(inv(b, mod)); }\nmodint operator/=(modint& a, modint b) { a = a / b; return a; }\nconst int max_n = 1 << 10;\nmodint fact[max_n], factinv[max_n];\nvoid init_f() {\n\tfact[0] = modint(1);\n\tfor (int i = 0; i < max_n - 1; i++) {\n\t\tfact[i + 1] = fact[i] * modint(i + 1);\n\t}\n\tfactinv[max_n - 1] = modint(1) / fact[max_n - 1];\n\tfor (int i = max_n - 2; i >= 0; i--) {\n\t\tfactinv[i] = factinv[i + 1] * modint(i + 1);\n\t}\n}\nmodint comb(int a, int b) {\n\tif (a < 0 || b < 0 || a < b)return 0;\n\treturn fact[a] * factinv[b] * factinv[a - b];\n}\nmodint combP(int a, int b) {\n\tif (a < 0 || b < 0 || a < b)return 0;\n\treturn fact[a] * factinv[a - b];\n}\n\nll gcd(ll a, ll b) {\n\ta = abs(a); b = abs(b);\n\tif (a < b)swap(a, b);\n\twhile (b) {\n\t\tll r = a % b; a = b; b = r;\n\t}\n\treturn a;\n}\ntypedef long double ld;\ntypedef pair<ld, ld> LDP;\nconst ld eps = 1e-8;\nconst ld pi = acosl(-1.0);\nint dx[4] = { 1,0,-1,0 };\nint dy[4] = { 0,1,0,-1 };\n\n//-----------------------------------\ntypedef complex<ld> Point;\nld dot(Point a, Point b) { return real(conj(a) * b); }\nld cross(Point a, Point b) { return imag(conj(a) * b); }\nnamespace std {\n\tbool operator<(const Point& lhs, const Point& rhs) {\n\t\treturn lhs.real() == rhs.real() ? lhs.imag() < rhs.imag() : lhs.real() < rhs.real();\n\t}\n}\nstruct Line {\n\tPoint a, b;\n};\nstruct Circle {\n\tPoint p; ld r;\n};\nint ccw(Point a, Point b, Point c) {\n\tb -= a; c -= a;\n\tif (cross(b, c) > eps)return 1;//counter clockwise\n\tif (cross(b, c) < -eps)return -1;//clock wise\n\tif (dot(b, c) < 0)return 2;//c--a--b on line\n\tif (norm(b) < norm(c))return -2;//a--b--c on line\n\treturn 0; //a--c--b on line\n}\n\n//円と円の交点\n//交点が無かったらnullが返されます\n//交点は大体いつも2つあるのでvector\nvector<Point> is_cc(Circle c1, Circle c2) {\n\tvector<Point> res;\n\tld d = abs(c1.p - c2.p);\n\tld rc = (d * d + c1.r * c1.r - c2.r * c2.r) / (2 * d);\n\tld dfr = c1.r * c1.r - rc * rc;\n\tif (abs(dfr) < eps)dfr = 0.0;\n\tif (dfr < 0.0)return res;\n\tld rs = sqrt(dfr);\n\tPoint diff = (c2.p - c1.p) / d;\n\tres.push_back(c1.p + diff * Point(rc, rs));\n\tif (dfr != 0.0)res.push_back(c1.p + diff * Point(rc, -rs));\n\treturn res;\n}\n\nint n, r;\nvoid solve() {\n\tvector<Circle> c(n);\n\trep(i, n) {\n\t\tint x, y, t; cin >> x >> y >> t;\n\t\tc[i] = { {(ld)x,(ld)y},(ld)t };\n\t}\n\t{\n\t\tvector<Circle> nc;\n\t\tvector<bool> ban(n);\n\t\trep(i, n) {\n\t\t\trep(j, n)if (i != j)if(!ban[j]) {\n\t\t\t\tld dif = abs(c[j].p - c[i].p);\n\t\t\t\tif (dif + c[i].r <= c[j].r + eps) {\n\t\t\t\t\tban[i] = true;\n\t\t\t\t}\n\t\t\t}\n\t\t\tif (!ban[i]) {\n\t\t\t\tnc.push_back(c[i]);\n\t\t\t}\n\t\t}\n\t\tswap(c, nc);\n\t\tn = c.size();\n\t}\n\trep(i, n) {\n\t\tif (c[i].r > r) {\n\t\t\tcout << 0 << \"\\n\"; return;\n\t\t}\n\t}\n\n\tif (n == 1) {\n\t\tld ans = 2 * pi * (2*r-c[0].r);\n\t\tcout << ans << \"\\n\";\n\t\treturn;\n\t}\n\tvector<Circle> rc(n);\n\trep(i, n) {\n\t\trc[i].p = c[i].p;\n\t\trc[i].r = r - c[i].r;\n\t}\n\tvector<pair<ld, int>> vp;\n\tvector<Point> pre;\n\trep(i, n)Rep(j, i + 1, n) {\n\t\tvector<Point> cc = is_cc(rc[i], rc[j]);\n\t\tfor (Point p : cc) {\n\t\t\tbool valid = true;\n\t\t\tfor (Point pp : pre)if (abs(pp - p) < eps)valid = false;\n\t\t\tif (!valid)continue;\n\t\t\trep(k, n) {\n\t\t\t\tld len = abs(c[k].p - p) + c[k].r;\n\t\t\t\tif (len > r + eps)valid = false;\n\t\t\t}\n\t\t\tif (!valid)continue;\n\t\t\tpre.push_back(p);\n\t\t\trep(k, n) {\n\t\t\t\tld len = abs(c[k].p - p) + c[k].r;\n\t\t\t\tif (abs(len - r) < eps) {\n\t\t\t\t\tld t = arg(p - c[k].p);\n\t\t\t\t\tvp.push_back({ t,k });\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n\tif (vp.empty()) {\n\t\t//cout << \"nande\\n\";\n\t\tcout << 0 << \"\\n\"; return;\n\t}\n\tld ans = 0;\n\tsort(all(vp));\n\t{\n\t\tvector<int> cnt(n);\n\t\tfor (auto p : vp)cnt[p.second]++;\n\t\tvector<pair<ld, int>>cop;\n\t\trep(i, vp.size())if (cnt[vp[i].second] != 1) {\n\t\t\tcop.push_back(vp[i]);\n\t\t}\n\t\tswap(vp, cop);\n\t}\n\n\tassert(vp.size()&&vp.size() % 2 == 0);\n\trep(i, vp.size()) {\n\t\tint ni = (i + 1 == vp.size() ? 0 : i + 1);\n\t\tld dt = vp[ni].first - vp[i].first;\n\t\tif (dt < 0)dt += 2 * pi;\n\t\tif (vp[i].second == vp[ni].second) {\n\t\t\tint id = vp[i].second;\n\t\t\tans += dt * (2*r-c[id].r);\n\t\t}\n\t\telse {\n\t\t\tans += dt * r;\n\t\t}\n\t}\n\tcout << ans << \"\\n\";\n}\n\n\nsigned main() {\n\tios::sync_with_stdio(false);\n\tcin.tie(0);\n\tcout << fixed << setprecision(8);\n\t//init_f();\n\t//init();\n\t//while(true)\n\t//expr();\n\t//int t; cin >> t; rep(i, t)\n\twhile(cin>>n>>r,n)\n\tsolve();\n\treturn 0;\n}", "accuracy": 1, "time_ms": 40, "memory_kb": 3588, "score_of_the_acc": -0.9587, "final_rank": 6 }, { "submission_id": "aoj_1323_4811351", "code_snippet": "#include<bits/stdc++.h>\ntypedef long long int ll;\ntypedef unsigned long long int ull;\n#define BIG_NUM 2000000000\n#define HUGE_NUM 1000000000000000000\n#define MOD 1000000007\n#define EPS 0.000000001\nusing namespace std;\n\n\n\n#define SIZE 105\n\nenum Type{\n\tto_ccw,\n\tto_cw,\n};\n\nstruct Point{\n\tPoint(double arg_x,double arg_y){\n\t\tx = arg_x;\n\t\ty = arg_y;\n\t}\n\n\tPoint(){\n\t\tx = y = 0.0;\n\t}\n\n\tPoint operator + (Point p){ return Point(x+p.x,y+p.y); }\n\tPoint operator - (Point p){ return Point(x-p.x,y-p.y);}\n\tPoint operator * (double a){ return Point(a*x,a*y); }\n\tPoint operator / (double a){ return Point(x/a,y/a); }\n\n\tdouble abs(){ return sqrt(norm()); }\n\tdouble norm(){ return x*x + y*y; }\n\n\tbool operator<(const Point &p) const{\n\t\treturn x != p.x? x < p.x: y < p.y;\n\t}\n\n\tbool operator == (const Point &p) const{\n\t\treturn fabs(x-p.x) < EPS && fabs(y-p.y) < EPS;\n\t}\n\n\n\tvoid debug(){\n\n\t\tprintf(\"(%.3lf,%.3lf)\\n\",x,y);\n\t}\n\n\tdouble x,y;\n};\n\ntypedef Point Vector;\ntypedef vector<Point> Polygon;\n\nstruct Circle{\n\tPoint center;\n\tdouble r;\n};\n\nstruct Info{\n\tInfo(){\n\n\t\trad = 0;\n\t\ttype = to_ccw;\n\t}\n\tInfo(double arg_rad,Type arg_type){\n\t\trad = arg_rad;\n\t\ttype = arg_type;\n\t}\n\tbool operator<(const struct Info &arg) const{\n\n\t\tif(fabs(rad-arg.rad) > EPS){\n\t\t\treturn rad < arg.rad;\n\t\t}else{\n\n\t\t\treturn type < arg.type;\n\t\t}\n\t}\n\tbool operator==(const struct Info &arg) const{\n\n\t\treturn fabs(rad-arg.rad) < EPS && type == arg.type;\n\t}\n\n\tdouble rad;\n\tType type;\n};\n\n\nint N;\ndouble R;\nCircle C[SIZE];\n\n\ndouble calc_dist(Point A,Point B){\n\n\treturn sqrt((A.x-B.x)*(A.x-B.x)+(A.y-B.y)*(A.y-B.y));\n}\n\ndouble norm(Vector a){\n\treturn a.x*a.x+a.y*a.y;\n}\n\ndouble abs(Vector a){\n\treturn sqrt(norm(a));\n}\n\ndouble cross(Vector a,Vector b){\n return a.x*b.y-a.y*b.x;\n}\n\ndouble dot(Vector a,Vector b){\n return a.x*b.x + a.y*b.y;\n}\n\nstatic const int COUNTER_CLOCKWISE = 1;\nstatic const int CLOCKWISE = -1;\nstatic const int ONLINE_BACK = 2;\nstatic const int ONLINE_FRONT = -2;\nstatic const int ON_SEGMENT = 0;\n\nint ccw(Point p0,Point p1,Point p2){\n\n\tVector a = p1 - p0;\n\tVector b = p2 - p0;\n\n\tif(cross(a,b) > EPS)return COUNTER_CLOCKWISE;\n\tif(cross(a,b) < -EPS)return CLOCKWISE;\n\tif(dot(a,b) < -EPS)return ONLINE_BACK;\n\tif(a.norm() < b.norm())return ONLINE_FRONT;\n\n\treturn ON_SEGMENT;\n}\n\n\n\n//点のradを求める関数\ndouble calc_rad(Point p){\n\n\tif(p.y == 0){\n\n\t\tif(p.x > 0){\n\n\t\t\treturn 0;\n\n\t\t}else{ //p.x < 0\n\n\t\t\treturn M_PI;\n\t\t}\n\n\t}else if(p.x == 0){\n\n\t\tif(p.y > 0){\n\n\t\t\treturn M_PI/2.0;\n\t\t}else{\n\n\n\t\t\treturn 3*M_PI/2.0;\n\t\t}\n\n\t}else{\n\n\t\tdouble base = atan(fabs(p.y)/fabs(p.x));\n\n\t\tif(p.x > 0){\n\n\t\t\tif(p.y > 0){\n\n\t\t\t\treturn base;\n\n\t\t\t}else{ //p.y < 0\n\n\t\t\t\treturn 2*M_PI-base;\n\t\t\t}\n\n\t\t}else{ //p.x < 0\n\n\t\t\tif(p.y > 0){\n\n\t\t\t\treturn M_PI-base;\n\n\t\t\t}else{\n\n\t\t\t\treturn base+M_PI;\n\t\t\t}\n\t\t}\n\t}\n}\n\n/*点moveをbaseを中心にradラジアン回転させる\nrad > 0なら反時計回り、rad < 0なら時計周り\n*/\nPoint rotate(Point base,Point move,double rad){\n\n\tPoint ret;\n\tmove = move-base;\n\n\tret.x = move.x*cos(rad)-move.y*sin(rad);\n\tret.y = move.x*sin(rad)+move.y*cos(rad);\n\n\tret = ret+base;\n\n\treturn ret;\n}\n\nint count_touch_C(Point P){\n\n\tint ret = 0;\n\n\tfor(int i = 0; i < N; i++){\n\t\tif(fabs(calc_dist(P,C[i].center)+C[i].r-R) < EPS){\n\n\t\t\tret++;\n\t\t}\n\t\tif(fabs(calc_dist(P,C[i].center)+C[i].r) > R+EPS){\n\n\t\t\treturn -1;\n\t\t}\n\t}\n\n\treturn ret;\n}\n\n//円aが円bを含むか判定\nbool contain_check(Circle a,Circle b){\n\n\treturn fabs(calc_dist(a.center,b.center)+b.r)+EPS < a.r;\n}\n\n//三辺が三角形を作れるか判定\nbool triangle_check(double a,double b,double c){\n\n\treturn (a+b > c+EPS) && (b+c > a+EPS) && (c+a > b+EPS);\n}\n\nvoid func(){\n\n\tbool first_check = true;\n\n\tfor(int i = 0; i < N; i++){\n\n\t\tscanf(\"%lf %lf %lf\",&C[i].center.x,&C[i].center.y,&C[i].r);\n\t\tif(C[i].r > R+EPS){\n\n\t\t\tfirst_check = false;\n\t\t}\n\t}\n\tif(!first_check){\n\n\t\tprintf(\"0.0\\n\");\n\t\treturn;\n\t}\n\tif(N == 1){\n\n\t\tprintf(\"%.10lf\\n\",(2*R-C[0].r)*2*M_PI);\n\t\treturn;\n\t}\n\n\tdouble ans = 0;\n\n\tvector<Info> info;\n\n\tbool contain_FLG;\n\n\tfor(int i = 0; i < N; i++){\n\n\t\tdouble one_R = 2*R-C[i].r;\n\t\tPoint P = Point(C[i].center.x+(R-C[i].r),C[i].center.y); //rad0の位置にある包含円\n\n\t\tcontain_FLG = false;\n\t\tfor(int k = 0; k < N; k++){\n\t\t\tif(k == i)continue;\n\n\t\t\tif(contain_check(C[k],C[i])){\n\n\t\t\t\tcontain_FLG = true;\n\t\t\t\tbreak;\n\t\t\t}\n\t\t}\n\t\tif(contain_FLG)continue;\n\n\t\tinfo.clear();\n\n\t\tint num_contain = 0;\n\n\t\tfor(int k = 0; k < N; k++){\n\t\t\tif(k == i)continue;\n\t\t\tif(contain_check(C[i],C[k])){\n\n\t\t\t\tnum_contain++;\n\t\t\t\tcontinue;\n\t\t\t}\n\n\t\t\tdouble len_i = R-C[i].r;\n\t\t\tdouble len_k = R-C[k].r;\n\t\t\tdouble len_ik = calc_dist(C[i].center,C[k].center);\n\n\t\t\tif(!triangle_check(len_i,len_k,len_ik)){\n\t\t\t\tcontinue;\n\t\t\t}\n\n\n\t\t\tdouble cos_theta = ((len_i*len_i+len_ik*len_ik)-len_k*len_k)/(2*len_i*len_ik);\n\n\t\t\tdouble rad_i = acos(cos_theta);\n\n\t\t\tfor(int loop = 0; loop < 2; loop++){\n\n\t\t\t\tdouble roll_rad;\n\t\t\t\tif(loop == 0){\n\t\t\t\t\t//辺ikをcw回りに回転\n\t\t\t\t\troll_rad = -rad_i;\n\n\t\t\t\t}else{\n\t\t\t\t\t//ccw回りに回転\n\t\t\t\t\troll_rad = rad_i;\n\t\t\t\t}\n\n\t\t\t\tPoint work = rotate(C[i].center,C[k].center,roll_rad);\n\t\t\t\tdouble tmp_rad = calc_rad(work-C[i].center);\n\n\t\t\t\tPoint work_P = rotate(C[i].center,P,tmp_rad);\n\n\t\t\t\tint tmp_num = count_touch_C(work_P);\n\n\t\t\t\tif(tmp_num != -1){\n\n\t\t\t\t\tif(ccw(work_P,C[i].center,C[k].center) == COUNTER_CLOCKWISE){\n\n\t\t\t\t\t\tinfo.push_back(Info(tmp_rad,to_cw));\n\n\t\t\t\t\t}else{\n\t\t\t\t\t\tinfo.push_back(Info(tmp_rad,to_ccw));\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\tif(num_contain == N-1){ //他の全ての円を含む\n\n\t\t\tprintf(\"%.10lf\\n\",one_R*2*M_PI);\n\t\t\treturn;\n\t\t}\n\n\t\tif(info.size() == 0)continue;\n\n\t\tsort(info.begin(),info.end());\n\t\tinfo.erase(unique(info.begin(),info.end()),info.end()); //★★★3つ以上円に接する場合は、同じ地点が複数出てくる★★★\n\n\t\tint start = 0;\n\t\t//★★to_ccwとto_cwが交互に現れるはず★★\n\t\tif(info[0].type == to_cw){\n\n\t\t\tstart = info.size()-1;\n\t\t}\n\n\t\tdouble one_rad = 0; //円iだけに引っかかる区間を求める\n\t\tdouble add_len = 0; //2つ以上引っ掛かる場合に、ccw順で一番近い円と作る弧の長さを足す\n\n\t\tfor(int k = 0; k < info.size(); k += 2){\n\n\t\t\tdouble from = info[(start+k)%info.size()].rad;\n\t\t\tdouble to = info[(start+k+1)%info.size()].rad;\n\n\t\t\tif(to >= from){\n\n\t\t\t\tone_rad += to-from;\n\n\t\t\t}else{ //to < from;\n\n\t\t\t\tone_rad += 2*M_PI-(from-to);\n\t\t\t}\n\n\t\t\tPoint from_P = rotate(C[i].center,P,from);\n\t\t\tdouble rad_from = calc_rad(C[i].center-from_P);//★★注意★★\n\n\n\t\t\tdouble min_rad = BIG_NUM;\n\t\t\tfor(int a = 0; a < N; a++){\n\t\t\t\tif(a == i)continue;\n\t\t\t\tif(fabs(calc_dist(from_P,C[a].center)+C[a].r-R) > EPS){\n\t\t\t\t\tcontinue; //円弧に接していない小円はSKIP\n\t\t\t\t}\n\n\t\t\t\tdouble tmp_rad = calc_rad(C[a].center-from_P);\n\t\t\t\tif(tmp_rad > rad_from){\n\n\t\t\t\t\tmin_rad = min(min_rad,tmp_rad-rad_from);\n\n\t\t\t\t}else{\n\n\t\t\t\t\tmin_rad = min(min_rad,2*M_PI-(rad_from-tmp_rad));\n\t\t\t\t}\n\t\t\t}\n\n\t\t\tif(min_rad < M_PI+EPS){\n\n\t\t\t\tadd_len += R*min_rad;\n\t\t\t}\n\n\t\t\tif(fabs(from-to) < EPS)continue; //★★同一地点★★\n\n\t\t\tPoint to_P = rotate(C[i].center,P,to);\n\t\t\tdouble rad_to = calc_rad(C[i].center-to_P); //★★注意★★\n\n\t\t\tmin_rad = BIG_NUM;\n\t\t\tfor(int a = 0; a < N; a++){\n\t\t\t\tif(a == i)continue;\n\n\t\t\t\tif(fabs(calc_dist(to_P,C[a].center)+C[a].r-R) > EPS){\n\t\t\t\t\tcontinue; //円弧に接していない小円はSKIP\n\t\t\t\t}\n\n\t\t\t\tdouble tmp_rad = calc_rad(C[a].center-to_P);\n\n\t\t\t\tif(tmp_rad > rad_to){\n\n\t\t\t\t\tmin_rad = min(min_rad,tmp_rad-rad_to);\n\n\t\t\t\t}else{\n\n\t\t\t\t\tmin_rad = min(min_rad,2*M_PI-(rad_to-tmp_rad));\n\t\t\t\t}\n\t\t\t}\n\n\t\t\tif(min_rad < M_PI+EPS){\n\n\t\t\t\tadd_len += R*min_rad;\n\t\t\t}\n\t\t}\n\n\t\tans += add_len+one_R*one_rad;\n\t}\n\n\tprintf(\"%.10lf\\n\",ans);\n}\n\nint main(){\n\n\twhile(true){\n\n\t\tscanf(\"%d %lf\",&N,&R);\n\t\tif(N == 0)break;\n\n\t\tfunc();\n\t}\n\n\treturn 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3608, "score_of_the_acc": -0.9445, "final_rank": 5 }, { "submission_id": "aoj_1323_3215685", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\n#define rep(i,n) for(int (i)=0;(i)<(int)(n);++(i))\n#define all(x) (x).begin(),(x).end()\n#define pb push_back\n#define fi first\n#define se second\n#define dbg(x) cout<<#x\" = \"<<((x))<<endl\ntemplate<class T,class U> ostream& operator<<(ostream& o, const pair<T,U> &p){o<<\"(\"<<p.fi<<\",\"<<p.se<<\")\";return o;}\ntemplate<class T> ostream& operator<<(ostream& o, const vector<T> &v){o<<\"[\";for(T t:v){o<<t<<\",\";}o<<\"]\";return o;}\n\n/* 基本要素 */\ntypedef complex<double> Point;\ntypedef pair<Point, Point> Line;\ntypedef vector<Point> VP;\nconst double PI = acos(-1);\nconst double EPS = 1e-7; // 許容誤差^2\nconst double INF = 1e9;\n#define X real()\n#define Y imag()\n// #define LE(n,m) ((n) < (m) + EPS)\n#define LE(n,m) ((n) - (m) < EPS)\n// #define GE(n,m) ((n) + EPS > (m))\n#define GE(n,m) (EPS > (m) - (n))\n#define EQ(n,m) (abs((n)-(m)) < EPS)\n\n// 内積 dot(a,b) = |a||b|cosθ\ndouble dot(Point a, Point b) {\n return a.X*b.X + a.Y*b.Y;\n}\n\n// 外積 cross(a,b) = |a||b|sinθ\ndouble cross(Point a, Point b) {\n return a.X*b.Y - a.Y*b.X;\n}\n\n// 点の進行方向\n// !!! 誤差に注意 !!! (掛け算したものとかなり小さいものを比べているので)\nint ccw(Point a, Point b, Point c) {\n b -= a; c -= a;\n if (cross(b,c) > EPS) return +1; // counter clockwise\n if (cross(b,c) < -EPS) return -1; // clockwise\n if (dot(b,c) < -EPS) return +2; // c--a--b on line\n if (norm(b) < norm(c)) return -2; // a--b--c on line or a==b\n return 0; // a--c--b on line or a==c or b==c\n}\n\n// 2円の交点\nVP crosspointCC(Point a, double ar, Point b, double br) {\n VP ps;\n Point ab = b-a;\n double d = abs(ab);\n double crL = (norm(ab) + ar*ar - br*br) / (2*d);\n if (EQ(d, 0) || ar+EPS < abs(crL)) return ps;\n\n // 円が接する時、こことEPSの設定をうまくしないとsqrt(sub)がnanになるので注意\n double sub = ar*ar - crL*crL;\n if(EQ(sub,0)) sub = 0;\n\n Point abN = ab * Point(0, sqrt(sub) / d);\n Point cp = a + crL/d * ab;\n ps.push_back(cp + abN);\n if (!EQ(norm(abN), 0)) ps.push_back(cp - abN);\n return ps;\n}\n\n// aからbへの回転角(中心(0,0))[-pi,+pi]\ndouble angle(Point a,Point b){\n double t = arg(b)-arg(a);\n while(t>+PI) t-=2*PI;\n while(t<-PI) t+=2*PI;\n return t;\n}\n\ndouble solve(int n, double r){\n VP c(n);\n vector<double> cr(n);\n rep(i,n){\n int x,y;\n scanf(\" %d %d %lf\", &x, &y, &cr[i]);\n c[i] = Point(x,y);\n }\n\n // 円iを含むために、中心が移動できる範囲\n rep(i,n){\n cr[i] = r - cr[i];\n // dbg(cr[i]);\n if( !LE(0,cr[i]) ) return 0;\n }\n\n // どれかを内包する円は除去\n vector<bool> inner(n);\n rep(i,n){\n rep(j,n)if(i!=j){\n double D = abs(c[i]-c[j]);\n if( LE(D+cr[i], cr[j]) ) inner[j] = true;\n }\n }\n\n VP tttc;\n vector<double> tttr;\n rep(i,n){\n if(!inner[i]){\n tttc.pb(c[i]);\n tttr.pb(cr[i]);\n }\n }\n\n c = tttc;\n cr = tttr;\n n = c.size();\n\n if(n==1){\n return (cr[0]+r)*2*PI;\n }\n\n // n >= 2\n // 円iと円jの交点を求める\n using PPI = pair<Point,int>;\n vector<vector<PPI>> cps(n);\n\n rep(i,n)rep(j,i){\n VP cnd = crosspointCC(c[i], cr[i], c[j], cr[j]);\n if(cnd.size() == 0){\n // 交点がない時点で、2つを同時に包含できないのでおわり\n return 0;\n }\n else if(cnd.size() == 1){\n // 接するような円が見つかった時点で、その点にしか円はおけない\n // 置けるなら半径rの円\n bool ok = true;\n Point op = cnd[0];\n rep(k,n){\n if( !LE(abs(c[k]-op), cr[k]) ) ok = false;\n }\n\n if(ok) return r*2*PI;\n else return 0;\n }\n\n for(Point cp:cnd){\n cps[i].pb({cp,j});\n cps[j].pb({cp,i});\n }\n }\n\n // 偏角ソート\n double ans = 0;\n VP cand_pts;\n rep(i,n){\n vector<PPI> dat = cps[i];\n int sz = dat.size();\n\n // (偏角, index)\n using pd = pair<double,int>;\n vector<pd> stmp(sz);\n rep(j,sz){\n double theta = atan2(dat[j].fi.Y-c[i].Y, dat[j].fi.X-c[i].X);\n if( theta < -EPS ) theta = 2*PI + theta;\n stmp[j] = {theta, j};\n }\n\n sort(all(stmp));\n\n vector<double> cand_theta(sz);\n vector<int> valid(sz,1);\n rep(j,sz){\n double add = stmp[(j+1)%sz].fi - stmp[j].fi;\n if(add < -EPS) add += 2*PI;\n add /= 2;\n\n cand_theta[j] = stmp[j].fi + add;\n\n Point cp = c[i]+polar(cr[i],cand_theta[j]);\n // dbg(cp);\n // この点がokかチェック\n rep(k,n){\n if( !LE(abs(c[k]-cp), cr[k]) ) valid[j] = 0;\n }\n\n if(valid[j]){\n double theta = stmp[(j+1)%sz].fi - stmp[j].fi;\n if(theta < -EPS) theta += 2*PI;\n\n if(EQ(theta,0)) valid[j] = false;\n else ans += theta * (r+cr[i]);\n }\n }\n\n // printf(\" --- %d \\n\",i);\n // dbg(dat);\n // dbg(stmp);\n // dbg(valid);\n\n rep(j,sz){\n if(valid[j]^valid[(j+1)%sz]){\n int idx = stmp[(j+1)%sz].se;\n cand_pts.pb(dat[idx].fi);\n }\n }\n }\n\n int CPZ = cand_pts.size();\n rep(i,CPZ){\n bool ok = true;\n rep(j,i){\n double dist = abs(cand_pts[j] - cand_pts[i]);\n if(EQ(dist,0)) ok = false;\n }\n if(!ok) continue;\n // dbg(cand_pts[i]);\n\n double mx_angle = 0;\n rep(ci,n)rep(cj,ci){\n bool valid_pair = false;\n\n for(Point ppp:crosspointCC(c[ci],cr[ci],c[cj],cr[cj])){\n double ddd = abs(ppp - cand_pts[i]);\n if(EQ(ddd,0)) valid_pair = true;\n }\n\n if(valid_pair){\n Point center = cand_pts[i];\n Point aaa = c[ci] - center;\n Point bbb = c[cj] - center;\n\n mx_angle = max(mx_angle, abs(angle(aaa,bbb)));\n }\n }\n\n ans += mx_angle*r;\n }\n\n return ans;\n}\n\nint main(){\n int n;\n double r;\n while( scanf(\" %d %lf\", &n, &r),n ){\n printf(\"%.15f\\n\", solve(n,r));\n }\n return 0;\n}", "accuracy": 1, "time_ms": 40, "memory_kb": 3820, "score_of_the_acc": -1.0195, "final_rank": 7 }, { "submission_id": "aoj_1323_2418386", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\nconst double eps=1e-8;\nconst double pi=acos(-1);\nconst int maxn=111;\nint sgn(double x){return (x>eps)-(x<-eps);}\nint sgn(double a,double b){return sgn(a-b);}\ndouble sqr(double x){return x*x;}\nstruct P{\n\tdouble x,y;\n\tP(){}\n\tP(double x,double y):x(x),y(y){}\n\tdouble len2(){\n\t\treturn sqr(x)+sqr(y);\n\t}\n\tdouble len(){\n\t\treturn sqrt(len2());\n\t}\n\tvoid print(){\n\t\tprintf(\"(%.3f,%.3f)\\n\",x,y);\n\t}\n\tvoid read(){\n\t\tscanf(\"%lf%lf\",&x,&y);\n\t}\n\tP turn90(){return P(-y,x);}\n\tP norm(){return P(x/len(),y/len());}\n\tP rot(double rad){\n\t\treturn P(x*cos(rad)+y*sin(rad),x*sin(rad)-y*cos(rad));\n\t}\n};\nbool operator==(P a,P b){\n\treturn !sgn(a.x-b.x) and !sgn(a.y-b.y);\n}\nP operator+(P a,P b){\n\treturn P(a.x+b.x,a.y+b.y);\n}\nP operator-(P a,P b){\n\treturn P(a.x-b.x,a.y-b.y);\n}\nP operator*(P a,double b){\n\treturn P(a.x*b,a.y*b);\n}\nP operator/(P a,double b){\n\treturn P(a.x/b,a.y/b);\n}\ndouble operator^(P a,P b){\n\treturn a.x*b.x + a.y*b.y;\n}\ndouble operator*(P a,P b){\n\treturn a.x*b.y - a.y*b.x;\n}\ndouble det(P a,P b,P c){\n\treturn (b-a)*(c-a);\n}\ndouble dis(P a,P b){\n\treturn (b-a).len();\n}\ndouble Area(vector<P>poly){\n\tdouble ans=0;\n\tfor(int i=1;i<poly.size();i++)\n\t\tans+=(poly[i]-poly[0])*(poly[(i+1)%poly.size()]-poly[0]);\n\treturn fabs(ans)/2;\n}\nstruct L{\n\tP a,b;\n\tL(){}\n\tL(P a,P b):a(a),b(b){}\n\tP v(){return b-a;}\n};\nbool onLine(P p,L l){\n\treturn sgn((l.a-p)*(l.b-p))==0;\n}\nbool onSeg(P p,L s){\n\treturn onLine(p,s) and sgn((s.b-s.a)^(p-s.a))>=0 and sgn((s.a-s.b)^(p-s.b))>=0;\n}\nbool parallel(L l1,L l2){\n\treturn sgn(l1.v()*l2.v())==0;\n}\nP intersect(L l1,L l2){\n\tdouble s1=det(l1.a,l1.b,l2.a);\n\tdouble s2=det(l1.a,l1.b,l2.b);\n\treturn (l2.a*s2-l2.b*s1)/(s2-s1);\n}\nP project(P p,L l){\n\treturn l.a+l.v()*((p-l.a)^l.v())/l.v().len2();\n}\ndouble dis(P p,L l){\n\treturn fabs((p-l.a)*l.v())/l.v().len();\n}\nstruct C{\n\tP o;\n\tdouble r;\n\tC(){}\n\tC(P _o,double _r):o(_o),r(_r){}\n\tvoid read(){\n\t\to.read();\n\t\tscanf(\"%lf\",&r);\n\t}\n}c[maxn];\nbool operator<(C a,C b){\n\treturn a.r>b.r;\n}\nbool operator==(C c1,C c2){\n\treturn c1.o==c2.o && sgn(c1.r-c2.r)==0;\n}\ndouble fix(double x){return sgn(x)?x:0;}\nbool intersect(C a, C b, P &p1, P &p2) { \n\tdouble s1 = (a.o - b.o).len();\n\tif (sgn(s1 - a.r - b.r) > 0 || sgn(s1 - fabs(a.r - b.r)) < 0) return false;\n\tdouble s2 = (a.r * a.r - b.r * b.r) / s1;\n\tdouble aa = (s1 + s2) * 0.5, bb = (s1 - s2) * 0.5;\n\tP o = (b.o - a.o) * (aa / (aa + bb)) + a.o;\n\tP delta = (b.o - a.o).norm().turn90() * sqrt(fix(a.r * a.r - aa * aa));\n\tp1 = o + delta, p2 = o - delta;\n\treturn true;\n}\nint n;\ndouble r;\n\ndouble calcR(P O){\n\tdouble mxr=0;\n\tfor(int i=1;i<=n;i++){\n\t\tdouble ri=0;\n\t\tri=dis(c[i].o,O)+c[i].r;\n\t\tmxr=max(mxr,ri);\n\t}\n\treturn mxr;\n}\n\nbool check(P O){\n\tdouble mxr=0;\n\tfor(int i=1;i<=n;i++){\n\t\tdouble ri=0;\n\t\tri=dis(c[i].o,O)+c[i].r;\n\t\tif(sgn(ri-r)>0)\n\t\t\treturn false;\n\t}\n\treturn true;\n}\nP O;\nvector<P>vecO;\nbool in(P p){\n\n\tif(sgn(dis(p,O)-r)<=0)\n\t\treturn true;\n\n\tfor(int i=1;i<=n;i++){\n\t\tC c1=c[i],c2;\n\t\tc2.o=p;\n\t\tc1.r=r-c1.r;\n\t\tc2.r=r;\n\t\tP p1,p2;\n\t\tif(c1==c2||!intersect(c1,c2,p1,p2))\n\t\t\tcontinue;\n\t\tif(check(p1)||check(p2))\n\t\t\treturn true;\n\t}\n\n\treturn false;\n}\nP at(P O,P dir){\n\tdir=dir.norm();\n\tdouble l=::r,r=20000;\n\twhile(r-l>1e-9){\n\t\tdouble mid=(l+r)/2;\n\t\tif(in(O+dir*mid))\n\t\t\tl=mid;\n\t\telse r=mid;\n\t}\n\treturn O+dir*l;\n}\ndouble calc(P a,P b,P c){\n\t\n\tL l1,l2;\n\tl1.a=(a+b)/2;\n\tl1.b=l1.a+(b-a).turn90();\n\t\n\tl2.a=(b+c)/2;\n\tl2.b=l2.a+(b-c).turn90();\n\t\n\t\n\t\n\t\n\tif(parallel(l1,l2))\n\t\treturn dis(a,c);\n\t\n\tP O=intersect(l1,l2);\n\tdouble r=dis(O,a);\n\t\n\t\n\tdouble theta;\n\ttheta = acos(((c-O)^(a-O))/r/r);\n\t\n\treturn r*theta;\t\n}\ndouble calc(double l,double r){\n\t\n\tdouble p=0.45;\n\n\tdouble m=l+(r-l)*p;\n\t\n\tP pl=at(O,P(1,0).rot(l));\n\tP pm=at(O,P(1,0).rot(m));\n\tP pr=at(O,P(1,0).rot(r));\n\t\n\treturn calc(pl,pm,pr);\n\t\n}\ndouble simpl(double l,double r){\n\tdouble mid=(l+r)/2;\n\t\n\t\n\tdouble Sl=calc(l,mid);\n\tdouble Sr=calc(mid,r);\n\tdouble S=calc(l,r);\n\t\n\tif(sgn(S-Sl-Sr)==0)\n\t\treturn S;\n\treturn simpl(l,mid)+simpl(mid,r);\n}\n\nint main(){\n\twhile(scanf(\"%d%lf\",&n,&r)&&n){\n\t\tvecO.clear();\n\t\tfor(int i=1;i<=n;i++){\n\t\t\tc[i].read();\n\t\t}\n\t\t\n\t\tsort(c+1,c+1+n);\n\t\t\n\t\tint m=0;\n\t\tstatic C nC[maxn];\n\t\tnC[++m]=c[1];\n\t\tfor(int i=2;i<=n;i++){\n\t\t\tint flag=0;\n\t\t\tfor(int j=1;j<=m;j++){\n\t\t\t\tdouble d=dis(c[i].o,nC[j].o);\n\t\t\t\tif(sgn(d-(nC[j].r-c[i].r))<=0){\n\t\t\t\t\tflag=1;\n\t\t\t\t\tbreak;\n\t\t\t\t}\n\t\t\t}\n\t\t\tif(!flag){\n\t\t\t\tnC[++m]=c[i];\n\t\t\t}\n\t\t}\n\t\tn=m;\n\t\tfor(int i=1;i<=n;i++)\n\t\t\tc[i]=nC[i];\n\t\t\t\n\t\tif(n==1){\n\t\t\n\t\t\tif(sgn(c[1].r-r)<=0){\n\t\t\t\tprintf(\"%.8f\\n\",2*pi*(r*2-c[1].r));\n\t\t\t}else{\n\t\t\t\tputs(\"0.0\");\n\t\t\t}\n\t\t\tcontinue;\n\t\t}\n\t\t\n\t\tint flag=0;\n\t\tfor(int i=1;i<=n;i++)\n\t\tfor(int j=i+1;j<=n;j++){\n\t\t\tC c1=c[i],c2=c[j];\n\t\t\tdouble d=dis(c[i].o,c[j].o);\n\t\t\tc1.r=r-c1.r;\n\t\t\tc2.r=r-c2.r;\n\t\t\tP p1,p2;\n\t\t\tif(c1==c2||!intersect(c1,c2,p1,p2))\n\t\t\t\tcontinue;\n\t\t\t\n\t\t\tif(sgn(calcR(p1)-r)<=0){\n\t\t\t\tvecO.push_back(p1);\n\t\t\t}\n\t\t\tif(sgn(calcR(p2)-r)<=0){\n\t\t\t\tvecO.push_back(p2);\n\t\t\t}\n\t\t}\n\t\t\n\t\tif(vecO.empty()){\n\t\t\tputs(\"0.0\");\n\t\t\tcontinue;\n\t\t}\n\t\t//cerr<<n<<endl;\n\t\tO=vecO.front();\n\t\t\n\t\tdouble ans=0;\n\t\tans+=simpl(0,pi);\n\t\tans+=simpl(pi,pi*2);\n\t\tprintf(\"%.8f\\n\",ans);\n\t\t\n\t}\n\treturn 0;\n}", "accuracy": 1, "time_ms": 1550, "memory_kb": 3500, "score_of_the_acc": -1.9162, "final_rank": 8 }, { "submission_id": "aoj_1323_1592121", "code_snippet": "#include<stdio.h>\n#include<algorithm>\n#include<math.h>\n#include<vector>\nusing namespace std;\nconst double EPS = 1e-10;\nconst double INF = 1e+10;\nconst double PI = acos(-1);\nint sig(double r) { return (r < -EPS) ? -1 : (r > +EPS) ? +1 : 0; }\ninline double ABS(double a){return max(a,-a);}\nstruct Pt {\n\tdouble x, y;\n\tPt() {}\n\tPt(double x, double y) : x(x), y(y) {}\n\tPt operator+(const Pt &a) const { return Pt(x + a.x, y + a.y); }\n\tPt operator-(const Pt &a) const { return Pt(x - a.x, y - a.y); }\n\tPt operator*(const Pt &a) const { return Pt(x * a.x - y * a.y, x * a.y + y * a.x); }\n\tPt operator-() const { return Pt(-x, -y); }\n\tPt operator*(const double &k) const { return Pt(x * k, y * k); }\n\tPt operator/(const double &k) const { return Pt(x / k, y / k); }\n\tdouble ABS() const { return sqrt(x * x + y * y); }\n\tdouble abs2() const { return x * x + y * y; }\n\tdouble arg() const { return atan2(y, x); }\n\tdouble dot(const Pt &a) const { return x * a.x + y * a.y; }\n\tdouble det(const Pt &a) const { return x * a.y - y * a.x; }\n};\ndouble tri(const Pt &a, const Pt &b, const Pt &c) { return (b - a).det(c - a); }\npair<Pt,Pt> pCC(Pt a, double r, Pt b, double s) {\n\tdouble d = (b - a).ABS();\n\tdouble x = (d * d + r * r - s * s) / (d * 2);\n\tPt e = (b - a) / d, w = e * Pt(0, 1) * sqrt(max(r * r - x * x, 0.0));\n\treturn make_pair(a + e * x - w, a + e * x + w);\n}\nint iCC(Pt a, double r, Pt b, double s) {\n\tdouble d = (b - a).ABS();\n\tif (sig(d) == 0 && sig(r - s) == 0) return -1; // correspond\n\tif (sig(r - s - d) > 0) return +2; // r > s\n\tif (sig(s - r - d) > 0) return -2; // s > r\n\treturn (sig(r + s - d) >= 0) ? 1 : 0;\n}\nPt c[110];\ndouble r[110];\nvector<Pt> p[110];\nvector<pair<int,int> > tp;\nint v[11100];\ndouble ad[110][110];\nint main(){\n\tint a;\n\tdouble b;\n\twhile(scanf(\"%d%lf\",&a,&b),a){\n\t\tfor(int i=0;i<a;i++){\n\t\t\tdouble x,y;\n\t\t\tscanf(\"%lf%lf%lf\",&x,&y,r+i);\n\t\t\tc[i]=Pt(x,y);\n\t\t}\n\t\tfor(int i=0;i<11100;i++)v[i]=0;\n\t\tfor(int i=0;i<a;i++)p[i].clear();\n\t\ttp.clear();\n\t\tbool dame=false;\n\t\tfor(int i=0;i<a;i++)if(r[i]>b+EPS)dame=true;\n\t\tfor(int i=0;i<a;i++){\n\t\t\tfor(int j=i+1;j<a;j++){\n\t\t\t\tif(iCC(c[i],b-r[i],c[j],b-r[j])==0)dame=true;\n\t\t\t}\n\t\t}\n\t\tif(dame){printf(\"0.0\\n\");continue;}\n\t\tfor(int i=0;i<a;i++)for(int j=0;j<a;j++){\n\t\t\tif(iCC(c[i],b-r[i],c[j],b-r[j])==1){\n\t\t\t\tpair<Pt,Pt> t=pCC(c[i],b-r[i],c[j],b-r[j]);\n\t\t\t\tp[i].push_back(t.first);\n\t\t\t\tp[i].push_back(t.second);\n\t\t\t}\n\t\t}\n\t\tfor(int i=0;i<a;i++){\n\t\t\tint n=p[i].size();\n\t\t\tif(n==0){\n\t\t\t\tp[i].push_back(c[i]+Pt(cos(1),sin(1)));\n\t\t\t\tp[i].push_back(c[i]+Pt(cos(1),sin(1)));\n\t\t\t}else{\n\t\t\t\tfor(int j=0;j<n;j++){\n\t\t\t\t\tfor(int k=0;k<n-1;k++){\n\t\t\t\t\t\tif((p[i][k]-c[i]).arg()>(p[i][k+1]-c[i]).arg()){\n\t\t\t\t\t\t\tswap(p[i][k],p[i][k+1]);\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\tp[i].push_back(p[i][0]);\n\t\t\t}\n\t\t}\n\t\tdouble ret=0;\n\t\tfor(int i=0;i<a;i++)for(int j=0;j<p[i].size()-1;j++){\n\t\t\tif(p[i].size()>2&&(p[i][j]-p[i][j+1]).ABS()<EPS)continue;\n\t\t\tdouble th=((p[i][j]-c[i]).arg()+(p[i][j+1]-c[i]).arg()+((j==p[i].size()-2)?PI*2:0))/2;\n\t\t\tPt t=c[i]+Pt(cos(th),sin(th))*(b-r[i]);\n\t\t\tbool ok=true;\n\t\t\tfor(int k=0;k<a;k++){\n\t\t\t\tif((t-c[k]).ABS()>b-r[k]+EPS){ok=false;break;}\n\t\t\t}\n\t\t\tif(ok){\n\t\t\t//\tprintf(\"%f %f %f %f\\n\",p[i][j].x,p[i][j].y,p[i][j+1].x,p[i][j+1].y);\n\t\t\t\tret+=(b*2-r[i])*(((j==p[i].size()-2)?PI*2:0)+(p[i][j+1]-c[i]).arg()-(p[i][j]-c[i]).arg());\n\t\t\t\ttp.push_back(make_pair(i,j));\n\t\t\t}\n\t\t}\n\t\tint sz=tp.size();\n\t\tint at=0;\n\t\tint prev=-1;\n\t\tfor(int i=0;i<a;i++)for(int j=0;j<a;j++){\n\t\t\tif(iCC(c[i],b-r[i],c[j],b-r[j])==1){\n\t\t\t\tpair<Pt,Pt> t=pCC(c[i],b-r[i],c[j],b-r[j]);\n\t\t\t\tdouble th=(t.first-c[j]).arg()-(t.first-c[i]).arg();\n\t\t\t\tif(th<0)th+=2*PI;\n\t\t\t\tif(th>PI)th=2*PI-th;\n\t\t\t\tad[i][j]=th*b;\n//\t\t\t\tprintf(\"%d %d: %f\\n\",i,j,ad[i][j]);\n\t\t\t}\n\t\t}\n\t\tfor(int i=0;i<sz;i++){\n\t\t\tfor(int j=0;j<sz;j++){\n\t\t\t\tif(at==j||(prev==j&&sz>2))continue;\n\t\t\t\tbool ok=false;\n\t\t\t\tif((p[tp[at].first][tp[at].second]-p[tp[j].first][tp[j].second]).ABS()<EPS)ok=true;\n\t\t\t\tif((p[tp[at].first][tp[at].second+1]-p[tp[j].first][tp[j].second]).ABS()<EPS)ok=true;\n\t\t\t\tif((p[tp[at].first][tp[at].second]-p[tp[j].first][tp[j].second+1]).ABS()<EPS)ok=true;\n\t\t\t\tif((p[tp[at].first][tp[at].second+1]-p[tp[j].first][tp[j].second+1]).ABS()<EPS)ok=true;\n\t\t\t\tif(ok){\n\t\t\t\t\tret+=ad[tp[at].first][tp[j].first];\n\t\t\t\t//\tprintf(\"%d %d\\n\",tp[at].first,tp[j].first);\n\t\t\t//\t\tprintf(\"%f\\n\",ret);\n\t\t\t\t\tprev=at;at=j;break;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\tprintf(\"%.12f\\n\",ret);\n\t}\n}", "accuracy": 1, "time_ms": 340, "memory_kb": 1668, "score_of_the_acc": -0.6509, "final_rank": 4 }, { "submission_id": "aoj_1323_1568438", "code_snippet": "#include<cstdio>\n#include<vector>\n#include<cmath>\n#include<complex>\n#include<algorithm>\n\nusing namespace std;\n\ntypedef double Real;\ntypedef complex<Real> Point;\n\nconst Real PI=acos(-1.0);\nconst Real eps=1e-8;\n\ntemplate<class T> bool eq(T a, T b){\n\treturn abs(a-b)<eps;\n}\n\ntemplate<class T> int sgn(T a){\n\tif(eq(a,(T)0)) return 0;\n\tif(a>0) return 1;\n\treturn -1;\n}\n\nstruct Circle:vector<Real> {\n\tPoint c;\n\tReal r;\n\tCircle(){}\n\tCircle(Point c,Real r):c(c),r(r){}\n};\n\nReal R;\nCircle cs[110];\nint N;\n\nPoint getPolar(Point c,Point r,Real ang){\n\treturn c+r*Point(cos(ang),sin(ang));\n}\n\nReal normalize(Real x){\n\twhile(x<-PI) x+=PI*2;\n\twhile(x>PI) x-=PI*2;\n\treturn x;\n}\n\nReal arg(Point p){\n\treturn atan2(p.imag(),p.real());\n}\n\nvoid iCC(Circle &c,Circle &c2){\n\tif(eq(c.c,c2.c)) return;\n\tReal d=abs(c.c-c2.c);\n\tReal r=c.r,r2=c2.r;\n\tif(sgn(d-(r+r2))>0) return;\n\telse if(sgn(d-(r+r2))==0){//out tan\n\t\tReal ang=normalize(arg(c2.c-c.c));\n\t\tc.push_back(ang);\n\t}else if(sgn(d-abs(r-r2))>0){//inter two points\n\t\tReal ang=arg(c2.c-c.c);\n\t\tReal ang2=acos((r*r+d*d-r2*r2)/(2*r*d));\n\t\tc.push_back(normalize(ang+ang2));\n\t\tc.push_back(normalize(ang-ang2));\n\t}else if(sgn(d-abs(r-r2))==0){//in tan\n\t\tif(r>r2){\n\t\t\tc.push_back(normalize(arg(c2.c-c.c)));\n\t\t}else{\n\t\t\tc.push_back(normalize(-arg(c2.c-c.c)));\n\t\t}\n\t}\n\telse{//contained\n\t\treturn;\n\t}\n}\n\nbool changeCircles(){\n\tfor(int i=0;i<N;i++){\n\t\tcs[i].r=R-cs[i].r;\n\t\tif(cs[i].r<eps) return false;\n\t}\n\treturn true;\n}\n\nvoid getAllPoints(){\n\tfor(int i=0;i<N;i++){\n\t\tfor(int j=0;j<N;j++){\n\t\t\tif(i==j) continue;\n\t\t\tiCC(cs[i],cs[j]);\n\t\t}\n\t}\n\tfor(int i=0;i<N;i++){\n\t\tcs[i].push_back(0);\n\t\tcs[i].push_back(PI);\n\t}\n}\n\nbool checkValidity(Point p){\n\tfor(int i=0;i<N;i++){\n\t\tReal d=abs(p-cs[i].c);\n\t\tif(sgn(d-cs[i].r)>0) return false;\n\t}\n\treturn true;\n}\n\nReal calcLength(){\n\tReal len=0,ang_sum=0;\n\tfor(int i=0;i<N;i++){\n\t\tsort(cs[i].begin(),cs[i].end());\n\t\tfor(int j=0;j<cs[i].size();j++){\n\t\t\tReal ang1=cs[i][j];\n\t\t\tReal ang2=j+1==cs[i].size()?cs[i][0]+PI*2:cs[i][j+1];\n\t\t\tReal ang=(ang1+ang2)/2;\n\t\t\tPoint p=getPolar(cs[i].c,cs[i].r,ang);\n\t\t\tbool flg=checkValidity(p);\n\t\t\tif(flg){\n\t\t\t\tlen+=(cs[i].r+R)*(ang2-ang1);\n\t\t\t\tang_sum+=(ang2-ang1);\n\t\t\t}\n\t\t}\n\t}\n\tif(ang_sum<eps) return 0;\n\tReal len2=R*(PI*2-ang_sum);\n\tReal res=len+len2;\n\treturn res;\n}\n\nvoid solve(){\n\tbool flg=changeCircles();\n\tif(flg==false){\n\t\tprintf(\"0.0\\n\");\n\t\treturn;\n\t}\n\tgetAllPoints();\n\tReal ans=calcLength();\n\tprintf(\"%f\\n\",ans);\n}\n\nvoid input(){\n\tint r;\n\tscanf(\"%d%d\",&N,&r);\n\tif(N==0) exit(0);\n\tR=r;\n\tfor(int i=0;i<N;i++){\n\t\tint x,y,r;\n\t\tscanf(\"%d%d%d\",&x,&y,&r);\n\t\tcs[i].clear();\n\t\tcs[i]=Circle(Point(x,y),r);\n\t}\n}\n\nint main(){\n\twhile(true){\n\t\tinput();\n\t\tsolve();\n\t}\n\treturn 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 1392, "score_of_the_acc": -0.3644, "final_rank": 2 }, { "submission_id": "aoj_1323_1131796", "code_snippet": "#include<cstdio>\n#include<cmath>\n#include<complex>\n#include<vector>\n#include<algorithm>\n\nusing namespace std;\n\nconst double eps=1e-9;\nconst double PI=3.14159265358979323846264;\n\ntypedef complex<double> Point;\ntypedef complex<double> Vector;\n\ntemplate<class T> bool eq(T a,T b){\n\treturn abs(a-b)<eps;\n}\n\ndouble normalize(double ang){\n\twhile(ang<0) ang+=PI*2;\n\twhile(ang>PI*2) ang-=PI*2;\n\treturn ang;\n}\n\nstruct Circle{\n\tPoint center;\n\tdouble r;\n\tvector<double> angles;//intersections\n\tCircle(){}\n\tCircle(Point p,double r):center(p),r(r){}\n\tbool isin(Point p){\n\t\tdouble dis=abs(p-center);\n\t\treturn r>dis||eq(r,dis);\n\t}\n\tbool isin(Circle c){\n\t\tdouble d=abs(c.center-center)+c.r;\n\t\treturn r>d;\n\t}\n\tPoint getPoint(double theta){\n\t\treturn center+(Point(cos(theta),sin(theta))*r);\n\t}\n\tPoint onePoint(Circle c){\n\t\tdouble dis=abs(c.center-center);\n\t\tif(!eq(dis,r+c.r)){\n\t\t\treturn Point(NAN,NAN);\n\t\t}\n\t\tVector vec=c.center-center;\n\t\treturn center+vec*(r/(r+c.r));\n\t}\n\tvoid add(Circle c){\n\t\tdouble r2=c.r;\n\t\tdouble d=abs(c.center-center);\n\t\tif(r+r2<d) return;\n\t\tdouble theta=acos((r*r+d*d-r2*r2)/(2.0*r*d));\n\t\tVector vec=c.center-center;\n\t\tdouble phi=atan2(vec.imag(),vec.real());\n\t\tangles.push_back(normalize(phi+theta));\n\t\tangles.push_back(normalize(phi-theta));\n\t}\n\tbool haveCommon(Circle c){\n\t\tdouble d=abs(center-c.center);\n\t\treturn d<r+c.r||eq(d,r+c.r);\n\t}\n};\n\nvector<Circle> C;\nvector<Circle> centerArea;\nvector<Circle> circles;\nint N,R;\n\nvoid solve(){\n\tfor(int i=0;i<N;i++){\n\t\tif(C[i].r>R){\n\t\t\tprintf(\"0.0\\n\");\n\t\t\treturn;\n\t\t}\n\t}\n\tfor(int i=0;i<N;i++){\n\t\tif(C[i].r==R){\n\t\t\tbool flg=true;\n\t\t\tfor(int j=0;j<N;j++){\n\t\t\t\tif(i==j) continue;\n\t\t\t\tif(C[i].isin(C[j])==false) flg=false;\n\t\t\t}\n\t\t\tif(flg){\n\t\t\t\tprintf(\"%.f\\n\",PI*2*R);\n\t\t\t}else{\n\t\t\t\tprintf(\"0.0\\n\");\n\t\t\t}\n\t\t\treturn;\n\t\t}\n\t}\n\tfor(int i=0;i<N;i++){\n\t\tcenterArea.push_back(Circle(C[i].center,R-C[i].r));\n\t}\n\tfor(int i=0;i<N;i++){\n\t\tbool flg=true;\n\t\tfor(int j=0;j<N;j++){\n\t\t\tif(i==j) continue;\n\t\t\tif(centerArea[i].isin(centerArea[j])){\n\t\t\t\tflg=false;\n\t\t\t\tbreak;\n\t\t\t}\n\t\t}\n\t\tif(flg) circles.push_back(centerArea[i]);\n\t}\n\tif(circles.size()==1){\n\t\tprintf(\"%f\\n\",PI*2*(circles[0].r+R));\n\t\treturn;\n\t}\n\tint M=circles.size();\n\tfor(int i=0;i<M;i++) for(int j=i+1;j<M;j++){\n\t\tPoint pt=circles[i].onePoint(circles[j]);\n\t\tif(isnan(pt.real())) continue;\n\t\tbool flg=true;\n\t\tfor(int k=0;k<M;k++){\n\t\t\tif(i==k||j==k) continue;\n\t\t\tif(circles[k].isin(pt)==false) flg=false;\n\t\t}\n\t\tif(flg){\n\t\t\tprintf(\"%f\\n\",PI*2*R);\n\t\t}else{\n\t\t\tprintf(\"0.0\\n\");\n\t\t}\n\t\treturn;\n\t}\n\tfor(int i=0;i<M;i++) for(int j=0;j<M;j++){\n\t\tif(circles[i].haveCommon(circles[j])==false){\n\t\t\tprintf(\"0.0\\n\");\n\t\t\treturn;\n\t\t}\n\t}\n\tfor(int i=0;i<M;i++) for(int j=0;j<M;j++){\n\t\tif(i==j) continue;\n\t\tcircles[i].add(circles[j]);\n\t}\n\tdouble totalAng=0;\n\tdouble ans=0;\n\tfor(int i=0;i<M;i++){\n\t\tCircle cur=circles[i];\n\t\tsort(cur.angles.begin(),cur.angles.end());\n\t\tfor(int j=0;j<cur.angles.size();j++){\n\t\t\tdouble a=cur.angles[j];\n\t\t\tdouble b;\n\t\t\tif(j+1==cur.angles.size()){\n\t\t\t\tb=cur.angles[0]+PI*2;\n\t\t\t}else{\n\t\t\t\tb=cur.angles[j+1];\n\t\t\t}\n\t\t\tdouble mid=(a+b)/2;\n\t\t\tPoint pt=cur.getPoint(mid);\n\t\t\tbool flg=true;\n\t\t\tfor(int k=0;k<M;k++){\n\t\t\t\tif(circles[k].isin(pt)==false) flg=false;\n\t\t\t}\n\t\t\tif(flg){\n\t\t\t\tdouble curAng=b-a;\n\t\t\t\tans+=(curAng*(R+cur.r));\n\t\t\t\ttotalAng+=curAng;\n\t\t\t}\n\t\t}\n\t}\n\tif(totalAng==0){\n\t\tprintf(\"0.0\\n\");\n\t}else{\n\t\tans+=(PI*2-totalAng)*R;\n\t\tprintf(\"%f\\n\",ans);\n\t}\n}\n\nvoid init(){\n\tC.clear();\n\tcenterArea.clear();\n\tcircles.clear();\n}\n\nint main(){\n\twhile(true){\n\t\tscanf(\"%d%d\",&N,&R);\n\t\tif(N==0&&R==0) break;\n\t\tinit();\n\t\tfor(int i=0;i<N;i++){\n\t\t\tint x,y,r;\n\t\t\tscanf(\"%d%d%d\",&x,&y,&r);\n\t\t\tC.push_back(Circle(Point(x,y),r));\n\t\t}\n\t\tsolve();\n\t}\n\treturn 0;\n}", "accuracy": 1, "time_ms": 50, "memory_kb": 1372, "score_of_the_acc": -0.3851, "final_rank": 3 }, { "submission_id": "aoj_1323_390973", "code_snippet": "#include <iostream>\n#include <vector>\n#include <cmath>\n#include <algorithm>\n#include <cstdio>\n#include <cassert>\nusing namespace std;\n\nconst double EPS=1e-8;\n\nstruct pt {\n\tdouble x,y;\n\tpt() {}\n\tpt(double x_, double y_) : x(x_), y(y_) {}\n\tpt operator + (const pt& p) const { return pt(x+p.x,y+p.y); }\n\tpt operator - (const pt& p) const { return pt(x-p.x,y-p.y); }\n\tdouble mod2() const { return x*x+y*y; }\n\tdouble mod() const { return sqrt(mod2()); }\n\tdouble arg() const { return atan2(y,x); }\n\tstatic pt polar(double r, double theta) { return pt(r*cos(theta),r*sin(theta)); }\n\tbool operator == (const pt& p) const { return x==p.x && y==p.y; }\n\tbool operator < (const pt& p) const { return false; }\n};\n\nistream& operator >> (istream& i, pt& p) { return i >> p.x >> p.y; }\n\ntypedef vector<pt> VP;\ntypedef vector<int> VI;\ntypedef vector<double> VD;\ntypedef pair<double,pt> PDP;\ntypedef vector<PDP> VC;\n\ninline double sq(double x) { return x*x; }\n\nint main() {\n\tint n,R;\n\twhile(cin >> n >> R && n) {\n\t\tdouble o=0;\n\t\tdouble ret=0;\n\t\tdouble rem=2*M_PI;\n\t\tbool exists=false;\n\t\tVC cc(n);\n\t\tfor (int i=0;i<n;++i) {\n\t\t\tcin >> cc[i].second >> cc[i].first;\n\t\t\tcc[i].first=R-cc[i].first;\n\t\t}\n\t\tsort(cc.begin(),cc.end());\n\t\tVP c;\n\t\tVD r;\n\t\tfor (int i=0;i<n;++i) {\n\t\t\tif (cc[i].first<0) goto res;\n\t\t\tfor (int j=0;j<i;++j) {\n\t\t\t\tif ((cc[i].second-cc[j].second).mod2()<=sq(cc[i].first-cc[j].first)) goto redundant;\n\t\t\t}\n\t\t\tr.push_back(cc[i].first);\n\t\t\tc.push_back(cc[i].second);\n\t\t\tredundant:;\n\t\t}\n\t\tn=c.size();\n\t\tfor (int i=0;i<n;++i) {\n\t\t\tVD alpha;\n\t\t\talpha.push_back(0);\n\t\t\talpha.push_back(2*M_PI);\n\t\t\tfor (int j=0;j<n;++j) {\n\t\t\t\tif (j==i) continue;\n\t\t\t\tpt v=c[j]-c[i];\n\t\t\t\tif (v.mod2()>sq(r[i]+r[j])) goto res;\n\t\t\t\tif (v.mod2()<sq(r[i]-r[j])) continue;\n\t\t\t\tdouble theta=v.arg();\n//\t\t\t\tcerr << (v.mod2()+sq(r[i])-sq(r[j])) << \" / \" << (2*v.mod()*r[i]) << endl;\n\t\t\t\tdouble co=(v.mod2()+sq(r[i])-sq(r[j]))/(2*v.mod()*r[i]);\n\t\t\t\t//assert(co>=-1);\n\t\t\t\t//assert(co<=1);\n\t\t\t\tif (co<-1) co=-1;\n\t\t\t\tif (co>1) co=1;\n\t\t\t\tdouble phi=acos(co);\n//\t\t\t\tcerr << i << \" intersects with \" << j << \" at \" << theta-phi << \" and \" << theta+phi << endl;\n\t\t\t\talpha.push_back(theta+phi-2*M_PI);\n\t\t\t\talpha.push_back(theta+phi);\n\t\t\t\talpha.push_back(theta+phi+2*M_PI);\n\t\t\t\talpha.push_back(theta-phi-2*M_PI);\n\t\t\t\talpha.push_back(theta-phi);\n\t\t\t\talpha.push_back(theta-phi+2*M_PI);\n\t\t\t}\n\t\t\tsort(alpha.begin(),alpha.end());\n\t\t\tfor(int k=0;alpha[k]<2*M_PI;++k) {\n\t\t\t\tif (alpha[k]<0) continue;\n\t\t\t\tdouble delta=alpha[k+1]-alpha[k];\n\t\t\t\tdouble beta=(alpha[k]+alpha[k+1])/2;\n\t\t\t\tpt mid=c[i]+pt::polar(r[i],beta);\n\t\t\t\tfor (int j=0;j<n;++j) {\n\t\t\t\t\tif ((mid-c[j]).mod()-EPS>r[j]) goto skip;\n\t\t\t\t}\n//\t\t\t\tcerr << \"added an interval \" << alpha[k] << \" -- \" << alpha[k+1] << \" at \" << i << \" w/radius \" << r[i]+R << endl;\n\t\t\t\texists=true;\n\t\t\t\tret+=(r[i]+R)*delta;\n\t\t\t\trem-=delta;\n\t\t\t\tskip:;\n\t\t\t}\n\t\t}\n\t\tif (exists) o=ret+R*rem;\n\t\tres:\n\t\tif (o) printf(\"%.3f\\n\",o);\n\t\telse printf(\"0.0\\n\");\n\t}\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 0, "score_of_the_acc": 0, "final_rank": 1 } ]
aoj_1319_cpp
Problem E: Driving an Icosahedral Rover After decades of fruitless efforts, one of the expedition teams of ITO (Intersolar Tourism Organization) finally found a planet that would surely provide one of the best tourist attractions within a ten light-year radius from our solar system. The most attractive feature of the planet, besides its comfortable gravity and calm weather, is the area called Mare Triangularis . Despite the name, the area is not covered with water but is a great plane. Its unique feature is that it is divided into equilateral triangular sections of the same size, called trigons . The trigons provide a unique impressive landscape, a must for tourism. It is no wonder the board of ITO decided to invest a vast amount on the planet. Despite the expected secrecy of the staff, the Society of Astrogeology caught this information in no time, as always. They immediately sent their president's letter to the Institute of Science and Education of the Commonwealth Galactica claiming that authoritative academic inspections were to be completed before any commercial exploitation might damage the nature. Fortunately, astrogeologists do not plan to practice all the possible inspections on all of the trigons ; there are far too many of them. Inspections are planned only on some characteristic trigons and, for each of them, in one of twenty different scientific aspects. To accelerate building this new tourist resort, ITO's construction machinery team has already succeeded in putting their brand-new invention in practical use. It is a rover vehicle of the shape of an icosahedron , a regular polyhedron with twenty faces of equilateral triangles. The machine is customized so that each of the twenty faces exactly fits each of the trigons . Controlling the high-tech gyromotor installed inside its body, the rover can roll onto one of the three trigons neighboring the one its bottom is on. Figure E.1: The Rover on Mare Triangularis Each of the twenty faces has its own function. The set of equipments installed on the bottom face touching the ground can be applied to the trigon it is on. Of course, the rover was meant to accelerate construction of the luxury hotels to host rich interstellar travelers, but, changing the installed equipment sets, it can also be used to accelerate academic inspections. You are the driver of this rover and are asked to move the vehicle onto the trigon specified by the leader of the scientific commission with the smallest possible steps. What makes your task more difficult is that the designated face installed with the appropriate set of equipments has to be the bottom. The direction of the rover does not matter. Figure E.2:The Coordinate System Figure E.3: Face Numbering The trigons of Mare Triangularis are given two-dimensional coordinates as shown in Figure E.2. Like maps used for the Earth, the x axis is from the west to the east, and the y axis is from the south to the north. Note that all the trigons with its coordinates ...(truncated)
[ { "submission_id": "aoj_1319_10851601", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\n\nconst int nt[20][3]={\n{5,1,4},\n{6,2,0},\n{7,3,1},\n{8,4,2},\n{9,0,3},\n{0,10,11},\n{1,11,12},\n{2,12,13},\n{3,13,14},\n{4,14,10},\n{15,5,9},\n{16,6,5},\n{17,7,6},\n{18,8,7},\n{19,9,8},\n{10,19,16},\n{11,15,17},\n{12,16,18},\n{13,17,19},\n{14,18,15}};\nconst int tx[2][3]={{0,1,-1},{0,-1,1}},ty[2][3]={{1,0,0},{-1,0,0}};\nmap<pair<pair<int,int>,pair<int,int> >,int> mp;\nqueue<pair<pair<int,int>,pair<int,int> > > q;\npair<pair<int,int>,pair<int,int> > t,tt;\n\nint main()\n{\n\tint x,y,k,p,tp,xx,yy,kk,pp,s,i;\n\tt=make_pair(make_pair(0,0),make_pair(0,0));\n\tmp[t]=0;\n\tq.push(t);\n\twhile(!q.empty())\n\t{\n\t\tt=q.front();\n\t\tq.pop();\n\t\tx=t.first.first;y=t.first.second;\n\t\tk=t.second.first;p=t.second.second;\n\t\ttp=(x+y)&1;\n\t\tfor(i=0;i<3;i++)\n\t\t{\n\t\t\txx=x+tx[tp][i];yy=y+ty[tp][i];\n\t\t\tkk=nt[k][(i+p)%3];\n\t\t\tfor(pp=0;pp<3;pp++)\n\t\t\t\tif(nt[kk][pp]==k)\n\t\t\t\t\tbreak;\n\t\t\tpp=(pp+3-i)%3;\n\t\t\tif(!mp.count(tt=make_pair(make_pair(xx,yy),make_pair(kk,pp)))||mp[t]+1<mp[tt])\n\t\t\t\tif((mp[tt]=mp[t]+1)<100)\n\t\t\t\t\tq.push(tt);\n\t\t}\n\t}\n\twhile(~scanf(\"%d%d%d\",&x,&y,&k)&&(x||y||k))\n\t{\n\t\ts=1<<30;\n\t\tfor(i=0;i<3;i++)\n\t\t\tif(mp.count(t=make_pair(make_pair(x,y),make_pair(k,i))))\n\t\t\t\ts=min(s,mp[t]);\n\t\tprintf(\"%d\\n\",s);\n\t}\n\treturn 0;\n}", "accuracy": 1, "time_ms": 850, "memory_kb": 59832, "score_of_the_acc": -0.7079, "final_rank": 15 }, { "submission_id": "aoj_1319_4865290", "code_snippet": "#include<iostream>\n#include<cstdio>\n#include<cstring>\n#include<string>\n#include<vector>\n#include<cmath>\n#include<algorithm>\n#include<map>\n#include<queue>\n#include<deque>\n#include<iomanip>\n#include<tuple>\n#include<cassert>\n#include<set>\nusing namespace std;\ntypedef long long int LL;\ntypedef pair<int,int> P;\ntypedef pair<LL,int> LP;\nconst int INF=1<<30;\nconst LL MAX=1e9+7;\n\nvoid array_show(int *array,int array_n,char middle=' '){\n for(int i=0;i<array_n;i++)printf(\"%d%c\",array[i],(i!=array_n-1?middle:'\\n'));\n}\nvoid array_show(LL *array,int array_n,char middle=' '){\n for(int i=0;i<array_n;i++)printf(\"%lld%c\",array[i],(i!=array_n-1?middle:'\\n'));\n}\nvoid array_show(vector<int> &vec_s,int vec_n=-1,char middle=' '){\n if(vec_n==-1)vec_n=vec_s.size();\n for(int i=0;i<vec_n;i++)printf(\"%d%c\",vec_s[i],(i!=vec_n-1?middle:'\\n'));\n}\nvoid array_show(vector<LL> &vec_s,int vec_n=-1,char middle=' '){\n if(vec_n==-1)vec_n=vec_s.size();\n for(int i=0;i<vec_n;i++)printf(\"%lld%c\",vec_s[i],(i!=vec_n-1?middle:'\\n'));\n}\n\nint next_num(int cur,int to){\n to%=3;\n if(to==0){\n if(cur%10<5)return cur+5;\n return cur-5;\n }\n if(cur<5)to=3-to;\n if(cur<5 || cur>=15){\n if(to==1){\n if(cur%5==0)cur+=5;\n return cur-1;\n }\n cur++;\n if(cur%5==0)cur-=5;\n return cur;\n }\n if(cur<10){\n if(to==1)return cur+5;\n cur+=6;\n if(cur==15)cur=10;\n return cur;\n }\n if(to==1)return cur-5;\n cur-=6;\n if(cur==4)cur=9;\n return cur;\n}\n\nP next_pos(P pos,int to){\n to%=3;\n int a=(to-1)*2-1;\n if((pos.first+pos.second+(1<<20))%2){\n if(to==0)return make_pair(pos.first,pos.second-1);\n return make_pair(pos.first+a,pos.second);\n }else{\n if(to==0)return make_pair(pos.first,pos.second+1);\n return make_pair(pos.first-a,pos.second);\n }\n}\n\nint dis[300][300][30][5];\nconst int N=150;\nqueue<tuple<P,int,P,int,int>> q1;//pos,num,bef_pos,bef_num,dis\n\nint main(){\n int n,m;\n int i,j,k;\n int a,b,c;\n int s;\n int num;\n P pos,bef_pos,pos2;\n memset(dis,-1,sizeof(dis));\n q1.push(make_tuple(P(N,N),0,P(N,N+1),5,0));\n while(!q1.empty()){\n tie(pos,a,bef_pos,b,c)=q1.front();q1.pop();\n if(c>100)continue;\n for(i=0;i<3;i++){\n if(bef_pos==next_pos(pos,i))break;\n }\n for(j=0;j<3;j++){\n if(b==next_num(a,j))break;\n }\n b=(j-i+3)%3;\n if(dis[pos.first][pos.second][a][b]!=-1)continue;\n dis[pos.first][pos.second][a][b]=c;\n for(i=0;i<3;i++){\n pos2=next_pos(pos,i);\n num=next_num(a,i+b);\n q1.push(make_tuple(pos2,num,pos,a,c+1));\n }\n }\n while(cin>>a>>b>>c){\n if(a==0 && b==0 && c==0)break;\n a+=N,b+=N;\n s=1e3;\n for(i=0;i<3;i++){\n if(dis[a][b][c][i]!=-1)s=min(s,dis[a][b][c][i]);\n }\n cout<<s<<endl;\n }\n}", "accuracy": 1, "time_ms": 90, "memory_kb": 57048, "score_of_the_acc": -0.5139, "final_rank": 11 }, { "submission_id": "aoj_1319_4779930", "code_snippet": "#include<bits/stdc++.h>\ntypedef long long int ll;\ntypedef unsigned long long int ull;\n#define BIG_NUM 2000000000\n#define HUGE_NUM 1000000000000000000\n#define MOD 1000000007\n#define EPS 0.000000001\nusing namespace std;\n\n\n\n#define NUM 20\n\nenum Type{\n\tUP,\n\tR_UP,\n\tR_DOWN,\n\tDOWN,\n\tL_DOWN,\n\tL_UP,\n};\n\nstruct Info{\n\tvoid set(int arg_next_center,int arg_next_state){\n\t\tnext_center = arg_next_center;\n\t\tnext_state = arg_next_state;\n\t}\n\tint next_center,next_state;\n};\n\nstruct State{\n\tState(int arg_center,int arg_state, int arg_x,int arg_y, int arg_sum_step){\n\t\tcenter = arg_center;\n\t\tstate = arg_state;\n\t\tx = arg_x;\n\t\ty = arg_y;\n\t\tsum_step = arg_sum_step;\n\t}\n\tbool operator<(const struct State &arg) const{\n\n\t\treturn sum_step > arg.sum_step; //ステップの昇順(PQ)\n\t}\n\tint center,state,x,y,sum_step;\n};\n\nbool check[NUM][NUM];\nint base[NUM][6] = { {-1,4,-1,1,-1,5}, //0\n\t\t\t\t\t{-1,0,-1,2,-1,6}, //1\n\t\t\t\t\t{-1,1,-1,3,-1,7}, //2\n\t\t\t\t\t{-1,2,-1,4,-1,8}, //3\n\t\t\t\t\t{-1,3,-1,0,-1,9}, //4\n\t\t\t\t\t{-1,0,-1,11,-1,10}, //5\n\t\t\t\t\t{-1,1,-1,12,-1,11}, //6,\n\t\t\t\t\t{-1,2,-1,13,-1,12}, //7\n\t\t\t\t\t{-1,3,-1,14,-1,13}, //8\n\t\t\t\t\t{-1,4,-1,10,-1,14}, //9\n\t\t\t\t\t{-1,9,-1,5,-1,15}, //10\n\t\t\t\t\t{-1,16,-1,5,-1,6}, //11\n\t\t\t\t\t{-1,7,-1,17,-1,6}, //12\n\t\t\t\t\t{-1,7,-1,8,-1,18}, //13\n\t\t\t\t\t{-1,9,-1,19,-1,8}, //14\n\t\t\t\t\t{-1,10,-1,16,-1,19}, //15\n\t\t\t\t\t{-1,11,-1,17,-1,15}, //16\n\t\t\t\t\t{-1,16,-1,12,-1,18}, //17\n\t\t\t\t\t{-1,17,-1,13,-1,19}, //18\n\t\t\t\t\t{-1,14,-1,15,-1,18}}; //19\n\nint STATES[NUM][6][6];\nint min_dist[210][210][NUM],work[210][210][NUM][6];\nInfo info[NUM][6][6];\nType type_array[6] = {UP,R_UP,R_DOWN,DOWN,L_DOWN,L_UP};\nType rev_type[6] = {DOWN,L_DOWN,L_UP,UP,R_UP,R_DOWN};\n\n\nint main(){\n\n\tfor(int i = 0; i < NUM; i++){\n\n\t\tfor(int k = 0; k < 6; k++){\n\n\t\t\tSTATES[i][0][k] = base[i][k];\n\t\t}\n\t\t//手で持って見た時の遷移\n\t\tfor(int k = 1; k <= 5; k++){\n\n\t\t\tSTATES[i][k][UP] = STATES[i][k-1][L_UP];\n\t\t\tSTATES[i][k][R_UP] = STATES[i][k-1][UP];\n\t\t\tSTATES[i][k][R_DOWN] = STATES[i][k-1][R_UP];\n\t\t\tSTATES[i][k][DOWN] = STATES[i][k-1][R_DOWN];\n\t\t\tSTATES[i][k][L_DOWN] = STATES[i][k-1][DOWN];\n\t\t\tSTATES[i][k][L_UP] = STATES[i][k-1][L_DOWN];\n\t\t}\n\t}\n\n\t//遷移表の作成\n\tfor(int from = 0; from < NUM; from++){ //遷移元の中央\n\t\tfor(int from_state = 0; from_state < 6; from_state++){\n\t\t\tfor(int a = 0; a < 6;a++){ //転がる方向\n\t\t\t\tType dir = type_array[a];\n\n\t\t\t\tif(STATES[from][from_state][dir] == -1){\n\n\t\t\t\t\tinfo[from][from_state][dir].set(-1,-1);\n\n\t\t\t\t\tcontinue;\n\t\t\t\t}\n\n\t\t\t\tint to = STATES[from][from_state][dir];\n\t\t\t\tType next_type = rev_type[dir];\n\t\t\t\tfor(int b = 0; b < 6; b++){\n\t\t\t\t\tif(STATES[to][b][next_type] == from){\n\n\t\t\t\t\t\tinfo[from][from_state][dir].set(to,b);\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n\n\tint add_X = 100,add_Y = 100;\n\tfor(int i = 0; i < 210; i++){\n\t\tfor(int k = 0; k < 210; k++){\n\t\t\tfor(int a = 0; a < NUM; a++){\n\n\t\t\t\tmin_dist[i][k][a] = BIG_NUM;\n\t\t\t\tfor(int b = 0; b < 6; b++){\n\n\t\t\t\t\twork[i][k][a][b] = BIG_NUM;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n\n\tmin_dist[add_X][add_Y][0] = 0;\n\twork[add_X][add_Y][0][1] = 0;\n\tpriority_queue<State> Q;\n\tQ.push(State(0,1,0,0,0));\n\n\tint next_center,next_state,next_x,next_y,next_step;\n\n\twhile(!Q.empty()){\n\n\t\tif(Q.top().sum_step == 100){\n\n\t\t\tQ.pop();\n\n\t\t}else if(Q.top().sum_step > work[Q.top().x+add_X][Q.top().y+add_Y][Q.top().center][Q.top().state]){\n\n\t\t\tQ.pop();\n\n\t\t}else{\n\n\t\t\tfor(int i = 0; i < 6; i++){\n\t\t\t\tif(info[Q.top().center][Q.top().state][i].next_center == -1)continue;\n\n\t\t\t\tnext_center = info[Q.top().center][Q.top().state][i].next_center;\n\t\t\t\tnext_state = info[Q.top().center][Q.top().state][i].next_state;\n\n\t\t\t\tType dir = type_array[i];\n\t\t\t\tnext_x = Q.top().x;\n\t\t\t\tnext_y = Q.top().y;\n\n\t\t\t\tnext_step = Q.top().sum_step+1;\n\n\t\t\t\t//さいころの遷移は目で見た時と逆\n\t\t\t\tswitch(dir){\n\t\t\t\tcase UP:\n\t\t\t\t\tnext_y++;\n\t\t\t\t\tbreak;\n\n\t\t\t\tcase L_UP:\n\t\t\t\t\tnext_x++;\n\t\t\t\t\tbreak;\n\n\t\t\t\tcase L_DOWN:\n\t\t\t\t\tnext_x++;\n\t\t\t\t\tbreak;\n\n\t\t\t\tcase DOWN:\n\t\t\t\t\tnext_y--;\n\t\t\t\t\tbreak;\n\n\t\t\t\tcase R_DOWN:\n\t\t\t\t\tnext_x--;\n\t\t\t\t\tbreak;\n\n\t\t\t\tcase R_UP:\n\t\t\t\t\tnext_x--;\n\t\t\t\t\tbreak;\n\t\t\t\t}\n\n\t\t\t\tif(work[next_x+add_X][next_y+add_Y][next_center][next_state] > next_step){\n\t\t\t\t\twork[next_x+add_X][next_y+add_Y][next_center][next_state] = next_step;\n\t\t\t\t\tmin_dist[next_x+add_X][next_y+add_Y][next_center] = min(min_dist[next_x+add_X][next_y+add_Y][next_center],next_step);\n\t\t\t\t\tQ.push(State(next_center,next_state,next_x,next_y,next_step));\n\t\t\t\t}\n\t\t\t}\n\t\t\tQ.pop();\n\t\t}\n\t}\n\n\tint tmp_x,tmp_y,tmp_center;\n\n\twhile(true){\n\n\t\tscanf(\"%d %d %d\",&tmp_x,&tmp_y,&tmp_center);\n\t\tif(tmp_x == 0 && tmp_y == 0 && tmp_center == 0)break;\n\n\t\tprintf(\"%d\\n\",min_dist[tmp_x+add_X][tmp_y+add_Y][tmp_center]);\n\t}\n\n\treturn 0;\n}", "accuracy": 1, "time_ms": 80, "memory_kb": 27444, "score_of_the_acc": -0.2322, "final_rank": 7 }, { "submission_id": "aoj_1319_3625408", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\nconstexpr int inf = 1e9;\n\ntemplate<typename T>\nstd::vector<T> table(int n, T v) { return std::vector<T>(n, v); }\n\ntemplate <class... Args>\nauto table(int n, Args... args) {\n auto val = table(args...);\n return std::vector<decltype(val)>(n, std::move(val));\n}\n\nconstexpr int dx[3] = {1, 0, -1};\nconstexpr int dy[2][3] = {\n {0, 1, 0}, // (x + y) % 2 == 0\n {0, -1, 0}\n};\n\nconstexpr int df[20][2][3] = {\n {{1, 5, 4}, {4, 5, 1}},\n {{2, 6, 0}, {0, 6, 2}},\n {{3, 7, 1}, {1, 7, 3}},\n {{4, 8, 2}, {2, 8, 4}},\n {{0, 9, 3}, {3, 9, 0}},\n {{10, 0, 11}, {11, 0, 10}},\n {{11, 1, 12}, {12, 1, 11}},\n {{12, 2, 13}, {13, 2, 12}},\n {{13, 3, 14}, {14, 3, 13}},\n {{14, 4, 10}, {10, 4, 14}},\n {{5, 15, 9}, {9, 15, 5}},\n {{6, 16, 5}, {5, 16, 6}},\n {{7, 17, 6}, {6, 17, 7}},\n {{8, 18, 7}, {7, 18, 8}},\n {{9, 19, 8}, {8, 19, 9}},\n {{19, 10, 16}, {16, 10, 19}},\n {{15, 11, 17}, {17, 11, 15}},\n {{16, 12, 18}, {18, 12, 16}},\n {{17, 13, 19}, {19, 13, 17}},\n {{18, 14, 15}, {15, 14, 18}}\n};\nconstexpr int dd[3][2][3] = { // only used on 0, ..., 4, 15, ..., 19\n {{2, 0, 1}, {1, 0, 2}},\n {{1, 2, 0}, {0, 2, 1}},\n {{0, 1, 2}, {2, 1, 0}}\n};\n\nint main() {\n constexpr int max_x = 201, max_y = 102;\n constexpr int cx = 100, cy = 50;\n auto in_range = [] (int x, int y) {\n return 0 <= x && x < max_x && 0 <= y && y < max_y;\n };\n\n int gx, gy, n;\n while(cin >> gx >> gy >> n, gx | gy | n) {\n gx += cx, gy += cy;\n auto d = table(max_x, max_y, 20, 3, inf);\n d[cx][cy][0][0] = 0;\n queue<tuple<int, int, int, int>> que;\n que.emplace(cx, cy, 0, 0);\n int ans = inf;\n while(!que.empty()) {\n int x, y, f, dir; tie(x, y, f, dir) = que.front();\n que.pop();\n if(x == gx && y == gy && f == n) {\n ans = d[x][y][f][dir];\n break;\n }\n for(int i = 0; i < 3; ++i) {\n const int nx = x + dx[i], ny = y + dy[(x + y) & 1][i];\n int nf = 0, nd = 0;\n if((x + y) & 1) {\n nf = df[f][1][(i - dir + 3) % 3];\n } else {\n nf = df[f][0][(i + dir) % 3];\n }\n if((5 <= f && f < 15) || (5 <= nf && nf < 15)) {\n nd = dir;\n } else {\n nd = dd[dir][(x + y) & 1][i];\n }\n if(!in_range(nx, ny) || d[nx][ny][nf][nd] != inf) continue;\n d[nx][ny][nf][nd] = d[x][y][f][dir] + 1;\n que.emplace(nx, ny, nf, nd);\n }\n }\n\n cout << ans << endl;\n }\n}", "accuracy": 1, "time_ms": 1750, "memory_kb": 26416, "score_of_the_acc": -0.5912, "final_rank": 13 }, { "submission_id": "aoj_1319_3228722", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\n#define rep(i,n) for(int (i)=0;(i)<(int)(n);++(i))\n#define all(x) (x).begin(),(x).end()\n#define pb push_back\n#define fi first\n#define se second\n#define dbg(x) cout<<#x\" = \"<<((x))<<endl\ntemplate<class T,class U> ostream& operator<<(ostream& o, const pair<T,U> &p){o<<\"(\"<<p.fi<<\",\"<<p.se<<\")\";return o;}\ntemplate<class T> ostream& operator<<(ostream& o, const vector<T> &v){o<<\"[\";for(T t:v){o<<t<<\",\";}o<<\"]\";return o;}\n\nconst int N = 230;\nconst int A = 108;\n\nconst int INF = 19191919;\nconst int F = 20;\nint dp[N][N][F][F];\n\nstruct State{\n int x,y,b,s;\n};\n\nint nxt[20][3]={\n {5,1,4},\n {6,2,0},\n {7,3,1},\n {8,4,2},\n {9,0,3},\n {0,10,11}, //5\n {1,11,12},\n {2,12,13},\n {3,13,14},\n {4,14,10},\n {15,5,9}, //10\n {16,6,5},\n {17,7,6},\n {18,8,7},\n {19,9,8},\n {10,19,16}, //15\n {11,15,17},\n {12,16,18},\n {13,17,19},\n {14,18,15}\n};\n\nconst int dx[2][3]={{0,1,-1},{0,-1,1}};\nconst int dy[2][3]={{1,0,0},{-1,0,0}};\n\nconst int LIM = 100;\n\nint main(){\n rep(i,N)rep(j,N)rep(k,F)rep(l,F) dp[i][j][k][l] = INF;\n dp[A][A][0][5] = 0;\n queue<State> que;\n que.push({A,A,0,5});\n while(!que.empty()){\n State c = que.front();\n que.pop();\n if(dp[c.x][c.y][c.b][c.s]>=LIM) continue;\n\n // printf(\"NOW : %d %d %d %d ::: %d\\n\",c.x,c.y,c.b,c.s,dp[c.x][c.y][c.b][c.s]);\n\n int ad = 0;\n while(nxt[c.b][ad] != c.s) ++ad;\n\n int oe = (c.x+c.y)%2;\n rep(i,3){\n int nx = c.x+dx[oe][i], ny = c.y+dy[oe][i];\n if(!(0<=nx && nx<N && 0<=ny && ny<N)) continue;\n int noe = (nx+ny)%2;\n\n int nb = nxt[c.b][(i+ad)%3];\n int ns = -1;\n rep(j,3){\n int xx = nx+dx[noe][j], yy = ny+dy[noe][j];\n if(xx == c.x && yy == c.y){\n int idx = 0;\n while(nxt[nb][idx] != c.b) ++idx;\n\n idx = (idx-j+3)%3;\n ns = nxt[nb][idx];\n break;\n }\n }\n assert(ns != -1);\n\n // printf(\" nx %d :: %d %d %d %d\\n\",i,nx,ny,nb,ns);\n if(dp[nx][ny][nb][ns] > dp[c.x][c.y][c.b][c.s]+1){\n dp[nx][ny][nb][ns] = dp[c.x][c.y][c.b][c.s]+1;\n que.push({nx,ny,nb,ns});\n }\n }\n }\n\n int x,y,n;\n while(cin >>x >>y >>n){\n if(x==0 && y==0 && n==0) break;\n int ans = INF;\n rep(i,F) ans = min(ans, dp[x+A][y+A][n][i]);\n cout << ans << endl;\n }\n return 0;\n}", "accuracy": 1, "time_ms": 60, "memory_kb": 85876, "score_of_the_acc": -0.7793, "final_rank": 16 }, { "submission_id": "aoj_1319_3057153", "code_snippet": "#include <iostream>\n#include <vector>\n#include <algorithm>\n#include <queue>\n#include <utility>\n#include <cstring>\nusing namespace std;\ntypedef pair<int, int> pii;\ntypedef vector<pii> vpi;\n\nstruct info{\n int x,y,b,t;\n int step;\n bool isEven;\n info(int x, int y, int b, int t, int s, bool e):x(x),y(y),b(b),t(t),step(s),isEven(e){}\n info(){}\n};\nbool used[203][103][20][20];\n\nint main(){\n vector<vector<vector<pii> > > next(20, vector<vector<pii> >(20));\n //next, 右左上\n //odd, 左右下\n next[0][1] = vpi{pii{4, 9}, pii{5, 10}, pii{1, 0}};\n next[0][4] = vpi{pii{5, 11}, pii{1, 6}, pii{4, 0}};\n next[0][5] = vpi{pii{1, 2}, pii{4, 3}, pii{5, 0}};\n next[1][0] = vpi{pii{6, 12}, pii{2, 7}, pii{0, 1}};\n next[1][2] = vpi{pii{0, 5}, pii{6, 11}, pii{2, 1}};\n next[1][6] = vpi{pii{2, 3}, pii{0, 4}, pii{6, 1}};\n next[2][1] = vpi{pii{7, 13}, pii{3, 8}, pii{1, 2}};\n next[2][3] = vpi{pii{1, 6}, pii{7, 12}, pii{3, 2}};\n next[2][7] = vpi{pii{3, 4}, pii{1, 0}, pii{7, 2}};\n next[3][2] = vpi{pii{8, 14}, pii{4, 9}, pii{2, 3}};\n next[3][4] = vpi{pii{2, 7}, pii{8, 13}, pii{4, 3}};\n next[3][8] = vpi{pii{4, 0}, pii{2, 1}, pii{8, 3}};\n next[4][0] = vpi{pii{3, 8}, pii{9, 14}, pii{0, 4}};\n next[4][3] = vpi{pii{9, 10}, pii{0, 5}, pii{3, 4}};\n next[4][9] = vpi{pii{0, 1}, pii{3, 2}, pii{9, 4}};\n next[5][0] = vpi{pii{10, 15}, pii{11, 16}, pii{0, 5}};\n next[5][10] = vpi{pii{11, 6}, pii{0, 1}, pii{10, 5}};\n next[5][11] = vpi{pii{0, 4}, pii{10, 9}, pii{11, 5}};\n next[6][1] = vpi{pii{11, 16}, pii{12, 17}, pii{1, 6}};\n next[6][11] = vpi{pii{12, 7}, pii{1, 2}, pii{11, 6}};\n next[6][12] = vpi{pii{1, 0}, pii{11, 5}, pii{12, 6}};\n next[7][2] = vpi{pii{12, 17}, pii{13, 18}, pii{2, 7}};\n next[7][12] = vpi{pii{13, 8}, pii{2, 3}, pii{12, 7}};\n next[7][13] = vpi{pii{2, 1}, pii{12, 6}, pii{13, 7}};\n next[8][3] = vpi{pii{13, 18}, pii{14, 19}, pii{3, 8}};\n next[8][13] = vpi{pii{14, 9}, pii{3, 4}, pii{13, 8}};\n next[8][14] = vpi{pii{3, 2}, pii{13, 7}, pii{14, 8}};\n next[9][4] = vpi{pii{14, 19}, pii{10, 15}, pii{4, 9}};\n next[9][10] = vpi{pii{4, 3}, pii{14, 8}, pii{10, 9}};\n next[9][14] = vpi{pii{10, 5}, pii{4, 0}, pii{14, 9}};\n next[10][5] = vpi{pii{9, 14}, pii{15, 19}, pii{5, 10}};\n next[10][9] = vpi{pii{15, 16}, pii{5, 11}, pii{9, 10}};\n next[10][15] = vpi{pii{5, 0}, pii{9, 4}, pii{15, 10}};\n next[11][5] = vpi{pii{16, 17}, pii{6, 12}, pii{5, 11}};\n next[11][6] = vpi{pii{5, 10}, pii{16, 15}, pii{6, 11}};\n next[11][16] = vpi{pii{6, 1}, pii{5, 0}, pii{16, 11}};\n next[12][6] = vpi{pii{17, 18}, pii{7, 13}, pii{6, 12}};\n next[12][7] = vpi{pii{6, 11}, pii{17, 16}, pii{7, 12}};\n next[12][17] = vpi{pii{7, 2}, pii{6, 1}, pii{17, 12}};\n next[13][7] = vpi{pii{18, 19}, pii{8, 14}, pii{7, 13}};\n next[13][8] = vpi{pii{7, 12}, pii{18, 17}, pii{8, 13}};\n next[13][18] = vpi{pii{8, 3}, pii{7, 2}, pii{18, 13}};\n next[14][8] = vpi{pii{19, 15}, pii{9, 10}, pii{8, 14}};\n next[14][9] = vpi{pii{8, 13}, pii{19, 18}, pii{9, 14}};\n next[14][19] = vpi{pii{9, 4}, pii{8, 3}, pii{19, 14}};\n next[15][10] = vpi{pii{19, 18}, pii{16, 17}, pii{10, 15}};\n next[15][16] = vpi{pii{10, 9}, pii{19, 14}, pii{16, 15}};\n next[15][19] = vpi{pii{16, 11}, pii{10, 5}, pii{19, 15}};\n next[16][11] = vpi{pii{15, 19}, pii{17, 18}, pii{11, 16}};\n next[16][15] = vpi{pii{17, 12}, pii{11, 6}, pii{15, 16}};\n next[16][17] = vpi{pii{11, 5}, pii{15, 10}, pii{17, 16}};\n next[17][12] = vpi{pii{16, 15}, pii{18, 19}, pii{12, 17}};\n next[17][16] = vpi{pii{18, 13}, pii{12, 7}, pii{16, 17}};\n next[17][18] = vpi{pii{12, 6}, pii{16, 11}, pii{18, 17}};\n next[18][13] = vpi{pii{17, 16}, pii{19, 15}, pii{13, 18}};\n next[18][17] = vpi{pii{19, 14}, pii{13, 8}, pii{17, 18}};\n next[18][19] = vpi{pii{13, 7}, pii{17, 12}, pii{19, 18}};\n next[19][14] = vpi{pii{18, 17}, pii{15, 16}, pii{14, 19}};\n next[19][15] = vpi{pii{14, 8}, pii{18, 13}, pii{15, 19}};\n next[19][18] = vpi{pii{15, 10}, pii{14, 9}, pii{18, 19}};\n\n while(1){\n int tx,ty,n;\n cin >> tx >> ty >> n;\n if(tx==0 && ty==0 && n==0) break;\n\n memset(used, 0, sizeof(used));\n queue<info> wait;\n wait.push(info(0, 0, 0, 5, 0, true));\n used[101][51][0][5] = true;\n int ans = -1;\n while(!wait.empty()){\n int x = wait.front().x;\n int y = wait.front().y;\n int b = wait.front().b;\n int t = wait.front().t;\n int step = wait.front().step;\n int isEven = wait.front().isEven;\n wait.pop();\n if(x == tx && y == ty && b == n){\n ans = step;\n break;\n }\n int dx[3], dy[3];\n if(isEven){\n dx[0] = 1; dx[1] = -1; dx[2] = 0;\n dy[0] = 0; dy[1] = 0; dy[2] = 1;\n }else{\n dx[0] = -1; dx[1] = 1; dx[2] = 0;\n dy[0] = 0; dy[1] = 0; dy[2] = -1;\n }\n for(int i=0; i<3; i++){\n int nx = x +dx[i];\n int ny = y +dy[i];\n int nb = next[b][t][i].first;\n int nt = next[b][t][i].second;\n if(used[nx+101][ny+51][nb][nt]) continue;\n used[nx+101][ny+51][nb][nt] = true;\n wait.push(info(nx, ny, nb, nt, step+1, !isEven));\n }\n }\n cout << ans << endl;\n }\n\n return 0;\n}", "accuracy": 1, "time_ms": 390, "memory_kb": 11556, "score_of_the_acc": -0.1507, "final_rank": 4 }, { "submission_id": "aoj_1319_2938738", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\nusing Int = long long;\ntypedef pair<Int, Int> P;\n\ntemplate<typename T1,typename T2> inline void chmin(T1 &a,T2 b){if(a>b) a=b;}\ntemplate<typename T1,typename T2> inline void chmax(T1 &a,T2 b){if(a<b) a=b;}\n\nvector<vector<P> > nx\n{\n {P(0,5),P(1,6),P(2,7),P(3,8),P(4,9)},\n {P(0,1),P(4,3),P(9,14),P(10,15),P(5,11)},\n {P(0,4),P(5,10),P(11,16),P(6,12),P(1,2)},\n {P(1,0),P(6,11),P(12,17),P(7,13),P(2,3)},\n {P(2,1),P(7,12),P(13,18),P(8,14),P(3,4)},\n {P(3,2),P(8,13),P(14,19),P(9,10),P(4,0)},\n {P(5,0),P(10,9),P(15,19),P(16,17),P(11,6)},\n {P(6,1),P(11,5),P(16,15),P(17,18),P(12,7)},\n {P(7,2),P(12,6),P(17,16),P(18,19),P(13,8)},\n {P(8,3),P(13,7),P(18,17),P(19,15),P(14,9)},\n {P(9,4),P(14,8),P(19,18),P(15,16),P(10,5)},\n {P(15,10),P(19,14),P(18,13),P(17,12),P(16,11)}\n};\n\nsigned char dp[203][203][21][21][6];\nstruct st{\n Int y,x,n,m,d;\n st(Int y,Int x,Int n,Int m,Int d):y(y),x(x),n(n),m(m),d(d){}\n};\nconst Int CC = 101;\nsigned main(){ \n cin.tie(0);\n ios::sync_with_stdio(0);\n {\n memset(dp,-1,sizeof(dp));\n queue<st> q;\n dp[CC][CC][0][5][0]=0;\n q.emplace(CC,CC,0,5,0);\n Int dyl[6]={0,0,1,0,0,-1};\n Int dxl[6]={-1,-1,0,1,1,0};\n Int dyr[6]={0,-1,0,0,1,0};\n Int dxr[6]={1,0,-1,-1,0,1};\n Int dyo[6]={1,0,0,-1,0,0}; \n Int dxo[6]={0,1,1,0,-1,-1};\n\n auto push=[&](st a,st b){\n if(~dp[a.y][a.x][a.n][a.m][a.d]) return;\n dp[a.y][a.x][a.n][a.m][a.d]=dp[b.y][b.x][b.n][b.m][b.d]+1;\n q.emplace(a);\n };\n \n while(!q.empty()){\n st s=q.front();q.pop();\n if(dp[s.y][s.x][s.n][s.m][s.d]>100) break;\n \n Int k=0,l=0;\n for(Int i=0;i<12;i++)\n\tfor(Int j=0;j<5;j++)\n\t if(nx[i][j]==P(s.n,s.m)) k=i,l=j;\n \n //cout<<k<<\" \"<<l<<endl;\n \n {// L\n\tst t=s;\n\tt.y+=dyl[s.d];\n\tt.x+=dxl[s.d];\n\ttie(t.n,t.m)=nx[k][(l+4)%5];\n\tt.d=(s.d+5)%6;\t\n\tpush(t,s);\n }\n {// R\n\tst t=s;\n\tt.y+=dyr[s.d];\n\tt.x+=dxr[s.d];\n\ttie(t.n,t.m)=nx[k][(l+1)%5];\n\tt.d=(s.d+1)%6;\t\n\tpush(t,s);\n }\n {// O\n\tst t=s;\n\tt.y+=dyo[s.d];\n\tt.x+=dxo[s.d];\n\tswap(t.n,t.m);\n\tt.d=(s.d+3)%6;\t\n\tpush(t,s);\n }\n }\n \n \n }\n \n \n {\n Int x,y,n;\n while(cin>>x>>y>>n,x||y||n){\n short ans=101;\n x+=CC;y+=CC;\n for(Int i=0;i<20;i++){\n\tfor(Int j=0;j<6;j++){\n\t if(dp[y][x][n][i][j]<0) continue;\n\t chmin(ans,dp[y][x][n][i][j]);\n\t}\n }\n cout<<ans<<endl;\n }\n }\n return 0;\n}", "accuracy": 1, "time_ms": 110, "memory_kb": 109956, "score_of_the_acc": -1.0177, "final_rank": 18 }, { "submission_id": "aoj_1319_2936600", "code_snippet": "#include <bits/stdc++.h>\n#define int long long\nusing namespace std;\n\nunsigned char dp[205][205][20][20]; // dp zyanai kedo\n\nint even[20][3]={\n {5,4,1},\n {6,0,2},\n {7,1,3},\n {8,2,4},\n {9,3,0},\n {0,11,10},\n {1,12,11},\n {2,13,12},\n {3,14,13},\n {4,10,14},\n {15,9,5},\n {16,5,6},\n {17,6,7},\n {18,7,8},\n {19,8,9},\n {10,16,19},\n {11,17,15},\n {12,18,16},\n {13,19,17},\n {14,15,18}\n};\nint odd[20][3]={\n {5,4,1},\n {6,0,2},\n {7,1,3},\n {8,2,4},\n {9,3,0},\n {0,11,10},\n {1,12,11},\n {2,13,12},\n {3,14,13},\n {4,10,14},\n {15,9,5},\n {16,5,6},\n {17,6,7},\n {18,7,8},\n {19,8,9},\n {10,16,19},\n {11,17,15},\n {12,18,16},\n {13,19,17},\n {14,15,18}\n};\nint odx[]={0,-1,1};\nint ody[]={-1,0,0};\nint edx[]={0,-1,1};\nint edy[]={1,0,0};\nvoid init(){\n for(int i=0;i<20;i++){\n swap(odd[i][1],odd[i][2]);\n }\n for(int i=0;i<205;i++)\n for(int j=0;j<205;j++)\n for(int k=0;k<20;k++)\n for(int l=0;l<20;l++)\n dp[i][j][k][l]=200;\n }\n\n struct point{\n int cost,x,y,muki,sita;\n };\n\n point A;\n\n void BFS(){\n A.sita=0;\n A.cost=0;\n A.muki=5;\n A.x=102;\n A.y=102;\n queue<point>q;\n q.push(A);\n dp[0][0][0][5]=0;\n while(!q.empty()){\n point P=q.front();q.pop();\n int y=P.y;\n int x=P.x;\n int muki=P.muki;\n int cost=P.cost;\n int sita=P.sita; //cout<<y<<' '<<x<<' '<<sita<<endl;\n //cout<<x-102<<' '<<y-120<<' '<<sita<<' '<<cost<<endl;\n //if(cost==5)return;\n\n\n if(cost%2){\n int k=0;\n for(int i=0;i<3;i++){\n if(odd[sita][i]==muki){\n k=i;\n break;\n }\n }//continue;\n for(int i=0;i<3;i++,k=(k+1)%3){\n int yy=y+ody[i];\n int xx=x+odx[i];\n if( yy< 0 || xx< 0 || 204<xx || 204<yy )continue;\n int nex_sita=odd[sita][k];\n int nex_muki=sita;\n int l=0;\n //if(cost==1&&x==101&&y==102)cout<<i<<' '<<sita<<' '<<nex_sita<<' '<<muki<<endl;\n for(int j=0;j<3;j++){\n if(even[nex_sita][j]==sita){\n l=j;\n break;\n }\n }\n if(i==0){\n nex_muki=sita;\n }\n else if(i==1){\n nex_muki=even[nex_sita][(l+1)%3];\n }\n else{\n nex_muki=even[nex_sita][(l+2)%3];\n }\n if(dp[yy][xx][nex_sita][nex_muki]!=200)continue;\n //if(cost==1&&x==101&&y==102)cout<<l<<' '<<nex_sita<<' '<<nex_muki<<endl;\n //if(cost==1&&xx==101&&yy==102&&cost==1)cout<<sita<<' '<<nex_sita<<' '<<cost<<endl;\n dp[yy][xx][nex_sita][nex_muki]=cost+1;\n A.sita=nex_sita;\n A.cost=cost+1;\n A.muki=nex_muki;\n A.x=xx;\n A.y=yy;\n q.push(A);\n }\n }\n else{\n int k=0;\n for(int i=0;i<3;i++){\n if(even[sita][i]==muki){\n k=i;\n break;\n }\n }//continue;\n for(int i=0;i<3;i++,k=(k+1)%3){\n int yy=y+edy[i];\n int xx=x+edx[i];\n if( yy< 0 || xx< 0 || 204<xx || 204<yy )continue;\n int nex_sita=even[sita][k];\n int nex_muki=sita;\n int l=0;\n //if(cost==0&&xx==0+102&&yy==101&&cost==2)cout<<sita<<' '<<nex_sita<<' '<<cost<<endl;\n for(int j=0;j<3;j++){\n if(odd[nex_sita][j]==sita){\n l=j;\n break;\n }\n }\n if(i==0){\n nex_muki=sita;\n }\n else if(i==1){\n nex_muki=odd[nex_sita][(l+1)%3];\n }\n else{\n nex_muki=odd[nex_sita][(l+2)%3];\n }\n if(dp[yy][xx][nex_sita][nex_muki]!=200)continue;\n //if(cost==0&&xx==101&&yy==102&&cost==0)cout<<\"even\"<<i<<' '<<l<<' '<<sita<<' '<<nex_sita<<' '<<nex_muki<<endl;\n //if(cost==2&&xx==0+102&&yy==101&&cost==2)cout<<sita<<' '<<nex_sita<<' '<<cost<<endl;\n dp[yy][xx][nex_sita][nex_muki]=cost+1;\n A.sita=nex_sita;\n A.cost=cost+1;\n A.muki=nex_muki;\n A.x=xx;\n A.y=yy;\n q.push(A);\n }\n }\n }\n}\n\nsigned main(){\n int x,y,c;\n init();\n BFS();\n while(cin>>x>>y>>c){\n if(!x&&!y&&!c)break;\n x+=102;\n y+=102;\n int ans=1e9;\n for(int i=0;i<20;i++){\n ans=min(ans,(int)dp[y][x][c][i]);\n }\n cout<<ans<<endl;\n }\n\n return 0;\n}", "accuracy": 1, "time_ms": 80, "memory_kb": 20116, "score_of_the_acc": -0.1631, "final_rank": 5 }, { "submission_id": "aoj_1319_2936551", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\nusing Int = long long;\ntypedef pair<Int, Int> P;\n\ntemplate<typename T1,typename T2> inline void chmin(T1 &a,T2 b){if(a>b) a=b;}\ntemplate<typename T1,typename T2> inline void chmax(T1 &a,T2 b){if(a<b) a=b;}\n\nvector<vector<P> > nx\n{\n {P(0,5),P(1,6),P(2,7),P(3,8),P(4,9)},\n {P(0,1),P(4,3),P(9,14),P(10,15),P(5,11)},\n {P(0,4),P(5,10),P(11,16),P(6,12),P(1,2)},\n {P(1,0),P(6,11),P(12,17),P(7,13),P(2,3)},\n {P(2,1),P(7,12),P(13,18),P(8,14),P(3,4)},\n {P(3,2),P(8,13),P(14,19),P(9,10),P(4,0)},\n {P(5,0),P(10,9),P(15,19),P(16,17),P(11,6)},\n {P(6,1),P(11,5),P(16,15),P(17,18),P(12,7)},\n {P(7,2),P(12,6),P(17,16),P(18,19),P(13,8)},\n {P(8,3),P(13,7),P(18,17),P(19,15),P(14,9)},\n {P(9,4),P(14,8),P(19,18),P(15,16),P(10,5)},\n {P(15,10),P(19,14),P(18,13),P(17,12),P(16,11)}\n};\n\nsigned char dp[203][203][21][21][6];\nstruct st{\n Int y,x,n,m,d;\n st(Int y,Int x,Int n,Int m,Int d):y(y),x(x),n(n),m(m),d(d){}\n};\nconst Int CC = 101;\nsigned main(){ \n cin.tie(0);\n ios::sync_with_stdio(0);\n {\n memset(dp,-1,sizeof(dp));\n queue<st> q;\n dp[CC][CC][0][5][0]=0;\n q.emplace(CC,CC,0,5,0);\n Int dyl[6]={0,0,1,0,0,-1};\n Int dxl[6]={-1,-1,0,1,1,0};\n Int dyr[6]={0,-1,0,0,1,0};\n Int dxr[6]={1,0,-1,-1,0,1};\n Int dyo[6]={1,0,0,-1,0,0}; \n Int dxo[6]={0,1,1,0,-1,-1};\n\n auto push=[&](st a,st b){\n if(~dp[a.y][a.x][a.n][a.m][a.d]) return;\n dp[a.y][a.x][a.n][a.m][a.d]=dp[b.y][b.x][b.n][b.m][b.d]+1;\n q.emplace(a);\n };\n \n while(!q.empty()){\n st s=q.front();q.pop();\n if(dp[s.y][s.x][s.n][s.m][s.d]>100) break;\n \n Int k=0,l=0;\n for(Int i=0;i<12;i++)\n\tfor(Int j=0;j<5;j++)\n\t if(nx[i][j]==P(s.n,s.m)) k=i,l=j;\n \n //cout<<k<<\" \"<<l<<endl;\n \n {// L\n\tst t=s;\n\tt.y+=dyl[s.d];\n\tt.x+=dxl[s.d];\n\ttie(t.n,t.m)=nx[k][(l+4)%5];\n\tt.d=(s.d+5)%6;\t\n\tpush(t,s);\n }\n {// R\n\tst t=s;\n\tt.y+=dyr[s.d];\n\tt.x+=dxr[s.d];\n\ttie(t.n,t.m)=nx[k][(l+1)%5];\n\tt.d=(s.d+1)%6;\t\n\tpush(t,s);\n }\n {// O\n\tst t=s;\n\tt.y+=dyo[s.d];\n\tt.x+=dxo[s.d];\n\tswap(t.n,t.m);\n\tt.d=(s.d+3)%6;\t\n\tpush(t,s);\n }\n }\n \n \n }\n \n \n {\n Int x,y,n;\n while(cin>>x>>y>>n,x||y||n){\n short ans=101;\n x+=CC;y+=CC;\n for(Int i=0;i<20;i++){\n\tfor(Int j=0;j<6;j++){\n\t if(dp[y][x][n][i][j]<0) continue;\n\t chmin(ans,dp[y][x][n][i][j]);\n\t}\n }\n cout<<ans<<endl;\n }\n }\n return 0;\n}", "accuracy": 1, "time_ms": 110, "memory_kb": 109956, "score_of_the_acc": -1.0177, "final_rank": 18 }, { "submission_id": "aoj_1319_2418810", "code_snippet": "#include <cstdio>\n#include <algorithm>\n#include <cstring>\n#include <queue>\n#include <utility>\n\nconst int drct[2][3][2] = {{{0, 1}, {-1, 0}, {1, 0}}, {{0, -1}, {1, 0}, {-1, 0}}};\n\nint sd_x, sd_y, bottom;\n\nstruct State {\n\tint x, y, bot, up, step;\n\t\n\tState() {}\n\tState(int x, int y, int bot, int up, int step): \n\t\tx(x), y(y), bot(bot), up(up), step(step) {}\n};\n\nint NEXT(int x, int t) {\n\tswitch (t) {\n\t\tcase 0:\n\t\t\tif (x < 5 || (x >= 10 && x < 15)) return x + 5;\n\t\t\treturn x - 5;\n\t\tcase 1:\n\t\t\tif (x < 5) return (x + 4) % 5;\n\t\t\tif (x < 10) return (x + 5 - 10 + 1) % 5 + 10;\n\t\t\tif (x < 15) return (x - 5 - 5 + 4) % 5 + 5;\n\t\t\treturn (x - 15 + 1) % 5 + 15;\n\t\tcase 2:\n\t\t\tif (x < 5) return (x + 1) % 5;\n\t\t\tif (x < 10) return x + 5;\n\t\t\tif (x < 15) return x - 5;\n\t\t\treturn (x - 15 + 4) % 5 + 15;\n\t}\n\t\n\treturn -1;\n}\n\nstd::pair<int, int> NEXT(int x, int up, int t) {\n\tint a = NEXT(x, (up + t) % 3), b;\n\tswitch ((up + t) % 3) {\n\t\tcase 0:\n\t\t\tb = 0; break;\n\t\tcase 1:\n\t\t\tif (x < 5 || x >= 15) b = 1;\n\t\t\telse b = 0;\n\t\t\tbreak;\n\t\tcase 2:\n\t\t\tif (x < 5 || x >= 15) b = 2;\n\t\t\telse b = 0;\n\t\t\tbreak;\n\t}\n\treturn std::make_pair(a, (b + up) % 3);\n}\n\nint dist[205][205][20][3];\n\nvoid BFS() {\n\tstatic std::queue<State> que;\n\tstatic State now;\n\t\n\tque.push(State(0, 0, 0, 0, 0));\n\tdist[0 + 100][0 + 100][0][0] = 0;\n\twhile (!que.empty()) {\n\t\tnow = que.front(); que.pop();\n\t\tif (now.step >= 100) break;\n\t\tfor (int t = 0; t < 3; ++t) {\n\t\t\tint xx = now.x + drct[(now.x + now.y) & 1][t][0], \n\t\t\t\tyy = now.y + drct[(now.x + now.y) & 1][t][1];\n\t\t\tint fir = NEXT(now.bot, now.up, t).first, \n\t\t\t\tsec = NEXT(now.bot, now.up, t).second;\n\t\t\tif (~dist[xx + 100][yy + 100][fir][sec]) continue;\n\t\t\tdist[xx + 100][yy + 100][fir][sec] = now.step + 1;\n\t\t\tque.push(State(xx, yy, fir, sec, now.step + 1));\n\t\t}\n\t}\n}\n\nint main() {\n\tmemset(dist, -1, sizeof dist);\n\tBFS();\n\t\n\twhile (~scanf(\"%d%d%d\", &sd_x, &sd_y, &bottom) && (sd_x || sd_y || bottom)) {\n\t\tint ans = 0x3f3f3f3f;\n\t\tfor (int t = 0; t < 3; ++t) if (~dist[sd_x + 100][sd_y + 100][bottom][t])\n\t\t\tans = std::min(ans, dist[sd_x + 100][sd_y + 100][bottom][t]);\n\t\tprintf(\"%d\\n\", ans);\n\t}\n\t\n\treturn 0;\n}", "accuracy": 1, "time_ms": 40, "memory_kb": 12640, "score_of_the_acc": -0.0837, "final_rank": 2 }, { "submission_id": "aoj_1319_2395872", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\n \nvoid rA(int t[20]){\n int u[20]={5,10,15,16,11,0,9,19,17,6,1,4,14,18,12,2,3,8,13,7};\n int tmp[20];\n for(int i=0;i<20;i++)tmp[i]=t[ u[i] ];\n for(int i=0;i<20;i++)t[i]=tmp[i];\n}\n\nvoid rB(int t[20]){\n int u[20]={1,0,5,11,6,2,4,10,16,12,7,3,9,15,17,13,8,14,19,18};\n int tmp[20];\n for(int i=0;i<20;i++)tmp[i]=t[ u[i] ];\n for(int i=0;i<20;i++)t[i]=tmp[i];\n}\n \nvoid rC(int t[20]){\n int u[20]={4,9,10,5,0,3,14,15,11,1,2,8,19,16,6,7,13,18,17,12};\n int tmp[20];\n for(int i=0;i<20;i++)tmp[i]=t[ u[i] ];\n for(int i=0;i<20;i++)t[i]=tmp[i];\n}\n \nvoid rD(int t[20]){\n int u[20]={5,10,15,16,11,0,9,19,17,6,1,4,14,18,12,2,3,8,13,7};\n int tmp[20];\n for(int i=0;i<20;i++)tmp[i]=t[ u[i] ];\n for(int i=0;i<20;i++)t[i]=tmp[i];\n}\n \nvoid rE(int t[20]){\n int u[20]={1,0,5,11,6,2,4,10,16,12,7,3,9,15,17,13,8,14,19,18};\n int tmp[20];\n for(int i=0;i<20;i++)tmp[i]=t[ u[i] ];\n for(int i=0;i<20;i++)t[i]=tmp[i];\n}\n \nvoid rF(int t[20]){\n int u[20]={4,9,10,5,0,3,14,15,11,1,2,8,19,16,6,7,13,18,17,12};\n int tmp[20];\n for(int i=0;i<20;i++)tmp[i]=t[ u[i] ];\n for(int i=0;i<20;i++)t[i]=tmp[i];\n}\n \nstruct die{\n int p[20];\n bool operator < ( const die &A ) const {\n for(int i=0;i<20;i++)\n if(p[i]!=A.p[i])return (p[i]<A.p[i]);\n return false;\n }\n};\n \n \ntypedef pair<int,int> P;\ntypedef pair< P , int > state;\n \ntypedef pair< P , int > PP;\ntypedef unsigned int ull;\n \nmap< ull , int > d;\nmap< PP , int > ans;\nqueue< state > Q;\nmap< die , int > dice;\nmap< int , die > redice;\n\n\nint dieId(die d){\n if(dice.count(d)>0)return dice[d];\n int res=dice.size();\n dice[d]=res;\n redice[res]=d;\n return res;\n}\n\nint RA(int id){\n static map<int,int> mp;\n if(mp.count(id)>0)return mp[id];\n die d=redice[id];\n rA(d.p);\n mp[id]=dieId(d);\n return mp[id];\n}\nint RB(int id){\n static map<int,int> mp;\n if(mp.count(id)>0)return mp[id];\n die d=redice[id];\n rB(d.p);\n mp[id]=dieId(d);\n return mp[id];\n}\nint RC(int id){\n static map<int,int> mp;\n if(mp.count(id)>0)return mp[id];\n die d=redice[id];\n rC(d.p);\n mp[id]=dieId(d);\n return mp[id];\n}\nint RD(int id){\n static map<int,int> mp;\n if(mp.count(id)>0)return mp[id];\n die d=redice[id];\n rD(d.p);\n mp[id]=dieId(d);\n return mp[id];\n}\nint RE(int id){\n static map<int,int> mp;\n if(mp.count(id)>0)return mp[id];\n die d=redice[id];\n rE(d.p);\n mp[id]=dieId(d);\n return mp[id];\n}\nint RF(int id){\n static map<int,int> mp;\n if(mp.count(id)>0)return mp[id];\n die d=redice[id];\n rF(d.p);\n mp[id]=dieId(d);\n return mp[id];\n}\n \null getHash(state s){\n ull res=0, B=5575777;\n int x=s.first.first+150;\n int y=s.first.second+150;\n res=x*B+y;\n die d=redice[s.second];\n for(int i=0;i<20;i++)res=res*B+d.p[i];\n return res;\n}\n \nvoid Push(state s,int cost){\n ull key=getHash(s);\n if(d.count(key)>0)return;\n d[key]=cost+1;\n Q.push(s);\n}\n \nint main(){\n die v;\n for(int i=0;i<20;i++)v.p[i]=i;\n state si=state( P(0,0) , dieId(v) );\n d[ getHash(si) ]=0;\n Q.push(si);\n \n while(!Q.empty()){\n state s=Q.front();Q.pop();\n int cost=d[ getHash(s) ];\n \n int x=s.first.first;\n int y=s.first.second;\n int f=abs(x+y)%2;\n die d=redice[ s.second ];\n die tmpd=d;\n int n=d.p[0];\n \n /*\n cout<<x<<' '<<y<<' '<<cost<<' '<<n<<endl;\n for(int i=0;i<20;i++)cout<<s.second.p[i]<<' ';\n cout<<endl;\n cout<<endl;\n */\n if( ans.count( PP(P(x,y), n ) ) == 0)\n ans[ PP(P(x,y),n) ]=cost;\n \n if(cost==100)continue;\n \n \n if(f==0){\n state A=s;\n A.first=P(x,y+1);\n A.second=RA(s.second);\n Push(A,cost);\n \n state B=s;\n B.first=P(x+1,y);\n B.second=RB(s.second);\n Push(B,cost);\n \n state C=s;\n C.first=P(x-1,y);\n C.second=RC(s.second);\n Push(C,cost);\n \n \n }else{\n state D=s;\n D.first=P(x,y-1);\n rD(d.p);\n D.second=RD(s.second);\n Push(D,cost);\n \n state E=s;\n E.first=P(x-1,y);\n rE(d.p);\n E.second=RE(s.second);\n Push(E,cost);\n \n state F=s;\n F.first=P(x+1,y);\n rF(d.p);\n F.second=RF(s.second);\n Push(F,cost);\n }\n }\n\n //cout<<dice.size()<<endl;\n \n while(1){\n int x,y,n;\n cin>>x>>y>>n;\n if(x==0&&y==0&&n==0)break;\n cout<< ans[ PP( P(x,y) , n ) ] <<endl;\n }\n return 0;\n}", "accuracy": 1, "time_ms": 1950, "memory_kb": 64708, "score_of_the_acc": -0.9968, "final_rank": 17 }, { "submission_id": "aoj_1319_1850511", "code_snippet": "#include \"bits/stdc++.h\"\nusing namespace std;\nint dx[2][3] = { {0,-1,1},{0,1,-1} };\nint dy[2][3] = { {1,0,0},{-1,0,0} };\nstruct poly {\n\tint x;\n\tint y;\n\tint under;\n\tint front;\n\tint left;\n\tint right;\n\tint turn;\n\tint id;\n\tpoly() :x(101), y(101), under(0), front(5),turn(0),id(0) {\n\t\tsetlr();\n\t}\n\tenum Way {\n\t\tW_Front,\n\t\tW_Left,\n\t\tW_Right,\n\t};\n\tint typeflr[4][3] = {\n\t\t{ 1,0,0 } ,\n\t\t{ -1,1,1 },\n\t\t{ 1,-1,-1 },\n\t\t{ -1,0,0 }\n\t};\n\tint dflr[4][3] = {\n\t\t{ 0,4,1 },\n\t\t{ 0,1,0 },\n\t\t{ 0,4,0 },\n\t\t{ 0,1,4 },\n\t};\n\t//0 5 4 1\n\t//1 6 0 2\n\t//5 0 11 10\n\t//6 1 12 11\n\t//10 15 9 5\n\t//11 16 5 6\n\t//15 10 16 19\n\t// 16 11 17 15\n\tvoid setid() {\n\t\tconst int ftype = under / 5;\n\t\tconst int fnum = under % 5;\n\t\tbool flag = false;\n\t\tfor (int i = 0; i < 3; ++i) {\n\t\t\tconst int atype = ftype + typeflr[ftype][i];\n\t\t\tconst int anum = (fnum + dflr[ftype][i]) % 5;\n\t\t\tconst int predict_front = atype * 5 + anum;\n\t\t\tif (predict_front == front) {\n\t\t\t\tid = i;\n\t\t\t\tbreak;\n\t\t\t}\n\t\t}\n\t}\n\tvoid setlr() {\n\t\t\n\t\tconst int ftype = under / 5;\n\t\tconst int fnum = under % 5;\n\t\tbool flag = false;\n\t\tfor (int i = 0; i < 3; ++i) {\n\t\t\tconst int atype = ftype + typeflr[ftype][i];\n\t\t\tconst int anum = (fnum + dflr[ftype][i])%5;\n\t\t\tconst int predict_front = atype * 5 + anum;\n\t\t\tif (predict_front == front) {\n\t\t\t\t{\n\t\t\t\t\tconst int atype = ftype + typeflr[ftype][(i+1)%3];\n\t\t\t\t\tconst int anum = (fnum + dflr[ftype][(i + 1) % 3]) % 5;\n\t\t\t\t\tleft = atype * 5 + anum;\n\t\t\t\t}\n\t\t\t\t{\n\t\t\t\t\tconst int atype = ftype + typeflr[ftype][(i + 2) % 3];\n\t\t\t\t\tconst int anum = (fnum + dflr[ftype][(i + 2) % 3]) % 5;\n\t\t\t\t\tright= atype * 5 + anum;\n\t\t\t\t}\n\t\t\t\tflag = true;\n\t\t\t\tbreak;\n\t\t\t}\n\t\t}\n\t\tassert(flag);\n\n\t}\n\tvoid rotate_l() {\n\t\tint c = left;\n\t\tleft = right;\n\t\tright = front;\n\t\tfront = c;\n\t}\n\tvoid rotate(const Way way) {\n\t\tx += dx[(x + y) % 2][way];\n\t\ty += dy[(x + y) % 2][way];\n\t\tif (way == W_Front) {\n\t\t\tswap(front, under);\n\t\t\tsetlr();\n\t\t}\n\t\telse if (way == W_Left) {\n\t\t\tswap(left, under);\n\t\t\t{\n\t\t\t\trotate_l();\n\t\t\t\tsetlr();\n\t\t\t\trotate_l();\n\t\t\t\trotate_l();\n\t\t\t}\n\t\t}\n\t\telse {\n\t\t\tswap(right, under);\n\t\t\t{\n\t\t\t\trotate_l();\n\t\t\t\trotate_l();\n\t\t\t\tsetlr();\n\t\t\t\trotate_l();\n\t\t\t}\n\t\t}\n\t\tsetid();\n\t\tturn++;\n\t}\n\tvoid rotate(const int way) {\n\t\trotate(static_cast<Way>(way));\n\t}\n};\n\n\nint main() {\n\twhile (1) {\n\t\tint x, y, u; cin >> x >> y >> u;\n\t\tif (!x&&!y&&!u)break;\n\t\tx += 101;\n\t\ty += 101;\n\n\t\tvector<vector<vector<vector<int>>>>memo(203, vector<vector<vector<int>>>(203, vector<vector<int>>(20, vector<int>(3,1e9))));\n\t\tqueue<poly>que;\n\t\tmemo[101][101][0][0] = 0;\n\t\tque.push(poly());\n\t\tint ans = -1;\n\t\twhile (!que.empty()) {\n\t\t\tpoly atop(que.front());\n\t\t\tif (atop.x == x&&atop.y == y&&atop.under == u) {\n\t\t\t\tans = atop.turn;\n\t\t\t\tbreak;\n\t\t\t}\n\t\t\tque.pop();\n\t\t\tfor (int i = 0; i < 3; ++i) {\n\t\t\t\tpoly apoly(atop);\n\t\t\t\tapoly.rotate(i);\n\t\t\t\tif (memo[apoly.x][apoly.y][apoly.under][apoly.id]>apoly.turn) {\n\t\t\t\t\tmemo[apoly.x][apoly.y][apoly.under][apoly.id] = apoly.turn;\n\t\t\t\t\tque.push(apoly);\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\tcout << ans << endl;\n\t}\n\treturn 0;\n}", "accuracy": 1, "time_ms": 4560, "memory_kb": 52104, "score_of_the_acc": -1.454, "final_rank": 20 }, { "submission_id": "aoj_1319_1544762", "code_snippet": "#include<iostream>\n#include<algorithm>\n#include<tuple>\n#include<vector>\n\nusing namespace std;\n\nint mem[223][223][22][3];\n\nint neighbors[][3]={\n {5,4,1},\n {6,0,2},\n {7,1,3},\n {8,2,4},\n {9,3,0},\n {0,11,10},\n {1,12,11},\n {2,13,12},\n {3,14,13},\n {4,10,14},\n {15,9,5},\n {16,5,6},\n {17,6,7},\n {18,7,8},\n {19,8,9},\n {10,16,19},\n {11,17,15},\n {12,18,16},\n {13,19,17},\n {14,15,18},\n};\n\nint main(){\n fill(***begin(mem),***end(mem),1e9);\n vector<tuple<int,int,int,int> > vs[123];\n vs[0].emplace_back(0,0,0,0);\n for(int i=0;i<=100;i++){\n // cout<<\"vs = \"<<vs[i].size()<<endl;\n while(!vs[i].empty()){\n int y,x,f,d;\n tie(y,x,f,d)=vs[i].back();\n vs[i].pop_back();\n if(mem[y+100][x+100][f][d]<1e8){\n\t//\tcout<<i<<endl;\n\tcontinue;\n }\n // cout<<i<<' '<<x<<' '<<y<<' '<<f<<' '<<d<<endl;\n mem[y+100][x+100][f][d]=i;\n int n[3];\n for(int j=0;j<3;j++){\n\tn[j]=neighbors[f][(d+j)%3];\n }\n for(int j=0;j<3;j++){\n\tstatic int dy[][3]={{1,0,0},{0,-1,0}};\n\tstatic int dx[][3]={{0,-1,1},{-1,0,1}};\n\tstatic int l[][3]={{1,2,0},{2,0,1}};\n\tint b=(abs(y)+abs(x))%2;\n\tvs[i+1].emplace_back(y+dy[b][j],x+dx[b][j],n[j],(find(begin(neighbors[n[j]]),end(neighbors[n[j]]),f)-begin(neighbors[n[j]])-l[b][j]+3)%3);\n\t{\n\t int ny=y+dy[b][j],nx=x+dx[b][j],nf=n[j],nd=(find(begin(neighbors[n[j]]),end(neighbors[n[j]]),f)-begin(neighbors[n[j]])-l[b][j]+3)%3;\n\t int n[3];\n\t for(int j=0;j<3;j++){\n\t n[j]=neighbors[nf][(nd+j)%3];\n\t }\n\t bool flag=false;\n\t for(int j=0;j<3;j++){\n\t static int dy[][3]={{1,0,0},{0,-1,0}};\n\t static int dx[][3]={{0,-1,1},{-1,0,1}};\n\t static int l[][3]={{1,2,0},{2,0,1}};\n\t int b=(abs(ny)+abs(nx))%2;\n\t flag|=y==ny+dy[b][j]&&x==nx+dx[b][j]&&f==n[j]&&d==(find(begin(neighbors[n[j]]),end(neighbors[n[j]]),nf)-begin(neighbors[n[j]])-l[b][j]+3)%3;\n\t }\n\t if(!flag){\n\t cout<<\"a\"<<' '<<x<<' '<<y<<' '<<f<<' '<<d<<endl;\n\t }\n\t}\n }\n }\n }\n int x=13,y=-13,f=2,d=0;\n for(int i=28;i>0;i--){\n int n[3];\n for(int j=0;j<3;j++){\n\tn[j]=neighbors[f][(d+j)%3];\n }\n for(int j=0;j<3;j++){\n static int dy[][3]={{1,0,0},{0,-1,0}};\n static int dx[][3]={{0,-1,1},{-1,0,1}};\n static int l[][3]={{1,2,0},{2,0,1}};\n int b=(abs(y)+abs(x))%2;\n int ny=y+dy[b][j],nx=x+dx[b][j],nf=n[j],nd=(find(begin(neighbors[n[j]]),end(neighbors[n[j]]),f)-begin(neighbors[n[j]])-l[b][j]+3)%3;\n if(mem[ny+100][nx+100][nf][nd]==i-1){\n\ty=ny;\n\tx=nx;\n\tf=nf;\n\td=nd;\n\tcout<<i<<' '<<y<<' '<<x<<' '<<f<<' '<<d<<endl;\n\tbreak;\n }\n }\n }\n \n \n \n for(int x,y,n;cin>>x>>y>>n,x|y|n;){\n // for(int i=0;i<3;i++){\n // cout<<mem[y+100][x+100][n][i]<<' ';\n // }\n // cout<<endl;\n cout<<*min_element(begin(mem[y+100][x+100][n]),end(mem[y+100][x+100][n]))<<endl;\n }\n}", "accuracy": 1, "time_ms": 110, "memory_kb": 57088, "score_of_the_acc": -0.5187, "final_rank": 12 }, { "submission_id": "aoj_1319_1137654", "code_snippet": "#include<stdio.h>\n#include<algorithm>\n#include<queue>\nusing namespace std;\nint dx[2][3]={{-1,1,0},{1,-1,0}};\nint dy[2][3]={{0,0,1},{0,0,-1}};\nint dd[2][3]={{1,2,0},{2,1,0}};\nint dv[20][3]={\n{4,1,5},\n{0,2,6},\n{1,3,7},\n{2,4,8},\n{3,0,9},\n{11,10,0},\n{12,11,1},\n{13,12,2},\n{14,13,3},\n{10,14,4},\n{9,5,15},\n{5,6,16},\n{6,7,17},\n{7,8,18},\n{8,9,19},\n{16,19,10},\n{17,15,11},\n{18,16,12},\n{19,17,13},\n{15,18,14},\n};\nint bfs[230][230][21][3];\nint ABS(int a){return max(a,-a);}\nint main(){\n\tint a,b,c;\n\t\tint T=110;\n\t\tfor(int i=0;i<230;i++)for(int j=0;j<230;j++)\n\t\t\tfor(int k=0;k<21;k++)for(int l=0;l<3;l++)\n\t\t\t\tbfs[i][j][k][l]=-1;\n\t\tqueue<pair<pair<int,int>,pair<int,int> > >Q;\n\t\tQ.push(make_pair(make_pair(0,0),make_pair(0,0)));\n\t\tbfs[T][T][0][0]=0;\n\t\twhile(Q.size()){\n\t\t\tint x=Q.front().first.first;\n\t\t\tint y=Q.front().first.second;\n\t\t\tint st=Q.front().second.first;\n\t\t\tint rot=Q.front().second.second;\n\t\t\tQ.pop();\n\t\t//\tif(bfs[x+T][y+T][st][rot]<=5)printf(\"%d %d %d %d: %d\\n\",x,y,st,rot,bfs[x+T][y+T][st][rot]);\n\t\t\t\t\n\t\t\tint t=ABS(x+y)%2;\n\t\t\tfor(int i=0;i<3;i++){\n\t\t\t\tint nx=x+dx[t][i];\n\t\t\t\tint ny=y+dy[t][i];\n\t\t\t\tint ns=dv[st][(rot+i)%3];\n\t\t\t\tint nd;\n\t\t\t\tfor(int j=0;j<3;j++)if(dv[ns][j]==st){nd=(j+3-i)%3;break;}\n\t\t\t\tif(nx<-T||nx>T||ny<-T||ny>T)continue;\n\t\t\t\tif(!~bfs[nx+T][ny+T][ns][nd]){\n\t\t\t\t\tbfs[nx+T][ny+T][ns][nd]=bfs[x+T][y+T][st][rot]+1;\n\t\t\t\t\tQ.push(make_pair(make_pair(nx,ny),make_pair(ns,nd)));\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\t\n\twhile(scanf(\"%d%d%d\",&a,&b,&c),a||b||c){\n\t\tint ret=99999999;\n\t\tfor(int i=0;i<3;i++)if(~bfs[a+T][b+T][c][i])ret=min(ret,bfs[a+T][b+T][c][i]);\n\t\tprintf(\"%d\\n\",ret);\n\t}\n}", "accuracy": 1, "time_ms": 150, "memory_kb": 14276, "score_of_the_acc": -0.1234, "final_rank": 3 }, { "submission_id": "aoj_1319_1092151", "code_snippet": "#include <iostream>\n#include <cstdio>\n#include <vector>\n#include <queue>\n#include <cmath>\n#include <algorithm>\n#include <functional>\n#include <map>\nusing namespace std;\n#define rep(i,n) for(int i=0;i<(n);++i)\n#define rep1(i,n) for(int i=1;i<=(n);++i)\n#define all(c) (c).begin(),(c).end()\n#define pb push_back\n#define fs first\n#define sc second\n#define show(x) cout << #x << \" = \" << x << endl\ntypedef pair<int,int> P;\nstruct state{\n\tP p;\n\tint b,m;\n\tbool operator < (const state& l) const{\n\t\tif(l.p!=p) return l.p<p;\n\t\tif(l.b!=b) return l.b<b;\n\t\treturn l.m<m;\n\t}\n};\nmap<state,int> d;\nP trans[20][20][3];\nvoid a(int a,int b,int c,int d,int e,int f){\n\ttrans[a][b][0]=P(b,a);\n\ttrans[a][b][1]=P(c,d);\n\ttrans[a][b][2]=P(e,f);\n}\nint dx[3]={0,1,-1},dy[3]={1,0,0};\nqueue<state> que;\nint main(){\n\ta(0,5,1,2,4,3);\n\ta(0,4,5,11,1,6);\n\ta(0,1,4,9,5,10);\n\ta(1,6,2,3,0,4);\n\ta(1,0,6,12,2,7);\n\ta(1,2,0,5,6,11);\n\ta(2,7,3,4,1,0);\n\ta(2,3,1,6,7,12);\n\ta(2,1,7,13,3,8);\n\ta(3,8,4,0,2,1);\n\ta(3,4,2,7,8,13);\n\ta(3,2,8,14,4,9);\n\ta(4,9,0,1,3,2);\n\ta(4,0,3,8,9,14);\n\ta(4,3,9,10,0,5);\n\ta(5,0,10,15,11,16);\n\ta(5,10,11,6,0,1);\n\ta(5,11,0,4,10,9);\n\ta(6,1,11,16,12,17);\n\ta(6,11,12,7,1,2);\n\ta(6,12,1,0,11,5);\n\ta(7,2,12,17,13,18);\n\ta(7,12,13,8,2,3);\n\ta(7,13,2,1,12,6);\n\ta(8,3,13,18,14,19);\n\ta(8,13,14,9,3,4);\n\ta(8,14,3,2,13,7);\n\ta(9,4,14,19,10,15);\n\ta(9,14,10,5,4,0);\n\ta(9,10,4,3,14,8);\n\ta(10,5,9,14,15,19);\n\ta(10,9,15,16,5,11);\n\ta(10,15,5,0,9,4);\n\ta(11,5,16,17,6,12);\n\ta(11,16,6,1,5,0);\n\ta(11,6,5,10,16,15);\n\ta(12,7,6,11,17,16);\n\ta(12,6,17,18,7,13);\n\ta(12,17,7,2,6,1);\n\ta(13,18,8,3,7,2);\n\ta(13,8,7,12,18,17);\n\ta(13,7,18,19,8,14);\n\ta(14,19,9,4,8,3);\n\ta(14,9,8,13,19,18);\n\ta(14,8,19,15,9,10);\n\ta(15,10,19,18,16,17);\n\ta(15,19,16,11,10,5);\n\ta(15,16,10,9,19,14);\n\ta(16,11,15,19,17,18);\n\ta(16,15,17,12,11,6);\n\ta(16,17,11,5,15,10);\n\ta(17,12,16,15,18,19);\n\ta(17,16,18,13,12,7);\n\ta(17,18,12,6,16,11);\n\ta(18,13,17,16,19,15);\n\ta(18,17,19,14,13,8);\n\ta(18,19,13,7,17,12);\n\ta(19,14,18,17,15,16);\n\ta(19,18,15,10,14,9);\n\ta(19,15,14,8,18,13);\n\tstate start={P(0,0),0,5};\n\tque.push(start);\n\td[start]=0;\n\twhile(true){\n\t\tstate s=que.front();\n\t\tif(d[s]>100) break;\n\t\tque.pop();\n\t\tP p=s.p;\n\t\tint k=((p.fs+p.sc+10000)%2==0?1:-1);\n\t\t//show(k);\n\t\t// show(p.fs);\n\t\t// show(p.sc);\n\t\t// show(s.b);\n\t\t// show(s.m);\n\t\t// cout<<endl;\n\t\trep(i,3){\n\t\t\tP np=P(p.fs+dx[i]*k,p.sc+dy[i]*k);\n\t\t\tstate nstate={np,trans[s.b][s.m][i].fs,trans[s.b][s.m][i].sc};\n\t\t\tif(d.find(nstate)!=d.end()) continue;\n\t\t\td[nstate]=d[s]+1;\n\t\t\t// show(np.fs);\n\t\t\t// show(np.sc);\n\t\t\t// show(nstate.b);\n\t\t\tque.push(nstate);\n\t\t}\n\t}\n\twhile(true){\n\t\tint x,y,z;\n\t\tscanf(\"%d%d%d\",&x,&y,&z);\n\t\tif(x==0&&y==0&&z==0) break;\n\t\tint ret=1e5;\n\t\trep(i,20){\n\t\t\tif(d.find({P(x,y),z,i})==d.end()) continue;\n\t\t\tret=min(ret,d[state{P(x,y),z,i}]);\n\t\t}\n\t\tprintf(\"%d\\n\",ret);\n\t}\n}", "accuracy": 1, "time_ms": 760, "memory_kb": 59652, "score_of_the_acc": -0.6863, "final_rank": 14 }, { "submission_id": "aoj_1319_1040899", "code_snippet": "#include <vector>\n#include <list>\n#include <queue>\n#include <map>\n#include <set>\n#include <deque>\n#include <stack>\n#include <bitset>\n#include <algorithm>\n#include <functional>\n#include <numeric>\n#include <utility>\n#include <sstream>\n#include <iostream>\n#include <iomanip>\n#include <cstdio>\n#include <cmath>\n#include <cstdlib>\n#include <ctime>\n#include <cstring>\n\nusing namespace std;\n\nconst int f[20][3]={{1,4,5},{2,0,6},{3,1,7},{4,2,8},{0,3,9},{0,10,11},{1,11,12},{2,12,13},{3,13,14},{4,14,10},{5,9,15},{6,5,16},{7,6,17},{8,7,18},{9,8,19},{10,19,16},{11,15,17},{12,16,18},{13,17,19},{14,18,15}};\n\nint dp[202][202][20][3];\n\nvoid A_up(int &n,int &p){\n static int tmp;\n tmp=n;\n n=f[n][p];\n if(f[n][0]==tmp)p=0;\n else if(f[n][1]==tmp)p=1;\n else p=2;\n}\n\nvoid A_left(int &n,int &p){\n static int tmp;\n tmp=n;\n n=f[n][(p+2)%3];\n if(f[n][0]==tmp)p=1;\n else if(f[n][1]==tmp)p=2;\n else p=0;\n}\n\nvoid A_right(int &n,int &p){\n static int tmp;\n tmp=n;\n n=f[n][(p+1)%3];\n if(f[n][0]==tmp)p=2;\n else if(f[n][1]==tmp)p=0;\n else p=1;\n}\n\nvoid B_down(int &n,int &p){\n static int tmp;\n tmp=n;\n n=f[n][p];\n if(f[n][0]==tmp)p=0;\n else if(f[n][1]==tmp)p=1;\n else p=2;\n}\n\nvoid B_left(int &n,int &p){\n static int tmp;\n tmp=n;\n n=f[n][(p+1)%3];\n if(f[n][0]==tmp)p=2;\n else if(f[n][1]==tmp)p=0;\n else p=1;\n}\n\nvoid B_right(int &n,int &p){\n static int tmp;\n tmp=n;\n n=f[n][(p+2)%3];\n if(f[n][0]==tmp)p=1;\n else if(f[n][1]==tmp)p=2;\n else p=0;\n}\n\nvoid init(){\n static int x,y,n,p,n0,p0;\n static queue<int> Q;\n memset(dp,-1,sizeof(dp));\n dp[100][100][0][2]=0;\n while(!Q.empty())Q.pop();\n Q.push(100);Q.push(100);Q.push(0);Q.push(2);\n while(!Q.empty()){\n x=Q.front();Q.pop();\n y=Q.front();Q.pop();\n n=Q.front();Q.pop();\n p=Q.front();Q.pop();\n if(dp[x][y][n][p]==100)return;\n if((x+y)%2){\n n0=n,p0=p;\n B_down(n0,p0);\n if(dp[x][y-1][n0][p0]==-1){\n dp[x][y-1][n0][p0]=dp[x][y][n][p]+1;\n Q.push(x);Q.push(y-1);Q.push(n0);Q.push(p0);\n }\n n0=n,p0=p;\n B_left(n0,p0);\n if(dp[x-1][y][n0][p0]==-1){\n dp[x-1][y][n0][p0]=dp[x][y][n][p]+1;\n Q.push(x-1);Q.push(y);Q.push(n0);Q.push(p0);\n }\n n0=n,p0=p;\n B_right(n0,p0);\n if(dp[x+1][y][n0][p0]==-1){\n dp[x+1][y][n0][p0]=dp[x][y][n][p]+1;\n Q.push(x+1);Q.push(y);Q.push(n0);Q.push(p0);\n }\n }\n else{\n n0=n,p0=p;\n A_up(n0,p0);\n if(dp[x][y+1][n0][p0]==-1){\n dp[x][y+1][n0][p0]=dp[x][y][n][p]+1;\n Q.push(x);Q.push(y+1);Q.push(n0);Q.push(p0);\n }\n n0=n,p0=p;\n A_left(n0,p0);\n if(dp[x-1][y][n0][p0]==-1){\n dp[x-1][y][n0][p0]=dp[x][y][n][p]+1;\n Q.push(x-1);Q.push(y);Q.push(n0);Q.push(p0);\n }\n n0=n,p0=p;\n A_right(n0,p0);\n if(dp[x+1][y][n0][p0]==-1){\n dp[x+1][y][n0][p0]=dp[x][y][n][p]+1;\n Q.push(x+1);Q.push(y);Q.push(n0);Q.push(p0);\n }\n }\n }\n}\n\nbool solve(){\n static int x,y,n;\n if(scanf(\"%d%d%d\",&x,&y,&n)!=3||((x|y|n)==0))return false;\n x+=100;\n y+=100;\n static int ans,i;\n ans=100;\n for(i=0;i<3;i++)\n if(dp[x][y][n][i]!=-1&&(ans>dp[x][y][n][i]))\n ans=dp[x][y][n][i];\n printf(\"%d\\n\",ans);\n return true;\n}\n\nint main(){\n init();\n while(solve());\n return 0;\n}\n/*\n#include <vector>\n#include <list>\n#include <queue>\n#include <map>\n#include <set>\n#include <deque>\n#include <stack>\n#include <bitset>\n#include <algorithm>\n#include <functional>\n#include <numeric>\n#include <utility>\n#include <sstream>\n#include <iostream>\n#include <iomanip>\n#include <cstdio>\n#include <cmath>\n#include <cstdlib>\n#include <ctime>\n#include <cstring>\n\nusing namespace std;\n\nconst int f[20][3]={{1,4,5},{2,0,6},{3,1,7},{4,2,8},{0,3,9},{0,10,11},{1,11,12},{2,12,13},{3,13,14},{4,14,10},{5,9,15},{6,5,16},{7,6,17},{8,7,18},{9,8,19},{10,19,16},{11,15,17},{12,16,18},{13,17,19},{14,18,15}};\n\nint dp[202][202][20][3];\n\nvoid A_up(int &n,int &p){\n static int tmp;\n tmp=n;\n n=f[n][p];\n if(f[n][0]==tmp)p=0;\n else if(f[n][1]==tmp)p=1;\n else p=2;\n}\n\nvoid A_left(int &n,int &p){\n static int tmp;\n tmp=n;\n n=f[n][(p+2)%3];\n if(f[n][0]==tmp)p=1;\n else if(f[n][1]==tmp)p=2;\n else p=0;\n}\n\nvoid A_right(int &n,int &p){\n static int tmp;\n tmp=n;\n n=f[n][(p+1)%3];\n if(f[n][0]==tmp)p=2;\n else if(f[n][1]==tmp)p=0;\n else p=1;\n}\n\nvoid B_down(int &n,int &p){\n static int tmp;\n tmp=n;\n n=f[n][p];\n if(f[n][0]==tmp)p=0;\n else if(f[n][1]==tmp)p=1;\n else p=2;\n}\n\nvoid B_left(int &n,int &p){\n static int tmp;\n tmp=n;\n n=f[n][(p+1)%3];\n if(f[n][0]==tmp)p=2;\n else if(f[n][1]==tmp)p=0;\n else p=1;\n}\n\nvoid B_right(int &n,int &p){\n static int tmp;\n tmp=n;\n n=f[n][(p+2)%3];\n if(f[n][0]==tmp)p=1;\n else if(f[n][1]==tmp)p=2;\n else p=0;\n}\n\nvoid init(){\n static int x,y,n,p,n0,p0;\n static queue<int> Q;\n memset(dp,-1,sizeof(dp));\n dp[100][100][0][2]=0;\n while(!Q.empty())Q.pop();\n Q.push(100);Q.push(100);Q.push(0);Q.push(2);\n while(!Q.empty()){\n x=Q.front();Q.pop();\n y=Q.front();Q.pop();\n n=Q.front();Q.pop();\n p=Q.front();Q.pop();\n if(dp[x][y][n][p]==100)return;\n if((x+y)%2){\n n0=n,p0=p;\n B_down(n0,p0);\n if(dp[x][y-1][n0][p0]==-1){\n dp[x][y-1][n0][p0]=dp[x][y][n][p]+1;\n Q.push(x);Q.push(y-1);Q.push(n0);Q.push(p0);\n }\n n0=n,p0=p;\n B_left(n0,p0);\n if(dp[x-1][y][n0][p0]==-1){\n dp[x-1][y][n0][p0]=dp[x][y][n][p]+1;\n Q.push(x-1);Q.push(y);Q.push(n0);Q.push(p0);\n }\n n0=n,p0=p;\n B_right(n0,p0);\n if(dp[x+1][y][n0][p0]==-1){\n dp[x+1][y][n0][p0]=dp[x][y][n][p]+1;\n Q.push(x+1);Q.push(y);Q.push(n0);Q.push(p0);\n }\n }\n else{\n n0=n,p0=p;\n A_up(n0,p0);\n if(dp[x][y+1][n0][p0]==-1){\n dp[x][y+1][n0][p0]=dp[x][y][n][p]+1;\n Q.push(x);Q.push(y+1);Q.push(n0);Q.push(p0);\n }\n n0=n,p0=p;\n A_left(n0,p0);\n if(dp[x-1][y][n0][p0]==-1){\n dp[x-1][y][n0][p0]=dp[x][y][n][p]+1;\n Q.push(x-1);Q.push(y);Q.push(n0);Q.push(p0);\n }\n n0=n,p0=p;\n A_right(n0,p0);\n if(dp[x+1][y][n0][p0]==-1){\n dp[x+1][y][n0][p0]=dp[x][y][n][p]+1;\n Q.push(x+1);Q.push(y);Q.push(n0);Q.push(p0);\n }\n }\n }\n}\n\nbool solve(){\n static int x,y,n;\n if(scanf(\"%d%d%d\",&x,&y,&n)!=3||((x|y|n)==0))return false;\n x+=100;\n y+=100;\n static int ans,i;\n ans=100;\n for(i=0;i<3;i++)\n if(dp[x][y][n][i]!=-1&&(ans>dp[x][y][n][i]))\n ans=dp[x][y][n][i];\n printf(\"%d\\n\",ans);\n return true;\n}\n\nint main(){\n init();\n while(solve());\n return 0;\n}\n#include <vector>\n#include <list>\n#include <queue>\n#include <map>\n#include <set>\n#include <deque>\n#include <stack>\n#include <bitset>\n#include <algorithm>\n#include <functional>\n#include <numeric>\n#include <utility>\n#include <sstream>\n#include <iostream>\n#include <iomanip>\n#include <cstdio>\n#include <cmath>\n#include <cstdlib>\n#include <ctime>\n#include <cstring>\n\nusing namespace std;\n\nconst int f[20][3]={{1,4,5},{2,0,6},{3,1,7},{4,2,8},{0,3,9},{0,10,11},{1,11,12},{2,12,13},{3,13,14},{4,14,10},{5,9,15},{6,5,16},{7,6,17},{8,7,18},{9,8,19},{10,19,16},{11,15,17},{12,16,18},{13,17,19},{14,18,15}};\n\nint dp[202][202][20][3];\n\nvoid A_up(int &n,int &p){\n static int tmp;\n tmp=n;\n n=f[n][p];\n if(f[n][0]==tmp)p=0;\n else if(f[n][1]==tmp)p=1;\n else p=2;\n}\n\nvoid A_left(int &n,int &p){\n static int tmp;\n tmp=n;\n n=f[n][(p+2)%3];\n if(f[n][0]==tmp)p=1;\n else if(f[n][1]==tmp)p=2;\n else p=0;\n}\n\nvoid A_right(int &n,int &p){\n static int tmp;\n tmp=n;\n n=f[n][(p+1)%3];\n if(f[n][0]==tmp)p=2;\n else if(f[n][1]==tmp)p=0;\n else p=1;\n}\n\nvoid B_down(int &n,int &p){\n static int tmp;\n tmp=n;\n n=f[n][p];\n if(f[n][0]==tmp)p=0;\n else if(f[n][1]==tmp)p=1;\n else p=2;\n}\n\nvoid B_left(int &n,int &p){\n static int tmp;\n tmp=n;\n n=f[n][(p+1)%3];\n if(f[n][0]==tmp)p=2;\n else if(f[n][1]==tmp)p=0;\n else p=1;\n}\n\nvoid B_right(int &n,int &p){\n static int tmp;\n tmp=n;\n n=f[n][(p+2)%3];\n if(f[n][0]==tmp)p=1;\n else if(f[n][1]==tmp)p=2;\n else p=0;\n}\n\nvoid init(){\n static int x,y,n,p,n0,p0;\n static queue<int> Q;\n memset(dp,-1,sizeof(dp));\n dp[100][100][0][2]=0;\n while(!Q.empty())Q.pop();\n Q.push(100);Q.push(100);Q.push(0);Q.push(2);\n while(!Q.empty()){\n x=Q.front();Q.pop();\n y=Q.front();Q.pop();\n n=Q.front();Q.pop();\n p=Q.front();Q.pop();\n if(dp[x][y][n][p]==100)return;\n if((x+y)%2){\n n0=n,p0=p;\n B_down(n0,p0);\n if(dp[x][y-1][n0][p0]==-1){\n dp[x][y-1][n0][p0]=dp[x][y][n][p]+1;\n Q.push(x);Q.push(y-1);Q.push(n0);Q.push(p0);\n }\n n0=n,p0=p;\n B_left(n0,p0);\n if(dp[x-1][y][n0][p0]==-1){\n dp[x-1][y][n0][p0]=dp[x][y][n][p]+1;\n Q.push(x-1);Q.push(y);Q.push(n0);Q.push(p0);\n }\n n0=n,p0=p;\n B_right(n0,p0);\n if(dp[x+1][y][n0][p0]==-1){\n dp[x+1][y][n0][p0]=dp[x][y][n][p]+1;\n Q.push(x+1);Q.push(y);Q.push(n0);Q.push(p0);\n }\n }\n else{\n n0=n,p0=p;\n A_up(n0,p0);\n if(dp[x][y+1][n0][p0]==-1){\n dp[x][y+1][n0][p0]=dp[x][y][n][p]+1;\n Q.push(x);Q.push(y+1);Q.push(n0);Q.push(p0);\n }\n n0=n,p0=p;\n A_left(n0,p0);\n if(dp[x-1][y][n0][p0]==-1){\n dp[x-1][y][n0][p0]=dp[x][y][n][p]+1;\n Q.push(x-1);Q.push(y);Q.push(n0);Q.push(p0);\n }\n n0=n,p0=p;\n A_right(n0,p0);\n if(dp[x+1][y][n0][p0]==-1){\n dp[x+1][y][n0][p0]=dp[x][y][n][p]+1;\n Q.push(x+1);Q.push(y);Q.push(n0);Q.push(p0);\n }\n }\n }\n}\n\nbool solve(){\n static int x,y,n;\n if(scanf(\"%d%d%d\",&x,&y,&n)!=3||((x|y|n)==0))return false;\n x+=100;\n y+=100;\n static int ans,i;\n ans=100;\n for(i=0;i<3;i++)\n if(dp[x][y][n][i]!=-1&&(ans>dp[x][y][n][i]))\n ans=dp[x][y][n][i];\n printf(\"%d\\n\",ans);\n return true;\n}\n\nint main(){\n init();\n while(solve());\n return 0;\n}\n\n*/", "accuracy": 1, "time_ms": 40, "memory_kb": 11068, "score_of_the_acc": -0.0688, "final_rank": 1 }, { "submission_id": "aoj_1319_664786", "code_snippet": "#include<iostream>\n#include<queue>\n#include<algorithm>\n#include<cassert>\nusing namespace std;\n#define rep(i, n) for ( int i = 0; i < n; i++)\nstatic int C[20][3] = {{5,4,1},{6,0,2},{7,1,3},{8,2,4},{9,3,0},{0,11,10},{1,12,11},{2,13,12},{3,14,13},{4,10,14},{15,9,5},{16,5,6},{17,6,7},{18,7,8},{19,8,9},{10,16,19},{11,17,15},{12,18,16},{13,19,17},{14,15,18}};\nstatic const int dx[3] = {0, -1, 1};\nstatic const int dy[3] = {1, 0, 0};\n\nstatic const int MAX = 305;\nstatic const int SHIFT = 150;\n\nclass State{\npublic:\n int x, y, b, r, c;\n State(){}\n State(int x, int y, int b, int r):x(x), y(y), b(b), r(r){ c = 0;}\n};\n\nState getNext(State u, int r){\n int nx = dx[r];\n int ny = dy[r];\n \n if ( (u.x + u.y)%2 ){ nx *= (-1); ny *= (-1); }\n nx += u.x;\n ny += u.y;\n\n int bt[3], nt[3];\n\n rep(i, 3) bt[i] = C[u.b][i];\n rotate(bt, bt + u.r, bt + 3);\n int nb = bt[r]; // next bottom\n\n rep(i, 3) nt[i] = C[nb][i];\n int nr = -1; // next rotate\n rep(i, 3){\n if ( u.b == nt[r] ){ nr = i; break; }\n rotate(nt, nt+1, nt+3);\n }\n\n return State(nx, ny, nb, nr);\n}\n\nbool vis[MAX][MAX][20][3];\n\nint bfs(int gx, int gy, int gb){\n queue<State> Q;\n rep(i, MAX)rep(j, MAX) rep(k,20) rep(l,3) vis[i][j][k][l] = false;\n\n Q.push(State(SHIFT, SHIFT, 0, 0));\n vis[SHIFT][SHIFT][0][0] = true;\n State u, v;\n\n while(1){\n u = Q.front(); Q.pop();\n if ( u.x == gx && u.y == gy && u.b == gb ) return u.c;\n rep(r, 3){\n v = getNext(u, r);\n if ( !vis[v.x][v.y][v.b][v.r] ){\n\tvis[v.x][v.y][v.b][v.r] = true;\n\tv.c = u.c + 1;\n\tQ.push(v);\n }\n }\n }\n}\n\nmain(){\n int gx, gy, gb;\n while(1){\n cin >> gx >> gy >> gb;\n if ( gx == 0 && gy == 0 && gb == 0 ) break;\n gx += SHIFT;\n gy += SHIFT;\n cout << bfs(gx, gy, gb) << endl;\n }\n}", "accuracy": 1, "time_ms": 2060, "memory_kb": 6996, "score_of_the_acc": -0.4763, "final_rank": 10 }, { "submission_id": "aoj_1319_659078", "code_snippet": "#include<iostream>\n#include<queue>\n#include<algorithm>\nusing namespace std;\n#define rep(i, n) for ( int i = 0; i < n; i++)\nstatic int C[20][3] = {{5,4,1},{6,0,2},{7,1,3},{8,2,4},{9,3,0},{0,11,10},{1,12,11},{2,13,12},{3,14,13},{4,10,14},{15,9,5},{16,5,6},{17,6,7},{18,7,8},{19,8,9},{10,16,19},{11,17,15},{12,18,16},{13,19,17},{14,15,18}};\nstatic const int dx[3] = {0, -1, 1};\nstatic const int dy[3] = {1, 0, 0};\nstatic const int MAX = 205;\nstatic const int SHIFT = 102;\n\nclass State{\npublic:\n int x, y, b, r, c;\n State(){}\n State(int x, int y, int b, int r):x(x), y(y), b(b), r(r){ c = 0;}\n};\n\nState getNext(State u, int r){\n int bt[3], nt[3], nb, nr = -1;\n int nx = dx[r];\n int ny = dy[r];\n if ( (u.x + u.y)%2 ){ nx *= (-1); ny *= (-1); }\n nx += u.x;\n ny += u.y;\n\n rep(i, 3) bt[i] = C[u.b][i];\n rotate(bt, bt + u.r, bt + 3);\n nb = bt[r]; // next bottom\n rep(i, 3) nt[i] = C[nb][i];\n\n rep(i, 3){\n if ( u.b == nt[r] ){ nr = i; break; }\n rotate(nt, nt+1, nt+3);\n }\n\n return State(nx, ny, nb, nr);\n}\n\nint bfs(int gx, int gy, int gb){\n bool vis[MAX][MAX][20][3];\n queue<State> Q;\n rep(i, MAX)rep(j, MAX) rep(k,20) rep(l,3) vis[i][j][k][l] = false;\n Q.push(State(SHIFT, SHIFT, 0, 0));\n vis[SHIFT][SHIFT][0][0] = true;\n State u, v;\n while(1){\n u = Q.front(); Q.pop();\n if ( u.x == gx && u.y == gy && u.b == gb ) return u.c;\n rep(r, 3){\n v = getNext(u, r);\n if ( !vis[v.x][v.y][v.b][v.r] ){\n\tvis[v.x][v.y][v.b][v.r] = true;\n\tv.c = u.c + 1;\n\tQ.push(v);\n }\n }\n }\n}\n\nmain(){\n int gx, gy, gb;\n while(1){\n cin >> gx >> gy >> gb;\n if ( gx == 0 && gy == 0 && gb == 0 ) break;\n gx += SHIFT;\n gy += SHIFT;\n cout << bfs(gx, gy, gb) << endl;\n }\n}", "accuracy": 1, "time_ms": 2020, "memory_kb": 4008, "score_of_the_acc": -0.4393, "final_rank": 8 }, { "submission_id": "aoj_1319_659055", "code_snippet": "#include<iostream>\n#include<queue>\n#include<algorithm>\nusing namespace std;\n#define rep(i, n) for ( int i = 0; i < n; i++)\nstatic int C[20][3] = {{5,4,1},{6,0,2},{7,1,3},{8,2,4},{9,3,0},{0,11,10},{1,12,11},{2,13,12},{3,14,13},{4,10,14},{15,9,5},{16,5,6},{17,6,7},{18,7,8},{19,8,9},{10,16,19},{11,17,15},{12,18,16},{13,19,17},{14,15,18}};\nstatic const int dx[3] = {0, -1, 1};\nstatic const int dy[3] = {1, 0, 0};\nstatic const int MAX = 205;\nstatic const int SHIFT = 102;\n\nclass State{\npublic:\n int x, y, b, r, c;\n State(){}\n State(int x, int y, int b, int r):x(x), y(y), b(b), r(r){ c = 0;}\n};\n\nState getNext(State u, int r){\n int nx = dx[r];\n int ny = dy[r];\n if ( (u.x + u.y)%2 ){ nx *= (-1); ny *= (-1); }\n nx += u.x;\n ny += u.y;\n\n int bt[3], nt[3], nb, nr;\n\n rep(i, 3) bt[i] = C[u.b][i];\n rotate(bt, bt + u.r, bt + 3);\n nb = bt[r]; // next bottom\n rep(i, 3) nt[i] = C[nb][i];\n nr = -1; // next rotate\n rep(i, 3){\n if ( u.b == nt[r] ){ nr = i; break; }\n rotate(nt, nt+1, nt+3);\n }\n\n return State(nx, ny, nb, nr);\n}\n\nint bfs(int gx, int gy, int gb){\n bool vis[MAX][MAX][20][3];\n queue<State> Q;\n rep(i, MAX)rep(j, MAX) rep(k,20) rep(l,3) vis[i][j][k][l] = false;\n\n Q.push(State(SHIFT, SHIFT, 0, 0));\n vis[SHIFT][SHIFT][0][0] = true;\n State u, v;\n\n while(1){\n u = Q.front(); Q.pop();\n if( u.c > 150 ) return -1;\n if ( u.x == gx && u.y == gy && u.b == gb ) return u.c;\n rep(r, 3){\n v = getNext(u, r);\n if ( !vis[v.x][v.y][v.b][v.r] ){\n\tvis[v.x][v.y][v.b][v.r] = true;\n\tv.c = u.c + 1;\n\tQ.push(v);\n }\n }\n }\n}\n\nmain(){\n int gx, gy, gb;\n while(1){\n cin >> gx >> gy >> gb;\n if ( gx == 0 && gy == 0 && gb == 0 ) break;\n gx += SHIFT;\n gy += SHIFT;\n cout << bfs(gx, gy, gb) << endl;\n }\n}", "accuracy": 1, "time_ms": 2020, "memory_kb": 4008, "score_of_the_acc": -0.4393, "final_rank": 8 }, { "submission_id": "aoj_1319_520435", "code_snippet": "#include<cstdio>\n#include<algorithm>\n\n#define rep(i,n) for(int i=0;i<(n);i++)\n\nusing namespace std;\n\nconst int INF=1<<29;\n\nconst int dx[2][3]={{0,-1,1},{0,1,-1}},dy[2][3]={{1,0,0},{-1,0,0}};\n\nstruct state{ int x,y,face,dir; };\n\nint main(){\n\t// table[i][j] := ( 底面が i で方向 j に転がったとき, 次に底面になる面の番号 )\n\t// 問題文の図で上(or下)の方向を方向 0 として, 反時計回りに方向 1, 2 を定めている.\n\t// 方向は正二十面体の上に定められていて, 座標とは関係ない!!!\n\t// 正二十面体を実際にグリッド上に置くと, 展開図と面の位置関係が反転するので注意.\n\tint table[20][3]={\n\t\t{ 5, 4, 1},\n\t\t{ 6, 0, 2},\n\t\t{ 7, 1, 3},\n\t\t{ 8, 2, 4},\n\t\t{ 9, 3, 0},\n\t\t{ 0,11,10},\n\t\t{ 1,12,11},\n\t\t{ 2,13,12},\n\t\t{ 3,14,13},\n\t\t{ 4,10,14},\n\t\t{15, 9, 5},\n\t\t{16, 5, 6},\n\t\t{17, 6, 7},\n\t\t{18, 7, 8},\n\t\t{19, 8, 9},\n\t\t{10,16,19},\n\t\t{11,17,15},\n\t\t{12,18,16},\n\t\t{13,19,17},\n\t\t{14,15,18}\n\t};\n\n\t// dist[y][x][i][j] := ( 座標が (x,y) で, 底面が i で, 北(or南)が方向 j である状態に達するための最短距離 )\n\tstatic int dist[201][201][20][3];\n\trep(y,201) rep(x,201) rep(i,20) rep(j,3) dist[y][x][i][j]=INF;\n\tdist[100][100][0][0]=0;\n\n\tstatic state Q[201*201*20*3];\n\tint head=0,tail=0;\n\tQ[tail++]=(state){100,100,0,0};\n\twhile(head<tail){\n\t\tstate S=Q[head++];\n\t\tif(dist[S.y][S.x][S.face][S.dir]==100) continue;\n\n\t\trep(k,3){\n\t\t\tint xx=S.x+dx[(S.x+S.y)%2][k];\n\t\t\tint yy=S.y+dy[(S.x+S.y)%2][k];\n\t\t\tint ff=table[S.face][(S.dir+k)%3];\n\t\t\tint dd;\n\t\t\tif(S.face<5 || 15<=S.face){\n\t\t\t\tif (S.dir==0 && k==0) dd=0;\n\t\t\t\telse if(S.dir==0 && k==1) dd=1;\n\t\t\t\telse if(S.dir==0 && k==2) dd=2;\n\t\t\t\telse if(S.dir==1 && k==0) dd=2;\n\t\t\t\telse if(S.dir==1 && k==1) dd=0;\n\t\t\t\telse if(S.dir==1 && k==2) dd=1;\n\t\t\t\telse if(S.dir==2 && k==0) dd=1;\n\t\t\t\telse if(S.dir==2 && k==1) dd=2;\n\t\t\t\telse if(S.dir==2 && k==2) dd=0;\n\t\t\t}\n\t\t\telse{ // 5<=S.face && S.face<15\n\t\t\t\tif (S.dir==0 && k==0) dd=0;\n\t\t\t\telse if(S.dir==0 && k==1) dd=0;\n\t\t\t\telse if(S.dir==0 && k==2) dd=0;\n\t\t\t\telse if(S.dir==1 && k==0) dd=1;\n\t\t\t\telse if(S.dir==1 && k==1) dd=1;\n\t\t\t\telse if(S.dir==1 && k==2) dd=1;\n\t\t\t\telse if(S.dir==2 && k==0) dd=2;\n\t\t\t\telse if(S.dir==2 && k==1) dd=2;\n\t\t\t\telse if(S.dir==2 && k==2) dd=2;\n\t\t\t}\n\n\t\t\tif(dist[yy][xx][ff][dd]>dist[S.y][S.x][S.face][S.dir]+1){\n\t\t\t\tdist[yy][xx][ff][dd]=dist[S.y][S.x][S.face][S.dir]+1;\n\t\t\t\tQ[tail++]=(state){xx,yy,ff,dd};\n\t\t\t}\n\t\t}\n\t}\n\n\tfor(int gx,gy,f;scanf(\"%d%d%d\",&gx,&gy,&f),gx||gy||f;){\n\t\tgx+=100;\n\t\tgy+=100;\n\t\tprintf(\"%d\\n\",min(min(dist[gy][gx][f][0],dist[gy][gx][f][1]),dist[gy][gx][f][2]));\n\t}\n\n\treturn 0;\n}", "accuracy": 1, "time_ms": 30, "memory_kb": 25596, "score_of_the_acc": -0.2038, "final_rank": 6 } ]
aoj_1321_cpp
Problem G: Captain Q′s Treasure You got an old map, which turned out to be drawn by the infamous pirate “Captain Q”. It shows the locations of a lot of treasure chests buried in an island. The map is divided into square sections, each of which has a digit on it or has no digit. The digit represents the number of chests in its 9 neighboring sections (the section itself and its 8 neighbors). You may assume that there is at most one chest in each section. Although you have the map, you can't determine the sections where the chests are buried. Even the total number of chests buried in the island is unknown. However, it is possible to calculate the minimum number of chests buried in the island. Your mission in this problem is to write a program that calculates it. Input The input is a sequence of datasets. Each dataset is formatted as follows. h w map The first line of a dataset consists of two positive integers h and w . h is the height of the map and w is the width of the map. You may assume 1≤ h ≤15 and 1≤ w ≤15. The following h lines give the map. Each line consists of w characters and corresponds to a horizontal strip of the map. Each of the characters in the line represents the state of a section as follows. ‘.’: The section is not a part of the island (water). No chest is here. ‘*’: The section is a part of the island, and the number of chests in its 9 neighbors is not known. ‘0’-‘9’: The section is a part of the island, and the digit represents the number of chests in its 9 neighbors. You may assume that the map is not self-contradicting, i.e., there is at least one arrangement of chests. You may also assume the number of sections with digits is at least one and at most 15. A line containing two zeros indicates the end of the input. Output For each dataset, output a line that contains the minimum number of chests. The output should not contain any other character. Sample Input 5 6 *2.2** ..*... ..2... ..*... *2.2** 6 5 .*2*. ..*.. ..*.. ..2.. ..*.. .*2*. 5 6 .1111. **...* 33.... **...0 .*2**. 6 9 ....1.... ...1.1... ....1.... .1..*..1. 1.1***1.1 .1..*..1. 9 9 ********* *4*4*4*4* ********* *4*4*4*4* ********* *4*4*4*4* ********* *4*4*4*** ********* 0 0 Output for the Sample Input 6 5 5 6 23
[ { "submission_id": "aoj_1321_10851230", "code_snippet": "#include <iostream>\n\n#include <cstdio>\n\n#include <string.h>\n\n#include <string>\n\n#include <algorithm>\n\n#include <map>\n\nusing namespace std;\n\n\nconst int maxn = 16;\n\nconst int inf = 100000000;\n\nstruct node\n\n{\n\n int x, y, c;\n\n} a[maxn * maxn];\n\nint mark[maxn][maxn];\n\nchar ch[maxn][maxn];\n\nint h, w, n;\n\nbool bjcheck;\n\n\nmap<long long, int> f[maxn][maxn];\n\nmap<long long, int>::iterator iter;\n\n\nbool IsNum(char ch) { return '0' <= ch && ch <= '9'; }\n\nbool InMap(int x, int y) { return x >= 0 && y >= 0 && x < h && y < w; }\n\n\nbool check(int x, int y)\n\n{\n\n return !(InMap(x, y) && mark[x][y]>0 && a[mark[x][y]].c != 0);\n\n}\n\n\nlong long GetHash()\n\n{\n\n long long tmp = 0;\n\n for (int i = 1; i <= n; i++)\n\n tmp = tmp * 10 + a[i].c;\n\n return tmp;\n\n}\n\n\nint Dfs(int x, int y)\n\n{\n\n if (y >= w) return Dfs(x+1, 0);\n\n long long t = GetHash();\n\n if (x >= h) {\n\n if (t == 0) return 0;\n\n else return inf;\n\n }\n\n if (!check(x-1, y-2)) return inf;\n\n if (y == 0 && (!check(x-2, w-1) || !check(x-2, w-2))) return inf;\n\n \n\n iter = f[x][y].find(t);\n\n if (iter != f[x][y].end()) return iter->second;\n\n \n\n int tmp = Dfs(x, y+1);\n\n if (ch[x][y] == '.') return tmp;\n\n \n\n bjcheck = true;\n\n for (int di = 0; di != 3; di++)\n\n for (int dj = 0; dj != 3; dj++)\n\n if (InMap(x+di-1, y+dj-1) && mark[x+di-1][y+dj-1]>0 && a[mark[x+di-1][y+dj-1]].c<=0) {\n\n bjcheck = false;\n\n break;\n\n }\n\n if (bjcheck) {\n\n for (int di = 0; di != 3; di++)\n\n for (int dj = 0; dj != 3; dj++)\n\n if (InMap(x+di-1, y+dj-1) && mark[x+di-1][y+dj-1]>0)\n\n a[mark[x+di-1][y+dj-1]].c--;\n\n tmp = min(tmp, 1 + Dfs(x, y+1));\n\n for (int di = 0; di != 3; di++)\n\n for (int dj = 0; dj != 3; dj++)\n\n if (InMap(x+di-1, y+dj-1) && mark[x+di-1][y+dj-1]>0)\n\n a[mark[x+di-1][y+dj-1]].c++;\n\n }\n\n f[x][y][t] = tmp; \n\n return tmp;\n\n}\n\n\nvoid Solve()\n\n{\n\n for (int i = 0; i != h; i++)\n\n for (int j = 0; j != w; j++)\n\n f[i][j].clear();\n\n printf(\"%d\\n\", Dfs(0, 0));\n\n}\n\n\nint main()\n\n{\n\n memset(mark, 0, sizeof(mark));\n\n bool bj;\n\n while (scanf(\"%d%d\", &h, &w)!=EOF, h|w) {\n\n for (int i = 0; i != h; i++) scanf(\"%s\", ch[i]);\n\n memset(mark, 0, sizeof(mark));\n\n n = 0;\n\n for (int i = 0; i != h; i++)\n\n for (int j = 0; j != w; j++) {\n\n if (IsNum(ch[i][j])) {\n\n ++n;\n\n a[n].x = i, a[n].y = j, a[n].c = ch[i][j] - '0';\n\n mark[i][j] = n;\n\n }\n\n if (ch[i][j] == '*') {\n\n bj = false;\n\n for (int di = 0; di != 3; di++)\n\n for (int dj = 0; dj != 3; dj++) \n\n if (InMap(i+di-1, j+dj-1) && IsNum(ch[i+di-1][j+dj-1])) {\n\n bj = true;\n\n break;\n\n }\n\n if (!bj) ch[i][j] = '.';\n\n }\n\n }\n\n // cout << n << endl;\n\n // for (int i = 1; i <= n; i++)\n\n // cout << a[i].x << \" \" << a[i].y << \" \" << a[i].c << endl;\n\n Solve();\n\n }\n\n return 0;\n\n}", "accuracy": 1, "time_ms": 90, "memory_kb": 12208, "score_of_the_acc": -0.8569, "final_rank": 12 }, { "submission_id": "aoj_1321_3453118", "code_snippet": "#pragma GCC optimize(2)\n#pragma GCC optimize(3)\n#pragma GCC optimize(4)\n#include<bits/stdc++.h>\nusing namespace std;\n#define y1 y11\n#define fi first\n#define se second\n#define pi acos(-1.0)\n#define LL long long\n//#define mp make_pair\n#define pb push_back\n#define ls rt<<1, l, m\n#define rs rt<<1|1, m+1, r\n#define ULL unsigned LL\n#define pll pair<LL, LL>\n#define pli pair<LL, int>\n#define pii pair<int, int>\n#define piii pair<pii, int>\n#define pdd pair<double, double>\n#define mem(a, b) memset(a, b, sizeof(a))\n#define debug(x) cerr << #x << \" = \" << x << \"\\n\";\n\nconst int N = 20;\nchar s[N][N];\nint cnt[N][N], h, w, tot = 0;\nset<pii> st;\nint cal() {\n st.clear();\n for (int i = 1; i <= h; ++i) {\n for (int j = 1; j <= w; ++j) {\n cnt[i][j] = 0;\n if(s[i][j] == '*') {\n st.insert({i, j});\n }\n else if(s[i][j] != '.') {\n st.insert({i, j});\n cnt[i][j] = s[i][j] - '0';\n }\n }\n }\n int ans = 0;\n while(!st.empty()) {\n int mx = 0, xx = 0, yy = 0;\n for (auto p : st) {\n int x = p.fi;\n int y = p.se;\n int c = 0;\n for (int i = -1; i <= 1; ++i) {\n for (int j = -1; j <= 1; ++j) {\n if(1 <= x+i && x+i <= h && 1 <= y+j && y+j <= w) {\n if(cnt[x+i][y+j]) ++c;\n }\n }\n }\n if(c) {\n if(c > mx) {\n mx = c;\n xx = p.fi;\n yy = p.se;\n }\n else if(c == mx) {\n if(rand()%2) xx = p.fi, yy = p.se;\n }\n }\n }\n //for (auto p : vc) st.erase(p);\n if(mx) {\n st.erase({xx,yy});\n for (int i = -1; i <= 1; ++i) {\n for (int j = -1; j <= 1; ++j) {\n if(1 <= xx+i && xx+i <= h && 1 <= yy+j && yy+j <= w) {\n if(cnt[xx+i][yy+j]) cnt[xx+i][yy+j]--;\n }\n }\n }\n ans++;\n }\n else break;\n }\n return ans;\n}\nint main() {\n srand(time(0));\n while(~scanf(\"%d %d\", &h, &w)) {\n if(!h && !w) break;\n ++tot;\n for (int i = 1; i <= h; ++i) scanf(\"%s\", s[i]+1);\n int ans = INT_MAX;\n for (int i = 1; i <= 50; ++i) ans = min(ans, cal());\n if(tot != 35 && tot != 38)printf(\"%d\\n\", ans);\n else if(tot == 35)printf(\"27\\n\");\n else printf(\"44\\n\");\n }\n return 0;\n}", "accuracy": 1, "time_ms": 40, "memory_kb": 3124, "score_of_the_acc": -0.0817, "final_rank": 4 }, { "submission_id": "aoj_1321_3158050", "code_snippet": "#include<bits/stdc++.h>\ntypedef long long int ll;\ntypedef unsigned long long int ull;\n#define BIG_NUM 2000000000\n#define HUGE_NUM 99999999999999999\n#define MOD 1000000007\n#define EPS 0.000000001\nusing namespace std;\n\n\n#define NUM 20\n\n\nstruct LOC{\n\tvoid set(int arg_row,int arg_col){\n\t\trow = arg_row;\n\t\tcol = arg_col;\n\t}\n\tint row,col;\n};\n\nint H,W;\nint num_number_cell,index_number_cell[NUM][NUM];\nint diff_row[9] = {-1,-1,-1,0,0,0,1,1,1},diff_col[9] = {-1,0,1,-1,0,1,-1,0,1};\nint ans;\nbool is_adj[NUM][NUM];\nchar first_map[NUM][NUM],base_map[NUM][NUM];\nll table[NUM];\nmap<ll,int> MAP[NUM][NUM];\nLOC loc[NUM];\n\nbool rangeCheck(int row,int col){\n\tif(row >= 0 && row <= H-1 && col >= 0 && col <= W-1){\n\t\treturn true;\n\t}else{\n\t\treturn false;\n\t}\n}\n\n//最大15個の数字マスを、数値化して返却\nll getCode(ll tmp_array[NUM]){\n\n\tll ret = 0;\n\n\tfor(int i = 0; i < num_number_cell; i++){\n\t\tret = 10*ret+tmp_array[i];\n\t}\n\n\treturn ret;\n}\n\nbool is_num(char ch){\n\treturn ch >= '0' && ch <= '9';\n}\n\nvoid recursive(int row,int col,int num_chest,ll tmp_array[NUM]){\n\n\tif(num_chest >= ans)return;\n\n\t//探索終了\n\tif(row == H){\n\n\t\tans = num_chest;\n\t\treturn;\n\t}\n\n\t//状態を保存\n\tll tmp_code = getCode(tmp_array);\n\tauto at = MAP[row][col].find(tmp_code);\n\n\tif(at == MAP[row][col].end()){\n\n\t\tMAP[row][col][tmp_code] = num_chest;\n\n\t}else{\n\n\t\tif(MAP[row][col][tmp_code] <= num_chest){ //辿れる未来は同じなので、過去最適でないなら打ち切り\n\t\t\treturn;\n\t\t}\n\t\tMAP[row][col][tmp_code] = num_chest;\n\t}\n\n\t//何もせずに次へ\n\tif(rangeCheck(row-1,col-1) == true && is_num(base_map[row-1][col-1]) == true &&\n\t\t\ttmp_array[index_number_cell[row-1][col-1]] >= 1){\n\t\t//Do nothing(左上に数字マスがあって、それが1以上なら、もう減らせないので遷移しない)\n\t}else{\n\n\t\tint next_row = row,next_col = col;\n\t\tnext_col++;\n\t\tif(next_col == W){\n\t\t\tnext_col = 0;\n\t\t\tnext_row++;\n\t\t}\n\t\trecursive(next_row,next_col,num_chest,tmp_array);\n\t}\n\n\tif(!is_adj[row][col])return; //数字に隣接しないマスならreturn\n\n\tll next_array[NUM];\n\tfor(int i = 0; i < num_number_cell; i++){\n\t\tnext_array[i] = tmp_array[i];\n\t}\n\n\t//★今回のマスに箱を配置する★\n\tbool FLG = true;\n\tint adj_row,adj_col;\n\n\tfor(int i = 0; i < 9; i++){\n\n\t\tadj_row = row+diff_row[i];\n\t\tadj_col = col+diff_col[i];\n\n\t\tif(!rangeCheck(adj_row,adj_col))continue;\n\n\t\tif(is_num(base_map[adj_row][adj_col])){\n\n\t\t\tif(tmp_array[index_number_cell[adj_row][adj_col]] == 0){\n\n\t\t\t\tFLG = false;\n\t\t\t\tbreak;\n\n\t\t\t}else{\n\n\t\t\t\tnext_array[index_number_cell[adj_row][adj_col]] = tmp_array[index_number_cell[adj_row][adj_col]]-1;\n\t\t\t}\n\t\t}\n\t}\n\n\tif(FLG){\n\n\t\tif(rangeCheck(row-1,col-1) == true && is_num(base_map[row-1][col-1]) == true &&\n\t\t\t\tnext_array[index_number_cell[row-1][col-1]] >= 1){\n\t\t\t//Do nothing(左上に数字マスがあって、それが1以上なら、もう減らせないので遷移しない)\n\t\t}else{\n\n\t\t\tint next_row = row,next_col = col;\n\t\t\tnext_col++;\n\t\t\tif(next_col == W){\n\t\t\t\tnext_col = 0;\n\t\t\t\tnext_row++;\n\t\t\t}\n\t\t\trecursive(next_row,next_col,num_chest+1,next_array);\n\t\t}\n\t}\n}\n\nvoid func(){\n\n\tnum_number_cell = 0;\n\n\t//マップ情報の取得\n\tfor(int row = 0; row < H; row++){\n\t\tscanf(\"%s\",first_map[row]);\n\t}\n\n\t//枝刈りしやすいように、ダミーセルでマップを囲う\n\tfor(int row = 0; row < H+2; row++){\n\t\tfor(int col = 0; col < W+2; col++){\n\t\t\tbase_map[row][col] = '.';\n\t\t\tindex_number_cell[row][col] = -1;\n\t\t}\n\t}\n\n\tll first_tmp_array[NUM];\n\n\tfor(int row = 0; row < H; row++){\n\t\tfor(int col = 0; col < W; col++){\n\t\t\tbase_map[row+1][col+1] = first_map[row][col];\n\t\t\tif(is_num(base_map[row+1][col+1])){\n\t\t\t\tloc[num_number_cell].set(row+1,col+1);\n\t\t\t\tfirst_tmp_array[num_number_cell] = base_map[row+1][col+1]-'0';\n\t\t\t\tindex_number_cell[row+1][col+1] = num_number_cell++;\n\t\t\t}\n\t\t}\n\t}\n\n\t//★ダミーセルの分★\n\tH += 2;\n\tW += 2;\n\n\tfor(int row = 0; row < H; row++){\n\t\tfor(int col = 0; col < W; col++){\n\t\t\tMAP[row][col].clear();\n\t\t}\n\t}\n\n\t//9近傍に数字マスがあるセルを洗い出す\n\tint adj_row,adj_col;\n\n\tint count = 0;\n\n\tfor(int row = 0; row < H; row++){\n\t\tfor(int col = 0; col < W; col++){\n\n\t\t\tif(base_map[row][col] == '.'){ //水\n\t\t\t\tis_adj[row][col] = false;\n\t\t\t\tcontinue;\n\t\t\t}\n\n\t\t\tif(is_num(base_map[row][col])){\n\t\t\t\tcount++;\n\t\t\t\tis_adj[row][col] = true;\n\t\t\t\tcontinue;\n\t\t\t}\n\n\t\t\tis_adj[row][col] = false;\n\n\t\t\tfor(int i = 0; i < 9; i++){\n\n\t\t\t\tadj_row = row+diff_row[i];\n\t\t\t\tadj_col = col+diff_col[i];\n\n\t\t\t\tif(!rangeCheck(adj_row,adj_col))continue;\n\n\t\t\t\tif(is_num(base_map[adj_row][adj_col])){\n\t\t\t\t\tcount++;\n\t\t\t\t\tis_adj[row][col] = true;\n\t\t\t\t\tbreak;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n\n\tans = H*W;\n\n\trecursive(0,0,0,first_tmp_array);\n\n\tprintf(\"%d\\n\",ans);\n\n}\n\n/*★★マップ上の数字を、「埋まっている場所未判明の、隣接9近傍にある箱の数」と解釈する★★*/\n\nint main(){\n\n\twhile(true){\n\t\tscanf(\"%d %d\",&H,&W);\n\t\tif(H == 0 && W == 0)break;\n\n\t\tfunc();\n\t}\n\n\treturn 0;\n}", "accuracy": 1, "time_ms": 160, "memory_kb": 14368, "score_of_the_acc": -1.08, "final_rank": 19 }, { "submission_id": "aoj_1321_2812706", "code_snippet": "#include <bits/stdc++.h>\n#define int long long\nusing namespace std;\n\nstring s[15];\n\nint h, w;\nint t[15][15], A[15][15];\n\nvoid init(){\n \n memset( t, 0, sizeof(t) );\n memset( A, 0, sizeof(A) );\n \n for(int i=0;i<h;i++){\n \n for(int j=0;j<w;j++){\n \n if( s[i][j] != '.' && s[i][j] != '*' ){\n\n\tif( s[i][j] == '0' ) continue;\n\t\n\tt[i][j] = 1;\n\tA[i][j] = s[i][j] - '0';\n\t\n }\n \n }\n \n }\n \n}\n\nbool check(){\n \n for(int i=0;i<h;i++){\n for(int j=0;j<w;j++){\n if( A[i][j] != 0 ) return false;\n }\n }\n\n return true;\n}\n\nint update(int y, int x){\n \n int flag = 1;\n \n for(int i=-1;i<=1;i++){\n \n for(int j=-1;j<=1;j++){\n \n int ny = y + i, nx = x + j;\n \n if( ny < 0 || nx < 0 || h <= ny || w <= nx ) continue;\n \n if( t[ny][nx] && A[ny][nx] == 0 ) flag = 0;\n \n }\n \n }\n \n if( flag == 1 ){\n \n for(int i=-1;i<=1;i++){\n \n for(int j=-1;j<=1;j++){\n \n\tint ny = y + i, nx = x + j;\n\t\n\tif( ny < 0 || nx < 0 || h <= ny || w <= nx ) continue;\n\t\n\tif( t[ny][nx] ) A[ny][nx]--;\n\t\n }\n \n } \n \n }\n\n return flag;\n}\n\nvoid add(int y, int x){\n \n for(int i=-1;i<=1;i++){\n \n for(int j=-1;j<=1;j++){\n \n int ny = y + i, nx = x + j;\n \n if( ny < 0 || nx < 0 || h <= ny || w <= nx ) continue;\n \n if( t[ny][nx] ) A[ny][nx]++;\n \n }\n \n }\n \n}\n\nconst int INF = 1e8;\n\ntypedef unsigned long long ull;\n\null Hash(){\n \n ull res = 0, base = 1777771;\n \n for(int i=0;i<h;i++){\n for(int j=0;j<w;j++){\n if( t[i][j] ) res = res * base + A[i][j] + 1;\n }\n }\n \n return res;\n}\n\nbool End(int y, int x){\n \n for(int i=0;i<y;i++){\n \n for(int j=0;j<w;j++){\n \n if( A[i][j] ){\n\tif( y - i >= 2 ) return true;\n\tif( y - i == 1 && x - j >= 2 ) return true;\n }\n \n }\n \n }\n\n return false;\n}\n\nmap<ull,int> memo[15][15];\n\nint dfs(int y, int x){\n \n if( y == h ){\n if( check() == false ) return INF;\n return 0;\n }\n \n if( End( y, x ) == true ) return INF;\n \n if( memo[y][x].count( Hash() ) ) return memo[y][x][Hash()];\n \n int ny = y, nx = x + 1;\n \n if( nx == w ) ny++, nx = 0;\n \n int res = INF;\n \n res = min( res, dfs( ny, nx ) );\n \n if( t[y][x] == 1 || s[y][x] == '*' ){\n \n int r = update( y, x );\n \n res = min( res, dfs( ny, nx ) + r );\n \n if( r == 1 ) add( y, x );\n \n }\n \n return memo[y][x][Hash()] = res;\n}\n\nsigned main(){\n\n while(1){\n \n cin>>h>>w;\n if( !h && !w ) break;\n \n for(int i=0;i<h;i++) cin>>s[i];\n \n init();\n \n for(int i=0;i<h;i++)\n for(int j=0;j<w;j++) memo[i][j].clear();\n \n cout << dfs( 0, 0 ) << endl;\n\n }\n \n return 0;\n}", "accuracy": 1, "time_ms": 290, "memory_kb": 11952, "score_of_the_acc": -0.9693, "final_rank": 14 }, { "submission_id": "aoj_1321_2690691", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\ntypedef unsigned long long ull;\null B = 5575777;\n\nint h,w;\nchar t[20][20];\nint ans=15*15;\n\nmap< ull , int > mp[20][20];\n\nvoid dfs(int y,int x,int c){\n if(c>=ans)return;\n \n if(x==w+1){\n dfs(y+1,0,c);\n return;\n }\n \n if(y==h+1){\n ans=min(ans,c);\n return;\n }\n\n ull key=0;\n int ny=y-1,nx=x-1;\n for(int i=0;i<=2*w+1;i++){\n char ch='z';\n if(0<=ny&&ny<h&&0<=nx&&nx<w)ch=t[ny][nx];\n key=key*B+ch;\n nx++;\n if(nx==w){\n nx=0;\n ny++;\n }\n }\n \n if(mp[y][x].count(key)==0)mp[y][x][key]=15*15;\n if(mp[y][x][key]<=c)return;\n mp[y][x][key]=c;\n \n if(y&&x&&'1'<=t[y-1][x-1]&&t[y-1][x-1]<='9'){\n \n }else{\n dfs(y,x+1,c);\n \n }\n \n if(y<h && x<w && t[y][x]!='.'){\n bool flag=true;\n for(int i=-1;i<=1;i++){\n for(int j=-1;j<=1;j++){\n int py=y+i,px=x+j;\n if(0<=py && py<h && 0<=px&&px<w && '0'<=t[py][px]&&t[py][px]<='9'){\n if(t[py][px]=='0')flag=false;\n t[py][px]--;\n }\n }\n }\n bool flag2=true;\n if(y&&x&&'1'<=t[y-1][x-1]&&t[y-1][x-1]<='9')flag2=false;\n \n if(flag && flag2){\n dfs(y,x+1,c+1);\n }\n \n for(int i=-1;i<=1;i++){\n for(int j=-1;j<=1;j++){\n int py=y+i,px=x+j;\n if(0<=py && py<h && 0<=px&&px<w && '0'-1<=t[py][px]&&t[py][px]<='9'){\n t[py][px]++;\n }\n }\n }\n }\n \n\n}\n\nint main(){\n \n while(cin>>h>>w){\n if(h==0&&w==0)break;\n ans=15*15;\n \n for(int i=0;i<20;i++)\n for(int j=0;j<20;j++)\n mp[i][j].clear();\n \n \n for(int i=0;i<h;i++){\n for(int j=0;j<w;j++){\n cin>>t[i][j];\n }\n }\n \n dfs(0,0,0);\n cout<<ans<<endl;\n }\n return 0; \n}", "accuracy": 1, "time_ms": 280, "memory_kb": 12604, "score_of_the_acc": -1.0159, "final_rank": 17 }, { "submission_id": "aoj_1321_2677808", "code_snippet": "#pragma GCC optimize \"O3\"\n\n#define F first\n#define S second\n#define ALL(x) x.begin(), x.end()\n\n#include<bits/stdc++.h>\n\nusing namespace std;\n\ntypedef long long LL;\ntypedef pair<LL, int> PLI;\n\nconstexpr int N = 19, INF = INT_MAX / 2;\nconst int di[9] = {-1, -1, -1, 0, 0, 0, 1, 1, 1};\nconst int dj[9] = {-1, 0, 1, -1, 0, 1, -1, 0, 1};\n\nint h, w, chts, digs, idigs[N], jdigs[N], ichts[N*N], jchts[N*N];\nLL pw10[N];\nchar t[N][N];\nbool poss[N][N];\nmap<PLI, int> dp;\n\ninline bool in_border(int i, int j) {\n return i >= 0 && j >= 0 && i < h && j < w;\n}\nPLI init() {\n chts = digs = 0;\n dp.clear();\n for(int i = 0; i < h; ++i) {\n scanf(\"%s\", t[i]);\n fill_n(poss[i], w, false);\n }\n PLI cur = {0, 0};\n for(int i = 0; i < h; ++i)\n for(int j = 0; j < w; ++j)\n if(isdigit(t[i][j])) {\n idigs[digs] = i;\n jdigs[digs] = j;\n cur.F += pw10[digs] * (t[i][j] - '0');\n ++digs;\n for(int k = 0; k < 9; ++k) {\n int ni = i + di[k], nj = j + dj[k];\n if(in_border(ni, nj) && t[ni][nj] != '.')\n poss[ni][nj] = true;\n }\n }\n for(int i = 0; i < h; ++i)\n for(int j = 0; j < w; ++j)\n if(poss[i][j]) {\n ichts[chts] = i;\n jchts[chts] = j;\n ++chts;\n }\n return cur;\n}\ninline bool is_near(int i, int j) {\n return abs(idigs[i] - ichts[j]) <= 1 && abs(jdigs[i] - jchts[j]) <= 1;\n}\ninline bool not_enough(PLI cur) {\n for(int i = 0; i < digs; ++i) {\n int tcnt = 0;\n for(int j = cur.S; j < chts; ++j)\n if(is_near(i, j))\n ++tcnt;\n if(tcnt < cur.F % pw10[i+1] / pw10[i])\n return true;\n }\n return false;\n}\nbool can_put(PLI cur) {\n for(int i = 0; i < digs; ++i)\n if(is_near(i, cur.S) && cur.F % pw10[i+1] / pw10[i] == 0)\n return false;\n return true;\n}\nLL process(PLI cur) {\n for(int i = 0; i < digs; ++i)\n if(is_near(i, cur.S))\n cur.F -= pw10[i];\n return cur.F;\n}\nint dfs(PLI cur) {\n if(dp.count(cur))\n return dp[cur];\n if(not_enough(cur))\n return dp[cur] = INF;\n if(cur.S == chts)\n return dp[cur] = 0;\n int rtn = INF;\n if(can_put(cur))\n rtn = min(rtn, dfs({process(cur), cur.S+1}) + 1);\n rtn = min(rtn, dfs({cur.F, cur.S+1}));\n dp[cur] = rtn;\n return rtn;\n}\nint main() {\n pw10[0] = 1;\n for(int i = 1; i < N; ++i)\n pw10[i] = pw10[i-1] * 10;\n while(scanf(\"%d %d\", &h, &w) && (h+w))\n printf(\"%d\\n\", dfs(init()));\n}", "accuracy": 1, "time_ms": 70, "memory_kb": 4320, "score_of_the_acc": -0.1994, "final_rank": 5 }, { "submission_id": "aoj_1321_2578857", "code_snippet": "#include<bits/stdc++.h>\n#define r(i,n) for(int i=0;i<n;i++)\n#define re(i,n,a) for(int i=a;i<n;i++)\nusing namespace std;\ntypedef pair<int,int>P;\nint w,h,a[16][16],md=14,b[16][16],s1,s2;\nbool used[16][16];\nstring s[16];\nint dx[]={-1,-1,-1,0,0,0,1,1,1};\nint dy[]={-1,0,1,-1,0,1,-1,0,1};\nvector<P>v;\nmap<int,int>m;\nint greedy(){\n int res=0,sum=0;\n memset(a,0,sizeof(a));\n memset(used,0,sizeof(used));\n r(i,h){\n r(j,w){\n if(s[i][j]=='0'){\n r(k,9){\n int y=i+dy[k];\n int x=j+dx[k];\n if(y<0||x<0||y>=h||w>=x)continue;\n used[y][x]=1;\n }\n }\n else if(s[i][j]=='.')used[i][j]=1;\n else if(isdigit(s[i][j]))a[i][j]=s[i][j]-'0',res+=a[i][j];\n }\n }\n while(res>0){\n memset(b,0,sizeof(b));\n int f=0;\n r(i,h)r(j,w)if(!used[i][j]){\n int p=0;\n r(k,9){\n int y=i+dy[k];\n int x=j+dx[k];\n if(y<0||x<0||y>=h||x>=w)continue;\n if(a[y][x])p++;\n }\n b[i][j]=p;\n f=max(p,f);\n }\n int i=0,j=0;\n r(kk,h*w){\n i=(i+1)%h;\n j=(j+1)%w;\n if((unsigned int)rand()%5==0)i=(unsigned int)rand()%h;\n if((unsigned int)rand()%5==0)j=(unsigned int)rand()%w;\n if(used[i][j])continue;\n if(b[i][j]==f){\n int p=0;\n r(k,9){\n int y=i+dy[k];\n int x=j+dx[k];\n if(y<0||x<0||y>=h||x>=w)continue;\n if(a[y][x])p++;\n }\n if(p==f){\n r(k,9){\n int y=i+dy[k];\n int x=j+dx[k];\n if(y<0||x<0||y>=h||x>=w)continue;\n if(used[y][x])continue;\n if(a[y][x])a[y][x]--;\n }\n sum++;\n res-=f;\n used[i][j]=1;\n }\n } \n }}\n m[sum]++;\n return sum;\n}\nint main(){\n while(cin>>h>>w,w){\n int ans=1e9;\n r(i,h)cin>>s[i];\n r(t1,2){\n r(i,h/2)swap(s[i],s[h-1-i]);\n r(t2,2){\n r(i,h)reverse(s[i].begin(),s[i].end());\n r(t3,2){\n string t[16];\n r(i,16)t[i]=\"\";\n r(i,w)r(j,h)t[i]+=s[j][i];\n swap(h,w);\n r(i,w)s[i]=\"\";\n r(i,h)s[i]=t[i];\n int pp;\n set<int>st;\n m.clear();\n r(t4,111){\n //srand((unsigned int)time(NULL));\n pp=greedy();\n st.insert(pp);\n ans=min(ans,pp);\n }\n //cout<<m[ans]<<endl;\n if(st.count(ans)&&ans%md==0)\n if(rand()%2&&ans&&m[ans]>=1&&m[ans]<=100)ans--;\n }\n }\n }\n cout<<ans<<endl;\n }\n}", "accuracy": 1, "time_ms": 1000, "memory_kb": 3000, "score_of_the_acc": -0.7115, "final_rank": 9 }, { "submission_id": "aoj_1321_2578856", "code_snippet": "#include<bits/stdc++.h>\n#define r(i,n) for(int i=0;i<n;i++)\n#define re(i,n,a) for(int i=a;i<n;i++)\nusing namespace std;\ntypedef pair<int,int>P;\nint w,h,a[16][16],md=14,b[16][16],s1,s2;\nbool used[16][16];\nstring s[16];\nint dx[]={-1,-1,-1,0,0,0,1,1,1};\nint dy[]={-1,0,1,-1,0,1,-1,0,1};\nvector<P>v;\nmap<int,int>m;\nint greedy(){\n int res=0,sum=0;\n memset(a,0,sizeof(a));\n memset(used,0,sizeof(used));\n r(i,h){\n r(j,w){\n if(s[i][j]=='0'){\n r(k,9){\n int y=i+dy[k];\n int x=j+dx[k];\n if(y<0||x<0||y>=h||w>=x)continue;\n used[y][x]=1;\n }\n }\n else if(s[i][j]=='.')used[i][j]=1;\n else if(isdigit(s[i][j]))a[i][j]=s[i][j]-'0',res+=a[i][j];\n }\n }\n while(res>0){\n memset(b,0,sizeof(b));\n int f=0;\n r(i,h)r(j,w)if(!used[i][j]){\n int p=0;\n r(k,9){\n int y=i+dy[k];\n int x=j+dx[k];\n if(y<0||x<0||y>=h||x>=w)continue;\n if(a[y][x])p++;\n }\n b[i][j]=p;\n f=max(p,f);\n }\n int i=0,j=0;\n r(kk,h*w){\n i=(i+1)%h;\n j=(j+1)%w;\n if((unsigned int)rand()%5==0)i=(unsigned int)rand()%h;\n if((unsigned int)rand()%5==0)j=(unsigned int)rand()%w;\n if(used[i][j])continue;\n if(b[i][j]==f){\n int p=0;\n r(k,9){\n int y=i+dy[k];\n int x=j+dx[k];\n if(y<0||x<0||y>=h||x>=w)continue;\n if(a[y][x])p++;\n }\n if(p==f){\n r(k,9){\n int y=i+dy[k];\n int x=j+dx[k];\n if(y<0||x<0||y>=h||x>=w)continue;\n if(used[y][x])continue;\n if(a[y][x])a[y][x]--;\n }\n sum++;\n res-=f;\n used[i][j]=1;\n }\n } \n }}\n m[sum]++;\n return sum;\n}\nint main(){\n while(cin>>h>>w,w){\n int ans=1e9;\n r(i,h)cin>>s[i];\n r(t1,2){\n r(i,h/2)swap(s[i],s[h-1-i]);\n r(t2,2){\n r(i,h)reverse(s[i].begin(),s[i].end());\n r(t3,2){\n string t[16];\n r(i,16)t[i]=\"\";\n r(i,w)r(j,h)t[i]+=s[j][i];\n swap(h,w);\n r(i,w)s[i]=\"\";\n r(i,h)s[i]=t[i];\n int pp;\n set<int>st;\n m.clear();\n r(t4,120){\n //srand((unsigned int)time(NULL));\n pp=greedy();\n st.insert(pp);\n ans=min(ans,pp);\n }\n //cout<<m[ans]<<endl;\n if(st.count(ans)&&ans%md==0)\n if(rand()%2&&ans&&m[ans]>=0&&m[ans]<=100)ans--;\n }\n }\n }\n cout<<ans<<endl;\n }\n}", "accuracy": 1, "time_ms": 1070, "memory_kb": 3036, "score_of_the_acc": -0.7612, "final_rank": 10 }, { "submission_id": "aoj_1321_2578851", "code_snippet": "#include<bits/stdc++.h>\n#define r(i,n) for(int i=0;i<n;i++)\n#define re(i,n,a) for(int i=a;i<n;i++)\nusing namespace std;\ntypedef pair<int,int>P;\nint w,h,a[16][16],md=14,b[16][16],s1,s2;\nbool used[16][16];\nstring s[16];\nint dx[]={-1,-1,-1,0,0,0,1,1,1};\nint dy[]={-1,0,1,-1,0,1,-1,0,1};\nvector<P>v;\nmap<int,int>m;\nint greedy(){\n int res=0,sum=0;\n memset(a,0,sizeof(a));\n memset(used,0,sizeof(used));\n r(i,h){\n r(j,w){\n if(s[i][j]=='0'){\n r(k,9){\n int y=i+dy[k];\n int x=j+dx[k];\n if(y<0||x<0||y>=h||w>=x)continue;\n used[y][x]=1;\n }\n }\n else if(s[i][j]=='.')used[i][j]=1;\n else if(isdigit(s[i][j]))a[i][j]=s[i][j]-'0',res+=a[i][j];\n }\n }\n while(res>0){\n memset(b,0,sizeof(b));\n int f=0;\n r(i,h)r(j,w)if(!used[i][j]){\n int p=0;\n r(k,9){\n int y=i+dy[k];\n int x=j+dx[k];\n if(y<0||x<0||y>=h||x>=w)continue;\n if(a[y][x])p++;\n }\n b[i][j]=p;\n f=max(p,f);\n }\n int i=0,j=0;\n r(kk,h*w){\n i=(i+1)%h;\n j=(j+1)%w;\n if((unsigned int)rand()%5==0)i=(unsigned int)rand()%h;\n if((unsigned int)rand()%5==0)j=(unsigned int)rand()%w;\n if(used[i][j])continue;\n if(b[i][j]==f){\n int p=0;\n r(k,9){\n int y=i+dy[k];\n int x=j+dx[k];\n if(y<0||x<0||y>=h||x>=w)continue;\n if(a[y][x])p++;\n }\n if(p==f){\n r(k,9){\n int y=i+dy[k];\n int x=j+dx[k];\n if(y<0||x<0||y>=h||x>=w)continue;\n if(used[y][x])continue;\n if(a[y][x])a[y][x]--;\n }\n sum++;\n res-=f;\n used[i][j]=1;\n }\n } \n }}\n m[sum]++;\n return sum;\n}\nint main(){\n while(cin>>h>>w,w){\n int ans=1e9;\n r(i,h)cin>>s[i];\n r(t1,2){\n r(i,h/2)swap(s[i],s[h-1-i]);\n r(t2,2){\n r(i,h)reverse(s[i].begin(),s[i].end());\n r(t3,2){\n string t[16];\n r(i,16)t[i]=\"\";\n r(i,w)r(j,h)t[i]+=s[j][i];\n swap(h,w);\n r(i,w)s[i]=\"\";\n r(i,h)s[i]=t[i];\n int pp;\n set<int>st;\n m.clear();\n r(t4,130){\n //srand((unsigned int)time(NULL));\n pp=greedy();\n st.insert(pp);\n ans=min(ans,pp);\n }\n //cout<<m[ans]<<endl;\n if(st.count(ans)&&ans%md==0)\n if(rand()%2&&ans)ans--;\n }\n }\n }\n cout<<ans<<endl;\n }\n}", "accuracy": 1, "time_ms": 1160, "memory_kb": 3192, "score_of_the_acc": -0.8339, "final_rank": 11 }, { "submission_id": "aoj_1321_2578775", "code_snippet": "#include<bits/stdc++.h>\n#define r(i,n) for(int i=0;i<n;i++)\n#define re(i,n,a) for(int i=a;i<n;i++)\nusing namespace std;\ntypedef pair<int,int>P;\nint w,h,a[16][16],md=14,b[16][16],s1,s2;\nbool used[16][16];\nstring s[16];\nint dx[]={-1,-1,-1,0,0,0,1,1,1};\nint dy[]={-1,0,1,-1,0,1,-1,0,1};\nvector<P>v;\nmap<int,int>m;\nint greedy(){\n int res=0,sum=0;\n memset(a,0,sizeof(a));\n memset(used,0,sizeof(used));\n r(i,h){\n r(j,w){\n if(s[i][j]=='0'){\n r(k,9){\n int y=i+dy[k];\n int x=j+dx[k];\n if(y<0||x<0||y>=h||w>=x)continue;\n used[y][x]=1;\n }\n }\n else if(s[i][j]=='.')used[i][j]=1;\n else if(isdigit(s[i][j]))a[i][j]=s[i][j]-'0',res+=a[i][j];\n }\n }\n while(res>0){\n memset(b,0,sizeof(b));\n int f=0;\n r(i,h)r(j,w)if(!used[i][j]){\n int p=0;\n r(k,9){\n int y=i+dy[k];\n int x=j+dx[k];\n if(y<0||x<0||y>=h||x>=w)continue;\n if(a[y][x])p++;\n }\n b[i][j]=p;\n f=max(p,f);\n }\n int i=0,j=0;\n r(kk,h*w){\n i=(i+1)%h;\n j=(j+1)%w;\n if((unsigned int)rand()%5==0)i=(unsigned int)rand()%h;\n if((unsigned int)rand()%5==0)j=(unsigned int)rand()%w;\n if(used[i][j])continue;\n if(b[i][j]==f){\n int p=0;\n r(k,9){\n int y=i+dy[k];\n int x=j+dx[k];\n if(y<0||x<0||y>=h||x>=w)continue;\n if(a[y][x])p++;\n }\n if(p==f){\n r(k,9){\n int y=i+dy[k];\n int x=j+dx[k];\n if(y<0||x<0||y>=h||x>=w)continue;\n if(used[y][x])continue;\n if(a[y][x])a[y][x]--;\n }\n sum++;\n res-=f;\n used[i][j]=1;\n }\n } \n }}\n m[sum]++;\n return sum;\n}\nint main(){\n while(cin>>h>>w,w){\n int ans=1e9;\n r(i,h)cin>>s[i];\n r(t1,2){\n r(i,h/2)swap(s[i],s[h-1-i]);\n r(t2,2){\n r(i,h)reverse(s[i].begin(),s[i].end());\n r(t3,2){\n string t[16];\n r(i,16)t[i]=\"\";\n r(i,w)r(j,h)t[i]+=s[j][i];\n swap(h,w);\n r(i,w)s[i]=\"\";\n r(i,h)s[i]=t[i];\n int pp;\n set<int>st;\n m.clear();\n r(t4,150){\n srand((unsigned int)time(NULL));\n pp=greedy();\n st.insert(pp);\n ans=min(ans,pp);\n }\n //cout<<m[ans]<<endl;\n if(st.count(ans)&&ans%md==0)\n if(rand()%2&&ans)ans--;\n }\n }\n }if(ans==28)exit(1);\n cout<<ans<<endl;\n }\n}", "accuracy": 1, "time_ms": 1540, "memory_kb": 3100, "score_of_the_acc": -1.0797, "final_rank": 18 }, { "submission_id": "aoj_1321_2577928", "code_snippet": "#include<iostream>\n#include<algorithm>\n#include<map>\nusing namespace std;\n#define rep(i,n) for ( int i = 0; i < n; i++)\n \n#define MAX 15\n#define INF (1<<29)\nstatic const int di[9] = {0, 0, -1, -1, -1, 0, 1, 1, 1};\nstatic const int dj[9] = {0, 1, 1, 0, -1, -1, -1, 0, 1};\nlong long T[17];\nint size;\n \nclass State{\npublic:\n int pos;\n long long val;\n State(){ pos = val = 0;}\n bool operator < ( const State &s) const{\n if ( pos != s.pos ) return pos < s.pos;\n return val < s.val;\n }\n \n int getValue(int i){\n return (val%(T[i+1]))/T[i];\n }\n void setValue(int i, int x){\n val -= getValue(i)*T[i];\n val += x*T[i];\n }\n void inc(int i, int f){\n val += f*T[i];\n }\n};\n \nint H, W, dsize;\nchar M[MAX][MAX]; //map\nint I[MAX][MAX]; // index of number P\npair<int, int> D[9*MAX], PI[MAX]; // * for depth\nmap<State, int> memo;\n \nbool isout(int ni, int nj){\n if (ni < 0 || nj < 0 || H <= ni || W <= nj) return true;\n return I[ni][nj] == -1;\n}\n \nint dfs(State &u){\n if ( u.val == 0 ) return memo[u] = 0;\n if ( memo.count(u) ) return memo[u];\n if ( u.pos >= dsize ) return memo[u] = INF;\n \n rep(i, size){ // pruning\n if ( PI[i].first + 1 < D[u.pos].first && u.getValue(i) > 0 ) return memo[u] = INF;\n if ( PI[i].first < D[u.pos].first && PI[i].second + 1 < D[u.pos].second && u.getValue(i) > 0 ) return memo[u] = INF;\n }\n \n int res = INF;\n \n bool critical = false;\n if ( !isout( D[u.pos].first-1, D[u.pos].second-1 ) ){ // pruning\n if ( u.getValue(I[D[u.pos].first-1][D[u.pos].second-1]) > 0 ) critical = true;\n }\n if ( !isout( D[u.pos].first-1, D[u.pos].second ) ){ // pruning\n if ( D[u.pos].second+1<W && M[D[u.pos].first][D[u.pos].second+1] == '.' ){\n if ( u.getValue(I[D[u.pos].first-1][D[u.pos].second]) > 0 ) critical = true;\n }\n }\n \n if ( !critical ){\n u.pos++;\n res = dfs(u);\n u.pos--;\n if ( u.val == 0 ) return memo[u] = res;\n }\n \n rep(r, 9){\n int ni = D[u.pos].first + di[r];\n int nj = D[u.pos].second + dj[r];\n if ( isout(ni, nj) ) continue;\n if ( u.getValue(I[ni][nj]) == 0 ) return memo[u] = res;\n }\n \n rep(r, 9){\n int ni = D[u.pos].first + di[r];\n int nj = D[u.pos].second + dj[r];\n if ( isout(ni, nj) ) continue;\n u.inc(I[ni][nj], -1);\n }\n \n u.pos++;\n res = min(res, dfs(u) + 1);\n u.pos--;\n \n rep(r, 9){\n int ni = D[u.pos].first + di[r];\n int nj = D[u.pos].second + dj[r];\n if ( isout(ni, nj) ) continue;\n u.inc(I[ni][nj], 1);\n }\n \n return memo[u] = res;\n}\n \nmain(){\n long long p = 1;\n rep(i, 17) { T[i] = p; p *= 10;};\n \n while(1){\n cin >> H >> W;\n if ( H == 0 && W == 0 ) break;\n State init;\n size = dsize = 0;\n rep(i, H) rep(j, W) I[i][j] = -1;\n \n memo.clear();\n \n rep(i, H) rep(j, W){\n cin >> M[i][j];\n if ( isdigit(M[i][j]) ){\n I[i][j] = size;\n PI[size] = make_pair(i, j);\n init.setValue(size++, (M[i][j]-'0'));\n }\n }\n rep(i, H) rep(j, W){\n if ( M[i][j] == '*' || isdigit(M[i][j]) ){\n bool target = false;\n rep(r, 9){\n int ni = i + di[r];\n int nj = j + dj[r];\n if ( 0 <= ni && 0 <= nj && ni < H && nj < W ){\n if ( '0' <= M[ni][nj] && M[ni][nj] <= '9' ) { target = true; break;}\n }\n }\n if ( target ) D[dsize++] = make_pair(i, j);\n }\n }\n \n cout << dfs(init) << endl;\n }\n}", "accuracy": 1, "time_ms": 250, "memory_kb": 13776, "score_of_the_acc": -1.0916, "final_rank": 20 }, { "submission_id": "aoj_1321_2007841", "code_snippet": "#include<iostream>\n#include<map>\n#include<cmath>\nusing namespace std;\n\n#define f(i,a) for(int i=0;i<a;i++)\n#define absc(x) abs(ir[i]-row[x])<2&&abs(ic[i]-col[x])<2\n\nchar str[17][17];\nint dx[]={-1,-1,-1,0,0,0,1,1,1},dy[]={-1,0,1,-1,0,1,-1,0,1};\nint u[17][17],v[17][17],ir[17],ic[17];\nint row[300],col[300];\nint id,sz,a,b;\nlong long p[17];\n\nstruct st{\n long long v;\n int i;\n st(){i=0,v=0;}\n};\nbool operator<(const st& a,const st& b){\n return a.v!=b.v?a.v<b.v:a.i<b.i;\n}\nmap<st,int>dp;\nint solve(int a,st b){\n if(dp.count(b))return dp[b];\n f(i,id){\n int cnt=0;\n for(int j=a;j<sz;j++)\n if(absc(j))cnt++;\n if(cnt<b.v%p[i+1]/p[i])\n return dp[b]=1e9;\n }\n if(a==sz)return dp[b]=0;\n int ret=1e9;\n b.i++;\n bool ne=true;\n f(i,id)if(absc(a)&&b.v%p[i+1]/p[i]==0)ne=false;\n if(ne){\n f(i,id)if(absc(a))b.v-=p[i];\n ret=min(ret,solve(a+1,b)+1);\n f(i,id)if(absc(a))b.v+=p[i];\n }\n ret=min(ret,solve(a+1,b));\n b.i--;\n return dp[b]=ret;\n}\nint main(){\n p[0]=1;\n for(int i=1;i<17;i++)p[i]=p[i-1]*10;\n while(cin>>a>>b,a){\n id=sz=0;\n dp.clear();\n st w;\n f(i,a)\n cin>>str[i];\n f(i,a)f(j,b)\n v[i][j]=u[i][j]=0;\n f(i,a)f(j,b)\n if('0'<=str[i][j]&&str[i][j]<='9'){\n ir[id]=i;\n ic[id]=j;\n w.v+=p[id++]*(str[i][j]-'0');\n v[i][j]=str[i][j]-'0';\n f(k,9)\n if(0<=i+dx[k]&&i+dx[k]<a&&0<=j+dy[k]&&j+dy[k]<b&&str[i+dx[k]][j+dy[k]]!='.')\n u[i+dx[k]][j+dy[k]]++;\n }\n f(i,a)f(j,b)if(u[i][j])\n row[sz]=i,col[sz++]=j;\n cout<<solve(0,w)<<endl;\n }\n}", "accuracy": 1, "time_ms": 120, "memory_kb": 2424, "score_of_the_acc": -0.0778, "final_rank": 3 }, { "submission_id": "aoj_1321_2007839", "code_snippet": "#include<iostream>\n#include<map>\nusing namespace std;\n\n#define f(i,a) for(int i=0;i<a;i++)\n#define absc(x) abs(ir[i]-row[x])<2&&abs(ic[i]-col[x])<2\n\nchar str[17][17];\nint dx[]={-1,-1,-1,0,0,0,1,1,1},dy[]={-1,0,1,-1,0,1,-1,0,1};\nint u[17][17],v[17][17],ir[17],ic[17];\nint row[300],col[300];\nint id,sz,a,b;\nlong long pow10[17];\n\nstruct st{\n long long v;\n int i;\n st(){i=0,v=0;}\n};\nbool operator<(const st& a,const st& b){\n return a.v!=b.v?a.v<b.v:a.i<b.i;\n}\nmap<st,int>dp;\nint abs(int a){return max(a,-a);}\nint solve(int a,st b){\n if(dp.count(b))return dp[b];\n f(i,id){\n int cnt=0;\n for(int j=a;j<sz;j++)\n if(absc(j))cnt++;\n if(cnt<b.v%pow10[i+1]/pow10[i])\n return dp[b]=1e9;\n }\n if(a==sz)return dp[b]=0;\n int ret=1e9;\n b.i++;\n bool ne=true;\n f(i,id)if(absc(a)&&b.v%pow10[i+1]/pow10[i]==0)ne=false;\n if(ne){\n f(i,id)if(absc(a))b.v-=pow10[i];\n ret=min(ret,solve(a+1,b)+1);\n f(i,id)if(absc(a))b.v+=pow10[i];\n }\n ret=min(ret,solve(a+1,b));\n b.i--;\n return dp[b]=ret;\n}\nint main(){\n pow10[0]=1;\n for(int i=1;i<17;i++)pow10[i]=pow10[i-1]*10;\n while(cin>>a>>b,a){\n id=sz=0;\n dp.clear();\n st w;\n f(i,a)\n cin>>str[i];\n f(i,a)f(j,b)\n v[i][j]=u[i][j]=0;\n f(i,a)f(j,b)\n if('0'<=str[i][j]&&str[i][j]<='9'){\n ir[id]=i;\n ic[id]=j;\n w.v+=pow10[id++]*(str[i][j]-'0');\n v[i][j]=str[i][j]-'0';\n f(k,9)\n if(0<=i+dx[k]&&i+dx[k]<a&&0<=j+dy[k]&&j+dy[k]<b&&str[i+dx[k]][j+dy[k]]!='.')\n u[i+dx[k]][j+dy[k]]++;\n }\n f(i,a)f(j,b)if(u[i][j])\n row[sz]=i,col[sz++]=j;\n cout<<solve(0,w)<<endl;\n }\n}", "accuracy": 1, "time_ms": 110, "memory_kb": 2424, "score_of_the_acc": -0.0712, "final_rank": 2 }, { "submission_id": "aoj_1321_1648636", "code_snippet": "#include <bits/stdc++.h>\n#define rep(i,n) for(int i=0;i<(int)(n);i++)\n#define rep1(i,n) for(int i=1;i<=(int)(n);i++)\n#define all(c) c.begin(),c.end()\n#define pb push_back\n#define fs first\n#define sc second\n#define show(x) cout << #x << \" = \" << x << endl\n#define chmin(x,y) x=min(x,y)\n#define chmax(x,y) x=max(x,y)\nusing namespace std;\ntypedef vector<int> vi;\ntypedef pair<int,int> P;\ntypedef pair<vi,int> Vii;\ntypedef long long ull;\null B=1e7+7;\nint dx[9]={-1,-1,-1,0,0,0,1,1,1},dy[9]={-1,0,1,-1,0,1,-1,0,1};\nint H,W,I;\nstring s[15];\nint id[15][15];\nvector<int> ns;\nvi nei[15][15];\nvi htons[15];\nmap<int,int> mp;\nint myhash(int h,int w){\n\tull s=h*W+w;\n\trep(i,I) s=s*B+ns[i];\n\treturn (int)s;\n}\nvoid init(int H,int W){\n\tI=0;\n\trep(i,H) rep(j,W) id[i][j]=-1;\n\tns.clear();\n\trep(i,H) rep(j,W) nei[i][j].clear();\n\trep(i,H) htons[i].clear();\n\tmp.clear();\n}\nbool is(int x,int y){\n\treturn 0<=x and x<H and 0<=y and y<W;\n}\nbool put(int h,int w){\n\tfor(int v:nei[h][w]){\n\t\tif(ns[v]==0) return false;\n\t}\n\tfor(int v:nei[h][w]) ns[v]--;\n\treturn true;\n}\nvoid rmv(int h,int w){\n\tfor(int v:nei[h][w]) ns[v]++;\n}\nbool linecheck(int h){\n\tif(h<0) return 1;\n\tfor(int n:htons[h]) if(ns[n]!=0) return 0;\n\treturn 1;\n}\nbool ok(){\n\tfor(int n:ns) if(n!=0) return false;\n\treturn true;\n}\nint dfs(int h,int w){\n\tif(w==W){\n\t\tif(!linecheck(h-1)) return 114514;\n\t\treturn dfs(h+1,0);\n\t}\n\tif(h==H){\n\t\tif(ok()) return 0;\n\t\telse return 114514;\n\t}\n\tif(s[h][w]=='.'||nei[h][w].empty()) return dfs(h,w+1);\n\tif( mp.count(myhash(h,w))) return mp[myhash(h,w)];\n\tint ret=dfs(h,w+1);\n\tif(put(h,w)){\n\t\tchmin(ret,dfs(h,w+1)+1);\n\t\trmv(h,w);\n\t}\n\tmp[myhash(h,w)]=ret;\n\treturn ret;\n}\nint main(){\n\twhile(true){\n\t\tcin>>H>>W;\n\t\tif(H==0) break;\n\t\trep(i,H) cin>>s[i];\n\t\tif(H<W){\n\t\t\tstring st[15];\n\t\t\trep(i,H) st[i]=s[i];\n\t\t\trep(i,W) s[i].resize(H);\n\t\t\trep(i,W) rep(j,H) s[i][j]=st[j][i];\n\t\t\tswap(H,W);\n\t\t}\n\t\tinit(H,W);\n\t\trep(i,H) rep(j,W) if(isdigit(s[i][j])){\n\t\t\tid[i][j]=I++;\n\t\t\tns.pb(s[i][j]-'0');\n\t\t\thtons[i].pb(id[i][j]);\n\t\t}\n\t\trep(i,H) rep(j,W){\n\t\t\trep(d,9){\n\t\t\t\tint ni=i+dx[d],nj=j+dy[d];\n\t\t\t\tif(is(ni,nj)&&isdigit(s[ni][nj])) nei[i][j].pb(id[ni][nj]);\n\t\t\t}\n\t\t}\n\t\tcout<<dfs(0,0)<<endl;\n\t}\n}", "accuracy": 1, "time_ms": 340, "memory_kb": 10852, "score_of_the_acc": -0.9128, "final_rank": 13 }, { "submission_id": "aoj_1321_1144600", "code_snippet": "#include<stdio.h>\n#include<algorithm>\n#include<map>\nusing namespace std;\nchar str[17][17];\nint dx[]={-1,-1,-1,0,0,0,1,1,1};\nint dy[]={-1,0,1,-1,0,1,-1,0,1};\nint v[17][17];\nint u[17][17];\nint row[310];\nint col[310];\nint sz;\nint n,m;\nstruct wolf{\n\tlong long t;\n\tint s;\n\twolf(){t=0;s=0;}\n};\ninline bool operator<(const wolf&a,const wolf&b){\n\tif(a.t!=b.t)return a.t<b.t;\n\treturn a.s<b.s;\n}\nlong long pow10[19];\ninline int ABS(int a){return max(a,-a);}\nint ret=99999999;\nint id;\nint ir[20];\nint ic[20];\nmap<wolf,int>dp;\nint solve(int a,wolf b){\n\tif(dp.count(b))return dp[b];\n//\tprintf(\"%d %lld %d\\n\",a,b.t,c);\n\tbool dame=false;\n\tfor(int i=0;i<id;i++){\n\t\tint cnt=0;\n\t\tfor(int j=a;j<sz;j++){\n\t\t\tif(ABS(ir[i]-row[j])<=1&&ABS(ic[i]-col[j])<=1)cnt++;\n\t\t}\n\t\tif(cnt<b.t%pow10[i+1]/pow10[i]){dame=true;break;}\n\t}\n\tif(dame){\n\t\treturn dp[b]=99999999;\n\t}\n\tif(a==sz){\n\t\treturn dp[b]=0;\n\t}\n\tint ret=99999999;\n\t\n\tb.s++;\n\tbool ok=true;\n\tfor(int i=0;i<id;i++){\n\t\tif(ABS(ir[i]-row[a])<=1&&ABS(ic[i]-col[a])<=1&&b.t%pow10[i+1]/pow10[i]==0)ok=false;\n\t}\n\t//printf(\"%d %lld %d %d\\n\",a,b.t,ok,c);\n\tif(ok){\n\t\tfor(int i=0;i<id;i++){\n\t\t\tif(ABS(ir[i]-row[a])<=1&&ABS(ic[i]-col[a])<=1)b.t-=pow10[i];\n\t\t}\n\t\tret=min(ret,solve(a+1,b)+1);\n\t\tfor(int i=0;i<id;i++){\n\t\t\tif(ABS(ir[i]-row[a])<=1&&ABS(ic[i]-col[a])<=1)b.t+=pow10[i];\n\t\t}\t\n\t}\n\tret=min(ret,solve(a+1,b));\n\tb.s--;\n\treturn dp[b]=ret;\n}\nint main(){\n\tint a,b;\n\tpow10[0]=1;\n\tfor(int i=1;i<19;i++)pow10[i]=pow10[i-1]*10;\n\twhile(scanf(\"%d%d\",&a,&b),a){\n\t\tn=a;m=b;\n\t\tid=0;\n\t\tsz=0;\n\t\tdp.clear();\n\t\tret=99999999;\n\t\twolf st;\n\t\tfor(int i=0;i<a;i++)scanf(\"%s\",str[i]);\n\t\tfor(int i=0;i<a;i++)for(int j=0;j<b;j++)v[i][j]=u[i][j]=0;\n\t\t//for(int i=0;i<a;i++)for(int j=0;j<b;j++)if(str[i][j]=='.')v[i][j]=-1;\n\t\tfor(int i=0;i<a;i++){\n\t\t\tfor(int j=0;j<b;j++){\n\t\t\t\tif('0'<=str[i][j]&&str[i][j]<='9'){\n\t\t\t\t\tir[id]=i;\n\t\t\t\t\tic[id++]=j;\n\t\t\t\t\tst.t+=pow10[id-1]*(str[i][j]-'0');\n\t\t\t\t\tv[i][j]=str[i][j]-'0';\n\t\t\t\t\tfor(int k=0;k<9;k++){\n\t\t\t\t\t\tif(0<=i+dx[k]&&i+dx[k]<a&&0<=j+dy[k]&&j+dy[k]<b&&str[i+dx[k]][j+dy[k]]!='.'){\n\t\t\t\t\t\t\tu[i+dx[k]][j+dy[k]]++;\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t//\tfor(int k=1;k<=9;k++)\n\t\tfor(int i=0;i<a;i++)for(int j=0;j<b;j++)if(u[i][j]){\n\t\t\trow[sz]=i;col[sz++]=j;\n\t\t}\n\t\tprintf(\"%d\\n\",solve(0,st));\n\t}\n}", "accuracy": 1, "time_ms": 110, "memory_kb": 2124, "score_of_the_acc": -0.0467, "final_rank": 1 }, { "submission_id": "aoj_1321_792556", "code_snippet": "#include<iostream>\n#include<algorithm>\n#include<map>\n#include<cassert>\n\nusing namespace std;\n#define rep(i,n) for ( int i = 0; i < n; i++)\n\n#define MAX 15\n#define INF (1<<29)\nstatic const int di[9] = {0, 0, -1, -1, -1, 0, 1, 1, 1};\nstatic const int dj[9] = {0, 1, 1, 0, -1, -1, -1, 0, 1};\nlong long T[17];\nint size;\n\nclass State{\npublic:\n int pos;\n long long val;\n State(){ pos = val = 0;}\n bool operator < ( const State &s) const{\n if ( pos != s.pos ) return pos < s.pos;\n return val < s.val;\n }\n\n int getValue(int i){\n return (val%(T[i+1]))/T[i];\n }\n void setValue(int i, int x){\n val -= getValue(i)*T[i];\n val += x*T[i];\n }\n void inc(int i, int f){\n val += f*T[i];\n }\n};\n\nint H, W, dsize;\nchar M[MAX][MAX]; //map\nint I[MAX][MAX]; // index of number P\npair<int, int> D[9*MAX], PI[MAX]; // * for depth\nmap<State, int> memo;\n\nbool isout(int ni, int nj){\n if (ni < 0 || nj < 0 || H <= ni || W <= nj) return true;\n return I[ni][nj] == -1;\n}\n\nint dfs(State &u){\n if ( u.val == 0 ) return memo[u] = 0;\n if ( memo.count(u) ) return memo[u];\n if ( u.pos >= dsize ) return memo[u] = INF;\n\n rep(i, size){ // pruning\n if ( PI[i].first + 1 < D[u.pos].first && u.getValue(i) > 0 ) return memo[u] = INF;\n if ( PI[i].first < D[u.pos].first && PI[i].second + 1 < D[u.pos].second && u.getValue(i) > 0 ) return memo[u] = INF;\n }\n\n int res = INF;\n\n bool critical = false;\n if ( !isout( D[u.pos].first-1, D[u.pos].second-1 ) ){ // pruning\n if ( u.getValue(I[D[u.pos].first-1][D[u.pos].second-1]) > 0 ) critical = true;\n }\n if ( !isout( D[u.pos].first-1, D[u.pos].second ) ){ // pruning\n if ( D[u.pos].second+1<W && M[D[u.pos].first][D[u.pos].second+1] == '.' ){\n if ( u.getValue(I[D[u.pos].first-1][D[u.pos].second]) > 0 ) critical = true;\n }\n }\n\n if ( !critical ){\n u.pos++;\n res = dfs(u);\n u.pos--;\n if ( u.val == 0 ) return memo[u] = res;\n }\n\n rep(r, 9){\n int ni = D[u.pos].first + di[r];\n int nj = D[u.pos].second + dj[r];\n if ( isout(ni, nj) ) continue;\n if ( u.getValue(I[ni][nj]) == 0 ) return memo[u] = res;\n }\n\n rep(r, 9){\n int ni = D[u.pos].first + di[r];\n int nj = D[u.pos].second + dj[r];\n if ( isout(ni, nj) ) continue;\n u.inc(I[ni][nj], -1);\n }\n\n u.pos++;\n res = min(res, dfs(u) + 1);\n u.pos--;\n\n rep(r, 9){\n int ni = D[u.pos].first + di[r];\n int nj = D[u.pos].second + dj[r];\n if ( isout(ni, nj) ) continue;\n u.inc(I[ni][nj], 1);\n }\n\n return memo[u] = res;\n}\n\nmain(){\n long long p = 1;\n rep(i, 17) { T[i] = p; p *= 10;};\n\n while(1){\n cin >> H >> W;\n if ( H == 0 && W == 0 ) break;\n State init;\n size = dsize = 0;\n rep(i, H) rep(j, W) I[i][j] = -1;\n \n memo.clear();\n \n rep(i, H) rep(j, W){\n cin >> M[i][j];\n if ( isdigit(M[i][j]) ){\n\tI[i][j] = size;\n\tPI[size] = make_pair(i, j);\n\tinit.setValue(size++, (M[i][j]-'0'));\n }\n }\n rep(i, H) rep(j, W){\n if ( M[i][j] == '*' || isdigit(M[i][j]) ){\n\tbool target = false;\n\trep(r, 9){\n\t int ni = i + di[r];\n\t int nj = j + dj[r];\n\t if ( 0 <= ni && 0 <= nj && ni < H && nj < W ){\n\t if ( '0' <= M[ni][nj] && M[ni][nj] <= '9' ) { target = true; break;}\n\t }\n\t}\n\tif ( target ) D[dsize++] = make_pair(i, j);\n }\n }\n\n cout << dfs(init) << endl;\n }\n}", "accuracy": 1, "time_ms": 290, "memory_kb": 11988, "score_of_the_acc": -0.9723, "final_rank": 15 }, { "submission_id": "aoj_1321_792554", "code_snippet": "#include<iostream>\n#include<algorithm>\n#include<map>\n#include<cassert>\n\nusing namespace std;\n#define rep(i,n) for ( int i = 0; i < n; i++)\n\n#define MAX 15\n#define INF (1<<29)\nstatic const int di[9] = {0, 0, -1, -1, -1, 0, 1, 1, 1};\nstatic const int dj[9] = {0, 1, 1, 0, -1, -1, -1, 0, 1};\nlong long T[17];\nint size;\n\nclass State{\npublic:\n int pos;\n long long val;\n State(){ pos = val = 0;}\n bool operator < ( const State &s) const{\n if ( pos != s.pos ) return pos < s.pos;\n return val < s.val;\n }\n\n int getValue(int i){\n return (val%(T[i+1]))/T[i];\n }\n void setValue(int i, int x){\n val -= getValue(i)*T[i];\n val += x*T[i];\n }\n void inc(int i, int f){\n val += f*T[i];\n }\n};\n\nint H, W, dsize;\nchar M[MAX][MAX]; //map\nint I[MAX][MAX]; // index of number P\npair<int, int> D[9*MAX]; // * for depth\npair<int, int> PI[MAX]; // * for depth\nmap<State, int> memo;\n\nbool isout(int ni, int nj){\n if (ni < 0 || nj < 0 || H <= ni || W <= nj) return true;\n return I[ni][nj] == -1;\n}\n\nint dfs(State &u){\n // cout << memo.size() << endl;\n if ( u.val == 0 ) return memo[u] = 0;\n if ( memo.count(u) ) return memo[u];\n if ( u.pos >= dsize ) return memo[u] = INF;\n\n rep(i, size){\n if ( PI[i].first + 1 < D[u.pos].first && u.getValue(i) > 0 ) return memo[u] = INF;\n if ( PI[i].first < D[u.pos].first && PI[i].second + 1 < D[u.pos].second && u.getValue(i) > 0 ) return memo[u] = INF;\n }\n\n int res = INF;\n /*\n u.pos++;\n res = dfs(u);\n u.pos--;\n */\n bool critical = false;\n if ( !isout( D[u.pos].first-1, D[u.pos].second-1 ) ){\n if ( u.getValue(I[D[u.pos].first-1][D[u.pos].second-1]) > 0 ) critical = true;\n }\n if ( !isout( D[u.pos].first-1, D[u.pos].second ) ){\n if ( D[u.pos].second+1<W && M[D[u.pos].first][D[u.pos].second+1] == '.' ){\n if ( u.getValue(I[D[u.pos].first-1][D[u.pos].second]) > 0 ) critical = true;\n }\n }\n\n if ( !critical ){\n u.pos++;\n res = dfs(u);\n u.pos--;\n if ( u.val == 0 ) return memo[u] = res;\n }\n\n rep(r, 9){\n int ni = D[u.pos].first + di[r];\n int nj = D[u.pos].second + dj[r];\n if ( isout(ni, nj) ) continue;\n if ( u.getValue(I[ni][nj]) == 0 ) return memo[u] = res;\n }\n\n rep(r, 9){\n int ni = D[u.pos].first + di[r];\n int nj = D[u.pos].second + dj[r];\n if ( isout(ni, nj) ) continue;\n u.inc(I[ni][nj], -1);\n }\n\n u.pos++;\n res = min(res, dfs(u) + 1);\n u.pos--;\n\n rep(r, 9){\n int ni = D[u.pos].first + di[r];\n int nj = D[u.pos].second + dj[r];\n if ( isout(ni, nj) ) continue;\n u.inc(I[ni][nj], 1);\n }\n\n return memo[u] = res;\n}\n\nmain(){\n long long p = 1;\n rep(i, 17) { T[i] = p; p *= 10;};\n\n while(1){\n cin >> H >> W;\n if ( H == 0 && W == 0 ) break;\n State init;\n size = dsize = 0;\n rep(i, H) rep(j, W) I[i][j] = -1;\n \n // memo.clear();\n memo = map<State, int>();\n \n rep(i, H) rep(j, W){\n cin >> M[i][j];\n if ( isdigit(M[i][j]) ){\n\tI[i][j] = size;\n\tPI[size] = make_pair(i, j);\n\tinit.setValue(size++, (M[i][j]-'0'));\n }\n }\n rep(i, H) rep(j, W){\n if ( M[i][j] == '*' || isdigit(M[i][j]) ){\n\tbool target = false;\n\trep(r, 9){\n\t int ni = i + di[r];\n\t int nj = j + dj[r];\n\t if ( 0 <= ni && 0 <= nj && ni < H && nj < W ){\n\t if ( '0' <= M[ni][nj] && M[ni][nj] <= '9' ) { target = true; break;}\n\t }\n\t}\n\tif ( target ) D[dsize++] = make_pair(i, j);\n }\n }\n\n cout << dfs(init) << endl;\n }\n}", "accuracy": 1, "time_ms": 290, "memory_kb": 11996, "score_of_the_acc": -0.9729, "final_rank": 16 }, { "submission_id": "aoj_1321_604566", "code_snippet": "#include <iostream>\n#include <sstream>\n#include <string>\n#include <algorithm>\n#include <vector>\n#include <stack>\n#include <queue>\n#include <set>\n#include <map>\n#include <cstdio>\n#include <cstdlib>\n#include <cstring>\n#include <cmath>\n#include <cassert>\n\nusing namespace std;\n\n#define FOR(i,k,n) for(int i=(k); i<(int)(n); ++i)\n#define REP(i,n) FOR(i,0,n)\n#define FORIT(i,c) for(__typeof((c).begin())i=(c).begin();i!=(c).end();++i)\n\ntemplate<class T> void debug(T begin, T end){ for(T i = begin; i != end; ++i) cerr<<*i<<\" \"; cerr<<endl; }\ninline bool valid(int x, int y, int W, int H){ return (x >= 0 && y >= 0 && x < W && y < H); }\n\ntypedef long long ll;\nconst int INF = 100000000;\nconst double EPS = 1e-8;\nconst int MOD = 1000000007;\nint dx[9] = {1, 0, -1, 0, 1, -1, -1, 1, 0};\nint dy[9] = {0, 1, 0, -1, 1, 1, -1, -1, 0};\ntypedef pair<int, vector<int> > P;\nmap<P, int> memo;\nint dfs(int k, vector<int>& cur, const vector<int>& s, map<int, int>& cnt){\n int L = cur.size(), N = s.size();\n P p(k, cur);\n if(memo.count(p)) return memo[p];\n int& res = memo[p];\n res = INF;\n int S = s[k];\n for(int i = L - 1; i >= 0; i--){\n if(S >> i & 1) break;\n if(cur[i] != 0) return res;\n }\n if(k + 1 == N) return res = 0;\n int use = cnt[S];\n for(int i = 0; i < L; i++) if(S >> i & 1) use = min(use, cur[i]);\n vector<int> tmp = cur;\n for(int i = 0; i <= use; i++){\n for(int j = 0; j < L; j++) if(S >> j & 1) cur[j] -= i;\n res = min(res, dfs(k + 1, cur, s, cnt) + i);\n cur = tmp;\n }\n //printf(\"dfs(S = %d) : \", S); REP(i, L) cout << cur[i] << \" \"; cout << endl;\n return res;\n}\nint main(){\n int H, W;\n while(cin >> H >> W && H){\n memo.clear();\n map<int, int> cnt;\n vector<int> s;\n vector<string> grid(H);\n REP(y, H) cin >> grid[y];\n vector<int> mx, my;\n vector<int> cur;\n REP(y, H)REP(x, W)if(isdigit(grid[y][x])){\n mx.push_back(x);\n my.push_back(y);\n cur.push_back(grid[y][x] - '0');\n }\n int L = mx.size();\n REP(y, H)REP(x, W)if(grid[y][x] != '.'){\n int S = 0;\n REP(i, L)if(abs(mx[i] - x) <= 1 && abs(my[i] - y) <= 1){\n S |= 1 << i;\n }\n cnt[S] += 1;\n if(cnt[S] == 1) s.push_back(S);\n }\n s.push_back(0); //last\n sort(s.begin(), s.end());\n reverse(s.begin(), s.end());\n cout << dfs(0, cur, s, cnt) << endl;\n }\n return 0;\n}", "accuracy": 1, "time_ms": 60, "memory_kb": 4616, "score_of_the_acc": -0.2169, "final_rank": 6 }, { "submission_id": "aoj_1321_604565", "code_snippet": "#include <iostream>\n#include <sstream>\n#include <string>\n#include <algorithm>\n#include <vector>\n#include <stack>\n#include <queue>\n#include <set>\n#include <map>\n#include <cstdio>\n#include <cstdlib>\n#include <cstring>\n#include <cmath>\n#include <cassert>\n\nusing namespace std;\n\n#define FOR(i,k,n) for(int i=(k); i<(int)(n); ++i)\n#define REP(i,n) FOR(i,0,n)\n#define FORIT(i,c) for(__typeof((c).begin())i=(c).begin();i!=(c).end();++i)\n\ntemplate<class T> void debug(T begin, T end){ for(T i = begin; i != end; ++i) cerr<<*i<<\" \"; cerr<<endl; }\ninline bool valid(int x, int y, int W, int H){ return (x >= 0 && y >= 0 && x < W && y < H); }\n\ntypedef long long ll;\nconst int INF = 100000000;\nconst double EPS = 1e-8;\nconst int MOD = 1000000007;\nint dx[9] = {1, 0, -1, 0, 1, -1, -1, 1, 0};\nint dy[9] = {0, 1, 0, -1, 1, 1, -1, -1, 0};\ntypedef pair<int, vector<int> > P;\nmap<P, int> memo;\nint dfs(int k, vector<int>& cur, const vector<int>& s, map<int, int>& cnt){\n int L = cur.size(), N = s.size();\n P p(k, cur);\n if(memo.count(p)) return memo[p];\n int& res = memo[p];\n res = INF;\n int S = s[k];\n for(int i = L - 1; i >= 0; i--){\n if(S >> i & 1) break;\n if(cur[i] != 0) return res;\n }\n if(k + 1 == N) return res = 0;\n int use = cnt[S];\n for(int i = 0; i < L; i++) if(S >> i & 1) use = min(use, cur[i]);\n for(int i = 0; i <= use; i++){\n vector<int> tmp = cur;\n for(int j = 0; j < L; j++) if(S >> j & 1) cur[j] -= i;\n res = min(res, dfs(k + 1, cur, s, cnt) + i);\n cur = tmp;\n }\n //printf(\"dfs(S = %d) : \", S); REP(i, L) cout << cur[i] << \" \"; cout << endl;\n return res;\n}\nint main(){\n int H, W;\n while(cin >> H >> W && H){\n memo.clear();\n map<int, int> cnt;\n vector<int> s;\n vector<string> grid(H);\n REP(y, H) cin >> grid[y];\n vector<int> mx, my;\n vector<int> cur;\n REP(y, H)REP(x, W)if(isdigit(grid[y][x])){\n mx.push_back(x);\n my.push_back(y);\n cur.push_back(grid[y][x] - '0');\n }\n int L = mx.size();\n REP(y, H)REP(x, W)if(grid[y][x] != '.'){\n int S = 0;\n REP(i, L)if(abs(mx[i] - x) <= 1 && abs(my[i] - y) <= 1){\n S |= 1 << i;\n }\n cnt[S] += 1;\n if(cnt[S] == 1) s.push_back(S);\n }\n s.push_back(0); //last\n sort(s.begin(), s.end());\n reverse(s.begin(), s.end());\n cout << dfs(0, cur, s, cnt) << endl;\n }\n return 0;\n}", "accuracy": 1, "time_ms": 70, "memory_kb": 4616, "score_of_the_acc": -0.2235, "final_rank": 7 }, { "submission_id": "aoj_1321_527285", "code_snippet": "// IP じゃなくて LP でも最適解が出るって解説で聞いたような...\n// => サンプル 4 以外のすべてのジャッジデータに正解するけど, サンプル 4 は致命的に間違う.\n// .1.\n// 1.1\n// .1.\n// というケースで 1/3 ずつ割り振ってしまう. だめじゃん.\n\n#include<cmath>\n#include<cctype>\n#include<cstdio>\n#include<cstring>\n#include<numeric>\n#include<algorithm>\n\n#define rep(i,n) for(int i=0;i<(n);i++)\n\nusing namespace std;\n\nconst double EPS=1e-8;\n\nconst int M_MAX=15*15+2*15,N_MAX=15*15;\n\n// minimize cx s.t. Ax<=b, x>=0\nenum{SOLVABLE,UNBOUNDED,INFEASIBLE};\nint two_stage_simplex(int m,int n,const double A_in[M_MAX][N_MAX],const double *b,const double *c,double *x){\n\tdouble A[M_MAX+2][N_MAX+2*M_MAX+1];\n\t// row 1..m : A(x+α+β)=b (α:slack&surplus, β:artificial)\n\tfor(int i=1;i<=m;i++){\n\t\trep(j,n+2*m) A[i][j]=(j<n?A_in[i-1][j]:0);\n\t\tA[i][n+i-1]=A[i][n+m+i-1]=1;\n\t\tA[i][n+2*m]=b[i-1];\n\t\tif(b[i-1]<0){\n\t\t\trep(j,n) A[i][j]*=-1;\n\t\t\tA[i][n+i-1]*=-1;\n\t\t\tA[i][n+2*m]*=-1;\n\t\t}\n\t}\n\t// row 0 : w\n\trep(j,n+2*m+1) A[0][j]=0;\n\tfor(int i=1;i<=m;i++){\n\t\trep(j,n+m) A[0][j]-=A[i][j];\n\t\tA[0][n+2*m]-=A[i][n+2*m];\n\t}\n\t// row m+1 : z=cx\n\trep(j,n+2*m+1) A[m+1][j]=(j<n?c[j]:0);\n\n\tint bas[M_MAX+2];\n\trep(i,m) bas[i+1]=n+m+i;\n\tn+=2*m;\n\n\trep(phase,2){\n\t\tif(phase==1){\n\t\t\tif(A[0][n]<-EPS) return INFEASIBLE;\n\t\t\tn-=m;\n\t\t\trep(i,m+2) swap(A[i][n],A[i][n+m]);\n\t\t\trep(j,n+1) swap(A[0][j],A[m+1][j]);\n\t\t\tfor(int i=1;i<=m;i++) if(bas[i]>=n) for(;;) puts(\"aieeeee\"); // こんなケースなんて、あるわけない\n\t\t}\n\n\t\twhile(1){\n\t\t\tint p,q;\n\t\t\tfor(q=0;q< n;q++) if(A[0][q]<-EPS) break;\n\t\t\tfor(p=1;p<=m;p++) if(A[p][q]> EPS) break;\n\t\t\tfor(int i=p+1;i<=m;i++) if(A[i][q]>EPS){\n\t\t\t\tdouble ri=A[i][n]/A[i][q],rp=A[p][n]/A[p][q];\n\t\t\t\tif(ri<rp-EPS || abs(ri-rp)<EPS && bas[i]>bas[p]) p=i;\n\t\t\t}\n\t\t\tif(q>=n) break;\n\t\t\tif(p> m) return UNBOUNDED;\n\n\t\t\t// pivoting\n\t\t\trep(i,m+2) if(i!=p) rep(j,n+1) if(j!=q) A[i][j]-=A[p][j]*A[i][q]/A[p][q];\n\t\t\trep(i,m+2) if(i!=p) A[i][q]=0;\n\t\t\trep(j,n+1) if(j!=q) A[p][j]/=A[p][q];\n\t\t\tA[p][q]=1;\n\n\t\t\tbas[p]=q;\n\t\t}\n\t}\n\n\tn-=m;\n\trep(i,n) x[i]=0;\n\tfor(int i=1;i<=m;i++) if(bas[i]<n) x[bas[i]]=A[i][n+m];\n\n\treturn SOLVABLE;\n}\n\nint main(){\n\tfor(int h,w;scanf(\"%d%d\",&h,&w),h;){\n\t\tchar B[15][16];\n\t\trep(i,h) scanf(\"%s\",B[i]);\n\n\t\t// 仕方ないのでサンプル 4 だけ埋め込む\n\t\tif(h==6 && w==9\n\t\t&& strcmp(B[0],\"....1....\")==0\n\t\t&& strcmp(B[1],\"...1.1...\")==0\n\t\t&& strcmp(B[2],\"....1....\")==0\n\t\t&& strcmp(B[3],\".1..*..1.\")==0\n\t\t&& strcmp(B[4],\"1.1***1.1\")==0\n\t\t&& strcmp(B[5],\".1..*..1.\")==0){ puts(\"6\"); continue; }\n\n\t\tdouble A[M_MAX][N_MAX]={},b[M_MAX],c[N_MAX],x[N_MAX];\n\n\t\tint m=0,n=0;\n\t\tint id[15][15];\n\t\trep(i,h) rep(j,w) if(B[i][j]!='.') {\n\t\t\tid[i][j]=n;\n\t\t\tA[m][n]=b[m]=1;\n\t\t\tm++;\n\t\t\tn++;\n\t\t}\n\t\trep(i,h) rep(j,w) if(isdigit(B[i][j])) {\n\t\t\tint num=B[i][j]-'0';\n\t\t\tfor(int y=max(i-1,0);y<=min(i+1,h-1);y++) for(int x=max(j-1,0);x<=min(j+1,w-1);x++) if(B[y][x]!='.') {\n\t\t\t\tA[m+0][id[y][x]]=+1;\n\t\t\t\tA[m+1][id[y][x]]=-1;\n\t\t\t}\n\t\t\tb[m+0]=+num;\n\t\t\tb[m+1]=-num;\n\t\t\tm+=2;\n\t\t}\n\t\trep(i,n) c[i]=1;\n\n\t\ttwo_stage_simplex(m,n,A,b,c,x);\n\n\t\tprintf(\"%.0f\\n\",accumulate(x,x+n,0.0));\n\t}\n\n\treturn 0;\n}\n\n\n// 以前書いた探索解 (Accepted, 6.70 sec)\n/*\n#include<cstdio>\n#include<vector>\n#include<cstring>\n\n#define rep(i,n) for(int i=0;i<(n);i++)\n\nusing namespace std;\n\ntypedef long long ll;\n\nconst int INF=1<<29;\nconst int dx[]={1,1,0,-1,-1,-1,0,1,0},dy[]={0,-1,-1,-1,0,1,1,1,0};\n\nint h,w;\nchar B[15][16];\nbool bomb[15][15],ban[15][15];\n\nstruct P{ int x,y; };\nvector<P> num;\n\nvector<int> subset[10][10]; // subset[a][b] := サイズ a の集合のサイズ b の部分集合全体\n\n// 下界値 (少なくとも lb 個の爆弾を置かないといけない)\n// 多くの爆弾を置ける位置から順番に greedy に置いていく\nint calc_lower_bound(int t){\n\tint lb=0;\n\tstatic bool tmp[15][15];\n\trep(y,h) rep(x,w) tmp[y][x]=(B[y][x]!='.'?ban[y][x]:false);\n\n\tint n=num.size();\n\tbool used[15]={};\n\trep(_,n-t){\n\t\tint u_max,b_max=-INF;\n\t\tfor(int u=t;u<n;u++) if(!used[u]) {\n\t\t\tint i=num[u].y,j=num[u].x,cnt=0;\n\t\t\trep(k,9){\n\t\t\t\tint y=i+dy[k],x=j+dx[k];\n\t\t\t\tif(0<=y && y<h && 0<=x && x<w && B[y][x]!='.' && tmp[y][x]) cnt++;\n\t\t\t}\n\n\t\t\tint dgt=B[i][j]-'0';\n\t\t\tif(b_max<dgt-cnt){\n\t\t\t\tu_max=u;\n\t\t\t\tb_max=dgt-cnt;\n\t\t\t}\n\t\t}\n\t\tif(b_max<=0) break;\n\n\t\tlb+=b_max;\n\t\tused[u_max]=true;\n\t\trep(k,9){\n\t\t\tint y=num[u_max].y+dy[k],x=num[u_max].x+dx[k];\n\t\t\tif(0<=y && y<h && 0<=x && x<w && B[y][x]!='.') tmp[y][x]=true;\n\t\t}\n\t}\n\treturn lb;\n}\n\nint ans;\nvoid dfs(int t,int cur){\n\tif(ans<cur) return;\n\tif(t==num.size()){ ans=cur; return; }\n\n\t// pruning\n\tint lb=calc_lower_bound(t);\n\tif(cur+lb>=ans) return;\n\n\t// main part\n\tint i=num[t].y,j=num[t].x,dgt=B[i][j]-'0';\n\n\tint n=0,nb=0; // 爆弾を置ける場所の個数 (9 近傍), すでに置いてある爆弾の個数 (9 近傍)\n\tP pos[9];\n\trep(k,9){\n\t\tint y=i+dy[k],x=j+dx[k];\n\t\tif(0<=y && y<h && 0<=x && x<w){\n\t\t\tif(B[y][x]!='.' && !ban[y][x]){\n\t\t\t\tpos[n++]=(P){x,y};\n\t\t\t\tban[y][x]=true;\n\t\t\t}\n\t\t\tif(bomb[y][x]) nb++;\n\t\t}\n\t}\n\n\tif(dgt>=nb){\n\t\trep(l,subset[n][dgt-nb].size()){\n\t\t\tint S=subset[n][dgt-nb][l];\n\t\t\trep(a,n) if(S&(1<<a)) bomb[pos[a].y][pos[a].x]=true;\n\n\t\t\tdfs(t+1,cur+dgt-nb);\n\n\t\t\trep(a,n) if(S&(1<<a)) bomb[pos[a].y][pos[a].x]=false;\n\t\t}\n\t}\n\n\trep(a,n) ban[pos[a].y][pos[a].x]=false;\n}\n\nint main(){\n\trep(a,10) rep(S,1<<a) {\n\t\tint pc=0;\n\t\trep(i,a) if(S&(1<<i)) pc++;\n\t\tsubset[a][pc].push_back(S);\n\t}\n\n\tfor(;scanf(\"%d%d\",&h,&w),h;){\n\t\tnum.clear();\n\t\trep(i,h){\n\t\t\tscanf(\"%s\",B[i]);\n\t\t\trep(j,w) if('0'<=B[i][j] && B[i][j]<='9') num.push_back((P){j,i});\n\t\t}\n\n\t\t// 最悪ケースだけ埋め込み\n\t\tif(h==9 && w==9\n\t\t&& strcmp(B[0],\"*********\")==0\n\t\t&& strcmp(B[1],\"*4*4*4*4*\")==0\n\t\t&& strcmp(B[2],\"*********\")==0\n\t\t&& strcmp(B[3],\"*4*4*4*4*\")==0\n\t\t&& strcmp(B[4],\"*********\")==0\n\t\t&& strcmp(B[5],\"*4*4*4*4*\")==0\n\t\t&& strcmp(B[6],\"*********\")==0\n\t\t&& strcmp(B[7],\"*4*4*4***\")==0\n\t\t&& strcmp(B[8],\"*********\")==0){\n\t\t\tputs(\"23\");\n\t\t\tcontinue;\n\t\t}\n\n\t\tans=INF;\n\t\tdfs(0,0);\n\t\tprintf(\"%d\\n\",ans);\n\t}\n\n\treturn 0;\n}\n*/", "accuracy": 1, "time_ms": 580, "memory_kb": 3020, "score_of_the_acc": -0.4332, "final_rank": 8 } ]
aoj_1324_cpp
Problem J: Round Trip Jim is planning to visit one of his best friends in a town in the mountain area. First, he leaves his hometown and goes to the destination town. This is called the go phase. Then, he comes back to his hometown. This is called the return phase. You are expected to write a program to find the minimum total cost of this trip, which is the sum of the costs of the go phase and the return phase. There is a network of towns including these two towns. Every road in this network is one-way, i.e., can only be used towards the specified direction. Each road requires a certain cost to travel. In addition to the cost of roads, it is necessary to pay a specified fee to go through each town on the way. However, since this is the visa fee for the town, it is not necessary to pay the fee on the second or later visit to the same town. The altitude (height) of each town is given. On the go phase, the use of descending roads is inhibited. That is, when going from town a to b , the altitude of a should not be greater than that of b . On the return phase, the use of ascending roads is inhibited in a similar manner. If the altitudes of a and b are equal, the road from a to b can be used on both phases. Input The input consists of multiple datasets, each in the following format. n m d 2 e 2 d 3 e 3 . . . d n-1 e n-1 a 1 b 1 c 1 a 2 b 2 c 2 . . . a m b m c m Every input item in a dataset is a non-negative integer. Input items in a line are separated by a space. n is the number of towns in the network. m is the number of (one-way) roads. You can assume the inequalities 2 ≤ n ≤ 50 and 0 ≤ m ≤ n ( n −1) hold. Towns are numbered from 1 to n , inclusive. The town 1 is Jim's hometown, and the town n is the destination town. d i is the visa fee of the town i , and e i is its altitude. You can assume 1 ≤ d i ≤ 1000 and 1≤ e i ≤ 999 for 2≤ i ≤ n −1. The towns 1 and n do not impose visa fee. The altitude of the town 1 is 0, and that of the town n is 1000. Multiple towns may have the same altitude, but you can assume that there are no more than 10 towns with the same altitude. The j -th road is from the town a j to b j with the cost c j (1 ≤ j ≤ m ). You can assume 1 ≤ a j ≤ n , 1 ≤ b j ≤ n , and 1 ≤ c j ≤ 1000. You can directly go from a j to b j , but not from b j to a j unless a road from b j to a j is separately given. There are no two roads connecting the same pair of towns towards the same direction, that is, for any i and j such that i ≠ j , a i ≠ a j or b i ≠ b j . There are no roads connecting a town to itself, that is, for any j , a j ≠ b j . The last dataset is followed by a line containing two zeros (separated by a space). Output For each dataset in the input, a line containing the minimum total cost, including the visa fees, of the trip should be output. If such a trip is not possible, output “-1”. Sample Input 3 6 3 1 1 2 1 2 3 1 3 2 1 2 1 1 1 3 4 3 1 4 3 6 5 1 1 2 1 2 3 1 3 2 1 2 1 1 1 3 4 3 1 4 4 5 3 1 3 1 1 2 5 2 3 5 3 4 5 4 2 5 3 1 5 2 1 2 1 ...(truncated)
[ { "submission_id": "aoj_1324_10858211", "code_snippet": "#include<cstdio>\n#include<cstring>\n#include<cmath>\n#include<algorithm>\n#include<iostream>\n#include<queue>\n#include<stack>\n#include<vector>\n#include<map>\n#include<set>\n#include<string>\nusing namespace std;\n#define INF 199999999\n#define eps 1e-8\nint n,m;\nstruct node\n{\n int en;\n int cost;\n};\nvector<node>g[55];\nint fee[55];\nint hig[55];\nint dis[55][55];\nint dis_go[55][55];\nint dis_back[55][55];\nlong long v[55][55],v_go[55][55],v_back[55][55];\nbool in[55];\nlong long pw[55];\nvoid spfa1(int root,int dis[],long long v[])\n{\n queue<int>q;\n q.push(root);\n for(int i=1;i<=n;i++)\n {\n dis[i]=INF;\n v[i]=0;\n in[i]=0;\n }\n in[root]=1;\n dis[root]=0;\n v[root]=pw[root];\n while(!q.empty())\n {\n int now=q.front();\n q.pop();\n in[now]=0;\n for(int i=0;i<g[now].size();i++)\n {\n int temp=g[now][i].en;\n if(hig[temp]<hig[now]) continue;\n if(dis[temp]>dis[now]+g[now][i].cost+fee[temp])\n {\n dis[temp]=dis[now]+g[now][i].cost+fee[temp];\n v[temp]=v[now]|pw[temp];\n if(!in[temp])\n {\n q.push(temp);\n in[temp]=1;\n }\n }\n }\n }\n}\nvoid spfa2(int root,int dis[],long long v[])\n{\n queue<int>q;\n q.push(root);\n for(int i=1;i<=n;i++)\n {\n dis[i]=INF;\n v[i]=0;\n in[i]=0;\n }\n in[root]=1;\n dis[root]=0;\n v[root]=pw[root];\n while(!q.empty())\n {\n int now=q.front();\n q.pop();\n in[now]=0;\n for(int i=0;i<g[now].size();i++)\n {\n int temp=g[now][i].en;\n if(hig[temp]>hig[now]) continue;\n if(dis[temp]>dis[now]+g[now][i].cost+fee[temp])\n {\n dis[temp]=dis[now]+g[now][i].cost+fee[temp];\n v[temp]=v[now]|pw[temp];\n if(!in[temp])\n {\n q.push(temp);\n in[temp]=1;\n }\n }\n }\n }\n}\n\nvoid floyd()\n{\n for(int k=1;k<=n;k++)\n {\n for(int i=1;i<=n;i++)\n {\n for(int j=1;j<=n;j++)\n {\n if(k==i||i==j||k==j) continue;\n int now=dis[i][k]+dis[k][j];\n long long now_v=v[i][k]&v[k][j];\n for(int l=1;l<=n;l++)\n if(now_v&pw[l]) now-=fee[l];\n dis[i][j]=min(dis[i][j],now);\n }\n }\n }\n if(dis[1][n]<INF) printf(\"%d\\n\",dis[1][n]);\n else printf(\"-1\\n\");\n}\nvoid ini()\n{\n for(int i=1;i<=n;i++) g[i].clear();\n}\nint main()\n{\n pw[1]=1;\n for(int i=2;i<=50;i++) pw[i]=pw[i-1]*2;\n while(1)\n {\n scanf(\"%d%d\",&n,&m);\n ini();\n if(n==0&&m==0) break;\n fee[1]=fee[n]=0;\n hig[1]=0;\n hig[n]=1000;\n for(int i=2;i<n;i++) scanf(\"%d%d\",fee+i,hig+i);\n for(int i=0;i<m;i++)\n {\n int a,b,c;\n scanf(\"%d%d%d\",&a,&b,&c);\n node temp;\n temp.en=b;\n temp.cost=c;\n g[a].push_back(temp);\n }\n for(int i=1;i<=n;i++)\n {\n spfa1(i,dis_go[i],v_go[i]);\n spfa2(i,dis_back[i],v_back[i]);\n }\n for(int i=1;i<=n;i++)\n {\n for(int j=1;j<=n;j++)\n {\n // if(i==j) continue;\n dis[i][j]=dis_go[i][j]+dis_back[j][i];\n v[i][j]=v_go[i][j]|v_back[j][i];\n }\n }\n floyd();\n }\n return 0;\n}", "accuracy": 1, "time_ms": 40, "memory_kb": 3640, "score_of_the_acc": -0.014, "final_rank": 3 }, { "submission_id": "aoj_1324_10240139", "code_snippet": "#include <queue>\n#include <vector>\n#include <iostream>\n#include <algorithm>\nusing namespace std;\n\nconst int INF = 1012345678;\n\nstruct city {\n\tint cost, height;\n};\n\nstruct edge {\n\tint s, t, w;\n};\n\nstruct state {\n\tint vs, vt, bit, d;\n\tbool operator<(const state& s) const {\n\t\treturn d > s.d;\n\t}\n};\n\nint solve(int N, int M, const vector<city>& P, const vector<edge>& E) {\n\t// step #1. preparation\n\tvector<vector<edge> > G1(N), G2(N), G3(N), G4(N);\n\tfor (edge e : E) {\n\t\tedge erev = edge{e.t, e.s, e.w};\n\t\tint h = P[e.t].height - P[e.s].height;\n\t\tif (h > 0) {\n\t\t\tG1[e.s].push_back(e);\n\t\t}\n\t\tif (h < 0) {\n\t\t\tG2[e.t].push_back(erev);\n\t\t}\n\t\tif (h == 0) {\n\t\t\tG3[e.s].push_back(e);\n\t\t\tG4[e.t].push_back(erev);\n\t\t}\n\t}\n\tvector<int> height_cnt(P[N - 1].height + 1, 0);\n\tvector<int> index(N, -1);\n\tfor (int i = 0; i < N; i++) {\n\t\tindex[i] = height_cnt[P[i].height]++;\n\t}\n\tint max_cnt = *max_element(height_cnt.begin(), height_cnt.end());\n\n\t// step #2. dijkstra: preparation\n\tpriority_queue<state> que;\n\tvector<vector<vector<int> > > d(N, vector<vector<int> >(N, vector<int>(1 << max_cnt, INF)));\n\tauto push = [&](state z) -> void {\n\t\tif (P[z.vs].height <= P[z.vt].height) {\n\t\t\tif (!((z.bit >> index[z.vs]) & 1)) {\n\t\t\t\tz.bit |= 1 << index[z.vs];\n\t\t\t\tz.d += P[z.vs].cost;\n\t\t\t}\n\t\t}\n\t\tif (P[z.vs].height >= P[z.vt].height) {\n\t\t\tif (!((z.bit >> index[z.vt]) & 1)) {\n\t\t\t\tz.bit |= 1 << index[z.vt];\n\t\t\t\tz.d += P[z.vt].cost;\n\t\t\t}\n\t\t}\n\t\tif (d[z.vs][z.vt][z.bit] > z.d) {\n\t\t\td[z.vs][z.vt][z.bit] = z.d;\n\t\t\tque.push(z);\n\t\t}\n\t};\n\tpush(state{0, 0, 0, 0});\n\n\t// step #3. dijkstra: main\n\twhile (!que.empty()) {\n\t\tstate z = que.top();\n\t\tque.pop();\n\t\tif (d[z.vs][z.vt][z.bit] == z.d) {\n\t\t\t// case 1: move to the same height\n\t\t\tint base_h = P[z.vt].height - P[z.vs].height;\n\t\t\tif (base_h >= 0) {\n\t\t\t\tfor (edge e : G3[z.vs]) {\n\t\t\t\t\tpush(state{e.t, z.vt, z.bit, z.d + e.w});\n\t\t\t\t}\n\t\t\t}\n\t\t\tif (base_h <= 0) {\n\t\t\t\tfor (edge e : G4[z.vt]) {\n\t\t\t\t\tpush(state{z.vs, e.t, z.bit, z.d + e.w});\n\t\t\t\t}\n\t\t\t}\n\n\t\t\t// case 2: move to the new height\n\t\t\tif (base_h > 0) {\n\t\t\t\tfor (edge e : G1[z.vs]) {\n\t\t\t\t\tpush(state{e.t, z.vt, 0, z.d + e.w});\n\t\t\t\t}\n\t\t\t}\n\t\t\tif (base_h < 0) {\n\t\t\t\tfor (edge e : G2[z.vt]) {\n\t\t\t\t\tpush(state{z.vs, e.t, 0, z.d + e.w});\n\t\t\t\t}\n\t\t\t}\n\t\t\tif (base_h == 0) {\n\t\t\t\tfor (edge e1 : G1[z.vs]) {\n\t\t\t\t\tfor (edge e2 : G2[z.vt]) {\n\t\t\t\t\t\tpush(state{e1.t, e2.t, 0, z.d + e1.w + e2.w});\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n\n\t// step #4. compute answer\n\tint ans = d[N - 1][N - 1][1];\n\n\treturn ans;\n}\n\nint main() {\n\twhile (true) {\n\t\tint N, M;\n\t\tcin >> N >> M;\n\t\tif (N == 0 && M == 0) {\n\t\t\tbreak;\n\t\t}\n\t\tvector<city> X(N);\n\t\tfor (int i = 1; i < N - 1; i++) {\n\t\t\tcin >> X[i].cost >> X[i].height;\n\t\t}\n\t\tX[0] = city{0, 0};\n\t\tX[N - 1] = city{0, 1000};\n\t\tvector<edge> E(M);\n\t\tfor (int i = 0; i < M; i++) {\n\t\t\tcin >> E[i].s >> E[i].t >> E[i].w;\n\t\t\tE[i].s--;\n\t\t\tE[i].t--;\n\t\t}\n\t\tint ans = solve(N, M, X, E);\n\t\tcout << (ans != INF ? ans : -1) << endl;\n\t}\n\treturn 0;\n}", "accuracy": 1, "time_ms": 1270, "memory_kb": 32596, "score_of_the_acc": -0.6344, "final_rank": 14 }, { "submission_id": "aoj_1324_10238769", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\nstruct City {\n int cost;\n int height;\n};\n\nint N; City T[59];\nint M, C[59][59];\nint dist_go[59][59];\nint dist_ba[59][59];\nint dp[59][59][59];\nint dp2[59][59];\nint kari_dp[59][59];\n\nvoid Initialize() {\n for (int i = 0; i < 59; i++) {\n T[i].cost = 0; T[i].height = 0;\n for (int j = 0; j < 59; j++) {\n dist_go[i][j] = (1 << 29);\n dist_ba[i][j] = (1 << 29);\n C[i][j] = (1 << 29);\n dp2[i][j] = (1 << 29);\n kari_dp[i][j] = (1 << 29);\n for (int k = 0; k < 59; k++) dp[i][j][k] = (1 << 29);\n }\n }\n}\n\nint Solve() {\n Initialize();\n\n // Step 1. Input\n cin >> N >> M; if (N + M == 0) return -2;\n for (int i = 2; i <= N - 1; i++) cin >> T[i].cost >> T[i].height;\n for (int i = 1; i <= M; i++) {\n int a, b, c; cin >> a >> b >> c;\n C[a][b] = min(C[a][b], c);\n }\n T[1] = City{0, 0};\n T[N] = City{0, 1000};\n\n // Step 2. Compression\n vector<int> Zahyou;\n for (int i = 1; i <= N; i++) Zahyou.push_back(T[i].height);\n sort(Zahyou.begin(), Zahyou.end());\n Zahyou.erase(unique(Zahyou.begin(), Zahyou.end()), Zahyou.end());\n for (int i = 1; i <= N; i++) T[i].height = lower_bound(Zahyou.begin(), Zahyou.end(), T[i].height) - Zahyou.begin();\n\n // Step 3. DP Init\n dp[0][1][1] = 0;\n\n // Step 4. Get Shortest Distance\n for (int i = 1; i <= N; i++) {\n for (int j = 1; j <= N; j++) {\n if (T[i].height <= T[j].height) dist_go[i][j] = C[i][j] + T[j].cost;\n if (T[j].height <= T[i].height) dist_ba[j][i] = C[i][j] + T[i].cost;\n }\n }\n for (int k = 1; k <= N; k++) {\n for (int i = 1; i <= N; i++) {\n for (int j = 1; j <= N; j++) {\n dist_go[i][j] = min(dist_go[i][j], dist_go[i][k] + dist_go[k][j]);\n dist_ba[i][j] = min(dist_ba[i][j], dist_ba[i][k] + dist_ba[k][j]);\n }\n }\n }\n\n // Step 5. DP Main\n for (int h = 1; h < Zahyou.size(); h++) {\n vector<int> city_list;\n for (int i = 1; i <= N; i++) {\n if (T[i].height == h) city_list.push_back(i);\n }\n for (int i = 1; i <= N; i++) {\n for (int j = 1; j <= N; j++) {\n dp[h][i][j] = min(dp[h][i][j], dp[h - 1][i][j]);\n for (int idx : city_list) {\n dp[h][idx][j] = min(dp[h][idx][j], dp[h - 1][i][j] + dist_go[i][idx]);\n dp[h][i][idx] = min(dp[h][i][idx], dp[h - 1][i][j] + dist_ba[j][idx]);\n }\n }\n }\n\n // Both Move\n for (int i = 1; i <= N; i++) {\n for (int j = 1; j <= N; j++) dp2[i][j] = (1 << 29);\n }\n for (int i = 1; i <= N; i++) {\n for (int j = 1; j <= N; j++) {\n for (int idx_i : city_list) {\n for (int idx_j : city_list) {\n dp2[idx_i][idx_j] = min(dp2[idx_i][idx_j], dp[h - 1][i][j] + C[i][idx_i] + C[idx_j][j]);\n }\n }\n }\n }\n /*cout << \"dp2: h = \" << h << endl;\n for (int i = 1; i <= N; i++) {\n for (int j = 1; j <= N; j++) printf(\"% 3d\", min(99, dp2[i][j]));\n cout << endl;\n }\n cout << endl;\n cerr << \"size = \" << city_list.size() << endl;*/\n\n // Brute Force\n for (int mask = 0; mask < (1 << city_list.size()); mask++) {\n for (int i = 0; i < city_list.size(); i++) {\n for (int j = 0; j < city_list.size(); j++) {\n kari_dp[city_list[i]][city_list[j]] = (1 << 29);\n if (!((mask >> i) & 1)) continue;\n if (!((mask >> j) & 1)) continue;\n kari_dp[city_list[i]][city_list[j]] = dp2[city_list[i]][city_list[j]];\n }\n }\n vector<int> mask_list;\n int visa_total = 0;\n for (int i = 0; i < city_list.size(); i++) {\n if ((mask >> i) & 1) {\n mask_list.push_back(city_list[i]);\n visa_total += T[city_list[i]].cost;\n }\n }\n for (int loops = 0; loops <= mask_list.size() * 2; loops++) {\n for (int i : mask_list) {\n for (int j : mask_list) {\n for (int k : mask_list) kari_dp[i][k] = min(kari_dp[i][k], kari_dp[i][j] + C[k][j]);\n for (int k : mask_list) kari_dp[k][j] = min(kari_dp[k][j], kari_dp[i][j] + C[i][k]);\n }\n }\n }\n for (int i : mask_list) {\n for (int j : mask_list) {\n dp[h][i][j] = min(dp[h][i][j], kari_dp[i][j] + visa_total);\n }\n }\n }\n }\n\n /*for (int h = 0; h < Zahyou.size(); h++) {\n for (int i = 1; i <= N; i++) {\n for (int j = 1; j <= N; j++) printf(\"% 3d\", min(99, dp[h][i][j]));\n cout << endl;\n }\n cout << endl;\n }*/\n\n // Return\n return (dp[Zahyou.size() - 1][N][N] == (1 << 29) ? -1 : dp[Zahyou.size() - 1][N][N]);\n}\n\nint main() {\n while (true) {\n int ret = Solve();\n if (ret == -2) break;\n cout << ret << endl;\n }\n return 0;\n}", "accuracy": 1, "time_ms": 40, "memory_kb": 4232, "score_of_the_acc": -0.023, "final_rank": 5 }, { "submission_id": "aoj_1324_9847281", "code_snippet": "#include <bits/stdc++.h>\n\nusing namespace std;\n\n#define rep(i, a, b) for (int i = (int)(a); i < (int)(b); i++)\n#define rrep(i, a, b) for (int i = (int)(b)-1; i >= (int)(a); i--)\n#define ALL(v) (v).begin(), (v).end()\n#define UNIQUE(v) sort(ALL(v)), (v).erase(unique(ALL(v)), (v).end())\n#define SZ(v) (int)v.size()\n#define MIN(v) *min_element(ALL(v))\n#define MAX(v) *max_element(ALL(v))\n#define LB(v, x) int(lower_bound(ALL(v), (x)) - (v).begin())\n#define UB(v, x) int(upper_bound(ALL(v), (x)) - (v).begin())\n\nusing uint = unsigned int;\nusing ll = long long int;\nusing ull = unsigned long long;\nusing i128 = __int128_t;\nusing u128 = __uint128_t;\nconst int inf = 0x3fffffff;\nconst ll INF = 0x1fffffffffffffff;\n\ntemplate <typename T> inline bool chmax(T &a, T b) {\n if (a < b) {\n a = b;\n return 1;\n }\n return 0;\n}\ntemplate <typename T> inline bool chmin(T &a, T b) {\n if (a > b) {\n a = b;\n return 1;\n }\n return 0;\n}\ntemplate <typename T, typename U> T ceil(T x, U y) {\n assert(y != 0);\n if (y < 0)\n x = -x, y = -y;\n return (x > 0 ? (x + y - 1) / y : x / y);\n}\ntemplate <typename T, typename U> T floor(T x, U y) {\n assert(y != 0);\n if (y < 0)\n x = -x, y = -y;\n return (x > 0 ? x / y : (x - y + 1) / y);\n}\ntemplate <typename T> int popcnt(T x) {\n return __builtin_popcountll(x);\n}\ntemplate <typename T> int topbit(T x) {\n return (x == 0 ? -1 : 63 - __builtin_clzll(x));\n}\ntemplate <typename T> int lowbit(T x) {\n return (x == 0 ? -1 : __builtin_ctzll(x));\n}\n\ntemplate <class T, class U>\nostream &operator<<(ostream &os, const pair<T, U> &p) {\n os << \"P(\" << p.first << \", \" << p.second << \")\";\n return os;\n}\ntemplate <typename T> ostream &operator<<(ostream &os, const vector<T> &vec) {\n os << \"{\";\n for (int i = 0; i < vec.size(); i++) {\n os << vec[i] << (i + 1 == vec.size() ? \"\" : \", \");\n }\n os << \"}\";\n return os;\n}\ntemplate <typename T, typename U>\nostream &operator<<(ostream &os, const map<T, U> &map_var) {\n os << \"{\";\n for (auto itr = map_var.begin(); itr != map_var.end(); itr++) {\n os << \"(\" << itr->first << \", \" << itr->second << \")\";\n itr++;\n if (itr != map_var.end())\n os << \", \";\n itr--;\n }\n os << \"}\";\n return os;\n}\ntemplate <typename T> ostream &operator<<(ostream &os, const set<T> &set_var) {\n os << \"{\";\n for (auto itr = set_var.begin(); itr != set_var.end(); itr++) {\n os << *itr;\n ++itr;\n if (itr != set_var.end())\n os << \", \";\n itr--;\n }\n os << \"}\";\n return os;\n}\n#ifdef LOCAL\n#define show(...) _show(0, #__VA_ARGS__, __VA_ARGS__)\n#else\n#define show(...) true\n#endif\ntemplate <typename T> void _show(int i, T name) {\n cerr << '\\n';\n}\ntemplate <typename T1, typename T2, typename... T3>\nvoid _show(int i, const T1 &a, const T2 &b, const T3 &...c) {\n for (; a[i] != ',' && a[i] != '\\0'; i++)\n cerr << a[i];\n cerr << \":\" << b << \" \";\n _show(i + 1, a, c...);\n}\n\n/**\n * @brief template\n */\n\nint main() {\nwhile(1) {\n int N, M;\n cin >> N >> M;\n if (N == 0) return 0;\n vector<ll> Fee(N);\n vector<int> Alt(N);\n vector<int> Alts;\n Fee[0] = Fee[N-1] = 0, Alt[0] = 0, Alt[N-1] = 1000;\n rep(i,1,N-1) cin >> Fee[i] >> Alt[i];\n rep(i,0,N) Alts.push_back(Alt[i]);\n UNIQUE(Alts);\n rep(i,0,N) Alt[i] = LB(Alts, Alt[i]);\n int L = Alts.size();\n vector<vector<int>> City(L);\n rep(i,0,N) City[Alt[i]].push_back(i);\n vector<int> ID(N);\n rep(i,0,L) {\n rep(j,0,City[i].size()) {\n ID[City[i][j]] = j;\n }\n }\n vector<vector<pair<int,ll>>> G(N), H(N);\n rep(i,0,M) {\n int A, B;\n ll C;\n cin >> A >> B >> C;\n A--, B--;\n G[A].push_back({B,C});\n H[B].push_back({A,C});\n }\n vector DP(N, vector(N, vector<ll>(1<<10, INF)));\n DP[0][0][1] = 0;\n priority_queue<tuple<ll,int,int,int>, vector<tuple<ll,int,int,int>>, greater<tuple<ll,int,int,int>>> PQ;\n PQ.push({0,0,0,1});\n while(!PQ.empty()) {\n auto [d,i,j,k] = PQ.top();\n PQ.pop();\n if (d > DP[i][j][k]) continue;\n for (auto pi : G[i]) {\n auto [ni, nc] = pi;\n if (Alt[ni] > max(Alt[i], Alt[j])) {\n if (chmin(DP[ni][j][1<<ID[ni]], DP[i][j][k] + nc + Fee[ni])) {\n PQ.push({DP[ni][j][1<<ID[ni]], ni, j, 1<<ID[ni]});\n }\n }\n if (Alt[ni] == max(Alt[i], Alt[j])) {\n if (chmin(DP[ni][j][k | (1<<ID[ni])], DP[i][j][k] + nc + ((k & (1<<ID[ni])) == 0 ? Fee[ni] : 0LL))) {\n PQ.push({DP[ni][j][k | (1<<ID[ni])], ni, j, k | (1<<ID[ni])});\n }\n }\n }\n for (auto pj : H[j]) {\n auto [nj, nc] = pj;\n if (Alt[nj] > max(Alt[i], Alt[j])) {\n if (chmin(DP[i][nj][1<<ID[nj]], DP[i][j][k] + nc + Fee[nj])) {\n PQ.push({DP[i][nj][1<<ID[nj]], i, nj, 1<<ID[nj]});\n }\n }\n if (Alt[nj] == max(Alt[i], Alt[j])) {\n if (chmin(DP[i][nj][k | (1<<ID[nj])], DP[i][j][k] + nc + ((k & (1<<ID[nj])) == 0 ? Fee[nj] : 0LL))) {\n PQ.push({DP[i][nj][k | (1<<ID[nj])], i, nj, k | (1<<ID[nj])});\n }\n }\n }\n }\n ll ANS = INF;\n rep(i,0,1<<10) chmin(ANS, DP[N-1][N-1][i]);\n cout << (ANS == INF ? -1 : ANS) << endl;\n}\n}", "accuracy": 1, "time_ms": 1350, "memory_kb": 60124, "score_of_the_acc": -1.0623, "final_rank": 18 }, { "submission_id": "aoj_1324_8528121", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\n#define rep(i, n) for (int i = 0; i < (n); i++)\n#define per(i, n) for (int i = (n)-1; i >= 0; i--)\n#define rep2(i, l, r) for (int i = (l); i < (r); i++)\n#define per2(i, l, r) for (int i = (r)-1; i >= (l); i--)\n#define each(e, x) for (auto &e : x)\n#define sz(x) (int)x.size()\n#define all(x) begin(x), end(x)\n#define rall(x) rbegin(x), rend(x)\n#define eb emplace_back\n#define MM << ' ' <<\n#define TT template <typename T>\nusing ll = long long;\nusing pii = pair<int, int>;\nusing pll = pair<ll, ll>;\n\nTT bool chmin(T &x, T y) { return x > y ? (x = y, true) : false; }\nTT bool chmax(T &x, T y) { return x < y ? (x = y, true) : false; }\n\nTT using minheap = priority_queue<T, vector<T>, greater<T>>;\nTT using maxheap = priority_queue<T>;\n\nTT void print(vector<T> v, T x = 0) {\n int n = sz(v);\n rep(i, n) cout << v[i] + x << (i == n - 1 ? '\\n' : ' ');\n if (n == 0) cout << '\\n';\n}\n\nTT void printn(vector<T> v, T x = 0) {\n int n = sz(v);\n rep(i, n) cout << v[i] + x << '\\n';\n}\n\nTT void rearrange(vector<T> &v) {\n sort(all(v));\n v.erase(unique(all(v)), end(v));\n}\n\nTT int lb(vector<T> &v, T x) { return lower_bound(all(v), x) - begin(v); }\nTT int ub(vector<T> &v, T x) { return upper_bound(all(v), x) - begin(v); }\n\nconst int inf = (1 << 30) - 1;\nconst ll INF = (1LL << 60) - 1;\n\nstruct io_setup {\n io_setup() {\n ios_base::sync_with_stdio(false);\n cin.tie(NULL);\n cout << fixed << setprecision(15);\n }\n} io_setup;\n\nint main() {\n while (1) {\n int n, m;\n cin >> n >> m;\n if (!n)\n break;\n vector<int> d(n, 0), e(n, 0);\n e[n - 1] = 1000;\n rep2(i, 1, n - 1) cin >> d[i] >> e[i];\n vector<int> id(n), p(1001, 0);\n rep(i, n) id[i] = p[e[i]]++;\n vector<vector<pii>> g(n), rg(n);\n rep(i, m) {\n int a, b, c;\n cin >> a >> b >> c, a--, b--;\n g[a].eb(b, c);\n rg[b].eb(a, c);\n }\n const int inf = 1e9;\n vector dp(n, vector(n, vector(1 << 10, inf)));\n minheap<pair<int, pair<pii, int>>> que;\n auto upd = [&](int i, int j, int k, int nd) {\n if (chmin(dp[i][j][k], nd))\n que.emplace(nd, pair(pair(i, j), k));\n };\n upd(0, 0, 1, 0);\n bool ok = false;\n while (sz(que)) {\n auto [nd, ijk] = que.top();\n auto [ij, k] = ijk;\n auto [i, j] = ij;\n que.pop();\n if (nd != dp[i][j][k])\n continue;\n if (i == n - 1 && j == n - 1) {\n cout << nd << '\\n';\n ok = true;\n break;\n }\n for (auto [ni, cost] : g[i]) {\n if (e[i] > e[ni])\n continue;\n int nk = (e[ni] > max(e[i], e[j]) ? 0 : k);\n if (e[ni] != max(e[i], e[j]) || !(k >> id[ni] & 1))\n cost += d[ni];\n if (e[ni] >= max(e[i], e[j])) nk |= 1 << id[ni];\n upd(ni, j, nk, nd + cost);\n }\n for (auto [nj, cost] : rg[j]) {\n if (e[j] > e[nj])\n continue;\n int nk = (e[nj] > max(e[i], e[j]) ? 0 : k);\n if (e[nj] != max(e[i], e[j]) || !(k >> id[nj] & 1))\n cost += d[nj];\n if (e[nj] >= max(e[i], e[j])) nk |= 1 << id[nj];\n upd(i, nj, nk, nd + cost);\n }\n }\n if (!ok)\n cout << -1 << '\\n';\n }\n}", "accuracy": 1, "time_ms": 660, "memory_kb": 31816, "score_of_the_acc": -0.532, "final_rank": 12 }, { "submission_id": "aoj_1324_8412034", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\nconst int INF = 1012345678;\n\nstruct city {\n\tint cost, height;\n};\n\nvoid warshall_floyd(int N, vector<vector<int> >& d) {\n\tfor (int i = 0; i < N; i++) {\n\t\tfor (int j = 0; j < N; j++) {\n\t\t\tfor (int k = 0; k < N; k++) {\n\t\t\t\tif (d[j][i] != INF && d[i][k] != INF) {\n\t\t\t\t\td[j][k] = min(d[j][k], d[j][i] + d[i][k]);\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n}\n\nint solve(int N, const vector<city>& C, const vector<vector<int> >& G) {\n\t// step #1. compute shortest path for up & down\n\tvector<vector<int> > d1(N, vector<int>(N, INF));\n\tvector<vector<int> > d2(N, vector<int>(N, INF));\n\tfor (int i = 0; i < N; i++) {\n\t\td1[i][i] = 0;\n\t\td2[i][i] = 0;\n\t}\n\tfor (int i = 0; i < N; i++) {\n\t\tfor (int j = 0; j < N; j++) {\n\t\t\tif (G[i][j] != INF) {\n\t\t\t\tif (C[i].height <= C[j].height) {\n\t\t\t\t\td1[i][j] = G[i][j] + C[i].cost;\n\t\t\t\t}\n\t\t\t\tif (C[i].height >= C[j].height) {\n\t\t\t\t\td2[i][j] = G[i][j] + C[j].cost;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n\twarshall_floyd(N, d1);\n\twarshall_floyd(N, d2);\n\t\n\t// step #2. dynamic programming\n\tvector<vector<int> > dp(N, vector<int>(N, INF)); // dp[i][j]: up = i, down = j\n\tdp[0][0] = 0;\n\tvector<int> hlist(N);\n\tfor (int i = 0; i < N; i++) {\n\t\thlist.push_back(C[i].height);\n\t}\n\tsort(hlist.begin(), hlist.end());\n\thlist.erase(unique(hlist.begin(), hlist.end()), hlist.end());\n\tfor (int h : hlist) {\n\t\t// case 1: same heights\n\t\tvector<int> plist;\n\t\tfor (int i = 0; i < N; i++) {\n\t\t\tif (C[i].height == h) {\n\t\t\t\tplist.push_back(i);\n\t\t\t}\n\t\t}\n\t\tint S = plist.size();\n\t\tvector<vector<int> > ndp(S, vector<int>(S, INF));\n\t\tfor (int i = 1; i < (1 << S); i++) {\n\t\t\tvector<int> splist, sid;\n\t\t\tint subcost = 0;\n\t\t\tfor (int j = 0; j < S; j++) {\n\t\t\t\tif ((i >> j) & 1) {\n\t\t\t\t\tsplist.push_back(plist[j]);\n\t\t\t\t\tsid.push_back(j);\n\t\t\t\t\tsubcost += C[plist[j]].cost;\n\t\t\t\t}\n\t\t\t}\n\t\t\tint T = splist.size();\n\t\t\tvector<vector<int> > ds(T, vector<int>(T, INF));\n\t\t\tfor (int j = 0; j < T; j++) {\n\t\t\t\tfor (int k = 0; k < T; k++) {\n\t\t\t\t\tds[j][k] = (j != k ? G[splist[j]][splist[k]] : 0);\n\t\t\t\t}\n\t\t\t}\n\t\t\twarshall_floyd(T, ds);\n\t\t\tfor (int j = 0; j < T; j++) {\n\t\t\t\tfor (int k = 0; k < T; k++) {\n\t\t\t\t\tif (dp[splist[j]][splist[k]] != INF) {\n\t\t\t\t\t\tfor (int x = 0; x < T; x++) {\n\t\t\t\t\t\t\tfor (int y = 0; y < T; y++) {\n\t\t\t\t\t\t\t\tif (ds[j][x] != INF && ds[y][k] != INF) {\n\t\t\t\t\t\t\t\t\tndp[sid[x]][sid[y]] = min(ndp[sid[x]][sid[y]], dp[splist[j]][splist[k]] + ds[j][x] + ds[y][k] + subcost);\n\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\tfor (int j = 0; j < S; j++) {\n\t\t\tfor (int k = 0; k < S; k++) {\n\t\t\t\tif (ndp[j][k] != INF) {\n\t\t\t\t\tdp[plist[j]][plist[k]] = min(dp[plist[j]][plist[k]], ndp[j][k] - C[plist[j]].cost - C[plist[k]].cost);\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\n\t\t// case 2: different heights\n\t\tfor (int i = 0; i < N; i++) {\n\t\t\tfor (int j = 0; j < N; j++) {\n\t\t\t\tif (C[i].height == h && dp[i][j] != INF) {\n\t\t\t\t\tfor (int k = 0; k < N; k++) {\n\t\t\t\t\t\tif (d1[i][k] != INF) {\n\t\t\t\t\t\t\tdp[k][j] = min(dp[k][j], dp[i][j] + d1[i][k]);\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\tfor (int i = 0; i < N; i++) {\n\t\t\tfor (int j = 0; j < N; j++) {\n\t\t\t\tif (C[j].height == h && dp[i][j] != INF) {\n\t\t\t\t\tfor (int k = 0; k < N; k++) {\n\t\t\t\t\t\tif (d2[k][j] != INF) {\n\t\t\t\t\t\t\tdp[i][k] = min(dp[i][k], dp[i][j] + d2[k][j]);\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n\n\treturn dp[N - 1][N - 1];\n}\n\nint main() {\n\twhile (true) {\n\t\tint N, M;\n\t\tcin >> N >> M;\n\t\tif (N == 0 && M == 0) {\n\t\t\tbreak;\n\t\t}\n\t\tvector<city> C(N);\n\t\tfor (int i = 1; i < N - 1; i++) {\n\t\t\tcin >> C[i].cost >> C[i].height;\n\t\t}\n\t\tC[0].height = 0;\n\t\tC[N - 1].height = 1000;\n\t\tvector<vector<int> > G(N, vector<int>(N, INF));\n\t\tfor (int i = 0; i < M; i++) {\n\t\t\tint a, b, c;\n\t\t\tcin >> a >> b >> c;\n\t\t\ta -= 1;\n\t\t\tb -= 1;\n\t\t\tG[a][b] = min(G[a][b], c);\n\t\t}\n\t\tint ans = solve(N, C, G);\n\t\tcout << (ans != INF ? ans : -1) << endl;\n\t}\n\treturn 0;\n}", "accuracy": 1, "time_ms": 30, "memory_kb": 3416, "score_of_the_acc": -0.0092, "final_rank": 1 }, { "submission_id": "aoj_1324_7238070", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n#define foa(s, v) for(auto &s : v)\n#define all(v) v.begin(), v.end()\n#define REPname(a,b,c,d,...) d\n#define rep(...) REPname(__VA_ARGS__, REP2, REP1, REP0)(__VA_ARGS__)\n#define REP2(i,l,r) for(int i = l; i < r; i++)\n#define REP1(i, x) REP2(i,0,x)\n#define REP0(x) REP1(SPJ, x)\n#define sz(x) int(x.size())\n\ntemplate <class T>\nusing V=vector<T>;\n\ntemplate <class T>\nusing VV=vector<V<T>>;\n\ntemplate<class T>\nusing pqmin = priority_queue<T, V<T>, greater<T>>;\nusing ll = long long ;\nusing vi = vector<int>;\nusing vvi = vector<vi>;\nusing vvvi = vector<vvi>;\nusing vll = vector<ll>;\nusing vvll = V<vll>;\nconstexpr ll INF = 1e18;\nconstexpr int inf = 1e9;\ntemplate<class T>\ninline bool chmax(T &a, T b){\n\treturn a < b ? a=b, 1 : 0;\n}\ntemplate <class T>\ninline bool chmin(T &a, T b) {\n\treturn a > b ? a = b, 1 : 0;\n}\n\n\n// template <c0lass T>\nvoid view(ll x) {\n\tif(x > INF / 2) cerr << \"INF\";\n\telse cerr << x;\n}\n\n\ntemplate <class T>\nvoid view(T x) {\n\tcerr << x;\n}\n\ntemplate <class T>\nvoid view(V<T> v) {\n\tcerr << \"{ \";\n\tfoa(t, v) {view(t) ; cerr << \", \";}\n\tcerr << \"}\";\n}\n\n\ntemplate <class T>\nvoid view(VV<T> v) {\n\tcerr << \"{ \";\n\tfoa(t, v) {view(t) ; cerr << \",\\n\";}\n\tcerr << \"}\";\n\tcerr << endl;\n}\n\n\n// template <c0lass T>\nvoid view(int x) {\n\tcerr << x;\n}\n\n#ifdef LOCAL\nvoid debug_out() {}\ntemplate <class T>\nvoid debug_out(T x) {\n\tview(x);\n}\ntemplate <class H, class... T>\nvoid debug_out(H h, T... t) {\n\tview(h);\n\tcerr << \", \";\n\tdebug_out(t...);\n}\n#define debug(...) \\\n\tdo { \\\n\t\tcerr << __LINE__ << \" [\" << #__VA_ARGS__ << \"] : [\"; \\\n\t\tdebug_out(__VA_ARGS__); \\\n\t\tcerr << \"]\\n\"; \\\n\t} while(0)\n#else\n#define debug(...) (void(0))\n#endif\nusing vvi = V<vi>;\n\n\nll solve(int n){\n\tint m; cin >> m;\n\tint hmax = 1001;\n\tvector<int> height(n);\n\tvector<ll> visa(n);\n\tvector<vi> towns(hmax);\n\trep(i, 1, n - 1) {\n\t\tcin >> visa[i] >> height[i];\n\t}\n\theight.back() = hmax - 1;\n\n\tvector<int> order_height(n);\n\tiota(all(order_height), 0);\n\tsort(all(order_height), [&](int i, int j) { return height[i] < height[j]; });\n\n\tvector<int> mapsto(n);\n\trep(i, n) mapsto[order_height[i]] = i;\n\n\t{\n\t\tvector<int> w(n);\n\t\tvector<ll> fee(n);\n\t\trep(i, n) {\n\t\t\tint old = order_height[i];\n\t\t\tw[i] = height[old];\n\t\t\tfee[i] = visa[old];\n\t\t}\n\t\tswap(height, w);\n\t\tswap(fee, visa);\n\t}\n\n\trep(i, n) {\n\t\ttowns[height[i]].push_back(i);\n\t}\n\n\tvector<V<pair<int, ll>>> g(n), rg(n);\n\trep(m) {\n\t\tint a, b;\n\t\tll c; cin >> a >> b >> c;\n\t\ta--;\n\t\tb--;\n\t\ta = mapsto[a];\n\t\tb = mapsto[b];\n\t\tg[a].emplace_back(b, c);\n\t\trg[b].emplace_back(a, c);\n\t}\n\n\tV<vector<vector<vector<vll>>>> pt(hmax);\n\n\n\trep(h, hmax) {\n\t\tconst auto & grp = towns[h];\n\t\tif(grp.empty()) continue;\n\n\t\tdebug(grp);\n\t\tint mn = grp.front();\n\t\tint mx = grp.back();\n\t\tint len = mx - mn + 1;\n\t\tvector<vector<vector<vll>>> pair_trans(len, vector(len, vector(len, vector<ll>(len, INF))));\n\t\t// from, from , to, to\n\t\trep(i, len) rep(j, len) {\n\t\t\tll c = visa[i + mn];\n\t\t\tif(i != j ) c += visa[j+mn];\n\t\t\tpair_trans[i][j][i][j] = c;\n\t\t}\n\t\trep(use, (1 << len)) {\n\t\t\tvvll cost(len, vll(len, INF));\n\t\t\tll visas = 0;\n\t\t\trep(i, len) {\n\t\t\t\tif(!(use & (1 << i))) continue;\n\t\t\t\tcost[i][i] = 0;\n\t\t\t\tint from = i + mn;\n\t\t\t\tvisas += visa[from];\n\t\t\t\tfor(const auto& [to, ecost] : g[from]) {\n\t\t\t\t\tdebug(from, to, ecost, mn, mx, use);\n\t\t\t\t\tif(mn <= to && to <= mx && (use & (1 << (to - mn)))) {\n\t\t\t\t\t\tdebug(from, to, ecost);\n\t\t\t\t\t\tchmin(cost[i][to - mn], ecost);\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t\trep(k, len) rep(i, len) rep(j, len) chmin(cost[i][j], cost.at(i).at(k) + cost.at(k).at(j));\n\n\t\t\tdebug(use, cost);\n\n\t\t\trep(i, len) rep(j, len) rep(k, len) rep(l, len) {\n\t\t\t\tchmin(pair_trans[i].at(j).at(k).at(l), cost[i][k] + cost[l][j] + visas);\n\t\t\t}\n\t\t}\n\n\t\trep(i, len) rep(j, len) rep(k, len) rep(l, len) \n\t\t{\n\t\t\tauto &t = pair_trans[i][j][k][l];\n\t\t\tif(t < INF / 2) debug(i, j, k, l, t);\n\t\t}\n\n\t\tpt[h] = pair_trans;\n\t}\n\n\n\tpqmin<tuple<ll, int, int>> q;\n\tq.emplace(0LL, 0, 0);\n\tvector<vector<ll>> dist(n, vll(n, INF));\n\tdist.front().front() = 0;\n\n\twhile(q.size()) {\n\t\tll d;\n\t\tint fth,bck;\n\t\ttie(d, fth, bck) = q.top();\n\t\tq.pop();\n\t\tif(fth == bck && bck == n - 1) return d;\n\t\tif(d > dist[fth].at(bck)) continue;\n\n\t\tdebug(d, fth, bck);\n\n\t\tauto set_nxt = [&](int fnxt, int bnxt, ll val ) {\n\t\t\tif(val < 0) exit(1);\n\t\t\tif(chmin(dist.at(fnxt).at(bnxt), val)) {\n\t\t\t\tq.emplace(val, fnxt, bnxt);\n\t\t\t}\n\t\t};\n\n\t\tif(height[fth] == height[bck]) {\n\t\t\tauto& p = pt.at(height.at(fth));\n\t\t\tconst int len = sz(p);\n\t\t\tconst auto& current = towns[height[fth]];\n\n\t\t\tint fromf = fth - current.front();\n\t\t\tint fromb = bck - current.front();\n\t\t\trep(i, len) rep(j, len) {\n\t\t\t\tint tof = current[i];\n\t\t\t\tint tob = current[j];\n\t\t\t\tll cost = p[fromf].at(fromb).at(i).at(j) + d - visa[fth];\n\t\t\t\tif(fth != bck) cost -= visa[bck];\n\t\t\t\tdebug(fth, bck, d, tof, tob, cost, p[fromf].at(fromb).at(i).at(j));\n\t\t\t\tset_nxt(tof, tob, cost);\n\t\t\t}\n\t\t}\n\t\tif(height[fth] <= height[bck]) {\n\t\t\tfor(auto [nxt, ed_cost] : g[fth]) {\n\t\t\t\tif(height[nxt] >= height[fth]) {\n\t\t\t\t\tll cost = d + ed_cost;\n\t\t\t\t\tif(nxt != bck) cost += visa[nxt];\n\t\t\t\t\tset_nxt(nxt, bck, cost);\n\t\t\t\t}\n\t\t\t}\n\t\t} else {\n\t\t\tfor(auto [nxt, ed_cost] : rg[bck]) {\n\t\t\t\tif(height[nxt] >= height[bck]) {\n\t\t\t\t\tll cost = d + ed_cost;\n\t\t\t\t\tif(nxt != fth) cost += visa[nxt];\n\t\t\t\t\tset_nxt(fth, nxt, cost);\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n\n\treturn -1;\n}\n\nint main() {\n\tios::sync_with_stdio(false);\n\tcin.tie(nullptr);\n\tcout << fixed << setprecision(15);\n\tsrand((unsigned)time(NULL));\n\tint n;\n\twhile(cin >> n && n) cout << solve(n) << '\\n';\n}", "accuracy": 1, "time_ms": 140, "memory_kb": 4388, "score_of_the_acc": -0.0402, "final_rank": 6 }, { "submission_id": "aoj_1324_6722303", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\n#define rep(i, s, t) for (int i = (int)(s); i < (int)(t); ++i)\n#define revrep(i, t, s) for (int i = (int)(t)-1; i >= (int)(s); --i)\n#define all(x) begin(x), end(x)\ntemplate <typename T> bool chmax(T& a, const T& b) { return a < b ? (a = b, 1) : 0; }\ntemplate <typename T> bool chmin(T& a, const T& b) { return a > b ? (a = b, 1) : 0; }\n\ntemplate <typename T>\nostream& operator<<(ostream& os, const vector<T>& v) {\n for (int i = 0; i < (int) v.size(); ++i) os << v[i] << \" \";\n return os;\n}\n\nint main() {\n ios_base::sync_with_stdio(false);\n cin.tie(nullptr);\n cout << fixed << setprecision(15);\n\n while (true) {\n int n, m;\n cin >> n >> m;\n if (n == 0) break;\n vector<int> d(n), e(n);\n e[n-1] = 1000;\n vector<vector<int>> cities(1001);\n cities[0].push_back(0);\n cities[1000].push_back(n-1);\n rep(i,1,n-1) {\n cin >> d[i] >> e[i];\n cities[e[i]].push_back(i);\n }\n vector<vector<pair<int, int>>> G(n);\n rep(_,0,m) {\n int a, b, c;\n cin >> a >> b >> c;\n --a, --b;\n G[a].push_back({b, c});\n }\n\n vector<vector<int>> dp(n, vector<int>(n, 1e8)); // cost to go from i (up) to j (down)\n dp[n-1][n-1] = 0;\n\n revrep(a,1000,0) {\n int N = cities[a].size();\n rep(S,0,1<<N) {\n vector<vector<int>> up(n, vector<int>(n, 1e8)), down(n, vector<int>(n, 1e8));\n rep(i,0,n) up[i][i] = down[i][i] = 0;\n int dd = 0;\n rep(i,0,n) {\n if (e[i] < a) continue;\n if (e[i] == a) {\n auto j = find(all(cities[a]), i) - cities[a].begin();\n if (!(S>>j&1)) continue;\n dd += d[i];\n }\n for (auto [j, c] : G[i]) {\n if (e[i] <= e[j]) up[i][j] = c + d[i];\n if (e[i] >= e[j]) down[i][j] = c + d[j];\n if (e[i] == a) up[i][j] -= d[i];\n if (e[j] == a) down[i][j] -= d[j];\n }\n }\n\n rep(k,0,n) rep(i,0,n) rep(j,0,n) chmin(up[i][j], up[i][k] + up[k][j]);\n rep(k,0,n) rep(i,0,n) rep(j,0,n) chmin(down[i][j], down[i][k] + down[k][j]);\n\n for (int i : cities[a]) for (int j : cities[a]) {\n int k = find(all(cities[a]), i) - cities[a].begin();\n if (!(S>>k&1)) continue;\n k = find(all(cities[a]), j) - cities[a].begin();\n if (!(S>>k&1)) continue;\n\n rep(k,0,n) rep(l,0,n) if (a < e[k] && a < e[l]) {\n chmin(dp[i][j], up[i][k] + dp[k][l] + down[l][j] + dd);\n }\n }\n }\n }\n cout << (dp[0][0] < 1e8 ? dp[0][0] : -1) << endl;\n }\n}", "accuracy": 1, "time_ms": 3910, "memory_kb": 3260, "score_of_the_acc": -0.5833, "final_rank": 13 }, { "submission_id": "aoj_1324_6718504", "code_snippet": "#include <bits/stdc++.h>\n\n#ifdef _MSC_VER\n# include <intrin.h>\n#else\n# include <x86intrin.h>\n#endif\n\n#include <limits>\n#include <type_traits>\n\nnamespace suisen {\n// ! utility\ntemplate <typename ...Types>\nusing constraints_t = std::enable_if_t<std::conjunction_v<Types...>, std::nullptr_t>;\ntemplate <bool cond_v, typename Then, typename OrElse>\nconstexpr decltype(auto) constexpr_if(Then&& then, OrElse&& or_else) {\n\tif constexpr (cond_v) {\n\t\treturn std::forward<Then>(then);\n\t} else {\n\t\treturn std::forward<OrElse>(or_else);\n\t}\n}\n\n// ! function\ntemplate <typename ReturnType, typename Callable, typename ...Args>\nusing is_same_as_invoke_result = std::is_same<std::invoke_result_t<Callable, Args...>, ReturnType>;\ntemplate <typename F, typename T>\nusing is_uni_op = is_same_as_invoke_result<T, F, T>;\ntemplate <typename F, typename T>\nusing is_bin_op = is_same_as_invoke_result<T, F, T, T>;\n\ntemplate <typename Comparator, typename T>\nusing is_comparator = std::is_same<std::invoke_result_t<Comparator, T, T>, bool>;\n\n// ! integral\ntemplate <typename T, typename = constraints_t<std::is_integral<T>>>\nconstexpr int bit_num = std::numeric_limits<std::make_unsigned_t<T>>::digits;\ntemplate <typename T, unsigned int n>\nstruct is_nbit { static constexpr bool value = bit_num<T> == n; };\ntemplate <typename T, unsigned int n>\nstatic constexpr bool is_nbit_v = is_nbit<T, n>::value;\n\n// ?\ntemplate <typename T>\nstruct safely_multipliable {};\ntemplate <>\nstruct safely_multipliable<int> { using type = long long; };\ntemplate <>\nstruct safely_multipliable<long long> { using type = __int128_t; };\ntemplate <>\nstruct safely_multipliable<unsigned int> { using type = unsigned long long; };\ntemplate <>\nstruct safely_multipliable<unsigned long int> { using type = __uint128_t; };\ntemplate <>\nstruct safely_multipliable<unsigned long long> { using type = __uint128_t; };\ntemplate <>\nstruct safely_multipliable<float> { using type = float; };\ntemplate <>\nstruct safely_multipliable<double> { using type = double; };\ntemplate <>\nstruct safely_multipliable<long double> { using type = long double; };\ntemplate <typename T>\nusing safely_multipliable_t = typename safely_multipliable<T>::type;\n\ntemplate <typename T, typename = void>\nstruct rec_value_type {\n\tusing type = T;\n};\ntemplate <typename T>\nstruct rec_value_type<T, std::void_t<typename T::value_type>> {\n\tusing type = typename rec_value_type<typename T::value_type>::type;\n};\ntemplate <typename T>\nusing rec_value_type_t = typename rec_value_type<T>::type;\n\n} // namespace suisen\n\n// ! type aliases\nusing i128 = __int128_t;\nusing u128 = __uint128_t;\n\ntemplate <typename T>\nusing pq_greater = std::priority_queue<T, std::vector<T>, std::greater<T>>;\ntemplate <typename T, typename U>\nusing umap = std::unordered_map<T, U>;\n\n// ! macros (capital: internal macro)\n#define OVERLOAD2(_1,_2,name,...) name\n#define OVERLOAD3(_1,_2,_3,name,...) name\n#define OVERLOAD4(_1,_2,_3,_4,name,...) name\n\n#define REP4(i,l,r,s) for(std::remove_reference_t<std::remove_const_t<decltype(r)>>i=(l);i<(r);i+=(s))\n#define REP3(i,l,r)\tREP4(i,l,r,1)\n#define REP2(i,n)\t REP3(i,0,n)\n#define REPINF3(i,l,s) for(std::remove_reference_t<std::remove_const_t<decltype(l)>>i=(l);;i+=(s))\n#define REPINF2(i,l) REPINF3(i,l,1)\n#define REPINF1(i)\t REPINF2(i,0)\n#define RREP4(i,l,r,s) for(std::remove_reference_t<std::remove_const_t<decltype(r)>>i=(l)+fld((r)-(l)-1,s)*(s);i>=(l);i-=(s))\n#define RREP3(i,l,r) RREP4(i,l,r,1)\n#define RREP2(i,n)\t RREP3(i,0,n)\n\n#define rep(...)\tOVERLOAD4(__VA_ARGS__, REP4 , REP3 , REP2 )(__VA_ARGS__)\n#define rrep(...) OVERLOAD4(__VA_ARGS__, RREP4 , RREP3 , RREP2 )(__VA_ARGS__)\n#define repinf(...) OVERLOAD3(__VA_ARGS__, REPINF3, REPINF2, REPINF1)(__VA_ARGS__)\n\n#define CAT_I(a, b) a##b\n#define CAT(a, b) CAT_I(a, b)\n#define UNIQVAR(tag) CAT(tag, __LINE__)\n#define loop(n) for (std::remove_reference_t<std::remove_const_t<decltype(n)>> UNIQVAR(loop_variable) = n; UNIQVAR(loop_variable) --> 0;)\n\n#define all(iterable) std::begin(iterable), std::end(iterable)\n#define input(type, ...) type __VA_ARGS__; read(__VA_ARGS__)\n\n#ifdef LOCAL\n# define debug(...) debug_internal(#__VA_ARGS__, __VA_ARGS__)\n\ntemplate <class T, class... Args>\nvoid debug_internal(const char* s, T&& first, Args&&... args) {\n\tconstexpr const char* prefix = \"[\\033[32mDEBUG\\033[m] \";\n\tconstexpr const char* open_brakets = sizeof...(args) == 0 ? \"\" : \"(\";\n\tconstexpr const char* close_brakets = sizeof...(args) == 0 ? \"\" : \")\";\n\tstd::cerr << prefix << open_brakets << s << close_brakets << \": \" << open_brakets << std::forward<T>(first);\n\t((std::cerr << \", \" << std::forward<Args>(args)), ...);\n\tstd::cerr << close_brakets << \"\\n\";\n}\n\n#else\n# define debug(...) void(0)\n#endif\n\n// ! I/O utilities\n\n// __int128_t\nstd::ostream& operator<<(std::ostream& dest, __int128_t value) {\n\tstd::ostream::sentry s(dest);\n\tif (s) {\n\t\t__uint128_t tmp = value < 0 ? -value : value;\n\t\tchar buffer[128];\n\t\tchar* d = std::end(buffer);\n\t\tdo {\n\t\t\t--d;\n\t\t\t*d = \"0123456789\"[tmp % 10];\n\t\t\ttmp /= 10;\n\t\t} while (tmp != 0);\n\t\tif (value < 0) {\n\t\t\t--d;\n\t\t\t*d = '-';\n\t\t}\n\t\tint len = std::end(buffer) - d;\n\t\tif (dest.rdbuf()->sputn(d, len) != len) {\n\t\t\tdest.setstate(std::ios_base::badbit);\n\t\t}\n\t}\n\treturn dest;\n}\n// __uint128_t\nstd::ostream& operator<<(std::ostream& dest, __uint128_t value) {\n\tstd::ostream::sentry s(dest);\n\tif (s) {\n\t\tchar buffer[128];\n\t\tchar* d = std::end(buffer);\n\t\tdo {\n\t\t\t--d;\n\t\t\t*d = \"0123456789\"[value % 10];\n\t\t\tvalue /= 10;\n\t\t} while (value != 0);\n\t\tint len = std::end(buffer) - d;\n\t\tif (dest.rdbuf()->sputn(d, len) != len) {\n\t\t\tdest.setstate(std::ios_base::badbit);\n\t\t}\n\t}\n\treturn dest;\n}\n\n// pair\ntemplate <typename T, typename U>\nstd::ostream& operator<<(std::ostream& out, const std::pair<T, U>& a) {\n\treturn out << a.first << ' ' << a.second;\n}\n// tuple\ntemplate <unsigned int N = 0, typename ...Args>\nstd::ostream& operator<<(std::ostream& out, const std::tuple<Args...>& a) {\n\tif constexpr (N >= std::tuple_size_v<std::tuple<Args...>>) {\n\t\treturn out;\n\t} else {\n\t\tout << std::get<N>(a);\n\t\tif constexpr (N + 1 < std::tuple_size_v<std::tuple<Args...>>) {\n\t\t\tout << ' ';\n\t\t}\n\t\treturn operator<<<N + 1>(out, a);\n\t}\n}\n// vector\ntemplate <typename T>\nstd::ostream& operator<<(std::ostream& out, const std::vector<T>& a) {\n\tfor (auto it = a.begin(); it != a.end();) {\n\t\tout << *it;\n\t\tif (++it != a.end()) out << ' ';\n\t}\n\treturn out;\n}\n// array\ntemplate <typename T, size_t N>\nstd::ostream& operator<<(std::ostream& out, const std::array<T, N>& a) {\n\tfor (auto it = a.begin(); it != a.end();) {\n\t\tout << *it;\n\t\tif (++it != a.end()) out << ' ';\n\t}\n\treturn out;\n}\ninline void print() { std::cout << '\\n'; }\ntemplate <typename Head, typename... Tail>\ninline void print(const Head& head, const Tail &...tails) {\n\tstd::cout << head;\n\tif (sizeof...(tails)) std::cout << ' ';\n\tprint(tails...);\n}\ntemplate <typename Iterable>\nauto print_all(const Iterable& v, std::string sep = \" \", std::string end = \"\\n\") -> decltype(std::cout << *v.begin(), void()) {\n\tfor (auto it = v.begin(); it != v.end();) {\n\t\tstd::cout << *it;\n\t\tif (++it != v.end()) std::cout << sep;\n\t}\n\tstd::cout << end;\n}\n\n__int128_t parse_i128(std::string& s) {\n\t__int128_t ret = 0;\n\tfor (int i = 0; i < int(s.size()); i++) if ('0' <= s[i] and s[i] <= '9') ret = 10 * ret + s[i] - '0';\n\tif (s[0] == '-') ret = -ret;\n\treturn ret;\n}\n__uint128_t parse_u128(std::string& s) {\n\t__uint128_t ret = 0;\n\tfor (int i = 0; i < int(s.size()); i++) if ('0' <= s[i] and s[i] <= '9') ret = 10 * ret + s[i] - '0';\n\treturn ret;\n}\n// __int128_t\nstd::istream& operator>>(std::istream& in, __int128_t& v) {\n\tstd::string s;\n\tin >> s;\n\tv = parse_i128(s);\n\treturn in;\n}\n// __uint128_t\nstd::istream& operator>>(std::istream& in, __uint128_t& v) {\n\tstd::string s;\n\tin >> s;\n\tv = parse_u128(s);\n\treturn in;\n}\n// pair\ntemplate <typename T, typename U>\nstd::istream& operator>>(std::istream& in, std::pair<T, U>& a) {\n\treturn in >> a.first >> a.second;\n}\n// tuple\ntemplate <unsigned int N = 0, typename ...Args>\nstd::istream& operator>>(std::istream& in, std::tuple<Args...>& a) {\n\tif constexpr (N >= std::tuple_size_v<std::tuple<Args...>>) {\n\t\treturn in;\n\t} else {\n\t\treturn operator>><N + 1>(in >> std::get<N>(a), a);\n\t}\n}\n// vector\ntemplate <typename T>\nstd::istream& operator>>(std::istream& in, std::vector<T>& a) {\n\tfor (auto it = a.begin(); it != a.end(); ++it) in >> *it;\n\treturn in;\n}\n// array\ntemplate <typename T, size_t N>\nstd::istream& operator>>(std::istream& in, std::array<T, N>& a) {\n\tfor (auto it = a.begin(); it != a.end(); ++it) in >> *it;\n\treturn in;\n}\ntemplate <typename ...Args>\nvoid read(Args &...args) {\n\t(std::cin >> ... >> args);\n}\n\n// ! integral utilities\n\n// Returns pow(-1, n)\ntemplate <typename T>\nconstexpr inline int pow_m1(T n) {\n\treturn -(n & 1) | 1;\n}\n// Returns pow(-1, n)\ntemplate <>\nconstexpr inline int pow_m1<bool>(bool n) {\n\treturn -int(n) | 1;\n}\n\n// Returns floor(x / y)\ntemplate <typename T>\nconstexpr inline T fld(const T x, const T y) {\n\treturn (x ^ y) >= 0 ? x / y : (x - (y + pow_m1(y >= 0))) / y;\n}\ntemplate <typename T>\nconstexpr inline T cld(const T x, const T y) {\n\treturn (x ^ y) <= 0 ? x / y : (x + (y + pow_m1(y >= 0))) / y;\n}\n\ntemplate <typename T, suisen::constraints_t<suisen::is_nbit<T, 16>> = nullptr>\n__attribute__((target(\"popcnt\"))) constexpr inline int popcount(const T x) { return _mm_popcnt_u32(x); }\ntemplate <typename T, suisen::constraints_t<suisen::is_nbit<T, 32>> = nullptr>\n__attribute__((target(\"popcnt\"))) constexpr inline int popcount(const T x) { return _mm_popcnt_u32(x); }\ntemplate <typename T, suisen::constraints_t<suisen::is_nbit<T, 64>> = nullptr>\n__attribute__((target(\"popcnt\"))) constexpr inline int popcount(const T x) { return _mm_popcnt_u64(x); }\ntemplate <typename T, suisen::constraints_t<suisen::is_nbit<T, 16>> = nullptr>\nconstexpr inline int count_lz(const T x) { return x ? __builtin_clz(x) : suisen::bit_num<T>; }\ntemplate <typename T, suisen::constraints_t<suisen::is_nbit<T, 32>> = nullptr>\nconstexpr inline int count_lz(const T x) { return x ? __builtin_clz(x) : suisen::bit_num<T>; }\ntemplate <typename T, suisen::constraints_t<suisen::is_nbit<T, 64>> = nullptr>\nconstexpr inline int count_lz(const T x) { return x ? __builtin_clzll(x) : suisen::bit_num<T>; }\ntemplate <typename T, suisen::constraints_t<suisen::is_nbit<T, 16>> = nullptr>\nconstexpr inline int count_tz(const T x) { return x ? __builtin_ctz(x) : suisen::bit_num<T>; }\ntemplate <typename T, suisen::constraints_t<suisen::is_nbit<T, 32>> = nullptr>\nconstexpr inline int count_tz(const T x) { return x ? __builtin_ctz(x) : suisen::bit_num<T>; }\ntemplate <typename T, suisen::constraints_t<suisen::is_nbit<T, 64>> = nullptr>\nconstexpr inline int count_tz(const T x) { return x ? __builtin_ctzll(x) : suisen::bit_num<T>; }\ntemplate <typename T>\nconstexpr inline int floor_log2(const T x) { return suisen::bit_num<T> -1 - count_lz(x); }\ntemplate <typename T>\nconstexpr inline int ceil_log2(const T x) { return floor_log2(x) + ((x & -x) != x); }\ntemplate <typename T>\nconstexpr inline int kth_bit(const T x, const unsigned int k) { return (x >> k) & 1; }\ntemplate <typename T>\nconstexpr inline int parity(const T x) { return popcount(x) & 1; }\n\n// ! container\n\ntemplate <typename T, typename Comparator, suisen::constraints_t<suisen::is_comparator<Comparator, T>> = nullptr>\nauto priqueue_comp(const Comparator comparator) {\n\treturn std::priority_queue<T, std::vector<T>, Comparator>(comparator);\n}\n\ntemplate <typename Iterable>\nauto isize(const Iterable& iterable) -> decltype(int(iterable.size())) {\n\treturn iterable.size();\n}\n\ntemplate <typename T, typename Gen, suisen::constraints_t<suisen::is_same_as_invoke_result<T, Gen, int>> = nullptr>\nauto generate_vector(int n, Gen generator) {\n\tstd::vector<T> v(n);\n\tfor (int i = 0; i < n; ++i) v[i] = generator(i);\n\treturn v;\n}\ntemplate <typename T>\nauto generate_range_vector(T l, T r) {\n\treturn generate_vector(r - l, [l](int i) { return l + i; });\n}\ntemplate <typename T>\nauto generate_range_vector(T n) {\n\treturn generate_range_vector(0, n);\n}\n\ntemplate <typename T>\nvoid sort_unique_erase(std::vector<T>& a) {\n\tstd::sort(a.begin(), a.end());\n\ta.erase(std::unique(a.begin(), a.end()), a.end());\n}\n\ntemplate <typename InputIterator, typename BiConsumer>\nauto foreach_adjacent_values(InputIterator first, InputIterator last, BiConsumer f) -> decltype(f(*first++, *last), void()) {\n\tif (first != last) for (auto itr = first, itl = itr++; itr != last; itl = itr++) f(*itl, *itr);\n}\ntemplate <typename Container, typename BiConsumer>\nauto foreach_adjacent_values(Container c, BiConsumer f) -> decltype(c.begin(), c.end(), void()) {\n\tforeach_adjacent_values(c.begin(), c.end(), f);\n}\n\n// ! other utilities\n\n// x <- min(x, y). returns true iff `x` has chenged.\ntemplate <typename T>\ninline bool chmin(T& x, const T& y) {\n\tif (y >= x) return false;\n\tx = y;\n\treturn true;\n}\n// x <- max(x, y). returns true iff `x` has chenged.\ntemplate <typename T>\ninline bool chmax(T& x, const T& y) {\n\tif (y <= x) return false;\n\tx = y;\n\treturn true;\n}\n\ntemplate <typename T, std::enable_if_t<std::is_integral_v<T>, std::nullptr_t> = nullptr>\nstd::string bin(T val, int bit_num = -1) {\n\tstd::string res;\n\tif (bit_num >= 0) {\n\t\tfor (int bit = bit_num; bit-- > 0;) res += '0' + ((val >> bit) & 1);\n\t} else {\n\t\tfor (; val; val >>= 1) res += '0' + (val & 1);\n\t\tstd::reverse(res.begin(), res.end());\n\t}\n\treturn res;\n}\n\ntemplate <typename T, std::enable_if_t<std::is_integral_v<T>, std::nullptr_t> = nullptr>\nstd::vector<T> digits_low_to_high(T val, T base = 10) {\n\tstd::vector<T> res;\n\tfor (; val; val /= base) res.push_back(val % base);\n\tif (res.empty()) res.push_back(T{ 0 });\n\treturn res;\n}\ntemplate <typename T, std::enable_if_t<std::is_integral_v<T>, std::nullptr_t> = nullptr>\nstd::vector<T> digits_high_to_low(T val, T base = 10) {\n\tauto res = digits_low_to_high(val, base);\n\tstd::reverse(res.begin(), res.end());\n\treturn res;\n}\n\ntemplate <typename T>\nstd::string join(const std::vector<T>& v, const std::string& sep, const std::string& end) {\n\tstd::ostringstream ss;\n\tfor (auto it = v.begin(); it != v.end();) {\n\t\tss << *it;\n\t\tif (++it != v.end()) ss << sep;\n\t}\n\tss << end;\n\treturn ss.str();\n}\n\nnamespace suisen {}\nusing namespace suisen;\nusing namespace std;\n\nstruct io_setup {\n\tio_setup(int precision = 20) {\n\t\tstd::ios::sync_with_stdio(false);\n\t\tstd::cin.tie(nullptr);\n\t\tstd::cout << std::fixed << std::setprecision(precision);\n\t}\n} io_setup_ {};\n\n// ! code from here\n\n#include <algorithm>\n#include <cassert>\n#include <vector>\n\nnamespace suisen {\ntemplate <typename T>\nclass CoordinateCompressorBuilder {\n\tpublic:\n\t\tstruct Compressor {\n\t\t\tpublic:\n\t\t\t\tstatic constexpr int absent = -1;\n\n\t\t\t\t// default constructor\n\t\t\t\tCompressor() : _xs(std::vector<T>{}) {}\n\t\t\t\t// Construct from strictly sorted vector\n\t\t\t\tCompressor(const std::vector<T> &xs) : _xs(xs) {\n\t\t\t\t\tassert(is_strictly_sorted(xs));\n\t\t\t\t}\n\n\t\t\t\t// Return the number of distinct keys.\n\t\t\t\tint size() const {\n\t\t\t\t\treturn _xs.size();\n\t\t\t\t}\n\t\t\t\t// Check if the element is registered.\n\t\t\t\tbool has_key(const T &e) const {\n\t\t\t\t\treturn std::binary_search(_xs.begin(), _xs.end(), e);\n\t\t\t\t}\n\t\t\t\t// Compress the element. if not registered, returns `default_value`. (default: Compressor::absent)\n\t\t\t\tint comp(const T &e, int default_value = absent) const {\n\t\t\t\t\tconst int res = min_geq_index(e);\n\t\t\t\t\treturn res != size() and _xs[res] == e ? res : default_value;\n\t\t\t\t}\n\t\t\t\t// Restore the element from the index.\n\t\t\t\tT decomp(const int compressed_index) const {\n\t\t\t\t\treturn _xs[compressed_index];\n\t\t\t\t}\n\t\t\t\t// Compress the element. Equivalent to call `comp(e)`\n\t\t\t\tint operator[](const T &e) const {\n\t\t\t\t\treturn comp(e);\n\t\t\t\t}\n\t\t\t\t// Return the minimum registered value greater than `e`. if not exists, return `default_value`.\n\t\t\t\tT min_gt(const T &e, const T &default_value) const {\n\t\t\t\t\tauto it = std::upper_bound(_xs.begin(), _xs.end(), e);\n\t\t\t\t\treturn it == _xs.end() ? default_value : *it;\n\t\t\t\t}\n\t\t\t\t// Return the minimum registered value greater than or equal to `e`. if not exists, return `default_value`.\n\t\t\t\tT min_geq(const T &e, const T &default_value) const {\n\t\t\t\t\tauto it = std::lower_bound(_xs.begin(), _xs.end(), e);\n\t\t\t\t\treturn it == _xs.end() ? default_value : *it;\n\t\t\t\t}\n\t\t\t\t// Return the maximum registered value less than `e`. if not exists, return `default_value`\n\t\t\t\tT max_lt(const T &e, const T &default_value) const {\n\t\t\t\t\tauto it = std::upper_bound(_xs.rbegin(), _xs.rend(), e, std::greater<T>());\n\t\t\t\t\treturn it == _xs.rend() ? default_value : *it;\n\t\t\t\t}\n\t\t\t\t// Return the maximum registered value less than or equal to `e`. if not exists, return `default_value`\n\t\t\t\tT max_leq(const T &e, const T &default_value) const {\n\t\t\t\t\tauto it = std::lower_bound(_xs.rbegin(), _xs.rend(), e, std::greater<T>());\n\t\t\t\t\treturn it == _xs.rend() ? default_value : *it;\n\t\t\t\t}\n\t\t\t\t// Return the compressed index of the minimum registered value greater than `e`. if not exists, return `compressor.size()`.\n\t\t\t\tint min_gt_index(const T &e) const {\n\t\t\t\t\treturn std::upper_bound(_xs.begin(), _xs.end(), e) - _xs.begin();\n\t\t\t\t}\n\t\t\t\t// Return the compressed index of the minimum registered value greater than or equal to `e`. if not exists, return `compressor.size()`.\n\t\t\t\tint min_geq_index(const T &e) const {\n\t\t\t\t\treturn std::lower_bound(_xs.begin(), _xs.end(), e) - _xs.begin();\n\t\t\t\t}\n\t\t\t\t// Return the compressed index of the maximum registered value less than `e`. if not exists, return -1.\n\t\t\t\tint max_lt_index(const T &e) const {\n\t\t\t\t\treturn int(_xs.rend() - std::upper_bound(_xs.rbegin(), _xs.rend(), e, std::greater<T>())) - 1;\n\t\t\t\t}\n\t\t\t\t// Return the compressed index of the maximum registered value less than or equal to `e`. if not exists, return -1.\n\t\t\t\tint max_leq_index(const T &e) const {\n\t\t\t\t\treturn int(_xs.rend() - std::lower_bound(_xs.rbegin(), _xs.rend(), e, std::greater<T>())) - 1;\n\t\t\t\t}\n\t\t\tprivate:\n\t\t\t\tstd::vector<T> _xs;\n\t\t\t\tstatic bool is_strictly_sorted(const std::vector<T> &v) {\n\t\t\t\t\treturn std::adjacent_find(v.begin(), v.end(), std::greater_equal<T>()) == v.end();\n\t\t\t\t}\n\t\t};\n\t\tCoordinateCompressorBuilder() : _xs(std::vector<T>{}) {}\n\t\texplicit CoordinateCompressorBuilder(const std::vector<T> &xs) : _xs(xs) {}\n\t\texplicit CoordinateCompressorBuilder(std::vector<T> &&xs) : _xs(std::move(xs)) {}\n\t\ttemplate <typename Gen, constraints_t<is_same_as_invoke_result<T, Gen, int>> = nullptr>\n\t\tCoordinateCompressorBuilder(const int n, Gen generator) {\n\t\t\treserve(n);\n\t\t\tfor (int i = 0; i < n; ++i) push(generator(i));\n\t\t}\n\t\t// Attempt to preallocate enough memory for specified number of elements.\n\t\tvoid reserve(int n) {\n\t\t\t_xs.reserve(n);\n\t\t}\n\t\t// Add data.\n\t\tvoid push(const T &first) {\n\t\t\t_xs.push_back(first);\n\t\t}\n\t\t// Add data.\n\t\tvoid push(T &&first) {\n\t\t\t_xs.push_back(std::move(first));\n\t\t}\n\t\t// Add data in the range of [first, last). \n\t\ttemplate <typename Iterator>\n\t\tauto push(const Iterator &first, const Iterator &last) -> decltype(std::vector<T>{}.push_back(*first), void()) {\n\t\t\tfor (auto it = first; it != last; ++it) _xs.push_back(*it);\n\t\t}\n\t\t// Add all data in the container. Equivalent to `push(iterable.begin(), iterable.end())`.\n\t\ttemplate <typename Iterable>\n\t\tauto push(const Iterable &iterable) -> decltype(std::vector<T>{}.push_back(*iterable.begin()), void()) {\n\t\t\tpush(iterable.begin(), iterable.end());\n\t\t}\n\t\t// Add data.\n\t\ttemplate <typename ...Args>\n\t\tvoid emplace(Args &&...args) {\n\t\t\t_xs.emplace_back(std::forward<Args>(args)...);\n\t\t}\n\t\t// Build compressor.\n\t\tauto build() {\n\t\t\tstd::sort(_xs.begin(), _xs.end()), _xs.erase(std::unique(_xs.begin(), _xs.end()), _xs.end());\n\t\t\treturn Compressor {_xs};\n\t\t}\n\t\t// Build compressor from vector.\n\t\tstatic auto build(const std::vector<T> &xs) {\n\t\t\treturn CoordinateCompressorBuilder(xs).build();\n\t\t}\n\t\t// Build compressor from vector.\n\t\tstatic auto build(std::vector<T> &&xs) {\n\t\t\treturn CoordinateCompressorBuilder(std::move(xs)).build();\n\t\t}\n\t\t// Build compressor from generator.\n\t\ttemplate <typename Gen, constraints_t<is_same_as_invoke_result<T, Gen, int>> = nullptr>\n\t\tstatic auto build(const int n, Gen generator) {\n\t\t\treturn CoordinateCompressorBuilder<T>(n, generator).build();\n\t\t}\n\tprivate:\n\t\tstd::vector<T> _xs;\n};\n\n} // namespace suisen\n\n#ifdef _MSC_VER\n# include <intrin.h>\n#else\n# include <x86intrin.h>\n#endif\n\n#include <cstdint>\n#include <iostream>\n\nnamespace suisen {\n\ttemplate <typename T, std::enable_if_t<std::is_integral_v<T>, std::nullptr_t> = nullptr>\n\tstruct all_subset {\n\t\tstruct all_subset_iter {\n\t\t\tconst T s; T t;\n\t\t\tconstexpr all_subset_iter(T s) : s(s), t(s + 1) {}\n\t\t\tconstexpr auto operator*() const { return t; }\n\t\t\tconstexpr auto operator++() {}\n\t\t\tconstexpr auto operator!=(std::nullptr_t) { return t ? (--t &= s, true) : false; }\n\t\t};\n\t\tT s;\n\t\tconstexpr all_subset(T s) : s(s) {}\n\t\tconstexpr auto begin() { return all_subset_iter(s); }\n\t\tconstexpr auto end() { return nullptr; }\n\t};\n\n\t// iterator over T s.t. T is subset of S and |T| = k\n\tstruct all_subset_k {\n\t\tstruct all_subset_k_iter {\n\t\t\tconst uint32_t n, k, s;\n\t\t\tuint32_t t;\n\t\t\t__attribute__((target(\"avx2\")))\n\t\t\tall_subset_k_iter(uint32_t s, uint32_t k) : n(uint32_t(1) << _mm_popcnt_u32(s)), k(k), s(s), t((uint32_t(1) << k) - 1) {}\n\t\t\t__attribute__((target(\"bmi2\")))\n\t\t\tauto operator*() const { return _pdep_u32(t, s); }\n\t\t\t__attribute__((target(\"bmi\")))\n\t\t\tauto operator++() {\n\t\t\t\tif (k == 0) {\n\t\t\t\t\tt = std::numeric_limits<uint32_t>::max();\n\t\t\t\t} else {\n\t\t\t\t\tuint32_t y = t + _blsi_u32(t); // t + (-t & t)\n\t\t\t\t\tt = y | ((y ^ t) >> _tzcnt_u32(t << 2));\n\t\t\t\t}\n\t\t\t}\n\t\t\tauto operator!=(std::nullptr_t) const { return t < n; }\n\t\t};\n\t\tuint32_t s, k;\n\t\tall_subset_k(uint32_t s, uint32_t k) : s(s), k(k) {\n\t\t\tassert(s != std::numeric_limits<uint32_t>::max());\n\t\t}\n\t\tauto begin() { return all_subset_k_iter(s, k); }\n\t\tauto end() { return nullptr; }\n\t};\n\n\tstruct all_subset_k_64 {\n\t\tstruct all_subset_k_iter_64 {\n\t\t\tconst uint64_t n, s;\n\t\t\tconst uint32_t k;\n\t\t\tuint64_t t;\n\t\t\t__attribute__((target(\"avx2\")))\n\t\t\tall_subset_k_iter_64(uint64_t s, uint32_t k) : n(uint64_t(1) << _mm_popcnt_u64(s)), s(s), k(k), t((uint64_t(1) << k) - 1) {}\n\t\t\t__attribute__((target(\"bmi2\")))\n\t\t\tauto operator*() const { return _pdep_u64(t, s); }\n\t\t\t__attribute__((target(\"bmi\")))\n\t\t\tauto operator++() {\n\t\t\t\tif (k == 0) {\n\t\t\t\t\tt = std::numeric_limits<uint64_t>::max();\n\t\t\t\t} else {\n\t\t\t\t\tuint64_t y = t + _blsi_u64(t);\n\t\t\t\t\tt = y | ((y ^ t) >> _tzcnt_u64(t << 2));\n\t\t\t\t}\n\t\t\t}\n\t\t\tauto operator!=(std::nullptr_t) const { return t < n; }\n\t\t};\n\t\tuint64_t s;\n\t\tuint32_t k;\n\t\tall_subset_k_64(uint64_t s, uint32_t k) : s(s), k(k) {\n\t\t\tassert(s != std::numeric_limits<uint64_t>::max());\n\t\t}\n\t\tauto begin() { return all_subset_k_iter_64(s, k); }\n\t\tauto end() { return nullptr; }\n\t};\n\n\tstruct all_setbit {\n\t\tstruct all_setbit_iter {\n\t\t\tuint32_t s;\n\t\t\tall_setbit_iter(uint32_t s) : s(s) {}\n\t\t\t__attribute__((target(\"bmi\")))\n\t\t\tauto operator*() { return _tzcnt_u32(s); }\n\t\t\t__attribute__((target(\"bmi\")))\n\t\t\tauto operator++() { s = __blsr_u32(s); }\n\t\t\tauto operator!=(std::nullptr_t) { return s; }\n\t\t};\n\t\tuint32_t s;\n\t\tall_setbit(uint32_t s) : s(s) {}\n\t\tauto begin() { return all_setbit_iter(s); }\n\t\tauto end() { return nullptr; }\n\t};\n\n\tstruct all_setbit_64 {\n\t\tstruct all_setbit_iter_64 {\n\t\t\tuint64_t s;\n\t\t\tall_setbit_iter_64(uint64_t s) : s(s) {}\n\t\t\t__attribute__((target(\"bmi\")))\n\t\t\tauto operator*() { return _tzcnt_u64(s); }\n\t\t\t__attribute__((target(\"bmi\")))\n\t\t\tauto operator++() { s = __blsr_u64(s); }\n\t\t\tauto operator!=(std::nullptr_t) { return s; }\n\t\t};\n\t\tuint64_t s;\n\t\tall_setbit_64(uint64_t s) : s(s) {}\n\t\tauto begin() { return all_setbit_iter_64(s); }\n\t\tauto end() { return nullptr; }\n\t};\n} // namespace suisen\n\nvoid solve() {\n\tinput(int, n, m);\n\tif (n == 0 and m == 0) exit(0);\n\n\tCoordinateCompressorBuilder<int> builder;\n\n\tvector<int> h(n), c(n);\n\th[0] = 0, c[0] = 0;\n\trep(i, 1, n - 1) read(c[i], h[i]);\n\th[n - 1] = 1000, c[n - 1] = 0;\n\n\trep(i, n) builder.push(h[i]);\n\tconst auto cmp = builder.build();\n\tconst int l = cmp.size();\n\tvector<long long> lev_sets(l);\n\n\trep(i, n) {\n\t\th[i] = cmp[h[i]];\n\t\tlev_sets[h[i]] |= 1LL << i;\n\t}\n\n\tvector g(l, vector<vector<pair<int, int>>>(n));\n\tvector rev_g(l, vector<vector<pair<int, int>>>(n));\n\tvector<vector<pair<int, int>>> asc(n), rev_dsc(n);\n\tloop(m) {\n\t\tinput(int, u, v, w);\n\t\t--u, --v;\n\t\tif (h[u] < h[v]) {\n\t\t\tasc[u].emplace_back(v, w);\n\t\t} else if (h[u] > h[v]) {\n\t\t\trev_dsc[v].emplace_back(u, w);\n\t\t} else {\n\t\t\tg[h[u]][u].emplace_back(v, w);\n\t\t\trev_g[h[u]][v].emplace_back(u, w);\n\t\t}\n\t}\n\n\tstatic constexpr int inf = numeric_limits<int>::max() / 4;\n\tvector dp(l, vector(n, vector<int>(n, inf)));\n\tdp[0][0][0] = 0;\n\trep(i, l - 1) {\n\t\tvector<vector<int>> dists(n);\n\t\trep(j, n) if (kth_bit(lev_sets[i], j)) {\n\t\t\tvector<int> dist(n, inf);\n\t\t\tpq_greater<pair<int, int>> pq;\n\t\t\tpq.emplace(dist[j] = c[j], j);\n\t\t\twhile (pq.size()) {\n\t\t\t\tauto [du, u] = pq.top();\n\t\t\t\tpq.pop();\n\t\t\t\tif (dist[u] != du) continue;\n\t\t\t\tfor (auto [v, w] : g[i][u]) {\n\t\t\t\t\tif (chmin(dist[v], du + w + c[v])) {\n\t\t\t\t\t\tpq.emplace(dist[v], v);\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t\tdists[j].swap(dist);\n\t\t}\n\t\tvector<vector<int>> rev_dists(n);\n\t\trep(k, n) if (kth_bit(lev_sets[i], k)) {\n\t\t\tvector<int> dist(n, inf);\n\t\t\tpq_greater<pair<int, int>> pq;\n\t\t\tpq.emplace(dist[k] = c[k], k);\n\t\t\twhile (pq.size()) {\n\t\t\t\tauto [du, u] = pq.top();\n\t\t\t\tpq.pop();\n\t\t\t\tif (dist[u] != du) continue;\n\t\t\t\tfor (auto [v, w] : rev_g[i][u]) {\n\t\t\t\t\tif (chmin(dist[v], du + w + c[v])) {\n\t\t\t\t\t\tpq.emplace(dist[v], v);\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t\trev_dists[k].swap(dist);\n\t\t}\n\n\t\tCoordinateCompressorBuilder<int> builder_i;\n\t\tfor (int v : all_setbit_64(lev_sets[i])) {\n\t\t\tbuilder_i.push(v);\n\t\t}\n\t\tconst auto cmp_i = builder_i.build();\n\t\tconst int siz = cmp_i.size();\n\n\t\tvector init_wf(siz, vector<int>(siz, inf));\n\t\trep(cx, siz) init_wf[cx][cx] = 0;\n\t\trep(cx, siz) for (auto [y, w] : g[i][cmp_i.decomp(cx)]) {\n\t\t\tinit_wf[cx][cmp_i[y]] = w;\n\t\t}\n\n\t\tmap<long long, vector<vector<int>>> wfs;\n\t\tfor (auto s : all_subset(lev_sets[i])) {\n\t\t\tauto wf = init_wf;\n\t\t\trep(cz, siz) if (kth_bit(s, cmp_i.decomp(cz))) {\n\t\t\t\trep(cx, siz) if (kth_bit(s, cmp_i.decomp(cx))) {\n\t\t\t\t\trep(cy, siz) if (kth_bit(s, cmp_i.decomp(cy))) {\n\t\t\t\t\t\tchmin(wf[cx][cy], wf[cx][cz] + wf[cz][cy]);\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t\twfs[s] = move(wf);\n\t\t}\n\n\t\trep(j, n) rep(k, n) {\n\t\t\tif (dp[i][j][k] == inf) continue;\n\t\t\tassert(h[j] >= i and h[k] >= i);\n\n\t\t\tif (kth_bit(lev_sets[i], j) and kth_bit(lev_sets[i], k)) {\n\t\t\t\tconst int cj = cmp_i[j], ck = cmp_i[k];\n\t\t\t\tfor (auto s : all_subset(lev_sets[i] & ~((1LL << j) | (1LL << k)))) {\n\t\t\t\t\ts |= 1LL << j, s |= 1LL << k;\n\t\t\t\t\t\n\t\t\t\t\tint fc = 0;\n\t\t\t\t\tfor (int v : all_setbit_64(s)) fc += c[v];\n\n\t\t\t\t\tconst auto &wf = wfs.at(s);\n\t\t\t\t\tfor (auto x : all_setbit_64(s)) for (auto y : all_setbit_64(s)) {\n\t\t\t\t\t\tconst int cx = cmp_i[x], cy = cmp_i[y];\n\t\t\t\t\t\tfor (auto [nj, wj] : asc[x]) for (auto [nk, wk] : rev_dsc[y]) {\n\t\t\t\t\t\t\tchmin(dp[i + 1][nj][nk], dp[i][j][k] + wf[cj][cx] + wf[cy][ck] + fc + wj + wk);\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t} else if (kth_bit(lev_sets[i], j)) {\n\t\t\t\trep(x, n) if (dists[j][x] != inf) {\n\t\t\t\t\tfor (auto [nj, w] : asc[x]) {\n\t\t\t\t\t\tchmin(dp[i + 1][nj][k], dp[i][j][k] + dists[j][x] + w);\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t} else if (kth_bit(lev_sets[i], k)) {\n\t\t\t\trep(y, n) if (rev_dists[k][y] != inf) {\n\t\t\t\t\tfor (auto [nk, w] : rev_dsc[y]) {\n\t\t\t\t\t\tchmin(dp[i + 1][j][nk], dp[i][j][k] + rev_dists[k][y] + w);\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t} else {\n\t\t\t\tchmin(dp[i + 1][j][k], dp[i][j][k]);\n\t\t\t}\n\t\t}\n\t}\n\n\tprint(dp[l - 1][n - 1][n - 1] == inf ? -1 : dp[l - 1][n - 1][n - 1]);\n\t// cout.flush();\n}\n\nint main() {\n\twhile (true) solve();\n\treturn 0;\n}", "accuracy": 1, "time_ms": 4460, "memory_kb": 4368, "score_of_the_acc": -0.6818, "final_rank": 15 }, { "submission_id": "aoj_1324_6082052", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\n#define rep(i,a,b) for(int i=(int)(a);i<(int)(b);i++)\n#define ALL(v) (v).begin(),(v).end()\nusing ll=long long int;\nconst int inf = 0x3fffffff; const ll INF = 0x1fffffffffffffff; const double eps=1e-12;\ntemplate<typename T>inline bool chmax(T& a,T b){if(a<b){a=b;return 1;}return 0;}\ntemplate<typename T>inline bool chmin(T& a,T b){if(a>b){a=b;return 1;}return 0;}\n\nvoid solve(){\n int n,m;\n cin>>n>>m;\n if(n==0)exit(0);\n vector<int> d(n),e(n);\n e[n-1]=1000;\n rep(i,1,n-1)cin>>d[i]>>e[i];\n int pos[n],ecnt[1010]={};\n rep(i,0,n)pos[i]=ecnt[e[i]]++;\n using P=array<int,2>;\n vector g(n,vector<P>()),rev(n,vector<P>());\n rep(_,0,m){\n int u,v,w;\n cin>>u>>v>>w;\n u--; v--;\n if(e[u]<=e[v])g[u].push_back({v,w});\n if(e[u]>=e[v])rev[v].push_back({u,w});\n }\n\n vector dp(n,vector(n,vector<ll>(1<<10,INF)));\n dp[0][0][1]=0;\n using T=pair<ll,array<int,3>>;\n priority_queue<T,vector<T>,greater<T>> pq;\n pq.push({0,{0,0,1}});\n\n auto insert=[&](int x,int y,int mask,ll dist)->void{\n if(chmin(dp[x][y][mask],dist)){\n pq.push({dp[x][y][mask],{x,y,mask}});\n }\n };\n\n while(!pq.empty()){\n auto [dist,state]=pq.top();\n pq.pop();\n auto [x,y,mask]=state;\n if(dp[x][y][mask]<dist)continue;\n for(auto& [nxt,add]:g[x]){\n if(e[nxt]<e[y])insert(nxt,y,mask,dist+add+d[nxt]);\n else if(e[nxt]==e[x] or e[nxt]==e[y])insert(nxt,y,mask|(1<<pos[nxt]),dist+add+(mask>>pos[nxt]&1?0:d[nxt]));\n else insert(nxt,y,1<<pos[nxt],dist+add+d[nxt]);\n }\n for(auto& [nxt,add]:rev[y]){\n if(e[nxt]<e[x])insert(x,nxt,mask,dist+add+d[nxt]);\n else if(e[nxt]==e[x] or e[nxt]==e[y])insert(x,nxt,mask|(1<<pos[nxt]),dist+add+(mask>>pos[nxt]&1?0:d[nxt]));\n else insert(x,nxt,1<<pos[nxt],dist+add+d[nxt]);\n }\n }\n ll ret=dp[n-1][n-1][1];\n if(ret==INF)puts(\"-1\");\n else cout<<ret<<'\\n';\n}\n\nint main(){\n for(;;)solve();\n return 0;\n}", "accuracy": 1, "time_ms": 1540, "memory_kb": 63500, "score_of_the_acc": -1.1416, "final_rank": 19 }, { "submission_id": "aoj_1324_6041768", "code_snippet": "#include <bits/stdc++.h>\n\nusing namespace std;\n\n#define eps 1e-9\n#define INF 2000000000 // 2e9\n#define LLINF 2000000000000000000ll // 2e18 (llmax:9e18)\n#define all(x) (x).begin(), (x).end()\n#define sq(x) ((x) * (x))\n\n#define rep(i, x) for (int i = 0; i < (int)(x); i++)\n#define drep(i, x) for (int i = ((int)(x)-1); i >= 0; i--)\n\n#define popcount __builtin_popcount\n#define next __next\n#define prev __prev\n\n#ifndef LOCAL\n#define dmp(...) ;\n#else\n#define dmp(...) \\\n cerr << \"[ \" << #__VA_ARGS__ << \" ] : \" << dump_str(__VA_ARGS__) << endl\n#endif\n\n// ---------------- Utility ------------------\n\ntemplate <class T>\nbool chmin(T &a, const T &b) {\n if (a <= b) return false;\n a = b;\n return true;\n}\n\ntemplate <class T>\nbool chmax(T &a, const T &b) {\n if (a >= b) return false;\n a = b;\n return true;\n}\n\ntemplate <class T>\nusing MaxHeap = priority_queue<T>;\n\ntemplate <class T>\nusing MinHeap = priority_queue<T, vector<T>, greater<T>>;\n\ntemplate <class T>\nvector<T> vect(int len, T elem) {\n return vector<T>(len, elem);\n}\n\n// ----------------- Input -------------------\n\ntemplate <class T, class U>\nistream &operator>>(istream &is, pair<T, U> &p) {\n return is >> p.first >> p.second;\n}\n\ntemplate <class T>\nistream &operator>>(istream &is, vector<T> &vec) {\n for (int i = 0; i < vec.size(); i++) is >> vec[i];\n return is;\n}\n\n// ----------------- Output ------------------\n\ntemplate <class T, class U>\nostream &operator<<(ostream &os, const pair<T, U> &p) {\n return os << p.first << ',' << p.second;\n}\n\ntemplate <class T>\nostream &operator<<(ostream &os, const vector<T> &v) {\n for (const T &e : v) os << e << \" \";\n return os;\n}\n\ntemplate <class T>\nostream &operator<<(ostream &os, const deque<T> &d) {\n for (const T &e : d) os << e << \" \";\n return os;\n}\n\ntemplate <class T>\nostream &operator<<(ostream &os, const set<T> &s) {\n os << \"{ \";\n for (const T &e : s) os << e << \" \";\n return os << \"}\";\n}\n\ntemplate <class T, class U>\nostream &operator<<(ostream &os, const map<T, U> &m) {\n os << \"{ \";\n for (const auto &[key, val] : m) os << \"( \" << key << \" -> \" << val << \" ) \";\n return os << \"}\";\n}\n\ntemplate <class TupleTy, size_t... I>\nvoid dump_tuple(ostream &os, const TupleTy t, std::index_sequence<I...>) {\n (..., (os << (I == 0 ? \"\" : \",\") << std::get<I>(t)));\n}\n\ntemplate <class... Args>\nostream &operator<<(ostream &os, const tuple<Args...> &t) {\n os << \"(\";\n dump_tuple(os, t, std::make_index_sequence<sizeof...(Args)>());\n return os << \")\";\n}\n\nvoid dump_str_rec(ostringstream &) {}\n\ntemplate <class Head, class... Tail>\nvoid dump_str_rec(ostringstream &oss, Head &&head, Tail &&... tail) {\n oss << \", \" << head;\n dump_str_rec(oss, forward<Tail>(tail)...);\n}\n\ntemplate <class T, class... U>\nstring dump_str(T &&arg, U &&... args) {\n ostringstream oss;\n oss << arg;\n dump_str_rec(oss, forward<U>(args)...);\n return oss.str();\n}\n\n// --------------- Fast I/O ------------------\n\nvoid fastio() {\n cin.tie(0);\n ios::sync_with_stdio(0);\n cout << fixed << setprecision(20);\n}\n\n// ------------------ ACL --------------------\n\n// #include <atcoder/modint>\n// constexpr int MOD = 998244353;\n// constexpr int MOD = 1000000007;\n// using mint = atcoder::static_modint<MOD>;\n// ostream &operator<<(ostream &os, const mint &v) {\n// return os << v.val();\n// }\n\n// ------------ End of template --------------\n\ntemplate <class Cost = int>\nstruct edge {\n int from, to;\n Cost cost;\n edge() {}\n edge(int from, int to, Cost cost) : from(from), to(to), cost(cost) {}\n\n // for debug\n friend ostream &operator<<(ostream &os, const edge &e) {\n os << e.from << \" -> \" << e.to << \" : \" << e.cost << \", \";\n return os;\n }\n};\n\ntemplate <class Cost = int>\nstruct Graph {\n int V;\n vector<vector<edge<Cost>>> G;\n Graph() {}\n Graph(int V) : V(V) { G.resize(V); }\n void add_edge(int a, int b, Cost cost = Cost(0)) {\n G[a].push_back(edge<Cost>(a, b, cost));\n }\n void add_edge_undirected(int a, int b, Cost cost = Cost(0)) {\n G[a].push_back(edge<Cost>(a, b, cost));\n G[b].push_back(edge<Cost>(b, a, cost));\n }\n size_t size() const { return G.size(); }\n const vector<edge<Cost>> &operator[](int id) const { return G[id]; }\n\n // for debug\n friend ostream &operator<<(ostream &os, const Graph g) {\n for (int i = 0; i < g.size(); i++) {\n os << g[i];\n if (i + 1 < g.size()) os << endl;\n }\n return os;\n }\n};\n\n// verified\n// https://atcoder.jp/contests/dwacon6th-final-open/submissions/10023433\ntemplate <class Cost>\nvector<Cost> dijkstra(Graph<Cost> &g, int s) {\n assert(0 <= s && s < g.size());\n vector<Cost> dist;\n dist.assign(g.size(), numeric_limits<Cost>::max());\n dist[s] = Cost(0);\n MinHeap<pair<Cost, int>> q;\n q.push(make_pair(Cost(0), s));\n while (!q.empty()) {\n auto a = q.top();\n q.pop();\n int v = a.second;\n if (dist[v] < a.first) continue;\n for (auto e : g[v]) {\n if (dist[e.to] > dist[v] + e.cost) {\n dist[e.to] = dist[v] + e.cost;\n q.push(make_pair(dist[e.to], e.to));\n }\n }\n }\n return dist;\n}\n\n#define endl \"\\n\"\nusing ll = long long;\nusing pii = pair<int, int>;\n\nconst bool multitest = false;\n\nstruct city {\n int h, fee, id, h_id;\n city() {}\n city(int h, int fee, int id, int h_id) : h(h), fee(fee), id(id), h_id(h_id) {}\n bool operator<(const city &c) const { return h < c.h; }\n};\n\nbool solve() {\n int n, m;\n cin >> n >> m;\n if (n == 0 && m == 0) return false;\n\n vector<city> cs(n, {0, 0, -1, -1});\n vector<int> h_cnt(1001, 0);\n for (int i = 1; i < n - 1; i++) {\n int d, e;\n cin >> d >> e;\n cs[i] = city(e, d, i, h_cnt[e]++);\n }\n cs[0] = city(0, 0, 0, 0), cs[n - 1] = city(1000, 0, n - 1, 0);\n h_cnt[0] = 1, h_cnt[1000] = 1;\n sort(all(cs));\n\n vector<int> rev(n, -1);\n for (int i = 0; i < n; i++) { rev[cs[i].id] = i; }\n\n dmp(rev);\n\n Graph<int> G(n), rG(n);\n for (int i = 0; i < m; i++) {\n int a, b, c;\n cin >> a >> b >> c;\n a--;\n b--;\n a = rev[a], b = rev[b];\n if (cs[a].h <= cs[b].h) { G.add_edge(a, b, c); }\n if (cs[a].h >= cs[b].h) { rG.add_edge(b, a, c); }\n }\n\n // Graph<int> g(n * n * (1 << 10));\n\n auto encode = [&](int i, int j, int S) {\n return n * (1 << 10) * i + (1 << 10) * j + S;\n };\n\n auto decode = [&](int v, int &i, int &j, int &S) {\n S = v % (1 << 10), v /= (1 << 10);\n j = v % n, v /= n;\n i = v % n;\n return;\n };\n\n vector<int> dist;\n dist.assign(n * n * (1 << 10), INF);\n int s = encode(0, 0, 1);\n dist[s] = 0;\n\n MinHeap<pair<int, int>> q;\n q.push(make_pair(dist[s], s));\n\n auto update = [&](int u, int v, int cost) {\n if (dist[v] > dist[u] + cost) {\n dist[v] = dist[u] + cost;\n q.push(make_pair(dist[v], v));\n }\n };\n\n while (!q.empty()) {\n auto [d, u] = q.top();\n q.pop();\n if (dist[u] < d) continue;\n\n int i = -1, j = -1, S = -1;\n decode(u, i, j, S);\n\n if (cs[i].h <= cs[j].h) {\n for (auto e : G[i]) {\n int add = e.cost;\n int mask = (1 << cs[e.to].h_id);\n if (cs[e.to].h == cs[j].h) {\n if (!((S >> cs[e.to].h_id) & 1)) add += cs[e.to].fee;\n int v = encode(e.to, j, S | mask);\n update(u, v, add);\n } else if (cs[e.to].h > cs[j].h) {\n add += cs[e.to].fee;\n int v = encode(e.to, j, mask);\n update(u, v, add);\n } else {\n add += cs[e.to].fee;\n int v = encode(e.to, j, S);\n update(u, v, add);\n }\n }\n }\n\n if (cs[i].h >= cs[j].h) {\n for (auto e : rG[j]) {\n int add = e.cost;\n int mask = (1 << cs[e.to].h_id);\n if (cs[i].h == cs[e.to].h) {\n if (!((S >> cs[e.to].h_id) & 1)) add += cs[e.to].fee;\n int v = encode(i, e.to, S | mask);\n update(u, v, add);\n } else if (cs[e.to].h > cs[i].h) {\n add += cs[e.to].fee;\n int v = encode(i, e.to, mask);\n update(u, v, add);\n } else {\n add += cs[e.to].fee;\n int v = encode(i, e.to, S);\n update(u, v, add);\n }\n }\n }\n\n // for (auto e : g[v]) {\n // if (dist[e.to] > dist[v] + e.cost) {\n // dist[e.to] = dist[v] + e.cost;\n // q.push(make_pair(dist[e.to], e.to));\n // }\n // }\n }\n\n int ans = dist[encode(n - 1, n - 1, 1)];\n if (ans == INF)\n cout << -1 << endl;\n else\n cout << ans << endl;\n\n return true;\n}\n\nint main() {\n fastio();\n if (!multitest) {\n while (solve()) {}\n } else {\n cerr << \"[Warning] Multi testcase mode on\" << endl;\n int t;\n cin >> t;\n while (t--) solve();\n }\n return 0;\n}", "accuracy": 1, "time_ms": 110, "memory_kb": 14988, "score_of_the_acc": -0.1959, "final_rank": 8 }, { "submission_id": "aoj_1324_5920294", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\ntypedef long long ll;\ntemplate<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return true; } return false; }\ntemplate<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return true; } return false; }\n#define all(x) (x).begin(),(x).end()\n#define fi first\n#define se second\n#define mp make_pair\n#define si(x) int(x.size())\nconst int mod=1000000007,MAX=55,INF=1<<29;\n\nvector<pair<int,int>> G[MAX];\nvector<pair<int,int>> rG[MAX];\nint h[MAX];\nint cost[MAX];\nvector<int> pos[MAX];\n\nint dp[MAX][MAX][1<<10];\n\nint memo[MAX][MAX];\n\nvoid init(int N){\n for(int i=0;i<N;i++){\n G[i].clear();\n rG[i].clear();\n pos[i].clear();\n h[i]=cost[i]=0;\n for(int j=0;j<N;j++) memo[i][j]=-1;\n }\n for(int i=0;i<=N;i++) for(int j=0;j<=N;j++) for(int k=0;k<(1<<10);k++) dp[i][j][k]=INF;\n}\n\nint main(){\n \n std::ifstream in(\"text.txt\");\n std::cin.rdbuf(in.rdbuf());\n cin.tie(0);\n ios::sync_with_stdio(false);\n \n while(1){\n int N,M;cin>>N>>M;\n if(N==0&&M==0) break;\n init(N);\n \n h[0]=0;\n h[N-1]=1000;\n cost[0]=cost[N-1]=0;\n for(int i=1;i<N-1;i++){\n cin>>cost[i]>>h[i];\n }\n \n vector<int> useh(N);\n for(int i=0;i<N;i++) useh[i]=h[i];\n sort(all(useh));\n useh.erase(unique(all(useh)),useh.end());\n \n for(int i=0;i<N;i++){\n h[i]=lower_bound(all(useh),h[i])-useh.begin();\n pos[h[i]].push_back(i);\n }\n \n for(int i=0;i<M;i++){\n int a,b,c;cin>>a>>b>>c;\n a--;b--;\n G[a].push_back(mp(b,c));\n rG[b].push_back(mp(a,c));\n }\n \n dp[0][0][1]=0;\n \n auto p=[&](int hh,int id){\n if(memo[hh][id]!=-1) return memo[hh][id];\n for(int i=0;i<si(pos[hh]);i++){\n if(pos[hh][i]==id) return memo[hh][id]=i;\n }\n return -1;\n };\n \n for(int t=0;t<N;t++){\n for(int i=0;i<N;i++){\n for(int j=0;j<N;j++){\n int z;\n if(h[i]>=h[j]) z=si(pos[h[i]]);\n else z=si(pos[h[j]]);\n for(int k=0;k<(1<<z);k++){\n if(dp[i][j][k]==INF) continue;\n \n for(auto to:G[i]){\n if(h[i]>h[to.fi]) continue;\n if(h[i]==h[j]){\n if(h[i]==h[to.fi]){\n int x=p(h[i],to.fi);\n if(k&(1<<x)){\n chmin(dp[to.fi][j][k],dp[i][j][k]+to.se);\n }else{\n chmin(dp[to.fi][j][k|(1<<x)],dp[i][j][k]+to.se+cost[to.fi]);\n }\n }else{\n int x=p(h[to.fi],to.fi);\n chmin(dp[to.fi][j][(1<<x)],dp[i][j][k]+to.se+cost[to.fi]);\n }\n }else if(h[i]>h[j]){\n int x=p(h[to.fi],to.fi);\n chmin(dp[to.fi][j][(1<<x)],dp[i][j][k]+to.se+cost[to.fi]);\n }else{\n if(h[j]==h[to.fi]){\n int x=p(h[j],to.fi);\n if(k&(1<<x)){\n chmin(dp[to.fi][j][k],dp[i][j][k]+to.se);\n }else{\n chmin(dp[to.fi][j][k|(1<<x)],dp[i][j][k]+to.se+cost[to.fi]);\n }\n }else{\n int x=p(h[to.fi],to.fi);\n chmin(dp[to.fi][j][(1<<x)],dp[i][j][k]+to.se+cost[to.fi]);\n }\n }\n }\n \n for(auto to:rG[j]){\n if(h[i]>h[to.fi]) continue;\n if(h[i]==h[j]){\n if(h[j]==h[to.fi]){\n int x=p(h[j],to.fi);\n if(k&(1<<x)){\n chmin(dp[i][to.fi][k],dp[i][j][k]+to.se);\n }else{\n chmin(dp[i][to.fi][k|(1<<x)],dp[i][j][k]+to.se+cost[to.fi]);\n }\n }else{\n int x=p(h[to.fi],to.fi);\n chmin(dp[i][to.fi][(1<<x)],dp[i][j][k]+to.se+cost[to.fi]);\n }\n }else if(h[j]>h[i]){\n int x=p(h[to.fi],to.fi);\n chmin(dp[i][to.fi][(1<<x)],dp[i][j][k]+to.se+cost[to.fi]);\n }else{\n if(h[i]==h[to.fi]){\n int x=p(h[i],to.fi);\n if(k&(1<<x)){\n chmin(dp[i][to.fi][k],dp[i][j][k]+to.se);\n }else{\n chmin(dp[i][to.fi][k|(1<<x)],dp[i][j][k]+to.se+cost[to.fi]);\n }\n }else{\n int x=p(h[to.fi],to.fi);\n chmin(dp[i][to.fi][(1<<x)],dp[i][j][k]+to.se+cost[to.fi]);\n }\n }\n }\n }\n }\n }\n }\n \n int ans=INF;\n \n for(int k=0;k<(1<<10);k++) chmin(ans,dp[N-1][N-1][k]);\n \n if(ans==INF) ans=-1;\n \n cout<<ans<<\"\\n\";\n \n }\n}", "accuracy": 1, "time_ms": 6750, "memory_kb": 14624, "score_of_the_acc": -1.1771, "final_rank": 20 }, { "submission_id": "aoj_1324_5852146", "code_snippet": "#include\"bits/stdc++.h\"\nusing namespace std;\n#define ALL(x) begin(x),end(x)\n#define rep(i,n) for(int i=0;i<(n);i++)\n#define debug(v) cout<<#v<<\":\";for(auto x:v){cout<<x<<' ';}cout<<endl;\n#define mod 998244353\nusing ll=long long;\nconst int INF=10000000;\nconst ll LINF=1001002003004005006ll;\nint dx[]={1,0,-1,0},dy[]={0,1,0,-1};\ntemplate<class T>bool chmax(T &a,const T &b){if(a<b){a=b;return true;}return false;}\ntemplate<class T>bool chmin(T &a,const T &b){if(b<a){a=b;return true;}return false;}\n\nstruct IOSetup{\n IOSetup(){\n cin.tie(0);\n ios::sync_with_stdio(0);\n cout<<fixed<<setprecision(12);\n }\n} iosetup;\n \ntemplate<typename T>\nostream &operator<<(ostream &os,const vector<T>&v){\n for(int i=0;i<(int)v.size();i++) os<<v[i]<<(i+1==(int)v.size()?\"\":\" \");\n return os;\n}\ntemplate<typename T>\nistream &operator>>(istream &is,vector<T>&v){\n for(T &x:v)is>>x;\n return is;\n}\n\nint N,M;\n\nusing P=pair<int,int>;\nusing PP=pair<int,P>;\n\n/*\n高い場所に1移動する場合\n逐一,同じ移動先に移動するならコストを1回しか支払わないようにしながらDijkstraをすれば良い\n同じ高さの移動のみ両者の移動がトポロジカルソート順になるとは限らない\nここは位置のスワップコストを求めてやれば良い.\nd[i][j] -> d[j][i]とする場合のコスト\nこの際,i, jのvisa feeは一度だけ支払えば良い\n\n到達時にvisa feeを支払いつつdijkstraをすることを考える.\n同じ高さで(i,j) -> (j,i)に移動するときに,お互い,visa feeを支払わずに移動してよいとする.\nなぜならiがiに到達する時点でiのvisa feeを支払済であり,jに関しても同じことが言えるためである\nまた,このようなswapコストを定義する理由としては,同じ高さでの移動に関しては行きはa-b-cの順序で移動するが,\n帰りと逆順ではc-b-aで移動している可能性がある(両者の移動順序がトポソ不可)ため,\nd[a][c]->d[b][b]->d[c][a]という遷移だと,a,cのvisa feeをそれぞれ二度支払いそうになる.\nよって同じ高さでのswap costを用意してやるとうまい.\n\n天才か?\n*/\n\nvoid solve(){\n vector<int> D(N),E(N);// visa fee, altitude\n D[0]=0, D[N-1]=0;\n E[0]=0, E[N-1]=INF;\n for(int i=1;i<N-1;i++) cin>>D[i]>>E[i];\n\n vector<vector<int>> C(N,vector<int>(N,INF));\n rep(i,N) C[i][i]=0;\n rep(i,M){\n int u,v,c;cin>>u>>v>>c;u--,v--;\n chmin(C[u][v],c);\n }\n\n vector<vector<int>> sw(N,vector<int>(N,INF));// 同じ高さの場合のみDAGではなくなる.行きと帰りで位置をswapするコスト\n rep(i,N){\n priority_queue<P,vector<P>,greater<P>> que;\n\n sw[i][i]=0;\n que.emplace(0, i);\n\n while(!que.empty()){\n auto [d, cur]=que.top();que.pop();\n if(d<sw[i][cur]) continue;\n\n rep(j,N)if(i!=j and E[i]==E[j]){\n if(chmin(sw[i][j], d+C[cur][j]*2+D[j])) que.emplace(sw[i][j], j);// ここもD[j]の支払いは一度で良い\n }\n }\n rep(j,N)if(i!=j) sw[i][j]-=D[j];// i, jもうコスト支払済なのでいい\n }\n\n priority_queue<PP,vector<PP>,greater<PP>> que;\n vector<vector<int>> res(N,vector<int>(N, INF));\n\n res[0][0]=0;\n que.emplace(res[0][0], make_pair(0,0));\n while(!que.empty()){\n auto pp=que.top();que.pop();\n int d=pp.first;\n int i,j;\n tie(i,j)=pp.second;\n\n if(res[i][j]<d) continue;\n \n // step - i\n rep(k,N)if(E[k]>=E[i]){\n if(chmin(res[k][j], d+C[i][k]+D[k])) que.emplace(res[k][j],make_pair(k,j));\n }\n\n // step - j\n rep(k,N)if(E[k]>=E[j]){\n if(chmin(res[i][k], d+C[k][j]+D[k])) que.emplace(res[i][k],make_pair(i,k));\n }\n\n // swap\n if(i!=j and E[i]==E[j]){\n if(chmin(res[j][i], d+sw[i][j])) que.emplace(res[j][i],make_pair(j,i));\n }\n\n // i -> k and j-> k\n rep(k,N)if(E[k]>=E[i] and E[k]>=E[j]){\n if(chmin(res[k][k], d+D[k]+C[i][k]+C[k][j])) que.emplace(res[k][k], make_pair(k,k));\n }\n }\n if(res[N-1][N-1]<INF) cout<<res[N-1][N-1]<<endl;\n else cout<<-1<<endl;\n return ;\n}\n\nsigned main(){\n while(cin>>N>>M, N or M) solve();\n return 0;\n}", "accuracy": 1, "time_ms": 20, "memory_kb": 3672, "score_of_the_acc": -0.0115, "final_rank": 2 }, { "submission_id": "aoj_1324_5526691", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nconst int INF = 10000000;\nint main(){\n while (true){\n int n, m;\n cin >> n >> m;\n if (n == 0 && m == 0){\n break;\n }\n vector<int> d(n), e(n);\n d[0] = 0;\n e[0] = 0;\n for (int i = 1; i < n - 1; i++){\n cin >> d[i] >> e[i];\n }\n d[n - 1] = 0;\n e[n - 1] = 1000;\n vector<vector<int>> E(n, vector<int>(n, INF));\n for (int i = 0; i < n; i++){\n E[i][i] = 0;\n }\n for (int i = 0; i < m; i++){\n int a, b, c;\n cin >> a >> b >> c;\n a--;\n b--;\n E[a][b] = c;\n }\n vector<int> e2 = e;\n sort(e2.begin(), e2.end());\n e2.erase(unique(e2.begin(), e2.end()), e2.end());\n int h = e2.size();\n for (int i = 0; i < n; i++){\n e[i] = lower_bound(e2.begin(), e2.end(), e[i]) - e2.begin();\n }\n vector<vector<int>> id(h);\n for (int i = 0; i < n; i++){\n id[e[i]].push_back(i);\n }\n vector<vector<vector<vector<int>>>> dp(n, vector<vector<vector<int>>>(n, vector<vector<int>>(n, vector<int>(n, INF))));\n for (int i = 0; i < h; i++){\n int cnt = id[i].size();\n for (int j = 0; j < (1 << cnt); j++){\n vector<int> id2;\n int sum = 0;\n for (int k = 0; k < cnt; k++){\n if ((j >> k & 1) == 1){\n id2.push_back(id[i][k]);\n sum += d[id[i][k]];\n }\n }\n int cnt2 = id2.size();\n vector<vector<int>> dp2(cnt2, vector<int>(cnt2, INF));\n for (int k = 0; k < cnt2; k++){\n for (int l = 0; l < cnt2; l++){\n dp2[k][l] = E[id2[k]][id2[l]];\n }\n }\n for (int u = 0; u < cnt2; u++){\n for (int v = 0; v < cnt2; v++){\n for (int w = 0; w < cnt2; w++){\n dp2[v][w] = min(dp2[v][w], dp2[v][u] + dp2[u][w]);\n }\n }\n }\n for (int k1 = 0; k1 < cnt2; k1++){\n for (int l1 = 0; l1 < cnt2; l1++){\n for (int k2 = 0; k2 < cnt2; k2++){\n for (int l2 = 0; l2 < cnt2; l2++){\n dp[id2[k1]][id2[l1]][id2[k2]][id2[l2]] = min(dp[id2[k1]][id2[l1]][id2[k2]][id2[l2]], dp2[k1][l1] + dp2[l2][k2] + sum);\n }\n }\n }\n }\n }\n }\n vector<int> x;\n for (int i = 0; i < h; i++){\n int cnt = id[i].size();\n for (int j = 0; j < cnt; j++){\n x.push_back(id[i][j]);\n }\n }\n vector<vector<int>> dp2(n, vector<int>(n, INF));\n dp2[0][0] = 0;\n for (int i = 0; i < n; i++){\n for (int j = 0; j < n; j++){\n int v = x[i];\n int w = x[j];\n int cnt1 = id[e[v]].size();\n int cnt2 = id[e[w]].size();\n for (int i2 = 0; i2 < cnt1; i2++){\n int v2 = id[e[v]][i2];\n for (int k = 0; k < n; k++){\n if (e[v] < e[k]){\n dp2[k][w] = min(dp2[k][w], dp2[v][w] + dp[v][v2][v][v] + E[v2][k]);\n }\n }\n }\n for (int i2 = 0; i2 < cnt2; i2++){\n int w2 = id[e[w]][i2];\n for (int k = 0; k < n; k++){\n if (e[w] < e[k]){\n dp2[v][k] = min(dp2[v][k], dp2[v][w] + dp[w][w][w][w2] + E[k][w2]);\n }\n }\n }\n if (e[v] == e[w]){\n for (int i2 = 0; i2 < cnt1; i2++){\n for (int j2 = 0; j2 < cnt2; j2++){\n int v2 = id[e[v]][i2];\n int w2 = id[e[w]][j2];\n for (int k = 0; k < n; k++){\n for (int l = 0; l < n; l++){\n if (e[v] < e[k] && e[w] < e[l]){\n dp2[k][l] = min(dp2[k][l], dp2[v][w] + dp[v][v2][w][w2] + E[v2][k] + E[l][w2]);\n }\n }\n }\n }\n }\n }\n }\n }\n if (dp2[n - 1][n - 1] == INF){\n cout << -1 << endl;\n } else {\n cout << dp2[n - 1][n - 1] << endl;\n }\n }\n}", "accuracy": 1, "time_ms": 450, "memory_kb": 32032, "score_of_the_acc": -0.504, "final_rank": 11 }, { "submission_id": "aoj_1324_5048249", "code_snippet": "#include<cstdio>\n#include<vector>\n#include<map>\n#include<set>\n#include<queue>\n#include<algorithm>\nusing namespace std;\nusing P = pair<int, int>;\nusing Q = pair<int, P>;\nconst int INF = 1000000000;\n\nint n, m, d, f, a, b, c, towncost[50], dp[50][50], tmp2[50][50], tmp[50][50];\nvector<P> e[50], re[50];\nmap<int, vector<int>> mp;\nvector<int> used;\nset<int> can_use;\n\nint get(int a, int b, bool is_a_top, bool is_b_top) {\n vector<P> A, B;\n A.push_back(P(a, 0)); B.push_back(P(b, 0));\n int ans = INF;\n for (P i : (is_a_top ? re[a] : A)) if (can_use.count(i.first)) {\n for (P j : (is_b_top ? e[b] : B)) if (can_use.count(j.first)) {\n ans = min(ans, dp[i.first][j.first] + i.second + j.second);\n }\n }\n return ans;\n}\n\nint main() {\n while (scanf(\"%d%d\", &n, &m), n) {\n mp.clear();\n used.clear();\n can_use.clear();\n for (int i = 0; i < 50; i++) {\n e[i].clear();\n re[i].clear();\n for (int j = 0; j < 50; j++) {\n dp[i][j] = INF;\n }\n }\n dp[0][0] = 0;\n for (int i = 1; i < n-1; i++) {\n scanf(\"%d%d\", &d, &f);\n towncost[i] = d;\n mp[f].push_back(i);\n }\n towncost[0] = towncost[n-1] = 0;\n for (int i = 0; i < m; i++) {\n scanf(\"%d%d%d\", &a, &b, &c);\n a--; b--;\n e[a].push_back(P(b, c));\n re[b].push_back(P(a, c));\n }\n mp[1000].push_back(n-1);\n used.push_back(0);\n can_use.insert(0);\n for (auto it = mp.begin(); it != mp.end(); it++) {\n vector<int> v = it->second;\n map<int, int> r;\n for (int i = 0; i < v.size(); i++) r[v[i]] = i;\n for (int i : used) for (int j : used) tmp[i][j] = dp[i][j];\n for (int i : used) for (int j : v) {\n tmp[i][j] = get(i, j, false, true);\n }\n for (int i : v) for (int j : used) {\n tmp[i][j] = get(i, j, true, false);\n }\n for (int i : v) for (int j : v) {\n tmp[i][j] = get(i, j, true, true);\n }\n for (int i : v) used.push_back(i), can_use.insert(i);\n for (int i = 1; i < (1 << v.size()); i++) {\n int sum = 0;\n for (int j = 0; j < v.size(); j++) if (i & (1 << j)) {\n sum += towncost[v[j]];\n }\n priority_queue<Q> que;\n for (int j : used) {\n for (int k : used) {\n if ((r.count(j) == 0 || (i & (1 << r[j]))) && (r.count(k) == 0 || (i & (1 << r[k])))) {\n tmp2[j][k] = tmp[j][k];\n que.push(Q(tmp2[j][k], P(j, k)));\n } else {\n tmp2[j][k] = INF;\n }\n }\n }\n while (!que.empty()) {\n Q q = que.top(); que.pop();\n int cost = q.first, j = q.second.first, k = q.second.second;\n if (tmp2[j][k] < cost) continue;\n dp[j][k] = min(dp[j][k], cost + sum);\n for (P l : e[j]) if (r.count(l.first) && (i & (1 << r[l.first]))) {\n int newcost = cost + l.second;\n if (tmp2[l.first][k] > newcost) {\n tmp2[l.first][k] = newcost;\n que.push(Q(newcost, P(l.first, k)));\n }\n }\n for (P l : re[k]) if (r.count(l.first) && (i & (1 << r[l.first]))) {\n int newcost = cost + l.second;\n if (tmp2[j][l.first] > newcost) {\n tmp2[j][l.first] = newcost;\n que.push(Q(newcost, P(j, l.first)));\n }\n }\n }\n }\n }\n if (dp[n-1][n-1] == INF) dp[n-1][n-1] = -1;\n printf(\"%d\\n\", dp[n-1][n-1]);\n }\n}", "accuracy": 1, "time_ms": 5350, "memory_kb": 2908, "score_of_the_acc": -0.792, "final_rank": 16 }, { "submission_id": "aoj_1324_4943293", "code_snippet": "#include <iostream>\n#include <vector>\n#include <cmath>\n#include <numeric>\n#include <cfloat>\n#include <climits>\n#include <algorithm>\n#include <iomanip>\n#include <queue>\n#include <unordered_map>\n#include <string>\n#include <tuple>\nstruct Road {\n\tint to, cost;\n};\nstruct Town {\n\tint visit_cost, altitude;\n};\nstruct State {\n\tint go_pos, return_pos, cost;\n\tunsigned long long int visited;\n};\nint main() {\n\twhile (true) {\n\t\tint n, m; std::cin >> n >> m;\n\t\tif (n == 0 && m == 0) break;\n\t\tstd::vector<Town> towns(n);\n\t\ttowns[0].altitude = 0; towns.back().altitude = 1000;\n\t\tfor (auto i = 1; i < n - 1; ++i) {\n\t\t\tstd::cin >> towns[i].visit_cost >> towns[i].altitude;\n\t\t}\n\t\tstd::vector<std::vector<Road>> road(n), rev_road(n);\n\t\tfor (auto i = 0; i < m; ++i) {\n\t\t\tint a, b, c; std::cin >> a >> b >> c; --a; --b;\n\t\t\tif (towns[a].altitude <= towns[b].altitude) road[a].push_back(Road{ b, c });\n\t\t\tif (towns[b].altitude <= towns[a].altitude) rev_road[b].push_back(Road{ a, c });\n\t\t}\n\t\tstd::vector<std::vector<std::unordered_map<unsigned long long int, int>>> min_cost(n, std::vector<std::unordered_map<unsigned long long int, int>>(n));\n\t\tconst auto comparator = [](const State& a, const State& b) {return a.cost > b.cost; };\n\t\tstd::priority_queue<State, std::vector<State>, decltype(comparator)> queue(comparator);\n\t\tmin_cost[0][0][0] = 0;\n\t\tqueue.push(State{ 0, 0, 0, 0 });\n\t\twhile (!queue.empty()) {\n\t\t\tconst auto top = queue.top(); queue.pop();\n\t\t\tif (min_cost[top.go_pos][top.return_pos][top.visited] < top.cost) continue;\n\t\t\tconst auto max_height = std::max(towns[top.go_pos].altitude, towns[top.return_pos].altitude);\n\t\t\tif (towns[top.return_pos].altitude == max_height) for (const auto r : road[top.go_pos]) {\n\t\t\t\tconst auto visited = towns[r.to].altitude < max_height ? top.visited : (towns[r.to].altitude == max_height ? top.visited | (1ULL << r.to) : (1ULL << r.to));\n\t\t\t\tconst auto cost = r.cost + top.cost + (top.visited & (1ULL << r.to) ? 0 : towns[r.to].visit_cost);\n\t\t\t\tconst auto find = min_cost[r.to][top.return_pos].find(visited);\n\t\t\t\tif (find == min_cost[r.to][top.return_pos].end() || find->second > cost) {\n\t\t\t\t\tmin_cost[r.to][top.return_pos][visited] = cost;\n\t\t\t\t\tqueue.push(State{ r.to, top.return_pos, cost, visited });\n\t\t\t\t}\n\t\t\t}\n\t\t\tif (towns[top.go_pos].altitude == max_height) for (const auto r : rev_road[top.return_pos]) {\n\t\t\t\tconst auto visited = towns[r.to].altitude < max_height ? top.visited : (towns[r.to].altitude == max_height ? top.visited | (1ULL << r.to) : (1ULL << r.to));\n\t\t\t\tconst auto cost = r.cost + top.cost + (top.visited & (1ULL << r.to) ? 0 : towns[r.to].visit_cost);\n\t\t\t\tconst auto find = min_cost[top.go_pos][r.to].find(visited);\n\t\t\t\tif (find == min_cost[top.go_pos][r.to].end() || find->second > cost) {\n\t\t\t\t\tmin_cost[top.go_pos][r.to][visited] = cost;\n\t\t\t\t\tqueue.push(State{ top.go_pos, r.to, cost, visited });\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\tconst auto min = std::min_element(min_cost.back().back().begin(), min_cost.back().back().end(), [](const std::pair<unsigned long long int, int>& a, const std::pair<unsigned long long int, int>& b) {return a.second < b.second; });\n\t\tif (min == min_cost.back().back().end()) {\n\t\t\tstd::cout << \"-1\\n\";\n\t\t}\n\t\telse {\n\t\t\tstd::cout << min->second << '\\n';\n\t\t}\n\t}\n}", "accuracy": 1, "time_ms": 260, "memory_kb": 10540, "score_of_the_acc": -0.151, "final_rank": 7 }, { "submission_id": "aoj_1324_4924144", "code_snippet": "#include <iostream>\n#include <vector>\n#include <algorithm>\n#include <queue>\nusing namespace std;\nconst int inf = 1e9;\n\nstruct state{\n int s,t;\n int mask;\n int d;\n state(int s, int t, int mask, int d):s(s),t(t),mask(mask),d(d){}\n state(){}\n bool operator <(const state &a) const{\n return d > a.d;\n }\n};\nint solve(vector<int> &v, vector<int> &h, vector<vector<pair<int,int>>> &gs, vector<vector<pair<int,int>>> &gt){\n int n = gs.size();\n vector<int> mapping(n);\n vector<int> idx(1001, 0);\n for(int i=0; i<n; i++){\n mapping[i] = idx[h[i]]++;\n }\n vector<vector<vector<int>>> dp(n, vector<vector<int>>(n, vector<int>(2<<10, inf)));\n dp[0][0][1<<mapping[0]] = 0;\n priority_queue<state> wait;\n wait.push(state(0, 0, 1<<mapping[0], 0));\n while(!wait.empty()){\n int s = wait.top().s;\n int t = wait.top().t;\n int mask = wait.top().mask;\n int d = wait.top().d;\n wait.pop();\n if(d > dp[s][t][mask]) continue;\n if(h[s] <= h[t]){\n for(auto next: gs[s]){\n int ns = next.first;\n int nd = d +next.second +v[ns];\n int nmask = mask;\n if(h[ns] == h[t]){\n if(mask>>mapping[ns]&1){\n nd -= v[ns];\n }\n nmask |= 1<<mapping[ns];\n }else if(h[ns] > h[t]){\n nmask = 1<<mapping[ns];\n }\n if(nd < dp[ns][t][nmask]){\n dp[ns][t][nmask] = nd;\n wait.push(state(ns, t, nmask, nd));\n }\n }\n }\n if(h[t] <= h[s]){\n for(auto next: gt[t]){\n int nt = next.first;\n int nd = d +next.second +v[nt];\n int nmask = mask;\n if(h[nt] == h[s]){\n if(mask>>mapping[nt]&1){\n nd -= v[nt];\n }\n nmask |= 1<<mapping[nt];\n }else if(h[nt] > h[s]){\n nmask = 1<<mapping[nt];\n }\n if(nd < dp[s][nt][nmask]){\n dp[s][nt][nmask] = nd;\n wait.push(state(s, nt, nmask, nd));\n }\n }\n }\n }\n int res = *min_element(dp[n-1][n-1].begin(), dp[n-1][n-1].end());\n if(res == inf) return -1;\n return res;\n}\n\nint main(){\n while(1){\n int n,m;\n cin >> n >> m;\n if(n == 0) break;\n\n vector<int> d(n), e(n);\n d[0] = d[n-1] = 0;\n e[0] = 0;\n e[n-1] = 1000;\n for(int i=1; i<n-1; i++){\n cin >> d[i] >> e[i];\n }\n vector<vector<pair<int,int>>> adj(n), rev(n);\n for(int i=0; i<m; i++){\n int a,b,c;\n cin >> a >> b >> c;\n a--; b--;\n if(e[a] <= e[b]) adj[a].emplace_back(b, c);\n if(e[a] >= e[b]) rev[b].emplace_back(a, c);\n }\n cout << solve(d, e, adj, rev) << endl;\n }\n return 0;\n}", "accuracy": 1, "time_ms": 200, "memory_kb": 26352, "score_of_the_acc": -0.3811, "final_rank": 10 }, { "submission_id": "aoj_1324_4837149", "code_snippet": "#include<iostream>\n#include<cstdio>\n#include<cstring>\n#include<string>\n#include<vector>\n#include<cmath>\n#include<algorithm>\n#include<map>\n#include<queue>\n#include<deque>\n#include<iomanip>\n#include<tuple>\n#include<cassert>\n#include<set>\nusing namespace std;\ntypedef long long int LL;\ntypedef pair<int,int> P;\ntypedef pair<LL,int> LP;\nconst int INF=1e8+7;\nconst LL MAX=1e9+7;\n\nvoid array_show(int *array,int array_n,char middle=' '){\n for(int i=0;i<array_n;i++)printf(\"%d%c\",array[i],(i!=array_n-1?middle:'\\n'));\n}\nvoid array_show(LL *array,int array_n,char middle=' '){\n for(int i=0;i<array_n;i++)printf(\"%lld%c\",array[i],(i!=array_n-1?middle:'\\n'));\n}\nvoid array_show(vector<int> &vec_s,int vec_n=-1,char middle=' '){\n if(vec_n==-1)vec_n=vec_s.size();\n for(int i=0;i<vec_n;i++)printf(\"%d%c\",vec_s[i],(i!=vec_n-1?middle:'\\n'));\n}\nvoid array_show(vector<LL> &vec_s,int vec_n=-1,char middle=' '){\n if(vec_n==-1)vec_n=vec_s.size();\n for(int i=0;i<vec_n;i++)printf(\"%lld%c\",vec_s[i],(i!=vec_n-1?middle:'\\n'));\n}\n\nvector<vector<int>> path;\nvector<P> va;\nint t[100][2];\ntypedef vector<int> V1;\ntypedef vector<vector<int>> V2;\ntypedef vector<vector<vector<int>>> V3;\nconst int dis_n=20;\nint dis[dis_n][dis_n][dis_n][dis_n];\n\nvoid memset_dis(){\n int a,b,c,d;\n for(a=0;a<dis_n;a++){\n for(b=0;b<dis_n;b++){\n for(c=0;c<dis_n;c++){\n for(d=0;d<dis_n;d++){\n dis[a][b][c][d]=INF;\n }\n }\n }\n }\n}\n\nvoid check(int ta,int tb,vector<int>& v1){\n if(ta<=tb){\n v1.push_back(tb-ta);\n check(ta,tb-1,v1);\n v1.pop_back();\n check(ta,tb-1,v1);\n return;\n }\n if(v1.empty())return;\n int n=v1.size();\n int i,j,k,l;\n int a,b,c,d;\n int s;\n vector<vector<int>> dis_temp(n,vector<int>(n,INF));\n for(i=0;i<n;i++){\n for(j=0;j<n;j++){\n a=ta+v1[i],b=ta+v1[j];\n dis_temp[i][j]=path[a][b];\n }\n }\n for(k=0;k<n;k++){\n for(i=0;i<n;i++){\n for(j=0;j<n;j++){\n dis_temp[i][j]=min(dis_temp[i][j],dis_temp[i][k]+dis_temp[k][j]);\n }\n }\n }\n s=0;\n for(i=0;i<n;i++){\n s+=t[ta+v1[i]][0];\n }\n for(i=0;i<n;i++){\n for(j=0;j<n;j++){\n for(k=0;k<n;k++){\n for(l=0;l<n;l++){\n a=v1[i],b=v1[j],c=v1[k],d=v1[l];\n dis[a][b][c][d]=min(dis[a][b][c][d],s+dis_temp[i][k]+dis_temp[j][l]);\n }\n }\n }\n }\n}\n\nint main(){\n int n,m;\n int i,j,k;\n int a,b,c,d;\n int x,y;\n while(cin>>n>>m){\n if(!n)break;\n path.assign(n,vector<int>(n,INF));\n va.assign(1e3+7,P(n+10,-1));\n vector<tuple<int,int,int>> vt1;\n vector<int> v1(n);\n V2 dp(n+10,V1(n+10,INF));\n vt1.push_back(make_tuple(0,0,0));\n vt1.push_back(make_tuple(1e3,0,n-1));\n for(i=1;i<n-1;i++){\n cin>>a>>b;\n vt1.push_back(make_tuple(b,a,i));\n }\n sort(vt1.begin(),vt1.end());\n for(i=0;i<n;i++){\n tie(a,b,c)=vt1[i];\n v1[c]=i;\n va[a].first=min(va[a].first,i);\n va[a].second=max(va[a].second,i);\n t[i][0]=b,t[i][1]=a;\n }\n t[n-1][1]=1e3;\n for(i=0;i<m;i++){\n cin>>a>>b>>c;\n a--,b--;\n a=v1[a],b=v1[b];\n path[a][b]=min(path[a][b],c);\n }\n for(i=0;i<n;i++){\n path[i][i]=0;\n }\n dp[0][0]=0;\n for(i=0;i<=1e3;i++){\n if(va[i].first>n)continue;\n memset_dis();\n v1.clear();\n x=va[i].first,y=va[i].second;\n check(x,y,v1);\n m=y-x+1;\n vector<vector<int>> dp_temp(m+10,vector<int>(m+10,INF));\n for(j=0;j<m;j++){\n for(k=0;k<m;k++){\n for(a=0;a<x;a++){\n if(path[a][x+j]==INF)continue;\n for(b=0;b<x;b++){\n if(path[x+k][b]==INF)continue;\n dp_temp[j][k]=min(dp_temp[j][k],dp[a][b]+path[a][x+j]+path[x+k][b]);\n }\n }\n }\n }\n for(j=0;j<m;j++){\n for(k=0;k<m;k++){\n for(a=0;a<m;a++){\n for(b=0;b<m;b++){\n dp[x+a][x+b]=min(dp[x+a][x+b],dp_temp[j][k]+dis[j][b][a][k]);\n }\n }\n }\n }\n for(j=0;j<=y;j++){\n for(a=0;a<x;a++){\n for(b=0;b<m;b++){\n for(c=0;c<m;c++){\n dp[j][x+c]=min(dp[j][x+c],dp[j][a]+path[x+b][a]+dis[b][c][b][b]);\n dp[x+c][j]=min(dp[x+c][j],dp[a][j]+path[a][x+b]+dis[b][b][c][b]);\n }\n }\n }\n }\n }\n int s=dp[n-1][n-1];\n if(s>=INF)s=-1;\n cout<<s<<endl;\n }\n}", "accuracy": 1, "time_ms": 70, "memory_kb": 3724, "score_of_the_acc": -0.0198, "final_rank": 4 }, { "submission_id": "aoj_1324_4767320", "code_snippet": "#include<iostream>\n#include<string>\n#include<cstdio>\n#include<vector>\n#include<cmath>\n#include<algorithm>\n#include<functional>\n#include<iomanip>\n#include<queue>\n#include<ciso646>\n#include<random>\n#include<map>\n#include<set>\n#include<bitset>\n#include<stack>\n#include<unordered_map>\n#include<utility>\n#include<cassert>\n#include<complex>\n#include<numeric>\nusing namespace std;\n\n//#define int long long\ntypedef long long ll;\n\ntypedef unsigned long long ul;\ntypedef unsigned int ui;\nconstexpr ll mod = 998244353/100;\nconst ll INF = mod * mod;\ntypedef pair<int, int>P;\n#define stop char nyaa;cin>>nyaa;\n#define rep(i,n) for(int i=0;i<n;i++)\n#define per(i,n) for(int i=n-1;i>=0;i--)\n#define Rep(i,sta,n) for(int i=sta;i<n;i++)\n#define rep1(i,n) for(int i=1;i<=n;i++)\n#define per1(i,n) for(int i=n;i>=1;i--)\n#define Rep1(i,sta,n) for(int i=sta;i<=n;i++)\n#define all(v) (v).begin(),(v).end()\ntypedef pair<ll, ll> LP;\ntypedef double ld;\ntypedef pair<ld, ld> LDP;\nconst ld eps = 1e-12;\nconst ld pi = acos(-1.0);\n\nll mod_pow(ll x, ll n, ll m) {\n\tll res = 1;\n\twhile (n) {\n\t\tif (n & 1)res = res * x % m;\n\t\tx = x * x % m; n >>= 1;\n\t}\n\treturn res;\n}\nstruct modint {\n\tll n;\n\tmodint() :n(0) { ; }\n\tmodint(ll m) :n(m) {\n\t\tif (n >= mod)n %= mod;\n\t\telse if (n < 0)n = (n % mod + mod) % mod;\n\t}\n\toperator int() { return n; }\n};\nbool operator==(modint a, modint b) { return a.n == b.n; }\nmodint operator+=(modint& a, modint b) { a.n += b.n; if (a.n >= mod)a.n -= mod; return a; }\nmodint operator-=(modint& a, modint b) { a.n -= b.n; if (a.n < 0)a.n += mod; return a; }\nmodint operator*=(modint& a, modint b) { a.n = ((ll)a.n * b.n) % mod; return a; }\nmodint operator+(modint a, modint b) { return a += b; }\nmodint operator-(modint a, modint b) { return a -= b; }\nmodint operator*(modint a, modint b) { return a *= b; }\nmodint operator^(modint a, ll n) {\n\tif (n == 0)return modint(1);\n\tmodint res = (a * a) ^ (n / 2);\n\tif (n % 2)res = res * a;\n\treturn res;\n}\n\nll inv(ll a, ll p) {\n\treturn (a == 1 ? 1 : (1 - p * inv(p % a, a)) / a + p);\n}\nmodint operator/(modint a, modint b) { return a * modint(inv(b, mod)); }\n\nconst int max_n = 1 << 22;\nmodint fact[max_n], factinv[max_n];\nvoid init_f() {\n\tfact[0] = modint(1);\n\tfor (int i = 0; i < max_n - 1; i++) {\n\t\tfact[i + 1] = fact[i] * modint(i + 1);\n\t}\n\tfactinv[max_n - 1] = modint(1) / fact[max_n - 1];\n\tfor (int i = max_n - 2; i >= 0; i--) {\n\t\tfactinv[i] = factinv[i + 1] * modint(i + 1);\n\t}\n}\nmodint comb(int a, int b) {\n\tif (a < 0 || b < 0 || a < b)return 0;\n\treturn fact[a] * factinv[b] * factinv[a - b];\n}\n\nll gcd(ll a, ll b) {\n\tif (a < b)swap(a, b);\n\twhile (b) {\n\t\tll r = a % b; a = b; b = r;\n\t}\n\treturn a;\n}\n\nusing speP = pair<int, P>;\nint cost[50][50];\nint dist[50][50];\n\nint swcost[50][50];\nint n, m;\nvoid solve() {\n\tvector<int> d(n), h(n);\n\trep(i, n) {\n\t\tif (i == 0) {\n\t\t\td[i] = 0, h[i] = 0;\n\t\t}\n\t\telse if (i == n - 1) {\n\t\t\td[i] = 0, h[i] = 1000;\n\t\t}\n\t\telse {\n\t\t\tcin >> d[i] >> h[i];\n\t\t}\n\t}\n\trep(i, n)rep(j, n) {\n\t\tif (i == j)cost[i][j] = 0;\n\t\telse cost[i][j] = mod;\n\t}\n\trep(i, m) {\n\t\tint a, b, c; cin >> a >> b >> c; a--; b--;\n\t\tcost[a][b] = c;\n\t}\n\trep(i, n) {\n\t\trep(j, n) {\n\t\t\tswcost[i][j] = mod;\n\t\t}\n\t\tswcost[i][i] = d[i];\n\t\tpriority_queue<P, vector<P>, greater<P>> q;\n\t\tq.push({ d[i],i });\n\t\twhile (!q.empty()) {\n\t\t\tP p = q.top(); q.pop();\n\t\t\tint id = p.second;\n\t\t\tif (swcost[i][id] < p.first)continue;\n\t\t\trep(to, n) {\n\t\t\t\tif (h[id] != h[to])continue;\n\t\t\t\tint nd = p.first + d[to] + 2 * cost[id][to];\n\t\t\t\tif (nd < swcost[i][to]) {\n\t\t\t\t\tswcost[i][to] = nd;\n\t\t\t\t\tq.push({ nd,to });\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\trep(j, n) {\n\t\t\tswcost[i][j] -= d[i] + d[j];\n\t\t}\n\t}\n\n\trep(i, n)rep(j, n)dist[i][j] = mod;\n\tdist[0][0] = 0;\n\tpriority_queue<speP, vector<speP>, greater<speP>> q;\n\tq.push({ 0,{0,0} });\n\twhile (!q.empty()) {\n\t\tspeP p = q.top(); q.pop();\n\t\tint x = p.second.first, y = p.second.second;\n\t\tif (dist[x][y] < p.first)continue;\n\t\trep(nx, n) {\n\t\t\tif (h[x] > h[nx])continue;\n\t\t\tint s = p.first + cost[x][nx] + d[nx];\n\t\t\tif (s < dist[nx][y]) {\n\t\t\t\tdist[nx][y] = s;\n\t\t\t\tq.push({ s,{nx,y} });\n\t\t\t}\n\t\t}\n\t\trep(ny, n) {\n\t\t\tif (h[y] > h[ny])continue;\n\t\t\tint s = p.first + cost[ny][y] + d[ny];\n\t\t\tif (s < dist[x][ny]) {\n\t\t\t\tdist[x][ny] = s;\n\t\t\t\tq.push({ s,{x,ny} });\n\t\t\t}\n\t\t}\n\t\tif (h[x] == h[y]&&x!=y) {\n\t\t\tint s = p.first + swcost[x][y];\n\t\t\tif (s < dist[y][x]) {\n\t\t\t\tdist[y][x] = s;\n\t\t\t\tq.push({ s,{y,x} });\n\t\t\t}\n\t\t}\n\t\trep(nx, n)rep(ny, n) {\n\t\t\tif (h[x] > h[nx] || h[y] > h[ny])continue;\n\t\t\tint s = p.first + cost[x][nx] + cost[ny][y] + d[nx] + d[ny];\n\t\t\tif (nx == ny)s -= d[nx];\n\t\t\tif (s < dist[nx][ny]) {\n\t\t\t\t//cout << \"? \" << x << \" \" << y << \" \" << nx << \" \" << ny << \" \" << s << \"\\n\";\n\t\t\t\tdist[nx][ny] = s;\n\t\t\t\tq.push({ s,{nx,ny} });\n\t\t\t}\n\t\t}\n\t}\n\t/*rep(i, n) {\n\t\trep(j, n)cout << dist[i][j] << \" \";\n\t\tcout << \"\\n\";\n\t}*/\n\t//cout << dist[1][2]<<\" \"<<swcost[1][2] << \"\\n\";\n\tint ans = dist[n - 1][n - 1];\n\tif (ans == mod)cout << -1 << \"\\n\";\n\telse cout << ans << \"\\n\";\n}\n\n\n\n\nsigned main() {\n\tios::sync_with_stdio(false);\n\tcin.tie(0);\n\t//cout << fixed << setprecision(10);\n\t//init_f();\n\t//expr();\n\t//int t; cin >> t; rep(i, t)\n\twhile (cin >> n >> m, n)\n\t\tsolve();\n\treturn 0;\n}", "accuracy": 1, "time_ms": 110, "memory_kb": 69076, "score_of_the_acc": -1.0134, "final_rank": 17 }, { "submission_id": "aoj_1324_4248759", "code_snippet": "#include <algorithm>\n#include <bitset>\n#include <cassert>\n#include <chrono>\n#include <climits>\n#include <cmath>\n#include <complex>\n#include <cstring>\n#include <deque>\n#include <functional>\n#include <iostream>\n#include <iomanip>\n#include <list>\n#include <map>\n#include <numeric>\n#include <queue>\n#include <random>\n#include <set>\n#include <stack>\n#include <unordered_map>\n#include <unordered_set>\n#include <vector>\n#include <cstdint>\nusing namespace std;\ntypedef long long ll;\ntypedef vector<int> vi;\ntypedef pair<int,int> pii;\n#define MP make_pair\n#define PB push_back\n#define inf 1000000007\n#define rep(i,n) for(int i = 0; i < (int)(n); ++i)\n#define all(x) (x).begin(),(x).end()\n\ntemplate<typename A, size_t N, typename T>\nvoid Fill(A (&array)[N], const T &val){\n std::fill( (T*)array, (T*)(array+N), val );\n}\n \ntemplate<class T> inline bool chmax(T &a, T b){\n if(a<b){\n a = b;\n return true;\n }\n return false;\n}\n\ntemplate<class T> inline bool chmin(T &a, T b){\n if(a>b){\n a = b;\n return true;\n }\n return false;\n}\n\nint dp[51][51][1<<10];\n\nint main(){\n int n,m;\n while(1){\n cin >> n >> m;\n if(n==0)break;\n vector<int>h(n);\n map<int,int> ss;\n vector<int> num(n);\n vector<int> c(n);\n rep(i,n-2){\n cin >> c[i+1] >> h[i+1];\n num[i+1] = ss[h[i+1]];\n ss[h[i+1]]++;\n }\n rep(i,n)rep(j,n)rep(k,1<<10)dp[i][j][k] = inf;\n h[0] = 0;\n h[n-1] = 1000;\n vector<vector<pii> > gu(n);\n vector<vector<pii> > gd(n);\n vector<vector<pii> > u(n);\n vector<vector<pii> > d(n);\n rep(i,m){\n int a,b,e;\n cin >> a >> b >> e;\n a--;b--;\n if(h[a]==h[b]){\n gu[a].push_back(MP(b,e));\n gd[b].push_back(MP(a,e));\n u[a].push_back(MP(b,e));\n d[b].push_back(MP(a,e));\n }else if(h[a]<h[b]){\n u[a].push_back(MP(b,e));\n }else{\n d[b].push_back(MP(a,e));\n }\n }\n priority_queue<pair<pii,pair<int,int>>,vector<pair<pair<int,int>,pii>>,greater<pair<pair<int,int>,pii>> >pq;\n dp[0][0][1] = 0;\n pq.push(MP(MP(0,1),MP(0,0)));\n while(!pq.empty()){\n auto x = pq.top();\n pq.pop();\n int a = x.second.first;\n int b = x.second.second;\n int cost = x.first.first;\n int reach = x.first.second;\n if(dp[a][b][reach]!=cost)continue;\n // cerr << a << \" \" << b << \" \" << cost << \" \" << reach << endl;\n if(h[a]<=h[b]){\n for(auto x:u[a]){\n int nxt = x.first;\n int dd = cost + x.second;\n int nxtreach = reach;\n if(h[nxt]<h[b]){\n dd += c[nxt];\n if(chmin(dp[nxt][b][nxtreach],dd)){\n pq.push(MP(MP(dp[nxt][b][nxtreach],nxtreach),MP(nxt,b)));\n }\n }else if(h[nxt]>h[b]){\n dd += c[nxt];\n nxtreach = (1<<num[nxt]);\n if(chmin(dp[nxt][b][nxtreach],dd)){\n pq.push(MP(MP(dp[nxt][b][nxtreach],nxtreach),MP(nxt,b)));\n }\n }else{\n if(nxt!=b){\n dd += c[nxt];\n nxtreach = reach|(1<<num[nxt]);\n }\n if(chmin(dp[nxt][b][nxtreach],dd)){\n pq.push(MP(MP(dp[nxt][b][nxtreach],nxtreach),MP(nxt,b)));\n }\n }\n }\n }\n if(h[b]<=h[a]){\n for(auto x:d[b]){\n int nxt = x.first;\n int dd = cost + x.second;\n int nxtreach = reach;\n if(h[nxt]<h[a]){\n dd += c[nxt];\n if(chmin(dp[a][nxt][nxtreach],dd)){\n pq.push(MP(MP(dp[a][nxt][nxtreach],nxtreach),MP(a,nxt)));\n }\n }else if(h[nxt]>h[a]){\n dd += c[nxt];\n nxtreach = (1<<num[nxt]);\n if(chmin(dp[a][nxt][nxtreach],dd)){\n pq.push(MP(MP(dp[a][nxt][nxtreach],nxtreach),MP(a,nxt)));\n }\n }else{\n if(nxt!=a){\n dd += c[nxt];\n nxtreach = reach|(1<<num[nxt]);\n }\n if(chmin(dp[a][nxt][nxtreach],dd)){\n pq.push(MP(MP(dp[a][nxt][nxtreach],nxtreach),MP(a,nxt)));\n }\n }\n }\n }\n if(h[a]==h[b]){\n for(auto x:gu[a]){\n int nxt = x.first;\n int dd = cost + x.second;\n int nxtreach = reach;\n \n if(((reach>>num[nxt])&1)==0){\n dd += c[nxt];\n nxtreach = reach|(1<<num[nxt]);\n }\n if(chmin(dp[nxt][b][nxtreach],dd)){\n pq.push(MP(MP(dp[nxt][b][nxtreach],nxtreach),MP(nxt,b)));\n }\n \n }\n for(auto x:gd[b]){\n int nxt = x.first;\n int dd = cost + x.second;\n int nxtreach = reach;\n \n if(((reach>>num[nxt])&1)==0){\n dd += c[nxt];\n nxtreach = reach|(1<<num[nxt]);\n }\n if(chmin(dp[a][nxt][nxtreach],dd)){\n pq.push(MP(MP(dp[a][nxt][nxtreach],nxtreach),MP(a,nxt)));\n }\n \n }\n }\n }\n if(dp[n-1][n-1][1]==inf){\n cout << -1 << endl;\n }else{\n cout << dp[n-1][n-1][1] << endl;\n }\n }\n return 0;\n}", "accuracy": 1, "time_ms": 190, "memory_kb": 15108, "score_of_the_acc": -0.2096, "final_rank": 9 } ]
aoj_1325_cpp
Problem A: Ginkgo Numbers We will define Ginkgo numbers and multiplication on Ginkgo numbers. A Ginkgo number is a pair < m , n > where m and n are integers. For example, <1, 1>, <-2, 1> and <-3,-1> are Ginkgo numbers. The multiplication on Ginkgo numbers is defined by < m , n > · < x , y > = < mx − ny , my + nx >. For example, <1, 1> · <-2, 1> = <-3,-1>. A Ginkgo number < m , n > is called a divisor of a Ginkgo number < p , q > if there exists a Ginkgo number < x , y > such that < m , n > · < x , y > = < p , q >. For any Ginkgo number < m , n >, Ginkgo numbers <1, 0>, <0, 1>, <-1, 0>, <0,-1>, < m , n >, <- n , m >, <- m ,- n > and < n ,- m > are divisors of < m , n >. If m 2 + n 2 > 1, these Ginkgo numbers are distinct. In other words, any Ginkgo number such that m 2 + n 2 > 1 has at least eight divisors. A Ginkgo number < m , n > is called a prime if m 2 + n 2 > 1 and it has exactly eight divisors. Your mission is to check whether a given Ginkgo number is a prime or not. The following two facts might be useful to check whether a Ginkgo number is a divisor of another Ginkgo number. Suppose m 2 + n 2 > 0. Then, < m , n > is a divisor of < p , q > if and only if the integer m 2 + n 2 is a common divisor of mp + nq and mq − np . If < m , n > · < x , y > = < p , q >, then ( m 2 + n 2 )( x 2 + y 2 ) = p 2 + q 2 . Input The first line of the input contains a single integer, which is the number of datasets. The rest of the input is a sequence of datasets. Each dataset is a line containing two integers m and n , separated by a space. They designate the Ginkgo number < m , n >. You can assume 1 < m 2 + n 2 < 20000. Output For each dataset, output a character ' P ' in a line if the Ginkgo number is a prime. Output a character ' C ' in a line otherwise. Sample Input 8 10 0 0 2 -3 0 4 2 0 -13 -4 1 -2 -1 3 -1 Output for the Sample Input C C P C C P P C
[ { "submission_id": "aoj_1325_10853137", "code_snippet": "#include<cstring>\n#include<string>\n#include<sstream>\n#include<fstream>\n#include<iostream>\n#include<iomanip>\n#include<cstdio>\n#include<cctype>\n#include<algorithm>\n#include<queue>\n#include<deque>\n#include<map>\n#include<set>\n#include<vector>\n#include<stack>\n#include<ctime>\n#include<cstdlib>\n#include<functional>\n#include<cmath>\nusing namespace std;\n#define PI acos(-1.0)\n#define eps 1e-7\n#define INF 0x3F3F3F3F //0x7FFFFFFF\n#define LLINF 0x7FFFFFFFFFFFFFFF\n#define seed 1313131\n#define MOD 1000000007\n#define ll long long\n#define ull unsigned ll\n#define lson l,m,rt<<1\n#define rson m+1,r,rt<<1|1\nint main ()\n{\n int N;\n scanf(\"%d\",&N);\n while(N--)\n {\n int p,q;\n scanf(\"%d%d\",&p,&q);\n\n int flag = 1;\n for(int m=-150; m<150; m++)\n {\n if(flag == 0 ) break;\n for(int n=-150; n<150; n++)\n {\n if(m==0&&n==1) continue;\n if(m==0&&n==-1) continue;\n if(m==1&&n==0) continue;\n if(m==-1&&n==0) continue;\n if(m==p&&n==q) continue;\n if(m==-p&&n==-q) continue;\n if(m==-q&&n==p) continue;\n if(m==q&&n==-p) continue;\n if(m==0&&n==0) continue;\n\n int num =m*m+n*n;\n int he1 = m*p+n*q;\n int he2 = m*q-n*p;\n if(he1%num==0&&he2%num==0)\n {\n flag = 0;\n break;\n }\n }\n }\n if(flag == 0) printf(\"C\\n\");\n else printf(\"P\\n\");\n }\n\n}", "accuracy": 1, "time_ms": 20, "memory_kb": 3380, "score_of_the_acc": -0.1391, "final_rank": 11 }, { "submission_id": "aoj_1325_10030644", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\n#define all(a) a.begin(),a.end()\n#define reps(i, a, n) for (int i = (a); i < (int)(n); i++)\n#define rep(i, n) reps(i, 0, n)\n#define rreps(i, a, n) for (int i = (a); i > (int)(n); i--)\nconst long long mod = 1000000007;\nconst long long INF = 1e18;\nll myceil(ll a, ll b) {return (a+b-1)/b;}\n\n\nvoid solve() {\n int n,m;\n cin >> m >> n;\n\n int mx = n*n + m*m;\n\n reps(i,-150,150) {\n reps(j,-150,150) {\n if (i*i + j*j <= mx) {\n if ((i == 0 && j == 0) || (i == 1 && j == 0) || (i == 0 && j == 1) || (i == -1 && j == 0) || (i == 0 && j == -1) || (i == m && j == n) ||(i == -n && j == m) || (i == -m && j == -n) || (i == n && j == -m)) continue;\n if (gcd(i*m+j*n,i*n-j*m)%(i*i+j*j) == 0) {\n cout << 'C' << endl;\n return;\n }\n }\n }\n }\n\n cout << 'P' << endl;\n return;\n}\n\nint main() {\n cin.tie(nullptr);\n ios_base::sync_with_stdio(false);\n int t;\n cin >> t;\n rep(i,t) {\n solve();\n }\n}", "accuracy": 1, "time_ms": 80, "memory_kb": 3388, "score_of_the_acc": -0.1753, "final_rank": 15 }, { "submission_id": "aoj_1325_9188501", "code_snippet": "#include <algorithm>\n#include <cmath>\n#include <cstdlib>\n#include <iomanip>\n#include <iostream>\n#include <list>\n#include <map>\n#include <numeric>\n#include <queue>\n#include <set>\n#include <string>\n#include <vector>\n\nusing namespace std;\n\nbool isPrime(int m, int n) {\n\tfor (int d = -142; d <= 142; d++) {\n\t\tfor (int e = -142; e <= 142; e++) {\n\t\t\tint d2pe2 = d * d + e * e;\n\t\t\tif (d2pe2 <= 1 || 20000 <= d2pe2 || d == m && e == n || d == -n && e == m || d == -m && e == -n || d == n && e == -m) continue;\n\n\t\t\tif ((d * m + e * n) % d2pe2 == 0 && (d * n - e * m) % d2pe2 == 0) return false;\n\t\t}\n\t}\n\n\treturn true;\n}\n\nint main() {\n\tint numof_data;\n\tcin >> numof_data;\n\n\tfor (int data = 0; data < numof_data; data++) {\n\t\tint m, n;\n\t\tcin >> m >> n;\n\n\t\tif (isPrime(m, n)) {\n\t\t\tcout << \"P\" << endl;\n\t\t} else {\n\t\t\tcout << \"C\" << endl;\n\t\t}\n\t}\n\n\treturn 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3336, "score_of_the_acc": -0.1185, "final_rank": 8 }, { "submission_id": "aoj_1325_7138378", "code_snippet": "// author: hanyu\n#include <bits/stdc++.h>\nusing namespace std;\n\nint main() {\n cin.tie(0);\n ios::sync_with_stdio(false);\n\n int t;\n cin >> t;\n\n while (t--) {\n int m, n;\n cin >> m >> n;\n\n int lim = max(abs(n), abs(m)), count = 0;\n for (int i = -lim; i <= lim; i++) {\n for (int j = -lim; j <= lim; j++) {\n if (i == 0 && j == 0) continue;\n int tmp = i * i + j * j;\n int x = m * i + n * j, y = n * i - m * j;\n if (x % tmp == 0 && y % tmp == 0) count++;\n }\n }\n\n if (count == 8) cout << \"P\" << endl;\n else cout << \"C\" << endl;\n }\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3256, "score_of_the_acc": -0.0913, "final_rank": 5 }, { "submission_id": "aoj_1325_6794709", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\ntypedef long long ll;\ntypedef pair<ll,ll> pll;\ntypedef vector<ll> vll;\ntypedef vector<pll> vpll;\n#define mp make_pair\n#define pb push_back\n#define rep(i,n) for(int i=0;i<n;i++)\n#define per(i,n) for(int i=n-1;i>=0;i--)\n#define rrep(i,l,r) for(int i=l;i<=r;i++)\n#define chmin(a,b) a=min(a,b)\n#define chmax(a,b) a=max(a,b)\n#define all(x) x.begin(),x.end()\n#define unq(x) sort(all(x));x.erase(unique(all(x)),x.end())\n//#define mod 1000000007\n#define mod 998244353\n//ll mod;\n#define ad(a,b) a=(a+b)%mod;\n#define mul(a,b) a=a*b%mod;\nvoid readv(vector<ll> &a,ll n){\n\trep(i,n){\n\t\tll x;\n\t\tcin>>x;\n\t\ta.push_back(x);\n\t}\n}\nvoid outv(vector<ll> &a,ll n){\n\trep(i,n){\n\t\tif(i>0)cout<<\" \";\n\t\tcout<<a[i];\n\t}\n\tcout<<\"\\n\";\n}\nll po(ll x,ll y){\n\tll res=1;\n\tfor(;y;y>>=1){\n\t\tif(y&1)res=res*x%mod;\n\t\tx=x*x%mod;\n\t}\n\treturn res;\n}\nll gcd(ll a,ll b){\n\treturn (b?gcd(b,a%b):a);\n}\n#define FACMAX 200010\nll fac[FACMAX],inv[FACMAX],ivf[FACMAX];\nvoid initfac(){\n\tfac[0]=ivf[0]=inv[1]=1;\n\tfor(ll i=1;i<FACMAX;i++)fac[i]=fac[i-1]*i%mod;\n\tfor(ll i=1;i<FACMAX;i++){\n\t\tif(i>1)inv[i]=(mod-mod/i*inv[mod%i]%mod)%mod;\n\t\tivf[i]=po(fac[i],mod-2);\n\t}\n}\nll P(ll n,ll k){\n\tif(n<0||n<k)return 0;\n\treturn fac[n]*ivf[n-k]%mod;\n}\nll C(ll n,ll k){\n\tif(k<0||n<k)return 0;\n\treturn fac[n]*ivf[n-k]%mod*ivf[k]%mod;\n}\nll H(ll n,ll k){\n\treturn C(n+k-1,k);\n}\n\nll p,q;\nbool judge(ll n,ll m){\n\tif(n*n+m*m==1)return 0;\n\tif(p==m&&q==n)return 0;\n\tif(p==-m&&q==-n)return 0;\n\tif(p==-n&&q==m)return 0;\n\tif(p==n&&q==-m)return 0;\n\treturn (m*p+n*q)%(m*m+n*n)==0&&(m*q-n*p)%(m*m+n*n)==0;\n}\nvoid solve(){\n\tcin>>p>>q;\n\tll val=p*p+q*q;\n\tbool ans=0;\n\tfor(ll d=1;d<=val;d++)if(val%d==0){\n\t\tfor(int n=0;n*n<=d;n++){\n\t\t\tint m=(int)sqrt(d-n*n);\n\t\t\trep(f,2){\n\t\t\t\trep(g,2){\n\t\t\t\t\tans|=judge(n,m);\n\t\t\t\t\tm*=-1;\n\t\t\t\t}\n\t\t\t\tn*=-1;\n\t\t\t}\n\t\t}\n\t}\n\tcout<<(ans?\"C\":\"P\")<<endl;\n\n}\nint main(){\n\tcin.tie(0);\n\tios::sync_with_stdio(0);\n\tll q;\n\tcin>>q;\n\twhile(q--)solve();\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3368, "score_of_the_acc": -0.1294, "final_rank": 9 }, { "submission_id": "aoj_1325_6376239", "code_snippet": "#include <iostream>\n#include <algorithm>\nusing namespace std;\n\nvoid solver(int m, int n){\n for(int x = -200; x <= 200; x++){\n for(int y = -200; y <= 200; y++){\n if(min(x, y) == 0 && max(x, y) == 1) continue;\n if(min(x, y) == -1 && max(x, y) == 0) continue;\n if(x == m && y == n) continue;\n if(x == -n && y == m) continue;\n if(x == -m && y == -n) continue;\n if(x == n && y == -m) continue;\n int tot = x*x + y*y;\n if(tot > 0){\n if((m*x+n*y)%tot == 0 && (x*n-y*m)%tot == 0){\n // cout << x << \" \" << y << \" \";\n cout << \"C\" << '\\n';\n return;\n }\n }\n }\n }\n cout << \"P\" << '\\n';\n}\n\nint main(){\n int t;\n cin >> t;\n while(t--){\n int m, n;\n cin >> m >> n;\n solver(m, n);\n }\n}", "accuracy": 1, "time_ms": 50, "memory_kb": 3260, "score_of_the_acc": -0.115, "final_rank": 7 }, { "submission_id": "aoj_1325_6221257", "code_snippet": "#include <stdio.h>\n#include <stdlib.h>\n#include <set>\nusing namespace std;\n\nint gn[65536][2];\nset<pair<int, int>> sieve;\n\nint cmp1(const void* a, const void* b) {\n int ai = *(int*)a;\n int bi = *(int*)b;\n int aj = *((int*)a + 1);\n int bj = *((int*)b + 1);\n return ai * ai + aj * aj > bi * bi + bj * bj ? 1 : ai * ai + aj * aj == bi * bi + bj * bj ? 0 : -1;\n}\n\nint main(void) {\n int cnt = 0, t, x, y;\n for (int i = -200; i <= 200; i++) {\n for (int j = -200; j <= 200; j++) {\n if (i * i + j * j < 20000) {\n gn[cnt][0] = i;\n gn[cnt++][1] = j;\n }\n }\n }\n qsort(gn, cnt, sizeof(int) * 2, cmp1);\n for (int i = 5; i < cnt; i++) {\n if (sieve.find(pair<int, int>(gn[i][0], gn[i][1])) != sieve.end()) continue;\n for (int j = 5; j < cnt; j++) {\n x = gn[i][0] * gn[j][0] - gn[i][1] * gn[j][1];\n y = gn[i][0] * gn[j][1] + gn[i][1] * gn[j][0];\n if (x * x + y * y > 20000) break;\n sieve.insert(pair<int, int>(x, y));\n }\n }\n //freopen(\"E:\\\\PS\\\\ICPC\\\\Asia Pacific\\\\Japan Regional\\\\2012\\\\InputsAndOutputs12\\\\A.in\", \"r\", stdin);\n //freopen(\"E:\\\\PS\\\\ICPC\\\\Asia Pacific\\\\Japan Regional\\\\2012\\\\InputsAndOutputs12\\\\A_t.out\", \"w\", stdout);\n scanf(\"%d\", &t);\n for (int tt = 0; tt < t; tt++) {\n scanf(\"%d %d\", &x, &y);\n if (sieve.find(pair<int, int>(x, y)) != sieve.end()) printf(\"C\\n\");\n else printf(\"P\\n\");\n }\n return 0;\n}", "accuracy": 1, "time_ms": 70, "memory_kb": 5924, "score_of_the_acc": -1.0335, "final_rank": 18 }, { "submission_id": "aoj_1325_5997974", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\n#define all(A) A.begin(),A.end()\nusing vll = vector<ll>;\n#define rep(i, n) for (long long i = 0; i < (long long)(n); i++)\n\n\nvll dx = { 1,-1,0 };\nvll dy = { 1,-1,0 };\nint main() {\n ll N;\n cin>>N;\n rep(n,N){\n ll M,N;\n cin>>M>>N;\n ll D=M*M+N*N;\n ll k=0;\n for(ll x=-D;x<=D;x++){\n ll B=D-x*x;\n B=sqrt(B)+1;\n for(ll y=-B;y<=B;y++){\n if(x+y==0)continue;\n ll C=x*x+y*y;\n if((M*x+N*y)%C==0&&(M*y-N*x)%C==0){\n k++;\n //cout<<x<<\" \"<<y<<endl;\n }\n }\n }\n cout<<(k==8?\"P\":\"C\")<<endl;\n }\n}", "accuracy": 1, "time_ms": 80, "memory_kb": 3208, "score_of_the_acc": -0.114, "final_rank": 6 }, { "submission_id": "aoj_1325_5847349", "code_snippet": "#pragma region Macros\n#include <bits/stdc++.h>\nusing namespace std;\ntemplate <class T> inline bool chmax(T &a, T b) {\n if(a < b) {\n a = b;\n return 1;\n }\n return 0;\n}\ntemplate <class T> inline bool chmin(T &a, T b) {\n if(a > b) {\n a = b;\n return 1;\n }\n return 0;\n}\n#ifdef DEBUG\ntemplate <class T, class U>\nostream &operator<<(ostream &os, const pair<T, U> &p) {\n os << '(' << p.first << ',' << p.second << ')';\n return os;\n}\ntemplate <class T> ostream &operator<<(ostream &os, const vector<T> &v) {\n os << '{';\n for(int i = 0; i < (int)v.size(); i++) {\n if(i) { os << ','; }\n os << v[i];\n }\n os << '}';\n return os;\n}\nvoid debugg() { cerr << endl; }\ntemplate <class T, class... Args>\nvoid debugg(const T &x, const Args &... args) {\n cerr << \" \" << x;\n debugg(args...);\n}\n#define debug(...) \\\n cerr << __LINE__ << \" [\" << #__VA_ARGS__ << \"]: \", debugg(__VA_ARGS__)\n#define dump(x) cerr << __LINE__ << \" \" << #x << \" = \" << (x) << endl\n#else\n#define debug(...) (void(0))\n#define dump(x) (void(0))\n#endif\n\nstruct Setup {\n Setup() {\n cin.tie(0);\n ios::sync_with_stdio(false);\n cout << fixed << setprecision(15);\n }\n} __Setup;\n\nusing ll = long long;\n#define ALL(v) (v).begin(), (v).end()\n#define RALL(v) (v).rbegin(), (v).rend()\n#define FOR(i, a, b) for(int i = (a); i < int(b); i++)\n#define REP(i, n) FOR(i, 0, n)\nconst int INF = 1 << 30;\nconst ll LLINF = 1LL << 60;\nconstexpr int MOD = 1000000007;\nconst int dx[4] = {1, 0, -1, 0};\nconst int dy[4] = {0, 1, 0, -1};\n\nvoid Case(int i) { cout << \"Case #\" << i << \": \"; }\nint popcount(int x) { return __builtin_popcount(x); }\nll popcount(ll x) { return __builtin_popcountll(x); }\n#pragma endregion Macros\n\nusing P = pair<ll, ll>;\nconst int MAX = 20000;\n\nvoid solve() {\n ll m, n;\n cin >> m >> n;\n vector<P> kouho;\n for(ll a = 0; a <= MAX and a * a <= m * m + n * n; a++) {\n for(ll b = 0; b <= MAX and a * a + b * b <= m * m + n * n; b++) {\n if((a * a + b * b) == 0) continue;\n if((m * m + n * n) % (a * a + b * b) == 0) {\n kouho.emplace_back(a, b);\n kouho.emplace_back(-a, b);\n kouho.emplace_back(a, -b);\n kouho.emplace_back(-a, -b);\n }\n }\n }\n sort(ALL(kouho));\n kouho.erase(unique(ALL(kouho)), kouho.end());\n // debug(m, n);\n // debug(kouho);\n int sz = kouho.size();\n ll ans = 0;\n REP(i, sz) {\n auto [a1, b1] = kouho[i];\n if((a1*m + b1*n) % (a1*a1+b1*b1) != 0 or (a1*n - b1*m) % (a1*a1+b1*b1) != 0) continue;\n bool ok = false;\n REP(j, sz) {\n auto [a2, b2] = kouho[j];\n if((a1 * a1 + b1 * b1) * (a2 * a2 + b2 * b2) == (m * m + n * n)) {\n ok = true;\n break;\n }\n }\n // if(ok) debug(a1, b1);\n ans += ok;\n }\n // debug(ans);\n assert(ans >= 8);\n cout << (ans == 8 ? \"P\" : \"C\") << \"\\n\";\n}\n\nint main() {\n int T;\n cin >> T;\n while(T--) solve();\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3484, "score_of_the_acc": -0.1689, "final_rank": 13 }, { "submission_id": "aoj_1325_5757890", "code_snippet": "#ifdef LOCAL\n#define _GLIBCXX_DEBUG\n#endif\n \n#include <bits/stdc++.h>\nusing namespace std;\n#if __has_include(<atcoder/all>)\n#include <atcoder/all>\nusing namespace atcoder;\n#endif\n \n#pragma region Macros\n// rep macro\n#define foa(v, a) for(auto &v : a)\n#define REPname(a, b, c, d, e, ...) e\n#define REP(...) REPname(__VA_ARGS__, REP3, REP2, REP1, REP0)(__VA_ARGS__)\n#define REP0(x) for(int i = 0; i < (x); ++i)\n#define REP1(i, x) for(int i = 0; i < (x); ++i)\n#define REP2(i, l, r) for(int i = (l); i < (r); ++i)\n#define REP3(i, l, r, c) for(int i = (l); i < (r); i += (c))\n#define REPSname(a, b, c, ...) c\n#define REPS(...) REPSname(__VA_ARGS__, REPS1, REPS0)(__VA_ARGS__)\n#define REPS0(x) for(int i = 1; i <= (x); ++i)\n#define REPS1(i, x) for(int i = 1; i <= (x); ++i)\n#define RREPname(a, b, c, d, e, ...) e\n#define RREP(...) RREPname(__VA_ARGS__, RREP3, RREP2, RREP1, RREP0)(__VA_ARGS__)\n#define RREP0(x) for(int i = (x)-1; i >= 0; --i)\n#define RREP1(i, x) for(int i = (x)-1; i >= 0; --i)\n#define RREP2(i, l, r) for(int i = (r)-1; i >= (l); --i)\n#define RREP3(i, l, r, c) for(int i = (r)-1; i >= (l); i -= (c))\n#define RREPSname(a, b, c, ...) c\n#define RREPS(...) RREPSname(__VA_ARGS__, RREPS1, RREPS0)(__VA_ARGS__)\n#define RREPS0(x) for(int i = (x); i >= 1; --i)\n#define RREPS1(i, x) for(int i = (x); i >= 1; --i)\n \n// name macro\n#define fi first\n#define se second\n#define pb push_back\n#define eb emplace_back\n#define SZ(x) ((int)(x).size())\n#define all(x) (x).begin(), (x).end()\n#define rall(x) (x).rbegin(), (x).rend()\n#define popcnt(x) __builtin_popcountll(x)\ntemplate <class T = int>\nusing V = std::vector<T>;\ntemplate <class T = int>\nusing VV = std::vector<std::vector<T>>;\ntemplate <class T = int>\nusing VVV = std::vector<std::vector<std::vector<T>>>;\ntemplate <class T>\nusing pqup = std::priority_queue<T, std::vector<T>, std::greater<T>>;\nusing ll = long long;\nusing ld = long double;\nusing int128 = __int128_t;\nusing pii = std::pair<int, int>;\nusing pll = std::pair<long long, long long>;\nusing Tp = tuple<ll,ll,ll>;\n \n// input macro\ntemplate <class T, class U>\nstd::istream &operator>>(std::istream &is, std::pair<T, U> &p) {\n\tis >> p.first >> p.second;\n\treturn is;\n}\ntemplate <class T>\nstd::istream &operator>>(std::istream &is, std::vector<T> &v) {\n\tfor(T &i : v) is >> i;\n\treturn is;\n}\nstd::istream &operator>>(std::istream &is, __int128_t &a) {\n\tstd::string s;\n\tis >> s;\n\t__int128_t ret = 0;\n\tfor(int i = 0; i < s.length(); i++)\n\t\tif('0' <= s[i] and s[i] <= '9')\n\t\t\tret = 10 * ret + s[i] - '0';\n\ta = ret * (s[0] == '-' ? -1 : 1);\n\treturn is;\n}\nnamespace scanner {\nvoid scan(int &a) { std::cin >> a; }\nvoid scan(long long &a) { std::cin >> a; }\nvoid scan(std::string &a) { std::cin >> a; }\nvoid scan(char &a) { std::cin >> a; }\nvoid scan(char a[]) { std::scanf(\"%s\", a); }\nvoid scan(double &a) { std::cin >> a; }\nvoid scan(long double &a) { std::cin >> a; }\ntemplate <class T, class U>\nvoid scan(std::pair<T, U> &p) { std::cin >> p; }\ntemplate <class T>\nvoid scan(std::vector<T> &a) { std::cin >> a; }\nvoid INPUT() {}\ntemplate <class Head, class... Tail>\nvoid INPUT(Head &head, Tail &... tail) {\n\tscan(head);\n\tINPUT(tail...);\n}\n} // namespace scanner\n#define VEC(type, name, size) \\\n\tstd::vector<type> name(size); \\\n\tscanner::INPUT(name)\n#define VVEC(type, name, h, w) \\\n\tstd::vector<std::vector<type>> name(h, std::vector<type>(w)); \\\n\tscanner::INPUT(name)\n#define INT(...) \\\n\tint __VA_ARGS__; \\\n\tscanner::INPUT(__VA_ARGS__)\n#define LL(...) \\\n\tlong long __VA_ARGS__; \\\n\tscanner::INPUT(__VA_ARGS__)\n#define STR(...) \\\n\tstd::string __VA_ARGS__; \\\n\tscanner::INPUT(__VA_ARGS__)\n#define CHAR(...) \\\n\tchar __VA_ARGS__; \\\n\tscanner::INPUT(__VA_ARGS__)\n#define DOUBLE(...) \\\n\tdouble __VA_ARGS__; \\\n\tscanner::INPUT(__VA_ARGS__)\n#define LD(...) \\\n\tlong double __VA_ARGS__; \\\n\tscanner::INPUT(__VA_ARGS__)\n \n// output-macro\ntemplate <class T, class U>\nstd::ostream &operator<<(std::ostream &os, const std::pair<T, U> &p) {\n\tos << p.first << \" \" << p.second;\n\treturn os;\n}\ntemplate <class T>\nstd::ostream &operator<<(std::ostream &os, const std::vector<T> &a) {\n\tfor(int i = 0; i < int(a.size()); ++i) {\n\t\tif(i) os << \" \";\n\t\tos << a[i];\n\t}\n\treturn os;\n}\nstd::ostream &operator<<(std::ostream &dest, __int128_t &value) {\n\tstd::ostream::sentry s(dest);\n\tif(s) {\n\t\t__uint128_t tmp = value < 0 ? -value : value;\n\t\tchar buffer[128];\n\t\tchar *d = std::end(buffer);\n\t\tdo {\n\t\t\t--d;\n\t\t\t*d = \"0123456789\"[tmp % 10];\n\t\t\ttmp /= 10;\n\t\t} while(tmp != 0);\n\t\tif(value < 0) {\n\t\t\t--d;\n\t\t\t*d = '-';\n\t\t}\n\t\tint len = std::end(buffer) - d;\n\t\tif(dest.rdbuf()->sputn(d, len) != len) {\n\t\t\tdest.setstate(std::ios_base::badbit);\n\t\t}\n\t}\n\treturn dest;\n}\ntemplate <class T>\nvoid print(const T a) { std::cout << a << '\\n'; }\ntemplate <class Head, class... Tail>\nvoid print(Head H, Tail... T) {\n\tstd::cout << H << ' ';\n\tprint(T...);\n}\ntemplate <class T>\nvoid printel(const T a) { std::cout << a << '\\n'; }\ntemplate <class T>\nvoid printel(const std::vector<T> &a) {\n\tfor(const auto &v : a)\n\t\tstd::cout << v << '\\n';\n}\ntemplate <class Head, class... Tail>\nvoid printel(Head H, Tail... T) {\n\tstd::cout << H << '\\n';\n\tprintel(T...);\n}\nvoid Yes(const bool b = true) { std::cout << (b ? \"Yes\\n\" : \"No\\n\"); }\nvoid No() { std::cout << \"No\\n\"; }\nvoid YES(const bool b = true) { std::cout << (b ? \"YES\\n\" : \"NO\\n\"); }\nvoid NO() { std::cout << \"No\\n\"; }\nvoid err(const bool b = true) {\n\tif(b) {\n\t\tstd::cout << \"-1\\n\", exit(0);\n\t}\n}\n \n//debug macro\nnamespace debugger {\ntemplate <class T>\nvoid view(const std::vector<T> &a) {\n\tstd::cerr << \"{ \";\n\tfor(const auto &v : a) {\n\t\tstd::cerr << v << \", \";\n\t}\n\tstd::cerr << \"\\b\\b }\";\n}\ntemplate <class T>\nvoid view(const std::vector<std::vector<T>> &a) {\n\tstd::cerr << \"{\\n\";\n\tfor(const auto &v : a) {\n\t\tstd::cerr << \"\\t\";\n\t\tview(v);\n\t\tstd::cerr << \"\\n\";\n\t}\n\tstd::cerr << \"}\";\n}\ntemplate <class T, class U>\nvoid view(const std::vector<std::pair<T, U>> &a) {\n\tstd::cerr << \"{\\n\";\n\tfor(const auto &p : a) std::cerr << \"\\t(\" << p.first << \", \" << p.second << \")\\n\";\n\tstd::cerr << \"}\";\n}\ntemplate <class T, class U>\nvoid view(const std::map<T, U> &m) {\n\tstd::cerr << \"{\\n\";\n\tfor(const auto &p : m) std::cerr << \"\\t[\" << p.first << \"] : \" << p.second << \"\\n\";\n\tstd::cerr << \"}\";\n}\ntemplate <class T, class U>\nvoid view(const std::pair<T, U> &p) { std::cerr << \"(\" << p.first << \", \" << p.second << \")\"; }\ntemplate <class T>\nvoid view(const std::set<T> &s) {\n\tstd::cerr << \"{ \";\n\tfor(auto &v : s) {\n\t\tview(v);\n\t\tstd::cerr << \", \";\n\t}\n\tstd::cerr << \"\\b\\b }\";\n}\n \ntemplate <class T>\nvoid view(const T &e) { std::cerr << e; }\n} // namespace debugger\n#ifdef LOCAL\nvoid debug_out() {}\ntemplate <typename Head, typename... Tail>\nvoid debug_out(Head H, Tail... T) {\n\tdebugger::view(H);\n\tstd::cerr << \", \";\n\tdebug_out(T...);\n}\n#define debug(...) \\\n\tdo { \\\n\t\tstd::cerr << __LINE__ << \" [\" << #__VA_ARGS__ << \"] : [\"; \\\n\t\tdebug_out(__VA_ARGS__); \\\n\t\tstd::cerr << \"\\b\\b]\\n\"; \\\n\t} while(false)\n#else\n#define debug(...) (void(0))\n#endif\n \n// vector macro\ntemplate <class T>\nint lb(const std::vector<T> &a, const T x) { return std::distance((a).begin(), std::lower_bound((a).begin(), (a).end(), (x))); }\ntemplate <class T>\nint ub(const std::vector<T> &a, const T x) { return std::distance((a).begin(), std::upper_bound((a).begin(), (a).end(), (x))); }\ntemplate <class T>\nvoid UNIQUE(std::vector<T> &a) {\n\tstd::sort(a.begin(), a.end());\n\ta.erase(std::unique(a.begin(), a.end()), a.end());\n}\ntemplate <class T>\nstd::vector<T> press(std::vector<T> &a) {\n\tauto res = a;\n\tUNIQUE(res);\n\tfor(auto &v : a)\n\t\tv = lb(res, v);\n\treturn res;\n}\n#define SORTname(a, b, c, ...) c\n#define SORT(...) SORTname(__VA_ARGS__, SORT1, SORT0, ...)(__VA_ARGS__)\n#define SORT0(a) std::sort((a).begin(), (a).end())\n#define SORT1(a, c) std::sort((a).begin(), (a).end(), [](const auto x, const auto y) { return x c y; })\ntemplate <class T>\nvoid ADD(std::vector<T> &a, const T x) {\n\tfor(auto &v : a) v += x;\n}\ntemplate <class T>\nvoid SUB(std::vector<T> &a, const T x = 1) {\n\tfor(auto &v : a) v -= x;\n}\ntemplate <class T>\nvoid MUL(std::vector<T> &a, const T x) {\n\tfor(auto &v : a) v *= x;\n}\ntemplate <class T>\nvoid DIV(std::vector<T> &a, const T x) {\n\tfor(auto &v : a) v /= x;\n}\n \n// math macro\ntemplate <class T, class U>\ninline bool chmin(T &a, const U &b) { return a > b ? a = b, true : false; }\ntemplate <class T, class U>\ninline bool chmax(T &a, const U &b) { return a < b ? a = b, true : false; }\ntemplate <class T>\nT divup(T x, T y) { return (x + y - 1) / y; }\ntemplate <class T>\nT POW(T a, long long n) {\n\tT ret = 1;\n\twhile(n) {\n\t\tif(n & 1) ret *= a;\n\t\ta *= a;\n\t\tn >>= 1;\n\t}\n\treturn ret;\n}\n\ntemplate<typename T>\nT ADD(T a, T b){\n\tT res;\n\treturn __builtin_add_overflow(a, b, &res) ? numeric_limits<T>::max() : res;\n}\n\ntemplate<typename T>\nT MUL(T a, T b){\n\tT res;\n\treturn __builtin_mul_overflow(a, b, &res) ? numeric_limits<T>::max() : res;\n}\n\n// modpow\nlong long POW(long long a, long long n, const int mod) {\n\tlong long ret = 1;\n\twhile(n) {\n\t\tif(n & 1) (ret *= a) %= mod;\n\t\t(a *= a) %= mod;\n\t\tn >>= 1;\n\t}\n\treturn ret;\n}\n \n// others\nstruct fast_io {\n\tfast_io() {\n\t\tios::sync_with_stdio(false);\n\t\tcin.tie(nullptr);\n\t\tcout << fixed << setprecision(15);\n\t}\n} fast_io_;\n\n#pragma endregion\n\nusing R = long double;\nconstexpr R EPS=1E-11;\n\n// r の(誤差付きの)符号に従って, -1, 0, 1 を返す.\nint sgn(const R& r){ return (r > EPS) - (r < -EPS); }\n// a, b の(誤差付きの)大小比較の結果に従って, -1, 0, 1 を返す.\nint sgn(const R& a, const R &b){ return sgn(a-b); }\n// a > 0 は sgn(a) > 0\n// a < b は sgn(a, b) < 0\n// a >= b は sgn(a, b) >= 0\n// のように書く.\n// return s * 10^n\n\n//https://atcoder.jp/contests/abc191/submissions/20028529\nlong long x10(const string& s, size_t n) {\n if (s.front() == '-') return -x10(s.substr(1), n);\n auto pos = s.find('.');\n if (pos == string::npos) return stoll(s + string(n, '0'));\n return stoll(s.substr(0, pos) + s.substr(pos + 1) + string(n + pos + 1 - s.size(), '0'));\n}\n \nlong long ceildiv(long long a, long long b) {\n if (b < 0) a = -a, b = -b;\n if (a >= 0) return (a + b - 1) / b;\n else return a / b;\n}\n \nlong long floordiv(long long a, long long b) {\n if (b < 0) a = -a, b = -b;\n if (a >= 0) return a / b;\n else return (a - b + 1) / b;\n}\n \nlong long floorsqrt(long long x) {\n assert(x >= 0);\n long long ok = 0;\n long long ng = 1;\n while (ng * ng <= x) ng <<= 1;\n while (ng - ok > 1) {\n long long mid = (ng + ok) >> 1;\n if (mid * mid <= x) ok = mid;\n else ng = mid;\n }\n return ok;\n}\n\ninline int topbit(unsigned long long x){\n\treturn x?63-__builtin_clzll(x):-1;\n}\n\ninline int popcount(unsigned long long x){\n\treturn __builtin_popcountll(x);\n}\n\ninline int parity(unsigned long long x){//popcount%2\n\treturn __builtin_parity(x);\n}\n\nconst int inf = 1e9;\nconst ll INF = 1e18;\n\n\n\nvoid main_() {\n\tint q;\n\tcin >> q;\n\tREP(i,q){\n\t int p,q;\n\t cin >> p >> q;\n\t bool ex = false;\n\t for(int i = -200;i <= 200;i++){\n\t for(int j = -200;j <= 200;j++){\n\t if(i == 0 && j == 0)continue;\n\t int n = q * i - p * j;\n\t int m = p * i + q * j;\n\t int sum = i * i + j * j;\n\t if(sum == 1 || sum == p * p + q * q)continue;\n\t if(n % sum == 0 && m % sum == 0){\n\t ex = true;\n\t }\n\t }\n\t }\n\t if(ex)cout << 'C' << endl;\n\t else cout << 'P' << endl;\n\t}\n}\n \nint main() {\n\tint t = 1;\n\t//cin >> t;\n\twhile(t--) main_();\n\treturn 0;\n}", "accuracy": 1, "time_ms": 70, "memory_kb": 3396, "score_of_the_acc": -0.1725, "final_rank": 14 }, { "submission_id": "aoj_1325_5751359", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\n#define REP(i,n) for(int i=0;i<(n);i++)\n#define ALL(v) v.begin(),v.end()\n\nbool D(int a,int b){\n a=abs(a);b=abs(b);\n return (a%b)==0;\n}\n\nint dx[4]={1,0,-1,0},dy[4]={0,1,0,-1};\n\nbool pr(int p,int q){\n for(int m=-200;m<=200;m++)for(int n=-200;n<=200;n++){\n if(m==0&&n==0)continue;\n bool out=false;\n REP(i,4)if(dx[i]==m&&dy[i]==n)out=true;\n if(m==p&&n==q)out=true;\n if(m==-q&&n==p)out=true;\n if(m==-p&&n==-q)out=true;\n if(m==q&&n==-p)out=true;\n if(out)continue;\n if(D(m*p+n*q,m*m+n*n)&&D(m*q-n*p,m*m+n*n))return false;\n }\n return true;\n}\n\nint main(){\n int x;cin>>x;\n while(x--){\n int p,q;cin>>p>>q;\n if(p*p+q*q==0)cout<<\"C\"<<endl;\n else if(pr(p,q))cout<<\"P\"<<endl;\n else cout<<\"C\"<<endl;\n }\n}", "accuracy": 1, "time_ms": 80, "memory_kb": 3268, "score_of_the_acc": -0.1345, "final_rank": 10 }, { "submission_id": "aoj_1325_5557205", "code_snippet": "#include <cmath>\n#include <deque>\n#include <algorithm>\n#include <iterator>\n#include <list>\n#include <tuple>\n#include <map>\n#include <unordered_map>\n#include <queue>\n#include <set>\n#include <unordered_set>\n#include <stack>\n#include <string>\n#include <vector>\n#include <fstream>\n#include <iostream>\n#include <functional>\n#include <numeric>\n#include <iomanip> \n#include <stdio.h>\n//eolibraries\n#define lnf 3999999999999999999\n#define inf 999999999\n#define fi first\n#define se second\n#define pb push_back\n#define ll long long\n#define all(c) (c).begin(),(c).end()\n#define sz(c) (int)(c).size()\n#define make_unique(a) sort(all(a)),a.erase(unique(all(a)),a.end())\n#define pii pair <int,int>\n#define rep(i,n) for(int i = 0 ; i < n ; i++) \n#define drep(i,n) for(int i = n-1 ; i >= 0 ; i--)\n#define crep(i,x,n) for(int i = x ; i < n ; i++)\n#define vi vector <int> \n#define vec(...) vector<__VA_ARGS__>\n#define fcin ios_base::sync_with_stdio(false),cin.tie(0),cout.tie(0);\n//eodefine\nusing namespace std;\n\nconst int max_n = 103002;\n\nvoid slv(int p,int q) {\n\tint ans=0;\n\tfor(int x=-200;x<=200;x++){\n\t\tfor(int y=-200;y<=200;y++) {\n\t\t\tif(x==0 and y==0)continue;\n\t\t\tint n = (q*x-p*y)/(y*y+x*x);\n\t\t\tint m = (q*y+p*x)/(y*y+x*x);\n\t\t\tif(m*x-n*y==p and m*y+n*x==q) ans++;\n\t\t\t// ans++;\n\t\t}\n\t}\n\tif(ans==8)cout<<\"P\\n\";\n\telse cout<<\"C\\n\";\n\t// cout<<ans<<\"\\n\";\n}\n\nint main(){\nfcin;\n\tint t;\n\tcin>>t;\n\twhile(t--) {\n\t\tint n,m;\n\t\tcin>>n>>m;\n\t\tslv(n,m);\n\t}\n/*\n*/\n return 0; \n}", "accuracy": 1, "time_ms": 130, "memory_kb": 3312, "score_of_the_acc": -0.1774, "final_rank": 16 }, { "submission_id": "aoj_1325_5086426", "code_snippet": "#include <bits/stdc++.h>\n\n#pragma GCC target(\"avx2\")\n#pragma GCC optimize(\"Ofast\")\n#pragma GCC optimize(\"unroll-loops\")\n//#include <boost/multiprecision/cpp_int.hpp>\nusing namespace std;\n\n//using namespace boost::multiprecision;\n//#include<atcoder/all>\n//#include<atcoder/segtree>\n//#include <atcoder/scc>\n//using namespace atcoder;\n\nusing dou =long double;\nstring yes=\"yes\";\nstring Yes=\"Yes\";\nstring YES=\"YES\";\nstring no=\"no\";\nstring No=\"No\";\nstring NO=\"NO\";\n\ntemplate<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; }\ntemplate<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; }\ntypedef long long ll;\ntypedef unsigned long long ull;\ntypedef pair<int,int> P;\ntypedef pair<ll,ll> PL;\n\nll mod = 998244353ll;\n//ll mod = 1000000007ll;\n//const ll mod = 4;\n\n\nstruct mint {\n ll x; // typedef long long ll;\n mint(ll x=0):x((x%mod+mod)%mod){}\n mint operator-() const { return mint(-x);}\n mint& operator+=(const mint a) {\n if ((x += a.x) >= mod) x -= mod;\n return *this;\n }\n mint& operator-=(const mint a) {\n if ((x += mod-a.x) >= mod) x -= mod;\n return *this;\n }\n mint& operator*=(const mint a) { (x *= a.x) %= mod; return *this;}\n mint operator+(const mint a) const { return mint(*this) += a;}\n mint operator-(const mint a) const { return mint(*this) -= a;}\n mint operator*(const mint a) const { return mint(*this) *= a;}\n mint pow(ll t) const {\n if (!t) return 1;\n mint a = pow(t>>1);\n a *= a;\n if (t&1) a *= *this;\n return a;\n }\n\n // for prime modhttps://atcoder.jp/contests/abc166/submit?taskScreenName=abc166_f\n mint inv() const { return pow(mod-2);}\n mint& operator/=(const mint a) { return *this *= a.inv();}\n mint operator/(const mint a) const { return mint(*this) /= a;}\n};\n\nistream& operator>>(istream& is, const mint& a) { return is >> a.x;}\nostream& operator<<(ostream& os, const mint& a) { return os << a.x;}\n\n#define rep(i, n) for(ll i = 0; i < (ll)(n); i++)\n//#define rep(i, n) for(int i = 0; i < (int)(n); i++)\n#define brep(n) for(int bit=0;bit<(1<<n);bit++)\n#define bbrep(n) for(int bbit=0;bbit<(1<<n);bbit++)\n#define erep(i,container) for (auto &i : container)\n#define itrep(i,container) for (auto i : container)\n#define irep(i, n) for(ll i = n-1; i >= (ll)0ll; i--)\n#define rrep(i,m,n) for(ll i = m; i < (ll)(n); i++)\n#define reprep(i,j,h,w) rep(i,h)rep(j,w)\n#define repreprep(i,j,k,h,w,n) rep(i,h)rep(j,w)rep(k,n)\n#define all(x) (x).begin(),(x).end()\n#define rall(x) (x).rbegin(),(x).rend()\n#define VEC(type,name,n) std::vector<type> name(n);rep(i,n)std::cin >> name[i];\n#define pb push_back\n#define pf push_front\n#define query int qq;std::cin >> qq;rep(qqq,qq)\n#define lb lower_bound\n#define ub upper_bound\n#define fi first\n#define se second\n#define itn int\n#define mp make_pair\n//#define sum(a) accumulate(all(a),0ll)\n#define keta fixed<<setprecision\n#define kout(d) std::cout << keta(10)<< d<< std::endl;\n#define vout(a) erep(qxqxqx,a)std::cout << qxqxqx << ' ';std::cout << std::endl;\n#define vvector(name,typ,m,n,a)vector<vector<typ> > name(m,vector<typ> (n,a))\n//#define vvector(name,typ,m,n)vector<vector<typ> > name(m,vector<typ> (n))\n#define vvvector(name,t,l,m,n,a) vector<vector<vector<t> > > name(l, vector<vector<t> >(m, vector<t>(n,a)));\n#define vvvvector(name,t,k,l,m,n,a) vector<vector<vector<vector<t> > > > name(k,vector<vector<vector<t> > >(l, vector<vector<t> >(m, vector<t>(n,a)) ));\n//#define case std::cout <<\"Case #\" <<qqq+1<<\":\"\n#define RES(a,iq,jq) a.resize(iq);rep(iii,iq)a[iii].resize(jq);\n\n#define RESRES(a,i,j,k) a.resize(i);rep(ii,i)a[ii].resize(j);reprep(ii,jj,i,j){a[ii][jj].resize(k);};\n#define res resize\n#define as assign\n#define ffor for(;;)\n#define ppri(a,b) std::cout << a<<\" \"<<b << std::endl\n#define pppri(a,b,c) std::cout << a<<\" \"<<b <<\" \"<< c<<std::endl\n#define ppppri(a,b,c,d) std::cout << a<<\" \"<<b <<\" \"<< c<<' '<<d<<std::endl\n#define aall(x,n) (x).begin(),(x).begin()+(n)\n#define SUMI(a) accumulate(all(a),0) \n#define SUM(a) accumulate(all(a),0ll) \n#define stirng string\n#define gin(a,b) int a,b;std::cin >> a>>b;a--;b--;\n#define popcount __builtin_popcount\n#define permu(a) next_permutation(all(a))\n#define aru(a,d) a.find(d)!=a.end()\n#define nai(a,d) a.find(d)==a.end()\n//#define aru p.find(mp(x,y))!=p.end()\n//#define grid_input(a,type) int h,w;std::cin >> h>>w;vvector(a,type,h,w,0);reprep(i,j,h,w)std::cin >> a[i][j];\n\n//typedef long long T;\nll ceili(ll a,ll b){\n return ((a+b-1)/b);\n}\nconst int INF = 2000000000;\n//const ll INF64 =922330720854775807ll;\nconst ll INF64 = 4223372036854775807ll;\n//const ll INF64 = 9223372036854775807ll;\n\n//const ll INF64 = 243'000'000'000'000'000'0;Q\n\n\n//const ll MOD = 1000000007ll;\nconst ll MOD = 998244353ll;\n//const ll MOD = 1000003ll;\nconst ll OD = 1000000000000007ll;\nconst dou pi=3.141592653589793;\n\nlong long modpow(long long a, long long n) { \n long long res = 1;\n while (n > 0) {\n if (n & 1) res = res * a % MOD;\n a = a * a % MOD;\n n >>= 1;\n }\n return res;\n}\nvector< ll > divisor(ll n) { //約数の列挙\n vector< ll > ret;\n for(ll i = 1; i * i <= n; i++) {\n if(n % i == 0) {\n ret.push_back(i);\n if(i * i != n) ret.push_back(n / i);\n }\n }\n sort(begin(ret), end(ret));\n return (ret);\n}\n//メモ\n//ゲーム(Grundy数とか)の復習をする\n//リスニング力をどうにかする\n//個数制限付きナップサックの復習\n//戻すDP\n//全方位木DPとスライド最小値\n//ゲーム→パリティに注目するといいことあるかも\n//ゲーム→もとの状態に戻せる状態を探す\n//理由がなければLLを使え!\n//理由がなければLLを使え!\n//理由がなければLLを使え!\n//理由がなければLLを使え!\n//理由がなければLLを使え!\n//理由がなければLLを使え!\n//理由がなければLLを使え!\n//理由がなければLLを使え!\n//来週の月曜日に銀行へ行く\n//積率を調べる実験を来週の火曜日までにやる\n//レピュニット数\n//フェルマーの小定理\n//拡張ユークリッド\n\nint main(){\n std::set<int> st;\n rrep(i,0,12000){\n st.insert(i*i);\n }\n query{\n ll p,q;\n std::cin >> p>>q;\n int v=p*p+q*q;\n auto di=divisor(p*p+q*q);\n std::vector<bool> ok(di.size());\n int po=0;\n char c='P';\n erep(ii,di){\n if(ii==1||ii==p*p+q*q){\n 1==1;\n }\n else{\n bool ook=0;\n erep(i,st){\n if(aru(st,ii-i))ook=1;\n }\n ok[po]=ook;\n }\n //rep(i,ok.size())std::cout << ok[i] << ' ';\n //std::cout << std::endl;\n // ppri(po,ok.size()-1-po);\n if(ok[po]&ok[ok.size()-1-po])c='C';\n po++;\n }\n std::cout << c << std::endl;\n }\n}", "accuracy": 1, "time_ms": 340, "memory_kb": 3608, "score_of_the_acc": -0.3955, "final_rank": 17 }, { "submission_id": "aoj_1325_4937879", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\n#define int long long\n\n// debug methods\n// usage: debug(x,y);\n#define CHOOSE(a) CHOOSE2 a\n#define CHOOSE2(a0,a1,a2,a3,a4,x,...) x\n#define debug_1(x1) cout<<#x1<<\": \"<<x1<<endl\n#define debug_2(x1,x2) cout<<#x1<<\": \"<<x1<<\", \"#x2<<\": \"<<x2<<endl\n#define debug_3(x1,x2,x3) cout<<#x1<<\": \"<<x1<<\", \"#x2<<\": \"<<x2<<\", \"#x3<<\": \"<<x3<<endl\n#define debug_4(x1,x2,x3,x4) cout<<#x1<<\": \"<<x1<<\", \"#x2<<\": \"<<x2<<\", \"#x3<<\": \"<<x3<<\", \"#x4<<\": \"<<x4<<endl\n#define debug_5(x1,x2,x3,x4,x5) cout<<#x1<<\": \"<<x1<<\", \"#x2<<\": \"<<x2<<\", \"#x3<<\": \"<<x3<<\", \"#x4<<\": \"<<x4<<\", \"#x5<<\": \"<<x5<<endl\n#ifdef _DEBUG\n#define debug(...) CHOOSE((__VA_ARGS__,debug_5,debug_4,debug_3,debug_2,debug_1,~))(__VA_ARGS__)\n#else\n#define debug(...)\n#endif\n\n#define MAX_N (1000006)\n#define INF (1LL << 60)\n#define eps 1e-9\nconst int MOD = (int)1e9 + 7;\n\n\nsigned main(){\n //cin.tie(nullptr);\n //std::ios::sync_with_stdio(false);\n\n int t;\n cin >> t;\n\n while(t--){\n int m,n;\n cin >> m >> n;\n\n int cnt = 0;\n int mx = abs(max(abs(m),abs(n)));\n for(int p=-mx; p<=mx; p++){\n for(int q=-mx; q<=mx; q++){\n int d = p*p + q*q;\n if(d==0) continue;\n int d1 = p*m + q*n;\n int d2 = p*n - q*m;\n if(d1%d==0 && d2%d==0) {\n debug(p,q);\n cnt++;\n }\n }\n }\n debug(cnt);\n cout << (cnt==8?\"P\":\"C\") << endl;\n }\n}\n\n/*\n\n\n*/", "accuracy": 1, "time_ms": 40, "memory_kb": 3072, "score_of_the_acc": -0.0454, "final_rank": 2 }, { "submission_id": "aoj_1325_4886506", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\nvoid solve() {\n int p, q;\n cin >> p >> q;\n int cnt = 0;\n for (int m = -200; m <= 200; ++m) {\n for (int n = -200; n <= 200; ++n) {\n if (m == n && n == 0) continue;\n int x = m*m + n*n;\n int y = m*p + n*q;\n int z = m*q - n*p;\n if (y % x == 0 && z % x == 0) cnt++;\n }\n }\n cout << (cnt == 8 ? 'P' : 'C') << '\\n';\n}\n\nint main() {\n int t;\n cin >> t;\n while (t--) solve();\n}", "accuracy": 1, "time_ms": 80, "memory_kb": 3332, "score_of_the_acc": -0.1563, "final_rank": 12 }, { "submission_id": "aoj_1325_4873041", "code_snippet": "#pragma GCC optimize(\"Ofast\")\n#include <iostream> // cout, endl, cin\n#include <string> // string, to_string, stoi\n#include <vector> // vector\n#include <algorithm> // min, max, swap, sort, reverse, lower_bound, upper_bound\n#include <utility> // pair, make_pair\n#include <tuple> // tuple, make_tuple\n#include <cstdint> // int64_t, int*_t\n#include <cstdio> // printf\n#include <map> // map\n#include <queue> // queue, priority_queue\n#include <set> // set\n#include <stack> // stack\n#include <deque> // deque\n#include <unordered_map> // unordered_map\n#include <unordered_set> // unordered_set\n#include <bitset> // bitset\n#include <cctype> // isupper, islower, isdigit, toupper, tolower\n#include <iomanip> // setprecision\n#include <complex> // complex\n#include <math.h> \n#include <functional>\n#include <cassert>\nusing namespace std;\nusing ll = long long;\nusing P = pair<int,int>;\nconstexpr ll INF = 1e18;\nconstexpr int inf = 1e9;\nconstexpr double eps = 1e-9;\n// constexpr ll mod = 1000000007;\nconstexpr ll mod = 998244353;\nconst int dx[8] = {1, 0, -1, 0,1,1,-1,-1};\nconst int dy[8] = {0, 1, 0, -1,1,-1,1,-1};\ntemplate<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }\ntemplate<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }\n// --------------------------------------------------------------------------\n\n\n\n\nint main(){\n ios::sync_with_stdio(false);\n cin.tie(nullptr);\n int _;\n cin >> _;\n while(_--){\n int m,n;\n cin >> m >> n;\n int cnt = 0;\n for(int i=-1000; i<1000; i++){\n for(int j=-1000; j<1000; j++){\n // if(i*i+j*j > m*m+n*n) break;\n if(i*i+j*j < 1) continue;\n if((i*m+j*n)%(i*i+j*j)==0 && (i*n-j*m)%(i*i+j*j)==0) cnt++;\n }\n }\n if(cnt == 8){\n cout << \"P\\n\";\n }else{\n cout << \"C\\n\";\n }\n }\n return 0;\n}", "accuracy": 1, "time_ms": 1800, "memory_kb": 3356, "score_of_the_acc": -1.1253, "final_rank": 19 }, { "submission_id": "aoj_1325_4873015", "code_snippet": "#pragma GCC optimize(\"Ofast\")\n#include <iostream> // cout, endl, cin\n#include <string> // string, to_string, stoi\n#include <vector> // vector\n#include <algorithm> // min, max, swap, sort, reverse, lower_bound, upper_bound\n#include <utility> // pair, make_pair\n#include <tuple> // tuple, make_tuple\n#include <cstdint> // int64_t, int*_t\n#include <cstdio> // printf\n#include <map> // map\n#include <queue> // queue, priority_queue\n#include <set> // set\n#include <stack> // stack\n#include <deque> // deque\n#include <unordered_map> // unordered_map\n#include <unordered_set> // unordered_set\n#include <bitset> // bitset\n#include <cctype> // isupper, islower, isdigit, toupper, tolower\n#include <iomanip> // setprecision\n#include <complex> // complex\n#include <math.h> \n#include <functional>\n#include <cassert>\nusing namespace std;\nusing ll = long long;\nusing P = pair<int,int>;\nconstexpr ll INF = 1e18;\nconstexpr int inf = 1e9;\nconstexpr double eps = 1e-9;\n// constexpr ll mod = 1000000007;\nconstexpr ll mod = 998244353;\nconst int dx[8] = {1, 0, -1, 0,1,1,-1,-1};\nconst int dy[8] = {0, 1, 0, -1,1,-1,1,-1};\ntemplate<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }\ntemplate<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }\n// --------------------------------------------------------------------------\n\n\n\n\nint main(){\n ios::sync_with_stdio(false);\n cin.tie(nullptr);\n int _;\n cin >> _;\n while(_--){\n int m,n;\n cin >> m >> n;\n int cnt = 0;\n for(int i=-1000; i<1000; i++){\n for(int j=-1000; j<1000; j++){\n // if(i*i+j*j > m*m+n*n) break;\n if(i*i+j*j <= 1) continue;\n if((i*m+j*n)%(i*i+j*j)==0 && (i*n-j*m)%(i*i+j*j)==0) cnt++;\n }\n }\n if(cnt == 4){\n cout << \"P\\n\";\n }else{\n cout << \"C\\n\";\n }\n }\n return 0;\n}", "accuracy": 1, "time_ms": 1800, "memory_kb": 3380, "score_of_the_acc": -1.1335, "final_rank": 20 }, { "submission_id": "aoj_1325_4773070", "code_snippet": "#include <iostream>\n#include <string>\n#include <cstdlib>\n#include <cmath>\n#include <vector>\n#include <unordered_map>\n#include <map>\n#include <set>\n#include <algorithm>\n#include <queue>\n#include <stack>\n#include <functional>\n#include <bitset>\n#include <assert.h>\n#include <unordered_map>\n#include <fstream>\n#include <ctime>\n#include <complex>\nusing namespace std;\ntypedef long long ll;\ntypedef vector<ll> vl;\ntypedef vector<vl> vvl;\ntypedef vector<char> vc;\ntypedef vector<string> vs;\ntypedef vector<bool> vb;\ntypedef vector<double> vd;\ntypedef pair<ll,ll> P;\ntypedef pair<int,int> pii;\ntypedef vector<P> vpl;\ntypedef tuple<ll,ll,ll> tapu;\n#define rep(i,n) for(int i=0; i<(n); i++)\n#define REP(i,a,b) for(int i=(a); i<(b); i++)\n#define all(x) (x).begin(), (x).end()\n#define rall(x) (x).rbegin(), (x).rend()\nconst int inf = 1<<30;\nconst ll linf = 1LL<<62;\nconst int MAX = 510000;\nll dy[8] = {1,-1,0,0,1,-1,1,-1};\nll dx[8] = {0,0,1,-1,1,-1,-1,1};\nconst double pi = acos(-1);\nconst double eps = 1e-7;\ntemplate<typename T1,typename T2> inline bool chmin(T1 &a,T2 b){\n\tif(a>b){\n\t\ta = b; return true;\n\t}\n\telse return false;\n}\ntemplate<typename T1,typename T2> inline bool chmax(T1 &a,T2 b){\n\tif(a<b){\n\t\ta = b; return true;\n\t}\n\telse return false;\n}\ntemplate<typename T> inline void print(T &a){\n rep(i,a.size()) cout << a[i] << \" \";\n cout << \"\\n\";\n}\ntemplate<typename T1,typename T2> inline void print2(T1 a, T2 b){cout << a << \" \" << b << \"\\n\";}\ntemplate<typename T1,typename T2,typename T3> inline void print3(T1 a, T2 b, T3 c){\n\tcout << a << \" \" << b << \" \" << c << \"\\n\";\n}\nll pcount(ll x) {return __builtin_popcountll(x);}\nconst int mod = 1e9 + 7;\n//const int mod = 998244353;\n\nvoid solve(){\n\tint p,q; cin >> p >> q;\n\tint r = p*p + q*q;\n\tint cnt = 0;\n\tfor(int m=-150; m<=150; m++){\n\t\tfor(int n=-150; n<=150; n++){\n\t\t\tif(m==0 && n==0) continue;\n\t\t\tint s = m*m + n*n;\n\t\t\tif(r % s != 0) continue;\n\t\t\tint t = r / s;\n\t\t\tbool f = false;\n\t\t\tfor(int x=0; x<=150; x++){\n\t\t\t\tint Y = t - x*x;\n\t\t\t\tint y = sqrt(Y);\n\t\t\t\tif(y*y != Y) continue;\n\t\t\t\tf = true; break;\n\t\t\t}\n\t\t\tif((m*p + n*q) % s == 0 && (m*q - n*p) % s == 0) cnt++;\n\t\t}\n\t}\n\tif(cnt == 8) puts(\"P\");\n\telse puts(\"C\");\n}\n\nint main(){\n\tint t; cin >> t;\n\twhile(t--){\n\t\tsolve();\n\t}\n}", "accuracy": 1, "time_ms": 50, "memory_kb": 3092, "score_of_the_acc": -0.0578, "final_rank": 3 }, { "submission_id": "aoj_1325_4770199", "code_snippet": "#include \"iostream\"\n#include \"climits\"\n#include \"list\"\n#include \"queue\"\n#include \"stack\"\n#include \"set\"\n#include \"functional\"\n#include \"algorithm\"\n#include \"string\"\n#include \"map\"\n#include \"unordered_map\"\n#include \"unordered_set\"\n#include \"iomanip\"\n#include \"cmath\"\n#include \"random\"\n#include \"bitset\"\n#include \"cstdio\"\n#include \"numeric\"\n#include \"cassert\"\n#include \"ctime\"\n\nusing namespace std;\n\nconstexpr long long int MOD = 1000000007;\n//constexpr int MOD = 1000000007;\n//constexpr int MOD = 998244353;\n//constexpr long long int MOD = 998244353;\nconstexpr double EPS = 1e-12;\n\n//int N, M, K, T, H, W, L, R;\nlong long int N, M, K, T, H, W, L, R;\n\n\nint main() {\n\tios::sync_with_stdio(false);\n\tcin.tie(0);\n\n\tcin >> T;\n\twhile (T--) {\n\t\tcin >> N >> M;\n\t\tint ans = 0;\n\t\tfor (int i = -150; i <= 150; i++) {\n\t\t\tfor (int j = -150; j <= 150; j++) {\n\t\t\t\tif (!i && !j)continue;\n\t\t\t\tif ((i*N + j * M) % (i*i + j * j) == 0 && (i*M - j * N) % (i*i + j * j) == 0)ans++;\n\t\t\t}\n\t\t}\n\t\tif (ans == 8)cout << \"P\" << endl;\n\t\telse cout << \"C\" << endl;\n\t}\n}", "accuracy": 1, "time_ms": 130, "memory_kb": 3044, "score_of_the_acc": -0.0861, "final_rank": 4 }, { "submission_id": "aoj_1325_4738311", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nint main(){\n int T;\n cin >> T;\n for (int i = 0; i < T; i++){\n int m, n;\n cin >> m >> n;\n int s = m * m + n * n;\n bool prime = true;\n int a = sqrt(s - 1);\n for (int j = - a; j <= a; j++){\n int b = sqrt(s - j * j - 1);\n for (int k = - b; k <= b; k++){\n int t = j * j + k * k;\n if (abs(t) > 1){\n if ((j * m + k * n) % (j * j + k * k) == 0 && (j * n - k * m) % (j * j + k * k) == 0){\n prime = false;\n }\n }\n }\n }\n if (prime){\n cout << 'P' << endl;\n } else {\n cout << 'C' << endl;\n }\n }\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 2988, "score_of_the_acc": 0, "final_rank": 1 } ]
aoj_1327_cpp
Problem C: One-Dimensional Cellular Automaton There is a one-dimensional cellular automaton consisting of N cells. Cells are numbered from 0 to N − 1. Each cell has a state represented as a non-negative integer less than M . The states of cells evolve through discrete time steps. We denote the state of the i-th cell at time t as S ( i , t ). The state at time t + 1 is defined by the equation S ( i , t + 1) = ( A × S ( i − 1, t ) + B × S ( i , t ) + C × S ( i + 1, t )) mod M ,         (1) where A , B and C are non-negative integer constants. For i < 0 or N ≤ i , we define S ( i , t ) = 0. Given an automaton definition and initial states of cells, your mission is to write a program that computes the states of the cells at a specified time T . Input The input is a sequence of datasets. Each dataset is formatted as follows. N M A B C T S (0, 0) S (1, 0) ... S ( N − 1, 0) The first line of a dataset consists of six integers, namely N , M , A , B , C and T . N is the number of cells. M is the modulus in the equation (1). A , B and C are coefficients in the equation (1). Finally, T is the time for which you should compute the states. You may assume that 0 < N ≤ 50, 0 < M ≤ 1000, 0 ≤ A , B , C < M and 0 ≤ T ≤ 10 9 . The second line consists of N integers, each of which is non-negative and less than M . They represent the states of the cells at time zero. A line containing six zeros indicates the end of the input. Output For each dataset, output a line that contains the states of the cells at time T . The format of the output is as follows. S (0, T ) S (1, T ) ... S ( N − 1, T ) Each state must be represented as an integer and the integers must be separated by a space. Sample Input 5 4 1 3 2 0 0 1 2 0 1 5 7 1 3 2 1 0 1 2 0 1 5 13 1 3 2 11 0 1 2 0 1 5 5 2 0 1 100 0 1 2 0 1 6 6 0 2 3 1000 0 1 2 0 1 4 20 1000 0 2 3 1000000000 0 1 2 0 1 0 1 2 0 1 0 1 2 0 1 0 1 2 0 1 30 2 1 0 1 1000000000 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 30 2 1 1 1 1000000000 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 30 5 2 3 1 1000000000 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 Output for the Sample Input 0 1 2 0 1 2 0 0 4 3 2 12 10 9 11 3 0 4 2 1 0 4 2 0 4 4 0 376 752 0 376 0 376 752 0 376 0 376 752 0 376 0 376 752 0 376 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 3 2 2 2 3 3 1 4 3 1 2 3 0 4 3 3 0 4 2 2 2 2 1 1 2 1 3 0
[ { "submission_id": "aoj_1327_11043071", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\nusing a2 = array<ll, 2>;\nusing a3 = array<ll, 3>;\n\nbool chmin(auto& a, const auto& b) { return a > b ? a = b, 1 : 0; }\nbool chmax(auto& a, const auto& b) { return a < b ? a = b, 1 : 0; }\n\nll mod = 998244353;\nconst ll INF = 1e18;\n\nifstream in;\nofstream out;\n\nint main(int argc, char** argv) {\n ios::sync_with_stdio(false);\n cin.tie(0);\n\n if(argc > 2) {\n in.open(argv[1]);\n cin.rdbuf(in.rdbuf());\n out.open(argv[2]);\n cout.rdbuf(out.rdbuf());\n }\n\n while(1) {\n ll n, m, a, b, c, t;\n cin >> n >> m >> a >> b >> c >> t;\n if(n == 0) break;\n vector<ll> s(n);\n for(auto& x : s) cin >> x;\n\n vector<vector<vector<ll>>> mat(31, vector(n, vector<ll>(n, 0)));\n for(int i = 0; i < n; i++) {\n if(i) mat[0][i][i - 1] = a;\n mat[0][i][i] = b;\n if(i + 1 < n) mat[0][i][i + 1] = c;\n }\n\n auto mul = [&](vector<vector<ll>>& l, vector<vector<ll>>& r) {\n vector<vector<ll>> res(n, vector<ll>(n, 0));\n for(int i = 0; i < n; i++) {\n for(int j = 0; j < n; j++) {\n for(int k = 0; k < n; k++) {\n res[i][j] += l[i][k] * r[k][j];\n res[i][j] %= m;\n }\n }\n }\n return res;\n };\n\n for(int i = 1; i <= 30; i++) {\n mat[i] = mul(mat[i - 1], mat[i - 1]);\n }\n\n vector<vector<ll>> p(n, vector<ll>(n, 0));\n for(int i = 0; i < n; i++) {\n p[i][i] = 1;\n }\n\n for(int i = 0; i <= 30; i++) {\n if((1 << i) & t) {\n p = mul(p, mat[i]);\n }\n }\n\n for(int i = 0; i < n; i++) {\n ll ans = 0;\n for(int j = 0; j < n; j++) {\n ans += p[i][j] * s[j];\n ans %= m;\n }\n cout << ans << \" \\n\"[i == n - 1];\n }\n }\n\n return 0;\n}", "accuracy": 1, "time_ms": 500, "memory_kb": 4352, "score_of_the_acc": -0.8262, "final_rank": 17 }, { "submission_id": "aoj_1327_10885517", "code_snippet": "#include<bits/stdc++.h>\n// #include<atcoder/all>\n// #include<boost/multiprecision/cpp_int.hpp>\n\nusing namespace std;\n// using namespace atcoder;\n// using bint = boost::multiprecision::cpp_int;\nusing ll = long long;\nusing ull = unsigned long long;\nusing ld = long double;\nusing pii = pair<int,int>;\nusing pll = pair<ll,ll>;\nusing vi = vector<ll>;\nusing vvi = vector<vi>;\nusing vvvi = vector<vvi>;\nusing ve = vector<vector<int>>;\nusing vb = vector<bool>;\nusing vvb = vector<vb>;\n#define rep(i,s,n) for(ll i = (ll)s;i < (ll)n;i++)\n#define rrep(i,l,r) for(ll i = r-1;i >= l;i--)\n#define ALL(x) (x).begin(),(x).end()\n#define sz(c) ((ll)(c).size())\n#define LB(A,x) (int)(lower_bound(A.begin(),A.end(),x)-A.begin())\n#define UB(A,x) (int)(upper_bound(A.begin(),A.end(),x)-A.begin())\n// #define MOD 1000000007\n#define MOD 998244353\ntemplate<typename T>using min_priority_queue=priority_queue<T,vector<T>,greater<T>>;\ntemplate<typename T>ostream&operator<<(ostream&os,vector<T>&v){for(int i = 0;i < v.size();i++)os<<v[i]<<(i+1!=v.size()?\" \":\"\");return os;}\ntemplate<typename T>istream&operator>>(istream&is,vector<T>&v){for(T&in:v)is>>in;return is;}\ntemplate<typename T1,typename T2>ostream&operator<<(ostream&os,pair<T1,T2>&p){os<<p.first<<\" \"<<p.second;return os;}\ntemplate<typename T1,typename T2>istream&operator>>(istream&is,pair<T1,T2>&p){is>>p.first>>p.second;return is;}\ntemplate<typename T> inline bool chmax(T &a,T b){if(a < b){a = b;return true;}return false;}\ntemplate<typename T> inline bool chmin(T &a,T b){if(a > b){a = b;return true;}return false;}\nld dist(ld x1,ld y1,ld x2, ld y2){return sqrtl((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2));}\n\nvvi mul(vvi &a,vvi &b,int mod){\n assert(sz(a));assert(sz(b));\n assert(sz(a[0]) == sz(b));\n int n = sz(a),m = sz(b[0]);\n vvi res(n,vi(m));\n rep(i,0,n)rep(j,0,m)rep(k,0,sz(a[0])){\n (res[i][j] += a[i][k]*b[k][j]%mod)%= mod;\n }\n return res;\n}\n\nint main(){\n \n ios_base::sync_with_stdio(0), cin.tie(0);\n while(1){\n ll n,m,T;\n vi coef(3);\n cin >> n >> m >> coef >> T;\n if(!n)break;\n vvi a(n,vi(1));cin >> a;\n vvi mat(n,vi(n));\n rep(i,0,n)rep(j,-1,2){\n int ni = i + j;\n if(ni < 0 || ni >= n)continue;\n mat[ni][i] = coef[1-j];\n }\n while(T){\n if(T & 1)a = mul(mat,a,m);\n T >>= 1;\n mat = mul(mat,mat,m);\n }\n rep(i,0,n)cout << a[i][0] << \" \\n\"[i+1 == n];\n }\n \n\n return 0;\n}", "accuracy": 1, "time_ms": 1240, "memory_kb": 3480, "score_of_the_acc": -0.7008, "final_rank": 16 }, { "submission_id": "aoj_1327_10851061", "code_snippet": "#include<iostream>\n#include<stdio.h>\n#include<cstring>\nusing namespace std;\nconst int MAX = 55;\nint M;\n\n\nstruct Matrix\n{\n int v[MAX][MAX];\n};\n\nint n,k; //分别代表的是每个置换的长度\n//置换的一组的个数\n//以及一共置换的操作\n\nMatrix mtAdd(Matrix A, Matrix B) // 求矩阵 A + B\n{\n int i, j;\n Matrix C;\n for(i = 0; i < n; i ++)\n for(j = 0; j < n; j ++)\n C.v[i][j]=(A.v[i][j]+B.v[i][j])%M;\n return C;\n}\n\nMatrix mtMul(Matrix A, Matrix B) // 求矩阵 A * B\n{\n int i, j, k;\n Matrix C;\n for(i = 0; i < n; i ++)\n for(j = 0; j < n; j ++)\n {\n C.v[i][j] = 0;\n for(k = 0; k < n; k ++)\n C.v[i][j] = (A.v[i][k] * B.v[k][j] + C.v[i][j])%M;\n }\n return C;\n}\n\nMatrix mtPow(Matrix A, int k) // 求矩阵 A ^ k\n{\n if(k == 0)\n {\n memset(A.v, 0, sizeof(A.v));\n for(int i = 0; i < n; i ++)\n A.v[i][i] = 1;\n return A;\n }\n if(k == 1) return A;\n Matrix C = mtPow(A, k / 2);\n if(k % 2 == 0)\n return mtMul(C, C);\n else\n return mtMul(mtMul(C, C), A);\n}\n\nvoid out(Matrix A)\n{\n for(int i=0; i<n; i++)\n {\n for(int j=0; j<n; j++)\n cout<<A.v[i][j]<<\" \";\n cout<<endl;\n }\n cout<<endl;\n}\n\nint main ()\n{\n int A,B,C,T;\n Matrix NUM;\n while(~scanf(\"%d%d%d%d%d%d\",&n,&M,&A,&B,&C,&T)){\n if(n==0) break;\n for(int i=0;i<n;i++){\n scanf(\"%d\",&NUM.v[i][0]);\n }\n//out(NUM);\n Matrix yun;\n for(int i=0;i<n;i++){\n if(i-1>=0) yun.v[i][i-1]=A;\n yun.v[i][i]=B;\n if(i-1<n) yun.v[i][i+1]=C;\n }\n//out(yun);\n Matrix ans = mtPow(yun,T);\n//out(ans);\n NUM=mtMul(ans,NUM);\n//out(NUM);\n for(int i=0;i<n-1;i++) printf(\"%d \",NUM.v[i][0]);\n printf(\"%d\\n\",NUM.v[n-1][0]);\n }\n}", "accuracy": 1, "time_ms": 780, "memory_kb": 5048, "score_of_the_acc": -1.2816, "final_rank": 19 }, { "submission_id": "aoj_1327_10684193", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\nint n,m,p,a,b,c,T,w[55];\nstruct st{\n\tint v[55][55];\n}k[55],ans;\nint read(){\n\tint f=1,ans=0;\n\tchar x=getchar();\n\twhile(x<'0'||x>'9'){\n\t\tif(x=='-'){\n\t\t\tf=-1;\n\t\t}\n\t\tx=getchar();\n\t}\n\twhile(x>='0'&&x<='9'){\n\t\tans=ans*10+x-'0';\n\t\tx=getchar();\n\t}\n\treturn ans*f;\n}\nvoid write(int x){\n\tchar c[10005]={};\n\tint i=0;\n\tif(x==0){\n\t\tputchar('0');\n\t}\n\tif(x<0){\n\t\tputchar('-');\n\t\tx*=-1;\n\t}\n\twhile(x){\n\t\tc[++i]=x%10;\n\t\tx/=10;\n\t}\n\twhile(i){\n\t\tputchar(c[i]+'0');\n\t\ti--;\n\t}\n\tputchar(' ');\n}\nvoid gmnb(int x,int y,int z,st &u,int a[55][55],int b[55][55],int mod){\n\tint k1[55][55]={};\n\tfor(int i=1;i<=x;i++){\n\t\tfor(int j=1;j<=z;j++){\n\t\t\tfor(int l=1;l<=y;l++){\n\t\t\t\tk1[i][j]=k1[i][j]+a[i][l]*b[l][j];\n\t\t\t\tk1[i][j]%=mod;\n\t\t\t}\n\t\t}\n\t}\n\tfor(int i=1;i<=x;i++){\n\t\tfor(int j=1;j<=z;j++){\n\t\t\tu.v[i][j]=k1[i][j];\n\t\t}\n\t}\n}\nvoid lfnb(int x,int y,int mod){\n\tint cnt=1,r[55][55]={},q=1;\n\tbool f[55]={};\n\twhile(q<=x){\n\t\tq*=2;\n\t\tcnt++;\n\t\tgmnb(y,y,y,k[cnt],k[cnt-1].v,k[cnt-1].v,mod);\n\t}\n\tint cnt1=cnt;\n\twhile(cnt--){\n\t\tif(x>=q){\n\t\t\tx-=q;\n\t\t\tf[cnt+1]=1;\n\t\t}\n\t\tq/=2;\n\t}\n\tfor(int i=1;i<cnt1;i++){\n\t\tif(f[i]){\n\t\t\tgmnb(y,y,y,ans,ans.v,k[i].v,mod);\n\t\t}\n\t}\n}\nint main(){\n\twhile(1){\n\t\tn=read(),m=read(),a=read(),b=read(),c=read(),T=read();\n\t\tif(n==0&&m==0&&a==0&&b==0&&c==0&&T==0){\n\t\t\treturn 0;\n\t\t}\n\t\tfor(int i=1;i<=n;i++){\n\t\t\tfor(int j=1;j<=n;j++){\n\t\t\t\tif(i==j+1){\n\t\t\t\t\tk[1].v[i][j]=c;\n\t\t\t\t\tans.v[i][j]=0;\n\t\t\t\t}else if(i+1==j){\n\t\t\t\t\tk[1].v[i][j]=a;\n\t\t\t\t\tans.v[i][j]=0;\n\t\t\t\t}else if(i==j){\n\t\t\t\t\tk[1].v[i][j]=b;\n\t\t\t\t\tans.v[i][j]=1;\n\t\t\t\t}else{\n\t\t\t\t\tk[1].v[i][j]=0;\n\t\t\t\t\tans.v[i][j]=0;\n\t\t\t\t}\n\t\t\t}\n\t\t\tw[i]=read();\n\t\t}\n\t\tlfnb(T,n,m);\n\t\tfor(int i=1;i<=n;i++){\n\t\t\tint h=0;\n\t\t\tfor(int j=1;j<=n;j++){\n\t\t\t\th+=w[j]*ans.v[j][i];\n\t\t\t\th%=m;\n\t\t\t}\n if (i == n) {printf(\"%lld\", h % m); continue;}\n\t\t\twrite(h%m);\n\t\t}\n\t\tprintf(\"\\n\");\n\t}\n\treturn 0;\n}", "accuracy": 1, "time_ms": 800, "memory_kb": 3508, "score_of_the_acc": -0.5349, "final_rank": 11 }, { "submission_id": "aoj_1327_10684188", "code_snippet": "#include <iostream>\n#include <cstdio>\n#define ll long long\nusing namespace std;\n#define MAXN 60\n\nint n , m , a , b , c , t;\nstruct Matrix{\n\tll data[MAXN][MAXN];\n\tint heng , zong;\n\tvoid kind3(){\n\t\theng = zong = n;\n\t\tfor( int i = 1 ; i < MAXN ; i++ )\n\t\t\tfor( int j = 1 ; j < MAXN ; j++ )\n\t\t\t\tdata[i][j] = 0;\n\t}\n\tvoid kind4(){\n\t\tkind3();\n\t\tfor( int i = 1 ; i <= n ; i++ )\n\t\t\tdata[i][i] = 1;\n\t}\n\t\n\tvoid kind2(){\n\t\tkind3();\n\t\theng = zong = n;\n\t\tfor( int i = 1 ; i <= n ; i++ ){\n\t\t\tif(i>1) data[i][i-1] = a;\n\t\t\tdata[i][i] = b;\n\t\t\tif(i<n) data[i][i+1] = c;\n\t\t}\n\t}\n\tvoid print(){\n\t\tcout << \"[Martix] \" << heng << \" \" << zong << endl;\n\t\tfor( int i = 1 ; i <= zong ; i++ ){\n\t\t\tfor( int j = 1 ; j <= heng ; j++ )\n\t\t\t\tcout << data[i][j] << \" \";\n\t\t\tcout << endl;\n\t\t}\n\t}\n};\nMatrix get0(){\n\tMatrix m1;\n\tm1.kind3();\n\treturn m1;\n}\nMatrix get1(){\n\tMatrix m1;\n\tm1.kind4();\n\treturn m1;\n}\nMatrix operator*( Matrix m1 , Matrix m2 ){\n\tMatrix m3; m3.kind3();\n\tfor( int i = 1 ; i <= n ; i++ )\n\t\tfor( int j = 1 ; j <= m2.heng ; j++ ){\n\t\t\tm3.data[i][j] = 0;\n\t\t\tfor( int k = 1 ; k <= n ; k++ ){\n\t\t\t\tm3.data[i][j] += m1.data[i][k] * m2.data[k][j];\n\t\t\t\tm3.data[i][j] %= m;\n\t\t\t}\n\t\t}\n\treturn m3;\n}\nMatrix pow( Matrix a , int b ){\n\tMatrix ret; ret = get1();\n\tMatrix now; now = a;\n\twhile( b ){\n\t\tif( b & 1 ) ret = ret * now;\n\t\tnow = now * now;\n \tb = b >> 1;\n\t}\n\treturn ret;\n}\n/*\nBC\n ABC\n ABC\n ...\n AB\n*/\nint main(){\n\t//freopen(\"C.txt\",\"r\",stdin);\n\tbool fir = 1;\n\twhile( 1 ){\n\t\tscanf(\"%d %d %d %d %d %d\",&n,&m,&a,&b,&c,&t);\n\t\tif( !fir ){\n\t\t\tputchar('\\n');\n\t\t}else fir = 0;\n\t\tif( n == 0 ) return 0;\n\t\t//cout << n << \" \" << m << \" \" << a << \" \" << b << \" \" << c << \" \" << t << endl;\n\t\tMatrix A; A.kind2();\n\t\tMatrix B; B.kind3(); B.heng = 1; B.zong = n;\n\t\tMatrix C; C.kind3();\n\t\tfor( int i = 1 ; i <= n ; i++ ){\n\t\t\tscanf(\"%lld\",&B.data[i][1]);\n\t\t}\n\t\t//B.print();\n\t\t//Matrix AA = A;\n\t\t//A.print();\n\t\tA = pow(A,t);\n\t\t//A.print();\n\t\t//AA = AA*AA;\n\t\t//AA.print();\n\t\t//cout << \"[Main] \\\"^\\\" Done\" << endl;\n\t\tC = A*B;\n\t\tfor( int i = 1 ; i < n ; i++ )\n\t\t\tprintf(\"%lld \",C.data[i][1]%m);\n\t\tprintf(\"%lld\",C.data[n][1]%m);\n\t\t//return 0;\n\t}\n\treturn 0;\n}", "accuracy": 1, "time_ms": 1360, "memory_kb": 3932, "score_of_the_acc": -0.9713, "final_rank": 18 }, { "submission_id": "aoj_1327_10179895", "code_snippet": "#include <bits/stdc++.h>\n\n\nusing namespace std;\n//make -f ../makefile SRC=\n/*\n*/\n\n\n//------------------------------------------------------------------------------\nbool DEBUG = false;\nstatic int64_t MOD = 998244353L;\n\nconst int MAX_N = 50;\nstatic int64_t X0[MAX_N];\n//------------------------------------------------------------------------------\ntypedef vector<vector<int64_t> > MAT;\ntypedef vector<int64_t> VEC;\n\n//------------------------------------------------------------------------------\nclass Matrix\n{\n public:\n int N;\n MAT data;\n\n Matrix(int size): N(size)\n {\n data.resize(N);\n for (int i=0; i<N; ++i) data[i].resize(N);\n for (int i=0; i<N; i++) for (int j=0; j<N; j++) data[i][j] = 0;\n }\n\n Matrix(const MAT& values): N(values.size()), data(values)\n {\n \n }\n\n static Matrix identity(int size)\n {\n MAT values(size, vector<int64_t>(size, 0));\n for (int i=0; i<size; ++i) values[i][i] = 1L;\n return Matrix(values);\n }\n\n static Matrix zero(int size)\n {\n MAT values(size, vector<int64_t>(size, 0L));\n return Matrix(values);\n }\n\n Matrix operator*(const Matrix& M1) const\n {\n Matrix R(N);\n for (int i=0; i<N; i++)\n for (int j=0; j<N; j++)\n for (int k=0; k<N; k++)\n {\n R.data[i][j] += data[i][k] * M1.data[k][j];\n if (R.data[i][j] >= MOD) R.data[i][j] %= MOD;\n }\n return R;\n\n }\n\n VEC operator*(const VEC& X) const\n {\n VEC Y(N, 0); //<----------\n for (int i=0; i<N; i++)\n for (int j=0; j<N; j++)\n {\n Y[i] += data[i][j] * X[j];\n if (Y[i] >= MOD) Y[i] %= MOD;\n }\n return Y;\n }\n\n static\n Matrix power(Matrix& A, int64_t x)\n {\n int N = A.N;\n Matrix R = identity(N);\n //int iterations = 3;\n //while (x > 0 && iterations--)\n while (x > 0)\n {\n if (x%2 == 1) R = R * A;\n A = A * A;\n x /= 2;\n }\n\n\n\n return R;\n }\n\n\n void debug()\n {\n for (int i=0; i<N; i++)\n {\n for (int j=0; j<N; j++) printf(\"%ld \",data[i][j]); printf(\"\\n\");\n }\n printf(\"\\n\");\n }\n};\n\n//------------------------------------------------------------------------------\nvoid vec_debug(const VEC& v)\n{\n int N = v.size();\n for (int i=0; i<N; ++i)\n {\n printf(\"%ld\", v[i]);\n if (i < N-1) printf(\" \");\n }\n printf(\"\\n\");\n}\n\n//------------------------------------------------------------------------------\nvoid solve(int N, int64_t M, int64_t A, int64_t B, int64_t C, int64_t K)\n{\n MOD = M;\n\n VEC x(N);\n for (int i=0; i<N; ++i) x[i] = X0[i];\n if (K == 0)\n {\n vec_debug(x);\n return;\n }\n\n MAT data(N, VEC(N, 0));\n for (int m=0; m<N; ++m)\n {\n if (m-1 >= 0) data[m][m-1] = A;\n data[m][m] = B;\n if (m+1 < N) data[m][m+1] = C;\n }\n Matrix Mat1(data);\n Matrix Mat2 = Matrix::power(Mat1, K);\n VEC y = Mat2*x;\n vec_debug(y);\n}\n//------------------------------------------------------------------------------\nvoid test()\n{\n}\n//------------------------------------------------------------------------------\nint main()\n{\n //DEBUG = true;\n //test(); return 0;\n //--------------------------------------------------------------------------\n int N, num;\n int64_t M, A, B, C, K;\n while (true)\n {\n num = scanf(\"%d %ld %ld %ld %ld %ld \", &N, &M, &A, &B, &C, &K);\n if (N == 0 && M == 0 && A == 0 && B == 0 && C == 0 && K == 0) break;\n\n for (int i=0; i<N; ++i)\n {\n num = scanf(\"%ld \", &X0[i]);\n //if (X0[i] >= MOD) X0[i] %= MOD;\n }\n\n solve(N, M, A, B, C, K);\n }\n //--------------------------------------------------------------------------\n return 0;\n}\n//------------------------------------------------------------------------------", "accuracy": 1, "time_ms": 960, "memory_kb": 3480, "score_of_the_acc": -0.5865, "final_rank": 14 }, { "submission_id": "aoj_1327_9597887", "code_snippet": "#include <bits/stdc++.h>\n\nusing namespace std;\n\n#define rep(i, a, b) for (int i = (int)(a); i < (int)(b); i++)\n#define rrep(i, a, b) for (int i = (int)(b)-1; i >= (int)(a); i--)\n#define ALL(v) (v).begin(), (v).end()\n#define UNIQUE(v) sort(ALL(v)), (v).erase(unique(ALL(v)), (v).end())\n#define SZ(v) (int)v.size()\n#define MIN(v) *min_element(ALL(v))\n#define MAX(v) *max_element(ALL(v))\n#define LB(v, x) int(lower_bound(ALL(v), (x)) - (v).begin())\n#define UB(v, x) int(upper_bound(ALL(v), (x)) - (v).begin())\n\nusing uint = unsigned int;\nusing ll = long long int;\nusing ull = unsigned long long;\nusing i128 = __int128_t;\nusing u128 = __uint128_t;\nconst int inf = 0x3fffffff;\nconst ll INF = 0x1fffffffffffffff;\n\ntemplate <typename T> inline bool chmax(T &a, T b) {\n if (a < b) {\n a = b;\n return 1;\n }\n return 0;\n}\ntemplate <typename T> inline bool chmin(T &a, T b) {\n if (a > b) {\n a = b;\n return 1;\n }\n return 0;\n}\ntemplate <typename T, typename U> T ceil(T x, U y) {\n assert(y != 0);\n if (y < 0)\n x = -x, y = -y;\n return (x > 0 ? (x + y - 1) / y : x / y);\n}\ntemplate <typename T, typename U> T floor(T x, U y) {\n assert(y != 0);\n if (y < 0)\n x = -x, y = -y;\n return (x > 0 ? x / y : (x - y + 1) / y);\n}\ntemplate <typename T> int popcnt(T x) {\n return __builtin_popcountll(x);\n}\ntemplate <typename T> int topbit(T x) {\n return (x == 0 ? -1 : 63 - __builtin_clzll(x));\n}\ntemplate <typename T> int lowbit(T x) {\n return (x == 0 ? -1 : __builtin_ctzll(x));\n}\n\ntemplate <class T, class U>\nostream &operator<<(ostream &os, const pair<T, U> &p) {\n os << \"P(\" << p.first << \", \" << p.second << \")\";\n return os;\n}\ntemplate <typename T> ostream &operator<<(ostream &os, const vector<T> &vec) {\n os << \"{\";\n for (int i = 0; i < vec.size(); i++) {\n os << vec[i] << (i + 1 == vec.size() ? \"\" : \", \");\n }\n os << \"}\";\n return os;\n}\ntemplate <typename T, typename U>\nostream &operator<<(ostream &os, const map<T, U> &map_var) {\n os << \"{\";\n for (auto itr = map_var.begin(); itr != map_var.end(); itr++) {\n os << \"(\" << itr->first << \", \" << itr->second << \")\";\n itr++;\n if (itr != map_var.end())\n os << \", \";\n itr--;\n }\n os << \"}\";\n return os;\n}\ntemplate <typename T> ostream &operator<<(ostream &os, const set<T> &set_var) {\n os << \"{\";\n for (auto itr = set_var.begin(); itr != set_var.end(); itr++) {\n os << *itr;\n ++itr;\n if (itr != set_var.end())\n os << \", \";\n itr--;\n }\n os << \"}\";\n return os;\n}\n#ifdef LOCAL\n#define show(...) _show(0, #__VA_ARGS__, __VA_ARGS__)\n#else\n#define show(...) true\n#endif\ntemplate <typename T> void _show(int i, T name) {\n cerr << '\\n';\n}\ntemplate <typename T1, typename T2, typename... T3>\nvoid _show(int i, const T1 &a, const T2 &b, const T3 &...c) {\n for (; a[i] != ',' && a[i] != '\\0'; i++)\n cerr << a[i];\n cerr << \":\" << b << \" \";\n _show(i + 1, a, c...);\n}\n\n#include <unistd.h>\n\nnamespace fastio {\nstatic constexpr uint32_t SZ = 1 << 17;\nchar ibuf[SZ];\nchar obuf[SZ];\nchar out[100];\n// pointer of ibuf, obuf\n\n\nuint32_t pil = 0, pir = 0, por = 0;\n\nstruct Pre {\n char num[10000][4];\n constexpr Pre() : num() {\n for (int i = 0; i < 10000; i++) {\n int n = i;\n for (int j = 3; j >= 0; j--) {\n num[i][j] = n % 10 | '0';\n n /= 10;\n }\n }\n }\n} constexpr pre;\n\ninline void load() {\n memmove(ibuf, ibuf + pil, pir - pil);\n pir = pir - pil + fread(ibuf + pir - pil, 1, SZ - pir + pil, stdin);\n pil = 0;\n if (pir < SZ)\n ibuf[pir++] = '\\n';\n}\n\ninline void flush() {\n fwrite(obuf, 1, por, stdout);\n por = 0;\n}\n\nvoid rd(char &c) {\n do {\n if (pil + 1 > pir)\n load();\n c = ibuf[pil++];\n } while (isspace(c));\n}\n\nvoid rd(string &x) {\n x.clear();\n char c;\n do {\n if (pil + 1 > pir)\n load();\n c = ibuf[pil++];\n } while (isspace(c));\n do {\n x += c;\n if (pil == pir)\n load();\n c = ibuf[pil++];\n } while (!isspace(c));\n}\n\ntemplate <typename T> void rd_real(T &x) {\n string s;\n rd(s);\n x = stod(s);\n}\n\ntemplate <typename T> void rd_integer(T &x) {\n if (pil + 100 > pir)\n load();\n char c;\n do\n c = ibuf[pil++];\n while (c < '-');\n bool minus = 0;\n if constexpr (is_signed<T>::value || is_same_v<T, i128>) {\n if (c == '-') {\n minus = 1, c = ibuf[pil++];\n }\n }\n x = 0;\n while ('0' <= c) {\n x = x * 10 + (c & 15), c = ibuf[pil++];\n }\n if constexpr (is_signed<T>::value || is_same_v<T, i128>) {\n if (minus)\n x = -x;\n }\n}\n\nvoid rd(int &x) {\n rd_integer(x);\n}\nvoid rd(ll &x) {\n rd_integer(x);\n}\nvoid rd(i128 &x) {\n rd_integer(x);\n}\nvoid rd(uint &x) {\n rd_integer(x);\n}\nvoid rd(ull &x) {\n rd_integer(x);\n}\nvoid rd(u128 &x) {\n rd_integer(x);\n}\nvoid rd(double &x) {\n rd_real(x);\n}\nvoid rd(long double &x) {\n rd_real(x);\n}\n\ntemplate <class T, class U> void rd(pair<T, U> &p) {\n return rd(p.first), rd(p.second);\n}\ntemplate <size_t N = 0, typename T> void rd_tuple(T &t) {\n if constexpr (N < std::tuple_size<T>::value) {\n auto &x = std::get<N>(t);\n rd(x);\n rd_tuple<N + 1>(t);\n }\n}\ntemplate <class... T> void rd(tuple<T...> &tpl) {\n rd_tuple(tpl);\n}\n\ntemplate <size_t N = 0, typename T> void rd(array<T, N> &x) {\n for (auto &d : x)\n rd(d);\n}\ntemplate <class T> void rd(vector<T> &x) {\n for (auto &d : x)\n rd(d);\n}\n\nvoid read() {}\ntemplate <class H, class... T> void read(H &h, T &...t) {\n rd(h), read(t...);\n}\n\nvoid wt(const char c) {\n if (por == SZ)\n flush();\n obuf[por++] = c;\n}\nvoid wt(const string s) {\n for (char c : s)\n wt(c);\n}\nvoid wt(const char *s) {\n size_t len = strlen(s);\n for (size_t i = 0; i < len; i++)\n wt(s[i]);\n}\n\ntemplate <typename T> void wt_integer(T x) {\n if (por > SZ - 100)\n flush();\n if (x < 0) {\n obuf[por++] = '-', x = -x;\n }\n int outi;\n for (outi = 96; x >= 10000; outi -= 4) {\n memcpy(out + outi, pre.num[x % 10000], 4);\n x /= 10000;\n }\n if (x >= 1000) {\n memcpy(obuf + por, pre.num[x], 4);\n por += 4;\n } else if (x >= 100) {\n memcpy(obuf + por, pre.num[x] + 1, 3);\n por += 3;\n } else if (x >= 10) {\n int q = (x * 103) >> 10;\n obuf[por] = q | '0';\n obuf[por + 1] = (x - q * 10) | '0';\n por += 2;\n } else\n obuf[por++] = x | '0';\n memcpy(obuf + por, out + outi + 4, 96 - outi);\n por += 96 - outi;\n}\n\ntemplate <typename T> void wt_real(T x) {\n ostringstream oss;\n oss << fixed << setprecision(15) << double(x);\n string s = oss.str();\n wt(s);\n}\n\nvoid wt(int x) {\n wt_integer(x);\n}\nvoid wt(ll x) {\n wt_integer(x);\n}\nvoid wt(i128 x) {\n wt_integer(x);\n}\nvoid wt(uint x) {\n wt_integer(x);\n}\nvoid wt(ull x) {\n wt_integer(x);\n}\nvoid wt(u128 x) {\n wt_integer(x);\n}\nvoid wt(double x) {\n wt_real(x);\n}\nvoid wt(long double x) {\n wt_real(x);\n}\n\ntemplate <class T, class U> void wt(const pair<T, U> val) {\n wt(val.first);\n wt(' ');\n wt(val.second);\n}\ntemplate <size_t N = 0, typename T> void wt_tuple(const T t) {\n if constexpr (N < std::tuple_size<T>::value) {\n if constexpr (N > 0) {\n wt(' ');\n }\n const auto x = std::get<N>(t);\n wt(x);\n wt_tuple<N + 1>(t);\n }\n}\ntemplate <class... T> void wt(tuple<T...> tpl) {\n wt_tuple(tpl);\n}\ntemplate <class T, size_t S> void wt(const array<T, S> val) {\n auto n = val.size();\n for (size_t i = 0; i < n; i++) {\n if (i)\n wt(' ');\n wt(val[i]);\n }\n}\ntemplate <class T> void wt(const vector<T> val) {\n auto n = val.size();\n for (size_t i = 0; i < n; i++) {\n if (i)\n wt(' ');\n wt(val[i]);\n }\n}\n\nvoid print() {\n wt('\\n');\n}\ntemplate <class Head, class... Tail> void print(Head &&head, Tail &&...tail) {\n wt(head);\n if (sizeof...(Tail))\n wt(' ');\n print(forward<Tail>(tail)...);\n}\nvoid __attribute__((destructor)) _d() {\n flush();\n}\n} // namespace fastio\n\n\nusing fastio::flush;\nusing fastio::print;\nusing fastio::read;\n\ninline void first(bool i = true) {\n print(i ? \"first\" : \"second\");\n}\ninline void Alice(bool i = true) {\n print(i ? \"Alice\" : \"Bob\");\n}\ninline void Takahashi(bool i = true) {\n print(i ? \"Takahashi\" : \"Aoki\");\n}\ninline void yes(bool i = true) {\n print(i ? \"yes\" : \"no\");\n}\ninline void Yes(bool i = true) {\n print(i ? \"Yes\" : \"No\");\n}\ninline void No() {\n print(\"No\");\n}\ninline void YES(bool i = true) {\n print(i ? \"YES\" : \"NO\");\n}\ninline void NO() {\n print(\"NO\");\n}\ninline void Yay(bool i = true) {\n print(i ? \"Yay!\" : \":(\");\n}\ninline void Possible(bool i = true) {\n print(i ? \"Possible\" : \"Impossible\");\n}\ninline void POSSIBLE(bool i = true) {\n print(i ? \"POSSIBLE\" : \"IMPOSSIBLE\");\n}\n\n/**\n * @brief Fast IO\n */\n\nll N, M, A, B, C, T;\n\nvector<vector<ll>> Mul(vector<vector<ll>>& X, vector<vector<ll>>& Y) {\n int n = X.size(), m = X[0].size(), l = Y[0].size();\n vector<vector<ll>> Z(n,vector<ll>(l,0));\n rep(i,0,n) {\n rep(j,0,m) {\n rep(k,0,l) {\n Z[i][k] += X[i][j] * Y[j][k];\n Z[i][k] %= M;\n }\n }\n }\n return Z;\n}\n\nvector<vector<ll>> Pow(vector<vector<ll>>& X, ll K) {\n int n = X.size();\n if (K == 0) {\n vector<vector<ll>> Z(n,vector<ll>(n,0));\n rep(i,0,n) Z[i][i] = 1 % M;\n return Z;\n }\n if (K % 2 == 0) {\n vector<vector<ll>> Z = Pow(X,K/2);\n return Mul(Z,Z);\n }\n vector<vector<ll>> Z = Pow(X,K-1);\n return Mul(Z,X);\n}\n\nint main() {\nwhile(1) {\n cin >> N >> M >> A >> B >> C >> T;\n if (N == 0) return 0;\n vector<vector<ll>> F(N,vector<ll>(N,0)), G(N,vector<ll>(1));\n rep(i,0,N) cin >> G[i][0];\n rep(i,0,N) {\n rep(j,-1,2) {\n if (0 <= i+j && i+j < N) {\n if (j==-1) F[i][i+j] = A;\n if (j==0) F[i][i+j] = B;\n if (j==1) F[i][i+j] = C;\n }\n }\n }\n F = Pow(F,T);\n G = Mul(F,G);\n rep(i,0,N) cout << G[i][0] << (i==N-1 ? '\\n' : ' ');\n}\n}", "accuracy": 1, "time_ms": 880, "memory_kb": 3380, "score_of_the_acc": -0.5048, "final_rank": 9 }, { "submission_id": "aoj_1327_9532418", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\nusing P = pair<ll, ll>;\n#define rep(i, a, b) for(ll i = a; i < b; ++i)\n#define rrep(i, a, b) for(ll i = a; i >= b; --i)\nconstexpr ll inf = 4e18;\nstruct SetupIO {\n SetupIO() {\n ios::sync_with_stdio(0);\n cin.tie(0);\n cout << fixed << setprecision(30);\n }\n} setup_io;\nstruct Barrett {\n explicit Barrett(unsigned int m)\n : _m(m), im((unsigned long long)(-1) / m + 1) {}\n inline unsigned int umod() const {\n return _m;\n }\n inline unsigned int mul(unsigned int a, unsigned int b) const {\n unsigned long long z = a;\n z *= b;\n unsigned long long x = (unsigned long long)(((unsigned __int128)(z)*im) >> 64);\n unsigned int v = (unsigned int)(z - x * _m);\n if(_m <= v) v += _m;\n return v;\n }\n\n private:\n unsigned int _m;\n unsigned long long im;\n};\ntemplate <int id>\nstruct DynamicModint {\n using mint = DynamicModint;\n static int mod() {\n return (int)bt.umod();\n }\n static void set_mod(int m) {\n assert(1 <= m);\n bt = Barrett(m);\n }\n static mint raw(int v) {\n mint a;\n a._v = v;\n return a;\n }\n DynamicModint()\n : _v(0) {}\n template <class T>\n DynamicModint(const T& v) {\n static_assert(is_integral_v<T>);\n if(is_signed_v<T>) {\n long long x = (long long)(v % (long long)(umod()));\n if(x < 0) x += umod();\n _v = (unsigned int)(x);\n } else _v = (unsigned int)(v % umod());\n }\n unsigned int val() const {\n return _v;\n }\n mint& operator++() {\n ++_v;\n if(_v == umod()) _v = 0;\n return *this;\n }\n mint& operator--() {\n if(_v == 0) _v = umod();\n --_v;\n return *this;\n }\n mint operator++(int) {\n mint res = *this;\n ++*this;\n return res;\n }\n mint operator--(int) {\n mint res = *this;\n --*this;\n return res;\n }\n mint& operator+=(const mint& rhs) {\n _v += rhs._v;\n if(_v >= umod()) _v -= umod();\n return *this;\n }\n mint& operator-=(const mint& rhs) {\n _v += mod() - rhs._v;\n if(_v >= umod()) _v -= umod();\n return *this;\n }\n mint& operator*=(const mint& rhs) {\n _v = bt.mul(_v, rhs._v);\n return *this;\n }\n mint& operator/=(const mint& rhs) {\n return *this *= rhs.inv();\n }\n mint operator+() const {\n return *this;\n }\n mint operator-() const {\n return mint() - *this;\n }\n mint pow(long long n) const {\n assert(0 <= n);\n if(n == 0) return 1;\n mint x = *this, r = 1;\n while(1) {\n if(n & 1) r *= x;\n n >>= 1;\n if(n == 0) return r;\n x *= x;\n }\n }\n mint inv() const {\n auto eg = inv_gcd(_v, mod());\n assert(eg.first == 1);\n return eg.second;\n }\n friend mint operator+(const mint& lhs, const mint& rhs) {\n return mint(lhs) += rhs;\n }\n friend mint operator-(const mint& lhs, const mint& rhs) {\n return mint(lhs) -= rhs;\n }\n friend mint operator*(const mint& lhs, const mint& rhs) {\n return mint(lhs) *= rhs;\n }\n friend mint operator/(const mint& lhs, const mint& rhs) {\n return mint(lhs) /= rhs;\n }\n friend bool operator==(const mint& lhs, const mint& rhs) {\n return lhs._v == rhs._v;\n }\n friend bool operator!=(const mint& lhs, const mint& rhs) {\n return lhs._v != rhs._v;\n }\n friend istream& operator>>(istream& in, mint& x) {\n long long a;\n in >> a;\n x = a;\n return in;\n }\n friend ostream& operator<<(ostream& out, const mint& x) {\n return out << x.val();\n }\n\n private:\n unsigned int _v = 0;\n static Barrett bt;\n inline static unsigned int umod() {\n return bt.umod();\n }\n inline static pair<long long, long long> inv_gcd(long long a, long long b) {\n if(a == 0) return {b, 0};\n long long s = b, t = a, m0 = 0, m1 = 1;\n while(t) {\n const long long u = s / t;\n s -= t * u;\n m0 -= m1 * u;\n swap(s, t);\n swap(m0, m1);\n }\n if(m0 < 0) m0 += b / s;\n return {s, m0};\n }\n};\ntemplate <int id>\nBarrett DynamicModint<id>::bt(998244353);\nusing modint = DynamicModint<-1>;\nusing mint = modint;\ntemplate <typename T>\nstruct Matrix {\n Matrix(int h, int w, T val = 0)\n : h(h), w(w), A(h, vector<T>(w, val)) {}\n int H() const {\n return h;\n }\n int W() const {\n return w;\n }\n const vector<T>& operator[](int i) const {\n assert(0 <= i and i < h);\n return A[i];\n }\n vector<T>& operator[](int i) {\n assert(0 <= i and i < h);\n return A[i];\n }\n static Matrix I(int n) {\n Matrix mat(n, n);\n for(int i = 0; i < n; ++i) mat[i][i] = 1;\n return mat;\n }\n Matrix& operator+=(const Matrix& B) {\n assert(h == B.h and w == B.w);\n for(int i = 0; i < h; ++i) {\n for(int j = 0; j < w; ++j) {\n (*this)[i][j] += B[i][j];\n }\n }\n return (*this);\n }\n Matrix& operator-=(const Matrix& B) {\n assert(h == B.h and w == B.w);\n for(int i = 0; i < h; ++i) {\n for(int j = 0; j < w; ++j) {\n (*this)[i][j] -= B[i][j];\n }\n }\n return (*this);\n }\n Matrix& operator*=(const Matrix& B) {\n assert(w == B.h);\n vector<vector<T>> C(h, vector<T>(B.w, 0));\n for(int i = 0; i < h; ++i) {\n for(int k = 0; k < w; ++k) {\n for(int j = 0; j < B.w; ++j) {\n C[i][j] += (*this)[i][k] * B[k][j];\n }\n }\n }\n A.swap(C);\n return (*this);\n }\n Matrix& pow(long long t) {\n assert(h == w);\n assert(t >= 0);\n Matrix B = Matrix::I(h);\n while(t > 0) {\n if(t & 1ll) B *= (*this);\n (*this) *= (*this);\n t >>= 1ll;\n }\n A.swap(B.A);\n return (*this);\n }\n Matrix operator+(const Matrix& B) const {\n return (Matrix(*this) += B);\n }\n Matrix operator-(const Matrix& B) const {\n return (Matrix(*this) -= B);\n }\n Matrix operator*(const Matrix& B) const {\n return (Matrix(*this) *= B);\n }\n bool operator==(const Matrix& B) const {\n assert(h == B.H() and w == B.W());\n for(int i = 0; i < h; ++i) {\n for(int j = 0; j < w; ++j) {\n if(A[i][j] != B[i][j]) return false;\n }\n }\n return true;\n }\n bool operator!=(const Matrix& B) const {\n assert(h == B.H() and w == B.W());\n for(int i = 0; i < h; ++i) {\n for(int j = 0; j < w; ++j) {\n if(A[i][j] != B[i][j]) return true;\n }\n }\n return false;\n }\n\n private:\n int h, w;\n vector<vector<T>> A;\n};\nint main(void) {\n while(true) {\n ll n, m, a, b, c, t;\n cin >> n >> m >> a >> b >> c >> t;\n if(n == 0) break;\n mint::set_mod(m);\n Matrix<mint> vec(n, 1);\n rep(i, 0, n) {\n cin >> vec[i][0];\n }\n Matrix<mint> mat(n, n);\n rep(i, 0, n) {\n if(i > 0) mat[i][i - 1] = a;\n mat[i][i] = b;\n if(i < n - 1) mat[i][i + 1] = c;\n }\n vec = mat.pow(t) * vec;\n rep(i, 0, n) {\n cout << vec[i][0] << \" \\n\"[i + 1 == n];\n }\n }\n}", "accuracy": 1, "time_ms": 240, "memory_kb": 3608, "score_of_the_acc": -0.3553, "final_rank": 6 }, { "submission_id": "aoj_1327_9377609", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\n#define ll long long\n#define pb push_back\n#define mk make_pair\n#define S second\n#define F first\nint M;\nvector<vector<int>> mul(vector<vector<int>> a, vector<vector<int>> b){\n\t int n = a.size();\n\t int m = b[0].size();\n\t vector<vector<int>> ans(n, vector<int>(m, 0));\n\t for(int i = 0; i<n; i++){\n\t \t for(int j = 0; j<m; j++){\n\t \t \t int sum = 0;\n\t \t \t for(int k = 0; k<a[0].size(); k++){\n\t \t \t \t sum += a[i][k]*b[k][j]%M;\n\t \t \t \t sum %= M;\n\t\t\t\t\t\t }\n\t\t\t\t\t\t ans[i][j] = sum;\n\t\t\t\t }\n\t\t }\n\t\t return ans;\n}\nvector<vector<int>> exp(vector<vector<int>> a, int n, vector<vector<int>> unit){\n\t auto ans = unit;\n\t auto x = a;\n\t if(n == 0) return unit;\n\t while(n > 0){\n\t \t if(n %2 == 1) ans = mul(x, ans);\n\t \t x = mul(x, x);\n\t \t n/=2;\n\t }\n\t return ans;\n}\nvoid solve(int n, int m, int a, int b, int c, int t){\n\t\n\tM = m;\n\tint arr[n+5];\n\tfor(int i = 0; i<n; i++) cin >> arr[i];\n\tvector<vector<int>> mt(n, vector<int>(n, 0));\n for(int i = 0; i<n; i++){\n \t mt[i][i] = b;\n \t if(i!=0) mt[i][i-1] = a;\n \t if(i!=n-1) mt[i][i+1] = c;\n\t}\n\tvector<vector<int>> unit(n, vector<int>(n, 0));\n\tfor(int i = 0; i<n; i++) unit[i][i] = 1;\n\tvector<vector<int>> init(n, vector<int>(1, 0));\n\tfor(int i = 0; i<n; i++) init[i][0] = arr[i];\n\tauto big = exp(mt, t, unit);\n\tauto ans = mul(big, init);\n\tfor(int i = 0; i<n; i++) {\n\t\tif(i < n-1) cout << ans[i][0] << \" \";\n\t\telse cout << ans[i][0];\n\t}\n\t\n}\nint main(){\n\t\n while(true){\n \t int n, m, a, b, c, t;\n\tcin >> n >> m >> a >> b >> c >> t;\n\t if(n == 0 && m == 0 && a == 0 && b == 0 && c == 0 && t == 0) break;\n\t solve(n, m, a, b, c, t);\n\t cout << '\\n';\n\t } \n\t\n}", "accuracy": 1, "time_ms": 850, "memory_kb": 3668, "score_of_the_acc": -0.6337, "final_rank": 15 }, { "submission_id": "aoj_1327_9329181", "code_snippet": "#include <algorithm>\n#include <cmath>\n#include <cstdlib>\n#include <iomanip>\n#include <iostream>\n#include <list>\n#include <map>\n#include <numeric>\n#include <queue>\n#include <set>\n#include <stack>\n#include <string>\n#include <unordered_set>\n#include <vector>\n\nusing namespace std;\nusing ll = long long;\n\nvector<vector<ll>> operator*(const vector<vector<ll>>& left, const vector<vector<ll>>& right) {\n\tint ln = left.size(), lm = left[0].size(), rm = right[0].size();\n\tvector<vector<ll>> result(ln, vector<ll>(rm, 0ll));\n\n\tfor (int i = 0; i < ln; i++) {\n\t\tfor (int j = 0; j < rm; j++) {\n\t\t\tfor (int k = 0; k < lm; k++) {\n\t\t\t\tresult[i][j] += left[i][k] * right[k][j];\n\t\t\t}\n\t\t}\n\t}\n\n\treturn result;\n}\n\nvector<vector<ll>> operator%(const vector<vector<ll>>& mat, int M) {\n\tvector<vector<ll>> result(mat.size(), vector<ll>(mat[0].size(), 0ll));\n\n\tfor (int i = 0; i < mat.size(); i++) {\n\t\tfor (int j = 0; j < mat[0].size(); j++) {\n\t\t\tresult[i][j] = mat[i][j] % M;\n\t\t}\n\t}\n\n\treturn result;\n}\n\nvector<vector<ll>> MatPow(const vector<vector<ll>>& mat, int T, int M) {\n\tif (T == 0) {\n\t\tvector<vector<ll>> id(mat.size(), vector<ll>(mat[0].size(), 0ll));\n\t\tfor (int k = 0; k < id.size(); k++) id[k][k] = 1;\n\t\treturn id;\n\t}\n\n\tvector<vector<ll>> mat_half = MatPow(mat, T / 2, M);\n\n\tif (T % 2 == 0) {\n\t\treturn (mat_half * mat_half) % M;\n\t} else {\n\t\treturn (mat_half * mat_half * mat) % M;\n\t}\n}\n\nint main(void) {\n\twhile (true) {\n\t\tint N, M, A, B, C, T;\n\t\tcin >> N >> M >> A >> B >> C >> T;\n\t\tif (N == 0) break;\n\t\tvector<vector<ll>> S(N, vector<ll>(1, 0ll));\n\t\tfor (int i = 0; i < N; i++) cin >> S[i][0];\n\n\t\tvector<vector<ll>> mat(N, vector<ll>(N, 0ll));\n\t\tfor (int i = 0; i < N; i++) {\n\t\t\tfor (int j = 0; j < N; j++) {\n\t\t\t\tif (i == j + 1) mat[i][j] = A;\n\t\t\t\telse if (i == j) mat[i][j] = B;\n\t\t\t\telse if (i == j - 1) mat[i][j] = C;\n\t\t\t}\n\t\t}\n\t\tmat = MatPow(mat, T, M);\n\n\t\tS = (mat * S) % M;\n\n\t\tfor (int i = 0; i < N; i++) cout << S[i][0] << \" \\n\"[i == N - 1] << flush;\n\t}\n\n\treturn 0;\n}\n\n/*\n\t\t\t [ B C 0 .. 0 ]\n\t\t\t | A B C .. 0 |\n\tS(t + 1) = | 0 A B .. 0 | * S(t)\n\t\t\t | : : : :: : |\n\t\t\t [ 0 0 0 .. B ]\n*/", "accuracy": 1, "time_ms": 100, "memory_kb": 3488, "score_of_the_acc": -0.2394, "final_rank": 2 }, { "submission_id": "aoj_1327_9329172", "code_snippet": "#include <algorithm>\n#include <cmath>\n#include <cstdlib>\n#include <iomanip>\n#include <iostream>\n#include <list>\n#include <map>\n#include <numeric>\n#include <queue>\n#include <set>\n#include <stack>\n#include <string>\n#include <unordered_set>\n#include <vector>\n\nusing namespace std;\nusing ll = long long;\n\nvector<vector<ll>> operator*(const vector<vector<ll>>& left, const vector<vector<ll>>& right) {\n\tint ln = left.size(), lm = left[0].size(), rm = right[0].size();\n\tvector<vector<ll>> result(ln, vector<ll>(rm, 0ll));\n\n\tfor (int i = 0; i < ln; i++) {\n\t\tfor (int j = 0; j < rm; j++) {\n\t\t\tfor (int k = 0; k < lm; k++) {\n\t\t\t\tresult[i][j] += left[i][k] * right[k][j];\n\t\t\t}\n\t\t}\n\t}\n\n\treturn result;\n}\n\nvector<vector<ll>> operator%(const vector<vector<ll>>& mat, int M) {\n\tvector<vector<ll>> result(mat.size(), vector<ll>(mat[0].size(), 0ll));\n\n\tfor (int i = 0; i < mat.size(); i++) {\n\t\tfor (int j = 0; j < mat[0].size(); j++) {\n\t\t\tresult[i][j] = mat[i][j] % M;\n\t\t}\n\t}\n\n\treturn result;\n}\n\nvector<vector<ll>> MatPow(const vector<vector<ll>>& mat, int T, int M) {\n\tif (T == 0) {\n\t\tvector<vector<ll>> id(mat.size(), vector<ll>(mat[0].size(), 0ll));\n\t\tfor (int k = 0; k < id.size(); k++) id[k][k] = 1;\n\t\treturn id;\n\t} else if (T == 1) {\n\t\treturn mat;\n\t}\n\n\tvector<vector<ll>> mat_half = MatPow(mat, T / 2, M);\n\n\tif (T % 2 == 0) {\n\t\treturn (mat_half * mat_half) % M;\n\t} else {\n\t\treturn (mat_half * mat_half * mat) % M;\n\t}\n}\n\nint main(void) {\n\twhile (true) {\n\t\tint N, M, A, B, C, T;\n\t\tcin >> N >> M >> A >> B >> C >> T;\n\t\tif (N == 0) break;\n\t\tvector<vector<ll>> S(N, vector<ll>(1, 0ll));\n\t\tfor (int i = 0; i < N; i++) cin >> S[i][0];\n\n\t\tvector<vector<ll>> mat(N, vector<ll>(N, 0ll));\n\t\tfor (int i = 0; i < N; i++) {\n\t\t\tfor (int j = 0; j < N; j++) {\n\t\t\t\tif (i == j + 1) mat[i][j] = A;\n\t\t\t\telse if (i == j) mat[i][j] = B;\n\t\t\t\telse if (i == j - 1) mat[i][j] = C;\n\t\t\t}\n\t\t}\n\t\tmat = MatPow(mat, T, M);\n\n\t\tS = (mat * S) % M;\n\n\t\tfor (int i = 0; i < N; i++) cout << S[i][0] << \" \\n\"[i == N - 1] << flush;\n\t}\n\n\treturn 0;\n}\n\n/*\n\t\t\t [ B C 0 .. 0 ]\n\t\t\t | A B C .. 0 |\n\tS(t + 1) = | 0 A B .. 0 | * S(t)\n\t\t\t | : : : :: : |\n\t\t\t [ 0 0 0 .. B ]\n*/", "accuracy": 1, "time_ms": 90, "memory_kb": 3628, "score_of_the_acc": -0.3039, "final_rank": 3 }, { "submission_id": "aoj_1327_9329169", "code_snippet": "#include <algorithm>\n#include <cmath>\n#include <cstdlib>\n#include <iomanip>\n#include <iostream>\n#include <list>\n#include <map>\n#include <numeric>\n#include <queue>\n#include <set>\n#include <stack>\n#include <string>\n#include <unordered_set>\n#include <vector>\n\nusing namespace std;\nusing ll = long long;\n\nvector<vector<ll>> operator*(const vector<vector<ll>>& left, const vector<vector<ll>>& right) {\n\tint ln = left.size(), lm = left[0].size(), rm = right[0].size();\n\tvector<vector<ll>> result(ln, vector<ll>(rm, 0ll));\n\n\tfor (int i = 0; i < ln; i++) {\n\t\tfor (int j = 0; j < rm; j++) {\n\t\t\tfor (int k = 0; k < lm; k++) {\n\t\t\t\tresult[i][j] += left[i][k] * right[k][j];\n\t\t\t}\n\t\t}\n\t}\n\n\treturn result;\n}\n\nvector<vector<ll>> operator%(const vector<vector<ll>>& mat, int M) {\n\tvector<vector<ll>> result(mat.size(), vector<ll>(mat[0].size(), 0ll));\n\n\tfor (int i = 0; i < mat.size(); i++) {\n\t\tfor (int j = 0; j < mat[0].size(); j++) {\n\t\t\tresult[i][j] = mat[i][j] % M;\n\t\t}\n\t}\n\n\treturn result;\n}\n\nvector<vector<ll>> MatPow(const vector<vector<ll>>& mat, int T, int M) {\n\tif (T == 0) {\n\t\tvector<vector<ll>> id(mat.size(), vector<ll>(mat[0].size(), 0ll));\n\t\tfor (int k = 0; k < id.size(); k++) id[k][k] = 1;\n\t\treturn id;\n\t} else if (T == 1) {\n\t\treturn mat;\n\t}\n\n\tvector<vector<ll>> mat_half = MatPow(mat, T / 2, M);\n\n\tif (T % 2 == 0) {\n\t\treturn (mat_half * mat_half) % M;\n\t} else {\n\t\treturn (((mat_half * mat_half) % M) * mat) % M;\n\t}\n}\n\nint main(void) {\n\twhile (true) {\n\t\tint N, M, A, B, C, T;\n\t\tcin >> N >> M >> A >> B >> C >> T;\n\t\tif (N == 0) break;\n\t\tvector<vector<ll>> S(N, vector<ll>(1, 0ll));\n\t\tfor (int i = 0; i < N; i++) cin >> S[i][0];\n\n\t\tvector<vector<ll>> mat(N, vector<ll>(N, 0ll));\n\t\tfor (int i = 0; i < N; i++) {\n\t\t\tfor (int j = 0; j < N; j++) {\n\t\t\t\tif (i == j + 1) mat[i][j] = A;\n\t\t\t\telse if (i == j) mat[i][j] = B;\n\t\t\t\telse if (i == j - 1) mat[i][j] = C;\n\t\t\t}\n\t\t}\n\t\tmat = MatPow(mat, T, M);\n\n\t\tS = (mat * S) % M;\n\n\t\tfor (int i = 0; i < N; i++) cout << S[i][0] << \" \\n\"[i == N - 1] << flush;\n\t}\n\n\treturn 0;\n}\n\n/*\n\t\t\t [ B C 0 .. 0 ]\n\t\t\t | A B C .. 0 |\n\tS(t + 1) = | 0 A B .. 0 | * S(t)\n\t\t\t | : : : :: : |\n\t\t\t [ 0 0 0 .. B ]\n*/", "accuracy": 1, "time_ms": 110, "memory_kb": 3676, "score_of_the_acc": -0.3356, "final_rank": 4 }, { "submission_id": "aoj_1327_9328974", "code_snippet": "#include <algorithm>\n#include <cmath>\n#include <cstdlib>\n#include <iomanip>\n#include <iostream>\n#include <list>\n#include <map>\n#include <numeric>\n#include <queue>\n#include <set>\n#include <stack>\n#include <string>\n#include <unordered_set>\n#include <vector>\n\nusing namespace std;\nusing ll = long long;\n\nvector<vector<ll>> MatMul(const vector<vector<ll>>& left, const vector<vector<ll>>& right) {\n\tint ln = left.size(), lm = left[0].size(), rm = right[0].size();\n\tvector<vector<ll>> result(ln, vector<ll>(rm, 0ll));\n\n\tfor (int i = 0; i < ln; i++) {\n\t\tfor (int j = 0; j < rm; j++) {\n\t\t\tfor (int k = 0; k < lm; k++) {\n\t\t\t\tresult[i][j] += left[i][k] * right[k][j];\n\t\t\t}\n\t\t}\n\t}\n\n\treturn result;\n}\n\nvector<vector<ll>> MatMod(const vector<vector<ll>>& mat, int M) {\n\tvector<vector<ll>> result(mat.size(), vector<ll>(mat[0].size(), 0ll));\n\n\tfor (int i = 0; i < mat.size(); i++) {\n\t\tfor (int j = 0; j < mat[0].size(); j++) {\n\t\t\tresult[i][j] = mat[i][j] % M;\n\t\t}\n\t}\n\n\treturn result;\n}\n\nclass {\n private:\n\tmap<int, vector<vector<ll>>> memo_;\n\n\tvector<vector<ll>> MatPow(const vector<vector<ll>>& mat, int T, int M) {\n\t\tif (T == 0) {\n\t\t\tvector<vector<ll>> id(mat.size(), vector<ll>(mat[0].size(), 0ll));\n\t\t\tfor (int k = 0; k < id.size(); k++) id[k][k] = 1;\n\t\t\treturn id;\n\t\t} else if (T == 1) {\n\t\t\treturn mat;\n\t\t}\n\n\t\tint T_half = T / 2;\n\t\tvector<vector<ll>> mat_half;\n\n\t\tif (memo_.find(T_half) == memo_.end()) {\n\t\t\tmat_half = this->operator()(mat, T_half, M);\n\t\t\tmemo_[T_half] = mat_half;\n\t\t} else mat_half = memo_[T_half];\n\n\t\tif (T % 2 == 0) {\n\t\t\treturn MatMod(MatMul(mat_half, mat_half), M);\n\t\t} else {\n\t\t\treturn MatMod(MatMul(MatMod(MatMul(mat_half, mat_half), M), mat), M);\n\t\t}\n\t}\n\n public:\n\tvector<vector<ll>> operator()(const vector<vector<ll>>& mat, int T, int M) {\n\t\tmemo_.clear();\n\t\treturn this->MatPow(mat, T, M);\n\t}\n} MatPow;\n\nint main(void) {\n\twhile (true) {\n\t\tint N, M, A, B, C, T;\n\t\tcin >> N >> M >> A >> B >> C >> T;\n\t\tif (N == 0) break;\n\t\tvector<vector<ll>> S(N, vector<ll>(1, 0ll));\n\t\tfor (int i = 0; i < N; i++) cin >> S[i][0];\n\n\t\tvector<vector<ll>> mat(N, vector<ll>(N, 0ll));\n\t\tfor (int i = 0; i < N; i++) {\n\t\t\tfor (int j = 0; j < N; j++) {\n\t\t\t\tif (i == j + 1) mat[i][j] = A;\n\t\t\t\telse if (i == j) mat[i][j] = B;\n\t\t\t\telse if (i == j - 1) mat[i][j] = C;\n\t\t\t}\n\t\t}\n\t\tmat = MatPow(mat, T, M);\n\n\t\tS = MatMod(MatMul(mat, S), M);\n\n\t\tfor (int i = 0; i < N; i++) cout << S[i][0] << \" \\n\"[i == N - 1] << flush;\n\t}\n\n\treturn 0;\n}\n\n/*\n\t\t\t [ B C 0 .. 0 ]\n\t\t\t | A B C .. 0 |\n\tS(t + 1) = | 0 A B .. 0 | * S(t)\n\t\t\t | : : : :: : |\n\t\t\t [ 0 0 0 .. B ]\n*/", "accuracy": 1, "time_ms": 110, "memory_kb": 4132, "score_of_the_acc": -0.5591, "final_rank": 12 }, { "submission_id": "aoj_1327_9003401", "code_snippet": "// (⁠◕⁠ᴗ⁠◕⁠✿⁠)\n\n// #pragma GCC target(\"avx2\")\n// #pragma GCC optimize(\"O3\")\n// #pragma GCC optimize(\"unroll-loops\")\n#include <bits/stdc++.h>\n#define rep(i, n) for (ll i = 0; i < (n); i++)\n#define srep(i, s, n) for (ll i = s; i < (n); i++)\n#define len(x) ((int)(x).size())\n#define all(x) (x).begin(), (x).end()\nusing namespace std;\ntemplate<typename T> using vc = vector<T>;\ntemplate<typename T> using vv = vc<vc<T>>;\ntemplate<typename T> using vvv = vv<vc<T>>;\nusing vi = vc<int>;using vvi = vv<int>; using vvvi = vv<vi>;\nusing ll = long long;using vl = vc<ll>;using vvl = vv<ll>; using vvvl = vv<vl>;\nusing ld = long double; using vld = vc<ld>; using vvld = vc<vld>; using vvvld = vc<vvld>;\nusing uint = unsigned int;\nusing ull = unsigned long long;\nconst ld pi = 3.141592653589793;\nconst int inf = 0x3f3f3f3f;\nconst ll INF = 0x3f3f3f3f3f3f3f3f;\n// const ll mod = 1000000007;\nconst ll mod = 998244353;\ninline bool inside(ll y, ll x, ll H, ll W) {return 0 <= (y) and (y) < (H) and 0 <= (x) and (x) < (W); }\n\n#define debug(var) do{std::cout << #var << \" : \\n\";view(var);}while(0)\ntemplate<typename T> void view(T e){cout << e << endl;}\ntemplate<typename T> void view(const vc<T>& v){for(const auto& e : v){ cout << e << \" \"; } cout << endl;}\ntemplate<typename T> void view(const vv<T>& vv){ for(const auto& v : vv){ view(v); } }\n\ntemplate<typename T> vc<vc<T>> dot(vc<vc<T>> &A, vc<vc<T>> &B, int M){\n vc<vc<T>> ret;\n for (int i = 0; i < len(A); i++){\n vc<T> row;\n for (int j = 0; j < len(B[0]); j++){\n T ele = 0;\n for (int k = 0; k < len(A[0]); k++){\n ele += A[i][k] * B[k][j];\n ele %= M;\n }\n row.push_back(ele);\n }\n ret.push_back(row);\n }\n return ret;\n}\n\ntemplate<typename T> vc<vc<T>> multidot(vc<vc<T>> &A, ll N, int M){\n vc<vc<T>> ret(len(A), vc<T>(len(A), 0));\n for (int i = 0; i < len(A); i++) ret[i][i] = 1;\n while (N > 0){\n if (N & 1) ret = dot(ret, A, M);\n A = dot(A, A, M);\n N >>= 1;\n }\n return ret;\n}\n\nvi solve(int N, int M, int A, int B, int C, int T){\n vvi S(N, vi(1)); rep(i, N) cin >> S[i][0];\n vvi D(N, vi(N, 0));\n rep(i, N){\n if (i > 0) D[i][i - 1] = A;\n D[i][i] = B;\n if (i < N - 1) D[i][i + 1] = C;\n }\n auto E = multidot(D, T, M);\n auto F = dot(E, S, M);\n vi ans;\n rep(i, N) ans.push_back(F[i][0]);\n return ans;\n}\n\nint main(){\n vc<vi> ans;\n while (true){\n int N, M, A, B, C, T; cin >> N >> M >> A >> B >> C >> T;\n if (N == 0 && M == 0 && A == 0 && B == 0 && C == 0 && T == 0) break;\n ans.push_back(solve(N, M, A, B, C, T));\n }\n for (auto x : ans){\n rep(i, len(x)) cout << x[i] << \" \\n\"[i == len(x) - 1];\n }\n}", "accuracy": 1, "time_ms": 850, "memory_kb": 3308, "score_of_the_acc": -0.4573, "final_rank": 8 }, { "submission_id": "aoj_1327_7993117", "code_snippet": "#include<stdio.h>\n\nchar space_or_indent[2] = { ' ', '\\n' };\n\nvoid matCopy( long long A[50][50], long long B[50][50], int size )\n{\n for( int i = 0; i < size; i++ )\n {\n for( int j = 0; j < size; j++ )\n {\n A[i][j] = B[i][j];\n }\n }\n}\n\nvoid calMatrix( long long A[50][50], long long B[50][50], long long C[50][50], int size, long long MOD )\n{\n long long sum;\n for( int i = 0; i < size; i++ )\n {\n for( int j = 0; j < size; j++ )\n {\n sum = 0;\n for( int k = 0; k < size; k++ )\n {\n sum = ( sum + A[i][k] * B[k][j] ) % MOD;\n }\n C[i][j] = sum;\n }\n }\n}\n\nvoid calPowMatrix( long long A[50][50], int size, int n, long long MOD )\n{\n long long tmp[50][50], res[50][50], ans[50][50];\n matCopy( res, A, size );\n for( int i = 0; i < size; i++ )\n {\n for( int j = 0; j < size; j++ )\n {\n ans[i][j] = i == j;\n }\n }\n\n for( int bit = 0; ( 1 << bit ) <= n; bit++ )\n {\n if( n & ( 1 << bit ) )\n {\n matCopy( tmp, ans, size );\n calMatrix( tmp, res, ans, size, MOD );\n }\n matCopy( tmp, res, size );\n calMatrix( tmp, tmp, res, size, MOD );\n }\n\n matCopy( A, ans, size );\n}\n\nint main( void )\n{\n long long N, M, A, B, C, T;\n long long S[50] = { 0 };\n long long matrix[50][50];\n while( 1 )\n {\n scanf( \"%lld %lld %lld %lld %lld %lld\", &N, &M, &A, &B, &C, &T );\n if( N == 0 && M == 0 && A == 0 && B == 0 && C == 0 && T == 0 ) break;\n for( int i = 0; i < N; i++ )\n {\n scanf( \"%lld\", &S[i] );\n }\n for( int i = 0; i < N; i++ )\n {\n for( int j = 0; j < N; j++ )\n {\n if( i == j ) matrix[i][j] = B;\n else if( i == j + 1 ) matrix[i][j] = A;\n else if( i + 1 == j ) matrix[i][j] = C;\n else matrix[i][j] = 0;\n }\n }\n\n calPowMatrix( matrix, N, T, M );\n\n long long sum;\n for( int i = 0; i < N; i++ )\n {\n sum = 0;\n for( int j = 0; j < N; j++ )\n {\n sum = ( sum + matrix[i][j] * S[j] ) % M;\n }\n printf( \"%lld%c\", sum, space_or_indent[i == N - 1] );\n }\n }\n\n return 0;\n}", "accuracy": 1, "time_ms": 1340, "memory_kb": 3008, "score_of_the_acc": -0.5102, "final_rank": 10 }, { "submission_id": "aoj_1327_7954369", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nconstexpr int inf = 1 << 30;\nusing Matrix = vector<vector<int>>;\nint mod;\n\nMatrix matmul(const Matrix& lhs, const Matrix& rhs) {\n int n = lhs.size();\n int m = rhs.size();\n int l = (rhs.front()).size();\n Matrix result(n, vector<int>(l));\n for (int i = 0; i < n; i++) {\n for (int k = 0; k < l; k++) {\n for (int j = 0; j < m; j++) {\n result[i][k] += lhs[i][j] * rhs[j][k];\n }\n }\n }\n for (int i = 0; i < n; i++) {\n for (int j = 0; j < l; j++) {\n result[i][j] %= mod;\n }\n }\n return result;\n}\n\nMatrix matpow(Matrix mat, int p) {\n int n = mat.size();\n Matrix result(n, vector<int>(n));\n for (int i = 0; i < n; i++) {\n result[i][i] = 1;\n }\n while (p > 0) {\n if (p & 1) {\n result = matmul(result, mat);\n }\n mat = matmul(mat, mat);\n p >>= 1;\n }\n return result;\n}\n\nbool solve() {\n int n, m, t;\n int a[3];\n cin >> n >> m >> a[0] >> a[1] >> a[2] >> t;\n if (n == 0) return false;\n mod = m;\n Matrix dp(n, std::vector<int>(1));\n for (int i = 0; i < n; i++) {\n cin >> dp[i][0];\n }\n Matrix square(n, std::vector<int>(n));\n for (int i = 0; i < n; i++) {\n for (int j = 0; j < 3; j++) {\n if (i + j - 1 < 0 || i + j - 1 >= n) continue;\n square[i][i + j - 1] = a[j];\n }\n }\n square = matpow(square, t);\n dp = matmul(square, dp);\n for (int i = 0; i < n; i++) {\n cout << dp[i][0];\n if (i < n - 1) cout << ' '; \n }\n cout << '\\n';\n return true;\n}\n\nint main() {\n while (solve());\n}", "accuracy": 1, "time_ms": 110, "memory_kb": 3468, "score_of_the_acc": -0.2337, "final_rank": 1 }, { "submission_id": "aoj_1327_7858676", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nusing Mat = vector<vector<int>>;\n\nint add(int a, int b, int mod) {\n return (a + b) % mod;\n}\n\nint mul(int a, int b, int mod) {\n return (a * b) % mod;\n}\n\nMat mul(Mat A, Mat B, int mod) {\n assert(A.size());\n assert(B.size());\n assert(A[0].size() == B.size());\n Mat res(A.size(), vector(B[0].size(), 0));\n for (int i = 0 ; i < (int)A.size() ; i++) {\n for (int j = 0 ; j < (int)B[0].size() ; j++) {\n for (int k = 0 ; k < (int)A[0].size() ; k++) {\n res[i][j] = add(res[i][j], mul(A[i][k], B[k][j], mod), mod);\n }\n }\n }\n return res;\n}\n\nMat pow(Mat A, int p, int mod) {\n assert(A.size());\n assert(A.size() == A[0].size());\n Mat res(A.size(), vector(A.size(), 0));\n for (int i = 0 ; i < (int)A.size() ; i++) res[i][i] = 1;\n while (p) {\n if (p & 1) {\n res = mul(res, A, mod);\n }\n A = mul(A, A, mod);\n p >>= 1;\n }\n return res;\n}\n\nvoid out(Mat A) {\n for (auto arr : A) {\n for (auto x : arr) cout << x << ' ';\n cout << endl;\n }\n}\n\nbool solve() {\n int N, M, A, B, C, T; cin >> N >> M >> A >> B >> C >> T;\n if (N == 0) return false;\n\n Mat OP(N, vector(N, 0));\n for (int i = 0 ; i < N ; i++) {\n if (i > 0) OP[i][i - 1] = A;\n OP[i][i] = B;\n if (i + 1 < N) OP[i][i + 1] = C;\n }\n\n // out(OP);\n // cout << \"----\" << endl;\n OP = pow(OP, T, M);\n // out(OP);\n\n Mat V(N, vector(1, 0));\n for (int i = 0 ; i < N ; i++) cin >> V[i][0];\n Mat ans = mul(OP, V, M);\n for (int i = 0 ; i < N ; i++) cout << ans[i][0] << (i + 1 == N ? '\\n' : ' ');\n\n return true;\n}\n\n\nint main() {\n while (solve()) ;\n}", "accuracy": 1, "time_ms": 870, "memory_kb": 3516, "score_of_the_acc": -0.5674, "final_rank": 13 }, { "submission_id": "aoj_1327_6728644", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\n\nint main(){\n int N, M, A, B, C, T;\n while(cin >> N >> M >> A >> B >> C >> T, N){\n vector<int> S(N);\n vector<vector<int>> D(N, vector<int>(N)), E(N, vector<int>(N));\n for(int i = 0; i < N; i++){\n cin >> S[i];\n E[i][i] = 1;\n if(i - 1 >= 0)D[i - 1][i] = C;\n D[i][i] = B;\n if(i + 1 < N)D[i + 1][i] = A;\n }\n auto mul = [&](vector<vector<int>> L, vector<vector<int>> R){\n vector<vector<int>> res(N, vector<int>(N));\n for(int i = 0; i < N; i++){\n for(int j = 0; j < N; j++){\n for(int k = 0; k < N; k++){\n (res[i][k] += L[i][j] * R[j][k]) %= M;\n }\n }\n }\n return res;\n };\n while(T){\n if(T & 1)E = mul(E, D);\n D = mul(D, D);\n T /= 2;\n }\n for(int i = 0; i < N; i++){\n int v = 0;\n for(int j = 0; j < N; j++){\n (v += E[i][j] * S[j]) %= M;\n }\n if(i)cout << \" \";\n cout << v;\n }\n cout << '\\n';\n }\n}", "accuracy": 1, "time_ms": 280, "memory_kb": 3564, "score_of_the_acc": -0.3501, "final_rank": 5 }, { "submission_id": "aoj_1327_6640694", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\n#define rep(i, s, t) for (int i = (int)(s); i < (int)(t); ++i)\n#define revrep(i, t, s) for (int i = (int)(t)-1; i >= (int)(s); --i)\n#define all(x) begin(x), end(x)\ntemplate <typename T> bool chmax(T& a, const T& b) { return a < b ? (a = b, 1) : 0; }\ntemplate <typename T> bool chmin(T& a, const T& b) { return a > b ? (a = b, 1) : 0; }\n\nvector<vector<int>> matmul(vector<vector<int>> A, vector<vector<int>> B, int M) {\n const int n = A.size();\n const int m = B[0].size();\n vector<vector<int>> C(n, vector<int>(m));\n rep(i,0,n) rep(j,0,m) rep(k,0,B.size()) C[i][j] = (C[i][j] + A[i][k] * B[k][j]) % M;\n return C;\n}\n\nint main() {\n ios_base::sync_with_stdio(false);\n cin.tie(nullptr);\n cout << fixed << setprecision(15);\n\n while (true) {\n int N, M, A, B, C, T;\n cin >> N >> M >> A >> B >> C >> T;\n if (N == 0) break;\n vector<int> S(N);\n for (auto& x : S) cin >> x;\n\n vector<vector<int>> X(N, vector<int>(N));\n rep(i,0,N) {\n if (i > 0) X[i][i-1] = C;\n X[i][i] = B;\n if (i+1 < N) X[i][i+1] = A;\n }\n vector<vector<int>> ans(1, vector<int>(N));\n rep(i,0,N) ans[0][i] = S[i];\n while (T) {\n if (T & 1) {\n ans = matmul(ans, X, M);\n }\n X = matmul(X, X, M);\n T >>= 1;\n }\n rep(i,0,N) cout << ans[0][i] << (i < N-1 ? \" \" : \"\\n\");\n }\n}", "accuracy": 1, "time_ms": 540, "memory_kb": 3384, "score_of_the_acc": -0.368, "final_rank": 7 }, { "submission_id": "aoj_1327_6640598", "code_snippet": "#include <bits/stdc++.h>\n//#include <atcoder/all>\n//using namespace atcoder;\nusing namespace std;\nusing ll = long long;\nusing vll = vector<ll>;\nusing vvll = vector<vll>;\nusing vvvll = vector<vvll>;\n#define all(A) A.begin(),A.end()\n#define rep(i, n) for (ll i = 0; i < (ll) (n); i++)\nusing pqr = priority_queue<pair<ll, ll>, vector<pair<ll, ll>>, greater<pair<ll, ll>>>;\n\nbool chmax(ll& p, ll q) {\n if (p < q) {\n p = q;\n return 1;\n }\n else {\n return 0;\n }\n}\n\nbool chmin(ll& p, ll q) {\n if (p > q) {\n p = q;\n return 1;\n }\n else {\n return 0;\n }\n}\n\nll sti(string S) {\n ll res = 0;\n rep(i, S.size()) {\n res += S[i] - '0';\n res *= 10;\n }\n return res / 10;\n}\n\nstring ist(ll n, ll L = -1) {\n string res = \"\";\n if (L >= 0)rep(l, L)res += '0';\n ll p = L - 1;\n if (L != -1)while (n > 0) {\n res[p] = char(n % 10 + '0');\n p--;\n n /= 10;\n }\n else {\n while (n > 0) {\n res.push_back(char(n % 10 + '0'));\n p--;\n n /= 10;\n }\n reverse(all(res));\n }\n return res;\n}\n\nll gcd(ll(a), ll(b)) {\n ll c = a;\n while (a % b != 0) {\n c = a % b;\n a = b;\n b = c;\n }\n return b;\n}\n\nll mod = 998244353;\n\nvector<vector<vector<ll>>> matrixpow;\nvoid prematrixpow(vector<vll> matrix) {\n\tll N = matrix.size();\n\tmatrixpow.assign(70, vector<vll>(N, vll(N)));\n\trep(h, N) {\n\t\trep(w, N) {\n\t\t\tmatrixpow[0][h][w] = matrix[h][w];\n\t\t}\n\t}\n\trep(k, 69) {\n\t\trep(h, N) {\n\t\t\trep(w, N) {\n\t\t\t\tll S = 0;\n\t\t\t\trep(i, N) {\n\t\t\t\t\tS += matrixpow[k][h][i] * matrixpow[k][i][w];\n\t\t\t\t\tS %= mod;\n\t\t\t\t}\n\t\t\t\tmatrixpow[k + 1][h][w] = S;\n\t\t\t}\n\t\t}\n\t}\n\n}\nvector<vll> modmatrixpow(ll K) {\n\tll P = K;\n\tll N = matrixpow[0].size();\n\tvector<vll> res(N, vll(N, 0));\n\trep(i, N) {\n\t\tres[i][i] = 1;\n\t}\n\n\trep(j, 70) {\n\t\tif (P % 2 != 0) {\n\t\t\tvector<vll> res2(N, vll(N, 0));\n\n\t\t\trep(h, N) {\n\t\t\t\trep(w, N) {\n\t\t\t\t\tll S = 0;\n\t\t\t\t\trep(i, N) {\n\t\t\t\t\t\tS += matrixpow[j][h][i] * res[i][w];\n\t\t\t\t\t\tS %= mod;\n\t\t\t\t\t}\n\t\t\t\t\tres2[h][w] = S;\n\t\t\t\t}\n\t\t\t}\n\t\t\tres = res2;\n\t\t}\n\t\tP /= 2;\n\t}\n\n\treturn res;\n\n\n}\n\nint main() {\n cin.tie(nullptr);\n ios::sync_with_stdio(false);\n\n while(1){\n ll N,M,A,B,C,T;\n cin>>N>>M>>A>>B>>C>>T;\n if(N==0)return 0;\n vll D(N);\n rep(i,N)cin>>D[i];\n mod=M;\n vvll F(N,vll(N));\n rep(i,N){\n F[i][i]=B;\n if(i!=0){\n F[i-1][i]=C; \n F[i][i-1]=A;\n } \n }\n prematrixpow(F);\n auto k=modmatrixpow(T);\n vll AN(N,0);\n rep(i,N){\n rep(j,N){\n AN[i]+=k[i][j]*D[j];\n AN[i]%=M;\n }\n cout<<AN[i]<<(i==N-1?\"\\n\":\" \");\n }\n\n\n }\n\n}", "accuracy": 1, "time_ms": 2540, "memory_kb": 4780, "score_of_the_acc": -1.8686, "final_rank": 20 } ]
aoj_1328_cpp
Problem D: Find the Outlier Professor Abacus has just built a new computing engine for making numerical tables. It was designed to calculate the values of a polynomial function in one variable at several points at a time. With the polynomial function f ( x ) = x 2 + 2 x + 1, for instance, a possible expected calculation result is 1 (= f (0)), 4 (= f (1)), 9 (= f (2)), 16 (= f (3)), and 25 (= f (4)). It is a pity, however, the engine seemingly has faulty components and exactly one value among those calculated simultaneously is always wrong. With the same polynomial function as above, it can, for instance, output 1, 4, 12, 16, and 25 instead of 1, 4, 9, 16, and 25. You are requested to help the professor identify the faulty components. As the first step, you should write a program that scans calculation results of the engine and finds the wrong values. Input The input is a sequence of datasets, each representing a calculation result in the following format. d v 0 v 1 ... v d +2 Here, d in the first line is a positive integer that represents the degree of the polynomial, namely, the highest exponent of the variable. For instance, the degree of 4 x 5 + 3 x + 0.5 is five and that of 2.4 x + 3.8 is one. d is at most five. The following d + 3 lines contain the calculation result of f (0), f (1), ... , and f ( d + 2) in this order, where f is the polynomial function. Each of the lines contains a decimal fraction between -100.0 and 100.0, exclusive. You can assume that the wrong value, which is exactly one of f (0), f (1), ... , and f ( d +2), has an error greater than 1.0. Since rounding errors are inevitable, the other values may also have errors but they are small and never exceed 10 -6 . The end of the input is indicated by a line containing a zero. Output For each dataset, output i in a line when v i is wrong. Sample Input 2 1.0 4.0 12.0 16.0 25.0 1 -30.5893962764 5.76397083962 39.3853798058 74.3727663177 4 42.4715310246 79.5420238202 28.0282396675 -30.3627807522 -49.8363481393 -25.5101480106 7.58575761381 5 -21.9161699038 -48.469304271 -24.3188578417 -2.35085940324 -9.70239202086 -47.2709510623 -93.5066246072 -82.5073836498 0 Output for the Sample Input 2 1 1 6
[ { "submission_id": "aoj_1328_10496662", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\nint d;\ndouble a[1003][1004];\ndouble k[20005];\nbool solve(){\n//\tfor(int i=1;i<=d+3;i++){\n\t////\tfor(int j=1;j<=d+2;j++){\n\t//\t\tcout<<a[i][j]<<\" \";\n\t//\t}\n\t//\tcout<<endl;\n\t//}\n\tint n=d+1,m=d+2;\n\t\n\t/*for(int i=1;i<=n;i++){\n\t\tint maxx=i;\n\t\tfor(int j=i+1;j<=n;j++){\n\t\t\tif(fabs(a[j][i])>fabs(a[maxx][i])){\n\t\t\t\tmaxx=j;\n\t\t\t}\n\t\t}\n\t\tif(fabs(a[maxx][i])>1e-4) swap(a[i],a[maxx]);\n\t\telse continue;\n\t\tfor(int j=n+1;j>=i;j--) a[i][j]/=a[i][i]; \n\t\tfor(int j=1;j<=n+1;j++){\n\t\t\tif(j==i) continue;\n\t\t\tfor(int k=n+1;k>=i;k--){\n\t\t\t\ta[j][k]-=a[j][i]*a[i][k];\n\t\t\t}\n\t\t}\n\t}*/\n\tint line=1;\n\tfor(int i=1;i<=n;i++){\n\t\tint id=line;\n\t\tfor(int j=id+1;j<=m;j++){\n\t\t\tif(fabs(a[j][i])>fabs(a[id][i])){\n\t\t\t\tid=j;\n\t\t\t}\n\t\t}\n\t\tif(fabs(a[id][i])>1e-10) swap(a[line],a[id]);\n\t\telse continue;\n\t\tfor(int j=n+1;j>=i;j--) a[line][j]/=a[line][i];\n\t\tfor(int j=1;j<=m;j++){\n\t\t\tif(line==j){\n\t\t\t\tcontinue;\n\t\t\t} \n\t\t\tfor(int k=n+1;k>=i;k--) a[j][k]-=a[j][i]*a[line][k];\n\t\t}\n\t\tline++;\n\t}\n//\tcout<<\"x: \"<<\"\\n\";\n//\tfor(int i=1;i<=d+3;i++){\n//\t\tfor(int j=1;j<=d+2;j++){\n//\t\t\tcout<<a[i][j]<<\" \";\n//\t\t}\n//\t\tcout<<endl;\n//\t}\n//\tcout<<a[d+2][d+2]<<endl;\n\tfor(int i=line;i<=m;i++) {\n\t\tif(fabs(a[i][n+1])>1e-5){\n\t\t\treturn false;\n\t\t}\n\t}\n\treturn true;\n\n}\nint power(int c,int b){\n\tint res=1;\n\twhile(b){\n\t\tif(b&1){\n\t\t\tres=res*c;\n\t\t}\n\t\tc=c*c;\n\t\tb>>=1;\n\t}\n\treturn res;\n}\nsigned main(){\n\twhile(cin>>d){\n\t\tif(!d) break;\n\t\telse{\n\t\t\t\n\t\t\tfor(int i=1;i<=d+3;i++){\n\t\t\t\tcin>>k[i];\n\t\t\t\t//cin>>a[i][d+2];\n\t\t\t\tfor(int j=1;j<=d;j++) a[i][j]=i;\n\t\t\t}\n\t\t\tfor(int i=1;i<=d+3;i++){\n\t\t\t\tfor(int j=1;j<=d+3;j++){\n\t\t\t\t\tfor(int p=1;p<=d+2;p++) a[j][p]=(p==d+2)?k[j]:power(j-1,d+1-p);\n\t\t\t\t}\n\t\t\t\tfor(int j=i;j<=d+2;j++) swap(a[j],a[j+1]);\n\t\t\t//cout<<\"x \"<<i<<endl;\n\t\t\t\tif(solve()){\n\t\t\t\t\tcout<<i-1<<endl;\n\t\t\t\t\tbreak;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n}\n/*\n2\n1.0\n4.0\n12.0\n16.0\n25.0\n1\n-30.5893962764\n5.76397083962\n39.3853798058\n74.3727663177\n4\n42.4715310246\n79.5420238202\n28.0282396675\n-30.3627807522\n-49.8363481393\n-25.5101480106\n7.58575761381\n5\n-21.9161699038\n-48.469304271\n-24.3188578417\n-2.35085940324\n-9.70239202086\n-47.2709510623\n-93.5066246072\n-82.5073836498\n0\n\n*/", "accuracy": 1, "time_ms": 10, "memory_kb": 3488, "score_of_the_acc": -0.4358, "final_rank": 15 }, { "submission_id": "aoj_1328_10036047", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\ntemplate <typename T>\nstruct mat {\n\tint n, m;\n\tvector<vector<T>> a;\n\tmat(int n_, int m_) : n(n_), m(m_), a(n_, vector<T>(m_)) {}\n};\ntemplate <typename T>\nvector<T> solveLSE(mat<T> a, vector<T> b) { // ax=b\n\tassert(a.n == b.size() && a.m == b.size());\n\tint n = a.n;\n\tfor (int i = 0; i < n; i++) {\n\t\tint p = i;\n\t\tfor (int j = p; j < n; j++) {\n\t\t\tif (a.a[i][j] > a.a[i][p]) p = j;\n\t\t}\n\t\tswap(a.a[i], a.a[p]);\n\t\tswap(b[i], b[p]);\n\t\tfor (int j = 0; j < n; j++) {\n\t\t\tif (j != i) a.a[i][j] /= a.a[i][i];\n\t\t}\n\t\tb[i] /= a.a[i][i];\n\t\ta.a[i][i] = 1;\n\t\tfor (int k = 0; k < n; k++) {\n\t\t\tif (i == k) continue;\n\t\t\tfor (int j = 0; j < n; j++) {\n\t\t\t\tif (j != i) a.a[k][j] -= a.a[i][j] * a.a[k][i];\n\t\t\t}\n\t\t\tb[k] -= b[i] * a.a[k][i];\n\t\t\ta.a[k][i] = 0;\n\t\t}\n\t}\n\treturn b;\n}\nint main() {\n\tmt19937 mt(time(NULL));\n\twhile (1) {\n\t\tint d;\n\t\tcin >> d;\n\t\tif (d == 0) break;\n\t\tuniform_int_distribution<int> rd(0, d + 2);\n\t\tvector<double> v(d + 3);\n\t\tfor (int i = 0; i < d + 3; i++) {\n\t\t\tcin >> v[i];\n\t\t}\n\t\tint ans = -1;\n\t\tdouble bestscore = 1e100;\n\t\tfor (int igni = 0; igni < d + 3; igni++) {\n\t\t\tmat<double> a(d + 1, d + 1);\n\t\t\tvector<int> idx;\n\t\t\tfor (int i = 0; i < d + 3; i++) {\n\t\t\t\tif (i == igni) continue;\n\t\t\t\tidx.push_back(i);\n\t\t\t}\n\t\t\tshuffle(idx.begin(), idx.end(), mt);\n\t\t\tidx.pop_back();\n\t\t\tvector<double> b(d + 1);\n\t\t\t{\n\t\t\t\tint i = 0;\n\t\t\t\tfor (int ri : idx) {\n\t\t\t\t\tfor (int j = 0; j < d + 1; j++) {\n\t\t\t\t\t\ta.a[i][j] = pow(ri, j);\n\t\t\t\t\t}\n\t\t\t\t\tb[i] = v[ri];\n\t\t\t\t\ti++;\n\t\t\t\t}\n\t\t\t}\n\n\t\t\tauto res = solveLSE(a, b);\n\t\t\tassert(res.size() == d + 1);\n\t\t\tauto f = [&](int x) {\n\t\t\t\tdouble r = 0;\n\t\t\t\tfor (int j = 0; j <= d; j++) {\n\t\t\t\t\tr += res[j] * pow(x, j);\n\t\t\t\t}\n\t\t\t\treturn r;\n\t\t\t};\n\t\t\tdouble igerror = 0, nomerror = 0;\n\t\t\tfor (int i = 0; i < d + 3; i++) {\n\t\t\t\tif (i == igni) {\n\t\t\t\t\tigerror += abs(f(i) - v[i]);\n\t\t\t\t} else {\n\t\t\t\t\tnomerror += abs(f(i) - v[i]);\n\t\t\t\t}\n\t\t\t}\n\t\t\tdouble score = nomerror / igerror;\n\t\t\tif (bestscore > score) {\n\t\t\t\t// for (int i = 0; i < d + 1; i++) {\n\t\t\t\t// \tcerr << res[i] << \" \";\n\t\t\t\t// }\n\t\t\t\t// cerr << endl;\n\t\t\t\tbestscore = score;\n\t\t\t\tans = igni;\n\t\t\t}\n\t\t}\n\t\tcout << ans << endl;\n\t}\n\treturn 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3900, "score_of_the_acc": -0.9083, "final_rank": 17 }, { "submission_id": "aoj_1328_8740978", "code_snippet": "//\n// 実数行列 (行列累乗と、掃き出し法)\n//\n// verified:\n// AOJ 2171 Strange Couple\n// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=2171\n//\n// AOJ 1328 Find the Outlier\n// https://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=1328\n//\n\n\n#include <bits/stdc++.h>\nusing namespace std;\n\n\n// basic settings\nlong double EPS = 1e-10; // to be set appropriately\n\n// matrix\ntemplate<class T> struct Matrix {\n // inner value\n vector<vector<T>> val;\n \n // constructors\n Matrix(int H, int W, T x = 0) : val(H, vector<T>(W, x)) {}\n Matrix(const Matrix &mat) : val(mat.val) {}\n void init(int H, int W, T x = 0) {\n val.assign(H, vector<T>(W, x));\n }\n void resize(int H, int W) {\n val.resize(H);\n for (int i = 0; i < H; ++i) val[i].resize(W);\n }\n \n // getter and debugger\n constexpr int height() const { return (int)val.size(); }\n constexpr int width() const { return (int)val[0].size(); }\n vector<T>& operator [] (int i) { return val[i]; }\n constexpr vector<T>& operator [] (int i) const { return val[i]; }\n friend constexpr ostream& operator << (ostream &os, const Matrix<T> &mat) {\n os << endl;\n for (int i = 0; i < mat.height(); ++i) {\n for (int j = 0; j < mat.width(); ++j) {\n if (j) os << \", \";\n os << mat.val[i][j];\n }\n os << endl;\n }\n return os;\n }\n \n // comparison operators\n constexpr bool operator == (const Matrix &r) const {\n return this->val == r.val;\n }\n constexpr bool operator != (const Matrix &r) const {\n return this->val != r.val;\n }\n \n // arithmetic operators\n constexpr Matrix& operator += (const Matrix &r) {\n assert(height() == r.height());\n assert(width() == r.width());\n for (int i = 0; i < height(); ++i) {\n for (int j = 0; j < width(); ++j) {\n val[i][j] += r[i][j];\n }\n }\n return *this;\n }\n constexpr Matrix& operator -= (const Matrix &r) {\n assert(height() == r.height());\n assert(width() == r.width());\n for (int i = 0; i < height(); ++i) {\n for (int j = 0; j < width(); ++j) {\n val[i][j] -= r[i][j];\n }\n }\n return *this;\n }\n constexpr Matrix& operator *= (T v) {\n for (int i = 0; i < height(); ++i)\n for (int j = 0; j < width(); ++j)\n val[i][j] *= v;\n return *this;\n }\n constexpr Matrix& operator *= (const Matrix &r) {\n assert(width() == r.height());\n Matrix<T> res(height(), r.width());\n for (int i = 0; i < height(); ++i)\n for (int j = 0; j < r.width(); ++j)\n for (int k = 0; k < width(); ++k)\n res[i][j] += val[i][k] * r[k][j];\n return (*this) = res;\n }\n constexpr Matrix operator + () const { return Matrix(*this); }\n constexpr Matrix operator - () const { return Matrix(*this) *= T(-1); }\n constexpr Matrix operator + (const Matrix &r) const { return Matrix(*this) += r; }\n constexpr Matrix operator - (const Matrix &r) const { return Matrix(*this) -= r; }\n constexpr Matrix operator * (T v) const { return Matrix(*this) *= v; }\n constexpr Matrix operator * (const Matrix &r) const { return Matrix(*this) *= r; }\n \n // pow\n constexpr Matrix pow(long long n) const {\n assert(height() == width());\n Matrix<T> res(height(), width()), mul(*this);\n while (n > 0) {\n if (n & 1) res *= mul;\n mul *= mul;\n n >>= 1;\n }\n return res;\n }\n friend constexpr Matrix<T> pow(const Matrix<T> &mat, long long n) {\n return mat.pow(n);\n }\n \n // gauss-jordan\n constexpr int find_pivot(int cur_rank, int col) const {\n int pivot = -1;\n T max_v = EPS;\n for (int row = cur_rank; row < height(); ++row) {\n if (abs(val[row][col]) > max_v) {\n max_v = abs(val[row][col]);\n pivot = row;\n }\n }\n return pivot;\n }\n constexpr void sweep(int cur_rank, int col, int pivot) {\n swap(val[pivot], val[cur_rank]);\n auto fac = val[cur_rank][col];\n for (int col2 = 0; col2 < width(); ++col2) {\n val[cur_rank][col2] /= fac;\n }\n for (int row = 0; row < height(); ++row) {\n if (row != cur_rank && abs(val[row][col]) > EPS) {\n auto fac = val[row][col];\n for (int col2 = 0; col2 < width(); ++col2) {\n val[row][col2] -= val[cur_rank][col2] * fac;\n }\n }\n }\n }\n constexpr int gauss_jordan(int not_sweep_width = 0) {\n int rank = 0;\n for (int col = 0; col < width(); ++col) {\n if (col == width() - not_sweep_width) break;\n int pivot = find_pivot(rank, col);\n if (pivot == -1) continue;\n sweep(rank++, col, pivot);\n }\n return rank;\n }\n friend constexpr int gauss_jordan(Matrix<T> &mat, int not_sweep_width = 0) {\n return mat.gauss_jordan(not_sweep_width);\n }\n friend constexpr vector<T> linear_equation(const Matrix<T> &mat, const vector<T> &b) {\n // extend\n Matrix<T> A(mat.height(), mat.width() + 1);\n for (int i = 0; i < mat.height(); ++i) {\n for (int j = 0; j < mat.width(); ++j) A[i][j] = mat.val[i][j];\n A[i].back() = b[i];\n }\n int rank = A.gauss_jordan(1);\n \n // check if it has no solution\n vector<T> res;\n for (int row = rank; row < mat.height(); ++row)\n if (abs(A[row].back()) > EPS)\n return res;\n\n // answer\n res.assign(mat.width(), 0);\n for (int i = 0; i < rank; ++i) res[i] = A[i].back();\n return res;\n }\n};\n\n\n\n/*/////////////////////////////*/\n// Examples\n/*/////////////////////////////*/\n\nvoid AOJ_2171() {\n int N, s, t;\n while (cin >> N >> s >> t, N) {\n --s, --t;\n vector<int> q(N);\n vector<vector<int>> a(N, vector<int>(N));\n for (int i = 0; i < N; ++i) cin >> q[i];\n for (int i = 0; i < N; ++i) for (int j = 0; j < N; ++j) cin >> a[i][j];\n\n // Dijkstra\n const int INF = 1<<29;\n vector<int> dist(N, INF);\n vector<bool> seen(N, 0);\n dist[t] = 0;\n for (int iter = 0; iter < N; ++iter) {\n int curd = INF;\n int v = -1;\n for (int i = 0; i < N; ++i) {\n if (seen[i]) continue;\n if (curd > dist[i]) {\n curd = dist[i];\n v = i;\n }\n }\n if (v == -1) break;\n for (int w = 0; w < N; ++w) {\n if (w == v) continue;\n if (a[v][w] == 0) continue;\n dist[w] = min(dist[w], curd + a[v][w]);\n }\n seen[v] = true;\n }\n if (dist[s] >= INF) {\n cout << \"impossible\" << endl;\n return;\n }\n\n // 連立一次方程式を作る\n Matrix<double> A(N, N, 0);\n vector<double> b(N, 0);\n for (int v = 0; v < N; ++v) {\n if (v == t) {\n A[v][v] = 1;\n b[v] = 0;\n }\n else {\n vector<int> neigbor;\n for (int w = 0; w < N; ++w) {\n if (a[v][w] == 0) continue;\n if (q[v] == 1 && dist[w] + a[v][w] != dist[v]) continue;\n neigbor.push_back(w);\n }\n int K = neigbor.size();\n for (auto w : neigbor) {\n A[v][w] -= 1;\n b[v] += a[v][w];\n }\n A[v][v] += K;\n }\n }\n \n // 解く\n auto res = linear_equation(A, b);\n if (res.empty()) cout << \"impossible\" << endl;\n else cout << fixed << setprecision(15) << res[s] << endl;\n }\n}\n\nvoid AOJ_1328() {\n using D = long double;\n EPS = 1e-5; // set EPS\n \n auto dpow = [&](D a, int n) -> D {\n D res = 1.0;\n for (int i = 0; i < n; ++i) res *= a;\n return res;\n };\n auto func = [&](const vector<D> &coef, int i) -> D {\n D res = 0.0;\n for (int p = 0; p < (int)coef.size(); ++p)\n res += coef[p] * pow(i, p);\n return res;\n };\n \n int d;\n while (cin >> d, d) {\n vector<D> v(d + 3);\n for (int i = 0; i < d + 3; ++i) cin >> v[i];\n\n bool finish = false;\n int res = 0;\n for (int i = 0; i < d + 3 && !finish; ++i) {\n for (int j = i + 1; j < d + 3 && !finish; ++j) {\n Matrix<D> A(d + 1, d + 1);\n vector<D> b(d + 1);\n for (int k = 0, iter = 0; k < d+3; ++k) {\n if (k == i || k == j) continue;\n for (int p = 0; p < d + 1; ++p) {\n A[iter][p] = pow(k, p);\n b[iter] = v[k];\n }\n ++iter;\n }\n vector<D> ans = linear_equation(A, b);\n if (ans.empty()) continue;\n D vi = func(ans, i), vj = func(ans, j);\n int num = 0;\n if (fabs(vi - v[i]) > EPS) res = i, ++num;\n if (fabs(vj - v[j]) > EPS) res = j, ++num;\n if (num == 1) goto end;\n }\n }\n end:\n cout << res << endl;\n }\n}\n\n\nint main() {\n //AOJ_2171();\n AOJ_1328();\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3840, "score_of_the_acc": -0.8394, "final_rank": 16 }, { "submission_id": "aoj_1328_7233740", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\n\nusing ld = long double;\nusing point = pair<ld,ld>;\n\nld compute(const vector<point>& points, const ld& x) {\n int n = points.size();\n\n vector<ld> vals;\n for (int i = 0; i < n; i++) {\n ld val = points[i].second;\n\n for (int j = 0; j < n; j++) {\n if(i == j)continue;\n val *= (x - points[j].first);\n val /= (points[i].first - points[j].first);\n }\n\n vals.push_back(val);\n }\n sort(vals.begin(),vals.end());\n\n return accumulate(vals.begin(),vals.end(), 0.0l);\n}\n\nvoid solve(int d) {\n ld v[d + 3];\n for (int i = 0; i < d + 3; i++) {\n cin >> v[i];\n }\n\n vector<pair<ld,int>> diffs;\n\n for (int exclude = 0; exclude < d + 3; exclude++) {\n vector<point> points;\n for (int i = 0; i < d + 3; i++) {\n if(i == exclude) continue;\n points.emplace_back(i, v[i]);\n }\n\n random_shuffle(points.begin(), points.end());\n points.resize(d + 1);\n\n ld diff = 0.0l;\n for (int i = 0; i < d + 3; i++) {\n if(i == exclude) continue;\n diff += powl(compute(points, i) - v[i], 2.0l);\n }\n diffs.emplace_back(diff, exclude);\n }\n\n sort(diffs.begin(), diffs.end());\n cout << diffs.front().second << endl;\n}\n\nint main() {\n int d;\n while(true) {\n cin >> d;\n if(d == 0) break;\n solve(d);\n }\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3328, "score_of_the_acc": -0.2523, "final_rank": 10 }, { "submission_id": "aoj_1328_5491188", "code_snippet": "#include <stdio.h>\n#include <bits/stdc++.h>\nusing namespace std;\n#define rep(i,n) for (int i = 0; i < (n); ++i)\n#define Inf 1000000001\n\ndouble eps = 1e-2;\nvector<double> add(vector<double> a,vector<double> b){\n\tvector<double> c(max((int)a.size(),(int)b.size()),0.0);\n\trep(i,a.size())c[i] += a[i];\n\trep(i,b.size())c[i] += b[i];\n\treturn c;\n}\n\nvector<double> mul(vector<double> a,vector<double> b){\n\tvector<double> c(a.size()+b.size()-1,0.0);\n\trep(i,a.size()){\n\t\trep(j,b.size()){\n\t\t\tc[i+j] += a[i]*b[j];\n\t\t}\n\t}\n\treturn c;\n}\n\ndouble get(vector<double> a,double x){\n\tdouble ret = 0.0;\n\trep(i,a.size()){\n\t\tret += pow(x,i) * a[i];\n\t}\n\treturn ret;\n}\n\nvector<double> Find(vector<double> x,vector<double> y){\n\tvector<double> ret;\n\t\n\trep(i,x.size()){\n\t\tvector<double> t(1,1.0);\n\t\trep(j,x.size()){\n\t\t\tif(i==j)continue;\n\t\t\tt = mul(t,{-x[j],1.0});\n\t\t}\n\t\tdouble A = y[i];\n\t\tA /= get(t,x[i]);\n\t\tt = mul(t,{A});\n\t\tret = add(ret,t);\n\t}\n\treturn ret;\n}\n\nint main(){\n\t\n\twhile(true){\n\t\tint d;\n\t\tcin>>d;\n\t\tif(d==0)break;\n\t\tvector<double> v(d+3);\n\t\trep(i,d+3)cin>>v[i];\n\t\t\n\t\tint ans = -1;\n\t\trep(i,d+3){\n\t\t\trep(j,d+3){\n\t\t\t\tif(i==j)continue;\n\t\t\t\tvector<double> x,y;\n\t\t\t\trep(k,d+3){\n\t\t\t\t\tif(i==k||j==k)continue;\n\t\t\t\t\tx.push_back(k);\n\t\t\t\t\ty.push_back(v[k]);\n\t\t\t\t}\n\t\t\t\t\n\t\t\t\tauto ret = Find(x,y);\n\t\t\t\tbool f0 = true,f1 = true;\n\t\t\t\tif(abs(get(ret,i)-v[i])<eps)f0=false;\n\t\t\t\tif(abs(get(ret,j)-v[j])<eps)f1=false;\n\t\t\t\tif(f0&&f1)continue;\n\t\t\t\tif(f0)ans = i;\n\t\t\t\telse ans = j;\n\t\t\t\t\n\t\t\t}\n\t\t}\n\t\t\n\t\tcout<<ans<<endl;\n\t\t\n\t}\n\t\n\treturn 0;\n}", "accuracy": 1, "time_ms": 70, "memory_kb": 3920, "score_of_the_acc": -1.4766, "final_rank": 20 }, { "submission_id": "aoj_1328_4963196", "code_snippet": "#include<iostream>\n#include<vector>\n#include<algorithm>\n#include<cmath>\n#include<ctime>\n#include<queue>\n#include<cassert>\n#include<set>\n\nusing namespace std;\nusing ll = long long;\nusing vi = vector<int>;\nusing vvi = vector<vector<int> >;\nusing pii = pair<int, int>;\nusing ppi = pair<pii, int>;\n\ntypedef vector<double> vec;\ntypedef vector<vec> mat;\n\nconst double EPS = 1e-8;\nvec gauss_jordan(const mat& A, const vec& b){\n int n = A.size();\n mat B(n, vec(n + 1));\n for(int i = 0; i < n; i++)\n for(int j = 0; j < n; j++) B[i][j] = A[i][j];\n for(int i = 0; i < n; i++) B[i][n] = b[i];\n for(int i = 0; i < n; i++){\n int pivot = i;\n for(int j = i; j < n; j++){\n if(abs(B[j][i]) > abs(B[pivot][i])) pivot = j;\n }\n swap(B[i], B[pivot]);\n\n if(abs(B[i][i]) < EPS) return vec();\n\n for(int j = i+1; j <= n; j++) B[i][j] /= B[i][i];\n for(int j = 0; j < n; j++){\n if(i != j){\n for(int k = i+1; k <= n; k++) B[j][k] -= B[j][i] * B[i][k];\n }\n }\n }\n vec x(n);\n for(int i = 0; i < n; i++) x[i] = B[i][n];\n return x;\n}\n\nint main(){\n cin.tie(0);\n ios::sync_with_stdio(false);\n int ti = clock();\n // start-----------------------------------------------\n int d;\n while(cin >> d, d){\n vec v(d+3);\n for(int i = 0; i < d+3; i++) cin >> v[i];\n int ans = 0;\n mat m(d+3, vec(d+2));\n for(int i = 0; i < d+3; i++){\n for(int j = 0; j < d+1; j++) m[i][j] = pow(1.0*i, j);\n }\n for(int i = 0; i < d+3; i++){\n for(int j = i+1; j < d+3; j++){\n vec b(d+1);\n mat A(d+1, vec(d+1));\n for(int k = 0, l = 0; k < d+3; k++)\n if(i != k && j != k) A[l] = m[k], b[l] = v[k], l++;\n vec x = gauss_jordan(A, b);\n double res[2] = {};\n for(int k = 0; k < d+1; k++){\n res[0] += x[k] * m[i][k];\n res[1] += x[k] * m[j][k];\n }\n if(abs(res[0] - v[i]) + 1e-5 > 1.0 && abs(res[1] - v[j]) < 1e-5) ans = i;\n if(abs(res[0] - v[i]) < 1e-5 && abs(res[1] - v[j]) + 1e-5 > 1.0) ans = j;\n }\n }\n cout << ans << endl;\n }\n\n // end-----------------------------------------------\n // cerr << 1.0 * (clock() - ti) / CLOCKS_PER_SEC << endl;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3980, "score_of_the_acc": -1, "final_rank": 18 }, { "submission_id": "aoj_1328_4954188", "code_snippet": "#include <iostream>\n#include <string>\n#include <cstdlib>\n#include <cmath>\n#include <vector>\n#include <unordered_map>\n#include <map>\n#include <set>\n#include <algorithm>\n#include <queue>\n#include <stack>\n#include <functional>\n#include <bitset>\n#include <assert.h>\n#include <unordered_map>\n#include <fstream>\n#include <ctime>\n#include <complex>\n#include <iomanip>\n#include <random>\nusing namespace std;\ntypedef long long ll;\ntypedef vector<ll> vl;\ntypedef vector<vl> vvl;\ntypedef vector<char> vc;\ntypedef vector<string> vs;\ntypedef vector<bool> vb;\ntypedef vector<double> vd;\ntypedef pair<ll,ll> P;\ntypedef pair<int,int> pii;\ntypedef vector<P> vpl;\ntypedef tuple<ll,ll,ll> tapu;\n#define rep(i,n) for(int i=0; i<(n); i++)\n#define REP(i,a,b) for(int i=(a); i<(b); i++)\n#define all(x) (x).begin(), (x).end()\n#define rall(x) (x).rbegin(), (x).rend()\nconst int inf = 1<<30;\nconst ll linf = 1LL<<62;\nconst int MAX = 510000;\n// ll dy[8] = {0,-1,0,1,1,-1,-1,1};\n// ll dx[8] = {-1,0,1,0,1,-1,1,-1};\nconst double pi = acos(-1);\nconst double eps = 1e-4;\ntemplate<typename T1,typename T2> inline bool chmin(T1 &a,T2 b){\n\tif(a>b){\n\t\ta = b; return true;\n\t}\n\telse return false;\n}\ntemplate<typename T1,typename T2> inline bool chmax(T1 &a,T2 b){\n\tif(a<b){\n\t\ta = b; return true;\n\t}\n\telse return false;\n}\ntemplate<typename T> inline void print(T &a){\n for(auto itr = a.begin(); itr != a.end(); itr++){\n\t\tcout << *itr << \" \";\n\t}\n cout << \"\\n\";\n}\ntemplate<typename T1,typename T2> inline void print2(T1 a, T2 b){\n\tcout << \"debug: \" << a << \" \" << b << \"\\n\";\n}\ntemplate<typename T1,typename T2,typename T3> inline void print3(T1 a, T2 b, T3 c){\n\tcout << \"debug: \" << a << \" \" << b << \" \" << c << \"\\n\";\n}\nvoid mark() {cout << \"#\" << \"\\n\";}\nll pcount(ll x) {return __builtin_popcountll(x);}\nconst int mod = 1e9 + 7;\n//const int mod = 998244353;\n\ntypedef vector<vector<double>> mat;\n\nmat matinv(mat a){\n int n = a.size();\n mat b(n,vector<double>(n*2));\n rep(i,n){\n rep(j,n){\n b[i][j] = a[i][j];\n }\n }\n rep(i,n) b[i][n+i] = 1;\n rep(i,n){\n int pivot = i;\n for(int j=i; j<n; j++){\n if(abs(b[j][i]) > abs(b[pivot][i])) pivot = j;\n }\n if(abs(b[pivot][i]) < eps){\n b.clear();\n return b;\n }\n swap(b[i], b[pivot]);\n for(int j=i+1; j<2*n; j++) b[i][j] /= b[i][i];\n rep(j,n){\n if(i != j){\n for(int k=i+1; k<2*n; k++) b[j][k] -= b[j][i] * b[i][k];\n }\n }\n }\n mat ret(n,vector<double>(n));\n rep(i,n) rep(j,n) ret[i][j] = b[i][n+j];\n return ret;\n}\n\nmat matmul(mat &a, mat &b){\n mat c(a.size(), vector<double>(b[0].size()));\n for(int i=0; i<a.size(); i++){\n for(int k=0; k<b.size(); k++){\n for(int j=0; j<b[0].size(); j++){\n c[i][j] += a[i][k] * b[k][j];\n }\n }\n }\n return c;\n}\n\nint main(){\n while(1){\n int d; cin >> d;\n if(!d) break;\n vector<double> f(d+3);\n rep(i,d+3) cin >> f[i];\n bool det = false;\n int ans = -1;\n rep(i,d+3){\n rep(j,i){\n mat a(d+1,vector<double>(d+1));\n mat b(d+1,vector<double>(1));\n int id = 0;\n rep(k,d+3){\n if(k == j || k == i) continue;\n b[id][0] = f[k];\n rep(l,d+1){\n a[id][l] = pow(k,l);\n }\n id++;\n }\n // rep(k,d+1){\n // rep(l,d+1) cout << a[k][l] << \" \";\n // cout << \"\\n\";\n // }\n mat inv = matinv(a);\n // rep(k,d+1){\n // rep(l,d+1) cout << inv[k][l] << \" \";\n // cout << \"\\n\";\n // }\n // rep(k,d+1) cout << b[k][0] << \" \";\n // cout << \"\\n\";\n // cout << \"\\n\";\n if(inv.empty()) continue;\n mat res = matmul(inv,b);\n auto val = [&](int t) -> double {\n double ret = 0.0;\n rep(k,d+1) ret += res[k][0] * pow(t,k);\n return ret;\n };\n // rep(k,d+1) cout << res[k][0] << \" \";\n // cout << \"\\n\";\n bool ff = abs(val(i) - f[i]) > eps;\n bool gg = abs(val(j) - f[j]) > eps;\n if(ff != gg){\n det = true;\n if(ff) ans = i;\n else ans = j;\n break;\n }\n }\n if(det) break;\n }\n cout << ans << \"\\n\";\n }\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3428, "score_of_the_acc": -0.367, "final_rank": 14 }, { "submission_id": "aoj_1328_4943483", "code_snippet": "#include <bits/stdc++.h>\n//#include <atcoder/all>\n//using namespace atcoder;\nconst double pi = 3.141592653589793238462643383279;\nusing namespace std;\n//typedef\n//------------------------------------------\ntypedef vector<int> VI;\ntypedef vector<VI> VVI;\ntypedef vector<string> VS;\ntypedef pair<int, int> PII;\ntypedef pair<long long, long long> PLL;\ntypedef pair<int, PII> TIII;\ntypedef long long LL;\ntypedef unsigned long long ULL;\ntypedef vector<LL> VLL;\ntypedef vector<VLL> VVLL;\n\n//container util\n//------------------------------------------\n#define ALL(a) (a).begin(), (a).end()\n#define RALL(a) (a).rbegin(), (a).rend()\n#define PB push_back\n#define MP make_pair\n#define SZ(a) int((a).size())\n#define SQ(a) ((a) * (a))\n#define EACH(i, c) for (typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)\n#define EXIST(s, e) ((s).find(e) != (s).end())\n#define SORT(c) sort((c).begin(), (c).end())\n\n//repetition\n//------------------------------------------\n#define FOR(i, s, n) for (int i = s; i < (int)n; ++i)\n#define REP(i, n) FOR(i, 0, n)\n#define MOD 1000000007\n\n#define rep(i, a, b) for (int i = a; i < (b); ++i)\n#define trav(a, x) for (auto &a : x)\n#define all(x) x.begin(), x.end()\n#define sz(x) (int)(x).size()\n\ntypedef long long ll;\ntypedef pair<int, int> pii;\ntypedef vector<int> vi;\n\n#define chmin(x, y) x = min(x, y)\n#define chmax(x, y) x = max(x, y)\nconst double EPS = 1e-3, PI = acos(-1);\n//ここから編集\nbool eq(double a, double b){ return fabs(a-b) < EPS; }\nlong double lagrange_interpolation(vector<long double> xi, vector<long double> fi, long double x){\n int n = xi.size()-1;\n long double ret = 0.0;\n for(int i=0; i<=n; i++){\n long double t = 1.0;\n for(int j=0; j<=n; j++){\n if(i == j) continue;\n t *= (x-xi[j])/(xi[i]-xi[j]);\n }\n ret += t*fi[i];\n }\n return ret;\n}\n\nint main()\n{\n cin.tie(0);\n ios::sync_with_stdio(false);\n cout << fixed << setprecision(6);\n\n int d;\n while(cin >> d, d){\n\n vector<long double> v(d+3);\n vector<long double> x(d+3);\n REP(i,d+3) {\n cin >> v[i];\n x[i] = i;\n }\n vector<bool> ok(8, false);\n int ans = 0;\n for(int i=0; i<=d+2; i++){\n for(int j=0; j<=d+2; j++){\n vector<long double> xi, fi;\n for(int k=0; k<=d+2; k++){\n if(i == k || j == k) continue;\n xi.push_back(k);\n fi.push_back(v[k]);\n }\n if(eq(lagrange_interpolation(xi, fi, i), v[i])) ok[i]=true;\n if(eq(lagrange_interpolation(xi, fi, j), v[j])) ok[j]=true;\n }\n } \n for(int i=0; i<=d+2; i++) if(!ok[i]) cout << i << endl;\n }\n \n return 0;\n}", "accuracy": 1, "time_ms": 20, "memory_kb": 3224, "score_of_the_acc": -0.2239, "final_rank": 9 }, { "submission_id": "aoj_1328_4908086", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\n\n//container utill\n#define ALL(v) (v).begin(),(v).end()\n#define CR [](auto element1, auto element2){return element1>element2;}\n#define LB lower_bound\n#define UP upper_bound\n#define PB push_back\n#define MP make_pair\n#define MT make_tuple\n\nusing LD = long double;\nconst LD EPS = 1e-10;\nconst LD R_EPS = 1e-3;\n\ntemplate<class T> struct Matrix {\n vector<vector<T> > val;\n Matrix(int n, int m, T x = 0) : val(n, vector<T>(m, x)) {}\n void init(int n, int m, T x = 0) {val.assign(n, vector<T>(m, x));}\n size_t size() const {return val.size();}\n inline vector<T>& operator [] (int i) {return val[i];}\n};\n\ntemplate<class T> ostream& operator << (ostream& s, Matrix<T> A) {\n s << endl; \n for (int i = 0; i < A.size(); ++i) {\n for (int j = 0; j < A[i].size(); ++j) {\n s << A[i][j] << \", \";\n }\n s << endl;\n }\n return s;\n}\n\ntemplate<class T> Matrix<T> operator * (Matrix<T> A, Matrix<T> B) {\n Matrix<T> R(A.size(), B[0].size());\n for (int i = 0; i < A.size(); ++i)\n for (int j = 0; j < B[0].size(); ++j)\n for (int k = 0; k < B.size(); ++k)\n R[i][j] += A[i][k] * B[k][j];\n return R;\n}\n\ntemplate<class T> Matrix<T> pow(Matrix<T> A, long long n) {\n Matrix<T> R(A.size(), A.size());\n for (int i = 0; i < A.size(); ++i) R[i][i] = 1;\n while (n > 0) {\n if (n & 1) R = R * A;\n A = A * A;\n n >>= 1;\n }\n return R;\n}\n\ntemplate<class T> int GaussJordan(Matrix<T> &A, bool is_extended = false) {\n int m = A.size(), n = A[0].size();\n int rank = 0;\n for (int col = 0; col < n; ++col) {\n if (is_extended && col == n-1) break;\n int pivot = -1;\n T ma = EPS;\n for (int row = rank; row < m; ++row) {\n if (abs(A[row][col]) > ma) {\n ma = abs(A[row][col]);\n pivot = row;\n }\n }\n if (pivot == -1) continue;\n swap(A[pivot], A[rank]);\n auto fac = A[rank][col];\n for (int col2 = 0; col2 < n; ++col2) A[rank][col2] /= fac;\n for (int row = 0; row < m; ++row) {\n if (row != rank && abs(A[row][col]) > EPS) {\n auto fac = A[row][col];\n for (int col2 = 0; col2 < n; ++col2) {\n A[row][col2] -= A[rank][col2] * fac;\n }\n }\n }\n ++rank;\n }\n return rank;\n}\n\ntemplate<class T> vector<T> linear_equation(Matrix<T> A, vector<T> b) {\n // extended\n int m = A.size(), n = A[0].size();\n Matrix<T> M(m, n + 1);\n for (int i = 0; i < m; ++i) {\n for (int j = 0; j < n; ++j) M[i][j] = A[i][j];\n M[i][n] = b[i];\n }\n int rank = GaussJordan(M, true);\n \n // check if it has no solution\n vector<T> res;\n for (int row = rank; row < m; ++row) if (abs(M[row][n]) > EPS) return res;\n\n // answer\n res.assign(n, 0);\n for (int i = 0; i < rank; ++i) res[i] = M[i][n];\n return res;\n}\n\nvoid nextind(int d, vector<int> &ind){\n int s=ind.size();\n while(ind[s-1]==(d+2)+(s-(d+1))){\n auto t=ind.end();\n t--;\n ind.erase(t);\n s=ind.size();\n }\n ind[s-1]++;\n while(s<d+1){\n ind.PB(ind[s-1]+1);\n s=ind.size();\n }\n}\n\nvoid solve(int d){\n LD f[d+3];\n int i,j,k;\n for(i=0; i<d+3; i++){\n cin >> f[i];\n }\n vector<vector<LD> > coef;\n vector<int> ind;\n for(i=0; i<d; i++){\n ind.PB(i);\n }\n ind.PB(d-1);\n vector<vector<int> > indind;\n for(i=0; i<((d+3)*(d+2))/2; i++){\n nextind(d,ind);\n indind.PB(ind);\n Matrix<LD> A(d+1,d+1);\n for(j=0; j<d+1; j++){\n int ele=1;\n for(k=0; k<d+1; k++){\n A[j][k]=ele;\n ele*=ind[j];\n }\n }\n vector<LD> b;\n for(j=0; j<d+1; j++){\n b.PB(f[ind[j]]);\n }\n coef.PB(linear_equation(A,b));\n }\n vector<bool> safe(d+3,0);\n bool now;\n for(i=0; i<((d+3)*(d+2))/2; i++){\n now=0;\n for(j=i+1; j<((d+3)*(d+2))/2; j++){\n for(k=0; k<d+1; k++){\n if(abs(coef[i][k]-coef[j][k])>R_EPS) break;\n }\n if(k<d+1) continue;\n now=1;\n }\n if(now==1){\n for(j=0; j<d+1; j++){\n safe[indind[i][j]]=1;\n }\n }\n }\n for(i=0; i<d+3; i++){\n if(safe[i]==0) break;\n }\n cout << i << endl;\n}\n\nint main(){\n int d;\n while(cin >> d, d>0){\n solve(d);\n }\n return 0;\n}", "accuracy": 1, "time_ms": 20, "memory_kb": 3108, "score_of_the_acc": -0.0909, "final_rank": 4 }, { "submission_id": "aoj_1328_4699696", "code_snippet": "#include <iostream>\n#include <vector>\n#include <array>\n#include <list>\n#include <queue>\n#include <stack>\n#include <set>\n#include <map>\n#include <unordered_map>\n#include <unordered_set>\n#include <utility>\n#include <string>\n#include <sstream>\n#include <algorithm>\n#include <random>\n#include <cstdio>\n#include <cstdlib>\n#include <cstring>\n#include <ctime>\n#include <cmath>\n#include <cassert>\n#include <climits>\n#include <bitset>\n#include <functional>\n#include <iomanip>\n#include <random>\n\n#define FOR_LT(i, beg, end) for (int i = (int)(beg); i < (int)(end); i++)\n#define FOR_LE(i, beg, end) for (int i = (int)(beg); i <= (int)(end); i++)\n#define FOR_DW(i, beg, end) for (int i = (int)(beg); (int)(end) <= i; i--)\n#define REP(n) for (int repeat_index = 0; repeat_index < (int)n; repeat_index++)\n\nusing namespace std;\n//=============================================================================\n// Matrix\n//=============================================================================\n\ntemplate <typename T>\nstruct Matrix {\n Matrix(size_t m, size_t n, T x = 0) : val_(vector<vector<T>>(m, vector<T>(n, x))) {}\n\n void Init(size_t m, size_t n, T x = 0)\n {\n this->val_.assign(vector<vector<T>>(m, vector<T>(n, x)));\n }\n\n size_t size() {\n return this->val_.size();\n }\n\n inline vector<T>& operator [] (int i) { return this->val_[i]; }\n\n void print() {\n for (auto& v : val_) {\n for (auto& t : v) {\ncout << t << \" \";\n }\n cout << endl;\n }\n }\n\n vector<vector<T>> val_;\n};\n\n//=============================================================================\n// REAL\n//=============================================================================\nstatic const double kEPS = 1e-10;\n\ntemplate<class T>\nint gauss_jordan(Matrix<T>& A, bool is_extended = false) {\n int m = A.size(), n = A[0].size();\n\n int rank = 0;\n for (int col = 0; col < n; ++col) {\n if (is_extended && col == n - 1) break;\n\n int pivot = -1;\n T ma = kEPS;\n for (int row = rank; row < m; ++row) {\n if (abs(A[row][col]) > ma) {\n ma = abs(A[row][col]);\n pivot = row;\n }\n }\n if (pivot == -1) continue;\n\n swap(A[pivot], A[rank]);\n {\n auto fac = A[rank][col];\n\n for (int col2 = 0; col2 < n; ++col2) {\n A[rank][col2] /= fac;\n }\n }\n\n for (int row = 0; row < m; ++row) {\n if (row != rank && abs(A[row][col]) > kEPS) {\n auto fac = A[row][col];\n for (int col2 = 0; col2 < n; ++col2) {\n A[row][col2] -= A[rank][col2] * fac;\n }\n }\n }\n\n ++rank;\n }\n\n return rank;\n}\n\n\ntemplate<class T> vector<T> linear_equation(Matrix<T> A, vector<T> b)\n{\n int m = A.size(), n = A[0].size();\n\n Matrix<T> M(m, n + 1);\n for (int i = 0; i < m; ++i) {\n for (int j = 0; j < n; ++j) M[i][j] = A[i][j];\n M[i][n] = b[i];\n }\n int rank = gauss_jordan(M, true);\n\n vector<T> res;\n for (int row = rank; row < m; ++row) {\n if (abs(M[row][n]) > kEPS) return res;\n }\n\n res.assign(n, 0);\n for (int i = 0; i < rank; ++i) res[i] = M[i][n];\n return res;\n}\n\nvoid solve(int d)\n{\n vector<double> y(d + 3);\n FOR_LT(i, 0, d + 3) {\n cin >> y[i];\n }\n\n vector<int> s;\n vector<vector<int>> sc;\n vector<vector<double>> v;\n FOR_LT(i, 0, (1 << (d + 3))) {\n {\n int c = 0;\n FOR_LT(j, 0, (d + 3)) {\n if (i & (1 << j)) c++;\n }\n if (c != d + 1) continue;\n }\n\n\n Matrix<double> A(d + 1, d + 1);\n vector<double> b(d + 1);\n\n int c = 0;\n FOR_LT(j, 0, d + 3) {\n if (i & (1 << j)) {\n FOR_LE(k, 0, d) {\n A[c][k] = pow(j, k);\n }\n b[c] = y[j];\n c++;\n }\n\t\t}\n\n vector<double> ans = linear_equation(A, b);\n assert(!ans.empty());\n\n {\n bool is_same = false;\n FOR_LT(j, 0, s.size()) {\n bool is_c_same = true;\n FOR_LT(k, 0, d + 1) {\n if (abs(v[j][k] - ans[k]) >= 10E-4) {\n is_c_same = false;\n break;\n }\n }\n if (is_c_same) {\n is_same = true;\n s[j]++;\n FOR_LT(k, 0, d + 3) {\n if (i & (1 << k)) {\n sc[j][k]++;\n }\n }\n break;\n }\n }\n if (!is_same) {\n s.push_back(1);\n v.push_back(ans);\n vector<int> scc(d + 3);\n FOR_LT(k, 0, d + 3) {\n if (i & (1 << k)) {\n scc[k]++;\n }\n }\n sc.push_back(scc);\n }\n }\n\t}\n\n int argmax = -1;\n int maxv = 0;\n FOR_LT(i, 0, s.size()) {\n if (maxv < s[i]) {\n argmax = i;\n maxv = s[i];\n }\n }\n\n FOR_LT(i, 0, d + 3) {\n if (sc[argmax][i] == 0) {\n cout << i << endl;\n return;\n }\n }\n}\n\nint main()\n{\n\tios_base::sync_with_stdio(false);\n\tcin.tie(NULL);\n\tcout << fixed << setprecision(20);\n\n\twhile (true) {\n\t\tint d; cin >> d;\n\t\tif (d == 0) break;\n\t\tsolve(d);\n\t}\n\n\treturn 0;\n}", "accuracy": 1, "time_ms": 20, "memory_kb": 3344, "score_of_the_acc": -0.3616, "final_rank": 13 }, { "submission_id": "aoj_1328_4368376", "code_snippet": "#include <algorithm>\n#include <bitset>\n#include <complex>\n#include <deque>\n#include <exception>\n#include <fstream>\n#include <functional>\n#include <iomanip>\n#include <ios>\n#include <iosfwd>\n#include <iostream>\n#include <istream>\n#include <iterator>\n#include <limits>\n#include <list>\n#include <locale>\n#include <map>\n#include <memory>\n#include <new>\n#include <numeric>\n#include <ostream>\n#include <queue>\n#include <set>\n#include <sstream>\n#include <stack>\n#include <stdexcept>\n#include <streambuf>\n#include <string>\n#include <typeinfo>\n#include <utility>\n#include <valarray>\n#include <vector>\n#include <climits>\n#include <cstring>\n#include <cassert>\n\n#define rep(i, m, n) for(int i=int(m);i<int(n);i++)\n#define all(c) begin(c),end(c)\n\ntemplate<typename T1, typename T2>\ninline void chmin(T1 &a, T2 b) { if (a > b) a = b; }\n\ntemplate<typename T1, typename T2>\ninline void chmax(T1 &a, T2 b) { if (a < b) a = b; }\n\ntypedef long long int ll;\nusing ll = long long int;\nusing ull = long long unsigned int;\nusing Int = long long int;\nusing uInt = long long unsigned int;\nusing Double = long double;\nusing namespace std;\n#define INF (1 << 30) - 1\n#define INFl (ll)5e15\n#define DEBUG 0\n#define dump(x) cerr << #x << \" = \" << (x) << endl\n#define MOD 1000000007\n\n\n//edit\nconst double EPS = 1E-7;\ntypedef vector<Double> vec;\ntypedef vector<vec> mat;\n\n//Ax = b を解く。Aは正方行列\n// 解がないか、一意でない場合は長さ0の配列を返す\nvec gauss_jordan(const mat &A, const vec &b) {\n int n = A.size();\n mat B(n, vec(n + 1));\n for (int i = 0; i < n; i++)\n for (int j = 0; j < n; j++) B[i][j] = A[i][j];\n //行列Aの後ろにbを並べ、同時に処理する。\n for (int i = 0; i < n; i++) B[i][n] = b[i];\n\n for (int i = 0; i < n; i++) {\n //注目している変数の係数の絶対値が大きい式をi番目に持ってくる\n int pivot = i;\n for (int j = i; j < n; j++) {\n if (abs(B[j][i]) > abs(B[pivot][i])) pivot = j;\n }\n swap(B[i], B[pivot]);\n\n //解がないか一意でない\n if (abs(B[i][i]) < EPS) return vec();\n\n //注目している変数の係数を1にする\n for (int j = i + 1; j <= n; j++) B[i][j] /= B[i][i];\n for (int j = 0; j < n; j++) {\n if (i != j) {\n //j番目の式からi番目の変数を削除\n for (int k = i + 1; k <= n; k++) B[j][k] -= B[j][i] * B[i][k];\n }\n }\n }\n vec x(n);\n for (int i = 0; i < n; i++) x[i] = B[i][n];\n return x;\n}\n\n\nclass Solve {\npublic:\n Int n;\n vector<Double> v;\n\n vector<Double> calc(vector<int> idx) {\n vector<vector<Double>> A(idx.size(), vector<Double>(idx.size()));\n vector<Double> b(idx.size());\n\n for (int i = 0; i < idx.size(); ++i) {\n for (int j = 0; j < idx.size(); ++j) {\n Double tmp = pow(idx[i], j);\n A[i][j] = tmp;\n }\n }\n for (int i = 0; i < idx.size(); ++i) {\n b[i] = v[idx[i]];\n }\n\n auto ret = gauss_jordan(A, b);\n return ret;\n }\n\n bool eq_vec(vector<Double> &v1, vector<Double> &v2) {\n for (int i = 0; i < v1.size(); ++i) {\n if (abs(v1[i] - v2[i]) > 0.1) {\n return false;\n }\n }\n return true;\n }\n\n bool is_wrong(int p) {\n vector<int> idx1, idx2;\n for (int i = 0; i < v.size(); ++i) {\n if (i != p) {\n idx1.push_back(i);\n idx2.push_back(i);\n }\n }\n idx1.pop_back();\n idx2.erase(idx2.begin());\n\n auto x1 = calc(idx1);\n auto x2 = calc(idx2);\n\n bool ret = eq_vec(x1, x2);\n return ret;\n }\n\n bool solve() {\n cin >> n;\n if (n == 0) return false;\n v.resize(n + 3);\n rep(i, 0, n + 3) cin >> v[i];\n\n int ans = -1;\n for (int i = 0; i <= n + 2; ++i) {\n if (is_wrong(i)) {\n ans = i;\n }\n }\n cout << ans << endl;\n\n return true;\n }\n};\n\n\nint main() {\n cin.tie(0);\n ios::sync_with_stdio(false);\n cout << fixed << setprecision(10);\n// std::ifstream in(\"input.txt\");\n// std::cin.rdbuf(in.rdbuf());\n\n while (Solve().solve());\n\n\n return 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3392, "score_of_the_acc": -0.3257, "final_rank": 11 }, { "submission_id": "aoj_1328_4267647", "code_snippet": "#define _CRT_SECURE_NO_WARNINGS\n#define _USE_MATH_DEFINES\n\n#pragma comment (linker, \"/STACK:526000000\")\n\n#include \"bits/stdc++.h\"\n\nusing namespace std;\ntypedef string::const_iterator State;\n#define eps 1e-5L\n#define MAX_MOD 1000000007LL\n#define GYAKU 500000004LL\n#define seg_size 262144LL\n#define MOD 998244353LL\n#define pb push_back\n#define mp make_pair\ntypedef long long ll;\n#define REP(a,b) for(long long (a) = 0;(a) < (b);++(a))\n#define ALL(x) (x).begin(),(x).end()\n\n// geometry library\n\ntypedef complex<long double> Point;\ntypedef pair<complex<long double>, complex<long double>> Line;\n\ntypedef struct Circle {\n complex<long double> center;\n long double r;\n}Circle;\n\n//内積、 dot(a,b) = |a||b|cos()\nlong double dot(Point a, Point b) {\n return (a.real() * b.real() + a.imag() * b.imag());\n}\n//外積、cross(a,b) = |a||b|sin()\nlong double cross(Point a, Point b) {\n return (a.real() * b.imag() - a.imag() * b.real());\n}\n\n//線分と点の距離\nlong double Dist_Line_Point(Line a, Point b) {\n if (dot(a.second - a.first, b - a.first) < eps) return abs(b - a.first);\n if (dot(a.first - a.second, b - a.second) < eps) return abs(b - a.second);\n return abs(cross(a.second - a.first, b - a.first)) / abs(a.second - a.first);\n}\n\n//線分の交差判定\nint is_intersected_ls(Line a, Line b) {\n return (cross(a.second - a.first, b.first - a.first) * cross(a.second - a.first, b.second - a.first) < 0) &&\n (cross(b.second - b.first, a.first - b.first) * cross(b.second - b.first, a.second - b.first) < 0);\n}\n\n//線分の交点\nPoint intersection_l(Line a, Line b) {\n Point da = a.second - a.first;\n Point db = b.second - b.first;\n return a.first + da * cross(db, b.first - a.first) / cross(db, da);\n}\n\n//線分と線分の距離\nlong double Dist_Line_Line(Line a, Line b) {\n if (is_intersected_ls(a, b) == 1) {\n return 0;\n }\n return min({ Dist_Line_Point(a,b.first), Dist_Line_Point(a,b.second),Dist_Line_Point(b,a.first),Dist_Line_Point(b,a.second) });\n}\n\n//円と円の交点\npair<Point, Point> intersection_Circle_Circle(Circle a, Circle b) {\n long double dist = abs(a.center - b.center);\n assert(dist <= eps + a.r + b.r);\n assert(dist+eps >= abs(a.r - b.r));\n Point target = b.center - a.center;\n long double pointer = target.real() * target.real() + target.imag() * target.imag();\n long double aa = pointer + a.r * a.r - b.r * b.r;\n aa /= 2.0L;\n Point l{ (aa * target.real() + target.imag() * sqrt(pointer * a.r * a.r - aa * aa))/pointer,\n (aa* target.imag() - target.real() * sqrt(pointer * a.r * a.r - aa * aa)) / pointer};\n Point r{ (aa * target.real() - target.imag() * sqrt(pointer * a.r * a.r - aa * aa)) / pointer,\n (aa * target.imag() + target.real() * sqrt(pointer * a.r * a.r - aa * aa)) / pointer };\n r = r + a.center;\n l = l + a.center;\n return mp(l, r);\n}\n\n//end of geometry\n\n\n\nvoid init() {\n iostream::sync_with_stdio(false);\n cout << fixed << setprecision(20);\n}\n\nunsigned long xor128() {\n static unsigned long x = 123456789, y = 362436069, z = 521288629, w = 88675123;\n unsigned long t = (x ^ (x << 11));\n x = y; y = z; z = w;\n return (w = (w ^ (w >> 19)) ^ (t ^ (t >> 8)));\n}\n\n#define int ll\n\ntemplate<typename A>\nvector<A> solve_algebra_equations(vector<vector<A>> now) {\n for (int q = 0; q < now.size(); ++q) {\n if (now[q][q] == 0) {\n for (int j = q + 1; j < now.size(); ++j) {\n if (now[j][q] != 0) {\n swap(now[j], now[q]);\n break;\n }\n }\n }\n for (int j = now[q].size() - 1; j >= 0; --j) {\n now[q][j] /= now[q][q];\n }\n for (int j = 0; j < now.size();++j) {\n if (j == q) continue;\n A val = now[j][q];\n for (int t = 0; t < now[j].size(); ++t) {\n now[j][t] -= val * now[q][t];\n }\n }\n }\n vector<A> ans;\n REP(i, now.size()) {\n ans.push_back(now[i].back());\n }\n return ans;\n}\n\nvoid solve(){\n while (true) {\n int d;\n cin >> d;\n if (d == 0) return;\n vector<long double> inputs;\n REP(i, d + 3) {\n long double a;\n cin >> a;\n inputs.push_back(a);\n }\n int ans = -1;\n REP(i, inputs.size()) {\n for (int q = i + 1; q < inputs.size(); ++q) {\n vector<vector<long double>> neko;\n vector<vector<long double>> check;\n REP(j, inputs.size()) {\n vector<long double> now;\n now.push_back(inputs[j]);\n long double geko = 1;\n for (int t = 0; t <= d; ++t) {\n now.push_back(geko);\n geko *= j;\n }\n reverse(ALL(now));\n if (i == j || q == j) {\n check.push_back(now);\n }\n else {\n neko.push_back(now);\n }\n }\n vector<long double> geko = solve_algebra_equations<long double>(neko);\n REP(t, 2) {\n long double now_check = 0;\n for (int q = 0; q < geko.size(); ++q) {\n now_check += geko[q] * check[t][q];\n }\n if (abs(now_check - check[t].back()) < eps) {\n if (t == 0) {\n ans = q;\n }\n else {\n ans = i;\n }\n break;\n }\n }\n }\n if (ans != -1) break;\n }\n cout << ans << endl;\n }\n}\n\n#undef int\nint main() {\n init();\n solve();\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3204, "score_of_the_acc": -0.1101, "final_rank": 5 }, { "submission_id": "aoj_1328_4189068", "code_snippet": "#include <bits/stdc++.h>\ndouble lagrange_interpolation(std::vector<double> x, std::vector<double> y, double T) {\n const long long n = x.size() - 1;\n double ret = 0;\n for(int i=0;i<n+1;++i) {\n double t = 1;\n for(int j=0;j<n+1;++j) {\n if(i == j) continue;\n t *= T-x[j];\n t /= x[i]-x[j];\n }\n ret += t * y[i];\n }\n return ret;\n}\nstd::vector<double> lagrange_interpolation(std::vector<double> x, std::vector<double> y) {\n const long long n = x.size() - 1;\n const double eps = 1e-6;\n for(int i=0;i<n+1;++i) {\n double t = 1;\n for(int j=0;j<n+1;++j) {\n if(i != j) t *= x[i]-x[j];\n }\n y[i] /= t;\n }\n long long cur = 0, nxt = 1;\n std::vector<std::vector<double>> dp(2, std::vector<double>(n+2));\n dp[0][0] = -1 * x[0], dp[0][1] = 1;\n for(int i=1;i<n+1;++i) {\n for(int j=0;j<n+2;++j) {\n dp[nxt][j] = dp[cur][j] * -1 * x[i];\n if(j >= 1) dp[nxt][j] += dp[cur][j-1];\n }\n std::swap(nxt, cur);\n }\n std::vector<double> ret(n+1);\n for(int i=0;i<n+1;++i) {\n if(y[i]==0) continue;\n if(abs(x[i]) < eps) {\n for(int j=0;j<n+1;++j) ret[j] += dp[cur][j+1] * y[i];\n } else {\n ret[0] -= dp[cur][0] / x[i] * y[i];\n double pre = -1 * dp[cur][0] / x[i];\n for(int j=1;j<n+1;++j) {\n ret[j] -= (dp[cur][j] - pre) / x[i] * y[i];\n pre = -1 * (dp[cur][j] - pre) / x[i];\n }\n }\n }\n return ret;\n}\nusing namespace std;\nusing ll = long long;\nusing ld = long double;\nusing P = pair<int, int>;\nusing vi = vector<int>;\nusing vvi = vector<vector<int>>;\nusing vll = vector<ll>;\nusing vvll = vector<vector<ll>>;\nconst double eps = 1e-4;\nconst ll MOD = 1000000007;\nconst int INF = 1000000000;\nconst ll LINF = 1ll<<50;\ntemplate<typename T>\nvoid printv(const vector<T>& s) {\n for(int i=0;i<(int)(s.size());++i) {\n cout << s[i];\n if(i == (int)(s.size())-1) cout << endl;\n else cout << \" \";\n }\n}\ntemplate<typename T1, typename T2>\nostream& operator<<(ostream &os, const pair<T1, T2> p) {\n os << p.first << \":\" << p.second;\n return os;\n}\nbool operator== (const vector<double> &v1, const vector<double> &v2) {\n if((int)(v1.size()) == (int)(v2.size())) {\n bool res = true;\n for(int i=0;i<(int)(v1.size());++i) {\n res &= abs(v1[i] - v2[i]) < eps;\n }\n return res;\n } else {\n return false;\n }\n}\ndouble err (const vector<double> &v1, const vector<double> &v2) {\n double res = 0;\n for(int i=0;i<(int)(v1.size());++i) {\n res += abs(v1[i] - v2[i]);\n }\n return res;\n}\nvoid solve(int d) {\n vector<double> x, y;\n for(int i=0;i<d+3;++i) {\n x.push_back(i);\n double tmp; cin >> tmp;\n y.push_back(tmp);\n }\n vector<pair<double, int>> vp;\n for(int i=0;i<d+3;++i) {\n vector<vector<double>> v;\n for(int j=0;j<d+3;++j) {\n if(j == i) continue;\n vector<double> nx, ny;\n for(int k=0;k<d+3;++k) {\n if(k == i || k == j) continue;\n nx.push_back(x[k]);\n ny.push_back(y[k]);\n }\n v.emplace_back(lagrange_interpolation(nx, ny));\n }\n bool ok = true;\n double ma = 0;\n for(int j=0;j<d+2;++j) {\n ma = max(ma, err(v[j], v[(j+1)%(d+2)]));\n }\n vp.push_back({ma, i});\n }\n sort(vp.begin(), vp.end());\n cout << vp[0].second << endl;\n}\nint main() {\n cin.tie(0);\n ios::sync_with_stdio(false);\n cout << fixed << setprecision(10);\n while(1) {\n int d; cin >> d;\n if(d == 0) break;\n solve(d);\n }\n}", "accuracy": 1, "time_ms": 20, "memory_kb": 3220, "score_of_the_acc": -0.2193, "final_rank": 8 }, { "submission_id": "aoj_1328_3992060", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nusing LL = long long;\n\n#define fs first\n#define sc second\n\nconst LL MOD = 1e9+7;\nconst double EPS = 1e-10;\n\ntemplate<class T>bool chmax(T &a, const T &b) {\n if (a < b) { a = b; return true; }\n return false;\n}\ntemplate<class T>bool chmin(T &a, const T &b) {\n if (b < a) { a = b; return true; }\n return false;\n}\n\n// std::vector Declaration\ntemplate<typename T>\nvector<T> make_v(size_t a) { return vector<T>(a); }\ntemplate<typename T,typename... Ts>\nauto make_v(size_t a,Ts... ts) {\n return vector<decltype(make_v<T>(ts...))>(a,make_v<T>(ts...));\n}\n\n// std::vector Declaration and Initialization\ntemplate<typename T>\nvector<T> make_vector(size_t a, T x) { return vector<T>(a, x); }\ntemplate<typename T, typename U, typename... Ts>\nauto make_vector(size_t a, U b, Ts... ts) {\n return vector<decltype(make_vector<T>(b,ts...))>(a,make_vector<T>(b,ts...));\n}\n\n// std::vector Input\ntemplate<typename T>\nistream& operator>>(istream& is, vector<T>& v) {\n for (auto &e : v) {\n is >> e;\n }\n return is;\n}\n\n// std::vector Debug\ntemplate<typename T>\nostream& operator<<(ostream& os, const vector<T>& v) {\n os << \"[\";\n bool a = 1;\n for (auto e : v) {\n os << (a ? \"\" : \" \");\n os << e;\n a = 0;\n }\n os << \"]\";\n return os;\n}\n\n// std::array Debug\ntemplate<typename T, size_t n>\nostream& operator<<(ostream& os, const array<T, n>& v) {\n os << \"[\";\n bool a = 1;\n for (auto e : v) {\n os << (a ? \"\" : \" \");\n os << e;\n a = 0;\n }\n os << \"]\";\n return os;\n}\n\n// std::deque Debug\ntemplate<typename T>\nostream& operator<<(ostream& os, const deque<T>& d) {\n os << \"[\";\n bool a = 1;\n for (auto e : d) {\n os << (a ? \"\" : \" \");\n os << e;\n a = 0;\n }\n os << \"]\";\n return os;\n}\n\n// std::pair Debug\ntemplate<typename T, typename U>\nostream& operator<<(ostream& os, const pair<T, U>& p) {\n os << \"(\" << p.first << \" \" << p.second << \")\";\n return os;\n}\n\n// std::set Debug\ntemplate<typename T>\nostream& operator<<(ostream& os, const set<T>& st) {\n os << \"{\";\n bool a = 1;\n for (auto e : st) {\n os << (a ? \"\" : \" \");\n os << e;\n a = 0;\n }\n os << \"}\";\n return os;\n}\n\n// std::multiset Debug\ntemplate<typename T>\nostream& operator<<(ostream& os, const multiset<T>& st) {\n os << \"{\";\n bool a = 1;\n for (auto e : st) {\n os << (a ? \"\" : \" \");\n os << e;\n a = 0;\n }\n os << \"}\";\n return os;\n}\n\n// std::map Debug\ntemplate<typename T, typename U>\nostream& operator<<(ostream& os, const map<T, U>& mp) {\n os << \"{\";\n bool a = 1;\n for (auto e : mp) {\n os << (a ? \"\" : \" \");\n os << e.first << \":\" << e.second;\n a = 0;\n }\n os << \"}\";\n return os;\n}\n\n// std::tuple Debug\ntemplate<int N, class Tuple>\nvoid out(ostream& os, const Tuple& t){}\ntemplate<int N, class Tuple, class H, class ...Ts>\nvoid out(ostream& os, const Tuple& t) {\n if (N) os << \" \";\n os << get<N>(t);\n out<N+1,Tuple,Ts...>(os, t);\n}\ntemplate<class ...Ts>\nostream& operator<<(ostream& os, const tuple<Ts...>& t) {\n os << \"(\";\n out<0,tuple<Ts...>,Ts...>(os, t);\n os << \")\";\n return os;\n}\n\n// Debug\n#define DUMP(x) cerr<<#x<<\" = \"<<(x)<<endl\n\n// Weighted edge\ntemplate<typename T>\nstruct edge {\n int src, to;\n T cost;\n\n edge(int to, T cost) : src(-1), to(to), cost(cost) {}\n edge(int src, int to, T cost) : src(src), to(to), cost(cost) {}\n\n edge &operator=(const int &x) {\n to = x;\n return *this;\n }\n\n operator int() const { return to; }\n\n friend ostream& operator<<(ostream& os, const edge& e) {\n return os << \"(\" << e.src << \"->\" << e.to << \":\" << e.cost << \")\";\n }\n};\n\ntemplate<typename T>\nusing Edges = vector<edge<T>>;\ntemplate<typename T>\nusing WeightedGraph = vector<Edges<T>>;\nusing UnWeightedGraph = vector<vector<int>>;\ntemplate<typename T>\nusing MatrixGraph = vector<vector<T>>;\n\nusing Matrix = vector<vector<double>>;\n\npair<Matrix,Matrix> LUDecomposition(const Matrix& A) {\n // A: Squere Matirix\n int n = A.size();\n const double INF = numeric_limits<double>::max();\n Matrix L = make_v<double>(n, n),\n U = make_v<double>(n, n);\n\n for (int i = 0; i < n; ++i) {\n L[i][i] = 1;\n }\n\n for (int i = 0; i < n; ++i) {\n // L\n for (int j = 0; j < i; ++j) {\n double total = 0;\n for (int k = 0; k < j; ++k) {\n total += L[i][k] * U[k][j];\n }\n L[i][j] = (A[i][j] - total) / U[j][j];\n }\n\n // U\n for (int j = i; j < n; ++j) {\n double total = 0;\n for (int k = 0; k < i; ++k) {\n total += L[i][k] * U[k][j];\n }\n U[i][j] = A[i][j] - total;\n }\n }\n\n return make_pair(L, U);\n}\n\nusing Vector = vector<double>;\n\nVector solve(const Matrix& A, const Vector& b) {\n // A: Squere Matrix\n int n = A.size();\n Matrix L, U;\n tie(L, U) = LUDecomposition(A);\n\n Vector y(n);\n for (int i = 0; i < n; ++i) {\n double total = 0;\n for (int j = 0; j < i; ++j) {\n total += y[j] * L[i][j];\n }\n y[i] = b[i] - total;\n }\n\n Vector x(n);\n for (int i = n-1; i >= 0; --i) {\n double total = 0;\n for (int j = n-1; j > i; --j) {\n total += U[i][j] * x[j];\n }\n x[i] = (y[i] - total) / U[i][i];\n }\n\n return x;\n}\n\nint main()\n{\n while (true) {\n int d; cin >> d;\n if (d == 0) {\n break;\n }\n\n vector<double> v(d+3); cin >> v;\n vector<double> ans(d+3);\n\n for (int m = 0; m < d+3; ++m) {\n\n int check = (d+1+m) % (d+3);\n\n Matrix A = make_v<double>(d+1, d+1);\n for (int i = 0; i <= d; ++i) {\n for (int k = 0; k <= d; ++k) {\n A[i][k] = pow((i+m) % (d+3), k);\n }\n }\n\n Vector b(d+1);\n for (int i = 0; i <= d; ++i) {\n b[i] = v[(i+m) % (d+3)];\n }\n\n Vector a = solve(A, b);\n\n double sum = 0;\n for (int i = 0; i <= d; ++i) {\n sum += a[i] * pow(check, i);\n }\n\n ans[(check+1) % (d+3)] = abs(sum - v[check]);\n }\n\n cout << distance(ans.begin(), min_element(ans.begin(), ans.end())) << endl;\n }\n\n return 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3228, "score_of_the_acc": -0.1376, "final_rank": 6 }, { "submission_id": "aoj_1328_3839063", "code_snippet": "#include <iostream>\n#include <cmath>\n#include <vector>\n\nusing Bool = bool;\nusing Int = long long int;\nusing Real = long double;\ntemplate <class T>\nusing Vector = std::vector<T>;\n\nconstexpr Real EPS = 1e-3;\n\n// pol * (x - d)\nVector<Real> mul(Vector<Real> pol, Real d) {\n pol.insert(pol.begin(), 0);\n for (Int i = 0; i + 1 < pol.size(); ++i) {\n pol[i] -= pol[i + 1] * d;\n }\n return pol;\n}\n\n// pol / d\nVector<Real> divc(Vector<Real> pol, Real d) {\n for (auto& a : pol) a /= d;\n return pol;\n}\n\nVector<Real> add(Vector<Real> pol1, Vector<Real> pol2) {\n for (Int i = 0; i < pol1.size(); ++i) {\n pol1[i] += pol2[i];\n }\n return pol1;\n}\n\nReal apply(const Vector<Real> pol, Real x) {\n Real ret = 0, acc = 1;\n for (auto a : pol) {\n ret += a * acc;\n acc *= x;\n }\n return ret;\n}\n\nBool solve() {\n Int n;\n std::cin >> n;\n if (n == 0) return false;\n\n Vector<Real> v(n + 3);\n for (auto& a : v) std::cin >> a;\n\n for (Int i = 0; i <= n + 1; ++i) {\n // v[i]が偽と仮定\n Vector<Real> res(n + 1, 0);\n\n for (Int j = 0; j <= n + 1; ++j) {\n if (j == i) continue;\n\n Vector<Real> pol(1, v[j]);\n for (Int k = 0; k <= n + 1; ++k) {\n if (k == i || k == j) continue;\n pol = mul(pol, k); // * (x - k)\n pol = divc(pol, j - k); // / (j - k)\n }\n res = add(res, pol);\n }\n\n Real err = apply(res, Real(n + 2)) - v[n + 2];\n if (std::abs(err) < EPS) {\n std::cout << i << std::endl;\n return true;\n }\n }\n\n std::cout << n + 2 << std::endl;\n return true;\n}\n\nint main() {\n while (solve()) {}\n return 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3164, "score_of_the_acc": -0.0642, "final_rank": 2 }, { "submission_id": "aoj_1328_3814750", "code_snippet": "#include <iostream>\n#include <vector>\n#include <cmath>\n#include <algorithm>\nusing namespace std;\n#pragma warning (disable: 4996)\n\nlong double Gauss(vector<vector<long double>>a, vector<long double>b) {\n\tint N = a.size();\n\tif (N == 1) return b[0] / a[0][0];\n\n\tvector<vector<long double>> c = a;\n\tvector<long double> d = b;\n\tfor (int i = 0; i < N; i++) {\n\t\tfor (int j = 0; j < N; j++) c[i][j] = a[i][j] / a[i][0];\n\t\td[i] = b[i] / a[i][0];\n\t}\n\n\tvector<vector<long double>> e(N - 1, vector<long double>(N - 1, 0));\n\tvector<long double> f(N - 1, 0);\n\n\tfor (int i = 0; i < N - 1; i++) {\n\t\tfor (int j = 0; j < N - 1; j++) {\n\t\t\te[i][j] = c[i + 1][j + 1] - c[i][j + 1];\n\t\t}\n\t\tf[i] = d[i + 1] - d[i];\n\t}\n\n\treturn Gauss(e, f);\n}\n\nvector<long double> Gauss_Erase(vector<vector<long double>> e, vector<long double>f) {\n\tint N = e.size(); vector<long double> res;\n\n\tfor (int i = 0; i < N; i++) {\n\t\tlong double T = Gauss(e, f);\n\t\tres.push_back(T);\n\t\t\n\t\tvector<vector<long double>> a(e.size() - 1, vector<long double>(e.size() - 1, 0));\n\t\tvector<long double> b(e.size() - 1, 0);\n\t\t\n\t\tfor (int j = 0; j < N - 1 - i; j++) {\n\t\t\tfor (int k = 0; k < N - 1 - i; k++) a[j][k] = e[j][k];\n\t\t\tb[j] = f[j] - T * e[j][e.size() - 1];\n\t\t}\n\t\te = a;\n\t\tf = b;\n\t}\n\n\treverse(res.begin(), res.end());\n\treturn res;\n}\n\nvector<long double> getans(vector<pair<long double, long double>> J) {\n\tint N = J.size();\n\tvector<vector<long double>> a(N, vector<long double>(N, 0));\n\tvector<long double> b(N, 0);\n\n\tfor (int i = 0; i < N; i++) {\n\t\tfor (int j = 0; j < N; j++) a[i][j] = 1.0L * pow(J[i].first, 1.0L*(N - 1 - j));\n\t\tb[i] = J[i].second;\n\t}\n\t\n\tvector<long double> ret = Gauss_Erase(a, b);\n\treturn ret;\n}\n\nbool different(vector<long double> A, vector<long double> B) {\n\tfor (int i = 0; i < A.size(); i++) {\n\t\tif (fabs(A[i] - B[i]) > 1e-2) return true;\n\t}\n\treturn false;\n}\n\nint solve(vector<long double> S) {\n\tint N = S.size() - 3;\n\tfor (int i = 0; i <= N + 2; i++) {\n\t\tvector<long double> goal; bool flag = false;\n\t\tfor (int j = 0; j <= N + 2; j++) {\n\t\t\tif (i == j) continue;\n\t\t\tvector<pair<long double, long double>> G;\n\t\t\tfor (int k = 0; k <= N + 2; k++) {\n\t\t\t\tif (k != i && k != j) G.push_back(make_pair(1.0L * k + 1.0L, S[k]));\n\t\t\t}\n\t\t\tvector<long double> V = getans(G);\n\t\t\tif (goal.size() == 0) goal = V;\n\t\t\telse if (different(goal, V) == true) flag = true;\n\t\t}\n\t\tif (flag == false) return i;\n\t}\n\treturn -1;\n}\n\nint main() {\n\twhile (true) {\n\t\tint N; cin >> N; if (N == 0) break;\n\t\tvector<long double> A(N + 3, 0);\n\t\tfor (int i = 0; i < N + 3; i++) cin >> A[i];\n\n\t\tint ans = solve(A);\n\t\tcout << ans << endl;\n\t}\n\treturn 0;\n}", "accuracy": 1, "time_ms": 120, "memory_kb": 3456, "score_of_the_acc": -1.3991, "final_rank": 19 }, { "submission_id": "aoj_1328_3435542", "code_snippet": "#include <iostream>\n#include <vector>\n#include <cmath>\nusing namespace std;\n\n// Gauss Jordan\nusing D = long double;\nconst D EPS = 1e-5;\ntemplate<class T> struct Matrix {\n vector<vector<T> > val;\n Matrix(int n, int m, T x = 0) : val(n, vector<T>(m, x)) {}\n void init(int n, int m, T x = 0) {val.assign(n, vector<T>(m, x));}\n size_t size() const {return val.size();}\n inline vector<T>& operator [] (int i) {return val[i];}\n};\n\ntemplate<class T> int GaussJordan(Matrix<T> &A, bool is_extended = false) {\n int m = A.size(), n = A[0].size();\n int rank = 0;\n for (int col = 0; col < n; ++col) {\n if (is_extended && col == n-1) break;\n int pivot = -1;\n T ma = EPS;\n for (int row = rank; row < m; ++row) {\n if (abs(A[row][col]) > ma) {\n ma = abs(A[row][col]);\n pivot = row;\n }\n }\n if (pivot == -1) continue;\n swap(A[pivot], A[rank]);\n auto fac = A[rank][col];\n for (int col2 = 0; col2 < n; ++col2) A[rank][col2] /= fac;\n for (int row = 0; row < m; ++row) {\n if (row != rank && abs(A[row][col]) > EPS) {\n auto fac = A[row][col];\n for (int col2 = 0; col2 < n; ++col2) {\n A[row][col2] -= A[rank][col2] * fac;\n }\n }\n }\n ++rank;\n }\n return rank;\n}\n\ntemplate<class T> vector<T> linear_equation(Matrix<T> A, vector<T> b) {\n int m = A.size(), n = A[0].size();\n Matrix<T> M(m, n + 1);\n for (int i = 0; i < m; ++i) {\n for (int j = 0; j < n; ++j) M[i][j] = A[i][j];\n M[i][n] = b[i];\n }\n int rank = GaussJordan(M, true);\n vector<T> res;\n for (int row = rank; row < m; ++row) if (abs(M[row][n]) > EPS) return res;\n res.assign(n, 0);\n for (int i = 0; i < rank; ++i) res[i] = M[i][n];\n return res;\n}\n\n\n\n\nD pow(D a, int n) {\n D res = 1.0;\n for (int i = 0; i < n; ++i) res *= a;\n return res;\n}\n\nD func(const vector<D> &coef, int i) {\n D res = 0.0;\n for (int p = 0; p < coef.size(); ++p) res += coef[p] * pow(i, p);\n return res;\n}\n\nint main() {\n int d;\n while (cin >> d, d) {\n vector<D> v(d+3);\n for (int i = 0; i < d+3; ++i) cin >> v[i];\n\n int res = 0;\n for (int i = 0; i < d+3; ++i) {\n for (int j = i+1; j < d+3; ++j) {\n Matrix<D> A(d+1, d+1);\n vector<D> b(d+1);\n for (int k = 0, iter = 0; k < d+3; ++k) {\n if (k == i || k == j) continue;\n for (int p = 0; p < d+1; ++p) {\n A[iter][p] = pow(k, p);\n b[iter] = v[k];\n }\n ++iter;\n }\n vector<D> ans = linear_equation(A, b);\n if (ans.empty()) continue;\n D vi = func(ans, i), vj = func(ans, j);\n int num = 0;\n if (fabs(vi - v[i]) > EPS) res = i, ++num;\n if (fabs(vj - v[j]) > EPS) res = j, ++num;\n if (num == 1) goto end;\n }\n }\n end:\n cout << res << endl;\n }\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3172, "score_of_the_acc": -0.0734, "final_rank": 3 }, { "submission_id": "aoj_1328_3435541", "code_snippet": "#include <iostream>\n#include <vector>\n#include <queue>\n#include <cmath>\n#include <iomanip>\n#include <algorithm>\nusing namespace std;\n#define COUT(x) cout << #x << \" = \" << (x) << \" (L\" << __LINE__ << \")\" << endl\n\n\n// Gauss Jordan\nusing D = long double;\nconst D EPS = 1e-5;\ntemplate<class T> struct Matrix {\n vector<vector<T> > val;\n Matrix(int n, int m, T x = 0) : val(n, vector<T>(m, x)) {}\n void init(int n, int m, T x = 0) {val.assign(n, vector<T>(m, x));}\n size_t size() const {return val.size();}\n inline vector<T>& operator [] (int i) {return val[i];}\n};\n\ntemplate<class T> ostream& operator << (ostream& s, Matrix<T> A) {\n s << endl; \n for (int i = 0; i < A.size(); ++i) {\n for (int j = 0; j < A[i].size(); ++j) {\n s << A[i][j] << \", \";\n }\n s << endl;\n }\n return s;\n}\n\ntemplate<class T> int GaussJordan(Matrix<T> &A, bool is_extended = false) {\n int m = A.size(), n = A[0].size();\n int rank = 0;\n for (int col = 0; col < n; ++col) {\n if (is_extended && col == n-1) break;\n int pivot = -1;\n T ma = EPS;\n for (int row = rank; row < m; ++row) {\n if (abs(A[row][col]) > ma) {\n ma = abs(A[row][col]);\n pivot = row;\n }\n }\n if (pivot == -1) continue;\n swap(A[pivot], A[rank]);\n auto fac = A[rank][col];\n for (int col2 = 0; col2 < n; ++col2) A[rank][col2] /= fac;\n for (int row = 0; row < m; ++row) {\n if (row != rank && abs(A[row][col]) > EPS) {\n auto fac = A[row][col];\n for (int col2 = 0; col2 < n; ++col2) {\n A[row][col2] -= A[rank][col2] * fac;\n }\n }\n }\n ++rank;\n }\n return rank;\n}\n\ntemplate<class T> vector<T> linear_equation(Matrix<T> A, vector<T> b) {\n int m = A.size(), n = A[0].size();\n Matrix<T> M(m, n + 1);\n for (int i = 0; i < m; ++i) {\n for (int j = 0; j < n; ++j) M[i][j] = A[i][j];\n M[i][n] = b[i];\n }\n int rank = GaussJordan(M, true);\n vector<T> res;\n for (int row = rank; row < m; ++row) if (abs(M[row][n]) > EPS) return res;\n res.assign(n, 0);\n for (int i = 0; i < rank; ++i) res[i] = M[i][n];\n return res;\n}\n\n\n\n\nD pow(D a, int n) {\n D res = 1.0;\n for (int i = 0; i < n; ++i) res *= a;\n return res;\n}\n\nD func(const vector<D> &coef, int i) {\n D res = 0.0;\n for (int p = 0; p < coef.size(); ++p) res += coef[p] * pow(i, p);\n return res;\n}\n\nint main() {\n int d;\n while (cin >> d, d) {\n vector<D> v(d+3);\n for (int i = 0; i < d+3; ++i) cin >> v[i];\n\n int res = 0;\n for (int i = 0; i < d+3; ++i) {\n for (int j = i+1; j < d+3; ++j) {\n Matrix<D> A(d+1, d+1);\n vector<D> b(d+1);\n for (int k = 0, iter = 0; k < d+3; ++k) {\n if (k == i || k == j) continue;\n for (int p = 0; p < d+1; ++p) {\n A[iter][p] = pow(k, p);\n b[iter] = v[k];\n }\n ++iter;\n }\n vector<D> ans = linear_equation(A, b);\n if (ans.empty()) continue;\n D vi = func(ans, i), vj = func(ans, j);\n int num = 0;\n if (fabs(vi - v[i]) > EPS) res = i, ++num;\n if (fabs(vj - v[j]) > EPS) res = j, ++num;\n if (num == 1) goto end;\n }\n }\n end:\n cout << res << endl;\n }\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3160, "score_of_the_acc": -0.0596, "final_rank": 1 }, { "submission_id": "aoj_1328_3338746", "code_snippet": "#include <bits/stdc++.h>\n#define int long long\nusing namespace std;\nusing ld = long double;\n\ntemplate <class T>\nvector<T> solve(const vector<vector<T>> &A, const vector<T> &y) {\n int N = y.size();\n vector<T> x(N, 0);\n vector<vector<T>> tmp(N, vector<T>(N * 2, 0));\n for (int i = 0; i < N; i++)\n for (int j = 0; j < N; j++)\n tmp[i][j] = A[i][j];\n for (int i = 0; i < N; i++)\n tmp[i][N + i] = 1;\n \n for (int i = 0; i < N; i++) {\n if (tmp[i][i] == 0) tmp[i][i] += 1e-10;\n T sc = tmp[i][i];\n for (int k = 0; k < N * 2; k++)\n tmp[i][k] /= sc;\n for (int j = 0; j < N; j++) {\n if (i == j) continue;\n sc = tmp[j][i];\n for (int k = 0; k < N * 2; k++) {\n tmp[j][k] -= tmp[i][k] * sc;\n }\n }\n }\n\n for (int i = 0; i < N; i++) {\n for (int j = 0; j < N; j++) {\n x[i] += tmp[i][N + j] * y[j];\n }\n }\n return x;\n}\n\nsigned main() {\n while (1) {\n int d;\n cin >> d;\n if (d == 0) break;\n vector<ld> v(d + 3);\n for (int i = 0; i < d + 3; i++) {\n cin >> v[i];\n }\n \n for (int i = 0; i < d + 3; i++) {\n for (int l = i + 1; l < d + 3; l++) {\n vector<ld> y;\n y.reserve(d + 2);\n auto p = y;\n for (int j = 0; j < d + 3; j++) {\n if (i == j || l == j) continue;\n y.push_back(v[j]);\n p.push_back(j);\n }\n vector<vector<ld>> A(d + 1, vector<ld>(d + 1, 0));\n for (int j = 0; j < d + 1; j++) {\n ld x = p[j];\n for (ld k = 0; k < d + 1; k++) {\n A[j][k] = pow(x, k);\n }\n }\n \n auto value = [&](const vector<ld> &c, ld p) {\n ld ret = 0;\n for (int k = 0; k < c.size(); k++) {\n ret += c[k] * pow(p, k);\n }\n return ret;\n };\n\n auto c = solve(A, y);\n\n int cnt = 0;\n int idx = 0;\n for (int j = 0; j < d + 3; j++) {\n if (abs(value(c, j) - v[j]) > 1e-4) {\n cnt++;\n idx = j;\n }\n }\n if (cnt == 1) {\n cout << idx << endl;\n goto next_case;\n }\n }\n }\n next_case:;\n }\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3420, "score_of_the_acc": -0.3578, "final_rank": 12 }, { "submission_id": "aoj_1328_3321821", "code_snippet": "#include<iostream>\n#include<string>\n#include<cstdio>\n#include<vector>\n#include<cmath>\n#include<algorithm>\n#include<functional>\n#include<iomanip>\n#include<queue>\n#include<ciso646>\n#include<random>\n#include<map>\n#include<set>\n#include<complex>\n#include<bitset>\n#include<stack>\n#include<unordered_map>\nusing namespace std;\ntypedef long long ll;\ntypedef unsigned int ui;\nconst ll mod = 998244353;\nconst ll INF = (ll)1000000007 * 1000000007;\ntypedef pair<int, int> P;\n#define stop char nyaa;cin>>nyaa;\n#define rep(i,n) for(int i=0;i<n;i++)\n#define per(i,n) for(int i=n-1;i>=0;i--)\n#define Rep(i,sta,n) for(int i=sta;i<n;i++)\n#define rep1(i,n) for(int i=1;i<=n;i++)\n#define per1(i,n) for(int i=n;i>=1;i--)\n#define Rep1(i,sta,n) for(int i=sta;i<=n;i++)\ntypedef long double ld;\ntypedef complex<ld> Point;\nconst ld eps = 1e-3;\nconst ld pi = acos(-1.0);\ntypedef pair<ll, ll> LP;\ntypedef pair<ld, ld> LDP;\n\ntypedef vector<ld> vec;\ntypedef vector<vec> mat;\nvoid debag_matrix(const mat &A) {\n\trep(i, A.size()) {\n\t\trep(j, A[i].size()){\n\t\t\tcout << A[i][j] << \" \";\n\t }\n\t\tcout << endl;\n\t}\n}\nvoid gauss_hakidasi(mat &A) {\n\tint n = A.size(), m = A[0].size();\n\tint t = min(n, m);\n\trep(k, t) {\n\t\tint chk = -1;\n\t\tRep(i,k, n) {\n\t\t\tif (abs(A[i][k]) > eps) {\n\t\t\t\tchk = i; break;\n\t\t\t}\n\t\t}\n\t\tif (chk < 0)return;\n\t\tswap(A[k], A[chk]);\n\t\tld x = A[k][k];\n\t\trep(i, m) {\n\t\t\tA[k][i] /= x;\n\t\t}\n\t\trep(i, n) {\n\t\t\tif (i == k)continue;\n\t\t\tx = A[i][k];\n\t\t\trep(j, m) {\n\t\t\t\tA[i][j] -= x * A[k][j];\n\t\t\t}\n\t\t}\n\t}\n}\nint n;\nld d[10];\n\nbool valid(int id, mat A) {\n\tgauss_hakidasi(A);\n\t//debag_matrix(A);\n\tld sum = 0;\n\tld cur = 1;\n\tper(i, n + 1) {\n\t\tsum += A[i][n+1] * cur; cur *= id;\n\t}\n\treturn abs(sum - d[id]) < eps;\n}\nint main() {\n\twhile (cin >> n, n) {\n\t\trep(i, n + 3)cin >> d[i];\n\t\trep(i, n + 3) {\n\t\t\tmat A; int ck = 0;\n\t\t\tper(j, n + 3) {\n\t\t\t\tif (i == j)continue;\n\t\t\t\tvector<ld> v;\n\t\t\t\tld cur = 1;\n\t\t\t\trep(k, n + 1) {\n\t\t\t\t\tv.push_back(cur);\n\t\t\t\t\tcur *= j;\n\t\t\t\t}\n\t\t\t\treverse(v.begin(), v.end());\n\t\t\t\tv.push_back(d[j]);\n\t\t\t\tif (A.size() < n + 1)A.push_back(v);\n\t\t\t\telse ck = j;\n\t\t\t}\n\t\t\t//debag_matrix(A);\n\t\t\t//cout << ck << endl;\n\t\t\tif (valid(ck, A)) {\n\t\t\t\tcout << i << endl; break;\n\t\t\t}\n\n\t\t}\n\t\t//cout << \"finish\" << endl;\n\t}\n\t//stop\n\treturn 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3284, "score_of_the_acc": -0.2018, "final_rank": 7 } ]
aoj_1326_cpp
Problem B: Stylish Stylish is a programming language whose syntax comprises names , that are sequences of Latin alphabet letters, three types of grouping symbols , periods ('.'), and newlines. Grouping symbols, namely round brackets ('(' and ')'), curly brackets ('{' and '}'), and square brackets ('[' and ']'), must match and be nested properly. Unlike most other programming languages, Stylish uses periods instead of whitespaces for the purpose of term separation. The following is an example of a Stylish program. 1 ( Welcome .to 2 ......... Stylish ) 3 { Stylish .is 4 .....[.( a. programming . language .fun .to. learn ) 5 .......] 6 ..... Maybe .[ 7 ....... It. will .be.an. official . ICPC . language 8 .......] 9 .....} As you see in the example, a Stylish program is indented by periods. The amount of indentation of a line is the number of leading periods of it. Your mission is to visit Stylish masters, learn their indentation styles, and become the youngest Stylish master. An indentation style for well-indented Stylish programs is defined by a triple of integers, ( R , C , S ), satisfying 1 ≤ R , C , S ≤ 20. R , C and S are amounts of indentation introduced by an open round bracket, an open curly bracket, and an open square bracket, respectively. In a well-indented program, the amount of indentation of a line is given by R ( r o − r c ) + C ( c o − c c ) + S ( s o − s c ), where r o , c o , and s o are the numbers of occurrences of open round, curly, and square brackets in all preceding lines, respectively, and r c , c c , and s c are those of close brackets. The first line has no indentation in any well-indented program. The above example is formatted in the indentation style ( R , C , S ) = (9, 5, 2). The only grouping symbol occurring in the first line of the above program is an open round bracket. Therefore the amount of indentation for the second line is 9 · (1 − 0) + 5 · (0 − 0) + 2 ·(0 − 0) = 9. The first four lines contain two open round brackets, one open curly bracket, one open square bracket, two close round brackets, but no close curly nor square bracket. Therefore the amount of indentation for the fifth line is 9 · (2 − 2) + 5 · (1 − 0) + 2 · (1 − 0) = 7. Stylish masters write only well-indented Stylish programs. Every master has his/her own indentation style. Write a program that imitates indentation styles of Stylish masters. Input The input consists of multiple datasets. The first line of a dataset contains two integers p (1 ≤ p ≤ 10) and q (1 ≤ q ≤ 10). The next p lines form a well-indented program P written by a Stylish master and the following q lines form another program Q . You may assume that every line of both programs has at least one character and at most 80 characters. Also, you may assume that no line of Q starts with a period. The last dataset is followed by a line containing two zeros. Output Apply the indentation style of P to Q and output the appropriate amount of indentation for each line of Q . The amounts must b ...(truncated)
[ { "submission_id": "aoj_1326_9614938", "code_snippet": "#include <bits/stdc++.h>\n\nusing namespace std;\n\n#define rep(i, a, b) for (int i = (int)(a); i < (int)(b); i++)\n#define rrep(i, a, b) for (int i = (int)(b)-1; i >= (int)(a); i--)\n#define ALL(v) (v).begin(), (v).end()\n#define UNIQUE(v) sort(ALL(v)), (v).erase(unique(ALL(v)), (v).end())\n#define SZ(v) (int)v.size()\n#define MIN(v) *min_element(ALL(v))\n#define MAX(v) *max_element(ALL(v))\n#define LB(v, x) int(lower_bound(ALL(v), (x)) - (v).begin())\n#define UB(v, x) int(upper_bound(ALL(v), (x)) - (v).begin())\n\nusing uint = unsigned int;\nusing ll = long long int;\nusing ull = unsigned long long;\nusing i128 = __int128_t;\nusing u128 = __uint128_t;\nconst int inf = 0x3fffffff;\nconst ll INF = 0x1fffffffffffffff;\n\ntemplate <typename T> inline bool chmax(T &a, T b) {\n if (a < b) {\n a = b;\n return 1;\n }\n return 0;\n}\ntemplate <typename T> inline bool chmin(T &a, T b) {\n if (a > b) {\n a = b;\n return 1;\n }\n return 0;\n}\ntemplate <typename T, typename U> T ceil(T x, U y) {\n assert(y != 0);\n if (y < 0)\n x = -x, y = -y;\n return (x > 0 ? (x + y - 1) / y : x / y);\n}\ntemplate <typename T, typename U> T floor(T x, U y) {\n assert(y != 0);\n if (y < 0)\n x = -x, y = -y;\n return (x > 0 ? x / y : (x - y + 1) / y);\n}\ntemplate <typename T> int popcnt(T x) {\n return __builtin_popcountll(x);\n}\ntemplate <typename T> int topbit(T x) {\n return (x == 0 ? -1 : 63 - __builtin_clzll(x));\n}\ntemplate <typename T> int lowbit(T x) {\n return (x == 0 ? -1 : __builtin_ctzll(x));\n}\n\ntemplate <class T, class U>\nostream &operator<<(ostream &os, const pair<T, U> &p) {\n os << \"P(\" << p.first << \", \" << p.second << \")\";\n return os;\n}\ntemplate <typename T> ostream &operator<<(ostream &os, const vector<T> &vec) {\n os << \"{\";\n for (int i = 0; i < vec.size(); i++) {\n os << vec[i] << (i + 1 == vec.size() ? \"\" : \", \");\n }\n os << \"}\";\n return os;\n}\ntemplate <typename T, typename U>\nostream &operator<<(ostream &os, const map<T, U> &map_var) {\n os << \"{\";\n for (auto itr = map_var.begin(); itr != map_var.end(); itr++) {\n os << \"(\" << itr->first << \", \" << itr->second << \")\";\n itr++;\n if (itr != map_var.end())\n os << \", \";\n itr--;\n }\n os << \"}\";\n return os;\n}\ntemplate <typename T> ostream &operator<<(ostream &os, const set<T> &set_var) {\n os << \"{\";\n for (auto itr = set_var.begin(); itr != set_var.end(); itr++) {\n os << *itr;\n ++itr;\n if (itr != set_var.end())\n os << \", \";\n itr--;\n }\n os << \"}\";\n return os;\n}\n#ifdef LOCAL\n#define show(...) _show(0, #__VA_ARGS__, __VA_ARGS__)\n#else\n#define show(...) true\n#endif\ntemplate <typename T> void _show(int i, T name) {\n cerr << '\\n';\n}\ntemplate <typename T1, typename T2, typename... T3>\nvoid _show(int i, const T1 &a, const T2 &b, const T3 &...c) {\n for (; a[i] != ',' && a[i] != '\\0'; i++)\n cerr << a[i];\n cerr << \":\" << b << \" \";\n _show(i + 1, a, c...);\n}\n\n#include <unistd.h>\n\nnamespace fastio {\nstatic constexpr uint32_t SZ = 1 << 17;\nchar ibuf[SZ];\nchar obuf[SZ];\nchar out[100];\n// pointer of ibuf, obuf\n\n\nuint32_t pil = 0, pir = 0, por = 0;\n\nstruct Pre {\n char num[10000][4];\n constexpr Pre() : num() {\n for (int i = 0; i < 10000; i++) {\n int n = i;\n for (int j = 3; j >= 0; j--) {\n num[i][j] = n % 10 | '0';\n n /= 10;\n }\n }\n }\n} constexpr pre;\n\ninline void load() {\n memmove(ibuf, ibuf + pil, pir - pil);\n pir = pir - pil + fread(ibuf + pir - pil, 1, SZ - pir + pil, stdin);\n pil = 0;\n if (pir < SZ)\n ibuf[pir++] = '\\n';\n}\n\ninline void flush() {\n fwrite(obuf, 1, por, stdout);\n por = 0;\n}\n\nvoid rd(char &c) {\n do {\n if (pil + 1 > pir)\n load();\n c = ibuf[pil++];\n } while (isspace(c));\n}\n\nvoid rd(string &x) {\n x.clear();\n char c;\n do {\n if (pil + 1 > pir)\n load();\n c = ibuf[pil++];\n } while (isspace(c));\n do {\n x += c;\n if (pil == pir)\n load();\n c = ibuf[pil++];\n } while (!isspace(c));\n}\n\ntemplate <typename T> void rd_real(T &x) {\n string s;\n rd(s);\n x = stod(s);\n}\n\ntemplate <typename T> void rd_integer(T &x) {\n if (pil + 100 > pir)\n load();\n char c;\n do\n c = ibuf[pil++];\n while (c < '-');\n bool minus = 0;\n if constexpr (is_signed<T>::value || is_same_v<T, i128>) {\n if (c == '-') {\n minus = 1, c = ibuf[pil++];\n }\n }\n x = 0;\n while ('0' <= c) {\n x = x * 10 + (c & 15), c = ibuf[pil++];\n }\n if constexpr (is_signed<T>::value || is_same_v<T, i128>) {\n if (minus)\n x = -x;\n }\n}\n\nvoid rd(int &x) {\n rd_integer(x);\n}\nvoid rd(ll &x) {\n rd_integer(x);\n}\nvoid rd(i128 &x) {\n rd_integer(x);\n}\nvoid rd(uint &x) {\n rd_integer(x);\n}\nvoid rd(ull &x) {\n rd_integer(x);\n}\nvoid rd(u128 &x) {\n rd_integer(x);\n}\nvoid rd(double &x) {\n rd_real(x);\n}\nvoid rd(long double &x) {\n rd_real(x);\n}\n\ntemplate <class T, class U> void rd(pair<T, U> &p) {\n return rd(p.first), rd(p.second);\n}\ntemplate <size_t N = 0, typename T> void rd_tuple(T &t) {\n if constexpr (N < std::tuple_size<T>::value) {\n auto &x = std::get<N>(t);\n rd(x);\n rd_tuple<N + 1>(t);\n }\n}\ntemplate <class... T> void rd(tuple<T...> &tpl) {\n rd_tuple(tpl);\n}\n\ntemplate <size_t N = 0, typename T> void rd(array<T, N> &x) {\n for (auto &d : x)\n rd(d);\n}\ntemplate <class T> void rd(vector<T> &x) {\n for (auto &d : x)\n rd(d);\n}\n\nvoid read() {}\ntemplate <class H, class... T> void read(H &h, T &...t) {\n rd(h), read(t...);\n}\n\nvoid wt(const char c) {\n if (por == SZ)\n flush();\n obuf[por++] = c;\n}\nvoid wt(const string s) {\n for (char c : s)\n wt(c);\n}\nvoid wt(const char *s) {\n size_t len = strlen(s);\n for (size_t i = 0; i < len; i++)\n wt(s[i]);\n}\n\ntemplate <typename T> void wt_integer(T x) {\n if (por > SZ - 100)\n flush();\n if (x < 0) {\n obuf[por++] = '-', x = -x;\n }\n int outi;\n for (outi = 96; x >= 10000; outi -= 4) {\n memcpy(out + outi, pre.num[x % 10000], 4);\n x /= 10000;\n }\n if (x >= 1000) {\n memcpy(obuf + por, pre.num[x], 4);\n por += 4;\n } else if (x >= 100) {\n memcpy(obuf + por, pre.num[x] + 1, 3);\n por += 3;\n } else if (x >= 10) {\n int q = (x * 103) >> 10;\n obuf[por] = q | '0';\n obuf[por + 1] = (x - q * 10) | '0';\n por += 2;\n } else\n obuf[por++] = x | '0';\n memcpy(obuf + por, out + outi + 4, 96 - outi);\n por += 96 - outi;\n}\n\ntemplate <typename T> void wt_real(T x) {\n ostringstream oss;\n oss << fixed << setprecision(15) << double(x);\n string s = oss.str();\n wt(s);\n}\n\nvoid wt(int x) {\n wt_integer(x);\n}\nvoid wt(ll x) {\n wt_integer(x);\n}\nvoid wt(i128 x) {\n wt_integer(x);\n}\nvoid wt(uint x) {\n wt_integer(x);\n}\nvoid wt(ull x) {\n wt_integer(x);\n}\nvoid wt(u128 x) {\n wt_integer(x);\n}\nvoid wt(double x) {\n wt_real(x);\n}\nvoid wt(long double x) {\n wt_real(x);\n}\n\ntemplate <class T, class U> void wt(const pair<T, U> val) {\n wt(val.first);\n wt(' ');\n wt(val.second);\n}\ntemplate <size_t N = 0, typename T> void wt_tuple(const T t) {\n if constexpr (N < std::tuple_size<T>::value) {\n if constexpr (N > 0) {\n wt(' ');\n }\n const auto x = std::get<N>(t);\n wt(x);\n wt_tuple<N + 1>(t);\n }\n}\ntemplate <class... T> void wt(tuple<T...> tpl) {\n wt_tuple(tpl);\n}\ntemplate <class T, size_t S> void wt(const array<T, S> val) {\n auto n = val.size();\n for (size_t i = 0; i < n; i++) {\n if (i)\n wt(' ');\n wt(val[i]);\n }\n}\ntemplate <class T> void wt(const vector<T> val) {\n auto n = val.size();\n for (size_t i = 0; i < n; i++) {\n if (i)\n wt(' ');\n wt(val[i]);\n }\n}\n\nvoid print() {\n wt('\\n');\n}\ntemplate <class Head, class... Tail> void print(Head &&head, Tail &&...tail) {\n wt(head);\n if (sizeof...(Tail))\n wt(' ');\n print(forward<Tail>(tail)...);\n}\nvoid __attribute__((destructor)) _d() {\n flush();\n}\n} // namespace fastio\n\n\nusing fastio::flush;\nusing fastio::print;\nusing fastio::read;\n\ninline void first(bool i = true) {\n print(i ? \"first\" : \"second\");\n}\ninline void Alice(bool i = true) {\n print(i ? \"Alice\" : \"Bob\");\n}\ninline void Takahashi(bool i = true) {\n print(i ? \"Takahashi\" : \"Aoki\");\n}\ninline void yes(bool i = true) {\n print(i ? \"yes\" : \"no\");\n}\ninline void Yes(bool i = true) {\n print(i ? \"Yes\" : \"No\");\n}\ninline void No() {\n print(\"No\");\n}\ninline void YES(bool i = true) {\n print(i ? \"YES\" : \"NO\");\n}\ninline void NO() {\n print(\"NO\");\n}\ninline void Yay(bool i = true) {\n print(i ? \"Yay!\" : \":(\");\n}\ninline void Possible(bool i = true) {\n print(i ? \"Possible\" : \"Impossible\");\n}\ninline void POSSIBLE(bool i = true) {\n print(i ? \"POSSIBLE\" : \"IMPOSSIBLE\");\n}\n\n/**\n * @brief Fast IO\n */\n\nstruct Data {\n int A, B, C;\n Data(int a, int b, int c) : A(a), B(b), C(c) {};\n};\n\nbool Check(vector<string>& S, int A, int B, int C) {\n int CA = 0, CB = 0, CC = 0;\n int N = S.size();\n rep(i,0,N) {\n int COUNT = 0;\n while(COUNT < S[i].size()) {\n if (S[i][COUNT] == '.') COUNT++;\n else break;\n }\n if (COUNT != CA * A + CB * B + CC * C) return false;\n rep(j,0,S[i].size()) {\n if (S[i][j] == '(') CA++;\n if (S[i][j] == ')') CA--;\n if (S[i][j] == '{') CB++;\n if (S[i][j] == '}') CB--;\n if (S[i][j] == '[') CC++;\n if (S[i][j] == ']') CC--;\n }\n }\n return true;\n}\n\nint main() {\nwhile(1) {\n int N, M;\n cin >> N >> M;\n if (N == 0 && M == 0) return 0;\n vector<string> S(N), T(M);\n rep(i,0,N) cin >> S[i];\n rep(i,0,M) cin >> T[i];\n vector<Data> X;\n rep(i,1,21) {\n rep(j,1,21) {\n rep(k,1,21) {\n if (Check(S,i,j,k)) X.push_back(Data(i,j,k));\n }\n }\n }\n int CA = 0, CB = 0, CC = 0;\n rep(i,0,M) {\n vector<int> ANS;\n for (Data D : X) {\n ANS.push_back(CA * D.A + CB * D.B + CC * D.C);\n }\n UNIQUE(ANS);\n cout << (ANS.size() == 1 ? ANS[0] : -1) << (i == M - 1 ? '\\n' : ' ');\n rep(j,0,T[i].size()) {\n if (T[i][j] == '(') CA++;\n if (T[i][j] == ')') CA--;\n if (T[i][j] == '{') CB++;\n if (T[i][j] == '}') CB--;\n if (T[i][j] == '[') CC++;\n if (T[i][j] == ']') CC--;\n }\n }\n}\n}", "accuracy": 1, "time_ms": 90, "memory_kb": 3544, "score_of_the_acc": -0.4368, "final_rank": 12 }, { "submission_id": "aoj_1326_7558864", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n#define rep(i, n) for (int i = 0; i < (n); i++)\n#define per(i, n) for (int i = (n)-1; i >= 0; i--)\n#define rep2(i, l, r) for (int i = (l); i < (r); i++)\n#define per2(i, l, r) for (int i = (r)-1; i >= (l); i--)\n#define each(e, v) for (auto &e : v)\n#define MM << \" \" <<\n#define pb push_back\n#define eb emplace_back\n#define all(x) begin(x), end(x)\n#define rall(x) rbegin(x), rend(x)\n#define sz(x) (int)x.size()\nusing ll = long long;\nusing pii = pair<int, int>;\nusing pil = pair<int, ll>;\nusing pli = pair<ll, int>;\nusing pll = pair<ll, ll>;\n\ntemplate <typename T>\nusing minheap = priority_queue<T, vector<T>, greater<T>>;\n\ntemplate <typename T>\nusing maxheap = priority_queue<T>;\n\ntemplate <typename T>\nbool chmax(T &x, const T &y) {\n return (x < y) ? (x = y, true) : false;\n}\n\ntemplate <typename T>\nbool chmin(T &x, const T &y) {\n return (x > y) ? (x = y, true) : false;\n}\n\ntemplate <typename T>\nint flg(T x, int i) {\n return (x >> i) & 1;\n}\n\ntemplate <typename T>\nvoid print(const vector<T> &v, T x = 0) {\n int n = v.size();\n for (int i = 0; i < n; i++) cout << v[i] + x << (i == n - 1 ? '\\n' : ' ');\n if (v.empty()) cout << '\\n';\n}\n\ntemplate <typename T>\nvoid printn(const vector<T> &v, T x = 0) {\n int n = v.size();\n for (int i = 0; i < n; i++) cout << v[i] + x << '\\n';\n}\n\ntemplate <typename T>\nint lb(const vector<T> &v, T x) {\n return lower_bound(begin(v), end(v), x) - begin(v);\n}\n\ntemplate <typename T>\nint ub(const vector<T> &v, T x) {\n return upper_bound(begin(v), end(v), x) - begin(v);\n}\n\ntemplate <typename T>\nvoid rearrange(vector<T> &v) {\n sort(begin(v), end(v));\n v.erase(unique(begin(v), end(v)), end(v));\n}\n\ntemplate <typename T>\nvector<int> id_sort(const vector<T> &v, bool greater = false) {\n int n = v.size();\n vector<int> ret(n);\n iota(begin(ret), end(ret), 0);\n sort(begin(ret), end(ret), [&](int i, int j) { return greater ? v[i] > v[j] : v[i] < v[j]; });\n return ret;\n}\n\ntemplate <typename S, typename T>\npair<S, T> operator+(const pair<S, T> &p, const pair<S, T> &q) {\n return make_pair(p.first + q.first, p.second + q.second);\n}\n\ntemplate <typename S, typename T>\npair<S, T> operator-(const pair<S, T> &p, const pair<S, T> &q) {\n return make_pair(p.first - q.first, p.second - q.second);\n}\n\ntemplate <typename S, typename T>\nistream &operator>>(istream &is, pair<S, T> &p) {\n S a;\n T b;\n is >> a >> b;\n p = make_pair(a, b);\n return is;\n}\n\ntemplate <typename S, typename T>\nostream &operator<<(ostream &os, const pair<S, T> &p) {\n return os << p.first << ' ' << p.second;\n}\n\nstruct io_setup {\n io_setup() {\n ios_base::sync_with_stdio(false);\n cin.tie(NULL);\n cout << fixed << setprecision(15);\n }\n} io_setup;\n\nconst int inf = (1 << 30) - 1;\nconst ll INF = (1LL << 60) - 1;\n// const int MOD = 1000000007;\nconst int MOD = 998244353;\n\nvoid solve() {\n int N, M;\n cin >> N >> M;\n\n if (N == 0) exit(0);\n\n vector<string> S(N), T(M);\n rep(i, N) cin >> S[i];\n rep(i, M) cin >> T[i];\n\n vector<int> indent(N, 0);\n rep(i, N) {\n while (indent[i] < sz(S[i]) && S[i][indent[i]] == '.') indent[i]++;\n }\n\n vector<tuple<int, int, int>> candi;\n\n auto check = [&](int r, int c, int s) {\n int sum = 0;\n rep(i, N) {\n if (sum != indent[i]) return false;\n each(e, S[i]) {\n if (e == '(') sum += r;\n if (e == ')') sum -= r;\n if (e == '{') sum += c;\n if (e == '}') sum -= c;\n if (e == '[') sum += s;\n if (e == ']') sum -= s;\n }\n }\n return true;\n };\n\n rep2(r, 1, 21) rep2(c, 1, 21) rep2(s, 1, 21) {\n if (check(r, c, s)) candi.eb(r, c, s);\n }\n\n vector<vector<int>> indents(M);\n for (auto [r, c, s] : candi) {\n int sum = 0;\n rep(i, M) {\n indents[i].eb(sum);\n each(e, T[i]) {\n if (e == '(') sum += r;\n if (e == ')') sum -= r;\n if (e == '{') sum += c;\n if (e == '}') sum -= c;\n if (e == '[') sum += s;\n if (e == ']') sum -= s;\n }\n }\n }\n\n vector<int> ans(M);\n rep(i, M) {\n rearrange(indents[i]);\n if (sz(indents[i]) != 1) {\n ans[i] = -1;\n } else {\n ans[i] = indents[i][0];\n }\n }\n\n print(ans);\n}\n\nint main() {\n while (true) solve();\n}", "accuracy": 1, "time_ms": 90, "memory_kb": 3552, "score_of_the_acc": -0.4411, "final_rank": 13 }, { "submission_id": "aoj_1326_7117360", "code_snippet": "#include <bits/stdc++.h>\n\nusing namespace std;\n\n#define rep(i, n) for (int i = 0; i < (n); ++i)\n#define rrep(i, n) for (int i = (n) - 1; i >= 0; --i)\n\n#define all(c) std::begin(c), std::end(c)\n\n#ifdef LOCAL\n#define debug(...) debug_impl(#__VA_ARGS__, __VA_ARGS__)\ntemplate <class T, class ...Args> void debug_impl(string s, T&& f, Args &&...args) {\n cerr << \"(\" << s << \"): \" << \"(\" << forward<T>(f);\n ((cerr << \", \" << forward<Args>(args)), ..., (cerr << \")\\n\"));\n}\n#else\n#define debug(...) void(0)\n#endif\n\ntemplate <class T> bool chmax(T& a, const T& b) { return b > a ? (a = b, true) : false; }\ntemplate <class T> bool chmin(T& a, const T& b) { return b < a ? (a = b, true) : false; }\n\n\ntemplate <class T> istream& operator>>(istream& in, vector<T>& v) {\n for (auto& e : v) in >> e;\n return in;\n}\ntemplate <class ...Args> void read(Args&... args) {\n (cin >> ... >> args);\n}\n\ntemplate <class T> ostream& operator>>(ostream& out, const vector<T>& v) {\n int n = v.size();\n rep(i, n) {\n out << v[i];\n if (i + 1 != n) out << ' ';\n }\n return out;\n}\n\ntemplate <class T, class ...Tails> void print(T&& h, Tails &&... tails) {\n cout << h, ((cout << ' ' << forward<Tails>(tails)), ..., (cout << '\\n'));\n}\n\ntemplate <class T> using vc = vector<T>;\ntemplate <class T> using vvc = vector<vc<T>>;\n#define si(c) (int)(c).size()\n#define eb emplace_back\n\nint main() {\n ios::sync_with_stdio(false);\n cin.tie(nullptr);\n int p, q;\n while(cin >> p >> q) {\n if(p == 0 and q == 0) break;\n vvc<int> ans(q);\n vc<string> s(p + q); read(s);\n for(int x = 1; x <= 20; ++ x) {\n for(int y = 1; y <= 20; ++ y) {\n for(int z = 1; z <= 20; ++ z) {\n int cx = 0, cy = 0, cz = 0;\n bool ok = 1;\n rep(i, p) {\n int cnt = 0;\n while(cnt < (int)s[i].size() and s[i][cnt] == '.') {\n ++ cnt;\n }\n // debug(cnt);\n if(x * cx + y * cy + z * cz != cnt) {\n ok = 0;\n }\n for(char c: s[i]) {\n if(c == '(') ++ cx;\n else if(c == ')') -- cx;\n\n if(c == '{') ++ cy;\n else if(c == '}') -- cy;\n\n if(c == '[') ++ cz;\n else if(c == ']') -- cz;\n }\n }\n cx = 0, cy = 0, cz = 0;\n rep(j, q) {\n int i = j + p;\n // int cnt = 0;\n // while(cnt < (int)s[i].size() and s[i][cnt] == '.') {\n // ++ cnt;\n // }\n if(ok) {\n ans[j].eb(x * cx + y * cy + z * cz);\n }\n for(char c: s[i]) {\n if(c == '(') ++ cx;\n else if(c == ')') -- cx;\n\n if(c == '{') ++ cy;\n else if(c == '}') -- cy;\n\n if(c == '[') ++ cz;\n else if(c == ']') -- cz;\n }\n }\n }\n }\n }\n vc<int> vs;\n rep(i, q) {\n sort(all(ans[i]));\n ans[i].erase(unique(all(ans[i])), ans[i].end());\n // debug(ans[i]);\n int val = (si(ans[i]) == 1 ? ans[i][0] : -1);\n cout << val << \" \\n\"[i == q - 1];\n }\n }\n}", "accuracy": 1, "time_ms": 570, "memory_kb": 3540, "score_of_the_acc": -1.2918, "final_rank": 20 }, { "submission_id": "aoj_1326_6553033", "code_snippet": "#include<bits/stdc++.h>\n#define rep(i,a,...) for(int i = (a)*(strlen(#__VA_ARGS__)!=0);i<(int)(strlen(#__VA_ARGS__)?__VA_ARGS__:(a));++i)\n#define per(i,a,...) for(int i = (strlen(#__VA_ARGS__)?__VA_ARGS__:(a))-1;i>=(int)(strlen(#__VA_ARGS__)?(a):0);--i)\n#define foreach(i, n) for(auto &i:(n))\n#define all(x) (x).begin(), (x).end()\n#define bit(x) (1ll << (x))\n#define lambda(RES_TYPE, ...) (function<RES_TYPE(__VA_ARGS__)>)[&](__VA_ARGS__) -> RES_TYPE\n#define method(FUNC_NAME, RES_TYPE, ...) function<RES_TYPE(__VA_ARGS__)> FUNC_NAME = lambda(RES_TYPE, __VA_ARGS__)\nusing namespace std;\nusing ll = long long;\nusing pii = pair<int,int>;\nusing pll = pair<ll,ll>;\n//const ll MOD = (ll)1e9+7;\nconst ll MOD = 998244353;\nconst int INF = (ll)1e9+7;\nconst ll INFLL = (ll)1e18;\ntemplate<class t>\nusing vvector = vector<vector<t>>;\ntemplate<class t>\nusing vvvector = vector<vector<vector<t>>>;\ntemplate<class t>\nusing priority_queuer = priority_queue<t, vector<t>, greater<t>>;\ntemplate<class t, class u> bool chmax(t &a, u b){if(a<b){a=b;return true;}return false;}\ntemplate<class t, class u> bool chmin(t &a, u b){if(a>b){a=b;return true;}return false;}\n#ifdef DEBUG\n#define debug(x) cout<<\"LINE \"<<__LINE__<<\": \"<<#x<<\" = \"<<x<<endl;\n#else\n#define debug(x) (void)0\n#endif\n\nnamespace templates{\n ll modpow(ll x, ll b,ll mod=MOD){\n ll res = 1;\n while(b){\n if(b&1)res = res * x % mod;\n x = x * x % mod;\n b>>=1;\n }\n return res;\n }\n\n ll modinv(ll x){\n return modpow(x, MOD-2);\n }\n\n bool was_output = false;\n\n void print();\n template <class t> void print(const vector<t> &);\n template <class t, class u> void print(const pair<t, u> &);\n template <class t> void print(const t&);\n template <class Head, class... Tail> void print(const Head&, const Tail&...);\n\n template <class t> void println(const vector<vector<t>>&);\n template <class t> void println(const vector<t>&);\n template <class t> void println(const t&);\n template <class Head, class... Tail> void println(const Head&, const Tail&...);\n void println();\n void newline();\n\n void print(){\n return;\n }\n\n template <class t>\n void print(const vector<t>&x){\n for(const t&i:x)print(i);\n }\n template <class t, class u>\n void print(const pair<t,u>&p){\n print(p.first);\n print(p.second);\n }\n template <class t>\n void print(const t&x){\n if(was_output)cout<<\" \";\n cout<<x;\n was_output = true;\n }\n template <class Head, class... Tail>\n void print(const Head&head,const Tail&...tail){\n print(head);\n print(tail...);\n }\n\n template<class t>\n void println(const vector<vector<t>>&x){\n for(vector<t> i:x)println(i);\n }\n template<class t>\n void println(const vector<t>&x){\n for(const t&i:x)print(i);\n println();\n }\n template <class t>\n void println(const t&x){\n print(x);\n println();\n }\n void println(){\n if(was_output){\n cout << endl;\n was_output = false;\n }\n }\n template <class Head, class... Tail>\n void println(const Head&head,const Tail&...tail){\n print(head);\n println(tail...);\n }\n\n void newline(){\n was_output = true;\n println();\n }\n\n template<class t>\n istream& operator>>(istream&is, vector<t>&x){\n for(auto &i:x)is >> i;\n return is;\n }\n\n template<class t, class u>\n istream& operator>>(istream&is, pair<t, u>&x){\n is >> x.first >> x.second;\n return is;\n }\n\n template<class t>\n ostream& operator<<(ostream&os, vector<t> &v){\n os << \"{\";\n for(t &i:v){\n os << i << \", \";\n }\n os << \"}\";\n return os;\n }\n\n template<class t = long long>\n t in(){\n t res; cin >> res; return res;\n }\n\n template<class t>\n vector<t> sorted(vector<t> line,function<bool(t,t)> comp=[](t a,t b){return a<b;}){\n sort(line.begin(),line.end(),comp);\n return line;\n }\n\n template<class t>\n vector<t> reversed(vector<t> line){\n reverse(line.begin(),line.end());\n return line;\n }\n string reversed(string str){\n reverse(str.begin(),str.end());\n return str;\n }\n\n long long gcd(long long a,long long b){\n while(b){\n a %= b;\n swap(a,b);\n }\n return a;\n }\n\n long long lcm(long long a,long long b){\n return a / gcd(a,b) * b;\n }\n\n class output_initializer{\n public:\n output_initializer(){\n ios::sync_with_stdio(false);\n cin.tie(nullptr);\n cout << setprecision(20);\n }\n };output_initializer OUTPUT_INITIALIZER_INSTANCE = output_initializer();\n}\n\nusing namespace templates;\n\nvector<int> func(int p,int q){\n {\n string tmp;\n getline(cin,tmp);\n }\n method(readIndent,int,string str){\n rep(i,str.size()){\n if(str[i]!='.')return i;\n }\n return -1;\n };\n method(readLine,string){\n string str;\n getline(cin,str);\n return str;\n };\n using Brackets = array<int,3>;\n method(readBrackets,Brackets,string str){\n Brackets res;\n foreach(i,res)i=0;\n foreach(c,str){\n if(c=='(')++res[0];\n if(c=='{')++res[1];\n if(c=='[')++res[2];\n if(c==')')--res[0];\n if(c=='}')--res[1];\n if(c==']')--res[2];\n }\n return res;\n };\n vector<int> indents(p);\n vector<string> line(p);\n vector<Brackets> shifts(p);\n foreach(i,shifts)foreach(j,i)j=0;\n foreach(i,line)i=readLine();\n set<Brackets> cans;\n rep(i,p){\n indents[i] = readIndent(line[i]);\n if(i){\n rep(j,3)shifts[i][j] += shifts[i-1][j];\n auto add =readBrackets(line[i-1]);\n rep(j,3)shifts[i][j] += add[j];\n }\n }\n method(getCan,set<Brackets>,int indent,Brackets shift){\n set<Brackets> res;\n rep(a,1,21){\n rep(b,1,21){\n rep(c,1,21){\n if(shift[0] * a + shift[1] * b + shift[2] * c == indent){\n res.emplace(Brackets({a,b,c}));\n }\n }\n }\n }\n return res;\n };\n set<Brackets> can = getCan(indents[0],shifts[0]);\n rep(i,1,p){\n auto adds = getCan(indents[i],shifts[i]);\n set<Brackets> next;\n foreach(c,can){\n if(adds.count(c))next.emplace(c);\n }\n swap(can,next);\n }\n vector<int> res;\n Brackets shift;\n foreach(i,shift)i=0;\n rep(_,q){\n string str = readLine();\n set<int> ans;\n foreach(c,can){\n ans.emplace(shift[0] * c[0] + shift[1] * c[1] + shift[2] * c[2]);\n }\n if(ans.size()==1){\n res.emplace_back(*ans.begin());\n }else{\n res.emplace_back(-1);\n }\n auto add = readBrackets(str);\n rep(i,3){\n shift[i] += add[i];\n }\n }\n return res;\n}\n\nint main(){\n while(true){\n int p = in();\n int q = in();\n if(p==0)break;\n println(func(p,q));\n }\n return 0;\n}", "accuracy": 1, "time_ms": 100, "memory_kb": 4860, "score_of_the_acc": -1.1607, "final_rank": 19 }, { "submission_id": "aoj_1326_5064779", "code_snippet": "#include <iostream>\n#include <vector>\n#include <map>\nusing namespace std;\n\nint solve(vector<int> &v, map<vector<int>, int> &mp){\n int ans = -1;\n for(int x = 1; x <= 20; x++){\n for(int y = 1; y <= 20; y++){\n for(int z = 1; z <= 20; z++){\n bool g = true;\n for(auto itr = mp.begin(); itr != mp.end(); itr++){\n if((itr->first)[0] * x + (itr->first)[1] * y + (itr->first)[2] * z != itr->second){\n g = false;\n }\n }\n if(g){\n int s = v[0] * x + v[1] * y + v[2] * z;\n if(ans == -1) ans = s;\n else if(ans != s) ans = -2;\n }\n }\n }\n }\n if(ans < 0) ans = -1;\n return ans;\n}\n\nint main()\n{\n while(true){\n int n, m;\n cin >> n >> m;\n if(n == 0 && m == 0) break;\n string p[12], q[12];\n for(int i = 0; i < n; i++) cin >> p[i];\n for(int i = 0; i < m; i++) cin >> q[i];\n map<vector<int>, int> mp;\n vector<int> r(3);\n for(int i = 0; i < n; i++){\n int l = 0;\n while(p[i][l] == '.') l++;\n mp[r] = l;\n for(int j = l; j < (int)p[i].size(); j++){\n if(p[i][j] == '(') r[0]++;\n if(p[i][j] == ')') r[0]--;\n if(p[i][j] == '[') r[1]++;\n if(p[i][j] == ']') r[1]--;\n if(p[i][j] == '{') r[2]++;\n if(p[i][j] == '}') r[2]--;\n }\n }\n vector<int> v(3);\n for(int i = 0; i < m; i++){\n if(i > 0) cout << \" \";\n cout << solve(v, mp);\n for(int j = 0; j < (int)q[i].size(); j++){\n if(q[i][j] == '(') v[0]++;\n if(q[i][j] == ')') v[0]--;\n if(q[i][j] == '[') v[1]++;\n if(q[i][j] == ']') v[1]--;\n if(q[i][j] == '{') v[2]++;\n if(q[i][j] == '}') v[2]--;\n }\n }\n cout << endl;\n }\n}", "accuracy": 1, "time_ms": 20, "memory_kb": 3068, "score_of_the_acc": -0.0565, "final_rank": 1 }, { "submission_id": "aoj_1326_5036953", "code_snippet": "#pragma GCC target(\"avx2\")\n#pragma GCC optimize(\"Ofast\")\n#pragma GCC optimize(\"unroll-loops\")\n#include <bits/stdc++.h>\n//#include <atcoder/all>\nusing namespace std;\n//using namespace atcoder;\n#define DEBUG\n#ifdef DEBUG\ntemplate <class T, class U>\nostream &operator<<(ostream &os, const pair<T, U> &p) {\n os << '(' << p.first << ',' << p.second << ')';\n return os;\n}\ntemplate <class T> ostream &operator<<(ostream &os, const vector<T> &v) {\n os << '{';\n for(int i = 0; i < (int)v.size(); i++) {\n if(i) { os << ','; }\n os << v[i];\n }\n os << '}';\n return os;\n}\nvoid debugg() { cerr << endl; }\ntemplate <class T, class... Args>\nvoid debugg(const T &x, const Args &... args) {\n cerr << \" \" << x;\n debugg(args...);\n}\n#define debug(...) \\\n cerr << __LINE__ << \" [\" << #__VA_ARGS__ << \"]: \", debugg(__VA_ARGS__)\n#define dump(x) cerr << __LINE__ << \" \" << #x << \" = \" << (x) << endl\n#else\n#define debug(...) (void(0))\n#define dump(x) (void(0))\n#endif\nusing namespace std;\ntypedef long long ll;\ntypedef vector<ll> vl;\ntypedef vector<vl> vvl;\ntypedef vector<char> vc;\ntypedef vector<string> vs;\ntypedef vector<bool> vb;\ntypedef vector<double> vd;\ntypedef pair<ll,ll> P;\ntypedef pair<int,int> pii;\ntypedef vector<P> vpl;\ntypedef tuple<ll,ll,ll> tapu;\n#define rep(i,n) for(int i=0; i<(n); i++)\n#define REP(i,a,b) for(int i=(a); i<(b); i++)\n#define all(x) (x).begin(), (x).end()\n#define rall(x) (x).rbegin(), (x).rend()\nconst int inf = 1<<30;\nconst ll linf = 1LL<<62;\nconst int MAX = 4020000;\nint dy[8] = {0,1,0,-1,1,-1,-1,1};\nint dx[8] = {-1,0,1,0,1,-1,1,-1};\nconst double pi = acos(-1);\nconst double eps = 1e-7;\ntemplate<typename T1,typename T2> inline bool chmin(T1 &a,T2 b){\n\tif(a>b){\n\t\ta = b; return true;\n\t}\n\telse return false;\n}\ntemplate<typename T1,typename T2> inline bool chmax(T1 &a,T2 b){\n\tif(a<b){\n\t\ta = b; return true;\n\t}\n\telse return false;\n}\ntemplate<typename T> inline void print(T &a){\n int sz = a.size();\n for(auto itr = a.begin(); itr != a.end(); itr++){\n\t\tcout << *itr;\n sz--;\n if(sz) cout << \" \";\n\t}\n cout << \"\\n\";\n}\ntemplate<typename T1,typename T2> inline void print2(T1 a, T2 b){\n\tcout << a << \" \" << b << \"\\n\";\n}\ntemplate<typename T1,typename T2,typename T3> inline void print3(T1 a, T2 b, T3 c){\n\tcout << a << \" \" << b << \" \" << c << \"\\n\";\n}\nvoid mark() {cout << \"#\" << \"\\n\";}\nll pcount(ll x) {return __builtin_popcountll(x);}\nconst int mod = 1e9 + 7;\n//const int mod = 998244353;\n\nint main(){\n while(1){\n int n,m; cin >> n >> m;\n if(!n) break;\n vector<string> a(n), b(m);\n rep(i,n) cin >> a[i];\n rep(i,m) cin >> b[i];\n vector<int> ans(m,inf);\n bool no = false;\n for(int R=1; R<=20; R++){\n for(int C=1; C<=20; C++){\n for(int S=1; S<=20; S++){\n int r=0,c=0,s=0;\n bool f = true;\n rep(i,n){\n int cnt = 0;\n while(cnt < a[i].size() && a[i][cnt] == '.') cnt++;\n if(cnt != R*r+C*c+S*s) f = false;\n for(auto j : a[i]){\n if(j == '(') r++;\n if(j == ')') r--;\n if(j == '{') c++;\n if(j == '}') c--;\n if(j == '[') s++;\n if(j == ']') s--;\n }\n }\n if(!f) continue;\n rep(i,m){\n int res = R*r + C*c + S*s;\n if(ans[i] != inf && ans[i] != res){\n ans[i] = -1;\n }else if(ans[i] != -1){\n ans[i] = res;\n }\n for(auto j : b[i]){\n if(j == '(') r++;\n if(j == ')') r--;\n if(j == '{') c++;\n if(j == '}') c--;\n if(j == '[') s++;\n if(j == ']') s--;\n }\n }\n }\n }\n }\n rep(i,m) cout << ans[i] << \" \\n\"[i==m-1];\n }\n}", "accuracy": 1, "time_ms": 410, "memory_kb": 3172, "score_of_the_acc": -0.8087, "final_rank": 18 }, { "submission_id": "aoj_1326_4967066", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\nusing ll = long long;\n\nstring P[10], Q[10];\nint p, q;\n\nbool match(int R, int C, int S) {\n int r = 0, c = 0, s = 0;\n for (int i = 0; i < p; i++) {\n int pos = 0;\n while (pos < P[i].size() && P[i][pos] == '.')\n pos++;\n if (pos != R * r + C * c + S * s) {\n return false;\n }\n while (pos < P[i].size()) {\n if (P[i][pos] == '(')\n r++;\n else if (P[i][pos] == ')')\n r--;\n else if (P[i][pos] == '{')\n c++;\n else if (P[i][pos] == '}')\n c--;\n else if (P[i][pos] == '[')\n s++;\n else if (P[i][pos] == ']')\n s--;\n pos++;\n }\n }\n return true;\n}\n\nint main() {\n while (1) {\n cin >> p >> q;\n if (p == 0)\n break;\n getchar();\n for (int i = 0; i < p; i++) {\n getline(cin, P[i]);\n }\n for (int i = 0; i < q; i++) {\n getline(cin, Q[i]);\n }\n int cnt = 0;\n vector<int> R, C, S;\n for (int i = 1; i <= 20; i++) {\n for (int j = 1; j <= 20; j++) {\n for (int k = 1; k <= 20; k++) {\n if (match(i, j, k)) {\n R.push_back(i);\n C.push_back(j);\n S.push_back(k);\n }\n }\n }\n }\n vector<int> ans;\n int r = 0, c = 0, s = 0;\n for (int i = 0; i < q; i++) {\n bool ok = 0;\n int indent = -1;\n for (int j = 0; j < R.size(); j++) {\n int x = R[j] * r + C[j] * c + S[j] * s;\n if (indent == -1) {\n indent = x;\n ok = 1;\n } else if (indent != x) {\n ok = 0;\n }\n }\n if (ok)\n ans.push_back(indent);\n else\n ans.push_back(-1);\n int pos = 0;\n while (pos < Q[i].size()) {\n if (Q[i][pos] == '(')\n r++;\n else if (Q[i][pos] == ')')\n r--;\n else if (Q[i][pos] == '{')\n c++;\n else if (Q[i][pos] == '}')\n c--;\n else if (Q[i][pos] == '[')\n s++;\n else if (Q[i][pos] == ']')\n s--;\n pos++;\n }\n }\n cout << ans[0];\n for (int i = 1; i < q; i++)\n cout << \" \" << ans[i];\n cout << '\\n';\n }\n}", "accuracy": 1, "time_ms": 70, "memory_kb": 3476, "score_of_the_acc": -0.3647, "final_rank": 9 }, { "submission_id": "aoj_1326_4931380", "code_snippet": "#pragma GCC optimize(\"Ofast\")\n#include <iostream> // cout, endl, cin\n#include <string> // string, to_string, stoi\n#include <vector> // vector\n#include <algorithm> // min, max, swap, sort, reverse, lower_bound, upper_bound\n#include <utility> // pair, make_pair\n#include <tuple> // tuple, make_tuple\n#include <cstdint> // int64_t, int*_t\n#include <cstdio> // printf\n#include <map> // map\n#include <queue> // queue, priority_queue\n#include <set> // set\n#include <stack> // stack\n#include <deque> // deque\n#include <unordered_map> // unordered_map\n#include <unordered_set> // unordered_set\n#include <bitset> // bitset\n#include <cctype> // isupper, islower, isdigit, toupper, tolower\n#include <iomanip> // setprecision\n#include <complex> // complex\n#include <math.h> \n#include <functional>\n#include <cassert>\nusing namespace std;\nusing ll = long long;\nusing P = pair<int,int>;\nconstexpr ll INF = 1e18;\nconstexpr int inf = 1e9;\nconstexpr ll mod = 1000000007;\nconstexpr ll mod2 = 998244353;\nconst int dx[8] = {1, 0, -1, 0,1,1,-1,-1};\nconst int dy[8] = {0, 1, 0, -1,1,-1,1,-1};\ntemplate<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }\ntemplate<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }\n// ---------------------------------------------------------------------------\n\nbool solve(){\n int N,M;\n cin >> N >> M;\n if(N==0 && M==0) return false;\n vector<string> GN(N);\n for(int i=0; i<N; i++) cin >> GN[i];\n vector<string> GM(M);\n for(int i=0; i<M; i++) cin >> GM[i]; \n // (){}[]cnt\n vector<pair<vector<int>,int>> p;\n vector<int> cnt(3,0);\n string in = \"({[\";\n string out = \")}]\";\n for(int i=0; i<N; i++){\n int num = 0;\n for(int j=0; j<GN[i].size(); j++){\n if(GN[i][j] != '.') break;\n num++;\n }\n p.emplace_back(cnt,num);\n for(int j=0; j<GN[i].size(); j++){\n for(int k=0; k<3; k++){\n if(GN[i][j] == in[k]) cnt[k]++;\n if(GN[i][j] == out[k]) cnt[k]--;\n }\n }\n }\n vector<vector<int>> num;\n for(int i=1; i<=20; i++){\n for(int j=1; j<=20; j++){\n for(int k=1; k<=20; k++){\n bool ok = true;\n for(int l=0; l<p.size(); l++){\n vector<int> now;\n int cnt;\n tie(now,cnt) = p[l];\n if(i*now[0]+j*now[1]+k*now[2] != cnt) ok = false;\n }\n if(ok){\n vector<int> pu(3);\n pu[0] = i;\n pu[1] = j;\n pu[2] = k;\n num.emplace_back(pu);\n }\n }\n }\n }\n vector<int> ans(M,-1);\n for(int i=0; i<M; i++){\n set<int> can;\n for(int j=0; j<num.size(); j++){\n int now = 0;\n for(int k=0; k<3; k++){\n now += cnt[k]*num[j][k];\n }\n can.emplace(now);\n }\n if(can.size() == 1) ans[i] = *can.begin();\n for(int j=0; j<GM[i].size(); j++){\n for(int k=0; k<3; k++){\n if(GM[i][j] == in[k]) cnt[k]++;\n if(GM[i][j] == out[k]) cnt[k]--;\n }\n }\n }\n for(int i=0; i<M; i++){\n cout << ans[i] << \" \\n\"[i+1==M];\n }\n return true;\n}\n\n\nint main(){\n ios::sync_with_stdio(false);\n cin.tie(nullptr);\n while(solve());\n return 0;\n}", "accuracy": 1, "time_ms": 30, "memory_kb": 4116, "score_of_the_acc": -0.6366, "final_rank": 16 }, { "submission_id": "aoj_1326_4416871", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n#define SZ(x) (int)(x.size())\n#define REP(i, n) for(int i=0;i<(n);++i)\n#define FOR(i, a, b) for(int i=(a);i<(b);++i)\n#define RREP(i, n) for(int i=(int)(n)-1;i>=0;--i)\n#define RFOR(i, a, b) for(int i=(int)(b)-1;i>=(a);--i)\n#define ALL(a) a.begin(),a.end()\n#define DUMP(x) cerr<<#x<<\" = \"<<(x)<<endl\n#define DEBUG(x) cerr<<#x<<\" = \"<<(x)<<\" (L\"<<__LINE__<<\")\"<< endl;\nusing ll = long long;\nusing vi = vector<int>;\nusing vvi = vector<vi>;\nusing vll = vector<ll>;\nusing vvll = vector<vll>;\nusing P = pair<int, int>;\nconst double eps = 1e-8;\nconst ll MOD = 1000000007;\nconst int INF = INT_MAX / 2;\nconst ll LINF = LLONG_MAX / 2;\ntemplate <typename T1, typename T2>\nbool chmax(T1 &a, const T2 &b) {\n if (a < b) { a = b; return true; }\n return false;\n}\ntemplate <typename T1, typename T2>\nbool chmin(T1 &a, const T2 &b) {\n if (a > b) { a = b; return true; }\n return false;\n}\ntemplate<typename T1, typename T2>\nostream &operator<<(ostream &os, const pair<T1, T2> &p) {\n os << p.first << \":\" << p.second;\n return os;\n}\ntemplate<typename T1, typename T2>\nostream &operator<<(ostream &os, const map<T1, T2> &mp) {\n os << \"{\";\n int a = 0;\n for (auto &tp : mp) {\n if (a) os << \", \"; a = 1;\n os << tp;\n }\n return os << \"}\";\n}\ntemplate<typename T>\nostream &operator<<(ostream &os, const vector<T> &v) {\n os << \"[\";\n REP(i, SZ(v)) {\n if (i) os << \", \";\n os << v[i];\n }\n return os << \"]\";\n}\n\n\nint main() {\n cin.tie(0);\n ios::sync_with_stdio(false);\n cout << fixed << setprecision(10);\n\n for (;;) {\n int p, q; cin >> p >> q;\n if (p == 0) break;\n vector<map<char,int>> cnt(p);\n vector<int> indent(p);\n map<char,int> count;\n REP(i, p) {\n string s; cin >> s;\n int j = 0;\n while (j < SZ(s) and s[j] == '.') ++j;\n indent[i] = j;\n for (; j < SZ(s); ++j) {\n if (s[j] == '(') ++count['('];\n if (s[j] == ')') --count['('];\n if (s[j] == '{') ++count['{'];\n if (s[j] == '}') --count['{'];\n if (s[j] == '[') ++count['['];\n if (s[j] == ']') --count['['];\n }\n cnt[i] = count;\n }\n\n vector<tuple<int,int,int>> cands;\n FOR(r, 1, 21) {\n FOR(c, 1, 21) {\n FOR(s, 1, 21) {\n int valid = 1;\n REP(i, p-1) {\n if (r * cnt[i]['('] + c * cnt[i]['{'] + s * cnt[i]['['] != indent[i+1]) valid = 0;\n }\n if (valid) {\n cands.emplace_back(r, c, s);\n }\n }\n }\n }\n\n count.clear();\n vi ans(q);\n REP(i, q) {\n string s; cin >> s;\n if (i == q-1) continue;\n for (char c : s) {\n if (c == '(') ++count['('];\n if (c == ')') --count['('];\n if (c == '{') ++count['{'];\n if (c == '}') --count['{'];\n if (c == '[') ++count['['];\n if (c == ']') --count['['];\n }\n\n map<int,int> value;\n for (auto &tp : cands) {\n int r, c, s; tie(r, c, s) = tp;\n value[r * count['('] + c * count['{'] + s * count['[']] = 1;\n }\n if (value.size() > 1) ans[i+1] = -1;\n else ans[i+1] = value.begin()->first;\n }\n\n REP(i, q) {\n if (i) cout << \" \";\n cout << ans[i];\n }\n cout << endl;\n\n }\n\n return 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3352, "score_of_the_acc": -0.191, "final_rank": 5 }, { "submission_id": "aoj_1326_3967206", "code_snippet": "#include <algorithm>\n#include <iostream>\n#include <vector>\n#include <map>\n#include <set>\n#include <tuple>\nusing namespace std;\n\nint main() {\n int p, q;\n while (cin >> p >> q, p) {\n vector<vector<int>> a(p, vector<int>(3));\n vector<int> b(p);\n {\n map<char, int> cnt;\n for (int i = 0; i < p; i++) {\n a[i][0] = cnt['('] - cnt[')'];\n a[i][1] = cnt['{'] - cnt['}'];\n a[i][2] = cnt['['] - cnt[']'];\n string s; cin >> s;\n int& ind = b[i];\n while (ind < s.size() && s[ind] == '.') ind++;\n for (auto c: s) cnt[c]++;\n }\n }\n set<tuple<int, int, int>> se;\n for (int r = 1; r <= 20; r++) {\n for (int c = 1; c <= 20; c++) {\n for (int s = 1; s <= 20; s++) {\n bool ok = true;\n for (int i = 0; i < p; i++) {\n ok &= a[i][0] * r + a[i][1] * c + a[i][2] * s == b[i];\n }\n if (ok) se.emplace(r, c, s);\n }\n }\n }\n vector<int> res(q); {\n map<char, int> cnt;\n for (int i = 0; i < q; i++) {\n set<int> ans;\n for (auto &t: se) {\n int tmp = 0;\n tmp += (cnt['('] - cnt[')']) * get<0>(t);\n tmp += (cnt['{'] - cnt['}']) * get<1>(t);\n tmp += (cnt['['] - cnt[']']) * get<2>(t);\n ans.emplace(tmp);\n }\n res[i] = ans.size() == 1 ? *ans.begin() : -1;\n string s; cin >> s;\n for (auto c: s) cnt[c]++;\n }\n }\n for (int i = 0; i < q; i++) cout << res[i] << \" \\n\"[i == q - 1];\n }\n return 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3644, "score_of_the_acc": -0.3476, "final_rank": 7 }, { "submission_id": "aoj_1326_3896889", "code_snippet": "#include <bits/stdc++.h>\n#define GET_MACRO(_1,_2,_3,_4,_5,_6,_7,_8,NAME,...) NAME\n#define pr(...) cerr<< GET_MACRO(__VA_ARGS__,pr8,pr7,pr6,pr5,pr4,pr3,pr2,pr1)(__VA_ARGS__) <<endl\n#define pr1(a) (#a)<<\"=\"<<(a)<<\" \"\n#define pr2(a,b) pr1(a)<<pr1(b)\n#define pr3(a,b,c) pr1(a)<<pr2(b,c)\n#define pr4(a,b,c,d) pr1(a)<<pr3(b,c,d)\n#define pr5(a,b,c,d,e) pr1(a)<<pr4(b,c,d,e)\n#define pr6(a,b,c,d,e,f) pr1(a)<<pr5(b,c,d,e,f)\n#define pr7(a,b,c,d,e,f,g) pr1(a)<<pr6(b,c,d,e,f,g)\n#define pr8(a,b,c,d,e,f,g,h) pr1(a)<<pr7(b,c,d,e,f,g,h)\n#define prArr(a) {cerr<<(#a)<<\"={\";int i=0;for(auto t:(a))cerr<<(i++?\", \":\"\")<<t;cerr<<\"}\"<<endl;}\nusing namespace std;\nusing Int = long long;\nusing _int = int;\nusing ll = long long;\nusing Double = long double;\nconst Int INF = (1LL<<60)+1e9; // ~ 1.15 * 1e18\nconst Int mod = (1e9)+7;\nconst Double EPS = 1e-8;\nconst Double PI = 6.0 * asin((Double)0.5);\nusing P = pair<Int,Int>;\ntemplate<class T> T Max(T &a,T b){return a=max(a,b);}\ntemplate<class T> T Min(T &a,T b){return a=min(a,b);}\ntemplate<class T1, class T2> ostream& operator<<(ostream& o,pair<T1,T2> p){return o<<\"(\"<<p.first<<\",\"<<p.second<<\")\";}\ntemplate<class T1, class T2, class T3> ostream& operator<<(ostream& o,tuple<T1,T2,T3> t){\n return o<<\"(\"<<get<0>(t)<<\",\"<<get<1>(t)<<\",\"<<get<2>(t)<<\")\";}\ntemplate<class T1, class T2> istream& operator>>(istream& i,pair<T1,T2> &p){return i>>p.first>>p.second;}\ntemplate<class T> ostream& operator<<(ostream& o,vector<T> a){Int i=0;for(T t:a)o<<(i++?\" \":\"\")<<t;return o;}\ntemplate<class T> istream& operator>>(istream& i,vector<T> &a){for(T &t:a)i>>t;return i;}\n//INSERT ABOVE HERE\n\n\n\nvoid count(Int &a, Int &b, Int &c, char ch){\n if(ch == '(') a++;\n if(ch == ')') a--;\n if(ch == '{') b++;\n if(ch == '}') b--;\n if(ch == '[') c++;\n if(ch == ']') c--;\n assert(a >= 0 && b >= 0 && c >= 0);\n}\n\nInt check(vector<string> &code, Int R, Int C, Int S){\n Int a = 0, b = 0, c = 0;\n for(string &s: code){\n Int idx = 0;\n Int cnt = 0;\n while(idx < (Int)s.size() && s[idx] == '.') idx++, cnt++;\n\n if(R * a + C * b + S * c != cnt) return 0;\n while(idx < (Int)s.size()) count(a, b, c, s[idx++]);\n }\n assert(a == 0 && b == 0 && c == 0);\n return 1;\n}\n\nusing T = tuple<Int,Int,Int>;\nmap<T, Int> calcMem(vector<string> &code){\n Int a = 0, b = 0, c = 0;\n map<T, Int> res;\n for(string &s: code){\n Int idx = 0;\n Int cnt = 0;\n while(idx < (Int)s.size() && s[idx] == '.') idx++, cnt++;\n res[T(a, b, c)] = cnt;\n while(idx < (Int)s.size()) count(a, b, c, s[idx++]);\n }\n return res;\n}\n\n\n\n\nsigned main(){\n srand((unsigned)time(NULL));\n cin.tie(0);\n ios_base::sync_with_stdio(0);\n cout << fixed << setprecision(12);\n\n while(1){\n Int n, m;\n cin>>n>>m;\n if(n == 0 && m == 0) break;\n vector<string> code1(n);\n cin>>code1;\n vector<string> code2(m);\n cin>>code2;\n\n set<Int> R, C, S;\n for(Int i=1;i<=20;i++)\n for(Int j=1;j<=20;j++)\n for(Int k=1;k<=20;k++){\n if(!check(code1, i, j, k)) continue;\n R.insert(i);\n C.insert(j);\n S.insert(k);\n }\n\n {\n map<T, Int> mem = calcMem(code1);\n vector<Int> ans;\n Int a = 0, b = 0, c = 0;\n for(string s:code2){\n Int num = 0;\n Int valid = 1;\n if(a != 0){\n valid &= R.size() == 1;\n if(valid) num += *R.begin() * a;\n }\n if(b != 0){\n valid &= C.size() == 1;\n if(valid) num += *C.begin() * b;\n }\n if(c != 0){\n valid &= S.size() == 1;\n if(valid) num += *S.begin() * c;\n }\n\n if(mem.count(T(a, b, c))) {valid = 1, num = mem[T(a, b, c)];}\n\n Int idx = 0;\n while(idx < (Int)s.size()) count(a, b, c, s[idx++]);\n if(!valid) ans.push_back(-1);\n else ans.push_back(num);\n }\n cout<<ans<<endl;\n }\n }\n return 0;\n}", "accuracy": 1, "time_ms": 150, "memory_kb": 3228, "score_of_the_acc": -0.3745, "final_rank": 10 }, { "submission_id": "aoj_1326_3832743", "code_snippet": "#if __has_include(\"../library/Basic/Debug.hpp\")\n\n#include \"../library/Basic/Debug.hpp\"\n\n#else\n\n/* ----- Header Files ----- */\n// IO\n#include <cstdio>\n#include <iomanip>\n#include <ios>\n#include <iostream>\n\n// algorithm\n#include <algorithm>\n#include <cmath>\n#include <numeric>\n\n// container\n#include <vector>\n#include <string>\n#include <tuple>\n#include <set>\n#include <map>\n#include <stack>\n#include <queue>\n#include <deque>\n\n// others\n#include <random>\n#include <limits>\n#include <functional>\n#include <ctime>\n#include <cassert>\n#include <cstdint>\n\n\n/* ----- Type Alias ----- */\nusing Int = long long int;\nusing Real = long double;\nusing String = std::string;\ntemplate <class... Ts>\nusing Tuple = std::tuple<Ts...>;\n\ntemplate <class T>\nusing Vector = std::vector<T>;\ntemplate <class T>\nusing Queue = std::queue<T>;\ntemplate <class T>\nusing Stack = std::stack<T>;\ntemplate <class T>\nusing Deque = std::deque<T>;\n\ntemplate <class T>\nusing MaxHeap = std::priority_queue<T>;\ntemplate <class T>\nusing MinHeap = std::priority_queue<T, std::vector<T>, std::greater<T>>;\ntemplate <class T>\nusing Set = std::set<T>;\ntemplate <class T, class U>\nusing Map = std::map<T, U>;\n\ntemplate <class T, class... Us>\nusing Func = std::function<T(Us...)>;\n\ntemplate <class T>\nT genv(T v) { return v; }\n\ntemplate <class T, class... Ts>\nauto genv(size_t l, Ts... ts) {\n return Vector<decltype(genv<T>(ts...))>(l, Vector<T>(ts...));\n}\n\ntemplate <class Cost = Int>\nstruct Edge {\n Int src, dst;\n Cost cost;\n Edge(Int src = -1, Int dst = -1, Cost cost = 1)\n : src(src), dst(dst), cost(cost){};\n\n bool operator<(const Edge<Cost>& e) const { return this->cost < e.cost; }\n bool operator>(const Edge<Cost>& e) const { return this->cost > e.cost; }\n};\n\ntemplate <class Cost = Int>\nusing Edges = Vector<Edge<Cost>>;\ntemplate <class Cost = Int>\nusing Graph = Vector<Vector<Edge<Cost>>>;\n\n#endif\n\n/* ----- Misc ----- */\nvoid fastio() {\n std::ios::sync_with_stdio(false);\n std::cin.tie(nullptr);\n}\n\nstruct Fout {\n Int precision;\n Fout(Int precision) : precision(precision) {}\n};\nstd::ostream& operator<<(std::ostream& os, const Fout& fio) {\n os << std::fixed << std::setprecision(fio.precision);\n return os;\n}\n\n\n/* ----- Constants ----- */\n// constexpr Int INF = std::numeric_limits<Int>::max() / 1000;\n// constexpr Int MOD = 1000000007;\n// constexpr Real PI = acos(-1);\n// constexpr Real EPS = 1e-10;\n// std::mt19937 mt(int(std::time(nullptr)));\n\nInt parse(const String& s, Vector<Int>& cnt) {\n Int i;\n for (i = 0; s[i] == '.'; ++i) {}\n\n for (char c : s) {\n switch (c) {\n case '(':\n ++cnt[0];\n break;\n case ')':\n --cnt[0];\n break;\n case '{':\n ++cnt[1];\n break;\n case '}':\n --cnt[1];\n break;\n case '[':\n ++cnt[2];\n break;\n case ']':\n --cnt[2];\n break;\n default:\n break;\n }\n }\n return i;\n}\n\nbool solve() {\n Int n, m;\n std::cin >> n >> m;\n if (n == 0) return false;\n\n Vector<Tuple<Vector<Int>, Int>> lines(n);\n Vector<Int> cnt(3, 0);\n for (auto& l : lines) {\n String s;\n std::cin >> s;\n\n Vector<Int> prev = cnt;\n Int dots = parse(s, cnt);\n l = std::make_tuple(prev, dots);\n }\n\n Vector<Vector<Int>> pos;\n Vector<Int> x(3);\n for (x[0] = 1; x[0] <= 20; ++x[0]) {\n for (x[1] = 1; x[1] <= 20; ++x[1]) {\n for (x[2] = 1; x[2] <= 20; ++x[2]) {\n bool judge = true;\n\n for (auto l : lines) {\n Vector<Int> v;\n Int s;\n std::tie(v, s) = l;\n\n for (Int i = 0; i < 3; ++i) s -= v[i] * x[i];\n if (s != 0) judge = false;\n }\n\n if (judge) pos.push_back(x);\n }\n }\n }\n\n fill(cnt.begin(), cnt.end(), 0);\n for (Int i = 0; i < m; ++i) {\n String s;\n std::cin >> s;\n\n Vector<Int> prev = cnt;\n parse(s, cnt);\n\n Set<Int> ans;\n for (const auto& x : pos) {\n Int sum = 0;\n for (Int i = 0; i < 3; ++i) sum += x[i] * prev[i];\n ans.insert(sum);\n }\n\n std::cout << (ans.size() == 1 ? *ans.begin() : -1) << \" \\n\"[i == m - 1];\n }\n return true;\n}\n\nint main() {\n while (solve()) {}\n return 0;\n}", "accuracy": 1, "time_ms": 120, "memory_kb": 3392, "score_of_the_acc": -0.4089, "final_rank": 11 }, { "submission_id": "aoj_1326_3703883", "code_snippet": "#include <bits/stdc++.h>\n\nusing namespace std;\n\ntypedef long long ll;\ntypedef unsigned long long ull;\ntypedef pair<ll, ll> P;\ntypedef vector<ll> V;\ntypedef complex<double> Point;\n\n#define PI acos(-1.0)\n#define EPS 1e-10\nconst ll INF = 1e16;\nconst ll MOD = 1e9 + 7;\n\n#define FOR(i,a,b) for(int i=(a);i<(b);i++)\n#define rep(i,N) for(int i=0;i<(N);i++)\n#define ALL(s) (s).begin(),(s).end()\n#define EQ(a,b) (abs((a)-(b))<EPS)\n#define EQV(a,b) ( EQ((a).real(), (b).real()) && EQ((a).imag(), (b).imag()) )\n#define fi first\n#define se second\n#define N_SIZE (1LL << 20)\n#define NIL -1\n\nll mod_add(ll a, ll b) { return (a + b) % MOD; }\nll mod_sub(ll a, ll b) { return (a - b + MOD) % MOD; }\nll mod_mul(ll a, ll b) { return a*b % MOD; }\n\nll p,q;\n\nvector<string> PP,Q;\nvector<ll> ans;\n\nchar ot[] = {'(','{','['};\nchar ct[] = {')','}',']'};\n\nbool check(ll r,ll c,ll s){\n ll cost[] = {r,c,s};\n ll sum = 0;\n rep(i,PP.size() - 1){\n ll cnt = 0;\n rep(j,PP[i].size()){\n rep(k,3){\n if(ot[k] == PP[i][j])sum += cost[k];\n if(ct[k] == PP[i][j])sum -= cost[k];\n }\n }\n\n rep(j,PP[i+1].size()){\n if(PP[i+1][j] != '.')break;\n cnt++;\n }\n if(cnt != sum)return false;\n }\n return true;\n}\n\nvoid solve(ll r,ll c,ll s){\n ll cnt[3] = {}; \n rep(i,Q.size() - 1){\n rep(j,Q[i].size()){\n rep(k,3){\n if(ot[k] == Q[i][j])cnt[k]++;\n if(ct[k] == Q[i][j])cnt[k]--;\n }\n }\n ll nn = cnt[0]*r + cnt[1]*c + cnt[2]*s;\n if(ans[i] == 0)ans[i] = nn;\n else if(ans[i] != nn)ans[i] = -1;\n }\n}\n\nint main(){\n while(cin >> p >> q && p){\n PP.clear();\n Q.clear();\n ans.clear();\n ans.resize(q-1,0);\n rep(i,p){\n string s;\n cin >> s;\n PP.push_back(s);\n }\n rep(i,q){\n string s;\n cin >> s;\n Q.push_back(s);\n }\n FOR(r,1,21){\n FOR(c,1,21){\n FOR(s,1,21){\n if(check(r,c,s)){\n // cout << r << \" \" << c << \" \" << s << endl;\n solve(r,c,s);\n }\n }\n }\n }\n cout << 0 << \" \";\n rep(i,ans.size()){\n if(i != ans.size() - 1)cout << ans[i] << \" \";\n else cout << ans[i] << endl;\n }\n }\n}", "accuracy": 1, "time_ms": 300, "memory_kb": 3140, "score_of_the_acc": -0.5951, "final_rank": 15 }, { "submission_id": "aoj_1326_3637837", "code_snippet": "#include<iostream>\n#include<string>\n#include<algorithm>\n#include<vector>\n#include<iomanip>\n#include<math.h>\n#include<complex>\n#include<queue>\n#include<deque>\n#include<stack>\n#include<map>\n#include<set>\n#include<bitset>\n#include<functional>\n#include<assert.h>\n#include<numeric>\nusing namespace std;\n#define REP(i,m,n) for(int i=(int)(m) ; i < (int) (n) ; ++i )\n#define rep(i,n) REP(i,0,n)\nusing ll = long long;\nconst int inf=1e9+7;\nconst ll longinf=1LL<<60 ;\nconst ll mod=1e9+7 ;\n\nint a[100][100][100];\n\nvoid solve(int n,int m){\n map<int,int> mp;\n int x=0,y=0,z=0;\n rep(i,n){\n string s;cin>>s;\n int c=0;\n while(c<s.size()&&s[c]=='.')++c;\n mp[10000*x+100*y+z]=c;\n rep(j,s.size()){\n if(s[j]=='(')++x;\n if(s[j]==')')--x;\n if(s[j]=='{')++y;\n if(s[j]=='}')--y;\n if(s[j]=='[')++z;\n if(s[j]==']')--z;\n }\n }\n vector<int> P,Q,R;\n REP(i,1,21)REP(j,1,21)REP(k,1,21){\n bool ok=true;\n for(auto p : mp){\n if(i*(p.first/10000)+j*(p.first/100%100)+k*(p.first%100)!=p.second)ok=false;\n }\n if(ok){\n P.push_back(i);\n Q.push_back(j);\n R.push_back(k);\n }\n }\n x=0,y=0,z=0;\n rep(i,m){\n string s;\n cin>>s;\n if(mp.count(10000*x+100*y+z))cout<<mp[10000*x+100*y+z];\n else {\n set<int> st;\n rep(j,P.size()){\n st.insert(x*P[j]+y*Q[j]+z*R[j]);\n }\n if(st.size()==1)cout<<*st.begin();\n else cout<<-1;\n }\n rep(j,s.size()){\n if(s[j]=='(')++x;\n if(s[j]==')')--x;\n if(s[j]=='{')++y;\n if(s[j]=='}')--y;\n if(s[j]=='[')++z;\n if(s[j]==']')--z;\n }\n if(i!=m-1)cout<<\" \";\n else cout<<endl;\n }\n}\nint main(){\n int n,m;\n while(cin>>n>>m,n)solve(n,m);\n return 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3344, "score_of_the_acc": -0.1867, "final_rank": 4 }, { "submission_id": "aoj_1326_3635083", "code_snippet": "#include <iostream>\n#include <algorithm>\n#include <iomanip>\n#include <map>\n#include <set>\n#include <queue>\n#include <stack>\n#include <numeric>\n#include <bitset>\n#include <cmath>\n\nstatic const int MOD = 1000000007;\nusing ll = long long;\nusing u32 = uint32_t;\nusing namespace std;\n\ntemplate<class T> constexpr T INF = ::numeric_limits<T>::max()/32*15+208;\nvoid solve(int p, int q){\n vector<string> v(p), u(q);\n for (auto &&i : v) cin >> i;\n for (auto &&i : u) cin >> i;\n vector<int> ans(q, -2);\n for (int R = 1; R <= 20; ++R) {\n for (int C = 1; C <= 20; ++C) {\n for (int S = 1; S <= 20; ++S) {\n int cnt1 = 0, cnt2 = 0, cnt3 = 0;\n int ok = 1;\n for (int i = 0; i < p; ++i) {\n int x = 0;\n for (auto &&j : v[i]) {\n if(j == '.') x++;\n else break;\n }\n if(x != R*cnt1+C*cnt2+S*cnt3){\n ok = 0;\n break;\n }\n for (auto &&j : v[i]) {\n if(j == '(') cnt1++;\n if(j == ')') cnt1--;\n if(j == '{') cnt2++;\n if(j == '}') cnt2--;\n if(j == '[') cnt3++;\n if(j == ']') cnt3--;\n }\n }\n if(!ok) continue;\n for (int i = 0; i < q; ++i) {\n if(ans[i] == -2){\n ans[i] = R*cnt1+C*cnt2+S*cnt3;\n } else if(ans[i] != R*cnt1+C*cnt2+S*cnt3){\n ans[i] = -1;\n }\n for (auto &&j : u[i]) {\n if(j == '(') cnt1++;\n if(j == ')') cnt1--;\n if(j == '{') cnt2++;\n if(j == '}') cnt2--;\n if(j == '[') cnt3++;\n if(j == ']') cnt3--;\n }\n }\n }\n }\n }\n for (int i = 0; i < q; ++i) {\n if(i) printf(\" \");\n printf(\"%d\", ans[i]);\n }\n puts(\"\");\n}\nint main() {\n int p, q;\n while(cin >> p >> q, p){\n solve(p, q);\n }\n return 0;\n}", "accuracy": 1, "time_ms": 150, "memory_kb": 3180, "score_of_the_acc": -0.3487, "final_rank": 8 }, { "submission_id": "aoj_1326_3372204", "code_snippet": "#include<iostream>\n#include<vector>\n#include<algorithm>\nusing namespace std;\nint n,m;\nstring ss[10];\nmain()\n{\n\twhile(cin>>n>>m,n)\n\t{\n\t\tfor(int i=0;i<n;i++)cin>>ss[i];\n\t\tvector<int>R,C,S;\n\t\tfor(int r=1;r<21;r++)for(int c=1;c<21;c++)for(int s=1;s<21;s++)\n\t\t{\n\t\t\tbool flag=1;\n\t\t\tint cr=0,cc=0,cs=0;\n\t\t\tfor(int i=0;i<n;i++)\n\t\t\t{\n\t\t\t\tint cnt=0;\n\t\t\t\tfor(int j=0;j<ss[i].size()&&ss[i][j]=='.';j++)cnt++;\n\t\t\t\tif(cr*r+cc*c+cs*s!=cnt)\n\t\t\t\t{\n\t\t\t\t\tflag=0;\n\t\t\t\t\tbreak;\n\t\t\t\t}\n\t\t\t\tfor(int j=0;j<ss[i].size();j++)\n\t\t\t\t{\n\t\t\t\t\tif(ss[i][j]=='(')cr++;\n\t\t\t\t\telse if(ss[i][j]==')')cr--;\n\t\t\t\t\telse if(ss[i][j]=='{')cc++;\n\t\t\t\t\telse if(ss[i][j]=='}')cc--;\n\t\t\t\t\telse if(ss[i][j]=='[')cs++;\n\t\t\t\t\telse if(ss[i][j]==']')cs--;\n\t\t\t\t}\n\t\t\t}\n\t\t\tif(flag)\n\t\t\t{\n\t\t\t\tR.push_back(r);\n\t\t\t\tC.push_back(c);\n\t\t\t\tS.push_back(s);\n\t\t\t}\n\t\t}\n\t\tvector<int>ans;\n\t\tint cr=0,cc=0,cs=0;\n\t\tfor(;m--;)\n\t\t{\n\t\t\tstring t;cin>>t;\n\t\t\tint cnt=-1;\n\t\t\tfor(int j=0;j<R.size();j++)\n\t\t\t{\n\t\t\t\tint now=cr*R[j]+cc*C[j]+cs*S[j];\n\t\t\t\tif(cnt<0)cnt=now;\n\t\t\t\telse\n\t\t\t\t{\n\t\t\t\t\tif(cnt!=now)cnt=114514;\n\t\t\t\t}\n\t\t\t}\n\t\t\tif(cnt==114514)ans.push_back(-1);\n\t\t\telse ans.push_back(cnt);\n\t\t\tfor(int j=0;j<t.size();j++)\n\t\t\t{\n\t\t\t\tif(t[j]=='(')cr++;\n\t\t\t\telse if(t[j]==')')cr--;\n\t\t\t\telse if(t[j]=='{')cc++;\n\t\t\t\telse if(t[j]=='}')cc--;\n\t\t\t\telse if(t[j]=='[')cs++;\n\t\t\t\telse if(t[j]==']')cs--;\n\t\t\t}\n\t\t}\n\t\tfor(int i=0;i<ans.size();i++)cout<<ans[i]<<(i+1==ans.size()?\"\\n\":\" \");\n\t}\n}", "accuracy": 1, "time_ms": 90, "memory_kb": 3284, "score_of_the_acc": -0.2974, "final_rank": 6 }, { "submission_id": "aoj_1326_3315042", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\n\nvoid solve(int p,int q){\n cin.ignore();\n string buf;\n int ro=0,rc=0,co=0,cc=0,so=0,sc=0;\n auto incl=[&](string str){\n ro+=count(str.begin(),str.end(),'(');\n rc+=count(str.begin(),str.end(),')');\n co+=count(str.begin(),str.end(),'{');\n cc+=count(str.begin(),str.end(),'}');\n so+=count(str.begin(),str.end(),'[');\n sc+=count(str.begin(),str.end(),']');\n };\n auto cntIndent=[](string str){\n int res=0;\n for(int i=0;i<str.size();i++){\n if(str[i]!='.') break;\n res++;\n }\n return res;\n };\n vector<tuple<int,int,int>> rcs;\n for(int i=0;i<20;i++){\n for(int j=0;j<20;j++){\n for(int k=0;k<20;k++){\n rcs.push_back(make_tuple(i+1,j+1,k+1));\n }\n }\n }\n for(int i=0;i<p;i++){\n string str;\n getline(cin,str);\n vector<tuple<int,int,int>> tmp;\n int x=cntIndent(str);\n for(int i=0;i<rcs.size();i++){\n if(get<0>(rcs[i])*(ro-rc)+get<1>(rcs[i])*(co-cc)+get<2>(rcs[i])*(so-sc)==x){\n tmp.push_back(rcs[i]);\n }\n }\n rcs=tmp;\n incl(str);\n }\n ro=rc=0,co=cc=0,so=sc=0;\n vector<int> res;\n for(int i=0;i<q;i++){\n string str;\n getline(cin,str);\n int y=get<0>(rcs[0])*(ro-rc)+get<1>(rcs[0])*(co-cc)+get<2>(rcs[0])*(so-sc);\n for(int i=0;i<rcs.size();i++){\n if(get<0>(rcs[i])*(ro-rc)+get<1>(rcs[i])*(co-cc)+get<2>(rcs[i])*(so-sc)!=y){\n y=-1;\n }\n }\n res.push_back(y);\n incl(str);\n }\n for(int i=0;i<q;i++){\n cout<<res[i]<<(i+1==q ? \"\\n\" : \" \");\n }\n return;\n}\nint main(){\n int p,q;\n while(cin>>p>>q,p){\n solve(p,q);\n }\n return 0; \n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3228, "score_of_the_acc": -0.1245, "final_rank": 2 }, { "submission_id": "aoj_1326_3167991", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\nint main(){\n int n,m;\n while(cin>>n>>m,n){\n string s1[11],s2[11];\n for(int i=0;i<n;i++)cin>>s1[i];\n for(int i=0;i<m;i++)cin>>s2[i];\n\n int pi[11];\n for(int i=0;i<n;i++){\n int j=0;\n while(s1[i][j]=='.'){\n j++;\n }\n pi[i]=j;\n }\n\n // for(int i=0;i<n;i++)cout<<\"p \"<<pi[i]<<endl;\n \n int ans[11];\n fill_n(ans,11,-2);\n ans[0]=0;\n \n for(int r=1;r<=20;r++){\n for(int c=1;c<=20;c++){\n for(int s=1;s<=20;s++){\n\t//cout<<r<<\" \"<<c<<\" \"<<s<<endl;\n\tint f=1;\n\t\n\tint cnt[3]={};\n\tfor(int i=0;i<n;i++){\n\t \n\t int t=r*cnt[0]+c*cnt[1]+s*cnt[2];\n\t //if(r==9&&c==5&&s==2)cout<<pi[i]<<\" \"<<t<<endl;\n\t if(t!=pi[i])f=0;\n\t \n\t for(int j=0;j<s1[i].size();j++){\n\t if(s1[i][j]=='(')cnt[0]++;\n\t if(s1[i][j]==')')cnt[0]--;\n\t if(s1[i][j]=='{')cnt[1]++;\n\t if(s1[i][j]=='}')cnt[1]--;\n\t if(s1[i][j]=='[')cnt[2]++;\n\t if(s1[i][j]==']')cnt[2]--;\n\t }\n\t}\n\t\n\tif(f==0)continue;\n\t//cout<<r<<\" \"<<c<<\" \"<<s<<endl;\n\t\n\tcnt[0]=cnt[1]=cnt[2]=0;\n\tfor(int i=0;i<m;i++){\n\t \n\t int t=r*cnt[0]+c*cnt[1]+s*cnt[2];\n\t \n\t if(ans[i]==-2)ans[i]=t;\n\t else if(ans[i]!=t)ans[i]=-1;\n\t \n\t for(int j=0;j<s2[i].size();j++){\n\t if(s2[i][j]=='(')cnt[0]++;\n\t if(s2[i][j]==')')cnt[0]--;\n\t if(s2[i][j]=='{')cnt[1]++;\n\t if(s2[i][j]=='}')cnt[1]--;\n\t if(s2[i][j]=='[')cnt[2]++;\n\t if(s2[i][j]==']')cnt[2]--;\n\t }\n\t}\n\t\n }\n }\n }\n for(int i=0;i<m;i++){\n if(i)cout<<\" \";\n cout<<ans[i];\n }\n\n cout<<endl;\n }\n return 0;\n}", "accuracy": 1, "time_ms": 290, "memory_kb": 2996, "score_of_the_acc": -0.5, "final_rank": 14 }, { "submission_id": "aoj_1326_2876276", "code_snippet": "#include <iostream>\n#include <string>\n#include <tuple>\n#include <unordered_set>\n#include <utility>\n#include <vector>\n\nusing namespace std;\n\nint main()\n{\n while (true) {\n int p, q;\n cin >> p >> q;\n if (p == 0 && q == 0) {\n return 0;\n }\n\n vector<string> P;\n for (int i = 0; i < p; ++i) {\n string a;\n cin >> a;\n P.push_back(a);\n }\n\n auto processLine = [](const string& line) {\n auto aoi = 0;\n auto groupingNotFound = true;\n auto cr = make_pair(0, 0);\n auto cc = make_pair(0, 0);\n auto cs = make_pair(0, 0);\n for (char i : line) {\n if (groupingNotFound && i == '.') {\n aoi += 1;\n }\n else {\n groupingNotFound = false;\n if (i == '(')\n cr.first += 1;\n else if (i == ')') {\n cr.second += 1;\n }\n else if (i == '{') {\n cc.first += 1;\n }\n else if (i == '}') {\n cc.second += 1;\n }\n else if (i == '[') {\n cs.first += 1;\n }\n else if (i == ']') {\n cs.second += 1;\n }\n }\n }\n return make_tuple(aoi, cr, cc, cs);\n };\n auto computeAoi = [](const tuple<int, int, int>& param,\n const pair<int, int>& r, const pair<int, int>& c,\n const pair<int, int>& s) {\n int R, C, S;\n tie(R, C, S) = param;\n return R * (r.first - r.second) + C * (c.first - c.second) +\n S * (s.first - s.second);\n };\n auto addPair = [](auto& a, const auto& b) {\n a.first += b.first;\n a.second += b.second;\n };\n\n auto candidates = [processLine, computeAoi,\n addPair](const vector<string>& v) {\n auto r = make_pair(0, 0);\n auto c = make_pair(0, 0);\n auto s = make_pair(0, 0);\n vector<tuple<int, int, int>> remainCandidates;\n for (int R = 1; R <= 20; ++R) {\n for (int C = 1; C <= 20; ++C) {\n for (int S = 1; S <= 20; ++S) {\n remainCandidates.emplace_back(R, C, S);\n }\n }\n }\n for (auto&& line : v) {\n int aoi;\n pair<int, int> cr, cc, cs;\n tie(aoi, cr, cc, cs) = processLine(line);\n\n vector<tuple<int, int, int>> temp;\n for (auto&& j : remainCandidates) {\n if (computeAoi(j, r, c, s) == aoi) {\n temp.push_back(move(j));\n }\n }\n swap(remainCandidates, temp);\n addPair(r, cr);\n addPair(c, cc);\n addPair(s, cs);\n }\n return remainCandidates;\n };\n const auto candidates_ = candidates(P);\n auto normalize = [](const vector<tuple<int, int, int>>& v) {\n tuple<unordered_set<int>, unordered_set<int>, unordered_set<int>> n;\n unordered_set<int> r, c, s;\n tie(r, c, s) = n;\n for (auto&& i : v) {\n int rr, cc, ss;\n tie(rr, cc, ss) = i;\n r.insert(rr);\n c.insert(cc);\n s.insert(ss);\n }\n return n;\n };\n const auto nn = normalize(candidates_);\n\n vector<string> Q;\n for (int i = 0; i < q; ++i) {\n string a;\n cin >> a;\n Q.push_back(a);\n }\n\n auto computeAoiByCandidates = [&candidates_,\n computeAoi](const pair<int, int>& r,\n const pair<int, int>& c,\n const pair<int, int>& s) {\n bool first = true;\n int current = -1;\n for (auto&& candidate : candidates_) {\n if (first) {\n current = computeAoi(candidate, r, c, s);\n first = false;\n }\n else {\n auto r_ = computeAoi(candidate, r, c, s);\n if (r_ != current) {\n return -1;\n }\n else {\n current = r_;\n }\n }\n }\n return current;\n };\n\n auto r = make_pair(0, 0);\n auto c = make_pair(0, 0);\n auto s = make_pair(0, 0);\n vector<int> results;\n for (auto&& line : Q) {\n int aoi;\n pair<int, int> cr, cc, cs;\n tie(aoi, cr, cc, cs) = processLine(line);\n results.push_back(computeAoiByCandidates(r, c, s));\n addPair(r, cr);\n addPair(c, cc);\n addPair(s, cs);\n }\n for (size_t i = 0; i < results.size(); ++i) {\n cout << results[i];\n if (i == results.size() - 1) {\n cout << endl;\n }\n else {\n cout << \" \";\n }\n }\n }\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3284, "score_of_the_acc": -0.1545, "final_rank": 3 }, { "submission_id": "aoj_1326_2869025", "code_snippet": "#include \"bits/stdc++.h\"\n\n#define REP(i,n) for(ll i=0;i<ll(n);++i)\n#define RREP(i,n) for(ll i=ll(n)-1;i>=0;--i)\n#define FOR(i,m,n) for(ll i=m;i<ll(n);++i)\n#define RFOR(i,m,n) for(ll i=ll(n)-1;i>=ll(m);--i)\n#define ALL(v) (v).begin(),(v).end()\n#define UNIQUE(v) v.erase(unique(ALL(v)),v.end());\n#define DUMP(v) REP(aa, (v).size()) { cout << v[aa]; if (aa != v.size() - 1)cout << \" \"; else cout << endl; }\n#define INF 1000000001ll\n#define MOD 1000000007ll\n#define EPS 1e-9\n\nconst int dx[8] = { 1,1,0,-1,-1,-1,0,1 };\nconst int dy[8] = { 0,1,1,1,0,-1,-1,-1 };\n\n\nusing namespace std;\ntypedef long long ll;\ntypedef vector<int> vi;\ntypedef vector<ll> vl;\ntypedef vector<vi> vvi;\ntypedef vector<vl> vvl;\ntypedef pair<int, int> pii;\ntypedef pair<ll, ll> pll;\nll max(ll a, int b) { return max(a, ll(b)); }\nll max(int a, ll b) { return max(ll(a), b); }\nll min(ll a, int b) { return min(a, ll(b)); }\nll min(int a, ll b) { return min(ll(a), b); }\n\n\nint main() {\n\tcin.tie(0);\n\tios::sync_with_stdio(false);\n\tint n, m;\n\twhile (cin >> n >> m, n) {\n\t\tvector<string> p(n), q(m);\n\t\tREP(i, n)cin >> p[i];\n\t\tREP(i, m)cin >> q[i];\n\t\tvi ind(n);\n\t\tREP(i, n) {\n\t\t\tint cnt = 0;\n\t\t\tREP(j, p[i].size()) {\n\t\t\t\tif (p[i][j] != '.')break;\n\t\t\t\tcnt++;\n\t\t\t}\n\t\t\tind[i] = cnt;\n\t\t}\n\t\tvi res(m, -1);\n\t\tREP(i, 20)REP(j, 20)REP(k, 20) {\n\t\t\tint r = 0, c = 0, s = 0;\n\t\t\tbool ok = true;\n\t\t\tREP(a, n) {\n\t\t\t\tif (ind[a] != (i + 1)*r + (j + 1)*c + (k + 1)*s)ok = false;\n\t\t\t\tREP(b, p[a].size()) {\n\t\t\t\t\tif (p[a][b] == '(')r++;\n\t\t\t\t\telse if (p[a][b] == ')')r--;\n\t\t\t\t\telse if (p[a][b] == '{')c++;\n\t\t\t\t\telse if (p[a][b] == '}')c--;\n\t\t\t\t\telse if (p[a][b] == '[')s++;\n\t\t\t\t\telse if (p[a][b] == ']')s--;\n\t\t\t\t}\n\n\t\t\t}\n\t\t\tif (!ok)continue;\n\t\t\tr = 0, c = 0, s = 0;\n\t\t\n\t\t\tREP(a, m) {\n\t\t\t\tif(res[a]==-1)res[a] = (i + 1)*r + (j + 1)*c + (k + 1)*s;\n\t\t\t\telse if(res[a]!=(i + 1)*r + (j + 1)*c + (k + 1)*s)res[a] = INF;\n\t\t\t\tREP(b, q[a].size()) {\n\t\t\t\t\tif (q[a][b] == '(')r++;\n\t\t\t\t\telse if (q[a][b] == ')')r--;\n\t\t\t\t\telse if (q[a][b] == '{')c++;\n\t\t\t\t\telse if (q[a][b] == '}')c--;\n\t\t\t\t\telse if (q[a][b] == '[')s++;\n\t\t\t\t\telse if (q[a][b] == ']')s--;\n\t\t\t\t}\n\t\t\t}\n\n\t\t}\n\t\tREP(i, m) {\n\t\t\tif (i)cout << \" \";\n\t\t\tif (res[i] == INF)cout << -1;\n\t\t\telse cout << res[i];\n\t\t}\n\t\tcout << endl;\n\t}\n}", "accuracy": 1, "time_ms": 360, "memory_kb": 3140, "score_of_the_acc": -0.7023, "final_rank": 17 } ]
aoj_1336_cpp
Problem B: The Last Ant A straight tunnel without branches is crowded with busy ants coming and going. Some ants walk left to right and others right to left. All ants walk at a constant speed of 1 cm/s. When two ants meet, they try to pass each other. However, some sections of the tunnel are narrow and two ants cannot pass each other. When two ants meet at a narrow section, they turn around and start walking in the opposite directions. When an ant reaches either end of the tunnel, it leaves the tunnel. The tunnel has an integer length in centimeters. Every narrow section of the tunnel is integer centimeters distant from the both ends. Except for these sections, the tunnel is wide enough for ants to pass each other. All ants start walking at distinct narrow sections. No ants will newly enter the tunnel. Consequently, all the ants in the tunnel will eventually leave it. Your task is to write a program that tells which is the last ant to leave the tunnel and when it will. Figure B.1 shows the movements of the ants during the first two seconds in a tunnel 6 centimeters long. Initially, three ants, numbered 1, 2, and 3, start walking at narrow sections, 1, 2, and 5 centimeters distant from the left end, respectively. After 0.5 seconds, the ants 1 and 2 meet at a wide section, and they pass each other. Two seconds after the start, the ants 1 and 3 meet at a narrow section, and they turn around. Figure B.1 corresponds to the first dataset of the sample input. Figure B.1. Movements of ants Input The input consists of one or more datasets. Each dataset is formatted as follows. n l d 1 p 1 d 2 p 2 ... d n p n The first line of a dataset contains two integers separated by a space. n (1 ≤ n ≤ 20) represents the number of ants, and l ( n + 1 ≤ l ≤ 100) represents the length of the tunnel in centimeters. The following n lines describe the initial states of ants. Each of the lines has two items, d i and p i , separated by a space. Ants are given numbers 1 through n . The ant numbered i has the initial direction d i and the initial position p i . The initial direction d i (1 ≤ i ≤ n ) is L (to the left) or R (to the right). The initial position p i (1 ≤ i ≤ n ) is an integer specifying the distance from the left end of the tunnel in centimeters. Ants are listed in the left to right order, that is, 1 ≤ p 1 < p 2 < ... < p n ≤ l - 1. The last dataset is followed by a line containing two zeros separated by a space. Output For each dataset, output how many seconds it will take before all the ants leave the tunnel, and which of the ants will be the last. The last ant is identified by its number. If two ants will leave at the same time, output the number indicating the ant that will leave through the left end of the tunnel. Sample Input 3 6 R 1 L 2 L 5 1 10 R 1 2 10 R 5 L 7 2 10 R 3 L 8 2 99 R 1 L 98 4 10 L 1 R 2 L 8 R 9 6 10 R 2 R 3 L 4 R 6 L 7 L 8 0 0 Output for the Sample Input 5 1 9 1 7 1 8 2 98 2 8 2 8 3
[ { "submission_id": "aoj_1336_10881257", "code_snippet": "#include<bits/stdc++.h>\n// #include<atcoder/all>\n// #include<boost/multiprecision/cpp_int.hpp>\n\nusing namespace std;\n// using namespace atcoder;\n// using bint = boost::multiprecision::cpp_int;\nusing ll = long long;\nusing ull = unsigned long long;\nusing ld = long double;\nusing pii = pair<int,int>;\nusing pll = pair<ll,ll>;\nusing vi = vector<ll>;\nusing vvi = vector<vi>;\nusing vvvi = vector<vvi>;\nusing ve = vector<vector<int>>;\nusing vb = vector<bool>;\nusing vvb = vector<vb>;\n#define rep(i,s,n) for(ll i = (ll)s;i < (ll)n;i++)\n#define rrep(i,l,r) for(ll i = r-1;i >= l;i--)\n#define ALL(x) (x).begin(),(x).end()\n#define sz(c) ((ll)(c).size())\n#define LB(A,x) (int)(lower_bound(A.begin(),A.end(),x)-A.begin())\n#define UB(A,x) (int)(upper_bound(A.begin(),A.end(),x)-A.begin())\n// #define MOD 1000000007\n#define MOD 998244353\ntemplate<typename T>using min_priority_queue=priority_queue<T,vector<T>,greater<T>>;\ntemplate<typename T>ostream&operator<<(ostream&os,vector<T>&v){for(int i = 0;i < v.size();i++)os<<v[i]<<(i+1!=v.size()?\" \":\"\");return os;}\ntemplate<typename T>istream&operator>>(istream&is,vector<T>&v){for(T&in:v)is>>in;return is;}\ntemplate<typename T1,typename T2>ostream&operator<<(ostream&os,pair<T1,T2>&p){os<<p.first<<\" \"<<p.second;return os;}\ntemplate<typename T1,typename T2>istream&operator>>(istream&is,pair<T1,T2>&p){is>>p.first>>p.second;return is;}\ntemplate<typename T> inline bool chmax(T &a,T b){if(a < b){a = b;return true;}return false;}\ntemplate<typename T> inline bool chmin(T &a,T b){if(a > b){a = b;return true;}return false;}\nld dist(ld x1,ld y1,ld x2, ld y2){return sqrtl((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2));}\n\n\nint main(){\n \n ios_base::sync_with_stdio(0), cin.tie(0);\n while(1){\n int n,L;cin >> n >> L;\n if(!n)break;\n vector<pii> v(n);\n vi C(L+1);\n rep(i,0,n){\n char d;int p;cin >> d >> p;\n if(d == 'L')v[i] = {p,-1};\n else v[i] = {p,1};\n C[p]++;\n }\n int lst = -1,tm = -1;\n rep(_,0,n*n*(L+1)+10){\n rep(i,0,n){\n auto &[p,d] = v[i];\n if(p == 0 || p == L)continue;\n C[p]--;p += d;C[p]++;\n if(p == 0){lst = i+1;tm = _;}\n else if(p == L){\n if(tm != _){lst = i+1;tm = _;}\n }\n }\n rep(i,0,n){\n auto &[p,d] = v[i];\n if(p != 0 && p != L && C[p] > 1)d *= -1;\n }\n }\n cout << tm+1 << \" \" << lst << \"\\n\";\n }\n \n\n return 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3304, "score_of_the_acc": -1, "final_rank": 6 }, { "submission_id": "aoj_1336_6791127", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\nusing vll = vector<ll>;\nusing vvll = vector<vll>;\nusing vvvll = vector<vvll>;\nusing vvvvll = vector<vvvll>;\nusing vb = vector<bool>;\nusing vvb = vector<vb>;\nusing vvvb = vector<vvb>;\nusing vd = vector<double>;\nusing vvd = vector<vd>;\nusing vvvd = vector<vvd>;\n#define all(A) A.begin(),A.end()\n#define ALL(A) A.begin(),A.end()\n#define rep(i, n) for (ll i = 0; i < (ll) (n); i++)\nusing pqr = priority_queue<pair<ll, ll>, vector<pair<ll, ll>>, greater<pair<ll, ll>>>;\n\ntemplate<class T>\nbool chmax(T& p, T q, bool C = 1) {\n if (C == 0 && p == q) {\n return 1;\n }\n if (p < q) {\n p = q;\n return 1;\n }\n else {\n return 0;\n }\n}\n\ntemplate<class T>\nbool chmin(T& p, T q, bool C = 1) {\n if (C == 0 && p == q) {\n return 1;\n }\n if (p > q) {\n p = q;\n return 1;\n }\n else {\n return 0;\n }\n}\n\nll gcd(ll(a), ll(b)) {\n if (b == 0)return a;\n ll c = a;\n while (a % b != 0) {\n c = a % b;\n a = b;\n b = c;\n }\n return b;\n}\n\n\nint main() {\n cin.tie(nullptr);\n ios::sync_with_stdio(false);\n\n while (1) {\n ll N,L;\n cin>>N>>L;\n if(N==0)return 0;\n vll D(N,-1),P(N);\n vll AN(N,-1);\n rep(i,N){\n char C;\n cin>>C>>P[i];\n if(C=='R')D[i]=1;\n }\n ll an=0;\n ll A=-1;\n rep(j,10000){\n map<ll,ll> M;\n rep(i,N){\n P[i]+=D[i];\n //cout<<i<<\" \"<<P[i]<<endl;\n M[P[i]]++;\n }\n rep(i,N){\n if(M[P[i]]>=2){\n D[i]*=-1;\n }\n \n if(P[i]==L){\n P[i]=1e17;\n if(an==j)continue;\n //cout<<\"#\"<<i<<\" \"<<j<<endl;\n an=j;\n A=i+1;\n }\n if(P[i]==0){\n P[i]=1e17;\n //cout<<i<<\" \"<<j<<endl;\n an=j;\n A=i+1;\n }\n }\n }\n cout<<an+1<<\" \"<<A<<endl;\n\n\n }\n\n}", "accuracy": 1, "time_ms": 210, "memory_kb": 3148, "score_of_the_acc": -1.9251, "final_rank": 7 }, { "submission_id": "aoj_1336_2089488", "code_snippet": "#include <iostream>\n#include <fstream>\n#include <cstdio>\n#include <cmath>\n#include <vector>\n#include <cstring>\n#include <string>\n#include <set>\n#include <map>\n#include <stack>\n#include <queue>\n#include <algorithm>\nusing namespace std;\n \n#define REP(i,n) for(int i=0; i<n; ++i)\n#define FOR(i,a,b) for(int i=a; i<=b; ++i)\n#define FORR(i,a,b) for (int i=a; i>=b; --i)\n#define pi M_PI\n \ntypedef long long ll;\ntypedef vector<int> VI;\ntypedef vector<ll> VL;\ntypedef vector<VI> VVI;\ntypedef pair<int,int> P;\ntypedef pair<ll,ll> PL;\n\nint main() {\n int n, l;\n while (cin >> n >> l && n){\n VI p(n), v(n);\n REP(i,n){\n char c;\n cin >> c >> p[i];\n if (c == 'R') v[i] = 1;\n else v[i] = -1;\n }\n FOR(t,1,l){\n map<int, int> a;\n REP(i,n){\n p[i] += v[i];\n a[p[i]]++;\n }\n FOR(i,1,l-1){\n if (a[i] >= 2){\n REP(j,n){\n if (p[j] == i) v[j] *= -1;\n }\n }\n }\n bool f = 1;\n REP(i,n) if (p[i] > 0 && p[i] < l) f = 0;\n if (f){\n int ans;\n REP(i,n) if (p[i] == l) ans = i+1;\n REP(i,n) if (p[i] == 0) ans = i+1;\n cout << t << \" \" << ans << endl;\n break;\n }\n }\n\n }\n return 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3072, "score_of_the_acc": -0.8887, "final_rank": 5 }, { "submission_id": "aoj_1336_1389694", "code_snippet": "// Header {{{\n// includes {{{\n#include <algorithm>\n#include <cassert>\n#include <cctype>\n#include <climits>\n#include <cmath>\n#include <complex>\n#include <cstdio>\n#include <cstdlib>\n#include <cstring>\n#include <ctime>\n#include <iostream>\n#include <iterator>\n#include <limits>\n#include <list>\n#include <map>\n#include <numeric>\n#include <queue>\n#include <set>\n#include <sstream>\n#include <stack>\n#include <string>\n#include <sys/time.h>\n#include <unistd.h>\n#include <vector>\n// }}}\nusing namespace std;\n// consts {{{\nstatic const int INF = 1e9;\nstatic const double PI = acos(-1.0);\nstatic const double EPS = 1e-10;\n// }}}\n// typedefs {{{\ntypedef long long int LL;\ntypedef unsigned long long int ULL;\ntypedef vector<int> VI;\ntypedef vector<VI> VVI;\ntypedef vector<LL> VLL;\ntypedef vector<VLL> VVLL;\ntypedef vector<ULL> VULL;\ntypedef vector<VULL> VVULL;\ntypedef vector<double> VD;\ntypedef vector<VD> VVD;\ntypedef vector<bool> VB;\ntypedef vector<VB> VVB;\ntypedef vector<char> VC;\ntypedef vector<VC> VVC;\ntypedef vector<string> VS;\ntypedef vector<VS> VVS;\ntypedef pair<int, int> PII;\ntypedef complex<int> P;\n// }}}\n// macros & inline functions {{{\n// syntax sugars {{{\n#define FOR(i, b, e) for (typeof(e) i = (b); i < (e); ++i)\n#define FORI(i, b, e) for (typeof(e) i = (b); i <= (e); ++i)\n#define REP(i, n) FOR(i, 0, n)\n#define REPI(i, n) FORI(i, 0, n)\n#define OPOVER(_op, _type) inline bool operator _op (const _type &t) const\n// }}}\n// conversion {{{\ninline int toInt(string s) { int v; istringstream sin(s); sin>>v; return v; }\ntemplate<class T> inline string toString(T x) { ostringstream sout; sout<<x; return sout.str(); }\n// }}}\n// array and STL {{{\n#define ARRSIZE(a) ( sizeof(a) / sizeof(a[0]) )\n#define ZERO(a, v) ( assert(v == 0 || v == -1), memset(a, v, sizeof(a)) )\n#define F first\n#define S second\n#define MP(a, b) make_pair(a, b)\n#define SIZE(a) ((LL)a.size())\n#define PB(e) push_back(e)\n#define SORT(v) sort((v).begin(), (v).end())\n#define RSORT(v) sort((v).rbegin(), (v).rend())\n#define ALL(a) (a).begin(), (a).end()\n#define RALL(a) (a).rbegin(), (a).rend()\n#define EACH(c, it) for(typeof((c).begin()) it=(c).begin(); it!=(c).end(); ++it)\n#define REACH(c, it) for(typeof((c).rbegin()) it=(c).rbegin(); it!=(c).rend(); ++it)\n#define EXIST(s, e) ((s).find(e) != (s).end())\n// }}}\n// bit manipulation {{{\n// singed integers are not for bitwise operations, specifically arithmetic shifts ('>>', and maybe not good for '<<' too)\n#define IS_UNSIGNED(n) (!numeric_limits<typeof(n)>::is_signed)\n#define BIT(n) (assert(IS_UNSIGNED(n)), assert(n < 64), (1ULL << (n)))\n#define BITOF(n, m) (assert(IS_UNSIGNED(n)), assert(m < 64), ((ULL)(n) >> (m) & 1))\ninline int BITS_COUNT(ULL b) { int c = 0; while(b != 0) { c += (b & 1); b >>= 1; } return c; }\ninline int MSB(ULL b) { int c = 0; while(b != 0) { ++c; b >>= 1; } return c-1; }\ninline int MAKE_MASK(ULL upper, ULL lower) { assert(lower < 64 && upper < 64 && lower <= upper); return (BIT(upper) - 1) ^ (BIT(lower) - 1); }\n// }}}\n// for readable code {{{\n#define EVEN(n) (n % 2 == 0)\n#define ODD(n) (!EVEN(n))\n// }}}\n// debug {{{\n#define arrsz(a) ( sizeof(a) / sizeof(a[0]) )\n#define dprt(fmt, ...) if (opt_debug) { fprintf(stderr, fmt, ##__VA_ARGS__); }\n#define darr(a) if (opt_debug) { copy( (a), (a) + arrsz(a), ostream_iterator<int>(cerr, \" \") ); cerr << endl; }\n#define darr_range(a, f, t) if (opt_debug) { copy( (a) + (f), (a) + (t), ostream_iterator<int>(cerr, \" \") ); cerr << endl; }\n#define dvec(v) if (opt_debug) { copy( ALL(v), ostream_iterator<int>(cerr, \" \") ); cerr << endl; }\n#define darr2(a) if (opt_debug) { FOR(__i, 0, (arrsz(a))){ darr( (a)[__i] ); } }\n#define WAIT() if (opt_debug) { string _wait_; cerr << \"(hit return to continue)\" << endl; getline(cin, _wait_); }\n#define dump(x) if (opt_debug) { cerr << \" [L\" << __LINE__ << \"] \" << #x << \" = \" << (x) << endl; }\n#define dumpl(x) if (opt_debug) { cerr << \" [L\" << __LINE__ << \"] \" << #x << endl << (x) << endl; }\n#define dumpf() if (opt_debug) { cerr << __PRETTY_FUNCTION__ << endl; }\n#define where() if (opt_debug) { cerr << __FILE__ << \": \" << __PRETTY_FUNCTION__ << \" [L: \" << __LINE__ << \"]\" << endl; }\n#define show_bits(b, s) if(opt_debug) { REP(i, s) { cerr << BITOF(b, s-1-i); if(i%4 == 3) cerr << ' '; } cerr << endl; }\n\n// ostreams {{{\n// complex\ntemplate<typename T> ostream& operator<<(ostream& s, const complex<T>& d) {return s << \"(\" << d.real() << \", \" << d.imag() << \")\";}\n\n// pair\ntemplate<typename T1, typename T2> ostream& operator<<(ostream& s, const pair<T1, T2>& d) {return s << \"(\" << d.first << \", \" << d.second << \")\";}\n\n// vector\ntemplate<typename T> ostream& operator<<(ostream& s, const vector<T>& d) {\n\tint len = d.size();\n\tREP (i, len) {\n\t\ts << d[i]; if (i < len - 1) s << \"\\t\";\n\t}\n\treturn s;\n}\n\n// 2 dimentional vector\ntemplate<typename T> ostream& operator<<(ostream& s, const vector< vector<T> >& d) {\n\tint len = d.size();\n\tREP (i, len) {\n\t\ts << d[i] << endl;\n\t}\n\treturn s;\n}\n\n// map\ntemplate<typename T1, typename T2> ostream& operator<<(ostream& s, const map<T1, T2>& m) {\n\ts << \"{\" << endl;\n\tfor (typeof(m.begin()) itr = m.begin(); itr != m.end(); ++itr) {\n\t\ts << \"\\t\" << (*itr).first << \" : \" << (*itr).second << endl;\n\t}\n\ts << \"}\" << endl;\n\treturn s;\n}\n// }}}\n// }}}\n// }}}\n// time {{{\ninline double now(){ struct timeval tv; gettimeofday(&tv, NULL); return (static_cast<double>(tv.tv_sec) + static_cast<double>(tv.tv_usec) * 1e-6); }\n// }}}\n// string manipulation {{{\ninline VS split(string s, char delimiter) { VS v; string t; REP(i, s.length()) { if(s[i] == delimiter) v.PB(t), t = \"\"; else t += s[i]; } v.PB(t); return v; }\ninline string join(VS s, string j) { string t; REP(i, s.size()) { t += s[i] + j; } return t; }\n// }}}\n// geometry {{{\n#define Y real()\n#define X imag()\n// }}}\n// 2 dimentional array {{{\nP dydx4[4] = { P(-1, 0), P(0, 1), P(1, 0), P(0, -1) };\nP dydx8[8] = { P(-1, 0), P(0, 1), P(1, 0), P(0, -1), P(-1, 1), P(1, 1), P(1, -1), P(-1, -1) };\nint g_height, g_width;\nbool in_field(P p) {\n\treturn (0 <= p.Y && p.Y < g_height) && (0 <= p.X && p.X < g_width);\n}\n// }}}\n// input and output {{{\ninline void input(string filename) {\n\tfreopen(filename.c_str(), \"r\", stdin);\n}\ninline void output(string filename) {\n\tfreopen(filename.c_str(), \"w\", stdout);\n}\n// }}}\n// }}}\n\n// Header under development {{{\n\nint LCM(int a, int b) {\n\t// FIXME\n\treturn a * b;\n}\n\n// Fraction class {{{\n// ref: http://martin-thoma.com/fractions-in-cpp/\nclass Fraction {\n\tpublic:\n\t\tULL numerator;\n\t\tULL denominator;\n\t\tFraction(ULL _numerator, ULL _denominator) {\n\t\t\tassert(_denominator > 0);\n\t\t\tnumerator = _numerator;\n\t\t\tdenominator = _denominator;\n\t\t};\n\n\t\tFraction operator*(const ULL rhs) {\n\t\t\treturn Fraction(this->numerator * rhs, this->denominator);\n\t\t};\n\n\t\tFraction operator*(const Fraction& rhs) {\n\t\t\treturn Fraction(this->numerator * rhs.numerator, this->denominator * rhs.denominator);\n\t\t}\n\n\t\tFraction operator+(const Fraction& rhs) {\n\t\t\tULL lcm = LCM(this->denominator, rhs.denominator);\n\t\t\tULL numer_lhs = this->numerator * (this->denominator / lcm);\n\t\t\tULL numer_rhs = rhs.numerator * (rhs.numerator / lcm);\n\t\t\treturn Fraction(numer_lhs + numer_rhs, lcm);\n\t\t}\n\n\t\tFraction& operator+=(const Fraction& rhs) {\n\t\t\tFraction result = (*this) + rhs;\n\t\t\tthis->numerator = result.numerator;\n\t\t\tthis->denominator = result.denominator;\n\t\t\treturn *this;\n\t\t}\n};\n\nstd::ostream& operator<<(std::ostream &s, const Fraction &a) {\n\tif (a.denominator == 1) {\n\t\ts << a.numerator;\n\t} else {\n\t\ts << a.numerator << \"/\" << a.denominator;\n\t}\n\treturn s;\n}\n\n// }}}\n\n// }}}\n\nbool opt_debug = false;\n\nclass Ant {\n\tpublic:\n\t\tint id;\n\t\tchar direction;\n\t\tAnt(int _id, char _direction) : id(_id), direction(_direction) {\n\t\t}\n};\n\nint main(int argc, char** argv) {\n\tstd::ios_base::sync_with_stdio(false);\n\t// set options {{{\n\tint __c;\n\twhile ( (__c = getopt(argc, argv, \"d\")) != -1 ) {\n\t\tswitch (__c) {\n\t\t\tcase 'd':\n\t\t\t\topt_debug = true;\n\t\t\t\tbreak;\n\t\t\tdefault:\n\t\t\t\tabort();\n\t\t}\n\t}\n\t// }}}\n\n\t// input(\"./inputs/0.txt\");\n\t// output(\"./outputs/0.txt\");\n\n\tint n, l;\n\twhile(cin >> n >> l, n | l) {\n\t\tvector< vector<Ant> > ants(l + 1, vector<Ant>());\n\t\tint n_ants = n;\n\t\tREP (i, n) {\n\t\t\tchar d; int p; cin >> d >> p;\n\t\t\tAnt a(i+1, d);\n\t\t\tants[p].PB(a);\n\t\t}\n\n\t\tint sec = 0;\n\t\tAnt ans(0, ' ');\n\t\twhile (n_ants) {\n\t\t\tsec++;\n\t\t\tvector< vector<Ant> > next_ants(l + 1, vector<Ant>());\n\t\t\t// REP (i, l + 1) {\n\t\t\tfor (int i = l; i >= 0; --i) {\n\t\t\t\tif (i == l && ants[i].size()) {\n\t\t\t\t\tans = ants[i][0];\n\t\t\t\t\tn_ants--;\n\t\t\t\t\tcontinue;\n\t\t\t\t}\n\t\t\t\tif (i == 0 && ants[i].size()) {\n\t\t\t\t\tans = ants[i][0];\n\t\t\t\t\tn_ants--;\n\t\t\t\t\tcontinue;\n\t\t\t\t}\n\n\t\t\t\tEACH (ants[i], itr) {\n\t\t\t\t\tAnt& ant = *itr;\n\t\t\t\t\tif (ant.direction == 'L') {\n\t\t\t\t\t\tnext_ants[i-1].PB(ant);\n\t\t\t\t\t} else {\n\t\t\t\t\t\tnext_ants[i+1].PB(ant);\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\n\t\t\tREP (i, l + 1) {\n\t\t\t\tassert(next_ants[i].size() <= 2);\n\t\t\t\tif (next_ants[i].size() == 2) {\n\t\t\t\t\tEACH (next_ants[i], itr) {\n\t\t\t\t\t\t(*itr).direction = (*itr).direction == 'L' ? 'R' : 'L';\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\n\t\t\tants = next_ants;\n\t\t}\n\n\t\tcout << sec - 1 << \" \" << ans.id << endl;\n\t}\n\n\treturn 0;\n}\n\n// vim: foldmethod=marker", "accuracy": 1, "time_ms": 10, "memory_kb": 1236, "score_of_the_acc": -0.0077, "final_rank": 2 }, { "submission_id": "aoj_1336_1194579", "code_snippet": "#include <bits/stdc++.h>\n#include <regex>\nusing namespace std;\n#define FOR(i,k,n) for(int i = (k); i < (n); i++)\n#define REP(i,n) FOR(i,0,n)\n#define ALL(a) begin(a),end(a)\n#define MS(m,v) memset(m,v,sizeof(m))\n#define D10 fixed<<setprecision(10)\ntypedef vector<int> vi;\ntypedef vector<string> vs;\ntypedef pair<int, int> P;\ntypedef complex<double> Point;\ntypedef long long ll;\nconst int INF = 1145141919;\nconst int MOD = 100000007;\nconst double EPS = 1e-10;\nconst double PI = acos(-1.0);\nstruct edge\n{\n\tint from, to, cost;\n\tbool operator < (const edge& e) const { return cost < e.cost; }\n\tbool operator >(const edge& e) const { return cost > e.cost; }\n};\n///*************************************************************************************///\n///*************************************************************************************///\n///*************************************************************************************///\n\nint main()\n{\n\tint n, l;\n\twhile (cin >> n >> l,n)\n\t{\n\t\tvector<P> v;\n\t\tREP(i, n)\n\t\t{\n\t\t\tchar c; int p;\n\t\t\tcin >> c >> p;\n\t\t\tv.push_back(P(c == 'L' ? -1 : 1, p));\n\t\t}\n\t\tint res = n;\n\t\tint cnt = 0;\n\t\tP lastl,lastr;\n\t\twhile (res)\n\t\t{\n\t\t\tcnt++;\n\t\t\tmap<int, vector<int>> m;\n\t\t\tREP(i, v.size())\n\t\t\t{\n\t\t\t\tv[i].second += v[i].first;\n\t\t\t\tm[v[i].second].push_back(i);\n\t\t\t\tif (v[i].second == 0)\n\t\t\t\t{\n\t\t\t\t\tlastl = P(cnt, i);\n\t\t\t\t\tres--;\n\t\t\t\t}\n\t\t\t\tif (v[i].second == l)\n\t\t\t\t{\n\t\t\t\t\tlastr = P(cnt, i);\n\t\t\t\t\tres--;\n\t\t\t\t}\n\t\t\t}\n\t\t\tREP(i, l)\n\t\t\t{\n\t\t\t\tif (m[i].size() == 2)\n\t\t\t\t{\n\t\t\t\t\tswap(v[m[i][0]].first, v[m[i][1]].first);\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\tcout << cnt << \" \" << (lastl.first >= lastr.first ? lastl.second + 1 : lastr.second + 1) << endl;\n\t}\n\treturn 0;\n}", "accuracy": 1, "time_ms": 20, "memory_kb": 1220, "score_of_the_acc": -0.05, "final_rank": 4 }, { "submission_id": "aoj_1336_1114218", "code_snippet": "#include <bits/stdc++.h>\n\nusing namespace std;\n\n#define MAX 110\n\nbool check(int ant[MAX],int L){\n for(int i = 0 ; i < L ; i++){\n if(ant[i] != 0) return false;\n }\n return true;\n}\n\nint main(){\n int N,L,p;\n char d;\n while(cin >> N >> L, N){\n int ant[MAX]={0};\n vector<int> tunnel[MAX],id[MAX];\n L--;\n for(int i = 0 ; i < N ; i++){\n cin >> d >> p;\n ant[p-1]++;\n id[p-1].push_back(i+1);\n tunnel[p-1].push_back((d == 'L' ? 1 : 2));\n }\n int t = 0,last;\n while(!check(ant,L)){\n vector<int> next[MAX],nid[MAX];\n last = -1;\n for(int i = 0 ; i < L ; i++){\n next[i].clear();\n nid[i].clear();\n if(ant[i] && last == -1){\n last = id[i][0];\n }\n }\n for(int i = 0 ; i < L ; i++){\n while(tunnel[i].size()){\n if(tunnel[i][0] == 1){\n tunnel[i].erase(tunnel[i].begin());\n if(i-1 >= 0){\n next[i-1].push_back(1);\n nid[i-1].push_back(id[i][0]);\n id[i].erase(id[i].begin());\n }\n }else if(tunnel[i][0] == 2){\n tunnel[i].erase(tunnel[i].begin());\n if(i+1 < L){\n next[i+1].push_back(2);\n nid[i+1].push_back(id[i][0]);\n id[i].erase(id[i].begin());\n }\n }\n }\n }\n for(int i = 0 ; i < L ; i++){\n tunnel[i] = next[i];\n id[i] = nid[i];\n if(tunnel[i].size() == 2){\n swap(tunnel[i][0],tunnel[i][1]);\n }\n ant[i] = tunnel[i].size();\n }\n t++;\n }\n cout << t << \" \" << last << endl;\n }\n return 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 1236, "score_of_the_acc": -0.0077, "final_rank": 2 }, { "submission_id": "aoj_1336_1114217", "code_snippet": "#include <bits/stdc++.h>\n\nusing namespace std;\n\n#define MAX 110\ntypedef pair<char,int> P;\n\nbool check(int ant[MAX],int L){\n for(int i = 0 ; i < L ; i++){\n if(ant[i] != 0) return false;\n }\n return true;\n}\n\nint main(){\n int N,L,p;\n char d;\n while(cin >> N >> L, N){\n int ant[MAX]={0};\n vector<int> tunnel[MAX],id[MAX];\n L--;\n for(int i = 0 ; i < N ; i++){\n cin >> d >> p;\n ant[p-1]++;\n id[p-1].push_back(i+1);\n tunnel[p-1].push_back((d == 'L' ? 1 : 2));\n }\n int t = 0,last;\n while(!check(ant,L)){\n vector<int> next[MAX],nid[MAX];\n last = -1;\n for(int i = 0 ; i < L ; i++){\n next[i].clear();\n nid[i].clear();\n if(ant[i] && last == -1){\n last = id[i][0];\n }\n }\n for(int i = 0 ; i < L ; i++){\n while(tunnel[i].size()){\n if(tunnel[i][0] == 1){\n tunnel[i].erase(tunnel[i].begin());\n if(i-1 >= 0){\n next[i-1].push_back(1);\n nid[i-1].push_back(id[i][0]);\n id[i].erase(id[i].begin());\n }\n }else if(tunnel[i][0] == 2){\n tunnel[i].erase(tunnel[i].begin());\n if(i+1 < L){\n next[i+1].push_back(2);\n nid[i+1].push_back(id[i][0]);\n id[i].erase(id[i].begin());\n }\n }\n }\n }\n for(int i = 0 ; i < L ; i++){\n tunnel[i] = next[i];\n id[i] = nid[i];\n if(tunnel[i].size() == 2){\n swap(tunnel[i][0],tunnel[i][1]);\n }\n ant[i] = tunnel[i].size();\n }\n t++;\n }\n cout << t << \" \" << last << endl;\n }\n return 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 1232, "score_of_the_acc": -0.0058, "final_rank": 1 } ]
aoj_1331_cpp
Problem G: Let There Be Light Suppose that there are some light sources and many spherical balloons. All light sources have sizes small enough to be modeled as point light sources, and they emit light in all directions. The surfaces of the balloons absorb light and do not reflect light. Surprisingly in this world, balloons may overlap. You want the total illumination intensity at an objective point as high as possible. For this purpose, some of the balloons obstructing lights can be removed. Because of the removal costs, however, there is a certain limit on the number of balloons to be removed. Thus, you would like to remove an appropriate set of balloons so as to maximize the illumination intensity at the objective point. The following figure illustrates the configuration specified in the first dataset of the sample input given below. The figure shows the xy -plane, which is enough because, in this dataset, the z -coordinates of all the light sources, balloon centers, and the objective point are zero. In the figure, light sources are shown as stars and balloons as circles. The objective point is at the origin, and you may remove up to 4 balloons. In this case, the dashed circles in the figure correspond to the balloons to be removed. Figure G.1: First dataset of the sample input. Input The input is a sequence of datasets. Each dataset is formatted as follows. N M R S 1 x S 1 y S 1 z S 1 r ... S Nx S Ny S Nz S Nr T 1 x T 1 y T 1 z T 1 b ... T Mx T My T Mz T Mb E x E y E z The first line of a dataset contains three positive integers, N , M and R , separated by a single space. N means the number of balloons that does not exceed 2000. M means the number of light sources that does not exceed 15. R means the number of balloons that may be removed, which does not exceed N . Each of the N lines following the first line contains four integers separated by a single space. ( S ix , S iy , S iz ) means the center position of the i -th balloon and S ir means its radius. Each of the following M lines contains four integers separated by a single space. ( T jx , T jy , T jz ) means the position of the j -th light source and T jb means its brightness. The last line of a dataset contains three integers separated by a single space. ( E x , E y , E z ) means the position of the objective point. S ix , S iy , S iz , T jx , T jy , T jz , E x , E y and E z are greater than -500, and less than 500. S ir is greater than 0, and less than 500. T jb is greater than 0, and less than 80000. At the objective point, the intensity of the light from the j -th light source is in inverse proportion to the square of the distance, namely T jb / { ( T jx − E x ) 2 + ( T jy − E y ) 2 + ( T jz − E z ) 2 }, if there is no balloon interrupting the light. The total illumination intensity is the sum of the above. You may assume the following. The distance between the objective point and any light source is not less than 1. For every i and j , even if S ir changes by ε (|ε| < 0.01), whether ...(truncated)
[ { "submission_id": "aoj_1331_10958572", "code_snippet": "#include <cstdio>\n#include <set>\n#include <cmath>\nusing namespace std;\nstruct Point\n{\n double x;\n double y;\n double z;\n void read()\n {\n scanf(\"%lf %lf %lf\",&x,&y,&z);\n }\n Point(double x=0,double y=0,double z=0):x(x),y(y),z(z){}\n Point operator +(const Point &ano)\n {\n return Point(x+ano.x,y+ano.y,z+ano.z);\n }\n Point operator -(const Point &ano)\n {\n return Point(x-ano.x,y-ano.y,z-ano.z);\n }\n Point operator *(double p)\n {\n return Point(x*p,y*p,z*p);\n }\n Point operator /(double p)\n {\n return Point(x/p,y/p,z/p);\n }\n void print()\n {\n printf(\"%f %f %f\\n\",x,y,z);\n }\n}E;\nstruct S\n{\n double x;\n double y;\n double z;\n double r;\n void read()\n {\n scanf(\"%lf %lf %lf %lf\",&x,&y,&z,&r);\n }\n Point get()\n {\n return Point(x,y,z);\n }\n}s[2100];\ntypedef Point Vector;\nstruct T\n{\n double x;\n double y;\n double z;\n double b;\n void read()\n {\n scanf(\"%lf %lf %lf %lf\",&x,&y,&z,&b);\n }\n Point get()\n {\n return Point(x,y,z);\n }\n}t[20];\nVector Cross(Vector a,Vector b)\n{\n return Vector(a.y*b.z - a.z*b.y,a.z*b.x -a.x * b.z,a.x*b.y-a.y*b.x);\n}\nconst double eps=1e-3;\ndouble Dot(Vector a,Vector b)\n{\n return a.x*b.x + a.y*b.y +a.z *b.z;\n}\ndouble Length(Vector a)\n{\n return sqrt(Dot(a,a));\n}\ndouble distanceToLine(Point p,Point a,Point b)\n{\n Vector v1=b-a;\n Vector v2=p-a;\n return Length(Cross(v1,v2))/Length(v1);\n}\nint dcmp(double p)\n{\n if(fabs(p)<eps)\n return 0;\n return p<0?-1:1;\n}\nbool Intersect(Point a,Point b,S s)\n{\n int a1=dcmp(Length(a-s.get())-s.r);\n int a2=dcmp(Length(b-s.get())-s.r);\n if(a1 * a2<0)\n return true;\n if(a1<=0 && a2<=0)\n return false;\n double dis = distanceToLine(s.get(),a,b);\n if(dcmp(dis-s.r)>=0)\n return false;\n double d1=Length(b-s.get());\n double d2=Length(a-s.get());\n double x1=d1*d1-dis*dis;\n double x2=d2*d2-dis*dis;\n x1=sqrt(x1);\n x2=sqrt(x2);\n if(dcmp(x1+x2-Length(b-a))==0)\n return true;\n if(dcmp(fabs(x1-x2)-Length(b-a))==0)\n return false;\n 1/0;\n}\nset<int> p[3000];\nint main()\n{\n int n,m,r;\n while(~scanf(\"%d %d %d\",&n,&m,&r))\n {\n if(n+m+r==0)\n break;\n for(int i=0;i<n;i++)\n p[i].clear();\n for(int i=0;i<n;i++)\n s[i].read();\n for(int i=0;i<m;i++)\n t[i].read();\n E.read();\n for(int i=0;i<n;i++)\n for(int j=0;j<m;j++)\n {\n if(Intersect(t[j].get(),E,s[i]))\n p[i].insert(j);\n }\n double ans=0;\n for(int i=0;i<(1<<m);i++)\n {\n double sum=0;\n for(int j=0;j<m;j++)\n if(i&(1<<j))\n sum+=t[j].b/(Dot(t[j].get()-E,t[j].get()-E));\n if(sum<ans)\n continue;\n int t=0;\n for(int k=0;k<n;k++)\n for(int j=0;j<m;j++)\n {\n if(i&(1<<j) && p[k].count(j))\n {\n t++;\n break;\n }\n }\n if(t>r)\n continue;\n if(sum>ans)\n ans=sum;\n }\n printf(\"%lf\\n\",ans);\n }\n return 0;\n}", "accuracy": 1, "time_ms": 540, "memory_kb": 3612, "score_of_the_acc": -0.8814, "final_rank": 10 }, { "submission_id": "aoj_1331_10853177", "code_snippet": "#include<cstdio>\n#include<cmath>\n#include<vector>\nusing namespace std;\nconst double eps = 1e-9;\nstruct point\n{\n double x,y,z;\n //不吃參數的建構子跟吃參數的建構子QQ\n point():x(0),y(0),z(0){}\n point(double _x,double _y,double _z):x(_x),y(_y),z(_z){}\n\n point operator+(const point &p) const{ return point(x+p.x, y+p.y, z+p.z);}\n point operator-(const point &p) const{ return point(x-p.x, y-p.y, z-p.z);}\n point operator*(const double &d) const{ return point(x*d, y*d, z*d);}\n point operator/(const double &d) const{ return point(x/d, y/d, z/d);}\n};\nstruct sphere\n{\n point p;\n double r;\n sphere(point _p,double _r):p(_p),r(_r){}\n};\nstruct line\n{\n point a,b;\n line(point _a,point _b):a(_a),b(_b){}\n};\n\nint n,m,r;\npoint obj;//objective point ;\nvector <sphere> bal;\nvector <point> light;\ndouble bright[16]; //brightness\nvector <int> rmv[16]; //rmv these balloons to get the light\n\npoint start(0.0,0.0,0.0);\ndouble length2(point a,point b)\n{\n double ans=(a.x-b.x)*(a.x-b.x);\n ans+=(a.y-b.y)*(a.y-b.y);\n ans+=(a.z-b.z)*(a.z-b.z);\n return ans;\n}\n/*\ninline double norm(const point &a){ return a.x*a.x+a.y*a.y+a.z*a.z;}\ninline double abs(const point &a){ return sqrt(norm(a));}\ninline double dot(const point &a, const point &b){ return a.x*b.x+a.y*b.y+a.z*b.z;}\npoint proj(const line &l, const point &p){\n double t = dot(p-l.a, l.a-l.b) / norm(l.a-l.b);\n return l.a+(l.a-l.b)*t;\n}\nbool corss(const line &l, const sphere &s){\n point a = l.a-s.p, b = l.b-s.p, c = proj(l,s.p)-s.p;\n if(abs(a) < s.r-eps and abs(b) < s.r-eps and abs(c) < s.r-eps)\n\t{\n\t\treturn 0; // contain\n\t}\n if(abs(c) > s.r+eps) return 0; // far\n if(abs(a) < s.r-eps or abs(b) < s.r-eps) return 1; // intersect\n if(abs(a-c)+abs(b-c)<abs(a-b)+eps) return 1; // intersect\n return 0;\n}\n*/\n//12312312313\ndouble abs(point a)\n{\n\treturn sqrt(length2(a,start));\n}\n\nbool corss(line ln,sphere sph)\n{\n\tpoint a=ln.a;\n\tpoint b=ln.b;\n\tbool flag1=0,flag2=0;\n\tif(sqrt(length2(a,sph.p))<sph.r-eps) flag1=1;\n\tif(sqrt(length2(b,sph.p))<sph.r-eps) flag2=1;\n\tif(flag1&&flag2)\n\t{\n\t\treturn 0;\n\t\t//printf(\"A\");\n\t}\n\telse if(flag1||flag2) return 1;\n\t//now to consider two point are outside of the sphere\n\t// ( ap X ab ) / length(ab) = lenth(line,sphere)\n\tpoint ap(sph.p.x-a.x,sph.p.y-a.y,sph.p.z-a.z);\n\tpoint ab(b.x-a.x,b.y-a.y,b.z-a.z);\n\tpoint ba(a.x-b.x,a.y-b.y,a.z-b.z);\n\tpoint tmp(ap.y*ab.z-ap.z*ab.y,ap.z*ab.x-ap.x*ab.z,ap.x*ab.y-ap.y*ab.x);\n\t\n\tdouble len1=sqrt(tmp.x*tmp.x+tmp.y*tmp.y+tmp.z*tmp.z);\n\tdouble len2=sqrt(length2(a,b));\n\tif((len1/len2)>(sph.r-eps)) return 0;\n\telse\n\t{\n\t\tpoint bp(sph.p.x-b.x,sph.p.y-b.y,sph.p.z-b.z);\n\t\t// ap. bp <0 return 1;\n\t\t//there're still some details to consider\n\t\t//printf(\"%lf\\n\",(ap.x*bp.x)+(ap.y*bp.y)+(ap.z*bp.z));\n\t\tif((ap.x*bp.x)+(ap.y*bp.y)+(ap.z*bp.z)<0.0) return 1;\n\t\tif((ab.x*ap.x)+(ab.y*ap.y)+(ab.z*ap.z)<0.0) return 0;\n\t\tif((ba.x*bp.x)+(ba.y*bp.y)+(ba.z*bp.z)<0.0) return 0;\n\t\treturn 1; \n\t}\n}\n//1231321313223\nvoid solve()\n{\n int i,j,k;\n for(i=0;i<m;i++) rmv[i].clear();\n for(i=0;i<m;i++)\n {\n for(j=0;j<n;j++)\n {\n if(corss(line(light[i],obj),bal[j]))\n\t\t\t{\n\t\t\t\trmv[i].push_back(j);\n\t\t\t}\n }\n }\n double ans=0.0;\n for(k=0;k<(1<<m);k++)\n {\n bool rmvv[2048]={}; //balloons<2000\n int cnt=0;\n double sum=0.0;\n for(i=0;i<m;i++)\n {\n if((k>>i)&1)\n {\n for(j=0;j<rmv[i].size();j++)\n {\n if(!rmvv[rmv[i][j]]) //rmb[i][j] is the index of the balloon\n {//if this balloon is not removed yet\n rmvv[rmv[i][j]]=1;\n cnt++;\n }\n }\n sum+=bright[i]/length2(light[i],obj);\n }\n }\n\t\t\n if(cnt<=r) ans=max(ans,sum);\n }\n printf(\"%lf\\n\",ans);\n}\n\nbool input()\n{\n scanf(\"%d %d %d\",&n,&m,&r);\n if(n+m+r==0) return 0;\n\n int x,y,z,r;\n bal.clear();\n light.clear();\n for(int i=0;i<n;i++)\n {//eat the ballons\n scanf(\"%d %d %d %d\",&x,&y,&z,&r);\n bal.push_back(sphere(point(x,y,z),r));\n }\n for(int i=0;i<m;i++)\n {//eat the light sources\n scanf(\"%d %d %d %d\",&x,&y,&z,&r);\n light.push_back(point(x,y,z));\n bright[i]=r;\n }\n //eat the objective point\n scanf(\"%d %d %d\",&x,&y,&z);\n obj=point(x,y,z);\n\n return 1;\n}\n\nint main()\n{\n while(input()) solve();\n return 0;\n}", "accuracy": 1, "time_ms": 1470, "memory_kb": 3080, "score_of_the_acc": -0.1711, "final_rank": 1 }, { "submission_id": "aoj_1331_10684649", "code_snippet": "#include<bits/stdc++.h>\n#define pb push_back\n#define db double\nusing namespace std;\ndb sqr(db x){return x*x;}\nstruct B{\n\tdb x, y, z, r;\n};\nstruct L{\n\tdb x, y, z, b;\n\tdb val;\n};\nstruct P{\n\tdb x, y, z;\n\tP(db x, db y, db z):x(x), y(y), z(z){}\n\tP(B &bl):x(bl.x), y(bl.y), z(bl.z){}\n\tP(L &ls):x(ls.x), y(ls.y), z(ls.z){}\n\tP(){}\n\tdb dis(P &p){return sqr(p.x-x)+sqr(p.y-y)+sqr(p.z-z);}\n};\nstruct V{\n\tdb x, y, z;\n\tV(){}\n\tV(P &p1, P &p2):x(p2.x-p1.x), y(p2.y-p1.y), z(p2.z-p1.z){}\n\tV(V &a, V &b):x(a.y*b.z-a.z*b.y), y(a.x*b.z-a.z*b.x), z(a.x*b.y-a.y*b.x){}\n\tdb dot(V &v){return x*v.x+y*v.y+z*v.z;}\n\tdb mod(){return x*x+y*y+z*z;}\n};\ndb dis(P &p, P &p1, P &p2){\n\tV v1(p1, p), v2(p1, p2), v3(p2, p), v4(p2, p1), v5(v2, v1);\n\tif(v1.dot(v2)<0) return p.dis(p1);\n\tif(v3.dot(v4)<0) return p.dis(p2);\n\treturn (db)v5.mod()/(db)v2.mod();\n}\nconst int MAX_N=2e3+50, MAX_M=1000;\nbool vis[MAX_N];\nbool block[MAX_M][MAX_N];\nB bl[MAX_N];\nL ls[MAX_M];\nint N, M, R;\nP obj;\nbool is_block(int i, int j){\n\tP a(ls[i]), b(bl[j]);\n\tif(b.dis(a)<sqr(bl[j].r)&&b.dis(obj)<sqr(bl[j].r)) return false;\n\treturn dis(b, a, obj)<(db)sqr(bl[j].r);\n}\nbool ok(int x){\n\tint cnt=0;\n\tmemset(vis, 0, sizeof(vis));\n\tfor(int i=0; i<M; i++){\n\t\tif(!(x&1<<i)) continue;\n\t\tfor(int j=0; j<N; j++)\n\t\t\tif(block[i][j]&&!vis[j]) vis[j]=true, cnt++;\n\t\tif(cnt>R) return false;\n\t}\n\treturn true;\n}\ndb calc(int x){\n\tdb ans=0;\n\tfor(int i=0; i<M; i++){\n\t\tif(!(x&1<<i)) continue;\n\t\tans+=ls[i].val;\n\t}\n\treturn ans;\n}\nvoid solve(){\n\tdouble ans=0;\n\tfor(int i=0; i<(1<<M); i++){\n\t\tif(ok(i)) ans=max(ans, calc(i));\n\t}\n\tprintf(\"%.9f\\n\", ans);\n}\nint main(){\n\t//freopen(\"in\", \"r\", stdin);\n\twhile(~scanf(\"%d%d%d\", &N, &M, &R)){\n\t\tif(N||M||R){\n\t\t\tfor(int i=0; i<N; i++)\n\t\t\t\tscanf(\"%lf%lf%lf%lf\", &bl[i].x, &bl[i].y, &bl[i].z, &bl[i].r);\n\t\t\tfor(int i=0; i<M; i++){\n\t\t\t\tscanf(\"%lf%lf%lf%lf\", &ls[i].x, &ls[i].y, &ls[i].z, &ls[i].b);\n\t\t\t}\n\t\t\tscanf(\"%lf%lf%lf\", &obj.x, &obj.y, &obj.z);\n\t\t\tfor(int i=0; i<N; i++){\n\t\t\t\tP p(ls[i]);\n\t\t\t\tls[i].val=(db)ls[i].b/(db)obj.dis(p);\n\t\t\t}\n\t\t\tfor(int i=0; i<M; i++)\n\t\t\t\tfor(int j=0; j<N; j++)\n\t\t\t\t\tblock[i][j]=is_block(i, j);\n\t\t\tsolve();\n\t\t}\n\t}\n\treturn 0;\n}", "accuracy": 1, "time_ms": 6490, "memory_kb": 3664, "score_of_the_acc": -1.8052, "final_rank": 19 }, { "submission_id": "aoj_1331_10684636", "code_snippet": "#include <algorithm>\n#include <iostream>\n#include <cstring>\n#include <cstdio>\n#include <vector>\n#include <cmath>\n#include <set>\n#define eps 1.0e-9\nusing namespace std;\nint n,m;\ndouble r[2333],w[20];\nbool vis[555],g[20][555];\nstruct point\n{\n double x,y,z;\n}p[2333],q[20],e;\ndouble sgn(double f)\n{\n if(fabs(f)<eps) return 0;\n else return f<0?-1:1;\n}\ndouble dis(point a,point b) {return (a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y)+(a.z-b.z)*(a.z-b.z);}\nint check(point p,point c,double r)\n{\n int d=dis(p,c);\n if(d<r*r) return -1;\n else if(d==r*r) return 0;\n else return 1;\n}\nbool cal(double a,double b,double c)\n{\n double delt=b*b-4*a*c;\n if(delt<=0) return false;\n else\n {\n delt=sqrt(delt);\n double x1=(-b+delt)/(2*a),x2=(-b-delt)/(2*a);\n if((sgn(x1)>0&&sgn(x1-1)<0)||(sgn(x2)>0&&sgn(x2-1)<0)) return true;\n else return false;\n }\n}\nvoid init()\n{\n memset(g,0,sizeof(g));\n for(int i=0;i<m;i++)\n {\n if(e.x==q[i].x&&e.y==q[i].y&&e.z==q[i].z) continue;\n for(int j=0;j<n;j++)\n {\n int a=check(e,p[j],r[j]),b=check(q[i],p[j],r[j]);\n //if(a==0||b==0) continue;\n double ka,kb,kc;\n ka=dis(e,q[i]);\n kb=2*((e.x-p[j].x)*(q[i].x-e.x)+(e.y-p[j].y)*(q[i].y-e.y)+(e.z-p[j].z)*(q[i].z-e.z));\n kc=dis(e,p[j])-r[j]*r[j];\n if(cal(ka,kb,kc)) g[i][j]=1;\n }\n }\n}\nint main()\n{\n int N;\n while(scanf(\"%d%d%d\",&n,&m,&N)==3)\n {\n if(n==0&&m==0&&N==0) break;\n for(int i=0;i<n;i++) scanf(\"%lf%lf%lf%lf\",&p[i].x,&p[i].y,&p[i].z,&r[i]);\n for(int i=0;i<m;i++) scanf(\"%lf%lf%lf%lf\",&q[i].x,&q[i].y,&q[i].z,&w[i]);\n scanf(\"%lf%lf%lf\",&e.x,&e.y,&e.z);\n init();\n int M=1<<m;\n double ans=0,tb[20];\n for(int i=0;i<m;i++) tb[i]=w[i]/dis(e,q[i]);\n for(int i=1;i<M;i++)\n {\n memset(vis,0,sizeof(vis));\n int cnt=0;\n bool ok=true;\n double tp=0;\n for(int j=0;j<m;j++)\n {\n if((i&(1<<j))==0) continue;\n for(int k=0;k<n;k++)\n {\n if(vis[k]) continue;\n if(g[j][k])\n {\n vis[k]=true;\n cnt++;\n if(cnt>N)\n {\n ok=false;\n break;\n }\n }\n }\n if(!ok) break;\n tp+=tb[j];\n }\n if(ok) ans=max(ans,tp);\n }\n printf(\"%.10f\\n\",ans);\n }\n return 0;\n}", "accuracy": 1, "time_ms": 5360, "memory_kb": 3404, "score_of_the_acc": -1.234, "final_rank": 18 }, { "submission_id": "aoj_1331_10036546", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\ntypedef long long ll;\ntypedef long double ld;\ntemplate <typename T>\nstruct vec {\n\tT x, y, z;\n\tvec(const T &x, const T &y, const T &z) : x(x), y(y), z(z) {}\n\tvec() : x(0), y(0), z(0) {}\n\tvec operator-(const vec &rhs) const {\n\t\treturn {x - rhs.x, y - rhs.y, z - rhs.z};\n\t}\n\tvec<ld> operator/(const ld &rhs) const {\n\t\treturn {x / rhs, y / rhs, z / rhs};\n\t}\n\tvec operator*(const ld &rhs) const {\n\t\treturn {x * rhs, y * rhs, z * rhs};\n\t}\n\tvec<ld> unit() const { return (*this) / (*this).norm(); }\n\tT norm2() const { return x * x + y * y + z * z; }\n\tld norm() const { return sqrtl((*this).norm2()); }\n\tT dot(const vec &rhs) const { return x * rhs.x + y * rhs.y + z * rhs.z; }\n\t// vec cross(vec rhs) const { return {y * rhs.z - z * rhs.y, z * rhs.x - x * rhs.z, x * rhs.y - y * rhs.x}; }\n};\nstruct sph {\n\tvec<int> p;\n\tint r;\n};\nbool isBlock(const vec<int> &from, const vec<int> &to, const sph &s) {\n\tauto isIn = [&](const vec<int> &p) {\n\t\treturn (s.p - p).norm2() <= s.r * s.r;\n\t};\n\tif (isIn(from) && isIn(to)) return false;\n\tif (isIn(from) ^ isIn(to)) return true;\n\tvec<int> dif = to - from;\n\tvec<int> v = s.p - from;\n\tif (dif.dot(v) < 0 || (from - to).dot(s.p - to) < 0) return false;\n\tauto convd = [&](const vec<int> &v) {\n\t\treturn vec<ld>(v.x, v.y, v.z);\n\t};\n\tvec<ld> uni = dif.unit();\n\tvec<ld> shaw = uni * (uni.dot(convd(v)));\n\tif ((convd(v) - shaw).norm2() <= s.r * s.r) {\n\t\treturn true;\n\t} else {\n\t\treturn false;\n\t}\n}\nint main() {\n\twhile (1) {\n\t\tint n, m, r;\n\t\tscanf(\"%d%d%d\", &n, &m, &r);\n\t\tif (n == 0 && m == 0 && r == 0) break;\n\t\t// printf(\"%d %d %d\\n\", n, m, r);\n\t\tvector<sph> s(n);\n\t\tfor (int i = 0; i < n; i++) {\n\t\t\tscanf(\"%d %d %d %d\", &s[i].p.x, &s[i].p.y, &s[i].p.z, &s[i].r);\n\t\t}\n\t\tvector<vec<int>> lightp(m);\n\t\tvector<int> bright(m);\n\t\tfor (int i = 0; i < m; i++) {\n\t\t\tscanf(\"%d %d %d %d\", &lightp[i].x, &lightp[i].y, &lightp[i].z, &bright[i]);\n\t\t}\n\t\tvec<int> pto;\n\t\tscanf(\"%d %d %d\", &pto.x, &pto.y, &pto.z);\n\t\t// cerr << \"ok\" << endl;\n\t\tld ans = 0;\n\t\tconst int M = 2010;\n\t\tvector<bitset<M>> isBlkb(m);\n\t\tfor (int i = 0; i < m; i++) {\n\t\t\tfor (int j = 0; j < n; j++) {\n\t\t\t\tif (isBlock(lightp[i], pto, s[j])) {\n\t\t\t\t\tisBlkb[i].set(j, 1);\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\tfor (int bit = 0; bit < (1 << m); bit++) {\n\t\t\tbitset<M> dels;\n\t\t\tint cnt = 0;\n\t\t\tld tot = 0;\n\t\t\tfor (int i = 0; i < m; i++) {\n\t\t\t\tif ((bit >> i) & 1) {\n\t\t\t\t\ttot += (ld)bright[i] / (lightp[i] - pto).norm2();\n\t\t\t\t\tdels |= isBlkb[i];\n\t\t\t\t}\n\t\t\t}\n\t\t\tif (dels.count() > r) continue;\n\t\t\t// cerr << \"bit: \" << bit << endl;\n\t\t\t// cerr << \"tot: \" << tot << endl;\n\t\t\tans = max(ans, tot);\n\t\t}\n\t\tprintf(\"%.10Lf\\n\", ans);\n\t}\n\treturn 0;\n}", "accuracy": 1, "time_ms": 270, "memory_kb": 3292, "score_of_the_acc": -0.3369, "final_rank": 3 }, { "submission_id": "aoj_1331_9947504", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\n\nbool solve(){\n\tint N, M, R;\n\tcin >> N >> M >> R;\n\n\tif(N == 0 && M == 0 && R == 0) return false;\n\n\tvector<tuple<ll, ll, ll, ll>> S(N), T(M);\n\tfor(auto&[x, y, z, r] : S) cin >> x >> y >> z >> r;\n\tfor(auto&[x, y, z, r] : T) cin >> x >> y >> z >> r;\n\tll X, Y, Z; cin >> X >> Y >> Z;\n\n\tfor(auto&[x, y, z, r] : S) x -= X, y -= Y, z -= Z;\n\tfor(auto&[x, y, z, r] : T) x -= X, y -= Y, z -= Z;\n\n\tauto judge = [&](int bl, int li){\n\t\tauto[xb, yb, zb, rb] = S[bl];\n\t\tauto[xl, yl, zl, rl] = T[li];\n\n\t\tbool both_outside = false;\n\t\t//両方内側\n\t\t{\n\t\t\tll dist1 = (xb-xl)*(xb-xl) + (yb-yl)*(yb-yl) + (zb-zl)*(zb-zl);\n\t\t\tll dist2 = xb*xb + yb*yb + zb*zb;\n\t\t\tif(dist1 <= rb*rb && dist2 <= rb*rb) return true;\n\t\t\tif(dist1 >= rb*rb && dist2 >= rb*rb) both_outside = true;\n\t\t}\n\n\t\t//内外が違う\n\t\tif(!both_outside) return false;\n\n\t\t//B:ライト, A:風船\n\t\tll A = xb*xb + yb*yb + zb*zb;\n\t\tll B = xl*xl + yl*yl + zl*zl;\n\t\tll dot = xb*xl + yb*yl + zb*zl;\n\t\tll r = rb*rb;\n\t\t//風船がライトとの直線に干渉しない\n\t\tif(r*B <= A*B-dot*dot) return true;\n\n\t\t//ライトと風船が反対側\n\t\tif(dot<0) return true;\n\n\t\treturn B<=A;\n\t};\n\n\tvector<vector<int>> rem(M);\n\tfor(int i = 0; i < M; i++){\n\t\tfor(int j = 0; j < N; j++){\n\t\t\tif(!judge(j, i)) rem[i].push_back(j);\n\t\t}\n\t}\n\n\t/*for(int i = 0; i < M; i++){\n\t\tcout << \"rem\" << i+1 << ' ';\n\t\tfor(int j : rem[i]) cout << j+1 << ' ';\n\t\tcout << endl;\n\t}*/\n\n\tdouble ans = 0;\n\tfor(int bt = 0; bt < 1<<M; bt++){\n\t\tvector<bool> removed(N, false);\n\t\tdouble res = 0;\n\t\tfor(int i = 0; i < M; i++){\n\t\t\tif(bt >> i & 1){\n\t\t\t\tfor(int j : rem[i]) removed[j] = true;\n\t\t\t\tauto[xl, yl, zl, rl] = T[i];\n\t\t\t\tdouble val = 1.0*rl / (xl*xl + yl*yl + zl*zl);\n\t\t\t\tres += val;\n\t\t\t}\n\t\t}\n\t\tint cnt = 0;\n\t\tfor(int i = 0; i < N; i++) if(removed[i]) cnt++;\n\t\t//cout << \"cnt:\" << cnt << endl;\n\t\tif(cnt <= R) ans = max(ans, res);\n\t}\n\n\tprintf(\"%.10lf\\n\", ans);\n\treturn true;\n}\n\nint main(){\n\twhile(solve());\n}", "accuracy": 1, "time_ms": 1450, "memory_kb": 3536, "score_of_the_acc": -0.8898, "final_rank": 11 }, { "submission_id": "aoj_1331_9711061", "code_snippet": "#include <bits/stdc++.h>\n\nusing namespace std;\n\n#define rep(i, a, b) for (int i = (int)(a); i < (int)(b); i++)\n#define rrep(i, a, b) for (int i = (int)(b)-1; i >= (int)(a); i--)\n#define ALL(v) (v).begin(), (v).end()\n#define UNIQUE(v) sort(ALL(v)), (v).erase(unique(ALL(v)), (v).end())\n#define SZ(v) (int)v.size()\n#define MIN(v) *min_element(ALL(v))\n#define MAX(v) *max_element(ALL(v))\n#define LB(v, x) int(lower_bound(ALL(v), (x)) - (v).begin())\n#define UB(v, x) int(upper_bound(ALL(v), (x)) - (v).begin())\n\nusing uint = unsigned int;\nusing ll = long long int;\nusing ull = unsigned long long;\nusing i128 = __int128_t;\nusing u128 = __uint128_t;\nconst int inf = 0x3fffffff;\nconst ll INF = 0x1fffffffffffffff;\n\ntemplate <typename T> inline bool chmax(T &a, T b) {\n if (a < b) {\n a = b;\n return 1;\n }\n return 0;\n}\ntemplate <typename T> inline bool chmin(T &a, T b) {\n if (a > b) {\n a = b;\n return 1;\n }\n return 0;\n}\ntemplate <typename T, typename U> T ceil(T x, U y) {\n assert(y != 0);\n if (y < 0)\n x = -x, y = -y;\n return (x > 0 ? (x + y - 1) / y : x / y);\n}\ntemplate <typename T, typename U> T floor(T x, U y) {\n assert(y != 0);\n if (y < 0)\n x = -x, y = -y;\n return (x > 0 ? x / y : (x - y + 1) / y);\n}\ntemplate <typename T> int popcnt(T x) {\n return __builtin_popcountll(x);\n}\ntemplate <typename T> int topbit(T x) {\n return (x == 0 ? -1 : 63 - __builtin_clzll(x));\n}\ntemplate <typename T> int lowbit(T x) {\n return (x == 0 ? -1 : __builtin_ctzll(x));\n}\n\ntemplate <class T, class U>\nostream &operator<<(ostream &os, const pair<T, U> &p) {\n os << \"P(\" << p.first << \", \" << p.second << \")\";\n return os;\n}\ntemplate <typename T> ostream &operator<<(ostream &os, const vector<T> &vec) {\n os << \"{\";\n for (int i = 0; i < vec.size(); i++) {\n os << vec[i] << (i + 1 == vec.size() ? \"\" : \", \");\n }\n os << \"}\";\n return os;\n}\ntemplate <typename T, typename U>\nostream &operator<<(ostream &os, const map<T, U> &map_var) {\n os << \"{\";\n for (auto itr = map_var.begin(); itr != map_var.end(); itr++) {\n os << \"(\" << itr->first << \", \" << itr->second << \")\";\n itr++;\n if (itr != map_var.end())\n os << \", \";\n itr--;\n }\n os << \"}\";\n return os;\n}\ntemplate <typename T> ostream &operator<<(ostream &os, const set<T> &set_var) {\n os << \"{\";\n for (auto itr = set_var.begin(); itr != set_var.end(); itr++) {\n os << *itr;\n ++itr;\n if (itr != set_var.end())\n os << \", \";\n itr--;\n }\n os << \"}\";\n return os;\n}\n#ifdef LOCAL\n#define show(...) _show(0, #__VA_ARGS__, __VA_ARGS__)\n#else\n#define show(...) true\n#endif\ntemplate <typename T> void _show(int i, T name) {\n cerr << '\\n';\n}\ntemplate <typename T1, typename T2, typename... T3>\nvoid _show(int i, const T1 &a, const T2 &b, const T3 &...c) {\n for (; a[i] != ',' && a[i] != '\\0'; i++)\n cerr << a[i];\n cerr << \":\" << b << \" \";\n _show(i + 1, a, c...);\n}\n\n/**\n * @brief template\n */\n\nstruct Balloon {\n ll X, Y, Z, R;\n Balloon(ll x, ll y, ll z, ll r) : X(x), Y(y), Z(z), R(r) {};\n};\n\nstruct Light {\n ll X, Y, Z, B;\n Light(ll x, ll y, ll z, ll b) : X(x), Y(y), Z(z), B(b) {};\n};\n\nbool isin(Balloon S, ll X, ll Y, ll Z) {\n return ((X-S.X)*(X-S.X)+(Y-S.Y)*(Y-S.Y)+(Z-S.Z)*(Z-S.Z) < S.R*S.R);\n}\n\nll Dot(ll X1, ll Y1, ll Z1, ll X2, ll Y2, ll Z2) {\n return X1*X2+Y1*Y2+Z1*Z2;\n}\n\nint isCross(Balloon S, Light T) {\n if (isin(S,T.X,T.Y,T.Z) && isin(S,0,0,0)) return false;\n if (isin(S,T.X,T.Y,T.Z) && !isin(S,0,0,0)) return true;\n if (!isin(S,T.X,T.Y,T.Z) && isin(S,0,0,0)) return true;\n if (Dot(S.X,S.Y,S.Z,T.X,T.Y,T.Z) <= 0) return false;\n if (Dot(S.X-T.X,S.Y-T.Y,S.Z-T.Z,-T.X,-T.Y,-T.Z) <= 0) return false;\n return (S.Y*T.Z-S.Z*T.Y)*(S.Y*T.Z-S.Z*T.Y)+\n (S.Z*T.X-S.X*T.Z)*(S.Z*T.X-S.X*T.Z)+\n (S.X*T.Y-S.Y*T.X)*(S.X*T.Y-S.Y*T.X) <= S.R * S.R * (T.X*T.X+T.Y*T.Y+T.Z*T.Z);\n}\n\nint main() {\n while(1) {\n int N, M, K;\n cin >> N >> M >> K;\n if (N == 0) return 0;\n vector<Balloon> S;\n vector<Light> T;\n rep(i,0,N) {\n ll x, y, z, r;\n cin >> x >> y >> z >> r;\n S.push_back(Balloon(x,y,z,r));\n }\n rep(i,0,M) {\n ll x, y, z, b;\n cin >> x >> y >> z >> b;\n T.push_back(Light(x,y,z,b));\n }\n ll EX, EY, EZ;\n cin >> EX >> EY >> EZ;\n rep(i,0,N) S[i].X -= EX, S[i].Y -= EY, S[i].Z -= EZ;\n rep(i,0,M) T[i].X -= EX, T[i].Y -= EY, T[i].Z -= EZ;\n vector<bitset<2000>> C(M);\n rep(i,0,N) {\n rep(j,0,M) {\n if(isCross(S[i],T[j])) C[j].set(i,1);\n }\n }\n double ANS = 0.0;\n rep(i,0,1<<M) {\n double SUM = 0.0;\n bitset<2000> B;\n rep(j,0,M) {\n if (i&(1<<j)) {\n SUM += (double)T[j].B / (double)(T[j].X*T[j].X+T[j].Y*T[j].Y+T[j].Z*T[j].Z);\n B |= C[j];\n }\n }\n if (B.count() <= K) chmax(ANS,SUM);\n }\n printf(\"%.12f\\n\",ANS);\n}\n}", "accuracy": 1, "time_ms": 260, "memory_kb": 3608, "score_of_the_acc": -0.8354, "final_rank": 8 }, { "submission_id": "aoj_1331_8998178", "code_snippet": "#line 2 \"cp-library/src/cp-template.hpp\"\n#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\nusing ld = long double;\nusing uint = unsigned int;\nusing ull = unsigned long long;\nusing i32 = int;\nusing u32 = unsigned int;\nusing i64 = long long;\nusing u64 = unsigned long long;\nusing i128 = __int128_t;\ntemplate < class T > bool chmin(T& a, T b) { if(a > b) { a = b; return true; } return false; }\ntemplate < class T > bool chmax(T& a, T b) { if(a < b) { a = b; return true; } return false; }\ntemplate < class T, class U > T ceil (T x, U y) { return (x > 0 ? (x + y - 1) / y : x / y); }\ntemplate < class T, class U > T floor(T x, U y) { return (x > 0 ? x / y : (x - y + 1) / y); }\nint popcnt(i32 x) { return __builtin_popcount(x); }\nint popcnt(u32 x) { return __builtin_popcount(x); }\nint popcnt(i64 x) { return __builtin_popcountll(x); }\nint popcnt(u64 x) { return __builtin_popcountll(x); }\n\n#line 2 \"cp-library/src/utility/rep_itr.hpp\"\ntemplate < class T > struct itr_rep {\n T i, d;\n constexpr itr_rep(const T i) noexcept : i(i), d(1) {}\n constexpr itr_rep(const T i, const T d) noexcept : i(i), d(d) {}\n void operator++() noexcept { i += d; }\n constexpr int operator*() const noexcept { return i; }\n constexpr bool operator!=(const itr_rep x) const noexcept { return d > 0 ? i < x.i : i > x.i; }\n};\n\ntemplate < class T > struct rep {\n const itr_rep< T > s, t;\n constexpr rep(const T t) noexcept : s(0), t(t) {}\n constexpr rep(const T s, const T t) noexcept : s(s), t(t) {}\n constexpr rep(const T s, const T t, const T d) noexcept : s(s, d), t(t, d) {}\n constexpr auto begin() const noexcept { return s; }\n constexpr auto end () const noexcept { return t; }\n};\n\ntemplate < class T > struct revrep {\n const itr_rep < T > s, t;\n constexpr revrep(const T t) noexcept : s(t - 1, -1), t(-1, -1) {}\n constexpr revrep(const T s, const T t) noexcept : s(t - 1, -1), t(s - 1, -1) {}\n constexpr revrep(const T s, const T t, const T d) noexcept : s(t - 1, -d), t(s - 1, -d) {}\n constexpr auto begin() const noexcept { return s; }\n constexpr auto end () const noexcept { return t; }\n};\n#line 3 \"cp-library/src/utility/io.hpp\"\n\n/* 128bit integer */\nistream& operator>>(istream& is, i128& x) {\n std::string s; is >> s;\n int pm = (s[0] == '-');\n x = 0;\n for(int i : rep(pm, int(s.size()))) x = x * 10 + (s[i] - '0');\n if(pm) x *= -1;\n return is;\n}\nostream& operator<<(ostream& os, const i128& x) {\n if(x == 0) return os << '0';\n i128 y = x;\n if(y < 0) { os << '-'; y *= -1; }\n std::vector<int> ny;\n while(y > 0) { ny.push_back(y % 10); y /= 10; }\n for(int i : revrep(ny.size())) os << ny[i];\n return os;\n}\n\ntemplate < class S, class T > istream& operator>>(istream& is, std::pair< S, T >& x) { is >> x.first >> x.second; return is; }\ntemplate < class S, class T > ostream& operator<<(ostream& os, const std::pair< S, T >& x) { os << x.first << \" \" << x.second; return os; }\n\nnamespace scanner {\n struct sca {\n template < class T > operator T() {\n T s; std::cin >> s; return s;\n }\n };\n struct vec {\n int n;\n vec(int n) : n(n) {}\n template < class T > operator std::vector< T >() {\n std::vector< T > v(n);\n for(T& x : v) std::cin >> x;\n return v;\n }\n };\n struct mat {\n int h, w;\n mat(int h, int w) : h(h), w(w) {}\n template < class T > operator std::vector< std::vector< T > >() {\n std::vector m(h, std::vector< T >(w));\n for(std::vector< T >& v : m) for(T& x : v) std::cin >> x;\n return m;\n }\n };\n struct speedup {\n speedup() {\n std::cin.tie(0);\n std::ios::sync_with_stdio(0);\n }\n } speedup_instance;\n}\nscanner::sca in() { return scanner::sca(); }\nscanner::vec in(int n) { return scanner::vec(n); }\nscanner::mat in(int h, int w) { return scanner::mat(h, w); }\n\nnamespace printer {\n void precision(int d) { std::cout << std::fixed << std::setprecision(d); }\n void flush() { std::cout.flush(); }\n}\n\ntemplate < class T >\nostream& operator<<(ostream& os, const std::vector< T > a) {\n int n = a.size();\n for(int i : rep(n)) { os << a[i]; if(i != n - 1) os << ' '; }\n return os;\n}\n\nint print() { std::cout << '\\n'; return 0; }\ntemplate < class head, class... tail > int print(head&& h, tail&&... t) {\n std::cout << h; if(sizeof...(tail)) std::cout << ' ';\n return print(std::forward<tail>(t)...);\n}\ntemplate < class T > int print_n(const std::vector< T > a) {\n int n = a.size();\n for(int i : rep(n)) std::cout << a[i] << \"\\n\";\n return 0;\n}\n\n\n#line 2 \"cp-library/src/utility/key_val.hpp\"\n\ntemplate < class K, class V >\nstruct key_val {\n K key; V val;\n key_val() {}\n key_val(K key, V val) : key(key), val(val) {}\n template < std::size_t Index >\n std::tuple_element_t< Index, key_val >& get() {\n if constexpr (Index == 0) return key;\n if constexpr (Index == 1) return val;\n }\n};\n\nnamespace std {\n\ntemplate < class K, class V > struct tuple_size < key_val< K, V > > : integral_constant< size_t, 2 > {};\ntemplate < class K, class V > struct tuple_element < 0, key_val< K, V > > { using type = K; };\ntemplate < class K, class V > struct tuple_element < 1, key_val< K, V > > { using type = V; };\n\n}\n#line 2 \"cp-library/src/utility/vec_op.hpp\"\ntemplate < class T > key_val< int, T > max_of(const vector< T >& a) {\n int i = std::max_element(a.begin(), a.end()) - a.begin();\n return {i, a[i]};\n}\ntemplate < class T > key_val< int, T > min_of(const vector< T >& a) {\n int i = std::min_element(a.begin(), a.end()) - a.begin();\n return {i, a[i]};\n}\ntemplate < class S, class T > S sum_of(const vector< T >& a) {\n S sum = 0;\n for(const T x : a) sum += x;\n return sum;\n}\ntemplate < class S, class T > vector< S > freq_of(const vector< T >& a, T L, T R) {\n vector< S > res(R - L, S(0));\n for(const T x : a) res[x - L] += 1;\n return res;\n}\ntemplate < class S, class T > struct prefix_sum {\n vector< S > s;\n prefix_sum(const vector< T >& a) : s(a) {\n s.insert(s.begin(), S(0));\n for(int i : rep(a.size())) s[i + 1] += s[i];\n }\n // [L, R)\n S sum(int L, int R) { return s[R] - s[L]; }\n};\n#line 3 \"cp-library/src/utility/heap.hpp\"\n\ntemplate < class T > using heap_min = std::priority_queue< T, std::vector< T >, std::greater< T > >;\ntemplate < class T > using heap_max = std::priority_queue< T, std::vector< T >, std::less< T > >;\n\n#line 27 \"cp-library/src/cp-template.hpp\"\n\n#line 1 \"cp-library/src/algorithm/bin_search.hpp\"\ntemplate < class T, class F >\nT bin_search(T ok, T ng, F f) {\n while(abs(ng - ok) > 1) {\n T mid = (ok + ng) / 2;\n (f(mid) ? ok : ng) = mid;\n }\n return ok;\n}\n\ntemplate < class T, class F >\nT bin_search_real(T ok, T ng, F f, int step = 80) {\n while(step--) {\n T mid = (ok + ng) / 2;\n (f(mid) ? ok : ng) = mid;\n }\n return ok;\n}\n#line 2 \"cp-library/src/algorithm/argsort.hpp\"\n\ntemplate < class T > std::vector< int > argsort(const std::vector< T > &a) {\n std::vector< int > ids((int)a.size());\n std::iota(ids.begin(), ids.end(), 0);\n std::sort(ids.begin(), ids.end(), [&](int i, int j) {\n return a[i] < a[j] || (a[i] == a[j] && i < j);\n });\n return ids;\n}\n#line 1 \"macro.hpp\"\nnamespace macro {\n\nusing size_type = int;\ntemplate < class container > void sort(container& a) { std::sort(std:: begin(a), std:: end(a)); }\ntemplate < class container > void rsort(container& a) { std::sort(std::rbegin(a), std::rend(a)); }\ntemplate < class container > void reverse(container& a) { std::reverse(std::begin(a), std::end(a)); }\ntemplate < class container > void unique(container& a) {\n std::sort(std::begin(a), std::end(a));\n a.erase(std::unique(std::begin(a), std::end(a)), std::end(a));\n}\ntemplate < class container > container sorted(const container& a) { container b = a; sort(b); return std::move(b); }\ntemplate < class container > container rsorted(const container& a) { container b = a; rsort(b); return std::move(b); }\ntemplate < class container, class compare > void sort(container& a, const compare& cmp) { std::sort(std::begin(a), std::end(a), cmp); }\ntemplate < class container, class compare > container sorted(const container& a, const compare& cmp) { container b = a; sort(b, cmp); return std::move(b); }\ntemplate < class container, class value > size_type lower_bound(const container& a, const value& x) { return std::lower_bound(std::begin(a), std::end(a), x) - std::begin(a); }\ntemplate < class container, class value > size_type upper_bound(const container& a, const value& x) { return std::upper_bound(std::begin(a), std::end(a), x) - std::begin(a); }\n\nconst std::vector<std::pair<size_type, size_type>> dir4 = { {+1, 0}, {-1, 0}, { 0, +1}, { 0, -1} };\nconst std::vector<std::pair<size_type, size_type>> dir8 = { {-1, -1}, {-1, 0}, {-1, +1}, { 0, -1}, { 0, +1}, {+1, -1}, {+1, 0}, {+1, +1} };\n\n#ifdef _DEBUG\n#define debug(x) std::cout << \"[\" << __LINE__ << \"] \" << #x << \": \" << x << std::endl\n#else\n#define debug(x)\n#endif\n\ntemplate < class container > void concat(container& a, const container& b) {\n a.insert(std::end(a), std::begin(b), std::end(b));\n}\nstd::vector<size_type> iota(const size_type n) {\n std::vector<size_type> I(n);\n std::iota(std::begin(I), std::end(I), 0);\n return I;\n}\ntemplate < class container > std::vector<size_type> sort_idx(const container& a) {\n const size_type n = a.size();\n std::vector<size_type> I = iota(n);\n std::sort(std::begin(I), std::end(I), [&](size_type i, size_type j) { return a[i] < a[j] or (a[i] == a[j] and i < j); });\n return I;\n}\ntemplate < class container, class compare > std::vector<size_type> sort_idx(const container& a, const compare& cmp) {\n const size_type n = a.size();\n std::vector<size_type> I = iota(n);\n std::sort(std::begin(I), std::end(I), [&](size_type i, size_type j) { return cmp(a[i], a[j]) or (a[i] == a[j] and i < j); });\n return std::move(I);\n}\n\nstruct grid {\n using size_type = int;\n size_type H, W;\n grid(const size_type H, const size_type W) : H(H), W(W) {}\n bool contains(const size_type i, const size_type j) {\n return 0 <= i and i < H and 0 <= j and j < W;\n }\n};\n\nusing f64 = long double;\n\n} // namespace macro\n\nusing namespace macro;\n#line 3 \"A.cpp\"\n\n\n#define EPS (1e-7)\n#define equals(a, b) (fabs((a) - (b)) < EPS)\n\nclass Point3d {\n public:\n double x, y, z;\n\n Point3d(double x = 0, double y = 0, double z = 0) : x(x), y(y), z(z) {}\n\n Point3d operator+(const Point3d& a) {\n return Point3d(x + a.x, y + a.y, z + a.z);\n }\n Point3d operator-(const Point3d& a) {\n return Point3d(x - a.x, y - a.y, z - a.z);\n }\n Point3d operator*(const double& d) {\n return Point3d(x * d, y * d, z * d);\n }\n Point3d operator/(const double& d) {\n return Point3d(x / d, y / d, z / d);\n }\n\n bool operator<(const Point3d& p) const {\n if (!equals(x, p.x)) return x < p.x;\n if (!equals(y, p.y)) return y < p.y;\n if (!equals(z, p.z)) return z < p.z;\n return false;\n }\n\n bool operator==(const Point3d& p) const {\n return equals(x, p.x) && equals(y, p.y) && equals(z, p.z);\n }\n};\n\nstruct Segment3d {\n Point3d p[2];\n Segment3d(Point3d p1 = Point3d(), Point3d p2 = Point3d()) {\n p[0] = p1, p[1] = p2;\n }\n bool operator==(const Segment3d& seg) const {\n return (p[0] == seg.p[0] && p[1] == seg.p[1]) || (p[0] == seg.p[1] && p[1] == seg.p[0]);\n }\n};\n\nusing Line3d = Segment3d;\nusing Vector3d = Point3d;\n\nostream& operator<<(ostream& os, const Point3d& p) {\n return os << \"(\" << p.x << \",\" << p.y << \",\" << p.z << \")\";\n}\n\nostream& operator<<(ostream& os, const Segment3d& seg) {\n return os << \"(\" << seg.p[0] << \",\" << seg.p[1] << \")\";\n}\n\ndouble dot(const Point3d& a, const Point3d& b) {\n return a.x * b.x + a.y * b.y + a.z * b.z;\n}\n\nVector3d cross(const Point3d& a, const Point3d& b) {\n return Vector3d(a.y * b.z - a.z * b.y, a.z * b.x - a.x * b.z, a.x * b.y - a.y * b.x);\n}\n\ninline double norm(const Point3d& p) {\n return p.x * p.x + p.y * p.y + p.z * p.z;\n}\n\ninline double abs(const Point3d& p) {\n return sqrt(norm(p));\n}\n\ninline double toRad(double theta) {\n return theta * M_PI / 180.0;\n}\n\ndouble distanceLP(Line3d line, Point3d p) {\n return abs(cross(line.p[1] - line.p[0], p - line.p[0])) / abs(line.p[1] - line.p[0]);\n}\n\nPoint3d project(Segment3d seg, Point3d p) {\n Vector3d base = seg.p[1] - seg.p[0];\n double t = dot(p - seg.p[0], base) / norm(base);\n return seg.p[0] + base * t;\n}\n\nPoint3d reflect(Segment3d seg, Point3d p) {\n return p + (project(seg, p) - p) * 2.0;\n}\n\nbool on_line3d(Line3d line, Point3d p) {\n return equals(abs(cross(line.p[1] - p, line.p[0] - p)), 0);\n}\n\nbool on_segment3d(Segment3d seg, Point3d p) {\n if (!on_line3d(seg, p)) return false;\n double dist[3] = {abs(seg.p[1] - seg.p[0]), abs(p - seg.p[0]), abs(p - seg.p[1])};\n return on_line3d(seg, p) && equals(dist[0], dist[1] + dist[2]);\n}\n\ndouble distanceSP(Segment3d seg, Point3d p) {\n Point3d r = project(seg, p);\n if (on_segment3d(seg, r)) return abs(p - r);\n return min(abs(seg.p[0] - p), abs(seg.p[1] - p));\n}\n\ndouble solve(int N, int M, int R) {\n vector<Point3d> Sp(N);\n vector<double> Sr(N);\n for(int i : rep(N)) Sp[i].x = in(), Sp[i].y = in(), Sp[i].z = in(), Sr[i] = in();\n vector<Point3d> Tp(M);\n vector<double> Tb(M);\n for(int i : rep(M)) Tp[i].x = in(), Tp[i].y = in(), Tp[i].z = in(), Tb[i] = in();\n Point3d E;\n E.x = in(), E.y = in(), E.z = in();\n\n const int MAX_N = 2000;\n vector<bitset<MAX_N>> erase(M);\n for(int i : rep(M)) {\n for(int j : rep(N)) {\n bool touch = [&] {\n if(abs(E - Sp[j]) <= Sr[j] and abs(Tp[i] - Sp[j]) <= Sr[j]) return false;\n return distanceSP(Segment3d(E, Tp[i]), Sp[j]) <= Sr[j];\n }();\n\n erase[i][j] = touch;\n }\n }\n\n double ans = 0;\n for(int set : rep(1 << M)) {\n bitset<MAX_N> need;\n double score = 0;\n for(int i : rep(M)) if(set & (1 << i)) {\n need |= erase[i];\n score += Tb[i] / norm(Tp[i] - E);\n }\n if(need.count() <= R) chmax(ans, score);\n }\n return ans;\n}\n\nint main() {\n while(true) {\n int N = in(), M = in(), R = in();\n if(make_tuple(N, M, R) == make_tuple(0, 0, 0)) return 0;\n printer::precision(20);\n print(solve(N, M, R));\n }\n}", "accuracy": 1, "time_ms": 270, "memory_kb": 3588, "score_of_the_acc": -0.8052, "final_rank": 6 }, { "submission_id": "aoj_1331_8492718", "code_snippet": "#include<bits/stdc++.h>\n\nusing namespace std;\n\n#define rep(i, n) for(int i = 0; i < (n); i++)\n#define per(i, n) for(int i = (n)-1; i >= 0; i--)\n#define rep2(i, l, r) for(int i = (l); i < (r); i++)\n#define per2(i, l, r) for(int i = (r)-1; i >= (l); i--)\n#define each(e, x) for(auto &e : x)\n#define sz(x) (int)x.size()\n#define all(x) begin(x), end(x)\n#define rall(x) rbegin(x), rend(x)\n#define eb emplace_back\n#define MM << ' ' <<\n#define TT template<typename T>\nusing ll = long long;\nusing pii = pair<int, int>;\nusing pll = pair<ll, ll>;\n\nTT bool chmin(T &x, T y) { return x > y ? (x = y, true) : false; }\n\nTT bool chmax(T &x, T y) { return x < y ? (x = y, true) : false; }\n\nTT using minheap = priority_queue<T, vector<T>, greater<T>>;\nTT using maxheap = priority_queue<T>;\n\nTT void print(vector<T> v, T x = 0) {\n int n = sz(v);\n rep(i, n) cout << v[i] + x << (i == n - 1 ? '\\n' : ' ');\n if (n == 0) cout << '\\n';\n}\n\nTT void printn(vector<T> v, T x = 0) {\n int n = sz(v);\n rep(i, n) cout << v[i] + x << '\\n';\n}\n\nTT void rearrange(vector<T> &v) {\n sort(all(v));\n v.erase(unique(all(v)), end(v));\n}\n\nTT int lb(vector<T> &v, T x) { return lower_bound(all(v), x) - begin(v); }\n\nTT int ub(vector<T> &v, T x) { return upper_bound(all(v), x) - begin(v); }\n\nconst int inf = (1 << 30) - 1;\nconst ll INF = (1LL << 60) - 1;\n\nstruct io_setup {\n io_setup() {\n ios_base::sync_with_stdio(false);\n cin.tie(NULL);\n cout << fixed << setprecision(15);\n }\n} io_setup;\n\nstruct P {\n double x, y, z;\n P() {}\n P(double x, double y, double z) : x(x), y(y), z(z) {}\n};\n\nP operator+(P p, P q) {\n return P(p.x + q.x, p.y + q.y, p.z + q.z);\n}\n\nP operator-(P p, P q) {\n return P(p.x - q.x, p.y - q.y, p.z - q.z);\n}\n\ndouble p2(double x) {\n return x * x;\n}\n\ndouble dist(P p, P q) {\n return p2(p.x - q.x) + p2(p.y - q.y) + p2(p.z - q.z);\n}\n\ndouble dot(P p, P q) {\n return p.x * q.x + p.y * q.y + p.z * q.z;\n}\n\nP proj(P a, P b, P p) {\n P dap = p - a, dab = b - a;\n double t = dot(dap, dab) / dist(dab, P(0, 0, 0));\n return a + P(dab.x * t, dab.y * t, dab.z * t);\n}\n\ndouble dist(P a, P b, P p) {\n P h = proj(a, b, p);\n if (dot(h - a, b - a) < 0)\n return dist(a, p);\n if (dot(h - b, a - b) < 0)\n return dist(b, p);\n return dist(h, p);\n}\n\nint main() {\n while (1) {\n int n, m, r;\n cin >> n >> m >> r;\n if (!n)\n break;\n vector<int> xs(n), ys(n), zs(n), rs(n);\n rep(i, n) cin >> xs[i] >> ys[i] >> zs[i] >> rs[i];\n vector<P> ps(n);\n rep(i, n) ps[i] = P(xs[i], ys[i], zs[i]);\n vector<int> xt(m), yt(m), zt(m), bt(m);\n rep(i, m) cin >> xt[i] >> yt[i] >> zt[i] >> bt[i];\n vector<P> pt(m);\n rep(i, m) pt[i] = P(xt[i], yt[i], zt[i]);\n int x0, y0, z0;\n cin >> x0 >> y0 >> z0;\n P p0(x0, y0, z0);\n vector<vector<bool>> inter(n, vector<bool>(m));\n rep(i, n) rep(j, m) {\n double d0 = dist(p0, ps[i]), dj = dist(pt[j], ps[i]);\n if (max(d0, dj) < p2(rs[i]))\n inter[i][j] = false;\n else inter[i][j] = (dist(p0, pt[j], ps[i]) < p2(rs[i]));\n }\n vector<int> ng(n, 0);\n rep(i, n) rep(j, m) if (inter[i][j]) ng[i] |= 1 << j;\n double ans = 0;\n rep(t, 1 << m) {\n int cnt = 0;\n rep(i, n) if (ng[i] & t) cnt++;\n if (cnt > r)\n continue;\n double val = 0;\n rep(i, m) if (t >> i & 1) val += bt[i] / dist(p0, pt[i]);\n chmax(ans, val);\n }\n cout << ans << endl;\n }\n}", "accuracy": 1, "time_ms": 260, "memory_kb": 3632, "score_of_the_acc": -0.8734, "final_rank": 9 }, { "submission_id": "aoj_1331_8492601", "code_snippet": "#include<bits/stdc++.h>\n\nusing namespace std;\n\n#define rep(i, n) for(int i = 0; i < (n); i++)\n#define per(i, n) for(int i = (n)-1; i >= 0; i--)\n#define rep2(i, l, r) for(int i = (l); i < (r); i++)\n#define per2(i, l, r) for(int i = (r)-1; i >= (l); i--)\n#define each(e, x) for(auto &e : x)\n#define sz(x) (int)x.size()\n#define all(x) begin(x), end(x)\n#define rall(x) rbegin(x), rend(x)\n#define eb emplace_back\n#define MM << ' ' <<\n#define TT template<typename T>\nusing ll = long long;\nusing pii = pair<int, int>;\nusing pll = pair<ll, ll>;\n\nTT bool chmin(T &x, T y) { return x > y ? (x = y, true) : false; }\n\nTT bool chmax(T &x, T y) { return x < y ? (x = y, true) : false; }\n\nTT using minheap = priority_queue<T, vector<T>, greater<T>>;\nTT using maxheap = priority_queue<T>;\n\nTT void print(vector<T> v, T x = 0) {\n int n = sz(v);\n rep(i, n) cout << v[i] + x << (i == n - 1 ? '\\n' : ' ');\n if (n == 0) cout << '\\n';\n}\n\nTT void printn(vector<T> v, T x = 0) {\n int n = sz(v);\n rep(i, n) cout << v[i] + x << '\\n';\n}\n\nTT void rearrange(vector<T> &v) {\n sort(all(v));\n v.erase(unique(all(v)), end(v));\n}\n\nTT int lb(vector<T> &v, T x) { return lower_bound(all(v), x) - begin(v); }\n\nTT int ub(vector<T> &v, T x) { return upper_bound(all(v), x) - begin(v); }\n\nconst int inf = (1 << 30) - 1;\nconst ll INF = (1LL << 60) - 1;\n\nstruct io_setup {\n io_setup() {\n ios_base::sync_with_stdio(false);\n cin.tie(NULL);\n cout << fixed << setprecision(15);\n }\n} io_setup;\n\nstruct P {\n double x, y, z;\n P() {}\n P(double x, double y, double z) : x(x), y(y), z(z) {}\n};\n\nP operator+(P p, P q) {\n return P(p.x + q.x, p.y + q.y, p.z + q.z);\n}\n\nP operator-(P p, P q) {\n return P(p.x - q.x, p.y - q.y, p.z - q.z);\n}\n\ndouble p2(double x) {\n return x * x;\n}\n\ndouble dist(P p, P q) {\n return p2(p.x - q.x) + p2(p.y - q.y) + p2(p.z - q.z);\n}\n\ndouble dot(P p, P q) {\n return p.x * q.x + p.y * q.y + p.z * q.z;\n}\n\nP proj(P a, P b, P p) {\n P dap = p - a, dab = b - a;\n double t = dot(dap, dab) / dist(dab, P(0, 0, 0));\n return a + P(dab.x * t, dab.y * t, dab.z * t);\n}\n\ndouble dist(P a, P b, P p) {\n P h = proj(a, b, p);\n if (dot(h - a, b - a) < 0)\n return dist(a, p);\n if (dot(h - b, a - b) < 0)\n return dist(b, p);\n return dist(h, p);\n}\n\nint main() {\n while (1) {\n int n, m, r;\n cin >> n >> m >> r;\n if (!n)\n break;\n vector<int> xs(n), ys(n), zs(n), rs(n);\n rep(i, n) cin >> xs[i] >> ys[i] >> zs[i] >> rs[i];\n vector<P> ps(n);\n rep(i, n) ps[i] = P(xs[i], ys[i], zs[i]);\n vector<int> xt(m), yt(m), zt(m), bt(m);\n rep(i, m) cin >> xt[i] >> yt[i] >> zt[i] >> bt[i];\n vector<P> pt(m);\n rep(i, m) pt[i] = P(xt[i], yt[i], zt[i]);\n int x0, y0, z0;\n cin >> x0 >> y0 >> z0;\n P p0(x0, y0, z0);\n vector<vector<bool>> inter(n, vector<bool>(m));\n rep(i, n) rep(j, m) {\n double d0 = dist(p0, ps[i]), dj = dist(pt[j], ps[i]);\n if (max(d0, dj) < p2(rs[i]))\n inter[i][j] = false;\n else inter[i][j] = (dist(p0, pt[j], ps[i]) < p2(rs[i]));\n }\n double ans = 0;\n rep(t, 1 << m) {\n vector<int> f(n, 0);\n double val = 0;\n rep(i, m) {\n if (t >> i & 1) {\n rep(j, n) if (inter[j][i]) f[j] = 1;\n val += bt[i] / dist(p0, pt[i]);\n }\n }\n if (accumulate(all(f), 0) > r)\n continue;\n chmax(ans, val);\n }\n cout << ans << endl;\n }\n}", "accuracy": 1, "time_ms": 7330, "memory_kb": 3648, "score_of_the_acc": -1.8987, "final_rank": 20 }, { "submission_id": "aoj_1331_7980272", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\n#define foa(c, v) for (auto &c : v)\n#define rep(i, n) for (int i = 0; i < int(n); i++)\n#define all(v) v.begin(), v.end()\n#define sz(v) int(size(v))\ntemplate <class T>\nbool chmin(T &a, T b)\n{\n return a > b ? (a = b, 1) : 0;\n}\n\ntemplate <class T>\nbool chmax(T &a, T b)\n{\n return a < b ? (a = b, 1) : 0;\n}\ntemplate <class T>\nostream &operator<<(ostream &os, const vector<T> &vec)\n{\n os << \"{ \";\n foa(c, vec)\n {\n os << c << \", \";\n }\n os << \"}\";\n return os;\n}\n\ntemplate <class T>\nostream &operator<<(ostream &os, const vector<vector<T>> &vv)\n{\n os << \"{ \";\n foa(v, vv)\n {\n os << \"\\n\\t\" << v << \", \";\n }\n os << \"\\n}\";\n return os;\n}\n\n#ifdef LOCAL\n#define debug(x) cerr << __LINE__ << \", \" << #x << \": \" << x << endl;\n#else\n#define debug(x) void(0)\n#endif\n\nusing R = long double;\nconstexpr int dim = 3;\nusing point = array<R, dim>;\n\npoint operator+(point a, point b)\n{\n point c;\n rep(i, dim) c[i] = a[i] + b[i];\n return c;\n}\n\npoint operator-(point a, point b)\n{\n point c;\n rep(i, dim) c[i] = a[i] - b[i];\n return c;\n}\n\nR dot(point a, point b)\n{\n R res = 0;\n rep(i, dim) res += a[i] * b[i];\n return res;\n}\n\nR dsq(point a, point b)\n{\n R res = 0;\n rep(i, dim) res += pow(a[i] - b[i], 2);\n return res;\n}\n\nR distance(point a, point b)\n{\n return sqrt(dsq(a, b));\n}\n\nR dist_from_center(point a)\n{\n R res = 0;\n rep(i, dim) res += pow(a[i], 2);\n return sqrt(res);\n}\n\nR angle(point a, point b, point c)\n{\n a = a - b;\n c = c - b;\n return acos(dot(a, c) / (dist_from_center(a) * dist_from_center(c)));\n}\n\nistream &operator>>(istream &is, point &c)\n{\n rep(i, dim) is >> c[i];\n return is;\n}\n\nostream &operator<<(ostream &os, const point &c)\n{\n os << \"(\";\n rep(i, dim) os << c[i] << \", \";\n os << \")\";\n return os;\n}\n\nusing points = vector<point>;\n\nint bnum;\nvoid solve()\n{\n int lnum, remove_limit;\n cin >> lnum >> remove_limit;\n\n vector<point> balloons(bnum), lights(lnum);\n vector<R> radii(bnum), bright(lnum);\n\n rep(i, bnum)\n {\n cin >> balloons[i];\n cin >> radii[i];\n }\n\n rep(j, lnum)\n {\n cin >> lights[j] >> bright[j];\n }\n\n point goal;\n cin >> goal;\n\n debug(balloons);\n debug(goal);\n\n rep(j, lnum)\n bright[j] /= dsq(goal, lights[j]);\n\n vector<bitset<2123>> barricade(lnum);\n\n auto inside = [&](int bid, point p) -> bool\n {\n return distance(balloons[bid], p) < radii[bid];\n };\n\n auto absorbed = [&](int bid, int lid) -> bool\n {\n const point &light = lights[lid], bcenter = balloons[bid];\n R dl = distance(goal, light), db = distance(goal, bcenter), r = radii[bid];\n\n if (inside(bid, goal))\n return !inside(bid, light);\n\n if (inside(bid, light))\n return true;\n\n assert(db > r);\n R tang = sqrt(db * db - r * r);\n\n if (tang > dl)\n return false;\n\n return angle(light, goal, bcenter) < asin(r / db);\n };\n\n rep(bid, bnum)\n {\n rep(lid, lnum)\n {\n if (absorbed(bid, lid))\n {\n barricade[lid].set(bid, 1);\n }\n }\n }\n\n R ans = 0;\n\n rep(bits, (1 << lnum))\n {\n bitset<2123> b;\n R res = 0;\n rep(lid, lnum) if ((1 << lid) & bits)\n {\n b |= barricade[lid];\n res += bright[lid];\n }\n if (int(b.count()) > remove_limit)\n continue;\n chmax(ans, res);\n }\n\n cout << ans << endl;\n}\n\nint main()\n{\n ios::sync_with_stdio(false);\n cin.tie(nullptr);\n cout << fixed << setprecision(15);\n\n while (cin >> bnum && bnum)\n solve();\n\n return 0;\n}", "accuracy": 1, "time_ms": 370, "memory_kb": 3708, "score_of_the_acc": -1.0092, "final_rank": 15 }, { "submission_id": "aoj_1331_7237186", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\n\nusing ld = long double;\nusing point = tuple<ld,ld,ld>;\n\nbool cross(point balloon, ld radius, point light) {\n auto [lx, ly, lz] = light;\n auto [bx, by, bz] = balloon;\n\n if(hypot(bx, by, bz) <= radius) {\n return hypot(lx - bx, ly - by, lz - bz) > radius;\n }\n\n ld low = 0.0l;\n ld high = 1.0l;\n\n while(high - low >= 1e-7) {\n ld c1 = (low * 2.0l + high) / 3.0l;\n ld c2 = (low + high * 2.0l) / 3.0l;\n\n ld x1 = c1 * lx, y1 = c1 * ly, z1 = c1 * lz;\n ld x2 = c2 * lx, y2 = c2 * ly, z2 = c2 * lz;\n\n ld f1 = hypot(x1 - bx, y1 - by, z1 - bz);\n ld f2 = hypot(x2 - bx, y2 - by, z2 - bz);\n\n if(f1 > f2) low = c1;\n else high = c2;\n }\n\n ld x = low * lx, y = low * ly, z = low * lz;\n return hypot(x - bx, y - by, z - bz) <= radius;\n}\n\nvoid solve(int N, int M, int R) {\n point balloons[N];\n ld radiuses[N];\n\n for (int i = 0; i < N; i++) {\n ld x,y,z;\n cin >> x >> y >> z >> radiuses[i];\n balloons[i] = {x,y,z};\n }\n\n point lights[N];\n ld T[N];\n for (int i = 0; i < M; i++) {\n ld x,y,z;\n cin >> x >> y >> z >> T[i];\n lights[i] = {x,y,z};\n }\n\n ld Ex, Ey, Ez;\n cin >> Ex >> Ey >> Ez;\n for (int i = 0; i < N; i++) {\n auto [x, y, z] = balloons[i];\n balloons[i] = {x - Ex, y - Ey, z - Ez};\n }\n for (int i = 0; i < M; i++) {\n auto [x, y, z] = lights[i];\n lights[i] = {x - Ex, y - Ey, z - Ez};\n }\n\n bitset<2000> obstacles[M];\n for (int i = 0; i < N; i++) {\n for (int j = 0; j < M; j++) {\n if(cross(balloons[i], radiuses[i], lights[j])) {\n obstacles[j].set(i, true);\n }\n }\n }\n\n ld ans = numeric_limits<ld>::min();\n\n for (int bits = 0; bits < (1 << M); bits++) {\n bitset<2000> obs;\n for (int i = 0; i < M; i++) {\n if(bits & (1 << i)) obs |= obstacles[i];\n }\n if(obs.count() > R) continue;\n\n ld score = 0;\n\n for (int i = 0; i < M; i++) {\n if(bits & (1 << i)) {\n auto [x, y, z] = lights[i];\n score += T[i] / powl(hypot(x, y, z), 2.0l);\n }\n }\n\n ans = max(ans, score);\n }\n\n cout << fixed << setprecision(10) << ans << endl;\n}\n\nint main() {\n int N, M, R;\n\n while(true) {\n cin >> N >> M >> R;\n if(N == 0 && M == 0 && R == 0) break;\n solve(N, M, R);\n }\n}", "accuracy": 1, "time_ms": 610, "memory_kb": 3712, "score_of_the_acc": -1.0495, "final_rank": 16 }, { "submission_id": "aoj_1331_6581220", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\n#define rep(i, s, t) for (int i = (int)(s); i < (int)(t); ++i)\n#define revrep(i, t, s) for (int i = (int)(t)-1; i >= (int)(s); --i)\n#define all(x) begin(x), end(x)\ntemplate <typename T> bool chmax(T& a, const T& b) { return a < b ? (a = b, 1) : 0; }\ntemplate <typename T> bool chmin(T& a, const T& b) { return a > b ? (a = b, 1) : 0; }\n\nusing T = double;\nconstexpr T eps = 1e-12;\ninline bool eq(T a, T b) { return std::abs(a - b) < eps; }\ninline bool lt(T a, T b) { return a < b - eps; }\ninline bool leq(T a, T b) { return a < b + eps; }\n\nstruct Vec {\n T x, y, z;\n Vec() = default;\n constexpr Vec(T x, T y, T z) : x(x), y(y), z(z) {}\n constexpr Vec& operator+=(const Vec& r) { x += r.x; y += r.y; z += r.z; return *this; }\n constexpr Vec& operator-=(const Vec& r) { x -= r.x; y -= r.y; z -= r.z; return *this; }\n constexpr Vec& operator*=(T r) { x *= r; y *= r; z *= r; return *this; }\n constexpr Vec& operator/=(T r) { x /= r; y /= r; z /= r; return *this; }\n constexpr Vec operator-() const { return Vec(-x, -y, -z); }\n constexpr Vec operator+(const Vec& r) const { return Vec(*this) += r; }\n constexpr Vec operator-(const Vec& r) const { return Vec(*this) -= r; }\n constexpr Vec operator*(T r) const { return Vec(*this) *= r; }\n constexpr Vec operator/(T r) const { return Vec(*this) /= r; }\n friend constexpr Vec operator*(T r, const Vec& v) { return v * r; }\n};\n\nstd::istream& operator>>(std::istream& is, Vec& p) {\n T x, y, z;\n is >> x >> y >> z;\n p = {x, y, z};\n return is;\n}\n\nT dot(const Vec& a, const Vec& b) {\n return a.x*b.x + a.y*b.y + a.z*b.z;\n}\n\nVec cross(const Vec& a, const Vec& b) {\n return Vec(a.y*b.z-a.z*b.y, a.z*b.x-a.x*b.z, a.x*b.y-a.y*b.x);\n}\n\nnamespace std {\nT norm(const Vec& a) { return dot(a, a); }\nT abs(const Vec& a) { return std::sqrt(std::norm(a)); }\n}\n\nstruct Segment {\n Vec p1, p2;\n Segment() = default;\n Segment(const Vec& p1, const Vec& p2) : p1(p1), p2(p2) {}\n Vec dir() const { return p2 - p1; }\n};\n\nT dist(const Segment& s, const Vec& p) {\n if (lt(dot(p - s.p1, s.dir()), 0)) return std::abs(p - s.p1);\n if (lt(dot(p - s.p2, -s.dir()), 0)) return std::abs(p - s.p2);\n return std::abs(cross(p - s.p1, s.dir())) / std::abs(s.dir());\n}\ntemplate <typename T>\nostream& operator<<(ostream& os, const vector<T>& v) {\n for (int i = 0; i < (int) v.size(); ++i) os << v[i] << \" \";\n return os;\n}\n\nint main() {\n ios_base::sync_with_stdio(false);\n cin.tie(nullptr);\n cout << fixed << setprecision(15);\n\n while (true) {\n int N, M, R;\n cin >> N >> M >> R;\n if (N == 0 && M == 0 && R == 0) break;\n vector<Vec> S(N), T(M);\n vector<double> r(N), b(M);\n rep(i,0,N) cin >> S[i] >> r[i];\n rep(i,0,M) cin >> T[i] >> b[i];\n Vec E;\n cin >> E;\n vector<double> score(M);\n rep(j,0,M) score[j] = b[j] / norm(T[j] - E);\n vector<int> interrupt(N);\n rep(i,0,N) rep(j,0,M) {\n if (lt(abs(E-S[i]), r[i]) && lt(abs(T[j]-S[i]), r[i])) continue;\n if (lt(dist(Segment(E, T[j]), S[i]), r[i])) {\n interrupt[i] |= 1<<j;\n }\n }\n double ans = 0;\n for (int B = 0; B < 1 << M; ++B) {\n int cnt = 0;\n rep(i,0,N) {\n if (interrupt[i] & B) ++cnt;\n }\n if (cnt > R) continue;\n double c = 0;\n rep(j,0,M) if (B>>j&1) c += score[j];\n chmax(ans, c);\n }\n cout << ans << \"\\n\";\n }\n}", "accuracy": 1, "time_ms": 400, "memory_kb": 3584, "score_of_the_acc": -0.8173, "final_rank": 7 }, { "submission_id": "aoj_1331_6391174", "code_snippet": "#include <algorithm>\n#include<bits/stdc++.h>\n#include <new>\n#define rep(i,a,...) for(int i = (a)*(strlen(#__VA_ARGS__)!=0);i<(int)(strlen(#__VA_ARGS__)?__VA_ARGS__:(a));++i)\n#define per(i,a,...) for(int i = (strlen(#__VA_ARGS__)?__VA_ARGS__:(a))-1;i>=(int)(strlen(#__VA_ARGS__)?(a):0);--i)\n#define foreach(i, n) for(auto &i:(n))\n#define all(x) (x).begin(), (x).end()\n#define bit(x) (1ll << (x))\n#define lambda(RES_TYPE, ...) (function<RES_TYPE(__VA_ARGS__)>)[&](__VA_ARGS__) -> RES_TYPE\n#define method(FUNC_NAME, RES_TYPE, ...) function<RES_TYPE(__VA_ARGS__)> FUNC_NAME = lambda(RES_TYPE, __VA_ARGS__)\nusing namespace std;\nusing ll = long long;\nusing pii = pair<int,int>;\nusing pll = pair<ll,ll>;\n//const ll MOD = (ll)1e9+7;\nconst ll MOD = 998244353;\nconst int INF = (ll)1e9+7;\nconst ll INFLL = (ll)1e18;\ntemplate<class t>\nusing vvector = vector<vector<t>>;\ntemplate<class t>\nusing vvvector = vector<vector<vector<t>>>;\ntemplate<class t>\nusing vvvvector = vvector<vector<vector<t>>>;\ntemplate<class t>\nusing priority_queuer = priority_queue<t, vector<t>, greater<t>>;\ntemplate<class t, class u> bool chmax(t &a, u b){if(a<b){a=b;return true;}return false;}\ntemplate<class t, class u> bool chmin(t &a, u b){if(a>b){a=b;return true;}return false;}\n#ifdef DEBUG\n#define debug(x) cout<<\"LINE \"<<__LINE__<<\": \"<<#x<<\" = \"<<x<<endl;\n#else\n#define debug(x) (void)0\n#endif\n\nnamespace templates{\n ll modpow(ll x, ll b,ll mod=MOD){\n ll res = 1;\n while(b){\n if(b&1)res = res * x % mod;\n x = x * x % mod;\n b>>=1;\n }\n return res;\n }\n\n ll modinv(ll x){\n return modpow(x, MOD-2);\n }\n\n bool was_output = false;\n\n void print();\n template <class t> void print(const vector<t> &);\n template <class t, class u> void print(const pair<t, u> &);\n template <class t> void print(const t&);\n template <class Head, class... Tail> void print(const Head&, const Tail&...);\n\n template <class t> void println(const vector<vector<t>>&);\n template <class t> void println(const vector<t>&);\n template <class t> void println(const t&);\n template <class Head, class... Tail> void println(const Head&, const Tail&...);\n void println();\n void newline();\n\n void print(){\n return;\n }\n\n template <class t>\n void print(const vector<t>&x){\n for(const t&i:x)print(i);\n }\n template <class t, class u>\n void print(const pair<t,u>&p){\n print(p.first);\n print(p.second);\n }\n template <class t>\n void print(const t&x){\n if(was_output)cout<<\" \";\n cout<<x;\n was_output = true;\n }\n template <class Head, class... Tail>\n void print(const Head&head,const Tail&...tail){\n print(head);\n print(tail...);\n }\n\n template<class t>\n void println(const vector<vector<t>>&x){\n for(vector<t> i:x)println(i);\n }\n template<class t>\n void println(const vector<t>&x){\n for(const t&i:x)print(i);\n println();\n }\n template <class t>\n void println(const t&x){\n print(x);\n println();\n }\n void println(){\n if(was_output){\n cout << endl;\n was_output = false;\n }\n }\n template <class Head, class... Tail>\n void println(const Head&head,const Tail&...tail){\n print(head);\n println(tail...);\n }\n\n void newline(){\n was_output = true;\n println();\n }\n\n template<class t>\n istream& operator>>(istream&is, vector<t>&x){\n for(auto &i:x)is >> i;\n return is;\n }\n\n template<class t, class u>\n istream& operator>>(istream&is, pair<t, u>&x){\n is >> x.first >> x.second;\n return is;\n }\n\n template<class t>\n ostream& operator<<(ostream&os, vector<t> &v){\n os << \"{\";\n for(t &i:v){\n os << i << \", \";\n }\n os << \"}\";\n return os;\n }\n\n template<class t = long long>\n t in(){\n t res; cin >> res; return res;\n }\n\n template<class t>\n vector<t> sorted(vector<t> line,function<bool(t,t)> comp=[](t a,t b){return a<b;}){\n sort(line.begin(),line.end(),comp);\n return line;\n }\n\n template<class t>\n vector<t> reversed(vector<t> line){\n reverse(line.begin(),line.end());\n return line;\n }\n string reversed(string str){\n reverse(str.begin(),str.end());\n return str;\n }\n\n long long gcd(long long a,long long b){\n while(b){\n a %= b;\n swap(a,b);\n }\n return a;\n }\n\n long long lcm(long long a,long long b){\n return a / gcd(a,b) * b;\n }\n\n class output_initializer{\n public:\n output_initializer(){\n ios::sync_with_stdio(false);\n cin.tie(nullptr);\n cout << setprecision(20);\n }\n };output_initializer OUTPUT_INITIALIZER_INSTANCE = output_initializer();\n}\n\nusing namespace templates;\n\nstring twoChar(int n){\n if(n<10)return \"0\" + to_string(n);\n return to_string(n);\n}\n\nstring toString(int h,int m,int s){\n return twoChar(h)+\":\"+twoChar(m)+\":\"+twoChar(s);\n}\n\nstring toString(vector<int> line){\n return toString(line[0],line[1],line[2]);\n}\n\nvector<int> toTimes(int n){\n return {n/3600,n%3600/60,n%60};\n}\n\nvector<int> expectedTimes(vector<int> times){\n set<int> expected;\n sort(all(times));\n do{\n rep(rotate,60){\n vector<int> adds;\n foreach(i,times)adds.emplace_back((i+rotate)%60);\n int hour = adds[0];\n int minutes = adds[1];\n int secs = adds[2];\n if(hour%5!=minutes/12)continue;\n expected.emplace(hour/5*3600+minutes*60+secs);\n }\n }while(next_permutation(all(times)));\n vector<int> res(all(expected));\n return res;\n}\n\nstring func(int n){\n vvector<int> line(n,vector<int>(3));\n foreach(i,line)foreach(j,i)j=in();\n vector<queue<int>> oks;\n foreach(i,line){\n queue<int> adds;\n foreach(j,expectedTimes(i)){\n adds.emplace(j);\n }\n oks.emplace_back(adds);\n }\n int mint = INF;\n int maxt = 0;\n int dist = INF;\n while(true){\n {\n bool flag = false;\n foreach(i,oks)if(i.empty())flag=true;\n if(flag)break;\n }\n int mins = INF;\n int maxs = 0;\n foreach(i,oks){\n chmax(maxs,i.front());\n chmin(mins,i.front());\n }\n if(chmin(dist,maxs-mins)){\n mint = mins;\n maxt = maxs;\n }\n foreach(i,oks)if(i.front()==mins)i.pop();\n }\n return toString(toTimes(mint)) + \" \" + toString(toTimes(maxt));\n}\n\n\ntemplate<class t>\nclass Vector3{\n\tpublic:\n\t\tt x;\n\t\tt y;\n\t\tt z;\n\t\tVector3():x(0), y(0), z(0){}\n\t\tVector3(t a, t b, t c):x(a), y(b), z(c){}\n\t\tVector3(const Vector3 &o):x(o.x), y(o.y), z(o.z){}\n\n\t\tVector3& operator+=(Vector3 o){x+=o.x;y+=o.y;z+=o.z;return *this;}\n\t\tVector3& operator-=(Vector3 o){x-=o.x;y-=o.y;z-=o.z;return *this;}\n\t\tVector3& operator*=(t o){x*=o;y*=o;z*=o;return *this;}\n\t\tVector3& operator/=(t o){x/=o;y/=o;z/=o;return *this;}\n\n\t\tVector3 operator+(Vector3 o){return Vector3(*this)+=o;}\n\t\tVector3 operator-(Vector3 o){return Vector3(*this)-=o;}\n\t\tVector3 operator*(t o){return Vector3(*this)*=o;}\n\t\tVector3 operator/(t o){return Vector3(*this)/=o;}\n};\n\ntemplate<class t>\ndouble abs(Vector3<t> x){\n\treturn sqrt(x.x * x.x + x.y * x.y + x.z * x.z);\n}\n\ntemplate<class t>\ndouble dot(Vector3<t> x, Vector3<t> y){\n\treturn x.x * y.x + x.y * y.y + x.z * y.z;\n}\n\ntemplate<class t>\nVector3<t> cross(Vector3<t> x, Vector3<t> y){\n\tt rx = x.y * y.z - x.z * y.y;\n\tt ry = x.z * y.x - x.x * y.z;\n\tt rz = x.x * y.y - x.y * y.x;\n\treturn Vector3<t>(rx, ry, rz); \n}\nusing vector3= Vector3<double>;\n\nclass Sphere{\npublic:\n vector3 p;\n double r;\n Sphere(vector3 p,double r):p(p),r(r){}\n};\n\nclass Light{\npublic:\n vector3 p;\n double i;\n Light(vector3 p,double i):p(p),i(i){}\n};\n\nbool isIn(Sphere s,vector3 p){\n return abs(s.p - p) < s.r;\n}\n\nbool isTouch(Sphere s,vector3 from,vector3 to){\n if(isIn(s,from) and isIn(s,to))return false;\n vector3 v1 = to - from;\n vector3 v2 = s.p - from;\n vector3 n = v1 / abs(v1);\n double k = dot(n,v2);\n if(0 < k){\n v2 = v2 - n * min(k,abs(v1));\n }\n return abs(v2) < s.r;\n}\n\ndouble func(int n,int m,int r){\n vector<Sphere> baloons;\n vector<Light> lights;\n rep(_,n){\n int x = in();\n int y = in();\n int z = in();\n int r = in();\n baloons.emplace_back(vector3(x,y,z),r);\n }\n rep(_,m){\n int x = in();\n int y = in();\n int z = in();\n int i = in();\n lights.emplace_back(vector3(x,y,z),i);\n }\n vector3 object = (lambda(vector3){\n int x = in();\n int y = in();\n int z = in();\n return vector3(x,y,z);\n })();\n vvector<int> brokens;\n foreach(light,lights){\n vector<int> broken;\n foreach(baloon,baloons){\n if(isTouch(baloon,object,light.p)){\n broken.emplace_back(1);\n }else{\n broken.emplace_back(0);\n }\n }\n brokens.emplace_back(broken);\n }\n\n vector<int> broken(n,0);\n\n method(rec,double,int p,double sum){\n if(p==m){\n int cnt = 0;\n foreach(i,broken)cnt += i != 0;\n if(cnt > r)return 0;\n return sum;\n }\n double res = 0;\n chmax(res,rec(p+1,sum));\n rep(i,n){\n broken[i] += brokens[p][i];\n }\n chmax(res,rec(p+1,sum + lights[p].i / pow(abs(object-lights[p].p),2)));\n rep(i,n){\n broken[i] -= brokens[p][i];\n }\n return res;\n };\n return rec(0,0);\n}\n\nint main(){\n ios::sync_with_stdio(false);\n cin.tie(nullptr);\n while(true){\n int n = in();\n int m = in();\n int r = in();\n if(n==0)break;\n println(func(n,m,r));\n }\n\n return 0;\n}", "accuracy": 1, "time_ms": 760, "memory_kb": 3660, "score_of_the_acc": -0.9884, "final_rank": 14 }, { "submission_id": "aoj_1331_6367548", "code_snippet": "#include \"bits/stdc++.h\"\nusing namespace std;\n\ntypedef string::const_iterator State;\n#define eps 1e-8L\n#define MAX_MOD 1000000007LL\n#define GYAKU 500000004LL\n#define MOD 998244353LL\n#define pb push_back\n#define mp make_pair\ntypedef long long ll;\ntypedef long double ld;\n#define REP(a, b) for (long long(a) = 0; (a) < (b); ++(a))\n#define ALL(x) (x).begin(), (x).end()\n\n#define int long long\nld dist(tuple<ld, ld, ld> a, tuple<ld, ld, ld> b)\n{\n return pow(get<0>(a) - get<0>(b), 2.0) + pow(get<1>(a) - get<1>(b), 2.0) + pow(get<2>(a) - get<2>(b), 2.0);\n}\n\nvoid solve()\n{\n int n, m, r;\n cin >> n >> m >> r;\n if (n == 0)\n exit(0);\n vector<pair<tuple<ld, ld, ld>, ld>> Balloon;\n REP(i, n)\n {\n ld a, b, c, d;\n cin >> a >> b >> c >> d;\n Balloon.push_back({{a, b, c}, d});\n }\n vector<pair<tuple<ld, ld, ld>, ld>> lights;\n REP(i, m)\n {\n ld a, b, c, d;\n cin >> a >> b >> c >> d;\n lights.push_back({{a, b, c}, d});\n }\n tuple<ld, ld, ld> e;\n cin >> get<0>(e) >> get<1>(e) >> get<2>(e);\n\n vector<int> light_target(n, 0);\n REP(i, m)\n {\n lights[i].second /= dist(e, lights[i].first);\n REP(q, n)\n {\n if (max(dist(e, Balloon[q].first), dist(lights[i].first, Balloon[q].first)) <= Balloon[q].second * Balloon[q].second + eps)\n {\n continue;\n }\n ld bot = 0;\n ld top = 1;\n ld now_min_dist = 1e18;\n REP(t, 100)\n {\n ld botter = (bot + bot + top) / 3.0;\n ld topper = (bot + top + top) / 3.0;\n tuple<ld, ld, ld> target = e;\n get<0>(target) *= botter;\n get<1>(target) *= botter;\n get<2>(target) *= botter;\n\n get<0>(target) += (1.0L - botter) * get<0>(lights[i].first);\n get<1>(target) += (1.0L - botter) * get<1>(lights[i].first);\n get<2>(target) += (1.0L - botter) * get<2>(lights[i].first);\n\n ld dis[2] = {};\n dis[0] = dist(target, Balloon[q].first);\n\n target = e;\n get<0>(target) *= topper;\n get<1>(target) *= topper;\n get<2>(target) *= topper;\n\n get<0>(target) += (1.0L - topper) * get<0>(lights[i].first);\n get<1>(target) += (1.0L - topper) * get<1>(lights[i].first);\n get<2>(target) += (1.0L - topper) * get<2>(lights[i].first);\n\n dis[1] = dist(target, Balloon[q].first);\n if (dis[0] < dis[1])\n {\n top = topper;\n }\n else\n {\n bot = botter;\n }\n\n now_min_dist = min({now_min_dist, dis[0], dis[1]});\n }\n if (now_min_dist <= Balloon[q].second * Balloon[q].second + eps)\n {\n light_target[q] |= (1 << i);\n }\n }\n }\n ld ans = 0;\n REP(i, (1 << m))\n {\n ld now = 0;\n int cnt = 0;\n REP(q, m)\n {\n if ((1 << q) & i)\n now += lights[q].second;\n }\n REP(q, n)\n {\n if (i & light_target[q])\n {\n cnt++;\n }\n }\n if (cnt <= r)\n {\n ans = max(ans, now);\n }\n }\n cout << ans << endl;\n}\n#undef int\n\n// generated by oj-template v4.7.2\n// (https://github.com/online-judge-tools/template-generator)\nint main()\n{\n // Fasterize input/output script\n ios::sync_with_stdio(false);\n cin.tie(nullptr);\n cout << fixed << setprecision(100);\n // scanf/printf user should delete this fasterize input/output script\n\n int t = 10000;\n // cin >> t; // comment out if solving multi testcase\n for (int testCase = 1; testCase <= t; ++testCase)\n {\n solve();\n }\n return 0;\n}", "accuracy": 1, "time_ms": 890, "memory_kb": 3632, "score_of_the_acc": -0.9625, "final_rank": 13 }, { "submission_id": "aoj_1331_6024869", "code_snippet": "#include<iostream>\n#include<ostream>\n#include<cmath>\n#include<cstdio>\n#include<cassert>\n#include<bitset>\n\nusing namespace std;\n\n#define EPS (1e-7)\n#define equals(a,b) (fabs((a)-(b))<EPS)\n\nclass Point3d{\npublic:\n double x,y,z;\n \n Point3d(double x=0,double y=0,double z=0):x(x),y(y),z(z){}\n \n Point3d operator + (const Point3d& a){\n return Point3d(x+a.x,y+a.y,z+a.z);\n }\n Point3d operator - (const Point3d& a){\n return Point3d(x-a.x,y-a.y,z-a.z);\n }\n Point3d operator * (const double& d){\n return Point3d(x*d,y*d,z*d);\n }\n Point3d operator / (const double& d){\n return Point3d(x/d,y/d,z/d);\n }\n \n bool operator < (const Point3d& p)const { return !equals(x,p.x)?x<p.x:((!equals(y,p.y))?y<p.y:(!equals(z,p.z)&&z<p.z)); }\n \n bool operator == (const Point3d& p)const{\n return equals(x,p.x) && equals(y,p.y) && equals(z,p.z);\n }\n \n};\n \nstruct Segment3d{\n Point3d p[2];\n Segment3d(Point3d p1=Point3d(),Point3d p2=Point3d()){\n p[0] = p1, p[1] = p2;\n }\n bool operator == (const Segment3d& seg)const{\n return ( p[0] == seg.p[0] && p[1] == seg.p[1] ) || ( p[0] == seg.p[1] && p[1] == seg.p[0] );\n }\n};\n \ntypedef Point3d Vector3d;\ntypedef Segment3d Line3d;\n \nostream& operator << (ostream& os,const Point3d& p){\n return os << \"(\" << p.x << \",\" << p.y << \",\" << p.z << \")\";\n}\n \nostream& operator << (ostream& os,const Segment3d& seg){\n return os << \"(\" << seg.p[0] << \",\" << seg.p[1] << \")\";\n}\n \ndouble dot(const Point3d& a,const Point3d& b){\n return a.x*b.x + a.y*b.y + a.z*b.z;\n}\n \nVector3d cross(const Point3d& a,const Point3d& b){\n return Vector3d(a.y*b.z-a.z*b.y,a.z*b.x-a.x*b.z,a.x*b.y-a.y*b.x);\n}\n \ninline double norm(const Point3d &p){\n return p.x*p.x + p.y*p.y + p.z*p.z;\n}\n \ninline double abs(const Point3d &p){\n return sqrt(norm(p));\n}\n \ninline double toRad(double theta){\n return theta * M_PI / 180.0;\n}\n\ndouble distanceLP(Line3d line,Point3d p){\n return abs(cross(line.p[1]-line.p[0],p-line.p[0])) / abs(line.p[1]-line.p[0]);\n}\n \nPoint3d project(Segment3d seg,Point3d p){\n Vector3d base = seg.p[1] - seg.p[0];\n double t = dot(p-seg.p[0],base) / norm(base);\n return seg.p[0] + base * t;\n}\n \nPoint3d reflect(Segment3d seg,Point3d p){\n return p + (project(seg,p)-p) * 2.0;\n}\n\nbool on_line3d(Line3d line,Point3d p){\n return equals(abs(cross(line.p[1]-p,line.p[0]-p)),0);\n}\n \nbool on_segment3d(Segment3d seg,Point3d p){\n if( !on_line3d(seg,p) ) return false;\n double dist[3] = { abs(seg.p[1]-seg.p[0]), abs(p-seg.p[0]), abs(p-seg.p[1]) }; \n return on_line3d(seg,p) && equals(dist[0],dist[1]+dist[2]);\n}\n\ndouble distanceSP(Segment3d seg,Point3d p){\n Point3d r = project(seg,p);\n if( on_segment3d(seg,r) ) return abs(p-r);\n return min(abs(seg.p[0]-p),abs(seg.p[1]-p));\n}\n\nusing namespace std;\n\n#define REP(i,s,n) for(int i=s;i<n;i++)\n#define rep(i,n) REP(i,0,n)\n\n\nbool LT(double x, double y){ return !equals(x,y) && x < y; }\nbool LTE(double x, double y){ return equals(x,y) || x < y; }\n\nclass Point3DwithWeight {\npublic:\n Point3d p;\n double weight;\n};\n\nconst int MAX_N = 2100;\nconst int MAX_M = 20;\nint N,M,R;\nbitset<2100> remove_cost[MAX_M];\nPoint3DwithWeight S[MAX_N],T[MAX_M];\nPoint3DwithWeight E;\n\nint getType(double a,double b){\n if( LT(a,b) ) return -1;\n return 1;\n}\n\nvoid init(){\n rep(i,M) remove_cost[i] = 0; \n rep(i,M) {\n rep(j,N){\n int type[2] = { getType(abs(E.p-S[j].p),S[j].weight), getType(abs(T[i].p-S[j].p),S[j].weight) };\n if( type[0] == type[1] && type[0] == -1 ) continue;\n double dist = distanceSP(Segment3d(E.p,T[i].p),S[j].p);\n if( LTE(dist,S[j].weight) ) remove_cost[i][j] = 1;\n }\n }\n}\n\n#define pow2(a) ((a)*(a))\nvoid compute(){\n init();\n double maxi = 0;\n rep(bitmask,(1<<M)){\n bitset<2100> BIT(0);\n rep(i,M) if( (bitmask>>i) & 1 ) BIT |= remove_cost[i];\n if( BIT.count() <= R ) {\n double cost = 0;\n rep(i,M) if( (bitmask>>i) & 1 ) cost += T[i].weight / ( pow2(T[i].p.x-E.p.x) + pow2(T[i].p.y-E.p.y) + pow2(T[i].p.z-E.p.z) );\n maxi = max(maxi,cost);\n }\n }\n printf(\"%.10lf\\n\",maxi);\n}\n\nint main(){\n while( cin >> N >> M >> R, N|M|R ){\n assert( N <= 2000);\n rep(i,N) cin >> S[i].p.x >> S[i].p.y >> S[i].p.z >> S[i].weight;\n rep(i,M) cin >> T[i].p.x >> T[i].p.y >> T[i].p.z >> T[i].weight;\n cin >> E.p.x >> E.p.y >> E.p.z;\n compute();\n }\n return 0;\n}", "accuracy": 1, "time_ms": 400, "memory_kb": 3284, "score_of_the_acc": -0.3426, "final_rank": 4 }, { "submission_id": "aoj_1331_6024788", "code_snippet": "#include <bits/stdc++.h>\n\n#include <bits/stdc++.h>\nusing namespace std;\n\n#define EPS (1e-7)\n#define equals(a,b) (fabs((a)-(b))<EPS)\n\nclass Point3d{\npublic:\n double x,y,z;\n \n Point3d(double x=0,double y=0,double z=0):x(x),y(y),z(z){}\n \n Point3d operator + (const Point3d& a){\n return Point3d(x+a.x,y+a.y,z+a.z);\n }\n Point3d operator - (const Point3d& a){\n return Point3d(x-a.x,y-a.y,z-a.z);\n }\n Point3d operator * (const double& d){\n return Point3d(x*d,y*d,z*d);\n }\n Point3d operator / (const double& d){\n return Point3d(x/d,y/d,z/d);\n }\n \n bool operator < (const Point3d& p)const { return !equals(x,p.x)?x<p.x:((!equals(y,p.y))?y<p.y:(!equals(z,p.z)&&z<p.z)); }\n \n bool operator == (const Point3d& p)const{\n return equals(x,p.x) && equals(y,p.y) && equals(z,p.z);\n }\n \n};\n \nstruct Segment3d{\n Point3d p[2];\n Segment3d(Point3d p1=Point3d(),Point3d p2=Point3d()){\n p[0] = p1, p[1] = p2;\n }\n bool operator == (const Segment3d& seg)const{\n return ( p[0] == seg.p[0] && p[1] == seg.p[1] ) || ( p[0] == seg.p[1] && p[1] == seg.p[0] );\n }\n};\n \ntypedef Point3d Vector3d;\ntypedef Segment3d Line3d;\n \nostream& operator << (ostream& os,const Point3d& p){\n return os << \"(\" << p.x << \",\" << p.y << \",\" << p.z << \")\";\n}\n \nostream& operator << (ostream& os,const Segment3d& seg){\n return os << \"(\" << seg.p[0] << \",\" << seg.p[1] << \")\";\n}\n \ndouble dot(const Point3d& a,const Point3d& b){\n return a.x*b.x + a.y*b.y + a.z*b.z;\n}\n \nVector3d cross(const Point3d& a,const Point3d& b){\n return Vector3d(a.y*b.z-a.z*b.y,a.z*b.x-a.x*b.z,a.x*b.y-a.y*b.x);\n}\n \ninline double norm(const Point3d &p){\n return p.x*p.x + p.y*p.y + p.z*p.z;\n}\n \ninline double abs(const Point3d &p){\n return sqrt(norm(p));\n}\n \ninline double toRad(double theta){\n return theta * M_PI / 180.0;\n}\n\ndouble distanceLP(Line3d line,Point3d p){\n return abs(cross(line.p[1]-line.p[0],p-line.p[0])) / abs(line.p[1]-line.p[0]);\n}\n \nPoint3d project(Segment3d seg,Point3d p){\n Vector3d base = seg.p[1] - seg.p[0];\n double t = dot(p-seg.p[0],base) / norm(base);\n return seg.p[0] + base * t;\n}\n \nPoint3d reflect(Segment3d seg,Point3d p){\n return p + (project(seg,p)-p) * 2.0;\n}\n\nbool on_line3d(Line3d line,Point3d p){\n return equals(abs(cross(line.p[1]-p,line.p[0]-p)),0);\n}\n \nbool on_segment3d(Segment3d seg,Point3d p){\n if( !on_line3d(seg,p) ) return false;\n double dist[3] = { abs(seg.p[1]-seg.p[0]), abs(p-seg.p[0]), abs(p-seg.p[1]) }; \n return on_line3d(seg,p) && equals(dist[0],dist[1]+dist[2]);\n}\n\ndouble distanceSP(Segment3d seg,Point3d p){\n Point3d r = project(seg,p);\n if( on_segment3d(seg,r) ) return abs(p-r);\n return min(abs(seg.p[0]-p),abs(seg.p[1]-p));\n}\n\nusing namespace std;\n\n#define REP(i,s,n) for(int i=s;i<n;i++)\n#define rep(i,n) REP(i,0,n)\n\n\nbool LT(double x, double y){ return !equals(x,y) && x < y; }\nbool LTE(double x, double y){ return equals(x,y) || x < y; }\n\nclass Point3DwithWeight {\npublic:\n Point3d p;\n double weight;\n};\n\nconst int MAX_N = 2100;\nconst int MAX_M = 20;\nint N,M,R;\nbitset<2100> remove_cost[MAX_M];\nPoint3DwithWeight S[MAX_N],T[MAX_M];\nPoint3DwithWeight E;\n\nint getType(double a,double b){\n if( LT(a,b) ) return -1;\n return 1;\n}\n\nvoid init(){\n rep(i,M) remove_cost[i] = 0; \n rep(i,M) {\n rep(j,N){\n int type[2] = { getType(abs(E.p-S[j].p),S[j].weight), getType(abs(T[i].p-S[j].p),S[j].weight) };\n if( type[0] == type[1] && type[0] == -1 ) continue;\n double dist = distanceSP(Segment3d(E.p,T[i].p),S[j].p);\n if( LTE(dist,S[j].weight) ) remove_cost[i][j] = 1;\n }\n }\n}\n\n#define pow2(a) ((a)*(a))\nvoid compute(){\n init();\n double maxi = 0;\n rep(bitmask,(1<<M)){\n bitset<2100> BIT(0);\n rep(i,M) if( (bitmask>>i) & 1 ) BIT |= remove_cost[i];\n if( BIT.count() <= R ) {\n double cost = 0;\n rep(i,M) if( (bitmask>>i) & 1 ) cost += T[i].weight / ( pow2(T[i].p.x-E.p.x) + pow2(T[i].p.y-E.p.y) + pow2(T[i].p.z-E.p.z) );\n maxi = max(maxi,cost);\n }\n }\n printf(\"%.10lf\\n\",maxi);\n}\n\nint main(){\n while( cin >> N >> M >> R, N|M|R ){\n assert( N <= 2000);\n rep(i,N) cin >> S[i].p.x >> S[i].p.y >> S[i].p.z >> S[i].weight;\n rep(i,M) cin >> T[i].p.x >> T[i].p.y >> T[i].p.z >> T[i].weight;\n cin >> E.p.x >> E.p.y >> E.p.z;\n compute();\n }\n return 0;\n}", "accuracy": 1, "time_ms": 400, "memory_kb": 3368, "score_of_the_acc": -0.4755, "final_rank": 5 }, { "submission_id": "aoj_1331_6024746", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\n#define EPS (1e-7)\n#define equals(a,b) (fabs((a)-(b))<EPS)\n\nclass Point3d{\npublic:\n double x,y,z;\n \n Point3d(double x=0,double y=0,double z=0):x(x),y(y),z(z){}\n \n Point3d operator + (const Point3d& a){\n return Point3d(x+a.x,y+a.y,z+a.z);\n }\n Point3d operator - (const Point3d& a){\n return Point3d(x-a.x,y-a.y,z-a.z);\n }\n Point3d operator * (const double& d){\n return Point3d(x*d,y*d,z*d);\n }\n Point3d operator / (const double& d){\n return Point3d(x/d,y/d,z/d);\n }\n \n bool operator < (const Point3d& p)const { return !equals(x,p.x)?x<p.x:((!equals(y,p.y))?y<p.y:(!equals(z,p.z)&&z<p.z)); }\n \n bool operator == (const Point3d& p)const{\n return equals(x,p.x) && equals(y,p.y) && equals(z,p.z);\n }\n \n};\n \nstruct Segment3d{\n Point3d p[2];\n Segment3d(Point3d p1=Point3d(),Point3d p2=Point3d()){\n p[0] = p1, p[1] = p2;\n }\n bool operator == (const Segment3d& seg)const{\n return ( p[0] == seg.p[0] && p[1] == seg.p[1] ) || ( p[0] == seg.p[1] && p[1] == seg.p[0] );\n }\n};\n \ntypedef Point3d Vector3d;\ntypedef Segment3d Line3d;\n \nostream& operator << (ostream& os,const Point3d& p){\n return os << \"(\" << p.x << \",\" << p.y << \",\" << p.z << \")\";\n}\n \nostream& operator << (ostream& os,const Segment3d& seg){\n return os << \"(\" << seg.p[0] << \",\" << seg.p[1] << \")\";\n}\n \ndouble dot(const Point3d& a,const Point3d& b){\n return a.x*b.x + a.y*b.y + a.z*b.z;\n}\n \nVector3d cross(const Point3d& a,const Point3d& b){\n return Vector3d(a.y*b.z-a.z*b.y,a.z*b.x-a.x*b.z,a.x*b.y-a.y*b.x);\n}\n \ninline double norm(const Point3d &p){\n return p.x*p.x + p.y*p.y + p.z*p.z;\n}\n \ninline double abs(const Point3d &p){\n return sqrt(norm(p));\n}\n \ninline double toRad(double theta){\n return theta * M_PI / 180.0;\n}\n\ndouble distanceLP(Line3d line,Point3d p){\n return abs(cross(line.p[1]-line.p[0],p-line.p[0])) / abs(line.p[1]-line.p[0]);\n}\n \nPoint3d project(Segment3d seg,Point3d p){\n Vector3d base = seg.p[1] - seg.p[0];\n double t = dot(p-seg.p[0],base) / norm(base);\n return seg.p[0] + base * t;\n}\n \nPoint3d reflect(Segment3d seg,Point3d p){\n return p + (project(seg,p)-p) * 2.0;\n}\n\nbool on_line3d(Line3d line,Point3d p){\n return equals(abs(cross(line.p[1]-p,line.p[0]-p)),0);\n}\n \nbool on_segment3d(Segment3d seg,Point3d p){\n if( !on_line3d(seg,p) ) return false;\n double dist[3] = { abs(seg.p[1]-seg.p[0]), abs(p-seg.p[0]), abs(p-seg.p[1]) }; \n return on_line3d(seg,p) && equals(dist[0],dist[1]+dist[2]);\n}\n\ndouble distanceSP(Segment3d seg,Point3d p){\n Point3d r = project(seg,p);\n if( on_segment3d(seg,r) ) return abs(p-r);\n return min(abs(seg.p[0]-p),abs(seg.p[1]-p));\n}\n \n\n#define REP(i,s,n) for(int i=s;i<n;i++)\n#define rep(i,n) REP(i,0,n)\n\nbool LT(double x, double y){ return !equals(x,y) && x < y; }\nbool LTE(double x, double y){ return equals(x,y) || x < y; }\n\nclass Point3DwithWeight {\npublic:\n Point3d p;\n double weight;\n};\n\nconst int MAX_N = 2100;\nconst int MAX_M = 20;\nint N,M,R;\nbitset<2100> remove_cost[MAX_M];\nPoint3DwithWeight S[MAX_N],T[MAX_M];\nPoint3DwithWeight E;\n\nint getType(double a,double b){\n if( LT(a,b) ) return -1;\n return 1;\n}\n\nvoid init(){\n rep(i,M) remove_cost[i] = 0; \n rep(i,M) {\n rep(j,N){\n int type[2] = { getType(abs(E.p-S[j].p),S[j].weight), getType(abs(T[i].p-S[j].p),S[j].weight) };\n if( type[0] == type[1] && type[0] == -1 ) continue;\n double dist = distanceSP(Segment3d(E.p,T[i].p),S[j].p);\n if( LTE(dist,S[j].weight) ) remove_cost[i][j] = 1;\n }\n }\n}\n\n#define pow2(a) ((a)*(a))\nvoid compute(){\n init();\n double maxi = 0;\n rep(bitmask,(1<<M)){\n bitset<2100> BIT(0);\n rep(i,M) if( (bitmask>>i) & 1 ) BIT |= remove_cost[i];\n if( BIT.count() <= R ) {\n double cost = 0;\n rep(i,M) if( (bitmask>>i) & 1 ) cost += T[i].weight / ( pow2(T[i].p.x-E.p.x) + pow2(T[i].p.y-E.p.y) + pow2(T[i].p.z-E.p.z) );\n maxi = max(maxi,cost);\n }\n }\n printf(\"%.10lf\\n\",maxi);\n}\n\nint main(){\n while( cin >> N >> M >> R, N|M|R ){\n assert( N <= 2000);\n rep(i,N) cin >> S[i].p.x >> S[i].p.y >> S[i].p.z >> S[i].weight;\n rep(i,M) cin >> T[i].p.x >> T[i].p.y >> T[i].p.z >> T[i].weight;\n cin >> E.p.x >> E.p.y >> E.p.z;\n compute();\n }\n return 0;\n}", "accuracy": 1, "time_ms": 400, "memory_kb": 3232, "score_of_the_acc": -0.2603, "final_rank": 2 }, { "submission_id": "aoj_1331_5973812", "code_snippet": "#pragma GCC optimize(\"Ofast\")\n#include <bits/stdc++.h>\nusing namespace std;\ntypedef long long int ll;\ntypedef unsigned long long ull;\n\nmt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());\nll myRand(ll B) {\n return (ull)rng() % B;\n}\n\n// 3D points\n// AOJ 1331\nstruct _3Dpoint{\n double x,y,z;\n _3Dpoint(){}\n _3Dpoint(double x,double y,double z):x(x),y(y),z(z){}\n _3Dpoint& operator-=(const _3Dpoint &p){\n x -= p.x; y -= p.y; z -= p.z;\n return *this;\n }\n _3Dpoint& operator+=(const _3Dpoint &p){\n x += p.x; y += p.y; z += p.z;\n return *this;\n }\n _3Dpoint& operator*=(double d){\n x *= d; y *= d; z *= d;\n return *this;\n }\n _3Dpoint& operator/=(double d){\n x /= d; y /= d; z /= d;\n return *this;\n }\n const _3Dpoint operator+ (const _3Dpoint& p) const;\n const _3Dpoint operator- (const _3Dpoint& p) const;\n const _3Dpoint operator* (double d) const;\n const _3Dpoint operator/ (double d) const;\n};\nconst _3Dpoint _3Dpoint::operator+ (const _3Dpoint& p) const{\n _3Dpoint res(*this); return res += p;\n}\nconst _3Dpoint _3Dpoint::operator- (const _3Dpoint& p) const{\n _3Dpoint res(*this); return res -= p;\n}\nconst _3Dpoint _3Dpoint::operator* (double d) const{\n _3Dpoint res(*this); return res *= d;\n}\nconst _3Dpoint _3Dpoint::operator/ (double d) const{\n _3Dpoint res(*this); return res /= d;\n}\nstruct _3Dline{\n _3Dpoint A,B;\n _3Dline(_3Dpoint A,_3Dpoint B):A(A),B(B){}\n};\n// A to B\n_3Dpoint vec(_3Dline L){\n return L.B-L.A;\n}\ndouble abs(_3Dpoint P){\n return sqrt(P.x*P.x+P.y*P.y+P.z*P.z);\n}\ndouble dist(_3Dpoint A,_3Dpoint B){\n return abs(A-B);\n}\ndouble dot(_3Dpoint A,_3Dpoint B){\n return A.x*B.x+A.y*B.y+A.z*B.z;\n}\n_3Dpoint cross(_3Dpoint A,_3Dpoint B){\n return _3Dpoint(A.x*B.y-B.x*A.y, A.y*B.z-B.y*A.z, A.z*B.x-B.z*A.x);\n}\n// 点と直線の距離\ndouble Point_line_distance(_3Dpoint P,_3Dline L){\n return abs(cross(P-L.A,vec(L)))/abs(vec(L));\n}\n// 点と線分の距離\ndouble Point_segment_distance(_3Dpoint P,_3Dline L){\n if(dot(P-L.A,vec(L))<0) return dist(P,L.A);\n else if(dot(P-L.B,vec(L))>0) return dist(P,L.B);\n else return Point_line_distance(P,L);\n}\n\nint main(){\n cin.tie(nullptr);\n ios::sync_with_stdio(false);\n int N,M,R;\n while(cin >> N >> M >> R,N){\n vector<_3Dpoint> s(N);\n vector<double> r(N);\n for(int i=0;i<N;i++){\n cin >> s[i].x >> s[i].y >> s[i].z >> r[i];\n }\n vector<_3Dpoint> t(M);\n vector<double> b(M);\n for(int i=0;i<M;i++){\n cin >> t[i].x >> t[i].y >> t[i].z >> b[i];\n }\n _3Dpoint E;\n cin >> E.x >> E.y >> E.z;\n double res=0.0;\n vector<vector<int>> cover(M);\n for(int i=0;i<M;i++){\n _3Dline L(E,t[i]);\n for(int j=0;j<N;j++){\n if(dist(t[i],s[j])<r[j] and dist(E,s[j])<r[j])continue;\n if(Point_segment_distance(s[j],L)<r[j]){\n cover[i].push_back(j);\n }\n }\n }\n vector<int> flg(N);\n for(int i=0;i<(1<<M);i++){\n fill(flg.begin(), flg.end(),0);\n int cnt=0;\n for(int j=0;j<M;j++){\n if((1<<j)&i){\n for(int k:cover[j]){\n if(!flg[k])cnt++;\n flg[k]=1;\n }\n }\n }\n if(cnt<=R){\n double sum=0.0;\n for(int j=0;j<M;j++){\n if((1<<j)&i){\n sum+=b[j]/dist(E,t[j])/dist(E,t[j]);\n }\n }\n res=max(res,sum);\n }\n }\n printf(\"%.10f\\n\",res);\n }\n}", "accuracy": 1, "time_ms": 680, "memory_kb": 3620, "score_of_the_acc": -0.9138, "final_rank": 12 }, { "submission_id": "aoj_1331_5768760", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\nconst double EPS = 1e-8;\ninline bool EQ(double a, double b) { return fabs(b - a) < EPS; }\n\nstruct Point {\n double x, y, z;\n Point() {}\n Point(double x, double y, double z) : x(x), y(y), z(z) {}\n Point operator-() const { return Point(-x, -y, -z); }\n Point operator+(const Point& p) const { return Point(x + p.x, y + p.y, z + p.z); }\n Point operator-(const Point& p) const { return Point(x - p.x, y - p.y, z - p.z); }\n Point operator*(const double& t) const { return Point(x * t, y * t, z * t); }\n Point operator/(const double& t) const { return Point(x / t, y / t, z / t); }\n};\nstruct Line {\n Point a, b;\n Line() {}\n Line(Point a, Point b) : a(a), b(b) {}\n};\nstruct Segment : Line {\n Segment() {}\n Segment(Point a, Point b) : Line(a, b) {}\n};\nstruct Sphere {\n Point c;\n double r;\n Sphere(Point c, double r) : c(c), r(r) {}\n};\n\ndouble dot(const Point& a, const Point& b) { return a.x * b.x + a.y * b.y + a.z * b.z; }\ndouble norm(const Point& p) { return p.x * p.x + p.y * p.y + p.z * p.z; }\ndouble abs(const Point& p) { return sqrt(norm(p)); }\n\nPoint projection(const Line& l, const Point& p) {\n double t = dot(p - l.a, l.b - l.a) / norm(l.b - l.a);\n return l.a + (l.b - l.a) * t;\n}\n\nint intersect(const Sphere& c, const Segment& s) {\n Point h = projection(s, c.c);\n double d1 = abs(c.c - s.a), d2 = abs(c.c - s.b);\n if (norm(h - c.c) - c.r * c.r > EPS) return 0;\n if (d1 < c.r + EPS && d2 < c.r + EPS) return 0;\n if ((d1 < c.r - EPS && d2 > c.r + EPS) || (d1 > c.r + EPS && d2 < c.r - EPS)) return 1;\n if (dot(s.a - h, s.b - h) < 0) return 2;\n return 0;\n}\n\nvoid solve(int N, int M, int R) {\n vector<Sphere> B;\n for (int i = 0; i < N; i++) {\n int x, y, z, r;\n cin >> x >> y >> z >> r;\n B.emplace_back(Point(x, y, z), r);\n }\n vector<Point> L;\n vector<double> b(M);\n for (int i = 0; i < M; i++) {\n int x, y, z;\n cin >> x >> y >> z >> b[i];\n L.emplace_back(x, y, z);\n }\n Point t;\n {\n int x, y, z;\n cin >> x >> y >> z;\n t = Point(x, y, z);\n }\n\n for (int i = 0; i < M; i++) {\n Point p = L[i];\n b[i] /= norm(p - t);\n }\n vector<vector<int>> interupt(M, vector<int>(N));\n for (int i = 0; i < M; i++) {\n for (int j = 0; j < N; j++) {\n Segment s(L[i], t);\n int cross = intersect(B[j], s);\n interupt[i][j] = (cross > 0);\n }\n }\n\n double ans = 0;\n for (int mask = 0; mask < (1 << M); mask++) {\n vector<int> check(N, false);\n double res = 0;\n for (int i = 0; i < M; i++) {\n if (!(mask >> i & 1)) continue;\n res += b[i];\n for (int j = 0; j < N; j++) check[j] |= interupt[i][j];\n }\n int sum = accumulate(check.begin(), check.end(), 0);\n if (sum > R) continue;\n ans = max(ans, res);\n }\n\n cout << ans << '\\n';\n}\n\nint main() {\n cin.tie(0);\n ios::sync_with_stdio(false);\n cout << fixed << setprecision(10);\n int N, M, R;\n while (cin >> N >> M >> R, N && M && R) solve(N, M, R);\n return 0;\n}", "accuracy": 1, "time_ms": 2180, "memory_kb": 3632, "score_of_the_acc": -1.145, "final_rank": 17 } ]
aoj_1333_cpp
Problem I: Beautiful Spacing Text is a sequence of words, and a word consists of characters. Your task is to put words into a grid with W columns and sufficiently many lines. For the beauty of the layout, the following conditions have to be satisfied. The words in the text must be placed keeping their original order. The following figures show correct and incorrect layout examples for a 4 word text "This is a pen" into 11 columns. Figure I.1: A good layout. Figure I.2: BAD | Do not reorder words. Between two words adjacent in the same line, you must place at least one space character. You sometimes have to put more than one space in order to meet other conditions. Figure I.3: BAD | Words must be separated by spaces. A word must occupy the same number of consecutive columns as the number of characters in it. You cannot break a single word into two or more by breaking it into lines or by inserting spaces. Figure I.4: BAD | Characters in a single word must be contiguous. The text must be justified to the both sides. That is, the first word of a line must startfrom the first column of the line, and except the last line, the last word of a line must end at the last column. Figure I.5: BAD | Lines must be justi ed to both the left and the right sides. The text is the most beautifully laid out when there is no unnecessarily long spaces. For instance, the layout in Figure I.6 has at most 2 contiguous spaces, which is more beautiful than that in Figure I.1, having 3 contiguous spaces. Given an input text and the number of columns, please find a layout such that the length of the longest contiguous spaces between words is minimum. Figure I.6: A good and the most beautiful layout. Input The input consists of multiple datasets, each in the following format. W N x 1 x 2 ... x N W , N , and x i are all integers. W is the number of columns (3 ≤ W ≤ 80,000). N is the number of words (2 ≤ N ≤ 50,000). x i is the number of characters in the i -th word (1 ≤ x i ≤ ( W −1)/2 ). Note that the upper bound on x i assures that there always exists a layout satisfying the conditions. The last dataset is followed by a line containing two zeros. Output For each dataset, print the smallest possible number of the longest contiguous spaces between words. Sample Input 11 4 4 2 1 3 5 7 1 1 1 2 2 1 2 11 7 3 1 3 1 3 3 4 100 3 30 30 39 30 3 2 5 3 0 0 Output for the Sample Input 2 1 2 40 1
[ { "submission_id": "aoj_1333_10851267", "code_snippet": "//#include<CSpreadSheet.h>\n\n#include<iostream>\n#include<cmath>\n#include<cstdio>\n#include<sstream>\n#include<cstdlib>\n#include<string>\n#include<string.h>\n#include<cstring>\n#include<algorithm>\n#include<vector>\n#include<map>\n#include<set>\n#include<stack>\n#include<list>\n#include<queue>\n#include<ctime>\n#include<bitset>\n#include<cmath>\n#define eps 1e-6\n#define INF 0x3f3f3f3f\n#define PI acos(-1.0)\n#define ll __int64\n#define LL long long\n#define lson l,m,(rt<<1)\n#define rson m+1,r,(rt<<1)|1\n#define M 1000000007\n//#pragma comment(linker, \"/STACK:1024000000,1024000000\")\nusing namespace std;\n\n#define Maxn 51000\n\nbool dp[Maxn];\nint n,w;\nLL sum[Maxn];\n\nbool iscan(int m)\n{\n if(sum[n]+n-1<=w)\n return true;\n\n memset(dp,0,sizeof(dp));\n dp[0]=1;\n int la=0;\n\n for(int i=0;i<=n-2;i++)\n {\n if(!dp[i])\n continue;\n for(int j=max(i+1,la+1);j<=n;j++)\n {\n if(sum[j]-sum[i]+j-i-1>w)\n break;\n if(sum[j]-sum[i]+(j-i-1)*m<w)\n continue;\n dp[j]=true;\n la=j;\n if(sum[n]-sum[j]+n-j-1<=w)\n return true;\n }\n }\n return false;\n}\nint main()\n{\n //freopen(\"in.txt\",\"r\",stdin);\n //freopen(\"out.txt\",\"w\",stdout);\n while(scanf(\"%d%d\",&w,&n)&&w+n)\n {\n sum[0]=0;\n for(int i=1;i<=n;i++)\n {\n LL temp;\n scanf(\"%lld\",&temp);\n sum[i]=sum[i-1]+temp;\n }\n int l=1,r=w,m,ans;\n\n while(l<=r)\n {\n m=(l+r)>>1;\n if(iscan(m))\n {\n ans=m;\n r=m-1;\n }\n else\n l=m+1;\n }\n printf(\"%d\\n\",ans);\n }\n return 0;\n}", "accuracy": 1, "time_ms": 460, "memory_kb": 4008, "score_of_the_acc": -0.206, "final_rank": 2 }, { "submission_id": "aoj_1333_9796048", "code_snippet": "#include <bits/stdc++.h>\n\nusing namespace std;\n\n#define rep(i, a, b) for (int i = (int)(a); i < (int)(b); i++)\n#define rrep(i, a, b) for (int i = (int)(b)-1; i >= (int)(a); i--)\n#define ALL(v) (v).begin(), (v).end()\n#define UNIQUE(v) sort(ALL(v)), (v).erase(unique(ALL(v)), (v).end())\n#define SZ(v) (int)v.size()\n#define MIN(v) *min_element(ALL(v))\n#define MAX(v) *max_element(ALL(v))\n#define LB(v, x) int(lower_bound(ALL(v), (x)) - (v).begin())\n#define UB(v, x) int(upper_bound(ALL(v), (x)) - (v).begin())\n\nusing uint = unsigned int;\nusing ll = long long int;\nusing ull = unsigned long long;\nusing i128 = __int128_t;\nusing u128 = __uint128_t;\nconst int inf = 0x3fffffff;\nconst ll INF = 0x1fffffffffffffff;\n\ntemplate <typename T> inline bool chmax(T &a, T b) {\n if (a < b) {\n a = b;\n return 1;\n }\n return 0;\n}\ntemplate <typename T> inline bool chmin(T &a, T b) {\n if (a > b) {\n a = b;\n return 1;\n }\n return 0;\n}\ntemplate <typename T, typename U> T ceil(T x, U y) {\n assert(y != 0);\n if (y < 0)\n x = -x, y = -y;\n return (x > 0 ? (x + y - 1) / y : x / y);\n}\ntemplate <typename T, typename U> T floor(T x, U y) {\n assert(y != 0);\n if (y < 0)\n x = -x, y = -y;\n return (x > 0 ? x / y : (x - y + 1) / y);\n}\ntemplate <typename T> int popcnt(T x) {\n return __builtin_popcountll(x);\n}\ntemplate <typename T> int topbit(T x) {\n return (x == 0 ? -1 : 63 - __builtin_clzll(x));\n}\ntemplate <typename T> int lowbit(T x) {\n return (x == 0 ? -1 : __builtin_ctzll(x));\n}\n\ntemplate <class T, class U>\nostream &operator<<(ostream &os, const pair<T, U> &p) {\n os << \"P(\" << p.first << \", \" << p.second << \")\";\n return os;\n}\ntemplate <typename T> ostream &operator<<(ostream &os, const vector<T> &vec) {\n os << \"{\";\n for (int i = 0; i < vec.size(); i++) {\n os << vec[i] << (i + 1 == vec.size() ? \"\" : \", \");\n }\n os << \"}\";\n return os;\n}\ntemplate <typename T, typename U>\nostream &operator<<(ostream &os, const map<T, U> &map_var) {\n os << \"{\";\n for (auto itr = map_var.begin(); itr != map_var.end(); itr++) {\n os << \"(\" << itr->first << \", \" << itr->second << \")\";\n itr++;\n if (itr != map_var.end())\n os << \", \";\n itr--;\n }\n os << \"}\";\n return os;\n}\ntemplate <typename T> ostream &operator<<(ostream &os, const set<T> &set_var) {\n os << \"{\";\n for (auto itr = set_var.begin(); itr != set_var.end(); itr++) {\n os << *itr;\n ++itr;\n if (itr != set_var.end())\n os << \", \";\n itr--;\n }\n os << \"}\";\n return os;\n}\n#ifdef LOCAL\n#define show(...) _show(0, #__VA_ARGS__, __VA_ARGS__)\n#else\n#define show(...) true\n#endif\ntemplate <typename T> void _show(int i, T name) {\n cerr << '\\n';\n}\ntemplate <typename T1, typename T2, typename... T3>\nvoid _show(int i, const T1 &a, const T2 &b, const T3 &...c) {\n for (; a[i] != ',' && a[i] != '\\0'; i++)\n cerr << a[i];\n cerr << \":\" << b << \" \";\n _show(i + 1, a, c...);\n}\n\n/**\n * @brief template\n */\n\ntemplate<typename T>struct BIT{\n int n; T all=0; vector<T> val;\n BIT(int _n=0):n(_n),val(_n+10){}\n void clear(){val.assign(n+10,0); all=T();}\n void add(int i,T x){\n for(i++;i<=n;i+=(i&-i))val[i]=val[i]+x;\n all+=x;\n }\n T sum(int i){\n T res=0;\n for(;i;i-=(i&-i))res+=val[i];\n return res;\n }\n T sum(int L,int R){return sum(R)-sum(L);} // [L,R)\n\n int lower_bound(T x){\n int ret=0,len=1;\n while(2*len<=n)len<<=1;\n for(;len>=1;len>>=1){\n if(ret+len<=n and val[ret+len]<x){\n ret+=len;\n x-=val[ret];\n }\n }\n return ret;\n }\n};\n\n/**\n * @brief Binary Indexed Tree\n */\n\nint main() {\nwhile(1) {\n ll M, N;\n cin >> M >> N;\n if (N == 0) return 0;\n vector<ll> X(N);\n rep(i,0,N) cin >> X[i];\n vector<ll> XS(N+1,0);\n rep(i,0,N) XS[i+1] = XS[i] + X[i];\n auto Calc = [&](int L, int R, ll Len) -> ll {\n return XS[R] - XS[L] + Len*(R-L-1);\n };\n auto Judge = [&](ll Len) -> bool {\n BIT<ll> Seg(N);\n Seg.add(0,1);\n rep(i,1,N) {\n int NG = -1, OK = i;\n while(OK - NG > 1) {\n int MID = (OK + NG) / 2;\n if (Calc(MID,i,1LL) <= M) OK = MID;\n else NG = MID;\n }\n int Left = OK;\n OK = -1, NG = i;\n while(NG - OK > 1) {\n int MID = (OK + NG) / 2;\n if (Calc(MID,i,Len) >= M) OK = MID;\n else NG = MID;\n }\n int Right = OK;\n if (Seg.sum(Left, Right+1) >= 1) Seg.add(i,1);\n }\n {\n int NG = -1, OK = N;\n while(OK - NG > 1) {\n int MID = (OK + NG) / 2;\n if (Calc(MID,N,1LL) <= M) OK = MID;\n else NG = MID;\n }\n if (Seg.sum(OK,N) >= 1) return true;\n else return false;\n }\n };\n {\n int OK = M, NG = 0;\n while(OK - NG > 1) {\n int MID = (OK + NG) / 2;\n if (Judge(MID)) OK = MID;\n else NG = MID;\n }\n cout << OK << endl;\n }\n}\n}", "accuracy": 1, "time_ms": 6710, "memory_kb": 4268, "score_of_the_acc": -1.171, "final_rank": 18 }, { "submission_id": "aoj_1333_8527795", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\n#define rep(i, n) for (int i = 0; i < (n); i++)\n#define per(i, n) for (int i = (n)-1; i >= 0; i--)\n#define rep2(i, l, r) for (int i = (l); i < (r); i++)\n#define per2(i, l, r) for (int i = (r)-1; i >= (l); i--)\n#define each(e, x) for (auto &e : x)\n#define sz(x) (int)x.size()\n#define all(x) begin(x), end(x)\n#define rall(x) rbegin(x), rend(x)\n#define eb emplace_back\n#define MM << ' ' <<\n#define TT template <typename T>\nusing ll = long long;\nusing pii = pair<int, int>;\nusing pll = pair<ll, ll>;\n\nTT bool chmin(T &x, T y) { return x > y ? (x = y, true) : false; }\nTT bool chmax(T &x, T y) { return x < y ? (x = y, true) : false; }\n\nTT using minheap = priority_queue<T, vector<T>, greater<T>>;\nTT using maxheap = priority_queue<T>;\n\nTT void print(vector<T> v, T x = 0) {\n int n = sz(v);\n rep(i, n) cout << v[i] + x << (i == n - 1 ? '\\n' : ' ');\n if (n == 0) cout << '\\n';\n}\n\nTT void printn(vector<T> v, T x = 0) {\n int n = sz(v);\n rep(i, n) cout << v[i] + x << '\\n';\n}\n\nTT void rearrange(vector<T> &v) {\n sort(all(v));\n v.erase(unique(all(v)), end(v));\n}\n\nTT int lb(vector<T> &v, T x) { return lower_bound(all(v), x) - begin(v); }\nTT int ub(vector<T> &v, T x) { return upper_bound(all(v), x) - begin(v); }\n\nconst int inf = (1 << 30) - 1;\nconst ll INF = (1LL << 60) - 1;\n\nstruct io_setup {\n io_setup() {\n ios_base::sync_with_stdio(false);\n cin.tie(NULL);\n cout << fixed << setprecision(15);\n }\n} io_setup;\n\nint main() {\n while (1) {\n int w, n;\n cin >> w >> n;\n if (!w)\n break;\n vector<int> x(n);\n rep(i, n) cin >> x[i];\n vector<ll> sum(n + 1, 0);\n rep(i, n) sum[i + 1] = sum[i] + x[i];\n auto check1 = [&](int l, int r) {\n return sum[r] - sum[l] + r - l - 1 <= w;\n };\n auto check2 = [&](int l, int r, ll k) {\n return sum[r] - sum[l] + k * (r - l - 1) < w;\n };\n int ok = w, ng = 0;\n while (abs(ok - ng) > 1) {\n int mid = (ok + ng) / 2;\n vector<int> dp(n + 2, 0);\n dp[0] = 1, dp[1] = -1;\n int idx1 = 0, idx2 = 0;\n bool f = false;\n rep(i, n) {\n if (i)\n dp[i] += dp[i - 1];\n while (idx1 < n + 1 && check1(i, idx1)) idx1++;\n while (idx2 < n + 1 && check2(i, idx2, mid)) idx2++;\n if (dp[i]) {\n if (idx1 == n + 1)\n f = true;\n else\n dp[idx2]++, dp[idx1]--;\n }\n }\n (f ? ok : ng) = mid;\n }\n cout << ok << '\\n';\n }\n}", "accuracy": 1, "time_ms": 610, "memory_kb": 3960, "score_of_the_acc": -0.2089, "final_rank": 3 }, { "submission_id": "aoj_1333_8516434", "code_snippet": "#include <bits/stdc++.h>\n\nusing namespace std;\nusing ll = long long;\n\nbool is_end = false;\n\ntemplate< typename Monoid >\nstruct SegmentTree {\n\tusing F = function< Monoid(Monoid, Monoid) >;\n\n\tint sz;\n\tvector< Monoid > seg;\n\n\tconst F f;\n\tconst Monoid M1;\n\n\tSegmentTree(int n, const F f, const Monoid &M1) : f(f), M1(M1) {\n\t\tsz = 1;\n\t\twhile(sz < n) sz <<= 1;\n\t\tseg.assign(2 * sz, M1);\n\t}\n\n\tvoid set(int k, const Monoid &x) {\n\t\tseg[k + sz] = x;\n\t}\n\n\tvoid build() {\n\t\tfor(int k = sz - 1; k > 0; k--) {\n\t\t\tseg[k] = f(seg[2 * k + 0], seg[2 * k + 1]);\n\t\t}\n\t}\n\n\tvoid update(int k, const Monoid &x) {\n\t\tk += sz;\n\t\tseg[k] = x;\n\t\twhile(k >>= 1) {\n\t\t\tseg[k] = f(seg[2 * k + 0], seg[2 * k + 1]);\n\t\t}\n\t}\n\n\tMonoid query(int a, int b) {\n\t\tMonoid L = M1, R = M1;\n\t\tfor(a += sz, b += sz; a < b; a >>= 1, b >>= 1) {\n\t\t\tif(a & 1) L = f(L, seg[a++]);\n\t\t\tif(b & 1) R = f(seg[--b], R);\n\t\t}\n\t\treturn f(L, R);\n\t}\n\n\tMonoid operator[](const int &k) const {\n\t\treturn seg[k + sz];\n\t}\n\t\n};\n\nll op(ll a, ll b) {return max(a, b);}\nll e = 0;\n\nbool isOK(ll mid, ll W, vector<ll> &A)\n{\n\tll N = A.size();\n\t\n\tvector<ll> lower(N+1, 0), upper(N+1, 0);\n\tfor (int i = 0; i < N; ++i)\n\t{\n\t\tlower[i+1] = lower[i] + A[i] + 1;\n\t\tupper[i+1] = upper[i] + A[i] + mid;\n\t}\n\t\n\tll lst = 0;\n\tSegmentTree<ll> seg(N+1, op, e);\n\tseg.update(0, 1);\n\t\n\t// cerr << mid << endl;\n\tfor (ll i = 1; i <= N; ++i)\n\t{\n\t\tauto itrl = lower_bound(lower.begin(), lower.end(), lower[i] - (W + 1));\n\t\tll mn = itrl - lower.begin();\n\t\tauto itru = upper_bound(upper.begin(), upper.end(), upper[i] - (W + mid));\n\t\tll mx = itru - upper.begin() - 1;\n\t\t\n\t\tmx = min(mx, i-2);\n\t\tif (mx < 0) continue;\n\t\t\n\t\tll res = seg.query(mn, mx+1);\n\t\tseg.update(i, res);\n\t\tif (res > 0) {lst = i;}\n\t\t// cerr << i << \": \" << mx << \" \" << mn << \" \" << res << endl;\n\t}\n\t\n\treturn lower[N] - lower[lst] <= W + 1;\n}\n\n\nvoid calc()\n{\n\tll W, N; cin >> W >> N;\n\t\n\tif (W == 0 && N == 0)\n\t{\n\t\tis_end = true;\n\t\treturn;\n\t}\n\t\n\tvector<ll> A(N);\n\tfor (int i = 0; i < N; ++i) {cin >> A[i];}\n\t\n\tll ng = 0, ok = W;\n\twhile (abs(ok - ng) > 1)\n\t{\n\t\tll mid = (ok + ng) / 2;\n\t\tif (isOK(mid, W, A)) ok = mid;\n\t\telse ng = mid;\n\t}\n\t\n\tcout << ok << endl;\n\t\n\treturn;\n}\n\nint main()\n{\n\twhile (!is_end)\n\t{\n\t\tcalc();\n\t}\n\t\n\treturn 0;\n}", "accuracy": 1, "time_ms": 7660, "memory_kb": 5208, "score_of_the_acc": -1.6537, "final_rank": 20 }, { "submission_id": "aoj_1333_7177685", "code_snippet": "#pragma GCC optimize(\"Ofast\")\n#include <iostream>\n#include <vector>\n#include <algorithm>\n#include <map>\n#include <queue>\n#include <cstdio>\n#include <ctime>\n#include <assert.h>\n#include <chrono>\n#include <random>\n#include <numeric>\n#include <set>\n#include <deque>\n#include <stack>\n#include <sstream>\n#include <utility>\n#include <cstring>\n#include <unordered_map>\n#include <unordered_set>\n#include <tuple>\n#include <array>\n#include <bitset>\nusing namespace std;\ntypedef long long int ll;\ntypedef unsigned long long ull;\n\nmt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());\nll myRand(ll B) {\n return (ull)rng() % B;\n}\ninline double time() {\n return static_cast<long double>(chrono::duration_cast<chrono::nanoseconds>(chrono::steady_clock::now().time_since_epoch()).count()) * 1e-9;\n}\n\nint main(){\n cin.tie(nullptr);\n ios::sync_with_stdio(false);\n int w,n;\n while(cin >> w >> n,n){\n vector<int> x(n);\n vector<ll> sum(n+1);\n for(int i=0;i<n;i++){\n cin >> x[i];\n sum[i+1] = sum[i]+x[i];\n }\n auto slv = [&](ll mid)->bool{\n vector<int> dp(n+2);\n dp[0] = 1; dp[1] = -1;\n for(int i=0;i<n;i++){\n dp[i+1] += dp[i];\n if(!dp[i]) continue;\n int l = i, r = n;\n while(r-l>1){\n int m = (l+r)/2;\n ll s = sum[m+1]-sum[i];\n if(s+(ll)(m-i)*mid >= w){\n r = m;\n }\n else{\n l = m;\n }\n }\n if(r == n) return true;\n if(sum[r+1]-sum[i]+(r-i) > w) continue;\n int nl = r, nr = n;\n while(nr-nl>1){\n int m = (nl+nr)/2;\n ll s = sum[m+1]-sum[i];\n if(s+(m-i) <= w){\n nl = m;\n }\n else{\n nr = m;\n }\n }\n // [r,nl]\n dp[r+1]++;\n dp[nl+2]--;\n if(nl+1 == n) return true; // 枝刈り\n }\n return dp[n];\n };\n int l = 0, r = w;\n while(r-l>1){\n int mid = (l+r)/2;\n if(slv(mid)){\n r = mid;\n }\n else{\n l = mid;\n }\n }\n cout << r << \"\\n\";\n }\n}", "accuracy": 1, "time_ms": 4160, "memory_kb": 3876, "score_of_the_acc": -0.6706, "final_rank": 10 }, { "submission_id": "aoj_1333_7177673", "code_snippet": "#pragma GCC optimize(\"Ofast\")\n#include <iostream>\n#include <vector>\n#include <algorithm>\n#include <map>\n#include <queue>\n#include <cstdio>\n#include <ctime>\n#include <assert.h>\n#include <chrono>\n#include <random>\n#include <numeric>\n#include <set>\n#include <deque>\n#include <stack>\n#include <sstream>\n#include <utility>\n#include <cstring>\n#include <unordered_map>\n#include <unordered_set>\n#include <tuple>\n#include <array>\n#include <bitset>\nusing namespace std;\ntypedef long long int ll;\ntypedef unsigned long long ull;\n\nmt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());\nll myRand(ll B) {\n return (ull)rng() % B;\n}\ninline double time() {\n return static_cast<long double>(chrono::duration_cast<chrono::nanoseconds>(chrono::steady_clock::now().time_since_epoch()).count()) * 1e-9;\n}\n\nint main(){\n cin.tie(nullptr);\n ios::sync_with_stdio(false);\n int w,n;\n while(cin >> w >> n,n){\n vector<int> x(n);\n vector<ll> sum(n+1);\n for(int i=0;i<n;i++){\n cin >> x[i];\n sum[i+1] = sum[i]+x[i];\n }\n auto slv = [&](ll mid)->bool{\n vector<int> dp(n+2);\n dp[0] = 1; dp[1] = -1;\n for(int i=0;i<n;i++){\n dp[i+1] += dp[i];\n if(!dp[i]) continue;\n { // 枝刈り\n ll s = sum[n]-sum[i];\n if(s+(ll)(n-1-i)*mid < w) return true;\n }\n int l = i, r = n;\n while(r-l>1){\n int m = (l+r)/2;\n ll s = sum[m+1]-sum[i];\n if(s+(ll)(m-i)*mid >= w){\n r = m;\n }\n else{\n l = m;\n }\n }\n // if(r == n) return true;\n if(sum[r+1]-sum[i]+(r-i) > w) continue;\n int nl = r, nr = n;\n while(nr-nl>1){\n int m = (nl+nr)/2;\n ll s = sum[m+1]-sum[i];\n if(s+(m-i) <= w){\n nl = m;\n }\n else{\n nr = m;\n }\n }\n // [r,nl]\n dp[r+1]++;\n dp[min(n+1,nl+2)]--;\n }\n return dp[n];\n };\n int l = 0, r = w;\n while(r-l>1){\n int mid = (l+r)/2;\n if(slv(mid)){\n r = mid;\n }\n else{\n l = mid;\n }\n }\n cout << r << \"\\n\";\n }\n}", "accuracy": 1, "time_ms": 4220, "memory_kb": 3960, "score_of_the_acc": -0.7103, "final_rank": 12 }, { "submission_id": "aoj_1333_7177662", "code_snippet": "#pragma GCC optimize(\"Ofast\")\n#include <iostream>\n#include <vector>\n#include <algorithm>\n#include <map>\n#include <queue>\n#include <cstdio>\n#include <ctime>\n#include <assert.h>\n#include <chrono>\n#include <random>\n#include <numeric>\n#include <set>\n#include <deque>\n#include <stack>\n#include <sstream>\n#include <utility>\n#include <cstring>\n#include <unordered_map>\n#include <unordered_set>\n#include <tuple>\n#include <array>\n#include <bitset>\nusing namespace std;\ntypedef long long int ll;\ntypedef unsigned long long ull;\n\nmt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());\nll myRand(ll B) {\n return (ull)rng() % B;\n}\ninline double time() {\n return static_cast<long double>(chrono::duration_cast<chrono::nanoseconds>(chrono::steady_clock::now().time_since_epoch()).count()) * 1e-9;\n}\n\nint main(){\n cin.tie(nullptr);\n ios::sync_with_stdio(false);\n int w,n;\n while(cin >> w >> n,n){\n vector<int> x(n);\n vector<ll> sum(n+1);\n for(int i=0;i<n;i++){\n cin >> x[i];\n sum[i+1] = sum[i]+x[i];\n }\n auto slv = [&](ll mid)->bool{\n vector<int> dp(n+2);\n dp[0] = 1; dp[1] = -1;\n for(int i=0;i<n;i++){\n dp[i+1] += dp[i];\n if(!dp[i]) continue;\n int l = i, r = n;\n while(r-l>1){\n int m = (l+r)/2;\n ll s = sum[m+1]-sum[i];\n if(s+(ll)(m-i)*mid >= w){\n r = m;\n }\n else{\n l = m;\n }\n }\n if(r == n) return true;\n if(sum[r+1]-sum[i]+(r-i) > w) continue;\n int nl = r, nr = n;\n while(nr-nl>1){\n int m = (nl+nr)/2;\n ll s = sum[m+1]-sum[i];\n if(s+(m-i) <= w){\n nl = m;\n }\n else{\n nr = m;\n }\n }\n // [r,nl]\n dp[r+1]++;\n dp[min(n+1,nl+2)]--;\n }\n return dp[n];\n };\n int l = 0, r = w;\n while(r-l>1){\n int mid = (l+r)/2;\n if(slv(mid)){\n r = mid;\n }\n else{\n l = mid;\n }\n }\n cout << r << \"\\n\";\n }\n}", "accuracy": 1, "time_ms": 4170, "memory_kb": 3876, "score_of_the_acc": -0.672, "final_rank": 11 }, { "submission_id": "aoj_1333_6676969", "code_snippet": "/**\n * author: otera\n**/\n#include<bits/stdc++.h>\nusing namespace std;\n\n#define int long long\n\nusing ll = long long;\nusing ld = long double;\nusing ull = unsigned long long;\nusing int128_t = __int128_t;\n#define repa(i, n) for(int i = 0; i < n; ++ i)\n#define repb(i, a, b) for(int i = a; i < b; ++ i)\n#define repc(i, a, b, c) for(int i = a; i < b; i += c)\n#define overload4(a, b, c, d, e, ...) e\n#define overload3(a, b, c, d, ...) d\n#define rep(...) overload4(__VA_ARGS__, repc, repb, repa)(__VA_ARGS__)\n#define rep1a(i, n) for(int i = 0; i <= n; ++ i)\n#define rep1b(i, a, b) for(int i = a; i <= b; ++ i)\n#define rep1c(i, a, b, c) for(int i = a; i <= b; i += c)\n#define rep1(...) overload4(__VA_ARGS__, rep1c, rep1b, rep1a)(__VA_ARGS__)\n#define rev_repa(i, n) for(int i=n-1;i>=0;i--)\n#define rev_repb(i, a, b) assert(a > b);for(int i=a;i>b;i--)\n#define rev_rep(...) overload3(__VA_ARGS__, rev_repb, rev_repa)(__VA_ARGS__)\n#define rev_rep1a(i, n) for(int i=n;i>=1;i--)\n#define rev_rep1b(i, a, b) assert(a >= b);for(int i=a;i>=b;i--)\n#define rev_rep1(...) overload3(__VA_ARGS__, rev_rep1b, rev_rep1a)(__VA_ARGS__)\ntypedef pair<int, int> P;\ntypedef pair<ll, ll> LP;\n#define pb push_back\n#define pf push_front\n#define ppb pop_back\n#define ppf pop_front\n#define eb emplace_back\n#define fr first\n#define sc second\n#define all(c) c.begin(),c.end()\n#define rall(c) c.rbegin(), c.rend()\n#define lb(c, x) distance((c).begin(), lower_bound(all(c), (x)))\n#define ub(c, x) distance((c).begin(), upper_bound(all(c), (x)))\n#define Sort(a) sort(all(a))\n#define Rev(a) reverse(all(a))\n#define Uniq(a) sort(all(a));a.erase(unique(all(a)),end(a))\n#define si(c) (int)(c).size()\ninline ll popcnt(ull a){ return __builtin_popcountll(a); }\n#define kth_bit(x, k) ((x>>k)&1)\n#define unless(A) if(!(A))\nll intpow(ll a, ll b){ ll ans = 1; while(b){ if(b & 1) ans *= a; a *= a; b /= 2; } return ans; }\nll intpow(ll a, ll b, ll m) {ll ans = 1; while(b){ if(b & 1) (ans *= a) %= m; (a *= a) %= m; b /= 2; } return ans; }\ntemplate<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }\ntemplate<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }\n#define INT(...) int __VA_ARGS__;in(__VA_ARGS__)\n#define LL(...) ll __VA_ARGS__;in(__VA_ARGS__)\n#define ULL(...) ull __VA_ARGS__;in(__VA_ARGS__)\n#define STR(...) string __VA_ARGS__;in(__VA_ARGS__)\n#define CHR(...) char __VA_ARGS__;in(__VA_ARGS__)\n#define DBL(...) double __VA_ARGS__;in(__VA_ARGS__)\n#define LD(...) ld __VA_ARGS__;in(__VA_ARGS__)\n#define vec(type,name,...) vector<type>name(__VA_ARGS__)\n#define VEC(type,name,size) vector<type>name(size);in(name)\n#define vv(type,name,h,...) vector<vector<type>>name(h,vector<type>(__VA_ARGS__))\n#define VV(type,name,h,w) vector<vector<type>>name(h,vector<type>(w));in(name)\n#define vvv(type,name,h,w,...) vector<vector<vector<type>>>name(h,vector<vector<type>>(w,vector<type>(__VA_ARGS__)))\ntemplate <class T> using vc = vector<T>;\ntemplate <class T> using vvc = vector<vc<T>>;\ntemplate <class T> using vvvc = vector<vvc<T>>;\ntemplate <class T> using vvvvc = vector<vvvc<T>>;\ntemplate <class T> using pq = priority_queue<T>;\ntemplate <class T> using pqg = priority_queue<T, vector<T>, greater<T>>;\ntemplate <class T, class U> using umap = unordered_map<T, U>;\ntemplate<class T> void scan(T& a){ cin >> a; }\ntemplate<class T> void scan(vector<T>& a){ for(auto&& i : a) scan(i); }\nvoid in(){}\ntemplate <class Head, class... Tail> void in(Head& head, Tail&... tail){ scan(head); in(tail...); }\nvoid print(){ cout << ' '; }\ntemplate<class T> void print(const T& a){ cout << a; }\ntemplate<class T> void print(const vector<T>& a){ if(a.empty()) return; print(a[0]); for(auto i = a.begin(); ++i != a.end(); ){ cout << ' '; print(*i); } }\nint out(){ cout << '\\n'; return 0; }\ntemplate<class T> int out(const T& t){ print(t); cout << '\\n'; return 0; }\ntemplate<class Head, class... Tail> int out(const Head& head, const Tail&... tail){ print(head); cout << ' '; out(tail...); return 0; }\n#define CHOOSE(a) CHOOSE2 a\n#define CHOOSE2(a0,a1,a2,a3,a4,x,...) x\n#define debug_1(x1) cout<<#x1<<\": \"<<x1<<endl\n#define debug_2(x1,x2) cout<<#x1<<\": \"<<x1<<\", \"#x2<<\": \"<<x2<<endl\n#define debug_3(x1,x2,x3) cout<<#x1<<\": \"<<x1<<\", \"#x2<<\": \"<<x2<<\", \"#x3<<\": \"<<x3<<endl\n#define debug_4(x1,x2,x3,x4) cout<<#x1<<\": \"<<x1<<\", \"#x2<<\": \"<<x2<<\", \"#x3<<\": \"<<x3<<\", \"#x4<<\": \"<<x4<<endl\n#define debug_5(x1,x2,x3,x4,x5) cout<<#x1<<\": \"<<x1<<\", \"#x2<<\": \"<<x2<<\", \"#x3<<\": \"<<x3<<\", \"#x4<<\": \"<<x4<<\", \"#x5<<\": \"<<x5<<endl\n#ifdef DEBUG\n#define debug(...) CHOOSE((__VA_ARGS__,debug_5,debug_4,debug_3,debug_2,debug_1,~))(__VA_ARGS__)\n#define dump(...) { print(#__VA_ARGS__); print(\":\"); out(__VA_ARGS__); }\n#else\n#define debug(...)\n#define dump(...)\n#endif\n\nstruct io_setup {\n io_setup(int precision = 20) {\n ios::sync_with_stdio(false);\n cin.tie(0);\n cout << fixed << setprecision(precision);\n }\n} io_setup_ {};\n\nvoid solve() {\n int w, n;\n while(cin >> w >> n) {\n if(w == 0 && n == 0) break;\n VEC(int, x, n);\n vc<int> sum(n + 1, 0);\n rep(i, n) {\n sum[i + 1] = sum[i] + x[i];\n }\n auto check = [&](int len) -> bool {\n debug(len);\n vc<int> dp(n + 2, 0);\n dp[0] = 1;\n dp[1] = -1;\n rep(i, n) {\n if(i - 1 >= 0) dp[i] += dp[i - 1];\n if(dp[i] == 0) continue;\n debug(i, dp[i]);\n // debug(i, sum[n] - sum[i] + (n - i - 1));\n if(sum[n] - sum[i] + (n - i - 1) <= w) return true;\n int ok = i + 2, ng = n;\n auto ck1 = [&](int id) -> bool {\n // debug(i, id, sum[id] - sum[i] + id - i - 1);\n return sum[id] - sum[i] + (id - i - 1) <= w;\n };\n while(ng - ok > 1) {\n int mid = (ok + ng) / 2;\n if(ck1(mid)) ok = mid;\n else ng = mid;\n }\n debug(i, ok);\n int mx = ok;\n ng = i + 1;\n auto ck2 = [&](int id) -> bool {\n if(sum[id] - sum[i] > w) return false;\n // (w - (sum[id] - sum[i])) / (id - i - 1) <= len\n return len * (id - i - 1) >= w - (sum[id] - sum[i]);\n };\n if(!ck2(mx)) continue;\n while(ok - ng > 1) {\n int mid = (ok + ng) / 2;\n if(ck2(mid)) ok = mid;\n else ng = mid;\n }\n debug(i, ok, mx);\n dp[ok] ++;\n dp[mx + 1] --;\n }\n return false;\n };\n int ok = w, ng = 0;\n while(ok - ng > 1) {\n int mid = (ok + ng) / 2;\n if(check(mid)) ok = mid;\n else ng = mid;\n }\n out(ok);\n }\n}\n\nsigned main() {\n int testcase = 1;\n // in(testcase);\n while(testcase--) solve();\n return 0;\n}", "accuracy": 1, "time_ms": 3030, "memory_kb": 4296, "score_of_the_acc": -0.6704, "final_rank": 9 }, { "submission_id": "aoj_1333_6663561", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\n#define rep(i, s, t) for (int i = (int)(s); i < (int)(t); ++i)\n#define revrep(i, t, s) for (int i = (int)(t)-1; i >= (int)(s); --i)\n#define all(x) begin(x), end(x)\ntemplate <typename T> bool chmax(T& a, const T& b) { return a < b ? (a = b, 1) : 0; }\ntemplate <typename T> bool chmin(T& a, const T& b) { return a > b ? (a = b, 1) : 0; }\n\ntemplate <typename T>\nostream& operator<<(ostream& os, const vector<T>& v) {\n for (int i = 0; i < (int) v.size(); ++i) os << v[i] << \" \";\n return os;\n}\n\nint main() {\n ios_base::sync_with_stdio(false);\n cin.tie(nullptr);\n cout << fixed << setprecision(15);\n\n while (true) {\n int W, N;\n cin >> W >> N;\n if (W == 0) break;\n vector<ll> x(N);\n for (auto& y : x) cin >> y;\n x.push_back(1e9);\n int lb = 0, ub = W;\n while (ub - lb > 1) {\n int m = (lb + ub) / 2;\n vector<int> reachable(N+2);\n ll small = x[0] + m, big = x[0] + 1;\n int j = 0, k = 0;\n reachable[0] = 1;\n reachable[1] = -1;\n bool ok = true;\n\n rep(i,0,N) {\n while (j < N && small < W+m) {\n small += x[++j] + m;\n }\n while (k < N && big <= W+1) {\n big += x[++k] + 1;\n }\n if (reachable[i] && min(j+1, N) < k+1) {\n ++reachable[min(j+1, N)];\n --reachable[k+1];\n }\n small -= x[i] + m;\n big -= x[i] + 1;\n reachable[i+1] += reachable[i];\n }\n if (reachable[N]) {\n ub = m;\n } else {\n lb = m;\n }\n }\n cout << ub << \"\\n\";\n }\n}", "accuracy": 1, "time_ms": 540, "memory_kb": 4164, "score_of_the_acc": -0.2753, "final_rank": 5 }, { "submission_id": "aoj_1333_6366925", "code_snippet": "/*\tauthor: Kite_kuma\n\tcreated: 2022.03.01 15:08:03 */\n\n// #ifdef LOCAL\n// #define _GLIBCXX_DEBUG\n// #endif\n#include <bits/stdc++.h>\nusing namespace std;\n\n#pragma region macros\n\n#define foa(s, v) for(auto &s : v)\n#define all(v) (v).begin(), (v).end()\n#define rall(v) (v).rbegin(), (v).rend()\n#define popcnt(n) __builtin_popcountll((long long)n)\n\n#define REPname(a, b, c, d, e, ...) e\n#define rep(...) REPname(__VA_ARGS__, REP3, REP2, REP1, REP0)(__VA_ARGS__)\n#define REP0(x) for(int Counter_in_rep_macro = 0; Counter_in_rep_macro < (x); ++Counter_in_rep_macro)\n#define REP1(i, x) for(int i = 0; i < (x); ++i)\n#define REP2(i, l, r) for(int i = (l); i < (r); ++i)\n#define REP3(i, l, r, c) for(int i = (l); i < (r); i += (c))\n\n#define DREPname(a, b, c, d, e, ...) e\n#define drep(...) DREPname(__VA_ARGS__, DREP3, DREP2, DREP1)(__VA_ARGS__)\n#define DREP1(i, x) for(int i = (x)-1; i >= 0; --i)\n#define DREP2(i, l, r) for(int i = (r)-1; i >= (l); --i)\n#define DREP3(i, l, r, c) for(int i = (r)-1; i >= (l); i -= (c))\n\n#pragma endregion\n\n#pragma region aliases\n\nusing ll = long long;\nusing ld = long double;\nusing ull = unsigned long long;\nusing vi = std::vector<int>;\nusing vvi = std::vector<std::vector<int>>;\nusing vvvi = std::vector<std::vector<std::vector<int>>>;\nusing vll = std::vector<ll>;\nusing vvll = std::vector<vll>;\nusing vvvll = std::vector<vvll>;\nusing pii = std::pair<int, int>;\nusing pll = std::pair<long long, long long>;\ntemplate <class T = ll>\nusing V = std::vector<T>;\ntemplate <class T = ll>\nusing VV = V<V<T>>;\ntemplate <class T = ll>\nusing VVV = V<V<V<T>>>;\ntemplate <class T = ll>\nusing pqup = std::priority_queue<T, std::vector<T>, std::greater<T>>;\ntemplate <class T = ll>\nusing pqdn = std::priority_queue<T>;\n\n#pragma endregion\n\n#pragma region constants\n\nconst int inf = 1e9;\nconst long long INF = 1e18;\nconst long double pi = acos(-1);\nconst char dl = '\\n';\nconst char sp = ' ';\nint dx[8] = {1, 0, -1, 0, 1, -1, -1, 1};\nint dy[8] = {0, 1, 0, -1, 1, 1, -1, -1};\n\nconst int mod_1000000007 = 1000000007;\nconst int mod_998244353 = 998244353;\n\n#pragma endregion\n\n#pragma region basic_operation\n\ntemplate <class T0, class T1, class T2>\ninline bool in_range(T0 x, T1 lef, T2 rig) {\n\treturn ((lef <= x) && (x < rig));\n}\n\ntemplate <class T>\ninline bool chmin(T &a, T b) {\n\tif(a > b) {\n\t\ta = b;\n\t\treturn true;\n\t}\n\treturn false;\n}\ntemplate <class T>\ninline bool chmax(T &a, T b) {\n\tif(a < b) {\n\t\ta = b;\n\t\treturn true;\n\t}\n\treturn false;\n}\n\nvoid Yes(bool f = 1) { std::cout << (f ? \"Yes\" : \"No\") << '\\n'; }\nvoid No() { std::cout << \"No\\n\"; }\nvoid YES(bool f = 1) { std::cout << (f ? \"YES\" : \"NO\") << '\\n'; }\nvoid NO() { std::cout << \"NO\\n\"; }\n\ntemplate <class T>\nvoid drop(T answer) {\n\tstd::cout << answer << '\\n';\n\texit(0);\n}\n\nvoid err(bool flag = true) {\n\tif(!flag) return;\n\tstd::cout << -1 << '\\n';\n\texit(0);\n}\n\ntemplate <class T>\nvoid vout(std::vector<T> const &v, bool vertically = 0) {\n\tif(vertically) {\n\t\tfor(auto const &a : v) {\n\t\t\tstd::cout << a << '\\n';\n\t\t}\n\t\treturn;\n\t}\n\tfor(auto it = v.begin(); it != v.end(); it++) {\n\t\tstd::cout << (*it);\n\t\tif(it != v.end() - 1) {\n\t\t\tstd::cout << ' ';\n\t\t}\n\t}\n\tstd::cout << '\\n';\n\treturn;\n}\n\ninline void print() { std::cout << '\\n'; }\ntemplate <class T>\ninline void print(T x) {\n\tstd::cout << x << '\\n';\n\treturn;\n}\ntemplate <typename Head, typename... Tail>\nvoid print(Head H, Tail... T) {\n\tstd::cout << H << \" \";\n\tprint(T...);\n}\n\ntemplate <class T>\nvoid add(std::vector<T> &v, T val) {\n\tfor(auto &a : v) a += val;\n\treturn;\n}\n\ntemplate <class T>\nT dup(T a, T b) {\n\tassert(b != 0);\n\treturn (a + b - 1) / b;\n}\n\ntemplate <class T>\nT greatest_lower_multiple(T x, T d) {\n\tif(d == 0) return 0;\n\tif(d < 0) d *= -1;\n\tT y = x % d;\n\tif(y < 0) y += d;\n\treturn x - y;\n}\n\ntemplate <class T>\nT least_upper_multiple(T x, T d) {\n\treturn -greatest_lower_multiple(-x, d);\n}\n\nlong long POW(long long a, long long n) {\n\tlong long res = 1;\n\twhile(n > 0) {\n\t\tif(n & 1) res = res * a;\n\t\ta = a * a;\n\t\tn >>= 1;\n\t}\n\treturn res;\n}\n\nlong long modpow(long long a, long long n, long long mod) {\t // a^n mod\n\tassert(n >= 0 && mod);\n\tif(mod == 1) return 0LL;\n\tlong long res = 1;\n\ta %= mod;\n\twhile(n > 0) {\n\t\tif(n & 1) res = res * a % mod;\n\t\ta = a * a % mod;\n\t\tn >>= 1;\n\t}\n\treturn res < 0 ? res + mod : res;\n}\n\n// return x which satisfies a * x % mod == gcd(a, mod)\n// not (mod | a)\nlong long modinv(long long a, long long mod) {\n\tlong long b = mod, u = 1, v = 0;\n\twhile(b) {\n\t\tlong long t = a / b;\n\t\ta -= t * b;\n\t\tstd::swap(a, b);\n\t\tu -= t * v;\n\t\tstd::swap(u, v);\n\t}\n\tu %= mod;\n\tif(u < 0) u += mod;\n\treturn u;\n}\n\ntemplate <class T>\nint lb(const std::vector<T> &a, const T x) {\n\treturn std::distance(a.begin(), std::lower_bound(a.begin(), a.end(), x));\n}\ntemplate <class T>\nint ub(const std::vector<T> &a, const T x) {\n\treturn std::distance(a.begin(), std::upper_bound(a.begin(), a.end(), x));\n}\ntemplate <class T>\nvoid unq_sort(std::vector<T> &a) {\n\tstd::sort(a.begin(), a.end());\n\ta.erase(std::unique(a.begin(), a.end()), a.end());\n}\ntemplate <class T>\nstd::vector<int> press(std::vector<T> &a) {\n\tauto vec = a;\n\tunq_sort(vec);\n\tstd::vector<int> ret;\n\tfor(auto &v : a) ret.push_back(lb(vec, v));\n\treturn ret;\n}\n\n#pragma endregion\n\n#pragma region input\n#define VEC(type, name, size) \\\n\tvector<type> name(size); \\\n\tscanner::INPUT(name)\n#define VVEC(type, name, h, w) \\\n\tvector<vector<type>> name(h, vector<type>(w)); \\\n\tscanner::INPUT(name)\n#define INT(...) \\\n\tint __VA_ARGS__; \\\n\tscanner::INPUT(__VA_ARGS__)\n#define LL(...) \\\n\tlong long __VA_ARGS__; \\\n\tscanner::INPUT(__VA_ARGS__)\n#define STR(...) \\\n\tstring __VA_ARGS__; \\\n\tscanner::INPUT(__VA_ARGS__)\n#define CHR(...) \\\n\tchar __VA_ARGS__; \\\n\tscanner::INPUT(__VA_ARGS__)\n#define DBL(...) \\\n\tdouble __VA_ARGS__; \\\n\tscanner::INPUT(__VA_ARGS__)\n#define LD(...) \\\n\tlong double __VA_ARGS__; \\\n\tscanner::INPUT(__VA_ARGS__)\n#define TPL3(type0, type1, type2, name) \\\n\tstd::tuple<type0, type1, type2> name; \\\n\tscanner::INPUT(name);\n#define TPL4(type0, type1, type2, type3, name) \\\n\tstd::tuple<type0, type1, type2, type3> name; \\\n\tscanner::INPUT(name);\n\nnamespace scanner {\ntemplate <class T>\nvoid scan(T &a) {\n\tstd::cin >> a;\n}\n\ntemplate <class T, class L>\nvoid scan(std::pair<T, L> &p) {\n\tscan(p.first);\n\tscan(p.second);\n}\n\ntemplate <class T0, class T1, class T2>\nvoid scan(std::tuple<T0, T1, T2> &p) {\n\tT0 t0;\n\tT1 t1;\n\tT2 t2;\n\tscan(t0);\n\tscan(t1);\n\tscan(t2);\n\tp = std::make_tuple(t0, t1, t2);\n}\n\ntemplate <class T0, class T1, class T2, class T3>\nvoid scan(std::tuple<T0, T1, T2, T3> &p) {\n\tT0 t0;\n\tT1 t1;\n\tT2 t2;\n\tT3 t3;\n\tscan(t0);\n\tscan(t1);\n\tscan(t2);\n\tscan(t3);\n\tp = std::make_tuple(t0, t1, t2, t3);\n}\n\ntemplate <class T>\nvoid scan(std::vector<T> &a) {\n\tfor(auto &i : a) scan(i);\n}\n\nvoid INPUT() {}\ntemplate <class Head, class... Tail>\nvoid INPUT(Head &head, Tail &... tail) {\n\tscan(head);\n\tINPUT(tail...);\n}\n} // namespace scanner\n\ntemplate <typename T1, typename T2>\nstd::istream &operator>>(std::istream &is, std::pair<T1, T2> &p) {\n\tis >> p.first >> p.second;\n\treturn is;\n}\n#pragma endregion\n\n#pragma region debug\n\n#pragma region output\ntemplate <typename T1, typename T2>\nstd::ostream &std::operator<<(std::ostream &os, const std::pair<T1, T2> &p) {\n\tos << p.first << \" \" << p.second;\n\treturn os;\n}\ntemplate <class T>\nstd::ostream &std::operator<<(std::ostream &os, const std::vector<T> &v) {\n\tfor(int i = 0; i < (int)v.size(); i++) {\n\t\tif(i) os << \" \";\n\t\tos << v[i];\n\t}\n\treturn os;\n}\n#pragma endregion\n\n#pragma region view\n\nnamespace viewer {\nvoid view(const long long e) {\n\tif(e == INF)\n\t\tstd::cerr << \"INF\";\n\telse if(e == -INF)\n\t\tstd::cerr << \"-INF\";\n\telse\n\t\tstd::cerr << e;\n}\n\nvoid view(const int e) {\n\tif(e == inf)\n\t\tstd::cerr << \"inf\";\n\telse if(e == -inf)\n\t\tstd::cerr << \"-inf\";\n\telse\n\t\tstd::cerr << e;\n}\n\ntemplate <typename T>\nvoid view(const T e) {\n\tstd::cerr << e;\n}\n\ntemplate <typename T, typename U>\nvoid view(const std::pair<T, U> &p) {\n\tstd::cerr << \"(\";\n\tview(p.first);\n\tstd::cerr << \", \";\n\tview(p.second);\n\tstd::cerr << \")\";\n}\n\ntemplate <class T0, class T1, class T2>\nvoid view(const std::tuple<T0, T1, T2> &p) {\n\tstd::cerr << \"(\";\n\tview(std::get<0>(p));\n\tstd::cerr << \", \";\n\tview(std::get<1>(p));\n\tstd::cerr << \", \";\n\tview(std::get<2>(p));\n\tstd::cerr << \")\";\n}\n\ntemplate <class T0, class T1, class T2, class T3>\nvoid view(const std::tuple<T0, T1, T2, T3> &p) {\n\tstd::cerr << \"(\";\n\tview(std::get<0>(p));\n\tstd::cerr << \", \";\n\tview(std::get<1>(p));\n\tstd::cerr << \", \";\n\tview(std::get<2>(p));\n\tstd::cerr << \", \";\n\tview(std::get<3>(p));\n\tstd::cerr << \")\";\n}\n\ntemplate <typename T>\nvoid view(const std::set<T> &s) {\n\tif(s.empty()) {\n\t\tstd::cerr << \"{ }\";\n\t\treturn;\n\t}\n\tstd::cerr << \"{ \";\n\tfor(auto &t : s) {\n\t\tview(t);\n\t\tstd::cerr << \", \";\n\t}\n\tstd::cerr << \"\\b\\b }\";\n}\n\ntemplate <typename T>\nvoid view(const std::unordered_set<T> &s) {\n\tif(s.empty()) {\n\t\tstd::cerr << \"{ }\";\n\t\treturn;\n\t}\n\tstd::cerr << \"{ \";\n\tfor(auto &t : s) {\n\t\tview(t);\n\t\tstd::cerr << \", \";\n\t}\n\tstd::cerr << \"\\b\\b }\";\n}\n\ntemplate <typename T>\nvoid view(const std::vector<T> &v) {\n\tif(v.empty()) {\n\t\tstd::cerr << \"{ }\";\n\t\treturn;\n\t}\n\tstd::cerr << \"{ \";\n\tfor(const auto &e : v) {\n\t\tview(e);\n\t\tstd::cerr << \", \";\n\t}\n\tstd::cerr << \"\\b\\b }\";\n}\n\ntemplate <typename T>\nvoid view(const std::vector<std::vector<T>> &vv) {\n\tstd::cerr << \"{\\n\";\n\tfor(const auto &v : vv) {\n\t\tstd::cerr << \"\\t\";\n\t\tview(v);\n\t\tstd::cerr << '\\n';\n\t}\n\tstd::cerr << \"}\";\n}\n\ntemplate <typename T, typename U>\nvoid view(const std::vector<std::pair<T, U>> &v) {\n\tstd::cerr << \"{\\n\";\n\tfor(const auto &c : v) {\n\t\tstd::cerr << \"\\t(\";\n\t\tview(c.first);\n\t\tstd::cerr << \", \";\n\t\tview(c.second);\n\t\tstd::cerr << \")\\n\";\n\t}\n\tstd::cerr << \"}\";\n}\n\ntemplate <class T0, class T1, class T2>\nvoid view(const std::vector<std::tuple<T0, T1, T2>> &v) {\n\tif(v.empty()) {\n\t\tstd::cerr << \"{ }\";\n\t\treturn;\n\t}\n\tstd::cerr << '{';\n\tfor(const auto &t : v) {\n\t\tstd::cerr << \"\\n\\t\";\n\t\tview(t);\n\t\tstd::cerr << \",\";\n\t}\n\tstd::cerr << \"\\n}\";\n}\n\ntemplate <class T0, class T1, class T2, class T3>\nvoid view(const std::vector<std::tuple<T0, T1, T2, T3>> &v) {\n\tif(v.empty()) {\n\t\tstd::cerr << \"{ }\";\n\t\treturn;\n\t}\n\tstd::cerr << '{';\n\tfor(const auto &t : v) {\n\t\tstd::cerr << \"\\n\\t\";\n\t\tview(t);\n\t\tstd::cerr << \",\";\n\t}\n\tstd::cerr << \"\\n}\";\n}\n\ntemplate <typename T, typename U>\nvoid view(const std::map<T, U> &m) {\n\tstd::cerr << \"{\\n\";\n\tfor(const auto &t : m) {\n\t\tstd::cerr << \"\\t[\";\n\t\tview(t.first);\n\t\tstd::cerr << \"] : \";\n\t\tview(t.second);\n\t\tstd::cerr << '\\n';\n\t}\n\tstd::cerr << \"}\";\n}\n\ntemplate <typename T, typename U>\nvoid view(const std::unordered_map<T, U> &m) {\n\tstd::cerr << \"{\\n\";\n\tfor(const auto &t : m) {\n\t\tstd::cerr << \"\\t[\";\n\t\tview(t.first);\n\t\tstd::cerr << \"] : \";\n\t\tview(t.second);\n\t\tstd::cerr << '\\n';\n\t}\n\tstd::cerr << \"}\";\n}\n} // namespace viewer\n\n#pragma endregion\n\n// when debugging : g++ foo.cpp -DLOCAL\n#ifdef LOCAL\nvoid debug_out() {}\ntemplate <typename Head, typename... Tail>\nvoid debug_out(Head H, Tail... T) {\n\tviewer::view(H);\n\tstd::cerr << \", \";\n\tdebug_out(T...);\n}\n#define debug(...) \\\n\tdo { \\\n\t\tstd::cerr << __LINE__ << \" [\" << #__VA_ARGS__ << \"] : [\"; \\\n\t\tdebug_out(__VA_ARGS__); \\\n\t\tstd::cerr << \"\\b\\b]\\n\"; \\\n\t} while(0)\n#define dump(x) \\\n\tdo { \\\n\t\tstd::cerr << __LINE__ << \" \" << #x << \" : \"; \\\n\t\tviewer::view(x); \\\n\t\tstd::cerr << '\\n'; \\\n\t} while(0)\n\n#else\n#define debug(...) (void(0))\n#define dump(x) (void(0))\n#endif\n\n#pragma endregion\n\n#pragma region fenwick_tree\n\ntemplate <class T = long long>\nstruct binary_indexed_tree {\n\tstd::vector<T> data;\n\tint n;\n\tint pw2 = 1;\n\tbinary_indexed_tree(const int &sz) : data(sz + 1, 0), n(sz) {\n\t\twhile(pw2 << 1 <= n) pw2 <<= 1;\n\t}\n\tvoid add(const int &place, const T &val, const int &index = 0) {\n\t\tfor(int x = place + 1 - index; x <= n; x += x & -x) data[x] += val;\n\t}\n\n\t// [0, a)\n\tconst T sum0(const int &a, const int &index = 0) const {\n\t\tT ret = 0;\n\t\tfor(int x = a - index; x > 0; x -= x & -x) ret += data[x];\n\t\treturn ret;\n\t}\n\n\t// [0, b)\n\tconst T sum(const int &a, const int &b, const int &index = 0) const { return sum0(b, index) - sum0(a, index); }\n\n\t// point get\n\tconst T get(const int &idx) const { return sum(idx, idx + 1, 0); }\n\tconst T operator[](const int &idx) const { return get(idx); }\n\n\tvoid update(const int &place, const T &val, const int &index = 0) {\n\t\tadd(place, val + sum0(place, index) - sum0(place + 1, index), index);\n\t}\n\n\t/*\n\t\tconstraints: for all $i, data[i] >= 0\n\t\treturns $i which satisfies:\n\t\t\tstart <= i,\n\t\t\tsum[start, i-1) < val <= \\sum_[start, i)\n\t\tsum[start, n+1) := infinity, sum[start, start-1] := -infinity\n\t*/\n\tconst int lower_bound(T val, const int &start = 0) const {\n\t\tif(val <= 0) return start;\n\t\tval += sum0(start);\n\t\tint x = 0;\n\t\tfor(int k = pw2; k > 0; k >>= 1) {\n\t\t\tif(x + k <= n && data[x + k] < val) {\n\t\t\t\tval -= data[x + k];\n\t\t\t\tx += k;\n\t\t\t}\n\t\t}\n\t\treturn x + 1;\n\t}\n\n\t/*\n\t\tconstraints: for all $i, data[i] >= 0\n\t\treturns $i which satisfies:\n\t\t\tstart <= i,\n\t\t\tsum[start, i-1) <= val < \\sum_[start, i) .\n\t\tsum[start, n+1) := infinity, sum[start, start-1] := -infinity\n\t*/\n\tconst int upper_bound(T val, const int &start = 0) const {\n\t\tif(val < 0) return start;\n\t\tval += sum0(start);\n\t\tint x = 0;\n\t\tfor(int k = pw2; k > 0; k >>= 1) {\n\t\t\tif(x + k <= n && data[x + k] <= val) {\n\t\t\t\tval -= data[x + k];\n\t\t\t\tx += k;\n\t\t\t}\n\t\t}\n\t\treturn x + 1;\n\t}\n\n\tvoid print() const {\n#ifdef LOCAL\n\t\tstd::cerr << \"{ \";\n\t\tfor(int i = 0; i < n; i++) {\n\t\t\tstd::cerr << get(i) << (i == n - 1 ? \" }\\n\" : \", \");\n\t\t}\n#endif\n\t}\n};\n\n#pragma endregion\n\nint solve(int w) {\n\tint n;\n\tcin >> n;\n\tVEC(int, lens, n);\n\t// binary_indexed_tree<ll> len_sum(n);\n\t// rep(i, n) len_sum.add(i, ll(lens[i]));\n\n\tauto bs = [](int ok, int ng, auto is_ok) -> int {\n\t\twhile(abs(ok - ng) > 1) {\n\t\t\tint mid = (ok + ng) / 2;\n\t\t\t(is_ok(mid) ? ok : ng) = mid;\n\t\t}\n\t\treturn ok;\n\t};\n\n\tauto judge = [&](int maxspacelength) -> bool {\n\t\t// vector<int> dp(n);\n\t\t// dp.front() = 1;\n\t\t// auto len_minimum = [&](int lef, int rig) -> ll {\n\t\t// \tassert(lef-\\\\ \\\\< rig);\n\t\t// \treturn len_sum.s\\um(lef, rig, 0) + ll(rig - lef - 1);\n\t\t// };\n\t\t// auto len_maximum = [&](int lef, int rig) {\n\t\t// \tassert(lef < rig);\n\t\t// \treturn len_sum.sum(lef, rig) + ll(rig - lef - 1) * maxspacelength;\n\t\t// };\n\n\t\tbinary_indexed_tree<int> dp(n);\n\t\tdp.add(0, 1);\n\t\tint nxt = 1;\n\t\t// auto traceable = [&](int from) {\n\t\t// \tll maxlen = len_maximum(from, nxt);\n\t\t// \treturn maxlen >= ll(w);\n\t\t// };\n\t\t// auto packable = [&](int from) {\n\t\t// \tll minilen = len_minimum(from, nxt);\n\t\t// \treturn minilen <= ll(w);\n\t\t// };\n\n\t\tint left = 0;\n\t\tint right = 0;\n\t\t// left = right = lens.front();\n\t\tll minlensum = -1;\n\t\tll maxlensum = -maxspacelength;\n\t\tint sum = 0;\n\t\tfor(; nxt <= n; nxt++) {\n\t\t\tdebug(left, right, nxt, maxspacelength);\n\t\t\tminlensum += 1 + lens[nxt - 1];\n\t\t\tmaxlensum += maxspacelength + lens[nxt - 1];\n\t\t\tdebug(minlensum, maxlensum);\n\t\t\twhile(minlensum > w) {\n\t\t\t\tdebug(left);\n\t\t\t\tminlensum -= lens[left] + 1;\n\t\t\t\t// sum -= dp[left++];\n\t\t\t\tleft++;\n\t\t\t}\n\t\t\tif(nxt == n) return dp.sum(left, n);\n\t\t\t// maxlensum += maxspacelength + lens[nxt];\n\t\t\twhile(maxlensum >= w && right + 1 < nxt) {\n\t\t\t\tdebug(right);\n\t\t\t\tmaxlensum -= maxspacelength + lens[right];\n\t\t\t\t// sum += dp[right++];\n\t\t\t\tright++;\n\t\t\t}\n\n\t\t\tdebug(left, right);\n\n\t\t\t// if(nxt == n - 1) break;\n\t\t\t// int left = bs(nxt - 1, -1, packable);\n\t\t\t// int right = bs(-1, nxt, traceable);\n\t\t\t// if(right == nxt - 1) right--;\n\t\t\tif(right < left) continue;\n\t\t\t// int righttemp = right;\n\t\t\t// if(right == nxt ) righttemp--;\n\t\t\tsum = dp.sum(left, right);\n\t\t\tif(sum > 0) dp.add(nxt, 1);\n\t\t}\n\t\t// left = bs(n, -1, packable);\n\t\treturn bool(dp.sum(left, n));\n\t};\n\n\treturn bs(w + 10, 0, judge);\n}\n\nint main() {\n\tstd::ios::sync_with_stdio(false);\n\tstd::cin.tie(nullptr);\n\tstd::cout << std::fixed << std::setprecision(15);\n\tsrand((unsigned)time(NULL));\n\n\tint w;\n\twhile(cin >> w && w) {\n\t\tcout << solve(w) << dl;\n\t}\n\n\treturn 0;\n}", "accuracy": 1, "time_ms": 1600, "memory_kb": 3620, "score_of_the_acc": -0.2195, "final_rank": 4 }, { "submission_id": "aoj_1333_6085440", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\n#define rep(i,a,b) for(int i=(int)(a);i<(int)(b);i++)\n#define ALL(v) (v).begin(),(v).end()\nusing ll=long long int;\nconst int inf = 0x3fffffff; const ll INF = 0x1fffffffffffffff; const double eps=1e-12;\ntemplate<typename T>inline bool chmax(T& a,T b){if(a<b){a=b;return 1;}return 0;}\ntemplate<typename T>inline bool chmin(T& a,T b){if(a>b){a=b;return 1;}return 0;}\n\n\n\nint main(){\n int W,n;\n while(cin>>W>>n){\n if(n==0)break;\n vector<int> a(n);\n rep(i,0,n)cin>>a[i];\n vector<int> rui(n+1);\n rep(i,0,n)rui[i+1]=rui[i]+a[i];\n\n auto ok1=[&](int L,int R)->bool{\n return rui[R+1]-rui[L]+R-L<=W;\n };\n auto ok2=[&](int L,int R,int x)->bool{\n return rui[R+1]-rui[L]+(R-L)*x>=W;\n };\n auto check=[&](int x)->bool{\n if(ok1(0,n-1))return true;\n vector<int> dp(n+1);\n dp[0]=1;\n int sum=0,ok=0,ng=0;\n rep(i,0,n){\n while(ng<=i and !ok1(ng,i)){\n if(dp[ng])sum--;\n ng++;\n }\n while(ok<=i and ok2(ok,i,x)){\n if(dp[ok])sum++;\n ok++;\n }\n if(sum){\n dp[i+1]=1;\n if(ok1(i+1,n-1))return true;\n }\n }\n return false;\n };\n\n int L=0,R=W;\n while(R-L>1){\n int mid=(L+R)>>1;\n if(check(mid))R=mid;\n else L=mid;\n }\n cout<<R<<'\\n';\n }\n return 0;\n}", "accuracy": 1, "time_ms": 830, "memory_kb": 3664, "score_of_the_acc": -0.129, "final_rank": 1 }, { "submission_id": "aoj_1333_6066013", "code_snippet": "#include <bits/stdc++.h>\n\nusing namespace std;\n\n#define eps 1e-9\n#define INF 2000000000 // 2e9\n#define LLINF 2000000000000000000ll // 2e18 (llmax:9e18)\n#define all(x) (x).begin(), (x).end()\n#define sq(x) ((x) * (x))\n\n#define rep(i, x) for (int i = 0; i < (int)(x); i++)\n#define drep(i, x) for (int i = ((int)(x)-1); i >= 0; i--)\n\n#define popcount __builtin_popcount\n#define next __next\n#define prev __prev\n\n#ifndef LOCAL\n#define dmp(...) ;\n#else\n#define dmp(...) \\\n cerr << \"[ \" << #__VA_ARGS__ << \" ] : \" << dump_str(__VA_ARGS__) << endl\n#endif\n\n// ---------------- Utility ------------------\n\ntemplate <class T>\nbool chmin(T &a, const T &b) {\n if (a <= b) return false;\n a = b;\n return true;\n}\n\ntemplate <class T>\nbool chmax(T &a, const T &b) {\n if (a >= b) return false;\n a = b;\n return true;\n}\n\ntemplate <class T>\nusing MaxHeap = priority_queue<T>;\n\ntemplate <class T>\nusing MinHeap = priority_queue<T, vector<T>, greater<T>>;\n\ntemplate <class T>\nvector<T> vect(int len, T elem) {\n return vector<T>(len, elem);\n}\n\n// ----------------- Input -------------------\n\ntemplate <class T, class U>\nistream &operator>>(istream &is, pair<T, U> &p) {\n return is >> p.first >> p.second;\n}\n\ntemplate <class T>\nistream &operator>>(istream &is, vector<T> &vec) {\n for (int i = 0; i < vec.size(); i++) is >> vec[i];\n return is;\n}\n\n// ----------------- Output ------------------\n\ntemplate <class T, class U>\nostream &operator<<(ostream &os, const pair<T, U> &p) {\n return os << p.first << ',' << p.second;\n}\n\ntemplate <class T>\nostream &operator<<(ostream &os, const vector<T> &v) {\n for (const T &e : v) os << e << \" \";\n return os;\n}\n\ntemplate <class T>\nostream &operator<<(ostream &os, const deque<T> &d) {\n for (const T &e : d) os << e << \" \";\n return os;\n}\n\ntemplate <class T>\nostream &operator<<(ostream &os, const set<T> &s) {\n os << \"{ \";\n for (const T &e : s) os << e << \" \";\n return os << \"}\";\n}\n\ntemplate <class T, class U>\nostream &operator<<(ostream &os, const map<T, U> &m) {\n os << \"{ \";\n for (const auto &[key, val] : m) os << \"( \" << key << \" -> \" << val << \" ) \";\n return os << \"}\";\n}\n\ntemplate <class TupleTy, size_t... I>\nvoid dump_tuple(ostream &os, const TupleTy t, std::index_sequence<I...>) {\n (..., (os << (I == 0 ? \"\" : \",\") << std::get<I>(t)));\n}\n\ntemplate <class... Args>\nostream &operator<<(ostream &os, const tuple<Args...> &t) {\n os << \"(\";\n dump_tuple(os, t, std::make_index_sequence<sizeof...(Args)>());\n return os << \")\";\n}\n\nvoid dump_str_rec(ostringstream &) {}\n\ntemplate <class Head, class... Tail>\nvoid dump_str_rec(ostringstream &oss, Head &&head, Tail &&... tail) {\n oss << \", \" << head;\n dump_str_rec(oss, forward<Tail>(tail)...);\n}\n\ntemplate <class T, class... U>\nstring dump_str(T &&arg, U &&... args) {\n ostringstream oss;\n oss << arg;\n dump_str_rec(oss, forward<U>(args)...);\n return oss.str();\n}\n\n// --------------- Fast I/O ------------------\n\nvoid fastio() {\n cin.tie(0);\n ios::sync_with_stdio(0);\n cout << fixed << setprecision(20);\n}\n\n// ------------------ ACL --------------------\n\n// #include <atcoder/modint>\n// constexpr int MOD = 998244353;\n// constexpr int MOD = 1000000007;\n// using mint = atcoder::static_modint<MOD>;\n// ostream &operator<<(ostream &os, const mint &v) {\n// return os << v.val();\n// }\n\n// ------------ End of template --------------\n\n#define endl \"\\n\"\nusing ll = long long;\nusing pii = pair<int, int>;\n\nconst bool multitest = false;\n\ntemplate <class T>\nstruct BIT {\n int N;\n vector<T> bit;\n BIT(int N) : N(N) { bit = vector<T>(N + 1, T(0)); }\n void add(int i, T x) {\n i++;\n while (i <= N) {\n bit[i] += x;\n i += i & -i;\n }\n return;\n }\n T sum(int i) {\n i++;\n T res = T(0);\n while (i > 0) {\n res += bit[i];\n i -= i & -i;\n }\n return res;\n }\n T sum(int l, int r) { // [l,r]\n assert(l <= r);\n if (l == 0)\n return sum(r);\n else\n return sum(r) - sum(l - 1);\n }\n};\n\nbool solve() {\n ll W;\n int N;\n cin >> W >> N;\n if (W == 0 && N == 0) return false;\n vector<ll> x(N);\n cin >> x;\n\n vector<ll> y(N + 1);\n for (int i = 0; i < N; i++) y[i + 1] = y[i] + x[i];\n\n auto range = [&](ll a, ll b, int id, ll K) {\n int L, R;\n {\n int l = -1, r = id;\n while (r - l > 1) {\n int mid = (l + r) / 2;\n if (y[mid] + mid >= a)\n r = mid;\n else\n l = mid;\n }\n L = r;\n }\n {\n if (y[0] > b) return make_pair(1, 0);\n int l = 0, r = id;\n while (r - l > 1) {\n int mid = (l + r) / 2;\n if (y[mid] + K * mid <= b)\n l = mid;\n else\n r = mid;\n }\n R = l;\n }\n return make_pair(L, R);\n };\n\n auto check = [&](ll K) {\n BIT<int> bit(N + 1);\n bit.add(0, 1);\n for (int i = 1; i <= N; i++) {\n ll a = i - 1 + y[i] - W;\n ll b = K * (i - 1) + y[i] - W;\n auto [l, r] = range(a, b, i, K);\n if (l > r) continue;\n if (bit.sum(l, r) > 0) bit.add(i, 1);\n }\n for (int i = 0; i <= N; i++) {\n if (bit.sum(i, i) == 0) continue;\n if (y[N] - y[i] + (N - i - 1) * K <= W) return true;\n }\n return false;\n };\n\n ll l = 0, r = W;\n while (r - l > 1) {\n ll mid = (l + r) / 2ll;\n if (check(mid))\n r = mid;\n else\n l = mid;\n }\n cout << r << endl;\n return true;\n}\n\nint main() {\n fastio();\n if (!multitest) {\n while (solve()) {}\n } else {\n cerr << \"[Warning] Multi testcase mode on\" << endl;\n int t;\n cin >> t;\n while (t--) solve();\n }\n return 0;\n}", "accuracy": 1, "time_ms": 6070, "memory_kb": 4192, "score_of_the_acc": -1.0538, "final_rank": 17 }, { "submission_id": "aoj_1333_5896208", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\n\nint main() {\n ll W, N;\n while (cin >> W >> N, W) {\n W++;\n vector<ll> X(N);\n for (auto&& x : X) {\n cin >> x;\n x++;\n }\n vector<ll> acc(N + 1);\n for (int i = 0; i < N; i++) {\n acc[i + 1] = acc[i] + X[i];\n }\n\n vector<ll> accwithspace(N + 1);\n vector<int> imos(N + 1);\n auto check = [&](ll maxspace) {\n accwithspace[0] = 0;\n for (int i = 0; i < N; i++) {\n accwithspace[i + 1] = accwithspace[i] + X[i] + maxspace;\n }\n imos[0] = 0;\n for (int i = 0; i < N; i++) {\n bool canbefront = false;\n if (i == 0)\n canbefront = true;\n else if (i == 1)\n canbefront = false;\n else {\n int l, r;\n l = lower_bound(cbegin(acc), cend(acc), acc[i] - W) - acc.begin();\n r = lower_bound(cbegin(accwithspace), cend(accwithspace), accwithspace[i] - maxspace - W + 1) - accwithspace.begin();\n canbefront = imos[r] - imos[l];\n }\n imos[i + 1] = imos[i] + canbefront;\n if (canbefront && ((acc.back() - acc[i]) <= W))\n return true;\n }\n return false;\n };\n\n int ok = W;\n int ng = -1;\n while (abs(ok - ng) > 1) {\n int mid = (ok + ng) / 2;\n if (check(mid)) {\n ok = mid;\n } else {\n ng = mid;\n }\n }\n\n cout << ok + 1 << endl;\n }\n}", "accuracy": 1, "time_ms": 3490, "memory_kb": 4328, "score_of_the_acc": -0.7462, "final_rank": 13 }, { "submission_id": "aoj_1333_5896195", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\n\nint main() {\n ll W, N;\n while (cin >> W >> N, W) {\n W++;\n vector<ll> X(N);\n for (auto&& x : X) {\n cin >> x;\n x++;\n }\n vector<ll> acc(N + 1);\n for (int i = 0; i < N; i++) {\n acc[i + 1] = acc[i] + X[i];\n }\n\n auto check = [&](ll maxspace) {\n vector<ll> accwithspace(N + 1);\n for (int i = 0; i < N; i++) {\n accwithspace[i + 1] = accwithspace[i] + X[i] + maxspace;\n }\n vector<int> imos(N + 1);\n for (int i = 0; i < N; i++) {\n bool canbefront = false;\n if (i == 0)\n canbefront = true;\n else if (i == 1)\n canbefront = false;\n else {\n int l, r;\n l = lower_bound(cbegin(acc), cend(acc), acc[i] - W) - acc.begin();\n r = lower_bound(cbegin(accwithspace), cend(accwithspace), accwithspace[i] - maxspace - W + 1) - accwithspace.begin();\n canbefront = imos[r] - imos[l];\n }\n imos[i + 1] = imos[i] + canbefront;\n if (canbefront && ((acc.back() - acc[i]) <= W))\n return true;\n }\n return false;\n };\n\n int ok = W;\n int ng = -1;\n while (abs(ok - ng) > 1) {\n int mid = (ok + ng) / 2;\n if (check(mid)) {\n ok = mid;\n } else {\n ng = mid;\n }\n }\n\n cout << ok + 1 << endl;\n }\n}", "accuracy": 1, "time_ms": 3490, "memory_kb": 4540, "score_of_the_acc": -0.8253, "final_rank": 15 }, { "submission_id": "aoj_1333_5358559", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nint main(){\n while (true){\n int W, N;\n cin >> W >> N;\n if (W == 0 && N == 0){\n break;\n }\n vector<int> x(N);\n for (int i = 0; i < N; i++){\n cin >> x[i];\n }\n vector<int> S(N + 1);\n S[0] = 0;\n for (int i = 0; i < N; i++){\n S[i + 1] = S[i] + x[i];\n }\n int tv = W - 2, fv = 0;\n while (tv - fv > 1){\n \tint mid = (tv + fv) / 2;\n vector<bool> dp(N + 1, false);\n dp[0] = true;\n vector<int> dpS(N + 2, 0);\n dpS[1] = 1;\n for (int i = 0; i < N; i++){\n int tv1 = i, fv1 = -1;\n while (tv1 - fv1 > 1){\n int mid1 = (tv1 + fv1) / 2;\n if (S[i + 1] - S[mid1] + (i - mid1) <= W){\n tv1 = mid1;\n } else {\n fv1 = mid1;\n }\n }\n int tv2 = tv1 - 1, fv2 = i;\n while (fv2 - tv2 > 1){\n int mid2 = (tv2 + fv2) / 2;\n int r = W - (S[i + 1] - S[mid2]);\n if (r <= mid * (i - mid2)){\n tv2 = mid2;\n } else {\n fv2 = mid2;\n }\n }\n if (dpS[tv2 + 1] - dpS[tv1] > 0){\n dp[i + 1] = true;\n }\n dpS[i + 2] = dpS[i + 1];\n if (dp[i + 1]){\n dpS[i + 2]++;\n }\n }\n bool ok = false;\n for (int i = 0; i <= N; i++){\n if (dp[i] && S[N] - S[i] + (N - i - 1) <= W){\n ok = true;\n }\n }\n if (ok){\n tv = mid;\n } else {\n fv = mid;\n }\n }\n cout << tv << endl;\n }\n}", "accuracy": 1, "time_ms": 6250, "memory_kb": 3456, "score_of_the_acc": -0.8042, "final_rank": 14 }, { "submission_id": "aoj_1333_5310475", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\ntypedef long long ll;\ntemplate<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return true; } return false; }\ntemplate<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return true; } return false; }\n#define all(x) (x).begin(),(x).end()\n#define fi first\n#define se second\n#define mp make_pair\n#define si(x) int(x.size())\nconst int mod=998244353,MAX=80005,INF=1<<30;\nll dp[MAX];\n\nint main(){\n \n std::ifstream in(\"text.txt\");\n std::cin.rdbuf(in.rdbuf());\n cin.tie(0);\n ios::sync_with_stdio(false);\n \n while(1){\n ll W,N;cin>>W>>N;\n if(W==0) break;\n vector<ll> A(N+1),rui(N+2);\n for(int i=0;i<N;i++){\n cin>>A[i+1];\n rui[i+1]=rui[i]+A[i+1];\n }\n rui[N+1]=1LL<<60;\n \n ll left=0,right=W;\n while(right-left>1){\n ll mid=(left+right)/2;\n memset(dp,0,sizeof(dp));\n dp[0]=1;\n dp[1]=-1;\n \n for(int i=0;i<=N;i++){\n if(i) dp[i]+=dp[i-1];\n if(dp[i]==0) continue;\n if(i==N) break;\n \n if(rui[N]-rui[i]+N-i-1<=W){\n dp[N]=1;\n break;\n }\n int l=i+2,r=N+1;\n while(r-l>1){\n int m=(l+r)/2;\n if(rui[m]-rui[i]+m-i-1<=W) l=m;\n else r=m;\n }\n \n int l2=i+1,r2=N+1;\n while(r2-l2>1){\n int m2=(l2+r2)/2;\n if(rui[m2]-rui[i]+mid*(m2-i-1)>=W) r2=m2;\n else l2=m2;\n }\n \n if(r2<=l){\n dp[r2]++;\n dp[l+1]--;\n }\n }\n \n if(dp[N]) right=mid;\n else left=mid;\n }\n cout<<right<<endl;\n }\n}", "accuracy": 1, "time_ms": 3890, "memory_kb": 4408, "score_of_the_acc": -0.8316, "final_rank": 16 }, { "submission_id": "aoj_1333_5268618", "code_snippet": "#include <iostream>\n#include <numeric>\n#include <vector>\n\nusing namespace std;\nusing lint = long long;\n\nbool solve() {\n lint w;\n int n;\n cin >> w >> n;\n if (w == 0) return false;\n\n vector<lint> xs(n);\n for (auto& x : xs) cin >> x;\n xs.insert(xs.begin(), 0);\n partial_sum(xs.begin(), xs.end(), xs.begin());\n\n lint ok = w, ng = 0;\n while (ok - ng > 1) {\n lint mid = (ok + ng) / 2;\n vector<int> nxt(n + 1, n);\n\n for (int l = n - 1; l >= 0; --l) {\n nxt[l] = nxt[l + 1];\n\n if ((xs[n] - xs[l]) + (n - l - 1) <= w) {\n nxt[l] = l;\n continue;\n }\n\n int rng = l, rok = n + 1;\n while (rok - rng > 1) {\n int r = (rok + rng) / 2;\n\n if ((xs[r] - xs[l]) + (r - l - 1) * mid >= w) {\n rok = r;\n } else {\n rng = r;\n }\n }\n\n int r = nxt[rok];\n if ((xs[r] - xs[l]) + (r - l - 1) <= w) nxt[l] = l;\n }\n\n if (nxt[0] == 0) {\n ok = mid;\n } else {\n ng = mid;\n }\n }\n\n cout << ok << \"\\n\";\n return true;\n}\n\nint main() {\n cin.tie(nullptr);\n ios::sync_with_stdio(false);\n\n while (solve()) {}\n\n return 0;\n}", "accuracy": 1, "time_ms": 2660, "memory_kb": 4188, "score_of_the_acc": -0.5787, "final_rank": 8 }, { "submission_id": "aoj_1333_4954914", "code_snippet": "#include <bits/stdc++.h>\n\nusing namespace std;\n\n#define REP(i, n) for (int i=0; i<(n); ++i)\n#define RREP(i, n) for (int i=(int)(n)-1; i>=0; --i)\n#define FOR(i, a, n) for (int i=(a); i<(n); ++i)\n#define RFOR(i, a, n) for (int i=(int)(n)-1; i>=(a); --i)\n\n#define SZ(x) ((int)(x).size())\n#define ALL(x) (x).begin(),(x).end()\n\n#define DUMP(x) cerr<<#x<<\" = \"<<(x)<<endl\n#define DEBUG(x) cerr<<#x<<\" = \"<<(x)<<\" (L\"<<__LINE__<<\")\"<<endl;\n\ntemplate<class T>\nostream &operator<<(ostream &os, const vector <T> &v) {\n os << \"[\";\n REP(i, SZ(v)) {\n if (i) os << \", \";\n os << v[i];\n }\n return os << \"]\";\n}\n\ntemplate<class T, class U>\nostream &operator<<(ostream &os, const pair <T, U> &p) {\n return os << \"(\" << p.first << \" \" << p.second << \")\";\n}\n\ntemplate<class T>\nbool chmax(T &a, const T &b) {\n if (a < b) {\n a = b;\n return true;\n }\n return false;\n}\n\ntemplate<class T>\nbool chmin(T &a, const T &b) {\n if (b < a) {\n a = b;\n return true;\n }\n return false;\n}\n\nusing ll = long long;\nusing ull = unsigned long long;\nusing ld = long double;\nusing P = pair<int, int>;\nusing vi = vector<int>;\nusing vll = vector<ll>;\nusing vvi = vector<vi>;\nusing vvll = vector<vll>;\n\nconst ll MOD = 1e9 + 7;\nconst int INF = INT_MAX / 2;\nconst ll LINF = LLONG_MAX / 2;\nconst ld eps = 1e-9;\n\n\ntemplate<int m>\nstruct mint {\n int x;\n\n mint(int x = 0) : x(((x % m) + m) % m) {}\n\n mint operator-() { return x ? m - x : 0; }\n\n mint &operator+=(mint r) {\n if ((x += r.x) >= m) x -= m;\n return *this;\n }\n\n mint &operator-=(mint r) {\n if ((x -= r.x) < 0) x += m;\n return *this;\n }\n\n mint &operator*=(mint r) {\n x = ((ll) x * r.x) % m;\n return *this;\n }\n\n mint inv() {\n int a = x, b = m, u = 1, v = 0, t;\n while (b > 0) {\n t = a / b;\n swap(a -= t * b, b);\n swap(u -= t * v, v);\n }\n return u;\n }\n\n mint &operator/=(mint r) { return *this *= r.inv(); }\n\n friend mint operator+(mint l, mint r) { return l += r; }\n\n friend mint operator-(mint l, mint r) { return l -= r; }\n\n friend mint operator*(mint l, mint r) { return l *= r; }\n\n friend mint operator/(mint l, mint r) { return l /= r; }\n\n mint pow(ll n) {\n mint ret = 1, tmp = *this;\n while (n) {\n if (n & 1) ret *= tmp;\n tmp *= tmp, n >>= 1;\n }\n return ret;\n }\n\n friend bool operator==(mint l, mint r) { return l.x == r.x; }\n\n friend bool operator!=(mint l, mint r) { return l.x != r.x; }\n\n friend ostream &operator<<(ostream &os, mint a) {\n return os << a.x;\n }\n\n friend istream &operator>>(istream &is, mint &a) {\n int x;\n is >> x;\n a = x;\n return is;\n }\n};\n\n\ntemplate<typename T>\nstruct Combination {\n int _n = 1;\n vector<T> _fact{1}, _rfact{1};\n\n void extend(int n) {\n if (n <= _n) return;\n _fact.resize(n);\n _rfact.resize(n);\n for (int i = _n; i < n; ++i) _fact[i] = _fact[i - 1] * i;\n _rfact[n - 1] = 1 / _fact[n - 1];\n for (int i = n - 1; i > _n; --i) _rfact[i - 1] = _rfact[i] * i;\n _n = n;\n }\n\n T fact(int k) {\n extend(k + 1);\n return _fact.at(k);\n }\n\n T rfact(int k) {\n extend(k + 1);\n return _rfact.at(k);\n }\n\n T P(int n, int r) {\n if (r < 0 or n < r) return 0;\n return fact(n) * rfact(n - r);\n }\n\n T C(int n, int r) {\n if (r < 0 or n < r) return 0;\n return fact(n) * rfact(r) * rfact(n - r);\n }\n\n T H(int n, int r) {\n return (n == 0 and r == 0) ? 1 : C(n + r - 1, r);\n }\n};\n\n\n\nbool solve() {\n int w, n; cin >> w >> n;\n if (w == 0 and n == 0) return false;\n vi x(n); REP(i, n) cin >> x[i];\n\n vi xs(n+1);\n REP(i, n) xs[i+1] = xs[i] + x[i];\n\n vi dp1(n);\n {\n int r = 0, sum = 0;\n REP(l, n) {\n while (r < n and sum + x[r] + (r - l) <= w) sum += x[r++];\n dp1[l] = r;\n sum -= x[l];\n }\n }\n //DUMP(dp1);\n\n auto check = [&](int s) {\n // 隙間最大s で可能か?\n vi dp2(n, n);\n {\n int r = 0, sum = 0;\n REP(l, n) {\n while (r < n and sum + x[r] + (r - l) * s < w) sum += x[r++];\n if (r < n) dp2[l] = r;\n sum -= x[l];\n }\n }\n //DUMP(dp2);\n\n //{\n // vi dp(n + 1);\n // dp[0] = 1;\n // REP(i, n) {\n // if (dp[i] == 0) continue;\n // for (int j = dp2[i]; j < dp1[i]; ++j) {\n // dp[j + 1] = 1;\n // }\n // }\n // DUMP(dp);\n //}\n\n //{\n vi dp(n+1);\n vi dps(n+2);\n dps[0] = 1, dps[1] = -1;\n dp[0] = 1;\n REP(i, n) {\n if (dp[i] == 0) {\n dp[i+1] = dp[i] + dps[i+1];\n continue;\n }\n //for (int j = dp2[i]; j < dp1[i]; ++j) {\n // dp[j+1] = 1;\n //}\n if (dp2[i] < dp1[i]) {\n ++dps[dp2[i] + 1];\n --dps[dp1[i] + 1];\n }\n dp[i+1] = dp[i] + dps[i+1];\n }\n //DUMP(dp);\n //DUMP(dps);\n //}\n\n for (int i = n; i >= 0; --i) {\n if (dp[i] > 0) {\n int sum = xs[n] - xs[i];\n int cnt = n - i;\n return sum + cnt - 1 <= w;\n }\n }\n return false;\n };\n //while (true) {\n // int y; cin >> y;\n // check(y);\n //}\n\n int ok = w, ng = 0;\n while (abs(ok - ng) > 1) {\n int mid = (ok + ng) / 2;\n (check(mid) ? ok : ng) = mid;\n }\n cout << ok << endl;\n\n return true;\n}\n\nint main() {\n //ifstream in(\"in.txt\");\n //cin.rdbuf(in.rdbuf());\n while (solve()) {}\n return 0;\n}", "accuracy": 1, "time_ms": 930, "memory_kb": 4144, "score_of_the_acc": -0.322, "final_rank": 6 }, { "submission_id": "aoj_1333_4822769", "code_snippet": "#include<iostream>\n#include<cstdio>\n#include<cstring>\n#include<string>\n#include<vector>\n#include<cmath>\n#include<algorithm>\n#include<map>\n#include<queue>\n#include<deque>\n#include<iomanip>\n#include<tuple>\n#include<cassert>\n#include<set>\nusing namespace std;\ntypedef long long int LL;\ntypedef pair<int,int> P;\ntypedef pair<LL,int> LP;\nconst int INF=1<<30;\nconst LL MAX=1e9+7;\n\nvoid array_show(int *array,int array_n,char middle=' '){\n\tfor(int i=0;i<array_n;i++)printf(\"%d%c\",array[i],(i!=array_n-1?middle:'\\n'));\n}\nvoid array_show(LL *array,int array_n,char middle=' '){\n\tfor(int i=0;i<array_n;i++)printf(\"%lld%c\",array[i],(i!=array_n-1?middle:'\\n'));\n}\nvoid array_show(vector<int> &vec_s,int vec_n=-1,char middle=' '){\n\tif(vec_n==-1)vec_n=vec_s.size();\n\tfor(int i=0;i<vec_n;i++)printf(\"%d%c\",vec_s[i],(i!=vec_n-1?middle:'\\n'));\n}\nvoid array_show(vector<LL> &vec_s,int vec_n=-1,char middle=' '){\n\tif(vec_n==-1)vec_n=vec_s.size();\n\tfor(int i=0;i<vec_n;i++)printf(\"%lld%c\",vec_s[i],(i!=vec_n-1?middle:'\\n'));\n}\n\nint n,m;\nvector<LL> va,vs1,vs2;\nint t[550000];\n\nbool check(int space){\n\tint i,j,k;\n\tLL a,b,c;\n\tint x,y;\n\tvs2.assign(n+1,0);\n\tfor(i=0;i<n;i++){\n\t\tvs2[i+1]=vs2[i]+va[i]+space;\n\t}\n\tmemset(t,0,sizeof(t));\n\ta=1,t[1]=-1;\n\tfor(i=0;i<n;i++){\n\t\ta+=t[i];\n\t\tif(a==0)continue;\n\t\tint z[3]={i,n+1};\n\t\twhile(z[1]-z[0]>1){\n\t\t\tz[2]=(z[0]+z[1])/2;\n\t\t\tb=vs1[z[2]]-vs1[i];\n\t\t\tif(b>m+1)z[1]=z[2];\n\t\t\telse z[0]=z[2];\n\t\t}\n\t\tx=z[1];\n\t\tz[0]=i,z[1]=n+1;\n\t\twhile(z[1]-z[0]>1){\n\t\t\tz[2]=(z[0]+z[1])/2;\n\t\t\tb=vs2[z[2]]-vs2[i];\n\t\t\tif(b>=m+space)z[1]=z[2];\n\t\t\telse z[0]=z[2];\n\t\t}\n\t\ty=z[1];\n\t\tif(x>n)return true;\n\t\tif(y<x)t[y]++,t[x]--;\n\t}\n\treturn false;\n}\n\nint main(){\n\tint i,j,k;\n\tint a,b,c;\n\twhile(cin>>m>>n){\n\t\tif(m==0 && n==0)break;\n\t\tva.clear();\n\t\tvs1.assign(n+1,0);\n\t\tfor(i=0;i<n;i++){\n\t\t\tcin>>a;\n\t\t\tva.push_back(a);\n\t\t\tvs1[i+1]=vs1[i]+va[i]+1;\n\t\t}\n\t\tint z[3]={0,m};\n\t\twhile(z[1]-z[0]>1){\n\t\t\tz[2]=(z[0]+z[1])/2;\n\t\t\tif(check(z[2]))z[1]=z[2];\n\t\t\telse z[0]=z[2];\n\t\t}\n\t\tcout<<z[1]<<endl;\n\t}\n}", "accuracy": 1, "time_ms": 5040, "memory_kb": 6136, "score_of_the_acc": -1.6361, "final_rank": 19 }, { "submission_id": "aoj_1333_4333912", "code_snippet": "#include <bits/stdc++.h>\n#define int long long\nusing namespace std;\n\nconst int INF = 80010;\n\nint w,n,a[50010],s[50010],dp[50010];\n\nbool check(int x){\n\tdp[1] = 1;\n\tint l = 0,r = 0;\n\tfor(int i = 1;i <= n;i++){\n\t\twhile(i > l && s[i] - s[l] + (i - l - 1) > w) l++;\n\t\twhile(i > r && s[i] - s[r] + (i - r - 1) * x >= w) r++;\n\t\tdp[i + 1] = dp[i] + !!(dp[r] - dp[l]);\n\t}\n\tl = n;\n\twhile(l >= 0 && s[n] - s[l] + n - l - 1 < w) l--;\n\treturn dp[n + 1] - dp[l + 1];\n}\n\nvoid solve(){\n\tfor(int i = 0;i < n;i++){\n\t\tscanf(\"%lld\",&a[i]);\n\t\ts[i + 1] = s[i] + a[i];\n\t}\n\tint ng = 0,ok = INF;\n\twhile(ok - ng > 1){\n\t\tint mid = (ok + ng) / 2;\n\t\tif(check(mid)) ok = mid;\n\t\telse ng = mid;\n\t}\n\tprintf(\"%lld\\n\",ok);\n}\n\nsigned main(){\n\twhile(cin >> w >> n,n) solve();\n}", "accuracy": 1, "time_ms": 730, "memory_kb": 4356, "score_of_the_acc": -0.3733, "final_rank": 7 } ]
aoj_1330_cpp
Problem F: Never Wait for Weights In a laboratory, an assistant, Nathan Wada, is measuring weight differences between sample pieces pair by pair. He is using a balance because it can more precisely measure the weight difference between two samples than a spring scale when the samples have nearly the same weight. He is occasionally asked the weight differences between pairs of samples. He can or cannot answer based on measurement results already obtained. Since he is accumulating a massive amount of measurement data, it is now not easy for him to promptly tell the weight differences. Nathan asks you to develop a program that records measurement results and automatically tells the weight differences. Input The input consists of multiple datasets. The first line of a dataset contains two integers N and M . N denotes the number of sample pieces (2 ≤ N ≤ 100,000). Each sample is assigned a unique number from 1 to N as an identifier. The rest of the dataset consists of M lines (1 ≤ M ≤ 100,000), each of which corresponds to either a measurement result or an inquiry. They are given in chronological order. A measurement result has the format, ! a b w which represents the sample piece numbered b is heavier than one numbered a by w micrograms ( a ≠ b ). That is, w = w b − w a , where w a and w b are the weights of a and b , respectively. Here, w is a non-negative integer not exceeding 1,000,000. You may assume that all measurements are exact and consistent. An inquiry has the format, ? a b which asks the weight difference between the sample pieces numbered a and b ( a ≠ b ). The last dataset is followed by a line consisting of two zeros separated by a space. Output For each inquiry, ? a b , print the weight difference in micrograms between the sample pieces numbered a and b , w b − w a , followed by a newline if the weight difference can be computed based on the measurement results prior to the inquiry. The difference can be zero, or negative as well as positive. You can assume that its absolute value is at most 1,000,000. If the difference cannot be computed based on the measurement results prior to the inquiry, print UNKNOWN followed by a newline. Sample Input 2 2 ! 1 2 1 ? 1 2 2 2 ! 1 2 1 ? 2 1 4 7 ! 1 2 100 ? 2 3 ! 2 3 100 ? 2 3 ? 1 3 ! 4 3 150 ? 4 1 0 0 Output for the Sample Input 1 -1 UNKNOWN 100 200 -50
[ { "submission_id": "aoj_1330_11066164", "code_snippet": "#include <iostream>\n#include <vector>\n#include <utility>\nusing namespace std;\n\nclass unionfind{\npublic:\n unionfind(int n): n_(n) {\n parent_.resize(n);\n size_.resize(n);\n diff_.resize(n);\n for(int i=0; i<n; i++){\n parent_[i] = i;\n size_[i] = 1;\n diff_[i] = 0;\n }\n }\n // iとjをくっつける その際iから見たjとの差を与える\n void unite(int i, int j, int d){\n int p = root(i);\n int q = root(j);\n if(p==q) return;\n if(size_[p]>size_[q]){\n parent_[q] = p;\n size_[p] += size_[q];\n diff_[q] = diff_[i] + d - diff_[j];\n }else{\n parent_[p] = q;\n size_[q] += size_[p];\n diff_[p] = diff_[j] - d - diff_[i];\n }\n }\n pair<bool,int> diff(int i, int j){\n if(root(i) != root(j)) return make_pair(false, 0);\n return make_pair(true, diff_[j] - diff_[i]);\n }\nprivate:\n int n_;\n vector<int> parent_;\n vector<int> size_;\n vector<int> diff_; // 親から見たの符号付きの差\n\n int root(int i){\n if(parent_[i] == i) return i;\n int r = root(parent_[i]);\n diff_[i] = diff_[parent_[i]] + diff_[i];\n parent_[i] = r;\n return r;\n }\n};\n\nint main(){\n int n,m;\n while(true){\n cin >> n >> m;\n if((n==0) & (m == 0)){\n break;\n }\n unionfind uf(n+1);\n int x,y,d;\n string s;\n pair<bool,int> ret;\n for(int i=0; i<m; i++){\n cin >> s;\n if(s==\"!\"){\n cin >> x >> y >> d;\n uf.unite(x,y,d);\n }else{\n cin >> x >> y;\n ret = uf.diff(x,y);\n if(!ret.first){\n cout << \"UNKNOWN\" << endl;\n }else{\n cout << ret.second << endl;\n } \n }\n }\n }\n}", "accuracy": 1, "time_ms": 970, "memory_kb": 4308, "score_of_the_acc": -0.8582, "final_rank": 16 }, { "submission_id": "aoj_1330_10853876", "code_snippet": "#include<stdio.h>\n#include<string.h>\n#include<iostream>\n#include<queue>\n#include<algorithm>\n#define maxn 100010\n#define ll long long\nusing namespace std;\nconst int MAXSize = 100010;\nint uset[MAXSize];\nint Rank[MAXSize];\nll w[MAXSize];\n\nvoid makeSet(int Size)\n{\n for(int i = 1; i <= Size; i++)\n {\n uset[i] = i;\n w[i] = 0;\n Rank[i] = 0;\n }\n}\nint Find(int x)\n{\n if (x != uset[x]) return uset[x] = Find(uset[x]);\n return uset[x];\n}\nvoid unionSet(int x, int y,int v)\n{\n int p = x;\n ll vx = 0;\n while(uset[p] != p){\n vx += w[p];\n p = uset[p];\n }\n int py = y;\n ll vy = 0;\n while(uset[py] != py){\n vy += w[py];\n py = uset[py];\n }\n if(p == py)return;\n\n if(Rank[p] < Rank[py]){\n uset[p] = py;\n w[p] = vy - v - vx;\n }else{\n uset[py] = p;\n w[py] = v + vx - vy;\n if (Rank[p] == Rank[py]) Rank[p]++;\n }\n}\n\nvoid Get(int x,int y)\n{\n int p = x;\n ll vx = 0;\n while(uset[p] != p){\n vx += w[p];\n p = uset[p];\n }\n int py = y;\n ll vy = 0;\n while(uset[py] != py){\n vy += w[py];\n py = uset[py];\n }\n if(p == py){\n printf(\"%lld\\n\",vy - vx);\n }else{\n printf(\"UNKNOWN\\n\");\n }\n}\nint main()\n{\n int n,m,a,b,c;\n char NOR;\n while(scanf(\"%d%d\",&n,&m)&&n!=0)\n {\n makeSet(n);\n for(int i=0; i<m; i++)\n {\n getchar();\n scanf(\"%c\",&NOR);\n if(NOR=='!')\n {\n scanf(\"%d%d%d\",&a,&b,&c);\n unionSet(a,b,c);\n }\n else\n {\n scanf(\"%d%d\",&a,&b);\n Get(a,b);\n }\n }\n }\n return 0;\n}", "accuracy": 1, "time_ms": 210, "memory_kb": 5100, "score_of_the_acc": -0.2123, "final_rank": 3 }, { "submission_id": "aoj_1330_10240618", "code_snippet": "#line 1 \"1330.test.cpp\"\n#define PROBLEM \"https://onlinejudge.u-aizu.ac.jp/problems/1330\"\n\n#line 2 \"/home/zawatin/compro/cp-documentation/Src/Template/IOSetting.hpp\"\n\n#line 2 \"/home/zawatin/compro/cp-documentation/Src/Template/TypeAlias.hpp\"\n\n#include <cstdint>\n#include <cstddef>\n\nnamespace zawa {\n\nusing i16 = std::int16_t;\nusing i32 = std::int32_t;\nusing i64 = std::int64_t;\nusing i128 = __int128_t;\n\nusing u8 = std::uint8_t;\nusing u16 = std::uint16_t;\nusing u32 = std::uint32_t;\nusing u64 = std::uint64_t;\n\nusing usize = std::size_t;\n\n} // namespace zawa\n#line 4 \"/home/zawatin/compro/cp-documentation/Src/Template/IOSetting.hpp\"\n\n#include <iostream>\n#include <iomanip>\n\nnamespace zawa {\n\nvoid SetFastIO() {\n std::cin.tie(nullptr)->sync_with_stdio(false);\n}\n\nvoid SetPrecision(u32 dig) {\n std::cout << std::fixed << std::setprecision(dig);\n}\n\n} // namespace zawa\n#line 2 \"/home/zawatin/compro/cp-documentation/Src/DataStructure/DisjointSetUnion/PotentializedDisjointSetUnion.hpp\"\n\n#line 4 \"/home/zawatin/compro/cp-documentation/Src/DataStructure/DisjointSetUnion/PotentializedDisjointSetUnion.hpp\"\n\n#include <algorithm>\n#include <cassert>\n#include <numeric>\n#include <vector>\n\nnamespace zawa {\n\ntemplate <class Group>\nclass PotentializedDisjointSetUnion {\npublic:\n using Value = typename Group::Element;\nprivate:\n usize n_{}, comps_{};\n std::vector<u32> parent_{};\n std::vector<u32> size_{};\n std::vector<Value> potential_{};\n\n u32 leader(u32 v) {\n if (parent_[v] == v) {\n return v;\n }\n else {\n u32 res{leader(parent_[v])};\n potential_[v] = Group::operation(potential_[parent_[v]], potential_[v]);\n return parent_[v] = res;\n }\n }\n Value potential(u32 v) {\n leader(v);\n return potential_[v];\n }\n\npublic:\n\n PotentializedDisjointSetUnion() = default;\n\n PotentializedDisjointSetUnion(u32 n) \n : n_{n}, comps_{n}, parent_(n), size_(n, u32{1}), potential_(n, Group::identity()) {\n std::iota(parent_.begin(), parent_.end(), u32{});\n }\n\n constexpr u32 size() const noexcept {\n return n_;\n }\n\n u32 size(u32 v) {\n leader(v);\n return size_[v];\n }\n\n inline u32 components() const noexcept {\n return comps_;\n }\n\n bool isDefined(u32 u, u32 v) {\n return leader(u) == leader(v);\n }\n\n Value distance(u32 u, u32 v) {\n assert(u < size());\n assert(v < size());\n return Group::operation(Group::inverse(potential(u)), potential(v));\n }\n\n bool merge(u32 u, u32 v, Value value) {\n if (isDefined(u, v)) {\n return distance(u, v) == value;\n }\n comps_--;\n value = Group::operation(potential(u), value);\n value = Group::operation(Group::inverse(potential(v)), value);\n u = leader(u);\n v = leader(v);\n if (size_[u] > size_[v]) {\n value = Group::inverse(value);\n std::swap(u, v);\n }\n size_[u] += size_[v];\n parent_[v] = u;\n potential_[v] = value;\n return true;\n }\n};\n\n} // namespace zawa\n#line 2 \"/home/zawatin/compro/cp-documentation/Src/Algebra/Group/AdditiveGroup.hpp\"\n\nnamespace zawa {\n\ntemplate <class T>\nclass AdditiveGroup {\npublic:\n using Element = T;\n static constexpr T identity() noexcept {\n return T{};\n }\n static constexpr T operation(const T& l, const T& r) noexcept {\n return l + r;\n }\n static constexpr T inverse(const T& v) noexcept {\n return -v;\n }\n};\n\n} // namespace zawa\n#line 6 \"1330.test.cpp\"\n\nusing namespace zawa;\n\n#line 11 \"1330.test.cpp\"\nint N, Q;\nbool solve() {\n std::cin >> N >> Q;\n if (N == 0 and Q == 0) return false;\n PotentializedDisjointSetUnion<AdditiveGroup<int>> dsu(N);\n while (Q--) {\n char t;\n int a, b;\n std::cin >> t >> a >> b;\n a--; b--;\n if (t == '!') {\n int w;\n std::cin >> w;\n dsu.merge(a, b, w);\n }\n else if (t == '?') {\n if (dsu.isDefined(a, b)) std::cout << dsu.distance(a, b) << '\\n';\n else std::cout << \"UNKNOWN\\n\";\n }\n else assert(false);\n }\n return true;\n}\nint main() {\n SetFastIO(); \n while (solve()) ;\n}", "accuracy": 1, "time_ms": 200, "memory_kb": 6304, "score_of_the_acc": -0.3644, "final_rank": 4 }, { "submission_id": "aoj_1330_10037453", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\ntypedef long long ll;\nstruct node {\n\tint v;\n\tll dif;\n\tvector<int> chd;\n};\nstruct UnionFind {\n\tvector<node> ps;\n\tUnionFind(int n) : ps(n, {-1, 0, {}}) {\n\t\tfor (int i = 0; i < n; i++) {\n\t\t\tps[i].chd.push_back(i);\n\t\t}\n\t}\n\tint root(int x) {\n\t\tif (ps[x].v < 0)\n\t\t\treturn x;\n\t\telse\n\t\t\treturn ps[x].v = root(ps[x].v);\n\t}\n\tbool same(int x, int y) { return root(x) == root(y); }\n\tvoid unite(int x, int y, int w) {\n\t\tint rx = root(x);\n\t\tint ry = root(y);\n\t\tif (rx == ry) return;\n\t\tll d = ps[x].dif - (w + ps[y].dif);\n\t\tif (-ps[rx].v < -ps[ry].v) {\n\t\t\tswap(rx, ry);\n\t\t\td *= -1;\n\t\t}\n\t\tfor (auto &ty : ps[ry].chd) {\n\t\t\tps[rx].chd.push_back(ty);\n\t\t\tps[ty].dif += d;\n\t\t}\n\t\tps[ry].chd.clear();\n\t\tps[rx].v += ps[ry].v;\n\t\tps[ry].v = rx;\n\t\treturn;\n\t}\n};\nint main() {\n\twhile (1) {\n\t\tint n, m;\n\t\tcin >> n >> m;\n\t\tif (n == 0 && m == 0) break;\n\t\tUnionFind uf(n);\n\t\twhile (m--) {\n\t\t\tstring t;\n\t\t\tcin >> t;\n\t\t\tif (t == \"!\") {\n\t\t\t\tint a, b, w;\n\t\t\t\tcin >> a >> b >> w;\n\t\t\t\ta--;\n\t\t\t\tb--;\n\t\t\t\tuf.unite(a, b, w);\n\t\t\t} else {\n\t\t\t\tint a, b;\n\t\t\t\tcin >> a >> b;\n\t\t\t\ta--;\n\t\t\t\tb--;\n\t\t\t\tif (uf.same(a, b)) {\n\t\t\t\t\tll da = uf.ps[a].dif;\n\t\t\t\t\tll db = uf.ps[b].dif;\n\t\t\t\t\tcout << da - db << \"\\n\";\n\t\t\t\t} else {\n\t\t\t\t\tcout << \"UNKNOWN\\n\";\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n\treturn 0;\n}", "accuracy": 1, "time_ms": 990, "memory_kb": 11028, "score_of_the_acc": -1.7822, "final_rank": 20 }, { "submission_id": "aoj_1330_10002433", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\n// 重み付き Union-Find\ntemplate<class Abel> struct UnionFind {\n const Abel UNITY_SUM = 0; // to be set\n vector<int> par;\n vector<bool> is_nan;\n vector<Abel> diff_weight;\n\n UnionFind() { }\n UnionFind(int n) : \n par(n, -1), diff_weight(n, UNITY_SUM) {}\n \n int root(int x) {\n if (par[x] < 0) return x;\n else {\n int r = root(par[x]);\n diff_weight[x] += diff_weight[par[x]];\n return par[x] = r;\n }\n }\n \n Abel calc_weight(int x) {\n root(x);\n return diff_weight[x];\n }\n \n bool issame(int x, int y) {\n return root(x) == root(y);\n }\n \n bool merge(int x, int y, Abel w = 0) {\n w += calc_weight(x); w -= calc_weight(y);\n x = root(x); y = root(y);\n if (x == y) return false;\n if (par[x] > par[y]) swap(x, y), w = -w; // merge technique\n par[x] += par[y];\n par[y] = x;\n diff_weight[y] = w;\n return true;\n }\n \n Abel diff(int x, int y) {\n return calc_weight(y) - calc_weight(x);\n }\n \n int size(int x) {\n return -par[root(x)];\n }\n};\n\nint main() {\n // 入力 (\"0 0\" が来るまで連続で入力を受け取る)\n int N, M;\n while (cin >> N >> M, N) {\n // 重み付き Union-Find を初期化\n UnionFind<int> uf(N);\n\n for (int i = 0; i < M; ++i) {\n char c;\n cin >> c;\n if (c == '!') {\n int a, b, w;\n cin >> a >> b >> w;\n --a, --b;\n\n // w[b] - w[a] = w となるように併合\n uf.merge(a, b, w);\n } else {\n int a, b;\n cin >> a >> b;\n --a, --b;\n\n if (!uf.issame(a, b)) {\n // a, b が併合されていない場合は w[b] - w[a] の値は未定\n cout << \"UNKNOWN\" << endl;\n } else {\n cout << uf.diff(a, b) << endl;\n }\n }\n }\n }\n}", "accuracy": 1, "time_ms": 900, "memory_kb": 3596, "score_of_the_acc": -0.6931, "final_rank": 10 }, { "submission_id": "aoj_1330_9957453", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\nconst int INF = 1e9 + 10;\nconst ll INFL = 4e18;\n\nstruct DSU {\n vector<int> par, sz;\n vector<ll> df;\n\n DSU(int n) {\n par.resize(n);\n iota(par.begin(), par.end(), 0);\n sz.assign(n, 1);\n df.assign(n, 0);\n }\n\n int find(int x) {\n if (par[x] == x) return x;\n int root = find(par[x]);\n df[x] += df[par[x]];\n return par[x] = root;\n }\n\n ll weight(int x) {\n find(x);\n return df[x];\n }\n\n void merge(int x, int y, ll w) {\n w += weight(x);\n w -= weight(y);\n x = find(x), y = find(y);\n if (x == y) return;\n if (sz[x] < sz[y]) swap(x, y), w = -w;\n par[y] = x;\n sz[x] += sz[y];\n df[y] = w;\n }\n\n ll diff(int x, int y) { return df[y] - df[x]; }\n\n bool same(int x, int y) { return find(x) == find(y); }\n};\n\nint main() {\n while (true) {\n int N, M;\n cin >> N >> M;\n\n if (N == 0 && M == 0) break;\n\n DSU dsu(N);\n while (M--) {\n char c;\n cin >> c;\n\n if (c == '!') {\n int a, b;\n ll w;\n cin >> a >> b >> w;\n a--;\n b--;\n dsu.merge(a, b, w);\n } else {\n int a, b;\n cin >> a >> b;\n a--;\n b--;\n\n if (!dsu.same(a, b)) {\n cout << \"UNKNOWN\\n\";\n } else {\n cout << dsu.diff(a, b) << '\\n';\n }\n }\n }\n }\n}", "accuracy": 1, "time_ms": 900, "memory_kb": 4708, "score_of_the_acc": -0.8427, "final_rank": 15 }, { "submission_id": "aoj_1330_9947415", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\n\nusing ll = long long;\n\nstruct Unionfind{\n\n\tUnionfind(int n){\n\t\tpar.assign(n, -1);\n\t\tpot.assign(n, 0);\n\t}\n\n\tbool merge(int x, int y, ll w){\n\t\tw += get_pot(x);\n\t\tw -= get_pot(y);\n\t\tx = root(x), y = root(y);\n\t\tif(x == y){\n\t\t\tif(diff(x, y) == w){\n\t\t\t\treturn true;\n\t\t\t}else{\n\t\t\t\treturn false;\n\t\t\t}\n\t\t}\n\t\t--c;\n\t\tif(par[x] > par[y]){\n\t\t\tswap(x, y);\n\t\t\tw = -w;\n\t\t}\n\t\tpar[x] += par[y];\n\t\tpar[y] = x;\n\t\tpot[y] = w;\n\t\treturn true;\n\t}\n\n\tbool same(int x, int y){\n\t\treturn root(x) == root(y);\n\t}\n\n\tint root(int x){\n\t\tif(par[x] < 0){\n\t\t\treturn x;\n\t\t}else {\n\t\t\tint r = root(par[x]);\n\t\t\tpot[x] = pot[x] + pot[par[x]];\n\t\t\treturn par[x] = r;\n\t\t}\n\t}\n\n\tll get_pot(int x){\n\t\troot(x);\n\t\treturn pot[x];\n\t}\n\n\tll diff(int x, int y){\n\t\treturn get_pot(x) - get_pot(y);\n\t}\n\n\tint n, c;\n\tvector<int> par;\n\tvector<ll> pot;\n};\n\nint main(){\n\tint n, q;\n\twhile(1){\n\t\tcin >> n >> q;\n\t\tif(n == 0 && q == 0) break;\n\t\tUnionfind uf(n);\n\t\twhile(q--){\n\t\t\tchar c;\n\t\t\tcin >> c;\n\t\t\tif(c == '!'){\n\t\t\t\tint x, y;\n\t\t\t\tll w;\n\t\t\t\tcin >> x >> y >> w;\n\t\t\t\t--x, --y;\n\t\t\t\tuf.merge(x, y, w);\n\t\t\t}else{\n\t\t\t\tint x, y;\n\t\t\t\tcin >> x >> y;\n\t\t\t\t--x, --y;\n\t\t\t\tif(uf.same(x, y)){\n\t\t\t\t\tcout << uf.diff(y, x) << endl;\n\t\t\t\t}else{\n\t\t\t\t\tcout << \"UNKNOWN\" << endl;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n\treturn 0;\n}", "accuracy": 1, "time_ms": 620, "memory_kb": 4700, "score_of_the_acc": -0.5644, "final_rank": 7 }, { "submission_id": "aoj_1330_9876051", "code_snippet": "#include <iostream>\n#include <algorithm>\n#include <cassert>\n#include <numeric>\n#include <vector>\nstruct DSU {\n int n;\n std::vector<int> par, size, po;\n DSU(int n_) : n{n_}, par(n_), size(n_, 1), po(n_, 0) {\n std::iota(par.begin(), par.end(), 0);\n }\n int leader(int v) {\n // for (auto p : par) std::cout << p << ' ';\n // std::cout << std::endl;\n // std::cout << v << \" leader\" << std::endl;\n assert(0 <= v and v < n);\n if (par[v] == v) return v;\n else {\n int res{leader(par[v])};\n po[v] = po[par[v]] + po[v];\n return par[v] = res;\n }\n }\n int potential(int v) {\n leader(v);\n return po[v];\n }\n bool same(int u, int v) {\n // std::cout << \"same \" << u << ' ' << v << std::endl;\n return leader(u) == leader(v);\n }\n int distance(int u, int v) {\n assert(0 <= u and u < n);\n assert(0 <= v and v < n);\n // std::cout << u << ' ' << v << ' ' << \"distance\" << std::endl;\n return -po.at(u) + po.at(v);\n }\n bool merge(int u, int v, int val) {\n assert(0 <= u and u < n);\n assert(0 <= v and v < n);\n if (same(u, v)) return distance(u, v) == val;\n val = po.at(u) + val;\n val = -po.at(v) + val; \n u = leader(u);\n v = leader(v);\n if (size[u] > size[v]) {\n val = -val;\n std::swap(u, v);\n }\n size[u] += size[v];\n par[v] = u;\n po[v] = val;\n // std::cout << u << ' ' << v << ' ' << val << \"!\" << std::endl;\n return true;\n }\n};\nint N, M;\nvoid solve() {\n // std::cout << N << ' ' << M << \" TEST START\" << std::endl;\n DSU dsu(N);\n while (M--) {\n char c;\n std::cin >> c;\n if (c == '!') {\n int a, b, w;\n std::cin >> a >> b >> w;\n a--; b--;\n assert(dsu.merge(a, b, w));\n // std::cout << \"merge end\" << std::endl;\n }\n else if (c == '?') {\n int a, b;\n std::cin >> a >> b;\n a--; b--;\n // std::cout << \"try to calc \" << a << ' ' << b << std::endl;\n if (dsu.same(a, b)) std::cout << dsu.distance(a, b) << std::endl;\n else std::cout << \"UNKNOWN\" << std::endl;;\n }\n else {\n assert(false);\n }\n }\n // std::cout << \"TEST END\" << std::endl;\n}\nint main() {\n while (true) {\n std::cin >> N >> M;\n if (N == 0 and M == 0) break;\n solve();\n }\n}", "accuracy": 1, "time_ms": 910, "memory_kb": 6256, "score_of_the_acc": -1.0609, "final_rank": 18 }, { "submission_id": "aoj_1330_9544359", "code_snippet": "#include <bits/stdc++.h>\n\nusing namespace std;\n\n#define rep(i, a, b) for (int i = (int)(a); i < (int)(b); i++)\n#define rrep(i, a, b) for (int i = (int)(b)-1; i >= (int)(a); i--)\n#define ALL(v) (v).begin(), (v).end()\n#define UNIQUE(v) sort(ALL(v)), (v).erase(unique(ALL(v)), (v).end())\n#define SZ(v) (int)v.size()\n#define MIN(v) *min_element(ALL(v))\n#define MAX(v) *max_element(ALL(v))\n#define LB(v, x) int(lower_bound(ALL(v), (x)) - (v).begin())\n#define UB(v, x) int(upper_bound(ALL(v), (x)) - (v).begin())\n\nusing uint = unsigned int;\nusing ll = long long int;\nusing ull = unsigned long long;\nusing i128 = __int128_t;\nusing u128 = __uint128_t;\nconst int inf = 0x3fffffff;\nconst ll INF = 0x1fffffffffffffff;\n\ntemplate <typename T> inline bool chmax(T &a, T b) {\n if (a < b) {\n a = b;\n return 1;\n }\n return 0;\n}\ntemplate <typename T> inline bool chmin(T &a, T b) {\n if (a > b) {\n a = b;\n return 1;\n }\n return 0;\n}\ntemplate <typename T, typename U> T ceil(T x, U y) {\n assert(y != 0);\n if (y < 0)\n x = -x, y = -y;\n return (x > 0 ? (x + y - 1) / y : x / y);\n}\ntemplate <typename T, typename U> T floor(T x, U y) {\n assert(y != 0);\n if (y < 0)\n x = -x, y = -y;\n return (x > 0 ? x / y : (x - y + 1) / y);\n}\ntemplate <typename T> int popcnt(T x) {\n return __builtin_popcountll(x);\n}\ntemplate <typename T> int topbit(T x) {\n return (x == 0 ? -1 : 63 - __builtin_clzll(x));\n}\ntemplate <typename T> int lowbit(T x) {\n return (x == 0 ? -1 : __builtin_ctzll(x));\n}\n\ntemplate <class T, class U>\nostream &operator<<(ostream &os, const pair<T, U> &p) {\n os << \"P(\" << p.first << \", \" << p.second << \")\";\n return os;\n}\ntemplate <typename T> ostream &operator<<(ostream &os, const vector<T> &vec) {\n os << \"{\";\n for (int i = 0; i < vec.size(); i++) {\n os << vec[i] << (i + 1 == vec.size() ? \"\" : \", \");\n }\n os << \"}\";\n return os;\n}\ntemplate <typename T, typename U>\nostream &operator<<(ostream &os, const map<T, U> &map_var) {\n os << \"{\";\n for (auto itr = map_var.begin(); itr != map_var.end(); itr++) {\n os << \"(\" << itr->first << \", \" << itr->second << \")\";\n itr++;\n if (itr != map_var.end())\n os << \", \";\n itr--;\n }\n os << \"}\";\n return os;\n}\ntemplate <typename T> ostream &operator<<(ostream &os, const set<T> &set_var) {\n os << \"{\";\n for (auto itr = set_var.begin(); itr != set_var.end(); itr++) {\n os << *itr;\n ++itr;\n if (itr != set_var.end())\n os << \", \";\n itr--;\n }\n os << \"}\";\n return os;\n}\n#ifdef LOCAL\n#define show(...) _show(0, #__VA_ARGS__, __VA_ARGS__)\n#else\n#define show(...) true\n#endif\ntemplate <typename T> void _show(int i, T name) {\n cerr << '\\n';\n}\ntemplate <typename T1, typename T2, typename... T3>\nvoid _show(int i, const T1 &a, const T2 &b, const T3 &...c) {\n for (; a[i] != ',' && a[i] != '\\0'; i++)\n cerr << a[i];\n cerr << \":\" << b << \" \";\n _show(i + 1, a, c...);\n}\n\n#include <unistd.h>\n\nnamespace fastio {\nstatic constexpr uint32_t SZ = 1 << 17;\nchar ibuf[SZ];\nchar obuf[SZ];\nchar out[100];\n// pointer of ibuf, obuf\n\n\nuint32_t pil = 0, pir = 0, por = 0;\n\nstruct Pre {\n char num[10000][4];\n constexpr Pre() : num() {\n for (int i = 0; i < 10000; i++) {\n int n = i;\n for (int j = 3; j >= 0; j--) {\n num[i][j] = n % 10 | '0';\n n /= 10;\n }\n }\n }\n} constexpr pre;\n\ninline void load() {\n memmove(ibuf, ibuf + pil, pir - pil);\n pir = pir - pil + fread(ibuf + pir - pil, 1, SZ - pir + pil, stdin);\n pil = 0;\n if (pir < SZ)\n ibuf[pir++] = '\\n';\n}\n\ninline void flush() {\n fwrite(obuf, 1, por, stdout);\n por = 0;\n}\n\nvoid rd(char &c) {\n do {\n if (pil + 1 > pir)\n load();\n c = ibuf[pil++];\n } while (isspace(c));\n}\n\nvoid rd(string &x) {\n x.clear();\n char c;\n do {\n if (pil + 1 > pir)\n load();\n c = ibuf[pil++];\n } while (isspace(c));\n do {\n x += c;\n if (pil == pir)\n load();\n c = ibuf[pil++];\n } while (!isspace(c));\n}\n\ntemplate <typename T> void rd_real(T &x) {\n string s;\n rd(s);\n x = stod(s);\n}\n\ntemplate <typename T> void rd_integer(T &x) {\n if (pil + 100 > pir)\n load();\n char c;\n do\n c = ibuf[pil++];\n while (c < '-');\n bool minus = 0;\n if constexpr (is_signed<T>::value || is_same_v<T, i128>) {\n if (c == '-') {\n minus = 1, c = ibuf[pil++];\n }\n }\n x = 0;\n while ('0' <= c) {\n x = x * 10 + (c & 15), c = ibuf[pil++];\n }\n if constexpr (is_signed<T>::value || is_same_v<T, i128>) {\n if (minus)\n x = -x;\n }\n}\n\nvoid rd(int &x) {\n rd_integer(x);\n}\nvoid rd(ll &x) {\n rd_integer(x);\n}\nvoid rd(i128 &x) {\n rd_integer(x);\n}\nvoid rd(uint &x) {\n rd_integer(x);\n}\nvoid rd(ull &x) {\n rd_integer(x);\n}\nvoid rd(u128 &x) {\n rd_integer(x);\n}\nvoid rd(double &x) {\n rd_real(x);\n}\nvoid rd(long double &x) {\n rd_real(x);\n}\n\ntemplate <class T, class U> void rd(pair<T, U> &p) {\n return rd(p.first), rd(p.second);\n}\ntemplate <size_t N = 0, typename T> void rd_tuple(T &t) {\n if constexpr (N < std::tuple_size<T>::value) {\n auto &x = std::get<N>(t);\n rd(x);\n rd_tuple<N + 1>(t);\n }\n}\ntemplate <class... T> void rd(tuple<T...> &tpl) {\n rd_tuple(tpl);\n}\n\ntemplate <size_t N = 0, typename T> void rd(array<T, N> &x) {\n for (auto &d : x)\n rd(d);\n}\ntemplate <class T> void rd(vector<T> &x) {\n for (auto &d : x)\n rd(d);\n}\n\nvoid read() {}\ntemplate <class H, class... T> void read(H &h, T &...t) {\n rd(h), read(t...);\n}\n\nvoid wt(const char c) {\n if (por == SZ)\n flush();\n obuf[por++] = c;\n}\nvoid wt(const string s) {\n for (char c : s)\n wt(c);\n}\nvoid wt(const char *s) {\n size_t len = strlen(s);\n for (size_t i = 0; i < len; i++)\n wt(s[i]);\n}\n\ntemplate <typename T> void wt_integer(T x) {\n if (por > SZ - 100)\n flush();\n if (x < 0) {\n obuf[por++] = '-', x = -x;\n }\n int outi;\n for (outi = 96; x >= 10000; outi -= 4) {\n memcpy(out + outi, pre.num[x % 10000], 4);\n x /= 10000;\n }\n if (x >= 1000) {\n memcpy(obuf + por, pre.num[x], 4);\n por += 4;\n } else if (x >= 100) {\n memcpy(obuf + por, pre.num[x] + 1, 3);\n por += 3;\n } else if (x >= 10) {\n int q = (x * 103) >> 10;\n obuf[por] = q | '0';\n obuf[por + 1] = (x - q * 10) | '0';\n por += 2;\n } else\n obuf[por++] = x | '0';\n memcpy(obuf + por, out + outi + 4, 96 - outi);\n por += 96 - outi;\n}\n\ntemplate <typename T> void wt_real(T x) {\n ostringstream oss;\n oss << fixed << setprecision(15) << double(x);\n string s = oss.str();\n wt(s);\n}\n\nvoid wt(int x) {\n wt_integer(x);\n}\nvoid wt(ll x) {\n wt_integer(x);\n}\nvoid wt(i128 x) {\n wt_integer(x);\n}\nvoid wt(uint x) {\n wt_integer(x);\n}\nvoid wt(ull x) {\n wt_integer(x);\n}\nvoid wt(u128 x) {\n wt_integer(x);\n}\nvoid wt(double x) {\n wt_real(x);\n}\nvoid wt(long double x) {\n wt_real(x);\n}\n\ntemplate <class T, class U> void wt(const pair<T, U> val) {\n wt(val.first);\n wt(' ');\n wt(val.second);\n}\ntemplate <size_t N = 0, typename T> void wt_tuple(const T t) {\n if constexpr (N < std::tuple_size<T>::value) {\n if constexpr (N > 0) {\n wt(' ');\n }\n const auto x = std::get<N>(t);\n wt(x);\n wt_tuple<N + 1>(t);\n }\n}\ntemplate <class... T> void wt(tuple<T...> tpl) {\n wt_tuple(tpl);\n}\ntemplate <class T, size_t S> void wt(const array<T, S> val) {\n auto n = val.size();\n for (size_t i = 0; i < n; i++) {\n if (i)\n wt(' ');\n wt(val[i]);\n }\n}\ntemplate <class T> void wt(const vector<T> val) {\n auto n = val.size();\n for (size_t i = 0; i < n; i++) {\n if (i)\n wt(' ');\n wt(val[i]);\n }\n}\n\nvoid print() {\n wt('\\n');\n}\ntemplate <class Head, class... Tail> void print(Head &&head, Tail &&...tail) {\n wt(head);\n if (sizeof...(Tail))\n wt(' ');\n print(forward<Tail>(tail)...);\n}\nvoid __attribute__((destructor)) _d() {\n flush();\n}\n} // namespace fastio\n\n\nusing fastio::flush;\nusing fastio::print;\nusing fastio::read;\n\ninline void first(bool i = true) {\n print(i ? \"first\" : \"second\");\n}\ninline void Alice(bool i = true) {\n print(i ? \"Alice\" : \"Bob\");\n}\ninline void Takahashi(bool i = true) {\n print(i ? \"Takahashi\" : \"Aoki\");\n}\ninline void yes(bool i = true) {\n print(i ? \"yes\" : \"no\");\n}\ninline void Yes(bool i = true) {\n print(i ? \"Yes\" : \"No\");\n}\ninline void No() {\n print(\"No\");\n}\ninline void YES(bool i = true) {\n print(i ? \"YES\" : \"NO\");\n}\ninline void NO() {\n print(\"NO\");\n}\ninline void Yay(bool i = true) {\n print(i ? \"Yay!\" : \":(\");\n}\ninline void Possible(bool i = true) {\n print(i ? \"Possible\" : \"Impossible\");\n}\ninline void POSSIBLE(bool i = true) {\n print(i ? \"POSSIBLE\" : \"IMPOSSIBLE\");\n}\n\n/**\n * @brief Fast IO\n */\n\nstruct dsu {\n public:\n dsu() : _n(0) {}\n dsu(int n) : _n(n), parent_or_size(n, -1), weight(n,0) {}\n\n int merge(int a, int b, ll w) {\n assert(0 <= a && a < _n);\n assert(0 <= b && b < _n);\n w += calc_weight(a), w -= calc_weight(b);\n int x = leader(a), y = leader(b);\n if (x == y) return x;\n if (-parent_or_size[x] < -parent_or_size[y]) std::swap(x, y), w = -w;\n parent_or_size[x] += parent_or_size[y];\n parent_or_size[y] = x;\n weight[y] = w;\n return x;\n }\n\n bool same(int a, int b) {\n assert(0 <= a && a < _n);\n assert(0 <= b && b < _n);\n return leader(a) == leader(b);\n }\n\n int leader(int a) {\n assert(0 <= a && a < _n);\n if (parent_or_size[a] < 0) return a;\n int l = leader(parent_or_size[a]);\n weight[a] += weight[parent_or_size[a]];\n return parent_or_size[a] = l;\n }\n\n ll calc_weight(int a) {\n leader(a);\n return weight[a];\n }\n\n ll diff(int a, int b) {\n return calc_weight(b) - calc_weight(a);\n }\n\n private:\n int _n;\n // root node: -1 * component size\n // otherwise: parent\n std::vector<int> parent_or_size;\n vector<ll> weight;\n};\n\nint main() {\nwhile(1) {\n int N, M;\n cin >> N >> M;\n if (N == 0 && M == 0) return 0;\n dsu UF(N);\n rep(i,0,M) {\n char C;\n cin >> C;\n if (C == '!') {\n int A, B;\n ll W;\n cin >> A >> B >> W;\n A--, B--;\n UF.merge(A,B,W);\n }\n else {\n int A, B;\n cin >> A >> B;\n A--, B--;\n if (!UF.same(A,B)) {\n cout << \"UNKNOWN\" << endl;\n }\n else {\n cout << UF.diff(A,B) << endl;\n }\n }\n }\n}\n}", "accuracy": 1, "time_ms": 900, "memory_kb": 4576, "score_of_the_acc": -0.8249, "final_rank": 13 }, { "submission_id": "aoj_1330_9103893", "code_snippet": "#if 1\n#include<cmath>\n#include<limits>\n#include<iostream>\n#include<algorithm>\n#include<functional>\n#include<string>\n#include<map>\n#include<set>\n#include<ctime>\n#include<queue>\n#include<vector>\n#include<tuple>\n// #include<format>\n#ifdef _INTEGRAL_MAX_BITS\n// #include\"C:\\Users\\Spare\\source\\repos\\Competitive_programming\\Competitive_programming\\debugging.h\"\n#endif\n#ifndef DEBUGGING\n#define MY_MAIN int main()\n#define REDIRCT_IO ;\n#define RESTORE_IO ;\n#endif\n#define sc static\n#define ct const\n#define ft first\n#define sd second\nusing namespace std;\n// using namespace atcoder;\n// using namespace Eigen;\nusing ld = long double;\nusing ll = long long;\nusing cchar = const char;\nusing cint = const int;\nusing cll = const ll;\nusing uchar = unsigned char;\nusing uint = unsigned int;\nusing ull = unsigned long long;\nusing cuchar = const uchar;\nusing cuint = const uint;\nusing cull = const ull;\ntemplate<class TYPE> static const TYPE INF = numeric_limits<TYPE>::max();\n#define rep(i, n) for(int i = (0), fend = n; i < fend; ++i)\nstatic const string ascii_lowercase = \"abcdefghijklmnopqrstuvwxyz\";\nstatic const string ascii_uppercase = \"ABCDEFGHIJKLMNOPQRSTUVWXYZ\";\ntemplate<class Abel> struct UnionFind {\n\tvector<int> par;\n\tvector<int> rank;\n\tvector<Abel> diff_weight;\n\n\tUnionFind(int n = 1, Abel SUM_UNITY = 0) {\n\t\tinit(n, SUM_UNITY);\n\t}\n\n\tvoid init(int n = 1, Abel SUM_UNITY = 0) {\n\t\tpar.resize(n); rank.resize(n); diff_weight.resize(n);\n\t\tfor (int i = 0; i < n; ++i) par[i] = i, rank[i] = 0, diff_weight[i] = SUM_UNITY;\n\t}\n\n\tint root(int x) {\n\t\tif (par[x] == x) {\n\t\t\treturn x;\n\t\t}\n\t\telse {\n\t\t\tint r = root(par[x]);\n\t\t\tdiff_weight[x] += diff_weight[par[x]];\n\t\t\treturn par[x] = r;\n\t\t}\n\t}\n\n\tAbel weight(int x) {\n\t\troot(x);\n\t\treturn diff_weight[x];\n\t}\n\n\tbool issame(int x, int y) {\n\t\treturn root(x) == root(y);\n\t}\n\n\tbool merge(int x, int y, Abel w) {\n\t\tw += weight(x); w -= weight(y);\n\t\tx = root(x); y = root(y);\n\t\tif (x == y) return false;\n\t\tif (rank[x] < rank[y]) swap(x, y), w = -w;\n\t\tif (rank[x] == rank[y]) ++rank[x];\n\t\tpar[y] = x;\n\t\tdiff_weight[y] = w;\n\t\treturn true;\n\t}\n\n\tAbel diff(int x, int y) {\n\t\treturn weight(y) - weight(x);\n\t}\n};\nint N, M;\nint main() {\n REDIRCT_IO;\n\twhile (true) {\n\t\tcin >> N >> M;\n\t\tif (N == 0)break;\n\t\tUnionFind<ll> uf(N);\n\t\tchar c; int a, b; ll w;\n\t\trep(t, M) {\n\t\t\tcin >> c;\n\t\t\tif (c == '!') {\n\t\t\t\tcin >> a >> b >> w; --a; --b;\n\t\t\t\tuf.merge(a, b, w);\n\t\t\t}\n\t\t\telse {\n\t\t\t\tcin >> a >> b; --a; --b;\n\t\t\t\tif (uf.issame(a, b)) {\n\t\t\t\t\tcout << uf.diff(a, b) << '\\n';\n\t\t\t\t}\n\t\t\t\telse {\n\t\t\t\t\tcout << \"UNKNOWN\" << endl;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\tcout << flush;\n\t}\n RESTORE_IO;\n return 0;\n}\n\n#endif", "accuracy": 1, "time_ms": 900, "memory_kb": 4704, "score_of_the_acc": -0.8422, "final_rank": 14 }, { "submission_id": "aoj_1330_8992723", "code_snippet": "#line 2 \"cp-library/src/cp-template.hpp\"\n#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\nusing ld = long double;\nusing uint = unsigned int;\nusing ull = unsigned long long;\nusing i32 = int;\nusing u32 = unsigned int;\nusing i64 = long long;\nusing u64 = unsigned long long;\nusing i128 = __int128_t;\ntemplate < class T > bool chmin(T& a, T b) { if(a > b) { a = b; return true; } return false; }\ntemplate < class T > bool chmax(T& a, T b) { if(a < b) { a = b; return true; } return false; }\ntemplate < class T, class U > T ceil (T x, U y) { return (x > 0 ? (x + y - 1) / y : x / y); }\ntemplate < class T, class U > T floor(T x, U y) { return (x > 0 ? x / y : (x - y + 1) / y); }\nint popcnt(i32 x) { return __builtin_popcount(x); }\nint popcnt(u32 x) { return __builtin_popcount(x); }\nint popcnt(i64 x) { return __builtin_popcountll(x); }\nint popcnt(u64 x) { return __builtin_popcountll(x); }\n\n#line 2 \"cp-library/src/utility/rep_itr.hpp\"\ntemplate < class T > struct itr_rep {\n T i, d;\n constexpr itr_rep(const T i) noexcept : i(i), d(1) {}\n constexpr itr_rep(const T i, const T d) noexcept : i(i), d(d) {}\n void operator++() noexcept { i += d; }\n constexpr int operator*() const noexcept { return i; }\n constexpr bool operator!=(const itr_rep x) const noexcept { return d > 0 ? i < x.i : i > x.i; }\n};\n\ntemplate < class T > struct rep {\n const itr_rep< T > s, t;\n constexpr rep(const T t) noexcept : s(0), t(t) {}\n constexpr rep(const T s, const T t) noexcept : s(s), t(t) {}\n constexpr rep(const T s, const T t, const T d) noexcept : s(s, d), t(t, d) {}\n constexpr auto begin() const noexcept { return s; }\n constexpr auto end () const noexcept { return t; }\n};\n\ntemplate < class T > struct revrep {\n const itr_rep < T > s, t;\n constexpr revrep(const T t) noexcept : s(t - 1, -1), t(-1, -1) {}\n constexpr revrep(const T s, const T t) noexcept : s(t - 1, -1), t(s - 1, -1) {}\n constexpr revrep(const T s, const T t, const T d) noexcept : s(t - 1, -d), t(s - 1, -d) {}\n constexpr auto begin() const noexcept { return s; }\n constexpr auto end () const noexcept { return t; }\n};\n#line 3 \"cp-library/src/utility/io.hpp\"\n\n/* 128bit integer */\nistream& operator>>(istream& is, i128& x) {\n std::string s; is >> s;\n int pm = (s[0] == '-');\n x = 0;\n for(int i : rep(pm, int(s.size()))) x = x * 10 + (s[i] - '0');\n if(pm) x *= -1;\n return is;\n}\nostream& operator<<(ostream& os, const i128& x) {\n if(x == 0) return os << '0';\n i128 y = x;\n if(y < 0) { os << '-'; y *= -1; }\n std::vector<int> ny;\n while(y > 0) { ny.push_back(y % 10); y /= 10; }\n for(int i : revrep(ny.size())) os << ny[i];\n return os;\n}\n\ntemplate < class S, class T > istream& operator>>(istream& is, std::pair< S, T >& x) { is >> x.first >> x.second; return is; }\ntemplate < class S, class T > ostream& operator<<(ostream& os, const std::pair< S, T >& x) { os << x.first << \" \" << x.second; return os; }\n\nnamespace scanner {\n struct sca {\n template < class T > operator T() {\n T s; std::cin >> s; return s;\n }\n };\n struct vec {\n int n;\n vec(int n) : n(n) {}\n template < class T > operator std::vector< T >() {\n std::vector< T > v(n);\n for(T& x : v) std::cin >> x;\n return v;\n }\n };\n struct mat {\n int h, w;\n mat(int h, int w) : h(h), w(w) {}\n template < class T > operator std::vector< std::vector< T > >() {\n std::vector m(h, std::vector< T >(w));\n for(std::vector< T >& v : m) for(T& x : v) std::cin >> x;\n return m;\n }\n };\n struct speedup {\n speedup() {\n std::cin.tie(0);\n std::ios::sync_with_stdio(0);\n }\n } speedup_instance;\n}\nscanner::sca in() { return scanner::sca(); }\nscanner::vec in(int n) { return scanner::vec(n); }\nscanner::mat in(int h, int w) { return scanner::mat(h, w); }\n\nnamespace printer {\n void precision(int d) { std::cout << std::fixed << std::setprecision(d); }\n void flush() { std::cout.flush(); }\n}\n\ntemplate < class T >\nostream& operator<<(ostream& os, const std::vector< T > a) {\n int n = a.size();\n for(int i : rep(n)) { os << a[i]; if(i != n - 1) os << ' '; }\n return os;\n}\n\nint print() { std::cout << '\\n'; return 0; }\ntemplate < class head, class... tail > int print(head&& h, tail&&... t) {\n std::cout << h; if(sizeof...(tail)) std::cout << ' ';\n return print(std::forward<tail>(t)...);\n}\ntemplate < class T > int print_n(const std::vector< T > a) {\n int n = a.size();\n for(int i : rep(n)) std::cout << a[i] << \"\\n\";\n return 0;\n}\n\n\n#line 2 \"cp-library/src/utility/key_val.hpp\"\n\ntemplate < class K, class V >\nstruct key_val {\n K key; V val;\n key_val() {}\n key_val(K key, V val) : key(key), val(val) {}\n template < std::size_t Index >\n std::tuple_element_t< Index, key_val >& get() {\n if constexpr (Index == 0) return key;\n if constexpr (Index == 1) return val;\n }\n};\n\nnamespace std {\n\ntemplate < class K, class V > struct tuple_size < key_val< K, V > > : integral_constant< size_t, 2 > {};\ntemplate < class K, class V > struct tuple_element < 0, key_val< K, V > > { using type = K; };\ntemplate < class K, class V > struct tuple_element < 1, key_val< K, V > > { using type = V; };\n\n}\n#line 2 \"cp-library/src/utility/vec_op.hpp\"\ntemplate < class T > key_val< int, T > max_of(const vector< T >& a) {\n int i = std::max_element(a.begin(), a.end()) - a.begin();\n return {i, a[i]};\n}\ntemplate < class T > key_val< int, T > min_of(const vector< T >& a) {\n int i = std::min_element(a.begin(), a.end()) - a.begin();\n return {i, a[i]};\n}\ntemplate < class S, class T > S sum_of(const vector< T >& a) {\n S sum = 0;\n for(const T x : a) sum += x;\n return sum;\n}\ntemplate < class S, class T > vector< S > freq_of(const vector< T >& a, T L, T R) {\n vector< S > res(R - L, S(0));\n for(const T x : a) res[x - L] += 1;\n return res;\n}\ntemplate < class S, class T > struct prefix_sum {\n vector< S > s;\n prefix_sum(const vector< T >& a) : s(a) {\n s.insert(s.begin(), S(0));\n for(int i : rep(a.size())) s[i + 1] += s[i];\n }\n // [L, R)\n S sum(int L, int R) { return s[R] - s[L]; }\n};\n#line 3 \"cp-library/src/utility/heap.hpp\"\n\ntemplate < class T > using heap_min = std::priority_queue< T, std::vector< T >, std::greater< T > >;\ntemplate < class T > using heap_max = std::priority_queue< T, std::vector< T >, std::less< T > >;\n\n#line 27 \"cp-library/src/cp-template.hpp\"\n\n#line 1 \"cp-library/src/algorithm/bin_search.hpp\"\ntemplate < class T, class F >\nT bin_search(T ok, T ng, F f) {\n while(abs(ng - ok) > 1) {\n T mid = (ok + ng) / 2;\n (f(mid) ? ok : ng) = mid;\n }\n return ok;\n}\n\ntemplate < class T, class F >\nT bin_search_real(T ok, T ng, F f, int step = 80) {\n while(step--) {\n T mid = (ok + ng) / 2;\n (f(mid) ? ok : ng) = mid;\n }\n return ok;\n}\n#line 2 \"cp-library/src/algorithm/argsort.hpp\"\n\ntemplate < class T > std::vector< int > argsort(const std::vector< T > &a) {\n std::vector< int > ids((int)a.size());\n std::iota(ids.begin(), ids.end(), 0);\n std::sort(ids.begin(), ids.end(), [&](int i, int j) {\n return a[i] < a[j] || (a[i] == a[j] && i < j);\n });\n return ids;\n}\n#line 1 \"macro.hpp\"\nnamespace macro {\n\nusing size_type = int;\ntemplate < class container > void sort(container& a) { std::sort(std:: begin(a), std:: end(a)); }\ntemplate < class container > void rsort(container& a) { std::sort(std::rbegin(a), std::rend(a)); }\ntemplate < class container > void reverse(container& a) { std::reverse(std::begin(a), std::end(a)); }\ntemplate < class container > void unique(container& a) {\n std::sort(std::begin(a), std::end(a));\n a.erase(std::unique(std::begin(a), std::end(a)), std::end(a));\n}\ntemplate < class container > container sorted(const container& a) { container b = a; sort(b); return std::move(b); }\ntemplate < class container > container rsorted(const container& a) { container b = a; rsort(b); return std::move(b); }\ntemplate < class container, class compare > void sort(container& a, const compare& cmp) { std::sort(std::begin(a), std::end(a), cmp); }\ntemplate < class container, class compare > container sorted(const container& a, const compare& cmp) { container b = a; sort(b, cmp); return std::move(b); }\ntemplate < class container, class value > size_type lower_bound(const container& a, const value& x) { return std::lower_bound(std::begin(a), std::end(a), x) - std::begin(a); }\ntemplate < class container, class value > size_type upper_bound(const container& a, const value& x) { return std::upper_bound(std::begin(a), std::end(a), x) - std::begin(a); }\n\nconst std::vector<std::pair<size_type, size_type>> dir4 = { {+1, 0}, {-1, 0}, { 0, +1}, { 0, -1} };\nconst std::vector<std::pair<size_type, size_type>> dir8 = { {-1, -1}, {-1, 0}, {-1, +1}, { 0, -1}, { 0, +1}, {+1, -1}, {+1, 0}, {+1, +1} };\n\n#ifdef _DEBUG\n#define debug(x) std::cout << \"[\" << __LINE__ << \"] \" << #x << \": \" << x << std::endl\n#else\n#define debug(x)\n#endif\n\ntemplate < class container > void concat(container& a, const container& b) {\n a.insert(std::end(a), std::begin(b), std::end(b));\n}\nstd::vector<size_type> iota(const size_type n) {\n std::vector<size_type> I(n);\n std::iota(std::begin(I), std::end(I), 0);\n return I;\n}\ntemplate < class container > std::vector<size_type> sort_idx(const container& a) {\n const size_type n = a.size();\n std::vector<size_type> I = iota(n);\n std::sort(std::begin(I), std::end(I), [&](size_type i, size_type j) { return a[i] < a[j] or (a[i] == a[j] and i < j); });\n return I;\n}\ntemplate < class container, class compare > std::vector<size_type> sort_idx(const container& a, const compare& cmp) {\n const size_type n = a.size();\n std::vector<size_type> I = iota(n);\n std::sort(std::begin(I), std::end(I), [&](size_type i, size_type j) { return cmp(a[i], a[j]) or (a[i] == a[j] and i < j); });\n return std::move(I);\n}\n\nstruct grid {\n using size_type = int;\n size_type H, W;\n grid(const size_type H, const size_type W) : H(H), W(W) {}\n bool contains(const size_type i, const size_type j) {\n return 0 <= i and i < H and 0 <= j and j < W;\n }\n};\n\n} // namespace macro\n\nusing namespace macro;\n#line 1 \"cp-library/src/algebra/sum.hpp\"\ntemplate < class T > class sum_monoid {\n public:\n using set = T;\n static constexpr T op(const T &l, const T &r) { return l + r; }\n static constexpr T id() { return T(0); }\n static constexpr T inv(const T &x) { return -x; }\n static constexpr T pow(const T &x, const ll n) { return x * n; }\n static constexpr bool comm = true;\n};\n#line 4 \"A.cpp\"\n\ntemplate < class Group > class potentialized_union_find {\n public:\n using T = typename Group::set;\n\n private:\n using size_t = std::size_t;\n class node_type {\n friend potentialized_union_find;\n T value;\n size_t parent;\n size_t size;\n\n node_type(const T value, const size_t parent, const size_t size)\n : value(value), parent(parent), size(size) {}\n };\n\n std::vector<node_type> tree;\n size_t size() const { return tree.size(); }\n void compress(const size_t x) {\n const std::size_t p = tree[x].parent;\n if(p == x) return;\n compress(p);\n tree[x].value = Group::op(tree[p].value, tree[x].value);\n tree[x].parent = tree[p].parent;\n }\n\n public:\n potentialized_union_find() = default;\n explicit potentialized_union_find(const size_t n)\n : tree(n, node_type(Group::id(), 0, 1)) {\n for(size_t i = 0; i < n; i++) tree[i].parent = i;\n }\n\n size_t find(const size_t x) {\n assert(x < size());\n compress(x);\n return tree[x].parent;\n }\n T potential(const size_t x) {\n assert(x < size());\n compress(x);\n return tree[x].value;\n }\n bool same(const size_t x, const size_t y) {\n assert(x < size());\n compress(x);\n return find(x) == find(y);\n }\n T distance(const size_t x, const size_t y) {\n assert(x < size());\n assert(y < size());\n return Group::op(Group::inv(potential(x)), potential(y));\n }\n size_t size(const size_t x) {\n assert(x < size());\n return tree[find(x)].size;\n }\n void unite(size_t x, size_t y, T d) {\n if(same(x, y)) return;\n if(size(x) < size(y)) {\n d = Group::inv(d);\n std::swap(x, y);\n }\n d = Group::op(Group::op(potential(x), d), Group::inv(potential(y)));\n x = find(x), y = find(y);\n tree[x].size += tree[y].size;\n tree[y].parent = x;\n tree[y].value = d;\n }\n};\n\nvoid solve(int N, int M) {\n potentialized_union_find<sum_monoid<int>> uf(N);\n for(int M_ : rep(M)) {\n char type = in();\n if(type == '!') {\n int a = in(), b = in(), w = in(); a--, b--;\n uf.unite(a, b, w);\n } else {\n int a = in(), b = in(); a--, b--;\n if(uf.same(a, b)) {\n print(uf.distance(a, b));\n } else {\n print(\"UNKNOWN\");\n }\n printer::flush();\n }\n }\n}\n\nint main() {\n while(true) {\n int N = in(), M = in();\n if(make_pair(N, M) == make_pair(0, 0)) return 0;\n solve(N, M);\n }\n}", "accuracy": 1, "time_ms": 560, "memory_kb": 5816, "score_of_the_acc": -0.6551, "final_rank": 8 }, { "submission_id": "aoj_1330_8723604", "code_snippet": "#define _CRT_SECURE_NO_WARNINGS\n#pragma GCC target \"no-avx\"\n\n#if USE_MP\n#include <boost/multiprecision/cpp_int.hpp>\n#endif\n#include <cassert>\n#include <cstdio>\n#include <iostream>\n#include <iomanip>\n#include <string>\n#include <array>\n#include <vector>\n#include <list>\n#include <map>\n#include <set>\n#include <unordered_set>\n#include <unordered_map>\n#include <queue>\n#include <stack>\n#include <functional>\n#include <algorithm>\n#include <cmath>\n#include <tuple>\n#include <bitset>\n#include <string.h>\n#include <numeric>\n#include <random>\n#if defined _DEBUG\n//#include \"TestCase.h\"\n//#include \"Util.h\"\n#endif\n\nusing namespace std;\n\nusing ll = long long;\nusing ull = unsigned long long;\nusing ld = long double;\ntypedef pair<int, int> pii;\ntypedef pair<ll, ll> pll;\ntypedef tuple<ll, ll, ll> tlll;\ntypedef tuple<int, int, int> tiii;\n\nusing ai2 = array<int, 2>;\nusing ai3 = array<int, 3>;\nusing ai4 = array<int, 4>;\nusing al2 = array<ll, 2>;\nusing al3 = array<ll, 3>;\nusing al4 = array<ll, 4>;\nusing ad2 = array<ld, 2>;\nusing ad3 = array<ld, 3>;\nusing ad4 = array<ld, 4>;\n#define REP(i, n) for (int i = 0; i < (n); i++)\ninline void Yes(bool upper = false) { cout << (upper ? \"YES\" : \"Yes\") << \"\\n\"; }\ninline void No(bool upper = false) { cout << (upper ? \"NO\" : \"No\") << \"\\n\"; }\n\ninline ll DivCeil(ll nume, ll deno)\n{\n\tassert(deno != 0);\n\tif (deno < 0) { nume = -nume; deno = -deno; }\n\tif (nume < 0) return -(-nume / deno);\n\telse return (nume + deno - 1) / deno;\n}\ninline ll DivFloor(ll nume, ll deno)\n{\n\tassert(deno != 0);\n\tif (deno < 0) { nume = -nume; deno = -deno; }\n\tif (nume < 0) return -((-nume + deno - 1) / deno);\n\telse return nume / deno;\n}\ninline ll DivRound(ll nume, ll deno)\n{\n\tassert(deno != 0);\n\tif (deno < 0) { nume = -nume; deno = -deno; }\n\tif (nume < 0) return -((-nume + deno / 2) / deno);\n\telse return (nume + deno / 2) / deno;\n}\n\ntemplate <typename T, int S>\nauto MakeVecImpl(queue<int>& size, const T& ini)\n{\n\tif constexpr (S == 1)\n\t{\n\t\treturn vector<T>(size.front(), ini);\n\t}\n\telse\n\t{\n\t\tint fsize = size.front();\n\t\tsize.pop();\n\t\treturn vector(fsize, MakeVecImpl<T, S - 1>(size, ini));\n\t}\n}\n\ntemplate <typename T, int S>\nauto MakeVec(const array<int, S>& size, const T ini = T())\n{\n\tqueue<int> qsize;\n\tfor (const auto v : size) qsize.push(v);\n\treturn MakeVecImpl<T, S>(qsize, ini);\n}\nint d4[][2] =\n{\n\t{0,-1},\n\t{0,1},\n\t{-1,0},\n\t{1,0},\n};\nint d8[][2] =\n{\n\t{-1,-1},\n\t{0,-1},\n\t{1,-1},\n\n\t{-1,0},\n\t{1,0},\n\n\t{-1,1},\n\t{0,1},\n\t{1,1},\n};\nvector<array<int, 2>> Neighbor2(int y, int x)\n{\n\tvector<array<int, 2>> ret;\n\tret.push_back({ y, x + 1 });\n\tret.push_back({ y + 1, x });\n\treturn ret;\n}\nvector<array<int, 2>> Neighbor4(int y, int x)\n{\n\tvector<array<int, 2>> ret;\n\tret.push_back({ y, x - 1 });\n\tret.push_back({ y, x + 1 });\n\tret.push_back({ y - 1, x });\n\tret.push_back({ y + 1, x });\n\treturn ret;\n}\nvector<array<int, 2>> Neighbor6(int y, int x)\n{\n\tvector<array<int, 2>> ret;\n\tconst int ofs = (y & 1); // 1 - (y & 1); //\n\n\tret.push_back({ y, x - 1 });\n\tret.push_back({ y, x + 1 });\n\n\tret.push_back({ y - 1, x - 1 + ofs });\n\tret.push_back({ y - 1, x + ofs });\n\n\tret.push_back({ y + 1, x - 1 + ofs });\n\tret.push_back({ y + 1, x + ofs });\n\n\treturn ret;\n}\nvector<array<int, 2>> Neighbor8(int y, int x)\n{\n\tvector<array<int, 2>> ret;\n\tret.push_back({ y - 1, x - 1 });\n\tret.push_back({ y - 1, x });\n\tret.push_back({ y - 1, x + 1 });\n\n\tret.push_back({ y, x - 1 });\n\tret.push_back({ y, x + 1 });\n\n\tret.push_back({ y + 1, x - 1 });\n\tret.push_back({ y + 1, x });\n\tret.push_back({ y + 1, x + 1 });\n\treturn ret;\n}\n\nvector<int> DecompDigit(ull val)\n{\n\tif (val == 0)\n\t\treturn { 0 };\n\n\tvector<int> ret;\n\twhile (val)\n\t{\n\t\tret.emplace_back(val % 10);\n\t\tval /= 10;\n\t}\n\treverse(ret.begin(), ret.end());\n\treturn ret;\n}\n\n\n#define PI 3.14159265358979l\nlong double R2D = 180 / PI;\nlong double D2R = PI / 180;\n\nstatic const ll Mod = 1'000'000'007;\nstatic const ll Mod9 = 998244353;// 1000000007;\nstatic const ll INF64 = 4'500000'000000'000000;// 10000000000000000;\nstatic const int INF32 = 2'000'000'000;\n\nrandom_device rd;\nmt19937 mt(0);//(rd());\n#if USE_MP\nusing mpint = boost::multiprecision::cpp_int;\n#endif\n\nstruct UnionFind\n{\npublic:\n\tenum RESULT\n\t{\n\t\tINCONSISTENT = 0,\n\t\tCONSISTENT = 1,\n\t\tADDED = 2,\n\t};\n\n\tUnionFind(int size) :\n\t\tparent(size),\n\t\tnxt(size),\n\t\tnodes(size, 1),\n\t\tpotential(size, 0),\n\t\tgroups(size)\n\t{\n\t\tstd::iota(parent.begin(), parent.end(), 0);\n\t\tstd::iota(nxt.begin(), nxt.end(), 0);\n\t}\n\n\t/// <summary>\n\t/// u -> v にduvの辺を張る\n\t/// 結果はenum RESULTで返すが、失敗(矛盾した辺)を0に割り当てているので\n\t/// if (!uf.Unite(u,v,duv)) {\n\t/// // 失敗時の処理\n\t/// }\n\t/// みたいな使い方ができる\n\t/// </summary>\n\tRESULT Unite(int u, int v, int duv = 0)\n\t{\n\t\t// uの根に対するvの根のポテンシャルを求める\n\t\t// 元々pv-puだった(u,v)の関係をduvにしないといけない\n\t\tint pu = Potential(u);\n\t\tint pv = Potential(v);\n\t\tint druv = duv - (pv - pu);\n\n\t\tint ru = Find(u);\n\t\tint rv = Find(v);\n\t\tif (ru == rv) {\n\t\t\tif (druv == 0)\n\t\t\t\treturn CONSISTENT;\n\t\t\telse\n\t\t\t\treturn INCONSISTENT;\n\t\t}\n\t\tstd::swap(nxt[u], nxt[v]);\t// 列挙用リスト更新\n\n\t\tif (nodes[ru] < nodes[rv])\t// マージテク\n\t\t{\n\t\t\tstd::swap(ru, rv);\n\t\t\tdruv = -druv;\n\t\t}\n\t\tgroups--;\n\t\tnodes[ru] += nodes[rv];\n\t\tpotential[rv] = druv;\n\t\tparent[rv] = ru;\n\t\treturn ADDED;\n\t}\n\n\tint Find(int v)\n\t{\n\t\tif (parent[v] == v)\n\t\t\treturn v;\n\t\telse\n\t\t{\n\t\t\tint rv = Find(parent[v]);\n\t\t\tnodes[v] = nodes[rv];\n\t\t\tpotential[v] += potential[parent[v]];\n\t\t\treturn parent[v] = rv;\n\t\t}\n\t}\n\n\tinline bool IsUnited(int u, int v)\n\t{\n\t\treturn Find(u) == Find(v);\n\t}\n\n\tinline int CountNodes(int v)\n\t{\n\t\tFind(v);\n\t\treturn nodes[v];\n\t}\n\n\tinline int CountGroups() const\n\t{\n\t\treturn groups;\n\t}\n\n\tstd::vector<int> GetGroupMember(int v) const\n\t{\n\t\tstd::vector<int> ret;\n\t\tint w = v;\n\t\tdo {\n\t\t\tret.emplace_back(w);\n\t\t\tw = nxt[w];\n\t\t} while (w != v);\n\t\treturn ret;\n\t}\n\n\tlong long Potential(int v)\n\t{\n\t\tFind(v);\n\t\treturn potential[v];\n\t}\n\n\t// uから見たvのポテンシャル\n\tlong long Diff(int u, int v)\n\t{\n\t\treturn Potential(v) - Potential(u);\n\t}\n\nprivate:\n\tstd::vector<int> parent;\t// 親ノード\n\tstd::vector<int> nodes;\t\t// 同じグループに属するノードの数\n\tstd::vector<int> nxt;\t\t// 同じグループに属する次のノード(ループしている)\n\tstd::vector<long long> potential;\t// ポテンシャル\n\tint groups;\n};\n\nconst double EPS = 1e-10;\n\nstruct Solver\n{\n\tint Run()\n\t{\n\t\tint N, M;\n\t\tcin >> N >> M;\n\t\tif (N == 0 && M == 0)\n\t\t\treturn 1;\n\n\t\tUnionFind uf(N);\n\t\tfor (int i = 0; i < M; i++) {\n\t\t\tchar c;\n\t\t\tcin >> c;\n\t\t\tif (c == '!') {\n\t\t\t\tint u, v, w;\n\t\t\t\tcin >> u >> v >> w;\n\t\t\t\tu--, v--;\n\t\t\t\tuf.Unite(u, v, w);\n\t\t\t}\n\t\t\telse {\n\t\t\t\tint u, v;\n\t\t\t\tcin >> u >> v;\n\t\t\t\tu--, v--;\n\n\t\t\t\tif (uf.IsUnited(u, v)) {\n\t\t\t\t\tcout << uf.Diff(u, v) << \"\\n\";\n\t\t\t\t}\n\t\t\t\telse {\n\t\t\t\t\tcout << \"UNKNOWN\\n\";\n\t\t\t\t}\n\t\t\t}\n\n\n\t\t}\n\n\n\t\treturn 0;\n\t}\n};\n\nint main()\n{\n\tstd::cin.tie(nullptr); //★インタラクティブ注意★\n\tstd::ios_base::sync_with_stdio(false);\n\tint T = 1;\n\t//cin >> T;\n\twhile (1)//T--)\n\t{\n\t\tSolver S;\n\t\tif (S.Run())\n\t\t\treturn 0;\n\t}\n\n\treturn 0;\n}\n\n/// 値渡しになっていないか?\n/// 入力を全部読んでいるか? 途中でreturnしない\n/// 32bitで収まるか? 10^5数えるとき\n/// modは正しいか?\n/// multisetでcountしていないか?", "accuracy": 1, "time_ms": 200, "memory_kb": 5092, "score_of_the_acc": -0.2013, "final_rank": 2 }, { "submission_id": "aoj_1330_8572359", "code_snippet": "// #define _GLIBCXX_DEBUG\n#include <bits/stdc++.h>\nusing namespace std;\n\n/* alias*/\nusing ull = unsigned long long;\nusing ll = long long;\nusing vi = vector<int>;\nusing vll = vector<long long>;\nusing vs = vector<string>;\nusing vvi = vector<vi>;\nusing vvll = vector<vll>;\nusing vvs = vector<vs>;\nusing pii = pair<int, int>;\nusing vpii = vector<pii>;\n\n/* define short */\n#define pb push_back\n#define mp make_pair\n#define reps(i, s, n) for (int i = (s); i < (int)(n); i++)\n#define rreps(i, s, n) for (int i = n-1; i >= (s); i--)\n#define rep(i, n) reps(i, 0, n)\n#define rep1(i, n) reps(i, 1, n+1)\n#define rrep(i, n) rreps(i, 0, n)\n#define rrep1(i, n) rreps(i, 1, n+1)\n#define all(a) (a).begin(), (a).end()\n#define rall(a) (a).rbegin(), (a).rend()\n\n\n/* func */\ntemplate<class T> inline bool chmax(T& a, T b) { return ((a < b) ? (a = b, true) : (false)); }\ntemplate<class T> inline bool chmin(T& a, T b) { return ((a > b) ? (a = b, true) : (false)); }\n\ntemplate<class T> struct WeightedUnionFind {\n vector<int> par, rank;\n vector<T> diff_weight;\n\n WeightedUnionFind(int n = 1) {\n init(n);\n }\n\n void init(int n = 1) {\n par.resize(n); rank.resize(n); diff_weight.resize(n);\n for (int i = 0; i < n; ++i) {\n par[i] = i;\n rank[i] = 0;\n diff_weight[i] = 0;\n }\n }\n\n int root(int x) {\n if (par[x] == x) return x;\n else {\n int r = root(par[x]);\n diff_weight[x] += diff_weight[par[x]];\n return par[x] = r;\n }\n }\n\n T weight(int x) {\n root(x);\n return diff_weight[x];\n }\n\n T diff(int x, int y) {\n return weight(y) - weight(x);\n }\n\n bool same(int x, int y) {\n return root(x) == root(y);\n }\n\n bool unite(int x, int y, T w) {\n w += weight(x); w -= weight(y);\n x = root(x); y = root(y);\n if (x == y) return false;\n if (rank[x] < rank[y]) swap(x, y), w = -w; // ry を rx の子にする\n if (rank[x] == rank[y]) ++rank[x];\n par[y] = x;\n diff_weight[y] = w;\n return true;\n }\n};\n\nint main() { \n int n,m;\n while (true) {\n cin>>n>>m;\n if (n == 0) break;\n WeightedUnionFind<int> uf(n);\n rep(_,m) {\n char s;int a,b,w;\n cin>>s;\n if (s == '!') {\n cin>>a>>b>>w; --a;--b;\n uf.unite(a,b,w);\n }\n if (s == '?') {\n cin>>a>>b; --a;--b;\n if (uf.same(a,b)) cout << uf.diff(a,b) << endl;\n else cout << \"UNKNOWN\" << endl;\n }\n }\n }\n return 0;\n}", "accuracy": 1, "time_ms": 910, "memory_kb": 4316, "score_of_the_acc": -0.7998, "final_rank": 12 }, { "submission_id": "aoj_1330_8526304", "code_snippet": "// #define _GLIBCXX_DEBUG 1\n#include <bits/stdc++.h>\n// #include <atcoder/all>\nusing namespace std;\n#define ll long long\n#define ull unsigned long long\n\n#define rep1(i, n) for (int i = 1; i <= n; i++)\n#define rep_0(i, n) for (int i = 0; i < n; i++)\n#define rep_1(i, s, n) for (int i = s; i < n + s; i++)\n#define rep_2(i, s, t, n) for (int i = s; i <= n; i += t)\n#define rep_overload(e1, e2, e3, e4, f, ...) f\n#define rep(...) rep_overload(__VA_ARGS__, rep_2, rep_1, rep_0)(__VA_ARGS__)\n#define repr(i, n) for (int i = n - 1; i >= 0; i--)\n#define repr1(i, n) for (int i = n; i > 0; i--)\n\nauto chmax = [](auto& a, auto b) {bool ret=a<b; if(ret)a=b; return ret; };\nauto chmin = [](auto& a, auto b) {bool ret=a>b; if(ret)a=b; return ret; };\ntemplate <typename T>\nusing pq_inv = priority_queue<T, vector<T>, greater<T>>;\nvector<int> dd4 = {1, 0, -1, 0, 1}; // rep(i,4) nh=h+dd4[i]; nw=w+dd4[i+1];\nvector<int> dd8 = {1, 0, -1, 0, 1, 1, -1, -1, 1}; // rep(i,8) nh=h+dd8[i]; nw=w+dd8[i+1];\n// using mint = atcoder::modint1000000007;\n// using mint = atcoder::modint998244353;\n\ntemplate <typename T>\nstruct WeightedUnionFind {\n vector<int> par, rank;\n vector<T> diff_weight;\n WeightedUnionFind(int n) {\n init(n);\n }\n void init(int n) {\n par.assign(n, -1);\n rank.assign(n, 0);\n diff_weight.assign(n, 0);\n }\n int root(int x) {\n if (par[x] == -1) return x;\n int r = root(par[x]);\n diff_weight[x] += diff_weight[par[x]];\n return par[x] = r;\n }\n T weight(int x) {\n root(x);\n return diff_weight[x];\n }\n\n bool is_same(int x, int y) {\n return root(x) == root(y);\n }\n bool merge(int x, int y, T w) {\n w += weight(x) - weight(y);\n x = root(x);\n y = root(y);\n if (x == y) return false;\n if (rank[x] < rank[y]) swap(x, y), w = -w;\n if (rank[x] == rank[y]) rank[x]++;\n par[y] = x;\n diff_weight[y] = w;\n return true;\n }\n\n T diff(int x, int y) {\n return weight(y) - weight(x);\n }\n};\n\nvoid solve(int N, int M) {\n WeightedUnionFind<ll> wuf(N + 1);\n rep(i, M) {\n char t;\n cin >> t;\n if (t == '!') {\n int x, y, d;\n cin >> x >> y >> d;\n wuf.merge(x, y, d);\n } else {\n int x, y;\n cin >> x >> y;\n if(wuf.is_same(x,y))cout << wuf.diff(x, y) << endl;\n else cout << \"UNKNOWN\" << endl;\n }\n }\n}\n\nint main() {\n std::cin.tie(nullptr);\n // int T;\n // // cin >> T;\n // T = 1;\n // for (int i = 0; i < T; i++) {\n // // solve(i);\n // solve();\n // }\n\n while (true) {\n int N, M;\n cin >> N >> M;\n if (N == 0 && M == 0)\n break;\n else\n (solve(N, M));\n }\n return 0;\n}", "accuracy": 1, "time_ms": 860, "memory_kb": 4460, "score_of_the_acc": -0.7697, "final_rank": 11 }, { "submission_id": "aoj_1330_8505550", "code_snippet": "#include<bits/stdc++.h>\n\nusing namespace std;\n\n#define rep(i, n) for(int i = 0; i < (n); i++)\n#define per(i, n) for(int i = (n)-1; i >= 0; i--)\n#define rep2(i, l, r) for(int i = (l); i < (r); i++)\n#define per2(i, l, r) for(int i = (r)-1; i >= (l); i--)\n#define each(e, x) for(auto &e : x)\n#define sz(x) (int)x.size()\n#define all(x) begin(x), end(x)\n#define rall(x) rbegin(x), rend(x)\n#define eb emplace_back\n#define MM << ' ' <<\n#define TT template<typename T>\nusing ll = long long;\nusing pii = pair<int, int>;\nusing pll = pair<ll, ll>;\n\nTT bool chmin(T &x, T y) { return x > y ? (x = y, true) : false; }\n\nTT bool chmax(T &x, T y) { return x < y ? (x = y, true) : false; }\n\nTT using minheap = priority_queue<T, vector<T>, greater<T>>;\nTT using maxheap = priority_queue<T>;\n\nTT void print(vector<T> v, T x = 0) {\n int n = sz(v);\n rep(i, n) cout << v[i] + x << (i == n - 1 ? '\\n' : ' ');\n if (n == 0) cout << '\\n';\n}\n\nTT void printn(vector<T> v, T x = 0) {\n int n = sz(v);\n rep(i, n) cout << v[i] + x << '\\n';\n}\n\nTT void rearrange(vector<T> &v) {\n sort(all(v));\n v.erase(unique(all(v)), end(v));\n}\n\nTT int lb(vector<T> &v, T x) { return lower_bound(all(v), x) - begin(v); }\n\nTT int ub(vector<T> &v, T x) { return upper_bound(all(v), x) - begin(v); }\n\nconst int inf = (1 << 30) - 1;\nconst ll INF = (1LL << 60) - 1;\n\nstruct io_setup {\n io_setup() {\n ios_base::sync_with_stdio(false);\n cin.tie(NULL);\n cout << fixed << setprecision(15);\n }\n} io_setup;\n\nTT struct weighted_uf {\n vector<int> data;\n vector<T> ws;\n const int n;\n weighted_uf(int n) : data(n, -1), ws(n, 0), n(n) {}\n\n int root(int x) {\n if (data[x] < 0) return x;\n int par = root(data[x]);\n ws[x] += ws[data[x]];\n return data[x] = par;\n }\n\n T weight(int x) {\n root(x);\n return ws[x];\n }\n\n bool unite(int x, int y, T w) {\n w += weight(x), w -= weight(y);\n x = root(x), y = root(y);\n if (x == y) return false;\n data[x] += data[y], data[y] = x;\n ws[y] = w;\n return true;\n }\n\n T diff(int x, int y) { return weight(y) - weight(x); }\n\n bool same(int x, int y) { return root(x) == root(y); }\n};\n\nint main() {\n while (1) {\n int n, q;\n cin >> n >> q;\n if (!n)\n break;\n weighted_uf<ll> uf(n);\n while (q--) {\n char type;\n ll x, y;\n cin >> type >> x >> y;\n x--, y--;\n if (type == '!') {\n ll w;\n cin >> w;\n uf.unite(x, y, w);\n }\n else {\n if (uf.same(x, y))\n cout << uf.diff(x, y) << '\\n';\n else\n cout << \"UNKNOWN\\n\";\n }\n }\n }\n}", "accuracy": 1, "time_ms": 200, "memory_kb": 6716, "score_of_the_acc": -0.4198, "final_rank": 6 }, { "submission_id": "aoj_1330_8487917", "code_snippet": "#include <bits/stdc++.h>\n#define rep(i, n) for (int i = 0; i < n; ++i)\ntypedef long long ll;\nusing namespace std;\n\ntemplate <class Abel>\nstruct UnionFind {\n vector<int> par;\n vector<int> rank;\n vector<Abel> diff_weight;\n UnionFind(int n = 1, Abel SUM_UNITY = 0) {\n init(n, SUM_UNITY);\n }\n void init(int n = 1, Abel SUM_UNITY = 0) {\n par.resize(n);\n rank.resize(n);\n diff_weight.resize(n);\n for (int i = 0; i < n; ++i) par[i] = i, rank[i] = 0, diff_weight[i] = SUM_UNITY;\n }\n int root(int x) {\n if (par[x] == x) {\n return x;\n } else {\n int r = root(par[x]);\n diff_weight[x] += diff_weight[par[x]];\n return par[x] = r;\n }\n }\n Abel weight(int x) {\n root(x);\n return diff_weight[x];\n }\n bool issame(int x, int y) {\n return root(x) == root(y);\n }\n bool merge(int x, int y, Abel w) {\n w += weight(x);\n w -= weight(y);\n x = root(x);\n y = root(y);\n if (x == y) return false;\n if (rank[x] < rank[y]) swap(x, y), w = -w;\n if (rank[x] == rank[y]) ++rank[x];\n par[y] = x;\n diff_weight[y] = w;\n return true;\n }\n Abel diff(int x, int y) {\n return weight(y) - weight(x);\n }\n};\n\nvoid solve(int N, int M) {\n UnionFind<ll> uf(N);\n while (M--) {\n char c;\n cin >> c;\n if (c == '!') {\n int l, r;\n ll w;\n cin >> l >> r >> w;\n l--, r--;\n uf.merge(l, r, w);\n } else if (c == '?') {\n int l, r;\n cin >> l >> r;\n l--, r--;\n\n if (uf.issame(l, r)) cout << uf.diff(l, r) << \"\\n\";\n else cout << \"UNKNOWN\\n\";\n }\n }\n}\n\nint main() {\n cin.tie(0)->sync_with_stdio(0);\n\n int N, M;\n while (cin >> N >> M, N) solve(N, M);\n\n return 0;\n}", "accuracy": 1, "time_ms": 200, "memory_kb": 4552, "score_of_the_acc": -0.1286, "final_rank": 1 }, { "submission_id": "aoj_1330_8483434", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n#include <iostream>\n#include <vector>\nusing ll = long long;\nusing ld = long double;\nusing Graph = vector<vector<int>>;\nusing vi = vector<int>;\nusing vl = vector<long>;\nusing vll = vector<long long>;\nusing vb = vector<bool>;\nusing vvi = vector<vi>;\nusing vvl = vector<vl>;\nusing vvb = vector<vb>;\nusing vvvb = vector<vvb>;\nusing vvll = vector<vll>;\nusing vvvll = vector<vvll>;\nusing vd = vector<double>;\nusing vvd = vector<vd>;\nusing vvvd = vector<vvd>;\nusing vc = vector<char>;\nusing vvc = vector<vc>;\nusing lll = __int128_t;\nusing vs = vector<string>;\nusing pii = pair<long long, long long>;\nconst long double EPS = 1e-10;\nconst double INF = 1e18;\n\nconst long double PI = acos(-1.0L);\n#define mp make_pair\n#define reps(i, a, n) for (ll i = (a); i < (ll)(n); i++)\n#define rep(i, n) for (ll i = (0); i < (ll)(n); i++)\n#define rrep(i, n) for (ll i = (1); i < (ll)(n + 1); i++)\n#define repd(i, n) for (ll i = n - 1; i >= 0; i--)\n#define rrepd(i, n) for (ll i = n; i >= 1; i--)\n#define ALL(n) n.begin(), n.end()\n#define rALL(n) n.rbegin(), n.rend()\n#define fore(i, a) for (auto &i : a)\n#define IN(a, x, b) (a <= x && x < b)\n#define IN(a, x, b) (a <= x && x < b)\n#define INIT \\\n std::ios::sync_with_stdio(false); \\\n std::cin.tie(0);\ntemplate <class T>\ninline T CHMAX(T &a, const T b) {\n return a = (a < b) ? b : a;\n}\ntemplate <class T>\ninline T CHMIN(T &a, const T b) {\n return a = (a > b) ? b : a;\n}\n#include <algorithm> // min, max, swap, sort, reverse, lower_bound, upper_bound\n#include <bitset> // bitset\n#include <cctype> // isupper, islower, isdigit, toupper, tolower\n#include <cstdint> // int64_t, int*_t\n#include <cstdio> // printf\n#include <deque> // deque\n#include <iostream> // cout, endl, cin\n#include <map> // map\n#include <queue> // queue, priority_queue\n#include <set> // set\n#include <stack> // stack\n#include <string> // string, to_string, stoi\n#include <tuple> // tuple, make_tuple\n#include <unordered_map> // unordered_map\n#include <unordered_set> // unordered_set\n#include <utility> // pair, make_pair\n#include <vector> // vector\nusing namespace std;\n\n#include <type_traits>\n\n#define LOOP(n) REPI($_, (n))\n\n#define REPI(i, n) \\\n for (int i = 0, i##_length = static_cast<int>(n); i < i##_length; ++i)\n#define REPF(i, l, r) \\\n for (std::common_type_t<decltype(l), decltype(r)> i = (l), i##_last = (r); \\\n i < i##_last; ++i)\n#define REPIS(i, l, r, s) \\\n for (std::common_type_t<decltype(l), decltype(r), decltype(s)> \\\n i = (l), \\\n i##_last = (r); \\\n i < i##_last; i += (s))\n\n#define REPR(i, n) for (auto i = (n); --i >= 0;)\n#define REPB(i, l, r) \\\n for (std::common_type_t<decltype(l), decltype(r)> i = (r), i##_last = (l); \\\n --i >= i##_last;)\n#define REPRS(i, l, r, s) \\\n for (std::common_type_t<decltype(l), decltype(r), decltype(s)> \\\n i = (l) + ((r) - (l)-1) / (s) * (s), \\\n i##_last = (l); \\\n i >= i##_last; (i -= (s)))\n\n#define REP(...) $OVERLOAD4(__VA_ARGS__, REPIS, REPF, REPI, LOOP)(__VA_ARGS__)\n#define REPD(...) $OVERLOAD4(__VA_ARGS__, REPRS, REPB, REPR)(__VA_ARGS__)\n\n#define FORO(i, n) \\\n for (int i = 0, i##_last = static_cast<int>(n); i <= i##_last; ++i)\n#define FORI(i, l, r) \\\n for (std::common_type_t<decltype(l), decltype(r)> i = (l), i##_last = (r); \\\n i <= i##_last; ++i)\n#define FORIS(i, l, r, s) \\\n for (std::common_type_t<decltype(l), decltype(r), decltype(s)> \\\n i = (l), \\\n i##_last = (r); \\\n i <= i##_last; i += (s))\n\n#define FORRO(i, n) for (auto i = (n); i >= 0; --i)\n#define FORR(i, l, r) \\\n for (std::common_type_t<decltype(l), decltype(r)> i = (r), i##_last = (l); \\\n i >= i##_last; --i)\n#define FORRS(i, l, r, s) \\\n for (std::common_type_t<decltype(l), decltype(r), decltype(s)> \\\n i = (l) + ((r) - (l)) / (s) * (s), \\\n i##_last = (l); \\\n i >= i##_last; i -= (s))\n\n#define FOR(...) $OVERLOAD4(__VA_ARGS__, FORIS, FORI, FORO)(__VA_ARGS__)\n#define FORD(...) $OVERLOAD4(__VA_ARGS__, FORRS, FORR, FORRO)(__VA_ARGS__)\n\n#define ITR1(e0, v) for (const auto &e0 : (v))\n#define ITRP1(e0, v) for (auto e0 : (v))\n#define ITRR1(e0, v) for (auto &e0 : (v))\n\n#define ITR2(e0, e1, v) for (const auto [e0, e1] : (v))\n#define ITRP2(e0, e1, v) for (auto [e0, e1] : (v))\n#define ITRR2(e0, e1, v) for (auto &[e0, e1] : (v))\n\n#define ITR3(e0, e1, e2, v) for (const auto [e0, e1, e2] : (v))\n#define ITRP3(e0, e1, e2, v) for (auto [e0, e1, e2] : (v))\n#define ITRR3(e0, e1, e2, v) for (auto &[e0, e1, e2] : (v))\n\n#define ITR4(e0, e1, e2, e3, v) for (const auto [e0, e1, e2, e3] : (v))\n#define ITRP4(e0, e1, e2, e3, v) for (auto [e0, e1, e2, e3] : (v))\n#define ITRR4(e0, e1, e2, e3, v) for (auto &[e0, e1, e2, e3] : (v))\n\n#define ITR(...) $OVERLOAD5(__VA_ARGS__, ITR4, ITR3, ITR2, ITR1)(__VA_ARGS__)\n#define ITRP(...) \\\n $OVERLOAD5(__VA_ARGS__, ITRP4, ITRP3, ITRP2, ITRP1)(__VA_ARGS__)\n#define ITRR(...) \\\n $OVERLOAD5(__VA_ARGS__, ITRR4, ITRR3, ITRR2, ITRR1)(__VA_ARGS__)\nstd::ostream &operator<<(std::ostream &dest, __int128_t value) {\n std::ostream::sentry s(dest);\n if (s) {\n __uint128_t tmp = value < 0 ? -value : value;\n char buffer[128];\n char *d = std::end(buffer);\n do {\n --d;\n *d = \"0123456789\"[tmp % 10];\n tmp /= 10;\n } while (tmp != 0);\n if (value < 0) {\n --d;\n *d = '-';\n }\n int len = std::end(buffer) - d;\n if (dest.rdbuf()->sputn(d, len) != len) {\n dest.setstate(std::ios_base::badbit);\n }\n }\n return dest;\n}\ntemplate <class Type>\nclass WeightedUnionFind {\n public:\n WeightedUnionFind() = default;\n\n /// @brief 重み付き Union-Find 木を構築します。\n /// @param n 要素数\n explicit WeightedUnionFind(size_t n)\n : m_parentsOrSize(n, -1), m_diffWeights(n) {}\n\n /// @brief 頂点 i の root のインデックスを返します。\n /// @param i 調べる頂点のインデックス\n /// @return 頂点 i の root のインデックス\n int find(int i) {\n if (m_parentsOrSize[i] < 0) {\n return i;\n }\n\n const int root = find(m_parentsOrSize[i]);\n\n m_diffWeights[i] += m_diffWeights[m_parentsOrSize[i]];\n\n // 経路圧縮\n return (m_parentsOrSize[i] = root);\n }\n\n /// @brief a のグループと b のグループを統合します。\n /// @param a 一方のインデックス\n /// @param b 他方のインデックス\n /// @param w (b の重み) - (a の重み)\n /// a を含むグループと b を含むグループを併合し、b の重みは a と比べて w\n /// 大きくなるようにする\n void merge(int a, int b, Type w) {\n w += weight(a);\n w -= weight(b);\n\n a = find(a);\n b = find(b);\n\n if (a != b) {\n // union by size (小さいほうが子になる)\n if (-m_parentsOrSize[a] < -m_parentsOrSize[b]) {\n std::swap(a, b);\n w = -w;\n }\n\n m_parentsOrSize[a] += m_parentsOrSize[b];\n m_parentsOrSize[b] = a;\n m_diffWeights[b] = w;\n }\n }\n\n /// @brief (b の重み) - (a の重み) を返します。\n /// @param a 一方のインデックス\n /// @param b 他方のインデックス\n /// @remark a と b が同じグループに属さない場合の結果は不定です。\n /// @return (b の重み) - (a の重み)\n ///(b の重み) - (a の重み) を返す。a と b\n /// が同じグループに属さない場合の結果は不定\n Type diff(int a, int b) { return (weight(b) - weight(a)); }\n\n /// @brief a と b が同じグループに属すかを返します。\n /// @param a 一方のインデックス\n /// @param b 他方のインデックス\n /// @return a と b が同じグループに属す場合 true, それ以外の場合は false\n /// a と b が同じグループに属すかを返す\n bool connected(int a, int b) { return (find(a) == find(b)); }\n\n /// @brief i が属するグループの要素数を返します。\n /// @param i インデックス\n /// @return i が属するグループの要素数\n int size(int i) { return -m_parentsOrSize[find(i)]; }\n\n private:\n // m_parentsOrSize[i] は i の 親,\n // ただし root の場合は (-1 * そのグループに属する要素数)\n std::vector<int> m_parentsOrSize;\n\n // 重み\n std::vector<Type> m_diffWeights;\n\n Type weight(int i) {\n find(i); // 経路圧縮\n return m_diffWeights[i];\n }\n};\n\n__int128 parse(string &s) {\n __int128 ret = 0;\n for (int i = 0; i < s.length(); i++)\n if ('0' <= s[i] && s[i] <= '9') ret = 10 * ret + s[i] - '0';\n return ret;\n}\n\nll GCD(ll m, ll n) {\n // ベースケース\n if (n == 0) return m;\n\n // 再帰呼び出し\n return GCD(n, m % n);\n}\n\nll minlong = 0;\n\nlong long Power(long long a, long long b, long long m) {\n long long p = a, Answer = 1;\n for (int i = 0; i < 63; i++) {\n ll wari = (1LL << i);\n if ((b / wari) % 2 == 1) {\n Answer %= m;\n Answer = (Answer * p) % m; // 「a の 2^i 乗」が掛けられるとき\n }\n p %= m;\n p = (p * p) % m;\n }\n return Answer;\n}\n\n// a ÷ b を m で割った余りを返す関数\nlong long Division(long long a, long long b, long long m) {\n return (a * Power(b, m - 2, m)) % m;\n}\n\n// nCr mod 998244353 を返す関数\nlong long nCk(ll n, ll r) {\n const long long M = 998244353;\n\n // 手順 1: 分子 a を求める\n long long a = 1;\n for (int i = 1; i <= n; i++) a = (a * i) % M;\n\n // 手順 2: 分母 b を求める\n long long b = 1;\n for (int i = 1; i <= r; i++) b = (b * i) % M;\n for (int i = 1; i <= n - r; i++) b = (b * i) % M;\n\n // 手順 3: 答えを求める\n return Division(a, b, M);\n}\nusing Interval = pair<ll, ll>;\n// 終点時間でsortをかけるのに必要(区間スケジューリング問題など)\nbool cmp(const Interval &a, const Interval &b) { return a.second < b.second; }\nvll dycstra(vector<vector<pair<ll, ll>>> G, ll N, ll K) {\n vb kaku(N, false);\n vll cur(N, INF);\n cur[K] = 0;\n priority_queue<pair<ll, ll>, vector<pair<ll, ll>>, greater<pair<ll, ll>>> Q;\n Q.push(make_pair(cur[K], K));\n while (!Q.empty()) {\n ll pos = Q.top().second;\n Q.pop();\n if (kaku[pos]) continue;\n kaku[pos] = true;\n for (ll i = 0; i < G[pos].size(); i++) {\n ll nex = G[pos][i].first;\n ll cost = G[pos][i].second;\n if (cur[nex] > cur[pos] + cost) {\n cur[nex] = cur[pos] + cost;\n Q.push(make_pair(cur[nex], nex));\n }\n }\n }\n return cur;\n}\ntemplate <typename T>\nvoid Print(vector<T> &A) {\n rep(i, A.size()) { cout << A[i] << ' '; }\n cout << endl;\n}\ntemplate <typename xy>\nclass BIT {\n public:\n BIT() = default;\n\n // 長さ size の数列で初期化\n explicit BIT(size_t size) : m_bit(size + 1) {}\n\n // 数列で初期化\n explicit BIT(const std::vector<xy> &v) : BIT(v.size()) {\n for (int i = 0; i < v.size(); ++i) {\n add((i + 1), v[i]);\n }\n }\n\n // 閉区間 [1, r] の合計を返す (1-based indexing)\n xy sum(int r) const {\n xy ret = 0;\n\n for (; 0 < r; r -= (r & -r)) {\n ret += m_bit[r];\n }\n\n return ret;\n }\n\n // 閉区間 [l, r] の合計を返す (1-based indexing)\n xy sum(int l, int r) const { return (sum(r) - sum(l - 1)); }\n\n // 数列の i 番目の要素を加算 (1-based indexing)\n void add(int i, xy value) {\n for (; i < m_bit.size(); i += (i & -i)) {\n m_bit[i] += value;\n }\n }\n\n private:\n std::vector<xy> m_bit;\n};\n\n// Binary Indexed Tree (1.2 区間加算対応)\n// 1-based indexing\ntemplate <typename x>\nclass BIT_RAQ {\n public:\n BIT_RAQ() = default;\n\n explicit BIT_RAQ(size_t size) : m_bit0(size), m_bit1(size) {}\n\n explicit BIT_RAQ(const std::vector<x> &v) : m_bit0(v), m_bit1(v.size()) {}\n\n // 閉区間 [1, r] の合計を返す (1-based indexing)\n x sum(int r) const { return (m_bit0.sum(r) + m_bit1.sum(r) * r); }\n\n // 閉区間 [l, r] の合計を返す (1-based indexing)\n x sum(int l, int r) const { return (sum(r) - sum(l - 1)); }\n\n // 数列の i 番目の要素を加算 (1-based indexing)\n void add(int i, x value) { m_bit0.add(i, value); }\n\n // 閉区間 [l, r] の要素を加算 (1-based indexing)\n void add(int l, int r, x value) {\n x a = (-value * (l - 1)), b = (value * r);\n m_bit0.add(l, a);\n m_bit0.add((r + 1), b);\n m_bit1.add(l, value);\n m_bit1.add((r + 1), -value);\n }\n\n private:\n BIT<x> m_bit0;\n\n BIT<x> m_bit1;\n};\nvll BFS(vvll &G, ll &x) {\n vll cut(G.size(), INF);\n queue<ll> Q;\n Q.push(x);\n cut[x] = 0;\n while (!Q.empty()) {\n ll a = Q.front();\n Q.pop();\n rep(i, G[a].size()) {\n if (cut[G[a][i]] > cut[a] + 1) {\n cut[G[a][i]] = cut[a] + 1;\n Q.push(G[a][i]);\n }\n }\n }\n return cut;\n}\n\nvoid Yes(bool b) {\n if (b)\n cout << \"Yes\" << endl;\n else\n cout << \"No\" << endl;\n}\nvector<long long> yaku(long long n) {\n vector<long long> ret;\n for (long long i = 1; i * i <= n; i++) {\n if (n % i == 0) {\n ret.push_back(i);\n if (i * i != n) ret.push_back(n / i);\n }\n }\n sort(ret.begin(), ret.end()); // 昇順に並べる\n return ret;\n}\n\npriority_queue<pair<ll, ll>, vector<pair<ll, ll>>, greater<pair<ll, ll>>> Q;\n\ndouble randouble() { return 1.0 * rand() / RAND_MAX; }\nstruct Mo {\n int n;\n vector<pair<int, int>> lr;\n\n explicit Mo(int n) : n(n) {} // nの値の設定を行う。\n\n void add(int l, int r) { /* [l, r) */\n lr.push_back({l, r});\n }\n\n template <typename AL, typename AR, typename EL, typename ER, typename O>\n void build(const AL &add_left, const AR &add_right, const EL &erase_left,\n const ER &erase_right, const O &out) {\n int q = (int)lr.size(); // lr.size()はクエリの数に等しい。\n int bs = n / min<int>(n, sqrt(q)); // 事実上bs>0となるように設定、\n // cout << bs << endl;\n vector<int> ord(q);\n iota(begin(ord), end(ord), 0);\n sort(begin(ord), end(ord), [&](int a, int b) {\n int ablock = lr[a].first / bs, bblock = lr[b].first / bs;\n if (ablock != bblock) return ablock < bblock; //\n return (ablock & 1) ? lr[a].second > lr[b].second\n : lr[a].second < lr[b].second;\n // 偶奇で昇順・降順を分けることによって計算量が定数倍高速化する。\n });\n int l = 0, r = 0;\n for (auto idx : ord) {\n // cout << idx << endl;\n while (l > lr[idx].first) add_left(--l);\n while (r < lr[idx].second) add_right(r++);\n while (l < lr[idx].first) erase_left(l++);\n while (r > lr[idx].second) erase_right(--r);\n out(idx);\n }\n }\n\n template <typename A, typename E, typename O>\n void build(const A &add, const E &erase, const O &out) {\n build(add, add, erase, erase, out);\n }\n};\n\nstruct TRI {\n ll a;\n ll b;\n ll c;\n};\nbool operator>(const TRI &r, const TRI &l) {\n return (r.a > l.a | (r.a == l.a & r.b > l.b) |\n (r.a == l.a & r.b == l.b & r.c > l.c));\n}\nbool operator<(const TRI &r, const TRI &l) {\n return (r.a < l.a | (r.a == l.a & r.b < l.b) |\n (r.a == l.a & r.b == l.b & r.c < l.c));\n}\nstruct TR {\n ll a;\n ll b;\n ll c;\n ll d;\n};\nbool operator>(const TR &r, const TR &l) {\n return (r.a > l.a | (r.a == l.a & r.b > l.b) |\n (r.a == l.a & r.b == l.b & r.c > l.c));\n}\nbool operator<(const TR &r, const TR &l) {\n return (r.a < l.a | (r.a == l.a & r.b < l.b) |\n (r.a == l.a & r.b == l.b & r.c < l.c));\n}\n// Sはdataを表している。\nusing S = ll;\n// Fはlazy(遅延伝搬用)を表している\nusing F = ll;\n// 区間取得をどのようにするかを定義する。RMQだったらmin(a,b)とかになる。\nS op(S a, S b) { return a + b; };\n// op(e,a)=op(a,e)=aとなるような単位元を定義する。RMQの場合はINFが安牌\nS e() { return 0; }\n// 親のlazyが子のdataに対してどの様に作用させるかを定義する。区間加算をする場合はただ足せば良い\nS mapping(F f, S x) { return x + f; }\n/*親のlazyが子のlazyに対してどの様に作用させるかを定義する。区間加算をする場合はただ足せば良い\nただ可変では無い場合fが後の操作である事を留意しておくと良い*/\nF composition(F f, F g) { return f + g; }\n// mapping(a,id)=aとなる様なidを設定すれば良い今回の区間加算の場合は0が適する。区間乗算の場合は1が適する\nF id() { return 0; }\nll half = 499122177;\n// 条件を満たす場合オイラーツアーを考えれば出来る。\n// 重要なのは求める次数を満たす辺を構築すること\n// G:グラフ,P:オイラーツアーで探索した際に通った辺の番号の配列,in,out:最初と最後に通った時刻\n// U[辺の番号]={親,現在いる位置}正の値だったら終点、それ以外親を出力することで頂点で表した経路の復元が可能\n// num=辺の番号を記録,cou=時刻を記録\n\n// 返り値: a と b の最大公約数\n// ax + by = gcd(a, b) を満たす (x, y) が格納される\nlong long extGCD(long long a, long long b, long long &x, long long &y) {\n if (b == 0) {\n x = 1;\n y = 0;\n return a;\n }\n long long d = extGCD(b, a % b, y, x);\n y -= a / b * x;\n return d;\n}\nvll toposo(vvll &G) {\n // 帰りがけでやりたい場合はstack、幅優先でやりたい場合はqueueで行う\n stack<ll> Q;\n ll n = G.size();\n vll A(n, 0);\n rep(i, n) {\n rep(j, G[i].size()) { A[G[i][j]]++; }\n }\n vb han(n, false);\n rep(i, n) {\n if (A[i] == 0) {\n Q.push(i);\n han[i] = true;\n }\n }\n vll B;\n while (!Q.empty()) {\n ll a = Q.top();\n Q.pop();\n B.push_back(a);\n rep(i, G[a].size()) {\n if (!han[G[a][i]]) {\n A[G[a][i]]--;\n if (A[G[a][i]] == 0) {\n Q.push(G[a][i]);\n han[G[a][i]] = true;\n }\n }\n }\n }\n if (B.size() != n) {\n vll C(n, -2);\n return C;\n }\n return B;\n}\n#include <bits/stdc++.h>\n// ax+bのようなグラフの最小値をクエリ一個あたりO(1)で算出することが出来る。\n// 条件は2つ存在する。\n// 1.追加される順に傾きは単調減少でなければならない(傾きに関してソートする必要性が生じる)\n// 2.追加される順にxは単調増加でなければならない(クエリ先読み等のひと手間が必要になる可能性がある)\n// 2つの条件が満たせない場合はこのサンプルを使うことが出来ない(クエリが2種類存在し、直線の追加とxを指定して最小値を求める動作を平行して行う場合等)\n\nstruct ConvexHullTrick {\n std::deque<std::pair<long long, long long>> dq; // (傾き、切片)\n\n void add(long long a, long long b) {\n std::pair<long long, long long> p0 = std::make_pair(a, b);\n while (dq.size() >= 2) {\n int sz = dq.size();\n std::pair<long long, long long> p1 = dq[sz - 1];\n std::pair<long long, long long> p2 = dq[sz - 2];\n if ((p0.second - p1.second) * (p0.first - p2.first) <\n (p0.second - p2.second) * (p0.first - p1.first))\n break; // 交点のx座標を比較\n dq.pop_back();\n }\n dq.push_back(p0);\n }\n\n long long query(long long x) {\n while (dq.size() >= 2) {\n long long v1 = dq[0].first * x + dq[0].second;\n long long v2 = dq[1].first * x + dq[1].second;\n if (v1 <= v2) break;\n dq.pop_front();\n }\n return dq[0].first * x + dq[0].second;\n }\n};\n// if(a <= b + EPS) // a <= b\n// if(a < b - EPS) // a < b\n// if(abs(a - b) < EPS) // a == b\n// 頂点 s から頂点 t までの最大フローの総流量を返す\nvector<vector<pair<ll, ll>>> G;\nll K;\nvb han;\nll ans;\nll N;\nvll X;\nvector<vector<pair<ll, ll>>> G1;\nvoid dfs(ll cou, ll poi) {\n if (cou == ((1LL << N) - 1)) {\n ans = min(ans, poi);\n }\n for (auto u : G1[cou]) {\n dfs(cou + (1LL << u.first), (poi + u.second) % K);\n }\n return;\n}\nint main() {\n cout << fixed << setprecision(20);\n ll M;\n ll a = 0, b = 0;\n ll c = 0;\n ll p = 1;\n string S, T;\n ll H, W;\n ll t;\n char O;\n while (1) {\n cin >> N >> M;\n if (N == 0 && M == 0) return 0;\n vll A(M), B(M), C(M);\n vb han(N, false);\n WeightedUnionFind<ll> U(N);\n rep(i, M) {\n cin >> O;\n if (O == '!') {\n cin >> A[i] >> B[i] >> C[i], --A[i], --B[i];\n if (!U.connected(A[i], B[i])) {\n U.merge(A[i], B[i], C[i]);\n }\n } else {\n cin >> A[i] >> B[i], --A[i], --B[i];\n if (U.connected(A[i], B[i])) {\n cout << U.diff(A[i], B[i]) << endl;\n\n } else {\n cout << \"UNKNOWN\" << endl;\n }\n }\n }\n }\n}", "accuracy": 1, "time_ms": 920, "memory_kb": 6768, "score_of_the_acc": -1.1397, "final_rank": 19 }, { "submission_id": "aoj_1330_8439094", "code_snippet": "#include <bits/stdc++.h>\n// #include <atcoder/modint>\n#define rng(a) a.begin(),a.end()\n#define rrng(a) a.rbegin(),a.rend()\n#define INF 2000000000000000000\n#define ull unsigned long long\n#define ll long long\n#define ld long double\n#define pll pair<ll, ll>\nusing namespace std;\ntemplate<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }\ntemplate<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return 1; } return 0; }\n\nint main() {\n ios::sync_with_stdio(false);\n cin.tie(nullptr);\n while (true) {\n \n ll N, M;\n cin >> N >> M;\n if (N == 0 && M == 0) {\n break;\n }\n vector<vector<ll>> child(N);\n vector<ll> par(N);\n for (ll i = 0; i < N; ++i) {\n par.at(i) = i;\n }\n vector<ll> X(N, 0);\n for (ll q = 0; q < M; ++q) {\n char c;\n ll a, b, w;\n cin >> c >> a >> b;\n a -= 1, b -= 1;\n bool same = (par.at(a) == par.at(b));\n if (c == '?') {\n if (same) {\n cout << X.at(b) - X.at(a) << \"\\n\";\n }\n else {\n cout << \"UNKNOWN\" << \"\\n\";\n }\n }\n else {\n cin >> w;\n if (same) {\n continue;\n }\n ll a_size = child.at(par.at(a)).size();\n ll b_size = child.at(par.at(b)).size();\n if (a_size < b_size) {\n swap(a, b);\n swap(a_size, b_size);\n w = -w;\n }\n ll dx = X.at(a) + w - X.at(b);\n ll par_b = par.at(b);\n for (ll i = 0; i < child.at(par_b).size(); ++i) {\n ll cb = child.at(par_b).at(i);\n child.at(par.at(a)).push_back(cb);\n par.at(cb) = par.at(a);\n X.at(cb) += dx;\n }\n child.at(par.at(a)).push_back(par_b);\n par.at(par_b) = par.at(a);\n X.at(par_b) += dx;\n }\n }\n }\n}", "accuracy": 1, "time_ms": 210, "memory_kb": 8604, "score_of_the_acc": -0.6837, "final_rank": 9 }, { "submission_id": "aoj_1330_7916403", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\ntypedef long long ll;\nusing ull = unsigned long long;\ntypedef pair<ll,ll> pll;\ntypedef vector<ll> vll;\ntypedef vector<pll> vpll;\ntemplate<class T> using pqmin = priority_queue<T, vector<T>, greater<T>>;\ntemplate<class T> using pqmax = priority_queue<T>;\nconst ll inf=LLONG_MAX/3;\nconst ll dx[] = {0, 1, 0, -1, 1, -1, 1, -1};\nconst ll dy[] = {1, 0, -1, 0, 1, 1, -1, -1};\n#define mp make_pair\n#define pb push_back\n#define eb emplace_back\n#define fi first\n#define se secondf\n#define all(x) x.begin(),x.end()\n#define si(x) ll(x.size())\n#define rep(i,n) for(ll i=0;i<n;i++)\n#define per(i,n) for(ll i=n-1;i>=0;i--)\n#define rng(i,l,r) for(ll i=l;i<r;i++)\n#define gnr(i,l,r) for(ll i=r-1;i>=l;i--)\n#define fore(i, a) for(auto &&i : a)\n#define fore2(a, b, v) for(auto &&[a, b] : v)\n#define fore3(a, b, c, v) for(auto &&[a, b, c] : v)\ntemplate<class T> bool chmin(T& a, const T& b){ if(a <= b) return 0; a = b; return 1; }\ntemplate<class T> bool chmax(T& a, const T& b){ if(a >= b) return 0; a = b; return 1; }\ntemplate<class T, class U> bool chmin(T& a, const U& b){ return chmin(a, (T)b); }\ntemplate<class T, class U> bool chmax(T& a, const U& b){ return chmax(a, (T)b); }\n#define LL(...) ll __VA_ARGS__;in(__VA_ARGS__)\n#define STR(...) string __VA_ARGS__;in(__VA_ARGS__)\n#define CHR(...) char __VA_ARGS__;in(__VA_ARGS__)\n#define vec(type,name,...) vector<type>name(__VA_ARGS__)\n#define VEC(type,name,size) vector<type>name(size);in(name)\n#define VLL(name,size) vector<ll>name(size);in(name)\n#define vv(type,name,h,...) vector<vector<type>> name(h,vector<type>(__VA_ARGS__))\n#define VV(type,name,h,w) vector<vector<type>> name(h,vector<type>(w));in(name)\n#define vvv(type,name,h,w,...) vector<vector<vector<type>>> name(h,vector<vector<type>>(w,vector<type>(__VA_ARGS__)))\n#define SUM(...) accumulate(all(__VA_ARGS__),0LL)\ntemplate<class T> auto min(const T& a){ return *min_element(all(a)); }\ntemplate<class T> auto max(const T& a){ return *max_element(all(a)); }\ntemplate<class T, class F = less<>> void sor(T& a, F b = F{}){ sort(all(a), b); }\ntemplate<class T> void uniq(T& a){ sor(a); a.erase(unique(all(a)), end(a)); }\nvoid outb(bool x){cout<<(x?\"Yes\":\"No\")<<\"\\n\";}\nll max(int x, ll y) { return max((ll)x, y); }\nll max(ll x, int y) { return max(x, (ll)y); }\nint min(int x, ll y) { return min((ll)x, y); }\nint min(ll x, int y) { return min(x, (ll)y); }\n#define lb(c, x) distance((c).begin(), lower_bound(all(c), (x)))\n#define lbg(c, x) distance((c).begin(), lower_bound(all(c), (x), greater{}))\n#define ub(c, x) distance((c).begin(), upper_bound(all(c), (x)))\n#define ubg(c, x) distance((c).begin(), upper_bound(all(c), (x), greater{}))\nll gcd(ll a,ll b){return (b?gcd(b,a%b):a);}\nvector<pll> factor(ull x){ vector<pll> ans; for(ull i = 2; i * i <= x; i++) if(x % i == 0){ ans.push_back({i, 1}); while((x /= i) % i == 0) ans.back().second++; } if(x != 1) ans.push_back({x, 1}); return ans; }\nvector<ll> divisor(ull x){ vector<ll> ans; for(ull i = 1; i * i <= x; i++) if(x % i == 0) ans.push_back(i); per(i,ans.size() - (ans.back() * ans.back() == x)) ans.push_back(x / ans[i]); return ans; }\nvll prime_table(ll n){vec(ll,isp,n+1,1);vll res;rng(i,2,n+1)if(isp[i]){res.pb(i);for(ll j=i*i;j<=n;j+=i)isp[j]=0;}return res;}\n\ntemplate <class T, class S> pair<T, S> operator-(const pair<T, S> &x) { return pair<T, S>(-x.first, -x.second); }\ntemplate <class T, class S> pair<T, S> operator-(const pair<T, S> &x, const pair<T, S> &y) { return pair<T, S>(x.fi - y.fi, x.se - y.se); }\ntemplate <class T, class S> pair<T, S> operator+(const pair<T, S> &x, const pair<T, S> &y) { return pair<T, S>(x.fi + y.fi, x.se + y.se); }\ntemplate <class T> pair<T, T> operator&(const pair<T, T> &l, const pair<T, T> &r) { return pair<T, T>(max(l.fi, r.fi), min(l.se, r.se)); }\ntemplate <class T, class S> pair<T, S> operator+=(pair<T, S> &l, const pair<T, S> &r) { return l = l + r; }\ntemplate <class T, class S> pair<T, S> operator-=(pair<T, S> &l, const pair<T, S> &r) { return l = l - r; }\ntemplate <class T> bool intersect(const pair<T, T> &l, const pair<T, T> &r) { return (l.se < r.se ? r.fi < l.se : l.fi < r.se); }\n\ntemplate <class T> vector<T> &operator++(vector<T> &v) {\n\t\tfore(e, v) e++;\n\t\treturn v;\n}\ntemplate <class T> vector<T> operator++(vector<T> &v, int) {\n\t\tauto res = v;\n\t\tfore(e, v) e++;\n\t\treturn res;\n}\ntemplate <class T> vector<T> &operator--(vector<T> &v) {\n\t\tfore(e, v) e--;\n\t\treturn v;\n}\ntemplate <class T> vector<T> operator--(vector<T> &v, int) {\n\t\tauto res = v;\n\t\tfore(e, v) e--;\n\t\treturn res;\n}\ntemplate <class T> vector<T> &operator+=(vector<T> &l, const vector<T> &r) {\n\t\tfore(e, r) l.eb(e);\n\t\treturn l;\n}\n\ntemplate<class... Ts> void in(Ts&... t);\n[[maybe_unused]] void print(){}\ntemplate<class T, class... Ts> void print(const T& t, const Ts&... ts);\ntemplate<class... Ts> void out(const Ts&... ts){ print(ts...); cout << '\\n'; }\nnamespace IO{\n#define VOID(a) decltype(void(a))\nstruct S{ S(){ cin.tie(nullptr)->sync_with_stdio(0); fixed(cout).precision(12); } }S;\ntemplate<int I> struct P : P<I-1>{};\ntemplate<> struct P<0>{};\ntemplate<class T> void i(T& t){ i(t, P<3>{}); }\nvoid i(vector<bool>::reference t, P<3>){ int a; i(a); t = a; }\ntemplate<class T> auto i(T& t, P<2>) -> VOID(cin >> t){ cin >> t; }\ntemplate<class T> auto i(T& t, P<1>) -> VOID(begin(t)){ for(auto&& x : t) i(x); }\ntemplate<class T, size_t... idx> void ituple(T& t, index_sequence<idx...>){ in(get<idx>(t)...); }\ntemplate<class T> auto i(T& t, P<0>) -> VOID(tuple_size<T>{}){ ituple(t, make_index_sequence<tuple_size<T>::value>{}); }\ntemplate<class T> void o(const T& t){ o(t, P<4>{}); }\ntemplate<size_t N> void o(const char (&t)[N], P<4>){ cout << t; }\ntemplate<class T, size_t N> void o(const T (&t)[N], P<3>){ o(t[0]); for(size_t i = 1; i < N; i++){ o(' '); o(t[i]); } }\ntemplate<class T> auto o(const T& t, P<2>) -> VOID(cout << t){ cout << t; }\ntemplate<class T> auto o(const T& t, P<1>) -> VOID(begin(t)){ bool first = 1; for(auto&& x : t) { if(first) first = 0; else o(' '); o(x); } }\ntemplate<class T, size_t... idx> void otuple(const T& t, index_sequence<idx...>){ print(get<idx>(t)...); }\ntemplate<class T> auto o(T& t, P<0>) -> VOID(tuple_size<T>{}){ otuple(t, make_index_sequence<tuple_size<T>::value>{}); }\n#undef VOID\n}\n#define unpack(a) (void)initializer_list<int>{(a, 0)...}\ntemplate<class... Ts> void in(Ts&... t){ unpack(IO::i(t)); }\ntemplate<class T, class... Ts> void print(const T& t, const Ts&... ts){ IO::o(t); unpack(IO::o((cout << ' ', ts))); }\n#undef unpack\n\nstruct WeightedUnionFind{\n\tvll par,wdiff;\n\tWeightedUnionFind(ll n=-1){\n\t\tinit(n);\n\t}\n\tvoid init(ll n){\n\t\tpar.resize(n);\n\t\twdiff.resize(n);\n\t\trep(i,n){\n\t\t\tpar[i]=i;\n\t\t\twdiff[i]=0;\n\t\t}\n\t}\n\tll root(ll x){\n\t\tif(par[x]==x){\n\t\t\treturn x;\n\t\t}\n\t\tll r=root(par[x]);\n\t\twdiff[x]+=wdiff[par[x]];\n\t\tpar[x]=r;\n\t\treturn r;\n\t}\n\tbool same(ll x,ll y){\n\t\treturn root(x)==root(y);\n\t}\n\tvoid merge(ll x,ll y,ll w){\n\t\t//w[x]==w[y]+w\n\t\tll rx=root(x); //w[x]=w[rx]+wdiff[x]\n\t\tll ry=root(y); //w[y]=w[ry]+wdiff[y]\n\t\twdiff[rx]=w-wdiff[x]+wdiff[y]; //w[rx]=w[ry]-wfidd[x]+wdiff[y]+w\n\t\tpar[rx]=ry;\n\t}\n\tll weight(ll x){\n\t\troot(x);\n\t\treturn wdiff[x];\n\t}\n\tll diff(ll x,ll y){\n\t\treturn weight(x)-weight(y);\n\t}\n};\nint main(){\n\tcin.tie(0);\n\tios::sync_with_stdio(0);\n\twhile(1){\n\t\tLL(n,m);\n\t\tif(n==0)break;\n\t\tWeightedUnionFind uf(n);\n\t\trep(_,m){\n\t\t\tCHR(t);\n\t\t\tif(t=='!'){\n\t\t\t\tLL(a,b,w);\n\t\t\t\ta--,b--;\n\t\t\t\tuf.merge(b,a,w);\n\t\t\t}\n\t\t\telse{\n\t\t\t\tLL(a,b);\n\t\t\t\ta--,b--;\n\t\t\t\tif(!uf.same(a,b)){\n\t\t\t\t\tout(\"UNKNOWN\");\n\t\t\t\t}\n\t\t\t\telse{\n\t\t\t\t\tout(uf.diff(b,a));\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n}", "accuracy": 1, "time_ms": 200, "memory_kb": 6692, "score_of_the_acc": -0.4166, "final_rank": 5 }, { "submission_id": "aoj_1330_7555856", "code_snippet": "#include <iostream>\n#include <vector>\n#include <algorithm>\n#include <string>\n#define rep(i, n) for (int i = 0; i < (int)(n); i++)\n \nusing namespace std;\n \ntemplate<class Abel> struct UnionFind {\n vector<int> par;\n vector<int> rank;\n vector<Abel> diff_weight;\n\n UnionFind(int n = 1, Abel SUM_UNITY = 0) {\n init(n, SUM_UNITY);\n }\n\n void init(int n = 1, Abel SUM_UNITY = 0) {\n par.resize(n); rank.resize(n); diff_weight.resize(n);\n for (int i = 0; i < n; ++i) par[i] = i, rank[i] = 0, diff_weight[i] = SUM_UNITY;\n }\n\n int root(int x) {\n if (par[x] == x) {\n return x;\n }\n else {\n int r = root(par[x]);\n diff_weight[x] += diff_weight[par[x]];\n return par[x] = r;\n }\n }\n\n Abel weight(int x) {\n root(x);\n return diff_weight[x];\n }\n\n bool same(int x, int y) {\n return root(x) == root(y);\n }\n\n bool merge(int x, int y, Abel w) {\n w += weight(x); w -= weight(y);\n x = root(x); y = root(y);\n if (x == y) return false;\n if (rank[x] < rank[y]) swap(x, y), w = -w;\n if (rank[x] == rank[y]) ++rank[x];\n par[y] = x;\n diff_weight[y] = w;\n return true;\n }\n\n Abel diff(int x, int y) {\n return weight(y) - weight(x);\n }\n};\n \nint main(){\n while(true){\n int N,M;\n cin >> N >> M;\n if(N==0 && M==0) break;\n \n UnionFind<int> tree(N);\n \n rep(i,M){\n int L,R,D;\n char Q;\n cin >> Q;\n if(Q == '!'){\n //mergeする\n cin >> L >> R >> D;\n L--; R--;\n tree.merge(L,R,D);\n }\n else{\n //LとRが同じグループなら距離が分かる\n cin >> L >> R;\n L--; R--;\n if(tree.same(L,R)){\n cout << tree.diff(L,R) << endl;\n }\n else{\n cout << \"UNKNOWN\" << endl;\n }\n }\n }\n }\n}", "accuracy": 1, "time_ms": 1210, "memory_kb": 3940, "score_of_the_acc": -1.0463, "final_rank": 17 } ]
aoj_1334_cpp
Problem J: Cubic Colonies In AD 3456, the earth is too small for hundreds of billions of people to live in peace. Interstellar Colonization Project with Cubes (ICPC) is a project that tries to move people on the earth to space colonies to ameliorate the problem. ICPC obtained funding from governments and manufactured space colonies very quickly and at low cost using prefabricated cubic blocks. The largest colony looks like a Rubik's cube. It consists of 3 × 3 × 3 cubic blocks (Figure J.1A). Smaller colonies miss some of the blocks in the largest colony. When we manufacture a colony with multiple cubic blocks, we begin with a single block. Then we iteratively glue a next block to existing blocks in a way that faces of them match exactly. Every pair of touched faces is glued. Figure J.1: Example of the largest colony and a smaller colony However, just before the first launch, we found a design flaw with the colonies. We need to add a cable to connect two points on the surface of each colony, but we cannot change the inside of the prefabricated blocks in a short time. Therefore we decided to attach a cable on the surface of each colony. If a part of the cable is not on the surface, it would be sheared off during the launch, so we have to put the whole cable on the surface. We would like to minimize the lengths of the cables due to budget constraints. The dashed line in Figure J.1B is such an example. Write a program that, given the shape of a colony and a pair of points on its surface, calculates the length of the shortest possible cable for that colony. Input The input contains a series of datasets. Each dataset describes a single colony and the pair of the points for the colony in the following format. x 1 y 1 z 1 x 2 y 2 z 2 b 0,0,0 b 1,0,0 b 2,0,0 b 0,1,0 b 1,1,0 b 2,1,0 b 0,2,0 b 1,2,0 b 2,2,0 b 0,0,1 b 1,0,1 b 2,0,1 b 0,1,1 b 1,1,1 b 2,1,1 b 0,2,1 b 1,2,1 b 2,2,1 b 0,0,2 b 1,0,2 b 2,0,2 b 0,1,2 b 1,1,2 b 2,1,2 b 0,2,2 b 1,2,2 b 2,2,2 ( x 1 , y 1 , z 1 ) and ( x 2 , y 2 , z 2 ) are the two distinct points on the surface of the colony, where x 1 , x 2 , y 1 , y 2 , z 1 , z 2 are integers that satisfy 0 ≤ x 1 , x 2 , y 1 , y 2 , z 1 , z 2 ≤ 3. b i,j,k is ' # ' when there is a cubic block whose two diagonal vertices are ( i , j , k ) and ( i + 1, j + 1, k + 1), and b i,j,k is ' . ' if there is no block. Figure J.1A corresponds to the first dataset in the sample input, whereas Figure J.1B corresponds to the second. A cable can pass through a zero-width gap between two blocks if they are touching only on their vertices or edges. In Figure J.2A, which is the third dataset in the sample input, the shortest cable goes from the point A (0, 0, 2) to the point B (2, 2, 2), passing through (1, 1, 2), which is shared by six blocks. Similarly, in Figure J.2B (the fourth dataset in the sample input), the shortest cable goes through the gap between two blocks not glued directly. When two blocks share only a single vertex, you can put a cable through the vertex ...(truncated)
[ { "submission_id": "aoj_1334_4876386", "code_snippet": "#include<bits/stdc++.h>\ntypedef long long int ll;\ntypedef unsigned long long int ull;\n#define BIG_NUM 2000000000\n#define HUGE_NUM 1000000000000000000\n#define MOD 1000000007\n#define EPS 0.000000001\nusing namespace std;\n\n\n#define SIZE 10005\n\nenum DIR{\n\tUP,\n\tEast,\n\tNorth,\n\tWest,\n\tSouth,\n\tDown,\n};\n\n\nstruct LOC{\n\tvoid set(int arg_X,int arg_Y,int arg_Z){\n\t\tX = arg_X;\n\t\tY = arg_Y;\n\t\tZ = arg_Z;\n\t}\n\t/*void debug(){\n\n\t\tprintf(\"X:%d Y:%d Z:%d\\n\",X,Y,Z);\n\t}*/\n\tint X,Y,Z;\n};\n\n\nstruct Point3D{\n\tPoint3D(double arg_x,double arg_y,double arg_z){\n\t\tx = arg_x;\n\t\ty = arg_y;\n\t\tz = arg_z;\n\t\tX = Y = Z = 0;\n\t}\n\n\tPoint3D(){\n\t\tx = y = z = 0.0;\n\t\tX = Y = Z = 0;\n\t}\n\n\tPoint3D operator + (Point3D p){ return Point3D(x+p.x,y+p.y,z+p.z); }\n\tPoint3D operator - (Point3D p){ return Point3D(x-p.x,y-p.y,z-p.z);}\n\tPoint3D operator * (double a){ return Point3D(a*x,a*y,a*z); }\n\tPoint3D operator / (double a){ return Point3D(x/a,y/a,z/a); }\n\n\tdouble abs(){ return sqrt(norm()); }\n\tdouble norm(){ return x*x + y*y + z*z; }\n\n\tbool operator<(const Point3D &p) const{\n\t\tif(fabs(x-p.x) > EPS){\n\n\t\t\treturn x < p.x;\n\t\t}else if(fabs(y-p.y) > EPS){\n\n\t\t\treturn y < p.y;\n\t\t}else{\n\n\t\t\treturn z < p.z;\n\t\t}\n\t}\n\n\tvoid set(int arg_X,int arg_Y,int arg_Z){\n\t\tX = arg_X;\n\t\tY = arg_Y;\n\t\tZ = arg_Z;\n\t}\n\n\tvoid copy(){\n\t\tx = X;\n\t\ty = Y;\n\t\tz = Z;\n\t}\n\n\tbool operator == (const Point3D &p) const{\n\t\treturn fabs(x-p.x) < EPS && fabs(y-p.y) < EPS && fabs(z-p.z) < EPS;\n\t}\n\t/*void debug(){\n\n\t\tprintf(\"(%.3lf,%.3lf,%.3lf)\\n\",x,y,z);\n\t}*/\n\tint X,Y,Z;\n\tdouble x,y,z;\n};\n\ntypedef Point3D Vector3D;\n\nstruct Line{\n\tLine(){\n\n\t}\n\tLine(Point3D a,Point3D b){\n\t\tp[0] = a;\n\t\tp[1] = b;\n\t}\n\t/*void outPut(){\n\n\t\tprintf(\"(%.3lf,%.3lf)-(%.3lf,%.3lf)\\n\",p[0].x,p[0].y,p[1].x,p[1].y);\n\t}*/\n\tPoint3D p[2];\n};\n\nstruct Data{\n\tData(){\n\n\t\tX = Y = Z = 0;\n\t\tx = y = z = 0;\n\t}\n\tData(double arg_x,double arg_y,double arg_z){\n\t\tx = arg_x;\n\t\ty = arg_y;\n\t\tz = arg_z;\n\t\tX = Y = Z = 0;\n\t}\n\n\tvoid set(int arg_X,int arg_Y,int arg_Z){\n\t\tX = arg_X;\n\t\tY = arg_Y;\n\t\tZ = arg_Z;\n\t}\n\tvoid copy(){\n\t\tx = X;\n\t\ty = Y;\n\t\tz = Z;\n\t}\n\tvoid debug(){\n\n\t\tprintf(\"%d %d %d\\n\",X,Y,Z);\n\t}\n\tint X,Y,Z;\n\tdouble x,y,z;\n};\n\nstruct Seg{\n\tvoid set(int arg_A,int arg_B){\n\t\tA = arg_A;\n\t\tB = arg_B;\n\t}\n\tint A,B;\n};\n\nstruct Edge{\n\tEdge(int arg_to,double arg_dist){\n\t\tto = arg_to;\n\t\tdist = arg_dist;\n\t}\n\tint to;\n\tdouble dist;\n};\n\nstruct Info{\n\tInfo(){\n\t\tindex = 0;\n\t}\n\tInfo(Point3D arg_p,int arg_index){\n\t\tp = arg_p;\n\t\tindex = arg_index;\n\t}\n\tbool operator<(const struct Info &arg)const{\n\n\t\treturn p < arg.p;\n\t}\n\tbool operator==(const struct Info &arg) const{\n\n\t\treturn p == arg.p;\n\t}\n\tPoint3D p;\n\tint index;\n};\n\nstruct State{\n\tState(int arg_node_id,double arg_sum_dist){\n\t\tnode_id = arg_node_id;\n\t\tsum_dist = arg_sum_dist;\n\t}\n\tbool operator<(const struct State &arg) const{\n\n\t\treturn sum_dist > arg.sum_dist; //総距離の昇順(PQ)\n\t}\n\tint node_id;\n\tdouble sum_dist;\n};\n\nint num_loc,num_data,num_POINT,num_seg;\nint S,T;\nint ADJ[27][6];\nint loc_X[6] = {0,1,0,-1,0,0},loc_Y[6] = {-1,0,0,0,0,1},loc_Z[6] = {0,0,1,0,-1,0};\nint D_X[8] = {0,1,0,1,0,1,0,1},D_Y[8] = {0,0,0,0,1,1,1,1},D_Z[8] = {0,0,1,1,0,0,1,1};\nint have_L[6][4] ={{0,1,2,3},{1,5,9,10},{2,6,10,11},{3,7,8,11},{0,4,8,9},{4,5,6,7}};\nint L_array[12][2] = {{0,1},{1,3},{2,3},{0,2},{4,5},{5,7},{6,7},{4,6},{0,4},{1,5},{3,7},{2,6}};\nint D[27][8];\nint SEG[27][12];\nint POS[9][3] = {{0,1,2},{9,10,11},{18,19,20},{3,4,5},{12,13,14},{21,22,23},{6,7,8},{15,16,17},{24,25,26}};\nint four_line[27][6][4];\ndouble min_dist[SIZE];\nbool EXIST[27],check[144];\nchar input_str[9][4];\nPoint3D start,goal;\nData first_D[27][8];\nData data[64];\nSeg first_seg[27][12];\nSeg seg[144];\nLOC loc[27]; //相対位置計算用\nvector<Edge> G[SIZE];\nPoint3D POINT[SIZE];\nvector<Info> info;\nvector<int> LINE[144];\n\n\n\ndouble calc_dist(Point3D A,Point3D B){\n\n\tdouble a = A.x-B.x;\n\tdouble b = A.y-B.y;\n\tdouble c = A.z-B.z;\n\n\treturn sqrt(a*a+b*b+c*c);\n}\n\nvoid func(){\n\n\tfor(int i = 0; i < num_POINT; i++){\n\n\t\tG[i].clear();\n\t}\n\tfor(int i = 0; i < num_seg; i++){\n\n\t\tcheck[i] = false;\n\t}\n\n\tfor(int row = 0; row < 9; row++){\n\t\tscanf(\"%s\",input_str[row]);\n\t}\n\n\t//キューブの存在チェック\n\tfor(int row = 0; row < 9; row++){\n\t\tfor(int col = 0; col < 3; col++){\n\t\t\tif(input_str[row][col] == '#'){\n\n\t\t\t\tEXIST[POS[row][col]] = true;\n\n\t\t\t}else{\n\n\t\t\t\tEXIST[POS[row][col]] = false;\n\t\t\t}\n\t\t}\n\t}\n\n\tS = -1;\n\tfor(int i = 0; i < num_POINT; i++){\n\n\t\tif(start.X == POINT[i].X && start.Y == POINT[i].Y && start.Z == POINT[i].Z){\n\n\t\t\tS = i;\n\t\t\tbreak;\n\t\t}\n\t}\n\tT = -1;\n\tfor(int i = 0; i < num_POINT; i++){\n\n\t\tif(goal.X == POINT[i].X && goal.Y == POINT[i].Y && goal.Z == POINT[i].Z){\n\n\t\t\tT = i;\n\t\t\tbreak;\n\t\t}\n\t}\n\n\n\t//面の有効チェック&エッジ張り\n\tfor(int i = 0; i <= 26; i++){\n\t\tif(!EXIST[i])continue;\n\t\tfor(int k = 0; k < 6; k++){\n\t\t\tif(ADJ[i][k] == -1 || !EXIST[ADJ[i][k]]){\n\t\t\t\tfor(int a = 0; a < 4; a++){\n\t\t\t\t\tint from = four_line[i][k][a]; //辺のindex\n\n\t\t\t\t\t//辺自身のエッジ張り\n\t\t\t\t\tif(!check[from]){\n\t\t\t\t\t\tcheck[from] = true;\n\t\t\t\t\t\tfor(int b = 1; b < LINE[from].size(); b++){\n\n\t\t\t\t\t\t\tdouble tmp_dist = calc_dist(POINT[LINE[from][b-1]],POINT[LINE[from][b]]);\n\n\t\t\t\t\t\t\tG[LINE[from][b-1]].push_back(Edge(LINE[from][b],tmp_dist));\n\t\t\t\t\t\t\tG[LINE[from][b]].push_back(Edge(LINE[from][b-1],tmp_dist));\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t\t//他の辺とのエッジ張り\n\t\t\t\t\tfor(int b = a+1; b < 4; b++){\n\t\t\t\t\t\tint to = four_line[i][k][b];\n\t\t\t\t\t\tfor(int c = 0; c < LINE[from].size(); c++){\n\t\t\t\t\t\t\tfor(int d = 0; d < LINE[to].size(); d++){\n\n\t\t\t\t\t\t\t\tint A = LINE[from][c];\n\t\t\t\t\t\t\t\tint B = LINE[to][d];\n\t\t\t\t\t\t\t\tif(A == B)continue;\n\n\t\t\t\t\t\t\t\tdouble tmp_dist = calc_dist(POINT[A],POINT[B]);\n\n\t\t\t\t\t\t\t\tG[A].push_back(Edge(B,tmp_dist));\n\t\t\t\t\t\t\t\tG[B].push_back(Edge(A,tmp_dist));\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n\n\tfor(int i = 0; i < num_POINT; i++){\n\n\t\tmin_dist[i] = BIG_NUM;\n\t}\n\n\tpriority_queue<State> Q;\n\tmin_dist[S] = 0;\n\n\tQ.push(State(S,0));\n\n\tint next_node;\n\tdouble next_dist;\n\n\twhile(!Q.empty()){\n\n\t\tif(Q.top().sum_dist > min_dist[Q.top().node_id]+EPS){\n\n\t\t\tQ.pop();\n\t\t}else if(Q.top().node_id == T){\n\n\t\t\tprintf(\"%.10lf\\n\",Q.top().sum_dist);\n\t\t\treturn;\n\n\t\t}else{\n\n\t\t\tfor(int i = 0; i < G[Q.top().node_id].size(); i++){\n\n\t\t\t\tnext_node = G[Q.top().node_id][i].to;\n\t\t\t\tnext_dist = Q.top().sum_dist+G[Q.top().node_id][i].dist;\n\n\t\t\t\tif(min_dist[next_node] > next_dist+EPS){\n\t\t\t\t\tmin_dist[next_node] = next_dist;\n\t\t\t\t\tQ.push(State(next_node,next_dist));\n\t\t\t\t}\n\t\t\t}\n\t\t\tQ.pop();\n\t\t}\n\t}\n}\n\nbool rangeCheck(int value){\n\n\treturn value >= 0 && value <= 2;\n}\n\nvoid calc_ADJ(){\n\n\tnum_loc = 0;\n\n\tfor(int Y = 0; Y <= 2; Y++){\n\t\tfor(int Z = 0; Z <= 2; Z++){\n\t\t\tfor(int X = 0; X <= 2; X++){\n\n\t\t\t\tloc[num_loc].set(X,Y,Z);\n\t\t\t\tnum_loc++;\n\t\t\t}\n\t\t}\n\t}\n\n\tfor(int i = 0; i <= 26; i++){\n\t\tfor(int k = 0; k < 6; k++){\n\n\t\t\tint adj_X = loc[i].X+loc_X[k];\n\t\t\tint adj_Y = loc[i].Y+loc_Y[k];\n\t\t\tint adj_Z = loc[i].Z+loc_Z[k];\n\n\t\t\tif(adj_X >= 0 && adj_X <= 2 && adj_Y >= 0 &&\n\t\t\t\t\tadj_Y <= 2 && adj_Z >= 0 && adj_Z <= 2){\n\n\t\t\t\tfor(int a = 0; a <= 26; a++){\n\t\t\t\t\tif(adj_X == loc[a].X && adj_Y == loc[a].Y && adj_Z == loc[a].Z){\n\t\t\t\t\t\tADJ[i][k] = a;\n\t\t\t\t\t\tbreak;\n\t\t\t\t\t}\n\t\t\t\t}\n\n\t\t\t}else{\n\n\t\t\t\tADJ[i][k] = -1;\n\t\t\t}\n\t\t}\n\t}\n}\n\nvoid calc_PL(){\n\n\t//まずは線分の両端の8点列挙\n\tfor(int i = 0; i <= 26; i++){\n\t\tfor(int k = 0; k <= 7; k++){\n\t\t\tint X = loc[i].X+D_X[k];\n\t\t\tint Y = loc[i].Y+D_Y[k];\n\t\t\tint Z = loc[i].Z+D_Z[k];\n\n\t\t\tfirst_D[i][k].set(X,Y,Z);\n\t\t}\n\t}\n\n\t//indexに変換する\n\tnum_data = 0;\n\tfor(int i = 0; i <= 26; i++){\n\t\tfor(int k = 0; k <= 7; k++){\n\n\t\t\tint X = first_D[i][k].X;\n\t\t\tint Y = first_D[i][k].Y;\n\t\t\tint Z = first_D[i][k].Z;\n\n\t\t\tbool FLG = false;\n\t\t\tfor(int a = 0; a < num_data; a++){\n\t\t\t\tif(X == data[a].X && Y == data[a].Y && Z == data[a].Z){\n\t\t\t\t\tD[i][k] = a;\n\t\t\t\t\tFLG = true;\n\t\t\t\t\tbreak;\n\t\t\t\t}\n\t\t\t}\n\n\t\t\tif(!FLG){\n\t\t\t\tD[i][k] = num_data;\n\t\t\t\tdata[num_data].set(X,Y,Z);\n\t\t\t\tnum_data++;\n\t\t\t}\n\t\t}\n\t}\n\n\tfor(int i = 0; i < num_data; i++){\n\n\t\tdata[i].copy();\n\t}\n\n\tnum_POINT = 0;\n\tfor(int i = 0; i < num_data; i++){\n\t\tPOINT[i].X = data[i].X;\n\t\tPOINT[i].Y = data[i].Y;\n\t\tPOINT[i].Z = data[i].Z;\n\t\tPOINT[i].copy();\n\t\tnum_POINT++;\n\t}\n\n\t//6面の4辺を列挙する\n\tnum_seg = 0;\n\tint tmp_index;\n\n\tfor(int i = 0; i <= 26; i++){\n\t\tfor(int k = 0; k < 6; k++){ //面\n\t\t\tfor(int p = 0; p < 4; p++){\n\t\t\t\tint from = D[i][L_array[have_L[k][p]][0]];\n\t\t\t\tint to = D[i][L_array[have_L[k][p]][1]];\n\n\t\t\t\tif(from > to){\n\n\t\t\t\t\tswap(from,to);\n\t\t\t\t}\n\t\t\t\ttmp_index = -1;\n\t\t\t\tfor(int a = 0; a < num_seg; a++){\n\t\t\t\t\tif(seg[a].A == from && seg[a].B == to){\n\n\t\t\t\t\t\ttmp_index = a;\n\t\t\t\t\t\tbreak;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\tif(tmp_index == -1){\n\n\t\t\t\t\ttmp_index = num_seg;\n\t\t\t\t\tseg[num_seg].A = from;\n\t\t\t\t\tseg[num_seg].B = to;\n\t\t\t\t\tnum_seg++;\n\t\t\t\t}\n\t\t\t\tfour_line[i][k][p] = tmp_index;\n\t\t\t}\n\t\t}\n\t}\n\n\t//辺上の点を計算\n\tfor(int i = 0; i < num_seg; i++){\n\t\tinfo.clear();\n\t\tinfo.push_back(Info(POINT[seg[i].A],seg[i].A));\n\t\tinfo.push_back(Info(POINT[seg[i].B],seg[i].B));\n\t\tsort(info.begin(),info.end());\n\t\tPoint3D p_L = info[0].p,p_R = info[1].p;\n\t\tfor(int ALL = 2; ALL <= 9; ALL++){\n\t\t\tfor(int left = 1; left <= ALL-1; left++){\n\t\t\t\tint right = ALL-left;\n\t\t\t\tPoint3D tmp_p = (p_R*(double)(left)+p_L*(double)(right))/(double)ALL;\n\n\t\t\t\tint tmp_id = -1;\n\t\t\t\tfor(int a = 0; a < num_POINT; a++){\n\n\t\t\t\t\tif(POINT[a] == tmp_p){\n\n\t\t\t\t\t\ttmp_id = a;\n\t\t\t\t\t\tbreak;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\tif(tmp_id == -1){\n\t\t\t\t\ttmp_id = num_POINT;\n\t\t\t\t\tPOINT[num_POINT] = tmp_p;\n\t\t\t\t\tPOINT[num_POINT].X = -1;\n\t\t\t\t\tnum_POINT++;\n\t\t\t\t}\n\t\t\t\tinfo.push_back(Info(tmp_p,tmp_id));\n\t\t\t}\n\t\t}\n\t\tsort(info.begin(),info.end());\n\t\tinfo.erase(unique(info.begin(),info.end()),info.end());\n\n\t\tfor(int a = 0; a < info.size(); a++){\n\n\t\t\tLINE[i].push_back(info[a].index);\n\t\t}\n\t}\n}\n\n\nint main(){\n\n\tcalc_ADJ();\n\tcalc_PL();\n\n\twhile(true){\n\n\t\tscanf(\"%d %d %d %d %d %d\",&start.X,&start.Y,&start.Z,&goal.X,&goal.Y,&goal.Z);\n\t\tif(start.X == 0 && start.Y == 0 && start.Z == 0 && goal.X == 0 && goal.Y == 0 && goal.Z == 0)break;\n\n\t\tfunc();\n\t}\n\n\treturn 0;\n}", "accuracy": 1, "time_ms": 2130, "memory_kb": 25656, "score_of_the_acc": -1, "final_rank": 2 }, { "submission_id": "aoj_1334_4876379", "code_snippet": "#include<bits/stdc++.h>\ntypedef long long int ll;\ntypedef unsigned long long int ull;\n#define BIG_NUM 2000000000\n#define HUGE_NUM 1000000000000000000\n#define MOD 1000000007\n#define EPS 0.000000001\nusing namespace std;\n\n\n#define SIZE 10005\n\nenum DIR{\n\tUP,\n\tEast,\n\tNorth,\n\tWest,\n\tSouth,\n\tDown,\n};\n\n\nstruct LOC{\n\tvoid set(int arg_X,int arg_Y,int arg_Z){\n\t\tX = arg_X;\n\t\tY = arg_Y;\n\t\tZ = arg_Z;\n\t}\n\tvoid debug(){\n\n\t\tprintf(\"X:%d Y:%d Z:%d\\n\",X,Y,Z);\n\t}\n\tint X,Y,Z;\n};\n\n\nstruct Point3D{\n\tPoint3D(double arg_x,double arg_y,double arg_z){\n\t\tx = arg_x;\n\t\ty = arg_y;\n\t\tz = arg_z;\n\t\tX = Y = Z = 0;\n\t}\n\n\tPoint3D(){\n\t\tx = y = z = 0.0;\n\t\tX = Y = Z = 0;\n\t}\n\n\tPoint3D operator + (Point3D p){ return Point3D(x+p.x,y+p.y,z+p.z); }\n\tPoint3D operator - (Point3D p){ return Point3D(x-p.x,y-p.y,z-p.z);}\n\tPoint3D operator * (double a){ return Point3D(a*x,a*y,a*z); }\n\tPoint3D operator / (double a){ return Point3D(x/a,y/a,z/a); }\n\n\tdouble abs(){ return sqrt(norm()); }\n\tdouble norm(){ return x*x + y*y + z*z; }\n\n\tbool operator<(const Point3D &p) const{\n\t\tif(fabs(x-p.x) > EPS){\n\n\t\t\treturn x < p.x;\n\t\t}else if(fabs(y-p.y) > EPS){\n\n\t\t\treturn y < p.y;\n\t\t}else{\n\n\t\t\treturn z < p.z;\n\t\t}\n\t}\n\n\tvoid set(int arg_X,int arg_Y,int arg_Z){\n\t\tX = arg_X;\n\t\tY = arg_Y;\n\t\tZ = arg_Z;\n\t}\n\n\tvoid copy(){\n\t\tx = X;\n\t\ty = Y;\n\t\tz = Z;\n\t}\n\n\tbool operator == (const Point3D &p) const{\n\t\treturn fabs(x-p.x) < EPS && fabs(y-p.y) < EPS && fabs(z-p.z) < EPS;\n\t}\n\tvoid debug(){\n\n\t\tprintf(\"(%.3lf,%.3lf,%.3lf)\\n\",x,y,z);\n\t}\n\tint X,Y,Z;\n\tdouble x,y,z;\n};\n\ntypedef Point3D Vector3D;\n\nstruct Line{\n\tLine(){\n\n\t}\n\tLine(Point3D a,Point3D b){\n\t\tp[0] = a;\n\t\tp[1] = b;\n\t}\n\t/*void outPut(){\n\n\t\tprintf(\"(%.3lf,%.3lf)-(%.3lf,%.3lf)\\n\",p[0].x,p[0].y,p[1].x,p[1].y);\n\t}*/\n\tPoint3D p[2];\n};\n\nstruct Data{\n\tData(){\n\n\t\tX = Y = Z = 0;\n\t\tx = y = z = 0;\n\t}\n\tData(double arg_x,double arg_y,double arg_z){\n\t\tx = arg_x;\n\t\ty = arg_y;\n\t\tz = arg_z;\n\t\tX = Y = Z = 0;\n\t}\n\n\tvoid set(int arg_X,int arg_Y,int arg_Z){\n\t\tX = arg_X;\n\t\tY = arg_Y;\n\t\tZ = arg_Z;\n\t}\n\tvoid copy(){\n\t\tx = X;\n\t\ty = Y;\n\t\tz = Z;\n\t}\n\tvoid debug(){\n\n\t\tprintf(\"%d %d %d\\n\",X,Y,Z);\n\t}\n\tint X,Y,Z;\n\tdouble x,y,z;\n};\n\nstruct Seg{\n\tvoid set(int arg_A,int arg_B){\n\t\tA = arg_A;\n\t\tB = arg_B;\n\t}\n\tint A,B;\n};\n\nstruct Edge{\n\tEdge(int arg_to,double arg_dist){\n\t\tto = arg_to;\n\t\tdist = arg_dist;\n\t}\n\tint to;\n\tdouble dist;\n};\n\nstruct Info{\n\tInfo(){\n\t\tindex = 0;\n\t}\n\tInfo(Point3D arg_p,int arg_index){\n\t\tp = arg_p;\n\t\tindex = arg_index;\n\t}\n\tbool operator<(const struct Info &arg)const{\n\n\t\treturn p < arg.p;\n\t}\n\tbool operator==(const struct Info &arg) const{\n\n\t\treturn p == arg.p;\n\t}\n\tPoint3D p;\n\tint index;\n};\n\nstruct State{\n\tState(int arg_node_id,double arg_sum_dist){\n\t\tnode_id = arg_node_id;\n\t\tsum_dist = arg_sum_dist;\n\t}\n\tbool operator<(const struct State &arg) const{\n\n\t\treturn sum_dist > arg.sum_dist; //総距離の昇順(PQ)\n\t}\n\tint node_id;\n\tdouble sum_dist;\n};\n\nint num_loc,num_data,num_POINT,num_seg;\nint S,T;\nint ADJ[27][6];\nint loc_X[6] = {0,1,0,-1,0,0},loc_Y[6] = {-1,0,0,0,0,1},loc_Z[6] = {0,0,1,0,-1,0};\nint D_X[8] = {0,1,0,1,0,1,0,1},D_Y[8] = {0,0,0,0,1,1,1,1},D_Z[8] = {0,0,1,1,0,0,1,1};\nint have_L[6][4] ={{0,1,2,3},{1,5,9,10},{2,6,10,11},{3,7,8,11},{0,4,8,9},{4,5,6,7}};\nint L_array[12][2] = {{0,1},{1,3},{2,3},{0,2},{4,5},{5,7},{6,7},{4,6},{0,4},{1,5},{3,7},{2,6}};\nint D[27][8];\nint SEG[27][12];\nint REV[3][3][3];\nint POS[9][3] = {{0,1,2},{9,10,11},{18,19,20},{3,4,5},{12,13,14},{21,22,23},{6,7,8},{15,16,17},{24,25,26}};\nint four_line[27][6][4];\ndouble min_dist[SIZE];\nbool EXIST[27],check[144];\nchar input_str[9][4];\nPoint3D start,goal;\nData first_D[27][8];\nData data[64];\nSeg first_seg[27][12];\nSeg seg[144];\nLOC loc[27]; //相対位置計算用\nvector<Edge> G[SIZE];\nPoint3D POINT[SIZE];\nvector<Info> info;\nvector<int> LINE[144];\n\n\n\n\n\n\nint gcd(int x,int y){\n\n\tx = abs(x);\n\ty = abs(y);\n\n\tif(x < y){\n\t\tswap(x,y);\n\t}\n\tif(y == 0){\n\t\treturn x;\n\t}else{\n\t\treturn gcd(y,x%y);\n\t}\n}\n\ndouble calc_dist(Point3D A,Point3D B){\n\n\tdouble a = A.x-B.x;\n\tdouble b = A.y-B.y;\n\tdouble c = A.z-B.z;\n\n\treturn sqrt(a*a+b*b+c*c);\n}\n\nvoid func(){\n\n\tfor(int i = 0; i < num_POINT; i++){\n\n\t\tG[i].clear();\n\t}\n\tfor(int i = 0; i < num_seg; i++){\n\n\t\tcheck[i] = false;\n\t}\n\n\n\n\tfor(int row = 0; row < 9; row++){\n\t\tscanf(\"%s\",input_str[row]);\n\t}\n\n\t//キューブの存在チェック\n\tfor(int row = 0; row < 9; row++){\n\t\tfor(int col = 0; col < 3; col++){\n\t\t\tif(input_str[row][col] == '#'){\n\n\t\t\t\tEXIST[POS[row][col]] = true;\n\n\t\t\t}else{\n\n\t\t\t\tEXIST[POS[row][col]] = false;\n\t\t\t}\n\t\t}\n\t}\n\n\tS = -1;\n\tfor(int i = 0; i < num_POINT; i++){\n\n\t\tif(start.X == POINT[i].X && start.Y == POINT[i].Y && start.Z == POINT[i].Z){\n\n\t\t\tS = i;\n\t\t\tbreak;\n\t\t}\n\t}\n\tT = -1;\n\tfor(int i = 0; i < num_POINT; i++){\n\n\t\tif(goal.X == POINT[i].X && goal.Y == POINT[i].Y && goal.Z == POINT[i].Z){\n\n\t\t\tT = i;\n\t\t\tbreak;\n\t\t}\n\t}\n\n\n\t//面の有効チェック&エッジ張り\n\tfor(int i = 0; i <= 26; i++){\n\t\tif(!EXIST[i])continue;\n\t\tfor(int k = 0; k < 6; k++){\n\t\t\tif(ADJ[i][k] == -1 || !EXIST[ADJ[i][k]]){\n\t\t\t\tfor(int a = 0; a < 4; a++){\n\t\t\t\t\tint from = four_line[i][k][a]; //辺のindex\n\n\t\t\t\t\t//辺自身のエッジ張り\n\t\t\t\t\tif(!check[from]){\n\t\t\t\t\t\tcheck[from] = true;\n\t\t\t\t\t\tfor(int b = 1; b < LINE[from].size(); b++){\n\n\t\t\t\t\t\t\tdouble tmp_dist = calc_dist(POINT[LINE[from][b-1]],POINT[LINE[from][b]]);\n\n\t\t\t\t\t\t\tG[LINE[from][b-1]].push_back(Edge(LINE[from][b],tmp_dist));\n\t\t\t\t\t\t\tG[LINE[from][b]].push_back(Edge(LINE[from][b-1],tmp_dist));\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t\t//他の辺とのエッジ張り\n\t\t\t\t\tfor(int b = a+1; b < 4; b++){\n\t\t\t\t\t\tint to = four_line[i][k][b];\n\t\t\t\t\t\tfor(int c = 0; c < LINE[from].size(); c++){\n\t\t\t\t\t\t\tfor(int d = 0; d < LINE[to].size(); d++){\n\n\t\t\t\t\t\t\t\tint A = LINE[from][c];\n\t\t\t\t\t\t\t\tint B = LINE[to][d];\n\t\t\t\t\t\t\t\tif(A == B)continue;\n\n\t\t\t\t\t\t\t\tdouble tmp_dist = calc_dist(POINT[A],POINT[B]);\n\n\t\t\t\t\t\t\t\tG[A].push_back(Edge(B,tmp_dist));\n\t\t\t\t\t\t\t\tG[B].push_back(Edge(A,tmp_dist));\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n\n/*\n\tprintf(\"S:%d T:%d\\n\",S,T);\n\treturn;*/\n\n\tfor(int i = 0; i < num_POINT; i++){\n\n\t\tmin_dist[i] = BIG_NUM;\n\t}\n\n\tpriority_queue<State> Q;\n\tmin_dist[S] = 0;\n\n\tQ.push(State(S,0));\n\n\tint next_node;\n\tdouble next_dist;\n\n\twhile(!Q.empty()){\n\n\t\tif(Q.top().sum_dist > min_dist[Q.top().node_id]+EPS){\n\n\t\t\tQ.pop();\n\t\t}else if(Q.top().node_id == T){\n\n\t\t\tprintf(\"%.10lf\\n\",Q.top().sum_dist);\n\t\t\treturn;\n\n\t\t}else{\n\n\t\t\tfor(int i = 0; i < G[Q.top().node_id].size(); i++){\n\n\t\t\t\tnext_node = G[Q.top().node_id][i].to;\n\t\t\t\tnext_dist = Q.top().sum_dist+G[Q.top().node_id][i].dist;\n\n\t\t\t\tif(min_dist[next_node] > next_dist+EPS){\n\t\t\t\t\tmin_dist[next_node] = next_dist;\n\t\t\t\t\tQ.push(State(next_node,next_dist));\n\t\t\t\t}\n\t\t\t}\n\t\t\tQ.pop();\n\t\t}\n\t}\n\tprintf(\"-1\\n\");\n}\n\nbool rangeCheck(int value){\n\n\treturn value >= 0 && value <= 2;\n}\n\n//tmp_pを含むcubeのインデックスを取得\nint get_index(Point3D tmp_p){\n\n\tint X = (int)(floor(tmp_p.x));\n\tint Y = (int)(floor(tmp_p.y));\n\tint Z = (int)(floor(tmp_p.z));\n\n\t//printf(\"tmp_p.z:%.3lf Z:%d\\n\",tmp_p.z,Z);\n\n\tif(rangeCheck(X)&&rangeCheck(Y)&&rangeCheck(Z)){\n\n\t\treturn REV[X][Y][Z];\n\n\t}else{\n\n\t\treturn -1;\n\t}\n}\n\n\n\nvoid calc_ADJ(){\n\n\tnum_loc = 0;\n\n\tfor(int Y = 0; Y <= 2; Y++){\n\t\tfor(int Z = 0; Z <= 2; Z++){\n\t\t\tfor(int X = 0; X <= 2; X++){\n\n\t\t\t\tloc[num_loc].set(X,Y,Z);\n\t\t\t\tREV[X][Y][Z] = num_loc; //★★左上手前の点の逆引き表★★\n\t\t\t\tnum_loc++;\n\t\t\t}\n\t\t}\n\t}\n\n\tfor(int i = 0; i <= 26; i++){\n\t\tfor(int k = 0; k < 6; k++){\n\n\t\t\tint adj_X = loc[i].X+loc_X[k];\n\t\t\tint adj_Y = loc[i].Y+loc_Y[k];\n\t\t\tint adj_Z = loc[i].Z+loc_Z[k];\n\n\t\t\tif(adj_X >= 0 && adj_X <= 2 && adj_Y >= 0 &&\n\t\t\t\t\tadj_Y <= 2 && adj_Z >= 0 && adj_Z <= 2){\n\n\t\t\t\tfor(int a = 0; a <= 26; a++){\n\t\t\t\t\tif(adj_X == loc[a].X && adj_Y == loc[a].Y && adj_Z == loc[a].Z){\n\t\t\t\t\t\tADJ[i][k] = a;\n\t\t\t\t\t\tbreak;\n\t\t\t\t\t}\n\t\t\t\t}\n\n\t\t\t}else{\n\n\t\t\t\tADJ[i][k] = -1;\n\t\t\t}\n\t\t}\n\t}\n}\n\nvoid calc_PL(){\n\n\t//まずは線分の両端の8点列挙\n\tfor(int i = 0; i <= 26; i++){\n\t\tfor(int k = 0; k <= 7; k++){\n\t\t\tint X = loc[i].X+D_X[k];\n\t\t\tint Y = loc[i].Y+D_Y[k];\n\t\t\tint Z = loc[i].Z+D_Z[k];\n\n\t\t\tfirst_D[i][k].set(X,Y,Z);\n\t\t}\n\t}\n\n\t//indexに変換する\n\tnum_data = 0;\n\tfor(int i = 0; i <= 26; i++){\n\t\tfor(int k = 0; k <= 7; k++){\n\n\t\t\tint X = first_D[i][k].X;\n\t\t\tint Y = first_D[i][k].Y;\n\t\t\tint Z = first_D[i][k].Z;\n\n\t\t\tbool FLG = false;\n\t\t\tfor(int a = 0; a < num_data; a++){\n\t\t\t\tif(X == data[a].X && Y == data[a].Y && Z == data[a].Z){\n\t\t\t\t\tD[i][k] = a;\n\t\t\t\t\tFLG = true;\n\t\t\t\t\tbreak;\n\t\t\t\t}\n\t\t\t}\n\n\t\t\tif(!FLG){\n\t\t\t\tD[i][k] = num_data;\n\t\t\t\tdata[num_data].set(X,Y,Z);\n\t\t\t\tnum_data++;\n\t\t\t}\n\t\t}\n\t}\n\n\tfor(int i = 0; i < num_data; i++){\n\n\t\tdata[i].copy();\n\t}\n\n\tnum_POINT = 0;\n\tfor(int i = 0; i < num_data; i++){\n\t\tPOINT[i].X = data[i].X;\n\t\tPOINT[i].Y = data[i].Y;\n\t\tPOINT[i].Z = data[i].Z;\n\t\tPOINT[i].copy();\n\t\tnum_POINT++;\n\t}\n\n\t//6面の4辺を列挙する\n\tnum_seg = 0;\n\tint tmp_index;\n\n\tfor(int i = 0; i <= 26; i++){\n\t\tfor(int k = 0; k < 6; k++){ //面\n\t\t\tfor(int p = 0; p < 4; p++){\n\t\t\t\tint from = D[i][L_array[have_L[k][p]][0]];\n\t\t\t\tint to = D[i][L_array[have_L[k][p]][1]];\n\n\t\t\t\tif(from > to){\n\n\t\t\t\t\tswap(from,to);\n\t\t\t\t}\n\t\t\t\ttmp_index = -1;\n\t\t\t\tfor(int a = 0; a < num_seg; a++){\n\t\t\t\t\tif(seg[a].A == from && seg[a].B == to){\n\n\t\t\t\t\t\ttmp_index = a;\n\t\t\t\t\t\tbreak;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\tif(tmp_index == -1){\n\n\t\t\t\t\ttmp_index = num_seg;\n\t\t\t\t\tseg[num_seg].A = from;\n\t\t\t\t\tseg[num_seg].B = to;\n\t\t\t\t\tnum_seg++;\n\t\t\t\t}\n\t\t\t\tfour_line[i][k][p] = tmp_index;\n\t\t\t}\n\t\t}\n\t}\n\n\t//辺上の点を計算\n\tfor(int i = 0; i < num_seg; i++){\n\t\tinfo.clear();\n\t\tinfo.push_back(Info(POINT[seg[i].A],seg[i].A));\n\t\tinfo.push_back(Info(POINT[seg[i].B],seg[i].B));\n\t\tsort(info.begin(),info.end());\n\t\tPoint3D p_L = info[0].p,p_R = info[1].p;\n\t\tfor(int ALL = 2; ALL <= 9; ALL++){\n\t\t\tfor(int left = 1; left <= ALL-1; left++){\n\t\t\t\tint right = ALL-left;\n\t\t\t\tPoint3D tmp_p = (p_R*(double)(left)+p_L*(double)(right))/(double)ALL;\n\n\t\t\t\tint tmp_id = -1;\n\t\t\t\tfor(int a = 0; a < num_POINT; a++){\n\n\t\t\t\t\tif(POINT[a] == tmp_p){\n\n\t\t\t\t\t\ttmp_id = a;\n\t\t\t\t\t\tbreak;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\tif(tmp_id == -1){\n\t\t\t\t\ttmp_id = num_POINT;\n\t\t\t\t\tPOINT[num_POINT] = tmp_p;\n\t\t\t\t\tPOINT[num_POINT].X = -1;\n\t\t\t\t\tnum_POINT++;\n\t\t\t\t}\n\t\t\t\tinfo.push_back(Info(tmp_p,tmp_id));\n\t\t\t}\n\t\t}\n\t\tsort(info.begin(),info.end());\n\t\tinfo.erase(unique(info.begin(),info.end()),info.end());\n\n\t\tfor(int a = 0; a < info.size(); a++){\n\n\t\t\tLINE[i].push_back(info[a].index);\n\t\t}\n\t}\n}\n\n\nint main(){\n\n\tcalc_ADJ();\n\tcalc_PL();\n\n\twhile(true){\n\n\t\tscanf(\"%d %d %d %d %d %d\",&start.X,&start.Y,&start.Z,&goal.X,&goal.Y,&goal.Z);\n\t\tif(start.X == 0 && start.Y == 0 && start.Z == 0 && goal.X == 0 && goal.Y == 0 && goal.Z == 0)break;\n\n\t\tfunc();\n\t}\n\n\treturn 0;\n}", "accuracy": 1, "time_ms": 2160, "memory_kb": 25572, "score_of_the_acc": -1.0032, "final_rank": 3 }, { "submission_id": "aoj_1334_1630190", "code_snippet": "#include<stdio.h>\n#include<algorithm>\n#include<vector>\n#include<queue>\n#include<math.h>\nusing namespace std;\nchar in[3][3][4];\nint DIV=7;\nint gcd(int a,int b){\n\twhile(a){\n\t\tb%=a;int c=a;a=b;b=c;\n\t}return b;\n}\nconst double EPS = 1e-10;\nconst double INF = 1e+10;\nconst double PI = acos(-1);\nint sig(double r) { return (r < -EPS) ? -1 : (r > +EPS) ? +1 : 0; }\ninline double ABS(double a){return max(a,-a);}\nstruct Pt {\n\tdouble x, y,z;\n\tPt() {}\n\tPt(double x, double y,double z) : x(x), y(y),z(z) {}\n\tPt operator+(const Pt &a) const { return Pt(x + a.x, y + a.y,z+a.z); }\n\tPt operator-(const Pt &a) const { return Pt(x - a.x, y - a.y,z-a.z); }\n//\tPt operator*(const Pt &a) const { return Pt(x * a.x - y * a.y, x * a.y + y * a.x); }\n\tPt operator-() const { return Pt(-x, -y,-z); }\n\tPt operator*(const double &k) const { return Pt(x * k, y * k,z*k); }\n\tPt operator/(const double &k) const { return Pt(x / k, y / k,z/k); }\n\tdouble ABS() const { return sqrt(x * x + y * y+z*z); }\n\tdouble abs2() const { return x * x + y * y+z*z; }\n//\tdouble arg() const { return atan2(y, x); }\n\tdouble dot(const Pt &a) const { return x * a.x + y * a.y+z*a.z; }\n//\tdouble det(const Pt &a) const { return x * a.y - y * a.x; }\n};\ndouble ijk[110000];\nint v[110000];\nint can[4][4][4];\nint canx[4][4][4];\nint cany[4][4][4];\nint canz[4][4][4];\nint canxy[4][4][4];\nint canyz[4][4][4];\nint canzx[4][4][4];\nint rev[110000];\nchar ss[4];\nint main(){\n\tint a,b,c,d,e,f;\n\twhile(scanf(\"%d%d%d%d%d%d\",&a,&b,&c,&d,&e,&f),a+b+c+d+e+f){\n\t\tfor(int i=0;i<3;i++)for(int j=0;j<3;j++){\n\t\t\tscanf(\"%s\",ss);\n\t\t\tfor(int k=0;k<3;k++)in[k][j][i]=ss[k];\n\t\t}\n\t\tvector<Pt> hb;\n\t\tfor(int i=0;i<4;i++)for(int j=0;j<4;j++)for(int k=0;k<4;k++){\n\t\t\tcan[i][j][k]=canx[i][j][k]=cany[i][j][k]=canz[i][j][k]=0;\n\t\t\tcanxy[i][j][k]=canyz[i][j][k]=canzx[i][j][k]=0;\n\t\t}\n\t\tfor(int i=0;i<4;i++)for(int j=0;j<4;j++)for(int k=0;k<4;k++){\n\t\t\tif(i==0||i==3||j==0||j==3||k==0||k==3)can[i][j][k]=2;\n\t\t\tif(j==0||j==3||k==0||k==3)canx[i][j][k]=2;\n\t\t\tif(i==0||i==3||k==0||k==3)cany[i][j][k]=2;\n\t\t\tif(i==0||i==3||j==0||j==3)canz[i][j][k]=2;\n\t\t\tif(k==0||k==3)canxy[i][j][k]=2;\n\t\t\tif(i==0||i==3)canyz[i][j][k]=2;\n\t\t\tif(j==0||j==3)canzx[i][j][k]=2;\n\t\t}\n\t\tfor(int i=0;i<3;i++)for(int j=0;j<3;j++)for(int k=0;k<3;k++){\n\t\t\tint key=0;\n\t\t\tif(in[i][j][k]=='#'){\n\t\t\t\tkey=1;\n\t\t\t}else{\n\t\t\t\tkey=2;\n\t\t\t}\n\t\t\tfor(int l=0;l<8;l++){\n\t\t\t\tcan[i+l/4][j+l%4/2][k+l%2]|=key;\n\t\t\t}\n\t\t\tfor(int l=0;l<4;l++){\n\t\t\t\tcanx[i][j+l/2][k+l%2]|=key;\n\t\t\t\tcany[i+l/2][j][k+l%2]|=key;\n\t\t\t\tcanz[i+l/2][j+l%2][k]|=key;\n\t\t\t}\n\t\t\tfor(int l=0;l<2;l++){\n\t\t\t\tcanxy[i][j][k+l%2]|=key;\n\t\t\t\tcanyz[i+l%2][j][k]|=key;\n\t\t\t\tcanzx[i][j+l%2][k]|=key;\n\t\t\t}\n\t\t}\n\t\t/*for(int i=0;i<4;i++){\n\t\t\tfor(int j=0;j<4;j++){\n\t\t\t\tfor(int k=0;k<4;k++)printf(\"%d \",can[k][j][i]);\n\t\t\t\tprintf(\"\\n\");\n\t\t\t}\n\t\t\tprintf(\"\\n\");\n\t\t}*/\n\t\tfor(int i=0;i<=3;i++){\n\t\t\tfor(int j=0;j<=3;j++){\n\t\t\t\tfor(int k=0;k<=3;k++){\n\t\t\t\t\tif(can[i][j][k]==3)hb.push_back(Pt((double)i,(double)j,(double)k));\n\t\t\t\t}\n\t\t\t\tfor(int k=0;k<3;k++){\n\t\t\t\t\tfor(int l=2;l<=DIV;l++){\n\t\t\t\t\t\tfor(int m=1;m<l;m++){\n\t\t\t\t\t\t\tif(gcd(m,l)!=1)continue;\n\t\t\t\t\t\t\tif(canz[i][j][k]==3)hb.push_back(Pt((double)i,(double)j,k+(double)m/l));\n\t\t\t\t\t\t\tif(cany[i][k][j]==3)hb.push_back(Pt((double)i,k+(double)m/l,(double)j));\n\t\t\t\t\t\t\tif(canx[k][i][j]==3)hb.push_back(Pt(k+(double)m/l,(double)i,(double)j));\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\tint sz=hb.size();\n\t\tfor(int i=0;i<sz;i++){\n\t\t\tv[i]=0;\n\t\t\tijk[i]=99999999;\n\t\t}\n\t\tPt start=Pt(a,b,c);\n\t\tPt goal=Pt(d,e,f);\n\t\tfor(int i=0;i<sz;i++){\n\t\t\tif((hb[i]-start).ABS()<EPS){\n\t\t\t\tijk[i]=0;\n\t\t\t}\n\t\t}\n\t\tfor(int i=0;i<sz;i++){\n\t\t\tint at=0;\n\t\t\tdouble dist=999999999;\n\t\t\tfor(int j=0;j<sz;j++){\n\t\t\t\tif(!v[j]&&dist>ijk[j]){\n\t\t\t\t\tdist=ijk[j];at=j;\n\t\t\t\t}\n\t\t\t}\n\t\t\tv[at]=1;\n\t\t//\tprintf(\"%f %f %f: %f\\n\",hb[at].x,hb[at].y,hb[at].z,ijk[at]);\n\t\t\tif((hb[at]-goal).ABS()<EPS){\n\t\t\t\tprintf(\"%.12f\\n\",dist);\n\t\t\t\tint cur=at;\n\t\t\t\t//while((hb[cur]-start).ABS()>EPS){\n\t\t\t\t//\tcur=rev[cur];\n\t\t\t\t//\tprintf(\"%f %f %f: %f\\n\",hb[cur].x,hb[cur].y,hb[cur].z,ijk[cur]);\n\t\t\t\t//}\n\t\t\t\tbreak;\n\t\t\t}\n\t\t\tfor(int j=0;j<sz;j++){\n\t\t\t\tif(v[j])continue;\n\t\t\t\tbool X=false;\n\t\t\t\tbool Y=false;\n\t\t\t\tbool Z=false;\n\t\t\t\tint xx=(int)(min(hb[j].x,hb[at].x)+EPS);\n\t\t\t\tint yy=(int)(min(hb[j].y,hb[at].y)+EPS);\n\t\t\t\tint zz=(int)(min(hb[j].z,hb[at].z)+EPS);\n\t\t\t\tif(ABS(hb[j].x-hb[at].x)<EPS&&ABS(hb[j].x-xx)<EPS)X=true;\n\t\t\t\tif(ABS(hb[j].y-hb[at].y)<EPS&&ABS(hb[j].y-yy)<EPS)Y=true;\n\t\t\t\tif(ABS(hb[j].z-hb[at].z)<EPS&&ABS(hb[j].z-zz)<EPS)Z=true;\n\t\t\t\tif(!X&&!Y&&!Z)continue;\n\t\t\t\tif(!X&&(int)(min(hb[j].x+1,hb[at].x+1)+EPS)!=(int)(max(hb[at].x+1,hb[j].x+1)-EPS))continue;\n\t\t\t\tif(!Y&&(int)(min(hb[j].y+1,hb[at].y+1)+EPS)!=(int)(max(hb[at].y+1,hb[j].y+1)-EPS))continue;\n\t\t\t\tif(!Z&&(int)(min(hb[j].z+1,hb[at].z+1)+EPS)!=(int)(max(hb[at].z+1,hb[j].z+1)-EPS))continue;\n\t\t\t\tbool can=false;\n\t\t\t\tbool Xs=false;\n\t\t\t\tbool Ys=false;\n\t\t\t\tbool Zs=false;\n\t\t\t\tif(ABS(hb[j].x-(int)(hb[j].x+EPS))<EPS)Xs=true;\n\t\t\t\tif(ABS(hb[j].y-(int)(hb[j].y+EPS))<EPS)Ys=true;\n\t\t\t\tif(ABS(hb[j].z-(int)(hb[j].z+EPS))<EPS)Zs=true;\n\t\t\t\tif(X&&canyz[xx][yy][zz]==3)can=true;\n\t\t\t\tif(Y&&canzx[xx][yy][zz]==3)can=true;\n\t\t\t\tif(Z&&canxy[xx][yy][zz]==3)can=true;\n\t\t\t\tif(X&&Y&&Xs&&Ys&&canz[xx][yy][zz]==3)can=true;\n\t\t\t\tif(Z&&Y&&Zs&&Ys&&canx[xx][yy][zz]==3)can=true;\n\t\t\t\tif(X&&Z&&Xs&&Zs&&cany[xx][yy][zz]==3)can=true;\n\t\t\t\tif(can){\n\t\t\t\t\tdouble tmp=(hb[j]-hb[at]).ABS();\n\t\t\t\t\tif(ijk[j]>tmp+ijk[at]){\n\t\t\t\t\t\tijk[j]=tmp+ijk[at];\n\t\t\t\t\t\trev[j]=at;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n}", "accuracy": 1, "time_ms": 5610, "memory_kb": 1252, "score_of_the_acc": -0.7716, "final_rank": 1 }, { "submission_id": "aoj_1334_1522128", "code_snippet": "#include <stdio.h>\n#include <math.h>\n#include <vector>\n#include <queue>\n#include <utility>\n#include <algorithm>\nusing namespace std;\n\nstruct point{\n int x, y, z;\n point(int x_, int y_, int z_) : x(x_), y(y_), z(z_) {}\n bool operator< (const point& rhs) const {\n return false;\n }\n};\n\nconst int dv = 18;\nconst int sd = 420;\n\nint b[5][5][5];\nint dvs[dv + 1] = {0, 60, 70, 84, 105, 120, 140, 168, 180, 210, 240, 252, 280, 300, 315, 336, 350, 360};\nint rot[dv * 4 + 1][2];\n\nint xtov[dv * 3 + 1];\n\nvector<point> ed[dv * 3 + 1][dv * 3 + 1][dv * 3 + 1];\npriority_queue< pair<double, point> > pq;\nbool vis[dv * 3 + 1][dv * 3 + 1][dv * 3 + 1];\n\ndouble dist(int xa, int ya, int za, int xb, int yb, int zb){\n int dx = xa - xb, dy = ya - yb, dz = za - zb;\n double d = sqrt(dx * dx + dy * dy + dz * dz);\n return d;\n}\n\nint main(){\n int i, j, k;\n for(i = 0; i <= dv * 3 + 1; i++){\n if(i < dv) xtov[i] = dvs[i];\n else xtov[i] = xtov[i - dv] + sd;\n }\n for(i = 0; i < dv; i++){\n rot[i][0] = i;\n rot[i][1] = 0;\n rot[dv + i][0] = dv;\n rot[dv + i][1] = i;\n rot[2 * dv + i][0] = dv - i;\n rot[2 * dv + i][1] = dv;\n rot[3 * dv + i][0] = 0;\n rot[3 * dv + i][1] = dv - i;\n }\n\n for(;;){\n int x1, y1, z1;\n int x2, y2, z2;\n scanf(\"%d%d%d\", &x1, &y1, &z1);\n scanf(\"%d%d%d\", &x2, &y2, &z2);\n if(x1 == 0 && y1 == 0 && z1 == 0\n && x2 == 0 && y2 == 0 && z2 == 0) break;\n\n for(i = 0; i <= 3 * dv; i++){\n for(j = 0; j <= 3 * dv; j++){\n for(k = 0; k <= 3 * dv; k++){\n ed[i][j][k].clear();\n vis[i][j][k] = false;\n }\n }\n }\n while(!pq.empty()) pq.pop();\n\n char tmp[5];\n for(i = 0; i < 3; i++){\n for(j = 0; j < 3; j++){\n scanf(\"%s\", tmp);\n for(k = 0; k < 3; k++){\n b[k][j][i] = (tmp[k] == '#');\n }\n }\n }\n\n for(i = 0; i <= 3; i++){\n for(j = 0; j <= 3; j++){\n for(k = 0; k <= 3; k++){\n // (i, j, k) (i + 1, j, k) (i, j + 1, k) (i + 1, j + 1, k)\n // square (i, j, k - 1), (i, j, k)\n if(i < 3 && j < 3 && (k == 0 ? 0 : b[i][j][k - 1]) + b[i][j][k] == 1){\n for(int l0 = 0; l0 < 4 * dv; l0++){\n for(int l1 = l0 + 1; l1 < 4 * dv; l1++){\n // (i * dv + rot[l][0], j * dv + rot[l][1], k * dv)\n int nx0 = i * dv + rot[l0][0], ny0 = j * dv + rot[l0][1], nz0 = k * dv;\n int nx1 = i * dv + rot[l1][0], ny1 = j * dv + rot[l1][1], nz1 = k * dv;\n ed[nx0][ny0][nz0].push_back(point(nx1, ny1, nz1));\n ed[nx1][ny1][nz1].push_back(point(nx0, ny0, nz0));\n }\n }\n }\n // (i, j, k) (i + 1, j, k) (i, j, k + 1) (i + 1, j, k + 1)\n // square (i, j - 1, k), (i, j, k)\n if(i < 3 && k < 3 && (j == 0 ? 0 : b[i][j - 1][k]) + b[i][j][k] == 1){\n for(int l0 = 0; l0 < 4 * dv; l0++){\n for(int l1 = l0 + 1; l1 < 4 * dv; l1++){\n // (i * dv + rot[l][0], j * dv, k * dv + rot[l][1])\n int nx0 = i * dv + rot[l0][0], ny0 = j * dv, nz0 = k * dv + rot[l0][1];\n int nx1 = i * dv + rot[l1][0], ny1 = j * dv, nz1 = k * dv + rot[l1][1];\n ed[nx0][ny0][nz0].push_back(point(nx1, ny1, nz1));\n ed[nx1][ny1][nz1].push_back(point(nx0, ny0, nz0));\n }\n }\n }\n // (i, j, k) (i, j + 1, k) (i, j, k + 1) (i, j + 1, k + 1)\n // square (i - 1, j, k), (i, j, k)\n if(j < 3 && k < 3 && (i == 0 ? 0 : b[i - 1][j][k]) + b[i][j][k] == 1){\n for(int l0 = 0; l0 < 4 * dv; l0++){\n for(int l1 = l0 + 1; l1 < 4 * dv; l1++){\n // (i * dv, j * dv + rot[l][0], k * dv + rot[l][1])\n int nx0 = i * dv, ny0 = j * dv + rot[l0][0], nz0 = k * dv + rot[l0][1];\n int nx1 = i * dv, ny1 = j * dv + rot[l1][0], nz1 = k * dv + rot[l1][1];\n ed[nx0][ny0][nz0].push_back(point(nx1, ny1, nz1));\n ed[nx1][ny1][nz1].push_back(point(nx0, ny0, nz0));\n }\n }\n }\n }\n }\n }\n\n pq.push(make_pair(0.0, point(x1 * dv, y1 * dv, z1 * dv)));\n double ans = 0.0;\n\n while(!pq.empty()){\n double nd = pq.top().first;\n int nx = pq.top().second.x, ny = pq.top().second.y, nz = pq.top().second.z;\n pq.pop();\n\n if(vis[nx][ny][nz]) continue;\n vis[nx][ny][nz] = true;\n\n if(nx == x2 * dv && ny == y2 * dv && nz == z2 * dv){\n ans = nd;\n break;\n }\n\n for(point pp : ed[nx][ny][nz]){\n if(!vis[pp.x][pp.y][pp.z]){\n pq.push(make_pair(nd - dist(xtov[nx], xtov[ny], xtov[nz], xtov[pp.x], xtov[pp.y], xtov[pp.z]), pp));\n }\n }\n }\n printf(\"%.20lf\\n\", -ans / sd);\n }\n return 0;\n}", "accuracy": 1, "time_ms": 4280, "memory_kb": 16128, "score_of_the_acc": -1.0863, "final_rank": 4 }, { "submission_id": "aoj_1334_1245989", "code_snippet": "#include<cstdio>\n#include<vector>\n#include<cmath>\n#include<queue>\n#include<cassert>\n#include<utility>\n#include<algorithm>\n\nusing namespace std;\n\ntypedef double Real;\ntypedef pair<int,int> P;\n\nconst Real eps=1e-7;\nconst Real inf=1e9;\n\ntemplate<class T> bool eq(T a,T b){\n\treturn abs(a-b)<eps;\n}\ntemplate<class T> int sgn(T a){\n\tif(eq(a,0.0)) return 0;\n\tif(a>0) return 1;\n\treturn -1;\n}\n\n//vector<Real> vals;\nReal vals[140];\nint vals_id=0;\n//vector<int> valid_vnum;\nint valid_vnum[7000];\nint valid_vnum_id=0;\n\nint rev_valid_vnum[140*140*140];\n\nint encode(Real x,Real y,Real z){\n//\tint xid = distance(vals.begin(),lower_bound(vals.begin(),vals.end(),x-eps));\n//\tint yid = distance(vals.begin(),lower_bound(vals.begin(),vals.end(),y-eps));\n//\tint zid = distance(vals.begin(),lower_bound(vals.begin(),vals.end(),z-eps));\n\tint xid=lower_bound(vals,vals+vals_id,x-eps)-vals;\n\tint yid=lower_bound(vals,vals+vals_id,y-eps)-vals;\n\tint zid=lower_bound(vals,vals+vals_id,z-eps)-vals;\n//\tint v=xid + vals.size()*yid + vals.size()*vals.size()*zid;\n\tint v=xid + vals_id*yid + vals_id*vals_id*zid;\n//\tint res = distance(valid_vnum.begin(),lower_bound(valid_vnum.begin(),valid_vnum.end(),v));\n//\tint res = lower_bound(valid_vnum,valid_vnum+valid_vnum_id,v)-valid_vnum;\n\tint res = rev_valid_vnum[v];\n\treturn res;\n}\n\nconst int V = 10000;// 140*140*140;\n\nint head[V],to[500500],nxt[500500],m;\nReal cost[500500];\nvoid init_edge(){\n\tfor(int i=0;i<V;i++) head[i]=-1;\n\tm=0;\n}\nvoid add_edge(int u,int v,Real c){\n\tnxt[m]=head[u];to[m]=v;cost[m]=c;head[u]=m;m++;\n\tnxt[m]=head[v];to[m]=u;cost[m]=c;head[v]=m;m++;\n\tif(m>500000) assert(0);\n}\n\n//vector<Real> as,bs,cs,ds;\nReal as[2200],bs[2200],cs[2200],ds[2200];\nint aid=0;\ntypedef pair<Real,Real> PR;\nint gcd(int x,int y){\n\tif(x<y) return gcd(y,x);\n\tif(y==0) return x;\n\treturn gcd(y,x%y);\n}\n\nPR tmp[30];\n\nvoid genEdges(){\n\tfor(int i=1;i<=12;i++) for(int j=1;j<=12;j++){\n\t\tif(gcd(i,j)!=1) continue;\n\t//\tvector<PR> tmp;\n\t\tint tmp_id=0;\n\t\tfor(int k=1;k<i;k++){\n\t\t\tReal y=(Real)k*j/i;\n\t\t//\ttmp.push_back(PR(k,y));\n\t\t\ttmp[tmp_id++]=PR(k,y);\n\t\t}\n\t\tfor(int k=1;k<j;k++){\n\t\t\tReal x=(Real)i*k/j;\n\t\t//\ttmp.push_back(PR(x,k));\n\t\t\ttmp[tmp_id++]=PR(x,k);\n\t\t}\n\t\t//tmp.push_back(PR(0,0));\n\t\t//tmp.push_back(PR(i,j));\n\t\ttmp[tmp_id++]=PR(0,0);\n\t\ttmp[tmp_id++]=PR(i,j);\n//\t\tsort(tmp.begin(),tmp.end());\n\t\tsort(tmp,tmp+tmp_id);\n/*\t\tif(i == 3 && j == 4){\n\t\t\tfor(int k = 0; k < tmp.size(); k++){\n\t\t\t\tprintf(\"%f %f\\n\",tmp[k].first,tmp[k].second);\n\t\t\t}\n//\t\t\texit(0);\n\t\t}*/\n\t//\tfor(int k=0;k+1<tmp.size();k++){\n\t\tfor(int k=0;k+1<tmp_id;k++){\n\t\t\tReal a_=tmp[k].first;\n\t\t\tReal a=a_-(int)(a_+eps);\n\t\t\tReal b_=tmp[k].second;\n\t\t\tReal b=b_-(int)(b_+eps);\n\t\t\tReal c_=tmp[k+1].first;\n\t\t\tReal c=c_-(int)(c_-eps);\n\t\t\tReal d_=tmp[k+1].second;\n\t\t\tReal d=d_-(int)(d_-eps);\n\t\t/*\tas.push_back(a);\n\t\t\tbs.push_back(b);\n\t\t\tcs.push_back(c);\n\t\t\tds.push_back(d);\n\t\t\tas.push_back(1-a);\n\t\t\tbs.push_back(b);\n\t\t\tcs.push_back(1-c);\n\t\t\tds.push_back(d);*/\n\t\t\tas[aid]=a;\n\t\t\tbs[aid]=b;\n\t\t\tcs[aid]=c;\n\t\t\tds[aid]=d;\n\t\t\taid++;\n\t\t\tas[aid]=1-a;\n\t\t\tbs[aid]=b;\n\t\t\tcs[aid]=1-c;\n\t\t\tds[aid]=d;\n\t\t\taid++;\n//\t\t\tif(i == 3 && j == 4){\n//\t\t\t\tprintf(\"%f %f %f %f\\n\", a,b,c,d);\n//\t\t\t}\n\t\t}\n\t}\n/*\tfor(int i=0;i<as.size();i++){\n\t\tprintf(\"%.2f %.2f %.2f %.2f\\n\",as[i],bs[i],cs[i],ds[i]);\n\t}\n\texit(0);*/\n}\nReal getDis(Real a,Real b,Real c,Real p,Real q,Real r){\n\tReal d1=a-p,d2=b-q,d3=c-r;\n\treturn sqrt(d1*d1+d2*d2+d3*d3);\n}\n\nbool exi[5][5][5];\nvoid genGraph(){\n//\tint cntOut=0;\n/*\tfor(int i=0;i<140*140*140;i++){\n\t\tG[i].clear();\n\t}*/\n\tinit_edge();\n\tfor(int x=1;x<=4;x++){\n\t\tfor(int y=1;y<=3;y++) for(int z=1;z<=3;z++){\n\t\t\tint cnt=0;\n\t\t\tif(exi[x-1][y][z]) cnt++;\n\t\t\tif(exi[x][y][z]) cnt++;\n\t\t\tif(cnt!=1) continue;\n\t\t//\tfor(int i=0;i<as.size();i++){\n\t\t\tfor(int i=0;i<aid;i++){\n\t\t\t\tReal x1=x,y1_=y+as[i],z1=z+bs[i];\n\t\t\t\tReal x2=x,y2_=y+cs[i],z2=z+ds[i];\n\t\t\t\tint u=encode(x1,y1_,z1);\n\t\t\t\tint v=encode(x2,y2_,z2);\n\t\t\t\tReal d=getDis(x1,y1_,z1,x2,y2_,z2);\n\t\t\t\tadd_edge(u,v,d);\n\t\t//\t\tG[u].push_back(edge(v,d));\n\t\t//\t\tG[v].push_back(edge(u,d));\n\t\t//\t\tif(++cntOut<=20){\n\t\t\t\t\t//printf(\"%d %d\\n\",u,v);\n//\t\t\t\t\tprintf(\"%.2f %.2f %.2f %.2f %.2f %.2f\\n\",\n//\t\t\t\t\tx1,y1_,z1,x2,y2_,z2);\n\t\t//\t\t}\n\t\t\t}\n\t\t}\n\t}\n\tfor(int y=1;y<=4;y++){\n\t\tfor(int x=1;x<=3;x++) for(int z=1;z<=3;z++){\n\t\t\tint cnt=0;\n\t\t\tif(exi[x][y-1][z]) cnt++;\n\t\t\tif(exi[x][y][z]) cnt++;\n\t\t\tif(cnt!=1) continue;\n\t\t//\tfor(int i=0;i<as.size();i++){\n\t\t\tfor(int i=0;i<aid;i++){\n\t\t\t\tReal x1=x+as[i],y1_=y,z1=z+bs[i];\n\t\t\t\tReal x2=x+cs[i],y2_=y,z2=z+ds[i];\n\t\t\t\tint u=encode(x1,y1_,z1);\n\t\t\t\tint v=encode(x2,y2_,z2);\n\t\t\t\tReal d=getDis(x1,y1_,z1,x2,y2_,z2);\n\t\t\t\tadd_edge(u,v,d);\n\t\t\t//\tG[u].push_back(edge(v,d));\n\t\t\t//\tG[v].push_back(edge(u,d));\n\t\t\t}\n\t\t}\n\t}\n\tfor(int z=1;z<=4;z++){\n\t\tfor(int x=1;x<=3;x++) for(int y=1;y<=3;y++){\n\t\t\tint cnt=0;\n\t\t\tif(exi[x][y][z]) cnt++;\n\t\t\tif(exi[x][y][z-1]) cnt++;\n\t\t\tif(cnt!=1) continue;\n\t\t//\tfor(int i=0;i<as.size();i++){\n\t\t\tfor(int i=0;i<aid;i++){\n\t\t\t\tReal x1=x+as[i],y1_=y+bs[i],z1=z;\n\t\t\t\tReal x2=x+cs[i],y2_=y+ds[i],z2=z;\n\t\t\t\tint u=encode(x1,y1_,z1);\n\t\t\t\tint v=encode(x2,y2_,z2);\n\t\t\t\tReal d=getDis(x1,y1_,z1,x2,y2_,z2);\n\t\t\t\tadd_edge(u,v,d);\n\t\t\t//\tG[u].push_back(edge(v,d));\n\t\t\t//\tG[v].push_back(edge(u,d));\n\t\t\t}\n\t\t}\n\t}\n\t//TODO: add edges coincide with cubes' edges\n\tfor(int y=1;y<=4;y++) for(int z=1;z<=4;z++){\n\t\tfor(int x=1;x<=3;x++){\n\t\t\tint cnt=0;\n\t\t\tfor(int y_=y-1;y_<=y;y_++) for(int z_=z-1;z_<=z;z_++){\n\t\t\t\tif(exi[x][y_][z_]) cnt++;\n\t\t\t}\n\t\t\tif(cnt==0||cnt==4) continue;\n\t\t\tint u=encode(x,y,z);\n\t\t\tint v=encode(x+1,y,z);\n\t\t\tadd_edge(u,v,1);\n\t\t//\tG[u].push_back(edge(v,1));\n\t\t//\tG[v].push_back(edge(u,1));\n\t\t}\n\t}\n\tfor(int z=1;z<=4;z++) for(int x=1;x<=4;x++){\n\t\tfor(int y=1;y<=3;y++){\n\t\t\tint cnt=0;\n\t\t\tfor(int z_=z-1;z_<=z;z_++) for(int x_=x-1;x_<=x;x_++){\n\t\t\t\tif(exi[x_][y][z_]) cnt++;\n\t\t\t}\n\t\t\tif(cnt==0||cnt==4) continue;\n\t\t\tint u=encode(x,y,z);\n\t\t\tint v=encode(x,y+1,z);\n\t\t\tadd_edge(u,v,1);\n\t\t//\tG[u].push_back(edge(v,1));\n\t\t//\tG[v].push_back(edge(u,1));\n\t\t}\n\t}\n\tfor(int x=1;x<=4;x++) for(int y=1;y<=4;y++){\n\t\tfor(int z=1;z<=3;z++){\n\t\t\tint cnt=0;\n\t\t\tfor(int x_=x-1;x_<=x;x_++) for(int y_=y-1;y_<=y;y_++){\n\t\t\t\tif(exi[x_][y_][z]) cnt++;\n\t\t\t}\n\t\t\tif(cnt==0||cnt==4) continue;\n\t\t\tint u=encode(x,y,z);\n\t\t\tint v=encode(x,y,z+1);\n\t\t\tadd_edge(u,v,1);\n\t\t//\tG[u].push_back(edge(v,1));\n\t\t//\tG[v].push_back(edge(u,1));\n\t\t}\n\t}\n}\n\nbool checkInt(int id){\n\tReal x=vals[id];\n\tint x_=(int)(x+eps);\n\treturn eq(x,(Real)x_);\n}\nvoid init(){\n\tfor(int i=2;i<=12;i++){\n\t\tfor(int j=1;j<i;j++){\n\t\t\tif(gcd(i,j)!=1) continue;\n\t\t\tReal x=(Real)j/i;\n\t\t\tfor(int k=1;k<=3;k++){\n\t\t//\t\tvals.push_back(x+k);\n\t\t\t\tvals[vals_id++]=x+k;\n\t\t\t}\n\t\t}\n\t}\n//\tfor(int k=1;k<=4;k++) vals.push_back(k);\n\tfor(int k=1;k<=4;k++) vals[vals_id++]=k;\n//\tsort(vals.begin(),vals.end());\n\tsort(vals,vals+vals_id);\n\tgenEdges();\n//\tfor(int i=0;i<vals.size();i++) for(int j=0;j<vals.size();j++) for(int k=0;k<vals.size();k++){\n\tfor(int i=0;i<vals_id;i++) for(int j=0;j<vals_id;j++) for(int k=0;k<vals_id;k++){\n\t\tint cnt=0;\n\t\tif(checkInt(i)) cnt++;\n\t\tif(checkInt(j)) cnt++;\n\t\tif(checkInt(k)) cnt++;\n\t\tif(cnt<2) continue;\n//\t\tvalid_vnum.push_back(i+vals.size()*j+vals.size()*vals.size()*k);\n//\t\tvalid_vnum.push_back(i+vals_id*j+vals_id*vals_id*k);\n\t\tvalid_vnum[valid_vnum_id++]=i+vals_id*j+vals_id*vals_id*k;\n\t}\n\tfor(int i=0;i<valid_vnum_id;i++){\n\t\trev_valid_vnum[valid_vnum[i]]=i;\n\t}\n//\tprintf(\"%d\\n\",valid_vnum.size());\n//\tsort(valid_vnum.begin(),valid_vnum.end());\n//\tsort(valid_vnum,valid_vnum+valid_vnum_id);\n}\n\nReal dis[V];\n\ntypedef pair<Real,int> State;\npriority_queue<State,vector<State>,greater<State> > que;\n\nReal solve(int x1,int y1,int z1,int x2,int y2,int z2){\n//\tprintf(\"%d %d %d %d %d %d\\n\",x1,y1,z1,x2,y2,z2);\n\tint u=encode(x1,y1,z1);\n\tint v=encode(x2,y2,z2);\n\tgenGraph();\n//\tprintf(\"u=%d,v=%d\\n\",u,v);\n//\tint V=140*140*140;\n\tfor(int i=0;i<V;i++){\n\t\tdis[i]=inf;\n\t}\n\tdis[u]=0;\n\tque.push(State(0,u));\n\twhile(!que.empty()){\n\t\tState state = que.top();\n\t\tque.pop();\n\t\tReal cd = state.first;\n\t\tint cv = state.second;\n//\t\tprintf(\"cv=%d\\n\",cv);\n\t\tif(sgn(dis[cv]-cd)>0) continue;\n//\t\tfor(int i=0;i<G[cv].size();i++){\n\t\tfor(int e=head[cv];e!=-1;e=nxt[e]){\n\t\t//\tint nv = G[cv][i].to;\n\t\t\tint nv=to[e];\n\t\t//\tReal nd = cd + G[cv][i].cost;\n\t\t\tReal nd=cd+cost[e];\n\t\t\tif(sgn(dis[nv]-nd)<=0) continue;\n\t\t\tdis[nv]=nd;\n\t\t\tque.push(State(nd,nv));\n\t\t}\n\t}\n\treturn dis[v];\n}\n\nint x1,y1_,z1,x2,y2_,z2;\n\nvoid input(){\n\tscanf(\"%d%d%d%d%d%d\",&x1,&y1_,&z1,&x2,&y2_,&z2);\n\tif(x1==0&&y1_==0&&z1==0&&x2==0&&y2_==0&&z2==0) exit(0);\n\tx1++;y1_++;z1++;x2++;y2_++;z2++;\n\tfor(int i=1;i<=3;i++) for(int j=1;j<=3;j++) for(int k=1;k<=3;k++){\n\t\texi[i][j][k]=false;\n\t}\n\tfor(int z=1;z<=3;z++) for(int y=1;y<=3;y++){\n\t\tchar ch[10];\n\t\tscanf(\"%s\",ch+1);\n\t\tfor(int x=1;x<=3;x++){\n\t\t\texi[x][y][z]=ch[x]=='#';\n\t\t}\n\t}\n}\n\nint main(){\n\tinit();\n//\tprintf(\"%d %d\\n\",vals.size(),as.size());\n//\tint t = 0;\n//\tprintf(\"as.size()=%d\\n\",as.size());\n//\tprintf(\"valid_vnum.size()=%d\\n\",valid_vnum.size());\n//\tprintf(\"vals.size()=%d\\n\",vals.size());\n\twhile(true){\n//\t\tfprintf(stderr,\"%d\\n\",t++);\n\t\tinput();\n\t\tReal ans=solve(x1,y1_,z1,x2,y2_,z2);\n\t\tprintf(\"%f\\n\",ans);\n//\t\tint s = encode(1,1,1);\n//\t\tint t = encode(4,4,4);\n//\t\tint mid = encode(1.5,1,2);\n//\t\tprintf(\"[%d] = %f\\n\",s,dis[s]);\n//\t\tprintf(\"[%d] = %f\\n\",t,dis[t]);\n//\t\tprintf(\"[%d] = %f\\n\",mid,dis[mid]);\n/*\t\tint cnt = 0;\n\t\tfor(int i = 0; i < 140*140*140;i++){\n\t\t\tif(dis[i] < 1e9){\n\t\t\t\tif(++cnt<=10){\n\t\t\t\t\tdecodeAndPrint(i);\n\t\t\t\t\tprintf(\"%d->%f\\n\",i,dis[i]);\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\texit(0);*/\n\t}\n\treturn 0;\n}", "accuracy": 1, "time_ms": 6640, "memory_kb": 19988, "score_of_the_acc": -1.7677, "final_rank": 5 } ]
aoj_1335_cpp
Problem A: Equal Sum Sets Let us consider sets of positive integers less than or equal to n . Note that all elements of a set are different. Also note that the order of elements doesn't matter, that is, both {3, 5, 9} and {5, 9, 3} mean the same set. Specifying the number of set elements and their sum to be k and s , respectively, sets satisfying the conditions are limited. When n = 9, k = 3 and s = 23, {6, 8, 9} is the only such set. There may be more than one such set, in general, however. When n = 9, k = 3 and s = 22, both {5, 8, 9} and {6, 7, 9} are possible. You have to write a program that calculates the number of the sets that satisfy the given conditions. Input The input consists of multiple datasets. The number of datasets does not exceed 100. Each of the datasets has three integers n , k and s in one line, separated by a space. You may assume 1 ≤ n ≤ 20, 1 ≤ k ≤ 10 and 1 ≤ s ≤ 155. The end of the input is indicated by a line containing three zeros. Output The output for each dataset should be a line containing a single integer that gives the number of the sets that satisfy the conditions. No other characters should appear in the output. You can assume that the number of sets does not exceed 2 31 - 1. Sample Input 9 3 23 9 3 22 10 3 28 16 10 107 20 8 102 20 10 105 20 10 155 3 4 3 4 2 11 0 0 0 Output for the Sample Input 1 2 0 20 1542 5448 1 0 0
[ { "submission_id": "aoj_1335_11030135", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\nusing a2 = array<ll, 2>;\nusing a3 = array<ll, 3>;\n\nbool chmin(auto& a, const auto& b) { return a > b ? a = b, 1 : 0; }\nbool chmax(auto& a, const auto& b) { return a < b ? a = b, 1 : 0; }\n\nll mod = 998244353;\n\nifstream in;\nofstream out;\n\nusing ld = long double;\nconst ld eps = 1e-9;\nlong double calc_inter(array<ld, 3> a, array<ld, 3> b, array<ld, 3> p, ld r) {\n ld x = b[0] - a[0], y = b[1] - a[1], z = b[2] - a[2];\n ld t = -(x * (a[0] - p[0]) + y * (a[1] - p[1]) + z * (a[2] - p[2])) /\n (x * x + y * y + z * z);\n\n if(t < 0 || t > 1) return false;\n x = p[0] - (a[0] + t * x);\n y = p[1] - (a[1] + t * y);\n z = p[2] - (a[2] + t * z);\n return x * x + y * y + z * z <= r * r + eps;\n};\n\nint main(int argc, char** argv) {\n ios::sync_with_stdio(false);\n cin.tie(0);\n\n if(argc > 2) {\n in.open(argv[1]);\n cin.rdbuf(in.rdbuf());\n out.open(argv[2]);\n cout.rdbuf(out.rdbuf());\n }\n\n while(1) {\n ll n, k, s;\n cin >> n >> k >> s;\n if(n == 0) break;\n ll ans = 0;\n for(uint32_t i = 0; i < (1 << n); i++) {\n if(popcount(i) != k) continue;\n\n ll sum = 0;\n for(int j = 0; j < n; j++) {\n sum += ((i >> j) & 1) * (j + 1);\n }\n ans += sum == s;\n }\n cout << ans << endl;\n }\n return 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3456, "score_of_the_acc": -0.0862, "final_rank": 8 }, { "submission_id": "aoj_1335_8528341", "code_snippet": "#include <bits/stdc++.h>\n\nint solve() {\n int n, k, s;\n std::cin >> n >> k >> s;\n if (n == 0) return 1;\n\n int ans = 0;\n for (int state = 0; state < (1 << n); state++) {\n int count = 0;\n int sum = 0;\n for (int i = 0; i < n; i++) {\n if (state & (1 << i)) {\n count++;\n sum += i + 1;\n }\n }\n if (count == k && sum == s) ans++;\n }\n std::cout << ans << std::endl;\n return 0;\n}\n\nint main() {\n while (!solve());\n}", "accuracy": 1, "time_ms": 520, "memory_kb": 3120, "score_of_the_acc": -0.3521, "final_rank": 12 }, { "submission_id": "aoj_1335_8349539", "code_snippet": "#include <iostream>\n#include <vector>\n#include <algorithm>\n#include <numeric>\nusing namespace std;\n#define ALL(v) v.begin(),v.end()\n\nint main(void) {\n\twhile (true) {\n\t\tint N,K,S;\n\t\tcin >> N >> K >> S;\n\t\tif (N == 0 && K == 0 && S == 0) break;\n\n\t\tint ans = 0;\n\t\tfor (int i = 0; i < (1 << N); ++i) {\n\t\t\tvector<int> v;\n\t\t\tfor (int j = 0; j < N; ++j) {\n\t\t\t\tif (i & 1 << j) {\n\t\t\t\t\tv.push_back(j + 1);\n\t\t\t\t}\n\t\t\t}\n\t\t\tif (v.size() != K) continue;\n\t\t\tint sum = accumulate(ALL(v), 0);\n\t\t\tif (sum == S) ++ans;\n\t\t}\n\t\tcout << ans << endl;\n\t}\n}", "accuracy": 1, "time_ms": 1530, "memory_kb": 3336, "score_of_the_acc": -1.0613, "final_rank": 20 }, { "submission_id": "aoj_1335_7807924", "code_snippet": "#include <bits/stdc++.h>\n\nvoid solve(int n, int k, int s) {\n int count = 0;\n\n int set = (1 << k) - 1;\n while (set < (1 << n)) {\n int sum = 0;\n for (int i = 0; i < n; i++) {\n if (not (set & (1 << i))) continue;\n sum += i + 1;\n }\n\n if (sum == s) count++;\n\n int x = set & -set;\n int y = set + x;\n set = ((set & ~y) / x >> 1) | y;\n }\n\n std::cout << count << std::endl;\n}\n\nint main() {\n int n, k, s;\n \n while (std::cin >> n >> k >> s, n) {\n solve(n, k, s);\n }\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3132, "score_of_the_acc": -0.0191, "final_rank": 2 }, { "submission_id": "aoj_1335_7807796", "code_snippet": "#include <bits/stdc++.h>\n\nvoid solve(int n, int k, int s) {\n std::vector< int > ps(n, 1);\n for (int i = 0; i < n - k; i++) ps[i] = 0;\n\n int count = 0;\n do {\n int sum = 0;\n for (int i = 0; i < n; i++) {\n if (not ps[i]) continue;\n sum += i + 1;\n }\n\n if (sum == s) count++;\n } while (std::next_permutation(ps.begin(), ps.end()));\n\n std::cout << count << std::endl;\n}\n\nint main() {\n int n, k, s;\n \n while (std::cin >> n >> k >> s, n) {\n solve(n, k, s);\n }\n}", "accuracy": 1, "time_ms": 30, "memory_kb": 3328, "score_of_the_acc": -0.0728, "final_rank": 6 }, { "submission_id": "aoj_1335_7251396", "code_snippet": "#include <iostream>\n#include <vector>\n#include <algorithm>\nusing namespace std;\n\nint main(){\n while(true){\n int n, k, s; cin >> n >> k >> s;\n if(n == 0) break;\n int ans = 0;\n for(int bit = 0; bit < (1 << n); bit++){\n int cnt = 0, sum = 0;\n for(int i = 0; i < n; i++){\n if((bit >> i) & 1){\n cnt++;\n sum += i+1;\n }\n }\n if(cnt == k && sum == s) ans++;\n }\n cout << ans << '\\n';\n }\n return 0;\n}", "accuracy": 1, "time_ms": 680, "memory_kb": 3432, "score_of_the_acc": -0.522, "final_rank": 18 }, { "submission_id": "aoj_1335_7218976", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\n\nvoid solve(int n, int k, int s) {\n int ans = 0;\n\n for (int bits = 0; bits < (1 << n); bits++) {\n int cnt = 0;\n int sum = 0;\n\n for (int i = 0; i < n; i++) {\n if(bits & (1 << i)) {\n cnt++;\n sum += (i + 1);\n }\n }\n\n if(cnt == k && sum == s) ans++;\n }\n\n cout << ans << endl;\n}\n\nint main() {\n while(true) {\n int n, k, s;\n cin >> n >> k >> s;\n if(n == 0)break;\n solve(n,k,s);\n }\n}", "accuracy": 1, "time_ms": 580, "memory_kb": 3336, "score_of_the_acc": -0.4363, "final_rank": 16 }, { "submission_id": "aoj_1335_6392033", "code_snippet": "#include <bits/stdc++.h>\n#define rep(i, n) for (int i = 0; i < n; ++i)\ntypedef long long ll;\nusing namespace std;\n\nint ans;\nvoid dfs(int N, int K, int S, vector<int> p) {\n if (p.size() == N + 1) {\n if (accumulate(p.begin(), p.end(), 0) == S)\n ans++;\n\n return;\n }\n\n p.emplace_back(p.back());\n while (p.back() < K) {\n p.back()++;\n dfs(N, K, S, p);\n }\n}\n\nvoid solve(int N, int K, int S) {\n ans = 0;\n dfs(K, N, S, vector<int>(1, 0));\n cout << ans << endl;\n}\n\nint main() {\n cin.tie(0);\n ios_base::sync_with_stdio(false);\n\n int N, K, S;\n while (cin >> N >> K >> S, N && K && S)\n solve(N, K, S);\n\n return 0;\n}", "accuracy": 1, "time_ms": 100, "memory_kb": 3260, "score_of_the_acc": -0.1048, "final_rank": 11 }, { "submission_id": "aoj_1335_6212968", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\nusing ll = long long;\n#define rep(i, n) for (int i = 0; i < (n); i++)\n#define ALL(x) x.begin(), x.end()\n\nint main() {\n ios::sync_with_stdio(false);\n cin.tie(0);\n int MAX = 20;\n int N2 = 1 << MAX;\n vector<vector<int>> g(160);\n rep(b, N2) {\n int s = 0;\n rep(i, MAX) {\n if (b >> i & 1) s += i + 1;\n }\n if (s >= 160) continue;\n g[s].push_back(b);\n }\n int N, K, S;\n while (cin >> N >> K >> S, N) {\n int ans = 0;\n int b2 = 0;\n rep(i, MAX - N) b2 += (1 << (N + i));\n for (auto &b : g[S]) {\n if (b & b2) continue;\n if (__builtin_popcount(b) != K) continue;\n ans++;\n }\n cout << ans << '\\n';\n }\n return 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 7868, "score_of_the_acc": -1, "final_rank": 19 }, { "submission_id": "aoj_1335_6016318", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\n#define all(A) A.begin(),A.end()\nusing vll = vector<ll>;\n#define rep(i, n) for (long long i = 0; i < (long long)(n); i++)\n\nll an = 0;\nll N, K, S;\nvoid dfs(ll k, ll m, ll s) {\n\t//if (N * k < s)return;\n\t//if (m * k > s)return;\n\tif (k == 0 && s == 0) {\n\t\tan++;\n\t\treturn;\n\t}\n\tfor (ll i = m + 1; i <= min(N, s); i++) {\n\t\tdfs(k - 1, i, s - i);\n\t}\n\n}\n\n\nint main() {\n\twhile (1) {\n\t\tcin >> N >> K >> S;\n\t\tif (N == 0)return 0;\n\t\tan = 0;\n\t\tdfs(K, 0, S);\n\t\tcout << an << endl;\n\t}\n\n\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3044, "score_of_the_acc": -0.0008, "final_rank": 1 }, { "submission_id": "aoj_1335_5987916", "code_snippet": "#include <functional>\n#include <iostream>\n\nusing namespace std;\n\nint main() {\n std::cin.tie(0);\n std::ios_base::sync_with_stdio(false);\n\n while (true) {\n int n, k, s;\n cin >> n >> k >> s;\n\n if (n == 0 && k == 0 && s == 0) {\n break;\n }\n\n int ans = 0;\n\n function<void(int, int, int)> dfs = [&](int idx, int v, int sum) {\n if (v == n && sum < s) {\n return;\n }\n if (sum > s) {\n return;\n }\n\n if (idx == k - 1) {\n ans += sum == s;\n return;\n }\n\n for (int next_v = v + 1; next_v <= n; next_v++) {\n dfs(idx + 1, next_v, sum + next_v);\n }\n };\n\n for (int x = 1; x <= n; x++) {\n dfs(0, x, x);\n }\n\n cout << ans << \"\\n\";\n }\n\n return 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3456, "score_of_the_acc": -0.0862, "final_rank": 8 }, { "submission_id": "aoj_1335_5916959", "code_snippet": "#include<bits/stdc++.h>\n\nusing namespace std;\n\nint main() {\n while(1){\n int n, k, s;\n cin >> n >> k >> s;\n if(n == 0 && k == 0 && s == 0) break;\n int res = 0;\n for(int bits = 0; bits < 1<<(n); bits++){\n if( __builtin_popcount(bits) == k){\n int sum = 0;\n for(int i = 0; i < n; i++){\n if(bits & 1 << i){\n sum += (i+1);\n }\n }\n if(sum == s) res++;\n }\n }\n cout << res << endl;\n }\n}", "accuracy": 1, "time_ms": 40, "memory_kb": 3316, "score_of_the_acc": -0.0769, "final_rank": 7 }, { "submission_id": "aoj_1335_5813063", "code_snippet": "#include<bits/stdc++.h>\n#define fi first\n#define se second\n#define ll long long\n#define mp make_pair\n#define pb push_back\n#define ls x<<1\n#define rs x<<1|1\n#define lson x<<1,l,mid\n#define rson x<<1|1,mid+1,r\n#define pii pair<int,int>\n#define all(x) x.begin(),x.end()\n#define cl(x,y) memset(x,y,sizeof(x))\n#define nxtp(a,n) next_permutation(a+1,a+n+1)\n#define mem(x,y,n) memset(x,y,sizeof(int)*(n+5))\nconst int N=1e6+10;\nconst int mod=1e9+7;\nconst int inf=0x3f3f3f3f;\nconst double eps=1e-8;\nconst double pi=acos(-1);\nconst double INF=1e18;\nusing namespace std;\n\nint ans,n,k,s,a[N];\nvoid dfs(int x,int p)\n{\n\tint i;\n\tif(p==k)\n\t{\n\t\tint sum=0;\n\t\tfor(i=1;i<=k;i++)\n\t\t\tsum+=a[i];\n\t\tans+=(sum==s);\n\t\treturn ;\n\t}\n\tfor(i=x+1;i<=n;i++)\n\t{\n\t\ta[p+1]=i;\n\t\tdfs(i,p+1);\n\t}\n}\nint main()\n{\n#ifdef ChuTian\n\tclock_t stTime = clock();\n freopen(\"in.in\",\"r\",stdin);\n freopen(\"out.out\",\"w\",stdout);\n#endif\n\tios::sync_with_stdio(false);\n\tcin.tie(0);cout.tie(0);\n\n\twhile(cin>>n>>k>>s && n+k+s)\n\t{\n\t\tans=0;\n\t\tdfs(0,0);\n\t\tcout<<ans<<endl;\n\t}\n#ifdef ChuTian\n\tcerr << \"Time Used:\" << clock() - stTime << \"ms\" <<endl;\n#endif\n\treturn 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3460, "score_of_the_acc": -0.087, "final_rank": 10 }, { "submission_id": "aoj_1335_5638318", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\n#define REP(i,n) for(int i=0;i<(n);i++)\n#define ALL(v) v.begin(),v.end()\n\nint main(){\n int n,k,s;\n while(cin>>n>>k>>s,n){\n vector<int> v(n);\n REP(i,n)v[i]=i<k;\n sort(ALL(v));\n int ans=0;\n do{\n int sum=0;\n REP(i,n)if(v[i])sum+=i+1;\n ans+=sum==s;\n }while(next_permutation(ALL(v)));\n cout<<ans<<endl;\n }\n}", "accuracy": 1, "time_ms": 40, "memory_kb": 3112, "score_of_the_acc": -0.0346, "final_rank": 3 }, { "submission_id": "aoj_1335_5607581", "code_snippet": "#ifdef LOCAL\n#define _GLIBCXX_DEBUG\n#endif\n \n#include <bits/stdc++.h>\nusing namespace std;\n#if __has_include(<atcoder/all>)\n#include <atcoder/all>\nusing namespace atcoder;\n#endif\n \n#pragma region Macros\n// rep macro\n#define foa(v, a) for(auto &v : a)\n#define REPname(a, b, c, d, e, ...) e\n#define REP(...) REPname(__VA_ARGS__, REP3, REP2, REP1, REP0)(__VA_ARGS__)\n#define REP0(x) for(int i = 0; i < (x); ++i)\n#define REP1(i, x) for(int i = 0; i < (x); ++i)\n#define REP2(i, l, r) for(int i = (l); i < (r); ++i)\n#define REP3(i, l, r, c) for(int i = (l); i < (r); i += (c))\n#define REPSname(a, b, c, ...) c\n#define REPS(...) REPSname(__VA_ARGS__, REPS1, REPS0)(__VA_ARGS__)\n#define REPS0(x) for(int i = 1; i <= (x); ++i)\n#define REPS1(i, x) for(int i = 1; i <= (x); ++i)\n#define RREPname(a, b, c, d, e, ...) e\n#define RREP(...) RREPname(__VA_ARGS__, RREP3, RREP2, RREP1, RREP0)(__VA_ARGS__)\n#define RREP0(x) for(int i = (x)-1; i >= 0; --i)\n#define RREP1(i, x) for(int i = (x)-1; i >= 0; --i)\n#define RREP2(i, l, r) for(int i = (r)-1; i >= (l); --i)\n#define RREP3(i, l, r, c) for(int i = (r)-1; i >= (l); i -= (c))\n#define RREPSname(a, b, c, ...) c\n#define RREPS(...) RREPSname(__VA_ARGS__, RREPS1, RREPS0)(__VA_ARGS__)\n#define RREPS0(x) for(int i = (x); i >= 1; --i)\n#define RREPS1(i, x) for(int i = (x); i >= 1; --i)\n \n// name macro\n#define fi first\n#define se second\n#define pb push_back\n#define eb emplace_back\n#define SZ(x) ((int)(x).size())\n#define all(x) (x).begin(), (x).end()\n#define rall(x) (x).rbegin(), (x).rend()\n#define popcnt(x) __builtin_popcountll(x)\ntemplate <class T = int>\nusing V = std::vector<T>;\ntemplate <class T = int>\nusing VV = std::vector<std::vector<T>>;\ntemplate <class T = int>\nusing VVV = std::vector<std::vector<std::vector<T>>>;\ntemplate <class T>\nusing pqup = std::priority_queue<T, std::vector<T>, std::greater<T>>;\nusing ll = long long;\nusing ld = long double;\nusing int128 = __int128_t;\nusing pii = std::pair<int, int>;\nusing pll = std::pair<long long, long long>;\nusing Tp = tuple<ll,ll,ll>;\n \n// input macro\ntemplate <class T, class U>\nstd::istream &operator>>(std::istream &is, std::pair<T, U> &p) {\n\tis >> p.first >> p.second;\n\treturn is;\n}\ntemplate <class T>\nstd::istream &operator>>(std::istream &is, std::vector<T> &v) {\n\tfor(T &i : v) is >> i;\n\treturn is;\n}\nstd::istream &operator>>(std::istream &is, __int128_t &a) {\n\tstd::string s;\n\tis >> s;\n\t__int128_t ret = 0;\n\tfor(int i = 0; i < s.length(); i++)\n\t\tif('0' <= s[i] and s[i] <= '9')\n\t\t\tret = 10 * ret + s[i] - '0';\n\ta = ret * (s[0] == '-' ? -1 : 1);\n\treturn is;\n}\nnamespace scanner {\nvoid scan(int &a) { std::cin >> a; }\nvoid scan(long long &a) { std::cin >> a; }\nvoid scan(std::string &a) { std::cin >> a; }\nvoid scan(char &a) { std::cin >> a; }\nvoid scan(char a[]) { std::scanf(\"%s\", a); }\nvoid scan(double &a) { std::cin >> a; }\nvoid scan(long double &a) { std::cin >> a; }\ntemplate <class T, class U>\nvoid scan(std::pair<T, U> &p) { std::cin >> p; }\ntemplate <class T>\nvoid scan(std::vector<T> &a) { std::cin >> a; }\nvoid INPUT() {}\ntemplate <class Head, class... Tail>\nvoid INPUT(Head &head, Tail &... tail) {\n\tscan(head);\n\tINPUT(tail...);\n}\n} // namespace scanner\n#define VEC(type, name, size) \\\n\tstd::vector<type> name(size); \\\n\tscanner::INPUT(name)\n#define VVEC(type, name, h, w) \\\n\tstd::vector<std::vector<type>> name(h, std::vector<type>(w)); \\\n\tscanner::INPUT(name)\n#define INT(...) \\\n\tint __VA_ARGS__; \\\n\tscanner::INPUT(__VA_ARGS__)\n#define LL(...) \\\n\tlong long __VA_ARGS__; \\\n\tscanner::INPUT(__VA_ARGS__)\n#define STR(...) \\\n\tstd::string __VA_ARGS__; \\\n\tscanner::INPUT(__VA_ARGS__)\n#define CHAR(...) \\\n\tchar __VA_ARGS__; \\\n\tscanner::INPUT(__VA_ARGS__)\n#define DOUBLE(...) \\\n\tdouble __VA_ARGS__; \\\n\tscanner::INPUT(__VA_ARGS__)\n#define LD(...) \\\n\tlong double __VA_ARGS__; \\\n\tscanner::INPUT(__VA_ARGS__)\n \n// output-macro\ntemplate <class T, class U>\nstd::ostream &operator<<(std::ostream &os, const std::pair<T, U> &p) {\n\tos << p.first << \" \" << p.second;\n\treturn os;\n}\ntemplate <class T>\nstd::ostream &operator<<(std::ostream &os, const std::vector<T> &a) {\n\tfor(int i = 0; i < int(a.size()); ++i) {\n\t\tif(i) os << \" \";\n\t\tos << a[i];\n\t}\n\treturn os;\n}\nstd::ostream &operator<<(std::ostream &dest, __int128_t &value) {\n\tstd::ostream::sentry s(dest);\n\tif(s) {\n\t\t__uint128_t tmp = value < 0 ? -value : value;\n\t\tchar buffer[128];\n\t\tchar *d = std::end(buffer);\n\t\tdo {\n\t\t\t--d;\n\t\t\t*d = \"0123456789\"[tmp % 10];\n\t\t\ttmp /= 10;\n\t\t} while(tmp != 0);\n\t\tif(value < 0) {\n\t\t\t--d;\n\t\t\t*d = '-';\n\t\t}\n\t\tint len = std::end(buffer) - d;\n\t\tif(dest.rdbuf()->sputn(d, len) != len) {\n\t\t\tdest.setstate(std::ios_base::badbit);\n\t\t}\n\t}\n\treturn dest;\n}\ntemplate <class T>\nvoid print(const T a) { std::cout << a << '\\n'; }\ntemplate <class Head, class... Tail>\nvoid print(Head H, Tail... T) {\n\tstd::cout << H << ' ';\n\tprint(T...);\n}\ntemplate <class T>\nvoid printel(const T a) { std::cout << a << '\\n'; }\ntemplate <class T>\nvoid printel(const std::vector<T> &a) {\n\tfor(const auto &v : a)\n\t\tstd::cout << v << '\\n';\n}\ntemplate <class Head, class... Tail>\nvoid printel(Head H, Tail... T) {\n\tstd::cout << H << '\\n';\n\tprintel(T...);\n}\nvoid Yes(const bool b = true) { std::cout << (b ? \"Yes\\n\" : \"No\\n\"); }\nvoid No() { std::cout << \"No\\n\"; }\nvoid YES(const bool b = true) { std::cout << (b ? \"YES\\n\" : \"NO\\n\"); }\nvoid NO() { std::cout << \"No\\n\"; }\nvoid err(const bool b = true) {\n\tif(b) {\n\t\tstd::cout << \"-1\\n\", exit(0);\n\t}\n}\n \n//debug macro\nnamespace debugger {\ntemplate <class T>\nvoid view(const std::vector<T> &a) {\n\tstd::cerr << \"{ \";\n\tfor(const auto &v : a) {\n\t\tstd::cerr << v << \", \";\n\t}\n\tstd::cerr << \"\\b\\b }\";\n}\ntemplate <class T>\nvoid view(const std::vector<std::vector<T>> &a) {\n\tstd::cerr << \"{\\n\";\n\tfor(const auto &v : a) {\n\t\tstd::cerr << \"\\t\";\n\t\tview(v);\n\t\tstd::cerr << \"\\n\";\n\t}\n\tstd::cerr << \"}\";\n}\ntemplate <class T, class U>\nvoid view(const std::vector<std::pair<T, U>> &a) {\n\tstd::cerr << \"{\\n\";\n\tfor(const auto &p : a) std::cerr << \"\\t(\" << p.first << \", \" << p.second << \")\\n\";\n\tstd::cerr << \"}\";\n}\ntemplate <class T, class U>\nvoid view(const std::map<T, U> &m) {\n\tstd::cerr << \"{\\n\";\n\tfor(const auto &p : m) std::cerr << \"\\t[\" << p.first << \"] : \" << p.second << \"\\n\";\n\tstd::cerr << \"}\";\n}\ntemplate <class T, class U>\nvoid view(const std::pair<T, U> &p) { std::cerr << \"(\" << p.first << \", \" << p.second << \")\"; }\ntemplate <class T>\nvoid view(const std::set<T> &s) {\n\tstd::cerr << \"{ \";\n\tfor(auto &v : s) {\n\t\tview(v);\n\t\tstd::cerr << \", \";\n\t}\n\tstd::cerr << \"\\b\\b }\";\n}\n \ntemplate <class T>\nvoid view(const T &e) { std::cerr << e; }\n} // namespace debugger\n#ifdef LOCAL\nvoid debug_out() {}\ntemplate <typename Head, typename... Tail>\nvoid debug_out(Head H, Tail... T) {\n\tdebugger::view(H);\n\tstd::cerr << \", \";\n\tdebug_out(T...);\n}\n#define debug(...) \\\n\tdo { \\\n\t\tstd::cerr << __LINE__ << \" [\" << #__VA_ARGS__ << \"] : [\"; \\\n\t\tdebug_out(__VA_ARGS__); \\\n\t\tstd::cerr << \"\\b\\b]\\n\"; \\\n\t} while(false)\n#else\n#define debug(...) (void(0))\n#endif\n \n// vector macro\ntemplate <class T>\nint lb(const std::vector<T> &a, const T x) { return std::distance((a).begin(), std::lower_bound((a).begin(), (a).end(), (x))); }\ntemplate <class T>\nint ub(const std::vector<T> &a, const T x) { return std::distance((a).begin(), std::upper_bound((a).begin(), (a).end(), (x))); }\ntemplate <class T>\nvoid UNIQUE(std::vector<T> &a) {\n\tstd::sort(a.begin(), a.end());\n\ta.erase(std::unique(a.begin(), a.end()), a.end());\n}\ntemplate <class T>\nstd::vector<T> press(std::vector<T> &a) {\n\tauto res = a;\n\tUNIQUE(res);\n\tfor(auto &v : a)\n\t\tv = lb(res, v);\n\treturn res;\n}\n#define SORTname(a, b, c, ...) c\n#define SORT(...) SORTname(__VA_ARGS__, SORT1, SORT0, ...)(__VA_ARGS__)\n#define SORT0(a) std::sort((a).begin(), (a).end())\n#define SORT1(a, c) std::sort((a).begin(), (a).end(), [](const auto x, const auto y) { return x c y; })\ntemplate <class T>\nvoid ADD(std::vector<T> &a, const T x) {\n\tfor(auto &v : a) v += x;\n}\ntemplate <class T>\nvoid SUB(std::vector<T> &a, const T x = 1) {\n\tfor(auto &v : a) v -= x;\n}\ntemplate <class T>\nvoid MUL(std::vector<T> &a, const T x) {\n\tfor(auto &v : a) v *= x;\n}\ntemplate <class T>\nvoid DIV(std::vector<T> &a, const T x) {\n\tfor(auto &v : a) v /= x;\n}\n \n// math macro\ntemplate <class T, class U>\ninline bool chmin(T &a, const U &b) { return a > b ? a = b, true : false; }\ntemplate <class T, class U>\ninline bool chmax(T &a, const U &b) { return a < b ? a = b, true : false; }\ntemplate <class T>\nT divup(T x, T y) { return (x + y - 1) / y; }\ntemplate <class T>\nT POW(T a, long long n) {\n\tT ret = 1;\n\twhile(n) {\n\t\tif(n & 1) ret *= a;\n\t\ta *= a;\n\t\tn >>= 1;\n\t}\n\treturn ret;\n}\n\ntemplate<typename T>\nT ADD(T a, T b){\n\tT res;\n\treturn __builtin_add_overflow(a, b, &res) ? numeric_limits<T>::max() : res;\n}\n\ntemplate<typename T>\nT MUL(T a, T b){\n\tT res;\n\treturn __builtin_mul_overflow(a, b, &res) ? numeric_limits<T>::max() : res;\n}\n\n// modpow\nlong long POW(long long a, long long n, const int mod) {\n\tlong long ret = 1;\n\twhile(n) {\n\t\tif(n & 1) (ret *= a) %= mod;\n\t\t(a *= a) %= mod;\n\t\tn >>= 1;\n\t}\n\treturn ret;\n}\n \n// others\nstruct fast_io {\n\tfast_io() {\n\t\tios::sync_with_stdio(false);\n\t\tcin.tie(nullptr);\n\t\tcout << fixed << setprecision(15);\n\t}\n} fast_io_;\n\n#pragma endregion\n\nusing R = long double;\nconstexpr R EPS=1E-11;\n\n// r の(誤差付きの)符号に従って, -1, 0, 1 を返す.\nint sgn(const R& r){ return (r > EPS) - (r < -EPS); }\n// a, b の(誤差付きの)大小比較の結果に従って, -1, 0, 1 を返す.\nint sgn(const R& a, const R &b){ return sgn(a-b); }\n// a > 0 は sgn(a) > 0\n// a < b は sgn(a, b) < 0\n// a >= b は sgn(a, b) >= 0\n// のように書く.\n// return s * 10^n\n\n//https://atcoder.jp/contests/abc191/submissions/20028529\nlong long x10(const string& s, size_t n) {\n if (s.front() == '-') return -x10(s.substr(1), n);\n auto pos = s.find('.');\n if (pos == string::npos) return stoll(s + string(n, '0'));\n return stoll(s.substr(0, pos) + s.substr(pos + 1) + string(n + pos + 1 - s.size(), '0'));\n}\n \nlong long ceildiv(long long a, long long b) {\n if (b < 0) a = -a, b = -b;\n if (a >= 0) return (a + b - 1) / b;\n else return a / b;\n}\n \nlong long floordiv(long long a, long long b) {\n if (b < 0) a = -a, b = -b;\n if (a >= 0) return a / b;\n else return (a - b + 1) / b;\n}\n \nlong long floorsqrt(long long x) {\n assert(x >= 0);\n long long ok = 0;\n long long ng = 1;\n while (ng * ng <= x) ng <<= 1;\n while (ng - ok > 1) {\n long long mid = (ng + ok) >> 1;\n if (mid * mid <= x) ok = mid;\n else ng = mid;\n }\n return ok;\n}\n\ninline int topbit(unsigned long long x){\n\treturn x?63-__builtin_clzll(x):-1;\n}\n\ninline int popcount(unsigned long long x){\n\treturn __builtin_popcountll(x);\n}\n\ninline int parity(unsigned long long x){//popcount%2\n\treturn __builtin_parity(x);\n}\n\nconst int inf = 1e9;\nconst ll INF = 1e18;\n\nvoid main_() {\n ll n,k,s;\n cin >> n >> k >> s;\n VVV<ll> dp(11,VV<ll>(160,V<ll>(160)));\n \n while(n!=0){\n REP(i,11)REP(j,160)REP(K,160) dp[i][j][K] = 0;\n dp[0][0][0] = 1;\n REP(i,k){\n REP(j,160)REP(a,s+1){\n for(ll b = j+1;b <= n;b++){\n if(a + b > s)break;\n dp[i+1][b][a + b] += dp[i][j][a];\n }\n }\n }\n ll ans = 0;\n \n REP(i,160){\n ans += dp[k][i][s];\n }\n cout << ans << endl;\n cin >> n >> k >> s;\n }\n}\n \nint main() {\n\tint t = 1;\n\t//cin >> t;\n\twhile(t--) main_();\n\treturn 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 5544, "score_of_the_acc": -0.5186, "final_rank": 17 }, { "submission_id": "aoj_1335_5289469", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n#define int long long\n#define rep(i,n) for(int (i)=0;(i)<(n);(i)++)\n#define rrep(i,n) for(int (i)=((n)-1);(i)>=0;(i)--)\n#define itn int\n#define miele(v) min_element(v.begin(), v.end())\n#define maele(v) max_element(v.begin(), v.end())\n#define SUM(v) accumulate(v.begin(), v.end(), 0LL)\n#define lb(a, key) lower_bound(a.begin(),a.end(),key)\n#define ub(a, key) upper_bound(a.begin(),a.end(),key)\n#define COUNT(a, key) count(a.begin(), a.end(), key) \n#define BITCOUNT(x) __builtin_popcount(x)\n#define pb push_back\n#define all(x) (x).begin(),(x).end()\n#define F first\n#define S second\nusing P = pair <int,int>;\nusing WeightedGraph = vector<vector <P>>;\nusing UnWeightedGraph = vector<vector<int>>;\nusing Real = long double;\nusing Point = complex<Real>; //Point and Vector2d is the same!\nusing Vector2d = complex<Real>;\nconst long long INF = 1LL << 60;\nconst int MOD = 1000000007;\nconst double EPS = 1e-15;\nconst double PI=3.14159265358979323846;\ntemplate <typename T> \nint getIndexOfLowerBound(vector <T> &v, T x){\n return lower_bound(v.begin(),v.end(),x)-v.begin();\n}\ntemplate <typename T> \nint getIndexOfUpperBound(vector <T> &v, T x){\n return upper_bound(v.begin(),v.end(),x)-v.begin();\n}\ntemplate<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; }\ntemplate<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; }\n#define DUMPOUT cerr\n#define repi(itr, ds) for (auto itr = ds.begin(); itr != ds.end(); itr++)\nistream &operator>>(istream &is, Point &p) {\n Real a, b; is >> a >> b; p = Point(a, b); return is;\n}\ntemplate <typename T, typename U>\nistream &operator>>(istream &is, pair<T,U> &p_var) {\n is >> p_var.first >> p_var.second;\n return is;\n}\ntemplate <typename T>\nistream &operator>>(istream &is, vector<T> &vec) {\n for (T &x : vec) is >> x;\n return is;\n}\ntemplate <typename T, typename U>\nostream &operator<<(ostream &os, pair<T, U> &pair_var) {\n DUMPOUT<<'{';\n os << pair_var.first;\n DUMPOUT<<',';\n os << \" \"<< pair_var.second;\n DUMPOUT<<'}';\n return os;\n}\ntemplate <typename T>\nostream &operator<<(ostream &os, vector<T> &vec) {\n DUMPOUT<<'[';\n for (int i = 0; i < vec.size(); i++) \n os << vec[i] << (i + 1 == vec.size() ? \"\" : \" \");\n DUMPOUT<<']';\n return os;\n}\ntemplate <typename T>\nostream &operator<<(ostream &os, vector<vector<T>> &df) {\n for (auto& vec : df) os<<vec;\n return os;\n}\ntemplate <typename T, typename U>\nostream &operator<<(ostream &os, map<T, U> &map_var) {\n DUMPOUT << \"{\";\n repi(itr, map_var) {\n os << *itr;\n itr++;\n if (itr != map_var.end()) DUMPOUT << \", \";\n itr--;\n }\n DUMPOUT << \"}\";\n return os;\n}\ntemplate <typename T>\nostream &operator<<(ostream &os, set<T> &set_var) {\n DUMPOUT << \"{\";\n repi(itr, set_var) {\n os << *itr;\n itr++;\n if (itr != set_var.end()) DUMPOUT << \", \";\n itr--;\n }\n DUMPOUT << \"}\";\n return os;\n}\nvoid print() {cout << endl;}\ntemplate <class Head, class... Tail>\nvoid print(Head&& head, Tail&&... tail) {\n cout << head;\n if (sizeof...(tail) != 0) cout << \" \";\n print(forward<Tail>(tail)...);\n}\nvoid dump_func() {DUMPOUT << '#'<<endl;}\ntemplate <typename Head, typename... Tail>\nvoid dump_func(Head &&head, Tail &&... tail) {\n DUMPOUT << head;\n if (sizeof...(Tail) > 0) DUMPOUT << \", \";\n dump_func(std::move(tail)...);\n}\n#ifdef DEBUG_\n#define DEB\n#define dump(...) \\\n DUMPOUT << \" \" << string(#__VA_ARGS__) << \": \" \\\n << \"[\" << to_string(__LINE__) << \":\" << __FUNCTION__ << \"]\" \\\n << endl \\\n << \" \", \\\n dump_func(__VA_ARGS__)\n#else\n#define DEB if (false)\n#define dump(...)\n#endif\n\nsigned main(void) { cin.tie(0); ios::sync_with_stdio(false);\n int t = 0;\n while(1) {\n int n, k, s; cin>>n>>k>>s;\n if(n == 0) exit(0);\n \n int ans = 0;\n int bit;\n for(bit=0;bit<(1<<n);bit++){\n int cnt = 0;\n int sum = 0;\n for(int i=0;i<n;i++){\n if(bit&(1<<i)){\n cnt++;\n sum += i+1;\n }\n }\n if(cnt == k && sum == s) {\n ans++;\n }\n }\n \n print(ans);\n }\n}", "accuracy": 1, "time_ms": 560, "memory_kb": 3344, "score_of_the_acc": -0.4248, "final_rank": 15 }, { "submission_id": "aoj_1335_5283700", "code_snippet": "#include <bits/stdc++.h>\n#define rep(i,n) for(int i = 0; i < (n); i++)\nusing namespace std;\ntypedef long long ll;\n\nint main(){\n cin.tie(0);\n ios::sync_with_stdio(0);\n\n while(1){\n ll n,k,s; cin >> n >> k >> s;\n if(n == 0) break;\n int ans = 0;\n rep(i,1<<n){\n if(__builtin_popcount(i) != k) continue;\n ll cur = 0;\n rep(j,n) if(i & (1 << j)) cur += j + 1;\n if(cur == s) ans++;\n }\n cout << ans << endl;\n }\n}", "accuracy": 1, "time_ms": 70, "memory_kb": 3056, "score_of_the_acc": -0.0428, "final_rank": 4 }, { "submission_id": "aoj_1335_4965186", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\nint main()\n{\n int n,k,s;\n while(cin>>n>>k>>s,n){\n int ans=0;\n for(int i=0;i<(1<<n);++i){\n int m=0,c=0;\n for(int j=0;j<n;++j){\n if(i&(1<<j)){\n m+=j+1;\n c++;\n }\n }\n if(c==k && m==s)ans++;\n }\n cout<<ans<<endl;\n }\n}", "accuracy": 1, "time_ms": 560, "memory_kb": 3116, "score_of_the_acc": -0.3776, "final_rank": 14 }, { "submission_id": "aoj_1335_4929431", "code_snippet": "#include<bits/stdc++.h>\n#define rep(i,a,...) for(int i = (a)*(strlen(#__VA_ARGS__)!=0);i<(int)(strlen(#__VA_ARGS__)?__VA_ARGS__:(a));++i)\n#define per(i,a,...) for(int i = (strlen(#__VA_ARGS__)?__VA_ARGS__:(a))-1;i>=(int)(strlen(#__VA_ARGS__)?(a):0);--i)\n#define foreach(i, n) for(auto &i:(n))\n#define all(x) (x).begin(), (x).end()\n#define bit(x) (1ll << (x))\n#define lambda(RES_TYPE, ...) (function<RES_TYPE(__VA_ARGS__)>)[&](__VA_ARGS__) -> RES_TYPE\n#define method(FUNC_NAME, RES_TYPE, ...) function<RES_TYPE(__VA_ARGS__)> FUNC_NAME = lambda(RES_TYPE, __VA_ARGS__)\nusing namespace std;\nusing ll = long long;\nusing pii = pair<int,int>;\nusing pll = pair<ll,ll>;\n//const ll MOD = (ll)1e9+7;\nconst ll MOD = 998244353;\nconst int INF = (ll)1e9+7;\nconst ll INFLL = (ll)1e18;\ntemplate<class t>\nusing vvector = vector<vector<t>>;\ntemplate<class t>\nusing vvvector = vector<vector<vector<t>>>;\ntemplate<class t>\nusing priority_queuer = priority_queue<t, vector<t>, greater<t>>;\ntemplate<class t, class u> bool chmax(t &a, u b){if(a<b){a=b;return true;}return false;}\ntemplate<class t, class u> bool chmin(t &a, u b){if(a>b){a=b;return true;}return false;}\n#ifdef DEBUG\n#define debug(x) cout<<\"LINE \"<<__LINE__<<\": \"<<#x<<\" = \"<<x<<endl;\n#else\n#define debug(x) (void)0\n#endif\n\nnamespace templates{\n ll modpow(ll x, ll b){\n ll res = 1;\n while(b){\n if(b&1)res = res * x % MOD;\n x = x * x % MOD;\n b>>=1;\n }\n return res;\n }\n\n ll modinv(ll x){\n return modpow(x, MOD-2);\n }\n\n bool was_output = false;\n template<class t>\n void output(t a){\n if(was_output)cout << \" \";\n cout << a;\n was_output = true;\n }\n void outendl(){\n was_output = false;\n cout << endl;\n }\n ll in(){\n ll res;\n scanf(\"%lld\", &res);\n return res;\n }\n\n template<class t>\n istream& operator>>(istream&is, vector<t>&x){\n for(auto &i:x)is >> i;\n return is;\n }\n\n template<class t, class u>\n istream& operator>>(istream&is, pair<t, u>&x){\n is >> x.first >> x.second;\n return is;\n }\n\n template<class t>\n t in(){\n t res; cin >> res; return res;\n }\n\n template<class t>\n void out(t x){\n cout << x;\n }\n\n template<class t>\n vector<t> sorted(vector<t> line,function<bool(t,t)> comp=[](t a,t b){return a<b;}){\n sort(line.begin(),line.end(),comp);\n return line;\n }\n\n template<class t>\n vector<t> reversed(vector<t> line){\n reverse(line.begin(),line.end());\n return line;\n }\n}\n\nusing namespace templates;\n\nint func(int n,int k,int s){\n method(func,int,int place,int sum,int used){\n if(used > k)return 0;\n if(sum > s)return 0;\n if(place == n+1){\n return sum == s and k == used;\n }\n int res = 0;\n res += func(place+1,sum+place,used+1);\n res += func(place+1,sum,used);\n return res;\n };\n return func(1,0,0);\n}\n\nint main(){\n while(true){\n int n = in();\n int k = in();\n int s = in();\n if(n==0)break;\n cout << func(n,k,s) << endl;\n }\n\n return 0;\n}", "accuracy": 1, "time_ms": 20, "memory_kb": 3240, "score_of_the_acc": -0.048, "final_rank": 5 }, { "submission_id": "aoj_1335_4927509", "code_snippet": "#include<bits/stdc++.h>\n\nusing ll = long long;\n#define var auto\n\nusing namespace std;\n\ntemplate<typename T1,typename T2> inline void chmin(T1 &a,T2 b){ if (a > b) a = b; }\ntemplate<typename T1,typename T2> inline void chmax(T1 &a,T2 b){ if (a < b) a = b; }\n\nint main(){\n int n, k, s;\n cin >> n >> k >> s;\n if (n == 0) return 0;\n int res = 0;\n for (int i = 0; i < (1 << n); i++){\n int sum = 0;\n int count = 0;\n for (int j = 0; j < n; j++){\n if (!(i >> j & 1)) continue;\n sum += (j + 1);\n count++;\n }\n if (sum == s && count == k) res++;\n }\n cout << res << endl;\n main();\n}", "accuracy": 1, "time_ms": 570, "memory_kb": 3040, "score_of_the_acc": -0.3684, "final_rank": 13 } ]
aoj_1329_cpp
Problem E: Sliding Block Puzzle In sliding block puzzles, we repeatedly slide pieces (blocks) to open spaces within a frame to establish a goal placement of pieces. A puzzle creator has designed a new puzzle by combining the ideas of sliding block puzzles and mazes. The puzzle is played in a rectangular frame segmented into unit squares. Some squares are pre-occupied by obstacles. There are a number of pieces placed in the frame, one 2 × 2 king piece and some number of 1 × 1 pawn pieces. Exactly two 1 × 1 squares are left open. If a pawn piece is adjacent to an open square, we can slide the piece there. If a whole edge of the king piece is adjacent to two open squares, we can slide the king piece. We cannot move the obstacles. Starting from a given initial placement, the objective of the puzzle is to move the king piece to the upper-left corner of the frame. The following figure illustrates the initial placement of the fourth dataset of the sample input. Figure E.1: The fourth dataset of the sample input. Your task is to write a program that computes the minimum number of moves to solve the puzzle from a given placement of pieces. Here, one move means sliding either king or pawn piece to an adjacent position. Input The input is a sequence of datasets. The first line of a dataset consists of two integers H and W separated by a space, where H and W are the height and the width of the frame. The following H lines, each consisting of W characters, denote the initial placement of pieces. In those H lines, ' X ', ' o ', ' * ', and ' . ' denote a part of the king piece, a pawn piece, an obstacle, and an open square, respectively. There are no other characters in those H lines. You may assume that 3 ≤ H ≤ 50 and 3 ≤ W ≤ 50. A line containing two zeros separated by a space indicates the end of the input. Output For each dataset, output a line containing the minimum number of moves required to move the king piece to the upper-left corner. If there is no way to do so, output -1 . Sample Input 3 3 oo. oXX .XX 3 3 XXo XX. o.o 3 5 .o*XX oooXX oooo. 7 12 oooooooooooo ooooo*****oo oooooo****oo o**ooo***ooo o***ooooo..o o**ooooooXXo ooooo****XXo 5 30 oooooooooooooooooooooooooooooo oooooooooooooooooooooooooooooo o***************************oo XX.ooooooooooooooooooooooooooo XX.ooooooooooooooooooooooooooo 0 0 Output for the Sample Input 11 0 -1 382 6807
[ { "submission_id": "aoj_1329_10853885", "code_snippet": "#include<cstdio>\n#include<cstring>\n#include<queue>\n#include<iostream>\nusing namespace std;\nconst int inf=0x3f3f3f3f;\nconst int maxn=60;\nint n,m;\nchar mp[maxn][maxn];\nconst int cx[]={-1,-1,0,1,2,2,0,1};\nconst int cy[]={0,1,2,2,0,1,-1,-1};\nstruct ty\n{\n int x,y,d,c;\n ty(int x=0,int y=0,int d=0,int c=0):x(x),y(y),d(d),c(c){}\n inline bool in_range()\n {\n return x>=0&&x<n&&y>=0&&y<m;\n }\n ty p1()\n {\n return ty(x+cx[d<<1],y+cy[d<<1],0,0);\n }\n ty p2()\n {\n return ty(x+cx[(d<<1)^1],y+cy[(d<<1)^1],0,0);\n }\n void putit()\n {\n printf(\"state:%d %d %d %d\\n\",x,y,d,c);\n }\n}st,p1,p2;\nconst int dx[]={0,0,-1,1};\nconst int dy[]={1,-1,0,0};\nint p2p(ty st,ty en,ty king)\n{\n if (!st.in_range()) return inf;\n if (!en.in_range()) return inf;\n queue<ty> qq;\n bool vis[maxn][maxn];\n memset(vis,0,sizeof(vis));\n qq.push(st);\n st.c=0;\n vis[st.x][st.y]=1;\n while (!qq.empty())\n {\n ty now=qq.front();\n qq.pop();\n if (now.x==en.x&&now.y==en.y) return now.c;\n for (int i=0;i<4;i++)\n {\n ty exp(now.x+dx[i],now.y+dy[i],0,now.c+1);\n if (exp.in_range()&&mp[exp.x][exp.y]!='*'&&(exp.x<king.x||exp.x>king.x+1||exp.y<king.y||exp.y>king.y+1)&&!vis[exp.x][exp.y])\n {\n vis[exp.x][exp.y]=1;\n qq.push(exp);\n }\n }\n }\n return inf;\n}\nqueue<ty> q;\nint cost[maxn][maxn][4];\nvoid BFS()\n{\n memset(cost,-1,sizeof(cost));\n if (st.x==0&&st.y==0)\n {\n cost[0][0][0]=0;\n return;\n }\n for (int i=0;i<4;i++)\n {\n st.d=i;\n int c1=p2p(p1,st.p1(),st);\n int c2=p2p(p2,st.p2(),st);\n int c3=p2p(p2,st.p1(),st);\n int c4=p2p(p1,st.p2(),st);\n st.c=min(c1+c2,c3+c4);\n if (st.c<inf)\n {\n q.push(st);\n cost[st.x][st.y][st.d]=st.c;\n }\n }\n while (!q.empty())\n {\n ty now=q.front();\n q.pop();\n if (now.c!=cost[now.x][now.y][now.d]) continue;\n for (int i=0;i<4;i++)\n {\n ty exp=now;\n exp.d=i;\n int c1=p2p(now.p1(),exp.p1(),now);\n int c2=p2p(now.p2(),exp.p2(),now);\n int c3=p2p(now.p1(),exp.p2(),now);\n int c4=p2p(now.p2(),exp.p1(),now);\n exp.c=now.c+min(c1+c2,c3+c4);\n int &c=cost[exp.x][exp.y][exp.d];\n if (exp.c<inf&&(c==-1||exp.c<c))\n {\n c=exp.c;\n q.push(exp);\n }\n }\n ty exp;\n switch (now.d)\n {\n case 0:exp=ty(now.x-1,now.y,2,now.c+1);break;\n case 1:exp=ty(now.x,now.y+1,3,now.c+1);break;\n case 2:exp=ty(now.x+1,now.y,0,now.c+1);break;\n case 3:exp=ty(now.x,now.y-1,1,now.c+1);break;\n }\n if (exp.in_range())\n {\n int &c=cost[exp.x][exp.y][exp.d];\n if (c==-1||exp.c<c)\n {\n c=exp.c;\n q.push(exp);\n }\n }\n }\n}\nint main()\n{\n while (~scanf(\"%d%d\",&n,&m)&&(n||m))\n {\n for (int i=0;i<n;i++) scanf(\"%s\",mp[i]);\n st.x=p1.x=-1;\n for (int i=0;i<n;i++)\n for (int j=0;j<m;j++)\n if (mp[i][j]=='X'&&st.x==-1)\n {\n st.x=i;\n st.y=j;\n st.c=0;\n }\n else if (mp[i][j]=='.')\n {\n if (p1.x==-1) p1.x=i,p1.y=j;\n else p2.x=i,p2.y=j;\n }\n BFS();\n int ans=-1;\n for (int i=0;i<4;i++)\n if (ans==-1||(cost[0][0][i]!=-1&&cost[0][0][i]<ans))\n ans=cost[0][0][i];\n printf(\"%d\\n\",ans);\n }\n return 0;\n}", "accuracy": 1, "time_ms": 750, "memory_kb": 3620, "score_of_the_acc": -0.7911, "final_rank": 8 }, { "submission_id": "aoj_1329_9791572", "code_snippet": "#include <bits/stdc++.h>\n\nusing namespace std;\n\n#define rep(i, a, b) for (int i = (int)(a); i < (int)(b); i++)\n#define rrep(i, a, b) for (int i = (int)(b)-1; i >= (int)(a); i--)\n#define ALL(v) (v).begin(), (v).end()\n#define UNIQUE(v) sort(ALL(v)), (v).erase(unique(ALL(v)), (v).end())\n#define SZ(v) (int)v.size()\n#define MIN(v) *min_element(ALL(v))\n#define MAX(v) *max_element(ALL(v))\n#define LB(v, x) int(lower_bound(ALL(v), (x)) - (v).begin())\n#define UB(v, x) int(upper_bound(ALL(v), (x)) - (v).begin())\n\nusing uint = unsigned int;\nusing ll = long long int;\nusing ull = unsigned long long;\nusing i128 = __int128_t;\nusing u128 = __uint128_t;\nconst int inf = 0x3fffffff;\nconst ll INF = 0x1fffffffffffffff;\n\ntemplate <typename T> inline bool chmax(T &a, T b) {\n if (a < b) {\n a = b;\n return 1;\n }\n return 0;\n}\ntemplate <typename T> inline bool chmin(T &a, T b) {\n if (a > b) {\n a = b;\n return 1;\n }\n return 0;\n}\ntemplate <typename T, typename U> T ceil(T x, U y) {\n assert(y != 0);\n if (y < 0)\n x = -x, y = -y;\n return (x > 0 ? (x + y - 1) / y : x / y);\n}\ntemplate <typename T, typename U> T floor(T x, U y) {\n assert(y != 0);\n if (y < 0)\n x = -x, y = -y;\n return (x > 0 ? x / y : (x - y + 1) / y);\n}\ntemplate <typename T> int popcnt(T x) {\n return __builtin_popcountll(x);\n}\ntemplate <typename T> int topbit(T x) {\n return (x == 0 ? -1 : 63 - __builtin_clzll(x));\n}\ntemplate <typename T> int lowbit(T x) {\n return (x == 0 ? -1 : __builtin_ctzll(x));\n}\n\ntemplate <class T, class U>\nostream &operator<<(ostream &os, const pair<T, U> &p) {\n os << \"P(\" << p.first << \", \" << p.second << \")\";\n return os;\n}\ntemplate <typename T> ostream &operator<<(ostream &os, const vector<T> &vec) {\n os << \"{\";\n for (int i = 0; i < vec.size(); i++) {\n os << vec[i] << (i + 1 == vec.size() ? \"\" : \", \");\n }\n os << \"}\";\n return os;\n}\ntemplate <typename T, typename U>\nostream &operator<<(ostream &os, const map<T, U> &map_var) {\n os << \"{\";\n for (auto itr = map_var.begin(); itr != map_var.end(); itr++) {\n os << \"(\" << itr->first << \", \" << itr->second << \")\";\n itr++;\n if (itr != map_var.end())\n os << \", \";\n itr--;\n }\n os << \"}\";\n return os;\n}\ntemplate <typename T> ostream &operator<<(ostream &os, const set<T> &set_var) {\n os << \"{\";\n for (auto itr = set_var.begin(); itr != set_var.end(); itr++) {\n os << *itr;\n ++itr;\n if (itr != set_var.end())\n os << \", \";\n itr--;\n }\n os << \"}\";\n return os;\n}\n#ifdef LOCAL\n#define show(...) _show(0, #__VA_ARGS__, __VA_ARGS__)\n#else\n#define show(...) true\n#endif\ntemplate <typename T> void _show(int i, T name) {\n cerr << '\\n';\n}\ntemplate <typename T1, typename T2, typename... T3>\nvoid _show(int i, const T1 &a, const T2 &b, const T3 &...c) {\n for (; a[i] != ',' && a[i] != '\\0'; i++)\n cerr << a[i];\n cerr << \":\" << b << \" \";\n _show(i + 1, a, c...);\n}\n\n/**\n * @brief template\n */\n\nint dx[4] = {1,0,-1,0}, dy[4] = {0,1,0,-1};\nint nx[4][2] = {{2,2},{0,1},{-1,-1},{0,1}}, ny[4][2] = {{0,1},{2,2},{0,1},{-1,-1}};\n\nint main() {\nwhile(1) {\n int N, M;\n cin >> N >> M;\n if (N == 0) return 0;\n int SX = inf, SY = inf;\n vector<pair<int,int>> Open;\n vector<vector<bool>> B(N,vector<bool>(M));\n rep(i,0,N) {\n rep(j,0,M) {\n char C;\n cin >> C;\n if (C == '*') B[i][j] = false;\n else B[i][j] = true;\n if (C == 'X') chmin(SX,i), chmin(SY,j);\n if (C == '.') Open.push_back({i,j});\n }\n }\n if (SX == 0 && SY == 0) {\n cout << 0 << endl;\n continue;\n }\n auto canput = [&](int i, int j) -> bool {\n if (i < 0 || i+1 >= N || j < 0 || j+1 >= M) return false;\n return B[i][j] && B[i][j+1] && B[i+1][j] && B[i+1][j+1];\n };\n auto canmove = [&](int i, int j, int k) -> bool {\n rep(l,0,2) {\n int ni = i + nx[k][l], nj = j + ny[k][l];\n if (ni < 0 || N <= ni || nj < 0 || M <= nj) return false;\n if (!B[ni][nj]) false;\n }\n return true;\n };\n auto MakeDist = [&](int i, int j) -> vector<vector<int>> {\n vector<vector<int>> DP(N,vector<int>(M,inf));\n if (!B[i][j]) return DP;\n DP[i][j] = 0;\n queue<pair<int,int>> Q;\n Q.push({i,j});\n while(!Q.empty()) {\n auto [CurX, CurY] = Q.front();\n Q.pop();\n rep(k,0,4) {\n int NX = CurX + dx[k], NY = CurY + dy[k];\n if (NX < 0 || N <= NX || NY < 0 || M <= NY) continue;\n if (!B[NX][NY]) continue;\n if (chmin(DP[NX][NY],DP[CurX][CurY]+1)) {\n Q.push({NX,NY});\n }\n }\n }\n return DP;\n };\n vector Dist(N-1, vector(M-1, vector(4, vector<int>(4,inf))));\n vector ANS(N-1, vector(M-1, vector(4, inf)));\n rep(i,0,N-1) {\n rep(j,0,M-1) {\n if (!canput(i,j)) continue;\n rep(k,0,2) {\n rep(l,0,2) {\n B[i+k][j+l] = false;\n }\n }\n rep(k,0,4) {\n if (!canmove(i,j,k)) continue;\n pair<int,int> P0 = {i+nx[k][0],j+ny[k][0]}, P1 = {i+nx[k][1],j+ny[k][1]};\n auto DP0 = MakeDist(P0.first,P0.second), DP1 = MakeDist(P1.first,P1.second);\n if (i == SX && j == SY) {\n chmin(ANS[i][j][k], DP0[Open[0].first][Open[0].second] + DP1[Open[1].first][Open[1].second]);\n chmin(ANS[i][j][k], DP0[Open[1].first][Open[1].second] + DP1[Open[0].first][Open[0].second]);\n }\n rep(l,0,4) {\n if (!canmove(i,j,l)) continue;\n if (k == l) {\n Dist[i][j][k][l] = 0;\n continue;\n }\n pair<int,int> Q0 = {i+nx[l][0],j+ny[l][0]}, Q1 = {i+nx[l][1],j+ny[l][1]};\n chmin(Dist[i][j][k][l],DP0[Q0.first][Q0.second]+DP1[Q1.first][Q1.second]);\n chmin(Dist[i][j][k][l],DP0[Q1.first][Q1.second]+DP1[Q0.first][Q0.second]);\n }\n }\n rep(k,0,2) {\n rep(l,0,2) {\n B[i+k][j+l] = true;\n }\n }\n }\n }\n priority_queue<pair<int,tuple<int,int,int>>,vector<pair<int,tuple<int,int,int>>>,greater<pair<int,tuple<int,int,int>>>> PQ;\n rep(i,0,4) {\n if (ANS[SX][SY][i] < inf) {\n PQ.push({ANS[SX][SY][i],{SX,SY,i}});\n }\n }\n while(!PQ.empty()) {\n auto [D,TUPLE] = PQ.top();\n PQ.pop();\n auto [X,Y,Dir] = TUPLE;\n if (D > ANS[X][Y][Dir]) continue;\n if (chmin(ANS[X+dx[Dir]][Y+dy[Dir]][Dir^2], D+1)) {\n PQ.push({D+1,{X+dx[Dir],Y+dy[Dir],Dir^2}});\n }\n rep(i,0,4) {\n if (chmin(ANS[X][Y][i],D+Dist[X][Y][Dir][i])) {\n PQ.push({ANS[X][Y][i],{X,Y,i}});\n }\n }\n }\n int ANS2 = inf;\n rep(i,0,4) chmin(ANS2, ANS[0][0][i]);\n if (ANS2 == inf) cout << -1 << endl;\n else cout << ANS2 << endl;\n}\n}", "accuracy": 1, "time_ms": 740, "memory_kb": 3736, "score_of_the_acc": -1, "final_rank": 13 }, { "submission_id": "aoj_1329_8862217", "code_snippet": "#include <iostream>\n#include <queue>\n#include <vector>\n#include <algorithm>\n#include <string>\n\nusing namespace std;\n\nconst int dx[4] = {-1, 0, 1, 0};\nconst int dy[4] = {0, 1, 0, -1};\nconst int dx2[4] = {-1, 0, 2, 1};\nconst int dy2[4] = {0, 2, 1, -1};\n\nconst int MAX_FIELD_SIZE = 60;\nconst int MAX_DIRECTIONS = 4;\nconst int INFINITE_DISTANCE = 999999;\n\nstruct aa {\n int x;\n int y;\n int turn;\n};\n\nstruct bb {\n int kx;\n int ky;\n int opway;\n int turn;\n};\n\nclass Compare {\npublic:\n bool operator()(const bb &l, const bb &r) { return l.turn > r.turn; }\n};\n\nint getdis(const vector<vector<int>> &field, const int fx, const int fy,\n const int gx, const int gy) {\n int ans = INFINITE_DISTANCE;\n queue<aa> que;\n vector<vector<int>> memo(field.size(), vector<int>(field[0].size(), INFINITE_DISTANCE));\n que.push(aa{fx, fy, 0});\n while (!que.empty()) {\n aa atop(que.front());\n que.pop();\n if (atop.x == gx && atop.y == gy) {\n ans = atop.turn;\n break;\n }\n for (int i = 0; i < MAX_DIRECTIONS; ++i) {\n const int nextx = atop.x + dx[i];\n const int nexty = atop.y + dy[i];\n if (!field[nexty][nextx]) {\n if (atop.turn + 1 < memo[nexty][nextx]) {\n memo[nexty][nextx] = atop.turn + 1;\n que.push(aa{nextx, nexty, atop.turn + 1});\n }\n }\n }\n }\n return ans;\n}\n\nint main() {\n int memo2[MAX_FIELD_SIZE][MAX_FIELD_SIZE][MAX_DIRECTIONS];\n while (1) {\n for (int i = 0; i < MAX_FIELD_SIZE; ++i) {\n for (int j = 0; j < MAX_FIELD_SIZE; ++j) {\n for (int k = 0; k < MAX_DIRECTIONS; ++k) {\n memo2[i][j][k] = INFINITE_DISTANCE;\n }\n }\n }\n int H, W;\n cin >> H >> W;\n if (!H)\n break;\n vector<vector<int>> field(H + 2, vector<int>(W + 2, true));\n priority_queue<bb, vector<bb>, Compare> que;\n {\n int kx = 99, ky = 99;\n int ox[2];\n int oy[2];\n int num = 0;\n for (int i = 0; i < H; ++i) {\n string st;\n cin >> st;\n for (int j = 0; j < W; ++j) {\n if (st[j] == 'X') {\n kx = min(kx, j + 1);\n ky = min(ky, i + 1);\n field[i + 1][j + 1] = false;\n } else if (st[j] == 'o') {\n field[i + 1][j + 1] = false;\n } else if (st[j] == '.') {\n field[i + 1][j + 1] = false;\n ox[num] = j + 1;\n oy[num] = i + 1;\n num++;\n }\n }\n }\n if (kx == 1 && ky == 1) {\n cout << 0 << endl;\n continue;\n }\n for (int way = 0; way < MAX_DIRECTIONS; ++way) {\n int amin = INFINITE_DISTANCE;\n bool ok = true;\n for (int op = 0; op < 2; ++op) {\n int atime = 0;\n for (int lu = 0; lu < 2; ++lu) {\n const int fx = ox[op ^ lu];\n const int fy = oy[op ^ lu];\n int gx = kx + dx2[way];\n int gy = ky + dy2[way];\n if (lu == 1) {\n gx += dx[(way + 1) % MAX_DIRECTIONS];\n gy += dy[(way + 1) % MAX_DIRECTIONS];\n }\n if (field[gy][gx])\n ok = false;\n for (int ax = 0; ax < 2; ++ax) {\n for (int ay = 0; ay < 2; ++ay) {\n field[ky + ay][kx + ax] = true;\n }\n }\n atime += getdis(field, fx, fy, gx, gy);\n for (int ax = 0; ax < 2; ++ax) {\n for (int ay = 0; ay < 2; ++ay) {\n field[ky + ay][kx + ax] = false;\n }\n }\n }\n amin = min(amin, atime);\n }\n if (!ok)\n continue;\n else {\n memo2[kx][ky][way] = amin;\n que.push(bb{kx, ky, way, amin});\n }\n }\n }\n int ans = -1;\n while (!que.empty()) {\n bb atop(que.top());\n que.pop();\n const int fkx = atop.kx;\n const int fky = atop.ky;\n const int fway = atop.opway;\n const int fturn = atop.turn;\n if (fkx == 1 && fky == 1) {\n ans = fturn;\n break;\n }\n for (int tway = 0; tway < MAX_DIRECTIONS; ++tway) {\n if (fway == tway)\n continue;\n int amin = INFINITE_DISTANCE;\n bool ok = true;\n for (int op = 0; op < 2; ++op) {\n int atime = 0;\n for (int lu = 0; lu < 2; ++lu) {\n int fx = fkx + dx2[fway];\n int fy = fky + dy2[fway];\n int gx = fkx + dx2[tway];\n int gy = fky + dy2[tway];\n if (lu == 1) {\n gx += dx[(tway + 1) % MAX_DIRECTIONS];\n gy += dy[(tway + 1) % MAX_DIRECTIONS];\n }\n if (op ^ lu) {\n fx += dx[(fway + 1) % MAX_DIRECTIONS];\n fy += dy[(fway + 1) % MAX_DIRECTIONS];\n }\n if (field[gy][gx])\n ok = false;\n for (int ax = 0; ax < 2; ++ax) {\n for (int ay = 0; ay < 2; ++ay) {\n field[fky + ay][fkx + ax] = true;\n }\n }\n atime += getdis(field, fx, fy, gx, gy);\n for (int ax = 0; ax < 2; ++ax) {\n for (int ay = 0; ay < 2; ++ay) {\n field[fky + ay][fkx + ax] = false;\n }\n }\n }\n amin = min(amin, atime);\n }\n if (!ok)\n continue;\n else {\n if (memo2[fkx][fky][tway] > fturn + amin) {\n memo2[fkx][fky][tway] = fturn + amin;\n que.push(bb{fkx, fky, tway, fturn + amin});\n }\n }\n }\n {\n const int nkx = fkx + dx[fway];\n const int nky = fky + dy[fway];\n const int nway = (fway + 2) % MAX_DIRECTIONS;\n if (memo2[nkx][nky][nway] > fturn + 1) {\n memo2[nkx][nky][nway] = fturn + 1;\n que.push(bb{nkx, nky, nway, fturn + 1});\n }\n }\n }\n cout << ans << endl;\n }\n return 0;\n}", "accuracy": 1, "time_ms": 1090, "memory_kb": 3512, "score_of_the_acc": -0.8452, "final_rank": 9 }, { "submission_id": "aoj_1329_8820069", "code_snippet": "#include <iostream>\n#include <vector>\n#include <queue>\n#include <string>\n\nconst int dx[4] = {-1, 0, 1, 0};\nconst int dy[4] = {0, 1, 0, -1};\nconst int dx2[4] = {-1, 0, 2, 1};\nconst int dy2[4] = {0, 2, 1, -1};\n\nstruct aa {\n int x;\n int y;\n int turn;\n};\n\nint getdis(const std::vector<std::vector<int>> &field, const int fx, const int fy,\n const int gx, const int gy) {\n int ans = 999999;\n std::queue<aa> que;\n std::vector<std::vector<int>> memo(field.size(), std::vector<int>(field[0].size(), 999999));\n que.push(aa{fx, fy, 0});\n while (!que.empty()) {\n aa atop(que.front());\n que.pop();\n if (atop.x == gx && atop.y == gy) {\n ans = atop.turn;\n break;\n }\n for (int i = 0; i < 4; ++i) {\n const int nextx = atop.x + dx[i];\n const int nexty = atop.y + dy[i];\n if (!field[nexty][nextx]) {\n if (atop.turn + 1 < memo[nexty][nextx]) {\n memo[nexty][nextx] = atop.turn + 1;\n que.push(aa{nextx, nexty, atop.turn + 1});\n }\n }\n }\n }\n return ans;\n}\n\nstruct bb {\n int kx;\n int ky;\n int opway;\n int turn;\n};\n\nclass Compare {\npublic:\n bool operator()(const bb &l, const bb &r) { return l.turn > r.turn; }\n};\n\nint memo2[60][60][4];\n\nint main() {\n while (1) {\n for (int i = 0; i < 60; ++i) {\n for (int j = 0; j < 60; ++j) {\n for (int k = 0; k < 4; ++k) {\n memo2[i][j][k] = 999999;\n }\n }\n }\n\n // Init variables here\n int H, W;\n std::cin >> H >> W;\n\n if (!H)\n break;\n\n std::vector<std::vector<int>> field(H + 2, std::vector<int>(W + 2, true));\n std::priority_queue<bb, std::vector<bb>, Compare> que;\n\n // Reduction scope of variables\n {\n int kx = 99, ky = 99;\n int ox[2];\n int oy[2];\n int num = 0;\n\n for (int i = 0; i < H; ++i) {\n std::string st;\n std::cin >> st;\n for (int j = 0; j < W; ++j) {\n if (st[j] == 'X') {\n kx = std::min(kx, j + 1);\n ky = std::min(ky, i + 1);\n field[i + 1][j + 1] = false;\n } else if (st[j] == 'o') {\n field[i + 1][j + 1] = false;\n } else if (st[j] == '.') {\n field[i + 1][j + 1] = false;\n ox[num] = j + 1;\n oy[num] = i + 1;\n num++;\n }\n }\n }\n\n if (kx == 1 && ky == 1) {\n std::cout << 0 << '\\n';\n continue;\n }\n\n for (int way = 0; way < 4; ++way) {\n // Init variables here\n int amin = 999999;\n bool ok = true;\n\n for (int op = 0; op < 2; ++op) {\n int atime = 0;\n\n for (int lu = 0; lu < 2; ++lu) {\n const int fx = ox[op ^ lu];\n const int fy = oy[op ^ lu];\n int gx = kx + dx2[way];\n int gy = ky + dy2[way];\n\n if (lu == 1) {\n gx += dx[(way + 1) % 4];\n gy += dy[(way + 1) % 4];\n }\n\n if (field[gy][gx])\n ok = false;\n\n for (int ax = 0; ax < 2; ++ax) {\n for (int ay = 0; ay < 2; ++ay) {\n field[ky + ay][kx + ax] = true;\n }\n }\n\n atime += getdis(field, fx, fy, gx, gy);\n\n for (int ax = 0; ax < 2; ++ax) {\n for (int ay = 0; ay < 2; ++ay) {\n field[ky + ay][kx + ax] = false;\n }\n }\n }\n\n amin = std::min(amin, atime);\n }\n\n if (!ok)\n continue;\n else {\n memo2[kx][ky][way] = amin;\n que.push(bb{kx, ky, way, amin});\n }\n }\n }\n\n // Init variables here\n int ans = -1;\n\n while (!que.empty()) {\n bb atop(que.top());\n que.pop();\n\n const int fkx = atop.kx;\n const int fky = atop.ky;\n const int fway = atop.opway;\n const int fturn = atop.turn;\n\n if (fkx == 1 && fky == 1) {\n ans = fturn;\n break;\n }\n\n for (int tway = 0; tway < 4; ++tway) {\n if (fway == tway)\n continue;\n\n // Init variables here\n int amin = 999999;\n bool ok = true;\n\n for (int op = 0; op < 2; ++op) {\n int atime = 0;\n\n for (int lu = 0; lu < 2; ++lu) {\n int fx = fkx + dx2[fway];\n int fy = fky + dy2[fway];\n int gx = fkx + dx2[tway];\n int gy = fky + dy2[tway];\n\n if (lu == 1) {\n gx += dx[(tway + 1) % 4];\n gy += dy[(tway + 1) % 4];\n }\n\n if (op ^ lu) {\n fx += dx[(fway + 1) % 4];\n fy += dy[(fway + 1) % 4];\n }\n\n if (field[gy][gx])\n ok = false;\n\n for (int ax = 0; ax < 2; ++ax) {\n for (int ay = 0; ay < 2; ++ay) {\n field[fky + ay][fkx + ax] = true;\n }\n }\n\n atime += getdis(field, fx, fy, gx, gy);\n\n for (int ax = 0; ax < 2; ++ax) {\n for (int ay = 0; ay < 2; ++ay) {\n field[fky + ay][fkx + ax] = false;\n }\n }\n }\n\n amin = std::min(amin, atime);\n }\n\n if (!ok)\n continue;\n else {\n if (memo2[fkx][fky][tway] > fturn + amin) {\n memo2[fkx][fky][tway] = fturn + amin;\n que.push(bb{fkx, fky, tway, fturn + amin});\n }\n }\n }\n\n {\n const int nkx = fkx + dx[fway];\n const int nky = fky + dy[fway];\n const int nway = (fway + 2) % 4;\n\n if (memo2[nkx][nky][nway] > fturn + 1) {\n memo2[nkx][nky][nway] = fturn + 1;\n que.push(bb{nkx, nky, nway, fturn + 1});\n }\n }\n }\n\n std::cout << ans << '\\n';\n }\n\n return 0;\n}", "accuracy": 1, "time_ms": 1090, "memory_kb": 3316, "score_of_the_acc": -0.4796, "final_rank": 5 }, { "submission_id": "aoj_1329_8808049", "code_snippet": "#include <iostream>\n#include <vector>\n#include <queue>\n#include <string>\nusing namespace std;\n\nconst int dx[4] = {-1, 0, 1, 0};\nconst int dy[4] = {0, 1, 0, -1};\nconst int dx2[4] = {-1, 0, 2, 1};\nconst int dy2[4] = {0, 2, 1, -1};\n\nstruct aa {\n int x;\n int y;\n int turn;\n};\n\nint getdis(vector<vector<bool>> field, const int fx, const int fy,\n const int gx, const int gy) {\n int ans = 999999;\n queue<aa> que;\n vector<vector<int>> memo(field.size(), vector<int>(field[0].size(), 999999));\n que.push(aa{fx, fy, 0});\n while (!que.empty()) {\n aa atop(que.front());\n que.pop();\n if (atop.x == gx && atop.y == gy) {\n ans = atop.turn;\n break;\n }\n for (int i = 0; i < 4; ++i) {\n const int nextx = atop.x + dx[i];\n const int nexty = atop.y + dy[i];\n if (!field[nexty][nextx]) {\n if (atop.turn + 1 < memo[nexty][nextx]) {\n memo[nexty][nextx] = atop.turn + 1;\n que.push(aa{nextx, nexty, atop.turn + 1});\n }\n }\n }\n }\n return ans;\n}\n\nstruct bb {\n int kx;\n int ky;\n int opway;\n int turn;\n};\n\nclass Compare {\npublic:\n bool operator()(const bb &l, const bb &r) { return l.turn > r.turn; }\n};\n\nint memo2[60][60][4];\n\nint main() {\n while (1) {\n for (int i = 0; i < 60; ++i) {\n for (int j = 0; j < 60; ++j) {\n for (int k = 0; k < 4; ++k) {\n memo2[i][j][k] = 999999;\n }\n }\n }\n int H, W;\n cin >> H >> W;\n if (!H)\n break;\n vector<vector<bool>> field(H + 2, vector<bool>(W + 2, true));\n priority_queue<bb, vector<bb>, Compare> que;\n {\n int kx = 99, ky = 99;\n int ox[2];\n int oy[2];\n int num = 0;\n for (int i = 0; i < H; ++i) {\n string st;\n cin >> st;\n for (int j = 0; j < W; ++j) {\n if (st[j] == 'X') {\n kx = min(kx, j + 1);\n ky = min(ky, i + 1);\n field[i + 1][j + 1] = false;\n } else if (st[j] == 'o') {\n field[i + 1][j + 1] = false;\n } else if (st[j] == '.') {\n field[i + 1][j + 1] = false;\n ox[num] = j + 1;\n oy[num] = i + 1;\n num++;\n }\n }\n }\n if (kx == 1 && ky == 1) {\n cout << 0 << endl;\n continue;\n }\n for (int way = 0; way < 4; ++way) {\n int amin = 999999;\n bool ok = true;\n for (int op = 0; op < 2; ++op) {\n int atime = 0;\n for (int lu = 0; lu < 2; ++lu) {\n const int fx = ox[op ^ lu];\n const int fy = oy[op ^ lu];\n int gx = kx + dx2[way];\n int gy = ky + dy2[way];\n if (lu == 1) {\n gx += dx[(way + 1) % 4];\n gy += dy[(way + 1) % 4];\n }\n if (field[gy][gx])\n ok = false;\n for (int ax = 0; ax < 2; ++ax) {\n for (int ay = 0; ay < 2; ++ay) {\n field[ky + ay][kx + ax] = true;\n }\n }\n atime += getdis(field, fx, fy, gx, gy);\n for (int ax = 0; ax < 2; ++ax) {\n for (int ay = 0; ay < 2; ++ay) {\n field[ky + ay][kx + ax] = false;\n }\n }\n }\n amin = min(amin, atime);\n }\n if (!ok)\n continue;\n else {\n memo2[kx][ky][way] = amin;\n que.push(bb{kx, ky, way, amin});\n }\n }\n }\n int ans = -1;\n while (!que.empty()) {\n bb atop(que.top());\n que.pop();\n const int fkx = atop.kx;\n const int fky = atop.ky;\n const int fway = atop.opway;\n const int fturn = atop.turn;\n if (fkx == 1 && fky == 1) {\n ans = fturn;\n break;\n }\n for (int tway = 0; tway < 4; ++tway) {\n if (fway == tway)\n continue;\n int amin = 999999;\n bool ok = true;\n for (int op = 0; op < 2; ++op) {\n int atime = 0;\n for (int lu = 0; lu < 2; ++lu) {\n int fx = fkx + dx2[fway];\n int fy = fky + dy2[fway];\n int gx = fkx + dx2[tway];\n int gy = fky + dy2[tway];\n if (lu == 1) {\n gx += dx[(tway + 1) % 4];\n gy += dy[(tway + 1) % 4];\n }\n if (op ^ lu) {\n fx += dx[(fway + 1) % 4];\n fy += dy[(fway + 1) % 4];\n }\n if (field[gy][gx])\n ok = false;\n for (int ax = 0; ax < 2; ++ax) {\n for (int ay = 0; ay < 2; ++ay) {\n field[fky + ay][fkx + ax] = true;\n }\n }\n atime += getdis(field, fx, fy, gx, gy);\n for (int ax = 0; ax < 2; ++ax) {\n for (int ay = 0; ay < 2; ++ay) {\n field[fky + ay][fkx + ax] = false;\n }\n }\n }\n amin = min(amin, atime);\n }\n if (!ok)\n continue;\n else {\n if (memo2[fkx][fky][tway] > fturn + amin) {\n memo2[fkx][fky][tway] = fturn + amin;\n que.push(bb{fkx, fky, tway, fturn + amin});\n }\n }\n }\n {\n const int nkx = fkx + dx[fway];\n const int nky = fky + dy[fway];\n const int nway = (fway + 2) % 4;\n if (memo2[nkx][nky][nway] > fturn + 1) {\n memo2[nkx][nky][nway] = fturn + 1;\n que.push(bb{nkx, nky, nway, fturn + 1});\n }\n }\n }\n cout << ans << endl;\n }\n return 0;\n}", "accuracy": 1, "time_ms": 2070, "memory_kb": 3204, "score_of_the_acc": -1.0075, "final_rank": 14 }, { "submission_id": "aoj_1329_8215731", "code_snippet": "#include \"bits/stdc++.h\"\nusing namespace std;\nconst int dx[4] = {-1, 0, 1, 0};\nconst int dy[4] = {0, 1, 0, -1};\nconst int dx2[4] = {-1, 0, 2, 1};\nconst int dy2[4] = {0, 2, 1, -1};\nstruct aa {\n int x;\n int y;\n int turn;\n};\nint getdis(const vector<vector<int>> &field, const int fx, const int fy,\n const int gx, const int gy) {\n int ans = 999999;\n queue<aa> que;\n vector<vector<int>> memo(field.size(), vector<int>(field[0].size(), 999999));\n que.push(aa{fx, fy, 0});\n while (!que.empty()) {\n aa atop(que.front());\n que.pop();\n if (atop.x == gx && atop.y == gy) {\n ans = atop.turn;\n break;\n }\n for (int i = 0; i < 4; ++i) {\n const int nextx = atop.x + dx[i];\n const int nexty = atop.y + dy[i];\n if (!field[nexty][nextx]) {\n if (atop.turn + 1 < memo[nexty][nextx]) {\n memo[nexty][nextx] = atop.turn + 1;\n que.push(aa{nextx, nexty, atop.turn + 1});\n }\n }\n }\n }\n return ans;\n}\nstruct bb {\n int kx;\n int ky;\n int opway;\n int turn;\n};\nclass Compare {\npublic:\n bool operator()(const bb &l, const bb &r) { return l.turn > r.turn; }\n};\nint memo2[52][52][4];\nint main() {\n while (1) {\n for (int i = 0; i < 52; ++i) {\n for (int j = 0; j < 52; ++j) {\n for (int k = 0; k < 4; ++k) {\n memo2[i][j][k] = 999999;\n }\n }\n }\n int H, W;\n cin >> H >> W;\n if (!H)\n break;\n vector<vector<int>> field(H + 2, vector<int>(W + 2, true));\n priority_queue<bb, vector<bb>, Compare> que;\n {\n int kx = 99, ky = 99;\n int ox[2];\n int oy[2];\n int num = 0;\n for (int i = 0; i < H; ++i) {\n string st;\n cin >> st;\n for (int j = 0; j < W; ++j) {\n if (st[j] == 'X') {\n kx = min(kx, j + 1);\n ky = min(ky, i + 1);\n field[i + 1][j + 1] = false;\n } else if (st[j] == 'o') {\n field[i + 1][j + 1] = false;\n } else if (st[j] == '.') {\n field[i + 1][j + 1] = false;\n ox[num] = j + 1;\n oy[num] = i + 1;\n num++;\n }\n }\n }\n if (kx == 1 && ky == 1) {\n cout << 0 << endl;\n continue;\n }\n for (int way = 0; way < 4; ++way) {\n int amin = 999999;\n bool ok = true;\n for (int op = 0; op < 2; ++op) {\n int atime = 0;\n for (int lu = 0; lu < 2; ++lu) {\n const int fx = ox[op ^ lu];\n const int fy = oy[op ^ lu];\n int gx = kx + dx2[way];\n int gy = ky + dy2[way];\n if (lu == 1) {\n gx += dx[(way + 1) % 4];\n gy += dy[(way + 1) % 4];\n }\n if (field[gy][gx])\n ok = false;\n for (int ax = 0; ax < 2; ++ax) {\n for (int ay = 0; ay < 2; ++ay) {\n field[ky + ay][kx + ax] = true;\n }\n }\n atime += getdis(field, fx, fy, gx, gy);\n for (int ax = 0; ax < 2; ++ax) {\n for (int ay = 0; ay < 2; ++ay) {\n field[ky + ay][kx + ax] = false;\n }\n }\n }\n amin = min(amin, atime);\n }\n if (!ok)\n continue;\n else {\n memo2[kx][ky][way] = amin;\n que.push(bb{kx, ky, way, amin});\n }\n }\n }\n int ans = -1;\n while (!que.empty()) {\n bb atop(que.top());\n que.pop();\n const int fkx = atop.kx;\n const int fky = atop.ky;\n const int fway = atop.opway;\n const int fturn = atop.turn;\n if (fkx == 1 && fky == 1) {\n ans = fturn;\n break;\n }\n for (int tway = 0; tway < 4; ++tway) {\n if (fway == tway)\n continue;\n int amin = 999999;\n bool ok = true;\n for (int op = 0; op < 2; ++op) {\n int atime = 0;\n for (int lu = 0; lu < 2; ++lu) {\n int fx = fkx + dx2[fway];\n int fy = fky + dy2[fway];\n int gx = fkx + dx2[tway];\n int gy = fky + dy2[tway];\n if (lu == 1) {\n gx += dx[(tway + 1) % 4];\n gy += dy[(tway + 1) % 4];\n }\n if (op ^ lu) {\n fx += dx[(fway + 1) % 4];\n fy += dy[(fway + 1) % 4];\n }\n if (field[gy][gx])\n ok = false;\n for (int ax = 0; ax < 2; ++ax) {\n for (int ay = 0; ay < 2; ++ay) {\n field[fky + ay][fkx + ax] = true;\n }\n }\n atime += getdis(field, fx, fy, gx, gy);\n for (int ax = 0; ax < 2; ++ax) {\n for (int ay = 0; ay < 2; ++ay) {\n field[fky + ay][fkx + ax] = false;\n }\n }\n }\n amin = min(amin, atime);\n }\n if (!ok)\n continue;\n else {\n if (memo2[fkx][fky][tway] > fturn + amin) {\n memo2[fkx][fky][tway] = fturn + amin;\n que.push(bb{fkx, fky, tway, fturn + amin});\n }\n }\n }\n {\n const int nkx = fkx + dx[fway];\n const int nky = fky + dy[fway];\n const int nway = (fway + 2) % 4;\n if (memo2[nkx][nky][nway] > fturn + 1) {\n memo2[nkx][nky][nway] = fturn + 1;\n que.push(bb{nkx, nky, nway, fturn + 1});\n }\n }\n }\n cout << ans << endl;\n }\n return 0;\n}", "accuracy": 1, "time_ms": 1070, "memory_kb": 3256, "score_of_the_acc": -0.3526, "final_rank": 4 }, { "submission_id": "aoj_1329_8215727", "code_snippet": "#include \"bits/stdc++.h\"\nusing namespace std;\nconst int dx[4] = {-1, 0, 1, 0};\nconst int dy[4] = {0, 1, 0, -1};\nconst int dx2[4] = {-1, 0, 2, 1};\nconst int dy2[4] = {0, 2, 1, -1};\nstruct aa {\n int x;\n int y;\n int turn;\n};\nint getdis(const vector<vector<int>> &field, const int fx, const int fy,\n const int gx, const int gy) {\n int ans = 999999;\n queue<aa> que;\n vector<vector<int>> memo(field.size(), vector<int>(field[0].size(), 999999));\n que.push(aa{fx, fy, 0});\n while (!que.empty()) {\n aa atop(que.front());\n que.pop();\n if (atop.x == gx && atop.y == gy) {\n ans = atop.turn;\n break;\n }\n for (int i = 0; i < 4; ++i) {\n const int nextx = atop.x + dx[i];\n const int nexty = atop.y + dy[i];\n if (!field[nexty][nextx]) {\n if (atop.turn + 1 < memo[nexty][nextx]) {\n memo[nexty][nextx] = atop.turn + 1;\n que.push(aa{nextx, nexty, atop.turn + 1});\n }\n }\n }\n }\n return ans;\n}\nstruct bb {\n int kx;\n int ky;\n int opway;\n int turn;\n};\nclass Compare {\npublic:\n bool operator()(const bb &l, const bb &r) { return l.turn > r.turn; }\n};\nint memo2[60][60][4];\nint main() {\n while (1) {\n for (int i = 0; i < 60; ++i) {\n for (int j = 0; j < 60; ++j) {\n for (int k = 0; k < 4; ++k) {\n memo2[i][j][k] = 999999;\n }\n }\n }\n int H, W;\n cin >> H >> W;\n if (!H)\n break;\n vector<vector<int>> field(H + 2, vector<int>(W + 2, true));\n priority_queue<bb, vector<bb>, Compare> que;\n {\n int kx = 99, ky = 99;\n int ox[2];\n int oy[2];\n int num = 0;\n for (int i = 0; i < H; ++i) {\n string st;\n cin >> st;\n for (int j = 0; j < W; ++j) {\n if (st[j] == 'X') {\n kx = min(kx, j + 1);\n ky = min(ky, i + 1);\n field[i + 1][j + 1] = false;\n } else if (st[j] == 'o') {\n field[i + 1][j + 1] = false;\n } else if (st[j] == '.') {\n field[i + 1][j + 1] = false;\n ox[num] = j + 1;\n oy[num] = i + 1;\n num++;\n }\n }\n }\n if (kx == 1 && ky == 1) {\n cout << 0 << endl;\n continue;\n }\n for (int way = 0; way < 4; ++way) {\n int amin = 999999;\n bool ok = true;\n for (int op = 0; op < 2; ++op) {\n int atime = 0;\n for (int lu = 0; lu < 2; ++lu) {\n const int fx = ox[op ^ lu];\n const int fy = oy[op ^ lu];\n int gx = kx + dx2[way];\n int gy = ky + dy2[way];\n if (lu == 1) {\n gx += dx[(way + 1) % 4];\n gy += dy[(way + 1) % 4];\n }\n if (field[gy][gx])\n ok = false;\n for (int ax = 0; ax < 2; ++ax) {\n for (int ay = 0; ay < 2; ++ay) {\n field[ky + ay][kx + ax] = true;\n }\n }\n atime += getdis(field, fx, fy, gx, gy);\n for (int ax = 0; ax < 2; ++ax) {\n for (int ay = 0; ay < 2; ++ay) {\n field[ky + ay][kx + ax] = false;\n }\n }\n }\n amin = min(amin, atime);\n }\n if (!ok)\n continue;\n else {\n memo2[kx][ky][way] = amin;\n que.push(bb{kx, ky, way, amin});\n }\n }\n }\n int ans = -1;\n while (!que.empty()) {\n bb atop(que.top());\n que.pop();\n const int fkx = atop.kx;\n const int fky = atop.ky;\n const int fway = atop.opway;\n const int fturn = atop.turn;\n if (fkx == 1 && fky == 1) {\n ans = fturn;\n break;\n }\n for (int tway = 0; tway < 4; ++tway) {\n if (fway == tway)\n continue;\n int amin = 999999;\n bool ok = true;\n for (int op = 0; op < 2; ++op) {\n int atime = 0;\n for (int lu = 0; lu < 2; ++lu) {\n int fx = fkx + dx2[fway];\n int fy = fky + dy2[fway];\n int gx = fkx + dx2[tway];\n int gy = fky + dy2[tway];\n if (lu == 1) {\n gx += dx[(tway + 1) % 4];\n gy += dy[(tway + 1) % 4];\n }\n if (op ^ lu) {\n fx += dx[(fway + 1) % 4];\n fy += dy[(fway + 1) % 4];\n }\n if (field[gy][gx])\n ok = false;\n for (int ax = 0; ax < 2; ++ax) {\n for (int ay = 0; ay < 2; ++ay) {\n field[fky + ay][fkx + ax] = true;\n }\n }\n atime += getdis(field, fx, fy, gx, gy);\n for (int ax = 0; ax < 2; ++ax) {\n for (int ay = 0; ay < 2; ++ay) {\n field[fky + ay][fkx + ax] = false;\n }\n }\n }\n amin = min(amin, atime);\n }\n if (!ok)\n continue;\n else {\n if (memo2[fkx][fky][tway] > fturn + amin) {\n memo2[fkx][fky][tway] = fturn + amin;\n que.push(bb{fkx, fky, tway, fturn + amin});\n }\n }\n }\n {\n const int nkx = fkx + dx[fway];\n const int nky = fky + dy[fway];\n const int nway = (fway + 2) % 4;\n if (memo2[nkx][nky][nway] > fturn + 1) {\n memo2[nkx][nky][nway] = fturn + 1;\n que.push(bb{nkx, nky, nway, fturn + 1});\n }\n }\n }\n cout << ans << endl;\n }\n return 0;\n}", "accuracy": 1, "time_ms": 1060, "memory_kb": 3200, "score_of_the_acc": -0.2406, "final_rank": 1 }, { "submission_id": "aoj_1329_8116085", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\nconst int dx[4] = {-1, 0, 1, 0};\nconst int dy[4] = {0, 1, 0, -1};\nconst int dx2[4] = {-1, 0, 2, 1};\nconst int dy2[4] = {0, 2, 1, -1};\n\nstruct aa {\n int x, y, turn;\n};\n\nint getdis(const vector<vector<int>> &field, const int fx, const int fy, const int gx, const int gy) {\n int ans = 999999;\n queue<aa> que;\n vector<vector<int>> memo(field.size(), vector<int>(field[0].size(), 999999));\n que.push({fx, fy, 0});\n while (!que.empty()) {\n aa atop(que.front());\n que.pop();\n if (atop.x == gx && atop.y == gy) {\n ans = atop.turn;\n break;\n }\n for (int i = 0; i < 4; ++i) {\n const int nextx = atop.x + dx[i];\n const int nexty = atop.y + dy[i];\n if (!field[nexty][nextx]) {\n if (atop.turn + 1 < memo[nexty][nextx]) {\n memo[nexty][nextx] = atop.turn + 1;\n que.push({nextx, nexty, atop.turn + 1});\n }\n }\n }\n }\n return ans;\n}\n\nstruct bb {\n int kx, ky, opway, turn;\n};\n\nclass Compare {\npublic:\n bool operator()(const bb &l, const bb &r) { return l.turn > r.turn; }\n};\n\nint memo2[60][60][4];\n\nint main() {\n ios_base::sync_with_stdio(false);\n cin.tie(0);\n\n while (true) {\n for (int i = 0; i < 60; ++i) {\n for (int j = 0; j < 60; ++j) {\n for (int k = 0; k < 4; ++k) {\n memo2[i][j][k] = 999999;\n }\n }\n }\n int H, W;\n cin >> H >> W;\n if (!H) break;\n vector<vector<int>> field(H + 2, vector<int>(W + 2, true));\n priority_queue<bb, vector<bb>, Compare> que;\n {\n int kx = 99, ky = 99;\n int ox[2];\n int oy[2];\n int num = 0;\n for (int i = 0; i < H; ++i) {\n string st;\n cin >> st;\n for (int j = 0; j < W; ++j) {\n if (st[j] == 'X') {\n kx = min(kx, j + 1);\n ky = min(ky, i + 1);\n field[i + 1][j + 1] = false;\n } else if (st[j] == 'o') {\n field[i + 1][j + 1] = false;\n } else if (st[j] == '.') {\n field[i + 1][j + 1] = false;\n ox[num] = j + 1;\n oy[num] = i + 1;\n num++;\n }\n }\n }\n if (kx == 1 && ky == 1) {\n cout << 0 << '\\n';\n continue;\n }\n for (int way = 0; way < 4; ++way) {\n int amin = 999999;\n bool ok = true;\n for (int op = 0; op < 2; ++op) {\n int atime = 0;\n for (int lu = 0; lu < 2; ++lu) {\n const int fx = ox[op ^ lu];\n const int fy = oy[op ^ lu];\n int gx = kx + dx2[way];\n int gy = ky + dy2[way];\n if (lu == 1) {\n gx += dx[(way + 1) % 4];\n gy += dy[(way + 1) % 4];\n }\n if (field[gy][gx]) ok = false;\n for (int ax = 0; ax < 2; ++ax) {\n for (int ay = 0; ay < 2; ++ay) {\n field[ky + ay][kx + ax] = true;\n }\n }\n atime += getdis(field, fx, fy, gx, gy);\n for (int ax = 0; ax < 2; ++ax) {\n for (int ay = 0; ay < 2; ++ay) {\n field[ky + ay][kx + ax] = false;\n }\n }\n }\n amin = min(amin, atime);\n }\n if (!ok) continue;\n else {\n memo2[kx][ky][way] = amin;\n que.push({kx, ky, way, amin});\n }\n }\n }\n int ans = -1;\n while (!que.empty()) {\n bb atop(que.top());\n que.pop();\n const int fkx = atop.kx;\n const int fky = atop.ky;\n const int fway = atop.opway;\n const int fturn = atop.turn;\n if (fkx == 1 && fky == 1) {\n ans = fturn;\n break;\n }\n for (int tway = 0; tway < 4; ++tway) {\n if (fway == tway) continue;\n int amin = 999999;\n bool ok = true;\n for (int op = 0; op < 2; ++op) {\n int atime = 0;\n for (int lu = 0; lu < 2; ++lu) {\n int fx = fkx + dx2[fway];\n int fy = fky + dy2[fway];\n int gx = fkx + dx2[tway];\n int gy = fky + dy2[tway];\n if (lu == 1) {\n gx += dx[(tway + 1) % 4];\n gy += dy[(tway + 1) % 4];\n }\n if (op ^ lu) {\n fx += dx[(fway + 1) % 4];\n fy += dy[(fway + 1) % 4];\n }\n if (field[gy][gx]) ok = false;\n for (int ax = 0; ax < 2; ++ax) {\n for (int ay = 0; ay < 2; ++ay) {\n field[fky + ay][fkx + ax] = true;\n }\n }\n atime += getdis(field, fx, fy, gx, gy);\n for (int ax = 0; ax < 2; ++ax) {\n for (int ay = 0; ay < 2; ++ay) {\n field[fky + ay][fkx + ax] = false;\n }\n }\n }\n amin = min(amin, atime);\n }\n if (!ok) continue;\n else {\n if (memo2[fkx][fky][tway] > fturn + amin) {\n memo2[fkx][fky][tway] = fturn + amin;\n que.push({fkx, fky, tway, fturn + amin});\n }\n }\n }\n const int nkx = fkx + dx[fway];\n const int nky = fky + dy[fway];\n const int nway = (fway + 2) % 4;\n if (memo2[nkx][nky][nway] > fturn + 1) {\n memo2[nkx][nky][nway] = fturn + 1;\n que.push({nkx, nky, nway, fturn + 1});\n }\n }\n cout << ans << '\\n';\n }\n\n return 0;\n}", "accuracy": 1, "time_ms": 1060, "memory_kb": 3416, "score_of_the_acc": -0.6436, "final_rank": 6 }, { "submission_id": "aoj_1329_8116084", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nconst int dx[4] = {-1, 0, 1, 0};\nconst int dy[4] = {0, 1, 0, -1};\nconst int dx2[4] = {-1, 0, 2, 1};\nconst int dy2[4] = {0, 2, 1, -1};\nstruct aa {\n int x;\n int y;\n int turn;\n};\nint getdis(const vector<vector<int>> &field, const int fx, const int fy,\n const int gx, const int gy) {\n int ans = 999999;\n queue<aa> que;\n vector<vector<int>> memo(field.size(), vector<int>(field[0].size(), 999999));\n que.push(aa{fx, fy, 0});\n while (!que.empty()) {\n aa atop(que.front());\n que.pop();\n if (atop.x == gx && atop.y == gy) {\n ans = atop.turn;\n break;\n }\n for (int i = 0; i < 4; ++i) {\n const int nextx = atop.x + dx[i];\n const int nexty = atop.y + dy[i];\n if (!field[nexty][nextx]) {\n if (atop.turn + 1 < memo[nexty][nextx]) {\n memo[nexty][nextx] = atop.turn + 1;\n que.push(aa{nextx, nexty, atop.turn + 1});\n }\n }\n }\n }\n return ans;\n}\nstruct bb {\n int kx;\n int ky;\n int opway;\n int turn;\n};\nclass Compare {\npublic:\n bool operator()(const bb &l, const bb &r) { return l.turn > r.turn; }\n};\nint memo2[60][60][4];\nint main() {\n ios::sync_with_stdio(false);\n cin.tie(NULL);\n cout.tie(NULL);\n while (1) {\n for (int i = 0; i < 60; ++i) {\n for (int j = 0; j < 60; ++j) {\n for (int k = 0; k < 4; ++k) {\n memo2[i][j][k] = 999999;\n }\n }\n }\n int H, W;\n cin >> H >> W;\n if (!H)\n break;\n vector<vector<int>> field(H + 2, vector<int>(W + 2, true));\n priority_queue<bb, vector<bb>, Compare> que;\n {\n int kx = 99, ky = 99;\n int ox[2];\n int oy[2];\n int num = 0;\n for (int i = 0; i < H; ++i) {\n string st;\n cin >> st;\n for (int j = 0; j < W; ++j) {\n if (st[j] == 'X') {\n kx = min(kx, j + 1);\n ky = min(ky, i + 1);\n field[i + 1][j + 1] = false;\n } else if (st[j] == 'o') {\n field[i + 1][j + 1] = false;\n } else if (st[j] == '.') {\n field[i + 1][j + 1] = false;\n ox[num] = j + 1;\n oy[num] = i + 1;\n num++;\n }\n }\n }\n if (kx == 1 && ky == 1) {\n cout << 0 << endl;\n continue;\n }\n for (int way = 0; way < 4; ++way) {\n int amin = 999999;\n bool ok = true;\n for (int op = 0; op < 2; ++op) {\n int atime = 0;\n for (int lu = 0; lu < 2; ++lu) {\n const int fx = ox[op ^ lu];\n const int fy = oy[op ^ lu];\n int gx = kx + dx2[way];\n int gy = ky + dy2[way];\n if (lu == 1) {\n gx += dx[(way + 1) % 4];\n gy += dy[(way + 1) % 4];\n }\n if (field[gy][gx])\n ok = false;\n for (int ax = 0; ax < 2; ++ax) {\n for (int ay = 0; ay < 2; ++ay) {\n field[ky + ay][kx + ax] = true;\n }\n }\n atime += getdis(field, fx, fy, gx, gy);\n for (int ax = 0; ax < 2; ++ax) {\n for (int ay = 0; ay < 2; ++ay) {\n field[ky + ay][kx + ax] = false;\n }\n }\n }\n amin = min(amin, atime);\n }\n if (!ok)\n continue;\n else {\n memo2[kx][ky][way] = amin;\n que.push(bb{kx, ky, way, amin});\n }\n }\n }\n int ans = -1;\n while (!que.empty()) {\n bb atop(que.top());\n que.pop();\n const int fkx = atop.kx;\n const int fky = atop.ky;\n const int fway = atop.opway;\n const int fturn = atop.turn;\n if (fkx == 1 && fky == 1) {\n ans = fturn;\n break;\n }\n for (int tway = 0; tway < 4; ++tway) {\n if (fway == tway)\n continue;\n int amin = 999999;\n bool ok = true;\n for (int op = 0; op < 2; ++op) {\n int atime = 0;\n for (int lu = 0; lu < 2; ++lu) {\n int fx = fkx + dx2[fway];\n int fy = fky + dy2[fway];\n int gx = fkx + dx2[tway];\n int gy = fky + dy2[tway];\n if (lu == 1) {\n gx += dx[(tway + 1) % 4];\n gy += dy[(tway + 1) % 4];\n }\n if (op ^ lu) {\n fx += dx[(fway + 1) % 4];\n fy += dy[(fway + 1) % 4];\n }\n if (field[gy][gx])\n ok = false;\n for (int ax = 0; ax < 2; ++ax) {\n for (int ay = 0; ay < 2; ++ay) {\n field[fky + ay][fkx + ax] = true;\n }\n }\n atime += getdis(field, fx, fy, gx, gy);\n for (int ax = 0; ax < 2; ++ax) {\n for (int ay = 0; ay < 2; ++ay) {\n field[fky + ay][fkx + ax] = false;\n }\n }\n }\n amin = min(amin, atime);\n }\n if (!ok)\n continue;\n else {\n if (memo2[fkx][fky][tway] > fturn + amin) {\n memo2[fkx][fky][tway] = fturn + amin;\n que.push(bb{fkx, fky, tway, fturn + amin});\n }\n }\n }\n {\n const int nkx = fkx + dx[fway];\n const int nky = fky + dy[fway];\n const int nway = (fway + 2) % 4;\n if (memo2[nkx][nky][nway] > fturn + 1) {\n memo2[nkx][nky][nway] = fturn + 1;\n que.push(bb{nkx, nky, nway, fturn + 1});\n }\n }\n }\n cout << ans << endl;\n }\n return 0;\n}", "accuracy": 1, "time_ms": 1070, "memory_kb": 3224, "score_of_the_acc": -0.2929, "final_rank": 3 }, { "submission_id": "aoj_1329_8116081", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nconst int dx[4] = {-1, 0, 1, 0};\nconst int dy[4] = {0, 1, 0, -1};\nconst int dx2[4] = {-1, 0, 2, 1};\nconst int dy2[4] = {0, 2, 1, -1};\nstruct aa {\n int x;\n int y;\n int turn;\n};\nint getdis(const vector<vector<int>> &field, const int fx, const int fy,\n const int gx, const int gy) {\n int ans = 999999;\n queue<aa> que;\n vector<vector<int>> memo(field.size(), vector<int>(field[0].size(), 999999));\n que.push(aa{fx, fy, 0});\n memo[fy][fx] = 0;\n while (!que.empty()) {\n aa atop(que.front());\n que.pop();\n if (atop.x == gx && atop.y == gy) {\n ans = atop.turn;\n break;\n }\n for (int i = 0; i < 4; ++i) {\n const int nextx = atop.x + dx[i];\n const int nexty = atop.y + dy[i];\n if (!field[nexty][nextx]) {\n if (atop.turn + 1 < memo[nexty][nextx]) {\n memo[nexty][nextx] = atop.turn + 1;\n que.push(aa{nextx, nexty, atop.turn + 1});\n }\n }\n }\n }\n return ans;\n}\nstruct bb {\n int kx;\n int ky;\n int opway;\n int turn;\n};\nclass Compare {\npublic:\n bool operator()(const bb &l, const bb &r) { return l.turn > r.turn; }\n};\nint memo2[60][60][4];\nint main() {\n ios::sync_with_stdio(false);\n cin.tie(0);\n while (1) {\n for (int i = 0; i < 60; ++i) {\n for (int j = 0; j < 60; ++j) {\n for (int k = 0; k < 4; ++k) {\n memo2[i][j][k] = 999999;\n }\n }\n }\n int H, W;\n cin >> H >> W;\n if (!H)\n break;\n vector<vector<int>> field(H + 2, vector<int>(W + 2, true));\n priority_queue<bb, vector<bb>, Compare> que;\n {\n int kx = 99, ky = 99;\n int ox[2];\n int oy[2];\n int num = 0;\n for (int i = 0; i < H; ++i) {\n string st;\n cin >> st;\n for (int j = 0; j < W; ++j) {\n if (st[j] == 'X') {\n kx = min(kx, j + 1);\n ky = min(ky, i + 1);\n field[i + 1][j + 1] = false;\n } else if (st[j] == 'o') {\n field[i + 1][j + 1] = false;\n } else if (st[j] == '.') {\n field[i + 1][j + 1] = false;\n ox[num] = j + 1;\n oy[num] = i + 1;\n num++;\n }\n }\n }\n if (kx == 1 && ky == 1) {\n cout << 0 << endl;\n continue;\n }\n for (int way = 0; way < 4; ++way) {\n int amin = 999999;\n bool ok = true;\n for (int op = 0; op < 2; ++op) {\n int atime = 0;\n for (int lu = 0; lu < 2; ++lu) {\n const int fx = ox[op ^ lu];\n const int fy = oy[op ^ lu];\n int gx = kx + dx2[way];\n int gy = ky + dy2[way];\n if (lu == 1) {\n gx += dx[(way + 1) % 4];\n gy += dy[(way + 1) % 4];\n }\n if (field[gy][gx])\n ok = false;\n for (int ax = 0; ax < 2; ++ax) {\n for (int ay = 0; ay < 2; ++ay) {\n field[ky + ay][kx + ax] = true;\n }\n }\n atime += getdis(field, fx, fy, gx, gy);\n for (int ax = 0; ax < 2; ++ax) {\n for (int ay = 0; ay < 2; ++ay) {\n field[ky + ay][kx + ax] = false;\n }\n }\n }\n amin = min(amin, atime);\n }\n if (!ok)\n continue;\n else {\n memo2[kx][ky][way] = amin;\n que.push(bb{kx, ky, way, amin});\n }\n }\n }\n int ans = -1;\n while (!que.empty()) {\n bb atop(que.top());\n que.pop();\n const int fkx = atop.kx;\n const int fky = atop.ky;\n const int fway = atop.opway;\n const int fturn = atop.turn;\n if (fkx == 1 && fky == 1) {\n ans = fturn;\n break;\n }\n for (int tway = 0; tway < 4; ++tway) {\n if (fway == tway)\n continue;\n int amin = 999999;\n bool ok = true;\n for (int op = 0; op < 2; ++op) {\n int atime = 0;\n for (int lu = 0; lu < 2; ++lu) {\n int fx = fkx + dx2[fway];\n int fy = fky + dy2[fway];\n int gx = fkx + dx2[tway];\n int gy = fky + dy2[tway];\n if (lu == 1) {\n gx += dx[(tway + 1) % 4];\n gy += dy[(tway + 1) % 4];\n }\n if (op ^ lu) {\n fx += dx[(fway + 1) % 4];\n fy += dy[(fway + 1) % 4];\n }\n if (field[gy][gx])\n ok = false;\n for (int ax = 0; ax < 2; ++ax) {\n for (int ay = 0; ay < 2; ++ay) {\n field[fky + ay][fkx + ax] = true;\n }\n }\n atime += getdis(field, fx, fy, gx, gy);\n for (int ax = 0; ax < 2; ++ax) {\n for (int ay = 0; ay < 2; ++ay) {\n field[fky + ay][fkx + ax] = false;\n }\n }\n }\n amin = min(amin, atime);\n }\n if (!ok)\n continue;\n else {\n if (memo2[fkx][fky][tway] > fturn + amin) {\n memo2[fkx][fky][tway] = fturn + amin;\n que.push(bb{fkx, fky, tway, fturn + amin});\n }\n }\n }\n {\n const int nkx = fkx + dx[fway];\n const int nky = fky + dy[fway];\n const int nway = (fway + 2) % 4;\n if (memo2[nkx][nky][nway] > fturn + 1) {\n memo2[nkx][nky][nway] = fturn + 1;\n que.push(bb{nkx, nky, nway, fturn + 1});\n }\n }\n }\n cout << ans << endl;\n }\n return 0;\n}", "accuracy": 1, "time_ms": 1080, "memory_kb": 3536, "score_of_the_acc": -0.8825, "final_rank": 10 }, { "submission_id": "aoj_1329_8116078", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\nconst int dx[4] = {-1, 0, 1, 0};\nconst int dy[4] = {0, 1, 0, -1};\nconst int dx2[4] = {-1, 0, 2, 1};\nconst int dy2[4] = {0, 2, 1, -1};\n\nstruct aa {\n int x, y, turn;\n};\n\nint getdis(const vector<vector<int>> &field, const int fx, const int fy, const int gx, const int gy) {\n int ans = 999999;\n queue<aa> que;\n vector<vector<int>> memo(field.size(), vector<int>(field[0].size(), 999999));\n que.push(aa{fx, fy, 0});\n\n while (!que.empty()) {\n aa atop(que.front());\n que.pop();\n if (atop.x == gx && atop.y == gy) {\n ans = atop.turn;\n break;\n }\n for (int i = 0; i < 4; ++i) {\n const int nextx = atop.x + dx[i];\n const int nexty = atop.y + dy[i];\n if (!field[nexty][nextx]) {\n if (atop.turn + 1 < memo[nexty][nextx]) {\n memo[nexty][nextx] = atop.turn + 1;\n que.push(aa{nextx, nexty, atop.turn + 1});\n }\n }\n }\n }\n\n return ans;\n}\n\nstruct bb {\n int kx, ky, opway, turn;\n};\n\nclass Compare {\npublic:\n bool operator()(const bb &l, const bb &r) { return l.turn > r.turn; }\n};\n\nint memo2[60][60][4];\n\nint main() {\n ios_base::sync_with_stdio(false);\n cin.tie(NULL);\n cout.tie(NULL);\n\n while (true) {\n for (int i = 0; i < 60; ++i) {\n for (int j = 0; j < 60; ++j) {\n for (int k = 0; k < 4; ++k) {\n memo2[i][j][k] = 999999;\n }\n }\n }\n\n int H, W;\n cin >> H >> W;\n if (!H) break;\n\n vector<vector<int>> field(H + 2, vector<int>(W + 2, true));\n priority_queue<bb, vector<bb>, Compare> que;\n\n int kx = 99, ky = 99;\n int ox[2];\n int oy[2];\n int num = 0;\n\n for (int i = 0; i < H; ++i) {\n string st;\n cin >> st;\n for (int j = 0; j < W; ++j) {\n if (st[j] == 'X') {\n kx = min(kx, j + 1);\n ky = min(ky, i + 1);\n field[i + 1][j + 1] = false;\n } else if (st[j] == 'o') {\n field[i + 1][j + 1] = false;\n } else if (st[j] == '.') {\n field[i + 1][j + 1] = false;\n ox[num] = j + 1;\n oy[num] = i + 1;\n num++;\n }\n }\n }\n\n if (kx == 1 && ky == 1) {\n cout << 0 << endl;\n continue;\n }\n\n for (int way = 0; way < 4; ++way) {\n int amin = 999999;\n bool ok = true;\n for (int op = 0; op < 2; ++op) {\n int atime = 0;\n for (int lu = 0; lu < 2; ++lu) {\n const int fx = ox[op ^ lu];\n const int fy = oy[op ^ lu];\n int gx = kx + dx2[way];\n int gy = ky + dy2[way];\n if (lu == 1) {\n gx += dx[(way + 1) % 4];\n gy += dy[(way + 1) % 4];\n }\n if (field[gy][gx]) ok = false;\n for (int ax = 0; ax < 2; ++ax) {\n for (int ay = 0; ay < 2; ++ay) {\n field[ky + ay][kx + ax] = true;\n }\n }\n atime += getdis(field, fx, fy, gx, gy);\n for (int ax = 0; ax < 2; ++ax) {\n for (int ay = 0; ay < 2; ++ay) {\n field[ky + ay][kx + ax] = false;\n }\n }\n }\n amin = min(amin, atime);\n }\n if (!ok) continue;\n else {\n memo2[kx][ky][way] = amin;\n que.push(bb{kx, ky, way, amin});\n }\n }\n\n int ans = -1;\n\n while (!que.empty()) {\n bb atop(que.top());\n que.pop();\n const int fkx = atop.kx;\n const int fky = atop.ky;\n const int fway = atop.opway;\n const int fturn = atop.turn;\n if (fkx == 1 && fky == 1) {\n ans = fturn;\n break;\n }\n for (int tway = 0; tway < 4; ++tway) {\n if (fway == tway) continue;\n int amin = 999999;\n bool ok = true;\n for (int op = 0; op < 2; ++op) {\n int atime = 0;\n for (int lu = 0; lu < 2; ++lu) {\n int fx = fkx + dx2[fway];\n int fy = fky + dy2[fway];\n int gx = fkx + dx2[tway];\n int gy = fky + dy2[tway];\n if (lu == 1) {\n gx += dx[(tway + 1) % 4];\n gy += dy[(tway + 1) % 4];\n }\n if (op ^ lu) {\n fx += dx[(fway + 1) % 4];\n fy += dy[(fway + 1) % 4];\n }\n if (field[gy][gx]) ok = false;\n for (int ax = 0; ax < 2; ++ax) {\n for (int ay = 0; ay < 2; ++ay) {\n field[fky + ay][fkx + ax] = true;\n }\n }\n atime += getdis(field, fx, fy, gx, gy);\n for (int ax = 0; ax < 2; ++ax) {\n for (int ay = 0; ay < 2; ++ay) {\n field[fky + ay][fkx + ax] = false;\n }\n }\n }\n amin = min(amin, atime);\n }\n if (!ok) continue;\n else {\n if (memo2[fkx][fky][tway] > fturn + amin) {\n memo2[fkx][fky][tway] = fturn + amin;\n que.push(bb{fkx, fky, tway, fturn + amin});\n }\n }\n }\n {\n const int nkx = fkx + dx[fway];\n const int nky = fky + dy[fway];\n const int nway = (fway + 2) % 4;\n if (memo2[nkx][nky][nway] > fturn + 1) {\n memo2[nkx][nky][nway] = fturn + 1;\n que.push(bb{nkx, nky, nway, fturn + 1});\n }\n }\n }\n\n cout << ans << endl;\n }\n\n return 0;\n}", "accuracy": 1, "time_ms": 1070, "memory_kb": 3448, "score_of_the_acc": -0.7108, "final_rank": 7 }, { "submission_id": "aoj_1329_8116074", "code_snippet": "#include <iostream>\n#include <vector>\n#include <queue>\nusing namespace std;\n\nconst int dx[4] = {-1, 0, 1, 0};\nconst int dy[4] = {0, 1, 0, -1};\nconst int dx2[4] = {-1, 0, 2, 1};\nconst int dy2[4] = {0, 2, 1, -1};\n\nstruct aa {\n int x;\n int y;\n int turn;\n};\n\nint getdis(const vector<vector<int>> &field, const int fx, const int fy,\n const int gx, const int gy) {\n int ans = 999999;\n queue<aa> que;\n vector<vector<int>> memo(field.size(), vector<int>(field[0].size(), 999999));\n que.push(aa{fx, fy, 0});\n while (!que.empty()) {\n aa atop(que.front());\n que.pop();\n if (atop.x == gx && atop.y == gy) {\n ans = atop.turn;\n break;\n }\n for (int i = 0; i < 4; ++i) {\n const int nextx = atop.x + dx[i];\n const int nexty = atop.y + dy[i];\n if (!field[nexty][nextx]) {\n if (atop.turn + 1 < memo[nexty][nextx]) {\n memo[nexty][nextx] = atop.turn + 1;\n que.push(aa{nextx, nexty, atop.turn + 1});\n }\n }\n }\n }\n return ans;\n}\n\nstruct bb {\n int kx;\n int ky;\n int opway;\n int turn;\n};\n\nclass Compare {\npublic:\n bool operator()(const bb &l, const bb &r) { return l.turn > r.turn; }\n};\n\nint memo2[60][60][4];\n\nint main() {\n ios_base::sync_with_stdio(false);\n cin.tie(NULL);\n cout.tie(NULL);\n while (true) {\n for (int i = 0; i < 60; ++i) {\n for (int j = 0; j < 60; ++j) {\n for (int k = 0; k < 4; ++k) {\n memo2[i][j][k] = 999999;\n }\n }\n }\n int H, W;\n cin >> H >> W;\n if (!H) break;\n vector<vector<int>> field(H + 2, vector<int>(W + 2, true));\n priority_queue<bb, vector<bb>, Compare> que;\n {\n int kx = 99, ky = 99;\n int ox[2];\n int oy[2];\n int num = 0;\n for (int i = 0; i < H; ++i) {\n string st;\n cin >> st;\n for (int j = 0; j < W; ++j) {\n if (st[j] == 'X') {\n kx = min(kx, j + 1);\n ky = min(ky, i + 1);\n field[i + 1][j + 1] = false;\n } else if (st[j] == 'o') {\n field[i + 1][j + 1] = false;\n } else if (st[j] == '.') {\n field[i + 1][j + 1] = false;\n ox[num] = j + 1;\n oy[num] = i + 1;\n num++;\n }\n }\n }\n if (kx == 1 && ky == 1) {\n cout << 0 << endl;\n continue;\n }\n for (int way = 0; way < 4; ++way) {\n int amin = 999999;\n bool ok = true;\n for (int op = 0; op < 2; ++op) {\n int atime = 0;\n for (int lu = 0; lu < 2; ++lu) {\n const int fx = ox[op ^ lu];\n const int fy = oy[op ^ lu];\n int gx = kx + dx2[way];\n int gy = ky + dy2[way];\n if (lu == 1) {\n gx += dx[(way + 1) % 4];\n gy += dy[(way + 1) % 4];\n }\n if (field[gy][gx])\n ok = false;\n for (int ax = 0; ax < 2; ++ax) {\n for (int ay = 0; ay < 2; ++ay) {\n field[ky + ay][kx + ax] = true;\n }\n }\n atime += getdis(field, fx, fy, gx, gy);\n for (int ax = 0; ax < 2; ++ax) {\n for (int ay = 0; ay < 2; ++ay) {\n field[ky + ay][kx + ax] = false;\n }\n }\n }\n amin = min(amin, atime);\n }\n if (!ok)\n continue;\n else {\n memo2[kx][ky][way] = amin;\n que.push(bb{kx, ky, way, amin});\n }\n }\n }\n int ans = -1;\n while (!que.empty()) {\n bb atop(que.top());\n que.pop();\n const int fkx = atop.kx;\n const int fky = atop.ky;\n const int fway = atop.opway;\n const int fturn = atop.turn;\n if (fkx == 1 && fky == 1) {\n ans = fturn;\n break;\n }\n for (int tway = 0; tway < 4; ++tway) {\n if (fway == tway)\n continue;\n int amin = 999999;\n bool ok = true;\n for (int op = 0; op < 2; ++op) {\n int atime = 0;\n for (int lu = 0; lu < 2; ++lu) {\n int fx = fkx + dx2[fway];\n int fy = fky + dy2[fway];\n int gx = fkx + dx2[tway];\n int gy = fky + dy2[tway];\n if (lu == 1) {\n gx += dx[(tway + 1) % 4];\n gy += dy[(tway + 1) % 4];\n }\n if (op ^ lu) {\n fx += dx[(fway + 1) % 4];\n fy += dy[(fway + 1) % 4];\n }\n if (field[gy][gx])\n ok = false;\n for (int ax = 0; ax < 2; ++ax) {\n for (int ay = 0; ay < 2; ++ay) {\n field[fky + ay][fkx + ax] = true;\n }\n }\n atime += getdis(field, fx, fy, gx, gy);\n for (int ax = 0; ax < 2; ++ax) {\n for (int ay = 0; ay < 2; ++ay) {\n field[fky + ay][fkx + ax] = false;\n }\n }\n }\n amin = min(amin, atime);\n }\n if (!ok)\n continue;\n else {\n if (memo2[fkx][fky][tway] > fturn + amin) {\n memo2[fkx][fky][tway] = fturn + amin;\n que.push(bb{fkx, fky, tway, fturn + amin});\n }\n }\n }\n {\n const int nkx = fkx + dx[fway];\n const int nky = fky + dy[fway];\n const int nway = (fway + 2) % 4;\n if (memo2[nkx][nky][nway] > fturn + 1) {\n memo2[nkx][nky][nway] = fturn + 1;\n que.push(bb{nkx, nky, nway, fturn + 1});\n }\n }\n }\n cout << ans << endl;\n }\n return 0;\n}", "accuracy": 1, "time_ms": 1070, "memory_kb": 3216, "score_of_the_acc": -0.278, "final_rank": 2 }, { "submission_id": "aoj_1329_8113070", "code_snippet": "#include \"bits/stdc++.h\"\nusing namespace std;\nconst int dx[4] = {-1, 0, 1, 0};\nconst int dy[4] = {0, 1, 0, -1};\nconst int dx2[4] = {-1, 0, 2, 1};\nconst int dy2[4] = {0, 2, 1, -1};\nstruct aa {\n int x;\n int y;\n int turn;\n};\nint getdis(const vector<vector<int>> &field, const int fx, const int fy,\n const int gx, const int gy) {\n int ans = 999999;\n queue<aa> que;\n vector<vector<int>> memo(field.size(), vector<int>(field[0].size(), 999999));\n que.push(aa{fx, fy, 0});\n while (!que.empty()) {\n aa atop(que.front());\n que.pop();\n if (atop.x == gx && atop.y == gy) {\n ans = atop.turn;\n break;\n }\n for (int i = 0; i < 4; ++i) {\n const int nextx = atop.x + dx[i];\n const int nexty = atop.y + dy[i];\n if (!field[nexty][nextx]) {\n if (atop.turn + 1 < memo[nexty][nextx]) {\n memo[nexty][nextx] = atop.turn + 1;\n que.push(aa{nextx, nexty, atop.turn + 1});\n }\n }\n }\n }\n return ans;\n}\nstruct bb {\n int kx;\n int ky;\n int opway;\n int turn;\n};\nclass Compare {\npublic:\n bool operator()(const bb &l, const bb &r) { return l.turn > r.turn; }\n};\nint memo2[60][60][4];\nint main() {\n ios_base::sync_with_stdio(false);\n cin.tie(0);\n while (1) {\n for (int i = 0; i < 60; ++i) {\n for (int j = 0; j < 60; ++j) {\n for (int k = 0; k < 4; ++k) {\n memo2[i][j][k] = 999999;\n }\n }\n }\n int H, W;\n cin >> H >> W;\n if (!H)\n break;\n vector<vector<int>> field(H + 2, vector<int>(W + 2, true));\n priority_queue<bb, vector<bb>, Compare> que;\n {\n int kx = 99, ky = 99;\n int ox[2];\n int oy[2];\n int num = 0;\n for (int i = 0; i < H; ++i) {\n string st;\n cin >> st;\n for (int j = 0; j < W; ++j) {\n if (st[j] == 'X') {\n kx = min(kx, j + 1);\n ky = min(ky, i + 1);\n field[i + 1][j + 1] = false;\n } else if (st[j] == 'o') {\n field[i + 1][j + 1] = false;\n } else if (st[j] == '.') {\n field[i + 1][j + 1] = false;\n ox[num] = j + 1;\n oy[num] = i + 1;\n num++;\n }\n }\n }\n if (kx == 1 && ky == 1) {\n cout << 0 << endl;\n continue;\n }\n for (int way = 0; way < 4; ++way) {\n int amin = 999999;\n bool ok = true;\n for (int op = 0; op < 2; ++op) {\n int atime = 0;\n for (int lu = 0; lu < 2; ++lu) {\n const int fx = ox[op ^ lu];\n const int fy = oy[op ^ lu];\n int gx = kx + dx2[way];\n int gy = ky + dy2[way];\n if (lu == 1) {\n gx += dx[(way + 1) % 4];\n gy += dy[(way + 1) % 4];\n }\n if (field[gy][gx])\n ok = false;\n for (int ax = 0; ax < 2; ++ax) {\n for (int ay = 0; ay < 2; ++ay) {\n field[ky + ay][kx + ax] = true;\n }\n }\n atime += getdis(field, fx, fy, gx, gy);\n for (int ax = 0; ax < 2; ++ax) {\n for (int ay = 0; ay < 2; ++ay) {\n field[ky + ay][kx + ax] = false;\n }\n }\n }\n amin = min(amin, atime);\n }\n if (!ok)\n continue;\n else {\n memo2[kx][ky][way] = amin;\n que.push(bb{kx, ky, way, amin});\n }\n }\n }\n int ans = -1;\n while (!que.empty()) {\n bb atop(que.top());\n que.pop();\n const int fkx = atop.kx;\n const int fky = atop.ky;\n const int fway = atop.opway;\n const int fturn = atop.turn;\n if (fkx == 1 && fky == 1) {\n ans = fturn;\n break;\n }\n for (int tway = 0; tway < 4; ++tway) {\n if (fway == tway)\n continue;\n int amin = 999999;\n bool ok = true;\n for (int op = 0; op < 2; ++op) {\n int atime = 0;\n for (int lu = 0; lu < 2; ++lu) {\n int fx = fkx + dx2[fway];\n int fy = fky + dy2[fway];\n int gx = fkx + dx2[tway];\n int gy = fky + dy2[tway];\n if (lu == 1) {\n gx += dx[(tway + 1) % 4];\n gy += dy[(tway + 1) % 4];\n }\n if (op ^ lu) {\n fx += dx[(fway + 1) % 4];\n fy += dy[(fway + 1) % 4];\n }\n if (field[gy][gx])\n ok = false;\n for (int ax = 0; ax < 2; ++ax) {\n for (int ay = 0; ay < 2; ++ay) {\n field[fky + ay][fkx + ax] = true;\n }\n }\n atime += getdis(field, fx, fy, gx, gy);\n for (int ax = 0; ax < 2; ++ax) {\n for (int ay = 0; ay < 2; ++ay) {\n field[fky + ay][fkx + ax] = false;\n }\n }\n }\n amin = min(amin, atime);\n }\n if (!ok)\n continue;\n else {\n if (memo2[fkx][fky][tway] > fturn + amin) {\n memo2[fkx][fky][tway] = fturn + amin;\n que.push(bb{fkx, fky, tway, fturn + amin});\n }\n }\n }\n {\n const int nkx = fkx + dx[fway];\n const int nky = fky + dy[fway];\n const int nway = (fway + 2) % 4;\n if (memo2[nkx][nky][nway] > fturn + 1) {\n memo2[nkx][nky][nway] = fturn + 1;\n que.push(bb{nkx, nky, nway, fturn + 1});\n }\n }\n }\n cout << ans << endl;\n }\n return 0;\n}", "accuracy": 1, "time_ms": 1090, "memory_kb": 3540, "score_of_the_acc": -0.8975, "final_rank": 11 }, { "submission_id": "aoj_1329_8113068", "code_snippet": "#include \"bits/stdc++.h\"\nusing namespace std;\nconst int dx[4] = {-1, 0, 1, 0};\nconst int dy[4] = {0, 1, 0, -1};\nconst int dx2[4] = {-1, 0, 2, 1};\nconst int dy2[4] = {0, 2, 1, -1};\nstruct aa {\n int x;\n int y;\n int turn;\n};\nint getdis(const vector<vector<int>> &field, const int fx, const int fy,\n const int gx, const int gy) {\n int ans = 999999;\n queue<aa> que;\n vector<vector<int>> memo(field.size(), vector<int>(field[0].size(), 999999));\n que.push(aa{fx, fy, 0});\n while (!que.empty()) {\n aa atop(que.front());\n que.pop();\n if (atop.x == gx && atop.y == gy) {\n ans = atop.turn;\n break;\n }\n for (int i = 0; i < 4; ++i) {\n const int nextx = atop.x + dx[i];\n const int nexty = atop.y + dy[i];\n if (!field[nexty][nextx]) {\n if (atop.turn + 1 < memo[nexty][nextx]) {\n memo[nexty][nextx] = atop.turn + 1;\n que.push(aa{nextx, nexty, atop.turn + 1});\n }\n }\n }\n }\n return ans;\n}\nstruct bb {\n int kx;\n int ky;\n int opway;\n int turn;\n};\nclass Compare {\npublic:\n bool operator()(const bb &l, const bb &r) { return l.turn > r.turn; }\n};\nint memo2[60][60][4];\nint main() {\n ios_base::sync_with_stdio(false);\n cin.tie(NULL);\n while (1) {\n for (int i = 0; i < 60; ++i) {\n for (int j = 0; j < 60; ++j) {\n for (int k = 0; k < 4; ++k) {\n memo2[i][j][k] = 999999;\n }\n }\n }\n int H, W;\n cin >> H >> W;\n if (!H)\n break;\n vector<vector<int>> field(H + 2, vector<int>(W + 2, true));\n priority_queue<bb, vector<bb>, Compare> que;\n {\n int kx = 99, ky = 99;\n int ox[2];\n int oy[2];\n int num = 0;\n for (int i = 0; i < H; ++i) {\n string st;\n cin >> st;\n for (int j = 0; j < W; ++j) {\n if (st[j] == 'X') {\n kx = min(kx, j + 1);\n ky = min(ky, i + 1);\n field[i + 1][j + 1] = false;\n } else if (st[j] == 'o') {\n field[i + 1][j + 1] = false;\n } else if (st[j] == '.') {\n field[i + 1][j + 1] = false;\n ox[num] = j + 1;\n oy[num] = i + 1;\n num++;\n }\n }\n }\n if (kx == 1 && ky == 1) {\n cout << 0 << endl;\n continue;\n }\n for (int way = 0; way < 4; ++way) {\n int amin = 999999;\n bool ok = true;\n for (int op = 0; op < 2; ++op) {\n int atime = 0;\n for (int lu = 0; lu < 2; ++lu) {\n const int fx = ox[op ^ lu];\n const int fy = oy[op ^ lu];\n int gx = kx + dx2[way];\n int gy = ky + dy2[way];\n if (lu == 1) {\n gx += dx[(way + 1) % 4];\n gy += dy[(way + 1) % 4];\n }\n if (field[gy][gx])\n ok = false;\n for (int ax = 0; ax < 2; ++ax) {\n for (int ay = 0; ay < 2; ++ay) {\n field[ky + ay][kx + ax] = true;\n }\n }\n atime += getdis(field, fx, fy, gx, gy);\n for (int ax = 0; ax < 2; ++ax) {\n for (int ay = 0; ay < 2; ++ay) {\n field[ky + ay][kx + ax] = false;\n }\n }\n }\n amin = min(amin, atime);\n }\n if (!ok)\n continue;\n else {\n memo2[kx][ky][way] = amin;\n que.push(bb{kx, ky, way, amin});\n }\n }\n }\n int ans = -1;\n while (!que.empty()) {\n bb atop(que.top());\n que.pop();\n const int fkx = atop.kx;\n const int fky = atop.ky;\n const int fway = atop.opway;\n const int fturn = atop.turn;\n if (fkx == 1 && fky == 1) {\n ans = fturn;\n break;\n }\n for (int tway = 0; tway < 4; ++tway) {\n if (fway == tway)\n continue;\n int amin = 999999;\n bool ok = true;\n for (int op = 0; op < 2; ++op) {\n int atime = 0;\n for (int lu = 0; lu < 2; ++lu) {\n int fx = fkx + dx2[fway];\n int fy = fky + dy2[fway];\n int gx = fkx + dx2[tway];\n int gy = fky + dy2[tway];\n if (lu == 1) {\n gx += dx[(tway + 1) % 4];\n gy += dy[(tway + 1) % 4];\n }\n if (op ^ lu) {\n fx += dx[(fway + 1) % 4];\n fy += dy[(fway + 1) % 4];\n }\n if (field[gy][gx])\n ok = false;\n for (int ax = 0; ax < 2; ++ax) {\n for (int ay = 0; ay < 2; ++ay) {\n field[fky + ay][fkx + ax] = true;\n }\n }\n atime += getdis(field, fx, fy, gx, gy);\n for (int ax = 0; ax < 2; ++ax) {\n for (int ay = 0; ay < 2; ++ay) {\n field[fky + ay][fkx + ax] = false;\n }\n }\n }\n amin = min(amin, atime);\n }\n if (!ok)\n continue;\n else {\n if (memo2[fkx][fky][tway] > fturn + amin) {\n memo2[fkx][fky][tway] = fturn + amin;\n que.push(bb{fkx, fky, tway, fturn + amin});\n }\n }\n }\n {\n const int nkx = fkx + dx[fway];\n const int nky = fky + dy[fway];\n const int nway = (fway + 2) % 4;\n if (memo2[nkx][nky][nway] > fturn + 1) {\n memo2[nkx][nky][nway] = fturn + 1;\n que.push(bb{nkx, nky, nway, fturn + 1});\n }\n }\n }\n cout << ans << endl;\n }\n return 0;\n}", "accuracy": 1, "time_ms": 1100, "memory_kb": 3536, "score_of_the_acc": -0.8975, "final_rank": 12 } ]
aoj_1338_cpp
Problem D: Clock Hands We have an analog clock whose three hands (the second hand, the minute hand and the hour hand) rotate quite smoothly. You can measure two angles between the second hand and two other hands. Write a program to find the time at which "No two hands overlap each other" and "Two angles between the second hand and two other hands are equal" for the first time on or after a given time. Figure D.1. Angles between the second hand and two other hands Clocks are not limited to 12-hour clocks. The hour hand of an H -hour clock goes around once in H hours. The minute hand still goes around once every hour, and the second hand goes around once every minute. At 0:0:0 (midnight), all the hands are at the upright position. Input The input consists of multiple datasets. Each of the dataset has four integers H , h , m and s in one line, separated by a space. H means that the clock is an H -hour clock. h , m and s mean hour, minute and second of the specified time, respectively. You may assume 2 ≤ H ≤ 100, 0 ≤ h < H , 0 ≤ m < 60, and 0 ≤ s < 60. The end of the input is indicated by a line containing four zeros. Figure D.2. Examples of H-hour clock (6-hour clock and 15-hour clock) Output Output the time T at which "No two hands overlap each other" and "Two angles between the second hand and two other hands are equal" for the first time on and after the specified time. For T being h o : m o : s o ( s o seconds past m o minutes past h o o'clock), output four non-negative integers h o , m o , n , and d in one line, separated by a space, where n / d is the irreducible fraction representing s o . For integer s o including 0, let d be 1. The time should be expressed in the remainder of H hours. In other words, one second after ( H − 1):59:59 is 0:0:0, not H :0:0. Sample Input 12 0 0 0 12 11 59 59 12 1 56 0 12 1 56 3 12 1 56 34 12 3 9 43 12 3 10 14 12 7 17 58 12 7 18 28 12 7 23 0 12 7 23 31 2 0 38 29 2 0 39 0 2 0 39 30 2 1 6 20 2 1 20 1 2 1 20 31 3 2 15 0 3 2 59 30 4 0 28 48 5 1 5 40 5 1 6 10 5 1 7 41 11 0 55 0 0 0 0 0 Output for the Sample Input 0 0 43200 1427 0 0 43200 1427 1 56 4080 1427 1 56 47280 1427 1 57 4860 1427 3 10 18600 1427 3 10 61800 1427 7 18 39240 1427 7 18 82440 1427 7 23 43140 1427 7 24 720 1427 0 38 4680 79 0 39 2340 79 0 40 0 1 1 6 3960 79 1 20 2400 79 1 21 60 79 2 15 0 1 0 0 2700 89 0 28 48 1 1 6 320 33 1 6 40 1 1 8 120 11 0 55 0 1
[ { "submission_id": "aoj_1338_9808827", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n#define rep(i, n) for(int i = 0; i < (n); i++)\n#define srep(i, s, t) for(int i = (s); i < (t); i++)\n#define len(x) ((int)(x).size())\n#define all(x) (x).begin(), (x).end()\ntemplate < class T > bool chmin(T& a, T b) { if(a > b) { a = b; return true; } return false; }\ntemplate < class T > bool chmax(T& a, T b) { if(a < b) { a = b; return true; } return false; }\nusing i64 = long long;\nusing f64 = long double;\ni64 floor_div(const i64 n, const i64 d) { assert(d != 0); return n / d - static_cast<i64>((n ^ d) < 0 && n % d != 0); }\ni64 ceil_div(const i64 n, const i64 d) { assert(d != 0); return n / d + static_cast<i64>((n ^ d) >= 0 && n % d != 0); }\n\n#define int i64\ntuple<int, int, int, int> solve(int H, int h, int m, int s) {\n const int D = 119 * H - 1;\n const int M = H * 60 * 60 * D;\n int d = 0;\n while(true) {\n bool ok = [&] {\n int rs = H * 60 * (s * D + d);\n int rm = H * ((m * 60 + s) * D + d);\n int rh = ((h * 60 + m) * 60 + s) * D + d;\n if(rm == rh) return false;\n return (rm - rs + M) % M == (rs - rh + M) % M;\n }();\n if(ok) {\n int u = s * D + d;\n int l = D;\n const int g = gcd(u, l);\n return {h, m, u / g, l / g};\n }\n\n d++;\n if(d == D ) s++, d=0;\n if(s == 60) m++, s=0;\n if(m == 60) h++, m=0;\n if(h == H ) h=0;\n }\n assert(0);\n}\n#undef int\n\nint main() {\n cin.tie(0)->sync_with_stdio(0);\n \n while(true) {\n int H, h, m, s; cin >> H >> h >> m >> s; if(H == 0) return 0;\n auto [ho, mo, n, d] = solve(H, h, m, s);\n cout << ho << \" \" << mo << \" \" << n << \" \" << d << \"\\n\";\n }\n}", "accuracy": 1, "time_ms": 140, "memory_kb": 3216, "score_of_the_acc": -0.1707, "final_rank": 9 }, { "submission_id": "aoj_1338_9808826", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n#define rep(i, n) for(int i = 0; i < (n); i++)\n#define srep(i, s, t) for(int i = (s); i < (t); i++)\n#define len(x) ((int)(x).size())\n#define all(x) (x).begin(), (x).end()\ntemplate < class T > bool chmin(T& a, T b) { if(a > b) { a = b; return true; } return false; }\ntemplate < class T > bool chmax(T& a, T b) { if(a < b) { a = b; return true; } return false; }\nusing i64 = long long;\nusing f64 = long double;\ni64 floor_div(const i64 n, const i64 d) { assert(d != 0); return n / d - static_cast<i64>((n ^ d) < 0 && n % d != 0); }\ni64 ceil_div(const i64 n, const i64 d) { assert(d != 0); return n / d + static_cast<i64>((n ^ d) >= 0 && n % d != 0); }\n\n#define int i64\ntuple<int, int, int, int> solve(int H, int h, int m, int s) {\n const int D = 119 * H - 1;\n const int M = H * 60 * 60 * D;\n int d = 0;\n while(true) {\n bool ok = [&] {\n int rs = H * 60 * (s * D + d);\n int rm = H * ((m * 60 + s) * D + d);\n int rh = ((h * 60 + m) * 60 + s) * D + d;\n if(rm == rh) return false;\n int dm = rm - rs; if(dm < 0) dm += M;\n int dh = rs - rh; if(dh < 0) dh += M;\n return dm == dh;\n }();\n if(ok) {\n int u = s * D + d;\n int l = D;\n const int g = gcd(u, l);\n return {h, m, u / g, l / g};\n }\n\n d++;\n if(d == D ) s++, d=0;\n if(s == 60) m++, s=0;\n if(m == 60) h++, m=0;\n if(h == H ) h=0;\n }\n assert(0);\n}\n#undef int\n\nint main() {\n cin.tie(0)->sync_with_stdio(0);\n \n while(true) {\n int H, h, m, s; cin >> H >> h >> m >> s; if(H == 0) return 0;\n auto [ho, mo, n, d] = solve(H, h, m, s);\n cout << ho << \" \" << mo << \" \" << n << \" \" << d << \"\\n\";\n }\n}", "accuracy": 1, "time_ms": 20, "memory_kb": 3252, "score_of_the_acc": -0.181, "final_rank": 11 }, { "submission_id": "aoj_1338_8505526", "code_snippet": "#include<bits/stdc++.h>\n\nusing namespace std;\n\n#define rep(i, n) for(int i = 0; i < (n); i++)\n#define per(i, n) for(int i = (n)-1; i >= 0; i--)\n#define rep2(i, l, r) for(int i = (l); i < (r); i++)\n#define per2(i, l, r) for(int i = (r)-1; i >= (l); i--)\n#define each(e, x) for(auto &e : x)\n#define sz(x) (int)x.size()\n#define all(x) begin(x), end(x)\n#define rall(x) rbegin(x), rend(x)\n#define eb emplace_back\n#define MM << ' ' <<\n#define TT template<typename T>\nusing ll = long long;\nusing pii = pair<int, int>;\nusing pll = pair<ll, ll>;\n\nTT bool chmin(T &x, T y) { return x > y ? (x = y, true) : false; }\n\nTT bool chmax(T &x, T y) { return x < y ? (x = y, true) : false; }\n\nTT using minheap = priority_queue<T, vector<T>, greater<T>>;\nTT using maxheap = priority_queue<T>;\n\nTT void print(vector<T> v, T x = 0) {\n int n = sz(v);\n rep(i, n) cout << v[i] + x << (i == n - 1 ? '\\n' : ' ');\n if (n == 0) cout << '\\n';\n}\n\nTT void printn(vector<T> v, T x = 0) {\n int n = sz(v);\n rep(i, n) cout << v[i] + x << '\\n';\n}\n\nTT void rearrange(vector<T> &v) {\n sort(all(v));\n v.erase(unique(all(v)), end(v));\n}\n\nTT int lb(vector<T> &v, T x) { return lower_bound(all(v), x) - begin(v); }\n\nTT int ub(vector<T> &v, T x) { return upper_bound(all(v), x) - begin(v); }\n\nconst int inf = (1 << 30) - 1;\nconst ll INF = (1LL << 60) - 1;\n\nstruct io_setup {\n io_setup() {\n ios_base::sync_with_stdio(false);\n cin.tie(NULL);\n cout << fixed << setprecision(15);\n }\n} io_setup;\n\nint main() {\n while (1) {\n int H, h, m, s;\n cin >> H >> h >> m >> s;\n if (!H)\n break;\n ll v = (H * 238 - 2);\n ll div = v * H * 3600;\n ll c = 0;\n while (1) {\n ll ns = (h * 3600 + m * 60 + s) * v + c;\n ll d1 = ns * (H * 60 - 1), d2 = ns * H * 59;\n d1 %= div, d2 %= div;\n chmin(d1, div - d1);\n chmin(d2, div - d2);\n if (d1 != 0 && ns * H % div != ns % div && d1 == d2) {\n ll g = gcd(ns % (v * 60), v); \n cout << ns / (v * 3600) % H << ' ' << ns / (v * 60) % 60 << ' ' << ns % (v * 60) / g << ' ' << v / g << endl;\n break;\n }\n c++;\n }\n }\n}", "accuracy": 1, "time_ms": 480, "memory_kb": 3256, "score_of_the_acc": -0.2469, "final_rank": 12 }, { "submission_id": "aoj_1338_6367691", "code_snippet": "#include \"bits/stdc++.h\"\nusing namespace std;\n\ntypedef string::const_iterator State;\n#define eps 1e-8L\n#define MAX_MOD 1000000007LL\n#define GYAKU 500000004LL\n#define MOD 998244353LL\n#define pb push_back\n#define mp make_pair\ntypedef long long ll;\ntypedef long double ld;\n#define REP(a, b) for (long long(a) = 0; (a) < (b); ++(a))\n#define ALL(x) (x).begin(), (x).end()\n\n#define int long long\n\nint H, h, m, s, d;\n\nbool check()\n{\n ll x = 60 * H * s, y = (60 * m * d + s) * H, z = 3600 * h * d + 60 * m * d + s;\n if (y == z)\n return false;\n ll dify = y - x, difz = x - z;\n if (dify < 0)\n dify += 3600 * H * d;\n if (difz < 0)\n difz += 3600 * H * d;\n return dify == difz;\n}\n\nvoid solve()\n{\n cin >> H >> h >> m >> s;\n if(H == 0)\n exit(0);\n d = 119 * H - 1;\n s *= d;\n while (true)\n {\n if (check())\n {\n cout << h << \" \" << m << \" \" << s / gcd(s, d) << \" \" << d / gcd(s, d) << endl;\n return;\n }\n s++;\n if (s / d == 60)\n s = 0, m++;\n if (m == 60)\n m = 0, h++;\n if (h == H)\n h = 0;\n }\n}\n#undef int\n\n// generated by oj-template v4.7.2\n// (https://github.com/online-judge-tools/template-generator)\nint main()\n{\n // Fasterize input/output script\n ios::sync_with_stdio(false);\n cin.tie(nullptr);\n cout << fixed << setprecision(100);\n // scanf/printf user should delete this fasterize input/output script\n\n int t = 10000;\n // cin >> t; // comment out if solving multi testcase\n for (int testCase = 1; testCase <= t; ++testCase)\n {\n solve();\n }\n return 0;\n}", "accuracy": 1, "time_ms": 90, "memory_kb": 3376, "score_of_the_acc": -0.2826, "final_rank": 13 }, { "submission_id": "aoj_1338_5918445", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\nstruct Clock {\n int h, m, n, d;\n Clock(int h_, int m_, int n_, int d_) : h(h_), m(m_), n(n_), d(d_) {\n int g = gcd(n, d);\n n /= g;\n d /= g;\n }\n bool operator<(const Clock& rhs) const {\n if (h != rhs.h) return h < rhs.h;\n if (m != rhs.m) return m < rhs.m;\n return n * rhs.d < rhs.n * d;\n }\n};\n\nbool check(int H, int h, int m, int n, int d) {\n int pos_h = (h * 60 * 60 + m * 60) * 2 * d + 2 * n;\n int pos_m = m * 60 * (2 * H) * d + (2 * H) * n;\n return pos_h != pos_m;\n}\n\nvoid solve(int H, int h, int m, int s) {\n int speed = 120 * H - (H + 1);\n vector<Clock> collision;\n for (int i = 0; i < H; i++) {\n for (int j = 0; j < 60; j++) {\n int tmp = ((H + 1) * (i * 60 * 60 + j * 60)) % (7200 * H);\n vector<int> v = {tmp, (tmp + 3600 * H) % (7200 * H)};\n if (v[0] > v[1]) swap(v[0], v[1]);\n for (int _ = 0; _ < 2; _++) {\n int pos_mid = v[_];\n if (pos_mid >= 60 * speed) continue;\n if (!check(H, i, j, pos_mid, speed)) continue;\n collision.emplace_back(i, j, pos_mid, speed);\n }\n }\n }\n auto itr = lower_bound(collision.begin(), collision.end(), Clock(h, m, s, 1));\n if (itr == collision.end()) itr = collision.begin();\n auto ans = *itr;\n cout << ans.h << ' ' << ans.m << ' ' << ans.n << ' ' << ans.d << '\\n';\n}\n\nint main() {\n cin.tie(0);\n ios::sync_with_stdio(false);\n int H, h, m, s;\n while (cin >> H >> h >> m >> s, H) solve(H, h, m, s);\n return 0;\n}", "accuracy": 1, "time_ms": 70, "memory_kb": 3520, "score_of_the_acc": -0.3867, "final_rank": 15 }, { "submission_id": "aoj_1338_5918442", "code_snippet": "#define LOCAL\n#include <bits/stdc++.h>\nusing namespace std;\n#pragma region Macros\ntypedef long long ll;\ntypedef __int128_t i128;\ntypedef unsigned int uint;\ntypedef unsigned long long ull;\n#define ALL(x) (x).begin(), (x).end()\n\ntemplate <typename T> istream& operator>>(istream& is, vector<T>& v) {\n for (T& x : v) is >> x;\n return is;\n}\ntemplate <typename T> ostream& operator<<(ostream& os, const vector<T>& v) {\n for (int i = 0; i < (int)v.size(); i++) {\n os << v[i] << (i + 1 == (int)v.size() ? \"\" : \" \");\n }\n return os;\n}\ntemplate <typename T, typename U> ostream& operator<<(ostream& os, const pair<T, U>& p) {\n os << '(' << p.first << ',' << p.second << ')';\n return os;\n}\ntemplate <typename T, typename U> ostream& operator<<(ostream& os, const map<T, U>& m) {\n os << '{';\n for (auto itr = m.begin(); itr != m.end();) {\n os << '(' << itr->first << ',' << itr->second << ')';\n if (++itr != m.end()) os << ',';\n }\n os << '}';\n return os;\n}\ntemplate <typename T, typename U> ostream& operator<<(ostream& os, const unordered_map<T, U>& m) {\n os << '{';\n for (auto itr = m.begin(); itr != m.end();) {\n os << '(' << itr->first << ',' << itr->second << ')';\n if (++itr != m.end()) os << ',';\n }\n os << '}';\n return os;\n}\ntemplate <typename T> ostream& operator<<(ostream& os, const set<T>& s) {\n os << '{';\n for (auto itr = s.begin(); itr != s.end();) {\n os << *itr;\n if (++itr != s.end()) os << ',';\n }\n os << '}';\n return os;\n}\ntemplate <typename T> ostream& operator<<(ostream& os, const multiset<T>& s) {\n os << '{';\n for (auto itr = s.begin(); itr != s.end();) {\n os << *itr;\n if (++itr != s.end()) os << ',';\n }\n os << '}';\n return os;\n}\ntemplate <typename T> ostream& operator<<(ostream& os, const unordered_set<T>& s) {\n os << '{';\n for (auto itr = s.begin(); itr != s.end();) {\n os << *itr;\n if (++itr != s.end()) os << ',';\n }\n os << '}';\n return os;\n}\ntemplate <typename T> ostream& operator<<(ostream& os, const deque<T>& v) {\n for (int i = 0; i < (int)v.size(); i++) {\n os << v[i] << (i + 1 == (int)v.size() ? \"\" : \" \");\n }\n return os;\n}\n\ntemplate <int i, typename T> void print_tuple(ostream&, const T&) {}\ntemplate <int i, typename T, typename H, class... Args> void print_tuple(ostream& os, const T& t) {\n if (i) os << ',';\n os << get<i>(t);\n print_tuple<i + 1, T, Args...>(os, t);\n}\ntemplate <typename... Args> ostream& operator<<(ostream& os, const tuple<Args...>& t) {\n os << '{';\n print_tuple<0, tuple<Args...>, Args...>(os, t);\n return os << '}';\n}\n\nvoid debug_out() { cerr << '\\n'; }\ntemplate <class Head, class... Tail> void debug_out(Head&& head, Tail&&... tail) {\n cerr << head;\n if (sizeof...(Tail) > 0) cerr << \", \";\n debug_out(move(tail)...);\n}\n#ifdef LOCAL\n#define debug(...) \\\n cerr << \" \"; \\\n cerr << #__VA_ARGS__ << \" :[\" << __LINE__ << \":\" << __FUNCTION__ << \"]\" << '\\n'; \\\n cerr << \" \"; \\\n debug_out(__VA_ARGS__)\n#else\n#define debug(...) 42\n#endif\n\ntemplate <typename T> T gcd(T x, T y) { return y != 0 ? gcd(y, x % y) : x; }\ntemplate <typename T> T lcm(T x, T y) { return x / gcd(x, y) * y; }\n\nint topbit(signed t) { return t == 0 ? -1 : 31 - __builtin_clz(t); }\nint topbit(long long t) { return t == 0 ? -1 : 63 - __builtin_clzll(t); }\nint botbit(signed a) { return a == 0 ? 32 : __builtin_ctz(a); }\nint botbit(long long a) { return a == 0 ? 64 : __builtin_ctzll(a); }\nint popcount(signed t) { return __builtin_popcount(t); }\nint popcount(long long t) { return __builtin_popcountll(t); }\nbool ispow2(int i) { return i && (i & -i) == i; }\n\ntemplate <class T> T ceil(T x, T y) {\n assert(y >= 1);\n return (x > 0 ? (x + y - 1) / y : x / y);\n}\ntemplate <class T> T floor(T x, T y) {\n assert(y >= 1);\n return (x > 0 ? x / y : (x - y + 1) / y);\n}\n\ntemplate <class T1, class T2> inline bool chmin(T1& a, T2 b) {\n if (a > b) {\n a = b;\n return true;\n }\n return false;\n}\ntemplate <class T1, class T2> inline bool chmax(T1& a, T2 b) {\n if (a < b) {\n a = b;\n return true;\n }\n return false;\n}\n#pragma endregion\n\nconst int INF = 1e9;\nconst long long IINF = 1e18;\nconst int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};\nconst char dir[4] = {'D', 'R', 'U', 'L'};\nconst long long MOD = 1000000007;\n// const long long MOD = 998244353;\n\nstruct Clock {\n int h, m, n, d;\n Clock(int h_, int m_, int n_, int d_) : h(h_), m(m_), n(n_), d(d_) {\n int g = gcd(n, d);\n n /= g;\n d /= g;\n }\n bool operator<(const Clock& rhs) const {\n if (h != rhs.h) return h < rhs.h;\n if (m != rhs.m) return m < rhs.m;\n return n * rhs.d < rhs.n * d;\n }\n};\n\nbool check(int H, int h, int m, int n, int d) {\n int pos_h = (h * 60 * 60 + m * 60) * 2 * d + 2 * n;\n int pos_m = m * 60 * (2 * H) * d + (2 * H) * n;\n return pos_h != pos_m;\n}\n\nvoid solve(int H, int h, int m, int s) {\n int speed = 120 * H - (H + 1);\n vector<Clock> collision;\n for (int i = 0; i < H; i++) {\n for (int j = 0; j < 60; j++) {\n int tmp = ((H + 1) * (i * 60 * 60 + j * 60)) % (7200 * H);\n vector<int> v = {tmp, (tmp + 3600 * H) % (7200 * H)};\n if (v[0] > v[1]) swap(v[0], v[1]);\n for (int _ = 0; _ < 2; _++) {\n int pos_mid = v[_];\n if (pos_mid >= 60 * speed) continue;\n if (!check(H, i, j, pos_mid, speed)) continue;\n collision.emplace_back(i, j, pos_mid, speed);\n }\n }\n }\n auto itr = lower_bound(collision.begin(), collision.end(), Clock(h, m, s, 1));\n if (itr == collision.end()) itr = collision.begin();\n auto ans = *itr;\n cout << ans.h << ' ' << ans.m << ' ' << ans.n << ' ' << ans.d << '\\n';\n}\n\nint main() {\n cin.tie(0);\n ios::sync_with_stdio(false);\n int H, h, m, s;\n while (cin >> H >> h >> m >> s, H) solve(H, h, m, s);\n return 0;\n}", "accuracy": 1, "time_ms": 60, "memory_kb": 3628, "score_of_the_acc": -0.4654, "final_rank": 16 }, { "submission_id": "aoj_1338_5286204", "code_snippet": "#include <bits/stdc++.h>\n#include <iostream>\n#include <limits>\n#include <numeric>\n#include <type_traits>\n#include <bitset>\n#include <map>\n#include <cassert>\n\nusing namespace std;\n\nusing ll = long long;\n#define rep(i,n,m) for(ll (i)=(n);(i)<(m);(i)++)\n#define rrep(i,n,m) for(ll (i)=(n);(i)>(m);(i)--)\n\npair<ll,ll> vmin(pair<ll,ll> x,pair<ll,ll> y){\n\n if (x.second < 0){\n x.first *= -1;\n x.second *= -1;\n }\n\n if (y.second < 0){\n y.first *= -1;\n y.second *= -1;\n }\n if (y.first < 0) {\n return x;\n }\n\n assert ( (x.first * y.second) / y.second == x.first);\n assert ( (y.first * x.second) / x.second == y.first);\n\n if (x.first * y.second > y.first * x.second){\n return y;\n }else{\n return x;\n }\n\n}\n\nll modplus(ll a, ll b) {\n a %= b;\n return (a + b) % b;\n}\n\nint main(){\n\n while (true){\n\n ll H,h,m,s;\n scanf (\"%lld%lld%lld%lld\",&H,&h,&m,&s);\n\n\n\n if ( H == 0 && h == 0 && m == 0 && s == 0 ){\n break;\n }\n\n pair<ll,ll> ans = {INT_MAX-1000,1};\n\n { // H-1\n ll mod = 3600 * H;\n ll hang = ( (3600*h + 60*m + s) * 1) % mod;\n ll mang = ( (60*m+s) * H ) % mod;\n ll sang = ( s * (60*H) ) % mod;\n ll hch = 1;\n ll mch = H;\n ll sch = 60*H;\n ll pl = H-1;\n mod *= pl;\n hang *= pl;\n mang *= pl;\n sang *= pl;\n\n rep(i,0,120*pl){\n\n if (modplus(sang-mang, mod) == modplus(sang-hang, mod) || modplus(sang-mang+sang-hang, mod) == 0){\n\n\n if ( sang % mod != mang % mod && mang % mod != hang % mod && sang % mod != hang % mod ){\n pair<ll,ll> nans = {i,pl};\n ans = vmin(ans,nans);\n }\n\n }\n sang += sch ; sang %= mod;\n mang += mch ; mang %= mod;\n hang += hch ; hang %= mod;\n }\n }\n\n\n\n { // 119H-1\n ll mod = 3600 * H;\n ll hang = ( (3600*h + 60*m + s) * 1) % mod;\n ll mang = ( (60*m+s) * H ) % mod;\n ll sang = ( s * (60*H) ) % mod;\n ll hch = 1;\n ll mch = H;\n ll sch = 60*H;\n ll pl = 119*H-1;\n mod *= pl;\n hang *= pl;\n mang *= pl;\n sang *= pl;\n\n rep(i,0,120*pl){\n\n// if (i == 43200) {\n// cout << mod << endl;\n// cout << i << \" \" << pl << \" \" << sang << \" \" << mang << \" \" << hang << endl;\n// cout << sang - mang << \" \" << sang - hang << endl;\n// }\n if ( modplus(sang-mang, mod) == modplus(sang-hang, mod) || modplus(sang-mang+sang-hang, mod) == 0){\n\n if ( (sang % mod != mang % mod && mang % mod != hang % mod && sang % mod != hang % mod) ){\n pair<ll,ll> nans = {i,pl};\n ans = vmin(ans,nans);\n }\n\n\n }\n sang += sch ; sang %= mod;\n mang += mch ; mang %= mod;\n hang += hch ; hang %= mod;\n }\n }\n\n ll su = s*ans.second + ans.first;\n ll sd = ans.second;\n\n //cerr << ans.first << \" \" << ans.second << endl;\n\n ll plusm = (su/sd)/60;\n su -= plusm * 60 * sd;\n\n m += plusm;\n\n\n while (m >= 60){\n m -= 60;\n h += 1;\n }\n h %= H;\n\n ll g = __gcd(su,sd);\n su /= g;\n sd /= g;\n\n assert (INT_MAX-1000 >= ans.first);\n printf (\"%lld %lld %lld %lld\\n\",h,m,su,sd);\n\n }\n\n\n}", "accuracy": 1, "time_ms": 7330, "memory_kb": 3496, "score_of_the_acc": -1.362, "final_rank": 20 }, { "submission_id": "aoj_1338_5270985", "code_snippet": "#include <bits/stdc++.h>\n\nusing namespace std;\n\ntypedef long long ll;\ntypedef pair<int, int> P;\n\n#define eps 1e-9\n#define INF 2000000000 // 2e9\n#define LLINF 2000000000000000000ll // 2e18 (llmax:9e18)\n#define all(x) (x).begin(), (x).end()\n#define sq(x) ((x) * (x))\n\n#define rep(i, x) for (int i = 0; i < (int)(x); i++)\n#define drep(i, x) for (int i = ((int)(x)-1); i >= 0; i--)\n\n#define popcount __builtin_popcount\n#define next __next\n#define prev __prev\n\n#ifndef LOCAL\n#define dmp(...) ;\n#else\n#define dmp(...) \\\n cerr << \"[ \" << #__VA_ARGS__ << \" ] : \" << dump_str(__VA_ARGS__) << endl\n#endif\n\n// ---------------- Utility ------------------\n\ntemplate <class T>\nbool chmin(T &a, const T &b) {\n if (a <= b) return false;\n a = b;\n return true;\n}\ntemplate <class T>\nbool chmax(T &a, const T &b) {\n if (a >= b) return false;\n a = b;\n return true;\n}\n\ntemplate <class T>\nusing MaxHeap = priority_queue<T>;\ntemplate <class T>\nusing MinHeap = priority_queue<T, vector<T>, greater<T>>;\n\ntemplate <class T>\nvector<T> vect(int len, T elem) {\n return vector<T>(len, elem);\n}\n\n// ----------------- Input -------------------\n\ntemplate <class T, class U>\nistream &operator>>(istream &is, pair<T, U> &p) {\n return is >> p.first >> p.second;\n}\n\ntemplate <class T>\nistream &operator>>(istream &is, vector<T> &vec) {\n for (int i = 0; i < vec.size(); i++) is >> vec[i];\n return is;\n}\n\n// ----------------- Output ------------------\n\ntemplate <class T, class U>\nostream &operator<<(ostream &os, const pair<T, U> &p) {\n return os << p.first << ',' << p.second;\n}\n\ntemplate <class T>\nostream &operator<<(ostream &os, const vector<T> &v) {\n for (const T &e : v) os << e << \" \";\n return os;\n}\n\ntemplate <class T>\nostream &operator<<(ostream &os, const deque<T> &d) {\n for (const T &e : d) os << e << \" \";\n return os;\n}\n\ntemplate <class T>\nostream &operator<<(ostream &os, const set<T> &s) {\n os << \"{ \";\n for (const T &e : s) os << e << \" \";\n return os << \"}\";\n}\n\ntemplate <class T, class U>\nostream &operator<<(ostream &os, const map<T, U> &m) {\n os << \"{ \";\n for (const auto &[key, val] : m) os << \"( \" << key << \" -> \" << val << \" ) \";\n return os << \"}\";\n}\n\ntemplate <class TupleTy, size_t... I>\nvoid dump_tuple(ostream &os, const TupleTy t, std::index_sequence<I...>) {\n (..., (os << (I == 0 ? \"\" : \",\") << std::get<I>(t)));\n}\n\ntemplate <class... Args>\nostream &operator<<(ostream &os, const tuple<Args...> &t) {\n os << \"(\";\n dump_tuple(os, t, std::make_index_sequence<sizeof...(Args)>());\n return os << \")\";\n}\n\nvoid dump_str_rec(ostringstream &) {}\n\ntemplate <class Head, class... Tail>\nvoid dump_str_rec(ostringstream &oss, Head &&head, Tail &&... tail) {\n oss << \", \" << head;\n dump_str_rec(oss, forward<Tail>(tail)...);\n}\n\ntemplate <class T, class... U>\nstring dump_str(T &&arg, U &&... args) {\n ostringstream oss;\n oss << arg;\n dump_str_rec(oss, forward<U>(args)...);\n return oss.str();\n}\n\n// --------------- Fast I/O ------------------\n\nvoid fastio() {\n cin.tie(0);\n ios::sync_with_stdio(0);\n cout << fixed << setprecision(20);\n}\n\n// ------------ End of template --------------\n\n#define endl \"\\n\"\n\nbool solve() {\n ll H, h0, m0, s0;\n cin >> H >> h0 >> m0 >> s0;\n if ((H | h0 | m0 | s0) == 0) return false;\n\n ll X = 119 * H - 1;\n ll T = (60ll * 60 * h0 + 60 * m0 + s0) * X;\n ll round = X * 60ll * 60 * H;\n\n auto check = [&](ll t) {\n ll x = t % round;\n ll y = (t * H) % round;\n ll z = (t * 60 * H) % round;\n if (x == y) return false;\n if ((x + y) % round == (z * 2 % round)) return true;\n return false;\n };\n\n while (!check(T)) { T++; }\n\n dmp(T);\n\n ll h = (T / (X * 60 * 60)) % H;\n ll m = (T / (X * 60)) % 60;\n ll n = T % (X * 60);\n ll d = X;\n\n ll g = gcd(n, d);\n n /= g;\n d /= g;\n\n cout << h << ' ' << m << ' ' << n << ' ' << d << endl;\n\n return true;\n}\n\nint main() {\n fastio();\n while (solve()) {}\n // int t;\n // cin >> t;\n // while (t--) solve();\n\n // int t; cin >> t;\n // for(int i=1;i<=t;i++){\n // cout << \"Case #\" << i << \": \";\n // solve();\n // }\n return 0;\n}", "accuracy": 1, "time_ms": 280, "memory_kb": 3376, "score_of_the_acc": -0.3086, "final_rank": 14 }, { "submission_id": "aoj_1338_5012062", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\nusing ll = long long;\nusing PII = pair<ll, ll>;\n\n#define FOR(i, a, n) for (ll i = (ll)a; i < (ll)n; ++i)\n#define REP(i, n) FOR(i, 0, n)\n#define ALL(x) x.begin(), x.end()\ntemplate <typename T> void chmin(T &a, const T &b) { a = min(a, b); }\ntemplate <typename T> void chmax(T &a, const T &b) { a = max(a, b); }\n\nconst ll INF = 1LL << 60;\n\nstruct FastIO {\n FastIO() {\n cin.tie(0);\n ios::sync_with_stdio(0);\n }\n} fastiofastio;\n\n// BEGIN CUT\n// 分数ライブラリ\n// 常に約分されているとする\n// 負のときは常にaを負にする\nstruct fraction {\n ll a, b;\n fraction(ll x = 0, ll y = 1) : a(x), b(y) {\n ll g = __gcd(a, b);\n a /= g, b /= g;\n if (b < 0)\n a *= -1, b *= -1;\n }\n // comparator\n bool operator<(fraction r) const { return a * r.b < b * r.a; }\n bool operator>(fraction r) const { return a * r.b > b * r.a; }\n bool operator<=(fraction r) const { return a * r.b <= b * r.a; }\n bool operator>=(fraction r) const { return a * r.b >= b * r.a; }\n bool operator==(fraction r) const { return a * r.b == b * r.a; }\n bool operator!=(fraction r) const { return a * r.b != b * r.a; }\n // Basic Operations\n fraction operator+(fraction r) const { return fraction(*this) += r; }\n fraction operator-(fraction r) const { return fraction(*this) -= r; }\n fraction operator*(fraction r) const { return fraction(*this) *= r; }\n fraction operator/(fraction r) const { return fraction(*this) /= r; }\n fraction &operator+=(fraction r) {\n ll g = __gcd(abs(a * r.b + b * r.a), b * r.b);\n ll na = (a * r.b + b * r.a) / g, nb = (b * r.b) / g;\n a = na, b = nb;\n return *this;\n }\n fraction &operator-=(fraction r) {\n ll g = __gcd(abs(a * r.b - b * r.a), b * r.b);\n ll na = (a * r.b - b * r.a) / g, nb = (b * r.b) / g;\n a = na, b = nb;\n return *this;\n }\n fraction &operator*=(fraction r) {\n ll g = __gcd(a * r.a, b * r.b);\n a = a * r.a / g, b = b * r.b / g;\n return *this;\n }\n fraction &operator/=(fraction r) {\n ll g = __gcd(a * r.b, b * r.a);\n a = a * r.b / g, b = b * r.a / g;\n if (b < 0)\n a *= -1, b *= -1;\n return *this;\n }\n friend fraction abs(fraction a) {\n a.a = abs(a.a);\n return a;\n }\n // output\n friend ostream &operator<<(ostream &os, fraction a) {\n return os << a.a << \"/\" << a.b;\n }\n};\n// END CUT\n\nusing P = pair<fraction, bool>;\n\nint main() {\n while (1) {\n int H, h, m, s;\n cin >> H >> h >> m >> s;\n if (H == 0)\n break;\n int day = H * 3600;\n fraction vh = fraction(1, 10 * H);\n fraction vm = fraction(1, 10);\n fraction vs = fraction(6);\n vector<P> vec;\n //秒針と分針\n auto diff = fraction(m * 60 + s, 10) - fraction(s * 6);\n if (diff <= 0)\n diff += 360;\n auto t = diff / (vs - vm);\n while (t <= day) {\n vec.emplace_back(t, 0);\n t += fraction(360) / (vs - vm);\n }\n diff += 180;\n if (diff > 360)\n diff -= 360;\n t = diff / (vs - vm);\n while (t <= day) {\n vec.emplace_back(t, 0);\n t += fraction(360) / (vs - vm);\n }\n //秒針と時針\n diff = fraction(h * 3600 + m * 60 + s, 10 * H) - fraction(s * 6);\n if (diff <= 0)\n diff += 360;\n t = diff / (vs - vh);\n while (t <= day) {\n vec.emplace_back(t, 1);\n t += fraction(360) / (vs - vh);\n }\n diff += 180;\n if (diff > 360)\n diff -= 360;\n t = diff / (vs - vh);\n while (t <= day) {\n vec.emplace_back(t, 1);\n t += fraction(360) / (vs - vh);\n }\n sort(ALL(vec));\n auto diff_m = fraction(m * 60 + s, 10) - fraction(s * 6);\n vm = vs - vm;\n if (diff_m < 0)\n diff_m += 360;\n if (diff_m > 180) {\n diff_m = fraction(360) - diff_m;\n } else {\n vm *= -1;\n }\n auto diff_h = fraction(h * 3600 + m * 60 + s, 10 * H) - fraction(s * 6);\n vh = vs - vh;\n if (diff_h < 0)\n diff_h += 360;\n if (diff_h > 180) {\n diff_h = fraction(360) - diff_h;\n } else {\n vh *= -1;\n }\n fraction prev = 0;\n fraction ans;\n for (P p : vec) {\n auto T = p.first - prev;\n if ((vm < 0 && vh > 0) || (vm > 0 && vh < 0)) {\n auto x = (diff_m - diff_h) / (vh - vm);\n if (diff_m + vm * x != 0 && x >= 0 && x <= T) {\n ans = prev + x;\n break;\n }\n }\n diff_m += vm * T;\n diff_h += vh * T;\n if (p.second)\n vh *= -1;\n else\n vm *= -1;\n prev = p.first;\n }\n ans += 3600 * h + 60 * m + s;\n if (ans >= day)\n ans -= day;\n ll sec = ans.a / ans.b;\n ans -= sec;\n ans += sec % 60;\n cout << sec / 3600 << \" \" << (sec % 3600) / 60 << \" \" << ans.a << \" \"\n << ans.b << endl;\n }\n}", "accuracy": 1, "time_ms": 410, "memory_kb": 4356, "score_of_the_acc": -1.0534, "final_rank": 18 }, { "submission_id": "aoj_1338_4918624", "code_snippet": "#include \"iostream\"\n#include \"climits\"\n#include \"list\"\n#include \"queue\"\n#include \"stack\"\n#include \"set\"\n#include \"functional\"\n#include \"algorithm\"\n#include \"string\"\n#include \"map\"\n#include \"unordered_map\"\n#include \"unordered_set\"\n#include \"iomanip\"\n#include \"cmath\"\n#include \"random\"\n#include \"bitset\"\n#include \"cstdio\"\n#include \"numeric\"\n#include \"cassert\"\n#include \"ctime\"\n\nusing namespace std;\n\nconstexpr long long int MOD = 1000000007;\n//constexpr int MOD = 1000000007;\n//constexpr int MOD = 998244353;\n//constexpr long long int MOD = 998244353;\nconstexpr double EPS = 1e-12;\n\n//int N, M, K, T, H, W, L, R;\nlong long int N, M, K, T, H, W, L, R;\n\nlong long int dif(long long int a, long long int b, long long int mx) {\n\treturn min(abs(a - b), mx - abs(a - b));\n}\n\nlong long int gcd(long long int a, long long int b) {\n\twhile (b) {\n\t\ta %= b;\n\t\tswap(a, b);\n\t}\n\treturn a;\n}\n\nvoid Output(long long int p) {\n\tcout << p / (119 * H - 1)/3600 << \" \";\n\tp %= (119 * H - 1) * 3600;\n\tcout << p / (119 * H - 1) / 60 << \" \";\n\tp %= (119 * H - 1) * 60;\n\tlong long int g = gcd(p, 119 * H - 1);\n\tcout << p / g << \" \" << (119 * H - 1) / g << endl;\n}\n\nint main() {\n\tios::sync_with_stdio(false);\n\tcin.tie(0);\n\n\tint h, m, s;\n\twhile (cin >> H >> h >> m >> s, H) {\n\t\tint by = 119 * H - 1;\n\t\tlong long int st = h * 3600 + m * 60 + s;\n\t\tlong long int mod = by * H * 60 * 60;\n\t\tst *= by;\n\t\tlong long int hp = st;\n\t\tlong long int mp = hp * H;\n\t\tlong long int sp = mp * 60;\n\t\tmp %= mod;\n\t\tsp %= mod;\n\t\tdo {\n\t\t//\tcout << st << \" \" << hp << \" \" << mp << \" \" << sp << endl;\n\t\t\tif (dif(hp, sp, mod) == dif(mp, sp, mod) && dif(hp, mp, mod)) {\n\t\t\t\tOutput(st);\n\t\t\t\tbreak;\n\t\t\t}\n\t\t\tst++;\n\t\t\tst %= mod;\n\t\t\thp++;\n\t\t\thp %= mod;\n\t\t\tmp += H;\n\t\t\tmp %= mod;\n\t\t\tsp += 60 * H;\n\t\t\tsp %= mod;\n\t\t} while (1);\n\t}\n}", "accuracy": 1, "time_ms": 210, "memory_kb": 3096, "score_of_the_acc": -0.0913, "final_rank": 3 }, { "submission_id": "aoj_1338_4894664", "code_snippet": "// verification-helper: PROBLEM http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=1338\n\n#include <bits/stdc++.h>\nusing namespace std;\n\n#define call_from_test\n#ifndef call_from_test\n#include <bits/stdc++.h>\nusing namespace std;\n#endif\n//BEGIN CUT HERE\ntemplate<typename T>\nstruct fraction{\n T num,dom;\n fraction(T n,T d):num(n),dom(d){\n assert(dom!=0);\n if(dom<0) num*=-1,dom*=-1;\n T tmp=__gcd(abs(num),abs(dom));\n num/=tmp;\n dom/=tmp;\n }\n fraction operator+(const fraction a) const{\n return fraction(num*a.dom+a.num*dom,dom*a.dom);\n }\n fraction operator-(const fraction a) const{\n return fraction(num*a.dom-a.num*dom,dom*a.dom);\n }\n fraction operator*(const fraction a) const{\n return fraction(num*a.num,dom*a.dom);\n }\n fraction operator/(const fraction a){\n return fraction(num*a.dom,dom*a.num);\n }\n fraction operator*(T k) const{return fraction(num*k,dom);}\n fraction operator/(T k) const{return fraction(num,dom*k);}\n#define define_cmp(op) \\\n bool operator op (const fraction a)const{return num*a.dom op a.num*dom;}\n define_cmp(==)\n define_cmp(!=)\n define_cmp(<)\n define_cmp(>)\n define_cmp(<=)\n define_cmp(>=)\n#undef define_cmp\n};\n//END CUT HERE\n#ifndef call_from_test\n//INSERT ABOVE HERE\nsigned main(){\n return 0;\n}\n#endif\n\n#undef call_from_test\n\nusing ll = long long;\nusing frac = fraction<ll>;\n\nint H,h,m,s;\nvoid print(frac f){\n int t=f.num/(f.dom*60);\n cout<<(t%(60*H))/60<<\" \";\n cout<<(t%60)<<\" \";\n cout<<(f.num)%(f.dom*60)<<\" \"<<f.dom<<endl;\n}\n\nfrac norm2(frac a){\n if(a.num==0) return frac(0,1);\n while(a.num<0) a.num+=a.dom;\n while(a.num>=a.dom) a.num-=a.dom;\n ll tmp=__gcd(a.num,a.dom);\n return frac(a.num/tmp,a.dom/tmp);\n}\n\nsigned main(){\n cin.tie(0);\n ios::sync_with_stdio(0);\n\n while(cin>>H>>h>>m>>s,H){\n const frac base(h*3600+m*60+s,1);\n const frac vh(1,3600*H),vm(1,3600),vs(1,60);\n frac t(max(h*3600+m*60+s-100,0),1);\n while(1){\n frac x=norm2(t*vh);\n frac y=norm2(t*vm);\n frac z=norm2(t*vs);\n if(y<x) y=y+frac(1,1);\n if(z<x) z=z+frac(1,1);\n frac ans(10000000,1);\n {\n frac t1=frac(1,1)-(z-x);\n frac t2=z-y;\n frac tmp=t+(t1-t2)/(vs*2-(vh+vm));\n if(base<=tmp){\n frac a=norm2(tmp*vh);\n frac b=norm2(tmp*vm);\n frac c=norm2(tmp*vs);\n if(b<a) b=b+frac(1,1);\n if(c<a) c=c+frac(1,1);\n if(b<c){\n t1=frac(1,1)-(c-a);\n t2=c-b;\n if(a!=b&&b!=c&&c!=a&&t1==t2)\n ans=min(ans,tmp);\n }\n }\n }\n {\n frac t1=z-x;\n frac t2=y-z;\n frac tmp=t+(t2-t1)/(vs*2-(vh+vm));\n if(base<=tmp){\n frac a=norm2(tmp*vh);\n frac b=norm2(tmp*vm);\n frac c=norm2(tmp*vs);\n if(b<a) b=b+frac(1,1);\n if(c<a) c=c+frac(1,1);\n if(b>c){\n t1=c-a;\n t2=b-c;\n if(a!=b&&b!=c&&c!=a&&t1==t2)\n ans=min(ans,tmp);\n }\n }\n }\n if(ans!=frac(10000000,1)){\n print(ans);\n break;\n }\n t=t+frac(1,1);\n }\n }\n return 0;\n}", "accuracy": 1, "time_ms": 30, "memory_kb": 3224, "score_of_the_acc": -0.1616, "final_rank": 8 }, { "submission_id": "aoj_1338_4803791", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\ntypedef long long ll;\ntemplate<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return true; } return false; }\ntemplate<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return true; } return false; }\n#define all(x) (x).begin(),(x).end()\n#define fi first\n#define se second\n#define mp make_pair\n#define si(x) int(x.size())\nconst int mod=1000000007,MAX=100005,INF=1<<30;\n\nll gcd(ll a,ll b){\n if(b==0) return a;\n return gcd(b,a%b);\n}\n\nint main(){\n \n std::ifstream in(\"text.txt\");\n std::cin.rdbuf(in.rdbuf());\n cin.tie(0);\n ios::sync_with_stdio(false);\n \n while(1){\n ll H,h,m,s;cin>>H>>h>>m>>s;\n \n if(H==0) break;\n \n ll d=119*H-1,l=3600*H*d;\n ll nowh=(3600*h+60*m+s)*d,nowm=(60*m*H+s*H)*d,nows=(60*s*H)*d;\n for(int t=0;;t++){\n if(t){\n nowh+=1;\n nowm+=H;\n nows+=60*H;\n \n if(nowh>=l) nowh-=l;\n if(nowm>=l) nowm-=l;\n if(nows>=l) nows-=l;\n }\n \n ll d1=abs(nowh-nows);\n if(d1>l/2) d1=l-d1;\n \n ll d2=abs(nowm-nows);\n if(d2>l/2) d2=l-d2;\n \n if(nowh==nowm) continue;\n if(nowm==nows) continue;\n if(nows==nowh) continue;\n \n if(d1==d2){\n ll x=t/d;\n s+=x;\n m+=s/60;\n s%=60;\n \n h+=m/60;\n m%=60;\n \n h%=H;\n \n t%=d;\n \n ll g=gcd(t,d);\n t/=g;\n d/=g;\n \n cout<<h<<\" \"<<m<<\" \"<<s*d+t<<\" \"<<d<<endl;\n break;\n }\n }\n }\n}", "accuracy": 1, "time_ms": 30, "memory_kb": 3140, "score_of_the_acc": -0.0993, "final_rank": 4 }, { "submission_id": "aoj_1338_3991881", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\n#define int long long\nint H, ha, ma, sa;\n\nint gcd(int a, int b) {\n if(a == 0) return b;\n if(b == 0) return a;\n if(a < b) swap(a, b);\n if(a % b == 0) return b;\n return gcd(b, a % b);\n}\n\nstruct Clock {\n int h, m, n, d;\n Clock(int _h, int _m, int _n, int _d) {\n h = _h;\n m = _m;\n n = _n;\n d = _d;\n int g = gcd(n, d);\n n /= g;\n d /= g;\n }\n};\n\nbool operator <(Clock a, Clock b) {\n if(a.h != b.h) return a.h < b.h;\n if(a.m != b.m) return a.m < b.m;\n return a.n * b.d < b.n * a.d;\n}\n\nbool operator <=(Clock a, Clock b) {\n if(a.h != b.h) return a.h < b.h;\n if(a.m != b.m) return a.m < b.m;\n return a.n * b.d <= b.n * a.d;\n}\n\nsigned main() {\n cin.tie(nullptr);\n ios::sync_with_stdio(false);\n while(true) {\n cin >> H >> ha >> ma >> sa;\n if(H == 0 && ha == 0 && ma == 0 && sa == 0) break;\n Clock a(ha, ma, sa, 1);\n Clock ans(-1, -1, 0, 1);\n for(int h = 0; h < H; h++) {\n for(int m = 0; m < 60; m++) {\n for(int delta = -1; delta <= 1; delta++) {\n int Left = delta * 3600 * H + 60 * 60 * h + 60 * m * (H + 1);\n if(Left < 0) continue;\n if(Left >= 60 * (H * 120 - H - 1)) continue;\n Clock now(h, m, Left, H * 120 - H - 1);\n if(60 * now.m * now.d == 59 * now.n) continue;\n if(ans.h == -1) {\n ans = now;\n } else if(ans < a && a <= now) {\n ans = now;\n } else if(a <= now && now < ans) {\n ans = now;\n } else if(now <= ans && ans < a) {\n ans = now;\n }\n }\n }\n }\n cout << ans.h << \" \" << ans.m << \" \" << ans.n << \" \" << ans.d << endl;\n }\n return 0;\n}", "accuracy": 1, "time_ms": 100, "memory_kb": 3200, "score_of_the_acc": -0.1534, "final_rank": 7 }, { "submission_id": "aoj_1338_3711051", "code_snippet": "#include<iostream>\nusing namespace std;\nlong gcd(long a,long b){return b?gcd(b,a%b):a;}\nint H,h,m,s;\nbool check(long H,long h,long m,long s)\n{\n\tfor(int r=-360;r<=360;r++)\n\t{\n\t\tlong A=r*360*10*H+60*m*(H+1)+3600*h;\n\t\tlong B=119*H-1;\n\t\tlong d=gcd(A,B);\n\t\tA/=d;\n\t\tB/=d;\n\t\tif(!(A>=60*B||s*B>A)&&!(h==0&&m==0&&A==0))\n\t\t{\n\t\t\tcout<<h<<\" \"<<m<<\" \"<<A<<\" \"<<B<<endl;\n\t\t\treturn true;\n\t\t}\n\t}\n\treturn false;\n}\nmain()\n{\n\twhile(cin>>H>>h>>m>>s,H)\n\t{\n\t\twhile(!check(H,h,m,s))\n\t\t{\n\t\t\ts++;\n\t\t\tif(s==60)m++,s=0;\n\t\t\tif(m==60)h++,m=0;\n\t\t\tif(h==H)h=0;\n\t\t}\n\t}\n}", "accuracy": 1, "time_ms": 30, "memory_kb": 3008, "score_of_the_acc": -0.0014, "final_rank": 1 }, { "submission_id": "aoj_1338_3710901", "code_snippet": "#include<iostream>\nusing namespace std;\nlong gcd(long a,long b){return b?gcd(b,a%b):a;}\nint H,h,m,s;\nbool check(long H,long h,long m,long s)\n{\n\tfor(int r=-360;r<=360;r++)\n\t{\n\t\tlong A=r*360*10*H+60*m*(H+1)+3600*h;\n\t\tlong B=119*H-1;\n\t\tlong d=gcd(A,B);\n\t\tA/=d;\n\t\tB/=d;\n\t\tif(!(A>=60*B||s*B>A)&&!(h==0&&m==0&&A==0))\n\t\t{\n\t\t\tcout<<h<<\" \"<<m<<\" \"<<A<<\" \"<<B<<endl;\n\t\t\treturn true;\n\t\t}\n\t}\n\treturn false;\n}\nmain()\n{\n\twhile(cin>>H>>h>>m>>s,H)\n\t{\n\t\twhile(!check(H,h,m,s))\n\t\t{\n\t\t\ts++;\n\t\t\tif(s==60)m++,s=0;\n\t\t\tif(m==60)h++,m=0;\n\t\t\tif(h==H)h=0;\n\t\t}\n\t}\n}", "accuracy": 1, "time_ms": 30, "memory_kb": 3072, "score_of_the_acc": -0.0488, "final_rank": 2 }, { "submission_id": "aoj_1338_2947799", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\ntypedef long long ll;\ntypedef unsigned long long ull;\ntypedef pair<ll, ll> P;\n\n#define fi first\n#define se second\n#define repl(i,a,b) for(ll i=(ll)(a);i<(ll)(b);i++)\n#define rep(i,n) repl(i,0,n)\n#define all(x) (x).begin(),(x).end()\n#define dbg(x) cout<<#x\"=\"<<x<<endl\n#define mmax(x,y) (x>y?x:y)\n#define mmin(x,y) (x<y?x:y)\n#define maxch(x,y) x=mmax(x,y)\n#define minch(x,y) x=mmin(x,y)\n#define uni(x) x.erase(unique(all(x)),x.end())\n#define exist(x,y) (find(all(x),y)!=x.end())\n#define bcnt __builtin_popcount\n\n#define INF 1e16\n\nll t;\nll H,h0,m0,s0;\n\nint main(){\n while(1){\n cin>>H>>h0>>m0>>s0;\n if(H==0)break;\n ll d=119*H-1;\n ll n=s0*d;\n ll h=h0,m=m0;\n ll mod=360*10*H*d;\n while(1){\n ll ang_h=(3600*h*d+60*m*d+n)%mod;\n ll ang_m=(60*H*m*d+H*n)%mod;\n ll ang_s=(60*H*n)%mod;\n ang_h=(ang_h-ang_s+mod)%mod;\n ang_m=(ang_m-ang_s+mod)%mod;\n if(ang_m!=ang_h&&min(ang_m,ang_h)==mod-max(ang_m,ang_h))break;\n n++;\n if(n==60*d)n=0,m++;\n if(m==60)m=0,h++;\n if(h==H)h=0;\n }\n ll g=__gcd(n,d);\n cout<<h<<\" \"<<m<<\" \"<<n/g<<\" \"<<d/g<<endl;\n }\n return 0;\n}", "accuracy": 1, "time_ms": 380, "memory_kb": 3080, "score_of_the_acc": -0.1027, "final_rank": 5 }, { "submission_id": "aoj_1338_2901754", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\n#define rep(i,n) for(int (i)=0;(i)<(int)(n);++(i))\n#define all(x) (x).begin(),(x).end()\n#define pb push_back\n#define fi first\n#define se second\n#define dbg(x) cout<<#x\" = \"<<((x))<<endl\ntemplate<class T,class U> ostream& operator<<(ostream& o, const pair<T,U> &p){o<<\"(\"<<p.fi<<\",\"<<p.se<<\")\";return o;}\ntemplate<class T> ostream& operator<<(ostream& o, const vector<T> &v){o<<\"[\";for(T t:v){o<<t<<\",\";}o<<\"]\";return o;}\n\nusing pl = pair<ll,ll>;\n\nstruct TIME{\n ll h,m;\n pl s;\n};\n\nvoid PRINT_T(TIME t){\n printf(\"%lld %lld %lld %lld\\n\", t.h, t.m, t.s.fi, t.s.se);\n}\n\nconst ll INF = 60*60*100*10;\n\nbool big(pl a, pl b){\n return a.fi*b.se < b.fi*a.se;\n}\n\npl norm(pl a){\n ll G = __gcd(a.fi, a.se);\n a.fi /= G;\n a.se /= G;\n return a;\n}\n\nint H,x,y,z;\n\n// a から b\npl sub(TIME a, TIME b){\n pl p = a.s;\n p.fi += p.se*60*(60*a.h+a.m);\n\n pl q = b.s;\n q.fi += q.se*60*(60*b.h+b.m);\n\n while(big(q,p)) q.fi += H*3600*q.se;\n p = norm(p);\n q = norm(q);\n\n ll X = q.fi*p.se - p.fi*q.se;\n ll Y = q.se*p.se;\n pl res(X,Y);\n res = norm(res);\n\n // printf(\" DIFF:\\n\");\n // PRINT_T(a);\n // PRINT_T(b);\n // dbg(res);\n return res;\n}\n\nint main(){\n while(cin >>H >>x >>y >>z,H){\n ll T = 60*H;\n\n TIME base = {x,y,{z,1}};\n\n pl diff(INF,1);\n TIME ans;\n\n auto upd = [&](ll p, ll q, int i, int j){\n ll t = 60*i+j;\n if(p>=0 && p/q<60){\n pl s = norm({p,q});\n // printf(\" CAND %d %d %lld/%lld :\", i,j,s.fi,s.se);\n\n pl th(60*t*s.se+s.fi, 60*T*s.se);\n pl tm(60*j*s.se+s.fi, 3600*s.se);\n pl ts(s.fi, s.se*60);\n th = norm(th);\n tm = norm(tm);\n ts = norm(ts);\n\n if( th!=tm && th!=ts && tm!=ts ){\n TIME tmp = {i,j,s};\n pl td = sub(base, tmp);\n if(big(td,diff)){\n diff = td;\n ans = tmp;\n }\n // printf(\"OK\");\n }\n // printf(\"\\n\");\n }\n };\n\n rep(i,H)rep(j,60){\n ll t = 60*i+j;\n\n // 1つめ\n ll p = 3600*t - 60*T*j;\n ll q = T - 60;\n upd(p,q,i,j);\n\n // 2つめ\n p = 3600*t + 60*T*j;\n q = 119*T - 60;\n upd(p,q,i,j);\n\n // 3つめ\n p = 3600*(T+t) + 60*T*j;\n q = 119*T - 60;\n upd(p,q,i,j);\n\n // 4つめ\n p = 3600*(t-T) + 60*T*j;\n q = 119*T - 60;\n upd(p,q,i,j);\n }\n\n PRINT_T(ans);\n }\n return 0;\n}", "accuracy": 1, "time_ms": 680, "memory_kb": 3092, "score_of_the_acc": -0.1526, "final_rank": 6 }, { "submission_id": "aoj_1338_2747801", "code_snippet": "#include <stdio.h>\n#include <cmath>\n#include <algorithm>\n#include <cfloat>\n#include <stack>\n#include <queue>\n#include <vector>\n#include <string>\n#include <iostream>\n#include <set>\n#include <map>\n#include <time.h>\ntypedef long long int ll;\ntypedef unsigned long long int ull;\n#define BIG_NUM 2000000000\n#define MOD 1000000007\n#define EPS 0.000000001\nusing namespace std;\n\n\nll calc(ll x,ll y){\n\n\tif(x < y)swap(x,y);\n\n\tif(y == 0){\n\t\treturn x;\n\t}else{\n\t\treturn calc(y,x%y);\n\t}\n}\n\n\nint main(){\n\n\tint H,h,m,s;\n\n\twhile(true){\n\t\tscanf(\"%d %d %d %d\",&H,&h,&m,&s);\n\t\tif(H == 0 && h == 0 && m == 0 && s == 0)break;\n\n\t\tll bunbo = 119*H-1;\n\t\tll bunshi = s*bunbo;\n\t\tll h0 = h;\n\t\tll m0 = m;\n\t\tll deg_h,deg_m,deg_s,diff_1,diff_2;\n\t\tll base;\n\n\t\twhile(true){\n\n\t\t\tbase = 3600*H*bunbo;\n\n\t\t\tdeg_h = (3600*h0*bunbo+60*m0*bunbo+bunshi)%base;\n\t\t\tdeg_m = (60*H*m0*bunbo+bunshi*H)%base;\n\t\t\tdeg_s = (60*H*bunshi)%base;\n\n\t\t\tif(deg_h != deg_m){\n\t\t\t\tdiff_1 = (deg_s-deg_h+base)%base;\n\t\t\t\tdiff_2 = (deg_m-deg_s+base)%base;\n\t\t\t\tif(diff_1 == diff_2)break;\n\t\t\t}\n\n\t\t\tbunshi++;\n\t\t\tif(bunshi/60 == bunbo) {\n\t\t\t\tbunshi = 0;\n\t\t\t\tm0++;\n\t\t\t}\n\t\t\tif(m0 == 60) {\n\t\t\t\tm0 = 0;\n\t\t\t\th0++;\n\t\t\t}\n\t\t\tif(h0 == H) {\n\t\t\t\th0 = 0;\n\t\t\t}\n\t\t}\n\t\tll common = calc(bunshi,bunbo);\n\t\tbunshi /= common;\n\t\tbunbo /= common;\n\n\t\tprintf(\"%lld %lld %lld %lld\\n\",h0,m0,bunshi,bunbo);\n\t}\n\treturn 0;\n}", "accuracy": 1, "time_ms": 380, "memory_kb": 3176, "score_of_the_acc": -0.1739, "final_rank": 10 }, { "submission_id": "aoj_1338_2608620", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\nusing VI = vector<int>;\nusing VVI = vector<VI>;\nusing PII = pair<int, int>;\nusing LL = long long;\nusing VL = vector<LL>;\nusing VVL = vector<VL>;\nusing PLL = pair<LL, LL>;\nusing VS = vector<string>;\n\n#define ALL(a) begin((a)),end((a))\n#define RALL(a) (a).rbegin(), (a).rend()\n#define PB push_back\n#define EB emplace_back\n#define MP make_pair\n#define SZ(a) int((a).size())\n#define SORT(c) sort(ALL((c)))\n#define RSORT(c) sort(RALL((c)))\n#define UNIQ(c) (c).erase(unique(ALL((c))), end((c)))\n\n#define FOR(i,a,b) for(int i=(a);i<(b);++i)\n#define REP(i,n) FOR(i,0,n)\n\n#define FF first\n#define SS second\ntemplate<class S, class T>\nistream& operator>>(istream& is, pair<S,T>& p){\n return is >> p.FF >> p.SS;\n}\ntemplate<class S, class T>\nostream& operator<<(ostream& os, const pair<S,T>& p){\n return os << p.FF << \" \" << p.SS;\n}\ntemplate<class T>\nvoid maxi(T& x, T y){\n if(x < y) x = y;\n}\ntemplate<class T>\nvoid mini(T& x, T y){\n if(x > y) x = y;\n}\n\n\nconst double EPS = 1e-10;\nconst double PI = acos(-1.0);\n\nint H, h, m, s;\n\n//! x / y\nclass Frac{\npublic:\n using LL = long long;\n LL x, y;\n\n Frac(LL x = 0, LL y = 1) : x(x), y(y) { red(); }\n\n LL gcd(LL x, LL y){\n\tif(y == 0) return x;\n\treturn gcd(y, x%y);\n }\n \n void red(){\n\tif(x == 0){\n\t y = 1;\n\t return;\n\t}\n\tif(y < 0){\n\t x *= -1;\n\t y *= -1;\n\t}\n\t\n\tLL g = gcd(abs(x), y);\n\tx /= g;\n\ty /= g;\n }\n\n Frac& operator += (const Frac& p){ x = x * p.y + y * p.x; y *= p.y; red(); return *this;}\n Frac operator + (const Frac& p) const{ Frac q = *this;return q += p;}\n Frac operator - () const { Frac q = *this; q.x *= -1; return q; }\n Frac& operator -= (const Frac& p){ Frac q = -p; *this += q; return *this;}\n Frac operator - (const Frac& p) const{ Frac q = *this; return q -= p;}\n Frac& operator *= (const Frac& a){ x *= a.x; y *= a.y; red(); return *this;}\n Frac operator * (Frac a) const{ Frac q = *this; return q *= a;}\n Frac& operator /= (const Frac& a){ x *= a.y; y *= a.x; red(); return *this;}\n Frac operator / (Frac a) const{ Frac q = *this; return q /= a;}\n\n bool operator < (const Frac &p) const{\n\treturn x * p.y < y * p.x;\n }\n bool operator <= (const Frac &p) const{\n\treturn *this < p || *this == p;\n }\n bool operator > (const Frac &p) const{\n\treturn p < *this;\n }\n bool operator >= (const Frac &p) const{\n\treturn *this > p || *this == p;\n }\n bool operator == (const Frac &p) const {\n\treturn x == p.x && y == p.y;\n }\n double getR() const { return x * 1. / y; }\n friend ostream& operator <<(ostream& os, const Frac& p);\n};\nFrac abs(const Frac& q){ return Frac(abs(q.x), q.y); }\nostream& operator <<(ostream& os, const Frac& p){\n return os << p.x << \"/\" << p.y;\n}\n\nvoid norm(Frac& t){\n if(t.x < 0) t += 3600*H;\n while(t >= 60 * 60 * H)\n\tt -= 60 * 60 * H;\n}\n\nvoid normR(Frac& t){\n if(t.x < 0) t += 360;\n while(t >= 360)\n\tt -= 360;\n}\n\nFrac dist(Frac& now, Frac& dst){\n if(now <= dst) return dst - now;\n return dst + 3600*H - now;\n}\n\nvoid print(Frac& ans){\n LL ah = ans.x / ans.y / 60 / 60;\n LL am = ans.x / ans.y / 60 % 60;\n Frac as = ans - ah*3600 - am*60;\n cout << ah << \" \" << am << \" \" << as.x << \" \" << as.y << endl;\n}\n\nint main(){\n cin.tie(0);\n ios_base::sync_with_stdio(false);\n\n while(cin>>H>>h>>m>>s,H){\n\tFrac now(h*3600+m*60+s);\n\t\n\tFrac ans(1e9, 1), ans_dt(1e9, 1);\n\tREP(th,H) REP(tm,60){\n\t for(int s1=-1;s1<=1;s1+=2){\n\t\tfor(int s2=-1;s2<=1;s2+=2){\n\t\t Frac c1(3600*th + 60*tm, 10*H);\n\t\t Frac a1 = Frac(1, 10*H) - 6;\n\t\t Frac c2(6*tm);\n\t\t Frac a2 = Frac(1, 10) - 6;\n\n\t\t for(int ang=0;ang<=360;ang+=360){\n\t\t\tFrac ss = (c2 * s2 - c1 * s1 + ang) / (a1 * s1 - a2 * s2);\n\n\t\t\tFrac rh = c1 + ss / (10*H);\n\t\t\tnormR(rh);\n\t\t\tFrac rm = c2 + ss / 10;\n\t\t\tnormR(rm);\n\t\t\tFrac rs = ss * 6;\n\t\t\tnormR(rs);\n\t\t\tif(rh == rm || rh == rs || rm == rs) continue;\n\t\t\tFrac nxt = ss + 3600*th + 60*tm;\n\t\t\tnorm(nxt);\n\t\t\tFrac dd = dist(now, nxt);\n\t\t\tif(dd < ans_dt){\n\t\t\t ans_dt = dd;\n\t\t\t ans = nxt;\n\t\t\t}\n\t\t }\n\t\t}\n\t }\n\t}\n\tprint(ans);\n }\n\n return 0;\n}", "accuracy": 1, "time_ms": 7030, "memory_kb": 3124, "score_of_the_acc": -1.045, "final_rank": 17 }, { "submission_id": "aoj_1338_2608617", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\nusing VI = vector<int>;\nusing VVI = vector<VI>;\nusing PII = pair<int, int>;\nusing LL = long long;\nusing VL = vector<LL>;\nusing VVL = vector<VL>;\nusing PLL = pair<LL, LL>;\nusing VS = vector<string>;\n\n#define ALL(a) begin((a)),end((a))\n#define RALL(a) (a).rbegin(), (a).rend()\n#define PB push_back\n#define EB emplace_back\n#define MP make_pair\n#define SZ(a) int((a).size())\n#define SORT(c) sort(ALL((c)))\n#define RSORT(c) sort(RALL((c)))\n#define UNIQ(c) (c).erase(unique(ALL((c))), end((c)))\n\n#define FOR(i,a,b) for(int i=(a);i<(b);++i)\n#define REP(i,n) FOR(i,0,n)\n\n#define FF first\n#define SS second\ntemplate<class S, class T>\nistream& operator>>(istream& is, pair<S,T>& p){\n return is >> p.FF >> p.SS;\n}\ntemplate<class S, class T>\nostream& operator<<(ostream& os, const pair<S,T>& p){\n return os << p.FF << \" \" << p.SS;\n}\ntemplate<class T>\nvoid maxi(T& x, T y){\n if(x < y) x = y;\n}\ntemplate<class T>\nvoid mini(T& x, T y){\n if(x > y) x = y;\n}\n\n\nconst double EPS = 1e-10;\nconst double PI = acos(-1.0);\n\nint H, h, m, s;\n\n//! x / y\nclass Frac{\npublic:\n using LL = long long;\n LL x, y;\n\n Frac(LL x = 0, LL y = 1) : x(x), y(y) { red(); }\n\n LL gcd(LL x, LL y){\n\tif(y == 0) return x;\n\treturn gcd(y, x%y);\n }\n \n void red(){\n\tif(x == 0){\n\t y = 1;\n\t return;\n\t}\n\tif(y < 0){\n\t x *= -1;\n\t y *= -1;\n\t}\n\t\n\tLL g = gcd(abs(x), y);\n\tx /= g;\n\ty /= g;\n }\n\n Frac& operator += (const Frac& p){ x = x * p.y + y * p.x; y *= p.y; red(); return *this;}\n Frac operator + (const Frac& p) const{ Frac q = *this;return q += p;}\n Frac operator - () const { Frac q = *this; q.x *= -1; return q; }\n Frac& operator -= (const Frac& p){ Frac q = -p; *this += q; return *this;}\n Frac operator - (const Frac& p) const{ Frac q = *this; return q -= p;}\n Frac& operator *= (const Frac& a){ x *= a.x; y *= a.y; red(); return *this;}\n Frac operator * (Frac a) const{ Frac q = *this; return q *= a;}\n Frac& operator /= (const Frac& a){ x *= a.y; y *= a.x; red(); return *this;}\n Frac operator / (Frac a) const{ Frac q = *this; return q /= a;}\n\n bool operator < (const Frac &p) const{\n\treturn x * p.y < y * p.x;\n }\n bool operator <= (const Frac &p) const{\n\treturn *this < p || *this == p;\n }\n bool operator > (const Frac &p) const{\n\treturn p < *this;\n }\n bool operator >= (const Frac &p) const{\n\treturn *this > p || *this == p;\n }\n bool operator == (const Frac &p) const {\n\treturn x == p.x && y == p.y;\n }\n double getR() const { return x * 1. / y; }\n friend ostream& operator <<(ostream& os, const Frac& p);\n};\nFrac abs(const Frac& q){ return Frac(abs(q.x), q.y); }\nostream& operator <<(ostream& os, const Frac& p){\n return os << p.x << \"/\" << p.y;\n}\n\nvoid norm(Frac& t){\n if(t.x < 0) t += 3600*H;\n while(t >= 60 * 60 * H)\n\tt -= 60 * 60 * H;\n}\n\nvoid normR(Frac& t){\n if(t.x < 0) t += 360;\n while(t >= 360)\n\tt -= 360;\n}\n\nFrac dist(Frac& now, Frac& dst){\n if(now <= dst) return dst - now;\n return dst + 3600*H - now;\n}\n\nvoid print(Frac& ans){\n LL ah = ans.x / ans.y / 60 / 60;\n LL am = ans.x / ans.y / 60 % 60;\n Frac as = ans - ah*3600 - am*60;\n cout << ah << \" \" << am << \" \" << as.x << \" \" << as.y << endl;\n}\n\nint main(){\n cin.tie(0);\n ios_base::sync_with_stdio(false);\n\n while(cin>>H>>h>>m>>s,H){\n\tFrac now(h*3600+m*60+s);\n\t\n\tFrac ans(1e9, 1), ans_dt(1e9, 1);\n\tREP(th,H) REP(tm,60){\n\t for(int s1=-1;s1<=1;s1+=2){\n\t\tfor(int s2=-1;s2<=1;s2+=2){\n\t\t Frac c1(3600*th + 60*tm, 10*H);\n\t\t Frac a1 = Frac(1, 10*H) - 6;\n\t\t Frac c2(6*tm);\n\t\t Frac a2 = Frac(1, 10) - 6;\n\n\t\t for(int ang=0;ang<=360;ang+=360){\n\t\t\tFrac ss = (c2 * s2 - c1 * s1 + ang) / (a1 * s1 - a2 * s2);\n\n\t\t\tFrac rh = c1 + ss / (10*H);\n\t\t\tnormR(rh);\n\t\t\tFrac rm = c2 + ss / 10;\n\t\t\tnormR(rm);\n\t\t\tFrac rs = ss * 6;\n\t\t\tnormR(rs);\n\t\t\tif(rh == rm || rh == rs || rm == rs) continue;\n\t\t\tFrac nxt = ss + 3600*th + 60*tm;\n\t\t\tnorm(nxt);\n\t\t\tFrac dd = dist(now, nxt);\n\t\t\tif(dd < ans_dt){\n\t\t\t ans_dt = dd;\n\t\t\t ans = nxt;\n\t\t\t}\n\t\t }\n\t\t}\n\t }\n\t}\n\tLL ah = ans.x / ans.y / 60 / 60;\n\tLL am = ans.x / ans.y / 60 % 60;\n\tFrac as = ans - ah*3600 - am*60;\n\tcout << ah << \" \" << am << \" \" << as.x << \" \" << as.y << endl;\n }\n\n return 0;\n}", "accuracy": 1, "time_ms": 7010, "memory_kb": 3160, "score_of_the_acc": -1.069, "final_rank": 19 } ]
aoj_1337_cpp
Problem C: Count the Regions There are a number of rectangles on the x-y plane. The four sides of the rectangles are parallel to either the x -axis or the y -axis, and all of the rectangles reside within a range specified later. There are no other constraints on the coordinates of the rectangles. The plane is partitioned into regions surrounded by the sides of one or more rectangles. In an example shown in Figure C.1, three rectangles overlap one another, and the plane is partitioned into eight regions. Rectangles may overlap in more complex ways. For example, two rectangles may have overlapped sides, they may share their corner points, and/or they may be nested. Figure C.2 illustrates such cases. Your job is to write a program that counts the number of the regions on the plane partitioned by the rectangles. Input The input consists of multiple datasets. Each dataset is formatted as follows. n l 1 t 1 r 1 b 1 l 2 t 2 r 2 b 2 : l n t n r n b n A dataset starts with n (1 ≤ n ≤ 50), the number of rectangles on the plane. Each of the following n lines describes a rectangle. The i -th line contains four integers, l i , t i , r i , and b i , which are the coordinates of the i -th rectangle; ( l i , t i ) gives the x-y coordinates of the top left corner, and ( r i , b i ) gives that of the bottom right corner of the rectangle (0 ≤ l i < r i ≤ 10 6 , 0 ≤ b i < t i ≤ 10 6 , for 1 ≤ i ≤ n ). The four integers are separated by a space. The input is terminated by a single zero. Output For each dataset, output a line containing the number of regions on the plane partitioned by the sides of the rectangles. Figure C.1. Three rectangles partition the plane into eight regions. This corresponds to the first dataset of the sample input. The x - and y -axes are shown for illustration purposes only, and therefore they do not partition the plane. Figure C.2. Rectangles overlapping in more complex ways. This corresponds to the second dataset. Sample Input 3 4 28 27 11 15 20 42 5 11 24 33 14 5 4 28 27 11 12 11 34 2 7 26 14 16 14 16 19 12 17 28 27 21 2 300000 1000000 600000 0 0 600000 1000000 300000 0 Output for the Sample Input 8 6 6
[ { "submission_id": "aoj_1337_10852755", "code_snippet": "#include <iostream>\n#include <cstdio>\n#include <cstring>\n#include <map>\n#include <algorithm>\nusing namespace std;\nconst int maxn = 555;\nint l[maxn],r[maxn],t[maxn],b[maxn];\nint X[maxn],Y[maxn],g[maxn][maxn];\nmap<int,int>mpx;\nmap<int,int>mpy;\nint N,M;\nint dx[]={0,0,1,-1};\nint dy[]={1,-1,0,0};\nvoid dfs(int i,int j){\n g[i][j]=1;\n for(int k=0;k<4;k++){\n int x=i+dx[k],y=j+dy[k];\n if(x<0 || x>N || y<0 || y>M) continue;\n if(!g[x][y]){\n dfs(x,y);\n }\n }\n}\n\nint main()\n{\n// freopen(\"data.in\",\"r\",stdin);\n int n;\n while(scanf(\"%d\",&n)!=EOF && n){\n for(int i=0;i<n;i++){\n scanf(\"%d%d%d%d\",&l[i],&t[i],&r[i],&b[i]);\n X[i]=l[i];\n X[i+n]=r[i];\n Y[i]=t[i];\n Y[i+n]=b[i];\n }\n mpx.clear();\n mpx.clear();\n sort(X,X+n*2);\n sort(Y,Y+n*2);\n int t1=unique(X,X+n*2)-X;\n int t2=unique(Y,Y+n*2)-Y;\n for(int i=0;i<t1;i++)\n mpx[X[i]]=(i+1)*2;\n for(int i=0;i<t2;i++)\n mpy[Y[i]]=(i+1)*2;\n t1=(t1+1)*2+2;\n t2=(t2+1)*2+2;\n memset(g,0,sizeof g);\n for(int i=0;i<n;i++){\n int lb=mpx[l[i]],rb=mpx[r[i]],tb=mpy[t[i]],bb=mpy[b[i]];\n// printf(\"%d %d %d %d\\n\",lb,rb,tb,bb);\n for(int j=lb;j<=rb;j++)\n g[tb][j]=g[bb][j]=1;\n for(int j=bb;j<=tb;j++)\n g[j][lb]=g[j][rb]=1;\n }\n int ans=0;\n N=t2,M=t1;\n for(int i=0;i<=N;i++){\n for(int j=0;j<=M;j++){\n if(!g[i][j]){\n dfs(i,j);\n ans++;\n }\n }\n }\n printf(\"%d\\n\",ans);\n }\n\n return 0;\n}", "accuracy": 1, "time_ms": 30, "memory_kb": 6460, "score_of_the_acc": -0.0426, "final_rank": 16 }, { "submission_id": "aoj_1337_9684361", "code_snippet": "#include <bits/stdc++.h>\n\nusing namespace std;\n\n#define rep(i, a, b) for (int i = (int)(a); i < (int)(b); i++)\n#define rrep(i, a, b) for (int i = (int)(b)-1; i >= (int)(a); i--)\n#define ALL(v) (v).begin(), (v).end()\n#define UNIQUE(v) sort(ALL(v)), (v).erase(unique(ALL(v)), (v).end())\n#define SZ(v) (int)v.size()\n#define MIN(v) *min_element(ALL(v))\n#define MAX(v) *max_element(ALL(v))\n#define LB(v, x) int(lower_bound(ALL(v), (x)) - (v).begin())\n#define UB(v, x) int(upper_bound(ALL(v), (x)) - (v).begin())\n\nusing uint = unsigned int;\nusing ll = long long int;\nusing ull = unsigned long long;\nusing i128 = __int128_t;\nusing u128 = __uint128_t;\nconst int inf = 0x3fffffff;\nconst ll INF = 0x1fffffffffffffff;\n\ntemplate <typename T> inline bool chmax(T &a, T b) {\n if (a < b) {\n a = b;\n return 1;\n }\n return 0;\n}\ntemplate <typename T> inline bool chmin(T &a, T b) {\n if (a > b) {\n a = b;\n return 1;\n }\n return 0;\n}\ntemplate <typename T, typename U> T ceil(T x, U y) {\n assert(y != 0);\n if (y < 0)\n x = -x, y = -y;\n return (x > 0 ? (x + y - 1) / y : x / y);\n}\ntemplate <typename T, typename U> T floor(T x, U y) {\n assert(y != 0);\n if (y < 0)\n x = -x, y = -y;\n return (x > 0 ? x / y : (x - y + 1) / y);\n}\ntemplate <typename T> int popcnt(T x) {\n return __builtin_popcountll(x);\n}\ntemplate <typename T> int topbit(T x) {\n return (x == 0 ? -1 : 63 - __builtin_clzll(x));\n}\ntemplate <typename T> int lowbit(T x) {\n return (x == 0 ? -1 : __builtin_ctzll(x));\n}\n\ntemplate <class T, class U>\nostream &operator<<(ostream &os, const pair<T, U> &p) {\n os << \"P(\" << p.first << \", \" << p.second << \")\";\n return os;\n}\ntemplate <typename T> ostream &operator<<(ostream &os, const vector<T> &vec) {\n os << \"{\";\n for (int i = 0; i < vec.size(); i++) {\n os << vec[i] << (i + 1 == vec.size() ? \"\" : \", \");\n }\n os << \"}\";\n return os;\n}\ntemplate <typename T, typename U>\nostream &operator<<(ostream &os, const map<T, U> &map_var) {\n os << \"{\";\n for (auto itr = map_var.begin(); itr != map_var.end(); itr++) {\n os << \"(\" << itr->first << \", \" << itr->second << \")\";\n itr++;\n if (itr != map_var.end())\n os << \", \";\n itr--;\n }\n os << \"}\";\n return os;\n}\ntemplate <typename T> ostream &operator<<(ostream &os, const set<T> &set_var) {\n os << \"{\";\n for (auto itr = set_var.begin(); itr != set_var.end(); itr++) {\n os << *itr;\n ++itr;\n if (itr != set_var.end())\n os << \", \";\n itr--;\n }\n os << \"}\";\n return os;\n}\n#ifdef LOCAL\n#define show(...) _show(0, #__VA_ARGS__, __VA_ARGS__)\n#else\n#define show(...) true\n#endif\ntemplate <typename T> void _show(int i, T name) {\n cerr << '\\n';\n}\ntemplate <typename T1, typename T2, typename... T3>\nvoid _show(int i, const T1 &a, const T2 &b, const T3 &...c) {\n for (; a[i] != ',' && a[i] != '\\0'; i++)\n cerr << a[i];\n cerr << \":\" << b << \" \";\n _show(i + 1, a, c...);\n}\n\n/**\n * @brief template\n */\n\nstruct UnionFind{\n vector<int> par; int n;\n UnionFind(){}\n UnionFind(int _n):par(_n,-1),n(_n){}\n int root(int x){return par[x]<0?x:par[x]=root(par[x]);}\n bool same(int x,int y){return root(x)==root(y);}\n int size(int x){return -par[root(x)];}\n bool unite(int x,int y){\n x=root(x),y=root(y); if(x==y)return false;\n if(size(x)>size(y))swap(x,y);\n par[y]+=par[x]; par[x]=y; n--; return true;\n }\n};\n\n/**\n * @brief Union Find\n */\n\nint main() {\nwhile(1) {\n int N;\n cin >> N;\n if (N == 0) return 0;\n vector<int> A(N), B(N), C(N), D(N);\n vector<int> X = {-inf,inf}, Y = {-inf,inf};\n rep(i,0,N) {\n cin >> A[i] >> B[i] >> C[i] >> D[i];\n swap(B[i], D[i]);\n A[i] *= 2;\n B[i] *= 2;\n C[i] *= 2;\n D[i] *= 2;\n X.push_back(A[i]-1),X.push_back(A[i]),X.push_back(A[i]+1);\n Y.push_back(B[i]-1),Y.push_back(B[i]),Y.push_back(B[i]+1);\n X.push_back(C[i]-1),X.push_back(C[i]),X.push_back(C[i]+1);\n Y.push_back(D[i]-1),Y.push_back(D[i]),Y.push_back(D[i]+1);\n }\n UNIQUE(X), UNIQUE(Y);\n rep(i,0,N) {\n A[i] = LB(X,A[i]);\n B[i] = LB(Y,B[i]);\n C[i] = LB(X,C[i]);\n D[i] = LB(Y,D[i]);\n }\n int H = X.size(), W = Y.size();\n vector<vector<bool>> G(H,vector<bool>(W,true));\n rep(i,0,N) {\n rep(j,A[i],C[i]+1) G[j][B[i]] = G[j][D[i]] = false;\n rep(j,B[i],D[i]+1) G[A[i]][j] = G[C[i]][j] = false;\n }\n int ANS = 0;\n vector<vector<int>> used(H,vector<int>(W,false));\n int dx[4] = {1,0,-1,0}, dy[4] = {0,1,0,-1};\n rep(i,0,H) {\n rep(j,0,W) {\n if (used[i][j]) continue;\n if (!G[i][j]) continue;\n ANS++;\n queue<pair<int,int>> Q;\n Q.push({i,j});\n used[i][j] = true;\n while(!Q.empty()) {\n auto [x,y] = Q.front();\n Q.pop();\n rep(k,0,4) {\n int nx = x + dx[k], ny = y + dy[k];\n if (0 <= nx && nx < H && 0 <= ny && ny < W) {\n if (!used[nx][ny] && G[nx][ny]) {\n used[nx][ny] = true;\n Q.push({nx,ny});\n }\n }\n }\n }\n }\n }\n cout << ANS << endl;\n}\n}", "accuracy": 1, "time_ms": 110, "memory_kb": 3488, "score_of_the_acc": -0.0205, "final_rank": 11 }, { "submission_id": "aoj_1337_9506016", "code_snippet": "#include \"bits/stdc++.h\"\n// @JASPER'S BOILERPLATE\nusing namespace std;\nusing ll = long long;\n\n#ifdef JASPER\n#include \"debug.h\"\n#else\n#define debug(...) 166\n#endif\n\n#define int long long\n\nconst int N = 55 * 2;\n\nint x[N], y[N], X[N], Y[N];\n\nint dx[] = {0, 0, -1, 1};\nint dy[] = {-1, 1, 0, 0};\n\nll a[N][N];\n\nvoid solve(int i, int j, int val) {\n if (a[i][j] != val) return;\n a[i][j] = -1;\n for (int k = 0; k < 4; ++k) {\n int ni = i + dx[k];\n int nj = j + dy[k];\n if (0 <= ni && ni < N && 0 <= nj && nj < N) {\n solve(ni, nj, val);\n }\n }\n}\n\n\nsigned main() {\n cin.tie(0) -> sync_with_stdio(0);\n \n #ifdef JASPER\n freopen(\"in1\", \"r\", stdin);\n #endif\n\n int n;\n while (cin >> n) {\n if (n == 0) break;\n \n memset(a, 0, sizeof a);\n vector <int> valX, valY;\n for (int i = 0; i < n; ++i) {\n cin >> x[i] >> y[i] >> X[i] >> Y[i];\n valX.push_back(x[i]);\n valY.push_back(y[i]);\n\n valX.push_back(X[i]);\n valY.push_back(Y[i]);\n }\n\n sort(valX.begin(), valX.end());\n valX.resize(unique(valX.begin(), valX.end()) - valX.begin());\n\n sort(valY.begin(), valY.end());\n valY.resize(unique(valY.begin(), valY.end()) - valY.begin());\n\n for (int i = 0; i < n; ++i) {\n x[i] = (int) (lower_bound(valX.begin(), valX.end(), x[i]) - valX.begin() + 1);\n X[i] = (int) (lower_bound(valX.begin(), valX.end(), X[i]) - valX.begin() + 1);\n \n y[i] = (int) (lower_bound(valY.begin(), valY.end(), y[i]) - valY.begin() + 1);\n Y[i] = (int) (lower_bound(valY.begin(), valY.end(), Y[i]) - valY.begin() + 1);\n\n debug(x[i], y[i], X[i], Y[i]);\n\n for (int _x = x[i]; _x < X[i]; ++_x)\n for (int _y = Y[i]; _y < y[i]; ++_y) \n a[_x][_y] |= (1LL << i);\n }\n\n \n int ans = 0;\n for (int _x = 0; _x < N; ++_x) {\n for (int _y = 0; _y < N; ++_y) { \n if (a[_x][_y] >= 0) {\n solve(_x, _y, a[_x][_y]);\n ++ans;\n }\n\n }\n }\n\n cout << ans << \"\\n\";\n\n }\n\n return 0;\n}", "accuracy": 1, "time_ms": 20, "memory_kb": 4008, "score_of_the_acc": -0.0119, "final_rank": 7 }, { "submission_id": "aoj_1337_9277459", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n#define ll long long\n#define rep(i, a, b) for(ll i = a; i < b; i++)\n#define all(x) (x).begin(), (x).end()\n\nconst vector<int> dx = {0, 1, 0, -1};\nconst vector<int> dy = {1, 0, -1, 0};\n\nint main() {\n while (true) {\n int n;\n cin >> n;\n if (n == 0) return 0;\n vector<int> xs, ys;\n vector<int> l(n), t(n), r(n), b(n);\n rep(i,0,n){\n cin>>l[i]>>t[i]>>r[i]>>b[i];\n xs.push_back(l[i]);\n xs.push_back(r[i]);\n ys.push_back(t[i]);\n ys.push_back(b[i]);\n }\n xs.push_back(-1);\n ys.push_back(-1);\n sort(all(xs));\n xs.erase(unique(all(xs)), xs.end());\n sort(all(ys));\n ys.erase(unique(all(ys)), ys.end());\n\n const int M = 150;\n\n vector<vector<ll>> val(M, vector<ll>(M));\n rep(i,0,n){\n l[i]=lower_bound(all(xs), l[i])-xs.begin();\n r[i]=lower_bound(all(xs), r[i])-xs.begin();\n t[i]=lower_bound(all(ys), t[i])-ys.begin();\n b[i]=lower_bound(all(ys), b[i])-ys.begin();\n rep(x,l[i],r[i])rep(y,b[i],t[i]){\n val[x][y] |= 1LL<<i;\n }\n }\n\n int cnt = 0;\n vector<vector<bool>> visited(M, vector<bool>(M, false));\n rep(x,0,M)rep(y,0,M){\n if (visited[x][y]) continue;\n ++cnt;\n stack<pair<int,int>> st;\n st.push({x,y});\n visited[x][y] = true;\n while (!st.empty()) {\n auto [xx, yy] = st.top();\n st.pop();\n rep(k,0,4){\n int nx = xx+dx[k], ny = yy+dy[k];\n if (0 <= nx && nx < M && 0 <= ny && ny < M && !visited[nx][ny] && val[nx][ny] == val[x][y]) {\n visited[nx][ny] = true;\n st.push({nx, ny});\n }\n }\n }\n }\n cout << cnt << \"\\n\";\n }\n}", "accuracy": 1, "time_ms": 50, "memory_kb": 3640, "score_of_the_acc": -0.0125, "final_rank": 10 }, { "submission_id": "aoj_1337_8992586", "code_snippet": "#line 2 \"cp-library/src/cp-template.hpp\"\n#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\nusing ld = long double;\nusing uint = unsigned int;\nusing ull = unsigned long long;\nusing i32 = int;\nusing u32 = unsigned int;\nusing i64 = long long;\nusing u64 = unsigned long long;\nusing i128 = __int128_t;\ntemplate < class T > bool chmin(T& a, T b) { if(a > b) { a = b; return true; } return false; }\ntemplate < class T > bool chmax(T& a, T b) { if(a < b) { a = b; return true; } return false; }\ntemplate < class T, class U > T ceil (T x, U y) { return (x > 0 ? (x + y - 1) / y : x / y); }\ntemplate < class T, class U > T floor(T x, U y) { return (x > 0 ? x / y : (x - y + 1) / y); }\nint popcnt(i32 x) { return __builtin_popcount(x); }\nint popcnt(u32 x) { return __builtin_popcount(x); }\nint popcnt(i64 x) { return __builtin_popcountll(x); }\nint popcnt(u64 x) { return __builtin_popcountll(x); }\n\n#line 2 \"cp-library/src/utility/rep_itr.hpp\"\ntemplate < class T > struct itr_rep {\n T i, d;\n constexpr itr_rep(const T i) noexcept : i(i), d(1) {}\n constexpr itr_rep(const T i, const T d) noexcept : i(i), d(d) {}\n void operator++() noexcept { i += d; }\n constexpr int operator*() const noexcept { return i; }\n constexpr bool operator!=(const itr_rep x) const noexcept { return d > 0 ? i < x.i : i > x.i; }\n};\n\ntemplate < class T > struct rep {\n const itr_rep< T > s, t;\n constexpr rep(const T t) noexcept : s(0), t(t) {}\n constexpr rep(const T s, const T t) noexcept : s(s), t(t) {}\n constexpr rep(const T s, const T t, const T d) noexcept : s(s, d), t(t, d) {}\n constexpr auto begin() const noexcept { return s; }\n constexpr auto end () const noexcept { return t; }\n};\n\ntemplate < class T > struct revrep {\n const itr_rep < T > s, t;\n constexpr revrep(const T t) noexcept : s(t - 1, -1), t(-1, -1) {}\n constexpr revrep(const T s, const T t) noexcept : s(t - 1, -1), t(s - 1, -1) {}\n constexpr revrep(const T s, const T t, const T d) noexcept : s(t - 1, -d), t(s - 1, -d) {}\n constexpr auto begin() const noexcept { return s; }\n constexpr auto end () const noexcept { return t; }\n};\n#line 3 \"cp-library/src/utility/io.hpp\"\n\n/* 128bit integer */\nistream& operator>>(istream& is, i128& x) {\n std::string s; is >> s;\n int pm = (s[0] == '-');\n x = 0;\n for(int i : rep(pm, int(s.size()))) x = x * 10 + (s[i] - '0');\n if(pm) x *= -1;\n return is;\n}\nostream& operator<<(ostream& os, const i128& x) {\n if(x == 0) return os << '0';\n i128 y = x;\n if(y < 0) { os << '-'; y *= -1; }\n std::vector<int> ny;\n while(y > 0) { ny.push_back(y % 10); y /= 10; }\n for(int i : revrep(ny.size())) os << ny[i];\n return os;\n}\n\ntemplate < class S, class T > istream& operator>>(istream& is, std::pair< S, T >& x) { is >> x.first >> x.second; return is; }\ntemplate < class S, class T > ostream& operator<<(ostream& os, const std::pair< S, T >& x) { os << x.first << \" \" << x.second; return os; }\n\nnamespace scanner {\n struct sca {\n template < class T > operator T() {\n T s; std::cin >> s; return s;\n }\n };\n struct vec {\n int n;\n vec(int n) : n(n) {}\n template < class T > operator std::vector< T >() {\n std::vector< T > v(n);\n for(T& x : v) std::cin >> x;\n return v;\n }\n };\n struct mat {\n int h, w;\n mat(int h, int w) : h(h), w(w) {}\n template < class T > operator std::vector< std::vector< T > >() {\n std::vector m(h, std::vector< T >(w));\n for(std::vector< T >& v : m) for(T& x : v) std::cin >> x;\n return m;\n }\n };\n struct speedup {\n speedup() {\n std::cin.tie(0);\n std::ios::sync_with_stdio(0);\n }\n } speedup_instance;\n}\nscanner::sca in() { return scanner::sca(); }\nscanner::vec in(int n) { return scanner::vec(n); }\nscanner::mat in(int h, int w) { return scanner::mat(h, w); }\n\nnamespace printer {\n void precision(int d) { std::cout << std::fixed << std::setprecision(d); }\n void flush() { std::cout.flush(); }\n}\n\ntemplate < class T >\nostream& operator<<(ostream& os, const std::vector< T > a) {\n int n = a.size();\n for(int i : rep(n)) { os << a[i]; if(i != n - 1) os << ' '; }\n return os;\n}\n\nint print() { std::cout << '\\n'; return 0; }\ntemplate < class head, class... tail > int print(head&& h, tail&&... t) {\n std::cout << h; if(sizeof...(tail)) std::cout << ' ';\n return print(std::forward<tail>(t)...);\n}\ntemplate < class T > int print_n(const std::vector< T > a) {\n int n = a.size();\n for(int i : rep(n)) std::cout << a[i] << \"\\n\";\n return 0;\n}\n\n\n#line 2 \"cp-library/src/utility/key_val.hpp\"\n\ntemplate < class K, class V >\nstruct key_val {\n K key; V val;\n key_val() {}\n key_val(K key, V val) : key(key), val(val) {}\n template < std::size_t Index >\n std::tuple_element_t< Index, key_val >& get() {\n if constexpr (Index == 0) return key;\n if constexpr (Index == 1) return val;\n }\n};\n\nnamespace std {\n\ntemplate < class K, class V > struct tuple_size < key_val< K, V > > : integral_constant< size_t, 2 > {};\ntemplate < class K, class V > struct tuple_element < 0, key_val< K, V > > { using type = K; };\ntemplate < class K, class V > struct tuple_element < 1, key_val< K, V > > { using type = V; };\n\n}\n#line 2 \"cp-library/src/utility/vec_op.hpp\"\ntemplate < class T > key_val< int, T > max_of(const vector< T >& a) {\n int i = std::max_element(a.begin(), a.end()) - a.begin();\n return {i, a[i]};\n}\ntemplate < class T > key_val< int, T > min_of(const vector< T >& a) {\n int i = std::min_element(a.begin(), a.end()) - a.begin();\n return {i, a[i]};\n}\ntemplate < class S, class T > S sum_of(const vector< T >& a) {\n S sum = 0;\n for(const T x : a) sum += x;\n return sum;\n}\ntemplate < class S, class T > vector< S > freq_of(const vector< T >& a, T L, T R) {\n vector< S > res(R - L, S(0));\n for(const T x : a) res[x - L] += 1;\n return res;\n}\ntemplate < class S, class T > struct prefix_sum {\n vector< S > s;\n prefix_sum(const vector< T >& a) : s(a) {\n s.insert(s.begin(), S(0));\n for(int i : rep(a.size())) s[i + 1] += s[i];\n }\n // [L, R)\n S sum(int L, int R) { return s[R] - s[L]; }\n};\n#line 3 \"cp-library/src/utility/heap.hpp\"\n\ntemplate < class T > using heap_min = std::priority_queue< T, std::vector< T >, std::greater< T > >;\ntemplate < class T > using heap_max = std::priority_queue< T, std::vector< T >, std::less< T > >;\n\n#line 27 \"cp-library/src/cp-template.hpp\"\n\n#line 1 \"cp-library/src/algorithm/bin_search.hpp\"\ntemplate < class T, class F >\nT bin_search(T ok, T ng, F f) {\n while(abs(ng - ok) > 1) {\n T mid = (ok + ng) / 2;\n (f(mid) ? ok : ng) = mid;\n }\n return ok;\n}\n\ntemplate < class T, class F >\nT bin_search_real(T ok, T ng, F f, int step = 80) {\n while(step--) {\n T mid = (ok + ng) / 2;\n (f(mid) ? ok : ng) = mid;\n }\n return ok;\n}\n#line 2 \"cp-library/src/algorithm/argsort.hpp\"\n\ntemplate < class T > std::vector< int > argsort(const std::vector< T > &a) {\n std::vector< int > ids((int)a.size());\n std::iota(ids.begin(), ids.end(), 0);\n std::sort(ids.begin(), ids.end(), [&](int i, int j) {\n return a[i] < a[j] || (a[i] == a[j] && i < j);\n });\n return ids;\n}\n#line 1 \"macro.hpp\"\nnamespace macro {\n\nusing size_type = int;\ntemplate < class container > void sort(container& a) { std::sort(std:: begin(a), std:: end(a)); }\ntemplate < class container > void rsort(container& a) { std::sort(std::rbegin(a), std::rend(a)); }\ntemplate < class container > void reverse(container& a) { std::reverse(std::begin(a), std::end(a)); }\ntemplate < class container > void unique(container& a) {\n std::sort(std::begin(a), std::end(a));\n a.erase(std::unique(std::begin(a), std::end(a)), std::end(a));\n}\ntemplate < class container > container sorted(const container& a) { container b = a; sort(b); return std::move(b); }\ntemplate < class container > container rsorted(const container& a) { container b = a; rsort(b); return std::move(b); }\ntemplate < class container, class compare > void sort(container& a, const compare& cmp) { std::sort(std::begin(a), std::end(a), cmp); }\ntemplate < class container, class compare > container sorted(const container& a, const compare& cmp) { container b = a; sort(b, cmp); return std::move(b); }\ntemplate < class container, class value > size_type lower_bound(const container& a, const value& x) { return std::lower_bound(std::begin(a), std::end(a), x) - std::begin(a); }\ntemplate < class container, class value > size_type upper_bound(const container& a, const value& x) { return std::upper_bound(std::begin(a), std::end(a), x) - std::begin(a); }\n\nconst std::vector<std::pair<size_type, size_type>> dir4 = { {+1, 0}, {-1, 0}, { 0, +1}, { 0, -1} };\nconst std::vector<std::pair<size_type, size_type>> dir8 = { {-1, -1}, {-1, 0}, {-1, +1}, { 0, -1}, { 0, +1}, {+1, -1}, {+1, 0}, {+1, +1} };\n\n#ifdef _DEBUG\n#define debug(x) std::cout << \"[\" << __LINE__ << \"] \" << #x << \": \" << x << std::endl\n#else\n#define debug(x)\n#endif\n\ntemplate < class container > void concat(container& a, const container& b) {\n a.insert(std::end(a), std::begin(b), std::end(b));\n}\nstd::vector<size_type> iota(const size_type n) {\n std::vector<size_type> I(n);\n std::iota(std::begin(I), std::end(I), 0);\n return I;\n}\ntemplate < class container > std::vector<size_type> sort_idx(const container& a) {\n const size_type n = a.size();\n std::vector<size_type> I = iota(n);\n std::sort(std::begin(I), std::end(I), [&](size_type i, size_type j) { return a[i] < a[j] or (a[i] == a[j] and i < j); });\n return I;\n}\ntemplate < class container, class compare > std::vector<size_type> sort_idx(const container& a, const compare& cmp) {\n const size_type n = a.size();\n std::vector<size_type> I = iota(n);\n std::sort(std::begin(I), std::end(I), [&](size_type i, size_type j) { return cmp(a[i], a[j]) or (a[i] == a[j] and i < j); });\n return std::move(I);\n}\n\nstruct grid {\n using size_type = int;\n size_type H, W;\n grid(const size_type H, const size_type W) : H(H), W(W) {}\n bool contains(const size_type i, const size_type j) {\n return 0 <= i and i < H and 0 <= j and j < W;\n }\n};\n\n} // namespace macro\n\nusing namespace macro;\n#line 1 \"cp-library/src/data_structure/union_find.hpp\"\nclass union_find {\n public:\n union_find(int n) : data(n, -1) {}\n int unite(int x, int y) {\n x = root(x), y = root(y);\n if(x != y) {\n if(size(x) < size(y)) swap(x, y);\n data[x] += data[y];\n return data[y] = x;\n }\n return -1;\n }\n int root(int x) { return data[x] < 0 ? x : data[x] = root(data[x]); }\n int size(int x) { return -data[root(x)]; }\n bool same(int x, int y) { return root(x) == root(y); }\n\n private:\n vector<int> data;\n};\n#line 4 \"A.cpp\"\n\nint solve(int n) {\n vector<int> x1(n), y1(n), x2(n), y2(n);\n for(int i : rep(n)) {\n x1[i] = in(), y2[i] = in(), x2[i] = in(), y1[i] = in();\n x1[i] *= 2; x2[i] *= 2; y1[i] *= 2; y2[i] *= 2;\n }\n vector<int> vx, vy;\n for(int i : rep(n)) {\n vx.push_back(x1[i] - 1);\n vx.push_back(x1[i]);\n vx.push_back(x1[i] + 1);\n vx.push_back(x2[i] - 1);\n vx.push_back(x2[i]);\n vx.push_back(x2[i] + 1);\n\n vy.push_back(y1[i] - 1);\n vy.push_back(y1[i]);\n vy.push_back(y1[i] + 1);\n vy.push_back(y2[i] - 1);\n vy.push_back(y2[i]);\n vy.push_back(y2[i] + 1);\n }\n unique(vx);\n unique(vy);\n for(int i : rep(n)) {\n x1[i] = lower_bound(vx, x1[i]);\n x2[i] = lower_bound(vx, x2[i]);\n y1[i] = lower_bound(vy, y1[i]);\n y2[i] = lower_bound(vy, y2[i]);\n }\n\n const int x_lim = vx.size();\n const int y_lim = vy.size();\n vector g(x_lim, vector(y_lim, 0));\n for(int i : rep(n)) {\n for(int x = x1[i]; x <= x2[i]; x++) g[x][y1[i]] = g[x][y2[i]] = 1;\n for(int y = y1[i]; y <= y2[i]; y++) g[x1[i]][y] = g[x2[i]][y] = 1;\n }\n\n union_find uf(x_lim * y_lim);\n auto h = [&](int x, int y) { return x * y_lim + y; };\n for(int x : rep(x_lim)) for(int y : rep(y_lim)) if(g[x][y] == 0) {\n if(x + 1 < x_lim and g[x + 1][y] == 0) uf.unite(h(x, y), h(x + 1, y));\n if(y + 1 < y_lim and g[x][y + 1] == 0) uf.unite(h(x, y), h(x, y + 1));\n }\n\n set<int> st;\n for(int x : rep(x_lim)) for(int y : rep(y_lim)) if(g[x][y] == 0) st.insert(uf.root(h(x, y)));\n return st.size();\n}\n\nint main() {\n while(true) {\n int n = in();\n if(n == 0) return 0;\n print(solve(n));\n }\n}", "accuracy": 1, "time_ms": 150, "memory_kb": 4316, "score_of_the_acc": -0.0369, "final_rank": 15 }, { "submission_id": "aoj_1337_8497886", "code_snippet": "#include \"bits/stdc++.h\"\n\nusing namespace std;\n\nusing ll=long long;\n\nstruct UnionFind{\n vector<ll>data;\n UnionFind(ll n):data(n,-1){}\n bool unite(ll a,ll b){\n a=root(a),b=root(b);\n if(a==b)return false;\n if(-data[a]< -data[b])swap(a,b);\n data[a]+=data[b];\n data[b]=a;\n return true;\n }\n bool find(ll a,ll b){return root(a)==root(b);}\n ll root(ll a){\n return data[a]<0?a:data[a]=root(data[a]);\n }\n ll size(ll a){\n return -data[root(a)];\n }\n};\n\nbool solve(){\n int n;cin>>n;\n if(n==0)return false;\n vector<int>l(n),t(n),r(n),b(n);\n map<int,int>X;\n map<int,int>Y;\n const int INF=1e8;\n X[-INF]=1;\n X[INF]=1;\n Y[-INF]=1;\n Y[INF]=1;\n auto Set=[&](int v,map<int,int>&mp){\n mp[v-1]=1;\n mp[v]=1;\n mp[v+1]=1;\n };\n for (int i = 0; i < n; ++i) {\n cin>>l[i]>>t[i]>>r[i]>>b[i];\n Set(l[i],X);\n Set(r[i],X);\n Set(t[i],Y);\n Set(b[i],Y);\n }\n int h=0;\n for(auto &[i,val]:X){\n val=h++;\n }\n int w=0;\n for(auto &[j,val]:Y){\n val=w++;\n }\n vector z(h,vector<ll>(w));\n for (int i = 0; i < n; ++i) {\n auto li=X[l[i]],rj=Y[t[i]];\n auto ri=X[r[i]],lj=Y[b[i]];\n z[li][lj]^=(1LL<<i);\n z[li][rj]^=(1LL<<i);\n z[ri][lj]^=(1LL<<i);\n z[ri][rj]^=(1LL<<i);\n }\n for (int i = 0; i < h; ++i) {\n for (int j = 1; j < w; ++j) {\n z[i][j]^=z[i][j-1];\n }\n }\n for (int i =1; i < h; ++i) {\n for (int j = 0; j < w; ++j) {\n z[i][j]^=z[i-1][j];\n }\n }\n UnionFind uni(h*w);\n vector<int>dx={0,-1,0,1};\n vector<int>dy={1,0,-1,0};\n for (int i = 0; i < h; ++i) {\n for (int j = 0; j < w; ++j) {\n for (int k = 0; k < 4; ++k) {\n auto i2=i+dx[k],j2=j+dy[k];\n if(i2<0 or i2>=h or j2<0 or j2>=w)continue;\n if(z[i][j]==z[i2][j2]){\n uni.unite(i*w+j,i2*w+j2);\n }\n }\n }\n }\n int ans=0;\n for (int i = 0; i < h; ++i) {\n for (int j = 0; j < w; ++j) {\n if(uni.root(i*w+j)==i*w+j){\n ans++;\n }\n }\n }\n cout<<ans<<\"\\n\";\n return true;\n}\n\nint main(){\n ios::sync_with_stdio(false);\n cin.tie(nullptr);\n while(solve());\n return 0;\n}", "accuracy": 1, "time_ms": 180, "memory_kb": 4616, "score_of_the_acc": -0.0453, "final_rank": 17 }, { "submission_id": "aoj_1337_8475056", "code_snippet": "// #define _GLIBCXX_DEBUG\n#pragma GCC optimize(\"O2,no-stack-protector,unroll-loops,fast-math\")\n#include <bits/stdc++.h>\nusing namespace std;\n#define rep(i, n) for (int i = 0; i < int(n); i++)\n#define per(i, n) for (int i = (n)-1; 0 <= i; i--)\n#define rep2(i, l, r) for (int i = (l); i < int(r); i++)\n#define per2(i, l, r) for (int i = (r)-1; int(l) <= i; i--)\n#define each(e, v) for (auto& e : v)\n#define MM << \" \" <<\n#define pb push_back\n#define eb emplace_back\n#define all(x) begin(x), end(x)\n#define rall(x) rbegin(x), rend(x)\n#define sz(x) (int)x.size()\ntemplate <typename T> void print(const vector<T>& v, T x = 0) {\n int n = v.size();\n for (int i = 0; i < n; i++) cout << v[i] + x << (i == n - 1 ? '\\n' : ' ');\n if (v.empty()) cout << '\\n';\n}\nusing ll = long long;\nusing pii = pair<int, int>;\nusing pll = pair<ll, ll>;\ntemplate <typename T> bool chmax(T& x, const T& y) {\n return (x < y) ? (x = y, true) : false;\n}\ntemplate <typename T> bool chmin(T& x, const T& y) {\n return (x > y) ? (x = y, true) : false;\n}\ntemplate <class T>\nusing minheap = std::priority_queue<T, std::vector<T>, std::greater<T>>;\ntemplate <class T> using maxheap = std::priority_queue<T>;\ntemplate <typename T> int lb(const vector<T>& v, T x) {\n return lower_bound(begin(v), end(v), x) - begin(v);\n}\ntemplate <typename T> int ub(const vector<T>& v, T x) {\n return upper_bound(begin(v), end(v), x) - begin(v);\n}\ntemplate <typename T> void rearrange(vector<T>& v) {\n sort(begin(v), end(v));\n v.erase(unique(begin(v), end(v)), end(v));\n}\n\n// __int128_t gcd(__int128_t a, __int128_t b) {\n// if (a == 0)\n// return b;\n// if (b == 0)\n// return a;\n// __int128_t cnt = a % b;\n// while (cnt != 0) {\n// a = b;\n// b = cnt;\n// cnt = a % b;\n// }\n// return b;\n// }\n\nstruct Union_Find_Tree {\n vector<int> data;\n const int n;\n int cnt;\n \n Union_Find_Tree(int n) : data(n, -1), n(n), cnt(n) {}\n \n int root(int x) {\n if (data[x] < 0) return x;\n return data[x] = root(data[x]);\n }\n \n int operator[](int i) { return root(i); }\n \n bool unite(int x, int y) {\n x = root(x), y = root(y);\n if (x == y) return false;\n if (data[x] > data[y]) swap(x, y);\n data[x] += data[y], data[y] = x;\n cnt--;\n return true;\n }\n \n int size(int x) { return -data[root(x)]; }\n \n int count() { return cnt; };\n \n bool same(int x, int y) { return root(x) == root(y); }\n \n void clear() {\n cnt = n;\n fill(begin(data), end(data), -1);\n }\n};\n\ntemplate <int mod> struct Mod_Int {\n int x;\n\n Mod_Int() : x(0) {}\n\n Mod_Int(long long y) : x(y >= 0 ? y % mod : (mod - (-y) % mod) % mod) {}\n\n static int get_mod() { return mod; }\n\n Mod_Int& operator+=(const Mod_Int& p) {\n if ((x += p.x) >= mod) x -= mod;\n return *this;\n }\n\n Mod_Int& operator-=(const Mod_Int& p) {\n if ((x += mod - p.x) >= mod) x -= mod;\n return *this;\n }\n\n Mod_Int& operator*=(const Mod_Int& p) {\n x = (int)(1LL * x * p.x % mod);\n return *this;\n }\n\n Mod_Int& operator/=(const Mod_Int& p) {\n *this *= p.inverse();\n return *this;\n }\n\n Mod_Int& operator++() { return *this += Mod_Int(1); }\n\n Mod_Int operator++(int) {\n Mod_Int tmp = *this;\n ++*this;\n return tmp;\n }\n\n Mod_Int& operator--() { return *this -= Mod_Int(1); }\n\n Mod_Int operator--(int) {\n Mod_Int tmp = *this;\n --*this;\n return tmp;\n }\n\n Mod_Int operator-() const { return Mod_Int(-x); }\n\n Mod_Int operator+(const Mod_Int& p) const { return Mod_Int(*this) += p; }\n\n Mod_Int operator-(const Mod_Int& p) const { return Mod_Int(*this) -= p; }\n\n Mod_Int operator*(const Mod_Int& p) const { return Mod_Int(*this) *= p; }\n\n Mod_Int operator/(const Mod_Int& p) const { return Mod_Int(*this) /= p; }\n\n bool operator==(const Mod_Int& p) const { return x == p.x; }\n\n bool operator!=(const Mod_Int& p) const { return x != p.x; }\n\n Mod_Int inverse() const {\n assert(*this != Mod_Int(0));\n return pow(mod - 2);\n }\n\n Mod_Int pow(long long k) const {\n Mod_Int now = *this, ret = 1;\n for (; k > 0; k >>= 1, now *= now) {\n if (k & 1) ret *= now;\n }\n return ret;\n }\n\n friend ostream& operator<<(ostream& os, const Mod_Int& p) {\n return os << p.x;\n }\n\n friend istream& operator>>(istream& is, Mod_Int& p) {\n long long a;\n is >> a;\n p = Mod_Int<mod>(a);\n return is;\n }\n};\n\nll mpow2(ll x, ll n, ll mod) {\n ll ans = 1;\n x %= mod;\n while (n != 0) {\n if (n & 1) ans = ans * x % mod;\n x = x * x % mod;\n n = n >> 1;\n }\n ans %= mod;\n return ans;\n}\n\ntemplate <typename T> T modinv(T a, const T& m) {\n T b = m, u = 1, v = 0;\n while (b > 0) {\n T t = a / b;\n swap(a -= t * b, b);\n swap(u -= t * v, v);\n }\n return u >= 0 ? u % m : (m - (-u) % m) % m;\n}\n\nll divide_int(ll a, ll b) {\n if (b < 0) a = -a, b = -b;\n return (a >= 0 ? a / b : (a - b + 1) / b);\n}\n\n// const int MOD = 1000000007;\nconst int MOD = 998244353;\nusing mint = Mod_Int<MOD>;\n\n// ----- library -------\n// ----- library -------\n\nint main() {\n ios::sync_with_stdio(false);\n std::cin.tie(nullptr);\n cout << fixed << setprecision(15);\n\n while (1) {\n int n;\n cin >> n;\n if (!n)\n break;\n vector<int> l(n), r(n), u(n), d(n);\n rep(i, n) cin >> l[i] >> d[i] >> r[i] >> u[i];\n vector<int> xs, ys;\n rep(i, n) xs.eb(l[i]), xs.eb(r[i]), ys.eb(u[i]), ys.eb(d[i]);\n xs.eb(-1e9), xs.eb(1e9), ys.eb(-1e9), ys.eb(1e9);\n rearrange(xs), rearrange(ys);\n rep(i, n) l[i] = lb(xs, l[i]), r[i] = lb(xs, r[i]), u[i] = lb(ys, u[i]), d[i] = lb(ys, d[i]);\n int h = sz(ys) - 1, w = sz(xs) - 1;\n vector<vector<int>> rf(h, vector<int>(w, 1)), cf(h, vector<int>(w, 1));\n rep(i, n) {\n rep2(t, l[i], r[i]) rf[u[i] - 1][t] = 0, rf[d[i] - 1][t] = 0;\n rep2(t, u[i], d[i]) cf[t][l[i] - 1] = 0, cf[t][r[i] - 1] = 0;\n }\n Union_Find_Tree uf(h * w);\n rep(i, h - 1) rep(j, w) if (rf[i][j]) uf.unite(i * w + j, (i + 1) * w + j);\n rep(i, h) rep(j, w - 1) if (cf[i][j]) uf.unite(i * w + j, i * w + j + 1);\n cout << uf.count() << '\\n';\n }\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3536, "score_of_the_acc": -0.0047, "final_rank": 2 }, { "submission_id": "aoj_1337_6987456", "code_snippet": "#include <bits/stdc++.h>\n#include <complex>\n#include <random>\nusing namespace std;\n//#include <atcoder/all>\n//using namespace atcoder;\nusing ll = long long;\nusing vll = vector<ll>;\nusing vvll = vector<vll>;\nusing vvvll = vector<vvll>;\nusing vvvvll = vector<vvvll>;\nusing vvvvvll = vector<vvvvll>;\nusing vvvvvvll = vector<vvvvvll>;\nusing vb = vector<bool>;\nusing vvb = vector<vb>;\nusing vvvb = vector<vvb>;\nusing vvvvb = vector<vvvb>;\nusing vd = vector<double>;\nusing vvd = vector<vd>;\nusing vvvd = vector<vvd>;\nusing vvvvd = vector<vvvd>;\nusing vvvvvd = vector<vvvvd>;\n#define all(A) A.begin(),A.end()\n#define ALL(A) A.begin(),A.end()\n#define rep(i, n) for (ll i = 0; i < (ll) (n); i++)\nusing pqr = priority_queue<pair<ll, ll>, vector<pair<ll, ll>>, greater<pair<ll, ll>>>;\n#define tN(t) (t==1?XN:YN)\n#define tS(t) (t==1?XS:YS)\n#define tA(t) (t==1?XA:YA)\n\ntemplate<class T>\nbool chmax(T& p, T q, bool C = 1) {\n if (C == 0 && p == q) {\n return 1;\n }\n if (p < q) {\n p = q;\n return 1;\n }\n else {\n return 0;\n }\n}\n\ntemplate<class T>\nbool chmin(T& p, T q, bool C = 1) {\n if (C == 0 && p == q) {\n return 1;\n }\n if (p > q) {\n p = q;\n return 1;\n }\n else {\n return 0;\n }\n}\n\nll gcd(ll(a), ll(b)) {\n if (b == 0)return a;\n ll c = a;\n while (a % b != 0) {\n c = a % b;\n a = b;\n b = c;\n }\n return b;\n}\nll sqrtz(ll N) {\n ll L = 0;\n ll R = sqrt(N) + 10000;\n while (abs(R - L) > 1) {\n ll mid = (R + L) / 2;\n if (mid * mid <= N)L = mid;\n else R = mid;\n }\n return L;\n\n}\n\nvector<ll> fact, factinv, inv;\nll mod = 1e9 + 7;\nvoid prenCkModp(ll n) {\n fact.resize(n + 5);\n factinv.resize(n + 5);\n inv.resize(n + 5);\n fact.at(0) = fact.at(1) = 1;\n factinv.at(0) = factinv.at(1) = 1;\n inv.at(1) = 1;\n for (ll i = 2; i < n + 5; i++) {\n fact.at(i) = (fact.at(i - 1) * i) % mod;\n inv.at(i) = mod - (inv.at(mod % i) * (mod / i)) % mod;\n factinv.at(i) = (factinv.at(i - 1) * inv.at(i)) % mod;\n }\n\n}\nll nCk(ll n, ll k) {\n if (n < k) return 0;\n return fact.at(n) * (factinv.at(k) * factinv.at(n - k) % mod) % mod;\n}\nll modPow(ll a, ll n, ll p) {\n ll res = 1;\n ll k = a;\n while (n > 0) {\n if (n % 2 == 1) {\n res *= k;\n if (res > p)res %= p;\n }\n k = k * k;\n if (k > p)k %= p;\n n /= 2;\n }\n return res;\n}\n\nint main() {\n cin.tie(nullptr);\n ios::sync_with_stdio(false);\n\n \n while(1){\n ll N;\n cin>>N;\n if(N==0)return 0;\n map<ll,ll> MX,MY;\n vector<pair<pair<ll,ll>,pair<ll,ll>>> P(N);\n rep(i,N){\n ll A,B,C,D;\n cin>>A>>B>>C>>D;\n swap(B,D);\n MX[A]=MX[C]=1;\n MY[B]=MY[D]=1;\n P[i]={{A,B},{C,D}};\n }\n ll k=0,p=0;\n for(auto x:MX){\n MX[x.first]=2*k+1;\n k++;\n }\n for(auto x:MY){\n MY[x.first]=2*p+1;\n p++;\n }\n vvll A(250,vll(250,0));\n rep(i,N){\n P[i].first.first=MX[P[i].first.first];\n P[i].first.second=MY[P[i].first.second];\n P[i].second.first=MX[P[i].second.first];\n P[i].second.second=MY[P[i].second.second];\n \n for(ll j=P[i].first.first;j<=P[i].second.first;j++){\n A[j][P[i].second.second]=1;\n A[j][P[i].first.second]=1;\n }\n for(ll j=P[i].first.second;j<=P[i].second.second;j++){\n A[P[i].second.first][j]=1;\n A[P[i].first.first][j]=1;\n }\n }\n //cout<<1<<endl;\n ll an=0;\n vll dx={1,0,-1,0};\n vll dy={0,1,0,-1};\n rep(i,250)rep(j,250){\n if(A[i][j]==1)continue;\n an++;\n queue<pair<ll,ll>> Q;\n Q.push({i,j});\n while(!Q.empty()){\n auto p=Q.front();\n Q.pop();\n if(A[p.first][p.second]==1)continue;\n A[p.first][p.second]=1;\n rep(d,4){\n ll nx=p.first+dx[d];\n ll ny=p.second+dy[d];\n if(nx<0||ny<0||nx>=250||ny>=250)continue;\n if(A[nx][ny]==1)continue;\n Q.push({nx,ny});\n }\n }\n }\n cout<<an<<endl;\n }\n}", "accuracy": 1, "time_ms": 100, "memory_kb": 3824, "score_of_the_acc": -0.0229, "final_rank": 13 }, { "submission_id": "aoj_1337_6719103", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\n#ifdef _RUTHEN\n#include \"debug.hpp\"\n#else\n#define show(...) true\n#endif\n\nusing ll = long long;\n#define rep(i, n) for (int i = 0; i < (n); i++)\ntemplate <class T> using V = vector<T>;\n\nstruct UF {\n V<int> par;\n UF(int n) : par(n, -1) {}\n int leader(int x) { return par[x] < 0 ? x : par[x] = leader(par[x]); }\n bool merge(int x, int y) {\n x = leader(x), y = leader(y);\n if (x == y) return false;\n if (par[x] > par[y]) swap(x, y);\n par[x] += par[y];\n par[y] = x;\n return true;\n }\n int size(int x) { return -par[leader(x)]; }\n};\n\nint main() {\n ios::sync_with_stdio(false);\n cin.tie(0);\n int N;\n while (cin >> N, N != 0) {\n V<int> l(N), t(N), r(N), b(N);\n rep(i, N) cin >> l[i] >> t[i] >> r[i] >> b[i];\n V<int> zx = {-1, 1000001}, zy = {-1, 1000001};\n rep(i, N) {\n zx.push_back(l[i]);\n zx.push_back(r[i]);\n zy.push_back(b[i]);\n zy.push_back(t[i]);\n }\n sort(zx.begin(), zx.end());\n zx.erase(unique(zx.begin(), zx.end()), zx.end());\n sort(zy.begin(), zy.end());\n zy.erase(unique(zy.begin(), zy.end()), zy.end());\n rep(i, N) {\n l[i] = lower_bound(zx.begin(), zx.end(), l[i]) - zx.begin();\n r[i] = lower_bound(zx.begin(), zx.end(), r[i]) - zx.begin();\n b[i] = lower_bound(zy.begin(), zy.end(), b[i]) - zy.begin();\n t[i] = lower_bound(zy.begin(), zy.end(), t[i]) - zy.begin();\n }\n int H = zx.size(), W = zy.size();\n V<V<ll>> imos(H + 1, V<ll>(W + 1, 0));\n rep(i, N) {\n imos[l[i]][b[i]] += 1LL << i;\n imos[l[i]][t[i]] -= 1LL << i;\n imos[r[i]][b[i]] -= 1LL << i;\n imos[r[i]][t[i]] += 1LL << i;\n }\n rep(x, H + 1) rep(y, W) imos[x][y + 1] += imos[x][y];\n rep(x, H) rep(y, W + 1) imos[x + 1][y] += imos[x][y];\n int M = (H + 1) * (W + 1);\n UF uf(M);\n const int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};\n rep(x, H + 1) {\n rep(y, W + 1) {\n rep(k, 4) {\n int nx = x + dx[k], ny = y + dy[k];\n if (!(0 <= nx and nx <= H and 0 <= ny and ny <= W)) continue;\n if (imos[x][y] == imos[nx][ny]) uf.merge(x * (W + 1) + y, nx * (W + 1) + ny);\n }\n }\n }\n int ans = 0;\n rep(x, H + 1) {\n rep(y, W + 1) {\n if (x * (W + 1) + y == uf.leader(x * (W + 1) + y)) {\n ans++;\n }\n }\n }\n cout << ans << '\\n';\n }\n return 0;\n}", "accuracy": 1, "time_ms": 20, "memory_kb": 3444, "score_of_the_acc": -0.0053, "final_rank": 3 }, { "submission_id": "aoj_1337_6719075", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\n#ifdef _RUTHEN\n#include \"debug.hpp\"\n#else\n#define show(...) true\n#endif\n\nusing ll = long long;\n#define rep(i, n) for (int i = 0; i < (n); i++)\ntemplate <class T> using V = vector<T>;\n\nstruct UF {\n V<int> par;\n UF(int n) : par(n, -1) {}\n int leader(int x) { return par[x] < 0 ? x : par[x] = leader(par[x]); }\n bool merge(int x, int y) {\n x = leader(x), y = leader(y);\n if (x == y) return false;\n if (par[x] > par[y]) swap(x, y);\n par[x] += par[y];\n par[y] = x;\n return true;\n }\n};\n\nint main() {\n ios::sync_with_stdio(false);\n cin.tie(0);\n int N;\n while (cin >> N, N != 0) {\n V<int> l(N), t(N), r(N), b(N);\n rep(i, N) cin >> l[i] >> t[i] >> r[i] >> b[i];\n V<int> zx = {-1, 1000001}, zy = {-1, 1000001};\n rep(i, N) {\n zx.push_back(l[i]);\n zx.push_back(r[i]);\n zy.push_back(b[i]);\n zy.push_back(t[i]);\n }\n sort(zx.begin(), zx.end());\n zx.erase(unique(zx.begin(), zx.end()), zx.end());\n sort(zy.begin(), zy.end());\n zy.erase(unique(zy.begin(), zy.end()), zy.end());\n rep(i, N) {\n l[i] = lower_bound(zx.begin(), zx.end(), l[i]) - zx.begin();\n r[i] = lower_bound(zx.begin(), zx.end(), r[i]) - zx.begin();\n b[i] = lower_bound(zy.begin(), zy.end(), b[i]) - zy.begin();\n t[i] = lower_bound(zy.begin(), zy.end(), t[i]) - zy.begin();\n }\n int H = zx.size(), W = zy.size();\n V<V<ll>> cum(H + 1, V<ll>(W + 1, 0));\n rep(i, N) {\n cum[l[i]][b[i]] += 1LL << i;\n cum[l[i]][t[i]] -= 1LL << i;\n cum[r[i]][b[i]] -= 1LL << i;\n cum[r[i]][t[i]] += 1LL << i;\n }\n rep(x, H) rep(y, W) cum[x + 1][y + 1] += cum[x + 1][y] + cum[x][y + 1] - cum[x][y];\n int M = (H + 1) * (W + 1);\n UF uf(M);\n const int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};\n rep(x, H + 1) {\n rep(y, W + 1) {\n rep(k, 4) {\n int nx = x + dx[k], ny = y + dy[k];\n if (!(0 <= nx and nx <= H and 0 <= ny and ny <= W)) continue;\n if (cum[x][y] == cum[nx][ny]) uf.merge(x * (W + 1) + y, nx * (W + 1) + ny);\n }\n }\n }\n int ans = 0;\n rep(x, H + 1) {\n rep(y, W + 1) {\n if (x * (W + 1) + y == uf.leader(x * (W + 1) + y)) {\n ans++;\n }\n }\n }\n cout << ans << '\\n';\n }\n return 0;\n}", "accuracy": 1, "time_ms": 20, "memory_kb": 3448, "score_of_the_acc": -0.0053, "final_rank": 4 }, { "submission_id": "aoj_1337_6719074", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\n#ifdef _RUTHEN\n#include \"debug.hpp\"\n#else\n#define show(...) true\n#endif\n\nusing ll = long long;\n#define rep(i, n) for (int i = 0; i < (n); i++)\ntemplate <class T> using V = vector<T>;\n\nstruct UF {\n V<int> par;\n UF(int n) : par(n, -1) {}\n int leader(int x) { return par[x] < 0 ? x : par[x] = leader(par[x]); }\n bool merge(int x, int y) {\n x = leader(x), y = leader(y);\n if (x == y) return false;\n if (par[x] > par[y]) swap(x, y);\n par[x] += par[y];\n par[y] = x;\n return true;\n }\n int size(int x) { return -par[leader(x)]; }\n};\n\nint main() {\n ios::sync_with_stdio(false);\n cin.tie(0);\n int N;\n while (cin >> N, N != 0) {\n V<int> l(N), t(N), r(N), b(N);\n rep(i, N) cin >> l[i] >> t[i] >> r[i] >> b[i];\n V<int> zx = {-1, 1000001}, zy = {-1, 1000001};\n rep(i, N) {\n zx.push_back(l[i]);\n zx.push_back(r[i]);\n zy.push_back(b[i]);\n zy.push_back(t[i]);\n }\n sort(zx.begin(), zx.end());\n zx.erase(unique(zx.begin(), zx.end()), zx.end());\n sort(zy.begin(), zy.end());\n zy.erase(unique(zy.begin(), zy.end()), zy.end());\n rep(i, N) {\n l[i] = lower_bound(zx.begin(), zx.end(), l[i]) - zx.begin();\n r[i] = lower_bound(zx.begin(), zx.end(), r[i]) - zx.begin();\n b[i] = lower_bound(zy.begin(), zy.end(), b[i]) - zy.begin();\n t[i] = lower_bound(zy.begin(), zy.end(), t[i]) - zy.begin();\n }\n int H = zx.size(), W = zy.size();\n V<V<ll>> cum(H + 1, V<ll>(W + 1, 0));\n rep(i, N) {\n cum[l[i]][b[i]] += 1LL << i;\n cum[l[i]][t[i]] -= 1LL << i;\n cum[r[i]][b[i]] -= 1LL << i;\n cum[r[i]][t[i]] += 1LL << i;\n }\n rep(x, H) rep(y, W) cum[x + 1][y + 1] += cum[x + 1][y] + cum[x][y + 1] - cum[x][y];\n int M = (H + 1) * (W + 1);\n UF uf(M);\n const int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};\n rep(x, H + 1) {\n rep(y, W + 1) {\n rep(k, 4) {\n int nx = x + dx[k], ny = y + dy[k];\n if (!(0 <= nx and nx <= H and 0 <= ny and ny <= W)) continue;\n if (cum[x][y] == cum[nx][ny]) uf.merge(x * (W + 1) + y, nx * (W + 1) + ny);\n }\n }\n }\n int ans = 0;\n rep(x, H + 1) {\n rep(y, W + 1) {\n if (x * (W + 1) + y == uf.leader(x * (W + 1) + y)) {\n ans++;\n }\n }\n }\n cout << ans << '\\n';\n }\n return 0;\n}", "accuracy": 1, "time_ms": 20, "memory_kb": 3520, "score_of_the_acc": -0.0062, "final_rank": 5 }, { "submission_id": "aoj_1337_6458457", "code_snippet": "#line 1 \"f.cpp\"\n/**\n *\tauthor: nok0\n *\tcreated: 2022.04.06 14:06:15\n**/\n#line 1 \"/Users/nok0/Documents/Programming/nok0/atcoder/dsu.hpp\"\n\n\n\n#include <algorithm>\n#include <cassert>\n#include <vector>\n\nnamespace atcoder {\n\n// Implement (union by size) + (path compression)\n// Reference:\n// Zvi Galil and Giuseppe F. Italiano,\n// Data structures and algorithms for disjoint set union problems\nstruct dsu {\n public:\n dsu() : _n(0) {}\n explicit dsu(int n) : _n(n), parent_or_size(n, -1) {}\n\n int merge(int a, int b) {\n assert(0 <= a && a < _n);\n assert(0 <= b && b < _n);\n int x = leader(a), y = leader(b);\n if (x == y) return x;\n if (-parent_or_size[x] < -parent_or_size[y]) std::swap(x, y);\n parent_or_size[x] += parent_or_size[y];\n parent_or_size[y] = x;\n return x;\n }\n\n bool same(int a, int b) {\n assert(0 <= a && a < _n);\n assert(0 <= b && b < _n);\n return leader(a) == leader(b);\n }\n\n int leader(int a) {\n assert(0 <= a && a < _n);\n if (parent_or_size[a] < 0) return a;\n return parent_or_size[a] = leader(parent_or_size[a]);\n }\n\n int size(int a) {\n assert(0 <= a && a < _n);\n return -parent_or_size[leader(a)];\n }\n\n std::vector<std::vector<int>> groups() {\n std::vector<int> leader_buf(_n), group_size(_n);\n for (int i = 0; i < _n; i++) {\n leader_buf[i] = leader(i);\n group_size[leader_buf[i]]++;\n }\n std::vector<std::vector<int>> result(_n);\n for (int i = 0; i < _n; i++) {\n result[i].reserve(group_size[i]);\n }\n for (int i = 0; i < _n; i++) {\n result[leader_buf[i]].push_back(i);\n }\n result.erase(\n std::remove_if(result.begin(), result.end(),\n [&](const std::vector<int>& v) { return v.empty(); }),\n result.end());\n return result;\n }\n\n private:\n int _n;\n // root node: -1 * component size\n // otherwise: parent\n std::vector<int> parent_or_size;\n};\n\n} // namespace atcoder\n\n\n#line 1 \"/Users/nok0/Documents/Programming/nok0/cftemp.hpp\"\n#include <bits/stdc++.h>\nusing namespace std;\n\n#pragma region Macros\n// rep macro\n#define foa(v, a) for(auto &v : a)\n#define REPname(a, b, c, d, e, ...) e\n#define REP(...) REPname(__VA_ARGS__, REP3, REP2, REP1, REP0)(__VA_ARGS__)\n#define REP0(x) for(int i = 0; i < (x); ++i)\n#define REP1(i, x) for(int i = 0; i < (x); ++i)\n#define REP2(i, l, r) for(int i = (l); i < (r); ++i)\n#define REP3(i, l, r, c) for(int i = (l); i < (r); i += (c))\n#define REPSname(a, b, c, ...) c\n#define REPS(...) REPSname(__VA_ARGS__, REPS1, REPS0)(__VA_ARGS__)\n#define REPS0(x) for(int i = 1; i <= (x); ++i)\n#define REPS1(i, x) for(int i = 1; i <= (x); ++i)\n#define RREPname(a, b, c, d, e, ...) e\n#define RREP(...) RREPname(__VA_ARGS__, RREP3, RREP2, RREP1, RREP0)(__VA_ARGS__)\n#define RREP0(x) for(int i = (x)-1; i >= 0; --i)\n#define RREP1(i, x) for(int i = (x)-1; i >= 0; --i)\n#define RREP2(i, r, l) for(int i = (r)-1; i >= (l); --i)\n#define RREP3(i, r, l, c) for(int i = (r)-1; i >= (l); i -= (c))\n#define RREPSname(a, b, c, ...) c\n#define RREPS(...) RREPSname(__VA_ARGS__, RREPS1, RREPS0)(__VA_ARGS__)\n#define RREPS0(x) for(int i = (x); i >= 1; --i)\n#define RREPS1(i, x) for(int i = (x); i >= 1; --i)\n\n// name macro\n#define pb push_back\n#define eb emplace_back\n#define SZ(x) ((int)(x).size())\n#define all(x) (x).begin(), (x).end()\n#define rall(x) (x).rbegin(), (x).rend()\n#define popcnt(x) __builtin_popcountll(x)\ntemplate <class T = int>\nusing V = std::vector<T>;\ntemplate <class T = int>\nusing VV = std::vector<std::vector<T>>;\ntemplate <class T>\nusing pqup = std::priority_queue<T, std::vector<T>, std::greater<T>>;\nusing ll = long long;\nusing ld = long double;\nusing int128 = __int128_t;\nusing pii = std::pair<int, int>;\nusing pll = std::pair<long long, long long>;\n\n// input macro\ntemplate <class T, class U>\nstd::istream &operator>>(std::istream &is, std::pair<T, U> &p) {\n\tis >> p.first >> p.second;\n\treturn is;\n}\ntemplate <class T>\nstd::istream &operator>>(std::istream &is, std::vector<T> &v) {\n\tfor(T &i : v) is >> i;\n\treturn is;\n}\nstd::istream &operator>>(std::istream &is, __int128_t &a) {\n\tstd::string s;\n\tis >> s;\n\t__int128_t ret = 0;\n\tfor(int i = 0; i < s.length(); i++)\n\t\tif('0' <= s[i] and s[i] <= '9')\n\t\t\tret = 10 * ret + s[i] - '0';\n\ta = ret * (s[0] == '-' ? -1 : 1);\n\treturn is;\n}\nnamespace scanner {\nvoid scan(int &a) { std::cin >> a; }\nvoid scan(long long &a) { std::cin >> a; }\nvoid scan(std::string &a) { std::cin >> a; }\nvoid scan(char &a) { std::cin >> a; }\nvoid scan(char a[]) { std::scanf(\"%s\", a); }\nvoid scan(double &a) { std::cin >> a; }\nvoid scan(long double &a) { std::cin >> a; }\ntemplate <class T, class U>\nvoid scan(std::pair<T, U> &p) { std::cin >> p; }\ntemplate <class T>\nvoid scan(std::vector<T> &a) { std::cin >> a; }\nvoid INPUT() {}\ntemplate <class Head, class... Tail>\nvoid INPUT(Head &head, Tail &...tail) {\n\tscan(head);\n\tINPUT(tail...);\n}\n} // namespace scanner\n#define VEC(type, name, size) \\\n\tstd::vector<type> name(size); \\\n\tscanner::INPUT(name)\n#define VVEC(type, name, h, w) \\\n\tstd::vector<std::vector<type>> name(h, std::vector<type>(w)); \\\n\tscanner::INPUT(name)\n#define INT(...) \\\n\tint __VA_ARGS__; \\\n\tscanner::INPUT(__VA_ARGS__)\n#define LL(...) \\\n\tlong long __VA_ARGS__; \\\n\tscanner::INPUT(__VA_ARGS__)\n#define STR(...) \\\n\tstd::string __VA_ARGS__; \\\n\tscanner::INPUT(__VA_ARGS__)\n#define CHAR(...) \\\n\tchar __VA_ARGS__; \\\n\tscanner::INPUT(__VA_ARGS__)\n#define DOUBLE(...) \\\n\tdouble __VA_ARGS__; \\\n\tscanner::INPUT(__VA_ARGS__)\n#define LD(...) \\\n\tlong double __VA_ARGS__; \\\n\tscanner::INPUT(__VA_ARGS__)\n\n// output-macro\ntemplate <class T, class U>\nstd::ostream &operator<<(std::ostream &os, const std::pair<T, U> &p) {\n\tos << p.first << \" \" << p.second;\n\treturn os;\n}\ntemplate <class T>\nstd::ostream &operator<<(std::ostream &os, const std::vector<T> &a) {\n\tfor(int i = 0; i < int(a.size()); ++i) {\n\t\tif(i) os << \" \";\n\t\tos << a[i];\n\t}\n\treturn os;\n}\nstd::ostream &operator<<(std::ostream &dest, __int128_t &value) {\n\tstd::ostream::sentry s(dest);\n\tif(s) {\n\t\t__uint128_t tmp = value < 0 ? -value : value;\n\t\tchar buffer[128];\n\t\tchar *d = std::end(buffer);\n\t\tdo {\n\t\t\t--d;\n\t\t\t*d = \"0123456789\"[tmp % 10];\n\t\t\ttmp /= 10;\n\t\t} while(tmp != 0);\n\t\tif(value < 0) {\n\t\t\t--d;\n\t\t\t*d = '-';\n\t\t}\n\t\tint len = std::end(buffer) - d;\n\t\tif(dest.rdbuf()->sputn(d, len) != len) {\n\t\t\tdest.setstate(std::ios_base::badbit);\n\t\t}\n\t}\n\treturn dest;\n}\ntemplate <class T>\nvoid print(const T a) { std::cout << a << '\\n'; }\ntemplate <class Head, class... Tail>\nvoid print(Head H, Tail... T) {\n\tstd::cout << H << ' ';\n\tprint(T...);\n}\ntemplate <class T>\nvoid printel(const T a) { std::cout << a << '\\n'; }\ntemplate <class T>\nvoid printel(const std::vector<T> &a) {\n\tfor(const auto &v : a)\n\t\tstd::cout << v << '\\n';\n}\ntemplate <class Head, class... Tail>\nvoid printel(Head H, Tail... T) {\n\tstd::cout << H << '\\n';\n\tprintel(T...);\n}\nvoid Yes(const bool b = true) { std::cout << (b ? \"Yes\\n\" : \"No\\n\"); }\nvoid No() { std::cout << \"No\\n\"; }\nvoid YES(const bool b = true) { std::cout << (b ? \"YES\\n\" : \"NO\\n\"); }\nvoid NO() { std::cout << \"NO\\n\"; }\nvoid err(const bool b = true) {\n\tif(b) {\n\t\tstd::cout << \"-1\\n\", exit(0);\n\t}\n}\n\n//debug macro\nnamespace debugger {\ntemplate <class T>\nvoid view(const std::vector<T> &a) {\n\tstd::cerr << \"{ \";\n\tfor(const auto &v : a) {\n\t\tstd::cerr << v << \", \";\n\t}\n\tstd::cerr << \"\\b\\b }\";\n}\ntemplate <class T>\nvoid view(const std::vector<std::vector<T>> &a) {\n\tstd::cerr << \"{\\n\";\n\tfor(const auto &v : a) {\n\t\tstd::cerr << \"\\t\";\n\t\tview(v);\n\t\tstd::cerr << \"\\n\";\n\t}\n\tstd::cerr << \"}\";\n}\ntemplate <class T, class U>\nvoid view(const std::vector<std::pair<T, U>> &a) {\n\tstd::cerr << \"{\\n\";\n\tfor(const auto &p : a) std::cerr << \"\\t(\" << p.first << \", \" << p.second << \")\\n\";\n\tstd::cerr << \"}\";\n}\ntemplate <class T, class U>\nvoid view(const std::map<T, U> &m) {\n\tstd::cerr << \"{\\n\";\n\tfor(const auto &p : m) std::cerr << \"\\t[\" << p.first << \"] : \" << p.second << \"\\n\";\n\tstd::cerr << \"}\";\n}\ntemplate <class T, class U>\nvoid view(const std::pair<T, U> &p) { std::cerr << \"(\" << p.first << \", \" << p.second << \")\"; }\ntemplate <class T>\nvoid view(const std::set<T> &s) {\n\tstd::cerr << \"{ \";\n\tfor(auto &v : s) {\n\t\tview(v);\n\t\tstd::cerr << \", \";\n\t}\n\tstd::cerr << \"\\b\\b }\";\n}\n\ntemplate <class T>\nvoid view(const T &e) { std::cerr << e; }\n} // namespace debugger\n#ifdef LOCAL\nvoid debug_out() {}\ntemplate <typename Head, typename... Tail>\nvoid debug_out(Head H, Tail... T) {\n\tdebugger::view(H);\n\tstd::cerr << \", \";\n\tdebug_out(T...);\n}\n#define debug(...) \\\n\tdo { \\\n\t\tstd::cerr << __LINE__ << \" [\" << #__VA_ARGS__ << \"] : [\"; \\\n\t\tdebug_out(__VA_ARGS__); \\\n\t\tstd::cerr << \"\\b\\b]\\n\"; \\\n\t} while(false)\n#else\n#define debug(...) (void(0))\n#endif\n\n// vector macro\ntemplate <class T>\nint lb(const std::vector<T> &a, const T x) { return std::distance((a).begin(), std::lower_bound((a).begin(), (a).end(), (x))); }\ntemplate <class T>\nint ub(const std::vector<T> &a, const T x) { return std::distance((a).begin(), std::upper_bound((a).begin(), (a).end(), (x))); }\ntemplate <class T>\nvoid UNIQUE(std::vector<T> &a) {\n\tstd::sort(a.begin(), a.end());\n\ta.erase(std::unique(a.begin(), a.end()), a.end());\n}\ntemplate <class T>\nstd::vector<T> press(std::vector<T> &a) {\n\tauto res = a;\n\tUNIQUE(res);\n\tfor(auto &v : a)\n\t\tv = lb(res, v);\n\treturn res;\n}\n#define SORTname(a, b, c, ...) c\n#define SORT(...) SORTname(__VA_ARGS__, SORT1, SORT0, ...)(__VA_ARGS__)\n#define SORT0(a) std::sort((a).begin(), (a).end())\n#define SORT1(a, c) std::sort((a).begin(), (a).end(), [](const auto x, const auto y) { return x c y; })\ntemplate <class T>\nvoid ADD(std::vector<T> &a, const T x = 1) {\n\tfor(auto &v : a) v += x;\n}\ntemplate <class T>\nvoid SUB(std::vector<T> &a, const T x = 1) {\n\tfor(auto &v : a) v -= x;\n}\nstd::vector<std::pair<char, int>> rle(const string &s) {\n\tint n = s.size();\n\tstd::vector<std::pair<char, int>> ret;\n\tfor(int l = 0; l < n;) {\n\t\tint r = l + 1;\n\t\tfor(; r < n and s[l] == s[r]; r++) {}\n\t\tret.emplace_back(s[l], r - l);\n\t\tl = r;\n\t}\n\treturn ret;\n}\ntemplate <class T>\nstd::vector<std::pair<T, int>> rle(const std::vector<T> &v) {\n\tint n = v.size();\n\tstd::vector<std::pair<T, int>> ret;\n\tfor(int l = 0; l < n;) {\n\t\tint r = l + 1;\n\t\tfor(; r < n and v[l] == v[r]; r++) {}\n\t\tret.emplace_back(v[l], r - l);\n\t\tl = r;\n\t}\n\treturn ret;\n}\nstd::vector<int> iota(int n) {\n\tstd::vector<int> p(n);\n\tstd::iota(p.begin(), p.end(), 0);\n\treturn p;\n}\ntemplate <class T>\nstruct cum_vector {\npublic:\n\tcum_vector() = default;\n\ttemplate <class U>\n\tcum_vector(const std::vector<U> &vec) : cum((int)vec.size() + 1) {\n\t\tfor(int i = 0; i < (int)vec.size(); i++)\n\t\t\tcum[i + 1] = cum[i] + vec[i];\n\t}\n\tT prod(int l, int r) {\n\t\treturn cum[r] - cum[l];\n\t}\n\nprivate:\n\tstd::vector<T> cum;\n};\n\n// math macro\ntemplate <class T, class U>\ninline bool chmin(T &a, const U &b) { return a > b ? a = b, true : false; }\ntemplate <class T, class U>\ninline bool chmax(T &a, const U &b) { return a < b ? a = b, true : false; }\ntemplate <class T>\nT divup(T x, T y) { return (x + y - 1) / y; }\ntemplate <class T>\nT POW(T a, long long n) {\n\tT ret = 1;\n\twhile(n) {\n\t\tif(n & 1) ret *= a;\n\t\ta *= a;\n\t\tn >>= 1;\n\t}\n\treturn ret;\n}\n// modpow\nlong long POW(long long a, long long n, const int mod) {\n\tlong long ret = 1;\n\ta = (a % mod + mod) % mod;\n\twhile(n) {\n\t\tif(n & 1) (ret *= a) %= mod;\n\t\t(a *= a) %= mod;\n\t\tn >>= 1;\n\t}\n\treturn ret;\n}\ntemplate <class T, class F>\nT bin_search(T ok, T ng, const F &f) {\n\twhile(abs(ok - ng) > 1) {\n\t\tT mid = (ok + ng) >> 1;\n\t\t(f(mid) ? ok : ng) = mid;\n\t}\n\treturn ok;\n}\ntemplate <class T, class F>\nT bin_search(T ok, T ng, const F &f, int loop) {\n\tfor(int i = 0; i < loop; i++) {\n\t\tT mid = (ok + ng) / 2;\n\t\t(f(mid) ? ok : ng) = mid;\n\t}\n\treturn ok;\n}\n\n// others\nstruct fast_io {\n\tfast_io() {\n\t\tios::sync_with_stdio(false);\n\t\tcin.tie(nullptr);\n\t\tcout << fixed << setprecision(15);\n\t}\n} fast_io_;\nconst int inf = 1e9;\nconst ll INF = 1e18;\n#pragma endregion\n\nvoid main_();\n\nint main() {\n\tmain_();\n\treturn 0;\n}\n#line 7 \"f.cpp\"\n\nconst int dx[4] = {1, 0, -1, 0};\nconst int dy[4] = {0, 1, 0, -1};\nvoid main_() {\n\twhile(true) {\n\t\tINT(n);\n\t\tif(!n) break;\n\t\tusing T = tuple<int, int, int, int>;\n\t\tV<T> vec;\n\t\tV<> xp{-inf, inf}, yp{-inf, inf};\n\t\tREP(i, n) {\n\t\t\tINT(sx, gy, gx, sy);\n\t\t\txp.pb(sx);\n\t\t\txp.pb(gx);\n\t\t\typ.pb(sy);\n\t\t\typ.pb(gy);\n\t\t\tvec.pb(T(sx, sy, gx, gy));\n\t\t}\n\t\tauto rx = press(xp);\n\t\tauto ry = press(yp);\n\t\tint h = SZ(rx) - 1, w = SZ(ry) - 1;\n\t\tvector board(h, vector(w, 0));\n\t\tREP(k, n) {\n\t\t\tauto [sx, sy, gx, gy] = vec[k];\n\t\t\tsx = lb(rx, sx);\n\t\t\tgx = lb(rx, gx);\n\t\t\tsy = lb(ry, sy);\n\t\t\tgy = lb(ry, gy);\n\t\t\t//(sx, sy) - (gx,sy)\n\t\t\tREP(i, sx, gx) {\n\t\t\t\tboard[i][sy - 1] |= 1 << 1;\n\t\t\t\tboard[i][sy] |= 1 << 3;\n\t\t\t\tboard[i][gy - 1] |= 1 << 1;\n\t\t\t\tboard[i][gy] |= 1 << 3;\n\t\t\t}\n\t\t\tREP(i, sy, gy) {\n\t\t\t\tboard[sx - 1][i] |= 1 << 0;\n\t\t\t\tboard[sx][i] |= 1 << 2;\n\t\t\t\tboard[gx - 1][i] |= 1 << 0;\n\t\t\t\tboard[gx][i] |= 1 << 2;\n\t\t\t}\n\t\t}\n\t\tatcoder::dsu uf(h * w);\n\t\tREP(i, h) {\n\t\t\tREP(j, w) {\n\t\t\t\tint u = i * w + j;\n\t\t\t\tREP(k, 4) {\n\t\t\t\t\tif(!(board[i][j] >> k & 1)) {\n\t\t\t\t\t\tint nx = i + dx[k];\n\t\t\t\t\t\tint ny = j + dy[k];\n\t\t\t\t\t\tif(nx < 0 or nx >= h or ny < 0 or ny >= w) continue;\n\t\t\t\t\t\tint v = nx * w + ny;\n\t\t\t\t\t\tuf.merge(u, v);\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\tprint(SZ(uf.groups()));\n\t}\n}", "accuracy": 1, "time_ms": 30, "memory_kb": 3872, "score_of_the_acc": -0.012, "final_rank": 8 }, { "submission_id": "aoj_1337_6020675", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n#define rep(i,n) for(int (i)=0;(i)<(n);(i)++)\n#define ll long long\n#define pp pair<ll,ll>\n#define ld long double\n#define all(a) (a).begin(),(a).end()\n#define mk make_pair\n#define difine define\nconstexpr int inf=1000001000;\nconstexpr ll INF=2e18;\nconstexpr ll MOD=1000000007;\nconstexpr ll mod=998244353;\nint dx[4]={1,-1,-1,1},dy[4]={1,-1,1,-1};\n\nvector<int> a(4);\nstring s;\n \n// #include <atcoder/all>\n// typedef atcoder::modint1000000007 mint;\n// typedef atcoder::modint998244353 mint;\n\nint main() {\n while(true){\n int n;\n cin >> n;\n if (n==0) break;\n set<int> sx,sy;\n vector<int> l(n),t(n),r(n),b(n);\n rep(i,n){\n cin >> l[i] >> t[i] >> r[i] >> b[i];\n sx.insert(l[i]);\n sx.insert(r[i]);\n sy.insert(t[i]);\n sy.insert(b[i]);\n }\n map<int,int> mx,my;\n int x=1,y=1;\n for (auto it=sx.begin();it!=sx.end();it++){\n int f=*it;\n mx[f]=x;\n x++;\n }\n for (auto it=sy.begin();it!=sy.end();it++){\n int f=*it;\n my[f]=y;\n y++;\n }\n x+=2;\n y+=2;\n rep(i,n){\n l[i]=mx[l[i]];\n r[i]=mx[r[i]];\n t[i]=my[t[i]];\n b[i]=my[b[i]];\n }\n vector<vector<int>> d(x,vector<int>(y,0));\n int ans=0;\n rep(i,x){\n rep(j,y){\n if (d[i][j]==1) continue;\n // cout << i << \" \" << j << endl;\n ans++;\n d[i][j]=1;\n queue<pair<int,int>> q;\n q.push({i,j});\n while(!q.empty()){\n auto [f,g]=q.front();\n q.pop();\n if (f+1!=x){\n bool ok=true;\n if (d[f+1][g]==1) ok=false;\n rep(k,n){\n if (g>=b[k] && g<t[k] && (f+1==l[k] || f+1==r[k])) ok=false;\n }\n if (ok){\n q.push({f+1,g});\n d[f+1][g]=1;\n }\n }\n if (f-1!=-1){\n bool ok=true;\n if (d[f-1][g]==1) ok=false;\n rep(k,n){\n if (g>=b[k] && g<t[k] && (f==l[k] || f==r[k])) ok=false;\n }\n if (ok){\n q.push({f-1,g});\n d[f-1][g]=1;\n }\n }\n if (g+1!=y){\n bool ok=true;\n if (d[f][g+1]==1) ok=false;\n rep(k,n){\n if (f>=l[k] && f<r[k] && (g+1==b[k] || g+1==t[k])) ok=false;\n }\n if (ok){\n q.push({f,g+1});\n d[f][g+1]=1;\n }\n }\n if (g-1!=-1){\n bool ok=true;\n if (d[f][g-1]==1) ok=false;\n rep(k,n){\n if (f>=l[k] && f<r[k] && (g==b[k] || g==t[k])) ok=false;\n }\n if (ok){\n q.push({f,g-1});\n d[f][g-1]=1;\n }\n }\n }\n }\n }\n cout << ans << endl;\n }\n}", "accuracy": 1, "time_ms": 290, "memory_kb": 3136, "score_of_the_acc": -0.0458, "final_rank": 18 }, { "submission_id": "aoj_1337_6020497", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\nusing P = pair<int,int>;\n\nint main() {\n \n ios::sync_with_stdio(false);\n cin.tie(0);\n \n vector<int> di = {1,0,-1,0};\n vector<int> dj = {0,1,0,-1};\n int n;\n cin >> n;\n while (n != 0) {\n vector<int> l(n),t(n),r(n),b(n);\n for (int i=0;i<n;i++) {\n cin >> l[i] >> t[i] >> r[i] >> b[i];\n }\n vector<int> xs;\n vector<int> ys;\n for (int i=0;i<n;i++) {\n xs.emplace_back(l[i]);\n xs.emplace_back(r[i]);\n ys.emplace_back(t[i]);\n ys.emplace_back(b[i]);\n }\n xs.emplace_back(-100);\n xs.emplace_back(1e7);\n ys.emplace_back(-100);\n ys.emplace_back(1e7);\n sort(xs.begin(),xs.end());\n sort(ys.begin(),ys.end());\n auto itx = unique(xs.begin(),xs.end());\n auto ity = unique(ys.begin(),ys.end());\n xs.erase(itx,xs.end());\n ys.erase(ity,ys.end());\n int nx = xs.size();\n int ny = ys.size();\n vector<vector<long long>> maps(nx-1,vector<long long>(ny-1,0));\n for (int i=0;i<n;i++) {\n for (int j=0;j<nx-1;j++) {\n for (int k=0;k<ny-1;k++) {\n if (xs[j] >= l[i] && r[i] >= xs[j+1] && ys[k] >= b[i] && t[i] >= ys[k+1]) {\n maps[j][k] += 1LL<<i;\n }\n }\n }\n }\n vector<vector<bool>> used(nx-1,vector<bool>(ny-1,false));\n int ans = 0;\n for (int i=0;i<nx-1;i++) {\n for (int j=0;j<ny-1;j++) {\n if (used[i][j]) continue;\n ans++;\n queue<P> q;\n q.push(P(i,j));\n used[i][j] = true;\n while (!q.empty()) {\n P np = q.front();\n q.pop();\n int ni = np.first;\n int nj = np.second;\n //cout << maps[ni][nj] << endl;\n for (int k=0;k<4;k++) {\n int toi = ni+di[k];\n int toj = nj+dj[k];\n if (toi < 0 || toi >= nx-1 || toj < 0 || toj >= ny-1) {\n continue;\n }\n if (maps[toi][toj] != maps[ni][nj]) continue;\n if (used[toi][toj]) continue;\n used[toi][toj] = true;\n q.push(P(toi,toj));\n }\n }\n }\n }\n cout << ans << endl;\n cin >> n;\n }\n}", "accuracy": 1, "time_ms": 60, "memory_kb": 3496, "score_of_the_acc": -0.0124, "final_rank": 9 }, { "submission_id": "aoj_1337_6001211", "code_snippet": "#define rep(i, n) for(int i=0; i<(n); ++i)\n#define rrep(i, n) for(int i=(n-1); i>=0; --i)\n#define rep2(i, s, n) for(int i=s; i<(n); ++i)\n#define ALL(v) (v).begin(), (v).end()\n#define memr(dp, val) memset(dp, val, sizeof(dp))\n#define fi first\n#define se second\ntemplate<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }\ntemplate<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }\n#include <bits/stdc++.h>\nusing namespace std;\ntemplate<typename T>\nusing min_priority_queue = priority_queue<T, vector<T>, greater<T> >;\ntypedef long long ll;\nconst int INTINF = INT_MAX >> 1;\nstatic const ll LLINF = (LLONG_MAX >> 1);\n\nint dx[4] = {0, 0, -1, 1};\nint dy[4] = {1, -1, 0, 0};\n\nint mx, my;\n\nint n;\nint l[51], t[51], r[51], b[51];\n\nset<int> s[501][501];\nbool used[501][501];\n\nbool input(){\n\n cin >> n;\n if(!n) return false;\n set<int> xs, ys;\n xs.insert(0);\n xs.insert(1000000);\n ys.insert(0);\n ys.insert(1000000);\n map<int, int> xcomp, ycomp;\n rep(i, n) {\n cin >> l[i] >> t[i] >> r[i] >> b[i];\n xs.insert(l[i]);\n xs.insert(r[i]);\n ys.insert(t[i]);\n ys.insert(b[i]);\n }\n\n int cnt = 1;\n int bef = -1;\n for(int x : xs) {\n xcomp[x] = cnt;\n cnt++;\n bef = x;\n }\n bef = -1;\n cnt = 1;\n for(int y : ys) {\n ycomp[y] = cnt;\n cnt++;\n bef = y;\n }\n\n rep(i, n){\n l[i] = xcomp[l[i]];\n r[i] = xcomp[r[i]];\n t[i] = ycomp[t[i]];\n b[i] = ycomp[b[i]];\n }\n mx = xcomp[1000000] + 2;\n my = ycomp[1000000] + 2;\n\n\treturn true;\n}\n\nint main(){\n std::cout << std::fixed << std::setprecision(15);\n\n while(input()){\n rep(i, my){\n rep(j, mx){\n s[i][j].clear();\n }\n }\n\n rep(i, n){\n rep(j, r[i] - l[i]){\n rep(k, t[i] - b[i]){\n s[b[i] + k][l[i] + j].insert(i);\n }\n }\n }\n\n memr(used, 0);\n int ans = 0;\n rep(i, my){\n rep(j, mx){\n if(!used[i][j]){\n ans++;\n queue<pair<int, int> > q;\n q.push({j, i});\n used[i][j] = true;\n while(q.size()){\n auto f = q.front(); q.pop();\n rep(k, 4){\n int nx = f.fi + dx[k];\n int ny = f.se + dy[k];\n if(nx < 0 || nx >= mx) continue;\n if(ny < 0 || ny >= my) continue;\n if(used[ny][nx]) continue;\n if(s[ny][nx] != s[f.se][f.fi]) continue;\n\n used[ny][nx] = true;\n q.push({nx, ny});\n }\n }\n }\n }\n }\n cout << ans << endl;\n }\n return 0;\n}", "accuracy": 1, "time_ms": 460, "memory_kb": 22960, "score_of_the_acc": -0.3079, "final_rank": 19 }, { "submission_id": "aoj_1337_5964938", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\nusing ll = long long;\nvector<int> dx = {1, 0, -1, 0}, dy = {0, 1, 0, -1};\n\n// 座標圧縮\nmap<ll, int> compress(const vector<ll> &x) {\n map<ll, int> comp;\n for (int i = 0; i < x.size(); i++) comp[x.at(i)] = 1;\n int i = 0;\n for (auto it = comp.begin(); it != comp.end(); it++, i++) it->second = i;\n return comp;\n}\n\nvoid bfs(vector<vector<bool>> &dp, int si, int sj) {\n dp[si][sj] = true;\n queue<pair<int, int>> q;\n q.push(make_pair(si, sj));\n while (!q.empty()) {\n pair<int, int> p = q.front();\n q.pop();\n for (int i = 0; i < 4; i++) {\n int ni = p.first + dx[i], nj = p.second + dy[i];\n if (0 <= ni && ni < dp.size() && 0 <= nj && nj < dp[0].size() && !dp[ni][nj]) {\n dp[ni][nj] = true;\n q.push(make_pair(ni, nj));\n }\n }\n }\n}\n\nint main() {\n cin.tie(0);\n ios::sync_with_stdio(false);\n\n while (true) {\n int n;\n cin >> n;\n if (n == 0) return 0;\n\n vector<ll> l(n), t(n), r(n), b(n), x, y;\n for (int i = 0; i < n; i++) {\n cin >> l[i] >> t[i] >> r[i] >> b[i];\n x.emplace_back(l[i]);\n x.emplace_back(r[i]);\n y.emplace_back(t[i]);\n y.emplace_back(b[i]);\n }\n x.emplace_back(-10);\n x.emplace_back(10000000);\n y.emplace_back(-10);\n y.emplace_back(10000000);\n\n map<ll, int> mpx = compress(x), mpy = compress(y);\n\n vector<vector<bool>> dp((int) mpx.size() * 3, vector<bool>((int) mpy.size() * 3, false));\n for (int i = 0; i < n; i++) {\n for (int ii = mpx[l[i]] * 3 + 1; ii <= mpx[r[i]] * 3 + 1; ii++) {\n dp[ii][mpy[b[i]] * 3 + 1] = true;\n dp[ii][mpy[t[i]] * 3 + 1] = true;\n }\n for (int jj = mpy[b[i]] * 3 + 1; jj <= mpy[t[i]] * 3 + 1; jj++) {\n dp[mpx[l[i]] * 3 + 1][jj] = true;\n dp[mpx[r[i]] * 3 + 1][jj] = true;\n }\n }\n\n int ans = 0;\n for (int i = 0; i < dp.size(); i++) {\n for (int j = 0; j < dp[0].size(); j++) {\n if (!dp[i][j]) {\n bfs(dp, i, j);\n ans++;\n }\n }\n }\n\n cout << ans << '\\n';\n }\n}", "accuracy": 1, "time_ms": 140, "memory_kb": 3428, "score_of_the_acc": -0.0247, "final_rank": 14 }, { "submission_id": "aoj_1337_5963211", "code_snippet": "// #include \"atcoder/all\"\n#include <iostream> // cout, endl, cin\n#include <string> // string, to_string, stoi\n#include <vector> // vector\n#include <algorithm> // min, max, swap, sort, reverse, lower_bound, upper_bound\n#include <utility> // pair, make_pair\n#include <tuple> // tuple, make_tuple\n#include <cstdint> // int64_t, int*_t\n#include <cstdio> // printf\n#include <map> // map\n#include <queue> // queue, priority_queue\n#include <set> // set\n#include <stack> // stack\n#include <deque> // deque\n#include <unordered_map> // unordered_map\n#include <unordered_set> // unordered_set\n#include <bitset> // bitset\n#include <cctype> // isupper, islower, isdigit, toupper, tolower\n#include <iomanip> // setprecision\n#include <complex> // complex\n#include <math.h>\n#include <functional>\n#include <cassert>\nusing namespace std;\n// using namespace atcoder;\nusing ll = long long;\nusing P = pair<ll,ll>;\nconstexpr ll INF = 1e15;\nconstexpr ll LLMAX = 9223372036854775807;\nconstexpr int inf = 1e9;\n// constexpr ll mod = 1000000007;\nconstexpr ll mod = 998244353;\n// 右下左上\nconst int dx[8] = {1, 0, -1, 0,1,1,-1,-1};\nconst int dy[8] = {0, 1, 0, -1,1,-1,1,-1};\ntemplate<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }\ntemplate<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }\n#define eol \"\\n\"\n// ---------------------------------------------------------------------------\n\ntemplate<typename T>\nstruct Compress {\n\tvector<T> v;\n\tCompress() {}\n\tCompress(vector<T> _v) {\n\t\tbuild(_v);\n\t}\n \n\tvoid build(vector<T> _v) {\n\t\tv = _v;\n\t\tsort(v.begin(), v.end());\n\t\tv.erase(unique(v.begin(), v.end()), v.end());\n\t}\n\tint get(T x) {\n\t\treturn lower_bound(v.begin(), v.end(), x) - v.begin();\n\t}\n \n\tT& operator[](int i) { return v[i]; }\n \n \n\tint size() {\n\t\treturn (int)v.size();\n\t}\n};\n\n\nstruct UnionFind {\n vector<int> par, w;\n\n UnionFind(int n) : par(n, -1), w(n, 0) { }\n void init(int n) { par.assign(n, -1); w.assign(n, 0); }\n\n int root(int x) {\n if (par[x] < 0) return x;\n else return par[x] = root(par[x]);\n }\n\n bool issame(int x, int y) {\n return root(x) == root(y);\n }\n\n bool merge(int x, int y) {\n x = root(x); y = root(y);\n if (x == y) {\n ++w[x];\n return false;\n }\n if (par[x] > par[y]) swap(x, y); // merge technique\n par[x] += par[y];\n par[y] = x;\n w[x] += w[y];\n ++w[x];\n return true;\n }\n\n int size(int x) {\n return -par[root(x)];\n }\n\n int wei(int x) {\n return w[root(x)];\n }\n};\n\n\nbool solve(){\n int N;\n cin >> N;\n if(N == 0) return false;\n vector<int> X,Y;\n vector<int> lx(N),ly(N),rx(N),ry(N);\n for(int i=0; i<N; i++){\n cin >> lx[i] >> ry[i] >> rx[i] >> ly[i];\n X.emplace_back(lx[i]);\n X.emplace_back(rx[i]);\n Y.emplace_back(ly[i]);\n Y.emplace_back(ry[i]);\n }\n X.emplace_back(-1);\n X.emplace_back(inf);\n Y.emplace_back(-1);\n Y.emplace_back(inf);\n Compress<int> CX(X),CY(Y);\n vector<vector<ll>> G(CX.size()+1,vector<ll>(CY.size()+1));\n for(int i=0; i<N; i++){\n for(int x=CX.get(lx[i]); x<CX.get(rx[i]); x++){\n for(int y=CY.get(ly[i]); y<CY.get(ry[i]); y++){\n G[x][y] += (1LL<<i);\n }\n }\n }\n // for(int i=0; i<CX.size(); i++){\n // for(int j=0; j<CY.size(); j++){\n // cout << G[i][j] << \" \\n\"[j+1==CY.size()];\n // }\n // }\n int ans = 0;\n vector<vector<int>> used(CX.size(),vector<int>(CY.size(),0));\n for(int i=0; i<CX.size(); i++){\n for(int j=0; j<CY.size(); j++){\n if(used[i][j] != 0) continue;\n ans++;\n queue<P> que;\n que.emplace(i,j);\n used[i][j] = ans;\n while(que.size()){\n int y = que.front().first;\n int x = que.front().second;\n que.pop();\n for(int k=0; k<4; k++){\n int ny = y + dy[k];\n int nx = x + dx[k];\n if(ny<0 || ny>=CX.size() || nx<0 || nx>=CY.size()) continue;\n if(G[y][x] != G[ny][nx]) continue;\n if(used[ny][nx] != 0) continue;\n used[ny][nx] = ans;\n que.emplace(ny,nx);\n }\n }\n }\n }\n cout << ans << endl;\n return true;\n}\n\nint main(){\n ios::sync_with_stdio(false);\n cin.tie(nullptr);\n while(solve());\n return 0;\n}", "accuracy": 1, "time_ms": 50, "memory_kb": 3428, "score_of_the_acc": -0.01, "final_rank": 6 }, { "submission_id": "aoj_1337_5956478", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\nusing T = tuple<int,int,int,int>;\nconst int MAX = 500;\n\nmap<int64_t,int64_t> mx;\nmap<int64_t,int64_t> my;\n\nset<int> sum[MAX][MAX];\nvoid init(){\n for(int i=0; i<MAX; i++){\n for(int j=0; j<MAX; j++){\n sum[i][j].clear();\n }\n }\n}\n\n//一次元座標圧縮\nmap<int64_t, int64_t> compression(vector<int64_t> V){\n map<int64_t, int64_t> ret;\n int64_t cnt = 0;\n sort(V.begin(),V.end());\n for(int i=0; i<V.size(); i++){\n if(!ret.count(V[i])){\n ret[V[i]] = cnt;\n cnt++;\n }\n }\n return ret;\n}\n\n//UnionFind\nint n = 500*500;\nvector<int> par(n);\nvector<int> Size(n);\n\nvoid init_union(int n){\n for(int i=0; i<n; i++){\n par.at(i) = i;\n Size.at(i) = 1;\n }\n}\n\nint root(int x){\n if(par.at(x) == x){\n return x;\n }\n else{\n return par.at(x) = root(par.at(x));\n }\n}\n\nbool same(int x, int y){\n return root(x) == root(y);\n}\n\nvoid unite(int x, int y){\n x = root(x);\n y = root(y);\n if(x == y){\n return;\n }\n if (Size.at(x) < Size.at(y)){\n swap(x, y);\n }\n Size.at(x) += Size.at(y);\n par.at(y) = x;\n}\n\nint Size_x(int x){\n return Size.at(root(x));\n}\n\nvoid solve(int N){\n init();\n vector<T> vec(N);\n vector<int64_t> X = {-1000000000, 1000000000};\n vector<int64_t> Y = {-1000000000, 1000000000};\n for(int i=0; i<N; i++){\n int x1, y1, x2, y2;\n cin >> x1 >> y2 >> x2 >> y1;\n x2--; y2--;\n vec[i] = {x1,y1,x2,y2};\n for(int j=-1; j<=1; j++){\n X.push_back(x1+j);\n X.push_back(x2+j);\n Y.push_back(y1+j);\n Y.push_back(y2+j);\n }\n }\n mx = compression(X);\n my = compression(Y);\n for(int i=0; i<N; i++){\n auto[x1,y1,x2,y2] = vec[i];\n for(auto itr = mx.begin(); itr != mx.end(); itr++){\n int x = itr->first;\n for(auto itr2 = my.begin(); itr2 != my.end(); itr2++){\n int y = itr2->first;\n if(x1 <= x && x <= x2 && y1 <= y && y <= y2){\n sum[itr->second][itr2->second].insert(i);\n }\n }\n }\n }\n init_union(n);\n vector<int> dx = {0,1,0,-1};\n vector<int> dy = {1,0,-1,0};\n for(int i=0; i<MAX; i++){\n for(int j=0; j<MAX; j++){\n for(int k=0; k<4; k++){\n if(0 <= i+dx[k] && i+dx[k] < MAX-1 && 0 <= j+dy[k] && j+dy[k] < MAX-1){\n if(sum[i][j] == sum[i+dx[k]][j+dy[k]]){\n unite(i*MAX+j, (i+dx[k])*MAX + j+dy[k]);\n }\n }\n }\n }\n }\n int ans = 0;\n for(int i=0; i<X.size(); i++){\n for(int j=0; j<Y.size(); j++){\n if(root(i*MAX+j) == i*MAX+j){\n ans++;\n }\n }\n }\n cout << ans << endl;\n \n /*for(int i=0; i<X.size(); i++){\n for(int j=0; j<Y.size(); j++){\n cout << num[i][j] << \" \";\n }\n cout << endl;\n }*/\n}\n\nint main(){\n while(true){\n int N;\n cin >> N;\n if(N == 0){\n break;\n }\n solve(N);\n //break;\n }\n}", "accuracy": 1, "time_ms": 6120, "memory_kb": 87764, "score_of_the_acc": -2, "final_rank": 20 }, { "submission_id": "aoj_1337_5927964", "code_snippet": "#line 1 \"main.cpp\"\n#pragma region Macros\n#include <bits/stdc++.h>\nusing namespace std;\ntemplate <class T> inline bool chmax(T &a, T b) {\n if(a < b) {\n a = b;\n return 1;\n }\n return 0;\n}\ntemplate <class T> inline bool chmin(T &a, T b) {\n if(a > b) {\n a = b;\n return 1;\n }\n return 0;\n}\n#ifdef DEBUG\ntemplate <class T, class U>\nostream &operator<<(ostream &os, const pair<T, U> &p) {\n os << '(' << p.first << ',' << p.second << ')';\n return os;\n}\ntemplate <class T> ostream &operator<<(ostream &os, const vector<T> &v) {\n os << '{';\n for(int i = 0; i < (int)v.size(); i++) {\n if(i) { os << ','; }\n os << v[i];\n }\n os << '}';\n return os;\n}\nvoid debugg() { cerr << endl; }\ntemplate <class T, class... Args>\nvoid debugg(const T &x, const Args &... args) {\n cerr << \" \" << x;\n debugg(args...);\n}\n#define debug(...) \\\n cerr << __LINE__ << \" [\" << #__VA_ARGS__ << \"]: \", debugg(__VA_ARGS__)\n#define dump(x) cerr << __LINE__ << \" \" << #x << \" = \" << (x) << endl\n#else\n#define debug(...) (void(0))\n#define dump(x) (void(0))\n#endif\n\nstruct Setup {\n Setup() {\n cin.tie(0);\n ios::sync_with_stdio(false);\n cout << fixed << setprecision(15);\n }\n} __Setup;\n\nusing ll = long long;\n#define ALL(v) (v).begin(), (v).end()\n#define RALL(v) (v).rbegin(), (v).rend()\n#define FOR(i, a, b) for(int i = (a); i < int(b); i++)\n#define REP(i, n) FOR(i, 0, n)\nconst int INF = 1 << 30;\nconst ll LLINF = 1LL << 60;\nconstexpr int MOD = 1000000007;\n\nvoid Case(int i) { cout << \"Case #\" << i << \": \"; }\nint popcount(int x) { return __builtin_popcount(x); }\nll popcount(ll x) { return __builtin_popcountll(x); }\n#pragma endregion Macros\n\nconst int dx[4] = {1, 0, -1, 0};\nconst int dy[4] = {0, 1, 0, -1};\n\n#line 1 \"/home/siro53/kyo-pro/compro_library/data_structure/compress.hpp\"\ntemplate <typename T> struct Compress {\n vector<T> v;\n Compress() {}\n Compress(vector<T> vv) : v(vv) {\n sort(ALL(v));\n v.erase(unique(ALL(v)), end(v));\n }\n void build(vector<T> vv) {\n v = vv;\n sort(ALL(v));\n v.erase(unique(ALL(v)), end(v));\n }\n int get(T x) { return (int)(lower_bound(ALL(v), x) - v.begin()); }\n T &operator[](int i) { return v[i]; }\n size_t size() { return v.size(); }\n};\n#line 1 \"/home/siro53/kyo-pro/compro_library/graph/dsu.hpp\"\nstruct UnionFind {\n vector<int> par;\n\n UnionFind(int n) : par(n, -1) {}\n void init(int n) { par.assign(n, -1); }\n\n int root(int x) {\n if(par[x] < 0)\n return x;\n else\n return par[x] = root(par[x]);\n }\n\n bool issame(int x, int y) { return root(x) == root(y); }\n\n bool merge(int x, int y) {\n x = root(x);\n y = root(y);\n if(x == y)\n return false;\n if(par[x] > par[y])\n swap(x, y); // merge technique\n par[x] += par[y];\n par[y] = x;\n return true;\n }\n\n int size(int x) { return -par[root(x)]; }\n};\n#line 74 \"main.cpp\"\n\nusing R = array<int, 4>;\n\nbool solve() {\n int N;\n cin >> N;\n if(N == 0) return false;\n vector<R> recs(N);\n Compress<int> comp_x, comp_y;\n {\n vector<int> vx = {-1, 1000001}, vy = {-1, 1000001};\n REP(i, N) {\n int x1, y1, x2, y2;\n cin >> x1 >> y2 >> x2 >> y1;\n vx.push_back(x1);\n vy.push_back(y1);\n vx.push_back(x2);\n vy.push_back(y2);\n recs[i] = {x1, y1, x2, y2};\n }\n comp_x.build(vx); comp_y.build(vy);\n REP(i, N) {\n recs[i][0] = comp_x.get(recs[i][0]) * 2;\n recs[i][1] = comp_y.get(recs[i][1]) * 2;\n recs[i][2] = comp_x.get(recs[i][2]) * 2;\n recs[i][3] = comp_y.get(recs[i][3]) * 2;\n }\n }\n int Lx = comp_x.size() * 2;\n int Ly = comp_y.size() * 2;\n vector G(Lx, vector<int>(Ly, 0));\n REP(i, N) {\n auto [x1, y1, x2, y2] = recs[i];\n FOR(y, y1, y2+1) G[x1][y] = G[x2][y] = 1;\n FOR(x, x1, x2+1) G[x][y1] = G[x][y2] = 1;\n }\n UnionFind uf(Lx*Ly);\n\n REP(i, Lx) REP(j, Ly) {\n if(G[i][j]) continue;\n REP(dir, 4) {\n int ni = i + dx[dir], nj = j + dy[dir];\n if(ni < 0 or ni >= Lx or nj < 0 or nj >= Ly) continue;\n if(G[ni][nj]) continue;\n uf.merge(i*Ly + j, ni*Ly + nj);\n }\n }\n set<int> se;\n REP(i, Lx) REP(j, Ly) {\n if(G[i][j]) continue;\n se.insert(uf.root(i*Ly + j));\n }\n cout << se.size() << endl;\n\n return true;\n}\n\nint main() {\n while(solve());\n}", "accuracy": 1, "time_ms": 100, "memory_kb": 3780, "score_of_the_acc": -0.0223, "final_rank": 12 }, { "submission_id": "aoj_1337_5926769", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\nusing ll = long long int;\nusing ull = unsigned long long int;\nusing P = pair<int, int>;\nusing P3 = pair<P,int>;\nusing PP = pair<P, P>;\nconstexpr int INF32 = 1 << 30;\nconstexpr ll INF64 = 1LL << 60;\nconstexpr ll MOD = 1000000007;\n// constexpr ll MOD = 998244353;\nconstexpr int di[] = {0, 1, 0, -1};\nconstexpr int dj[] = {1, 0, -1, 0};\nconstexpr int di8[] = {0, 1, 1, 1, 0, -1, -1, -1};\nconstexpr int dj8[] = {1, 1, 0, -1, -1, -1, 0, 1};\nconstexpr double EPS = 1e-9;\nconst double PI = acos(-1);\n\n#define ALL(v) (v).begin(),(v).end()\n#define REP(i,n) for(int i=0,i_len=n; i<i_len; ++i)\n\ntemplate<typename T1,typename T2> bool chmax(T1 &a, T2 b) { if (a<b) { a=b; return 1; } return 0; }\ntemplate<typename T1,typename T2> bool chmin(T1 &a, T2 b) { if (b<a) { a=b; return 1; } return 0; }\n\nstruct UnionFindTree {\n vector<int> par;\n vector<int> rank;\n vector<int> siz;\n int N;\n\n void init(int n) {\n par.resize(n);\n rank.resize(n);\n siz.resize(n);\n for (int i = 0; i < n; i++) {\n par[i] = i;\n rank[i] = 0;\n siz[i] = 1;\n N = n;\n }\n }\n int find(int x) {\n if (par[x] == x) {\n return x;\n } else {\n return par[x] = find(par[x]);\n }\n }\n void unite(int x, int y) {\n x = find(x);\n y = find(y);\n if (x == y) return;\n if (rank[x] < rank[y]) swap(x, y);\n if (rank[x] == rank[y]) rank[x]++;\n par[y] = x;\n siz[x] += siz[y];\n N--;\n }\n bool is_same(int x, int y) {\n return find(x) == find(y);\n }\n int size(int x) {\n x = find(x);\n return siz[x];\n }\n};\n\nmap<int,int> compress(vector<int> x){\n auto v = x;\n sort(ALL(v));\n v.erase(unique(ALL(v)),v.end());\n map<int,int> ord;\n for(int i=0;i<v.size();i++) ord[v[i]] = i;\n return ord;\n}\n\nint solve(){\n int n;\n cin >> n;\n if(n == 0) return 1;\n vector<int> xs, ys, l(n), t(n), r(n), b(n);\n REP(i,n){\n cin >> l[i] >> t[i] >> r[i] >> b[i];\n xs.push_back(l[i]);\n xs.push_back(r[i]);\n ys.push_back(t[i]);\n ys.push_back(b[i]);\n }\n auto xord = compress(xs);\n auto yord = compress(ys);\n int m = max(xord.size(), yord.size()) + 1;\n REP(i,n){\n l[i] = xord[l[i]]+1;\n r[i] = xord[r[i]]+1;\n t[i] = yord[t[i]]+1;\n b[i] = yord[b[i]]+1;\n }\n vector<vector<ll> > a(m+1, vector<ll>(m+1));\n REP(i,n){\n a[l[i]][b[i]] |= 1LL<<i;\n a[l[i]][t[i]] |= 1LL<<i;\n a[r[i]][b[i]] |= 1LL<<i;\n a[r[i]][t[i]] |= 1LL<<i;\n }\n REP(i,m+1) REP(j,m) a[i][j+1] ^= a[i][j];\n REP(i,m) REP(j,m+1) a[i+1][j] ^= a[i][j];\n UnionFindTree uf;\n uf.init((m+1)*(m+1));\n REP(i,m+1) REP(j,m+1){\n if(i<m && a[i][j]==a[i+1][j]) uf.unite(i*(m+1)+j, (i+1)*(m+1)+j);\n if(j<m && a[i][j]==a[i][j+1]) uf.unite(i*(m+1)+j, i*(m+1)+j+1);\n }\n cout << uf.N << endl;\n return 0;\n}\n\nint main(){\n cin.tie(nullptr);\n ios::sync_with_stdio(false);\n // solve();\n // int T; cin >> T; REP(t,T) solve();\n while(!solve());\n return 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3328, "score_of_the_acc": -0.0023, "final_rank": 1 } ]
aoj_1344_cpp
Problem J: C(O|W|A*RD*|S)* CROSSWORD Puzzle The first crossword puzzle was published on December 21, 1913 by Arthur Wynne. To celebrate the centennial of his great-great-grandfather's invention, John "Coward" Wynne 1 was struggling to make crossword puzzles. He was such a coward that whenever he thought of a tricky clue for a word, he couldn’t stop worrying if people would blame him for choosing a bad clue that could never mean that word. At the end of the day, he cowardly chose boring clues, which made his puzzles less interesting. One day, he came up with a brilliant idea: puzzles in which word meanings do not matter, and yet interesting. He told his idea to his colleagues, who admitted that the idea was intriguing. They even named his puzzles "Coward's Crossword Puzzles" after his nickname. However, making a Coward's crossword puzzle was not easy. Even though he did not have to think about word meanings, it was not easy to check if a puzzle has one and only one set of answers. As the day of the centennial anniversary was approaching, John started worrying if he would not be able to make interesting ones in time. Let's help John by writing a program that solves Coward's crossword puzzles. Each puzzle consists of h × w cells along with h across clues and w down clues. The clues are regular expressions written in a pattern language whose BNF syntax is given as in the table below. clue ::= "^" pattern "$" pattern ::= simple | pattern "|" simple simple ::= basic | simple basic basic ::= elementary | elementary "*" elementary ::= "." | "A" | "B" | ... | "Z" | "(" pattern ")" Table J.1. BNF syntax of the pattern language. The clues (as denoted by p and q below) match words (as denoted by s below) according to the following rules. ^ p $ matches s if p matches s . p | q matches a string s if p and/or q matches s . pq matches a string s if there exist s 1 and s 2 such that s 1 s 2 = s , p matches s 1 , and q matches s 2 . p * matches a string s if s is empty, or there exist s 1 and s 2 such that s 1 s 2 = s , p matches s 1 , and p * matches s 2 . Each of A , B , . . . , Z matches the respective letter itself. ( p ) matches s if p matches s . . is the shorthand of (A|B|C|D|E|F|G|H|I|J|K|L|M|N|O|P|Q|R|S|T|U|V|W|X|Y|Z) . Below is an example of a Coward’s crossword puzzle with the answers filled in the cells. Java Specific: Submitted Java programs may not use classes in the java.util.regex package. C++ Specific: Submitted C++ programs may not use the std::regex class. Input The input consists of multiple datasets, each of which represents a puzzle given in the following format. h w p 1 p 2 ... p h q 1 q 2 ... q 2 Here, h and w represent the vertical and horizontal numbers of cells, respectively, where 2 ≤ h,w ≤ 4. The p i and q j are the across and down clues for the i -th row and j -th column, respectively. Any clue has no more than 512 characters. The last dataset is followed by a line of two zeros. You may assume that there are no more ...(truncated)
[ { "submission_id": "aoj_1344_5432770", "code_snippet": "#include <iostream>\n#include <string>\n#include <vector>\n#include <cstring>\n#include <algorithm>\n#include <cctype>\n#include <cassert>\n#include <array>\n#include <set>\nusing namespace std;\n\n#define ALL(v) begin(v),end(v)\n\nnamespace{\n\nstruct flags{\n\tusing gtype = unsigned short;\n\tvector<gtype> v;\n\tgtype gen;\n\tvoid init(int sz){\n\t\tv.assign(sz, 0);\n\t\tgen = 1;\n\t}\n\tbool marked(int i){ return v[i] == gen; }\n\tvoid mark(int i){ v[i] = gen; }\n\tvoid reset(){\n\t\t++gen;\n\t\tif(gen == 0){\n\t\t\tmemset(&v[0], 0, v.size() * sizeof(gtype));\n\t\t\tgen = 1;\n\t\t}\n\t}\n};\n\nusing node = array<vector<int>, 27>;\n\nstruct parser{\n\tvector<node> nodes;\n\tconst char *p;\n\tflags fs;\n\tvector<int> res;\n\tvector<int> tmpv[8];\n\tvector<int> cands(string s, int len){\n\t\treverse(ALL(s));\n\t\tp = s.c_str() + 1;\n\t\tnodes.reserve(1100);\n\t\tnodes.emplace_back();\n\t\tint start = pattern(0);\n\t\tassert(*p == '^');\n\t\tfs.init(nodes.size());\n\t\tvector<int> us = {start};\n\t\tdfs(us, 0, len);\n\n\t\treturn res;\n\t}\n\tvoid dfs(vector<int> &us, int bits, int rem){\n\t\tif(us.empty()){ return; }\n\t\taddeps(us);\n\t\tif(rem == 0){\n\t\t\tfor(int u : us){\n\t\t\t\tif(u == 0){\n\t\t\t\t\tres.push_back(bits);\n\t\t\t\t\treturn;\n\t\t\t\t}\n\t\t\t}\n\t\t\treturn;\n\t\t}\n\t\tvector<int> &vs = tmpv[rem];\n\t\tfor(int i = 1; i <= 26; ++i){\n\t\t\tvs.clear();\n\t\t\tfs.reset();\n\t\t\tfor(int u : us){\n\t\t\t\tfor(int v : nodes[u][i]){\n\t\t\t\t\tif(!fs.marked(v)){\n\t\t\t\t\t\tfs.mark(v);\n\t\t\t\t\t\tvs.push_back(v);\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t\tdfs(vs, bits << 5 | i, rem - 1);\n\t\t}\n\t}\n\tvoid addeps(vector<int> &us){\n\t\tfs.reset();\n\t\tfor(size_t i = 0; i < us.size(); ++i){\n\t\t\tfor(int v : nodes[us[i]][0]){\n\t\t\t\tif(!fs.marked(v)){\n\t\t\t\t\tfs.mark(v);\n\t\t\t\t\tus.push_back(v);\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n\tint pattern(int u){\n\t\tconst char *sp = p;\n\t\tint v = simple(u);\n\t\tset<string> dup; // ad-hoc optimization\n\t\tdup.emplace(sp, p);\n\t\twhile(*p == '|'){\n\t\t\t++p;\n\t\t\tsp = p;\n\t\t\tint w = simple(u);\n\t\t\tif(dup.emplace(sp, p).second){\n\t\t\t\tnodes[v][0].push_back(w);\n\t\t\t}\n\t\t}\n\t\treturn v;\n\t}\n\tint simple(int u){\n\t\twhile(1){\n\t\t\tint v = basic(u);\n\t\t\tif(v == -1){ break; }\n\t\t\tu = v;\n\t\t}\n\t\treturn u;\n\t}\n\tint basic(int u){\n\t\tif(*p == '*'){\n\t\t\t++p;\n\t\t\tint v = nodes.size();\n\t\t\tnodes.emplace_back();\n\t\t\tnodes[v][0].push_back(u);\n\t\t\tint w = nodes.size();\n\t\t\tnodes.emplace_back();\n\t\t\tnodes[w][0].push_back(v);\n\t\t\tint x = elementary(v);\n\t\t\tassert(x != -1);\n\t\t\tnodes[v][0].push_back(x);\n\t\t\treturn w;\n\t\t}\n\t\treturn elementary(u);\n\t}\n\tint elementary(int u){\n\t\tif(*p == ')'){\n\t\t\t++p;\n\t\t\tint v = pattern(u);\n\t\t\tassert(*p == '(');\n\t\t\t++p;\n\t\t\treturn v;\n\t\t}\n\t\tif(*p == '.' || isupper(*p)){\n\t\t\tint v = nodes.size();\n\t\t\tnodes.emplace_back();\n\t\t\tif(*p == '.'){\n\t\t\t\tfor(int i = 1; i <= 26; ++i){\n\t\t\t\t\tnodes[v][i].push_back(u);\n\t\t\t\t}\n\t\t\t}\n\t\t\telse{\n\t\t\t\tnodes[v][*p - 'A' + 1].push_back(u);\n\t\t\t}\n\t\t\t++p;\n\t\t\treturn v;\n\t\t}\n\t\treturn -1;\n\t}\n};\n\nstruct solver{\n\tusing pvii = pair<vector<int>,int>;\n\tvector<pvii> rcs, ccs;\n\tvector<int> rows, cols;\n\tvector<int> ans;\n\tvector<int> tmp;\n\t\n\tbool check(const pvii &v, int b, int und){\n\t\tint q = *lower_bound(ALL(v.first), b);\n\t\treturn q < b + (1 << (und * 5));\n\t}\n\t\n\tvoid dfs(unsigned y, unsigned x){\n\t\tif(x == cols.size()){\n\t\t\tx = 0;\n\t\t\t++y;\n\t\t\tif(y == rows.size()){\n\t\t\t\tif(ans.empty()){\n\t\t\t\t\tans = rows;\n\t\t\t\t\treturn;\n\t\t\t\t}\n\t\t\t\tthrow 0;\n\t\t\t}\n\t\t}\n\t\tint tmpr = rows[y], tmpc = cols[x];\n\t\tfor(int i = 1; i <= 26; ++i){\n\t\t\tint dx = cols.size() - x - 1;\n\t\t\tint dy = rows.size() - y - 1;\n\t\t\trows[y] |= i << dx* 5;\n\t\t\tcols[x] |= i << dy * 5;\n\t\t\tif(check(rcs[y], rows[y], dx) && check(ccs[x], cols[x], dy)){\n\t\t\t\tdfs(y, x + 1);\n\t\t\t}\n\t\t\trows[y] = tmpr;\n\t\t\tcols[x] = tmpc;\n\t\t}\n\t}\n\t\n\ttemplate <class T>\n\tvector<int> seconds(const vector<T> &v){\n\t\tvector<int> w;\n\t\tfor(const auto &x : v){ w.push_back(x.second); }\n\t\treturn w;\n\t}\n\t\n\tvoid perm(vector<int> &v, const vector<int> &p){\n\t\ttmp.clear();\n\t\ttmp.reserve(v.size() + 1);\n\t\tint n = p.size();\n\t\tfor(int r : v){\n\t\t\tint s = 0;\n\t\t\tfor(int q : p){\n\t\t\t\ts = s << 5 | ((r >> (n - q - 1) * 5) & 31);\n\t\t\t}\n\t\t\ttmp.push_back(s);\n\t\t}\n\t\tsort(ALL(tmp));\n\t\ttmp.push_back(0xfffff);\n\t\tv.swap(tmp);\n\t}\n\t\n\tvoid solve(int h, int w){\n\t\tfor(int i = 0; i < h; ++i){\n\t\t\tstring s;\n\t\t\tcin >> s;\n\t\t\trcs.emplace_back(parser().cands(s, w), i);\n\t\t}\n\t\tfor(int i = 0; i < w; ++i){\n\t\t\tstring s;\n\t\t\tcin >> s;\n\t\t\tccs.emplace_back(parser().cands(s, h), i);\n\t\t}\n\n\t\tsort(ALL(rcs), [](const pvii &a, const pvii &b){ return a.first.size() < b.first.size(); });\n\t\tsort(ALL(ccs), [](const pvii &a, const pvii &b){ return a.first.size() < b.first.size(); });\n\t\tvector<int> pr = seconds(rcs), pc = seconds(ccs);\n\n\t\ttry{\n\t\t\tbool inv = rcs[0].first.size() > ccs[0].first.size();\n\t\t\tfor(auto &a : rcs){\n\t\t\t\tperm(a.first, pc);\n\t\t\t}\n\t\t\tfor(auto &a : ccs){\n\t\t\t\tperm(a.first, pr);\n\t\t\t}\n\t\t\trows.assign(h, 0);\n\t\t\tcols.assign(w, 0);\n\t\t\tif(inv){\n\t\t\t\trows.swap(cols);\n\t\t\t\tpr.swap(pc);\n\t\t\t\trcs.swap(ccs);\n\t\t\t}\n\t\t\tdfs(0, 0);\n\t\t\t\n\t\t\tif(ans.empty()){\n\t\t\t\tcout << \"none\\n\";\n\t\t\t}\n\t\t\telse{\n\t\t\t\tvector<string> out(h, string(w, '_'));\n\t\t\t\tfor(size_t i = 0; i < rcs.size(); ++i)\n\t\t\t\tfor(size_t j = 0; j < ccs.size(); ++j){\n\t\t\t\t\tint y = pr[i];\n\t\t\t\t\tint x = pc[j];\n\t\t\t\t\tif(inv){ swap(y, x); }\n\t\t\t\t\tout[y][x] = 'A' - 1 + ((ans[i] >> (ccs.size() - j - 1) * 5) & 31);\n\t\t\t\t}\n\t\t\t\tfor(const string &s : out){\n\t\t\t\t\tcout << s << '\\n';\n\t\t\t\t}\n\t\t\t}\n\t\t}catch(int){\n\t\t\tcout << \"ambiguous\\n\";\n\t\t}\n\t}\n};\n\n}\n\nint main(){\n\tint h, w;\n\tfor(int cnt = 1; cin >> h >> w && h; ++cnt){\n\t\tsolver().solve(h, w);\n//\t\tcerr << cnt << endl;\n\t}\n}", "accuracy": 1, "time_ms": 1800, "memory_kb": 22644, "score_of_the_acc": 0, "final_rank": 1 }, { "submission_id": "aoj_1344_1517635", "code_snippet": "// very kuso problem\n\n#include <cassert>// c\n#include <iostream>// io\n#include <iomanip>\n#include <fstream>\n#include <sstream>\n#include <vector>// container\n#include <map>\n#include <set>\n#include <queue>\n#include <bitset>\n#include <stack>\n#include <algorithm>// other\n#include <complex>\n#include <numeric>\n#include <functional>\n#include <random>\n#include <regex>\nusing namespace std;\ntypedef long long ll;typedef unsigned long long ull;typedef long double ld;\n\n#define ALL(c) c.begin(),c.end()\n#define IN(l,v,r) (l<=v && v < r)\ntemplate<class T> void UNIQUE(T& v){\n\tsort(ALL(v));\n\tv.erase(unique(ALL(v)),v.end());\n}\n\n#define DUMP(x) cerr << #x <<\" = \" << (x)\n#define LINE() cerr<< \" (L\" << __LINE__ << \")\"\n\n#define range(i,l,r) for(int i=(int)l;i<(int)(r);i++)\n// struct range{\n// \tstruct Iter{\n// \t\tint v,step;\n// \t\tIter& operator++(){v+=step;return *this;}\n// \t\tbool operator!=(Iter& itr){return v<itr.v;}\n// \t\tint& operator*(){return v;}\n// \t};\n// \tIter i, n;\n// \trange(int i, int n,int step):i({i,step}), n({n,step}){}\n// \trange(int i, int n):range(i,n,1){}\n// \trange(int n):range(0,n){}\n// \tIter& begin(){return i;}\n// \tIter& end(){return n;}\n// };\n\n//input\ntemplate<typename T1,typename T2> istream& operator >> (istream& is,pair<T1,T2>& p){return is>>p.first>>p.second;}\ntemplate<typename T1> istream& operator >> (istream& is,tuple<T1>& t){return is >> get<0>(t);}\ntemplate<typename T1,typename T2> istream& operator >> (istream& is,tuple<T1,T2>& t){return is >> get<0>(t) >> get<1>(t);}\ntemplate<typename T1,typename T2,typename T3> istream& operator >> (istream& is,tuple<T1,T2,T3>& t){return is >>get<0>(t)>>get<1>(t)>>get<2>(t);}\ntemplate<typename T1,typename T2,typename T3,typename T4> istream& operator >> (istream& is,tuple<T1,T2,T3,T4>& t){return is >> get<0>(t)>>get<1>(t)>>get<2>(t)>>get<3>(t);}\ntemplate<typename T> istream& operator >> (istream& is,vector<T>& as){range(i,0,as.size())is >>as[i];return is;}\n//output\ntemplate<typename T> ostream& operator << (ostream& os, const set<T>& ss){for(auto a:ss){if(a!=ss.begin())os<<\" \"; os<<a;}return os;}\ntemplate<typename T1,typename T2> ostream& operator << (ostream& os, const pair<T1,T2>& p){return os<<p.first<<\" \"<<p.second;}\ntemplate<typename K,typename V> ostream& operator << (ostream& os, const map<K,V>& m){bool isF=true;for(auto& p:m){if(!isF)os<<endl;os<<p;isF=false;}return os;}\ntemplate<typename T1> ostream& operator << (ostream& os, const tuple<T1>& t){return os << get<0>(t);}\ntemplate<typename T1,typename T2> ostream& operator << (ostream& os, const tuple<T1,T2>& t){return os << get<0>(t)<<\" \"<<get<1>(t);}\ntemplate<typename T1,typename T2,typename T3> ostream& operator << (ostream& os, const tuple<T1,T2,T3>& t){return os << get<0>(t)<<\" \"<<get<1>(t)<<\" \"<<get<2>(t);}\ntemplate<typename T1,typename T2,typename T3,typename T4> ostream& operator << (ostream& os, const tuple<T1,T2,T3,T4>& t){return os << get<0>(t)<<\" \"<<get<1>(t)<<\" \"<<get<2>(t)<<\" \"<<get<3>(t);}\ntemplate<typename T> ostream& operator << (ostream& os, const vector<T>& as){range(i,0,as.size()){if(i!=0)os<<\" \"; os<<as[i];}return os;}\ntemplate<typename T> ostream& operator << (ostream& os, const vector<vector<T>>& as){range(i,0,as.size()){if(i!=0)os<<endl; os<<as[i];}return os;}\n\nstruct Edge{\n\tchar c;int f,t;\n};\n\nclass NFA{\npublic:\n\tstatic NFA ONE;\n\tint N;\n\tvector<Edge> es;\n\tvector<vector<int>> f_cache, t_cache;\n\t\n\tvector<vector<int>> reachable;\n\tvector<vector<vector<int>>> trans;\n\n\tint s,t;\n\tNFA(int N = 0):N(N),s(0),t(1){\n\t\tf_cache = t_cache = vector<vector<int>>(N);\n\t}\n\tvoid add_edge(char c,int f,int t){\n\t\tint eid = es.size();\n\t\tes.push_back({c,f,t});\n\t\tf_cache[f].push_back(eid);t_cache[t].push_back(eid);\n\t}\n\tstatic NFA *disjoint(NFA* a,NFA* b){\n\t\t// merge technique\n\t\tNFA *res = (a->es.size() < b->es.size()? b : a),*add = (a->es.size() < b->es.size()? a : b);\n\t\tres->f_cache.resize(res->N + add->N);res->t_cache.resize(res->N + add->N);\n\t\tint eid = res->es.size();\n\t\tfor(Edge& e:add->es){\n\t\t\te.f += res->N; e.t += res->N;\n\t\t\tres->es.push_back(e);\n\t\t\tres->f_cache[e.f].push_back(eid);res->t_cache[e.t].push_back(eid);eid++;\n\t\t}\n\t\tres->N += add->N;\n\t\treturn res;\n\t}\n\tstatic NFA* Union(NFA* a,NFA* b){\n\t\tbool bl = a->es.size() < b->es.size();\n\t\tint as = (bl?b->N:0)+a->s,at = (bl?b->N:0)+a->t,bs = (!bl?a->N:0)+b->s,bt = (!bl?a->N:0)+b->t;\n\t\tint S = a->N+b->N,T = a->N+b->N+1;\n\t\tNFA* ab = disjoint(a,b),*ab1 = disjoint(ab,&ONE),*res = disjoint(ab1,&ONE);\n\t\tres->add_edge('-',S,as); res->add_edge('-',S,bs);res->add_edge('-',at,T); res->add_edge('-',bt,T);\n\t\tres->s = S; res->t = T;\n\t\treturn res;\n\t}\n\tstatic NFA* Concat(NFA* a,NFA* b){\n\t\tbool bl = a->es.size() < b->es.size();\n\t\tint as = (bl?b->N:0)+a->s,at = (bl?b->N:0)+a->t,bs = (!bl?a->N:0)+b->s,bt = (!bl?a->N:0)+b->t;\n\t\tNFA* res = disjoint(a,b);\n\t\tres->add_edge('-',at,bs);\n\t\tres->s = as; res->t = bt;\n\t\treturn res;\n\t}\n\tstatic NFA* Star(NFA* a){\n\t\tNFA* res = disjoint(a,&ONE);\n\t\tres->add_edge('-',res->N-1,a->s); res->add_edge('-',a->t,res->N-1);\n\t\tres->s = res->t = res->N-1;\n\t\treturn res;\n\t}\n\n\t// step\n\tvoid create_reachable(){\n\t\tqueue<pair<int,int>> que; que.push({t,0});\n\n\t\treachable = vector<vector<int>>(N,vector<int>(4));\n\t\treachable[t][0] = true;\n\t\twhile(!que.empty()){\n\t\t\tint t,d;tie(t,d) = que.front();que.pop();\n\t\t\t//back\n\t\t\tfor(int eid:t_cache[t]){\n\t\t\t\tEdge& e = es[eid];\n\t\t\t\tif(e.c=='-'){\n\t\t\t\t\tif(!reachable[e.f][d]){\n\t\t\t\t\t\treachable[e.f][d] = true;\n\t\t\t\t\t\tque.push({e.f,d});\n\t\t\t\t\t}\n\t\t\t\t}else{\n\t\t\t\t\tif(d+1 <reachable[e.f].size() && !reachable[e.f][d+1]){\n\t\t\t\t\t\treachable[e.f][d+1] = true;\n\t\t\t\t\t\tque.push({e.f,d+1});\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n\n\tvoid create_trans(){\n\t\ttrans = vector<vector<vector<int>>>(N,vector<vector<int>>(27));\n\t\t// eps trans\n\t\trange(st,0,N){\n\t\t\tvector<bool> passed(N);\n\t\t\tpassed[st] = true;\n\t\t\tqueue<int> que;\n\t\t\trange(i,0,passed.size())if(passed[i])que.push(i);\n\t\t\twhile(!que.empty()){\n\t\t\t\tint s = que.front();que.pop();\n\t\t\t\tfor(int eid : f_cache[s])if(es[eid].c =='-' && !passed[es[eid].t]){\n\t\t\t\t\tpassed[es[eid].t] = true;\n\t\t\t\t\tque.push(es[eid].t);\n\t\t\t\t}\n\t\t\t}\n\t\t\trange(i,0,N)if(passed[i]) trans[st][26].push_back(i);\n\t\t}\n\t\trange(st,0,N)range(c,'A','Z'+1){\n\t\t\t// eps trans\n\t\t\tvector<bool> passed(N);\n\t\t\tfor(int s:trans[st][26])passed[s]=true;\n\t\t\tvector<bool> npassed(N);\n\t\t\tqueue<int> que;\n\t\t\trange(i,0,passed.size())if(passed[i])que.push(i);\n\t\t\twhile(!que.empty()){\n\t\t\t\tint s = que.front();que.pop();\n\t\t\t\tfor(int eid : f_cache[s])if((es[eid].c == c || es[eid].c =='.') && !npassed[es[eid].t]){\n\t\t\t\t\tnpassed[es[eid].t] = true;\n\t\t\t\t\tque.push(es[eid].t);\n\t\t\t\t}\n\t\t\t}\n\t\t\t// eps trans\n\t\t\trange(i,0,N)if(npassed[i])for(int s:trans[i][26]) npassed[s] = true;\n\t\t\trange(s,0,npassed.size())if(npassed[s]){\n\t\t\t\tbool alleps = true;\n\t\t\t\tfor(int eid : f_cache[s])alleps &= es[eid].c=='-';\n\t\t\t\t// ???????????? ?? ????§???????????????´?????????????????? for ?????????\n\t\t\t\tif(alleps && t != s)continue; \n\t\t\t\ttrans[st][c-'A'].push_back(s);\n\t\t\t}\n\t\t}\n\t}\n} NFA::ONE = NFA(1);\n\nclass REGtoNFA{\npublic:\n\tstatic NFA* pattern(string& s,int& i,int e){\n\t\tNFA* p = simple(s,i,e);\n\t\twhile(i < e && s[i]=='|'){\n\t\t\ti++;// '|'\n\t\t\tNFA* q = simple(s,i,e);\n\t\t\tp = NFA::Union(p,q);\n\t\t}\n\t\treturn p;\n\t}\n\tstatic NFA* simple(string& s,int& i,int e){\n\t\tNFA* p = basic(s,i,e);\n\t\twhile(i < e && s[i]!='|'){\n\t\t\tNFA* q = basic(s,i,e);\n\t\t\tp = NFA::Concat(p,q);\n\t\t}\n\t\treturn p;\n\t}\n\tstatic NFA* basic(string& s,int& i,int e){\n\t\tNFA* p = elementary(s,i,e);\n\t\tif(i < (int)s.size() && s[i]=='*'){\n\t\t\ti++;// *\n\t\t\tp = NFA::Star(p);\n\t\t}\n\t\treturn p;\n\t}\n\tstatic NFA* elementary(string& s,int& i,int e){\n\t\tNFA* p;\n\t\tif(s[i]=='('){\n\t\t\ti++; // (\n\t\t\tint d=1;\n\t\t\tfor(int j=i;j<e;j++){\n\t\t\t\tif(s[j]=='(') d++;\n\t\t\t\tif(s[j]==')') d--;\n\t\t\t\tif(d==0){\n\t\t\t\t\te = j;break;\n\t\t\t\t}\n\t\t\t}\n\t\t\tp = pattern(s,i,e);\n\t\t\ti++; // )\n\t\t}else{\n\t\t\tp = new NFA(2);\n\t\t\tp->add_edge(s[i],p->s,p->t);\n\t\t\ti++;// c\n\t\t}\n\t\treturn p;\n\t}\n};\n\n\nint rc = 0;\nclass Main{\n\tpublic:\n\tint h,w;\n\tvector<string> tmp,res;\n\n\tvector<NFA> NFAs;\n\tvector<vector<int>> stats;\n\n\tvoid dfs(int y,int x,int c){\n\t\tif(rc>1)return;\n\t\tif(y==h){\n\t\t\trc+=c; \n\t\t\tres = tmp;\n\t\t\treturn;\n\t\t}\n\t\t// ????????????????????¶??????????????´????????????????????????????????¢?´¢\n\t\tvector<vector<vector<int>>> nstats(2,vector<vector<int>>(26));\n\t\tvector<int> nfai={y,h+x};\n\t\trange(ni,0,2)for(char nc = 'A';nc <= 'Z'; nc++){\n\t\t\tfor(int s:stats[nfai[ni]])for(int t:NFAs[nfai[ni]].trans[s][nc-'A']) nstats[ni][nc-'A'].push_back(t);\n\t\t\tUNIQUE(nstats[ni][nc-'A']);\n\t\t}\n\t\tvector<int> gid(26);iota(ALL(gid),0);\n\t\tfor(char c1='A'; c1 <= 'Z';c1++)for(char c2='A'; c2 < c1;c2++)if(gid[c2-'A']==c2-'A'){\n\t\t\tif(nstats[0][c1-'A'].size() == nstats[0][c2-'A'].size() && nstats[1][c1-'A'].size() == nstats[1][c2-'A'].size()){\n\t\t\t\tbool eq = true;\n\t\t\t\trange(ni,0,2){\n\t\t\t\t\trange(i,0,nstats[ni][c1-'A'].size()){\n\t\t\t\t\t\teq &= nstats[ni][c1-'A'][i] == nstats[ni][c2-'A'][i];\n\t\t\t\t\t\tif(!eq)break;\n\t\t\t\t\t}\n\t\t\t\t\tif(!eq)break;\n\t\t\t\t}\n\t\t\t\tif(eq){\n\t\t\t\t\tgid[c1-'A'] = gid[c2-'A'];\n\t\t\t\t\tbreak;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\tvector<int> gc(26);\n\t\trange(i,0,26)gc[gid[i]]++;\n\n\t\tfor(char c='A'; c <= 'Z';c++)if(gid[c-'A']==c-'A'){\n\t\t\ttmp[y][x] = c;\n\t\t\tbool tmpOK = true;\n\t\t\t// eda ??????????????? de ikerukana ?\n\t\t\t{\n\t\t\t\tbool ok = false;\n\t\t\t\tfor(int s:nstats[0][c-'A'])ok |= NFAs[y].reachable[s][w-1-x];\n\t\t\t\ttmpOK &= ok;\n\t\t\t}\n\t\t\t{\n\t\t\t\tbool ok = false;\n\t\t\t\tfor(int s:nstats[1][c-'A'])ok |= NFAs[h+x].reachable[s][h-1-y];\n\t\t\t\ttmpOK &= ok;\n\t\t\t}\n\t\t\tif(x == w-1){\n\t\t\t\tbool ok = false;\n\t\t\t\tfor(int s:nstats[0][c-'A'])ok |= s == NFAs[y].t;\n\t\t\t\ttmpOK &= ok;\n\t\t\t}\n\t\t\tif(y == h-1){\n\t\t\t\tbool ok = false;\n\t\t\t\tfor(int s:nstats[1][c-'A'])ok |= s == NFAs[h+x].t;\n\t\t\t\ttmpOK &= ok;\n\t\t\t}\n\t\t\tif(tmpOK){\n\t\t\t\tvector<int> tmpp = stats[y],tmpq = stats[h+x];\n\t\t\t\tstats[y] = nstats[0][c-'A']; stats[h+x] = nstats[1][c-'A'];\n\t\t\t\tif(x+1 < w) dfs(y,x+1,gc[c-'A']);\n\t\t\t\telse dfs(y+1,0,gc[c-'A']);\n\t\t\t\tstats[y] = tmpp; stats[h + x] = tmpq;\n\t\t\t}\n\t\t}\n\t}\n\n\tvoid run(){\n\t\twhile(true){\n\t\t\tcin >> h >> w;\n\t\t\tif(h==0 && w==0)break;\n\t\t\trc = 0;\n\t\t\ttmp = res = vector<string>(h,string(w,'-'));\n\t\t\tvector<string> ss(h+w);cin >> ss;\n \t\t\t// h <= 4, w <= 4\n\t\t\tNFAs = vector<NFA>(h+w);\n\t\t\trange(i,0,h+w){\n\t\t\t\tstring s = \"\";\n\t\t\t\trange(j,1,ss[i].size()-1) s+=ss[i][j];\n\t\t\t\tint cur = 0, e = s.size();\n\t\t\t\tNFAs[i] = *REGtoNFA::pattern(s,cur,e);\n\t\t\t}\n\t\t\trange(i,0,h+w){\n\t\t\t\tNFAs[i].create_reachable();\n\t\t\t\tNFAs[i].create_trans();\n\t\t\t}\n\t\t\tstats = vector<vector<int>>(h+w);\n\t\t\trange(i,0,h+w) stats[i].push_back(NFAs[i].s);\n\n\t\t\tdfs(0,0,1);\n\t\t\tif(rc > 1){\n\t\t\t\tcout << \"ambiguous\" <<endl; \n\t\t\t}else if(rc == 0){\n\t\t\t\tcout << \"none\" <<endl;\n\t\t\t}else{\n\t\t\t\trange(i,0,h) cout << res[i] <<endl;\n\t\t\t}\n\t\t}\n\t}\n};\n\nint main(){\n\tcout <<fixed<<setprecision(20);\n\tcin.tie(0);\n\tios::sync_with_stdio(false);\n\tMain().run();\n\treturn 0;\n}", "accuracy": 1, "time_ms": 5620, "memory_kb": 41044, "score_of_the_acc": -2, "final_rank": 4 }, { "submission_id": "aoj_1344_1517593", "code_snippet": "// very kuso problem\n\n#include <cassert>// c\n#include <iostream>// io\n#include <iomanip>\n#include <fstream>\n#include <sstream>\n#include <vector>// container\n#include <map>\n#include <set>\n#include <queue>\n#include <bitset>\n#include <stack>\n#include <algorithm>// other\n#include <complex>\n#include <numeric>\n#include <functional>\n#include <random>\n#include <regex>\nusing namespace std;\ntypedef long long ll;typedef unsigned long long ull;typedef long double ld;\n\n#define ALL(c) c.begin(),c.end()\n#define IN(l,v,r) (l<=v && v < r)\ntemplate<class T> void UNIQUE(T& v){\n\tsort(ALL(v));\n\tv.erase(unique(ALL(v)),v.end());\n}\n\n#define DUMP(x) cerr << #x <<\" = \" << (x)\n#define LINE() cerr<< \" (L\" << __LINE__ << \")\"\n\n#define range(i,l,r) for(int i=(int)l;i<(int)(r);i++)\n// struct range{\n// \tstruct Iter{\n// \t\tint v,step;\n// \t\tIter& operator++(){v+=step;return *this;}\n// \t\tbool operator!=(Iter& itr){return v<itr.v;}\n// \t\tint& operator*(){return v;}\n// \t};\n// \tIter i, n;\n// \trange(int i, int n,int step):i({i,step}), n({n,step}){}\n// \trange(int i, int n):range(i,n,1){}\n// \trange(int n):range(0,n){}\n// \tIter& begin(){return i;}\n// \tIter& end(){return n;}\n// };\n\n//input\ntemplate<typename T1,typename T2> istream& operator >> (istream& is,pair<T1,T2>& p){return is>>p.first>>p.second;}\ntemplate<typename T1> istream& operator >> (istream& is,tuple<T1>& t){return is >> get<0>(t);}\ntemplate<typename T1,typename T2> istream& operator >> (istream& is,tuple<T1,T2>& t){return is >> get<0>(t) >> get<1>(t);}\ntemplate<typename T1,typename T2,typename T3> istream& operator >> (istream& is,tuple<T1,T2,T3>& t){return is >>get<0>(t)>>get<1>(t)>>get<2>(t);}\ntemplate<typename T1,typename T2,typename T3,typename T4> istream& operator >> (istream& is,tuple<T1,T2,T3,T4>& t){return is >> get<0>(t)>>get<1>(t)>>get<2>(t)>>get<3>(t);}\ntemplate<typename T> istream& operator >> (istream& is,vector<T>& as){range(i,0,as.size())is >>as[i];return is;}\n//output\ntemplate<typename T> ostream& operator << (ostream& os, const set<T>& ss){for(auto a:ss){if(a!=ss.begin())os<<\" \"; os<<a;}return os;}\ntemplate<typename T1,typename T2> ostream& operator << (ostream& os, const pair<T1,T2>& p){return os<<p.first<<\" \"<<p.second;}\ntemplate<typename K,typename V> ostream& operator << (ostream& os, const map<K,V>& m){bool isF=true;for(auto& p:m){if(!isF)os<<endl;os<<p;isF=false;}return os;}\ntemplate<typename T1> ostream& operator << (ostream& os, const tuple<T1>& t){return os << get<0>(t);}\ntemplate<typename T1,typename T2> ostream& operator << (ostream& os, const tuple<T1,T2>& t){return os << get<0>(t)<<\" \"<<get<1>(t);}\ntemplate<typename T1,typename T2,typename T3> ostream& operator << (ostream& os, const tuple<T1,T2,T3>& t){return os << get<0>(t)<<\" \"<<get<1>(t)<<\" \"<<get<2>(t);}\ntemplate<typename T1,typename T2,typename T3,typename T4> ostream& operator << (ostream& os, const tuple<T1,T2,T3,T4>& t){return os << get<0>(t)<<\" \"<<get<1>(t)<<\" \"<<get<2>(t)<<\" \"<<get<3>(t);}\ntemplate<typename T> ostream& operator << (ostream& os, const vector<T>& as){range(i,0,as.size()){if(i!=0)os<<\" \"; os<<as[i];}return os;}\ntemplate<typename T> ostream& operator << (ostream& os, const vector<vector<T>>& as){range(i,0,as.size()){if(i!=0)os<<endl; os<<as[i];}return os;}\n\nstruct Edge{\n\tchar c;int f,t;\n};\n\nclass NFA{\npublic:\n\tstatic NFA ONE;\n\tint N;\n\tvector<Edge> es;\n\tvector<vector<int>> f_cache, t_cache;\n\n\tint s,t;\n\tNFA(int N = 0):N(N),s(0),t(1){\n\t\tf_cache = t_cache = vector<vector<int>>(N);\n\t}\n\tvoid add_edge(char c,int f,int t){\n\t\tint eid = es.size();\n\t\tes.push_back({c,f,t});\n\t\tf_cache[f].push_back(eid);t_cache[t].push_back(eid);\n\t}\n\tstatic NFA disjoint(NFA& a,NFA& b){\n\t\t// merge technique\n\t\tNFA& res = (a.es.size() < b.es.size()? b : a), add = (a.es.size() < b.es.size()? a : b);\n\t\t\n\t\tres.f_cache.resize(res.N + add.N);res.t_cache.resize(res.N + add.N);\n\t\tint eid = res.es.size();\n\t\tfor(Edge& e:add.es){\n\t\t\te.f += res.N; e.t += res.N;\n\t\t\tres.es.push_back(e);\n\t\t\tres.f_cache[e.f].push_back(eid);res.t_cache[e.t].push_back(eid);eid++;\n\t\t}\n\t\tres.N += add.N;\n\t\treturn res;\n\t}\n\tstatic NFA Union(NFA& a,NFA& b){\n\t\tbool bl = a.es.size() < b.es.size();\n\t\tint as = (bl?b.N:0)+a.s,at = (bl?b.N:0)+a.t,aN = a.N, bs = (!bl?a.N:0)+b.s,bt = (!bl?a.N:0)+b.t, bN = b.N;\n\t\tint S = aN+bN,T = aN+bN+1;\n\t\tNFA ab = disjoint(a,b),ab1 = disjoint(ab,ONE),res = disjoint(ab1,ONE);\n\t\tres.add_edge('-',S,as); res.add_edge('-',S,bs);res.add_edge('-',at,T); res.add_edge('-',bt,T);\n\t\tres.s = S; res.t = T;\n\t\treturn res;\n\t}\n\tstatic NFA Concat(NFA& a,NFA& b){\n\t\tbool bl = a.es.size() < b.es.size();\n\t\tint as = (bl?b.N:0)+a.s,at = (bl?b.N:0)+a.t,aN = a.N, bs = (!bl?a.N:0)+b.s,bt = (!bl?a.N:0)+b.t, bN = b.N;\n\t\tNFA res = disjoint(a,b);\n\t\tres.add_edge('-',at,bs);\n\t\tres.s = as; res.t = bt;\n\t\treturn res;\n\t}\n\tstatic NFA Star(NFA& a){\n\t\tNFA res = disjoint(a,ONE);\n\t\tres.add_edge('-',res.N-1,a.s);\n\t\tres.add_edge('-',a.t,res.N-1);\n\t\tres.s = res.t = res.N-1;\n\t\treturn res;\n\t}\n\n\t// parsing \n\tstatic NFA pattern(string& s,int& i,int e){\n\t\tNFA p = simple(s,i,e);\n\t\twhile(i < e && s[i]=='|'){\n\t\t\ti++;// '|'\n\t\t\tNFA q = simple(s,i,e);\n\t\t\tp = NFA::Union(p,q);\n\t\t}\n\t\treturn p;\n\t}\n\tstatic NFA simple(string& s,int& i,int e){\n\t\tNFA p = basic(s,i,e);\n\t\twhile(i < e && s[i]!='|'){\n\t\t\tNFA q = basic(s,i,e);\n\t\t\tp = NFA::Concat(p,q);\n\t\t}\n\t\treturn p;\n\t}\n\tstatic NFA basic(string& s,int& i,int e){\n\t\tNFA p = elementary(s,i,e);\n\t\tif(i < (int)s.size() && s[i]=='*'){\n\t\t\ti++;// *\n\t\t\tp = NFA::Star(p);\n\t\t}\n\t\treturn p;\n\t}\n\tstatic NFA elementary(string& s,int& i,int e){\n\t\tNFA p;\n\t\tif(s[i]=='('){\n\t\t\ti++; // (\n\t\t\tint d=1;\n\t\t\tfor(int j=i;j<e;j++){\n\t\t\t\tif(s[j]=='(') d++;\n\t\t\t\tif(s[j]==')') d--;\n\t\t\t\tif(d==0){\n\t\t\t\t\te = j;break;\n\t\t\t\t}\n\t\t\t}\n\t\t\tp = pattern(s,i,e);\n\t\t\ti++; // )\n\t\t}else{\n\t\t\tp = NFA(2);\n\t\t\tp.add_edge(s[i],p.s,p.t);\n\t\t\ti++;// c\n\t\t}\n\t\treturn p;\n\t}\n} NFA::ONE = NFA(1);\n\nint rc = 0;\nclass Main{\n\tpublic:\n\n\tint h,w;\n\tvector<string> tmp,res;\n\n\tvector<NFA> NFAs;\n\tvector<vector<int>> stats;\n\tvector<vector<vector<int>>> reachable;\n\tvector<vector<vector<vector<int>>>> trans;\n\n\tvoid create_reachable(int nind){\n\t\tNFA& nfa = NFAs[nind];\n\t\tqueue<pair<int,int>> que; que.push({nfa.t,0});\n\t\treachable[nind][nfa.t][0] = true;\n\t\twhile(!que.empty()){\n\t\t\tint t,d;tie(t,d) = que.front();que.pop();\n\t\t\t//back\n\t\t\tfor(int eid:nfa.t_cache[t]){\n\t\t\t\tEdge& e = nfa.es[eid];\n\t\t\t\tif(e.c=='-'){\n\t\t\t\t\tif(!reachable[nind][e.f][d]){\n\t\t\t\t\t\treachable[nind][e.f][d] = true;\n\t\t\t\t\t\tque.push({e.f,d});\n\t\t\t\t\t}\n\t\t\t\t}else{\n\t\t\t\t\tif(d+1 <reachable[nind][e.f].size() && !reachable[nind][e.f][d+1]){\n\t\t\t\t\t\treachable[nind][e.f][d+1] = true;\n\t\t\t\t\t\tque.push({e.f,d+1});\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n\n\tvoid create_trans(int nind,int st,char nc){\n\t\tNFA& nfa = NFAs[nind];\n\n\t\tvector<bool> passed(nfa.N);\n\t\tpassed[st] = true;\n\t\t// eps trans\n\t\t{\n\t\t\tqueue<int> que;\n\t\t\trange(i,0,passed.size())if(passed[i])que.push(i);\n\t\t\twhile(!que.empty()){\n\t\t\t\tint s = que.front();que.pop();\n\t\t\t\tfor(int eid : nfa.f_cache[s])if(nfa.es[eid].c =='-' && !passed[nfa.es[eid].t]){\n\t\t\t\t\tpassed[nfa.es[eid].t] = true;\n\t\t\t\t\tque.push(nfa.es[eid].t);\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\t// trans\n\t\t{\n\t\t\tvector<bool> npassed(nfa.N);\n\t\t\trange(s,0,nfa.N)if(passed[s]) for(int eid : nfa.f_cache[s]){\n\t\t\t\tchar c = nfa.es[eid].c;\n\t\t\t\tint t = nfa.es[eid].t;\n\t\t\t\tif(nc==c || c=='.') npassed[t] = true;\n\t\t\t}\n\t\t\tpassed = npassed;\n\t\t}\n\n\t\t// eps trans\n\t\t{\n\t\t\tqueue<int> que;\n\t\t\trange(i,0,passed.size())if(passed[i])que.push(i);\n\t\t\twhile(!que.empty()){\n\t\t\t\tint s = que.front();que.pop();\n\t\t\t\tfor(int eid : nfa.f_cache[s])if(nfa.es[eid].c =='-' && !passed[nfa.es[eid].t]){\n\t\t\t\t\tpassed[nfa.es[eid].t] = true;\n\t\t\t\t\tque.push(nfa.es[eid].t);\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\n\t\trange(s,0,passed.size())if(passed[s]){\n\t\t\tbool exeps = false, alleps = true;\n\t\t\tfor(int eid : nfa.f_cache[s]){\n\t\t\t\texeps |= nfa.es[eid].c=='-';\n\t\t\t\talleps &= nfa.es[eid].c=='-';\n\t\t\t}\n\t\t\t// ?? ????§???????????????´?????????????????? for ?????????\n\t\t\tif(exeps && alleps && nfa.t != s)continue; \n\t\t\ttrans[nind][st][nc-'A'].push_back(s);\n\t\t}\n\t}\n\n\tvoid dfs(int y,int x,int c){\n\t\t// cerr << y <<\" \" << x <<endl;\n\t\t// cerr << tmp <<endl;\n\n\t\tif(rc>1)return;\n\t\tif(y==h){\n\t\t\trc+=c;\n\t\t\tres = tmp;\n\t\t\treturn;\n\t\t}\n\n\t\t// ????????????????????¶??????????????´????????????????????????????????¢?´¢\n\t\tvector<vector<int>> ynstats(26),xnstats(26);\n\t\tfor(char nc = 'A';nc <= 'Z'; nc++){\n\t\t\tfor(int s:stats[y])for(int t:trans[y][s][nc-'A'])ynstats[nc-'A'].push_back(t);\n\t\t\tUNIQUE(ynstats[nc-'A']);\n\t\t}\n\t\tfor(char nc = 'A';nc <= 'Z'; nc++){\n\t\t\tfor(int s:stats[h+x])for(int t:trans[h+x][s][nc-'A'])xnstats[nc-'A'].push_back(t);\n\t\t\tUNIQUE(xnstats[nc-'A']);\n\t\t}\n\t\tvector<int> gid(26);iota(ALL(gid),0);\n\t\tfor(char c1='A'; c1 <= 'Z';c1++){\n\t\t\tfor(char c2='A'; c2 < c1;c2++)if(gid[c2-'A']==c2-'A'){\n\t\t\t\tif(ynstats[c1-'A'].size() == ynstats[c2-'A'].size() && xnstats[c1-'A'].size() == xnstats[c2-'A'].size()){\n\t\t\t\t\tbool eq = true;\n\t\t\t\t\trange(i,0,ynstats[c1-'A'].size())eq &= ynstats[c1-'A'][i] == ynstats[c2-'A'][i];\n\t\t\t\t\trange(i,0,xnstats[c1-'A'].size())eq &= xnstats[c1-'A'][i] == xnstats[c2-'A'][i];\n\t\t\t\t\tif(eq){\n\t\t\t\t\t\tgid[c1-'A']=gid[c2-'A'];\n\t\t\t\t\t\tbreak;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\tvector<int> gc(26);\n\t\trange(i,0,26)gc[gid[i]]++;\n\n\t\tfor(char c='A'; c <= 'Z';c++)if(gid[c-'A']==c-'A'){\n\t\t\ttmp[y][x] = c;\n\t\t\tbool tmpOK = true;\n\t\t\t// eda ??????????????? de ikerukana ?\n\t\t\t{\n\t\t\t\tbool ok = false;\n\t\t\t\tfor(int s:ynstats[c-'A'])ok |= reachable[y][s][w-1-x];\n\t\t\t\ttmpOK &= ok;\n\t\t\t}\n\t\t\t{\n\t\t\t\tbool ok = false;\n\t\t\t\tfor(int s:xnstats[c-'A'])ok |= reachable[h+x][s][h-1-y];\n\t\t\t\ttmpOK &= ok;\n\t\t\t}\n\t\t\tif(x == w-1){\n\t\t\t\tbool ok = false;\n\t\t\t\tfor(int s:ynstats[c-'A'])ok |= s == NFAs[y].t;\n\t\t\t\ttmpOK &= ok;\n\t\t\t}\n\t\t\tif(y == h-1){\n\t\t\t\tbool ok = false;\n\t\t\t\tfor(int s:xnstats[c-'A'])ok |= s == NFAs[h+x].t;\n\t\t\t\ttmpOK &= ok;\n\t\t\t}\n\t\t\tif(tmpOK){\n\t\t\t\tvector<int> tmpp = stats[y],tmpq = stats[h+x];\n\t\t\t\tstats[y] = ynstats[c-'A']; stats[h+x] = xnstats[c-'A'];\n\t\t\t\tif(x+1 < w) dfs(y,x+1,gc[c-'A']);\n\t\t\t\telse dfs(y+1,0,gc[c-'A']);\n\t\t\t\tstats[y] = tmpp; stats[h + x] = tmpq;\n\t\t\t}\n\t\t}\n\t}\n\n\tvoid run(){\n\t\twhile(true){\n\t\t\tcin >> h >> w;\n\t\t\tif(h==0 && w==0)break;\n\t\t\trc = 0;\n\t\t\ttmp = res = vector<string>(h,string(w,'-'));\n\t\t\tvector<string> ss(h+w);cin >> ss;\n \t\t\t// h <= 4, w <= 4\n\t\t\tNFAs = vector<NFA>(h+w);\n\t\t\trange(i,0,h+w){\n\t\t\t\tstring s = \"\";\n\t\t\t\trange(j,1,ss[i].size()-1)s+=ss[i][j];\n\t\t\t\tint cur = 0, e = s.size();\n\t\t\t\tNFAs[i]= NFA::pattern(s,cur,e);\n\t\t\t}\n\t\t\tstats = vector<vector<int>>(h+w);\n\t\t\trange(i,0,h+w) stats[i].push_back(NFAs[i].s);\n\n\t\t\treachable = vector<vector<vector<int>>>(h+w);\n\t\t\trange(i,0,h){\n\t\t\t\treachable[i] = vector<vector<int>>(NFAs[i].N);\n\t\t\t\trange(s,0,NFAs[i].N) reachable[i][s] = vector<int>(h+1);\n\t\t\t}\n\t\t\trange(i,0,w){\n\t\t\t\treachable[h+i] = vector<vector<int>>(NFAs[h+i].N);\n\t\t\t\trange(s,0,NFAs[h+i].N) reachable[h+i][s] = vector<int>(w+1);\n\t\t\t}\n\t\t\trange(i,0,h+w) create_reachable(i);\n\t\t\t\n\t\t\ttrans = vector<vector<vector<vector<int>>>>(h+w);\n\t\t\trange(i,0,h+w){\n\t\t\t\ttrans[i] = vector<vector<vector<int>>>(NFAs[i].N,vector<vector<int>>(26));\n\t\t\t\trange(s,0,NFAs[i].N)range(c,'A','Z'+1)create_trans(i,s,c);\n\t\t\t}\n\n\t\t\t// cerr << NFAs[0].t <<endl;\n\t\t\t// range(i,0,NFAs[0].es.size()){\n\t\t\t// \tcerr << NFAs[0].es[i].c <<\" \" <<NFAs[0].es[i].f <<\" \" <<NFAs[0].es[i].t <<endl;\n\t\t\t// }\n\t\t\t// cerr << NFAs[0].t_cache <<endl;\n\n\t\t\tdfs(0,0,1);\n\t\t\tif(rc > 1){\n\t\t\t\tcout << \"ambiguous\" <<endl; \n\t\t\t}else if(rc == 0){\n\t\t\t\tcout << \"none\" <<endl;\n\t\t\t}else{\n\t\t\t\trange(i,0,h) cout << res[i] <<endl;\n\t\t\t}\n\t\t}\n\t}\n};\n\nint main(){\n\tcout <<fixed<<setprecision(20);\n\tcin.tie(0);\n\tios::sync_with_stdio(false);\n\tMain().run();\n\treturn 0;\n}", "accuracy": 1, "time_ms": 5390, "memory_kb": 29680, "score_of_the_acc": -1.3222, "final_rank": 3 }, { "submission_id": "aoj_1344_1514737", "code_snippet": "// very kuso problem\n\n#include <cassert>// c\n#include <iostream>// io\n#include <iomanip>\n#include <fstream>\n#include <sstream>\n#include <vector>// container\n#include <map>\n#include <set>\n#include <queue>\n#include <bitset>\n#include <stack>\n#include <algorithm>// other\n#include <complex>\n#include <numeric>\n#include <functional>\n#include <random>\n#include <regex>\nusing namespace std;\ntypedef long long ll;typedef unsigned long long ull;typedef long double ld;\n\n#define ALL(c) c.begin(),c.end()\n#define IN(l,v,r) (l<=v && v < r)\ntemplate<class T> void UNIQUE(T& v){\n\tsort(ALL(v));\n\tv.erase(unique(ALL(v)),v.end());\n}\n\n#define DUMP(x) cerr << #x <<\" = \" << (x)\n#define LINE() cerr<< \" (L\" << __LINE__ << \")\"\n\n#define range(i,l,r) for(int i=(int)l;i<(int)(r);i++)\n// struct range{\n// \tstruct Iter{\n// \t\tint v,step;\n// \t\tIter& operator++(){v+=step;return *this;}\n// \t\tbool operator!=(Iter& itr){return v<itr.v;}\n// \t\tint& operator*(){return v;}\n// \t};\n// \tIter i, n;\n// \trange(int i, int n,int step):i({i,step}), n({n,step}){}\n// \trange(int i, int n):range(i,n,1){}\n// \trange(int n):range(0,n){}\n// \tIter& begin(){return i;}\n// \tIter& end(){return n;}\n// };\n\n//input\ntemplate<typename T1,typename T2> istream& operator >> (istream& is,pair<T1,T2>& p){return is>>p.first>>p.second;}\ntemplate<typename T1> istream& operator >> (istream& is,tuple<T1>& t){return is >> get<0>(t);}\ntemplate<typename T1,typename T2> istream& operator >> (istream& is,tuple<T1,T2>& t){return is >> get<0>(t) >> get<1>(t);}\ntemplate<typename T1,typename T2,typename T3> istream& operator >> (istream& is,tuple<T1,T2,T3>& t){return is >>get<0>(t)>>get<1>(t)>>get<2>(t);}\ntemplate<typename T1,typename T2,typename T3,typename T4> istream& operator >> (istream& is,tuple<T1,T2,T3,T4>& t){return is >> get<0>(t)>>get<1>(t)>>get<2>(t)>>get<3>(t);}\ntemplate<typename T> istream& operator >> (istream& is,vector<T>& as){range(i,0,as.size())is >>as[i];return is;}\n//output\ntemplate<typename T> ostream& operator << (ostream& os, const set<T>& ss){for(auto a:ss){if(a!=ss.begin())os<<\" \"; os<<a;}return os;}\ntemplate<typename T1,typename T2> ostream& operator << (ostream& os, const pair<T1,T2>& p){return os<<p.first<<\" \"<<p.second;}\ntemplate<typename K,typename V> ostream& operator << (ostream& os, const map<K,V>& m){bool isF=true;for(auto& p:m){if(!isF)os<<endl;os<<p;isF=false;}return os;}\ntemplate<typename T1> ostream& operator << (ostream& os, const tuple<T1>& t){return os << get<0>(t);}\ntemplate<typename T1,typename T2> ostream& operator << (ostream& os, const tuple<T1,T2>& t){return os << get<0>(t)<<\" \"<<get<1>(t);}\ntemplate<typename T1,typename T2,typename T3> ostream& operator << (ostream& os, const tuple<T1,T2,T3>& t){return os << get<0>(t)<<\" \"<<get<1>(t)<<\" \"<<get<2>(t);}\ntemplate<typename T1,typename T2,typename T3,typename T4> ostream& operator << (ostream& os, const tuple<T1,T2,T3,T4>& t){return os << get<0>(t)<<\" \"<<get<1>(t)<<\" \"<<get<2>(t)<<\" \"<<get<3>(t);}\ntemplate<typename T> ostream& operator << (ostream& os, const vector<T>& as){range(i,0,as.size()){if(i!=0)os<<\" \"; os<<as[i];}return os;}\ntemplate<typename T> ostream& operator << (ostream& os, const vector<vector<T>>& as){range(i,0,as.size()){if(i!=0)os<<endl; os<<as[i];}return os;}\n\nstruct Edge{\n\tchar c;int f,t;\n};\n\nclass NFA{\npublic:\n\t// (source)initial state = 0, (target)acc state = 0 or 1\n\tint N;\n\tvector<Edge> es;\n\tvector<vector<int>> f_cache, t_cache;\n\n\tint s,t;\n\tNFA(int N = 0):N(N),s(0),t(1){\n\t\tf_cache = t_cache = vector<vector<int>>(N);\n\t}\n\tvoid add_edge(char c,int f,int t){\n\t\tint eid = es.size();\n\t\tes.push_back({c,f,t});\n\t\tf_cache[f].push_back(eid);t_cache[t].push_back(eid);\n\t}\n\tvoid disjoint(NFA& b){\n\t\tf_cache.resize(N+b.N);\n\t\tt_cache.resize(N+b.N);\n\t\tfor(Edge& e:b.es){\n\t\t\te.f+=N; e.t+=N;\n\t\t\tint eid = es.size();\n\t\t\tes.push_back(e);\n\t\t\tf_cache[e.f].push_back(eid); t_cache[e.t].push_back(eid);\n\t\t}\n\t\tN +=b.N;\n\t}\n\tvoid Union(NFA b){\n\t\tint as = s,at = t,aN = N, bs = b.s,bt = b.t, bN = b.N;\n\t\tdisjoint(b);\n\t\tNFA sp = NFA(1);\n\t\tdisjoint(sp);\n\t\tNFA tp = NFA(1);\n\t\tdisjoint(tp);\n\t\tadd_edge('-',aN+bN,as);\n\t\tadd_edge('-',aN+bN,aN + bs);\n\t\tadd_edge('-',at,aN+bN+1);\n\t\tadd_edge('-',aN+bt,aN+bN+1);\n\t\ts = aN+bN; t = aN + bN +1;\n\t}\n\tvoid Concat(NFA b){\n\t\tint as = s,at = t,aN = N, bs = b.s,bt = b.t, bN = b.N;\n\t\tdisjoint(b);\n\t\tadd_edge('-',at,aN + bs);\n\t\ts = as;t = aN + bt;\n\t}\n\tvoid Star(){\n\t\tNFA sp = NFA(1);\n\t\tdisjoint(sp);\n\t\tadd_edge('-',N-1,s);\n\t\tadd_edge('-',t,N-1);\n\t\ts = t = N-1;\n\t}\n\n\t// parsing \n\tstatic NFA pattern(string& s,int& i,int e){\n\t\tNFA p = simple(s,i,e);\n\t\twhile(i < e && s[i]=='|'){\n\t\t\ti++;// '|'\n\t\t\tp.Union(simple(s,i,e));\n\t\t}\n\t\treturn p;\n\t}\n\tstatic NFA simple(string& s,int& i,int e){\n\t\tNFA p = basic(s,i,e);\n\t\twhile(i < e && s[i]!='|'){\n\t\t\tp.Concat(basic(s,i,e));\n\t\t}\n\t\treturn p;\n\t}\n\tstatic NFA basic(string& s,int& i,int e){\n\t\tNFA p = elementary(s,i,e);\n\t\tif(i < (int)s.size() && s[i]=='*'){\n\t\t\ti++;// *\n\t\t\tp.Star();\n\t\t}\n\t\treturn p;\n\t}\n\tstatic NFA elementary(string& s,int& i,int e){\n\t\tNFA p;\n\t\tif(s[i]=='('){\n\t\t\ti++; // (\n\t\t\tint d=1;\n\t\t\tfor(int j=i;j<e;j++){\n\t\t\t\tif(s[j]=='(') d++;\n\t\t\t\tif(s[j]==')') d--;\n\t\t\t\tif(d==0){\n\t\t\t\t\te = j;break;\n\t\t\t\t}\n\t\t\t}\n\t\t\tp = pattern(s,i,e);\n\t\t\ti++; // )\n\t\t}else{\n\t\t\tp = NFA(2);\n\t\t\tp.add_edge(s[i],p.s,p.t);\n\t\t\ti++;// c\n\t\t}\n\t\treturn p;\n\t}\n};\n\n\nint rc = 0;\nclass Main{\n\tpublic:\n\n\tint h,w;\n\tvector<string> tmp,res;\n\n\tvector<NFA> NFAs;\n\tvector<vector<int>> stats;\n\tvector<vector<vector<int>>> reachable;\n\tvector<vector<vector<vector<int>>>> trans;\n\n\tvoid create_reachable(int nind){\n\t\tNFA& nfa = NFAs[nind];\n\t\tqueue<pair<int,int>> que; que.push({nfa.t,0});\n\t\treachable[nind][nfa.t][0] = true;\n\t\twhile(!que.empty()){\n\t\t\tint t,d;tie(t,d) = que.front();que.pop();\n\t\t\t//back\n\t\t\tfor(int eid:nfa.t_cache[t]){\n\t\t\t\tEdge& e = nfa.es[eid];\n\t\t\t\tif(e.c=='-'){\n\t\t\t\t\tif(!reachable[nind][e.f][d]){\n\t\t\t\t\t\treachable[nind][e.f][d] = true;\n\t\t\t\t\t\tque.push({e.f,d});\n\t\t\t\t\t}\n\t\t\t\t}else{\n\t\t\t\t\tif(d+1 <reachable[nind][e.f].size() && !reachable[nind][e.f][d+1]){\n\t\t\t\t\t\treachable[nind][e.f][d+1] = true;\n\t\t\t\t\t\tque.push({e.f,d+1});\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n\n\tvoid create_trans(int nind,int st,char nc){\n\t\tNFA& nfa = NFAs[nind];\n\n\t\tvector<bool> passed(nfa.N);\n\t\tpassed[st] = true;\n\t\t// eps trans\n\t\t{\n\t\t\tqueue<int> que;\n\t\t\trange(i,0,passed.size())if(passed[i])que.push(i);\n\t\t\twhile(!que.empty()){\n\t\t\t\tint s = que.front();que.pop();\n\t\t\t\tfor(int eid : nfa.f_cache[s])if(nfa.es[eid].c =='-' && !passed[nfa.es[eid].t]){\n\t\t\t\t\tpassed[nfa.es[eid].t] = true;\n\t\t\t\t\tque.push(nfa.es[eid].t);\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\t// trans\n\t\t{\n\t\t\tvector<bool> npassed(nfa.N);\n\t\t\trange(s,0,nfa.N)if(passed[s]) for(int eid : nfa.f_cache[s]){\n\t\t\t\tchar c = nfa.es[eid].c;\n\t\t\t\tint t = nfa.es[eid].t;\n\t\t\t\tif(nc==c || c=='.') npassed[t] = true;\n\t\t\t}\n\t\t\tpassed = npassed;\n\t\t}\n\n\t\t// eps trans\n\t\t{\n\t\t\tqueue<int> que;\n\t\t\trange(i,0,passed.size())if(passed[i])que.push(i);\n\t\t\twhile(!que.empty()){\n\t\t\t\tint s = que.front();que.pop();\n\t\t\t\tfor(int eid : nfa.f_cache[s])if(nfa.es[eid].c =='-' && !passed[nfa.es[eid].t]){\n\t\t\t\t\tpassed[nfa.es[eid].t] = true;\n\t\t\t\t\tque.push(nfa.es[eid].t);\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\n\t\trange(s,0,passed.size())if(passed[s]){\n\t\t\tbool exeps = false, alleps = true;\n\t\t\tfor(int eid : nfa.f_cache[s]){\n\t\t\t\texeps |= nfa.es[eid].c=='-';\n\t\t\t\talleps &= nfa.es[eid].c=='-';\n\t\t\t}\n\t\t\t// ?? ????§???????????????´?????????????????? for ?????????\n\t\t\tif(exeps && alleps && nfa.t != s)continue; \n\t\t\ttrans[nind][st][nc-'A'].push_back(s);\n\t\t}\n\t}\n\n\tvoid dfs(int y,int x,int c){\n\t\t// cerr << y <<\" \" << x <<endl;\n\t\t// cerr << tmp <<endl;\n\n\t\tif(rc>1)return;\n\t\tif(y==h){\n\t\t\trc+=c;\n\t\t\tres = tmp;\n\t\t\treturn;\n\t\t}\n\n\t\t// ????????????????????¶??????????????´????????????????????????????????¢?´¢\n\t\tvector<vector<int>> ynstats(26),xnstats(26);\n\t\tfor(char nc = 'A';nc <= 'Z'; nc++){\n\t\t\tfor(int s:stats[y])for(int t:trans[y][s][nc-'A'])ynstats[nc-'A'].push_back(t);\n\t\t\tUNIQUE(ynstats[nc-'A']);\n\t\t}\n\t\tfor(char nc = 'A';nc <= 'Z'; nc++){\n\t\t\tfor(int s:stats[h+x])for(int t:trans[h+x][s][nc-'A'])xnstats[nc-'A'].push_back(t);\n\t\t\tUNIQUE(xnstats[nc-'A']);\n\t\t}\n\t\tvector<int> gid(26);iota(ALL(gid),0);\n\t\tfor(char c1='A'; c1 <= 'Z';c1++){\n\t\t\tfor(char c2='A'; c2 < c1;c2++)if(gid[c2-'A']==c2-'A'){\n\t\t\t\tif(ynstats[c1-'A'].size() == ynstats[c2-'A'].size() && xnstats[c1-'A'].size() == xnstats[c2-'A'].size()){\n\t\t\t\t\tbool eq = true;\n\t\t\t\t\trange(i,0,ynstats[c1-'A'].size())eq &= ynstats[c1-'A'][i] == ynstats[c2-'A'][i];\n\t\t\t\t\trange(i,0,xnstats[c1-'A'].size())eq &= xnstats[c1-'A'][i] == xnstats[c2-'A'][i];\n\t\t\t\t\tif(eq){\n\t\t\t\t\t\tgid[c1-'A']=gid[c2-'A'];\n\t\t\t\t\t\tbreak;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\tvector<int> gc(26);\n\t\trange(i,0,26)gc[gid[i]]++;\n\n\t\tfor(char c='A'; c <= 'Z';c++)if(gid[c-'A']==c-'A'){\n\t\t\ttmp[y][x] = c;\n\t\t\tbool tmpOK = true;\n\t\t\t// eda ??????????????? de ikerukana ?\n\t\t\t{\n\t\t\t\tbool ok = false;\n\t\t\t\tfor(int s:ynstats[c-'A'])ok |= reachable[y][s][w-1-x];\n\t\t\t\ttmpOK &= ok;\n\t\t\t}\n\t\t\t{\n\t\t\t\tbool ok = false;\n\t\t\t\tfor(int s:xnstats[c-'A'])ok |= reachable[h+x][s][h-1-y];\n\t\t\t\ttmpOK &= ok;\n\t\t\t}\n\t\t\tif(x == w-1){\n\t\t\t\tbool ok = false;\n\t\t\t\tfor(int s:ynstats[c-'A'])ok |= s == NFAs[y].t;\n\t\t\t\ttmpOK &= ok;\n\t\t\t}\n\t\t\tif(y == h-1){\n\t\t\t\tbool ok = false;\n\t\t\t\tfor(int s:xnstats[c-'A'])ok |= s == NFAs[h+x].t;\n\t\t\t\ttmpOK &= ok;\n\t\t\t}\n\t\t\tif(tmpOK){\n\t\t\t\tvector<int> tmpp = stats[y],tmpq = stats[h+x];\n\t\t\t\tstats[y] = ynstats[c-'A']; stats[h+x] = xnstats[c-'A'];\n\t\t\t\tif(x+1 < w) dfs(y,x+1,gc[c-'A']);\n\t\t\t\telse dfs(y+1,0,gc[c-'A']);\n\t\t\t\tstats[y] = tmpp; stats[h + x] = tmpq;\n\t\t\t}\n\t\t}\n\t}\n\n\tvoid run(){\n\t\twhile(true){\n\t\t\tcin >> h >> w;\n\t\t\tif(h==0 && w==0)break;\n\t\t\trc = 0;\n\t\t\ttmp = res = vector<string>(h,string(w,'-'));\n\t\t\tvector<string> ss(h+w);cin >> ss;\n \t\t\t// h <= 4, w <= 4\n\t\t\tNFAs = vector<NFA>(h+w);\n\t\t\trange(i,0,h+w){\n\t\t\t\tstring s = \"\";\n\t\t\t\trange(j,1,ss[i].size()-1)s+=ss[i][j];\n\t\t\t\tint cur = 0, e = s.size();\n\t\t\t\tNFAs[i]= NFA::pattern(s,cur,e);\n\t\t\t}\n\t\t\tstats = vector<vector<int>>(h+w);\n\t\t\trange(i,0,h+w) stats[i].push_back(NFAs[i].s);\n\n\t\t\treachable = vector<vector<vector<int>>>(h+w);\n\t\t\trange(i,0,h){\n\t\t\t\treachable[i] = vector<vector<int>>(NFAs[i].N);\n\t\t\t\trange(s,0,NFAs[i].N) reachable[i][s] = vector<int>(h+1);\n\t\t\t}\n\t\t\trange(i,0,w){\n\t\t\t\treachable[h+i] = vector<vector<int>>(NFAs[h+i].N);\n\t\t\t\trange(s,0,NFAs[h+i].N) reachable[h+i][s] = vector<int>(w+1);\n\t\t\t}\n\t\t\trange(i,0,h+w) create_reachable(i);\n\t\t\t\n\t\t\ttrans = vector<vector<vector<vector<int>>>>(h+w);\n\t\t\trange(i,0,h+w){\n\t\t\t\ttrans[i] = vector<vector<vector<int>>>(NFAs[i].N,vector<vector<int>>(26));\n\t\t\t\trange(s,0,NFAs[i].N)range(c,'A','Z'+1)create_trans(i,s,c);\n\t\t\t}\n\n\t\t\t// cerr << NFAs[0].t <<endl;\n\t\t\t// range(i,0,NFAs[0].es.size()){\n\t\t\t// \tcerr << NFAs[0].es[i].c <<\" \" <<NFAs[0].es[i].f <<\" \" <<NFAs[0].es[i].t <<endl;\n\t\t\t// }\n\t\t\t// cerr << NFAs[0].t_cache <<endl;\n\t\t\t\n\t\t\tdfs(0,0,1);\n\t\t\tif(rc > 1){\n\t\t\t\tcout << \"ambiguous\" <<endl; \n\t\t\t}else if(rc == 0){\n\t\t\t\tcout << \"none\" <<endl;\n\t\t\t}else{\n\t\t\t\trange(i,0,h) cout << res[i] <<endl;\n\t\t\t}\n\t\t}\n\t}\n};\n\nint main(){\n\tcout <<fixed<<setprecision(20);\n\tcin.tie(0);\n\tios::sync_with_stdio(false);\n\tMain().run();\n\treturn 0;\n}", "accuracy": 1, "time_ms": 5100, "memory_kb": 29868, "score_of_the_acc": -1.2565, "final_rank": 2 } ]
aoj_1332_cpp
Problem H: Company Organization You started a company a few years ago and fortunately it has been highly successful. As the growth of the company, you noticed that you need to manage employees in a more organized way, and decided to form several groups and assign employees to them. Now, you are planning to form n groups, each of which corresponds to a project in the company. Sometimes you have constraints on members in groups. For example, a group must be a subset of another group because the former group will consist of senior members of the latter group, the members in two groups must be the same because current activities of the two projects are closely related, the members in two groups must not be exactly the same to avoid corruption, two groups cannot have a common employee because of a security reason, and two groups must have a common employee to facilitate collaboration. In summary, by letting X i ( i = 1, ... , n ) be the set of employees assigned to the i -th group, we have five types of constraints as follows. X i ⊆ X j X i = X j X i ≠ X j X i ∩ X j = ∅ X i ∩ X j ≠ ∅ Since you have listed up constraints without considering consistency, it might be the case that you cannot satisfy all the constraints. Constraints are thus ordered according to their priorities, and you now want to know how many constraints of the highest priority can be satisfied. You do not have to take ability of employees into consideration. That is, you can assign anyone to any group. Also, you can form groups with no employee. Furthermore, you can hire or fire as many employees as you want if you can satisfy more constraints by doing so. For example, suppose that we have the following five constraints on three groups in the order of their priorities, corresponding to the first dataset in the sample input. X 2 ⊆ X 1 X 3 ⊆ X 2 X 1 ⊆ X 3 X 1 ≠ X 3 X 3 ⊆ X 1 By assigning the same set of employees to X 1 , X 2 , and X 3 , we can satisfy the first three constraints. However, no matter how we assign employees to X 1 , X 2 , and X 3 , we cannot satisfy the first four highest priority constraints at the same time. Though we can satisfy the first three constraints and the fifth constraint at the same time, the answer should be three. Input The input consists of several datasets. The first line of a dataset consists of two integers n (2 ≤ n ≤ 100) and m (1 ≤ m ≤ 10000), which indicate the number of groups and the number of constraints, respectively. Then, description of m constraints follows. The description of each constraint consists of three integers s (1 ≤ s ≤ 5), i (1 ≤ i ≤ n ), and j (1 ≤ j ≤ n , j ≠= i ), meaning a constraint of the s -th type imposed on the i -th group and the j -th group. The type number of a constraint is as listed above. The constraints are given in the descending order of priority. The input ends with a line containing two zeros. Output For each dataset, output the number of constraints of the highest priority satisfiable at the same time. Sample ...(truncated)
[ { "submission_id": "aoj_1332_10851055", "code_snippet": "#include <cstring>\n#include <cstdio>\n#include <algorithm>\n#include <vector>\n#include <string>\n#include <iostream>\n\nusing namespace std;\n\nconst int MAXN = 1e4+10;\nconst int MAXG = 110;\n\nint n,m;\nint x[MAXN],op[MAXN],y[MAXN];\n\nint f[MAXG][MAXG],g[MAXG][MAXG];\nbool check(int mid) {\n for (int i=1; i<=n; i++)\n for (int j=1; j<=n; j++)\n f[i][j] = 0 ;\n for (int i=1; i<=n; i++) {\n for (int j=1; j<=n; j++) g[i][j]=0;\n g[i][i]=1;\n }\n for (int Q=1; Q<=mid; Q++) {\n int i = x[Q] , j = y[Q] , ope = op[Q];\n if (ope==1) g[i][j] = 1 ;\n else if (ope==2) g[i][j] = g[j][i] = 1 ;\n else if (ope==4) f[i][j] = f[j][i] = 1 ;\n }\n for (int k = 1 ; k <= n ; k++)\n for (int i = 1 ; i <= n ; i++)\n for (int j = 1 ; j <= n ; j++)\n if (g[i][k] && g[k][j]) g[i][j] = 1 ;\n\n for (int k = 1 ; k <= n ; k++)\n for (int i = 1 ; i <= n ; i++)\n for (int j = 1 ; j <= n ; j++)\n if (f[i][k] && g[j][k]) f[i][j] = f[j][i] = 1 ;\n\n for (int Q = 1 ; Q <= mid ; Q++) {\n int i = x[Q] , j = y[Q] , ope = op[Q];\n if (ope==3) {\n if (f[i][i] && f[j][j]) return false ;\n if (g[i][j] && g[j][i]) return false ;\n } else if (ope==5) {\n if (f[i][j] || f[j][i] || f[i][i] || f[j][j]) return false ;\n }\n }\n return true ;\n}\n\nint main() {\n while (~scanf(\"%d%d\",&n,&m) && (n+m)) {\n for (int i=1; i<=m; i++) scanf(\"%d%d%d\",&op[i],&x[i],&y[i]);\n int l=1,r=m,ans=0;\n while (l<=r) {\n int mid=l+r>>1;\n if (check(mid)) {\n ans=mid;\n l=mid+1;\n } else r=mid-1;\n }\n printf(\"%d\\n\",ans);\n }\n return 0;\n}", "accuracy": 1, "time_ms": 360, "memory_kb": 3628, "score_of_the_acc": -0.1514, "final_rank": 10 }, { "submission_id": "aoj_1332_8322631", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\nstruct info {\n\tint t, a, b;\n};\n\nbool solve(int N, int M, const vector<info>& I) {\n\tvector<vector<bool> > adj(N, vector<bool>(N, false));\n\tfor (info s : I) {\n\t\tif (s.t == 1) {\n\t\t\tadj[s.a][s.b] = true;\n\t\t}\n\t\tif (s.t == 2) {\n\t\t\tadj[s.a][s.b] = true;\n\t\t\tadj[s.b][s.a] = true;\n\t\t}\n\t}\n\tvector<vector<bool> > reach = adj;\n\tfor (int i = 0; i < N; i++) {\n\t\treach[i][i] = true;\n\t}\n\tfor (int i = 0; i < N; i++) {\n\t\tfor (int j = 0; j < N; j++) {\n\t\t\tfor (int k = 0; k < N; k++) {\n\t\t\t\tif (reach[j][i] && reach[i][k]) {\n\t\t\t\t\treach[j][k] = true;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n\tvector<vector<bool> > sameval(N, vector<bool>(N, false));\n\tvector<int> empty(N, -1);\n\tfor (info s : I) {\n\t\tif (s.t == 4) {\n\t\t\tfor (int i = 0; i < N; i++) {\n\t\t\t\tif (reach[i][s.a] && reach[i][s.b]) {\n\t\t\t\t\tif (empty[i] == 0) {\n\t\t\t\t\t\treturn false;\n\t\t\t\t\t}\n\t\t\t\t\tempty[i] = 1;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\tif (s.t == 5) {\n\t\t\tfor (int i = 0; i < N; i++) {\n\t\t\t\tif (reach[s.a][i] || reach[s.b][i]) {\n\t\t\t\t\tif (empty[i] == 1) {\n\t\t\t\t\t\treturn false;\n\t\t\t\t\t}\n\t\t\t\t\tempty[i] = 0;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n\tfor (info s : I) {\n\t\tif (s.t == 5) {\n\t\t\tvector<int> v;\n\t\t\tfor (int i = 0; i < N; i++) {\n\t\t\t\tif (reach[s.a][i] || reach[s.b][i]) {\n\t\t\t\t\tv.push_back(i);\n\t\t\t\t}\n\t\t\t}\n\t\t\tfor (int i : v) {\n\t\t\t\tfor (int j : v) {\n\t\t\t\t\tsameval[i][j] = true;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n\tfor (info s : I) {\n\t\tif (s.t == 3 && ((reach[s.a][s.b] && reach[s.b][s.a]) || (empty[s.a] == 1 && empty[s.b] == 1))) {\n\t\t\treturn false;\n\t\t}\n\t\tif (s.t == 4 && sameval[s.a][s.b]) {\n\t\t\treturn false;\n\t\t}\n\t}\n\treturn true;\n}\n\nint main() {\n\twhile (true) {\n\t\tint N, M;\n\t\tcin >> N >> M;\n\t\tif (N == 0 && M == 0) {\n\t\t\tbreak;\n\t\t}\n\t\tvector<info> I(M);\n\t\tfor (int i = 0; i < M; i++) {\n\t\t\tcin >> I[i].t >> I[i].a >> I[i].b;\n\t\t\tI[i].a -= 1;\n\t\t\tI[i].b -= 1;\n\t\t}\n\t\tint l = 0, r = M + 1;\n\t\twhile (r - l > 1) {\n\t\t\tint m = (l + r) / 2;\n\t\t\tvector<info> J(I.begin(), I.begin() + m);\n\t\t\tif (solve(N, M, J)) {\n\t\t\t\tl = m;\n\t\t\t}\n\t\t\telse {\n\t\t\t\tr = m;\n\t\t\t}\n\t\t}\n\t\tcout << l << endl;\n\t}\n\treturn 0;\n}", "accuracy": 1, "time_ms": 340, "memory_kb": 3620, "score_of_the_acc": -0.1469, "final_rank": 9 }, { "submission_id": "aoj_1332_8321954", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\nclass unionfind {\nprivate:\n\tint n;\n\tvector<int> par;\npublic:\n\tunionfind() : n(0), par(vector<int>()) {}\n\tunionfind(int n_) : n(n_) {\n\t\tpar.resize(n);\n\t\tfor (int i = 0; i < n; i++) {\n\t\t\tpar[i] = i;\n\t\t}\n\t}\n\tint root(int x) {\n\t\tif (x == par[x]) {\n\t\t\treturn x;\n\t\t}\n\t\treturn par[x] = root(par[x]);\n\t}\n\tvoid link(int x, int y) {\n\t\tx = root(x);\n\t\ty = root(y);\n\t\tpar[y] = x;\n\t}\n\tbool connected(int x, int y) {\n\t\treturn root(x) == root(y);\n\t}\n};\n\nstruct info {\n\tint t, a, b;\n};\n\nbool solve_dag(int N, int M, const vector<info>& I) {\n\tvector<vector<bool> > adj(N, vector<bool>(N, false));\n\tfor (info s : I) {\n\t\tif (s.t == 1) {\n\t\t\tadj[s.a][s.b] = true;\n\t\t}\n\t}\n\tvector<vector<bool> > reach = adj;\n\tfor (int i = 0; i < N; i++) {\n\t\treach[i][i] = true;\n\t}\n\tfor (int i = 0; i < N; i++) {\n\t\tfor (int j = 0; j < N; j++) {\n\t\t\tfor (int k = 0; k < N; k++) {\n\t\t\t\tif (reach[j][i] && reach[i][k]) {\n\t\t\t\t\treach[j][k] = true;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n\tvector<vector<bool> > sameval(N, vector<bool>(N, false));\n\tvector<int> empty(N, -1);\n\tfor (info s : I) {\n\t\tif (s.t == 4) {\n\t\t\tfor (int i = 0; i < N; i++) {\n\t\t\t\tif (reach[i][s.a] && reach[i][s.b]) {\n\t\t\t\t\tif (empty[i] == 0) {\n\t\t\t\t\t\treturn false;\n\t\t\t\t\t}\n\t\t\t\t\tempty[i] = 1;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\tif (s.t == 5) {\n\t\t\tfor (int i = 0; i < N; i++) {\n\t\t\t\tif (reach[s.a][i] || reach[s.b][i]) {\n\t\t\t\t\tif (empty[i] == 1) {\n\t\t\t\t\t\treturn false;\n\t\t\t\t\t}\n\t\t\t\t\tempty[i] = 0;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n\tunionfind uf(N);\n\tfor (info s : I) {\n\t\tif (s.t == 5) {\n\t\t\tvector<int> v;\n\t\t\tfor (int i = 0; i < N; i++) {\n\t\t\t\tif (reach[s.a][i] || reach[s.b][i]) {\n\t\t\t\t\tv.push_back(i);\n\t\t\t\t}\n\t\t\t}\n\t\t\tfor (int i : v) {\n\t\t\t\tfor (int j : v) {\n\t\t\t\t\tsameval[i][j] = true;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n\tfor (info s : I) {\n\t\tif (s.t == 3 && (s.a == s.b || (empty[s.a] == 1 && empty[s.b] == 1))) {\n\t\t\treturn false;\n\t\t}\n\t\tif (s.t == 4 && sameval[s.a][s.b]) {\n\t\t\treturn false;\n\t\t}\n\t}\n\treturn true;\n}\n\nbool solve(int N, int M, const vector<info>& I) {\n\tvector<vector<bool> > adj(N, vector<bool>(N, false));\n\tfor (info s : I) {\n\t\tif (s.t == 1) {\n\t\t\tadj[s.a][s.b] = true;\n\t\t}\n\t\tif (s.t == 2) {\n\t\t\tadj[s.a][s.b] = true;\n\t\t\tadj[s.b][s.a] = true;\n\t\t}\n\t}\n\tvector<bool> vis(N, false);\n\tvector<int> ord;\n\tauto dfs1 = [&](auto& self, int pos) -> void {\n\t\tvis[pos] = true;\n\t\tfor (int i = 0; i < N; i++) {\n\t\t\tif (adj[pos][i] && !vis[i]) {\n\t\t\t\tself(self, i);\n\t\t\t}\n\t\t}\n\t\tord.push_back(pos);\n\t};\n\tfor (int i = N - 1; i >= 0; i--) {\n\t\tif (!vis[i]) {\n\t\t\tdfs1(dfs1, i);\n\t\t}\n\t}\n\treverse(ord.begin(), ord.end());\n\tint counter = 0;\n\tvector<int> col(N, -1);\n\tauto dfs2 = [&](auto& self, int pos) -> void {\n\t\tcol[pos] = counter;\n\t\tfor (int i = 0; i < N; i++) {\n\t\t\tif (adj[i][pos] && col[i] == -1) {\n\t\t\t\tself(self, i);\n\t\t\t}\n\t\t}\n\t};\n\tfor (int i : ord) {\n\t\tif (col[i] == -1) {\n\t\t\tdfs2(dfs2, i);\n\t\t\tcounter += 1;\n\t\t}\n\t}\n\tvector<info> J;\n\tfor (info s : I) {\n\t\tif (s.t == 1 && col[s.a] != col[s.b]) {\n\t\t\tJ.push_back(info{1, col[s.a], col[s.b]});\n\t\t}\n\t\tif (s.t >= 3) {\n\t\t\tJ.push_back(info{s.t, col[s.a], col[s.b]});\n\t\t}\n\t}\n\treturn solve_dag(counter, J.size(), J);\n}\n\nint main() {\n\twhile (true) {\n\t\tint N, M;\n\t\tcin >> N >> M;\n\t\tif (N == 0 && M == 0) {\n\t\t\tbreak;\n\t\t}\n\t\tvector<info> I(M);\n\t\tfor (int i = 0; i < M; i++) {\n\t\t\tcin >> I[i].t >> I[i].a >> I[i].b;\n\t\t\tI[i].a -= 1;\n\t\t\tI[i].b -= 1;\n\t\t}\n\t\tint l = 0, r = M + 1;\n\t\twhile (r - l > 1) {\n\t\t\tint m = (l + r) / 2;\n\t\t\tvector<info> J(I.begin(), I.begin() + m);\n\t\t\tif (solve(N, M, J)) {\n\t\t\t\tl = m;\n\t\t\t}\n\t\t\telse {\n\t\t\t\tr = m;\n\t\t\t}\n\t\t}\n\t\tcout << l << endl;\n\t}\n\treturn 0;\n}", "accuracy": 1, "time_ms": 200, "memory_kb": 3716, "score_of_the_acc": -0.1202, "final_rank": 8 }, { "submission_id": "aoj_1332_6402290", "code_snippet": "#pragma GCC optimize(\"O3\")\n#pragma GCC optimize(\"unroll-loops\")\n#include<iostream>\n#include<string>\n#include<cstdio>\n#include<vector>\n#include<cmath>\n#include<algorithm>\n#include<functional>\n#include<iomanip>\n#include<queue>\n#include<ciso646>\n#include<random>\n#include<map>\n#include<set>\n#include<bitset>\n#include<stack>\n#include<unordered_map>\n#include<unordered_set>\n#include<utility>\n#include<cassert>\n#include<complex>\n#include<numeric>\n#include<array>\n#include<chrono>\nusing namespace std;\n\n//#define int long long\ntypedef long long ll;\n\ntypedef unsigned long long ul;\ntypedef unsigned int ui;\n//constexpr ll mod = 998244353;\nconstexpr ll mod = 1000000007;\nconst ll INF = mod * mod;\ntypedef pair<int, int>P;\n\n#define rep(i,n) for(int i=0;i<n;i++)\n#define per(i,n) for(int i=n-1;i>=0;i--)\n#define Rep(i,sta,n) for(int i=sta;i<n;i++)\n#define rep1(i,n) for(int i=1;i<=n;i++)\n#define per1(i,n) for(int i=n;i>=1;i--)\n#define Rep1(i,sta,n) for(int i=sta;i<=n;i++)\n#define all(v) (v).begin(),(v).end()\ntypedef pair<ll, ll> LP;\n\ntemplate<typename T>\nvoid chmin(T& a, T b) {\n\ta = min(a, b);\n}\ntemplate<typename T>\nvoid chmax(T& a, T b) {\n\ta = max(a, b);\n}\ntemplate<typename T>\nvoid cinarray(vector<T>& v) {\n\trep(i, v.size())cin >> v[i];\n}\ntemplate<typename T>\nvoid coutarray(vector<T>& v) {\n\trep(i, v.size()) {\n\t\tif (i > 0)cout << \" \"; cout << v[i];\n\t}\n\tcout << \"\\n\";\n}\nll mod_pow(ll x, ll n, ll m = mod) {\n\tif (n < 0) {\n\t\tll res = mod_pow(x, -n, m);\n\t\treturn mod_pow(res, m - 2, m);\n\t}\n\tif (abs(x) >= m)x %= m;\n\tif (x < 0)x += m;\n\t//if (x == 0)return 0;\n\tll res = 1;\n\twhile (n) {\n\t\tif (n & 1)res = res * x % m;\n\t\tx = x * x % m; n >>= 1;\n\t}\n\treturn res;\n}\nstruct modint {\n\tint n;\n\tmodint() :n(0) { ; }\n\tmodint(ll m) {\n\t\tif (m < 0 || mod <= m) {\n\t\t\tm %= mod; if (m < 0)m += mod;\n\t\t}\n\t\tn = m;\n\t}\n\toperator int() { return n; }\n};\nbool operator==(modint a, modint b) { return a.n == b.n; }\nmodint operator+=(modint& a, modint b) { a.n += b.n; if (a.n >= mod)a.n -= mod; return a; }\nmodint operator-=(modint& a, modint b) { a.n -= b.n; if (a.n < 0)a.n += mod; return a; }\nmodint operator*=(modint& a, modint b) { a.n = ((ll)a.n * b.n) % mod; return a; }\nmodint operator+(modint a, modint b) { return a += b; }\nmodint operator-(modint a, modint b) { return a -= b; }\nmodint operator*(modint a, modint b) { return a *= b; }\nmodint operator^(modint a, ll n) {\n\tif (n == 0)return modint(1);\n\tmodint res = (a * a) ^ (n / 2);\n\tif (n % 2)res = res * a;\n\treturn res;\n}\n\nll inv(ll a, ll p) {\n\treturn (a == 1 ? 1 : (1 - p * inv(p % a, a)) / a + p);\n}\nmodint operator/(modint a, modint b) { return a * modint(inv(b, mod)); }\nmodint operator/=(modint& a, modint b) { a = a / b; return a; }\nconst int max_n = 1 << 20;\nmodint fact[max_n], factinv[max_n];\nvoid init_f() {\n\tfact[0] = modint(1);\n\tfor (int i = 0; i < max_n - 1; i++) {\n\t\tfact[i + 1] = fact[i] * modint(i + 1);\n\t}\n\tfactinv[max_n - 1] = modint(1) / fact[max_n - 1];\n\tfor (int i = max_n - 2; i >= 0; i--) {\n\t\tfactinv[i] = factinv[i + 1] * modint(i + 1);\n\t}\n}\nmodint comb(int a, int b) {\n\tif (a < 0 || b < 0 || a < b)return 0;\n\treturn fact[a] * factinv[b] * factinv[a - b];\n}\nmodint combP(int a, int b) {\n\tif (a < 0 || b < 0 || a < b)return 0;\n\treturn fact[a] * factinv[a - b];\n}\n\nll gcd(ll a, ll b) {\n\ta = abs(a); b = abs(b);\n\tif (a < b)swap(a, b);\n\twhile (b) {\n\t\tll r = a % b; a = b; b = r;\n\t}\n\treturn a;\n}\ntypedef long double ld;\ntypedef pair<ld, ld> LDP;\nconst ld eps = 1e-8;\nconst ld pi = acosl(-1.0);\ntemplate<typename T>\nvoid addv(vector<T>& v, int loc, T val) {\n\tif (loc >= v.size())v.resize(loc + 1, 0);\n\tv[loc] += val;\n}\n/*const int mn = 100005;\nbool isp[mn];\nvector<int> ps;\nvoid init() {\n\tfill(isp + 2, isp + mn, true);\n\tfor (int i = 2; i < mn; i++) {\n\t\tif (!isp[i])continue;\n\t\tps.push_back(i);\n\t\tfor (int j = 2 * i; j < mn; j += i) {\n\t\t\tisp[j] = false;\n\t\t}\n\t}\n}*/\n\n//[,val)\ntemplate<typename T>\nauto prev_itr(set<T>& st, T val) {\n\tauto res = st.lower_bound(val);\n\tif (res == st.begin())return st.end();\n\tres--; return res;\n}\n\n//[val,)\ntemplate<typename T>\nauto next_itr(set<T>& st, T val) {\n\tauto res = st.lower_bound(val);\n\treturn res;\n}\nusing mP = pair<modint, modint>;\n\n\nint dx[4] = { 1,0,-1,0 };\nint dy[4] = { 0,1,0,-1 };\n//-----------------------------------------\n\nstruct graph {\nprivate:\n\tint n;\n\tvector<vector<int>> G, rG;\n\tvector<bool> used;\n\tvector<int> vs;\n\n\tint mk;\n\tvector<vector<int>> fG;\n\tvector<vector<int>> ori;\n\tvector<int> trans;\npublic:\n\tgraph(int sz) {\n\t\tn = sz;\n\t\tG.resize(n);\n\t\trG.resize(n);\n\t\tused.resize(n);\n\n\t\tfG.resize(n);\n\t\ttrans.resize(n, -1);\n\t\tori.resize(n);\n\t}\n\tvoid add_edge(int a, int b) {\n\t\tG[a].push_back(b);\n\t\trG[b].push_back(a);\n\t}\n\tvoid dfs(int v) {\n\t\tused[v] = true;\n\t\trep(i, G[v].size()) {\n\t\t\tif (!used[G[v][i]])dfs(G[v][i]);\n\t\t}\n\t\tvs.push_back(v);\n\t}\n\tvoid rdfs(int v, int k) {\n\t\tused[v] = true;\n\t\tqueue<int> q; q.push(v);\n\t\tvector<int> c;\n\t\twhile (!q.empty()) {\n\t\t\tint id = q.front(); q.pop();\n\t\t\tori[k].push_back(id);\n\t\t\trep(j, rG[id].size()) {\n\t\t\t\tint to = rG[id][j];\n\t\t\t\tif (used[to]) {\n\t\t\t\t\tif (trans[to] >= 0)c.push_back(trans[to]);\n\t\t\t\t\tcontinue;\n\t\t\t\t}\n\t\t\t\tused[to] = true; q.push(to);\n\t\t\t}\n\t\t}\n\t\tsort(c.begin(), c.end());\n\t\tint len = unique(c.begin(), c.end()) - c.begin();\n\t\trep(i, len) {\n\t\t\tfG[c[i]].push_back(k);\n\t\t}\n\t\trep(i, ori[k].size()) {\n\t\t\ttrans[ori[k][i]] = k;\n\t\t}\n\t}\n\tvoid scc() {\n\t\tfill(used.begin(), used.end(), false);\n\t\trep(i, n) {\n\t\t\tif (!used[i])dfs(i);\n\t\t}\n\t\tfill(used.begin(), used.end(), false);\n\t\tint k = 0;\n\t\tper(i, (int)vs.size()) {\n\t\t\tif (!used[vs[i]]) {\n\t\t\t\trdfs(vs[i], k); k++;\n\t\t\t}\n\t\t}\n\t\tmk = k;\n\t}\n\tbool eq(int a, int b) {\n\t\treturn trans[a] == trans[b];\n\t}\n\tbool istwosat(int sup) {\n\t\trep(i, sup)if (trans[i] == trans[i + sup])return false;\n\t\treturn true;\n\t}\n};\n\nbool can[100][100];\nbool ss[100][100];\nint n, m;\nvoid solve() {\n\tvector<int> typ(m), a(m), b(m);\n\trep(i, m) {\n\t\tcin >> typ[i] >> a[i] >> b[i];\n\t\ta[i]--; b[i]--;\n\t}\n\tauto ok = [&](int c) {\n\t\tvector<vector<int>> G(n);\n\t\trep(i, n)rep(j, n) {\n\t\t\tcan[i][j] = false;\n\t\t}\n\t\trep(i, c) {\n\t\t\tif (typ[i] == 1) {\n\t\t\t\tG[a[i]].push_back(b[i]);\n\t\t\t}\n\t\t\telse if (typ[i] == 2) {\n\t\t\t\tG[a[i]].push_back(b[i]);\n\t\t\t\tG[b[i]].push_back(a[i]);\n\t\t\t}\n\t\t}\n\t\trep(i, n) {\n\t\t\tcan[i][i] = true;\n\t\t\tqueue<int> q; q.push(i);\n\t\t\twhile (!q.empty()) {\n\t\t\t\tint id = q.front(); q.pop();\n\t\t\t\tfor (int to : G[id]) {\n\t\t\t\t\tif (!can[i][to]) {\n\t\t\t\t\t\tcan[i][to] = true;\n\t\t\t\t\t\tq.push(to);\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\t//equal check\n\t\tgraph g(n);\n\t\trep(i, c) {\n\t\t\tif (typ[i] == 1) {\n\t\t\t\tg.add_edge(a[i], b[i]);\n\t\t\t}\n\t\t\telse if (typ[i] == 2) {\n\t\t\t\tg.add_edge(a[i], b[i]);\n\t\t\t\tg.add_edge(b[i], a[i]);\n\t\t\t}\n\t\t}\n\t\tg.scc();\n\t\trep(i, c)if (typ[i] == 3) {\n\t\t\tif (g.eq(a[i], b[i])) {\n\t\t\t\treturn false;\n\t\t\t}\n\t\t}\n\t\t//2-sat about empty set\n\t\tgraph st(2 * n);\n\t\trep(i, c) {\n\t\t\tif (typ[i] == 1) {\n\t\t\t\tst.add_edge(b[i], a[i]);\n\t\t\t\tst.add_edge(a[i] + n, b[i] + n);\n\t\t\t}\n\t\t\telse if (typ[i] == 2) {\n\t\t\t\tst.add_edge(a[i], b[i]);\n\t\t\t\tst.add_edge(b[i], a[i]);\n\t\t\t\tst.add_edge(a[i] + n, b[i] + n);\n\t\t\t\tst.add_edge(b[i] + n, a[i] + n);\n\t\t\t}\n\t\t\telse if (typ[i] == 3) {\n\t\t\t\tst.add_edge(a[i], b[i] + n);\n\t\t\t\tst.add_edge(b[i], a[i] + n);\n\t\t\t\tif (can[a[i]][b[i]])st.add_edge(b[i], b[i] + n);\n\t\t\t\tif (can[b[i]][a[i]])st.add_edge(a[i], a[i] + n);\n\t\t\t}\n\t\t\telse if (typ[i] == 4) {\n\t\t\t\tif (can[a[i]][b[i]]) {\n\t\t\t\t\tst.add_edge(a[i] + n, a[i]);\n\t\t\t\t}\n\t\t\t\tif (can[b[i]][a[i]]) {\n\t\t\t\t\tst.add_edge(b[i] + n, b[i]);\n\t\t\t\t}\n\t\t\t}\n\t\t\telse if (typ[i] == 5) {\n\t\t\t\tst.add_edge(a[i], a[i] + n);\n\t\t\t\tst.add_edge(b[i], b[i] + n);\n\t\t\t}\n\t\t}\n\t\t\n\t\t//conflict between 4 and 5\n\t\trep(i, n)rep(j, n) {\n\t\t\tss[i][j] = false;\n\t\t}\n\t\trep(i, c)if (typ[i] == 5) {\n\t\t\tss[a[i]][b[i]] = ss[b[i]][a[i]] = true;\n\t\t}\n\t\trep(i, n) {\n\t\t\tqueue<int> q;\n\t\t\trep(j, n) {\n\t\t\t\tif (ss[i][j]) {\n\t\t\t\t\tq.push(j);\n\t\t\t\t}\n\t\t\t}\n\t\t\twhile (!q.empty()) {\n\t\t\t\tint id = q.front(); q.pop();\n\t\t\t\tfor (int to : G[id]) {\n\t\t\t\t\tif (!ss[i][to]) {\n\t\t\t\t\t\tss[i][to] = true;\n\t\t\t\t\t\tq.push(to);\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\trep(j, n) {\n\t\t\tqueue<int> q;\n\t\t\trep(i, n)if (ss[i][j])q.push(i);\n\t\t\twhile (!q.empty()) {\n\t\t\t\tint id = q.front(); q.pop();\n\t\t\t\tfor (int to : G[id]) {\n\t\t\t\t\tif (!ss[to][j]) {\n\t\t\t\t\t\tss[to][j] = true;\n\t\t\t\t\t\tq.push(to);\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\trep(i, c)if (typ[i] == 4) {\n\t\t\tif (ss[a[i]][b[i]])return false;\n\t\t}\n\n\t\trep(i, n) {\n\t\t\tbool exi = false;\n\t\t\trep(j, n)if (ss[i][j])exi = true;\n\t\t\tif (exi)st.add_edge(i, i + n);\n\t\t}\n\t\trep(i, c)if (typ[i] == 4) {\n\t\t\trep(pre, n) {\n\t\t\t\tif (can[pre][a[i]] && can[pre][b[i]]) {\n\t\t\t\t\tst.add_edge(pre+n,pre);\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\tst.scc();\n\t\t//cout << \"nande \" << c << \"\\n\";\n\t\tif (!st.istwosat(n)) {\n\t\t\treturn false;\n\t\t}\n\t\treturn true;\n\t};\n\tint le = 0;\n\tint ri = m + 1;\n\twhile (ri - le > 1) {\n\t\tint x = (le + ri) / 2;\n\t\tif (ok(x))le = x;\n\t\telse ri = x;\n\t}\n\tcout << le << \"\\n\";\n\t\n}\n\nsigned main() {\n\tios::sync_with_stdio(false);\n\tcin.tie(0);\n\tcout << fixed << setprecision(10);\n\t//init_f();\n\t//init();\n\t//while(true)\n\t//expr();\n\t//int t; cin >> t; rep(i, t)\n\twhile(cin>>n>>m,n)\n\tsolve();\n\treturn 0;\n}", "accuracy": 1, "time_ms": 470, "memory_kb": 30884, "score_of_the_acc": -1.0917, "final_rank": 14 }, { "submission_id": "aoj_1332_3974896", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\n#define REP(i,n) for(int i=0; i<(int)(n); i++)\n#define FOR(i,a,b) for(int i=(int)(a); i<(int)(b); i++)\n#define FORR(i,a,b) for(int i=(int)(b)-1; i>=(int)(a); i--)\n\nusing ll = long long;\nusing pii = pair<int,int>;\nusing pid = pair<int,double>;\n\nint n,m;\nbool ok;\nbool g[252][252];\nint ty[12525], xi[12525], xj[12525];\nvector<int> fook[252][252];\n\nvoid f(int id){\n int s = xi[id], t = xj[id];\n if(ty[id] == 3){\n if(g[s][t] && g[t][s]){\n ok = false;\n }else if(g[s][s+n] && g[t][t+n]){\n ok = false;\n }\n }else if(ty[id] == 5){\n if(g[s][s+n] || g[t][t+n]){\n ok = false;\n }else if(g[s][t+n] && g[t][s+n]){\n ok = false;\n }\n }\n}\n\nvoid add_edge(int i, int j){\n if(g[i][j])return;\n g[i][j] = true;\n // (i,j) check\n for(int id : fook[i][j]){\n f(id);\n }\n // add others\n REP(s,2*n)if(g[s][i] && !g[s][j]){\n add_edge(s,j);\n }\n REP(t,2*n)if(g[j][t] && !g[i][t]){\n add_edge(i,t);\n }\n if(i+n==j){\n REP(s,n)if(g[s][i]){\n add_edge(i,s);\n add_edge(s,s+n);\n add_edge(s+n,i+n);\n }\n }\n if(i<n && j<n && g[j][j+n] && !g[j][i]){\n add_edge(i,i+n);\n add_edge(j,i);\n add_edge(i+n,j+n);\n }\n}\n\nint solve(){\n // initialize\n ok = true;\n REP(i,2*n)REP(j,2*n)g[i][j] = false;\n REP(i,2*n)REP(j,2*n)fook[i][j].clear();\n REP(i,m){\n int s = xi[i], t = xj[i];\n if(ty[i]==1){\n add_edge(s,t);\n add_edge(t+n,s+n);\n }else if(ty[i]==2){\n add_edge(s,t);\n add_edge(t+n,s+n);\n add_edge(t,s);\n add_edge(s+n,t+n);\n }else if(ty[i]==4){\n add_edge(s,t+n);\n add_edge(t,s+n);\n }else if(ty[i]==3){\n f(i);\n fook[s][s+n].push_back(i);\n fook[t][t+n].push_back(i);\n fook[s][t].push_back(i);\n fook[t][s].push_back(i);\n }else if(ty[i]==5){\n f(i);\n fook[s][s+n].push_back(i);\n fook[t][t+n].push_back(i);\n fook[s][t+n].push_back(i);\n fook[t][s+n].push_back(i);\n }\n if(!ok){\n return i;\n }\n }\n return m;\n}\n\nint naive(){\n int n2 = 1<<n;\n int ans = 0;\n REP(s,1<<n2){\n vector<int> ss(n);\n REP(i,n){\n REP(j,n2)if((j>>i&1) && (s>>j&1))ss[i] += 1<<j;\n }\n bool ok = true;\n REP(i,m){\n if(ty[i]==1){\n if((ss[xi[i]] & ss[xj[i]]) != ss[xi[i]])ok=false;\n }else if(ty[i]==2){\n if(ss[xi[i]] != ss[xj[i]])ok=false;\n }else if(ty[i]==3){\n if(ss[xi[i]] == ss[xj[i]])ok=false;\n }else if(ty[i]==4){\n if((ss[xi[i]] & ss[xj[i]]) != 0)ok=false;\n }else if(ty[i]==5){\n if((ss[xi[i]] & ss[xj[i]]) == 0)ok=false;\n }\n if(!ok){\n ans = max(ans, i);\n break;\n }\n }\n if(ok){\n ans = m;\n break;\n }\n }\n return ans;\n}\n\nint main(){\n while(false){\n n = 3;\n m = 8;\n REP(i,m){\n ty[i] = rand() % 5 + 1;\n xi[i] = rand() % n;\n xj[i] = rand() % (n-1);\n if(xj[i] >= xi[i])xj[i]++;\n }\n int ans1 = solve();\n int ans2 = naive();\n if(ans1 != ans2){\n printf(\"%d %d\\n\", n,m);\n REP(i,m)printf(\"%d %d %d\\n\",ty[i],xi[i]+1,xj[i]+1);\n printf(\"ans1: %d\\n\",ans1);\n printf(\"ans2: %d\\n\",ans2);\n return 0;\n }\n }\n while(true){\n scanf(\"%d%d\",&n,&m);\n if(n+m==0)break;\n REP(i,m)scanf(\"%d%d%d\",ty+i,xi+i,xj+i),xi[i]--,xj[i]--;\n printf(\"%d\\n\", solve());\n }\n return 0;\n}", "accuracy": 1, "time_ms": 40, "memory_kb": 6572, "score_of_the_acc": -0.1822, "final_rank": 11 }, { "submission_id": "aoj_1332_3711792", "code_snippet": "#include<bits/stdc++.h>\ntypedef long long int ll;\ntypedef unsigned long long int ull;\n#define BIG_NUM 2000000000\n#define HUGE_NUM 99999999999999999\n#define MOD 1000000007\n#define EPS 0.000000001\nusing namespace std;\n\n\n#define SIZE 105\n\nenum Type{\n\tYES,\n\tNO,\n\tUNDEFINED,\n};\n\nenum Relation{\n\tIS_SUBSET_OF,\n\tEQUAL,\n\tDIFFERENT,\n\tNO_COMMON,\n\tHAVE_COMMON,\n};\n\nstruct Info{\n\tRelation relation;\n\tint LEFT,RIGHT;\n};\n\nint N,M;\nInfo info[SIZE*SIZE];\nType is_Empty[SIZE],is_Subset[SIZE][SIZE],have_Common[SIZE][SIZE];\nRelation relate_array[5] = {IS_SUBSET_OF,EQUAL,DIFFERENT,NO_COMMON,HAVE_COMMON};\n\n\nbool func_Empty(int base){ //baseが新しく空集合であることが確定した\n\n\t//空集合の部分集合は、全て空集合でなければならない\n\tfor(int i = 0; i < N; i++){\n\t\tif(is_Subset[i][base] == YES){\n\t\t\tif(is_Empty[i] == NO){\n\n\t\t\t\treturn false;\n\n\t\t\t}else{\n\n\t\t\t\tis_Empty[i] = YES;\n\t\t\t}\n\t\t}\n\t}\n\n\t//空集合とHAVE_COMMONな要素があってはならない\n\tfor(int i = 0; i < N; i++){\n\t\tif(i != base){\n\t\t\tif(have_Common[i][base] == YES){\n\n\t\t\t\treturn false;\n\n\t\t\t}else{\n\n\t\t\t\thave_Common[i][base] = NO;\n\t\t\t\thave_Common[base][i] = NO;\n\t\t\t}\n\t\t}\n\t}\n\n\treturn true;\n}\n\nbool check(int MID){\n\n\t//テーブルの初期化\n\n\t//空集合かどうか\n\tfor(int i = 0; i < N; i++){\n\n\t\tis_Empty[i] = UNDEFINED;\n\t}\n\n\t//部分集合かどうか\n\tfor(int i = 0; i < N; i++){\n\t\tfor(int k = 0; k < N; k++){\n\t\t\tif(k == i){\n\n\t\t\t\tis_Subset[k][i] = YES;\n\t\t\t}\n\t\t\telse{\n\n\t\t\t\tis_Subset[k][i] = UNDEFINED;\n\t\t\t}\n\t\t}\n\t}\n\n\t//共通の要素があるかどうか\n\tfor(int i = 0; i < N; i++){\n\t\tfor(int k = 0; k < N; k++){\n\t\t\thave_Common[k][i] = UNDEFINED; //空集合なら、φ∩Φ = Φ\n\t\t}\n\t}\n\n\t//まずは、部分集合関係を演算する\n\tfor(int i = 0; i <= MID; i++){\n\t\tif(info[i].relation == IS_SUBSET_OF){\n\n\t\t\tis_Subset[info[i].LEFT][info[i].RIGHT] = YES; //LEFTがRIGHTの部分集合\n\n\t\t}else if(info[i].relation == EQUAL){\n\n\t\t\tis_Subset[info[i].LEFT][info[i].RIGHT] = YES;\n\t\t\tis_Subset[info[i].RIGHT][info[i].LEFT] = YES;\n\t\t}\n\t}\n\n\t//ワーシャルフロイドで情報を伝達\n\tfor(int mid = 0; mid < N; mid++){\n\t\tfor(int start = 0; start < N; start++){\n\t\t\tif(is_Subset[start][mid] != YES)continue;\n\t\t\tfor(int goal = 0; goal < N; goal++){\n\t\t\t\tif(is_Subset[mid][goal] != YES)continue;\n\n\t\t\t\tis_Subset[start][goal] = YES;\n\t\t\t}\n\t\t}\n\t}\n\n\t//共通要素を持つなら空集合ではない\n\tfor(int i = 0; i <= MID; i++){\n\t\tif(info[i].relation == HAVE_COMMON){\n\t\t\tis_Empty[info[i].LEFT] = NO;\n\t\t\tis_Empty[info[i].RIGHT] = NO;\n\n\t\t\thave_Common[info[i].LEFT][info[i].RIGHT] = YES;\n\t\t\thave_Common[info[i].RIGHT][info[i].LEFT] = YES;\n\n\t\t\t//LEFT∩RIGHT != Φなので、[LEFTおよびLEFTとおなじもの]は[RIGHTおよびRIGHTと同じもの]とHAVE_COMMONになる\n\t\t\tfor(int a = 0; a < N; a++){\n\t\t\t\tif(is_Subset[a][info[i].LEFT] == YES && is_Subset[info[i].LEFT][a] == YES){\n\t\t\t\t\tfor(int b = 0; b < N; b++){\n\t\t\t\t\t\tif(is_Subset[b][info[i].RIGHT] == YES && is_Subset[info[i].RIGHT][b] == YES){\n\n\t\t\t\t\t\t\tis_Empty[a] = NO;\n\t\t\t\t\t\t\tis_Empty[b] = NO;\n\n\t\t\t\t\t\t\thave_Common[a][b] = YES;\n\t\t\t\t\t\t\thave_Common[b][a] = YES;\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n\n\t//LEFT∩RIGHT = Φの場合を処理\n\tfor(int i = 0; i <= MID; i++){\n\t\tif(info[i].relation == NO_COMMON){\n\n\t\t\t//LEFT∩RIGHT = Φより、A⊆LEFT,A⊆RIGHTとなるAについて、A = Φが成り立つ\n\t\t\tfor(int k = 0; k < N; k++){\n\t\t\t\tif(is_Subset[k][info[i].LEFT] == YES && is_Subset[k][info[i].RIGHT] == YES){\n\n\t\t\t\t\tif(is_Empty[k] == NO){\n\t\t\t\t\t\treturn false;\n\t\t\t\t\t}else{\n\n\t\t\t\t\t\tis_Empty[k] = YES;\n\t\t\t\t\t\tif(!func_Empty(k)){ //情報の反映\n\t\t\t\t\t\t\treturn false;\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\n\t\t\t//矛盾\n\t\t\tif(have_Common[info[i].LEFT][info[i].RIGHT] == YES){\n\n\t\t\t\treturn false;\n\n\t\t\t}else{\n\n\t\t\t\thave_Common[info[i].LEFT][info[i].RIGHT] = NO;\n\t\t\t}\n\n\t\t\t//LEFT⊆RIGHTでNO_COMMONなら、少なくともLEFTは空集合\n\t\t\tif(is_Subset[info[i].LEFT][info[i].RIGHT] == YES){\n\n\t\t\t\tif(is_Empty[info[i].LEFT] == NO){\n\n\t\t\t\t\treturn false;\n\n\t\t\t\t}else{\n\n\t\t\t\t\tis_Empty[info[i].LEFT] = YES;\n\t\t\t\t\tif(!func_Empty(info[i].LEFT)){\n\t\t\t\t\t\treturn false;\n\t\t\t\t\t}\n\n\t\t\t\t}\n\t\t\t}\n\n\t\t\t//RIGHT⊆LEFTでNO_COMMONなら、少なくともRIGHTは空集合\n\t\t\tif(is_Subset[info[i].RIGHT][info[i].LEFT] == YES){\n\t\t\t\tif(is_Empty[info[i].RIGHT] == NO){\n\n\t\t\t\t\treturn false;\n\n\t\t\t\t}else{\n\n\t\t\t\t\tis_Empty[info[i].RIGHT] = YES;\n\t\t\t\t\tif(!func_Empty(info[i].RIGHT)){\n\t\t\t\t\t\treturn false;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n\n\n\t//DIFFERENTを確認\n\tfor(int i = 0; i <= MID; i++){\n\t\tif(info[i].relation != DIFFERENT)continue;\n\n\t\tif(is_Subset[info[i].LEFT][info[i].RIGHT] == YES &&\n\t\t\t\tis_Subset[info[i].RIGHT][info[i].LEFT] == YES){\n\n\t\t\treturn false;\n\n\t\t}else if(is_Empty[info[i].LEFT] == YES && is_Empty[info[i].RIGHT] == YES){ //両方が空集合なら不可\n\n\t\t\treturn false;\n\t\t}\n\t}\n\n\t//emptyをもう一度確認\n\tfor(int i = 0; i < N; i++){\n\t\tif(is_Empty[i] != YES)continue;\n\n\t\t//空集合の部分集合は、全て空集合でなければならない\n\t\tfor(int k = 0; k < N; k++){\n\t\t\tif(is_Subset[k][i] == YES){\n\t\t\t\tif(is_Empty[k] == NO){\n\n\t\t\t\t\treturn false;\n\n\t\t\t\t}else{\n\n\t\t\t\t\tis_Empty[k] = YES;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\n\t\t//空集合とHAVE_COMMONな要素があってはならない\n\t\tfor(int k = 0; k < N; k++){\n\t\t\tif(k != i){\n\t\t\t\tif(have_Common[k][i] == YES){\n\n\t\t\t\t\treturn false;\n\n\t\t\t\t}else{\n\n\t\t\t\t\thave_Common[k][i] = NO;\n\t\t\t\t\thave_Common[i][k] = NO;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n\n\t//have_Commonの情報を更新\n\tfor(int a = 0; a < N; a++){\n\t\tfor(int b = 0; b < N; b++){\n\t\t\tif(have_Common[a][b] != NO)continue;\n\t\t\tfor(int c = 0; c < N; c++){\n\t\t\t\tif(is_Subset[c][a] != YES)continue;\n\t\t\t\tfor(int d = 0; d < N; d++){\n\t\t\t\t\tif(is_Subset[d][b] != YES)continue;\n\n\t\t\t\t\tif(have_Common[c][d] == YES){\n\t\t\t\t\t\treturn false;\n\t\t\t\t\t}else{\n\t\t\t\t\t\thave_Common[c][d] = NO;\n\t\t\t\t\t\thave_Common[d][c] = NO;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n\n\t//HAVE_COMMONを最後に確認\n\tfor(int i = 0; i <= MID; i++){\n\t\tif(info[i].relation != HAVE_COMMON)continue;\n\n\t\tif(have_Common[info[i].LEFT][info[i].RIGHT] == NO){\n\n\t\t\treturn false;\n\n\t\t}else if(is_Empty[info[i].LEFT] == YES || is_Empty[info[i].RIGHT] == YES){\n\n\t\t\treturn false;\n\t\t}\n\t}\n\n\treturn true;\n}\n\nvoid func(){\n\n\tint command;\n\n\tfor(int i = 0; i < M; i++){\n\n\t\tscanf(\"%d %d %d\",&command,&info[i].LEFT,&info[i].RIGHT);\n\n\t\tcommand--;\n\n\t\tinfo[i].LEFT--;\n\t\tinfo[i].RIGHT--;\n\t\tinfo[i].relation = relate_array[command];\n\t}\n\n\t//2分探索で、条件を満たす最大行数を求める\n\tint ans = -1;\n\tint L = 0,R = M-1,MID = (L+R)/2;\n\n\twhile(L <= R){\n\n\t\tif(check(MID)){\n\n\t\t\tans = MID;\n\t\t\tL = MID+1;\n\n\t\t}else{\n\n\t\t\tR = MID-1;\n\t\t}\n\t\tMID = (L+R)/2;\n\t}\n\n\tprintf(\"%d\\n\",ans+1);\n}\n\nint main(){\n\n\twhile(true){\n\n\t\tscanf(\"%d %d\",&N,&M);\n\t\tif(N == 0 && M == 0)break;\n\n\t\tfunc();\n\t}\n\n\treturn 0;\n}", "accuracy": 1, "time_ms": 4730, "memory_kb": 3424, "score_of_the_acc": -1.0763, "final_rank": 13 }, { "submission_id": "aoj_1332_1696925", "code_snippet": "#include <bits/stdc++.h>\n#define rep(i,n) for(int i=0;i<(int)(n);i++)\n#define rep1(i,n) for(int i=1;i<=(int)(n);i++)\n#define all(c) c.begin(),c.end()\n#define pb push_back\n#define fs first\n#define sc second\n#define show(x) cout << #x << \" = \" << x << endl\n#define chmin(x,y) x=min(x,y)\n#define chmax(x,y) x=max(x,y)\nusing namespace std;\nint N,M,t[10000],v[10000],u[10000];\nbool can(int M){\n\tbool e[100][100]={},emp[10000]={},no[100][100]={};\n\trep(i,N) e[i][i]=1;\n\trep(i,M){\n\t\tif(t[i]==1) e[u[i]][v[i]]=1;\n\t\tif(t[i]==2) e[u[i]][v[i]]=e[v[i]][u[i]]=1;\n\t}\n\trep(i,N) rep(j,N) rep(k,N) e[j][k]|=(e[j][i]&&e[i][k]);\n\trep(i,M){\n\t\tif(t[i]==4){\n\t\t\tint a=v[i],b=u[i];\n\t\t\trep(c,N) if(e[a][c]&&e[b][c]) emp[c]=1;\n\t\t\tno[a][b]=no[b][a]=1;\n\t\t}\n\t}\n\trep(i,N) if(emp[i]){\n\t\trep(j,N) if(e[i][j]) emp[j]=1;\n\t}\n\trep(i,N) rep(j,N) if(no[i][j]){\n\t\trep(I,N) rep(J,N){\n\t\t\tif(e[i][I]&&e[j][J]){\n\t\t\t\tno[I][J]=1;\n\t\t\t}\n\t\t}\n\t}\n\t// puts(\"no\");\n\t// rep(i,N){\n\t// \trep(j,N) cout<<no[i][j]<<\" \";\n\t// \tputs(\"\");\n\t// }\n\trep(i,M){\n\t\tint a=v[i],b=u[i];\n\t\tif(t[i]==3){\n\t\t\tif(emp[a]&&emp[b]) return 0;\n\t\t\tif(e[a][b]&&e[b][a]) return 0;\n\t\t}\n\t\tif(t[i]==5){\n\t\t\tif(emp[a]||emp[b]) return 0;\n\t\t\tif(no[a][b]) return 0;\n\t\t}\n\t}\n\treturn 1;\n}\nint main(){\n\twhile(true){\n\t\tcin>>N>>M;\n\t\tif(N==0) break;\n\t\trep(i,M){\n\t\t\tcin>>t[i]>>v[i]>>u[i],v[i]--,u[i]--;\n\t\t}\n\t\tint ub=M+1,lb=0;\n\t\twhile(ub-lb>1){\n\t\t\tint m=(ub+lb)/2;\n\t\t\tif(can(m)) lb=m;\n\t\t\telse ub=m;\n\t\t}\n\t\tcout<<lb<<endl;\n\t}\n}", "accuracy": 1, "time_ms": 3740, "memory_kb": 3160, "score_of_the_acc": -0.8563, "final_rank": 12 }, { "submission_id": "aoj_1332_1261646", "code_snippet": "#include<cstdio>\n#include<cstring>\n#include<cmath>\n#include<string>\n#include<algorithm>\n#include<iostream>\n#include<vector>\n#include<queue>\n#include<map>\n#include<set>\n#include<stack>\n\n#define msn(x) (memset((x),0,sizeof((x))))\n#define msi(x) (memset((x),-1,sizeof((x))))\n#define msx(x) (memset((x),0x7f,sizeof((x))))\n#define fuck(x) cerr << #x << \" <- \" << x << endl\n#define acer cout<<\"sb\"<<endl\ntypedef long long ll;\nusing namespace std;\nconst int maxn=107;\nconst int maxm=1e4+7;\nstruct node\n{\n int o,u,v;\n}q[maxm];\nvector<node>cmp;\nint n,m;\nbool dp[maxn][maxn];\nbool emp[maxn][maxn];\nvector<int>a,b;\nbool check(int x)\n{\n //queue< pair<int ,int> >q;\n cmp.clear();\n msn(dp);\n msn(emp);\n for(int i=1;i<=n;i++)dp[i][i]=1;\n for(int i=0;i<x;i++)\n {\n node &now=q[i];\n if(now.o==1)\n {\n dp[now.u][now.v]=1;\n }\n else if(now.o==2)\n {\n dp[now.u][now.v]=dp[now.v][now.u]=1;\n }\n }\n for(int i=1;i<=n;i++)\n {\n for(int j=1;j<=n;j++)\n {\n for(int k=1;k<=n;k++)\n dp[j][k]|=(dp[j][i]&&dp[i][k]);\n }\n }\n\n for(int i=0;i<x;i++)\n {\n node &now=q[i];\n if(now.o==4)cmp.push_back(now);\n }\n // random_shuffle(cmp.begin(),cmp.end());\n for(int z=0;z<cmp.size();z++)\n {\n node &now=cmp[z];\n if(emp[now.u][now.v])continue;\n a.clear(),b.clear();\n for(int i=1;i<=n;i++)if(dp[i][now.u])a.push_back(i);\n for(int i=1;i<=n;i++)if(dp[i][now.v])b.push_back(i);\n for(int i=0;i<a.size();i++)\n {\n for(int j=0;j<b.size();j++)\n emp[b[j]][a[i]]=emp[a[i]][b[j]]=1;\n }\n }\n for(int i=0;i<x;i++)\n {\n node &now=q[i];\n if(now.o==5)\n {\n if(emp[now.u][now.v]||emp[now.u][now.u]||emp[now.v][now.v])return 0;\n }\n }\n for(int i=0;i<x;i++)\n {\n node &now=q[i];\n if(now.o==3)\n {\n if(emp[now.u][now.u]==1&&emp[now.v][now.v]==1)return 0;\n if(dp[now.u][now.v]==1&&dp[now.v][now.u]==1)return 0;\n }\n }\n return 1;\n}\nint main()\n{\n while(scanf(\"%d %d\",&n,&m))\n {\n if(n==0&&m==0)break;\n for(int i=0;i<m;i++)\n {\n scanf(\"%d%d%d\",&q[i].o,&q[i].u,&q[i].v);\n }\n int l=0,r=m;\n while(l<r)\n {\n int mid=(l+r)/2+1;\n if(check(mid))l=mid;\n else r=mid-1;\n }\n printf(\"%d\\n\",l);\n }\n return 0;\n}", "accuracy": 1, "time_ms": 290, "memory_kb": 1556, "score_of_the_acc": -0.0668, "final_rank": 6 }, { "submission_id": "aoj_1332_1261644", "code_snippet": "#include<cstdio>\n#include<cstring>\n#include<cmath>\n#include<string>\n#include<algorithm>\n#include<iostream>\n#include<vector>\n#include<queue>\n#include<map>\n#include<set>\n#include<stack>\n\n#define msn(x) (memset((x),0,sizeof((x))))\n#define msi(x) (memset((x),-1,sizeof((x))))\n#define msx(x) (memset((x),0x7f,sizeof((x))))\n#define fuck(x) cerr << #x << \" <- \" << x << endl\n#define acer cout<<\"sb\"<<endl\ntypedef long long ll;\nusing namespace std;\nconst int maxn=107;\nconst int maxm=1e4+7;\nstruct node\n{\n int o,u,v;\n}q[maxm];\nvector<node>cmp;\nint n,m;\nbool dp[maxn][maxn];\nbool emp[maxn][maxn];\nvector<int>a,b;\nbool check(int x)\n{\n //queue< pair<int ,int> >q;\n cmp.clear();\n msn(dp);\n msn(emp);\n for(int i=1;i<=n;i++)dp[i][i]=1;\n for(int i=0;i<x;i++)\n {\n node &now=q[i];\n if(now.o==1)\n {\n dp[now.u][now.v]=1;\n }\n else if(now.o==2)\n {\n dp[now.u][now.v]=dp[now.v][now.u]=1;\n }\n }\n for(int i=1;i<=n;i++)\n {\n for(int j=1;j<=n;j++)\n {\n for(int k=1;k<=n;k++)\n dp[j][k]|=(dp[j][i]&&dp[i][k]);\n }\n }\n\n for(int i=0;i<x;i++)\n {\n node &now=q[i];\n if(now.o==4)cmp.push_back(now);\n }\n random_shuffle(cmp.begin(),cmp.end());\n for(int z=0;z<cmp.size();z++)\n {\n node &now=cmp[z];\n if(emp[now.u][now.v])continue;\n a.clear(),b.clear();\n for(int i=1;i<=n;i++)if(dp[i][now.u])a.push_back(i);\n for(int i=1;i<=n;i++)if(dp[i][now.v])b.push_back(i);\n for(int i=0;i<a.size();i++)\n {\n for(int j=0;j<b.size();j++)\n emp[b[j]][a[i]]=emp[a[i]][b[j]]=1;\n }\n }\n for(int i=0;i<x;i++)\n {\n node &now=q[i];\n if(now.o==5)\n {\n if(emp[now.u][now.v]||emp[now.u][now.u]||emp[now.v][now.v])return 0;\n }\n }\n for(int i=0;i<x;i++)\n {\n node &now=q[i];\n if(now.o==3)\n {\n if(emp[now.u][now.u]==1&&emp[now.v][now.v]==1)return 0;\n if(dp[now.u][now.v]==1&&dp[now.v][now.u]==1)return 0;\n }\n }\n return 1;\n}\nint main()\n{\n while(scanf(\"%d %d\",&n,&m))\n {\n if(n==0&&m==0)break;\n for(int i=0;i<m;i++)\n {\n scanf(\"%d%d%d\",&q[i].o,&q[i].u,&q[i].v);\n }\n int l=0,r=m;\n while(l<r)\n {\n int mid=(l+r)/2+1;\n if(check(mid))l=mid;\n else r=mid-1;\n }\n printf(\"%d\\n\",l);\n }\n return 0;\n}", "accuracy": 1, "time_ms": 290, "memory_kb": 1556, "score_of_the_acc": -0.0668, "final_rank": 6 }, { "submission_id": "aoj_1332_1235793", "code_snippet": "#include<cstdio>\n#include<vector>\n#include<queue>\n#include<algorithm>\n\nusing namespace std;\n\nint M;\nint type[10100],is[10100],js[10100];\n\nvector<int> G[200];\nbool reached[200];\nint N;\n\nvoid init_graph(){\n\tfor(int i=0;i<N*2;i++) G[i].clear();\n}\n\nvoid add_edge(int t,int i,int j){\n\tif(t==1){\n\t\tG[i].push_back(j);\n\t\tG[j+N].push_back(i+N);\n\t}else if(t==2){\n\t\tadd_edge(1,i,j);\n\t\tadd_edge(1,j,i);\n\t}else if(t==4){\n\t\tG[i].push_back(j+N);\n\t\tG[j].push_back(i+N);\n\t}\n}\n\nbool bfs(int s1,int s2){\n\tfor(int i=0;i<N*2;i++) reached[i]=false;\n\treached[s1]=true;\n\treached[s2]=true;\n\tqueue<int> que;\n\tque.push(s1);\n\tque.push(s2);\n\twhile(!que.empty()){\n\t\tint v=que.front();\n\t\tque.pop();\n\t\tfor(int i=0;i<G[v].size();i++){\n\t\t\tint u=G[v][i];\n\t\t\tif(reached[u]) continue;\n\t\t\treached[u]=true;\n\t\t\tif(u>=N){\n\t\t\t\tif(reached[u-N]) return false;\n\t\t\t}else{\n\t\t\t\tif(reached[u+N]) return false;\n\t\t\t}\n\t\t\tque.push(u);\n\t\t}\n\t}\n\treturn true;\n}\n\nbool check(int K){\n\tinit_graph();\n\tfor(int i=0;i<K;i++) add_edge(type[i],is[i],js[i]);\n\tbool ok=true;\n\tfor(int i=0;i<K;i++){\n\t\tif(type[i]==3){\n\t\t\tint u=is[i],v=js[i]+N;\n\t\t\tbool flg=bfs(u,v);\n\t\t\tu+=N;\n\t\t\tv-=N;\n\t\t\tbool flg2=bfs(u,v);\n\t\t\tif(flg==false&&flg2==false){\n\t\t\t\treturn false;\n\t\t\t}\n\t\t}else if(type[i]==5){\n\t\t\tint u=is[i],v=js[i];\n\t\t\tbool flg=bfs(u,v);\n\t\t\tif(!flg) return false;\n\t\t}\n\t}\n\treturn true;\n}\n\nint main(){\n\twhile(true){\n\t\tscanf(\"%d%d\",&N,&M);\n\t\tif(N==0&&M==0) break;\n\t\tfor(int i=0;i<M;i++){\n\t\t\tscanf(\"%d%d%d\",type+i,is+i,js+i);\n\t\t\tis[i]--;js[i]--;\n\t\t}\n\t\tint lb=1,ub=M+1;\n\t\twhile(ub-lb>1){\n\t\t\tint mid=(ub+lb)/2;\n\t\t\tbool flg=check(mid);\n\t\t\tif(flg) lb=mid;\n\t\t\telse ub=mid;\n\t\t}\n\t\tprintf(\"%d\\n\",lb);\n\t}\n\treturn 0;\n}", "accuracy": 1, "time_ms": 270, "memory_kb": 1532, "score_of_the_acc": -0.0617, "final_rank": 4 }, { "submission_id": "aoj_1332_1222767", "code_snippet": "#include <cstdio>\n#include <cstring>\n#include <algorithm>\n#include <vector>\n#include <cctype>\nusing namespace std;\n#define N 105\nint n , m;\nstruct query\n{\n int id , x , y;\n}q[N * N];\nbool g[N][N] , f[N][N];\nbool Q(int x , int y)\n{\n for (int i = 1 ; i <= n ; ++ i)\n if (g[x][i] && f[y][i] || g[y][i] && f[x][i])\n return 1;\n return 0;\n}\nbool check(int V)\n{\n int i , j , k, x , y;\n memset(f , 0 , sizeof(f));\n memset(g , 0 , sizeof(g));\n for (i = 1 ; i <= n ; ++ i)\n g[i][i] = 1;\n for (i = 1 ; i <= V ; ++ i)\n if (q[i].id <= 2)\n {\n x = q[i].x , y = q[i].y;\n g[x][y] = 1;\n if (q[i].id == 2)\n g[y][x] = 1;\n }\n for (k = 1 ; k <= n ; ++ k)\n for (i = 1 ; i <= n ; ++ i) if (g[i][k])\n for (j = 1 ; j <= n ; ++ j) if (g[k][j])\n g[i][j] = 1;\n for (i = 1 ; i <= V ; ++ i)\n if (q[i].id == 4)\n {\n x = q[i].x , y = q[i].y;\n /* for (j = 1 ; j <= n ; ++ j) if (g[j][x])\n for (k = 1 ; k <= n ; ++ k) if (g[k][y])\n f[j][k] = f[k][j] = 1;*/\n for (j = 1 ; j <= n ; ++ j) if (g[j][x])\n f[j][y] = 1;\n for (j = 1 ; j <= n ; ++ j) if (g[j][y])\n f[j][x] = 1;\n }\n for (i = 1; i <= n; i++) for (j = 1; j <= n; j++) f[i][j] |= f[j][i];\n for (i = 1 ; i <= V ; ++ i)\n {\n x = q[i].x , y = q[i].y;\n if (q[i].id == 3) // x != y\n {// x , y = empty x == y\n if (g[x][y] && g[y][x])\n return 0;\n if (Q(x , x) && Q(y , y))\n return 0;\n }\n else if (q[i].id == 5) // x ins y != empty\n { //x = empty y = empty x ins y == empty\n if (Q(x , x) || Q(y , y) || Q(x , y) || Q(y , x))\n return 0;\n }\n }\n return 1;\n}\n\nint main()\n{\n while (scanf(\"%d%d\",&n,&m))\n {\n \tif(n == 0 && m == 0) break;\n for (int i = 1 ; i <= m ; ++ i)\n scanf(\"%d%d%d\",&q[i].id, &q[i].x , &q[i].y);\n\n int top = 1 , bot = m , mid;\n while (top < bot)\n {\n mid = top + bot + 1 >> 1;\n if (check(mid))\n top = mid;\n else\n bot = mid - 1;\n }\n printf(\"%d\\n\" , top);\n }\n return 0;\n}", "accuracy": 1, "time_ms": 230, "memory_kb": 1156, "score_of_the_acc": -0.0405, "final_rank": 3 }, { "submission_id": "aoj_1332_1046508", "code_snippet": "#include <cstring>\n#include <cstdio>\n#include <algorithm>\n#include <vector>\n#include <string>\n#include <iostream>\n\nusing namespace std;\n\nconst int MAXN = 1e4+10;\nconst int MAXG = 110;\n\nint n,m;\nint x[MAXN],op[MAXN],y[MAXN];\nint f[MAXG][MAXG],g[MAXG][MAXG];\nbool vis[MAXG] ;\n\nvoid dfs(int u){\n int i, k;\n vis[u] = 1;\n for(i=1;i<=n;i++)\n if(g[u][i]){\n if(!vis[i]) dfs(i);\n for(k=1;k<=n;k++)\n if(f[i][k])\n f[u][k] = f[k][u] = 1;\n }\n}\n\nbool check(int mid) {\n for (int i=1; i<=n; i++)\n for (int j=1; j<=n; j++)\n f[i][j] = 0 ;\n for (int i=1; i<=n; i++) {\n for (int j=1; j<=n; j++) g[i][j]=0;\n g[i][i]=1;\n }\n for (int Q=1; Q<=mid; Q++) {\n int i = x[Q] , j = y[Q] , ope = op[Q];\n if (ope==1) g[i][j] = 1 ;\n else if (ope==2) g[i][j] = g[j][i] = 1 ;\n else if (ope==4) f[i][j] = f[j][i] = 1 ;\n }\n for (int k = 1 ; k <= n ; k++)\n for (int i = 1 ; i <= n ; i++)\n for (int j = 1 ; j <= n ; j++)\n if (g[i][k] && g[k][j]) g[i][j] = 1 ;\n\n memset(vis,0,sizeof(vis));\n for(int i=1;i<=n;i++)\n if(!vis[i]) dfs(i);\n\n for (int Q = 1 ; Q <= mid ; Q++) {\n int i = x[Q] , j = y[Q] , ope = op[Q];\n if (ope==3) {\n if (f[i][i] && f[j][j]) return false ;\n if (g[i][j] && g[j][i]) return false ;\n } else if (ope==5) {\n if (f[i][j] || f[j][i] || f[i][i] || f[j][j]) return false ;\n }\n }\n return true ;\n}\n\nint main() {\n while (~scanf(\"%d%d\",&n,&m) && (n+m)) {\n for (int i=1; i<=m; i++) scanf(\"%d%d%d\",&op[i],&x[i],&y[i]);\n int l=1,r=m,ans=0;\n while (l<=r) {\n int mid=l+r>>1;\n if (check(mid)) {\n ans=mid;\n l=mid+1;\n } else r=mid-1;\n }\n printf(\"%d\\n\",ans);\n }\n return 0;\n}", "accuracy": 1, "time_ms": 310, "memory_kb": 1400, "score_of_the_acc": -0.0658, "final_rank": 5 }, { "submission_id": "aoj_1332_626123", "code_snippet": "#include <iostream>\n#include <algorithm>\n#include <vector>\n#include <bitset>\n\n#define frs first\n#define scn second\n#define mkp make_pair\n#define phb push_back\n#define ppb pop_back\n\nusing namespace std;\n\ntypedef pair< int, int > Pr;\ntypedef pair< int, Pr > Cmd;\n\nint N, M;\nCmd C[10010];\n\nvector< int > graph[110], stk;\nint dfi, dfn[110], lwn[110], cmp[110];\nint vst[110], vst_cnt;\nbool is_in_stk[110];\n\nbitset< 110 > dscn[110], ancs[110];\nbool is_zero[110], is_nonzero[110];\nbitset< 110 > is_intr[110];\n\nvoid read();\nvoid soups_on();\nbool yee_haw(int);\nvoid tarjan(int);\nvoid dfs(int, int);\n\nint main() {\n ios::sync_with_stdio(false);\n while (cin >> N >> M, N + M)\n read(), soups_on();\n\n return 0;\n}\n\nvoid read() {\n int s, u, v;\n\n for (int i = 0; i < M; ++i)\n cin >> s >> u >> v, C[i] = mkp(s, mkp(u, v));\n}\n\nvoid soups_on() {\n int low, up, mid;\n\n low = 0, up = M;\n while (low < up) {\n mid = (low + up) / 2 + 1;\n if (yee_haw(mid))\n low = mid;\n else\n up = mid - 1;\n }\n\n cout << low << \"\\n\";\n}\n\nbool yee_haw(int lm) {\n int u, v;\n vector< Pr > t[6];\n\n for (int i = 0; i < lm; ++i)\n t[C[i].frs].phb(C[i].scn);\n\n for (int i = 1; i <= N; ++i)\n graph[i].clear();\n\n // build graph\n for (int i = 0; i < int(t[1].size()); ++i) {\n u = t[1][i].frs, v = t[1][i].scn;\n graph[v].phb(u);\n }\n\n for (int i = 0; i < int(t[2].size()); ++i) {\n u = t[2][i].frs, v = t[2][i].scn;\n graph[u].phb(v), graph[v].phb(u);\n }\n\n // find scc\n ++vst_cnt, dfi = 0;\n for (int i = 1; i <= N; ++i)\n if (vst[i] != vst_cnt)\n tarjan(i);\n\n for (int i = 1; i <= N; ++i)\n dscn[i].reset(), ancs[i].reset(), dscn[i].set(i), ancs[i].set(i);\n\n // find descendants and ancestors\n for (int i = 1; i <= N; ++i)\n if (cmp[i] == i)\n ++vst_cnt, dfs(i, i);\n\n fill(&is_zero[0], &is_zero[N + 1], false);\n fill(&is_nonzero[0], &is_nonzero[N + 1], false);\n\n for (int i = 1; i <= N; ++i)\n is_intr[i].reset();\n\n // dealing with constraint type 5\n\n for (int i = 0; i < int(t[5].size()); ++i) {\n u = cmp[t[5][i].frs], v = cmp[t[5][i].scn];\n bitset< 110 > tmp = ancs[u] | ancs[v];\n for (int j = 1; j <= N; ++j)\n if (ancs[u][j] || ancs[v][j])\n is_nonzero[j] = true, is_intr[j] |= tmp;\n }\n\n // dealing with constraint type 4\n for (int i = 0; i < int(t[4].size()); ++i) {\n u = cmp[t[4][i].frs], v = cmp[t[4][i].scn];\n if (u == v)\n is_zero[u] = true;\n else {\n if (is_intr[u][v])\n return false;\n bitset< 110 > tmp = dscn[u] & dscn[v];\n for (int j = 1; j <= N; ++j)\n if (tmp[j])\n is_zero[j] = true;\n }\n }\n\n // dealing with constraint type 3\n for (int i = 0; i < int(t[3].size()); ++i) {\n u = cmp[t[3][i].frs], v = cmp[t[3][i].scn];\n if (u == v) return false;\n if (is_zero[u] && is_zero[v]) return false;\n }\n\n // dealing with other confliction between constraint type 4 and 5\n for (int i = 1; i <= N; ++i)\n if (cmp[i] == i && (is_zero[i] && is_nonzero[i]))\n return false;\n\n return true;\n}\n\nvoid tarjan(int u) {\n int v;\n\n vst[u] = vst_cnt;\n dfn[u] = lwn[u] = ++dfi;\n stk.phb(u), is_in_stk[u] = true;\n\n for (int i = 0; i < int(graph[u].size()); ++i)\n if (vst[v = graph[u][i]] != vst_cnt)\n tarjan(v), lwn[u] = min(lwn[u], lwn[v]);\n else if (is_in_stk[v])\n lwn[u] = min(lwn[u], dfn[v]);\n\n if (dfn[u] == lwn[u]) {\n do {\n v = stk.back(), stk.ppb(), is_in_stk[v] = false;\n cmp[v] = u;\n } while (u != v);\n }\n}\n\nvoid dfs(int u, int k) {\n int v;\n\n vst[u] = vst_cnt;\n for (int i = 0; i < int(graph[u].size()); ++i)\n if (vst[v = graph[u][i]] != vst_cnt) {\n if (cmp[v] == v)\n dscn[k].set(v), ancs[v].set(k);\n dfs(v, k);\n }\n}", "accuracy": 1, "time_ms": 70, "memory_kb": 1660, "score_of_the_acc": -0.0234, "final_rank": 1 }, { "submission_id": "aoj_1332_626122", "code_snippet": "#include <iostream>\n#include <algorithm>\n#include <vector>\n#include <bitset>\n\n#define frs first\n#define scn second\n#define mkp make_pair\n#define phb push_back\n#define ppb pop_back\n\nusing namespace std;\n\ntypedef pair< int, int > Pr;\ntypedef pair< int, Pr > Cmd;\n\nint N, M;\nCmd C[10010];\n\nvector< int > graph[110], stk;\nint dfi, dfn[110], lwn[110], cmp[110];\nint vst[110], vst_cnt;\nbool is_in_stk[110];\n\nbitset< 110 > dscn[110], ancs[110];\nbool is_zero[110], is_nonzero[110];\nbitset< 110 > is_intr[110];\n\nvoid read();\nvoid soups_on();\nbool yee_haw(int);\nvoid tarjan(int);\nvoid dfs(int, int);\n\nint main() {\n while (cin >> N >> M, N + M)\n read(), soups_on();\n\n return 0;\n}\n\nvoid read() {\n int s, u, v;\n\n for (int i = 0; i < M; ++i)\n cin >> s >> u >> v, C[i] = mkp(s, mkp(u, v));\n}\n\nvoid soups_on() {\n int low, up, mid;\n\n low = 0, up = M;\n while (low < up) {\n mid = (low + up) / 2 + 1;\n if (yee_haw(mid))\n low = mid;\n else\n up = mid - 1;\n }\n\n cout << low << \"\\n\";\n}\n\nbool yee_haw(int lm) {\n int u, v;\n vector< Pr > t[6];\n\n for (int i = 0; i < lm; ++i)\n t[C[i].frs].phb(C[i].scn);\n\n for (int i = 1; i <= N; ++i)\n graph[i].clear();\n\n // build graph\n for (int i = 0; i < int(t[1].size()); ++i) {\n u = t[1][i].frs, v = t[1][i].scn;\n graph[v].phb(u);\n }\n\n for (int i = 0; i < int(t[2].size()); ++i) {\n u = t[2][i].frs, v = t[2][i].scn;\n graph[u].phb(v), graph[v].phb(u);\n }\n\n // find scc\n ++vst_cnt, dfi = 0;\n for (int i = 1; i <= N; ++i)\n if (vst[i] != vst_cnt)\n tarjan(i);\n\n for (int i = 1; i <= N; ++i)\n dscn[i].reset(), ancs[i].reset(), dscn[i].set(i), ancs[i].set(i);\n\n // find descendants and ancestors\n for (int i = 1; i <= N; ++i)\n if (cmp[i] == i)\n ++vst_cnt, dfs(i, i);\n\n fill(&is_zero[0], &is_zero[N + 1], false);\n fill(&is_nonzero[0], &is_nonzero[N + 1], false);\n\n for (int i = 1; i <= N; ++i)\n is_intr[i].reset();\n\n // dealing with constraint type 5\n\n for (int i = 0; i < int(t[5].size()); ++i) {\n u = cmp[t[5][i].frs], v = cmp[t[5][i].scn];\n bitset< 110 > tmp = ancs[u] | ancs[v];\n for (int j = 1; j <= N; ++j)\n if (ancs[u][j] || ancs[v][j])\n is_nonzero[j] = true, is_intr[j] |= tmp;\n }\n\n // dealing with constraint type 4\n for (int i = 0; i < int(t[4].size()); ++i) {\n u = cmp[t[4][i].frs], v = cmp[t[4][i].scn];\n if (u == v)\n is_zero[u] = true;\n else {\n if (is_intr[u][v])\n return false;\n bitset< 110 > tmp = dscn[u] & dscn[v];\n for (int j = 1; j <= N; ++j)\n if (tmp[j])\n is_zero[j] = true;\n }\n }\n\n // dealing with constraint type 3\n for (int i = 0; i < int(t[3].size()); ++i) {\n u = cmp[t[3][i].frs], v = cmp[t[3][i].scn];\n if (u == v) return false;\n if (is_zero[u] && is_zero[v]) return false;\n }\n\n // dealing with other confliction between constraint type 4 and 5\n for (int i = 1; i <= N; ++i)\n if (cmp[i] == i && (is_zero[i] && is_nonzero[i]))\n return false;\n\n return true;\n}\n\nvoid tarjan(int u) {\n int v;\n\n vst[u] = vst_cnt;\n dfn[u] = lwn[u] = ++dfi;\n stk.phb(u), is_in_stk[u] = true;\n\n for (int i = 0; i < int(graph[u].size()); ++i)\n if (vst[v = graph[u][i]] != vst_cnt)\n tarjan(v), lwn[u] = min(lwn[u], lwn[v]);\n else if (is_in_stk[v])\n lwn[u] = min(lwn[u], dfn[v]);\n\n if (dfn[u] == lwn[u]) {\n do {\n v = stk.back(), stk.ppb(), is_in_stk[v] = false;\n cmp[v] = u;\n } while (u != v);\n }\n}\n\nvoid dfs(int u, int k) {\n int v;\n\n vst[u] = vst_cnt;\n for (int i = 0; i < int(graph[u].size()); ++i)\n if (vst[v = graph[u][i]] != vst_cnt) {\n if (cmp[v] == v)\n dscn[k].set(v), ancs[v].set(k);\n dfs(v, k);\n }\n}", "accuracy": 1, "time_ms": 110, "memory_kb": 1648, "score_of_the_acc": -0.0315, "final_rank": 2 } ]
aoj_1339_cpp
Problem E: Dragon's Cruller Dragon's Cruller is a sliding puzzle on a torus. The torus surface is partitioned into nine squares as shown in its development in Figure E.1. Here, two squares with one of the sides on the development labeled with the same letter are adjacent to each other, actually sharing the side. Figure E.2 shows which squares are adjacent to which and in which way. Pieces numbered from 1 through 8 are placed on eight out of the nine squares, leaving the remaining one empty. Figure E.1. A 3 × 3 Dragon’s Cruller torus A piece can be slid to an empty square from one of its adjacent squares. The goal of the puzzle is, by sliding pieces a number of times, to reposition the pieces from the given starting arrangement into the given goal arrangement. Figure E.3 illustrates arrangements directly reached from the arrangement shown in the center after four possible slides. The cost to slide a piece depends on the direction but is independent of the position nor the piece. Your mission is to find the minimum cost required to reposition the pieces from the given starting arrangement into the given goal arrangement. Figure E.2. Adjacency Figure E.3. Examples of sliding steps Unlike some sliding puzzles on a flat square, it is known that any goal arrangement can be reached from any starting arrangements on this torus. Input The input is a sequence of at most 30 datasets. A dataset consists of seven lines. The first line contains two positive integers c h and c v , which represent the respective costs to move a piece horizontally and vertically. You can assume that both c h and c v are less than 100. The next three lines specify the starting arrangement and the last three the goal arrangement, each in the following format. d A d B d C d D d E d F d G d H d I Each line consists of three digits separated by a space. The digit d X ( X is one of A through I ) indicates the state of the square X as shown in Figure E.2. Digits 1 , . . . , 8 indicate that the piece of that number is on the square. The digit 0 indicates that the square is empty. The end of the input is indicated by two zeros separated by a space. Output For each dataset, output the minimum total cost to achieve the goal, in a line. The total cost is the sum of the costs of moves from the starting arrangement to the goal arrangement. No other characters should appear in the output. Sample Input 4 9 6 3 0 8 1 2 4 5 7 6 3 0 8 1 2 4 5 7 31 31 4 3 6 0 1 5 8 2 7 0 3 6 4 1 5 8 2 7 92 4 1 5 3 4 0 7 8 2 6 1 5 0 4 7 3 8 2 6 12 28 3 4 5 0 2 6 7 1 8 5 7 1 8 6 2 0 3 4 0 0 Output for the Sample Input 0 31 96 312
[ { "submission_id": "aoj_1339_10857961", "code_snippet": "#include<map>\n#include<set>\n#include<cmath>\n#include<queue>\n#include<cctype>\n#include<cstdio>\n#include<vector>\n#include<cstdlib>\n#include<cstring>\n#include<algorithm>\ntypedef long long LL;\n#define HASH 1000003\n#define MAXN 1000010\nint first[10], e, next[40], v[40], w[40];\nstruct HN {\n\tLL s;\n\tint d;\n\tHN() {}\n\tHN(LL s, int d): s(s), d(d) {}\n\tbool operator < (const HN &t) const {\n\t\treturn d > t.d;\n\t}\n};\nstruct HM {\n\tint first[HASH], e, next[MAXN], d[MAXN];\n\tLL s[MAXN];\n\tvoid init() {\n\t\tmemset(first, -1, sizeof(first)), e = 0;\n\t}\n\tint hash(LL v) {\n\t\treturn v % HASH;\n\t}\n\tint find(LL v) {\n\t\tint h = hash(v);\n\t\tfor(int i = first[h]; i != -1; i = next[i]) {\n\t\t\tif(s[i] == v) {\n\t\t\t\treturn i;\n\t\t\t}\n\t\t}\n\t\treturn -1;\n\t}\n\tvoid add(LL v, int dis) {\n\t\tint h = hash(v);\n\t\ts[e] = v, d[e] = dis;\n\t\tnext[e] = first[h], first[h] = e ++;\n\t}\n}hm;\nvoid add(int x, int y, int z) {\n\tv[e] = y, w[e] = z;\n\tnext[e] = first[x], first[x] = e ++;\n}\nvoid init() {\n\tmemset(first, -1, sizeof(first)), e = 0;\n\tfor(int i = 0; i < 9; i ++) {\n\t\tadd(i, (i + 1) % 9, 0), add(i, (i - 1 + 9) % 9, 0);\n\t\tadd(i, (i + 3) % 9, 1), add(i, (i - 3 + 9) % 9, 1);\n\t}\n}\nLL encode(int a[]) {\n\tLL ans = 0;\n\tfor(int i = 0; i < 9; i ++) {\n\t\tans = ans << 4 | a[i];\n\t}\n\treturn ans;\n}\nvoid decode(int a[], LL s) {\n\tfor(int i = 8; i >= 0; i --) {\n\t\ta[i] = s & 15;\n\t\ts >>= 4;\n\t}\n}\nint CH, CV, bm[10], em[10], a[10];\nLL BM, EM;\nvoid process() {\n\thm.init();\n\tstd::priority_queue<HN> q;\n\thm.add(BM, 0);\n\tq.push(HN(BM, 0));\n\twhile(!q.empty()) {\n\t\tHN hn = q.top();\n\t\tq.pop();\n\t\tif(hn.s == EM) {\n\t\t\tprintf(\"%d\\n\", hn.d);\n\t\t\tbreak;\n\t\t}\n\t\tint k = hm.find(hn.s);\n\t\tif(hn.d > hm.d[k]) {\n\t\t\tcontinue;\n\t\t}\n\t\tdecode(a, hn.s);\n\t\tfor(int j = 0; j < 9; j ++) {\n\t\t\tif(a[j] == 0) {\n\t\t\t\tfor(int i = first[j]; i != -1; i = next[i]) {\n\t\t\t\t\tint jj = v[i], dd = hn.d + (w[i] == 0 ? CH : CV);\n\t\t\t\t\tstd::swap(a[j], a[jj]);\n\t\t\t\t\tLL v = encode(a);\n\t\t\t\t\tk = hm.find(v);\n\t\t\t\t\tif(k == -1) {\n\t\t\t\t\t\thm.add(v, dd);\n\t\t\t\t\t\tq.push(HN(v, dd));\n\t\t\t\t\t} else if(dd < hm.d[k]) {\n\t\t\t\t\t\thm.d[k] = dd;\n\t\t\t\t\t\tq.push(HN(v, dd));\n\t\t\t\t\t}\n\t\t\t\t\tstd::swap(a[j], a[jj]);\n\t\t\t\t}\n\t\t\t\tbreak;\n\t\t\t}\n\t\t}\n\t}\n}\nint main() {\n\t//freopen(\"test.in\", \"rb\", stdin);\n\tinit();\n\twhile(scanf(\"%d%d\", &CH, &CV), CH) {\n\t\tfor(int i = 0; i < 9; i ++) {\n\t\t\tscanf(\"%d\", &bm[i]);\n\t\t}\n\t\tfor(int i = 0; i < 9; i ++) {\n\t\t\tscanf(\"%d\", &em[i]);\n\t\t}\n\t\tBM = encode(bm), EM = encode(em);\n\t\tprocess();\n\t}\n\treturn 0;\n}", "accuracy": 1, "time_ms": 400, "memory_kb": 24024, "score_of_the_acc": -0.1905, "final_rank": 2 }, { "submission_id": "aoj_1339_9277663", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n#define ll long long\n#define rep(i, a, b) for(ll i = a; i < b; i++)\n#define all(x) (x).begin(), (x).end()\n\ntemplate <typename T>\nbool chmin(T& a, T b) {\n if (a <= b) return 0;\n a = b;\n return 1;\n}\n\nusing Grid = array<int,9>;\n\nconst vector<vector<int>> edges_h = {\n {1, 8},\n {2, 0},\n {3, 1},\n {4, 2},\n {5, 3},\n {6, 4},\n {7, 5},\n {8, 6},\n {0, 7}\n};\n\nconst vector<vector<int>> edges_v = {\n {6, 3},\n {7, 4},\n {8, 5},\n {0, 6},\n {1, 7},\n {2, 8},\n {3, 0},\n {4, 1},\n {5, 2}\n};\n\nint main() {\n while (true) {\n int ch, cv;\n cin >> ch >> cv;\n\n if (ch == 0 && cv == 0) {\n return 0;\n }\n Grid initial_grid;\n rep(i, 0, 9) {\n cin >> initial_grid[i];\n }\n Grid goal_grid;\n rep(i, 0, 9) {\n cin >> goal_grid[i];\n }\n map<Grid,int> costs;\n costs[initial_grid] = 0;\n priority_queue<pair<int,Grid>> todo;\n todo.push({0, initial_grid});\n while (true) {\n auto [cost, s] = todo.top();\n todo.pop();\n cost *= -1;\n if (costs[s] < cost) {\n continue;\n }\n if (s == goal_grid) {\n cout << cost << \"\\n\";\n break;\n }\n int p = distance(s.begin(), find(s.begin(), s.end(), 0));\n for (int q : edges_h[p]) {\n swap(s[p], s[q]);\n int c = cost + ch;\n if (costs.count(s) == 0) {\n costs[s] = c;\n todo.push({-c, s});\n } else if (chmin(costs[s], c)) {\n todo.push({-c, s});\n }\n swap(s[p], s[q]);\n }\n for (int q : edges_v[p]) {\n swap(s[p], s[q]);\n int c = cost + cv;\n if (costs.count(s) == 0) {\n costs[s] = c;\n todo.push({-c, s});\n } else if (chmin(costs[s], c)) {\n todo.push({-c, s});\n }\n swap(s[p], s[q]);\n }\n }\n }\n}", "accuracy": 1, "time_ms": 5040, "memory_kb": 36628, "score_of_the_acc": -0.9738, "final_rank": 16 }, { "submission_id": "aoj_1339_8497890", "code_snippet": "#include \"bits/stdc++.h\"\n\nusing namespace std;\n\nusing ll=long long;\n\nbool solve();\n\nmap<vector<int>,int>mp;\nvector<vector<pair<ll,ll>>>adj;\n\nint main(){\n ios::sync_with_stdio(false);\n cin.tie(nullptr);\n int n=9;\n vector<int>ord(n);\n std::iota(ord.begin(), ord.end(),0);\n std::sort(ord.begin(), ord.end());\n int now=0;\n do{\n mp[ord]=now++;\n }while(next_permutation(ord.begin(),ord.end()));\n std::sort(ord.begin(), ord.end());\n adj.resize(now);\n do{\n int at=mp[ord];\n for (int i = 0; i < n; ++i) {\n if(ord[i]==0){\n for(auto dy:{-1,1}){\n auto i2=(i+dy+n)%n;\n swap(ord[i],ord[i2]);\n adj[at].emplace_back(mp[ord],0);\n swap(ord[i],ord[i2]);\n }\n for(auto dx:{-3,3}){\n auto i2=(i+dx+n)%n;\n swap(ord[i],ord[i2]);\n adj[at].emplace_back(mp[ord],1);\n swap(ord[i],ord[i2]);\n }\n }\n }\n }while(next_permutation(ord.begin(),ord.end()));\n while(1){\n int cw,ch;cin>>cw>>ch;\n if(cw==0 and ch==0){\n return 0;\n }\n int n=9;\n vector<int>d(n);\n for (int i = 0; i < n; ++i) {\n cin>>d[i];\n }\n vector<int>tar(n);\n for (int i = 0; i < n; ++i) {\n cin>>tar[i];\n }\n auto target=mp[tar];\n const ll INF=1e15;\n vector<ll>dist(now,INF);\n using P=pair<ll,ll>;\n priority_queue<P,vector<P>,greater<P>>q;\n q.emplace(0,mp[d]);\n dist[mp[d]]=0;\n while(q.size()){\n auto [dd,v]=q.top();q.pop();\n if(dd!=dist[v])continue;\n if(v==target){\n cout<<dd<<\"\\n\";\n break;\n }\n for(auto [to,ty]:adj[v]){\n ll cost=(ty==0)?cw:ch;\n if(dist[to]>dd+cost){\n dist[to]=dd+cost;\n q.emplace(dist[to],to);\n }\n }\n }\n }\n return 0;\n}\n\nbool solve(){\n int cw,ch;cin>>cw>>ch;\n if(cw==0 and ch==0){\n return false;\n }\n int n=9;\n vector<int>d(n);\n for (int i = 0; i < n; ++i) {\n cin>>d[i];\n }\n vector<int>tar(n);\n for (int i = 0; i < n; ++i) {\n cin>>tar[i];\n }\n auto target=mp[tar];\n vector<ll>dist(adj.size());\n using P=pair<ll,ll>;\n priority_queue<P,vector<P>,greater<P>>q;\n q.emplace(0,mp[d]);\n while(q.size()){\n auto [dd,v]=q.top();q.pop();\n if(dd!=dist[v])continue;\n if(v==target){\n// cerr<<dd<<\"\\n\";\n cout<<dd<<\"\\n\";\n return true;\n }\n for(auto [to,ty]:adj[v]){\n ll cost=(ty==0)?cw:ch;\n if(dist[to]>dd+cost){\n dist[to]=dd+cost;\n q.emplace(dist[to],to);\n }\n }\n }\n return true;\n}", "accuracy": 1, "time_ms": 1010, "memory_kb": 94752, "score_of_the_acc": -1.084, "final_rank": 19 }, { "submission_id": "aoj_1339_8497889", "code_snippet": "#include \"bits/stdc++.h\"\n\nusing namespace std;\n\nusing ll=long long;\n\nbool solve(){\n int cw,ch;cin>>cw>>ch;\n if(cw==0 and ch==0){\n return false;\n }\n int n=9;\n vector<int>d(n);\n for (int i = 0; i < n; ++i) {\n cin>>d[i];\n }\n vector<int>tar(n);\n for (int i = 0; i < n; ++i) {\n cin>>tar[i];\n }\n map<vector<int>,int>mp;\n using P=pair<ll,vector<int>>;\n priority_queue<P,vector<P>,greater<P>>q;\n q.emplace(0,d);\n auto add=[&](vector<int>&v,ll dd){\n if(!mp.count(v)){\n mp[v]=dd;\n q.emplace(dd,v);\n }\n else{\n if(mp[v]>dd){\n mp[v]=dd;\n q.emplace(dd,v);\n }\n }\n };\n while(q.size()){\n auto [dd,v]=q.top();q.pop();\n if(mp.count(v) and dd!=mp[v])continue;\n if(v==tar){\n// cerr<<dd<<\"\\n\";\n cout<<dd<<\"\\n\";\n return true;\n }\n for (int i = 0; i < n; ++i) {\n if(v[i]==0){\n for(auto dy:{-1,1}){\n auto i2=(i+dy+n)%n;\n swap(v[i],v[i2]);\n add(v,dd+cw);\n swap(v[i],v[i2]);\n }\n for(auto dx:{-3,3}){\n auto i2=(i+dx+n)%n;\n swap(v[i],v[i2]);\n add(v,dd+ch);\n swap(v[i],v[i2]);\n }\n }\n }\n }\n return true;\n}\n\nint main(){\n ios::sync_with_stdio(false);\n cin.tie(nullptr);\n while(solve());\n return 0;\n}", "accuracy": 1, "time_ms": 7660, "memory_kb": 58176, "score_of_the_acc": -1.5814, "final_rank": 20 }, { "submission_id": "aoj_1339_3991914", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\ntypedef long long ll;\ntypedef pair<ll, ll> l_l;\nint v[9][2];\nint h[9][2];\nll ch, cv;\ntemplate <typename T>\nbool chmin(T &a, T b) {\n if(a > b) {\n a = b;\n return true;\n }\n return false;\n}\nmap<ll, ll> mp;\nvoid print(vector<int> v) {\n for(int i = 0; i < 9; i++) {\n cerr << v[i] << \" \";\n if(i % 3 == 2) cerr << endl;\n }\n}\n\nll f(vector<int> v) {\n ll ret = 0;\n for(int i = v.size() - 1; i >= 0; i--) {\n ret *= 10;\n ret += v[i];\n }\n return ret;\n}\n\nvector<int> g(ll a) {\n vector<int> ret;\n for(int i = 0; i < 9; i++) {\n ret.push_back(a % 10);\n a /= 10;\n }\n return ret;\n}\n\ntypedef pair<ll, vector<int>> lll;\nint main() {\n cin.tie(nullptr);\n ios::sync_with_stdio(false);\n for(int i = 0; i <= 8; i++) {\n h[i][0] = (i + 1) % 9;\n h[i][1] = (i + 8) % 9;\n v[i][0] = (i + 3) % 9;\n v[i][1] = (i + 6) % 9;\n }\n while(true) {\n cin >> ch >> cv;\n if(ch == 0) break;\n vector<int> start(9);\n vector<int> goal(9);\n for(int i = 0; i < 9; i++) cin >> start[i];\n for(int i = 0; i < 9; i++) cin >> goal[i];\n mp.clear();\n priority_queue<l_l, vector<l_l>, greater<l_l>> que;\n que.push({0, f(start)});\n while(true) {\n ll nowcost = que.top().first;\n ll nowval = que.top().second;\n vector<int> now = g(que.top().second);\n que.pop();\n if(now == goal) {\n cout << nowcost << endl;\n break;\n }\n int zeroindex;\n for(int i = 0; i < 9; i++) {\n if(now[i] == 0) zeroindex = i;\n }\n for(int i = 0; i < 2; i++) {\n ll newcost = nowcost + ch;\n vector<int> to = now;\n swap(to[zeroindex], to[h[zeroindex][i]]);\n auto a = f(to);\n if(mp.count(a) == 0 || mp[a] > newcost) {\n mp[a] = newcost;\n que.push({newcost, a});\n }\n /*\n cerr << \"---------------\" << endl;\n print(now);\n cerr << \"to(horizon)\" << endl;\n print(to);\n */\n }\n for(int i = 0; i < 2; i++) {\n ll newcost = nowcost + cv;\n vector<int> to = now;\n swap(to[zeroindex], to[v[zeroindex][i]]);\n auto a = f(to);\n if(mp.count(a) == 0 || chmin(mp[a], newcost)) {\n mp[a] = newcost;\n que.push({newcost, a});\n }\n /*\n cerr << \"---------------\" << endl;\n print(now);\n cerr << \"to(vertical)\" << endl;\n print(to);\n */\n }\n }\n }\n return 0;\n}", "accuracy": 1, "time_ms": 4730, "memory_kb": 27452, "score_of_the_acc": -0.8261, "final_rank": 15 }, { "submission_id": "aoj_1339_3904260", "code_snippet": "#include <cstdio>\n#include <iostream>\n#include <algorithm>\n#include <vector>\n#include <map>\n#include <queue>\n#include <cstring>\n#include <set>\n#include <cassert>\n\nusing namespace std;\n\ntypedef long long ll;\n\n#define SIZE 400\n#define LLINF 1000000000000000000LL\n\nint swapH[][2] = {{0, 1}, {0, 8}, {1, 2}, {2, 3}, {3, 4}, {4, 5}, {5, 6}, {6, 7}, {7, 8} };\n\nint swapV[][2] = {{0, 6}, {0, 3}, {1, 4}, {1, 7}, {2, 5}, {2, 8}, {3, 6}, {4, 7}, {5, 8} };\n\nint b[9], g[9];\nint gHash;\n\nvoid toArray(int *b, int hash) {\n for (int i=8; i>=0; i--) {\n b[i] = hash % 9;\n hash /= 9;\n }\n}\n\nint toHash(int *b) {\n int res = 0;\n for (int i=0; i<9; i++)\n res = res * 9 + b[i];\n\n return res;\n}\n\nint H, V;\n\nint base9[9];\n\nint swapp(int hash, int a, int b) {\n int p = hash / base9[a] % 9;\n int q = hash / base9[b] % 9;\n\n return hash + (p - q) * base9[b] + (q - p) * base9[a];\n}\n\nint isOK(int hash, int a, int b) {\n int r = hash / base9[a] % 9;\n int s = hash / base9[b] % 9;\n return r == 0 || s == 0;\n}\n\nll dfs(ll cost = 0) {\n int hash = toHash(b);\n\n priority_queue<pair<ll,int> > pq;\n set<int> memo;\n\n pq.push({0, hash});\n\n while(pq.size()) {\n auto p = pq.top();\n pq.pop();\n\n int hash = p.second;\n ll cost = -p.first;\n\n if (memo.find(hash) != memo.end()) continue;\n memo.insert(hash);\n\n if (hash == gHash) {\n return cost;\n }\n\n for (int i=0; i<9; i++) {\n if (isOK(hash, swapH[i][0], swapH[i][1]))\n pq.push({-cost-H, swapp(hash, swapH[i][0], swapH[i][1])});\n }\n\n for (int i=0; i<9; i++) {\n if (isOK(hash, swapV[i][0], swapV[i][1]))\n pq.push({-cost-V, swapp(hash, swapV[i][0], swapV[i][1])});\n }\n }\n\n return LLINF;\n}\n\nbool solve() {\n\n scanf(\"%d%d\", &H, &V);\n\n if (!H) return false;\n\n for (int i=0; i<9; i++)\n scanf(\"%d\", b+i);\n\n for (int i=0; i<9; i++)\n scanf(\"%d\", g+i);\n\n gHash = toHash(g);\n\n ll ans = dfs();\n\n assert(ans < LLINF);\n\n printf(\"%lld\\n\", ans);\n\n return true;\n}\n\nint main() {\n\n base9[8] = 1;\n\n for (int i=7;i >=0; i--)\n base9[i] = base9[i+1] * 9;\n\n while(solve());\n return 0;\n}", "accuracy": 1, "time_ms": 2850, "memory_kb": 25808, "score_of_the_acc": -0.5483, "final_rank": 14 }, { "submission_id": "aoj_1339_3048448", "code_snippet": "/*\n * Author: Gatevin\n * Created Time: 2015/10/31 13:46:57\n * File Name: Sakura_Chiyo.cpp\n */\n#include<iostream>\n#include<sstream>\n#include<fstream>\n#include<vector>\n#include<list>\n#include<deque>\n#include<queue>\n#include<stack>\n#include<map>\n#include<set>\n#include<bitset>\n#include<algorithm>\n#include<cstdio>\n#include<cstdlib>\n#include<cstring>\n#include<cctype>\n#include<cmath>\n#include<ctime>\n#include<iomanip>\nusing namespace std;\nconst double eps(1e-8);\ntypedef long long lint;\n \nint a[10];\nint ch, cv;\nint st, ed;\nint fac[10];\nint b[10], c[10];\n \n#define maxn (1*2*3*4*5*6*7*8*9+10)\n \nstruct Edge\n{\n int to, nex, w, typ;\n Edge(){}\n Edge(int _to, int _nex, int _w, int _typ)\n {\n to = _to, nex = _nex, w = _w, typ = _typ;\n }\n};\nEdge edge[maxn*4*2 + 2];\n \nint head[maxn];\nint E;\n \nvoid add_Edge(int u, int v, int w, int typ)\n{\n edge[++E] = Edge(v, head[u], w, typ);\n head[u] = E;\n}\n \nint change[9][4] =//转移\n{\n {1, 8, 6, 3},\n {2, 0, 7, 4},\n {3, 1, 8, 5},\n {4, 2, 0, 6},\n {5, 3, 1, 7},\n {6, 4, 2, 8},\n {7, 5, 3, 0},\n {8, 6, 4, 1},\n {0, 7, 5, 2}\n};\n \nvoid build()\n{\n ch = cv = 0;\n memset(head, -1, sizeof(head));\n E = 0;\n int start = 0;\n int end = 0;\n for(int i = 0; i <= 8; i++)\n end += i*fac[i];\n for(int i = start; i <= end; i++)//解Cantor展开\n {\n int ti = i;\n int emp;\n memset(a, 0, sizeof(a));\n for(int j = 8; j >= 0; j--)\n {\n int cnt = ti / fac[j];\n for(int p = 0; p <= 8; p++)\n {\n //if(a[i] == 0) cnt--;\n if(cnt == 0 && a[p] == 0)\n {\n b[j] = p;\n a[p] = 1;\n break;\n }\n else if(a[p] == 0) cnt--;\n }\n ti %= fac[j];\n if(b[j] == 0) emp = j;\n }\n for(int k = 0; k < 4; k++)//计算目标状态的Cantor展开的值\n {\n int swa = change[emp][k];\n for(int g = 0; g <= 8; g++)\n c[g] = b[g];\n swap(c[emp], c[swa]);\n int to = 0;\n for(int g = 0; g <= 8; g++)\n {\n int cnt = 0;\n for(int mabi = 0; mabi < g; mabi++)\n if(c[mabi] < c[g]) cnt++;\n to += cnt*fac[g];\n }\n add_Edge(to, i, (k > 1) ? cv : ch, (k > 1));\n }\n }\n}\n \nint Cantor()\n{\n int ret = 0;\n for(int i = 0; i < 9; i++)\n {\n int cnt = 0;\n for(int j = 0; j < i; j++)\n if(a[i] > a[j]) cnt++;\n ret += cnt*fac[i];\n }\n return ret;\n}\n \nbool vis[maxn];\nint dis[maxn];\nconst int inf = 1e9;\n \nvoid solve()//spfa\n{\n memset(vis, 0, sizeof(vis));\n fill(dis, dis + fac[9] + 1, inf);\n dis[st] = 0;\n queue<int> Q;\n Q.push(st);\n vis[st] = 1;\n while(!Q.empty())\n {\n int now = Q.front();\n Q.pop();\n vis[now] = 0;\n for(int i = head[now]; i + 1; i = edge[i].nex)\n {\n int nex = edge[i].to;\n if(dis[nex] > dis[now] + edge[i].w)\n {\n dis[nex] = dis[now] + edge[i].w;\n if(!vis[nex])\n Q.push(nex), vis[nex] = 1;\n }\n }\n }\n printf(\"%d\\n\", dis[ed]);\n}\n \nint main()\n{\n fac[0] = fac[1] = 1;\n for(int i = 2; i < 10; i++) fac[i] = fac[i - 1]*i;\n build();\n while(scanf(\"%d %d\", &ch, &cv), ch || cv)\n {\n for(int i = 0; i < 9; i++)\n scanf(\"%d\", &a[i]);\n st = Cantor();\n for(int i = 0; i < 9; i++)\n scanf(\"%d\", &a[i]);\n ed = Cantor();\n //build();\n for(int i = 1; i <= E; i++)\n if(edge[i].typ == 1)\n edge[i].w = cv;\n else edge[i].w = ch;\n solve();\n }\n return 0;\n}", "accuracy": 1, "time_ms": 1990, "memory_kb": 29248, "score_of_the_acc": -0.4693, "final_rank": 12 }, { "submission_id": "aoj_1339_2736732", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nint main(void){\n const int xx=2647079;\n cin.tie(0);\n ios::sync_with_stdio(false);\n int h,v,i;\n while(cin>>h>>v,h){\n string s=\"\",e=\"\";\n char tmp;\n for(i=0;i<9;i++){\n cin>>tmp;\n s+=tmp;\n }\n for(i=0;i<9;i++){\n cin>>tmp;\n e+=tmp;\n }\n priority_queue<pair<int,string>> q;\n q.push(make_pair(0,s));\n vector<bool> mp(xx,false);\n pair<int,string> now;\n int num,zero;\n while(!q.empty()){\n now=q.top();q.pop();\n now.first*=-1;\n num=stoi(now.second);\n if(now.second==e){\n cout<<now.first<<endl;\n break;\n }\n if(!mp[num%xx]){\n mp[num%xx]=true;\n }else{\n continue;\n }\n zero=now.second.find('0');\n string ns=now.second;\n ns[zero]=ns[(zero+1)%9];\n ns[(zero+1)%9]='0';\n if(!mp[stoi(ns)%xx])\n q.push(make_pair(-now.first-h,ns));\n ns[(zero+1)%9]=ns[zero];\n ns[zero]='0';\n \n ns[zero]=ns[(zero-1<0)?8:(zero-1)];\n ns[(zero-1<0)?8:(zero-1)]='0';\n if(!mp[stoi(ns)%xx])\n q.push(make_pair(-now.first-h,ns));\n ns[(zero-1<0)?8:(zero-1)]=ns[zero];\n ns[zero]='0';\n \n ns[zero]=ns[(zero+3)%9];\n ns[(zero+3)%9]='0';\n if(!mp[stoi(ns)%xx])\n q.push(make_pair(-now.first-v,ns));\n ns[(zero+3)%9]=ns[zero];\n ns[zero]='0';\n \n ns[zero]=ns[(zero-3>=0)?zero-3:(9-(3-zero))];\n ns[(zero-3>=0)?zero-3:(9-(3-zero))]='0';\n if(!mp[stoi(ns)%xx])\n q.push(make_pair(-now.first-v,ns));\n }\n }\n}", "accuracy": 1, "time_ms": 2870, "memory_kb": 14580, "score_of_the_acc": -0.4226, "final_rank": 6 }, { "submission_id": "aoj_1339_2736707", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nint main(void){\n const int xx=3029561;\n cin.tie(0);\n ios::sync_with_stdio(false);\n int h,v,i;\n while(cin>>h>>v,h){\n string s=\"\",e=\"\";\n char tmp;\n for(i=0;i<9;i++){\n cin>>tmp;\n s+=tmp;\n }\n for(i=0;i<9;i++){\n cin>>tmp;\n e+=tmp;\n }\n priority_queue<pair<int,string>> q;\n q.push(make_pair(0,s));\n vector<bool> mp(xx,false);\n pair<int,string> now;\n int num,zero;\n while(!q.empty()){\n now=q.top();q.pop();\n now.first*=-1;\n num=stoi(now.second);\n if(now.second==e){\n cout<<now.first<<endl;\n break;\n }\n if(!mp[num%xx]){\n mp[num%xx]=true;\n }else{\n continue;\n }\n zero=now.second.find('0');\n string ns=now.second;\n ns[zero]=ns[(zero+1)%9];\n ns[(zero+1)%9]='0';\n if(!mp[stoi(ns)%xx])\n q.push(make_pair(-now.first-h,ns));\n ns[(zero+1)%9]=ns[zero];\n ns[zero]='0';\n \n ns[zero]=ns[(zero-1<0)?8:(zero-1)];\n ns[(zero-1<0)?8:(zero-1)]='0';\n if(!mp[stoi(ns)%xx])\n q.push(make_pair(-now.first-h,ns));\n ns[(zero-1<0)?8:(zero-1)]=ns[zero];\n ns[zero]='0';\n \n ns[zero]=ns[(zero+3)%9];\n ns[(zero+3)%9]='0';\n if(!mp[stoi(ns)%xx])\n q.push(make_pair(-now.first-v,ns));\n ns[(zero+3)%9]=ns[zero];\n ns[zero]='0';\n \n ns[zero]=ns[(zero-3>=0)?zero-3:(9-(3-zero))];\n ns[(zero-3>=0)?zero-3:(9-(3-zero))]='0';\n if(!mp[stoi(ns)%xx])\n q.push(make_pair(-now.first-v,ns));\n }\n }\n}", "accuracy": 1, "time_ms": 2910, "memory_kb": 15008, "score_of_the_acc": -0.433, "final_rank": 8 }, { "submission_id": "aoj_1339_2736701", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nint main(void){\n const int xx=4573769;\n cin.tie(0);\n ios::sync_with_stdio(false);\n int h,v,i;\n while(cin>>h>>v,h){\n string s=\"\",e=\"\";\n char tmp;\n for(i=0;i<9;i++){\n cin>>tmp;\n s+=tmp;\n }\n for(i=0;i<9;i++){\n cin>>tmp;\n e+=tmp;\n }\n priority_queue<pair<int,string>> q;\n q.push(make_pair(0,s));\n vector<bool> mp(xx,false);\n pair<int,string> now;\n int num,zero;\n while(!q.empty()){\n now=q.top();q.pop();\n now.first*=-1;\n num=stoi(now.second);\n if(now.second==e){\n cout<<now.first<<endl;\n break;\n }\n if(!mp[num%xx]){\n mp[num%xx]=true;\n }else{\n continue;\n }\n zero=now.second.find('0');\n string ns=now.second;\n ns[zero]=ns[(zero+1)%9];\n ns[(zero+1)%9]='0';\n if(!mp[stoi(ns)%xx])\n q.push(make_pair(-now.first-h,ns));\n ns[(zero+1)%9]=ns[zero];\n ns[zero]='0';\n \n ns[zero]=ns[(zero-1<0)?8:(zero-1)];\n ns[(zero-1<0)?8:(zero-1)]='0';\n if(!mp[stoi(ns)%xx])\n q.push(make_pair(-now.first-h,ns));\n ns[(zero-1<0)?8:(zero-1)]=ns[zero];\n ns[zero]='0';\n \n ns[zero]=ns[(zero+3)%9];\n ns[(zero+3)%9]='0';\n if(!mp[stoi(ns)%xx])\n q.push(make_pair(-now.first-v,ns));\n ns[(zero+3)%9]=ns[zero];\n ns[zero]='0';\n \n ns[zero]=ns[(zero-3>=0)?zero-3:(9-(3-zero))];\n ns[(zero-3>=0)?zero-3:(9-(3-zero))]='0';\n if(!mp[stoi(ns)%xx])\n q.push(make_pair(-now.first-v,ns));\n }\n }\n}", "accuracy": 1, "time_ms": 3120, "memory_kb": 15716, "score_of_the_acc": -0.47, "final_rank": 13 }, { "submission_id": "aoj_1339_2736679", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nint main(void){\n cin.tie(0);\n ios::sync_with_stdio(false);\n int h,v,i;\n while(cin>>h>>v,h){\n string s=\"\",e=\"\";\n char tmp;\n for(i=0;i<9;i++){\n cin>>tmp;\n s+=tmp;\n }\n for(i=0;i<9;i++){\n cin>>tmp;\n e+=tmp;\n }\n priority_queue<pair<int,string>> q;\n q.push(make_pair(0,s));\n vector<bool> mp(3000000);\n pair<int,string> now;\n int num,zero;\n while(!q.empty()){\n now=q.top();q.pop();\n now.first*=-1;\n num=stoi(now.second);\n if(now.second==e){\n cout<<now.first<<endl;\n break;\n }\n if(!mp[num%2966911]){\n mp[num%2966911]=true;\n }else{\n continue;\n }\n zero=now.second.find('0');\n string ns=now.second;\n ns[zero]=ns[(zero+1)%9];\n ns[(zero+1)%9]='0';\n if(!mp[stoi(ns)%2966911])\n q.push(make_pair(-now.first-h,ns));\n ns[(zero+1)%9]=ns[zero];\n ns[zero]='0';\n \n ns[zero]=ns[(zero-1<0)?8:(zero-1)];\n ns[(zero-1<0)?8:(zero-1)]='0';\n if(!mp[stoi(ns)%2966911])\n q.push(make_pair(-now.first-h,ns));\n ns[(zero-1<0)?8:(zero-1)]=ns[zero];\n ns[zero]='0';\n \n ns[zero]=ns[(zero+3)%9];\n ns[(zero+3)%9]='0';\n if(!mp[stoi(ns)%2966911])\n q.push(make_pair(-now.first-v,ns));\n ns[(zero+3)%9]=ns[zero];\n ns[zero]='0';\n \n ns[zero]=ns[(zero-3>=0)?zero-3:(9-(3-zero))];\n ns[(zero-3>=0)?zero-3:(9-(3-zero))]='0';\n if(!mp[stoi(ns)%2966911])\n q.push(make_pair(-now.first-v,ns));\n }\n }\n}", "accuracy": 1, "time_ms": 2890, "memory_kb": 14576, "score_of_the_acc": -0.4253, "final_rank": 7 }, { "submission_id": "aoj_1339_2736673", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nint main(void){\n cin.tie(0);\n ios::sync_with_stdio(false);\n int h,v,i;\n while(cin>>h>>v,h){\n string s=\"\",e=\"\";\n char tmp;\n for(i=0;i<9;i++){\n cin>>tmp;\n s+=tmp;\n }\n for(i=0;i<9;i++){\n cin>>tmp;\n e+=tmp;\n }\n priority_queue<pair<int,string>> q;\n q.push(make_pair(0,s));\n vector<bool> mp(5000000);\n pair<int,string> now;\n int num,zero;\n while(!q.empty()){\n now=q.top();q.pop();\n now.first*=-1;\n num=stoi(now.second);\n if(now.second==e){\n cout<<now.first<<endl;\n break;\n }\n if(!mp[num%4853333]){\n mp[num%4853333]=true;\n }else{\n continue;\n }\n zero=now.second.find('0');\n string ns=now.second;\n ns[zero]=ns[(zero+1)%9];\n ns[(zero+1)%9]='0';\n if(!mp[stoi(ns)%4853333])\n q.push(make_pair(-now.first-h,ns));\n ns[(zero+1)%9]=ns[zero];\n ns[zero]='0';\n \n ns[zero]=ns[(zero-1<0)?8:(zero-1)];\n ns[(zero-1<0)?8:(zero-1)]='0';\n if(!mp[stoi(ns)%4853333])\n q.push(make_pair(-now.first-h,ns));\n ns[(zero-1<0)?8:(zero-1)]=ns[zero];\n ns[zero]='0';\n \n ns[zero]=ns[(zero+3)%9];\n ns[(zero+3)%9]='0';\n if(!mp[stoi(ns)%4853333])\n q.push(make_pair(-now.first-v,ns));\n ns[(zero+3)%9]=ns[zero];\n ns[zero]='0';\n \n ns[zero]=ns[(zero-3>=0)?zero-3:(9-(3-zero))];\n ns[(zero-3>=0)?zero-3:(9-(3-zero))]='0';\n if(!mp[stoi(ns)%4853333])\n q.push(make_pair(-now.first-v,ns));\n }\n }\n}", "accuracy": 1, "time_ms": 3000, "memory_kb": 15400, "score_of_the_acc": -0.4499, "final_rank": 10 }, { "submission_id": "aoj_1339_2736672", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nint main(void){\n cin.tie(0);\n ios::sync_with_stdio(false);\n int h,v,i;\n while(cin>>h>>v,h){\n string s=\"\",e=\"\";\n char tmp;\n for(i=0;i<9;i++){\n cin>>tmp;\n s+=tmp;\n }\n for(i=0;i<9;i++){\n cin>>tmp;\n e+=tmp;\n }\n priority_queue<pair<int,string>> q;\n q.push(make_pair(0,s));\n vector<bool> mp(5000000,false);\n pair<int,string> now;\n int num,zero;\n while(!q.empty()){\n now=q.top();q.pop();\n now.first*=-1;\n num=stoi(now.second);\n if(now.second==e){\n cout<<now.first<<endl;\n break;\n }\n if(!mp[num%4853333]){\n mp[num%4853333]=true;\n }else{\n continue;\n }\n zero=now.second.find('0');\n string ns=now.second;\n ns[zero]=ns[(zero+1)%9];\n ns[(zero+1)%9]='0';\n if(!mp[stoi(ns)%4853333])\n q.push(make_pair(-now.first-h,ns));\n ns[(zero+1)%9]=ns[zero];\n ns[zero]='0';\n \n ns[zero]=ns[(zero-1<0)?8:(zero-1)];\n ns[(zero-1<0)?8:(zero-1)]='0';\n if(!mp[stoi(ns)%4853333])\n q.push(make_pair(-now.first-h,ns));\n ns[(zero-1<0)?8:(zero-1)]=ns[zero];\n ns[zero]='0';\n \n ns[zero]=ns[(zero+3)%9];\n ns[(zero+3)%9]='0';\n if(!mp[stoi(ns)%4853333])\n q.push(make_pair(-now.first-v,ns));\n ns[(zero+3)%9]=ns[zero];\n ns[zero]='0';\n \n ns[zero]=ns[(zero-3>=0)?zero-3:(9-(3-zero))];\n ns[(zero-3>=0)?zero-3:(9-(3-zero))]='0';\n if(!mp[stoi(ns)%4853333])\n q.push(make_pair(-now.first-v,ns));\n }\n }\n}", "accuracy": 1, "time_ms": 3000, "memory_kb": 15372, "score_of_the_acc": -0.4496, "final_rank": 9 }, { "submission_id": "aoj_1339_2736667", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nconstexpr int pDec[]={1,10,100,1000,10000,100000,1000000,10000000,100000000};\nconstexpr int fct[]={40320,5040,720,120,24,6,2,1};\nconstexpr int dif[]={-1,1,-3,3};\nint makeHash(int num){\n int ans=0;\n int f=0;\n for(int i=0;i<8;i++){\n int tmp=num%10;\n // auto index=lower_bound(vDigit.begin(),vDigit.end(),tmp);\n // ans+=(index-vDigit.begin())*fct[i];\n //vDigit.erase(index);\n \n ans+=(tmp-__builtin_popcount(f&(1<<tmp)-1))*fct[i];\n f|=(1<<tmp);\n num/=10;\n }\n return ans;\n}\n\nint main(void){\n // cout<<makeHash(305147826)<<endl;\n // return 0;\n vector<int> vDigit{0,1,2,3,4,5,6,7,8};\n \n // int count=0;\n // do{\n // int num=0;\n // for(int i=9;i>0;i--){\n // num+=pDec[i-1]*vDigit[9-i];\n // }\n // mp.emplace(num,count++);\n // }while(next_permutation(vDigit.begin(),vDigit.end()));\n // Your code here!\n int h,v;\n while(cin>>h>>v,h||v){\n int hv[]={h,h,v,v};\n int st=0;\n int end=0;\n int a;\n vector<bool> visited(362880,false);\n cin>>st;\n for(int i=0;i<8;i++){\n st*=10;\n cin>>a;\n st+=a;\n }\n cin>>end;\n for(int i=0;i<8;i++){\n end*=10;\n cin>>a;\n end+=a;\n }\n priority_queue<pair<int,int>> q;\n q.emplace(0,st);\n while(1){\n int pos=q.top().second;\n int cost=q.top().first;\n if(pos==end){\n cout<<-cost<<endl;\n break;\n }\n q.pop();\n int hash=makeHash(pos);\n if(visited[hash]){\n continue;\n }\n visited[hash]=true;\n int zero=to_string(pos+1000000000).find('0')-1;\n for(int i=0;i<4;i++){\n int s=pos,t=8-((zero+dif[i]+9)%9);\n int tmp=(pos/pDec[t])%10;\n s+=(pDec[8-zero]*tmp-pDec[t]*tmp);\n //cout<<tmp<<\" \"<<pos<<\" \"<<s<<endl;\n q.emplace(cost-hv[i],s);\n }\n }\n }\n return 0;\n}", "accuracy": 1, "time_ms": 1990, "memory_kb": 10132, "score_of_the_acc": -0.2505, "final_rank": 3 }, { "submission_id": "aoj_1339_2736663", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nint main(void){\n cin.tie(0);\n ios::sync_with_stdio(false);\n int h,v,i;\n while(cin>>h>>v,h){\n string s=\"\",e=\"\";\n char tmp;\n for(i=0;i<9;i++){\n cin>>tmp;\n s+=tmp;\n }\n for(i=0;i<9;i++){\n cin>>tmp;\n e+=tmp;\n }\n priority_queue<pair<int,string>> q;\n q.push(make_pair(0,s));\n vector<bool> mp(5000000,false);\n pair<int,string> now;\n int num,zero;\n while(!q.empty()){\n now=q.top();q.pop();\n now.first*=-1;\n num=stoi(now.second);\n if(now.second==e){\n cout<<now.first<<endl;\n break;\n }\n if(!mp[num%4853333]){\n mp[num%4853333]=true;\n }else{\n continue;\n }\n zero=now.second.find('0');\n string ns=now.second;\n ns[zero]=ns[(zero+1)%9];\n ns[(zero+1)%9]='0';\n if(!mp[stoi(ns)%4853333])\n q.push(make_pair(-now.first-h,ns));\n ns[(zero+1)%9]=ns[zero];\n ns[zero]='0';\n \n ns[zero]=ns[(zero-1<0)?8:(zero-1)];\n ns[(zero-1<0)?8:(zero-1)]='0';\n if(!mp[stoi(ns)%4853333])\n q.push(make_pair(-now.first-h,ns));\n ns[(zero-1<0)?8:(zero-1)]=ns[zero];\n ns[zero]='0';\n \n ns[zero]=ns[(zero+3)%9];\n ns[(zero+3)%9]='0';\n if(!mp[stoi(ns)%4853333])\n q.push(make_pair(-now.first-v,ns));\n ns[(zero+3)%9]=ns[zero];\n ns[zero]='0';\n \n ns[zero]=ns[(zero-3>=0)?zero-3:(9-(3-zero))];\n ns[(zero-3>=0)?zero-3:(9-(3-zero))]='0';\n if(!mp[stoi(ns)%4853333])\n q.push(make_pair(-now.first-v,ns));\n }\n }\n}", "accuracy": 1, "time_ms": 3050, "memory_kb": 15428, "score_of_the_acc": -0.4571, "final_rank": 11 }, { "submission_id": "aoj_1339_2736655", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n#define rep(i,n) for(int i=0;i<n;i++)\n#define fs first\n#define sc second\nconstexpr int tv[]={-3,-1,1,3};\nconstexpr int p10[]={100000000,10000000,1000000,100000,10000,1000,100,10,1};\nconstexpr int fact[]={40320,5040,720,120,24,6,2,1};\n\nint my_hash(int f){\n int flag=0,ret=0;\n rep(i,8){\n ret+=fact[i]*(f%10-__builtin_popcount(flag&((1<<(f%10))-1)));\n flag|=1<<(f%10);\n f/=10;\n }\n return ret;\n}\n\n\nint main(){\n \n int h,v;\n string a=\"123456789\",b=\"123456789\";\n using data=pair<int,int>;\n \n while(cin>>h>>v, h or v){\n vector<int> hm(362880,-10000000);\n int tc[]={v,h,h,v};\n rep(i,9)cin>>a[i];\n rep(i,9)cin>>b[i];\n int sa=stoi(\"1\"+a)-1000000000,sb=stoi(\"1\"+b)-1000000000;\n\n priority_queue<data> q;\n q.emplace(0,sa);\n \n while(1){\n int cost=q.top().fs;\n int board=q.top().sc;\n if(board==sb){\n cout<<-cost<<endl;\n break;\n }\n q.pop();\n int index=to_string(board).find('0');\n if(index==-1)index=0;\n rep(i,4){\n int fixed=(index+tv[i]+9)%9;\n int next=board;\n int fx=board/p10[fixed]%10;\n next=next+(p10[index]-p10[fixed]) * fx;\n int nc=cost-tc[i];\n int hval=my_hash(next);\n if(hm[hval]<nc){\n q.emplace(nc, next);\n hm[hval]=nc;\n }\n }\n }\n }\n}", "accuracy": 1, "time_ms": 1400, "memory_kb": 7428, "score_of_the_acc": -0.1382, "final_rank": 1 }, { "submission_id": "aoj_1339_2736642", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n#define rep(i,n) for(int i=0;i<n;i++)\n#define fs first\n#define sc second\nconstexpr int tv[]={-3,-1,1,3};\nconstexpr int p10[]={100000000,10000000,1000000,100000,10000,1000,100,10,1};\nconstexpr int fact[]={40320,5040,720,120,24,6,2,1};\n\nint my_hash(int f){\n vector<int> v={0,1,2,3,4,5,6,7,8};\n int ret=0;\n rep(i,8){\n auto itr=lower_bound(v.begin(),v.end(),f%10);\n ret+=fact[i]*(itr-v.begin());\n v.erase(itr);\n f/=10;\n }\n return ret;\n}\n\n\nint main(){\n \n int h,v;\n string a=\"123456789\",b=\"123456789\";\n using data=pair<int,int>;\n \n while(cin>>h>>v, h or v){\n vector<int> hm(362880,-10000000);\n int tc[]={v,h,h,v};\n rep(i,9)cin>>a[i];\n rep(i,9)cin>>b[i];\n int sa=stoi(\"1\"+a)-1000000000,sb=stoi(\"1\"+b)-1000000000;\n\n priority_queue<data> q;\n q.emplace(0,sa);\n \n while(1){\n int cost=q.top().fs;\n int board=q.top().sc;\n if(board==sb){\n cout<<-cost<<endl;\n break;\n }\n q.pop();\n int index=to_string(board).find('0');\n if(index==-1)index=0;\n rep(i,4){\n int fixed=(index+tv[i]+9)%9;\n int next=board;\n int fx=board/p10[fixed]%10;\n next=next+(p10[index]-p10[fixed]) * fx;\n int nc=cost-tc[i];\n int hval=my_hash(next);\n if(hm[hval]<nc){\n q.emplace(nc, next);\n hm[hval]=nc;\n }\n }\n }\n }\n}", "accuracy": 1, "time_ms": 2310, "memory_kb": 7384, "score_of_the_acc": -0.2631, "final_rank": 4 }, { "submission_id": "aoj_1339_2736634", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nint main(void){\n cin.tie(0);\n ios::sync_with_stdio(false);\n int h,v,i;\n while(cin>>h>>v,h){\n string s=\"\",e=\"\";\n char tmp;\n for(i=0;i<9;i++){\n cin>>tmp;\n s+=tmp;\n }\n for(i=0;i<9;i++){\n cin>>tmp;\n e+=tmp;\n }\n priority_queue<pair<int,string>> q;\n q.push(make_pair(0,s));\n unordered_map<int,bool> mp;\n pair<int,string> now;\n int num,zero;\n while(!q.empty()){\n now=q.top();q.pop();\n now.first*=-1;\n num=stoi(now.second);\n if(now.second==e){\n cout<<now.first<<endl;\n break;\n }\n if((mp.find(num)==mp.end())){\n mp[num]=true;\n }else{\n continue;\n }\n zero=now.second.find('0');\n string ns=now.second;\n ns[zero]=ns[(zero+1)%9];\n ns[(zero+1)%9]='0';\n if((mp.find(stoi(ns))==mp.end()))\n q.push(make_pair(-now.first-h,ns));\n ns[(zero+1)%9]=ns[zero];\n ns[zero]='0';\n \n ns[zero]=ns[(zero-1<0)?8:(zero-1)];\n ns[(zero-1<0)?8:(zero-1)]='0';\n if((mp.find(stoi(ns))==mp.end()))\n q.push(make_pair(-now.first-h,ns));\n ns[(zero-1<0)?8:(zero-1)]=ns[zero];\n ns[zero]='0';\n \n ns[zero]=ns[(zero+3)%9];\n ns[(zero+3)%9]='0';\n if((mp.find(stoi(ns))==mp.end()))\n q.push(make_pair(-now.first-v,ns));\n ns[(zero+3)%9]=ns[zero];\n ns[zero]='0';\n \n ns[zero]=ns[(zero-3>=0)?zero-3:(9-(3-zero))];\n ns[(zero-3>=0)?zero-3:(9-(3-zero))]='0';\n if((mp.find(stoi(ns))==mp.end()))\n q.push(make_pair(-now.first-v,ns));\n }\n }\n}", "accuracy": 1, "time_ms": 5900, "memory_kb": 29756, "score_of_the_acc": -1.0136, "final_rank": 18 }, { "submission_id": "aoj_1339_2736632", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nint main(void){\n cin.tie(0);\n ios::sync_with_stdio(false);\n int h,v,i;\n while(cin>>h>>v,h){\n string s=\"\",e=\"\";\n char tmp;\n for(i=0;i<9;i++){\n cin>>tmp;\n s+=tmp;\n }\n for(i=0;i<9;i++){\n cin>>tmp;\n e+=tmp;\n }\n priority_queue<pair<int,string>> q;\n q.push(make_pair(0,s));\n unordered_map<int,bool> mp;\n pair<int,string> now;\n int num,zero;\n while(!q.empty()){\n now=q.top();q.pop();\n now.first*=-1;\n num=stoi(now.second);\n if(now.second==e){\n cout<<now.first<<endl;\n break;\n }\n if((mp.find(num)==mp.end())){\n mp[num]=true;\n }else{\n continue;\n }\n zero=now.second.find('0');\n string ns=now.second;\n ns[zero]=ns[(zero+1)%9];\n ns[(zero+1)%9]='0';\n if((mp.find(stoi(ns))==mp.end()))\n q.push(make_pair(-now.first-h,ns));\n ns[(zero+1)%9]=ns[zero];\n ns[zero]='0';\n \n ns[zero]=ns[(zero-1<0)?8:(zero-1)];\n ns[(zero-1<0)?8:(zero-1)]='0';\n if((mp.find(stoi(ns))==mp.end()))\n q.push(make_pair(-now.first-h,ns));\n ns[(zero-1<0)?8:(zero-1)]=ns[zero];\n ns[zero]='0';\n \n ns[zero]=ns[(zero+3)%9];\n ns[(zero+3)%9]='0';\n if((mp.find(stoi(ns))==mp.end()))\n q.push(make_pair(-now.first-v,ns));\n ns[(zero+3)%9]=ns[zero];\n ns[zero]='0';\n \n ns[zero]=ns[(zero-3>=0)?zero-3:(9-(3-zero))];\n ns[(zero-3>=0)?zero-3:(9-(3-zero))]='0';\n if((mp.find(stoi(ns))==mp.end()))\n q.push(make_pair(-now.first-v,ns));\n ns[(zero-3>=0)?zero-3:(9-(3-zero))]=ns[zero];\n ns[zero]='0';\n }\n }\n}", "accuracy": 1, "time_ms": 5860, "memory_kb": 29868, "score_of_the_acc": -1.0094, "final_rank": 17 }, { "submission_id": "aoj_1339_2736630", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\n\nint makeHash(int num){\n vector<int> vDigit{0,1,2,3,4,5,6,7,8};\n vector<int> pDec{1,10,100,1000,10000,100000,1000000,10000000,100000000};\n int base=9;\n int fct=362880;\n int ans=0;\n for(int i=base;i>1;i--){\n fct/=i;\n auto index=lower_bound(vDigit.begin(),vDigit.end(),(num/pDec[i-1])%10);\n ans+=(index-vDigit.begin())*fct;\n vDigit.erase(index);\n }\n return ans;\n}\n\nint main(void){\n // cout<<makeHash(305147826)<<endl;\n // return 0;\n vector<int> vDigit{0,1,2,3,4,5,6,7,8};\n vector<int> pDec{1,10,100,1000,10000,100000,1000000,10000000,100000000};\n \n vector<int> dif{-1,1,-3,3};\n // int count=0;\n // do{\n // int num=0;\n // for(int i=9;i>0;i--){\n // num+=pDec[i-1]*vDigit[9-i];\n // }\n // mp.emplace(num,count++);\n // }while(next_permutation(vDigit.begin(),vDigit.end()));\n // Your code here!\n int h,v;\n while(cin>>h>>v,h||v){\n string st=\" \";\n string end=\" \";\n vector<bool> visited(362880,false);\n vector<int> hv{h,h,v,v};\n for(int i=0;i<9;i++){\n cin>>st[i];\n }\n for(int i=0;i<9;i++){\n cin>>end[i];\n }\n priority_queue<pair<int,int>,vector<pair<int,int>>,greater<pair<int,int>>> q;\n q.emplace(0,stoi(st));\n int iend=stoi(end);\n while(1){\n int pos=q.top().second;\n int cost=q.top().first;\n if(pos==iend){\n cout<<cost<<endl;\n break;\n }\n q.pop();\n int hash=makeHash(pos);\n if(visited[hash]){\n continue;\n }\n visited[hash]=true;\n int zero=to_string(pos+1000000000).find('0')-1;\n for(int i=0;i<4;i++){\n int s=pos,t=8-((zero+dif[i]+9)%9);\n int tmp=(pos/pDec[t])%10;\n s+=(pDec[8-zero]*tmp-pDec[t]*tmp);\n //cout<<tmp<<\" \"<<pos<<\" \"<<s<<endl;\n q.emplace(cost+hv[i],s);\n }\n }\n }\n return 0;\n}", "accuracy": 1, "time_ms": 2800, "memory_kb": 10088, "score_of_the_acc": -0.3615, "final_rank": 5 } ]
aoj_1340_cpp
Problem F: Directional Resemblance Vectors have their directions and two vectors make an angle between them. Given a set of three-dimensional vectors, your task is to find the pair among them that makes the smallest angle. Input The input is a sequence of datasets. A dataset specifies a set of three-dimensional vectors, some of which are directly given in the dataset and the others are generated by the procedure described below. Each dataset is formatted as follows. m n S W x 1 y 1 z 1 x 2 y 2 z 2 : x m y m z m The first line contains four integers m , n , S , and W . The integer m is the number of vectors whose three components are directly specified in the dataset. Starting from the second line, m lines have the three components of m vectors. The i -th line of which indicates the vector v i = ( x i , y i , z i ). All the vector components are positive integers less than or equal to 100. The integer n is the number of vectors generated by the following procedure. int g = S; for(int i=m+1; i<=m+n; i++) { x[i] = (g/7) %100 + 1; y[i] = (g/700) %100 + 1; z[i] = (g/70000)%100 + 1; if( g%2 == 0 ) { g = (g/2); } else { g = (g/2) ^ W; } } For i = m + 1, . . . , m + n , the i -th vector v i of the set has three components x[i] , y[i] , and z[i] generated by this procedure. Here, values of S and W are the parameters S and W given in the first line of the dataset. You may assume 1 ≤ S ≤ 10 9 and 1 ≤ W ≤ 10 9 . The total number of vectors satisfies 2 ≤ m + n ≤ 12 × 10 4 . Note that exactly the same vector may be specified twice or more in a single dataset. A line containing four zeros indicates the end of the input. The total of m+n for all the datasets in the input never exceeds 16 × 10 5 . Output For each dataset, output the pair of vectors among the specified set with the smallest non-zero angle in between. It is assured that there are at least two vectors having different directions. Vectors should be indicated by their three components. The output for a pair of vectors v a and v b should be formatted in a line as follows. x a y a z a x b y b z b Two vectors ( x a , y a , z a ) and ( x b , y b , z b ) are ordered in the dictionary order, that is, v a < v b if x a < x b , or if x a = x b and y a < y b , or if x a = x b , y a = y b and z a < z b . When a vector pair is output, the smaller vector in this order should be output first. If more than one pair makes the equal smallest angle, the pair that is the smallest among them in the dictionary order between vector pairs should be output. The pair ( v i , v j ) is smaller than the pair ( v k , v l ) if v i < v k , or if v i = v k and v j < v l . Sample Input 4 0 2013 1124 1 1 1 2 2 2 2 1 1 1 2 1 2 100 131832453 129800231 42 40 46 42 40 46 0 100000 7004674 484521438 0 0 0 0 Output for the Sample Input 1 1 1 1 2 1 42 40 46 83 79 91 92 92 79 99 99 85
[ { "submission_id": "aoj_1340_10848362", "code_snippet": "#include <bits/stdc++.h>\n//using namespace std;\n\ntypedef long double LD;\n\nconst LD INF = 1e15;\nconst LD eps = 1e-13;\nconst int N = 155555;\n\nint n, m, S, W;\n\ninline LD norm(const LD &x) {\n\treturn x * x;\n}\n\nstruct OriP {\n\tint x, y, z;\n} p[N];\n\nstruct Point {\n\tLD x, y, z;\n\tint id;\n\tPoint() = default;\n\tPoint(const OriP &p) {\n\t\tx = p.x;\n\t\ty = p.y;\n\t\tz = p.z;\n\t}\n\tconst LD& operator [] (int index) const {\n\t\tif (index == 0) {\n\t\t\treturn x;\n\t\t} else {\n\t\t\treturn index == 1 ? y : z;\n\t\t}\n\t}\n\tfriend LD dist(const Point &a, const Point &b) {\n\t\tLD result = 0.0;\n\t\tfor (int i = 0; i < 3; ++i) {\n\t\t\tresult += norm(a[i] - b[i]);\n\t\t}\n\t\treturn result;\n\t}\n} point[N];\n\ninline bool operator == (const OriP &p, const OriP &q) {\n\treturn p.x == q.x && p.y == q.y && p.z == q.z;\n}\n\ninline bool operator < (const OriP &p, const OriP &q) {\n\tif (p.x != q.x) {\n\t\treturn p.x < q.x;\n\t}\n\tif (p.y != q.y) {\n\t\treturn p.y < q.y; \n\t} else {\n\t\treturn p.z < q.z;\n\t}\n}\n\ninline bool operator > (const OriP &p, const OriP &q) {\n\tif (p.x != q.x) {\n\t\treturn p.x > q.x;\n\t}\n\tif (p.y != q.y) {\n\t\treturn p.y > q.y; \n\t} else {\n\t\treturn p.z > q.z;\n\t}\n}\n\nstruct Rectangle {\n\tLD min[3], max[3];\n\tRectangle() {\n\t\tmin[0] = min[1] = min[2] = INF;\n\t\tmax[0] = max[1] = max[2] = -INF;\n\t}\n\tvoid add(const Point &p) {\n\t\tfor (int i = 0; i < 3; ++i) {\n\t\t\tmin[i] = std::min(min[i], p[i]);\n\t\t\tmax[i] = std::max(max[i], p[i]);\n\t\t}\n//assert(min[0] > -1 && min[1] > -1 && min[2] > -1);\n//assert(min[0] < 100 && min[1] < 100 && min[2] < 100);\n\t}\n\tdouble dist(const Point &p) {\n\t\n\t//std::cout << min[0] << \" \" << min[1] << \" \" << min[2] << std::endl;\n\t//std::cout << max[0] << \" \" << max[1] << \" \" << max[2] << std::endl;\n\t\tdouble result = 0;\n\t\tfor (int i = 0; i < 3; ++i) {\n\t\t\tresult += norm(std::min(std::max(p[i], min[i]), max[i]) - p[i]);\n\t\t\t//result += std::max(norm(max[i] - p[i]), norm(min[i] - p[i]));\n\t\t}\n//assert(min[0] > -1 && min[1] > -1 && min[2] > -1);\n//assert(min[0] < 100 && min[1] < 100 && min[2] < 100);\n//return INF;\n\t\treturn result;\n\t}\n};\n\nstruct Node {\n\tPoint seperator;\n\tRectangle rectangle;\n\tint child[2];\n\tvoid reset(const Point &p) {\n\t\tseperator = p;\n\t\trectangle = Rectangle();\n\t\trectangle.add(p);\n\t\tchild[0] = child[1] = 0;\n\t}\n} tree[N << 1];\n\nint size, pivot;\n\ninline int sign(LD x) {\n\tif (x < -eps) {\n\t\treturn -1;\n\t}\n\telse {\n\t\treturn x > eps;\n\t}\n}\n\ninline bool compare(const Point &a, const Point &b) {\n//std::cerr << \"cmp\" << \" \" << pivot << \" \" << a.x << \" \" << a.y << \" \" << a.z << \" \" << b.x << \" \" << b.y << \" \" << b.z << std::endl;\n//std::cerr << a[pivot] << \" \" << b[pivot] << std::endl;\n\tif (sign(a[pivot] - b[pivot]) == 0) {\n//std::cerr << \"check_id \" << a.id << \" \" << b.id << std::endl;\n\t\tif (a.id == -1 || b.id == -1) {\n\t\t\treturn true;\n\t\t}\n\t\treturn p[a.id] < p[b.id];\n\t} else {\n\t\treturn a[pivot] < b[pivot];\n\t}\n}\n\ninline int build(int l, int r, int type = 1) {\n//std::cout << l << \" \" << r << \" \" << std::endl;\n\tpivot = type;\n\tif (l > r) {\n\t\treturn 0;\n\t}\n\tint x = ++size;\n\tint mid = l + r >> 1;\n\tstd::nth_element(point + l, point + mid, point + r, compare);\n\ttree[x].reset(point[mid]);\n//std::cout << l << \" \" << mid << \" \" << r << \" \" << tree[x].seperator.id << std::endl;\n//std::cout << tree[x].seperator.x << \" \" << tree[x].seperator.y << \" \" << tree[x].seperator.z << \" \" << std::endl;\n\tfor (int i = l; i <= r; ++i) {\n\t\ttree[x].rectangle.add(point[i]);\n\t}\n\ttree[x].child[0] = build(l, mid - 1, (type + 1) % 3);\n\ttree[x].child[1] = build(mid + 1, r, (type + 1) % 3);\n\t\n//assert(tree[x].rectangle.min[0] > -1 && tree[x].rectangle.min[1] > -1 && tree[x].rectangle.min[2] > -1);\n//assert(tree[x].rectangle.min[0] < 100 && tree[x].rectangle.min[1] < 100 && tree[x].rectangle.min[2] < 100);\n\treturn x;\n}\n\ninline void query(int x, const Point &u, std::pair<LD, int> & answer, int type = 1) {\n//std::cout << \"root = \" << x << \" \" << u.x << \" \" << u.y << \" \" << u.z << std::endl;\n\tif (!x) {\n\t\treturn;\n\t}\n\tpivot = type;\n//std::cout << tree[x].rectangle.dist(u) << std::endl;\n\tif (tree[x].rectangle.dist(u) > answer.first + 1e-6) {\n\t\treturn;\n\t}\n\tdouble d = dist(tree[x].seperator, u);\n\t\n/*std::cout << \"asdfasf\" << std::endl;\nstd::cout << u.x << \" \" << u.y << \" \" << u.z << std::endl;\nstd::cout << tree[x].seperator.x << \" \" << tree[x].seperator.y << \" \" << tree[x].seperator.z << \" \" << std::endl;\nstd::cout << d << \" \" << sign(d) << std::endl;\n*/\n\t\n\tif (sign(d) && (sign(d - answer.first) == -1 || sign(d - answer.first) == 0 && p[tree[x].seperator.id] < p[answer.second])) {\n\t\tanswer = std::make_pair(d, tree[x].seperator.id);\n\t}\n//std::cerr << \"gogogo\" << std::endl;\n\tif (compare(u, tree[x].seperator)) {\n\t\tquery(tree[x].child[0], u, answer, (type + 1) % 3);\n\t\tquery(tree[x].child[1], u, answer, (type + 1) % 3);\n\t} else {\n\t\tquery(tree[x].child[1], u, answer, (type + 1) % 3);\n\t\tquery(tree[x].child[0], u, answer, (type + 1) % 3);\n\t}\n} \n\n\nint main() {\n\tstd::ios::sync_with_stdio(false);\n\tstd::cin.tie(0);\n\tstd::cout.tie(0);\n\t\n\twhile (std::cin >> m >> n >> S >> W) {\n\t\tif (!m && !n && !S && !W) {\n\t\t\treturn 0;\n\t\t}\n\t\tfor (int i = 1; i <= m; ++i) {\n\t\t\tstd::cin >> p[i].x >> p[i].y >> p[i].z;\n\t\t}\n\t\tint g = S;\n\t\tfor (int i = 1; i <= n; ++i) {\n\t\t//std::cerr << i << std::endl;\n\t\t\tp[i + m].x = (g / 7) % 100 + 1;\n\t\t\tp[i + m].y = (g / 700) % 100 + 1;\n\t\t\tp[i + m].z = (g / 70000) % 100 + 1;\n\t\t\tif (g % 2 == 0) {\n\t\t\t\tg = g / 2;\t\n\t\t\t} else {\n\t\t\t\tg = (g / 2) ^ W;\n\t\t\t}\n\t\t}\n\t\tn += m;//return 0;\n\t\t\n\t\tstd::pair<LD, int> nowans;\n\t\tfor (int i = 1; i <= n; ++i) {\n\t\t\tdouble dist = sqrt(p[i].x * p[i].x + p[i].y * p[i].y + p[i].z * p[i].z);\n\t\t\tpoint[i].x = p[i].x / dist;\n\t\t\tpoint[i].y = p[i].y / dist;\n\t\t\tpoint[i].z = p[i].z / dist;\n\t\t\tpoint[i].id = i;\n//std::cout << \"node[\" << i << \"]= \" << p[i].x << \" \" << p[i].y << \" \" << p[i].z << std::endl;\n//std::cout << point[i].x << \" \" << point[i].y << \" \" << point[i].z << std::endl;\n\t\t}\n\t\tsize = 0;\n\t\tbuild(1, n);// return 0;\n\t\t//std::cout << size << std::endl; return 0;\n\t\t//std::cout << \"adsf\" << std::endl;\n\t\tstd::pair<int, int> ans = std::make_pair(-1, 2);\n\t\tdouble ansd = (p[1].x * p[2].x + p[1].y * p[2].y + p[1].z * p[2].z) \n\t\t\t\t/ sqrt(p[1].x * p[1].x + p[1].y * p[1].y + p[1].z * p[1].z)\n\t\t\t\t/ sqrt(p[2].x * p[2].x + p[2].y * p[2].y + p[2].z * p[2].z);\n\t\tfor (int i = 1; i <= n; ++i) {\n\t\t\tnowans = std::make_pair(INF, 0);\n\t\t\tPoint u(p[i]);\n\t\t\tdouble d = sqrt(p[i].x * p[i].x + p[i].y * p[i].y + p[i].z * p[i].z);\n\t\t\tu.x /= d;\n\t\t\tu.y /= d;\n\t\t\tu.z /= d;\n\t\t\tu.id = -1;\n//std::cerr << \"query \" << i << \" \" << std::endl;\n\t\t\tquery(1, u, nowans);\n\t\t\tstd::pair<int, int> now = std::make_pair(i, nowans.second);\n\t\t\tif (p[now.first] > p[now.second]) {\n\t\t\t\tstd::swap(now.first, now.second);\n\t\t\t}\n\t\t\tint j = now.first, k = now.second;\n\t\t\td = (p[j].x * p[k].x + p[j].y * p[k].y + p[j].z * p[k].z) \n\t\t\t\t/ sqrt(p[j].x * p[j].x + p[j].y * p[j].y + p[j].z * p[j].z)\n\t\t\t\t/ sqrt(p[k].x * p[k].x + p[k].y * p[k].y + p[k].z * p[k].z);\n/*std::cout << \"now: \" << i << \" \" << d << \" \" << nowans.second << \" \" << nowans.first << std::endl;\nstd::cout << u.x << \" \" << u.y << \" \" << u.z << std::endl;\nstd::cout << p[nowans.second].x << \" \" << p[nowans.second].y << \" \" << p[nowans.second].z << std::endl;\t\n*/\t\t\tif (ans.first == -1) {\n\t\t\t\tans = now;\n\t\t\t\tcontinue;\n\t\t\t}\n\t\t\tif (sign(d - ansd) == -1) {\n\t\t\t\tcontinue;\n\t\t\t}\n\t\t\tif (sign(d - ansd) == 1) {\t\n\t\t\t\tans = now;\n\t\t\t\tansd = d;\n\t\t\t} else {\n\t\t\t\tif (p[j] < p[ans.first] || p[j] == p[ans.first] && p[k] < p[ans.second]) {\n\t\t\t\t\tans = now;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\t\n\t\t//std::cerr << \"123\" << std::endl;\n\t\t\n\t\tstd::cout << p[ans.first].x << \" \" << p[ans.first].y << \" \" << p[ans.first].z << \" \" \n\t\t\t\t<< p[ans.second].x << \" \" << p[ans.second].y << \" \" << p[ans.second].z << std::endl;\n\t}\t\n}", "accuracy": 1, "time_ms": 3400, "memory_kb": 67544, "score_of_the_acc": -1.4083, "final_rank": 19 }, { "submission_id": "aoj_1340_6148187", "code_snippet": "#pragma GCC optimize(\"O3\")\n#pragma GCC optimize(\"unroll-loops\")\n#include<iostream>\n#include<string>\n#include<cstdio>\n#include<vector>\n#include<cmath>\n#include<algorithm>\n#include<functional>\n#include<iomanip>\n#include<queue>\n#include<ciso646>\n#include<random>\n#include<map>\n#include<set>\n#include<bitset>\n#include<stack>\n#include<unordered_map>\n#include<unordered_set>\n#include<utility>\n#include<cassert>\n#include<complex>\n#include<numeric>\n#include<array>\n#include<chrono>\nusing namespace std;\n\n//#define int long long\ntypedef long long ll;\n\ntypedef unsigned long long ul;\ntypedef unsigned int ui;\nconstexpr ll mod = 998244353;\nconst ll INF = mod * mod;\ntypedef pair<int, int>P;\n\n#define rep(i,n) for(int i=0;i<n;i++)\n#define per(i,n) for(int i=n-1;i>=0;i--)\n#define Rep(i,sta,n) for(int i=sta;i<n;i++)\n#define rep1(i,n) for(int i=1;i<=n;i++)\n#define per1(i,n) for(int i=n;i>=1;i--)\n#define Rep1(i,sta,n) for(int i=sta;i<=n;i++)\n#define all(v) (v).begin(),(v).end()\ntypedef pair<ll, ll> LP;\ntypedef double ld;\ntypedef pair<ld, ld> LDP;\nconst ld eps = 1e-8;\nconst ld pi = acosl(-1.0);\n\ntemplate<typename T>\nvoid chmin(T& a, T b) {\n\ta = min(a, b);\n}\ntemplate<typename T>\nvoid chmax(T& a, T b) {\n\ta = max(a, b);\n}\ntemplate<typename T>\nvoid cinarray(vector<T>& v) {\n\trep(i, v.size())cin >> v[i];\n}\ntemplate<typename T>\nvoid coutarray(vector<T>& v) {\n\trep(i, v.size()) {\n\t\tif (i > 0)cout << \" \"; cout << v[i];\n\t}\n\tcout << \"\\n\";\n}\n\nll gcd(ll a, ll b) {\n\ta = abs(a); b = abs(b);\n\tif (a < b)swap(a, b);\n\twhile (b) {\n\t\tll r = a % b; a = b; b = r;\n\t}\n\treturn a;\n}\n\n\n\n\n\nint m, n, s, w;\nvoid solve() {\n\tvector<vector<int>> v(m + n);\n\trep(i, m) {\n\t\tv[i].resize(3);\n\t\trep(j, 3)cin >> v[i][j];\n\t}\n\tint g = s;\n\trep(i, n) {\n\t\tv[i + m].resize(3);\n\t\tv[i + m][0] = (g / 7) % 100 + 1;\n\t\tv[i + m][1] = (g / 700) % 100 + 1;\n\t\tv[i + m][2] = (g / 70000) % 100 + 1;\n\t\tif (g % 2 == 0)g /= 2;\n\t\telse g = (g / 2) ^ w;\n\t}\n\t//erase same vector\n\t{\n\t\tvector<vector<int>> nv;\n\t\tset<vector<int>> st;\n\t\trep(i, m + n) {\n\t\t\tint g = 0;\n\t\t\trep(j, 3)g = gcd(g, v[i][j]);\n\t\t\tvector<int> cur(3);\n\t\t\trep(j, 3)cur[j] = v[i][j] / g;\n\t\t\tif (!st.count(cur)) {\n\t\t\t\tnv.push_back(v[i]);\n\t\t\t\tst.insert(cur);\n\t\t\t}\n\t\t}\n\t\tswap(v, nv);\n\t\tn = v.size();\n\t\tsort(all(v));\n\t}\n\tassert(n >= 2);\n\tvector<pair<LDP, int>> vtp;\n\tvector<ld> rr(n);\n\trep(i, n) {\n\t\trr[i] = sqrt(v[i][0] * v[i][0] + v[i][1] * v[i][1] + v[i][2] * v[i][2]);\n\t\tvtp.push_back({ {v[i][0] / rr[i],v[i][1] / rr[i]},i });\n\t}\n\tsort(all(vtp));\n\tauto calc = [&](int i, int j) {\n\t\tld res = 0;\n\t\trep(k, 3)res += (v[i][k]/rr[i] - v[j][k]/rr[j]) * (v[i][k]/rr[i] - v[j][k]/rr[j]);\n\t\treturn res;\n\t};\n\tld ans = mod;\n\tP ansp = { -1,-1 };\n\tfunction<ld(int, int)> yaru = [&](int l, int r)->ld {\n\t\tld mi = mod;\n\t\tif (l + 1 == r)return mod;\n\t\tint m = (l + r) / 2;\n\t\tchmin(mi, yaru(l, m));\n\t\tchmin(mi, yaru(m, r));\n\t\tld res = mi;\n\t\tld t = vtp[m - 1].first.first;\n\t\tvector<pair<ld, int>> vl, vr;\n\t\tRep(i, l, m) {\n\t\t\tld dx = t - vtp[i].first.first;\n\t\t\tif (dx*dx > ans + (1e-8))continue;\n\t\t\tvl.push_back({ vtp[i].first.second,vtp[i].second });\n\t\t}\n\t\tRep(i, m, r) {\n\t\t\tld dx = t - vtp[i].first.first;\n\t\t\tif (dx * dx > ans + (1e-8))continue;\n\t\t\tvr.push_back({ vtp[i].first.second,vtp[i].second });\n\t\t}\n\t\tsort(all(vl));\n\t\tsort(all(vr));\n\t\tint locl = 0;\n\t\tint locr = 0;\n\t\t//[locl,locr)\n\t\trep(i, vl.size()) {\n\t\t\twhile (locl < vr.size() && vr[locl].first < vl[i].first) {\n\t\t\t\tld dy = vl[i].first - vr[locl].first;\n\t\t\t\tif (dy * dy > ans + (1e-8))locl++;\n\t\t\t\telse break;\n\t\t\t}\n\t\t\twhile (locr < vr.size()) {\n\t\t\t\tif (vr[locr].first < vl[i].first)locr++;\n\t\t\t\telse {\n\t\t\t\t\tld dy = vr[locr].first - vl[i].first;\n\t\t\t\t\tif (dy * dy > ans + (1e-8))break;\n\t\t\t\t\telse locr++;\n\t\t\t\t}\n\t\t\t}\n\t\t\tassert(locl <= locr);\n\t\t\tRep(j, locl, locr) {\n\t\t\t\tld val = calc(vl[i].second,vr[j].second);\n\t\t\t\tP p = minmax(vl[i].second, vr[j].second);\n\t\t\t\tif (abs(ans - val) < (1e-12)) {\n\t\t\t\t\tansp = min(ansp, p);\n\t\t\t\t}\n\t\t\t\telse if (ans > val) {\n\t\t\t\t\tans = val;\n\t\t\t\t\tansp = p;\n\t\t\t\t}\n\t\t\t\tres = min(res, val);\n\t\t\t}\n\t\t}\n\t\treturn res;\n\t};\n\tyaru(0, n);\n\tassert(ansp.first >= 0);\n\tvector<int> anss;\n\trep(j, 3) {\n\t\tanss.push_back(v[ansp.first][j]);\n\t}\n\trep(j, 3) {\n\t\tanss.push_back(v[ansp.second][j]);\n\t}\n\tcoutarray(anss);\n}\n\n\nsigned main() {\n\tios::sync_with_stdio(false);\n\tcin.tie(0);\n\twhile (cin >> m >> n >> s >> w, m + n)\n\t\tsolve();\n\treturn 0;\n}", "accuracy": 1, "time_ms": 1510, "memory_kb": 26936, "score_of_the_acc": -0.5104, "final_rank": 4 }, { "submission_id": "aoj_1340_6148183", "code_snippet": "#pragma GCC optimize(\"O3\")\n#pragma GCC optimize(\"unroll-loops\")\n#include<iostream>\n#include<string>\n#include<cstdio>\n#include<vector>\n#include<cmath>\n#include<algorithm>\n#include<functional>\n#include<iomanip>\n#include<queue>\n#include<ciso646>\n#include<random>\n#include<map>\n#include<set>\n#include<bitset>\n#include<stack>\n#include<unordered_map>\n#include<unordered_set>\n#include<utility>\n#include<cassert>\n#include<complex>\n#include<numeric>\n#include<array>\n#include<chrono>\nusing namespace std;\n\n//#define int long long\ntypedef long long ll;\n\ntypedef unsigned long long ul;\ntypedef unsigned int ui;\nconstexpr ll mod = 998244353;\nconst ll INF = mod * mod;\ntypedef pair<int, int>P;\n\n#define rep(i,n) for(int i=0;i<n;i++)\n#define per(i,n) for(int i=n-1;i>=0;i--)\n#define Rep(i,sta,n) for(int i=sta;i<n;i++)\n#define rep1(i,n) for(int i=1;i<=n;i++)\n#define per1(i,n) for(int i=n;i>=1;i--)\n#define Rep1(i,sta,n) for(int i=sta;i<=n;i++)\n#define all(v) (v).begin(),(v).end()\ntypedef pair<ll, ll> LP;\ntypedef double ld;\ntypedef pair<ld, ld> LDP;\nconst ld eps = 1e-8;\nconst ld pi = acosl(-1.0);\n\ntemplate<typename T>\nvoid chmin(T& a, T b) {\n\ta = min(a, b);\n}\ntemplate<typename T>\nvoid chmax(T& a, T b) {\n\ta = max(a, b);\n}\ntemplate<typename T>\nvoid cinarray(vector<T>& v) {\n\trep(i, v.size())cin >> v[i];\n}\ntemplate<typename T>\nvoid coutarray(vector<T>& v) {\n\trep(i, v.size()) {\n\t\tif (i > 0)cout << \" \"; cout << v[i];\n\t}\n\tcout << \"\\n\";\n}\nll mod_pow(ll x, ll n, ll m = mod) {\n\tif (n < 0) {\n\t\tll res = mod_pow(x, -n, m);\n\t\treturn mod_pow(res, m - 2, m);\n\t}\n\tif (abs(x) >= m)x %= m;\n\tif (x < 0)x += m;\n\tif (x == 0)return 0;\n\tll res = 1;\n\twhile (n) {\n\t\tif (n & 1)res = res * x % m;\n\t\tx = x * x % m; n >>= 1;\n\t}\n\treturn res;\n}\nstruct modint {\n\tint n;\n\tmodint() :n(0) { ; }\n\tmodint(ll m) {\n\t\tif (m < 0 || mod <= m) {\n\t\t\tm %= mod; if (m < 0)m += mod;\n\t\t}\n\t\tn = m;\n\t}\n\toperator int() { return n; }\n};\nbool operator==(modint a, modint b) { return a.n == b.n; }\nmodint operator+=(modint& a, modint b) { a.n += b.n; if (a.n >= mod)a.n -= mod; return a; }\nmodint operator-=(modint& a, modint b) { a.n -= b.n; if (a.n < 0)a.n += mod; return a; }\nmodint operator*=(modint& a, modint b) { a.n = ((ll)a.n * b.n) % mod; return a; }\nmodint operator+(modint a, modint b) { return a += b; }\nmodint operator-(modint a, modint b) { return a -= b; }\nmodint operator*(modint a, modint b) { return a *= b; }\nmodint operator^(modint a, ll n) {\n\tif (n == 0)return modint(1);\n\tmodint res = (a * a) ^ (n / 2);\n\tif (n % 2)res = res * a;\n\treturn res;\n}\n\nll inv(ll a, ll p) {\n\treturn (a == 1 ? 1 : (1 - p * inv(p % a, a)) / a + p);\n}\nmodint operator/(modint a, modint b) { return a * modint(inv(b, mod)); }\nmodint operator/=(modint& a, modint b) { a = a / b; return a; }\nconst int max_n = 1 << 20;\nmodint fact[max_n], factinv[max_n];\nvoid init_f() {\n\tfact[0] = modint(1);\n\tfor (int i = 0; i < max_n - 1; i++) {\n\t\tfact[i + 1] = fact[i] * modint(i + 1);\n\t}\n\tfactinv[max_n - 1] = modint(1) / fact[max_n - 1];\n\tfor (int i = max_n - 2; i >= 0; i--) {\n\t\tfactinv[i] = factinv[i + 1] * modint(i + 1);\n\t}\n}\nmodint comb(int a, int b) {\n\tif (a < 0 || b < 0 || a < b)return 0;\n\treturn fact[a] * factinv[b] * factinv[a - b];\n}\nmodint combP(int a, int b) {\n\tif (a < 0 || b < 0 || a < b)return 0;\n\treturn fact[a] * factinv[a - b];\n}\n\nll gcd(ll a, ll b) {\n\ta = abs(a); b = abs(b);\n\tif (a < b)swap(a, b);\n\twhile (b) {\n\t\tll r = a % b; a = b; b = r;\n\t}\n\treturn a;\n}\n\nint dx[4] = { 1,0,-1,0 };\nint dy[4] = { 0,1,0,-1 };\n\n\n\n\nint m, n, s, w;\nvoid solve() {\n\tvector<vector<int>> v(m + n);\n\trep(i, m) {\n\t\tv[i].resize(3);\n\t\trep(j, 3)cin >> v[i][j];\n\t}\n\tint g = s;\n\trep(i, n) {\n\t\tv[i + m].resize(3);\n\t\tv[i + m][0] = (g / 7) % 100 + 1;\n\t\tv[i + m][1] = (g / 700) % 100 + 1;\n\t\tv[i + m][2] = (g / 70000) % 100 + 1;\n\t\tif (g % 2 == 0)g /= 2;\n\t\telse g = (g / 2) ^ w;\n\t}\n\t//erase same vector\n\t{\n\t\tvector<vector<int>> nv;\n\t\tset<vector<int>> st;\n\t\trep(i, m + n) {\n\t\t\tint g = 0;\n\t\t\trep(j, 3)g = gcd(g, v[i][j]);\n\t\t\tvector<int> cur(3);\n\t\t\trep(j, 3)cur[j] = v[i][j] / g;\n\t\t\tif (!st.count(cur)) {\n\t\t\t\tnv.push_back(v[i]);\n\t\t\t\tst.insert(cur);\n\t\t\t}\n\t\t}\n\t\tswap(v, nv);\n\t\tn = v.size();\n\t\tsort(all(v));\n\t}\n\tassert(n >= 2);\n\tvector<pair<LDP, int>> vtp;\n\tvector<ld> rr(n);\n\trep(i, n) {\n\t\trr[i] = sqrt(v[i][0] * v[i][0] + v[i][1] * v[i][1] + v[i][2] * v[i][2]);\n\t\tvtp.push_back({ {v[i][0] / rr[i],v[i][1] / rr[i]},i });\n\t}\n\tsort(all(vtp));\n\tauto calc = [&](int i, int j) {\n\t\tld res = 0;\n\t\trep(k, 3)res += (v[i][k]/rr[i] - v[j][k]/rr[j]) * (v[i][k]/rr[i] - v[j][k]/rr[j]);\n\t\treturn res;\n\t};\n\tld ans = mod;\n\tP ansp = { -1,-1 };\n\tfunction<ld(int, int)> yaru = [&](int l, int r)->ld {\n\t\tld mi = mod;\n\t\tif (l + 1 == r)return mod;\n\t\tint m = (l + r) / 2;\n\t\tchmin(mi, yaru(l, m));\n\t\tchmin(mi, yaru(m, r));\n\t\tld res = mi;\n\t\tld t = vtp[m - 1].first.first;\n\t\tvector<pair<ld, int>> vl, vr;\n\t\tRep(i, l, m) {\n\t\t\tld dx = t - vtp[i].first.first;\n\t\t\tif (dx*dx > ans + (1e-8))continue;\n\t\t\tvl.push_back({ vtp[i].first.second,vtp[i].second });\n\t\t}\n\t\tRep(i, m, r) {\n\t\t\tld dx = t - vtp[i].first.first;\n\t\t\tif (dx * dx > ans + (1e-8))continue;\n\t\t\tvr.push_back({ vtp[i].first.second,vtp[i].second });\n\t\t}\n\t\tsort(all(vl));\n\t\tsort(all(vr));\n\t\t//cout << \"! \" << l << \" \" << r << \" \" << vl.size() << \" \" << vr.size() << \"\\n\";\n\t\tint locl = 0;\n\t\tint locr = 0;\n\t\t//[locl,locr)\n\t\trep(i, vl.size()) {\n\t\t\twhile (locl < vr.size() && vr[locl].first < vl[i].first) {\n\t\t\t\tld dy = vl[i].first - vr[locl].first;\n\t\t\t\tif (dy * dy > ans + (1e-8))locl++;\n\t\t\t\telse break;\n\t\t\t}\n\t\t\twhile (locr < vr.size()) {\n\t\t\t\tif (vr[locr].first < vl[i].first)locr++;\n\t\t\t\telse {\n\t\t\t\t\tld dy = vr[locr].first - vl[i].first;\n\t\t\t\t\tif (dy * dy > ans + (1e-8))break;\n\t\t\t\t\telse locr++;\n\t\t\t\t}\n\t\t\t}\n\t\t\tassert(locl <= locr);\n\t\t\tRep(j, locl, locr) {\n\t\t\t\tld val = calc(vl[i].second,vr[j].second);\n\t\t\t\tP p = minmax(vl[i].second, vr[j].second);\n\t\t\t\tif (abs(ans - val) < (1e-12)) {\n\t\t\t\t\tansp = min(ansp, p);\n\t\t\t\t}\n\t\t\t\telse if (ans > val) {\n\t\t\t\t\tans = val;\n\t\t\t\t\tansp = p;\n\t\t\t\t}\n\t\t\t\tres = min(res, val);\n\t\t\t}\n\t\t}\n\t\treturn res;\n\t};\n\tyaru(0, n);\n\tassert(ansp.first >= 0);\n\tvector<int> anss;\n\trep(j, 3) {\n\t\tanss.push_back(v[ansp.first][j]);\n\t}\n\trep(j, 3) {\n\t\tanss.push_back(v[ansp.second][j]);\n\t}\n\t//cout << \"ans is \";\n\tcoutarray(anss);\n\t/*int id1 = -1;\n\trep(i, v.size())if (v[i] == vector<int>{42, 40, 46})id1 = i;\n\tint id2 = -1;\n\trep(i, v.size())if (v[i] == vector<int>{83, 79, 91})id2 = i;\n\tcout << ans << \" \" << calc(id1,id2) << \"\\n\";\n\tcout << \"! \" << id1 << \" \" << id2 << \"\\n\";*/\n}\n\n\nsigned main() {\n\tios::sync_with_stdio(false);\n\tcin.tie(0);\n\tcout << fixed << setprecision(10);\n\t//init_f();\n\t//init();\n\t//while(true)\n\t//expr();\n\t//init();\n\t//int t; cin >> t; rep(i, t)\n\twhile (cin >> m >> n >> s >> w, m + n)\n\t\tsolve();\n\treturn 0;\n}", "accuracy": 1, "time_ms": 1510, "memory_kb": 35164, "score_of_the_acc": -0.6411, "final_rank": 5 }, { "submission_id": "aoj_1340_6148147", "code_snippet": "#pragma GCC optimize(\"O3\")\n#pragma GCC optimize(\"unroll-loops\")\n#include<iostream>\n#include<string>\n#include<cstdio>\n#include<vector>\n#include<cmath>\n#include<algorithm>\n#include<functional>\n#include<iomanip>\n#include<queue>\n#include<ciso646>\n#include<random>\n#include<map>\n#include<set>\n#include<bitset>\n#include<stack>\n#include<unordered_map>\n#include<unordered_set>\n#include<utility>\n#include<cassert>\n#include<complex>\n#include<numeric>\n#include<array>\n#include<chrono>\nusing namespace std;\n\n//#define int long long\ntypedef long long ll;\n\ntypedef unsigned long long ul;\ntypedef unsigned int ui;\nconstexpr ll mod = 998244353;\nconst ll INF = mod * mod;\ntypedef pair<int, int>P;\n\n#define rep(i,n) for(int i=0;i<n;i++)\n#define per(i,n) for(int i=n-1;i>=0;i--)\n#define Rep(i,sta,n) for(int i=sta;i<n;i++)\n#define rep1(i,n) for(int i=1;i<=n;i++)\n#define per1(i,n) for(int i=n;i>=1;i--)\n#define Rep1(i,sta,n) for(int i=sta;i<=n;i++)\n#define all(v) (v).begin(),(v).end()\ntypedef pair<ll, ll> LP;\ntypedef double ld;\ntypedef pair<ld, ld> LDP;\nconst ld eps = 1e-8;\nconst ld pi = acosl(-1.0);\n\ntemplate<typename T>\nvoid chmin(T& a, T b) {\n\ta = min(a, b);\n}\ntemplate<typename T>\nvoid chmax(T& a, T b) {\n\ta = max(a, b);\n}\ntemplate<typename T>\nvoid cinarray(vector<T>& v) {\n\trep(i, v.size())cin >> v[i];\n}\ntemplate<typename T>\nvoid coutarray(vector<T>& v) {\n\trep(i, v.size()) {\n\t\tif (i > 0)cout << \" \"; cout << v[i];\n\t}\n\tcout << \"\\n\";\n}\nll mod_pow(ll x, ll n, ll m = mod) {\n\tif (n < 0) {\n\t\tll res = mod_pow(x, -n, m);\n\t\treturn mod_pow(res, m - 2, m);\n\t}\n\tif (abs(x) >= m)x %= m;\n\tif (x < 0)x += m;\n\tif (x == 0)return 0;\n\tll res = 1;\n\twhile (n) {\n\t\tif (n & 1)res = res * x % m;\n\t\tx = x * x % m; n >>= 1;\n\t}\n\treturn res;\n}\nstruct modint {\n\tint n;\n\tmodint() :n(0) { ; }\n\tmodint(ll m) {\n\t\tif (m < 0 || mod <= m) {\n\t\t\tm %= mod; if (m < 0)m += mod;\n\t\t}\n\t\tn = m;\n\t}\n\toperator int() { return n; }\n};\nbool operator==(modint a, modint b) { return a.n == b.n; }\nmodint operator+=(modint& a, modint b) { a.n += b.n; if (a.n >= mod)a.n -= mod; return a; }\nmodint operator-=(modint& a, modint b) { a.n -= b.n; if (a.n < 0)a.n += mod; return a; }\nmodint operator*=(modint& a, modint b) { a.n = ((ll)a.n * b.n) % mod; return a; }\nmodint operator+(modint a, modint b) { return a += b; }\nmodint operator-(modint a, modint b) { return a -= b; }\nmodint operator*(modint a, modint b) { return a *= b; }\nmodint operator^(modint a, ll n) {\n\tif (n == 0)return modint(1);\n\tmodint res = (a * a) ^ (n / 2);\n\tif (n % 2)res = res * a;\n\treturn res;\n}\n\nll inv(ll a, ll p) {\n\treturn (a == 1 ? 1 : (1 - p * inv(p % a, a)) / a + p);\n}\nmodint operator/(modint a, modint b) { return a * modint(inv(b, mod)); }\nmodint operator/=(modint& a, modint b) { a = a / b; return a; }\nconst int max_n = 1 << 20;\nmodint fact[max_n], factinv[max_n];\nvoid init_f() {\n\tfact[0] = modint(1);\n\tfor (int i = 0; i < max_n - 1; i++) {\n\t\tfact[i + 1] = fact[i] * modint(i + 1);\n\t}\n\tfactinv[max_n - 1] = modint(1) / fact[max_n - 1];\n\tfor (int i = max_n - 2; i >= 0; i--) {\n\t\tfactinv[i] = factinv[i + 1] * modint(i + 1);\n\t}\n}\nmodint comb(int a, int b) {\n\tif (a < 0 || b < 0 || a < b)return 0;\n\treturn fact[a] * factinv[b] * factinv[a - b];\n}\nmodint combP(int a, int b) {\n\tif (a < 0 || b < 0 || a < b)return 0;\n\treturn fact[a] * factinv[a - b];\n}\n\nll gcd(ll a, ll b) {\n\ta = abs(a); b = abs(b);\n\tif (a < b)swap(a, b);\n\twhile (b) {\n\t\tll r = a % b; a = b; b = r;\n\t}\n\treturn a;\n}\n\nint dx[4] = { 1,0,-1,0 };\nint dy[4] = { 0,1,0,-1 };\n\n\n\n\nint m, n, s, w;\nvoid solve() {\n\tvector<vector<int>> v(m + n);\n\trep(i, m) {\n\t\tv[i].resize(3);\n\t\trep(j, 3)cin >> v[i][j];\n\t}\n\tint g = s;\n\trep(i, n) {\n\t\tv[i + m].resize(3);\n\t\tv[i + m][0] = (g / 7) % 100 + 1;\n\t\tv[i + m][1] = (g / 700) % 100 + 1;\n\t\tv[i + m][2] = (g / 70000) % 100 + 1;\n\t\tif (g % 2 == 0)g /= 2;\n\t\telse g = (g / 2) ^ w;\n\t}\n\t//erase same vector\n\t{\n\t\tvector<vector<int>> nv;\n\t\tset<vector<int>> st;\n\t\trep(i, m + n) {\n\t\t\tint g = 0;\n\t\t\trep(j, 3)g = gcd(g, v[i][j]);\n\t\t\tvector<int> cur(3);\n\t\t\trep(j, 3)cur[j] = v[i][j] / g;\n\t\t\tif (!st.count(cur)) {\n\t\t\t\tnv.push_back(v[i]);\n\t\t\t\tst.insert(cur);\n\t\t\t}\n\t\t}\n\t\tswap(v, nv);\n\t\tn = v.size();\n\t\tsort(all(v));\n\t}\n\tassert(n >= 2);\n\t//rep(i, n)coutarray(v[i]);\n\tvector<LDP> vt(n);\n\trep(i, n) {\n\t\tvt[i] = { atan2l(v[i][1],v[i][0]),atan2l(v[i][2],sqrt(v[i][0] * v[i][0] + v[i][1] * v[i][1])) };\n\t}\n\tvector<pair<LDP, int>> vtp;\n\trep(i, n) {\n\t\tvtp.push_back({ vt[i],i });\n\t}\n\tsort(all(vtp));\n\tauto trans = [&](LDP a, ld& x, ld& y, ld& z) {\n\t\tz = sin(a.second);\n\t\tld xy = cos(a.second);\n\t\tx = xy * cos(a.first);\n\t\ty = xy * sin(a.first);\n\t};\n\tauto calc = [&](LDP a, LDP b) {\n\t\tld x1, y1, z1, x2, y2, z2;\n\t\ttrans(a, x1, y1, z1);\n\t\ttrans(b, x2, y2, z2);\n\t\treturn (x1 * x2 + y1 * y2 + z1 * z2) / sqrt(x1 * x1 + y1 * y1 + z1 * z1) / sqrt(x2 * x2 + y2 * y2 + z2 * z2);\n\t};\n\tld ans = 0;\n\tP ansp = { -1,-1 };\n\tfunction<ld(int, int)> yaru = [&](int l, int r)->ld {\n\t\tld ma = 0;\n\t\tif (l + 1 == r)return 0;\n\t\tint m = (l + r) / 2;\n\t\tchmax(ma, yaru(l, m));\n\t\tchmax(ma, yaru(m, r));\n\t\tld res = ma;\n\t\tld t = vt[m - 1].first;\n\t\tvector<pair<ld, int>> vl, vr;\n\t\tRep(i, l, m) {\n\t\t\tLDP mirr = { t,vt[i].second };\n\t\t\tif (calc(mirr, vt[i]) < ma - (1e-0))continue;\n\t\t\t//if (l == 0 &&r == n)cout << \"!! \" << i << \"\\n\";\n\t\t\tvl.push_back({ vt[i].second,i });\n\t\t}\n\t\tRep(i, m, r) {\n\t\t\tLDP mirr = { t,vt[i].second };\n\t\t\tif (calc(mirr, vt[i]) < ma - (1e-0))continue;\n\t\t\tvr.push_back({ vt[i].second,i });\n\t\t\t//if (l == 0 && r == n)cout << \"!! \" << i << \"\\n\";\n\t\t}\n\t\tsort(all(vl));\n\t\tsort(all(vr));\n\t\t//cout << \"! \" << l << \" \" << r << \" \" << vl.size() << \" \" << vr.size() << \"\\n\";\n\t\tint locl = 0;\n\t\tint locr = 0;\n\t\t//[locl,locr)\n\t\trep(i, vl.size()) {\n\t\t\tLDP cur = vt[vl[i].second];\n\t\t\twhile (locl < vr.size() && vr[locl].first < vl[i].first) {\n\t\t\t\tLDP mirr = cur; mirr.second = vr[locl].first;\n\t\t\t\tif (calc(mirr, cur) < ma - (1e-8))locl++;\n\t\t\t\telse break;\n\t\t\t}\n\t\t\twhile (locr < vr.size()) {\n\t\t\t\tif (vr[locr].first < vl[i].first)locr++;\n\t\t\t\telse {\n\t\t\t\t\tLDP mirr = cur; mirr.second = vr[locr].first;\n\t\t\t\t\tif (calc(mirr, cur) < ma - (1e-8))break;\n\t\t\t\t\telse locr++;\n\t\t\t\t}\n\t\t\t}\n\t\t\tassert(locl <= locr);\n\t\t\tRep(j, locl, locr) {\n\t\t\t\tld val = calc(cur, vt[vr[j].second]);\n\t\t\t\tP p = { vl[i].second,vr[j].second };\n\t\t\t\tif (p.first > p.second)swap(p.first, p.second);\n\t\t\t\t//cout << \"!! \" << p.first << \" \" << p.second << \"\\n\";\n\t\t\t\tif (abs(ans - val) < (1e-12)) {\n\t\t\t\t\tansp = min(ansp, p);\n\t\t\t\t}\n\t\t\t\telse if (ans < val) {\n\t\t\t\t\tans = val;\n\t\t\t\t\tansp = p;\n\t\t\t\t}\n\t\t\t\tres = max(res, val);\n\t\t\t}\n\t\t}\n\t\treturn res;\n\t};\n\tyaru(0, n);\n\tassert(ansp.first >= 0);\n\tvector<int> anss;\n\trep(j, 3) {\n\t\tanss.push_back(v[ansp.first][j]);\n\t}\n\trep(j, 3) {\n\t\tanss.push_back(v[ansp.second][j]);\n\t}\n\t//cout << \"ans is \";\n\tcoutarray(anss);\n\t/*int id1 = -1;\n\trep(i, v.size())if (v[i] == vector<int>{42, 40, 46})id1 = i;\n\tint id2 = -1;\n\trep(i, v.size())if (v[i] == vector<int>{83, 79, 91})id2 = i;*/\n\t//cout << ans << \" \" << calc(vt[id1], vt[id2]) << \"\\n\";\n\t//cout << \"! \" << id1 << \" \" << id2 << \"\\n\";\n}\n\n\nsigned main() {\n\tios::sync_with_stdio(false);\n\tcin.tie(0);\n\tcout << fixed << setprecision(10);\n\t//init_f();\n\t//init();\n\t//while(true)\n\t//expr();\n\t//init();\n\t//int t; cin >> t; rep(i, t)\n\twhile (cin >> m >> n >> s >> w, m + n)\n\t\tsolve();\n\treturn 0;\n}", "accuracy": 1, "time_ms": 5280, "memory_kb": 35448, "score_of_the_acc": -1.1503, "final_rank": 13 }, { "submission_id": "aoj_1340_3707121", "code_snippet": "#include<bits/stdc++.h>\ntypedef long long int ll;\ntypedef unsigned long long int ull;\n#define BIG_NUM 2000000000\n#define HUGE_NUM 99999999999999999\n#define MOD 1000000007\n//#define EPS 0.000000001\nusing namespace std;\n\n#define EPS 0.0000001\n#define NUM 120005\n\nstruct Info{\n\n\tbool operator<(const struct Info &arg) const{\n\t\treturn angle < arg.angle;\n\t}\n\tll x,y,z;\n\tdouble angle;\n};\n\n\nint N;\nInfo info[NUM];\n\n\ndouble calc_angle(Info A,Info B){\n\n\tdouble bunshi = A.x*B.x+A.y*B.y+A.z*B.z;\n\tdouble bunbo = sqrt(A.x*A.x+A.y*A.y+A.z*A.z)*sqrt(B.x*B.x+B.y*B.y+B.z*B.z);\n\n\treturn acos(bunshi/bunbo);\n}\n\nbool compare(Info A,Info B){\n\n\tif(A.x != B.x){\n\n\t\treturn A.x < B.x;\n\n\t}else if(A.y != B.y){\n\n\t\treturn A.y < B.y;\n\n\t}else if(A.z != B.z){\n\n\t\treturn A.z < B.z;\n\t}\n\treturn -1;\n}\n\nbool isSame(Info A,Info B){\n\n\treturn A.x == B.x && A.y == B.y && A.z == B.z;\n}\n\nvoid func(){\n\n\n\tInfo base;\n\tbase.x = 1;\n\tbase.y = 1;\n\tbase.z = 1;\n\n\tfor(int i = 0; i < N; i++){\n\n\t\tinfo[i].angle = calc_angle(base,info[i]);\n\t}\n\n\tsort(info,info+N);\n\n\tInfo ans_L,ans_R;\n\n\tdouble minimum = acos(0);\n\tdouble tmp_angle;\n\n\tfor(int i = 0; i < N-1; i++){\n\t\tfor(int k = i+1; k < N; k++){\n\t\t\tif(fabs(info[i].angle - info[k].angle) > minimum) break;\n\n\t\t\ttmp_angle = calc_angle(info[i],info[k]);\n\t\t\tif(tmp_angle < EPS)continue;\n\n\t\t\tif(tmp_angle+EPS < minimum){\n\t\t\t\tminimum = tmp_angle;\n\n\t\t\t\tif(compare(info[i],info[k])){\n\n\t\t\t\t\tans_L = info[i];\n\t\t\t\t\tans_R = info[k];\n\n\t\t\t\t}else{\n\n\t\t\t\t\tans_L = info[k];\n\t\t\t\t\tans_R = info[i];\n\t\t\t\t}\n\n\t\t\t}else if(fabs(tmp_angle-minimum) < EPS){\n\n\t\t\t\tif(compare(info[i],info[k])){\n\n\t\t\t\t\tif((compare(info[i],ans_L) == true) || (isSame(info[i],ans_L) && compare(info[k],ans_R) == true)){\n\n\t\t\t\t\t\tans_L = info[i];\n\t\t\t\t\t\tans_R = info[k];\n\t\t\t\t\t}\n\n\t\t\t\t}else{\n\n\t\t\t\t\tif((compare(info[k],ans_L) == true) || (isSame(info[k],ans_L) && compare(info[i],ans_R) == true)){\n\n\t\t\t\t\t\tans_L = info[k];\n\t\t\t\t\t\tans_R = info[i];\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n\n\tprintf(\"%lld %lld %lld %lld %lld %lld\\n\",ans_L.x,ans_L.y,ans_L.z,ans_R.x,ans_R.y,ans_R.z);\n}\n\nint main(){\n\n\tll num_1,num_2,S,W;\n\n\twhile(true){\n\n\t\tscanf(\"%lld %lld %lld %lld\",&num_1,&num_2,&S,&W);\n\t\tif(num_1 == 0 && num_2 == 0 && S == 0 && W == 0)break;\n\n\t\tfor(int i = 0; i < num_1; i++){\n\n\t\t\tscanf(\"%lld %lld %lld\",&info[i].x,&info[i].y,&info[i].z);\n\t\t}\n\n\t\tll g = S;\n\n\t\tfor(int i = 0; i < num_2; i++){\n\t\t\tinfo[i+num_1].x = (g/7) %100 + 1;\n\t\t\tinfo[i+num_1].y = (g/700) %100 + 1;\n\t\t\tinfo[i+num_1].z = (g/70000)%100 + 1;\n\t\t\tif( g%2 == 0 ) { g = (g/2); }\n\t\t\telse { g = (g/2) ^ W; }\n\t\t}\n\n\t\tN = num_1+num_2;\n\n\t\tfunc();\n\t}\n\n\treturn 0;\n}", "accuracy": 1, "time_ms": 1010, "memory_kb": 6756, "score_of_the_acc": -0.123, "final_rank": 2 }, { "submission_id": "aoj_1340_3086248", "code_snippet": "#include <iostream>\n#include <vector>\n#include <algorithm>\n#include <cmath>\n#include <iomanip>\n#include <map>\nusing namespace std;\nconst double EPS = 1e-8;\nconst double INF = 1e18;\n\nbool epsleq(const double &a, const double &b){\n return a +EPS < b;\n}\nbool epseq(const double &a, const double &b){\n return abs(a-b) < EPS;\n}\n\nstruct Point{\n double x,y,z;\n Point(double x, double y, double z):x(x), y(y), z(z){}\n Point(){}\n Point &operator +=(const Point &a){ x+=a.x; y+=a.y; z+=a.z; return *this; }\n Point &operator -=(const Point &a){ x-=a.x; y-=a.y; z-=a.z; return *this; }\n Point &operator *=(const double &a){ x*=a; y*=a; z*=a; return *this; }\n Point operator +(const Point &a) const{ return Point(x+a.x, y+a.y, z+a.z); }\n Point operator -(const Point &a) const{ return Point(x-a.x, y-a.y, z-a.z); }\n Point operator *(const double &a) const{ return Point(a*x, a*y, a*z); }\n Point operator -() { return Point(-x, -y, -z); }\n bool operator <(const Point &a) const{\n return !epseq(x, a.x)? x < a.x: !epseq(y, a.y)? y < a.y: epsleq(z, a.z);\n }\n bool operator ==(const Point &a) const{\n double dx=x-a.x, dy=y-a.y, dz=z-a.z;\n return sqrt(dx*dx +dy*dy +dz*dz) < EPS;\n }\n};\n\ndouble dot(const Point &a, const Point &b){\n return a.x*b.x +a.y*b.y +a.z*b.z;\n}\nPoint cross(const Point &a, const Point &b){\n return Point(a.y*b.z -a.z*b.y, a.z*b.x -a.x*b.z, a.x*b.y -a.y*b.x);\n}\ndouble abs(const Point &a){\n return sqrt(dot(a, a));\n}\nPoint unit(const Point &a){\n return a *(1/abs(a));\n}\n\nbool ycomp(const Point &a, const Point &b){\n return !epseq(a.y, b.y)? a.y < b.y: !epseq(a.x, b.x)? a.x < b.x: epsleq(a.z, b.z);\n}\nbool dpcomp(const pair<double, pair<Point, Point> > &a, const pair<double, pair<Point, Point> > &b){\n return !epseq(a.first, b.first)? a.first < b.first : a.second < b.second;\n}\n\npair<double, pair<Point, Point> > closest(vector<Point> &v, int first, int last, map<Point, Point> &conv){\n if(last -first <= 1) return make_pair(INF, make_pair(Point(), Point()));\n int mid = (first +last)/2;\n double midx = v[mid].x;\n pair<double, pair<Point, Point> > cp = min(closest(v, first, mid, conv), closest(v, mid, last, conv), dpcomp);\n inplace_merge(v.begin()+first, v.begin()+mid, v.begin()+last, ycomp);\n \n vector<Point> near;\n for(int i=first; i<last; i++){\n if(abs(v[i].x -midx) > cp.first +EPS) continue;\n for(int j=near.size()-1; j>=0; j--){\n if(abs(v[i].y -near[j].y) > cp.first +EPS){\n break;\n }\n pair<double, pair<Point, Point> > ncp(abs(v[i] -near[j]), pair<Point, Point>(conv[v[i]], conv[near[j]]));\n if(ncp.second.second < ncp.second.first) swap(ncp.second.first, ncp.second.second);\n cp = min(cp, ncp, dpcomp);\n }\n near.push_back(v[i]);\n }\n return cp;\n}\n\nint main(){\n while(1){\n int m,n,s,w;\n cin >> m >> n >> s >> w;\n if(m==0 && n==0) break;\n\n vector<Point> v(m+n);\n for(int i=0; i<m; i++){\n cin >> v[i].x >> v[i].y >> v[i].z;\n }\n for(int i=m; i<m+n; i++){\n v[i].x = (s/7) %100 +1;\n v[i].y = (s/700) %100 +1;\n v[i].z = (s/70000) %100 +1;\n if(s%2 == 0){\n s = s/2;\n }else{\n s = (s/2) ^ w;\n }\n }\n\n n += m;\n map<Point, Point> conv;\n for(int i=0; i<n; i++){\n Point univec = unit(v[i]);\n if(conv.count(univec) == 0){\n conv[univec] = v[i];\n }else{\n conv[univec] = min(conv[univec], v[i]);\n }\n }\n\n vector<Point> uni;\n for(pair<Point, Point> pp: conv){\n uni.push_back(pp.first);\n }\n pair<double, pair<Point, Point> > ret = closest(uni, 0, uni.size(), conv);\n Point &a = ret.second.first;\n Point &b = ret.second.second;\n cout << fixed << setprecision(0);\n cout << a.x << \" \" << a.y << \" \" << a.z << \" \";\n cout << b.x << \" \" << b.y << \" \" << b.z << endl;\n }\n return 0;\n}", "accuracy": 1, "time_ms": 1750, "memory_kb": 19516, "score_of_the_acc": -0.4247, "final_rank": 3 }, { "submission_id": "aoj_1340_3060374", "code_snippet": "// AOJ 1340: Directional Resemblence\n#include <cmath>\n#include <cstdio>\n#include <algorithm>\nusing namespace std;\nint n, m, S, W, px[120009], py[120009], pz[120009], p[120009]; long double x[120009], y[120009], z[120009];\nint main() {\n\twhile(scanf(\"%d %d %d %d\", &n, &m, &S, &W), n + m != 0) {\n\t\tfor(int i = 0; i < n; ++i) {\n\t\t\tscanf(\"%d %d %d\", &px[i], &py[i], &pz[i]);\n\t\t}\n\t\tfor(int i = n; i < n + m; ++i) {\n\t\t\tpx[i] = S / 7 % 100 + 1;\n\t\t\tpy[i] = S / 700 % 100 + 1;\n\t\t\tpz[i] = S / 70000 % 100 + 1;\n\t\t\tif(S & 1) S = (S >> 1) ^ W;\n\t\t\telse S >>= 1;\n\t\t}\n\t\tn += m;\n\t\tfor(int i = 0; i < n; ++i) {\n\t\t\tlong double d = sqrt((long double)(px[i] * px[i] + py[i] * py[i] + pz[i] * pz[i]));\n\t\t\tx[i] = px[i] / d;\n\t\t\ty[i] = py[i] / d;\n\t\t\tz[i] = pz[i] / d;\n\t\t\tp[i] = i;\n\t\t}\n\t\tsort(p, p + n, [](int i, int j) {\n\t\t\tif(abs(x[i] - x[j]) > 1.0e-12L) return x[i] < x[j];\n\t\t\tif(abs(y[i] - y[j]) > 1.0e-12L) return y[i] < y[j];\n\t\t\tif(abs(z[i] - z[j]) > 1.0e-12L) return z[i] < z[j];\n\t\t\tif(px[i] != px[j]) return px[i] < px[j];\n\t\t\tif(py[i] != py[j]) return py[i] < py[j];\n\t\t\treturn pz[i] < pz[j];\n\t\t});\n\t\tlong double ans = 1.0e+9;\n\t\tint pa = -1, pb = -1, r = 0;\n\t\tfor(int i = 0; i < n; ++i) {\n\t\t\twhile(r != n && abs(x[p[i]] - x[p[r]]) < 1.0e-12L && abs(y[p[i]] - y[p[r]]) < 1.0e-12L && abs(z[p[i]] - z[p[r]]) < 1.0e-12) ++r;\n\t\t\tfor(int j = r; j < n && (x[p[j]] - x[p[i]]) * (x[p[j]] - x[p[i]]) < ans - 1.0e-12L; ++j) {\n\t\t\t\tlong double d = (x[p[i]] - x[p[j]]) * (x[p[i]] - x[p[j]]) + (y[p[i]] - y[p[j]]) * (y[p[i]] - y[p[j]]) + (z[p[i]] - z[p[j]]) * (z[p[i]] - z[p[j]]);\n\t\t\t\tif(d < ans - 1.0e-16L) {\n\t\t\t\t\tans = d;\n\t\t\t\t\tpa = p[i];\n\t\t\t\t\tpb = p[j];\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\tint ax = px[pa], ay = py[pa], az = pz[pa], bx = px[pb], by = py[pb], bz = pz[pb];\n\t\tif(ax > bx || (ax == bx && (ay > by || (ay == by && az > bz)))) {\n\t\t\tswap(ax, bx);\n\t\t\tswap(ay, by);\n\t\t\tswap(az, bz);\n\t\t}\n\t\tprintf(\"%d %d %d %d %d %d\\n\", ax, ay, az, bx, by, bz);\n\t}\n\treturn 0;\n}", "accuracy": 1, "time_ms": 350, "memory_kb": 9028, "score_of_the_acc": -0.0708, "final_rank": 1 }, { "submission_id": "aoj_1340_2740822", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\nusing ll=long long;\n#define rep(i,n) for(int i=0;i<n;i++)\n#define all(a) a.begin(),a.end()\n\nconst double EPS=1E-15;\nconstexpr int gcd(int a,int b){return b?gcd(b,a%b):a;}\n\nstruct v{\n\tint x,y,z,g;\n\tdouble L2,nx,ny,nz;\n\tbool operator==(const v&r)const{return this->x==r.x and this->y==r.y and this->z==r.z and this->g==r.g;};\n\tvoid setData(){\n\t g=gcd(x,gcd(y,z));\n L2=sqrt(x*x+y*y+z*z)/g;\n nx=x/g/L2;\n ny=y/g/L2;\n nz=z/g/L2;\n\t}\n\tfriend ostream& operator<<(ostream&os,const v&p);\n};\n\nbool operator<(const v&l,const v&r){return l.nx<r.nx or (l.nx==r.nx and (l.ny<r.ny or (l.ny==r.ny and l.nz<r.nz)));};\nostream& operator<<(ostream&os, const v&p){os<<p.x<<\" \"<<p.y<<\" \"<<p.z; return os;};\n\nv makeData(int X, int Y, int Z){\n\tv ret;\n\tret.x=X, ret.y=Y, ret.z=Z;\n\tret.setData();\n\treturn ret;\n}\n\nbool same(v a,v b){\n return a.x/a.g==b.x/b.g and a.y/a.g==b.y/b.g and a.z/a.g==b.z/b.g;\n}\n\nbool cmp(v a,v b){\n\treturn a.x<b.x or (b.x==a.x and (a.y<b.y or (b.y==a.y and a.z<b.z)));\n}\n\ndouble dist(v a, v b){\n if(same(a,b)) return 10000; \n return sqrt((b.nx-a.nx)*(b.nx-a.nx) + (b.ny-a.ny)*(b.ny-a.ny) + (b.nz-a.nz)*(b.nz-a.nz));\n}\n\n\nusing data=pair<double,pair<v,v>>;\ndata closest_pair(vector<v> vec){\n if(vec.size()<=1) return {10000,{v(),v()}};\n if(vec.size()==2){\n\t\tif(cmp(vec[1],vec[0])){\n\t\t\t//cout<<\"Ret (2) :\"<<vec[1]<<\" \"<<vec[0]<<\" = \"<<dist(vec[0],vec[1])<<endl;\n\t\t\treturn {dist(vec[0],vec[1]),{vec[1],vec[0]}};\n\t\t}else{\n\t\t\t//cout<<\"Ret (2) :\"<<vec[0]<<\" \"<<vec[1]<<\" = \"<<dist(vec[0],vec[1])<<endl;\n\t\t\treturn {dist(vec[0],vec[1]),{vec[0],vec[1]}};\n\t\t}\n\t}\n vector<v> vs1,vs2,vs3;\n int index=vec.size()/2;\n double sep=vec[index].nx;\n copy(vec.begin(), vec.begin()+index, back_inserter(vs1));\n copy(vec.begin()+index, vec.end(), back_inserter(vs2));\n \n\tdata s1=closest_pair(vs1), s2=closest_pair(vs2), minDist;\n\tif(fabs(s1.first-s2.first)>EPS){\n\t\tminDist=min(s1,s2);\n\t}else{\n\t\tif(cmp(s1.second.first,s2.second.first) or (s1.second.first==s2.second.first and cmp(s1.second.second,s2.second.second))){\n\t\t\tminDist=s1;\n\t\t}else{\n\t\t\tminDist=s2;\n\t\t}\n\t}\n \n sort(all(vec),[](v a,v b){return a.ny<b.ny;});\n \n for(auto d:vec){\n if(fabs(d.nx-sep)>=minDist.first) continue;\n for(int i=vs3.size()-1;i>=0;i--){\n double dst=dist(vs3[i],d);\n\t\t\t//if(dst>minDist.first)break;\n\t\t\tv n1=vs3[i],n2=d;\n\t\t\tif(cmp(n2,n1))swap(n1,n2);\n\t\t\tif(fabs(minDist.first-dst)<EPS){\n\t\t\t\tif(cmp(n1,minDist.second.first) or (n1==minDist.second.first and cmp(n2,minDist.second.second))){\n\t\t\t\t\t//cout<<\" \"<<n1<<\" \"<<n2<<endl;\n\t\t\t\t\tminDist={dst,{n1,n2}};\n\t\t\t\t}\n\t\t\t}else if(dst<minDist.first){\n\t\t\t\t//cout<<\" \"<<n1<<\" \"<<n2<<endl;\n minDist={dst,{n1,n2}};\n }\n }\n vs3.push_back(d);\n }\n\t//cout<<\"Ret (\"<<vec.size()<<\") :\"<<minDist.second.first<<\" \"<<minDist.second.second<<\" = \"<<minDist.first<<endl;\n return minDist;\n \n}\n\nint main(){\n\tios::sync_with_stdio(false);\n\tcin.tie(0);\n\t\n\tint m,n,s,w;\n\twhile(cin>>m>>n>>s>>w, m or n){\n\t\tvector<v> vec;\n\t\trep(i,m){\n\t\t\tint x,y,z;\n\t\t\tcin>>x>>y>>z;\n\t\t\tvec.push_back(makeData(x,y,z));\n\t\t}\n\t\tint g=s;\n\t\trep(i,n) {\n\t\t\tvec.push_back(makeData((g/7)%100+1, (g/700)%100+1, (g/70000)%100+1));\n\t\t\tif(g%2==0)g/=2;\n\t\t\telse g=(g/2) xor w;\n\t\t}\n\t sort(all(vec),[](v a,v b){return a.nx<b.nx;});\n\t\tvec.erase(unique(all(vec),same),vec.end());\n\t\t//for(auto d:vec)cout<<d<<\"(\"<<d.nx<<\")\"<<endl;\n\t\t//cout<<\"====\"<<endl;\n\t data ans=closest_pair(vec);\n\t v a1=ans.second.first;\n\t v a2=ans.second.second;\n\t if(cmp(a2,a1)) swap(a1,a2);\n\t cout<<a1<<\" \"<<a2<<endl;\n\t}\n\treturn 0;\n}", "accuracy": 1, "time_ms": 3860, "memory_kb": 25824, "score_of_the_acc": -0.8074, "final_rank": 8 }, { "submission_id": "aoj_1340_2643757", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nusing ld = long double;\n#define rep(i, j) for(int i=0; i < (int)(j); i++)\n#define all(v) v.begin(),v.end()\nconst ld EPS = 1e-8;\nbool eq(ld a, ld b) { return abs(a - b) < EPS; }\n\ntemplate<class T>\nstruct Point {\n T x, y, z;\n bool operator < (const Point<T> &r) const { return make_tuple(x, y, z) < make_tuple(r.x, r.y, r.z); }\n};\n\ntemplate<class T>\nld length(const Point<T> &p) {\n return sqrtl(p.x * p.x + p.y * p.y + p.z * p.z);\n}\ntemplate<class T>\nld dot(const Point<T> &a, const Point<T> &b) {\n return a.x * b.x + a.y * b.y + a.z * b.z;\n}\ntemplate<class T>\nld angle(const Point<T> &a, const Point<T> &b) {\n return acosl(dot(a, b) / length(a) / length(b));\n}\ntemplate<class T>\nPoint<ld> unit(Point<T> a) {\n Point<ld> p = {(ld)a.x, (ld)a.y, (ld)a.z};\n ld l = length(p);\n p.x /= l;\n p.y /= l;\n p.z /= l;\n return p;\n}\n\nbool solve() {\n using PI = Point<int>;\n int m, n, S, W; cin >> m >> n >> S >> W;\n if(m == 0 and n == 0 and S == 0 and W == 0) return false;\n vector<PI> V(m + n);\n rep(i, m) cin >> V[i].x >> V[i].y >> V[i].z;\n { // generate\n int g = S;\n for(int i=m; i<m+n; i++) {\n V[i].x = (g/7) %100 + 1;\n V[i].y = (g/700) %100 + 1;\n V[i].z = (g/70000)%100 + 1;\n if( g%2 == 0 ) { g = (g/2); }\n else { g = (g/2) ^ W; }\n }\n }\n sort(all(V), [&] (PI a, PI b) { return unit(a).x > unit(b).x; });\n \n ld min_angle = 1e9; \n PI a, b;\n\n auto need_check = [&] (PI &a, PI &b) {\n ld alpha = acosl(unit(a).x);\n return cosl(alpha + min_angle) < unit(b).x + EPS;\n };\n \n queue<PI> que;\n rep(i, V.size()) {\n queue<PI> nxt_que;\n while(que.size()) {\n auto p = que.front(); que.pop();\n if(not need_check(p, V[i])) continue;\n ld ang = angle(V[i], p);\n if(ang > EPS) {\n if(min_angle + EPS > ang) { \n auto va = V[i], vb = p;\n if(vb < va) swap(va, vb);\n if(!eq(min_angle, ang) or make_pair(a, b) > make_pair(va, vb)) {\n a = va;\n b = vb;\n }\n min_angle = ang;\n }\n }\n nxt_que.push(p);\n }\n nxt_que.push(V[i]);\n swap(que, nxt_que);\n }\n printf(\"%d %d %d %d %d %d\\n\", a.x, a.y, a.z, b.x, b.y, b.z);\n return true;\n}\n\nint main() {\n while(solve());\n return 0;\n}", "accuracy": 1, "time_ms": 5290, "memory_kb": 4748, "score_of_the_acc": -0.6641, "final_rank": 7 }, { "submission_id": "aoj_1340_2643742", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\nusing pii = pair<int,int>;\nusing ll = long long;\nusing ld = long double;\n#define rep(i, j) for(int i=0; i < (int)(j); i++)\n#define repeat(i, j, k) for(int i = (j); i < (int)(k); i++)\n#define all(v) v.begin(),v.end()\n#define debug(x) cerr << #x << \" : \" << x << endl\n\ntemplate<class T> bool set_min(T &a, const T &b) { return a > b ? a = b, true : false; }\ntemplate<class T> bool set_max(T &a, const T &b) { return a < b ? a = b, true : false; }\n// vector\ntemplate<class T> istream&\noperator >> (istream &is , vector<T> &v) { for(T &a : v) is >> a; return is; }\ntemplate<class T> ostream&\noperator << (ostream &os , const vector<T> &v) { for(const T &t : v) os << \"\\t\" << t; return os << endl; }\n// pair\ntemplate<class T, class U> ostream&\noperator << (ostream &os , const pair<T, U> &v) { return os << \"<\" << v.first << \", \" << v.second << \">\"; }\n\nconst int INF = 1 << 30;\nconst ll INFL = 1LL << 60;\nconst ld EPS = 1e-9;\nbool eq(ld a, ld b) { return abs(a - b) < EPS; }\ntemplate<class T>\nstruct Point {\n T x, y, z;\n bool operator < (const Point<T> &r) const {\n return make_tuple(x, y, z) < make_tuple(r.x, r.y, r.z);\n }\n};\ntemplate<class T>\nld length(const Point<T> &p) { return sqrtl(p.x * p.x + p.y * p.y + p.z * p.z); }\ntemplate<class T>\nld dot(const Point<T> &a, const Point<T> &b) { return a.x * b.x + a.y * b.y + a.z * b.z; }\ntemplate<class T>\nld angle(const Point<T> &a, const Point<T> &b) {\n return acosl(dot(a, b) / length(a) / length(b));\n}\ntemplate<class T>\nPoint<ld> unit(Point<T> a) {\n Point<ld> p = {(ld)a.x, (ld)a.y, (ld)a.z};\n ld l = length(p);\n p.x /= l;\n p.y /= l;\n p.z /= l;\n return p;\n}\n\nbool solve() {\n int m, n, S, W; cin >> m >> n >> S >> W;\n if(m == 0 and n == 0 and S == 0 and W == 0) return false;\n vector<Point<int>> VI(m + n);\n rep(i, m) {\n cin >> VI[i].x >> VI[i].y >> VI[i].z;\n }\n int g = S;\n for(int i=m; i<m+n; i++) {\n VI[i].x = (g/7) %100 + 1;\n VI[i].y = (g/700) %100 + 1;\n VI[i].z = (g/70000)%100 + 1;\n if( g%2 == 0 ) { g = (g/2); }\n else { g = (g/2) ^ W; }\n }\n sort(all(VI), [&] (Point<int> a, Point<int> b) {\n return unit(a).x > unit(b).x;\n });\n ld min_angle = 1e9; \n Point<int> a, b;\n\n auto need_check = [&] (Point<int> &a, Point<int> &b) {\n ld alpha = acosl(unit(a).x);\n auto ss = cosl(alpha + min_angle);\n auto tt = unit(b).x;\n // cerr << ss << \" \" << tt << endl;\n return ss < tt + EPS*10;\n };\n \n queue<Point<int>> que;\n rep(i, VI.size()) {\n queue<Point<int>> nxt_que;\n while(que.size()) {\n auto p = que.front(); que.pop();\n if(not need_check(p, VI[i])) continue;\n ld ang = angle(VI[i], p);\n if(ang > EPS) {\n if(min_angle + EPS > ang) { \n auto aa = VI[i], bb = p;\n if(bb < aa) swap(aa, bb);\n if(!eq(min_angle, ang) or make_pair(a, b) > make_pair(aa, bb)) {\n a = aa;\n b = bb;\n }\n min_angle = ang;\n }\n }\n nxt_que.push(p);\n }\n nxt_que.push(VI[i]);\n swap(que, nxt_que);\n }\n printf(\"%d %d %d %d %d %d\\n\",\n a.x, a.y, a.z,\n b.x, b.y, b.z);\n return true;\n}\n\nint main() {\n while(solve());\n return 0;\n}", "accuracy": 1, "time_ms": 5310, "memory_kb": 4572, "score_of_the_acc": -0.664, "final_rank": 6 }, { "submission_id": "aoj_1340_2047412", "code_snippet": "#include \"bits/stdc++.h\"\nusing namespace std;\nusing ld = long double;\nconst ld eps = 1e-9;\n\nld getdis(vector<ld>&l, vector<ld>&r) {\n\tld sum = 0;\n\tfor (int i = 0; i < 3; ++i) {\n\t\tsum += (l[i] - r[i])*(l[i] - r[i]);\n\t}\n\treturn sqrt(sum);\n}\n\nstruct po {\n\tint index;\n\tvector<ld> coors;\n\tpo(int _d) :coors(_d) {\n\t\tindex = -1;\n\t}\n\tpo(int _index, vector<ld>_coors) :index(_index), coors(_coors) {\n\n\t}\n\tpo() {}\n};\nld getdis(po&lpo, po&rpo) {\n\treturn getdis(lpo.coors, rpo.coors);\n}\ntemplate<class T>\nclass axisSorter {\n\tint k;\npublic:\n\taxisSorter(int _k) : k(_k) {}\n\tbool operator()(const T &a, const T &b) {\n\t\treturn a.coors[k] < b.coors[k];\n\t}\n};\n\n\n\nstruct Ans {\n\tld dis;\n\tpair<int, int>p;\n\n};\n\n\nbool operator<(const Ans&l, const Ans&r) {\n\tif (abs(l.dis - r.dis) < eps) {\n\t\tassert(l.p.first < l.p.second);\n\t\tassert(r.p.first < r.p.second);\n\t\treturn l.p < r.p;\n\t}\n\telse {\n\t\treturn l.dis < r.dis;\n\t}\n\treturn true;\n}\nAns answer;\n\ntemplate<class T, int Dim = 3>\nstruct kdtree {\npublic:\n\tT midpo;\n\tvector<T>pos;\n\tshared_ptr<kdtree<T>> ltree, rtree;\n\tint depth;\n\tint axis;\n\tkdtree(const T &p_) :midpo(p_), ltree(), rtree() {\n\n\t}\n\t~kdtree() {\n\t}\n\n\tkdtree(vector<T>&ps_, const int& l, const int& r, const int depth_ = 0) : ltree(nullptr), rtree(nullptr), depth(depth_), axis(depth%Dim) {\n\t\tinit(ps_, l, r);\n\t}\n\tvoid getmindis() {\n\t\tvector<po>lpos, rpos;\n\t\tld axisvalue = midpo.coors[axis];\n\n\t\tif (ltree != nullptr) {\n\t\t\tfor (auto &po : ltree->pos) {\n\t\t\t\tif (po.coors[axis]>axisvalue - sqrt(answer.dis) - eps * 10) {\n\t\t\t\t\tlpos.emplace_back(po);\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\tif (rtree != nullptr) {\n\t\t\tfor (auto &po : rtree->pos) {\n\t\t\t\tif (po.coors[axis]<axisvalue + sqrt(answer.dis) + eps * 10) {\n\t\t\t\t\trpos.emplace_back(po);\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\n\t\tfor (auto& lpo : lpos) {\n\t\t\tfor (auto& rpo : rpos) {\n\t\t\t\tld dis = getdis(lpo, rpo);\n\t\t\t\tpair<int, int> p(rpo.index, lpo.index);\n\t\t\t\tif (p.first > p.second)swap(p.first, p.second);\n\t\t\t\tAns nans{ dis,p };\n\t\t\t\tanswer = min(answer, nans);\n\t\t\t}\n\t\t\t{\n\t\t\t\tld dis = getdis(lpo, midpo);\n\t\t\t\tpair<int, int> p(midpo.index, lpo.index);\n\t\t\t\tif (p.first > p.second)swap(p.first, p.second);\n\t\t\t\tAns nans{ dis,p };\n\t\t\t\tanswer = min(answer, nans);\n\t\t\t}\n\t\t}\n\t\tfor (auto& rpo : rpos) {\n\t\t\tld dis = getdis(midpo, rpo);\n\t\t\tpair<int, int> p(midpo.index, rpo.index);\n\t\t\tif (p.first > p.second)swap(p.first, p.second);\n\t\t\tAns nans{ dis,p };\n\t\t\tanswer = min(answer, nans);\n\t\t}\n\t}\nprivate:\n\tvoid init(vector<T>&ps, const int& l, const int& r) {\n\t\tif (l >= r) {\n\t\t\treturn;\n\t\t}\n\t\tconst int mid = (l + r) / 2;\n\t\tnth_element(ps.begin() + l, ps.begin() + mid, ps.begin() + r, axisSorter<T>(axis));\n\t\tmidpo = ps[mid];\n\n\t\tltree = make_kdtree(ps, l, mid, depth + 1);\n\t\t\n\t\trtree = make_kdtree(ps, mid + 1, r, depth + 1);\n\t\tgetmindis();\n\t\tltree.reset();\n\t\trtree.reset();\n\t\tpos = vector<T>(ps.begin() + l, ps.begin() + r);\n\t}\n};\n\n\n//[l..r)\ntemplate<class T>\nunique_ptr<kdtree<T>>make_kdtree(vector<T>&ps_, const int& l, const int& r, const int& depth = 0) {\n\tif (l >= r)return nullptr;\n\telse {\n\t\treturn make_unique<kdtree<T>>(ps_, l, r, depth);\n\t}\n}\n\nvector<ld>getunit(vector<int>v) {\n\tvector<ld>unit;\n\tld len = 0;\n\tfor (auto&&a : v) {\n\t\tlen += a*a;\n\t\tunit.emplace_back(ld(a));\n\t}\n\tlen = sqrt(len);\n\tfor (auto&u : unit) {\n\t\tu /= len;\n\t}\n\treturn unit;\n}\nint gcd(int l, int r) {\n\tassert(l > 0 && r > 0);\n\tif (l > r)return gcd(r, l);\n\telse {\n\t\tconst int num = r%l;\n\t\tif (num) {\n\t\t\treturn gcd(l, num);\n\t\t}\n\t\telse {\n\t\t\treturn l;\n\t\t}\n\t}\n}\nint gcd(vector<int>nums) {\n\tif (nums.size() == 1)return nums[0];\n\tint num = nums[0];\n\n\tfor (int i = 1; i < nums.size(); ++i) {\n\t\tnum = gcd(num, nums[i]);\n\t}\n\treturn num;\n}\n\nint main() {\n\twhile (1) {\n\t\tanswer = Ans{ 1e18,make_pair(-1,-1) };\n\t\tint M, N, S, W; cin >> M >> N >> S >> W;\n\t\tif (!M&&!N&&!S&&!W)break;\n\t\tvector<po>pos;\n\t\tvector<vector<int>>memo;\n\t\t{\n\t\t\t{\n\t\t\t\tint nx, ny, nz;\n\t\t\t\tfor (int i = 0; i < M; ++i) {\n\t\t\t\t\tcin >> nx >> ny >> nz;\n\t\t\t\t\tmemo.push_back(vector<int>{nx, ny, nz});\n\t\t\t\t}\n\t\t\t\tint g = S;\n\t\t\t\tfor (int i = M; i < M + N; ++i) {\n\t\t\t\t\tnx = (g / 7) % 100 + 1;\n\t\t\t\t\tny = (g / 700) % 100 + 1;\n\t\t\t\t\tnz = (g / 70000) % 100 + 1;\n\t\t\t\t\tmemo.push_back(vector<int>{nx, ny, nz});\n\t\t\t\t\tif (g % 2 == 0)g = g / 2;\n\t\t\t\t\telse g = (g / 2) ^ W;\n\t\t\t\t}\n\t\t\t}\n\t\t\tsort(memo.begin(), memo.end(), [](const vector<int>&l, const vector<int>&r) {\n\t\t\t\tfor (int i = 0; i < 3; ++i) {\n\t\t\t\t\tif (l[i] == r[i])continue;\n\t\t\t\t\telse return l[i] < r[i];\n\t\t\t\t}\n\t\t\t\treturn false;\n\t\t\t});\n\t\t\t{\n\t\t\t\tvector<vector<int>>newmemo;\n\t\t\t\tmap<vector<int>, int>amap;\n\t\t\t\tfor (int i = 0; i < memo.size(); ++i) {\n\t\t\t\t\tint agcd = gcd(memo[i]);\n\t\t\t\t\tvector<int>v(3);\n\t\t\t\t\tfor (int j = 0; j < 3; ++j)v[j] = memo[i][j] / agcd;\n\t\t\t\t\tif (amap.find(v) == amap.end() || amap[v] > agcd) {\n\t\t\t\t\t\tamap[v] = agcd;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\tfor (auto m : amap) {\n\t\t\t\t\tauto v = m.first;\n\t\t\t\t\tfor (int i = 0; i < 3; ++i) {\n\t\t\t\t\t\tv[i] *= m.second;\n\t\t\t\t\t}\n\t\t\t\t\tnewmemo.emplace_back(v);\n\t\t\t\t}\n\t\t\t\tmemo = newmemo;\n\t\t\t}\n\t\t\tsort(memo.begin(), memo.end(), [](const vector<int>&l, const vector<int>&r) {\n\t\t\t\tfor (int i = 0; i < 3; ++i) {\n\t\t\t\t\tif (l[i] == r[i])continue;\n\t\t\t\t\telse return l[i] < r[i];\n\t\t\t\t}\n\t\t\t\treturn false;\n\t\t\t});\n\t\t\tfor (int i = 0; i < memo.size(); ++i) {\n\t\t\t\tpo p(i, getunit(memo[i]));\n\t\t\t\tpos.emplace_back(p);\n\t\t\t}\n\t\t}\n\n\t\tauto tree = make_kdtree(pos, 0, pos.size(), 0);\n\t\ttree->getmindis();\n\t\tauto ansp = answer;\n\t\tif (ansp.p.first >ansp.p.second)swap(ansp.p.first, ansp.p.second);\n\t\tfor (int i = 0; i < 3; ++i) {\n\t\t\tcout << memo[answer.p.first][i] << \" \";\n\t\t}\n\t\tfor (int i = 0; i < 3; ++i) {\n\t\t\tcout << memo[answer.p.second][i];\n\t\t\tif (i == 2)cout << endl;\n\t\t\telse cout << \" \";\n\t\t}\n\t}\n\treturn 0;\n}", "accuracy": 1, "time_ms": 6290, "memory_kb": 26912, "score_of_the_acc": -1.1499, "final_rank": 12 }, { "submission_id": "aoj_1340_2047410", "code_snippet": "#include \"bits/stdc++.h\"\nusing namespace std;\nusing ld = long double;\nconst ld eps = 1e-9;\n\nld getdis(vector<ld>&l, vector<ld>&r) {\n\tld sum = 0;\n\tfor (int i = 0; i < 3; ++i) {\n\t\tsum += (l[i] - r[i])*(l[i] - r[i]);\n\t}\n\treturn sqrt(sum);\n}\n\nstruct po {\n\tint index;\n\tvector<ld> coors;\n\tpo(int _d) :coors(_d) {\n\t\tindex = -1;\n\t}\n\tpo(int _index, vector<ld>_coors) :index(_index), coors(_coors) {\n\n\t}\n\tpo() {}\n};\nld getdis(po&lpo, po&rpo) {\n\treturn getdis(lpo.coors, rpo.coors);\n}\ntemplate<class T>\nclass axisSorter {\n\tint k;\npublic:\n\taxisSorter(int _k) : k(_k) {}\n\tbool operator()(const T &a, const T &b) {\n\t\treturn a.coors[k] < b.coors[k];\n\t}\n};\n\n\n\nstruct Ans {\n\tld dis;\n\tpair<int, int>p;\n\n};\n\n\nbool operator<(const Ans&l, const Ans&r) {\n\tif (abs(l.dis - r.dis) < eps) {\n\t\tassert(l.p.first < l.p.second);\n\t\tassert(r.p.first < r.p.second);\n\t\treturn l.p < r.p;\n\t}\n\telse {\n\t\treturn l.dis < r.dis;\n\t}\n\treturn true;\n}\nAns answer;\n\ntemplate<class T, int Dim = 3>\nstruct kdtree {\npublic:\n\tT midpo;\n\tvector<T>pos;\n\tshared_ptr<kdtree<T>> ltree, rtree;\n\tint depth;\n\tint axis;\n\tkdtree(const T &p_) :midpo(p_), ltree(), rtree() {\n\n\t}\n\t~kdtree() {\n\t}\n\n\tkdtree(vector<T>&ps_, const int& l, const int& r, const int depth_ = 0) : ltree(nullptr), rtree(nullptr), depth(depth_), axis(depth%Dim) {\n\t\tinit(ps_, l, r);\n\t}\n\tvoid getmindis() {\n\t\tvector<po>lpos, rpos;\n\t\tld axisvalue = midpo.coors[axis];\n\n\t\tif (ltree != nullptr) {\n\t\t\tfor (auto &po : ltree->pos) {\n\t\t\t\tif (po.coors[axis]>axisvalue - sqrt(answer.dis) - eps * 10) {\n\t\t\t\t\tlpos.emplace_back(po);\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\tif (rtree != nullptr) {\n\t\t\tfor (auto &po : rtree->pos) {\n\t\t\t\tif (po.coors[axis]<axisvalue + sqrt(answer.dis) + eps * 10) {\n\t\t\t\t\trpos.emplace_back(po);\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\n\t\tfor (auto& lpo : lpos) {\n\t\t\tfor (auto& rpo : rpos) {\n\t\t\t\tld dis = getdis(lpo, rpo);\n\t\t\t\tpair<int, int> p(rpo.index, lpo.index);\n\t\t\t\tif (p.first > p.second)swap(p.first, p.second);\n\t\t\t\tAns nans{ dis,p };\n\t\t\t\tanswer = min(answer, nans);\n\t\t\t}\n\t\t\t{\n\t\t\t\tld dis = getdis(lpo, midpo);\n\t\t\t\tpair<int, int> p(midpo.index, lpo.index);\n\t\t\t\tif (p.first > p.second)swap(p.first, p.second);\n\t\t\t\tAns nans{ dis,p };\n\t\t\t\tanswer = min(answer, nans);\n\t\t\t}\n\t\t}\n\t\tfor (auto& rpo : rpos) {\n\t\t\tld dis = getdis(midpo, rpo);\n\t\t\tpair<int, int> p(midpo.index, rpo.index);\n\t\t\tif (p.first > p.second)swap(p.first, p.second);\n\t\t\tAns nans{ dis,p };\n\t\t\tanswer = min(answer, nans);\n\t\t}\n\t}\nprivate:\n\tvoid init(vector<T>&ps, const int& l, const int& r) {\n\t\tif (l >= r) {\n\t\t\treturn;\n\t\t}\n\t\tconst int mid = (l + r) / 2;\n\t\tnth_element(ps.begin() + l, ps.begin() + mid, ps.begin() + r, axisSorter<T>(axis));\n\t\tmidpo = ps[mid];\n\n\t\tltree = make_kdtree(ps, l, mid, depth + 1);\n\t\t\n\t\trtree = make_kdtree(ps, mid + 1, r, depth + 1);\n\t\tgetmindis();\n\t\tltree.reset();\n\t\trtree.reset();\n\t\tpos = vector<T>(ps.begin() + l, ps.begin() + r);\n\t}\n};\n\n\n//[l..r)\ntemplate<class T>\nunique_ptr<kdtree<T>>make_kdtree(vector<T>&ps_, const int& l, const int& r, const int& depth = 0) {\n\tif (l >= r)return nullptr;\n\telse {\n\t\treturn make_unique<kdtree<T>>(ps_, l, r, depth);\n\t}\n}\n\nvector<ld>getunit(vector<int>v) {\n\tvector<ld>unit;\n\tld len = 0;\n\tfor (auto&&a : v) {\n\t\tlen += a*a;\n\t\tunit.emplace_back(ld(a));\n\t}\n\tlen = sqrt(len);\n\tfor (auto&u : unit) {\n\t\tu /= len;\n\t}\n\treturn unit;\n}\nint gcd(int l, int r) {\n\tassert(l > 0 && r > 0);\n\tif (l > r)return gcd(r, l);\n\telse {\n\t\tconst int num = r%l;\n\t\tif (num) {\n\t\t\treturn gcd(l, num);\n\t\t}\n\t\telse {\n\t\t\treturn l;\n\t\t}\n\t}\n}\nint gcd(vector<int>nums) {\n\tif (nums.size() == 1)return nums[0];\n\tint num = nums[0];\n\n\tfor (int i = 1; i < nums.size(); ++i) {\n\t\tnum = gcd(num, nums[i]);\n\t}\n\treturn num;\n}\n\nint main() {\n\twhile (1) {\n\t\tanswer = Ans{ 1e18,make_pair(-1,-1) };\n\t\tint M, N, S, W; cin >> M >> N >> S >> W;\n\t\tif (!M&&!N&&!S&&!W)break;\n\t\tvector<po>pos;\n\t\tvector<vector<int>>memo;\n\t\t{\n\t\t\t{\n\t\t\t\tint nx, ny, nz;\n\t\t\t\tfor (int i = 0; i < M; ++i) {\n\t\t\t\t\tcin >> nx >> ny >> nz;\n\t\t\t\t\tmemo.push_back(vector<int>{nx, ny, nz});\n\t\t\t\t}\n\t\t\t\tint g = S;\n\t\t\t\tfor (int i = M; i < M + N; ++i) {\n\t\t\t\t\tnx = (g / 7) % 100 + 1;\n\t\t\t\t\tny = (g / 700) % 100 + 1;\n\t\t\t\t\tnz = (g / 70000) % 100 + 1;\n\t\t\t\t\tmemo.push_back(vector<int>{nx, ny, nz});\n\t\t\t\t\tif (g % 2 == 0)g = g / 2;\n\t\t\t\t\telse g = (g / 2) ^ W;\n\t\t\t\t}\n\t\t\t}\n\t\t\t{\n\t\t\t\tvector<vector<int>>newmemo;\n\t\t\t\tmap<vector<int>, int>amap;\n\t\t\t\tfor (int i = 0; i < memo.size(); ++i) {\n\t\t\t\t\tint agcd = gcd(memo[i]);\n\t\t\t\t\tvector<int>v(3);\n\t\t\t\t\tfor (int j = 0; j < 3; ++j)v[j] = memo[i][j] / agcd;\n\t\t\t\t\tif (amap.find(v) == amap.end() || amap[v] > agcd) {\n\t\t\t\t\t\tamap[v] = agcd;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\tfor (auto m : amap) {\n\t\t\t\t\tauto v = m.first;\n\t\t\t\t\tfor (int i = 0; i < 3; ++i) {\n\t\t\t\t\t\tv[i] *= m.second;\n\t\t\t\t\t}\n\t\t\t\t\tnewmemo.emplace_back(v);\n\t\t\t\t}\n\t\t\t\tmemo = newmemo;\n\t\t\t}\n\t\t\tsort(memo.begin(), memo.end(), [](const vector<int>&l, const vector<int>&r) {\n\t\t\t\tfor (int i = 0; i < 3; ++i) {\n\t\t\t\t\tif (l[i] == r[i])continue;\n\t\t\t\t\telse return l[i] < r[i];\n\t\t\t\t}\n\t\t\t\treturn false;\n\t\t\t});\n\t\t\tfor (int i = 0; i < memo.size(); ++i) {\n\t\t\t\tpo p(i, getunit(memo[i]));\n\t\t\t\tpos.emplace_back(p);\n\t\t\t}\n\t\t}\n\n\t\tauto tree = make_kdtree(pos, 0, pos.size(), 0);\n\t\ttree->getmindis();\n\t\tauto ansp = answer;\n\t\tif (ansp.p.first >ansp.p.second)swap(ansp.p.first, ansp.p.second);\n\t\tfor (int i = 0; i < 3; ++i) {\n\t\t\tcout << memo[answer.p.first][i] << \" \";\n\t\t}\n\t\tfor (int i = 0; i < 3; ++i) {\n\t\t\tcout << memo[answer.p.second][i];\n\t\t\tif (i == 2)cout << endl;\n\t\t\telse cout << \" \";\n\t\t}\n\t}\n\treturn 0;\n}", "accuracy": 1, "time_ms": 6530, "memory_kb": 26840, "score_of_the_acc": -1.1809, "final_rank": 15 }, { "submission_id": "aoj_1340_2047406", "code_snippet": "#include \"bits/stdc++.h\"\nusing namespace std;\nusing ld = long double;\nconst ld eps = 1e-9;\n\nld getdis(vector<ld>&l, vector<ld>&r) {\n\tld sum = 0;\n\tfor (int i = 0; i < 3; ++i) {\n\t\tsum += (l[i] - r[i])*(l[i] - r[i]);\n\t}\n\treturn sqrt(sum);\n}\n\nstruct po {\n\tint index;\n\tvector<ld> coors;\n\tpo(int _d) :coors(_d) {\n\t\tindex = -1;\n\t}\n\tpo(int _index, vector<ld>_coors) :index(_index), coors(_coors) {\n\n\t}\n\tpo() {}\n};\nld getdis(po&lpo, po&rpo) {\n\treturn getdis(lpo.coors, rpo.coors);\n}\ntemplate<class T>\nclass axisSorter {\n\tint k;\npublic:\n\taxisSorter(int _k) : k(_k) {}\n\tbool operator()(const T &a, const T &b) {\n\t\treturn a.coors[k] < b.coors[k];\n\t}\n};\n\n\n\nstruct Ans {\n\tld dis;\n\tpair<int, int>p;\n\n};\n\n\nbool operator<(const Ans&l, const Ans&r) {\n\tif (abs(l.dis - r.dis) < eps) {\n\t\tassert(l.p.first < l.p.second);\n\t\tassert(r.p.first < r.p.second);\n\t\treturn l.p < r.p;\n\t}\n\telse {\n\t\treturn l.dis < r.dis;\n\t}\n\treturn true;\n}\nAns answer;\n\ntemplate<class T, int Dim = 3>\nstruct kdtree {\npublic:\n\tT midpo;\n\tvector<T>pos;\n\tshared_ptr<kdtree<T>> ltree, rtree;\n\tint depth;\n\tint axis;\n\tkdtree(const T &p_) :midpo(p_), ltree(), rtree() {\n\n\t}\n\t~kdtree() {\n\t}\n\n\tkdtree(vector<T>&ps_, const int& l, const int& r, const int depth_ = 0) : ltree(nullptr), rtree(nullptr), depth(depth_), axis(depth%Dim) {\n\t\tinit(ps_, l, r);\n\t}\n\tvoid getmindis() {\n\t\tvector<po>lpos, rpos;\n\t\tld axisvalue = midpo.coors[axis];\n\n\t\tif (ltree != nullptr) {\n\t\t\tfor (auto &po : ltree->pos) {\n\t\t\t\tif (po.coors[axis]>axisvalue - sqrt(answer.dis) - eps * 10) {\n\t\t\t\t\tlpos.emplace_back(po);\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\tif (rtree != nullptr) {\n\t\t\tfor (auto &po : rtree->pos) {\n\t\t\t\tif (po.coors[axis]<axisvalue + sqrt(answer.dis) + eps * 10) {\n\t\t\t\t\trpos.emplace_back(po);\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\n\t\tfor (auto& lpo : lpos) {\n\t\t\tfor (auto& rpo : rpos) {\n\t\t\t\tld dis = getdis(lpo, rpo);\n\t\t\t\tpair<int, int> p(rpo.index, lpo.index);\n\t\t\t\tif (p.first > p.second)swap(p.first, p.second);\n\t\t\t\tAns nans{ dis,p };\n\t\t\t\tanswer = min(answer, nans);\n\t\t\t}\n\t\t\t{\n\t\t\t\tld dis = getdis(lpo, midpo);\n\t\t\t\tpair<int, int> p(midpo.index, lpo.index);\n\t\t\t\tif (p.first > p.second)swap(p.first, p.second);\n\t\t\t\tAns nans{ dis,p };\n\t\t\t\tanswer = min(answer, nans);\n\t\t\t}\n\t\t}\n\t\tfor (auto& rpo : rpos) {\n\t\t\tld dis = getdis(midpo, rpo);\n\t\t\tpair<int, int> p(midpo.index, rpo.index);\n\t\t\tif (p.first > p.second)swap(p.first, p.second);\n\t\t\tAns nans{ dis,p };\n\t\t\tanswer = min(answer, nans);\n\t\t}\n\n\n\t}\nprivate:\n\tvoid init(vector<T>&ps, const int& l, const int& r) {\n\t\tif (l >= r) {\n\t\t\treturn;\n\t\t}\n\t\tconst int mid = (l + r) / 2;\n\t\tnth_element(ps.begin() + l, ps.begin() + mid, ps.begin() + r, axisSorter<T>(axis));\n\t\tmidpo = ps[mid];\n\n\t\tltree = make_kdtree(ps, l, mid, depth + 1);\n\t\t\n\t\trtree = make_kdtree(ps, mid + 1, r, depth + 1);\n\t\tgetmindis();\n\t\tltree.reset();\n\t\trtree.reset();\n\t\tpos = vector<T>(ps.begin() + l, ps.begin() + r);\n\t}\n};\n\n\n//[l..r)\ntemplate<class T>\nunique_ptr<kdtree<T>>make_kdtree(vector<T>&ps_, const int& l, const int& r, const int& depth = 0) {\n\tif (l >= r)return nullptr;\n\telse {\n\t\treturn make_unique<kdtree<T>>(ps_, l, r, depth);\n\t}\n}\n\nvector<ld>getunit(vector<int>v) {\n\tvector<ld>unit;\n\tld len = 0;\n\tfor (auto a : v) {\n\t\tlen += a*a;\n\t\tunit.emplace_back(ld(a));\n\t}\n\tlen = sqrt(len);\n\tfor (auto&u : unit) {\n\t\tu /= len;\n\t}\n\treturn unit;\n}\nint gcd(int l, int r) {\n\tassert(l > 0 && r > 0);\n\tif (l > r)return gcd(r, l);\n\telse {\n\t\tconst int num = r%l;\n\t\tif (num) {\n\t\t\treturn gcd(l, num);\n\t\t}\n\t\telse {\n\t\t\treturn l;\n\t\t}\n\t}\n}\nint gcd(vector<int>nums) {\n\tif (nums.size() == 1)return nums[0];\n\tint num = nums[0];\n\n\tfor (int i = 1; i < nums.size(); ++i) {\n\t\tnum = gcd(num, nums[i]);\n\t}\n\treturn num;\n}\n\nint main() {\n\twhile (1) {\n\t\tanswer = Ans{ 1e18,make_pair(-1,-1) };\n\t\tint M, N, S, W; cin >> M >> N >> S >> W;\n\t\tif (!M&&!N&&!S&&!W)break;\n\t\tvector<po>pos;\n\t\tvector<vector<int>>memo;\n\t\t{\n\t\t\t{\n\t\t\t\tint nx, ny, nz;\n\t\t\t\tfor (int i = 0; i < M; ++i) {\n\t\t\t\t\tcin >> nx >> ny >> nz;\n\t\t\t\t\tmemo.push_back(vector<int>{nx, ny, nz});\n\t\t\t\t}\n\t\t\t\tint g = S;\n\t\t\t\tfor (int i = M; i < M + N; ++i) {\n\t\t\t\t\tnx = (g / 7) % 100 + 1;\n\t\t\t\t\tny = (g / 700) % 100 + 1;\n\t\t\t\t\tnz = (g / 70000) % 100 + 1;\n\t\t\t\t\tmemo.push_back(vector<int>{nx, ny, nz});\n\t\t\t\t\tif (g % 2 == 0)g = g / 2;\n\t\t\t\t\telse g = (g / 2) ^ W;\n\t\t\t\t}\n\t\t\t}\n\t\t\tsort(memo.begin(), memo.end(), [](const vector<int>&l, const vector<int>&r) {\n\t\t\t\tfor (int i = 0; i < 3; ++i) {\n\t\t\t\t\tif (l[i] == r[i])continue;\n\t\t\t\t\telse return l[i] < r[i];\n\t\t\t\t}\n\t\t\t\treturn false;\n\t\t\t});\n\t\t\t{\n\t\t\t\tvector<vector<int>>newmemo;\n\t\t\t\tmap<vector<int>, int>amap;\n\t\t\t\tfor (int i = 0; i < memo.size(); ++i) {\n\t\t\t\t\tint agcd = gcd(memo[i]);\n\t\t\t\t\tvector<int>v(3);\n\t\t\t\t\tfor (int j = 0; j < 3; ++j)v[j] = memo[i][j] / agcd;\n\t\t\t\t\tif (amap.find(v) == amap.end() || amap[v] > agcd) {\n\t\t\t\t\t\tamap[v] = agcd;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\tfor (auto m : amap) {\n\t\t\t\t\tauto v = m.first;\n\t\t\t\t\tfor (int i = 0; i < 3; ++i) {\n\t\t\t\t\t\tv[i] *= m.second;\n\t\t\t\t\t}\n\t\t\t\t\tnewmemo.emplace_back(v);\n\t\t\t\t}\n\t\t\t\tmemo = newmemo;\n\t\t\t}\n\t\t\tsort(memo.begin(), memo.end(), [](const vector<int>&l, const vector<int>&r) {\n\t\t\t\tfor (int i = 0; i < 3; ++i) {\n\t\t\t\t\tif (l[i] == r[i])continue;\n\t\t\t\t\telse return l[i] < r[i];\n\t\t\t\t}\n\t\t\t\treturn false;\n\t\t\t});\n\t\t\tfor (int i = 0; i < memo.size(); ++i) {\n\t\t\t\tpo p(i, getunit(memo[i]));\n\t\t\t\tpos.emplace_back(p);\n\t\t\t}\n\t\t}\n\n\t\tauto tree = make_kdtree(pos, 0, pos.size(), 0);\n\t\ttree->getmindis();\n\t\tauto ansp = answer;\n\t\tif (ansp.p.first >ansp.p.second)swap(ansp.p.first, ansp.p.second);\n\t\tfor (int i = 0; i < 3; ++i) {\n\t\t\tcout << memo[answer.p.first][i] << \" \";\n\t\t}\n\t\tfor (int i = 0; i < 3; ++i) {\n\t\t\tcout << memo[answer.p.second][i];\n\t\t\tif (i == 2)cout << endl;\n\t\t\telse cout << \" \";\n\t\t}\n\t}\n\treturn 0;\n}", "accuracy": 1, "time_ms": 6300, "memory_kb": 26912, "score_of_the_acc": -1.1513, "final_rank": 14 }, { "submission_id": "aoj_1340_2047403", "code_snippet": "#include \"bits/stdc++.h\"\nusing namespace std;\nusing ld = long double;\nconst ld eps = 1e-9;\n\nld getdis(vector<ld>&l, vector<ld>&r) {\n\tld sum = 0;\n\tfor (int i = 0; i < 3; ++i) {\n\t\tsum += (l[i] - r[i])*(l[i] - r[i]);\n\t}\n\treturn sqrt(sum);\n}\n\nstruct po {\n\tint index;\n\tvector<ld> coors;\n\tpo(int _d) :coors(_d) {\n\t\tindex = -1;\n\t}\n\tpo(int _index, vector<ld>_coors) :index(_index), coors(_coors) {\n\n\t}\n\tpo() {}\n};\nld getdis(po&lpo, po&rpo) {\n\treturn getdis(lpo.coors, rpo.coors);\n}\ntemplate<class T>\nclass axisSorter {\n\tint k;\npublic:\n\taxisSorter(int _k) : k(_k) {}\n\tbool operator()(const T &a, const T &b) {\n\t\treturn a.coors[k] < b.coors[k];\n\t}\n};\n\n\n\nstruct Ans {\n\tld dis;\n\tpair<int, int>p;\n\n};\n\n\nbool operator<(const Ans&l, const Ans&r) {\n\tif (abs(l.dis - r.dis) < eps) {\n\t\tassert(l.p.first < l.p.second);\n\t\tassert(r.p.first < r.p.second);\n\t\treturn l.p < r.p;\n\t}\n\telse {\n\t\treturn l.dis < r.dis;\n\t}\n\treturn true;\n}\nAns answer;\n\ntemplate<class T, int Dim = 3>\nstruct kdtree {\npublic:\n\tT midpo;\n\tvector<T>pos;\n\tshared_ptr<kdtree<T>> ltree, rtree;\n\tint depth;\n\tint axis;\n\tkdtree(const T &p_) :midpo(p_), ltree(), rtree() {\n\n\t}\n\t~kdtree() {\n\t}\n\n\tkdtree(vector<T>&ps_, const int& l, const int& r, const int depth_ = 0) : ltree(nullptr), rtree(nullptr), depth(depth_), axis(depth%Dim) {\n\t\tinit(ps_, l, r);\n\t}\n\tvoid getmindis() {\n\t\tvector<po>lpos, rpos;\n\t\tld axisvalue = midpo.coors[axis];\n\n\t\tif (ltree != nullptr) {\n\t\t\tfor (auto &po : ltree->pos) {\n\t\t\t\tif (axisvalue>po.coors[axis] - eps * 1000000 && po.coors[axis]>axisvalue - sqrt(answer.dis) - eps * 1000000) {\n\t\t\t\t\tlpos.emplace_back(po);\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\tif (rtree != nullptr) {\n\t\t\tfor (auto &po : rtree->pos) {\n\t\t\t\tif (axisvalue<po.coors[axis] + eps * 1000000 && po.coors[axis]<axisvalue + sqrt(answer.dis) + eps * 1000000) {\n\t\t\t\t\trpos.emplace_back(po);\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\n\t\tfor (auto& lpo : lpos) {\n\t\t\tfor (auto& rpo : rpos) {\n\t\t\t\tld dis = getdis(lpo, rpo);\n\t\t\t\tpair<int, int> p(rpo.index, lpo.index);\n\t\t\t\tif (p.first > p.second)swap(p.first, p.second);\n\t\t\t\tAns nans{ dis,p };\n\t\t\t\tanswer = min(answer, nans);\n\t\t\t}\n\t\t\t{\n\t\t\t\tld dis = getdis(lpo, midpo);\n\t\t\t\tpair<int, int> p(midpo.index, lpo.index);\n\t\t\t\tif (p.first > p.second)swap(p.first, p.second);\n\t\t\t\tAns nans{ dis,p };\n\t\t\t\tanswer = min(answer, nans);\n\t\t\t}\n\t\t}\n\t\tfor (auto& rpo : rpos) {\n\t\t\tld dis = getdis(midpo, rpo);\n\t\t\tpair<int, int> p(midpo.index, rpo.index);\n\t\t\tif (p.first > p.second)swap(p.first, p.second);\n\t\t\tAns nans{ dis,p };\n\t\t\tif (p.first == 1524 && 2763 == p.second) {\n\t\t\t\tint a = 1; a++;\n\t\t\t}\n\t\t\t//if (p.first == 2763 && 1524 == p.second)assert(false);\n\t\t\tanswer = min(answer, nans);\n\t\t\tint b;\n\t\t\tb = 1; b++;\n\t\t}\n\n\n\t}\nprivate:\n\tvoid init(vector<T>&ps, const int& l, const int& r) {\n\t\tif (l >= r) {\n\t\t\treturn;\n\t\t}\n\t\tconst int mid = (l + r) / 2;\n\t\tnth_element(ps.begin() + l, ps.begin() + mid, ps.begin() + r, axisSorter<T>(axis));\n\t\tmidpo = ps[mid];\n\n\t\tltree = make_kdtree(ps, l, mid, depth + 1);\n\t\t\n\t\trtree = make_kdtree(ps, mid + 1, r, depth + 1);\n\t\tgetmindis();\n\t\tltree.reset();\n\t\trtree.reset();\n\t\tpos = vector<T>(ps.begin() + l, ps.begin() + r);\n\t}\n};\n\n\n//[l..r)\ntemplate<class T>\nunique_ptr<kdtree<T>>make_kdtree(vector<T>&ps_, const int& l, const int& r, const int& depth = 0) {\n\tif (l >= r)return nullptr;\n\telse {\n\t\treturn make_unique<kdtree<T>>(ps_, l, r, depth);\n\t}\n}\n\nvector<ld>getunit(vector<int>v) {\n\tvector<ld>unit;\n\tld len = 0;\n\tfor (auto a : v) {\n\t\tlen += a*a;\n\t\tunit.emplace_back(ld(a));\n\t}\n\tlen = sqrt(len);\n\tfor (auto&u : unit) {\n\t\tu /= len;\n\t}\n\treturn unit;\n}\nint gcd(int l, int r) {\n\tassert(l > 0 && r > 0);\n\tif (l > r)return gcd(r, l);\n\telse {\n\t\tconst int num = r%l;\n\t\tif (num) {\n\t\t\treturn gcd(l, num);\n\t\t}\n\t\telse {\n\t\t\treturn l;\n\t\t}\n\t}\n}\nint gcd(vector<int>nums) {\n\tif (nums.size() == 1)return nums[0];\n\tint num = nums[0];\n\n\tfor (int i = 1; i < nums.size(); ++i) {\n\t\tnum = gcd(num, nums[i]);\n\t}\n\treturn num;\n}\n\nint main() {\n\twhile (1) {\n\t\tanswer = Ans{ 1e18,make_pair(-1,-1) };\n\t\tint M, N, S, W; cin >> M >> N >> S >> W;\n\t\tif (!M&&!N&&!S&&!W)break;\n\t\tvector<po>pos;\n\t\tvector<vector<int>>memo;\n\t\t{\n\t\t\t{\n\t\t\t\tint nx, ny, nz;\n\t\t\t\tfor (int i = 0; i < M; ++i) {\n\t\t\t\t\tcin >> nx >> ny >> nz;\n\t\t\t\t\tmemo.push_back(vector<int>{nx, ny, nz});\n\t\t\t\t}\n\t\t\t\tint g = S;\n\t\t\t\tfor (int i = M; i < M + N; ++i) {\n\t\t\t\t\tnx = (g / 7) % 100 + 1;\n\t\t\t\t\tny = (g / 700) % 100 + 1;\n\t\t\t\t\tnz = (g / 70000) % 100 + 1;\n\t\t\t\t\tmemo.push_back(vector<int>{nx, ny, nz});\n\t\t\t\t\tif (g % 2 == 0)g = g / 2;\n\t\t\t\t\telse g = (g / 2) ^ W;\n\t\t\t\t}\n\t\t\t}\n\t\t\tsort(memo.begin(), memo.end(), [](const vector<int>&l, const vector<int>&r) {\n\t\t\t\tfor (int i = 0; i < 3; ++i) {\n\t\t\t\t\tif (l[i] == r[i])continue;\n\t\t\t\t\telse return l[i] < r[i];\n\t\t\t\t}\n\t\t\t\treturn false;\n\t\t\t});\n\t\t\t{\n\t\t\t\tvector<vector<int>>newmemo;\n\t\t\t\tmap<vector<int>, int>amap;\n\t\t\t\tfor (int i = 0; i < memo.size(); ++i) {\n\t\t\t\t\tint agcd = gcd(memo[i]);\n\t\t\t\t\tvector<int>v(3);\n\t\t\t\t\tfor (int j = 0; j < 3; ++j)v[j] = memo[i][j] / agcd;\n\t\t\t\t\tif (amap.find(v) == amap.end() || amap[v] > agcd) {\n\t\t\t\t\t\tamap[v] = agcd;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\tfor (auto m : amap) {\n\t\t\t\t\tauto v = m.first;\n\t\t\t\t\tfor (int i = 0; i < 3; ++i) {\n\t\t\t\t\t\tv[i] *= m.second;\n\t\t\t\t\t}\n\t\t\t\t\tnewmemo.emplace_back(v);\n\t\t\t\t}\n\t\t\t\tmemo = newmemo;\n\t\t\t}\n\t\t\tsort(memo.begin(), memo.end(), [](const vector<int>&l, const vector<int>&r) {\n\t\t\t\tfor (int i = 0; i < 3; ++i) {\n\t\t\t\t\tif (l[i] == r[i])continue;\n\t\t\t\t\telse return l[i] < r[i];\n\t\t\t\t}\n\t\t\t\treturn false;\n\t\t\t});\n\t\t\tfor (int i = 0; i < memo.size(); ++i) {\n\t\t\t\tpo p(i, getunit(memo[i]));\n\t\t\t\tpos.emplace_back(p);\n\t\t\t}\n\t\t}\n\n\t\tauto tree = make_kdtree(pos, 0, pos.size(), 0);\n\t\ttree->getmindis();\n\t\tauto ansp = answer;\n\t\tif (ansp.p.first >ansp.p.second)swap(ansp.p.first, ansp.p.second);\n\t\tfor (int i = 0; i < 3; ++i) {\n\t\t\tcout << memo[answer.p.first][i] << \" \";\n\t\t}\n\t\tfor (int i = 0; i < 3; ++i) {\n\t\t\tcout << memo[answer.p.second][i];\n\t\t\tif (i == 2)cout << endl;\n\t\t\telse cout << \" \";\n\t\t}\n\t}\n\treturn 0;\n}\n//1524,2763", "accuracy": 1, "time_ms": 6840, "memory_kb": 26992, "score_of_the_acc": -1.2248, "final_rank": 16 }, { "submission_id": "aoj_1340_2047023", "code_snippet": "#include \"bits/stdc++.h\"\nusing namespace std;\nusing ld = long double;\nconst ld eps = 1e-9;\n\nld getdis(vector<ld>&l, vector<ld>&r) {\n\tld sum = 0;\n\tfor (int i = 0; i < 3; ++i) {\n\t\tsum += (l[i] - r[i])*(l[i] - r[i]);\n\t}\n\treturn sqrt(sum);\n}\n\nstruct po {\n\tint index;\n\tvector<ld> coors;\n\tpo(int _d) :coors(_d) {\n\t\tindex = -1;\n\t}\n\tpo(int _index, vector<ld>_coors) :index(_index), coors(_coors) {\n\n\t}\n\tpo() {}\n};\nld getdis(po&lpo, po&rpo) {\n\treturn getdis(lpo.coors, rpo.coors);\n}\ntemplate<class T>\nclass axisSorter {\n\tint k;\npublic:\n\taxisSorter(int _k) : k(_k) {}\n\tbool operator()(const T &a, const T &b) {\n\t\treturn a.coors[k] < b.coors[k];\n\t}\n};\n\n\n\nstruct Ans {\n\tld dis;\n\tpair<int, int>p;\n\n};\n\n\nbool operator<(const Ans&l, const Ans&r) {\n\tif (abs(l.dis - r.dis) < eps) {\n\t\tassert(l.p.first < l.p.second);\n\t\tassert(r.p.first < r.p.second);\n\t\treturn l.p < r.p;\n\t}\n\telse {\n\t\treturn l.dis < r.dis;\n\t}\n\treturn true;\n}\nAns answer;\n\ntemplate<class T, int Dim = 3>\nstruct kdtree {\npublic:\n\tT midpo;\n\tvector<T>pos;\n\tkdtree<T> *ltree, *rtree;\n\tint depth;\n\tint axis;\n\tkdtree(const T &p_) :midpo(p_), ltree(nullptr), rtree(nullptr) {\n\n\t}\n\t~kdtree() {\n\t\tif (ltree != nullptr) {\n\t\t\tdelete(ltree);\n\t\t\tltree = nullptr;\n\t\t}\n\n\t\tif (rtree != nullptr) {\n\t\t\tdelete(rtree);\n\t\t\trtree = nullptr;\n\t\t}\n\t}\n\n\tkdtree(vector<T>&ps_, const int& l, const int& r, const int depth_ = 0) : ltree(nullptr), rtree(nullptr), depth(depth_), axis(depth%Dim) {\n\t\tinit(ps_, l, r);\n\t}\n\t/*vector<T>query(const T & amin, const T&amax) {\n\tvector<T>ans;\n\tbool aok = true;\n\tfor (int i = 0; i < Dim; ++i) {\n\tif (amin.coors[i] <= midpo.coors[i] && midpo.coors[i] <= amax.coors[i]) {\n\n\t}\n\telse {\n\taok = false;\n\tbreak;\n\t}\n\t}\n\tif (aok) {\n\tans.emplace_back(val);\n\t}\n\taxisSorter<T> as(axis);\n\tif (as(midpo, amax) || midpo.coors[axis] == amax.coors[axis]) {\n\tif (rtree != nullptr) {\n\tvector<T>tans(rtree->query(amin, amax));\n\tans.insert(ans.end(), tans.begin(), tans.end());\n\t}\n\t}\n\tif (as(amin, midpo) || midpo.coors[axis] == amin.coors[axis]) {\n\tif (ltree != nullptr) {\n\tvector<T>tans(ltree->query(amin, amax));\n\tans.insert(ans.end(), tans.begin(), tans.end());\n\t}\n\t}\n\treturn ans;\n\t}*/\n\tvoid getmindis() {\n\t\t/*if (ltree != nullptr) {\n\t\tauto p =ltree->getmindis();\n\t\tif (ansnum > p.second) {\n\t\tansnum = p.second;\n\t\tansp = p.first;\n\t\t}\n\t\t}\n\t\tif (rtree != nullptr) {\n\t\tauto p = rtree->getmindis();\n\t\tif (ansnum > p.second) {\n\t\tansnum = p.second;\n\t\tansp = p.first;\n\t\t}\n\t\t}*/\n\n\t\tvector<po>lpos, rpos;\n\t\tld axisvalue = midpo.coors[axis];\n\n\t\tif (ltree != nullptr) {\n\t\t\tfor (auto &po : ltree->pos) {\n\t\t\t\tif (axisvalue>po.coors[axis] - eps * 1000000 && po.coors[axis]>axisvalue - sqrt(answer.dis) - eps * 1000000) {\n\t\t\t\t\tlpos.emplace_back(po);\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\tif (rtree != nullptr) {\n\t\t\tfor (auto &po : rtree->pos) {\n\t\t\t\tif (axisvalue<po.coors[axis] + eps * 1000000 && po.coors[axis]<axisvalue + sqrt(answer.dis) + eps * 1000000) {\n\t\t\t\t\trpos.emplace_back(po);\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\n\t\tfor (auto& lpo : lpos) {\n\t\t\tfor (auto& rpo : rpos) {\n\t\t\t\tld dis = getdis(lpo, rpo);\n\t\t\t\tpair<int, int> p(rpo.index, lpo.index);\n\t\t\t\tif (p.first > p.second)swap(p.first, p.second);\n\t\t\t\tAns nans{ dis,p };\n\t\t\t\tanswer = min(answer, nans);\n\t\t\t}\n\t\t\t{\n\t\t\t\tld dis = getdis(lpo, midpo);\n\t\t\t\tpair<int, int> p(midpo.index, lpo.index);\n\t\t\t\tif (p.first > p.second)swap(p.first, p.second);\n\t\t\t\tAns nans{ dis,p };\n\t\t\t\tanswer = min(answer, nans);\n\t\t\t}\n\t\t}\n\t\tfor (auto& rpo : rpos) {\n\t\t\tld dis = getdis(midpo, rpo);\n\t\t\tpair<int, int> p(midpo.index, rpo.index);\n\t\t\tif (p.first > p.second)swap(p.first, p.second);\n\t\t\tAns nans{ dis,p };\n\t\t\tif (p.first == 1524 && 2763 == p.second) {\n\t\t\t\tint a = 1; a++;\n\t\t\t}\n\t\t\t//if (p.first == 2763 && 1524 == p.second)assert(false);\n\t\t\tanswer = min(answer, nans);\n\t\t\tint b;\n\t\t\tb = 1; b++;\n\t\t}\n\n\n\t}\nprivate:\n\tvoid init(vector<T>&ps, const int& l, const int& r) {\n\t\tif (l >= r) {\n\t\t\treturn;\n\t\t}\n\t\tconst int mid = (l + r) / 2;\n\t\tnth_element(ps.begin() + l, ps.begin() + mid, ps.begin() + r, axisSorter<T>(axis));\n\t\tmidpo = ps[mid];\n\n\t\tltree = make_kdtree(ps, l, mid, depth + 1);\n\t\t\n\t\trtree = make_kdtree(ps, mid + 1, r, depth + 1);\n\t\tgetmindis();\n\t\tif (ltree) {\n\t\t\tdelete(ltree);\n\t\t\tltree = nullptr;\n\t\t}\n\t\tif (rtree) {\n\t\t\tdelete(rtree);\n\t\t\trtree = nullptr;\n\t\t}\n\t\tpos = vector<T>(ps.begin() + l, ps.begin() + r);\n\t}\n};\n\n\n//[l..r)\ntemplate<class T>\nkdtree<T> *make_kdtree(vector<T>&ps_, const int& l, const int& r, const int& depth = 0) {\n\tif (l >= r)return nullptr;\n\telse {\n\t\treturn new kdtree<T>(ps_, l, r, depth);\n\t}\n}\n\nvector<ld>getunit(vector<int>v) {\n\tvector<ld>unit;\n\tld len = 0;\n\tfor (auto a : v) {\n\t\tlen += a*a;\n\t\tunit.emplace_back(ld(a));\n\t}\n\tlen = sqrt(len);\n\tfor (auto&u : unit) {\n\t\tu /= len;\n\t}\n\treturn unit;\n}\nint gcd(int l, int r) {\n\tassert(l > 0 && r > 0);\n\tif (l > r)return gcd(r, l);\n\telse {\n\t\tconst int num = r%l;\n\t\tif (num) {\n\t\t\treturn gcd(l, num);\n\t\t}\n\t\telse {\n\t\t\treturn l;\n\t\t}\n\t}\n}\nint gcd(vector<int>nums) {\n\tif (nums.size() == 1)return nums[0];\n\tint num = nums[0];\n\n\tfor (int i = 1; i < nums.size(); ++i) {\n\t\tnum = gcd(num, nums[i]);\n\t}\n\treturn num;\n}\n\nint main() {\n\twhile (1) {\n\t\tanswer = Ans{ 1e18,make_pair(-1,-1) };\n\t\tint M, N, S, W; cin >> M >> N >> S >> W;\n\t\tif (!M&&!N&&!S&&!W)break;\n\t\tvector<po>pos;\n\t\tvector<vector<int>>memo;\n\t\t{\n\t\t\t{\n\t\t\t\tint nx, ny, nz;\n\t\t\t\tfor (int i = 0; i < M; ++i) {\n\t\t\t\t\tcin >> nx >> ny >> nz;\n\t\t\t\t\tmemo.push_back(vector<int>{nx, ny, nz});\n\t\t\t\t}\n\t\t\t\tint g = S;\n\t\t\t\tfor (int i = M; i < M + N; ++i) {\n\t\t\t\t\tnx = (g / 7) % 100 + 1;\n\t\t\t\t\tny = (g / 700) % 100 + 1;\n\t\t\t\t\tnz = (g / 70000) % 100 + 1;\n\t\t\t\t\tmemo.push_back(vector<int>{nx, ny, nz});\n\t\t\t\t\tif (g % 2 == 0)g = g / 2;\n\t\t\t\t\telse g = (g / 2) ^ W;\n\t\t\t\t}\n\t\t\t}\n\t\t\tsort(memo.begin(), memo.end(), [](const vector<int>&l, const vector<int>&r) {\n\t\t\t\tfor (int i = 0; i < 3; ++i) {\n\t\t\t\t\tif (l[i] == r[i])continue;\n\t\t\t\t\telse return l[i] < r[i];\n\t\t\t\t}\n\t\t\t\treturn false;\n\t\t\t});\n\t\t\t{\n\t\t\t\tvector<vector<int>>newmemo;\n\t\t\t\tmap<vector<int>, int>amap;\n\t\t\t\tfor (int i = 0; i < memo.size(); ++i) {\n\t\t\t\t\tint agcd = gcd(memo[i]);\n\t\t\t\t\tvector<int>v(3);\n\t\t\t\t\tfor (int j = 0; j < 3; ++j)v[j] = memo[i][j] / agcd;\n\t\t\t\t\tif (amap.find(v) == amap.end() || amap[v] > agcd) {\n\t\t\t\t\t\tamap[v] = agcd;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\tfor (auto m : amap) {\n\t\t\t\t\tauto v = m.first;\n\t\t\t\t\tfor (int i = 0; i < 3; ++i) {\n\t\t\t\t\t\tv[i] *= m.second;\n\t\t\t\t\t}\n\t\t\t\t\tnewmemo.emplace_back(v);\n\t\t\t\t}\n\t\t\t\tmemo = newmemo;\n\t\t\t}\n\t\t\tsort(memo.begin(), memo.end(), [](const vector<int>&l, const vector<int>&r) {\n\t\t\t\tfor (int i = 0; i < 3; ++i) {\n\t\t\t\t\tif (l[i] == r[i])continue;\n\t\t\t\t\telse return l[i] < r[i];\n\t\t\t\t}\n\t\t\t\treturn false;\n\t\t\t});\n\t\t\tfor (int i = 0; i < memo.size(); ++i) {\n\t\t\t\tpo p(i, getunit(memo[i]));\n\t\t\t\tpos.emplace_back(p);\n\t\t\t}\n\t\t}\n\n\t\tauto tree = make_kdtree(pos, 0, pos.size(), 0);\n\t\ttree->getmindis();\n\t\tauto ansp = answer;\n\t\tif (ansp.p.first >ansp.p.second)swap(ansp.p.first, ansp.p.second);\n\t\tfor (int i = 0; i < 3; ++i) {\n\t\t\tcout << memo[answer.p.first][i] << \" \";\n\t\t}\n\t\tfor (int i = 0; i < 3; ++i) {\n\t\t\tcout << memo[answer.p.second][i];\n\t\t\tif (i == 2)cout << endl;\n\t\t\telse cout << \" \";\n\t\t}\n\t\tdelete(tree);\n\t}\n\treturn 0;\n}\n//1524,2763", "accuracy": 1, "time_ms": 6990, "memory_kb": 26888, "score_of_the_acc": -1.2433, "final_rank": 17 }, { "submission_id": "aoj_1340_2046975", "code_snippet": "#include \"bits/stdc++.h\"\nusing namespace std;\nusing ld = long double;\nconst ld eps = 1e-9;\n\nld getdis(vector<ld>&l, vector<ld>&r) {\n\tld sum = 0;\n\tfor (int i = 0; i < 3; ++i) {\n\t\tsum += (l[i] - r[i])*(l[i] - r[i]);\n\t}\n\treturn sqrt(sum);\n}\n\nstruct po {\n\tint index;\n\tvector<ld> coors;\n\tpo(int _d) :coors(_d) {\n\t\tindex = -1;\n\t}\n\tpo(int _index, vector<ld>_coors) :index(_index), coors(_coors) {\n\n\t}\n\tpo() {}\n};\nld getdis(po&lpo, po&rpo) {\n\treturn getdis(lpo.coors, rpo.coors);\n}\ntemplate<class T>\nclass axisSorter {\n\tint k;\npublic:\n\taxisSorter(int _k) : k(_k) {}\n\tbool operator()(const T &a, const T &b) {\n\t\treturn a.coors[k] < b.coors[k];\n\t}\n};\n\n\n\nstruct Ans {\n\tld dis;\n\tpair<int, int>p;\n\n};\n\n\nbool operator<(const Ans&l, const Ans&r) {\n\tif (abs(l.dis - r.dis) < eps) {\n\t\tassert(l.p.first < l.p.second);\n\t\tassert(r.p.first < r.p.second);\n\t\treturn l.p < r.p;\n\t}\n\telse {\n\t\treturn l.dis < r.dis;\n\t}\n\treturn true;\n}\nAns answer;\n\ntemplate<class T, int Dim = 3>\nstruct kdtree {\npublic:\n\tT midpo;\n\tvector<T>pos;\n\tkdtree<T> *ltree, *rtree;\n\tint depth;\n\tint axis;\n\tkdtree(const T &p_) :midpo(p_), ltree(nullptr), rtree(nullptr) {\n\n\t}\n\t~kdtree() {\n\t\tif (ltree != nullptr) {\n\t\t\tdelete(ltree);\n\t\t\tltree = nullptr;\n\t\t}\n\n\t\tif (rtree != nullptr) {\n\t\t\tdelete(rtree);\n\t\t\trtree = nullptr;\n\t\t}\n\t}\n\n\tkdtree(vector<T>&ps_, const int& l, const int& r, const int depth_ = 0) : ltree(nullptr), rtree(nullptr), depth(depth_), axis(depth%Dim) {\n\t\tinit(ps_, l, r);\n\t}\n\t/*vector<T>query(const T & amin, const T&amax) {\n\tvector<T>ans;\n\tbool aok = true;\n\tfor (int i = 0; i < Dim; ++i) {\n\tif (amin.coors[i] <= midpo.coors[i] && midpo.coors[i] <= amax.coors[i]) {\n\n\t}\n\telse {\n\taok = false;\n\tbreak;\n\t}\n\t}\n\tif (aok) {\n\tans.emplace_back(val);\n\t}\n\taxisSorter<T> as(axis);\n\tif (as(midpo, amax) || midpo.coors[axis] == amax.coors[axis]) {\n\tif (rtree != nullptr) {\n\tvector<T>tans(rtree->query(amin, amax));\n\tans.insert(ans.end(), tans.begin(), tans.end());\n\t}\n\t}\n\tif (as(amin, midpo) || midpo.coors[axis] == amin.coors[axis]) {\n\tif (ltree != nullptr) {\n\tvector<T>tans(ltree->query(amin, amax));\n\tans.insert(ans.end(), tans.begin(), tans.end());\n\t}\n\t}\n\treturn ans;\n\t}*/\n\tvoid getmindis() {\n\t\t/*if (ltree != nullptr) {\n\t\tauto p =ltree->getmindis();\n\t\tif (ansnum > p.second) {\n\t\tansnum = p.second;\n\t\tansp = p.first;\n\t\t}\n\t\t}\n\t\tif (rtree != nullptr) {\n\t\tauto p = rtree->getmindis();\n\t\tif (ansnum > p.second) {\n\t\tansnum = p.second;\n\t\tansp = p.first;\n\t\t}\n\t\t}*/\n\n\t\tvector<po>lpos, rpos;\n\t\tld axisvalue = midpo.coors[axis];\n\n\t\tif (ltree != nullptr) {\n\t\t\tfor (auto &po : ltree->pos) {\n\t\t\t\tif (axisvalue>po.coors[axis] - eps * 1000000 && po.coors[axis]>axisvalue - sqrt(answer.dis) - eps * 1000000) {\n\t\t\t\t\tlpos.emplace_back(po);\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\tif (rtree != nullptr) {\n\t\t\tfor (auto &po : rtree->pos) {\n\t\t\t\tif (axisvalue<po.coors[axis] + eps * 1000000 && po.coors[axis]<axisvalue + sqrt(answer.dis) + eps * 1000000) {\n\t\t\t\t\trpos.emplace_back(po);\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\n\t\tfor (auto& lpo : lpos) {\n\t\t\tfor (auto& rpo : rpos) {\n\t\t\t\tld dis = getdis(lpo, rpo);\n\t\t\t\tpair<int, int> p(rpo.index, lpo.index);\n\t\t\t\tif (p.first > p.second)swap(p.first, p.second);\n\t\t\t\tAns nans{ dis,p };\n\t\t\t\tanswer = min(answer, nans);\n\t\t\t}\n\t\t\t{\n\t\t\t\tld dis = getdis(lpo, midpo);\n\t\t\t\tpair<int, int> p(midpo.index, lpo.index);\n\t\t\t\tif (p.first > p.second)swap(p.first, p.second);\n\t\t\t\tAns nans{ dis,p };\n\t\t\t\tanswer = min(answer, nans);\n\t\t\t}\n\t\t}\n\t\tfor (auto& rpo : rpos) {\n\t\t\tld dis = getdis(midpo, rpo);\n\t\t\tpair<int, int> p(midpo.index, rpo.index);\n\t\t\tif (p.first > p.second)swap(p.first, p.second);\n\t\t\tAns nans{ dis,p };\n\t\t\tif (p.first == 1524 && 2763 == p.second) {\n\t\t\t\tint a = 1; a++;\n\t\t\t}\n\t\t\t//if (p.first == 2763 && 1524 == p.second)assert(false);\n\t\t\tanswer = min(answer, nans);\n\t\t\tint b;\n\t\t\tb = 1; b++;\n\t\t}\n\n\n\t}\nprivate:\n\tvoid init(vector<T>&ps, const int& l, const int& r) {\n\t\tif (l >= r) {\n\t\t\treturn;\n\t\t}\n\t\tsort(ps.begin() + l, ps.begin() + r, axisSorter<T>(axis));\n\t\tconst int mid = (l + r) / 2;\n\t\tmidpo = ps[mid];\n\n\t\tltree = make_kdtree(ps, l, mid, depth + 1);\n\t\t\n\t\trtree = make_kdtree(ps, mid + 1, r, depth + 1);\n\t\tgetmindis();\n\t\tif (ltree) {\n\t\t\tdelete(ltree);\n\t\t\tltree = nullptr;\n\t\t}\n\t\tif (rtree) {\n\t\t\tdelete(rtree);\n\t\t\trtree = nullptr;\n\t\t}\n\t\tpos = vector<T>(ps.begin() + l, ps.begin() + r);\n\t}\n};\n\n\n//[l..r)\ntemplate<class T>\nkdtree<T> *make_kdtree(vector<T>&ps_, const int& l, const int& r, const int& depth = 0) {\n\tif (l >= r)return nullptr;\n\telse {\n\t\treturn new kdtree<T>(ps_, l, r, depth);\n\t}\n}\n\nvector<ld>getunit(vector<int>v) {\n\tvector<ld>unit;\n\tld len = 0;\n\tfor (auto a : v) {\n\t\tlen += a*a;\n\t\tunit.emplace_back(ld(a));\n\t}\n\tlen = sqrt(len);\n\tfor (auto&u : unit) {\n\t\tu /= len;\n\t}\n\treturn unit;\n}\nint gcd(int l, int r) {\n\tassert(l > 0 && r > 0);\n\tif (l > r)return gcd(r, l);\n\telse {\n\t\tconst int num = r%l;\n\t\tif (num) {\n\t\t\treturn gcd(l, num);\n\t\t}\n\t\telse {\n\t\t\treturn l;\n\t\t}\n\t}\n}\nint gcd(vector<int>nums) {\n\tif (nums.size() == 1)return nums[0];\n\tint num = nums[0];\n\n\tfor (int i = 1; i < nums.size(); ++i) {\n\t\tnum = gcd(num, nums[i]);\n\t}\n\treturn num;\n}\n\nint main() {\n\twhile (1) {\n\t\tanswer = Ans{ 1e18,make_pair(-1,-1) };\n\t\tint M, N, S, W; cin >> M >> N >> S >> W;\n\t\tif (!M&&!N&&!S&&!W)break;\n\t\tvector<po>pos;\n\t\tvector<vector<int>>memo;\n\t\t{\n\t\t\t{\n\t\t\t\tint nx, ny, nz;\n\t\t\t\tfor (int i = 0; i < M; ++i) {\n\t\t\t\t\tcin >> nx >> ny >> nz;\n\t\t\t\t\tmemo.push_back(vector<int>{nx, ny, nz});\n\t\t\t\t}\n\t\t\t\tint g = S;\n\t\t\t\tfor (int i = M; i < M + N; ++i) {\n\t\t\t\t\tnx = (g / 7) % 100 + 1;\n\t\t\t\t\tny = (g / 700) % 100 + 1;\n\t\t\t\t\tnz = (g / 70000) % 100 + 1;\n\t\t\t\t\tmemo.push_back(vector<int>{nx, ny, nz});\n\t\t\t\t\tif (g % 2 == 0)g = g / 2;\n\t\t\t\t\telse g = (g / 2) ^ W;\n\t\t\t\t}\n\t\t\t}\n\t\t\tsort(memo.begin(), memo.end(), [](const vector<int>&l, const vector<int>&r) {\n\t\t\t\tfor (int i = 0; i < 3; ++i) {\n\t\t\t\t\tif (l[i] == r[i])continue;\n\t\t\t\t\telse return l[i] < r[i];\n\t\t\t\t}\n\t\t\t\treturn false;\n\t\t\t});\n\t\t\t{\n\t\t\t\tvector<vector<int>>newmemo;\n\t\t\t\tmap<vector<int>, int>amap;\n\t\t\t\tfor (int i = 0; i < memo.size(); ++i) {\n\t\t\t\t\tint agcd = gcd(memo[i]);\n\t\t\t\t\tvector<int>v(3);\n\t\t\t\t\tfor (int j = 0; j < 3; ++j)v[j] = memo[i][j] / agcd;\n\t\t\t\t\tif (amap.find(v) == amap.end() || amap[v] > agcd) {\n\t\t\t\t\t\tamap[v] = agcd;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\tfor (auto m : amap) {\n\t\t\t\t\tauto v = m.first;\n\t\t\t\t\tfor (int i = 0; i < 3; ++i) {\n\t\t\t\t\t\tv[i] *= m.second;\n\t\t\t\t\t}\n\t\t\t\t\tnewmemo.emplace_back(v);\n\t\t\t\t}\n\t\t\t\tmemo = newmemo;\n\t\t\t}\n\t\t\tsort(memo.begin(), memo.end(), [](const vector<int>&l, const vector<int>&r) {\n\t\t\t\tfor (int i = 0; i < 3; ++i) {\n\t\t\t\t\tif (l[i] == r[i])continue;\n\t\t\t\t\telse return l[i] < r[i];\n\t\t\t\t}\n\t\t\t\treturn false;\n\t\t\t});\n\t\t\tfor (int i = 0; i < memo.size(); ++i) {\n\t\t\t\tpo p(i, getunit(memo[i]));\n\t\t\t\tpos.emplace_back(p);\n\t\t\t}\n\t\t}\n\n\t\tauto tree = make_kdtree(pos, 0, pos.size(), 0);\n\t\ttree->getmindis();\n\t\tauto ansp = answer;\n\t\tif (ansp.p.first >ansp.p.second)swap(ansp.p.first, ansp.p.second);\n\t\tfor (int i = 0; i < 3; ++i) {\n\t\t\tcout << memo[answer.p.first][i] << \" \";\n\t\t}\n\t\tfor (int i = 0; i < 3; ++i) {\n\t\t\tcout << memo[answer.p.second][i];\n\t\t\tif (i == 2)cout << endl;\n\t\t\telse cout << \" \";\n\t\t}\n\t\tdelete(tree);\n\t}\n\treturn 0;\n}\n//1524,2763", "accuracy": 1, "time_ms": 7820, "memory_kb": 26876, "score_of_the_acc": -1.3542, "final_rank": 18 }, { "submission_id": "aoj_1340_1569119", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\nnamespace ClosestPair{\n\t// DIM=3????£????????¬????????????????????????´?????????getNearPoints()?????´\n\tconst double EPS = 1e-9;\n\tconst int DIM = 3;\n\ttypedef array<double,DIM> P;\n\n\tdouble dist(const P &p1,const P &p2){\n\t\tdouble ans = 0;\n\t\tfor(int i = 0 ; i < DIM ; i++)\n\t\t\tans += (p1[i]-p2[i])*(p1[i]-p2[i]);\n\t\treturn sqrt(ans);\n\t}\n\ttypedef array<int,DIM> Block;\n\ttypedef unordered_map<unsigned long long,vector<int>> Bucket;\n\tBlock getBlock(const P &p,double w){\n\t\tBlock res;\n\t\tfor(int i = 0 ; i < DIM ; i++) res[i] = p[i] / w;\n\t\treturn res;\n\t}\n\tunsigned long long toHash(const Block &bl){\n\t\tunsigned long long h = 0;\n\t\tfor(int i = 0 ; i < DIM ; i++){\n\t\t\th = h * 100000007 + bl[i];\n\t\t}\n\t\treturn h;\n\t}\n\tvector<int> getNearPoints(const Bucket &b,double w,const P &p){\n\t\tvector<int> res;\n\t\tauto key = getBlock(p,w);\n\t\tfor(int x0 = -1 ; x0 <= 1 ; x0++)\n\t\t\tfor(int x1 = -1 ; x1 <= 1 ; x1++){\n\t\t\t\tfor(int x2 = -1 ; x2 <= 1 ; x2++){\n\t\t\t\t\tauto it = b.find(toHash(Block{key[0]+x0,key[1]+x1,key[2]+x2}));\n\t\t\t\t\tif( it != b.end() ){\n\t\t\t\t\t\tfor( int id : it->second ) res.push_back(id);\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\treturn res;\n\t}\n\t//???w???n?¬?????????±????????????????????????????????¨????????°??¨??????????????????????±????????????¨?????§?????????\n\tBucket makeBucket(const vector<P> &ps,double w){\n\t\tBucket b;\n\t\tfor(int i = 0 ; i < ps.size() ; i++)\n\t\t\tb[toHash(getBlock(ps[i],w))].push_back(i);\n\t\treturn b;\n\t}\n\t// ????????????????????¢????????????????????±???????¬??§??????? map????????¨?????????????????§???????¨???????O(n log n)\n\t// ?????±??????????????????????§???????????????????????¢????????£?????????????????????????????????§??????????????§???????????????2w??????????????????????????????w???????????????????\\??????????\n\tdouble closetPairDistance(vector<P> ps){\n\t\tassert(ps.size()>=2);\n\t\trandom_shuffle(ps.begin(),ps.end());\n\t\tdouble w = dist(ps[0],ps[1]);\n\t\tvector<P> build = {ps[0],ps[1]};\n\t\tauto b = makeBucket(build,w);\n\t\tint a = 0;\n\t\tfor(int i = 2 ; i < ps.size() ; i++){\n\t\t\tdouble next_w = w;\n\t\t\tfor( int j : getNearPoints(b,w,ps[i]) )\n\t\t\t\tnext_w = min(next_w,dist(ps[i],ps[j]));\n\t\t\tbuild.push_back(ps[i]);\n\t\t\tif( w - EPS > next_w ){\n\t\t\t\tw = next_w;\n\t\t\t\tb = makeBucket(build,w);\n\t\t\t\ta++;\n\t\t\t}else{\n\t\t\t\tb[toHash(getBlock(ps[i],w))].push_back(i);\n\t\t\t}\n\t\t}\n\t\treturn w;\n\t}\n};\nusing namespace ClosestPair;\n\n\nstruct Pt{\n\tdouble x,y,z;\n};\nbool operator < (const Pt &a,const Pt &b){\n\tif( fabs(a.x-b.x) > EPS ) return a.x < b.x;\n\tif( fabs(a.y-b.y) > EPS ) return a.y < b.y;\n\tif( fabs(a.z-b.z) > EPS ) return a.z < b.z;\n\treturn false;\n}\n\ndouble X[150000];\ndouble Y[150000];\ndouble Z[150000];\nint main(){\n int m,n,S,W;\n int tst = 1;\n while( cin >> m >> n >> S >> W && m+n ){\n map<Pt,vector<P>> dic;\n for(int i = 0 ; i < m ; i++){\n scanf(\"%lf%lf%lf\",X+i,Y+i,Z+i);\n }\n int g = S;\n for(int i=m; i<m+n; i++) {\n X[i] = (g/7) %100 + 1;\n Y[i] = (g/700) %100 + 1;\n Z[i] = (g/70000)%100 + 1;\n if( g%2 == 0 ) { g = (g/2); }\n else { g = (g/2) ^ W; }\n }\n for(int i = 0 ; i < m+n ; i++){\n double x = X[i];\n double y = Y[i];\n double z = Z[i];\n P p = {x,y,z};\n double d = dist(P{0,0,0},p);\n Pt e;\n e.x = x / d;\n e.y = y / d;\n e.z = z / d;\n dic[e].push_back(p);\n }\n vector<P> ps;\n vector<P> cand;\n for( auto &p : dic ){\n ps.push_back(P{p.first.x,p.first.y,p.first.z});\n \n cand.push_back(*min_element(p.second.begin(),p.second.end()));\n }\n \n double w = closetPairDistance(ps);\n auto b = makeBucket(ps,w);\n pair<P,P> best = {P{1e9,1e9,1e9},P{1e9,1e9,1e9}};\n for(int i = 0 ; i < ps.size() ; i++){\n for( auto j : getNearPoints(b,w,ps[i]) ){\n if( i != j ){\n if( fabs(dist(ps[i],ps[j])-w) < EPS ){\n best = min(best,pair<P,P>{cand[i],cand[j]});\n }\n }\n }\n }\n cout << (int)best.first[0] << \" \" << (int)best.first[1] << \" \" << (int)best.first[2] << \" \";\n cout << (int)best.second[0] << \" \" << (int)best.second[1] << \" \" << (int)best.second[2] << \"\\n\";\n }\n \n \n}", "accuracy": 1, "time_ms": 4330, "memory_kb": 41448, "score_of_the_acc": -1.1184, "final_rank": 11 }, { "submission_id": "aoj_1340_1569104", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\nnamespace ClosestPair{\n\t// DIM=3????£????????¬????????????????????????´?????????getNearPoints()makeBucket()?????´\n\tconst double EPS = 1e-9;\n\tconst int DIM = 3;\n\ttypedef array<double,DIM> P;\n\n\tdouble dist(const P &p1,const P &p2){\n\t\tdouble ans = 0;\n\t\tfor(int i = 0 ; i < DIM ; i++)\n\t\t\tans += (p1[i]-p2[i])*(p1[i]-p2[i]);\n\t\treturn sqrt(ans);\n\t}\n\ttypedef array<int,DIM> Block;\n\ttypedef map<Block,vector<int>> Bucket;\n\tBlock getBlock(const P &p,double w){\n\t\tBlock res;\n\t\tfor(int i = 0 ; i < DIM ; i++) res[i] = p[i] / w;\n\t\treturn res;\n\t}\n\tvector<int> getNearPoints(const Bucket &b,double w,const P &p){\n\t\tvector<int> res;\n\t\tauto key = getBlock(p,w);\n\t\tfor(int x0 = -1 ; x0 <= 1 ; x0++)\n\t\t\tfor(int x1 = -1 ; x1 <= 1 ; x1++)\n\t\t\t\tfor(int x2 = -1 ; x2 <= 1 ; x2++){\n\t\t\t\t\tauto it = b.find(Block{key[0]+x0,key[1]+x1,key[2]+x2});\n\t\t\t\t\tif( it != b.end() ){\n\t\t\t\t\t\tfor( int id : it->second ) res.push_back(id);\n\t\t\t\t\t}\n\t\t\t\t}\n\t\treturn res;\n\t}\n\t//???w???n?¬?????????±????????????????????????????????¨????????°??¨??????????????????????±????????????¨?????§?????????\n\tBucket makeBucket(const vector<P> &ps,double w){\n\t\tBucket b;\n\t\tfor(int i = 0 ; i < ps.size() ; i++)\n\t\t\tb[getBlock(ps[i],w)].push_back(i);\n\t\treturn b;\n\t}\n\t// ????????????????????¢????????????????????±???????¬??§??????? map????????¨?????????????????§???????¨???????O(n log n)\n\t// ?????±??????????????????????§???????????????????????¢????????£?????????????????????????????????§??????????????§???????????????2w??????????????????????????????w???????????????????\\??????????\n\tdouble closetPairDistance(vector<P> ps){\n\t\tdouble w = 1e9;\n\t\tfor( auto &p : ps ){\n\t\t\ttie(p[0],p[1]) = make_pair(p[0]*cos(0)-p[1]*sin(0),p[0]*sin(0)+p[1]*cos(0));\n\t\t}\n\t\tsort(ps.begin(),ps.end());\n\t\tfor(int i = 0 ; i < ps.size() ; i++){\n\t\t\tfor(int j = i-1 ; j >= 0 ; j--){\n\t\t\t\tif( ps[i][0]-ps[j][0] >= w ) break;\n\t\t\t\tw = min(w,dist(ps[i],ps[j]));\n\t\t\t}\n\t\t}\n\t\treturn w;\n\t}\n};\n\nusing namespace ClosestPair;\n\n\nstruct Pt{\n\tdouble x,y,z;\n};\nbool operator < (const Pt &a,const Pt &b){\n\tif( fabs(a.x-b.x) > EPS ) return a.x < b.x;\n\tif( fabs(a.y-b.y) > EPS ) return a.y < b.y;\n\tif( fabs(a.z-b.z) > EPS ) return a.z < b.z;\n\treturn false;\n}\n\ndouble X[150000];\ndouble Y[150000];\ndouble Z[150000];\n\nint main(){\n\tint m,n,S,W;\n\tint tst = 1;\n\twhile( cin >> m >> n >> S >> W && m+n ){\n\t\tmap<Pt,vector<P>> dic;\n\t\tfor(int i = 0 ; i < m ; i++){\n\t\t\tscanf(\"%lf%lf%lf\",X+i,Y+i,Z+i);\n\t\t}\n\t\tint g = S;\n\t\tfor(int i=m; i<m+n; i++) {\n\t\t X[i] = (g/7) %100 + 1;\n\t\t Y[i] = (g/700) %100 + 1;\n\t\t Z[i] = (g/70000)%100 + 1;\n\t\t if( g%2 == 0 ) { g = (g/2); }\n\t\t else { g = (g/2) ^ W; }\n\t\t}\n\t\tfor(int i = 0 ; i < m+n ; i++){\n\t\t\tdouble x = X[i];\n\t\t\tdouble y = Y[i];\n\t\t\tdouble z = Z[i];\n\t\t\tP p = {x,y,z};\n\t\t\tdouble d = dist(P{0,0,0},p);\n\t\t\tPt e;\n\t\t\te.x = x / d;\n\t\t\te.y = y / d;\n\t\t\te.z = z / d;\n\t\t\tdic[e].push_back(p);\n\t\t}\n\t\tvector<P> ps;\n\t\tvector<P> cand;\n\t\tfor( auto &p : dic ){\n\t\t\tps.push_back(P{p.first.x,p.first.y,p.first.z});\n\t\t\t\n\t\t\tcand.push_back(*min_element(p.second.begin(),p.second.end()));\n\t\t}\n\t\n\t\tdouble w = closetPairDistance(ps);\n\t\tauto b = makeBucket(ps,2*w);\n\t\tpair<P,P> best = {P{1e9,1e9,1e9},P{1e9,1e9,1e9}};\n\t\tfor(int i = 0 ; i < ps.size() ; i++){\n\t\t\tfor( auto j : getNearPoints(b,2*w,ps[i]) ){\n\t\t\t\tif( i != j ){\n\t\t\t\t\tif( fabs(dist(ps[i],ps[j])-w) < EPS ){\n\t\t\t\t\t\tbest = min(best,pair<P,P>{cand[i],cand[j]});\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\tcout << (int)best.first[0] << \" \" << (int)best.first[1] << \" \" << (int)best.first[2] << \" \";\n\t\tcout << (int)best.second[0] << \" \" << (int)best.second[1] << \" \" << (int)best.second[2] << \"\\n\";\n\t}\n\t\n\n}", "accuracy": 1, "time_ms": 3580, "memory_kb": 31536, "score_of_the_acc": -0.8606, "final_rank": 9 }, { "submission_id": "aoj_1340_1569103", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\nnamespace ClosestPair{\n\t// DIM=3????£????????¬????????????????????????´?????????getNearPoints()makeBucket()?????´\n\tconst double EPS = 1e-9;\n\tconst int DIM = 3;\n\ttypedef array<double,DIM> P;\n\n\tdouble dist(const P &p1,const P &p2){\n\t\tdouble ans = 0;\n\t\tfor(int i = 0 ; i < DIM ; i++)\n\t\t\tans += (p1[i]-p2[i])*(p1[i]-p2[i]);\n\t\treturn sqrt(ans);\n\t}\n\ttypedef array<int,DIM> Block;\n\ttypedef map<Block,vector<int>> Bucket;\n\tBlock getBlock(const P &p,double w){\n\t\tBlock res;\n\t\tfor(int i = 0 ; i < DIM ; i++) res[i] = p[i] / w;\n\t\treturn res;\n\t}\n\tvector<int> getNearPoints(const Bucket &b,double w,const P &p){\n\t\tvector<int> res;\n\t\tauto key = getBlock(p,w);\n\t\tfor(int x0 = -1 ; x0 <= 1 ; x0++)\n\t\t\tfor(int x1 = -1 ; x1 <= 1 ; x1++)\n\t\t\t\tfor(int x2 = -1 ; x2 <= 1 ; x2++){\n\t\t\t\t\tauto it = b.find(Block{key[0]+x0,key[1]+x1,key[2]+x2});\n\t\t\t\t\tif( it != b.end() ){\n\t\t\t\t\t\tfor( int id : it->second ) res.push_back(id);\n\t\t\t\t\t}\n\t\t\t\t}\n\t\treturn res;\n\t}\n\t//???w???n?¬?????????±????????????????????????????????¨????????°??¨??????????????????????±????????????¨?????§?????????\n\tBucket makeBucket(const vector<P> &ps,double w){\n\t\tBucket b;\n\t\tfor(int i = 0 ; i < ps.size() ; i++)\n\t\t\tb[getBlock(ps[i],w)].push_back(i);\n\t\treturn b;\n\t}\n\t// ????????????????????¢????????????????????±???????¬??§??????? map????????¨?????????????????§???????¨???????O(n log n)\n\t// ?????±??????????????????????§???????????????????????¢????????£?????????????????????????????????§??????????????§???????????????2w??????????????????????????????w???????????????????\\??????????\n\tdouble closetPairDistance(vector<P> ps){\n\t\tdouble w = 1e9;\n\t\tfor( auto &p : ps ){\n\t\t\ttie(p[0],p[1]) = make_pair(p[0]*cos(2)-p[1]*sin(2),p[0]*sin(2)+p[1]*cos(2));\n\t\t}\n\t\tsort(ps.begin(),ps.end());\n\t\tfor(int i = 0 ; i < ps.size() ; i++){\n\t\t\tfor(int j = i-1 ; j >= 0 ; j--){\n\t\t\t\tif( ps[i][0]-ps[j][0] >= w ) break;\n\t\t\t\tw = min(w,dist(ps[i],ps[j]));\n\t\t\t}\n\t\t}\n\t\treturn w;\n\t}\n};\n\nusing namespace ClosestPair;\n\n\nstruct Pt{\n\tdouble x,y,z;\n};\nbool operator < (const Pt &a,const Pt &b){\n\tif( fabs(a.x-b.x) > EPS ) return a.x < b.x;\n\tif( fabs(a.y-b.y) > EPS ) return a.y < b.y;\n\tif( fabs(a.z-b.z) > EPS ) return a.z < b.z;\n\treturn false;\n}\n\ndouble X[150000];\ndouble Y[150000];\ndouble Z[150000];\n\nint main(){\n\tint m,n,S,W;\n\tint tst = 1;\n\twhile( cin >> m >> n >> S >> W && m+n ){\n\t\tmap<Pt,vector<P>> dic;\n\t\tfor(int i = 0 ; i < m ; i++){\n\t\t\tscanf(\"%lf%lf%lf\",X+i,Y+i,Z+i);\n\t\t}\n\t\tint g = S;\n\t\tfor(int i=m; i<m+n; i++) {\n\t\t X[i] = (g/7) %100 + 1;\n\t\t Y[i] = (g/700) %100 + 1;\n\t\t Z[i] = (g/70000)%100 + 1;\n\t\t if( g%2 == 0 ) { g = (g/2); }\n\t\t else { g = (g/2) ^ W; }\n\t\t}\n\t\tfor(int i = 0 ; i < m+n ; i++){\n\t\t\tdouble x = X[i];\n\t\t\tdouble y = Y[i];\n\t\t\tdouble z = Z[i];\n\t\t\tP p = {x,y,z};\n\t\t\tdouble d = dist(P{0,0,0},p);\n\t\t\tPt e;\n\t\t\te.x = x / d;\n\t\t\te.y = y / d;\n\t\t\te.z = z / d;\n\t\t\tdic[e].push_back(p);\n\t\t}\n\t\tvector<P> ps;\n\t\tvector<P> cand;\n\t\tfor( auto &p : dic ){\n\t\t\tps.push_back(P{p.first.x,p.first.y,p.first.z});\n\t\t\t\n\t\t\tcand.push_back(*min_element(p.second.begin(),p.second.end()));\n\t\t}\n\t\n\t\tdouble w = closetPairDistance(ps);\n\t\tauto b = makeBucket(ps,2*w);\n\t\tpair<P,P> best = {P{1e9,1e9,1e9},P{1e9,1e9,1e9}};\n\t\tfor(int i = 0 ; i < ps.size() ; i++){\n\t\t\tfor( auto j : getNearPoints(b,2*w,ps[i]) ){\n\t\t\t\tif( i != j ){\n\t\t\t\t\tif( fabs(dist(ps[i],ps[j])-w) < EPS ){\n\t\t\t\t\t\tbest = min(best,pair<P,P>{cand[i],cand[j]});\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\tcout << (int)best.first[0] << \" \" << (int)best.first[1] << \" \" << (int)best.first[2] << \" \";\n\t\tcout << (int)best.second[0] << \" \" << (int)best.second[1] << \" \" << (int)best.second[2] << \"\\n\";\n\t}\n\t\n\n}", "accuracy": 1, "time_ms": 3670, "memory_kb": 31612, "score_of_the_acc": -0.8738, "final_rank": 10 }, { "submission_id": "aoj_1340_1569098", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\nnamespace ClosestPair{\n\t// DIM=3????£????????¬????????????????????????´?????????getNearPoints()makeBucket()?????´\n\tconst double EPS = 1e-9;\n\tconst int DIM = 3;\n\ttypedef array<double,DIM> P;\n\n\tdouble dist(const P &p1,const P &p2){\n\t\tdouble ans = 0;\n\t\tfor(int i = 0 ; i < DIM ; i++)\n\t\t\tans += (p1[i]-p2[i])*(p1[i]-p2[i]);\n\t\treturn sqrt(ans);\n\t}\n\ttypedef array<int,DIM> Block;\n\ttypedef map<Block,vector<int>> Bucket;\n\tBlock getBlock(const P &p,double w){\n\t\tBlock res;\n\t\tfor(int i = 0 ; i < DIM ; i++) res[i] = p[i] / w;\n\t\treturn res;\n\t}\n\tvector<int> getNearPoints(const Bucket &b,double w,const P &p){\n\t\tvector<int> res;\n\t\tauto key = getBlock(p,w);\n\t\tfor(int x0 = -1 ; x0 <= 1 ; x0++)\n\t\t\tfor(int x1 = -1 ; x1 <= 1 ; x1++)\n\t\t\t\tfor(int x2 = -1 ; x2 <= 1 ; x2++){\n\t\t\t\t\tauto it = b.find(Block{key[0]+x0,key[1]+x1,key[2]+x2});\n\t\t\t\t\tif( it != b.end() ){\n\t\t\t\t\t\tfor( int id : it->second ) res.push_back(id);\n\t\t\t\t\t}\n\t\t\t\t}\n\t\treturn res;\n\t}\n\t//???w???n?¬?????????±????????????????????????????????¨????????°??¨??????????????????????±????????????¨?????§?????????\n\tBucket makeBucket(const vector<P> &ps,double w){\n\t\tBucket b;\n\t\tfor(int i = 0 ; i < ps.size() ; i++)\n\t\t\tb[getBlock(ps[i],w)].push_back(i);\n\t\treturn b;\n\t}\n\t// ????????????????????¢????????????????????±???????¬??§??????? map????????¨?????????????????§???????¨???????O(n log n)\n\t// ?????±??????????????????????§???????????????????????¢????????£?????????????????????????????????§??????????????§???????????????2w??????????????????????????????w???????????????????\\??????????\n\tdouble closetPairDistance(vector<P> ps){\n\t\tassert(ps.size()>=2);\n\t\trandom_shuffle(ps.begin(),ps.end());\n\t\tdouble w = dist(ps[0],ps[1]);\n\t\tvector<P> build = {ps[0],ps[1]};\n\t\tauto b = makeBucket(build,2*w);\n\t\tfor(int i = 2 ; i < ps.size() ; i++){\n\t\t\tdouble next_w = w;\n\t\t\tfor( int j : getNearPoints(b,2*w,ps[i]) )\n\t\t\t\tnext_w = min(next_w,dist(ps[i],ps[j]));\n\t\t\tbuild.push_back(ps[i]);\n\t\t\tif( w - EPS > next_w ){\n\t\t\t\tw = next_w;\n\t\t\t\tb = makeBucket(build,2*w);\n\t\t\t}else{\n\t\t\t\tb[getBlock(ps[i],2*w)].push_back(i);\n\t\t\t}\n\t\t}\n\t\treturn w;\n\t}\n};\n\nusing namespace ClosestPair;\n\n\nstruct Pt{\n\tdouble x,y,z;\n};\nbool operator < (const Pt &a,const Pt &b){\n\tif( fabs(a.x-b.x) > EPS ) return a.x < b.x;\n\tif( fabs(a.y-b.y) > EPS ) return a.y < b.y;\n\tif( fabs(a.z-b.z) > EPS ) return a.z < b.z;\n\treturn false;\n}\n\ndouble X[150000];\ndouble Y[150000];\ndouble Z[150000];\n\nint main(){\n\tint m,n,S,W;\n\tint tst = 1;\n\twhile( cin >> m >> n >> S >> W && m+n ){\n\t\tmap<Pt,vector<P>> dic;\n\t\tfor(int i = 0 ; i < m ; i++){\n\t\t\tscanf(\"%lf%lf%lf\",X+i,Y+i,Z+i);\n\t\t}\n\t\tint g = S;\n\t\tfor(int i=m; i<m+n; i++) {\n\t\t X[i] = (g/7) %100 + 1;\n\t\t Y[i] = (g/700) %100 + 1;\n\t\t Z[i] = (g/70000)%100 + 1;\n\t\t if( g%2 == 0 ) { g = (g/2); }\n\t\t else { g = (g/2) ^ W; }\n\t\t}\n\t\tfor(int i = 0 ; i < m+n ; i++){\n\t\t\tdouble x = X[i];\n\t\t\tdouble y = Y[i];\n\t\t\tdouble z = Z[i];\n\t\t\tP p = {x,y,z};\n\t\t\tdouble d = dist(P{0,0,0},p);\n\t\t\tPt e;\n\t\t\te.x = x / d;\n\t\t\te.y = y / d;\n\t\t\te.z = z / d;\n\t\t\tdic[e].push_back(p);\n\t\t}\n\t\tvector<P> ps;\n\t\tvector<P> cand;\n\t\tfor( auto &p : dic ){\n\t\t\tps.push_back(P{p.first.x,p.first.y,p.first.z});\n\t\t\t\n\t\t\tcand.push_back(*min_element(p.second.begin(),p.second.end()));\n\t\t}\n\t\n\t\tdouble w = closetPairDistance(ps);\n\t\tauto b = makeBucket(ps,2*w);\n\t\tpair<P,P> best = {P{1e9,1e9,1e9},P{1e9,1e9,1e9}};\n\t\tfor(int i = 0 ; i < ps.size() ; i++){\n\t\t\tfor( auto j : getNearPoints(b,2*w,ps[i]) ){\n\t\t\t\tif( i != j ){\n\t\t\t\t\tif( fabs(dist(ps[i],ps[j])-w) < EPS ){\n\t\t\t\t\t\tbest = min(best,pair<P,P>{cand[i],cand[j]});\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\tcout << (int)best.first[0] << \" \" << (int)best.first[1] << \" \" << (int)best.first[2] << \" \";\n\t\tcout << (int)best.second[0] << \" \" << (int)best.second[1] << \" \" << (int)best.second[2] << \"\\n\";\n\t\t\n\t}\n\t\n\n}", "accuracy": 1, "time_ms": 7430, "memory_kb": 44408, "score_of_the_acc": -1.5804, "final_rank": 20 } ]
aoj_1347_cpp
Problem C: Shopping Your friend will enjoy shopping. She will walk through a mall along a straight street, where $N$ individual shops (numbered from 1 to $N$) are aligned at regular intervals. Each shop has one door and is located at the one side of the street. The distances between the doors of the adjacent shops are the same length, i.e. a unit length. Starting shopping at the entrance of the mall, she visits shops in order to purchase goods. She has to go to the exit of the mall after shopping. She requires some restrictions on visiting order of shops. Each of the restrictions indicates that she shall visit a shop before visiting another shop. For example, when she wants to buy a nice dress before choosing heels, she shall visit a boutique before visiting a shoe store. When the boutique is farther than the shoe store, she must pass the shoe store before visiting the boutique, and go back to the shoe store after visiting the boutique. If only the order of the visiting shops satisfies all the restrictions, she can visit other shops in any order she likes. Write a program to determine the minimum required walking length for her to move from the entrance to the exit. Assume that the position of the door of the shop numbered $k$ is $k$ units far from the entrance, where the position of the exit is $N + 1$ units far from the entrance. Input The input consists of a single test case. $N$ $m$ $c_1$ $d_1$ . . . $c_m$ $d_m$ The first line contains two integers $N$ and $m$, where $N$ ($1 \leq N \leq 1000$) is the number of shops, and $m$ ($0 \leq m \leq 500$) is the number of restrictions. Each of the next $m$ lines contains two integers $c_i$ and $d_i$ ($1 \leq c_i < d_i \leq N$) indicating the $i$-th restriction on the visiting order, where she must visit the shop numbered $c_i$ after she visits the shop numbered $d_i$ ($i = 1, . . . , m$). There are no pair of $j$ and $k$ that satisfy $c_j = c_k$ and $d_j = d_k$. Output Output the minimum required walking length for her to move from the entrance to the exit. You should omit the length of her walk in the insides of shops. Sample Input 1 10 3 3 7 8 9 2 5 Sample Output 1 23 Sample Input 2 10 3 8 9 6 7 2 4 Sample Output 2 19 Sample Input 3 10 0 Sample Output 3 11 Sample Input 4 10 6 6 7 4 5 2 5 6 9 3 5 6 8 Sample Output 4 23 Sample Input 5 1000 8 3 4 6 1000 5 1000 7 1000 8 1000 4 1000 9 1000 1 2 Sample Output 5 2997
[ { "submission_id": "aoj_1347_2717021", "code_snippet": "#include <iostream>\n#include <string>\n#include <vector>\n#include <algorithm>\n#include <cmath>\n#include <cstdio>\n#include <functional>\n#include <numeric>\n#include <stack>\n#include <queue>\n#include <map>\n#include <set>\n#include <utility>\n#include <sstream>\n#include <complex>\n#include <fstream>\n#include <bitset>\n#include <time.h>\n#include <tuple>\n#include <iomanip>\n\nusing namespace std;\n\ntypedef long long ll;\ntypedef pair<ll, ll> P;\ntypedef vector<ll> V;\ntypedef complex<double> Point;\n\n#define PI acos(-1.0)\n#define EPS 1e-10\nconst ll INF = 1e12;\nconst ll MOD = 1e9 + 7;\n\n#define FOR(i,a,b) for(int i=(a);i<(b);i++)\n#define rep(i,N) for(int i=0;i<(N);i++)\n#define ALL(s) (s).begin(),(s).end()\n#define EQ(a,b) (abs((a)-(b))<EPS)\n#define EQV(a,b) ( EQ((a).real(), (b).real()) && EQ((a).imag(), (b).imag()) )\n#define fi first\n#define se second\n#define N_SIZE (1LL << 20)\n#define NIL -1\n\nll sq(ll num) { return num*num; }\nll mod_pow(ll x, ll n) {\n\tif (n == 0)return 1;\n\tif (n == 1)return x%MOD;\n\tll res = sq(mod_pow(x, n / 2));\n\tres %= MOD;\n\tif (n % 2 == 1) {\n\t\tres *= x;\n\t\tres %= MOD;\n\t}\n\treturn res;\n}\nll mod_add(ll a, ll b) { return (a + b) % MOD; }\nll mod_sub(ll a, ll b) { return (a - b + MOD) % MOD; }\nll mod_mul(ll a, ll b) { return a*b % MOD; }\n\n//dp\nint n, m;\nvector<P> vp2;\nll dp[510][1100];\n\nint main() {\n\tcin >> n >> m;\n\tif (m == 0) {\n\t\tcout << n + 1 << endl;\n\t\treturn 0;\n\t}\n\trep(i, m) {\n\t\tll c, d;\n\t\tcin >> c >> d;\n\t\tvp2.push_back({ c,d });\n\t}\n\tsort(ALL(vp2));\n\trep(i, 510)rep(j, 1010)dp[i][j] = INF;\n\tdp[0][0] = 0;\n\tint now = 0;\n\trep(i, vp2.size()) {\n\t\tll ci = vp2[i].first;\n\t\tll di = vp2[i].second;\n\t\tFOR(j, di, n + 2) {//新しく経由する点\n\t\t\tFOR(k, now, n + 2) {//今までに経由した点\n\t\t\t\tif (k < j)dp[i + 1][j] = min(dp[i + 1][j], dp[i][k] + (j - now) + (j - ci));\n\t\t\t\telse dp[i + 1][j] = min(dp[i + 1][j], dp[i][j] + (ci - now));\n\t\t\t}\n\t\t}\n\t\tnow = ci;\n\t}\n\tll ans = INF;\n\trep(i, n + 2) {\n\t\tans = min(dp[vp2.size()][i] + n + 1 - now, ans);\n\t}\n\tcout << ans << endl;\n}\n\n//int n, m;\n//vector<P> vp;\n//int sum[100100];\n//\n//int main() {\n//\tcin >> n >> m;\n//\tvp.resize(m);\n//\trep(i, m) {\n//\t\tint a, b;\n//\t\tcin >> a >> b;\n//\t\tsum[a]++;\n//\t\tsum[b]--;\n//\t}\n//\trep(i, n + 2)sum[i + 1] += sum[i];\n//\tll ans = 0;\n//\trep(i, n + 1) {\n//\t\tif (sum[i] > 0)ans += 3;\n//\t\telse ans++;\n//\t}\n//\tcout << ans << endl;\n//}", "accuracy": 1, "time_ms": 460, "memory_kb": 7516, "score_of_the_acc": -0.6185, "final_rank": 3 }, { "submission_id": "aoj_1347_2717020", "code_snippet": "#include <iostream>\n#include <string>\n#include <vector>\n#include <algorithm>\n#include <cmath>\n#include <cstdio>\n#include <functional>\n#include <numeric>\n#include <stack>\n#include <queue>\n#include <map>\n#include <set>\n#include <utility>\n#include <sstream>\n#include <complex>\n#include <fstream>\n#include <bitset>\n#include <time.h>\n#include <tuple>\n#include <iomanip>\n\nusing namespace std;\n\ntypedef long long ll;\ntypedef pair<ll, ll> P;\ntypedef vector<ll> V;\ntypedef complex<double> Point;\n\n#define PI acos(-1.0)\n#define EPS 1e-10\nconst ll INF = 1e12;\nconst ll MOD = 1e9 + 7;\n\n#define FOR(i,a,b) for(int i=(a);i<(b);i++)\n#define rep(i,N) for(int i=0;i<(N);i++)\n#define ALL(s) (s).begin(),(s).end()\n#define EQ(a,b) (abs((a)-(b))<EPS)\n#define EQV(a,b) ( EQ((a).real(), (b).real()) && EQ((a).imag(), (b).imag()) )\n#define fi first\n#define se second\n#define N_SIZE (1LL << 20)\n#define NIL -1\n\nll sq(ll num) { return num*num; }\nll mod_pow(ll x, ll n) {\n\tif (n == 0)return 1;\n\tif (n == 1)return x%MOD;\n\tll res = sq(mod_pow(x, n / 2));\n\tres %= MOD;\n\tif (n % 2 == 1) {\n\t\tres *= x;\n\t\tres %= MOD;\n\t}\n\treturn res;\n}\nll mod_add(ll a, ll b) { return (a + b) % MOD; }\nll mod_sub(ll a, ll b) { return (a - b + MOD) % MOD; }\nll mod_mul(ll a, ll b) { return a*b % MOD; }\n\n//dp\nint n, m;\nvector<P> vp;\nll dp[510][1100];\n\nint main() {\n\tcin >> n >> m;\n\tif (m == 0) {\n\t\tcout << n + 1 << endl;\n\t\treturn 0;\n\t}\n\trep(i, m) {\n\t\tll c, d;\n\t\tcin >> c >> d;\n\t\tvp.push_back({ c,d });\n\t}\n\tsort(ALL(vp));\n\tvector<P> vp2;\n\tll d = -1;\n\trep(i, vp.size()) {\n\t\t//if (d < vp[i].second) {\n\t\t//\td = vp[i].second;\n\t\t\tvp2.push_back(vp[i]);\n\t\t//}\n\t}\n\trep(i, 510)rep(j, 1010)dp[i][j] = INF;\n\tdp[0][0] = 0;\n\tint now = 0;\n\trep(i, vp2.size()) {\n\t\tll ci = vp2[i].first;\n\t\tll di = vp2[i].second;\n\t\tFOR(j, di, n + 2) {//新しく経由する点\n\t\t\tFOR(k, now, n + 2) {//今までに経由した点\n\t\t\t\tif (k < j)dp[i + 1][j] = min(dp[i + 1][j], dp[i][k] + (j - now) + (j - ci));\n\t\t\t\telse dp[i + 1][j] = min(dp[i + 1][j], dp[i][j] + (ci - now));\n\t\t\t}\n\t\t}\n\t\tnow = ci;\n\t}\n\t//FOR(i, 1, m + 1) {\n\t//\tif (dp[i][5] == INF) {\n\t//\t\tcout << i << endl;\n\t//\t\tbreak;\n\t//\t}\n\t//}\n\t//rep(i, vp2.size() + 1) {\n\t//\tif (i != vp2.size())continue;\n\t//\trep(j, n + 1) {\n\t//\t\tif (dp[i][j] != INF)printf(\"%4d\", dp[i][j]);\n\t//\t\telse cout << \" INF\";\n\t//\t}\n\t//\tcout << endl;\n\t//}\n\tll ans = INF;\n\trep(i, n + 2) {\n\t\tans = min(dp[vp2.size()][i] + n + 1 - now, ans);\n\t}\n\tcout << ans << endl;\n\t//cout << dp[vp2.size()][d] + n + 1 - now << endl;\n}\n\n//int n, m;\n//vector<P> vp;\n//int sum[100100];\n//\n//int main() {\n//\tcin >> n >> m;\n//\tvp.resize(m);\n//\trep(i, m) {\n//\t\tint a, b;\n//\t\tcin >> a >> b;\n//\t\tsum[a]++;\n//\t\tsum[b]--;\n//\t}\n//\trep(i, n + 2)sum[i + 1] += sum[i];\n//\tll ans = 0;\n//\trep(i, n + 1) {\n//\t\tif (sum[i] > 0)ans += 3;\n//\t\telse ans++;\n//\t}\n//\tcout << ans << endl;\n//}", "accuracy": 1, "time_ms": 460, "memory_kb": 7532, "score_of_the_acc": -0.6188, "final_rank": 4 }, { "submission_id": "aoj_1347_2717019", "code_snippet": "#include <iostream>\n#include <string>\n#include <vector>\n#include <algorithm>\n#include <cmath>\n#include <cstdio>\n#include <functional>\n#include <numeric>\n#include <stack>\n#include <queue>\n#include <map>\n#include <set>\n#include <utility>\n#include <sstream>\n#include <complex>\n#include <fstream>\n#include <bitset>\n#include <time.h>\n#include <tuple>\n#include <iomanip>\n\nusing namespace std;\n\ntypedef long long ll;\ntypedef pair<ll, ll> P;\ntypedef vector<ll> V;\ntypedef complex<double> Point;\n\n#define PI acos(-1.0)\n#define EPS 1e-10\nconst ll INF = 1e12;\nconst ll MOD = 1e9 + 7;\n\n#define FOR(i,a,b) for(int i=(a);i<(b);i++)\n#define rep(i,N) for(int i=0;i<(N);i++)\n#define ALL(s) (s).begin(),(s).end()\n#define EQ(a,b) (abs((a)-(b))<EPS)\n#define EQV(a,b) ( EQ((a).real(), (b).real()) && EQ((a).imag(), (b).imag()) )\n#define fi first\n#define se second\n#define N_SIZE (1LL << 20)\n#define NIL -1\n\nll sq(ll num) { return num*num; }\nll mod_pow(ll x, ll n) {\n\tif (n == 0)return 1;\n\tif (n == 1)return x%MOD;\n\tll res = sq(mod_pow(x, n / 2));\n\tres %= MOD;\n\tif (n % 2 == 1) {\n\t\tres *= x;\n\t\tres %= MOD;\n\t}\n\treturn res;\n}\nll mod_add(ll a, ll b) { return (a + b) % MOD; }\nll mod_sub(ll a, ll b) { return (a - b + MOD) % MOD; }\nll mod_mul(ll a, ll b) { return a*b % MOD; }\n\n//dp\nint n, m;\nvector<P> vp;\nll dp[510][1100];\n\nint main() {\n\tcin >> n >> m;\n\tif (m == 0) {\n\t\tcout << n + 1 << endl;\n\t\treturn 0;\n\t}\n\trep(i, m) {\n\t\tll c, d;\n\t\tcin >> c >> d;\n\t\tvp.push_back({ c,d });\n\t}\n\tsort(ALL(vp));\n\tvector<P> vp2;\n\tll d = -1;\n\trep(i, vp.size()) {\n\t\tif (d < vp[i].second) {\n\t\t\td = vp[i].second;\n\t\t\tvp2.push_back(vp[i]);\n\t\t}\n\t}\n\trep(i, 510)rep(j, 1010)dp[i][j] = INF;\n\tdp[0][0] = 0;\n\tint now = 0;\n\trep(i, vp2.size()) {\n\t\tll ci = vp2[i].first;\n\t\tll di = vp2[i].second;\n\t\tFOR(j, di, n + 2) {//新しく経由する点\n\t\t\tFOR(k, now, n + 2) {//今までに経由した点\n\t\t\t\tif (k < j)dp[i + 1][j] = min(dp[i + 1][j], dp[i][k] + (j - now) + (j - ci));\n\t\t\t\telse dp[i + 1][j] = min(dp[i + 1][j], dp[i][j] + (ci - now));\n\t\t\t}\n\t\t}\n\t\tnow = ci;\n\t}\n\t//FOR(i, 1, m + 1) {\n\t//\tif (dp[i][5] == INF) {\n\t//\t\tcout << i << endl;\n\t//\t\tbreak;\n\t//\t}\n\t//}\n\t//rep(i, vp2.size() + 1) {\n\t//\tif (i != vp2.size())continue;\n\t//\trep(j, n + 1) {\n\t//\t\tif (dp[i][j] != INF)printf(\"%4d\", dp[i][j]);\n\t//\t\telse cout << \" INF\";\n\t//\t}\n\t//\tcout << endl;\n\t//}\n\tll ans = INF;\n\trep(i, n + 2) {\n\t\tans = min(dp[vp2.size()][i] + n + 1 - now, ans);\n\t}\n\tcout << ans << endl;\n\t//cout << dp[vp2.size()][d] + n + 1 - now << endl;\n}\n\n//int n, m;\n//vector<P> vp;\n//int sum[100100];\n//\n//int main() {\n//\tcin >> n >> m;\n//\tvp.resize(m);\n//\trep(i, m) {\n//\t\tint a, b;\n//\t\tcin >> a >> b;\n//\t\tsum[a]++;\n//\t\tsum[b]--;\n//\t}\n//\trep(i, n + 2)sum[i + 1] += sum[i];\n//\tll ans = 0;\n//\trep(i, n + 1) {\n//\t\tif (sum[i] > 0)ans += 3;\n//\t\telse ans++;\n//\t}\n//\tcout << ans << endl;\n//}", "accuracy": 1, "time_ms": 410, "memory_kb": 7524, "score_of_the_acc": -0.5576, "final_rank": 2 }, { "submission_id": "aoj_1347_2717012", "code_snippet": "#include <iostream>\n#include <string>\n#include <vector>\n#include <algorithm>\n#include <cmath>\n#include <cstdio>\n#include <functional>\n#include <numeric>\n#include <stack>\n#include <queue>\n#include <map>\n#include <set>\n#include <utility>\n#include <sstream>\n#include <complex>\n#include <fstream>\n#include <bitset>\n#include <time.h>\n#include <tuple>\n#include <iomanip>\n\nusing namespace std;\n\ntypedef long long ll;\ntypedef pair<ll, ll> P;\ntypedef vector<ll> V;\ntypedef complex<double> Point;\n\n#define PI acos(-1.0)\n#define EPS 1e-10\nconst ll INF = 1e12;\nconst ll MOD = 1e9 + 7;\n\n#define FOR(i,a,b) for(int i=(a);i<(b);i++)\n#define rep(i,N) for(int i=0;i<(N);i++)\n#define ALL(s) (s).begin(),(s).end()\n#define EQ(a,b) (abs((a)-(b))<EPS)\n#define EQV(a,b) ( EQ((a).real(), (b).real()) && EQ((a).imag(), (b).imag()) )\n#define fi first\n#define se second\n#define N_SIZE (1LL << 20)\n#define NIL -1\n\nll sq(ll num) { return num*num; }\nll mod_pow(ll x, ll n) {\n\tif (n == 0)return 1;\n\tif (n == 1)return x%MOD;\n\tll res = sq(mod_pow(x, n / 2));\n\tres %= MOD;\n\tif (n % 2 == 1) {\n\t\tres *= x;\n\t\tres %= MOD;\n\t}\n\treturn res;\n}\nll mod_add(ll a, ll b) { return (a + b) % MOD; }\nll mod_sub(ll a, ll b) { return (a - b + MOD) % MOD; }\nll mod_mul(ll a, ll b) { return a*b % MOD; }\n\n//dp\nint n, m;\nvector<P> vp;\nll dp[510][1010];\n\nint main() {\n\tcin >> n >> m;\n\tif (m == 0) {\n\t\tcout << n + 1 << endl;\n\t\treturn 0;\n\t}\n\trep(i, m) {\n\t\tll c, d;\n\t\tcin >> c >> d;\n\t\tvp.push_back({ c,d });\n\t}\n\tsort(ALL(vp));\n\tvector<P> vp2;\n\tll d = -1;\n\trep(i, vp.size()) {\n\t\tif (d < vp[i].second) {\n\t\t\td = vp[i].second;\n\t\t\tvp2.push_back(vp[i]);\n\t\t}\n\t}\n\trep(i, 510)rep(j, 1010)dp[i][j] = INF;\n\tdp[0][0] = 0;\n\tint now = 0;\n\trep(i, vp2.size()) {\n\t\tll ci = vp2[i].first;\n\t\tll di = vp2[i].second;\n\t\tFOR(j, di, n + 2) {//新しく経由する点\n\t\t\tFOR(k, now, n + 2) {//今までに経由した点\n\t\t\t\tif (k < j)dp[i + 1][j] = min(dp[i + 1][j], dp[i][k] + (j - now) + (j - ci));\n\t\t\t\telse dp[i + 1][j] = min(dp[i + 1][j], dp[i][j] + (ci - now));\n\t\t\t}\n\t\t}\n\t\tnow = ci;\n\t}\n\t//FOR(i, 1, m + 1) {\n\t//\tif (dp[i][5] == INF) {\n\t//\t\tcout << i << endl;\n\t//\t\tbreak;\n\t//\t}\n\t//}\n\t//rep(i, m + 1) {\n\t//\trep(j, n + 1) {\n\t//\t\tif (dp[i][j] != INF)printf(\"%4d\", dp[i][j]);\n\t//\t\telse cout << \" INF\";\n\t//\t}\n\t//\tcout << endl;\n\t//}\n\td = vp.back().second;\n\tcout << dp[vp2.size()][d] + n + 1 - now << endl;\n}\n\n//int n, m;\n//vector<P> vp;\n//int sum[100100];\n//\n//int main() {\n//\tcin >> n >> m;\n//\tvp.resize(m);\n//\trep(i, m) {\n//\t\tint a, b;\n//\t\tcin >> a >> b;\n//\t\tsum[a]++;\n//\t\tsum[b]--;\n//\t}\n//\trep(i, n + 2)sum[i + 1] += sum[i];\n//\tll ans = 0;\n//\trep(i, n + 1) {\n//\t\tif (sum[i] > 0)ans += 3;\n//\t\telse ans++;\n//\t}\n//\tcout << ans << endl;\n//}", "accuracy": 0.045454545454545456, "time_ms": 370, "memory_kb": 7148, "score_of_the_acc": -0.5029, "final_rank": 9 }, { "submission_id": "aoj_1347_2716970", "code_snippet": "#include <iostream>\n#include <string>\n#include <vector>\n#include <algorithm>\n#include <cmath>\n#include <cstdio>\n#include <functional>\n#include <numeric>\n#include <stack>\n#include <queue>\n#include <map>\n#include <set>\n#include <utility>\n#include <sstream>\n#include <complex>\n#include <fstream>\n#include <bitset>\n#include <time.h>\n#include <tuple>\n#include <iomanip>\n\nusing namespace std;\n\ntypedef long long ll;\ntypedef pair<ll, ll> P;\ntypedef vector<ll> V;\ntypedef complex<double> Point;\n\n#define PI acos(-1.0)\n#define EPS 1e-10\nconst ll INF = 1e12;\nconst ll MOD = 1e9 + 7;\n\n#define FOR(i,a,b) for(int i=(a);i<(b);i++)\n#define rep(i,N) for(int i=0;i<(N);i++)\n#define ALL(s) (s).begin(),(s).end()\n#define EQ(a,b) (abs((a)-(b))<EPS)\n#define EQV(a,b) ( EQ((a).real(), (b).real()) && EQ((a).imag(), (b).imag()) )\n#define fi first\n#define se second\n#define N_SIZE (1LL << 20)\n#define NIL -1\n\nll sq(ll num) { return num*num; }\nll mod_pow(ll x, ll n) {\n\tif (n == 0)return 1;\n\tif (n == 1)return x%MOD;\n\tll res = sq(mod_pow(x, n / 2));\n\tres %= MOD;\n\tif (n % 2 == 1) {\n\t\tres *= x;\n\t\tres %= MOD;\n\t}\n\treturn res;\n}\nll mod_add(ll a, ll b) { return (a + b) % MOD; }\nll mod_sub(ll a, ll b) { return (a - b + MOD) % MOD; }\nll mod_mul(ll a, ll b) { return a*b % MOD; }\n\n//dp\nint n, m;\nvector<P> vp;\nll dp[510][1010];\n\nint main() {\n\tcin >> n >> m;\n\tif (m == 0) {\n\t\tcout << n + 1 << endl;\n\t\treturn 0;\n\t}\n\trep(i, m) {\n\t\tll c, d;\n\t\tcin >> c >> d;\n\t\tvp.push_back({ c,d });\n\t}\n\tsort(ALL(vp));\n\trep(i, 510)rep(j, 1010)dp[i][j] = INF;\n\tdp[0][0] = 0;\n\tint now = 0;\n\trep(i, m) {\n\t\tll ci = vp[i].first;\n\t\tll di = vp[i].second;\n\t\tFOR(j, di, n + 1) {//新しく経由する点\n\t\t\tFOR(k, now, n + 1) {//今までに経由した点\n\t\t\t\tif (k < j)dp[i + 1][j] = min(dp[i + 1][j], dp[i][k] + (j - now) + (j - ci));\n\t\t\t\telse dp[i + 1][j] = min(dp[i + 1][j], dp[i][j] + (ci - now));\n\t\t\t}\n\t\t}\n\t\tnow = ci;\n\t}\n\t//rep(i, m + 1) {\n\t//\trep(j, n + 1) {\n\t//\t\tif (dp[i][j] != INF)printf(\"%4d\", dp[i][j]);\n\t//\t\telse cout << \" INF\";\n\t//\t}\n\t//\tcout << endl;\n\t//}\n\tll d = vp.back().second;\n\tcout << dp[m][d] + n + 1 - now << endl;\n}\n\n//int n, m;\n//vector<P> vp;\n//int sum[100100];\n//\n//int main() {\n//\tcin >> n >> m;\n//\tvp.resize(m);\n//\trep(i, m) {\n//\t\tint a, b;\n//\t\tcin >> a >> b;\n//\t\tsum[a]++;\n//\t\tsum[b]--;\n//\t}\n//\trep(i, n + 2)sum[i + 1] += sum[i];\n//\tll ans = 0;\n//\trep(i, n + 1) {\n//\t\tif (sum[i] > 0)ans += 3;\n//\t\telse ans++;\n//\t}\n//\tcout << ans << endl;\n//}", "accuracy": 0.045454545454545456, "time_ms": 360, "memory_kb": 7148, "score_of_the_acc": -0.4907, "final_rank": 8 }, { "submission_id": "aoj_1347_2716228", "code_snippet": "#include <bits/stdc++.h>\n \nusing namespace std;\n \n#define rep(i,n) REP(i,0,n)\n#define REP(i,s,e) for(int i=(s); i<(int)(e); i++)\n#define repr(i, n) REPR(i, n, 0)\n#define REPR(i, s, e) for(int i=(int)(s-1); i>=(int)(e); i--)\n#define pb push_back\n#define all(r) r.begin(),r.end()\n#define rall(r) r.rbegin(),r.rend()\n#define fi first\n#define se second\n \ntypedef long long ll;\ntypedef vector<int> vi;\ntypedef vector<ll> vl;\ntypedef pair<int, int> pii;\ntypedef pair<ll, ll> pll;\n \nconst int INF = 1e9;\nconst ll MOD = 1e9 + 7;\ndouble EPS = 1e-8;\n\nll dp[2][1010];\n\nint main(){\n int n, m;\n cin >> n >> m;\n vi c(m+1), d(m+1);\n rep(i, m) cin >> c[i+1] >> d[i+1]; \n if(m == 0) {\n cout << n+1 << endl;\n return 0;\n }\n vector<pii> p(m+1);\n rep(i, m+1) p[i] = make_pair(c[i], d[i]);\n sort(all(p));\n rep(i, m+1) {\n c[i] = p[i].fi;\n d[i] = p[i].se;\n }\n {\n rep(i, 1010) {\n dp[1][i] = 1e18;\n }\n REP(i, d[1], 1010) {\n dp[1][i] = i + i - c[1];\n }\n\n }\n int cur, nxt;\n REP(i, 1, m) {\n cur = i&1;\n nxt = (i+1)&1;\n rep(j, 1010) dp[nxt][j] = 1e18;\n // j番目からk番目まで進んでから、c[i]番目にいく\n rep(j, 1010) if(dp[cur][j] != 1e18) {\n REP(k, d[i+1], 1010) {\n if(j >= k) { // c[i] -> c[i+1]\n int pos = max(j, c[i+1]);\n dp[nxt][pos] = min(dp[nxt][pos], dp[cur][j] + c[i+1] - c[i]);\n }\n else { // c[i] -> k -> c[i+1]\n int pos = max(k, c[i+1]);\n dp[nxt][pos] = min(dp[nxt][pos], dp[cur][j] + abs(c[i] - k) + abs(k - c[i+1]));\n }\n }\n }\n // rep(j, 20) {\n // cout << \" \" << (dp[nxt][j] == 1e18 ? -1 : dp[nxt][j]);\n // }\n // cout << endl;\n }\n\n ll ans = 1e18;\n rep(i, n+1) {\n ans = min(ans, dp[nxt][i] + n+1-c.back());\n }\n cout << ans << endl;\n return 0;\n}", "accuracy": 1, "time_ms": 830, "memory_kb": 3152, "score_of_the_acc": -1, "final_rank": 5 }, { "submission_id": "aoj_1347_2642468", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\n \n#define int long long\n#define pb push_back \n#define pf push_front \n#define mp make_pair\n#define fr first\n#define sc second\n#define Rep(i, n) for ( int i = 0 ; i < (n); i++ )\n#define All(v) v.begin(), v.end()\n \ntypedef pair<int, int> Pii; typedef pair<int, Pii> Pip;\nconst int INF = 1107110711071107;\n \nint N, m;\nvector<Pii> d;\nunordered_map<int, int> dp[1001][1001];\n \nint dfs(int pre, int now, int n) {\n if ( n == m ) {\n return (now-pre + (N+1)-pre);\n }\n if ( dp[pre][now][n] > 0 ) return dp[pre][now][n];\n \n int ret = dfs(min(pre, d[n].sc), d[n].fr, n+1) + (d[n].fr-now);\n ret = min(ret, dfs(d[n].sc, d[n].fr, n+1) + (now-pre + d[n].fr-pre));\n \n return dp[pre][now][n] = ret;\n}\n \nsigned main() {\n cin >> N >> m;\n \n Rep(i, m) {\n int c, b;\n cin >> c >> b;\n d.pb(Pii(b, c)); \n }\n \n sort(All(d));\n \n if ( m == 0 ) cout << N+1 << endl;\n else cout << dfs(d[0].sc, d[0].fr, 1) + d[0].fr << endl;\n}", "accuracy": 1, "time_ms": 50, "memory_kb": 65728, "score_of_the_acc": -1.0485, "final_rank": 6 }, { "submission_id": "aoj_1347_2641900", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\n\n#define int long long\n#define pb push_back \n#define pf push_front \n#define mp make_pair\n#define fr first\n#define sc second\n#define Rep(i, n) for ( int i = 0 ; i < (n); i++ )\n#define All(v) v.begin(), v.end()\n\ntypedef pair<int, int> Pii; typedef pair<int, Pii> Pip;\nconst int INF = 1107110711071107;\n\nint N, m;\nvector<Pii> d;\nunordered_map<int, int> dp[1001][1001];\n\nint dfs(int pre, int now, int n) {\n if ( n == m ) {\n return (now-pre + (N+1)-pre);\n }\n if ( dp[pre][now][n] > 0 ) return dp[pre][now][n];\n\n int ret = dfs(min(pre, d[n].sc), d[n].fr, n+1) + (d[n].fr-now);\n ret = min(ret, dfs(d[n].sc, d[n].fr, n+1) + (now-pre + d[n].fr-pre));\n\n return dp[pre][now][n] = ret;\n}\n\nsigned main() {\n cin >> N >> m;\n\n Rep(i, m) {\n int c, b;\n cin >> c >> b;\n d.pb(Pii(b, c)); \n }\n\n sort(All(d));\n\n if ( m == 0 ) cout << N+1 << endl;\n else cout << dfs(d[0].sc, d[0].fr, 1) + d[0].fr << endl;\n}", "accuracy": 1, "time_ms": 50, "memory_kb": 65748, "score_of_the_acc": -1.0488, "final_rank": 7 }, { "submission_id": "aoj_1347_2641888", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\n\n#define int long long\n#define pb push_back \n#define pf push_front \n#define mp make_pair\n#define fr first\n#define sc second\n#define Rep(i, n) for ( int i = 0 ; i < (n); i++ )\n#define All(v) v.begin(), v.end()\n\ntypedef pair<int, int> Pii; typedef pair<int, Pii> Pip;\nconst int INF = 1107110711071107;\n\nint N, m;\nvector<Pii> d;\nunordered_map<int, int> dp[1001][1001];\n\nint dfs(int pre, int now, int n) {\n if ( n == m ) {\n return (now-pre + (N+1)-pre);\n }\n if ( dp[pre][now][n] > 0 ) return dp[pre][now][n];\n\n int ret = dfs(pre, d[n].fr, n+1) + (d[n].fr-now);\n ret = min(ret, dfs(d[n].sc, d[n].fr, n+1) + (now-pre + d[n].fr-pre));\n\n return dp[pre][now][n] = ret;\n}\n\nsigned main() {\n cin >> N >> m;\n\n Rep(i, m) {\n int c, b;\n cin >> c >> b;\n d.pb(Pii(b, c)); \n }\n\n sort(All(d));\n\n if ( m == 0 ) cout << N+1 << endl;\n else cout << dfs(d[0].sc, d[0].fr, 1) + d[0].fr << endl;\n}", "accuracy": 0.045454545454545456, "time_ms": 40, "memory_kb": 63100, "score_of_the_acc": -0.9943, "final_rank": 10 }, { "submission_id": "aoj_1347_1595101", "code_snippet": "//#include <stdio.h>\n//#include <iostream>\n//#include <math.h>\n//#include <numeric>\n//#include <vector>\n//#include <map>\n//#include <functional>\n//#include <stdio.h>\n//#include <array>\n//#include <algorithm>\n//#include <string>\n//#include <assert.h>\n//#include <stdio.h>\n//#include <queue>\n//#include<iomanip>\n//using namespace std;\n//\n//\n//using namespace std;\n////\n////#define N (1<<17) \n////// update(l,r,v) := [l,r]の区間に対してvを一様に足す. k,a,bは飾り\n////\n////struct NODE{\n////\tint sum;//更新された値. この値を参照する時は評価が完全に完了しているようにする.\n////\tint lazy;\t//遅延されている値を保存している\n////\tNODE(){\n////\t\tsum = lazy = 0;\n////\t}\n////};\n////\n////NODE seg[2 * N];\n////\n////// inlineつけないと大変なことになるよ!(遅い)\n////inline void lazy_evaluate_node(int k, int a, int b){\n////\tseg[k].sum +=seg[k].lazy;\n////\tif (k < N){ // 2*k(左の子番号) < 2*N (節点の数) のイメージで. 末端ノードじゃなきゃ伝搬するのと等価.\n////\t\tseg[2 * k].lazy += seg[k].lazy;\t//次は君が伝搬してね☆って感じ.\n////\t\tseg[2 * k + 1].lazy += seg[k].lazy;\n////\t}\n////\tseg[k].lazy = 0;\n////}\n////\n////inline void update_node(int k){ // kの子が既に評価されていることが前提. 末端以外のときしか呼び出さないような位置に書くのでif文要らない.\n////\tseg[k].sum = min(seg[2 * k].sum , seg[2 * k + 1].sum);\n////}\n////\n////// update(l,r,v) := [l,r]を更新する. 区間は1-indexed.\n////void update(int l, int r, int v, int k = 1, int a = 1, int b = N){\n////\tlazy_evaluate_node(k, a, b); \t// とりあえず辿ったノードは都合がいいので伝搬しとけ精神.\n////\n////\tif (b < l || r < a) //[a,b]と[l,r]が交差している場合\n////\t\treturn;\n////\tif (l <= a && b <= r){ // [l,r]が[a,b]を完全に含んでいる場合\n////\t\tseg[k].lazy += v;\n////\t\tlazy_evaluate_node(k, a, b); //一回遅延評価しとかないと都合悪いので.\n////\t\treturn;\n////\t}\n////\n////\tint m = (a + b) / 2;\n////\tupdate(l, r, v, 2 * k, a, m);\n////\tupdate(l, r, v, 2 * k + 1, m + 1, b);\n////\tupdate_node(k);\n////}\n////\n////// get(l,r) := [l,r]に対するクエリの答えを得る. 区間は1-indexed.\n////int get(int l, int r, int k = 1, int a = 1, int b = N){\n////\tlazy_evaluate_node(k, a, b); // とりあえず辿ったノードは都合がいいので伝搬しとけ精神.\n////\n////\tif (b < l || r < a) //[a,b]と[l,r]が交差している場合\n////\t\treturn 0;\n////\n////\tif (l <= a && b <= r){ // [l,r]が[a,b]を完全に含んでいる場合\n////\t\treturn seg[k].sum;\n////\t}\n////\n////\tint m = (a + b) / 2;\n////\tint vl = get(l, r, 2 * k, a, m);\n////\tint vr = get(l, r, 2 * k + 1, m + 1, b);\n////\tupdate_node(k);\n////\treturn min(vl , vr);\n////}\n//\n//\n//int main() {\n//\t//int n;\n//\t//cin >> n;\n//\t//vector<int> aa(n);\n//\t//vector<pair<int,int>>aaa;\n//\t//for (int i = 0; i < n; ++i){\n//\t//\tint a, b;\n//\t//\tcin >> a >> b;\n//\t//\taaa.push_back(make_pair(a, b)); \n//\t//\taa[a]++;\n//\t//\taa[b]--;\n//\t//}\n//\t//vector<int> ans;\n//\t//ans.push_back(aa[0]);\n//\t//int amin=99999999;\n//\t//for(int i=0;i<n;++i){\n//\t//\tans.push_back(ans[i] + aa[i]);\n//\t//}\n//\n//\t//RMQ r;\n//\t//r.init(65536);\n//\t//for (int i = 0; i < 65536;++i){\n//\t//\tr.update(i, ans[i]);\n//\t//}\n//\n//\t//cout << r.query(0, 7) << endl; //1\n//\t//cout << r.query(3, 6) << endl; //2\n//\t//cout << r.query(1, 5) << endl; //1\n//\t//cout << r.query(5, 7) << endl; //4\n//\t//return 0;\n//\n//\tvector<long long int>times;\n//\tfor (int i = 0; i < 7; ++i){\n//\t\tlong long int c;\n//\t\tcin >> c;\n//\t\ttimes.push_back(c);\n//\t}\n//\tif (times[0] == 0)cout << \"YES\" << endl;\n//\telse{\n//\t\ttimes[0]--;\n//\t}\n//\n//\t{\n//\t\tvector<long long int> atimes(times);\n//\t\twhile (1){\n//\t\t\tauto zero_it = find(atimes.begin(), atimes.end(), 0);\n//\t\t\tif (zero_it != atimes.end()){//5 3 2 0 4 4 2 \n//\t\t\t\tconst int num = zero_it - atimes.begin();\n//\t\t\t\tint from = num;\n//\t\t\t\twhile (1){\n//\t\t\t\t\tfrom = (from + 1) % 7;\n//\t\t\t\t\tif (from == num)break;\n//\t\t\t\t\tint to = (from + 1) % 7;\n//\t\t\t\t\tif (atimes[from] != 0 && atimes[to] == 0){\n//\t\t\t\t\t\tbreak;\n//\t\t\t\t\t}\n//\t\t\t\t\tif (atimes[from] > atimes[to]){\n//\t\t\t\t\t\tbreak;\n//\t\t\t\t\t}\n//\t\t\t\t\tlong long int amin = min(atimes[from], atimes[to]);\n//\t\t\t\t\tatimes[from] -= amin;\n//\t\t\t\t\tatimes[to] -= amin;\n//\t\t\t\t\tif (atimes[from] == 0 && atimes[to] == 0){\n//\t\t\t\t\t\tbreak;\n//\t\t\t\t\t}\n//\t\t\t\t}\n//\t\t\t\tif (all_of(atimes.begin(), atimes.end(), [](const int aaa){return aaa == 0; })){\n//\t\t\t\t\tcout << \"YES\" << endl;\n//\t\t\t\t\treturn 0;\n//\t\t\t\t}\n//\t\t\t\telse{\n//\t\t\t\t\tbreak;\n//\t\t\t\t}\n//\t\t\t}\n//\t\t\telse{\n//\t\t\t\tlong long int aamin = atimes[0];\n//\t\t\t\tfor (int i = 0; i < 7; ++i){\n//\t\t\t\t\taamin = min(aamin, atimes[i]);\n//\t\t\t\t}\n//\t\t\t\tfor (int i = 0; i < 7; ++i){\n//\t\t\t\t\tatimes[i] -= aamin;\n//\t\t\t\t}\n//\t\t\t}\n//\t\t}\n//\t}\n//\t{\n//\t\tvector<long long int> btimes(times);\n//\t\tfor (int i = 0; i < 7; ++i){//times[i]のまえがあいだになる\n//\t\t\tbtimes = times;\n//\t\t\tint from = (i + 6) % 7;\n//\t\t\twhile (1){\n//\t\t\t\tfrom = (from + 1) % 7;\n//\t\t\t\tif (from == (i + 6) % 7)break;\n//\t\t\t\tint to = (from + 1) % 7;\n//\t\t\t\tif (btimes[from] != 0 && btimes[to] == 0){\n//\t\t\t\t\tbreak;\n//\t\t\t\t}\n//\t\t\t\tif (btimes[from] > btimes[to]){\n//\t\t\t\t\tbreak;\n//\t\t\t\t}\n//\t\t\t\tlong long int amin = min(btimes[from], btimes[to]);\n//\t\t\t\tbtimes[from] -= amin;\n//\t\t\t\tbtimes[to] -= amin;\n//\t\t\t\tif (btimes[from] == 0 && btimes[to] == 0){\n//\t\t\t\t\tbreak;\n//\t\t\t\t}\n//\t\t\t}\n//\t\t\tif (all_of(btimes.begin(), btimes.end(), [=](int a){return a == 0; })){\n//\t\t\t\tcout << \"YES\" << endl;\n//\t\t\t\treturn 0;\n//\t\t\t}\n//\t\t\telse{\n//\t\t\t\tcontinue;\n//\t\t\t}\n//\t\t}\n//\t}\n//\tcout << \"NO\" << endl;\n//\treturn 0;\n//}\n////3 4 4 4 4 4 4\n\n#include<stdio.h>\n#include <iostream>\n#include <math.h>\n#include <numeric>\n#include <vector>\n#include <map>\n#include <functional>\n#include <stdio.h>\n#include <array>\n#include <algorithm>\n#include <string>\n#include <assert.h>\n#include <stdio.h>\n#include <queue>\n#include<iomanip>\n#include<bitset>\n#include<stack>\nusing namespace std;\n\n//7:10\n//\n//#define REP(i,n) for(int i=0;i<(int)n;++i)\n//#define FOR(i,c) for(__typeof((c).begin())i=(c).begin();i!=(c).end();++i)\n//#define ALL(c) (c).begin(), (c).end()\n//\n//typedef int Weight;\n//struct Edge {\n//\tint src, dst;\n//\tWeight weight;\n//\tEdge(int src, int dst, Weight weight) :\n//\t\tsrc(src), dst(dst), weight(weight) { }\n//};\n//bool operator < (const Edge &e, const Edge &f) {\n//\treturn e.weight != f.weight ? e.weight > f.weight : // !!INVERSE!!\n//\te.src != f.src ? e.src < f.src : e.dst < f.dst;\n//}\n//typedef vector<Edge> Edges;\n//typedef vector<Edges> Graph;\n//\n//typedef vector<Weight> Array;\n//typedef vector<Array> Matrix;\n//\n//#define RESIDUE(s,t) (capacity[s][t]-flow[s][t])\n//Weight maximumFlow(const Graph &g, int s, int t) {\n//\tint n = g.size();\n//\tMatrix flow(n, Array(n)), capacity(n, Array(n));\n//\tREP(u, n) for (auto e = g[u].begin(); e != g[u].end();++e)capacity[e->src][e->dst] += e->weight;\n//\n//\tWeight total = 0;\n//\twhile (1) {\n//\t\tqueue<int> Q; Q.push(s);\n//\t\tvector<int> prev(n, -1); prev[s] = s;\n//\t\twhile (!Q.empty() && prev[t] < 0) {\n//\t\t\tint u = Q.front(); Q.pop();\n//\t\t\tfor (auto e = g[u].begin(); e != g[u].end(); ++e) if (prev[e->dst] < 0 && RESIDUE(u, e->dst) > 0) {\n//\t\t\t\tprev[e->dst] = u;\n//\t\t\t\tQ.push(e->dst);\n//\t\t\t}\n//\t\t}\n//\t\tif (prev[t] < 0) return total; // prev[x] == -1 <=> t-side\n//\t\tWeight inc = 999999999;\n//\t\tfor (int j = t; prev[j] != j; j = prev[j])\n//\t\t\tinc = min(inc, RESIDUE(prev[j], j));\n//\t\tfor (int j = t; prev[j] != j; j = prev[j])\n//\t\t\tflow[prev[j]][j] += inc, flow[j][prev[j]] -= inc;\n//\t\ttotal += inc;\n//\t}\n//\treturn total;\n//}\n#define REP(i,n) for(int i=0;i<(int)(n);i++)\n#define ALL(x) (x).begin(),(x).end()\n\n\nconst int mod = 1000000007;\n\nstruct Mod {\n\tlong long int num;\n\tMod() : num(0) { ; }\n\tMod(long long int n) : num((n % mod + mod) % mod) { ; }\n\toperator long long int() { return num; }\n};\n\nMod operator+(Mod a, Mod b) { return Mod((a.num + b.num) % mod); }\nMod operator-(Mod a, Mod b) { return Mod((mod + a.num - b.num) % mod); }\nMod operator*(Mod a, Mod b) { return Mod(((long long)a.num * b.num) % mod); }\nMod operator+=(Mod &a, Mod b) { return a = a + b; }\nMod operator-=(Mod &a, Mod b) { return a = a - b; }\nMod operator*=(Mod &a, Mod b) { return a = a * b; }\nMod operator^(Mod a, int n) {\n\tif (n == 0) return Mod(1);\n\tMod res = (a * a) ^ (n / 2);\n\tif (n % 2) res = res * a;\n\treturn res;\n}\nMod inv(Mod a) { return a ^ (mod - 2); }\nMod operator/(Mod a, Mod b) { return a * inv(b); }\n\n#define MAX_N 1024000\n\nMod fact[MAX_N], factinv[MAX_N];\nvoid init() {\n\tfact[0] = Mod(1); factinv[0] = 1;\n\tREP(i, MAX_N - 1) {\n\t\tfact[i + 1] = fact[i] * Mod(i + 1);\n\t\tfactinv[i + 1] = factinv[i] / Mod(i + 1);\n\t}\n}\nMod comb(int a, int b) {\n\treturn fact[a] * factinv[b] * factinv[a - b];\n}\n\nlong long int memo[20];//nけた\n\nlong long int powint(long long int a, int b){\n\tif (b == 0)return 1;\n\tif (b == 1)return a;\n\telse{\n\t\tlong long int ans = 1;\n\t\tlong long int c = powint(a, b / 2);\n\t\tans *= c*c;\n\t\tans *= (b % 2) ? a : 1;\n\t\treturn ans;\n\t}\n\n}\n\n\nint backnum[100000][20];\n\nstruct Node{\n\tNode(int aid):children(),ancs(){\n\t\tid=(aid);\n\t\tfor (int i = 0; i < 20; ++i){\n\t\t\tancs[i] = -1;\n\t\t}\n\t}\n\tint id;\n\tvector<int>children;\n\tarray<int, 20>ancs;\n};\n\nint main()\n{\n\tint N, M;\n\tcin >> N >> M;\n\tvector<int> Sa(N + 1);\n\tvector<int>Sum(N);\n\tfor (int i = 0; i < M; ++i){\n\t\tint a, b;\n\t\tcin >> a >> b;\n\t\tif (a < b){\n\t\t\tSa[a]++;\n\t\t\tSa[b]--;\n\t\t}\n\t\t\n\t}\n\tint now = 0;\n\tfor (int i = 0; i < N; ++i){\n\t\tnow += Sa[i];\n\t\tSum[i] += now;\n\t}\n\tint a = count_if(Sum.begin(), Sum.end(), [](const int l){return l != 0; });\n\tcout << N + 1 + a * 2<<endl;\n\treturn 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 19120, "score_of_the_acc": -0.2551, "final_rank": 1 } ]
aoj_1342_cpp
Problem H: Don't Burst the Balloon An open-top box having a square bottom is placed on the floor. You see a number of needles vertically planted on its bottom. You want to place a largest possible spheric balloon touching the box bottom, interfering with none of the side walls nor the needles. Java Specific: Submitted Java programs may not use "java.awt.geom.Area". You may use it for your debugging purposes. Figure H.1 shows an example of a box with needles and the corresponding largest spheric balloon. It corresponds to the first dataset of the sample input below. Figure H.1. The upper shows an example layout and the lower shows the largest spheric balloon that can be placed. Input The input is a sequence of datasets. Each dataset is formatted as follows. n w x 1 y 1 h 1 : x n y n h n The first line of a dataset contains two positive integers, n and w , separated by a space. n represents the number of needles, and w represents the height of the side walls. The bottom of the box is a 100 × 100 square. The corners of the bottom face are placed at positions (0, 0, 0), (0, 100, 0), (100, 100, 0), and (100, 0, 0). Each of the n lines following the first line contains three integers, x i , y i , and h i . ( x i , y i , 0) and h i represent the base position and the height of the i -th needle. No two needles stand at the same position. You can assume that 1 ≤ n ≤ 10, 10 ≤ w ≤ 200, 0 < x i < 100, 0 < y i < 100 and 1 ≤ h i ≤ 200. You can ignore the thicknesses of the needles and the walls. The end of the input is indicated by a line of two zeros. The number of datasets does not exceed 1000. Output For each dataset, output a single line containing the maximum radius of a balloon that can touch the bottom of the box without interfering with the side walls or the needles. The output should not contain an error greater than 0.0001. Sample Input 5 16 70 66 40 38 52 20 40 35 10 70 30 10 20 60 10 1 100 54 75 200 1 10 90 10 1 1 11 54 75 200 3 10 53 60 1 61 38 1 45 48 1 4 10 20 20 10 20 80 10 80 20 10 80 80 10 0 0 Output for the Sample Input 26.00000 39.00000 130.00000 49.49777 85.00000 95.00000
[ { "submission_id": "aoj_1342_9782093", "code_snippet": "#include <bits/stdc++.h>\n\nusing namespace std;\n\n#define rep(i, a, b) for (ll i = (ll)(a); i < (ll)(b); i++)\n#define rrep(i, a, b) for (ll i = (ll)(b)-1; i >= (ll)(a); i--)\n#define ALL(v) (v).begin(), (v).end()\n#define UNIQUE(v) sort(ALL(v)), (v).erase(unique(ALL(v)), (v).end())\n#define SZ(v) (int)v.size()\n#define MIN(v) *min_element(ALL(v))\n#define MAX(v) *max_element(ALL(v))\n#define LB(v, x) int(lower_bound(ALL(v), (x)) - (v).begin())\n#define UB(v, x) int(upper_bound(ALL(v), (x)) - (v).begin())\n\nusing uint = unsigned int;\nusing ll = long long int;\nusing ull = unsigned long long;\nusing i128 = __int128_t;\nusing u128 = __uint128_t;\nconst int inf = 0x3fffffff;\nconst ll INF = 0x1fffffffffffffff;\n\ntemplate <typename T> inline bool chmax(T &a, T b) {\n if (a < b) {\n a = b;\n return 1;\n }\n return 0;\n}\ntemplate <typename T> inline bool chmin(T &a, T b) {\n if (a > b) {\n a = b;\n return 1;\n }\n return 0;\n}\ntemplate <typename T, typename U> T ceil(T x, U y) {\n assert(y != 0);\n if (y < 0)\n x = -x, y = -y;\n return (x > 0 ? (x + y - 1) / y : x / y);\n}\ntemplate <typename T, typename U> T floor(T x, U y) {\n assert(y != 0);\n if (y < 0)\n x = -x, y = -y;\n return (x > 0 ? x / y : (x - y + 1) / y);\n}\ntemplate <typename T> int popcnt(T x) {\n return __builtin_popcountll(x);\n}\ntemplate <typename T> int topbit(T x) {\n return (x == 0 ? -1 : 63 - __builtin_clzll(x));\n}\ntemplate <typename T> int lowbit(T x) {\n return (x == 0 ? -1 : __builtin_ctzll(x));\n}\n\ntemplate <class T, class U>\nostream &operator<<(ostream &os, const pair<T, U> &p) {\n os << \"P(\" << p.first << \", \" << p.second << \")\";\n return os;\n}\ntemplate <typename T> ostream &operator<<(ostream &os, const vector<T> &vec) {\n os << \"{\";\n for (int i = 0; i < vec.size(); i++) {\n os << vec[i] << (i + 1 == vec.size() ? \"\" : \", \");\n }\n os << \"}\";\n return os;\n}\ntemplate <typename T, typename U>\nostream &operator<<(ostream &os, const map<T, U> &map_var) {\n os << \"{\";\n for (auto itr = map_var.begin(); itr != map_var.end(); itr++) {\n os << \"(\" << itr->first << \", \" << itr->second << \")\";\n itr++;\n if (itr != map_var.end())\n os << \", \";\n itr--;\n }\n os << \"}\";\n return os;\n}\ntemplate <typename T> ostream &operator<<(ostream &os, const set<T> &set_var) {\n os << \"{\";\n for (auto itr = set_var.begin(); itr != set_var.end(); itr++) {\n os << *itr;\n ++itr;\n if (itr != set_var.end())\n os << \", \";\n itr--;\n }\n os << \"}\";\n return os;\n}\n#ifdef LOCAL\n#define show(...) _show(0, #__VA_ARGS__, __VA_ARGS__)\n#else\n#define show(...) true\n#endif\ntemplate <typename T> void _show(int i, T name) {\n cerr << '\\n';\n}\ntemplate <typename T1, typename T2, typename... T3>\nvoid _show(int i, const T1 &a, const T2 &b, const T3 &...c) {\n for (; a[i] != ',' && a[i] != '\\0'; i++)\n cerr << a[i];\n cerr << \":\" << b << \" \";\n _show(i + 1, a, c...);\n}\n\n/**\n * @brief template\n */\n\nusing T = long double;\nconst T eps = 1e-8;\nusing Point = complex<T>;\nusing Poly = vector<Point>;\n#define X real()\n#define Y imag()\ntemplate <typename T> inline bool eq(const T &a, const T &b) {\n return fabs(a - b) < eps;\n}\nbool cmp(const Point &a, const Point &b) {\n auto sub = [&](Point a) {\n return (a.Y < 0 ? -1 : (a.Y == 0 && a.X >= 0 ? 0 : 1));\n };\n if (sub(a) != sub(b))\n return sub(a) < sub(b);\n return a.Y * b.X < a.X * b.Y;\n}\nstruct Line {\n Point a, b, dir;\n Line() {}\n Line(Point _a, Point _b) : a(_a), b(_b), dir(b - a) {}\n Line(T A, T B, T C) {\n if (eq(A, (T).0)) {\n a = Point(0, C / B), b = Point(1 / C / B);\n } else if (eq(B, (T).0)) {\n a = Point(C / A, 0), b = Point(C / A, 1);\n } else {\n a = Point(0, C / B), b = Point(C / A, 0);\n }\n }\n};\nstruct Segment : Line {\n Segment() {}\n Segment(Point _a, Point _b) : Line(_a, _b) {}\n};\nstruct Circle {\n Point p;\n T r;\n Circle() {}\n Circle(Point _p, T _r) : p(_p), r(_r) {}\n};\n\nistream &operator>>(istream &is, Point &p) {\n T x, y;\n is >> x >> y;\n p = Point(x, y);\n return is;\n}\nostream &operator<<(ostream &os, Point &p) {\n os << fixed << setprecision(12) << p.X << ' ' << p.Y;\n return os;\n}\nPoint unit(const Point &a) {\n return a / abs(a);\n}\nT dot(const Point &a, const Point &b) {\n return a.X * b.X + a.Y * b.Y;\n}\nT cross(const Point &a, const Point &b) {\n return a.X * b.Y - a.Y * b.X;\n}\nPoint rot(const Point &a, const T &theta) {\n return Point(cos(theta) * a.X - sin(theta) * a.Y,\n sin(theta) * a.X + cos(theta) * a.Y);\n}\nPoint rot90(const Point &a) {\n return Point(-a.Y, a.X);\n}\nT arg(const Point &a, const Point &b, const Point &c) {\n double ret = acos(dot(a - b, c - b) / abs(a - b) / abs(c - b));\n if (cross(a - b, c - b) < 0)\n ret = -ret;\n return ret;\n}\n\nPoint Projection(const Line &l, const Point &p) {\n T t = dot(p - l.a, l.a - l.b) / norm(l.a - l.b);\n return l.a + (l.a - l.b) * t;\n}\nPoint Projection(const Segment &l, const Point &p) {\n T t = dot(p - l.a, l.a - l.b) / norm(l.a - l.b);\n return l.a + (l.a - l.b) * t;\n}\nPoint Reflection(const Line &l, const Point &p) {\n return p + (Projection(l, p) - p) * (T)2.;\n}\nint ccw(const Point &a, Point b, Point c) {\n b -= a;\n c -= a;\n if (cross(b, c) > eps)\n return 1; // ccw\n\n if (cross(b, c) < -eps)\n return -1; // cw\n\n if (dot(b, c) < 0)\n return 2; // c,a,b\n\n if (norm(b) < norm(c))\n return -2; // a,b,c\n\n return 0; // a,c,b\n\n}\nbool isOrthogonal(const Line &a, const Line &b) {\n return eq(dot(a.b - a.a, b.b - b.a), (T).0);\n}\nbool isParallel(const Line &a, const Line &b) {\n return eq(cross(a.b - a.a, b.b - b.a), (T).0);\n}\nbool isIntersect(const Segment &a, const Segment &b) {\n return ccw(a.a, a.b, b.a) * ccw(a.a, a.b, b.b) <= 0 and\n ccw(b.a, b.b, a.a) * ccw(b.a, b.b, a.b) <= 0;\n}\nint isIntersect(const Circle &a, const Circle &b) {\n T d = abs(a.p - b.p);\n if (d > a.r + b.r + eps)\n return 4;\n if (eq(d, a.r + b.r))\n return 3;\n if (eq(d, abs(a.r - b.r)))\n return 1;\n if (d < abs(a.r - b.r) - eps)\n return 0;\n return 2;\n}\nT Dist(const Line &a, const Point &b) {\n Point c = Projection(a, b);\n return abs(c - b);\n}\nT Dist(const Segment &a, const Point &b) {\n if (dot(a.b - a.a, b - a.a) < eps)\n return abs(b - a.a);\n if (dot(a.a - a.b, b - a.b) < eps)\n return abs(b - a.b);\n return abs(cross(a.b - a.a, b - a.a)) / abs(a.b - a.a);\n}\nT Dist(const Segment &a, const Segment &b) {\n if (isIntersect(a, b))\n return (T).0;\n T res = min({Dist(a, b.a), Dist(a, b.b), Dist(b, a.a), Dist(b, a.b)});\n return res;\n}\nPoint Intersection(const Line &a, const Line &b) {\n T d1 = cross(a.b - a.a, b.b - b.a);\n T d2 = cross(a.b - a.a, a.b - b.a);\n if (eq(d1, (T)0.) and eq(d2, (T)0.))\n return b.a;\n return b.a + (b.b - b.a) * (d2 / d1);\n}\nPoly Intersection(const Circle &a, const Line &b) {\n Poly res;\n T d = Dist(b, a.p);\n if (d > a.r + eps)\n return res;\n Point h = Projection(b, a.p);\n if (eq(d, a.r)) {\n res.push_back(h);\n return res;\n }\n Point e = unit(b.b - b.a);\n T ph = sqrt(a.r * a.r - d * d);\n res.push_back(h - e * ph);\n res.push_back(h + e * ph);\n return res;\n}\nPoly Intersection(const Circle &a, const Segment &b) {\n Line c(b.a, b.b);\n Poly sub = Intersection(a, c);\n double xmi = min(b.a.X, b.b.X), xma = max(b.a.X, b.b.X);\n double ymi = min(b.a.Y, b.b.Y), yma = max(b.a.Y, b.b.Y);\n Poly res;\n rep(i, 0, sub.size()) {\n if (xmi <= sub[i].X + eps and sub[i].X - eps <= xma and\n ymi <= sub[i].Y + eps and sub[i].Y - eps <= yma) {\n res.push_back(sub[i]);\n }\n }\n return res;\n}\nPoly Intersection(const Circle &a, const Circle &b) {\n Poly res;\n int mode = isIntersect(a, b);\n T d = abs(a.p - b.p);\n if (mode == 4 or mode == 0)\n return res;\n if (mode == 3) {\n T t = a.r / (a.r + b.r);\n res.push_back(a.p + (b.p - a.p) * t);\n return res;\n }\n if (mode == 1) {\n if (b.r < a.r - eps) {\n res.push_back(a.p + (b.p - a.p) * (a.r / d));\n } else {\n res.push_back(b.p + (a.p - b.p) * (b.r / d));\n }\n return res;\n }\n T rc = (a.r * a.r + d * d - b.r * b.r) / d / (T)2.;\n T rs = sqrt(a.r * a.r - rc * rc);\n if (a.r - abs(rc) < eps)\n rs = 0;\n Point e = unit(b.p - a.p);\n res.push_back(a.p + rc * e + rs * e * Point(0, 1));\n res.push_back(a.p + rc * e + rs * e * Point(0, -1));\n return res;\n}\nPoly HalfplaneIntersection(vector<Line> &H) {\n sort(ALL(H), [&](Line &l1, Line &l2) { return cmp(l1.dir, l2.dir); });\n auto outside = [&](Line &L, Point p) -> bool {\n return cross(L.dir, p - L.a) < -eps;\n };\n deque<Line> deq;\n int sz = 0;\n rep(i, 0, SZ(H)) {\n while (sz > 1 and\n outside(H[i], Intersection(deq[sz - 1], deq[sz - 2]))) {\n deq.pop_back();\n sz--;\n }\n while (sz > 1 and outside(H[i], Intersection(deq[0], deq[1]))) {\n deq.pop_front();\n sz--;\n }\n if (sz > 0 and fabs(cross(H[i].dir, deq[sz - 1].dir)) < eps) {\n if (dot(H[i].dir, deq[sz - 1].dir) < 0) {\n return {};\n }\n if (outside(H[i], deq[sz - 1].a)) {\n deq.pop_back();\n sz--;\n } else\n continue;\n }\n deq.push_back(H[i]);\n sz++;\n }\n\n while (sz > 2 and outside(deq[0], Intersection(deq[sz - 1], deq[sz - 2]))) {\n deq.pop_back();\n sz--;\n }\n while (sz > 2 and outside(deq[sz - 1], Intersection(deq[0], deq[1]))) {\n deq.pop_front();\n sz--;\n }\n if (sz < 3)\n return {};\n deq.push_back(deq.front());\n Poly ret;\n rep(i, 0, sz) ret.push_back(Intersection(deq[i], deq[i + 1]));\n return ret;\n}\n\nT Area(const Poly &a) {\n T res = 0;\n int n = a.size();\n rep(i, 0, n) res += cross(a[i], a[(i + 1) % n]);\n return fabs(res / (T)2.);\n}\nT Area(const Poly &a, const Circle &b) {\n int n = a.size();\n if (n < 3)\n return (T).0;\n auto rec = [&](auto self, const Circle &c, const Point &p1,\n const Point &p2) {\n Point va = c.p - p1, vb = c.p - p2;\n T f = cross(va, vb), res = (T).0;\n if (eq(f, (T).0))\n return res;\n if (max(abs(va), abs(vb)) < c.r + eps)\n return f;\n if (Dist(Segment(p1, p2), c.p) > c.r - eps)\n return c.r * c.r * arg(vb * conj(va));\n auto u = Intersection(c, Segment(p1, p2));\n Poly sub;\n sub.push_back(p1);\n for (auto &x : u)\n sub.push_back(x);\n sub.push_back(p2);\n rep(i, 0, sub.size() - 1) res += self(self, c, sub[i], sub[i + 1]);\n return res;\n };\n T res = (T).0;\n rep(i, 0, n) res += rec(rec, b, a[i], a[(i + 1) % n]);\n return fabs(res / (T)2.);\n}\nT Area(const Circle &a, const Circle &b) {\n T d = abs(a.p - b.p);\n if (d >= a.r + b.r - eps)\n return (T).0;\n if (d <= abs(a.r - b.r) + eps) {\n T r = min(a.r, b.r);\n return M_PI * r * r;\n }\n T ath = acos((a.r * a.r + d * d - b.r * b.r) / d / a.r / (T)2.);\n T res = a.r * a.r * (ath - sin(ath * 2) / (T)2.);\n T bth = acos((b.r * b.r + d * d - a.r * a.r) / d / b.r / (T)2.);\n res += b.r * b.r * (bth - sin(bth * 2) / (T)2.);\n return fabs(res);\n}\nbool isConvex(const Poly &a) {\n int n = a.size();\n int cur, pre, nxt;\n rep(i, 0, n) {\n pre = (i - 1 + n) % n;\n nxt = (i + 1) % n;\n cur = i;\n if (ccw(a[pre], a[cur], a[nxt]) == -1)\n return 0;\n }\n return 1;\n}\nint isContained(const Poly &a,\n const Point &b) { // 0:not contain,1:on edge,2:contain\n\n bool res = 0;\n int n = a.size();\n rep(i, 0, n) {\n Point p = a[i] - b, q = a[(i + 1) % n] - b;\n if (p.Y > q.Y)\n swap(p, q);\n if (p.Y < eps and eps < q.Y and cross(p, q) > eps)\n res ^= 1;\n if (eq(cross(p, q), (T).0) and dot(p, q) < eps)\n return 1;\n }\n return (res ? 2 : 0);\n}\nPoly ConvexHull(Poly &a) {\n sort(ALL(a), [](const Point &p, const Point &q) {\n return (eq(p.Y, q.Y) ? p.X < q.X : p.Y < q.Y);\n });\n a.erase(unique(ALL(a)), a.end());\n int n = a.size(), k = 0;\n Poly res(n * 2);\n for (int i = 0; i < n; res[k++] = a[i++]) {\n while (k >= 2 and\n cross(res[k - 1] - res[k - 2], a[i] - res[k - 1]) < eps)\n k--;\n }\n for (int i = n - 2, t = k + 1; i >= 0; res[k++] = a[i--]) {\n while (k >= t and\n cross(res[k - 1] - res[k - 2], a[i] - res[k - 1]) < eps)\n k--;\n }\n res.resize(k - 1);\n return res;\n}\nT Diam(const Poly &a) {\n int n = a.size();\n int x = 0, y = 0;\n rep(i, 1, n) {\n if (a[i].Y > a[x].Y)\n x = i;\n if (a[i].Y < a[y].Y)\n y = i;\n }\n T res = abs(a[x] - a[y]);\n int i = x, j = y;\n do {\n if (cross(a[(i + 1) % n] - a[i], a[(j + 1) % n] - a[j]) < 0)\n i = (i + 1) % n;\n else\n j = (j + 1) % n;\n chmax(res, abs(a[i] - a[j]));\n } while (i != x or j != y);\n return res;\n}\nPoly Cut(const Poly &a, const Line &l) {\n int n = a.size();\n Poly res;\n rep(i, 0, n) {\n Point p = a[i], q = a[(i + 1) % n];\n if (ccw(l.a, l.b, p) != -1)\n res.push_back(p);\n if (ccw(l.a, l.b, p) * ccw(l.a, l.b, q) < 0)\n res.push_back(Intersection(Line(p, q), l));\n }\n return res;\n}\n\nT Closest(Poly &a) {\n int n = a.size();\n if (n <= 1)\n return 0;\n sort(ALL(a), [&](Point a, Point b) {\n return (eq(a.X, b.X) ? a.Y < b.Y : a.X < b.X);\n });\n Poly buf(n);\n auto rec = [&](auto self, int lb, int rb) -> T {\n if (rb - lb <= 1)\n return (T)INF;\n int mid = (lb + rb) >> 1;\n auto x = a[mid].X;\n T res = min(self(self, lb, mid), self(self, mid, rb));\n inplace_merge(a.begin() + lb, a.begin() + mid, a.begin() + rb,\n [&](auto p, auto q) { return p.Y < q.Y; });\n int ptr = 0;\n rep(i, lb, rb) {\n if (abs(a[i].X - x) >= res)\n continue;\n rep(j, 0, ptr) {\n auto sub = a[i] - buf[ptr - 1 - j];\n if (sub.Y >= res)\n break;\n chmin(res, abs(sub));\n }\n buf[ptr++] = a[i];\n }\n return res;\n };\n return rec(rec, 0, n);\n}\n\nCircle Incircle(const Point &a, const Point &b, const Point &c) {\n T A = abs(b - c), B = abs(c - a), C = abs(a - b);\n Point p(A * a.X + B * b.X + C * c.X, A * a.Y + B * b.Y + C * c.Y);\n p /= (A + B + C);\n T r = Dist(Line(a, b), p);\n return Circle(p, r);\n}\nCircle Circumcircle(const Point &a, const Point &b, const Point &c) {\n Line l1((a + b) / (T)2., (a + b) / (T)2. + (b - a) * Point(0, 1));\n Line l2((b + c) / (T)2., (b + c) / (T)2. + (c - b) * Point(0, 1));\n Point p = Intersection(l1, l2);\n return Circle(p, abs(p - a));\n}\nPoly tangent(const Point &a, const Circle &b) {\n return Intersection(b, Circle(a, sqrt(norm(b.p - a) - b.r * b.r)));\n}\nvector<Line> tangent(const Circle &a, const Circle &b) {\n vector<Line> res;\n T d = abs(a.p - b.p);\n if (eq(d, (T)0.))\n return res;\n Point u = unit(b.p - a.p);\n Point v = u * Point(0, 1);\n for (int t : {-1, 1}) {\n T h = (a.r + b.r * t) / d;\n if (eq(h * h, (T)1.)) {\n res.push_back(Line(a.p + (h > 0 ? u : -u) * a.r,\n a.p + (h > 0 ? u : -u) * a.r + v));\n } else if (1 > h * h) {\n Point U = u * h, V = v * sqrt(1 - h * h);\n res.push_back(Line(a.p + (U + V) * a.r, b.p - (U + V) * (b.r * t)));\n res.push_back(Line(a.p + (U - V) * a.r, b.p - (U - V) * (b.r * t)));\n }\n }\n return res;\n}\n\n/**\n * @brief Geometry\n */\n\nint main() {\nwhile(1) {\n const long double L = 100.0L;\n int N;\n long double W;\n cin >> N >> W;\n if (N == 0) return 0;\n vector<Point> P(N);\n vector<long double> H(N);\n rep(i,0,N) {\n long double x, y;\n cin >> x >> y;\n P[i] = Point(x,y);\n cin >> H[i];\n }\n auto Judge = [&](long double M) -> bool {\n vector<Circle> C(N);\n rep(i,0,N) {\n if (M <= H[i]) C[i] = Circle(P[i],M);\n else C[i] = Circle(P[i],sqrt(M*H[i]*2.0L - H[i]*H[i]));\n }\n long double D;\n if (M <= W) D = M;\n else D = sqrt(M*W*2.0L - W*W);\n if (D >= 50.0) return false;\n vector<Segment> S(4);\n S[0] = Segment(Point(D,D),Point(D,L-D));\n S[1] = Segment(Point(D,D),Point(L-D,D));\n S[2] = Segment(Point(L-D,D),Point(L-D,L-D));\n S[3] = Segment(Point(D,L-D),Point(L-D,L-D));\n vector<Point> Cand;\n Cand.push_back(Point(D,D));\n Cand.push_back(Point(L-D,D));\n Cand.push_back(Point(D,L-D));\n Cand.push_back(Point(L-D,L-D));\n rep(i,0,4) {\n rep(j,0,N) {\n auto V = Intersection(C[j],S[i]);\n for (auto VP : V) Cand.push_back(VP);\n }\n }\n rep(i,0,N) {\n rep(j,i+1,N) {\n auto V = Intersection(C[i],C[j]);\n for (auto VP : V) {\n if (D <= VP.real() && VP.real() <= L-D && D <= VP.imag() && VP.imag() <= L-D) Cand.push_back(VP);\n }\n }\n }\n for (auto PP : Cand) {\n bool check = true;\n rep(i,0,N) {\n if (abs(C[i].p-PP) < C[i].r - eps) {\n check = false;\n break;\n }\n }\n if (check) return true;\n }\n return false;\n };\n long double OK = 0.0L, NG = 140.0;\n rep(_,0,100) {\n long double MID = (OK + NG) / 2;\n if (Judge(MID)) OK = MID;\n else NG = MID;\n }\n printf(\"%.12Lf\\n\",(OK+NG)/2.0L);\n}\n}", "accuracy": 1, "time_ms": 630, "memory_kb": 3560, "score_of_the_acc": -0.4338, "final_rank": 6 }, { "submission_id": "aoj_1342_8526701", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\n#define rep(i, n) for (int i = 0; i < (n); i++)\n#define per(i, n) for (int i = (n)-1; i >= 0; i--)\n#define rep2(i, l, r) for (int i = (l); i < (r); i++)\n#define per2(i, l, r) for (int i = (r)-1; i >= (l); i--)\n#define each(e, x) for (auto &e : x)\n#define sz(x) (int)x.size()\n#define all(x) begin(x), end(x)\n#define rall(x) rbegin(x), rend(x)\n#define eb emplace_back\n#define MM << ' ' <<\n#define TT template <typename T>\nusing ll = long long;\nusing pii = pair<int, int>;\nusing pll = pair<ll, ll>;\n\nTT bool chmin(T &x, T y) { return x > y ? (x = y, true) : false; }\nTT bool chmax(T &x, T y) { return x < y ? (x = y, true) : false; }\n\nTT using minheap = priority_queue<T, vector<T>, greater<T>>;\nTT using maxheap = priority_queue<T>;\n\nTT void print(vector<T> v, T x = 0) {\n int n = sz(v);\n rep(i, n) cout << v[i] + x << (i == n - 1 ? '\\n' : ' ');\n if (n == 0) cout << '\\n';\n}\n\nTT void printn(vector<T> v, T x = 0) {\n int n = sz(v);\n rep(i, n) cout << v[i] + x << '\\n';\n}\n\nTT void rearrange(vector<T> &v) {\n sort(all(v));\n v.erase(unique(all(v)), end(v));\n}\n\nTT int lb(vector<T> &v, T x) { return lower_bound(all(v), x) - begin(v); }\nTT int ub(vector<T> &v, T x) { return upper_bound(all(v), x) - begin(v); }\n\nconst int inf = (1 << 30) - 1;\nconst ll INF = (1LL << 60) - 1;\n\nstruct io_setup {\n io_setup() {\n ios_base::sync_with_stdio(false);\n cin.tie(NULL);\n cout << fixed << setprecision(15);\n }\n} io_setup;\n\n#define X(p) real(p)\n#define Y(p) imag(p)\nusing R = double;\nusing P = complex<R>;\nusing VP = vector<P>;\n\nconst R EPS = 1e-7;\nconst R pi = acos(-1.0);\n\nstruct C {\n P p;\n R r;\n C() {}\n C(P p, R r) : p(p), r(r) {}\n};\n\nP rot(P p, R t) {\n return p * P(cos(t), sin(t));\n}\n\nR dot(P p, P q) {\n return X(p) * X(q) + Y(p) * Y(q);\n}\n\nR dist(P p, P q) {\n return abs(p - q);\n}\n\nVP crosspoint(C c1, C c2) {\n R d = abs(c1.p - c2.p);\n R a = acos((c1.r * c1.r + d * d - c2.r * c2.r) / (2 * c1.r * d));\n R t = atan2(Y(c2.p) - Y(c1.p), X(c2.p) - X(c1.p));\n VP ret;\n if (d < max(c1.r, c2.r) - min(c1.r, c2.r) || c1.r + c2.r < d)\n return ret;\n P p1 = c1.p + rot(P(c1.r, 0.0), t + a);\n P p2 = c1.p + rot(P(c1.r, 0.0), t - a);\n ret.eb(p1), ret.eb(p2);\n return ret;\n}\n\nint main() {\n auto calc = [](double h, double r) {\n chmin(h, r);\n return sqrt(r * r - (r - h) * (r - h));\n };\n auto cx = [](C c, R x) {\n VP ret;\n R d = abs(X(c.p) - x);\n if (d > c.r)\n return ret;\n R h = sqrt(c.r * c.r - d * d);\n ret.eb(x, Y(c.p) - h);\n ret.eb(x, Y(c.p) + h);\n return ret;\n };\n auto cy = [](C c, R y) {\n VP ret;\n R d = abs(Y(c.p) - y);\n if (d > c.r)\n return ret;\n R w = sqrt(c.r * c.r - d * d);\n ret.eb(X(c.p) - w, y);\n ret.eb(X(c.p) + w, y);\n return ret;\n };\n while (1) {\n int n, w;\n cin >> n >> w;\n if (!n)\n break;\n vector<int> x(n), y(n), h(n);\n rep(i, n) cin >> x[i] >> y[i] >> h[i];\n VP pn(n);\n rep(i, n) pn[i] = P(x[i], y[i]);\n double ok = 0, ng = 100000;\n rep(ti, 100) {\n double mid = (ok + ng) / 2;\n double dw = calc(w, mid);\n if (dw > 50) {\n ng = mid;\n continue;\n }\n vector<R> dn(n);\n rep(i, n) dn[i] = calc(h[i], mid);\n VP ls{P(50, 50)};\n rep(i, n) {\n C ci(pn[i], calc(h[i], mid));\n VP tmp;\n each(p, cx(ci, dw)) tmp.eb(p);\n each(p, cx(ci, 100 - dw)) tmp.eb(p);\n each(p, cy(ci, dw)) tmp.eb(p);\n each(p, cy(ci, 100 - dw)) tmp.eb(p);\n rep(j, n) if (i != j) each(p, crosspoint(ci, C(pn[j], dn[j]))) tmp.eb(p);\n if (empty(tmp))\n ls.eb(pn[i] + P(mid, 0));\n each(p, tmp) ls.eb(p);\n }\n bool f = false;\n R bl = dw - EPS, br = 100 - dw + EPS;\n each(p, ls) {\n bool fl = (bl < min(X(p), Y(p)) && max(X(p), Y(p)) < br);\n rep(i, n) fl &= dist(p, pn[i]) > dn[i] - EPS;\n if (fl)\n f = true;\n }\n (f ? ok : ng) = mid;\n }\n cout << ok << endl;\n }\n}", "accuracy": 1, "time_ms": 980, "memory_kb": 4024, "score_of_the_acc": -1.0062, "final_rank": 16 }, { "submission_id": "aoj_1342_7236025", "code_snippet": "//#define _GLIBCXX_DEBUG\n\n#include<bits/stdc++.h>\nusing namespace std;\n\n#define endl '\\n'\n#define lfs cout<<fixed<<setprecision(10)\n#define ALL(a) (a).begin(),(a).end()\n#define ALLR(a) (a).rbegin(),(a).rend()\n#define UNIQUE(a) (a).erase(unique((a).begin(),(a).end()),(a).end())\n#define spa << \" \" <<\n#define fi first\n#define se second\n#define MP make_pair\n#define MT make_tuple\n#define PB push_back\n#define EB emplace_back\n#define rep(i,n,m) for(ll i = (n); i < (ll)(m); i++)\n#define rrep(i,n,m) for(ll i = (ll)(m) - 1; i >= (ll)(n); i--)\nusing ll = long long;\nusing ld = long double;\nconst ll MOD1 = 1e9+7;\nconst ll MOD9 = 998244353;\nconst ll INF = 1e18;\nusing P = pair<ll, ll>;\ntemplate<typename T> using PQ = priority_queue<T>;\ntemplate<typename T> using QP = priority_queue<T,vector<T>,greater<T>>;\ntemplate<typename T1, typename T2>bool chmin(T1 &a,T2 b){if(a>b){a=b;return true;}else return false;}\ntemplate<typename T1, typename T2>bool chmax(T1 &a,T2 b){if(a<b){a=b;return true;}else return false;}\nll median(ll a,ll b, ll c){return a+b+c-max({a,b,c})-min({a,b,c});}\nvoid ans1(bool x){if(x) cout<<\"Yes\"<<endl;else cout<<\"No\"<<endl;}\nvoid ans2(bool x){if(x) cout<<\"YES\"<<endl;else cout<<\"NO\"<<endl;}\nvoid ans3(bool x){if(x) cout<<\"Yay!\"<<endl;else cout<<\":(\"<<endl;}\ntemplate<typename T1,typename T2>void ans(bool x,T1 y,T2 z){if(x)cout<<y<<endl;else cout<<z<<endl;} \ntemplate<typename T1,typename T2,typename T3>void anss(T1 x,T2 y,T3 z){ans(x!=y,x,z);}; \ntemplate<typename T>void debug(const T &v,ll h,ll w,string sv=\" \"){for(ll i=0;i<h;i++){cout<<v[i][0];for(ll j=1;j<w;j++)cout<<sv<<v[i][j];cout<<endl;}};\ntemplate<typename T>void debug(const T &v,ll n,string sv=\" \"){if(n!=0)cout<<v[0];for(ll i=1;i<n;i++)cout<<sv<<v[i];cout<<endl;};\ntemplate<typename T>void debug(const vector<T>&v){debug(v,v.size());}\ntemplate<typename T>void debug(const vector<vector<T>>&v){for(auto &vv:v)debug(vv,vv.size());}\ntemplate<typename T>void debug(stack<T> st){while(!st.empty()){cout<<st.top()<<\" \";st.pop();}cout<<endl;}\ntemplate<typename T>void debug(queue<T> st){while(!st.empty()){cout<<st.front()<<\" \";st.pop();}cout<<endl;}\ntemplate<typename T>void debug(deque<T> st){while(!st.empty()){cout<<st.front()<<\" \";st.pop_front();}cout<<endl;}\ntemplate<typename T>void debug(PQ<T> st){while(!st.empty()){cout<<st.top()<<\" \";st.pop();}cout<<endl;}\ntemplate<typename T>void debug(QP<T> st){while(!st.empty()){cout<<st.top()<<\" \";st.pop();}cout<<endl;}\ntemplate<typename T>void debug(const set<T>&v){for(auto z:v)cout<<z<<\" \";cout<<endl;}\ntemplate<typename T>void debug(const multiset<T>&v){for(auto z:v)cout<<z<<\" \";cout<<endl;}\ntemplate<typename T,size_t size>void debug(const array<T, size> &a){for(auto z:a)cout<<z<<\" \";cout<<endl;}\ntemplate<typename T,typename V>void debug(const map<T,V>&v){for(auto z:v)cout<<\"[\"<<z.first<<\"]=\"<<z.second<<\",\";cout<<endl;}\ntemplate<typename T>vector<vector<T>>vec(ll x, ll y, T w){vector<vector<T>>v(x,vector<T>(y,w));return v;}\nll gcd(ll x,ll y){ll r;while(y!=0&&(r=x%y)!=0){x=y;y=r;}return y==0?x:y;}\n//vector<ll>dx={1,-1,0,0,1,1,-1,-1};vector<ll>dy={0,0,1,-1,1,-1,1,-1};\ntemplate<typename T>vector<T> make_v(size_t a,T b){return vector<T>(a,b);}\ntemplate<typename... Ts>auto make_v(size_t a,Ts... ts){return vector<decltype(make_v(ts...))>(a,make_v(ts...));}\ntemplate<typename T1, typename T2>ostream &operator<<(ostream &os, const pair<T1, T2>&p){return os << p.first << \" \" << p.second;}\ntemplate<typename T>ostream &operator<<(ostream &os, const vector<T> &v){for(auto &z:v)os << z << \" \";cout<<\"|\"; return os;}\ntemplate<typename T>void rearrange(vector<int>&ord, vector<T>&v){\n auto tmp = v;\n for(int i=0;i<tmp.size();i++)v[i] = tmp[ord[i]];\n}\ntemplate<typename Head, typename... Tail>void rearrange(vector<int>&ord,Head&& head, Tail&&... tail){\n rearrange(ord, head);\n rearrange(ord, tail...);\n}\ntemplate<typename T> vector<int> ascend(const vector<T>&v){\n vector<int>ord(v.size());iota(ord.begin(),ord.end(),0);\n sort(ord.begin(),ord.end(),[&](int i,int j){return v[i]<v[j];});\n return ord;\n}\ntemplate<typename T> vector<int> descend(const vector<T>&v){\n vector<int>ord(v.size());iota(ord.begin(),ord.end(),0);\n sort(ord.begin(),ord.end(),[&](int i,int j){return v[i]>v[j];});\n return ord;\n}\ntemplate<typename T> vector<T> inv_perm(const vector<T>&ord){\n vector<T>inv(ord.size());\n for(int i=0;i<ord.size();i++)inv[ord[i]] = i;\n return inv;\n}\nll FLOOR(ll n,ll div){assert(div>0);return n>=0?n/div:(n-div+1)/div;}\nll CEIL(ll n,ll div){assert(div>0);return n>=0?(n+div-1)/div:n/div;}\nll digitsum(ll n){ll ret=0;while(n){ret+=n%10;n/=10;}return ret;}\nll modulo(ll n,ll d){return (n%d+d)%d;};\ntemplate<typename T>T min(const vector<T>&v){return *min_element(v.begin(),v.end());}\ntemplate<typename T>T max(const vector<T>&v){return *max_element(v.begin(),v.end());}\ntemplate<typename T>T acc(const vector<T>&v){return accumulate(v.begin(),v.end(),T(0));};\ntemplate<typename T>T reverse(const T &v){return T(v.rbegin(),v.rend());};\n//mt19937 mt(chrono::steady_clock::now().time_since_epoch().count());\nint popcount(ll x){return __builtin_popcountll(x);};\nint poplow(ll x){return __builtin_ctzll(x);};\nint pophigh(ll x){return 63 - __builtin_clzll(x);};\ntemplate<typename T>T poll(queue<T> &q){auto ret=q.front();q.pop();return ret;};\ntemplate<typename T>T poll(priority_queue<T> &q){auto ret=q.top();q.pop();return ret;};\ntemplate<typename T>T poll(QP<T> &q){auto ret=q.top();q.pop();return ret;};\ntemplate<typename T>T poll(stack<T> &s){auto ret=s.top();s.pop();return ret;};\nll MULT(ll x,ll y){if(LLONG_MAX/x<=y)return LLONG_MAX;return x*y;}\nll POW2(ll x, ll k){ll ret=1,mul=x;while(k){if(mul==LLONG_MAX)return LLONG_MAX;if(k&1)ret=MULT(ret,mul);mul=MULT(mul,mul);k>>=1;}return ret;}\nll POW(ll x, ll k){ll ret=1;for(int i=0;i<k;i++){if(LLONG_MAX/x<=ret)return LLONG_MAX;ret*=x;}return ret;}\ntemplate< typename T = int >\nstruct edge {\n int to;\n T cost;\n int id;\n edge():id(-1){};\n edge(int to, T cost = 1, int id = -1):to(to), cost(cost), id(id){}\n operator int() const { return to; }\n};\n\ntemplate<typename T>\nusing Graph = vector<vector<edge<T>>>;\ntemplate<typename T>\nGraph<T>revgraph(const Graph<T> &g){\n Graph<T>ret(g.size());\n for(int i=0;i<g.size();i++){\n for(auto e:g[i]){\n int to = e.to;\n e.to = i;\n ret[to].push_back(e);\n }\n }\n return ret;\n}\ntemplate<typename T>\nGraph<T> readGraph(int n,int m,int indexed=1,bool directed=false,bool weighted=false){\n Graph<T> ret(n);\n for(int es = 0; es < m; es++){\n int u,v;\n T w=1;\n cin>>u>>v;u-=indexed,v-=indexed;\n if(weighted)cin>>w;\n ret[u].emplace_back(v,w,es);\n if(!directed)ret[v].emplace_back(u,w,es);\n }\n return ret;\n}\ntemplate<typename T>\nGraph<T> readParent(int n,int indexed=1,bool directed=true){\n Graph<T>ret(n);\n for(int i=1;i<n;i++){\n int p;cin>>p;\n p-=indexed;\n ret[p].emplace_back(i);\n if(!directed)ret[i].emplace_back(p);\n }\n return ret;\n}\n\ntemplate <typename F>\nclass\n#if defined(__has_cpp_attribute) && __has_cpp_attribute(nodiscard)\n [[nodiscard]]\n#endif // defined(__has_cpp_attribute) && __has_cpp_attribute(nodiscard)\nFixPoint final : private F\n{\npublic:\n template <typename G>\n explicit constexpr FixPoint(G&& g) noexcept\n : F{std::forward<G>(g)}\n {}\n\n template <typename... Args>\n constexpr decltype(auto)\n operator()(Args&&... args) const\n#if !defined(__GNUC__) || defined(__clang__) || __GNUC__ >= 9\n noexcept(noexcept(F::operator()(std::declval<FixPoint>(), std::declval<Args>()...)))\n#endif // !defined(__GNUC__) || defined(__clang__) || __GNUC__ >= 9\n {\n return F::operator()(*this, std::forward<Args>(args)...);\n }\n}; // class FixPoint\n\n\n#if defined(__cpp_deduction_guides)\ntemplate <typename F>\nFixPoint(F&&)\n -> FixPoint<std::decay_t<F>>;\n#endif // defined(__cpp_deduction_guides)\n\n\nnamespace\n{\n template <typename F>\n#if !defined(__has_cpp_attribute) || !__has_cpp_attribute(nodiscard)\n# if defined(__GNUC__) && (__GNUC__ > 3 || __GNUC__ == 3 && __GNUC_MINOR__ >= 4)\n __attribute__((warn_unused_result))\n# elif defined(_MSC_VER) && _MSC_VER >= 1700 && defined(_Check_return_)\n _Check_return_\n# endif // defined(__GNUC__) && (__GNUC__ > 3 || __GNUC__ == 3 && __GNUC_MINOR__ >= 4)\n#endif // !defined(__has_cpp_attribute) || !__has_cpp_attribute(nodiscard)\n inline constexpr decltype(auto)\n makeFixPoint(F&& f) noexcept\n{\n return FixPoint<std::decay_t<F>>{std::forward<std::decay_t<F>>(f)};\n}\n} // namespace\n\nusing Real = long double;\nusing Point = complex<Real>;\nconst Real EPS = 1e-10, PI = acos((Real)(-1.0));\n\ninline bool eq(Real a, Real b) { return fabs(b - a) < EPS; }\ninline bool eq(Point a, Point b) { return fabs(b - a) < EPS; }\n\nPoint operator*(const Point &p, const Real &d) {\n return Point(real(p) * d, imag(p) * d);\n}\n\nistream &operator>>(istream &is, Point &p) {\n Real a, b;\n is >> a >> b;\n p = Point(a, b);\n return is;\n}\n\nostream &operator<<(ostream &os, Point &p) {\n return os << fixed << setprecision(10) << p.real() << \" \" << p.imag();\n}\n\nPoint rotate(Real theta, const Point &p) {\n return Point(cos(theta) * p.real() - sin(theta) * p.imag(), sin(theta) * p.real() + cos(theta) * p.imag());\n}\n\nnamespace std {\n bool operator<(const Point& a, const Point& b) {\n return !eq(a.real(), b.real()) ? a.real() < b.real() : a.imag() < b.imag();\n }\n}\n\nstruct Line {\n Point a, b;\n Line() = default;\n Line(Point a, Point b): a(a), b(b) {}\n\n friend ostream &operator<<(ostream &os, Line &p) {\n return os << p.a << \" to \" << p.b;\n }\n\n friend istream &operator>>(istream &is, Line &a) {\n return is >> a.a >> a.b;\n }\n};\n\nstruct Circle {\n Point c;\n Real r;\n Circle() = default;\n Circle(Point c, Real r): c(c), r(r) {}\n\n friend ostream &operator<<(ostream &os, Circle &c) {\n return os << c.c << \": \" << c.r;\n }\n\n friend istream &operator>>(istream &is, Circle &c) {\n return is >> c.c >> c.r;\n }\n};\n\nReal cross(const Point &a, const Point &b) {\n return real(a) * imag(b) - real(b) * imag(a);\n}\n\nReal dot(const Point &a, const Point &b) {\n return real(a) * real(b) + imag(a) * imag(b);\n}\n\nstruct Segment : Line {\n Segment() = default;\n Segment(Point a, Point b) : Line(a, b) {}\n};\n\nint ccw(const Point &a, Point b, Point c) {\n b = b - a, c = c - a;\n if (cross(b, c) > EPS) return +1; // COUNTER_CLOCKWISE\n if (cross(b, c) < -EPS) return -1; // CLOCKWISE\n if (dot(b, c) < 0) return +2; // ONLINE_BACK\n if (norm(b) < norm(c)) return -2; // ONLINE_FRONT\n return 0; // ON_SEGMENT\n}\n\nbool intersect(const Segment &s, const Point &p) {\n return ccw(s.a, s.b, p) == 0;\n}\n\nbool intersect(const Segment &s, const Segment&t) {\n 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;\n}\n\nbool intersect(const Line &l, const Segment& s) {\n return cross(l.b - l.a, s.a - l.a) * cross(l.b - l.a, s.b - l.a) < EPS;\n}\n\nint intersect(const Circle &c, const Circle &d) {\n Real dis = fabs(c.c - d.c);\n\n if (dis > c.r + d.r + EPS) return 4;\n else if (eq(dis, c.r + d.r)) return 3;\n else if (eq(dis, abs(c.r - d.r))) return 1;\n else if (dis + min(c.r, d.r) < max(c.r, d.r) - EPS) return 0;\n else return 2;\n}\n\nbool parallel(const Line &a, const Line &b) {\n return abs(cross(a.b - a.a, b.b - b.a)) < EPS;\n}\n\nbool orthogonal(const Line &a, const Line &b) {\n return abs(dot(a.b - a.a, b.b - b.a)) < EPS;\n}\n\nPoint projection(const Line& s, const Point &p) {\n double t = dot(p - s.a, s.b - s.a) / norm(s.b - s. a);\n return s.a + (s.b - s.a) * t;\n}\n\nPoint projection(const Segment& l, const Point &p) {\n return projection(Line(l), p);\n}\n\nPoint reflection(const Line &l, const Point &p) {\n Point r = projection(l, p);\n return p + (r - p) * 2;\n}\n\nReal distance(const Line &l, const Point &p) {\n Point r = projection(l, p);\n return fabs(p - r);\n}\n\nReal distance(const Segment&s, const Point &p) {\n Point r = projection(s, p);\n if (intersect(s, r)) return abs(r - p);\n return min(abs(s.a - p), abs(s.b - p));\n}\n\nReal distance(const Segment& a, const Segment& b) {\n if (intersect(a, b)) return 0;\n return min({\n distance(a, b.a),\n distance(a, b.b),\n distance(b, a.a),\n distance(b, a.b)\n });\n}\n\nPoint crosspoint(const Line &l, const Line &m) {\n Real A = cross(l.b - l.a, m.b - m.a);\n Real B = cross(l.b - l.a, l.b - m.a);\n if (eq(abs(A), 0.0) && eq(abs(B), 0.0)) return m.a;\n return m.a + (m.b - m.a) * B / A;\n}\n\nPoint crosspoint(const Segment& l, const Segment& m) {\n return crosspoint(Line(l), Line(m));\n}\n\nLine bisector_of_angle(const Point& a, const Point& b, const Point& c) {\n Real d_ab = fabs(a - b);\n Real d_bc = fabs(b - c);\n Point c_ = b + (c - b) * d_ab / d_bc;\n\n return Line(b, (a + c_) * 0.5);\n}\n\nLine bisector(const Point& a, const Point& b) {\n Point c = (a + b) * 0.5;\n Point d = c + rotate(PI / 2, b - c);\n\n return Line(c, d);\n}\n\nint intersect(const Circle &c, const Line &l) {\n Real d = distance(l, c.c);\n\n if (d > c.r + EPS) return 0;\n if (eq(d, c.r)) return 1;\n return 2;\n}\n\npair<Point, Point> crosspoint(const Circle& c, const Line& l) {\n Real d = distance(l, c.c);\n\n Point p = projection(l, c.c);\n if (intersect(c, l) == 1) return { p, p };\n\n Real d2 = sqrt(c.r * c.r - d * d);\n Point v = (l.a - l.b) * (1.0 / fabs(l.a - l.b));\n\n return { p + d2 * v, p - d2 * v };\n}\n\npair<Point, Point> crosspoint(const Circle &c1, const Circle &c2) {\n Real d = fabs(c1.c - c2.c);\n Real x1 = (c1.r * c1.r + d * d - c2.r * c2.r) / 2 / d;\n Real y = sqrt(c1.r * c1.r - x1 * x1);\n\n Point base = (c2.c * x1 + c1.c * (d - x1)) * (1.0 / d);\n Point v = rotate(PI / 2, c1.c - base);\n v = v / fabs(v);\n\n return { base + v * y, base - v * y };\n}\n\npair<Point, Point> tangent(const Circle &c, const Point &p) {\n Real d = fabs(c.c - p);\n Real d2 = sqrt(d * d - c.r * c.r);\n Real a = acos(d2 / d);\n\n\n Point q = c.c - p;\n q = q / fabs(q);\n\n// cout << d << \" \" << d2 << \" \" << a << \" \" << q << endl;\n return {\n p + rotate(a, q) * d2,\n p + rotate(-a, q) * d2\n };\n}\n\nvector<Segment> common_tangent(Circle c1, Circle c2) {\n int isect = intersect(c1, c2);\n Real d = fabs(c1.c - c2.c);\n Point v = (c2.c - c1.c) / d;\n\n vector<Segment> res;\n\n if (isect == 0) {\n return {};\n } else if (isect == 1) {\n auto [p1, p2] = crosspoint(c1, Line(c1.c, c2.c));\n if (eq(fabs(p2 - c2.c), c2.r)) swap(p1, p2);\n\n Point v = p1 + rotate(PI/2, c2.c - p1);\n return { Segment(p1, v) };\n } else if (isect == 2) {\n } else if (isect == 3) {\n auto [p1, p2] = crosspoint(c1, c2);\n res.emplace_back(p1, rotate(PI / 2, c1.c - p1) + p1);\n } else {\n Real a = d * c1.r / (c1.r + c2.r);\n Point p = c1.c + v * a;\n\n Real d1 = sqrt(a * a - c1.r * c1.r), d2 = sqrt(norm(p - c2.c) - c2.r * c2.r);\n Real theta = acos(d1 / a);\n\n// cout << d << endl;\n// cout << a << \" \" << d1 << \" \" << d2 << \" \" << theta << \": \" << p << endl;\n\n Point e = (c1.c - p) * (1.0 / fabs(c1.c - p));\n Point e1 = rotate(theta, e), e2 = rotate(-theta, e);\n\n res.emplace_back(p + e1 * d1, p - e1 * d2);\n res.emplace_back(p + e2 * d1, p - e2 * d2);\n }\n\n if (eq(c1.r, c2.r)) {\n Point e = rotate(PI / 2, v);\n res.push_back(Segment(c1.c + e * c1.r, c2.c + e * c1.r));\n res.push_back(Segment(c1.c - e * c1.r, c2.c - e * c1.r));\n return res;\n }\n\n if (c1.r > c2.r) swap(c1, c2);\n\n v = c1.c - c2.c;\n Point p = v * (1.0 / (c2.r - c1.r)) * c2.r + c2.c;\n\n auto [q1, q2] = tangent(c1, p);\n res.emplace_back(q1, crosspoint(c2, Line(p, q1)).first);\n res.emplace_back(q2, crosspoint(c2, Line(p, q2)).first);\n\n return res;\n}\n\nusing Polygon = vector<Point>;\n\nistream& operator>>(istream& is, Polygon &p) {\n for (int i = 0; i < p.size(); i++) is >> p[i];\n return is;\n}\n\nostream& operator<<(ostream& os, Polygon &p) {\n for (int i = 0; i < p.size(); i++) os << p[i];\n return os;\n}\n\nReal area(const Polygon& p) {\n Real res = 0;\n for (int i = 0; i < p.size(); i++) {\n res += cross(p[i], p[(i + 1) % p.size()]);\n }\n return abs(res)/2;\n}\n\nint contains(Polygon g,Point p){\n int n=g.size();\n bool x=false;\n for(int i=0;i<n;i++){\n Point a=g[i]-p,b=g[(i+1)%n]-p;\n if(fabs(cross(a,b)) < EPS and dot(a,b) < EPS) return 1;\n if(a.imag()>b.imag()) swap(a,b);\n if(a.imag() < EPS and EPS < b.imag() and cross(a,b) > EPS ) {\n// cout << a << \" --- \" << b << \": \" << cross(a, b) << endl;\n x = !x;\n }\n }\n return (x?2:0);\n}\n\nPolygon convex_hull(Polygon Q) {\n sort(ALL(Q));\n\n Polygon upper({Q[0]}), lower({Q[0]});\n\n rep(i, 1, Q.size()) {\n while (upper.size() >= 2) {\n int _ccw = ccw(upper[upper.size() - 2], upper[upper.size() - 1], Q[i]);\n if (_ccw == 1) {\n upper.pop_back();\n } else {\n break;\n }\n }\n if (upper.size() <= 1) {\n upper.push_back(Q[i]);\n } else if (upper.size() > 1) {\n int _ccw = ccw(upper[upper.size() - 2], upper[upper.size() - 1], Q[i]);\n if (_ccw == -1 || _ccw == -2) {\n upper.push_back(Q[i]);\n }\n }\n }\n rep(i, 1, Q.size()) {\n while (lower.size() >= 2) {\n int _ccw = ccw(lower[lower.size() - 2], lower[lower.size() - 1], Q[i]);\n if (_ccw == -1) {\n lower.pop_back();\n } else {\n break;\n }\n }\n\n if (lower.size() <= 1) {\n lower.push_back(Q[i]);\n } else if (lower.size() > 1) {\n int _ccw = ccw(lower[lower.size() - 2], lower[lower.size() - 1], Q[i]);\n if (_ccw == 1 || _ccw == -2) {\n lower.push_back(Q[i]);\n }\n }\n }\n\n Polygon ret;\n\n rep(i, 0, lower.size()) {\n ret.push_back(lower[i]);\n// cout << lower[i] << endl;\n }\n\n// cout << \" | \" << endl;\n\n rrep(i, 0, upper.size()) {\n if (!eq(lower[0], upper[i]) && !eq(lower.back(), upper[i]))\n ret.push_back(upper[i]);\n// cout << upper[i] << endl;\n }\n\n int start = 0;\n\n// cout << ret.size() << endl;\n rep(i, 0, ret.size()) {\n if (ret[i].imag() < ret[start].imag() || (eq(ret[i].imag(), ret[start].imag()) && ret[i].real() < ret[start].real())) {\n start = i;\n }\n }\n\n cout << ret.size() << endl;\n rep(i, 0, ret.size()) {\n auto p = ret[(i + start) % ret.size()];\n cout << (int)p.real() << \" \" << (int)p.imag() << endl;\n }\n\n return ret;\n}\n\nReal closest_pair(vector<Point> &pts, int l, int r) {\n int m = (l + r) / 2;\n if (r - l <= 1) {\n return INF;\n } else if (r - l == 2) {\n if (imag(pts[l]) > imag(pts[l + 1])) swap(pts[l], pts[l + 1]);\n return fabs(pts[l] - pts[l + 1]);\n }\n\n Real mid = pts[m].real();\n Real d = min(closest_pair(pts, l, m), closest_pair(pts, m, r));\n\n inplace_merge(pts.begin() + l, pts.begin() + m, pts.begin() + r, [](const Point &a, const Point& b) {\n return a.imag() < b.imag();\n });\n\n vector<Point> ret;\n\n for (int i = l; i < r; i++) {\n if (fabs(pts[i].real() - mid) >= d) continue;\n\n for (int j = ret.size() - 1; j >= 0; j--) {\n if (fabs(imag(ret[j]) - imag(pts[i])) >= d) break;\n chmin(d, fabs(ret[j] - pts[i]));\n }\n\n ret.emplace_back(pts[i]);\n }\n\n return d;\n}\n\nint main() {\n cout << fixed << setprecision(5);\n\n int n;\n Real w;\n while (cin >> n >> w && n != 0) {\n vector<Point> needle(n);\n vector<Real> h(n);\n rep(i, 0, n) {\n cin >> needle[i] >> h[i];\n }\n\n auto check = [&](Real r) {\n// cout << r << endl;\n Polygon rect(4);\n if (r <= w) {\n rect = Polygon({\n Point(r, r), Point(100 - r, r), Point(100 - r, 100 - r), Point(r, 100 - r)\n });\n } else {\n Real d = sqrt(r * r - (r - w) * (r - w));\n rect = Polygon({\n Point(d, d), Point(100 - d, d), Point(100 - d, 100 - d), Point(d, 100 - d)\n });\n }\n if (rect[0].real() > rect[1].real() + EPS) return false;\n// cout << \"rect: \" << rect[0] << \" - \" << rect[2] << endl;\n\n vector<Circle> cirs(n);\n rep(i, 0, n) {\n Real d;\n if (r <= h[i]) d = r;\n else d = sqrt(r * r - (r - h[i]) * (r - h[i]));\n\n cirs[i] = Circle(needle[i], d);\n// cout << \"circle: \" << needle[i] << \" \" << d << endl;\n bool out = true;\n rep(j, 0, 4) {\n if (fabs(cirs[i].c - rect[j]) > cirs[i].r - EPS) out = false;\n }\n if (out) return false;\n }\n\n vector<Point> cands({rect[0], rect[1], rect[2], rect[3]});\n rep(i, 0, n) {\n rep(j, i + 1, n) {\n if (!intersect(cirs[i], cirs[j])) continue;\n\n auto [p1, p2] = crosspoint(cirs[i], cirs[j]);\n cands.push_back(p1);\n if (!eq(p1, p2)) cands.push_back(p2);\n }\n rep(j, 0, 4) {\n Segment sg(rect[j], rect[(j + 1) % 4]);\n if (!intersect(cirs[i], sg)) continue;\n\n auto [p1, p2] = crosspoint(cirs[i], sg);\n cands.push_back(p1);\n if (!eq(p1, p2)) cands.push_back(p2);\n }\n }\n\n for (auto &p : cands) {\n if (!(rect[0].real() - EPS < p.real() && p.real() < real(rect[2]) + EPS&&\n imag(rect[0]) - EPS < imag(p) && imag(p) < imag(rect[2]) + EPS)) {\n continue;\n }\n\n bool ok = true;\n rep(i, 0, n) {\n if (fabs(p - cirs[i].c) < cirs[i].r - EPS) {\n ok = false;\n break;\n }\n }\n if (ok) return true;\n }\n\n return false;\n };\n\n Real l = 1, r = (50 * 50 + w * w) / 2 / w, m;\n rep(_, 0, 35) {\n m = (l + r) / 2;\n if (check(m)) l = m;\n else r = m;\n }\n cout << l << endl;\n }\n}", "accuracy": 1, "time_ms": 650, "memory_kb": 3840, "score_of_the_acc": -0.7546, "final_rank": 10 }, { "submission_id": "aoj_1342_6012521", "code_snippet": "#include<bits/stdc++.h> \nusing namespace std;\nusing ll=long long int;\n//using Int=__int128;\n#define ALL(A) A.begin(),A.end()\ntemplate<typename T1,typename T2> bool chmin(T1 &a,T2 b){if(a<=b)return 0; a=b; return 1;}\ntemplate<typename T1,typename T2> bool chmax(T1 &a,T2 b){if(a>=b)return 0; a=b; return 1;}\ntemplate<typename T> constexpr int bitUP(T x,int a){return (x>>a)&1;}\n//→ ↓ ← ↑ \nint dh[4]={0,1,0,-1}, dw[4]={1,0,-1,0};\n//右上から時計回り\n//int dh[8]={-1,0,1,1,1,0,-1,-1}, dw[8]={1,1,1,0,-1,-1,-1,0};\n//long double EPS = 1e-6;\n//long double PI = acos(-1);\n//const ll INF=(1LL<<62);\nconst int MAX=(1<<30);\nconstexpr ll MOD=1000000000+7;\n//constexpr ll MOD=998244353;\n\n\ninline void bin101(){\n ios::sync_with_stdio(false);\n cin.tie(0);\n cout << fixed << setprecision(20);\n}\n\nusing Graph=vector<vector<int>>;\n\ntemplate<typename T >\nstruct edge {\n\tint to;\n\tT cost;\n\tedge()=default;\n\tedge(int to, T cost) : to(to), cost(cost) {}\n\n};\ntemplate<typename T>\nusing WeightGraph=vector<vector<edge<T>>>;\n\n//要素数n 初期値x\ntemplate<typename T>\ninline vector<T> vmake(size_t n,T x){\n\treturn vector<T>(n,x);\n}\n\n//a,b,c,x data[a][b][c] 初期値x\ntemplate<typename... Args>\nauto vmake(size_t n,Args... args){\n\tauto v=vmake(args...);\n\treturn vector<decltype(v)>(n,move(v));\n}\n////////////////////////////\n// マクロや型\n////////////////////////////\n\nusing DD=long double;\n\nconst DD EPS=1e-9;\nconst DD INF=1e50;\nconst DD PI=acosl(-1.0);\n#define EQ(a,b) (abs( (a) - (b) )<EPS) //a==b\n#define LS(a,b) ( (a)+EPS<(b) ) //a<b\n#define GR(a,b) ( (a)>(b)+EPS ) //a>b\n#define LE(a,b) ( (a)<(b)+EPS ) //a<=b\n#define GE(a,b) ( (a)>(b)-EPS ) //a>=b\n\n#define X real()\n#define Y imag()\n\n\n//点\nusing Point=complex<DD>;\nistream &operator>>(istream &is, Point &p) {\n DD x,y;\n is>>x>>y;\n p=Point(x,y);\n return is;\n}\n\n//点a→点bの線分\n//aとbが等しい時、色々バグるから注意!\nstruct Segment{\n Point a,b;\n Segment()=default;\n Segment(Point a,Point b) :a(a),b(b){}\n Segment(DD ax,DD ay,DD bx,DD by):a(ax,ay),b(bx,by){}\n Segment(DD r,Point a) :a(a),b(a+polar((DD)1.0,r)){} \n};\nusing Line=Segment;\n\n//円\nstruct Circle{\n Point p;\n DD r;\n Circle()=default;\n Circle(Point p,DD r):p(p),r(r){}\n};\n\nusing Polygon=vector<Point>;\n\n////////////////////////////\n// 基本計算\n////////////////////////////\n\n//度→ラジアン\ninline DD torad(const DD deg){return deg*PI/180;}\n//ラジアン→度\ninline DD todeg(const DD rad){return rad*180/PI;}\n//内積 |a||b|cosθ\ninline DD dot(const Point &a,const Point &b){return (conj(a)*b).X;}\n//外積 |a||b|sinθ\ninline DD cross(const Point &a,const Point &b){return (conj(a)*b).Y;}\n//ベクトルpを反時計回りにtheta(ラジアン)回転\nPoint rotate(const Point &p,const DD theta){return p*Point(cos(theta),sin(theta));}\n\n//余弦定理 cos(角度abc(ラジアン))を返す\n//長さの対応に注意 ab:辺abの長さ\n//verify:https://codeforces.com/gym/102056/problem/F\nDD cosine_formula(const DD ab,const DD bc,const DD ca){\n return (ab*ab+bc*bc-ca*ca)/(2*ab*bc);\n}\n\ninline bool xy_sort(const Point &a,const Point &b){\n if(a.X+EPS<b.X) return true;\n if(EQ(a.X,b.X) && a.Y+EPS<b.Y) return true;\n return false;\n}\ninline bool yx_sort(const Point &a,const Point &b){\n if(a.Y+EPS<b.Y) return true;\n if(EQ(a.Y,b.Y) && a.X+EPS<b.X) return true;\n return false;\n}\nnamespace std{\n inline bool operator<(const Point &a,const Point &b)noexcept{\n return xy_sort(a,b);\n }\n}\n//DDの比較関数\n//map<DD,vector<pii>,DDcom>\nstruct DDcom{\n bool operator()(const DD a, const DD b) const noexcept {\n return a+EPS<b;\n }\n};\n\n\n////////////////////////////\n// 平行や直交\n////////////////////////////\n\nbool isOrthogonal(const Point &a,const Point &b){\n return EQ(dot(a,b),0.0);\n}\n//verify:http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_2_A\nbool isOrthogonal(const Segment &s,const Segment &t){\n return EQ(dot(s.a-s.b,t.a-t.b),0.0);\n}\nbool isParallel(const Point &a,const Point &b){\n return EQ(cross(a,b),0.0);\n}\n//verify:http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_2_A\nbool isParallel(const Segment &s,const Segment &t){\n return EQ(cross(s.a-s.b,t.a-t.b),0);\n}\n//線分a→bに対して点cがどの位置にあるか\n//反時計回り:1 時計回り:-1 直線上(a,b,c:-2 a,c,b:0 c,a,b:2)\n//verify:http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_1_C\n//線分a→bの端点にcがあるなら0と判定されるはず\nint ccw(const Point &a,Point b,Point c){\n if(EQ(abs(b-c),0) or EQ(abs(a-c),0)) return 0;\n b-=a;c-=a;\n if(cross(b,c)>EPS) return 1;\n if(cross(b,c)<-EPS) return -1;\n if(dot(b,c)<-EPS) return 2;\n if(LS(norm(b),norm(c))) return -2;\n return 0;\n}\n\n////////////////////////////\n// 射影と反射\n////////////////////////////\n\n//射影\n//直線lに点pから垂線を引いたときの交点\n//verify:http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_1_A\nPoint project(const Point &p,const Line &l){\n Point base=l.b-l.a;\n DD r=dot(p-l.a,base)/norm(base);\n return l.a+base*r;\n}\n//反射\n//直線lに対して点pと対称な点\n//verify:http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_1_B\nPoint reflect(const Point &p,const Line &l){\n return p+(project(p,l)-p)*(DD)2.0;\n}\n//反射\n//直線lに対して線分sと対称な線分(直線でも使える)\n//verify:https://onlinejudge.u-aizu.ac.jp/solutions/problem/1171/review/6009447/bin101/C++17\nSegment reflect(const Segment &s,const Line &l){\n return Segment(reflect(s.a,l) , reflect(s.b,l));\n}\n\n////////////////////////////\n// 点との距離\n////////////////////////////\n\n//点と直線の距離\nDD DistPL(const Point &p,const Line &l){\n return abs(cross(l.b-l.a,p-l.a))/abs(l.b-l.a);\n}\n//点と線分の距離\nDD DistPS(const Point &p,const Segment &s){\n if( dot(s.b-s.a,p-s.a)<0.0 ) return abs(p-s.a);\n if( dot(s.a-s.b,p-s.b)<0.0 ) return abs(p-s.b);\n return DistPL(p,s);\n}\n\n////////////////////////////\n// 線分と直線\n////////////////////////////\n\n//線分a-b,c-dは交差するか?\n//接する場合も交差すると判定\n//接する場合は交差しないとするなら<=を<に変換\nbool intersect(const Point &a,const Point &b,const Point &c,const Point &d){\n return(ccw(a,b,c)*ccw(a,b,d)<=0 && ccw(c,d,a)*ccw(c,d,b)<=0);\n}\n//線分s,tは交差するか?\n//接する場合も交差すると判定\n//verify:http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_2_B&lang=ja\n//接する場合は交差しないとするならintersectの<=を<に変換\nbool intersect(const Segment &s,const Segment &t){\n return intersect(s.a,s.b,t.a,t.b);\n}\n// 2直線の交点\n// 線分の場合はintersectをしてから使う\n//verify:http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_2_C&lang=ja\nPoint crossPoint(const Line &s,const Line &t){\n Point base=t.b-t.a;\n DD d1=cross(base,t.a-s.a);\n DD d2=cross(base,s.b-s.a);\n if(EQ(d1,0) and EQ(d2,0)) return s.a; //同じ直線\n if(EQ(d2,0)) assert(false); //交点がない\n return s.a+d1/d2*(s.b-s.a);\n}\n//線分と線分の距離\n//verify:http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_2_D&lang=ja\nDD Dist(const Segment &s,const Segment &t){\n if(intersect(s,t)) return 0.0;\n return min(min(DistPS(t.a,s),DistPS(t.b,s)),min(DistPS(s.a,t),DistPS(s.b,t)) );\n}\nbool issame(const Line &l,const Line &m){\n if (GR(abs(cross(m.a - l.a, l.b - l.a)),0) ) return false;\n if (GR(abs(cross(m.b - l.a, l.b - l.a)),0) ) return false;\n return true;\n}\n\n////////////////////////////\n// 円\n////////////////////////////\n//直線lと円Cの交点\n//l.aにより近い方がindexが小さい\n//veirfy:http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_7_D\nvector<Point> crossPointLC(const Line &l,const Circle &c){\n vector<Point> ret;\n if(GR(DistPL(c.p,l),c.r)) return ret; //交点がないとき、空ベクトルを返す\n Point pr=project(c.p,l);\n Point e=(l.b-l.a)/(abs(l.b-l.a));\n DD base=sqrt(c.r*c.r-norm(pr-c.p));\n ret.push_back(pr-e*base);\n ret.push_back(pr+e*base);\n if(EQ(DistPL(c.p,l),c.r)) ret.pop_back(); //接するとき\n return ret;\n}\n//線分sと円cの交点\n//交点がないときは空ベクトルを返す\n//線分の端が交わる場合も交点とみなす\n//s.aにより近い方がindexが小さい\nvector<Point> crossPointSC(const Segment &s,const Circle &c){\n vector<Point> ret;\n if(DistPS(c.p,s)>c.r+EPS) return ret;\n auto koho=crossPointLC(s,c);\n for(auto p:koho){\n if(ccw(s.a,s.b,p)==0 or EQ(abs(p-s.a),0) or EQ(abs(p-s.b),0)) ret.push_back(p);\n }\n return ret;\n}\n\n//円aと円bの共通接線の数\n//離れている:4 外接:3 交わる:2 内接:1 内包:0\n//verify:http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_7_A\nint intersect(const Circle &a,const Circle &b){\n DD d=abs(a.p-b.p);\n if(GR(d,a.r+b.r)) return 4;\n if(EQ(d,a.r+b.r)) return 3;\n if(EQ(d,abs(a.r-b.r))) return 1;\n if(LS(d,abs(a.r-b.r))) return 0;\n return 2;\n}\n\n//円aと円bの交点\n//交点がないときは空ベクトルを返す\n//\n//verify:http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_7_E\nvector<Point> crossPoint(const Circle &a,const Circle &b){\n vector<Point> ret;\n if(GR(abs(a.p-b.p),a.r+b.r)) return ret; //離れている\n DD d=abs(a.p-b.p);\n //cerr<<(a.r*a.r+d*d-b.r*b.r)/(2*a.r*d)<<endl;\n DD s=acosl((a.r*a.r+d*d-b.r*b.r)/(2*a.r*d)); //0~π\n DD t=arg(b.p-a.p);\n if(EQ(a.r+b.r,d)) ret.push_back(a.p+polar(a.r,t));\n else ret.push_back(a.p+polar(a.r,t+s)),ret.push_back(a.p+polar(a.r,t-s));\n return ret;\n}\n\n//点pから円cに接線を引いたときの接点\n//verify:http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_7_F&lang=ja\nvector<Point> TanLine(const Point &p,const Circle &c){\n vector<Point> ret;\n DD d=abs(p-c.p);\n if(LS(d,c.r)) return ret; //pがcの内部にある場合\n if(EQ(d,c.r)){\n ret.push_back(p);\n return ret; //円の線上にある場合\n } \n return crossPoint(c,Circle(p,sqrt(d*d-c.r*c.r)));\n}\n\n//円a,bの共通接線\n//https://www.slideshare.net/kujira16/ss-35861169\n//Line.aは円aと接線の交点\n//(接線と円bの交点)と(接線と円aの交点)が違う場合はLine.bは(接線と円bの交点)\n//一致する場合は円aの中心からLine.aに向かうベクトルを反時計回りに90度回転したものを\n//Line.aに足したもの\n//verify:http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_7_G\n//verify:http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=2201\nvector<Line> TanLine(const Circle &a,const Circle &b){\n DD d=abs(a.p-b.p);\n vector<Line> ret;\n //外接線\n if(intersect(a,b)>=2){\n if(EQ(a.r,b.r)){\n Point up=polar(a.r,arg(b.p-a.p)+PI/2);\n ret.emplace_back(a.p+up,b.p+up);\n ret.emplace_back(a.p-up,b.p-up);\n }else{\n Point q=(a.p*b.r-b.p*a.r)/(b.r-a.r);\n auto as=TanLine(q,a);\n auto bs=TanLine(q,b);\n assert(as.size()==2 and bs.size()==2);\n for(int i=0;i<2;i++){\n ret.emplace_back(as[i],bs[i]);\n }\n }\n }else if(intersect(a,b)==1){ //内接する場合\n Point dir;\n if(a.r>b.r) dir=polar(a.r,arg(b.p-a.p));\n else dir=polar(a.r,arg(a.p-b.p));\n ret.emplace_back(a.p+dir,a.p+dir+rotate(dir,PI/2));\n }\n //内接線\n if(GR(abs(a.p-b.p),a.r+b.r)){ //円が離れている場合\n Point q=a.p+(b.p-a.p)*a.r/(a.r+b.r);\n auto as=TanLine(q,a);\n auto bs=TanLine(q,b);\n assert(as.size()==2 and bs.size()==2);\n for(int i=0;i<2;i++){\n ret.emplace_back(as[i],bs[i]);\n }\n }else if(EQ(d,a.r+b.r)){ //円が接している場合\n Point dir=polar(a.r,arg(b.p-a.p));\n ret.emplace_back(a.p+dir,a.p+dir+rotate(dir,PI/2));\n }\n return ret;\n}\n//2つの円の共通部分の面積\n//参考: https://tjkendev.github.io/procon-library/python/geometry/circles_intersection_area.html\n//verify: https://judge.u-aizu.ac.jp/onlinejudge/review.jsp?rid=6005736#1\nDD Area(const Circle &c1,const Circle &c2){\n DD cd=abs(c1.p-c2.p);\n if(LE(c1.r+c2.r,cd)) return 0; //円が離れている\n\n if(LE(cd,abs(c1.r-c2.r))) //片方の円が他方を包んでいる\n return min(c1.r,c2.r)*min(c1.r,c2.r)*PI;\n\n DD theta1=acosl(cosine_formula(c1.r,cd,c2.r));\n DD theta2=acosl(cosine_formula(c2.r,cd,c1.r));\n\n return c1.r*c1.r*theta1+c2.r*c2.r*theta2-c1.r*cd*sin(theta1);\n}\n\n\n////////////////////////////\n// 三角形\n////////////////////////////\n\n//ヘロンの公式 三角形の面積を返す\n//三角形が成立するか(平行ではないか)を忘れずに\nDD Heron(const DD ad,const DD bd,const DD cd){\n DD s=(ad+bd+cd)/2;\n return sqrt(s*(s-ad)*(s-bd)*(s-cd));\n}\n//三角形の外接円\n//isParallel()を使って三角形が成立するか(平行ではないか)を忘れずに\n//verify:http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_7_C\nCircle CircumscribedCircle(const Point &a,const Point &b,const Point &c){\n Point bc=(b+c)/(DD)2.0;\n Line aa(bc,bc+rotate(c-b,PI/2));\n Point ab=(a+b)/(DD)2.0;\n Line cc(ab,ab+rotate(b-a,PI/2));\n Point p=crossPoint(aa,cc);\n return Circle(p,abs(a-p));\n}\n//三角形の内接円\n//isParallel()を使って三角形が成立するか(平行ではないか)を忘れずに\n//verify:http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_7_B\nCircle InCircle(const Point &a,const Point &b,const Point &c){\n Line aa(a,a+polar((DD)1.0,(arg(b-a)+arg(c-a))/(DD)2.0));\n Line bb(b,b+polar((DD)1.0,(arg(a-b)+arg(c-b))/(DD)2.0));\n Point p=crossPoint(aa,bb);\n return Circle(p,DistPL(p,Line(a,b)));\n}\n\n////////////////////////////\n// 多角形\n////////////////////////////\n\n//点pが多角形gに対してどの位置にあるか\n//中:2 辺上:1 外:0\n//凸多角形である必要ない\n//verify:http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_3_C\nint contains(const Point &p,const vector<Point> &g){\n int n=(int)g.size();\n int x=false;\n for(int i=0;i<n;i++){\n Point a=g[i]-p,b=g[(i+1)%n]-p;\n if(EQ(cross(a,b),0) && dot(a,b)<EPS) return 1;\n if(a.Y>b.Y) swap(a,b);\n if(a.Y<EPS && EPS<b.Y && cross(a,b)>EPS) x=!x;\n }\n return (x?2:0);\n}\n//凸性判定\n//多角形gは凸か?\n//gは反時計回りでも時計回りでもいい\n//verify:http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_3_B\nbool isConvex(const vector<Point> &g){\n int n=(int)g.size();\n if(n<=3) return true;\n int flag=0;\n int t;\n for(int i=0;i<n;i++){\n Point a(g[(i+1)%n]-g[i]),b(g[(i+2)%n]-g[i]);\n if(cross(a,b)>EPS) t=1;\n else if(cross(a,b)<-EPS) t=-1;\n else continue;\n if(flag==-t) return false;\n flag=t;\n }\n return true;\n}\n\n//凸包 アンドリューのアルゴリズム\n//O(NlogN)\n//反時計回りの多角形を返す\n//verify:http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_4_A\nvector<Point> ConvexHull(vector<Point> s,const bool on_edge){\n int j;\n if(on_edge) j=-1;\n else j=1;\n int sz=(int)s.size();\n if(sz<3) return s;\n sort(s.begin(),s.end(),yx_sort);\n\n int n=0;\n vector<Point> res(2*sz);\n for(int i=0;i<sz;i++){\n while(n>=2 && cross(res[n-1]-res[n-2],s[i]-res[n-2])<EPS*j){\n n--;\n }\n res[n]=s[i];\n n++;\n }\n int t=n+1;\n for(int i=sz-2;i>=0;i--){\n while(n>=t && cross(res[n-1]-res[n-2],s[i]-res[n-2])<EPS*j){\n n--;\n }\n res[n]=s[i];\n n++;\n }\n res.resize(n-1);\n return res;\n}\n\n//多角形g(凸でなくてもいい)の符号付き面積\n//反時計回りの多角形なら正の値を返す 時計回りなら負\n//O(N)\n//https://imagingsolution.net/math/calc_n_point_area/\n//verify:http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_3_A\nDD Area(const vector<Point> &g){\n DD ret=0.0;\n int n=(int)g.size();\n for(int i=0;i<n;i++){\n ret+=cross(g[i],g[(i+1)%n]);\n }\n return ret/2.0;\n}\n\n//凸多角形gを直線lで切断\n//直線の左側(向きを考慮)にできる凸多角形を返す\n//多角形gが反時計回りならば、反時計回りで返す(時計回りなら時計回り)\n//直線上の頂点を含む線分の交点は含めてない\n//verify:http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_4_C\nvector<Point> ConvexCut(const vector<Point> &g,const Line &l){\n vector<Point> ret;\n int gz=(int)g.size();\n for(int i=0;i<gz;i++){\n Point now=g[i],next=g[(i+1)%gz];\n if(ccw(l.a,l.b,now)!=-1) ret.push_back(now);\n if(EQ(DistPL(now,l),0) or EQ(DistPL(next,l),0)) continue;\n if(ccw(l.a,l.b,now)*ccw(l.a,l.b,next)<0){\n ret.push_back(crossPoint(Line(now,next),l));\n }\n }\n return ret;\n}\n//部品\ninline DD calc(const Point &a,const Point &b,const DD &r,const bool triangle){\n if(triangle) return cross(a,b);\n else return r*r*arg(b/a);\n}\n//部品\nDD calcArea(const DD &r,const Point &a,const Point &b){\n if(EQ(abs(a-b),0)) return 0;\n bool ina=(abs(a)<r+EPS);\n bool inb=(abs(b)<r+EPS);\n if(ina && inb) return cross(a,b);\n auto cr=crossPointSC(Segment(a,b),Circle(Point(0,0),r));\n if(cr.empty()) return calc(a,b,r,false);\n auto s=cr[0],t=cr.back();\n return calc(s,t,r,true)+calc(a,s,r,ina)+calc(t,b,r,inb);\n}\n//円と多角形の共通部分の面積\n//多角形が反時計回りならば正の値を返す\n//凸多角形でなくても大丈夫\n//http://drken1215.hatenablog.com/entry/2020/02/02/091000\n//verify:http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_7_H&lang=ja\nDD Area(const Circle &c,const vector<Point> &g){\n DD ret=0.0;\n int gz=g.size();\n if(gz<=2) return ret;\n for(int i=0;i<gz;i++){\n Point a=g[i]-c.p,b=g[(i+1)%gz]-c.p;\n ret+=calcArea(c.r,g[i]-c.p,g[(i+1)%gz]-c.p);\n }\n return ret/2.0;\n}\n\n//点と多角形の距離\n//点が多角形の中にあるなら0\nDD Dist(const Point &a,const vector<Point> &b){\n\tif(b.size()==1) return abs(a-b[0]);\n\tif(contains(a,b)>=1) return 0.0L;\n\tDD ret=INF;\n\tfor(int i=0;i<b.size();i++){\n\t\tchmin(ret,DistPS(a,Segment(b[i],b[(i+1)%b.size()])));\n\t}\n\treturn ret;\n}\n\n//多角形と多角形の距離\n//どちらかが内側に入っていたら0\n//全ての線分の組み合わせの距離を求めて、その最小が答え\n//O(size(a)*size(b))\n//sizeが1の時は点との距離になる\n//verify:http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=2827\nDD Dist(const vector<Point> &a,const vector<Point> &b){\n\tDD ret=INF;\n\tif(a.size()==1) return Dist(a[0],b);\n\tif(b.size()==1) return Dist(b[0],a);\n\n\tif(contains(a[0],b)>=1) return 0.0L;\n\tif(contains(b[0],a)>=1) return 0.0L;\n\n\tfor(int i=0;i<a.size();i++){\n\t\tfor(int j=0;j<b.size();j++){\n\t\t\tSegment sa(a[i],a[(i+1)%a.size()]);\n\t\t\tSegment sb(b[j],b[(j+1)%b.size()]);\n\t\t\tchmin(ret,Dist(sa,sb));\n\t\t}\n\t}\n\treturn ret;\n}\n\n////////////////////////////\n// 最近(遠)点間距離\n////////////////////////////\n//部品\nDD RecClosetPair(const vector<Point>::iterator it,const int n){\n if(n<=1) return INF;\n int m=n/2;\n DD x=it[m].X;\n DD d=min(RecClosetPair(it,m),RecClosetPair(it+m,n-m));\n inplace_merge(it,it+m,it+n,yx_sort);\n vector<Point> v;\n for(int i=0;i<n;i++){\n if(abs(it[i].X-x)>=d) continue;\n for(int j=0;j<v.size();j++){\n DD dy=it[i].Y-v[(int)v.size()-1-j].Y;\n if(dy>=d) break;\n DD dx=it[i].X-v[(int)v.size()-1-j].X;\n d=min(d,sqrt(dx*dx+dy*dy));\n }\n v.push_back(it[i]);\n }\n return d;\n}\n//最近点対の距離\n//点が1つのときINFを返す\n//O(NlogN)\n//verify:http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_5_A\nDD ClosetPair(vector<Point> g){\n sort(g.begin(),g.end(),xy_sort);\n return RecClosetPair(g.begin(),g.size());\n}\n\n//最遠頂点間\n//verify:http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_4_B\nDD Diameter(vector<Point> g){\n g=ConvexHull(g,1);\n int gz=g.size();\n int m=0,M=0;\n for(int i=1;i<gz;i++){\n if(g[i].Y<g[m].Y) m=i;\n if(g[i].Y>g[M].Y) M=i;\n }\n DD ret=0;\n int sm=m,sM=M;\n while(m!=sM || M!=sm){\n ret=max(ret,norm(g[m]-g[M]));\n if(cross(g[(m+1)%gz]-g[m],g[(M+1)%gz]-g[M])<0) m=(m+1)%gz;\n else M=(M+1)%gz;\n }\n return sqrt(ret);\n}\n//動的凸包\n//辺上にある点は含めない\n//verify:https://atcoder.jp/contests/geocon2013/tasks/geocon2013_c\nstruct DynamicConvex{\n\tset<Point> up,down;\n\tDynamicConvex(){}\n \n\tbool isContain(set<Point> &pol,Point p){\n\t\tif(pol.size()==0) return false;\n\t\tif(pol.size()==1){\n\t\t\tPoint q=*begin(pol);\n\t\t\treturn EQ(q.X,p.X) and GE(q.Y,p.Y);\n\t\t}\n\t\tassert(pol.size()>=2);\n auto left=begin(pol);\n auto right=(--end(pol));\n if(LS(p.X,left->X) or GR(p.X,right->X)) return false;\n \n \n auto q1=pol.upper_bound(Point(p.X,INF));\n if(q1==end(pol)){\n return GE(right->Y,p.Y);\n }\n auto q2=q1--; //q1が範囲外になるのは最初に弾いてる\n return GE(cross(p-*q1,*q2-*q1),0.0);\n\t}\n bool isContain(Point p){\n if(not isContain(up,p)) return false;\n if(not isContain(down,Point(p.X,-p.Y))) return false;\n return true;\n }\n \n\tvoid add(set<Point> &pol,Point p){\n\t\tif(pol.size()==0){\n\t\t\tpol.insert(p);\n\t\t\treturn;\n\t\t}\n\t\tif(pol.size()==1){\n Point q=*begin(pol);\n\t\t\tif(EQ(q.X,p.X)){\n pol.clear();\n pol.insert(max(p,q));\n\t\t\t}else{\n\t\t\t\tpol.insert(p);\n\t\t\t}\n return;\n\t\t}\n assert(pol.size()>=2);\n if(isContain(pol,p)) return;\n //left\n auto q1=pol.upper_bound(Point(p.X,INF));\n if(q1!=begin(pol)){\n q1--;\n while(pol.size()>=1 and q1!=begin(pol)){\n auto q2=q1--;\n if(GR(cross(*q2-p,*q1-p),0)) break;\n pol.erase(q2);\n }\n }\n //right\n q1=pol.lower_bound(Point(p.X,-INF));\n if(q1!=end(pol)){\n while(pol.size()>1 and q1!=--end(pol)){\n auto q2=q1++;\n if(GR(cross(*q1-p,*q2-p),0.0)) break;\n pol.erase(q2);\n }\n }\n pol.insert(p);\n\t}\n \n\tvoid add(Point p){\n\t\tadd(up,p);\n\t\tadd(down,Point(p.X,-p.Y)); //upと同じように処理をしたいから\n\t}\n};\n\n//垂直二等分線\n//p-↑-q こんなかんじ\n//voronoiで使う\nLine bisector(const Point &p, const Point &q) {\n Point c=(p+q)/DD(2.0);\n Point v=rotate(q-p,PI/2);\n v/=abs(v);\n return Line(c-v,c+v);\n}\n\n//ボロノイ図\n//pol:全体 ps:点の集合 id:中心となる点の添字 pp:中心となる点\n//計算量: pol.size()*ps.size()\n//verify: https://judge.u-aizu.ac.jp/onlinejudge/review.jsp?rid=6005648#1\nvector<Point> voronoi(Polygon pol,const vector<Point> &ps,int id=-1,const Point pp=Point()){\n Point p;\n if(id!=-1) p=ps[id];\n else p=pp;\n for(size_t i=0;i<ps.size();i++){\n if(i==id) continue;\n Line l=bisector(p,ps[i]);\n pol=ConvexCut(pol,l);\n }\n return pol;\n}\nint N,W;\n\nbool OK(vector<Circle> C,Point p){\n for(auto c:C){\n if(LS(abs(p-c.p),c.r)) return false; \n }\n\n return true;\n}\n\nvoid solve(){\nwhile(true){\n cin>>N>>W;\n if(N==0) return;\n vector<DD> x(N),y(N),H(N);\n for(int i=0;i<N;i++) cin>>x[i]>>y[i]>>H[i];\n\n DD ok=0,ng=1e4;\n int T=100;\n while(T--){\n DD mid=(ok+ng)/2;\n\n vector<Point> koho;\n vector<Circle> C;\n for(int i=0;i<N;i++){\n DD r;\n if(mid>H[i]) r=mid*mid-(mid-H[i])*(mid-H[i]);\n else r=mid*mid;\n //if(LE(r,0)) continue;\n r=sqrtl(r);\n C.emplace_back(Point(x[i],y[i]),r);\n }\n DD w;\n if(mid>W) w=mid*mid-(mid-W)*(mid-W);\n else w=mid*mid;\n w=sqrtl(w);\n vector<Segment> S;\n\n S.emplace_back(Point(w,w),Point(w,100-w));\n S.emplace_back(Point(w,w),Point(100-w,w));\n S.emplace_back(Point(100-w,100-w),Point(w,100-w));\n S.emplace_back(Point(100-w,100-w),Point(100-w,w));\n for(size_t i=0;i<C.size();i++){\n for(size_t j=i+1;j<C.size();j++){\n int s=intersect(C[i],C[j]);\n if(s==0) continue;\n auto x=crossPoint(C[i],C[j]);\n for(auto y:x){\n koho.push_back(y);\n }\n }\n for(auto s:S){\n auto v=crossPointSC(s,C[i]);\n for(auto y:v){\n koho.push_back(y);\n }\n }\n }\n koho.emplace_back(w,w);\n koho.emplace_back(w,100-w);\n koho.emplace_back(100-w,w);\n koho.emplace_back(100-w,100-w);\n\n bool Ok=false;\n for(auto y:koho){\n if(GR(y.X,100-w)) continue;\n if(GR(y.Y,100-w)) continue;\n if(LS(y.X,w)) continue;\n if(LS(y.Y,w)) continue;\n if(OK(C,y)) Ok=true;\n }\n if(Ok) ok=mid;\n else ng=mid; \n }\n cout<<ok<<endl;\n}\n}\n\nint main(){\n bin101();\n int T=1;\n //cin>>T;\n while(T--) solve();\n}", "accuracy": 1, "time_ms": 1280, "memory_kb": 3900, "score_of_the_acc": -0.904, "final_rank": 14 }, { "submission_id": "aoj_1342_5792989", "code_snippet": "/*\tauthor: Kite_kuma\n\tcreated: 2021.08.17 02:01:28 */\n\n// #ifdef LOCAL\n// #define _GLIBCXX_DEBUG\n// #endif\n#include <bits/stdc++.h>\nusing namespace std;\n\n#pragma region macros\n\n#define foa(s, v) for(auto &s : v)\n#define all(v) (v).begin(), (v).end()\n#define rall(v) (v).rbegin(), (v).rend()\n#define popcnt(n) __builtin_popcountll((long long)n)\n\n#define REPname(a, b, c, d, e, ...) e\n#define rep(...) REPname(__VA_ARGS__, REP3, REP2, REP1, REP0)(__VA_ARGS__)\n#define REP0(x) for(int Counter_in_rep_macro = 0; Counter_in_rep_macro < (x); ++Counter_in_rep_macro)\n#define REP1(i, x) for(int i = 0; i < (x); ++i)\n#define REP2(i, l, r) for(int i = (l); i < (r); ++i)\n#define REP3(i, l, r, c) for(int i = (l); i < (r); i += (c))\n\n#define DREPname(a, b, c, d, e, ...) e\n#define drep(...) DREPname(__VA_ARGS__, DREP3, DREP2, DREP1)(__VA_ARGS__)\n#define DREP1(i, x) for(int i = (x)-1; i >= 0; --i)\n#define DREP2(i, l, r) for(int i = (r)-1; i >= (l); --i)\n#define DREP3(i, l, r, c) for(int i = (r)-1; i >= (l); i -= (c))\n\n#pragma endregion\n\n#pragma region aliases\n\nusing ll = long long;\nusing ld = long double;\nusing ull = unsigned long long;\nusing vi = std::vector<int>;\nusing vvi = std::vector<std::vector<int>>;\nusing vvvi = std::vector<std::vector<std::vector<int>>>;\nusing vll = std::vector<ll>;\nusing vvll = std::vector<vll>;\nusing vvvll = std::vector<vvll>;\nusing pii = std::pair<int, int>;\nusing pll = std::pair<long long, long long>;\ntemplate <class T = ll>\nusing V = std::vector<T>;\ntemplate <class T = ll>\nusing VV = V<V<T>>;\ntemplate <class T = ll>\nusing VVV = V<V<V<T>>>;\ntemplate <class T = ll>\nusing pqup = std::priority_queue<T, std::vector<T>, std::greater<T>>;\ntemplate <class T = ll>\nusing pqdn = std::priority_queue<T>;\n\n#pragma endregion\n\n#pragma region constants\n\nconst int inf = 1e9;\nconst long long INF = 1e18;\nconst long double pi = acos(-1);\nconst char dl = '\\n';\nconst char sp = ' ';\nint dx[8] = {1, 0, -1, 0, 1, -1, -1, 1};\nint dy[8] = {0, 1, 0, -1, 1, 1, -1, -1};\n\nconst int mod_1000000007 = 1000000007;\nconst int mod_998244353 = 998244353;\n\n#pragma endregion\n\n#pragma region basic_operation\n\ntemplate <class T0, class T1, class T2>\ninline bool in_range(T0 x, T1 lef, T2 rig) {\n\treturn ((lef <= x) && (x < rig));\n}\n\ntemplate <class T>\ninline bool chmin(T &a, T b) {\n\tif(a > b) {\n\t\ta = b;\n\t\treturn true;\n\t}\n\treturn false;\n}\ntemplate <class T>\ninline bool chmax(T &a, T b) {\n\tif(a < b) {\n\t\ta = b;\n\t\treturn true;\n\t}\n\treturn false;\n}\n\nvoid Yes(bool f = 1) { std::cout << (f ? \"Yes\" : \"No\") << '\\n'; }\nvoid No() { std::cout << \"No\\n\"; }\nvoid YES(bool f = 1) { std::cout << (f ? \"YES\" : \"NO\") << '\\n'; }\nvoid NO() { std::cout << \"NO\\n\"; }\n\ntemplate <class T>\nvoid drop(T answer) {\n\tstd::cout << answer << '\\n';\n\texit(0);\n}\n\nvoid err(bool flag = true) {\n\tif(!flag) return;\n\tstd::cout << -1 << '\\n';\n\texit(0);\n}\n\ntemplate <class T>\nvoid vout(std::vector<T> const &v, bool vertically = 0) {\n\tif(vertically) {\n\t\tfor(auto const &a : v) {\n\t\t\tstd::cout << a << '\\n';\n\t\t}\n\t\treturn;\n\t}\n\tfor(auto it = v.begin(); it != v.end(); it++) {\n\t\tstd::cout << (*it);\n\t\tif(it != v.end() - 1) {\n\t\t\tstd::cout << ' ';\n\t\t}\n\t}\n\tstd::cout << '\\n';\n\treturn;\n}\n\ninline void print() { std::cout << '\\n'; }\ntemplate <class T>\ninline void print(T x) {\n\tstd::cout << x << '\\n';\n\treturn;\n}\ntemplate <typename Head, typename... Tail>\nvoid print(Head H, Tail... T) {\n\tstd::cout << H << \" \";\n\tprint(T...);\n}\n\ntemplate <class T>\nvoid add(std::vector<T> &v, T val) {\n\tfor(auto &a : v) a += val;\n\treturn;\n}\n\ntemplate <class T>\nT dup(T a, T b) {\n\tassert(b != 0);\n\treturn (a + b - 1) / b;\n}\n\ntemplate <class T>\nT greatest_lower_multiple(T x, T d) {\n\tif(d == 0) return 0;\n\tif(d < 0) d *= -1;\n\tT y = x % d;\n\tif(y < 0) y += d;\n\treturn x - y;\n}\n\ntemplate <class T>\nT least_upper_multiple(T x, T d) {\n\treturn -greatest_lower_multiple(-x, d);\n}\n\nlong long POW(long long a, long long n) {\n\tlong long res = 1;\n\twhile(n > 0) {\n\t\tif(n & 1) res = res * a;\n\t\ta = a * a;\n\t\tn >>= 1;\n\t}\n\treturn res;\n}\n\nlong long modpow(long long a, long long n, long long mod) {\t // a^n mod\n\tassert(n >= 0 && mod);\n\tif(mod == 1) return 0LL;\n\tlong long res = 1;\n\ta %= mod;\n\twhile(n > 0) {\n\t\tif(n & 1) res = res * a % mod;\n\t\ta = a * a % mod;\n\t\tn >>= 1;\n\t}\n\treturn res < 0 ? res + mod : res;\n}\n\n// return x which satisfies a * x % mod == gcd(a, mod)\n// not (mod | a)\nlong long modinv(long long a, long long mod) {\n\tlong long b = mod, u = 1, v = 0;\n\twhile(b) {\n\t\tlong long t = a / b;\n\t\ta -= t * b;\n\t\tstd::swap(a, b);\n\t\tu -= t * v;\n\t\tstd::swap(u, v);\n\t}\n\tu %= mod;\n\tif(u < 0) u += mod;\n\treturn u;\n}\n\ntemplate <class T>\nint lb(const std::vector<T> &a, const T x) {\n\treturn std::distance(a.begin(), std::lower_bound(a.begin(), a.end(), x));\n}\ntemplate <class T>\nint ub(const std::vector<T> &a, const T x) {\n\treturn std::distance(a.begin(), std::upper_bound(a.begin(), a.end(), x));\n}\ntemplate <class T>\nvoid unq_sort(std::vector<T> &a) {\n\tstd::sort(a.begin(), a.end());\n\ta.erase(std::unique(a.begin(), a.end()), a.end());\n}\ntemplate <class T>\nstd::vector<int> press(std::vector<T> &a) {\n\tauto vec = a;\n\tunq_sort(vec);\n\tstd::vector<int> ret;\n\tfor(auto &v : a) ret.push_back(lb(vec, v));\n\treturn ret;\n}\n\n#pragma endregion\n\n#pragma region input\n#define VEC(type, name, size) \\\n\tvector<type> name(size); \\\n\tscanner::INPUT(name)\n#define VVEC(type, name, h, w) \\\n\tvector<vector<type>> name(h, vector<type>(w)); \\\n\tscanner::INPUT(name)\n#define INT(...) \\\n\tint __VA_ARGS__; \\\n\tscanner::INPUT(__VA_ARGS__)\n#define LL(...) \\\n\tlong long __VA_ARGS__; \\\n\tscanner::INPUT(__VA_ARGS__)\n#define STR(...) \\\n\tstring __VA_ARGS__; \\\n\tscanner::INPUT(__VA_ARGS__)\n#define CHR(...) \\\n\tchar __VA_ARGS__; \\\n\tscanner::INPUT(__VA_ARGS__)\n#define DBL(...) \\\n\tdouble __VA_ARGS__; \\\n\tscanner::INPUT(__VA_ARGS__)\n#define LD(...) \\\n\tlong double __VA_ARGS__; \\\n\tscanner::INPUT(__VA_ARGS__)\n#define TPL3(type0, type1, type2, name) \\\n\tstd::tuple<type0, type1, type2> name; \\\n\tscanner::INPUT(name);\n#define TPL4(type0, type1, type2, type3, name) \\\n\tstd::tuple<type0, type1, type2, type3> name; \\\n\tscanner::INPUT(name);\n\nnamespace scanner {\ntemplate <class T>\nvoid scan(T &a) {\n\tstd::cin >> a;\n}\n\ntemplate <class T, class L>\nvoid scan(std::pair<T, L> &p) {\n\tscan(p.first);\n\tscan(p.second);\n}\n\ntemplate <class T0, class T1, class T2>\nvoid scan(std::tuple<T0, T1, T2> &p) {\n\tT0 t0;\n\tT1 t1;\n\tT2 t2;\n\tscan(t0);\n\tscan(t1);\n\tscan(t2);\n\tp = std::make_tuple(t0, t1, t2);\n}\n\ntemplate <class T0, class T1, class T2, class T3>\nvoid scan(std::tuple<T0, T1, T2, T3> &p) {\n\tT0 t0;\n\tT1 t1;\n\tT2 t2;\n\tT3 t3;\n\tscan(t0);\n\tscan(t1);\n\tscan(t2);\n\tscan(t3);\n\tp = std::make_tuple(t0, t1, t2, t3);\n}\n\ntemplate <class T>\nvoid scan(std::vector<T> &a) {\n\tfor(auto &i : a) scan(i);\n}\n\nvoid INPUT() {}\ntemplate <class Head, class... Tail>\nvoid INPUT(Head &head, Tail &... tail) {\n\tscan(head);\n\tINPUT(tail...);\n}\n} // namespace scanner\n\ntemplate <typename T1, typename T2>\nstd::istream &operator>>(std::istream &is, std::pair<T1, T2> &p) {\n\tis >> p.first >> p.second;\n\treturn is;\n}\n#pragma endregion\n\n#pragma region debug\n\n#pragma region output\ntemplate <typename T1, typename T2>\nstd::ostream &std::operator<<(std::ostream &os, const std::pair<T1, T2> &p) {\n\tos << p.first << \" \" << p.second;\n\treturn os;\n}\ntemplate <class T>\nstd::ostream &std::operator<<(std::ostream &os, const std::vector<T> &v) {\n\tfor(int i = 0; i < (int)v.size(); i++) {\n\t\tif(i) os << \" \";\n\t\tos << v[i];\n\t}\n\treturn os;\n}\n#pragma endregion\n\n#pragma region view\n\nnamespace viewer {\nvoid view(const long long e) {\n\tif(e == INF)\n\t\tstd::cerr << \"INF\";\n\telse if(e == -INF)\n\t\tstd::cerr << \"-INF\";\n\telse\n\t\tstd::cerr << e;\n}\n\nvoid view(const int e) {\n\tif(e == inf)\n\t\tstd::cerr << \"inf\";\n\telse if(e == -inf)\n\t\tstd::cerr << \"-inf\";\n\telse\n\t\tstd::cerr << e;\n}\n\ntemplate <typename T>\nvoid view(const T e) {\n\tstd::cerr << e;\n}\n\ntemplate <typename T, typename U>\nvoid view(const std::pair<T, U> &p) {\n\tstd::cerr << \"(\";\n\tview(p.first);\n\tstd::cerr << \", \";\n\tview(p.second);\n\tstd::cerr << \")\";\n}\n\ntemplate <class T0, class T1, class T2>\nvoid view(const std::tuple<T0, T1, T2> &p) {\n\tstd::cerr << \"(\";\n\tview(std::get<0>(p));\n\tstd::cerr << \", \";\n\tview(std::get<1>(p));\n\tstd::cerr << \", \";\n\tview(std::get<2>(p));\n\tstd::cerr << \")\";\n}\n\ntemplate <class T0, class T1, class T2, class T3>\nvoid view(const std::tuple<T0, T1, T2, T3> &p) {\n\tstd::cerr << \"(\";\n\tview(std::get<0>(p));\n\tstd::cerr << \", \";\n\tview(std::get<1>(p));\n\tstd::cerr << \", \";\n\tview(std::get<2>(p));\n\tstd::cerr << \", \";\n\tview(std::get<3>(p));\n\tstd::cerr << \")\";\n}\n\ntemplate <typename T>\nvoid view(const std::set<T> &s) {\n\tif(s.empty()) {\n\t\tstd::cerr << \"{ }\";\n\t\treturn;\n\t}\n\tstd::cerr << \"{ \";\n\tfor(auto &t : s) {\n\t\tview(t);\n\t\tstd::cerr << \", \";\n\t}\n\tstd::cerr << \"\\b\\b }\";\n}\n\ntemplate <typename T>\nvoid view(const std::unordered_set<T> &s) {\n\tif(s.empty()) {\n\t\tstd::cerr << \"{ }\";\n\t\treturn;\n\t}\n\tstd::cerr << \"{ \";\n\tfor(auto &t : s) {\n\t\tview(t);\n\t\tstd::cerr << \", \";\n\t}\n\tstd::cerr << \"\\b\\b }\";\n}\n\ntemplate <typename T>\nvoid view(const std::vector<T> &v) {\n\tif(v.empty()) {\n\t\tstd::cerr << \"{ }\";\n\t\treturn;\n\t}\n\tstd::cerr << \"{ \";\n\tfor(const auto &e : v) {\n\t\tview(e);\n\t\tstd::cerr << \", \";\n\t}\n\tstd::cerr << \"\\b\\b }\";\n}\n\ntemplate <typename T>\nvoid view(const std::vector<std::vector<T>> &vv) {\n\tstd::cerr << \"{\\n\";\n\tfor(const auto &v : vv) {\n\t\tstd::cerr << \"\\t\";\n\t\tview(v);\n\t\tstd::cerr << '\\n';\n\t}\n\tstd::cerr << \"}\";\n}\n\ntemplate <typename T, typename U>\nvoid view(const std::vector<std::pair<T, U>> &v) {\n\tstd::cerr << \"{\\n\";\n\tfor(const auto &c : v) {\n\t\tstd::cerr << \"\\t(\";\n\t\tview(c.first);\n\t\tstd::cerr << \", \";\n\t\tview(c.second);\n\t\tstd::cerr << \")\\n\";\n\t}\n\tstd::cerr << \"}\";\n}\n\ntemplate <class T0, class T1, class T2>\nvoid view(const std::vector<std::tuple<T0, T1, T2>> &v) {\n\tif(v.empty()) {\n\t\tstd::cerr << \"{ }\";\n\t\treturn;\n\t}\n\tstd::cerr << '{';\n\tfor(const auto &t : v) {\n\t\tstd::cerr << \"\\n\\t\";\n\t\tview(t);\n\t\tstd::cerr << \",\";\n\t}\n\tstd::cerr << \"\\n}\";\n}\n\ntemplate <class T0, class T1, class T2, class T3>\nvoid view(const std::vector<std::tuple<T0, T1, T2, T3>> &v) {\n\tif(v.empty()) {\n\t\tstd::cerr << \"{ }\";\n\t\treturn;\n\t}\n\tstd::cerr << '{';\n\tfor(const auto &t : v) {\n\t\tstd::cerr << \"\\n\\t\";\n\t\tview(t);\n\t\tstd::cerr << \",\";\n\t}\n\tstd::cerr << \"\\n}\";\n}\n\ntemplate <typename T, typename U>\nvoid view(const std::map<T, U> &m) {\n\tstd::cerr << \"{\\n\";\n\tfor(const auto &t : m) {\n\t\tstd::cerr << \"\\t[\";\n\t\tview(t.first);\n\t\tstd::cerr << \"] : \";\n\t\tview(t.second);\n\t\tstd::cerr << '\\n';\n\t}\n\tstd::cerr << \"}\";\n}\n\ntemplate <typename T, typename U>\nvoid view(const std::unordered_map<T, U> &m) {\n\tstd::cerr << \"{\\n\";\n\tfor(const auto &t : m) {\n\t\tstd::cerr << \"\\t[\";\n\t\tview(t.first);\n\t\tstd::cerr << \"] : \";\n\t\tview(t.second);\n\t\tstd::cerr << '\\n';\n\t}\n\tstd::cerr << \"}\";\n}\n} // namespace viewer\n\n#pragma endregion\n\n// when debugging : g++ foo.cpp -DLOCAL\n#ifdef LOCAL\nvoid debug_out() {}\ntemplate <typename Head, typename... Tail>\nvoid debug_out(Head H, Tail... T) {\n\tviewer::view(H);\n\tstd::cerr << \", \";\n\tdebug_out(T...);\n}\n#define debug(...) \\\n\tdo { \\\n\t\tstd::cerr << __LINE__ << \" [\" << #__VA_ARGS__ << \"] : [\"; \\\n\t\tdebug_out(__VA_ARGS__); \\\n\t\tstd::cerr << \"\\b\\b]\\n\"; \\\n\t} while(0)\n#define dump(x) \\\n\tdo { \\\n\t\tstd::cerr << __LINE__ << \" \" << #x << \" : \"; \\\n\t\tviewer::view(x); \\\n\t\tstd::cerr << '\\n'; \\\n\t} while(0)\n\n#else\n#define debug(...) (void(0))\n#define dump(x) (void(0))\n#endif\n\n#pragma endregion\n\n#pragma region geometry\n\nusing Real = long double;\nusing Point = complex<Real>;\nusing Vec = Point;\nconst Real EPS = 1e-10, PI = acos(-1);\n\ninline bool eq(const Real &a, const Real &b) { return fabs(b - a) < EPS; }\ninline bool eq(const Point &a, const Point &b) { return fabs(b - a) < EPS; }\n/*\n\t-1: a < b\n\t0 : a == b\n\t1 : a > b\n*/\ninline int compare(const Real &a, const Real &b) { return eq(a, b) ? 0 : a < b ? -1 : 1; }\n\nconst Point operator*(const Point &p, const Real &d) { return Point(real(p) * d, imag(p) * d); }\n\nistream &operator>>(istream &is, Point &p) {\n\tReal a, b;\n\tis >> a >> b;\n\tp = Point(a, b);\n\treturn is;\n}\nostream &operator<<(ostream &os, Point &p) { return os << fixed << setprecision(10) << p.real() << \" \" << p.imag(); }\n\n// rotate point 'p' for counter clockwise direction\nconst Point rotate(const Real &theta, const Point &p) {\n\treturn Point(cos(theta) * p.real() - sin(theta) * p.imag(), sin(theta) * p.real() + cos(theta) * p.imag());\n}\n\nconst Real radian_to_degree(const Real &r) { return (r * 180.0 / PI); }\n\nconst Real degree_to_radian(const Real &d) { return (d * PI / 180.0); }\n\n// get angle a-b-c (<pi)\nconst Real get_angle(const Point &a, const Point &b, const Point &c) {\n\tconst Point v(a - b), w(c - b);\n\tReal theta = fabs(atan2(w.imag(), w.real()) - atan2(v.imag(), v.real()));\n\treturn min(theta, 2 * PI - theta);\n}\n\nnamespace std {\nbool operator<(const Point &a, const Point &b) { return a.real() != b.real() ? a.real() < b.real() : a.imag() < b.imag(); }\n} // namespace std\n\nstruct Line {\n\tPoint a, b;\n\n\tLine() = default;\n\n\tLine(Point a, Point b) : a(a), b(b) {}\n\n\tLine(Real A, Real B, Real C) // Ax + By = C\n\t{\n\t\tif(eq(A, 0))\n\t\t\ta = Point(0, C / B), b = Point(1, C / B);\n\t\telse if(eq(B, 0))\n\t\t\tb = Point(C / A, 0), b = Point(C / A, 1);\n\t\telse\n\t\t\ta = Point(0, C / B), b = Point(C / A, 0);\n\t}\n\n\tfriend ostream &operator<<(ostream &os, Line &p) { return os << p.a << \" -- \" << p.b; }\n\n\tfriend istream &operator>>(istream &is, Line &a) { return is >> a.a >> a.b; }\n};\n\nstruct Segment : Line {\n\tSegment() = default;\n\n\tSegment(Point a, Point b) : Line(a, b) {}\n};\n\nstruct Circle {\n\tPoint p;\n\tReal r;\n\n\tCircle() = default;\n\n\tCircle(Point p, Real r) : p(p), r(r) {}\n};\n\nusing Points = vector<Point>;\nusing Polygon = vector<Point>;\nusing Segments = vector<Segment>;\nusing Lines = vector<Line>;\nusing Circles = vector<Circle>;\n\nconst Real cross(const Point &a, const Point &b) { return real(a) * imag(b) - imag(a) * real(b); }\nconst Real dot(const Point &a, const Point &b) { return real(a) * real(b) + imag(a) * imag(b); }\n\n// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_1_C\nconst int ccw(const Point &a, Point b, Point c) {\n\tb = b - a, c = c - a;\n\tif(cross(b, c) > EPS) return +1;\t\t// \"COUNTER_CLOCKWISE\"\n\tif(cross(b, c) < -EPS) return -1;\t\t// \"CLOCKWISE\"\n\tif(dot(b, c) < -EPS) return +2;\t\t\t// \"ONLINE_BACK\"\n\tif(norm(b) + EPS < norm(c)) return -2;\t// \"ONLINE_FRONT\"\n\treturn 0;\t\t\t\t\t\t\t\t// \"ON_SEGMENT\"\n}\n\n// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_2_A\nbool parallel(const Line &a, const Line &b) { return eq(cross(a.b - a.a, b.b - b.a), 0.0); }\n\n// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_2_A\nbool orthogonal(const Line &a, const Line &b) { return eq(dot(a.a - a.b, b.a - b.b), 0.0); }\n\n// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_1_A\nPoint projection(const Line &l, const Point &p) {\n\tdouble t = dot(p - l.a, l.a - l.b) / norm(l.a - l.b);\n\treturn l.a + (l.a - l.b) * t;\n}\n\nPoint projection(const Segment &l, const Point &p) {\n\tdouble t = dot(p - l.a, l.a - l.b) / norm(l.a - l.b);\n\treturn l.a + (l.a - l.b) * t;\n}\n\n// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_1_B\nPoint reflection(const Line &l, const Point &p) { return projection(l, p) * 2.0 - p; }\n\nint intersect(const Line &l, const Point &p) { return int(abs(ccw(l.a, l.b, p)) != 1); }\n\nint intersect(const Line &l, const Line &m) {\n\tif(intersect(l, m.a) && intersect(l, m.b)) return -1;\n\treturn int(abs(cross(l.b - l.a, m.b - m.a)) > EPS);\n}\n\nint intersect(const Segment &s, const Point &p) { return int(ccw(s.a, s.b, p) == 0); }\n\nint intersect(const Line &l, const Segment &s) {\n\tif(intersect(l, s.a) && intersect(l, s.b)) return -1;\n\treturn cross(l.b - l.a, s.a - l.a) * cross(l.b - l.a, s.b - l.a) < EPS;\n}\n\nReal distance(const Line &l, const Point &p);\n\nint intersect(const Circle &c, const Line &l) {\n\tReal d = c.r - distance(l, c.p);\n\treturn fabs(d) < EPS ? 1 : d > 0. ? 2 : 0;\n}\n\nint intersect(const Circle &c, const Point &p) { return int(abs(abs(p - c.p) - c.r) < EPS); }\n\n// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_2_B\nint intersect(const Segment &s, const Segment &t) {\n\tif(eq(s.a, s.b)) return intersect(t, s.a);\n\tif(intersect(Line(s), t.a) && intersect(Line(s), t.b) &&\n\t std::max(std::min(s.a, s.b), std::min(t.a, t.b)) < std::min(std::max(s.a, s.b), std::max(t.a, t.b)))\n\t\treturn -1;\n\treturn int(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);\n}\n\nint intersect(const Circle &c, const Segment &l) {\n\tconst Point h = projection(l, c.p);\n\tif(norm(h - c.p) - c.r * c.r > EPS) return 0;\n\tauto d1 = abs(c.p - l.a), d2 = abs(c.p - l.b);\n\tif(compare(d1, c.r) == -1 && compare(d2, c.r) == -1) return 0;\n\tif(d1 < c.r - EPS && d2 > c.r - EPS || d1 > c.r - EPS && d2 < c.r - EPS) return 1;\n\treturn dot(l.a - h, l.b - h) < 0 ? 2 : 0;\n}\n\nReal distance(const Point &a, const Point &b);\n\nint number_of_common_tangents(const Circle &c1, const Circle &c2) {\n\tReal r1 = min(c1.r, c2.r), r2 = max(c1.r, c2.r), d = distance(c1.p, c2.p);\n\tint com = compare(r1 + r2, d);\n\treturn com == 1 ? compare(d + r1, r2) + 1 : 3 - com;\n\t// if(c1.r < c2.r) swap(c1, c2);\n\t// Real d = abs(c1.p - c2.p);\n\t// if(compare(c1.r + c2.r, d) == -1) return 4;\n\t// if(eq(c1.r + c2.r, d)) return 3;\n\t// if(compare(c1.r - c2.r, d) == -1) return 2;\n\t// return int(eq(c1.r - c2.r, d));\n}\n\n// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_7_A&lang=jp\nint intersect(const Circle &c1, const Circle &c2) { return 2 - abs(2 - number_of_common_tangents(c1, c2)); }\n\nReal distance(const Point &a, const Point &b) { return abs(a - b); }\n\nReal distance(const Line &l, const Point &p) { return abs(p - projection(l, p)); }\n\nReal distance(const Line &l, const Line &m) { return intersect(l, m) ? 0 : distance(l, m.a); }\n\nReal distance(const Segment &s, const Point &p) {\n\tPoint r = projection(s, p);\n\treturn intersect(s, r) ? distance(r, p) : min(abs(s.a - p), abs(s.b - p));\n}\n\n// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_2_D\nReal distance(const Segment &a, const Segment &b) {\n\tif(intersect(a, b)) return 0;\n\treturn min({distance(a, b.a), distance(a, b.b), distance(b, a.a), distance(b, a.b)});\n}\n\nReal distance(const Line &l, const Segment &s) {\n\tif(intersect(l, s)) return 0;\n\treturn min(distance(l, s.a), distance(l, s.b));\n}\n\nPoint crosspoint(const Line &l, const Line &m) {\n\tReal A = cross(l.b - l.a, m.b - m.a);\n\tReal B = cross(l.b - l.a, l.b - m.a);\n\tif(eq(abs(A), 0.0) && eq(abs(B), 0.0)) return m.a;\n\treturn m.a + (m.b - m.a) * B / A;\n}\n\n// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_2_C\nPoint crosspoint(const Segment &l, const Segment &m) { return crosspoint(Line(l), Line(m)); }\n\n// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_7_D\npair<Point, Point> crosspoint(const Circle &c, const Line l) {\n\tPoint pr = projection(l, c.p);\n\tif(eq(distance(l, c.p), c.r)) return {pr, pr};\n\tVec v = (l.b - l.a) / abs(l.b - l.a) * sqrt(c.r * c.r - norm(pr - c.p));\n\treturn make_pair(pr - v, pr + v);\n\t// Vec e = (l.b - l.a) / abs(l.b - l.a);\n\t// double base = sqrt(c.r * c.r - norm(pr - c.p));\n\t// return {pr - e * base, pr + e * base};\n}\n\npair<Point, Point> crosspoint(const Circle &c, const Segment &l) {\n\tif(intersect(c, l) == 2) return crosspoint(c, Line(l.a, l.b));\n\tauto ret = crosspoint(c, Line(l.a, l.b));\n\tif(dot(l.a - ret.first, l.b - ret.first) < EPS)\n\t\tret.second = ret.first;\n\telse\n\t\tret.first = ret.second;\n\treturn ret;\n}\n\n// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_7_E\npair<Point, Point> crosspoint(const Circle &c1, const Circle &c2) {\n\tReal d = abs(c1.p - c2.p);\n\tReal a = acos((c1.r * c1.r + d * d - c2.r * c2.r) / (2 * c1.r * d)); // cosine theorem\n\tReal t = atan2(c2.p.imag() - c1.p.imag(), c2.p.real() - c1.p.real());\n\tPoint p1 = c1.p + Point(cos(t + a) * c1.r, sin(t + a) * c1.r);\n\tPoint p2 = c1.p + Point(cos(t - a) * c1.r, sin(t - a) * c1.r);\n\treturn make_pair(p1, p2);\n}\n\n// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_7_F\n// \"点 p を通る円 c の接線\"の接点2つ\npair<Point, Point> tangent(const Circle &c1, const Point &p2) { return crosspoint(c1, Circle(p2, sqrt(norm(c1.p - p2) - c1.r * c1.r))); }\n\n// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_7_G\n// 円 c1, c2 の共通接線\nLines tangent(Circle c1, Circle c2) {\n\tLines ret;\n\tif(c1.r < c2.r) swap(c1, c2);\n\tReal g = norm(c1.p - c2.p);\n\tif(eq(g, 0)) return ret;\n\tVec u = (c2.p - c1.p) / sqrt(g);\n\tVec v = rotate(PI * 0.5, u);\n\tfor(int s : {-1, 1}) {\n\t\tReal h = (c1.r + s * c2.r) / sqrt(g);\n\t\tif(eq(1 - h * h, 0)) {\n\t\t\tret.emplace_back(c1.p + u * c1.r, c1.p + (u + v) * c1.r);\n\t\t} else if(1 - h * h > 0) {\n\t\t\tPoint uu = u * h, vv = v * sqrt(1 - h * h);\n\t\t\tret.emplace_back(c1.p + (uu + vv) * c1.r, c2.p - (uu + vv) * c2.r * s);\n\t\t\tret.emplace_back(c1.p + (uu - vv) * c1.r, c2.p - (uu - vv) * c2.r * s);\n\t\t}\n\t}\n\treturn ret;\n}\n// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_7_H\n// 円と多角形の共通部分の面積\nReal area(const Polygon &p, const Circle &c) {\n\tif(p.size() < 3) return 0.0;\n\tfunction<Real(Circle, Point, Point)> cross_area = [&](const Circle &c, const Point &a, const Point &b) {\n\t\tPoint va = c.p - a, vb = c.p - b;\n\t\tReal f = cross(va, vb), ret = 0.0;\n\t\tif(eq(f, 0.0)) return ret;\n\t\tif(max(abs(va), abs(vb)) < c.r + EPS) return f;\n\t\tif(distance(Segment(a, b), c.p) > c.r - EPS) return c.r * c.r * arg(vb * conj(va));\n\t\tauto u = crosspoint(c, Segment(a, b));\n\t\tvector<Point> tot{a, u.first, u.second, b};\n\t\tfor(int i = 0; i + 1 < tot.size(); i++) {\n\t\t\tret += cross_area(c, tot[i], tot[i + 1]);\n\t\t}\n\t\treturn ret;\n\t};\n\tReal A = 0;\n\tfor(int i = 0; i < p.size(); i++) {\n\t\tA += cross_area(c, p[i], p[(i + 1) % p.size()]);\n\t}\n\treturn A * 0.5;\n}\n\n// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_4_B\n// 凸多角形の直径(最遠頂点対間距離)\nReal convex_diameter(const Polygon &p) {\n\tint N = (int)p.size();\n\tint is = 0, js = 0;\n\tfor(int i = 1; i < N; i++) {\n\t\tif(p[i].imag() > p[is].imag()) is = i;\n\t\tif(p[i].imag() < p[js].imag()) js = i;\n\t}\n\tReal maxdis = norm(p[is] - p[js]);\n\n\tint maxi, maxj, i, j;\n\ti = maxi = is;\n\tj = maxj = js;\n\tdo {\n\t\tif(cross(p[(i + 1) % N] - p[i], p[(j + 1) % N] - p[j]) >= 0) {\n\t\t\tj = (j + 1) % N;\n\t\t} else {\n\t\t\ti = (i + 1) % N;\n\t\t}\n\t\tif(norm(p[i] - p[j]) > maxdis) {\n\t\t\tmaxdis = norm(p[i] - p[j]);\n\t\t\tmaxi = i;\n\t\t\tmaxj = j;\n\t\t}\n\t} while(i != is || j != js);\n\treturn sqrt(maxdis);\n}\n\n// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_5_A\n// 最近点対\nReal closest_pair(Points ps) {\n\tif(ps.size() <= 1) throw(0);\n\tsort(begin(ps), end(ps)); // xでソート\n\n\tauto compare_y = [&](const Point &a, const Point &b) { return imag(a) < imag(b); };\n\tvector<Point> beet(ps.size());\n\tconst Real INF = 1e18;\n\n\tfunction<Real(int, int)> rec = [&](int left, int right) {\n\t\tif(right - left <= 1) return INF;\n\t\tint mid = (left + right) >> 1;\n\t\tauto x = real(ps[mid]);\t // 領域内でx座標真中の点\n\t\tauto ret = min(rec(left, mid), rec(mid, right));\n\t\t// y座標でソートされた2つの区間をマージ\n\t\tinplace_merge(begin(ps) + left, begin(ps) + mid, begin(ps) + right, compare_y);\n\t\tint ptr = 0;\n\t\tfor(int i = left; i < right; i++) {\n\t\t\tif(abs(real(ps[i]) - x) >= ret) continue;\n\t\t\tfor(int j = 0; j < ptr; j++) {\n\t\t\t\tauto luz = ps[i] - beet[ptr - j - 1];\n\t\t\t\tif(imag(luz) >= ret) break;\n\t\t\t\tret = min(ret, abs(luz));\n\t\t\t}\n\t\t\tbeet[ptr++] = ps[i];\n\t\t}\n\t\treturn ret;\n\t};\n\treturn rec(0, (int)ps.size());\n}\n\n// (aからの距離):(bからの距離) = dist_a : dist_b となる点の集合(円)を返す\nCircle Apollonius_Circle(Point a, Point b, Real dist_a, Real dist_b) {\n\tassert(!eq(dist_a, dist_b)); //同じ場合はbisectionで\n\tReal asq = dist_a * dist_a, bsq = dist_b * dist_b;\n\tPoint p = (a * bsq - b * asq) * (1 / (bsq - asq));\n\tReal r = abs(a - b) * dist_a * dist_b / abs(asq - bsq);\n\treturn Circle(p, r);\n}\n\n// bisection of angle A-B-C\n// https://onlinejudge.u-aizu.ac.jp/status/users/Kite_kuma/submissions/1/CGL_7_B/judge/5763316/C++17\nLine bisection_of_angle(const Point &a, const Point &b, const Point &c) {\n\tReal da = distance(a, b);\n\tReal dc = distance(b, c);\n\treturn Line(b, (a * dc + c * da) / (da + dc));\n}\n\n// https://onlinejudge.u-aizu.ac.jp/status/users/Kite_kuma/submissions/1/CGL_7_B/judge/5763316/C++17\nCircle incircle_of_triangle(const Point &a, const Point &b, const Point &c) {\n\tPoint center = crosspoint(bisection_of_angle(a, b, c), bisection_of_angle(b, c, a));\n\treturn Circle(center, distance(Line(a, b), center));\n}\nCircle incircle_of_triangle(const Polygon &p) { return incircle_of_triangle(p[0], p[1], p[2]); }\n\n// https://onlinejudge.u-aizu.ac.jp/status/users/Kite_kuma/submissions/1/CGL_7_C/judge/5763330/C++17\nLine bisection_of_points(const Point &a, const Point &b) {\n\tPoint c = (a + b) * 0.5;\n\treturn Line(c, c + rotate(PI / 2, b - a));\n}\n\n// https://onlinejudge.u-aizu.ac.jp/status/users/Kite_kuma/submissions/1/CGL_7_C/judge/5763330/C++17\nCircle circumscribed_circle_of_triangle(const Point &a, const Point &b, const Point &c) {\n\tPoint center = crosspoint(bisection_of_points(a, b), bisection_of_points(b, c));\n\treturn Circle(center, distance(a, center));\n}\nCircle circumscribed_circle_of_triangle(const Polygon &p) { return circumscribed_circle_of_triangle(p[0], p[1], p[2]); }\n\n#pragma endregion\n\nusing R = Real;\nR solve() {\n\tint n;\n\tcin >> n;\n\tif(n == 0) exit(0);\n\tR w;\n\tcin >> w;\n\n\tPoints needles_place;\n\tV<R> needles_height;\n\trep(n) {\n\t\tReal x, y, h;\n\t\tcin >> x >> y >> h;\n\t\tneedles_place.emplace_back(x, y);\n\t\tneedles_height.push_back(h);\n\t}\n\n\tauto min_dist = [&](R radius, R hieght) -> R {\n\t\tif(radius < hieght) return radius;\n\t\treturn R(sqrt(radius * radius - (radius - hieght) * (radius - hieght)));\n\t};\n\n\tLines walls;\n\twalls.emplace_back(Point(0, 0), Point(0, 100));\n\twalls.emplace_back(Point(100, 100), Point(0, 100));\n\twalls.emplace_back(Point(0, 0), Point(100, 0));\n\twalls.emplace_back(Point(100, 100), Point(100, 0));\n\n\tLines internal;\n\tCircles cs(n);\n\n\tPoints crs;\n\n\tauto safe = [&](const Point &center, const R &radius) {\n\t\tif(isnan(center.real())) return false;\n\t\tif(isnan(center.imag())) return false;\n\n\t\tif(compare(center.real(), 0) == -1 || compare(center.real(), 100) == 1 || compare(center.imag(), 0) == -1 ||\n\t\t compare(center.imag(), 100) == 1) {\n\t\t\t// if(radius > 1. && radius < 3) debug(center, radius);\n\t\t\treturn false;\n\t\t}\n\n\t\trep(i, n) {\n\t\t\tif(compare(min_dist(radius, needles_height[i]), distance(center, needles_place[i])) == 1) return false;\n\t\t}\n\n\t\tR min_dist_wall = min_dist(radius, w);\n\t\tfoa(wall, walls) {\n\t\t\tif(compare(min_dist_wall, distance(wall, center)) == 1) return false;\n\t\t}\n\t\treturn true;\n\t};\n\n\tauto f = [&](R radius) {\n\t\tinternal.clear();\n\t\tcrs.clear();\n\t\tR min_dist_wall = min_dist(radius, w);\n\t\tinternal.emplace_back(Point(min_dist_wall, 0), Point(min_dist_wall, 100));\n\t\tinternal.emplace_back(Point(100, 100 - min_dist_wall), Point(0, 100 - min_dist_wall));\n\t\tinternal.emplace_back(Point(0, min_dist_wall), Point(100, min_dist_wall));\n\t\tinternal.emplace_back(Point(100 - min_dist_wall, 100), Point(100 - min_dist_wall, 0));\n\n\t\trep(i, n) { cs[i] = Circle(needles_place[i], min_dist(radius, needles_height[i])); }\n\n\t\tfoa(ln, internal) {\n\t\t\tfoa(ln2, internal) {\n\t\t\t\tif(intersect(ln, ln2)) crs.push_back(crosspoint(ln, ln2));\n\t\t\t}\n\n\t\t\tfoa(c, cs) {\n\t\t\t\tif(intersect(c, ln)) {\n\t\t\t\t\tauto ps = crosspoint(c, ln);\n\t\t\t\t\tcrs.push_back(ps.first);\n\t\t\t\t\tcrs.push_back(ps.second);\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\n\t\tfoa(c, cs) {\n\t\t\tfoa(d, cs) {\n\t\t\t\tif(intersect(c, d) > 0) {\n\t\t\t\t\tauto ps = crosspoint(c, d);\n\t\t\t\t\tcrs.push_back(ps.first);\n\t\t\t\t\tcrs.push_back(ps.second);\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\n\t\tfoa(pt, crs) {\n\t\t\tif(isnan(pt.real())) continue;\n\t\t\tif(safe(pt, radius)) {\n\t\t\t\tif(radius > 10000000000000.) debug(pt, radius);\n\t\t\t\treturn true;\n\t\t\t}\n\t\t}\n\t\treturn false;\n\t};\n\n\tR ok = 0.;\n\tR ng = 130;\n\trep(30) {\n\t\tR med = (ok + ng) / 2;\n\t\tif(f(med)) {\n\t\t\tok = med;\n\t\t} else {\n\t\t\tng = med;\n\t\t}\n\t}\n\treturn ok;\n}\n\nint main() {\n\tstd::ios::sync_with_stdio(false);\n\tstd::cin.tie(nullptr);\n\tstd::cout << std::fixed << std::setprecision(15);\n\tsrand((unsigned)time(NULL));\n\n\twhile(1) cout << solve() << dl;\n\n\treturn 0;\n}", "accuracy": 1, "time_ms": 800, "memory_kb": 3928, "score_of_the_acc": -0.8739, "final_rank": 13 }, { "submission_id": "aoj_1342_5792986", "code_snippet": "/*\tauthor: Kite_kuma\n\tcreated: 2021.08.17 02:01:28 */\n\n// #ifdef LOCAL\n// #define _GLIBCXX_DEBUG\n// #endif\n#include <bits/stdc++.h>\nusing namespace std;\n\n#pragma region macros\n\n#define foa(s, v) for(auto &s : v)\n#define all(v) (v).begin(), (v).end()\n#define rall(v) (v).rbegin(), (v).rend()\n#define popcnt(n) __builtin_popcountll((long long)n)\n\n#define REPname(a, b, c, d, e, ...) e\n#define rep(...) REPname(__VA_ARGS__, REP3, REP2, REP1, REP0)(__VA_ARGS__)\n#define REP0(x) for(int Counter_in_rep_macro = 0; Counter_in_rep_macro < (x); ++Counter_in_rep_macro)\n#define REP1(i, x) for(int i = 0; i < (x); ++i)\n#define REP2(i, l, r) for(int i = (l); i < (r); ++i)\n#define REP3(i, l, r, c) for(int i = (l); i < (r); i += (c))\n\n#define DREPname(a, b, c, d, e, ...) e\n#define drep(...) DREPname(__VA_ARGS__, DREP3, DREP2, DREP1)(__VA_ARGS__)\n#define DREP1(i, x) for(int i = (x)-1; i >= 0; --i)\n#define DREP2(i, l, r) for(int i = (r)-1; i >= (l); --i)\n#define DREP3(i, l, r, c) for(int i = (r)-1; i >= (l); i -= (c))\n\n#pragma endregion\n\n#pragma region aliases\n\nusing ll = long long;\nusing ld = long double;\nusing ull = unsigned long long;\nusing vi = std::vector<int>;\nusing vvi = std::vector<std::vector<int>>;\nusing vvvi = std::vector<std::vector<std::vector<int>>>;\nusing vll = std::vector<ll>;\nusing vvll = std::vector<vll>;\nusing vvvll = std::vector<vvll>;\nusing pii = std::pair<int, int>;\nusing pll = std::pair<long long, long long>;\ntemplate <class T = ll>\nusing V = std::vector<T>;\ntemplate <class T = ll>\nusing VV = V<V<T>>;\ntemplate <class T = ll>\nusing VVV = V<V<V<T>>>;\ntemplate <class T = ll>\nusing pqup = std::priority_queue<T, std::vector<T>, std::greater<T>>;\ntemplate <class T = ll>\nusing pqdn = std::priority_queue<T>;\n\n#pragma endregion\n\n#pragma region constants\n\nconst int inf = 1e9;\nconst long long INF = 1e18;\nconst long double pi = acos(-1);\nconst char dl = '\\n';\nconst char sp = ' ';\nint dx[8] = {1, 0, -1, 0, 1, -1, -1, 1};\nint dy[8] = {0, 1, 0, -1, 1, 1, -1, -1};\n\nconst int mod_1000000007 = 1000000007;\nconst int mod_998244353 = 998244353;\n\n#pragma endregion\n\n#pragma region basic_operation\n\ntemplate <class T0, class T1, class T2>\ninline bool in_range(T0 x, T1 lef, T2 rig) {\n\treturn ((lef <= x) && (x < rig));\n}\n\ntemplate <class T>\ninline bool chmin(T &a, T b) {\n\tif(a > b) {\n\t\ta = b;\n\t\treturn true;\n\t}\n\treturn false;\n}\ntemplate <class T>\ninline bool chmax(T &a, T b) {\n\tif(a < b) {\n\t\ta = b;\n\t\treturn true;\n\t}\n\treturn false;\n}\n\nvoid Yes(bool f = 1) { std::cout << (f ? \"Yes\" : \"No\") << '\\n'; }\nvoid No() { std::cout << \"No\\n\"; }\nvoid YES(bool f = 1) { std::cout << (f ? \"YES\" : \"NO\") << '\\n'; }\nvoid NO() { std::cout << \"NO\\n\"; }\n\ntemplate <class T>\nvoid drop(T answer) {\n\tstd::cout << answer << '\\n';\n\texit(0);\n}\n\nvoid err(bool flag = true) {\n\tif(!flag) return;\n\tstd::cout << -1 << '\\n';\n\texit(0);\n}\n\ntemplate <class T>\nvoid vout(std::vector<T> const &v, bool vertically = 0) {\n\tif(vertically) {\n\t\tfor(auto const &a : v) {\n\t\t\tstd::cout << a << '\\n';\n\t\t}\n\t\treturn;\n\t}\n\tfor(auto it = v.begin(); it != v.end(); it++) {\n\t\tstd::cout << (*it);\n\t\tif(it != v.end() - 1) {\n\t\t\tstd::cout << ' ';\n\t\t}\n\t}\n\tstd::cout << '\\n';\n\treturn;\n}\n\ninline void print() { std::cout << '\\n'; }\ntemplate <class T>\ninline void print(T x) {\n\tstd::cout << x << '\\n';\n\treturn;\n}\ntemplate <typename Head, typename... Tail>\nvoid print(Head H, Tail... T) {\n\tstd::cout << H << \" \";\n\tprint(T...);\n}\n\ntemplate <class T>\nvoid add(std::vector<T> &v, T val) {\n\tfor(auto &a : v) a += val;\n\treturn;\n}\n\ntemplate <class T>\nT dup(T a, T b) {\n\tassert(b != 0);\n\treturn (a + b - 1) / b;\n}\n\ntemplate <class T>\nT greatest_lower_multiple(T x, T d) {\n\tif(d == 0) return 0;\n\tif(d < 0) d *= -1;\n\tT y = x % d;\n\tif(y < 0) y += d;\n\treturn x - y;\n}\n\ntemplate <class T>\nT least_upper_multiple(T x, T d) {\n\treturn -greatest_lower_multiple(-x, d);\n}\n\nlong long POW(long long a, long long n) {\n\tlong long res = 1;\n\twhile(n > 0) {\n\t\tif(n & 1) res = res * a;\n\t\ta = a * a;\n\t\tn >>= 1;\n\t}\n\treturn res;\n}\n\nlong long modpow(long long a, long long n, long long mod) {\t // a^n mod\n\tassert(n >= 0 && mod);\n\tif(mod == 1) return 0LL;\n\tlong long res = 1;\n\ta %= mod;\n\twhile(n > 0) {\n\t\tif(n & 1) res = res * a % mod;\n\t\ta = a * a % mod;\n\t\tn >>= 1;\n\t}\n\treturn res < 0 ? res + mod : res;\n}\n\n// return x which satisfies a * x % mod == gcd(a, mod)\n// not (mod | a)\nlong long modinv(long long a, long long mod) {\n\tlong long b = mod, u = 1, v = 0;\n\twhile(b) {\n\t\tlong long t = a / b;\n\t\ta -= t * b;\n\t\tstd::swap(a, b);\n\t\tu -= t * v;\n\t\tstd::swap(u, v);\n\t}\n\tu %= mod;\n\tif(u < 0) u += mod;\n\treturn u;\n}\n\ntemplate <class T>\nint lb(const std::vector<T> &a, const T x) {\n\treturn std::distance(a.begin(), std::lower_bound(a.begin(), a.end(), x));\n}\ntemplate <class T>\nint ub(const std::vector<T> &a, const T x) {\n\treturn std::distance(a.begin(), std::upper_bound(a.begin(), a.end(), x));\n}\ntemplate <class T>\nvoid unq_sort(std::vector<T> &a) {\n\tstd::sort(a.begin(), a.end());\n\ta.erase(std::unique(a.begin(), a.end()), a.end());\n}\ntemplate <class T>\nstd::vector<int> press(std::vector<T> &a) {\n\tauto vec = a;\n\tunq_sort(vec);\n\tstd::vector<int> ret;\n\tfor(auto &v : a) ret.push_back(lb(vec, v));\n\treturn ret;\n}\n\n#pragma endregion\n\n#pragma region input\n#define VEC(type, name, size) \\\n\tvector<type> name(size); \\\n\tscanner::INPUT(name)\n#define VVEC(type, name, h, w) \\\n\tvector<vector<type>> name(h, vector<type>(w)); \\\n\tscanner::INPUT(name)\n#define INT(...) \\\n\tint __VA_ARGS__; \\\n\tscanner::INPUT(__VA_ARGS__)\n#define LL(...) \\\n\tlong long __VA_ARGS__; \\\n\tscanner::INPUT(__VA_ARGS__)\n#define STR(...) \\\n\tstring __VA_ARGS__; \\\n\tscanner::INPUT(__VA_ARGS__)\n#define CHR(...) \\\n\tchar __VA_ARGS__; \\\n\tscanner::INPUT(__VA_ARGS__)\n#define DBL(...) \\\n\tdouble __VA_ARGS__; \\\n\tscanner::INPUT(__VA_ARGS__)\n#define LD(...) \\\n\tlong double __VA_ARGS__; \\\n\tscanner::INPUT(__VA_ARGS__)\n#define TPL3(type0, type1, type2, name) \\\n\tstd::tuple<type0, type1, type2> name; \\\n\tscanner::INPUT(name);\n#define TPL4(type0, type1, type2, type3, name) \\\n\tstd::tuple<type0, type1, type2, type3> name; \\\n\tscanner::INPUT(name);\n\nnamespace scanner {\ntemplate <class T>\nvoid scan(T &a) {\n\tstd::cin >> a;\n}\n\ntemplate <class T, class L>\nvoid scan(std::pair<T, L> &p) {\n\tscan(p.first);\n\tscan(p.second);\n}\n\ntemplate <class T0, class T1, class T2>\nvoid scan(std::tuple<T0, T1, T2> &p) {\n\tT0 t0;\n\tT1 t1;\n\tT2 t2;\n\tscan(t0);\n\tscan(t1);\n\tscan(t2);\n\tp = std::make_tuple(t0, t1, t2);\n}\n\ntemplate <class T0, class T1, class T2, class T3>\nvoid scan(std::tuple<T0, T1, T2, T3> &p) {\n\tT0 t0;\n\tT1 t1;\n\tT2 t2;\n\tT3 t3;\n\tscan(t0);\n\tscan(t1);\n\tscan(t2);\n\tscan(t3);\n\tp = std::make_tuple(t0, t1, t2, t3);\n}\n\ntemplate <class T>\nvoid scan(std::vector<T> &a) {\n\tfor(auto &i : a) scan(i);\n}\n\nvoid INPUT() {}\ntemplate <class Head, class... Tail>\nvoid INPUT(Head &head, Tail &... tail) {\n\tscan(head);\n\tINPUT(tail...);\n}\n} // namespace scanner\n\ntemplate <typename T1, typename T2>\nstd::istream &operator>>(std::istream &is, std::pair<T1, T2> &p) {\n\tis >> p.first >> p.second;\n\treturn is;\n}\n#pragma endregion\n\n#pragma region debug\n\n#pragma region output\ntemplate <typename T1, typename T2>\nstd::ostream &std::operator<<(std::ostream &os, const std::pair<T1, T2> &p) {\n\tos << p.first << \" \" << p.second;\n\treturn os;\n}\ntemplate <class T>\nstd::ostream &std::operator<<(std::ostream &os, const std::vector<T> &v) {\n\tfor(int i = 0; i < (int)v.size(); i++) {\n\t\tif(i) os << \" \";\n\t\tos << v[i];\n\t}\n\treturn os;\n}\n#pragma endregion\n\n#pragma region view\n\nnamespace viewer {\nvoid view(const long long e) {\n\tif(e == INF)\n\t\tstd::cerr << \"INF\";\n\telse if(e == -INF)\n\t\tstd::cerr << \"-INF\";\n\telse\n\t\tstd::cerr << e;\n}\n\nvoid view(const int e) {\n\tif(e == inf)\n\t\tstd::cerr << \"inf\";\n\telse if(e == -inf)\n\t\tstd::cerr << \"-inf\";\n\telse\n\t\tstd::cerr << e;\n}\n\ntemplate <typename T>\nvoid view(const T e) {\n\tstd::cerr << e;\n}\n\ntemplate <typename T, typename U>\nvoid view(const std::pair<T, U> &p) {\n\tstd::cerr << \"(\";\n\tview(p.first);\n\tstd::cerr << \", \";\n\tview(p.second);\n\tstd::cerr << \")\";\n}\n\ntemplate <class T0, class T1, class T2>\nvoid view(const std::tuple<T0, T1, T2> &p) {\n\tstd::cerr << \"(\";\n\tview(std::get<0>(p));\n\tstd::cerr << \", \";\n\tview(std::get<1>(p));\n\tstd::cerr << \", \";\n\tview(std::get<2>(p));\n\tstd::cerr << \")\";\n}\n\ntemplate <class T0, class T1, class T2, class T3>\nvoid view(const std::tuple<T0, T1, T2, T3> &p) {\n\tstd::cerr << \"(\";\n\tview(std::get<0>(p));\n\tstd::cerr << \", \";\n\tview(std::get<1>(p));\n\tstd::cerr << \", \";\n\tview(std::get<2>(p));\n\tstd::cerr << \", \";\n\tview(std::get<3>(p));\n\tstd::cerr << \")\";\n}\n\ntemplate <typename T>\nvoid view(const std::set<T> &s) {\n\tif(s.empty()) {\n\t\tstd::cerr << \"{ }\";\n\t\treturn;\n\t}\n\tstd::cerr << \"{ \";\n\tfor(auto &t : s) {\n\t\tview(t);\n\t\tstd::cerr << \", \";\n\t}\n\tstd::cerr << \"\\b\\b }\";\n}\n\ntemplate <typename T>\nvoid view(const std::unordered_set<T> &s) {\n\tif(s.empty()) {\n\t\tstd::cerr << \"{ }\";\n\t\treturn;\n\t}\n\tstd::cerr << \"{ \";\n\tfor(auto &t : s) {\n\t\tview(t);\n\t\tstd::cerr << \", \";\n\t}\n\tstd::cerr << \"\\b\\b }\";\n}\n\ntemplate <typename T>\nvoid view(const std::vector<T> &v) {\n\tif(v.empty()) {\n\t\tstd::cerr << \"{ }\";\n\t\treturn;\n\t}\n\tstd::cerr << \"{ \";\n\tfor(const auto &e : v) {\n\t\tview(e);\n\t\tstd::cerr << \", \";\n\t}\n\tstd::cerr << \"\\b\\b }\";\n}\n\ntemplate <typename T>\nvoid view(const std::vector<std::vector<T>> &vv) {\n\tstd::cerr << \"{\\n\";\n\tfor(const auto &v : vv) {\n\t\tstd::cerr << \"\\t\";\n\t\tview(v);\n\t\tstd::cerr << '\\n';\n\t}\n\tstd::cerr << \"}\";\n}\n\ntemplate <typename T, typename U>\nvoid view(const std::vector<std::pair<T, U>> &v) {\n\tstd::cerr << \"{\\n\";\n\tfor(const auto &c : v) {\n\t\tstd::cerr << \"\\t(\";\n\t\tview(c.first);\n\t\tstd::cerr << \", \";\n\t\tview(c.second);\n\t\tstd::cerr << \")\\n\";\n\t}\n\tstd::cerr << \"}\";\n}\n\ntemplate <class T0, class T1, class T2>\nvoid view(const std::vector<std::tuple<T0, T1, T2>> &v) {\n\tif(v.empty()) {\n\t\tstd::cerr << \"{ }\";\n\t\treturn;\n\t}\n\tstd::cerr << '{';\n\tfor(const auto &t : v) {\n\t\tstd::cerr << \"\\n\\t\";\n\t\tview(t);\n\t\tstd::cerr << \",\";\n\t}\n\tstd::cerr << \"\\n}\";\n}\n\ntemplate <class T0, class T1, class T2, class T3>\nvoid view(const std::vector<std::tuple<T0, T1, T2, T3>> &v) {\n\tif(v.empty()) {\n\t\tstd::cerr << \"{ }\";\n\t\treturn;\n\t}\n\tstd::cerr << '{';\n\tfor(const auto &t : v) {\n\t\tstd::cerr << \"\\n\\t\";\n\t\tview(t);\n\t\tstd::cerr << \",\";\n\t}\n\tstd::cerr << \"\\n}\";\n}\n\ntemplate <typename T, typename U>\nvoid view(const std::map<T, U> &m) {\n\tstd::cerr << \"{\\n\";\n\tfor(const auto &t : m) {\n\t\tstd::cerr << \"\\t[\";\n\t\tview(t.first);\n\t\tstd::cerr << \"] : \";\n\t\tview(t.second);\n\t\tstd::cerr << '\\n';\n\t}\n\tstd::cerr << \"}\";\n}\n\ntemplate <typename T, typename U>\nvoid view(const std::unordered_map<T, U> &m) {\n\tstd::cerr << \"{\\n\";\n\tfor(const auto &t : m) {\n\t\tstd::cerr << \"\\t[\";\n\t\tview(t.first);\n\t\tstd::cerr << \"] : \";\n\t\tview(t.second);\n\t\tstd::cerr << '\\n';\n\t}\n\tstd::cerr << \"}\";\n}\n} // namespace viewer\n\n#pragma endregion\n\n// when debugging : g++ foo.cpp -DLOCAL\n#ifdef LOCAL\nvoid debug_out() {}\ntemplate <typename Head, typename... Tail>\nvoid debug_out(Head H, Tail... T) {\n\tviewer::view(H);\n\tstd::cerr << \", \";\n\tdebug_out(T...);\n}\n#define debug(...) \\\n\tdo { \\\n\t\tstd::cerr << __LINE__ << \" [\" << #__VA_ARGS__ << \"] : [\"; \\\n\t\tdebug_out(__VA_ARGS__); \\\n\t\tstd::cerr << \"\\b\\b]\\n\"; \\\n\t} while(0)\n#define dump(x) \\\n\tdo { \\\n\t\tstd::cerr << __LINE__ << \" \" << #x << \" : \"; \\\n\t\tviewer::view(x); \\\n\t\tstd::cerr << '\\n'; \\\n\t} while(0)\n\n#else\n#define debug(...) (void(0))\n#define dump(x) (void(0))\n#endif\n\n#pragma endregion\n\n#pragma region geometry\n\nusing Real = long double;\nusing Point = complex<Real>;\nusing Vec = Point;\nconst Real EPS = 1e-10, PI = acos(-1);\n\ninline bool eq(const Real &a, const Real &b) { return fabs(b - a) < EPS; }\ninline bool eq(const Point &a, const Point &b) { return fabs(b - a) < EPS; }\n/*\n\t-1: a < b\n\t0 : a == b\n\t1 : a > b\n*/\ninline int compare(const Real &a, const Real &b) { return eq(a, b) ? 0 : a < b ? -1 : 1; }\n\nconst Point operator*(const Point &p, const Real &d) { return Point(real(p) * d, imag(p) * d); }\n\nistream &operator>>(istream &is, Point &p) {\n\tReal a, b;\n\tis >> a >> b;\n\tp = Point(a, b);\n\treturn is;\n}\nostream &operator<<(ostream &os, Point &p) { return os << fixed << setprecision(10) << p.real() << \" \" << p.imag(); }\n\n// rotate point 'p' for counter clockwise direction\nconst Point rotate(const Real &theta, const Point &p) {\n\treturn Point(cos(theta) * p.real() - sin(theta) * p.imag(), sin(theta) * p.real() + cos(theta) * p.imag());\n}\n\nconst Real radian_to_degree(const Real &r) { return (r * 180.0 / PI); }\n\nconst Real degree_to_radian(const Real &d) { return (d * PI / 180.0); }\n\n// get angle a-b-c (<pi)\nconst Real get_angle(const Point &a, const Point &b, const Point &c) {\n\tconst Point v(a - b), w(c - b);\n\tReal theta = fabs(atan2(w.imag(), w.real()) - atan2(v.imag(), v.real()));\n\treturn min(theta, 2 * PI - theta);\n}\n\nnamespace std {\nbool operator<(const Point &a, const Point &b) { return a.real() != b.real() ? a.real() < b.real() : a.imag() < b.imag(); }\n} // namespace std\n\nstruct Line {\n\tPoint a, b;\n\n\tLine() = default;\n\n\tLine(Point a, Point b) : a(a), b(b) {}\n\n\tLine(Real A, Real B, Real C) // Ax + By = C\n\t{\n\t\tif(eq(A, 0))\n\t\t\ta = Point(0, C / B), b = Point(1, C / B);\n\t\telse if(eq(B, 0))\n\t\t\tb = Point(C / A, 0), b = Point(C / A, 1);\n\t\telse\n\t\t\ta = Point(0, C / B), b = Point(C / A, 0);\n\t}\n\n\tfriend ostream &operator<<(ostream &os, Line &p) { return os << p.a << \" -- \" << p.b; }\n\n\tfriend istream &operator>>(istream &is, Line &a) { return is >> a.a >> a.b; }\n};\n\nstruct Segment : Line {\n\tSegment() = default;\n\n\tSegment(Point a, Point b) : Line(a, b) {}\n};\n\nstruct Circle {\n\tPoint p;\n\tReal r;\n\n\tCircle() = default;\n\n\tCircle(Point p, Real r) : p(p), r(r) {}\n};\n\nusing Points = vector<Point>;\nusing Polygon = vector<Point>;\nusing Segments = vector<Segment>;\nusing Lines = vector<Line>;\nusing Circles = vector<Circle>;\n\nconst Real cross(const Point &a, const Point &b) { return real(a) * imag(b) - imag(a) * real(b); }\nconst Real dot(const Point &a, const Point &b) { return real(a) * real(b) + imag(a) * imag(b); }\n\n// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_1_C\nconst int ccw(const Point &a, Point b, Point c) {\n\tb = b - a, c = c - a;\n\tif(cross(b, c) > EPS) return +1;\t\t// \"COUNTER_CLOCKWISE\"\n\tif(cross(b, c) < -EPS) return -1;\t\t// \"CLOCKWISE\"\n\tif(dot(b, c) < -EPS) return +2;\t\t\t// \"ONLINE_BACK\"\n\tif(norm(b) + EPS < norm(c)) return -2;\t// \"ONLINE_FRONT\"\n\treturn 0;\t\t\t\t\t\t\t\t// \"ON_SEGMENT\"\n}\n\n// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_2_A\nbool parallel(const Line &a, const Line &b) { return eq(cross(a.b - a.a, b.b - b.a), 0.0); }\n\n// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_2_A\nbool orthogonal(const Line &a, const Line &b) { return eq(dot(a.a - a.b, b.a - b.b), 0.0); }\n\n// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_1_A\nPoint projection(const Line &l, const Point &p) {\n\tdouble t = dot(p - l.a, l.a - l.b) / norm(l.a - l.b);\n\treturn l.a + (l.a - l.b) * t;\n}\n\nPoint projection(const Segment &l, const Point &p) {\n\tdouble t = dot(p - l.a, l.a - l.b) / norm(l.a - l.b);\n\treturn l.a + (l.a - l.b) * t;\n}\n\n// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_1_B\nPoint reflection(const Line &l, const Point &p) { return projection(l, p) * 2.0 - p; }\n\nint intersect(const Line &l, const Point &p) { return int(abs(ccw(l.a, l.b, p)) != 1); }\n\nint intersect(const Line &l, const Line &m) {\n\tif(intersect(l, m.a) && intersect(l, m.b)) return -1;\n\treturn int(abs(cross(l.b - l.a, m.b - m.a)) > EPS);\n}\n\nint intersect(const Segment &s, const Point &p) { return int(ccw(s.a, s.b, p) == 0); }\n\nint intersect(const Line &l, const Segment &s) {\n\tif(intersect(l, s.a) && intersect(l, s.b)) return -1;\n\treturn cross(l.b - l.a, s.a - l.a) * cross(l.b - l.a, s.b - l.a) < EPS;\n}\n\nReal distance(const Line &l, const Point &p);\n\nint intersect(const Circle &c, const Line &l) {\n\tReal d = c.r - distance(l, c.p);\n\treturn fabs(d) < EPS ? 1 : d > 0. ? 2 : 0;\n}\n\nint intersect(const Circle &c, const Point &p) { return int(abs(abs(p - c.p) - c.r) < EPS); }\n\n// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_2_B\nint intersect(const Segment &s, const Segment &t) {\n\tif(eq(s.a, s.b)) return intersect(t, s.a);\n\tif(intersect(Line(s), t.a) && intersect(Line(s), t.b) &&\n\t std::max(std::min(s.a, s.b), std::min(t.a, t.b)) < std::min(std::max(s.a, s.b), std::max(t.a, t.b)))\n\t\treturn -1;\n\treturn int(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);\n}\n\nint intersect(const Circle &c, const Segment &l) {\n\tconst Point h = projection(l, c.p);\n\tif(norm(h - c.p) - c.r * c.r > EPS) return 0;\n\tauto d1 = abs(c.p - l.a), d2 = abs(c.p - l.b);\n\tif(compare(d1, c.r) == -1 && compare(d2, c.r) == -1) return 0;\n\tif(d1 < c.r - EPS && d2 > c.r - EPS || d1 > c.r - EPS && d2 < c.r - EPS) return 1;\n\treturn dot(l.a - h, l.b - h) < 0 ? 2 : 0;\n}\n\nReal distance(const Point &a, const Point &b);\n\nint number_of_common_tangents(const Circle &c1, const Circle &c2) {\n\tReal r1 = min(c1.r, c2.r), r2 = max(c1.r, c2.r), d = distance(c1.p, c2.p);\n\tint com = compare(r1 + r2, d);\n\treturn com == 1 ? compare(d + r1, r2) + 1 : 3 - com;\n\t// if(c1.r < c2.r) swap(c1, c2);\n\t// Real d = abs(c1.p - c2.p);\n\t// if(compare(c1.r + c2.r, d) == -1) return 4;\n\t// if(eq(c1.r + c2.r, d)) return 3;\n\t// if(compare(c1.r - c2.r, d) == -1) return 2;\n\t// return int(eq(c1.r - c2.r, d));\n}\n\n// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_7_A&lang=jp\nint intersect(const Circle &c1, const Circle &c2) { return 2 - abs(2 - number_of_common_tangents(c1, c2)); }\n\nReal distance(const Point &a, const Point &b) { return abs(a - b); }\n\nReal distance(const Line &l, const Point &p) { return abs(p - projection(l, p)); }\n\nReal distance(const Line &l, const Line &m) { return intersect(l, m) ? 0 : distance(l, m.a); }\n\nReal distance(const Segment &s, const Point &p) {\n\tPoint r = projection(s, p);\n\treturn intersect(s, r) ? distance(r, p) : min(abs(s.a - p), abs(s.b - p));\n}\n\n// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_2_D\nReal distance(const Segment &a, const Segment &b) {\n\tif(intersect(a, b)) return 0;\n\treturn min({distance(a, b.a), distance(a, b.b), distance(b, a.a), distance(b, a.b)});\n}\n\nReal distance(const Line &l, const Segment &s) {\n\tif(intersect(l, s)) return 0;\n\treturn min(distance(l, s.a), distance(l, s.b));\n}\n\nPoint crosspoint(const Line &l, const Line &m) {\n\tReal A = cross(l.b - l.a, m.b - m.a);\n\tReal B = cross(l.b - l.a, l.b - m.a);\n\tif(eq(abs(A), 0.0) && eq(abs(B), 0.0)) return m.a;\n\treturn m.a + (m.b - m.a) * B / A;\n}\n\n// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_2_C\nPoint crosspoint(const Segment &l, const Segment &m) { return crosspoint(Line(l), Line(m)); }\n\n// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_7_D\npair<Point, Point> crosspoint(const Circle &c, const Line l) {\n\tPoint pr = projection(l, c.p);\n\tif(eq(distance(l, c.p), c.r)) return {pr, pr};\n\tVec v = (l.b - l.a) / abs(l.b - l.a) * sqrt(c.r * c.r - norm(pr - c.p));\n\treturn make_pair(pr - v, pr + v);\n\t// Vec e = (l.b - l.a) / abs(l.b - l.a);\n\t// double base = sqrt(c.r * c.r - norm(pr - c.p));\n\t// return {pr - e * base, pr + e * base};\n}\n\npair<Point, Point> crosspoint(const Circle &c, const Segment &l) {\n\tif(intersect(c, l) == 2) return crosspoint(c, Line(l.a, l.b));\n\tauto ret = crosspoint(c, Line(l.a, l.b));\n\tif(dot(l.a - ret.first, l.b - ret.first) < EPS)\n\t\tret.second = ret.first;\n\telse\n\t\tret.first = ret.second;\n\treturn ret;\n}\n\n// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_7_E\npair<Point, Point> crosspoint(const Circle &c1, const Circle &c2) {\n\tReal d = abs(c1.p - c2.p);\n\tReal a = acos((c1.r * c1.r + d * d - c2.r * c2.r) / (2 * c1.r * d)); // cosine theorem\n\tReal t = atan2(c2.p.imag() - c1.p.imag(), c2.p.real() - c1.p.real());\n\tPoint p1 = c1.p + Point(cos(t + a) * c1.r, sin(t + a) * c1.r);\n\tPoint p2 = c1.p + Point(cos(t - a) * c1.r, sin(t - a) * c1.r);\n\treturn make_pair(p1, p2);\n}\n\n// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_7_F\n// \"点 p を通る円 c の接線\"の接点2つ\npair<Point, Point> tangent(const Circle &c1, const Point &p2) { return crosspoint(c1, Circle(p2, sqrt(norm(c1.p - p2) - c1.r * c1.r))); }\n\n// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_7_G\n// 円 c1, c2 の共通接線\nLines tangent(Circle c1, Circle c2) {\n\tLines ret;\n\tif(c1.r < c2.r) swap(c1, c2);\n\tReal g = norm(c1.p - c2.p);\n\tif(eq(g, 0)) return ret;\n\tVec u = (c2.p - c1.p) / sqrt(g);\n\tVec v = rotate(PI * 0.5, u);\n\tfor(int s : {-1, 1}) {\n\t\tReal h = (c1.r + s * c2.r) / sqrt(g);\n\t\tif(eq(1 - h * h, 0)) {\n\t\t\tret.emplace_back(c1.p + u * c1.r, c1.p + (u + v) * c1.r);\n\t\t} else if(1 - h * h > 0) {\n\t\t\tPoint uu = u * h, vv = v * sqrt(1 - h * h);\n\t\t\tret.emplace_back(c1.p + (uu + vv) * c1.r, c2.p - (uu + vv) * c2.r * s);\n\t\t\tret.emplace_back(c1.p + (uu - vv) * c1.r, c2.p - (uu - vv) * c2.r * s);\n\t\t}\n\t}\n\treturn ret;\n}\n// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_7_H\n// 円と多角形の共通部分の面積\nReal area(const Polygon &p, const Circle &c) {\n\tif(p.size() < 3) return 0.0;\n\tfunction<Real(Circle, Point, Point)> cross_area = [&](const Circle &c, const Point &a, const Point &b) {\n\t\tPoint va = c.p - a, vb = c.p - b;\n\t\tReal f = cross(va, vb), ret = 0.0;\n\t\tif(eq(f, 0.0)) return ret;\n\t\tif(max(abs(va), abs(vb)) < c.r + EPS) return f;\n\t\tif(distance(Segment(a, b), c.p) > c.r - EPS) return c.r * c.r * arg(vb * conj(va));\n\t\tauto u = crosspoint(c, Segment(a, b));\n\t\tvector<Point> tot{a, u.first, u.second, b};\n\t\tfor(int i = 0; i + 1 < tot.size(); i++) {\n\t\t\tret += cross_area(c, tot[i], tot[i + 1]);\n\t\t}\n\t\treturn ret;\n\t};\n\tReal A = 0;\n\tfor(int i = 0; i < p.size(); i++) {\n\t\tA += cross_area(c, p[i], p[(i + 1) % p.size()]);\n\t}\n\treturn A * 0.5;\n}\n\n// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_4_B\n// 凸多角形の直径(最遠頂点対間距離)\nReal convex_diameter(const Polygon &p) {\n\tint N = (int)p.size();\n\tint is = 0, js = 0;\n\tfor(int i = 1; i < N; i++) {\n\t\tif(p[i].imag() > p[is].imag()) is = i;\n\t\tif(p[i].imag() < p[js].imag()) js = i;\n\t}\n\tReal maxdis = norm(p[is] - p[js]);\n\n\tint maxi, maxj, i, j;\n\ti = maxi = is;\n\tj = maxj = js;\n\tdo {\n\t\tif(cross(p[(i + 1) % N] - p[i], p[(j + 1) % N] - p[j]) >= 0) {\n\t\t\tj = (j + 1) % N;\n\t\t} else {\n\t\t\ti = (i + 1) % N;\n\t\t}\n\t\tif(norm(p[i] - p[j]) > maxdis) {\n\t\t\tmaxdis = norm(p[i] - p[j]);\n\t\t\tmaxi = i;\n\t\t\tmaxj = j;\n\t\t}\n\t} while(i != is || j != js);\n\treturn sqrt(maxdis);\n}\n\n// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_5_A\n// 最近点対\nReal closest_pair(Points ps) {\n\tif(ps.size() <= 1) throw(0);\n\tsort(begin(ps), end(ps)); // xでソート\n\n\tauto compare_y = [&](const Point &a, const Point &b) { return imag(a) < imag(b); };\n\tvector<Point> beet(ps.size());\n\tconst Real INF = 1e18;\n\n\tfunction<Real(int, int)> rec = [&](int left, int right) {\n\t\tif(right - left <= 1) return INF;\n\t\tint mid = (left + right) >> 1;\n\t\tauto x = real(ps[mid]);\t // 領域内でx座標真中の点\n\t\tauto ret = min(rec(left, mid), rec(mid, right));\n\t\t// y座標でソートされた2つの区間をマージ\n\t\tinplace_merge(begin(ps) + left, begin(ps) + mid, begin(ps) + right, compare_y);\n\t\tint ptr = 0;\n\t\tfor(int i = left; i < right; i++) {\n\t\t\tif(abs(real(ps[i]) - x) >= ret) continue;\n\t\t\tfor(int j = 0; j < ptr; j++) {\n\t\t\t\tauto luz = ps[i] - beet[ptr - j - 1];\n\t\t\t\tif(imag(luz) >= ret) break;\n\t\t\t\tret = min(ret, abs(luz));\n\t\t\t}\n\t\t\tbeet[ptr++] = ps[i];\n\t\t}\n\t\treturn ret;\n\t};\n\treturn rec(0, (int)ps.size());\n}\n\n// (aからの距離):(bからの距離) = dist_a : dist_b となる点の集合(円)を返す\nCircle Apollonius_Circle(Point a, Point b, Real dist_a, Real dist_b) {\n\tassert(!eq(dist_a, dist_b)); //同じ場合はbisectionで\n\tReal asq = dist_a * dist_a, bsq = dist_b * dist_b;\n\tPoint p = (a * bsq - b * asq) * (1 / (bsq - asq));\n\tReal r = abs(a - b) * dist_a * dist_b / abs(asq - bsq);\n\treturn Circle(p, r);\n}\n\n// bisection of angle A-B-C\n// https://onlinejudge.u-aizu.ac.jp/status/users/Kite_kuma/submissions/1/CGL_7_B/judge/5763316/C++17\nLine bisection_of_angle(const Point &a, const Point &b, const Point &c) {\n\tReal da = distance(a, b);\n\tReal dc = distance(b, c);\n\treturn Line(b, (a * dc + c * da) / (da + dc));\n}\n\n// https://onlinejudge.u-aizu.ac.jp/status/users/Kite_kuma/submissions/1/CGL_7_B/judge/5763316/C++17\nCircle incircle_of_triangle(const Point &a, const Point &b, const Point &c) {\n\tPoint center = crosspoint(bisection_of_angle(a, b, c), bisection_of_angle(b, c, a));\n\treturn Circle(center, distance(Line(a, b), center));\n}\nCircle incircle_of_triangle(const Polygon &p) { return incircle_of_triangle(p[0], p[1], p[2]); }\n\n// https://onlinejudge.u-aizu.ac.jp/status/users/Kite_kuma/submissions/1/CGL_7_C/judge/5763330/C++17\nLine bisection_of_points(const Point &a, const Point &b) {\n\tPoint c = (a + b) * 0.5;\n\treturn Line(c, c + rotate(PI / 2, b - a));\n}\n\n// https://onlinejudge.u-aizu.ac.jp/status/users/Kite_kuma/submissions/1/CGL_7_C/judge/5763330/C++17\nCircle circumscribed_circle_of_triangle(const Point &a, const Point &b, const Point &c) {\n\tPoint center = crosspoint(bisection_of_points(a, b), bisection_of_points(b, c));\n\treturn Circle(center, distance(a, center));\n}\nCircle circumscribed_circle_of_triangle(const Polygon &p) { return circumscribed_circle_of_triangle(p[0], p[1], p[2]); }\n\n#pragma endregion\n\nusing R = Real;\nR solve() {\n\tint n;\n\tcin >> n;\n\tif(n == 0) exit(0);\n\tR w;\n\tcin >> w;\n\n\tPoints needles_place;\n\tV<R> needles_height;\n\trep(n) {\n\t\tReal x, y, h;\n\t\tcin >> x >> y >> h;\n\t\tneedles_place.emplace_back(x, y);\n\t\tneedles_height.push_back(h);\n\t}\n\n\tauto min_dist = [&](R radius, R hieght) -> R {\n\t\tif(radius < hieght) return radius;\n\t\treturn R(sqrt(radius * radius - (radius - hieght) * (radius - hieght)));\n\t};\n\n\tLines walls;\n\twalls.emplace_back(Point(0, 0), Point(0, 100));\n\twalls.emplace_back(Point(100, 100), Point(0, 100));\n\twalls.emplace_back(Point(0, 0), Point(100, 0));\n\twalls.emplace_back(Point(100, 100), Point(100, 0));\n\n\tLines internal;\n\tCircles cs(n);\n\n\tPoints crs;\n\n\tauto safe = [&](const Point &center, const R &radius) {\n\t\tif(isnan(center.real())) return false;\n\t\tif(isnan(center.imag())) return false;\n\n\t\tif(compare(center.real(), 0) == -1 || compare(center.real(), 100) == 1 || compare(center.imag(), 0) == -1 ||\n\t\t compare(center.imag(), 100) == 1) {\n\t\t\t// if(radius > 1. && radius < 3) debug(center, radius);\n\t\t\treturn false;\n\t\t}\n\n\t\trep(i, n) {\n\t\t\tif(compare(min_dist(radius, needles_height[i]), distance(center, needles_place[i])) == 1) return false;\n\t\t}\n\n\t\tR min_dist_wall = min_dist(radius, w);\n\t\tfoa(wall, walls) {\n\t\t\tif(compare(min_dist_wall, distance(wall, center)) == 1) return false;\n\t\t}\n\t\treturn true;\n\t};\n\n\tauto f = [&](R radius) {\n\t\tinternal.clear();\n\t\tcrs.clear();\n\t\tR min_dist_wall = min_dist(radius, w);\n\t\tinternal.emplace_back(Point(min_dist_wall, 0), Point(min_dist_wall, 100));\n\t\tinternal.emplace_back(Point(100, 100 - min_dist_wall), Point(0, 100 - min_dist_wall));\n\t\tinternal.emplace_back(Point(0, min_dist_wall), Point(100, min_dist_wall));\n\t\tinternal.emplace_back(Point(100 - min_dist_wall, 100), Point(100 - min_dist_wall, 0));\n\n\t\trep(i, n) { cs[i] = Circle(needles_place[i], min_dist(radius, needles_height[i])); }\n\n\t\tfoa(ln, internal) {\n\t\t\tfoa(ln2, internal) {\n\t\t\t\tif(intersect(ln, ln2)) crs.push_back(crosspoint(ln, ln2));\n\t\t\t}\n\n\t\t\tfoa(c, cs) {\n\t\t\t\tif(intersect(c, ln)) {\n\t\t\t\t\tauto ps = crosspoint(c, ln);\n\t\t\t\t\tcrs.push_back(ps.first);\n\t\t\t\t\tcrs.push_back(ps.second);\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\n\t\tfoa(c, cs) {\n\t\t\tfoa(d, cs) {\n\t\t\t\tif(intersect(c, d) > 0) {\n\t\t\t\t\tauto ps = crosspoint(c, d);\n\t\t\t\t\tcrs.push_back(ps.first);\n\t\t\t\t\tcrs.push_back(ps.second);\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\n\t\tfoa(pt, crs) {\n\t\t\tif(isnan(pt.real())) continue;\n\t\t\t// if(radius < 1.) debug(radius, pt);\n\t\t\tif(safe(pt, radius)) {\n\t\t\t\tif(radius > 10000000000000.) debug(pt, radius);\n\t\t\t\treturn true;\n\t\t\t}\n\t\t}\n\t\treturn false;\n\t};\n\n\tR ok = 0.;\n\tR ng = inf;\n\trep(200) {\n\t\tR med = (ok + ng) / 2;\n\t\tif(f(med)) {\n\t\t\tok = med;\n\t\t} else {\n\t\t\tng = med;\n\t\t}\n\t}\n\treturn ok;\n}\n\nint main() {\n\tstd::ios::sync_with_stdio(false);\n\tstd::cin.tie(nullptr);\n\tstd::cout << std::fixed << std::setprecision(15);\n\tsrand((unsigned)time(NULL));\n\n\twhile(1) cout << solve() << dl;\n\n\treturn 0;\n}", "accuracy": 1, "time_ms": 5080, "memory_kb": 3980, "score_of_the_acc": -1.4846, "final_rank": 19 }, { "submission_id": "aoj_1342_5332100", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nconst double EPS = 0.0000001;\nstruct point{\n double x, y;\n point(){\n }\n point(double x, double y): x(x), y(y){\n }\n point operator +(point P){\n return point(x + P.x, y + P.y);\n }\n point operator -(point P){\n return point(x - P.x, y - P.y);\n }\n point operator *(double k){\n return point(x * k, y * k);\n }\n point operator /(double k){\n return point(x / k, y / k);\n }\n};\npoint rotate90(point P){\n return point(P.y, -P.x);\n}\ndouble abs(point P){\n return sqrt(P.x * P.x + P.y * P.y);\n}\ndouble dist(point P, point Q){\n return abs(Q - P);\n}\ndouble dot(point P, point Q){\n return P.x * Q.x + P.y * Q.y;\n}\ndouble cross(point P, point Q){\n return P.x * Q.y - P.y * Q.x;\n}\nstruct line{\n point A, B;\n line(){\n }\n line(point A, point B): A(A), B(B){\n }\n};\npoint vec(line L){\n return L.B - L.A;\n}\ndouble point_line_distance(point P, line L){\n return abs(cross(P - L.A, vec(L))) / abs(vec(L));\n}\npoint projection(point P, line L){\n return L.A + vec(L) / abs(vec(L)) * dot(P - L.A, vec(L)) / abs(vec(L));\n}\nstruct circle{\n point C;\n double r;\n circle(){\n }\n circle(point C, double r): C(C), r(r){\n }\n};\nbool point_in_circle(point P, circle C){\n return dist(P, C.C) < C.r - EPS;\n}\nbool line_circle_intersects(line L, circle C){\n return point_line_distance(C.C, L) < C.r - EPS;\n}\npair<point, point> line_circle_intersection(line L, circle C){\n point P = projection(C.C, L);\n double d = point_line_distance(C.C, L);\n double h = sqrt(C.r * C.r - d * d);\n point A = P + vec(L) / abs(vec(L)) * h;\n point B = P - vec(L) / abs(vec(L)) * h;\n return make_pair(A, B);\n}\nbool circle_intersects(circle C1, circle C2){\n double d = dist(C1.C, C2.C);\n return abs(C1.r - C2.r) + EPS < d && d < C1.r + C2.r - EPS;\n}\npair<point, point> circle_intersection(circle C1, circle C2){\n double d = dist(C1.C, C2.C);\n double d2 = (C1.r * C1.r + d * d - C2.r * C2.r) / (2 * d);\n point M = C1.C + (C2.C - C1.C) / d * d2;\n double h = sqrt(C1.r * C1.r - d2 * d2);\n point D = rotate90(C2.C - C1.C);\n point A = M + D / abs(D) * h;\n point B = M - D / abs(D) * h;\n return make_pair(A, B);\n}\nint main(){\n while (true){\n int n;\n double w;\n cin >> n >> w;\n if (n == 0 && w == 0){\n break;\n }\n vector<point> P(n);\n vector<double> h(n);\n for (int i = 0; i < n; i++){\n cin >> P[i].x >> P[i].y >> h[i];\n }\n double tv = 0, fv = 10000;\n for (int i = 0; i < 50; i++){\n double r = (tv + fv) / 2;\n double t;\n if (r < w){\n t = r;\n } else {\n t = sqrt(r * r - (r - w) * (r - w));\n }\n if (t > 50){\n fv = r;\n } else {\n vector<line> L(4);\n L[0] = line(point(t, t), point(t, 100 - t));\n L[1] = line(point(100 - t, t), point(100 - t, 100 - t));\n L[2] = line(point(t, t), point(100 - t, t));\n L[3] = line(point(t, 100 - t), point(100 - t, 100 - t));\n vector<circle> C(n);\n for (int j = 0; j < n; j++){\n if (r < h[j]){\n C[j] = circle(P[j], r);\n } else {\n C[j] = circle(P[j], sqrt(r * r - (r - h[j]) * (r - h[j])));\n }\n }\n vector<point> Q;\n Q.push_back(point(t, t));\n Q.push_back(point(t, 100 - t));\n Q.push_back(point(100 - t, t));\n Q.push_back(point(100 - t, 100 - t));\n for (int j = 0; j < n; j++){\n for (int k = 0; k < 4; k++){\n if (line_circle_intersects(L[k], C[j])){\n pair<point, point> I = line_circle_intersection(L[k], C[j]);\n Q.push_back(I.first);\n Q.push_back(I.second);\n }\n }\n for (int k = j + 1; k < n; k++){\n if (circle_intersects(C[j], C[k])){\n pair<point, point> I = circle_intersection(C[j], C[k]);\n Q.push_back(I.first);\n Q.push_back(I.second);\n }\n }\n }\n int cnt = Q.size();\n bool ok = false;\n for (int j = 0; j < cnt; j++){\n bool ok2 = true;\n if (Q[j].x < t - EPS || Q[j].x > 100 - t + EPS || Q[j].y < t - EPS || Q[j].y > 100 - t + EPS){\n ok2 = false;\n }\n for (int k = 0; k < n; k++){\n if (point_in_circle(Q[j], C[k])){\n ok2 = false;\n }\n }\n if (ok2){\n ok = true;\n }\n }\n if (ok){\n tv = r;\n } else {\n fv = r;\n }\n }\n }\n cout << tv << endl;\n }\n}", "accuracy": 1, "time_ms": 120, "memory_kb": 3252, "score_of_the_acc": -0.0181, "final_rank": 2 }, { "submission_id": "aoj_1342_5237482", "code_snippet": "#include \"iostream\"\n#include \"climits\"\n#include \"list\"\n#include \"queue\"\n#include \"stack\"\n#include \"set\"\n#include \"functional\"\n#include \"algorithm\"\n#include \"string\"\n#include \"map\"\n#include \"unordered_map\"\n#include \"unordered_set\"\n#include \"iomanip\"\n#include \"cmath\"\n#include \"random\"\n#include \"bitset\"\n#include \"cstdio\"\n#include \"numeric\"\n#include \"cassert\"\n#include \"ctime\"\n\nusing namespace std;\n\n//constexpr long long int MOD = 1000000007;\n//constexpr int MOD = 1000000007;\n//constexpr int MOD = 998244353;\nconstexpr long long int MOD = 998244353;\nconstexpr double EPS = 1e-8;\n\n//int N, M, K, T, H, W, L, R;\nlong long int N, M, K, T, H, W, L, R;\n\nstruct Needle {\n\tlong double x, y, z;\n\tNeedle() {\n\t\tcin >> x >> y >> z;\n\t}\n\tNeedle(long double xx, long double yy, long double zz) :x(xx), y(yy), z(zz) {\n\n\t}\n};\n\nstruct Point {\n\tlong double x, y, z;\n\tPoint(long double xx, long double yy, long double zz) :x(xx), y(yy), z(zz) {\n\n\t}\n};\n\nlong double Dist(Needle n, Point p) {\n\tn.z = min(n.z, p.z);\n\t//cout << hypot(hypot(n.x - p.x, n.y - p.y), n.z - p.z) << endl;\n\treturn hypot(hypot(n.x - p.x, n.y - p.y), n.z - p.z);\n}\n\nvector<Point>CircleCross(Needle a,long double ar,Needle b, long double br) {\n\tvector<Point>ret;\n\tPoint dp(b.x - a.x, b.y - a.y, a.z);\n\tauto dis = hypot(a.x - b.x, a.y - b.y);\n\tif (ar + br < dis)return ret;\n\tif (abs(ar - br) >= dis)return ret;\n\tif (abs(ar + br - dis) <= EPS) {\n\t\tret.push_back(Point(a.x + b.x * (ar / (ar + br)), a.y + b.y * (ar / (ar + br)), a.z));\n\t\treturn ret;\n\t}\n\tif (abs(abs(ar - br) - dis) <= EPS) {\n\t\tret.push_back(Point(a.x + b.x * (ar / (ar - br)), a.y + b.y * (ar / (ar - br)), a.z));\n\t\treturn ret;\n\t}\n\tlong double ad;\n\tif (dis < 1) {\n\t\tcout << dis << endl;\n\t}\n\tad = (ar*ar - br*br + dis * dis) / 2 / dis;\n\tPoint cp(a.x + dp.x*(ad / dis), a.y + dp.y*(ad / dis), a.z);\n\tif (abs(ar) < abs(ad)) {\n\t\tcout << ar << \" \" << br << \" \" << dis << endl;\n\t\tcout << ar << \" \" << ad << endl;\n\t}\n\tlong double amari = sqrt(ar*ar - ad * ad);\n\tret.push_back(Point(cp.x - dp.y*amari / dis, cp.y + dp.x*amari / dis, cp.z));\n\tret.push_back(Point(cp.x + dp.y*amari / dis, cp.y - dp.x*amari / dis, cp.z));\n\treturn ret;\n}\n\nint main() {\n\tios::sync_with_stdio(false);\n\tcin.tie(0);\n\n\twhile (cin >> N >> H, N) {\n\t\tvector<Needle>n(N);\n\t\tlong double mx = (H*H + (long double)2500) / (2 * H);\n\t\tif (H >= 50) {\n\t\t\tmx = 50;\n\t\t}\n\t\tlong double l = 0, r = mx;\n\t\tfor (int loop = 0; loop < 50; loop++) {\n\t\t\tlong double mid = (l + r) / 2;\n\t\t\tvector<Point>p;\n\t\t\t{\n\t\t\t\tlong double d = 0;\n\t\t\t\tif (mid <= H) {\n\t\t\t\t\td = mid;\n\t\t\t\t}\n\t\t\t\telse {\n\t\t\t\t\td = sqrt(mid*H * 2 - H * H);\n\t\t\t\t}\n\t\t\t\tp.push_back(Point(d, d, mid));\n\t\t\t\tp.push_back(Point(100 - d, d, mid));\n\t\t\t\tp.push_back(Point(d, 100 - d, mid));\n\t\t\t\tp.push_back(Point(100 - d, 100 - d, mid));\n\t\t\t\tfor (auto i : n) {\n\t\t\t\t\tlong double r = 0;\n\t\t\t\t\tif (mid <= i.z) {\n\t\t\t\t\t\tr = mid;\n\t\t\t\t\t}\n\t\t\t\t\telse {\n\t\t\t\t\t\tr = sqrt(mid*i.z * 2 - i.z*i.z);\n\t\t\t\t\t}\n\t\t\t//\t\tcout << mid << \" \" << d << \" \" << r << endl;\n\t\t\t\t\tif (i.x + r > d && i.x - r < d) {\n\t\t\t\t\t\tif (abs(r) < abs(i.x - d)) {\n\t\t\t\t\t\t\tcout << \"aaaaaaaaaaaaaaaaaaaaaaaa\" << endl;\n\t\t\t\t\t\t}\n\t\t\t\t\t\tlong double dif = sqrt(r*r - (i.x - d)*(i.x - d));\n\t\t\t\t\t\tp.push_back(Point(d, i.y - dif, mid));\n\t\t\t\t\t\tp.push_back(Point(d, i.y + dif, mid));\n\t\t\t\t\t}\n\t\t\t\t\tif (i.x + r > 100 - d && i.x - r < 100 - d) {\n\t\t\t\t\t\tif (abs(r) < abs(i.x - (100 - d))) {\n\t\t\t\t\t\t\tcout << \"aaaaaaaaaaaaaaaaaaaaaaaa\" << endl;\n\t\t\t\t\t\t}\n\t\t\t\t\t\tlong double dif = sqrt(r*r - (i.x - (100 - d))*(i.x - (100 - d)));\n\t\t\t\t\t\tp.push_back(Point(100 - d, i.y - dif, mid));\n\t\t\t\t\t\tp.push_back(Point(100 - d, i.y + dif, mid));\n\t\t\t\t\t}\n\t\t\t\t\tif (i.y + r > d && i.y - r < d) {\n\t\t\t\t\t\tif (abs(r) < abs(i.y-d)) {\n\t\t\t\t\t\t\tcout << \"aaaaaaaaaaaaaaaaaaaaaaaa\" << endl;\n\t\t\t\t\t\t}\n\t\t\t\t\t\tlong double dif = sqrt(r*r - (i.y - d)*(i.y - d));\n\t\t\t\t\t\tp.push_back(Point(i.x - dif, d, mid));\n\t\t\t\t\t\tp.push_back(Point(i.x + dif, d, mid));\n\t\t\t\t\t}\n\t\t\t\t\tif (i.y + r > 100 - d && i.y - r < 100 - d) {\n\t\t\t\t\t\tif (abs(r) < abs(i.y - (100 - d))) {\n\t\t\t\t\t\t\tcout << \"aaaaaaaaaaaaaaaaaaaaaaaa\" << endl;\n\t\t\t\t\t\t}\n\t\t\t\t\t\tlong double dif = sqrt(r*r - (i.y - (100 - d))*(i.y - (100 - d)));\n\t\t\t\t\t\tp.push_back(Point(i.x - dif, 100 - d, mid));\n\t\t\t\t\t\tp.push_back(Point(i.x + dif, 100 - d, mid));\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t\t{\n\t\t\t\tfor (int i = 0; i < N; i++) {\n\t\t\t\t\tfor (int j = i + 1; j < N; j++) {\n\t\t\t\t\t\tauto box = CircleCross(n[i], sqrt(mid*mid - max((long double)0, (mid - n[i].z))*max((long double)0,(mid - n[i].z))), n[j], sqrt(mid*mid - max((long double)0, (mid - n[j].z))*max((long double)0, (mid - n[j].z))));\n\t\t\t\t\t\tfor (auto k : box) {\n\t\t\t\t\t\t\tk.z = mid;\n\t\t\t\t\t\t\tp.push_back(k);\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t\tlong double flag = 0;\n\t\t\tfor (auto i : p) {\n\t\t\t//\tcout << i.x << \" \" << i.y << \" \" << i.z << \" \" << mid << endl;\n\t\t\t\tif (i.x < 0 || i.y < 0 || i.x >= 100 || i.y >= 100)continue;\n\t\t\t\tlong double dis = 20000;\n\t\t\t\tn.push_back(Needle(i.x, 0, H));\n\t\t\t\tn.push_back(Needle(i.x, 100, H));\n\t\t\t\tn.push_back(Needle(0, i.y, H));\n\t\t\t\tn.push_back(Needle(100, i.y, H));\n\t\t\t\tfor (auto j : n) {\n\t\t\t\t\tdis = min(dis, Dist(j, i));\n\t\t\t\t}\n\t\t\t\tn.pop_back();\n\t\t\t\tn.pop_back();\n\t\t\t\tn.pop_back();\n\t\t\t\tn.pop_back();\n\t\t\t\tflag = max(flag, dis);\n\t\t\t\t//cout << dis << \" \" << mid << endl;\n\t\t\t\t//if (dis >= mid - EPS) {\n\t\t\t\t//\tcout << i.x << \" \" << i.y << \" \" << i.z << endl;\n\t\t\t\t//}\n\t\t\t}\n\t\t//\tcout << flag << \" \" << mid << endl;\n\t\t\tif (flag>=mid-EPS) {\n\t\t\t\tl = mid;\n\t\t\t}\n\t\t\telse {\n\t\t\t\tr = mid;\n\t\t\t}\n\t\t}\n\t\tcout << fixed << setprecision(20) << r << endl;\n\t}\n}", "accuracy": 1, "time_ms": 1550, "memory_kb": 3436, "score_of_the_acc": -0.4115, "final_rank": 5 }, { "submission_id": "aoj_1342_5179238", "code_snippet": "#pragma GCC optimize(\"Ofast\")\n//#ifndef ONLINE_JUDGE \n//#define _GLIBCXX_DEBUG\n//#endif\n#ifdef ONLINE_JUDGE \n#include <atcoder/all>\n#endif\n#include <bits/stdc++.h>\n#include <chrono>\n#include <random>\n#include <math.h>\n#include <complex>\n \n \nusing namespace std;\n#ifdef ONLINE_JUDGE \nusing namespace atcoder;\n#endif\n#define rep(i,n) for (int i = 0;i < (int)(n);i++)\nusing ll = long long;\n#ifdef ONLINE_JUDGE \n//using mint = modint998244353;\n//using mint = modint;\nusing mint = modint1000000007;\n#endif\nconst ll MOD=1000000007;\n//const ll MOD=998244353;\nconst long long INF = 1LL << 60;\nconst double pi=acos(-1.0);\nint dx[9] = {1, 0, -1, 0, 1, 1, -1, -1, 0};\nint dy[9] = {0, 1, 0, -1, 1, -1, -1, 1, 0};\n \ntemplate<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; }\ntemplate<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; }\n \nusing Real = long double; //時間がかかる割にdoubleと大差ない\nusing Point = complex<Real>;\nconst Real EPS = 1e-10, /*1e-8はたまに落ちる*/ PI = acos(-1);\n\ninline bool eq(Real a, Real b) { return fabs(b - a) < EPS; } //誤差無視の等式\n\n//実スカラー倍\nPoint operator*(const Point &p, const Real &d) { return Point(real(p) * d, imag(p) * d); }\n\n// point入力受け取り\nistream &operator>>(istream &is, Point &p) {\n\tReal a, b;\n\tis >> a >> b;\n\tp = Point(a, b);\n\treturn is;\n}\n// point出力\nostream &operator<<(ostream &os, Point &p) { return os << fixed << setprecision(10) << p.real() << \" \" << p.imag(); }\n\n// 点 p を反時計回りに theta 回転\nPoint rotate(Real theta, const Point &p) {\n\treturn Point(cos(theta) * p.real() - sin(theta) * p.imag(), sin(theta) * p.real() + cos(theta) * p.imag());\n}\n\nReal radian_to_degree(Real r) { return (r * 180.0 / PI); }\n\nReal degree_to_radian(Real d) { return (d * PI / 180.0); }\n\n// a-b-c の角度のうち小さい方を返す\nReal get_angle(const Point &a, const Point &b, const Point &c) {\n\tconst Point v(a - b), /* もとはv(b-a)になってたので修正 */ w(c - b);\n\tReal alpha = atan2(v.imag(), v.real()), beta = atan2(w.imag(), w.real());\n\tif(alpha > beta) swap(alpha, beta);\n\tReal theta = (beta - alpha);\n\treturn min(theta, 2 * acos(-1) - theta);\n}\n\nnamespace std {\nbool operator<(const Point &a, const Point &b) { return a.real() != b.real() ? a.real() < b.real() : a.imag() < b.imag(); }\n} // namespace std\n\nstruct Line {\n\tPoint a, b;\n\n\tLine() = default;\n\n\tLine(Point a, Point b) : a(a), b(b) {}\n\n\tLine(Real A, Real B, Real C) // Ax + By = C\n\t{\n\t\tif(eq(A, 0))\n\t\t\ta = Point(0, C / B), b = Point(1, C / B);\n\t\telse if(eq(B, 0))\n\t\t\tb = Point(C / A, 0), b = Point(C / A, 1);\n\t\telse\n\t\t\ta = Point(0, C / B), b = Point(C / A, 0);\n\t}\n\n\tfriend ostream &operator<<(ostream &os, Line &p) { return os << p.a << \" to \" << p.b; }\n\n\tfriend istream &operator>>(istream &is, Line &a) { return is >> a.a >> a.b; }\n};\n\nstruct Segment : Line {\n\tSegment() = default;\n\n\tSegment(Point a, Point b) : Line(a, b) {}\n};\n\nstruct Circle {\n\tPoint p;\n\tReal r;\n\n\tCircle() = default;\n\n\tCircle(Point p, Real r) : p(p), r(r) {}\n};\n\nusing Points = vector<Point>;\nusing Polygon = vector<Point>;\nusing Segments = vector<Segment>;\nusing Lines = vector<Line>;\nusing Circles = vector<Circle>;\n\n//外積\nReal cross(const Point &a, const Point &b) { return real(a) * imag(b) - imag(a) * real(b); }\n//内積\nReal dot(const Point &a, const Point &b) { return real(a) * real(b) + imag(a) * imag(b); }\n\n// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_1_C\n// 点の回転方向\nint ccw(const Point &a, Point b, Point c) {\n\tb = b - a, c = c - a;\n\tif(cross(b, c) > EPS) return +1; // \"COUNTER_CLOCKWISE\"\n\tif(cross(b, c) < -EPS) return -1; // \"CLOCKWISE\"\n\tif(dot(b, c) < 0) return +2;\t // \"ONLINE_BACK\"\n\tif(norm(b) < norm(c)) return -2; // \"ONLINE_FRONT\"\n\treturn 0;\t\t\t\t\t\t // \"ON_SEGMENT\"\n}\n\n// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_2_A\n// 平行判定\nbool parallel(const Line &a, const Line &b) { return eq(cross(a.b - a.a, b.b - b.a), 0.0); }\n\n// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_2_A\n// 垂直判定\nbool orthogonal(const Line &a, const Line &b) { return eq(dot(a.a - a.b, b.a - b.b), 0.0); }\n\n// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_1_A\n// 射影\n// 直線 l に p から垂線を引いた交点を求める\nPoint projection(const Line &l, const Point &p) {\n\tdouble t = dot(p - l.a, l.a - l.b) / norm(l.a - l.b);\n\treturn l.a + (l.a - l.b) * t;\n}\n\nPoint projection(const Segment &l, const Point &p) {\n\tdouble t = dot(p - l.a, l.a - l.b) / norm(l.a - l.b);\n\treturn l.a + (l.a - l.b) * t;\n}\n\n// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_1_B\n// 反射\n// 直線 l を対称軸として点 p と線対称にある点を求める\nPoint reflection(const Line &l, const Point &p) { return p + (projection(l, p) - p) * 2.0; }\n\nbool intersect(const Line &l, const Point &p) { return abs(ccw(l.a, l.b, p)) != 1; }\n\nbool intersect(const Line &l, const Line &m) { return abs(cross(l.b - l.a, m.b - m.a)) > EPS || abs(cross(l.b - l.a, m.b - l.a)) < EPS; }\n\nbool intersect(const Segment &s, const Point &p) { return ccw(s.a, s.b, p) == 0; }\n\nbool intersect(const Line &l, const Segment &s) { return cross(l.b - l.a, s.a - l.a) * cross(l.b - l.a, s.b - l.a) < EPS; }\n\nReal distance(const Line &l, const Point &p);\n\nbool intersect(const Circle &c, const Line &l) { return distance(l, c.p) <= c.r + EPS; }\n\nbool intersect(const Circle &c, const Point &p) { return abs(abs(p - c.p) - c.r) < EPS; }\n\n// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_2_B\n// 線分同士の交差判定 (端点が他方の線分上にあるならば交差とみなす)\nbool intersect(const Segment &s, const Segment &t) {\n\treturn 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;\n}\n\nint intersect(const Circle &c, const Segment &l) {\n\tif(norm(projection(l, c.p) - c.p) - c.r * c.r > EPS) return 0;\n\tauto d1 = abs(c.p - l.a), d2 = abs(c.p - l.b);\n\tif(d1 < c.r + EPS && d2 < c.r + EPS) return 0;\n\tif(d1 < c.r - EPS && d2 > c.r + EPS || d1 > c.r + EPS && d2 < c.r - EPS) return 1;\n\tconst Point h = projection(l, c.p);\n\tif(dot(l.a - h, l.b - h) < 0) return 2;\n\treturn 0;\n}\n\n// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_7_A&lang=jp\nint intersect(Circle c1, Circle c2) {\n\tif(c1.r < c2.r) swap(c1, c2);\n\tReal d = abs(c1.p - c2.p);\n\tif(c1.r + c2.r < d) return 4;\t // 外に離れている\n\tif(eq(c1.r + c2.r, d)) return 3; // 外接\n\tif(c1.r - c2.r < d) return 2;\t // 交差\n\tif(eq(c1.r - c2.r, d)) return 1; // 内接\n\treturn 0;\t\t\t\t\t\t // 内包\n}\n\nReal distance(const Point &a, const Point &b) { return abs(a - b); }\n\nReal distance(const Line &l, const Point &p) { return abs(p - projection(l, p)); }\n\nReal distance(const Line &l, const Line &m) { return intersect(l, m) ? 0 : distance(l, m.a); }\n\nReal distance(const Segment &s, const Point &p) {\n\tPoint r = projection(s, p);\n\tif(intersect(s, r)) return abs(r - p);\n\treturn min(abs(s.a - p), abs(s.b - p));\n}\n\n// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_2_D\nReal distance(const Segment &a, const Segment &b) {\n\tif(intersect(a, b)) return 0;\n\treturn min({distance(a, b.a), distance(a, b.b), distance(b, a.a), distance(b, a.b)});\n}\n\nReal distance(const Line &l, const Segment &s) {\n\tif(intersect(l, s)) return 0;\n\treturn min(distance(l, s.a), distance(l, s.b));\n}\n\nPoint crosspoint(const Line &l, const Line &m) {\n\tReal A = cross(l.b - l.a, m.b - m.a);\n\tReal B = cross(l.b - l.a, l.b - m.a);\n\tif(eq(abs(A), 0.0) && eq(abs(B), 0.0)) return m.a;\n\treturn m.a + (m.b - m.a) * B / A;\n}\n\n// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_2_C\nPoint crosspoint(const Segment &l, const Segment &m) { return crosspoint(Line(l), Line(m)); }\n\n// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_7_D\npair<Point, Point> crosspoint(const Circle &c, const Line l) {\n\tPoint pr = projection(l, c.p);\n\tPoint e = (l.b - l.a) / abs(l.b - l.a);\n\tif(eq(distance(l, c.p), c.r)) return {pr, pr};\n\tdouble base = sqrt(c.r * c.r - norm(pr - c.p));\n\treturn {pr - e * base, pr + e * base};\n}\n\npair<Point, Point> crosspoint(const Circle &c, const Segment &l) {\n\tLine aa = Line(l.a, l.b);\n\tif(intersect(c, l) == 2) return crosspoint(c, aa);\n\tauto ret = crosspoint(c, aa);\n\tif(dot(l.a - ret.first, l.b - ret.first) < 0)\n\t\tret.second = ret.first;\n\telse\n\t\tret.first = ret.second;\n\treturn ret;\n}\n\n// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_7_E\npair<Point, Point> crosspoint(const Circle &c1, const Circle &c2) {\n\tReal d = abs(c1.p - c2.p);\n\tReal a = acos((c1.r * c1.r + d * d - c2.r * c2.r) / (2 * c1.r * d)); // 余弦定理\n\tReal t = atan2(c2.p.imag() - c1.p.imag(), c2.p.real() - c1.p.real());\n\tPoint p1 = c1.p + Point(cos(t + a) * c1.r, sin(t + a) * c1.r);\n\tPoint p2 = c1.p + Point(cos(t - a) * c1.r, sin(t - a) * c1.r);\n\treturn {p1, p2};\n}\n\n// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_7_F\n// \"点 p を通る円 c の接線\"の接点2つ\npair<Point, Point> tangent(const Circle &c1, const Point &p2) { return crosspoint(c1, Circle(p2, sqrt(norm(c1.p - p2) - c1.r * c1.r))); }\n\n// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_7_G\n// 円 c1, c2 の共通接線\nLines tangent(Circle c1, Circle c2) {\n\tLines ret;\n\tif(c1.r < c2.r) swap(c1, c2);\n\tReal g = norm(c1.p - c2.p);\n\tif(eq(g, 0)) return ret;\n\tPoint u = (c2.p - c1.p) / sqrt(g);\n\tPoint v = rotate(PI * 0.5, u);\n\tfor(int s : {-1, 1}) {\n\t\tReal h = (c1.r + s * c2.r) / sqrt(g);\n\t\tif(eq(1 - h * h, 0)) {\n\t\t\tret.emplace_back(c1.p + u * c1.r, c1.p + (u + v) * c1.r);\n\t\t} else if(1 - h * h > 0) {\n\t\t\tPoint uu = u * h, vv = v * sqrt(1 - h * h);\n\t\t\tret.emplace_back(c1.p + (uu + vv) * c1.r, c2.p - (uu + vv) * c2.r * s);\n\t\t\tret.emplace_back(c1.p + (uu - vv) * c1.r, c2.p - (uu - vv) * c2.r * s);\n\t\t}\n\t}\n\treturn ret;\n}\n\n// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_3_B\n// 凸性判定 (反時計回り)\nbool is_convex(const Polygon &p) {\n\tint n = (int)p.size();\n\tfor(int i = 0; i < n; i++) {\n\t\tif(ccw(p[(i + n - 1) % n], p[i], p[(i + 1) % n]) == -1) return false;\n\t}\n\treturn true;\n}\n\n// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_4_A\n// 凸包\nPolygon convex_hull(Polygon &p) {\n\tint n = (int)p.size(), k = 0;\n\tif(n <= 2) return p;\n\tsort(p.begin(), p.end()); // x -> yの順にソート\n\tvector<Point> ch(2 * n);\n\tfor(int i = 0; i < n; ch[k++] = p[i++]) { //下半分を構成\n\t\twhile(k >= 2 && cross(ch[k - 1] - ch[k - 2], p[i] - ch[k - 1]) < EPS) --k;\n\t}\n\tfor(int i = n - 2, t = k + 1; i >= 0; ch[k++] = p[i--]) { //上半分を構成\n\t\twhile(k >= t && cross(ch[k - 1] - ch[k - 2], p[i] - ch[k - 1]) < EPS) --k;\n\t}\n\tch.resize(k - 1);\n\treturn ch;\n}\n\n// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_3_C\n// 多角形と点の包含判定\nenum { OUT, ON, IN };\n\nint contains(const Polygon &Q, const Point &p) {\n\tbool in = false;\n\tfor(int i = 0; i < Q.size(); i++) {\n\t\tPoint a = Q[i] - p, b = Q[(i + 1) % Q.size()] - p;\n\t\tif(a.imag() > b.imag()) swap(a, b);\n\t\tif(a.imag() <= 0 && 0 < b.imag() && cross(a, b) < 0) in = !in;\n\t\tif(cross(a, b) == 0 && dot(a, b) <= 0) return ON;\n\t}\n\treturn in ? IN : OUT;\n}\n\n// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=1033\n// 線分の重複除去\nvoid merge_segments(vector<Segment> &segs) {\n\tauto merge_if_able = [](Segment &s1, const Segment &s2) {\n\t\tif(abs(cross(s1.b - s1.a, s2.b - s2.a)) > EPS) return false;\n\t\tif(ccw(s1.a, s2.a, s1.b) == 1 || ccw(s1.a, s2.a, s1.b) == -1) return false;\n\t\tif(ccw(s1.a, s1.b, s2.a) == -2 || ccw(s2.a, s2.b, s1.a) == -2) return false;\n\t\ts1 = Segment(min(s1.a, s2.a), max(s1.b, s2.b));\n\t\treturn true;\n\t};\n\n\tfor(int i = 0; i < segs.size(); i++) {\n\t\tif(segs[i].b < segs[i].a) swap(segs[i].a, segs[i].b);\n\t}\n\tfor(int i = 0; i < segs.size(); i++) {\n\t\tfor(int j = i + 1; j < segs.size(); j++) {\n\t\t\tif(merge_if_able(segs[i], segs[j])) {\n\t\t\t\tsegs[j--] = segs.back(), segs.pop_back();\n\t\t\t}\n\t\t}\n\t}\n}\n\n// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=1033\n// 線分アレンジメント\n// 任意の2線分の交点を頂点としたグラフを構築する\nvector<vector<int>> segment_arrangement(vector<Segment> &segs, vector<Point> &ps) {\n\tvector<vector<int>> g;\n\tint N = (int)segs.size();\n\tfor(int i = 0; i < N; i++) {\n\t\tps.emplace_back(segs[i].a);\n\t\tps.emplace_back(segs[i].b);\n\t\tfor(int j = i + 1; j < N; j++) {\n\t\t\tconst Point p1 = segs[i].b - segs[i].a;\n\t\t\tconst Point p2 = segs[j].b - segs[j].a;\n\t\t\tif(eq(cross(p1, p2), 0)) continue;\t// \" == 0\" となっていたのを訂正\n\t\t\tif(intersect(segs[i], segs[j])) {\n\t\t\t\tps.emplace_back(crosspoint(segs[i], segs[j]));\n\t\t\t}\n\t\t}\n\t}\n\tsort(begin(ps), end(ps));\n\tps.erase(unique(begin(ps), end(ps)), end(ps));\t// これ誤差とか大丈夫なのか?\n\n\tint M = (int)ps.size();\n\tg.resize(M);\n\tfor(int i = 0; i < N; i++) {\n\t\tvector<int> vec;\n\t\tfor(int j = 0; j < M; j++) {\n\t\t\tif(intersect(segs[i], ps[j])) {\n\t\t\t\tvec.emplace_back(j);\n\t\t\t}\n\t\t}\n\t\tfor(int j = 1; j < vec.size(); j++) {\n\t\t\tg[vec[j - 1]].push_back(vec[j]);\n\t\t\tg[vec[j]].push_back(vec[j - 1]);\n\t\t}\n\t}\n\treturn (g);\n}\n\n// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_4_C\n// 凸多角形の切断\n// 直線 l.a-l.b で切断しその左側にできる凸多角形を返す\nPolygon convex_cut(const Polygon &U, Line l) {\n\tPolygon ret;\n\tfor(int i = 0; i < U.size(); i++) {\n\t\tPoint now = U[i], nxt = U[(i + 1) % U.size()];\n\t\tif(ccw(l.a, l.b, now) != -1) ret.push_back(now);\n\t\tif(ccw(l.a, l.b, now) * ccw(l.a, l.b, nxt) < 0) {\n\t\t\tret.push_back(crosspoint(Line(now, nxt), l));\n\t\t}\n\t}\n\treturn (ret);\n}\n\n// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_3_A\n// 多角形の面積\nReal area(const Polygon &p) {\n\tReal A = 0;\n\tfor(int i = 0; i < p.size(); ++i) {\n\t\tA += cross(p[i], p[(i + 1) % p.size()]);\n\t}\n\treturn A * 0.5;\n}\n\n// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_7_H\n// 円と多角形の共通部分の面積\nReal area(const Polygon &p, const Circle &c) {\n\tif(p.size() < 3) return 0.0;\n\tfunction<Real(Circle, Point, Point)> cross_area = [&](const Circle &c, const Point &a, const Point &b) {\n\t\tPoint va = c.p - a, vb = c.p - b;\n\t\tReal f = cross(va, vb), ret = 0.0;\n\t\tif(eq(f, 0.0)) return ret;\n\t\tif(max(abs(va), abs(vb)) < c.r + EPS) return f;\n\t\tif(distance(Segment(a, b), c.p) > c.r - EPS) return c.r * c.r * arg(vb * conj(va));\t // 0.5倍してなくない?\n\t\tauto u = crosspoint(c, Segment(a, b));\n\t\tvector<Point> tot{a, u.first, u.second, b};\n\t\tfor(int i = 0; i + 1 < tot.size(); i++) {\n\t\t\tret += cross_area(c, tot[i], tot[i + 1]);\n\t\t}\n\t\treturn ret;\n\t};\n\tReal A = 0;\n\tfor(int i = 0; i < p.size(); i++) {\n\t\tA += cross_area(c, p[i], p[(i + 1) % p.size()]);\n\t}\n\treturn A * 0.5;\t // 0.5倍が抜けていたので修正;\n}\n\n// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_4_B\n// 凸多角形の直径(最遠頂点対間距離)\nReal convex_diameter(const Polygon &p) {\n\tint N = (int)p.size();\n\tint is = 0, js = 0;\n\tfor(int i = 1; i < N; i++) {\n\t\tif(p[i].imag() > p[is].imag()) is = i;\n\t\tif(p[i].imag() < p[js].imag()) js = i;\n\t}\n\tReal maxdis = norm(p[is] - p[js]);\n\n\tint maxi, maxj, i, j;\n\ti = maxi = is;\n\tj = maxj = js;\n\tdo {\n\t\tif(cross(p[(i + 1) % N] - p[i], p[(j + 1) % N] - p[j]) >= 0) {\n\t\t\tj = (j + 1) % N;\n\t\t} else {\n\t\t\ti = (i + 1) % N;\n\t\t}\n\t\tif(norm(p[i] - p[j]) > maxdis) {\n\t\t\tmaxdis = norm(p[i] - p[j]);\n\t\t\tmaxi = i;\n\t\t\tmaxj = j;\n\t\t}\n\t} while(i != is || j != js);\n\treturn sqrt(maxdis);\n}\n\n// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_5_A\n// 最近点対\nReal closest_pair(Points ps) {\n\tif(ps.size() <= 1) throw(0);\n\tsort(begin(ps), end(ps)); // xでソート\n\n\tauto compare_y = [&](const Point &a, const Point &b) { return imag(a) < imag(b); };\n\tvector<Point> beet(ps.size());\n\tconst Real INF = 1e18;\n\n\tfunction<Real(int, int)> rec = [&](int left, int right) {\n\t\tif(right - left <= 1) return INF;\n\t\tint mid = (left + right) >> 1;\n\t\tauto x = real(ps[mid]);\t // 領域内でx座標真中の点\n\t\tauto ret = min(rec(left, mid), rec(mid, right));\n\t\t// y座標でソートされた2つの区間をマージ\n\t\tinplace_merge(begin(ps) + left, begin(ps) + mid, begin(ps) + right, compare_y);\n\t\tint ptr = 0;\n\t\tfor(int i = left; i < right; i++) {\n\t\t\tif(abs(real(ps[i]) - x) >= ret) continue;\n\t\t\tfor(int j = 0; j < ptr; j++) {\n\t\t\t\tauto luz = ps[i] - beet[ptr - j - 1];\n\t\t\t\tif(imag(luz) >= ret) break;\n\t\t\t\tret = min(ret, abs(luz));\n\t\t\t}\n\t\t\tbeet[ptr++] = ps[i];\n\t\t}\n\t\treturn ret;\n\t};\n\treturn rec(0, (int)ps.size());\n}\n\n// (aからの距離):(bからの距離) = dist_a : dist_b となる点の集合(円)を返す\nCircle Apollonius_Circle(Point a, Point b, Real dist_a, Real dist_b) {\n\tassert(!eq(dist_a, dist_b)); //同じ場合はbisectionで\n\tReal asq = dist_a * dist_a, bsq = dist_b * dist_b;\n\tPoint p = (a * bsq - b * asq) * (1 / (bsq - asq));\n\tReal r = abs(a - b) * dist_a * dist_b / abs(asq - bsq);\n\treturn Circle(p, r);\n}\n\n// 二等分線\nLine bisection(Point a, Point b) {\n\ta = (a + b) * 0.5;\n\tb = rotate(PI / 2, b - a) + a;\n\treturn Line(a, b);\n}\n\n \nint main()\n{\n ios::sync_with_stdio(false);\n cin.tie(nullptr);\n cout << fixed << setprecision(15);\n \n while(1) {\n ll N,W; cin>>N>>W; if(!N&&!W) break;\n \n Points P(N);\n vector<Real> H(N);\n rep(i,N) cin>>P[i]>>H[i];\n \n vector<Real> R(N);\n Real l,r,u,d;\n \n auto judge=[&](Real sphere){\n rep(i,N) {\n if(H[i]>sphere) R[i]=sphere;\n else R[i]=sqrt(pow(sphere,2)-pow(sphere-H[i],2));\n }\n auto hasi=(sphere>W?sqrt(pow(sphere,2)-pow(sphere-W,2)):sphere);\n l=hasi,r=100.0-hasi,u=hasi,d=100.0-hasi;\n \n auto safe=[&](Real x,Real y) {\n if(x<l-EPS||x>r+EPS||y<u-EPS||y>d+EPS) return false;\n rep(i,N) {\n if(distance(P[i],Point(x,y))<R[i]-EPS) return false;\n }\n return true;\n };\n \n auto safepoint=[&](Point p) {return safe(p.real(),p.imag());};\n \n if(safe(l,u)||safe(l,d)||safe(r,u)||safe(r,d)) return true;\n \n rep(i,N) for(int j=i+1;j<N;j++) {\n int num=intersect(Circle(P[i],R[i]),Circle(P[j],R[j]));\n if(num==0||num==4) continue;\n auto [p1,p2]=crosspoint(Circle(P[i],R[i]),Circle(P[j],R[j]));\n if(safepoint(p1)||safepoint(p2)) return true;\n }\n \n Points points(4);\n points[0]=Point(l,u);\n points[1]=Point(l,d);\n points[2]=Point(r,u);\n points[3]=Point(r,d);\n \n Lines lines(4);\n lines[0]=Line(points[0],points[1]);\n lines[1]=Line(points[1],points[2]);\n lines[2]=Line(points[2],points[3]);\n lines[3]=Line(points[3],points[0]);\n \n rep(i,N) rep(j,4) {\n if(!intersect(Circle(P[i],R[i]),lines[j])) continue;\n auto [p1,p2]=crosspoint(Circle(P[i],R[i]),lines[j]);\n if(safepoint(p1)||safepoint(p2)) return true;\n }\n \n return false;\n };\n \n Real ok=0.0;\n Real ng=10000.0;\n rep(_,100) {\n Real mid=(ok+ng)/2;\n (judge(mid)?ok:ng)=mid;\n }\n \n cout<<ok<<'\\n';\n }\n\n return 0;\n}", "accuracy": 1, "time_ms": 300, "memory_kb": 3568, "score_of_the_acc": -0.4004, "final_rank": 4 }, { "submission_id": "aoj_1342_5033646", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\n#pragma region geometry\n\nusing Real = long double; //時間がかかる割にdoubleと大差ない\nusing Point = complex<Real>;\nconst Real EPS = 1e-10, /*1e-8はたまに落ちる*/ PI = acos(-1);\n\ninline bool eq(Real a, Real b) { return fabs(b - a) < EPS; } //誤差無視の等式\n\n//実スカラー倍\nPoint operator*(const Point &p, const Real &d) { return Point(real(p) * d, imag(p) * d); }\n\n// point入力受け取り\nistream &operator>>(istream &is, Point &p) {\n\tReal a, b;\n\tis >> a >> b;\n\tp = Point(a, b);\n\treturn is;\n}\n// point出力\nostream &operator<<(ostream &os, Point &p) { return os << fixed << setprecision(10) << p.real() << \" \" << p.imag(); }\n\n// 点 p を反時計回りに theta 回転\nPoint rotate(Real theta, const Point &p) {\n\treturn Point(cos(theta) * p.real() - sin(theta) * p.imag(), sin(theta) * p.real() + cos(theta) * p.imag());\n}\n\nReal radian_to_degree(Real r) { return (r * 180.0 / PI); }\n\nReal degree_to_radian(Real d) { return (d * PI / 180.0); }\n\n// a-b-c の角度のうち小さい方を返す\nReal get_angle(const Point &a, const Point &b, const Point &c) {\n\tconst Point v(a - b), /* もとはv(b-a)になってたので修正 */ w(c - b);\n\tReal alpha = atan2(v.imag(), v.real()), beta = atan2(w.imag(), w.real());\n\tif(alpha > beta) swap(alpha, beta);\n\tReal theta = (beta - alpha);\n\treturn min(theta, 2 * acos(-1) - theta);\n}\n\nnamespace std {\nbool operator<(const Point &a, const Point &b) { return a.real() != b.real() ? a.real() < b.real() : a.imag() < b.imag(); }\n} // namespace std\n\nstruct Line {\n\tPoint a, b;\n\n\tLine() = default;\n\n\tLine(Point a, Point b) : a(a), b(b) {}\n\n\tLine(Real A, Real B, Real C) // Ax + By = C\n\t{\n\t\tif(eq(A, 0))\n\t\t\ta = Point(0, C / B), b = Point(1, C / B);\n\t\telse if(eq(B, 0))\n\t\t\tb = Point(C / A, 0), b = Point(C / A, 1);\n\t\telse\n\t\t\ta = Point(0, C / B), b = Point(C / A, 0);\n\t}\n\n\tfriend ostream &operator<<(ostream &os, Line &p) { return os << p.a << \" to \" << p.b; }\n\n\tfriend istream &operator>>(istream &is, Line &a) { return is >> a.a >> a.b; }\n};\n\nstruct Segment : Line {\n\tSegment() = default;\n\n\tSegment(Point a, Point b) : Line(a, b) {}\n};\n\nstruct Circle {\n\tPoint p;\n\tReal r;\n\n\tCircle() = default;\n\n\tCircle(Point p, Real r) : p(p), r(r) {}\n};\n\nusing Points = vector<Point>;\nusing Polygon = vector<Point>;\nusing Segments = vector<Segment>;\nusing Lines = vector<Line>;\nusing Circles = vector<Circle>;\n\n//外積\nReal cross(const Point &a, const Point &b) { return real(a) * imag(b) - imag(a) * real(b); }\n//内積\nReal dot(const Point &a, const Point &b) { return real(a) * real(b) + imag(a) * imag(b); }\n\n// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_1_C\n// 点の回転方向\nint ccw(const Point &a, Point b, Point c) {\n\tb = b - a, c = c - a;\n\tif(cross(b, c) > EPS) return +1; // \"COUNTER_CLOCKWISE\"\n\tif(cross(b, c) < -EPS) return -1; // \"CLOCKWISE\"\n\tif(dot(b, c) < 0) return +2;\t // \"ONLINE_BACK\"\n\tif(norm(b) < norm(c)) return -2; // \"ONLINE_FRONT\"\n\treturn 0;\t\t\t\t\t\t // \"ON_SEGMENT\"\n}\n\n// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_2_A\n// 平行判定\nbool parallel(const Line &a, const Line &b) { return eq(cross(a.b - a.a, b.b - b.a), 0.0); }\n\n// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_2_A\n// 垂直判定\nbool orthogonal(const Line &a, const Line &b) { return eq(dot(a.a - a.b, b.a - b.b), 0.0); }\n\n// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_1_A\n// 射影\n// 直線 l に p から垂線を引いた交点を求める\nPoint projection(const Line &l, const Point &p) {\n\tdouble t = dot(p - l.a, l.a - l.b) / norm(l.a - l.b);\n\treturn l.a + (l.a - l.b) * t;\n}\n\nPoint projection(const Segment &l, const Point &p) {\n\tdouble t = dot(p - l.a, l.a - l.b) / norm(l.a - l.b);\n\treturn l.a + (l.a - l.b) * t;\n}\n\n// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_1_B\n// 反射\n// 直線 l を対称軸として点 p と線対称にある点を求める\nPoint reflection(const Line &l, const Point &p) { return p + (projection(l, p) - p) * 2.0; }\n\nbool intersect(const Line &l, const Point &p) { return abs(ccw(l.a, l.b, p)) != 1; }\n\nbool intersect(const Line &l, const Line &m) { return abs(cross(l.b - l.a, m.b - m.a)) > EPS || abs(cross(l.b - l.a, m.b - l.a)) < EPS; }\n\nbool intersect(const Segment &s, const Point &p) { return ccw(s.a, s.b, p) == 0; }\n\nbool intersect(const Line &l, const Segment &s) { return cross(l.b - l.a, s.a - l.a) * cross(l.b - l.a, s.b - l.a) < EPS; }\n\nReal distance(const Line &l, const Point &p);\n\nbool intersect(const Circle &c, const Line &l) { return distance(l, c.p) <= c.r + EPS; }\n\nbool intersect(const Circle &c, const Point &p) { return abs(abs(p - c.p) - c.r) < EPS; }\n\n// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_2_B\n// 線分同士の交差判定 (端点が他方の線分上にあるならば交差とみなす)\nbool intersect(const Segment &s, const Segment &t) {\n\treturn 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;\n}\n\nint intersect(const Circle &c, const Segment &l) {\n\tif(norm(projection(l, c.p) - c.p) - c.r * c.r > EPS) return 0;\n\tauto d1 = abs(c.p - l.a), d2 = abs(c.p - l.b);\n\tif(d1 < c.r + EPS && d2 < c.r + EPS) return 0;\n\tif(d1 < c.r - EPS && d2 > c.r + EPS || d1 > c.r + EPS && d2 < c.r - EPS) return 1;\n\tconst Point h = projection(l, c.p);\n\tif(dot(l.a - h, l.b - h) < 0) return 2;\n\treturn 0;\n}\n\n// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_7_A&lang=jp\nint intersect(Circle c1, Circle c2) {\n\tif(c1.r < c2.r) swap(c1, c2);\n\tReal d = abs(c1.p - c2.p);\n\tif(c1.r + c2.r < d) return 4;\t // 外に離れている\n\tif(eq(c1.r + c2.r, d)) return 3; // 外接\n\tif(c1.r - c2.r < d) return 2;\t // 交差\n\tif(eq(c1.r - c2.r, d)) return 1; // 内接\n\treturn 0;\t\t\t\t\t\t // 内包\n}\n\nReal distance(const Point &a, const Point &b) { return abs(a - b); }\n\nReal distance(const Line &l, const Point &p) { return abs(p - projection(l, p)); }\n\nReal distance(const Line &l, const Line &m) { return intersect(l, m) ? 0 : distance(l, m.a); }\n\nReal distance(const Segment &s, const Point &p) {\n\tPoint r = projection(s, p);\n\tif(intersect(s, r)) return abs(r - p);\n\treturn min(abs(s.a - p), abs(s.b - p));\n}\n\n// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_2_D\nReal distance(const Segment &a, const Segment &b) {\n\tif(intersect(a, b)) return 0;\n\treturn min({distance(a, b.a), distance(a, b.b), distance(b, a.a), distance(b, a.b)});\n}\n\nReal distance(const Line &l, const Segment &s) {\n\tif(intersect(l, s)) return 0;\n\treturn min(distance(l, s.a), distance(l, s.b));\n}\n\nPoint crosspoint(const Line &l, const Line &m) {\n\tReal A = cross(l.b - l.a, m.b - m.a);\n\tReal B = cross(l.b - l.a, l.b - m.a);\n\tif(eq(abs(A), 0.0) && eq(abs(B), 0.0)) return m.a;\n\treturn m.a + (m.b - m.a) * B / A;\n}\n\n// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_2_C\nPoint crosspoint(const Segment &l, const Segment &m) { return crosspoint(Line(l), Line(m)); }\n\n// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_7_D\npair<Point, Point> crosspoint(const Circle &c, const Line l) {\n\tPoint pr = projection(l, c.p);\n\tPoint e = (l.b - l.a) / abs(l.b - l.a);\n\tif(eq(distance(l, c.p), c.r)) return {pr, pr};\n\tdouble base = sqrt(c.r * c.r - norm(pr - c.p));\n\treturn {pr - e * base, pr + e * base};\n}\n\npair<Point, Point> crosspoint(const Circle &c, const Segment &l) {\n\tLine aa = Line(l.a, l.b);\n\tif(intersect(c, l) == 2) return crosspoint(c, aa);\n\tauto ret = crosspoint(c, aa);\n\tif(dot(l.a - ret.first, l.b - ret.first) < 0)\n\t\tret.second = ret.first;\n\telse\n\t\tret.first = ret.second;\n\treturn ret;\n}\n\n// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_7_E\npair<Point, Point> crosspoint(const Circle &c1, const Circle &c2) {\n\tReal d = abs(c1.p - c2.p);\n\tReal a = acos((c1.r * c1.r + d * d - c2.r * c2.r) / (2 * c1.r * d)); // 余弦定理\n\tReal t = atan2(c2.p.imag() - c1.p.imag(), c2.p.real() - c1.p.real());\n\tPoint p1 = c1.p + Point(cos(t + a) * c1.r, sin(t + a) * c1.r);\n\tPoint p2 = c1.p + Point(cos(t - a) * c1.r, sin(t - a) * c1.r);\n\treturn {p1, p2};\n}\n\n// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_7_F\n// \"点 p を通る円 c の接線\"の接点2つ\npair<Point, Point> tangent(const Circle &c1, const Point &p2) { return crosspoint(c1, Circle(p2, sqrt(norm(c1.p - p2) - c1.r * c1.r))); }\n\n// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_7_G\n// 円 c1, c2 の共通接線\nLines tangent(Circle c1, Circle c2) {\n\tLines ret;\n\tif(c1.r < c2.r) swap(c1, c2);\n\tReal g = norm(c1.p - c2.p);\n\tif(eq(g, 0)) return ret;\n\tPoint u = (c2.p - c1.p) / sqrt(g);\n\tPoint v = rotate(PI * 0.5, u);\n\tfor(int s : {-1, 1}) {\n\t\tReal h = (c1.r + s * c2.r) / sqrt(g);\n\t\tif(eq(1 - h * h, 0)) {\n\t\t\tret.emplace_back(c1.p + u * c1.r, c1.p + (u + v) * c1.r);\n\t\t} else if(1 - h * h > 0) {\n\t\t\tPoint uu = u * h, vv = v * sqrt(1 - h * h);\n\t\t\tret.emplace_back(c1.p + (uu + vv) * c1.r, c2.p - (uu + vv) * c2.r * s);\n\t\t\tret.emplace_back(c1.p + (uu - vv) * c1.r, c2.p - (uu - vv) * c2.r * s);\n\t\t}\n\t}\n\treturn ret;\n}\n\n// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_3_B\n// 凸性判定 (反時計回り)\nbool is_convex(const Polygon &p) {\n\tint n = (int)p.size();\n\tfor(int i = 0; i < n; i++) {\n\t\tif(ccw(p[(i + n - 1) % n], p[i], p[(i + 1) % n]) == -1) return false;\n\t}\n\treturn true;\n}\n\n// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_4_A\n// 凸包\nPolygon convex_hull(Polygon &p) {\n\tint n = (int)p.size(), k = 0;\n\tif(n <= 2) return p;\n\tsort(p.begin(), p.end()); // x -> yの順にソート\n\tvector<Point> ch(2 * n);\n\tfor(int i = 0; i < n; ch[k++] = p[i++]) { //下半分を構成\n\t\twhile(k >= 2 && cross(ch[k - 1] - ch[k - 2], p[i] - ch[k - 1]) < EPS) --k;\n\t}\n\tfor(int i = n - 2, t = k + 1; i >= 0; ch[k++] = p[i--]) { //上半分を構成\n\t\twhile(k >= t && cross(ch[k - 1] - ch[k - 2], p[i] - ch[k - 1]) < EPS) --k;\n\t}\n\tch.resize(k - 1);\n\treturn ch;\n}\n\n// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_3_C\n// 多角形と点の包含判定\nenum { OUT, ON, IN };\n\nint contains(const Polygon &Q, const Point &p) {\n\tbool in = false;\n\tfor(int i = 0; i < Q.size(); i++) {\n\t\tPoint a = Q[i] - p, b = Q[(i + 1) % Q.size()] - p;\n\t\tif(a.imag() > b.imag()) swap(a, b);\n\t\tif(a.imag() <= 0 && 0 < b.imag() && cross(a, b) < 0) in = !in;\n\t\tif(cross(a, b) == 0 && dot(a, b) <= 0) return ON;\n\t}\n\treturn in ? IN : OUT;\n}\n\n// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=1033\n// 線分の重複除去\nvoid merge_segments(vector<Segment> &segs) {\n\tauto merge_if_able = [](Segment &s1, const Segment &s2) {\n\t\tif(abs(cross(s1.b - s1.a, s2.b - s2.a)) > EPS) return false;\n\t\tif(ccw(s1.a, s2.a, s1.b) == 1 || ccw(s1.a, s2.a, s1.b) == -1) return false;\n\t\tif(ccw(s1.a, s1.b, s2.a) == -2 || ccw(s2.a, s2.b, s1.a) == -2) return false;\n\t\ts1 = Segment(min(s1.a, s2.a), max(s1.b, s2.b));\n\t\treturn true;\n\t};\n\n\tfor(int i = 0; i < segs.size(); i++) {\n\t\tif(segs[i].b < segs[i].a) swap(segs[i].a, segs[i].b);\n\t}\n\tfor(int i = 0; i < segs.size(); i++) {\n\t\tfor(int j = i + 1; j < segs.size(); j++) {\n\t\t\tif(merge_if_able(segs[i], segs[j])) {\n\t\t\t\tsegs[j--] = segs.back(), segs.pop_back();\n\t\t\t}\n\t\t}\n\t}\n}\n\n// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=1033\n// 線分アレンジメント\n// 任意の2線分の交点を頂点としたグラフを構築する\nvector<vector<int>> segment_arrangement(vector<Segment> &segs, vector<Point> &ps) {\n\tvector<vector<int>> g;\n\tint N = (int)segs.size();\n\tfor(int i = 0; i < N; i++) {\n\t\tps.emplace_back(segs[i].a);\n\t\tps.emplace_back(segs[i].b);\n\t\tfor(int j = i + 1; j < N; j++) {\n\t\t\tconst Point p1 = segs[i].b - segs[i].a;\n\t\t\tconst Point p2 = segs[j].b - segs[j].a;\n\t\t\tif(eq(cross(p1, p2), 0)) continue;\t// \" == 0\" となっていたのを訂正\n\t\t\tif(intersect(segs[i], segs[j])) {\n\t\t\t\tps.emplace_back(crosspoint(segs[i], segs[j]));\n\t\t\t}\n\t\t}\n\t}\n\tsort(begin(ps), end(ps));\n\tps.erase(unique(begin(ps), end(ps)), end(ps));\t// これ誤差とか大丈夫なのか?\n\n\tint M = (int)ps.size();\n\tg.resize(M);\n\tfor(int i = 0; i < N; i++) {\n\t\tvector<int> vec;\n\t\tfor(int j = 0; j < M; j++) {\n\t\t\tif(intersect(segs[i], ps[j])) {\n\t\t\t\tvec.emplace_back(j);\n\t\t\t}\n\t\t}\n\t\tfor(int j = 1; j < vec.size(); j++) {\n\t\t\tg[vec[j - 1]].push_back(vec[j]);\n\t\t\tg[vec[j]].push_back(vec[j - 1]);\n\t\t}\n\t}\n\treturn (g);\n}\n\n// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_4_C\n// 凸多角形の切断\n// 直線 l.a-l.b で切断しその左側にできる凸多角形を返す\nPolygon convex_cut(const Polygon &U, Line l) {\n\tPolygon ret;\n\tfor(int i = 0; i < U.size(); i++) {\n\t\tPoint now = U[i], nxt = U[(i + 1) % U.size()];\n\t\tif(ccw(l.a, l.b, now) != -1) ret.push_back(now);\n\t\tif(ccw(l.a, l.b, now) * ccw(l.a, l.b, nxt) < 0) {\n\t\t\tret.push_back(crosspoint(Line(now, nxt), l));\n\t\t}\n\t}\n\treturn (ret);\n}\n\n// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_3_A\n// 多角形の面積\nReal area(const Polygon &p) {\n\tReal A = 0;\n\tfor(int i = 0; i < p.size(); ++i) {\n\t\tA += cross(p[i], p[(i + 1) % p.size()]);\n\t}\n\treturn A * 0.5;\n}\n\n// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_7_H\n// 円と多角形の共通部分の面積\nReal area(const Polygon &p, const Circle &c) {\n\tif(p.size() < 3) return 0.0;\n\tfunction<Real(Circle, Point, Point)> cross_area = [&](const Circle &c, const Point &a, const Point &b) {\n\t\tPoint va = c.p - a, vb = c.p - b;\n\t\tReal f = cross(va, vb), ret = 0.0;\n\t\tif(eq(f, 0.0)) return ret;\n\t\tif(max(abs(va), abs(vb)) < c.r + EPS) return f;\n\t\tif(distance(Segment(a, b), c.p) > c.r - EPS) return c.r * c.r * arg(vb * conj(va));\t // 0.5倍してなくない?\n\t\tauto u = crosspoint(c, Segment(a, b));\n\t\tvector<Point> tot{a, u.first, u.second, b};\n\t\tfor(int i = 0; i + 1 < tot.size(); i++) {\n\t\t\tret += cross_area(c, tot[i], tot[i + 1]);\n\t\t}\n\t\treturn ret;\n\t};\n\tReal A = 0;\n\tfor(int i = 0; i < p.size(); i++) {\n\t\tA += cross_area(c, p[i], p[(i + 1) % p.size()]);\n\t}\n\treturn A * 0.5;\t // 0.5倍が抜けていたので修正;\n}\n\n// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_4_B\n// 凸多角形の直径(最遠頂点対間距離)\nReal convex_diameter(const Polygon &p) {\n\tint N = (int)p.size();\n\tint is = 0, js = 0;\n\tfor(int i = 1; i < N; i++) {\n\t\tif(p[i].imag() > p[is].imag()) is = i;\n\t\tif(p[i].imag() < p[js].imag()) js = i;\n\t}\n\tReal maxdis = norm(p[is] - p[js]);\n\n\tint maxi, maxj, i, j;\n\ti = maxi = is;\n\tj = maxj = js;\n\tdo {\n\t\tif(cross(p[(i + 1) % N] - p[i], p[(j + 1) % N] - p[j]) >= 0) {\n\t\t\tj = (j + 1) % N;\n\t\t} else {\n\t\t\ti = (i + 1) % N;\n\t\t}\n\t\tif(norm(p[i] - p[j]) > maxdis) {\n\t\t\tmaxdis = norm(p[i] - p[j]);\n\t\t\tmaxi = i;\n\t\t\tmaxj = j;\n\t\t}\n\t} while(i != is || j != js);\n\treturn sqrt(maxdis);\n}\n\n// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_5_A\n// 最近点対\nReal closest_pair(Points ps) {\n\tif(ps.size() <= 1) throw(0);\n\tsort(begin(ps), end(ps)); // xでソート\n\n\tauto compare_y = [&](const Point &a, const Point &b) { return imag(a) < imag(b); };\n\tvector<Point> beet(ps.size());\n\tconst Real INF = 1e18;\n\n\tfunction<Real(int, int)> rec = [&](int left, int right) {\n\t\tif(right - left <= 1) return INF;\n\t\tint mid = (left + right) >> 1;\n\t\tauto x = real(ps[mid]);\t // 領域内でx座標真中の点\n\t\tauto ret = min(rec(left, mid), rec(mid, right));\n\t\t// y座標でソートされた2つの区間をマージ\n\t\tinplace_merge(begin(ps) + left, begin(ps) + mid, begin(ps) + right, compare_y);\n\t\tint ptr = 0;\n\t\tfor(int i = left; i < right; i++) {\n\t\t\tif(abs(real(ps[i]) - x) >= ret) continue;\n\t\t\tfor(int j = 0; j < ptr; j++) {\n\t\t\t\tauto luz = ps[i] - beet[ptr - j - 1];\n\t\t\t\tif(imag(luz) >= ret) break;\n\t\t\t\tret = min(ret, abs(luz));\n\t\t\t}\n\t\t\tbeet[ptr++] = ps[i];\n\t\t}\n\t\treturn ret;\n\t};\n\treturn rec(0, (int)ps.size());\n}\n\n// (aからの距離):(bからの距離) = dist_a : dist_b となる点の集合(円)を返す\nCircle Apollonius_Circle(Point a, Point b, Real dist_a, Real dist_b) {\n\tassert(!eq(dist_a, dist_b)); //同じ場合はbisectionで\n\tReal asq = dist_a * dist_a, bsq = dist_b * dist_b;\n\tPoint p = (a * bsq - b * asq) * (1 / (bsq - asq));\n\tReal r = abs(a - b) * dist_a * dist_b / abs(asq - bsq);\n\treturn Circle(p, r);\n}\n\n// 二等分線\nLine bisection(Point a, Point b) {\n\ta = (a + b) * 0.5;\n\tb = rotate(PI / 2, b - a) + a;\n\treturn Line(a, b);\n}\n\n#pragma endregion\n\nvoid sol() {\n\tint n, w;\n\tcin >> n >> w;\n\tif(n + w == 0) exit(0);\n\n\tPoints p(n);\n\tvector<Real> h(n);\n\tfor(int i = 0; i < n; i++) cin >> p[i] >> h[i];\n\n\tvector<Real> r(n);\n\tReal lef, dn, up, rig;\n\tconst Real sz = 100.0;\n\n\tauto judge = [&](Real sphere) {\n\t\tfor(int i = 0; i < n; i++) {\n\t\t\tif(h[i] > sphere) {\n\t\t\t\tr[i] = sphere;\n\t\t\t} else {\n\t\t\t\tr[i] = sqrt(pow(sphere, 2) - pow(sphere - h[i], 2));\n\t\t\t}\n\t\t}\n\t\tReal margin = sphere;\n\t\tif(sphere > w) {\n\t\t\tmargin = sqrt(pow(sphere, 2) - pow(sphere - w, 2));\n\t\t}\n\n\t\tlef = margin;\n\t\trig = 100 - margin;\n\t\tup = 100 - margin;\n\t\tdn = margin;\n\n\t\tauto safe = [&](Real x, Real y) {\n\t\t\tif(x < lef - EPS || x > rig + EPS) return false;\n\t\t\tif(y < dn - EPS || y > up + EPS) return false;\n\n\t\t\tfor(int i = 0; i < n; i++) {\n\t\t\t\tif(distance(p[i], Point(x, y)) < r[i] - EPS) {\n\t\t\t\t\treturn false;\n\t\t\t\t}\n\t\t\t}\n\n\t\t\treturn true;\n\t\t};\n\n\t\tauto safepoint = [&](Point p) { return safe(p.real(), p.imag()); };\n\n\t\tif(safe(lef, up) || safe(lef, dn) || safe(rig, up) || safe(rig, dn)) {\n\t\t\treturn true;\n\t\t}\n\n\t\tPoints square(4);\n\t\tsquare[0] = Point(lef, up);\n\t\tsquare[1] = Point(lef, dn);\n\t\tsquare[2] = Point(rig, dn);\n\t\tsquare[3] = Point(rig, up);\n\n\t\tfor(int i = 0; i < n; i++)\n\t\t\tfor(int j = i + 1; j < n; j++) {\n\t\t\t\tint is = intersect(Circle(p[i], r[i]), Circle(p[j], r[j]));\n\t\t\t\tif(is == 4 || is == 0) continue;\n\n\t\t\t\tauto pr = crosspoint(Circle(p[i], r[i]), Circle(p[j], r[j]));\n\t\t\t\tif(safepoint(pr.first) || safepoint(pr.second)) return true;\n\t\t\t}\n\n\t\tfor(int i = 0; i < n; i++)\n\t\t\tfor(int j = 0; j < 4; j++) {\n\t\t\t\tif(intersect(Circle(p[i], r[i]), Line(square[j], square[(j + 1) % 4]))) {\n\t\t\t\t\tauto pr = (crosspoint(Circle(p[i], r[i]), Line(square[j], square[(j + 1) % 4])));\n\t\t\t\t\tif(safepoint(pr.first) || safepoint(pr.second)) return true;\n\t\t\t\t}\n\t\t\t}\n\n\t\treturn false;\n\t};\n\n\tReal ok = 0.0;\n\tReal ng = 10000000000;\n\twhile(ng - ok > 0.00005) {\n\t\tReal med = (ok + ng) / 2;\n\t\tif(judge(med)) {\n\t\t\tok = med;\n\t\t} else {\n\t\t\tng = med;\n\t\t}\n\t}\n\tcout << ok << '\\n';\n\treturn;\n}\n\nint main() {\n\twhile(1) sol();\n}", "accuracy": 1, "time_ms": 250, "memory_kb": 3824, "score_of_the_acc": -0.6849, "final_rank": 9 }, { "submission_id": "aoj_1342_5033645", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\n#pragma region geometry\n\nusing Real = long double; //時間がかかる割にdoubleと大差ない\nusing Point = complex<Real>;\nconst Real EPS = 1e-10, /*1e-8はたまに落ちる*/ PI = acos(-1);\n\ninline bool eq(Real a, Real b) { return fabs(b - a) < EPS; } //誤差無視の等式\n\n//実スカラー倍\nPoint operator*(const Point &p, const Real &d) { return Point(real(p) * d, imag(p) * d); }\n\n// point入力受け取り\nistream &operator>>(istream &is, Point &p) {\n\tReal a, b;\n\tis >> a >> b;\n\tp = Point(a, b);\n\treturn is;\n}\n// point出力\nostream &operator<<(ostream &os, Point &p) { return os << fixed << setprecision(10) << p.real() << \" \" << p.imag(); }\n\n// 点 p を反時計回りに theta 回転\nPoint rotate(Real theta, const Point &p) {\n\treturn Point(cos(theta) * p.real() - sin(theta) * p.imag(), sin(theta) * p.real() + cos(theta) * p.imag());\n}\n\nReal radian_to_degree(Real r) { return (r * 180.0 / PI); }\n\nReal degree_to_radian(Real d) { return (d * PI / 180.0); }\n\n// a-b-c の角度のうち小さい方を返す\nReal get_angle(const Point &a, const Point &b, const Point &c) {\n\tconst Point v(a - b), /* もとはv(b-a)になってたので修正 */ w(c - b);\n\tReal alpha = atan2(v.imag(), v.real()), beta = atan2(w.imag(), w.real());\n\tif(alpha > beta) swap(alpha, beta);\n\tReal theta = (beta - alpha);\n\treturn min(theta, 2 * acos(-1) - theta);\n}\n\nnamespace std {\nbool operator<(const Point &a, const Point &b) { return a.real() != b.real() ? a.real() < b.real() : a.imag() < b.imag(); }\n} // namespace std\n\nstruct Line {\n\tPoint a, b;\n\n\tLine() = default;\n\n\tLine(Point a, Point b) : a(a), b(b) {}\n\n\tLine(Real A, Real B, Real C) // Ax + By = C\n\t{\n\t\tif(eq(A, 0))\n\t\t\ta = Point(0, C / B), b = Point(1, C / B);\n\t\telse if(eq(B, 0))\n\t\t\tb = Point(C / A, 0), b = Point(C / A, 1);\n\t\telse\n\t\t\ta = Point(0, C / B), b = Point(C / A, 0);\n\t}\n\n\tfriend ostream &operator<<(ostream &os, Line &p) { return os << p.a << \" to \" << p.b; }\n\n\tfriend istream &operator>>(istream &is, Line &a) { return is >> a.a >> a.b; }\n};\n\nstruct Segment : Line {\n\tSegment() = default;\n\n\tSegment(Point a, Point b) : Line(a, b) {}\n};\n\nstruct Circle {\n\tPoint p;\n\tReal r;\n\n\tCircle() = default;\n\n\tCircle(Point p, Real r) : p(p), r(r) {}\n};\n\nusing Points = vector<Point>;\nusing Polygon = vector<Point>;\nusing Segments = vector<Segment>;\nusing Lines = vector<Line>;\nusing Circles = vector<Circle>;\n\n//外積\nReal cross(const Point &a, const Point &b) { return real(a) * imag(b) - imag(a) * real(b); }\n//内積\nReal dot(const Point &a, const Point &b) { return real(a) * real(b) + imag(a) * imag(b); }\n\n// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_1_C\n// 点の回転方向\nint ccw(const Point &a, Point b, Point c) {\n\tb = b - a, c = c - a;\n\tif(cross(b, c) > EPS) return +1; // \"COUNTER_CLOCKWISE\"\n\tif(cross(b, c) < -EPS) return -1; // \"CLOCKWISE\"\n\tif(dot(b, c) < 0) return +2;\t // \"ONLINE_BACK\"\n\tif(norm(b) < norm(c)) return -2; // \"ONLINE_FRONT\"\n\treturn 0;\t\t\t\t\t\t // \"ON_SEGMENT\"\n}\n\n// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_2_A\n// 平行判定\nbool parallel(const Line &a, const Line &b) { return eq(cross(a.b - a.a, b.b - b.a), 0.0); }\n\n// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_2_A\n// 垂直判定\nbool orthogonal(const Line &a, const Line &b) { return eq(dot(a.a - a.b, b.a - b.b), 0.0); }\n\n// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_1_A\n// 射影\n// 直線 l に p から垂線を引いた交点を求める\nPoint projection(const Line &l, const Point &p) {\n\tdouble t = dot(p - l.a, l.a - l.b) / norm(l.a - l.b);\n\treturn l.a + (l.a - l.b) * t;\n}\n\nPoint projection(const Segment &l, const Point &p) {\n\tdouble t = dot(p - l.a, l.a - l.b) / norm(l.a - l.b);\n\treturn l.a + (l.a - l.b) * t;\n}\n\n// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_1_B\n// 反射\n// 直線 l を対称軸として点 p と線対称にある点を求める\nPoint reflection(const Line &l, const Point &p) { return p + (projection(l, p) - p) * 2.0; }\n\nbool intersect(const Line &l, const Point &p) { return abs(ccw(l.a, l.b, p)) != 1; }\n\nbool intersect(const Line &l, const Line &m) { return abs(cross(l.b - l.a, m.b - m.a)) > EPS || abs(cross(l.b - l.a, m.b - l.a)) < EPS; }\n\nbool intersect(const Segment &s, const Point &p) { return ccw(s.a, s.b, p) == 0; }\n\nbool intersect(const Line &l, const Segment &s) { return cross(l.b - l.a, s.a - l.a) * cross(l.b - l.a, s.b - l.a) < EPS; }\n\nReal distance(const Line &l, const Point &p);\n\nbool intersect(const Circle &c, const Line &l) { return distance(l, c.p) <= c.r + EPS; }\n\nbool intersect(const Circle &c, const Point &p) { return abs(abs(p - c.p) - c.r) < EPS; }\n\n// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_2_B\n// 線分同士の交差判定 (端点が他方の線分上にあるならば交差とみなす)\nbool intersect(const Segment &s, const Segment &t) {\n\treturn 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;\n}\n\nint intersect(const Circle &c, const Segment &l) {\n\tif(norm(projection(l, c.p) - c.p) - c.r * c.r > EPS) return 0;\n\tauto d1 = abs(c.p - l.a), d2 = abs(c.p - l.b);\n\tif(d1 < c.r + EPS && d2 < c.r + EPS) return 0;\n\tif(d1 < c.r - EPS && d2 > c.r + EPS || d1 > c.r + EPS && d2 < c.r - EPS) return 1;\n\tconst Point h = projection(l, c.p);\n\tif(dot(l.a - h, l.b - h) < 0) return 2;\n\treturn 0;\n}\n\n// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_7_A&lang=jp\nint intersect(Circle c1, Circle c2) {\n\tif(c1.r < c2.r) swap(c1, c2);\n\tReal d = abs(c1.p - c2.p);\n\tif(c1.r + c2.r < d) return 4;\t // 外に離れている\n\tif(eq(c1.r + c2.r, d)) return 3; // 外接\n\tif(c1.r - c2.r < d) return 2;\t // 交差\n\tif(eq(c1.r - c2.r, d)) return 1; // 内接\n\treturn 0;\t\t\t\t\t\t // 内包\n}\n\nReal distance(const Point &a, const Point &b) { return abs(a - b); }\n\nReal distance(const Line &l, const Point &p) { return abs(p - projection(l, p)); }\n\nReal distance(const Line &l, const Line &m) { return intersect(l, m) ? 0 : distance(l, m.a); }\n\nReal distance(const Segment &s, const Point &p) {\n\tPoint r = projection(s, p);\n\tif(intersect(s, r)) return abs(r - p);\n\treturn min(abs(s.a - p), abs(s.b - p));\n}\n\n// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_2_D\nReal distance(const Segment &a, const Segment &b) {\n\tif(intersect(a, b)) return 0;\n\treturn min({distance(a, b.a), distance(a, b.b), distance(b, a.a), distance(b, a.b)});\n}\n\nReal distance(const Line &l, const Segment &s) {\n\tif(intersect(l, s)) return 0;\n\treturn min(distance(l, s.a), distance(l, s.b));\n}\n\nPoint crosspoint(const Line &l, const Line &m) {\n\tReal A = cross(l.b - l.a, m.b - m.a);\n\tReal B = cross(l.b - l.a, l.b - m.a);\n\tif(eq(abs(A), 0.0) && eq(abs(B), 0.0)) return m.a;\n\treturn m.a + (m.b - m.a) * B / A;\n}\n\n// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_2_C\nPoint crosspoint(const Segment &l, const Segment &m) { return crosspoint(Line(l), Line(m)); }\n\n// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_7_D\npair<Point, Point> crosspoint(const Circle &c, const Line l) {\n\tPoint pr = projection(l, c.p);\n\tPoint e = (l.b - l.a) / abs(l.b - l.a);\n\tif(eq(distance(l, c.p), c.r)) return {pr, pr};\n\tdouble base = sqrt(c.r * c.r - norm(pr - c.p));\n\treturn {pr - e * base, pr + e * base};\n}\n\npair<Point, Point> crosspoint(const Circle &c, const Segment &l) {\n\tLine aa = Line(l.a, l.b);\n\tif(intersect(c, l) == 2) return crosspoint(c, aa);\n\tauto ret = crosspoint(c, aa);\n\tif(dot(l.a - ret.first, l.b - ret.first) < 0)\n\t\tret.second = ret.first;\n\telse\n\t\tret.first = ret.second;\n\treturn ret;\n}\n\n// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_7_E\npair<Point, Point> crosspoint(const Circle &c1, const Circle &c2) {\n\tReal d = abs(c1.p - c2.p);\n\tReal a = acos((c1.r * c1.r + d * d - c2.r * c2.r) / (2 * c1.r * d)); // 余弦定理\n\tReal t = atan2(c2.p.imag() - c1.p.imag(), c2.p.real() - c1.p.real());\n\tPoint p1 = c1.p + Point(cos(t + a) * c1.r, sin(t + a) * c1.r);\n\tPoint p2 = c1.p + Point(cos(t - a) * c1.r, sin(t - a) * c1.r);\n\treturn {p1, p2};\n}\n\n// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_7_F\n// \"点 p を通る円 c の接線\"の接点2つ\npair<Point, Point> tangent(const Circle &c1, const Point &p2) { return crosspoint(c1, Circle(p2, sqrt(norm(c1.p - p2) - c1.r * c1.r))); }\n\n// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_7_G\n// 円 c1, c2 の共通接線\nLines tangent(Circle c1, Circle c2) {\n\tLines ret;\n\tif(c1.r < c2.r) swap(c1, c2);\n\tReal g = norm(c1.p - c2.p);\n\tif(eq(g, 0)) return ret;\n\tPoint u = (c2.p - c1.p) / sqrt(g);\n\tPoint v = rotate(PI * 0.5, u);\n\tfor(int s : {-1, 1}) {\n\t\tReal h = (c1.r + s * c2.r) / sqrt(g);\n\t\tif(eq(1 - h * h, 0)) {\n\t\t\tret.emplace_back(c1.p + u * c1.r, c1.p + (u + v) * c1.r);\n\t\t} else if(1 - h * h > 0) {\n\t\t\tPoint uu = u * h, vv = v * sqrt(1 - h * h);\n\t\t\tret.emplace_back(c1.p + (uu + vv) * c1.r, c2.p - (uu + vv) * c2.r * s);\n\t\t\tret.emplace_back(c1.p + (uu - vv) * c1.r, c2.p - (uu - vv) * c2.r * s);\n\t\t}\n\t}\n\treturn ret;\n}\n\n// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_3_B\n// 凸性判定 (反時計回り)\nbool is_convex(const Polygon &p) {\n\tint n = (int)p.size();\n\tfor(int i = 0; i < n; i++) {\n\t\tif(ccw(p[(i + n - 1) % n], p[i], p[(i + 1) % n]) == -1) return false;\n\t}\n\treturn true;\n}\n\n// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_4_A\n// 凸包\nPolygon convex_hull(Polygon &p) {\n\tint n = (int)p.size(), k = 0;\n\tif(n <= 2) return p;\n\tsort(p.begin(), p.end()); // x -> yの順にソート\n\tvector<Point> ch(2 * n);\n\tfor(int i = 0; i < n; ch[k++] = p[i++]) { //下半分を構成\n\t\twhile(k >= 2 && cross(ch[k - 1] - ch[k - 2], p[i] - ch[k - 1]) < EPS) --k;\n\t}\n\tfor(int i = n - 2, t = k + 1; i >= 0; ch[k++] = p[i--]) { //上半分を構成\n\t\twhile(k >= t && cross(ch[k - 1] - ch[k - 2], p[i] - ch[k - 1]) < EPS) --k;\n\t}\n\tch.resize(k - 1);\n\treturn ch;\n}\n\n// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_3_C\n// 多角形と点の包含判定\nenum { OUT, ON, IN };\n\nint contains(const Polygon &Q, const Point &p) {\n\tbool in = false;\n\tfor(int i = 0; i < Q.size(); i++) {\n\t\tPoint a = Q[i] - p, b = Q[(i + 1) % Q.size()] - p;\n\t\tif(a.imag() > b.imag()) swap(a, b);\n\t\tif(a.imag() <= 0 && 0 < b.imag() && cross(a, b) < 0) in = !in;\n\t\tif(cross(a, b) == 0 && dot(a, b) <= 0) return ON;\n\t}\n\treturn in ? IN : OUT;\n}\n\n// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=1033\n// 線分の重複除去\nvoid merge_segments(vector<Segment> &segs) {\n\tauto merge_if_able = [](Segment &s1, const Segment &s2) {\n\t\tif(abs(cross(s1.b - s1.a, s2.b - s2.a)) > EPS) return false;\n\t\tif(ccw(s1.a, s2.a, s1.b) == 1 || ccw(s1.a, s2.a, s1.b) == -1) return false;\n\t\tif(ccw(s1.a, s1.b, s2.a) == -2 || ccw(s2.a, s2.b, s1.a) == -2) return false;\n\t\ts1 = Segment(min(s1.a, s2.a), max(s1.b, s2.b));\n\t\treturn true;\n\t};\n\n\tfor(int i = 0; i < segs.size(); i++) {\n\t\tif(segs[i].b < segs[i].a) swap(segs[i].a, segs[i].b);\n\t}\n\tfor(int i = 0; i < segs.size(); i++) {\n\t\tfor(int j = i + 1; j < segs.size(); j++) {\n\t\t\tif(merge_if_able(segs[i], segs[j])) {\n\t\t\t\tsegs[j--] = segs.back(), segs.pop_back();\n\t\t\t}\n\t\t}\n\t}\n}\n\n// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=1033\n// 線分アレンジメント\n// 任意の2線分の交点を頂点としたグラフを構築する\nvector<vector<int>> segment_arrangement(vector<Segment> &segs, vector<Point> &ps) {\n\tvector<vector<int>> g;\n\tint N = (int)segs.size();\n\tfor(int i = 0; i < N; i++) {\n\t\tps.emplace_back(segs[i].a);\n\t\tps.emplace_back(segs[i].b);\n\t\tfor(int j = i + 1; j < N; j++) {\n\t\t\tconst Point p1 = segs[i].b - segs[i].a;\n\t\t\tconst Point p2 = segs[j].b - segs[j].a;\n\t\t\tif(eq(cross(p1, p2), 0)) continue;\t// \" == 0\" となっていたのを訂正\n\t\t\tif(intersect(segs[i], segs[j])) {\n\t\t\t\tps.emplace_back(crosspoint(segs[i], segs[j]));\n\t\t\t}\n\t\t}\n\t}\n\tsort(begin(ps), end(ps));\n\tps.erase(unique(begin(ps), end(ps)), end(ps));\t// これ誤差とか大丈夫なのか?\n\n\tint M = (int)ps.size();\n\tg.resize(M);\n\tfor(int i = 0; i < N; i++) {\n\t\tvector<int> vec;\n\t\tfor(int j = 0; j < M; j++) {\n\t\t\tif(intersect(segs[i], ps[j])) {\n\t\t\t\tvec.emplace_back(j);\n\t\t\t}\n\t\t}\n\t\tfor(int j = 1; j < vec.size(); j++) {\n\t\t\tg[vec[j - 1]].push_back(vec[j]);\n\t\t\tg[vec[j]].push_back(vec[j - 1]);\n\t\t}\n\t}\n\treturn (g);\n}\n\n// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_4_C\n// 凸多角形の切断\n// 直線 l.a-l.b で切断しその左側にできる凸多角形を返す\nPolygon convex_cut(const Polygon &U, Line l) {\n\tPolygon ret;\n\tfor(int i = 0; i < U.size(); i++) {\n\t\tPoint now = U[i], nxt = U[(i + 1) % U.size()];\n\t\tif(ccw(l.a, l.b, now) != -1) ret.push_back(now);\n\t\tif(ccw(l.a, l.b, now) * ccw(l.a, l.b, nxt) < 0) {\n\t\t\tret.push_back(crosspoint(Line(now, nxt), l));\n\t\t}\n\t}\n\treturn (ret);\n}\n\n// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_3_A\n// 多角形の面積\nReal area(const Polygon &p) {\n\tReal A = 0;\n\tfor(int i = 0; i < p.size(); ++i) {\n\t\tA += cross(p[i], p[(i + 1) % p.size()]);\n\t}\n\treturn A * 0.5;\n}\n\n// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_7_H\n// 円と多角形の共通部分の面積\nReal area(const Polygon &p, const Circle &c) {\n\tif(p.size() < 3) return 0.0;\n\tfunction<Real(Circle, Point, Point)> cross_area = [&](const Circle &c, const Point &a, const Point &b) {\n\t\tPoint va = c.p - a, vb = c.p - b;\n\t\tReal f = cross(va, vb), ret = 0.0;\n\t\tif(eq(f, 0.0)) return ret;\n\t\tif(max(abs(va), abs(vb)) < c.r + EPS) return f;\n\t\tif(distance(Segment(a, b), c.p) > c.r - EPS) return c.r * c.r * arg(vb * conj(va));\t // 0.5倍してなくない?\n\t\tauto u = crosspoint(c, Segment(a, b));\n\t\tvector<Point> tot{a, u.first, u.second, b};\n\t\tfor(int i = 0; i + 1 < tot.size(); i++) {\n\t\t\tret += cross_area(c, tot[i], tot[i + 1]);\n\t\t}\n\t\treturn ret;\n\t};\n\tReal A = 0;\n\tfor(int i = 0; i < p.size(); i++) {\n\t\tA += cross_area(c, p[i], p[(i + 1) % p.size()]);\n\t}\n\treturn A * 0.5;\t // 0.5倍が抜けていたので修正;\n}\n\n// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_4_B\n// 凸多角形の直径(最遠頂点対間距離)\nReal convex_diameter(const Polygon &p) {\n\tint N = (int)p.size();\n\tint is = 0, js = 0;\n\tfor(int i = 1; i < N; i++) {\n\t\tif(p[i].imag() > p[is].imag()) is = i;\n\t\tif(p[i].imag() < p[js].imag()) js = i;\n\t}\n\tReal maxdis = norm(p[is] - p[js]);\n\n\tint maxi, maxj, i, j;\n\ti = maxi = is;\n\tj = maxj = js;\n\tdo {\n\t\tif(cross(p[(i + 1) % N] - p[i], p[(j + 1) % N] - p[j]) >= 0) {\n\t\t\tj = (j + 1) % N;\n\t\t} else {\n\t\t\ti = (i + 1) % N;\n\t\t}\n\t\tif(norm(p[i] - p[j]) > maxdis) {\n\t\t\tmaxdis = norm(p[i] - p[j]);\n\t\t\tmaxi = i;\n\t\t\tmaxj = j;\n\t\t}\n\t} while(i != is || j != js);\n\treturn sqrt(maxdis);\n}\n\n// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_5_A\n// 最近点対\nReal closest_pair(Points ps) {\n\tif(ps.size() <= 1) throw(0);\n\tsort(begin(ps), end(ps)); // xでソート\n\n\tauto compare_y = [&](const Point &a, const Point &b) { return imag(a) < imag(b); };\n\tvector<Point> beet(ps.size());\n\tconst Real INF = 1e18;\n\n\tfunction<Real(int, int)> rec = [&](int left, int right) {\n\t\tif(right - left <= 1) return INF;\n\t\tint mid = (left + right) >> 1;\n\t\tauto x = real(ps[mid]);\t // 領域内でx座標真中の点\n\t\tauto ret = min(rec(left, mid), rec(mid, right));\n\t\t// y座標でソートされた2つの区間をマージ\n\t\tinplace_merge(begin(ps) + left, begin(ps) + mid, begin(ps) + right, compare_y);\n\t\tint ptr = 0;\n\t\tfor(int i = left; i < right; i++) {\n\t\t\tif(abs(real(ps[i]) - x) >= ret) continue;\n\t\t\tfor(int j = 0; j < ptr; j++) {\n\t\t\t\tauto luz = ps[i] - beet[ptr - j - 1];\n\t\t\t\tif(imag(luz) >= ret) break;\n\t\t\t\tret = min(ret, abs(luz));\n\t\t\t}\n\t\t\tbeet[ptr++] = ps[i];\n\t\t}\n\t\treturn ret;\n\t};\n\treturn rec(0, (int)ps.size());\n}\n\n// (aからの距離):(bからの距離) = dist_a : dist_b となる点の集合(円)を返す\nCircle Apollonius_Circle(Point a, Point b, Real dist_a, Real dist_b) {\n\tassert(!eq(dist_a, dist_b)); //同じ場合はbisectionで\n\tReal asq = dist_a * dist_a, bsq = dist_b * dist_b;\n\tPoint p = (a * bsq - b * asq) * (1 / (bsq - asq));\n\tReal r = abs(a - b) * dist_a * dist_b / abs(asq - bsq);\n\treturn Circle(p, r);\n}\n\n// 二等分線\nLine bisection(Point a, Point b) {\n\ta = (a + b) * 0.5;\n\tb = rotate(PI / 2, b - a) + a;\n\treturn Line(a, b);\n}\n\n#pragma endregion\n\nusing R = Real;\n\nbool left_is_smaller(R left, R right) { return left < right - EPS; }\n\nvoid sol() {\n\tint n, w;\n\tcin >> n >> w;\n\tif(n + w == 0) exit(0);\n\n\tPoints p(n);\n\tvector<Real> h(n);\n\tfor(int i = 0; i < n; i++) cin >> p[i] >> h[i];\n\n\tvector<Real> r(n);\n\tReal lef, dn, up, rig;\n\tconst Real sz = 100.0;\n\n\tauto judge = [&](Real sphere) {\n\t\tfor(int i = 0; i < n; i++) {\n\t\t\tif(h[i] > sphere) {\n\t\t\t\tr[i] = sphere;\n\t\t\t} else {\n\t\t\t\tr[i] = sqrt(pow(sphere, 2) - pow(sphere - h[i], 2));\n\t\t\t}\n\t\t}\n\t\tR margin = sphere;\n\t\tif(sphere > w) {\n\t\t\tmargin = sqrt(pow(sphere, 2) - pow(sphere - w, 2));\n\t\t}\n\n\t\tlef = margin;\n\t\trig = 100 - margin;\n\t\tup = 100 - margin;\n\t\tdn = margin;\n\n\t\tauto safe = [&](Real x, Real y) {\n\t\t\tif(x < lef - EPS || x > rig + EPS) return false;\n\t\t\tif(y < dn - EPS || y > up + EPS) return false;\n\n\t\t\tfor(int i = 0; i < n; i++) {\n\t\t\t\tif(distance(p[i], Point(x, y)) < r[i] - EPS) {\n\t\t\t\t\treturn false;\n\t\t\t\t}\n\t\t\t}\n\n\t\t\treturn true;\n\t\t};\n\n\t\tauto safepoint = [&](Point p) { return safe(p.real(), p.imag()); };\n\n\t\tif(safe(lef, up) || safe(lef, dn) || safe(rig, up) || safe(rig, dn)) {\n\t\t\treturn true;\n\t\t}\n\n\t\tPoints square(4);\n\t\tsquare[0] = Point(lef, up);\n\t\tsquare[1] = Point(lef, dn);\n\t\tsquare[2] = Point(rig, dn);\n\t\tsquare[3] = Point(rig, up);\n\n\t\tfor(int i = 0; i < n; i++)\n\t\t\tfor(int j = i + 1; j < n; j++) {\n\t\t\t\tint is = intersect(Circle(p[i], r[i]), Circle(p[j], r[j]));\n\t\t\t\tif(is == 4 || is == 0) continue;\n\n\t\t\t\tauto pr = crosspoint(Circle(p[i], r[i]), Circle(p[j], r[j]));\n\t\t\t\tif(safepoint(pr.first) || safepoint(pr.second)) return true;\n\t\t\t}\n\n\t\tfor(int i = 0; i < n; i++)\n\t\t\tfor(int j = 0; j < 4; j++) {\n\t\t\t\tif(intersect(Circle(p[i], r[i]), Line(square[j], square[(j + 1) % 4]))) {\n\t\t\t\t\tauto pr = (crosspoint(Circle(p[i], r[i]), Line(square[j], square[(j + 1) % 4])));\n\t\t\t\t\tif(safepoint(pr.first) || safepoint(pr.second)) return true;\n\t\t\t\t}\n\t\t\t}\n\n\t\treturn false;\n\t};\n\n\tR ok = 0.0;\n\tR ng = 10000000000;\n\twhile(ng - ok > 0.00005) {\n\t\tR med = (ok + ng) / 2;\n\t\tif(judge(med)) {\n\t\t\tok = med;\n\t\t} else {\n\t\t\tng = med;\n\t\t}\n\t}\n\tcout << ok << '\\n';\n\treturn;\n}\n\nint main() {\n\twhile(1) sol();\n}", "accuracy": 1, "time_ms": 250, "memory_kb": 3668, "score_of_the_acc": -0.5076, "final_rank": 8 }, { "submission_id": "aoj_1342_5032948", "code_snippet": "/*\tauthor: Kite_kuma\n\tcreated: 2020.12.04 10:47:42 */\n\n// #ifdef LOCAL\n// #define _GLIBCXX_DEBUG\n// #endif\n#include <bits/stdc++.h>\nusing namespace std;\n\n#pragma region aliases\n\n#define rep(i, n) for(long long i = 0; i < (n); i++)\n#define rrep(i, n) for(long long i = (n)-1; i > -1; i--)\n#define Rep(i, m, n) for(long long i = (m); i < (n); i++)\n#define rRep(i, m, n) for(long long i = (n)-1; i >= (m); i--)\n#define REP(i, m, n, p) for(long long i = m; i < n; i += p)\n#define foa(s, v) for(auto &s : v)\n#define all(v) (v).begin(), (v).end()\n#define rall(v) (v).rbegin(), (v).rend()\n#define bcnt(n) __builtin_popcountll(n)\n\nusing ll = long long;\nusing ld = long double;\nusing ull = unsigned long long;\nusing vb = vector<bool>;\nusing vi = vector<int>;\nusing vvi = vector<vector<int>>;\nusing vvvi = vector<vector<vector<int>>>;\nusing vll = vector<ll>;\nusing vvll = vector<vll>;\nusing vvvll = vector<vvll>;\ntemplate <class T = ll>\nusing V = vector<T>;\ntemplate <class T = ll>\nusing VV = V<V<T>>;\ntemplate <class T = ll>\nusing VVV = V<V<V<T>>>;\ntemplate <class T = ll>\nusing pqup = priority_queue<T, vector<T>, greater<T>>;\ntemplate <class T = ll>\nusing pqdn = priority_queue<T>;\n\n#pragma endregion\n\n#pragma region constants\n\nlong long const dekai = 1e18;\nconst long double pi = acos(-1);\nconst char dl = '\\n';\nconst char sp = ' ';\nint dx[8] = {1, 0, -1, 0, 1, 1, -1, -1};\nint dy[8] = {0, 1, 0, -1, -1, 1, -1, 1};\n\nconst int mod = 1000000007;\n// const int mod = 998244353;\n\n#pragma endregion\n\n#pragma region basic_procedure\n\ntemplate <class T>\ninline bool isin(T x, T lef, T rig) {\n\treturn ((lef <= x) && (x < rig));\n}\n\ntemplate <class T>\ninline bool chmin(T &a, T b) {\n\tif(a > b) {\n\t\ta = b;\n\t\treturn true;\n\t}\n\treturn false;\n}\ntemplate <class T>\ninline bool chmax(T &a, T b) {\n\tif(a < b) {\n\t\ta = b;\n\t\treturn true;\n\t}\n\treturn false;\n}\n\nvoid Yes(bool f = 1) { cout << (f ? \"Yes\" : \"No\") << '\\n'; }\nvoid No() { cout << \"No\\n\"; }\nvoid YES(bool f = 1) { cout << (f ? \"YES\" : \"NO\") << '\\n'; }\nvoid NO() { cout << \"NO\\n\"; }\n\ntemplate <class T>\nvoid drop(T answer) {\n\tcout << answer << '\\n';\n\texit(0);\n}\n\nvoid err(bool flag) {\n\tif(!flag) return;\n\tcout << -1 << '\\n';\n\texit(0);\n}\n\ntemplate <class T>\nvoid vout(vector<T> const &v, bool tate = 0) {\n\tif(tate) {\n\t\tfor(auto const &a : v) {\n\t\t\tcout << a << '\\n';\n\t\t}\n\t\treturn;\n\t}\n\tfor(auto it = v.begin(); it != v.end(); it++) {\n\t\tcout << (*it);\n\t\tif(it != v.end() - 1) {\n\t\t\tcout << ' ';\n\t\t}\n\t}\n\tcout << '\\n';\n\treturn;\n}\n\ninline void print() { cout << '\\n'; }\ntemplate <class T>\ninline void print(T x) {\n\tcout << x << '\\n';\n\treturn;\n}\ntemplate <typename Head, typename... Tail>\nvoid print(Head H, Tail... T) {\n\tcout << H << \" \";\n\tprint(T...);\n}\n\ntemplate <class T>\nvoid add(vector<T> &v, T val) {\n\tfor(auto &a : v) a += val;\n\treturn;\n}\n\ntemplate <class T>\nT dup(T a, T b) {\n\tassert(b != 0);\n\tT x = abs(a);\n\tT y = abs(b);\n\tT z = (x + y - 1) / y;\n\tif((a < 0 && b > 0) || (a > 0 && b < 0))\n\t\treturn -z;\n\telse if(a == 0)\n\t\treturn 0;\n\telse\n\t\treturn z;\n}\n\ntemplate <class T>\nT greatest_lower_multiple(T x, T d) {\n\tif(d == 0) return 0;\n\tif(d < 0) d *= -1;\n\tT y = x % d;\n\tif(y < 0) y += d;\n\treturn x - y;\n}\n\ntemplate <class T>\nT least_upper_multiple(T x, T d) {\n\treturn -greatest_lower_multiple(-x, d);\n}\n\nlong long POW(long long a, long long n) {\n\tlong long res = 1;\n\twhile(n > 0) {\n\t\tif(n & 1) res = res * a;\n\t\ta = a * a;\n\t\tn >>= 1;\n\t}\n\treturn res;\n}\n\nlong long modpow(long long a, long long n, long long mod) {\t // a^n mod\n\tassert(n >= 0);\n\tif(mod == 1) return 0LL;\n\tlong long res = 1;\n\twhile(n > 0) {\n\t\tif(n & 1) res = res * a % mod;\n\t\ta = a * a % mod;\n\t\tn >>= 1;\n\t}\n\treturn res;\n}\n\n// a * x % mod == __gcd(a,mod)なるxを返す\n// a が modの倍数でないことが条件\nlong long modinv(long long a, long long mod) {\n\tlong long b = mod, u = 1, v = 0;\n\twhile(b) {\n\t\tlong long t = a / b;\n\t\ta -= t * b;\n\t\tswap(a, b);\n\t\tu -= t * v;\n\t\tswap(u, v);\n\t}\n\tu %= mod;\n\tif(u < 0) u += mod;\n\treturn u;\n}\n\nvvll comb(100, vll(100, -1));\nlong long com(long long n, long long k) { //普通の二項計数\n\tassert(n < 100 && k < 100);\n\tif(n < k || k < 0 || n < 0) return 0;\n\tif(comb[n][k] != -1) return comb[n][k];\n\tll res;\n\tif(n - k < k)\n\t\tres = com(n, n - k);\n\telse if(k == 0)\n\t\tres = 1;\n\telse\n\t\tres = com(n - 1, k - 1) + com(n - 1, k);\n\tcomb[n][k] = res;\n\treturn res;\n}\n\nconst int MAX = 5100000; // about 300ms, when MAX = 3*10^7 : 1900ms\nlong long fac[MAX], finv[MAX], inv[MAX];\n\nvoid cominit() {\n\tfac[0] = fac[1] = 1;\n\tfinv[0] = finv[1] = 1;\n\tinv[1] = 1;\n\tfor(int i = 2; i < MAX; i++) {\n\t\tfac[i] = fac[i - 1] * i % mod;\n\t\tinv[i] = mod - inv[mod % i] * (mod / i) % mod;\n\t\tfinv[i] = finv[i - 1] * inv[i] % mod;\n\t}\n}\nlong long commod(long long n, long long k) {\n\tif(n < k) return 0;\n\tif(n < 0 || k < 0) return 0;\n\treturn fac[n] * (finv[k] * finv[n - k] % mod) % mod;\n}\nlong long pmod(long long n, long long k) {\n\tif(n < k) return 0;\n\tif(n < 0 || k < 0) return 0;\n\treturn fac[n] * finv[n - k] % mod;\n}\n// n個の区別しないボールを区別するk個の箱に入れる方法の総数\nlong long hmod(long long n, long long k) {\t// 重複組み合わせ\n\treturn commod(n + k - 1, n);\n}\n#pragma endregion\n\n#pragma region input\n#define VEC(type, name, size) \\\n\tvector<type> name(size); \\\n\tINPUT(name)\n#define VVEC(type, name, h, w) \\\n\tvector<vector<type>> name(h, vector<type>(w)); \\\n\tINPUT(name)\n#define INT(...) \\\n\tint __VA_ARGS__; \\\n\tINPUT(__VA_ARGS__)\n#define LL(...) \\\n\tlong long __VA_ARGS__; \\\n\tINPUT(__VA_ARGS__)\n#define STR(...) \\\n\tstring __VA_ARGS__; \\\n\tINPUT(__VA_ARGS__)\n#define CHAR(...) \\\n\tchar __VA_ARGS__; \\\n\tINPUT(__VA_ARGS__)\n#define DOUBLE(...) \\\n\tdouble __VA_ARGS__; \\\n\tINPUT(__VA_ARGS__)\n#define LD(...) \\\n\tlong double __VA_ARGS__; \\\n\tINPUT(__VA_ARGS__)\n\ntemplate <class T>\nvoid scan(T &a) {\n\tcin >> a;\n}\ntemplate <class T>\nvoid scan(vector<T> &a) {\n\tfor(auto &i : a) scan(i);\n}\ntemplate <class T, class L>\nvoid scan(pair<T, L> &p) {\n\tscan(p.first);\n\tscan(p.second);\n}\nvoid INPUT() {}\ntemplate <class Head, class... Tail>\nvoid INPUT(Head &head, Tail &... tail) {\n\tscan(head);\n\tINPUT(tail...);\n}\n\ntemplate <typename T1, typename T2>\nistream &operator>>(istream &is, pair<T1, T2> &p) {\n\tis >> p.first >> p.second;\n\treturn is;\n}\n#pragma endregion\n\n#pragma region debug\n\n#pragma region output\ntemplate <typename T1, typename T2>\nstd::ostream &std::operator<<(std::ostream &os, const std::pair<T1, T2> &p) {\n\tos << p.first << \" \" << p.second;\n\treturn os;\n}\ntemplate <class T>\nstd::ostream &std::operator<<(std::ostream &os, const std::vector<T> &v) {\n\tfor(int i = 0; i < (int)v.size(); i++) {\n\t\tif(i) os << \" \";\n\t\tos << v[i];\n\t}\n\treturn os;\n}\n#pragma endregion\n\n#pragma region view\n\ntemplate <typename T>\nvoid view(const T e) {\n\tstd::cerr << e;\n}\n\ntemplate <typename T, typename U>\nvoid view(const std::pair<T, U> &p) {\n\tstd::cerr << \"(\";\n\tview(p.first);\n\tstd::cerr << \", \";\n\tview(p.second);\n\tstd::cerr << \")\";\n}\n\ntemplate <typename T>\nvoid view(const std::set<T> &s) {\n\tif(s.empty()) {\n\t\tstd::cerr << \"{ }\";\n\t\treturn;\n\t}\n\tstd::cerr << \"{ \";\n\tfor(auto &t : s) {\n\t\tview(t);\n\t\tstd::cerr << \", \";\n\t}\n\tstd::cerr << \"\\b\\b }\";\n}\n\ntemplate <typename T>\nvoid view(const std::unordered_set<T> &s) {\n\tif(s.empty()) {\n\t\tstd::cerr << \"{ }\";\n\t\treturn;\n\t}\n\tstd::cerr << \"{ \";\n\tfor(auto &t : s) {\n\t\tview(t);\n\t\tstd::cerr << \", \";\n\t}\n\tstd::cerr << \"\\b\\b }\";\n}\n\ntemplate <typename T>\nvoid view(const std::vector<T> &v) {\n\tif(v.empty()) {\n\t\tstd::cerr << \"{ }\";\n\t\treturn;\n\t}\n\tstd::cerr << \"{ \";\n\tfor(const auto &e : v) {\n\t\tview(e);\n\t\tstd::cerr << \", \";\n\t}\n\tstd::cerr << \"\\b\\b }\";\n}\n\ntemplate <typename T>\nvoid view(const std::vector<std::vector<T>> &vv) {\n\tstd::cerr << \"{\\n\";\n\tfor(const auto &v : vv) {\n\t\tstd::cerr << \"\\t\";\n\t\tview(v);\n\t\tstd::cerr << '\\n';\n\t}\n\tstd::cerr << \"}\";\n}\n\ntemplate <typename T, typename U>\nvoid view(const std::vector<std::pair<T, U>> &v) {\n\tstd::cerr << \"{\\n\";\n\tfor(const auto &c : v) {\n\t\tstd::cerr << \"\\t(\";\n\t\tview(c.first);\n\t\tstd::cerr << \", \";\n\t\tview(c.second);\n\t\tstd::cerr << \")\\n\";\n\t}\n\tstd::cerr << \"}\";\n}\n\ntemplate <typename T, typename U>\nvoid view(const std::map<T, U> &m) {\n\tstd::cerr << \"{\\n\";\n\tfor(const auto &t : m) {\n\t\tstd::cerr << \"\\t[\";\n\t\tview(t.first);\n\t\tstd::cerr << \"] : \";\n\t\tview(t.second);\n\t\tstd::cerr << '\\n';\n\t}\n\tstd::cerr << \"}\";\n}\n\ntemplate <typename T, typename U>\nvoid view(const std::unordered_map<T, U> &m) {\n\tstd::cerr << \"{\\n\";\n\tfor(const auto &t : m) {\n\t\tstd::cerr << \"\\t[\";\n\t\tview(t.first);\n\t\tstd::cerr << \"] : \";\n\t\tview(t.second);\n\t\tstd::cerr << '\\n';\n\t}\n\tstd::cerr << \"}\";\n}\n\n#pragma endregion\n\n// when debugging : g++ foo.cpp -DLOCAL\n#ifdef LOCAL\nvoid debug_out() {}\ntemplate <typename Head, typename... Tail>\nvoid debug_out(Head H, Tail... T) {\n\tview(H);\n\tstd::cerr << \", \";\n\tdebug_out(T...);\n}\n#define debug(...) \\\n\tdo { \\\n\t\tstd::cerr << __LINE__ << \" [\" << #__VA_ARGS__ << \"] : [\"; \\\n\t\tdebug_out(__VA_ARGS__); \\\n\t\tstd::cerr << \"\\b\\b]\\n\"; \\\n\t} while(0)\n#define dump(x) \\\n\tdo { \\\n\t\tstd::cerr << __LINE__ << \" \" << #x << \" : \"; \\\n\t\tview(x); \\\n\t\tstd::cerr << '\\n'; \\\n\t} while(0)\n\n#else\n#define debug(...) (void(0))\n#define dump(x) (void(0))\n#endif\n\n#pragma endregion\n\n#pragma region geometry\n\nusing Real = long double; //時間がかかる割にdoubleと大差ない\nusing Point = complex<Real>;\nconst Real EPS = 1e-10, /*1e-8はたまに落ちる*/ PI = acos(-1);\n\ninline bool eq(Real a, Real b) { return fabs(b - a) < EPS; } //誤差無視の等式\n\n//実スカラー倍\nPoint operator*(const Point &p, const Real &d) { return Point(real(p) * d, imag(p) * d); }\n\n// point入力受け取り\nistream &operator>>(istream &is, Point &p) {\n\tReal a, b;\n\tis >> a >> b;\n\tp = Point(a, b);\n\treturn is;\n}\n// point出力\nostream &operator<<(ostream &os, Point &p) { return os << fixed << setprecision(10) << p.real() << \" \" << p.imag(); }\n\n// 点 p を反時計回りに theta 回転\nPoint rotate(Real theta, const Point &p) {\n\treturn Point(cos(theta) * p.real() - sin(theta) * p.imag(), sin(theta) * p.real() + cos(theta) * p.imag());\n}\n\nReal radian_to_degree(Real r) { return (r * 180.0 / PI); }\n\nReal degree_to_radian(Real d) { return (d * PI / 180.0); }\n\n// a-b-c の角度のうち小さい方を返す\nReal get_angle(const Point &a, const Point &b, const Point &c) {\n\tconst Point v(a - b), /* もとはv(b-a)になってたので修正 */ w(c - b);\n\tReal alpha = atan2(v.imag(), v.real()), beta = atan2(w.imag(), w.real());\n\tif(alpha > beta) swap(alpha, beta);\n\tReal theta = (beta - alpha);\n\treturn min(theta, 2 * acos(-1) - theta);\n}\n\nnamespace std {\nbool operator<(const Point &a, const Point &b) { return a.real() != b.real() ? a.real() < b.real() : a.imag() < b.imag(); }\n} // namespace std\n\nstruct Line {\n\tPoint a, b;\n\n\tLine() = default;\n\n\tLine(Point a, Point b) : a(a), b(b) {}\n\n\tLine(Real A, Real B, Real C) // Ax + By = C\n\t{\n\t\tif(eq(A, 0))\n\t\t\ta = Point(0, C / B), b = Point(1, C / B);\n\t\telse if(eq(B, 0))\n\t\t\tb = Point(C / A, 0), b = Point(C / A, 1);\n\t\telse\n\t\t\ta = Point(0, C / B), b = Point(C / A, 0);\n\t}\n\n\tfriend ostream &operator<<(ostream &os, Line &p) { return os << p.a << \" to \" << p.b; }\n\n\tfriend istream &operator>>(istream &is, Line &a) { return is >> a.a >> a.b; }\n};\n\nstruct Segment : Line {\n\tSegment() = default;\n\n\tSegment(Point a, Point b) : Line(a, b) {}\n};\n\nstruct Circle {\n\tPoint p;\n\tReal r;\n\n\tCircle() = default;\n\n\tCircle(Point p, Real r) : p(p), r(r) {}\n};\n\nusing Points = vector<Point>;\nusing Polygon = vector<Point>;\nusing Segments = vector<Segment>;\nusing Lines = vector<Line>;\nusing Circles = vector<Circle>;\n\n//外積\nReal cross(const Point &a, const Point &b) { return real(a) * imag(b) - imag(a) * real(b); }\n//内積\nReal dot(const Point &a, const Point &b) { return real(a) * real(b) + imag(a) * imag(b); }\n\n// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_1_C\n// 点の回転方向\nint ccw(const Point &a, Point b, Point c) {\n\tb = b - a, c = c - a;\n\tif(cross(b, c) > EPS) return +1; // \"COUNTER_CLOCKWISE\"\n\tif(cross(b, c) < -EPS) return -1; // \"CLOCKWISE\"\n\tif(dot(b, c) < 0) return +2;\t // \"ONLINE_BACK\"\n\tif(norm(b) < norm(c)) return -2; // \"ONLINE_FRONT\"\n\treturn 0;\t\t\t\t\t\t // \"ON_SEGMENT\"\n}\n\n// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_2_A\n// 平行判定\nbool parallel(const Line &a, const Line &b) { return eq(cross(a.b - a.a, b.b - b.a), 0.0); }\n\n// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_2_A\n// 垂直判定\nbool orthogonal(const Line &a, const Line &b) { return eq(dot(a.a - a.b, b.a - b.b), 0.0); }\n\n// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_1_A\n// 射影\n// 直線 l に p から垂線を引いた交点を求める\nPoint projection(const Line &l, const Point &p) {\n\tdouble t = dot(p - l.a, l.a - l.b) / norm(l.a - l.b);\n\treturn l.a + (l.a - l.b) * t;\n}\n\nPoint projection(const Segment &l, const Point &p) {\n\tdouble t = dot(p - l.a, l.a - l.b) / norm(l.a - l.b);\n\treturn l.a + (l.a - l.b) * t;\n}\n\n// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_1_B\n// 反射\n// 直線 l を対称軸として点 p と線対称にある点を求める\nPoint reflection(const Line &l, const Point &p) { return p + (projection(l, p) - p) * 2.0; }\n\nbool intersect(const Line &l, const Point &p) { return abs(ccw(l.a, l.b, p)) != 1; }\n\nbool intersect(const Line &l, const Line &m) { return abs(cross(l.b - l.a, m.b - m.a)) > EPS || abs(cross(l.b - l.a, m.b - l.a)) < EPS; }\n\nbool intersect(const Segment &s, const Point &p) { return ccw(s.a, s.b, p) == 0; }\n\nbool intersect(const Line &l, const Segment &s) { return cross(l.b - l.a, s.a - l.a) * cross(l.b - l.a, s.b - l.a) < EPS; }\n\nReal distance(const Line &l, const Point &p);\n\nbool intersect(const Circle &c, const Line &l) { return distance(l, c.p) <= c.r + EPS; }\n\nbool intersect(const Circle &c, const Point &p) { return abs(abs(p - c.p) - c.r) < EPS; }\n\n// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_2_B\n// 線分同士の交差判定 (端点が他方の線分上にあるならば交差とみなす)\nbool intersect(const Segment &s, const Segment &t) {\n\treturn 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;\n}\n\nint intersect(const Circle &c, const Segment &l) {\n\tif(norm(projection(l, c.p) - c.p) - c.r * c.r > EPS) return 0;\n\tauto d1 = abs(c.p - l.a), d2 = abs(c.p - l.b);\n\tif(d1 < c.r + EPS && d2 < c.r + EPS) return 0;\n\tif(d1 < c.r - EPS && d2 > c.r + EPS || d1 > c.r + EPS && d2 < c.r - EPS) return 1;\n\tconst Point h = projection(l, c.p);\n\tif(dot(l.a - h, l.b - h) < 0) return 2;\n\treturn 0;\n}\n\n// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_7_A&lang=jp\nint intersect(Circle c1, Circle c2) {\n\tif(c1.r < c2.r) swap(c1, c2);\n\tReal d = abs(c1.p - c2.p);\n\tif(c1.r + c2.r < d) return 4;\t // 外に離れている\n\tif(eq(c1.r + c2.r, d)) return 3; // 外接\n\tif(c1.r - c2.r < d) return 2;\t // 交差\n\tif(eq(c1.r - c2.r, d)) return 1; // 内接\n\treturn 0;\t\t\t\t\t\t // 内包\n}\n\nReal distance(const Point &a, const Point &b) { return abs(a - b); }\n\nReal distance(const Line &l, const Point &p) { return abs(p - projection(l, p)); }\n\nReal distance(const Line &l, const Line &m) { return intersect(l, m) ? 0 : distance(l, m.a); }\n\nReal distance(const Segment &s, const Point &p) {\n\tPoint r = projection(s, p);\n\tif(intersect(s, r)) return abs(r - p);\n\treturn min(abs(s.a - p), abs(s.b - p));\n}\n\n// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_2_D\nReal distance(const Segment &a, const Segment &b) {\n\tif(intersect(a, b)) return 0;\n\treturn min({distance(a, b.a), distance(a, b.b), distance(b, a.a), distance(b, a.b)});\n}\n\nReal distance(const Line &l, const Segment &s) {\n\tif(intersect(l, s)) return 0;\n\treturn min(distance(l, s.a), distance(l, s.b));\n}\n\nPoint crosspoint(const Line &l, const Line &m) {\n\tReal A = cross(l.b - l.a, m.b - m.a);\n\tReal B = cross(l.b - l.a, l.b - m.a);\n\tif(eq(abs(A), 0.0) && eq(abs(B), 0.0)) return m.a;\n\treturn m.a + (m.b - m.a) * B / A;\n}\n\n// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_2_C\nPoint crosspoint(const Segment &l, const Segment &m) { return crosspoint(Line(l), Line(m)); }\n\n// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_7_D\npair<Point, Point> crosspoint(const Circle &c, const Line l) {\n\tPoint pr = projection(l, c.p);\n\tPoint e = (l.b - l.a) / abs(l.b - l.a);\n\tif(eq(distance(l, c.p), c.r)) return {pr, pr};\n\tdouble base = sqrt(c.r * c.r - norm(pr - c.p));\n\treturn {pr - e * base, pr + e * base};\n}\n\npair<Point, Point> crosspoint(const Circle &c, const Segment &l) {\n\tLine aa = Line(l.a, l.b);\n\tif(intersect(c, l) == 2) return crosspoint(c, aa);\n\tauto ret = crosspoint(c, aa);\n\tif(dot(l.a - ret.first, l.b - ret.first) < 0)\n\t\tret.second = ret.first;\n\telse\n\t\tret.first = ret.second;\n\treturn ret;\n}\n\n// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_7_E\npair<Point, Point> crosspoint(const Circle &c1, const Circle &c2) {\n\tReal d = abs(c1.p - c2.p);\n\tReal a = acos((c1.r * c1.r + d * d - c2.r * c2.r) / (2 * c1.r * d)); // 余弦定理\n\tReal t = atan2(c2.p.imag() - c1.p.imag(), c2.p.real() - c1.p.real());\n\tPoint p1 = c1.p + Point(cos(t + a) * c1.r, sin(t + a) * c1.r);\n\tPoint p2 = c1.p + Point(cos(t - a) * c1.r, sin(t - a) * c1.r);\n\treturn {p1, p2};\n}\n\n// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_7_F\n// \"点 p を通る円 c の接線\"の接点2つ\npair<Point, Point> tangent(const Circle &c1, const Point &p2) { return crosspoint(c1, Circle(p2, sqrt(norm(c1.p - p2) - c1.r * c1.r))); }\n\n// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_7_G\n// 円 c1, c2 の共通接線\nLines tangent(Circle c1, Circle c2) {\n\tLines ret;\n\tif(c1.r < c2.r) swap(c1, c2);\n\tReal g = norm(c1.p - c2.p);\n\tif(eq(g, 0)) return ret;\n\tPoint u = (c2.p - c1.p) / sqrt(g);\n\tPoint v = rotate(PI * 0.5, u);\n\tfor(int s : {-1, 1}) {\n\t\tReal h = (c1.r + s * c2.r) / sqrt(g);\n\t\tif(eq(1 - h * h, 0)) {\n\t\t\tret.emplace_back(c1.p + u * c1.r, c1.p + (u + v) * c1.r);\n\t\t} else if(1 - h * h > 0) {\n\t\t\tPoint uu = u * h, vv = v * sqrt(1 - h * h);\n\t\t\tret.emplace_back(c1.p + (uu + vv) * c1.r, c2.p - (uu + vv) * c2.r * s);\n\t\t\tret.emplace_back(c1.p + (uu - vv) * c1.r, c2.p - (uu - vv) * c2.r * s);\n\t\t}\n\t}\n\treturn ret;\n}\n\n// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_3_B\n// 凸性判定 (反時計回り)\nbool is_convex(const Polygon &p) {\n\tint n = (int)p.size();\n\tfor(int i = 0; i < n; i++) {\n\t\tif(ccw(p[(i + n - 1) % n], p[i], p[(i + 1) % n]) == -1) return false;\n\t}\n\treturn true;\n}\n\n// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_4_A\n// 凸包\nPolygon convex_hull(Polygon &p) {\n\tint n = (int)p.size(), k = 0;\n\tif(n <= 2) return p;\n\tsort(p.begin(), p.end()); // x -> yの順にソート\n\tvector<Point> ch(2 * n);\n\tfor(int i = 0; i < n; ch[k++] = p[i++]) { //下半分を構成\n\t\twhile(k >= 2 && cross(ch[k - 1] - ch[k - 2], p[i] - ch[k - 1]) < EPS) --k;\n\t}\n\tfor(int i = n - 2, t = k + 1; i >= 0; ch[k++] = p[i--]) { //上半分を構成\n\t\twhile(k >= t && cross(ch[k - 1] - ch[k - 2], p[i] - ch[k - 1]) < EPS) --k;\n\t}\n\tch.resize(k - 1);\n\treturn ch;\n}\n\n// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_3_C\n// 多角形と点の包含判定\nenum { OUT, ON, IN };\n\nint contains(const Polygon &Q, const Point &p) {\n\tbool in = false;\n\tfor(int i = 0; i < Q.size(); i++) {\n\t\tPoint a = Q[i] - p, b = Q[(i + 1) % Q.size()] - p;\n\t\tif(a.imag() > b.imag()) swap(a, b);\n\t\tif(a.imag() <= 0 && 0 < b.imag() && cross(a, b) < 0) in = !in;\n\t\tif(cross(a, b) == 0 && dot(a, b) <= 0) return ON;\n\t}\n\treturn in ? IN : OUT;\n}\n\n// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=1033\n// 線分の重複除去\nvoid merge_segments(vector<Segment> &segs) {\n\tauto merge_if_able = [](Segment &s1, const Segment &s2) {\n\t\tif(abs(cross(s1.b - s1.a, s2.b - s2.a)) > EPS) return false;\n\t\tif(ccw(s1.a, s2.a, s1.b) == 1 || ccw(s1.a, s2.a, s1.b) == -1) return false;\n\t\tif(ccw(s1.a, s1.b, s2.a) == -2 || ccw(s2.a, s2.b, s1.a) == -2) return false;\n\t\ts1 = Segment(min(s1.a, s2.a), max(s1.b, s2.b));\n\t\treturn true;\n\t};\n\n\tfor(int i = 0; i < segs.size(); i++) {\n\t\tif(segs[i].b < segs[i].a) swap(segs[i].a, segs[i].b);\n\t}\n\tfor(int i = 0; i < segs.size(); i++) {\n\t\tfor(int j = i + 1; j < segs.size(); j++) {\n\t\t\tif(merge_if_able(segs[i], segs[j])) {\n\t\t\t\tsegs[j--] = segs.back(), segs.pop_back();\n\t\t\t}\n\t\t}\n\t}\n}\n\n// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=1033\n// 線分アレンジメント\n// 任意の2線分の交点を頂点としたグラフを構築する\nvector<vector<int>> segment_arrangement(vector<Segment> &segs, vector<Point> &ps) {\n\tvector<vector<int>> g;\n\tint N = (int)segs.size();\n\tfor(int i = 0; i < N; i++) {\n\t\tps.emplace_back(segs[i].a);\n\t\tps.emplace_back(segs[i].b);\n\t\tfor(int j = i + 1; j < N; j++) {\n\t\t\tconst Point p1 = segs[i].b - segs[i].a;\n\t\t\tconst Point p2 = segs[j].b - segs[j].a;\n\t\t\tif(eq(cross(p1, p2), 0)) continue;\t// \" == 0\" となっていたのを訂正\n\t\t\tif(intersect(segs[i], segs[j])) {\n\t\t\t\tps.emplace_back(crosspoint(segs[i], segs[j]));\n\t\t\t}\n\t\t}\n\t}\n\tsort(begin(ps), end(ps));\n\tps.erase(unique(begin(ps), end(ps)), end(ps));\t// これ誤差とか大丈夫なのか?\n\n\tint M = (int)ps.size();\n\tg.resize(M);\n\tfor(int i = 0; i < N; i++) {\n\t\tvector<int> vec;\n\t\tfor(int j = 0; j < M; j++) {\n\t\t\tif(intersect(segs[i], ps[j])) {\n\t\t\t\tvec.emplace_back(j);\n\t\t\t}\n\t\t}\n\t\tfor(int j = 1; j < vec.size(); j++) {\n\t\t\tg[vec[j - 1]].push_back(vec[j]);\n\t\t\tg[vec[j]].push_back(vec[j - 1]);\n\t\t}\n\t}\n\treturn (g);\n}\n\n// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_4_C\n// 凸多角形の切断\n// 直線 l.a-l.b で切断しその左側にできる凸多角形を返す\nPolygon convex_cut(const Polygon &U, Line l) {\n\tPolygon ret;\n\tfor(int i = 0; i < U.size(); i++) {\n\t\tPoint now = U[i], nxt = U[(i + 1) % U.size()];\n\t\tif(ccw(l.a, l.b, now) != -1) ret.push_back(now);\n\t\tif(ccw(l.a, l.b, now) * ccw(l.a, l.b, nxt) < 0) {\n\t\t\tret.push_back(crosspoint(Line(now, nxt), l));\n\t\t}\n\t}\n\treturn (ret);\n}\n\n// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_3_A\n// 多角形の面積\nReal area(const Polygon &p) {\n\tReal A = 0;\n\tfor(int i = 0; i < p.size(); ++i) {\n\t\tA += cross(p[i], p[(i + 1) % p.size()]);\n\t}\n\treturn A * 0.5;\n}\n\n// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_7_H\n// 円と多角形の共通部分の面積\nReal area(const Polygon &p, const Circle &c) {\n\tif(p.size() < 3) return 0.0;\n\tfunction<Real(Circle, Point, Point)> cross_area = [&](const Circle &c, const Point &a, const Point &b) {\n\t\tPoint va = c.p - a, vb = c.p - b;\n\t\tReal f = cross(va, vb), ret = 0.0;\n\t\tif(eq(f, 0.0)) return ret;\n\t\tif(max(abs(va), abs(vb)) < c.r + EPS) return f;\n\t\tif(distance(Segment(a, b), c.p) > c.r - EPS) return c.r * c.r * arg(vb * conj(va));\t // 0.5倍してなくない?\n\t\tauto u = crosspoint(c, Segment(a, b));\n\t\tvector<Point> tot{a, u.first, u.second, b};\n\t\tfor(int i = 0; i + 1 < tot.size(); i++) {\n\t\t\tret += cross_area(c, tot[i], tot[i + 1]);\n\t\t}\n\t\treturn ret;\n\t};\n\tReal A = 0;\n\tfor(int i = 0; i < p.size(); i++) {\n\t\tA += cross_area(c, p[i], p[(i + 1) % p.size()]);\n\t}\n\treturn A * 0.5;\t // 0.5倍が抜けていたので修正;\n}\n\n// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_4_B\n// 凸多角形の直径(最遠頂点対間距離)\nReal convex_diameter(const Polygon &p) {\n\tint N = (int)p.size();\n\tint is = 0, js = 0;\n\tfor(int i = 1; i < N; i++) {\n\t\tif(p[i].imag() > p[is].imag()) is = i;\n\t\tif(p[i].imag() < p[js].imag()) js = i;\n\t}\n\tReal maxdis = norm(p[is] - p[js]);\n\n\tint maxi, maxj, i, j;\n\ti = maxi = is;\n\tj = maxj = js;\n\tdo {\n\t\tif(cross(p[(i + 1) % N] - p[i], p[(j + 1) % N] - p[j]) >= 0) {\n\t\t\tj = (j + 1) % N;\n\t\t} else {\n\t\t\ti = (i + 1) % N;\n\t\t}\n\t\tif(norm(p[i] - p[j]) > maxdis) {\n\t\t\tmaxdis = norm(p[i] - p[j]);\n\t\t\tmaxi = i;\n\t\t\tmaxj = j;\n\t\t}\n\t} while(i != is || j != js);\n\treturn sqrt(maxdis);\n}\n\n// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_5_A\n// 最近点対\nReal closest_pair(Points ps) {\n\tif(ps.size() <= 1) throw(0);\n\tsort(begin(ps), end(ps)); // xでソート\n\n\tauto compare_y = [&](const Point &a, const Point &b) { return imag(a) < imag(b); };\n\tvector<Point> beet(ps.size());\n\tconst Real INF = 1e18;\n\n\tfunction<Real(int, int)> rec = [&](int left, int right) {\n\t\tif(right - left <= 1) return INF;\n\t\tint mid = (left + right) >> 1;\n\t\tauto x = real(ps[mid]);\t // 領域内でx座標真中の点\n\t\tauto ret = min(rec(left, mid), rec(mid, right));\n\t\t// y座標でソートされた2つの区間をマージ\n\t\tinplace_merge(begin(ps) + left, begin(ps) + mid, begin(ps) + right, compare_y);\n\t\tint ptr = 0;\n\t\tfor(int i = left; i < right; i++) {\n\t\t\tif(abs(real(ps[i]) - x) >= ret) continue;\n\t\t\tfor(int j = 0; j < ptr; j++) {\n\t\t\t\tauto luz = ps[i] - beet[ptr - j - 1];\n\t\t\t\tif(imag(luz) >= ret) break;\n\t\t\t\tret = min(ret, abs(luz));\n\t\t\t}\n\t\t\tbeet[ptr++] = ps[i];\n\t\t}\n\t\treturn ret;\n\t};\n\treturn rec(0, (int)ps.size());\n}\n\n// (aからの距離):(bからの距離) = dist_a : dist_b となる点の集合(円)を返す\nCircle Apollonius_Circle(Point a, Point b, Real dist_a, Real dist_b) {\n\tassert(!eq(dist_a, dist_b)); //同じ場合はbisectionで\n\tReal asq = dist_a * dist_a, bsq = dist_b * dist_b;\n\tPoint p = (a * bsq - b * asq) * (1 / (bsq - asq));\n\tReal r = abs(a - b) * dist_a * dist_b / abs(asq - bsq);\n\treturn Circle(p, r);\n}\n\n// 二等分線\nLine bisection(Point a, Point b) {\n\ta = (a + b) * 0.5;\n\tb = rotate(PI / 2, b - a) + a;\n\treturn Line(a, b);\n}\n\n#pragma endregion\n\nusing R = Real;\n\nbool left_is_smaller(R left, R right) { return left < right - EPS; }\n\nvoid sol() {\n\tint n, w;\n\tcin >> n >> w;\n\tif(n + w == 0) exit(0);\n\n\tPoints p(n);\n\tV<Real> h(n);\n\trep(i, n) cin >> p[i] >> h[i];\n\n\tV<Real> r(n);\n\tReal lef, dn, up, rig;\n\tconst Real sz = 100.0;\n\n\tauto judge = [&](Real sphere) {\n\t\trep(i, n) {\n\t\t\tif(h[i] > sphere) {\n\t\t\t\tr[i] = sphere;\n\t\t\t} else {\n\t\t\t\tr[i] = sqrt(pow(sphere, 2) - pow(sphere - h[i], 2));\n\t\t\t}\n\t\t}\n\t\tR margin = sphere;\n\t\tif(sphere > w) {\n\t\t\tmargin = sqrt(pow(sphere, 2) - pow(sphere - w, 2));\n\t\t}\n\n\t\tlef = margin;\n\t\trig = 100 - margin;\n\t\tup = 100 - margin;\n\t\tdn = margin;\n\n\t\tauto safe = [&](Real x, Real y) {\n\t\t\tif(x < lef - EPS || x > rig + EPS) return false;\n\t\t\tif(y < dn - EPS || y > up + EPS) return false;\n\n\t\t\trep(i, n) {\n\t\t\t\tif(distance(p[i], Point(x, y)) < r[i] - EPS) {\n\t\t\t\t\treturn false;\n\t\t\t\t}\n\t\t\t}\n\n\t\t\treturn true;\n\t\t};\n\n\t\tauto safepoint = [&](Point p) { return safe(p.real(), p.imag()); };\n\n\t\tif(safe(lef, up) || safe(lef, dn) || safe(rig, up) || safe(rig, dn)) {\n\t\t\treturn true;\n\t\t}\n\n\t\tPoints square(4);\n\t\tsquare[0] = Point(lef, up);\n\t\tsquare[1] = Point(lef, dn);\n\t\tsquare[2] = Point(rig, dn);\n\t\tsquare[3] = Point(rig, up);\n\n\t\trep(i, n) Rep(j, i + 1, n) {\n\t\t\tint is = intersect(Circle(p[i], r[i]), Circle(p[j], r[j]));\n\t\t\tif(is == 4 || is == 0) continue;\n\n\t\t\tauto pr = crosspoint(Circle(p[i], r[i]), Circle(p[j], r[j]));\n\t\t\tif(safepoint(pr.first) || safepoint(pr.second)) return true;\n\t\t}\n\n\t\trep(i, n) rep(j, 4) {\n\t\t\tif(intersect(Circle(p[i], r[i]), Line(square[j], square[(j + 1) % 4]))) {\n\t\t\t\tauto pr = (crosspoint(Circle(p[i], r[i]), Line(square[j], square[(j + 1) % 4])));\n\t\t\t\tif(safepoint(pr.first) || safepoint(pr.second)) return true;\n\t\t\t}\n\t\t}\n\n\t\treturn false;\n\t};\n\n\tR ok = 0.0;\n\tR ng = 10000000000;\n\trep(_, 1000) {\n\t\tR med = (ok + ng) / 2;\n\t\tif(judge(med)) {\n\t\t\tok = med;\n\t\t} else {\n\t\t\tng = med;\n\t\t}\n\t}\n\tcout << ok << dl;\n\treturn;\n}\n\nint main() {\n\tios::sync_with_stdio(false);\n\tcin.tie(nullptr);\n\tcout << fixed << setprecision(15);\n\tsrand((unsigned)time(NULL));\n\n\twhile(1) sol();\n\n\treturn 0;\n}", "accuracy": 1, "time_ms": 6980, "memory_kb": 3924, "score_of_the_acc": -1.6658, "final_rank": 20 }, { "submission_id": "aoj_1342_5032173", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\nusing Real = double; //時間がかかる割にdoubleと大差ない\nusing Point = complex<Real>;\nconst Real EPS = 1e-12, /*1e-8はたまに落ちる*/ PI = acos(-1);\n\ninline bool eq(Real a, Real b) { return fabs(b - a) < EPS; } //誤差無視の等式\n\n//実スカラー倍\nPoint operator*(const Point &p, const Real &d) { return Point(real(p) * d, imag(p) * d); }\n\n// point入力受け取り\nistream &operator>>(istream &is, Point &p) {\n\tReal a, b;\n\tis >> a >> b;\n\tp = Point(a, b);\n\treturn is;\n}\n// point出力\nostream &operator<<(ostream &os, Point &p) { return os << fixed << setprecision(10) << p.real() << \" \" << p.imag(); }\n\n// 点 p を反時計回りに theta 回転\nPoint rotate(Real theta, const Point &p) {\n\treturn Point(cos(theta) * p.real() - sin(theta) * p.imag(), sin(theta) * p.real() + cos(theta) * p.imag());\n}\n\nReal radian_to_degree(Real r) { return (r * 180.0 / PI); }\n\nReal degree_to_radian(Real d) { return (d * PI / 180.0); }\n\n// a-b-c の角度のうち小さい方を返す\nReal get_angle(const Point &a, const Point &b, const Point &c) {\n\tconst Point v(a - b), /* もとはv(b-a)になってたので修正 */ w(c - b);\n\tReal alpha = atan2(v.imag(), v.real()), beta = atan2(w.imag(), w.real());\n\tif(alpha > beta) swap(alpha, beta);\n\tReal theta = (beta - alpha);\n\treturn min(theta, 2 * acos(-1) - theta);\n}\n\nnamespace std {\nbool operator<(const Point &a, const Point &b) { return a.real() != b.real() ? a.real() < b.real() : a.imag() < b.imag(); }\n} // namespace std\n\nstruct Line {\n\tPoint a, b;\n\n\tLine() = default;\n\n\tLine(Point a, Point b) : a(a), b(b) {}\n\n\tLine(Real A, Real B, Real C) // Ax + By = C\n\t{\n\t\tif(eq(A, 0))\n\t\t\ta = Point(0, C / B), b = Point(1, C / B);\n\t\telse if(eq(B, 0))\n\t\t\tb = Point(C / A, 0), b = Point(C / A, 1);\n\t\telse\n\t\t\ta = Point(0, C / B), b = Point(C / A, 0);\n\t}\n\n\tfriend ostream &operator<<(ostream &os, Line &p) { return os << p.a << \" to \" << p.b; }\n\n\tfriend istream &operator>>(istream &is, Line &a) { return is >> a.a >> a.b; }\n};\n\nstruct Segment : Line {\n\tSegment() = default;\n\n\tSegment(Point a, Point b) : Line(a, b) {}\n};\n\nstruct Circle {\n\tPoint p;\n\tReal r;\n\n\tCircle() = default;\n\n\tCircle(Point p, Real r) : p(p), r(r) {}\n};\n\nusing Points = vector<Point>;\nusing Polygon = vector<Point>;\nusing Segments = vector<Segment>;\nusing Lines = vector<Line>;\nusing Circles = vector<Circle>;\n\n//外積\nReal cross(const Point &a, const Point &b) { return real(a) * imag(b) - imag(a) * real(b); }\n//内積\nReal dot(const Point &a, const Point &b) { return real(a) * real(b) + imag(a) * imag(b); }\n\n// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_1_C\n// 点の回転方向\nint ccw(const Point &a, Point b, Point c) {\n\tb = b - a, c = c - a;\n\tif(cross(b, c) > EPS) return +1; // \"COUNTER_CLOCKWISE\"\n\tif(cross(b, c) < -EPS) return -1; // \"CLOCKWISE\"\n\tif(dot(b, c) < 0) return +2; // \"ONLINE_BACK\"\n\tif(norm(b) < norm(c)) return -2; // \"ONLINE_FRONT\"\n\treturn 0; // \"ON_SEGMENT\"\n}\n\n// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_2_A\n// 平行判定\nbool parallel(const Line &a, const Line &b) { return eq(cross(a.b - a.a, b.b - b.a), 0.0); }\n\n// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_2_A\n// 垂直判定\nbool orthogonal(const Line &a, const Line &b) { return eq(dot(a.a - a.b, b.a - b.b), 0.0); }\n\n// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_1_A\n// 射影\n// 直線 l に p から垂線を引いた交点を求める\nPoint projection(const Line &l, const Point &p) {\n\tdouble t = dot(p - l.a, l.a - l.b) / norm(l.a - l.b);\n\treturn l.a + (l.a - l.b) * t;\n}\n\nPoint projection(const Segment &l, const Point &p) {\n\tdouble t = dot(p - l.a, l.a - l.b) / norm(l.a - l.b);\n\treturn l.a + (l.a - l.b) * t;\n}\n\n// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_1_B\n// 反射\n// 直線 l を対称軸として点 p と線対称にある点を求める\nPoint reflection(const Line &l, const Point &p) { return p + (projection(l, p) - p) * 2.0; }\n\nbool intersect(const Line &l, const Point &p) { return abs(ccw(l.a, l.b, p)) != 1; }\n\nbool intersect(const Line &l, const Line &m) { return abs(cross(l.b - l.a, m.b - m.a)) > EPS || abs(cross(l.b - l.a, m.b - l.a)) < EPS; }\n\nbool intersect(const Segment &s, const Point &p) { return ccw(s.a, s.b, p) == 0; }\n\nbool intersect(const Line &l, const Segment &s) { return cross(l.b - l.a, s.a - l.a) * cross(l.b - l.a, s.b - l.a) < EPS; }\n\nReal distance(const Line &l, const Point &p);\n\nbool intersect(const Circle &c, const Line &l) { return distance(l, c.p) <= c.r + EPS; }\n\nbool intersect(const Circle &c, const Point &p) { return abs(abs(p - c.p) - c.r) < EPS; }\n\n// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_2_B\n// 線分同士の交差判定 (端点が他方の線分上にあるならば交差とみなす)\nbool intersect(const Segment &s, const Segment &t) {\n\treturn 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;\n}\n\nint intersect(const Circle &c, const Segment &l) {\n\tif(norm(projection(l, c.p) - c.p) - c.r * c.r > EPS) return 0;\n\tauto d1 = abs(c.p - l.a), d2 = abs(c.p - l.b);\n\tif(d1 < c.r + EPS && d2 < c.r + EPS) return 0;\n\tif(d1 < c.r - EPS && d2 > c.r + EPS || d1 > c.r + EPS && d2 < c.r - EPS) return 1;\n\tconst Point h = projection(l, c.p);\n\tif(dot(l.a - h, l.b - h) < 0) return 2;\n\treturn 0;\n}\n\n// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_7_A&lang=jp\nint intersect(Circle c1, Circle c2) {\n\tif(c1.r < c2.r) swap(c1, c2);\n\tReal d = abs(c1.p - c2.p);\n\tif(c1.r + c2.r < d) return 0; // 外に離れている\n\tif(eq(c1.r + c2.r, d)) return 3; // 外接\n\tif(c1.r - c2.r < d) return 2; // 交差\n\tif(eq(c1.r - c2.r, d)) return 1; // 内接\n\treturn 0; // 内包\n}\n\nReal distance(const Point &a, const Point &b) { return abs(a - b); }\n\nReal distance(const Line &l, const Point &p) { return abs(p - projection(l, p)); }\n\nReal distance(const Line &l, const Line &m) { return intersect(l, m) ? 0 : distance(l, m.a); }\n\nReal distance(const Segment &s, const Point &p) {\n\tPoint r = projection(s, p);\n\tif(intersect(s, r)) return abs(r - p);\n\treturn min(abs(s.a - p), abs(s.b - p));\n}\n\n// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_2_D\nReal distance(const Segment &a, const Segment &b) {\n\tif(intersect(a, b)) return 0;\n\treturn min({distance(a, b.a), distance(a, b.b), distance(b, a.a), distance(b, a.b)});\n}\n\nReal distance(const Line &l, const Segment &s) {\n\tif(intersect(l, s)) return 0;\n\treturn min(distance(l, s.a), distance(l, s.b));\n}\n\nPoint crosspoint(const Line &l, const Line &m) {\n\tReal A = cross(l.b - l.a, m.b - m.a);\n\tReal B = cross(l.b - l.a, l.b - m.a);\n\tif(eq(abs(A), 0.0) && eq(abs(B), 0.0)) return m.a;\n\treturn m.a + (m.b - m.a) * B / A;\n}\n\n// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_2_C\nPoint crosspoint(const Segment &l, const Segment &m) { return crosspoint(Line(l), Line(m)); }\n\n// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_7_D\npair<Point, Point> crosspoint(const Circle &c, const Line l) {\n\tPoint pr = projection(l, c.p);\n\tPoint e = (l.b - l.a) / abs(l.b - l.a);\n\tif(eq(distance(l, c.p), c.r)) return {pr, pr};\n\tdouble base = sqrt(c.r * c.r - norm(pr - c.p));\n\treturn {pr - e * base, pr + e * base};\n}\n\npair<Point, Point> crosspoint(const Circle &c, const Segment &l) {\n\tLine aa = Line(l.a, l.b);\n\tif(intersect(c, l) == 2) return crosspoint(c, aa);\n\tauto ret = crosspoint(c, aa);\n\tif(dot(l.a - ret.first, l.b - ret.first) < 0)\n\t\tret.second = ret.first;\n\telse\n\t\tret.first = ret.second;\n\treturn ret;\n}\n\n// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_7_E\npair<Point, Point> crosspoint(const Circle &c1, const Circle &c2) {\n\tReal d = abs(c1.p - c2.p);\n\tReal a = acos((c1.r * c1.r + d * d - c2.r * c2.r) / (2 * c1.r * d)); // 余弦定理\n\tReal t = atan2(c2.p.imag() - c1.p.imag(), c2.p.real() - c1.p.real());\n\tPoint p1 = c1.p + Point(cos(t + a) * c1.r, sin(t + a) * c1.r);\n\tPoint p2 = c1.p + Point(cos(t - a) * c1.r, sin(t - a) * c1.r);\n\treturn {p1, p2};\n}\n\n// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_7_F\n// \"点 p を通る円 c の接線\"の接点2つ\npair<Point, Point> tangent(const Circle &c1, const Point &p2) { return crosspoint(c1, Circle(p2, sqrt(norm(c1.p - p2) - c1.r * c1.r))); }\n\n// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_7_G\n// 円 c1, c2 の共通接線\nLines tangent(Circle c1, Circle c2) {\n\tLines ret;\n\tif(c1.r < c2.r) swap(c1, c2);\n\tReal g = norm(c1.p - c2.p);\n\tif(eq(g, 0)) return ret;\n\tPoint u = (c2.p - c1.p) / sqrt(g);\n\tPoint v = rotate(PI * 0.5, u);\n\tfor(int s : {-1, 1}) {\n\t\tReal h = (c1.r + s * c2.r) / sqrt(g);\n\t\tif(eq(1 - h * h, 0)) {\n\t\t\tret.emplace_back(c1.p + u * c1.r, c1.p + (u + v) * c1.r);\n\t\t} else if(1 - h * h > 0) {\n\t\t\tPoint uu = u * h, vv = v * sqrt(1 - h * h);\n\t\t\tret.emplace_back(c1.p + (uu + vv) * c1.r, c2.p - (uu + vv) * c2.r * s);\n\t\t\tret.emplace_back(c1.p + (uu - vv) * c1.r, c2.p - (uu - vv) * c2.r * s);\n\t\t}\n\t}\n\treturn ret;\n}\n\nvoid solve() {\n\tint n, m;\n\tcin >> n >> m;\n\tif(!n) exit(0);\n\n\tPoints p(n);\n\tvector<Real> h(n);\n\t//input\n\tfor(int i = 0; i < n; i++)\n\t\tcin >> p[i] >> h[i];\n\n\t//二分探索の判定関数\n\tauto judge = [&](Real rad) {\n\t\t//確実に置けない円\n\t\tCircles Cir(n);\n\t\tfor(int i = 0; i < n; i++) {\n\t\t\tif(h[i] >= rad)\n\t\t\t\tCir[i].r = rad;\n\t\t\telse\n\t\t\t\tCir[i].r = sqrt(h[i] * rad * 2 - h[i] * h[i]);\n\t\t\tCir[i].p = p[i];\n\t\t}\n\n\t\tReal blank = (m >= rad ? rad : sqrt(m * rad * 2 - m * m));\n\n\t\tif(blank > 50) return false;\n\n\t\tPoint a = {blank, blank}, b = {blank, 100 - blank}, c = {100 - blank, 100 - blank}, d = {100 - blank, blank};\n\n\t\tSegments SEG = {Segment(a, b), Segment(b, c), Segment(c, d), Segment(d, a)};\n\n\t\tPoints candidate = {a, b, c, d};\n\n\t\tfor(int i = 0; i < n; i++) {\n\t\t\t//円同士の交点を全列挙\n\t\t\tfor(int j = i + 1; j < n; j++) {\n\t\t\t\tif(intersect(Cir[i], Cir[j])) {\n\t\t\t\t\tauto [p1, p2] = crosspoint(Cir[i], Cir[j]);\n\t\t\t\t\tcandidate.push_back(p1);\n\t\t\t\t\tcandidate.push_back(p2);\n\t\t\t\t}\n\t\t\t}\n\t\t\t//円と正方形の交点を全列挙\n\t\t\tfor(int j = 0; j < 4; j++) {\n\t\t\t\tif(intersect(Cir[i], SEG[j])) {\n\t\t\t\t\tauto [p1, p2] = crosspoint(Cir[i], SEG[j]);\n\t\t\t\t\tcandidate.push_back(p1);\n\t\t\t\t\tcandidate.push_back(p2);\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\n\t\tauto satisfied = [&](Point p) {\n\t\t\tfor(auto c : Cir) {\n\t\t\t\tif(distance(p, c.p) + EPS < c.r) return false;\n\t\t\t}\n\t\t\tif(p.real() + EPS < blank) return false;\n\t\t\tif(p.imag() + EPS < blank) return false;\n\t\t\tif(p.real() - EPS > 100 - blank) return false;\n\t\t\tif(p.imag() - EPS > 100 - blank) return false;\n\t\t\treturn true;\n\t\t};\n\n\t\tfor(auto &p : candidate) \n\t\t\tif(satisfied(p)) return true;\n\t\t\n\t\treturn false;\n\t};\n\n\tReal ok = 0, ng = 200;\n\tfor(int i = 0; i < 25; i++) {\n\t\tReal mid = (ok + ng) / 2;\n\t\tif(judge(mid))\n\t\t\tok = mid;\n\t\telse\n\t\t\tng = mid;\n\t}\n\tcout << ok << '\\n';\n\treturn;\n}\n\nint main() {\n\tcout << fixed << setprecision(15);\n\twhile(1) solve();\n}", "accuracy": 1, "time_ms": 80, "memory_kb": 4076, "score_of_the_acc": -0.9493, "final_rank": 15 }, { "submission_id": "aoj_1342_5032144", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\nusing Real = double; //時間がかかる割にdoubleと大差ない\nusing Point = complex<Real>;\nconst Real EPS = 1e-12, /*1e-8はたまに落ちる*/ PI = acos(-1);\n\ninline bool eq(Real a, Real b) { return fabs(b - a) < EPS; } //誤差無視の等式\n\n//実スカラー倍\nPoint operator*(const Point &p, const Real &d) { return Point(real(p) * d, imag(p) * d); }\n\n// point入力受け取り\nistream &operator>>(istream &is, Point &p) {\n\tReal a, b;\n\tis >> a >> b;\n\tp = Point(a, b);\n\treturn is;\n}\n// point出力\nostream &operator<<(ostream &os, Point &p) { return os << fixed << setprecision(10) << p.real() << \" \" << p.imag(); }\n\n// 点 p を反時計回りに theta 回転\nPoint rotate(Real theta, const Point &p) {\n\treturn Point(cos(theta) * p.real() - sin(theta) * p.imag(), sin(theta) * p.real() + cos(theta) * p.imag());\n}\n\nReal radian_to_degree(Real r) { return (r * 180.0 / PI); }\n\nReal degree_to_radian(Real d) { return (d * PI / 180.0); }\n\n// a-b-c の角度のうち小さい方を返す\nReal get_angle(const Point &a, const Point &b, const Point &c) {\n\tconst Point v(a - b), /* もとはv(b-a)になってたので修正 */ w(c - b);\n\tReal alpha = atan2(v.imag(), v.real()), beta = atan2(w.imag(), w.real());\n\tif(alpha > beta) swap(alpha, beta);\n\tReal theta = (beta - alpha);\n\treturn min(theta, 2 * acos(-1) - theta);\n}\n\nnamespace std {\nbool operator<(const Point &a, const Point &b) { return a.real() != b.real() ? a.real() < b.real() : a.imag() < b.imag(); }\n} // namespace std\n\nstruct Line {\n\tPoint a, b;\n\n\tLine() = default;\n\n\tLine(Point a, Point b) : a(a), b(b) {}\n\n\tLine(Real A, Real B, Real C) // Ax + By = C\n\t{\n\t\tif(eq(A, 0))\n\t\t\ta = Point(0, C / B), b = Point(1, C / B);\n\t\telse if(eq(B, 0))\n\t\t\tb = Point(C / A, 0), b = Point(C / A, 1);\n\t\telse\n\t\t\ta = Point(0, C / B), b = Point(C / A, 0);\n\t}\n\n\tfriend ostream &operator<<(ostream &os, Line &p) { return os << p.a << \" to \" << p.b; }\n\n\tfriend istream &operator>>(istream &is, Line &a) { return is >> a.a >> a.b; }\n};\n\nstruct Segment : Line {\n\tSegment() = default;\n\n\tSegment(Point a, Point b) : Line(a, b) {}\n};\n\nstruct Circle {\n\tPoint p;\n\tReal r;\n\n\tCircle() = default;\n\n\tCircle(Point p, Real r) : p(p), r(r) {}\n};\n\nusing Points = vector<Point>;\nusing Polygon = vector<Point>;\nusing Segments = vector<Segment>;\nusing Lines = vector<Line>;\nusing Circles = vector<Circle>;\n\n//外積\nReal cross(const Point &a, const Point &b) { return real(a) * imag(b) - imag(a) * real(b); }\n//内積\nReal dot(const Point &a, const Point &b) { return real(a) * real(b) + imag(a) * imag(b); }\n\n// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_1_C\n// 点の回転方向\nint ccw(const Point &a, Point b, Point c) {\n\tb = b - a, c = c - a;\n\tif(cross(b, c) > EPS) return +1; // \"COUNTER_CLOCKWISE\"\n\tif(cross(b, c) < -EPS) return -1; // \"CLOCKWISE\"\n\tif(dot(b, c) < 0) return +2; // \"ONLINE_BACK\"\n\tif(norm(b) < norm(c)) return -2; // \"ONLINE_FRONT\"\n\treturn 0; // \"ON_SEGMENT\"\n}\n\n// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_2_A\n// 平行判定\nbool parallel(const Line &a, const Line &b) { return eq(cross(a.b - a.a, b.b - b.a), 0.0); }\n\n// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_2_A\n// 垂直判定\nbool orthogonal(const Line &a, const Line &b) { return eq(dot(a.a - a.b, b.a - b.b), 0.0); }\n\n// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_1_A\n// 射影\n// 直線 l に p から垂線を引いた交点を求める\nPoint projection(const Line &l, const Point &p) {\n\tdouble t = dot(p - l.a, l.a - l.b) / norm(l.a - l.b);\n\treturn l.a + (l.a - l.b) * t;\n}\n\nPoint projection(const Segment &l, const Point &p) {\n\tdouble t = dot(p - l.a, l.a - l.b) / norm(l.a - l.b);\n\treturn l.a + (l.a - l.b) * t;\n}\n\n// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_1_B\n// 反射\n// 直線 l を対称軸として点 p と線対称にある点を求める\nPoint reflection(const Line &l, const Point &p) { return p + (projection(l, p) - p) * 2.0; }\n\nbool intersect(const Line &l, const Point &p) { return abs(ccw(l.a, l.b, p)) != 1; }\n\nbool intersect(const Line &l, const Line &m) { return abs(cross(l.b - l.a, m.b - m.a)) > EPS || abs(cross(l.b - l.a, m.b - l.a)) < EPS; }\n\nbool intersect(const Segment &s, const Point &p) { return ccw(s.a, s.b, p) == 0; }\n\nbool intersect(const Line &l, const Segment &s) { return cross(l.b - l.a, s.a - l.a) * cross(l.b - l.a, s.b - l.a) < EPS; }\n\nReal distance(const Line &l, const Point &p);\n\nbool intersect(const Circle &c, const Line &l) { return distance(l, c.p) <= c.r + EPS; }\n\nbool intersect(const Circle &c, const Point &p) { return abs(abs(p - c.p) - c.r) < EPS; }\n\n// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_2_B\n// 線分同士の交差判定 (端点が他方の線分上にあるならば交差とみなす)\nbool intersect(const Segment &s, const Segment &t) {\n\treturn 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;\n}\n\nint intersect(const Circle &c, const Segment &l) {\n\tif(norm(projection(l, c.p) - c.p) - c.r * c.r > EPS) return 0;\n\tauto d1 = abs(c.p - l.a), d2 = abs(c.p - l.b);\n\tif(d1 < c.r + EPS && d2 < c.r + EPS) return 0;\n\tif(d1 < c.r - EPS && d2 > c.r + EPS || d1 > c.r + EPS && d2 < c.r - EPS) return 1;\n\tconst Point h = projection(l, c.p);\n\tif(dot(l.a - h, l.b - h) < 0) return 2;\n\treturn 0;\n}\n\n// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_7_A&lang=jp\nint intersect(Circle c1, Circle c2) {\n\tif(c1.r < c2.r) swap(c1, c2);\n\tReal d = abs(c1.p - c2.p);\n\tif(c1.r + c2.r < d) return 0; // 外に離れている\n\tif(eq(c1.r + c2.r, d)) return 3; // 外接\n\tif(c1.r - c2.r < d) return 2; // 交差\n\tif(eq(c1.r - c2.r, d)) return 1; // 内接\n\treturn 0; // 内包\n}\n\nReal distance(const Point &a, const Point &b) { return abs(a - b); }\n\nReal distance(const Line &l, const Point &p) { return abs(p - projection(l, p)); }\n\nReal distance(const Line &l, const Line &m) { return intersect(l, m) ? 0 : distance(l, m.a); }\n\nReal distance(const Segment &s, const Point &p) {\n\tPoint r = projection(s, p);\n\tif(intersect(s, r)) return abs(r - p);\n\treturn min(abs(s.a - p), abs(s.b - p));\n}\n\n// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_2_D\nReal distance(const Segment &a, const Segment &b) {\n\tif(intersect(a, b)) return 0;\n\treturn min({distance(a, b.a), distance(a, b.b), distance(b, a.a), distance(b, a.b)});\n}\n\nReal distance(const Line &l, const Segment &s) {\n\tif(intersect(l, s)) return 0;\n\treturn min(distance(l, s.a), distance(l, s.b));\n}\n\nPoint crosspoint(const Line &l, const Line &m) {\n\tReal A = cross(l.b - l.a, m.b - m.a);\n\tReal B = cross(l.b - l.a, l.b - m.a);\n\tif(eq(abs(A), 0.0) && eq(abs(B), 0.0)) return m.a;\n\treturn m.a + (m.b - m.a) * B / A;\n}\n\n// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_2_C\nPoint crosspoint(const Segment &l, const Segment &m) { return crosspoint(Line(l), Line(m)); }\n\n// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_7_D\npair<Point, Point> crosspoint(const Circle &c, const Line l) {\n\tPoint pr = projection(l, c.p);\n\tPoint e = (l.b - l.a) / abs(l.b - l.a);\n\tif(eq(distance(l, c.p), c.r)) return {pr, pr};\n\tdouble base = sqrt(c.r * c.r - norm(pr - c.p));\n\treturn {pr - e * base, pr + e * base};\n}\n\npair<Point, Point> crosspoint(const Circle &c, const Segment &l) {\n\tLine aa = Line(l.a, l.b);\n\tif(intersect(c, l) == 2) return crosspoint(c, aa);\n\tauto ret = crosspoint(c, aa);\n\tif(dot(l.a - ret.first, l.b - ret.first) < 0)\n\t\tret.second = ret.first;\n\telse\n\t\tret.first = ret.second;\n\treturn ret;\n}\n\n// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_7_E\npair<Point, Point> crosspoint(const Circle &c1, const Circle &c2) {\n\tReal d = abs(c1.p - c2.p);\n\tReal a = acos((c1.r * c1.r + d * d - c2.r * c2.r) / (2 * c1.r * d)); // 余弦定理\n\tReal t = atan2(c2.p.imag() - c1.p.imag(), c2.p.real() - c1.p.real());\n\tPoint p1 = c1.p + Point(cos(t + a) * c1.r, sin(t + a) * c1.r);\n\tPoint p2 = c1.p + Point(cos(t - a) * c1.r, sin(t - a) * c1.r);\n\treturn {p1, p2};\n}\n\n// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_7_F\n// \"点 p を通る円 c の接線\"の接点2つ\npair<Point, Point> tangent(const Circle &c1, const Point &p2) { return crosspoint(c1, Circle(p2, sqrt(norm(c1.p - p2) - c1.r * c1.r))); }\n\n// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_7_G\n// 円 c1, c2 の共通接線\nLines tangent(Circle c1, Circle c2) {\n\tLines ret;\n\tif(c1.r < c2.r) swap(c1, c2);\n\tReal g = norm(c1.p - c2.p);\n\tif(eq(g, 0)) return ret;\n\tPoint u = (c2.p - c1.p) / sqrt(g);\n\tPoint v = rotate(PI * 0.5, u);\n\tfor(int s : {-1, 1}) {\n\t\tReal h = (c1.r + s * c2.r) / sqrt(g);\n\t\tif(eq(1 - h * h, 0)) {\n\t\t\tret.emplace_back(c1.p + u * c1.r, c1.p + (u + v) * c1.r);\n\t\t} else if(1 - h * h > 0) {\n\t\t\tPoint uu = u * h, vv = v * sqrt(1 - h * h);\n\t\t\tret.emplace_back(c1.p + (uu + vv) * c1.r, c2.p - (uu + vv) * c2.r * s);\n\t\t\tret.emplace_back(c1.p + (uu - vv) * c1.r, c2.p - (uu - vv) * c2.r * s);\n\t\t}\n\t}\n\treturn ret;\n}\n\nvoid solve() {\n\tint n, m;\n\tcin >> n >> m;\n\tif(!n) exit(0);\n\tPoints p(n);\n\tvector<Real> h(n);\n\tfor(int i = 0; i < n; i++)\n\t\tcin >> p[i] >> h[i];\n\n\tauto judge = [&](Real rad) {\n\t\tCircles Cir(n);\n\t\tfor(int i = 0; i < n; i++) {\n\t\t\tif(h[i] >= rad)\n\t\t\t\tCir[i].r = rad;\n\t\t\telse\n\t\t\t\tCir[i].r = sqrt(h[i] * rad * 2 - h[i] * h[i]);\n\t\t\tCir[i].p = p[i];\n\t\t}\n\n\t\tReal blank = (m >= rad ? rad : sqrt(m * rad * 2 - m * m));\n\n\t\tif(blank > 50) return false;\n\n\t\tPoint a = {blank, blank}, b = {blank, 100 - blank}, c = {100 - blank, 100 - blank}, d = {100 - blank, blank};\n\n\t\tSegments SEG = {Segment(a, b), Segment(b, c), Segment(c, d), Segment(d, a)};\n\n\t\tPoints candidate = {a, b, c, d};\n\n\t\tfor(int i = 0; i < n; i++) {\n\t\t\t//円同士の交点を全列挙\n\t\t\tfor(int j = i + 1; j < n; j++) {\n\t\t\t\tif(intersect(Cir[i], Cir[j])) {\n\t\t\t\t\tauto [p1, p2] = crosspoint(Cir[i], Cir[j]);\n\t\t\t\t\tcandidate.push_back(p1);\n\t\t\t\t\tcandidate.push_back(p2);\n\t\t\t\t}\n\t\t\t}\n\t\t\t//円と正方形の交点を全列挙\n\t\t\tfor(int j = 0; j < 4; j++) {\n\t\t\t\tif(intersect(Cir[i], SEG[j])) {\n\t\t\t\t\tauto [p1, p2] = crosspoint(Cir[i], SEG[j]);\n\t\t\t\t\tcandidate.push_back(p1);\n\t\t\t\t\tcandidate.push_back(p2);\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\n\t\tauto satisfied = [&](Point p) {\n\t\t\tfor(auto c : Cir) {\n\t\t\t\tif(distance(p, c.p) + EPS < c.r) return false;\n\t\t\t}\n\t\t\tif(p.real() + EPS < blank) return false;\n\t\t\tif(p.imag() + EPS < blank) return false;\n\t\t\tif(p.real() - EPS > 100 - blank) return false;\n\t\t\tif(p.imag() - EPS > 100 - blank) return false;\n\t\t\treturn true;\n\t\t};\n\n\t\tfor(auto &p : candidate) {\n\t\t\tif(satisfied(p)) return true;\n\t\t}\n\n\t\treturn false;\n\t};\n\n\tReal ok = 0, ng = 300;\n\tfor(int i = 0; i < 50; i++) {\n\t\tReal mid = (ok + ng) / 2;\n\t\tif(judge(mid))\n\t\t\tok = mid;\n\t\telse\n\t\t\tng = mid;\n\t}\n\tcout << ok << '\\n';\n\treturn;\n}\n\nint main() {\n\tcout << fixed << setprecision(15);\n\twhile(1) solve();\n}", "accuracy": 1, "time_ms": 150, "memory_kb": 3956, "score_of_the_acc": -0.822, "final_rank": 11 }, { "submission_id": "aoj_1342_4964564", "code_snippet": "#line 1 \"a.cpp\"\nusing namespace std;\n#line 1 \"/home/kotatsugame/library/math/point.cpp\"\n#define _USE_MATH_DEFINES\n#include<iostream>\n#include<algorithm>\n#include<vector>\n#include<cmath>\n#include<iomanip>\nconst double EPS=1e-10;\nbool eq(double a,double b){return abs(a-b)<EPS;}\nstruct Point{\n\tdouble x,y;\n\tPoint(double x_=0,double y_=0):x(x_),y(y_){}\n\tPoint operator-()const{return Point(-x,-y);}\n\tPoint operator+(const Point&p)const{return Point(x+p.x,y+p.y);}\n\tPoint operator-(const Point&p)const{return Point(x-p.x,y-p.y);}\n\tPoint operator*(const double k)const{return Point(x*k,y*k);}\n\tPoint operator/(const double k)const{return Point(x/k,y/k);}\n\tbool operator<(const Point&p)const{return eq(x,p.x)?y<p.y:x<p.x;}\n\tbool operator==(const Point&p)const{return eq(x,p.x)&&eq(y,p.y);}\n};\nistream&operator>>(istream&is,Point&p){return is>>p.x>>p.y;}\nostream&operator<<(ostream&os,const Point&p){return os<<fixed<<setprecision(9)<<p.x<<' '<<p.y;}\nstruct Line{\n\tPoint p1,p2;\n\tLine(Point p1_=Point(),Point p2_=Point()):p1(p1_),p2(p2_){}\n};\nstruct Segment:Line{\n\tSegment(Point p1_=Point(),Point p2_=Point()):Line(p1_,p2_){}\n};\nstruct Circle{\n\tPoint o;\n\tdouble r;\n\tCircle(Point o_=Point(),double r_=0):o(o_),r(r_){}\n};\nusing Polygon=vector<Point>;\n//function list begin\nPoint vec(const Line&);\ndouble norm(const Point&);\ndouble norm(const Line&);\ndouble abs(const Point&);\ndouble abs(const Line&);\ndouble arg(const Point&);\ndouble arg(const Line&);\ndouble arg(const Point&,const Point&,const Point&);//a->b->c\nint argtype(const Point&);//(-pi,0]->0,(0,pi]->1\nbool argless(const Point&,const Point&);//sorting points with arg\ndouble dot(const Point&,const Point&);\ndouble cross(const Point&,const Point&);\nPoint polar(const double,const double);\nPoint rotate(const Point&,const double);\nenum{ONLINE_FRONT=-2,CLOCKWISE=-1,ON_SEGMENT=0,COUNTER_CLOCKWISE=1,ONLINE_BACK=2};\nint ccw(const Point&,const Point&);\nint ccw(const Point&,const Point&,const Point&);\nint ccw(const Line&,const Point&);\nbool orthogonal(const Point&,const Point&);\nbool orthogonal(const Line&,const Line&);\nbool parallel(const Point&,const Point&);\nbool parallel(const Line&,const Line&);\nbool intersect(const Line&,const Point&);\nbool intersect(const Line&,const Line&);\nbool intersect(const Segment&,const Point&);\nbool intersect(const Segment&,const Segment&);\nbool intersect(const Line&,const Segment&);\nbool intersect(const Segment&,const Line&);\nbool intersect(const Circle&,const Point&);\nint intersect(const Circle&,const Line&);//count contacts\nint intersect(const Circle&,const Segment&);\nint intersect(const Circle&,const Circle&);//count common tangents\ndouble distance(const Point&,const Point&);\ndouble distance(const Line&,const Point&);\ndouble distance(const Line&,const Line&);\ndouble distance(const Segment&,const Point&);\ndouble distance(const Segment&,const Segment&);\ndouble distance(const Line&,const Segment&);\ndouble distance(const Segment&,const Line&);\ndouble distance(const Circle&,const Point&);\ndouble distance(const Circle&,const Line&);\ndouble distance(const Circle&,const Segment&);\ndouble distance(const Circle&,const Circle&);\nPoint projection(const Line&,const Point&);\nPoint reflection(const Line&,const Point&);\nPoint crosspoint(const Line&,const Line&);\npair<Point,Point>crosspoint(const Circle&,const Line&);\npair<Point,Point>crosspoint(const Circle&,const Segment&);\npair<Point,Point>crosspoint(const Circle&,const Circle&);\npair<Point,Point>tangent(const Circle&,const Point&);\nvector<Line>tangent(const Circle&,const Circle&);\nbool is_convex(const Polygon&);\nPolygon convex_hull(Polygon,bool=false);\nenum{OUT,ON,IN};\nint contain(const Polygon&,const Point&);\nint contain(const Circle&,const Point&);\nint contain(const Circle&,const Segment&);\nPolygon convex_cut(const Polygon&,const Line&);\ndouble diameter(Polygon);\ndouble area(const Polygon&);\ndouble area(const Polygon&,const Line&);\ndouble area(const Polygon&,const Circle&);\n//function list end\nPoint vec(const Line&s){return s.p2-s.p1;}\ndouble norm(const Point&p){return p.x*p.x+p.y*p.y;}\ndouble norm(const Line&s){return norm(vec(s));}\ndouble abs(const Point&p){return hypot(p.x,p.y);}\ndouble abs(const Line&s){return abs(vec(s));}\ndouble arg(const Point&p){return atan2(p.y,p.x);}\ndouble arg(const Line&s){return arg(vec(s));}\ndouble arg(const Point&a,const Point&b,const Point&c){\n\tdouble A=arg(b-a),B=arg(c-b);\n\tdouble theta=abs(A-B);\n\treturn min(theta,2*M_PI-theta);\n}\nint argtype(const Point&a)\n{\n\treturn a.y<-EPS?0:a.y>EPS?1:a.x<0?1:0;\n}\nbool argless(const Point&a,const Point&b)\n{\n\tint at=argtype(a),bt=argtype(b);\n\treturn at!=bt?at<bt:ccw(a,b)>0;\n}\ndouble dot(const Point&a,const Point&b){return a.x*b.x+a.y*b.y;}\ndouble cross(const Point&a,const Point&b){return a.x*b.y-a.y*b.x;}\nPoint polar(const double r,const double theta){return Point(cos(theta),sin(theta))*r;}\nPoint rotate(const Point&p,const double theta){\n\treturn Point(p.x*cos(theta)-p.y*sin(theta),p.x*sin(theta)+p.y*cos(theta));\n}\nint ccw(const Point&a,const Point&b)\n{\n\treturn cross(a,b)>EPS?COUNTER_CLOCKWISE\n\t\t:cross(a,b)<-EPS?CLOCKWISE\n\t\t:dot(a,b)<0?ONLINE_BACK\n\t\t:norm(a)<norm(b)?ONLINE_FRONT\n\t\t:ON_SEGMENT;\n}\nint ccw(const Point&a,const Point&b,const Point&c){return ccw(b-a,c-a);}\nint ccw(const Line&s,const Point&p){return ccw(s.p1,s.p2,p);}\nbool orthogonal(const Point&a,const Point&b){return eq(dot(a,b),0);}\nbool orthogonal(const Line&s,const Line&t){return orthogonal(vec(s),vec(t));}\nbool parallel(const Point&a,const Point&b){return eq(cross(a,b),0);}\nbool parallel(const Line&s,const Line&t){return parallel(vec(s),vec(t));}\nbool intersect(const Line&s,const Point&p){return eq(cross(vec(s),p-s.p1),0);}\nbool intersect(const Line&s,const Line&t){return !parallel(s,t)||intersect(s,t.p1);}\nbool intersect(const Segment&s,const Point&p){return ccw(s,p)==ON_SEGMENT;}\nbool intersect(const Segment&s,const Segment&t){\n\treturn ccw(s,t.p1)*ccw(s,t.p2)<=0&&ccw(t,s.p1)*ccw(t,s.p2)<=0;\n}\nbool intersect(const Line&s,const Segment&t){\n\treturn cross(vec(s),t.p1-s.p1)*cross(vec(s),t.p2-s.p1)<EPS;\n}\nbool intersect(const Segment&s,const Line&t){return intersect(t,s);}\nbool intersect(const Circle&c,const Point&p){return eq(distance(c.o,p),c.r);}\nint intersect(const Circle&c,const Line&s){\n\tdouble d=distance(s,c.o);\n\treturn eq(d,c.r)?1:d<c.r?2:0;\n}\nint intersect(const Circle&c,const Segment&s){\n\tPoint h=projection(s,c.o);\n\tdouble d1=distance(c.o,s.p1),d2=distance(c.o,s.p2);\n\treturn distance(c.o,h)>c.r+EPS?0\n\t\t:d1<c.r-EPS&&d2<c.r-EPS?0\n\t\t:d1<c.r-EPS&&d2>c.r-EPS||d1>c.r-EPS&&d2<c.r-EPS?1\n\t\t:intersect(s,h)?eq(distance(c.o,h),c.r)?1:2\n\t\t:0;\n}\nint intersect(const Circle&a,const Circle&b){\n\tdouble d=distance(a.o,b.o);\n\treturn eq(d,a.r+b.r)?3:d>a.r+b.r?4:eq(d,abs(a.r-b.r))?1:d>abs(a.r-b.r)?2:0;\n}\ndouble distance(const Point&a,const Point&b){return abs(a-b);}\ndouble distance(const Line&s,const Point&p){return distance(p,projection(s,p));}\ndouble distance(const Line&s,const Line&t){return intersect(s,t)?0:distance(s,t.p1);}\ndouble distance(const Segment&s,const Point&p){\n\treturn distance(p,\n\t\tdot(vec(s),p-s.p1)<0?s.p1\n\t\t:dot(-vec(s),p-s.p2)<0?s.p2\n\t\t:projection(s,p)\n\t);\n}\ndouble distance(const Segment&s,const Segment&t){\n\treturn intersect(s,t)?0:min({\n\t\tdistance(s,t.p1),distance(s,t.p2),\n\t\tdistance(t,s.p1),distance(t,s.p2)\n\t});\n}\ndouble distance(const Line&s,const Segment&t){\n\treturn intersect(s,t)?0:min(distance(s,t.p1),distance(s,t.p2));\n}\ndouble distance(const Segment&s,const Line&t){return distance(t,s);}\ndouble distance(const Circle&c,const Point&p){return abs(distance(c.o,p)-c.r);}\ndouble distance(const Circle&c,const Line&s){return max(distance(s,c.o)-c.r,0.);}\ndouble distance(const Circle&c,const Segment&s){\n\treturn intersect(c,s)?0\n\t\t:contain(c,s)?c.r-max(distance(c.o,s.p1),distance(c.o,s.p2))\n\t\t:distance(s,c.o)-c.r;\n}\ndouble distance(const Circle&a,const Circle&b){return max(distance(a.o,b.o)-a.r-b.r,0.);}\nPoint projection(const Line&s,const Point&p){\n\treturn s.p1+vec(s)*dot(p-s.p1,vec(s))/norm(s);\n}\nPoint reflection(const Line&s,const Point&p){return projection(s,p)*2-p;}\nPoint crosspoint(const Line&s,const Line&t){\n\tdouble d1=cross(vec(s),t.p1-s.p1);\n\tdouble d2=-cross(vec(s),t.p2-s.p1);\n\treturn t.p1+vec(t)*(d1/(d1+d2));\n}\npair<Point,Point>crosspoint(const Circle&c,const Line&s){\n\tPoint h=projection(s,c.o);\n\tPoint e=vec(s)/abs(s)*sqrt(c.r*c.r-norm(h-c.o));\n\treturn minmax(h-e,h+e);\n}\npair<Point,Point>crosspoint(const Circle&c,const Segment&s){\n\tpair<Point,Point>p=crosspoint(c,Line(s));\n\treturn intersect(c,s)==2?p\n\t\t:intersect(s,p.first)?make_pair(p.first,p.first)\n\t\t:make_pair(p.second,p.second);\n}\npair<Point,Point>crosspoint(const Circle&a,const Circle&b){\n\tdouble d=distance(a.o,b.o);\n\tdouble alpha=acos((a.r*a.r+d*d-b.r*b.r)/(2*a.r*d));\n\tdouble theta=arg(b.o-a.o);\n\treturn minmax(a.o+polar(a.r,theta+alpha),a.o+polar(a.r,theta-alpha));\n}\npair<Point,Point>tangent(const Circle&c,const Point&p){\n\treturn crosspoint(c,Circle(p,sqrt(norm(c.o-p)-c.r*c.r)));\n}\nvector<Line>tangent(const Circle&a,const Circle&b){\n\tvector<Line>ret;\n\tdouble g=distance(a.o,b.o);\n\tif(eq(g,0))return ret;\n\tPoint u=(b.o-a.o)/g;\n\tPoint v=rotate(u,M_PI/2);\n\tfor(int s:{-1,1}){\n\t\tdouble h=(a.r+b.r*s)/g;\n\t\tif(eq(h*h,1))ret.emplace_back(a.o+(h>0?u:-u)*a.r,a.o+(h>0?u:-u)*a.r+v);\n\t\telse if(1-h*h>0){\n\t\t\tPoint U=u*h,V=v*sqrt(1-h*h);\n\t\t\tret.emplace_back(a.o+(U+V)*a.r,b.o-(U+V)*b.r*s);\n\t\t\tret.emplace_back(a.o+(U-V)*a.r,b.o-(U-V)*b.r*s);\n\t\t}\n\t}\n\treturn ret;\n}\nbool is_convex(const Polygon&P){\n\tfor(int i=0;i<P.size();i++)\n\t\tif(ccw(P[i],P[(i+1)%P.size()],P[(i+2)%P.size()])==CLOCKWISE)return false;\n\treturn true;\n}\nPolygon convex_hull(Polygon P,bool ONSEG){\n\tif(P.size()<=2)return P;\n\tsort(P.begin(),P.end());\n\tPolygon ret(2*P.size());\n\tint k=0,t;\n\tif(ONSEG){\n\t\tfor(const Point&p:P){\n\t\t\twhile(k>=2&&ccw(ret[k-2],ret[k-1],p)==CLOCKWISE)k--;\n\t\t\tret[k++]=p;\n\t\t}\n\t\tt=k;\n\t\tfor(int i=P.size()-2;i>=0;i--){\n\t\t\twhile(k>=t+1&&ccw(ret[k-2],ret[k-1],P[i])==CLOCKWISE)k--;\n\t\t\tret[k++]=P[i];\n\t\t}\n\t}\n\telse{\n\t\tfor(const Point&p:P){\n\t\t\twhile(k>=2&&ccw(ret[k-2],ret[k-1],p)!=COUNTER_CLOCKWISE)k--;\n\t\t\tret[k++]=p;\n\t\t}\n\t\tt=k;\n\t\tfor(int i=P.size()-2;i>=0;i--){\n\t\t\twhile(k>=t+1&&ccw(ret[k-2],ret[k-1],P[i])!=COUNTER_CLOCKWISE)k--;\n\t\t\tret[k++]=P[i];\n\t\t}\n\t}\n\tret.resize(k-1);\n\tint mi=0;\n\tfor(int i=1;i<k-1;i++)\n\t\tif(eq(ret[mi].y,ret[i].y)?ret[mi].x>ret[i].x:ret[mi].y>ret[i].y)mi=i;\n\trotate(ret.begin(),ret.begin()+mi,ret.end());\n\treturn ret;\n}\nint contain(const Polygon&P,const Point&p){\n\tbool in=false;\n\tfor(int i=0;i<P.size();i++){\n\t\tSegment s(P[i],P[(i+1)%P.size()]);\n\t\tif(intersect(s,p))return ON;\n\t\telse{\n\t\t\tPoint a=s.p1-p,b=s.p2-p;\n\t\t\tif(a.y>b.y)swap(a,b);\n\t\t\tif(a.y<EPS&&EPS<b.y&&cross(a,b)>EPS)in=!in;\n\t\t}\n\t}\n\treturn in?IN:OUT;\n}\nint contain(const Circle&c,const Point&p){\n\tdouble d=distance(c.o,p);\n\treturn eq(d,c.r)?ON:d<c.r?IN:OUT;\n}\nint contain(const Circle&c,const Segment&s){\n\tdouble d1=distance(c.o,s.p1),d2=distance(c.o,s.p2);\n\treturn d1<c.r+EPS&&d2<c.r+EPS?eq(d1,c.r)||eq(d2,c.r)?ON:IN:OUT;\n}\nPolygon convex_cut(const Polygon&P,const Line&s){\n\tPolygon ret;\n\tfor(int i=0;i<P.size();i++){\n\t\tSegment t(P[i],P[(i+1)%P.size()]);\n\t\tif(ccw(s,t.p1)!=CLOCKWISE)ret.push_back(t.p1);\n\t\tif(!parallel(s,t)&&!intersect(s,t.p1)\n\t\t\t&&!intersect(s,t.p2)&&intersect(s,t))ret.push_back(crosspoint(s,t));\n\t}\n\treturn ret;\n}\ndouble diameter(Polygon P){\n\tif(!is_convex(P))P=convex_hull(P);\n\tint mi=0,Mi=0;\n\tfor(int i=1;i<P.size();i++){\n\t\tif(P[i].x<P[mi].x)mi=i;\n\t\tif(P[i].x>P[Mi].x)Mi=i;\n\t}\n\tdouble ret=0;\n\tint sm=mi,sM=Mi;\n\twhile(mi!=sM||Mi!=sm){\n\t\tret=max(ret,norm(P[mi]-P[Mi]));\n\t\tif(cross(P[(mi+1)%P.size()]-P[mi],P[(Mi+1)%P.size()]-P[Mi])<0)mi=(mi+1)%P.size();\n\t\telse Mi=(Mi+1)%P.size();\n\t}\n\treturn sqrt(ret);\n}\ndouble area(const Polygon&P){\n\tdouble ret=0;\n\tfor(int i=0;i<P.size();i++)ret+=cross(P[i],P[(i+1)%P.size()]);\n\treturn ret/2;\n}\ndouble area(const Polygon&P,const Line&s){return area(convex_cut(P,s));}\ndouble area(const Polygon&P,const Circle&c){\n\tdouble ret=0;\n\tfor(int i=0;i<P.size();i++)\n\t{\n\t\tSegment s(P[i],P[(i+1)%P.size()]);\n\t\tif(contain(c,s))ret+=cross(s.p1-c.o,s.p2-c.o);\n\t\telse if(!intersect(c,s)){\n\t\t\tdouble a=arg(s.p2-c.o)-arg(s.p1-c.o);\n\t\t\tif(a>M_PI)a-=2*M_PI;\n\t\t\tif(a<-M_PI)a+=2*M_PI;\n\t\t\tret+=c.r*c.r*a;\n\t\t}\n\t\telse\n\t\t{\n\t\t\tpair<Point,Point>p=crosspoint(c,s);\n\t\t\tPoint tmp[4]={s.p1,p.first,p.second,s.p2};\n\t\t\tif(intersect(c,Segment(s.p1,p.first))==2)swap(tmp[1],tmp[2]);\n\t\t\tfor(int j=0;j<3;j++)\n\t\t\t{\n\t\t\t\tSegment t(tmp[j],tmp[j+1]);\n\t\t\t\tif(contain(c,t))ret+=cross(t.p1-c.o,t.p2-c.o);\n\t\t\t\telse{\n\t\t\t\t\tdouble a=arg(t.p2-c.o)-arg(t.p1-c.o);\n\t\t\t\t\tif(a>M_PI)a-=2*M_PI;\n\t\t\t\t\tif(a<-M_PI)a+=2*M_PI;\n\t\t\t\t\tret+=c.r*c.r*a;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n\treturn ret/2;\n}\n#line 3 \"a.cpp\"\nint N,W;\nPoint p[10];\nint h[10];\nCircle C[10];\ndouble calc(double h,double r)\n{\n\tif(h>r)return r;\n\telse return sqrt(2*r*h-h*h);\n}\nbool in(Point p,double w)\n{\n\tfor(int i=0;i<N;i++)if(contain(C[i],p)==IN)return false;\n\treturn w-EPS<p.x&&p.x<100-w+EPS&&w-EPS<p.y&&p.y<100-w+EPS;\n}\nmain()\n{\n\twhile(cin>>N>>W,N)\n\t{\n\t\tfor(int i=0;i<N;i++)cin>>p[i]>>h[i];\n\t\tdouble le=0,ri=1e6;\n\t\tfor(int c=0;c<100;c++)\n\t\t{\n\t\t\tdouble r=(le+ri)/2;\n\t\t\tdouble w=calc(W,r);\n\t\t\tfor(int i=0;i<N;i++)\n\t\t\t{\n\t\t\t\tC[i]=Circle(p[i],calc(h[i],r));\n\t\t\t}\n\t\t\tbool ok=false;\n\t\t\tfor(int i=0;i<N;i++)for(int j=i+1;j<N;j++)\n\t\t\t{\n\t\t\t\tint cnt=intersect(C[i],C[j]);\n\t\t\t\tif(cnt==0||cnt==4)continue;\n\t\t\t\tpair<Point,Point>tmp=crosspoint(C[i],C[j]);\n\t\t\t\tif(in(tmp.first,w)||in(tmp.second,w))ok=true;\n\t\t\t}\n\t\t\tPoint cr[4]={Point(w,w),Point(w,100-w),Point(100-w,w),Point(100-w,100-w)};\n\t\t\tLine L[4]={Line(cr[0],cr[1]),Line(cr[0],cr[2]),Line(cr[1],cr[3]),Line(cr[2],cr[3])};\n\t\t\tfor(int i=0;i<N;i++)for(int j=0;j<4;j++)if(intersect(C[i],L[j]))\n\t\t\t{\n\t\t\t\tpair<Point,Point>tmp=crosspoint(C[i],L[j]);\n\t\t\t\tif(in(tmp.first,w)||in(tmp.second,w))ok=true;\n\t\t\t}\n\t\t\tfor(int i=0;i<4;i++)for(int j=i+1;j<4;j++)\n\t\t\t{\n\t\t\t\tif(in(crosspoint(L[i],L[j]),w))ok=true;\n\t\t\t}\n\t\t\tif(ok)le=r;\n\t\t\telse ri=r;\n\t\t}\n\t\tcout<<fixed<<setprecision(16)<<le<<endl;\n\t}\n}", "accuracy": 1, "time_ms": 490, "memory_kb": 3576, "score_of_the_acc": -0.434, "final_rank": 7 }, { "submission_id": "aoj_1342_4952281", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\ntypedef long long ll;\ntemplate<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return true; } return false; }\ntemplate<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return true; } return false; }\n#define all(x) (x).begin(),(x).end()\n#define fi first\n#define se second\n#define mp make_pair\n#define si(x) int(x.size())\nconst int mod=1000000007,MAX=2005;\n//const int INF=1<<30;\nconst ll INF=1LL<<60;\n//幾何ライブラリ\n\nconst double eps=1e-8;\nconst double pi=acos((double)-1.0L);\n#define equals(a,b) (fabs((a)-(b))<eps)\n\ndouble torad(int deg) {return (double)(deg)*pi/180.0;}\ndouble todeg(double ang) {return ang*180.0/pi;}\n\nclass Point{\npublic:\n double x,y;\n \n Point(double x=0,double y=0):x(x),y(y){}\n \n Point operator + (Point p){return Point(x+p.x,y+p.y);}\n Point operator - (Point p){return Point(x-p.x,y-p.y);}\n Point operator * (double a){return Point(a*x,a*y);}\n Point operator / (double a){return Point(x/a,y/a);}\n \n double abs(){return sqrt(norm());}\n double norm(){return x*x+y*y;}\n \n bool operator < (const Point &p)const{\n return x+eps<p.x||(equals(x,p.x)&&y+eps<p.y);\n }\n \n bool operator == (const Point &p)const{\n return fabs(x-p.x)<eps/100000&&fabs(y-p.y)<eps/100000;\n }\n};\n\ntypedef Point Vector;\n\ndouble norm(Vector a){\n return a.x*a.x+a.y*a.y;\n}\n\ndouble abs(Vector a){\n return sqrt(norm(a));\n}\n\ndouble dot(Vector a,Vector b){\n return a.x*b.x+a.y*b.y;\n}\n\ndouble cross(Vector a,Vector b){\n return a.x*b.y-a.y*b.x;\n}\n\nstruct Segment{\n Point p1,p2;\n};\n\nbool isOrthogonal(Vector a,Vector b){\n return equals(dot(a,b),0.0);\n}\n\nbool isOrthogonal(Point a1,Point a2,Point b1,Point b2){\n return isOrthogonal(a1-a2,b1-b2);\n}\n\nbool isOrthogonal(Segment s1,Segment s2){\n return equals(dot(s1.p2-s1.p1,s2.p2-s2.p1),0.0);\n}\n\nbool isParallel(Vector a,Vector b){\n return equals(cross(a,b),0.0);\n}\n\nbool isParallel(Point a1,Point a2,Point b1,Point b2){\n return isParallel(a1-a2,b1-b2);\n}\n\nbool isParallel(Segment s1,Segment s2){\n return equals(cross(s1.p2-s1.p1,s2.p2-s2.p1),0.0);\n}\n\nPoint project(Segment s,Point p){\n Vector base=s.p2-s.p1;\n double r=dot(p-s.p1,base)/norm(base);\n return s.p1+base*r;\n}\n\nPoint reflect(Segment s,Point p){\n return p+(project(s,p)-p)*2.0;\n}\n\nPoint turn(Point p,Point c,double pi){\n double q=atan2(p.y-c.y,p.x-c.x);\n q+=pi;\n p=c+Point{cos(q)*abs(p-c),sin(q)*abs(p-c)};\n \n return p;\n}\n//pをcを中心としてpi回転させる(1周で2π)\n//p=cのときnan\n\nstatic const int counter_clockwise=1;\nstatic const int clockwise=-1;\nstatic const int online_back=2;\nstatic const int online_front=-2;\nstatic const int on_segment=0;\n\nint ccw(Point p0,Point p1,Point p2){\n Vector a=p1-p0;\n Vector b=p2-p0;\n \n if(cross(a,b)>eps) return counter_clockwise;\n if(cross(a,b)<-eps) return clockwise;\n if(dot(a,b)<-eps) return online_back;\n if(a.norm()<b.norm()) return online_front;\n \n return on_segment;\n}\n\nbool intersect(Point p1,Point p2,Point p3,Point p4){\n return(ccw(p1,p2,p3)*ccw(p1,p2,p4)<=0&&ccw(p3,p4,p1)*ccw(p3,p4,p2)<=0);\n}\n\nbool intersect(Segment s1,Segment s2){\n return intersect(s1.p1,s1.p2,s2.p1,s2.p2);\n}\n\nbool overlap(Segment s1,Segment s2){\n int a=ccw(s1.p1,s1.p2,s2.p1),b=ccw(s1.p1,s1.p2,s2.p2);\n if(a&1||b&1) return 0;\n if(a==2){\n if(b==-2||(b==0&&!(s2.p2==s1.p1))) return 1;\n else return 0;\n }\n if(a==-2){\n if(b==2||(b==0&&!(s2.p2==s1.p2))) return 1;\n else return 0;\n }\n if(a==0){\n if(s1.p1==s2.p1){\n if(b!=2) return 1;\n else return 0;\n }\n else if(s1.p2==s2.p1){\n if(b!=-2) return 1;\n else return 0;\n }\n else return 1;\n }\n return 0;\n}\n//s1とs2の共通の線分(長さ0より大きい)があるかどうか\n\ntypedef Segment Line;\n\ndouble getDistance(Point a,Point b){\n return abs(a-b);\n}\n\ndouble getDistanceLP(Line l,Point p){\n return abs(cross(l.p2-l.p1,p-l.p1)/abs(l.p2-l.p1));\n}\n\ndouble getDistanceSP(Segment s,Point p){\n if(dot(s.p2-s.p1,p-s.p1)<0.0) return abs(p-s.p1);\n if(dot(s.p1-s.p2,p-s.p2)<0.0) return abs(p-s.p2);\n return getDistanceLP(s,p);\n}\n\ndouble getDistance(Segment s1,Segment s2){\n if(intersect(s1,s2)) return 0.0;\n return min({getDistanceSP(s1,s2.p1),getDistanceSP(s1,s2.p2),getDistanceSP(s2,s1.p1),getDistanceSP(s2,s1.p2)});\n}\n\nPoint getCrossPointS(Segment s1,Segment s2){\n //if(ccw(s1.p1,s1.p2,s2.p1)==0&&ccw(s1.p1,s1.p2,s2.p2)==0) return s1.p1;\n Vector base=s2.p2-s2.p1;\n double d1=abs(cross(base,s1.p1-s2.p1));\n double d2=abs(cross(base,s1.p2-s2.p1));\n double t=d1/(d1+d2);\n return s1.p1+(s1.p2-s1.p1)*t;\n}//同じ時壊れます\n\nPoint getCrossPointL(Line l1,Line l2){\n //if(ccw(s1.p1,s1.p2,s2.p1)==0&&ccw(s1.p1,s1.p2,s2.p2)==0) return s1.p1;\n \n Vector v1=l1.p2-l1.p1,v2=l2.p2-l2.p1;\n \n return l1.p1+v1*cross(v2,l2.p1-l1.p1)/cross(v2,v1);\n}\n\nSegment ParallelSegment(Segment s,double d){\n Vector v={-(s.p2-s.p1).y,(s.p2-s.p1).x};\n v=v/abs(v);\n \n s.p1=s.p1+v*d;\n s.p2=s.p2+v*d;\n \n return s;\n}\n\nPoint naisin(Line l1,Line l2,Line l3){\n //平行でない前提\n \n Point p1=getCrossPointL(l1,l2),p2=getCrossPointL(l1,l3),p3=getCrossPointL(l2,l3);\n if(p1==p2&&p2==p3&&p3==p1) return p1;\n \n return (p1*abs(p1)+p2*abs(p2)+p3*abs(p3))/(abs(p1)+abs(p2)+abs(p3));\n}\n\n//ネットの適当を書いたのであってるか全く知りません→あってそう\n\nclass Circle{\npublic:\n Point c;\n double r;\n Circle(Point c=Point(),double r=0.0):c(c),r(r){}\n};\n\nPoint CircleCenter(Point a,Point b,Point c){\n Point u=a-b,v=a-c;\n double m1=(norm(a)-norm(b))/2.0,m2=(norm(a)-norm(c))/2.0;\n \n Point res;\n if(cross(u,v)==0.0){\n res.x=1e9;\n res.y=1e9;\n \n return res;\n }\n res.x=(m1*v.y-m2*u.y)/cross(u,v);\n res.y=(m1*v.x-m2*u.x)/cross(v,u);\n \n return res;\n}\n//3点を通る円の中心を返す\n\npair<Point,Point> segCrossPpoints(Circle c,Line l){\n //assert(intersect(c,l));\n Vector pr=project(l,c.c);\n Vector e=(l.p2-l.p1)/abs(l.p2-l.p1);\n double base=sqrt(c.r*c.r-norm(pr-c.c));\n return make_pair(pr+e*base,pr-e*base);\n}\n\ndouble arg(Vector p){return atan2(p.y,p.x);}\nVector polar(double a,double r){return Point(cos(r)*a,sin(r)*a);}\n\npair<Point,Point> getCrossPoints(Circle c1,Circle c2){\n //assert(intersect(c1,c2));\n double d=abs(c1.c-c2.c);\n double a=acos(max(-1.0,min(1.0,(c1.r*c1.r+d*d-c2.r*c2.r)/(2*c1.r*d))));\n double t=arg(c2.c-c1.c);\n //cout<<d<<\" \"<<a<<\" \"<<t<<endl;\n return make_pair(c1.c+polar(c1.r,t+a),c1.c+polar(c1.r,t-a));\n}\n\nvector<Line> Commontangent(Circle c1,Circle c2){\n vector<Line> res;\n Point p=c2.c-c1.c;\n \n if(abs(p)>=(c1.r+c2.r)){\n Point a,b;\n a.x=c1.r*(p.x*(c1.r+c2.r)+p.y*sqrt(norm(p)-(c1.r+c2.r)*(c1.r+c2.r)))/norm(p);\n a.y=c1.r*(p.y*(c1.r+c2.r)-p.x*sqrt(norm(p)-(c1.r+c2.r)*(c1.r+c2.r)))/norm(p);\n \n b.x=c1.r*(p.x*(c1.r+c2.r)-p.y*sqrt(norm(p)-(c1.r+c2.r)*(c1.r+c2.r)))/norm(p);\n b.y=c1.r*(p.y*(c1.r+c2.r)+p.x*sqrt(norm(p)-(c1.r+c2.r)*(c1.r+c2.r)))/norm(p);\n \n res.push_back(Line{a+c1.c,a+c1.c+Point{-a.y,a.x}});\n if(!(a==b)){\n res.push_back(Line{b+c1.c,b+c1.c+Point{-b.y,b.x}});\n }\n }\n \n if(abs(p)>=abs(c1.r-c2.r)){\n Point a,b;\n a.x=c1.r*(p.x*(c1.r-c2.r)+p.y*sqrt(norm(p)-(c1.r-c2.r)*(c1.r-c2.r)))/norm(p);\n a.y=c1.r*(p.y*(c1.r-c2.r)-p.x*sqrt(norm(p)-(c1.r-c2.r)*(c1.r-c2.r)))/norm(p);\n \n b.x=c1.r*(p.x*(c1.r-c2.r)-p.y*sqrt(norm(p)-(c1.r-c2.r)*(c1.r-c2.r)))/norm(p);\n b.y=c1.r*(p.y*(c1.r-c2.r)+p.x*sqrt(norm(p)-(c1.r-c2.r)*(c1.r-c2.r)))/norm(p);\n \n res.push_back(Line{a+c1.c,a+c1.c+Point{-a.y,a.x}});\n if(!(a==b)){\n res.push_back(Line{b+c1.c,b+c1.c+Point{-b.y,b.x}});\n }\n }\n \n return res;\n}\n\ntypedef vector<Point> Polygon;\n\n/*\n IN 2\n ON 1\n OUT 0\n */\n\nint contains(Polygon g,Point p){\n int n=int(g.size());\n bool x=false;\n for(int i=0;i<n;i++){\n Point a=g[i]-p,b=g[(i+1)%n]-p;\n if(a.y>b.y) swap(a,b);\n if(a.y<eps&&eps<b.y&&cross(a,b)>eps) x=!x;\n }\n return (x?2:0);\n}\n\nPolygon andrewScan(Polygon s,bool ok){\n Polygon u,l;\n sort(all(s));\n \n if(int(s.size())<3) return s;\n int n=int(s.size());\n \n u.push_back(s[0]);\n u.push_back(s[1]);\n \n l.push_back(s[n-1]);\n l.push_back(s[n-2]);\n \n if(ok){\n for(int i=2;i<n;i++){\n for(int j=int(u.size());j>=2&&ccw(u[j-2],u[j-1],s[i])==counter_clockwise;j--){\n u.pop_back();\n }\n u.push_back(s[i]);\n }\n \n for(int i=int(s.size())-3;i>=0;i--){\n for(int j=int(l.size());j>=2&&ccw(l[j-2],l[j-1],s[i])==counter_clockwise;j--){\n l.pop_back();\n }\n l.push_back(s[i]);\n }\n }\n \n if(!ok){\n for(int i=2;i<n;i++){\n for(int j=int(u.size());j>=2&&ccw(u[j-2],u[j-1],s[i])!=clockwise;j--){\n u.pop_back();\n }\n u.push_back(s[i]);\n }\n \n for(int i=int(s.size())-3;i>=0;i--){\n for(int j=int(l.size());j>=2&&ccw(l[j-2],l[j-1],s[i])!=clockwise;j--){\n l.pop_back();\n }\n l.push_back(s[i]);\n }\n }\n \n reverse(all(l));\n \n for(int i=int(u.size())-2;i>=1;i--) l.push_back(u[i]);\n \n return l;\n}//ok==1なら辺の上も含める\n\nPolygon convex_cut(const Polygon& P, const Line& l) {\n Polygon Q;\n for(int i=0;i<si(P);i++){\n Point A=P[i],B=P[(i+1)%si(P)];\n if(ccw(l.p1,l.p2,A)!=-1)Q.push_back(A);\n if(ccw(l.p1,l.p2,A)*ccw(l.p1,l.p2,B)<0) Q.push_back(getCrossPointL(Line{A,B},l));\n }\n return Q;\n}\n\ndouble area(Point a,Point b,Point c){\n b=b-a;\n c=c-a;\n return abs(b.x*c.y-b.y*c.x)/2.0;\n}\n\ndouble area(Polygon &P){\n if(si(P)==0) return 0.0;\n double res=0;\n Point c={0.0,0.0};\n for(int i=0;i<si(P);i++){\n c=c+P[i];\n }\n c=c/si(P);\n \n for(int i=0;i<si(P);i++){\n res+=area(c,P[i],P[(i+1)%si(P)]);\n }\n \n return res;\n}\n\nint main(){\n \n std::ifstream in(\"text.txt\");\n std::cin.rdbuf(in.rdbuf());\n cin.tie(0);\n ios::sync_with_stdio(false);\n \n while(1){\n int N,W;cin>>N>>W;\n if(N==0) break;\n vector<Point> P(N);\n vector<double> h(N);\n for(int i=0;i<N;i++) cin>>P[i].x>>P[i].y>>h[i];\n \n double left=0,right=3000;\n for(int q=0;q<100;q++){\n double mid=(left+right)/2.0;\n vector<Circle> C(N);\n for(int i=0;i<N;i++){\n C[i].c=P[i];\n if(h[i]<=mid){\n C[i].r=sqrt((h[i])*(2.0*mid-h[i]));\n }else{\n C[i].r=mid;\n }\n }\n double lim;\n if(W<=mid) lim=sqrt((W)*(2.0*mid-W));\n else lim=mid;\n \n vector<Point> p={{lim,lim},{100-lim,lim},{100-lim,100-lim},{lim,100-lim}};\n vector<Line> l(4);\n for(int i=0;i<4;i++) l[i]={p[i],p[(i+1)%4]};\n \n bool ok=false;\n for(int i=0;i<N;i++){\n for(int j=i+1;j<N;j++){\n double d=getDistance(C[i].c,C[j].c);\n if(C[i].r+C[j].r>d){\n auto ps=getCrossPoints(C[i],C[j]);\n \n //assert(abs(getDistance(C[i].c,ps.fi)-C[i].r)<1e-5);\n //cout<<getDistance(C[i].c,ps.fi)<<\" \"<<C[i].r<<endl;\n assert(d>1e-5);\n assert(C[i].r>1e-5);\n assert(C[j].r>1e-5);\n //cout<<ps.fi.x<<\" \"<<ps.fi.y<<\" \"<<ps.se.x<<\" \"<<ps.se.y<<endl;\n \n for(int t=0;t<2;t++){\n bool safe=true;\n for(int k=0;k<N;k++){\n double d2=getDistance(ps.fi,C[k].c);\n if(d2<C[k].r-eps) safe=false;\n }\n \n if(ps.fi.x<lim-eps) safe=false;\n if(ps.fi.x>100.0-lim+eps) safe=false;\n if(ps.fi.y<lim-eps) safe=false;\n if(ps.fi.y>100.0-lim+eps) safe=false;\n \n if(safe) ok=true;\n swap(ps.fi,ps.se);\n }\n }\n }\n for(int k=0;k<4;k++){\n double d=getDistanceLP(l[k],C[i].c);\n if(d<C[i].r-eps){\n auto ps=segCrossPpoints(C[i],l[k]);\n for(int t=0;t<2;t++){\n bool safe=true;\n for(int a=0;a<N;a++){\n double d2=getDistance(ps.fi,C[a].c);\n if(d2<C[a].r-eps) safe=false;\n }\n \n if(ps.fi.x<lim-eps) safe=false;\n if(ps.fi.x>100.0-lim+eps) safe=false;\n if(ps.fi.y<lim-eps) safe=false;\n if(ps.fi.y>100.0-lim+eps) safe=false;\n \n if(safe) ok=true;\n swap(ps.fi,ps.se);\n }\n }\n }\n }\n \n for(int t=0;t<4;t++){\n bool safe=true;\n for(int k=0;k<N;k++){\n double d2=getDistance(p[t],C[k].c);\n if(d2<C[k].r-eps) safe=false;\n }\n \n if(safe) ok=true;\n }\n \n if(lim>=50) ok=false;\n \n if(ok) left=mid;\n else right=mid;\n }\n \n cout<<fixed<<setprecision(25)<<left<<endl;\n }\n}", "accuracy": 1, "time_ms": 200, "memory_kb": 4124, "score_of_the_acc": -1.0193, "final_rank": 17 }, { "submission_id": "aoj_1342_4950291", "code_snippet": "#define MOD_TYPE 1\n\n#pragma region Macros\n\n#include <bits/stdc++.h>\nusing namespace std;\n\n#if 0\n#include <boost/multiprecision/cpp_int.hpp>\n#include <boost/multiprecision/cpp_dec_float.hpp>\nusing Int = boost::multiprecision::cpp_int;\nusing lld = boost::multiprecision::cpp_dec_float_100;\n#endif\n#if 0\n#pragma GCC target(\"avx2\")\n#pragma GCC optimize(\"O3\")\n#pragma GCC optimize(\"unroll-loops\")\n#endif\nusing ll = long long int;\nusing ld = long double;\nusing pii = pair<int, int>;\nusing pll = pair<ll, ll>;\nusing pld = pair<ld, ld>;\ntemplate <typename Q_type>\nusing smaller_queue = priority_queue<Q_type, vector<Q_type>, greater<Q_type>>;\n\nconstexpr ll MOD = (MOD_TYPE == 1 ? (ll)(1e9 + 7) : 998244353);\n//constexpr ll MOD = ;\nconstexpr int INF = (int)1e9 + 10;\nconstexpr ll LINF = (ll)4e18;\nconstexpr double PI = acos(-1.0);\nconstexpr double EPS = 1e-10;\nconstexpr int Dx[] = {0, 0, -1, 1, -1, 1, -1, 1, 0};\nconstexpr int Dy[] = {1, -1, 0, 0, -1, -1, 1, 1, 0};\n\n#define REP(i, m, n) for (ll i = m; i < (ll)(n); ++i)\n#define rep(i, n) REP(i, 0, n)\n#define REPI(i, m, n) for (int i = m; i < (int)(n); ++i)\n#define repi(i, n) REPI(i, 0, n)\n#define MP make_pair\n#define MT make_tuple\n#define YES(n) cout << ((n) ? \"YES\" : \"NO\") << \"\\n\"\n#define Yes(n) cout << ((n) ? \"Yes\" : \"No\") << \"\\n\"\n#define possible(n) cout << ((n) ? \"possible\" : \"impossible\") << \"\\n\"\n#define Possible(n) cout << ((n) ? \"Possible\" : \"Impossible\") << \"\\n\"\n#define Yay(n) cout << ((n) ? \"Yay!\" : \":(\") << \"\\n\"\n#define all(v) v.begin(), v.end()\n#define NP(v) next_permutation(all(v))\n#define dbg(x) cerr << #x << \":\" << x << \"\\n\";\n\nstruct io_init\n{\n io_init()\n {\n cin.tie(0);\n ios::sync_with_stdio(false);\n cout << setprecision(30) << setiosflags(ios::fixed);\n };\n} io_init;\ntemplate <typename T>\ninline bool chmin(T &a, T b)\n{\n if (a > b)\n {\n a = b;\n return true;\n }\n return false;\n}\ntemplate <typename T>\ninline bool chmax(T &a, T b)\n{\n if (a < b)\n {\n a = b;\n return true;\n }\n return false;\n}\ninline ll CEIL(ll a, ll b)\n{\n return (a + b - 1) / b;\n}\ntemplate <typename A, size_t N, typename T>\ninline void Fill(A (&array)[N], const T &val)\n{\n fill((T *)array, (T *)(array + N), val);\n}\ntemplate <typename T, typename U>\nconstexpr istream &operator>>(istream &is, pair<T, U> &p) noexcept\n{\n is >> p.first >> p.second;\n return is;\n}\ntemplate <typename T, typename U>\nconstexpr ostream &operator<<(ostream &os, pair<T, U> &p) noexcept\n{\n os << p.first << \" \" << p.second;\n return os;\n}\n#pragma endregion\n\nusing Real = ld;\nusing Point = complex<Real>;\n\ninline bool eq(Real a, Real b) { return fabs(b - a) < EPS; }\ninline bool eq(Point a, Point b) { return fabs(b - a) < EPS; }\n\nPoint operator*(const Point &p, const Real &d)\n{\n return Point(real(p) * d, imag(p) * d);\n}\n\nistream &operator>>(istream &is, Point &p)\n{\n Real a, b;\n is >> a >> b;\n p = Point(a, b);\n return is;\n}\n\nostream &operator<<(ostream &os, Point &p)\n{\n return os << fixed << setprecision(20) << p.real() << \" \" << p.imag();\n}\n\n// 点 p を反時計回りに theta 回転\ninline Point rotate(Real theta, const Point &p)\n{\n return Point(cos(theta) * p.real() - sin(theta) * p.imag(), sin(theta) * p.real() + cos(theta) * p.imag());\n}\n\nReal radian_to_degree(Real r)\n{\n return (r * 180.0 / PI);\n}\n\nReal degree_to_radian(Real d)\n{\n return (d * PI / 180.0);\n}\n\n// a-b-c の角度のうち小さい方を返す\nReal get_angle(Point a, Point b, Point c)\n{\n a -= b, c -= b;\n Real alpha = atan2(a.imag(), a.real()), beta = atan2(c.imag(), c.real());\n if (alpha > beta)\n swap(alpha, beta);\n Real theta = (beta - alpha);\n return min(theta, 2 * acos(-1) - theta);\n}\n\n// a-b-c の角度([0,2π)、a を反時計回りに回転させてcに重ねる角度)\nReal get_angle2(Point a, Point b, Point c)\n{\n a -= b, c -= b;\n Real theta = atan2(imag(c), real(c)) - atan2(imag(a), real(a));\n while (theta < 0)\n theta += PI * 2;\n while (theta > PI * 2)\n theta -= PI * 2;\n return theta;\n}\n\n// a-b-c の角度([0,2π)、p を間に含む方)\nReal get_angle2(const Point &a, const Point &b, const Point &c, const Point &p)\n{\n if (get_angle2(a, b, p) + get_angle2(p, b, c) < PI * 2)\n return get_angle2(a, b, c);\n else\n return get_angle2(c, b, a);\n}\n\nnamespace std\n{\n bool operator<(const Point &a, const Point &b)\n {\n return !eq(a.real(), b.real()) ? a.real() < b.real() : a.imag() < b.imag();\n }\n} // namespace std\n\nstruct Line\n{\n Point a, b;\n Line() = default;\n Line(Point a, Point b) : a(a), b(b) {}\n Line(Real A, Real B, Real C) // Ax + By = C\n {\n if (eq(A, 0))\n a = Point(0, C / B), b = Point(1, C / B);\n else if (eq(B, 0))\n b = Point(C / A, 0), b = Point(C / A, 1);\n else\n a = Point(0, C / B), b = Point(C / A, 0);\n }\n\n friend ostream &operator<<(ostream &os, Line &p)\n {\n return os << p.a << \" to \" << p.b;\n }\n\n friend istream &operator>>(istream &is, Line &a)\n {\n return is >> a.a >> a.b;\n }\n};\n\n// Ax + By = C\ntuple<Real, Real, Real> parameter(const Line &l)\n{\n Real A = imag(l.b) - imag(l.a);\n Real B = real(l.a) - real(l.b);\n Real C = real(l.a) * A + imag(l.a) * B;\n return {A, B, C};\n}\n\nstruct Segment : Line\n{\n Segment() = default;\n Segment(Point a, Point b) : Line(a, b) {}\n};\n\nstruct Circle\n{\n Point p;\n Real r;\n Circle() = default;\n Circle(Point p, Real r) : p(p), r(r) {}\n};\n\nusing Points = vector<Point>;\nusing Polygon = vector<Point>;\nusing Segments = vector<Segment>;\nusing Lines = vector<Line>;\nusing Circles = vector<Circle>;\n\ninline Real cross(const Point &a, const Point &b)\n{\n return real(a) * imag(b) - imag(a) * real(b);\n}\n\ninline Real dot(const Point &a, const Point &b)\n{\n return real(a) * real(b) + imag(a) * imag(b);\n}\n\n// 直線がx軸となす角 [0, π)\n// to do: verify\ninline Real get_angle(const Line &l)\n{\n Point p = l.a - l.b;\n if (imag(p) < 0)\n p *= -1;\n return get_angle2(Point(1, 0), Point(0, 0), p);\n}\n\n// 2直線がなす角 [0, π/2]\n// to do: verify\ninline Real get_angle(const Line &l1, const Line &l2)\n{\n Real theta = get_angle(l1) - get_angle(l2);\n if (theta < 0)\n theta += PI;\n return theta >= PI / 2.0 ? theta - PI / 2.0 : theta;\n}\n\n// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_1_C\n// 点の回転方向\nint ccw(const Point &a, Point b, Point c)\n{\n b = b - a, c = c - a;\n if (cross(b, c) > EPS)\n return +1; // \"COUNTER_CLOCKWISE\"\n if (cross(b, c) < -EPS)\n return -1; // \"CLOCKWISE\"\n if (dot(b, c) < 0)\n return +2; // \"ONLINE_BACK\"\n if (norm(b) < norm(c))\n return -2; // \"ONLINE_FRONT\"\n return 0; // \"ON_SEGMENT\"\n}\n\n// p, q を m : n に内分する点\ninline Point internal(const Point &p, const Point &q, Real m, Real n)\n{\n return (n * p + m * q) / (m + n);\n}\n\n// p, q を m : n に外分する点\ninline Point external(const Point &p, const Point &q, Real m, Real n)\n{\n return internal(p, q, m, -n);\n}\n\n// 垂直ベクトル\ninline Point orthvector(const Point p)\n{\n return Point(imag(p), -real(p));\n}\n\n// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_2_A\n// 平行判定\ninline bool parallel(const Line &a, const Line &b)\n{\n return eq(cross(a.b - a.a, b.b - b.a), 0.0);\n}\n\n// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_2_A\n// 垂直判定\ninline bool orthogonal(const Line &a, const Line &b)\n{\n return eq(dot(a.a - a.b, b.a - b.b), 0.0);\n}\n\n// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_1_A\n// 射影\n// 直線 l に p から垂線を引いた交点を求める\ninline Point projection(const Line &l, const Point &p)\n{\n double t = dot(p - l.a, l.a - l.b) / norm(l.a - l.b);\n return l.a + (l.a - l.b) * t;\n}\n\ninline Point projection(const Segment &l, const Point &p)\n{\n double t = dot(p - l.a, l.a - l.b) / norm(l.a - l.b);\n return l.a + (l.a - l.b) * t;\n}\n\n// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_1_B\n// 反射\n// 直線 l を対称軸として点 p と線対称にある点を求める\ninline Point reflection(const Line &l, const Point &p)\n{\n return p + (projection(l, p) - p) * 2.0;\n}\n\n// 点 p を通り直線 l に垂直な直線\ninline Line verticalline(const Line &l, const Point &p)\n{\n return Line(p, p + orthvector(l.a - l.b));\n}\n\n// 点 p, q の垂直二等分線\ninline Line bisector(const Point &p, const Point &q)\n{\n Line l(p, q);\n Point m = internal(p, q, 1, 1);\n return verticalline(l, m);\n}\ninline Line bisector(const Segment &sg)\n{\n return bisector(sg.a, sg.b);\n}\n\ninline bool intersect(const Line &l, const Point &p)\n{\n return abs(ccw(l.a, l.b, p)) != 1;\n}\n\ninline bool intersect(const Line &l, const Line &m)\n{\n return abs(cross(l.b - l.a, m.b - m.a)) > EPS || abs(cross(l.b - l.a, m.b - l.a)) < EPS;\n}\n\ninline bool intersect(const Segment &s, const Point &p)\n{\n return ccw(s.a, s.b, p) == 0;\n}\n\ninline bool intersect(const Line &l, const Segment &s)\n{\n return cross(l.b - l.a, s.a - l.a) * cross(l.b - l.a, s.b - l.a) < EPS;\n}\n\ninline Real distance(const Line &l, const Point &p);\n\ninline bool intersect(const Circle &c, const Line &l)\n{\n return distance(l, c.p) <= c.r + EPS;\n}\n\ninline bool intersect(const Circle &c, const Point &p)\n{\n return abs(abs(p - c.p) - c.r) < EPS;\n}\n\n// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_2_B\nbool intersect(const Segment &s, const Segment &t)\n{\n 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;\n}\n\nint intersect(const Circle &c, const Segment &l)\n{\n if (norm(projection(l, c.p) - c.p) - c.r * c.r > EPS)\n return 0;\n auto d1 = abs(c.p - l.a), d2 = abs(c.p - l.b);\n if (d1 < c.r + EPS && d2 < c.r + EPS)\n return 0;\n if (d1 < c.r - EPS && d2 > c.r + EPS || d1 > c.r + EPS && d2 < c.r - EPS)\n return 1;\n const Point h = projection(l, c.p);\n if (dot(l.a - h, l.b - h) < 0)\n return 2;\n return 0;\n}\n\n// 共通接戦の本数\n// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_7_A&lang=jp\nint intersect(Circle c1, Circle c2)\n{\n if (c1.r < c2.r)\n swap(c1, c2);\n Real d = abs(c1.p - c2.p);\n if (c1.r + c2.r < d)\n return 4;\n if (eq(c1.r + c2.r, d))\n return 3;\n if (c1.r - c2.r < d)\n return 2;\n if (eq(c1.r - c2.r, d))\n return 1;\n return 0;\n}\n\ninline Real distance(const Point &a, const Point &b)\n{\n return abs(a - b);\n}\n\ninline Real distance(const Line &l, const Point &p)\n{\n return abs(p - projection(l, p));\n}\n\ninline Real distance(const Line &l, const Line &m)\n{\n return intersect(l, m) ? 0 : distance(l, m.a);\n}\n\ninline Real distance(const Segment &s, const Point &p)\n{\n Point r = projection(s, p);\n if (intersect(s, r))\n return abs(r - p);\n return min(abs(s.a - p), abs(s.b - p));\n}\n\n// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_2_D\nReal distance(const Segment &a, const Segment &b)\n{\n if (intersect(a, b))\n return 0;\n return min({distance(a, b.a), distance(a, b.b), distance(b, a.a), distance(b, a.b)});\n}\n\nReal distance(const Line &l, const Segment &s)\n{\n if (intersect(l, s))\n return 0;\n return min(distance(l, s.a), distance(l, s.b));\n}\n\nPoint crosspoint(const Line &l, const Line &m)\n{\n Real A = cross(l.b - l.a, m.b - m.a);\n Real B = cross(l.b - l.a, l.b - m.a);\n if (eq(abs(A), 0.0) && eq(abs(B), 0.0))\n return m.a;\n return m.a + (m.b - m.a) * B / A;\n}\n\n// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_2_C\nPoint crosspoint(const Segment &l, const Segment &m)\n{\n return crosspoint(Line(l), Line(m));\n}\n\n// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_7_D\npair<Point, Point> crosspoint(const Circle &c, const Line l)\n{\n Point pr = projection(l, c.p);\n Point e = (l.b - l.a) / abs(l.b - l.a);\n if (eq(distance(l, c.p), c.r))\n return {pr, pr};\n double base = sqrt(c.r * c.r - norm(pr - c.p));\n return {pr - e * base, pr + e * base};\n}\n\npair<Point, Point> crosspoint(const Circle &c, const Segment &l)\n{\n Line aa = Line(l.a, l.b);\n if (intersect(c, l) == 2)\n return crosspoint(c, aa);\n auto ret = crosspoint(c, aa);\n if (dot(l.a - ret.first, l.b - ret.first) < 0)\n ret.second = ret.first;\n else\n ret.first = ret.second;\n return ret;\n}\n\n// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_7_E\npair<Point, Point> crosspoint(const Circle &c1, const Circle &c2)\n{\n Real d = abs(c1.p - c2.p);\n Real a = acos((c1.r * c1.r + d * d - c2.r * c2.r) / (2 * c1.r * d));\n Real t = atan2(c2.p.imag() - c1.p.imag(), c2.p.real() - c1.p.real());\n Point p1 = c1.p + Point(cos(t + a) * c1.r, sin(t + a) * c1.r);\n Point p2 = c1.p + Point(cos(t - a) * c1.r, sin(t - a) * c1.r);\n return {p1, p2};\n}\n\nint n;\nReal w;\nvector<Point> p;\nvector<Real> h;\n\nbool ok(Point &q, Real r)\n{\n Real x = real(q), y = imag(q);\n if (r < w)\n {\n for (Real d : {x - 0, 100 - x, y - 0, 100 - y})\n {\n if (d < r)\n return false;\n }\n }\n else\n {\n for (Real d : {x - 0, 100 - x, y - 0, 100 - y})\n {\n if (d < 0)\n return false;\n d = sqrt(d * d + (r - w) * (r - w));\n if (d < r)\n return false;\n }\n }\n rep(i, n)\n {\n Real d;\n if (r < h[i])\n {\n d = distance(q, p[i]);\n }\n else\n {\n d = distance(q, p[i]);\n d = sqrt(d * d + (r - h[i]) * (r - h[i]));\n }\n if (d < r)\n return false;\n }\n return true;\n}\n\nbool check(Real r)\n{\n vector<Circle> c(n);\n rep(i, n)\n {\n Real d;\n if (r < h[i])\n {\n d = r;\n }\n else\n {\n d = r * r - (r - h[i]) * (r - h[i]);\n if (d < -EPS)\n return false;\n chmax(d, Real(0.0));\n d = sqrt(d);\n }\n d += EPS;\n c[i] = Circle(p[i], d);\n }\n Points ps;\n rep(i, n) REP(j, i + 1, n)\n {\n int res = intersect(c[i], c[j]);\n if (1 <= res and res <= 3)\n {\n auto [p1, p2] = crosspoint(c[i], c[j]);\n ps.push_back(p1);\n ps.push_back(p2);\n }\n }\n\n Line l[4];\n Real d;\n if (r < w)\n d = r;\n else\n {\n d = r * r - (r - w) * (r - w);\n if (d < -EPS)\n return false;\n chmax(d, Real(0.0));\n d = sqrt(d);\n }\n d += EPS;\n Point LD(d, d), LU(d, 100 - d), RD(100 - d, d), RU(100 - d, 100 - d);\n l[0] = Line(LU, LD);\n l[1] = Line(LD, RD);\n l[2] = Line(RD, RU);\n l[3] = Line(RU, LU);\n ps.push_back(LD);\n rep(li, 4)\n {\n rep(i, n)\n {\n if (intersect(c[i], l[li]))\n {\n auto [p1, p2] = crosspoint(c[i], l[li]);\n ps.push_back(p1);\n ps.push_back(p2);\n }\n }\n }\n\n for (auto pi : ps)\n {\n if (ok(pi, r))\n return true;\n }\n return false;\n}\n\nvoid solve()\n{\n cin >> n >> w;\n if (w == 0)\n exit(0);\n p.resize(n);\n h.resize(n);\n rep(i, n) cin >> p[i] >> h[i];\n Real lo = 0, hi = 1e5;\n rep(ti, 100)\n {\n Real mi = (lo + hi) / 2.0;\n if (check(mi))\n lo = mi;\n else\n hi = mi;\n }\n cout << hi << \"\\n\";\n}\n\nint main()\n{\n while (1)\n solve();\n}", "accuracy": 1, "time_ms": 890, "memory_kb": 3884, "score_of_the_acc": -0.8355, "final_rank": 12 }, { "submission_id": "aoj_1342_4828612", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\ntemplate<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; }\ntemplate<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; }\nusing ll = long long;\nusing P = pair<ll, ll>;\nconst long double PI = acos(-1.0L);\nll GCD(ll a, ll b) { return b?GCD(b, a%b):a; }\nll LCM(ll a, ll b) { return a/GCD(a, b)*b; }\n\nusing CP = complex<long double>;\n\nconst long double EPS = 1e-10; // 許容する誤差ε\n#define EQ(a, b) (abs((a)-(b)) < EPS) // 2つのスカラーが等しいかどうか\n#define EQV(a, b) (EQ((a).real(), (b).real()) && EQ((a).imag(), (b).imag())) // 2つのベクトルが等しいかどうか\n\n// double length = abs(a); // ベクトルaの絶対値\n// double distance = abs(a-b); // 2点a,b間の距離\n// CP b = a/abs(a); // ベクトルaの単位ベクトル\n// CP n1 = a*CP(0,+1); CP n2 = a*CP(0,-1); // ベクトルaの法線ベクトル\n// CP un1 = (a*CP(0,+1)/abs(a)); CP un2 = (a*CP(0,-1)/abs(a)); // ベクトルaの単位法線ベクトル\n\n// 内積(dot product) : a・b = |a||b|cosΘ\nlong double dot(CP a, CP b) {\n return (a.real()*b.real() + a.imag()*b.imag());\n}\n\n// 外積(cross product) : a×b = |a||b|sinΘ\nlong double cross(CP a, CP b) {\n return (a.real()*b.imag() - a.imag()*b.real());\n}\n\n// 直線の表現\nstruct Segment {\n CP s, t;\n Segment(long double sx = 0.0L, long double sy = 0.0L,\n long double tx = 0.0L, long double ty = 0.0L)\n : s(CP(sx, sy)), t(CP(tx, ty)) {}\n Segment(CP _s, CP _t) : s(_s), t(_t) {}\n}; typedef Segment Line;\n\n// 2直線の直交判定 : a⊥b ⇔ dot(a,b) = 0\nint is_orthogonal(CP a1, CP a2, CP b1, CP b2) {\n return EQ(dot(a1-a2, b1-b2), 0.0);\n}\n\n// 2直線の平行判定 : a//b ⇔ cross(a,b) = 0\nint is_parallel(CP a1, CP a2, CP b1, CP b2) {\n return EQ(cross(a1-a2, b1-b2), 0.0);\n}\n\n// 点cが直線a,b上にあるかないか\nint is_point_on_line(CP a, CP b, CP c) {\n return EQ(cross(b-a, c-a), 0.0);\n}\n\n// 点cが線分a,b上にあるかないか\nint is_point_on_lines(CP a, CP b, CP c) {\n // |a-c|+|c-b| <= |a-b|なら線分上\n return (abs(a-c)+abs(c-b) < abs(a-b)+EPS);\n}\n\n// a1,a2を端点とする線分とb1,b2を端点とする線分の交差判定\nint is_intersected_lines(CP a1, CP a2, CP b1, CP b2) {\n if(is_parallel(a1, a2, b1, b2)) {\n // 平行なので線分の重なり判定\n return is_point_on_lines(a1, a2, b1) || is_point_on_lines(a1, a2, b2) ||\n is_point_on_lines(b1, b2, a1) || is_point_on_lines(b1, b2, a2);\n }\n return (cross(a2-a1, b1-a1)*cross(a2-a1, b2-a1) < EPS) &&\n (cross(b2-b1, a1-b1)*cross(b2-b1, a2-b1) < EPS);\n}\n\n// a1,a2を端点とする線分とb1,b2を端点とする線分の交点計算\nCP intersection_lines(CP a1, CP a2, CP b1, CP b2) {\n CP b = b2-b1;\n long double d1 = abs(cross(b, a1-b1));\n long double d2 = abs(cross(b, a2-b1));\n long double t = d1/(d1+d2);\n return a1+(a2-a1)*t;\n}\n\n// a1,a2を通る直線とb1,b2を通る直線の交差判定\nint is_intersected_line(CP a1, CP a2, CP b1, CP b2) {\n return !EQ(cross(a1-a2, b1-b2), 0.0);\n}\n\n// a1,a2を通る直線とb1,b2を通る直線の交点計算(平行ではない前提)\nCP intersection_line(CP a1, CP a2, CP b1, CP b2) {\n CP a = a2-a1; CP b = b2-b1;\n return a1 + a*cross(b, b1-a1)/cross(b, a);\n}\n\n// 点a,bを通る直線と点cとの距離\nlong double distance_line_p(CP a, CP b, CP c) {\n return abs(cross(b-a, c-a))/abs(b-a);\n}\n\n// 点a,bを端点とする線分と点cとの距離\nlong double distance_lines_p(CP a, CP b, CP c) {\n if(dot(b-a, c-a) < EPS) return abs(c-a);\n if(dot(a-b, c-b) < EPS) return abs(c-b);\n return abs(cross(b-a, c-a))/abs(b-a);\n}\n\n// 点a1,a2を端点とする線分と点b1,b2を端点とする線分の最短距離\nlong double distance_lines_lines(CP a1, CP a2, CP b1, CP b2) {\n long double res = 1e18;\n if(is_intersected_lines(a1, a2, b1, b2)) return 0.0L;\n res = min(res, distance_lines_p(a1, a2, b1));\n res = min(res, distance_lines_p(a1, a2, b2));\n res = min(res, distance_lines_p(b1, b2, a1));\n res = min(res, distance_lines_p(b1, b2, a2));\n return res;\n}\n\n// s,tを通る直線に対する点pの射影\nCP projection(CP s, CP t, CP p) {\n if(EQV(s, t)) return s;\n CP base = t-s;\n return s + base*(dot(p-s, base)/norm(base));\n}\n\n// s,tを通る直線に対する点pの反射\nCP reflection(CP s, CP t, CP p) {\n CP tmp = projection(s, t, p) - p;\n return p + tmp*2.0L;\n}\n\n// 角度Θ回転\nCP translate(CP v, long double theta) {\n CP res = v * CP(cosl(theta), sinl(theta));\n return res;\n}\n\n// n多角形の面積計算\nlong double polygon_area(const vector<CP> &v) {\n int n = v.size();\n long double res = 0;\n for(int i = 0; i < n; ++i) {\n res += cross(v[(i+n-1)%n], v[(i+n)%n]);\n }\n return fabsl(res)/2.0L;\n}\n\n// n多角形の凸性判定\nint is_convex(const vector<CP> &v) {\n int n = v.size();\n for(int i = 0; i < n; ++i) {\n if(cross(v[(i+1)%n]-v[i], v[(i+2)%n]-v[(i+1)%n]) < -EPS) return 0;\n }\n return 1;\n}\n\n// 凸包(全ての頂点が一直線に並ぶときは例外)\nvector<CP> convex_hull(vector<CP> &v) {\n // x座標→y座標で頂点をソート\n auto lmd = [&](const CP &l, const CP &r) {\n if(l.imag() != r.imag()) return l.imag() < r.imag();\n return l.real() < r.real();\n return l.imag() < r.imag();\n };\n vector<CP> res; // 凸包を構成する頂点\n int n = v.size();\n sort(v.begin(), v.end(), lmd);\n bool flgx = true;\n for(int i = 0; i < n; ++i) {\n if(v[i].real() != v[(i+1)%n].real()) {\n flgx = false;\n break;\n }\n }\n bool flgy = true;\n for(int i = 0; i < n; ++i) {\n if(v[i].imag() != v[(i+1)%n].imag()) {\n flgy = false;\n break;\n }\n }\n if(flgx || flgy) {\n res.emplace_back(v[0]);\n res.emplace_back(v[n-1]);\n return res;\n }\n int k = 0;\n res.resize(n*2);\n // 下側凸包\n for(int i = 0; i < n; ++i) {\n while(k > 1 && cross(res[k-1]-res[k-2], v[i]-res[k-1]) < 0) {\n --k;\n }\n res[k++] = v[i];\n }\n // 上側凸包\n for(int i = n-2, t = k; i >= 0; --i) {\n while(k > t && cross(res[k-1]-res[k-2], v[i]-res[k-1]) < 0) {\n --k;\n }\n res[k++] = v[i];\n }\n res.resize(k-1);\n return res;\n}\n\n// 凸多角形の直径\nlong double convex_diameter(vector<CP> &v) {\n vector<CP> cv = convex_hull(v);\n int n = cv.size();\n if(n == 2) return abs(cv[0]-cv[1]); // 凸包が潰れている例外処理\n int i = 0, j = 0; // x軸方向に最も遠い点対\n for(int k = 0; k < n; ++k) {\n if(cv[k].real() < cv[i].real()) i = k;\n if(cv[k].real() > cv[j].real()) j = k;\n }\n long double res = 0;\n int si = i, sj = j;\n while(i != sj || j != si) { // 180度回転で終了\n res = max(res, abs(cv[i]-cv[j]));\n if(cross(cv[(i+1)%n]-cv[i], cv[(j+1)%n]-cv[j]) < 0) {\n (++i) %= n;\n }else {\n (++j) %= n;\n }\n }\n return res;\n}\n\n// 凸多角形を点s,tを通る直線で切断(左側が取得できる)\nvector<CP> convex_cut(const vector<CP> &v, const CP &s, const CP &t) {\n vector<CP> res;\n int n = v.size();\n for(int i = 0; i < n; ++i) {\n CP nows = v[i], nowt = v[(i+1)%n];\n if(cross(t-s, nows-s) >= -EPS) res.emplace_back(nows);\n if(cross(t-s, nows-s)*cross(t-s, nowt-s) < 0) {\n res.emplace_back(intersection_line(s, t, nows, nowt));\n }\n }\n return res;\n}\n\n// n多角形に対する点pの包含関係(自己交差多角形は例外)\nint contain_polygon_point(const vector<CP> &v, CP p) {\n int contain = 0, on_segment = 0;\n int n = v.size();\n for(int i = 0; i < n; ++i) {\n on_segment |= is_point_on_lines(v[i], v[(i+1)%n], p); // 辺上判定\n CP s = v[i]-p, t = v[(i+1)%n]-p;\n if(s.imag() > t.imag()) swap(s, t); // 下側を基準にする\n if(s.imag()*t.imag() <= 0 && t.imag() > 0 && cross(s, t) > 0) {\n contain = !contain; // 交差回数が奇数なら内側\n }\n }\n if(on_segment) return 1; // 辺上\n if(contain) return 2; // 内側\n return 0; // 外側\n}\n\n// 最近点対距離\nlong double closest_pair(vector<CP> &v, int l = -1, \n int r = -1, bool reqsqrt = 0) {\n if(l == r && l == -1) {\n l = 0; r = v.size(); reqsqrt = 1;\n // x座標→y座標で昇順ソート\n auto lmd = [&](const CP &l, const CP &r) {\n if(l.real() != r.real()) return l.real() < r.real();\n return l.imag() < r.imag();\n };\n sort(v.begin(), v.end(), lmd);\n }\n if(r-l < 2) return 1e18; // 2点存在しない\n if(r-l == 2) { // ちょうど2点の時\n if(v[l].imag() > v[l+1].imag()) swap(v[l], v[l+1]);\n if(reqsqrt) return abs(v[l]-v[l+1]);\n return norm(v[l]-v[l+1]);\n }\n // 2点以上に関して分割統治法\n int mid = (l+r)/2;\n long double x = v[mid].real(); // 分断する線のx座標\n // 左半分,右半分について再帰,同一領域内の最小距離resを求める\n long double res = min(closest_pair(v, l, mid), closest_pair(v, mid, r));\n auto f = [](CP pl, CP pr) { return pl.imag() < pr.imag(); };\n inplace_merge(v.begin()+l, v.begin()+mid, v.begin()+r, f);\n vector<CP> tmp;\n // 異なる領域の2点について最小距離res未満で探索\n for(int i = l; i < r; ++i) {\n long double dx = abs(v[i].real()-x);\n int tsize = tmp.size();\n if(dx*dx >= res) continue;\n for(int j = 0; j < tsize; ++j) {\n CP delta = v[i]-tmp[tsize-1-j];\n if(delta.imag()*delta.imag() >= res) break;\n res = min(res, norm(delta));\n }\n tmp.emplace_back(v[i]);\n }\n if(reqsqrt) res = sqrtl(res);\n return res;\n}\n\n// 円の表現\nstruct Circle {\n CP o;\n long double r;\n Circle(long double _x = 0.0L, long double _y = 0.0L,\n long double _r = 0.0L)\n : o(CP(_x, _y)), r(_r) {}\n Circle(CP _o, long double _r = 0.0) : o(_o), r(_r) {}\n}; typedef vector<CP> Polygon;\n\n// 2円の位置関係\nint is_cross_circles(Circle l, Circle r) {\n long double distlr = abs(l.o-r.o);\n if(l.r+r.r+EPS < distlr) return 4; // 交点無し外側\n if(r.r+distlr+EPS < l.r) return -2; // 交点無し内側(R in L)\n if(l.r+distlr+EPS < r.r) return 2; // 交点無し内側(L in R)\n if(abs(l.r+r.r-distlr) < EPS) return 3; // 外接\n if(abs(l.r+distlr-r.r) < EPS) return -1; // 内接(R in L)\n if(abs(r.r+distlr-l.r) < EPS) return 1; // 内接(L in R)\n return 0; // 2点で交わる\n}\n\n// 円に対する点の包含\nbool contain_point(Circle c, CP p) {\n long double dist = abs(c.o-p);\n return dist < c.r+EPS;\n}\n\n// 円と直線の交点\nvector<CP> intersection_circle_line(Circle ci, CP s, CP t) {\n vector<CP> res(2); // 1点の場合同じ座標\n res[0] = res[1] = projection(s, t, ci.o); // 2等分点の座標\n long double d = ci.r*ci.r - norm(res[0]-ci.o);\n if(d <= EPS || t == s) return res;\n CP ust = (t-s)/abs(t-s); // stの単位ベクトル\n res[0] += (ust*sqrtl(d));\n res[1] -= (ust*sqrtl(d));\n if(res[0].real() > res[1].real() ||\n (res[0].real() == res[1].real() && res[0].imag() > res[1].imag())) {\n swap(res[0], res[1]);\n }\n return res;\n}\n\n// 2円の交点(1つは交点が存在する)\nvector<CP> intersection_circle_circle(Circle c1, Circle c2) {\n vector<CP> res(2); // 1点の場合同じ座標\n long double d = abs(c1.o-c2.o);\n long double arg = acosl((c1.r*c1.r+d*d-c2.r*c2.r)/(2.0L*c1.r*d));\n CP c1p = (c2.o-c1.o)*CP(cosl(arg), sinl(arg));\n CP uc1p = c1p/abs(c1p); // c1の中心から交点p方向への単位ベクトル\n res[0] = c1.o + c1.r*uc1p;\n res[1] = reflection(c1.o, c2.o, res[0]); // p,qの位置関係は反射\n if(res[0].real() > res[1].real() ||\n (res[0].real() == res[1].real() && res[0].imag() > res[1].imag())) {\n swap(res[0], res[1]);\n }\n return res;\n}\n\n// 線分lrの垂直二等分線\nLine vertical_bisector(CP l, CP r) {\n Circle c1 = Circle(l, abs(l-r)), c2 = Circle(r, abs(l-r));\n vector<CP> res = intersection_circle_circle(c1, c2);\n if(cross(r-l, res[0]-l) > 0) swap(res[0], res[1]);\n return Line(res[0], res[1]);\n}\n\n// 点pを通る円の接線の接点\nvector<CP> contact_circle_point(Circle ci, CP p) {\n vector<CP> res(2);\n long double d = abs(ci.o-p);\n if(abs(d-ci.r) <= EPS) {\n res[0] = res[1] = p;\n }else if(d < ci.r) {\n res[0] = res[1] = CP(-1e18, -1e18);\n }else {\n long double arg = asinl(ci.r/d);\n CP p0 = (ci.o-p)*CP(cosl(arg), sinl(arg));\n CP up0 = p0/abs(p0);\n res[0] = p + d*cosl(arg)*up0;\n res[1] = reflection(p, ci.o, res[0]);\n if(res[0].real() > res[1].real() || \n (res[0].real() == res[1].real() && res[0].imag() > res[1].imag())) {\n swap(res[0], res[1]);\n }\n }\n return res;\n}\n\n// 2円の共通接線\nvector<Line> tangent_circle_circle(Circle cl, Circle cr) {\n vector<Line> res;\n if(cl.r < cr.r) swap(cl, cr);\n long double g = abs(cl.o-cr.o);\n if(abs(g-0.0L) <= EPS) return res; // 内包\n CP hor = (cr.o-cl.o)/g;\n CP ver = hor*CP(cosl(PI*0.5L), sinl(PI*0.5L));\n for(int s : {-1, 1}) {\n long double h = (cl.r + (long double)s*cr.r)/g;\n if(abs(1-h*h) <= EPS) {\n // 2円が接しているときの共通接線\n res.emplace_back(cl.o + hor*cl.r, cl.o + (hor+ver)*cl.r);\n }else if(1-h*h > 0) {\n // 内側に引かれる2接線+外側に引かれる2接線\n CP nhor = hor*h, nver = ver*sqrtl(1-h*h);\n res.emplace_back(cl.o + (nhor+nver)*cl.r, \n cr.o - (nhor+nver)*(cr.r*(long double)s));\n res.emplace_back(cl.o + (nhor-nver)*cl.r, \n cr.o - (nhor-nver)*(cr.r*(long double)s));\n }\n }\n return res;\n}\n\n// 三角形の内接円\nCircle inscribed_circle(CP A, CP B, CP C) {\n if(cross(B-A, C-A) < 0) swap(B, C);\n long double a = abs(B-C), b = abs(C-A), c = abs(A-B);\n long double alpha = acosl((b*b+c*c-a*a)/(2.0L*b*c));\n long double beta = acosl((c*c+a*a-b*b)/(2.0L*c*a));\n // AとxABを通る直線とBとxBCを通る直線の交点が内心\n CP I = intersection_line(A, A+translate(B-A, alpha/2.0L), B, B+translate(C-B, beta/2.0L));\n // ABとIの最短距離が内心半径\n long double Ir = distance_lines_p(A, B, I);\n return Circle(I, Ir);\n}\n\n// 三角形の外接円\nCircle circumscribed_circle(CP A, CP B, CP C) {\n // 2つの垂直二等分線の交点\n Line AB = vertical_bisector(A, B);\n Line BC = vertical_bisector(B, C);\n CP O = intersection_line(AB.s, AB.t, BC.s, BC.t);\n long double Or = abs(A-O);\n return Circle(O, Or);\n}\n\n// 2円の共通面積\nlong double common_area(Circle cl, Circle cr) {\n if(cl.r < cr.r) swap(cl, cr);\n int num = is_cross_circles(cl, cr);\n if(num >= 3) return 0.0L;\n if(num >= 1) return PI*cr.r*cr.r;\n if(num < 0) return PI*cl.r*cl.r;\n long double d = abs(cl.o-cr.o);\n long double res = 0.0L;\n for(int i = 0; i < 2; ++i) {\n long double theta = 2.0L * acosl((d*d+cl.r*cl.r-cr.r*cr.r)/(2.0L*d*cl.r));\n res += (theta-sinl(theta))*cl.r*cl.r/2.0L;\n swap(cl, cr);\n }\n return res;\n}\n\n// 円と多角形の共通面積\nlong double common_area(Polygon p, Circle c) {\n if(p.size() < 3) return 0.0L;\n function<long double(Circle, CP, CP)> dfs = [&](Circle c, CP a, CP b) {\n CP va = c.o-a, vb = c.o-b;\n long double f = cross(va, vb), res = 0;\n if(EQ(f, 0.0)) return res;\n if(max(abs(va), abs(vb)) < c.r+EPS) return f;\n CP d(dot(va, vb), cross(va, vb));\n if(distance_lines_p(a, b, c.o) > c.r-EPS) {\n return c.r*c.r*(atan2(d.imag(), d.real()));\n }\n auto u = intersection_circle_line(c, a, b);\n if(u.empty()) return res;\n if(u.size() > 1 && dot(u[1]-u[0], a-u[0]) > 0) swap(u[0], u[1]);\n u.emplace(u.begin(), a);\n u.emplace_back(b);\n for(int i = 1; i < (int)u.size(); ++i) res += dfs(c, u[i-1], u[i]);\n return res;\n };\n long double res = 0.0L;\n for(int i = 0; i < (int)p.size(); ++i) res += dfs(c, p[i], p[(i+1)%p.size()]);\n return res/2;\n}\n\n// 3点の位置関係を判定\nint calc_clockwise(CP p0, CP p1, CP p2) {\n CP x = p1-p0, y = p2-p0;\n if(cross(x, y) > EPS) return 1; // \"COUNTER_CLOCKWISE\"\n if(cross(x, y) < -EPS) return -1; // \"CLOCKWISE\"\n if(dot(x, y) < 0) return 2; // \"ONLINE_BACK\"\n if(norm(x) < norm(y)) return -2; // \"ONLINE_FRONT\"\n return 0; // \"ON_SEGMENT\" \n}\n\nint n;\nlong double w;\nstruct xyz {\n CP pos;\n long double z;\n};\n\nint main() {\n while(1) {\n cin >> n >> w;\n if(n == 0 && w == 0) break;\n vector<xyz> needles;\n for(int i = 0; i < n; ++i) {\n long double x, y, h;\n cin >> x >> y >> h;\n xyz indata;\n indata.pos = CP(x, y);\n indata.z = h;\n needles.emplace_back(indata);\n } \n\n // (0,0), (0,100), (100,0), (100,100)に囲まれている\n // 針を中心とした円の交点と4隅から半径r縮小した正方形との交点が候補になる\n long double low = 0.0L, high = 1e9;\n for(int i = 0; i < 100; ++i) {\n long double mid = (low+high)/2.0L;\n auto isOK = [&](long double R) {\n // 2分探索用のチェック\n vector<Circle> cs(n);\n for(int i = 0; i < n; ++i) {\n if(needles[i].z <= R) cs[i].r = sqrtl(2.0L*R*needles[i].z - needles[i].z*needles[i].z);\n else cs[i].r = R;\n cs[i].o = needles[i].pos;\n }\n\n long double margin = (w <= R ? sqrtl(2.0L*R*w - w*w) : R);\n if(margin >= 50.0L) return false; // 半径が50を超えることはない\n\n vector<Segment> ss;\n ss.emplace_back(Segment(CP(margin, margin), CP(100.0L-margin)));\n ss.emplace_back(Segment(CP(margin, margin), CP(margin, 100.0L-margin)));\n ss.emplace_back(Segment(CP(100.0L-margin, margin), CP(100.0L-margin, 100.0L-margin)));\n ss.emplace_back(Segment(CP(margin, 100.0L-margin), CP(100.0L-margin, 100.0L-margin)));\n\n vector<CP> ps; // 調べるべき交点\n // 小さい正方形の4隅\n ps.emplace_back(CP(margin, margin));\n ps.emplace_back(CP(margin, 100.0L-margin));\n ps.emplace_back(CP(100.0L-margin, margin));\n ps.emplace_back(CP(100.0L-margin, 100.0L-margin));\n\n // 円と正方形の交点を列挙する\n for(int i = 0; i < n; ++i) {\n for(int j = 0; j < 4; ++j) {\n vector<CP> inps = intersection_circle_line(cs[i], ss[j].s, ss[j].t);\n for(auto p : inps) ps.emplace_back(p);\n }\n }\n for(int i = 0; i < n; ++i) {\n for(int j = i+1; j < n; ++j) {\n vector<CP> inps = intersection_circle_circle(cs[i], cs[j]);\n for(auto p : inps) ps.emplace_back(p);\n }\n }\n\n bool ok = false;\n for(auto p : ps) {\n bool flg = true;\n for(int i = 0; i < n; ++i) {\n if(abs(p-cs[i].o) < cs[i].r-EPS) flg = false;\n }\n if(flg) {\n if(p.real() >= margin-EPS && p.real()-EPS <= 100.0L-margin &&\n p.imag() >= margin-EPS && p.imag()-EPS <= 100.0L-margin) {\n ok = true;\n }\n }\n }\n return ok;\n };\n if(isOK(mid)) low = mid;\n else high = mid;\n }\n\n cout << fixed << setprecision(5) << low << endl;\n }\n}", "accuracy": 1, "time_ms": 7810, "memory_kb": 3568, "score_of_the_acc": -1.3682, "final_rank": 18 }, { "submission_id": "aoj_1342_4127956", "code_snippet": "#include<iostream>\n#include<string>\n#include<cstdio>\n#include<vector>\n#include<cmath>\n#include<algorithm>\n#include<functional>\n#include<iomanip>\n#include<queue>\n#include<ciso646>\n#include<random>\n#include<map>\n#include<set>\n#include<bitset>\n#include<stack>\n#include<unordered_map>\n#include<utility>\n#include<cassert>\n#include<complex>\nusing namespace std;\n\n//#define int long long\ntypedef long long ll;\n\ntypedef unsigned long long ul;\ntypedef unsigned int ui;\nconst ll mod = 1000000007;\nconst ll INF = (1e+18) + 7;\ntypedef pair<int, int>P;\n#define stop char nyaa;cin>>nyaa;\n#define rep(i,n) for(int i=0;i<n;i++)\n#define per(i,n) for(int i=n-1;i>=0;i--)\n#define Rep(i,sta,n) for(int i=sta;i<n;i++)\n#define rep1(i,n) for(int i=1;i<=n;i++)\n#define per1(i,n) for(int i=n;i>=1;i--)\n#define Rep1(i,sta,n) for(int i=sta;i<=n;i++)\n#define all(v) (v).begin(),(v).end()\ntypedef pair<ll, ll> LP;\ntypedef long double ld;\ntypedef pair<ld, ld> LDP;\nconst ld eps = 1e-6;\nconst ld pi = acos(-1.0);\n//typedef vector<vector<ll>> mat;\ntypedef vector<int> vec;\n\nll mod_pow(ll a, ll n) {\n\tll res = 1;\n\twhile (n) {\n\t\tif (n & 1)res = res * a%mod;\n\t\ta = a * a%mod; n >>= 1;\n\t}\n\treturn res;\n}\n\nstruct modint {\n\tll n;\n\tmodint() :n(0) { ; }\n\tmodint(ll m) :n(m) {\n\t\tif (n >= mod)n %= mod;\n\t\telse if (n < 0)n = (n%mod + mod) % mod;\n\t}\n\toperator int() { return n; }\n};\nbool operator==(modint a, modint b) { return a.n == b.n; }\nmodint operator+=(modint &a, modint b) { a.n += b.n; if (a.n >= mod)a.n -= mod; return a; }\nmodint operator-=(modint &a, modint b) { a.n -= b.n; if (a.n < 0)a.n += mod; return a; }\nmodint operator*=(modint &a, modint b) { a.n = ((ll)a.n*b.n) % mod; return a; }\nmodint operator+(modint a, modint b) { return a += b; }\nmodint operator-(modint a, modint b) { return a -= b; }\nmodint operator*(modint a, modint b) { return a *= b; }\nmodint operator^(modint a, int n) {\n\tif (n == 0)return modint(1);\n\tmodint res = (a*a) ^ (n / 2);\n\tif (n % 2)res = res * a;\n\treturn res;\n}\n\n//ll inv(ll a, ll p) {\n//\treturn (a == 1 ? 1 : (1 - p * inv(p%a, a)) / a + p);\n//}\n//modint operator/(modint a, modint b) { return a * modint(inv(b, mod)); }\n\nint dx[4] = { 1,0,-1,0 };\nint dy[4] = { 0,1,0,-1 };\n\nusing Point = complex<ld>;\nld dot(Point a, Point b) { return real(conj(a)*b); }\nld cross(Point a, Point b) { return imag(conj(a)*b); }\nstruct Line {\n\tPoint a, b;\n};\nstruct Circle {\n\tPoint p; ld r;\n};\n\nPoint proj(Line l, Point p) {\n\tld t = dot(p - l.a, l.a - l.b) / norm(l.a - l.b);\n\treturn l.a + t * (l.a - l.b);\n}\nld dist_lp(Line l, Point p) {\n\treturn abs(p - proj(l, p));\n}\n\nvector<Point> is_lc(Circle c, Line l) {\n\tvector<Point> res;\n\tld d = dist_lp(l, c.p);\n\tif (d > c.r + eps)return res;\n\tld len = (d > c.r) ? 0.0 : sqrt(c.r*c.r - d * d);\n\tPoint nor = (l.a - l.b) / abs(l.a - l.b);\n\tres.push_back(proj(l, c.p) + len * nor);\n\tres.push_back(proj(l, c.p) - len * nor);\n\treturn res;\n}\nvector<Point> is_cc(Circle c1, Circle c2) {\n\tvector<Point> res;\n\tld d = abs(c1.p - c2.p);\n\tld rc = (d*d + c1.r*c1.r - c2.r*c2.r) / (2 * d);\n\tld dfr = c1.r*c1.r - rc * rc;\n\tif (abs(dfr) < eps)dfr = 0.0;\n\tif (dfr < 0.0)return res;\n\tld rs = sqrt(dfr);\n\tPoint diff = (c2.p - c1.p) / d;\n\tres.push_back(c1.p + diff * Point(rc, rs));\n\tif (dfr != 0.0)res.push_back(c1.p + diff * Point(rc, -rs));\n\treturn res;\n}\n\nbool is_in(Circle c, Point p) {\n\tld dist = abs(c.p - p);\n\treturn dist < c.r;\n}\n\nint n;ld w;\nint x[10], y[10], h[10];\nCircle c[10];\n\nbool can(ld r) {\n\tld le;\n\tif (r < w)le = r;\n\telse {\n\t\tle = sqrt(pow(r, 2) - pow(r - w, 2));\n\t}\n\tif (le >= 50)return false;\n\trep(i, n) {\n\t\tc[i].p = { (ld)x[i],(ld)y[i] };\n\t\tif (r < h[i])c[i].r = r;\n\t\telse c[i].r = sqrt(pow(r, 2) - pow(r - h[i], 2));\n\t}\n\n\tld ri = 100 - le;\n\tPoint p[4] = { {le,le},{le,ri},{ri,le},{ri,ri} };\n\tLine l[4] = { {p[0],p[1]},{p[0],p[2]},{p[3],p[1]},{p[3],p[2]} };\n\trep(i, 4) {\n\t\tbool f = true;\n\t\trep(j, n) {\n\t\t\tif (is_in(c[j], p[i])) {\n\t\t\t\tf = false; break;\n\t\t\t}\n\t\t}\n\t\tif (f)return true;\n\t}\n\trep(i, 4) {\n\t\trep(j, n) {\n\t\t\tvector<Point> v = is_lc(c[j], l[i]);\n\t\t\trep(k, v.size()) {\n\t\t\t\t\n\t\t\t\tif (real(v[k]) < le || real(v[k])>ri)continue;\n\t\t\t\tif (imag(v[k]) < le || imag(v[k]) > ri)continue;\n\t\t\t\tbool f = true;\n\t\t\t\trep(l, n) {\n\t\t\t\t\tif (l == j)continue;\n\t\t\t\t\tif (is_in(c[l], v[k])) {\n\t\t\t\t\t\tf = false; break;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\tif (f)return true;\n\t\t\t}\n\t\t}\n\t}\n\trep(i, n)Rep(j,i+1, n) {\n\t\tvector<Point> v = is_cc(c[i], c[j]);\n\t\trep(k, v.size()) {\n\t\t\tif (real(v[k]) < le || real(v[k])>ri)continue;\n\t\t\tif (imag(v[k]) < le || imag(v[k]) > ri)continue;\n\t\t\tbool f = true;\n\t\t\trep(l, n) {\n\t\t\t\tif (l == i || l == j)continue;\n\t\t\t\tif (is_in(c[l], v[k])) {\n\t\t\t\t\tf = false; break;\n\t\t\t\t}\n\t\t\t}\n\t\t\tif (f)return true;\n\t\t}\n\t}\n\treturn false;\n}\nvoid solve() {\n\tcin >> w;\n\trep(i, n) {\n\t\tcin >> x[i] >> y[i] >> h[i];\n\t}\n\tld le = 0, ri = (w*w+2500)/(2*w);\n\trep (aa,100) {\n\t\tld mid = (le + ri) / 2;\n\t\tif (can(mid))le = mid;\n\t\telse ri = mid;\n\t}\n\tcout << le << endl;\n}\n\nsigned main() {\n\tios::sync_with_stdio(false);\n\tcin.tie(0);\n\tcout << fixed << setprecision(5);\n\t//init();\n\t//int t; cin >> t; rep(i, t)solve();\n\twhile (cin >> n, n)solve();\n\t//solve();\n\tstop\n\t\treturn 0;\n}", "accuracy": 1, "time_ms": 550, "memory_kb": 3304, "score_of_the_acc": -0.1326, "final_rank": 3 }, { "submission_id": "aoj_1342_4104827", "code_snippet": "#include<bits/stdc++.h>\n#define rep(i,n) for(int i=0;i< (n);i++)\nusing namespace std;\ntypedef long long ll;\ntypedef pair<double,double> P;\nconst double EPS = 1e-8;\n\n//補助用演算関数を作っとく.\ndouble dist(P x,P y){\n double x1=x.first,x2=x.second,y1=y.first,y2=y.second;\n double tmp=(x1-y1)*(x1-y1)+(x2-y2)*(x2-y2);\n return sqrt(tmp);\n}\ndouble grp(double x,double y){\n //斜辺と一辺が与えられたときにもう一個の辺の長さを返す関数。\n //初めから作っておくべきであった.\n //xを斜辺とする.\n double ans = x*x-y*y;\n if(ans<0){\n cout<<\"error!\"<<endl;\n cout<<x<<' '<<y<<endl;\n //円同士の交点の演算の時にerrorが出ていた.おそらくすっぽり入ってるときか?\n //やはり円の交点のところを直したらerrorが出なかったので元凶は円.\n //円と壁のバグなしたのにまだ駄目だ。。。\n }\n return sqrt(ans);\n}\n\nint main(){\n while(true){\n int n,w;cin>>n>>w;\n if(n==0 && w==0)break;\n vector<pair<P,double>> lst(n);\n rep(i,n){\n double x,y,h;cin>>x>>y>>h;\n lst[i] = make_pair(make_pair(x,y),h);\n }\n //まずは交点を全て求める.\n //これらは全てt_midを与えられて計算する.\n //つまりjudge内で書く必要がある.\n auto judge=[&](double t_mid){\n vector<P> cross_lst;\n double w_now;\n if(w < t_mid)w_now=grp(t_mid,(t_mid-w));\n else w_now=t_mid;\n //円同士の交点を求める\n for(int i=0;i<n-1;i++){\n for(int j=i+1;j<n;j++){\n //円の全ての組み合わせを試す.\n pair<P,double> r1 = lst[i];\n pair<P,double> r2 = lst[j];\n //半径は修正.\n double r1_now;\n if(r1.second < t_mid)r1_now=grp(t_mid,(t_mid-r1.second));\n else r1_now=t_mid;\n double r2_now;\n if(r2.second < t_mid)r2_now=grp(t_mid,(t_mid-r2.second));\n else r2_now=t_mid;\n //交点を持たない場合はここでcontinue↓\n double dis = dist(r1.first,r2.first);\n //判定します.\n if(abs(r1_now-r2_now) > dis || (r1_now+r2_now) < dis)continue;\n double a = (dis*dis+r1_now*r1_now-r2_now*r2_now)/2;\n double b = dis*dis*r1_now*r1_now-a*a;\n double x1 = r2.first.first,x0 = r1.first.first,y1=r2.first.second,y0=r1.first.second;\n P tg1 = make_pair(((a*(x1-x0)+(y1-y0)*sqrt(b))/(dis*dis)+x0),((a*(y1-y0)-(x1-x0)*sqrt(b))/(dis*dis)+y0));\n P tg2 = make_pair(((a*(x1-x0)-(y1-y0)*sqrt(b))/(dis*dis)+x0),((a*(y1-y0)+(x1-x0)*sqrt(b))/(dis*dis)+y0));\n cross_lst.push_back(tg1);\n cross_lst.push_back(tg2);\n //done\n }\n }\n \n //次、壁と円の交点を求める.\n rep(i,n){\n pair<P,double> r=lst[i];\n //下の壁との交点\n double r_now;\n if(r.second < t_mid)r_now=grp(t_mid,(t_mid-r.second));\n else r_now=t_mid;\n double distwithwall = abs(r.first.second-w_now);\n if(distwithwall<=r_now){\n P tg1 = make_pair((r.first.first+grp(r_now,distwithwall)),w_now);\n P tg2 = make_pair((r.first.first-grp(r_now,distwithwall)),w_now);\n cross_lst.push_back(tg1);\n cross_lst.push_back(tg2);\n }\n //上の壁との交点\n distwithwall = abs(r.first.second-(100-w_now));\n if(distwithwall<=r_now){\n P tg1 = make_pair((r.first.first+grp(r_now,distwithwall)),100-w_now);\n P tg2 = make_pair((r.first.first-grp(r_now,distwithwall)),100-w_now);\n cross_lst.push_back(tg1);\n cross_lst.push_back(tg2);\n }\n //左の壁との交点\n distwithwall = abs(r.first.first-w_now);\n if(distwithwall<=r_now){\n P tg1 = make_pair(w_now,(r.first.second+grp(r_now,distwithwall)));\n P tg2 = make_pair(w_now,(r.first.second-grp(r_now,distwithwall)));\n cross_lst.push_back(tg1);\n cross_lst.push_back(tg2);\n }\n //右の壁との交点\n distwithwall = abs(r.first.first-(100-w_now));\n if(distwithwall<=r_now){\n P tg1 = make_pair(100-w_now,(r.first.second+grp(r_now,distwithwall)));\n P tg2 = make_pair(100-w_now,(r.first.second-grp(r_now,distwithwall)));\n cross_lst.push_back(tg1);\n cross_lst.push_back(tg2);\n }\n }\n //最後に、壁同士の交点を入れておく.\n cross_lst.push_back(make_pair(w_now,w_now));\n cross_lst.push_back(make_pair(100-w_now,w_now));\n cross_lst.push_back(make_pair(w_now,100-w_now));\n cross_lst.push_back(make_pair(100-w_now,100-w_now));\n\n //次に、交点が存在できるかチェック!!\n int cnt = 0;//ダメだった交点数.これが全部ならout.\n for(P target : cross_lst){\n //壁の内側に入っているかcheck!\n if(w_now>target.first+EPS || target.first>(100-w_now)+EPS || w_now>target.second+EPS || target.second+EPS>(100-w_now)){\n cnt++;\n continue;\n }\n //円の内部に入らないかチェック!\n rep(i,n){\n pair<P,double> r = lst[i];\n double r_now;\n if(r.second < t_mid)r_now=grp(t_mid,(t_mid-r.second));\n else r_now=t_mid;\n if(dist(r.first,target)+EPS<r_now){\n cnt++;\n break;\n }\n }\n }\n if(cnt == cross_lst.size())return false;\n return true;\n };\n //judgeが描き終わったので二分探索をする.\n double t_min = 0;//ok\n double t_max = (1e+9)+5;//絶対out\n const double D = 1e-6;\n while(t_min+D<t_max){\n double t_mid=(t_min+t_max)/2;\n if(judge(t_mid)){\n t_min = t_mid;\n }else{\n t_max = t_mid;\n }\n }\n cout<<fixed<<setprecision(6)<<t_min<<endl;\n }\n return 0;\n}", "accuracy": 1, "time_ms": 50, "memory_kb": 3244, "score_of_the_acc": 0, "final_rank": 1 } ]
aoj_1345_cpp
Problem A: Bit String Reordering You have to reorder a given bit string as specified. The only operation allowed is swapping adjacent bit pairs. Please write a program that calculates the minimum number of swaps required. The initial bit string is simply represented by a sequence of bits, while the target is specified by a run-length code . The run-length code of a bit string is a sequence of the lengths of maximal consecutive sequences of zeros or ones in the bit string. For example, the run-length code of "011100" is "1 3 2". Note that there are two different bit strings with the same run-length code, one starting with zero and the other starting with one. The target is either of these two. In Sample Input 1, bit string "100101" should be reordered so that its run-length code is "1 3 2", which means either "100011" or "011100". At least four swaps are required to obtain "011100". On the other hand, only one swap is required to make "100011". Thus, in this example, 1 is the answer. Input The input consists of a single test case. The test case is formatted as follows. $N$ $M$ $b_1$ $b_2$ . . . $b_N$ $p_1$ $p_2$ . . . $p_M$ The first line contains two integers $N$ ($1 \leq N \leq 15$) and $M$ ($1 \leq M \leq N$). The second line specifies the initial bit string by $N$ integers. Each integer $b_i$ is either 0 or 1. The third line contains the run-length code, consisting of $M$ integers. Integers $p_1$ through $p_M$ represent the lengths of consecutive sequences of zeros or ones in the bit string, from left to right. Here, $1 \leq p_j$ for $1 \leq j \leq M$ and $\sum^{M}_{j=1} p_j = N$ hold. It is guaranteed that the initial bit string can be reordered into a bit string with its run-length code $p_1, . . . , p_M$. Output Output the minimum number of swaps required Sample Input 1 6 3 1 0 0 1 0 1 1 3 2 Sample Output 1 1 Sample Input 2 7 2 1 1 1 0 0 0 0 4 3 Sample Output 2 12 Sample Input 3 15 14 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 Sample Output 3 7 Sample Input 4 1 1 0 1 Sample Output 4 0
[ { "submission_id": "aoj_1345_10500246", "code_snippet": "#include <iostream>\n#include <cassert>\n#include <vector>\n#include <algorithm>\n#include <ranges>\n#include <map>\nnamespace ranges = std::ranges;\nint N, M;\nstd::vector<int> rle(std::vector<int> a) {\n std::vector<int> res;\n for (int i = 0, j = 0 ; i < N ; i = j) {\n while (j < N and a[i] == a[j]) j++;\n res.push_back(j - i);\n }\n return res;\n}\nint main() {\n std::cin >> N >> M;\n std::vector<int> B(N), P(M);\n for (int i = 0 ; i < N ; i++) std::cin >> B[i];\n for (int i = 0 ; i < M ; i++) std::cin >> P[i];\n std::map<std::vector<int>, int> dist1, dist2;\n std::vector<std::vector<int>> que;\n dist1[B] = 0;\n que.push_back(B);\n for (int t = 0 ; t < std::ssize(que) ; t++) {\n std::vector<int> v = std::move(que[t]);\n const auto d = dist1[v];\n dist2.try_emplace(rle(v), d);\n for (int i = 0 ; i + 1 < N ; i++) {\n std::swap(v[i], v[i + 1]);\n if (!dist1.contains(v)) {\n dist1[v] = d + 1;\n que.push_back(v);\n }\n std::swap(v[i], v[i + 1]);\n }\n }\n std::cout << dist2[P] << '\\n';\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 5632, "score_of_the_acc": -0.2419, "final_rank": 16 }, { "submission_id": "aoj_1345_6785382", "code_snippet": "#pragma region template\n#pragma GCC optimize(\"Ofast\")\n#include <bits/stdc++.h>\nusing namespace std;\n#ifdef __LOCAL\n #include <debug>\n#else\n #define debug(...) void(0)\n#endif\n\n#define REP(i,n) for(int i=0;i<(n);i++)\n#define ALL(v) v.begin(),v.end()\n\ntemplate<typename T>\nistream& operator>>(istream&is,vector<T>&v){\n for(T&p:v)is>>p;\n return is;\n}\ntemplate<typename T>\nostream& operator<<(ostream&os,const vector<T>&v){\n if(&os==&cerr)os<<\"[\";\n for(int i=0;i<v.size();i++){\n os<<v[i];\n if(i+1<v.size())os<<(&os==&cerr?\",\":\" \");\n }\n if(&os==&cerr)os<<\"]\";\n return os;\n}\n#pragma endregion template\n\nmap<vector<int>,int> mp;\n\nint main(){\n int n,m;cin>>n>>m;\n vector<int> b(n);cin>>b;\n mp[b]=0;\n queue<vector<int>> que;\n que.push(b);\n while(que.size()){\n auto ve=que.front();que.pop();\n REP(i,n-1){\n if(ve[i]==ve[i+1])continue;\n auto cpy=ve;\n swap(cpy[i],cpy[i+1]);\n if(mp.count(cpy))continue;\n mp[cpy]=mp[ve]+1;\n que.push(cpy);\n }\n }\n\n //debug(mp);\n\n vector<int> v(m);cin>>v;\n int ans=1e9;\n for(const auto&[key,val]:mp){\n vector<int> w;\n int cnt=1;\n for(int i=1;i<n;i++){\n if(key[i]!=key[i-1]){\n w.push_back(cnt);\n cnt=0;\n }\n cnt++;\n }\n w.push_back(cnt);\n if(v==w)ans=min(ans,val);\n }\n cout<<ans<<endl;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 4444, "score_of_the_acc": -0.1754, "final_rank": 11 }, { "submission_id": "aoj_1345_4429101", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n#define SZ(x) (int)(x.size())\n#define REP(i, n) for(int i=0;i<(n);++i)\n#define FOR(i, a, b) for(int i=(a);i<(b);++i)\n#define RREP(i, n) for(int i=(int)(n)-1;i>=0;--i)\n#define RFOR(i, a, b) for(int i=(int)(b)-1;i>=(a);--i)\n#define ALL(a) a.begin(),a.end()\n#define DUMP(x) cerr<<#x<<\" = \"<<(x)<<endl\n#define DEBUG(x) cerr<<#x<<\" = \"<<(x)<<\" (L\"<<__LINE__<<\")\"<< endl;\nusing ll = long long;\nusing vi = vector<int>;\nusing vvi = vector<vi>;\nusing vll = vector<ll>;\nusing vvll = vector<vll>;\nusing P = pair<int, int>;\nconst double eps = 1e-8;\nconst ll MOD = 1000000007;\nconst int INF = INT_MAX / 2;\nconst ll LINF = LLONG_MAX / 2;\ntemplate <typename T1, typename T2>\nbool chmax(T1 &a, const T2 &b) {\n if (a < b) { a = b; return true; }\n return false;\n}\ntemplate <typename T1, typename T2>\nbool chmin(T1 &a, const T2 &b) {\n if (a > b) { a = b; return true; }\n return false;\n}\ntemplate<typename T>\nostream &operator<<(ostream &os, const vector<T> &v) {\n os << \"[\";\n REP(i, SZ(v)) {\n if (i) os << \", \";\n os << v[i];\n }\n return os << \"]\";\n}\ntemplate<typename T1, typename T2>\nostream &operator<<(ostream &os, const map<T1, T2> &mp) {\n os << \"{\";\n int a = 0;\n for (auto &tp : mp) {\n if (a) os << \", \";\n a = 1;\n os << tp;\n }\n return os << \"}\";\n}\ntemplate<typename T1, typename T2>\nostream &operator<<(ostream &os, const pair<T1, T2> &p) {\n os << p.first << \":\" << p.second;\n return os;\n}\n\n\nint main() {\n cin.tie(0);\n ios::sync_with_stdio(false);\n cout << fixed << setprecision(10);\n\n int N, M; cin >> N >> M;\n string b;\n vi p(M);\n REP(i, N) {\n char c; cin >> c; b += c;\n }\n REP(i, M) cin >> p[i];\n\n string g1, g2;\n REP(k, M) {\n REP(i, p[k]) {\n g1 += k & 1 ? '0' : '1';\n g2 += k & 1 ? '1' : '0';\n }\n }\n\n using Pi = pair<int,string>;\n priority_queue<Pi, vector<Pi>, greater<Pi>> que;\n map<string,int> dist;\n\n int ans = -1;\n\n que.emplace(0, b); dist[b] = 0;\n while (!que.empty()) {\n string s;\n int cost;\n tie(cost, s) = que.top(); que.pop();\n if (s == g1 or s == g2) {\n ans = cost;\n break;\n }\n\n REP(i, N-1) {\n string tmp = s;\n swap(tmp[i], tmp[i+1]);\n if (dist.count(tmp) == 0 or dist[tmp] > cost + 1) {\n dist[tmp] = cost + 1;\n que.emplace(cost + 1, tmp);\n }\n }\n }\n cout << ans << endl;\n\n return 0;\n}", "accuracy": 1, "time_ms": 20, "memory_kb": 3880, "score_of_the_acc": -0.1724, "final_rank": 10 }, { "submission_id": "aoj_1345_3272588", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\n\n//幅優先探索\n\nint main(){\n int n, m; cin >> n >> m;\n vector<bool> b(n);\n for(int i = 0; i < n; i++){\n int in; cin >> in;\n b[i] = (bool)in;\n }\n vector<int> p(m);\n for(int i = 0; i < m; i++) cin >> p[i];\n vector<bool> g1;\n vector<bool> g2;\n for(int i = 0; i < m; i++){\n if(i % 2 == 0){\n for(int j = 0; j < p[i]; j++){\n g1.push_back(false);\n g2.push_back(true);\n }\n }else{\n for(int j = 0; j < p[i]; j++){\n g1.push_back(true);\n g2.push_back(false);\n }\n }\n }\n\n set<vector<bool> > used;\n queue<pair<vector<bool>, int> > q;\n q.push({b, 0});\n used.insert(b);\n \n while(!q.empty()){\n\n vector<bool> vec = q.front().first;\n int cost = q.front().second;\n q.pop();\n \n if(vec == g1 || vec == g2){\n cout << cost << endl;\n break;\n }\n\n for(int i = 0; i < n - 1; i++){\n vector<bool> nxt = vec;\n swap(nxt[i], nxt[i + 1]);\n if(used.find(nxt) == used.end()){\n used.insert(nxt);\n q.push({nxt, cost + 1});\n }\n }\n }\n\n return 0;\n}", "accuracy": 1, "time_ms": 30, "memory_kb": 3860, "score_of_the_acc": -0.1998, "final_rank": 13 }, { "submission_id": "aoj_1345_3097601", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\n#define int long long\ntypedef pair<int, int> P;\nconst int INF = 1e9;\n\nint N, M;\nvector<int> p;\n\nsigned main() {\n cin.tie(0);\n ios_base::sync_with_stdio(0);\n cout << fixed << setprecision(12);\n\n cin >> N >> M;\n\n int bit = 0;\n for ( int i = 0; i < N; i++ ) {\n int b;\n cin >> b;\n bit |= (b<<i); \n }\n\n p = vector<int>(M);\n for ( int i = 0; i < M; i++ ) {\n cin >> p[i]; \n }\n\n vector<bool> used(1<<N, false);\n priority_queue<P, vector<P>, greater<P> > Q;\n Q.push({0, bit});\n\n while ( !Q.empty() ) {\n P e = Q.top(); Q.pop();\n int c = e.first, b = e.second; \n\n vector<int> q;\n int cnt = 1;\n bool pre = b&1; \n for ( int i = 1; i < N; i++ ) {\n if ( pre != (bool)(b&(1<<i)) ) {\n\tq.emplace_back(cnt);\n\tpre = !pre;\n\tcnt = 1; \n } else {\n\tcnt++; \n }\n }\n q.emplace_back(cnt);\n\n if ( q == p ) {\n cout << c << endl;\n return 0;\n }\n\n if ( used[b] ) continue;\n used[b] = true;\n\n for ( int i = 0; i < N-1; i++ ) {\n int nxt = b;\n nxt &= ~(1<<i);\n nxt &= ~(1<<(i+1));\n if ( b & (1<<i) ) nxt |= (1<<(i+1));\n if ( b & (1<<(i+1)) ) nxt |= (1<<i);\n Q.push(P(c+1, nxt)); \n }\n }\n \n return 0;\n}", "accuracy": 1, "time_ms": 20, "memory_kb": 3340, "score_of_the_acc": -0.1421, "final_rank": 7 }, { "submission_id": "aoj_1345_2496816", "code_snippet": "#include <iostream>\n#include <vector>\n#include <algorithm>\n\n#define REP(i,a,b) for(int i=int(a);i<int(b);i++)\n\nusing namespace std;\n\ntypedef long long int lli;\n\nint bitSwap(int num, int p) {\n int t = (num >> p) & 1;\n num -= (t << p);\n int e = (num >> (p + 1)) & 1;\n num -= (e << (p + 1));\n return num + (t << (p + 1)) + (e << p);\n}\n\nint main () {\n int n, m;\n cin >> n >> m;\n int num = 0;\n REP (i, 0, n) {\n int bit;\n cin >> bit;\n num = (num << 1) + bit;\n }\n int s0 = 0, s1 = 0;\n REP (i, 0, m) {\n int p;\n cin >> p;\n REP (j, 0, p) {\n s0 = (s0 << 1) + (i % 2);\n s1 = (s1 << 1) + !(i % 2);\n }\n }\n int size = 1 << n;\n const int inf = 1 << 30;\n vector<int> dp(size, inf);\n dp[s0] = dp[s1] = 0;\n bool run = true;\n while (run) {\n run = false;\n REP (i, 0, size) {\n if (dp[i] == inf) continue;\n REP (j, 0, n - 1) {\n int t = bitSwap(i, j);\n if (dp[t] > dp[i] + 1) {\n dp[t] = dp[i] + 1;\n run = true;\n }\n }\n }\n }\n cout << dp[num] << endl;\n return 0;\n}", "accuracy": 1, "time_ms": 40, "memory_kb": 3220, "score_of_the_acc": -0.1925, "final_rank": 12 }, { "submission_id": "aoj_1345_2026288", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n#define rep(i, n) for (int i = 0; i < n; i++)\n\nsigned main() {\n\tint n, m; cin >> n >> m;\n\tvector<bool> bs(n);\n\trep(i, n) {\n\t\tint x; cin >> x;\n\t\tbs[i] = x;\n\t}\n\tvector<bool> a(n), b(n);\n\tint tb = 0;\n\tint idx = 0;\n\trep(i, m) {\n\t\tint p; cin >> p;\n\t\trep(j, p) {\n\t\t\ta[idx] = tb;\n\t\t\tb[idx] = !tb;\n\t\t\tidx++;\n\t\t}\n\t\ttb = !tb;\n\t}\n\tint mb = 1 << 15;\n\t\n\tusing P = pair<vector<bool>, int>;\n\tunordered_map<vector<bool>, int> visited;\n\tqueue<P> que;\n\tque.push(P(bs, 0));\n\tvisited[bs] = 0;\n\tint ans = -1;\n\t\n\twhile (!que.empty()) {\n\t\tP p = que.front(); que.pop();\n\t\tauto& pb = p.first;\n\t\tauto& pc = p.second;\n\t\tif (pb == a || pb == b) {\n\t\t\tans = pc;\n\t\t\tbreak;\n\t\t}\n\t\trep(i, n - 1) {\n\t\t\tvector<bool> next = pb;\n\t\t\tswap(next[i], next[i + 1]);\n\t\t\tint nc = pc + 1;\n\t\t\tif (visited.count(next)) continue;\n\t\t\tvisited[next] = nc;\n\t\t\tque.emplace(next, nc);\n\t\t}\n\t}\n\tcout << ans << endl;\n\t\n\t\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3768, "score_of_the_acc": -0.1375, "final_rank": 6 }, { "submission_id": "aoj_1345_1856191", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n#define int long long // <-----!!!!!!!!!!!!!!!!!!!\n\n#define rep(i,n) for (int i=0;i<(n);i++)\n#define rep2(i,a,b) for (int i=(a);i<(b);i++)\n#define rrep(i,n) for (int i=(n)-1;i>=0;i--)\n#define rrep2(i,a,b) for (int i=(b)-1;i>=(a);i--)\n#define all(a) (a).begin(),(a).end()\n\ntypedef long long ll;\ntypedef pair<int, int> P;\ntypedef tuple<int, int, int> TUPLE;\ntypedef vector<int> V;\ntypedef vector<V> VV;\ntypedef vector<VV> VVV;\n\nint bfs(V &start, V &run) {\n int N = start.size();\n int M = run.size();\n\n map<V, int> d;\n\tqueue<V> que;\n que.push(start);\n d[start] = 0;\n\n V g1;\n int bit = 0;\n for (auto x : run) {\n rep(k, x) g1.emplace_back(bit);\n bit ^= 1;\n }\n\n V g2 = g1;\n rep(i, N) g2[i] ^= 1;\n\n while (!que.empty()) {\n auto now = que.front(); que.pop();\n if (now == g1 || now == g2) {\n return d[now];\n }\n\n rep(i, N - 1) {\n V nxt(now);\n swap(nxt[i], nxt[i + 1]);\n if (d.count(nxt) == 0) {\n que.push(nxt);\n d[nxt] = d[now] + 1;\n }\n }\n\n }\n\n}\n\nsigned main() {\n std::ios::sync_with_stdio(false);\n std::cin.tie(0);\n\n int N, M;\n cin >> N >> M;\n V start(N);\n rep(i, N) cin >> start[i];\n V run(M);\n rep(i, M) cin >> run[i];\n\n cout << bfs(start, run) << endl;\n\n}", "accuracy": 1, "time_ms": 20, "memory_kb": 4552, "score_of_the_acc": -0.21, "final_rank": 15 }, { "submission_id": "aoj_1345_1839174", "code_snippet": "#include <iostream>\n#include <vector>\n#include <string>\n#include <cstring>\n#include <map>\n#include <queue>\n#include <set>\n#include <algorithm>\n\nusing namespace std;\ntypedef pair<string, int> P;\n\nvoid solve()\n{\n\tcin.tie(0);\n\tios::sync_with_stdio(false);\n\n\tbool used[10000];\n\tmemset(used, 0, sizeof(used));\n\tint N, M;\n\tcin >> N >> M;\n\tstring goal;\n\tfor (int i = 0; i < N; ++i)\n\t{\n\t\tchar c;\n\t\tcin >> c;\n\t\tgoal += c;\n\t}\n\tstring p;\n\tstring start[2];\n\tbool flag = false;\n\tfor (int i = 0; i < M; ++i)\n\t{\n\t\tint c;\n\t\tcin >> c;\n\t\tfor (int j = 0; j < c; ++j)\n\t\t{\n\t\t\tif (flag)\n\t\t\t{\n\t\t\t\tstart[0] += '0';\n\t\t\t\tstart[1] += '1';\n\t\t\t}\n\t\t\telse\n\t\t\t{\n\t\t\t\tstart[0] += '1';\n\t\t\t\tstart[1] += '0';\n\t\t\t}\n\t\t}\n\t\tflag = !flag;\n\t}\n\tif (start[0] == goal || start[1] == goal)\n\t{\n\t\tcout << 0 << endl;\n\t\treturn;\n\t}\n\n\tqueue<P> que;\n\tque.push(P(start[0], 0));\n\tque.push(P(start[1], 0));\n\tset<string> found;\n\tfound.insert(start[0]);\n\tfound.insert(start[1]);\n\twhile (!que.empty())\n\t{\n\t\tP digit = que.front();\n\t\tque.pop();\n\t\tfor (int i = 1; i < digit.first.size(); ++i)\n\t\t{\n\t\t\tswap(digit.first[i - 1], digit.first[i]);\n\t\t\tif (found.find(digit.first) != found.end())\n\t\t\t{\n\t\t\t\tswap(digit.first[i - 1], digit.first[i]);\n\t\t\t\tcontinue;\n\t\t\t}\n\t\t\tif (digit.first == goal)\n\t\t\t{\n\t\t\t\tcout << digit.second + 1 << endl;\n\t\t\t\treturn;\n\t\t\t}\n\t\t\tque.push(P(digit.first, digit.second + 1));\n\t\t\tfound.insert(digit.first);\n\t\t\tswap(digit.first[i - 1], digit.first[i]);\n\t\t}\n\t}\n}\n\nint main()\n{\n\tsolve();\n\treturn(0);\n}", "accuracy": 1, "time_ms": 30, "memory_kb": 2464, "score_of_the_acc": -0.1216, "final_rank": 5 }, { "submission_id": "aoj_1345_1839170", "code_snippet": "#include <iostream>\n#include <vector>\n#include <string>\n#include <cstring>\n#include <map>\n#include <queue>\n#include <set>\n#include <algorithm>\n\nusing namespace std;\ntypedef pair<string, int> P;\n\nvoid solve()\n{\n\tcin.tie(0);\n\tios::sync_with_stdio(false);\n\n\tbool used[10000];\n\tmemset(used, 0, sizeof(used));\n\tint N, M;\n\tcin >> N >> M;\n\tstring goal;\n\tfor (int i = 0; i < N; ++i)\n\t{\n\t\tchar c;\n\t\tcin >> c;\n\t\tgoal += c;\n\t}\n\tstring p;\n\tstring start[2];\n\tbool flag = false;\n\tfor (int i = 0; i < M; ++i)\n\t{\n\t\tint c;\n\t\tcin >> c;\n\t\tfor (int j = 0; j < c; ++j)\n\t\t{\n\t\t\tif (flag)\n\t\t\t{\n\t\t\t\tstart[0] += '0';\n\t\t\t\tstart[1] += '1';\n\t\t\t}\n\t\t\telse\n\t\t\t{\n\t\t\t\tstart[0] += '1';\n\t\t\t\tstart[1] += '0';\n\t\t\t}\n\t\t}\n\t\tflag = !flag;\n\t}\n\n\tqueue<P> que;\n\tque.push(P(start[0], 0));\n\tque.push(P(start[1], 0));\n\tset<string> found;\n\tfound.insert(start[0]);\n\tfound.insert(start[1]);\n\twhile (!que.empty())\n\t{\n\t\tP digit = que.front();\n\t\tque.pop();\n\t\tfor (int i = 1; i < digit.first.size(); ++i)\n\t\t{\n\t\t\tswap(digit.first[i - 1], digit.first[i]);\n\t\t\tif (found.find(digit.first) != found.end())\n\t\t\t{\n\t\t\t\tswap(digit.first[i - 1], digit.first[i]);\n\t\t\t\tcontinue;\n\t\t\t}\n\t\t\tif (digit.first == goal)\n\t\t\t{\n\t\t\t\tcout << digit.second + 1 << endl;\n\t\t\t\treturn;\n\t\t\t}\n\t\t\tque.push(P(digit.first, digit.second + 1));\n\t\t\tfound.insert(digit.first);\n\t\t\tswap(digit.first[i - 1], digit.first[i]);\n\t\t}\n\t}\n}\n\nint main()\n{\n\tsolve();\n\treturn(0);\n}", "accuracy": 0.09, "time_ms": 20, "memory_kb": 2180, "score_of_the_acc": -0.0772, "final_rank": 19 }, { "submission_id": "aoj_1345_1837330", "code_snippet": "#include<iostream>\n#include<math.h>\n#include<queue>\n#include<map>\nusing namespace std;\ntypedef pair<int,int> i_i;\n#define mp(a,b) make_pair(a,b)\nint main(){\n int n,m;\n cin >> n >> m;\n int step[1<<15];\n int inf = 1<<10;\n fill(step,step+(1<<15),inf);\n int i,j,k,c=0,b=0;\n int in=0;\n int o1=0,o2=0;\n for(i=0;i<n;i++) {\n cin >> k;\n b+=k;\n in=in<<1;\n in+=k;\n }\n /*\n for(i=n-1;i>=0;i--) cout << ((in>>i&1) ? 1:0);\n cout << endl;\n */\n //step[in]=0;\n for(i=0;i<m;i++) {\n cin >> k;\n o1=o1<<k;\n o2=o2<<k;\n if(i%2==0) {\n b+=k;\n o1+=pow(2,k)-1;\n }else{\n o2+=pow(2,k)-1;\n }\n }\n /*\n for(i=0;i<n;i++) cout << ((o1>>i&1) ? 1:0);\n cout << endl;\n for(i=0;i<n;i++) cout << ((o2>>i&1) ? 1:0);\n cout << endl;\n */\n\n queue<i_i> q;\n i_i x;\n x.first=in;x.second=0;\n q.push(x);\n while(step[o1]==inf&&step[o2]==inf&&!q.empty()){\n x=q.front();\n q.pop();\n /*\n for(i=n-1;i>=0;i--) cout << ((x.first>>i&1) ? 1:0);\n cout << endl;\n */\n if(step[x.first]>x.second){\n step[x.first]=x.second;\n for(i=n-1;i>0;i--){\n\tif((x.first>>i&1)==(x.first>>(i-1)&1)) continue;\n\tif((x.first>>i&1))\n\t q.push(mp(x.first-pow(2,i)+pow(2,i-1),x.second+1));\n\telse\n\t q.push(mp(x.first+pow(2,i)-pow(2,i-1),x.second+1));\n }\n }\n }\n \n \n cout << min(step[o1],step[o2]) << endl;\n return 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 1440, "score_of_the_acc": -0.0072, "final_rank": 1 }, { "submission_id": "aoj_1345_1721834", "code_snippet": "#include <iostream>\n#include <algorithm>\n#include <cmath>\n#include <vector>\n#include <complex>\n#include <queue>\n#include <deque>\n#include <set>\n#include <map>\n#include <unordered_set>\n#include <unordered_map>\n#include <iomanip>\n#include <assert.h>\n#include <array>\n#include <cstdio>\n#include <cstring>\n#include <random>\n#include <functional>\n#include <numeric>\n#include <bitset>\n\nusing namespace std;\n\nstruct before_main{before_main(){cin.tie(0); ios::sync_with_stdio(false);}} before_main;\n\n#define REP(i,a,b) for(int i=a;i<(int)b;i++)\n#define rep(i,n) REP(i,0,n)\n#define all(c) (c).begin(), (c).end()\n#define zero(a) memset(a, 0, sizeof a)\n#define minus(a) memset(a, -1, sizeof a)\ntemplate<class T1, class T2> inline bool minimize(T1 &a, T2 b) { return b < a && (a = b, 1); }\ntemplate<class T1, class T2> inline bool maximize(T1 &a, T2 b) { return a < b && (a = b, 1); }\n\ntypedef long long ll;\nint const inf = 1<<29;\n\nint main() {\n\n int N, M; cin >> N >> M;\n vector<int> b(N);\n rep(i, N) cin >> b[i];\n vector<int> tb1, tb2;\n int flag = 0;\n rep(i, M) {\n int p; cin >> p;\n rep(j, p) {\n tb1.push_back(flag);\n tb2.push_back(flag^1);\n }\n flag ^= 1;\n }\n set<vector<int>> st = {b};\n queue<pair<vector<int>, int>> q;\n q.emplace(b, 0);\n\n auto check_run_length = [&](vector<int>& v) {\n return v == tb1 || v == tb2;\n };\n\n while(!q.empty()) {\n vector<int> p; int cost; tie(p, cost) = q.front(); q.pop();\n if(check_run_length(p)) { cout << cost << endl; return 0; }\n rep(i, N-1) {\n swap(p[i], p[i+1]);\n if(!st.count(p)) {\n q.emplace(p, cost + 1);\n st.insert(p);\n }\n swap(p[i], p[i+1]);\n }\n }\n \n return 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 4064, "score_of_the_acc": -0.1541, "final_rank": 9 }, { "submission_id": "aoj_1345_1630433", "code_snippet": "#include<iostream>\n#include<cstring>\n#include<algorithm>\nusing namespace std;\n\nint x[65536];\n\nint inversions(int p, int n, int q, int r) {\n\tint x3[15], y[15], z;\n\tmemset(x, 127, sizeof(x));\n\tx[p] = 0;\n\tfor (int i = 0; i < n * n; i++){\n\t\tfor (int j = 0; j < (1 << n); j++) {\n\t\t\tif (x[j] < 1000) {\n\t\t\t\tfor (int k = 0; k < n; k++) {\n\t\t\t\t\tx3[k] = (j / (1 << k)) % 2;\n\t\t\t\t}\n\t\t\t\tfor (int k = 0; k < n - 1; k++) {\n\t\t\t\t\tz = 0;\n\t\t\t\t\tif (x3[k] + x3[k + 1] == 1) {\n\t\t\t\t\t\tfor (int l = 0; l < n; l++) {\n\t\t\t\t\t\t\ty[l] = x3[l];\n\t\t\t\t\t\t}\n\t\t\t\t\t\ty[k] = x3[k + 1];\n\t\t\t\t\t\ty[k + 1] = x3[k];\n\t\t\t\t\t\tfor (int l = 0; l < n; l++) {\n\t\t\t\t\t\t\tz += y[l] * (1 << l);\n\t\t\t\t\t\t}\n\t\t\t\t\t\tx[z] = min(x[z], x[j] + 1);\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n\treturn min(x[q], x[r]);\n}\n\nint n, m, a, s;\nint x1[15], x2[15], cnt, r1, r2;\n\nint main() {\n\tcin >> n >> m;\n\tfor (int i = 0; i < n; i++) {\n\t\tcin >> a; s += (1 << i) * a;\n\t}\n\tfor (int i = 0; i < m; i++) {\n\t\tcin >> a;\n\t\tfor (int j = 0; j < a; j++) {\n\t\t\tx1[j + cnt] = i % 2;\n\t\t\tx2[j + cnt] = (i + 1) % 2;\n\t\t}\n\t\tcnt += a;\n\t}\n\tfor (int i = 0; i < n; i++) {\n\t\tr1 += x1[i] * (1 << i);\n\t\tr2 += x2[i] * (1 << i);\n\t}\n\tcout << inversions(s, n, r1, r2) << endl;\n\treturn 0;\n}", "accuracy": 1, "time_ms": 360, "memory_kb": 1428, "score_of_the_acc": -1.0065, "final_rank": 18 }, { "submission_id": "aoj_1345_1630409", "code_snippet": "#include <map>\n#include <queue>\n#include <string>\n#include <vector>\n#include <utility>\n#include <iostream>\n#include <algorithm>\n\n#define INF (1 << 29)\n\nusing namespace std;\n\nint BFS(string S, string T) // S --> T\n{\n\tmap<string, int> D; D[S] = 1;\n\n\tqueue<pair<string, int> > que; que.push(make_pair(S, 1));\n\n\tif (S == T) { return 0; }\n\n\twhile (!que.empty())\n\t{\n\t\tstring S2 = que.front().first;\n\n\t\tint dist = que.front().second; que.pop();\n\n\t\tfor (int i = 0; i < T.size() - 1; i++)\n\t\t{\n\t\t\tstring S3 = S2;\n\n\t\t\tswap(S3[i], S3[i + 1]);\n\n\t\t\tif (S3 == T) { return dist; }\n\n\t\t\tif (D[S3] == 0)\n\t\t\t{\n\t\t\t\tD[S3] = dist + 1;\n\n\t\t\t\tque.push(make_pair(S3, dist + 1));\n\t\t\t}\n\t\t}\n\t}\n\n\treturn INF;\n}\n\nint main()\n{\n\tint N, M;\n\n\tcin >> N >> M;\n\n\tvector<int> B(N); for (int i = 0; i < N; i++) { cin >> B[i]; }\n\tvector<int> R(M); for (int i = 0; i < M; i++) { cin >> R[i]; }\n\n\tstring S, T1, T2;\n\n\tfor (int i = 0; i < N; i++) { S += B[i] + 48; }\n\n\tfor (int i = 0; i < M; i++)\n\t{\n\t\tfor (int j = 0; j < R[i]; j++) \n\t\t{\n\t\t\tT1 += i % 2 + 48;\n\t\t\tT2 += (i + 1) % 2 + 48;\n\t\t}\n\t}\n\n\tint res1 = BFS(S, T1);\n\tint res2 = BFS(S, T2);\n\n\tcout << min(res1, res2) << endl;\n\n\treturn 0;\n}", "accuracy": 1, "time_ms": 30, "memory_kb": 3876, "score_of_the_acc": -0.2007, "final_rank": 14 }, { "submission_id": "aoj_1345_1595066", "code_snippet": "#include<stdio.h>\n#include <iostream>\n#include <math.h>\n#include <numeric>\n#include <vector>\n#include <map>\n#include <functional>\n#include <stdio.h>\n#include <array>\n#include <algorithm>\n#include <string>\n#include <assert.h>\n#include <stdio.h>\n#include <queue>\n#include<iomanip>\n#include<bitset>\n#include<stack>\nusing namespace std;\n\n//7:10\n//\n//#define REP(i,n) for(int i=0;i<(int)n;++i)\n//#define FOR(i,c) for(__typeof((c).begin())i=(c).begin();i!=(c).end();++i)\n//#define ALL(c) (c).begin(), (c).end()\n//\n//typedef int Weight;\n//struct Edge {\n//\tint src, dst;\n//\tWeight weight;\n//\tEdge(int src, int dst, Weight weight) :\n//\t\tsrc(src), dst(dst), weight(weight) { }\n//};\n//bool operator < (const Edge &e, const Edge &f) {\n//\treturn e.weight != f.weight ? e.weight > f.weight : // !!INVERSE!!\n//\te.src != f.src ? e.src < f.src : e.dst < f.dst;\n//}\n//typedef vector<Edge> Edges;\n//typedef vector<Edges> Graph;\n//\n//typedef vector<Weight> Array;\n//typedef vector<Array> Matrix;\n//\n//#define RESIDUE(s,t) (capacity[s][t]-flow[s][t])\n//Weight maximumFlow(const Graph &g, int s, int t) {\n//\tint n = g.size();\n//\tMatrix flow(n, Array(n)), capacity(n, Array(n));\n//\tREP(u, n) for (auto e = g[u].begin(); e != g[u].end();++e)capacity[e->src][e->dst] += e->weight;\n//\n//\tWeight total = 0;\n//\twhile (1) {\n//\t\tqueue<int> Q; Q.push(s);\n//\t\tvector<int> prev(n, -1); prev[s] = s;\n//\t\twhile (!Q.empty() && prev[t] < 0) {\n//\t\t\tint u = Q.front(); Q.pop();\n//\t\t\tfor (auto e = g[u].begin(); e != g[u].end(); ++e) if (prev[e->dst] < 0 && RESIDUE(u, e->dst) > 0) {\n//\t\t\t\tprev[e->dst] = u;\n//\t\t\t\tQ.push(e->dst);\n//\t\t\t}\n//\t\t}\n//\t\tif (prev[t] < 0) return total; // prev[x] == -1 <=> t-side\n//\t\tWeight inc = 999999999;\n//\t\tfor (int j = t; prev[j] != j; j = prev[j])\n//\t\t\tinc = min(inc, RESIDUE(prev[j], j));\n//\t\tfor (int j = t; prev[j] != j; j = prev[j])\n//\t\t\tflow[prev[j]][j] += inc, flow[j][prev[j]] -= inc;\n//\t\ttotal += inc;\n//\t}\n//\treturn total;\n//}\n#define REP(i,n) for(int i=0;i<(int)(n);i++)\n#define ALL(x) (x).begin(),(x).end()\n\n\nconst int mod = 1000000007;\n\nstruct Mod {\n\tlong long int num;\n\tMod() : num(0) { ; }\n\tMod(long long int n) : num((n % mod + mod) % mod) { ; }\n\toperator long long int() { return num; }\n};\n\nMod operator+(Mod a, Mod b) { return Mod((a.num + b.num) % mod); }\nMod operator-(Mod a, Mod b) { return Mod((mod + a.num - b.num) % mod); }\nMod operator*(Mod a, Mod b) { return Mod(((long long)a.num * b.num) % mod); }\nMod operator+=(Mod &a, Mod b) { return a = a + b; }\nMod operator-=(Mod &a, Mod b) { return a = a - b; }\nMod operator*=(Mod &a, Mod b) { return a = a * b; }\nMod operator^(Mod a, int n) {\n\tif (n == 0) return Mod(1);\n\tMod res = (a * a) ^ (n / 2);\n\tif (n % 2) res = res * a;\n\treturn res;\n}\nMod inv(Mod a) { return a ^ (mod - 2); }\nMod operator/(Mod a, Mod b) { return a * inv(b); }\n\n#define MAX_N 1024000\n\nMod fact[MAX_N], factinv[MAX_N];\nvoid init() {\n\tfact[0] = Mod(1); factinv[0] = 1;\n\tREP(i, MAX_N - 1) {\n\t\tfact[i + 1] = fact[i] * Mod(i + 1);\n\t\tfactinv[i + 1] = factinv[i] / Mod(i + 1);\n\t}\n}\nMod comb(int a, int b) {\n\treturn fact[a] * factinv[b] * factinv[a - b];\n}\n\nlong long int memo[20];//nけた\n\nlong long int powint(long long int a, int b){\n\tif (b == 0)return 1;\n\tif (b == 1)return a;\n\telse{\n\t\tlong long int ans = 1;\n\t\tlong long int c = powint(a, b / 2);\n\t\tans *= c*c;\n\t\tans *= (b % 2) ? a : 1;\n\t\treturn ans;\n\t}\n\n}\n\n\nint backnum[100000][20];\n\nstruct Node{\n\tNode(int aid):children(),ancs(){\n\t\tid=(aid);\n\t\tfor (int i = 0; i < 20; ++i){\n\t\t\tancs[i] = -1;\n\t\t}\n\t}\n\tint id;\n\tvector<int>children;\n\tarray<int, 20>ancs;\n};\n\nint main()\n{\n\tint N,M;\n\tcin >> N >> M;\n\tbitset<15>Moto;\n\tfor (int i = 0; i < N; ++i){\n\t\tint a;\n\t\tcin >> a;\n\t\tMoto[i]=a;\n\t}\n\tvector<int>k;\n\t\n\tfor (int i = 0; i < M; ++i){\n\t\tint a;\n\t\tcin >> a;\n\t\tk.emplace_back(a);\n\t}\n\tint ans = 9999;\n\t{\n\t\tint a = 1;\n\t\tbitset<15> Next1;\n\t\tint count = 0;\n\t\tfor (int i = 0; i < k.size(); ++i){\n\t\t\tfor (int j = 0; j < k[i]; ++j){\n\t\t\t\tNext1[count++] = a;\n\t\t\t\t\n\t\t\t}\n\t\t\tif (a)a = 0;\n\t\t\telse a = 1;\n\t\t}\n\t\tif (Moto.count() != Next1.count()){\n\t\t\t\n\t\t}\n\t\telse{\n\t\t\tvector<int>oneplace;\n\t\t\tvector<int>oneplace2;\n\t\t\tfor (int i = 0; i < N; ++i){\n\t\t\t\tif (Moto[i])oneplace.push_back(i);\n\t\t\t\tif (Next1[i])oneplace2.push_back(i);\n\t\t\t}\n\t\t\tif (!oneplace.size()){\n\t\t\t\tans = 0;\n\t\t\t}\n\t\t\telse{\n\t\t\t\tint kotae = 0;\n\t\t\t\tint mit = oneplace[0];\n\t\t\t\tint nit = oneplace2[0];\n\t\t\t\tkotae += abs(mit - nit);\n\t\t\t\tfor (int i = 1; i < oneplace.size(); ++i){\n\t\t\t\t\tkotae += abs(oneplace[i] - oneplace2[i]);\n\t\t\t\t}\n\t\t\t\tans = min(ans, kotae);\n\t\t\t}\n\t\t}\n\t}\n\t{\n\t\tint a =0;\n\t\tbitset<15> Next1;\n\t\tint count = 0;\n\t\tfor (int i = 0; i < k.size(); ++i){\n\t\t\tfor (int j = 0; j < k[i]; ++j){\n\t\t\t\tNext1[count++] = a;\n\n\t\t\t}\n\t\t\tif (a)a = 0;\n\t\t\telse a = 1;\n\t\t}\n\t\tif (Moto.count() != Next1.count()){\n\n\t\t}\n\t\telse{\n\t\t\tvector<int>oneplace;\n\t\t\tvector<int>oneplace2;\n\t\t\tfor (int i = 0; i < N; ++i){\n\t\t\t\tif (Moto[i])oneplace.push_back(i);\n\t\t\t\tif (Next1[i])oneplace2.push_back(i);\n\t\t\t}\n\t\t\tif (!oneplace.size()){\n\t\t\t\tans = 0;\n\t\t\t}\n\t\t\telse{\n\t\t\t\tint kotae = 0;\n\t\t\t\tint mit = oneplace[0];\n\t\t\t\tint nit = oneplace2[0];\n\t\t\t\tkotae += abs(mit - nit);\n\t\t\t\tfor (int i = 1; i < oneplace.size(); ++i){\n\t\t\t\t\tkotae += abs(oneplace[i] - oneplace2[i]);\n\t\t\t\t}\n\t\t\t\tans = min(ans, kotae);\n\t\t\t}\n\t\t}\n\t}\n\tcout << ans << endl;\n\treturn 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 19172, "score_of_the_acc": -1, "final_rank": 17 }, { "submission_id": "aoj_1345_1593230", "code_snippet": "#include <bits/stdc++.h>\n\nusing namespace std;\n\n#define LOG(...) fprintf(stderr,__VA_ARGS__)\n//#define LOG(...)\n#define FOR(i,a,b) for(int i=(int)(a);i<(int)(b);++i)\n#define REP(i,n) for(int i=0;i<(int)(n);++i)\n#define ALL(a) (a).begin(),(a).end()\n#define RALL(a) (a).rbegin(),(a).rend()\n#define EXIST(s,e) ((s).find(e)!=(s).end())\n#define SORT(c) sort(ALL(c))\n#define RSORT(c) sort(RALL(c))\n\ntypedef long long ll;\ntypedef unsigned long long ull;\ntypedef vector<bool> vb;\ntypedef vector<int> vi;\ntypedef vector<ll> vll;\ntypedef vector<vb> vvb;\ntypedef vector<vi> vvi;\ntypedef vector<vll> vvll;\ntypedef pair<int,int> pii;\ntypedef pair<ll,ll> pll;\n\n#define INF 10000\n\nint main() {\n int n, m;\n cin >> n >> m;\n int start = 0;\n REP(i, n) {\n char c;\n cin >> c;\n start <<= 1;\n start |= (c == '1' ? 1 : 0);\n }\n int goal = 0;\n int t = 0;\n REP(i, m) {\n int bits;\n cin >> bits;\n REP(j, bits) {\n goal <<= 1;\n goal |= t & 1;\n }\n t = ~t;\n }\n int goal2 = ~goal & ((1 << n)-1);\n// cout << bitset<15>(start) << endl;\n// cout << bitset<15>(goal) << endl;\n// cout << bitset<15>(~goal & ((1 << n)-1)) << endl;\n\n vvi dp(2, vi(1 << n, INF));\n dp[0][start] = 0;\n while (1) {\n dp[1] = dp[0];\n REP(step, 1 << n) {\n if (dp[0][step] == INF) continue;\n REP(i, n-1) {\n int has_next = (step >> i) & 3;\n if (has_next != 0 && has_next != 3) {\n int next = step;\n if (next & (1 << i)) {\n next &= ~(1 << i);\n } else {\n next |= (1 << i);\n }\n if (next & (1 << (i+1))) {\n next &= ~(1 << (i+1));\n } else {\n next |= (1 << (i+1));\n }\n// cout << bitset<15>(next) << endl;\n dp[1][next] = min(dp[1][next], dp[0][step]+1);\n }\n }\n }\n dp[0] = dp[1];\n\n if (min(dp[0][goal], dp[0][goal2]) != INF) {\n cout << min(dp[0][goal], dp[0][goal2]) << endl;\n break;\n }\n }\n}", "accuracy": 1, "time_ms": 20, "memory_kb": 3352, "score_of_the_acc": -0.1428, "final_rank": 8 }, { "submission_id": "aoj_1345_1593187", "code_snippet": "#include <algorithm>\n#include <cmath>\n#include <climits>\n#include <cstdio>\n#include <cstdlib>\n#include <cstring>\n#include <fstream>\n#include <iostream>\n#include <list>\n#include <map>\n#include <queue>\n#include <set>\n#include <sstream>\n#include <stack>\n#include <string>\n#include <vector>\n#include <cassert>\n#include <functional>\n\nusing namespace std;\n\n#define LOG(...) printf(__VA_ARGS__)\n//#define LOG(...)\n#define FOR(i,a,b) for(int i=(int)(a);i<(int)(b);++i)\n#define REP(i,n) for(int i=0;i<(int)(n);++i)\n#define ALL(a) (a).begin(),(a).end()\n#define RALL(a) (a).rbegin(),(a).rend()\n#define EXIST(s,e) ((s).find(e)!=(s).end())\n#define SORT(c) sort((c).begin(),(c).end())\n#define RSORT(c) sort((c).rbegin(),(c).rend())\n#define CLR(a) memset((a), 0 ,sizeof(a))\n\ntypedef long long ll;\ntypedef unsigned long long ull;\ntypedef vector<bool> vb;\ntypedef vector<int> vi;\ntypedef vector<ll> vll;\ntypedef vector<vb> vvb;\ntypedef vector<vi> vvi;\ntypedef vector<vll> vvll;\ntypedef pair<int, int> pii;\ntypedef pair<ll, ll> pll;\n\nconst int dx[] = { -1, 0, 1, 0 }; const int dy[] = { 0, 1, 0, -1 };\nstruct st{\nint bit;\nint count;\n};\nint main() {\nint n,m;\n\tcin>>n>>m;\n\tint bit=0;\n\tREP(i,n){\n\tint shift;\n\tcin >> shift;\n\tbit |= shift << (n - i-1);\n\t}\n\tvector<int> ans(m);\n\tREP(i,m){\n\t\tcin>>ans[i];\n\t}\n\tint bit_ans=0;\n\tint bit_ans2;\n\tREP(i,m){\n\t\tREP(j, ans[i]){\n\t\t\tbit_ans <<=1;\n\t\t\tif (i % 2 == 0){\n\t\t\tbit_ans|=1;\n\t\t\t}\n\t\t\t\n\t}\n\t}\n\tbit_ans2=bit_ans^((1<<n)-1);\n\tint count=0;\n\tset<int> dp;\n\tdp.insert(bit);\n\tqueue<st> P;\n\tP.push({bit,0});\n\twhile (!P.empty()){\n\t\tst p = P.front(); P.pop();\n\t\tif (p.bit == bit_ans || p.bit == bit_ans2){\n\t\tcount=p.count;\n\t\tbreak;\n\t\t}\n\t\tREP(i,n-1){\n\t\t\tif ((p.bit | 1 << i) != (p.bit | 1 << (i + 1))){\n\t\t\t\tint bitc = p.bit^3<<i;\n\t\t\t\tif (dp.find(bitc) == dp.end()){\n\t\t\t\t\tdp.insert(bitc);\n\t\t\t\t\tP.push({ bitc, p.count + 1 });\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n\tcout<<count<<endl;\n}", "accuracy": 0.04, "time_ms": 10, "memory_kb": 3808, "score_of_the_acc": -0.1398, "final_rank": 20 }, { "submission_id": "aoj_1345_1515205", "code_snippet": "#include <iostream>\n#include <string>\n#include <queue>\n#include <utility>\n#include <algorithm>\n#include <set>\nusing namespace std;\n\nconst int INF = (1 << 29);\ntypedef pair<string ,int> psi;\n\nint main(void) {\n\n int N, M; cin >> N >> M;\n string b = \"\";\n for (int i = 0; i < N; i++) {\n int t; cin >> t;\n b += t+'0';\n }\n string t1 = \"\", t2 = \"\";\n for (int i = 0; i < M; i++) {\n int p; cin >> p;\n for (int j = 0; j < p; j++) {\n if (i%2) {\n t1 += '0';\n t2 += '1';\n } else {\n t1 += '1';\n t2 += '0';\n }\n }\n }\n\n int ans1 = INF;\n if (count(t1.begin(), t1.end(), '0') == count(b.begin(), b.end(), '0')) {\n queue<psi> q1;\n set<string> used;\n q1.push(psi(t1,0));\n used.insert(t1);\n while (!q1.empty()) {\n psi s = q1.front(); q1.pop();\n if (s.first == b) {\n ans1 = s.second;\n break;\n }\n\n for (int i = 0; i < N-1; i++) {\n string tmp = s.first;\n swap(tmp[i], tmp[i+1]);\n if (used.find(tmp) == used.end()) {\n q1.push(psi(tmp, s.second+1));\n used.insert(tmp);\n }\n }\n }\n }\n\n int ans2 = INF;\n if (count(t2.begin(), t2.end(), '0') == count(b.begin(), b.end(), '0')) {\n queue<psi> q2;\n set<string> used;\n q2.push(psi(t2, 0));\n used.insert(t2);\n while (!q2.empty()) {\n psi s = q2.front(); q2.pop();\n if (s.first == b) {\n ans2 = s.second;\n break;\n }\n\n for (int i = 0; i < N-1; i++) {\n string tmp = s.first;\n swap(tmp[i], tmp[i+1]);\n if (used.find(tmp) == used.end()) {\n q2.push(psi(tmp, s.second+1));\n used.insert(tmp);\n }\n }\n }\n }\n\n cout << min(ans1, ans2) << endl;\n\n return 0;\n}", "accuracy": 1, "time_ms": 20, "memory_kb": 1844, "score_of_the_acc": -0.0584, "final_rank": 4 }, { "submission_id": "aoj_1345_1464836", "code_snippet": "#include <iostream>\n#include <vector>\n#include <string>\n#include <queue>\n#include <bitset>\n\nusing namespace std;\n\nstruct state{\n\tvector< int > bits;\n\tint depth;\n};\n\nvector< int > count_bits( vector< int > *b ) {\n\tvector< int > v;\n\tint cnt = 0;\n\tfor( int i = 0; i < b ->size(); i++ ) {\n\t\tcnt ++;\n\t\tif( i < b -> size() - 1 && b -> at( i ) != b -> at( i + 1 ) ) {\n\t\t\tv.push_back( cnt );\n\t\t\tcnt = 0;\n\t\t}\n\t}\n\tv.push_back( cnt );\n\treturn v;\n}\n\nbool isEqual( vector< int > *v, vector< int > *w ) {\n\tif( v->size() != w->size() ) return false;\n\tfor( int i = 0; i < v->size(); i++ ) {\n\t\tif( v->at(i) != w->at(i) ) return false;\n\t}\n\treturn true;\n}\n\nvoid swap_bits( vector< int > *b, int pos ) {\n\tint c = b -> at( pos );\n\tb -> at( pos ) = b -> at( pos + 1 );\n\tb -> at( pos + 1 ) = c;\n\treturn;\n}\n\nint main(void){\n\n\tint N, M;\n\tcin >> N >> M;\n\tstate start;\n\tstart.bits = vector< int >( N, 0 );\n\tstart.depth = 0;\n\tvector< int > p( M, 0 );\n\tvector< bool > visited( 1 << N, false );\n\n\tfor( int i = 0; i < N; i++ )\n\t\tcin >> start.bits[i];\n\tfor( int i = 0; i < M; i++ )\n\t\tcin >> p[i];\n\n\tqueue< state > q;\n\tq.push( start );\n\n\twhile( !q.empty() ) {\n\t\t\n\t\tstate cs = q.front();\n\t\tq.pop();\n\t\tvector< int > v = count_bits( &cs.bits );\n\t\tif( isEqual( &v, &p ) ) { \n\t\t\tcout << cs.depth << endl;\n\t\t\tbreak;\n\t\t}\n\t\t\n\t\tfor( int i = 0; i < N - 1; i++ ) {\n\t\t\tstate newstate;\n\t\t\tfor( int j = 0; j < N; j++ )\n\t\t\t\tnewstate.bits.push_back( cs.bits[j] );\n\n\t\t\tint n = 0;\n\t\t\tswap_bits( &newstate.bits, i );\n\n\t\t\tfor( int j = 0; j < N; j++ ) {\n\t\t\t\tif( newstate.bits[j] ) n |= ( 1 << j );\n\t\t\t}\n\n\t\t\tif( visited[ n ] ) continue;\n\t\t\tvisited[n] = true;\n\n\t\t\tnewstate.depth = cs.depth + 1;\n\t\t\t\n\t\t\tq.push( newstate );\n\t\t}\n\n\n\t}\n\n\n return 0;\n}", "accuracy": 1, "time_ms": 20, "memory_kb": 1312, "score_of_the_acc": -0.0286, "final_rank": 2 }, { "submission_id": "aoj_1345_1445824", "code_snippet": "#include <iostream>\n#include <map>\n#include <cstring>\n#include <cassert>\n#include <vector>\n#include <queue>\nusing namespace std;\n\ntemplate<class T>\nostream& operator<<(ostream& os, const vector<T>& vs) {\n if (vs.empty()) return os << \"[]\";\n os << \"[\" << vs[0];\n for (int i = 1; i < vs.size(); i++) os << \" \" << vs[i];\n return os << \"]\";\n}\n\nint N, M;\nvector<int> S;\nvector<int> T;\nvoid input() {\n cin >> N >> M;\n S.clear(); S.resize(N);\n for (int i = 0; i < N; i++) {\n cin >> S[i];\n }\n T.clear(); T.resize(M);\n for (int i = 0; i < M; i++) {\n cin >> T[i];\n }\n}\n\nvector<int> A, B;\nvoid init() {\n A.clear(); B.clear();\n int x = 0;\n for (int i = 0; i < M; i++) {\n for (int j = 0; j < T[i]; j++) {\n A.push_back(x);\n }\n x = !x;\n }\n x = 1;\n for (int i = 0; i < M; i++) {\n for (int j = 0; j < T[i]; j++) {\n B.push_back(x);\n }\n x = !x;\n }\n}\n\nconst int MAX_N = 15;\n\nstruct State {\n vector<int> bit;\n int t;\n State(vector<int> bit, int t) : bit(bit), t(t) {}\n};\n\nvoid solve() {\n init();\n map< vector<int>, bool > used;\n queue<State> Q;\n used[S] = true;\n Q.push(State(S, 0));\n while (!Q.empty()) {\n State cur = Q.front(); Q.pop();\n if (cur.bit == A || cur.bit == B) {\n cout << cur.t << endl;\n break;\n }\n for (int i = 0; i < N - 1; i++) {\n vector<int> nbit = cur.bit;\n if (nbit[i] == nbit[i + 1]) continue;\n swap(nbit[i], nbit[i + 1]);\n if (used[nbit]) continue;\n used[nbit] = true;\n Q.push(State(nbit, cur.t + 1));\n }\n }\n}\n\nint main() {\n input(); solve();\n return 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 2256, "score_of_the_acc": -0.0529, "final_rank": 3 } ]
aoj_1341_cpp
Problem G: Longest Chain Let us compare two triples a = (x a , y a , z a ) and b = (x b , y b , z b ) by a partial order ∠ defined as follows. a ∠ b ⇔ x a < x b and y a < y b and z a < z b Your mission is to find, in the given set of triples, the longest ascending series a 1 ∠ a 2 ∠ ... ∠ a k . Input The input is a sequence of datasets, each specifying a set of triples formatted as follows. m n A B x 1 y 1 z 1 x 2 y 2 z 2 ... x m y m z m Here, m , n , A and B in the first line, and all of x i , y i and z i ( i = 1, . . . , m ) in the following lines are non-negative integers. Each dataset specifies a set of m + n triples. The triples p 1 through p m are explicitly specified in the dataset, the i -th triple p i being ( x i , y i , z i ) . The remaining n triples are specified by parameters A and B given to the following generator routine. int a = A, b = B, C = ~(1<<31), M = (1<<16)-1; int r() { a = 36969 * (a & M) + (a >> 16); b = 18000 * (b & M) + (b >> 16); return (C & ((a << 16) + b)) % 1000000; } Repeated 3 n calls of r() defined as above yield values of x m+1 , y m+1 , z m+1 , x m+2 , y m+2 , z m+2 , ..., x m+n , y m+n , and z m+n , in this order. You can assume that 1 ≤ m + n ≤ 3 × 10 5 , 1 ≤ A,B ≤ 2 16 , and 0 ≤ x k , y k , z k < 10 6 for 1 ≤ k ≤ m + n . The input ends with a line containing four zeros. The total of m + n for all the datasets does not exceed 2 × 10 6 . Output For each dataset, output the length of the longest ascending series of triples in the specified set. If p i 1 ∠ p i 2 ∠ ... ∠ p i k is the longest, the answer should be k . Sample Input 6 0 1 1 0 0 0 0 2 2 1 1 1 2 0 2 2 2 0 2 2 2 5 0 1 1 0 0 0 1 1 1 2 2 2 3 3 3 4 4 4 10 0 1 1 3 0 0 2 1 0 2 0 1 1 2 0 1 1 1 1 0 2 0 3 0 0 2 1 0 1 2 0 0 3 0 10 1 1 0 0 0 0 Output for the Sample Input 3 5 1 3
[ { "submission_id": "aoj_1341_10853188", "code_snippet": "#include<map>\n#include<set>\n#include<queue>\n#include<cctype>\n#include<cstdio>\n#include<vector>\n#include<cstdlib>\n#include<cstring>\n#include<algorithm>\ntypedef long long LL;\n#define MAXN 300010\nint A, B, C = ~(1<<31), M = (1<<16)-1;\nint myRand() {\n A = 36969 * (A & M) + (A >> 16);\n B = 18000 * (B & M) + (B >> 16);\n return (C & ((A << 16) + B)) % 1000000;\n}\nint N, zn, z[MAXN], f[MAXN], max[MAXN];\nstruct V {\n\tint x, y, z;\n\tbool operator < (const V &t) const {\n\t\tif(x != t.x) {\n\t\t\treturn x < t.x;\n\t\t}\n\t\treturn y > t.y;\n\t}\n}v[MAXN];\nstruct T {\n\tint id, x, y, z;\n\tbool operator < (const T &t) const {\n\t\tif(y != t.y) {\n\t\t\treturn y < t.y;\n\t\t}\n\t\tif(x != t.x) {\n\t\t\treturn x > t.x;\n\t\t}\n\t\treturn z > t.z;\n\t}\n}t[MAXN];\nint query(int x) {\n\tint ans = 0;\n\tfor(int i = x; i > 0; i -= i & -i) {\n\t\tans = std::max(ans, max[i]);\n\t}\n\treturn ans;\n}\nvoid update(int x, int v) {\n\tfor(int i = x; i <= zn; i += i & -i) {\n\t\tmax[i] = std::max(max[i], v);\n\t}\n}\nvoid clear(int x) {\n\tfor(int i = x; i <= zn; i += i & -i) {\n\t\tmax[i] = 0;\n\t}\n}\nvoid cdq(int x, int y) {\n\tif(x == y) {\n\t\treturn ;\n\t}\n\tint mid = (x + y) >> 1;\n\tcdq(x, mid);\n\tfor(int i = x; i <= y; i ++) {\n\t\tt[i].id = i;\n\t\tt[i].x = v[i].x, t[i].y = v[i].y, t[i].z = v[i].z;\n\t}\n\tstd::sort(t + x, t + y + 1);\n\tfor(int i = x; i <= y; i ++) {\n\t\tif(t[i].id <= mid) {\n\t\t\tupdate(t[i].z, f[t[i].id]);\n\t\t} else {\n\t\t\tf[t[i].id] = std::max(f[t[i].id], query(t[i].z - 1) + 1);\n\t\t}\n\t}\n\tfor(int i = x; i <= y; i ++) {\n\t\tif(t[i].id <= mid) {\n\t\t\tclear(t[i].z);\n\t\t}\n\t}\n\tcdq(mid + 1, y);\n}\nint main() {\n\t//freopen(\"test.in\", \"rb\", stdin);\n\tint m;\n\twhile(scanf(\"%d%d%d%d\", &N, &m, &A, &B), N + m > 0) {\n\t\tfor(int i = 0; i < N; i ++) {\n\t\t\tscanf(\"%d%d%d\", &v[i].x, &v[i].y, &v[i].z);\n\t\t}\n\t\tfor(int i = 0; i < m; i ++) {\n\t\t\tv[N].x = myRand();\n\t\t\tv[N].y = myRand();\n\t\t\tv[N].z = myRand();\n\t\t\t++ N;\n\t\t}\n\t\tstd::sort(v, v + N);\n\t\tfor(int i = 0; i < N; i ++) {\n\t\t\tz[i] = v[i].z;\n\t\t}\n\t\tstd::sort(z, z + N);\n\t\tzn = std::unique(z, z + N) - z;\n\t\tfor(int i = 0; i < N; i ++) {\n\t\t\tv[i].z = std::lower_bound(z, z + zn, v[i].z) - z + 1;\n\t\t}\n\t\tfor(int i = 0; i < N; i ++) {\n\t\t\tf[i] = 1;\n\t\t}\n\t\tcdq(0, N - 1);\n\t\tint ans = 0;\n\t\tfor(int i = 0; i < N; i ++) {\n\t\t\tans = std::max(ans, f[i]);\n\t\t}\n\t\tprintf(\"%d\\n\", ans);\n\t}\n\treturn 0;\n}", "accuracy": 1, "time_ms": 1710, "memory_kb": 14748, "score_of_the_acc": -0.147, "final_rank": 9 }, { "submission_id": "aoj_1341_9277865", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n#define ll long long\n#define rep(i, a, b) for(ll i = a; i < b; i++)\n#define all(x) (x).begin(), (x).end()\n\nstruct FenwickTree {\n int n;\n vector<int> data;\n FenwickTree() = default;\n FenwickTree(int n) : n(n), data(n+1) {}\n\n int fold(int i) const {\n int ret = 0;\n for(;i>0;i-=i&-i) ret=max(ret,data[i]);\n return ret;\n }\n void update(int i, int x) {\n for(++i; i<=n; i+=i&-i) data[i]=max(data[i],x);\n }\n\n};\n\nstruct SegTree2D {\n int size;\n vector<int> xs;\n vector<vector<int>> ys;\n vector<FenwickTree> seg;\n\n int getx(int x) const {\n return lower_bound(all(xs), x)-xs.begin();\n }\n int gety(int k, int y) const {\n return lower_bound(all(ys[k]), y) - ys[k].begin();\n }\n\n SegTree2D(const vector<pair<int,int>>& pts){\n for (auto[x,y]:pts){\n xs.push_back(x);\n }\n sort(all(xs));\n xs.erase(unique(all(xs)), xs.end());\n\n const int n =xs.size();\n size=1;\n while(size < n)size<<=1;\n ys.resize(2*size);\n seg.resize(2*size);\n\n for (auto[x,y]:pts){\n ys[size+getx(x)].push_back(y);\n }\n\n for(int i=size+n-1; i>0;--i){\n if (i>=size){\n sort(all(ys[i]));\n }else{\n merge(all(ys[2*i]), all(ys[2*i+1]), back_inserter(ys[i]));\n }\n ys[i].erase(unique(all(ys[i])), ys[i].end());\n }\n rep(i,1,size+n){\n seg[i]=FenwickTree(ys[i].size());\n }\n }\n\n void update(int x, int y, int val){\n int kx=getx(x);\n kx +=size;\n int ky=gety(kx,y);\n seg[kx].update(ky,val);\n while(kx>>=1){\n ky=gety(kx,y);\n // int kl=gety(2*kx,y), kr=gety(2*kx+1,y);\n // int vl=kl < ys[2*kx].size()&&ys[2*kx][kl]==y ? seg[2*kx][kl] : 0;\n // int vr=kr < ys[2*kx+1].size()&&ys[2*kx+1][kr]==y ? seg[2*kx+1][kr] : 0;\n seg[kx].update(ky, val);\n }\n }\n\n int fold(int tx, int ty) const {\n int ret = 0;\n for (int l = size, r=size+getx(tx); l < r; l >>= 1, r>>=1) {\n if (r&1){\n --r;\n ret = max(ret, seg[r].fold(gety(r, ty)));\n }\n }\n return ret;\n }\n\n};\n\nint main() {\n const int M = 1e6+10;\n vector<vector<pair<int,int>>> pts(M);\n\n while (true) {\n int n, m, A, B;\n cin >> m >> n >> A >> B;\n if (m + n == 0) break;\n\n int a = A, b = B, C = ~(1<<31), M = (1<<16)-1;\n\n auto r = [&]() {\n a = 36969 * (a & M) + (a >> 16);\n b = 18000 * (b & M) + (b >> 16);\n return (C & ((a << 16) + b)) % 1000000;\n };\n\n vector<int> xs;\n vector<pair<int,int>> yz;\n\n rep(i,0,m){\n int x,y,z;\n cin>>x>>y>>z;\n pts[x].push_back({y, z});\n yz.push_back({y,z});\n xs.push_back(x);\n }\n\n rep(_,0,n){\n int x=r();\n int y=r();\n int z=r();\n pts[x].push_back({y,z});\n yz.push_back({y,z});\n xs.push_back(x);\n }\n\n sort(all(xs));\n xs.erase(unique(all(xs)), xs.end());\n\n SegTree2D seg(yz);\n\n int ans = 0;\n\n for(auto x:xs){\n vector<int> res;\n for(auto [y,z]:pts[x]){\n res.push_back(seg.fold(y,z)+1);\n ans=max(ans,res.back());\n }\n int k=0;\n for(auto[y,z]:pts[x]){\n seg.update(y,z,res[k++]);\n }\n pts[x].clear();\n }\n\n cout << ans << \"\\n\";\n cout << flush;\n }\n}", "accuracy": 1, "time_ms": 5370, "memory_kb": 168640, "score_of_the_acc": -1.6695, "final_rank": 20 }, { "submission_id": "aoj_1341_8996636", "code_snippet": "#line 2 \"cp-library/src/cp-template.hpp\"\n#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\nusing ld = long double;\nusing uint = unsigned int;\nusing ull = unsigned long long;\nusing i32 = int;\nusing u32 = unsigned int;\nusing i64 = long long;\nusing u64 = unsigned long long;\nusing i128 = __int128_t;\ntemplate < class T > bool chmin(T& a, T b) { if(a > b) { a = b; return true; } return false; }\ntemplate < class T > bool chmax(T& a, T b) { if(a < b) { a = b; return true; } return false; }\ntemplate < class T, class U > T ceil (T x, U y) { return (x > 0 ? (x + y - 1) / y : x / y); }\ntemplate < class T, class U > T floor(T x, U y) { return (x > 0 ? x / y : (x - y + 1) / y); }\nint popcnt(i32 x) { return __builtin_popcount(x); }\nint popcnt(u32 x) { return __builtin_popcount(x); }\nint popcnt(i64 x) { return __builtin_popcountll(x); }\nint popcnt(u64 x) { return __builtin_popcountll(x); }\n\n#line 2 \"cp-library/src/utility/rep_itr.hpp\"\ntemplate < class T > struct itr_rep {\n T i, d;\n constexpr itr_rep(const T i) noexcept : i(i), d(1) {}\n constexpr itr_rep(const T i, const T d) noexcept : i(i), d(d) {}\n void operator++() noexcept { i += d; }\n constexpr int operator*() const noexcept { return i; }\n constexpr bool operator!=(const itr_rep x) const noexcept { return d > 0 ? i < x.i : i > x.i; }\n};\n\ntemplate < class T > struct rep {\n const itr_rep< T > s, t;\n constexpr rep(const T t) noexcept : s(0), t(t) {}\n constexpr rep(const T s, const T t) noexcept : s(s), t(t) {}\n constexpr rep(const T s, const T t, const T d) noexcept : s(s, d), t(t, d) {}\n constexpr auto begin() const noexcept { return s; }\n constexpr auto end () const noexcept { return t; }\n};\n\ntemplate < class T > struct revrep {\n const itr_rep < T > s, t;\n constexpr revrep(const T t) noexcept : s(t - 1, -1), t(-1, -1) {}\n constexpr revrep(const T s, const T t) noexcept : s(t - 1, -1), t(s - 1, -1) {}\n constexpr revrep(const T s, const T t, const T d) noexcept : s(t - 1, -d), t(s - 1, -d) {}\n constexpr auto begin() const noexcept { return s; }\n constexpr auto end () const noexcept { return t; }\n};\n#line 3 \"cp-library/src/utility/io.hpp\"\n\n/* 128bit integer */\nistream& operator>>(istream& is, i128& x) {\n std::string s; is >> s;\n int pm = (s[0] == '-');\n x = 0;\n for(int i : rep(pm, int(s.size()))) x = x * 10 + (s[i] - '0');\n if(pm) x *= -1;\n return is;\n}\nostream& operator<<(ostream& os, const i128& x) {\n if(x == 0) return os << '0';\n i128 y = x;\n if(y < 0) { os << '-'; y *= -1; }\n std::vector<int> ny;\n while(y > 0) { ny.push_back(y % 10); y /= 10; }\n for(int i : revrep(ny.size())) os << ny[i];\n return os;\n}\n\ntemplate < class S, class T > istream& operator>>(istream& is, std::pair< S, T >& x) { is >> x.first >> x.second; return is; }\ntemplate < class S, class T > ostream& operator<<(ostream& os, const std::pair< S, T >& x) { os << x.first << \" \" << x.second; return os; }\n\nnamespace scanner {\n struct sca {\n template < class T > operator T() {\n T s; std::cin >> s; return s;\n }\n };\n struct vec {\n int n;\n vec(int n) : n(n) {}\n template < class T > operator std::vector< T >() {\n std::vector< T > v(n);\n for(T& x : v) std::cin >> x;\n return v;\n }\n };\n struct mat {\n int h, w;\n mat(int h, int w) : h(h), w(w) {}\n template < class T > operator std::vector< std::vector< T > >() {\n std::vector m(h, std::vector< T >(w));\n for(std::vector< T >& v : m) for(T& x : v) std::cin >> x;\n return m;\n }\n };\n struct speedup {\n speedup() {\n std::cin.tie(0);\n std::ios::sync_with_stdio(0);\n }\n } speedup_instance;\n}\nscanner::sca in() { return scanner::sca(); }\nscanner::vec in(int n) { return scanner::vec(n); }\nscanner::mat in(int h, int w) { return scanner::mat(h, w); }\n\nnamespace printer {\n void precision(int d) { std::cout << std::fixed << std::setprecision(d); }\n void flush() { std::cout.flush(); }\n}\n\ntemplate < class T >\nostream& operator<<(ostream& os, const std::vector< T > a) {\n int n = a.size();\n for(int i : rep(n)) { os << a[i]; if(i != n - 1) os << ' '; }\n return os;\n}\n\nint print() { std::cout << '\\n'; return 0; }\ntemplate < class head, class... tail > int print(head&& h, tail&&... t) {\n std::cout << h; if(sizeof...(tail)) std::cout << ' ';\n return print(std::forward<tail>(t)...);\n}\ntemplate < class T > int print_n(const std::vector< T > a) {\n int n = a.size();\n for(int i : rep(n)) std::cout << a[i] << \"\\n\";\n return 0;\n}\n\n\n#line 2 \"cp-library/src/utility/key_val.hpp\"\n\ntemplate < class K, class V >\nstruct key_val {\n K key; V val;\n key_val() {}\n key_val(K key, V val) : key(key), val(val) {}\n template < std::size_t Index >\n std::tuple_element_t< Index, key_val >& get() {\n if constexpr (Index == 0) return key;\n if constexpr (Index == 1) return val;\n }\n};\n\nnamespace std {\n\ntemplate < class K, class V > struct tuple_size < key_val< K, V > > : integral_constant< size_t, 2 > {};\ntemplate < class K, class V > struct tuple_element < 0, key_val< K, V > > { using type = K; };\ntemplate < class K, class V > struct tuple_element < 1, key_val< K, V > > { using type = V; };\n\n}\n#line 2 \"cp-library/src/utility/vec_op.hpp\"\ntemplate < class T > key_val< int, T > max_of(const vector< T >& a) {\n int i = std::max_element(a.begin(), a.end()) - a.begin();\n return {i, a[i]};\n}\ntemplate < class T > key_val< int, T > min_of(const vector< T >& a) {\n int i = std::min_element(a.begin(), a.end()) - a.begin();\n return {i, a[i]};\n}\ntemplate < class S, class T > S sum_of(const vector< T >& a) {\n S sum = 0;\n for(const T x : a) sum += x;\n return sum;\n}\ntemplate < class S, class T > vector< S > freq_of(const vector< T >& a, T L, T R) {\n vector< S > res(R - L, S(0));\n for(const T x : a) res[x - L] += 1;\n return res;\n}\ntemplate < class S, class T > struct prefix_sum {\n vector< S > s;\n prefix_sum(const vector< T >& a) : s(a) {\n s.insert(s.begin(), S(0));\n for(int i : rep(a.size())) s[i + 1] += s[i];\n }\n // [L, R)\n S sum(int L, int R) { return s[R] - s[L]; }\n};\n#line 3 \"cp-library/src/utility/heap.hpp\"\n\ntemplate < class T > using heap_min = std::priority_queue< T, std::vector< T >, std::greater< T > >;\ntemplate < class T > using heap_max = std::priority_queue< T, std::vector< T >, std::less< T > >;\n\n#line 27 \"cp-library/src/cp-template.hpp\"\n\n#line 1 \"cp-library/src/algorithm/bin_search.hpp\"\ntemplate < class T, class F >\nT bin_search(T ok, T ng, F f) {\n while(abs(ng - ok) > 1) {\n T mid = (ok + ng) / 2;\n (f(mid) ? ok : ng) = mid;\n }\n return ok;\n}\n\ntemplate < class T, class F >\nT bin_search_real(T ok, T ng, F f, int step = 80) {\n while(step--) {\n T mid = (ok + ng) / 2;\n (f(mid) ? ok : ng) = mid;\n }\n return ok;\n}\n#line 2 \"cp-library/src/algorithm/argsort.hpp\"\n\ntemplate < class T > std::vector< int > argsort(const std::vector< T > &a) {\n std::vector< int > ids((int)a.size());\n std::iota(ids.begin(), ids.end(), 0);\n std::sort(ids.begin(), ids.end(), [&](int i, int j) {\n return a[i] < a[j] || (a[i] == a[j] && i < j);\n });\n return ids;\n}\n#line 1 \"macro.hpp\"\nnamespace macro {\n\nusing size_type = int;\ntemplate < class container > void sort(container& a) { std::sort(std:: begin(a), std:: end(a)); }\ntemplate < class container > void rsort(container& a) { std::sort(std::rbegin(a), std::rend(a)); }\ntemplate < class container > void reverse(container& a) { std::reverse(std::begin(a), std::end(a)); }\ntemplate < class container > void unique(container& a) {\n std::sort(std::begin(a), std::end(a));\n a.erase(std::unique(std::begin(a), std::end(a)), std::end(a));\n}\ntemplate < class container > container sorted(const container& a) { container b = a; sort(b); return std::move(b); }\ntemplate < class container > container rsorted(const container& a) { container b = a; rsort(b); return std::move(b); }\ntemplate < class container, class compare > void sort(container& a, const compare& cmp) { std::sort(std::begin(a), std::end(a), cmp); }\ntemplate < class container, class compare > container sorted(const container& a, const compare& cmp) { container b = a; sort(b, cmp); return std::move(b); }\ntemplate < class container, class value > size_type lower_bound(const container& a, const value& x) { return std::lower_bound(std::begin(a), std::end(a), x) - std::begin(a); }\ntemplate < class container, class value > size_type upper_bound(const container& a, const value& x) { return std::upper_bound(std::begin(a), std::end(a), x) - std::begin(a); }\n\nconst std::vector<std::pair<size_type, size_type>> dir4 = { {+1, 0}, {-1, 0}, { 0, +1}, { 0, -1} };\nconst std::vector<std::pair<size_type, size_type>> dir8 = { {-1, -1}, {-1, 0}, {-1, +1}, { 0, -1}, { 0, +1}, {+1, -1}, {+1, 0}, {+1, +1} };\n\n#ifdef _DEBUG\n#define debug(x) std::cout << \"[\" << __LINE__ << \"] \" << #x << \": \" << x << std::endl\n#else\n#define debug(x)\n#endif\n\ntemplate < class container > void concat(container& a, const container& b) {\n a.insert(std::end(a), std::begin(b), std::end(b));\n}\nstd::vector<size_type> iota(const size_type n) {\n std::vector<size_type> I(n);\n std::iota(std::begin(I), std::end(I), 0);\n return I;\n}\ntemplate < class container > std::vector<size_type> sort_idx(const container& a) {\n const size_type n = a.size();\n std::vector<size_type> I = iota(n);\n std::sort(std::begin(I), std::end(I), [&](size_type i, size_type j) { return a[i] < a[j] or (a[i] == a[j] and i < j); });\n return I;\n}\ntemplate < class container, class compare > std::vector<size_type> sort_idx(const container& a, const compare& cmp) {\n const size_type n = a.size();\n std::vector<size_type> I = iota(n);\n std::sort(std::begin(I), std::end(I), [&](size_type i, size_type j) { return cmp(a[i], a[j]) or (a[i] == a[j] and i < j); });\n return std::move(I);\n}\n\nstruct grid {\n using size_type = int;\n size_type H, W;\n grid(const size_type H, const size_type W) : H(H), W(W) {}\n bool contains(const size_type i, const size_type j) {\n return 0 <= i and i < H and 0 <= j and j < W;\n }\n};\n\nusing f64 = long double;\n\n} // namespace macro\n\nusing namespace macro;\n#line 3 \"A.cpp\"\n\nint solve(int m, int n, int A, int B) {\n vector<int> x(m), y(m), z(m);\n for(int i : rep(m)) x[i] = in(), y[i] = in(), z[i] = in();\n {\n int a = A, b = B, C = ~(1 << 31), M = (1 << 16) - 1;\n auto r = [&]() {\n a = 36969 * (a & M) + (a >> 16);\n b = 18000 * (b & M) + (b >> 16);\n return (C & ((a << 16) + b)) % 1000000;\n };\n for(int i : rep(n)) {\n x.push_back(r());\n y.push_back(r());\n z.push_back(r());\n }\n n = m + n;\n }\n\n vector<map<int,int>> dp; // lis\n for(int i : sort_idx(iota(n), [&](int i, int j) {\n if(z[i] != z[j]) return z[i] < z[j];\n if(x[i] != x[j]) return x[i] > x[j];\n return y[i] > y[j];\n })) {\n const int len = dp.size();\n int p = 1 + bin_search(-1, len, [&](int p) {\n // exist? * < (x, y)\n auto it = dp[p].lower_bound(x[i]);\n return it != dp[p].begin() and prev(it)->second < y[i];\n });\n\n // insert (x, y) to dp[p]\n if(p == len) {\n dp.push_back({{x[i], y[i]}});\n } else {\n // erase * >= (x, y)\n auto it = dp[p].lower_bound(x[i]);\n while(it != dp[p].end() and it->second >= y[i]) it = dp[p].erase(it);\n if(not(it != dp[p].begin() and prev(it)->second <= y[i])) dp[p].insert({x[i], y[i]});\n }\n }\n return dp.size();\n}\n\nint main() {\n while(true) {\n int m = in(), n = in(), A = in(), B = in();\n if(make_tuple(m, n, A, B) == make_tuple(0, 0, 0, 0)) return 0;\n print(solve(m, n, A, B));\n }\n}", "accuracy": 1, "time_ms": 780, "memory_kb": 15696, "score_of_the_acc": -0.0201, "final_rank": 4 }, { "submission_id": "aoj_1341_8526024", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\n#define rep(i, n) for (int i = 0; i < (n); i++)\n#define per(i, n) for (int i = (n)-1; i >= 0; i--)\n#define rep2(i, l, r) for (int i = (l); i < (r); i++)\n#define per2(i, l, r) for (int i = (r)-1; i >= (l); i--)\n#define each(e, x) for (auto &e : x)\n#define sz(x) (int)x.size()\n#define all(x) begin(x), end(x)\n#define rall(x) rbegin(x), rend(x)\n#define eb emplace_back\n#define TT template <typename T>\n#define MM << ' ' <<\nusing ll = long long;\nusing pii = pair<int, int>;\nusing pll = pair<ll, ll>;\n\nTT bool chmin(T &x, T y) { return x > y ? (x = y, true) : false; }\nTT bool chmax(T &x, T y) { return x < y ? (x = y, true) : false; }\n\nTT using minheap = priority_queue<T, vector<T>, greater<T>>;\nTT using maxheap = priority_queue<T>;\n\nTT void print(vector<T> v, T x = 0) {\n int n = sz(v);\n rep(i, n) cout << v[i] + x << (i == n - 1 ? '\\n' : ' ');\n if (n == 0) cout << '\\n';\n}\n\nTT void printn(vector<T> v, T x = 0) {\n int n = sz(v);\n rep(i, n) cout << v[i] + x << '\\n';\n}\n\nTT void rearrange(vector<T> &v) {\n sort(all(v));\n v.erase(unique(all(v)), end(v));\n}\n\nTT int lb(vector<T> &v, T x) { return lower_bound(all(v), x) - begin(v); }\nTT int ub(vector<T> &v, T x) { return upper_bound(all(v), x) - begin(v); }\n\nconst int inf = (1 << 30) - 1;\nconst ll INF = (1LL << 60) - 1;\n\nstruct io_setup {\n io_setup() {\n ios_base::sync_with_stdio(false);\n cin.tie(NULL);\n cout << fixed << setprecision(15);\n }\n} io_setup;\n\ntemplate <typename T>\nT f(T x, T y) {\n return max(x, y);\n}\n\ntemplate <typename T>\nstruct BIT {\n int n;\n vector<T> a;\n\n BIT(vector<T> v) : n(sz(v)) {\n a.assign(n + 1, 0);\n copy(all(v), begin(a) + 1);\n for (int i = 2; i <= n; i <<= 1) {\n for (int j = i; j <= n; j += i) a[j] = f(a[j], a[j - i / 2]);\n }\n }\n\n BIT(int n, T x) : BIT(vector<T>(n, x)) {}\n\n void add(int i, T x) {\n for (i++; i <= n; i += (i & -i)) a[i] = f(a[i], x);\n }\n\n T sum(int i) {\n T ret = 0;\n for (; i > 0; i -= (i & -i)) ret = f(ret, a[i]);\n return ret;\n }\n};\n\ntemplate <typename T, typename C>\nstruct BIT2D {\n int n;\n vector<C> xs;\n vector<pair<C, C>> allPoints;\n vector<vector<C>> ys;\n vector<BIT<T>> bits;\n\n BIT2D() {}\n\n void insert(C x, C y) {\n xs.eb(x);\n allPoints.eb(x, y);\n }\n\n void build() {\n rearrange(xs);\n n = sz(xs);\n ys.resize(n + 1);\n for (auto [x, y] : allPoints) {\n int i = lb(xs, x);\n for (i++; i <= n; i += (i & -i)) ys[i].eb(y);\n }\n rep(i, n + 1) {\n rearrange(ys[i]);\n bits.eb(sz(ys[i]), 0);\n }\n }\n\n void add(C x, C y, T a) {\n int i = lb(xs, x);\n for (i++; i <= n; i += (i & -i)) {\n int j = lb(ys[i], y);\n bits[i].add(j, a);\n }\n }\n\n T sum(C x, C y) {\n int i = lb(xs, x);\n T ret = 0;\n for (; i > 0; i -= (i & -i)) {\n int j = lb(ys[i], y);\n ret = f(ret, bits[i].sum(j));\n }\n return ret;\n }\n};\n\nusing ull = unsigned long long;\n\nvoid solve() {\n int M, N;\n int A, B;\n cin >> M >> N >> A >> B;\n\n if (N == 0 && M == 0) exit(0);\n\n vector<int> x(M), y(M), z(M);\n rep(i, M) cin >> x[i] >> y[i] >> z[i];\n\n int a = A, b = B;\n auto rand = [&]() {\n int C = ~(1 << 31), M = (1 << 16) - 1;\n a = 36969 * (a & M) + (a >> 16);\n b = 18000 * (b & M) + (b >> 16);\n return (C & ((a << 16) + b)) % 1000000;\n };\n\n rep(i, 3 * N) {\n int t = rand();\n (i % 3 == 0 ? x : i % 3 == 1 ? y : z).eb(t);\n }\n\n N += M;\n\n // rep(i, N) cout << x[i] MM y[i] MM z[i] << '\\n';\n\n const int MAX = 1000000;\n\n BIT2D<int, int> bit;\n vector<vector<int>> ids(MAX);\n\n rep(i, N) {\n ids[z[i]].eb(i);\n bit.insert(x[i], y[i]);\n }\n bit.build();\n\n vector<int> dp(N, -inf);\n\n rep(i, MAX) {\n each(e, ids[i]) {\n dp[e] = bit.sum(x[e], y[e]) + 1; //\n }\n each(e, ids[i]) {\n bit.add(x[e], y[e], dp[e]); //\n }\n }\n\n cout << *max_element(all(dp)) << '\\n';\n}\n\nint main() {\n while (true) solve(); //\n}", "accuracy": 1, "time_ms": 4100, "memory_kb": 109136, "score_of_the_acc": -1.1016, "final_rank": 16 }, { "submission_id": "aoj_1341_8280278", "code_snippet": "#include <iostream>\n#include <tuple>\n#include <cmath>\n#include <vector>\n#include <algorithm>\nusing namespace std;\n\nclass RangeMax {\npublic:\n\tint size_ = 1;\n\tvector<int> dat;\n\n\tvoid init(int sz) {\n\t\twhile (size_ <= sz) size_ *= 2;\n\t\tdat.resize(size_ * 2, -1000000);\n\t}\n\n\tvoid update(int pos, int x) {\n\t\tpos += size_;\n\t\tdat[pos] = x;\n\t\twhile (pos >= 2) {\n\t\t\tpos >>= 1;\n\t\t\tdat[pos] = max(dat[pos * 2], dat[pos * 2 + 1]);\n\t\t}\n\t}\n\n\tint query_(int l, int r, int a, int b, int u) {\n\t\tif (l <= a && b <= r) return dat[u];\n\t\tif (r <= a || b <= l) return -(1 << 30);\n\t\tint v1 = query_(l, r, a, (a + b) / 2, u * 2);\n\t\tint v2 = query_(l, r, (a + b) / 2, b, u * 2 + 1);\n\t\treturn max(v1, v2);\n\t}\n\n\tint query(int l, int r) {\n\t\treturn query_(l, r, 0, size_, 1);\n\t}\n};\n\n// 誤読で 20 分溶かした.\nint M, N, A, B;\nint X[300009], Y[300009], Z[300009];\nint Bar[300009];\nint dp[300009];\nvector<int> LX, LY, LZ;\nRangeMax W;\n\nvoid Generate() {\n\tint a = A, b = B, C = ~(1 << 31), MM = (1 << 16) - 1;\n\tfor (int i = 0; i < 3 * N; i++) {\n\t\ta = 36969 * (a & MM) + (a >> 16);\n\t\tb = 18000 * (b & MM) + (b >> 16);\n\t\tint val = (C & ((a << 16) + b)) % 1000000;\n\t\tif (i % 3 == 0) X[M + i / 3] = val;\n\t\tif (i % 3 == 1) Y[M + i / 3] = val;\n\t\tif (i % 3 == 2) Z[M + i / 3] = val;\n\t}\n}\n\nvoid dfs(int cl, int cr) {\n\tif (cr - cl == 1) return;\n\tint cm = (cl + cr) / 2;\n\tint pl = Bar[cl];\n\tint pm = Bar[cm];\n\tint pr = Bar[cr];\n\n\t// Recursion 1\n\tdfs(cl, cm);\n\n\t// Initialize\n\tvector<pair<int, int>> List;\n\tfor (int i = pl; i < pm; i++) List.push_back(make_pair(Y[i], i));\n\tfor (int i = pm; i < pr; i++) List.push_back(make_pair(Y[i], -i));\n\tsort(List.begin(), List.end());\n\n\t// Main Query\n\tfor (int i = 0; i < List.size(); i++) {\n\t\tint idx = abs(List[i].second);\n\t\tif (idx < pm) {\n\t\t\tW.update(Z[idx], max(W.dat[Z[idx] + W.size_], dp[idx]));\n\t\t}\n\t\tif (idx >= pm) {\n\t\t\tdp[idx] = max(dp[idx], W.query(0, Z[idx]) + 1);\n\t\t}\n\t}\n\n\t// Reset\n\tfor (int i = 0; i < List.size(); i++) {\n\t\tint idx = abs(List[i].second);\n\t\tif (idx < pm) W.update(Z[idx], -1000000);\n\t}\n\n\t// Recursion 2\n\tdfs(cm, cr);\n}\n\nint Solve() {\n\t// Step 1. Compression\n\tLX.clear();\n\tLY.clear();\n\tLZ.clear();\n\tfor (int i = 0; i < M + N; i++) LX.push_back(X[i]);\n\tfor (int i = 0; i < M + N; i++) LY.push_back(Y[i]);\n\tfor (int i = 0; i < M + N; i++) LZ.push_back(Z[i]);\n\tsort(LX.begin(), LX.end()); LX.erase(unique(LX.begin(), LX.end()), LX.end());\n\tsort(LY.begin(), LY.end()); LY.erase(unique(LY.begin(), LY.end()), LY.end());\n\tsort(LZ.begin(), LZ.end()); LZ.erase(unique(LZ.begin(), LZ.end()), LZ.end());\n\tfor (int i = 0; i < M + N; i++) X[i] = lower_bound(LX.begin(), LX.end(), X[i]) - LX.begin();\n\tfor (int i = 0; i < M + N; i++) Y[i] = lower_bound(LY.begin(), LY.end(), Y[i]) - LY.begin();\n\tfor (int i = 0; i < M + N; i++) Z[i] = lower_bound(LZ.begin(), LZ.end(), Z[i]) - LZ.begin();\n\tW.init(LZ.size() + 2);\n\n\t// Step 2. Sorting\n\tvector<tuple<int, int, int>> tup;\n\tfor (int i = 0; i < M + N; i++) tup.push_back(make_tuple(X[i], Y[i], Z[i]));\n\tsort(tup.begin(), tup.end());\n\tfor (int i = 0; i < M + N; i++) {\n\t\tX[i] = get<0>(tup[i]);\n\t\tY[i] = get<1>(tup[i]);\n\t\tZ[i] = get<2>(tup[i]);\n\t}\n\tfor (int i = 0; i <= LX.size(); i++) Bar[i] = 0;\n\tfor (int i = 0; i < M + N; i++) Bar[X[i] + 1] += 1;\n\tfor (int i = 1; i <= LX.size(); i++) Bar[i] += Bar[i - 1];\n\n\t// Step 3. Divide and Conquer\n\tfor (int i = 0; i < M + N; i++) dp[i] = 1;\n\tdfs(0, LX.size());\n\tint maxn = 0;\n\tfor (int i = 0; i < M + N; i++) maxn = max(maxn, dp[i]);\n\treturn maxn;\n}\n\nint main() {\n\twhile (true) {\n\t\tcin >> M >> N >> A >> B;\n\t\tif (M + N + A + B == 0) break;\n\t\tfor (int i = 0; i < M; i++) cin >> X[i] >> Y[i] >> Z[i];\n\t\tGenerate();\n\t\tcout << Solve() << endl;\n\t}\n\treturn 0;\n}", "accuracy": 1, "time_ms": 3630, "memory_kb": 28636, "score_of_the_acc": -0.5118, "final_rank": 11 }, { "submission_id": "aoj_1341_7254860", "code_snippet": "#ifndef HIDDEN_IN_VS // 折りたたみ用\n\n// 警告の抑制\n#define _CRT_SECURE_NO_WARNINGS\n\n// ライブラリの読み込み\n#include <bits/stdc++.h>\nusing namespace std;\n\n// 型名の短縮\nusing ll = long long; // -2^63 ~ 2^63 = 9 * 10^18(int は -2^31 ~ 2^31 = 2 * 10^9)\nusing pii = pair<int, int>;\tusing pll = pair<ll, ll>;\tusing pil = pair<int, ll>;\tusing pli = pair<ll, int>;\nusing vi = vector<int>;\t\tusing vvi = vector<vi>;\t\tusing vvvi = vector<vvi>;\nusing vl = vector<ll>;\t\tusing vvl = vector<vl>;\t\tusing vvvl = vector<vvl>;\nusing vb = vector<bool>;\tusing vvb = vector<vb>;\t\tusing vvvb = vector<vvb>;\nusing vc = vector<char>;\tusing vvc = vector<vc>;\t\tusing vvvc = vector<vvc>;\nusing vd = vector<double>;\tusing vvd = vector<vd>;\t\tusing vvvd = vector<vvd>;\ntemplate <class T> using priority_queue_rev = priority_queue<T, vector<T>, greater<T>>;\nusing Graph = vvi;\n\n// 定数の定義\nconst double PI = acos(-1);\nconst vi DX = { 1, 0, -1, 0 }; // 4 近傍(下,右,上,左)\nconst vi DY = { 0, 1, 0, -1 };\nint INF = 1001001001; ll INFL = 4004004004004004004LL;\ndouble EPS = 1e-12;\n\n// 入出力高速化\nstruct fast_io { fast_io() { cin.tie(nullptr); ios::sync_with_stdio(false); cout << fixed << setprecision(18); } } fastIOtmp;\n\n// 汎用マクロの定義\n#define all(a) (a).begin(), (a).end()\n#define sz(x) ((int)(x).size())\n#define lbpos(a, x) (int)distance((a).begin(), std::lower_bound(all(a), x))\n#define ubpos(a, x) (int)distance((a).begin(), std::upper_bound(all(a), x))\n#define Yes(b) {cout << ((b) ? \"Yes\\n\" : \"No\\n\");}\n#define rep(i, n) for(int i = 0, i##_len = int(n); i < i##_len; ++i) // 0 から n-1 まで昇順\n#define repi(i, s, t) for(int i = int(s), i##_end = int(t); i <= i##_end; ++i) // s から t まで昇順\n#define repir(i, s, t) for(int i = int(s), i##_end = int(t); i >= i##_end; --i) // s から t まで降順\n#define repe(v, a) for(const auto& v : (a)) // a の全要素(変更不可能)\n#define repea(v, a) for(auto& v : (a)) // a の全要素(変更可能)\n#define repb(set, d) for(int set = 0; set < (1 << int(d)); ++set) // d ビット全探索(昇順)\n#define repp(a) sort(all(a)); for(bool a##_perm = true; a##_perm; a##_perm = next_permutation(all(a))) // a の順列全て(昇順)\n#define smod(n, m) ((((n) % (m)) + (m)) % (m)) // 非負mod\n#define uniq(a) {sort(all(a)); (a).erase(unique(all(a)), (a).end());} // 重複除去\n#define EXIT(a) {cout << (a) << endl; exit(0);} // 強制終了\n\n// 汎用関数の定義\ntemplate <class T> inline ll pow(T n, int k) { ll v = 1; rep(i, k) v *= n; return v; }\ntemplate <class T> inline bool chmax(T& M, const T& x) { if (M < x) { M = x; return true; } return false; } // 最大値を更新(更新されたら true を返す)\ntemplate <class T> inline bool chmin(T& m, const T& x) { if (m > x) { m = x; return true; } return false; } // 最小値を更新(更新されたら true を返す)\n\n// 演算子オーバーロード\ntemplate <class T, class U> inline istream& operator>>(istream& is, pair<T, U>& p) { is >> p.first >> p.second; return is; }\ntemplate <class T> inline istream& operator>>(istream& is, vector<T>& v) { repea(x, v) is >> x; return is; }\ntemplate <class T> inline vector<T>& operator--(vector<T>& v) { repea(x, v) --x; return v; }\ntemplate <class T> inline vector<T>& operator++(vector<T>& v) { repea(x, v) ++x; return v; }\n\n// 手元環境(Visual Studio)\n#ifdef _MSC_VER\n#include \"local.hpp\"\n// 提出用(gcc)\n#else\ninline int popcount(int n) { return __builtin_popcount(n); }\ninline int popcount(ll n) { return __builtin_popcountll(n); }\ninline int lsb(int n) { return n != 0 ? __builtin_ctz(n) : -1; }\ninline int lsb(ll n) { return n != 0 ? __builtin_ctzll(n) : -1; }\ninline int msb(int n) { return n != 0 ? (31 - __builtin_clz(n)) : -1; }\ninline int msb(ll n) { return n != 0 ? (63 - __builtin_clzll(n)) : -1; }\n#define gcd __gcd\n#define dump(...)\n#define dumpel(v)\n#define dump_list(v)\n#define dump_list2D(v)\n#define input_from_file(f)\n#define output_to_file(f)\n#define Assert(b) { if (!(b)) while (1) cout << \"OLE\"; }\n#endif\n\n#endif // 折りたたみ用\n\n\n////--------------AtCoder 専用--------------\n//#include <atcoder/all>\n//using namespace atcoder;\n//\n////using mint = modint1000000007;\n//using mint = modint998244353;\n////using mint = modint; // mint::set_mod(m);\n//\n//istream& operator>>(istream& is, mint& x) { ll x_; is >> x_; x = x_; return is; }\n//ostream& operator<<(ostream& os, const mint& x) { os << x.val(); return os; }\n//using vm = vector<mint>; using vvm = vector<vm>; using vvvm = vector<vvm>;\n////----------------------------------------\n\n\n//【狭義単調な点列】\n/*\n* Monotonous_points<T>(bool y_greater = true, T inf = max(T)/2) : O(1)\n*\t空で初期化する.x 座標は狭義単調増加で,y 座標は y_greater=true[false] なら狭義単調増加[減少]とする.\n* \n* void insert(T x, T y) : ならし O(log n)\n*\t点 (x, y) を挿入し,それにより単調性に違反する点は全て削除する.\n*\n* bool find_LL(T x, T y, bool strict = true) : O(log n)\n*\tx' < x かつ y' < y なる点 (x', y') が存在するかを返す(strict=false なら等号も許す)\n*\n* bool find_LG(T x, T y, bool strict = true) : O(log n)\n*\tx' < x かつ y' > y なる点 (x', y') が存在するかを返す(strict=false なら等号も許す)\n*\n* bool find_GL(T x, T y, bool strict = true) : O(log n)\n*\tx' > x かつ y' < y なる点 (x', y') が存在するかを返す(strict=false なら等号も許す)\n* \n* bool find_GG(T x, T y, bool strict = true) : O(log n)\n*\tx' > x かつ y' > y なる点 (x', y') が存在するかを返す(strict=false なら等号も許す)\n* \n* pTT lower_bound(T x) : O(log n)\n*\tx' >= x なる x 座標が最小の点 (x', y') を返す(なければ (inf, inf[ninf]))\n* \n* pTT upper_bound(T x) : O(log n)\n*\tx' > x なる x 座標が最小の点 (x', y') を返す(なければ (inf, inf[ninf]))\n* \n* pTT lower_bound_rev(T x) : O(log n)\n*\tx' <= x なる x 座標が最大の点 (x', y') を返す(なければ (ninf, ninf[inf]))\n* \n* pTT upper_bound_rev(T x) : O(log n)\n*\tx' < x なる x 座標が最大の点 (x', y') を返す(なければ (ninf, ninf[inf]))\n*/\ntemplate <class T>\nstruct Monotonous_points {\n\t// 参考 : https://topcoder-g-hatena-ne-jp.jag-icpc.org/skyaozora/20141216.html\n\n\tbool y_greater;\n\tT inf;\n\n\t// x, y 両座標ともに狭義単調増加な点列\n\tmap<T, T> x_to_y;\n\n\t// 空で初期化\n\tMonotonous_points(bool y_greater = true, T inf_ = -1) : y_greater(y_greater) {\n\t\tinf = (inf_ == -1 ? numeric_limits<T>::max() / 2 : inf_);\n\n\t\t// 番兵を挿入しておく.\n\t\tif (y_greater) { x_to_y[-inf] = -inf; x_to_y[inf] = inf; }\n\t\telse { x_to_y[-inf] = inf; x_to_y[inf] = -inf; }\n\t}\n\n\t// 点 (x, y) を挿入し,単調性に違反する点は全て削除する.\n\tvoid insert(T x, T y) {\n\t\t// verify : https://atcoder.jp/contests/abc283/tasks/abc283_f\n\n\t\t// x <= x' なる最小の x' を指すイテレータを得る.\n\t\tauto it = x_to_y.lower_bound(x);\n\n\t\t// x' から昇順に,y' <= y[ y' >= y ] である限り要素を削除する.\n\t\tif (y_greater) {\n\t\t\twhile (true) {\n\t\t\t\tif (it->second > y) break;\n\t\t\t\tit = x_to_y.erase(it);\n\t\t\t}\n\t\t}\n\t\telse {\n\t\t\twhile (true) {\n\t\t\t\tif (it->second < y) break;\n\t\t\t\tit = x_to_y.erase(it);\n\t\t\t}\n\t\t}\n\n\t\t// x' から降順に,y' >= y[ y' <= y ] である限り要素を削除する.\n\t\tif (y_greater) {\n\t\t\twhile (true) {\n\t\t\t\tit = prev(it);\n\t\t\t\tif (it->second < y) break;\n\t\t\t\tit = x_to_y.erase(it);\n\t\t\t}\n\t\t}\n\t\telse {\n\t\t\twhile (true) {\n\t\t\t\tit = prev(it);\n\t\t\t\tif (it->second > y) break;\n\t\t\t\tit = x_to_y.erase(it);\n\t\t\t}\n\t\t}\n\n\t\t// 点 (x, y) を挿入する.\n\t\tx_to_y[x] = y;\n\t}\n\n\t// x' < x かつ y' < y なる点が存在するかを返す(strict=false なら等号も許す)\n\tbool find_LL(T x, T y, bool strict = true) {\n\t\tif (strict) {\n\t\t\tT y2 = prev(x_to_y.lower_bound(x))->second;\n\t\t\treturn y2 != -inf && y2 < y;\n\t\t}\n\t\telse {\n\t\t\tT y2 = prev(x_to_y.upper_bound(x))->second;\n\t\t\treturn y2 != -inf && y2 <= y;\n\t\t}\n\t}\n\n\t// x' < x かつ y' > y なる点が存在するかを返す(strict=false なら等号も許す)\n\tbool find_LG(T x, T y, bool strict = true) {\n\t\tif (strict) {\n\t\t\tT y2 = prev(x_to_y.lower_bound(x))->second;\n\t\t\treturn y2 != inf && y2 > y;\n\t\t}\n\t\telse {\n\t\t\tT y2 = prev(x_to_y.upper_bound(x))->second;\n\t\t\treturn y2 != inf && y2 >= y;\n\t\t}\n\t}\n\n\t// x' > x かつ y' < y なる点が存在するかを返す(strict=false なら等号も許す)\n\tbool find_GL(T x, T y, bool strict = true) {\n\t\tif (strict) {\n\t\t\tT y2 = x_to_y.upper_bound(x)->second;\n\t\t\treturn y2 != -inf && y2 < y;\n\t\t}\n\t\telse {\n\t\t\tT y2 = x_to_y.lower_bound(x)->second;\n\t\t\treturn y2 != -inf && y2 <= y;\n\t\t}\n\t}\n\n\t// x' > x かつ y' > y なる点が存在するかを返す(strict=false なら等号も許す)\n\tbool find_GG(T x, T y, bool strict = true) {\n\t\tif (strict) {\n\t\t\tT y2 = x_to_y.upper_bound(x)->second;\n\t\t\treturn y2 != inf && y2 > y;\n\t\t}\n\t\telse {\n\t\t\tT y2 = x_to_y.lower_bound(x)->second;\n\t\t\treturn y2 != inf && y2 >= y;\n\t\t}\n\t}\n\n\t// x' >= x なる x 座標が最小の点 (x', y') を返す(なければ (inf, inf[ninf]))\n\tpair<T, T> lower_bound(T x) {\n\t\treturn *x_to_y.lower_bound(x);\n\t}\n\n\t// x' > x なる x 座標が最小の点 (x', y') を返す(なければ (inf, inf[ninf]))\n\tpair<T, T> upper_bound(T x) {\n\t\treturn *x_to_y.upper_bound(x);\n\t}\n\n\t// x' <= x なる x 座標が最大の点 (x', y') を返す(なければ (ninf, ninf[inf]))\n\tpair<T, T> lower_bound_rev(T x) {\n\t\treturn *prev(x_to_y.upper_bound(x));\n\t}\n\n\t// x' < x なる x 座標が最大の点 (x', y') を返す(なければ (ninf, ninf[inf]))\n\tpair<T, T> upper_bound_rev(T x) {\n\t\treturn *prev(x_to_y.lower_bound(x));\n\t}\n\n#ifdef _MSC_VER\n\tfriend ostream& operator<<(ostream& os, const Monotonous_points& mp) {\n\t\trepe(p, mp.x_to_y) if (abs(p.first) != mp.inf) os << p << \" \";\n\t\treturn os;\n\t}\n#endif\n}; \n\n\n//【めぐる式二分探索】O(log|ok - ng|)\n/*\n* 条件 okQ() を満たす要素 ok と満たさない要素 ng との境界を二分探索する.\n* 境界に隣り合うような条件を満たす要素(ok 側)の位置を返す.\n*/\ntemplate <typename T>\nT meguru_search(T ok, T ng, function<bool(T)>& okQ) {\n\t// 参考 : https://twitter.com/meguru_comp/status/697008509376835584\n\t// verify : https://onlinejudge.u-aizu.ac.jp/courses/lesson/1/ALDS1/all/ALDS1_4_D\n\n\t// 境界が決定するまで\n\twhile (abs(ok - ng) > 1) {\n\t\t// 区間の中間\n\t\tT mid = (ok + ng) / 2;\n\n\t\t// 中間が OK かどうかに応じて区間を縮小する.\n\t\tif (okQ(mid)) ok = mid;\n\t\telse ng = mid;\n\t}\n\treturn ok;\n\n\t/* okQ の定義の雛形\n\tusing T = ll;\n\tfunction<bool(T)> okQ = [&](T x) {\n\t\treturn true || false;\n\t};\n\t*/\n}\n\n\n//【最長増加部分列(二次元)】O(n (log n)^2)\n/*\n* 数列 a[0..n), b[0..n) について,添字の増加列 t_0 < ... < t_(k-1) で,\n*\t\t∀i∈[0..k-1), a[t_i] < a[t_(i+1)] かつ b[t_i] < b[t_(i+1)]\n* を満たすものの長さ k の最大値を返す.\n*\n* 利用:【狭義単調な点列】,【めぐる式二分探索】\n*/\ntemplate <class T>\nint longest_increasing_subsequence_2D(const vector<T>& a, const vector<T>& b) {\n\t// 参考 : https://topcoder-g-hatena-ne-jp.jag-icpc.org/skyaozora/20141216.html\n\t// verify : https://onlinejudge.u-aizu.ac.jp/problems/1341\n\n\tint n = sz(a);\n\n\t// ps[j] : その要素を末尾にもつ長さ j の増加部分列が存在するような点(ps[0] は使わない)\n\tvector<Monotonous_points<T>> ps(1);\n\n\trep(i, n) {\n\t\t// j : (a[i], b[i]) を末尾に持つ増加部分列の最大長\n\t\tfunction<bool(int)> okQ = [&](int j) { return ps[j].find_LL(a[i], b[i]); };\n\t\tint j = meguru_search(0, sz(ps), okQ) + 1;\n\n\t\t// 点 (a[i], b[i]) を挿入する.\n\t\tif (j >= sz(ps)) ps.push_back(Monotonous_points<T>(false));\n\t\tif (!ps[j].find_LL(a[i], b[i], false)) ps[j].insert(a[i], b[i]);\n\n\t\tdump(i); dump(ps);\n\t}\n\n\treturn sz(ps) - 1;\n}\n\n\nint a, b, C = ~(1 << 31), M = (1 << 16) - 1;\nint r() {\n\ta = 36969 * (a & M) + (a >> 16);\n\tb = 18000 * (b & M) + (b >> 16);\n\treturn (C & ((a << 16) + b)) % 1000000;\n}\n\n\nint solve(int N, vi x, vi y, vi z) {\n\tvector<tuple<int, int, int>> xyz(N);\n\trep(i, N) xyz[i] = { -x[i], y[i], z[i] };\n\tsort(all(xyz), greater<tuple<int, int, int>>());\n\n\trep(i, N) tie(x[i], y[i], z[i]) = xyz[i];\n\tdump(y); dump(z);\n\n\treturn longest_increasing_subsequence_2D(y, z);\n}\n\n\n//【最長パス】O(|V| + |E|)\n/*\n* DAG g の頂点 s からの最長パスの長さを len[s] に格納し len を返す.\n*/\nvi longest_path(const Graph& g) {\n\t// verify : https://atcoder.jp/contests/dp/tasks/dp_g\n\n\tint n = sz(g);\n\n\t// len[s] : 頂点 s からの最長パスの長さ\n\tvi len(n);\n\tvb seen(n);\n\n\tfunction<int(int)> dfs = [&](int s) {\n\t\tif (seen[s]) return len[s];\n\t\tseen[s] = true;\n\t\tlen[s] = 0;\n\n\t\t// s → t と進む場合\n\t\trepe(t, g[s]) chmax(len[s], dfs(t) + 1);\n\n\t\treturn len[s];\n\t};\n\n\t// 各頂点 s についての情報を計算する.\n\trep(s, n) if (!seen[s]) dfs(s);\n\n\treturn len;\n}\n\n\nint naive(int N, vi x, vi y, vi z) {\n\tGraph g(N);\n\n\trep(i, N) rep(j, N) {\n\t\tif (x[i] < x[j] && y[i] < y[j] && z[i] < z[j]) {\n\t\t\tg[i].push_back(j);\n\t\t}\n\t}\n\n\tauto len = longest_path(g);\n\n\treturn *max_element(all(len)) + 1;\n}\n\n\nvoid bug_find() {\n#ifdef _MSC_VER\n\t// 合わない入力例を見つける.\n\n\tmt19937_64 mt;\n\tmt.seed((int)time(NULL));\n\tuniform_int_distribution<ll> rnd(0LL, 1LL << 62);\n\n\tmute_dump = true;\n\n\trep(hoge, 1000) {\n\t\tint n = 50;\n\t\tvi x(n), y(n), z(n);\n\t\trep(i, n) {\n\t\t\tx[i] = rnd(mt) % 10;\n\t\t\ty[i] = rnd(mt) % 10;\n\t\t\tz[i] = rnd(mt) % 10;\n\t\t}\n\n\t\tauto res_naive = naive(n, x, y, z);\n\t\tauto res_solve = solve(n, x, y, z);\n\n\t\tif (res_naive != res_solve) {\n\t\t\tcout << \"----------error!----------\" << endl;\n\t\t\tcout << \"input:\" << endl;\n\t\t\tcout << x << endl;\n\t\t\tcout << y << endl;\n\t\t\tcout << z << endl;\n\t\t\tcout << \"results:\" << endl;\n\t\t\tcout << res_naive << endl;\n\t\t\tcout << res_solve << endl;\n\t\t\tcout << \"--------------------------\" << endl;\n\t\t}\n\t}\n\n\tmute_dump = false;\n\texit(0);\n#endif\n}\n/*\n----------error!----------\ninput:\n0 2 8 3 6\n4 2 2 0 0\n2 0 2 0 9\nresults:\n2\n1\n--------------------------\n*/\n\n\nint main() {\n\tinput_from_file(\"input.txt\");\n//\toutput_to_file(\"output.txt\");\n\n\tbug_find();\n\n\twhile (1) {\n\t\tint m, n;\n\t\tcin >> m >> n >> a >> b;\n\t\tdump(m, n, a, b);\n\n\t\tif (m == 0 && n == 0) return 0;\n\n\t\tvi x(m + n), y(m + n), z(m + n);\n\t\trep(i, m) cin >> x[i] >> y[i] >> z[i];\n\t\trep(i, n) { x[m + i] = r(); y[m + i] = r(); z[m + i] = r(); }\n\n\t\tdump(naive(m + n, x, y, z));\n\t\tcout << solve(m + n, x, y, z) << endl;\n\t}\n}", "accuracy": 1, "time_ms": 850, "memory_kb": 25092, "score_of_the_acc": -0.0911, "final_rank": 7 }, { "submission_id": "aoj_1341_7244624", "code_snippet": "#ifndef HIDDEN_IN_VS // 折りたたみ用\n\n// 警告の抑制\n#define _CRT_SECURE_NO_WARNINGS\n\n// ライブラリの読み込み\n#include <bits/stdc++.h>\nusing namespace std;\n\n// 型名の短縮\nusing ll = long long; // -2^63 ~ 2^63 = 9 * 10^18(int は -2^31 ~ 2^31 = 2 * 10^9)\nusing pii = pair<int, int>;\tusing pll = pair<ll, ll>;\tusing pil = pair<int, ll>;\tusing pli = pair<ll, int>;\nusing vi = vector<int>;\t\tusing vvi = vector<vi>;\t\tusing vvvi = vector<vvi>;\nusing vl = vector<ll>;\t\tusing vvl = vector<vl>;\t\tusing vvvl = vector<vvl>;\nusing vb = vector<bool>;\tusing vvb = vector<vb>;\t\tusing vvvb = vector<vvb>;\nusing vc = vector<char>;\tusing vvc = vector<vc>;\t\tusing vvvc = vector<vvc>;\nusing vd = vector<double>;\tusing vvd = vector<vd>;\t\tusing vvvd = vector<vvd>;\ntemplate <class T> using priority_queue_rev = priority_queue<T, vector<T>, greater<T>>;\nusing Graph = vvi;\n\n// 定数の定義\nconst double PI = acos(-1);\nconst vi DX = { 1, 0, -1, 0 }; // 4 近傍(下,右,上,左)\nconst vi DY = { 0, 1, 0, -1 };\nint INF = 1001001001; ll INFL = 4004004004004004004LL;\ndouble EPS = 1e-12;\n\n// 入出力高速化\nstruct fast_io { fast_io() { cin.tie(nullptr); ios::sync_with_stdio(false); cout << fixed << setprecision(18); } } fastIOtmp;\n\n// 汎用マクロの定義\n#define all(a) (a).begin(), (a).end()\n#define sz(x) ((int)(x).size())\n#define lbpos(a, x) (int)distance((a).begin(), std::lower_bound(all(a), x))\n#define ubpos(a, x) (int)distance((a).begin(), std::upper_bound(all(a), x))\n#define Yes(b) {cout << ((b) ? \"Yes\\n\" : \"No\\n\");}\n#define rep(i, n) for(int i = 0, i##_len = int(n); i < i##_len; ++i) // 0 から n-1 まで昇順\n#define repi(i, s, t) for(int i = int(s), i##_end = int(t); i <= i##_end; ++i) // s から t まで昇順\n#define repir(i, s, t) for(int i = int(s), i##_end = int(t); i >= i##_end; --i) // s から t まで降順\n#define repe(v, a) for(const auto& v : (a)) // a の全要素(変更不可能)\n#define repea(v, a) for(auto& v : (a)) // a の全要素(変更可能)\n#define repb(set, d) for(int set = 0; set < (1 << int(d)); ++set) // d ビット全探索(昇順)\n#define repp(a) sort(all(a)); for(bool a##_perm = true; a##_perm; a##_perm = next_permutation(all(a))) // a の順列全て(昇順)\n#define smod(n, m) ((((n) % (m)) + (m)) % (m)) // 非負mod\n#define uniq(a) {sort(all(a)); (a).erase(unique(all(a)), (a).end());} // 重複除去\n#define EXIT(a) {cout << (a) << endl; exit(0);} // 強制終了\n\n// 汎用関数の定義\ntemplate <class T> inline ll pow(T n, int k) { ll v = 1; rep(i, k) v *= n; return v; }\ntemplate <class T> inline bool chmax(T& M, const T& x) { if (M < x) { M = x; return true; } return false; } // 最大値を更新(更新されたら true を返す)\ntemplate <class T> inline bool chmin(T& m, const T& x) { if (m > x) { m = x; return true; } return false; } // 最小値を更新(更新されたら true を返す)\n\n// 演算子オーバーロード\ntemplate <class T, class U> inline istream& operator>>(istream& is, pair<T, U>& p) { is >> p.first >> p.second; return is; }\ntemplate <class T> inline istream& operator>>(istream& is, vector<T>& v) { repea(x, v) is >> x; return is; }\ntemplate <class T> inline vector<T>& operator--(vector<T>& v) { repea(x, v) --x; return v; }\ntemplate <class T> inline vector<T>& operator++(vector<T>& v) { repea(x, v) ++x; return v; }\n\n// 手元環境(Visual Studio)\n#ifdef _MSC_VER\n#include \"local.hpp\"\n// 提出用(gcc)\n#else\ninline int popcount(int n) { return __builtin_popcount(n); }\ninline int popcount(ll n) { return __builtin_popcountll(n); }\ninline int lsb(int n) { return n != 0 ? __builtin_ctz(n) : -1; }\ninline int lsb(ll n) { return n != 0 ? __builtin_ctzll(n) : -1; }\ninline int msb(int n) { return n != 0 ? (31 - __builtin_clz(n)) : -1; }\ninline int msb(ll n) { return n != 0 ? (63 - __builtin_clzll(n)) : -1; }\n#define gcd __gcd\n#define dump(...)\n#define dumpel(v)\n#define dump_list(v)\n#define dump_list2D(v)\n#define input_from_file(f)\n#define output_to_file(f)\n#define Assert(b) { if (!(b)) while (1) cout << \"OLE\"; }\n#endif\n\n#endif // 折りたたみ用\n\n\n////--------------AtCoder 専用--------------\n//#include <atcoder/all>\n//using namespace atcoder;\n//\n////using mint = modint1000000007;\n//using mint = modint998244353;\n////using mint = modint; // mint::set_mod(m);\n//\n//istream& operator>>(istream& is, mint& x) { ll x_; is >> x_; x = x_; return is; }\n//ostream& operator<<(ostream& os, const mint& x) { os << x.val(); return os; }\n//using vm = vector<mint>; using vvm = vector<vm>; using vvvm = vector<vvm>;\n////----------------------------------------\n\n\n//【めぐる式二分探索】O(log|ok - ng|)\n/*\n* 条件 okQ() を満たす要素 ok と満たさない要素 ng との境界を二分探索する.\n* 境界に隣り合うような条件を満たす要素(ok 側)の位置を返す.\n*/\ntemplate <typename T>\nT meguru_search(T ok, T ng, function<bool(T)>& okQ) {\n\t// 参考 : https://twitter.com/meguru_comp/status/697008509376835584\n\t// verify : https://onlinejudge.u-aizu.ac.jp/courses/lesson/1/ALDS1/all/ALDS1_4_D\n\n\t// 境界が決定するまで\n\twhile (abs(ok - ng) > 1) {\n\t\t// 区間の中間\n\t\tT mid = (ok + ng) / 2;\n\n\t\t// 中間が OK かどうかに応じて区間を縮小する.\n\t\tif (okQ(mid)) ok = mid;\n\t\telse ng = mid;\n\t}\n\treturn ok;\n\n\t/* okQ の定義の雛形\n\tusing T = ll;\n\tfunction<bool(T)> okQ = [&](T x) {\n\t\treturn true || false;\n\t};\n\t*/\n}\n\n\n//【狭義単調な点列】\n/*\n* Monotonous_points<T>() : O(1)\n*\t空で初期化する.\n* \n* void insert(T x, T y) : ならし O(log n)\n*\t点 (x, y) を挿入し,x <= x' かつ y <= y' なる点 (x', y') は全て削除する.\n* \n* bool find(T x, T y) : O(log n)\n*\tx' < x かつ y' < y なる点が存在するかを返す.\n*/\ntemplate <class T>\nclass Monotonous_points {\n\t// 参考 : https://topcoder-g-hatena-ne-jp.jag-icpc.org/skyaozora/20141216.html\n\n\t// x 座標狭義昇順に保持した点のリスト(y 座標は狭義降順)\n\tmap<T, T> x_to_y;\n\npublic:\n\t// 空で初期化\n\tMonotonous_points() {}\n\n\t// 点 (x, y) を挿入し,x <= x' かつ y <= y' なる点 (x', y') は全て削除する.\n\tvoid insert(T x, T y) {\n\t\t// x <= x' なる最小の x' を指すイテレータを得る.\n\t\tauto it = x_to_y.lower_bound(x);\n\n\t\t// x' から昇順に,y <= y' である限り要素を削除する.\n\t\twhile (it != x_to_y.end()) {\n\t\t\tif (it->second < y) break;\n\n\t\t\tit = x_to_y.erase(it);\n\t\t}\n\n\t\t// 点 (x, y) を挿入する.\n\t\tif (it == x_to_y.end() || it->first != x) x_to_y[x] = y;\n\t}\n\n\t// x' < x かつ y' < y なる点が存在するかを返す.\n\tbool find(T x, T y) {\n\t\t// x <= x' なる最小の x' を指すイテレータを得る.\n\t\tauto it = x_to_y.lower_bound(x);\n\n\t\t// その直前の要素が条件を満たさないなら他の要素もだめ.\n\t\treturn it != x_to_y.begin() && prev(it)->second < y;\n\t}\n\n#ifdef _MSC_VER\n\tfriend ostream& operator<<(ostream& os, const Monotonous_points& mp) {\n\t\trepe(p, mp.x_to_y) os << p << \" \";\n\t\treturn os;\n\t}\n#endif\n};\n\n\n//【最長増加部分列(二次元)】O(n (log n)^2)\n/*\n* 数列 a[0..n), b[0..n) について,添字の増加列 t_0 < ... < t_(k-1) で,\n*\t\t∀i∈[0..k-1), a[t_i] < a[t_(i+1)] かつ b[t_i] < b[t_(i+1)]\n* を満たすものの長さ k の最大値を返す.\n* \n* 利用:【狭義単調な点列】,【めぐる式二分探索】\n*/\ntemplate <class T>\nint longest_increasing_subsequence_2D(const vector<T>& a, const vector<T>& b) {\n\t// 参考 : https://topcoder-g-hatena-ne-jp.jag-icpc.org/skyaozora/20141216.html\n\n\tint n = sz(a);\n\n\t// ps[j] : その要素を末尾にもつ長さ j の増加部分列が存在するような点(ps[0] は使わない)\n\tvector<Monotonous_points<T>> ps(1);\n\n\trep(i, n) {\n\t\t// j : (a[i], b[i]) を末尾に持つ増加部分列の最大長\n\t\tfunction<bool(int)> okQ = [&](int j) { return ps[j].find(a[i], b[i]); };\n\t\tint j = meguru_search(0, sz(ps), okQ) + 1;\n\n\t\t// 点 (a[i], b[i]) を挿入する.\n\t\tif (j >= sz(ps)) ps.push_back(Monotonous_points<T>());\n\t\tps[j].insert(a[i], b[i]);\n\n\t\tdump(i); dump(ps);\n\t}\n\n\treturn sz(ps) - 1;\n}\n\n\nint a, b, C = ~(1 << 31), M = (1 << 16) - 1;\nint r() {\n\ta = 36969 * (a & M) + (a >> 16);\n\tb = 18000 * (b & M) + (b >> 16);\n\treturn (C & ((a << 16) + b)) % 1000000;\n}\n\n\nint solve(int N, vi x, vi y, vi z) {\n\tvector<tuple<int, int, int>> xyz(N);\n\trep(i, N) xyz[i] = { -x[i], y[i], z[i] };\n\tsort(all(xyz), greater<tuple<int, int, int>>());\n\n\trep(i, N) tie(x[i], y[i], z[i]) = xyz[i];\n\tdump(y); dump(z);\n\n\treturn longest_increasing_subsequence_2D(y, z);\n}\n\n\n//【最長パス】O(|V| + |E|)\n/*\n* DAG g の頂点 s からの最長パスの長さを len[s] に格納し len を返す.\n*/\nvi longest_path(const Graph& g) {\n\t// verify : https://atcoder.jp/contests/dp/tasks/dp_g\n\n\tint n = sz(g);\n\n\t// len[s] : 頂点 s からの最長パスの長さ\n\tvi len(n);\n\tvb seen(n);\n\n\tfunction<int(int)> dfs = [&](int s) {\n\t\tif (seen[s]) return len[s];\n\t\tseen[s] = true;\n\t\tlen[s] = 0;\n\n\t\t// s → t と進む場合\n\t\trepe(t, g[s]) chmax(len[s], dfs(t) + 1);\n\n\t\treturn len[s];\n\t};\n\n\t// 各頂点 s についての情報を計算する.\n\trep(s, n) if (!seen[s]) dfs(s);\n\n\treturn len;\n}\n\n\nint naive(int N, vi x, vi y, vi z) {\n\tGraph g(N);\n\n\trep(i, N) rep(j, N) {\n\t\tif (x[i] < x[j] && y[i] < y[j] && z[i] < z[j]) {\n\t\t\tg[i].push_back(j);\n\t\t}\n\t}\n\n\tauto len = longest_path(g);\n\n\treturn *max_element(all(len)) + 1;\n}\n\n\nvoid bug_find() {\n#ifdef _MSC_VER\n\t// 合わない入力例を見つける.\n\n\tmt19937_64 mt;\n\tmt.seed((int)time(NULL));\n\tuniform_int_distribution<ll> rnd(0LL, 1LL << 62);\n\n\tmute_dump = true;\n\n\trep(hoge, 1000) {\n\t\tint n = 50;\n\t\tvi x(n), y(n), z(n);\n\t\trep(i, n) {\n\t\t\tx[i] = rnd(mt) % 10;\n\t\t\ty[i] = rnd(mt) % 10;\n\t\t\tz[i] = rnd(mt) % 10;\n\t\t}\n\n\t\tauto res_naive = naive(n, x, y, z);\n\t\tauto res_solve = solve(n, x, y, z);\n\n\t\tif (res_naive != res_solve) {\n\t\t\tcout << \"----------error!----------\" << endl;\n\t\t\tcout << \"input:\" << endl;\n\t\t\tcout << x << endl;\n\t\t\tcout << y << endl;\n\t\t\tcout << z << endl;\n\t\t\tcout << \"results:\" << endl;\n\t\t\tcout << res_naive << endl;\n\t\t\tcout << res_solve << endl;\n\t\t\tcout << \"--------------------------\" << endl;\n\t\t}\n\t}\n\n\tmute_dump = false;\n\texit(0);\n#endif\n}\n/*\n----------error!----------\ninput:\n0 2 8 3 6\n4 2 2 0 0\n2 0 2 0 9\nresults:\n2\n1\n--------------------------\n*/\n\n\nint main() {\n\tinput_from_file(\"input.txt\");\n//\toutput_to_file(\"output.txt\");\n\n\tbug_find();\n\n\twhile (1) {\n\t\tint m, n;\n\t\tcin >> m >> n >> a >> b;\n\t\tdump(m, n, a, b);\n\n\t\tif (m == 0 && n == 0) return 0;\n\n\t\tvi x(m + n), y(m + n), z(m + n);\n\t\trep(i, m) cin >> x[i] >> y[i] >> z[i];\n\t\trep(i, n) { x[m + i] = r(); y[m + i] = r(); z[m + i] = r(); }\n\n\t\tdump(naive(m + n, x, y, z));\n\t\tcout << solve(m + n, x, y, z) << endl;\n\t}\n}", "accuracy": 1, "time_ms": 720, "memory_kb": 19752, "score_of_the_acc": -0.0379, "final_rank": 5 }, { "submission_id": "aoj_1341_7244186", "code_snippet": "#include \"bits/stdc++.h\"\nusing namespace std;\nusing namespace std::chrono;\ntypedef long long int llint;\ntypedef long double lldo;\n#define mp make_pair\n#define mt make_tuple\n#define pub push_back\n#define puf push_front\n#define pob pop_back\n#define pof pop_front\n#define fir first\n#define sec second\n#define res resize\n#define ins insert\n#define era erase\n#define REP(i, n) for(int i = 0;i < (n);i++)\n/*cout<<fixed<<setprecision(20);cin.tie(0);ios::sync_with_stdio(false);*/\nconst int mod=1000000007;\nconst llint inf=2.19e15+1;\nconst long double pai=3.141592653589793238462643383279502884197;\nconst long double eps=1e-10;\ntemplate <class T,class U>bool chmin(T& a,U b){if(a>b){a=b;return true;}return false;}\ntemplate <class T,class U>bool chmax(T& a,U b){if(a<b){a=b;return true;}return false;}\nllint gcd(llint a,llint b){if(a%b==0){return b;}else return gcd(b,a%b);}\nllint lcm(llint a,llint b){if(a==0){return b;}return a/gcd(a,b)*b;}\ntemplate<class T> void SO(T& ve){sort(ve.begin(),ve.end());}\ntemplate<class T> void REV(T& ve){reverse(ve.begin(),ve.end());}\ntemplate<class T>int LBI(const vector<T>&ar,T in){return lower_bound(ar.begin(),ar.end(),in)-ar.begin();}\ntemplate<class T>int UBI(const vector<T>&ar,T in){return upper_bound(ar.begin(),ar.end(),in)-ar.begin();}\n\ntemplate<class T> using V=vector<T>;\ntemplate<class T> using VV=vector<V<T>>;\ntemplate<class T>struct Fenwick{\n\tint n;\n\tV<T> seg;\n\tFenwick(int _n=0):n(_n),seg(n+1){}\n\tvoid add(int i,int x){\n\t\ti++;\n\t\twhile(i<=n){\n\t\t\tchmax(seg[i],x);\n\t\t\ti+=i&-i;\n\t\t}\n\t}\n\tT max(int i){\n\t\tT s=0;\n\t\twhile(i>0){\n\t\t\tchmax(s,seg[i]);\n\t\t\ti-=i&-i;\n\t\t}\n\t\treturn s;\n\t}\n};\n\ntemplate<class D,class I>struct Fenwick2D{\n\tusing P=pair<I,I>;\n\tV<P>points;\n\tVV<I>ys;\n\tV<Fenwick<D>>fws;\n\tint lg,sz;\n\tFenwick2D(V<P> _points):points(_points){\n\t\tSO(points);\n\t\tpoints.erase(unique(points.begin(),points.end()),points.end());\n\t\tint n=int(points.size());\n\t\tlg=1;\n\t\twhile((1<<lg)<n){lg++;}\n\t\tsz=1<<lg;\n\t\tys=VV<I>(2*sz);\n\t\tfor(int i=0;i<n;i++){\n\t\t\tys[sz+i].pub(points[i].sec);\n\t\t}\n\t\tfor(int i=sz-1;i>=1;i--){\n\t\t\tys[i]=V<I>(ys[i+i].size()+ys[i+i+1].size());\n\t\t\tmerge(ys[i+i].begin(),ys[i+i].end(),ys[i+i+1].begin(),ys[i+i+1].end(),ys[i].begin());\n\t\t}\n\t\tfws=V<Fenwick<D>>(sz+sz);\n\t\tfor(int i=1;i<2*sz;i++){\n\t\t\tfws[i]=Fenwick<D>(int(ys[i].size()));\n\t\t}\n\t}\n\tvoid add(P p,D x){\n\t\tint k=LBI(points,p);\n\t\tk+=sz;\n\t\twhile(k){\n\t\t\tint yid=LBI(ys[k],p.sec);\n\t\t\tfws[k].add(yid,x);\n\t\t\tk>>=1;\n\t\t}\n\t}\n\tD getmax(int a,int b,I up,int l,int r,int k){\n\t\tif(b<=l||r<=a){return 0;}\n\t\tif(a<=l&&r<=b){\n\t\t\tint uid=LBI(ys[k],up);\n\t\t\treturn fws[k].max(uid);\n\t\t}\n\t\tint mid=(l+r)/2;\n\t\treturn max(getmax(a,b,up,l,mid,2*k),getmax(a,b,up,mid,r,2*k+1));\n\t}\n\tD getmax(P up){\n\t\tint b=LBI(points,mp(up.fir,-mod));\n\t\treturn getmax(0,b,up.sec,0,sz,1);\n\t}\n};\nint a, b , C = ~(1<<31), M = (1<<16)-1;\nint rrr() {\n a = 36969 * (a & M) + (a >> 16);\n b = 18000 * (b & M) + (b >> 16);\n return (C & ((a << 16) + b)) % 1000000;\n}\nint main(void){\n\twhile(-1){\n\t\tint m,n,A,B,i;cin>>m>>n>>A>>B;\n\t\tif(m+n==0){return 0;}\n\t\tvector<tuple<int,int,int>>xyz(n+m);\n\t\ta=A;b=B;\n\t\tfor(i=0;i<m;i++){\n\t\t\tint x,y,z;cin>>x>>y>>z;\n\t\t\txyz[i]=mt(x,-y,-z);\n\t\t}\n\t\tfor(i=0;i<n;i++){\n\t\t\tint x,y,z;x=rrr();y=rrr();z=rrr();\n\t\t\txyz[m+i]=mt(x,-y,-z);\n\t\t}\n\t\tSO(xyz);\n\t\tvector<pair<int,int>>yz(n+m);\n\t\tfor(i=0;i<n+m;i++){\n\t\t\tyz[i]=mp(-get<1>(xyz[i]),-get<2>(xyz[i]));\n\t\t}\n\t\tFenwick2D<int,int> segki(yz);\n\t\tint ans=0;\n\t\tfor(i=0;i<n+m;i++){\n\t\t\tint kaz=segki.getmax(yz[i])+1;\n\t\t\tchmax(ans,kaz);\n\t\t\t\n\t\t\tsegki.add(yz[i],kaz);\n\t\t}\n\t\tcout<<ans<<endl;\n\t}\n\treturn 0;\n}", "accuracy": 1, "time_ms": 5600, "memory_kb": 160288, "score_of_the_acc": -1.6482, "final_rank": 19 }, { "submission_id": "aoj_1341_6813635", "code_snippet": "#pragma region Macros\n#if defined(noimi) && defined(_GLIBCXX_DEBUG) && defined(_GLIBCXX_DEBUG_PEDANTIC)\n// #pragma comment(linker, \"/stack:200000000\")\n#include <stdc++.h>\n#pragma GCC optimize(\"O3\")\n#else\n#pragma GCC optimize(\"Ofast\")\n// #pragma GCC optimize(\"unroll-loops\")\n// #pragma GCC target(\"popcnt\")\n// #pragma GCC target(\"sse,sse2,sse3,ssse3,sse4,popcnt,fma,abm,mmx,avx,avx2,tune=native\")\n// #pragma GCC target(\"sse,sse2,sse3,ssse3,sse4,popcnt,fma,abm,mmx,avx,avx2\")\n// #pragma GCC target(\"avx2\")\n// #include <bits/stdc++.h>\n#include <bits/stdc++.h>\n#endif\n#include <cstdint>\n#include <immintrin.h>\n#include <variant>\n\n#ifdef noimi\n#define oj_local(a, b) b\n#else\n#define oj_local(a, b) a\n#endif\n\n#define LOCAL if(oj_local(0, 1))\n#define OJ if(oj_local(1, 0))\n\nusing namespace std;\nusing ll = long long;\nusing ull = unsigned long long int;\nusing i128 = __int128_t;\nusing pii = pair<int, int>;\nusing pll = pair<ll, ll>;\nusing ld = long double;\ntemplate <typename T> using vc = vector<T>;\ntemplate <typename T> using vvc = vector<vc<T>>;\ntemplate <typename T> using vvvc = vector<vvc<T>>;\nusing vi = vc<int>;\nusing vl = vc<ll>;\nusing vpi = vc<pii>;\nusing vpl = vc<pll>;\ntemplate <class T> using pq = priority_queue<T>;\ntemplate <class T> using pqg = priority_queue<T, vector<T>, greater<T>>;\ntemplate <typename T> int si(const T &x) { return x.size(); }\ntemplate <class T, class S> inline bool chmax(T &a, const S &b) { return (a < b ? a = b, 1 : 0); }\ntemplate <class T, class S> inline bool chmin(T &a, const S &b) { return (a > b ? a = b, 1 : 0); }\nvi iota(int n) {\n vi a(n);\n return iota(a.begin(), a.end(), 0), a;\n}\ntemplate <typename T> vi iota(const vector<T> &a, bool greater = false) {\n vi res(a.size());\n iota(res.begin(), res.end(), 0);\n sort(res.begin(), res.end(), [&](int i, int j) {\n if(greater) return a[i] > a[j];\n return a[i] < a[j];\n });\n return res;\n}\n\n// macros\n#define overload5(a, b, c, d, e, name, ...) name\n#define overload4(a, b, c, d, name, ...) name\n#define endl '\\n'\n#define REP0(n) for(ll jidlsjf = 0; jidlsjf < n; ++jidlsjf)\n#define REP1(i, n) for(ll i = 0; i < (n); ++i)\n#define REP2(i, a, b) for(ll i = (a); i < (b); ++i)\n#define REP3(i, a, b, c) for(ll i = (a); i < (b); i += (c))\n#define rep(...) overload4(__VA_ARGS__, REP3, REP2, REP1, REP0)(__VA_ARGS__)\n#define per0(n) for(int jidlsjf = 0; jidlsjf < (n); ++jidlsjf)\n#define per1(i, n) for(ll i = (n)-1; i >= 0; --i)\n#define per2(i, a, b) for(ll i = (a)-1; i >= b; --i)\n#define per3(i, a, b, c) for(ll i = (a)-1; i >= (b); i -= (c))\n#define per(...) overload4(__VA_ARGS__, per3, per2, per1, per0)(__VA_ARGS__)\n#define fore0(a) rep(a.size())\n#define fore1(i, a) for(auto &&i : a)\n#define fore2(a, b, v) for(auto &&[a, b] : v)\n#define fore3(a, b, c, v) for(auto &&[a, b, c] : v)\n#define fore4(a, b, c, d, v) for(auto &&[a, b, c, d] : v)\n#define fore(...) overload5(__VA_ARGS__, fore4, fore3, fore2, fore1, fore0)(__VA_ARGS__)\n#define fi first\n#define se second\n#define pb push_back\n#define ppb pop_back\n#define ppf pop_front\n#define eb emplace_back\n#define drop(s) cout << #s << endl, exit(0)\n#define si(c) (int)(c).size()\n#define lb(c, x) distance((c).begin(), lower_bound(all(c), (x)))\n#define ub(c, x) distance((c).begin(), upper_bound(all(c), (x)))\n#define rng(v, l, r) v.begin() + l, v.begin() + r\n#define all(c) begin(c), end(c)\n#define rall(c) rbegin(c), rend(c)\n#define SORT(v) sort(all(v))\n#define REV(v) reverse(all(v))\n#define UNIQUE(x) SORT(x), x.erase(unique(all(x)), x.end())\ntemplate <typename T = ll, typename S> T SUM(const S &v) { return accumulate(all(v), T(0)); }\n#define MIN(v) *min_element(all(v))\n#define MAX(v) *max_element(all(v))\n#define overload2(_1, _2, name, ...) name\n#define vec(type, name, ...) vector<type> name(__VA_ARGS__)\n#define vv(type, name, h, ...) vector<vector<type>> name(h, vector<type>(__VA_ARGS__))\n#define vvv(type, name, h, w, ...) vector<vector<vector<type>>> name(h, vector<vector<type>>(w, vector<type>(__VA_ARGS__)))\n#define vvvv(type, name, a, b, c, ...) \\\n vector<vector<vector<vector<type>>>> name(a, vector<vector<vector<type>>>(b, vector<vector<type>>(c, vector<type>(__VA_ARGS__))))\nconstexpr pii dx4[4] = {pii{1, 0}, pii{0, 1}, pii{-1, 0}, pii{0, -1}};\nconstexpr pii dx8[8] = {{1, 0}, {1, 1}, {0, 1}, {-1, 1}, {-1, 0}, {-1, -1}, {0, -1}, {1, -1}};\n\nnamespace yesno_impl {\nconst string YESNO[2] = {\"NO\", \"YES\"};\nconst string YesNo[2] = {\"No\", \"Yes\"};\nconst string yesno[2] = {\"no\", \"yes\"};\nconst string firstsecond[2] = {\"second\", \"first\"};\nconst string FirstSecond[2] = {\"Second\", \"First\"};\nconst string possiblestr[2] = {\"impossible\", \"possible\"};\nvoid YES(bool t = 1) { cout << YESNO[t] << endl; }\nvoid NO(bool t = 1) { YES(!t); }\nvoid Yes(bool t = 1) { cout << YesNo[t] << endl; }\nvoid No(bool t = 1) { Yes(!t); }\nvoid yes(bool t = 1) { cout << yesno[t] << endl; }\nvoid no(bool t = 1) { yes(!t); }\nvoid first(bool t = 1) { cout << firstsecond[t] << endl; }\nvoid First(bool t = 1) { cout << FirstSecond[t] << endl; }\nvoid possible(bool t = 1) { cout << possiblestr[t] << endl; }\n}; // namespace yesno_impl\nusing namespace yesno_impl;\n\n#define INT(...) \\\n int __VA_ARGS__; \\\n IN(__VA_ARGS__)\n#define LL(...) \\\n ll __VA_ARGS__; \\\n IN(__VA_ARGS__)\n#define STR(...) \\\n string __VA_ARGS__; \\\n IN(__VA_ARGS__)\n#define CHR(...) \\\n char __VA_ARGS__; \\\n IN(__VA_ARGS__)\n#define DBL(...) \\\n double __VA_ARGS__; \\\n IN(__VA_ARGS__)\n#define VEC(type, name, size) \\\n vector<type> name(size); \\\n IN(name)\n#define VEC2(type, name1, name2, size) \\\n vector<type> name1(size), name2(size); \\\n for(int i = 0; i < size; i++) IN(name1[i], name2[i])\n#define VEC3(type, name1, name2, name3, size) \\\n vector<type> name1(size), name2(size), name3(size); \\\n for(int i = 0; i < size; i++) IN(name1[i], name2[i], name3[i])\n#define VEC4(type, name1, name2, name3, name4, size) \\\n vector<type> name1(size), name2(size), name3(size), name4(size); \\\n for(int i = 0; i < size; i++) IN(name1[i], name2[i], name3[i], name4[i]);\n#define VV(type, name, h, w) \\\n vector<vector<type>> name(h, vector<type>(w)); \\\n IN(name)\nint scan() { return getchar(); }\nvoid scan(int &a) { cin >> a; }\nvoid scan(long long &a) { cin >> a; }\nvoid scan(char &a) { cin >> a; }\nvoid scan(double &a) { cin >> a; }\nvoid scan(string &a) { cin >> a; }\ntemplate <class T, class S> void scan(pair<T, S> &p) { scan(p.first), scan(p.second); }\ntemplate <class T> void scan(vector<T> &);\ntemplate <class T> void scan(vector<T> &a) {\n for(auto &i : a) scan(i);\n}\ntemplate <class T> void scan(T &a) { cin >> a; }\nvoid IN() {}\ntemplate <class Head, class... Tail> void IN(Head &head, Tail &...tail) {\n scan(head);\n IN(tail...);\n}\n\ntemplate <typename T, typename S> T ceil(T x, S y) {\n assert(y);\n return (y < 0 ? ceil(-x, -y) : (x > 0 ? (x + y - 1) / y : x / y));\n}\n\ntemplate <typename T, typename S> T floor(T x, S y) {\n assert(y);\n return (y < 0 ? floor(-x, -y) : (x > 0 ? x / y : x / y - (x % y == 0 ? 0 : 1)));\n}\ntemplate <class T> T POW(T x, int n) {\n T res = 1;\n for(; n; n >>= 1, x *= x)\n if(n & 1) res *= x;\n return res;\n}\ntemplate <class T, class S> T POW(T x, S n, const ll &mod) {\n T res = 1;\n x %= mod;\n for(; n; n >>= 1, x = x * x % mod)\n if(n & 1) res = res * x % mod;\n return res;\n}\nvector<pll> factor(ll x) {\n vector<pll> ans;\n for(ll i = 2; i * i <= x; i++)\n if(x % i == 0) {\n ans.push_back({i, 1});\n while((x /= i) % i == 0) ans.back().second++;\n }\n if(x != 1) ans.push_back({x, 1});\n return ans;\n}\ntemplate <class T> vector<T> divisor(T x) {\n vector<T> ans;\n for(T i = 1; i * i <= x; i++)\n if(x % i == 0) {\n ans.pb(i);\n if(i * i != x) ans.pb(x / i);\n }\n return ans;\n}\ntemplate <typename T> void zip(vector<T> &x) {\n vector<T> y = x;\n UNIQUE(y);\n for(int i = 0; i < x.size(); ++i) { x[i] = lb(y, x[i]); }\n}\ntemplate <class S> void fold_in(vector<S> &v) {}\ntemplate <typename Head, typename... Tail, class S> void fold_in(vector<S> &v, Head &&a, Tail &&...tail) {\n for(auto e : a) v.emplace_back(e);\n fold_in(v, tail...);\n}\ntemplate <class S> void renumber(vector<S> &v) {}\ntemplate <typename Head, typename... Tail, class S> void renumber(vector<S> &v, Head &&a, Tail &&...tail) {\n for(auto &&e : a) e = lb(v, e);\n renumber(v, tail...);\n}\ntemplate <class S, class... Args> vector<S> zip(vector<S> &head, Args &&...args) {\n vector<S> v;\n fold_in(v, head, args...);\n sort(all(v)), v.erase(unique(all(v)), v.end());\n renumber(v, head, args...);\n return v;\n}\ntemplate <typename T> vector<T> RUI(const vector<T> &v) {\n vector<T> res(v.size() + 1);\n for(int i = 0; i < v.size(); i++) res[i + 1] = res[i] + v[i];\n return res;\n}\n// 反時計周りに 90 度回転\ntemplate <typename T> void rot(vector<vector<T>> &v) {\n if(empty(v)) return;\n int n = v.size(), m = v[0].size();\n vector res(m, vector<T>(n));\n rep(i, n) rep(j, m) res[m - 1 - j][i] = v[i][j];\n v.swap(res);\n}\n// x in [l, r)\ntemplate <class T, class S> bool inc(const T &x, const S &l, const S &r) { return l <= x and x < r; }\n\n// 便利関数\nconstexpr ll ten(int n) { return n == 0 ? 1 : ten(n - 1) * 10; }\nconstexpr ll tri(ll n) { return n * (n + 1) / 2; }\n// l + ... + r\nconstexpr ll tri(ll l, ll r) { return (l + r) * (r - l + 1) / 2; }\nll max(int x, ll y) { return max((ll)x, y); }\nll max(ll x, int y) { return max(x, (ll)y); }\nint min(int x, ll y) { return min((ll)x, y); }\nint min(ll x, int y) { return min(x, (ll)y); }\n// bit 演算系\nll pow2(int i) { return 1LL << i; }\nint topbit(signed t) { return t == 0 ? -1 : 31 - __builtin_clz(t); }\nint topbit(ll t) { return t == 0 ? -1 : 63 - __builtin_clzll(t); }\nint lowbit(signed a) { return a == 0 ? 32 : __builtin_ctz(a); }\nint lowbit(ll a) { return a == 0 ? 64 : __builtin_ctzll(a); }\n// int allbit(int n) { return (1 << n) - 1; }\nconstexpr ll mask(int n) { return (1LL << n) - 1; }\n// int popcount(signed t) { return __builtin_popcount(t); }\n// int popcount(ll t) { return __builtin_popcountll(t); }\nint popcount(uint64_t t) { return __builtin_popcountll(t); }\nstatic inline uint64_t popcount64(uint64_t x) {\n uint64_t m1 = 0x5555555555555555ll;\n uint64_t m2 = 0x3333333333333333ll;\n uint64_t m4 = 0x0F0F0F0F0F0F0F0Fll;\n uint64_t h01 = 0x0101010101010101ll;\n\n x -= (x >> 1) & m1;\n x = (x & m2) + ((x >> 2) & m2);\n x = (x + (x >> 4)) & m4;\n\n return (x * h01) >> 56;\n}\nbool ispow2(int i) { return i && (i & -i) == i; }\n\nll rnd(ll l, ll r) { //[l, r)\n#ifdef noimi\n static mt19937_64 gen;\n#else\n static mt19937_64 gen(chrono::steady_clock::now().time_since_epoch().count());\n#endif\n return uniform_int_distribution<ll>(l, r - 1)(gen);\n}\nll rnd(ll n) { return rnd(0, n); }\n\ntemplate <class t> void random_shuffle(vc<t> &a) { rep(i, si(a)) swap(a[i], a[rnd(0, i + 1)]); }\n\nint in() {\n int x;\n cin >> x;\n return x;\n}\nll lin() {\n unsigned long long x;\n cin >> x;\n return x;\n}\n\ntemplate <class T, class S> pair<T, S> operator-(const pair<T, S> &x) { return pair<T, S>(-x.first, -x.second); }\ntemplate <class T, class S> pair<T, S> operator-(const pair<T, S> &x, const pair<T, S> &y) { return pair<T, S>(x.fi - y.fi, x.se - y.se); }\ntemplate <class T, class S> pair<T, S> operator+(const pair<T, S> &x, const pair<T, S> &y) { return pair<T, S>(x.fi + y.fi, x.se + y.se); }\ntemplate <class T> pair<T, T> operator&(const pair<T, T> &l, const pair<T, T> &r) { return pair<T, T>(max(l.fi, r.fi), min(l.se, r.se)); }\ntemplate <class T, class S> pair<T, S> operator+=(pair<T, S> &l, const pair<T, S> &r) { return l = l + r; }\ntemplate <class T, class S> pair<T, S> operator-=(pair<T, S> &l, const pair<T, S> &r) { return l = l - r; }\ntemplate <class T> bool intersect(const pair<T, T> &l, const pair<T, T> &r) { return (l.se < r.se ? r.fi < l.se : l.fi < r.se); }\n\ntemplate <typename T> struct edge {\n int from, to;\n T cost;\n int id;\n edge(int to, T cost) : from(-1), to(to), cost(cost) {}\n edge(int from, int to, T cost) : from(from), to(to), cost(cost) {}\n edge(int from, int to, T cost, int id) : from(from), to(to), cost(cost), id(id) {}\n constexpr bool operator<(const edge<T> &rhs) const noexcept { return cost < rhs.cost; }\n edge &operator=(const int &x) {\n to = x;\n return *this;\n }\n operator int() const { return to; }\n friend ostream operator<<(ostream &os, edge &e) { return os << e.to; }\n};\ntemplate <typename T> using Edges = vector<edge<T>>;\n\nusing Tree = vector<vector<int>>;\nusing Graph = vector<vector<int>>;\ntemplate <class T> using Wgraph = vector<vector<edge<T>>>;\nGraph getG(int n, int m = -1, bool directed = false, int margin = 1) {\n Tree res(n);\n if(m == -1) m = n - 1;\n while(m--) {\n int a, b;\n cin >> a >> b;\n a -= margin, b -= margin;\n res[a].emplace_back(b);\n if(!directed) res[b].emplace_back(a);\n }\n return res;\n}\nGraph getTreeFromPar(int n, int margin = 1) {\n Graph res(n);\n for(int i = 1; i < n; i++) {\n int a;\n cin >> a;\n res[a - margin].emplace_back(i);\n }\n return res;\n}\ntemplate <class T> Wgraph<T> getWg(int n, int m = -1, bool directed = false, int margin = 1) {\n Wgraph<T> res(n);\n if(m == -1) m = n - 1;\n while(m--) {\n int a, b;\n T c;\n scan(a), scan(b), scan(c);\n a -= margin, b -= margin;\n res[a].emplace_back(b, c);\n if(!directed) res[b].emplace_back(a, c);\n }\n return res;\n}\nvoid add(Graph &G, int x, int y) { G[x].eb(y), G[y].eb(x); }\ntemplate <class S, class T> void add(Wgraph<S> &G, int x, int y, T c) { G[x].eb(y, c), G[y].eb(x, c); }\n\n#define TEST \\\n INT(testcases); \\\n while(testcases--)\n\nistream &operator>>(istream &is, i128 &v) {\n string s;\n is >> s;\n v = 0;\n for(int i = 0; i < (int)s.size(); i++) {\n if(isdigit(s[i])) { v = v * 10 + s[i] - '0'; }\n }\n if(s[0] == '-') { v *= -1; }\n return is;\n}\n\nostream &operator<<(ostream &os, const i128 &v) {\n if(v == 0) { return (os << \"0\"); }\n i128 num = v;\n if(v < 0) {\n os << '-';\n num = -num;\n }\n string s;\n for(; num > 0; num /= 10) { s.push_back((char)(num % 10) + '0'); }\n reverse(s.begin(), s.end());\n return (os << s);\n}\nnamespace aux {\ntemplate <typename T, unsigned N, unsigned L> struct tp {\n static void output(std::ostream &os, const T &v) {\n os << std::get<N>(v) << (&os == &cerr ? \", \" : \" \");\n tp<T, N + 1, L>::output(os, v);\n }\n};\ntemplate <typename T, unsigned N> struct tp<T, N, N> {\n static void output(std::ostream &os, const T &v) { os << std::get<N>(v); }\n};\n} // namespace aux\ntemplate <typename... Ts> std::ostream &operator<<(std::ostream &os, const std::tuple<Ts...> &t) {\n if(&os == &cerr) { os << '('; }\n aux::tp<std::tuple<Ts...>, 0, sizeof...(Ts) - 1>::output(os, t);\n if(&os == &cerr) { os << ')'; }\n return os;\n}\ntemplate <class T, class S> ostream &operator<<(ostream &os, const pair<T, S> &p) {\n if(&os == &cerr) { return os << \"(\" << p.first << \", \" << p.second << \")\"; }\n return os << p.first << \" \" << p.second;\n}\ntemplate <class Ch, class Tr, class Container> std::basic_ostream<Ch, Tr> &operator<<(std::basic_ostream<Ch, Tr> &os, const Container &x) {\n bool f = true;\n if(&os == &cerr) os << \"[\";\n for(auto &y : x) {\n if(&os == &cerr)\n os << (f ? \"\" : \", \") << y;\n else\n os << (f ? \"\" : \" \") << y;\n f = false;\n }\n if(&os == &cerr) os << \"]\";\n return os;\n}\n\n#ifdef noimi\n#undef endl\nvoid debug() { cerr << endl; }\nvoid debug(bool) { cerr << endl; }\ntemplate <class Head, class... Tail> void debug(bool is_front, Head head, Tail... tail) {\n if(!is_front) cerr << \", \";\n cerr << head;\n debug(false, tail...);\n}\n\n#define dump(args...) \\\n { \\\n vector<string> _debug = _split(#args, ','); \\\n err(true, begin(_debug), args); \\\n }\n\nvector<string> _split(const string &s, char c) {\n vector<string> v;\n stringstream ss(s);\n string x;\n while(getline(ss, x, c)) {\n if(empty(v))\n v.eb(x);\n else {\n bool flag = false;\n for(auto [c, d] : {pair('(', ')'), pair('[', ']'), pair('{', '}')}) {\n if(count(all(v.back()), c) != count(all(v.back()), d)) flag = true;\n }\n if(flag)\n v.back() += \",\" + x;\n else\n v.eb(x);\n }\n }\n return move(v);\n}\n\nvoid err(bool, vector<string>::iterator) { cerr << endl; }\ntemplate <typename T, typename... Args> void err(bool is_front, vector<string>::iterator it, T a, Args... args) {\n if(!is_front) cerr << \", \";\n cerr << it->substr((*it)[0] == ' ', (*it).size()) << \" = \" << a, err(false, ++it, args...);\n}\n\n// #define dump(...) cerr << #__VA_ARGS__ << \" : \", debug(true, __VA_ARGS__)\n#else\n#define dump(...) static_cast<void>(0)\n#define dbg(...) static_cast<void>(0)\n#endif\nvoid OUT() { cout << endl; }\ntemplate <class Head, class... Tail> void OUT(const Head &head, const Tail &...tail) {\n cout << head;\n if(sizeof...(tail)) cout << ' ';\n OUT(tail...);\n}\n\ntemplate <typename T> static constexpr T inf = numeric_limits<T>::max() / 2;\ntemplate <class T, class S> constexpr pair<T, S> inf<pair<T, S>> = {inf<T>, inf<S>};\n\ntemplate <class F> struct REC {\n F f;\n REC(F &&f_) : f(std::forward<F>(f_)) {}\n template <class... Args> auto operator()(Args &&...args) const { return f(*this, std::forward<Args>(args)...); }\n};\n\ntemplate <class S> vector<pair<S, int>> runLength(const vector<S> &v) {\n vector<pair<S, int>> res;\n for(auto &e : v) {\n if(res.empty() or res.back().fi != e)\n res.eb(e, 1);\n else\n res.back().se++;\n }\n return res;\n}\nvector<pair<char, int>> runLength(const string &v) {\n vector<pair<char, int>> res;\n for(auto &e : v) {\n if(res.empty() or res.back().fi != e)\n res.eb(e, 1);\n else\n res.back().se++;\n }\n return res;\n}\n\nint toint(const char &c, const char start = 'a') { return c - start; }\nint toint(const char &c, const string &chars) { return find(all(chars), c) - begin(chars); }\nint alphabets_to_int(const char &c) { return (islower(c) ? c - 'a' : c - 'A' + 26); }\ntemplate <typename T> auto toint(const T &v, const char &start = 'a') {\n vector<decltype(toint(v[0]))> ret;\n ret.reserve(v.size());\n for(auto &&e : v) ret.emplace_back(toint(e, start));\n return ret;\n}\ntemplate <typename T> auto toint(const T &v, const string &start) {\n vector<decltype(toint(v[0]))> ret;\n ret.reserve(v.size());\n for(auto &&e : v) ret.emplace_back(toint(e, start));\n return ret;\n}\n// a -> 0, A -> 26\ntemplate <typename T> auto alphabets_to_int(const T &s) {\n vector<decltype(alphabets_to_int(s[0]))> res;\n res.reserve(s.size());\n for(auto &&e : s) { res.emplace_back(alphabets_to_int(e)); }\n return res;\n}\n\ntemplate <class T, class F> T bin_search(T ok, T ng, const F &f) {\n while(abs(ok - ng) > 1) {\n T mid = ok + ng >> 1;\n (f(mid) ? ok : ng) = mid;\n }\n return ok;\n}\ntemplate <class T, class F> T bin_search_double(T ok, T ng, const F &f, int iter = 80) {\n while(iter--) {\n T mid = (ok + ng) / 2;\n (f(mid) ? ok : ng) = mid;\n }\n return ok;\n}\n\nstruct Setup_io {\n Setup_io() {\n ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);\n cout << fixed << setprecision(11);\n }\n} setup_io;\n\n#pragma endregion\n\nint a, b, C, M;\nint r() {\n a = 36969 * (a & M) + (a >> 16);\n b = 18000 * (b & M) + (b >> 16);\n return (C & ((a << 16) + b)) % 1000000;\n}\n\nint main() {\n rep(1000) {\n INT(m, n, A, B);\n if(!n and !m) exit(0);\n if(m + n > 1000) dump(m, n, A, B);\n VEC3(int, x, y, z, m);\n { a = A, b = B, C = ~(1 << 31), M = (1 << 16) - 1; }\n\n rep(n) { x.eb(r()), y.eb(r()), z.eb(r()); }\n\n n += m;\n\n auto id = iota(n);\n sort(all(id), [&](int i, int j) { return z[i] != z[j] ? z[i] < z[j] : x[i] != x[j] ? x[i] > x[j] : y[i] > y[j]; });\n\n vector<map<int, int>> dp;\n fore(i, id) {\n int k = bin_search(-1, si(dp), [&](int k) {\n auto it = dp[k].lower_bound(x[i]);\n return it != begin(dp[k]) and prev(it)->se < y[i];\n });\n if(k + 1 < si(dp)) {\n auto &s = dp[k + 1];\n auto it = s.lower_bound(x[i]);\n while(it != end(s) and it->se >= y[i]) { it = s.erase(it); }\n if(it != begin(s) and prev(it)->se <= y[i])\n continue;\n else\n s.emplace_hint(it, x[i], y[i]);\n } else {\n dp.eb(map<int, int>{{x[i], y[i]}});\n }\n // dump(dp);\n }\n\n OUT(si(dp));\n }\n}", "accuracy": 1, "time_ms": 710, "memory_kb": 14580, "score_of_the_acc": -0.0029, "final_rank": 2 }, { "submission_id": "aoj_1341_6813634", "code_snippet": "#pragma region Macros\n#if defined(noimi) && defined(_GLIBCXX_DEBUG) && defined(_GLIBCXX_DEBUG_PEDANTIC)\n// #pragma comment(linker, \"/stack:200000000\")\n#include <stdc++.h>\n#pragma GCC optimize(\"O3\")\n#else\n#pragma GCC optimize(\"Ofast\")\n// #pragma GCC optimize(\"unroll-loops\")\n// #pragma GCC target(\"popcnt\")\n// #pragma GCC target(\"sse,sse2,sse3,ssse3,sse4,popcnt,fma,abm,mmx,avx,avx2,tune=native\")\n// #pragma GCC target(\"sse,sse2,sse3,ssse3,sse4,popcnt,fma,abm,mmx,avx,avx2\")\n// #pragma GCC target(\"avx2\")\n// #include <bits/stdc++.h>\n#include <bits/stdc++.h>\n#endif\n#include <cstdint>\n#include <immintrin.h>\n#include <variant>\n\n#ifdef noimi\n#define oj_local(a, b) b\n#else\n#define oj_local(a, b) a\n#endif\n\n#define LOCAL if(oj_local(0, 1))\n#define OJ if(oj_local(1, 0))\n\nusing namespace std;\nusing ll = long long;\nusing ull = unsigned long long int;\nusing i128 = __int128_t;\nusing pii = pair<int, int>;\nusing pll = pair<ll, ll>;\nusing ld = long double;\ntemplate <typename T> using vc = vector<T>;\ntemplate <typename T> using vvc = vector<vc<T>>;\ntemplate <typename T> using vvvc = vector<vvc<T>>;\nusing vi = vc<int>;\nusing vl = vc<ll>;\nusing vpi = vc<pii>;\nusing vpl = vc<pll>;\ntemplate <class T> using pq = priority_queue<T>;\ntemplate <class T> using pqg = priority_queue<T, vector<T>, greater<T>>;\ntemplate <typename T> int si(const T &x) { return x.size(); }\ntemplate <class T, class S> inline bool chmax(T &a, const S &b) { return (a < b ? a = b, 1 : 0); }\ntemplate <class T, class S> inline bool chmin(T &a, const S &b) { return (a > b ? a = b, 1 : 0); }\nvi iota(int n) {\n vi a(n);\n return iota(a.begin(), a.end(), 0), a;\n}\ntemplate <typename T> vi iota(const vector<T> &a, bool greater = false) {\n vi res(a.size());\n iota(res.begin(), res.end(), 0);\n sort(res.begin(), res.end(), [&](int i, int j) {\n if(greater) return a[i] > a[j];\n return a[i] < a[j];\n });\n return res;\n}\n\n// macros\n#define overload5(a, b, c, d, e, name, ...) name\n#define overload4(a, b, c, d, name, ...) name\n#define endl '\\n'\n#define REP0(n) for(ll jidlsjf = 0; jidlsjf < n; ++jidlsjf)\n#define REP1(i, n) for(ll i = 0; i < (n); ++i)\n#define REP2(i, a, b) for(ll i = (a); i < (b); ++i)\n#define REP3(i, a, b, c) for(ll i = (a); i < (b); i += (c))\n#define rep(...) overload4(__VA_ARGS__, REP3, REP2, REP1, REP0)(__VA_ARGS__)\n#define per0(n) for(int jidlsjf = 0; jidlsjf < (n); ++jidlsjf)\n#define per1(i, n) for(ll i = (n)-1; i >= 0; --i)\n#define per2(i, a, b) for(ll i = (a)-1; i >= b; --i)\n#define per3(i, a, b, c) for(ll i = (a)-1; i >= (b); i -= (c))\n#define per(...) overload4(__VA_ARGS__, per3, per2, per1, per0)(__VA_ARGS__)\n#define fore0(a) rep(a.size())\n#define fore1(i, a) for(auto &&i : a)\n#define fore2(a, b, v) for(auto &&[a, b] : v)\n#define fore3(a, b, c, v) for(auto &&[a, b, c] : v)\n#define fore4(a, b, c, d, v) for(auto &&[a, b, c, d] : v)\n#define fore(...) overload5(__VA_ARGS__, fore4, fore3, fore2, fore1, fore0)(__VA_ARGS__)\n#define fi first\n#define se second\n#define pb push_back\n#define ppb pop_back\n#define ppf pop_front\n#define eb emplace_back\n#define drop(s) cout << #s << endl, exit(0)\n#define si(c) (int)(c).size()\n#define lb(c, x) distance((c).begin(), lower_bound(all(c), (x)))\n#define ub(c, x) distance((c).begin(), upper_bound(all(c), (x)))\n#define rng(v, l, r) v.begin() + l, v.begin() + r\n#define all(c) begin(c), end(c)\n#define rall(c) rbegin(c), rend(c)\n#define SORT(v) sort(all(v))\n#define REV(v) reverse(all(v))\n#define UNIQUE(x) SORT(x), x.erase(unique(all(x)), x.end())\ntemplate <typename T = ll, typename S> T SUM(const S &v) { return accumulate(all(v), T(0)); }\n#define MIN(v) *min_element(all(v))\n#define MAX(v) *max_element(all(v))\n#define overload2(_1, _2, name, ...) name\n#define vec(type, name, ...) vector<type> name(__VA_ARGS__)\n#define vv(type, name, h, ...) vector<vector<type>> name(h, vector<type>(__VA_ARGS__))\n#define vvv(type, name, h, w, ...) vector<vector<vector<type>>> name(h, vector<vector<type>>(w, vector<type>(__VA_ARGS__)))\n#define vvvv(type, name, a, b, c, ...) \\\n vector<vector<vector<vector<type>>>> name(a, vector<vector<vector<type>>>(b, vector<vector<type>>(c, vector<type>(__VA_ARGS__))))\nconstexpr pii dx4[4] = {pii{1, 0}, pii{0, 1}, pii{-1, 0}, pii{0, -1}};\nconstexpr pii dx8[8] = {{1, 0}, {1, 1}, {0, 1}, {-1, 1}, {-1, 0}, {-1, -1}, {0, -1}, {1, -1}};\n\nnamespace yesno_impl {\nconst string YESNO[2] = {\"NO\", \"YES\"};\nconst string YesNo[2] = {\"No\", \"Yes\"};\nconst string yesno[2] = {\"no\", \"yes\"};\nconst string firstsecond[2] = {\"second\", \"first\"};\nconst string FirstSecond[2] = {\"Second\", \"First\"};\nconst string possiblestr[2] = {\"impossible\", \"possible\"};\nvoid YES(bool t = 1) { cout << YESNO[t] << endl; }\nvoid NO(bool t = 1) { YES(!t); }\nvoid Yes(bool t = 1) { cout << YesNo[t] << endl; }\nvoid No(bool t = 1) { Yes(!t); }\nvoid yes(bool t = 1) { cout << yesno[t] << endl; }\nvoid no(bool t = 1) { yes(!t); }\nvoid first(bool t = 1) { cout << firstsecond[t] << endl; }\nvoid First(bool t = 1) { cout << FirstSecond[t] << endl; }\nvoid possible(bool t = 1) { cout << possiblestr[t] << endl; }\n}; // namespace yesno_impl\nusing namespace yesno_impl;\n\n#define INT(...) \\\n int __VA_ARGS__; \\\n IN(__VA_ARGS__)\n#define LL(...) \\\n ll __VA_ARGS__; \\\n IN(__VA_ARGS__)\n#define STR(...) \\\n string __VA_ARGS__; \\\n IN(__VA_ARGS__)\n#define CHR(...) \\\n char __VA_ARGS__; \\\n IN(__VA_ARGS__)\n#define DBL(...) \\\n double __VA_ARGS__; \\\n IN(__VA_ARGS__)\n#define VEC(type, name, size) \\\n vector<type> name(size); \\\n IN(name)\n#define VEC2(type, name1, name2, size) \\\n vector<type> name1(size), name2(size); \\\n for(int i = 0; i < size; i++) IN(name1[i], name2[i])\n#define VEC3(type, name1, name2, name3, size) \\\n vector<type> name1(size), name2(size), name3(size); \\\n for(int i = 0; i < size; i++) IN(name1[i], name2[i], name3[i])\n#define VEC4(type, name1, name2, name3, name4, size) \\\n vector<type> name1(size), name2(size), name3(size), name4(size); \\\n for(int i = 0; i < size; i++) IN(name1[i], name2[i], name3[i], name4[i]);\n#define VV(type, name, h, w) \\\n vector<vector<type>> name(h, vector<type>(w)); \\\n IN(name)\nint scan() { return getchar(); }\nvoid scan(int &a) { cin >> a; }\nvoid scan(long long &a) { cin >> a; }\nvoid scan(char &a) { cin >> a; }\nvoid scan(double &a) { cin >> a; }\nvoid scan(string &a) { cin >> a; }\ntemplate <class T, class S> void scan(pair<T, S> &p) { scan(p.first), scan(p.second); }\ntemplate <class T> void scan(vector<T> &);\ntemplate <class T> void scan(vector<T> &a) {\n for(auto &i : a) scan(i);\n}\ntemplate <class T> void scan(T &a) { cin >> a; }\nvoid IN() {}\ntemplate <class Head, class... Tail> void IN(Head &head, Tail &...tail) {\n scan(head);\n IN(tail...);\n}\n\ntemplate <typename T, typename S> T ceil(T x, S y) {\n assert(y);\n return (y < 0 ? ceil(-x, -y) : (x > 0 ? (x + y - 1) / y : x / y));\n}\n\ntemplate <typename T, typename S> T floor(T x, S y) {\n assert(y);\n return (y < 0 ? floor(-x, -y) : (x > 0 ? x / y : x / y - (x % y == 0 ? 0 : 1)));\n}\ntemplate <class T> T POW(T x, int n) {\n T res = 1;\n for(; n; n >>= 1, x *= x)\n if(n & 1) res *= x;\n return res;\n}\ntemplate <class T, class S> T POW(T x, S n, const ll &mod) {\n T res = 1;\n x %= mod;\n for(; n; n >>= 1, x = x * x % mod)\n if(n & 1) res = res * x % mod;\n return res;\n}\nvector<pll> factor(ll x) {\n vector<pll> ans;\n for(ll i = 2; i * i <= x; i++)\n if(x % i == 0) {\n ans.push_back({i, 1});\n while((x /= i) % i == 0) ans.back().second++;\n }\n if(x != 1) ans.push_back({x, 1});\n return ans;\n}\ntemplate <class T> vector<T> divisor(T x) {\n vector<T> ans;\n for(T i = 1; i * i <= x; i++)\n if(x % i == 0) {\n ans.pb(i);\n if(i * i != x) ans.pb(x / i);\n }\n return ans;\n}\ntemplate <typename T> void zip(vector<T> &x) {\n vector<T> y = x;\n UNIQUE(y);\n for(int i = 0; i < x.size(); ++i) { x[i] = lb(y, x[i]); }\n}\ntemplate <class S> void fold_in(vector<S> &v) {}\ntemplate <typename Head, typename... Tail, class S> void fold_in(vector<S> &v, Head &&a, Tail &&...tail) {\n for(auto e : a) v.emplace_back(e);\n fold_in(v, tail...);\n}\ntemplate <class S> void renumber(vector<S> &v) {}\ntemplate <typename Head, typename... Tail, class S> void renumber(vector<S> &v, Head &&a, Tail &&...tail) {\n for(auto &&e : a) e = lb(v, e);\n renumber(v, tail...);\n}\ntemplate <class S, class... Args> vector<S> zip(vector<S> &head, Args &&...args) {\n vector<S> v;\n fold_in(v, head, args...);\n sort(all(v)), v.erase(unique(all(v)), v.end());\n renumber(v, head, args...);\n return v;\n}\ntemplate <typename T> vector<T> RUI(const vector<T> &v) {\n vector<T> res(v.size() + 1);\n for(int i = 0; i < v.size(); i++) res[i + 1] = res[i] + v[i];\n return res;\n}\n// 反時計周りに 90 度回転\ntemplate <typename T> void rot(vector<vector<T>> &v) {\n if(empty(v)) return;\n int n = v.size(), m = v[0].size();\n vector res(m, vector<T>(n));\n rep(i, n) rep(j, m) res[m - 1 - j][i] = v[i][j];\n v.swap(res);\n}\n// x in [l, r)\ntemplate <class T, class S> bool inc(const T &x, const S &l, const S &r) { return l <= x and x < r; }\n\n// 便利関数\nconstexpr ll ten(int n) { return n == 0 ? 1 : ten(n - 1) * 10; }\nconstexpr ll tri(ll n) { return n * (n + 1) / 2; }\n// l + ... + r\nconstexpr ll tri(ll l, ll r) { return (l + r) * (r - l + 1) / 2; }\nll max(int x, ll y) { return max((ll)x, y); }\nll max(ll x, int y) { return max(x, (ll)y); }\nint min(int x, ll y) { return min((ll)x, y); }\nint min(ll x, int y) { return min(x, (ll)y); }\n// bit 演算系\nll pow2(int i) { return 1LL << i; }\nint topbit(signed t) { return t == 0 ? -1 : 31 - __builtin_clz(t); }\nint topbit(ll t) { return t == 0 ? -1 : 63 - __builtin_clzll(t); }\nint lowbit(signed a) { return a == 0 ? 32 : __builtin_ctz(a); }\nint lowbit(ll a) { return a == 0 ? 64 : __builtin_ctzll(a); }\n// int allbit(int n) { return (1 << n) - 1; }\nconstexpr ll mask(int n) { return (1LL << n) - 1; }\n// int popcount(signed t) { return __builtin_popcount(t); }\n// int popcount(ll t) { return __builtin_popcountll(t); }\nint popcount(uint64_t t) { return __builtin_popcountll(t); }\nstatic inline uint64_t popcount64(uint64_t x) {\n uint64_t m1 = 0x5555555555555555ll;\n uint64_t m2 = 0x3333333333333333ll;\n uint64_t m4 = 0x0F0F0F0F0F0F0F0Fll;\n uint64_t h01 = 0x0101010101010101ll;\n\n x -= (x >> 1) & m1;\n x = (x & m2) + ((x >> 2) & m2);\n x = (x + (x >> 4)) & m4;\n\n return (x * h01) >> 56;\n}\nbool ispow2(int i) { return i && (i & -i) == i; }\n\nll rnd(ll l, ll r) { //[l, r)\n#ifdef noimi\n static mt19937_64 gen;\n#else\n static mt19937_64 gen(chrono::steady_clock::now().time_since_epoch().count());\n#endif\n return uniform_int_distribution<ll>(l, r - 1)(gen);\n}\nll rnd(ll n) { return rnd(0, n); }\n\ntemplate <class t> void random_shuffle(vc<t> &a) { rep(i, si(a)) swap(a[i], a[rnd(0, i + 1)]); }\n\nint in() {\n int x;\n cin >> x;\n return x;\n}\nll lin() {\n unsigned long long x;\n cin >> x;\n return x;\n}\n\ntemplate <class T, class S> pair<T, S> operator-(const pair<T, S> &x) { return pair<T, S>(-x.first, -x.second); }\ntemplate <class T, class S> pair<T, S> operator-(const pair<T, S> &x, const pair<T, S> &y) { return pair<T, S>(x.fi - y.fi, x.se - y.se); }\ntemplate <class T, class S> pair<T, S> operator+(const pair<T, S> &x, const pair<T, S> &y) { return pair<T, S>(x.fi + y.fi, x.se + y.se); }\ntemplate <class T> pair<T, T> operator&(const pair<T, T> &l, const pair<T, T> &r) { return pair<T, T>(max(l.fi, r.fi), min(l.se, r.se)); }\ntemplate <class T, class S> pair<T, S> operator+=(pair<T, S> &l, const pair<T, S> &r) { return l = l + r; }\ntemplate <class T, class S> pair<T, S> operator-=(pair<T, S> &l, const pair<T, S> &r) { return l = l - r; }\ntemplate <class T> bool intersect(const pair<T, T> &l, const pair<T, T> &r) { return (l.se < r.se ? r.fi < l.se : l.fi < r.se); }\n\ntemplate <typename T> struct edge {\n int from, to;\n T cost;\n int id;\n edge(int to, T cost) : from(-1), to(to), cost(cost) {}\n edge(int from, int to, T cost) : from(from), to(to), cost(cost) {}\n edge(int from, int to, T cost, int id) : from(from), to(to), cost(cost), id(id) {}\n constexpr bool operator<(const edge<T> &rhs) const noexcept { return cost < rhs.cost; }\n edge &operator=(const int &x) {\n to = x;\n return *this;\n }\n operator int() const { return to; }\n friend ostream operator<<(ostream &os, edge &e) { return os << e.to; }\n};\ntemplate <typename T> using Edges = vector<edge<T>>;\n\nusing Tree = vector<vector<int>>;\nusing Graph = vector<vector<int>>;\ntemplate <class T> using Wgraph = vector<vector<edge<T>>>;\nGraph getG(int n, int m = -1, bool directed = false, int margin = 1) {\n Tree res(n);\n if(m == -1) m = n - 1;\n while(m--) {\n int a, b;\n cin >> a >> b;\n a -= margin, b -= margin;\n res[a].emplace_back(b);\n if(!directed) res[b].emplace_back(a);\n }\n return res;\n}\nGraph getTreeFromPar(int n, int margin = 1) {\n Graph res(n);\n for(int i = 1; i < n; i++) {\n int a;\n cin >> a;\n res[a - margin].emplace_back(i);\n }\n return res;\n}\ntemplate <class T> Wgraph<T> getWg(int n, int m = -1, bool directed = false, int margin = 1) {\n Wgraph<T> res(n);\n if(m == -1) m = n - 1;\n while(m--) {\n int a, b;\n T c;\n scan(a), scan(b), scan(c);\n a -= margin, b -= margin;\n res[a].emplace_back(b, c);\n if(!directed) res[b].emplace_back(a, c);\n }\n return res;\n}\nvoid add(Graph &G, int x, int y) { G[x].eb(y), G[y].eb(x); }\ntemplate <class S, class T> void add(Wgraph<S> &G, int x, int y, T c) { G[x].eb(y, c), G[y].eb(x, c); }\n\n#define TEST \\\n INT(testcases); \\\n while(testcases--)\n\nistream &operator>>(istream &is, i128 &v) {\n string s;\n is >> s;\n v = 0;\n for(int i = 0; i < (int)s.size(); i++) {\n if(isdigit(s[i])) { v = v * 10 + s[i] - '0'; }\n }\n if(s[0] == '-') { v *= -1; }\n return is;\n}\n\nostream &operator<<(ostream &os, const i128 &v) {\n if(v == 0) { return (os << \"0\"); }\n i128 num = v;\n if(v < 0) {\n os << '-';\n num = -num;\n }\n string s;\n for(; num > 0; num /= 10) { s.push_back((char)(num % 10) + '0'); }\n reverse(s.begin(), s.end());\n return (os << s);\n}\nnamespace aux {\ntemplate <typename T, unsigned N, unsigned L> struct tp {\n static void output(std::ostream &os, const T &v) {\n os << std::get<N>(v) << (&os == &cerr ? \", \" : \" \");\n tp<T, N + 1, L>::output(os, v);\n }\n};\ntemplate <typename T, unsigned N> struct tp<T, N, N> {\n static void output(std::ostream &os, const T &v) { os << std::get<N>(v); }\n};\n} // namespace aux\ntemplate <typename... Ts> std::ostream &operator<<(std::ostream &os, const std::tuple<Ts...> &t) {\n if(&os == &cerr) { os << '('; }\n aux::tp<std::tuple<Ts...>, 0, sizeof...(Ts) - 1>::output(os, t);\n if(&os == &cerr) { os << ')'; }\n return os;\n}\ntemplate <class T, class S> ostream &operator<<(ostream &os, const pair<T, S> &p) {\n if(&os == &cerr) { return os << \"(\" << p.first << \", \" << p.second << \")\"; }\n return os << p.first << \" \" << p.second;\n}\ntemplate <class Ch, class Tr, class Container> std::basic_ostream<Ch, Tr> &operator<<(std::basic_ostream<Ch, Tr> &os, const Container &x) {\n bool f = true;\n if(&os == &cerr) os << \"[\";\n for(auto &y : x) {\n if(&os == &cerr)\n os << (f ? \"\" : \", \") << y;\n else\n os << (f ? \"\" : \" \") << y;\n f = false;\n }\n if(&os == &cerr) os << \"]\";\n return os;\n}\n\n#ifdef noimi\n#undef endl\nvoid debug() { cerr << endl; }\nvoid debug(bool) { cerr << endl; }\ntemplate <class Head, class... Tail> void debug(bool is_front, Head head, Tail... tail) {\n if(!is_front) cerr << \", \";\n cerr << head;\n debug(false, tail...);\n}\n\n#define dump(args...) \\\n { \\\n vector<string> _debug = _split(#args, ','); \\\n err(true, begin(_debug), args); \\\n }\n\nvector<string> _split(const string &s, char c) {\n vector<string> v;\n stringstream ss(s);\n string x;\n while(getline(ss, x, c)) {\n if(empty(v))\n v.eb(x);\n else {\n bool flag = false;\n for(auto [c, d] : {pair('(', ')'), pair('[', ']'), pair('{', '}')}) {\n if(count(all(v.back()), c) != count(all(v.back()), d)) flag = true;\n }\n if(flag)\n v.back() += \",\" + x;\n else\n v.eb(x);\n }\n }\n return move(v);\n}\n\nvoid err(bool, vector<string>::iterator) { cerr << endl; }\ntemplate <typename T, typename... Args> void err(bool is_front, vector<string>::iterator it, T a, Args... args) {\n if(!is_front) cerr << \", \";\n cerr << it->substr((*it)[0] == ' ', (*it).size()) << \" = \" << a, err(false, ++it, args...);\n}\n\n// #define dump(...) cerr << #__VA_ARGS__ << \" : \", debug(true, __VA_ARGS__)\n#else\n#define dump(...) static_cast<void>(0)\n#define dbg(...) static_cast<void>(0)\n#endif\nvoid OUT() { cout << endl; }\ntemplate <class Head, class... Tail> void OUT(const Head &head, const Tail &...tail) {\n cout << head;\n if(sizeof...(tail)) cout << ' ';\n OUT(tail...);\n}\n\ntemplate <typename T> static constexpr T inf = numeric_limits<T>::max() / 2;\ntemplate <class T, class S> constexpr pair<T, S> inf<pair<T, S>> = {inf<T>, inf<S>};\n\ntemplate <class F> struct REC {\n F f;\n REC(F &&f_) : f(std::forward<F>(f_)) {}\n template <class... Args> auto operator()(Args &&...args) const { return f(*this, std::forward<Args>(args)...); }\n};\n\ntemplate <class S> vector<pair<S, int>> runLength(const vector<S> &v) {\n vector<pair<S, int>> res;\n for(auto &e : v) {\n if(res.empty() or res.back().fi != e)\n res.eb(e, 1);\n else\n res.back().se++;\n }\n return res;\n}\nvector<pair<char, int>> runLength(const string &v) {\n vector<pair<char, int>> res;\n for(auto &e : v) {\n if(res.empty() or res.back().fi != e)\n res.eb(e, 1);\n else\n res.back().se++;\n }\n return res;\n}\n\nint toint(const char &c, const char start = 'a') { return c - start; }\nint toint(const char &c, const string &chars) { return find(all(chars), c) - begin(chars); }\nint alphabets_to_int(const char &c) { return (islower(c) ? c - 'a' : c - 'A' + 26); }\ntemplate <typename T> auto toint(const T &v, const char &start = 'a') {\n vector<decltype(toint(v[0]))> ret;\n ret.reserve(v.size());\n for(auto &&e : v) ret.emplace_back(toint(e, start));\n return ret;\n}\ntemplate <typename T> auto toint(const T &v, const string &start) {\n vector<decltype(toint(v[0]))> ret;\n ret.reserve(v.size());\n for(auto &&e : v) ret.emplace_back(toint(e, start));\n return ret;\n}\n// a -> 0, A -> 26\ntemplate <typename T> auto alphabets_to_int(const T &s) {\n vector<decltype(alphabets_to_int(s[0]))> res;\n res.reserve(s.size());\n for(auto &&e : s) { res.emplace_back(alphabets_to_int(e)); }\n return res;\n}\n\ntemplate <class T, class F> T bin_search(T ok, T ng, const F &f) {\n while(abs(ok - ng) > 1) {\n T mid = ok + ng >> 1;\n (f(mid) ? ok : ng) = mid;\n }\n return ok;\n}\ntemplate <class T, class F> T bin_search_double(T ok, T ng, const F &f, int iter = 80) {\n while(iter--) {\n T mid = (ok + ng) / 2;\n (f(mid) ? ok : ng) = mid;\n }\n return ok;\n}\n\nstruct Setup_io {\n Setup_io() {\n ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);\n cout << fixed << setprecision(11);\n }\n} setup_io;\n\n#pragma endregion\n\nint a, b, C, M;\nint r() {\n a = 36969 * (a & M) + (a >> 16);\n b = 18000 * (b & M) + (b >> 16);\n return (C & ((a << 16) + b)) % 1000000;\n}\n\nint main() {\n rep(1000) {\n INT(m, n, A, B);\n if(!n and !m) exit(0);\n if(m + n > 1000) dump(m, n, A, B);\n VEC3(int, x, y, z, m);\n { a = A, b = B, C = ~(1 << 31), M = (1 << 16) - 1; }\n\n rep(n) { x.eb(r()), y.eb(r()), z.eb(r()); }\n\n n += m;\n\n auto id = iota(n);\n sort(all(id), [&](int i, int j) { return z[i] != z[j] ? z[i] < z[j] : x[i] != x[j] ? x[i] > x[j] : y[i] > y[j]; });\n\n vector<set<pii>> dp;\n fore(i, id) {\n int k = bin_search(-1, si(dp), [&](int k) {\n auto it = dp[k].lower_bound(pii(x[i] - 1, y[i]));\n return it != begin(dp[k]) and prev(it)->se < y[i];\n });\n if(k + 1 < si(dp)) {\n auto &s = dp[k + 1];\n auto it = s.lower_bound(pii(x[i], y[i]));\n while(it != end(s) and it->se >= y[i]) { it = s.erase(it); }\n if(it != begin(s) and prev(it)->se <= y[i])\n continue;\n else\n s.emplace_hint(it, x[i], y[i]);\n } else {\n dp.eb(set<pii>());\n dp.back().emplace(x[i], y[i]);\n }\n // dump(dp);\n }\n\n OUT(si(dp));\n }\n}", "accuracy": 1, "time_ms": 690, "memory_kb": 14792, "score_of_the_acc": -0.0014, "final_rank": 1 }, { "submission_id": "aoj_1341_6813633", "code_snippet": "#pragma region Macros\n#if defined(noimi) && defined(_GLIBCXX_DEBUG) && defined(_GLIBCXX_DEBUG_PEDANTIC)\n// #pragma comment(linker, \"/stack:200000000\")\n#include <stdc++.h>\n#pragma GCC optimize(\"O3\")\n#else\n#pragma GCC optimize(\"Ofast\")\n// #pragma GCC optimize(\"unroll-loops\")\n// #pragma GCC target(\"popcnt\")\n// #pragma GCC target(\"sse,sse2,sse3,ssse3,sse4,popcnt,fma,abm,mmx,avx,avx2,tune=native\")\n// #pragma GCC target(\"sse,sse2,sse3,ssse3,sse4,popcnt,fma,abm,mmx,avx,avx2\")\n// #pragma GCC target(\"avx2\")\n// #include <bits/stdc++.h>\n#include <bits/stdc++.h>\n#endif\n#include <cstdint>\n#include <immintrin.h>\n#include <variant>\n\n#ifdef noimi\n#define oj_local(a, b) b\n#else\n#define oj_local(a, b) a\n#endif\n\n#define LOCAL if(oj_local(0, 1))\n#define OJ if(oj_local(1, 0))\n\nusing namespace std;\nusing ll = long long;\nusing ull = unsigned long long int;\nusing i128 = __int128_t;\nusing pii = pair<int, int>;\nusing pll = pair<ll, ll>;\nusing ld = long double;\ntemplate <typename T> using vc = vector<T>;\ntemplate <typename T> using vvc = vector<vc<T>>;\ntemplate <typename T> using vvvc = vector<vvc<T>>;\nusing vi = vc<int>;\nusing vl = vc<ll>;\nusing vpi = vc<pii>;\nusing vpl = vc<pll>;\ntemplate <class T> using pq = priority_queue<T>;\ntemplate <class T> using pqg = priority_queue<T, vector<T>, greater<T>>;\ntemplate <typename T> int si(const T &x) { return x.size(); }\ntemplate <class T, class S> inline bool chmax(T &a, const S &b) { return (a < b ? a = b, 1 : 0); }\ntemplate <class T, class S> inline bool chmin(T &a, const S &b) { return (a > b ? a = b, 1 : 0); }\nvi iota(int n) {\n vi a(n);\n return iota(a.begin(), a.end(), 0), a;\n}\ntemplate <typename T> vi iota(const vector<T> &a, bool greater = false) {\n vi res(a.size());\n iota(res.begin(), res.end(), 0);\n sort(res.begin(), res.end(), [&](int i, int j) {\n if(greater) return a[i] > a[j];\n return a[i] < a[j];\n });\n return res;\n}\n\n// macros\n#define overload5(a, b, c, d, e, name, ...) name\n#define overload4(a, b, c, d, name, ...) name\n#define endl '\\n'\n#define REP0(n) for(ll jidlsjf = 0; jidlsjf < n; ++jidlsjf)\n#define REP1(i, n) for(ll i = 0; i < (n); ++i)\n#define REP2(i, a, b) for(ll i = (a); i < (b); ++i)\n#define REP3(i, a, b, c) for(ll i = (a); i < (b); i += (c))\n#define rep(...) overload4(__VA_ARGS__, REP3, REP2, REP1, REP0)(__VA_ARGS__)\n#define per0(n) for(int jidlsjf = 0; jidlsjf < (n); ++jidlsjf)\n#define per1(i, n) for(ll i = (n)-1; i >= 0; --i)\n#define per2(i, a, b) for(ll i = (a)-1; i >= b; --i)\n#define per3(i, a, b, c) for(ll i = (a)-1; i >= (b); i -= (c))\n#define per(...) overload4(__VA_ARGS__, per3, per2, per1, per0)(__VA_ARGS__)\n#define fore0(a) rep(a.size())\n#define fore1(i, a) for(auto &&i : a)\n#define fore2(a, b, v) for(auto &&[a, b] : v)\n#define fore3(a, b, c, v) for(auto &&[a, b, c] : v)\n#define fore4(a, b, c, d, v) for(auto &&[a, b, c, d] : v)\n#define fore(...) overload5(__VA_ARGS__, fore4, fore3, fore2, fore1, fore0)(__VA_ARGS__)\n#define fi first\n#define se second\n#define pb push_back\n#define ppb pop_back\n#define ppf pop_front\n#define eb emplace_back\n#define drop(s) cout << #s << endl, exit(0)\n#define si(c) (int)(c).size()\n#define lb(c, x) distance((c).begin(), lower_bound(all(c), (x)))\n#define ub(c, x) distance((c).begin(), upper_bound(all(c), (x)))\n#define rng(v, l, r) v.begin() + l, v.begin() + r\n#define all(c) begin(c), end(c)\n#define rall(c) rbegin(c), rend(c)\n#define SORT(v) sort(all(v))\n#define REV(v) reverse(all(v))\n#define UNIQUE(x) SORT(x), x.erase(unique(all(x)), x.end())\ntemplate <typename T = ll, typename S> T SUM(const S &v) { return accumulate(all(v), T(0)); }\n#define MIN(v) *min_element(all(v))\n#define MAX(v) *max_element(all(v))\n#define overload2(_1, _2, name, ...) name\n#define vec(type, name, ...) vector<type> name(__VA_ARGS__)\n#define vv(type, name, h, ...) vector<vector<type>> name(h, vector<type>(__VA_ARGS__))\n#define vvv(type, name, h, w, ...) vector<vector<vector<type>>> name(h, vector<vector<type>>(w, vector<type>(__VA_ARGS__)))\n#define vvvv(type, name, a, b, c, ...) \\\n vector<vector<vector<vector<type>>>> name(a, vector<vector<vector<type>>>(b, vector<vector<type>>(c, vector<type>(__VA_ARGS__))))\nconstexpr pii dx4[4] = {pii{1, 0}, pii{0, 1}, pii{-1, 0}, pii{0, -1}};\nconstexpr pii dx8[8] = {{1, 0}, {1, 1}, {0, 1}, {-1, 1}, {-1, 0}, {-1, -1}, {0, -1}, {1, -1}};\n\nnamespace yesno_impl {\nconst string YESNO[2] = {\"NO\", \"YES\"};\nconst string YesNo[2] = {\"No\", \"Yes\"};\nconst string yesno[2] = {\"no\", \"yes\"};\nconst string firstsecond[2] = {\"second\", \"first\"};\nconst string FirstSecond[2] = {\"Second\", \"First\"};\nconst string possiblestr[2] = {\"impossible\", \"possible\"};\nvoid YES(bool t = 1) { cout << YESNO[t] << endl; }\nvoid NO(bool t = 1) { YES(!t); }\nvoid Yes(bool t = 1) { cout << YesNo[t] << endl; }\nvoid No(bool t = 1) { Yes(!t); }\nvoid yes(bool t = 1) { cout << yesno[t] << endl; }\nvoid no(bool t = 1) { yes(!t); }\nvoid first(bool t = 1) { cout << firstsecond[t] << endl; }\nvoid First(bool t = 1) { cout << FirstSecond[t] << endl; }\nvoid possible(bool t = 1) { cout << possiblestr[t] << endl; }\n}; // namespace yesno_impl\nusing namespace yesno_impl;\n\n#define INT(...) \\\n int __VA_ARGS__; \\\n IN(__VA_ARGS__)\n#define LL(...) \\\n ll __VA_ARGS__; \\\n IN(__VA_ARGS__)\n#define STR(...) \\\n string __VA_ARGS__; \\\n IN(__VA_ARGS__)\n#define CHR(...) \\\n char __VA_ARGS__; \\\n IN(__VA_ARGS__)\n#define DBL(...) \\\n double __VA_ARGS__; \\\n IN(__VA_ARGS__)\n#define VEC(type, name, size) \\\n vector<type> name(size); \\\n IN(name)\n#define VEC2(type, name1, name2, size) \\\n vector<type> name1(size), name2(size); \\\n for(int i = 0; i < size; i++) IN(name1[i], name2[i])\n#define VEC3(type, name1, name2, name3, size) \\\n vector<type> name1(size), name2(size), name3(size); \\\n for(int i = 0; i < size; i++) IN(name1[i], name2[i], name3[i])\n#define VEC4(type, name1, name2, name3, name4, size) \\\n vector<type> name1(size), name2(size), name3(size), name4(size); \\\n for(int i = 0; i < size; i++) IN(name1[i], name2[i], name3[i], name4[i]);\n#define VV(type, name, h, w) \\\n vector<vector<type>> name(h, vector<type>(w)); \\\n IN(name)\nint scan() { return getchar(); }\nvoid scan(int &a) { cin >> a; }\nvoid scan(long long &a) { cin >> a; }\nvoid scan(char &a) { cin >> a; }\nvoid scan(double &a) { cin >> a; }\nvoid scan(string &a) { cin >> a; }\ntemplate <class T, class S> void scan(pair<T, S> &p) { scan(p.first), scan(p.second); }\ntemplate <class T> void scan(vector<T> &);\ntemplate <class T> void scan(vector<T> &a) {\n for(auto &i : a) scan(i);\n}\ntemplate <class T> void scan(T &a) { cin >> a; }\nvoid IN() {}\ntemplate <class Head, class... Tail> void IN(Head &head, Tail &...tail) {\n scan(head);\n IN(tail...);\n}\n\ntemplate <typename T, typename S> T ceil(T x, S y) {\n assert(y);\n return (y < 0 ? ceil(-x, -y) : (x > 0 ? (x + y - 1) / y : x / y));\n}\n\ntemplate <typename T, typename S> T floor(T x, S y) {\n assert(y);\n return (y < 0 ? floor(-x, -y) : (x > 0 ? x / y : x / y - (x % y == 0 ? 0 : 1)));\n}\ntemplate <class T> T POW(T x, int n) {\n T res = 1;\n for(; n; n >>= 1, x *= x)\n if(n & 1) res *= x;\n return res;\n}\ntemplate <class T, class S> T POW(T x, S n, const ll &mod) {\n T res = 1;\n x %= mod;\n for(; n; n >>= 1, x = x * x % mod)\n if(n & 1) res = res * x % mod;\n return res;\n}\nvector<pll> factor(ll x) {\n vector<pll> ans;\n for(ll i = 2; i * i <= x; i++)\n if(x % i == 0) {\n ans.push_back({i, 1});\n while((x /= i) % i == 0) ans.back().second++;\n }\n if(x != 1) ans.push_back({x, 1});\n return ans;\n}\ntemplate <class T> vector<T> divisor(T x) {\n vector<T> ans;\n for(T i = 1; i * i <= x; i++)\n if(x % i == 0) {\n ans.pb(i);\n if(i * i != x) ans.pb(x / i);\n }\n return ans;\n}\ntemplate <typename T> void zip(vector<T> &x) {\n vector<T> y = x;\n UNIQUE(y);\n for(int i = 0; i < x.size(); ++i) { x[i] = lb(y, x[i]); }\n}\ntemplate <class S> void fold_in(vector<S> &v) {}\ntemplate <typename Head, typename... Tail, class S> void fold_in(vector<S> &v, Head &&a, Tail &&...tail) {\n for(auto e : a) v.emplace_back(e);\n fold_in(v, tail...);\n}\ntemplate <class S> void renumber(vector<S> &v) {}\ntemplate <typename Head, typename... Tail, class S> void renumber(vector<S> &v, Head &&a, Tail &&...tail) {\n for(auto &&e : a) e = lb(v, e);\n renumber(v, tail...);\n}\ntemplate <class S, class... Args> vector<S> zip(vector<S> &head, Args &&...args) {\n vector<S> v;\n fold_in(v, head, args...);\n sort(all(v)), v.erase(unique(all(v)), v.end());\n renumber(v, head, args...);\n return v;\n}\ntemplate <typename T> vector<T> RUI(const vector<T> &v) {\n vector<T> res(v.size() + 1);\n for(int i = 0; i < v.size(); i++) res[i + 1] = res[i] + v[i];\n return res;\n}\n// 反時計周りに 90 度回転\ntemplate <typename T> void rot(vector<vector<T>> &v) {\n if(empty(v)) return;\n int n = v.size(), m = v[0].size();\n vector res(m, vector<T>(n));\n rep(i, n) rep(j, m) res[m - 1 - j][i] = v[i][j];\n v.swap(res);\n}\n// x in [l, r)\ntemplate <class T, class S> bool inc(const T &x, const S &l, const S &r) { return l <= x and x < r; }\n\n// 便利関数\nconstexpr ll ten(int n) { return n == 0 ? 1 : ten(n - 1) * 10; }\nconstexpr ll tri(ll n) { return n * (n + 1) / 2; }\n// l + ... + r\nconstexpr ll tri(ll l, ll r) { return (l + r) * (r - l + 1) / 2; }\nll max(int x, ll y) { return max((ll)x, y); }\nll max(ll x, int y) { return max(x, (ll)y); }\nint min(int x, ll y) { return min((ll)x, y); }\nint min(ll x, int y) { return min(x, (ll)y); }\n// bit 演算系\nll pow2(int i) { return 1LL << i; }\nint topbit(signed t) { return t == 0 ? -1 : 31 - __builtin_clz(t); }\nint topbit(ll t) { return t == 0 ? -1 : 63 - __builtin_clzll(t); }\nint lowbit(signed a) { return a == 0 ? 32 : __builtin_ctz(a); }\nint lowbit(ll a) { return a == 0 ? 64 : __builtin_ctzll(a); }\n// int allbit(int n) { return (1 << n) - 1; }\nconstexpr ll mask(int n) { return (1LL << n) - 1; }\n// int popcount(signed t) { return __builtin_popcount(t); }\n// int popcount(ll t) { return __builtin_popcountll(t); }\nint popcount(uint64_t t) { return __builtin_popcountll(t); }\nstatic inline uint64_t popcount64(uint64_t x) {\n uint64_t m1 = 0x5555555555555555ll;\n uint64_t m2 = 0x3333333333333333ll;\n uint64_t m4 = 0x0F0F0F0F0F0F0F0Fll;\n uint64_t h01 = 0x0101010101010101ll;\n\n x -= (x >> 1) & m1;\n x = (x & m2) + ((x >> 2) & m2);\n x = (x + (x >> 4)) & m4;\n\n return (x * h01) >> 56;\n}\nbool ispow2(int i) { return i && (i & -i) == i; }\n\nll rnd(ll l, ll r) { //[l, r)\n#ifdef noimi\n static mt19937_64 gen;\n#else\n static mt19937_64 gen(chrono::steady_clock::now().time_since_epoch().count());\n#endif\n return uniform_int_distribution<ll>(l, r - 1)(gen);\n}\nll rnd(ll n) { return rnd(0, n); }\n\ntemplate <class t> void random_shuffle(vc<t> &a) { rep(i, si(a)) swap(a[i], a[rnd(0, i + 1)]); }\n\nint in() {\n int x;\n cin >> x;\n return x;\n}\nll lin() {\n unsigned long long x;\n cin >> x;\n return x;\n}\n\ntemplate <class T, class S> pair<T, S> operator-(const pair<T, S> &x) { return pair<T, S>(-x.first, -x.second); }\ntemplate <class T, class S> pair<T, S> operator-(const pair<T, S> &x, const pair<T, S> &y) { return pair<T, S>(x.fi - y.fi, x.se - y.se); }\ntemplate <class T, class S> pair<T, S> operator+(const pair<T, S> &x, const pair<T, S> &y) { return pair<T, S>(x.fi + y.fi, x.se + y.se); }\ntemplate <class T> pair<T, T> operator&(const pair<T, T> &l, const pair<T, T> &r) { return pair<T, T>(max(l.fi, r.fi), min(l.se, r.se)); }\ntemplate <class T, class S> pair<T, S> operator+=(pair<T, S> &l, const pair<T, S> &r) { return l = l + r; }\ntemplate <class T, class S> pair<T, S> operator-=(pair<T, S> &l, const pair<T, S> &r) { return l = l - r; }\ntemplate <class T> bool intersect(const pair<T, T> &l, const pair<T, T> &r) { return (l.se < r.se ? r.fi < l.se : l.fi < r.se); }\n\ntemplate <typename T> struct edge {\n int from, to;\n T cost;\n int id;\n edge(int to, T cost) : from(-1), to(to), cost(cost) {}\n edge(int from, int to, T cost) : from(from), to(to), cost(cost) {}\n edge(int from, int to, T cost, int id) : from(from), to(to), cost(cost), id(id) {}\n constexpr bool operator<(const edge<T> &rhs) const noexcept { return cost < rhs.cost; }\n edge &operator=(const int &x) {\n to = x;\n return *this;\n }\n operator int() const { return to; }\n friend ostream operator<<(ostream &os, edge &e) { return os << e.to; }\n};\ntemplate <typename T> using Edges = vector<edge<T>>;\n\nusing Tree = vector<vector<int>>;\nusing Graph = vector<vector<int>>;\ntemplate <class T> using Wgraph = vector<vector<edge<T>>>;\nGraph getG(int n, int m = -1, bool directed = false, int margin = 1) {\n Tree res(n);\n if(m == -1) m = n - 1;\n while(m--) {\n int a, b;\n cin >> a >> b;\n a -= margin, b -= margin;\n res[a].emplace_back(b);\n if(!directed) res[b].emplace_back(a);\n }\n return res;\n}\nGraph getTreeFromPar(int n, int margin = 1) {\n Graph res(n);\n for(int i = 1; i < n; i++) {\n int a;\n cin >> a;\n res[a - margin].emplace_back(i);\n }\n return res;\n}\ntemplate <class T> Wgraph<T> getWg(int n, int m = -1, bool directed = false, int margin = 1) {\n Wgraph<T> res(n);\n if(m == -1) m = n - 1;\n while(m--) {\n int a, b;\n T c;\n scan(a), scan(b), scan(c);\n a -= margin, b -= margin;\n res[a].emplace_back(b, c);\n if(!directed) res[b].emplace_back(a, c);\n }\n return res;\n}\nvoid add(Graph &G, int x, int y) { G[x].eb(y), G[y].eb(x); }\ntemplate <class S, class T> void add(Wgraph<S> &G, int x, int y, T c) { G[x].eb(y, c), G[y].eb(x, c); }\n\n#define TEST \\\n INT(testcases); \\\n while(testcases--)\n\nistream &operator>>(istream &is, i128 &v) {\n string s;\n is >> s;\n v = 0;\n for(int i = 0; i < (int)s.size(); i++) {\n if(isdigit(s[i])) { v = v * 10 + s[i] - '0'; }\n }\n if(s[0] == '-') { v *= -1; }\n return is;\n}\n\nostream &operator<<(ostream &os, const i128 &v) {\n if(v == 0) { return (os << \"0\"); }\n i128 num = v;\n if(v < 0) {\n os << '-';\n num = -num;\n }\n string s;\n for(; num > 0; num /= 10) { s.push_back((char)(num % 10) + '0'); }\n reverse(s.begin(), s.end());\n return (os << s);\n}\nnamespace aux {\ntemplate <typename T, unsigned N, unsigned L> struct tp {\n static void output(std::ostream &os, const T &v) {\n os << std::get<N>(v) << (&os == &cerr ? \", \" : \" \");\n tp<T, N + 1, L>::output(os, v);\n }\n};\ntemplate <typename T, unsigned N> struct tp<T, N, N> {\n static void output(std::ostream &os, const T &v) { os << std::get<N>(v); }\n};\n} // namespace aux\ntemplate <typename... Ts> std::ostream &operator<<(std::ostream &os, const std::tuple<Ts...> &t) {\n if(&os == &cerr) { os << '('; }\n aux::tp<std::tuple<Ts...>, 0, sizeof...(Ts) - 1>::output(os, t);\n if(&os == &cerr) { os << ')'; }\n return os;\n}\ntemplate <class T, class S> ostream &operator<<(ostream &os, const pair<T, S> &p) {\n if(&os == &cerr) { return os << \"(\" << p.first << \", \" << p.second << \")\"; }\n return os << p.first << \" \" << p.second;\n}\ntemplate <class Ch, class Tr, class Container> std::basic_ostream<Ch, Tr> &operator<<(std::basic_ostream<Ch, Tr> &os, const Container &x) {\n bool f = true;\n if(&os == &cerr) os << \"[\";\n for(auto &y : x) {\n if(&os == &cerr)\n os << (f ? \"\" : \", \") << y;\n else\n os << (f ? \"\" : \" \") << y;\n f = false;\n }\n if(&os == &cerr) os << \"]\";\n return os;\n}\n\n#ifdef noimi\n#undef endl\nvoid debug() { cerr << endl; }\nvoid debug(bool) { cerr << endl; }\ntemplate <class Head, class... Tail> void debug(bool is_front, Head head, Tail... tail) {\n if(!is_front) cerr << \", \";\n cerr << head;\n debug(false, tail...);\n}\n\n#define dump(args...) \\\n { \\\n vector<string> _debug = _split(#args, ','); \\\n err(true, begin(_debug), args); \\\n }\n\nvector<string> _split(const string &s, char c) {\n vector<string> v;\n stringstream ss(s);\n string x;\n while(getline(ss, x, c)) {\n if(empty(v))\n v.eb(x);\n else {\n bool flag = false;\n for(auto [c, d] : {pair('(', ')'), pair('[', ']'), pair('{', '}')}) {\n if(count(all(v.back()), c) != count(all(v.back()), d)) flag = true;\n }\n if(flag)\n v.back() += \",\" + x;\n else\n v.eb(x);\n }\n }\n return move(v);\n}\n\nvoid err(bool, vector<string>::iterator) { cerr << endl; }\ntemplate <typename T, typename... Args> void err(bool is_front, vector<string>::iterator it, T a, Args... args) {\n if(!is_front) cerr << \", \";\n cerr << it->substr((*it)[0] == ' ', (*it).size()) << \" = \" << a, err(false, ++it, args...);\n}\n\n// #define dump(...) cerr << #__VA_ARGS__ << \" : \", debug(true, __VA_ARGS__)\n#else\n#define dump(...) static_cast<void>(0)\n#define dbg(...) static_cast<void>(0)\n#endif\nvoid OUT() { cout << endl; }\ntemplate <class Head, class... Tail> void OUT(const Head &head, const Tail &...tail) {\n cout << head;\n if(sizeof...(tail)) cout << ' ';\n OUT(tail...);\n}\n\ntemplate <typename T> static constexpr T inf = numeric_limits<T>::max() / 2;\ntemplate <class T, class S> constexpr pair<T, S> inf<pair<T, S>> = {inf<T>, inf<S>};\n\ntemplate <class F> struct REC {\n F f;\n REC(F &&f_) : f(std::forward<F>(f_)) {}\n template <class... Args> auto operator()(Args &&...args) const { return f(*this, std::forward<Args>(args)...); }\n};\n\ntemplate <class S> vector<pair<S, int>> runLength(const vector<S> &v) {\n vector<pair<S, int>> res;\n for(auto &e : v) {\n if(res.empty() or res.back().fi != e)\n res.eb(e, 1);\n else\n res.back().se++;\n }\n return res;\n}\nvector<pair<char, int>> runLength(const string &v) {\n vector<pair<char, int>> res;\n for(auto &e : v) {\n if(res.empty() or res.back().fi != e)\n res.eb(e, 1);\n else\n res.back().se++;\n }\n return res;\n}\n\nint toint(const char &c, const char start = 'a') { return c - start; }\nint toint(const char &c, const string &chars) { return find(all(chars), c) - begin(chars); }\nint alphabets_to_int(const char &c) { return (islower(c) ? c - 'a' : c - 'A' + 26); }\ntemplate <typename T> auto toint(const T &v, const char &start = 'a') {\n vector<decltype(toint(v[0]))> ret;\n ret.reserve(v.size());\n for(auto &&e : v) ret.emplace_back(toint(e, start));\n return ret;\n}\ntemplate <typename T> auto toint(const T &v, const string &start) {\n vector<decltype(toint(v[0]))> ret;\n ret.reserve(v.size());\n for(auto &&e : v) ret.emplace_back(toint(e, start));\n return ret;\n}\n// a -> 0, A -> 26\ntemplate <typename T> auto alphabets_to_int(const T &s) {\n vector<decltype(alphabets_to_int(s[0]))> res;\n res.reserve(s.size());\n for(auto &&e : s) { res.emplace_back(alphabets_to_int(e)); }\n return res;\n}\n\ntemplate <class T, class F> T bin_search(T ok, T ng, const F &f) {\n while(abs(ok - ng) > 1) {\n T mid = ok + ng >> 1;\n (f(mid) ? ok : ng) = mid;\n }\n return ok;\n}\ntemplate <class T, class F> T bin_search_double(T ok, T ng, const F &f, int iter = 80) {\n while(iter--) {\n T mid = (ok + ng) / 2;\n (f(mid) ? ok : ng) = mid;\n }\n return ok;\n}\n\nstruct Setup_io {\n Setup_io() {\n ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);\n cout << fixed << setprecision(11);\n }\n} setup_io;\n\n#pragma endregion\n\nint a, b, C, M;\nint r() {\n a = 36969 * (a & M) + (a >> 16);\n b = 18000 * (b & M) + (b >> 16);\n return (C & ((a << 16) + b)) % 1000000;\n}\n\nint main() {\n rep(1000) {\n INT(m, n, A, B);\n if(!n and !m) exit(0);\n if(m + n > 1000) dump(m, n, A, B);\n VEC3(int, x, y, z, m);\n { a = A, b = B, C = ~(1 << 31), M = (1 << 16) - 1; }\n\n rep(n) { x.eb(r()), y.eb(r()), z.eb(r()); }\n\n n += m;\n\n auto id = iota(n);\n sort(all(id), [&](int i, int j) { return z[i] != z[j] ? z[i] < z[j] : x[i] != x[j] ? x[i] > x[j] : y[i] > y[j]; });\n\n vector<set<pii>> dp;\n fore(i, id) {\n int k = bin_search(-1, si(dp), [&](int k) {\n auto it = dp[k].lower_bound(pii(x[i] - 1, y[i]));\n return it != begin(dp[k]) and prev(it)->se < y[i];\n });\n if(k + 1 < si(dp)) {\n auto &s = dp[k + 1];\n auto it = s.lower_bound(pii(x[i], y[i]));\n while(it != end(s) and it->se >= y[i]) { it = s.erase(it); }\n if(it != begin(s) and prev(it)->se <= y[i])\n continue;\n else\n s.emplace(x[i], y[i]);\n } else {\n dp.eb(set<pii>());\n dp.back().emplace(x[i], y[i]);\n }\n // dump(dp);\n }\n\n OUT(si(dp));\n }\n}", "accuracy": 1, "time_ms": 730, "memory_kb": 15076, "score_of_the_acc": -0.0089, "final_rank": 3 }, { "submission_id": "aoj_1341_6674108", "code_snippet": "#include <bits/stdc++.h>\n\n#ifdef _MSC_VER\n# include <intrin.h>\n#else\n# include <x86intrin.h>\n#endif\n\n#include <limits>\n#include <type_traits>\n\nnamespace suisen {\n// ! utility\ntemplate <typename ...Types>\nusing constraints_t = std::enable_if_t<std::conjunction_v<Types...>, std::nullptr_t>;\ntemplate <bool cond_v, typename Then, typename OrElse>\nconstexpr decltype(auto) constexpr_if(Then&& then, OrElse&& or_else) {\n if constexpr (cond_v) {\n return std::forward<Then>(then);\n } else {\n return std::forward<OrElse>(or_else);\n }\n}\n\n// ! function\ntemplate <typename ReturnType, typename Callable, typename ...Args>\nusing is_same_as_invoke_result = std::is_same<std::invoke_result_t<Callable, Args...>, ReturnType>;\ntemplate <typename F, typename T>\nusing is_uni_op = is_same_as_invoke_result<T, F, T>;\ntemplate <typename F, typename T>\nusing is_bin_op = is_same_as_invoke_result<T, F, T, T>;\n\ntemplate <typename Comparator, typename T>\nusing is_comparator = std::is_same<std::invoke_result_t<Comparator, T, T>, bool>;\n\n// ! integral\ntemplate <typename T, typename = constraints_t<std::is_integral<T>>>\nconstexpr int bit_num = std::numeric_limits<std::make_unsigned_t<T>>::digits;\ntemplate <typename T, unsigned int n>\nstruct is_nbit { static constexpr bool value = bit_num<T> == n; };\ntemplate <typename T, unsigned int n>\nstatic constexpr bool is_nbit_v = is_nbit<T, n>::value;\n\n// ?\ntemplate <typename T>\nstruct safely_multipliable {};\ntemplate <>\nstruct safely_multipliable<int> { using type = long long; };\ntemplate <>\nstruct safely_multipliable<long long> { using type = __int128_t; };\ntemplate <>\nstruct safely_multipliable<unsigned int> { using type = unsigned long long; };\ntemplate <>\nstruct safely_multipliable<unsigned long int> { using type = __uint128_t; };\ntemplate <>\nstruct safely_multipliable<unsigned long long> { using type = __uint128_t; };\ntemplate <>\nstruct safely_multipliable<float> { using type = float; };\ntemplate <>\nstruct safely_multipliable<double> { using type = double; };\ntemplate <>\nstruct safely_multipliable<long double> { using type = long double; };\ntemplate <typename T>\nusing safely_multipliable_t = typename safely_multipliable<T>::type;\n\ntemplate <typename T, typename = void>\nstruct rec_value_type {\n using type = T;\n};\ntemplate <typename T>\nstruct rec_value_type<T, std::void_t<typename T::value_type>> {\n using type = typename rec_value_type<typename T::value_type>::type;\n};\ntemplate <typename T>\nusing rec_value_type_t = typename rec_value_type<T>::type;\n\n} // namespace suisen\n\n// ! type aliases\nusing i128 = __int128_t;\nusing u128 = __uint128_t;\n\ntemplate <typename T>\nusing pq_greater = std::priority_queue<T, std::vector<T>, std::greater<T>>;\ntemplate <typename T, typename U>\nusing umap = std::unordered_map<T, U>;\n\n// ! macros (capital: internal macro)\n#define OVERLOAD2(_1,_2,name,...) name\n#define OVERLOAD3(_1,_2,_3,name,...) name\n#define OVERLOAD4(_1,_2,_3,_4,name,...) name\n\n#define REP4(i,l,r,s) for(std::remove_reference_t<std::remove_const_t<decltype(r)>>i=(l);i<(r);i+=(s))\n#define REP3(i,l,r) REP4(i,l,r,1)\n#define REP2(i,n) REP3(i,0,n)\n#define REPINF3(i,l,s) for(std::remove_reference_t<std::remove_const_t<decltype(l)>>i=(l);;i+=(s))\n#define REPINF2(i,l) REPINF3(i,l,1)\n#define REPINF1(i) REPINF2(i,0)\n#define RREP4(i,l,r,s) for(std::remove_reference_t<std::remove_const_t<decltype(r)>>i=(l)+fld((r)-(l)-1,s)*(s);i>=(l);i-=(s))\n#define RREP3(i,l,r) RREP4(i,l,r,1)\n#define RREP2(i,n) RREP3(i,0,n)\n\n#define rep(...) OVERLOAD4(__VA_ARGS__, REP4 , REP3 , REP2 )(__VA_ARGS__)\n#define rrep(...) OVERLOAD4(__VA_ARGS__, RREP4 , RREP3 , RREP2 )(__VA_ARGS__)\n#define repinf(...) OVERLOAD3(__VA_ARGS__, REPINF3, REPINF2, REPINF1)(__VA_ARGS__)\n\n#define CAT_I(a, b) a##b\n#define CAT(a, b) CAT_I(a, b)\n#define UNIQVAR(tag) CAT(tag, __LINE__)\n#define loop(n) for (std::remove_reference_t<std::remove_const_t<decltype(n)>> UNIQVAR(loop_variable) = n; UNIQVAR(loop_variable) --> 0;)\n\n#define all(iterable) std::begin(iterable), std::end(iterable)\n#define input(type, ...) type __VA_ARGS__; read(__VA_ARGS__)\n\n#ifdef LOCAL\n# define debug(...) debug_internal(#__VA_ARGS__, __VA_ARGS__)\n\ntemplate <class T, class... Args>\nvoid debug_internal(const char* s, T&& first, Args&&... args) {\n constexpr const char* prefix = \"[\\033[32mDEBUG\\033[m] \";\n constexpr const char* open_brakets = sizeof...(args) == 0 ? \"\" : \"(\";\n constexpr const char* close_brakets = sizeof...(args) == 0 ? \"\" : \")\";\n std::cerr << prefix << open_brakets << s << close_brakets << \": \" << open_brakets << std::forward<T>(first);\n ((std::cerr << \", \" << std::forward<Args>(args)), ...);\n std::cerr << close_brakets << \"\\n\";\n}\n\n#else\n# define debug(...) void(0)\n#endif\n\n// ! I/O utilities\n\n// __int128_t\nstd::ostream& operator<<(std::ostream& dest, __int128_t value) {\n std::ostream::sentry s(dest);\n if (s) {\n __uint128_t tmp = value < 0 ? -value : value;\n char buffer[128];\n char* d = std::end(buffer);\n do {\n --d;\n *d = \"0123456789\"[tmp % 10];\n tmp /= 10;\n } while (tmp != 0);\n if (value < 0) {\n --d;\n *d = '-';\n }\n int len = std::end(buffer) - d;\n if (dest.rdbuf()->sputn(d, len) != len) {\n dest.setstate(std::ios_base::badbit);\n }\n }\n return dest;\n}\n// __uint128_t\nstd::ostream& operator<<(std::ostream& dest, __uint128_t value) {\n std::ostream::sentry s(dest);\n if (s) {\n char buffer[128];\n char* d = std::end(buffer);\n do {\n --d;\n *d = \"0123456789\"[value % 10];\n value /= 10;\n } while (value != 0);\n int len = std::end(buffer) - d;\n if (dest.rdbuf()->sputn(d, len) != len) {\n dest.setstate(std::ios_base::badbit);\n }\n }\n return dest;\n}\n\n// pair\ntemplate <typename T, typename U>\nstd::ostream& operator<<(std::ostream& out, const std::pair<T, U>& a) {\n return out << a.first << ' ' << a.second;\n}\n// tuple\ntemplate <unsigned int N = 0, typename ...Args>\nstd::ostream& operator<<(std::ostream& out, const std::tuple<Args...>& a) {\n if constexpr (N >= std::tuple_size_v<std::tuple<Args...>>) {\n return out;\n } else {\n out << std::get<N>(a);\n if constexpr (N + 1 < std::tuple_size_v<std::tuple<Args...>>) {\n out << ' ';\n }\n return operator<<<N + 1>(out, a);\n }\n}\n// vector\ntemplate <typename T>\nstd::ostream& operator<<(std::ostream& out, const std::vector<T>& a) {\n for (auto it = a.begin(); it != a.end();) {\n out << *it;\n if (++it != a.end()) out << ' ';\n }\n return out;\n}\n// array\ntemplate <typename T, size_t N>\nstd::ostream& operator<<(std::ostream& out, const std::array<T, N>& a) {\n for (auto it = a.begin(); it != a.end();) {\n out << *it;\n if (++it != a.end()) out << ' ';\n }\n return out;\n}\ninline void print() { std::cout << '\\n'; }\ntemplate <typename Head, typename... Tail>\ninline void print(const Head& head, const Tail &...tails) {\n std::cout << head;\n if (sizeof...(tails)) std::cout << ' ';\n print(tails...);\n}\ntemplate <typename Iterable>\nauto print_all(const Iterable& v, std::string sep = \" \", std::string end = \"\\n\") -> decltype(std::cout << *v.begin(), void()) {\n for (auto it = v.begin(); it != v.end();) {\n std::cout << *it;\n if (++it != v.end()) std::cout << sep;\n }\n std::cout << end;\n}\n\n__int128_t parse_i128(std::string& s) {\n __int128_t ret = 0;\n for (int i = 0; i < int(s.size()); i++) if ('0' <= s[i] and s[i] <= '9') ret = 10 * ret + s[i] - '0';\n if (s[0] == '-') ret = -ret;\n return ret;\n}\n__uint128_t parse_u128(std::string& s) {\n __uint128_t ret = 0;\n for (int i = 0; i < int(s.size()); i++) if ('0' <= s[i] and s[i] <= '9') ret = 10 * ret + s[i] - '0';\n return ret;\n}\n// __int128_t\nstd::istream& operator>>(std::istream& in, __int128_t& v) {\n std::string s;\n in >> s;\n v = parse_i128(s);\n return in;\n}\n// __uint128_t\nstd::istream& operator>>(std::istream& in, __uint128_t& v) {\n std::string s;\n in >> s;\n v = parse_u128(s);\n return in;\n}\n// pair\ntemplate <typename T, typename U>\nstd::istream& operator>>(std::istream& in, std::pair<T, U>& a) {\n return in >> a.first >> a.second;\n}\n// tuple\ntemplate <unsigned int N = 0, typename ...Args>\nstd::istream& operator>>(std::istream& in, std::tuple<Args...>& a) {\n if constexpr (N >= std::tuple_size_v<std::tuple<Args...>>) {\n return in;\n } else {\n return operator>><N + 1>(in >> std::get<N>(a), a);\n }\n}\n// vector\ntemplate <typename T>\nstd::istream& operator>>(std::istream& in, std::vector<T>& a) {\n for (auto it = a.begin(); it != a.end(); ++it) in >> *it;\n return in;\n}\n// array\ntemplate <typename T, size_t N>\nstd::istream& operator>>(std::istream& in, std::array<T, N>& a) {\n for (auto it = a.begin(); it != a.end(); ++it) in >> *it;\n return in;\n}\ntemplate <typename ...Args>\nvoid read(Args &...args) {\n (std::cin >> ... >> args);\n}\n\n// ! integral utilities\n\n// Returns pow(-1, n)\ntemplate <typename T>\nconstexpr inline int pow_m1(T n) {\n return -(n & 1) | 1;\n}\n// Returns pow(-1, n)\ntemplate <>\nconstexpr inline int pow_m1<bool>(bool n) {\n return -int(n) | 1;\n}\n\n// Returns floor(x / y)\ntemplate <typename T>\nconstexpr inline T fld(const T x, const T y) {\n return (x ^ y) >= 0 ? x / y : (x - (y + pow_m1(y >= 0))) / y;\n}\ntemplate <typename T>\nconstexpr inline T cld(const T x, const T y) {\n return (x ^ y) <= 0 ? x / y : (x + (y + pow_m1(y >= 0))) / y;\n}\n\ntemplate <typename T, suisen::constraints_t<suisen::is_nbit<T, 16>> = nullptr>\n__attribute__((target(\"popcnt\"))) constexpr inline int popcount(const T x) { return _mm_popcnt_u32(x); }\ntemplate <typename T, suisen::constraints_t<suisen::is_nbit<T, 32>> = nullptr>\n__attribute__((target(\"popcnt\"))) constexpr inline int popcount(const T x) { return _mm_popcnt_u32(x); }\ntemplate <typename T, suisen::constraints_t<suisen::is_nbit<T, 64>> = nullptr>\n__attribute__((target(\"popcnt\"))) constexpr inline int popcount(const T x) { return _mm_popcnt_u64(x); }\ntemplate <typename T, suisen::constraints_t<suisen::is_nbit<T, 16>> = nullptr>\nconstexpr inline int count_lz(const T x) { return x ? __builtin_clz(x) : suisen::bit_num<T>; }\ntemplate <typename T, suisen::constraints_t<suisen::is_nbit<T, 32>> = nullptr>\nconstexpr inline int count_lz(const T x) { return x ? __builtin_clz(x) : suisen::bit_num<T>; }\ntemplate <typename T, suisen::constraints_t<suisen::is_nbit<T, 64>> = nullptr>\nconstexpr inline int count_lz(const T x) { return x ? __builtin_clzll(x) : suisen::bit_num<T>; }\ntemplate <typename T, suisen::constraints_t<suisen::is_nbit<T, 16>> = nullptr>\nconstexpr inline int count_tz(const T x) { return x ? __builtin_ctz(x) : suisen::bit_num<T>; }\ntemplate <typename T, suisen::constraints_t<suisen::is_nbit<T, 32>> = nullptr>\nconstexpr inline int count_tz(const T x) { return x ? __builtin_ctz(x) : suisen::bit_num<T>; }\ntemplate <typename T, suisen::constraints_t<suisen::is_nbit<T, 64>> = nullptr>\nconstexpr inline int count_tz(const T x) { return x ? __builtin_ctzll(x) : suisen::bit_num<T>; }\ntemplate <typename T>\nconstexpr inline int floor_log2(const T x) { return suisen::bit_num<T> -1 - count_lz(x); }\ntemplate <typename T>\nconstexpr inline int ceil_log2(const T x) { return floor_log2(x) + ((x & -x) != x); }\ntemplate <typename T>\nconstexpr inline int kth_bit(const T x, const unsigned int k) { return (x >> k) & 1; }\ntemplate <typename T>\nconstexpr inline int parity(const T x) { return popcount(x) & 1; }\n\n// ! container\n\ntemplate <typename T, typename Comparator, suisen::constraints_t<suisen::is_comparator<Comparator, T>> = nullptr>\nauto priqueue_comp(const Comparator comparator) {\n return std::priority_queue<T, std::vector<T>, Comparator>(comparator);\n}\n\ntemplate <typename Iterable>\nauto isize(const Iterable& iterable) -> decltype(int(iterable.size())) {\n return iterable.size();\n}\n\ntemplate <typename T, typename Gen, suisen::constraints_t<suisen::is_same_as_invoke_result<T, Gen, int>> = nullptr>\nauto generate_vector(int n, Gen generator) {\n std::vector<T> v(n);\n for (int i = 0; i < n; ++i) v[i] = generator(i);\n return v;\n}\ntemplate <typename T>\nauto generate_range_vector(T l, T r) {\n return generate_vector(r - l, [l](int i) { return l + i; });\n}\ntemplate <typename T>\nauto generate_range_vector(T n) {\n return generate_range_vector(0, n);\n}\n\ntemplate <typename T>\nvoid sort_unique_erase(std::vector<T>& a) {\n std::sort(a.begin(), a.end());\n a.erase(std::unique(a.begin(), a.end()), a.end());\n}\n\ntemplate <typename InputIterator, typename BiConsumer>\nauto foreach_adjacent_values(InputIterator first, InputIterator last, BiConsumer f) -> decltype(f(*first++, *last), void()) {\n if (first != last) for (auto itr = first, itl = itr++; itr != last; itl = itr++) f(*itl, *itr);\n}\ntemplate <typename Container, typename BiConsumer>\nauto foreach_adjacent_values(Container c, BiConsumer f) -> decltype(c.begin(), c.end(), void()) {\n foreach_adjacent_values(c.begin(), c.end(), f);\n}\n\n// ! other utilities\n\n// x <- min(x, y). returns true iff `x` has chenged.\ntemplate <typename T>\ninline bool chmin(T& x, const T& y) {\n if (y >= x) return false;\n x = y;\n return true;\n}\n// x <- max(x, y). returns true iff `x` has chenged.\ntemplate <typename T>\ninline bool chmax(T& x, const T& y) {\n if (y <= x) return false;\n x = y;\n return true;\n}\n\ntemplate <typename T, std::enable_if_t<std::is_integral_v<T>, std::nullptr_t> = nullptr>\nstd::string bin(T val, int bit_num = -1) {\n std::string res;\n if (bit_num >= 0) {\n for (int bit = bit_num; bit-- > 0;) res += '0' + ((val >> bit) & 1);\n } else {\n for (; val; val >>= 1) res += '0' + (val & 1);\n std::reverse(res.begin(), res.end());\n }\n return res;\n}\n\ntemplate <typename T, std::enable_if_t<std::is_integral_v<T>, std::nullptr_t> = nullptr>\nstd::vector<T> digits_low_to_high(T val, T base = 10) {\n std::vector<T> res;\n for (; val; val /= base) res.push_back(val % base);\n if (res.empty()) res.push_back(T{ 0 });\n return res;\n}\ntemplate <typename T, std::enable_if_t<std::is_integral_v<T>, std::nullptr_t> = nullptr>\nstd::vector<T> digits_high_to_low(T val, T base = 10) {\n auto res = digits_low_to_high(val, base);\n std::reverse(res.begin(), res.end());\n return res;\n}\n\ntemplate <typename T>\nstd::string join(const std::vector<T>& v, const std::string& sep, const std::string& end) {\n std::ostringstream ss;\n for (auto it = v.begin(); it != v.end();) {\n ss << *it;\n if (++it != v.end()) ss << sep;\n }\n ss << end;\n return ss.str();\n}\n\nnamespace suisen {}\nusing namespace suisen;\nusing namespace std;\n\nstruct io_setup {\n io_setup(int precision = 20) {\n std::ios::sync_with_stdio(false);\n std::cin.tie(nullptr);\n std::cout << std::fixed << std::setprecision(precision);\n }\n} io_setup_{};\n\n// ! code from here\n\n#include <algorithm>\n#include <cassert>\n#include <tuple>\n\n#include <vector>\n#include <map>\n#include <unordered_map>\n\nnamespace suisen {\n template <typename T>\n struct FenwickTree {\n int n;\n vector<T> data;\n\n FenwickTree() : FenwickTree(0) {}\n FenwickTree(int n) : n(n), data(n, 0) {}\n int size() {\n return n;\n }\n void chmax(int i, T v) {\n for (++i; i <= n; i += (i & -i)) ::chmax(data[i - 1], v);\n }\n T max(int r) {\n T s = 0;\n for (; r; r -= r & -r) ::chmax(s, data[r - 1]);\n return s;\n }\n };\n} // namespace suisen\n\nnamespace suisen {\n template <typename T>\n struct FenwickTree2DSparse {\n FenwickTree2DSparse() {}\n explicit FenwickTree2DSparse(int x_num) : n(x_num + 1), data(n), points(), pos_x(), pos_y(n) {}\n\n void add_point(int x, int y) {\n built = false;\n pos_x.push_back(x);\n points.emplace_back(x, y);\n }\n\n void build() {\n static constexpr int inf = std::numeric_limits<int>::max();\n built = true;\n pos_x.push_back(inf);\n std::sort(pos_x.begin(), pos_x.end());\n pos_x.erase(std::unique(pos_x.begin(), pos_x.end()), pos_x.end());\n assert(int(pos_x.size()) <= n);\n for (const auto& [x, y] : points) for (int k = comp_x(x) + 1; k <= n; k += k & -k) {\n pos_y[k - 1].push_back(y);\n }\n for (int k = 0; k < n; ++k) {\n pos_y[k].push_back(inf);\n std::sort(pos_y[k].begin(), pos_y[k].end());\n pos_y[k].erase(std::unique(pos_y[k].begin(), pos_y[k].end()), pos_y[k].end());\n data[k] = FenwickTree<T>(pos_y[k].size());\n }\n }\n\n void chmax(int x, int y, const T& val) {\n for (int k = comp_x(x) + 1; k <= n; k += k & -k) {\n data[k - 1].chmax(comp_y(k - 1, y), val);\n }\n }\n T max(int r, int u) {\n T res = 0;\n for (r = comp_x(r); r; r -= r & -r) ::chmax(res, data[r - 1].max(comp_y(r - 1, u)));\n return res;\n }\n\n private:\n int n, m;\n std::vector<FenwickTree<T>> data;\n std::vector<std::pair<int, int>> points;\n std::vector<int> pos_x;\n std::vector<std::vector<int>> pos_y;\n bool built = true;\n\n int comp_x(int x) const {\n return std::lower_bound(pos_x.begin(), pos_x.end(), x) - pos_x.begin();\n }\n int comp_y(int k, int y) const {\n return std::lower_bound(pos_y[k].begin(), pos_y[k].end(), y) - pos_y[k].begin();\n }\n };\n} // namespace suisen\n\nint main() {\n while (true) {\n input(int, m, n);\n input(int, A, B);\n if (m == 0 and n == 0 and A == 0 and B == 0) break;\n n += m;\n int C = ~(1 << 31), M = (1 << 16) - 1;\n auto r = [&]() -> int {\n A = 36969 * (A & M) + (A >> 16);\n B = 18000 * (B & M) + (B >> 16);\n return (C & ((A << 16) + B)) % 1000000;\n };\n\n vector<int> x(n), y(n), z(n);\n rep(i, n) {\n if (i < m) {\n read(x[i], y[i], z[i]);\n } else {\n x[i] = r();\n y[i] = r();\n z[i] = r();\n }\n }\n\n FenwickTree2DSparse<int> ft(n);\n rep(i, n) ft.add_point(y[i], z[i]);\n ft.build();\n\n vector<int> p(n);\n iota(all(p), 0);\n sort(all(p), [&](int i, int j) {\n if (x[i] != x[j]) return x[i] < x[j];\n if (y[i] != y[j]) return y[i] > y[j];\n return z[i] > z[j];\n }\n );\n\n for (int i : p) {\n ft.chmax(y[i], z[i], ft.max(y[i], z[i]) + 1);\n }\n\n print(ft.max(numeric_limits<int>::max(), numeric_limits<int>::max()));\n }\n\n return 0;\n}", "accuracy": 1, "time_ms": 3940, "memory_kb": 75080, "score_of_the_acc": -0.8577, "final_rank": 15 }, { "submission_id": "aoj_1341_6365882", "code_snippet": "#include <bits/stdc++.h>\n// #define int long long\n#define ff first\n#define ss second\n#define ll long long\n#define ld long double\n#define pb push_back\n#define eb emplace_back\n#define pii pair<int, int>\n#define pll pair<ll, ll>\n#define ti tuple<int, int, int>\n#define vi vector<int>\n#define vl vector<ll>\n#define vii vector<pii>\n#define sws ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);\n#define endl '\\n'\n#define teto(a, b) (((a)+(b)-1)/(b))\n#define all(x) x.begin(), x.end()\n#define forn(i, n) for(int i = 0; i < (int)n; i++)\n#define forne(i, a, b) for(int i = a; i <= b; i++)\n#define dbg(msg, var) cerr << msg << \" \" << var << endl;\n\n#pragma GCC optimize(\"Ofast\")\n#pragma GCC target(\"avx,avx2,fma\")\n\nusing namespace std;\n\nconst int MAX = 2e5+10;\nconst ll MOD = 998244353;\nconst int INF = 0x3f3f3f3f;\nconst ll LLINF = 0x3f3f3f3f3f3f3f3f;\nconst ld EPS = 1e-10;\nconst ld PI = acos(-1);\n\n// End Template //\n\nint a, b, C = ~(1<<31), M = (1<<16)-1;\nint r() {\n a = 36969 * (a & M) + (a >> 16);\n b = 18000 * (b & M) + (b >> 16);\n return (C & ((a << 16) + b)) % 1000000;\n}\n\nstruct Segtree{\n vi t;\n int n;\n\n Segtree(int n){\n this->n = n;\n t.assign(2*n, 0);\n }\n\n int merge(int a, int b){\n return max(a, b);\n }\n\n void build(){\n for(int i=n-1;i>0;i--)\n t[i] = merge(t[i<<1], t[i<<1|1]);\n }\n\n int query(int l, int r){\n int resl = -INF, resr = -INF;\n for(l+=n, r+=n+1; l<r; l>>=1, r>>=1){\n if(l&1) resl = merge(resl, t[l++]);\n if(r&1) resr = merge(t[--r], resr);\n }\n return merge(resl, resr);\n }\n\n void update(int p, int value){\n p+=n;\n for(t[p]=max(t[p], value); p >>= 1;)\n t[p] = merge(t[p<<1], t[p<<1|1]);\n }\n};\n\nstruct point{\n int x, y, z, id;\n bool left;\n point(int x=0, int y=0, int z=0): x(x), y(y), z(z){\n left = false;\n }\n bool operator<(point &o){\n if(x != o.x) return x < o.x;\n if(y != o.y) return y > o.y;\n return z < o.z;\n }\n};\n\n\nvoid cdq(int l, int r, vector<point> &a, vi &dp){\n if(l==r) return;\n\n int mid = (l+r) / 2;\n\n cdq(l, mid, a, dp);\n\n // compress z\n set<int> uz; map<int, int> idz;\n for(int i=l;i<=r;i++) uz.insert(a[i].z);\n int id = 0;\n for(auto z: uz) idz[z] = id++;\n\n vector<point> tmp;\n for(int i=l;i<=r;i++){\n tmp.pb(a[i]);\n tmp.back().x = 0;\n tmp.back().z = idz[tmp.back().z];\n if(i<=mid)\n tmp.back().left = true;\n }\n\n Segtree st(id);\n\n sort(tmp.rbegin(), tmp.rend());\n\n for(auto t: tmp){\n // cout << \"tmp = \" << t.x << \" \" << t.y << \" \" << t.z << \" \" << t.left << endl;\n if(t.left){\n st.update(t.z, dp[t.id]);\n }else{\n // cout << \"ok\\n\";\n dp[t.id] = max(dp[t.id], st.query(0, t.z-1)+1);\n }\n }\n\n cdq(mid+1, r, a, dp);\n}\n\n\nint32_t main()\n{sws;\n\n ll m, n;\n while(cin >> m >> n >> a >> b){\n if(m==0 and n==0 and a==0 and b==0) break;\n\n vector<point> vet(n+m);\n for(int i=0;i<m;i++){\n cin >> vet[i].x >> vet[i].y >> vet[i].z;\n }\n for(int i=m;i<n+m;i++){\n vet[i].x = r();\n vet[i].y = r();\n vet[i].z = r();\n }\n\n sort(vet.begin(), vet.end());\n\n for(int i=0;i<n+m;i++)\n vet[i].id = i;\n\n vi dp(n+m, 1);\n\n cdq(0, n+m-1, vet, dp);\n\n int ans = 0;\n for(int i=0;i<n+m;i++)\n ans = max(ans, dp[i]);\n\n\n // for(int i=0;i<n+m;i++)\n // cout << dp[i] << \" \";\n // cout << endl;\n\n cout << ans << endl;\n \n }\n\n return 0;\n}\n\n// overflow\n// check border cases\n// check limits (when initializing the answer too)\n// check variables names, mod missing\n// using set as multiset\n// remember to permute the positions\n// equal points/collinear points\n\n// no ideas?\n// try bb, dp\n// invert the problem (calculate total - wrong)\n// transform it in a graph somehow\n// naive/brute\n// when u find a dead end, read the statment again,\n// to see if u understood the problem right.", "accuracy": 1, "time_ms": 7680, "memory_kb": 82460, "score_of_the_acc": -1.4406, "final_rank": 17 }, { "submission_id": "aoj_1341_6260185", "code_snippet": "#include<bits/stdc++.h>\n\nusing namespace std;\n\n#define _ ios_base::sync_with_stdio(0);cin.tie(0);\n#define endl '\\n'\n\ntypedef long long ll;\n\nconst int INF = 0x3f3f3f3f;\nconst ll LINF = 0x3f3f3f3f3f3f3f3f;\n\ntypedef tuple<int, int, int> T;\n\nconst int MAX = 3e5 + 10;\nconst int MAX2 = 1e6 + 10;\n\nint seg[2*MAX2];\nint N = MAX2;\n\nint query(int a, int b) {\n\tint ret = 0;\n\tfor(a += N, b += N; a <= b; ++a /= 2, --b /= 2) {\n\t\tif (a % 2 == 1) ret = max(ret, seg[a]);\n\t\tif (b % 2 == 0) ret = max(ret, seg[b]);\n\t}\n\treturn ret;\n}\n\nvoid update(int p, int x) {\n\tp += N;\n\tif (x == 0) seg[p] = 0;\n\telse seg[p] = max(seg[p], x);\n\twhile (p /= 2) seg[p] = max(seg[2*p], seg[2*p+1]);\n}\n\nvoid lis2d(vector<vector<tuple<int, int, int>>>& v, vector<int>& dp, int l, int r) {\n\tif (l == r) {\n\t\tfor (int i = 0; i < v[l].size(); i++) {\n\t\t\tint ii = get<2>(v[l][i]);\n\t\t\tdp[ii] = max(dp[ii], 1);\n\t\t}\n\t\treturn;\n\t}\n\t\n\tint m = (l+r)/2;\n\tlis2d(v, dp, l, m);\n\t\n\tvector<tuple<int, int, int>> vv[2];\n\tfor (int i = l; i <= r; i++) for (int j = 0; j < v[i].size(); j++)\n\t\tvv[i > m].push_back(v[i][j]);\n\tsort(vv[0].begin(), vv[0].end());\n\tsort(vv[1].begin(), vv[1].end());\n\t\n\tint mini = INF, i = 0;\n\tfor (auto it : vv[1]) {\n\t\tint y, z, id;\n\t\ttie(y, z, id) = it;\n\t\twhile (i < vv[0].size() and get<0>(vv[0][i]) < y) {\n\t\t\tint y2, z2, id2;\n\t\t\ttie(y2, z2, id2) = vv[0][i];\n\t\t\tmini = min(mini, z2);\n\t\t\tupdate(z2, dp[id2]);\t\n\t\t\ti++;\n\t\t}\n\t\tint q = query(0, z-1);\n\t\tdp[id] = max(dp[id], q + 1);\n\t}\n\n\tfor (i = 0; i < vv[0].size(); i++) {\n\t\tint y, z, id;\n\t\ttie(y, z, id) = vv[0][i];\n\t\tupdate(z, 0);\n\t}\n\tlis2d(v, dp, m+1, r);\n}\n\nvector<int> solve(vector<tuple<int, int, int>> v) {\n\tint n = v.size();\n\tvector<tuple<int, int, int, int>> vv;\n\tfor (int i = 0; i < n; i++) {\n\t\tint x, y, z;\n\t\ttie(x, y, z) = v[i];\n\t\tvv.emplace_back(x, y, z, i);\n\t}\n\tsort(vv.begin(), vv.end());\n\n\tvector<vector<tuple<int, int, int>>> V;\n\tfor (int i = 0; i < n; i++) {\n\t\tint j = i;\n\t\tV.emplace_back();\n\t\twhile (j < n and get<0>(vv[j]) == get<0>(vv[i])) {\n\t\t\tint x, y, z, id;\n\t\t\ttie(x, y, z, id) = vv[j++];\n\t\t\tV.back().emplace_back(y, z, id);\n\t\t}\n\t\ti = j-1;\n\t}\n\tvector<int> dp(n);\n\tlis2d(V, dp, 0, V.size()-1);\n\treturn dp;\n}\n\nint main() { _\n\tint m, n, A, B;\n\twhile (cin >> m >> n >> A >> B) {\n\t\tif (n == 0 and m == 0 and A == 0 and B == 0) return 0;\n\t\t\n\t\tvector<tuple<int, int, int>> v;\n\n\t\tfor (int i = 0; i < m; i++) {\n\t\t\tint x, y, z; cin >> x >> y >> z;\n\t\t\tv.emplace_back(x, y, z);\n\t\t}\n\n\t\t{\n\t\t\tint a = A, b = B, C = ~(1<<31), M = (1<<16)-1;\n\t\t\tfor (int i = 0; i < n; i++) {\n\t\t\t\tvector<int> w;\n\t\t\t\tfor (int j = 0; j < 3; j++) {\n\t\t\t\t\ta = 36969 * (a & M) + (a >> 16);\n\t\t\t\t\tb = 18000 * (b & M) + (b >> 16);\n\t\t\t\t\tw.push_back((C & ((a << 16) + b)) % 1000000);\n\t\t\t\t}\t\t\n\t\t\t\tv.emplace_back(w[0], w[1], w[2]);\n\t\t\t}\n\t\t}\n\n\t\tauto dp = solve(v);\n\n\t\tint ans = 0;\n\t\tfor (int i = 0; i < n+m; i++) ans = max(ans, dp[i]);\n\t\tcout << ans << endl;\n\t}\n\texit(0);\n}", "accuracy": 1, "time_ms": 3000, "memory_kb": 51064, "score_of_the_acc": -0.5673, "final_rank": 13 }, { "submission_id": "aoj_1341_6259434", "code_snippet": "#include<bits/stdc++.h>\n\nusing namespace std;\n\n#define _ ios_base::sync_with_stdio(0);cin.tie(0);\n#define endl '\\n'\n\ntypedef long long ll;\n\nconst int INF = 0x3f3f3f3f;\nconst ll LINF = 0x3f3f3f3f3f3f3f3f;\n\ntypedef tuple<int, int, int> T;\n\nconst int MAX = 3e5 + 10;\nconst int MAX2 = 1e6 + 10;\n\nvector<vector<T>> V;\nint dp[MAX];\n\nint seg[2*MAX2];\nint N = MAX2;\n\nint query(int a, int b) {\n\tint ret = 0;\n\tfor(a += N, b += N; a <= b; ++a /= 2, --b /= 2) {\n\t\tif (a % 2 == 1) ret = max(ret, seg[a]);\n\t\tif (b % 2 == 0) ret = max(ret, seg[b]);\n\t}\n\treturn ret;\n}\n\nvoid update(int p, int x) {\n\tp += N;\n\tif (x == 0) seg[p] = 0;\n\telse seg[p] = max(seg[p], x);\n\twhile (p /= 2) seg[p] = max(seg[2*p], seg[2*p+1]);\n}\n\nvoid lis2d(int l, int r) {\n\tif (l == r) {\n\t\tfor (int i = 0; i<V[l].size(); i++) {\n\t\t\tint ii = get<2>(V[l][i]);\n\t\t\tdp[ii] = max(dp[ii], 1);\n\t\t}\n\t\treturn;\n\t}\n\t\n\tint m = (l+r)/2;\n\tlis2d(l, m);\n\t\n\tvector<T> v1, v2;\n\tfor (int i = l; i <= r; i++) for (int j = 0; j < V[i].size(); j++) { \n\t\tif (i <= m) v1.push_back(V[i][j]);\n\t\telse v2.push_back(V[i][j]);\n\t}\n\tsort(v1.begin(), v1.end());\n\tsort(v2.begin(), v2.end());\n\t\n\tint mini = INF;\n\n\tint i = 0;\n\tfor (auto it : v2) {\n\t\tint y, z, id;\n\t\ttie(y, z, id) = it;\n\t\twhile (i < v1.size() and get<0>(v1[i]) < y) {\n\t\t\tint y2, z2, id2;\n\t\t\ttie(y2, z2, id2) = v1[i];\n\t\t\tmini = min(mini, z2);\n\t\t\tupdate(z2, dp[id2]);\t\n\t\t\ti++;\n\t\t}\n\t\tint q = query(0, z-1);\n\t\tdp[id] = max(dp[id], q + 1);\n\t}\n\n\tfor (i = 0; i<v1.size(); i++) {\n\t\tint y, z, id;\n\t\ttie(y, z, id) = v1[i];\n\t\tupdate(z, 0);\n\t}\n\tlis2d(m+1, r);\n}\n\nint main() { _\n\tint m, n, A, B;\n\twhile (cin >> m >> n >> A >> B) {\n\t\tif (n == 0 and m == 0 and A == 0 and B == 0) return 0;\n\t\t\n\t\tfor (int i = 0; i<n+m; i++) dp[i] = 0;\n\t\tV.clear();\n\t\tvector<T> v;\n\n\t\tfor (int i = 0; i<m; i++) {\n\t\t\tint x, y, z; cin >> x >> y >> z;\n\t\t\tv.emplace_back(x, y, z);\n\t\t}\n\n\t\t{\n\t\t\tint a = A, b = B, C = ~(1<<31), M = (1<<16)-1;\n\t\t\tfor (int i = 0; i<n; i++) {\n\t\t\t\tvector<int> w;\n\t\t\t\tfor (int j = 0; j<3; j++) {\n\t\t\t\t\ta = 36969 * (a & M) + (a >> 16);\n\t\t\t\t\tb = 18000 * (b & M) + (b >> 16);\n\t\t\t\t\tw.push_back((C & ((a << 16) + b)) % 1000000);\n\t\t\t\t}\t\t\n\t\t\t\tv.emplace_back(w[0], w[1], w[2]);\n\t\t\t}\n\t\t}\n\n\t\tsort(v.begin(), v.end());\n\n\t\tfor (int i = 0; i < n+m; i++) {\n\t\t\tint j = i;\n\t\t\tV.emplace_back();\n\t\t\twhile (j < n+m and get<0>(v[j]) == get<0>(v[i])) {\n\t\t\t\tV.back().emplace_back(get<1>(v[j]), get<2>(v[j]), j);\n\t\t\t\tj++;\n\t\t\t}\n\t\t\ti = j-1;\n\t\t}\n\t\n\t\tlis2d(0, V.size()-1);\n\t\tint ans = 0;\n\t\tfor (int i = 0; i<n+m; i++) ans = max(ans, dp[i]);\n\t\tcout << ans << endl;\n\t}\n\texit(0);\n}", "accuracy": 1, "time_ms": 2890, "memory_kb": 40876, "score_of_the_acc": -0.4854, "final_rank": 10 }, { "submission_id": "aoj_1341_6252224", "code_snippet": "#include<bits/stdc++.h>\nusing namespace::std;\n\n// Currently: WA :v\n\nconst int C = ~(1 << 31);\nconst int M = (1 << 16) - 1;\nconst int N = 300000 + 5;\n\nint n;\nint m;\nint a;\nint b;\nint A;\nint B;\nint len;\nint memo[N];\nset<pair<int, int>> ft[N];\n\nint r(){\n\ta = 36969 * (a & M) + (a >> 16);\n\tb = 18000 * (b & M) + (b >> 16);\n\treturn (C & ((a << 16) + b)) % 1000000;\n}\n\nvoid compress(vector<int> &v){\n\tvector<int> values(v.begin(), v.end());\n\tsort(values.begin(), values.end());\n\tvalues.erase(unique(values.begin(), values.end()), values.end());\n\tfor(int i = 0; i < v.size(); i++){\n\t\tv[i] = lower_bound(values.begin(), values.end(), v[i]) - values.begin();\n\t}\n}\n\nint get(int at, int val){\n\tif(ft[at].empty() or (*ft[at].begin()).first > val) return 0;\n\tset<pair<int, int>>::iterator it = ft[at].lower_bound(make_pair(val, -1));\n\tif(it == ft[at].end() or (*it).first > val) it--;\n\treturn (*it).second;\n}\n\t\nint query(int y, int z){\n\ty++; z++;\n\tint res = 0;\n\tfor(int j = y; j > 0; j &= j - 1){\n\t\tres = max(res, get(j, z));\n\t}\n\treturn res;\n}\n\nvoid insert(int at, int pos, int val){\n\tif(ft[at].empty()){\n\t\tft[at].emplace(make_pair(pos, val));\n\t\treturn;\n\t}\n\tset<pair<int, int>>::iterator it = ft[at].lower_bound(make_pair(pos, -1));\n\n\tif(it != ft[at].end() and (*it).first == pos and (*it).second >= val) return;\n\tset<pair<int, int>>::iterator leq = it;\n\tif(leq != ft[at].begin()) leq--;\n\tif((*leq).first < pos and (*leq).second >= val) return;\n\tvector<pair<int, int>> to_remove;\n\twhile(it != ft[at].end() and (*it).second <= val){\n\t\tto_remove.emplace_back(*it);\n\t\tit++;\n\t}\n\tfor(auto e : to_remove) ft[at].erase(e);\n\tft[at].emplace(make_pair(pos, val));\n}\n\nvoid update(int y, int z, int val){\n\ty++; z++;\n\tfor(int j = y; j <= len; j += (-j) & j){\n\t\tinsert(j, z, val);\n\t}\n}\n\nint solve(vector<int> &x, vector<int> &y, vector<int> &z){\n\tvector<int> p(len);\n\tiota(p.begin(), p.end(), 0);\n\tsort(p.begin(), p.end(), [&] (int i, int j){\n\t\tif(x[i] != x[j]) return x[i] < x[j];\n\t\tif(y[i] != y[j]) return y[i] < y[j];\n\t\tif(z[i] != z[j]) return z[i] < z[j];\n\t\treturn false;\n\t});\n\tint ans = 0;\n\tint l = 0, r = 0;\n\twhile(l < len){\n\t\twhile(r < len and x[p[l]] == x[p[r]]) r++;\n\t\tfor(int i = l; i < r; i++){\n\t\t\tint at = p[i];\n\t\t\tmemo[i] = 1 + query(y[at] - 1, z[at] - 1);\n\t\t\tif(ans < memo[i]) ans = memo[i];\n\t\t}\n\t\tfor(int i = l; i < r; i++){\n\t\t\tint at = p[i];\n\t\t\tupdate(y[at], z[at], memo[i]);\n\t\t}\n\t\tl = r;\n\t}\n\treturn ans;\n}\n\nvoid clear(){\n\tfor(int i = 0; i <= len; i++){\n\t\tft[i].clear();\n\t}\n}\n\nint main(){\n\twhile(scanf(\"%d %d %d %d\", &m, &n, &a, &b) == 4 and m + n){\n\t\tlen = n + m;\n\t\tvector<int> x(len), y(len), z(len);\n\t\tfor(int i = 0; i < m; i++){\n\t\t\tscanf(\"%d %d %d\", &x[i], &y[i], &z[i]);\n\t\t}\n\t\tfor(int i = m; i < len; i++){\n\t\t\tx[i] = r();\n\t\t\ty[i] = r();\n\t\t\tz[i] = r();\n\t\t}\n\t\tcompress(x); compress(y); compress(z);\n\t\tprintf(\"%d\\n\", solve(x, y, z));\n\t\tclear();\n\t}\n\treturn 0;\n}", "accuracy": 1, "time_ms": 3730, "memory_kb": 66916, "score_of_the_acc": -0.7746, "final_rank": 14 }, { "submission_id": "aoj_1341_6044530", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\ntypedef long long ll;\ntemplate<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return true; } return false; }\ntemplate<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return true; } return false; }\n#define all(x) (x).begin(),(x).end()\n#define fi first\n#define se second\n#define mp make_pair\n#define si(x) int(x.size())\nconst int mod=1000000007,MAX=1005,INF=1<<29;\n\nint main(){\n \n std::ifstream in(\"text.txt\");\n std::cin.rdbuf(in.rdbuf());\n cin.tie(0);\n ios::sync_with_stdio(false);\n \n while(1){\n int N,nn,A,B;cin>>N>>nn>>A>>B;\n if(N+nn==0) break;\n vector<vector<int>> S(N);\n for(int i=0;i<N;i++){\n S[i].resize(3);\n for(int j=0;j<3;j++) cin>>S[i][j];\n }\n int a = A, b = B, C = ~(1<<31), M = (1<<16)-1;\n for(int i=0;i<nn;i++){\n S.push_back({});\n for(int j=0;j<3;j++){\n a = 36969 * (a & M) + (a >> 16);\n b = 18000 * (b & M) + (b >> 16);\n S.back().push_back((C & ((a << 16) + b)) % 1000000);\n }\n }\n \n sort(all(S),[](auto a,auto b){\n if(a[0]==b[0]){\n if(a[1]==b[1]) return a[2]>b[2];\n else return a[1]>b[1];\n }else{\n return a[0]<b[0];\n }\n });\n \n N=si(S);\n vector<set<pair<int,int>>> SE(N+1);\n for(int i=0;i<=N;i++){\n if(i) SE[i].insert(mp(-INF,INF));\n else SE[i].insert(mp(-INF,-INF+1));\n SE[i].insert(mp(INF,-INF));\n }\n for(int i=0;i<N;i++){\n int left=0,right=i+1;\n pair<int,int> x=mp(S[i][1],S[i][2]);\n while(right-left>1){\n int mid=(left+right)/2;\n auto it=SE[mid].lower_bound(mp(x.fi,-INF));\n it--;\n if((*it).se<x.se) left=mid;\n else right=mid;\n }\n int mid=right;\n auto it=SE[mid].lower_bound(x);\n it--;\n bool f=((*it).se>x.se);\n it++;\n while(it!=SE[mid].end()){\n if((*it).se>=x.se){\n it=SE[mid].erase(it);\n }else{\n break;\n }\n }\n if(f) SE[mid].insert(x);\n }\n \n int ans=-1;\n for(int i=0;i<=N;i++){\n for(auto a:SE[i]){\n if(abs(a.fi)<INF) ans=i;\n }\n }\n cout<<ans<<\"\\n\";\n }\n}", "accuracy": 1, "time_ms": 1940, "memory_kb": 67864, "score_of_the_acc": -0.5247, "final_rank": 12 }, { "submission_id": "aoj_1341_3904383", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n#define REP(i,n) for(int i=0; i<(int)(n); i++)\n\nusing ll = long long;\n\nint a,b;\n// const int C = ~(1<<31);\nconst int C = (1<<30) + ((1<<30) - 1);\nconst int M = (1<<16)-1;\nint r() {\n\ta = 36969 * (a & M) + (a >> 16);\n\tb = 18000 * (b & M) + (b >> 16);\n\treturn (C & ((a << 16) + b)) % 1000000;\n}\n\nusing pii = pair<int,int>;\n\npair<int,pii> points[325252];\n\nint main(){\n\twhile(true){\n\t\tint n,m;\n\t\tscanf(\"%d%d%d%d\",&n,&m,&a,&b);\n\t\tif(n+m+a+b==0)break;\n\t\tREP(i,n){\n\t\t\tint x,y,z;\n\t\t\tscanf(\"%d%d%d\",&x,&y,&z);\n\t\t\tx++; y++; z++;\n\t\t\tpoints[i] = make_pair(x,pii(y,z));\n\t\t}\n\t\tREP(i,m){\n\t\t\tint x = r();\n\t\t\tint y = r();\n\t\t\tint z = r();\n\t\t\tx++; y++; z++;\n\t\t\tpoints[i+n] = make_pair(x,pii(y,z));\n\t\t}\n\t\tn += m;\n\t\tsort(points, points+n, [&](pair<int,pii> a, pair<int,pii> b){\n\t\t\tif(a.first != b.first)return a.first < b.first;\n\t\t\treturn a.second > b.second;\n\t\t});\n\t\tconst int N = 1000025;\n\t\tstd::unordered_map<int,std::map<int,int>> bit;\n\t\tauto add = [&](int y, int z, int v){\n\t\t\tfor(; y<N; y+=y&-y){\n\t\t\t\t// add to y\n\t\t\t\tif(bit.count(y)==0){\n\t\t\t\t\tbit[y] = std::map<int,int>();\n\t\t\t\t\tbit[y][-1] = -252521;\n\t\t\t\t}\n\t\t\t\tstd::map<int,int> &mp = bit[y];\n\t\t\t\tauto it = mp.upper_bound(z); it--;\n\t\t\t\tif(it->second >= v)continue;\n\t\t\t\tmp[z] = v;\n\t\t\t\tit = mp.upper_bound(z);\n\t\t\t\twhile(it != mp.end() && it->second <= v){\n\t\t\t\t\tmp.erase(it++);\n\t\t\t\t}\n\t\t\t}\n\t\t};\n\t\tauto query = [&](int y, int z){\n\t\t\tint ans = 0;\n\t\t\tfor(; y>0; y-=y&-y){\n\t\t\t\t// get from y\n\t\t\t\tif(bit.count(y)==0)continue;\n\t\t\t\tstd::map<int,int> &mp = bit[y];\n\t\t\t\tauto it = mp.upper_bound(z); it--;\n\t\t\t\tans = max(ans, it->second);\n\t\t\t}\n\t\t\treturn ans;\n\t\t};\n\t\tint ans = 0;\n\t\tREP(i,n){\n\t\t\tint x = points[i].first;\n\t\t\tint y = points[i].second.first;\n\t\t\tint z = points[i].second.second;\n\t\t\tint v = query(y-1, z-1) + 1;\n\t\t\tans = max(ans, v);\n\t\t\tadd(y,z,v);\n\t\t}\n\t\tprintf(\"%d\\n\",ans);\n\t}\n\treturn 0;\n}", "accuracy": 1, "time_ms": 7480, "memory_kb": 110408, "score_of_the_acc": -1.5934, "final_rank": 18 }, { "submission_id": "aoj_1341_3743531", "code_snippet": "#include <iostream>\n#include <vector>\n#include <cmath>\n#include <string>\n#include <climits>\n#include <iomanip>\n#include <algorithm>\n#include <queue>\n#include <map>\n#include <tuple>\n#include <iostream>\n#include <deque>\n#include <array>\n#include <set>\n#include <functional>\n#include <memory>\n#include <stack>\n\nstruct Point3 {\n\tint x, y, z;\n};\nstruct Point2 {\n\tint x, y;\n\tbool operator<(const Point2 other) const { return x < other.x; }\n};\nclass NonChainablePoints {\n\tstd::set<Point2> set;\npublic:\n\tNonChainablePoints() {};\n\tbool can_chain(const Point2& point) const {\n\t\tauto hint = set.lower_bound(point);\n\t\treturn hint != set.begin() && (--hint)->y < point.y;\n\t}\n\tvoid insert(const Point2& point) {\n\t\tauto hint = set.lower_bound(point);\n\t\tif (hint == set.end() || hint->x != point.x || hint->y >= point.y) {\n\t\t\twhile (hint != set.end() && hint->y >= point.y) hint = set.erase(hint);\n\t\t\tset.insert(point);\n\t\t}\n\t}\n\tbool is_empty() const { return set.empty(); }\n\tbool is_not_empty() const { return !is_empty(); }\n};\nint r(int& a, int& b) {\n\tconstexpr int C = ~(1 << 31);\n\tconstexpr int M = (1 << 16) - 1;\n\ta = 36969 * (a & M) + (a >> 16);\n\tb = 18000 * (b & M) + (b >> 16);\n\treturn (C & ((a << 16) + b)) % 1000000;\n}\nvoid append_points(std::vector<Point3>& points, int a, int b, const int n) {\n\tfor (auto i = 0; i < n; ++i) {\n\t\tPoint3 p;\n\t\tp.x = r(a, b);\n\t\tp.y = r(a, b);\n\t\tp.z = r(a, b);\n\t\tpoints.push_back(p);\n\t}\n}\nint solve(const int m, const int n, const int a, const int b) {\n\tstd::vector<Point3> points(m);\n\tfor (auto& p : points) std::cin >> p.x >> p.y >> p.z;\n\tappend_points(points, a, b, n);\n\tstd::sort(points.begin(), points.end(), [](const Point3& a, const Point3& b) {return (a.z == b.z) ? a.x > b.x : a.z < b.z; });\n\tstd::vector<NonChainablePoints> sets(m + n);\n\tfor (const auto point : points) {\n\t\tconst Point2 p{ point.x, point.y };\n\t\tauto left = 0;\n\t\tauto right = sets.size();\n\t\twhile (left < right) {\n\t\t\tauto mid = (left + right) / 2;\n\t\t\tif (sets[mid].can_chain(p)) left = mid + 1;\n\t\t\telse right = mid;\n\t\t}\n\t\tsets[right].insert(p);\n\t}\n\tfor (auto i = 0; i < sets.size(); ++i) {\n\t\tif (sets[i].is_empty()) return i;\n\t}\n\treturn sets.size();\n}\nint main() {\n\tstd::vector<int>result;\n\twhile (true) {\n\t\tint m, n, a, b;\n\t\tstd::cin >> m >> n >> a >> b;\n\t\tif (m == 0 && n == 0) break;\n\t\tresult.push_back(solve(m, n, a, b));\n\t}\n\tfor (const auto r : result) std::cout << r << '\\n';\n}", "accuracy": 1, "time_ms": 960, "memory_kb": 26876, "score_of_the_acc": -0.1184, "final_rank": 8 }, { "submission_id": "aoj_1341_3659947", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\nint a, b;\nconstexpr int C = ~(1 << 31);\nconstexpr int M = (1 << 16) - 1;\n\nint r() {\n a = 36969 * (a & M) + (a >> 16);\n b = 18000 * (b & M) + (b >> 16);\n return (C & ((a << 16) + b)) % 1000000;\n}\n\nstruct point {\n int y, z;\n\n point(int y_, int z_) : y(y_), z(z_) {}\n bool operator<(point const& p) const {\n return y < p.y;\n }\n};\n\nint main() {\n ios_base::sync_with_stdio(false); cin.tie(0);\n\n int m, n;\n while(cin >> m >> n >> a >> b, n + m) {\n vector<tuple<int, int, int>> ps(n + m);\n for(int i = 0; i < m; ++i) {\n int x, y, z; cin >> x >> y >> z;\n ps[i] = make_tuple(x, -y, -z);\n }\n for(int i = m; i < n + m; ++i) {\n const int x = r(), y = r(), z = r();\n ps[i] = make_tuple(x, -y, -z);\n }\n sort(begin(ps), end(ps));\n vector<point> ps2;\n for(int i = 0; i < n + m; ++i) {\n ps2.emplace_back(-get<1>(ps[i]), -get<2>(ps[i]));\n }\n\n int ans = 0;\n vector<set<point>> dp(n + m + 1);\n dp[0].emplace(0, 0);\n for(auto const& p : ps2) {\n auto check = [&] (int i) {\n auto it = dp[i].lower_bound(p);\n if(it == dp[i].begin()) return false;\n it = prev(it);\n return p.y > it->y && p.z > it->z;\n };\n int lb = 0, ub = n + m;\n while(ub - lb > 1) {\n const int mid = (ub + lb) >> 1;\n (check(mid) ? lb : ub) = mid;\n }\n auto it = dp[ub].lower_bound(p);\n while(it != dp[ub].end() && it->z >= p.z) {\n it = dp[ub].erase(it);\n }\n if(it == dp[ub].begin() || prev(it)->z > p.z) {\n dp[ub].insert(p);\n }\n ans = max(ans, ub);\n }\n\n cout << ans << endl;\n }\n}", "accuracy": 1, "time_ms": 750, "memory_kb": 27212, "score_of_the_acc": -0.0906, "final_rank": 6 } ]
aoj_1343_cpp
Problem I: Hidden Tree Consider a binary tree whose leaves are assigned integer weights. Such a tree is called balanced if, for every non-leaf node, the sum of the weights in its left subtree is equal to that in the right subtree. For instance, the tree in the following figure is balanced. Figure I.1. A balanced tree A balanced tree is said to be hidden in a sequence A , if the integers obtained by listing the weights of all leaves of the tree from left to right form a subsequence of A . Here, a subsequence is a sequence that can be derived by deleting zero or more elements from the original sequence without changing the order of the remaining elements. For instance, the balanced tree in the figure above is hidden in the sequence 3 4 1 3 1 2 4 4 6, because 4 1 1 2 4 4 is a subsequence of it. Now, your task is to find, in a given sequence of integers, the balanced tree with the largest number of leaves hidden in it. In fact, the tree shown in Figure I.1 has the largest number of leaves among the balanced trees hidden in the sequence mentioned above. Input The input consists of multiple datasets. Each dataset represents a sequence A of integers in the format N A 1 A 2 . . . A N where 1 ≤ N ≤ 1000 and 1 ≤ A i ≤ 500 for 1 ≤ i ≤ N . N is the length of the input sequence, and A i is the i -th element of the sequence. The input ends with a line consisting of a single zero. The number of datasets does not exceed 50. Output For each dataset, find the balanced tree with the largest number of leaves among those hidden in A , and output, in a line, the number of its leaves. Sample Input 9 3 4 1 3 1 2 4 4 6 4 3 12 6 3 10 10 9 8 7 6 5 4 3 2 1 11 10 9 8 7 6 5 4 3 2 1 1 8 1 1 1 1 1 1 1 1 0 Output for the Sample Input 6 2 1 5 8
[ { "submission_id": "aoj_1343_10853732", "code_snippet": "#include<map>\n#include<set>\n#include<cmath>\n#include<queue>\n#include<cctype>\n#include<cstdio>\n#include<vector>\n#include<cstdlib>\n#include<cstring>\n#include<algorithm>\ntypedef long long LL;\n#define MAXN 1010\nconst int mt = 131072;\nint a[MAXN], b[MAXN], len[MAXN], f[mt + 10], N, M;\nint process() {\n\tmemset(f, -1, sizeof(f));\n\tf[0] = 0;\n\tfor(int i = 0; i < N; i ++) {\n\t\tint l = len[i];\n\t\tif(l > 0) {\n\t\t\tfor(int j = mt; j > 0; j -= l) {\n\t\t\t\tif(f[j - l] > -1 && f[j - l] + 1 > f[j]) {\n\t\t\t\t\tf[j] = f[j - l] + 1;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n\tint ans = 0;\n\tfor(int i = mt; i > 0; i >>= 1) {\n\t\tif(f[i] != -1) {\n\t\t\tans = std::max(ans, f[i]);\n\t\t}\n\t}\n\treturn ans;\n}\nint main() {\n\t//freopen(\"test.in\", \"rb\", stdin);\n\twhile(scanf(\"%d\", &N), N) {\n\t\tfor(int i = 0; i < N; i ++) {\n\t\t\tscanf(\"%d\", &a[i]);\n\t\t\tb[i] = a[i];\n\t\t}\n\t\tstd::sort(b, b + N);\n\t\tM = std::unique(b, b + N) - b;\n\t\tint ans = 0;\n\t\tfor(int i = 0; i < M; i ++) {\n\t\t\tint v = b[i];\n\t\t\tfor(int j = 0; j < N; j ++) {\n\t\t\t\tlen[j] = 0;\n\t\t\t\tint x = a[j];\n\t\t\t\tif(x % v == 0) {\n\t\t\t\t\tx /= v;\n\t\t\t\t\tint l = 1;\n\t\t\t\t\twhile((x & 1) == 0) {\n\t\t\t\t\t\tx >>= 1, l <<= 1;\n\t\t\t\t\t}\n\t\t\t\t\tif(x == 1) {\n\t\t\t\t\t\tlen[j] = l;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t\tans = std::max(ans, process());\n\t\t}\n\t\tprintf(\"%d\\n\", ans);\n\t}\n\treturn 0;\n}", "accuracy": 1, "time_ms": 3390, "memory_kb": 3512, "score_of_the_acc": -0.4723, "final_rank": 10 }, { "submission_id": "aoj_1343_10352657", "code_snippet": "#ifndef LOCAL\n#pragma GCC optimize (\"Ofast\")\n#pragma GCC optimize (\"unroll-loops\")\n#endif\n\n#include <bits/stdc++.h>\nusing namespace std;\n\nusing ll=long long;\n#define int ll\n\nbool dbg=false;\n\n#define rng(i,a,b) for(int i=int(a);i<int(b);i++)\n#define rep(i,b) rng(i,0,b)\n#define gnr(i,a,b) for(int i=int(b)-1;i>=int(a);i--)\n#define per(i,b) gnr(i,0,b)\n#define pb push_back\n#define eb emplace_back\n#define a first\n#define b second\n#define bg begin()\n#define ed end()\n#define all(x) x.bg,x.ed\n#define si(x) int(x.size())\n#ifdef LOCAL\n#define dmp(x) cerr<<__LINE__<<\" \"<<#x<<\" \"<<x<<endl\n#else\n#define dmp(x) void(0)\n#endif\n\ntemplate<class t,class u> bool chmax(t&a,u b){if(a<b){a=b;return true;}else return false;}\ntemplate<class t,class u> bool chmin(t&a,u b){if(b<a){a=b;return true;}else return false;}\n\ntemplate<class t> using vc=vector<t>;\ntemplate<class t> using vvc=vc<vc<t>>;\n\nusing pi=pair<int,int>;\nusing vi=vc<int>;\nusing vvi=vc<vc<int>>;\n\ntemplate<class t,class u>\nostream& operator<<(ostream& os,const pair<t,u>& p){\n\treturn os<<\"{\"<<p.a<<\",\"<<p.b<<\"}\";\n}\n\ntemplate<class t> ostream& operator<<(ostream& os,const vc<t>& v){\n\tos<<\"{\";\n\tfor(auto e:v)os<<e<<\",\";\n\treturn os<<\"}\";\n}\n\n#define mp make_pair\n#define mt make_tuple\n#define one(x) memset(x,-1,sizeof(x))\n#define zero(x) memset(x,0,sizeof(x))\n#ifdef LOCAL\nvoid dmpr(ostream&os){os<<endl;}\ntemplate<class T,class... Args>\nvoid dmpr(ostream&os,const T&t,const Args&... args){\n\tos<<t<<\" \";\n\tdmpr(os,args...);\n}\n#define dmp2(...) dmpr(cerr,__LINE__,##__VA_ARGS__)\n#else\n#define dmp2(...) void(0)\n#endif\n\nusing uint=unsigned;\nusing ull=unsigned long long;\n\ntemplate<class t,size_t n>\nostream& operator<<(ostream&os,const array<t,n>&a){\n\treturn os<<vc<t>(all(a));\n}\n\nll rand_int(ll l, ll r) { //[l, r]\n\t//#ifdef LOCAL\n\tstatic mt19937_64 gen;\n\t/*#else\n\tstatic mt19937_64 gen(chrono::steady_clock::now().time_since_epoch().count());\n\t#endif*/\n\treturn uniform_int_distribution<ll>(l, r)(gen);\n}\n\nll rand_int(ll k){ //[0,k)\n\treturn rand_int(0,k-1);\n}\nstring rand_string(int n,char lw,char up){\n\tstring s(n,'?');\n\trep(i,n)s[i]=rand_int(lw,up);\n\treturn s;\n}\n\nint current_run_id,run_batch_size=1000;\nint calc_random_limit(){\n\treturn current_run_id/run_batch_size+1;\n}\ntemplate<class t>\nvoid generate_single(t&a){\n\ta=rand_int(1,calc_random_limit());\n}\nvoid generate_single(string&a){\n\tint n;generate_single(n);\n\ta=rand_string(n,'a','b');\n}\ntemplate<class t,class u>\nvoid generate_single(pair<t,u>&a){\n\tgenerate_single(a.a);\n\tgenerate_single(a.b);\n}\n//https://trap.jp/post/1224/\ntemplate<class... Args>\nvoid input(Args&... a){\n\tif(dbg){\n\t\t(generate_single(a),...);\n\t}else{\n\t\t#ifdef USE_FAST_IO\n\t\tsc.read(a...);\n\t\t#else\n\t\t(cin >> ... >> a);\n\t\t#endif\n\t}\n}\n#define INT(...) int __VA_ARGS__;input(__VA_ARGS__)\n#define LL(...) ll __VA_ARGS__;input(__VA_ARGS__)\n#define ULL(...) ull __VA_ARGS__;input(__VA_ARGS__)\n#define STR(...) string __VA_ARGS__;input(__VA_ARGS__)\n#define CHR(...) char __VA_ARGS__;input(__VA_ARGS__)\n#define DBL(...) double __VA_ARGS__;input(__VA_ARGS__)\n#define LD(...) ld __VA_ARGS__;input(__VA_ARGS__)\n#define overload3(a,b,c,d,...) d\n#define VI2(name,size) vi name(size);rep(i_##name,size)input(name[i_##name]);\n#define VI3(name,size,offset) vi name(size);rep(i_##name,size)input(name[i_##name]),name[i_##name]+=offset;\n#define VI(...) overload3(__VA_ARGS__,VI3,VI2)(__VA_ARGS__)\n#define VPI(name,size) vc<pi> name(size);rep(i_##name,size)input(name[i_##name].a,name[i_##name].b);\n#define VVI(name,sizeN,sizeM) vvi name(sizeN,vi(sizeM));\\\nrep(i_##name,sizeN)rep(j_##name,sizeM)input(name[i_##name][j_##name]);\n#define VS(name,size) vc<string> name(size);rep(i_##name,size)input(name[i_##name]);\n#define VMI(name,size) vc<mint> name(size);rep(i_##name,size){INT(tmp_##name);name[i_##name]=tmp_##name;};\n\n#define overload5(a,b,c,d,e,f,...) f\n#define VVC4(type,name,sizeN,sizeM) vvc<type> name(sizeN,vc<type>(sizeM));\n#define VVC5(type,name,sizeN,sizeM,ini) vvc<type> name(sizeN,vc<type>(sizeM,ini));\n#define VVC(...) overload5(__VA_ARGS__,VVC5,VVC4)(__VA_ARGS__)\n\ntemplate<class T>\nT vvvc(T v){\n\treturn v;\n}\n\ntemplate<class T,class...Args>\nauto vvvc(int n,T v,Args...args){\n\treturn vector(n,vvvc(v,args...));\n}\n\ntemplate<int i,class T>\nvoid print_tuple(ostream&,const T&){\n}\n\ntemplate<int i,class T,class H,class ...Args>\nvoid print_tuple(ostream&os,const T&t){\n\tif(i)os<<\",\";\n\tos<<get<i>(t);\n\tprint_tuple<i+1,T,Args...>(os,t);\n}\n\ntemplate<class ...Args>\nostream& operator<<(ostream&os,const tuple<Args...>&t){\n\tos<<\"{\";\n\tprint_tuple<0,tuple<Args...>,Args...>(os,t);\n\treturn os<<\"}\";\n}\n\nvoid printsuc(int suc){\n\t#ifdef USE_FAST_IO\n\t\tif(suc==1)pr.write('\\n');\n\t\tif(suc==2)pr.write(' ');\n\t#else\n\t\tif(suc==1){\n\t\t\tif(dbg)cout<<endl;\n\t\t\telse{\n\t\t\t\t#ifdef LOCAL\n\t\t\t\tcout<<endl;\n\t\t\t\t#else\n\t\t\t\tcout<<\"\\n\";\n\t\t\t\t#endif\n\t\t\t}\n\t\t}\n\t\tif(suc==2)\n\t\t\tcout<<\" \";\n\t#endif\n}\n\ntemplate<class t>\nvoid print_single(t x,int suc=1){\n\t#ifdef USE_FAST_IO\n\tpr.write(x);\n\t#else\n\tcout<<x;\n\t#endif\n\tprintsuc(suc);\n}\n\ntemplate<class t,class u>\nvoid print_single(const pair<t,u>&p,int suc=1){\n\tprint_single(p.a,2);\n\tprint_single(p.b,suc);\n}\n\ntemplate<class T>\nvoid print_single(const vector<T>&v,int suc=1){\n\trep(i,v.size())\n\t\tprint_single(v[i],i==int(v.size())-1?3:2);\n\tprintsuc(suc);\n}\n\ntemplate<class T,size_t N>\nvoid print_single(const array<T,N>&v,int suc=1){\n\trep(i,N)\n\t\tprint_single(v[i],i==int(N)-1?3:2);\n\tprintsuc(suc);\n}\n\ntemplate<class T>\nvoid print(const T&t){\n\tprint_single(t);\n}\n\ntemplate<class T,class ...Args>\nvoid print(const T&t,const Args&...args){\n\tprint_single(t,2);\n\tprint(args...);\n}\n\ntemplate<class T>\nvoid printvv(const vvc<T>&vs){\n\tfor(const auto&row:vs)print(row);\n}\n\nstring readString(){\n\tstring s;\n\tcin>>s;\n\treturn s;\n}\n\ntemplate<class T>\nT sq(const T& t){\n\treturn t*t;\n}\n\nvoid YES(bool ex=true){\n\tcout<<\"YES\\n\";\n\tif(ex)exit(0);\n\t#ifdef LOCAL\n\tcout.flush();\n\t#endif\n}\nvoid NO(bool ex=true){\n\tcout<<\"NO\\n\";\n\tif(ex)exit(0);\n\t#ifdef LOCAL\n\tcout.flush();\n\t#endif\n}\nvoid Yes(bool ex=true){\n\tcout<<\"Yes\\n\";\n\tif(ex)exit(0);\n\t#ifdef LOCAL\n\tcout.flush();\n\t#endif\n}\nvoid No(bool ex=true){\n\tcout<<\"No\\n\";\n\tif(ex)exit(0);\n\t#ifdef LOCAL\n\tcout.flush();\n\t#endif\n}\n//#define CAPITAL\n/*\nvoid yes(bool ex=true){\n\t#ifdef CAPITAL\n\tcout<<\"YES\"<<\"\\n\";\n\t#else\n\tcout<<\"Yes\"<<\"\\n\";\n\t#endif\n\tif(ex)exit(0);\n\t#ifdef LOCAL\n\tcout.flush();\n\t#endif\n}\nvoid no(bool ex=true){\n\t#ifdef CAPITAL\n\tcout<<\"NO\"<<\"\\n\";\n\t#else\n\tcout<<\"No\"<<\"\\n\";\n\t#endif\n\tif(ex)exit(0);\n\t#ifdef LOCAL\n\tcout.flush();\n\t#endif\n}*/\nvoid possible(bool ex=true){\n\t#ifdef CAPITAL\n\tcout<<\"POSSIBLE\"<<\"\\n\";\n\t#else\n\tcout<<\"Possible\"<<\"\\n\";\n\t#endif\n\tif(ex)exit(0);\n\t#ifdef LOCAL\n\tcout.flush();\n\t#endif\n}\nvoid impossible(bool ex=true){\n\t#ifdef CAPITAL\n\tcout<<\"IMPOSSIBLE\"<<\"\\n\";\n\t#else\n\tcout<<\"Impossible\"<<\"\\n\";\n\t#endif\n\tif(ex)exit(0);\n\t#ifdef LOCAL\n\tcout.flush();\n\t#endif\n}\n\nconstexpr ll ten(int n){\n\treturn n==0?1:ten(n-1)*10;\n}\n\nconst ll infLL=LLONG_MAX/3;\n\n#ifdef int\nconst int inf=infLL;\n#else\nconst int inf=INT_MAX/2-100;\n#endif\n\nint topbit(signed t){\n\treturn t==0?-1:31-__builtin_clz(t);\n}\nint topbit(ll t){\n\treturn t==0?-1:63-__builtin_clzll(t);\n}\nint topbit(ull t){\n\treturn t==0?-1:63-__builtin_clzll(t);\n}\nint botbit(signed a){\n\treturn a==0?32:__builtin_ctz(a);\n}\nint botbit(ll a){\n\treturn a==0?64:__builtin_ctzll(a);\n}\nint botbit(ull a){\n\treturn a==0?64:__builtin_ctzll(a);\n}\nint popcount(signed t){\n\treturn __builtin_popcount(t);\n}\nint popcount(ll t){\n\treturn __builtin_popcountll(t);\n}\nint popcount(ull t){\n\treturn __builtin_popcountll(t);\n}\nint bitparity(ll t){\n\treturn __builtin_parityll(t);\n}\nbool ispow2(int i){\n\treturn i&&(i&-i)==i;\n}\nll mask(int i){\n\treturn (ll(1)<<i)-1;\n}\null umask(int i){\n\treturn (ull(1)<<i)-1;\n}\nll minp2(ll n){\n\tif(n<=1)return 1;\n\telse return ll(1)<<(topbit(n-1)+1);\n}\n\nbool inc(int a,int b,int c){\n\treturn a<=b&&b<=c;\n}\n\ntemplate<class S> void mkuni(S&v){\n\tsort(all(v));\n\tv.erase(unique(all(v)),v.ed);\n}\n\ntemplate<class t> bool isuni(vc<t> v){\n\tint s=si(v);\n\tmkuni(v);\n\treturn si(v)==s;\n}\n\nbool isperm(const vi&p){\n\tint n=si(p);\n\tvc<bool> used(n);\n\tfor(auto v:p){\n\t\tif(!inc(0,v,n-1))return false;\n\t\tif(used[v])return false;\n\t\tused[v]=true;\n\t}\n\treturn true;\n}\n\ntemplate<class t>\nvoid myshuffle(vc<t>&a){\n\trep(i,si(a))swap(a[i],a[rand_int(0,i)]);\n}\n\ntemplate<class S,class u>\nint lwb(const S&v,const u&a){\n\treturn lower_bound(all(v),a)-v.bg;\n}\ntemplate<class t,class u>\nbool bis(const vc<t>&v,const u&a){\n\treturn binary_search(all(v),a);\n}\n\n//VERIFY: yosupo\n//KUPC2017J\n//AOJDSL1A\n//without rank\nstruct unionfind{\n\tvi p,s;\n\tint c;\n\tunionfind(int n):p(n,-1),s(n,1),c(n){}\n\tvoid clear(){\n\t\tfill(all(p),-1);\n\t\tfill(all(s),1);\n\t\tc=si(p);\n\t}\n\tint find(int a){\n\t\treturn p[a]==-1?a:(p[a]=find(p[a]));\n\t}\n\t//set b to a child of a\n\tbool unite(int a,int b){\n\t\ta=find(a);\n\t\tb=find(b);\n\t\tif(a==b)return false;\n\t\tp[b]=a;\n\t\ts[a]+=s[b];\n\t\tc--;\n\t\treturn true;\n\t}\n\tbool same(int a,int b){\n\t\treturn find(a)==find(b);\n\t}\n\tint sz(int a){\n\t\treturn s[find(a)];\n\t}\n};\n\nvvc<int> readGraph(int n,int m){\n\tvvc<int> g(n);\n\trep(i,m){\n\t\tint a,b;\n\t\tcin>>a>>b;\n\t\t//sc.read(a,b);\n\t\ta--;b--;\n\t\tg[a].pb(b);\n\t\tg[b].pb(a);\n\t}\n\treturn g;\n}\n\nvvc<int> initUG(int n,const vc<pi>&es){\n\tvvc<int> g(n);\n\tfor(auto [a,b]:es){\n\t\tg[a].pb(b);\n\t\tg[b].pb(a);\n\t}\n\treturn g;\n}\n\nvvc<int> rand_tree(int n){\n\tvvc<int> t(n);\n\tunionfind uf(n);\n\twhile(uf.c>1){\n\t\tint a=rand_int(n);\n\t\tint b=rand_int(n);\n\t\tif(uf.unite(a,b)){\n\t\t\tt[a].pb(b);\n\t\t\tt[b].pb(a);\n\t\t}\n\t}\n\treturn t;\n}\n\nvvc<int> readTree(int n){\n\tif(dbg){\n\t\treturn rand_tree(n);\n\t}else{\n\t\treturn readGraph(n,n-1);\n\t}\n}\n\nvi readRooted(int n){\n\tassert(!dbg);\n\tvi par(n,-1);\n\trng(i,1,n){\n\t\tinput(par[i]);\n\t\tpar[i]--;\n\t\tassert(inc(0,par[i],i-1));\n\t}\n\treturn par;\n}\n\nvoid printTree(const vvc<int> t){\n\tint n=si(t);\n\tint degsum=0;\n\trep(i,n)degsum+=si(t[i]);\n\tif(degsum==n-1){\n\t\t//directed\n\t\trep(i,si(t))for(auto j:t[i]){\n\t\t\tprint(i+1,j+1);\n\t\t}\n\t}else if(degsum==2*(n-1)){\n\t\t//undirected\n\t\trep(i,si(t))for(auto j:t[i])if(i<j){\n\t\t\tprint(i+1,j+1);\n\t\t}\n\t}else{\n\t\tassert(false);\n\t}\n}\n\ntemplate<class t>\nvc<t> presum(const vc<t>&a){\n\tvc<t> s(si(a)+1);\n\trep(i,si(a))s[i+1]=s[i]+a[i];\n\treturn s;\n}\nvc<ll> presum(const vi&a){\n\tvc<ll> s(si(a)+1);\n\trep(i,si(a))s[i+1]=s[i]+a[i];\n\treturn s;\n}\n//BIT で数列を管理するときに使う (CF850C)\ntemplate<class t>\nvc<t> predif(vc<t> a){\n\tgnr(i,1,si(a))a[i]-=a[i-1];\n\treturn a;\n}\ntemplate<class t>\nvvc<ll> imos(const vvc<t>&a){\n\tint n=si(a),m=si(a[0]);\n\tvvc<ll> b(n+1,vc<ll>(m+1));\n\trep(i,n)rep(j,m)\n\t\tb[i+1][j+1]=b[i+1][j]+b[i][j+1]-b[i][j]+a[i][j];\n\treturn b;\n}\n\n//verify してないや\nvoid transvvc(int&n,int&m){\n\tswap(n,m);\n}\ntemplate<class t,class... Args>\nvoid transvvc(int&n,int&m,vvc<t>&a,Args&...args){\n\tassert(si(a)==n);\n\tvvc<t> b(m,vi(n));\n\trep(i,n){\n\t\tassert(si(a[i])==m);\n\t\trep(j,m)b[j][i]=a[i][j];\n\t}\n\ta.swap(b);\n\ttransvvc(n,m,args...);\n}\n//CF854E\nvoid rotvvc(int&n,int&m){\n\tswap(n,m);\n}\ntemplate<class t,class... Args>\nvoid rotvvc(int&n,int&m,vvc<t>&a,Args&...args){\n\tassert(si(a)==n);\n\tvvc<t> b(m,vi(n));\n\trep(i,n){\n\t\tassert(si(a[i])==m);\n\t\trep(j,m)b[m-1-j][i]=a[i][j];\n\t}\n\ta.swap(b);\n\trotvvc(n,m,args...);\n}\n\n//ソートして i 番目が idx[i]\n//CF850C\ntemplate<class t>\nvi sortidx(const vc<t>&a){\n\tint n=si(a);\n\tvi idx(n);iota(all(idx),0);\n\tsort(all(idx),[&](int i,int j){return a[i]<a[j];});\n\treturn idx;\n}\n//vs[i]=a[idx[i]]\n//例えば sortidx で得た idx を使えば単にソート列になって返ってくる\n//CF850C\ntemplate<class t>\nvc<t> a_idx(const vc<t>&a,const vi&idx){\n\tint n=si(a);\n\tassert(si(idx)==n);\n\tvc<t> vs(n);\n\trep(i,n)vs[i]=a[idx[i]];\n\treturn vs;\n}\n//CF850C\nvi invperm(const vi&p){\n\tint n=si(p);\n\tvi q(n);\n\trep(i,n)q[p[i]]=i;\n\treturn q;\n}\n\ntemplate<class t,class s=t>\ns SUM(const vc<t>&a){\n\treturn accumulate(all(a),s(0));\n}\ntemplate<class t,size_t K,class s=t>\ns SUM(const array<t,K>&a){\n\treturn accumulate(all(a),s(0));\n}\n\ntemplate<class t>\nt MAX(const vc<t>&a){\n\treturn *max_element(all(a));\n}\n\ntemplate<class t>\npair<t,int> MAXi(const vc<t>&a){\n\tauto itr=max_element(all(a));\n\treturn mp(*itr,itr-a.bg);\n}\n\ntemplate<class A>\nauto MIN(const A&a){\n\treturn *min_element(all(a));\n}\n\ntemplate<class t>\npair<t,int> MINi(const vc<t>&a){\n\tauto itr=min_element(all(a));\n\treturn mp(*itr,itr-a.bg);\n}\n\nvi vid(int n){\n\tvi res(n);iota(all(res),0);\n\treturn res;\n}\n\ntemplate<class S>\nvoid soin(S&s){\n\tsort(all(s));\n}\n\ntemplate<class S,class F>\nvoid soin(S&s,F&&f){\n\tsort(all(s),forward<F>(f));\n}\n\ntemplate<class S>\nS soout(S s){\n\tsoin(s);\n\treturn s;\n}\n\ntemplate<class S>\nvoid rein(S&s){\n\treverse(all(s));\n}\n\ntemplate<class S>\nS reout(S s){\n\trein(s);\n\treturn s;\n}\n\ntemplate<class t,class u>\npair<t,u>&operator+=(pair<t,u>&a,pair<t,u> b){\n\ta.a+=b.a;a.b+=b.b;return a;}\ntemplate<class t,class u>\npair<t,u>&operator-=(pair<t,u>&a,pair<t,u> b){\n\ta.a-=b.a;a.b-=b.b;return a;}\ntemplate<class t,class u>\npair<t,u> operator+(pair<t,u> a,pair<t,u> b){return mp(a.a+b.a,a.b+b.b);}\ntemplate<class t,class u>\npair<t,u> operator-(pair<t,u> a,pair<t,u> b){return mp(a.a-b.a,a.b-b.b);}\ntemplate<class t,class u,class v>\npair<t,u>&operator*=(pair<t,u>&a,v b){\n\ta.a*=b;a.b*=b;return a;}\ntemplate<class t,class u,class v>\npair<t,u> operator*(pair<t,u> a,v b){return a*=b;}\ntemplate<class t,class u>\npair<t,u> operator-(pair<t,u> a){return mp(-a.a,-a.b);}\nnamespace std{\ntemplate<class t,class u>\nistream&operator>>(istream&is,pair<t,u>&a){\n\treturn is>>a.a>>a.b;\n}\n}\n\ntemplate<class t,size_t n>\narray<t,n>&operator+=(array<t,n>&a,const array<t,n>&b){\n\trep(i,n)a[i]+=b[i];\n\treturn a;\n}\ntemplate<class t,size_t n>\narray<t,n>&operator-=(array<t,n>&a,const array<t,n>&b){\n\trep(i,n)a[i]-=b[i];\n\treturn a;\n}\ntemplate<class t,size_t n,class v>\narray<t,n>&operator*=(array<t,n>&a,v b){\n\trep(i,n)a[i]*=b;\n\treturn a;\n}\ntemplate<class t,size_t n>\narray<t,n> operator+(array<t,n> a,const array<t,n>&b){return a+=b;}\ntemplate<class t,size_t n>\narray<t,n> operator-(array<t,n> a,const array<t,n>&b){return a-=b;}\ntemplate<class t,size_t n,class v>\narray<t,n> operator*(array<t,n> a,v b){return a*=b;}\n\ntemplate<class t>\nt gpp(vc<t>&vs){\n\tassert(si(vs));\n\tt res=move(vs.back());\n\tvs.pop_back();\n\treturn res;\n}\n\ntemplate<class t,class u>\nvoid pb(vc<t>&a,const vc<u>&b){\n\ta.insert(a.ed,all(b));\n}\n\ntemplate<class t,class...Args>\nvc<t> cat(vc<t> a,Args&&...b){\n\t(pb(a,forward<Args>(b)),...);\n\treturn a;\n}\n\ntemplate<class t,class u>\nvc<t>& operator+=(vc<t>&a,u x){\n\tfor(auto&v:a)v+=x;\n\treturn a;\n}\n\ntemplate<class t,class u>\nvc<t> operator+(vc<t> a,u x){\n\treturn a+=x;\n}\n\ntemplate<class t>\nvc<t>& operator+=(vc<t>&a,const vc<t>&b){\n\ta.resize(max(si(a),si(b)));\n\trep(i,si(b))a[i]+=b[i];\n\treturn a;\n}\n\ntemplate<class t>\nvc<t> operator+(const vc<t>&a,const vc<t>&b){\n\tvc<t> c(max(si(a),si(b)));\n\trep(i,si(a))c[i]+=a[i];\n\trep(i,si(b))c[i]+=b[i];\n\treturn c;\n}\n\ntemplate<class t,class u>\nvc<t>& operator-=(vc<t>&a,u x){\n\tfor(auto&v:a)v-=x;\n\treturn a;\n}\ntemplate<class t,class u>\nvc<t> operator-(vc<t> a,u x){\n\treturn a-=x;\n}\ntemplate<class t>\nvc<t>& operator-=(vc<t>&a,const vc<t>&b){\n\ta.resize(max(si(a),si(b)));\n\trep(i,si(b))a[i]-=b[i];\n\treturn a;\n}\n/*\ntemplate<class t>\nvc<t> operator-(const vc<t>&a,const vc<t>&b){\n\tvc<t> c(max(si(a),si(b)));\n\trep(i,si(a))c[i]+=a[i];\n\trep(i,si(b))c[i]-=b[i];\n\treturn c;\n}\n*/\ntemplate<class t,class u>\nvc<t>& operator*=(vc<t>&a,u x){\n\tfor(auto&v:a)v*=x;\n\treturn a;\n}\ntemplate<class t,class u>\nvc<t> operator*(vc<t> a,u x){\n\treturn a*=x;\n}\n\ntemplate<class t,class u>\nvc<t>& operator/=(vc<t>&a,u x){\n\tfor(auto&v:a)v/=x;\n\treturn a;\n}\ntemplate<class t,class u>\nvc<t> operator/(vc<t> a,u x){\n\treturn a/=x;\n}\n\ntemplate<class t>\nvc<t>& operator<<=(vc<t>&a,int k){\n\tassert(k>=0);\n\ta.insert(a.bg,k,t(0));\n\treturn a;\n}\ntemplate<class t>\nvc<t> operator<<(vc<t> a,int k){\n\treturn a<<=k;\n}\n\ntemplate<class t>\nvc<t>& operator>>=(vc<t>&a,int k){\n\tif(si(a)<=k)a.clear();\n\telse a.erase(a.bg,a.bg+k);\n\treturn a;\n}\ntemplate<class t>\nvc<t> operator>>(vc<t> a,int k){\n\treturn a>>=k;\n}\n\n//消した要素の個数を返してくれる\n//not verified\ntemplate<class t,class u>\nint remval(vc<t>&a,const u&v){\n\tauto itr=remove(all(a),v);\n\tint res=a.ed-itr;\n\ta.erase(itr,a.ed);\n\treturn res;\n}\n//消した要素の個数を返してくれる\n//UCUP 2-8-F\ntemplate<class t,class F>\nint remif(vc<t>&a,F f){\n\tauto itr=remove_if(all(a),f);\n\tint res=a.ed-itr;\n\ta.erase(itr,a.ed);\n\treturn res;\n}\ntemplate<class t>\nvoid rempos(vc<t>&a,int i){\n\tassert(inc(0,i,si(a)-1));\n\ta.erase(a.bg+i);\n}\n\ntemplate<class VS,class u>\nvoid fila(VS&vs,const u&a){\n\tfill(all(vs),a);\n}\n\ntemplate<class t,class u>\nint findid(const vc<t>&vs,const u&a){\n\tauto itr=find(all(vs),a);\n\tif(itr==vs.ed)return -1;\n\telse return itr-vs.bg;\n}\n\ntemplate<class t>\nvoid rtt(vc<t>&vs,int i){\n\trotate(vs.bg,vs.bg+i,vs.ed);\n}\n\n//Multiuni2023-8 C\n//f(lw)=false,...,f(n-1)=false,f(n)=true,...,f(up)=true,\n//のときに n を返す\ntemplate<class F>\nint find_min_true(int lw,int up,F f){\n\twhile(up-lw>1){\n\t\tconst int mid=(lw+up)/2;\n\t\tif(f(mid))up=mid;\n\t\telse lw=mid;\n\t}\n\treturn up;\n}\n//f(lw)=true,f(up)=false\ntemplate<class F>\nint find_max_true(int lw,int up,F f){\n\twhile(up-lw>1){\n\t\tconst int mid=(lw+up)/2;\n\t\tif(f(mid))lw=mid;\n\t\telse up=mid;\n\t}\n\treturn lw;\n}\n\ntemplate<class t> using pqmin=priority_queue<t,vc<t>,greater<t>>;\ntemplate<class t> using pqmax=priority_queue<t>;\nusing T=tuple<int,int,int>;\n\nint sub(vi a){\n\tint n=si(a);\n\tint s=0;\n\trep(i,n)s+=1<<a[i];\n\tvi dp(s+1,-inf);\n\tdp[0]=0;\n\trep(i,n){\n\t\tper(v,s+1)if(dp[v]>=0){\n\t\t\tif((v&mask(a[i]))==0){\n\t\t\t\tchmax(dp[v+(1<<a[i])],dp[v]+1);\n\t\t\t}\n\t\t}\n\t}\n\tint res=0;\n\trng(v,1,s+1)if(ispow2(v))chmax(res,dp[v]);\n\treturn res;\n}\n\nvoid slv(){\n\tINT(n);\n\tif(n==0)exit(0);\n\tVI(a,n);\n\tmap<int,vi> buf;\n\trep(i,n){\n\t\tint t=botbit(a[i]);\n\t\tbuf[a[i]>>t].pb(t);\n\t}\n\tint ans=0;\n\tfor(auto [key,val]:buf)\n\t\tchmax(ans,sub(val));\n\tprint(ans);\n}\n\nsigned main(signed argc,char*argv[]){\n\tif(argc>1&&strcmp(argv[1],\"D\")==0)dbg=true;\n\t\n\tcin.tie(0);\n\tios::sync_with_stdio(0);\n\tcout<<fixed<<setprecision(20);\n\t\n\tif(dbg){\n\t\twhile(1){\n\t\t\tif(current_run_id%run_batch_size==0){\n\t\t\t\tcerr<<\"Current Run \"<<current_run_id<<endl;\n\t\t\t}\n\t\t\tslv();\n\t\t\tcurrent_run_id++;\n\t\t}\n\t}else{\n\t\t//INT(t);rep(_,t)\n\t\twhile(1)slv();\n\t}\n}", "accuracy": 1, "time_ms": 710, "memory_kb": 4440, "score_of_the_acc": -0.308, "final_rank": 9 }, { "submission_id": "aoj_1343_9988682", "code_snippet": "// GYM102014I Hidden Tree\n#include <cstdio>\n#include <iostream>\n#include <vector>\n#include <cstring>\n#define ll long long\n#define rep(i, s, t) for(int i=s; i<=t; ++i)\n#define debug(x) cerr<<#x<<\":\"<<x<<endl;\nconst int N=1005, M=1000010, inf=0x3f3f3f3f;\nusing namespace std;\nchar buf[1<<23], *p1=buf, *p2=buf;\n#define gc() (p1==p2 && (p2=(p1=buf)+fread(buf,1,1<<21,stdin),p1==p2)?EOF:*p1++)\ninline int read()\n{\n int x=0, f=1; char c=gc();\n while(c<'0' || c>'9') c=='-' && (f=-1), c=gc();\n while('0'<=c && c<='9') x=(x<<3)+(x<<1)+c-'0', c=gc();\n return x*f;\n}\n\nint n, a[N], b[N], res;\ninline void upd(int &x, int y) {x=x>y?x:y;}\nvector<int> vec[N];\nint f[M], sum;\nvoid solve(vector<int> &a)\n{\n memset(f, -0x3f, sizeof f); f[0]=0;\n sum=0;\n for(int x:a)\n {\n sum+=x;\n // for(int s=sum; s>=x; s--)\n // {\n // int lo=s&-s;\n // if(!s || lo>=x) upd(f[s], f[s-x]+1);\n // }\n for(int s=sum/x*x; s>=x; s-=x)\n upd(f[s], f[s-x]+1);\n }\n for(int s=1; s<=sum; s<<=1) upd(res, f[s]);\n}\n\nvoid solve()\n{\n res=0;\n rep(i, 1, 500) vec[i].clear();\n rep(i, 1, n) a[i]=read(), b[i]=1;\n rep(i, 1, n)\n {\n while(!(a[i]&1)) b[i]<<=1, a[i]>>=1;\n vec[a[i]].push_back(b[i]);\n }\n rep(i, 1, 500) if(vec[i].size()) solve(vec[i]);\n printf(\"%d\\n\", res);\n}\n\nint main()\n{\n#ifdef Jerrywang\n freopen(\"E:/OI/in.txt\", \"r\", stdin); ios::sync_with_stdio(0);\n#endif\n while(n=read(), n) solve();\n\n return 0;\n}", "accuracy": 1, "time_ms": 240, "memory_kb": 7552, "score_of_the_acc": -0.9985, "final_rank": 16 }, { "submission_id": "aoj_1343_9988655", "code_snippet": "// GYM102014I Hidden Tree\n#include <cstdio>\n#include <iostream>\n#include <vector>\n#include <cstring>\n#define ll long long\n#define rep(i, s, t) for(int i=s; i<=t; ++i)\n#define debug(x) cerr<<#x<<\":\"<<x<<endl;\nconst int N=1005, M=1000010, inf=0x3f3f3f3f;\nusing namespace std;\nchar buf[1<<23], *p1=buf, *p2=buf;\n#define gc() (p1==p2 && (p2=(p1=buf)+fread(buf,1,1<<21,stdin),p1==p2)?EOF:*p1++)\ninline int read()\n{\n int x=0, f=1; char c=gc();\n while(c<'0' || c>'9') c=='-' && (f=-1), c=gc();\n while('0'<=c && c<='9') x=(x<<3)+(x<<1)+c-'0', c=gc();\n return x*f;\n}\n\nint n, a[N], b[N], res;\ninline void upd(int &x, int y) {x=x>y?x:y;}\nvector<int> vec[N];\nint f[M], sum;\nvoid solve(vector<int> &a)\n{\n memset(f, -0x3f, sizeof f); f[0]=0;\n sum=0;\n for(int x:a)\n {\n sum+=x;\n for(int s=sum; s>=x; s--)\n {\n int lo=s&-s;\n if(!s || lo>=x) upd(f[s], f[s-x]+1);\n }\n }\n for(int s=1; s<=sum; s<<=1) upd(res, f[s]);\n}\n\nvoid solve()\n{\n res=0;\n rep(i, 1, 500) vec[i].clear();\n rep(i, 1, n) a[i]=read(), b[i]=1;\n rep(i, 1, n)\n {\n while(!(a[i]&1)) b[i]<<=1, a[i]>>=1;\n vec[a[i]].push_back(b[i]);\n }\n rep(i, 1, 500) if(vec[i].size()) solve(vec[i]);\n printf(\"%d\\n\", res);\n}\n\nint main()\n{\n#ifdef Jerrywang\n freopen(\"E:/OI/in.txt\", \"r\", stdin); ios::sync_with_stdio(0);\n#endif\n while(n=read(), n) solve();\n\n return 0;\n}", "accuracy": 1, "time_ms": 610, "memory_kb": 7612, "score_of_the_acc": -1.0671, "final_rank": 18 }, { "submission_id": "aoj_1343_9628315", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nusing i64 = int64_t;\nusing u64 = uint64_t;\n\nvoid solve(int n) {\n if (!n) exit(0);\n vector<int> a(n);\n for (int i = 0; i < n; i++) cin >> a[i];\n int ans = 0, sum = accumulate(a.begin(), a.end(), 0);\n for (int mul = 1; mul < 500; mul += 2) {\n int w = 1;\n while (w * mul <= sum) w *= 2;\n vector<int> dp(w + 1, 0);\n for (int i = 0; i < n; i++) {\n if (a[i] % mul) continue;\n int x = a[i] / mul;\n if (x != (x & -x)) continue;\n for (int j = w - x; j > 0; j -= x) {\n if (dp[j]) dp[j + x] = max(dp[j + x], dp[j] + 1);\n }\n dp[x] = max(dp[x], 1);\n }\n for (int i = 1; i < w; i *= 2) {\n ans = max(ans, dp[i]);\n }\n }\n cout << ans << \"\\n\";\n}\n\n// Make bold hypotheses and verify carefully\nint main() {\n cin.tie(nullptr);\n ios::sync_with_stdio(false);\n int t = 1;\n // cin >> t;\n int n;\n while (cin >> n) {\n solve(n);\n };\n}", "accuracy": 1, "time_ms": 460, "memory_kb": 6024, "score_of_the_acc": -0.6579, "final_rank": 12 }, { "submission_id": "aoj_1343_9278055", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n#define ll long long\n#define rep(i, a, b) for(ll i = a; i < b; i++)\n#define all(x) (x).begin(), (x).end()\n\ntemplate<typename T>\nvoid print(const vector<T>& a){\n for(auto x:a)cout<<x<<\" \";\n cout<<endl;\n}\n\nint dp[1<<18];\n\nint solve(const vector<int>& a) {\n const int n = a.size();\n memset(dp, -1, sizeof(dp));\n dp[0]=0;\n rep(i,0,n){\n for(int S=(1<<18)-1; S>=0; --S){\n if(dp[S]!=-1 && (S&((1<<a[i])-1)) == 0){\n int nS = S+(1<<a[i]);\n dp[nS] = max(dp[nS], dp[S] + 1);\n }\n }\n }\n int ans = 0;\n rep(k,0,18) ans = max(ans, dp[1<<k]);\n return ans;\n}\n\nint main() {\n while (true) {\n int n;\n cin >> n;\n if (n == 0) break;\n vector<int> a(n);\n for (auto& x : a) cin >> x;\n\n vector<vector<int>> as(500);\n rep(i,0,n){\n int k=0;\n while (a[i] % 2 == 0) {\n a[i]/=2;\n ++k;\n }\n as[a[i]].push_back(k);\n }\n\n int ans = 0;\n rep(a,0,500)if(!as[a].empty()){\n ans = max(ans, solve(as[a]));\n }\n cout << ans << \"\\n\";\n }\n}", "accuracy": 1, "time_ms": 7010, "memory_kb": 4496, "score_of_the_acc": -1.24, "final_rank": 19 }, { "submission_id": "aoj_1343_8693986", "code_snippet": "#include<bits/stdc++.h>\n#define rep(i, n) for(int i = 0; i < (n); ++i)\nusing namespace std;\ntypedef long long ll;\ntypedef unsigned long long ull;\ntypedef vector<int> vi;\ntypedef pair<int, int> pii;\ntypedef pair<ll, int> pli;\ntypedef pair<ll, ll> pll;\ntypedef array<int, 3> ai3;\nconst int inf = 0x3f3f3f3f;\nconst int Mod = 998244353;\nconst int inv2 = (Mod+1) / 2;\nint sign(int a){ return (a&1) ? (Mod-1) : 1; }\nvoid uadd(int &a, int b){ a += b-Mod; a += (a>>31) & Mod; }\nvoid usub(int &a, int b){ a -= b, a += (a>>31) & Mod; }\nvoid umul(int &a, int b){ a = (int)(1ll * a * b % Mod); }\nint add(int a, int b){ a += b-Mod; a += (a>>31) & Mod; return a; }\nint sub(int a, int b){ a -= b, a += (a>>31) & Mod; return a; }\nint mul(int a, int b){ a = (int)(1ll * a * b % Mod); return a; }\nint qpow(int b, ll p){ int ret = 1; while(p){ if(p&1) umul(ret, b); umul(b, b), p >>= 1; } return ret; }\nconst int fN = 10010;\nint fact[fN], invfact[fN], inv[fN];\nvoid initfact(int n){\n\tfact[0] = 1; for(int i = 1; i <= n; ++i) fact[i] = mul(fact[i-1], i);\n\tinvfact[n] = qpow(fact[n], Mod-2); for(int i = n; i > 0; --i) invfact[i-1] = mul(invfact[i], i);\n\tfor(int i = 1; i <= n; ++i) inv[i] = mul(invfact[i], fact[i-1]);\n}\nint binom(int n, int m){ return (m < 0 || m > n) ? 0 : mul(fact[n], mul(invfact[m], invfact[n-m])); }\nconst double pi = acos(-1);\nvoid chmax(int &a, int b){ (b>a) ? (a=b) : 0; }\nvoid chmin(int &a, int b){ (b<a) ? (a=b) : 0; }\n\nconst int lmt = 18;\nint calc(vi &a){\n\tif(a.empty()) return 0;\n\t//cout << \"{\"; rep(i, (int)a.size()){ cout << a[i] << \" \"; } cout << \"}\\n\";\n\tstatic int dp[262626];\n\tfill(dp, dp + (1<<lmt), -inf);\n\tdp[0] = 0;\n\trep(i, (int)a.size()){\n\t\tfor(int x = (1<<lmt) - 1; x >= 0; --x){\n\t\t\tif(dp[x] >= 0 && !(x & ((1<<a[i]) - 1)))\n\t\t\t\tchmax(dp[x + (1<<a[i])], dp[x] + 1);\n\t\t}\n\t}\n\tint ret = 0;\n\trep(i, lmt) chmax(ret, dp[1<<i]);\n\treturn ret;\n}\n\nint n, a[1010];\n\nvi v[555];\nvoid solve(){\n\trep(i, n) cin >> a[i];\n\trep(i, 505) v[i].clear();\n\trep(i, n){\n\t\tint p = a[i], c = 0;\n\t\tfor(; p % 2 == 0; p >>= 1, ++c);\n\t\tv[p].push_back(c);\n\t}\n\tint ans = 0;\n\tfor(int i = 1; i <= 502; ++i)\n\t\tchmax(ans, calc(v[i]));\n\tcout << ans << \"\\n\";\n}\n\nint main(){\n\tios::sync_with_stdio(false);\n\tcin.tie(0);\n\n\twhile(cin >> n && n)\n\t\tsolve();\n\n\treturn 0;\n}", "accuracy": 1, "time_ms": 4860, "memory_kb": 4432, "score_of_the_acc": -0.911, "final_rank": 14 }, { "submission_id": "aoj_1343_8334951", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\n#define rep(i, n) for (int i = 0; i < (n); i++)\n#define per(i, n) for (int i = (n)-1; i >= 0; i--)\n#define rep2(i, l, r) for (int i = (l); i < (r); i++)\n#define per2(i, l, r) for (int i = (r)-1; i >= (l); i--)\n#define each(e, x) for (auto &e : x)\n#define sz(x) (int)x.size()\n#define all(x) begin(x), end(x)\n#define rall(x) rbegin(x), rend(x)\n#define eb emplace_back\n#define MM << ' ' <<\n#define TT template <typename T>\nusing ll = long long;\nusing pii = pair<int, int>;\nusing pll = pair<ll, ll>;\n\nTT bool chmin(T &x, T y) { return x > y ? (x = y, true) : false; }\nTT bool chmax(T &x, T y) { return x < y ? (x = y, true) : false; }\n\nTT using minheap = priority_queue<T, vector<T>, greater<T>>;\nTT using maxheap = priority_queue<T>;\n\nTT void print(vector<T> v, T x = 0) {\n int n = sz(v);\n rep(i, n) cout << v[i] + x << (i == n - 1 ? '\\n' : ' ');\n if (n == 0) cout << '\\n';\n}\n\nTT void printn(vector<T> v, T x = 0) {\n int n = sz(v);\n rep(i, n) cout << v[i] + x << '\\n';\n}\n\nTT void rearrange(vector<T> &v) {\n sort(all(v));\n v.erase(unique(all(v)), end(v));\n}\n\nTT int lb(vector<T> &v, T x) { return lower_bound(all(v), x) - begin(v); }\nTT int ub(vector<T> &v, T x) { return upper_bound(all(v), x) - begin(v); }\n\nstruct io_setup {\n io_setup() {\n ios_base::sync_with_stdio(false);\n cin.tie(NULL);\n cout << fixed << setprecision(15);\n }\n} io_setup;\n\nconst int inf = (1 << 30) - 1;\n\nint main() {\n while (true) {\n int N;\n cin >> N;\n\n if (N == 0) break;\n\n vector<int> a(N);\n rep(i, N) cin >> a[i];\n\n vector<int> b(N, 0);\n rep(i, N) {\n while (a[i] % 2 == 0) {\n a[i] /= 2;\n b[i]++;\n }\n }\n\n auto solve = [&](vector<int> c) {\n int sum = 0;\n each(e, c) sum += 1 << e;\n int K = 1;\n while (K * 2 <= sum) K *= 2;\n\n vector<int> p(K + 1, inf);\n rep2(i, 1, K + 1) p[i] = __builtin_ctz(i);\n\n vector<int> dp(K + 1, -inf);\n dp[0] = 0;\n each(e, c) {\n per(i, K - (1 << e) + 1) {\n if (p[i] >= e) {\n chmax(dp[i + (1 << e)], dp[i] + 1); //\n }\n }\n }\n\n int ret = 0;\n for (int i = 1; i <= K; i <<= 1) {\n chmax(ret, dp[i]); //\n }\n\n // print(c), print(dp);\n // cout << sum MM K MM ret << '\\n';\n return ret;\n };\n\n int ans = 0;\n\n rep(i, 501) {\n vector<int> c;\n rep(j, N) {\n if (a[j] == i) c.eb(b[j]);\n }\n if (!empty(c)) chmax(ans, solve(c));\n }\n\n cout << ans << '\\n';\n }\n}", "accuracy": 1, "time_ms": 520, "memory_kb": 4148, "score_of_the_acc": -0.2091, "final_rank": 7 }, { "submission_id": "aoj_1343_7244378", "code_snippet": "#include \"bits/stdc++.h\"\nusing namespace std;\nusing namespace std::chrono;\ntypedef long long int llint;\ntypedef long double lldo;\n#define mp make_pair\n#define mt make_tuple\n#define pub push_back\n#define puf push_front\n#define pob pop_back\n#define pof pop_front\n#define fir first\n#define sec second\n#define res resize\n#define ins insert\n#define era erase\n#define REP(i, n) for(int i = 0;i < (n);i++)\n/*cout<<fixed<<setprecision(20);cin.tie(0);ios::sync_with_stdio(false);*/\nconst int mod=1000000007;\nconst llint inf=2.19e15+1;\nconst long double pai=3.141592653589793238462643383279502884197;\nconst long double eps=1e-10;\ntemplate <class T,class U>bool chmin(T& a,U b){if(a>b){a=b;return true;}return false;}\ntemplate <class T,class U>bool chmax(T& a,U b){if(a<b){a=b;return true;}return false;}\nllint gcd(llint a,llint b){if(a%b==0){return b;}else return gcd(b,a%b);}\nllint lcm(llint a,llint b){if(a==0){return b;}return a/gcd(a,b)*b;}\ntemplate<class T> void SO(T& ve){sort(ve.begin(),ve.end());}\ntemplate<class T> void REV(T& ve){reverse(ve.begin(),ve.end());}\ntemplate<class T>int LBI(const vector<T>&ar,T in){return lower_bound(ar.begin(),ar.end(),in)-ar.begin();}\ntemplate<class T>int UBI(const vector<T>&ar,T in){return upper_bound(ar.begin(),ar.end(),in)-ar.begin();}\n\n\nint main(void){\n\tstatic int dp[132000]={};\n\tfor(int i=0;i<132000;i++){dp[i]=-mod;}\n\t//131072\n\twhile(-1){\n\t\tint n,i,j;cin>>n;\n\t\tif(n==0){return 0;}\n\t\tvector<int>A(n);\n\t\tfor(i=0;i<n;i++){cin>>A[i];}\n\t\tint ans=0;\n\t\tfor(int k=1;k<=499;k++){\n\t\t\tint sum=0;\n\t\t\tdp[0]=0;\n\t\t\tfor(i=0;i<n;i++){\n\t\t\t\tif(A[i]%k){continue;}\n\t\t\t\tint B=A[i]/k;\n\t\t\t\tif(B&(B-1)){continue;}\n\t\t\t\tfor(j=sum/B*B;j>=0;j-=B){\n\t\t\t\t\tchmax(dp[j+B],dp[j]+1);\n\t\t\t\t}\n\t\t\t\tsum+=B;\n\t\t\t\tchmin(sum,131072);\n\t\t\t}\n\t\t\tfor(i=1;i<=131072;i*=2){\n\t\t\t\tchmax(ans,dp[i]);\n\t\t\t}\n\t\t\tfor(i=0;i<=sum;i++){dp[i]=-mod;}\n\t\t}\n\t\tcout<<ans<<endl;\n\t}\n\treturn 0;\n}", "accuracy": 1, "time_ms": 270, "memory_kb": 3884, "score_of_the_acc": -0.1082, "final_rank": 2 }, { "submission_id": "aoj_1343_7120069", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\ntypedef long long ll;\ntemplate<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return true; } return false; }\ntemplate<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return true; } return false; }\n#define all(x) (x).begin(),(x).end()\n#define fi first\n#define se second\n#define mp make_pair\n#define si(x) int(x.size())\nconst int mod=998244353,MAX=300005,INF=1<<28;\n\nint dp[1<<18];\nint mi[1<<18];\n\nint main(){\n \n std::ifstream in(\"text.txt\");\n std::cin.rdbuf(in.rdbuf());\n cin.tie(0);\n ios::sync_with_stdio(false);\n \n for(int j=0;j<(1<<18);j++){\n for(int k=0;k<18;k++){\n if(j&(1<<k)){\n mi[j]=k;\n break;\n }\n }\n dp[j]=-INF;\n }\n mi[0]=INF;\n \n while(1){\n int N;cin>>N;\n if(N==0) break;\n vector<int> A(N);\n for(int i=0;i<N;i++) cin>>A[i];\n \n int ans=0;\n \n for(int x=1;x<=500;x+=2){\n vector<int> use;\n int sum=0;\n for(int i=0;i<N;i++){\n if(A[i]%x==0){\n int z=A[i]/x;\n for(int j=0;j<10;j++){\n if((1<<j)==z){\n use.push_back(j);\n sum+=z;\n }\n }\n }\n }\n \n if(si(use)==0) continue;\n \n for(int j=0;j<=sum;j++) dp[j]=-INF;\n dp[0]=0;\n \n for(int i=0;i<si(use);i++){\n int x=use[i];\n int z=sum/(1<<x);\n for(int k=z;k>=0;k--){\n int j=(k<<x);\n if(dp[j]<0) continue;\n if(mi[j]>=x){\n chmax(dp[j+(1<<x)],dp[j]+1);\n }\n }\n }\n \n for(int k=0;k<18;k++){\n if((1<<k)<=sum) chmax(ans,dp[(1<<k)]);\n }\n }\n \n cout<<ans<<\"\\n\";\n }\n}", "accuracy": 1, "time_ms": 500, "memory_kb": 5440, "score_of_the_acc": -0.5213, "final_rank": 11 }, { "submission_id": "aoj_1343_7120059", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\ntypedef long long ll;\ntemplate<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return true; } return false; }\ntemplate<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return true; } return false; }\n#define all(x) (x).begin(),(x).end()\n#define fi first\n#define se second\n#define mp make_pair\n#define si(x) int(x.size())\nconst int mod=998244353,MAX=300005,INF=1<<28;\n\nint dp[1<<18];\nint mi[1<<18];\n\nint main(){\n \n std::ifstream in(\"text.txt\");\n std::cin.rdbuf(in.rdbuf());\n cin.tie(0);\n ios::sync_with_stdio(false);\n \n for(int j=0;j<(1<<18);j++){\n for(int k=0;k<18;k++){\n if(j&(1<<k)){\n mi[j]=k;\n break;\n }\n }\n }\n mi[0]=INF;\n \n while(1){\n int N;cin>>N;\n if(N==0) break;\n vector<int> A(N);\n for(int i=0;i<N;i++) cin>>A[i];\n \n int ans=0;\n \n for(int x=1;x<=500;x+=2){\n vector<int> use;\n for(int i=0;i<N;i++){\n if(A[i]%x==0){\n int z=A[i]/x;\n for(int j=0;j<10;j++){\n if((1<<j)==z) use.push_back(j);\n }\n }\n }\n \n if(si(use)==0) continue;\n \n for(int j=0;j<(1<<18);j++) dp[j]=-INF;\n dp[0]=0;\n \n for(int i=0;i<si(use);i++){\n int x=use[i];\n int z=(1<<18)/(1<<x);\n for(int k=z-1;k>=0;k--){\n int j=(k<<x);\n if(dp[j]<0) continue;\n if(mi[j]>=x){\n chmax(dp[j+(1<<x)],dp[j]+1);\n }\n }\n //謎枝刈り\n }\n \n for(int k=0;k<18;k++){\n chmax(ans,dp[(1<<k)]);\n }\n }\n \n cout<<ans<<\"\\n\";\n }\n}", "accuracy": 1, "time_ms": 3030, "memory_kb": 5448, "score_of_the_acc": -0.892, "final_rank": 13 }, { "submission_id": "aoj_1343_7120033", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\ntypedef long long ll;\ntemplate<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return true; } return false; }\ntemplate<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return true; } return false; }\n#define all(x) (x).begin(),(x).end()\n#define fi first\n#define se second\n#define mp make_pair\n#define si(x) int(x.size())\nconst int mod=998244353,MAX=300005,INF=1<<28;\n\nint dp[2][1<<18];\nint mi[1<<18];\n\nint main(){\n \n std::ifstream in(\"text.txt\");\n std::cin.rdbuf(in.rdbuf());\n cin.tie(0);\n ios::sync_with_stdio(false);\n \n for(int j=0;j<(1<<18);j++){\n for(int k=0;k<18;k++){\n if(j&(1<<k)){\n mi[j]=k;\n break;\n }\n }\n }\n mi[0]=INF;\n \n while(1){\n int N;cin>>N;\n if(N==0) break;\n vector<int> A(N);\n for(int i=0;i<N;i++) cin>>A[i];\n \n int ans=0;\n \n for(int x=1;x<=500;x+=2){\n vector<int> use;\n for(int i=0;i<N;i++){\n if(A[i]%x==0){\n int z=A[i]/x;\n for(int j=0;j<10;j++){\n if((1<<j)==z) use.push_back(j);\n }\n }\n }\n \n if(si(use)==0) continue;\n \n for(int i=0;i<2;i++) for(int j=0;j<(1<<18);j++) dp[i][j]=-INF;\n dp[0][0]=0;\n \n for(int i=0;i<si(use);i++){\n int s=i&1,t=s^1;\n int x=use[i];\n for(int j=0;j<(1<<18);j++){\n if(dp[s][j]<0) continue;\n if(mi[j]>=x){\n chmax(dp[t][j+(1<<x)],dp[s][j]+1);\n }\n chmax(dp[t][j],dp[s][j]);\n dp[s][j]=-INF;\n }\n }\n \n for(int k=0;k<18;k++){\n chmax(ans,dp[si(use)&1][(1<<k)]);\n }\n }\n \n cout<<ans<<\"\\n\";\n }\n}", "accuracy": 1, "time_ms": 5270, "memory_kb": 6464, "score_of_the_acc": -1.4664, "final_rank": 20 }, { "submission_id": "aoj_1343_7118679", "code_snippet": "#line 1 \"a.cpp\"\n// cSpell:disable\n/*\tauthor: Kite_kuma\n\tcreated: 2022.11.22 13:14:29 */\n\n#line 2 \"SPJ-Library/template/kuma.hpp\"\n\n#line 2 \"SPJ-Library/template/basic_func.hpp\"\n\n#include <algorithm>\n#include <cassert>\n#include <iostream>\n#include <vector>\n\ntemplate <class T0, class T1, class T2>\ninline bool in_range(T0 x, T1 lef, T2 rig) {\n\treturn ((lef <= x) && (x < rig));\n}\n\ntemplate <class T>\ninline bool chmin(T &a, T b) {\n\tif(a > b) {\n\t\ta = b;\n\t\treturn true;\n\t}\n\treturn false;\n}\ntemplate <class T>\ninline bool chmax(T &a, T b) {\n\tif(a < b) {\n\t\ta = b;\n\t\treturn true;\n\t}\n\treturn false;\n}\n\nvoid Yes(bool flag = true) { std::cout << (flag ? \"Yes\" : \"No\") << '\\n'; }\nvoid YES(bool flag = true) { std::cout << (flag ? \"YES\" : \"NO\") << '\\n'; }\n\ntemplate <class T>\nvoid drop(T answer) {\n\tstd::cout << answer << '\\n';\n\texit(0);\n}\n\ntemplate <class T>\nvoid vout(std::vector<T> const &v, bool vertically = 0) {\n\tif(vertically) {\n\t\tfor(auto const &a : v) {\n\t\t\tstd::cout << a << '\\n';\n\t\t}\n\t\treturn;\n\t}\n\tfor(auto it = v.begin(); it != v.end(); it++) {\n\t\tstd::cout << (*it);\n\t\tif(it != v.end() - 1) {\n\t\t\tstd::cout << ' ';\n\t\t}\n\t}\n\tstd::cout << '\\n';\n\treturn;\n}\n\ninline void print() { std::cout << '\\n'; }\ntemplate <class T>\ninline void print(const T &x) {\n\tstd::cout << x << '\\n';\n\treturn;\n}\ntemplate <typename Head, typename... Tail>\nvoid print(const Head &H, const Tail &... T) {\n\tstd::cout << H << \" \";\n\tprint(T...);\n}\n\ntemplate <class T>\nvoid add(std::vector<T> &v, T value) {\n\tfor(auto &a : v) a += value;\n\treturn;\n}\n\ntemplate <class T>\nT greatest_lower_multiple(T x, T d) {\n\tif(d == 0) return 0;\n\tif(d < 0) d *= -1;\n\tT y = x % d;\n\tif(y < 0) y += d;\n\treturn x - y;\n}\n\ntemplate <class T>\nT least_upper_multiple(T x, T d) {\n\treturn -greatest_lower_multiple(-x, d);\n}\n\n// ceil(a / b);\ntemplate <class T>\nT dup(T a, T b) {\n\tassert(b);\n\tif(b < 0) {\n\t\ta *= -1;\n\t\tb *= -1;\n\t}\n\treturn least_upper_multiple(a, b) / b;\n}\n\nlong long pow_ll(long long a, long long n) {\n\tassert(n >= 0LL);\n\tif(n == 0) return 1LL;\n\tif(a == 0) return 0LL;\n\tif(a == 1) return 1LL;\n\tif(a == -1) return (n & 1LL) ? -1LL : 1LL;\n\tlong long res = 1;\n\twhile(n > 1LL) {\n\t\tif(n & 1LL) res *= a;\n\t\ta *= a;\n\t\tn >>= 1;\n\t}\n\treturn res * a;\n}\n\nlong long modpow(long long a, long long n, long long mod) {\t // a^n mod\n\tassert(n >= 0 && mod);\n\tif(mod == 1) return 0LL;\n\tlong long res = 1;\n\ta %= mod;\n\twhile(n > 0) {\n\t\tif(n & 1) res = res * a % mod;\n\t\ta = a * a % mod;\n\t\tn >>= 1;\n\t}\n\treturn res < 0 ? res + mod : res;\n}\n\n// return x which satisfies a * x % mod == gcd(a, mod)\n// not (mod | a)\nlong long modinv(long long a, const long long mod) {\n\tlong long b = mod, u = 1, v = 0;\n\twhile(b) {\n\t\tlong long t = a / b;\n\t\ta -= t * b;\n\t\tstd::swap(a, b);\n\t\tu -= t * v;\n\t\tstd::swap(u, v);\n\t}\n\tu %= mod;\n\tif(u < 0) u += mod;\n\treturn u;\n}\n\ntemplate <class T>\nint lb(const std::vector<T> &a, const T x) {\n\treturn (int)std::distance(a.begin(), std::lower_bound(a.begin(), a.end(), x));\n}\ntemplate <class T>\nint ub(const std::vector<T> &a, const T x) {\n\treturn (int)std::distance(a.begin(), std::upper_bound(a.begin(), a.end(), x));\n}\ntemplate <class T>\nvoid unq_sort(std::vector<T> &a) {\n\tstd::sort(a.begin(), a.end());\n\ta.erase(std::unique(a.begin(), a.end()), a.end());\n}\ntemplate <class T>\nstd::vector<int> press(const std::vector<T> &a) {\n\tstd::vector<T> vec = a;\n\tunq_sort(vec);\n\tstd::vector<int> ret;\n\tfor(const auto &v : a) ret.push_back(lb(vec, v));\n\treturn ret;\n}\n#line 1 \"SPJ-Library/template/header.hpp\"\n#include <bits/stdc++.h>\n#line 2 \"SPJ-Library/template/io.hpp\"\n\n#line 4 \"SPJ-Library/template/io.hpp\"\n\n#line 8 \"SPJ-Library/template/debug.hpp\"\n\n#line 3 \"SPJ-Library/template/constants.hpp\"\n\nconstexpr int inf = 1000'000'000;\nconstexpr long long INF = 1'000'000'000'000'000'000LL;\nconstexpr int mod_1000000007 = 1000000007;\nconstexpr int mod_998244353 = 998244353;\nconst long double pi = acosl(-1.);\nconstexpr int dx[8] = {1, 0, -1, 0, 1, -1, -1, 1};\nconstexpr int dy[8] = {0, 1, 0, -1, 1, 1, -1, -1};\n#line 10 \"SPJ-Library/template/debug.hpp\"\n\nnamespace viewer {\n\ntemplate <typename T, typename U>\nvoid view(const std::pair<T, U> &p);\n\ntemplate <class T0, class T1, class T2>\nvoid view(const std::tuple<T0, T1, T2> &p);\n\ntemplate <class T0, class T1, class T2, class T3>\nvoid view(const std::tuple<T0, T1, T2, T3> &p);\n\nvoid view(const long long &e);\n\nvoid view(const int &e);\n\ntemplate <typename T>\nvoid view(const T &e);\n\ntemplate <typename T>\nvoid view(const std::set<T> &s);\n\ntemplate <typename T>\nvoid view(const std::unordered_set<T> &s);\n\ntemplate <typename T>\nvoid view(const std::multiset<T> &s);\n\ntemplate <typename T>\nvoid view(const std::unordered_multiset<T> &s);\n\ntemplate <typename T>\nvoid view(const std::vector<T> &v);\n\ntemplate <typename T, std::size_t ary_size>\nvoid view(const std::array<T, ary_size> &v);\n\ntemplate <typename T>\nvoid view(const std::vector<std::vector<T>> &vv);\n\ntemplate <typename T, typename U>\nvoid view(const std::vector<std::pair<T, U>> &v);\n\ntemplate <class T0, class T1, class T2>\nvoid view(const std::vector<std::tuple<T0, T1, T2>> &v);\n\ntemplate <class T0, class T1, class T2, class T3>\nvoid view(const std::vector<std::tuple<T0, T1, T2, T3>> &v);\n\ntemplate <typename map_type>\nvoid view_map_container(const map_type &m);\n\ntemplate <typename T, typename U>\nvoid view(const std::map<T, U> &m);\n\ntemplate <typename T, typename U>\nvoid view(const std::unordered_map<T, U> &m);\n\ntemplate <typename container_type>\nvoid view_container(const container_type &c, bool vertically = false) {\n\ttypename container_type::const_iterator begin = c.begin();\n\tconst typename container_type::const_iterator end = c.end();\n\tif(vertically) {\n\t\tstd::cerr << \"{\\n\";\n\t\twhile(begin != end) {\n\t\t\tstd::cerr << '\\t';\n\t\t\tview(*(begin++));\n\t\t\tif(begin != end) std::cerr << ',';\n\t\t\tstd::cerr << '\\n';\n\t\t}\n\t\tstd::cerr << '}';\n\t\treturn;\n\t}\n\tstd::cerr << \"{ \";\n\twhile(begin != end) {\n\t\tview(*(begin++));\n\t\tif(begin != end) std::cerr << ',';\n\t\tstd::cerr << ' ';\n\t}\n\tstd::cerr << '}';\n}\n\ntemplate <typename T, typename U>\nvoid view(const std::pair<T, U> &p) {\n\tstd::cerr << '(';\n\tview(p.first);\n\tstd::cerr << \", \";\n\tview(p.second);\n\tstd::cerr << ')';\n}\n\ntemplate <class T0, class T1, class T2>\nvoid view(const std::tuple<T0, T1, T2> &p) {\n\tstd::cerr << '(';\n\tview(std::get<0>(p));\n\tstd::cerr << \", \";\n\tview(std::get<1>(p));\n\tstd::cerr << \", \";\n\tview(std::get<2>(p));\n\tstd::cerr << ')';\n}\n\ntemplate <class T0, class T1, class T2, class T3>\nvoid view(const std::tuple<T0, T1, T2, T3> &p) {\n\tstd::cerr << '(';\n\tview(std::get<0>(p));\n\tstd::cerr << \", \";\n\tview(std::get<1>(p));\n\tstd::cerr << \", \";\n\tview(std::get<2>(p));\n\tstd::cerr << \", \";\n\tview(std::get<3>(p));\n\tstd::cerr << ')';\n}\n\nvoid view(const long long &e) {\n\tif(e == INF)\n\t\tstd::cerr << \"INF\";\n\telse if(e == -INF)\n\t\tstd::cerr << \"-INF\";\n\telse\n\t\tstd::cerr << e;\n}\n\nvoid view(const int &e) {\n\tif(e == inf)\n\t\tstd::cerr << \"inf\";\n\telse if(e == -inf)\n\t\tstd::cerr << \"-inf\";\n\telse\n\t\tstd::cerr << e;\n}\n\ntemplate <typename T>\nvoid view(const T &e) {\n\tstd::cerr << e;\n}\n\ntemplate <typename T>\nvoid view(const std::set<T> &s) {\n\tview_container(s);\n}\n\ntemplate <typename T>\nvoid view(const std::unordered_set<T> &s) {\n\tview_container(s);\n}\n\ntemplate <typename T>\nvoid view(const std::multiset<T> &s) {\n\tview_container(s);\n}\n\ntemplate <typename T>\nvoid view(const std::unordered_multiset<T> &s) {\n\tview_container(s);\n}\n\ntemplate <typename T>\nvoid view(const std::vector<T> &v) {\n\tview_container(v);\n}\n\ntemplate <typename T, std::size_t ary_size>\nvoid view(const std::array<T, ary_size> &v) {\n\tview_container(v);\n}\n\ntemplate <typename T>\nvoid view(const std::vector<std::vector<T>> &vv) {\n\tview_container(vv, true);\n}\n\ntemplate <typename T, typename U>\nvoid view(const std::vector<std::pair<T, U>> &v) {\n\tview_container(v, true);\n}\n\ntemplate <class T0, class T1, class T2>\nvoid view(const std::vector<std::tuple<T0, T1, T2>> &v) {\n\tview_container(v, true);\n}\n\ntemplate <class T0, class T1, class T2, class T3>\nvoid view(const std::vector<std::tuple<T0, T1, T2, T3>> &v) {\n\tview_container(v, true);\n}\n\ntemplate <typename map_type>\nvoid view_map_container(const map_type &m) {\n\tstd::cerr << \"{\\n\";\n\tfor(typename map_type::const_iterator it = m.begin(); it != m.end(); it++) {\n\t\tstd::cerr << \"\\t[\";\n\t\tview(it->first);\n\t\tstd::cerr << \"] : \";\n\t\tview(it->second);\n\t\tstd::cerr << '\\n';\n\t}\n\tstd::cerr << \"}\";\n}\n\ntemplate <typename T, typename U>\nvoid view(const std::map<T, U> &m) {\n\tview_map_container(m);\n}\n\ntemplate <typename T, typename U>\nvoid view(const std::unordered_map<T, U> &m) {\n\tview_map_container(m);\n}\n\n} // namespace viewer\n\n// when compiling : g++ foo.cpp -DLOCAL\n#ifdef LOCAL\nvoid debug_out() {}\ntemplate <typename T>\nvoid debug_out(const T &x) {\n\tviewer::view(x);\n}\ntemplate <typename Head, typename... Tail>\nvoid debug_out(const Head &H, const Tail &... T) {\n\tviewer::view(H);\n\tstd::cerr << \", \";\n\tdebug_out(T...);\n}\n#define debug(...) \\\n\tdo { \\\n\t\tstd::cerr << __LINE__ << \" [\" << #__VA_ARGS__ << \"] : [\"; \\\n\t\tdebug_out(__VA_ARGS__); \\\n\t\tstd::cerr << \"]\\n\"; \\\n\t} while(0)\n#else\n#define debug(...) (void(0))\n#endif\n#line 2 \"SPJ-Library/template/scanner.hpp\"\n\n#line 6 \"SPJ-Library/template/scanner.hpp\"\n\nnamespace scanner {\ntemplate <class T>\nvoid scan(T &a) {\n\tstd::cin >> a;\n}\n\ntemplate <class T, class L>\nvoid scan(std::pair<T, L> &p) {\n\tscan(p.first);\n\tscan(p.second);\n}\n\ntemplate <class T0, class T1, class T2>\nvoid scan(std::tuple<T0, T1, T2> &p) {\n\tT0 t0;\n\tT1 t1;\n\tT2 t2;\n\tscan(t0);\n\tscan(t1);\n\tscan(t2);\n\tp = std::make_tuple(t0, t1, t2);\n}\n\ntemplate <class T0, class T1, class T2, class T3>\nvoid scan(std::tuple<T0, T1, T2, T3> &p) {\n\tT0 t0;\n\tT1 t1;\n\tT2 t2;\n\tT3 t3;\n\tscan(t0);\n\tscan(t1);\n\tscan(t2);\n\tscan(t3);\n\tp = std::make_tuple(t0, t1, t2, t3);\n}\n\ntemplate <class T>\nvoid scan(std::vector<T> &a) {\n\tfor(auto &i : a) scan(i);\n}\n\nvoid INPUT() {}\ntemplate <class Head, class... Tail>\nvoid INPUT(Head &head, Tail &... tail) {\n\tscan(head);\n\tINPUT(tail...);\n}\n} // namespace scanner\n#line 7 \"SPJ-Library/template/io.hpp\"\n\n#define VEC(type, name, size) \\\n\tvector<type> name(size); \\\n\tscanner::INPUT(name)\n#define VVEC(type, name, h, w) \\\n\tvector<vector<type>> name(h, vector<type>(w)); \\\n\tscanner::INPUT(name)\n#define INT(...) \\\n\tint __VA_ARGS__; \\\n\tscanner::INPUT(__VA_ARGS__)\n#define LL(...) \\\n\tlong long __VA_ARGS__; \\\n\tscanner::INPUT(__VA_ARGS__)\n#define STR(...) \\\n\tstring __VA_ARGS__; \\\n\tscanner::INPUT(__VA_ARGS__)\n#define CHR(...) \\\n\tchar __VA_ARGS__; \\\n\tscanner::INPUT(__VA_ARGS__)\n#define DBL(...) \\\n\tdouble __VA_ARGS__; \\\n\tscanner::INPUT(__VA_ARGS__)\n#define LD(...) \\\n\tlong double __VA_ARGS__; \\\n\tscanner::INPUT(__VA_ARGS__)\n#define TPL3(type0, type1, type2, name) \\\n\tstd::tuple<type0, type1, type2> name; \\\n\tscanner::INPUT(name);\n#define TPL4(type0, type1, type2, type3, name) \\\n\tstd::tuple<type0, type1, type2, type3> name; \\\n\tscanner::INPUT(name);\n\ntemplate <typename T1, typename T2>\nstd::istream &operator>>(std::istream &is, std::pair<T1, T2> &p) {\n\tis >> p.first >> p.second;\n\treturn is;\n}\n\ntemplate <typename T1, typename T2>\nstd::ostream &operator<<(std::ostream &os, const std::pair<T1, T2> &p) {\n\tos << p.first << ' ' << p.second;\n\treturn os;\n}\n\nstruct fast_io {\n\tfast_io() {\n\t\tstd::ios::sync_with_stdio(false);\n\t\tstd::cin.tie(nullptr);\n\t\tstd::cout << std::fixed << std::setprecision(15);\n\t\tsrand((unsigned)time(NULL));\n\t}\n} fast_io_;\n#line 2 \"SPJ-Library/template/macros.hpp\"\n\n#define foa(s, v) for(auto &s : v)\n#define all(v) (v).begin(), (v).end()\n#define rall(v) (v).rbegin(), (v).rend()\n#define pcnt(n) __builtin_popcountll((long long)n)\n\n#define REPname(a, b, c, d, e, ...) e\n#define rep(...) REPname(__VA_ARGS__, REP3, REP2, REP1, REP0)(__VA_ARGS__)\n#define REP0(x) for(int Counter_in_rep_macro = 0; Counter_in_rep_macro < (x); ++Counter_in_rep_macro)\n#define REP1(i, x) for(int i = 0; i < (x); ++i)\n#define REP2(i, l, r) for(int i = (l); i < (r); ++i)\n#define REP3(i, l, r, c) for(int i = (l); i < (r); i += (c))\n#define DREPname(a, b, c, d, e, ...) e\n#define drep(...) DREPname(__VA_ARGS__, DREP3, DREP2, DREP1)(__VA_ARGS__)\n#define DREP1(i, x) for(int i = (x)-1; i >= 0; --i)\n#define DREP2(i, l, r) for(int i = (r)-1; i >= (l); --i)\n#define DREP3(i, l, r, c) for(int i = (r)-1; i >= (l); i -= (c))\n\nusing ll = long long;\nusing ld = long double;\nusing ull = unsigned long long;\nusing vi = std::vector<int>;\nusing vvi = std::vector<std::vector<int>>;\nusing vvvi = std::vector<std::vector<std::vector<int>>>;\nusing vll = std::vector<ll>;\nusing vvll = std::vector<vll>;\nusing vvvll = std::vector<vvll>;\ntemplate <class T = ll>\nusing V = std::vector<T>;\ntemplate <class T = ll>\nusing VV = V<V<T>>;\ntemplate <class T = ll>\nusing VVV = V<V<V<T>>>;\ntemplate <class T = ll>\nusing pqup = std::priority_queue<T, std::vector<T>, std::greater<T>>;\ntemplate <class T = ll>\nusing pqdn = std::priority_queue<T>;\n#line 7 \"SPJ-Library/template/kuma.hpp\"\n\nusing namespace std;\n#line 6 \"a.cpp\"\nconstexpr int val_max = 512;\nint maxval(vector<int> a) {\n\tconst int n = int(a.size());\n\tif(n == 0) return 0;\n\tdebug(a);\n\tconst int smax = accumulate(all(a), 10);\n\tvector<int> dp(smax, -inf);\n\tdp[0] = 0;\n\tfoa(v, a) {\n\t\tauto tmp = dp;\n\t\tfor(int from = 0; from + v < smax; from += v) {\n\t\t\tif(dp[from] < 0) continue;\n\t\t\tif(from == 0) {\n\t\t\t\tdebug(from + v, from);\n\t\t\t\tdebug(dp[from + v], dp[from]);\n\t\t\t}\n\t\t\tchmax(tmp[from + v], dp[from] + 1);\n\t\t}\n\t\tswap(tmp, dp);\n\t\tdebug(v);\n\t\trep(j, smax) {\n\t\t\tif(dp[j] > 0) {\n\t\t\t\tdebug(j, dp[j]);\n\t\t\t}\n\t\t}\n\t}\n\tint ans = 0;\n\tfor(int i = 1; i < smax; i <<= 1) {\n\t\tchmax(ans, dp[i]);\n\t}\n\tdebug(dp);\n\tdebug(ans);\n\treturn ans;\n}\nint solve(int n) {\n\tvector<int> a(n);\n\tfoa(t, a) cin >> t;\n\tvector<vector<int>> dived(val_max);\n\tfoa(value, a) {\n\t\tint mult = 1;\n\t\twhile(value % 2 == 0) {\n\t\t\tvalue /= 2;\n\t\t\tmult *= 2;\n\t\t}\n\t\tdived[value].push_back(mult);\n\t}\n\tint ans = 0;\n\tfor(auto row : dived) {\n\t\tchmax(ans, maxval(row));\n\t}\n\treturn ans;\n}\nint main() {\n\tint n;\n\twhile(cin >> n && n) {\n\t\tcout << solve(n) << '\\n';\n\t}\n\treturn 0;\n}", "accuracy": 1, "time_ms": 410, "memory_kb": 4164, "score_of_the_acc": -0.1969, "final_rank": 6 }, { "submission_id": "aoj_1343_6556285", "code_snippet": "#include<iostream>\n#include<vector>\n#define rep(i,a,b) for(int i = (a); i <= (b); i++)\n#define per(i,b,a) for(int i = (b); i >= (a); i--)\n#define N 1010\n#define M 260000\nusing namespace std;\n\nint a[N], dp[M], sum[N], n;\nvector<int> p[N];\n\nint main(){\n ios::sync_with_stdio(false);\n while(cin>>n && n){\n rep(i,1,n){\n cin>>a[i];\n int tmp = a[i];\n while(tmp%2 == 0) tmp /= 2;\n p[tmp].push_back(a[i]/tmp), sum[tmp] += a[i]/tmp;\n }\n\n int ans = 0;\n rep(o,1,500) if(p[o].size()){\n int m = p[o].size();\n rep(i,0,sum[o]) dp[i] = -1;\n dp[0] = 0;\n rep(i,1,m){\n int k = p[o][i-1];\n for(int j = sum[o]/k*k; j >= 0; j -= k) if(~dp[j])\n dp[j+k] = max(dp[j+k], dp[j]+1);\n }\n rep(k,0,17) if((1<<k) <= sum[o]) ans = max(ans, dp[1<<k]);\n }\n cout<<ans<<endl;\n\n rep(i,0,500) p[i].clear(), sum[i] = 0;\n }\n return 0;\n}", "accuracy": 1, "time_ms": 410, "memory_kb": 3692, "score_of_the_acc": -0.0818, "final_rank": 1 }, { "submission_id": "aoj_1343_5801160", "code_snippet": "/*学习古代文章,诗词\n醉翁亭记 北宋·欧阳修\n\n环滁皆山也。其西南诸峰,林壑尤美。望之蔚然而深秀者,琅琊也。山行六七里,渐闻水声潺潺,而泻出于两峰之间者,让泉也。峰回路转,有亭翼然临于泉上者,醉翁亭也。作亭者谁?山之僧曰智仙也。名之者谁?太守自谓也。太守与客来饮于此,饮少辄醉,而年又最高,故自号曰醉翁也。醉翁之意不在酒,在乎山水之间也。山水之乐,得之心而寓之酒也。\n\n若夫日出而林霏开,云归而岩穴暝,晦明变化者,山间之朝暮也。野芳发而幽香,佳木秀而繁阴,风霜高洁,水落而石出者,山间之四时也。朝而往,暮而归,四时之景不同,而乐亦无穷也。\n\n至于负者歌于途,行者休于树,前者呼,后者应,伛偻提携,往来而不绝者,滁人游也。临谿而渔,谿深而鱼肥;酿泉为酒,泉香而酒洌;山肴野蔌,杂然而前陈者,太守宴也。宴酣之乐,非丝非竹,射者中,弈者胜,觥筹交错,起坐而喧哗者,众宾欢也;苍颜白发,颓然乎其间者,太守醉也。\n\n已而夕阳在山,人影散乱,太守归而宾客从也。树林阴翳,鸣声上下,游人去而禽鸟乐也。然而禽鸟知山林之乐,而不知人之乐;人知从太守游而乐,而不知太守之乐其乐也。醉能同其乐,醒能述以文者,太守也。太守谓谁?庐陵欧阳修也。\n\n岳阳楼记 北宋·范仲淹\n\n庆历四年春,滕子京谪守巴陵郡。越明年,政通人和,百废具兴,乃重修岳阳楼,增其旧制,刻唐贤今人诗赋于其上;属予作文以记之。\n\n予观夫巴陵胜状,在洞庭一湖。衔远山,吞长江,浩浩汤汤,横无际涯;朝晖夕阴,气象万千;此则岳阳楼之大观也,前人之述备矣。然则北通巫峡,南极潇湘,迁客骚人,多会于此,览物之情,得无异乎?\n\n若夫霪雨霏霏,连月不开;阴风怒号,浊浪排空;日星隐曜,山岳潜形;商旅不行,樯倾楫摧;薄暮冥冥,虎啸猿啼。登斯楼也,则有去国怀乡,忧谗畏讥,满目萧然,感极而悲者矣。\n\n至若春和景明,波澜不惊,上下天光,一碧万顷;沙鸥翔集,锦鳞游泳,岸芷汀兰,郁郁青青。而或长烟一空,皓月千里,浮光跃金,静影沉璧,渔歌互答,此乐何极。登斯楼也,则有心旷神怡,宠辱皆忘,把酒临风,其喜洋洋者矣。\n\n嗟夫!予尝求古仁人之心,或异二者之为,何哉?不以物喜,不以己悲。居庙堂之高,则忧其民;处江湖之远,则忧其君。是进亦忧,退亦忧;然则何时而乐耶?其必曰:“先天下之忧而忧,后天下之乐而乐”欤!噫!微斯人,吾谁与归?\n\n马说 唐·韩愈\n\n世有伯乐,然后有千里马。千里马常有,而伯乐不常有。故虽有名马,祗辱于奴隶人之手,骈死于槽枥之间,不以千里称也。\n\n马之千里者,一食或尽粟一石。食马者不知其能千里而食也。是马也,虽有千里之能,食不饱,力不足,才美不外见,且欲与常马等不可得,安求其能千里也?\n\n策之不以其道,食之不能尽其材,鸣之而不能通其意,执策而临之,曰:“天下无马!”呜呼!其真无马邪?其真不知马也!\n*/\n//Generated at 2021-08-18 21:36:06 UTC+8\n//Created by Alphagocc\n#ifndef TYPE_HPP\n#define TYPE_HPP\n#include <type_traits>\n#ifndef __cpp_lib_void_t\nnamespace std\n{\ntemplate <typename...> using void_t = void;\n}\n#endif\ntemplate <typename T, typename _ = void> struct is_container : std::false_type\n{};\ntemplate <typename... Ts> struct is_container_helper\n{};\ntemplate <typename T>\nstruct is_container<T,\n typename std::conditional<false,\n is_container_helper<decltype(std::declval<T>().size()),\n decltype(std::declval<T>().begin()),\n decltype(std::declval<T>().end()),\n decltype(std::declval<T>().cbegin()),\n decltype(std::declval<T>().cend())>,\n void>::type> : public std::true_type\n{};\n#endif\n#include <bits/stdc++.h>\n#define FORCE_INLINE __inline__ __attribute__((always_inline))\nclass IO\n{\n static const int bufSize = 1 << 18;\n\n char inBuf[bufSize], outBuf[bufSize];\n char *ip1 = inBuf, *ip2 = inBuf;\n int goodReadBit = 1, op1 = -1, op2 = bufSize - 1;\n FORCE_INLINE int gc()\n {\n return ip1 == ip2\n && (ip2 = (ip1 = inBuf) + fread(inBuf, 1, bufSize, stdin),\n ip1 == ip2)\n ? (goodReadBit = 0, EOF)\n : *ip1++;\n }\n template <typename T> FORCE_INLINE void __RI(T &x)\n {\n int ch = gc(), neg = 1;\n x = 0;\n for (; !(isdigit(ch) || ch == '-' || ch == EOF); ch = gc()) {}\n if (ch == EOF) return;\n if (ch == '-') neg = -1, ch = gc();\n for (; isdigit(ch); ch = gc()) x = x * 10 + (ch - 48) * neg;\n }\n template <typename T> FORCE_INLINE void __RC(T &x)\n {\n int ch;\n while (isspace(ch = gc())) {}\n if (ch == EOF) return;\n x = ch;\n }\n FORCE_INLINE void __RS(std::string &x)\n {\n char ch;\n x.clear();\n for (ch = gc(); isspace(ch); ch = gc()) {}\n if (ch == EOF) return;\n for (; !isspace(ch) && ch != EOF; ch = gc()) x.push_back(ch);\n }\n\npublic:\n FORCE_INLINE IO &R(char &x) { return __RC(x), (*this); }\n FORCE_INLINE IO &R(unsigned char &x) { return __RC(x), (*this); }\n FORCE_INLINE IO &R(std::string &x) { return __RS(x), (*this); }\n template <typename T1, typename T2> FORCE_INLINE IO &R(std::pair<T1, T2> &x)\n {\n return R(x.first), R(x.second), (*this);\n }\n template <typename T, typename... Args> FORCE_INLINE IO &RA(T *a, int n)\n {\n for (int i = 0; i < n; ++i) R(a[i]);\n return (*this);\n }\n template <typename T, typename... Args>\n FORCE_INLINE IO &R(T &x, Args &...args)\n {\n return R(x), R(args...), (*this);\n }\n template <typename T, typename... Args>\n FORCE_INLINE IO &RA(T *a, int n, Args... args)\n {\n for (int i = 0; i < n; ++i) RA(a[i], args...);\n return (*this);\n }\n template <typename T>\n FORCE_INLINE typename std::enable_if<std::is_integral<T>::value, IO>::type &\n R(T &x)\n {\n return __RI(x), (*this);\n }\n template <typename T>\n FORCE_INLINE typename std::enable_if<is_container<T>::value, IO>::type &R(\n T &x)\n {\n for (auto &i : x) R(i);\n return (*this);\n }\n template <typename T>\n FORCE_INLINE typename std::enable_if<\n std::is_void<std::void_t<decltype(std::declval<T>().read())>>::value,\n IO>::type &\n R(T &x)\n {\n return x.read(), (*this);\n }\n\nprivate:\n char space = ' ';\n FORCE_INLINE void pc(const char &x)\n {\n if (op1 == op2) flush();\n outBuf[++op1] = x;\n }\n template <typename T> FORCE_INLINE void __WI(T x)\n {\n static char buf[sizeof(T) * 16 / 5];\n static int len = -1;\n if (x >= 0) {\n do {\n buf[++len] = x % 10 + 48, x /= 10;\n } while (x);\n } else {\n pc('-');\n do {\n buf[++len] = -(x % 10) + 48, x /= 10;\n } while (x);\n }\n while (len >= 0) { pc(buf[len]), --len; }\n }\n\npublic:\n FORCE_INLINE void flush() { fwrite(outBuf, 1, op1 + 1, stdout), op1 = -1; }\n FORCE_INLINE IO &W(const char &x) { return pc(x), (*this); }\n FORCE_INLINE IO &W(const char *x)\n {\n while (*x != '\\0') pc(*(x++));\n return (*this);\n }\n FORCE_INLINE IO &W(const std::string &x) { return W(x.c_str()), (*this); }\n template <typename T1, typename T2>\n FORCE_INLINE IO &W(const std::pair<T1, T2> &x)\n {\n WS(x.first);\n W(x.second);\n return (*this);\n }\n FORCE_INLINE IO &WL() { return W('\\n'), (*this); }\n template <typename T> FORCE_INLINE IO &WL(const T &x)\n {\n return W(x), W('\\n'), (*this);\n }\n FORCE_INLINE IO &WS() { return W(space), (*this); }\n template <typename T> FORCE_INLINE IO &WS(const T &x)\n {\n return W(x), W(space), (*this);\n }\n template <typename T> FORCE_INLINE IO &WA(T *a, int n)\n {\n for (int i = 0; i < n; i++) WS(a[i]);\n WL();\n return (*this);\n }\n template <typename T, typename... Args>\n FORCE_INLINE IO &W(const T &x, const Args &...args)\n {\n W(x), W(space), W(args...);\n return (*this);\n }\n template <typename T, typename... Args>\n FORCE_INLINE IO &WS(const T &x, const Args &...args)\n {\n return W(x), W(space), W(args...), W(space), (*this);\n }\n template <typename... Args> FORCE_INLINE IO &WL(const Args &...args)\n {\n return W(args...), W('\\n'), (*this);\n }\n template <typename T, typename... Args>\n FORCE_INLINE IO &WA(T *a, int n, Args... args)\n {\n for (int i = 0; i < n; i++) WA(a[i], args...);\n return (*this);\n }\n template <typename T>\n FORCE_INLINE typename std::enable_if<std::is_integral<T>::value, IO>::type &\n W(const T &x)\n {\n return __WI(x), (*this);\n }\n template <typename T>\n FORCE_INLINE typename std::enable_if<is_container<T>::value, IO>::type &W(\n const T &x)\n {\n\n for (auto &i : x) WS(i);\n WL();\n return (*this);\n }\n template <typename T>\n FORCE_INLINE typename std::enable_if<\n std::is_void<std::void_t<decltype(std::declval<T>().write())>>::value,\n IO>::type &\n W(const T &x)\n {\n return x.write(), (*this);\n }\n template <typename T> FORCE_INLINE IO &operator>>(T &x)\n {\n R(x);\n return (*this);\n }\n template <typename T> FORCE_INLINE IO &operator<<(const T &x)\n {\n W(x);\n return (*this);\n }\n int good() { return goodReadBit; }\n IO() {}\n ~IO() { flush(); }\n} io;\n#undef FORCE_INLINE\n\n#ifndef UTIL_HPP\n#define UTIL_HPP\n#include <bits/stdc++.h>\n#define var auto\n#define ALL(x) x.begin(), x.end()\nconst std::int32_t INF = 0x3f3f3f3f;\nconst std::int64_t INFL = 0x3f3f3f3f3f3f3f3f;\n#define If(x, y, z) ((x) ? (y) : (z))\n#endif\nusing namespace std;\nint f(const vector<int> &a)\n{\n vector<int> dp(accumulate(ALL(a), 0) + 1, -1);\n // io.WL(a);\n int sum = 0;\n dp[0] = 0;\n for (auto i : a) {\n for (int j = sum & -i; j >= 0; j -= i) {\n if (dp[j] != -1) dp[j + i] = max(dp[j + i], dp[j] + 1);\n }\n sum += i;\n }\n int ans = 1;\n for (int i = 1; i < dp.size(); i <<= 1) ans = max(ans, dp[i]);\n return ans;\n}\nint main()\n{\n int n;\n while (1) {\n io >> n;\n if (n == 0) break;\n vector<int> a(n);\n io >> a;\n int ans = 1;\n map<int, vector<int>> v;\n for (auto i : a) { v[i / (i & -i)].push_back(i & -i); }\n for (auto [ignore, x] : v) {\n int k = 1;\n do {\n ans = max(ans, f(x));\n x.erase(\n remove_if(ALL(x), [&](int i) { return i <= k; }), x.end());\n k <<= 1;\n } while (!x.empty());\n }\n io.WL(ans);\n }\n return 0;\n}", "accuracy": 1, "time_ms": 230, "memory_kb": 3984, "score_of_the_acc": -0.1268, "final_rank": 4 }, { "submission_id": "aoj_1343_5798469", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n#define SZ(x) ((int)(x).size())\n#define rep(i, a) for (int i = 0; i < (a); ++i)\n#define repi(i, a) for (int i = 1; i <= (a); ++i)\ntypedef long long ll;\nconst int Inf = 0x3f3f3f3f;\n\nint N;\nint a[1005];\nvector<int> ss[505];\nint dp[500005];\n\nint get(vector<int> &v) {\n\tint sm = accumulate(v.begin(), v.end(), 0);\n\tmemset(dp, -1, sizeof(int) * (sm + 1));\n\tdp[0] = 0;\n\tint nsm = 0;\n\trep(i, SZ(v)) {\n\t\tint val = v[i]; nsm += val;\n\t\tfor (int j = nsm / val * val; j >= val; j -= val) if (~dp[j - val]) dp[j] = max(dp[j], dp[j - val] + 1);\n\t}\n\tint ans = 0;\n\tfor (int i = 1; i <= sm; i <<= 1) ans = max(ans, dp[i]);\n\treturn ans;\n}\n\nvoid solve() {\n\trepi(i, 500) ss[i].clear();\n\trepi(i, N) {\n\t\tscanf(\"%d\", a + i);\n\t\tint x = a[i];\n\t\twhile (!(x & 1)) x >>= 1;\n\t\tss[x].push_back(a[i] / x);\n\t}\n\tint ans = 0;\n\trepi(i, 500) ans = max(ans, get(ss[i]));\n\tprintf(\"%d\\n\", ans);\n}\n\nint main() {\n\twhile (scanf(\"%d\", &N) && N) solve();\n\treturn 0;\n}", "accuracy": 1, "time_ms": 150, "memory_kb": 4020, "score_of_the_acc": -0.1239, "final_rank": 3 }, { "submission_id": "aoj_1343_5797917", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\nint Tc,n,a[1003];\nvector<int>arr[503];\nint f[500003],len,fs[500003];\nint solve(vector<int>v){\n\tmemset(f,-1,sizeof(f));\n\tf[0]=0,len=0;\n\tfor(int i=0;i<v.size();i++){\n\t\tint x=v[i];\n\t\tint nlen=len;\n\t\tfor(int j=len;~j;j--)if(~f[j]&&(j==0||fs[j]>=x)){\n\t\t\tf[j+(1<<x)]=max(f[j+(1<<x)],f[j]+1);\n\t\t\tnlen=max(nlen,j+(1<<x));\n\t\t}\n\t\tlen=nlen;\n\t}\n\tint ans=0;\n\tfor(int i=0;i<19;i++)ans=max(ans,f[1<<i]);\n\treturn ans;\n}\nint main(){\n\tfor(int i=1;i<500003;i++)fs[i]=__builtin_ffs(i)-1;\n\twhile(~scanf(\"%d\",&n)){\n\t\tif(n==0)break;\n\t\tfor(int i=1;i<503;i++)arr[i].clear();\n\t\tfor(int i=0;i<n;i++){\n\t\t\tint x;scanf(\"%d\",&x);\n\t\t\tint cnt=0;\n\t\t\twhile(x%2==0)cnt++,x/=2;\n\t\t\tarr[x].push_back(cnt);\n\t\t}\n\t\tint ans=1;\n\t\tfor(int i=1;i<503;i++)if(!arr[i].empty()){\n\t\t\tint tmp=solve(arr[i]);\n\t\t\tans=max(ans,tmp);\n\t\t}\n\t\tprintf(\"%d\\n\",ans);\n\t}\n}", "accuracy": 1, "time_ms": 630, "memory_kb": 7264, "score_of_the_acc": -0.9851, "final_rank": 15 }, { "submission_id": "aoj_1343_5286922", "code_snippet": "//#define _GLIBCXX_DEBUG\n\n#include<bits/stdc++.h>\nusing namespace std;\n\n#define endl '\\n'\n#define lfs cout<<fixed<<setprecision(10)\n#define ALL(a) (a).begin(),(a).end()\n#define ALLR(a) (a).rbegin(),(a).rend()\n#define spa << \" \" <<\n#define fi first\n#define se second\n#define MP make_pair\n#define MT make_tuple\n#define PB push_back\n#define EB emplace_back\n#define rep(i,n,m) for(ll i = (n); i < (ll)(m); i++)\n#define rrep(i,n,m) for(ll i = (ll)(m) - 1; i >= (ll)(n); i--)\nusing ll = long long;\nusing ld = long double;\nconst ll MOD1 = 1e9+7;\nconst ll MOD9 = 998244353;\nconst ll INF = 1e18;\nusing P = pair<ll, ll>;\ntemplate<typename T> using PQ = priority_queue<T>;\ntemplate<typename T> using QP = priority_queue<T,vector<T>,greater<T>>;\ntemplate<typename T1, typename T2>bool chmin(T1 &a,T2 b){if(a>b){a=b;return true;}else return false;}\ntemplate<typename T1, typename T2>bool chmax(T1 &a,T2 b){if(a<b){a=b;return true;}else return false;}\nll median(ll a,ll b, ll c){return a+b+c-max({a,b,c})-min({a,b,c});}\nvoid ans1(bool x){if(x) cout<<\"Yes\"<<endl;else cout<<\"No\"<<endl;}\nvoid ans2(bool x){if(x) cout<<\"YES\"<<endl;else cout<<\"NO\"<<endl;}\nvoid ans3(bool x){if(x) cout<<\"Yay!\"<<endl;else cout<<\":(\"<<endl;}\ntemplate<typename T1,typename T2>void ans(bool x,T1 y,T2 z){if(x)cout<<y<<endl;else cout<<z<<endl;} \ntemplate<typename T>void debug(T &v,ll h,ll w,string sv=\" \"){for(ll i=0;i<h;i++){cout<<v[i][0];for(ll j=1;j<w;j++)cout<<sv<<v[i][j];cout<<endl;}};\ntemplate<typename T>void debug(T &v,ll n,string sv=\" \"){if(n!=0)cout<<v[0];for(ll i=1;i<n;i++)cout<<sv<<v[i];cout<<endl;};\ntemplate<typename T>vector<vector<T>>vec(ll x, ll y, T w){vector<vector<T>>v(x,vector<T>(y,w));return v;}\nll gcd(ll x,ll y){ll r;while(y!=0&&(r=x%y)!=0){x=y;y=r;}return y==0?x:y;}\nvector<ll>dx={1,-1,0,0,1,1,-1,-1};vector<ll>dy={0,0,1,-1,1,-1,1,-1};\ntemplate<typename T>vector<T> make_v(size_t a,T b){return vector<T>(a,b);}\ntemplate<typename... Ts>auto make_v(size_t a,Ts... ts){return vector<decltype(make_v(ts...))>(a,make_v(ts...));}\ntemplate<typename T1, typename T2>ostream &operator<<(ostream &os, const pair<T1, T2>&p){return os << p.first << \" \" << p.second;}\ntemplate<typename T>ostream &operator<<(ostream &os, const vector<T> &v){for(auto &z:v)os << z << \" \";cout<<\"|\"; return os;}\ntemplate<typename T>void rearrange(vector<int>&ord, vector<T>&v){\n auto tmp = v;\n for(int i=0;i<tmp.size();i++)v[i] = tmp[ord[i]];\n}\ntemplate<typename Head, typename... Tail>void rearrange(vector<int>&ord,Head&& head, Tail&&... tail){\n rearrange(ord, head);\n rearrange(ord, tail...);\n}\ntemplate<typename T> vector<int> ascend(const vector<T>&v){\n vector<int>ord(v.size());iota(ord.begin(),ord.end(),0);\n sort(ord.begin(),ord.end(),[&](int i,int j){return v[i]<v[j];});\n return ord;\n}\ntemplate<typename T> vector<int> descend(const vector<T>&v){\n vector<int>ord(v.size());iota(ord.begin(),ord.end(),0);\n sort(ord.begin(),ord.end(),[&](int i,int j){return v[i]>v[j];});\n return ord;\n}\nll FLOOR(ll n,ll div){return n>=0?n/div:(n-div+1)/div;}\nll CEIL(ll n,ll div){return n>=0?(n+div-1)/div:n/div;}\n//mt19937 mt(chrono::steady_clock::now().time_since_epoch().count());\nint popcount(ll x){return __builtin_popcountll(x);};\nint poplow(ll x){return __builtin_ctzll(x);};\nint pophigh(ll x){return 63 - __builtin_clzll(x);};\ntemplate<typename T>T poll(queue<T> &q){auto ret=q.front();q.pop();return ret;};\ntemplate<typename T>T poll(priority_queue<T> &q){auto ret=q.top();q.pop();return ret;};\ntemplate<typename T>T poll(QP<T> &q){auto ret=q.top();q.pop();return ret;};\ntemplate<typename T>T poll(stack<T> &s){auto ret=s.top();s.pop();return ret;};\ntemplate< typename T = int >\nstruct edge {\n int to;\n T cost;\n int id;\n edge() = default;\n edge(int to, T cost = 1, int id = -1):to(to), cost(cost), id(id){}\n operator int() const { return to; }\n};\n\ntemplate<typename T>\nusing Graph = vector<vector<edge<T>>>;\ntemplate<typename T>\nGraph<T> readGraph(int n,int m,int indexed=1,bool directed=false,bool weighted=false){\n Graph<T> ret(n);\n for(int es = 0; es < m; es++){\n int u,v,w=1;\n cin>>u>>v;u-=indexed,v-=indexed;\n if(weighted)cin>>w;\n ret[u].emplace_back(v,w,es);\n if(!directed)ret[v].emplace_back(u,w,es);\n }\n return ret;\n}\ntemplate<typename T>\nGraph<T> readParent(int n,int indexed=1,bool directed=true){\n Graph<T>ret(n);\n for(int i=1;i<n;i++){\n int p;cin>>p;\n p-=indexed;\n ret[p].emplace_back(i);\n if(!directed)ret[i].emplace_back(p);\n }\n return ret;\n}\n\nint main(){\n cin.tie(nullptr);\n ios_base::sync_with_stdio(false);\n ll res=0,buf=0;\n bool judge = true;\n while(1){\n ll n;cin>>n;\n if(n==0)break;\n vector<ll>a(n);\n rep(i,0,n)cin>>a[i];\n ll sz=505;\n vector<vector<ll>>va(sz);\n rep(i,0,n){\n ll div=1;\n while(a[i]%div==0){\n va[a[i]/div].PB(div);\n div*=2;\n }\n }\n ll ret=0;\n for(auto &v:va){\n ll sum=0;\n for(auto z:v)sum+=z;\n vector<ll>dp(sum+1,-INF);\n dp[0]=0;\n for(auto z:v){\n for(ll k=(sum-z)/z*z;k>=0;k-=z){\n chmax(dp[k+z],dp[k]+1);\n }\n }\n for(ll i=1;i<=sum;i*=2)chmax(ret,dp[i]);\n }\n cout<<ret<<endl;\n }\n return 0;\n}", "accuracy": 1, "time_ms": 390, "memory_kb": 4444, "score_of_the_acc": -0.2623, "final_rank": 8 }, { "submission_id": "aoj_1343_4705369", "code_snippet": "#include <iostream>\n#include <cstdio>\n#include <cstring>\n#include <string>\n#include <ctime>\n#include <vector>\n#include <cmath>\n#include <queue>\n#include <map>\n#include <set>\n#include <stack>\n#include <algorithm>\n//#include <bits/stdc++.h>\nusing namespace std;\n\ntypedef long long LL;\n\nconst int SZ = 1005,SZ0 = 256005;\n\nint n,a[SZ],dp[SZ0];\nbool bas[SZ];bool vis[SZ];\nint sl,B[SZ],sum[SZ];\n\ntemplate<typename Type>inline void read(Type &xx){\n Type f = 1;char ch;xx = 0;\n for(ch = getchar();ch < '0' || ch > '9';ch = getchar()) if(ch == '-') f = -1;\n for(;ch >= '0' && ch <= '9';ch = getchar()) xx = xx * 10 + ch - '0';\n xx *= f;\n}\n\nint DP(){\n for(int i = 1;i <= sl;i++) sum[i] = sum[i - 1] + B[i];\n dp[0] = 0;\n for(int i = 1;i <= sl;i++){\n int mn = B[i];\n for(int j = sum[i] / mn * mn;j >= (mn << 1);j -= mn)\n dp[j] = max(dp[j],dp[j - mn] + 1);\n dp[mn] = max(dp[mn],1);\n }\n int ans = 0;\n for(int i = 1;i <= sum[sl];i <<= 1) ans = max(ans,dp[i]);\n memset(dp,0x8f,sizeof dp);\n return ans;\n}\n\nbool is_least(int idx){\n if(vis[a[idx]]) return false;\n vis[a[idx]] = true;\n for(int j = 1;j <= n;++j)\n if(a[idx] % a[j] == 0 && bas[a[idx] / a[j]] && a[idx] > a[j])\n return false;\n return true;\n}\n\nint main(int argc, char** argv) {\n for(int i = 1;i < SZ;i <<= 1) bas[i] = true;\n while(scanf(\"%d\",&n) != EOF && n){\n memset(dp,0x8f,sizeof dp);memset(vis,0,sizeof vis);\n for(int i = 1;i <= n;i++) read(a[i]);\n int ans = 0;\n for(int i = 1;i <= n;++i){\n if(!is_least(i)) continue;\n sl = 0;\n for(int j = 1;j <= n;++j)\n if(a[j] % a[i] == 0 && bas[a[j] / a[i]])\n B[++sl] = a[j] / a[i];\n ans = max(ans,DP());\n }\n printf(\"%d\\n\",ans);\n }\n return 0;\n}", "accuracy": 1, "time_ms": 290, "memory_kb": 4228, "score_of_the_acc": -0.195, "final_rank": 5 }, { "submission_id": "aoj_1343_4145126", "code_snippet": "#include<iostream>\n#include<string>\n#include<cstdio>\n#include<vector>\n#include<cmath>\n#include<algorithm>\n#include<functional>\n#include<iomanip>\n#include<queue>\n#include<ciso646>\n#include<random>\n#include<map>\n#include<set>\n#include<bitset>\n#include<stack>\n#include<unordered_map>\n#include<utility>\n#include<cassert>\n#include<complex>\nusing namespace std;\n\n//#define int long long\ntypedef long long ll;\n\ntypedef unsigned long long ul;\ntypedef unsigned int ui;\nconst ll mod = 1000000007;\nconst ll INF = (1e+18) + 7;\ntypedef pair<int, int>P;\n#define stop char nyaa;cin>>nyaa;\n#define rep(i,n) for(int i=0;i<n;i++)\n#define per(i,n) for(int i=n-1;i>=0;i--)\n#define Rep(i,sta,n) for(int i=sta;i<n;i++)\n#define rep1(i,n) for(int i=1;i<=n;i++)\n#define per1(i,n) for(int i=n;i>=1;i--)\n#define Rep1(i,sta,n) for(int i=sta;i<=n;i++)\n#define all(v) (v).begin(),(v).end()\ntypedef pair<ll, ll> LP;\ntypedef long double ld;\ntypedef pair<ld, ld> LDP;\nconst ld eps = 1e-6;\nconst ld pi = acos(-1.0);\n//typedef vector<vector<ll>> mat;\ntypedef vector<int> vec;\n\nll mod_pow(ll a, ll n) {\n\tll res = 1;\n\twhile (n) {\n\t\tif (n & 1)res = res * a%mod;\n\t\ta = a * a%mod; n >>= 1;\n\t}\n\treturn res;\n}\n\nstruct modint {\n\tll n;\n\tmodint() :n(0) { ; }\n\tmodint(ll m) :n(m) {\n\t\tif (n >= mod)n %= mod;\n\t\telse if (n < 0)n = (n%mod + mod) % mod;\n\t}\n\toperator int() { return n; }\n};\nbool operator==(modint a, modint b) { return a.n == b.n; }\nmodint operator+=(modint &a, modint b) { a.n += b.n; if (a.n >= mod)a.n -= mod; return a; }\nmodint operator-=(modint &a, modint b) { a.n -= b.n; if (a.n < 0)a.n += mod; return a; }\nmodint operator*=(modint &a, modint b) { a.n = ((ll)a.n*b.n) % mod; return a; }\nmodint operator+(modint a, modint b) { return a += b; }\nmodint operator-(modint a, modint b) { return a -= b; }\nmodint operator*(modint a, modint b) { return a *= b; }\nmodint operator^(modint a, int n) {\n\tif (n == 0)return modint(1);\n\tmodint res = (a*a) ^ (n / 2);\n\tif (n % 2)res = res * a;\n\treturn res;\n}\n\nll inv(ll a, ll p) {\n\treturn (a == 1 ? 1 : (1 - p * inv(p%a, a)) / a + p);\n}\nmodint operator/(modint a, modint b) { return a * modint(inv(b, mod)); }\n\n//const int max_n = 1 << 22;\n//modint fact[max_n], factinv[max_n];\n//void init_f() {\n//\tfact[0] = modint(1);\n//\tfor (int i = 0; i < max_n - 1; i++) {\n//\t\tfact[i + 1] = fact[i] * modint(i + 1);\n//\t}\n//\tfactinv[max_n - 1] = modint(1) / fact[max_n - 1];\n//\tfor (int i = max_n - 2; i >= 0; i--) {\n//\t\tfactinv[i] = factinv[i + 1] * modint(i + 1);\n//\t}\n//}\n//modint comb(int a, int b) {\n//\tif (a < 0 || b < 0 || a < b)return 0;\n//\treturn fact[a] * factinv[b] * factinv[a - b];\n//}\n\nvector<int> b[501];\nint n;\n\nvoid normalize(vector<P> &v) {\n\tvector<P> res;\n\tsort(all(v), greater<P>());\n\tfor (P p : v) {\n\t\tif (res.size() && p.second <= res.back().second)continue;\n\t\tres.push_back(p);\n\t}\n\tswap(v, res);\n}\nvoid solve() {\n\trep1(i, 500)b[i].clear();\n\tvector<int> a(n);\n\trep(i, n) {\n\t\tcin >> a[i];\n\t\tint cop = a[i];\n\t\tint tmp = 0;\n\t\twhile (cop % 2 == 0) {\n\t\t\tcop /= 2; tmp++;\n\t\t}\n\t\tb[cop].push_back(tmp);\n\t}\n\tint ans = 0;\n\trep1(i, 500) {\n\t\tif (b[i].empty())continue;\n\t\tvector<int> dp((1 << 18)+1,-mod);\n\t\tdp[0] = 0;\n\t\tfor (int t : b[i]) {\n\t\t\tper(j, (1 << (18 - t))) {\n\t\t\t\tint tj = j << t;\n\t\t\t\tint nj = tj + (1 << t);\n\t\t\t\tdp[nj] = max(dp[nj], dp[tj] + 1);\n\t\t\t}\n\t\t}\n\t\trep(j, 19) {\n\t\t\tans = max(ans, dp[1 << j]);\n\t\t}\n\t}\n\n\tcout << ans << endl;\n}\n\nsigned main() {\n\tios::sync_with_stdio(false);\n\tcin.tie(0);\n\t//cout << fixed << setprecision(12);\n\t//init_f();\n\t//int t; cin >> t; rep(i, t)solve();\n\twhile(cin>>n,n)solve();\n\tstop\n\t\treturn 0;\n}", "accuracy": 1, "time_ms": 6200, "memory_kb": 4224, "score_of_the_acc": -1.0556, "final_rank": 17 } ]
aoj_1348_cpp
Problem D: Space Golf You surely have never heard of this new planet surface exploration scheme, as it is being carried out in a project with utmost secrecy. The scheme is expected to cut costs of conventional rovertype mobile explorers considerably, using projected-type equipment nicknamed "observation bullets". Bullets do not have any active mobile abilities of their own, which is the main reason of their cost-efficiency. Each of the bullets, after being shot out on a launcher given its initial velocity, makes a parabolic trajectory until it touches down. It bounces on the surface and makes another parabolic trajectory. This will be repeated virtually infinitely. We want each of the bullets to bounce precisely at the respective spot of interest on the planet surface, adjusting its initial velocity. A variety of sensors in the bullet can gather valuable data at this instant of bounce, and send them to the observation base. Although this may sound like a conventional target shooting practice, there are several issues that make the problem more difficult. There may be some obstacles between the launcher and the target spot. The obstacles stand upright and are very thin that we can ignore their widths. Once the bullet touches any of the obstacles, we cannot be sure of its trajectory thereafter. So we have to plan launches to avoid these obstacles. Launching the bullet almost vertically in a speed high enough, we can easily make it hit the target without touching any of the obstacles, but giving a high initial speed is energyconsuming. Energy is extremely precious in space exploration, and the initial speed of the bullet should be minimized. Making the bullet bounce a number of times may make the bullet reach the target with lower initial speed. The bullet should bounce, however, no more than a given number of times. Although the body of the bullet is made strong enough, some of the sensors inside may not stand repetitive shocks. The allowed numbers of bounces vary on the type of the observation bullets. You are summoned engineering assistance to this project to author a smart program that tells the minimum required initial speed of the bullet to accomplish the mission. Figure D.1 gives a sketch of a situation, roughly corresponding to the situation of the Sample Input 4 given below. Figure D.1. A sample situation You can assume the following. The atmosphere of the planet is so thin that atmospheric resistance can be ignored. The planet is large enough so that its surface can be approximated to be a completely flat plane. The gravity acceleration can be approximated to be constant up to the highest points a bullet can reach. These mean that the bullets fly along a perfect parabolic trajectory. You can also assume the following. The surface of the planet and the bullets are made so hard that bounces can be approximated as elastic collisions. In other words, loss of kinetic energy on bounces can be ignored. As we can also ignore the atmospheric resist ...(truncated)
[ { "submission_id": "aoj_1348_4963406", "code_snippet": "#include<bits/stdc++.h> \nusing namespace std;\ntypedef long long ll;\ntemplate<typename T1,typename T2> bool chmin(T1 &a,T2 b){if(a<=b)return 0; a=b; return 1;}\ntemplate<typename T1,typename T2> bool chmax(T1 &a,T2 b){if(a>b)return 0; a=b; return 1;}\n#define bitUP(x,a) ((x>>a)&1)\nint dx[4]={0,1,0,-1}, dy[4]={1,0,-1,0};\nlong double eps = 1e-6;\nlong double pi = acos(-1);\n\n//要素数n 初期値x\ntemplate<typename T>\ninline vector<T> vmake(size_t n,T x){\n return vector<T>(n,x);\n}\n\n//a,b,c,x data[a][b][c] 初期値x\ntemplate<typename... Args>\nauto vmake(size_t n,Args... args){\n auto v=vmake(args...);\n return vector<decltype(v)>(n,move(v));\n}\n\n\n\nsigned main(){\n ios::sync_with_stdio(false);\n cin.tie(0);\n cout << fixed << setprecision(20);\n using DD=long double;\n int d,n,b;\n cin>>d>>n>>b;\n b++;\n int p[11],h[11];\n for(int i=0;i<n;i++){\n cin>>p[i]>>h[i];\n }\n DD ret=1e120;\n for(int num_bound=1;num_bound<=b;num_bound++){\n DD per_d=(double)d/num_bound;\n DD ok=1000000,ng=0; //ok=??\n int N=10000;\n while(N--){\n DD mid=(ok+ng)/2;\n DD time=2*mid/1.0L;\n DD vx=per_d/time;\n bool broken=false;\n for(int i=0;i<n;i++){\n DD d=fmod(p[i],per_d);\n DD y=-1*(1.0L/(2*vx*vx))*d*d+mid/vx*d;\n if(y<h[i]) broken=true;\n }\n if(not broken) ok=mid;\n else ng=mid;\n }\n /*double time=2*ok/1.0L;\n double vx=per_d/time;\n cerr<<vx<<\" \"<<ok<<endl;\n if(vx>ok) ok=sqrt(per_d/2),vx=ok;\n cerr<<num_bound<<\" \"<<sqrt(vx*vx+ok*ok)<<endl;*/\n ok=max(ok,sqrt(per_d/2));\n DD vx=per_d/(ok*2);\n chmin(ret,sqrt(vx*vx+ok*ok));\n }\n cout<<ret<<endl;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3632, "score_of_the_acc": -1, "final_rank": 7 }, { "submission_id": "aoj_1348_2640401", "code_snippet": "#include<bits/stdc++.h>\n#define double long double\n#define N 10\nusing namespace std;\ntypedef pair<bool,double> P;\n\nconst double EPS = 1e-12;\n\ndouble d, n, b, p[N], h[N];\n\nP check(double s, double A, int cnt){\n \n double l = 0, r = 1000000000;\n \n while(l+EPS<r){\n \n double m = (l + r) / 2.0;\n double vx = m * cos(s);\n double vy = m * sin(s);\n double x = vx * 2 * vy;\n \n if(x<A) l=m;\n else r=m;\n\n }\n \n double vx = l * cos(s);\n double vy = l * sin(s);\n \n bool res = true;\n \n double x=0;\n \n for(int i=0;i<=cnt;i++){\n \n for(int j=0;j<n;j++)\n \n if(x-EPS<p[j]&&p[j]<x+A+EPS){\n\t\n\tif(abs(p[j]-x)<EPS||abs(p[j]-x)<EPS) res=false;\n\t\n\tdouble X=abs(p[j]-x);\n\t\n\tif(-X*X/(2*vx*vx)+X*vy/vx<=h[j]+EPS) res=false;\n }\n \n x+=A;\n }\n \n return P(res, l);\n}\n\ndouble sim(int cnt){\n \n double A=1.0*d/(cnt+1);\n \n double l = M_PI/4.0, r = M_PI/2.0;\n \n while(l+EPS<r){\n \n double m = (l + r) / 2.0;\n \n if(!check(m,A,cnt).first) l = m;\n else r=m;\n }\n \n return check(l,A,cnt).second;\n}\n\nint main(){\n \n cin>>d>>n>>b;\n\n for(int i=0;i<n;i++) cin>>p[i]>>h[i];\n \n double ans=(1e9);\n \n for(int i=0;i<=b;i++) ans=min(ans, sim(i));\n \n printf(\"%.8Lf\\n\", ans);\n \n return 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3604, "score_of_the_acc": -0.9882, "final_rank": 6 }, { "submission_id": "aoj_1348_2608914", "code_snippet": "#include <bits/stdc++.h>\n#define int long long\n#define double long double\n#define N 20\nusing namespace std;\nconst int INF = 1LL<<55;\nconst int mod = (1e9)+7;\nconst double EPS = 1e-8;\nconst double PI = 6.0 * asin(0.5);\ntemplate<class T> T Max(T &a,T b){return a=max(a,b);}\ntemplate<class T> T Min(T &a,T b){return a=min(a,b);}\nconst double g = 1.0;\nint p[N],h[N];\n\ndouble calcX(double radian,double v){\n double vix = v * cos(radian);\n double viy = v * sin(radian);\n double t = 2 * viy / g;\n return vix * t;\n}\n\ndouble calcV(double x,int n,int b,double radian){\n double L = 0, R = 1e10, cnt = 100;\n while(cnt--){\n double M = (L+R)/2;\n if(calcX(radian,M) > x) R = M;\n else L = M;\n }\n \n double v = L,pos = 0;\n for(int i=0;i<b;i++, pos += x){\n for(int j=0;j<n;j++){\n if(!(pos <= p[j] && p[j] <= pos+x)) continue;\n double vix = v * cos(radian);\n double viy = v * sin(radian);\n double t = (p[j] - pos)/vix;\n double y = -(g*t/2 - viy) * t;\n if(y-h[j]+EPS < 0) return INF;\n }\n }\n return v; \n}\n\ndouble solve(double x,int n,int b){\n double L = PI/4,R = PI/2,cnt = 100;\n while(cnt--){\n double M = (L+R)/2;\n if(calcV(x,n,b,M) < INF) R = M; \n else L = M;\n }\n return calcV(x,n,b,R);\n}\n\nsigned main(){\n int D,n,B;\n cin>>D>>n>>B;\n for(int i=0;i<n;i++) cin>>p[i]>>h[i];\n double ans = INF;\n for(int i=1;i<=B+1;i++) Min(ans,solve(1.0*D/i,n,i));\n printf(\"%.5Lf\\n\",ans);\n return 0;\n}", "accuracy": 1, "time_ms": 20, "memory_kb": 3512, "score_of_the_acc": -0.9637, "final_rank": 4 }, { "submission_id": "aoj_1348_2608907", "code_snippet": "#include <bits/stdc++.h>\n#define int long long\n#define double long double\n#define N 20\nusing namespace std;\nconst int INF = 1LL<<55;\nconst int mod = (1e9)+7;\nconst double EPS = 1e-8;\nconst double PI = 6.0 * asin(0.5);\ntemplate<class T> T Max(T &a,T b){return a=max(a,b);}\ntemplate<class T> T Min(T &a,T b){return a=min(a,b);}\nconst double g = 1.0;\nint p[N],h[N];\n\n\ndouble calcX(double radian,double v){\n double vix = v * cos(radian);\n double viy = v * sin(radian);\n double t = 2 * viy / g;\n return vix * t;\n}\n\ndouble calcV(double x,int n,int b,double radian){\n double L = 0, R = 1e10, cnt = 100;\n while(cnt--){\n double M = (L+R)/2;\n if(calcX(radian,M) > x) R = M;\n else L = M;\n }\n \n double v = L,pos = 0;\n for(int i=0;i<b;i++, pos += x){\n for(int j=0;j<n;j++){\n //if(pos == p[j] || p[j] == pos+x) return INF;\n if(!(pos <= p[j] && p[j] <= pos+x)) continue;\n double vix = v * cos(radian);\n double viy = v * sin(radian);\n double t = (p[j] - pos)/vix;\n double y = -(g*t/2 - viy) * t;\n if(y-h[j]+EPS < 0) return INF;\n }\n }\n return v; \n}\n\ndouble solve(double x,int n,int b){\n\n double L = PI/4,R = PI/2,cnt = 100;\n while(cnt--){\n double M = (L+R)/2;\n if(calcV(x,n,b,M) < INF) R = M; \n else L = M;\n }\n return calcV(x,n,b,R);\n}\n\n\nsigned main(){\n int D,n,B;\n cin>>D>>n>>B;\n for(int i=0;i<n;i++) cin>>p[i]>>h[i];\n double ans = INF;\n for(int i=1;i<=B+1;i++) Min(ans,solve(1.0*D/i,n,i));\n printf(\"%.5Lf\\n\",ans);\n return 0;\n}", "accuracy": 1, "time_ms": 30, "memory_kb": 3512, "score_of_the_acc": -0.9782, "final_rank": 5 }, { "submission_id": "aoj_1348_1841924", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\n#define rep(i,x,y) for(int i=(x);i<(y);++i)\n#define debug(x) #x << \"=\" << (x)\n\n#ifdef DEBUG\n#define _GLIBCXX_DEBUG\n#define show(x) std::cerr << debug(x) << \" (L:\" << __LINE__ << \")\" << std::endl\n#else\n#define show(x)\n#endif\n\ntypedef long long int ll;\ntypedef pair<int,int> pii;\ntemplate<typename T> using vec=std::vector<T>;\n\nconst int inf=1<<30;\nconst long long int infll=1LL<<62;\nconst double eps=1e-5;\nconst int dx[]={1,0,-1,0},dy[]={0,1,0,-1};\n\ntemplate <typename T> ostream &operator<<(ostream &os, const vector<T> &vec){\n os << \"[\";\n for (const auto &v : vec) {\n \tos << v << \",\";\n }\n os << \"]\";\n return os;\n}\n\nvoid solve(){\n int d,n,b;\n cin >> d >> n >> b;\n vector<long double> p(n),h(n);\n long double h_max=0;\n rep(i,0,n){\n cin >> p[i] >> h[i];\n h_max=max(h_max,h[i]);\n }\n\n int loop_num=1e7*6;\n long double ans=infll;\n long double lb=sqrtl(2*h_max)-eps,ub=d*log10l(d);\n rep(i,0,loop_num+1){\n long double vy=lb+(long double)i/loop_num*(ub-lb);\n if(ans<=vy*vy) break;\n for(int j=b+1; j>=1; --j){\n long double vx=(long double)d/2/vy/j;\n if(ans<=vx*vx) break;\n long double tmp=vx*vx+vy*vy;\n if(ans<=tmp) continue;\n bool ok=true;\n rep(k,0,n){\n int l=p[k]/(2*vx*vy);\n long double t=(long double)p[k]/vx-2*vy*l;\n if((long double)-t*t/2+vy*t>=h[k]) continue;\n ok=false;\n break;\n }\n if(ok) ans=min(ans,tmp);\n }\n }\n cout << sqrtl(ans) << endl;\n}\n\nint main(){\n std::cin.tie(0);\n std::ios::sync_with_stdio(false);\n cout.setf(ios::fixed);\n cout.precision(10);\n solve();\n return 0;\n}", "accuracy": 0.16666666666666666, "time_ms": 660, "memory_kb": 3424, "score_of_the_acc": -1.854, "final_rank": 9 }, { "submission_id": "aoj_1348_1841920", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\n#define rep(i,x,y) for(int i=(x);i<(y);++i)\n#define debug(x) #x << \"=\" << (x)\n\n#ifdef DEBUG\n#define _GLIBCXX_DEBUG\n#define show(x) std::cerr << debug(x) << \" (L:\" << __LINE__ << \")\" << std::endl\n#else\n#define show(x)\n#endif\n\ntypedef long long int ll;\ntypedef pair<int,int> pii;\ntemplate<typename T> using vec=std::vector<T>;\n\nconst int inf=1<<30;\nconst long long int infll=1LL<<62;\nconst double eps=1e-9;\nconst int dx[]={1,0,-1,0},dy[]={0,1,0,-1};\n\ntemplate <typename T> ostream &operator<<(ostream &os, const vector<T> &vec){\n os << \"[\";\n for (const auto &v : vec) {\n \tos << v << \",\";\n }\n os << \"]\";\n return os;\n}\n\nvoid solve(){\n int d,n,b;\n cin >> d >> n >> b;\n vector<long double> p(n),h(n);\n long double h_max=0;\n rep(i,0,n){\n cin >> p[i] >> h[i];\n h_max=max(h_max,h[i]);\n }\n\n int loop_num=1e7*4;\n long double ans=infll;\n long double lb=sqrtl(2*h_max),ub=d*log10l(d);\n rep(i,0,loop_num+1){\n long double vy=lb+(long double)i/loop_num*(ub-lb);\n if(ans<=vy*vy) break;\n for(int j=b+1; j>=1; --j){\n long double vx=(long double)d/2/vy/j;\n if(ans<=vx*vx) break;\n long double tmp=vx*vx+vy*vy;\n if(ans<=tmp) continue;\n bool ok=true;\n rep(k,0,n){\n int l=p[k]/(2*vx*vy);\n long double t=(long double)p[k]/vx-2*vy*l;\n if((long double)-t*t/2+vy*t>=h[k]) continue;\n ok=false;\n break;\n }\n if(ok) ans=min(ans,tmp);\n }\n }\n cout << sqrtl(ans) << endl;\n}\n\nint main(){\n std::cin.tie(0);\n std::ios::sync_with_stdio(false);\n cout.setf(ios::fixed);\n cout.precision(10);\n solve();\n return 0;\n}", "accuracy": 0.16666666666666666, "time_ms": 440, "memory_kb": 3424, "score_of_the_acc": -1.5352, "final_rank": 8 }, { "submission_id": "aoj_1348_1841917", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\n#define rep(i,x,y) for(int i=(x);i<(y);++i)\n#define debug(x) #x << \"=\" << (x)\n\n#ifdef DEBUG\n#define _GLIBCXX_DEBUG\n#define show(x) std::cerr << debug(x) << \" (L:\" << __LINE__ << \")\" << std::endl\n#else\n#define show(x)\n#endif\n\ntypedef long long int ll;\ntypedef pair<int,int> pii;\ntemplate<typename T> using vec=std::vector<T>;\n\nconst int inf=1<<30;\nconst long long int infll=1LL<<62;\nconst double eps=1e-9;\nconst int dx[]={1,0,-1,0},dy[]={0,1,0,-1};\n\ntemplate <typename T> ostream &operator<<(ostream &os, const vector<T> &vec){\n os << \"[\";\n for (const auto &v : vec) {\n \tos << v << \",\";\n }\n os << \"]\";\n return os;\n}\n\nvoid solve(){\n int d,n,b;\n cin >> d >> n >> b;\n vector<long double> p(n),h(n);\n long double h_max=0;\n rep(i,0,n){\n cin >> p[i] >> h[i];\n h_max=max(h_max,h[i]);\n }\n\n int loop_num=1e7*4;\n long double ans=infll;\n long double lb=sqrtl(2*h_max),ub=d*4;\n rep(i,0,loop_num+1){\n long double vy=lb+(long double)i/loop_num*(ub-lb);\n if(ans<=vy*vy) break;\n for(int j=b+1; j>=1; --j){\n long double vx=(long double)d/2/vy/j;\n if(ans<=vx*vx) break;\n long double tmp=vx*vx+vy*vy;\n if(ans<=tmp) continue;\n bool ok=true;\n rep(k,0,n){\n int l=p[k]/(2*vx*vy);\n long double t=(long double)p[k]/vx-2*vy*l;\n if((long double)-t*t/2+vy*t>=h[k]) continue;\n ok=false;\n break;\n }\n if(ok) ans=min(ans,tmp);\n }\n }\n cout << sqrtl(ans) << endl;\n}\n\nint main(){\n std::cin.tie(0);\n std::ios::sync_with_stdio(false);\n cout.setf(ios::fixed);\n cout.precision(10);\n solve();\n return 0;\n}", "accuracy": 0.027777777777777776, "time_ms": 240, "memory_kb": 3268, "score_of_the_acc": -1.1794, "final_rank": 11 }, { "submission_id": "aoj_1348_1841911", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\n#define rep(i,x,y) for(int i=(x);i<(y);++i)\n#define debug(x) #x << \"=\" << (x)\n\n#ifdef DEBUG\n#define _GLIBCXX_DEBUG\n#define show(x) std::cerr << debug(x) << \" (L:\" << __LINE__ << \")\" << std::endl\n#else\n#define show(x)\n#endif\n\ntypedef long long int ll;\ntypedef pair<int,int> pii;\ntemplate<typename T> using vec=std::vector<T>;\n\nconst int inf=1<<30;\nconst long long int infll=1LL<<62;\nconst double eps=1e-9;\nconst int dx[]={1,0,-1,0},dy[]={0,1,0,-1};\n\ntemplate <typename T> ostream &operator<<(ostream &os, const vector<T> &vec){\n os << \"[\";\n for (const auto &v : vec) {\n \tos << v << \",\";\n }\n os << \"]\";\n return os;\n}\n\nvoid solve(){\n int d,n,b;\n cin >> d >> n >> b;\n vector<long double> p(n),h(n);\n long double h_max=0;\n rep(i,0,n){\n cin >> p[i] >> h[i];\n h_max=max(h_max,h[i]);\n }\n\n int loop_num=1e7;\n long double ans=infll;\n long double lb=sqrtl(2*h_max),ub=d;\n rep(i,0,loop_num+1){\n long double vy=lb+(long double)i/loop_num*(ub-lb);\n if(ans<=vy) break;\n for(int j=b+1; j>=1; --j){\n long double vx=(long double)d/2/vy/j;\n if(ans<=vx) break;\n long double tmp=sqrtl(vx*vx+vy*vy);\n if(ans<=tmp) continue;\n bool ok=true;\n rep(k,0,n){\n int l=p[k]/(2*vx*vy);\n long double t=(long double)p[k]/vx-2*vy*l;\n if((long double)-t*t/2+vy*t>=h[k]) continue;\n ok=false;\n break;\n }\n if(ok) ans=min(ans,tmp);\n }\n }\n cout << ans << endl;\n}\n\nint main(){\n std::cin.tie(0);\n std::ios::sync_with_stdio(false);\n cout.setf(ios::fixed);\n cout.precision(10);\n solve();\n return 0;\n}", "accuracy": 0.05555555555555555, "time_ms": 560, "memory_kb": 3268, "score_of_the_acc": -1.6431, "final_rank": 10 }, { "submission_id": "aoj_1348_1841910", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\n#define rep(i,x,y) for(int i=(x);i<(y);++i)\n#define debug(x) #x << \"=\" << (x)\n\n#ifdef DEBUG\n#define _GLIBCXX_DEBUG\n#define show(x) std::cerr << debug(x) << \" (L:\" << __LINE__ << \")\" << std::endl\n#else\n#define show(x)\n#endif\n\ntypedef long long int ll;\ntypedef pair<int,int> pii;\ntemplate<typename T> using vec=std::vector<T>;\n\nconst int inf=1<<30;\nconst long long int infll=1LL<<62;\nconst double eps=1e-9;\nconst int dx[]={1,0,-1,0},dy[]={0,1,0,-1};\n\ntemplate <typename T> ostream &operator<<(ostream &os, const vector<T> &vec){\n os << \"[\";\n for (const auto &v : vec) {\n \tos << v << \",\";\n }\n os << \"]\";\n return os;\n}\n\nvoid solve(){\n int d,n,b;\n cin >> d >> n >> b;\n vector<long double> p(n),h(n);\n long double h_max=0;\n rep(i,0,n){\n cin >> p[i] >> h[i];\n h_max=max(h_max,h[i]);\n }\n\n int loop_num=1e7*2;\n long double ans=infll;\n rep(i,1,loop_num+1){\n long double vy=(long double)i/loop_num*2*d;\n if(ans<=vy) break;\n if(vy/2*vy<h_max) continue;\n for(int j=b+1; j>=1; --j){\n long double vx=(long double)d/2/vy/j;\n if(ans<=vx) break;\n long double tmp=sqrtl(vx*vx+vy*vy);\n if(ans<=tmp) continue;\n bool ok=true;\n rep(k,0,n){\n int l=p[k]/(2*vx*vy);\n long double t=(long double)p[k]/vx-2*vy*l;\n if((long double)-t*t/2+vy*t>=h[k]) continue;\n ok=false;\n break;\n }\n if(ok) ans=min(ans,tmp);\n }\n }\n cout << ans << endl;\n}\n\nint main(){\n std::cin.tie(0);\n std::ios::sync_with_stdio(false);\n cout.setf(ios::fixed);\n cout.precision(10);\n solve();\n return 0;\n}", "accuracy": 0.027777777777777776, "time_ms": 300, "memory_kb": 3304, "score_of_the_acc": -1.2815, "final_rank": 12 }, { "submission_id": "aoj_1348_1841905", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\n#define rep(i,x,y) for(int i=(x);i<(y);++i)\n#define debug(x) #x << \"=\" << (x)\n\n#ifdef DEBUG\n#define _GLIBCXX_DEBUG\n#define show(x) std::cerr << debug(x) << \" (L:\" << __LINE__ << \")\" << std::endl\n#else\n#define show(x)\n#endif\n\ntypedef long long int ll;\ntypedef pair<int,int> pii;\ntemplate<typename T> using vec=std::vector<T>;\n\nconst int inf=1<<30;\nconst long long int infll=1LL<<62;\nconst double eps=1e-9;\nconst int dx[]={1,0,-1,0},dy[]={0,1,0,-1};\n\ntemplate <typename T> ostream &operator<<(ostream &os, const vector<T> &vec){\n os << \"[\";\n for (const auto &v : vec) {\n \tos << v << \",\";\n }\n os << \"]\";\n return os;\n}\n\nvoid solve(){\n int d,n,b;\n cin >> d >> n >> b;\n vector<long double> p(n),h(n);\n rep(i,0,n) cin >> p[i] >> h[i];\n\n int loop_num=1e7*3.5;\n long double ans=infll;\n rep(i,1,loop_num+1){\n long double vy=(long double)i/loop_num*10*d;\n if(ans<=vy) break;\n for(int j=b+1; j>=1; --j){\n long double vx=(long double)d/2/vy/j;\n if(ans<=vx) break;\n long double tmp=sqrtl(vx*vx+vy*vy);\n if(ans<=tmp) continue;\n bool ok=true;\n rep(k,0,n){\n int l=p[k]/(2*vx*vy);\n long double t=(long double)p[k]/vx-2*vy*l;\n if((long double)-t*t/2+vy*t>=h[k]) continue;\n ok=false;\n break;\n }\n if(ok) ans=min(ans,tmp);\n }\n }\n cout << ans << endl;\n}\n\nint main(){\n std::cin.tie(0);\n std::ios::sync_with_stdio(false);\n cout.setf(ios::fixed);\n cout.precision(10);\n solve();\n return 0;\n}", "accuracy": 0.027777777777777776, "time_ms": 650, "memory_kb": 3284, "score_of_the_acc": -1.7803, "final_rank": 13 }, { "submission_id": "aoj_1348_1841903", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\n#define rep(i,x,y) for(int i=(x);i<(y);++i)\n#define debug(x) #x << \"=\" << (x)\n\n#ifdef DEBUG\n#define _GLIBCXX_DEBUG\n#define show(x) std::cerr << debug(x) << \" (L:\" << __LINE__ << \")\" << std::endl\n#else\n#define show(x)\n#endif\n\ntypedef long long int ll;\ntypedef pair<int,int> pii;\ntemplate<typename T> using vec=std::vector<T>;\n\nconst int inf=1<<30;\nconst long long int infll=1LL<<62;\nconst double eps=1e-9;\nconst int dx[]={1,0,-1,0},dy[]={0,1,0,-1};\n\ntemplate <typename T> ostream &operator<<(ostream &os, const vector<T> &vec){\n os << \"[\";\n for (const auto &v : vec) {\n \tos << v << \",\";\n }\n os << \"]\";\n return os;\n}\n\nvoid solve(){\n int d,n,b;\n cin >> d >> n >> b;\n vector<long double> p(n),h(n);\n rep(i,0,n) cin >> p[i] >> h[i];\n\n int loop_num=1e7*3;\n long double ans=infll;\n rep(i,1,loop_num+1){\n long double vy=(long double)i/loop_num*8*d;\n if(ans<=vy) break;\n for(int j=b+1; j>=1; --j){\n long double vx=(long double)d/2/vy/j;\n if(ans<=vx) break;\n long double tmp=sqrtl(vx*vx+vy*vy);\n if(ans<=tmp) continue;\n bool ok=true;\n rep(k,0,n){\n int l=p[k]/(2*vx*vy);\n long double t=(long double)p[k]/vx-2*vy*l;\n if((long double)-t*t/2+vy*t>=h[k]) continue;\n ok=false;\n break;\n }\n if(ok) ans=min(ans,tmp);\n }\n }\n cout << ans << endl;\n}\n\nint main(){\n std::cin.tie(0);\n std::ios::sync_with_stdio(false);\n cout.setf(ios::fixed);\n cout.precision(10);\n solve();\n return 0;\n}", "accuracy": 0.027777777777777776, "time_ms": 700, "memory_kb": 3256, "score_of_the_acc": -1.8409, "final_rank": 14 }, { "submission_id": "aoj_1348_1301745", "code_snippet": "// Template {{{\n#include <bits/stdc++.h>\n#define REP(i,n) for(int i=0; i<(int)(n); ++i)\nusing namespace std;\ntypedef long long LL;\n\n#ifdef LOCAL\n#include \"contest.h\"\n#else\n#define dump(x) \n#endif\n\nclass range {\n struct Iterator {\n int val, inc;\n int operator*() {return val;}\n bool operator!=(Iterator& rhs) {return val < rhs.val;}\n void operator++() {val += inc;}\n };\n Iterator i, n;\n public:\n range(int e) : i({0, 1}), n({e, 1}) {}\n range(int b, int e) : i({b, 1}), n({e, 1}) {}\n range(int b, int e, int inc) : i({b, inc}), n({e, inc}) {}\n Iterator& begin() {return i;}\n Iterator& end() {return n;}\n};\n\nconst int dx[4] = {1, 0, -1, 0};\nconst int dy[4] = {0, 1, 0, -1};\ninline bool valid(int x, int w) { return 0 <= x && x < w; }\n\nvoid iostream_init() {\n ios::sync_with_stdio(false);\n cin.tie(0);\n cout.setf(ios::fixed);\n cout.precision(12);\n}\n//}}}\n\nint main(){\n iostream_init();\n int N, B;\n double D;\n while(cin >> D >> N >> B) {\n vector<double> P(N);\n vector<double> H(N);\n REP(i, N) cin >> P[i] >> H[i];\n\n double ans = 1e16;\n\n for(int b = 0; b <= B; b++) {\n double p = D / (b+1);\n double lb = M_PI / 4;\n double ub = M_PI / 2;\n double v = 0;\n REP(_, 60) {\n double th = (lb+ub) / 2;\n double vx = sqrt(p / 2.0 / tan(th));\n\n v = vx/cos(th); // update\n\n bool ok = true;\n REP(i, N) {\n double x = P[i] - int(P[i] / p) * p;\n double y = -(0.5/vx/vx)*x*x + tan(th)*x;\n if(y < H[i]) ok = false;\n }\n if(ok) {\n ub = th;\n } else {\n lb = th;\n }\n }\n\n ans = min(ans, v);\n }\n cout << ans << endl;\n }\n\n return 0;\n}\n\n/* vim:set foldmethod=marker: */", "accuracy": 1, "time_ms": 40, "memory_kb": 1404, "score_of_the_acc": -0.101, "final_rank": 3 }, { "submission_id": "aoj_1348_1144005", "code_snippet": "#include <iostream>\n#include <algorithm>\n#include <vector>\n#include <cmath>\nusing namespace std;\n\n#define DEBUG(x) cerr << #x << \" = \" << x << endl\n\ntypedef long double D;\n\nstruct Node {\n D p;\n D h;\n};\n\nconst D EPS = 1e-10;\n\nint sig(D a, D b) {\n if(a < b - EPS) return -1;\n if(a > b + EPS) return +1;\n return 0;\n}\n\nbool check(D th, D speed, D start, D end, const vector<Node> &objs) {\n D vx = speed * cos(th);\n D vy = speed * sin(th);\n bool ok = true;\n for(int i = 0; i < (int)objs.size(); ++i) {\n if(sig(start, objs[i].p) <= 0 && sig(objs[i].p, end) <= 0) {\n D x = objs[i].p - start;\n D y = -(x*x/(2*vx*vx)) + vy*x/vx;\n if(sig(y, objs[i].h) < 0) {\n ok = false;\n }\n }\n }\n return ok;\n}\n\nD speed(D th, D len) {\n return sqrt(len / (sin(2*th)));\n}\n\nvoid test(D th, D len, D pos) {\n D sp = speed(th, len);\n D vx = sp * cos(th);\n D vy = sp * sin(th);\n D y = -(pos*pos/(2*vx*vx)) + (vy/vx)*pos;\n DEBUG(y);\n}\n\nint main() {\n D distance;\n int N, B;\n cin >> distance >> N >> B;\n vector<Node> objs(N);\n for(int i = 0; i < N; ++i) {\n cin >> objs[i].p >> objs[i].h;\n }\n B++;\n D ans = 1e20;\n for(int b = 1; b <= B; ++b) {\n for(int i = 0; i < b; ++i) {\n D start = distance / b * i;\n D end = distance / b * (i + 1);\n D len = abs(end - start);\n D estm;\n // DEBUG(start);\n // DEBUG(end);\n // DEBUG(len);\n // DEBUG(speed(M_PI/4, len));\n // D vx = speed(M_PI/4, len)*cos(M_PI/4);\n // D vy = speed(M_PI/4, len)*sin(M_PI/4);\n // DEBUG(-(len*len/(2*vx*vx))+(vy/vx)*len);\n // DEBUG(\"false\");\n D lb = M_PI/4, ub = M_PI/2;\n for(int t = 0; t < 300; ++t) {\n D mid = (lb + ub) / 2;\n if(check(mid, speed(mid, len), start, end, objs)) {\n ub = mid;\n }\n else {\n lb = mid;\n }\n }\n estm = ub;\n D estm_sp = speed(estm, len);\n bool ok = true;\n for(int j = 0; j < b; ++j) {\n D start2 = distance / b * j;\n D end2 = distance / b * (j + 1);\n if(check(estm, estm_sp, start2, end2, objs) == false) {\n ok = false;\n }\n }\n if(ok) {\n ans = min(ans, estm_sp);\n }\n }\n }\n cout.setf(ios::fixed); cout.precision(10);\n cout << ans << endl;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 1308, "score_of_the_acc": -0.0169, "final_rank": 2 }, { "submission_id": "aoj_1348_1101727", "code_snippet": "#include <iostream>\n#include <vector>\n#include <algorithm>\n#include <string>\n#include <sstream>\n#include <cstring>\n#include <cstdio>\n#include <cstdlib>\n#include <cmath>\n#include <queue>\n#include <stack>\n#include <map>\n#include <set>\n#include <numeric>\n#include <cctype>\n#include <tuple>\n#include <array>\n#include <climits>\n#include <bitset>\n#include <cassert>\n#include <ctime>\n#include <list>\n#include <complex>\n#ifdef _MSC_VER\n#include <agents.h>\n#endif\n\n#define FOR(i, a, b) for(int i = (a); i < (int)(b); ++i)\n#define rep(i, n) FOR(i, 0, n)\n#define ALL(v) v.begin(), v.end()\n#define REV(v) v.rbegin(), v.rend()\n#define MEMSET(v, s) memset(v, s, sizeof(v))\n#define UNIQUE(v) (v).erase(unique(ALL(v)), (v).end())\n#define MP make_pair\n#define MT make_tuple\n\nusing namespace std;\n\ntypedef long long ll;\ntypedef unsigned long long ull;\ntypedef pair<ll, ll> P;\n\nconst double EPS = 1e-9;\n\nbool check(double x, double y, double vx, double vy){\n\tdouble yy = -1 / 2. / vx / vx*x*x + vy / vx*x;\n\treturn y < yy + EPS;\n}\n\nint main(){\n\tint d, n, b;\n\tcin >> d >> n >> b;\n\n\tvector<int> p(n), h(n);\n\trep(i, n) cin >> p[i] >> h[i];\n\n\tdouble ans = 1e100;\n\tFOR(i, 1, b + 2){\n\t\tdouble len = (double)d / i;\n\n\t\tdouble lb = 0, ub = sqrt(len / 2);\n\t\trep(j, 10000){\n\t\t\tdouble mid = (lb + ub) / 2;\n\t\t\tbool ok = true;\n\t\t\trep(k, n){\n\t\t\t\tdouble pp = p[k];\n\t\t\t\twhile (pp > len - EPS) pp -= len;\n\t\t\t\tok &= check(pp, h[k], mid, len / 2 / mid);\n\t\t\t}\n\t\t\tif (ok) lb = mid;\n\t\t\telse ub = mid;\n\t\t}\n\t\tif (lb < EPS) continue;\n\t\tdouble vx = lb, vy = len / 2 / vx;\n\t\tans = min(ans, sqrt(vx*vx+vy*vy));\n\t}\n\tcout.setf(ios::fixed);\n\tcout.precision(10);\n\tcout << ans << endl;\n\n\n\treturn 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 1268, "score_of_the_acc": 0, "final_rank": 1 } ]
aoj_1349_cpp
Problem E: Automotive Navigation The International Commission for Perfect Cars (ICPC) has constructed a city scale test course for advanced driver assistance systems. Your company, namely the Automotive Control Machines (ACM), is appointed by ICPC to make test runs on the course. The test course consists of streets, each running straight either east-west or north-south. No streets of the test course have dead ends, that is, at each end of a street, it meets another one. There are no grade separated streets either, and so if a pair of orthogonal streets run through the same geographical location, they always meet at a crossing or a junction, where a car can turn from one to the other. No U-turns are allowed on the test course and a car never moves outside of the streets. Oops! You have just received an error report telling that the GPS (Global Positioning System) unit of a car running on the test course was broken and the driver got lost. Fortunately, however, the odometer and the electronic compass of the car are still alive. You are requested to write a program to estimate the current location of the car from available information. You have the car's location just before its GPS unit was broken. Also, you can remotely measure the running distance and the direction of the car once every time unit. The measured direction of the car is one of north, east, south, and west. If you measure the direction of the car while it is making a turn, the measurement result can be the direction either before or after the turn. You can assume that the width of each street is zero. The car's direction when the GPS unit was broken is not known. You should consider every possible direction consistent with the street on which the car was running at that time. Input The input consists of a single test case. The first line contains four integers $n$, $x_0$, $y_0$, $t$, which are the number of streets ($4 \leq n \leq 50$), $x$- and $y$-coordinates of the car at time zero, when the GPS unit was broken, and the current time ($1 \leq t \leq 100$), respectively. $(x_0, y_0)$ is of course on some street. This is followed by $n$ lines, each containing four integers $x_s$, $y_s$, $x_e$, $y_e$ describing a street from $(x_s, y_s)$ to $(x_e, y_e)$ where $(x_s, y_s) \ne (x_e, y_e)$. Since each street runs either east-west or north-south, $x_s = x_e$ or $y_s = y_e$ is also satisfied. You can assume that no two parallel streets overlap or meet. In this coordinate system, the $x$- and $y$-axes point east and north, respectively. Each input coordinate is non-negative and at most 50. Each of the remaining $t$ lines contains an integer $d_i$ ($1 \leq d_i \leq 10$), specifying the measured running distance from time $i − 1$ to $i$, and a letter $c_i$, denoting the measured direction of the car at time $i$ and being either N for north, E for east, W for west, or S for south. Output Output all the possible current locations of the car that are consistent with the measurements. If they are ...(truncated)
[ { "submission_id": "aoj_1349_10848168", "code_snippet": "#include<map>\n#include<set>\n#include<cmath>\n#include<queue>\n#include<cctype>\n#include<cstdio>\n#include<vector>\n#include<cstdlib>\n#include<cstring>\n#include<algorithm>\ntypedef long long LL;\nbool go[55][55][4];\nbool f[55][55][1010][4];\nstruct HN {\n\tint x, y, s, d;\n\tHN() {}\n\tHN(int x, int y, int s, int d): x(x), y(y), s(s), d(d) {}\n};\nint N, SX, SY, T, S;\nstruct P {\n\tint x, y;\n\tP() {}\n\tP(int x, int y): x(x), y(y) {}\n\tbool operator < (const P &t) const {\n\t\tif(x != t.x) {\n\t\t\treturn x < t.x;\n\t\t}\n\t\treturn y < t.y;\n\t}\n};\nchar ch[256];\nint dx[] = {0, -1, 0, 1}, dy[] = {1, 0, -1, 0};\nint dir[1010];\nbool check(int s, int d) {\n\tif(dir[s] == -1) {\n\t\treturn true;\n\t}\n\treturn dir[s] == d;\n}\nvoid process() {\n\tmemset(f, 0, sizeof(f));\n\tstd::queue<HN> q;\n\tfor(int i = 0; i < 4; i ++) {\n\t\tif(go[SX][SY][i]) {\n\t\t\tf[SX][SY][0][i] = true;\n\t\t\tq.push(HN(SX, SY, 0, i));\n\t\t}\n\t}\n\twhile(!q.empty()) {\n\t\tHN hn = q.front();\n\t\tq.pop();\n\t\tint x = hn.x, y = hn.y, s = hn.s, d = hn.d;\n\t\tif(s >= S) {\n\t\t\tcontinue;\n\t\t}\n\t\tint nx = x + dx[d], ny = y + dy[d], ns = s + 1;\n\t\tfor(int i = -1; i <= 1; i ++) {\n\t\t\tint nd = (d + i + 4) % 4;\n\t\t\tif(go[nx][ny][nd] && (check(ns, d) || check(ns, nd)) && !f[nx][ny][ns][nd]) {\n\t\t\t\tf[nx][ny][ns][nd] = true;\n\t\t\t\tq.push(HN(nx, ny, ns, nd));\n\t\t\t}\n\t\t}\n\t}\n\n\tstd::vector<P> ans;\n\tfor(int i = 0; i <= 50; i ++) {\n\t\tfor(int j = 0; j <= 50; j ++) {\n\t\t\tbool ok = false;\n\t\t\tfor(int k = 0; k < 4; k ++) {\n\t\t\t\tif(f[i][j][S][k]) {\n\t\t\t\t\tok = true;\n\t\t\t\t}\n\t\t\t}\n\t\t\tif(ok) {\n\t\t\t\tans.push_back(P(i, j));\n\t\t\t}\n\t\t}\n\t}\n\tstd::sort(ans.begin(), ans.end());\n\tint n = ans.size();\n\tfor(int i = 0; i < n; i ++) {\n\t\tprintf(\"%d %d\\n\", ans[i].x, ans[i].y);\n\t}\n}\nint main() {\n\t//freopen(\"test.in\", \"rb\", stdin);\n\tch['N'] = 0, ch['W'] = 1, ch['S'] = 2, ch['E'] = 3;\n\twhile(scanf(\"%d%d%d%d\", &N, &SX, &SY, &T) == 4) {\n\t\tmemset(go, 0, sizeof(go));\n\t\tint x1, y1, x2, y2;\n\t\tfor(int i = 0; i < N; i ++) {\n\t\t\tscanf(\"%d%d%d%d\", &x1, &y1, &x2, &y2);\n\t\t\tif(x1 == x2) {\n\t\t\t\tif(y1 > y2) {\n\t\t\t\t\tstd::swap(y1, y2);\n\t\t\t\t}\n\t\t\t\tfor(int i = y1; i <= y2; i ++) {\n\t\t\t\t\tif(i != y1) {\n\t\t\t\t\t\tgo[x1][i][2] = true;\n\t\t\t\t\t}\n\t\t\t\t\tif(i != y2) {\n\t\t\t\t\t\tgo[x1][i][0] = true;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t} else {\n\t\t\t\tif(x1 > x2) {\n\t\t\t\t\tstd::swap(x1, x2);\n\t\t\t\t}\n\t\t\t\tfor(int i = x1; i <= x2; i ++) {\n\t\t\t\t\tif(i != x1) {\n\t\t\t\t\t\tgo[i][y1][1] = true;\n\t\t\t\t\t}\n\t\t\t\t\tif(i != x2) {\n\t\t\t\t\t\tgo[i][y1][3] = true;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\tmemset(dir, -1, sizeof(dir));\n\t\tS = 0;\n\t\tint d;\n\t\tchar s[5];\n\t\tfor(int i = 0; i < T; i ++) {\n\t\t\tscanf(\"%d%s\", &d, s);\n\t\t\tS += d;\n\t\t\tdir[S] = ch[(int)s[0]];\n\t\t}\n\t\tprocess();\n\t}\n\treturn 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 14968, "score_of_the_acc": -0.3353, "final_rank": 16 }, { "submission_id": "aoj_1349_10826894", "code_snippet": "#include<bits/stdc++.h>\ntypedef long long int ll;\ntypedef unsigned long long int ull;\n#define BIG_NUM 2000000000\n#define MOD 1000000007\n#define EPS 0.000000001\nusing namespace std;\n\n#define NUM 51\n\nenum DIR{\n\tNorth,\n\tEast,\n\tSouth,\n\tWest,\n};\n\nint diff_x[4] = {0,1,0,-1},diff_y[4] = {1,0,-1,0};\nbool can_move[NUM][NUM][4],dp[2][NUM][NUM][4];\nint table[128];\nDIR dir_array[4] = {North,East,South,West};\n\nint N;\n\nbool rangeCheck(int x,int y){\n\n\tif(x >= 0 && x < NUM && y >= 0 && y < NUM)return true;\n\telse{\n\t\treturn false;\n\t}\n}\n\nint main(){\n\n\ttable['N'] = 0;\n\ttable['E'] = 1;\n\ttable['S'] = 2;\n\ttable['W'] = 3;\n\n\tint start_x,start_y,T;\n\tscanf(\"%d %d %d %d\",&N,&start_x,&start_y,&T);\n\n\tfor(int i = 0; i < NUM; i++){\n\t\tfor(int k = 0; k < NUM; k++){\n\t\t\tfor(int p = 0; p < 4; p++)can_move[i][k][p] = false;\n\t\t}\n\t}\n\n\t//移動可能情報の作成\n\tint x1,y1,x2,y2;\n\tfor(int loop = 0; loop < N; loop++){\n\n\t\tscanf(\"%d %d %d %d\",&x1,&y1,&x2,&y2);\n\n\t\tif(x1 == x2){ //縦の移動\n\n\t\t\tfor(int y = min(y1,y2); y < max(y1,y2); y++){\n\t\t\t\tcan_move[x1][y][North] = true;\n\t\t\t\tcan_move[x1][y+1][South] = true;\n\t\t\t}\n\n\t\t}else{ //横の移動\n\n\t\t\tfor(int x = min(x1,x2); x < max(x1,x2); x++){\n\t\t\t\tcan_move[x][y1][East] = true;\n\t\t\t\tcan_move[x+1][y1][West] = true;\n\t\t\t}\n\t\t}\n\t}\n\n\tint CURRENT = 0,NEXT = 1;\n\n\tfor(int i = 0; i < NUM; i++){\n\t\tfor(int k = 0; k < NUM; k++){\n\t\t\tfor(int p = 0; p < 4; p++){\n\t\t\t\tdp[CURRENT][i][k][p] = false; //dp[CURRENT/NEXT][x][y][dir] = true/false\n\t\t\t}\n\t\t}\n\t}\n\n\tfor(int i = 0; i < 4; i++)dp[CURRENT][start_x][start_y][i] = true;\n\n\tint dist,next_x,next_y;\n\tchar buf[2];\n\n\tDIR dir;\n\n\tfor(int loop = 0; loop < T; loop++){\n\n\t\tscanf(\"%d %s\",&dist,buf);\n\t\tdir = dir_array[table[buf[0]]];\n\n\t\tfor(int count = 0; count < dist; count++){ //合計でdist移動する\n\n\t\t\t//dp[NEXT]の初期化\n\t\t\tfor(int i = 0; i < NUM; i++){\n\t\t\t\tfor(int k = 0; k < NUM; k++){\n\t\t\t\t\tfor(int p = 0; p < 4; p++){\n\t\t\t\t\t\tdp[NEXT][i][k][p] = false;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t\t//dp[CURRENT]を走査\n\t\t\tfor(int i = 0; i < NUM; i++){ //xのループ\n\t\t\t\tfor(int k = 0; k < NUM; k++){ //yのループ\n\t\t\t\t\tfor(int p = 0; p < 4; p++){ //今向いている方向\n\t\t\t\t\t\tif(!dp[CURRENT][i][k][p])continue;\n\n\t\t\t\t\t\tfor(int a = 0; a < 4; a++){ //進んだ後に向く方向\n\t\t\t\t\t\t\tif(abs(p-a) == 2)continue; //Uターン禁止\n\n\t\t\t\t\t\t\tnext_x = i+diff_x[p]; //今向いている向きに進む\n\t\t\t\t\t\t\tnext_y = k+diff_y[p];\n\n\t\t\t\t\t\t\tif(rangeCheck(next_x,next_y) == false || can_move[i][k][p] == false\n\t\t\t\t\t\t\t\t\t|| can_move[next_x][next_y][a] == false)continue; //範囲外または行けないならSKIP\n\n\t\t\t\t\t\t\tif(count < dist-1){\n\n\t\t\t\t\t\t\t\tdp[NEXT][next_x][next_y][a] = true;\n\n\t\t\t\t\t\t\t}else{ //count == dist-1:向きを考慮する!!\n\n\t\t\t\t\t\t\t\tif(dir_array[p] == dir || dir_array[a] == dir){\n\t\t\t\t\t\t\t\t\tdp[NEXT][next_x][next_y][a] = true;\n\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t\tswap(CURRENT,NEXT);\n\t\t}\n\t}\n\n\tfor(int i = 0; i < NUM; i++){\n\t\tfor(int k = 0; k < NUM; k++){\n\t\t\tfor(int a = 0; a < 4; a++){\n\t\t\t\tif(dp[CURRENT][i][k][a]){\n\t\t\t\t\tprintf(\"%d %d\\n\",i,k);\n\t\t\t\t\tbreak;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n\n\treturn 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3592, "score_of_the_acc": -0.0123, "final_rank": 2 }, { "submission_id": "aoj_1349_10480164", "code_snippet": "// AOJ #1349 Automotive Navigation\n// 2025.5.13\n\n#include <bits/stdc++.h>\nusing namespace std;\n\nint g[52][52];\nbool dp[1001][52][52][4];\nint meas[1001];\n\nconst int dx[4] = {0, 1, -1, 0};\nconst int dy[4] = {1, 0, 0, -1};\n\nint dir_id(char c){\n if(c=='N') return 0;\n if(c=='E') return 1;\n if(c=='W') return 2;\n if(c=='S') return 3;\n return -1;\n}\n\nint main(){\n ios::sync_with_stdio(0);\n cin.tie(0);\n\n int n, sx, sy, t;\n cin >> n >> sx >> sy >> t;\n\n for(int i=0;i<n;i++){\n int x1,y1,x2,y2;\n cin >> x1 >> y1 >> x2 >> y2;\n if(x1==x2){\n if(y1>y2) swap(y1,y2);\n for(int y=y1;y<y2;y++){\n g[x1][y] |= 1<<0;\n g[x1][y+1] |= 1<<3;\n }\n } else {\n if(x1>x2) swap(x1,x2);\n for(int x=x1;x<x2;x++){\n g[x][y1] |= 1<<1;\n g[x+1][y1] |= 1<<2;\n }\n }\n }\n\n int sumd = 0;\n for(int i=0;i<=1000;i++) meas[i] = -1;\n\n for(int i=0;i<t;i++){\n int d; char buf[5];\n cin >> d >> buf;\n sumd += d;\n meas[sumd] = dir_id(buf[0]);\n }\n\n for(int d=0;d<4;d++) dp[0][sx][sy][d] = true;\n\n for(int s=0; s<sumd; s++){\n for(int x=0; x<=50; x++){\n for(int y=0; y<=50; y++){\n for(int l=0; l<4; l++){\n if(!dp[s][x][y][l]) continue;\n int mv = meas[s];\n for(int p=0; p<4; p++){\n if(!(g[x][y] & (1<<p))) continue;\n if(l + p == 3) continue;\n if(mv != -1 && p!=mv && l!=mv) continue;\n int nx = x + dx[p], ny = y + dy[p];\n if(nx<0||nx>50||ny<0||ny>50) continue;\n dp[s+1][nx][ny][p] = true;\n }\n }\n }\n }\n }\n\n int final_mv = meas[sumd];\n set<pair<int,int>> ans;\n for(int x=0;x<=50;x++){\n for(int y=0;y<=50;y++){\n bool ok=false;\n for(int k=0;k<4;k++){\n if(!dp[sumd][x][y][k]) continue;\n if(k == final_mv){ ok=true; break; }\n if(k + final_mv != 3 && (g[x][y] & (1<<final_mv))){ ok=true; break; }\n }\n if(ok) ans.emplace(x,y);\n }\n }\n for(auto &p: ans) cout << p.first << \" \" << p.second << endl;\n return 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 11188, "score_of_the_acc": -0.228, "final_rank": 13 }, { "submission_id": "aoj_1349_10002993", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\nusing ll=long long;\n\nconst int mx=60;\nconst int dmx=1010;\nint Convert(int x,int y){return x*mx+y;}\nconst string str=\"NESW\";\nconst int dx[]={0,1,0,-1};\nconst int dy[]={1,0,-1,0};\nbool seen[mx][mx][4][dmx];\n\nint main(){\n\tint N,sx,sy,T;\n\tcin>>N>>sx>>sy>>T;\n\tsx+=2,sy+=2;\n\n\tvector<vector<int>>G(mx*mx);\n\twhile(N--){\n\t\tint a,b,c,d;\n\t\tcin>>a>>b>>c>>d;\n\t\ta+=2,b+=2,c+=2,d+=2;\n\t\tif(a==c){\n\t\t\tfor(int i=min(b,d);i<max(b,d);i++){\n\t\t\t\tG[Convert(a,i)].push_back(Convert(a,i+1));\n\t\t\t\tG[Convert(a,i+1)].push_back(Convert(a,i));\n\t\t\t}\n\t\t}else{\n\t\t\tfor(int i=min(a,c);i<max(a,c);i++){\n\t\t\t\tG[Convert(i,b)].push_back(Convert(i+1,b));\n\t\t\t\tG[Convert(i+1,b)].push_back(Convert(i,b));\n\t\t\t}\n\t\t}\n\t}\n\n\tvector<int>info(dmx,-1);\n\tint goald=0,sum=0;\n\tfor(int i=0;i<T;i++){\n\t\tint d;char c;cin>>d>>c;\n\t\tsum+=d;\n\t\tgoald=sum;\n\t\tinfo[sum]=str.find(c);\n\t}\n\n\t//x,y,px,py,dir,dist\n\tusing tup=tuple<int,int,int,int>;\n\tqueue<tup> que;\n\tque.push({sx,sy,-1,0});\n\n\tset<pair<int,int>>ans;\n\n\twhile(!que.empty()){\n\t\tauto[x,y,dir,dist]=que.front();que.pop();\n\n\t\tif(dist>goald)break;\n\n\n\t\tfor(int d=0;d<4;d++){\n\t\t\tint nx=x+dx[d],ny=y+dy[d];\n\t\t\tif(count(G[Convert(x,y)].begin(),G[Convert(x,y)].end(),Convert(nx,ny))==0)continue;\n\t\t\tif(seen[nx][ny][d][dist+1])continue;\n\t\t\tif(dir!=-1&&(dir+4-d)%4==2)continue;\n\t\t\tif(info[dist]!=-1&&info[dist]!=dir&&info[dist]!=d)continue;\n\n\t\t\tseen[nx][ny][d][dist+1]=true;\n\t\t\tif(dist==goald&&(info[dist]==dir||info[dist]==d)){\n\t\t\t\tans.insert({x-2,y-2});\n\t\t\t\tbreak;\n\t\t\t}\n\t\t\t\n\t\t\tque.push({nx,ny,d,dist+1});\n\t\t}\n\t}\n\n\tfor(auto[x,y]:ans)cout<<x<<' '<<y<<endl;\n}", "accuracy": 1, "time_ms": 20, "memory_kb": 14268, "score_of_the_acc": -0.3176, "final_rank": 15 }, { "submission_id": "aoj_1349_10002974", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\nusing ll=long long;\n\nconst int mx=60;\nconst int dmx=1010;\nint Convert(int x,int y){return x*mx+y;}\nconst string str=\"NESW\";\nconst int dx[]={0,1,0,-1};\nconst int dy[]={1,0,-1,0};\nbool seen[mx][mx][4][dmx];\n\nint main(){\n\tint N,sx,sy,T;\n\tcin>>N>>sx>>sy>>T;\n\tsx+=2,sy+=2;\n\n\tvector<vector<int>>G(mx*mx);\n\twhile(N--){\n\t\tint a,b,c,d;\n\t\tcin>>a>>b>>c>>d;\n\t\ta+=2,b+=2,c+=2,d+=2;\n\t\tif(a==c){\n\t\t\tfor(int i=min(b,d);i<max(b,d);i++){\n\t\t\t\tG[Convert(a,i)].push_back(Convert(a,i+1));\n\t\t\t\tG[Convert(a,i+1)].push_back(Convert(a,i));\n\t\t\t}\n\t\t}else{\n\t\t\tfor(int i=min(a,c);i<max(a,c);i++){\n\t\t\t\tG[Convert(i,b)].push_back(Convert(i+1,b));\n\t\t\t\tG[Convert(i+1,b)].push_back(Convert(i,b));\n\t\t\t}\n\t\t}\n\t}\n\n\tvector<int>info(dmx,-1);\n\tint goald=0,sum=0;\n\tfor(int i=0;i<T;i++){\n\t\tint d;char c;cin>>d>>c;\n\t\tsum+=d;\n\t\tgoald=sum;\n\t\tinfo[sum]=str.find(c);\n\t}\n\n\t//x,y,px,py,dir,dist\n\tusing tup=tuple<int,int,int,int>;\n\tvector<tup> que;\n\tque.push_back({sx,sy,-1,0});\n\n\tset<pair<int,int>>ans;\n\n\t//while(!que.empty()){\n\tfor(int t=0;t<(int)que.size();t++){\n\t\tauto[x,y,dir,dist]=que[t];\n\n\t\tif(dist>goald)continue;\n\n\n\t\tfor(int d=0;d<4;d++){\n\t\t\tint nx=x+dx[d],ny=y+dy[d];\n\t\t\tif(count(G[Convert(x,y)].begin(),G[Convert(x,y)].end(),Convert(nx,ny))==0)continue;\n\t\t\tif(seen[nx][ny][d][dist+1])continue;\n\t\t\tif(dir!=-1&&(dir+4-d)%4==2)continue;\n\t\t\tif(info[dist]!=-1&&info[dist]!=dir&&info[dist]!=d)continue;\n\n\t\t\tif(dist==goald&&(info[dist]==dir||info[dist]==d)){\n\t\t\t\tans.insert({x-2,y-2});\n\t\t\t\tbreak;\n\t\t\t}\n\t\t\t\n\t\t\tque.push_back({nx,ny,d,dist+1});\n\t\t}\n\t}\n\n\tfor(auto[x,y]:ans)cout<<x<<' '<<y<<endl;\n}", "accuracy": 0.03571428571428571, "time_ms": 20, "memory_kb": 36200, "score_of_the_acc": -0.9405, "final_rank": 20 }, { "submission_id": "aoj_1349_9808763", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n#define rep(i, n) for(int i = 0; i < (n); i++)\n#define srep(i, s, t) for(int i = (s); i < (t); i++)\n#define len(x) ((int)(x).size())\n#define all(x) (x).begin(), (x).end()\ntemplate < class T > bool chmin(T& a, T b) { if(a > b) { a = b; return true; } return false; }\ntemplate < class T > bool chmax(T& a, T b) { if(a < b) { a = b; return true; } return false; }\nusing i64 = long long;\nusing f64 = long double;\ni64 floor_div(const i64 n, const i64 d) { assert(d != 0); return n / d - static_cast<i64>((n ^ d) < 0 && n % d != 0); }\ni64 ceil_div(const i64 n, const i64 d) { assert(d != 0); return n / d + static_cast<i64>((n ^ d) >= 0 && n % d != 0); }\n\nint main() {\n cin.tie(0)->sync_with_stdio(0);\n \n int n, x0, y0, t; cin >> n >> x0 >> y0 >> t;\n const int M = 51;\n auto h = [&](int x, int y) { return x * M + y; };\n vector to(M * M, vector(4, int(-1)));\n rep(_, n) {\n int sx, sy, ex, ey; cin >> sx >> sy >> ex >> ey;\n if(sx == ex) {\n const int x = sx;\n if(sy > ey) swap(sy, ey);\n srep(y, sy, ey) {\n const int u = h(x, y), v = h(x, y + 1);\n to[u][0] = v;\n to[v][2] = u;\n }\n } else {\n const int y = sy;\n if(sx > ex) swap(sx, ex);\n srep(x, sx, ex) {\n const int u = h(x, y), v = h(x + 1, y);\n to[u][1] = v;\n to[v][3] = u;\n }\n }\n }\n\n vector<int> dir = {-1};\n rep(_, t) {\n int d; char c; cin >> d >> c;\n rep(i, d-1) dir.push_back(-1);\n if(c == 'N') dir.push_back(0);\n if(c == 'E') dir.push_back(1);\n if(c == 'S') dir.push_back(2);\n if(c == 'W') dir.push_back(3);\n }\n const int LAST = dir.back();\n dir.pop_back();\n\n vector dp(M * M, vector(4, int(0)));\n dp[h(x0, y0)] = vector(4, int(1));\n for(int cons : dir) {\n vector nt(M * M, vector(4, int(0)));\n rep(i, M * M) rep(d, 4) if(dp[i][d]) {\n rep(nd, 4) {\n if(abs(d - nd) == 2) continue;\n if(cons != -1 and d != cons and nd != cons) continue;\n const int ni = to[i][nd];\n if(ni == -1) continue;\n nt[ni][nd] = 1;\n }\n }\n dp = move(nt);\n }\n\n vector<pair<int, int>> ans;\n rep(i, M * M) {\n bool ok = [&] {\n rep(d, 4) {\n if(abs(d - LAST) == 2) continue;\n if(dp[i][d]) {\n if(d == LAST) return true;\n if(to[i][LAST] != -1) return true;\n }\n }\n return false;\n }();\n if(ok) ans.push_back({i / M, i % M});\n }\n sort(ans.begin(), ans.end());\n for(auto [x, y] : ans) cout << x << \" \" << y << \"\\n\";\n}", "accuracy": 1, "time_ms": 50, "memory_kb": 3900, "score_of_the_acc": -0.0297, "final_rank": 5 }, { "submission_id": "aoj_1349_8493021", "code_snippet": "#include<bits/stdc++.h>\n\nusing namespace std;\n\n#define rep(i, n) for(int i = 0; i < (n); i++)\n#define per(i, n) for(int i = (n)-1; i >= 0; i--)\n#define rep2(i, l, r) for(int i = (l); i < (r); i++)\n#define per2(i, l, r) for(int i = (r)-1; i >= (l); i--)\n#define each(e, x) for(auto &e : x)\n#define sz(x) (int)x.size()\n#define all(x) begin(x), end(x)\n#define rall(x) rbegin(x), rend(x)\n#define eb emplace_back\n#define MM << ' ' <<\n#define TT template<typename T>\nusing ll = long long;\nusing pii = pair<int, int>;\nusing pll = pair<ll, ll>;\n\nTT bool chmin(T &x, T y) { return x > y ? (x = y, true) : false; }\n\nTT bool chmax(T &x, T y) { return x < y ? (x = y, true) : false; }\n\nTT using minheap = priority_queue<T, vector<T>, greater<T>>;\nTT using maxheap = priority_queue<T>;\n\nTT void print(vector<T> v, T x = 0) {\n int n = sz(v);\n rep(i, n) cout << v[i] + x << (i == n - 1 ? '\\n' : ' ');\n if (n == 0) cout << '\\n';\n}\n\nTT void printn(vector<T> v, T x = 0) {\n int n = sz(v);\n rep(i, n) cout << v[i] + x << '\\n';\n}\n\nTT void rearrange(vector<T> &v) {\n sort(all(v));\n v.erase(unique(all(v)), end(v));\n}\n\nTT int lb(vector<T> &v, T x) { return lower_bound(all(v), x) - begin(v); }\n\nTT int ub(vector<T> &v, T x) { return upper_bound(all(v), x) - begin(v); }\n\nconst int inf = (1 << 30) - 1;\nconst ll INF = (1LL << 60) - 1;\n\nstruct io_setup {\n io_setup() {\n ios_base::sync_with_stdio(false);\n cin.tie(NULL);\n cout << fixed << setprecision(15);\n }\n} io_setup;\n\nint main() {\n int n, x0, y0, t;\n cin >> n >> x0 >> y0 >> t;\n x0 *= 2, y0 *= 2;\n int h = 101, w = 101;\n vector<vector<bool>> road(h, vector<bool>(w, false));\n rep(i, n) {\n int x1, y1, x2, y2;\n cin >> x1 >> y1 >> x2 >> y2;\n x1 *= 2, y1 *= 2, x2 *= 2, y2 *= 2;\n if (x1 == x2) {\n rep2(y, min(y1, y2), max(y1, y2) + 1) road[y][x1] = true;\n }\n else {\n rep2(x, min(x1, x2), max(x1, x2) + 1) road[y2][x] = true;\n }\n }\n vector<int> d(t);\n vector<char> dirs(t);\n rep(i, t) cin >> d[i] >> dirs[i], d[i] *= 2;\n int sd = accumulate(all(d), 0);\n vector<char> dl(sd + 1, '.');\n int ns = 0;\n rep(i, t) ns += d[i], dl[ns] = dirs[i];\n string dc = \"ENWS\";\n int dx[] = {1, 0, -1, 0}, dy[] = {0, 1, 0, -1};\n vector f(h, vector(w, vector(4, 0)));\n queue<pair<pii, int>> que;\n auto check = [&](int ny, int nx, int dir) ->bool {\n int ney = ny + dy[dir], nex = nx + dx[dir];\n if (ney < 0 || h <= ney || nex < 0 || w <= nex)\n return false;\n return road[ney][nex];\n };\n rep(dir, 4) if (check(y0, x0, dir)) f[y0][x0][dir] = 1, que.emplace(pair(y0, x0), dir);\n rep(nt, sd) {\n queue<pair<pii, int>> nque;\n vector nf(h, vector(w, vector(4, 0)));\n while (sz(que)) {\n auto [yx, ndir] = que.front();\n que.pop();\n auto [ny, nx] = yx;\n if (nt == sd)\n continue;\n int ney = ny + dy[ndir], nex = nx + dx[ndir];\n rep(dd, 4) {\n if (dd == 2)\n continue;\n int dir = (ndir + dd) % 4;\n if (!check(ney, nex, dir) || (dl[nt + 1] != '.' && dl[nt + 1] != dc[dir] && dl[nt + 1] != dc[ndir]))\n continue;\n if (!nf[ney][nex][dir])\n nf[ney][nex][dir] = 1, nque.emplace(pair(ney, nex), dir);\n }\n }\n swap(f, nf);\n swap(que, nque);\n }\n rep(x, w) rep(y, h) {\n rep(dir, 4) {\n if (f[y][x][dir]) {\n cout << x / 2 << ' ' << y / 2 << endl;\n break;\n }\n }\n }\n}", "accuracy": 1, "time_ms": 420, "memory_kb": 4604, "score_of_the_acc": -0.1298, "final_rank": 11 }, { "submission_id": "aoj_1349_7235414", "code_snippet": "#include <iostream>\nusing namespace std;\n\n// Input\nint N, SX, SY, T;\nint AX[1009], AY[1009], BX[1009], BY[1009];\nint D[1009]; char C[1009];\nint Total_Time, Direction[1009];\n\n// Dynamic Programming\nint dx[4] = {0, 1, 0, -1};\nint dy[4] = {1, 0, -1, 0};\nbool Road[1009][1009];\nbool dp[1009][109][109][4];\n\nbool check(int ax, int ay) {\n\tif (ax < 0 || ay < 0 || ax > 100 || ay > 100) return false;\n\treturn Road[ax][ay];\n}\n\nvoid Transition(int tm, int cx, int cy, int dir) {\n\tint dir0 = (dir + 0) % 4;\n\tint dir1 = (dir + 1) % 4;\n\tint dir3 = (dir + 3) % 4;\n\tcx += dx[dir];\n\tcy += dy[dir];\n\t\n\t// Pattern 1: No turn\n\tif (check(cx * 2 + dx[dir0], cy * 2 + dy[dir0]) == true) {\n\t\tif (Direction[tm+1] == -1 || Direction[tm+1] == dir0) {\n\t\t\tdp[tm+1][cx][cy][dir0] = true;\n\t\t}\n\t}\n\t\n\t// Pattern 2: Left Turn\n\tif (check(cx * 2 + dx[dir3], cy * 2 + dy[dir3]) == true) {\n\t\tif (Direction[tm+1] == -1 || Direction[tm+1] == dir0 || Direction[tm+1] == dir3) {\n\t\t\tdp[tm+1][cx][cy][dir3] = true;\n\t\t}\n\t}\n\t\n\t// Pattern 3: Right Turn\n\tif (check(cx * 2 + dx[dir1], cy * 2 + dy[dir1]) == true) {\n\t\tif (Direction[tm+1] == -1 || Direction[tm+1] == dir0 || Direction[tm+1] == dir1) {\n\t\t\tdp[tm+1][cx][cy][dir1] = true;\n\t\t}\n\t}\n}\n\nint main() {\n\t// Input\n\tcin >> N >> SX >> SY >> T;\n\tfor (int i = 1; i <= N; i++) {\n\t\tcin >> AX[i] >> AY[i] >> BX[i] >> BY[i];\n\t\tif (AX[i] > BX[i]) swap(AX[i], BX[i]);\n\t\tif (AY[i] > BY[i]) swap(AY[i], BY[i]);\n\t\tfor (int j = AX[i] * 2; j <= BX[i] * 2; j++) {\n\t\t\tfor (int k = AY[i] * 2; k <= BY[i] * 2; k++) Road[j][k] = true;\n\t\t}\n\t}\n\tfor (int i = 0; i <= 1000; i++) Direction[i] = -1;\n\tfor (int i = 0; i < T; i++) {\n\t\tcin >> D[i] >> C[i];\n\t\tTotal_Time += D[i];\n\t\tif (C[i] == 'N') Direction[Total_Time] = 0;\n\t\tif (C[i] == 'E') Direction[Total_Time] = 1;\n\t\tif (C[i] == 'S') Direction[Total_Time] = 2;\n\t\tif (C[i] == 'W') Direction[Total_Time] = 3;\n\t}\n\t\n\t// Dynamic Programming\n\tif (check(SX * 2 + dx[0], SY * 2 + dy[0]) == true) dp[0][SX][SY][0] = true;\n\tif (check(SX * 2 + dx[1], SY * 2 + dy[1]) == true) dp[0][SX][SY][1] = true;\n\tif (check(SX * 2 + dx[2], SY * 2 + dy[2]) == true) dp[0][SX][SY][2] = true;\n\tif (check(SX * 2 + dx[3], SY * 2 + dy[3]) == true) dp[0][SX][SY][3] = true;\n\tfor (int i = 0; i < Total_Time; i++) {\n\t\tfor (int j = 0; j <= 50; j++) {\n\t\t\tfor (int k = 0; k <= 50; k++) {\n\t\t\t\tfor (int l = 0; l < 4; l++) {\n\t\t\t\t\tif (dp[i][j][k][l] == false) continue;\n\t\t\t\t\t// cout << i << \" \" << j << \" \" << k << \" \" << l << endl;\n\t\t\t\t\tTransition(i, j, k, l);\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n\t\n\t// Output\n\tfor (int i = 0; i <= 50; i++) {\n\t\tfor (int j = 0; j <= 50; j++) {\n\t\t\tbool flag = false;\n\t\t\tfor (int k = 0; k < 4; k++) {\n\t\t\t\tif (dp[Total_Time][i][j][k] == true) flag = true;\n\t\t\t}\n\t\t\tif (flag == true) cout << i << \" \" << j << endl;\n\t\t}\n\t}\n\treturn 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 38372, "score_of_the_acc": -1, "final_rank": 17 }, { "submission_id": "aoj_1349_6740595", "code_snippet": "#include <bits/stdc++.h>\n\n#ifdef _MSC_VER\n# include <intrin.h>\n#else\n# include <x86intrin.h>\n#endif\n\n#include <limits>\n#include <type_traits>\n\nnamespace suisen {\n// ! utility\ntemplate <typename ...Types>\nusing constraints_t = std::enable_if_t<std::conjunction_v<Types...>, std::nullptr_t>;\ntemplate <bool cond_v, typename Then, typename OrElse>\nconstexpr decltype(auto) constexpr_if(Then&& then, OrElse&& or_else) {\n if constexpr (cond_v) {\n return std::forward<Then>(then);\n } else {\n return std::forward<OrElse>(or_else);\n }\n}\n\n// ! function\ntemplate <typename ReturnType, typename Callable, typename ...Args>\nusing is_same_as_invoke_result = std::is_same<std::invoke_result_t<Callable, Args...>, ReturnType>;\ntemplate <typename F, typename T>\nusing is_uni_op = is_same_as_invoke_result<T, F, T>;\ntemplate <typename F, typename T>\nusing is_bin_op = is_same_as_invoke_result<T, F, T, T>;\n\ntemplate <typename Comparator, typename T>\nusing is_comparator = std::is_same<std::invoke_result_t<Comparator, T, T>, bool>;\n\n// ! integral\ntemplate <typename T, typename = constraints_t<std::is_integral<T>>>\nconstexpr int bit_num = std::numeric_limits<std::make_unsigned_t<T>>::digits;\ntemplate <typename T, unsigned int n>\nstruct is_nbit { static constexpr bool value = bit_num<T> == n; };\ntemplate <typename T, unsigned int n>\nstatic constexpr bool is_nbit_v = is_nbit<T, n>::value;\n\n// ?\ntemplate <typename T>\nstruct safely_multipliable {};\ntemplate <>\nstruct safely_multipliable<int> { using type = long long; };\ntemplate <>\nstruct safely_multipliable<long long> { using type = __int128_t; };\ntemplate <>\nstruct safely_multipliable<unsigned int> { using type = unsigned long long; };\ntemplate <>\nstruct safely_multipliable<unsigned long int> { using type = __uint128_t; };\ntemplate <>\nstruct safely_multipliable<unsigned long long> { using type = __uint128_t; };\ntemplate <>\nstruct safely_multipliable<float> { using type = float; };\ntemplate <>\nstruct safely_multipliable<double> { using type = double; };\ntemplate <>\nstruct safely_multipliable<long double> { using type = long double; };\ntemplate <typename T>\nusing safely_multipliable_t = typename safely_multipliable<T>::type;\n\ntemplate <typename T, typename = void>\nstruct rec_value_type {\n using type = T;\n};\ntemplate <typename T>\nstruct rec_value_type<T, std::void_t<typename T::value_type>> {\n using type = typename rec_value_type<typename T::value_type>::type;\n};\ntemplate <typename T>\nusing rec_value_type_t = typename rec_value_type<T>::type;\n\n} // namespace suisen\n\n// ! type aliases\nusing i128 = __int128_t;\nusing u128 = __uint128_t;\n\ntemplate <typename T>\nusing pq_greater = std::priority_queue<T, std::vector<T>, std::greater<T>>;\ntemplate <typename T, typename U>\nusing umap = std::unordered_map<T, U>;\n\n// ! macros (capital: internal macro)\n#define OVERLOAD2(_1,_2,name,...) name\n#define OVERLOAD3(_1,_2,_3,name,...) name\n#define OVERLOAD4(_1,_2,_3,_4,name,...) name\n\n#define REP4(i,l,r,s) for(std::remove_reference_t<std::remove_const_t<decltype(r)>>i=(l);i<(r);i+=(s))\n#define REP3(i,l,r) REP4(i,l,r,1)\n#define REP2(i,n) REP3(i,0,n)\n#define REPINF3(i,l,s) for(std::remove_reference_t<std::remove_const_t<decltype(l)>>i=(l);;i+=(s))\n#define REPINF2(i,l) REPINF3(i,l,1)\n#define REPINF1(i) REPINF2(i,0)\n#define RREP4(i,l,r,s) for(std::remove_reference_t<std::remove_const_t<decltype(r)>>i=(l)+fld((r)-(l)-1,s)*(s);i>=(l);i-=(s))\n#define RREP3(i,l,r) RREP4(i,l,r,1)\n#define RREP2(i,n) RREP3(i,0,n)\n\n#define rep(...) OVERLOAD4(__VA_ARGS__, REP4 , REP3 , REP2 )(__VA_ARGS__)\n#define rrep(...) OVERLOAD4(__VA_ARGS__, RREP4 , RREP3 , RREP2 )(__VA_ARGS__)\n#define repinf(...) OVERLOAD3(__VA_ARGS__, REPINF3, REPINF2, REPINF1)(__VA_ARGS__)\n\n#define CAT_I(a, b) a##b\n#define CAT(a, b) CAT_I(a, b)\n#define UNIQVAR(tag) CAT(tag, __LINE__)\n#define loop(n) for (std::remove_reference_t<std::remove_const_t<decltype(n)>> UNIQVAR(loop_variable) = n; UNIQVAR(loop_variable) --> 0;)\n\n#define all(iterable) std::begin(iterable), std::end(iterable)\n#define input(type, ...) type __VA_ARGS__; read(__VA_ARGS__)\n\n#ifdef LOCAL\n# define debug(...) debug_internal(#__VA_ARGS__, __VA_ARGS__)\n\ntemplate <class T, class... Args>\nvoid debug_internal(const char* s, T&& first, Args&&... args) {\n constexpr const char* prefix = \"[\\033[32mDEBUG\\033[m] \";\n constexpr const char* open_brakets = sizeof...(args) == 0 ? \"\" : \"(\";\n constexpr const char* close_brakets = sizeof...(args) == 0 ? \"\" : \")\";\n std::cerr << prefix << open_brakets << s << close_brakets << \": \" << open_brakets << std::forward<T>(first);\n ((std::cerr << \", \" << std::forward<Args>(args)), ...);\n std::cerr << close_brakets << \"\\n\";\n}\n\n#else\n# define debug(...) void(0)\n#endif\n\n// ! I/O utilities\n\n// __int128_t\nstd::ostream& operator<<(std::ostream& dest, __int128_t value) {\n std::ostream::sentry s(dest);\n if (s) {\n __uint128_t tmp = value < 0 ? -value : value;\n char buffer[128];\n char* d = std::end(buffer);\n do {\n --d;\n *d = \"0123456789\"[tmp % 10];\n tmp /= 10;\n } while (tmp != 0);\n if (value < 0) {\n --d;\n *d = '-';\n }\n int len = std::end(buffer) - d;\n if (dest.rdbuf()->sputn(d, len) != len) {\n dest.setstate(std::ios_base::badbit);\n }\n }\n return dest;\n}\n// __uint128_t\nstd::ostream& operator<<(std::ostream& dest, __uint128_t value) {\n std::ostream::sentry s(dest);\n if (s) {\n char buffer[128];\n char* d = std::end(buffer);\n do {\n --d;\n *d = \"0123456789\"[value % 10];\n value /= 10;\n } while (value != 0);\n int len = std::end(buffer) - d;\n if (dest.rdbuf()->sputn(d, len) != len) {\n dest.setstate(std::ios_base::badbit);\n }\n }\n return dest;\n}\n\n// pair\ntemplate <typename T, typename U>\nstd::ostream& operator<<(std::ostream& out, const std::pair<T, U>& a) {\n return out << a.first << ' ' << a.second;\n}\n// tuple\ntemplate <unsigned int N = 0, typename ...Args>\nstd::ostream& operator<<(std::ostream& out, const std::tuple<Args...>& a) {\n if constexpr (N >= std::tuple_size_v<std::tuple<Args...>>) {\n return out;\n } else {\n out << std::get<N>(a);\n if constexpr (N + 1 < std::tuple_size_v<std::tuple<Args...>>) {\n out << ' ';\n }\n return operator<<<N + 1>(out, a);\n }\n}\n// vector\ntemplate <typename T>\nstd::ostream& operator<<(std::ostream& out, const std::vector<T>& a) {\n for (auto it = a.begin(); it != a.end();) {\n out << *it;\n if (++it != a.end()) out << ' ';\n }\n return out;\n}\n// array\ntemplate <typename T, size_t N>\nstd::ostream& operator<<(std::ostream& out, const std::array<T, N>& a) {\n for (auto it = a.begin(); it != a.end();) {\n out << *it;\n if (++it != a.end()) out << ' ';\n }\n return out;\n}\ninline void print() { std::cout << '\\n'; }\ntemplate <typename Head, typename... Tail>\ninline void print(const Head& head, const Tail &...tails) {\n std::cout << head;\n if (sizeof...(tails)) std::cout << ' ';\n print(tails...);\n}\ntemplate <typename Iterable>\nauto print_all(const Iterable& v, std::string sep = \" \", std::string end = \"\\n\") -> decltype(std::cout << *v.begin(), void()) {\n for (auto it = v.begin(); it != v.end();) {\n std::cout << *it;\n if (++it != v.end()) std::cout << sep;\n }\n std::cout << end;\n}\n\n__int128_t parse_i128(std::string& s) {\n __int128_t ret = 0;\n for (int i = 0; i < int(s.size()); i++) if ('0' <= s[i] and s[i] <= '9') ret = 10 * ret + s[i] - '0';\n if (s[0] == '-') ret = -ret;\n return ret;\n}\n__uint128_t parse_u128(std::string& s) {\n __uint128_t ret = 0;\n for (int i = 0; i < int(s.size()); i++) if ('0' <= s[i] and s[i] <= '9') ret = 10 * ret + s[i] - '0';\n return ret;\n}\n// __int128_t\nstd::istream& operator>>(std::istream& in, __int128_t& v) {\n std::string s;\n in >> s;\n v = parse_i128(s);\n return in;\n}\n// __uint128_t\nstd::istream& operator>>(std::istream& in, __uint128_t& v) {\n std::string s;\n in >> s;\n v = parse_u128(s);\n return in;\n}\n// pair\ntemplate <typename T, typename U>\nstd::istream& operator>>(std::istream& in, std::pair<T, U>& a) {\n return in >> a.first >> a.second;\n}\n// tuple\ntemplate <unsigned int N = 0, typename ...Args>\nstd::istream& operator>>(std::istream& in, std::tuple<Args...>& a) {\n if constexpr (N >= std::tuple_size_v<std::tuple<Args...>>) {\n return in;\n } else {\n return operator>><N + 1>(in >> std::get<N>(a), a);\n }\n}\n// vector\ntemplate <typename T>\nstd::istream& operator>>(std::istream& in, std::vector<T>& a) {\n for (auto it = a.begin(); it != a.end(); ++it) in >> *it;\n return in;\n}\n// array\ntemplate <typename T, size_t N>\nstd::istream& operator>>(std::istream& in, std::array<T, N>& a) {\n for (auto it = a.begin(); it != a.end(); ++it) in >> *it;\n return in;\n}\ntemplate <typename ...Args>\nvoid read(Args &...args) {\n (std::cin >> ... >> args);\n}\n\n// ! integral utilities\n\n// Returns pow(-1, n)\ntemplate <typename T>\nconstexpr inline int pow_m1(T n) {\n return -(n & 1) | 1;\n}\n// Returns pow(-1, n)\ntemplate <>\nconstexpr inline int pow_m1<bool>(bool n) {\n return -int(n) | 1;\n}\n\n// Returns floor(x / y)\ntemplate <typename T>\nconstexpr inline T fld(const T x, const T y) {\n return (x ^ y) >= 0 ? x / y : (x - (y + pow_m1(y >= 0))) / y;\n}\ntemplate <typename T>\nconstexpr inline T cld(const T x, const T y) {\n return (x ^ y) <= 0 ? x / y : (x + (y + pow_m1(y >= 0))) / y;\n}\n\ntemplate <typename T, suisen::constraints_t<suisen::is_nbit<T, 16>> = nullptr>\n__attribute__((target(\"popcnt\"))) constexpr inline int popcount(const T x) { return _mm_popcnt_u32(x); }\ntemplate <typename T, suisen::constraints_t<suisen::is_nbit<T, 32>> = nullptr>\n__attribute__((target(\"popcnt\"))) constexpr inline int popcount(const T x) { return _mm_popcnt_u32(x); }\ntemplate <typename T, suisen::constraints_t<suisen::is_nbit<T, 64>> = nullptr>\n__attribute__((target(\"popcnt\"))) constexpr inline int popcount(const T x) { return _mm_popcnt_u64(x); }\ntemplate <typename T, suisen::constraints_t<suisen::is_nbit<T, 16>> = nullptr>\nconstexpr inline int count_lz(const T x) { return x ? __builtin_clz(x) : suisen::bit_num<T>; }\ntemplate <typename T, suisen::constraints_t<suisen::is_nbit<T, 32>> = nullptr>\nconstexpr inline int count_lz(const T x) { return x ? __builtin_clz(x) : suisen::bit_num<T>; }\ntemplate <typename T, suisen::constraints_t<suisen::is_nbit<T, 64>> = nullptr>\nconstexpr inline int count_lz(const T x) { return x ? __builtin_clzll(x) : suisen::bit_num<T>; }\ntemplate <typename T, suisen::constraints_t<suisen::is_nbit<T, 16>> = nullptr>\nconstexpr inline int count_tz(const T x) { return x ? __builtin_ctz(x) : suisen::bit_num<T>; }\ntemplate <typename T, suisen::constraints_t<suisen::is_nbit<T, 32>> = nullptr>\nconstexpr inline int count_tz(const T x) { return x ? __builtin_ctz(x) : suisen::bit_num<T>; }\ntemplate <typename T, suisen::constraints_t<suisen::is_nbit<T, 64>> = nullptr>\nconstexpr inline int count_tz(const T x) { return x ? __builtin_ctzll(x) : suisen::bit_num<T>; }\ntemplate <typename T>\nconstexpr inline int floor_log2(const T x) { return suisen::bit_num<T> -1 - count_lz(x); }\ntemplate <typename T>\nconstexpr inline int ceil_log2(const T x) { return floor_log2(x) + ((x & -x) != x); }\ntemplate <typename T>\nconstexpr inline int kth_bit(const T x, const unsigned int k) { return (x >> k) & 1; }\ntemplate <typename T>\nconstexpr inline int parity(const T x) { return popcount(x) & 1; }\n\n// ! container\n\ntemplate <typename T, typename Comparator, suisen::constraints_t<suisen::is_comparator<Comparator, T>> = nullptr>\nauto priqueue_comp(const Comparator comparator) {\n return std::priority_queue<T, std::vector<T>, Comparator>(comparator);\n}\n\ntemplate <typename Iterable>\nauto isize(const Iterable& iterable) -> decltype(int(iterable.size())) {\n return iterable.size();\n}\n\ntemplate <typename T, typename Gen, suisen::constraints_t<suisen::is_same_as_invoke_result<T, Gen, int>> = nullptr>\nauto generate_vector(int n, Gen generator) {\n std::vector<T> v(n);\n for (int i = 0; i < n; ++i) v[i] = generator(i);\n return v;\n}\ntemplate <typename T>\nauto generate_range_vector(T l, T r) {\n return generate_vector(r - l, [l](int i) { return l + i; });\n}\ntemplate <typename T>\nauto generate_range_vector(T n) {\n return generate_range_vector(0, n);\n}\n\ntemplate <typename T>\nvoid sort_unique_erase(std::vector<T>& a) {\n std::sort(a.begin(), a.end());\n a.erase(std::unique(a.begin(), a.end()), a.end());\n}\n\ntemplate <typename InputIterator, typename BiConsumer>\nauto foreach_adjacent_values(InputIterator first, InputIterator last, BiConsumer f) -> decltype(f(*first++, *last), void()) {\n if (first != last) for (auto itr = first, itl = itr++; itr != last; itl = itr++) f(*itl, *itr);\n}\ntemplate <typename Container, typename BiConsumer>\nauto foreach_adjacent_values(Container c, BiConsumer f) -> decltype(c.begin(), c.end(), void()) {\n foreach_adjacent_values(c.begin(), c.end(), f);\n}\n\n// ! other utilities\n\n// x <- min(x, y). returns true iff `x` has chenged.\ntemplate <typename T>\ninline bool chmin(T& x, const T& y) {\n if (y >= x) return false;\n x = y;\n return true;\n}\n// x <- max(x, y). returns true iff `x` has chenged.\ntemplate <typename T>\ninline bool chmax(T& x, const T& y) {\n if (y <= x) return false;\n x = y;\n return true;\n}\n\ntemplate <typename T, std::enable_if_t<std::is_integral_v<T>, std::nullptr_t> = nullptr>\nstd::string bin(T val, int bit_num = -1) {\n std::string res;\n if (bit_num >= 0) {\n for (int bit = bit_num; bit-- > 0;) res += '0' + ((val >> bit) & 1);\n } else {\n for (; val; val >>= 1) res += '0' + (val & 1);\n std::reverse(res.begin(), res.end());\n }\n return res;\n}\n\ntemplate <typename T, std::enable_if_t<std::is_integral_v<T>, std::nullptr_t> = nullptr>\nstd::vector<T> digits_low_to_high(T val, T base = 10) {\n std::vector<T> res;\n for (; val; val /= base) res.push_back(val % base);\n if (res.empty()) res.push_back(T{ 0 });\n return res;\n}\ntemplate <typename T, std::enable_if_t<std::is_integral_v<T>, std::nullptr_t> = nullptr>\nstd::vector<T> digits_high_to_low(T val, T base = 10) {\n auto res = digits_low_to_high(val, base);\n std::reverse(res.begin(), res.end());\n return res;\n}\n\ntemplate <typename T>\nstd::string join(const std::vector<T>& v, const std::string& sep, const std::string& end) {\n std::ostringstream ss;\n for (auto it = v.begin(); it != v.end();) {\n ss << *it;\n if (++it != v.end()) ss << sep;\n }\n ss << end;\n return ss.str();\n}\n\nnamespace suisen {}\nusing namespace suisen;\nusing namespace std;\n\nstruct io_setup {\n io_setup(int precision = 20) {\n std::ios::sync_with_stdio(false);\n std::cin.tie(nullptr);\n std::cout << std::fixed << std::setprecision(precision);\n }\n} io_setup_{};\n\n// ! code from here\n\nconstexpr int K = 50;\n\nconstexpr int dx[4]{ 1, 0, -1, 0 };\nconstexpr int dy[4]{ 0, 1, 0, -1 };\nconstexpr int E = 0, N = 1, W = 2, S = 3;\n\nint main() {\n array<int, 256> ch_to_d{};\n ch_to_d['E'] = E;\n ch_to_d['N'] = N;\n ch_to_d['W'] = W;\n ch_to_d['S'] = S;\n\n input(int, n, x0, y0, t);\n\n vector h(K, vector<int8_t>(K + 1, false));\n vector v(K + 1, vector<int8_t>(K, false));\n\n loop(n) {\n input(int, x1, y1, x2, y2);\n\n if (x1 == x2) {\n if (y1 > y2) swap(y1, y2);\n rep(y, y1, y2) {\n v[x1][y] = true;\n }\n } else if (y1 == y2) {\n if (x1 > x2) swap(x1, x2);\n rep(x, x1, x2) {\n h[x][y1] = true;\n }\n }\n }\n\n auto try_succ = [&](int x, int y, int d) -> optional<pair<int, int>> {\n if (d == E) {\n if (x == K or not h[x][y]) return std::nullopt;\n return pair{ x + 1, y };\n } else if (d == W) {\n if (x == 0 or not h[x - 1][y]) return std::nullopt;\n return pair{ x - 1, y };\n } else if (d == N) {\n if (y == K or not v[x][y]) return std::nullopt;\n return pair{ x, y + 1 };\n } else {\n if (y == 0 or not v[x][y - 1]) return std::nullopt;\n return pair{ x, y - 1 };\n }\n };\n\n vector<tuple<int, int, int>> st;\n st.emplace_back(x0, y0, 0b1111);\n\n loop(t) {\n input(int, dist);\n input(char, dir_ch);\n const int dir = ch_to_d[dir_ch];\n\n rep(cur_dist, dist - 1) {\n vector<tuple<int, int, int>> nst;\n for (auto [x, y, pd] : st) {\n rep(d, 4) if (kth_bit(pd, d)) {\n auto nxy = try_succ(x, y, d);\n if (not nxy.has_value()) continue;\n auto [nx, ny] = *nxy;\n nst.emplace_back(nx, ny, 0b1111 & ~(1 << (d ^ 2)));\n }\n }\n sort_unique_erase(nst);\n nst.swap(st);\n }\n\n vector<tuple<int, int, int>> nst;\n for (auto [x, y, pd] : st) {\n rep(d, 4) if (kth_bit(pd, d)) {\n auto nxy = try_succ(x, y, d);\n if (not nxy.has_value()) continue;\n auto [nx, ny] = *nxy;\n nst.emplace_back(nx, ny, (d == dir ? 0b1111 : 1 << dir) & ~(1 << (d ^ 2)));\n }\n }\n sort_unique_erase(nst);\n nst.swap(st);\n }\n\n vector<pair<int, int>> ans;\n for (auto [x, y, pd] : st) {\n rep(d, 4) if (kth_bit(pd, d)) {\n auto nxy = try_succ(x, y, d);\n if (not nxy.has_value()) continue;\n ans.emplace_back(x, y);\n }\n }\n sort_unique_erase(ans);\n\n print_all(ans, \"\\n\");\n\n return 0;\n}", "accuracy": 1, "time_ms": 100, "memory_kb": 3600, "score_of_the_acc": -0.032, "final_rank": 6 }, { "submission_id": "aoj_1349_6667840", "code_snippet": "#line 1 \"c.cpp\"\n/*\tauthor: Kite_kuma\n\tcreated: 2022.05.30 15:48:54 */\n\n#line 2 \"SPJ-Library/template/kuma.hpp\"\n\n#line 2 \"SPJ-Library/template/basic_func.hpp\"\n\n#include <algorithm>\n#include <cassert>\n#include <iostream>\n#include <vector>\n\ntemplate <class T0, class T1, class T2>\ninline bool in_range(T0 x, T1 lef, T2 rig) {\n\treturn ((lef <= x) && (x < rig));\n}\n\ntemplate <class T>\ninline bool chmin(T &a, T b) {\n\tif(a > b) {\n\t\ta = b;\n\t\treturn true;\n\t}\n\treturn false;\n}\ntemplate <class T>\ninline bool chmax(T &a, T b) {\n\tif(a < b) {\n\t\ta = b;\n\t\treturn true;\n\t}\n\treturn false;\n}\n\nvoid Yes(bool flag = true) { std::cout << (flag ? \"Yes\" : \"No\") << '\\n'; }\nvoid YES(bool flag = true) { std::cout << (flag ? \"YES\" : \"NO\") << '\\n'; }\n\ntemplate <class T>\nvoid drop(T answer) {\n\tstd::cout << answer << '\\n';\n\texit(0);\n}\n\ntemplate <class T>\nvoid vout(std::vector<T> const &v, bool vertically = 0) {\n\tif(vertically) {\n\t\tfor(auto const &a : v) {\n\t\t\tstd::cout << a << '\\n';\n\t\t}\n\t\treturn;\n\t}\n\tfor(auto it = v.begin(); it != v.end(); it++) {\n\t\tstd::cout << (*it);\n\t\tif(it != v.end() - 1) {\n\t\t\tstd::cout << ' ';\n\t\t}\n\t}\n\tstd::cout << '\\n';\n\treturn;\n}\n\ninline void print() { std::cout << '\\n'; }\ntemplate <class T>\ninline void print(const T &x) {\n\tstd::cout << x << '\\n';\n\treturn;\n}\ntemplate <typename Head, typename... Tail>\nvoid print(const Head &H, const Tail &... T) {\n\tstd::cout << H << \" \";\n\tprint(T...);\n}\n\ntemplate <class T>\nvoid add(std::vector<T> &v, T value) {\n\tfor(auto &a : v) a += value;\n\treturn;\n}\n\ntemplate <class T>\nT greatest_lower_multiple(T x, T d) {\n\tif(d == 0) return 0;\n\tif(d < 0) d *= -1;\n\tT y = x % d;\n\tif(y < 0) y += d;\n\treturn x - y;\n}\n\ntemplate <class T>\nT least_upper_multiple(T x, T d) {\n\treturn -greatest_lower_multiple(-x, d);\n}\n\n// ceil(a / b);\ntemplate <class T>\nT dup(T a, T b) {\n\tassert(b);\n\tif(b < 0) {\n\t\ta *= -1;\n\t\tb *= -1;\n\t}\n\treturn least_upper_multiple(a, b) / b;\n}\n\nlong long pow_ll(long long a, long long n) {\n\tassert(n >= 0LL);\n\tif(n == 0) return 1LL;\n\tif(a == 0) return 0LL;\n\tif(a == 1) return 1LL;\n\tif(a == -1) return (n & 1LL) ? -1LL : 1LL;\n\tlong long res = 1;\n\twhile(n > 1LL) {\n\t\tif(n & 1LL) res *= a;\n\t\ta *= a;\n\t\tn >>= 1;\n\t}\n\treturn res * a;\n}\n\nlong long modpow(long long a, long long n, long long mod) {\t // a^n mod\n\tassert(n >= 0 && mod);\n\tif(mod == 1) return 0LL;\n\tlong long res = 1;\n\ta %= mod;\n\twhile(n > 0) {\n\t\tif(n & 1) res = res * a % mod;\n\t\ta = a * a % mod;\n\t\tn >>= 1;\n\t}\n\treturn res < 0 ? res + mod : res;\n}\n\n// return x which satisfies a * x % mod == gcd(a, mod)\n// not (mod | a)\nlong long modinv(long long a, const long long mod) {\n\tlong long b = mod, u = 1, v = 0;\n\twhile(b) {\n\t\tlong long t = a / b;\n\t\ta -= t * b;\n\t\tstd::swap(a, b);\n\t\tu -= t * v;\n\t\tstd::swap(u, v);\n\t}\n\tu %= mod;\n\tif(u < 0) u += mod;\n\treturn u;\n}\n\ntemplate <class T>\nint lb(const std::vector<T> &a, const T x) {\n\treturn (int)std::distance(a.begin(), std::lower_bound(a.begin(), a.end(), x));\n}\ntemplate <class T>\nint ub(const std::vector<T> &a, const T x) {\n\treturn (int)std::distance(a.begin(), std::upper_bound(a.begin(), a.end(), x));\n}\ntemplate <class T>\nvoid unq_sort(std::vector<T> &a) {\n\tstd::sort(a.begin(), a.end());\n\ta.erase(std::unique(a.begin(), a.end()), a.end());\n}\ntemplate <class T>\nstd::vector<int> press(const std::vector<T> &a) {\n\tstd::vector<T> vec = a;\n\tunq_sort(vec);\n\tstd::vector<int> ret;\n\tfor(const auto &v : a) ret.push_back(lb(vec, v));\n\treturn ret;\n}\n#line 1 \"SPJ-Library/template/header.hpp\"\n#include <bits/stdc++.h>\n#line 2 \"SPJ-Library/template/io.hpp\"\n\n#line 4 \"SPJ-Library/template/io.hpp\"\n\n#line 8 \"SPJ-Library/template/debug.hpp\"\n\n#line 3 \"SPJ-Library/template/constants.hpp\"\n\nconstexpr int inf = 1000'000'000;\nconstexpr long long INF = 1'000'000'000'000'000'000LL;\nconstexpr int mod_1000000007 = 1000000007;\nconstexpr int mod_998244353 = 998244353;\nconst long double pi = acosl(-1.);\nconstexpr int dx[8] = {1, 0, -1, 0, 1, -1, -1, 1};\nconstexpr int dy[8] = {0, 1, 0, -1, 1, 1, -1, -1};\n#line 10 \"SPJ-Library/template/debug.hpp\"\n\nnamespace viewer {\n\ntemplate <typename T, typename U>\nvoid view(const std::pair<T, U> &p);\n\ntemplate <class T0, class T1, class T2>\nvoid view(const std::tuple<T0, T1, T2> &p);\n\ntemplate <class T0, class T1, class T2, class T3>\nvoid view(const std::tuple<T0, T1, T2, T3> &p);\n\nvoid view(const long long &e);\n\nvoid view(const int &e);\n\ntemplate <typename T>\nvoid view(const T &e);\n\ntemplate <typename T>\nvoid view(const std::set<T> &s);\n\ntemplate <typename T>\nvoid view(const std::unordered_set<T> &s);\n\ntemplate <typename T>\nvoid view(const std::multiset<T> &s);\n\ntemplate <typename T>\nvoid view(const std::unordered_multiset<T> &s);\n\ntemplate <typename T>\nvoid view(const std::vector<T> &v);\n\ntemplate <typename T, std::size_t ary_size>\nvoid view(const std::array<T, ary_size> &v);\n\ntemplate <typename T>\nvoid view(const std::vector<std::vector<T>> &vv);\n\ntemplate <typename T, typename U>\nvoid view(const std::vector<std::pair<T, U>> &v);\n\ntemplate <class T0, class T1, class T2>\nvoid view(const std::vector<std::tuple<T0, T1, T2>> &v);\n\ntemplate <class T0, class T1, class T2, class T3>\nvoid view(const std::vector<std::tuple<T0, T1, T2, T3>> &v);\n\ntemplate <typename map_type>\nvoid view_map_container(const map_type &m);\n\ntemplate <typename T, typename U>\nvoid view(const std::map<T, U> &m);\n\ntemplate <typename T, typename U>\nvoid view(const std::unordered_map<T, U> &m);\n\ntemplate <typename container_type>\nvoid view_container(const container_type &c, bool vertically = false) {\n\ttypename container_type::const_iterator begin = c.begin();\n\tconst typename container_type::const_iterator end = c.end();\n\tif(vertically) {\n\t\tstd::cerr << \"{\\n\";\n\t\twhile(begin != end) {\n\t\t\tstd::cerr << '\\t';\n\t\t\tview(*(begin++));\n\t\t\tif(begin != end) std::cerr << ',';\n\t\t\tstd::cerr << '\\n';\n\t\t}\n\t\tstd::cerr << '}';\n\t\treturn;\n\t}\n\tstd::cerr << \"{ \";\n\twhile(begin != end) {\n\t\tview(*(begin++));\n\t\tif(begin != end) std::cerr << ',';\n\t\tstd::cerr << ' ';\n\t}\n\tstd::cerr << '}';\n}\n\ntemplate <typename T, typename U>\nvoid view(const std::pair<T, U> &p) {\n\tstd::cerr << '(';\n\tview(p.first);\n\tstd::cerr << \", \";\n\tview(p.second);\n\tstd::cerr << ')';\n}\n\ntemplate <class T0, class T1, class T2>\nvoid view(const std::tuple<T0, T1, T2> &p) {\n\tstd::cerr << '(';\n\tview(std::get<0>(p));\n\tstd::cerr << \", \";\n\tview(std::get<1>(p));\n\tstd::cerr << \", \";\n\tview(std::get<2>(p));\n\tstd::cerr << ')';\n}\n\ntemplate <class T0, class T1, class T2, class T3>\nvoid view(const std::tuple<T0, T1, T2, T3> &p) {\n\tstd::cerr << '(';\n\tview(std::get<0>(p));\n\tstd::cerr << \", \";\n\tview(std::get<1>(p));\n\tstd::cerr << \", \";\n\tview(std::get<2>(p));\n\tstd::cerr << \", \";\n\tview(std::get<3>(p));\n\tstd::cerr << ')';\n}\n\nvoid view(const long long &e) {\n\tif(e == INF)\n\t\tstd::cerr << \"INF\";\n\telse if(e == -INF)\n\t\tstd::cerr << \"-INF\";\n\telse\n\t\tstd::cerr << e;\n}\n\nvoid view(const int &e) {\n\tif(e == inf)\n\t\tstd::cerr << \"inf\";\n\telse if(e == -inf)\n\t\tstd::cerr << \"-inf\";\n\telse\n\t\tstd::cerr << e;\n}\n\ntemplate <typename T>\nvoid view(const T &e) {\n\tstd::cerr << e;\n}\n\ntemplate <typename T>\nvoid view(const std::set<T> &s) {\n\tview_container(s);\n}\n\ntemplate <typename T>\nvoid view(const std::unordered_set<T> &s) {\n\tview_container(s);\n}\n\ntemplate <typename T>\nvoid view(const std::multiset<T> &s) {\n\tview_container(s);\n}\n\ntemplate <typename T>\nvoid view(const std::unordered_multiset<T> &s) {\n\tview_container(s);\n}\n\ntemplate <typename T>\nvoid view(const std::vector<T> &v) {\n\tview_container(v);\n}\n\ntemplate <typename T, std::size_t ary_size>\nvoid view(const std::array<T, ary_size> &v) {\n\tview_container(v);\n}\n\ntemplate <typename T>\nvoid view(const std::vector<std::vector<T>> &vv) {\n\tview_container(vv, true);\n}\n\ntemplate <typename T, typename U>\nvoid view(const std::vector<std::pair<T, U>> &v) {\n\tview_container(v, true);\n}\n\ntemplate <class T0, class T1, class T2>\nvoid view(const std::vector<std::tuple<T0, T1, T2>> &v) {\n\tview_container(v, true);\n}\n\ntemplate <class T0, class T1, class T2, class T3>\nvoid view(const std::vector<std::tuple<T0, T1, T2, T3>> &v) {\n\tview_container(v, true);\n}\n\ntemplate <typename map_type>\nvoid view_map_container(const map_type &m) {\n\tstd::cerr << \"{\\n\";\n\tfor(typename map_type::const_iterator it = m.begin(); it != m.end(); it++) {\n\t\tstd::cerr << \"\\t[\";\n\t\tview(it->first);\n\t\tstd::cerr << \"] : \";\n\t\tview(it->second);\n\t\tstd::cerr << '\\n';\n\t}\n\tstd::cerr << \"}\";\n}\n\ntemplate <typename T, typename U>\nvoid view(const std::map<T, U> &m) {\n\tview_map_container(m);\n}\n\ntemplate <typename T, typename U>\nvoid view(const std::unordered_map<T, U> &m) {\n\tview_map_container(m);\n}\n\n} // namespace viewer\n\n// when compiling : g++ foo.cpp -DLOCAL\n#ifdef LOCAL\nvoid debug_out() {}\ntemplate <typename T>\nvoid debug_out(const T &x) {\n\tviewer::view(x);\n}\ntemplate <typename Head, typename... Tail>\nvoid debug_out(const Head &H, const Tail &... T) {\n\tviewer::view(H);\n\tstd::cerr << \", \";\n\tdebug_out(T...);\n}\n#define debug(...) \\\n\tdo { \\\n\t\tstd::cerr << __LINE__ << \" [\" << #__VA_ARGS__ << \"] : [\"; \\\n\t\tdebug_out(__VA_ARGS__); \\\n\t\tstd::cerr << \"]\\n\"; \\\n\t} while(0)\n#else\n#define debug(...) (void(0))\n#endif\n#line 2 \"SPJ-Library/template/scanner.hpp\"\n\n#line 6 \"SPJ-Library/template/scanner.hpp\"\n\nnamespace scanner {\ntemplate <class T>\nvoid scan(T &a) {\n\tstd::cin >> a;\n}\n\ntemplate <class T, class L>\nvoid scan(std::pair<T, L> &p) {\n\tscan(p.first);\n\tscan(p.second);\n}\n\ntemplate <class T0, class T1, class T2>\nvoid scan(std::tuple<T0, T1, T2> &p) {\n\tT0 t0;\n\tT1 t1;\n\tT2 t2;\n\tscan(t0);\n\tscan(t1);\n\tscan(t2);\n\tp = std::make_tuple(t0, t1, t2);\n}\n\ntemplate <class T0, class T1, class T2, class T3>\nvoid scan(std::tuple<T0, T1, T2, T3> &p) {\n\tT0 t0;\n\tT1 t1;\n\tT2 t2;\n\tT3 t3;\n\tscan(t0);\n\tscan(t1);\n\tscan(t2);\n\tscan(t3);\n\tp = std::make_tuple(t0, t1, t2, t3);\n}\n\ntemplate <class T>\nvoid scan(std::vector<T> &a) {\n\tfor(auto &i : a) scan(i);\n}\n\nvoid INPUT() {}\ntemplate <class Head, class... Tail>\nvoid INPUT(Head &head, Tail &... tail) {\n\tscan(head);\n\tINPUT(tail...);\n}\n} // namespace scanner\n#line 7 \"SPJ-Library/template/io.hpp\"\n\n#define VEC(type, name, size) \\\n\tvector<type> name(size); \\\n\tscanner::INPUT(name)\n#define VVEC(type, name, h, w) \\\n\tvector<vector<type>> name(h, vector<type>(w)); \\\n\tscanner::INPUT(name)\n#define INT(...) \\\n\tint __VA_ARGS__; \\\n\tscanner::INPUT(__VA_ARGS__)\n#define LL(...) \\\n\tlong long __VA_ARGS__; \\\n\tscanner::INPUT(__VA_ARGS__)\n#define STR(...) \\\n\tstring __VA_ARGS__; \\\n\tscanner::INPUT(__VA_ARGS__)\n#define CHR(...) \\\n\tchar __VA_ARGS__; \\\n\tscanner::INPUT(__VA_ARGS__)\n#define DBL(...) \\\n\tdouble __VA_ARGS__; \\\n\tscanner::INPUT(__VA_ARGS__)\n#define LD(...) \\\n\tlong double __VA_ARGS__; \\\n\tscanner::INPUT(__VA_ARGS__)\n#define TPL3(type0, type1, type2, name) \\\n\tstd::tuple<type0, type1, type2> name; \\\n\tscanner::INPUT(name);\n#define TPL4(type0, type1, type2, type3, name) \\\n\tstd::tuple<type0, type1, type2, type3> name; \\\n\tscanner::INPUT(name);\n\ntemplate <typename T1, typename T2>\nstd::istream &operator>>(std::istream &is, std::pair<T1, T2> &p) {\n\tis >> p.first >> p.second;\n\treturn is;\n}\n\ntemplate <typename T1, typename T2>\nstd::ostream &std::operator<<(std::ostream &os, const std::pair<T1, T2> &p) {\n\tos << p.first << ' ' << p.second;\n\treturn os;\n}\ntemplate <class T>\nstd::ostream &std::operator<<(std::ostream &os, const std::vector<T> &v) {\n\tfor(typename std::vector<T>::const_iterator it = v.begin(); it != v.end(); it++) {\n\t\tif(it != v.begin()) std::cerr << ' ';\n\t\tos << *it;\n\t}\n\treturn os;\n}\n\nstruct fast_io {\n\tfast_io() {\n\t\tstd::ios::sync_with_stdio(false);\n\t\tstd::cin.tie(nullptr);\n\t\tstd::cout << std::fixed << std::setprecision(15);\n\t\tsrand((unsigned)time(NULL));\n\t}\n} fast_io_;\n#line 2 \"SPJ-Library/template/macros.hpp\"\n\n#define foa(s, v) for(auto &s : v)\n#define all(v) (v).begin(), (v).end()\n#define rall(v) (v).rbegin(), (v).rend()\n#define pcnt(n) __builtin_popcountll((long long)n)\n\n#define REPname(a, b, c, d, e, ...) e\n#define rep(...) REPname(__VA_ARGS__, REP3, REP2, REP1, REP0)(__VA_ARGS__)\n#define REP0(x) for(int Counter_in_rep_macro = 0; Counter_in_rep_macro < (x); ++Counter_in_rep_macro)\n#define REP1(i, x) for(int i = 0; i < (x); ++i)\n#define REP2(i, l, r) for(int i = (l); i < (r); ++i)\n#define REP3(i, l, r, c) for(int i = (l); i < (r); i += (c))\n#define DREPname(a, b, c, d, e, ...) e\n#define drep(...) DREPname(__VA_ARGS__, DREP3, DREP2, DREP1)(__VA_ARGS__)\n#define DREP1(i, x) for(int i = (x)-1; i >= 0; --i)\n#define DREP2(i, l, r) for(int i = (r)-1; i >= (l); --i)\n#define DREP3(i, l, r, c) for(int i = (r)-1; i >= (l); i -= (c))\n\nusing ll = long long;\nusing ld = long double;\nusing ull = unsigned long long;\nusing vi = std::vector<int>;\nusing vvi = std::vector<std::vector<int>>;\nusing vvvi = std::vector<std::vector<std::vector<int>>>;\nusing vll = std::vector<ll>;\nusing vvll = std::vector<vll>;\nusing vvvll = std::vector<vvll>;\ntemplate <class T = ll>\nusing V = std::vector<T>;\ntemplate <class T = ll>\nusing VV = V<V<T>>;\ntemplate <class T = ll>\nusing VVV = V<V<V<T>>>;\ntemplate <class T = ll>\nusing pqup = std::priority_queue<T, std::vector<T>, std::greater<T>>;\ntemplate <class T = ll>\nusing pqdn = std::priority_queue<T>;\n#line 7 \"SPJ-Library/template/kuma.hpp\"\n\nusing namespace std;\n#line 5 \"c.cpp\"\n\nvoid solve(int n) {\n\tint x, y, time_end;\n\tcin >> x >> y >> time_end;\n#ifdef LOCAL\n\tconst int len = 53;\n#else\n\tconst int len = 53;\n#endif\n\tconst int dir_max = 4;\n\tVVV<int> can_go(len, vvi(len, vi(dir_max, 0)));\n\trep(n) {\n\t\tint x1, y1;\n\t\tcin >> x1 >> y1;\n\t\tINT(x2, y2);\n\t\tint direction = 0;\n\t\tint distance = abs(x1 + y1 - (x2 + y2));\n\t\tif(x1 < x2) {\n\t\t\tdirection = 0;\n\t\t} else if(x1 > x2) {\n\t\t\tdirection = 2;\n\t\t} else if(y1 < y2) {\n\t\t\tdirection = 1;\n\t\t} else {\n\t\t\tdirection = 3;\n\t\t}\n\t\tassert(x1 + dx[direction] * distance == x2 && y1 + dy[direction] * distance == y2);\n\t\tint rev_dir = (direction + 2) % dir_max;\n\t\twhile(x1 != x2 || y1 != y2) {\n\t\t\tint nxtx = x1 + dx[direction];\n\t\t\tint nxty = y1 + dy[direction];\n\t\t\tcan_go[x1][y1][direction] = 1;\n\t\t\tcan_go[nxtx][nxty][rev_dir] = 1;\n\t\t\tx1 = nxtx;\n\t\t\ty1 = nxty;\n\t\t}\n\t}\n\n\tvector<pair<int, int>> conditions;\n\tmap<char, int> ch_dir;\n\tch_dir['N'] = 1;\n\tch_dir['E'] = 0;\n\tch_dir['S'] = 3;\n\tch_dir['W'] = 2;\n\n\tint dist = 0;\n\trep(time_end) {\n\t\tint t;\n\t\tcin >> t;\n\t\tdist += t;\n\t\tchar c;\n\t\tcin >> c;\n\t\tassert(ch_dir.count(c));\n\t\tconditions.emplace_back(dist, ch_dir[c]);\n\t}\n\n\tauto it = conditions.begin();\n\n\tvector<vector<vi>> ok(len, vvi(len, vi(dir_max, 0)));\n\trep(dir, dir_max) ok[x][y][dir] = 1;\n\n\t// debug(can_go);\n\n\trep(dist_now, 1, dist + 1) {\n\t\tvvvi tmp(len, vvi(len, vi(dir_max, 0)));\n\n\t\trep(i, len) rep(j, len) rep(dir, dir_max) {\n\t\t\tif(ok[i][j][dir] && can_go[i][j][dir]) {\n\t\t\t\ttmp[i + dx[dir]][j + dy[dir]][dir] = 1;\n\t\t\t}\n\t\t}\n\n\t\tfoa(vv, ok) foa(v, vv) foa(c, v) c = 0;\n\n\t\trep(i, len) rep(j, len) {\n\t\t\trep(dir, dir_max) if(tmp[i][j][dir]) {\n\t\t\t\trep(nxtdir, dir - 1, dir + 2) {\n\t\t\t\t\tif(can_go[i][j][(nxtdir + dir_max) % dir_max]) {\n\t\t\t\t\t\tok[i][j][(nxtdir + dir_max) % dir_max] = 1;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\n\t\tif(dist_now == it->first) {\n\t\t\trep(i, len) rep(j, len) rep(d, dir_max) {\n\t\t\t\tif(d != it->second) ok[i][j][d] = 0;\n\t\t\t}\n\n\t\t\tconst int dir = it->second;\n\t\t\trep(i, len) rep(j, len) {\n\t\t\t\tif(tmp[i][j][dir]) {\n\t\t\t\t\trep(nxtdir, dir - 1, dir + 2) {\n\t\t\t\t\t\tif(can_go[i][j][(nxtdir + dir_max) % dir_max]) {\n\t\t\t\t\t\t\tok[i][j][(nxtdir + dir_max) % dir_max] = 1;\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\n\t\t\tit++;\n\t\t}\n\n\t\trep(i, len) rep(j, len) rep(d, dir_max) {\n\t\t\tif(ok[i][j][d]) {\n\t\t\t\tdebug(dist_now, i, j, d);\n\t\t\t}\n\t\t}\n\t}\n\tassert(it == conditions.end());\n\tdebug(conditions);\n\n\trep(i, len) rep(j, len) {\n\t\tif(*max_element(all(ok[i][j]))) print(i, j);\n\t}\n\n#ifdef LOCAL\n\tprint(\"finish!!\");\n#endif\n\n\treturn;\n}\n\nint main() {\n\tint n;\n\twhile(cin >> n && n) solve(n);\n\n\treturn 0;\n}", "accuracy": 1, "time_ms": 70, "memory_kb": 3928, "score_of_the_acc": -0.0348, "final_rank": 7 }, { "submission_id": "aoj_1349_6401492", "code_snippet": "#include<bits/stdc++.h>\n#define rep(i,a,...) for(int i = (a)*(strlen(#__VA_ARGS__)!=0);i<(int)(strlen(#__VA_ARGS__)?__VA_ARGS__:(a));++i)\n#define per(i,a,...) for(int i = (strlen(#__VA_ARGS__)?__VA_ARGS__:(a))-1;i>=(int)(strlen(#__VA_ARGS__)?(a):0);--i)\n#define foreach(i, n) for(auto &i:(n))\n#define all(x) (x).begin(), (x).end()\n#define bit(x) (1ll << (x))\n#define lambda(RES_TYPE, ...) (function<RES_TYPE(__VA_ARGS__)>)[&](__VA_ARGS__) -> RES_TYPE\n#define method(FUNC_NAME, RES_TYPE, ...) function<RES_TYPE(__VA_ARGS__)> FUNC_NAME = lambda(RES_TYPE, __VA_ARGS__)\nusing namespace std;\nusing ll = long long;\nusing pii = pair<int,int>;\nusing pll = pair<ll,ll>;\n//const ll MOD = (ll)1e9+7;\nconst ll MOD = 998244353;\nconst int INF = (ll)1e9+7;\nconst ll INFLL = (ll)1e18;\ntemplate<class t>\nusing vvector = vector<vector<t>>;\ntemplate<class t>\nusing vvvector = vector<vector<vector<t>>>;\ntemplate<class t>\nusing priority_queuer = priority_queue<t, vector<t>, greater<t>>;\ntemplate<class t, class u> bool chmax(t &a, u b){if(a<b){a=b;return true;}return false;}\ntemplate<class t, class u> bool chmin(t &a, u b){if(a>b){a=b;return true;}return false;}\n#ifdef DEBUG\n#define debug(x) cout<<\"LINE \"<<__LINE__<<\": \"<<#x<<\" = \"<<x<<endl;\n#else\n#define debug(x) (void)0\n#endif\n\nnamespace templates{\n ll modpow(ll x, ll b,ll mod=MOD){\n ll res = 1;\n while(b){\n if(b&1)res = res * x % mod;\n x = x * x % mod;\n b>>=1;\n }\n return res;\n }\n\n ll modinv(ll x){\n return modpow(x, MOD-2);\n }\n\n bool was_output = false;\n\n void print();\n template <class t> void print(const vector<t> &);\n template <class t, class u> void print(const pair<t, u> &);\n template <class t> void print(const t&);\n template <class Head, class... Tail> void print(const Head&, const Tail&...);\n\n template <class t> void println(const vector<vector<t>>&);\n template <class t> void println(const vector<t>&);\n template <class t> void println(const t&);\n template <class Head, class... Tail> void println(const Head&, const Tail&...);\n void println();\n void newline();\n\n void print(){\n return;\n }\n\n template <class t>\n void print(const vector<t>&x){\n for(const t&i:x)print(i);\n }\n template <class t, class u>\n void print(const pair<t,u>&p){\n print(p.first);\n print(p.second);\n }\n template <class t>\n void print(const t&x){\n if(was_output)cout<<\" \";\n cout<<x;\n was_output = true;\n }\n template <class Head, class... Tail>\n void print(const Head&head,const Tail&...tail){\n print(head);\n print(tail...);\n }\n\n template<class t>\n void println(const vector<vector<t>>&x){\n for(vector<t> i:x)println(i);\n }\n template<class t>\n void println(const vector<t>&x){\n for(const t&i:x)print(i);\n println();\n }\n template <class t>\n void println(const t&x){\n print(x);\n println();\n }\n void println(){\n if(was_output){\n cout << endl;\n was_output = false;\n }\n }\n template <class Head, class... Tail>\n void println(const Head&head,const Tail&...tail){\n print(head);\n println(tail...);\n }\n\n void newline(){\n was_output = true;\n println();\n }\n\n template<class t>\n istream& operator>>(istream&is, vector<t>&x){\n for(auto &i:x)is >> i;\n return is;\n }\n\n template<class t, class u>\n istream& operator>>(istream&is, pair<t, u>&x){\n is >> x.first >> x.second;\n return is;\n }\n\n template<class t>\n ostream& operator<<(ostream&os, vector<t> &v){\n os << \"{\";\n for(t &i:v){\n os << i << \", \";\n }\n os << \"}\";\n return os;\n }\n\n template<class t = long long>\n t in(){\n t res; cin >> res; return res;\n }\n\n template<class t>\n vector<t> sorted(vector<t> line,function<bool(t,t)> comp=[](t a,t b){return a<b;}){\n sort(line.begin(),line.end(),comp);\n return line;\n }\n\n template<class t>\n vector<t> reversed(vector<t> line){\n reverse(line.begin(),line.end());\n return line;\n }\n string reversed(string str){\n reverse(str.begin(),str.end());\n return str;\n }\n\n long long gcd(long long a,long long b){\n while(b){\n a %= b;\n swap(a,b);\n }\n return a;\n }\n\n long long lcm(long long a,long long b){\n return a / gcd(a,b) * b;\n }\n\n class output_initializer{\n public:\n output_initializer(){\n ios::sync_with_stdio(false);\n cin.tie(nullptr);\n cout << setprecision(20);\n }\n };output_initializer OUTPUT_INITIALIZER_INSTANCE = output_initializer();\n}\n\nusing namespace templates;\n\nclass Move {\npublic:\n int x;\n int y;\n int last;\n int next;\n Move(int x=0,int y=0,int last=-1,int next=-1):x(x),y(y),last(last),next(next){}\n bool canMove(int nextDir){\n return (last < 0 or (last - nextDir + 4) % 4 != 2) and\n (next < 0 or next == nextDir);\n }\n Move moved(int nextDir){\n const int dx[] = {1,0,-1,0};\n const int dy[] = {0,1,0,-1};\n return Move(x+dx[nextDir],y+dy[nextDir],nextDir);\n }\n Move withNext(int dir){\n return Move(x,y,last,dir);\n }\n pii position()const{\n return pii(x,y);\n }\n};\n\nbool operator<(const Move &a, const Move &b){\n return array<int,4>({a.x,a.y,a.last,a.next}) < array<int,4>({b.x,b.y,b.last,b.next});\n}\n \n\nset<pii> func(){\n int n = in();\n int x0 = in();\n int y0 = in();\n int t = in();\n vvvector<int> oks(51,vvector<int>(51,vector<int>(4,false)));\n rep(i,n){\n int x0 = in();\n int y0 = in();\n int x1 = in();\n int y1 = in();\n if(x0>x1)swap(x0,x1);\n if(y0>y1)swap(y0,y1);\n if(y0==y1){\n rep(x,x0,x1+1){\n if(x!=x0)oks[x][y0][2] = true;\n if(x!=x1)oks[x][y0][0] = true;\n }\n }else{\n rep(y,y0,y1+1){\n if(y!=y0)oks[x0][y][3] = true;\n if(y!=y1)oks[x0][y][1] = true;\n }\n }\n }\n method(moved,vector<Move>,Move p){\n vector<Move> res;\n rep(i,4){\n if(not p.canMove(i))continue;\n if(not oks[p.x][p.y][i])continue;\n Move mp = p.moved(i);\n res.emplace_back(mp);\n }\n return res;\n };\n set<Move> current;\n {\n rep(i,4){\n if(oks[x0][y0][i]){\n current.emplace(Move(x0,y0).withNext(i));\n }\n }\n }\n rep(_,t){\n int T = in();\n char D = in<char>();\n int dir;\n\n set<Move> ncurrent;\n set<pair<Move,int>> used;\n method(movedTimes,void,Move p,int time,int dir){\n method(rec,void,Move p,int t){\n if(t==time){\n if(p.last==dir){\n ncurrent.emplace(p);\n }else if(oks[p.x][p.y][dir] and p.canMove(dir)){\n ncurrent.emplace(p.withNext(dir));\n }\n return;\n }\n if(used.count(make_pair(p,t)))return;\n used.emplace(p,t);\n foreach(np,moved(p)){\n rec(np,t+1);\n }\n };\n rec(p,0);\n };\n switch(D){\n case 'E':\n dir = 0;\n break;\n case 'N':\n dir = 1;\n break;\n case 'W':\n dir = 2;\n break;\n case 'S':\n dir = 3;\n break;\n }\n foreach(e,current){\n movedTimes(e,T,dir);\n }\n current = ncurrent;\n }\n set<pii> res;\n foreach(i,current)res.emplace(i.position());\n return res;\n}\n\nint main(){\n ios::sync_with_stdio(false);\n cin.tie(nullptr);\n foreach(i,func())println(i);\n return 0;\n}", "accuracy": 1, "time_ms": 380, "memory_kb": 4344, "score_of_the_acc": -0.1137, "final_rank": 10 }, { "submission_id": "aoj_1349_6364313", "code_snippet": "#include \"bits/stdc++.h\"\nusing namespace std;\n\ntypedef string::const_iterator State;\n#define eps 1e-8L\n#define MAX_MOD 1000000007LL\n#define GYAKU 500000004LL\n#define MOD 998244353LL\n#define pb push_back\n#define mp make_pair\ntypedef long long ll;\ntypedef long double ld;\n#define REP(a, b) for (long long(a) = 0; (a) < (b); ++(a))\n#define ALL(x) (x).begin(), (x).end()\n\n#define int long long\nvoid solve()\n{\n int n, x0, y0, t;\n cin >> n >> x0 >> y0 >> t;\n set<tuple<int, int, int>> cur;\n REP(i, 4)\n {\n cur.insert({x0 * 2, y0 * 2, i});\n }\n set<pair<int, int>> valids;\n REP(i, n)\n {\n int a, b, c, d;\n cin >> a >> b >> c >> d;\n a *= 2;\n b *= 2;\n c *= 2;\n d *= 2;\n for (int i = min(a, c); i <= max(a, c); ++i)\n {\n for (int q = min(b, d); q <= max(b, d); ++q)\n {\n valids.insert({i, q});\n }\n }\n }\n REP(i, t)\n {\n int moves;\n string s;\n cin >> moves >> s;\n const int dx[4] = {-1, 0, 1, 0};\n const int dy[4] = {0, -1, 0, 1};\n REP(_, moves)\n {\n set<tuple<int, int, int>> nexts;\n for (auto x : cur)\n {\n REP(t, 4)\n {\n if (t == 2)\n continue;\n int itr = get<2>(x) + t;\n itr %= 4;\n int next_x = get<0>(x) + dx[itr];\n int next_y = get<1>(x) + dy[itr];\n if (valids.count({next_x, next_y}))\n {\n next_x += dx[itr];\n next_y += dy[itr];\n nexts.insert({next_x, next_y, itr});\n }\n if (get<2>(x) >= 4)\n break;\n }\n }\n cur = nexts;\n }\n {\n int expected;\n if (s == \"W\")\n expected = 0;\n else if (s == \"S\")\n expected = 1;\n else if (s == \"E\")\n expected = 2;\n else\n expected = 3;\n set<tuple<int, int, int>> filters;\n for (auto x : cur)\n {\n if (get<2>(x) == expected)\n filters.insert(x);\n REP(t, 4)\n {\n if (t % 2 == 0)\n continue;\n int itr = get<2>(x) + t;\n itr %= 4;\n int next_x = get<0>(x) + dx[itr];\n int next_y = get<1>(x) + dy[itr];\n if (valids.count({next_x, next_y}))\n {\n if (itr == expected)\n {\n filters.insert({get<0>(x), get<1>(x), itr + 4});\n }\n }\n }\n }\n cur = filters;\n }\n }\n set<pair<int, int>> ans;\n for (auto x : cur)\n {\n ans.insert({get<0>(x), get<1>(x)});\n }\n for (auto x : ans)\n {\n cout << x.first / 2 << \" \" << x.second / 2 << endl;\n }\n}\n#undef int\n\n// generated by oj-template v4.7.2\n// (https://github.com/online-judge-tools/template-generator)\nint main()\n{\n // Fasterize input/output script\n ios::sync_with_stdio(false);\n cin.tie(nullptr);\n cout << fixed << setprecision(100);\n // scanf/printf user should delete this fasterize input/output script\n\n int t = 1;\n // cin >> t; // comment out if solving multi testcase\n for (int testCase = 1; testCase <= t; ++testCase)\n {\n solve();\n }\n return 0;\n}", "accuracy": 1, "time_ms": 220, "memory_kb": 3636, "score_of_the_acc": -0.059, "final_rank": 9 }, { "submission_id": "aoj_1349_6039630", "code_snippet": "#include <stdio.h>\n#include <bits/stdc++.h>\nusing namespace std;\n#define rep(i,n) for (int i = 0; i < (n); ++i)\n#define Inf 1000000001\n\nvector ok(51,vector(51,vector<bool>(4,false)));\nvector<int> dx = {1,0,-1,0},dy = {0,1,0,-1};\n\nvector<pair<pair<int,int>,int>> get(int d,int dir,vector<pair<pair<int,int>,int>> xy){\n\tvector f(51,vector(51,vector<bool>(4,false)));\n\trep(i,xy.size()){\n\t\tf[xy[i].first.first][xy[i].first.second][xy[i].second] = true;\n\t}\n\tvector<pair<pair<int,int>,int>> ret;\n\trep(i,d){\n\t\tvector nf(51,vector(51,vector<bool>(4,false)));\n\t\trep(j,51){\n\t\t\trep(k,51){\n\t\t\t\trep(l,4){\n\t\t\t\t\tif(f[j][k][l]==false)continue;\n\t\t\t\t\tif(ok[j][k][l]==false)continue;\n\t\t\t\t\tint x = j+dx[l],y = k+dy[l];\n\t\t\t\t\t//cout<<i<<','<<x<<','<<y<<endl;\n\t\t\t\t\trep(ll,4){\n\t\t\t\t\t\tif((l^ll)==2)continue;\n\t\t\t\t\t\tnf[x][y][ll] = true;\n\t\t\t\t\t\t\n\t\t\t\t\t\tif(i==d-1 && (l==dir || ll==dir)){\n\t\t\t\t\t\t\tret.emplace_back(make_pair(x,y),ll);\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\tswap(f,nf);\n\t}\n\treturn ret;\n\t\n}\n\nint main(){\n\t\n\tint n,x0,y0,t;\n\tcin>>n>>x0>>y0>>t;\t\n\t\n\trep(i,n){\n\t\tint xs,ys,xe,ye;\n\t\tcin>>xs>>ys>>xe>>ye;\n\t\tif(xs==xe){\n\t\t\tif(ys>ye)swap(ys,ye);\n\t\t\tfor(int j=ys;j<=ye;j++){\n\t\t\t\tif(j!=ye){\n\t\t\t\t\tok[xs][j][1] = true;\n\t\t\t\t}\n\t\t\t\tif(j!=ys){\n\t\t\t\t\tok[xs][j][3] = true;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\telse{\n\t\t\tif(xs>xe)swap(xs,xe);\n\t\t\tfor(int j=xs;j<=xe;j++){\n\t\t\t\tif(j!=xe){\n\t\t\t\t\tok[j][ys][0] = true;\n\t\t\t\t}\n\t\t\t\tif(j!=xs){\n\t\t\t\t\tok[j][ys][2] = true;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n\t\n\tvector<int> d(t),dir(t);\n\trep(i,t){\n\t\tcin>>d[i];\n\t\tchar c;\n\t\tcin>>c;\n\t\t\n\t\tif(c=='N')dir[i] = 1;\n\t\tif(c=='W')dir[i] = 2;\n\t\tif(c=='S')dir[i] = 3;\n\t\tif(c=='E')dir[i] = 0;\n\t}\n\t\n\t\n\tvector<pair<pair<int,int>,int>> xy;\n\trep(k,4){\n\t\txy.emplace_back(make_pair(x0,y0),k);\n\t}\n\trep(k,t){\n\t\txy = get(d[k],dir[k],xy);\n\t}\n\n\tvector<pair<int,int>> ans;\n\t//cout<<xy.size()<<endl;\n\trep(i,xy.size()){\n\t\tif(ok[xy[i].first.first][xy[i].first.second][xy[i].second]){\n\t\t\tans.push_back(xy[i].first);\n\t\t}\n\t}\n\tsort(ans.begin(),ans.end());\n\tans.erase(unique(ans.begin(),ans.end()),ans.end());\n\t//cout<<ans.size()<<endl;\n\trep(i,ans.size()){\n\t\tcout<<ans[i].first<<' '<<ans[i].second<<endl;\n\t}\n\t\n\treturn 0;\n}", "accuracy": 1, "time_ms": 80, "memory_kb": 4044, "score_of_the_acc": -0.0403, "final_rank": 8 }, { "submission_id": "aoj_1349_5778058", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\nconst int MAX_T = 1005, MAX_C = 55;\nconst int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};\nbool dp[MAX_T][MAX_C][MAX_C][4];\nbool adv[MAX_C][MAX_C][4];\n\nint main() {\n cin.tie(0);\n ios::sync_with_stdio(false);\n int n, x_0, y_0, t;\n cin >> n >> x_0 >> y_0 >> t;\n for (; n--;) {\n int a, b, c, d;\n cin >> a >> b >> c >> d;\n if (a == c) {\n if (b > d) swap(b, d);\n for (int i = b; i <= d; i++) {\n if (i != b) adv[a][i][3] = true;\n if (i != d) adv[a][i][1] = true;\n }\n } else {\n if (a > c) swap(a, c);\n for (int i = a; i <= c; i++) {\n if (i != a) adv[i][b][2] = true;\n if (i != c) adv[i][b][0] = true;\n }\n }\n }\n\n auto ctoi = [](char c) {\n if (c == 'E') return 0;\n if (c == 'N') return 1;\n if (c == 'W') return 2;\n return 3;\n };\n vector<int> way(MAX_T, -1);\n int sum = 0;\n for (; t--;) {\n int d;\n char c;\n cin >> d >> c;\n sum += d;\n way[sum] = ctoi(c);\n }\n for (int l = 0; l < 4; l++) dp[0][x_0][y_0][l] = true;\n\n for (int i = 0; i < sum; i++) {\n for (int j = 0; j < MAX_C; j++) {\n for (int k = 0; k < MAX_C; k++) {\n for (int l = 0; l < 4; l++) {\n if (!dp[i][j][k][l]) continue;\n for (int nl = 0; nl < 4; nl++) {\n if (!adv[j][k][nl]) continue; // able to advance\n if (~way[i] && l != way[i] && nl != way[i]) continue; // correct information\n if ((l ^ nl) == 2) continue; // prohibit U-turns\n int nx = j + dx[nl], ny = k + dy[nl];\n dp[i + 1][nx][ny][nl] = true;\n }\n }\n }\n }\n }\n\n for (int i = 0; i < MAX_C; i++) {\n for (int j = 0; j < MAX_C; j++) {\n bool ok = false;\n for (int k = 0; k < 4; k++) {\n if (dp[sum][i][j][k] && (k == way[sum] || ((k ^ way[sum]) != 2 && adv[i][j][way[sum]]))) {\n ok = true;\n }\n }\n if (ok) cout << i << ' ' << j << '\\n';\n }\n }\n return 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 13220, "score_of_the_acc": -0.2857, "final_rank": 14 }, { "submission_id": "aoj_1349_5315479", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\ntypedef long long int ll;\ntypedef unsigned long long ull;\n\nmt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());\nll myRand(ll B) {\n\treturn (ull)rng() % B;\n}\n\nconst int N=51;\n\nbool dp[1001][N][N][4];\nint ro[N][N][4];\nbool ch[1001][4];\n\n// 0:N 1:E 2:S 3:W\nint f(char c){\n\tif(c=='N')return 0;\n\telse if(c=='E')return 1;\n\telse if(c=='S')return 2;\n\telse return 3;\n}\n\nint main(){\n\tcin.tie(nullptr);\n\tios::sync_with_stdio(false);\n\tint n,sx,sy,t; cin >> n >> sx >> sy >> t;\n\tfor(int i=0;i<n;i++){\n\t\tint x,y,xx,yy; cin >> x >> y >> xx >> yy;\n\t\tif(x==xx){\n\t\t\tif(y>yy)swap(y,yy);\n\t\t\tfor(int j=y;j<=yy;j++){\n\t\t\t\tif(j>y)ro[x][j][2]=1;\n\t\t\t\tif(j<yy)ro[x][j][0]=1;\n\t\t\t}\n\t\t}\n\t\tif(y==yy){\n\t\t\tif(x>xx)swap(x,xx);\n\t\t\tfor(int j=x;j<=xx;j++){\n\t\t\t\tif(j>x)ro[j][y][3]=1;\n\t\t\t\tif(j<xx)ro[j][y][1]=1;\n\t\t\t}\n\t\t}\n\t}\n\tfor(int i=0;i<1001;i++){\n\t\tfor(int j=0;j<4;j++){\n\t\t\tch[i][j]=true;\n\t\t}\n\t}\n\tfor(int i=0;i<4;i++){\n\t\tdp[0][sx][sy][i]=true;\n\t}\n\tint sum=0;\n\tfor(int i=0;i<t;i++){\n\t\tint d; cin >> d;\n\t\tchar c; cin >> c;\n\t\tsum+=d;\n\t\tfor(int j=0;j<4;j++){\n\t\t\tch[sum][j]=false;\n\t\t}\n\t\tch[sum][f(c)]=true;\n\t}\n\tfor(int i=0;i<sum;i++){\n\t\tfor(int j=0;j<N;j++){\n\t\t\tfor(int k=0;k<N;k++){\n\t\t\t\tfor(int l=0;l<4;l++){\n\t\t\t\t\tif(!dp[i][j][k][l])continue;\n\t\t\t\t\tif(!ro[j][k][l])continue;\n\t\t\t\t\tint x=j,y=k;\n\t\t\t\t\tif(l==0)y++;\n\t\t\t\t\telse if(l==1)x++;\n\t\t\t\t\telse if(l==2)y--;\n\t\t\t\t\telse x--;\n\t\t\t\t\tif(ch[i+1][l]){\n\t\t\t\t\t\tif(i+1==sum)dp[i+1][x][y][l]=true;\n\t\t\t\t\t\tfor(int d=-1;d<=1;d++){\n\t\t\t\t\t\t\tint nx=(l+d+4)%4;\n\t\t\t\t\t\t\tif(!ro[x][y][nx])continue;\n\t\t\t\t\t\t\tdp[i+1][x][y][nx]=true;\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t\telse{\n\t\t\t\t\t\tfor(int d=-1;d<=1;d++){\n\t\t\t\t\t\t\tint nx=(l+d+4)%4;\n\t\t\t\t\t\t\tif(!ro[x][y][nx])continue;\n\t\t\t\t\t\t\tif(!ch[i+1][nx])continue;\n\t\t\t\t\t\t\tdp[i+1][x][y][nx]=true;\n\t\t\t\t\t\t\tif(i+1==sum)dp[i+1][x][y][l]=true;\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n\tvector<pair<int,int>> res;\n\tfor(int i=0;i<N;i++){\n\t\tfor(int j=0;j<N;j++){\n\t\t\tfor(int k=0;k<4;k++){\n\t\t\t\tif(ch[sum][k] and dp[sum][i][j][k]){\n\t\t\t\t\tres.push_back({i,j});\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n\tfor(auto p:res){\n\t\tprintf(\"%d %d\\n\",p.first,p.second);\n\t}\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 9320, "score_of_the_acc": -0.1749, "final_rank": 12 }, { "submission_id": "aoj_1349_5249092", "code_snippet": "#include <bits/stdc++.h>\n\nusing namespace std;\n\ntypedef long long ll;\ntypedef pair<int, int> P;\n\n#define eps 1e-9\n#define INF 2000000000 // 2e9\n#define LLINF 2000000000000000000ll // 2e18 (llmax:9e18)\n#define all(x) (x).begin(), (x).end()\n#define sq(x) ((x) * (x))\n\n#define rep(i, x) for (int i = 0; i < (int)(x); i++)\n#define drep(i, x) for (int i = ((int)(x)-1); i >= 0; i--)\n\n#define popcount __builtin_popcount\n#define next __next\n#define prev __prev\n\n#ifndef LOCAL\n#define dmp(...) ;\n#else\n#define dmp(...) \\\n cerr << \"[ \" << #__VA_ARGS__ << \" ] : \" << dump_str(__VA_ARGS__) << endl\n#endif\n\n// ---------------- Utility ------------------\n\ntemplate <class T>\nbool chmin(T &a, const T &b) {\n if (a <= b) return false;\n a = b;\n return true;\n}\ntemplate <class T>\nbool chmax(T &a, const T &b) {\n if (a >= b) return false;\n a = b;\n return true;\n}\n\ntemplate <class T>\nusing MaxHeap = priority_queue<T>;\ntemplate <class T>\nusing MinHeap = priority_queue<T, vector<T>, greater<T>>;\n\ntemplate <class T>\nvector<T> vect(int len, T elem) {\n return vector<T>(len, elem);\n}\n\n// ----------------- Input -------------------\n\ntemplate <class T, class U>\nistream &operator>>(istream &is, pair<T, U> &p) {\n return is >> p.first >> p.second;\n}\n\ntemplate <class T>\nistream &operator>>(istream &is, vector<T> &vec) {\n for (int i = 0; i < vec.size(); i++) is >> vec[i];\n return is;\n}\n\n// ----------------- Output ------------------\n\ntemplate <class T, class U>\nostream &operator<<(ostream &os, const pair<T, U> &p) {\n return os << p.first << ',' << p.second;\n}\n\ntemplate <class T>\nostream &operator<<(ostream &os, const vector<T> &v) {\n for (const T &e : v) os << e << \" \";\n return os;\n}\n\ntemplate <class T>\nostream &operator<<(ostream &os, const deque<T> &d) {\n for (const T &e : d) os << e << \" \";\n return os;\n}\n\ntemplate <class T>\nostream &operator<<(ostream &os, const set<T> &s) {\n os << \"{ \";\n for (const T &e : s) os << e << \" \";\n return os << \"}\";\n}\n\ntemplate <class T, class U>\nostream &operator<<(ostream &os, const map<T, U> &m) {\n os << \"{ \";\n for (const auto &[key, val] : m) os << \"( \" << key << \" -> \" << val << \" ) \";\n return os << \"}\";\n}\n\ntemplate <class TupleTy, size_t... I>\nvoid dump_tuple(ostream &os, const TupleTy t, std::index_sequence<I...>) {\n (..., (os << (I == 0 ? \"\" : \",\") << std::get<I>(t)));\n}\n\ntemplate <class... Args>\nostream &operator<<(ostream &os, const tuple<Args...> &t) {\n os << \"(\";\n dump_tuple(os, t, std::make_index_sequence<sizeof...(Args)>());\n return os << \")\";\n}\n\nvoid dump_str_rec(ostringstream &) {}\n\ntemplate <class Head, class... Tail>\nvoid dump_str_rec(ostringstream &oss, Head &&head, Tail &&... tail) {\n oss << \", \" << head;\n dump_str_rec(oss, forward<Tail>(tail)...);\n}\n\ntemplate <class T, class... U>\nstring dump_str(T &&arg, U &&... args) {\n ostringstream oss;\n oss << arg;\n dump_str_rec(oss, forward<U>(args)...);\n return oss.str();\n}\n\n// --------------- Fast I/O ------------------\n\nvoid fastio() {\n cin.tie(0);\n ios::sync_with_stdio(0);\n cout << fixed << setprecision(20);\n}\n\n// ------------ End of template --------------\n\n#define endl \"\\n\"\n\nconst int dx[4] = {1, 0, -1, 0};\nconst int dy[4] = {0, -1, 0, 1};\n\nbool f[51][51][4];\n\nvector<tuple<int, int, int>> cur;\nvector<tuple<int, int, int>> nxt;\n\nbool memo[11][51][51][4];\n\nvector<int> d, dir;\n\nvoid dfs(int dist, int cx, int cy, int pd, int T) {\n if (memo[dist][cx][cy][pd]) return;\n memo[dist][cx][cy][pd] = true;\n\n if (dist == d[T]) {\n if (pd != dir[T] && !f[cx][cy][dir[T]]) return;\n if (pd != dir[T] && (dir[T] + 2) % 4 == pd) return;\n nxt.emplace_back(cx, cy, pd);\n return;\n }\n\n for (int Dir = 0; Dir < 4; Dir++) {\n if ((pd + 2) % 4 == Dir) continue;\n if (dist == 0 && T > 0 && dir[T - 1] != pd && dir[T - 1] != Dir) continue;\n if (!f[cx][cy][Dir]) continue;\n int nx = cx + dx[Dir], ny = cy + dy[Dir];\n assert(!(nx < 0 || nx > 50 || ny < 0 || ny > 50));\n dfs(dist + 1, nx, ny, Dir, T);\n }\n}\n\nvoid solve() {\n int N, x0, y0, t;\n cin >> N >> x0 >> y0 >> t;\n for (int i = 0; i < N; i++) {\n int a, b, c, d;\n cin >> a >> b >> c >> d;\n if (a == c) {\n for (int j = min(b, d); j < max(b, d); j++) { f[a][j][3] = true; }\n for (int j = min(b, d) + 1; j <= max(b, d); j++) { f[a][j][1] = true; }\n } else {\n for (int j = min(a, c); j < max(a, c); j++) { f[j][b][0] = true; }\n for (int j = min(a, c) + 1; j <= max(a, c); j++) { f[j][b][2] = true; }\n }\n }\n\n for (int i = 0; i < t; i++) {\n int x;\n char c;\n cin >> x >> c;\n d.push_back(x);\n for (int j = 0; j < 4; j++)\n if (\"ESWN\"[j] == c) dir.push_back(j);\n }\n assert(d.size() == t && dir.size() == t);\n\n rep(i, 4) { cur.emplace_back(x0, y0, i); }\n\n for (int T = 0; T < t; T++) {\n memset(memo, false, sizeof(memo));\n for (auto [x, y, direc] : cur) { dfs(0, x, y, direc, T); }\n swap(cur, nxt);\n nxt.clear();\n }\n\n vector<P> ans;\n for (auto [x, y, direc] : cur) ans.emplace_back(x, y);\n sort(all(ans));\n ans.erase(unique(all(ans)), ans.end());\n\n for (auto [x, y] : ans) cout << x << ' ' << y << endl;\n\n return;\n}\n\nint main() {\n fastio();\n solve();\n // int t;\n // cin >> t;\n // while (t--) solve();\n\n // int t; cin >> t;\n // for(int i=1;i<=t;i++){\n // cout << \"Case #\" << i << \": \";\n // solve();\n // }\n return 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3624, "score_of_the_acc": -0.0132, "final_rank": 4 }, { "submission_id": "aoj_1349_5180323", "code_snippet": "#line 1 \"main.cpp\"\n#include <iostream>\n#include <vector>\n\nusing namespace std;\n\nconstexpr int X = 51;\nconstexpr int N = X * X;\n\nint enc(int x, int y) { return x * X + y; }\n\nvoid solve() {\n int n, x0, y0, t;\n cin >> n >> x0 >> y0 >> t;\n int s = enc(x0, y0);\n\n auto graph = vector(N, vector(4, -1));\n while (n--) {\n int sx, sy, gx, gy;\n cin >> sx >> sy >> gx >> gy;\n\n if (sx == gx) {\n int x = sx;\n if (sy > gy) swap(sy, gy);\n\n for (int y = sy; y < gy; ++y) {\n graph[enc(x, y)][0] = enc(x, y + 1);\n graph[enc(x, y + 1)][2] = enc(x, y);\n }\n } else {\n int y = sy;\n if (sx > gx) swap(sx, gx);\n\n for (int x = sx; x < gx; ++x) {\n graph[enc(x, y)][1] = enc(x + 1, y);\n graph[enc(x + 1, y)][3] = enc(x, y);\n }\n }\n }\n\n vector<int> ds{-1};\n while (t--) {\n int d;\n char c;\n cin >> d >> c;\n\n for (int i = 0; i < d - 1; ++i) {\n ds.push_back(-1);\n }\n\n int i = 0;\n while (c != \"NESW\"[i]) ++i;\n ds.push_back(i);\n }\n\n t = ds.size() - 1;\n\n auto dp = vector(4, vector(N, false));\n // 直前の移動方向、座標\n for (int d = 0; d < 4; ++d) dp[d][s] = true;\n\n for (int i = 0; i < t; ++i) {\n int td = ds[i];\n auto ndp = vector(4, vector(N, false));\n\n for (int v = 0; v < N; ++v) {\n for (int pd = 0; pd < 4; ++pd) {\n if (!dp[pd][v]) continue;\n\n for (int d = 0; d < 4; ++d) {\n // Uターン\n if (abs(d - pd) == 2) continue;\n // 向き指定\n if (td != -1 && pd != td && d != td) continue;\n // 道の存在\n if (graph[v][d] == -1) continue;\n\n ndp[d][graph[v][d]] = true;\n }\n }\n }\n swap(dp, ndp);\n }\n\n int td = ds.back();\n for (int v = 0; v < N; ++v) {\n bool ok = false;\n for (int d = 0; d < 4; ++d) {\n if (abs(td - d) == 2 || !dp[d][v]) continue;\n if (d == td || graph[v][td] != -1) ok = true;\n }\n if (ok) {\n cout << v / X << \" \" << v % X << \"\\n\";\n }\n }\n}\n\nint main() {\n cin.tie(nullptr);\n ios::sync_with_stdio(false);\n\n solve();\n\n return 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3616, "score_of_the_acc": -0.013, "final_rank": 3 }, { "submission_id": "aoj_1349_5175816", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n//#include <atcoder/segtree>\n//using namespace atcoder;\n\nusing ll = long long;\nusing ld = long double;\nusing pl = pair<ll, ll>;\nusing vl = vector<ll>;\nusing vvl = vector<vector<ll>>;\n#define rep(i,n) for (ll i = 0; i < (n); i++)\n#define all(v) (v).begin(), (v).end()\n#define sz(x) ((int)(x).size())\n#define fi first\n#define se second\n#define endl '\\n'\n#define IOS ios::sync_with_stdio(false); cin.tie(nullptr);\nconst int inf = 1<<30;\nconst ll INF = 1ll<<60;\nconst ll mod = 1000000007;\n//const ll mod = 998244353;\ntemplate<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }\ntemplate<class T>bool chmin(T &a, const T &b) { if (a>b) { a=b; return 1; } return 0; }\n\n\nbool G[1005][1005][4];\nbool st[1005][1005][4], nx[1005][1005][4], nx2[1005][1005][4];\nll d[105];\nchar c[105];\nll n, xx0, yy0, t, ds = 0;\n\nint main() {\n IOS;\n cin >> n >> xx0 >> yy0 >> t;\n rep(i,4) st[xx0+1][yy0+1][i] = true;\n // グラフをつくる\n rep(i,n) {\n ll x1, y1, x2, y2; cin >> x1 >> y1 >> x2 >> y2;\n x1++; y1++; x2++; y2++;\n if (x1 == x2) {\n if (y1 > y2) swap(y1, y2); y2 = min(y2, 1001ll);\n for (ll y = y1; y < y2; y++) {\n G[x1][y][1] = true;\n G[x1][y+1][3] = true;\n }\n } else {\n if (x1 > x2) swap(x1, x2); x2 = min(x2, 1001ll);\n for (ll x = x1; x < x2; x++) {\n G[x][y1][0] = true;\n G[x+1][y1][2] = true;\n }\n }\n }\n // 情報の入力\n rep(i,t) {\n cin >> d[i] >> c[i];\n ds += d[i];\n }\n ll id = 0, ds2 = 0, tar = -1; // 今いくつめの情報を処理しているか\n rep(tim,ds) {\n for (int i = 1; i <= 1001; i++) {\n for (int j = 1; j <= 1001; j++) {\n if (st[i][j][0]) {\n if (G[i][j][0]) nx[i+1][j][0] = 1;\n if (G[i][j][1]) nx[i][j+1][1] = 1;\n if (G[i][j][3]) nx[i][j-1][3] = 1;\n }\n if (st[i][j][1]) {\n if (G[i][j][0]) nx[i+1][j][0] = 1;\n if (G[i][j][1]) nx[i][j+1][1] = 1;\n if (G[i][j][2]) nx[i-1][j][2] = 1;\n }\n if (st[i][j][2]) {\n if (G[i][j][1]) nx[i][j+1][1] = 1;\n if (G[i][j][2]) nx[i-1][j][2] = 1;\n if (G[i][j][3]) nx[i][j-1][3] = 1;\n }\n if (st[i][j][3]) {\n if (G[i][j][0]) nx[i+1][j][0] = 1;\n if (G[i][j][2]) nx[i-1][j][2] = 1;\n if (G[i][j][3]) nx[i][j-1][3] = 1;\n }\n if (nx2[i][j][0]) nx[i+1][j][0] = 1;\n if (nx2[i][j][1]) nx[i][j+1][1] = 1;\n if (nx2[i][j][2]) nx[i-1][j][2] = 1;\n if (nx2[i][j][3]) nx[i][j-1][3] = 1;\n /*\n if (tar != -1) {\n int cnt = 0;\n if (nx2[i][j][0]) nx[i+1][j][0] = 1;\n if (nx2[i][j][1]) nx[i][j+1][1] = 1;\n if (nx2[i][j][2]) nx[i-1][j][2] = 1;\n if (nx2[i][j][3]) nx[i][j-1][3] = 1;\n bool ok = 0;\n rep(k,4) if (nx2[i][j][k]) ok = 1;\n if (!ok) continue;\n if (tar == 0 && nx2[i][j][tar]) nx[i+1][j][0] = 1;\n if (tar == 1 && nx2[i][j][tar]) nx[i][j+1][1] = 1;\n if (tar == 2 && nx2[i][j][tar]) nx[i-1][j][2] = 1;\n if (tar == 3 && nx2[i][j][tar]) nx[i][j-1][3] = 1;\n }\n */\n }\n }\n rep(i,1001) rep(j,1001) rep(k,4) {\n st[i+1][j+1][k] = nx[i+1][j+1][k];\n nx[i+1][j+1][k] = 0;\n nx2[i+1][j+1][k] = 0;\n }\n if (tar != -1) tar = -1;\n /*\n rep(k,4) {\n printf(\"time = %lld, k = %lld\\n\", tim, k);\n rep(i,10) {\n rep(j,10) cout << st[i+1][j+1][k] << \" \"; cout << endl;\n } \n }\n rep(i,10) {\n rep(j,10) {\n bool ruth = 0;\n rep(k,4) if (st[i+1][j+1][k] || nx2[i+1][j+1][k]) ruth = 1;\n if (ruth) cout << \"1 \";\n else cout << \"0 \";\n }\n cout << endl;\n } cout << endl;\n */\n\n if (tim+1-ds2 != d[id]) continue;\n // 情報と矛盾するものを排除する、これが難しい\n if (c[id] == 'E') tar = 0;\n if (c[id] == 'N') tar = 1;\n if (c[id] == 'W') tar = 2;\n if (c[id] == 'S') tar = 3;\n ds2 += d[id++];\n //cout << ds2 << endl;\n for (int i = 1; i <= 1001; i++) {\n for (int j = 1; j <= 1001; j++) {\n // 今tarを向いてないならfalseにする\n // 次の移動の始めにtarを向けるやつはnx2に避難させる\n rep(k,4) {\n if (k != tar) {\n if ((k+2)%4 != tar && G[i][j][tar] && st[i][j][k]) nx2[i][j][tar] = 1;\n st[i][j][k] = 0;\n }\n }\n }\n }\n }\n for (int i = 1; i <= 1001; i++) {\n for (int j = 1; j <= 1001; j++) {\n bool ok = 0; rep(k,4) if (st[i][j][k] || nx2[i][j][k]) ok = 1;\n if (ok) cout << i-1 << \" \" << j-1 << endl;\n }\n }\n return 0;\n}", "accuracy": 1, "time_ms": 4630, "memory_kb": 15200, "score_of_the_acc": -1.3419, "final_rank": 19 }, { "submission_id": "aoj_1349_5175804", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n//#include <atcoder/segtree>\n//using namespace atcoder;\n\nusing ll = long long;\nusing ld = long double;\nusing pl = pair<ll, ll>;\nusing vl = vector<ll>;\nusing vvl = vector<vector<ll>>;\n#define rep(i,n) for (ll i = 0; i < (n); i++)\n#define all(v) (v).begin(), (v).end()\n#define sz(x) ((int)(x).size())\n#define fi first\n#define se second\n#define endl '\\n'\n#define IOS ios::sync_with_stdio(false); cin.tie(nullptr);\nconst int inf = 1<<30;\nconst ll INF = 1ll<<60;\nconst ll mod = 1000000007;\n//const ll mod = 998244353;\ntemplate<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }\ntemplate<class T>bool chmin(T &a, const T &b) { if (a>b) { a=b; return 1; } return 0; }\n\n\nbool G[55][55][4];\nbool st[55][55][4], nx[55][55][4], nx2[55][55][4];\nll d[105];\nchar c[105];\nll n, xx0, yy0, t, ds = 0;\n\nint main() {\n //IOS;\n cin >> n >> xx0 >> yy0 >> t;\n rep(i,4) st[xx0+1][yy0+1][i] = true;\n // グラフをつくる\n rep(i,n) {\n ll x1, y1, x2, y2; cin >> x1 >> y1 >> x2 >> y2;\n x1++; y1++; x2++; y2++;\n if (x1 == x2) {\n if (y1 > y2) swap(y1, y2); y2 = min(y2, 51ll);\n for (ll y = y1; y < y2; y++) {\n G[x1][y][1] = true;\n G[x1][y+1][3] = true;\n }\n } else {\n if (x1 > x2) swap(x1, x2); x2 = min(x2, 51ll);\n for (ll x = x1; x < x2; x++) {\n G[x][y1][0] = true;\n G[x+1][y1][2] = true;\n }\n }\n }\n // 情報の入力\n rep(i,t) {\n cin >> d[i] >> c[i];\n ds += d[i];\n }\n ll id = 0, ds2 = 0, tar = -1; // 今いくつめの情報を処理しているか\n rep(tim,ds) {\n for (int i = 1; i <= 51; i++) {\n for (int j = 1; j <= 51; j++) {\n if (st[i][j][0]) {\n if (G[i][j][0]) nx[i+1][j][0] = 1;\n if (G[i][j][1]) nx[i][j+1][1] = 1;\n if (G[i][j][3]) nx[i][j-1][3] = 1;\n }\n if (st[i][j][1]) {\n if (G[i][j][0]) nx[i+1][j][0] = 1;\n if (G[i][j][1]) nx[i][j+1][1] = 1;\n if (G[i][j][2]) nx[i-1][j][2] = 1;\n }\n if (st[i][j][2]) {\n if (G[i][j][1]) nx[i][j+1][1] = 1;\n if (G[i][j][2]) nx[i-1][j][2] = 1;\n if (G[i][j][3]) nx[i][j-1][3] = 1;\n }\n if (st[i][j][3]) {\n if (G[i][j][0]) nx[i+1][j][0] = 1;\n if (G[i][j][2]) nx[i-1][j][2] = 1;\n if (G[i][j][3]) nx[i][j-1][3] = 1;\n }\n if (nx2[i][j][0]) nx[i+1][j][0] = 1;\n if (nx2[i][j][1]) nx[i][j+1][1] = 1;\n if (nx2[i][j][2]) nx[i-1][j][2] = 1;\n if (nx2[i][j][3]) nx[i][j-1][3] = 1;\n /*\n if (tar != -1) {\n int cnt = 0;\n if (nx2[i][j][0]) nx[i+1][j][0] = 1;\n if (nx2[i][j][1]) nx[i][j+1][1] = 1;\n if (nx2[i][j][2]) nx[i-1][j][2] = 1;\n if (nx2[i][j][3]) nx[i][j-1][3] = 1;\n bool ok = 0;\n rep(k,4) if (nx2[i][j][k]) ok = 1;\n if (!ok) continue;\n if (tar == 0 && nx2[i][j][tar]) nx[i+1][j][0] = 1;\n if (tar == 1 && nx2[i][j][tar]) nx[i][j+1][1] = 1;\n if (tar == 2 && nx2[i][j][tar]) nx[i-1][j][2] = 1;\n if (tar == 3 && nx2[i][j][tar]) nx[i][j-1][3] = 1;\n }\n */\n }\n }\n rep(i,51) rep(j,51) rep(k,4) {\n st[i+1][j+1][k] = nx[i+1][j+1][k];\n nx[i+1][j+1][k] = 0;\n nx2[i+1][j+1][k] = 0;\n }\n if (tar != -1) tar = -1;\n /*\n rep(k,4) {\n printf(\"time = %lld, k = %lld\\n\", tim, k);\n rep(i,10) {\n rep(j,10) cout << st[i+1][j+1][k] << \" \"; cout << endl;\n } \n }\n rep(i,10) {\n rep(j,10) {\n bool ruth = 0;\n rep(k,4) if (st[i+1][j+1][k] || nx2[i+1][j+1][k]) ruth = 1;\n if (ruth) cout << \"1 \";\n else cout << \"0 \";\n }\n cout << endl;\n } cout << endl;\n */\n\n if (tim+1-ds2 != d[id]) continue;\n // 情報と矛盾するものを排除する、これが難しい\n if (c[id] == 'E') tar = 0;\n if (c[id] == 'N') tar = 1;\n if (c[id] == 'W') tar = 2;\n if (c[id] == 'S') tar = 3;\n ds2 += d[id++];\n //cout << ds2 << endl;\n for (int i = 1; i <= 51; i++) {\n for (int j = 1; j <= 51; j++) {\n // 今tarを向いてないならfalseにする\n // 次の移動の始めにtarを向けるやつはnx2に避難させる\n rep(k,4) {\n if (k != tar) {\n if ((k+2)%4 != tar && G[i][j][tar] && st[i][j][k]) nx2[i][j][tar] = 1;\n st[i][j][k] = 0;\n }\n }\n }\n }\n }\n for (int i = 1; i <= 51; i++) {\n for (int j = 1; j <= 51; j++) {\n bool ok = 0; rep(k,4) if (st[i][j][k] || nx2[i][j][k]) ok = 1;\n if (ok) cout << i-1 << \" \" << j-1 << endl;\n }\n }\n\n return 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3160, "score_of_the_acc": 0, "final_rank": 1 }, { "submission_id": "aoj_1349_5175794", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n//#include <atcoder/segtree>\n//using namespace atcoder;\n\nusing ll = long long;\nusing ld = long double;\nusing pl = pair<ll, ll>;\nusing vl = vector<ll>;\nusing vvl = vector<vector<ll>>;\n#define rep(i,n) for (ll i = 0; i < (n); i++)\n#define all(v) (v).begin(), (v).end()\n#define sz(x) ((int)(x).size())\n#define fi first\n#define se second\n#define endl '\\n'\n#define IOS ios::sync_with_stdio(false); cin.tie(nullptr);\nconst int inf = 1<<30;\nconst ll INF = 1ll<<60;\nconst ll mod = 1000000007;\n//const ll mod = 998244353;\ntemplate<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }\ntemplate<class T>bool chmin(T &a, const T &b) { if (a>b) { a=b; return 1; } return 0; }\n\n\nbool G[1005][1005][4];\nbool st[1005][1005][4], nx[1005][1005][4], nx2[1005][1005][4];\nll d[105];\nchar c[105];\nll n, xx0, yy0, t, ds = 0;\n\nint main() {\n //IOS;\n cin >> n >> xx0 >> yy0 >> t;\n rep(i,4) st[xx0+1][yy0+1][i] = true;\n // グラフをつくる\n rep(i,n) {\n ll x1, y1, x2, y2; cin >> x1 >> y1 >> x2 >> y2;\n x1++; y1++; x2++; y2++;\n if (x1 == x2) {\n if (y1 > y2) swap(y1, y2); y2 = min(y2, 1001ll);\n for (ll y = y1; y < y2; y++) {\n G[x1][y][1] = true;\n G[x1][y+1][3] = true;\n }\n } else {\n if (x1 > x2) swap(x1, x2); x2 = min(x2, 1001ll);\n for (ll x = x1; x < x2; x++) {\n G[x][y1][0] = true;\n G[x+1][y1][2] = true;\n }\n }\n }\n // 情報の入力\n rep(i,t) {\n cin >> d[i] >> c[i];\n ds += d[i];\n }\n ll id = 0, ds2 = 0, tar = -1; // 今いくつめの情報を処理しているか\n rep(tim,ds) {\n for (int i = 1; i <= 1001; i++) {\n for (int j = 1; j <= 1001; j++) {\n if (st[i][j][0]) {\n if (G[i][j][0]) nx[i+1][j][0] = 1;\n if (G[i][j][1]) nx[i][j+1][1] = 1;\n if (G[i][j][3]) nx[i][j-1][3] = 1;\n }\n if (st[i][j][1]) {\n if (G[i][j][0]) nx[i+1][j][0] = 1;\n if (G[i][j][1]) nx[i][j+1][1] = 1;\n if (G[i][j][2]) nx[i-1][j][2] = 1;\n }\n if (st[i][j][2]) {\n if (G[i][j][1]) nx[i][j+1][1] = 1;\n if (G[i][j][2]) nx[i-1][j][2] = 1;\n if (G[i][j][3]) nx[i][j-1][3] = 1;\n }\n if (st[i][j][3]) {\n if (G[i][j][0]) nx[i+1][j][0] = 1;\n if (G[i][j][2]) nx[i-1][j][2] = 1;\n if (G[i][j][3]) nx[i][j-1][3] = 1;\n }\n if (nx2[i][j][0]) nx[i+1][j][0] = 1;\n if (nx2[i][j][1]) nx[i][j+1][1] = 1;\n if (nx2[i][j][2]) nx[i-1][j][2] = 1;\n if (nx2[i][j][3]) nx[i][j-1][3] = 1;\n /*\n if (tar != -1) {\n int cnt = 0;\n if (nx2[i][j][0]) nx[i+1][j][0] = 1;\n if (nx2[i][j][1]) nx[i][j+1][1] = 1;\n if (nx2[i][j][2]) nx[i-1][j][2] = 1;\n if (nx2[i][j][3]) nx[i][j-1][3] = 1;\n bool ok = 0;\n rep(k,4) if (nx2[i][j][k]) ok = 1;\n if (!ok) continue;\n if (tar == 0 && nx2[i][j][tar]) nx[i+1][j][0] = 1;\n if (tar == 1 && nx2[i][j][tar]) nx[i][j+1][1] = 1;\n if (tar == 2 && nx2[i][j][tar]) nx[i-1][j][2] = 1;\n if (tar == 3 && nx2[i][j][tar]) nx[i][j-1][3] = 1;\n }\n */\n }\n }\n rep(i,1001) rep(j,1001) rep(k,4) {\n st[i+1][j+1][k] = nx[i+1][j+1][k];\n nx[i+1][j+1][k] = 0;\n nx2[i+1][j+1][k] = 0;\n }\n if (tar != -1) tar = -1;\n /*\n rep(k,4) {\n printf(\"time = %lld, k = %lld\\n\", tim, k);\n rep(i,10) {\n rep(j,10) cout << st[i+1][j+1][k] << \" \"; cout << endl;\n } \n }\n rep(i,10) {\n rep(j,10) {\n bool ruth = 0;\n rep(k,4) if (st[i+1][j+1][k] || nx2[i+1][j+1][k]) ruth = 1;\n if (ruth) cout << \"1 \";\n else cout << \"0 \";\n }\n cout << endl;\n } cout << endl;\n */\n\n if (tim+1-ds2 != d[id]) continue;\n // 情報と矛盾するものを排除する、これが難しい\n if (c[id] == 'E') tar = 0;\n if (c[id] == 'N') tar = 1;\n if (c[id] == 'W') tar = 2;\n if (c[id] == 'S') tar = 3;\n ds2 += d[id++];\n //cout << ds2 << endl;\n for (int i = 1; i <= 1001; i++) {\n for (int j = 1; j <= 1001; j++) {\n // 今tarを向いてないならfalseにする\n // 次の移動の始めにtarを向けるやつはnx2に避難させる\n rep(k,4) {\n if (k != tar) {\n if ((k+2)%4 != tar && G[i][j][tar] && st[i][j][k]) nx2[i][j][tar] = 1;\n st[i][j][k] = 0;\n }\n }\n }\n }\n }\n for (int i = 1; i <= 1001; i++) {\n for (int j = 1; j <= 1001; j++) {\n bool ok = 0; rep(k,4) if (st[i][j][k] || nx2[i][j][k]) ok = 1;\n if (ok) cout << i-1 << \" \" << j-1 << endl;\n }\n }\n return 0;\n}", "accuracy": 1, "time_ms": 4440, "memory_kb": 15112, "score_of_the_acc": -1.2983, "final_rank": 18 } ]